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"
52 #include "refcounttree.h"
54 #include "buffer_head_io.h"
56 enum ocfs2_contig_type {
63 static enum ocfs2_contig_type
64 ocfs2_extent_rec_contig(struct super_block *sb,
65 struct ocfs2_extent_rec *ext,
66 struct ocfs2_extent_rec *insert_rec);
68 * Operations for a specific extent tree type.
70 * To implement an on-disk btree (extent tree) type in ocfs2, add
71 * an ocfs2_extent_tree_operations structure and the matching
72 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
73 * for the allocation portion of the extent tree.
75 struct ocfs2_extent_tree_operations {
77 * last_eb_blk is the block number of the right most leaf extent
78 * block. Most on-disk structures containing an extent tree store
79 * this value for fast access. The ->eo_set_last_eb_blk() and
80 * ->eo_get_last_eb_blk() operations access this value. They are
83 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
85 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
88 * The on-disk structure usually keeps track of how many total
89 * clusters are stored in this extent tree. This function updates
90 * that value. new_clusters is the delta, and must be
91 * added to the total. Required.
93 void (*eo_update_clusters)(struct ocfs2_extent_tree *et,
97 * If this extent tree is supported by an extent map, insert
98 * a record into the map.
100 void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et,
101 struct ocfs2_extent_rec *rec);
104 * If this extent tree is supported by an extent map, truncate the
107 void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et,
111 * If ->eo_insert_check() exists, it is called before rec is
112 * inserted into the extent tree. It is optional.
114 int (*eo_insert_check)(struct ocfs2_extent_tree *et,
115 struct ocfs2_extent_rec *rec);
116 int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
119 * --------------------------------------------------------------
120 * The remaining are internal to ocfs2_extent_tree and don't have
125 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
128 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
131 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
132 * it exists. If it does not, et->et_max_leaf_clusters is set
133 * to 0 (unlimited). Optional.
135 void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
138 * ->eo_extent_contig test whether the 2 ocfs2_extent_rec
139 * are contiguous or not. Optional. Don't need to set it if use
140 * ocfs2_extent_rec as the tree leaf.
142 enum ocfs2_contig_type
143 (*eo_extent_contig)(struct ocfs2_extent_tree *et,
144 struct ocfs2_extent_rec *ext,
145 struct ocfs2_extent_rec *insert_rec);
150 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
153 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
154 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
156 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
158 static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
159 struct ocfs2_extent_rec *rec);
160 static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
162 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
163 struct ocfs2_extent_rec *rec);
164 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
165 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
166 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
167 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
168 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
169 .eo_update_clusters = ocfs2_dinode_update_clusters,
170 .eo_extent_map_insert = ocfs2_dinode_extent_map_insert,
171 .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate,
172 .eo_insert_check = ocfs2_dinode_insert_check,
173 .eo_sanity_check = ocfs2_dinode_sanity_check,
174 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
177 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
180 struct ocfs2_dinode *di = et->et_object;
182 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
183 di->i_last_eb_blk = cpu_to_le64(blkno);
186 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
188 struct ocfs2_dinode *di = et->et_object;
190 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
191 return le64_to_cpu(di->i_last_eb_blk);
194 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
197 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
198 struct ocfs2_dinode *di = et->et_object;
200 le32_add_cpu(&di->i_clusters, clusters);
201 spin_lock(&oi->ip_lock);
202 oi->ip_clusters = le32_to_cpu(di->i_clusters);
203 spin_unlock(&oi->ip_lock);
206 static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
207 struct ocfs2_extent_rec *rec)
209 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
211 ocfs2_extent_map_insert_rec(inode, rec);
214 static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
217 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
219 ocfs2_extent_map_trunc(inode, clusters);
222 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
223 struct ocfs2_extent_rec *rec)
225 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
226 struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
228 BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
229 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
230 (oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
231 "Device %s, asking for sparse allocation: inode %llu, "
232 "cpos %u, clusters %u\n",
234 (unsigned long long)oi->ip_blkno,
235 rec->e_cpos, oi->ip_clusters);
240 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
242 struct ocfs2_dinode *di = et->et_object;
244 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
245 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
250 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
252 struct ocfs2_dinode *di = et->et_object;
254 et->et_root_el = &di->id2.i_list;
258 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
260 struct ocfs2_xattr_value_buf *vb = et->et_object;
262 et->et_root_el = &vb->vb_xv->xr_list;
265 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
268 struct ocfs2_xattr_value_buf *vb = et->et_object;
270 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
273 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
275 struct ocfs2_xattr_value_buf *vb = et->et_object;
277 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
280 static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
283 struct ocfs2_xattr_value_buf *vb = et->et_object;
285 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
288 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
289 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
290 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
291 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
292 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
295 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
297 struct ocfs2_xattr_block *xb = et->et_object;
299 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
302 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
304 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
305 et->et_max_leaf_clusters =
306 ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
309 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
312 struct ocfs2_xattr_block *xb = et->et_object;
313 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
315 xt->xt_last_eb_blk = cpu_to_le64(blkno);
318 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
320 struct ocfs2_xattr_block *xb = et->et_object;
321 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
323 return le64_to_cpu(xt->xt_last_eb_blk);
326 static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
329 struct ocfs2_xattr_block *xb = et->et_object;
331 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
334 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
335 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
336 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
337 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
338 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
339 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
342 static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
345 struct ocfs2_dx_root_block *dx_root = et->et_object;
347 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
350 static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
352 struct ocfs2_dx_root_block *dx_root = et->et_object;
354 return le64_to_cpu(dx_root->dr_last_eb_blk);
357 static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
360 struct ocfs2_dx_root_block *dx_root = et->et_object;
362 le32_add_cpu(&dx_root->dr_clusters, clusters);
365 static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
367 struct ocfs2_dx_root_block *dx_root = et->et_object;
369 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
374 static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
376 struct ocfs2_dx_root_block *dx_root = et->et_object;
378 et->et_root_el = &dx_root->dr_list;
381 static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
382 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
383 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
384 .eo_update_clusters = ocfs2_dx_root_update_clusters,
385 .eo_sanity_check = ocfs2_dx_root_sanity_check,
386 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
389 static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et)
391 struct ocfs2_refcount_block *rb = et->et_object;
393 et->et_root_el = &rb->rf_list;
396 static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
399 struct ocfs2_refcount_block *rb = et->et_object;
401 rb->rf_last_eb_blk = cpu_to_le64(blkno);
404 static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
406 struct ocfs2_refcount_block *rb = et->et_object;
408 return le64_to_cpu(rb->rf_last_eb_blk);
411 static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et,
414 struct ocfs2_refcount_block *rb = et->et_object;
416 le32_add_cpu(&rb->rf_clusters, clusters);
419 static enum ocfs2_contig_type
420 ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et,
421 struct ocfs2_extent_rec *ext,
422 struct ocfs2_extent_rec *insert_rec)
427 static struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = {
428 .eo_set_last_eb_blk = ocfs2_refcount_tree_set_last_eb_blk,
429 .eo_get_last_eb_blk = ocfs2_refcount_tree_get_last_eb_blk,
430 .eo_update_clusters = ocfs2_refcount_tree_update_clusters,
431 .eo_fill_root_el = ocfs2_refcount_tree_fill_root_el,
432 .eo_extent_contig = ocfs2_refcount_tree_extent_contig,
435 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
436 struct ocfs2_caching_info *ci,
437 struct buffer_head *bh,
438 ocfs2_journal_access_func access,
440 struct ocfs2_extent_tree_operations *ops)
445 et->et_root_journal_access = access;
447 obj = (void *)bh->b_data;
450 et->et_ops->eo_fill_root_el(et);
451 if (!et->et_ops->eo_fill_max_leaf_clusters)
452 et->et_max_leaf_clusters = 0;
454 et->et_ops->eo_fill_max_leaf_clusters(et);
457 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
458 struct ocfs2_caching_info *ci,
459 struct buffer_head *bh)
461 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di,
462 NULL, &ocfs2_dinode_et_ops);
465 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
466 struct ocfs2_caching_info *ci,
467 struct buffer_head *bh)
469 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb,
470 NULL, &ocfs2_xattr_tree_et_ops);
473 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
474 struct ocfs2_caching_info *ci,
475 struct ocfs2_xattr_value_buf *vb)
477 __ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb,
478 &ocfs2_xattr_value_et_ops);
481 void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
482 struct ocfs2_caching_info *ci,
483 struct buffer_head *bh)
485 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr,
486 NULL, &ocfs2_dx_root_et_ops);
489 void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
490 struct ocfs2_caching_info *ci,
491 struct buffer_head *bh)
493 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb,
494 NULL, &ocfs2_refcount_tree_et_ops);
497 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
500 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
503 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
505 return et->et_ops->eo_get_last_eb_blk(et);
508 static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
511 et->et_ops->eo_update_clusters(et, clusters);
514 static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et,
515 struct ocfs2_extent_rec *rec)
517 if (et->et_ops->eo_extent_map_insert)
518 et->et_ops->eo_extent_map_insert(et, rec);
521 static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et,
524 if (et->et_ops->eo_extent_map_truncate)
525 et->et_ops->eo_extent_map_truncate(et, clusters);
528 static inline int ocfs2_et_root_journal_access(handle_t *handle,
529 struct ocfs2_extent_tree *et,
532 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
536 static inline enum ocfs2_contig_type
537 ocfs2_et_extent_contig(struct ocfs2_extent_tree *et,
538 struct ocfs2_extent_rec *rec,
539 struct ocfs2_extent_rec *insert_rec)
541 if (et->et_ops->eo_extent_contig)
542 return et->et_ops->eo_extent_contig(et, rec, insert_rec);
544 return ocfs2_extent_rec_contig(
545 ocfs2_metadata_cache_get_super(et->et_ci),
549 static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
550 struct ocfs2_extent_rec *rec)
554 if (et->et_ops->eo_insert_check)
555 ret = et->et_ops->eo_insert_check(et, rec);
559 static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
563 if (et->et_ops->eo_sanity_check)
564 ret = et->et_ops->eo_sanity_check(et);
568 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
569 struct ocfs2_extent_block *eb);
570 static void ocfs2_adjust_rightmost_records(handle_t *handle,
571 struct ocfs2_extent_tree *et,
572 struct ocfs2_path *path,
573 struct ocfs2_extent_rec *insert_rec);
575 * Reset the actual path elements so that we can re-use the structure
576 * to build another path. Generally, this involves freeing the buffer
579 void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
581 int i, start = 0, depth = 0;
582 struct ocfs2_path_item *node;
587 for(i = start; i < path_num_items(path); i++) {
588 node = &path->p_node[i];
596 * Tree depth may change during truncate, or insert. If we're
597 * keeping the root extent list, then make sure that our path
598 * structure reflects the proper depth.
601 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
603 path_root_access(path) = NULL;
605 path->p_tree_depth = depth;
608 void ocfs2_free_path(struct ocfs2_path *path)
611 ocfs2_reinit_path(path, 0);
617 * All the elements of src into dest. After this call, src could be freed
618 * without affecting dest.
620 * Both paths should have the same root. Any non-root elements of dest
623 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
627 BUG_ON(path_root_bh(dest) != path_root_bh(src));
628 BUG_ON(path_root_el(dest) != path_root_el(src));
629 BUG_ON(path_root_access(dest) != path_root_access(src));
631 ocfs2_reinit_path(dest, 1);
633 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
634 dest->p_node[i].bh = src->p_node[i].bh;
635 dest->p_node[i].el = src->p_node[i].el;
637 if (dest->p_node[i].bh)
638 get_bh(dest->p_node[i].bh);
643 * Make the *dest path the same as src and re-initialize src path to
646 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
650 BUG_ON(path_root_bh(dest) != path_root_bh(src));
651 BUG_ON(path_root_access(dest) != path_root_access(src));
653 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
654 brelse(dest->p_node[i].bh);
656 dest->p_node[i].bh = src->p_node[i].bh;
657 dest->p_node[i].el = src->p_node[i].el;
659 src->p_node[i].bh = NULL;
660 src->p_node[i].el = NULL;
665 * Insert an extent block at given index.
667 * This will not take an additional reference on eb_bh.
669 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
670 struct buffer_head *eb_bh)
672 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
675 * Right now, no root bh is an extent block, so this helps
676 * catch code errors with dinode trees. The assertion can be
677 * safely removed if we ever need to insert extent block
678 * structures at the root.
682 path->p_node[index].bh = eb_bh;
683 path->p_node[index].el = &eb->h_list;
686 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
687 struct ocfs2_extent_list *root_el,
688 ocfs2_journal_access_func access)
690 struct ocfs2_path *path;
692 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
694 path = kzalloc(sizeof(*path), GFP_NOFS);
696 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
698 path_root_bh(path) = root_bh;
699 path_root_el(path) = root_el;
700 path_root_access(path) = access;
706 struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
708 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
709 path_root_access(path));
712 struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
714 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
715 et->et_root_journal_access);
719 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
720 * otherwise it's the root_access function.
722 * I don't like the way this function's name looks next to
723 * ocfs2_journal_access_path(), but I don't have a better one.
725 int ocfs2_path_bh_journal_access(handle_t *handle,
726 struct ocfs2_caching_info *ci,
727 struct ocfs2_path *path,
730 ocfs2_journal_access_func access = path_root_access(path);
733 access = ocfs2_journal_access;
736 access = ocfs2_journal_access_eb;
738 return access(handle, ci, path->p_node[idx].bh,
739 OCFS2_JOURNAL_ACCESS_WRITE);
743 * Convenience function to journal all components in a path.
745 int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
747 struct ocfs2_path *path)
754 for(i = 0; i < path_num_items(path); i++) {
755 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
767 * Return the index of the extent record which contains cluster #v_cluster.
768 * -1 is returned if it was not found.
770 * Should work fine on interior and exterior nodes.
772 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
776 struct ocfs2_extent_rec *rec;
777 u32 rec_end, rec_start, clusters;
779 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
780 rec = &el->l_recs[i];
782 rec_start = le32_to_cpu(rec->e_cpos);
783 clusters = ocfs2_rec_clusters(el, rec);
785 rec_end = rec_start + clusters;
787 if (v_cluster >= rec_start && v_cluster < rec_end) {
797 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
798 * ocfs2_extent_rec_contig only work properly against leaf nodes!
800 static int ocfs2_block_extent_contig(struct super_block *sb,
801 struct ocfs2_extent_rec *ext,
804 u64 blk_end = le64_to_cpu(ext->e_blkno);
806 blk_end += ocfs2_clusters_to_blocks(sb,
807 le16_to_cpu(ext->e_leaf_clusters));
809 return blkno == blk_end;
812 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
813 struct ocfs2_extent_rec *right)
817 left_range = le32_to_cpu(left->e_cpos) +
818 le16_to_cpu(left->e_leaf_clusters);
820 return (left_range == le32_to_cpu(right->e_cpos));
823 static enum ocfs2_contig_type
824 ocfs2_extent_rec_contig(struct super_block *sb,
825 struct ocfs2_extent_rec *ext,
826 struct ocfs2_extent_rec *insert_rec)
828 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
831 * Refuse to coalesce extent records with different flag
832 * fields - we don't want to mix unwritten extents with user
835 if (ext->e_flags != insert_rec->e_flags)
838 if (ocfs2_extents_adjacent(ext, insert_rec) &&
839 ocfs2_block_extent_contig(sb, ext, blkno))
842 blkno = le64_to_cpu(ext->e_blkno);
843 if (ocfs2_extents_adjacent(insert_rec, ext) &&
844 ocfs2_block_extent_contig(sb, insert_rec, blkno))
851 * NOTE: We can have pretty much any combination of contiguousness and
854 * The usefulness of APPEND_TAIL is more in that it lets us know that
855 * we'll have to update the path to that leaf.
857 enum ocfs2_append_type {
862 enum ocfs2_split_type {
868 struct ocfs2_insert_type {
869 enum ocfs2_split_type ins_split;
870 enum ocfs2_append_type ins_appending;
871 enum ocfs2_contig_type ins_contig;
872 int ins_contig_index;
876 struct ocfs2_merge_ctxt {
877 enum ocfs2_contig_type c_contig_type;
878 int c_has_empty_extent;
879 int c_split_covers_rec;
882 static int ocfs2_validate_extent_block(struct super_block *sb,
883 struct buffer_head *bh)
886 struct ocfs2_extent_block *eb =
887 (struct ocfs2_extent_block *)bh->b_data;
889 mlog(0, "Validating extent block %llu\n",
890 (unsigned long long)bh->b_blocknr);
892 BUG_ON(!buffer_uptodate(bh));
895 * If the ecc fails, we return the error but otherwise
896 * leave the filesystem running. We know any error is
897 * local to this block.
899 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
901 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
902 (unsigned long long)bh->b_blocknr);
907 * Errors after here are fatal.
910 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
912 "Extent block #%llu has bad signature %.*s",
913 (unsigned long long)bh->b_blocknr, 7,
918 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
920 "Extent block #%llu has an invalid h_blkno "
922 (unsigned long long)bh->b_blocknr,
923 (unsigned long long)le64_to_cpu(eb->h_blkno));
927 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
929 "Extent block #%llu has an invalid "
930 "h_fs_generation of #%u",
931 (unsigned long long)bh->b_blocknr,
932 le32_to_cpu(eb->h_fs_generation));
939 int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
940 struct buffer_head **bh)
943 struct buffer_head *tmp = *bh;
945 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
946 ocfs2_validate_extent_block);
948 /* If ocfs2_read_block() got us a new bh, pass it up. */
957 * How many free extents have we got before we need more meta data?
959 int ocfs2_num_free_extents(struct ocfs2_super *osb,
960 struct ocfs2_extent_tree *et)
963 struct ocfs2_extent_list *el = NULL;
964 struct ocfs2_extent_block *eb;
965 struct buffer_head *eb_bh = NULL;
969 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
972 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
978 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
982 BUG_ON(el->l_tree_depth != 0);
984 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
992 /* expects array to already be allocated
994 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
997 static int ocfs2_create_new_meta_bhs(handle_t *handle,
998 struct ocfs2_extent_tree *et,
1000 struct ocfs2_alloc_context *meta_ac,
1001 struct buffer_head *bhs[])
1003 int count, status, i;
1004 u16 suballoc_bit_start;
1006 u64 suballoc_loc, first_blkno;
1007 struct ocfs2_super *osb =
1008 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
1009 struct ocfs2_extent_block *eb;
1012 while (count < wanted) {
1013 status = ocfs2_claim_metadata(handle,
1017 &suballoc_bit_start,
1025 for(i = count; i < (num_got + count); i++) {
1026 bhs[i] = sb_getblk(osb->sb, first_blkno);
1027 if (bhs[i] == NULL) {
1032 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
1034 status = ocfs2_journal_access_eb(handle, et->et_ci,
1036 OCFS2_JOURNAL_ACCESS_CREATE);
1042 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
1043 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
1044 /* Ok, setup the minimal stuff here. */
1045 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
1046 eb->h_blkno = cpu_to_le64(first_blkno);
1047 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
1048 eb->h_suballoc_slot =
1049 cpu_to_le16(meta_ac->ac_alloc_slot);
1050 eb->h_suballoc_loc = cpu_to_le64(suballoc_loc);
1051 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1052 eb->h_list.l_count =
1053 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1055 suballoc_bit_start++;
1058 /* We'll also be dirtied by the caller, so
1059 * this isn't absolutely necessary. */
1060 ocfs2_journal_dirty(handle, bhs[i]);
1069 for(i = 0; i < wanted; i++) {
1079 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1081 * Returns the sum of the rightmost extent rec logical offset and
1084 * ocfs2_add_branch() uses this to determine what logical cluster
1085 * value should be populated into the leftmost new branch records.
1087 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1088 * value for the new topmost tree record.
1090 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1094 i = le16_to_cpu(el->l_next_free_rec) - 1;
1096 return le32_to_cpu(el->l_recs[i].e_cpos) +
1097 ocfs2_rec_clusters(el, &el->l_recs[i]);
1101 * Change range of the branches in the right most path according to the leaf
1102 * extent block's rightmost record.
1104 static int ocfs2_adjust_rightmost_branch(handle_t *handle,
1105 struct ocfs2_extent_tree *et)
1108 struct ocfs2_path *path = NULL;
1109 struct ocfs2_extent_list *el;
1110 struct ocfs2_extent_rec *rec;
1112 path = ocfs2_new_path_from_et(et);
1118 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
1124 status = ocfs2_extend_trans(handle, path_num_items(path));
1130 status = ocfs2_journal_access_path(et->et_ci, handle, path);
1136 el = path_leaf_el(path);
1137 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1139 ocfs2_adjust_rightmost_records(handle, et, path, rec);
1142 ocfs2_free_path(path);
1147 * Add an entire tree branch to our inode. eb_bh is the extent block
1148 * to start at, if we don't want to start the branch at the root
1151 * last_eb_bh is required as we have to update it's next_leaf pointer
1152 * for the new last extent block.
1154 * the new branch will be 'empty' in the sense that every block will
1155 * contain a single record with cluster count == 0.
1157 static int ocfs2_add_branch(handle_t *handle,
1158 struct ocfs2_extent_tree *et,
1159 struct buffer_head *eb_bh,
1160 struct buffer_head **last_eb_bh,
1161 struct ocfs2_alloc_context *meta_ac)
1163 int status, new_blocks, i;
1164 u64 next_blkno, new_last_eb_blk;
1165 struct buffer_head *bh;
1166 struct buffer_head **new_eb_bhs = NULL;
1167 struct ocfs2_extent_block *eb;
1168 struct ocfs2_extent_list *eb_el;
1169 struct ocfs2_extent_list *el;
1170 u32 new_cpos, root_end;
1172 BUG_ON(!last_eb_bh || !*last_eb_bh);
1175 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1178 el = et->et_root_el;
1180 /* we never add a branch to a leaf. */
1181 BUG_ON(!el->l_tree_depth);
1183 new_blocks = le16_to_cpu(el->l_tree_depth);
1185 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1186 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1187 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1190 * If there is a gap before the root end and the real end
1191 * of the righmost leaf block, we need to remove the gap
1192 * between new_cpos and root_end first so that the tree
1193 * is consistent after we add a new branch(it will start
1196 if (root_end > new_cpos) {
1197 mlog(0, "adjust the cluster end from %u to %u\n",
1198 root_end, new_cpos);
1199 status = ocfs2_adjust_rightmost_branch(handle, et);
1206 /* allocate the number of new eb blocks we need */
1207 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1215 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
1216 meta_ac, new_eb_bhs);
1222 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1223 * linked with the rest of the tree.
1224 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1226 * when we leave the loop, new_last_eb_blk will point to the
1227 * newest leaf, and next_blkno will point to the topmost extent
1229 next_blkno = new_last_eb_blk = 0;
1230 for(i = 0; i < new_blocks; i++) {
1232 eb = (struct ocfs2_extent_block *) bh->b_data;
1233 /* ocfs2_create_new_meta_bhs() should create it right! */
1234 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1235 eb_el = &eb->h_list;
1237 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
1238 OCFS2_JOURNAL_ACCESS_CREATE);
1244 eb->h_next_leaf_blk = 0;
1245 eb_el->l_tree_depth = cpu_to_le16(i);
1246 eb_el->l_next_free_rec = cpu_to_le16(1);
1248 * This actually counts as an empty extent as
1251 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
1252 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
1254 * eb_el isn't always an interior node, but even leaf
1255 * nodes want a zero'd flags and reserved field so
1256 * this gets the whole 32 bits regardless of use.
1258 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
1259 if (!eb_el->l_tree_depth)
1260 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1262 ocfs2_journal_dirty(handle, bh);
1263 next_blkno = le64_to_cpu(eb->h_blkno);
1266 /* This is a bit hairy. We want to update up to three blocks
1267 * here without leaving any of them in an inconsistent state
1268 * in case of error. We don't have to worry about
1269 * journal_dirty erroring as it won't unless we've aborted the
1270 * handle (in which case we would never be here) so reserving
1271 * the write with journal_access is all we need to do. */
1272 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
1273 OCFS2_JOURNAL_ACCESS_WRITE);
1278 status = ocfs2_et_root_journal_access(handle, et,
1279 OCFS2_JOURNAL_ACCESS_WRITE);
1285 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
1286 OCFS2_JOURNAL_ACCESS_WRITE);
1293 /* Link the new branch into the rest of the tree (el will
1294 * either be on the root_bh, or the extent block passed in. */
1295 i = le16_to_cpu(el->l_next_free_rec);
1296 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
1297 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1298 el->l_recs[i].e_int_clusters = 0;
1299 le16_add_cpu(&el->l_next_free_rec, 1);
1301 /* fe needs a new last extent block pointer, as does the
1302 * next_leaf on the previously last-extent-block. */
1303 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
1305 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
1306 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1308 ocfs2_journal_dirty(handle, *last_eb_bh);
1309 ocfs2_journal_dirty(handle, et->et_root_bh);
1311 ocfs2_journal_dirty(handle, eb_bh);
1314 * Some callers want to track the rightmost leaf so pass it
1317 brelse(*last_eb_bh);
1318 get_bh(new_eb_bhs[0]);
1319 *last_eb_bh = new_eb_bhs[0];
1324 for (i = 0; i < new_blocks; i++)
1325 brelse(new_eb_bhs[i]);
1334 * adds another level to the allocation tree.
1335 * returns back the new extent block so you can add a branch to it
1338 static int ocfs2_shift_tree_depth(handle_t *handle,
1339 struct ocfs2_extent_tree *et,
1340 struct ocfs2_alloc_context *meta_ac,
1341 struct buffer_head **ret_new_eb_bh)
1345 struct buffer_head *new_eb_bh = NULL;
1346 struct ocfs2_extent_block *eb;
1347 struct ocfs2_extent_list *root_el;
1348 struct ocfs2_extent_list *eb_el;
1350 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
1357 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1358 /* ocfs2_create_new_meta_bhs() should create it right! */
1359 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1361 eb_el = &eb->h_list;
1362 root_el = et->et_root_el;
1364 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
1365 OCFS2_JOURNAL_ACCESS_CREATE);
1371 /* copy the root extent list data into the new extent block */
1372 eb_el->l_tree_depth = root_el->l_tree_depth;
1373 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1374 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1375 eb_el->l_recs[i] = root_el->l_recs[i];
1377 ocfs2_journal_dirty(handle, new_eb_bh);
1379 status = ocfs2_et_root_journal_access(handle, et,
1380 OCFS2_JOURNAL_ACCESS_WRITE);
1386 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1388 /* update root_bh now */
1389 le16_add_cpu(&root_el->l_tree_depth, 1);
1390 root_el->l_recs[0].e_cpos = 0;
1391 root_el->l_recs[0].e_blkno = eb->h_blkno;
1392 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1393 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1394 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1395 root_el->l_next_free_rec = cpu_to_le16(1);
1397 /* If this is our 1st tree depth shift, then last_eb_blk
1398 * becomes the allocated extent block */
1399 if (root_el->l_tree_depth == cpu_to_le16(1))
1400 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1402 ocfs2_journal_dirty(handle, et->et_root_bh);
1404 *ret_new_eb_bh = new_eb_bh;
1415 * Should only be called when there is no space left in any of the
1416 * leaf nodes. What we want to do is find the lowest tree depth
1417 * non-leaf extent block with room for new records. There are three
1418 * valid results of this search:
1420 * 1) a lowest extent block is found, then we pass it back in
1421 * *lowest_eb_bh and return '0'
1423 * 2) the search fails to find anything, but the root_el has room. We
1424 * pass NULL back in *lowest_eb_bh, but still return '0'
1426 * 3) the search fails to find anything AND the root_el is full, in
1427 * which case we return > 0
1429 * return status < 0 indicates an error.
1431 static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
1432 struct buffer_head **target_bh)
1436 struct ocfs2_extent_block *eb;
1437 struct ocfs2_extent_list *el;
1438 struct buffer_head *bh = NULL;
1439 struct buffer_head *lowest_bh = NULL;
1443 el = et->et_root_el;
1445 while(le16_to_cpu(el->l_tree_depth) > 1) {
1446 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1447 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1448 "Owner %llu has empty "
1449 "extent list (next_free_rec == 0)",
1450 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
1454 i = le16_to_cpu(el->l_next_free_rec) - 1;
1455 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1457 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1458 "Owner %llu has extent "
1459 "list where extent # %d has no physical "
1461 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
1469 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
1475 eb = (struct ocfs2_extent_block *) bh->b_data;
1478 if (le16_to_cpu(el->l_next_free_rec) <
1479 le16_to_cpu(el->l_count)) {
1486 /* If we didn't find one and the fe doesn't have any room,
1487 * then return '1' */
1488 el = et->et_root_el;
1489 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1492 *target_bh = lowest_bh;
1501 * Grow a b-tree so that it has more records.
1503 * We might shift the tree depth in which case existing paths should
1504 * be considered invalid.
1506 * Tree depth after the grow is returned via *final_depth.
1508 * *last_eb_bh will be updated by ocfs2_add_branch().
1510 static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1511 int *final_depth, struct buffer_head **last_eb_bh,
1512 struct ocfs2_alloc_context *meta_ac)
1515 struct ocfs2_extent_list *el = et->et_root_el;
1516 int depth = le16_to_cpu(el->l_tree_depth);
1517 struct buffer_head *bh = NULL;
1519 BUG_ON(meta_ac == NULL);
1521 shift = ocfs2_find_branch_target(et, &bh);
1528 /* We traveled all the way to the bottom of the allocation tree
1529 * and didn't find room for any more extents - we need to add
1530 * another tree level */
1533 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1535 /* ocfs2_shift_tree_depth will return us a buffer with
1536 * the new extent block (so we can pass that to
1537 * ocfs2_add_branch). */
1538 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
1546 * Special case: we have room now if we shifted from
1547 * tree_depth 0, so no more work needs to be done.
1549 * We won't be calling add_branch, so pass
1550 * back *last_eb_bh as the new leaf. At depth
1551 * zero, it should always be null so there's
1552 * no reason to brelse.
1554 BUG_ON(*last_eb_bh);
1561 /* call ocfs2_add_branch to add the final part of the tree with
1563 mlog(0, "add branch. bh = %p\n", bh);
1564 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
1573 *final_depth = depth;
1579 * This function will discard the rightmost extent record.
1581 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1583 int next_free = le16_to_cpu(el->l_next_free_rec);
1584 int count = le16_to_cpu(el->l_count);
1585 unsigned int num_bytes;
1588 /* This will cause us to go off the end of our extent list. */
1589 BUG_ON(next_free >= count);
1591 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1593 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1596 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1597 struct ocfs2_extent_rec *insert_rec)
1599 int i, insert_index, next_free, has_empty, num_bytes;
1600 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1601 struct ocfs2_extent_rec *rec;
1603 next_free = le16_to_cpu(el->l_next_free_rec);
1604 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1608 /* The tree code before us didn't allow enough room in the leaf. */
1609 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1612 * The easiest way to approach this is to just remove the
1613 * empty extent and temporarily decrement next_free.
1617 * If next_free was 1 (only an empty extent), this
1618 * loop won't execute, which is fine. We still want
1619 * the decrement above to happen.
1621 for(i = 0; i < (next_free - 1); i++)
1622 el->l_recs[i] = el->l_recs[i+1];
1628 * Figure out what the new record index should be.
1630 for(i = 0; i < next_free; i++) {
1631 rec = &el->l_recs[i];
1633 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1638 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1639 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1641 BUG_ON(insert_index < 0);
1642 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1643 BUG_ON(insert_index > next_free);
1646 * No need to memmove if we're just adding to the tail.
1648 if (insert_index != next_free) {
1649 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1651 num_bytes = next_free - insert_index;
1652 num_bytes *= sizeof(struct ocfs2_extent_rec);
1653 memmove(&el->l_recs[insert_index + 1],
1654 &el->l_recs[insert_index],
1659 * Either we had an empty extent, and need to re-increment or
1660 * there was no empty extent on a non full rightmost leaf node,
1661 * in which case we still need to increment.
1664 el->l_next_free_rec = cpu_to_le16(next_free);
1666 * Make sure none of the math above just messed up our tree.
1668 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1670 el->l_recs[insert_index] = *insert_rec;
1674 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1676 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1678 BUG_ON(num_recs == 0);
1680 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1682 size = num_recs * sizeof(struct ocfs2_extent_rec);
1683 memmove(&el->l_recs[0], &el->l_recs[1], size);
1684 memset(&el->l_recs[num_recs], 0,
1685 sizeof(struct ocfs2_extent_rec));
1686 el->l_next_free_rec = cpu_to_le16(num_recs);
1691 * Create an empty extent record .
1693 * l_next_free_rec may be updated.
1695 * If an empty extent already exists do nothing.
1697 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1699 int next_free = le16_to_cpu(el->l_next_free_rec);
1701 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1706 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1709 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1710 "Asked to create an empty extent in a full list:\n"
1711 "count = %u, tree depth = %u",
1712 le16_to_cpu(el->l_count),
1713 le16_to_cpu(el->l_tree_depth));
1715 ocfs2_shift_records_right(el);
1718 le16_add_cpu(&el->l_next_free_rec, 1);
1719 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1723 * For a rotation which involves two leaf nodes, the "root node" is
1724 * the lowest level tree node which contains a path to both leafs. This
1725 * resulting set of information can be used to form a complete "subtree"
1727 * This function is passed two full paths from the dinode down to a
1728 * pair of adjacent leaves. It's task is to figure out which path
1729 * index contains the subtree root - this can be the root index itself
1730 * in a worst-case rotation.
1732 * The array index of the subtree root is passed back.
1734 int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1735 struct ocfs2_path *left,
1736 struct ocfs2_path *right)
1741 * Check that the caller passed in two paths from the same tree.
1743 BUG_ON(path_root_bh(left) != path_root_bh(right));
1749 * The caller didn't pass two adjacent paths.
1751 mlog_bug_on_msg(i > left->p_tree_depth,
1752 "Owner %llu, left depth %u, right depth %u\n"
1753 "left leaf blk %llu, right leaf blk %llu\n",
1754 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1755 left->p_tree_depth, right->p_tree_depth,
1756 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1757 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1758 } while (left->p_node[i].bh->b_blocknr ==
1759 right->p_node[i].bh->b_blocknr);
1764 typedef void (path_insert_t)(void *, struct buffer_head *);
1767 * Traverse a btree path in search of cpos, starting at root_el.
1769 * This code can be called with a cpos larger than the tree, in which
1770 * case it will return the rightmost path.
1772 static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
1773 struct ocfs2_extent_list *root_el, u32 cpos,
1774 path_insert_t *func, void *data)
1779 struct buffer_head *bh = NULL;
1780 struct ocfs2_extent_block *eb;
1781 struct ocfs2_extent_list *el;
1782 struct ocfs2_extent_rec *rec;
1785 while (el->l_tree_depth) {
1786 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1787 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1788 "Owner %llu has empty extent list at "
1790 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1791 le16_to_cpu(el->l_tree_depth));
1797 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1798 rec = &el->l_recs[i];
1801 * In the case that cpos is off the allocation
1802 * tree, this should just wind up returning the
1805 range = le32_to_cpu(rec->e_cpos) +
1806 ocfs2_rec_clusters(el, rec);
1807 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1811 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1813 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1814 "Owner %llu has bad blkno in extent list "
1815 "at depth %u (index %d)\n",
1816 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1817 le16_to_cpu(el->l_tree_depth), i);
1824 ret = ocfs2_read_extent_block(ci, blkno, &bh);
1830 eb = (struct ocfs2_extent_block *) bh->b_data;
1833 if (le16_to_cpu(el->l_next_free_rec) >
1834 le16_to_cpu(el->l_count)) {
1835 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1836 "Owner %llu has bad count in extent list "
1837 "at block %llu (next free=%u, count=%u)\n",
1838 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1839 (unsigned long long)bh->b_blocknr,
1840 le16_to_cpu(el->l_next_free_rec),
1841 le16_to_cpu(el->l_count));
1852 * Catch any trailing bh that the loop didn't handle.
1860 * Given an initialized path (that is, it has a valid root extent
1861 * list), this function will traverse the btree in search of the path
1862 * which would contain cpos.
1864 * The path traveled is recorded in the path structure.
1866 * Note that this will not do any comparisons on leaf node extent
1867 * records, so it will work fine in the case that we just added a tree
1870 struct find_path_data {
1872 struct ocfs2_path *path;
1874 static void find_path_ins(void *data, struct buffer_head *bh)
1876 struct find_path_data *fp = data;
1879 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1882 int ocfs2_find_path(struct ocfs2_caching_info *ci,
1883 struct ocfs2_path *path, u32 cpos)
1885 struct find_path_data data;
1889 return __ocfs2_find_path(ci, path_root_el(path), cpos,
1890 find_path_ins, &data);
1893 static void find_leaf_ins(void *data, struct buffer_head *bh)
1895 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1896 struct ocfs2_extent_list *el = &eb->h_list;
1897 struct buffer_head **ret = data;
1899 /* We want to retain only the leaf block. */
1900 if (le16_to_cpu(el->l_tree_depth) == 0) {
1906 * Find the leaf block in the tree which would contain cpos. No
1907 * checking of the actual leaf is done.
1909 * Some paths want to call this instead of allocating a path structure
1910 * and calling ocfs2_find_path().
1912 * This function doesn't handle non btree extent lists.
1914 int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1915 struct ocfs2_extent_list *root_el, u32 cpos,
1916 struct buffer_head **leaf_bh)
1919 struct buffer_head *bh = NULL;
1921 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
1933 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1935 * Basically, we've moved stuff around at the bottom of the tree and
1936 * we need to fix up the extent records above the changes to reflect
1939 * left_rec: the record on the left.
1940 * left_child_el: is the child list pointed to by left_rec
1941 * right_rec: the record to the right of left_rec
1942 * right_child_el: is the child list pointed to by right_rec
1944 * By definition, this only works on interior nodes.
1946 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1947 struct ocfs2_extent_list *left_child_el,
1948 struct ocfs2_extent_rec *right_rec,
1949 struct ocfs2_extent_list *right_child_el)
1951 u32 left_clusters, right_end;
1954 * Interior nodes never have holes. Their cpos is the cpos of
1955 * the leftmost record in their child list. Their cluster
1956 * count covers the full theoretical range of their child list
1957 * - the range between their cpos and the cpos of the record
1958 * immediately to their right.
1960 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1961 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1962 BUG_ON(right_child_el->l_tree_depth);
1963 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1964 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1966 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1967 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1970 * Calculate the rightmost cluster count boundary before
1971 * moving cpos - we will need to adjust clusters after
1972 * updating e_cpos to keep the same highest cluster count.
1974 right_end = le32_to_cpu(right_rec->e_cpos);
1975 right_end += le32_to_cpu(right_rec->e_int_clusters);
1977 right_rec->e_cpos = left_rec->e_cpos;
1978 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1980 right_end -= le32_to_cpu(right_rec->e_cpos);
1981 right_rec->e_int_clusters = cpu_to_le32(right_end);
1985 * Adjust the adjacent root node records involved in a
1986 * rotation. left_el_blkno is passed in as a key so that we can easily
1987 * find it's index in the root list.
1989 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1990 struct ocfs2_extent_list *left_el,
1991 struct ocfs2_extent_list *right_el,
1996 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1997 le16_to_cpu(left_el->l_tree_depth));
1999 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
2000 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
2005 * The path walking code should have never returned a root and
2006 * two paths which are not adjacent.
2008 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2010 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
2011 &root_el->l_recs[i + 1], right_el);
2015 * We've changed a leaf block (in right_path) and need to reflect that
2016 * change back up the subtree.
2018 * This happens in multiple places:
2019 * - When we've moved an extent record from the left path leaf to the right
2020 * path leaf to make room for an empty extent in the left path leaf.
2021 * - When our insert into the right path leaf is at the leftmost edge
2022 * and requires an update of the path immediately to it's left. This
2023 * can occur at the end of some types of rotation and appending inserts.
2024 * - When we've adjusted the last extent record in the left path leaf and the
2025 * 1st extent record in the right path leaf during cross extent block merge.
2027 static void ocfs2_complete_edge_insert(handle_t *handle,
2028 struct ocfs2_path *left_path,
2029 struct ocfs2_path *right_path,
2033 struct ocfs2_extent_list *el, *left_el, *right_el;
2034 struct ocfs2_extent_rec *left_rec, *right_rec;
2035 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2038 * Update the counts and position values within all the
2039 * interior nodes to reflect the leaf rotation we just did.
2041 * The root node is handled below the loop.
2043 * We begin the loop with right_el and left_el pointing to the
2044 * leaf lists and work our way up.
2046 * NOTE: within this loop, left_el and right_el always refer
2047 * to the *child* lists.
2049 left_el = path_leaf_el(left_path);
2050 right_el = path_leaf_el(right_path);
2051 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2052 mlog(0, "Adjust records at index %u\n", i);
2055 * One nice property of knowing that all of these
2056 * nodes are below the root is that we only deal with
2057 * the leftmost right node record and the rightmost
2060 el = left_path->p_node[i].el;
2061 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2062 left_rec = &el->l_recs[idx];
2064 el = right_path->p_node[i].el;
2065 right_rec = &el->l_recs[0];
2067 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2070 ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2071 ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2074 * Setup our list pointers now so that the current
2075 * parents become children in the next iteration.
2077 left_el = left_path->p_node[i].el;
2078 right_el = right_path->p_node[i].el;
2082 * At the root node, adjust the two adjacent records which
2083 * begin our path to the leaves.
2086 el = left_path->p_node[subtree_index].el;
2087 left_el = left_path->p_node[subtree_index + 1].el;
2088 right_el = right_path->p_node[subtree_index + 1].el;
2090 ocfs2_adjust_root_records(el, left_el, right_el,
2091 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2093 root_bh = left_path->p_node[subtree_index].bh;
2095 ocfs2_journal_dirty(handle, root_bh);
2098 static int ocfs2_rotate_subtree_right(handle_t *handle,
2099 struct ocfs2_extent_tree *et,
2100 struct ocfs2_path *left_path,
2101 struct ocfs2_path *right_path,
2105 struct buffer_head *right_leaf_bh;
2106 struct buffer_head *left_leaf_bh = NULL;
2107 struct buffer_head *root_bh;
2108 struct ocfs2_extent_list *right_el, *left_el;
2109 struct ocfs2_extent_rec move_rec;
2111 left_leaf_bh = path_leaf_bh(left_path);
2112 left_el = path_leaf_el(left_path);
2114 if (left_el->l_next_free_rec != left_el->l_count) {
2115 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
2116 "Inode %llu has non-full interior leaf node %llu"
2118 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2119 (unsigned long long)left_leaf_bh->b_blocknr,
2120 le16_to_cpu(left_el->l_next_free_rec));
2125 * This extent block may already have an empty record, so we
2126 * return early if so.
2128 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2131 root_bh = left_path->p_node[subtree_index].bh;
2132 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2134 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2141 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2142 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2149 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2157 right_leaf_bh = path_leaf_bh(right_path);
2158 right_el = path_leaf_el(right_path);
2160 /* This is a code error, not a disk corruption. */
2161 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2162 "because rightmost leaf block %llu is empty\n",
2163 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2164 (unsigned long long)right_leaf_bh->b_blocknr);
2166 ocfs2_create_empty_extent(right_el);
2168 ocfs2_journal_dirty(handle, right_leaf_bh);
2170 /* Do the copy now. */
2171 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2172 move_rec = left_el->l_recs[i];
2173 right_el->l_recs[0] = move_rec;
2176 * Clear out the record we just copied and shift everything
2177 * over, leaving an empty extent in the left leaf.
2179 * We temporarily subtract from next_free_rec so that the
2180 * shift will lose the tail record (which is now defunct).
2182 le16_add_cpu(&left_el->l_next_free_rec, -1);
2183 ocfs2_shift_records_right(left_el);
2184 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2185 le16_add_cpu(&left_el->l_next_free_rec, 1);
2187 ocfs2_journal_dirty(handle, left_leaf_bh);
2189 ocfs2_complete_edge_insert(handle, left_path, right_path,
2197 * Given a full path, determine what cpos value would return us a path
2198 * containing the leaf immediately to the left of the current one.
2200 * Will return zero if the path passed in is already the leftmost path.
2202 int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2203 struct ocfs2_path *path, u32 *cpos)
2207 struct ocfs2_extent_list *el;
2209 BUG_ON(path->p_tree_depth == 0);
2213 blkno = path_leaf_bh(path)->b_blocknr;
2215 /* Start at the tree node just above the leaf and work our way up. */
2216 i = path->p_tree_depth - 1;
2218 el = path->p_node[i].el;
2221 * Find the extent record just before the one in our
2224 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2225 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2229 * We've determined that the
2230 * path specified is already
2231 * the leftmost one - return a
2237 * The leftmost record points to our
2238 * leaf - we need to travel up the
2244 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
2245 *cpos = *cpos + ocfs2_rec_clusters(el,
2246 &el->l_recs[j - 1]);
2253 * If we got here, we never found a valid node where
2254 * the tree indicated one should be.
2257 "Invalid extent tree at extent block %llu\n",
2258 (unsigned long long)blkno);
2263 blkno = path->p_node[i].bh->b_blocknr;
2272 * Extend the transaction by enough credits to complete the rotation,
2273 * and still leave at least the original number of credits allocated
2274 * to this transaction.
2276 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
2278 struct ocfs2_path *path)
2281 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
2283 if (handle->h_buffer_credits < credits)
2284 ret = ocfs2_extend_trans(handle,
2285 credits - handle->h_buffer_credits);
2291 * Trap the case where we're inserting into the theoretical range past
2292 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2293 * whose cpos is less than ours into the right leaf.
2295 * It's only necessary to look at the rightmost record of the left
2296 * leaf because the logic that calls us should ensure that the
2297 * theoretical ranges in the path components above the leaves are
2300 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2303 struct ocfs2_extent_list *left_el;
2304 struct ocfs2_extent_rec *rec;
2307 left_el = path_leaf_el(left_path);
2308 next_free = le16_to_cpu(left_el->l_next_free_rec);
2309 rec = &left_el->l_recs[next_free - 1];
2311 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2316 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2318 int next_free = le16_to_cpu(el->l_next_free_rec);
2320 struct ocfs2_extent_rec *rec;
2325 rec = &el->l_recs[0];
2326 if (ocfs2_is_empty_extent(rec)) {
2330 rec = &el->l_recs[1];
2333 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2334 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2340 * Rotate all the records in a btree right one record, starting at insert_cpos.
2342 * The path to the rightmost leaf should be passed in.
2344 * The array is assumed to be large enough to hold an entire path (tree depth).
2346 * Upon successful return from this function:
2348 * - The 'right_path' array will contain a path to the leaf block
2349 * whose range contains e_cpos.
2350 * - That leaf block will have a single empty extent in list index 0.
2351 * - In the case that the rotation requires a post-insert update,
2352 * *ret_left_path will contain a valid path which can be passed to
2353 * ocfs2_insert_path().
2355 static int ocfs2_rotate_tree_right(handle_t *handle,
2356 struct ocfs2_extent_tree *et,
2357 enum ocfs2_split_type split,
2359 struct ocfs2_path *right_path,
2360 struct ocfs2_path **ret_left_path)
2362 int ret, start, orig_credits = handle->h_buffer_credits;
2364 struct ocfs2_path *left_path = NULL;
2365 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2367 *ret_left_path = NULL;
2369 left_path = ocfs2_new_path_from_path(right_path);
2376 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2382 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2385 * What we want to do here is:
2387 * 1) Start with the rightmost path.
2389 * 2) Determine a path to the leaf block directly to the left
2392 * 3) Determine the 'subtree root' - the lowest level tree node
2393 * which contains a path to both leaves.
2395 * 4) Rotate the subtree.
2397 * 5) Find the next subtree by considering the left path to be
2398 * the new right path.
2400 * The check at the top of this while loop also accepts
2401 * insert_cpos == cpos because cpos is only a _theoretical_
2402 * value to get us the left path - insert_cpos might very well
2403 * be filling that hole.
2405 * Stop at a cpos of '0' because we either started at the
2406 * leftmost branch (i.e., a tree with one branch and a
2407 * rotation inside of it), or we've gone as far as we can in
2408 * rotating subtrees.
2410 while (cpos && insert_cpos <= cpos) {
2411 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2414 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
2420 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2421 path_leaf_bh(right_path),
2422 "Owner %llu: error during insert of %u "
2423 "(left path cpos %u) results in two identical "
2424 "paths ending at %llu\n",
2425 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2427 (unsigned long long)
2428 path_leaf_bh(left_path)->b_blocknr);
2430 if (split == SPLIT_NONE &&
2431 ocfs2_rotate_requires_path_adjustment(left_path,
2435 * We've rotated the tree as much as we
2436 * should. The rest is up to
2437 * ocfs2_insert_path() to complete, after the
2438 * record insertion. We indicate this
2439 * situation by returning the left path.
2441 * The reason we don't adjust the records here
2442 * before the record insert is that an error
2443 * later might break the rule where a parent
2444 * record e_cpos will reflect the actual
2445 * e_cpos of the 1st nonempty record of the
2448 *ret_left_path = left_path;
2452 start = ocfs2_find_subtree_root(et, left_path, right_path);
2454 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2456 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2457 right_path->p_tree_depth);
2459 ret = ocfs2_extend_rotate_transaction(handle, start,
2460 orig_credits, right_path);
2466 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
2473 if (split != SPLIT_NONE &&
2474 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2477 * A rotate moves the rightmost left leaf
2478 * record over to the leftmost right leaf
2479 * slot. If we're doing an extent split
2480 * instead of a real insert, then we have to
2481 * check that the extent to be split wasn't
2482 * just moved over. If it was, then we can
2483 * exit here, passing left_path back -
2484 * ocfs2_split_extent() is smart enough to
2485 * search both leaves.
2487 *ret_left_path = left_path;
2492 * There is no need to re-read the next right path
2493 * as we know that it'll be our current left
2494 * path. Optimize by copying values instead.
2496 ocfs2_mv_path(right_path, left_path);
2498 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2506 ocfs2_free_path(left_path);
2512 static int ocfs2_update_edge_lengths(handle_t *handle,
2513 struct ocfs2_extent_tree *et,
2514 int subtree_index, struct ocfs2_path *path)
2517 struct ocfs2_extent_rec *rec;
2518 struct ocfs2_extent_list *el;
2519 struct ocfs2_extent_block *eb;
2523 * In normal tree rotation process, we will never touch the
2524 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2525 * doesn't reserve the credits for them either.
2527 * But we do have a special case here which will update the rightmost
2528 * records for all the bh in the path.
2529 * So we have to allocate extra credits and access them.
2531 ret = ocfs2_extend_trans(handle, subtree_index);
2537 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
2543 /* Path should always be rightmost. */
2544 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2545 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2548 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2549 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2550 rec = &el->l_recs[idx];
2551 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2553 for (i = 0; i < path->p_tree_depth; i++) {
2554 el = path->p_node[i].el;
2555 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2556 rec = &el->l_recs[idx];
2558 rec->e_int_clusters = cpu_to_le32(range);
2559 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2561 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2567 static void ocfs2_unlink_path(handle_t *handle,
2568 struct ocfs2_extent_tree *et,
2569 struct ocfs2_cached_dealloc_ctxt *dealloc,
2570 struct ocfs2_path *path, int unlink_start)
2573 struct ocfs2_extent_block *eb;
2574 struct ocfs2_extent_list *el;
2575 struct buffer_head *bh;
2577 for(i = unlink_start; i < path_num_items(path); i++) {
2578 bh = path->p_node[i].bh;
2580 eb = (struct ocfs2_extent_block *)bh->b_data;
2582 * Not all nodes might have had their final count
2583 * decremented by the caller - handle this here.
2586 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2588 "Inode %llu, attempted to remove extent block "
2589 "%llu with %u records\n",
2590 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2591 (unsigned long long)le64_to_cpu(eb->h_blkno),
2592 le16_to_cpu(el->l_next_free_rec));
2594 ocfs2_journal_dirty(handle, bh);
2595 ocfs2_remove_from_cache(et->et_ci, bh);
2599 el->l_next_free_rec = 0;
2600 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2602 ocfs2_journal_dirty(handle, bh);
2604 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2608 ocfs2_remove_from_cache(et->et_ci, bh);
2612 static void ocfs2_unlink_subtree(handle_t *handle,
2613 struct ocfs2_extent_tree *et,
2614 struct ocfs2_path *left_path,
2615 struct ocfs2_path *right_path,
2617 struct ocfs2_cached_dealloc_ctxt *dealloc)
2620 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2621 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2622 struct ocfs2_extent_list *el;
2623 struct ocfs2_extent_block *eb;
2625 el = path_leaf_el(left_path);
2627 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2629 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2630 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2633 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2635 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2636 le16_add_cpu(&root_el->l_next_free_rec, -1);
2638 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2639 eb->h_next_leaf_blk = 0;
2641 ocfs2_journal_dirty(handle, root_bh);
2642 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2644 ocfs2_unlink_path(handle, et, dealloc, right_path,
2648 static int ocfs2_rotate_subtree_left(handle_t *handle,
2649 struct ocfs2_extent_tree *et,
2650 struct ocfs2_path *left_path,
2651 struct ocfs2_path *right_path,
2653 struct ocfs2_cached_dealloc_ctxt *dealloc,
2656 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2657 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2658 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2659 struct ocfs2_extent_block *eb;
2663 right_leaf_el = path_leaf_el(right_path);
2664 left_leaf_el = path_leaf_el(left_path);
2665 root_bh = left_path->p_node[subtree_index].bh;
2666 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2668 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2671 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2672 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2674 * It's legal for us to proceed if the right leaf is
2675 * the rightmost one and it has an empty extent. There
2676 * are two cases to handle - whether the leaf will be
2677 * empty after removal or not. If the leaf isn't empty
2678 * then just remove the empty extent up front. The
2679 * next block will handle empty leaves by flagging
2682 * Non rightmost leaves will throw -EAGAIN and the
2683 * caller can manually move the subtree and retry.
2686 if (eb->h_next_leaf_blk != 0ULL)
2689 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2690 ret = ocfs2_journal_access_eb(handle, et->et_ci,
2691 path_leaf_bh(right_path),
2692 OCFS2_JOURNAL_ACCESS_WRITE);
2698 ocfs2_remove_empty_extent(right_leaf_el);
2700 right_has_empty = 1;
2703 if (eb->h_next_leaf_blk == 0ULL &&
2704 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2706 * We have to update i_last_eb_blk during the meta
2709 ret = ocfs2_et_root_journal_access(handle, et,
2710 OCFS2_JOURNAL_ACCESS_WRITE);
2716 del_right_subtree = 1;
2720 * Getting here with an empty extent in the right path implies
2721 * that it's the rightmost path and will be deleted.
2723 BUG_ON(right_has_empty && !del_right_subtree);
2725 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2732 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2733 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2740 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2748 if (!right_has_empty) {
2750 * Only do this if we're moving a real
2751 * record. Otherwise, the action is delayed until
2752 * after removal of the right path in which case we
2753 * can do a simple shift to remove the empty extent.
2755 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2756 memset(&right_leaf_el->l_recs[0], 0,
2757 sizeof(struct ocfs2_extent_rec));
2759 if (eb->h_next_leaf_blk == 0ULL) {
2761 * Move recs over to get rid of empty extent, decrease
2762 * next_free. This is allowed to remove the last
2763 * extent in our leaf (setting l_next_free_rec to
2764 * zero) - the delete code below won't care.
2766 ocfs2_remove_empty_extent(right_leaf_el);
2769 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2770 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2772 if (del_right_subtree) {
2773 ocfs2_unlink_subtree(handle, et, left_path, right_path,
2774 subtree_index, dealloc);
2775 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
2782 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2783 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2786 * Removal of the extent in the left leaf was skipped
2787 * above so we could delete the right path
2790 if (right_has_empty)
2791 ocfs2_remove_empty_extent(left_leaf_el);
2793 ocfs2_journal_dirty(handle, et_root_bh);
2797 ocfs2_complete_edge_insert(handle, left_path, right_path,
2805 * Given a full path, determine what cpos value would return us a path
2806 * containing the leaf immediately to the right of the current one.
2808 * Will return zero if the path passed in is already the rightmost path.
2810 * This looks similar, but is subtly different to
2811 * ocfs2_find_cpos_for_left_leaf().
2813 int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2814 struct ocfs2_path *path, u32 *cpos)
2818 struct ocfs2_extent_list *el;
2822 if (path->p_tree_depth == 0)
2825 blkno = path_leaf_bh(path)->b_blocknr;
2827 /* Start at the tree node just above the leaf and work our way up. */
2828 i = path->p_tree_depth - 1;
2832 el = path->p_node[i].el;
2835 * Find the extent record just after the one in our
2838 next_free = le16_to_cpu(el->l_next_free_rec);
2839 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2840 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2841 if (j == (next_free - 1)) {
2844 * We've determined that the
2845 * path specified is already
2846 * the rightmost one - return a
2852 * The rightmost record points to our
2853 * leaf - we need to travel up the
2859 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2865 * If we got here, we never found a valid node where
2866 * the tree indicated one should be.
2869 "Invalid extent tree at extent block %llu\n",
2870 (unsigned long long)blkno);
2875 blkno = path->p_node[i].bh->b_blocknr;
2883 static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2884 struct ocfs2_extent_tree *et,
2885 struct ocfs2_path *path)
2888 struct buffer_head *bh = path_leaf_bh(path);
2889 struct ocfs2_extent_list *el = path_leaf_el(path);
2891 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2894 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
2895 path_num_items(path) - 1);
2901 ocfs2_remove_empty_extent(el);
2902 ocfs2_journal_dirty(handle, bh);
2908 static int __ocfs2_rotate_tree_left(handle_t *handle,
2909 struct ocfs2_extent_tree *et,
2911 struct ocfs2_path *path,
2912 struct ocfs2_cached_dealloc_ctxt *dealloc,
2913 struct ocfs2_path **empty_extent_path)
2915 int ret, subtree_root, deleted;
2917 struct ocfs2_path *left_path = NULL;
2918 struct ocfs2_path *right_path = NULL;
2919 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2921 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2923 *empty_extent_path = NULL;
2925 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
2931 left_path = ocfs2_new_path_from_path(path);
2938 ocfs2_cp_path(left_path, path);
2940 right_path = ocfs2_new_path_from_path(path);
2947 while (right_cpos) {
2948 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
2954 subtree_root = ocfs2_find_subtree_root(et, left_path,
2957 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2959 (unsigned long long)
2960 right_path->p_node[subtree_root].bh->b_blocknr,
2961 right_path->p_tree_depth);
2963 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2964 orig_credits, left_path);
2971 * Caller might still want to make changes to the
2972 * tree root, so re-add it to the journal here.
2974 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2981 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
2982 right_path, subtree_root,
2984 if (ret == -EAGAIN) {
2986 * The rotation has to temporarily stop due to
2987 * the right subtree having an empty
2988 * extent. Pass it back to the caller for a
2991 *empty_extent_path = right_path;
3001 * The subtree rotate might have removed records on
3002 * the rightmost edge. If so, then rotation is
3008 ocfs2_mv_path(left_path, right_path);
3010 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
3019 ocfs2_free_path(right_path);
3020 ocfs2_free_path(left_path);
3025 static int ocfs2_remove_rightmost_path(handle_t *handle,
3026 struct ocfs2_extent_tree *et,
3027 struct ocfs2_path *path,
3028 struct ocfs2_cached_dealloc_ctxt *dealloc)
3030 int ret, subtree_index;
3032 struct ocfs2_path *left_path = NULL;
3033 struct ocfs2_extent_block *eb;
3034 struct ocfs2_extent_list *el;
3037 ret = ocfs2_et_sanity_check(et);
3041 * There's two ways we handle this depending on
3042 * whether path is the only existing one.
3044 ret = ocfs2_extend_rotate_transaction(handle, 0,
3045 handle->h_buffer_credits,
3052 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
3058 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3067 * We have a path to the left of this one - it needs
3070 left_path = ocfs2_new_path_from_path(path);
3077 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
3083 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
3089 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
3091 ocfs2_unlink_subtree(handle, et, left_path, path,
3092 subtree_index, dealloc);
3093 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
3100 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
3101 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
3104 * 'path' is also the leftmost path which
3105 * means it must be the only one. This gets
3106 * handled differently because we want to
3107 * revert the root back to having extents
3110 ocfs2_unlink_path(handle, et, dealloc, path, 1);
3112 el = et->et_root_el;
3113 el->l_tree_depth = 0;
3114 el->l_next_free_rec = 0;
3115 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3117 ocfs2_et_set_last_eb_blk(et, 0);
3120 ocfs2_journal_dirty(handle, path_root_bh(path));
3123 ocfs2_free_path(left_path);
3128 * Left rotation of btree records.
3130 * In many ways, this is (unsurprisingly) the opposite of right
3131 * rotation. We start at some non-rightmost path containing an empty
3132 * extent in the leaf block. The code works its way to the rightmost
3133 * path by rotating records to the left in every subtree.
3135 * This is used by any code which reduces the number of extent records
3136 * in a leaf. After removal, an empty record should be placed in the
3137 * leftmost list position.
3139 * This won't handle a length update of the rightmost path records if
3140 * the rightmost tree leaf record is removed so the caller is
3141 * responsible for detecting and correcting that.
3143 static int ocfs2_rotate_tree_left(handle_t *handle,
3144 struct ocfs2_extent_tree *et,
3145 struct ocfs2_path *path,
3146 struct ocfs2_cached_dealloc_ctxt *dealloc)
3148 int ret, orig_credits = handle->h_buffer_credits;
3149 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3150 struct ocfs2_extent_block *eb;
3151 struct ocfs2_extent_list *el;
3153 el = path_leaf_el(path);
3154 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3157 if (path->p_tree_depth == 0) {
3158 rightmost_no_delete:
3160 * Inline extents. This is trivially handled, so do
3163 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
3170 * Handle rightmost branch now. There's several cases:
3171 * 1) simple rotation leaving records in there. That's trivial.
3172 * 2) rotation requiring a branch delete - there's no more
3173 * records left. Two cases of this:
3174 * a) There are branches to the left.
3175 * b) This is also the leftmost (the only) branch.
3177 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3178 * 2a) we need the left branch so that we can update it with the unlink
3179 * 2b) we need to bring the root back to inline extents.
3182 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3184 if (eb->h_next_leaf_blk == 0) {
3186 * This gets a bit tricky if we're going to delete the
3187 * rightmost path. Get the other cases out of the way
3190 if (le16_to_cpu(el->l_next_free_rec) > 1)
3191 goto rightmost_no_delete;
3193 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3195 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3196 "Owner %llu has empty extent block at %llu",
3197 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
3198 (unsigned long long)le64_to_cpu(eb->h_blkno));
3203 * XXX: The caller can not trust "path" any more after
3204 * this as it will have been deleted. What do we do?
3206 * In theory the rotate-for-merge code will never get
3207 * here because it'll always ask for a rotate in a
3211 ret = ocfs2_remove_rightmost_path(handle, et, path,
3219 * Now we can loop, remembering the path we get from -EAGAIN
3220 * and restarting from there.
3223 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3224 dealloc, &restart_path);
3225 if (ret && ret != -EAGAIN) {
3230 while (ret == -EAGAIN) {
3231 tmp_path = restart_path;
3232 restart_path = NULL;
3234 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
3237 if (ret && ret != -EAGAIN) {
3242 ocfs2_free_path(tmp_path);
3250 ocfs2_free_path(tmp_path);
3251 ocfs2_free_path(restart_path);
3255 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3258 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3261 if (rec->e_leaf_clusters == 0) {
3263 * We consumed all of the merged-from record. An empty
3264 * extent cannot exist anywhere but the 1st array
3265 * position, so move things over if the merged-from
3266 * record doesn't occupy that position.
3268 * This creates a new empty extent so the caller
3269 * should be smart enough to have removed any existing
3273 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3274 size = index * sizeof(struct ocfs2_extent_rec);
3275 memmove(&el->l_recs[1], &el->l_recs[0], size);
3279 * Always memset - the caller doesn't check whether it
3280 * created an empty extent, so there could be junk in
3283 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3287 static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
3288 struct ocfs2_path *left_path,
3289 struct ocfs2_path **ret_right_path)
3293 struct ocfs2_path *right_path = NULL;
3294 struct ocfs2_extent_list *left_el;
3296 *ret_right_path = NULL;
3298 /* This function shouldn't be called for non-trees. */
3299 BUG_ON(left_path->p_tree_depth == 0);
3301 left_el = path_leaf_el(left_path);
3302 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3304 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3305 left_path, &right_cpos);
3311 /* This function shouldn't be called for the rightmost leaf. */
3312 BUG_ON(right_cpos == 0);
3314 right_path = ocfs2_new_path_from_path(left_path);
3321 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
3327 *ret_right_path = right_path;
3330 ocfs2_free_path(right_path);
3335 * Remove split_rec clusters from the record at index and merge them
3336 * onto the beginning of the record "next" to it.
3337 * For index < l_count - 1, the next means the extent rec at index + 1.
3338 * For index == l_count - 1, the "next" means the 1st extent rec of the
3339 * next extent block.
3341 static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
3343 struct ocfs2_extent_tree *et,
3344 struct ocfs2_extent_rec *split_rec,
3347 int ret, next_free, i;
3348 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3349 struct ocfs2_extent_rec *left_rec;
3350 struct ocfs2_extent_rec *right_rec;
3351 struct ocfs2_extent_list *right_el;
3352 struct ocfs2_path *right_path = NULL;
3353 int subtree_index = 0;
3354 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3355 struct buffer_head *bh = path_leaf_bh(left_path);
3356 struct buffer_head *root_bh = NULL;
3358 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3359 left_rec = &el->l_recs[index];
3361 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3362 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3363 /* we meet with a cross extent block merge. */
3364 ret = ocfs2_get_right_path(et, left_path, &right_path);
3370 right_el = path_leaf_el(right_path);
3371 next_free = le16_to_cpu(right_el->l_next_free_rec);
3372 BUG_ON(next_free <= 0);
3373 right_rec = &right_el->l_recs[0];
3374 if (ocfs2_is_empty_extent(right_rec)) {
3375 BUG_ON(next_free <= 1);
3376 right_rec = &right_el->l_recs[1];
3379 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3380 le16_to_cpu(left_rec->e_leaf_clusters) !=
3381 le32_to_cpu(right_rec->e_cpos));
3383 subtree_index = ocfs2_find_subtree_root(et, left_path,
3386 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3387 handle->h_buffer_credits,
3394 root_bh = left_path->p_node[subtree_index].bh;
3395 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3397 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3404 for (i = subtree_index + 1;
3405 i < path_num_items(right_path); i++) {
3406 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3413 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3422 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3423 right_rec = &el->l_recs[index + 1];
3426 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
3427 path_num_items(left_path) - 1);
3433 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3435 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3436 le64_add_cpu(&right_rec->e_blkno,
3437 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3439 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3441 ocfs2_cleanup_merge(el, index);
3443 ocfs2_journal_dirty(handle, bh);
3445 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3446 ocfs2_complete_edge_insert(handle, left_path, right_path,
3451 ocfs2_free_path(right_path);
3455 static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
3456 struct ocfs2_path *right_path,
3457 struct ocfs2_path **ret_left_path)
3461 struct ocfs2_path *left_path = NULL;
3463 *ret_left_path = NULL;
3465 /* This function shouldn't be called for non-trees. */
3466 BUG_ON(right_path->p_tree_depth == 0);
3468 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3469 right_path, &left_cpos);
3475 /* This function shouldn't be called for the leftmost leaf. */
3476 BUG_ON(left_cpos == 0);
3478 left_path = ocfs2_new_path_from_path(right_path);
3485 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
3491 *ret_left_path = left_path;
3494 ocfs2_free_path(left_path);
3499 * Remove split_rec clusters from the record at index and merge them
3500 * onto the tail of the record "before" it.
3501 * For index > 0, the "before" means the extent rec at index - 1.
3503 * For index == 0, the "before" means the last record of the previous
3504 * extent block. And there is also a situation that we may need to
3505 * remove the rightmost leaf extent block in the right_path and change
3506 * the right path to indicate the new rightmost path.
3508 static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
3510 struct ocfs2_extent_tree *et,
3511 struct ocfs2_extent_rec *split_rec,
3512 struct ocfs2_cached_dealloc_ctxt *dealloc,
3515 int ret, i, subtree_index = 0, has_empty_extent = 0;
3516 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3517 struct ocfs2_extent_rec *left_rec;
3518 struct ocfs2_extent_rec *right_rec;
3519 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3520 struct buffer_head *bh = path_leaf_bh(right_path);
3521 struct buffer_head *root_bh = NULL;
3522 struct ocfs2_path *left_path = NULL;
3523 struct ocfs2_extent_list *left_el;
3527 right_rec = &el->l_recs[index];
3529 /* we meet with a cross extent block merge. */
3530 ret = ocfs2_get_left_path(et, right_path, &left_path);
3536 left_el = path_leaf_el(left_path);
3537 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3538 le16_to_cpu(left_el->l_count));
3540 left_rec = &left_el->l_recs[
3541 le16_to_cpu(left_el->l_next_free_rec) - 1];
3542 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3543 le16_to_cpu(left_rec->e_leaf_clusters) !=
3544 le32_to_cpu(split_rec->e_cpos));
3546 subtree_index = ocfs2_find_subtree_root(et, left_path,
3549 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3550 handle->h_buffer_credits,
3557 root_bh = left_path->p_node[subtree_index].bh;
3558 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3560 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3567 for (i = subtree_index + 1;
3568 i < path_num_items(right_path); i++) {
3569 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3576 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3584 left_rec = &el->l_recs[index - 1];
3585 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3586 has_empty_extent = 1;
3589 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3590 path_num_items(right_path) - 1);
3596 if (has_empty_extent && index == 1) {
3598 * The easy case - we can just plop the record right in.
3600 *left_rec = *split_rec;
3602 has_empty_extent = 0;
3604 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3606 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3607 le64_add_cpu(&right_rec->e_blkno,
3608 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3610 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3612 ocfs2_cleanup_merge(el, index);
3614 ocfs2_journal_dirty(handle, bh);
3616 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3619 * In the situation that the right_rec is empty and the extent
3620 * block is empty also, ocfs2_complete_edge_insert can't handle
3621 * it and we need to delete the right extent block.
3623 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3624 le16_to_cpu(el->l_next_free_rec) == 1) {
3626 ret = ocfs2_remove_rightmost_path(handle, et,
3634 /* Now the rightmost extent block has been deleted.
3635 * So we use the new rightmost path.
3637 ocfs2_mv_path(right_path, left_path);
3640 ocfs2_complete_edge_insert(handle, left_path,
3641 right_path, subtree_index);
3645 ocfs2_free_path(left_path);
3649 static int ocfs2_try_to_merge_extent(handle_t *handle,
3650 struct ocfs2_extent_tree *et,
3651 struct ocfs2_path *path,
3653 struct ocfs2_extent_rec *split_rec,
3654 struct ocfs2_cached_dealloc_ctxt *dealloc,
3655 struct ocfs2_merge_ctxt *ctxt)
3658 struct ocfs2_extent_list *el = path_leaf_el(path);
3659 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3661 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3663 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3665 * The merge code will need to create an empty
3666 * extent to take the place of the newly
3667 * emptied slot. Remove any pre-existing empty
3668 * extents - having more than one in a leaf is
3671 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3677 rec = &el->l_recs[split_index];
3680 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3682 * Left-right contig implies this.
3684 BUG_ON(!ctxt->c_split_covers_rec);
3687 * Since the leftright insert always covers the entire
3688 * extent, this call will delete the insert record
3689 * entirely, resulting in an empty extent record added to
3692 * Since the adding of an empty extent shifts
3693 * everything back to the right, there's no need to
3694 * update split_index here.
3696 * When the split_index is zero, we need to merge it to the
3697 * prevoius extent block. It is more efficient and easier
3698 * if we do merge_right first and merge_left later.
3700 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
3708 * We can only get this from logic error above.
3710 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3712 /* The merge left us with an empty extent, remove it. */
3713 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3719 rec = &el->l_recs[split_index];
3722 * Note that we don't pass split_rec here on purpose -
3723 * we've merged it into the rec already.
3725 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3726 dealloc, split_index);
3733 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3735 * Error from this last rotate is not critical, so
3736 * print but don't bubble it up.
3743 * Merge a record to the left or right.
3745 * 'contig_type' is relative to the existing record,
3746 * so for example, if we're "right contig", it's to
3747 * the record on the left (hence the left merge).
3749 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3750 ret = ocfs2_merge_rec_left(path, handle, et,
3758 ret = ocfs2_merge_rec_right(path, handle,
3767 if (ctxt->c_split_covers_rec) {
3769 * The merge may have left an empty extent in
3770 * our leaf. Try to rotate it away.
3772 ret = ocfs2_rotate_tree_left(handle, et, path,
3784 static void ocfs2_subtract_from_rec(struct super_block *sb,
3785 enum ocfs2_split_type split,
3786 struct ocfs2_extent_rec *rec,
3787 struct ocfs2_extent_rec *split_rec)
3791 len_blocks = ocfs2_clusters_to_blocks(sb,
3792 le16_to_cpu(split_rec->e_leaf_clusters));
3794 if (split == SPLIT_LEFT) {
3796 * Region is on the left edge of the existing
3799 le32_add_cpu(&rec->e_cpos,
3800 le16_to_cpu(split_rec->e_leaf_clusters));
3801 le64_add_cpu(&rec->e_blkno, len_blocks);
3802 le16_add_cpu(&rec->e_leaf_clusters,
3803 -le16_to_cpu(split_rec->e_leaf_clusters));
3806 * Region is on the right edge of the existing
3809 le16_add_cpu(&rec->e_leaf_clusters,
3810 -le16_to_cpu(split_rec->e_leaf_clusters));
3815 * Do the final bits of extent record insertion at the target leaf
3816 * list. If this leaf is part of an allocation tree, it is assumed
3817 * that the tree above has been prepared.
3819 static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3820 struct ocfs2_extent_rec *insert_rec,
3821 struct ocfs2_extent_list *el,
3822 struct ocfs2_insert_type *insert)
3824 int i = insert->ins_contig_index;
3826 struct ocfs2_extent_rec *rec;
3828 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3830 if (insert->ins_split != SPLIT_NONE) {
3831 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3833 rec = &el->l_recs[i];
3834 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3835 insert->ins_split, rec,
3841 * Contiguous insert - either left or right.
3843 if (insert->ins_contig != CONTIG_NONE) {
3844 rec = &el->l_recs[i];
3845 if (insert->ins_contig == CONTIG_LEFT) {
3846 rec->e_blkno = insert_rec->e_blkno;
3847 rec->e_cpos = insert_rec->e_cpos;
3849 le16_add_cpu(&rec->e_leaf_clusters,
3850 le16_to_cpu(insert_rec->e_leaf_clusters));
3855 * Handle insert into an empty leaf.
3857 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3858 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3859 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3860 el->l_recs[0] = *insert_rec;
3861 el->l_next_free_rec = cpu_to_le16(1);
3868 if (insert->ins_appending == APPEND_TAIL) {
3869 i = le16_to_cpu(el->l_next_free_rec) - 1;
3870 rec = &el->l_recs[i];
3871 range = le32_to_cpu(rec->e_cpos)
3872 + le16_to_cpu(rec->e_leaf_clusters);
3873 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3875 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3876 le16_to_cpu(el->l_count),
3877 "owner %llu, depth %u, count %u, next free %u, "
3878 "rec.cpos %u, rec.clusters %u, "
3879 "insert.cpos %u, insert.clusters %u\n",
3880 ocfs2_metadata_cache_owner(et->et_ci),
3881 le16_to_cpu(el->l_tree_depth),
3882 le16_to_cpu(el->l_count),
3883 le16_to_cpu(el->l_next_free_rec),
3884 le32_to_cpu(el->l_recs[i].e_cpos),
3885 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3886 le32_to_cpu(insert_rec->e_cpos),
3887 le16_to_cpu(insert_rec->e_leaf_clusters));
3889 el->l_recs[i] = *insert_rec;
3890 le16_add_cpu(&el->l_next_free_rec, 1);
3896 * Ok, we have to rotate.
3898 * At this point, it is safe to assume that inserting into an
3899 * empty leaf and appending to a leaf have both been handled
3902 * This leaf needs to have space, either by the empty 1st
3903 * extent record, or by virtue of an l_next_rec < l_count.
3905 ocfs2_rotate_leaf(el, insert_rec);
3908 static void ocfs2_adjust_rightmost_records(handle_t *handle,
3909 struct ocfs2_extent_tree *et,
3910 struct ocfs2_path *path,
3911 struct ocfs2_extent_rec *insert_rec)
3913 int ret, i, next_free;
3914 struct buffer_head *bh;
3915 struct ocfs2_extent_list *el;
3916 struct ocfs2_extent_rec *rec;
3919 * Update everything except the leaf block.
3921 for (i = 0; i < path->p_tree_depth; i++) {
3922 bh = path->p_node[i].bh;
3923 el = path->p_node[i].el;
3925 next_free = le16_to_cpu(el->l_next_free_rec);
3926 if (next_free == 0) {
3927 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3928 "Owner %llu has a bad extent list",
3929 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
3934 rec = &el->l_recs[next_free - 1];
3936 rec->e_int_clusters = insert_rec->e_cpos;
3937 le32_add_cpu(&rec->e_int_clusters,
3938 le16_to_cpu(insert_rec->e_leaf_clusters));
3939 le32_add_cpu(&rec->e_int_clusters,
3940 -le32_to_cpu(rec->e_cpos));
3942 ocfs2_journal_dirty(handle, bh);
3946 static int ocfs2_append_rec_to_path(handle_t *handle,
3947 struct ocfs2_extent_tree *et,
3948 struct ocfs2_extent_rec *insert_rec,
3949 struct ocfs2_path *right_path,
3950 struct ocfs2_path **ret_left_path)
3953 struct ocfs2_extent_list *el;
3954 struct ocfs2_path *left_path = NULL;
3956 *ret_left_path = NULL;
3959 * This shouldn't happen for non-trees. The extent rec cluster
3960 * count manipulation below only works for interior nodes.
3962 BUG_ON(right_path->p_tree_depth == 0);
3965 * If our appending insert is at the leftmost edge of a leaf,
3966 * then we might need to update the rightmost records of the
3969 el = path_leaf_el(right_path);
3970 next_free = le16_to_cpu(el->l_next_free_rec);
3971 if (next_free == 0 ||
3972 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3975 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3976 right_path, &left_cpos);
3982 mlog(0, "Append may need a left path update. cpos: %u, "
3983 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3987 * No need to worry if the append is already in the
3991 left_path = ocfs2_new_path_from_path(right_path);
3998 ret = ocfs2_find_path(et->et_ci, left_path,
4006 * ocfs2_insert_path() will pass the left_path to the
4012 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4018 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
4020 *ret_left_path = left_path;
4024 ocfs2_free_path(left_path);
4029 static void ocfs2_split_record(struct ocfs2_extent_tree *et,
4030 struct ocfs2_path *left_path,
4031 struct ocfs2_path *right_path,
4032 struct ocfs2_extent_rec *split_rec,
4033 enum ocfs2_split_type split)
4036 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4037 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4038 struct ocfs2_extent_rec *rec, *tmprec;
4040 right_el = path_leaf_el(right_path);
4042 left_el = path_leaf_el(left_path);
4045 insert_el = right_el;
4046 index = ocfs2_search_extent_list(el, cpos);
4048 if (index == 0 && left_path) {
4049 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4052 * This typically means that the record
4053 * started in the left path but moved to the
4054 * right as a result of rotation. We either
4055 * move the existing record to the left, or we
4056 * do the later insert there.
4058 * In this case, the left path should always
4059 * exist as the rotate code will have passed
4060 * it back for a post-insert update.
4063 if (split == SPLIT_LEFT) {
4065 * It's a left split. Since we know
4066 * that the rotate code gave us an
4067 * empty extent in the left path, we
4068 * can just do the insert there.
4070 insert_el = left_el;
4073 * Right split - we have to move the
4074 * existing record over to the left
4075 * leaf. The insert will be into the
4076 * newly created empty extent in the
4079 tmprec = &right_el->l_recs[index];
4080 ocfs2_rotate_leaf(left_el, tmprec);
4083 memset(tmprec, 0, sizeof(*tmprec));
4084 index = ocfs2_search_extent_list(left_el, cpos);
4085 BUG_ON(index == -1);
4090 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4092 * Left path is easy - we can just allow the insert to
4096 insert_el = left_el;
4097 index = ocfs2_search_extent_list(el, cpos);
4098 BUG_ON(index == -1);
4101 rec = &el->l_recs[index];
4102 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4103 split, rec, split_rec);
4104 ocfs2_rotate_leaf(insert_el, split_rec);
4108 * This function only does inserts on an allocation b-tree. For tree
4109 * depth = 0, ocfs2_insert_at_leaf() is called directly.
4111 * right_path is the path we want to do the actual insert
4112 * in. left_path should only be passed in if we need to update that
4113 * portion of the tree after an edge insert.
4115 static int ocfs2_insert_path(handle_t *handle,
4116 struct ocfs2_extent_tree *et,
4117 struct ocfs2_path *left_path,
4118 struct ocfs2_path *right_path,
4119 struct ocfs2_extent_rec *insert_rec,
4120 struct ocfs2_insert_type *insert)
4122 int ret, subtree_index;
4123 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
4127 * There's a chance that left_path got passed back to
4128 * us without being accounted for in the
4129 * journal. Extend our transaction here to be sure we
4130 * can change those blocks.
4132 ret = ocfs2_extend_trans(handle, left_path->p_tree_depth);
4138 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
4146 * Pass both paths to the journal. The majority of inserts
4147 * will be touching all components anyway.
4149 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4155 if (insert->ins_split != SPLIT_NONE) {
4157 * We could call ocfs2_insert_at_leaf() for some types
4158 * of splits, but it's easier to just let one separate
4159 * function sort it all out.
4161 ocfs2_split_record(et, left_path, right_path,
4162 insert_rec, insert->ins_split);
4165 * Split might have modified either leaf and we don't
4166 * have a guarantee that the later edge insert will
4167 * dirty this for us.
4170 ocfs2_journal_dirty(handle,
4171 path_leaf_bh(left_path));
4173 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4176 ocfs2_journal_dirty(handle, leaf_bh);
4180 * The rotate code has indicated that we need to fix
4181 * up portions of the tree after the insert.
4183 * XXX: Should we extend the transaction here?
4185 subtree_index = ocfs2_find_subtree_root(et, left_path,
4187 ocfs2_complete_edge_insert(handle, left_path, right_path,
4196 static int ocfs2_do_insert_extent(handle_t *handle,
4197 struct ocfs2_extent_tree *et,
4198 struct ocfs2_extent_rec *insert_rec,
4199 struct ocfs2_insert_type *type)
4201 int ret, rotate = 0;
4203 struct ocfs2_path *right_path = NULL;
4204 struct ocfs2_path *left_path = NULL;
4205 struct ocfs2_extent_list *el;
4207 el = et->et_root_el;
4209 ret = ocfs2_et_root_journal_access(handle, et,
4210 OCFS2_JOURNAL_ACCESS_WRITE);
4216 if (le16_to_cpu(el->l_tree_depth) == 0) {
4217 ocfs2_insert_at_leaf(et, insert_rec, el, type);
4218 goto out_update_clusters;
4221 right_path = ocfs2_new_path_from_et(et);
4229 * Determine the path to start with. Rotations need the
4230 * rightmost path, everything else can go directly to the
4233 cpos = le32_to_cpu(insert_rec->e_cpos);
4234 if (type->ins_appending == APPEND_NONE &&
4235 type->ins_contig == CONTIG_NONE) {
4240 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
4247 * Rotations and appends need special treatment - they modify
4248 * parts of the tree's above them.
4250 * Both might pass back a path immediate to the left of the
4251 * one being inserted to. This will be cause
4252 * ocfs2_insert_path() to modify the rightmost records of
4253 * left_path to account for an edge insert.
4255 * XXX: When modifying this code, keep in mind that an insert
4256 * can wind up skipping both of these two special cases...
4259 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
4260 le32_to_cpu(insert_rec->e_cpos),
4261 right_path, &left_path);
4268 * ocfs2_rotate_tree_right() might have extended the
4269 * transaction without re-journaling our tree root.
4271 ret = ocfs2_et_root_journal_access(handle, et,
4272 OCFS2_JOURNAL_ACCESS_WRITE);
4277 } else if (type->ins_appending == APPEND_TAIL
4278 && type->ins_contig != CONTIG_LEFT) {
4279 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
4280 right_path, &left_path);
4287 ret = ocfs2_insert_path(handle, et, left_path, right_path,
4294 out_update_clusters:
4295 if (type->ins_split == SPLIT_NONE)
4296 ocfs2_et_update_clusters(et,
4297 le16_to_cpu(insert_rec->e_leaf_clusters));
4299 ocfs2_journal_dirty(handle, et->et_root_bh);
4302 ocfs2_free_path(left_path);
4303 ocfs2_free_path(right_path);
4308 static enum ocfs2_contig_type
4309 ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4310 struct ocfs2_path *path,
4311 struct ocfs2_extent_list *el, int index,
4312 struct ocfs2_extent_rec *split_rec)
4315 enum ocfs2_contig_type ret = CONTIG_NONE;
4316 u32 left_cpos, right_cpos;
4317 struct ocfs2_extent_rec *rec = NULL;
4318 struct ocfs2_extent_list *new_el;
4319 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4320 struct buffer_head *bh;
4321 struct ocfs2_extent_block *eb;
4322 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
4325 rec = &el->l_recs[index - 1];
4326 } else if (path->p_tree_depth > 0) {
4327 status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
4331 if (left_cpos != 0) {
4332 left_path = ocfs2_new_path_from_path(path);
4336 status = ocfs2_find_path(et->et_ci, left_path,
4341 new_el = path_leaf_el(left_path);
4343 if (le16_to_cpu(new_el->l_next_free_rec) !=
4344 le16_to_cpu(new_el->l_count)) {
4345 bh = path_leaf_bh(left_path);
4346 eb = (struct ocfs2_extent_block *)bh->b_data;
4348 "Extent block #%llu has an "
4349 "invalid l_next_free_rec of "
4350 "%d. It should have "
4351 "matched the l_count of %d",
4352 (unsigned long long)le64_to_cpu(eb->h_blkno),
4353 le16_to_cpu(new_el->l_next_free_rec),
4354 le16_to_cpu(new_el->l_count));
4358 rec = &new_el->l_recs[
4359 le16_to_cpu(new_el->l_next_free_rec) - 1];
4364 * We're careful to check for an empty extent record here -
4365 * the merge code will know what to do if it sees one.
4368 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4369 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4372 ret = ocfs2_et_extent_contig(et, rec, split_rec);
4377 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4378 rec = &el->l_recs[index + 1];
4379 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4380 path->p_tree_depth > 0) {
4381 status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
4385 if (right_cpos == 0)
4388 right_path = ocfs2_new_path_from_path(path);
4392 status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
4396 new_el = path_leaf_el(right_path);
4397 rec = &new_el->l_recs[0];
4398 if (ocfs2_is_empty_extent(rec)) {
4399 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4400 bh = path_leaf_bh(right_path);
4401 eb = (struct ocfs2_extent_block *)bh->b_data;
4403 "Extent block #%llu has an "
4404 "invalid l_next_free_rec of %d",
4405 (unsigned long long)le64_to_cpu(eb->h_blkno),
4406 le16_to_cpu(new_el->l_next_free_rec));
4410 rec = &new_el->l_recs[1];
4415 enum ocfs2_contig_type contig_type;
4417 contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
4419 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4420 ret = CONTIG_LEFTRIGHT;
4421 else if (ret == CONTIG_NONE)
4427 ocfs2_free_path(left_path);
4429 ocfs2_free_path(right_path);
4434 static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
4435 struct ocfs2_insert_type *insert,
4436 struct ocfs2_extent_list *el,
4437 struct ocfs2_extent_rec *insert_rec)
4440 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4442 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4444 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4445 contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4447 if (contig_type != CONTIG_NONE) {
4448 insert->ins_contig_index = i;
4452 insert->ins_contig = contig_type;
4454 if (insert->ins_contig != CONTIG_NONE) {
4455 struct ocfs2_extent_rec *rec =
4456 &el->l_recs[insert->ins_contig_index];
4457 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4458 le16_to_cpu(insert_rec->e_leaf_clusters);
4461 * Caller might want us to limit the size of extents, don't
4462 * calculate contiguousness if we might exceed that limit.
4464 if (et->et_max_leaf_clusters &&
4465 (len > et->et_max_leaf_clusters))
4466 insert->ins_contig = CONTIG_NONE;
4471 * This should only be called against the righmost leaf extent list.
4473 * ocfs2_figure_appending_type() will figure out whether we'll have to
4474 * insert at the tail of the rightmost leaf.
4476 * This should also work against the root extent list for tree's with 0
4477 * depth. If we consider the root extent list to be the rightmost leaf node
4478 * then the logic here makes sense.
4480 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4481 struct ocfs2_extent_list *el,
4482 struct ocfs2_extent_rec *insert_rec)
4485 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4486 struct ocfs2_extent_rec *rec;
4488 insert->ins_appending = APPEND_NONE;
4490 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4492 if (!el->l_next_free_rec)
4493 goto set_tail_append;
4495 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4496 /* Were all records empty? */
4497 if (le16_to_cpu(el->l_next_free_rec) == 1)
4498 goto set_tail_append;
4501 i = le16_to_cpu(el->l_next_free_rec) - 1;
4502 rec = &el->l_recs[i];
4505 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4506 goto set_tail_append;
4511 insert->ins_appending = APPEND_TAIL;
4515 * Helper function called at the begining of an insert.
4517 * This computes a few things that are commonly used in the process of
4518 * inserting into the btree:
4519 * - Whether the new extent is contiguous with an existing one.
4520 * - The current tree depth.
4521 * - Whether the insert is an appending one.
4522 * - The total # of free records in the tree.
4524 * All of the information is stored on the ocfs2_insert_type
4527 static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
4528 struct buffer_head **last_eb_bh,
4529 struct ocfs2_extent_rec *insert_rec,
4531 struct ocfs2_insert_type *insert)
4534 struct ocfs2_extent_block *eb;
4535 struct ocfs2_extent_list *el;
4536 struct ocfs2_path *path = NULL;
4537 struct buffer_head *bh = NULL;
4539 insert->ins_split = SPLIT_NONE;
4541 el = et->et_root_el;
4542 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4544 if (el->l_tree_depth) {
4546 * If we have tree depth, we read in the
4547 * rightmost extent block ahead of time as
4548 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4549 * may want it later.
4551 ret = ocfs2_read_extent_block(et->et_ci,
4552 ocfs2_et_get_last_eb_blk(et),
4558 eb = (struct ocfs2_extent_block *) bh->b_data;
4563 * Unless we have a contiguous insert, we'll need to know if
4564 * there is room left in our allocation tree for another
4567 * XXX: This test is simplistic, we can search for empty
4568 * extent records too.
4570 *free_records = le16_to_cpu(el->l_count) -
4571 le16_to_cpu(el->l_next_free_rec);
4573 if (!insert->ins_tree_depth) {
4574 ocfs2_figure_contig_type(et, insert, el, insert_rec);
4575 ocfs2_figure_appending_type(insert, el, insert_rec);
4579 path = ocfs2_new_path_from_et(et);
4587 * In the case that we're inserting past what the tree
4588 * currently accounts for, ocfs2_find_path() will return for
4589 * us the rightmost tree path. This is accounted for below in
4590 * the appending code.
4592 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
4598 el = path_leaf_el(path);
4601 * Now that we have the path, there's two things we want to determine:
4602 * 1) Contiguousness (also set contig_index if this is so)
4604 * 2) Are we doing an append? We can trivially break this up
4605 * into two types of appends: simple record append, or a
4606 * rotate inside the tail leaf.
4608 ocfs2_figure_contig_type(et, insert, el, insert_rec);
4611 * The insert code isn't quite ready to deal with all cases of
4612 * left contiguousness. Specifically, if it's an insert into
4613 * the 1st record in a leaf, it will require the adjustment of
4614 * cluster count on the last record of the path directly to it's
4615 * left. For now, just catch that case and fool the layers
4616 * above us. This works just fine for tree_depth == 0, which
4617 * is why we allow that above.
4619 if (insert->ins_contig == CONTIG_LEFT &&
4620 insert->ins_contig_index == 0)
4621 insert->ins_contig = CONTIG_NONE;
4624 * Ok, so we can simply compare against last_eb to figure out
4625 * whether the path doesn't exist. This will only happen in
4626 * the case that we're doing a tail append, so maybe we can
4627 * take advantage of that information somehow.
4629 if (ocfs2_et_get_last_eb_blk(et) ==
4630 path_leaf_bh(path)->b_blocknr) {
4632 * Ok, ocfs2_find_path() returned us the rightmost
4633 * tree path. This might be an appending insert. There are
4635 * 1) We're doing a true append at the tail:
4636 * -This might even be off the end of the leaf
4637 * 2) We're "appending" by rotating in the tail
4639 ocfs2_figure_appending_type(insert, el, insert_rec);
4643 ocfs2_free_path(path);
4653 * Insert an extent into a btree.
4655 * The caller needs to update the owning btree's cluster count.
4657 int ocfs2_insert_extent(handle_t *handle,
4658 struct ocfs2_extent_tree *et,
4663 struct ocfs2_alloc_context *meta_ac)
4666 int uninitialized_var(free_records);
4667 struct buffer_head *last_eb_bh = NULL;
4668 struct ocfs2_insert_type insert = {0, };
4669 struct ocfs2_extent_rec rec;
4671 mlog(0, "add %u clusters at position %u to owner %llu\n",
4673 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
4675 memset(&rec, 0, sizeof(rec));
4676 rec.e_cpos = cpu_to_le32(cpos);
4677 rec.e_blkno = cpu_to_le64(start_blk);
4678 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4679 rec.e_flags = flags;
4680 status = ocfs2_et_insert_check(et, &rec);
4686 status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
4687 &free_records, &insert);
4693 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4694 "Insert.contig_index: %d, Insert.free_records: %d, "
4695 "Insert.tree_depth: %d\n",
4696 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4697 free_records, insert.ins_tree_depth);
4699 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4700 status = ocfs2_grow_tree(handle, et,
4701 &insert.ins_tree_depth, &last_eb_bh,
4709 /* Finally, we can add clusters. This might rotate the tree for us. */
4710 status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
4714 ocfs2_et_extent_map_insert(et, &rec);
4724 * Allcate and add clusters into the extent b-tree.
4725 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4726 * The extent b-tree's root is specified by et, and
4727 * it is not limited to the file storage. Any extent tree can use this
4728 * function if it implements the proper ocfs2_extent_tree.
4730 int ocfs2_add_clusters_in_btree(handle_t *handle,
4731 struct ocfs2_extent_tree *et,
4732 u32 *logical_offset,
4733 u32 clusters_to_add,
4735 struct ocfs2_alloc_context *data_ac,
4736 struct ocfs2_alloc_context *meta_ac,
4737 enum ocfs2_alloc_restarted *reason_ret)
4741 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4742 u32 bit_off, num_bits;
4745 struct ocfs2_super *osb =
4746 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
4748 BUG_ON(!clusters_to_add);
4751 flags = OCFS2_EXT_UNWRITTEN;
4753 free_extents = ocfs2_num_free_extents(osb, et);
4754 if (free_extents < 0) {
4755 status = free_extents;
4760 /* there are two cases which could cause us to EAGAIN in the
4761 * we-need-more-metadata case:
4762 * 1) we haven't reserved *any*
4763 * 2) we are so fragmented, we've needed to add metadata too
4765 if (!free_extents && !meta_ac) {
4766 mlog(0, "we haven't reserved any metadata!\n");
4768 reason = RESTART_META;
4770 } else if ((!free_extents)
4771 && (ocfs2_alloc_context_bits_left(meta_ac)
4772 < ocfs2_extend_meta_needed(et->et_root_el))) {
4773 mlog(0, "filesystem is really fragmented...\n");
4775 reason = RESTART_META;
4779 status = __ocfs2_claim_clusters(handle, data_ac, 1,
4780 clusters_to_add, &bit_off, &num_bits);
4782 if (status != -ENOSPC)
4787 BUG_ON(num_bits > clusters_to_add);
4789 /* reserve our write early -- insert_extent may update the tree root */
4790 status = ocfs2_et_root_journal_access(handle, et,
4791 OCFS2_JOURNAL_ACCESS_WRITE);
4797 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4798 mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
4800 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
4801 status = ocfs2_insert_extent(handle, et, *logical_offset, block,
4802 num_bits, flags, meta_ac);
4808 ocfs2_journal_dirty(handle, et->et_root_bh);
4810 clusters_to_add -= num_bits;
4811 *logical_offset += num_bits;
4813 if (clusters_to_add) {
4814 mlog(0, "need to alloc once more, wanted = %u\n",
4817 reason = RESTART_TRANS;
4823 *reason_ret = reason;
4827 static void ocfs2_make_right_split_rec(struct super_block *sb,
4828 struct ocfs2_extent_rec *split_rec,
4830 struct ocfs2_extent_rec *rec)
4832 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4833 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4835 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4837 split_rec->e_cpos = cpu_to_le32(cpos);
4838 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4840 split_rec->e_blkno = rec->e_blkno;
4841 le64_add_cpu(&split_rec->e_blkno,
4842 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4844 split_rec->e_flags = rec->e_flags;
4847 static int ocfs2_split_and_insert(handle_t *handle,
4848 struct ocfs2_extent_tree *et,
4849 struct ocfs2_path *path,
4850 struct buffer_head **last_eb_bh,
4852 struct ocfs2_extent_rec *orig_split_rec,
4853 struct ocfs2_alloc_context *meta_ac)
4856 unsigned int insert_range, rec_range, do_leftright = 0;
4857 struct ocfs2_extent_rec tmprec;
4858 struct ocfs2_extent_list *rightmost_el;
4859 struct ocfs2_extent_rec rec;
4860 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4861 struct ocfs2_insert_type insert;
4862 struct ocfs2_extent_block *eb;
4866 * Store a copy of the record on the stack - it might move
4867 * around as the tree is manipulated below.
4869 rec = path_leaf_el(path)->l_recs[split_index];
4871 rightmost_el = et->et_root_el;
4873 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4875 BUG_ON(!(*last_eb_bh));
4876 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4877 rightmost_el = &eb->h_list;
4880 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4881 le16_to_cpu(rightmost_el->l_count)) {
4882 ret = ocfs2_grow_tree(handle, et,
4883 &depth, last_eb_bh, meta_ac);
4890 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4891 insert.ins_appending = APPEND_NONE;
4892 insert.ins_contig = CONTIG_NONE;
4893 insert.ins_tree_depth = depth;
4895 insert_range = le32_to_cpu(split_rec.e_cpos) +
4896 le16_to_cpu(split_rec.e_leaf_clusters);
4897 rec_range = le32_to_cpu(rec.e_cpos) +
4898 le16_to_cpu(rec.e_leaf_clusters);
4900 if (split_rec.e_cpos == rec.e_cpos) {
4901 insert.ins_split = SPLIT_LEFT;
4902 } else if (insert_range == rec_range) {
4903 insert.ins_split = SPLIT_RIGHT;
4906 * Left/right split. We fake this as a right split
4907 * first and then make a second pass as a left split.
4909 insert.ins_split = SPLIT_RIGHT;
4911 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4912 &tmprec, insert_range, &rec);
4916 BUG_ON(do_leftright);
4920 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
4926 if (do_leftright == 1) {
4928 struct ocfs2_extent_list *el;
4931 split_rec = *orig_split_rec;
4933 ocfs2_reinit_path(path, 1);
4935 cpos = le32_to_cpu(split_rec.e_cpos);
4936 ret = ocfs2_find_path(et->et_ci, path, cpos);
4942 el = path_leaf_el(path);
4943 split_index = ocfs2_search_extent_list(el, cpos);
4951 static int ocfs2_replace_extent_rec(handle_t *handle,
4952 struct ocfs2_extent_tree *et,
4953 struct ocfs2_path *path,
4954 struct ocfs2_extent_list *el,
4956 struct ocfs2_extent_rec *split_rec)
4960 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
4961 path_num_items(path) - 1);
4967 el->l_recs[split_index] = *split_rec;
4969 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4975 * Split part or all of the extent record at split_index in the leaf
4976 * pointed to by path. Merge with the contiguous extent record if needed.
4978 * Care is taken to handle contiguousness so as to not grow the tree.
4980 * meta_ac is not strictly necessary - we only truly need it if growth
4981 * of the tree is required. All other cases will degrade into a less
4982 * optimal tree layout.
4984 * last_eb_bh should be the rightmost leaf block for any extent
4985 * btree. Since a split may grow the tree or a merge might shrink it,
4986 * the caller cannot trust the contents of that buffer after this call.
4988 * This code is optimized for readability - several passes might be
4989 * made over certain portions of the tree. All of those blocks will
4990 * have been brought into cache (and pinned via the journal), so the
4991 * extra overhead is not expressed in terms of disk reads.
4993 int ocfs2_split_extent(handle_t *handle,
4994 struct ocfs2_extent_tree *et,
4995 struct ocfs2_path *path,
4997 struct ocfs2_extent_rec *split_rec,
4998 struct ocfs2_alloc_context *meta_ac,
4999 struct ocfs2_cached_dealloc_ctxt *dealloc)
5002 struct ocfs2_extent_list *el = path_leaf_el(path);
5003 struct buffer_head *last_eb_bh = NULL;
5004 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5005 struct ocfs2_merge_ctxt ctxt;
5006 struct ocfs2_extent_list *rightmost_el;
5008 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5009 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5010 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5016 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
5021 * The core merge / split code wants to know how much room is
5022 * left in this allocation tree, so we pass the
5023 * rightmost extent list.
5025 if (path->p_tree_depth) {
5026 struct ocfs2_extent_block *eb;
5028 ret = ocfs2_read_extent_block(et->et_ci,
5029 ocfs2_et_get_last_eb_blk(et),
5036 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5037 rightmost_el = &eb->h_list;
5039 rightmost_el = path_root_el(path);
5041 if (rec->e_cpos == split_rec->e_cpos &&
5042 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5043 ctxt.c_split_covers_rec = 1;
5045 ctxt.c_split_covers_rec = 0;
5047 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5049 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5050 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5051 ctxt.c_split_covers_rec);
5053 if (ctxt.c_contig_type == CONTIG_NONE) {
5054 if (ctxt.c_split_covers_rec)
5055 ret = ocfs2_replace_extent_rec(handle, et, path, el,
5056 split_index, split_rec);
5058 ret = ocfs2_split_and_insert(handle, et, path,
5059 &last_eb_bh, split_index,
5060 split_rec, meta_ac);
5064 ret = ocfs2_try_to_merge_extent(handle, et, path,
5065 split_index, split_rec,
5077 * Change the flags of the already-existing extent at cpos for len clusters.
5079 * new_flags: the flags we want to set.
5080 * clear_flags: the flags we want to clear.
5081 * phys: the new physical offset we want this new extent starts from.
5083 * If the existing extent is larger than the request, initiate a
5084 * split. An attempt will be made at merging with adjacent extents.
5086 * The caller is responsible for passing down meta_ac if we'll need it.
5088 int ocfs2_change_extent_flag(handle_t *handle,
5089 struct ocfs2_extent_tree *et,
5090 u32 cpos, u32 len, u32 phys,
5091 struct ocfs2_alloc_context *meta_ac,
5092 struct ocfs2_cached_dealloc_ctxt *dealloc,
5093 int new_flags, int clear_flags)
5096 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5097 u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
5098 struct ocfs2_extent_rec split_rec;
5099 struct ocfs2_path *left_path = NULL;
5100 struct ocfs2_extent_list *el;
5101 struct ocfs2_extent_rec *rec;
5103 left_path = ocfs2_new_path_from_et(et);
5110 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
5115 el = path_leaf_el(left_path);
5117 index = ocfs2_search_extent_list(el, cpos);
5118 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5120 "Owner %llu has an extent at cpos %u which can no "
5121 "longer be found.\n",
5122 (unsigned long long)
5123 ocfs2_metadata_cache_owner(et->et_ci), cpos);
5129 rec = &el->l_recs[index];
5130 if (new_flags && (rec->e_flags & new_flags)) {
5131 mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
5132 "extent that already had them",
5133 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5138 if (clear_flags && !(rec->e_flags & clear_flags)) {
5139 mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
5140 "extent that didn't have them",
5141 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5146 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5147 split_rec.e_cpos = cpu_to_le32(cpos);
5148 split_rec.e_leaf_clusters = cpu_to_le16(len);
5149 split_rec.e_blkno = cpu_to_le64(start_blkno);
5150 split_rec.e_flags = rec->e_flags;
5152 split_rec.e_flags |= new_flags;
5154 split_rec.e_flags &= ~clear_flags;
5156 ret = ocfs2_split_extent(handle, et, left_path,
5157 index, &split_rec, meta_ac,
5163 ocfs2_free_path(left_path);
5169 * Mark the already-existing extent at cpos as written for len clusters.
5170 * This removes the unwritten extent flag.
5172 * If the existing extent is larger than the request, initiate a
5173 * split. An attempt will be made at merging with adjacent extents.
5175 * The caller is responsible for passing down meta_ac if we'll need it.
5177 int ocfs2_mark_extent_written(struct inode *inode,
5178 struct ocfs2_extent_tree *et,
5179 handle_t *handle, u32 cpos, u32 len, u32 phys,
5180 struct ocfs2_alloc_context *meta_ac,
5181 struct ocfs2_cached_dealloc_ctxt *dealloc)
5185 mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
5186 inode->i_ino, cpos, len, phys);
5188 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5189 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5190 "that are being written to, but the feature bit "
5191 "is not set in the super block.",
5192 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5198 * XXX: This should be fixed up so that we just re-insert the
5199 * next extent records.
5201 ocfs2_et_extent_map_truncate(et, 0);
5203 ret = ocfs2_change_extent_flag(handle, et, cpos,
5204 len, phys, meta_ac, dealloc,
5205 0, OCFS2_EXT_UNWRITTEN);
5213 static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5214 struct ocfs2_path *path,
5215 int index, u32 new_range,
5216 struct ocfs2_alloc_context *meta_ac)
5218 int ret, depth, credits;
5219 struct buffer_head *last_eb_bh = NULL;
5220 struct ocfs2_extent_block *eb;
5221 struct ocfs2_extent_list *rightmost_el, *el;
5222 struct ocfs2_extent_rec split_rec;
5223 struct ocfs2_extent_rec *rec;
5224 struct ocfs2_insert_type insert;
5227 * Setup the record to split before we grow the tree.
5229 el = path_leaf_el(path);
5230 rec = &el->l_recs[index];
5231 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5232 &split_rec, new_range, rec);
5234 depth = path->p_tree_depth;
5236 ret = ocfs2_read_extent_block(et->et_ci,
5237 ocfs2_et_get_last_eb_blk(et),
5244 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5245 rightmost_el = &eb->h_list;
5247 rightmost_el = path_leaf_el(path);
5249 credits = path->p_tree_depth +
5250 ocfs2_extend_meta_needed(et->et_root_el);
5251 ret = ocfs2_extend_trans(handle, credits);
5257 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5258 le16_to_cpu(rightmost_el->l_count)) {
5259 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
5267 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5268 insert.ins_appending = APPEND_NONE;
5269 insert.ins_contig = CONTIG_NONE;
5270 insert.ins_split = SPLIT_RIGHT;
5271 insert.ins_tree_depth = depth;
5273 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
5282 static int ocfs2_truncate_rec(handle_t *handle,
5283 struct ocfs2_extent_tree *et,
5284 struct ocfs2_path *path, int index,
5285 struct ocfs2_cached_dealloc_ctxt *dealloc,
5289 u32 left_cpos, rec_range, trunc_range;
5290 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5291 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5292 struct ocfs2_path *left_path = NULL;
5293 struct ocfs2_extent_list *el = path_leaf_el(path);
5294 struct ocfs2_extent_rec *rec;
5295 struct ocfs2_extent_block *eb;
5297 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5298 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5307 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5308 path->p_tree_depth) {
5310 * Check whether this is the rightmost tree record. If
5311 * we remove all of this record or part of its right
5312 * edge then an update of the record lengths above it
5315 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5316 if (eb->h_next_leaf_blk == 0)
5317 is_rightmost_tree_rec = 1;
5320 rec = &el->l_recs[index];
5321 if (index == 0 && path->p_tree_depth &&
5322 le32_to_cpu(rec->e_cpos) == cpos) {
5324 * Changing the leftmost offset (via partial or whole
5325 * record truncate) of an interior (or rightmost) path
5326 * means we have to update the subtree that is formed
5327 * by this leaf and the one to it's left.
5329 * There are two cases we can skip:
5330 * 1) Path is the leftmost one in our btree.
5331 * 2) The leaf is rightmost and will be empty after
5332 * we remove the extent record - the rotate code
5333 * knows how to update the newly formed edge.
5336 ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
5342 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5343 left_path = ocfs2_new_path_from_path(path);
5350 ret = ocfs2_find_path(et->et_ci, left_path,
5359 ret = ocfs2_extend_rotate_transaction(handle, 0,
5360 handle->h_buffer_credits,
5367 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
5373 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
5379 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5380 trunc_range = cpos + len;
5382 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5385 memset(rec, 0, sizeof(*rec));
5386 ocfs2_cleanup_merge(el, index);
5389 next_free = le16_to_cpu(el->l_next_free_rec);
5390 if (is_rightmost_tree_rec && next_free > 1) {
5392 * We skip the edge update if this path will
5393 * be deleted by the rotate code.
5395 rec = &el->l_recs[next_free - 1];
5396 ocfs2_adjust_rightmost_records(handle, et, path,
5399 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5400 /* Remove leftmost portion of the record. */
5401 le32_add_cpu(&rec->e_cpos, len);
5402 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5403 le16_add_cpu(&rec->e_leaf_clusters, -len);
5404 } else if (rec_range == trunc_range) {
5405 /* Remove rightmost portion of the record */
5406 le16_add_cpu(&rec->e_leaf_clusters, -len);
5407 if (is_rightmost_tree_rec)
5408 ocfs2_adjust_rightmost_records(handle, et, path, rec);
5410 /* Caller should have trapped this. */
5411 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5413 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5414 le32_to_cpu(rec->e_cpos),
5415 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5422 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
5423 ocfs2_complete_edge_insert(handle, left_path, path,
5427 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5429 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5436 ocfs2_free_path(left_path);
5440 int ocfs2_remove_extent(handle_t *handle,
5441 struct ocfs2_extent_tree *et,
5443 struct ocfs2_alloc_context *meta_ac,
5444 struct ocfs2_cached_dealloc_ctxt *dealloc)
5447 u32 rec_range, trunc_range;
5448 struct ocfs2_extent_rec *rec;
5449 struct ocfs2_extent_list *el;
5450 struct ocfs2_path *path = NULL;
5453 * XXX: Why are we truncating to 0 instead of wherever this
5456 ocfs2_et_extent_map_truncate(et, 0);
5458 path = ocfs2_new_path_from_et(et);
5465 ret = ocfs2_find_path(et->et_ci, path, cpos);
5471 el = path_leaf_el(path);
5472 index = ocfs2_search_extent_list(el, cpos);
5473 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5474 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5475 "Owner %llu has an extent at cpos %u which can no "
5476 "longer be found.\n",
5477 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5484 * We have 3 cases of extent removal:
5485 * 1) Range covers the entire extent rec
5486 * 2) Range begins or ends on one edge of the extent rec
5487 * 3) Range is in the middle of the extent rec (no shared edges)
5489 * For case 1 we remove the extent rec and left rotate to
5492 * For case 2 we just shrink the existing extent rec, with a
5493 * tree update if the shrinking edge is also the edge of an
5496 * For case 3 we do a right split to turn the extent rec into
5497 * something case 2 can handle.
5499 rec = &el->l_recs[index];
5500 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5501 trunc_range = cpos + len;
5503 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5505 mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
5506 "(cpos %u, len %u)\n",
5507 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5509 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5511 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5512 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5519 ret = ocfs2_split_tree(handle, et, path, index,
5520 trunc_range, meta_ac);
5527 * The split could have manipulated the tree enough to
5528 * move the record location, so we have to look for it again.
5530 ocfs2_reinit_path(path, 1);
5532 ret = ocfs2_find_path(et->et_ci, path, cpos);
5538 el = path_leaf_el(path);
5539 index = ocfs2_search_extent_list(el, cpos);
5540 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5541 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5542 "Owner %llu: split at cpos %u lost record.",
5543 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5550 * Double check our values here. If anything is fishy,
5551 * it's easier to catch it at the top level.
5553 rec = &el->l_recs[index];
5554 rec_range = le32_to_cpu(rec->e_cpos) +
5555 ocfs2_rec_clusters(el, rec);
5556 if (rec_range != trunc_range) {
5557 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5558 "Owner %llu: error after split at cpos %u"
5559 "trunc len %u, existing record is (%u,%u)",
5560 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5561 cpos, len, le32_to_cpu(rec->e_cpos),
5562 ocfs2_rec_clusters(el, rec));
5567 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5576 ocfs2_free_path(path);
5581 * ocfs2_reserve_blocks_for_rec_trunc() would look basically the
5582 * same as ocfs2_lock_alloctors(), except for it accepts a blocks
5583 * number to reserve some extra blocks, and it only handles meta
5586 * Currently, only ocfs2_remove_btree_range() uses it for truncating
5587 * and punching holes.
5589 static int ocfs2_reserve_blocks_for_rec_trunc(struct inode *inode,
5590 struct ocfs2_extent_tree *et,
5591 u32 extents_to_split,
5592 struct ocfs2_alloc_context **ac,
5595 int ret = 0, num_free_extents;
5596 unsigned int max_recs_needed = 2 * extents_to_split;
5597 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5601 num_free_extents = ocfs2_num_free_extents(osb, et);
5602 if (num_free_extents < 0) {
5603 ret = num_free_extents;
5608 if (!num_free_extents ||
5609 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed))
5610 extra_blocks += ocfs2_extend_meta_needed(et->et_root_el);
5613 ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, ac);
5624 ocfs2_free_alloc_context(*ac);
5632 int ocfs2_remove_btree_range(struct inode *inode,
5633 struct ocfs2_extent_tree *et,
5634 u32 cpos, u32 phys_cpos, u32 len, int flags,
5635 struct ocfs2_cached_dealloc_ctxt *dealloc,
5638 int ret, credits = 0, extra_blocks = 0;
5639 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5640 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5641 struct inode *tl_inode = osb->osb_tl_inode;
5643 struct ocfs2_alloc_context *meta_ac = NULL;
5644 struct ocfs2_refcount_tree *ref_tree = NULL;
5646 if ((flags & OCFS2_EXT_REFCOUNTED) && len) {
5647 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
5648 OCFS2_HAS_REFCOUNT_FL));
5650 ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
5657 ret = ocfs2_prepare_refcount_change_for_del(inode,
5669 ret = ocfs2_reserve_blocks_for_rec_trunc(inode, et, 1, &meta_ac,
5676 mutex_lock(&tl_inode->i_mutex);
5678 if (ocfs2_truncate_log_needs_flush(osb)) {
5679 ret = __ocfs2_flush_truncate_log(osb);
5686 handle = ocfs2_start_trans(osb,
5687 ocfs2_remove_extent_credits(osb->sb) + credits);
5688 if (IS_ERR(handle)) {
5689 ret = PTR_ERR(handle);
5694 ret = ocfs2_et_root_journal_access(handle, et,
5695 OCFS2_JOURNAL_ACCESS_WRITE);
5701 dquot_free_space_nodirty(inode,
5702 ocfs2_clusters_to_bytes(inode->i_sb, len));
5704 ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
5710 ocfs2_et_update_clusters(et, -len);
5712 ocfs2_journal_dirty(handle, et->et_root_bh);
5715 if (flags & OCFS2_EXT_REFCOUNTED)
5716 ret = ocfs2_decrease_refcount(inode, handle,
5717 ocfs2_blocks_to_clusters(osb->sb,
5722 ret = ocfs2_truncate_log_append(osb, handle,
5730 ocfs2_commit_trans(osb, handle);
5732 mutex_unlock(&tl_inode->i_mutex);
5735 ocfs2_free_alloc_context(meta_ac);
5738 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
5743 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5745 struct buffer_head *tl_bh = osb->osb_tl_bh;
5746 struct ocfs2_dinode *di;
5747 struct ocfs2_truncate_log *tl;
5749 di = (struct ocfs2_dinode *) tl_bh->b_data;
5750 tl = &di->id2.i_dealloc;
5752 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5753 "slot %d, invalid truncate log parameters: used = "
5754 "%u, count = %u\n", osb->slot_num,
5755 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5756 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5759 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5760 unsigned int new_start)
5762 unsigned int tail_index;
5763 unsigned int current_tail;
5765 /* No records, nothing to coalesce */
5766 if (!le16_to_cpu(tl->tl_used))
5769 tail_index = le16_to_cpu(tl->tl_used) - 1;
5770 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5771 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5773 return current_tail == new_start;
5776 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5779 unsigned int num_clusters)
5782 unsigned int start_cluster, tl_count;
5783 struct inode *tl_inode = osb->osb_tl_inode;
5784 struct buffer_head *tl_bh = osb->osb_tl_bh;
5785 struct ocfs2_dinode *di;
5786 struct ocfs2_truncate_log *tl;
5788 mlog(0, "start_blk = %llu, num_clusters = %u\n",
5789 (unsigned long long)start_blk, num_clusters);
5791 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5793 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5795 di = (struct ocfs2_dinode *) tl_bh->b_data;
5797 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5798 * by the underlying call to ocfs2_read_inode_block(), so any
5799 * corruption is a code bug */
5800 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5802 tl = &di->id2.i_dealloc;
5803 tl_count = le16_to_cpu(tl->tl_count);
5804 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5806 "Truncate record count on #%llu invalid "
5807 "wanted %u, actual %u\n",
5808 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5809 ocfs2_truncate_recs_per_inode(osb->sb),
5810 le16_to_cpu(tl->tl_count));
5812 /* Caller should have known to flush before calling us. */
5813 index = le16_to_cpu(tl->tl_used);
5814 if (index >= tl_count) {
5820 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5821 OCFS2_JOURNAL_ACCESS_WRITE);
5827 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5828 "%llu (index = %d)\n", num_clusters, start_cluster,
5829 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5831 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5833 * Move index back to the record we are coalescing with.
5834 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5838 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5839 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5840 index, le32_to_cpu(tl->tl_recs[index].t_start),
5843 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5844 tl->tl_used = cpu_to_le16(index + 1);
5846 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5848 ocfs2_journal_dirty(handle, tl_bh);
5850 osb->truncated_clusters += num_clusters;
5856 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5858 struct inode *data_alloc_inode,
5859 struct buffer_head *data_alloc_bh)
5863 unsigned int num_clusters;
5865 struct ocfs2_truncate_rec rec;
5866 struct ocfs2_dinode *di;
5867 struct ocfs2_truncate_log *tl;
5868 struct inode *tl_inode = osb->osb_tl_inode;
5869 struct buffer_head *tl_bh = osb->osb_tl_bh;
5871 di = (struct ocfs2_dinode *) tl_bh->b_data;
5872 tl = &di->id2.i_dealloc;
5873 i = le16_to_cpu(tl->tl_used) - 1;
5875 /* Caller has given us at least enough credits to
5876 * update the truncate log dinode */
5877 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5878 OCFS2_JOURNAL_ACCESS_WRITE);
5884 tl->tl_used = cpu_to_le16(i);
5886 ocfs2_journal_dirty(handle, tl_bh);
5888 /* TODO: Perhaps we can calculate the bulk of the
5889 * credits up front rather than extending like
5891 status = ocfs2_extend_trans(handle,
5892 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5898 rec = tl->tl_recs[i];
5899 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5900 le32_to_cpu(rec.t_start));
5901 num_clusters = le32_to_cpu(rec.t_clusters);
5903 /* if start_blk is not set, we ignore the record as
5906 mlog(0, "free record %d, start = %u, clusters = %u\n",
5907 i, le32_to_cpu(rec.t_start), num_clusters);
5909 status = ocfs2_free_clusters(handle, data_alloc_inode,
5910 data_alloc_bh, start_blk,
5920 osb->truncated_clusters = 0;
5927 /* Expects you to already be holding tl_inode->i_mutex */
5928 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5931 unsigned int num_to_flush;
5933 struct inode *tl_inode = osb->osb_tl_inode;
5934 struct inode *data_alloc_inode = NULL;
5935 struct buffer_head *tl_bh = osb->osb_tl_bh;
5936 struct buffer_head *data_alloc_bh = NULL;
5937 struct ocfs2_dinode *di;
5938 struct ocfs2_truncate_log *tl;
5940 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5942 di = (struct ocfs2_dinode *) tl_bh->b_data;
5944 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5945 * by the underlying call to ocfs2_read_inode_block(), so any
5946 * corruption is a code bug */
5947 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5949 tl = &di->id2.i_dealloc;
5950 num_to_flush = le16_to_cpu(tl->tl_used);
5951 mlog(0, "Flush %u records from truncate log #%llu\n",
5952 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5953 if (!num_to_flush) {
5958 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5959 GLOBAL_BITMAP_SYSTEM_INODE,
5960 OCFS2_INVALID_SLOT);
5961 if (!data_alloc_inode) {
5963 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5967 mutex_lock(&data_alloc_inode->i_mutex);
5969 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5975 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5976 if (IS_ERR(handle)) {
5977 status = PTR_ERR(handle);
5982 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5987 ocfs2_commit_trans(osb, handle);
5990 brelse(data_alloc_bh);
5991 ocfs2_inode_unlock(data_alloc_inode, 1);
5994 mutex_unlock(&data_alloc_inode->i_mutex);
5995 iput(data_alloc_inode);
6002 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
6005 struct inode *tl_inode = osb->osb_tl_inode;
6007 mutex_lock(&tl_inode->i_mutex);
6008 status = __ocfs2_flush_truncate_log(osb);
6009 mutex_unlock(&tl_inode->i_mutex);
6014 static void ocfs2_truncate_log_worker(struct work_struct *work)
6017 struct ocfs2_super *osb =
6018 container_of(work, struct ocfs2_super,
6019 osb_truncate_log_wq.work);
6021 status = ocfs2_flush_truncate_log(osb);
6025 ocfs2_init_steal_slots(osb);
6030 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
6031 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
6034 if (osb->osb_tl_inode) {
6035 /* We want to push off log flushes while truncates are
6038 cancel_delayed_work(&osb->osb_truncate_log_wq);
6040 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
6041 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
6045 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
6047 struct inode **tl_inode,
6048 struct buffer_head **tl_bh)
6051 struct inode *inode = NULL;
6052 struct buffer_head *bh = NULL;
6054 inode = ocfs2_get_system_file_inode(osb,
6055 TRUNCATE_LOG_SYSTEM_INODE,
6059 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
6063 status = ocfs2_read_inode_block(inode, &bh);
6077 /* called during the 1st stage of node recovery. we stamp a clean
6078 * truncate log and pass back a copy for processing later. if the
6079 * truncate log does not require processing, a *tl_copy is set to
6081 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6083 struct ocfs2_dinode **tl_copy)
6086 struct inode *tl_inode = NULL;
6087 struct buffer_head *tl_bh = NULL;
6088 struct ocfs2_dinode *di;
6089 struct ocfs2_truncate_log *tl;
6093 mlog(0, "recover truncate log from slot %d\n", slot_num);
6095 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6101 di = (struct ocfs2_dinode *) tl_bh->b_data;
6103 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6104 * validated by the underlying call to ocfs2_read_inode_block(),
6105 * so any corruption is a code bug */
6106 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6108 tl = &di->id2.i_dealloc;
6109 if (le16_to_cpu(tl->tl_used)) {
6110 mlog(0, "We'll have %u logs to recover\n",
6111 le16_to_cpu(tl->tl_used));
6113 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6120 /* Assuming the write-out below goes well, this copy
6121 * will be passed back to recovery for processing. */
6122 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6124 /* All we need to do to clear the truncate log is set
6128 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
6129 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
6141 if (status < 0 && (*tl_copy)) {
6150 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6151 struct ocfs2_dinode *tl_copy)
6155 unsigned int clusters, num_recs, start_cluster;
6158 struct inode *tl_inode = osb->osb_tl_inode;
6159 struct ocfs2_truncate_log *tl;
6161 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6162 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6166 tl = &tl_copy->id2.i_dealloc;
6167 num_recs = le16_to_cpu(tl->tl_used);
6168 mlog(0, "cleanup %u records from %llu\n", num_recs,
6169 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
6171 mutex_lock(&tl_inode->i_mutex);
6172 for(i = 0; i < num_recs; i++) {
6173 if (ocfs2_truncate_log_needs_flush(osb)) {
6174 status = __ocfs2_flush_truncate_log(osb);
6181 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6182 if (IS_ERR(handle)) {
6183 status = PTR_ERR(handle);
6188 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6189 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6190 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6192 status = ocfs2_truncate_log_append(osb, handle,
6193 start_blk, clusters);
6194 ocfs2_commit_trans(osb, handle);
6202 mutex_unlock(&tl_inode->i_mutex);
6208 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6211 struct inode *tl_inode = osb->osb_tl_inode;
6214 cancel_delayed_work(&osb->osb_truncate_log_wq);
6215 flush_workqueue(ocfs2_wq);
6217 status = ocfs2_flush_truncate_log(osb);
6221 brelse(osb->osb_tl_bh);
6222 iput(osb->osb_tl_inode);
6228 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6231 struct inode *tl_inode = NULL;
6232 struct buffer_head *tl_bh = NULL;
6234 status = ocfs2_get_truncate_log_info(osb,
6241 /* ocfs2_truncate_log_shutdown keys on the existence of
6242 * osb->osb_tl_inode so we don't set any of the osb variables
6243 * until we're sure all is well. */
6244 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6245 ocfs2_truncate_log_worker);
6246 osb->osb_tl_bh = tl_bh;
6247 osb->osb_tl_inode = tl_inode;
6254 * Delayed de-allocation of suballocator blocks.
6256 * Some sets of block de-allocations might involve multiple suballocator inodes.
6258 * The locking for this can get extremely complicated, especially when
6259 * the suballocator inodes to delete from aren't known until deep
6260 * within an unrelated codepath.
6262 * ocfs2_extent_block structures are a good example of this - an inode
6263 * btree could have been grown by any number of nodes each allocating
6264 * out of their own suballoc inode.
6266 * These structures allow the delay of block de-allocation until a
6267 * later time, when locking of multiple cluster inodes won't cause
6272 * Describe a single bit freed from a suballocator. For the block
6273 * suballocators, it represents one block. For the global cluster
6274 * allocator, it represents some clusters and free_bit indicates
6277 struct ocfs2_cached_block_free {
6278 struct ocfs2_cached_block_free *free_next;
6281 unsigned int free_bit;
6284 struct ocfs2_per_slot_free_list {
6285 struct ocfs2_per_slot_free_list *f_next_suballocator;
6288 struct ocfs2_cached_block_free *f_first;
6291 static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6294 struct ocfs2_cached_block_free *head)
6299 struct inode *inode;
6300 struct buffer_head *di_bh = NULL;
6301 struct ocfs2_cached_block_free *tmp;
6303 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6310 mutex_lock(&inode->i_mutex);
6312 ret = ocfs2_inode_lock(inode, &di_bh, 1);
6318 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6319 if (IS_ERR(handle)) {
6320 ret = PTR_ERR(handle);
6327 bg_blkno = head->free_bg;
6329 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6331 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6332 head->free_bit, (unsigned long long)head->free_blk);
6334 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6335 head->free_bit, bg_blkno, 1);
6341 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6348 head = head->free_next;
6353 ocfs2_commit_trans(osb, handle);
6356 ocfs2_inode_unlock(inode, 1);
6359 mutex_unlock(&inode->i_mutex);
6363 /* Premature exit may have left some dangling items. */
6365 head = head->free_next;
6372 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6373 u64 blkno, unsigned int bit)
6376 struct ocfs2_cached_block_free *item;
6378 item = kzalloc(sizeof(*item), GFP_NOFS);
6385 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6386 bit, (unsigned long long)blkno);
6388 item->free_blk = blkno;
6389 item->free_bit = bit;
6390 item->free_next = ctxt->c_global_allocator;
6392 ctxt->c_global_allocator = item;
6396 static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6397 struct ocfs2_cached_block_free *head)
6399 struct ocfs2_cached_block_free *tmp;
6400 struct inode *tl_inode = osb->osb_tl_inode;
6404 mutex_lock(&tl_inode->i_mutex);
6407 if (ocfs2_truncate_log_needs_flush(osb)) {
6408 ret = __ocfs2_flush_truncate_log(osb);
6415 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6416 if (IS_ERR(handle)) {
6417 ret = PTR_ERR(handle);
6422 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6425 ocfs2_commit_trans(osb, handle);
6427 head = head->free_next;
6436 mutex_unlock(&tl_inode->i_mutex);
6439 /* Premature exit may have left some dangling items. */
6441 head = head->free_next;
6448 int ocfs2_run_deallocs(struct ocfs2_super *osb,
6449 struct ocfs2_cached_dealloc_ctxt *ctxt)
6452 struct ocfs2_per_slot_free_list *fl;
6457 while (ctxt->c_first_suballocator) {
6458 fl = ctxt->c_first_suballocator;
6461 mlog(0, "Free items: (type %u, slot %d)\n",
6462 fl->f_inode_type, fl->f_slot);
6463 ret2 = ocfs2_free_cached_blocks(osb,
6473 ctxt->c_first_suballocator = fl->f_next_suballocator;
6477 if (ctxt->c_global_allocator) {
6478 ret2 = ocfs2_free_cached_clusters(osb,
6479 ctxt->c_global_allocator);
6485 ctxt->c_global_allocator = NULL;
6491 static struct ocfs2_per_slot_free_list *
6492 ocfs2_find_per_slot_free_list(int type,
6494 struct ocfs2_cached_dealloc_ctxt *ctxt)
6496 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6499 if (fl->f_inode_type == type && fl->f_slot == slot)
6502 fl = fl->f_next_suballocator;
6505 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6507 fl->f_inode_type = type;
6510 fl->f_next_suballocator = ctxt->c_first_suballocator;
6512 ctxt->c_first_suballocator = fl;
6517 int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6518 int type, int slot, u64 suballoc,
6519 u64 blkno, unsigned int bit)
6522 struct ocfs2_per_slot_free_list *fl;
6523 struct ocfs2_cached_block_free *item;
6525 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6532 item = kzalloc(sizeof(*item), GFP_NOFS);
6539 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6540 type, slot, bit, (unsigned long long)blkno);
6542 item->free_bg = suballoc;
6543 item->free_blk = blkno;
6544 item->free_bit = bit;
6545 item->free_next = fl->f_first;
6554 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6555 struct ocfs2_extent_block *eb)
6557 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6558 le16_to_cpu(eb->h_suballoc_slot),
6559 le64_to_cpu(eb->h_suballoc_loc),
6560 le64_to_cpu(eb->h_blkno),
6561 le16_to_cpu(eb->h_suballoc_bit));
6564 static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
6566 set_buffer_uptodate(bh);
6567 mark_buffer_dirty(bh);
6571 void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6572 unsigned int from, unsigned int to,
6573 struct page *page, int zero, u64 *phys)
6575 int ret, partial = 0;
6577 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6582 zero_user_segment(page, from, to);
6585 * Need to set the buffers we zero'd into uptodate
6586 * here if they aren't - ocfs2_map_page_blocks()
6587 * might've skipped some
6589 ret = walk_page_buffers(handle, page_buffers(page),
6594 else if (ocfs2_should_order_data(inode)) {
6595 ret = ocfs2_jbd2_file_inode(handle, inode);
6601 SetPageUptodate(page);
6603 flush_dcache_page(page);
6606 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6607 loff_t end, struct page **pages,
6608 int numpages, u64 phys, handle_t *handle)
6612 unsigned int from, to = PAGE_CACHE_SIZE;
6613 struct super_block *sb = inode->i_sb;
6615 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6620 to = PAGE_CACHE_SIZE;
6621 for(i = 0; i < numpages; i++) {
6624 from = start & (PAGE_CACHE_SIZE - 1);
6625 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6626 to = end & (PAGE_CACHE_SIZE - 1);
6628 BUG_ON(from > PAGE_CACHE_SIZE);
6629 BUG_ON(to > PAGE_CACHE_SIZE);
6631 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6634 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6638 ocfs2_unlock_and_free_pages(pages, numpages);
6641 int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
6642 struct page **pages, int *num)
6644 int numpages, ret = 0;
6645 struct address_space *mapping = inode->i_mapping;
6646 unsigned long index;
6647 loff_t last_page_bytes;
6649 BUG_ON(start > end);
6652 last_page_bytes = PAGE_ALIGN(end);
6653 index = start >> PAGE_CACHE_SHIFT;
6655 pages[numpages] = find_or_create_page(mapping, index, GFP_NOFS);
6656 if (!pages[numpages]) {
6664 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6669 ocfs2_unlock_and_free_pages(pages, numpages);
6678 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6679 struct page **pages, int *num)
6681 struct super_block *sb = inode->i_sb;
6683 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6684 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6686 return ocfs2_grab_pages(inode, start, end, pages, num);
6690 * Zero the area past i_size but still within an allocated
6691 * cluster. This avoids exposing nonzero data on subsequent file
6694 * We need to call this before i_size is updated on the inode because
6695 * otherwise block_write_full_page() will skip writeout of pages past
6696 * i_size. The new_i_size parameter is passed for this reason.
6698 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6699 u64 range_start, u64 range_end)
6701 int ret = 0, numpages;
6702 struct page **pages = NULL;
6704 unsigned int ext_flags;
6705 struct super_block *sb = inode->i_sb;
6708 * File systems which don't support sparse files zero on every
6711 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6714 pages = kcalloc(ocfs2_pages_per_cluster(sb),
6715 sizeof(struct page *), GFP_NOFS);
6716 if (pages == NULL) {
6722 if (range_start == range_end)
6725 ret = ocfs2_extent_map_get_blocks(inode,
6726 range_start >> sb->s_blocksize_bits,
6727 &phys, NULL, &ext_flags);
6734 * Tail is a hole, or is marked unwritten. In either case, we
6735 * can count on read and write to return/push zero's.
6737 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
6740 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6747 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6748 numpages, phys, handle);
6751 * Initiate writeout of the pages we zero'd here. We don't
6752 * wait on them - the truncate_inode_pages() call later will
6755 ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
6767 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6768 struct ocfs2_dinode *di)
6770 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6771 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
6773 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6774 memset(&di->id2, 0, blocksize -
6775 offsetof(struct ocfs2_dinode, id2) -
6778 memset(&di->id2, 0, blocksize -
6779 offsetof(struct ocfs2_dinode, id2));
6782 void ocfs2_dinode_new_extent_list(struct inode *inode,
6783 struct ocfs2_dinode *di)
6785 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6786 di->id2.i_list.l_tree_depth = 0;
6787 di->id2.i_list.l_next_free_rec = 0;
6788 di->id2.i_list.l_count = cpu_to_le16(
6789 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
6792 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6794 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6795 struct ocfs2_inline_data *idata = &di->id2.i_data;
6797 spin_lock(&oi->ip_lock);
6798 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6799 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6800 spin_unlock(&oi->ip_lock);
6803 * We clear the entire i_data structure here so that all
6804 * fields can be properly initialized.
6806 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6808 idata->id_count = cpu_to_le16(
6809 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
6812 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6813 struct buffer_head *di_bh)
6815 int ret, i, has_data, num_pages = 0;
6817 u64 uninitialized_var(block);
6818 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6819 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6820 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6821 struct ocfs2_alloc_context *data_ac = NULL;
6822 struct page **pages = NULL;
6823 loff_t end = osb->s_clustersize;
6824 struct ocfs2_extent_tree et;
6827 has_data = i_size_read(inode) ? 1 : 0;
6830 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6831 sizeof(struct page *), GFP_NOFS);
6832 if (pages == NULL) {
6838 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6845 handle = ocfs2_start_trans(osb,
6846 ocfs2_inline_to_extents_credits(osb->sb));
6847 if (IS_ERR(handle)) {
6848 ret = PTR_ERR(handle);
6853 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
6854 OCFS2_JOURNAL_ACCESS_WRITE);
6862 unsigned int page_end;
6865 ret = dquot_alloc_space_nodirty(inode,
6866 ocfs2_clusters_to_bytes(osb->sb, 1));
6871 data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
6873 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
6881 * Save two copies, one for insert, and one that can
6882 * be changed by ocfs2_map_and_dirty_page() below.
6884 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6887 * Non sparse file systems zero on extend, so no need
6890 if (!ocfs2_sparse_alloc(osb) &&
6891 PAGE_CACHE_SIZE < osb->s_clustersize)
6892 end = PAGE_CACHE_SIZE;
6894 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6901 * This should populate the 1st page for us and mark
6904 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6910 page_end = PAGE_CACHE_SIZE;
6911 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6912 page_end = osb->s_clustersize;
6914 for (i = 0; i < num_pages; i++)
6915 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6916 pages[i], i > 0, &phys);
6919 spin_lock(&oi->ip_lock);
6920 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6921 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6922 spin_unlock(&oi->ip_lock);
6924 ocfs2_dinode_new_extent_list(inode, di);
6926 ocfs2_journal_dirty(handle, di_bh);
6930 * An error at this point should be extremely rare. If
6931 * this proves to be false, we could always re-build
6932 * the in-inode data from our pages.
6934 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
6935 ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
6941 inode->i_blocks = ocfs2_inode_sector_count(inode);
6945 if (ret < 0 && did_quota)
6946 dquot_free_space_nodirty(inode,
6947 ocfs2_clusters_to_bytes(osb->sb, 1));
6949 ocfs2_commit_trans(osb, handle);
6953 ocfs2_free_alloc_context(data_ac);
6957 ocfs2_unlock_and_free_pages(pages, num_pages);
6965 * It is expected, that by the time you call this function,
6966 * inode->i_size and fe->i_size have been adjusted.
6968 * WARNING: This will kfree the truncate context
6970 int ocfs2_commit_truncate(struct ocfs2_super *osb,
6971 struct inode *inode,
6972 struct buffer_head *di_bh)
6974 int status = 0, i, flags = 0;
6975 u32 new_highest_cpos, range, trunc_cpos, trunc_len, phys_cpos, coff;
6977 struct ocfs2_extent_list *el;
6978 struct ocfs2_extent_rec *rec;
6979 struct ocfs2_path *path = NULL;
6980 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6981 struct ocfs2_extent_list *root_el = &(di->id2.i_list);
6982 u64 refcount_loc = le64_to_cpu(di->i_refcount_loc);
6983 struct ocfs2_extent_tree et;
6984 struct ocfs2_cached_dealloc_ctxt dealloc;
6986 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
6987 ocfs2_init_dealloc_ctxt(&dealloc);
6989 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
6990 i_size_read(inode));
6992 path = ocfs2_new_path(di_bh, &di->id2.i_list,
6993 ocfs2_journal_access_di);
7000 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7004 * Check that we still have allocation to delete.
7006 if (OCFS2_I(inode)->ip_clusters == 0) {
7012 * Truncate always works against the rightmost tree branch.
7014 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
7020 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7021 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7024 * By now, el will point to the extent list on the bottom most
7025 * portion of this tree. Only the tail record is considered in
7028 * We handle the following cases, in order:
7029 * - empty extent: delete the remaining branch
7030 * - remove the entire record
7031 * - remove a partial record
7032 * - no record needs to be removed (truncate has completed)
7034 el = path_leaf_el(path);
7035 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7036 ocfs2_error(inode->i_sb,
7037 "Inode %llu has empty extent block at %llu\n",
7038 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7039 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7044 i = le16_to_cpu(el->l_next_free_rec) - 1;
7045 rec = &el->l_recs[i];
7046 flags = rec->e_flags;
7047 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
7049 if (i == 0 && ocfs2_is_empty_extent(rec)) {
7051 * Lower levels depend on this never happening, but it's best
7052 * to check it up here before changing the tree.
7054 if (root_el->l_tree_depth && rec->e_int_clusters == 0) {
7055 ocfs2_error(inode->i_sb, "Inode %lu has an empty "
7056 "extent record, depth %u\n", inode->i_ino,
7057 le16_to_cpu(root_el->l_tree_depth));
7061 trunc_cpos = le32_to_cpu(rec->e_cpos);
7064 } else if (le32_to_cpu(rec->e_cpos) >= new_highest_cpos) {
7066 * Truncate entire record.
7068 trunc_cpos = le32_to_cpu(rec->e_cpos);
7069 trunc_len = ocfs2_rec_clusters(el, rec);
7070 blkno = le64_to_cpu(rec->e_blkno);
7071 } else if (range > new_highest_cpos) {
7073 * Partial truncate. it also should be
7074 * the last truncate we're doing.
7076 trunc_cpos = new_highest_cpos;
7077 trunc_len = range - new_highest_cpos;
7078 coff = new_highest_cpos - le32_to_cpu(rec->e_cpos);
7079 blkno = le64_to_cpu(rec->e_blkno) +
7080 ocfs2_clusters_to_blocks(inode->i_sb, coff);
7083 * Truncate completed, leave happily.
7089 phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
7091 status = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
7092 phys_cpos, trunc_len, flags, &dealloc,
7099 ocfs2_reinit_path(path, 1);
7102 * The check above will catch the case where we've truncated
7103 * away all allocation.
7109 ocfs2_schedule_truncate_log_flush(osb, 1);
7111 ocfs2_run_deallocs(osb, &dealloc);
7113 ocfs2_free_path(path);
7120 * 'start' is inclusive, 'end' is not.
7122 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7123 unsigned int start, unsigned int end, int trunc)
7126 unsigned int numbytes;
7128 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7129 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7130 struct ocfs2_inline_data *idata = &di->id2.i_data;
7132 if (end > i_size_read(inode))
7133 end = i_size_read(inode);
7135 BUG_ON(start >= end);
7137 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7138 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7139 !ocfs2_supports_inline_data(osb)) {
7140 ocfs2_error(inode->i_sb,
7141 "Inline data flags for inode %llu don't agree! "
7142 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7143 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7144 le16_to_cpu(di->i_dyn_features),
7145 OCFS2_I(inode)->ip_dyn_features,
7146 osb->s_feature_incompat);
7151 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7152 if (IS_ERR(handle)) {
7153 ret = PTR_ERR(handle);
7158 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7159 OCFS2_JOURNAL_ACCESS_WRITE);
7165 numbytes = end - start;
7166 memset(idata->id_data + start, 0, numbytes);
7169 * No need to worry about the data page here - it's been
7170 * truncated already and inline data doesn't need it for
7171 * pushing zero's to disk, so we'll let readpage pick it up
7175 i_size_write(inode, start);
7176 di->i_size = cpu_to_le64(start);
7179 inode->i_blocks = ocfs2_inode_sector_count(inode);
7180 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7182 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7183 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7185 ocfs2_journal_dirty(handle, di_bh);
7188 ocfs2_commit_trans(osb, handle);