1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * In-memory extent map for OCFS2. Man, this code was prettier in
9 * Copyright (C) 2004 Oracle. All rights reserved.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public
13 * License, version 2, as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
27 #include <linux/init.h>
28 #include <linux/types.h>
29 #include <linux/slab.h>
30 #include <linux/rbtree.h>
32 #define MLOG_MASK_PREFIX ML_EXTENT_MAP
33 #include <cluster/masklog.h>
37 #include "extent_map.h"
41 #include "buffer_head_io.h"
46 * Our headers are so bad that struct ocfs2_extent_map is in ocfs.h
49 struct ocfs2_extent_map_entry
{
50 struct rb_node e_node
;
52 struct ocfs2_extent_rec e_rec
;
55 struct ocfs2_em_insert_context
{
58 struct ocfs2_extent_map_entry
*new_ent
;
59 struct ocfs2_extent_map_entry
*old_ent
;
60 struct ocfs2_extent_map_entry
*left_ent
;
61 struct ocfs2_extent_map_entry
*right_ent
;
64 static struct kmem_cache
*ocfs2_em_ent_cachep
= NULL
;
67 static struct ocfs2_extent_map_entry
*
68 ocfs2_extent_map_lookup(struct ocfs2_extent_map
*em
,
69 u32 cpos
, u32 clusters
,
70 struct rb_node
***ret_p
,
71 struct rb_node
**ret_parent
);
72 static int ocfs2_extent_map_insert(struct inode
*inode
,
73 struct ocfs2_extent_rec
*rec
,
75 static int ocfs2_extent_map_insert_entry(struct ocfs2_extent_map
*em
,
76 struct ocfs2_extent_map_entry
*ent
);
77 static int ocfs2_extent_map_find_leaf(struct inode
*inode
,
78 u32 cpos
, u32 clusters
,
79 struct ocfs2_extent_list
*el
);
80 static int ocfs2_extent_map_lookup_read(struct inode
*inode
,
81 u32 cpos
, u32 clusters
,
82 struct ocfs2_extent_map_entry
**ret_ent
);
83 static int ocfs2_extent_map_try_insert(struct inode
*inode
,
84 struct ocfs2_extent_rec
*rec
,
86 struct ocfs2_em_insert_context
*ctxt
);
88 /* returns 1 only if the rec contains all the given clusters -- that is that
89 * rec's cpos is <= the cluster cpos and that the rec endpoint (cpos +
90 * clusters) is >= the argument's endpoint */
91 static int ocfs2_extent_rec_contains_clusters(struct ocfs2_extent_rec
*rec
,
92 u32 cpos
, u32 clusters
)
94 if (le32_to_cpu(rec
->e_cpos
) > cpos
)
96 if (cpos
+ clusters
> le32_to_cpu(rec
->e_cpos
) +
97 le32_to_cpu(rec
->e_clusters
))
104 * Find an entry in the tree that intersects the region passed in.
105 * Note that this will find straddled intervals, it is up to the
106 * callers to enforce any boundary conditions.
108 * Callers must hold ip_lock. This lookup is not guaranteed to return
109 * a tree_depth 0 match, and as such can race inserts if the lock
112 * The rb_node garbage lets insertion share the search. Trivial
115 static struct ocfs2_extent_map_entry
*
116 ocfs2_extent_map_lookup(struct ocfs2_extent_map
*em
,
117 u32 cpos
, u32 clusters
,
118 struct rb_node
***ret_p
,
119 struct rb_node
**ret_parent
)
121 struct rb_node
**p
= &em
->em_extents
.rb_node
;
122 struct rb_node
*parent
= NULL
;
123 struct ocfs2_extent_map_entry
*ent
= NULL
;
128 ent
= rb_entry(parent
, struct ocfs2_extent_map_entry
,
130 if ((cpos
+ clusters
) <= le32_to_cpu(ent
->e_rec
.e_cpos
)) {
133 } else if (cpos
>= (le32_to_cpu(ent
->e_rec
.e_cpos
) +
134 le32_to_cpu(ent
->e_rec
.e_clusters
))) {
143 if (ret_parent
!= NULL
)
144 *ret_parent
= parent
;
149 * Find the leaf containing the interval we want. While we're on our
150 * way down the tree, fill in every record we see at any depth, because
151 * we might want it later.
153 * Note that this code is run without ip_lock. That's because it
154 * sleeps while reading. If someone is also filling the extent list at
155 * the same time we are, we might have to restart.
157 static int ocfs2_extent_map_find_leaf(struct inode
*inode
,
158 u32 cpos
, u32 clusters
,
159 struct ocfs2_extent_list
*el
)
162 struct buffer_head
*eb_bh
= NULL
;
165 struct ocfs2_extent_block
*eb
;
166 struct ocfs2_extent_rec
*rec
;
169 * The bh data containing the el cannot change here, because
170 * we hold alloc_sem. So we can do this without other
173 while (el
->l_tree_depth
)
176 for (i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
177 rec
= &el
->l_recs
[i
];
178 rec_end
= (le32_to_cpu(rec
->e_cpos
) +
179 le32_to_cpu(rec
->e_clusters
));
182 if (rec_end
> OCFS2_I(inode
)->ip_clusters
) {
184 ocfs2_error(inode
->i_sb
,
185 "Extent %d at e_blkno %llu of inode %llu goes past ip_clusters of %u\n",
187 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
188 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
189 OCFS2_I(inode
)->ip_clusters
);
193 if (rec_end
<= cpos
) {
194 ret
= ocfs2_extent_map_insert(inode
, rec
,
195 le16_to_cpu(el
->l_tree_depth
));
196 if (ret
&& (ret
!= -EEXIST
)) {
202 if ((cpos
+ clusters
) <= le32_to_cpu(rec
->e_cpos
)) {
203 ret
= ocfs2_extent_map_insert(inode
, rec
,
204 le16_to_cpu(el
->l_tree_depth
));
205 if (ret
&& (ret
!= -EEXIST
)) {
213 * We've found a record that matches our
214 * interval. We don't insert it because we're
215 * about to traverse it.
218 /* Check to see if we're stradling */
220 if (!ocfs2_extent_rec_contains_clusters(rec
,
228 * If we've already found a record, the el has
229 * two records covering the same interval.
235 ocfs2_error(inode
->i_sb
,
236 "Multiple extents for (cpos = %u, clusters = %u) on inode %llu; e_blkno %llu and rec %d at e_blkno %llu\n",
238 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
239 (unsigned long long)blkno
, i
,
240 (unsigned long long)le64_to_cpu(rec
->e_blkno
));
244 blkno
= le64_to_cpu(rec
->e_blkno
);
248 * We don't support holes, and we're still up
249 * in the branches, so we'd better have found someone
253 ocfs2_error(inode
->i_sb
,
254 "No record found for (cpos = %u, clusters = %u) on inode %llu\n",
256 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
265 ret
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
),
266 blkno
, &eb_bh
, OCFS2_BH_CACHED
,
272 eb
= (struct ocfs2_extent_block
*)eb_bh
->b_data
;
273 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
274 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
281 BUG_ON(el
->l_tree_depth
);
283 for (i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
284 rec
= &el
->l_recs
[i
];
286 if ((le32_to_cpu(rec
->e_cpos
) + le32_to_cpu(rec
->e_clusters
)) >
287 OCFS2_I(inode
)->ip_clusters
) {
290 ocfs2_error(inode
->i_sb
,
291 "Extent %d at e_blkno %llu of inode %llu goes past ip_clusters of %u\n",
293 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
294 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
295 OCFS2_I(inode
)->ip_clusters
);
299 ret
= ocfs2_extent_map_insert(inode
, rec
,
300 le16_to_cpu(el
->l_tree_depth
));
301 if (ret
&& (ret
!= -EEXIST
)) {
317 * This lookup actually will read from disk. It has one invariant:
318 * It will never re-traverse blocks. This means that all inserts should
319 * be new regions or more granular regions (both allowed by insert).
321 static int ocfs2_extent_map_lookup_read(struct inode
*inode
,
324 struct ocfs2_extent_map_entry
**ret_ent
)
328 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
329 struct ocfs2_extent_map_entry
*ent
;
330 struct buffer_head
*bh
= NULL
;
331 struct ocfs2_extent_block
*eb
;
332 struct ocfs2_dinode
*di
;
333 struct ocfs2_extent_list
*el
;
335 spin_lock(&OCFS2_I(inode
)->ip_lock
);
336 ent
= ocfs2_extent_map_lookup(em
, cpos
, clusters
, NULL
, NULL
);
338 if (!ent
->e_tree_depth
) {
339 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
343 blkno
= le64_to_cpu(ent
->e_rec
.e_blkno
);
344 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
346 ret
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
), blkno
, &bh
,
347 OCFS2_BH_CACHED
, inode
);
354 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
355 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
356 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode
->i_sb
, eb
);
362 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
364 ret
= ocfs2_read_block(OCFS2_SB(inode
->i_sb
),
365 OCFS2_I(inode
)->ip_blkno
, &bh
,
366 OCFS2_BH_CACHED
, inode
);
373 di
= (struct ocfs2_dinode
*)bh
->b_data
;
374 if (!OCFS2_IS_VALID_DINODE(di
)) {
376 OCFS2_RO_ON_INVALID_DINODE(inode
->i_sb
, di
);
379 el
= &di
->id2
.i_list
;
382 ret
= ocfs2_extent_map_find_leaf(inode
, cpos
, clusters
, el
);
389 ent
= ocfs2_extent_map_lookup(em
, cpos
, clusters
, NULL
, NULL
);
396 /* FIXME: Make sure this isn't a corruption */
397 BUG_ON(ent
->e_tree_depth
);
405 * Callers must hold ip_lock. This can insert pieces of the tree,
406 * thus racing lookup if the lock weren't held.
408 static int ocfs2_extent_map_insert_entry(struct ocfs2_extent_map
*em
,
409 struct ocfs2_extent_map_entry
*ent
)
411 struct rb_node
**p
, *parent
;
412 struct ocfs2_extent_map_entry
*old_ent
;
414 old_ent
= ocfs2_extent_map_lookup(em
, le32_to_cpu(ent
->e_rec
.e_cpos
),
415 le32_to_cpu(ent
->e_rec
.e_clusters
),
420 rb_link_node(&ent
->e_node
, parent
, p
);
421 rb_insert_color(&ent
->e_node
, &em
->em_extents
);
428 * Simple rule: on any return code other than -EAGAIN, anything left
429 * in the insert_context will be freed.
431 * Simple rule #2: A return code of -EEXIST from this function or
432 * its calls to ocfs2_extent_map_insert_entry() signifies that another
433 * thread beat us to the insert. It is not an actual error, but it
434 * tells the caller we have no more work to do.
436 static int ocfs2_extent_map_try_insert(struct inode
*inode
,
437 struct ocfs2_extent_rec
*rec
,
439 struct ocfs2_em_insert_context
*ctxt
)
442 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
443 struct ocfs2_extent_map_entry
*old_ent
;
446 ctxt
->need_right
= 0;
447 ctxt
->old_ent
= NULL
;
449 spin_lock(&OCFS2_I(inode
)->ip_lock
);
450 ret
= ocfs2_extent_map_insert_entry(em
, ctxt
->new_ent
);
452 ctxt
->new_ent
= NULL
;
456 /* Since insert_entry failed, the map MUST have old_ent */
457 old_ent
= ocfs2_extent_map_lookup(em
, le32_to_cpu(rec
->e_cpos
),
458 le32_to_cpu(rec
->e_clusters
),
463 if (old_ent
->e_tree_depth
< tree_depth
) {
464 /* Another thread beat us to the lower tree_depth */
469 if (old_ent
->e_tree_depth
== tree_depth
) {
471 * Another thread beat us to this tree_depth.
472 * Let's make sure we agree with that thread (the
473 * extent_rec should be identical).
475 if (!memcmp(rec
, &old_ent
->e_rec
,
476 sizeof(struct ocfs2_extent_rec
)))
479 /* FIXME: Should this be ESRCH/EBADR??? */
486 * We do it in this order specifically so that no actual tree
487 * changes occur until we have all the pieces we need. We
488 * don't want malloc failures to leave an inconsistent tree.
489 * Whenever we drop the lock, another process could be
490 * inserting. Also note that, if another process just beat us
491 * to an insert, we might not need the same pieces we needed
492 * the first go round. In the end, the pieces we need will
493 * be used, and the pieces we don't will be freed.
495 ctxt
->need_left
= !!(le32_to_cpu(rec
->e_cpos
) >
496 le32_to_cpu(old_ent
->e_rec
.e_cpos
));
497 ctxt
->need_right
= !!((le32_to_cpu(old_ent
->e_rec
.e_cpos
) +
498 le32_to_cpu(old_ent
->e_rec
.e_clusters
)) >
499 (le32_to_cpu(rec
->e_cpos
) + le32_to_cpu(rec
->e_clusters
)));
501 if (ctxt
->need_left
) {
504 *(ctxt
->left_ent
) = *old_ent
;
505 ctxt
->left_ent
->e_rec
.e_clusters
=
506 cpu_to_le32(le32_to_cpu(rec
->e_cpos
) -
507 le32_to_cpu(ctxt
->left_ent
->e_rec
.e_cpos
));
509 if (ctxt
->need_right
) {
510 if (!ctxt
->right_ent
)
512 *(ctxt
->right_ent
) = *old_ent
;
513 ctxt
->right_ent
->e_rec
.e_cpos
=
514 cpu_to_le32(le32_to_cpu(rec
->e_cpos
) +
515 le32_to_cpu(rec
->e_clusters
));
516 ctxt
->right_ent
->e_rec
.e_clusters
=
517 cpu_to_le32((le32_to_cpu(old_ent
->e_rec
.e_cpos
) +
518 le32_to_cpu(old_ent
->e_rec
.e_clusters
)) -
519 le32_to_cpu(ctxt
->right_ent
->e_rec
.e_cpos
));
522 rb_erase(&old_ent
->e_node
, &em
->em_extents
);
523 /* Now that he's erased, set him up for deletion */
524 ctxt
->old_ent
= old_ent
;
526 if (ctxt
->need_left
) {
527 ret
= ocfs2_extent_map_insert_entry(em
,
531 ctxt
->left_ent
= NULL
;
534 if (ctxt
->need_right
) {
535 ret
= ocfs2_extent_map_insert_entry(em
,
539 ctxt
->right_ent
= NULL
;
542 ret
= ocfs2_extent_map_insert_entry(em
, ctxt
->new_ent
);
545 ctxt
->new_ent
= NULL
;
548 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
554 static int ocfs2_extent_map_insert(struct inode
*inode
,
555 struct ocfs2_extent_rec
*rec
,
559 struct ocfs2_em_insert_context ctxt
= {0, };
561 if ((le32_to_cpu(rec
->e_cpos
) + le32_to_cpu(rec
->e_clusters
)) >
562 OCFS2_I(inode
)->ip_map
.em_clusters
) {
568 /* Zero e_clusters means a truncated tail record. It better be EOF */
569 if (!rec
->e_clusters
) {
570 if ((le32_to_cpu(rec
->e_cpos
) + le32_to_cpu(rec
->e_clusters
)) !=
571 OCFS2_I(inode
)->ip_map
.em_clusters
) {
574 ocfs2_error(inode
->i_sb
,
575 "Zero e_clusters on non-tail extent record at e_blkno %llu on inode %llu\n",
576 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
577 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
581 /* Ignore the truncated tail */
586 ctxt
.new_ent
= kmem_cache_alloc(ocfs2_em_ent_cachep
,
593 ctxt
.new_ent
->e_rec
= *rec
;
594 ctxt
.new_ent
->e_tree_depth
= tree_depth
;
598 if (ctxt
.need_left
&& !ctxt
.left_ent
) {
600 kmem_cache_alloc(ocfs2_em_ent_cachep
,
605 if (ctxt
.need_right
&& !ctxt
.right_ent
) {
607 kmem_cache_alloc(ocfs2_em_ent_cachep
,
613 ret
= ocfs2_extent_map_try_insert(inode
, rec
,
615 } while (ret
== -EAGAIN
);
617 if ((ret
< 0) && (ret
!= -EEXIST
))
621 kmem_cache_free(ocfs2_em_ent_cachep
, ctxt
.left_ent
);
623 kmem_cache_free(ocfs2_em_ent_cachep
, ctxt
.right_ent
);
625 kmem_cache_free(ocfs2_em_ent_cachep
, ctxt
.old_ent
);
627 kmem_cache_free(ocfs2_em_ent_cachep
, ctxt
.new_ent
);
633 * Append this record to the tail of the extent map. It must be
634 * tree_depth 0. The record might be an extension of an existing
635 * record, and as such that needs to be handled. eg:
637 * Existing record in the extent map:
639 * cpos = 10, len = 10
644 * cpos = 10, len = 20
645 * |------------------|
647 * The passed record is the new on-disk record. The new_clusters value
648 * is how many clusters were added to the file. If the append is a
649 * contiguous append, the new_clusters has been added to
650 * rec->e_clusters. If the append is an entirely new extent, then
651 * rec->e_clusters is == new_clusters.
653 int ocfs2_extent_map_append(struct inode
*inode
,
654 struct ocfs2_extent_rec
*rec
,
658 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
659 struct ocfs2_extent_map_entry
*ent
;
660 struct ocfs2_extent_rec
*old
;
662 BUG_ON(!new_clusters
);
663 BUG_ON(le32_to_cpu(rec
->e_clusters
) < new_clusters
);
665 if (em
->em_clusters
< OCFS2_I(inode
)->ip_clusters
) {
667 * Size changed underneath us on disk. Drop any
668 * straddling records and update our idea of
671 ocfs2_extent_map_drop(inode
, em
->em_clusters
- 1);
672 em
->em_clusters
= OCFS2_I(inode
)->ip_clusters
;
675 mlog_bug_on_msg((le32_to_cpu(rec
->e_cpos
) +
676 le32_to_cpu(rec
->e_clusters
)) !=
677 (em
->em_clusters
+ new_clusters
),
679 "rec->e_cpos = %u + rec->e_clusters = %u = %u\n"
680 "em->em_clusters = %u + new_clusters = %u = %u\n",
681 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
682 le32_to_cpu(rec
->e_cpos
), le32_to_cpu(rec
->e_clusters
),
683 le32_to_cpu(rec
->e_cpos
) + le32_to_cpu(rec
->e_clusters
),
684 em
->em_clusters
, new_clusters
,
685 em
->em_clusters
+ new_clusters
);
687 em
->em_clusters
+= new_clusters
;
690 if (le32_to_cpu(rec
->e_clusters
) > new_clusters
) {
691 /* This is a contiguous append */
692 ent
= ocfs2_extent_map_lookup(em
, le32_to_cpu(rec
->e_cpos
), 1,
696 BUG_ON((le32_to_cpu(rec
->e_cpos
) +
697 le32_to_cpu(rec
->e_clusters
)) !=
698 (le32_to_cpu(old
->e_cpos
) +
699 le32_to_cpu(old
->e_clusters
) +
701 if (ent
->e_tree_depth
== 0) {
702 BUG_ON(le32_to_cpu(old
->e_cpos
) !=
703 le32_to_cpu(rec
->e_cpos
));
704 BUG_ON(le64_to_cpu(old
->e_blkno
) !=
705 le64_to_cpu(rec
->e_blkno
));
709 * Let non-leafs fall through as -ENOENT to
710 * force insertion of the new leaf.
712 le32_add_cpu(&old
->e_clusters
, new_clusters
);
717 ret
= ocfs2_extent_map_insert(inode
, rec
, 0);
724 /* Code here is included but defined out as it completes the extent
725 * map api and may be used in the future. */
728 * Look up the record containing this cluster offset. This record is
729 * part of the extent map. Do not free it. Any changes you make to
730 * it will reflect in the extent map. So, if your last extent
731 * is (cpos = 10, clusters = 10) and you truncate the file by 5
732 * clusters, you can do:
734 * ret = ocfs2_extent_map_get_rec(em, orig_size - 5, &rec);
735 * rec->e_clusters -= 5;
737 * The lookup does not read from disk. If the map isn't filled in for
738 * an entry, you won't find it.
740 * Also note that the returned record is valid until alloc_sem is
741 * dropped. After that, truncate and extend can happen. Caveat Emptor.
743 int ocfs2_extent_map_get_rec(struct inode
*inode
, u32 cpos
,
744 struct ocfs2_extent_rec
**rec
,
748 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
749 struct ocfs2_extent_map_entry
*ent
;
753 if (cpos
>= OCFS2_I(inode
)->ip_clusters
)
756 if (cpos
>= em
->em_clusters
) {
758 * Size changed underneath us on disk. Drop any
759 * straddling records and update our idea of
762 ocfs2_extent_map_drop(inode
, em
->em_clusters
- 1);
763 em
->em_clusters
= OCFS2_I(inode
)->ip_clusters
;
766 ent
= ocfs2_extent_map_lookup(&OCFS2_I(inode
)->ip_map
, cpos
, 1,
772 *tree_depth
= ent
->e_tree_depth
;
779 int ocfs2_extent_map_get_clusters(struct inode
*inode
,
780 u32 v_cpos
, int count
,
781 u32
*p_cpos
, int *ret_count
)
785 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
786 struct ocfs2_extent_map_entry
*ent
= NULL
;
788 *p_cpos
= ccount
= 0;
790 if ((v_cpos
+ count
) > OCFS2_I(inode
)->ip_clusters
)
793 if ((v_cpos
+ count
) > em
->em_clusters
) {
795 * Size changed underneath us on disk. Drop any
796 * straddling records and update our idea of
799 ocfs2_extent_map_drop(inode
, em
->em_clusters
- 1);
800 em
->em_clusters
= OCFS2_I(inode
)->ip_clusters
;
804 ret
= ocfs2_extent_map_lookup_read(inode
, v_cpos
, count
, &ent
);
809 /* We should never find ourselves straddling an interval */
810 if (!ocfs2_extent_rec_contains_clusters(&ent
->e_rec
,
815 coff
= v_cpos
- le32_to_cpu(ent
->e_rec
.e_cpos
);
816 *p_cpos
= ocfs2_blocks_to_clusters(inode
->i_sb
,
817 le64_to_cpu(ent
->e_rec
.e_blkno
)) +
821 *ret_count
= le32_to_cpu(ent
->e_rec
.e_clusters
) - coff
;
832 int ocfs2_extent_map_get_blocks(struct inode
*inode
,
833 u64 v_blkno
, int count
,
834 u64
*p_blkno
, int *ret_count
)
839 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
840 struct ocfs2_extent_map_entry
*ent
= NULL
;
841 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
842 struct ocfs2_extent_rec
*rec
;
846 cpos
= ocfs2_blocks_to_clusters(inode
->i_sb
, v_blkno
);
847 clusters
= ocfs2_blocks_to_clusters(inode
->i_sb
,
848 (u64
)count
+ bpc
- 1);
849 if ((cpos
+ clusters
) > OCFS2_I(inode
)->ip_clusters
) {
855 if ((cpos
+ clusters
) > em
->em_clusters
) {
857 * Size changed underneath us on disk. Drop any
858 * straddling records and update our idea of
861 ocfs2_extent_map_drop(inode
, em
->em_clusters
- 1);
862 em
->em_clusters
= OCFS2_I(inode
)->ip_clusters
;
865 ret
= ocfs2_extent_map_lookup_read(inode
, cpos
, clusters
, &ent
);
875 /* We should never find ourselves straddling an interval */
876 if (!ocfs2_extent_rec_contains_clusters(rec
, cpos
, clusters
)) {
882 boff
= ocfs2_clusters_to_blocks(inode
->i_sb
, cpos
-
883 le32_to_cpu(rec
->e_cpos
));
884 boff
+= (v_blkno
& (u64
)(bpc
- 1));
885 *p_blkno
= le64_to_cpu(rec
->e_blkno
) + boff
;
888 *ret_count
= ocfs2_clusters_to_blocks(inode
->i_sb
,
889 le32_to_cpu(rec
->e_clusters
)) - boff
;
898 int ocfs2_extent_map_init(struct inode
*inode
)
900 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
902 em
->em_extents
= RB_ROOT
;
909 static void __ocfs2_extent_map_drop(struct inode
*inode
,
911 struct rb_node
**free_head
,
912 struct ocfs2_extent_map_entry
**tail_ent
)
914 struct rb_node
*node
, *next
;
915 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
916 struct ocfs2_extent_map_entry
*ent
;
921 node
= rb_last(&em
->em_extents
);
924 next
= rb_prev(node
);
926 ent
= rb_entry(node
, struct ocfs2_extent_map_entry
,
928 if (le32_to_cpu(ent
->e_rec
.e_cpos
) < new_clusters
)
931 rb_erase(&ent
->e_node
, &em
->em_extents
);
933 node
->rb_right
= *free_head
;
940 /* Do we have an entry straddling new_clusters? */
943 ((le32_to_cpu(ent
->e_rec
.e_cpos
) +
944 le32_to_cpu(ent
->e_rec
.e_clusters
)) > new_clusters
))
951 static void __ocfs2_extent_map_drop_cleanup(struct rb_node
*free_head
)
953 struct rb_node
*node
;
954 struct ocfs2_extent_map_entry
*ent
;
958 free_head
= node
->rb_right
;
960 ent
= rb_entry(node
, struct ocfs2_extent_map_entry
,
962 kmem_cache_free(ocfs2_em_ent_cachep
, ent
);
967 * Remove all entries past new_clusters, inclusive of an entry that
968 * contains new_clusters. This is effectively a cache forget.
970 * If you want to also clip the last extent by some number of clusters,
971 * you need to call ocfs2_extent_map_trunc().
972 * This code does not check or modify ip_clusters.
974 int ocfs2_extent_map_drop(struct inode
*inode
, u32 new_clusters
)
976 struct rb_node
*free_head
= NULL
;
977 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
978 struct ocfs2_extent_map_entry
*ent
;
980 spin_lock(&OCFS2_I(inode
)->ip_lock
);
982 __ocfs2_extent_map_drop(inode
, new_clusters
, &free_head
, &ent
);
985 rb_erase(&ent
->e_node
, &em
->em_extents
);
986 ent
->e_node
.rb_right
= free_head
;
987 free_head
= &ent
->e_node
;
990 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
993 __ocfs2_extent_map_drop_cleanup(free_head
);
999 * Remove all entries past new_clusters and also clip any extent
1000 * straddling new_clusters, if there is one. This does not check
1001 * or modify ip_clusters
1003 int ocfs2_extent_map_trunc(struct inode
*inode
, u32 new_clusters
)
1005 struct rb_node
*free_head
= NULL
;
1006 struct ocfs2_extent_map_entry
*ent
= NULL
;
1008 spin_lock(&OCFS2_I(inode
)->ip_lock
);
1010 __ocfs2_extent_map_drop(inode
, new_clusters
, &free_head
, &ent
);
1013 ent
->e_rec
.e_clusters
= cpu_to_le32(new_clusters
-
1014 le32_to_cpu(ent
->e_rec
.e_cpos
));
1016 OCFS2_I(inode
)->ip_map
.em_clusters
= new_clusters
;
1018 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
1021 __ocfs2_extent_map_drop_cleanup(free_head
);
1026 int __init
init_ocfs2_extent_maps(void)
1028 ocfs2_em_ent_cachep
=
1029 kmem_cache_create("ocfs2_em_ent",
1030 sizeof(struct ocfs2_extent_map_entry
),
1031 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
1032 if (!ocfs2_em_ent_cachep
)
1038 void exit_ocfs2_extent_maps(void)
1040 kmem_cache_destroy(ocfs2_em_ent_cachep
);