1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Extent allocs and frees
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
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/types.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>
30 #include <linux/swap.h>
31 #include <linux/quotaops.h>
33 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
34 #include <cluster/masklog.h>
40 #include "blockcheck.h"
42 #include "extent_map.h"
45 #include "localalloc.h"
53 #include "buffer_head_io.h"
57 * Operations for a specific extent tree type.
59 * To implement an on-disk btree (extent tree) type in ocfs2, add
60 * an ocfs2_extent_tree_operations structure and the matching
61 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
62 * for the allocation portion of the extent tree.
64 struct ocfs2_extent_tree_operations
{
66 * last_eb_blk is the block number of the right most leaf extent
67 * block. Most on-disk structures containing an extent tree store
68 * this value for fast access. The ->eo_set_last_eb_blk() and
69 * ->eo_get_last_eb_blk() operations access this value. They are
72 void (*eo_set_last_eb_blk
)(struct ocfs2_extent_tree
*et
,
74 u64 (*eo_get_last_eb_blk
)(struct ocfs2_extent_tree
*et
);
77 * The on-disk structure usually keeps track of how many total
78 * clusters are stored in this extent tree. This function updates
79 * that value. new_clusters is the delta, and must be
80 * added to the total. Required.
82 void (*eo_update_clusters
)(struct inode
*inode
,
83 struct ocfs2_extent_tree
*et
,
87 * If ->eo_insert_check() exists, it is called before rec is
88 * inserted into the extent tree. It is optional.
90 int (*eo_insert_check
)(struct inode
*inode
,
91 struct ocfs2_extent_tree
*et
,
92 struct ocfs2_extent_rec
*rec
);
93 int (*eo_sanity_check
)(struct inode
*inode
, struct ocfs2_extent_tree
*et
);
96 * --------------------------------------------------------------
97 * The remaining are internal to ocfs2_extent_tree and don't have
102 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
105 void (*eo_fill_root_el
)(struct ocfs2_extent_tree
*et
);
108 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
109 * it exists. If it does not, et->et_max_leaf_clusters is set
110 * to 0 (unlimited). Optional.
112 void (*eo_fill_max_leaf_clusters
)(struct inode
*inode
,
113 struct ocfs2_extent_tree
*et
);
118 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
121 static u64
ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree
*et
);
122 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
124 static void ocfs2_dinode_update_clusters(struct inode
*inode
,
125 struct ocfs2_extent_tree
*et
,
127 static int ocfs2_dinode_insert_check(struct inode
*inode
,
128 struct ocfs2_extent_tree
*et
,
129 struct ocfs2_extent_rec
*rec
);
130 static int ocfs2_dinode_sanity_check(struct inode
*inode
,
131 struct ocfs2_extent_tree
*et
);
132 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree
*et
);
133 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops
= {
134 .eo_set_last_eb_blk
= ocfs2_dinode_set_last_eb_blk
,
135 .eo_get_last_eb_blk
= ocfs2_dinode_get_last_eb_blk
,
136 .eo_update_clusters
= ocfs2_dinode_update_clusters
,
137 .eo_insert_check
= ocfs2_dinode_insert_check
,
138 .eo_sanity_check
= ocfs2_dinode_sanity_check
,
139 .eo_fill_root_el
= ocfs2_dinode_fill_root_el
,
142 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
145 struct ocfs2_dinode
*di
= et
->et_object
;
147 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
148 di
->i_last_eb_blk
= cpu_to_le64(blkno
);
151 static u64
ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
153 struct ocfs2_dinode
*di
= et
->et_object
;
155 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
156 return le64_to_cpu(di
->i_last_eb_blk
);
159 static void ocfs2_dinode_update_clusters(struct inode
*inode
,
160 struct ocfs2_extent_tree
*et
,
163 struct ocfs2_dinode
*di
= et
->et_object
;
165 le32_add_cpu(&di
->i_clusters
, clusters
);
166 spin_lock(&OCFS2_I(inode
)->ip_lock
);
167 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(di
->i_clusters
);
168 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
171 static int ocfs2_dinode_insert_check(struct inode
*inode
,
172 struct ocfs2_extent_tree
*et
,
173 struct ocfs2_extent_rec
*rec
)
175 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
177 BUG_ON(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
);
178 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb
) &&
179 (OCFS2_I(inode
)->ip_clusters
!=
180 le32_to_cpu(rec
->e_cpos
)),
181 "Device %s, asking for sparse allocation: inode %llu, "
182 "cpos %u, clusters %u\n",
184 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
186 OCFS2_I(inode
)->ip_clusters
);
191 static int ocfs2_dinode_sanity_check(struct inode
*inode
,
192 struct ocfs2_extent_tree
*et
)
194 struct ocfs2_dinode
*di
= et
->et_object
;
196 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
197 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
202 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree
*et
)
204 struct ocfs2_dinode
*di
= et
->et_object
;
206 et
->et_root_el
= &di
->id2
.i_list
;
210 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree
*et
)
212 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
214 et
->et_root_el
= &vb
->vb_xv
->xr_list
;
217 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
220 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
222 vb
->vb_xv
->xr_last_eb_blk
= cpu_to_le64(blkno
);
225 static u64
ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
227 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
229 return le64_to_cpu(vb
->vb_xv
->xr_last_eb_blk
);
232 static void ocfs2_xattr_value_update_clusters(struct inode
*inode
,
233 struct ocfs2_extent_tree
*et
,
236 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
238 le32_add_cpu(&vb
->vb_xv
->xr_clusters
, clusters
);
241 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops
= {
242 .eo_set_last_eb_blk
= ocfs2_xattr_value_set_last_eb_blk
,
243 .eo_get_last_eb_blk
= ocfs2_xattr_value_get_last_eb_blk
,
244 .eo_update_clusters
= ocfs2_xattr_value_update_clusters
,
245 .eo_fill_root_el
= ocfs2_xattr_value_fill_root_el
,
248 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree
*et
)
250 struct ocfs2_xattr_block
*xb
= et
->et_object
;
252 et
->et_root_el
= &xb
->xb_attrs
.xb_root
.xt_list
;
255 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode
*inode
,
256 struct ocfs2_extent_tree
*et
)
258 et
->et_max_leaf_clusters
=
259 ocfs2_clusters_for_bytes(inode
->i_sb
,
260 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
);
263 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
266 struct ocfs2_xattr_block
*xb
= et
->et_object
;
267 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
269 xt
->xt_last_eb_blk
= cpu_to_le64(blkno
);
272 static u64
ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
274 struct ocfs2_xattr_block
*xb
= et
->et_object
;
275 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
277 return le64_to_cpu(xt
->xt_last_eb_blk
);
280 static void ocfs2_xattr_tree_update_clusters(struct inode
*inode
,
281 struct ocfs2_extent_tree
*et
,
284 struct ocfs2_xattr_block
*xb
= et
->et_object
;
286 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, clusters
);
289 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops
= {
290 .eo_set_last_eb_blk
= ocfs2_xattr_tree_set_last_eb_blk
,
291 .eo_get_last_eb_blk
= ocfs2_xattr_tree_get_last_eb_blk
,
292 .eo_update_clusters
= ocfs2_xattr_tree_update_clusters
,
293 .eo_fill_root_el
= ocfs2_xattr_tree_fill_root_el
,
294 .eo_fill_max_leaf_clusters
= ocfs2_xattr_tree_fill_max_leaf_clusters
,
297 static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
300 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
302 dx_root
->dr_last_eb_blk
= cpu_to_le64(blkno
);
305 static u64
ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
307 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
309 return le64_to_cpu(dx_root
->dr_last_eb_blk
);
312 static void ocfs2_dx_root_update_clusters(struct inode
*inode
,
313 struct ocfs2_extent_tree
*et
,
316 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
318 le32_add_cpu(&dx_root
->dr_clusters
, clusters
);
321 static int ocfs2_dx_root_sanity_check(struct inode
*inode
,
322 struct ocfs2_extent_tree
*et
)
324 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
326 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root
));
331 static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree
*et
)
333 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
335 et
->et_root_el
= &dx_root
->dr_list
;
338 static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops
= {
339 .eo_set_last_eb_blk
= ocfs2_dx_root_set_last_eb_blk
,
340 .eo_get_last_eb_blk
= ocfs2_dx_root_get_last_eb_blk
,
341 .eo_update_clusters
= ocfs2_dx_root_update_clusters
,
342 .eo_sanity_check
= ocfs2_dx_root_sanity_check
,
343 .eo_fill_root_el
= ocfs2_dx_root_fill_root_el
,
346 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree
*et
,
348 struct buffer_head
*bh
,
349 ocfs2_journal_access_func access
,
351 struct ocfs2_extent_tree_operations
*ops
)
355 et
->et_root_journal_access
= access
;
357 obj
= (void *)bh
->b_data
;
360 et
->et_ops
->eo_fill_root_el(et
);
361 if (!et
->et_ops
->eo_fill_max_leaf_clusters
)
362 et
->et_max_leaf_clusters
= 0;
364 et
->et_ops
->eo_fill_max_leaf_clusters(inode
, et
);
367 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree
*et
,
369 struct buffer_head
*bh
)
371 __ocfs2_init_extent_tree(et
, inode
, bh
, ocfs2_journal_access_di
,
372 NULL
, &ocfs2_dinode_et_ops
);
375 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree
*et
,
377 struct buffer_head
*bh
)
379 __ocfs2_init_extent_tree(et
, inode
, bh
, ocfs2_journal_access_xb
,
380 NULL
, &ocfs2_xattr_tree_et_ops
);
383 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree
*et
,
385 struct ocfs2_xattr_value_buf
*vb
)
387 __ocfs2_init_extent_tree(et
, inode
, vb
->vb_bh
, vb
->vb_access
, vb
,
388 &ocfs2_xattr_value_et_ops
);
391 void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree
*et
,
393 struct buffer_head
*bh
)
395 __ocfs2_init_extent_tree(et
, inode
, bh
, ocfs2_journal_access_dr
,
396 NULL
, &ocfs2_dx_root_et_ops
);
399 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
402 et
->et_ops
->eo_set_last_eb_blk(et
, new_last_eb_blk
);
405 static inline u64
ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
407 return et
->et_ops
->eo_get_last_eb_blk(et
);
410 static inline void ocfs2_et_update_clusters(struct inode
*inode
,
411 struct ocfs2_extent_tree
*et
,
414 et
->et_ops
->eo_update_clusters(inode
, et
, clusters
);
417 static inline int ocfs2_et_root_journal_access(handle_t
*handle
,
419 struct ocfs2_extent_tree
*et
,
422 return et
->et_root_journal_access(handle
, inode
, et
->et_root_bh
,
426 static inline int ocfs2_et_insert_check(struct inode
*inode
,
427 struct ocfs2_extent_tree
*et
,
428 struct ocfs2_extent_rec
*rec
)
432 if (et
->et_ops
->eo_insert_check
)
433 ret
= et
->et_ops
->eo_insert_check(inode
, et
, rec
);
437 static inline int ocfs2_et_sanity_check(struct inode
*inode
,
438 struct ocfs2_extent_tree
*et
)
442 if (et
->et_ops
->eo_sanity_check
)
443 ret
= et
->et_ops
->eo_sanity_check(inode
, et
);
447 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
);
448 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
449 struct ocfs2_extent_block
*eb
);
452 * Structures which describe a path through a btree, and functions to
455 * The idea here is to be as generic as possible with the tree
458 struct ocfs2_path_item
{
459 struct buffer_head
*bh
;
460 struct ocfs2_extent_list
*el
;
463 #define OCFS2_MAX_PATH_DEPTH 5
467 ocfs2_journal_access_func p_root_access
;
468 struct ocfs2_path_item p_node
[OCFS2_MAX_PATH_DEPTH
];
471 #define path_root_bh(_path) ((_path)->p_node[0].bh)
472 #define path_root_el(_path) ((_path)->p_node[0].el)
473 #define path_root_access(_path)((_path)->p_root_access)
474 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
475 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
476 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
478 static int ocfs2_find_path(struct inode
*inode
, struct ocfs2_path
*path
,
480 static void ocfs2_adjust_rightmost_records(struct inode
*inode
,
482 struct ocfs2_path
*path
,
483 struct ocfs2_extent_rec
*insert_rec
);
485 * Reset the actual path elements so that we can re-use the structure
486 * to build another path. Generally, this involves freeing the buffer
489 static void ocfs2_reinit_path(struct ocfs2_path
*path
, int keep_root
)
491 int i
, start
= 0, depth
= 0;
492 struct ocfs2_path_item
*node
;
497 for(i
= start
; i
< path_num_items(path
); i
++) {
498 node
= &path
->p_node
[i
];
506 * Tree depth may change during truncate, or insert. If we're
507 * keeping the root extent list, then make sure that our path
508 * structure reflects the proper depth.
511 depth
= le16_to_cpu(path_root_el(path
)->l_tree_depth
);
513 path_root_access(path
) = NULL
;
515 path
->p_tree_depth
= depth
;
518 static void ocfs2_free_path(struct ocfs2_path
*path
)
521 ocfs2_reinit_path(path
, 0);
527 * All the elements of src into dest. After this call, src could be freed
528 * without affecting dest.
530 * Both paths should have the same root. Any non-root elements of dest
533 static void ocfs2_cp_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
537 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
538 BUG_ON(path_root_el(dest
) != path_root_el(src
));
539 BUG_ON(path_root_access(dest
) != path_root_access(src
));
541 ocfs2_reinit_path(dest
, 1);
543 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
544 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
545 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
547 if (dest
->p_node
[i
].bh
)
548 get_bh(dest
->p_node
[i
].bh
);
553 * Make the *dest path the same as src and re-initialize src path to
556 static void ocfs2_mv_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
560 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
561 BUG_ON(path_root_access(dest
) != path_root_access(src
));
563 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
564 brelse(dest
->p_node
[i
].bh
);
566 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
567 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
569 src
->p_node
[i
].bh
= NULL
;
570 src
->p_node
[i
].el
= NULL
;
575 * Insert an extent block at given index.
577 * This will not take an additional reference on eb_bh.
579 static inline void ocfs2_path_insert_eb(struct ocfs2_path
*path
, int index
,
580 struct buffer_head
*eb_bh
)
582 struct ocfs2_extent_block
*eb
= (struct ocfs2_extent_block
*)eb_bh
->b_data
;
585 * Right now, no root bh is an extent block, so this helps
586 * catch code errors with dinode trees. The assertion can be
587 * safely removed if we ever need to insert extent block
588 * structures at the root.
592 path
->p_node
[index
].bh
= eb_bh
;
593 path
->p_node
[index
].el
= &eb
->h_list
;
596 static struct ocfs2_path
*ocfs2_new_path(struct buffer_head
*root_bh
,
597 struct ocfs2_extent_list
*root_el
,
598 ocfs2_journal_access_func access
)
600 struct ocfs2_path
*path
;
602 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) >= OCFS2_MAX_PATH_DEPTH
);
604 path
= kzalloc(sizeof(*path
), GFP_NOFS
);
606 path
->p_tree_depth
= le16_to_cpu(root_el
->l_tree_depth
);
608 path_root_bh(path
) = root_bh
;
609 path_root_el(path
) = root_el
;
610 path_root_access(path
) = access
;
616 static struct ocfs2_path
*ocfs2_new_path_from_path(struct ocfs2_path
*path
)
618 return ocfs2_new_path(path_root_bh(path
), path_root_el(path
),
619 path_root_access(path
));
622 static struct ocfs2_path
*ocfs2_new_path_from_et(struct ocfs2_extent_tree
*et
)
624 return ocfs2_new_path(et
->et_root_bh
, et
->et_root_el
,
625 et
->et_root_journal_access
);
629 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
630 * otherwise it's the root_access function.
632 * I don't like the way this function's name looks next to
633 * ocfs2_journal_access_path(), but I don't have a better one.
635 static int ocfs2_path_bh_journal_access(handle_t
*handle
,
637 struct ocfs2_path
*path
,
640 ocfs2_journal_access_func access
= path_root_access(path
);
643 access
= ocfs2_journal_access
;
646 access
= ocfs2_journal_access_eb
;
648 return access(handle
, inode
, path
->p_node
[idx
].bh
,
649 OCFS2_JOURNAL_ACCESS_WRITE
);
653 * Convenience function to journal all components in a path.
655 static int ocfs2_journal_access_path(struct inode
*inode
, handle_t
*handle
,
656 struct ocfs2_path
*path
)
663 for(i
= 0; i
< path_num_items(path
); i
++) {
664 ret
= ocfs2_path_bh_journal_access(handle
, inode
, path
, i
);
676 * Return the index of the extent record which contains cluster #v_cluster.
677 * -1 is returned if it was not found.
679 * Should work fine on interior and exterior nodes.
681 int ocfs2_search_extent_list(struct ocfs2_extent_list
*el
, u32 v_cluster
)
685 struct ocfs2_extent_rec
*rec
;
686 u32 rec_end
, rec_start
, clusters
;
688 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
689 rec
= &el
->l_recs
[i
];
691 rec_start
= le32_to_cpu(rec
->e_cpos
);
692 clusters
= ocfs2_rec_clusters(el
, rec
);
694 rec_end
= rec_start
+ clusters
;
696 if (v_cluster
>= rec_start
&& v_cluster
< rec_end
) {
705 enum ocfs2_contig_type
{
714 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
715 * ocfs2_extent_contig only work properly against leaf nodes!
717 static int ocfs2_block_extent_contig(struct super_block
*sb
,
718 struct ocfs2_extent_rec
*ext
,
721 u64 blk_end
= le64_to_cpu(ext
->e_blkno
);
723 blk_end
+= ocfs2_clusters_to_blocks(sb
,
724 le16_to_cpu(ext
->e_leaf_clusters
));
726 return blkno
== blk_end
;
729 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec
*left
,
730 struct ocfs2_extent_rec
*right
)
734 left_range
= le32_to_cpu(left
->e_cpos
) +
735 le16_to_cpu(left
->e_leaf_clusters
);
737 return (left_range
== le32_to_cpu(right
->e_cpos
));
740 static enum ocfs2_contig_type
741 ocfs2_extent_contig(struct inode
*inode
,
742 struct ocfs2_extent_rec
*ext
,
743 struct ocfs2_extent_rec
*insert_rec
)
745 u64 blkno
= le64_to_cpu(insert_rec
->e_blkno
);
748 * Refuse to coalesce extent records with different flag
749 * fields - we don't want to mix unwritten extents with user
752 if (ext
->e_flags
!= insert_rec
->e_flags
)
755 if (ocfs2_extents_adjacent(ext
, insert_rec
) &&
756 ocfs2_block_extent_contig(inode
->i_sb
, ext
, blkno
))
759 blkno
= le64_to_cpu(ext
->e_blkno
);
760 if (ocfs2_extents_adjacent(insert_rec
, ext
) &&
761 ocfs2_block_extent_contig(inode
->i_sb
, insert_rec
, blkno
))
768 * NOTE: We can have pretty much any combination of contiguousness and
771 * The usefulness of APPEND_TAIL is more in that it lets us know that
772 * we'll have to update the path to that leaf.
774 enum ocfs2_append_type
{
779 enum ocfs2_split_type
{
785 struct ocfs2_insert_type
{
786 enum ocfs2_split_type ins_split
;
787 enum ocfs2_append_type ins_appending
;
788 enum ocfs2_contig_type ins_contig
;
789 int ins_contig_index
;
793 struct ocfs2_merge_ctxt
{
794 enum ocfs2_contig_type c_contig_type
;
795 int c_has_empty_extent
;
796 int c_split_covers_rec
;
799 static int ocfs2_validate_extent_block(struct super_block
*sb
,
800 struct buffer_head
*bh
)
803 struct ocfs2_extent_block
*eb
=
804 (struct ocfs2_extent_block
*)bh
->b_data
;
806 mlog(0, "Validating extent block %llu\n",
807 (unsigned long long)bh
->b_blocknr
);
809 BUG_ON(!buffer_uptodate(bh
));
812 * If the ecc fails, we return the error but otherwise
813 * leave the filesystem running. We know any error is
814 * local to this block.
816 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &eb
->h_check
);
818 mlog(ML_ERROR
, "Checksum failed for extent block %llu\n",
819 (unsigned long long)bh
->b_blocknr
);
824 * Errors after here are fatal.
827 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
829 "Extent block #%llu has bad signature %.*s",
830 (unsigned long long)bh
->b_blocknr
, 7,
835 if (le64_to_cpu(eb
->h_blkno
) != bh
->b_blocknr
) {
837 "Extent block #%llu has an invalid h_blkno "
839 (unsigned long long)bh
->b_blocknr
,
840 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
844 if (le32_to_cpu(eb
->h_fs_generation
) != OCFS2_SB(sb
)->fs_generation
) {
846 "Extent block #%llu has an invalid "
847 "h_fs_generation of #%u",
848 (unsigned long long)bh
->b_blocknr
,
849 le32_to_cpu(eb
->h_fs_generation
));
856 int ocfs2_read_extent_block(struct inode
*inode
, u64 eb_blkno
,
857 struct buffer_head
**bh
)
860 struct buffer_head
*tmp
= *bh
;
862 rc
= ocfs2_read_block(inode
, eb_blkno
, &tmp
,
863 ocfs2_validate_extent_block
);
865 /* If ocfs2_read_block() got us a new bh, pass it up. */
874 * How many free extents have we got before we need more meta data?
876 int ocfs2_num_free_extents(struct ocfs2_super
*osb
,
878 struct ocfs2_extent_tree
*et
)
881 struct ocfs2_extent_list
*el
= NULL
;
882 struct ocfs2_extent_block
*eb
;
883 struct buffer_head
*eb_bh
= NULL
;
889 last_eb_blk
= ocfs2_et_get_last_eb_blk(et
);
892 retval
= ocfs2_read_extent_block(inode
, last_eb_blk
, &eb_bh
);
897 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
901 BUG_ON(el
->l_tree_depth
!= 0);
903 retval
= le16_to_cpu(el
->l_count
) - le16_to_cpu(el
->l_next_free_rec
);
911 /* expects array to already be allocated
913 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
916 static int ocfs2_create_new_meta_bhs(struct ocfs2_super
*osb
,
920 struct ocfs2_alloc_context
*meta_ac
,
921 struct buffer_head
*bhs
[])
923 int count
, status
, i
;
924 u16 suballoc_bit_start
;
927 struct ocfs2_extent_block
*eb
;
932 while (count
< wanted
) {
933 status
= ocfs2_claim_metadata(osb
,
945 for(i
= count
; i
< (num_got
+ count
); i
++) {
946 bhs
[i
] = sb_getblk(osb
->sb
, first_blkno
);
947 if (bhs
[i
] == NULL
) {
952 ocfs2_set_new_buffer_uptodate(inode
, bhs
[i
]);
954 status
= ocfs2_journal_access_eb(handle
, inode
, bhs
[i
],
955 OCFS2_JOURNAL_ACCESS_CREATE
);
961 memset(bhs
[i
]->b_data
, 0, osb
->sb
->s_blocksize
);
962 eb
= (struct ocfs2_extent_block
*) bhs
[i
]->b_data
;
963 /* Ok, setup the minimal stuff here. */
964 strcpy(eb
->h_signature
, OCFS2_EXTENT_BLOCK_SIGNATURE
);
965 eb
->h_blkno
= cpu_to_le64(first_blkno
);
966 eb
->h_fs_generation
= cpu_to_le32(osb
->fs_generation
);
967 eb
->h_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
968 eb
->h_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
970 cpu_to_le16(ocfs2_extent_recs_per_eb(osb
->sb
));
972 suballoc_bit_start
++;
975 /* We'll also be dirtied by the caller, so
976 * this isn't absolutely necessary. */
977 status
= ocfs2_journal_dirty(handle
, bhs
[i
]);
990 for(i
= 0; i
< wanted
; i
++) {
1000 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1002 * Returns the sum of the rightmost extent rec logical offset and
1005 * ocfs2_add_branch() uses this to determine what logical cluster
1006 * value should be populated into the leftmost new branch records.
1008 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1009 * value for the new topmost tree record.
1011 static inline u32
ocfs2_sum_rightmost_rec(struct ocfs2_extent_list
*el
)
1015 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
1017 return le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
1018 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
1022 * Change range of the branches in the right most path according to the leaf
1023 * extent block's rightmost record.
1025 static int ocfs2_adjust_rightmost_branch(handle_t
*handle
,
1026 struct inode
*inode
,
1027 struct ocfs2_extent_tree
*et
)
1030 struct ocfs2_path
*path
= NULL
;
1031 struct ocfs2_extent_list
*el
;
1032 struct ocfs2_extent_rec
*rec
;
1034 path
= ocfs2_new_path_from_et(et
);
1040 status
= ocfs2_find_path(inode
, path
, UINT_MAX
);
1046 status
= ocfs2_extend_trans(handle
, path_num_items(path
) +
1047 handle
->h_buffer_credits
);
1053 status
= ocfs2_journal_access_path(inode
, handle
, path
);
1059 el
= path_leaf_el(path
);
1060 rec
= &el
->l_recs
[le32_to_cpu(el
->l_next_free_rec
) - 1];
1062 ocfs2_adjust_rightmost_records(inode
, handle
, path
, rec
);
1065 ocfs2_free_path(path
);
1070 * Add an entire tree branch to our inode. eb_bh is the extent block
1071 * to start at, if we don't want to start the branch at the dinode
1074 * last_eb_bh is required as we have to update it's next_leaf pointer
1075 * for the new last extent block.
1077 * the new branch will be 'empty' in the sense that every block will
1078 * contain a single record with cluster count == 0.
1080 static int ocfs2_add_branch(struct ocfs2_super
*osb
,
1082 struct inode
*inode
,
1083 struct ocfs2_extent_tree
*et
,
1084 struct buffer_head
*eb_bh
,
1085 struct buffer_head
**last_eb_bh
,
1086 struct ocfs2_alloc_context
*meta_ac
)
1088 int status
, new_blocks
, i
;
1089 u64 next_blkno
, new_last_eb_blk
;
1090 struct buffer_head
*bh
;
1091 struct buffer_head
**new_eb_bhs
= NULL
;
1092 struct ocfs2_extent_block
*eb
;
1093 struct ocfs2_extent_list
*eb_el
;
1094 struct ocfs2_extent_list
*el
;
1095 u32 new_cpos
, root_end
;
1099 BUG_ON(!last_eb_bh
|| !*last_eb_bh
);
1102 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
1105 el
= et
->et_root_el
;
1107 /* we never add a branch to a leaf. */
1108 BUG_ON(!el
->l_tree_depth
);
1110 new_blocks
= le16_to_cpu(el
->l_tree_depth
);
1112 eb
= (struct ocfs2_extent_block
*)(*last_eb_bh
)->b_data
;
1113 new_cpos
= ocfs2_sum_rightmost_rec(&eb
->h_list
);
1114 root_end
= ocfs2_sum_rightmost_rec(et
->et_root_el
);
1117 * If there is a gap before the root end and the real end
1118 * of the righmost leaf block, we need to remove the gap
1119 * between new_cpos and root_end first so that the tree
1120 * is consistent after we add a new branch(it will start
1123 if (root_end
> new_cpos
) {
1124 mlog(0, "adjust the cluster end from %u to %u\n",
1125 root_end
, new_cpos
);
1126 status
= ocfs2_adjust_rightmost_branch(handle
, inode
, et
);
1133 /* allocate the number of new eb blocks we need */
1134 new_eb_bhs
= kcalloc(new_blocks
, sizeof(struct buffer_head
*),
1142 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, new_blocks
,
1143 meta_ac
, new_eb_bhs
);
1149 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1150 * linked with the rest of the tree.
1151 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1153 * when we leave the loop, new_last_eb_blk will point to the
1154 * newest leaf, and next_blkno will point to the topmost extent
1156 next_blkno
= new_last_eb_blk
= 0;
1157 for(i
= 0; i
< new_blocks
; i
++) {
1159 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1160 /* ocfs2_create_new_meta_bhs() should create it right! */
1161 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
1162 eb_el
= &eb
->h_list
;
1164 status
= ocfs2_journal_access_eb(handle
, inode
, bh
,
1165 OCFS2_JOURNAL_ACCESS_CREATE
);
1171 eb
->h_next_leaf_blk
= 0;
1172 eb_el
->l_tree_depth
= cpu_to_le16(i
);
1173 eb_el
->l_next_free_rec
= cpu_to_le16(1);
1175 * This actually counts as an empty extent as
1178 eb_el
->l_recs
[0].e_cpos
= cpu_to_le32(new_cpos
);
1179 eb_el
->l_recs
[0].e_blkno
= cpu_to_le64(next_blkno
);
1181 * eb_el isn't always an interior node, but even leaf
1182 * nodes want a zero'd flags and reserved field so
1183 * this gets the whole 32 bits regardless of use.
1185 eb_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(0);
1186 if (!eb_el
->l_tree_depth
)
1187 new_last_eb_blk
= le64_to_cpu(eb
->h_blkno
);
1189 status
= ocfs2_journal_dirty(handle
, bh
);
1195 next_blkno
= le64_to_cpu(eb
->h_blkno
);
1198 /* This is a bit hairy. We want to update up to three blocks
1199 * here without leaving any of them in an inconsistent state
1200 * in case of error. We don't have to worry about
1201 * journal_dirty erroring as it won't unless we've aborted the
1202 * handle (in which case we would never be here) so reserving
1203 * the write with journal_access is all we need to do. */
1204 status
= ocfs2_journal_access_eb(handle
, inode
, *last_eb_bh
,
1205 OCFS2_JOURNAL_ACCESS_WRITE
);
1210 status
= ocfs2_et_root_journal_access(handle
, inode
, et
,
1211 OCFS2_JOURNAL_ACCESS_WRITE
);
1217 status
= ocfs2_journal_access_eb(handle
, inode
, eb_bh
,
1218 OCFS2_JOURNAL_ACCESS_WRITE
);
1225 /* Link the new branch into the rest of the tree (el will
1226 * either be on the root_bh, or the extent block passed in. */
1227 i
= le16_to_cpu(el
->l_next_free_rec
);
1228 el
->l_recs
[i
].e_blkno
= cpu_to_le64(next_blkno
);
1229 el
->l_recs
[i
].e_cpos
= cpu_to_le32(new_cpos
);
1230 el
->l_recs
[i
].e_int_clusters
= 0;
1231 le16_add_cpu(&el
->l_next_free_rec
, 1);
1233 /* fe needs a new last extent block pointer, as does the
1234 * next_leaf on the previously last-extent-block. */
1235 ocfs2_et_set_last_eb_blk(et
, new_last_eb_blk
);
1237 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
1238 eb
->h_next_leaf_blk
= cpu_to_le64(new_last_eb_blk
);
1240 status
= ocfs2_journal_dirty(handle
, *last_eb_bh
);
1243 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
1247 status
= ocfs2_journal_dirty(handle
, eb_bh
);
1253 * Some callers want to track the rightmost leaf so pass it
1256 brelse(*last_eb_bh
);
1257 get_bh(new_eb_bhs
[0]);
1258 *last_eb_bh
= new_eb_bhs
[0];
1263 for (i
= 0; i
< new_blocks
; i
++)
1264 brelse(new_eb_bhs
[i
]);
1273 * adds another level to the allocation tree.
1274 * returns back the new extent block so you can add a branch to it
1277 static int ocfs2_shift_tree_depth(struct ocfs2_super
*osb
,
1279 struct inode
*inode
,
1280 struct ocfs2_extent_tree
*et
,
1281 struct ocfs2_alloc_context
*meta_ac
,
1282 struct buffer_head
**ret_new_eb_bh
)
1286 struct buffer_head
*new_eb_bh
= NULL
;
1287 struct ocfs2_extent_block
*eb
;
1288 struct ocfs2_extent_list
*root_el
;
1289 struct ocfs2_extent_list
*eb_el
;
1293 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, 1, meta_ac
,
1300 eb
= (struct ocfs2_extent_block
*) new_eb_bh
->b_data
;
1301 /* ocfs2_create_new_meta_bhs() should create it right! */
1302 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
1304 eb_el
= &eb
->h_list
;
1305 root_el
= et
->et_root_el
;
1307 status
= ocfs2_journal_access_eb(handle
, inode
, new_eb_bh
,
1308 OCFS2_JOURNAL_ACCESS_CREATE
);
1314 /* copy the root extent list data into the new extent block */
1315 eb_el
->l_tree_depth
= root_el
->l_tree_depth
;
1316 eb_el
->l_next_free_rec
= root_el
->l_next_free_rec
;
1317 for (i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1318 eb_el
->l_recs
[i
] = root_el
->l_recs
[i
];
1320 status
= ocfs2_journal_dirty(handle
, new_eb_bh
);
1326 status
= ocfs2_et_root_journal_access(handle
, inode
, et
,
1327 OCFS2_JOURNAL_ACCESS_WRITE
);
1333 new_clusters
= ocfs2_sum_rightmost_rec(eb_el
);
1335 /* update root_bh now */
1336 le16_add_cpu(&root_el
->l_tree_depth
, 1);
1337 root_el
->l_recs
[0].e_cpos
= 0;
1338 root_el
->l_recs
[0].e_blkno
= eb
->h_blkno
;
1339 root_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(new_clusters
);
1340 for (i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1341 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
1342 root_el
->l_next_free_rec
= cpu_to_le16(1);
1344 /* If this is our 1st tree depth shift, then last_eb_blk
1345 * becomes the allocated extent block */
1346 if (root_el
->l_tree_depth
== cpu_to_le16(1))
1347 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
1349 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
1355 *ret_new_eb_bh
= new_eb_bh
;
1366 * Should only be called when there is no space left in any of the
1367 * leaf nodes. What we want to do is find the lowest tree depth
1368 * non-leaf extent block with room for new records. There are three
1369 * valid results of this search:
1371 * 1) a lowest extent block is found, then we pass it back in
1372 * *lowest_eb_bh and return '0'
1374 * 2) the search fails to find anything, but the root_el has room. We
1375 * pass NULL back in *lowest_eb_bh, but still return '0'
1377 * 3) the search fails to find anything AND the root_el is full, in
1378 * which case we return > 0
1380 * return status < 0 indicates an error.
1382 static int ocfs2_find_branch_target(struct ocfs2_super
*osb
,
1383 struct inode
*inode
,
1384 struct ocfs2_extent_tree
*et
,
1385 struct buffer_head
**target_bh
)
1389 struct ocfs2_extent_block
*eb
;
1390 struct ocfs2_extent_list
*el
;
1391 struct buffer_head
*bh
= NULL
;
1392 struct buffer_head
*lowest_bh
= NULL
;
1398 el
= et
->et_root_el
;
1400 while(le16_to_cpu(el
->l_tree_depth
) > 1) {
1401 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1402 ocfs2_error(inode
->i_sb
, "Dinode %llu has empty "
1403 "extent list (next_free_rec == 0)",
1404 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1408 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
1409 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1411 ocfs2_error(inode
->i_sb
, "Dinode %llu has extent "
1412 "list where extent # %d has no physical "
1414 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, i
);
1422 status
= ocfs2_read_extent_block(inode
, blkno
, &bh
);
1428 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1431 if (le16_to_cpu(el
->l_next_free_rec
) <
1432 le16_to_cpu(el
->l_count
)) {
1439 /* If we didn't find one and the fe doesn't have any room,
1440 * then return '1' */
1441 el
= et
->et_root_el
;
1442 if (!lowest_bh
&& (el
->l_next_free_rec
== el
->l_count
))
1445 *target_bh
= lowest_bh
;
1454 * Grow a b-tree so that it has more records.
1456 * We might shift the tree depth in which case existing paths should
1457 * be considered invalid.
1459 * Tree depth after the grow is returned via *final_depth.
1461 * *last_eb_bh will be updated by ocfs2_add_branch().
1463 static int ocfs2_grow_tree(struct inode
*inode
, handle_t
*handle
,
1464 struct ocfs2_extent_tree
*et
, int *final_depth
,
1465 struct buffer_head
**last_eb_bh
,
1466 struct ocfs2_alloc_context
*meta_ac
)
1469 struct ocfs2_extent_list
*el
= et
->et_root_el
;
1470 int depth
= le16_to_cpu(el
->l_tree_depth
);
1471 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1472 struct buffer_head
*bh
= NULL
;
1474 BUG_ON(meta_ac
== NULL
);
1476 shift
= ocfs2_find_branch_target(osb
, inode
, et
, &bh
);
1483 /* We traveled all the way to the bottom of the allocation tree
1484 * and didn't find room for any more extents - we need to add
1485 * another tree level */
1488 mlog(0, "need to shift tree depth (current = %d)\n", depth
);
1490 /* ocfs2_shift_tree_depth will return us a buffer with
1491 * the new extent block (so we can pass that to
1492 * ocfs2_add_branch). */
1493 ret
= ocfs2_shift_tree_depth(osb
, handle
, inode
, et
,
1502 * Special case: we have room now if we shifted from
1503 * tree_depth 0, so no more work needs to be done.
1505 * We won't be calling add_branch, so pass
1506 * back *last_eb_bh as the new leaf. At depth
1507 * zero, it should always be null so there's
1508 * no reason to brelse.
1510 BUG_ON(*last_eb_bh
);
1517 /* call ocfs2_add_branch to add the final part of the tree with
1519 mlog(0, "add branch. bh = %p\n", bh
);
1520 ret
= ocfs2_add_branch(osb
, handle
, inode
, et
, bh
, last_eb_bh
,
1529 *final_depth
= depth
;
1535 * This function will discard the rightmost extent record.
1537 static void ocfs2_shift_records_right(struct ocfs2_extent_list
*el
)
1539 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1540 int count
= le16_to_cpu(el
->l_count
);
1541 unsigned int num_bytes
;
1544 /* This will cause us to go off the end of our extent list. */
1545 BUG_ON(next_free
>= count
);
1547 num_bytes
= sizeof(struct ocfs2_extent_rec
) * next_free
;
1549 memmove(&el
->l_recs
[1], &el
->l_recs
[0], num_bytes
);
1552 static void ocfs2_rotate_leaf(struct ocfs2_extent_list
*el
,
1553 struct ocfs2_extent_rec
*insert_rec
)
1555 int i
, insert_index
, next_free
, has_empty
, num_bytes
;
1556 u32 insert_cpos
= le32_to_cpu(insert_rec
->e_cpos
);
1557 struct ocfs2_extent_rec
*rec
;
1559 next_free
= le16_to_cpu(el
->l_next_free_rec
);
1560 has_empty
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
1564 /* The tree code before us didn't allow enough room in the leaf. */
1565 BUG_ON(el
->l_next_free_rec
== el
->l_count
&& !has_empty
);
1568 * The easiest way to approach this is to just remove the
1569 * empty extent and temporarily decrement next_free.
1573 * If next_free was 1 (only an empty extent), this
1574 * loop won't execute, which is fine. We still want
1575 * the decrement above to happen.
1577 for(i
= 0; i
< (next_free
- 1); i
++)
1578 el
->l_recs
[i
] = el
->l_recs
[i
+1];
1584 * Figure out what the new record index should be.
1586 for(i
= 0; i
< next_free
; i
++) {
1587 rec
= &el
->l_recs
[i
];
1589 if (insert_cpos
< le32_to_cpu(rec
->e_cpos
))
1594 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1595 insert_cpos
, insert_index
, has_empty
, next_free
, le16_to_cpu(el
->l_count
));
1597 BUG_ON(insert_index
< 0);
1598 BUG_ON(insert_index
>= le16_to_cpu(el
->l_count
));
1599 BUG_ON(insert_index
> next_free
);
1602 * No need to memmove if we're just adding to the tail.
1604 if (insert_index
!= next_free
) {
1605 BUG_ON(next_free
>= le16_to_cpu(el
->l_count
));
1607 num_bytes
= next_free
- insert_index
;
1608 num_bytes
*= sizeof(struct ocfs2_extent_rec
);
1609 memmove(&el
->l_recs
[insert_index
+ 1],
1610 &el
->l_recs
[insert_index
],
1615 * Either we had an empty extent, and need to re-increment or
1616 * there was no empty extent on a non full rightmost leaf node,
1617 * in which case we still need to increment.
1620 el
->l_next_free_rec
= cpu_to_le16(next_free
);
1622 * Make sure none of the math above just messed up our tree.
1624 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) > le16_to_cpu(el
->l_count
));
1626 el
->l_recs
[insert_index
] = *insert_rec
;
1630 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list
*el
)
1632 int size
, num_recs
= le16_to_cpu(el
->l_next_free_rec
);
1634 BUG_ON(num_recs
== 0);
1636 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
1638 size
= num_recs
* sizeof(struct ocfs2_extent_rec
);
1639 memmove(&el
->l_recs
[0], &el
->l_recs
[1], size
);
1640 memset(&el
->l_recs
[num_recs
], 0,
1641 sizeof(struct ocfs2_extent_rec
));
1642 el
->l_next_free_rec
= cpu_to_le16(num_recs
);
1647 * Create an empty extent record .
1649 * l_next_free_rec may be updated.
1651 * If an empty extent already exists do nothing.
1653 static void ocfs2_create_empty_extent(struct ocfs2_extent_list
*el
)
1655 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1657 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
1662 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
1665 mlog_bug_on_msg(el
->l_count
== el
->l_next_free_rec
,
1666 "Asked to create an empty extent in a full list:\n"
1667 "count = %u, tree depth = %u",
1668 le16_to_cpu(el
->l_count
),
1669 le16_to_cpu(el
->l_tree_depth
));
1671 ocfs2_shift_records_right(el
);
1674 le16_add_cpu(&el
->l_next_free_rec
, 1);
1675 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
1679 * For a rotation which involves two leaf nodes, the "root node" is
1680 * the lowest level tree node which contains a path to both leafs. This
1681 * resulting set of information can be used to form a complete "subtree"
1683 * This function is passed two full paths from the dinode down to a
1684 * pair of adjacent leaves. It's task is to figure out which path
1685 * index contains the subtree root - this can be the root index itself
1686 * in a worst-case rotation.
1688 * The array index of the subtree root is passed back.
1690 static int ocfs2_find_subtree_root(struct inode
*inode
,
1691 struct ocfs2_path
*left
,
1692 struct ocfs2_path
*right
)
1697 * Check that the caller passed in two paths from the same tree.
1699 BUG_ON(path_root_bh(left
) != path_root_bh(right
));
1705 * The caller didn't pass two adjacent paths.
1707 mlog_bug_on_msg(i
> left
->p_tree_depth
,
1708 "Inode %lu, left depth %u, right depth %u\n"
1709 "left leaf blk %llu, right leaf blk %llu\n",
1710 inode
->i_ino
, left
->p_tree_depth
,
1711 right
->p_tree_depth
,
1712 (unsigned long long)path_leaf_bh(left
)->b_blocknr
,
1713 (unsigned long long)path_leaf_bh(right
)->b_blocknr
);
1714 } while (left
->p_node
[i
].bh
->b_blocknr
==
1715 right
->p_node
[i
].bh
->b_blocknr
);
1720 typedef void (path_insert_t
)(void *, struct buffer_head
*);
1723 * Traverse a btree path in search of cpos, starting at root_el.
1725 * This code can be called with a cpos larger than the tree, in which
1726 * case it will return the rightmost path.
1728 static int __ocfs2_find_path(struct inode
*inode
,
1729 struct ocfs2_extent_list
*root_el
, u32 cpos
,
1730 path_insert_t
*func
, void *data
)
1735 struct buffer_head
*bh
= NULL
;
1736 struct ocfs2_extent_block
*eb
;
1737 struct ocfs2_extent_list
*el
;
1738 struct ocfs2_extent_rec
*rec
;
1739 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1742 while (el
->l_tree_depth
) {
1743 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1744 ocfs2_error(inode
->i_sb
,
1745 "Inode %llu has empty extent list at "
1747 (unsigned long long)oi
->ip_blkno
,
1748 le16_to_cpu(el
->l_tree_depth
));
1754 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
) - 1; i
++) {
1755 rec
= &el
->l_recs
[i
];
1758 * In the case that cpos is off the allocation
1759 * tree, this should just wind up returning the
1762 range
= le32_to_cpu(rec
->e_cpos
) +
1763 ocfs2_rec_clusters(el
, rec
);
1764 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
1768 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1770 ocfs2_error(inode
->i_sb
,
1771 "Inode %llu has bad blkno in extent list "
1772 "at depth %u (index %d)\n",
1773 (unsigned long long)oi
->ip_blkno
,
1774 le16_to_cpu(el
->l_tree_depth
), i
);
1781 ret
= ocfs2_read_extent_block(inode
, blkno
, &bh
);
1787 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1790 if (le16_to_cpu(el
->l_next_free_rec
) >
1791 le16_to_cpu(el
->l_count
)) {
1792 ocfs2_error(inode
->i_sb
,
1793 "Inode %llu has bad count in extent list "
1794 "at block %llu (next free=%u, count=%u)\n",
1795 (unsigned long long)oi
->ip_blkno
,
1796 (unsigned long long)bh
->b_blocknr
,
1797 le16_to_cpu(el
->l_next_free_rec
),
1798 le16_to_cpu(el
->l_count
));
1809 * Catch any trailing bh that the loop didn't handle.
1817 * Given an initialized path (that is, it has a valid root extent
1818 * list), this function will traverse the btree in search of the path
1819 * which would contain cpos.
1821 * The path traveled is recorded in the path structure.
1823 * Note that this will not do any comparisons on leaf node extent
1824 * records, so it will work fine in the case that we just added a tree
1827 struct find_path_data
{
1829 struct ocfs2_path
*path
;
1831 static void find_path_ins(void *data
, struct buffer_head
*bh
)
1833 struct find_path_data
*fp
= data
;
1836 ocfs2_path_insert_eb(fp
->path
, fp
->index
, bh
);
1839 static int ocfs2_find_path(struct inode
*inode
, struct ocfs2_path
*path
,
1842 struct find_path_data data
;
1846 return __ocfs2_find_path(inode
, path_root_el(path
), cpos
,
1847 find_path_ins
, &data
);
1850 static void find_leaf_ins(void *data
, struct buffer_head
*bh
)
1852 struct ocfs2_extent_block
*eb
=(struct ocfs2_extent_block
*)bh
->b_data
;
1853 struct ocfs2_extent_list
*el
= &eb
->h_list
;
1854 struct buffer_head
**ret
= data
;
1856 /* We want to retain only the leaf block. */
1857 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
1863 * Find the leaf block in the tree which would contain cpos. No
1864 * checking of the actual leaf is done.
1866 * Some paths want to call this instead of allocating a path structure
1867 * and calling ocfs2_find_path().
1869 * This function doesn't handle non btree extent lists.
1871 int ocfs2_find_leaf(struct inode
*inode
, struct ocfs2_extent_list
*root_el
,
1872 u32 cpos
, struct buffer_head
**leaf_bh
)
1875 struct buffer_head
*bh
= NULL
;
1877 ret
= __ocfs2_find_path(inode
, root_el
, cpos
, find_leaf_ins
, &bh
);
1889 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1891 * Basically, we've moved stuff around at the bottom of the tree and
1892 * we need to fix up the extent records above the changes to reflect
1895 * left_rec: the record on the left.
1896 * left_child_el: is the child list pointed to by left_rec
1897 * right_rec: the record to the right of left_rec
1898 * right_child_el: is the child list pointed to by right_rec
1900 * By definition, this only works on interior nodes.
1902 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec
*left_rec
,
1903 struct ocfs2_extent_list
*left_child_el
,
1904 struct ocfs2_extent_rec
*right_rec
,
1905 struct ocfs2_extent_list
*right_child_el
)
1907 u32 left_clusters
, right_end
;
1910 * Interior nodes never have holes. Their cpos is the cpos of
1911 * the leftmost record in their child list. Their cluster
1912 * count covers the full theoretical range of their child list
1913 * - the range between their cpos and the cpos of the record
1914 * immediately to their right.
1916 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[0].e_cpos
);
1917 if (!ocfs2_rec_clusters(right_child_el
, &right_child_el
->l_recs
[0])) {
1918 BUG_ON(right_child_el
->l_tree_depth
);
1919 BUG_ON(le16_to_cpu(right_child_el
->l_next_free_rec
) <= 1);
1920 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[1].e_cpos
);
1922 left_clusters
-= le32_to_cpu(left_rec
->e_cpos
);
1923 left_rec
->e_int_clusters
= cpu_to_le32(left_clusters
);
1926 * Calculate the rightmost cluster count boundary before
1927 * moving cpos - we will need to adjust clusters after
1928 * updating e_cpos to keep the same highest cluster count.
1930 right_end
= le32_to_cpu(right_rec
->e_cpos
);
1931 right_end
+= le32_to_cpu(right_rec
->e_int_clusters
);
1933 right_rec
->e_cpos
= left_rec
->e_cpos
;
1934 le32_add_cpu(&right_rec
->e_cpos
, left_clusters
);
1936 right_end
-= le32_to_cpu(right_rec
->e_cpos
);
1937 right_rec
->e_int_clusters
= cpu_to_le32(right_end
);
1941 * Adjust the adjacent root node records involved in a
1942 * rotation. left_el_blkno is passed in as a key so that we can easily
1943 * find it's index in the root list.
1945 static void ocfs2_adjust_root_records(struct ocfs2_extent_list
*root_el
,
1946 struct ocfs2_extent_list
*left_el
,
1947 struct ocfs2_extent_list
*right_el
,
1952 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) <=
1953 le16_to_cpu(left_el
->l_tree_depth
));
1955 for(i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
) - 1; i
++) {
1956 if (le64_to_cpu(root_el
->l_recs
[i
].e_blkno
) == left_el_blkno
)
1961 * The path walking code should have never returned a root and
1962 * two paths which are not adjacent.
1964 BUG_ON(i
>= (le16_to_cpu(root_el
->l_next_free_rec
) - 1));
1966 ocfs2_adjust_adjacent_records(&root_el
->l_recs
[i
], left_el
,
1967 &root_el
->l_recs
[i
+ 1], right_el
);
1971 * We've changed a leaf block (in right_path) and need to reflect that
1972 * change back up the subtree.
1974 * This happens in multiple places:
1975 * - When we've moved an extent record from the left path leaf to the right
1976 * path leaf to make room for an empty extent in the left path leaf.
1977 * - When our insert into the right path leaf is at the leftmost edge
1978 * and requires an update of the path immediately to it's left. This
1979 * can occur at the end of some types of rotation and appending inserts.
1980 * - When we've adjusted the last extent record in the left path leaf and the
1981 * 1st extent record in the right path leaf during cross extent block merge.
1983 static void ocfs2_complete_edge_insert(struct inode
*inode
, handle_t
*handle
,
1984 struct ocfs2_path
*left_path
,
1985 struct ocfs2_path
*right_path
,
1989 struct ocfs2_extent_list
*el
, *left_el
, *right_el
;
1990 struct ocfs2_extent_rec
*left_rec
, *right_rec
;
1991 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
1994 * Update the counts and position values within all the
1995 * interior nodes to reflect the leaf rotation we just did.
1997 * The root node is handled below the loop.
1999 * We begin the loop with right_el and left_el pointing to the
2000 * leaf lists and work our way up.
2002 * NOTE: within this loop, left_el and right_el always refer
2003 * to the *child* lists.
2005 left_el
= path_leaf_el(left_path
);
2006 right_el
= path_leaf_el(right_path
);
2007 for(i
= left_path
->p_tree_depth
- 1; i
> subtree_index
; i
--) {
2008 mlog(0, "Adjust records at index %u\n", i
);
2011 * One nice property of knowing that all of these
2012 * nodes are below the root is that we only deal with
2013 * the leftmost right node record and the rightmost
2016 el
= left_path
->p_node
[i
].el
;
2017 idx
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
2018 left_rec
= &el
->l_recs
[idx
];
2020 el
= right_path
->p_node
[i
].el
;
2021 right_rec
= &el
->l_recs
[0];
2023 ocfs2_adjust_adjacent_records(left_rec
, left_el
, right_rec
,
2026 ret
= ocfs2_journal_dirty(handle
, left_path
->p_node
[i
].bh
);
2030 ret
= ocfs2_journal_dirty(handle
, right_path
->p_node
[i
].bh
);
2035 * Setup our list pointers now so that the current
2036 * parents become children in the next iteration.
2038 left_el
= left_path
->p_node
[i
].el
;
2039 right_el
= right_path
->p_node
[i
].el
;
2043 * At the root node, adjust the two adjacent records which
2044 * begin our path to the leaves.
2047 el
= left_path
->p_node
[subtree_index
].el
;
2048 left_el
= left_path
->p_node
[subtree_index
+ 1].el
;
2049 right_el
= right_path
->p_node
[subtree_index
+ 1].el
;
2051 ocfs2_adjust_root_records(el
, left_el
, right_el
,
2052 left_path
->p_node
[subtree_index
+ 1].bh
->b_blocknr
);
2054 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2056 ret
= ocfs2_journal_dirty(handle
, root_bh
);
2061 static int ocfs2_rotate_subtree_right(struct inode
*inode
,
2063 struct ocfs2_path
*left_path
,
2064 struct ocfs2_path
*right_path
,
2068 struct buffer_head
*right_leaf_bh
;
2069 struct buffer_head
*left_leaf_bh
= NULL
;
2070 struct buffer_head
*root_bh
;
2071 struct ocfs2_extent_list
*right_el
, *left_el
;
2072 struct ocfs2_extent_rec move_rec
;
2074 left_leaf_bh
= path_leaf_bh(left_path
);
2075 left_el
= path_leaf_el(left_path
);
2077 if (left_el
->l_next_free_rec
!= left_el
->l_count
) {
2078 ocfs2_error(inode
->i_sb
,
2079 "Inode %llu has non-full interior leaf node %llu"
2081 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2082 (unsigned long long)left_leaf_bh
->b_blocknr
,
2083 le16_to_cpu(left_el
->l_next_free_rec
));
2088 * This extent block may already have an empty record, so we
2089 * return early if so.
2091 if (ocfs2_is_empty_extent(&left_el
->l_recs
[0]))
2094 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2095 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2097 ret
= ocfs2_path_bh_journal_access(handle
, inode
, right_path
,
2104 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
2105 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
2112 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
2120 right_leaf_bh
= path_leaf_bh(right_path
);
2121 right_el
= path_leaf_el(right_path
);
2123 /* This is a code error, not a disk corruption. */
2124 mlog_bug_on_msg(!right_el
->l_next_free_rec
, "Inode %llu: Rotate fails "
2125 "because rightmost leaf block %llu is empty\n",
2126 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2127 (unsigned long long)right_leaf_bh
->b_blocknr
);
2129 ocfs2_create_empty_extent(right_el
);
2131 ret
= ocfs2_journal_dirty(handle
, right_leaf_bh
);
2137 /* Do the copy now. */
2138 i
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
2139 move_rec
= left_el
->l_recs
[i
];
2140 right_el
->l_recs
[0] = move_rec
;
2143 * Clear out the record we just copied and shift everything
2144 * over, leaving an empty extent in the left leaf.
2146 * We temporarily subtract from next_free_rec so that the
2147 * shift will lose the tail record (which is now defunct).
2149 le16_add_cpu(&left_el
->l_next_free_rec
, -1);
2150 ocfs2_shift_records_right(left_el
);
2151 memset(&left_el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2152 le16_add_cpu(&left_el
->l_next_free_rec
, 1);
2154 ret
= ocfs2_journal_dirty(handle
, left_leaf_bh
);
2160 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
2168 * Given a full path, determine what cpos value would return us a path
2169 * containing the leaf immediately to the left of the current one.
2171 * Will return zero if the path passed in is already the leftmost path.
2173 static int ocfs2_find_cpos_for_left_leaf(struct super_block
*sb
,
2174 struct ocfs2_path
*path
, u32
*cpos
)
2178 struct ocfs2_extent_list
*el
;
2180 BUG_ON(path
->p_tree_depth
== 0);
2184 blkno
= path_leaf_bh(path
)->b_blocknr
;
2186 /* Start at the tree node just above the leaf and work our way up. */
2187 i
= path
->p_tree_depth
- 1;
2189 el
= path
->p_node
[i
].el
;
2192 * Find the extent record just before the one in our
2195 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
2196 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
2200 * We've determined that the
2201 * path specified is already
2202 * the leftmost one - return a
2208 * The leftmost record points to our
2209 * leaf - we need to travel up the
2215 *cpos
= le32_to_cpu(el
->l_recs
[j
- 1].e_cpos
);
2216 *cpos
= *cpos
+ ocfs2_rec_clusters(el
,
2217 &el
->l_recs
[j
- 1]);
2224 * If we got here, we never found a valid node where
2225 * the tree indicated one should be.
2228 "Invalid extent tree at extent block %llu\n",
2229 (unsigned long long)blkno
);
2234 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2243 * Extend the transaction by enough credits to complete the rotation,
2244 * and still leave at least the original number of credits allocated
2245 * to this transaction.
2247 static int ocfs2_extend_rotate_transaction(handle_t
*handle
, int subtree_depth
,
2249 struct ocfs2_path
*path
)
2251 int credits
= (path
->p_tree_depth
- subtree_depth
) * 2 + 1 + op_credits
;
2253 if (handle
->h_buffer_credits
< credits
)
2254 return ocfs2_extend_trans(handle
, credits
);
2260 * Trap the case where we're inserting into the theoretical range past
2261 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2262 * whose cpos is less than ours into the right leaf.
2264 * It's only necessary to look at the rightmost record of the left
2265 * leaf because the logic that calls us should ensure that the
2266 * theoretical ranges in the path components above the leaves are
2269 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path
*left_path
,
2272 struct ocfs2_extent_list
*left_el
;
2273 struct ocfs2_extent_rec
*rec
;
2276 left_el
= path_leaf_el(left_path
);
2277 next_free
= le16_to_cpu(left_el
->l_next_free_rec
);
2278 rec
= &left_el
->l_recs
[next_free
- 1];
2280 if (insert_cpos
> le32_to_cpu(rec
->e_cpos
))
2285 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list
*el
, u32 cpos
)
2287 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
2289 struct ocfs2_extent_rec
*rec
;
2294 rec
= &el
->l_recs
[0];
2295 if (ocfs2_is_empty_extent(rec
)) {
2299 rec
= &el
->l_recs
[1];
2302 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2303 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
2309 * Rotate all the records in a btree right one record, starting at insert_cpos.
2311 * The path to the rightmost leaf should be passed in.
2313 * The array is assumed to be large enough to hold an entire path (tree depth).
2315 * Upon succesful return from this function:
2317 * - The 'right_path' array will contain a path to the leaf block
2318 * whose range contains e_cpos.
2319 * - That leaf block will have a single empty extent in list index 0.
2320 * - In the case that the rotation requires a post-insert update,
2321 * *ret_left_path will contain a valid path which can be passed to
2322 * ocfs2_insert_path().
2324 static int ocfs2_rotate_tree_right(struct inode
*inode
,
2326 enum ocfs2_split_type split
,
2328 struct ocfs2_path
*right_path
,
2329 struct ocfs2_path
**ret_left_path
)
2331 int ret
, start
, orig_credits
= handle
->h_buffer_credits
;
2333 struct ocfs2_path
*left_path
= NULL
;
2335 *ret_left_path
= NULL
;
2337 left_path
= ocfs2_new_path_from_path(right_path
);
2344 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
, &cpos
);
2350 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos
, cpos
);
2353 * What we want to do here is:
2355 * 1) Start with the rightmost path.
2357 * 2) Determine a path to the leaf block directly to the left
2360 * 3) Determine the 'subtree root' - the lowest level tree node
2361 * which contains a path to both leaves.
2363 * 4) Rotate the subtree.
2365 * 5) Find the next subtree by considering the left path to be
2366 * the new right path.
2368 * The check at the top of this while loop also accepts
2369 * insert_cpos == cpos because cpos is only a _theoretical_
2370 * value to get us the left path - insert_cpos might very well
2371 * be filling that hole.
2373 * Stop at a cpos of '0' because we either started at the
2374 * leftmost branch (i.e., a tree with one branch and a
2375 * rotation inside of it), or we've gone as far as we can in
2376 * rotating subtrees.
2378 while (cpos
&& insert_cpos
<= cpos
) {
2379 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2382 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
2388 mlog_bug_on_msg(path_leaf_bh(left_path
) ==
2389 path_leaf_bh(right_path
),
2390 "Inode %lu: error during insert of %u "
2391 "(left path cpos %u) results in two identical "
2392 "paths ending at %llu\n",
2393 inode
->i_ino
, insert_cpos
, cpos
,
2394 (unsigned long long)
2395 path_leaf_bh(left_path
)->b_blocknr
);
2397 if (split
== SPLIT_NONE
&&
2398 ocfs2_rotate_requires_path_adjustment(left_path
,
2402 * We've rotated the tree as much as we
2403 * should. The rest is up to
2404 * ocfs2_insert_path() to complete, after the
2405 * record insertion. We indicate this
2406 * situation by returning the left path.
2408 * The reason we don't adjust the records here
2409 * before the record insert is that an error
2410 * later might break the rule where a parent
2411 * record e_cpos will reflect the actual
2412 * e_cpos of the 1st nonempty record of the
2415 *ret_left_path
= left_path
;
2419 start
= ocfs2_find_subtree_root(inode
, left_path
, right_path
);
2421 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2423 (unsigned long long) right_path
->p_node
[start
].bh
->b_blocknr
,
2424 right_path
->p_tree_depth
);
2426 ret
= ocfs2_extend_rotate_transaction(handle
, start
,
2427 orig_credits
, right_path
);
2433 ret
= ocfs2_rotate_subtree_right(inode
, handle
, left_path
,
2440 if (split
!= SPLIT_NONE
&&
2441 ocfs2_leftmost_rec_contains(path_leaf_el(right_path
),
2444 * A rotate moves the rightmost left leaf
2445 * record over to the leftmost right leaf
2446 * slot. If we're doing an extent split
2447 * instead of a real insert, then we have to
2448 * check that the extent to be split wasn't
2449 * just moved over. If it was, then we can
2450 * exit here, passing left_path back -
2451 * ocfs2_split_extent() is smart enough to
2452 * search both leaves.
2454 *ret_left_path
= left_path
;
2459 * There is no need to re-read the next right path
2460 * as we know that it'll be our current left
2461 * path. Optimize by copying values instead.
2463 ocfs2_mv_path(right_path
, left_path
);
2465 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
2474 ocfs2_free_path(left_path
);
2480 static int ocfs2_update_edge_lengths(struct inode
*inode
, handle_t
*handle
,
2481 int subtree_index
, struct ocfs2_path
*path
)
2484 struct ocfs2_extent_rec
*rec
;
2485 struct ocfs2_extent_list
*el
;
2486 struct ocfs2_extent_block
*eb
;
2490 * In normal tree rotation process, we will never touch the
2491 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2492 * doesn't reserve the credits for them either.
2494 * But we do have a special case here which will update the rightmost
2495 * records for all the bh in the path.
2496 * So we have to allocate extra credits and access them.
2498 ret
= ocfs2_extend_trans(handle
,
2499 handle
->h_buffer_credits
+ subtree_index
);
2505 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
2511 /* Path should always be rightmost. */
2512 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
2513 BUG_ON(eb
->h_next_leaf_blk
!= 0ULL);
2516 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
2517 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2518 rec
= &el
->l_recs
[idx
];
2519 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2521 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
2522 el
= path
->p_node
[i
].el
;
2523 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2524 rec
= &el
->l_recs
[idx
];
2526 rec
->e_int_clusters
= cpu_to_le32(range
);
2527 le32_add_cpu(&rec
->e_int_clusters
, -le32_to_cpu(rec
->e_cpos
));
2529 ocfs2_journal_dirty(handle
, path
->p_node
[i
].bh
);
2535 static void ocfs2_unlink_path(struct inode
*inode
, handle_t
*handle
,
2536 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2537 struct ocfs2_path
*path
, int unlink_start
)
2540 struct ocfs2_extent_block
*eb
;
2541 struct ocfs2_extent_list
*el
;
2542 struct buffer_head
*bh
;
2544 for(i
= unlink_start
; i
< path_num_items(path
); i
++) {
2545 bh
= path
->p_node
[i
].bh
;
2547 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
2549 * Not all nodes might have had their final count
2550 * decremented by the caller - handle this here.
2553 if (le16_to_cpu(el
->l_next_free_rec
) > 1) {
2555 "Inode %llu, attempted to remove extent block "
2556 "%llu with %u records\n",
2557 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2558 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
2559 le16_to_cpu(el
->l_next_free_rec
));
2561 ocfs2_journal_dirty(handle
, bh
);
2562 ocfs2_remove_from_cache(inode
, bh
);
2566 el
->l_next_free_rec
= 0;
2567 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2569 ocfs2_journal_dirty(handle
, bh
);
2571 ret
= ocfs2_cache_extent_block_free(dealloc
, eb
);
2575 ocfs2_remove_from_cache(inode
, bh
);
2579 static void ocfs2_unlink_subtree(struct inode
*inode
, handle_t
*handle
,
2580 struct ocfs2_path
*left_path
,
2581 struct ocfs2_path
*right_path
,
2583 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2586 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
2587 struct ocfs2_extent_list
*root_el
= left_path
->p_node
[subtree_index
].el
;
2588 struct ocfs2_extent_list
*el
;
2589 struct ocfs2_extent_block
*eb
;
2591 el
= path_leaf_el(left_path
);
2593 eb
= (struct ocfs2_extent_block
*)right_path
->p_node
[subtree_index
+ 1].bh
->b_data
;
2595 for(i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
2596 if (root_el
->l_recs
[i
].e_blkno
== eb
->h_blkno
)
2599 BUG_ON(i
>= le16_to_cpu(root_el
->l_next_free_rec
));
2601 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
2602 le16_add_cpu(&root_el
->l_next_free_rec
, -1);
2604 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2605 eb
->h_next_leaf_blk
= 0;
2607 ocfs2_journal_dirty(handle
, root_bh
);
2608 ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2610 ocfs2_unlink_path(inode
, handle
, dealloc
, right_path
,
2614 static int ocfs2_rotate_subtree_left(struct inode
*inode
, handle_t
*handle
,
2615 struct ocfs2_path
*left_path
,
2616 struct ocfs2_path
*right_path
,
2618 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2620 struct ocfs2_extent_tree
*et
)
2622 int ret
, i
, del_right_subtree
= 0, right_has_empty
= 0;
2623 struct buffer_head
*root_bh
, *et_root_bh
= path_root_bh(right_path
);
2624 struct ocfs2_extent_list
*right_leaf_el
, *left_leaf_el
;
2625 struct ocfs2_extent_block
*eb
;
2629 right_leaf_el
= path_leaf_el(right_path
);
2630 left_leaf_el
= path_leaf_el(left_path
);
2631 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2632 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2634 if (!ocfs2_is_empty_extent(&left_leaf_el
->l_recs
[0]))
2637 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(right_path
)->b_data
;
2638 if (ocfs2_is_empty_extent(&right_leaf_el
->l_recs
[0])) {
2640 * It's legal for us to proceed if the right leaf is
2641 * the rightmost one and it has an empty extent. There
2642 * are two cases to handle - whether the leaf will be
2643 * empty after removal or not. If the leaf isn't empty
2644 * then just remove the empty extent up front. The
2645 * next block will handle empty leaves by flagging
2648 * Non rightmost leaves will throw -EAGAIN and the
2649 * caller can manually move the subtree and retry.
2652 if (eb
->h_next_leaf_blk
!= 0ULL)
2655 if (le16_to_cpu(right_leaf_el
->l_next_free_rec
) > 1) {
2656 ret
= ocfs2_journal_access_eb(handle
, inode
,
2657 path_leaf_bh(right_path
),
2658 OCFS2_JOURNAL_ACCESS_WRITE
);
2664 ocfs2_remove_empty_extent(right_leaf_el
);
2666 right_has_empty
= 1;
2669 if (eb
->h_next_leaf_blk
== 0ULL &&
2670 le16_to_cpu(right_leaf_el
->l_next_free_rec
) == 1) {
2672 * We have to update i_last_eb_blk during the meta
2675 ret
= ocfs2_et_root_journal_access(handle
, inode
, et
,
2676 OCFS2_JOURNAL_ACCESS_WRITE
);
2682 del_right_subtree
= 1;
2686 * Getting here with an empty extent in the right path implies
2687 * that it's the rightmost path and will be deleted.
2689 BUG_ON(right_has_empty
&& !del_right_subtree
);
2691 ret
= ocfs2_path_bh_journal_access(handle
, inode
, right_path
,
2698 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
2699 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
2706 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
2714 if (!right_has_empty
) {
2716 * Only do this if we're moving a real
2717 * record. Otherwise, the action is delayed until
2718 * after removal of the right path in which case we
2719 * can do a simple shift to remove the empty extent.
2721 ocfs2_rotate_leaf(left_leaf_el
, &right_leaf_el
->l_recs
[0]);
2722 memset(&right_leaf_el
->l_recs
[0], 0,
2723 sizeof(struct ocfs2_extent_rec
));
2725 if (eb
->h_next_leaf_blk
== 0ULL) {
2727 * Move recs over to get rid of empty extent, decrease
2728 * next_free. This is allowed to remove the last
2729 * extent in our leaf (setting l_next_free_rec to
2730 * zero) - the delete code below won't care.
2732 ocfs2_remove_empty_extent(right_leaf_el
);
2735 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2738 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
2742 if (del_right_subtree
) {
2743 ocfs2_unlink_subtree(inode
, handle
, left_path
, right_path
,
2744 subtree_index
, dealloc
);
2745 ret
= ocfs2_update_edge_lengths(inode
, handle
, subtree_index
,
2752 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2753 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
2756 * Removal of the extent in the left leaf was skipped
2757 * above so we could delete the right path
2760 if (right_has_empty
)
2761 ocfs2_remove_empty_extent(left_leaf_el
);
2763 ret
= ocfs2_journal_dirty(handle
, et_root_bh
);
2769 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
2777 * Given a full path, determine what cpos value would return us a path
2778 * containing the leaf immediately to the right of the current one.
2780 * Will return zero if the path passed in is already the rightmost path.
2782 * This looks similar, but is subtly different to
2783 * ocfs2_find_cpos_for_left_leaf().
2785 static int ocfs2_find_cpos_for_right_leaf(struct super_block
*sb
,
2786 struct ocfs2_path
*path
, u32
*cpos
)
2790 struct ocfs2_extent_list
*el
;
2794 if (path
->p_tree_depth
== 0)
2797 blkno
= path_leaf_bh(path
)->b_blocknr
;
2799 /* Start at the tree node just above the leaf and work our way up. */
2800 i
= path
->p_tree_depth
- 1;
2804 el
= path
->p_node
[i
].el
;
2807 * Find the extent record just after the one in our
2810 next_free
= le16_to_cpu(el
->l_next_free_rec
);
2811 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
2812 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
2813 if (j
== (next_free
- 1)) {
2816 * We've determined that the
2817 * path specified is already
2818 * the rightmost one - return a
2824 * The rightmost record points to our
2825 * leaf - we need to travel up the
2831 *cpos
= le32_to_cpu(el
->l_recs
[j
+ 1].e_cpos
);
2837 * If we got here, we never found a valid node where
2838 * the tree indicated one should be.
2841 "Invalid extent tree at extent block %llu\n",
2842 (unsigned long long)blkno
);
2847 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2855 static int ocfs2_rotate_rightmost_leaf_left(struct inode
*inode
,
2857 struct ocfs2_path
*path
)
2860 struct buffer_head
*bh
= path_leaf_bh(path
);
2861 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
2863 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2866 ret
= ocfs2_path_bh_journal_access(handle
, inode
, path
,
2867 path_num_items(path
) - 1);
2873 ocfs2_remove_empty_extent(el
);
2875 ret
= ocfs2_journal_dirty(handle
, bh
);
2883 static int __ocfs2_rotate_tree_left(struct inode
*inode
,
2884 handle_t
*handle
, int orig_credits
,
2885 struct ocfs2_path
*path
,
2886 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2887 struct ocfs2_path
**empty_extent_path
,
2888 struct ocfs2_extent_tree
*et
)
2890 int ret
, subtree_root
, deleted
;
2892 struct ocfs2_path
*left_path
= NULL
;
2893 struct ocfs2_path
*right_path
= NULL
;
2895 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path
)->l_recs
[0])));
2897 *empty_extent_path
= NULL
;
2899 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, path
,
2906 left_path
= ocfs2_new_path_from_path(path
);
2913 ocfs2_cp_path(left_path
, path
);
2915 right_path
= ocfs2_new_path_from_path(path
);
2922 while (right_cpos
) {
2923 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
2929 subtree_root
= ocfs2_find_subtree_root(inode
, left_path
,
2932 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2934 (unsigned long long)
2935 right_path
->p_node
[subtree_root
].bh
->b_blocknr
,
2936 right_path
->p_tree_depth
);
2938 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_root
,
2939 orig_credits
, left_path
);
2946 * Caller might still want to make changes to the
2947 * tree root, so re-add it to the journal here.
2949 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
2956 ret
= ocfs2_rotate_subtree_left(inode
, handle
, left_path
,
2957 right_path
, subtree_root
,
2958 dealloc
, &deleted
, et
);
2959 if (ret
== -EAGAIN
) {
2961 * The rotation has to temporarily stop due to
2962 * the right subtree having an empty
2963 * extent. Pass it back to the caller for a
2966 *empty_extent_path
= right_path
;
2976 * The subtree rotate might have removed records on
2977 * the rightmost edge. If so, then rotation is
2983 ocfs2_mv_path(left_path
, right_path
);
2985 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
2994 ocfs2_free_path(right_path
);
2995 ocfs2_free_path(left_path
);
3000 static int ocfs2_remove_rightmost_path(struct inode
*inode
, handle_t
*handle
,
3001 struct ocfs2_path
*path
,
3002 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3003 struct ocfs2_extent_tree
*et
)
3005 int ret
, subtree_index
;
3007 struct ocfs2_path
*left_path
= NULL
;
3008 struct ocfs2_extent_block
*eb
;
3009 struct ocfs2_extent_list
*el
;
3012 ret
= ocfs2_et_sanity_check(inode
, et
);
3016 * There's two ways we handle this depending on
3017 * whether path is the only existing one.
3019 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
3020 handle
->h_buffer_credits
,
3027 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
3033 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
3041 * We have a path to the left of this one - it needs
3044 left_path
= ocfs2_new_path_from_path(path
);
3051 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
3057 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
3063 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
3065 ocfs2_unlink_subtree(inode
, handle
, left_path
, path
,
3066 subtree_index
, dealloc
);
3067 ret
= ocfs2_update_edge_lengths(inode
, handle
, subtree_index
,
3074 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
3075 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
3078 * 'path' is also the leftmost path which
3079 * means it must be the only one. This gets
3080 * handled differently because we want to
3081 * revert the inode back to having extents
3084 ocfs2_unlink_path(inode
, handle
, dealloc
, path
, 1);
3086 el
= et
->et_root_el
;
3087 el
->l_tree_depth
= 0;
3088 el
->l_next_free_rec
= 0;
3089 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
3091 ocfs2_et_set_last_eb_blk(et
, 0);
3094 ocfs2_journal_dirty(handle
, path_root_bh(path
));
3097 ocfs2_free_path(left_path
);
3102 * Left rotation of btree records.
3104 * In many ways, this is (unsurprisingly) the opposite of right
3105 * rotation. We start at some non-rightmost path containing an empty
3106 * extent in the leaf block. The code works its way to the rightmost
3107 * path by rotating records to the left in every subtree.
3109 * This is used by any code which reduces the number of extent records
3110 * in a leaf. After removal, an empty record should be placed in the
3111 * leftmost list position.
3113 * This won't handle a length update of the rightmost path records if
3114 * the rightmost tree leaf record is removed so the caller is
3115 * responsible for detecting and correcting that.
3117 static int ocfs2_rotate_tree_left(struct inode
*inode
, handle_t
*handle
,
3118 struct ocfs2_path
*path
,
3119 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3120 struct ocfs2_extent_tree
*et
)
3122 int ret
, orig_credits
= handle
->h_buffer_credits
;
3123 struct ocfs2_path
*tmp_path
= NULL
, *restart_path
= NULL
;
3124 struct ocfs2_extent_block
*eb
;
3125 struct ocfs2_extent_list
*el
;
3127 el
= path_leaf_el(path
);
3128 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
3131 if (path
->p_tree_depth
== 0) {
3132 rightmost_no_delete
:
3134 * Inline extents. This is trivially handled, so do
3137 ret
= ocfs2_rotate_rightmost_leaf_left(inode
, handle
,
3145 * Handle rightmost branch now. There's several cases:
3146 * 1) simple rotation leaving records in there. That's trivial.
3147 * 2) rotation requiring a branch delete - there's no more
3148 * records left. Two cases of this:
3149 * a) There are branches to the left.
3150 * b) This is also the leftmost (the only) branch.
3152 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3153 * 2a) we need the left branch so that we can update it with the unlink
3154 * 2b) we need to bring the inode back to inline extents.
3157 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
3159 if (eb
->h_next_leaf_blk
== 0) {
3161 * This gets a bit tricky if we're going to delete the
3162 * rightmost path. Get the other cases out of the way
3165 if (le16_to_cpu(el
->l_next_free_rec
) > 1)
3166 goto rightmost_no_delete
;
3168 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
3170 ocfs2_error(inode
->i_sb
,
3171 "Inode %llu has empty extent block at %llu",
3172 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
3173 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
3178 * XXX: The caller can not trust "path" any more after
3179 * this as it will have been deleted. What do we do?
3181 * In theory the rotate-for-merge code will never get
3182 * here because it'll always ask for a rotate in a
3186 ret
= ocfs2_remove_rightmost_path(inode
, handle
, path
,
3194 * Now we can loop, remembering the path we get from -EAGAIN
3195 * and restarting from there.
3198 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
, path
,
3199 dealloc
, &restart_path
, et
);
3200 if (ret
&& ret
!= -EAGAIN
) {
3205 while (ret
== -EAGAIN
) {
3206 tmp_path
= restart_path
;
3207 restart_path
= NULL
;
3209 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
,
3212 if (ret
&& ret
!= -EAGAIN
) {
3217 ocfs2_free_path(tmp_path
);
3225 ocfs2_free_path(tmp_path
);
3226 ocfs2_free_path(restart_path
);
3230 static void ocfs2_cleanup_merge(struct ocfs2_extent_list
*el
,
3233 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[index
];
3236 if (rec
->e_leaf_clusters
== 0) {
3238 * We consumed all of the merged-from record. An empty
3239 * extent cannot exist anywhere but the 1st array
3240 * position, so move things over if the merged-from
3241 * record doesn't occupy that position.
3243 * This creates a new empty extent so the caller
3244 * should be smart enough to have removed any existing
3248 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
3249 size
= index
* sizeof(struct ocfs2_extent_rec
);
3250 memmove(&el
->l_recs
[1], &el
->l_recs
[0], size
);
3254 * Always memset - the caller doesn't check whether it
3255 * created an empty extent, so there could be junk in
3258 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
3262 static int ocfs2_get_right_path(struct inode
*inode
,
3263 struct ocfs2_path
*left_path
,
3264 struct ocfs2_path
**ret_right_path
)
3268 struct ocfs2_path
*right_path
= NULL
;
3269 struct ocfs2_extent_list
*left_el
;
3271 *ret_right_path
= NULL
;
3273 /* This function shouldn't be called for non-trees. */
3274 BUG_ON(left_path
->p_tree_depth
== 0);
3276 left_el
= path_leaf_el(left_path
);
3277 BUG_ON(left_el
->l_next_free_rec
!= left_el
->l_count
);
3279 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
3286 /* This function shouldn't be called for the rightmost leaf. */
3287 BUG_ON(right_cpos
== 0);
3289 right_path
= ocfs2_new_path_from_path(left_path
);
3296 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
3302 *ret_right_path
= right_path
;
3305 ocfs2_free_path(right_path
);
3310 * Remove split_rec clusters from the record at index and merge them
3311 * onto the beginning of the record "next" to it.
3312 * For index < l_count - 1, the next means the extent rec at index + 1.
3313 * For index == l_count - 1, the "next" means the 1st extent rec of the
3314 * next extent block.
3316 static int ocfs2_merge_rec_right(struct inode
*inode
,
3317 struct ocfs2_path
*left_path
,
3319 struct ocfs2_extent_rec
*split_rec
,
3322 int ret
, next_free
, i
;
3323 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3324 struct ocfs2_extent_rec
*left_rec
;
3325 struct ocfs2_extent_rec
*right_rec
;
3326 struct ocfs2_extent_list
*right_el
;
3327 struct ocfs2_path
*right_path
= NULL
;
3328 int subtree_index
= 0;
3329 struct ocfs2_extent_list
*el
= path_leaf_el(left_path
);
3330 struct buffer_head
*bh
= path_leaf_bh(left_path
);
3331 struct buffer_head
*root_bh
= NULL
;
3333 BUG_ON(index
>= le16_to_cpu(el
->l_next_free_rec
));
3334 left_rec
= &el
->l_recs
[index
];
3336 if (index
== le16_to_cpu(el
->l_next_free_rec
) - 1 &&
3337 le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
)) {
3338 /* we meet with a cross extent block merge. */
3339 ret
= ocfs2_get_right_path(inode
, left_path
, &right_path
);
3345 right_el
= path_leaf_el(right_path
);
3346 next_free
= le16_to_cpu(right_el
->l_next_free_rec
);
3347 BUG_ON(next_free
<= 0);
3348 right_rec
= &right_el
->l_recs
[0];
3349 if (ocfs2_is_empty_extent(right_rec
)) {
3350 BUG_ON(next_free
<= 1);
3351 right_rec
= &right_el
->l_recs
[1];
3354 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3355 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3356 le32_to_cpu(right_rec
->e_cpos
));
3358 subtree_index
= ocfs2_find_subtree_root(inode
,
3359 left_path
, right_path
);
3361 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3362 handle
->h_buffer_credits
,
3369 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3370 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3372 ret
= ocfs2_path_bh_journal_access(handle
, inode
, right_path
,
3379 for (i
= subtree_index
+ 1;
3380 i
< path_num_items(right_path
); i
++) {
3381 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
3388 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
3397 BUG_ON(index
== le16_to_cpu(el
->l_next_free_rec
) - 1);
3398 right_rec
= &el
->l_recs
[index
+ 1];
3401 ret
= ocfs2_path_bh_journal_access(handle
, inode
, left_path
,
3402 path_num_items(left_path
) - 1);
3408 le16_add_cpu(&left_rec
->e_leaf_clusters
, -split_clusters
);
3410 le32_add_cpu(&right_rec
->e_cpos
, -split_clusters
);
3411 le64_add_cpu(&right_rec
->e_blkno
,
3412 -ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
3413 le16_add_cpu(&right_rec
->e_leaf_clusters
, split_clusters
);
3415 ocfs2_cleanup_merge(el
, index
);
3417 ret
= ocfs2_journal_dirty(handle
, bh
);
3422 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
3426 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3427 right_path
, subtree_index
);
3431 ocfs2_free_path(right_path
);
3435 static int ocfs2_get_left_path(struct inode
*inode
,
3436 struct ocfs2_path
*right_path
,
3437 struct ocfs2_path
**ret_left_path
)
3441 struct ocfs2_path
*left_path
= NULL
;
3443 *ret_left_path
= NULL
;
3445 /* This function shouldn't be called for non-trees. */
3446 BUG_ON(right_path
->p_tree_depth
== 0);
3448 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
3449 right_path
, &left_cpos
);
3455 /* This function shouldn't be called for the leftmost leaf. */
3456 BUG_ON(left_cpos
== 0);
3458 left_path
= ocfs2_new_path_from_path(right_path
);
3465 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
3471 *ret_left_path
= left_path
;
3474 ocfs2_free_path(left_path
);
3479 * Remove split_rec clusters from the record at index and merge them
3480 * onto the tail of the record "before" it.
3481 * For index > 0, the "before" means the extent rec at index - 1.
3483 * For index == 0, the "before" means the last record of the previous
3484 * extent block. And there is also a situation that we may need to
3485 * remove the rightmost leaf extent block in the right_path and change
3486 * the right path to indicate the new rightmost path.
3488 static int ocfs2_merge_rec_left(struct inode
*inode
,
3489 struct ocfs2_path
*right_path
,
3491 struct ocfs2_extent_rec
*split_rec
,
3492 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3493 struct ocfs2_extent_tree
*et
,
3496 int ret
, i
, subtree_index
= 0, has_empty_extent
= 0;
3497 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3498 struct ocfs2_extent_rec
*left_rec
;
3499 struct ocfs2_extent_rec
*right_rec
;
3500 struct ocfs2_extent_list
*el
= path_leaf_el(right_path
);
3501 struct buffer_head
*bh
= path_leaf_bh(right_path
);
3502 struct buffer_head
*root_bh
= NULL
;
3503 struct ocfs2_path
*left_path
= NULL
;
3504 struct ocfs2_extent_list
*left_el
;
3508 right_rec
= &el
->l_recs
[index
];
3510 /* we meet with a cross extent block merge. */
3511 ret
= ocfs2_get_left_path(inode
, right_path
, &left_path
);
3517 left_el
= path_leaf_el(left_path
);
3518 BUG_ON(le16_to_cpu(left_el
->l_next_free_rec
) !=
3519 le16_to_cpu(left_el
->l_count
));
3521 left_rec
= &left_el
->l_recs
[
3522 le16_to_cpu(left_el
->l_next_free_rec
) - 1];
3523 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3524 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3525 le32_to_cpu(split_rec
->e_cpos
));
3527 subtree_index
= ocfs2_find_subtree_root(inode
,
3528 left_path
, right_path
);
3530 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3531 handle
->h_buffer_credits
,
3538 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3539 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3541 ret
= ocfs2_path_bh_journal_access(handle
, inode
, right_path
,
3548 for (i
= subtree_index
+ 1;
3549 i
< path_num_items(right_path
); i
++) {
3550 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
3557 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
3565 left_rec
= &el
->l_recs
[index
- 1];
3566 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
3567 has_empty_extent
= 1;
3570 ret
= ocfs2_path_bh_journal_access(handle
, inode
, right_path
,
3571 path_num_items(right_path
) - 1);
3577 if (has_empty_extent
&& index
== 1) {
3579 * The easy case - we can just plop the record right in.
3581 *left_rec
= *split_rec
;
3583 has_empty_extent
= 0;
3585 le16_add_cpu(&left_rec
->e_leaf_clusters
, split_clusters
);
3587 le32_add_cpu(&right_rec
->e_cpos
, split_clusters
);
3588 le64_add_cpu(&right_rec
->e_blkno
,
3589 ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
3590 le16_add_cpu(&right_rec
->e_leaf_clusters
, -split_clusters
);
3592 ocfs2_cleanup_merge(el
, index
);
3594 ret
= ocfs2_journal_dirty(handle
, bh
);
3599 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
3604 * In the situation that the right_rec is empty and the extent
3605 * block is empty also, ocfs2_complete_edge_insert can't handle
3606 * it and we need to delete the right extent block.
3608 if (le16_to_cpu(right_rec
->e_leaf_clusters
) == 0 &&
3609 le16_to_cpu(el
->l_next_free_rec
) == 1) {
3611 ret
= ocfs2_remove_rightmost_path(inode
, handle
,
3619 /* Now the rightmost extent block has been deleted.
3620 * So we use the new rightmost path.
3622 ocfs2_mv_path(right_path
, left_path
);
3625 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3626 right_path
, subtree_index
);
3630 ocfs2_free_path(left_path
);
3634 static int ocfs2_try_to_merge_extent(struct inode
*inode
,
3636 struct ocfs2_path
*path
,
3638 struct ocfs2_extent_rec
*split_rec
,
3639 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3640 struct ocfs2_merge_ctxt
*ctxt
,
3641 struct ocfs2_extent_tree
*et
)
3645 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
3646 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
3648 BUG_ON(ctxt
->c_contig_type
== CONTIG_NONE
);
3650 if (ctxt
->c_split_covers_rec
&& ctxt
->c_has_empty_extent
) {
3652 * The merge code will need to create an empty
3653 * extent to take the place of the newly
3654 * emptied slot. Remove any pre-existing empty
3655 * extents - having more than one in a leaf is
3658 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3665 rec
= &el
->l_recs
[split_index
];
3668 if (ctxt
->c_contig_type
== CONTIG_LEFTRIGHT
) {
3670 * Left-right contig implies this.
3672 BUG_ON(!ctxt
->c_split_covers_rec
);
3675 * Since the leftright insert always covers the entire
3676 * extent, this call will delete the insert record
3677 * entirely, resulting in an empty extent record added to
3680 * Since the adding of an empty extent shifts
3681 * everything back to the right, there's no need to
3682 * update split_index here.
3684 * When the split_index is zero, we need to merge it to the
3685 * prevoius extent block. It is more efficient and easier
3686 * if we do merge_right first and merge_left later.
3688 ret
= ocfs2_merge_rec_right(inode
, path
,
3697 * We can only get this from logic error above.
3699 BUG_ON(!ocfs2_is_empty_extent(&el
->l_recs
[0]));
3701 /* The merge left us with an empty extent, remove it. */
3702 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3709 rec
= &el
->l_recs
[split_index
];
3712 * Note that we don't pass split_rec here on purpose -
3713 * we've merged it into the rec already.
3715 ret
= ocfs2_merge_rec_left(inode
, path
,
3725 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3728 * Error from this last rotate is not critical, so
3729 * print but don't bubble it up.
3736 * Merge a record to the left or right.
3738 * 'contig_type' is relative to the existing record,
3739 * so for example, if we're "right contig", it's to
3740 * the record on the left (hence the left merge).
3742 if (ctxt
->c_contig_type
== CONTIG_RIGHT
) {
3743 ret
= ocfs2_merge_rec_left(inode
,
3753 ret
= ocfs2_merge_rec_right(inode
,
3763 if (ctxt
->c_split_covers_rec
) {
3765 * The merge may have left an empty extent in
3766 * our leaf. Try to rotate it away.
3768 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3780 static void ocfs2_subtract_from_rec(struct super_block
*sb
,
3781 enum ocfs2_split_type split
,
3782 struct ocfs2_extent_rec
*rec
,
3783 struct ocfs2_extent_rec
*split_rec
)
3787 len_blocks
= ocfs2_clusters_to_blocks(sb
,
3788 le16_to_cpu(split_rec
->e_leaf_clusters
));
3790 if (split
== SPLIT_LEFT
) {
3792 * Region is on the left edge of the existing
3795 le32_add_cpu(&rec
->e_cpos
,
3796 le16_to_cpu(split_rec
->e_leaf_clusters
));
3797 le64_add_cpu(&rec
->e_blkno
, len_blocks
);
3798 le16_add_cpu(&rec
->e_leaf_clusters
,
3799 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3802 * Region is on the right edge of the existing
3805 le16_add_cpu(&rec
->e_leaf_clusters
,
3806 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3811 * Do the final bits of extent record insertion at the target leaf
3812 * list. If this leaf is part of an allocation tree, it is assumed
3813 * that the tree above has been prepared.
3815 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec
*insert_rec
,
3816 struct ocfs2_extent_list
*el
,
3817 struct ocfs2_insert_type
*insert
,
3818 struct inode
*inode
)
3820 int i
= insert
->ins_contig_index
;
3822 struct ocfs2_extent_rec
*rec
;
3824 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
3826 if (insert
->ins_split
!= SPLIT_NONE
) {
3827 i
= ocfs2_search_extent_list(el
, le32_to_cpu(insert_rec
->e_cpos
));
3829 rec
= &el
->l_recs
[i
];
3830 ocfs2_subtract_from_rec(inode
->i_sb
, insert
->ins_split
, rec
,
3836 * Contiguous insert - either left or right.
3838 if (insert
->ins_contig
!= CONTIG_NONE
) {
3839 rec
= &el
->l_recs
[i
];
3840 if (insert
->ins_contig
== CONTIG_LEFT
) {
3841 rec
->e_blkno
= insert_rec
->e_blkno
;
3842 rec
->e_cpos
= insert_rec
->e_cpos
;
3844 le16_add_cpu(&rec
->e_leaf_clusters
,
3845 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3850 * Handle insert into an empty leaf.
3852 if (le16_to_cpu(el
->l_next_free_rec
) == 0 ||
3853 ((le16_to_cpu(el
->l_next_free_rec
) == 1) &&
3854 ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3855 el
->l_recs
[0] = *insert_rec
;
3856 el
->l_next_free_rec
= cpu_to_le16(1);
3863 if (insert
->ins_appending
== APPEND_TAIL
) {
3864 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
3865 rec
= &el
->l_recs
[i
];
3866 range
= le32_to_cpu(rec
->e_cpos
)
3867 + le16_to_cpu(rec
->e_leaf_clusters
);
3868 BUG_ON(le32_to_cpu(insert_rec
->e_cpos
) < range
);
3870 mlog_bug_on_msg(le16_to_cpu(el
->l_next_free_rec
) >=
3871 le16_to_cpu(el
->l_count
),
3872 "inode %lu, depth %u, count %u, next free %u, "
3873 "rec.cpos %u, rec.clusters %u, "
3874 "insert.cpos %u, insert.clusters %u\n",
3876 le16_to_cpu(el
->l_tree_depth
),
3877 le16_to_cpu(el
->l_count
),
3878 le16_to_cpu(el
->l_next_free_rec
),
3879 le32_to_cpu(el
->l_recs
[i
].e_cpos
),
3880 le16_to_cpu(el
->l_recs
[i
].e_leaf_clusters
),
3881 le32_to_cpu(insert_rec
->e_cpos
),
3882 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3884 el
->l_recs
[i
] = *insert_rec
;
3885 le16_add_cpu(&el
->l_next_free_rec
, 1);
3891 * Ok, we have to rotate.
3893 * At this point, it is safe to assume that inserting into an
3894 * empty leaf and appending to a leaf have both been handled
3897 * This leaf needs to have space, either by the empty 1st
3898 * extent record, or by virtue of an l_next_rec < l_count.
3900 ocfs2_rotate_leaf(el
, insert_rec
);
3903 static void ocfs2_adjust_rightmost_records(struct inode
*inode
,
3905 struct ocfs2_path
*path
,
3906 struct ocfs2_extent_rec
*insert_rec
)
3908 int ret
, i
, next_free
;
3909 struct buffer_head
*bh
;
3910 struct ocfs2_extent_list
*el
;
3911 struct ocfs2_extent_rec
*rec
;
3914 * Update everything except the leaf block.
3916 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
3917 bh
= path
->p_node
[i
].bh
;
3918 el
= path
->p_node
[i
].el
;
3920 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3921 if (next_free
== 0) {
3922 ocfs2_error(inode
->i_sb
,
3923 "Dinode %llu has a bad extent list",
3924 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3929 rec
= &el
->l_recs
[next_free
- 1];
3931 rec
->e_int_clusters
= insert_rec
->e_cpos
;
3932 le32_add_cpu(&rec
->e_int_clusters
,
3933 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3934 le32_add_cpu(&rec
->e_int_clusters
,
3935 -le32_to_cpu(rec
->e_cpos
));
3937 ret
= ocfs2_journal_dirty(handle
, bh
);
3944 static int ocfs2_append_rec_to_path(struct inode
*inode
, handle_t
*handle
,
3945 struct ocfs2_extent_rec
*insert_rec
,
3946 struct ocfs2_path
*right_path
,
3947 struct ocfs2_path
**ret_left_path
)
3950 struct ocfs2_extent_list
*el
;
3951 struct ocfs2_path
*left_path
= NULL
;
3953 *ret_left_path
= NULL
;
3956 * This shouldn't happen for non-trees. The extent rec cluster
3957 * count manipulation below only works for interior nodes.
3959 BUG_ON(right_path
->p_tree_depth
== 0);
3962 * If our appending insert is at the leftmost edge of a leaf,
3963 * then we might need to update the rightmost records of the
3966 el
= path_leaf_el(right_path
);
3967 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3968 if (next_free
== 0 ||
3969 (next_free
== 1 && ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3972 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
3979 mlog(0, "Append may need a left path update. cpos: %u, "
3980 "left_cpos: %u\n", le32_to_cpu(insert_rec
->e_cpos
),
3984 * No need to worry if the append is already in the
3988 left_path
= ocfs2_new_path_from_path(right_path
);
3995 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
4002 * ocfs2_insert_path() will pass the left_path to the
4008 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
4014 ocfs2_adjust_rightmost_records(inode
, handle
, right_path
, insert_rec
);
4016 *ret_left_path
= left_path
;
4020 ocfs2_free_path(left_path
);
4025 static void ocfs2_split_record(struct inode
*inode
,
4026 struct ocfs2_path
*left_path
,
4027 struct ocfs2_path
*right_path
,
4028 struct ocfs2_extent_rec
*split_rec
,
4029 enum ocfs2_split_type split
)
4032 u32 cpos
= le32_to_cpu(split_rec
->e_cpos
);
4033 struct ocfs2_extent_list
*left_el
= NULL
, *right_el
, *insert_el
, *el
;
4034 struct ocfs2_extent_rec
*rec
, *tmprec
;
4036 right_el
= path_leaf_el(right_path
);
4038 left_el
= path_leaf_el(left_path
);
4041 insert_el
= right_el
;
4042 index
= ocfs2_search_extent_list(el
, cpos
);
4044 if (index
== 0 && left_path
) {
4045 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
4048 * This typically means that the record
4049 * started in the left path but moved to the
4050 * right as a result of rotation. We either
4051 * move the existing record to the left, or we
4052 * do the later insert there.
4054 * In this case, the left path should always
4055 * exist as the rotate code will have passed
4056 * it back for a post-insert update.
4059 if (split
== SPLIT_LEFT
) {
4061 * It's a left split. Since we know
4062 * that the rotate code gave us an
4063 * empty extent in the left path, we
4064 * can just do the insert there.
4066 insert_el
= left_el
;
4069 * Right split - we have to move the
4070 * existing record over to the left
4071 * leaf. The insert will be into the
4072 * newly created empty extent in the
4075 tmprec
= &right_el
->l_recs
[index
];
4076 ocfs2_rotate_leaf(left_el
, tmprec
);
4079 memset(tmprec
, 0, sizeof(*tmprec
));
4080 index
= ocfs2_search_extent_list(left_el
, cpos
);
4081 BUG_ON(index
== -1);
4086 BUG_ON(!ocfs2_is_empty_extent(&left_el
->l_recs
[0]));
4088 * Left path is easy - we can just allow the insert to
4092 insert_el
= left_el
;
4093 index
= ocfs2_search_extent_list(el
, cpos
);
4094 BUG_ON(index
== -1);
4097 rec
= &el
->l_recs
[index
];
4098 ocfs2_subtract_from_rec(inode
->i_sb
, split
, rec
, split_rec
);
4099 ocfs2_rotate_leaf(insert_el
, split_rec
);
4103 * This function only does inserts on an allocation b-tree. For tree
4104 * depth = 0, ocfs2_insert_at_leaf() is called directly.
4106 * right_path is the path we want to do the actual insert
4107 * in. left_path should only be passed in if we need to update that
4108 * portion of the tree after an edge insert.
4110 static int ocfs2_insert_path(struct inode
*inode
,
4112 struct ocfs2_path
*left_path
,
4113 struct ocfs2_path
*right_path
,
4114 struct ocfs2_extent_rec
*insert_rec
,
4115 struct ocfs2_insert_type
*insert
)
4117 int ret
, subtree_index
;
4118 struct buffer_head
*leaf_bh
= path_leaf_bh(right_path
);
4121 int credits
= handle
->h_buffer_credits
;
4124 * There's a chance that left_path got passed back to
4125 * us without being accounted for in the
4126 * journal. Extend our transaction here to be sure we
4127 * can change those blocks.
4129 credits
+= left_path
->p_tree_depth
;
4131 ret
= ocfs2_extend_trans(handle
, credits
);
4137 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
4145 * Pass both paths to the journal. The majority of inserts
4146 * will be touching all components anyway.
4148 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
4154 if (insert
->ins_split
!= SPLIT_NONE
) {
4156 * We could call ocfs2_insert_at_leaf() for some types
4157 * of splits, but it's easier to just let one separate
4158 * function sort it all out.
4160 ocfs2_split_record(inode
, left_path
, right_path
,
4161 insert_rec
, insert
->ins_split
);
4164 * Split might have modified either leaf and we don't
4165 * have a guarantee that the later edge insert will
4166 * dirty this for us.
4169 ret
= ocfs2_journal_dirty(handle
,
4170 path_leaf_bh(left_path
));
4174 ocfs2_insert_at_leaf(insert_rec
, path_leaf_el(right_path
),
4177 ret
= ocfs2_journal_dirty(handle
, leaf_bh
);
4183 * The rotate code has indicated that we need to fix
4184 * up portions of the tree after the insert.
4186 * XXX: Should we extend the transaction here?
4188 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
,
4190 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
4191 right_path
, subtree_index
);
4199 static int ocfs2_do_insert_extent(struct inode
*inode
,
4201 struct ocfs2_extent_tree
*et
,
4202 struct ocfs2_extent_rec
*insert_rec
,
4203 struct ocfs2_insert_type
*type
)
4205 int ret
, rotate
= 0;
4207 struct ocfs2_path
*right_path
= NULL
;
4208 struct ocfs2_path
*left_path
= NULL
;
4209 struct ocfs2_extent_list
*el
;
4211 el
= et
->et_root_el
;
4213 ret
= ocfs2_et_root_journal_access(handle
, inode
, et
,
4214 OCFS2_JOURNAL_ACCESS_WRITE
);
4220 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
4221 ocfs2_insert_at_leaf(insert_rec
, el
, type
, inode
);
4222 goto out_update_clusters
;
4225 right_path
= ocfs2_new_path_from_et(et
);
4233 * Determine the path to start with. Rotations need the
4234 * rightmost path, everything else can go directly to the
4237 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4238 if (type
->ins_appending
== APPEND_NONE
&&
4239 type
->ins_contig
== CONTIG_NONE
) {
4244 ret
= ocfs2_find_path(inode
, right_path
, cpos
);
4251 * Rotations and appends need special treatment - they modify
4252 * parts of the tree's above them.
4254 * Both might pass back a path immediate to the left of the
4255 * one being inserted to. This will be cause
4256 * ocfs2_insert_path() to modify the rightmost records of
4257 * left_path to account for an edge insert.
4259 * XXX: When modifying this code, keep in mind that an insert
4260 * can wind up skipping both of these two special cases...
4263 ret
= ocfs2_rotate_tree_right(inode
, handle
, type
->ins_split
,
4264 le32_to_cpu(insert_rec
->e_cpos
),
4265 right_path
, &left_path
);
4272 * ocfs2_rotate_tree_right() might have extended the
4273 * transaction without re-journaling our tree root.
4275 ret
= ocfs2_et_root_journal_access(handle
, inode
, et
,
4276 OCFS2_JOURNAL_ACCESS_WRITE
);
4281 } else if (type
->ins_appending
== APPEND_TAIL
4282 && type
->ins_contig
!= CONTIG_LEFT
) {
4283 ret
= ocfs2_append_rec_to_path(inode
, handle
, insert_rec
,
4284 right_path
, &left_path
);
4291 ret
= ocfs2_insert_path(inode
, handle
, left_path
, right_path
,
4298 out_update_clusters
:
4299 if (type
->ins_split
== SPLIT_NONE
)
4300 ocfs2_et_update_clusters(inode
, et
,
4301 le16_to_cpu(insert_rec
->e_leaf_clusters
));
4303 ret
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
4308 ocfs2_free_path(left_path
);
4309 ocfs2_free_path(right_path
);
4314 static enum ocfs2_contig_type
4315 ocfs2_figure_merge_contig_type(struct inode
*inode
, struct ocfs2_path
*path
,
4316 struct ocfs2_extent_list
*el
, int index
,
4317 struct ocfs2_extent_rec
*split_rec
)
4320 enum ocfs2_contig_type ret
= CONTIG_NONE
;
4321 u32 left_cpos
, right_cpos
;
4322 struct ocfs2_extent_rec
*rec
= NULL
;
4323 struct ocfs2_extent_list
*new_el
;
4324 struct ocfs2_path
*left_path
= NULL
, *right_path
= NULL
;
4325 struct buffer_head
*bh
;
4326 struct ocfs2_extent_block
*eb
;
4329 rec
= &el
->l_recs
[index
- 1];
4330 } else if (path
->p_tree_depth
> 0) {
4331 status
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
4336 if (left_cpos
!= 0) {
4337 left_path
= ocfs2_new_path_from_path(path
);
4341 status
= ocfs2_find_path(inode
, left_path
, left_cpos
);
4345 new_el
= path_leaf_el(left_path
);
4347 if (le16_to_cpu(new_el
->l_next_free_rec
) !=
4348 le16_to_cpu(new_el
->l_count
)) {
4349 bh
= path_leaf_bh(left_path
);
4350 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4351 ocfs2_error(inode
->i_sb
,
4352 "Extent block #%llu has an "
4353 "invalid l_next_free_rec of "
4354 "%d. It should have "
4355 "matched the l_count of %d",
4356 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
4357 le16_to_cpu(new_el
->l_next_free_rec
),
4358 le16_to_cpu(new_el
->l_count
));
4362 rec
= &new_el
->l_recs
[
4363 le16_to_cpu(new_el
->l_next_free_rec
) - 1];
4368 * We're careful to check for an empty extent record here -
4369 * the merge code will know what to do if it sees one.
4372 if (index
== 1 && ocfs2_is_empty_extent(rec
)) {
4373 if (split_rec
->e_cpos
== el
->l_recs
[index
].e_cpos
)
4376 ret
= ocfs2_extent_contig(inode
, rec
, split_rec
);
4381 if (index
< (le16_to_cpu(el
->l_next_free_rec
) - 1))
4382 rec
= &el
->l_recs
[index
+ 1];
4383 else if (le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
) &&
4384 path
->p_tree_depth
> 0) {
4385 status
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
,
4390 if (right_cpos
== 0)
4393 right_path
= ocfs2_new_path_from_path(path
);
4397 status
= ocfs2_find_path(inode
, right_path
, right_cpos
);
4401 new_el
= path_leaf_el(right_path
);
4402 rec
= &new_el
->l_recs
[0];
4403 if (ocfs2_is_empty_extent(rec
)) {
4404 if (le16_to_cpu(new_el
->l_next_free_rec
) <= 1) {
4405 bh
= path_leaf_bh(right_path
);
4406 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4407 ocfs2_error(inode
->i_sb
,
4408 "Extent block #%llu has an "
4409 "invalid l_next_free_rec of %d",
4410 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
4411 le16_to_cpu(new_el
->l_next_free_rec
));
4415 rec
= &new_el
->l_recs
[1];
4420 enum ocfs2_contig_type contig_type
;
4422 contig_type
= ocfs2_extent_contig(inode
, rec
, split_rec
);
4424 if (contig_type
== CONTIG_LEFT
&& ret
== CONTIG_RIGHT
)
4425 ret
= CONTIG_LEFTRIGHT
;
4426 else if (ret
== CONTIG_NONE
)
4432 ocfs2_free_path(left_path
);
4434 ocfs2_free_path(right_path
);
4439 static void ocfs2_figure_contig_type(struct inode
*inode
,
4440 struct ocfs2_insert_type
*insert
,
4441 struct ocfs2_extent_list
*el
,
4442 struct ocfs2_extent_rec
*insert_rec
,
4443 struct ocfs2_extent_tree
*et
)
4446 enum ocfs2_contig_type contig_type
= CONTIG_NONE
;
4448 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4450 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
4451 contig_type
= ocfs2_extent_contig(inode
, &el
->l_recs
[i
],
4453 if (contig_type
!= CONTIG_NONE
) {
4454 insert
->ins_contig_index
= i
;
4458 insert
->ins_contig
= contig_type
;
4460 if (insert
->ins_contig
!= CONTIG_NONE
) {
4461 struct ocfs2_extent_rec
*rec
=
4462 &el
->l_recs
[insert
->ins_contig_index
];
4463 unsigned int len
= le16_to_cpu(rec
->e_leaf_clusters
) +
4464 le16_to_cpu(insert_rec
->e_leaf_clusters
);
4467 * Caller might want us to limit the size of extents, don't
4468 * calculate contiguousness if we might exceed that limit.
4470 if (et
->et_max_leaf_clusters
&&
4471 (len
> et
->et_max_leaf_clusters
))
4472 insert
->ins_contig
= CONTIG_NONE
;
4477 * This should only be called against the righmost leaf extent list.
4479 * ocfs2_figure_appending_type() will figure out whether we'll have to
4480 * insert at the tail of the rightmost leaf.
4482 * This should also work against the root extent list for tree's with 0
4483 * depth. If we consider the root extent list to be the rightmost leaf node
4484 * then the logic here makes sense.
4486 static void ocfs2_figure_appending_type(struct ocfs2_insert_type
*insert
,
4487 struct ocfs2_extent_list
*el
,
4488 struct ocfs2_extent_rec
*insert_rec
)
4491 u32 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4492 struct ocfs2_extent_rec
*rec
;
4494 insert
->ins_appending
= APPEND_NONE
;
4496 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4498 if (!el
->l_next_free_rec
)
4499 goto set_tail_append
;
4501 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
4502 /* Were all records empty? */
4503 if (le16_to_cpu(el
->l_next_free_rec
) == 1)
4504 goto set_tail_append
;
4507 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
4508 rec
= &el
->l_recs
[i
];
4511 (le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)))
4512 goto set_tail_append
;
4517 insert
->ins_appending
= APPEND_TAIL
;
4521 * Helper function called at the begining of an insert.
4523 * This computes a few things that are commonly used in the process of
4524 * inserting into the btree:
4525 * - Whether the new extent is contiguous with an existing one.
4526 * - The current tree depth.
4527 * - Whether the insert is an appending one.
4528 * - The total # of free records in the tree.
4530 * All of the information is stored on the ocfs2_insert_type
4533 static int ocfs2_figure_insert_type(struct inode
*inode
,
4534 struct ocfs2_extent_tree
*et
,
4535 struct buffer_head
**last_eb_bh
,
4536 struct ocfs2_extent_rec
*insert_rec
,
4538 struct ocfs2_insert_type
*insert
)
4541 struct ocfs2_extent_block
*eb
;
4542 struct ocfs2_extent_list
*el
;
4543 struct ocfs2_path
*path
= NULL
;
4544 struct buffer_head
*bh
= NULL
;
4546 insert
->ins_split
= SPLIT_NONE
;
4548 el
= et
->et_root_el
;
4549 insert
->ins_tree_depth
= le16_to_cpu(el
->l_tree_depth
);
4551 if (el
->l_tree_depth
) {
4553 * If we have tree depth, we read in the
4554 * rightmost extent block ahead of time as
4555 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4556 * may want it later.
4558 ret
= ocfs2_read_extent_block(inode
,
4559 ocfs2_et_get_last_eb_blk(et
),
4565 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
4570 * Unless we have a contiguous insert, we'll need to know if
4571 * there is room left in our allocation tree for another
4574 * XXX: This test is simplistic, we can search for empty
4575 * extent records too.
4577 *free_records
= le16_to_cpu(el
->l_count
) -
4578 le16_to_cpu(el
->l_next_free_rec
);
4580 if (!insert
->ins_tree_depth
) {
4581 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4582 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4586 path
= ocfs2_new_path_from_et(et
);
4594 * In the case that we're inserting past what the tree
4595 * currently accounts for, ocfs2_find_path() will return for
4596 * us the rightmost tree path. This is accounted for below in
4597 * the appending code.
4599 ret
= ocfs2_find_path(inode
, path
, le32_to_cpu(insert_rec
->e_cpos
));
4605 el
= path_leaf_el(path
);
4608 * Now that we have the path, there's two things we want to determine:
4609 * 1) Contiguousness (also set contig_index if this is so)
4611 * 2) Are we doing an append? We can trivially break this up
4612 * into two types of appends: simple record append, or a
4613 * rotate inside the tail leaf.
4615 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4618 * The insert code isn't quite ready to deal with all cases of
4619 * left contiguousness. Specifically, if it's an insert into
4620 * the 1st record in a leaf, it will require the adjustment of
4621 * cluster count on the last record of the path directly to it's
4622 * left. For now, just catch that case and fool the layers
4623 * above us. This works just fine for tree_depth == 0, which
4624 * is why we allow that above.
4626 if (insert
->ins_contig
== CONTIG_LEFT
&&
4627 insert
->ins_contig_index
== 0)
4628 insert
->ins_contig
= CONTIG_NONE
;
4631 * Ok, so we can simply compare against last_eb to figure out
4632 * whether the path doesn't exist. This will only happen in
4633 * the case that we're doing a tail append, so maybe we can
4634 * take advantage of that information somehow.
4636 if (ocfs2_et_get_last_eb_blk(et
) ==
4637 path_leaf_bh(path
)->b_blocknr
) {
4639 * Ok, ocfs2_find_path() returned us the rightmost
4640 * tree path. This might be an appending insert. There are
4642 * 1) We're doing a true append at the tail:
4643 * -This might even be off the end of the leaf
4644 * 2) We're "appending" by rotating in the tail
4646 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4650 ocfs2_free_path(path
);
4660 * Insert an extent into an inode btree.
4662 * The caller needs to update fe->i_clusters
4664 int ocfs2_insert_extent(struct ocfs2_super
*osb
,
4666 struct inode
*inode
,
4667 struct ocfs2_extent_tree
*et
,
4672 struct ocfs2_alloc_context
*meta_ac
)
4675 int uninitialized_var(free_records
);
4676 struct buffer_head
*last_eb_bh
= NULL
;
4677 struct ocfs2_insert_type insert
= {0, };
4678 struct ocfs2_extent_rec rec
;
4680 mlog(0, "add %u clusters at position %u to inode %llu\n",
4681 new_clusters
, cpos
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4683 memset(&rec
, 0, sizeof(rec
));
4684 rec
.e_cpos
= cpu_to_le32(cpos
);
4685 rec
.e_blkno
= cpu_to_le64(start_blk
);
4686 rec
.e_leaf_clusters
= cpu_to_le16(new_clusters
);
4687 rec
.e_flags
= flags
;
4688 status
= ocfs2_et_insert_check(inode
, et
, &rec
);
4694 status
= ocfs2_figure_insert_type(inode
, et
, &last_eb_bh
, &rec
,
4695 &free_records
, &insert
);
4701 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4702 "Insert.contig_index: %d, Insert.free_records: %d, "
4703 "Insert.tree_depth: %d\n",
4704 insert
.ins_appending
, insert
.ins_contig
, insert
.ins_contig_index
,
4705 free_records
, insert
.ins_tree_depth
);
4707 if (insert
.ins_contig
== CONTIG_NONE
&& free_records
== 0) {
4708 status
= ocfs2_grow_tree(inode
, handle
, et
,
4709 &insert
.ins_tree_depth
, &last_eb_bh
,
4717 /* Finally, we can add clusters. This might rotate the tree for us. */
4718 status
= ocfs2_do_insert_extent(inode
, handle
, et
, &rec
, &insert
);
4721 else if (et
->et_ops
== &ocfs2_dinode_et_ops
)
4722 ocfs2_extent_map_insert_rec(inode
, &rec
);
4732 * Allcate and add clusters into the extent b-tree.
4733 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4734 * The extent b-tree's root is specified by et, and
4735 * it is not limited to the file storage. Any extent tree can use this
4736 * function if it implements the proper ocfs2_extent_tree.
4738 int ocfs2_add_clusters_in_btree(struct ocfs2_super
*osb
,
4739 struct inode
*inode
,
4740 u32
*logical_offset
,
4741 u32 clusters_to_add
,
4743 struct ocfs2_extent_tree
*et
,
4745 struct ocfs2_alloc_context
*data_ac
,
4746 struct ocfs2_alloc_context
*meta_ac
,
4747 enum ocfs2_alloc_restarted
*reason_ret
)
4751 enum ocfs2_alloc_restarted reason
= RESTART_NONE
;
4752 u32 bit_off
, num_bits
;
4756 BUG_ON(!clusters_to_add
);
4759 flags
= OCFS2_EXT_UNWRITTEN
;
4761 free_extents
= ocfs2_num_free_extents(osb
, inode
, et
);
4762 if (free_extents
< 0) {
4763 status
= free_extents
;
4768 /* there are two cases which could cause us to EAGAIN in the
4769 * we-need-more-metadata case:
4770 * 1) we haven't reserved *any*
4771 * 2) we are so fragmented, we've needed to add metadata too
4773 if (!free_extents
&& !meta_ac
) {
4774 mlog(0, "we haven't reserved any metadata!\n");
4776 reason
= RESTART_META
;
4778 } else if ((!free_extents
)
4779 && (ocfs2_alloc_context_bits_left(meta_ac
)
4780 < ocfs2_extend_meta_needed(et
->et_root_el
))) {
4781 mlog(0, "filesystem is really fragmented...\n");
4783 reason
= RESTART_META
;
4787 status
= __ocfs2_claim_clusters(osb
, handle
, data_ac
, 1,
4788 clusters_to_add
, &bit_off
, &num_bits
);
4790 if (status
!= -ENOSPC
)
4795 BUG_ON(num_bits
> clusters_to_add
);
4797 /* reserve our write early -- insert_extent may update the tree root */
4798 status
= ocfs2_et_root_journal_access(handle
, inode
, et
,
4799 OCFS2_JOURNAL_ACCESS_WRITE
);
4805 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
4806 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4807 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4808 status
= ocfs2_insert_extent(osb
, handle
, inode
, et
,
4809 *logical_offset
, block
,
4810 num_bits
, flags
, meta_ac
);
4816 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
4822 clusters_to_add
-= num_bits
;
4823 *logical_offset
+= num_bits
;
4825 if (clusters_to_add
) {
4826 mlog(0, "need to alloc once more, wanted = %u\n",
4829 reason
= RESTART_TRANS
;
4835 *reason_ret
= reason
;
4839 static void ocfs2_make_right_split_rec(struct super_block
*sb
,
4840 struct ocfs2_extent_rec
*split_rec
,
4842 struct ocfs2_extent_rec
*rec
)
4844 u32 rec_cpos
= le32_to_cpu(rec
->e_cpos
);
4845 u32 rec_range
= rec_cpos
+ le16_to_cpu(rec
->e_leaf_clusters
);
4847 memset(split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
4849 split_rec
->e_cpos
= cpu_to_le32(cpos
);
4850 split_rec
->e_leaf_clusters
= cpu_to_le16(rec_range
- cpos
);
4852 split_rec
->e_blkno
= rec
->e_blkno
;
4853 le64_add_cpu(&split_rec
->e_blkno
,
4854 ocfs2_clusters_to_blocks(sb
, cpos
- rec_cpos
));
4856 split_rec
->e_flags
= rec
->e_flags
;
4859 static int ocfs2_split_and_insert(struct inode
*inode
,
4861 struct ocfs2_path
*path
,
4862 struct ocfs2_extent_tree
*et
,
4863 struct buffer_head
**last_eb_bh
,
4865 struct ocfs2_extent_rec
*orig_split_rec
,
4866 struct ocfs2_alloc_context
*meta_ac
)
4869 unsigned int insert_range
, rec_range
, do_leftright
= 0;
4870 struct ocfs2_extent_rec tmprec
;
4871 struct ocfs2_extent_list
*rightmost_el
;
4872 struct ocfs2_extent_rec rec
;
4873 struct ocfs2_extent_rec split_rec
= *orig_split_rec
;
4874 struct ocfs2_insert_type insert
;
4875 struct ocfs2_extent_block
*eb
;
4879 * Store a copy of the record on the stack - it might move
4880 * around as the tree is manipulated below.
4882 rec
= path_leaf_el(path
)->l_recs
[split_index
];
4884 rightmost_el
= et
->et_root_el
;
4886 depth
= le16_to_cpu(rightmost_el
->l_tree_depth
);
4888 BUG_ON(!(*last_eb_bh
));
4889 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
4890 rightmost_el
= &eb
->h_list
;
4893 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
4894 le16_to_cpu(rightmost_el
->l_count
)) {
4895 ret
= ocfs2_grow_tree(inode
, handle
, et
,
4896 &depth
, last_eb_bh
, meta_ac
);
4903 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
4904 insert
.ins_appending
= APPEND_NONE
;
4905 insert
.ins_contig
= CONTIG_NONE
;
4906 insert
.ins_tree_depth
= depth
;
4908 insert_range
= le32_to_cpu(split_rec
.e_cpos
) +
4909 le16_to_cpu(split_rec
.e_leaf_clusters
);
4910 rec_range
= le32_to_cpu(rec
.e_cpos
) +
4911 le16_to_cpu(rec
.e_leaf_clusters
);
4913 if (split_rec
.e_cpos
== rec
.e_cpos
) {
4914 insert
.ins_split
= SPLIT_LEFT
;
4915 } else if (insert_range
== rec_range
) {
4916 insert
.ins_split
= SPLIT_RIGHT
;
4919 * Left/right split. We fake this as a right split
4920 * first and then make a second pass as a left split.
4922 insert
.ins_split
= SPLIT_RIGHT
;
4924 ocfs2_make_right_split_rec(inode
->i_sb
, &tmprec
, insert_range
,
4929 BUG_ON(do_leftright
);
4933 ret
= ocfs2_do_insert_extent(inode
, handle
, et
, &split_rec
, &insert
);
4939 if (do_leftright
== 1) {
4941 struct ocfs2_extent_list
*el
;
4944 split_rec
= *orig_split_rec
;
4946 ocfs2_reinit_path(path
, 1);
4948 cpos
= le32_to_cpu(split_rec
.e_cpos
);
4949 ret
= ocfs2_find_path(inode
, path
, cpos
);
4955 el
= path_leaf_el(path
);
4956 split_index
= ocfs2_search_extent_list(el
, cpos
);
4964 static int ocfs2_replace_extent_rec(struct inode
*inode
,
4966 struct ocfs2_path
*path
,
4967 struct ocfs2_extent_list
*el
,
4969 struct ocfs2_extent_rec
*split_rec
)
4973 ret
= ocfs2_path_bh_journal_access(handle
, inode
, path
,
4974 path_num_items(path
) - 1);
4980 el
->l_recs
[split_index
] = *split_rec
;
4982 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
4988 * Mark part or all of the extent record at split_index in the leaf
4989 * pointed to by path as written. This removes the unwritten
4992 * Care is taken to handle contiguousness so as to not grow the tree.
4994 * meta_ac is not strictly necessary - we only truly need it if growth
4995 * of the tree is required. All other cases will degrade into a less
4996 * optimal tree layout.
4998 * last_eb_bh should be the rightmost leaf block for any extent
4999 * btree. Since a split may grow the tree or a merge might shrink it,
5000 * the caller cannot trust the contents of that buffer after this call.
5002 * This code is optimized for readability - several passes might be
5003 * made over certain portions of the tree. All of those blocks will
5004 * have been brought into cache (and pinned via the journal), so the
5005 * extra overhead is not expressed in terms of disk reads.
5007 static int __ocfs2_mark_extent_written(struct inode
*inode
,
5008 struct ocfs2_extent_tree
*et
,
5010 struct ocfs2_path
*path
,
5012 struct ocfs2_extent_rec
*split_rec
,
5013 struct ocfs2_alloc_context
*meta_ac
,
5014 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5017 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
5018 struct buffer_head
*last_eb_bh
= NULL
;
5019 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
5020 struct ocfs2_merge_ctxt ctxt
;
5021 struct ocfs2_extent_list
*rightmost_el
;
5023 if (!(rec
->e_flags
& OCFS2_EXT_UNWRITTEN
)) {
5029 if (le32_to_cpu(rec
->e_cpos
) > le32_to_cpu(split_rec
->e_cpos
) ||
5030 ((le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)) <
5031 (le32_to_cpu(split_rec
->e_cpos
) + le16_to_cpu(split_rec
->e_leaf_clusters
)))) {
5037 ctxt
.c_contig_type
= ocfs2_figure_merge_contig_type(inode
, path
, el
,
5042 * The core merge / split code wants to know how much room is
5043 * left in this inodes allocation tree, so we pass the
5044 * rightmost extent list.
5046 if (path
->p_tree_depth
) {
5047 struct ocfs2_extent_block
*eb
;
5049 ret
= ocfs2_read_extent_block(inode
,
5050 ocfs2_et_get_last_eb_blk(et
),
5057 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
5058 rightmost_el
= &eb
->h_list
;
5060 rightmost_el
= path_root_el(path
);
5062 if (rec
->e_cpos
== split_rec
->e_cpos
&&
5063 rec
->e_leaf_clusters
== split_rec
->e_leaf_clusters
)
5064 ctxt
.c_split_covers_rec
= 1;
5066 ctxt
.c_split_covers_rec
= 0;
5068 ctxt
.c_has_empty_extent
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
5070 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5071 split_index
, ctxt
.c_contig_type
, ctxt
.c_has_empty_extent
,
5072 ctxt
.c_split_covers_rec
);
5074 if (ctxt
.c_contig_type
== CONTIG_NONE
) {
5075 if (ctxt
.c_split_covers_rec
)
5076 ret
= ocfs2_replace_extent_rec(inode
, handle
,
5078 split_index
, split_rec
);
5080 ret
= ocfs2_split_and_insert(inode
, handle
, path
, et
,
5081 &last_eb_bh
, split_index
,
5082 split_rec
, meta_ac
);
5086 ret
= ocfs2_try_to_merge_extent(inode
, handle
, path
,
5087 split_index
, split_rec
,
5088 dealloc
, &ctxt
, et
);
5099 * Mark the already-existing extent at cpos as written for len clusters.
5101 * If the existing extent is larger than the request, initiate a
5102 * split. An attempt will be made at merging with adjacent extents.
5104 * The caller is responsible for passing down meta_ac if we'll need it.
5106 int ocfs2_mark_extent_written(struct inode
*inode
,
5107 struct ocfs2_extent_tree
*et
,
5108 handle_t
*handle
, u32 cpos
, u32 len
, u32 phys
,
5109 struct ocfs2_alloc_context
*meta_ac
,
5110 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5113 u64 start_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys
);
5114 struct ocfs2_extent_rec split_rec
;
5115 struct ocfs2_path
*left_path
= NULL
;
5116 struct ocfs2_extent_list
*el
;
5118 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5119 inode
->i_ino
, cpos
, len
, phys
, (unsigned long long)start_blkno
);
5121 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode
->i_sb
))) {
5122 ocfs2_error(inode
->i_sb
, "Inode %llu has unwritten extents "
5123 "that are being written to, but the feature bit "
5124 "is not set in the super block.",
5125 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
5131 * XXX: This should be fixed up so that we just re-insert the
5132 * next extent records.
5134 * XXX: This is a hack on the extent tree, maybe it should be
5137 if (et
->et_ops
== &ocfs2_dinode_et_ops
)
5138 ocfs2_extent_map_trunc(inode
, 0);
5140 left_path
= ocfs2_new_path_from_et(et
);
5147 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
5152 el
= path_leaf_el(left_path
);
5154 index
= ocfs2_search_extent_list(el
, cpos
);
5155 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5156 ocfs2_error(inode
->i_sb
,
5157 "Inode %llu has an extent at cpos %u which can no "
5158 "longer be found.\n",
5159 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
5164 memset(&split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
5165 split_rec
.e_cpos
= cpu_to_le32(cpos
);
5166 split_rec
.e_leaf_clusters
= cpu_to_le16(len
);
5167 split_rec
.e_blkno
= cpu_to_le64(start_blkno
);
5168 split_rec
.e_flags
= path_leaf_el(left_path
)->l_recs
[index
].e_flags
;
5169 split_rec
.e_flags
&= ~OCFS2_EXT_UNWRITTEN
;
5171 ret
= __ocfs2_mark_extent_written(inode
, et
, handle
, left_path
,
5172 index
, &split_rec
, meta_ac
,
5178 ocfs2_free_path(left_path
);
5182 static int ocfs2_split_tree(struct inode
*inode
, struct ocfs2_extent_tree
*et
,
5183 handle_t
*handle
, struct ocfs2_path
*path
,
5184 int index
, u32 new_range
,
5185 struct ocfs2_alloc_context
*meta_ac
)
5187 int ret
, depth
, credits
= handle
->h_buffer_credits
;
5188 struct buffer_head
*last_eb_bh
= NULL
;
5189 struct ocfs2_extent_block
*eb
;
5190 struct ocfs2_extent_list
*rightmost_el
, *el
;
5191 struct ocfs2_extent_rec split_rec
;
5192 struct ocfs2_extent_rec
*rec
;
5193 struct ocfs2_insert_type insert
;
5196 * Setup the record to split before we grow the tree.
5198 el
= path_leaf_el(path
);
5199 rec
= &el
->l_recs
[index
];
5200 ocfs2_make_right_split_rec(inode
->i_sb
, &split_rec
, new_range
, rec
);
5202 depth
= path
->p_tree_depth
;
5204 ret
= ocfs2_read_extent_block(inode
,
5205 ocfs2_et_get_last_eb_blk(et
),
5212 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
5213 rightmost_el
= &eb
->h_list
;
5215 rightmost_el
= path_leaf_el(path
);
5217 credits
+= path
->p_tree_depth
+
5218 ocfs2_extend_meta_needed(et
->et_root_el
);
5219 ret
= ocfs2_extend_trans(handle
, credits
);
5225 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
5226 le16_to_cpu(rightmost_el
->l_count
)) {
5227 ret
= ocfs2_grow_tree(inode
, handle
, et
, &depth
, &last_eb_bh
,
5235 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
5236 insert
.ins_appending
= APPEND_NONE
;
5237 insert
.ins_contig
= CONTIG_NONE
;
5238 insert
.ins_split
= SPLIT_RIGHT
;
5239 insert
.ins_tree_depth
= depth
;
5241 ret
= ocfs2_do_insert_extent(inode
, handle
, et
, &split_rec
, &insert
);
5250 static int ocfs2_truncate_rec(struct inode
*inode
, handle_t
*handle
,
5251 struct ocfs2_path
*path
, int index
,
5252 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5254 struct ocfs2_extent_tree
*et
)
5257 u32 left_cpos
, rec_range
, trunc_range
;
5258 int wants_rotate
= 0, is_rightmost_tree_rec
= 0;
5259 struct super_block
*sb
= inode
->i_sb
;
5260 struct ocfs2_path
*left_path
= NULL
;
5261 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
5262 struct ocfs2_extent_rec
*rec
;
5263 struct ocfs2_extent_block
*eb
;
5265 if (ocfs2_is_empty_extent(&el
->l_recs
[0]) && index
> 0) {
5266 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
, et
);
5275 if (index
== (le16_to_cpu(el
->l_next_free_rec
) - 1) &&
5276 path
->p_tree_depth
) {
5278 * Check whether this is the rightmost tree record. If
5279 * we remove all of this record or part of its right
5280 * edge then an update of the record lengths above it
5283 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
5284 if (eb
->h_next_leaf_blk
== 0)
5285 is_rightmost_tree_rec
= 1;
5288 rec
= &el
->l_recs
[index
];
5289 if (index
== 0 && path
->p_tree_depth
&&
5290 le32_to_cpu(rec
->e_cpos
) == cpos
) {
5292 * Changing the leftmost offset (via partial or whole
5293 * record truncate) of an interior (or rightmost) path
5294 * means we have to update the subtree that is formed
5295 * by this leaf and the one to it's left.
5297 * There are two cases we can skip:
5298 * 1) Path is the leftmost one in our inode tree.
5299 * 2) The leaf is rightmost and will be empty after
5300 * we remove the extent record - the rotate code
5301 * knows how to update the newly formed edge.
5304 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
,
5311 if (left_cpos
&& le16_to_cpu(el
->l_next_free_rec
) > 1) {
5312 left_path
= ocfs2_new_path_from_path(path
);
5319 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
5327 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
5328 handle
->h_buffer_credits
,
5335 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
5341 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
5347 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5348 trunc_range
= cpos
+ len
;
5350 if (le32_to_cpu(rec
->e_cpos
) == cpos
&& rec_range
== trunc_range
) {
5353 memset(rec
, 0, sizeof(*rec
));
5354 ocfs2_cleanup_merge(el
, index
);
5357 next_free
= le16_to_cpu(el
->l_next_free_rec
);
5358 if (is_rightmost_tree_rec
&& next_free
> 1) {
5360 * We skip the edge update if this path will
5361 * be deleted by the rotate code.
5363 rec
= &el
->l_recs
[next_free
- 1];
5364 ocfs2_adjust_rightmost_records(inode
, handle
, path
,
5367 } else if (le32_to_cpu(rec
->e_cpos
) == cpos
) {
5368 /* Remove leftmost portion of the record. */
5369 le32_add_cpu(&rec
->e_cpos
, len
);
5370 le64_add_cpu(&rec
->e_blkno
, ocfs2_clusters_to_blocks(sb
, len
));
5371 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5372 } else if (rec_range
== trunc_range
) {
5373 /* Remove rightmost portion of the record */
5374 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5375 if (is_rightmost_tree_rec
)
5376 ocfs2_adjust_rightmost_records(inode
, handle
, path
, rec
);
5378 /* Caller should have trapped this. */
5379 mlog(ML_ERROR
, "Inode %llu: Invalid record truncate: (%u, %u) "
5380 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5381 le32_to_cpu(rec
->e_cpos
),
5382 le16_to_cpu(rec
->e_leaf_clusters
), cpos
, len
);
5389 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
5390 ocfs2_complete_edge_insert(inode
, handle
, left_path
, path
,
5394 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
5396 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
, et
);
5403 ocfs2_free_path(left_path
);
5407 int ocfs2_remove_extent(struct inode
*inode
,
5408 struct ocfs2_extent_tree
*et
,
5409 u32 cpos
, u32 len
, handle_t
*handle
,
5410 struct ocfs2_alloc_context
*meta_ac
,
5411 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5414 u32 rec_range
, trunc_range
;
5415 struct ocfs2_extent_rec
*rec
;
5416 struct ocfs2_extent_list
*el
;
5417 struct ocfs2_path
*path
= NULL
;
5419 ocfs2_extent_map_trunc(inode
, 0);
5421 path
= ocfs2_new_path_from_et(et
);
5428 ret
= ocfs2_find_path(inode
, path
, cpos
);
5434 el
= path_leaf_el(path
);
5435 index
= ocfs2_search_extent_list(el
, cpos
);
5436 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5437 ocfs2_error(inode
->i_sb
,
5438 "Inode %llu has an extent at cpos %u which can no "
5439 "longer be found.\n",
5440 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
5446 * We have 3 cases of extent removal:
5447 * 1) Range covers the entire extent rec
5448 * 2) Range begins or ends on one edge of the extent rec
5449 * 3) Range is in the middle of the extent rec (no shared edges)
5451 * For case 1 we remove the extent rec and left rotate to
5454 * For case 2 we just shrink the existing extent rec, with a
5455 * tree update if the shrinking edge is also the edge of an
5458 * For case 3 we do a right split to turn the extent rec into
5459 * something case 2 can handle.
5461 rec
= &el
->l_recs
[index
];
5462 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5463 trunc_range
= cpos
+ len
;
5465 BUG_ON(cpos
< le32_to_cpu(rec
->e_cpos
) || trunc_range
> rec_range
);
5467 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5468 "(cpos %u, len %u)\n",
5469 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
, len
, index
,
5470 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
));
5472 if (le32_to_cpu(rec
->e_cpos
) == cpos
|| rec_range
== trunc_range
) {
5473 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
5480 ret
= ocfs2_split_tree(inode
, et
, handle
, path
, index
,
5481 trunc_range
, meta_ac
);
5488 * The split could have manipulated the tree enough to
5489 * move the record location, so we have to look for it again.
5491 ocfs2_reinit_path(path
, 1);
5493 ret
= ocfs2_find_path(inode
, path
, cpos
);
5499 el
= path_leaf_el(path
);
5500 index
= ocfs2_search_extent_list(el
, cpos
);
5501 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5502 ocfs2_error(inode
->i_sb
,
5503 "Inode %llu: split at cpos %u lost record.",
5504 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5511 * Double check our values here. If anything is fishy,
5512 * it's easier to catch it at the top level.
5514 rec
= &el
->l_recs
[index
];
5515 rec_range
= le32_to_cpu(rec
->e_cpos
) +
5516 ocfs2_rec_clusters(el
, rec
);
5517 if (rec_range
!= trunc_range
) {
5518 ocfs2_error(inode
->i_sb
,
5519 "Inode %llu: error after split at cpos %u"
5520 "trunc len %u, existing record is (%u,%u)",
5521 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5522 cpos
, len
, le32_to_cpu(rec
->e_cpos
),
5523 ocfs2_rec_clusters(el
, rec
));
5528 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
5537 ocfs2_free_path(path
);
5541 int ocfs2_remove_btree_range(struct inode
*inode
,
5542 struct ocfs2_extent_tree
*et
,
5543 u32 cpos
, u32 phys_cpos
, u32 len
,
5544 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5547 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
5548 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5549 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5551 struct ocfs2_alloc_context
*meta_ac
= NULL
;
5553 ret
= ocfs2_lock_allocators(inode
, et
, 0, 1, NULL
, &meta_ac
);
5559 mutex_lock(&tl_inode
->i_mutex
);
5561 if (ocfs2_truncate_log_needs_flush(osb
)) {
5562 ret
= __ocfs2_flush_truncate_log(osb
);
5569 handle
= ocfs2_start_trans(osb
, ocfs2_remove_extent_credits(osb
->sb
));
5570 if (IS_ERR(handle
)) {
5571 ret
= PTR_ERR(handle
);
5576 ret
= ocfs2_et_root_journal_access(handle
, inode
, et
,
5577 OCFS2_JOURNAL_ACCESS_WRITE
);
5583 vfs_dq_free_space_nodirty(inode
,
5584 ocfs2_clusters_to_bytes(inode
->i_sb
, len
));
5586 ret
= ocfs2_remove_extent(inode
, et
, cpos
, len
, handle
, meta_ac
,
5593 ocfs2_et_update_clusters(inode
, et
, -len
);
5595 ret
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
5601 ret
= ocfs2_truncate_log_append(osb
, handle
, phys_blkno
, len
);
5606 ocfs2_commit_trans(osb
, handle
);
5608 mutex_unlock(&tl_inode
->i_mutex
);
5611 ocfs2_free_alloc_context(meta_ac
);
5616 int ocfs2_truncate_log_needs_flush(struct ocfs2_super
*osb
)
5618 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5619 struct ocfs2_dinode
*di
;
5620 struct ocfs2_truncate_log
*tl
;
5622 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5623 tl
= &di
->id2
.i_dealloc
;
5625 mlog_bug_on_msg(le16_to_cpu(tl
->tl_used
) > le16_to_cpu(tl
->tl_count
),
5626 "slot %d, invalid truncate log parameters: used = "
5627 "%u, count = %u\n", osb
->slot_num
,
5628 le16_to_cpu(tl
->tl_used
), le16_to_cpu(tl
->tl_count
));
5629 return le16_to_cpu(tl
->tl_used
) == le16_to_cpu(tl
->tl_count
);
5632 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log
*tl
,
5633 unsigned int new_start
)
5635 unsigned int tail_index
;
5636 unsigned int current_tail
;
5638 /* No records, nothing to coalesce */
5639 if (!le16_to_cpu(tl
->tl_used
))
5642 tail_index
= le16_to_cpu(tl
->tl_used
) - 1;
5643 current_tail
= le32_to_cpu(tl
->tl_recs
[tail_index
].t_start
);
5644 current_tail
+= le32_to_cpu(tl
->tl_recs
[tail_index
].t_clusters
);
5646 return current_tail
== new_start
;
5649 int ocfs2_truncate_log_append(struct ocfs2_super
*osb
,
5652 unsigned int num_clusters
)
5655 unsigned int start_cluster
, tl_count
;
5656 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5657 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5658 struct ocfs2_dinode
*di
;
5659 struct ocfs2_truncate_log
*tl
;
5661 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5662 (unsigned long long)start_blk
, num_clusters
);
5664 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5666 start_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, start_blk
);
5668 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5670 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5671 * by the underlying call to ocfs2_read_inode_block(), so any
5672 * corruption is a code bug */
5673 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5675 tl
= &di
->id2
.i_dealloc
;
5676 tl_count
= le16_to_cpu(tl
->tl_count
);
5677 mlog_bug_on_msg(tl_count
> ocfs2_truncate_recs_per_inode(osb
->sb
) ||
5679 "Truncate record count on #%llu invalid "
5680 "wanted %u, actual %u\n",
5681 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
,
5682 ocfs2_truncate_recs_per_inode(osb
->sb
),
5683 le16_to_cpu(tl
->tl_count
));
5685 /* Caller should have known to flush before calling us. */
5686 index
= le16_to_cpu(tl
->tl_used
);
5687 if (index
>= tl_count
) {
5693 status
= ocfs2_journal_access_di(handle
, tl_inode
, tl_bh
,
5694 OCFS2_JOURNAL_ACCESS_WRITE
);
5700 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5701 "%llu (index = %d)\n", num_clusters
, start_cluster
,
5702 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
, index
);
5704 if (ocfs2_truncate_log_can_coalesce(tl
, start_cluster
)) {
5706 * Move index back to the record we are coalescing with.
5707 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5711 num_clusters
+= le32_to_cpu(tl
->tl_recs
[index
].t_clusters
);
5712 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5713 index
, le32_to_cpu(tl
->tl_recs
[index
].t_start
),
5716 tl
->tl_recs
[index
].t_start
= cpu_to_le32(start_cluster
);
5717 tl
->tl_used
= cpu_to_le16(index
+ 1);
5719 tl
->tl_recs
[index
].t_clusters
= cpu_to_le32(num_clusters
);
5721 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5732 static int ocfs2_replay_truncate_records(struct ocfs2_super
*osb
,
5734 struct inode
*data_alloc_inode
,
5735 struct buffer_head
*data_alloc_bh
)
5739 unsigned int num_clusters
;
5741 struct ocfs2_truncate_rec rec
;
5742 struct ocfs2_dinode
*di
;
5743 struct ocfs2_truncate_log
*tl
;
5744 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5745 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5749 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5750 tl
= &di
->id2
.i_dealloc
;
5751 i
= le16_to_cpu(tl
->tl_used
) - 1;
5753 /* Caller has given us at least enough credits to
5754 * update the truncate log dinode */
5755 status
= ocfs2_journal_access_di(handle
, tl_inode
, tl_bh
,
5756 OCFS2_JOURNAL_ACCESS_WRITE
);
5762 tl
->tl_used
= cpu_to_le16(i
);
5764 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5770 /* TODO: Perhaps we can calculate the bulk of the
5771 * credits up front rather than extending like
5773 status
= ocfs2_extend_trans(handle
,
5774 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC
);
5780 rec
= tl
->tl_recs
[i
];
5781 start_blk
= ocfs2_clusters_to_blocks(data_alloc_inode
->i_sb
,
5782 le32_to_cpu(rec
.t_start
));
5783 num_clusters
= le32_to_cpu(rec
.t_clusters
);
5785 /* if start_blk is not set, we ignore the record as
5788 mlog(0, "free record %d, start = %u, clusters = %u\n",
5789 i
, le32_to_cpu(rec
.t_start
), num_clusters
);
5791 status
= ocfs2_free_clusters(handle
, data_alloc_inode
,
5792 data_alloc_bh
, start_blk
,
5807 /* Expects you to already be holding tl_inode->i_mutex */
5808 int __ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5811 unsigned int num_to_flush
;
5813 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5814 struct inode
*data_alloc_inode
= NULL
;
5815 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5816 struct buffer_head
*data_alloc_bh
= NULL
;
5817 struct ocfs2_dinode
*di
;
5818 struct ocfs2_truncate_log
*tl
;
5822 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5824 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5826 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5827 * by the underlying call to ocfs2_read_inode_block(), so any
5828 * corruption is a code bug */
5829 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5831 tl
= &di
->id2
.i_dealloc
;
5832 num_to_flush
= le16_to_cpu(tl
->tl_used
);
5833 mlog(0, "Flush %u records from truncate log #%llu\n",
5834 num_to_flush
, (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
);
5835 if (!num_to_flush
) {
5840 data_alloc_inode
= ocfs2_get_system_file_inode(osb
,
5841 GLOBAL_BITMAP_SYSTEM_INODE
,
5842 OCFS2_INVALID_SLOT
);
5843 if (!data_alloc_inode
) {
5845 mlog(ML_ERROR
, "Could not get bitmap inode!\n");
5849 mutex_lock(&data_alloc_inode
->i_mutex
);
5851 status
= ocfs2_inode_lock(data_alloc_inode
, &data_alloc_bh
, 1);
5857 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5858 if (IS_ERR(handle
)) {
5859 status
= PTR_ERR(handle
);
5864 status
= ocfs2_replay_truncate_records(osb
, handle
, data_alloc_inode
,
5869 ocfs2_commit_trans(osb
, handle
);
5872 brelse(data_alloc_bh
);
5873 ocfs2_inode_unlock(data_alloc_inode
, 1);
5876 mutex_unlock(&data_alloc_inode
->i_mutex
);
5877 iput(data_alloc_inode
);
5884 int ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5887 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5889 mutex_lock(&tl_inode
->i_mutex
);
5890 status
= __ocfs2_flush_truncate_log(osb
);
5891 mutex_unlock(&tl_inode
->i_mutex
);
5896 static void ocfs2_truncate_log_worker(struct work_struct
*work
)
5899 struct ocfs2_super
*osb
=
5900 container_of(work
, struct ocfs2_super
,
5901 osb_truncate_log_wq
.work
);
5905 status
= ocfs2_flush_truncate_log(osb
);
5909 ocfs2_init_inode_steal_slot(osb
);
5914 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5915 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super
*osb
,
5918 if (osb
->osb_tl_inode
) {
5919 /* We want to push off log flushes while truncates are
5922 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5924 queue_delayed_work(ocfs2_wq
, &osb
->osb_truncate_log_wq
,
5925 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL
);
5929 static int ocfs2_get_truncate_log_info(struct ocfs2_super
*osb
,
5931 struct inode
**tl_inode
,
5932 struct buffer_head
**tl_bh
)
5935 struct inode
*inode
= NULL
;
5936 struct buffer_head
*bh
= NULL
;
5938 inode
= ocfs2_get_system_file_inode(osb
,
5939 TRUNCATE_LOG_SYSTEM_INODE
,
5943 mlog(ML_ERROR
, "Could not get load truncate log inode!\n");
5947 status
= ocfs2_read_inode_block(inode
, &bh
);
5961 /* called during the 1st stage of node recovery. we stamp a clean
5962 * truncate log and pass back a copy for processing later. if the
5963 * truncate log does not require processing, a *tl_copy is set to
5965 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super
*osb
,
5967 struct ocfs2_dinode
**tl_copy
)
5970 struct inode
*tl_inode
= NULL
;
5971 struct buffer_head
*tl_bh
= NULL
;
5972 struct ocfs2_dinode
*di
;
5973 struct ocfs2_truncate_log
*tl
;
5977 mlog(0, "recover truncate log from slot %d\n", slot_num
);
5979 status
= ocfs2_get_truncate_log_info(osb
, slot_num
, &tl_inode
, &tl_bh
);
5985 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5987 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5988 * validated by the underlying call to ocfs2_read_inode_block(),
5989 * so any corruption is a code bug */
5990 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5992 tl
= &di
->id2
.i_dealloc
;
5993 if (le16_to_cpu(tl
->tl_used
)) {
5994 mlog(0, "We'll have %u logs to recover\n",
5995 le16_to_cpu(tl
->tl_used
));
5997 *tl_copy
= kmalloc(tl_bh
->b_size
, GFP_KERNEL
);
6004 /* Assuming the write-out below goes well, this copy
6005 * will be passed back to recovery for processing. */
6006 memcpy(*tl_copy
, tl_bh
->b_data
, tl_bh
->b_size
);
6008 /* All we need to do to clear the truncate log is set
6012 ocfs2_compute_meta_ecc(osb
->sb
, tl_bh
->b_data
, &di
->i_check
);
6013 status
= ocfs2_write_block(osb
, tl_bh
, tl_inode
);
6025 if (status
< 0 && (*tl_copy
)) {
6034 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super
*osb
,
6035 struct ocfs2_dinode
*tl_copy
)
6039 unsigned int clusters
, num_recs
, start_cluster
;
6042 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6043 struct ocfs2_truncate_log
*tl
;
6047 if (OCFS2_I(tl_inode
)->ip_blkno
== le64_to_cpu(tl_copy
->i_blkno
)) {
6048 mlog(ML_ERROR
, "Asked to recover my own truncate log!\n");
6052 tl
= &tl_copy
->id2
.i_dealloc
;
6053 num_recs
= le16_to_cpu(tl
->tl_used
);
6054 mlog(0, "cleanup %u records from %llu\n", num_recs
,
6055 (unsigned long long)le64_to_cpu(tl_copy
->i_blkno
));
6057 mutex_lock(&tl_inode
->i_mutex
);
6058 for(i
= 0; i
< num_recs
; i
++) {
6059 if (ocfs2_truncate_log_needs_flush(osb
)) {
6060 status
= __ocfs2_flush_truncate_log(osb
);
6067 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
6068 if (IS_ERR(handle
)) {
6069 status
= PTR_ERR(handle
);
6074 clusters
= le32_to_cpu(tl
->tl_recs
[i
].t_clusters
);
6075 start_cluster
= le32_to_cpu(tl
->tl_recs
[i
].t_start
);
6076 start_blk
= ocfs2_clusters_to_blocks(osb
->sb
, start_cluster
);
6078 status
= ocfs2_truncate_log_append(osb
, handle
,
6079 start_blk
, clusters
);
6080 ocfs2_commit_trans(osb
, handle
);
6088 mutex_unlock(&tl_inode
->i_mutex
);
6094 void ocfs2_truncate_log_shutdown(struct ocfs2_super
*osb
)
6097 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6102 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
6103 flush_workqueue(ocfs2_wq
);
6105 status
= ocfs2_flush_truncate_log(osb
);
6109 brelse(osb
->osb_tl_bh
);
6110 iput(osb
->osb_tl_inode
);
6116 int ocfs2_truncate_log_init(struct ocfs2_super
*osb
)
6119 struct inode
*tl_inode
= NULL
;
6120 struct buffer_head
*tl_bh
= NULL
;
6124 status
= ocfs2_get_truncate_log_info(osb
,
6131 /* ocfs2_truncate_log_shutdown keys on the existence of
6132 * osb->osb_tl_inode so we don't set any of the osb variables
6133 * until we're sure all is well. */
6134 INIT_DELAYED_WORK(&osb
->osb_truncate_log_wq
,
6135 ocfs2_truncate_log_worker
);
6136 osb
->osb_tl_bh
= tl_bh
;
6137 osb
->osb_tl_inode
= tl_inode
;
6144 * Delayed de-allocation of suballocator blocks.
6146 * Some sets of block de-allocations might involve multiple suballocator inodes.
6148 * The locking for this can get extremely complicated, especially when
6149 * the suballocator inodes to delete from aren't known until deep
6150 * within an unrelated codepath.
6152 * ocfs2_extent_block structures are a good example of this - an inode
6153 * btree could have been grown by any number of nodes each allocating
6154 * out of their own suballoc inode.
6156 * These structures allow the delay of block de-allocation until a
6157 * later time, when locking of multiple cluster inodes won't cause
6162 * Describe a single bit freed from a suballocator. For the block
6163 * suballocators, it represents one block. For the global cluster
6164 * allocator, it represents some clusters and free_bit indicates
6167 struct ocfs2_cached_block_free
{
6168 struct ocfs2_cached_block_free
*free_next
;
6170 unsigned int free_bit
;
6173 struct ocfs2_per_slot_free_list
{
6174 struct ocfs2_per_slot_free_list
*f_next_suballocator
;
6177 struct ocfs2_cached_block_free
*f_first
;
6180 static int ocfs2_free_cached_blocks(struct ocfs2_super
*osb
,
6183 struct ocfs2_cached_block_free
*head
)
6188 struct inode
*inode
;
6189 struct buffer_head
*di_bh
= NULL
;
6190 struct ocfs2_cached_block_free
*tmp
;
6192 inode
= ocfs2_get_system_file_inode(osb
, sysfile_type
, slot
);
6199 mutex_lock(&inode
->i_mutex
);
6201 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
6207 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
6208 if (IS_ERR(handle
)) {
6209 ret
= PTR_ERR(handle
);
6215 bg_blkno
= ocfs2_which_suballoc_group(head
->free_blk
,
6217 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6218 head
->free_bit
, (unsigned long long)head
->free_blk
);
6220 ret
= ocfs2_free_suballoc_bits(handle
, inode
, di_bh
,
6221 head
->free_bit
, bg_blkno
, 1);
6227 ret
= ocfs2_extend_trans(handle
, OCFS2_SUBALLOC_FREE
);
6234 head
= head
->free_next
;
6239 ocfs2_commit_trans(osb
, handle
);
6242 ocfs2_inode_unlock(inode
, 1);
6245 mutex_unlock(&inode
->i_mutex
);
6249 /* Premature exit may have left some dangling items. */
6251 head
= head
->free_next
;
6258 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6259 u64 blkno
, unsigned int bit
)
6262 struct ocfs2_cached_block_free
*item
;
6264 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6271 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6272 bit
, (unsigned long long)blkno
);
6274 item
->free_blk
= blkno
;
6275 item
->free_bit
= bit
;
6276 item
->free_next
= ctxt
->c_global_allocator
;
6278 ctxt
->c_global_allocator
= item
;
6282 static int ocfs2_free_cached_clusters(struct ocfs2_super
*osb
,
6283 struct ocfs2_cached_block_free
*head
)
6285 struct ocfs2_cached_block_free
*tmp
;
6286 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6290 mutex_lock(&tl_inode
->i_mutex
);
6293 if (ocfs2_truncate_log_needs_flush(osb
)) {
6294 ret
= __ocfs2_flush_truncate_log(osb
);
6301 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
6302 if (IS_ERR(handle
)) {
6303 ret
= PTR_ERR(handle
);
6308 ret
= ocfs2_truncate_log_append(osb
, handle
, head
->free_blk
,
6311 ocfs2_commit_trans(osb
, handle
);
6313 head
= head
->free_next
;
6322 mutex_unlock(&tl_inode
->i_mutex
);
6325 /* Premature exit may have left some dangling items. */
6327 head
= head
->free_next
;
6334 int ocfs2_run_deallocs(struct ocfs2_super
*osb
,
6335 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
6338 struct ocfs2_per_slot_free_list
*fl
;
6343 while (ctxt
->c_first_suballocator
) {
6344 fl
= ctxt
->c_first_suballocator
;
6347 mlog(0, "Free items: (type %u, slot %d)\n",
6348 fl
->f_inode_type
, fl
->f_slot
);
6349 ret2
= ocfs2_free_cached_blocks(osb
,
6359 ctxt
->c_first_suballocator
= fl
->f_next_suballocator
;
6363 if (ctxt
->c_global_allocator
) {
6364 ret2
= ocfs2_free_cached_clusters(osb
,
6365 ctxt
->c_global_allocator
);
6371 ctxt
->c_global_allocator
= NULL
;
6377 static struct ocfs2_per_slot_free_list
*
6378 ocfs2_find_per_slot_free_list(int type
,
6380 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
6382 struct ocfs2_per_slot_free_list
*fl
= ctxt
->c_first_suballocator
;
6385 if (fl
->f_inode_type
== type
&& fl
->f_slot
== slot
)
6388 fl
= fl
->f_next_suballocator
;
6391 fl
= kmalloc(sizeof(*fl
), GFP_NOFS
);
6393 fl
->f_inode_type
= type
;
6396 fl
->f_next_suballocator
= ctxt
->c_first_suballocator
;
6398 ctxt
->c_first_suballocator
= fl
;
6403 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6404 int type
, int slot
, u64 blkno
,
6408 struct ocfs2_per_slot_free_list
*fl
;
6409 struct ocfs2_cached_block_free
*item
;
6411 fl
= ocfs2_find_per_slot_free_list(type
, slot
, ctxt
);
6418 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6425 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6426 type
, slot
, bit
, (unsigned long long)blkno
);
6428 item
->free_blk
= blkno
;
6429 item
->free_bit
= bit
;
6430 item
->free_next
= fl
->f_first
;
6439 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6440 struct ocfs2_extent_block
*eb
)
6442 return ocfs2_cache_block_dealloc(ctxt
, EXTENT_ALLOC_SYSTEM_INODE
,
6443 le16_to_cpu(eb
->h_suballoc_slot
),
6444 le64_to_cpu(eb
->h_blkno
),
6445 le16_to_cpu(eb
->h_suballoc_bit
));
6448 /* This function will figure out whether the currently last extent
6449 * block will be deleted, and if it will, what the new last extent
6450 * block will be so we can update his h_next_leaf_blk field, as well
6451 * as the dinodes i_last_eb_blk */
6452 static int ocfs2_find_new_last_ext_blk(struct inode
*inode
,
6453 unsigned int clusters_to_del
,
6454 struct ocfs2_path
*path
,
6455 struct buffer_head
**new_last_eb
)
6457 int next_free
, ret
= 0;
6459 struct ocfs2_extent_rec
*rec
;
6460 struct ocfs2_extent_block
*eb
;
6461 struct ocfs2_extent_list
*el
;
6462 struct buffer_head
*bh
= NULL
;
6464 *new_last_eb
= NULL
;
6466 /* we have no tree, so of course, no last_eb. */
6467 if (!path
->p_tree_depth
)
6470 /* trunc to zero special case - this makes tree_depth = 0
6471 * regardless of what it is. */
6472 if (OCFS2_I(inode
)->ip_clusters
== clusters_to_del
)
6475 el
= path_leaf_el(path
);
6476 BUG_ON(!el
->l_next_free_rec
);
6479 * Make sure that this extent list will actually be empty
6480 * after we clear away the data. We can shortcut out if
6481 * there's more than one non-empty extent in the
6482 * list. Otherwise, a check of the remaining extent is
6485 next_free
= le16_to_cpu(el
->l_next_free_rec
);
6487 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6491 /* We may have a valid extent in index 1, check it. */
6493 rec
= &el
->l_recs
[1];
6496 * Fall through - no more nonempty extents, so we want
6497 * to delete this leaf.
6503 rec
= &el
->l_recs
[0];
6508 * Check it we'll only be trimming off the end of this
6511 if (le16_to_cpu(rec
->e_leaf_clusters
) > clusters_to_del
)
6515 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
6521 ret
= ocfs2_find_leaf(inode
, path_root_el(path
), cpos
, &bh
);
6527 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
6530 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6531 * Any corruption is a code bug. */
6532 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
6535 get_bh(*new_last_eb
);
6536 mlog(0, "returning block %llu, (cpos: %u)\n",
6537 (unsigned long long)le64_to_cpu(eb
->h_blkno
), cpos
);
6545 * Trim some clusters off the rightmost edge of a tree. Only called
6548 * The caller needs to:
6549 * - start journaling of each path component.
6550 * - compute and fully set up any new last ext block
6552 static int ocfs2_trim_tree(struct inode
*inode
, struct ocfs2_path
*path
,
6553 handle_t
*handle
, struct ocfs2_truncate_context
*tc
,
6554 u32 clusters_to_del
, u64
*delete_start
)
6556 int ret
, i
, index
= path
->p_tree_depth
;
6559 struct buffer_head
*bh
;
6560 struct ocfs2_extent_list
*el
;
6561 struct ocfs2_extent_rec
*rec
;
6565 while (index
>= 0) {
6566 bh
= path
->p_node
[index
].bh
;
6567 el
= path
->p_node
[index
].el
;
6569 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6570 index
, (unsigned long long)bh
->b_blocknr
);
6572 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
6575 (path
->p_tree_depth
- le16_to_cpu(el
->l_tree_depth
))) {
6576 ocfs2_error(inode
->i_sb
,
6577 "Inode %lu has invalid ext. block %llu",
6579 (unsigned long long)bh
->b_blocknr
);
6585 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
6586 rec
= &el
->l_recs
[i
];
6588 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6589 "next = %u\n", i
, le32_to_cpu(rec
->e_cpos
),
6590 ocfs2_rec_clusters(el
, rec
),
6591 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6592 le16_to_cpu(el
->l_next_free_rec
));
6594 BUG_ON(ocfs2_rec_clusters(el
, rec
) < clusters_to_del
);
6596 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
6598 * If the leaf block contains a single empty
6599 * extent and no records, we can just remove
6602 if (i
== 0 && ocfs2_is_empty_extent(rec
)) {
6604 sizeof(struct ocfs2_extent_rec
));
6605 el
->l_next_free_rec
= cpu_to_le16(0);
6611 * Remove any empty extents by shifting things
6612 * left. That should make life much easier on
6613 * the code below. This condition is rare
6614 * enough that we shouldn't see a performance
6617 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6618 le16_add_cpu(&el
->l_next_free_rec
, -1);
6621 i
< le16_to_cpu(el
->l_next_free_rec
); i
++)
6622 el
->l_recs
[i
] = el
->l_recs
[i
+ 1];
6624 memset(&el
->l_recs
[i
], 0,
6625 sizeof(struct ocfs2_extent_rec
));
6628 * We've modified our extent list. The
6629 * simplest way to handle this change
6630 * is to being the search from the
6633 goto find_tail_record
;
6636 le16_add_cpu(&rec
->e_leaf_clusters
, -clusters_to_del
);
6639 * We'll use "new_edge" on our way back up the
6640 * tree to know what our rightmost cpos is.
6642 new_edge
= le16_to_cpu(rec
->e_leaf_clusters
);
6643 new_edge
+= le32_to_cpu(rec
->e_cpos
);
6646 * The caller will use this to delete data blocks.
6648 *delete_start
= le64_to_cpu(rec
->e_blkno
)
6649 + ocfs2_clusters_to_blocks(inode
->i_sb
,
6650 le16_to_cpu(rec
->e_leaf_clusters
));
6653 * If it's now empty, remove this record.
6655 if (le16_to_cpu(rec
->e_leaf_clusters
) == 0) {
6657 sizeof(struct ocfs2_extent_rec
));
6658 le16_add_cpu(&el
->l_next_free_rec
, -1);
6661 if (le64_to_cpu(rec
->e_blkno
) == deleted_eb
) {
6663 sizeof(struct ocfs2_extent_rec
));
6664 le16_add_cpu(&el
->l_next_free_rec
, -1);
6669 /* Can this actually happen? */
6670 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
6674 * We never actually deleted any clusters
6675 * because our leaf was empty. There's no
6676 * reason to adjust the rightmost edge then.
6681 rec
->e_int_clusters
= cpu_to_le32(new_edge
);
6682 le32_add_cpu(&rec
->e_int_clusters
,
6683 -le32_to_cpu(rec
->e_cpos
));
6686 * A deleted child record should have been
6689 BUG_ON(le32_to_cpu(rec
->e_int_clusters
) == 0);
6693 ret
= ocfs2_journal_dirty(handle
, bh
);
6699 mlog(0, "extent list container %llu, after: record %d: "
6700 "(%u, %u, %llu), next = %u.\n",
6701 (unsigned long long)bh
->b_blocknr
, i
,
6702 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
),
6703 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6704 le16_to_cpu(el
->l_next_free_rec
));
6707 * We must be careful to only attempt delete of an
6708 * extent block (and not the root inode block).
6710 if (index
> 0 && le16_to_cpu(el
->l_next_free_rec
) == 0) {
6711 struct ocfs2_extent_block
*eb
=
6712 (struct ocfs2_extent_block
*)bh
->b_data
;
6715 * Save this for use when processing the
6718 deleted_eb
= le64_to_cpu(eb
->h_blkno
);
6720 mlog(0, "deleting this extent block.\n");
6722 ocfs2_remove_from_cache(inode
, bh
);
6724 BUG_ON(ocfs2_rec_clusters(el
, &el
->l_recs
[0]));
6725 BUG_ON(le32_to_cpu(el
->l_recs
[0].e_cpos
));
6726 BUG_ON(le64_to_cpu(el
->l_recs
[0].e_blkno
));
6728 ret
= ocfs2_cache_extent_block_free(&tc
->tc_dealloc
, eb
);
6729 /* An error here is not fatal. */
6744 static int ocfs2_do_truncate(struct ocfs2_super
*osb
,
6745 unsigned int clusters_to_del
,
6746 struct inode
*inode
,
6747 struct buffer_head
*fe_bh
,
6749 struct ocfs2_truncate_context
*tc
,
6750 struct ocfs2_path
*path
)
6753 struct ocfs2_dinode
*fe
;
6754 struct ocfs2_extent_block
*last_eb
= NULL
;
6755 struct ocfs2_extent_list
*el
;
6756 struct buffer_head
*last_eb_bh
= NULL
;
6759 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
6761 status
= ocfs2_find_new_last_ext_blk(inode
, clusters_to_del
,
6769 * Each component will be touched, so we might as well journal
6770 * here to avoid having to handle errors later.
6772 status
= ocfs2_journal_access_path(inode
, handle
, path
);
6779 status
= ocfs2_journal_access_eb(handle
, inode
, last_eb_bh
,
6780 OCFS2_JOURNAL_ACCESS_WRITE
);
6786 last_eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
6789 el
= &(fe
->id2
.i_list
);
6792 * Lower levels depend on this never happening, but it's best
6793 * to check it up here before changing the tree.
6795 if (el
->l_tree_depth
&& el
->l_recs
[0].e_int_clusters
== 0) {
6796 ocfs2_error(inode
->i_sb
,
6797 "Inode %lu has an empty extent record, depth %u\n",
6798 inode
->i_ino
, le16_to_cpu(el
->l_tree_depth
));
6803 vfs_dq_free_space_nodirty(inode
,
6804 ocfs2_clusters_to_bytes(osb
->sb
, clusters_to_del
));
6805 spin_lock(&OCFS2_I(inode
)->ip_lock
);
6806 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
) -
6808 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
6809 le32_add_cpu(&fe
->i_clusters
, -clusters_to_del
);
6810 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
6812 status
= ocfs2_trim_tree(inode
, path
, handle
, tc
,
6813 clusters_to_del
, &delete_blk
);
6819 if (le32_to_cpu(fe
->i_clusters
) == 0) {
6820 /* trunc to zero is a special case. */
6821 el
->l_tree_depth
= 0;
6822 fe
->i_last_eb_blk
= 0;
6824 fe
->i_last_eb_blk
= last_eb
->h_blkno
;
6826 status
= ocfs2_journal_dirty(handle
, fe_bh
);
6833 /* If there will be a new last extent block, then by
6834 * definition, there cannot be any leaves to the right of
6836 last_eb
->h_next_leaf_blk
= 0;
6837 status
= ocfs2_journal_dirty(handle
, last_eb_bh
);
6845 status
= ocfs2_truncate_log_append(osb
, handle
, delete_blk
,
6859 static int ocfs2_zero_func(handle_t
*handle
, struct buffer_head
*bh
)
6861 set_buffer_uptodate(bh
);
6862 mark_buffer_dirty(bh
);
6866 static void ocfs2_map_and_dirty_page(struct inode
*inode
, handle_t
*handle
,
6867 unsigned int from
, unsigned int to
,
6868 struct page
*page
, int zero
, u64
*phys
)
6870 int ret
, partial
= 0;
6872 ret
= ocfs2_map_page_blocks(page
, phys
, inode
, from
, to
, 0);
6877 zero_user_segment(page
, from
, to
);
6880 * Need to set the buffers we zero'd into uptodate
6881 * here if they aren't - ocfs2_map_page_blocks()
6882 * might've skipped some
6884 ret
= walk_page_buffers(handle
, page_buffers(page
),
6889 else if (ocfs2_should_order_data(inode
)) {
6890 ret
= ocfs2_jbd2_file_inode(handle
, inode
);
6896 SetPageUptodate(page
);
6898 flush_dcache_page(page
);
6901 static void ocfs2_zero_cluster_pages(struct inode
*inode
, loff_t start
,
6902 loff_t end
, struct page
**pages
,
6903 int numpages
, u64 phys
, handle_t
*handle
)
6907 unsigned int from
, to
= PAGE_CACHE_SIZE
;
6908 struct super_block
*sb
= inode
->i_sb
;
6910 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb
)));
6915 to
= PAGE_CACHE_SIZE
;
6916 for(i
= 0; i
< numpages
; i
++) {
6919 from
= start
& (PAGE_CACHE_SIZE
- 1);
6920 if ((end
>> PAGE_CACHE_SHIFT
) == page
->index
)
6921 to
= end
& (PAGE_CACHE_SIZE
- 1);
6923 BUG_ON(from
> PAGE_CACHE_SIZE
);
6924 BUG_ON(to
> PAGE_CACHE_SIZE
);
6926 ocfs2_map_and_dirty_page(inode
, handle
, from
, to
, page
, 1,
6929 start
= (page
->index
+ 1) << PAGE_CACHE_SHIFT
;
6933 ocfs2_unlock_and_free_pages(pages
, numpages
);
6936 static int ocfs2_grab_eof_pages(struct inode
*inode
, loff_t start
, loff_t end
,
6937 struct page
**pages
, int *num
)
6939 int numpages
, ret
= 0;
6940 struct super_block
*sb
= inode
->i_sb
;
6941 struct address_space
*mapping
= inode
->i_mapping
;
6942 unsigned long index
;
6943 loff_t last_page_bytes
;
6945 BUG_ON(start
> end
);
6947 BUG_ON(start
>> OCFS2_SB(sb
)->s_clustersize_bits
!=
6948 (end
- 1) >> OCFS2_SB(sb
)->s_clustersize_bits
);
6951 last_page_bytes
= PAGE_ALIGN(end
);
6952 index
= start
>> PAGE_CACHE_SHIFT
;
6954 pages
[numpages
] = grab_cache_page(mapping
, index
);
6955 if (!pages
[numpages
]) {
6963 } while (index
< (last_page_bytes
>> PAGE_CACHE_SHIFT
));
6968 ocfs2_unlock_and_free_pages(pages
, numpages
);
6978 * Zero the area past i_size but still within an allocated
6979 * cluster. This avoids exposing nonzero data on subsequent file
6982 * We need to call this before i_size is updated on the inode because
6983 * otherwise block_write_full_page() will skip writeout of pages past
6984 * i_size. The new_i_size parameter is passed for this reason.
6986 int ocfs2_zero_range_for_truncate(struct inode
*inode
, handle_t
*handle
,
6987 u64 range_start
, u64 range_end
)
6989 int ret
= 0, numpages
;
6990 struct page
**pages
= NULL
;
6992 unsigned int ext_flags
;
6993 struct super_block
*sb
= inode
->i_sb
;
6996 * File systems which don't support sparse files zero on every
6999 if (!ocfs2_sparse_alloc(OCFS2_SB(sb
)))
7002 pages
= kcalloc(ocfs2_pages_per_cluster(sb
),
7003 sizeof(struct page
*), GFP_NOFS
);
7004 if (pages
== NULL
) {
7010 if (range_start
== range_end
)
7013 ret
= ocfs2_extent_map_get_blocks(inode
,
7014 range_start
>> sb
->s_blocksize_bits
,
7015 &phys
, NULL
, &ext_flags
);
7022 * Tail is a hole, or is marked unwritten. In either case, we
7023 * can count on read and write to return/push zero's.
7025 if (phys
== 0 || ext_flags
& OCFS2_EXT_UNWRITTEN
)
7028 ret
= ocfs2_grab_eof_pages(inode
, range_start
, range_end
, pages
,
7035 ocfs2_zero_cluster_pages(inode
, range_start
, range_end
, pages
,
7036 numpages
, phys
, handle
);
7039 * Initiate writeout of the pages we zero'd here. We don't
7040 * wait on them - the truncate_inode_pages() call later will
7043 ret
= do_sync_mapping_range(inode
->i_mapping
, range_start
,
7044 range_end
- 1, SYNC_FILE_RANGE_WRITE
);
7055 static void ocfs2_zero_dinode_id2_with_xattr(struct inode
*inode
,
7056 struct ocfs2_dinode
*di
)
7058 unsigned int blocksize
= 1 << inode
->i_sb
->s_blocksize_bits
;
7059 unsigned int xattrsize
= le16_to_cpu(di
->i_xattr_inline_size
);
7061 if (le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_XATTR_FL
)
7062 memset(&di
->id2
, 0, blocksize
-
7063 offsetof(struct ocfs2_dinode
, id2
) -
7066 memset(&di
->id2
, 0, blocksize
-
7067 offsetof(struct ocfs2_dinode
, id2
));
7070 void ocfs2_dinode_new_extent_list(struct inode
*inode
,
7071 struct ocfs2_dinode
*di
)
7073 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
7074 di
->id2
.i_list
.l_tree_depth
= 0;
7075 di
->id2
.i_list
.l_next_free_rec
= 0;
7076 di
->id2
.i_list
.l_count
= cpu_to_le16(
7077 ocfs2_extent_recs_per_inode_with_xattr(inode
->i_sb
, di
));
7080 void ocfs2_set_inode_data_inline(struct inode
*inode
, struct ocfs2_dinode
*di
)
7082 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
7083 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
7085 spin_lock(&oi
->ip_lock
);
7086 oi
->ip_dyn_features
|= OCFS2_INLINE_DATA_FL
;
7087 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
7088 spin_unlock(&oi
->ip_lock
);
7091 * We clear the entire i_data structure here so that all
7092 * fields can be properly initialized.
7094 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
7096 idata
->id_count
= cpu_to_le16(
7097 ocfs2_max_inline_data_with_xattr(inode
->i_sb
, di
));
7100 int ocfs2_convert_inline_data_to_extents(struct inode
*inode
,
7101 struct buffer_head
*di_bh
)
7103 int ret
, i
, has_data
, num_pages
= 0;
7105 u64
uninitialized_var(block
);
7106 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
7107 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
7108 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
7109 struct ocfs2_alloc_context
*data_ac
= NULL
;
7110 struct page
**pages
= NULL
;
7111 loff_t end
= osb
->s_clustersize
;
7112 struct ocfs2_extent_tree et
;
7115 has_data
= i_size_read(inode
) ? 1 : 0;
7118 pages
= kcalloc(ocfs2_pages_per_cluster(osb
->sb
),
7119 sizeof(struct page
*), GFP_NOFS
);
7120 if (pages
== NULL
) {
7126 ret
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
7133 handle
= ocfs2_start_trans(osb
,
7134 ocfs2_inline_to_extents_credits(osb
->sb
));
7135 if (IS_ERR(handle
)) {
7136 ret
= PTR_ERR(handle
);
7141 ret
= ocfs2_journal_access_di(handle
, inode
, di_bh
,
7142 OCFS2_JOURNAL_ACCESS_WRITE
);
7150 unsigned int page_end
;
7153 if (vfs_dq_alloc_space_nodirty(inode
,
7154 ocfs2_clusters_to_bytes(osb
->sb
, 1))) {
7160 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
,
7168 * Save two copies, one for insert, and one that can
7169 * be changed by ocfs2_map_and_dirty_page() below.
7171 block
= phys
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
7174 * Non sparse file systems zero on extend, so no need
7177 if (!ocfs2_sparse_alloc(osb
) &&
7178 PAGE_CACHE_SIZE
< osb
->s_clustersize
)
7179 end
= PAGE_CACHE_SIZE
;
7181 ret
= ocfs2_grab_eof_pages(inode
, 0, end
, pages
, &num_pages
);
7188 * This should populate the 1st page for us and mark
7191 ret
= ocfs2_read_inline_data(inode
, pages
[0], di_bh
);
7197 page_end
= PAGE_CACHE_SIZE
;
7198 if (PAGE_CACHE_SIZE
> osb
->s_clustersize
)
7199 page_end
= osb
->s_clustersize
;
7201 for (i
= 0; i
< num_pages
; i
++)
7202 ocfs2_map_and_dirty_page(inode
, handle
, 0, page_end
,
7203 pages
[i
], i
> 0, &phys
);
7206 spin_lock(&oi
->ip_lock
);
7207 oi
->ip_dyn_features
&= ~OCFS2_INLINE_DATA_FL
;
7208 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
7209 spin_unlock(&oi
->ip_lock
);
7211 ocfs2_dinode_new_extent_list(inode
, di
);
7213 ocfs2_journal_dirty(handle
, di_bh
);
7217 * An error at this point should be extremely rare. If
7218 * this proves to be false, we could always re-build
7219 * the in-inode data from our pages.
7221 ocfs2_init_dinode_extent_tree(&et
, inode
, di_bh
);
7222 ret
= ocfs2_insert_extent(osb
, handle
, inode
, &et
,
7223 0, block
, 1, 0, NULL
);
7229 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
7233 if (ret
< 0 && did_quota
)
7234 vfs_dq_free_space_nodirty(inode
,
7235 ocfs2_clusters_to_bytes(osb
->sb
, 1));
7237 ocfs2_commit_trans(osb
, handle
);
7241 ocfs2_free_alloc_context(data_ac
);
7245 ocfs2_unlock_and_free_pages(pages
, num_pages
);
7253 * It is expected, that by the time you call this function,
7254 * inode->i_size and fe->i_size have been adjusted.
7256 * WARNING: This will kfree the truncate context
7258 int ocfs2_commit_truncate(struct ocfs2_super
*osb
,
7259 struct inode
*inode
,
7260 struct buffer_head
*fe_bh
,
7261 struct ocfs2_truncate_context
*tc
)
7263 int status
, i
, credits
, tl_sem
= 0;
7264 u32 clusters_to_del
, new_highest_cpos
, range
;
7265 struct ocfs2_extent_list
*el
;
7266 handle_t
*handle
= NULL
;
7267 struct inode
*tl_inode
= osb
->osb_tl_inode
;
7268 struct ocfs2_path
*path
= NULL
;
7269 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
7273 new_highest_cpos
= ocfs2_clusters_for_bytes(osb
->sb
,
7274 i_size_read(inode
));
7276 path
= ocfs2_new_path(fe_bh
, &di
->id2
.i_list
,
7277 ocfs2_journal_access_di
);
7284 ocfs2_extent_map_trunc(inode
, new_highest_cpos
);
7288 * Check that we still have allocation to delete.
7290 if (OCFS2_I(inode
)->ip_clusters
== 0) {
7296 * Truncate always works against the rightmost tree branch.
7298 status
= ocfs2_find_path(inode
, path
, UINT_MAX
);
7304 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7305 OCFS2_I(inode
)->ip_clusters
, path
->p_tree_depth
);
7308 * By now, el will point to the extent list on the bottom most
7309 * portion of this tree. Only the tail record is considered in
7312 * We handle the following cases, in order:
7313 * - empty extent: delete the remaining branch
7314 * - remove the entire record
7315 * - remove a partial record
7316 * - no record needs to be removed (truncate has completed)
7318 el
= path_leaf_el(path
);
7319 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
7320 ocfs2_error(inode
->i_sb
,
7321 "Inode %llu has empty extent block at %llu\n",
7322 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7323 (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
7328 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
7329 range
= le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
7330 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
7331 if (i
== 0 && ocfs2_is_empty_extent(&el
->l_recs
[i
])) {
7332 clusters_to_del
= 0;
7333 } else if (le32_to_cpu(el
->l_recs
[i
].e_cpos
) >= new_highest_cpos
) {
7334 clusters_to_del
= ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
7335 } else if (range
> new_highest_cpos
) {
7336 clusters_to_del
= (ocfs2_rec_clusters(el
, &el
->l_recs
[i
]) +
7337 le32_to_cpu(el
->l_recs
[i
].e_cpos
)) -
7344 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7345 clusters_to_del
, (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
7347 mutex_lock(&tl_inode
->i_mutex
);
7349 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7350 * record is free for use. If there isn't any, we flush to get
7351 * an empty truncate log. */
7352 if (ocfs2_truncate_log_needs_flush(osb
)) {
7353 status
= __ocfs2_flush_truncate_log(osb
);
7360 credits
= ocfs2_calc_tree_trunc_credits(osb
->sb
, clusters_to_del
,
7361 (struct ocfs2_dinode
*)fe_bh
->b_data
,
7363 handle
= ocfs2_start_trans(osb
, credits
);
7364 if (IS_ERR(handle
)) {
7365 status
= PTR_ERR(handle
);
7371 status
= ocfs2_do_truncate(osb
, clusters_to_del
, inode
, fe_bh
, handle
,
7378 mutex_unlock(&tl_inode
->i_mutex
);
7381 ocfs2_commit_trans(osb
, handle
);
7384 ocfs2_reinit_path(path
, 1);
7387 * The check above will catch the case where we've truncated
7388 * away all allocation.
7394 ocfs2_schedule_truncate_log_flush(osb
, 1);
7397 mutex_unlock(&tl_inode
->i_mutex
);
7400 ocfs2_commit_trans(osb
, handle
);
7402 ocfs2_run_deallocs(osb
, &tc
->tc_dealloc
);
7404 ocfs2_free_path(path
);
7406 /* This will drop the ext_alloc cluster lock for us */
7407 ocfs2_free_truncate_context(tc
);
7414 * Expects the inode to already be locked.
7416 int ocfs2_prepare_truncate(struct ocfs2_super
*osb
,
7417 struct inode
*inode
,
7418 struct buffer_head
*fe_bh
,
7419 struct ocfs2_truncate_context
**tc
)
7422 unsigned int new_i_clusters
;
7423 struct ocfs2_dinode
*fe
;
7424 struct ocfs2_extent_block
*eb
;
7425 struct buffer_head
*last_eb_bh
= NULL
;
7431 new_i_clusters
= ocfs2_clusters_for_bytes(osb
->sb
,
7432 i_size_read(inode
));
7433 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
7435 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7436 "%llu\n", le32_to_cpu(fe
->i_clusters
), new_i_clusters
,
7437 (unsigned long long)le64_to_cpu(fe
->i_size
));
7439 *tc
= kzalloc(sizeof(struct ocfs2_truncate_context
), GFP_KERNEL
);
7445 ocfs2_init_dealloc_ctxt(&(*tc
)->tc_dealloc
);
7447 if (fe
->id2
.i_list
.l_tree_depth
) {
7448 status
= ocfs2_read_extent_block(inode
,
7449 le64_to_cpu(fe
->i_last_eb_blk
),
7455 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
7458 (*tc
)->tc_last_eb_bh
= last_eb_bh
;
7464 ocfs2_free_truncate_context(*tc
);
7472 * 'start' is inclusive, 'end' is not.
7474 int ocfs2_truncate_inline(struct inode
*inode
, struct buffer_head
*di_bh
,
7475 unsigned int start
, unsigned int end
, int trunc
)
7478 unsigned int numbytes
;
7480 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
7481 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
7482 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
7484 if (end
> i_size_read(inode
))
7485 end
= i_size_read(inode
);
7487 BUG_ON(start
>= end
);
7489 if (!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) ||
7490 !(le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_DATA_FL
) ||
7491 !ocfs2_supports_inline_data(osb
)) {
7492 ocfs2_error(inode
->i_sb
,
7493 "Inline data flags for inode %llu don't agree! "
7494 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7495 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7496 le16_to_cpu(di
->i_dyn_features
),
7497 OCFS2_I(inode
)->ip_dyn_features
,
7498 osb
->s_feature_incompat
);
7503 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
7504 if (IS_ERR(handle
)) {
7505 ret
= PTR_ERR(handle
);
7510 ret
= ocfs2_journal_access_di(handle
, inode
, di_bh
,
7511 OCFS2_JOURNAL_ACCESS_WRITE
);
7517 numbytes
= end
- start
;
7518 memset(idata
->id_data
+ start
, 0, numbytes
);
7521 * No need to worry about the data page here - it's been
7522 * truncated already and inline data doesn't need it for
7523 * pushing zero's to disk, so we'll let readpage pick it up
7527 i_size_write(inode
, start
);
7528 di
->i_size
= cpu_to_le64(start
);
7531 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
7532 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
7534 di
->i_ctime
= di
->i_mtime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
7535 di
->i_ctime_nsec
= di
->i_mtime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
7537 ocfs2_journal_dirty(handle
, di_bh
);
7540 ocfs2_commit_trans(osb
, handle
);
7546 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
)
7549 * The caller is responsible for completing deallocation
7550 * before freeing the context.
7552 if (tc
->tc_dealloc
.c_first_suballocator
!= NULL
)
7554 "Truncate completion has non-empty dealloc context\n");
7556 brelse(tc
->tc_last_eb_bh
);