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 kmem_cache_t
*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
));
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 static int ocfs2_extent_map_try_insert(struct inode
*inode
,
432 struct ocfs2_extent_rec
*rec
,
434 struct ocfs2_em_insert_context
*ctxt
)
437 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
438 struct ocfs2_extent_map_entry
*old_ent
;
441 ctxt
->need_right
= 0;
442 ctxt
->old_ent
= NULL
;
444 spin_lock(&OCFS2_I(inode
)->ip_lock
);
445 ret
= ocfs2_extent_map_insert_entry(em
, ctxt
->new_ent
);
447 ctxt
->new_ent
= NULL
;
451 old_ent
= ocfs2_extent_map_lookup(em
, le32_to_cpu(rec
->e_cpos
),
452 le32_to_cpu(rec
->e_clusters
), NULL
,
458 if (old_ent
->e_tree_depth
< tree_depth
)
461 if (old_ent
->e_tree_depth
== tree_depth
) {
462 if (!memcmp(rec
, &old_ent
->e_rec
,
463 sizeof(struct ocfs2_extent_rec
)))
466 /* FIXME: Should this be ESRCH/EBADR??? */
471 * We do it in this order specifically so that no actual tree
472 * changes occur until we have all the pieces we need. We
473 * don't want malloc failures to leave an inconsistent tree.
474 * Whenever we drop the lock, another process could be
475 * inserting. Also note that, if another process just beat us
476 * to an insert, we might not need the same pieces we needed
477 * the first go round. In the end, the pieces we need will
478 * be used, and the pieces we don't will be freed.
480 ctxt
->need_left
= !!(le32_to_cpu(rec
->e_cpos
) >
481 le32_to_cpu(old_ent
->e_rec
.e_cpos
));
482 ctxt
->need_right
= !!((le32_to_cpu(old_ent
->e_rec
.e_cpos
) +
483 le32_to_cpu(old_ent
->e_rec
.e_clusters
)) >
484 (le32_to_cpu(rec
->e_cpos
) + le32_to_cpu(rec
->e_clusters
)));
486 if (ctxt
->need_left
) {
489 *(ctxt
->left_ent
) = *old_ent
;
490 ctxt
->left_ent
->e_rec
.e_clusters
=
491 cpu_to_le32(le32_to_cpu(rec
->e_cpos
) -
492 le32_to_cpu(ctxt
->left_ent
->e_rec
.e_cpos
));
494 if (ctxt
->need_right
) {
495 if (!ctxt
->right_ent
)
497 *(ctxt
->right_ent
) = *old_ent
;
498 ctxt
->right_ent
->e_rec
.e_cpos
=
499 cpu_to_le32(le32_to_cpu(rec
->e_cpos
) +
500 le32_to_cpu(rec
->e_clusters
));
501 ctxt
->right_ent
->e_rec
.e_clusters
=
502 cpu_to_le32((le32_to_cpu(old_ent
->e_rec
.e_cpos
) +
503 le32_to_cpu(old_ent
->e_rec
.e_clusters
)) -
504 le32_to_cpu(ctxt
->right_ent
->e_rec
.e_cpos
));
507 rb_erase(&old_ent
->e_node
, &em
->em_extents
);
508 /* Now that he's erased, set him up for deletion */
509 ctxt
->old_ent
= old_ent
;
511 if (ctxt
->need_left
) {
512 ret
= ocfs2_extent_map_insert_entry(em
,
516 ctxt
->left_ent
= NULL
;
519 if (ctxt
->need_right
) {
520 ret
= ocfs2_extent_map_insert_entry(em
,
524 ctxt
->right_ent
= NULL
;
527 ret
= ocfs2_extent_map_insert_entry(em
, ctxt
->new_ent
);
530 ctxt
->new_ent
= NULL
;
533 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
539 static int ocfs2_extent_map_insert(struct inode
*inode
,
540 struct ocfs2_extent_rec
*rec
,
544 struct ocfs2_em_insert_context ctxt
= {0, };
546 if ((le32_to_cpu(rec
->e_cpos
) + le32_to_cpu(rec
->e_clusters
)) >
547 OCFS2_I(inode
)->ip_map
.em_clusters
) {
553 /* Zero e_clusters means a truncated tail record. It better be EOF */
554 if (!rec
->e_clusters
) {
555 if ((le32_to_cpu(rec
->e_cpos
) + le32_to_cpu(rec
->e_clusters
)) !=
556 OCFS2_I(inode
)->ip_map
.em_clusters
) {
559 ocfs2_error(inode
->i_sb
,
560 "Zero e_clusters on non-tail extent record at e_blkno %llu on inode %llu\n",
561 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
562 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
566 /* Ignore the truncated tail */
571 ctxt
.new_ent
= kmem_cache_alloc(ocfs2_em_ent_cachep
,
578 ctxt
.new_ent
->e_rec
= *rec
;
579 ctxt
.new_ent
->e_tree_depth
= tree_depth
;
583 if (ctxt
.need_left
&& !ctxt
.left_ent
) {
585 kmem_cache_alloc(ocfs2_em_ent_cachep
,
590 if (ctxt
.need_right
&& !ctxt
.right_ent
) {
592 kmem_cache_alloc(ocfs2_em_ent_cachep
,
598 ret
= ocfs2_extent_map_try_insert(inode
, rec
,
600 } while (ret
== -EAGAIN
);
606 kmem_cache_free(ocfs2_em_ent_cachep
, ctxt
.left_ent
);
608 kmem_cache_free(ocfs2_em_ent_cachep
, ctxt
.right_ent
);
610 kmem_cache_free(ocfs2_em_ent_cachep
, ctxt
.old_ent
);
612 kmem_cache_free(ocfs2_em_ent_cachep
, ctxt
.new_ent
);
618 * Append this record to the tail of the extent map. It must be
619 * tree_depth 0. The record might be an extension of an existing
620 * record, and as such that needs to be handled. eg:
622 * Existing record in the extent map:
624 * cpos = 10, len = 10
629 * cpos = 10, len = 20
630 * |------------------|
632 * The passed record is the new on-disk record. The new_clusters value
633 * is how many clusters were added to the file. If the append is a
634 * contiguous append, the new_clusters has been added to
635 * rec->e_clusters. If the append is an entirely new extent, then
636 * rec->e_clusters is == new_clusters.
638 int ocfs2_extent_map_append(struct inode
*inode
,
639 struct ocfs2_extent_rec
*rec
,
643 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
644 struct ocfs2_extent_map_entry
*ent
;
645 struct ocfs2_extent_rec
*old
;
647 BUG_ON(!new_clusters
);
648 BUG_ON(le32_to_cpu(rec
->e_clusters
) < new_clusters
);
650 if (em
->em_clusters
< OCFS2_I(inode
)->ip_clusters
) {
652 * Size changed underneath us on disk. Drop any
653 * straddling records and update our idea of
656 ocfs2_extent_map_drop(inode
, em
->em_clusters
- 1);
657 em
->em_clusters
= OCFS2_I(inode
)->ip_clusters
;
660 mlog_bug_on_msg((le32_to_cpu(rec
->e_cpos
) +
661 le32_to_cpu(rec
->e_clusters
)) !=
662 (em
->em_clusters
+ new_clusters
),
664 "rec->e_cpos = %u + rec->e_clusters = %u = %u\n"
665 "em->em_clusters = %u + new_clusters = %u = %u\n",
666 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
667 le32_to_cpu(rec
->e_cpos
), le32_to_cpu(rec
->e_clusters
),
668 le32_to_cpu(rec
->e_cpos
) + le32_to_cpu(rec
->e_clusters
),
669 em
->em_clusters
, new_clusters
,
670 em
->em_clusters
+ new_clusters
);
672 em
->em_clusters
+= new_clusters
;
675 if (le32_to_cpu(rec
->e_clusters
) > new_clusters
) {
676 /* This is a contiguous append */
677 ent
= ocfs2_extent_map_lookup(em
, le32_to_cpu(rec
->e_cpos
), 1,
681 BUG_ON((le32_to_cpu(rec
->e_cpos
) +
682 le32_to_cpu(rec
->e_clusters
)) !=
683 (le32_to_cpu(old
->e_cpos
) +
684 le32_to_cpu(old
->e_clusters
) +
686 if (ent
->e_tree_depth
== 0) {
687 BUG_ON(le32_to_cpu(old
->e_cpos
) !=
688 le32_to_cpu(rec
->e_cpos
));
689 BUG_ON(le64_to_cpu(old
->e_blkno
) !=
690 le64_to_cpu(rec
->e_blkno
));
694 * Let non-leafs fall through as -ENOENT to
695 * force insertion of the new leaf.
697 le32_add_cpu(&old
->e_clusters
, new_clusters
);
702 ret
= ocfs2_extent_map_insert(inode
, rec
, 0);
709 /* Code here is included but defined out as it completes the extent
710 * map api and may be used in the future. */
713 * Look up the record containing this cluster offset. This record is
714 * part of the extent map. Do not free it. Any changes you make to
715 * it will reflect in the extent map. So, if your last extent
716 * is (cpos = 10, clusters = 10) and you truncate the file by 5
717 * clusters, you can do:
719 * ret = ocfs2_extent_map_get_rec(em, orig_size - 5, &rec);
720 * rec->e_clusters -= 5;
722 * The lookup does not read from disk. If the map isn't filled in for
723 * an entry, you won't find it.
725 * Also note that the returned record is valid until alloc_sem is
726 * dropped. After that, truncate and extend can happen. Caveat Emptor.
728 int ocfs2_extent_map_get_rec(struct inode
*inode
, u32 cpos
,
729 struct ocfs2_extent_rec
**rec
,
733 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
734 struct ocfs2_extent_map_entry
*ent
;
738 if (cpos
>= OCFS2_I(inode
)->ip_clusters
)
741 if (cpos
>= em
->em_clusters
) {
743 * Size changed underneath us on disk. Drop any
744 * straddling records and update our idea of
747 ocfs2_extent_map_drop(inode
, em
->em_clusters
- 1);
748 em
->em_clusters
= OCFS2_I(inode
)->ip_clusters
;
751 ent
= ocfs2_extent_map_lookup(&OCFS2_I(inode
)->ip_map
, cpos
, 1,
757 *tree_depth
= ent
->e_tree_depth
;
764 int ocfs2_extent_map_get_clusters(struct inode
*inode
,
765 u32 v_cpos
, int count
,
766 u32
*p_cpos
, int *ret_count
)
770 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
771 struct ocfs2_extent_map_entry
*ent
= NULL
;
773 *p_cpos
= ccount
= 0;
775 if ((v_cpos
+ count
) > OCFS2_I(inode
)->ip_clusters
)
778 if ((v_cpos
+ count
) > em
->em_clusters
) {
780 * Size changed underneath us on disk. Drop any
781 * straddling records and update our idea of
784 ocfs2_extent_map_drop(inode
, em
->em_clusters
- 1);
785 em
->em_clusters
= OCFS2_I(inode
)->ip_clusters
;
789 ret
= ocfs2_extent_map_lookup_read(inode
, v_cpos
, count
, &ent
);
794 /* We should never find ourselves straddling an interval */
795 if (!ocfs2_extent_rec_contains_clusters(&ent
->e_rec
,
800 coff
= v_cpos
- le32_to_cpu(ent
->e_rec
.e_cpos
);
801 *p_cpos
= ocfs2_blocks_to_clusters(inode
->i_sb
,
802 le64_to_cpu(ent
->e_rec
.e_blkno
)) +
806 *ret_count
= le32_to_cpu(ent
->e_rec
.e_clusters
) - coff
;
817 int ocfs2_extent_map_get_blocks(struct inode
*inode
,
818 u64 v_blkno
, int count
,
819 u64
*p_blkno
, int *ret_count
)
824 int bpc
= ocfs2_clusters_to_blocks(inode
->i_sb
, 1);
825 struct ocfs2_extent_map_entry
*ent
= NULL
;
826 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
827 struct ocfs2_extent_rec
*rec
;
831 cpos
= ocfs2_blocks_to_clusters(inode
->i_sb
, v_blkno
);
832 clusters
= ocfs2_blocks_to_clusters(inode
->i_sb
,
833 (u64
)count
+ bpc
- 1);
834 if ((cpos
+ clusters
) > OCFS2_I(inode
)->ip_clusters
) {
840 if ((cpos
+ clusters
) > em
->em_clusters
) {
842 * Size changed underneath us on disk. Drop any
843 * straddling records and update our idea of
846 ocfs2_extent_map_drop(inode
, em
->em_clusters
- 1);
847 em
->em_clusters
= OCFS2_I(inode
)->ip_clusters
;
850 ret
= ocfs2_extent_map_lookup_read(inode
, cpos
, clusters
, &ent
);
860 /* We should never find ourselves straddling an interval */
861 if (!ocfs2_extent_rec_contains_clusters(rec
, cpos
, clusters
)) {
867 boff
= ocfs2_clusters_to_blocks(inode
->i_sb
, cpos
-
868 le32_to_cpu(rec
->e_cpos
));
869 boff
+= (v_blkno
& (u64
)(bpc
- 1));
870 *p_blkno
= le64_to_cpu(rec
->e_blkno
) + boff
;
873 *ret_count
= ocfs2_clusters_to_blocks(inode
->i_sb
,
874 le32_to_cpu(rec
->e_clusters
)) - boff
;
883 int ocfs2_extent_map_init(struct inode
*inode
)
885 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
887 em
->em_extents
= RB_ROOT
;
894 static void __ocfs2_extent_map_drop(struct inode
*inode
,
896 struct rb_node
**free_head
,
897 struct ocfs2_extent_map_entry
**tail_ent
)
899 struct rb_node
*node
, *next
;
900 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
901 struct ocfs2_extent_map_entry
*ent
;
906 node
= rb_last(&em
->em_extents
);
909 next
= rb_prev(node
);
911 ent
= rb_entry(node
, struct ocfs2_extent_map_entry
,
913 if (le32_to_cpu(ent
->e_rec
.e_cpos
) < new_clusters
)
916 rb_erase(&ent
->e_node
, &em
->em_extents
);
918 node
->rb_right
= *free_head
;
925 /* Do we have an entry straddling new_clusters? */
928 ((le32_to_cpu(ent
->e_rec
.e_cpos
) +
929 le32_to_cpu(ent
->e_rec
.e_clusters
)) > new_clusters
))
936 static void __ocfs2_extent_map_drop_cleanup(struct rb_node
*free_head
)
938 struct rb_node
*node
;
939 struct ocfs2_extent_map_entry
*ent
;
943 free_head
= node
->rb_right
;
945 ent
= rb_entry(node
, struct ocfs2_extent_map_entry
,
947 kmem_cache_free(ocfs2_em_ent_cachep
, ent
);
952 * Remove all entries past new_clusters, inclusive of an entry that
953 * contains new_clusters. This is effectively a cache forget.
955 * If you want to also clip the last extent by some number of clusters,
956 * you need to call ocfs2_extent_map_trunc().
957 * This code does not check or modify ip_clusters.
959 int ocfs2_extent_map_drop(struct inode
*inode
, u32 new_clusters
)
961 struct rb_node
*free_head
= NULL
;
962 struct ocfs2_extent_map
*em
= &OCFS2_I(inode
)->ip_map
;
963 struct ocfs2_extent_map_entry
*ent
;
965 spin_lock(&OCFS2_I(inode
)->ip_lock
);
967 __ocfs2_extent_map_drop(inode
, new_clusters
, &free_head
, &ent
);
970 rb_erase(&ent
->e_node
, &em
->em_extents
);
971 ent
->e_node
.rb_right
= free_head
;
972 free_head
= &ent
->e_node
;
975 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
978 __ocfs2_extent_map_drop_cleanup(free_head
);
984 * Remove all entries past new_clusters and also clip any extent
985 * straddling new_clusters, if there is one. This does not check
986 * or modify ip_clusters
988 int ocfs2_extent_map_trunc(struct inode
*inode
, u32 new_clusters
)
990 struct rb_node
*free_head
= NULL
;
991 struct ocfs2_extent_map_entry
*ent
= NULL
;
993 spin_lock(&OCFS2_I(inode
)->ip_lock
);
995 __ocfs2_extent_map_drop(inode
, new_clusters
, &free_head
, &ent
);
998 ent
->e_rec
.e_clusters
= cpu_to_le32(new_clusters
-
999 le32_to_cpu(ent
->e_rec
.e_cpos
));
1001 OCFS2_I(inode
)->ip_map
.em_clusters
= new_clusters
;
1003 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
1006 __ocfs2_extent_map_drop_cleanup(free_head
);
1011 int __init
init_ocfs2_extent_maps(void)
1013 ocfs2_em_ent_cachep
=
1014 kmem_cache_create("ocfs2_em_ent",
1015 sizeof(struct ocfs2_extent_map_entry
),
1016 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
1017 if (!ocfs2_em_ent_cachep
)
1023 void exit_ocfs2_extent_maps(void)
1025 kmem_cache_destroy(ocfs2_em_ent_cachep
);