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>
32 #include <linux/blkdev.h>
33 #include <linux/sched/signal.h>
35 #include <cluster/masklog.h>
41 #include "blockcheck.h"
43 #include "extent_map.h"
46 #include "localalloc.h"
53 #include "refcounttree.h"
54 #include "ocfs2_trace.h"
56 #include "buffer_head_io.h"
58 enum ocfs2_contig_type {
65 static enum ocfs2_contig_type
66 ocfs2_extent_rec_contig(struct super_block *sb,
67 struct ocfs2_extent_rec *ext,
68 struct ocfs2_extent_rec *insert_rec);
70 * Operations for a specific extent tree type.
72 * To implement an on-disk btree (extent tree) type in ocfs2, add
73 * an ocfs2_extent_tree_operations structure and the matching
74 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
75 * for the allocation portion of the extent tree.
77 struct ocfs2_extent_tree_operations {
79 * last_eb_blk is the block number of the right most leaf extent
80 * block. Most on-disk structures containing an extent tree store
81 * this value for fast access. The ->eo_set_last_eb_blk() and
82 * ->eo_get_last_eb_blk() operations access this value. They are
85 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
87 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
90 * The on-disk structure usually keeps track of how many total
91 * clusters are stored in this extent tree. This function updates
92 * that value. new_clusters is the delta, and must be
93 * added to the total. Required.
95 void (*eo_update_clusters)(struct ocfs2_extent_tree *et,
99 * If this extent tree is supported by an extent map, insert
100 * a record into the map.
102 void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et,
103 struct ocfs2_extent_rec *rec);
106 * If this extent tree is supported by an extent map, truncate the
109 void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et,
113 * If ->eo_insert_check() exists, it is called before rec is
114 * inserted into the extent tree. It is optional.
116 int (*eo_insert_check)(struct ocfs2_extent_tree *et,
117 struct ocfs2_extent_rec *rec);
118 int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
121 * --------------------------------------------------------------
122 * The remaining are internal to ocfs2_extent_tree and don't have
127 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
130 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
133 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
134 * it exists. If it does not, et->et_max_leaf_clusters is set
135 * to 0 (unlimited). Optional.
137 void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
140 * ->eo_extent_contig test whether the 2 ocfs2_extent_rec
141 * are contiguous or not. Optional. Don't need to set it if use
142 * ocfs2_extent_rec as the tree leaf.
144 enum ocfs2_contig_type
145 (*eo_extent_contig)(struct ocfs2_extent_tree *et,
146 struct ocfs2_extent_rec *ext,
147 struct ocfs2_extent_rec *insert_rec);
152 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
155 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
156 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
158 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
160 static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
161 struct ocfs2_extent_rec *rec);
162 static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
164 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
165 struct ocfs2_extent_rec *rec);
166 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
167 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
168 static const struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
169 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
170 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
171 .eo_update_clusters = ocfs2_dinode_update_clusters,
172 .eo_extent_map_insert = ocfs2_dinode_extent_map_insert,
173 .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate,
174 .eo_insert_check = ocfs2_dinode_insert_check,
175 .eo_sanity_check = ocfs2_dinode_sanity_check,
176 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
179 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
182 struct ocfs2_dinode *di = et->et_object;
184 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
185 di->i_last_eb_blk = cpu_to_le64(blkno);
188 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
190 struct ocfs2_dinode *di = et->et_object;
192 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
193 return le64_to_cpu(di->i_last_eb_blk);
196 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
199 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
200 struct ocfs2_dinode *di = et->et_object;
202 le32_add_cpu(&di->i_clusters, clusters);
203 spin_lock(&oi->ip_lock);
204 oi->ip_clusters = le32_to_cpu(di->i_clusters);
205 spin_unlock(&oi->ip_lock);
208 static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
209 struct ocfs2_extent_rec *rec)
211 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
213 ocfs2_extent_map_insert_rec(inode, rec);
216 static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
219 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
221 ocfs2_extent_map_trunc(inode, clusters);
224 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
225 struct ocfs2_extent_rec *rec)
227 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
228 struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
230 BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
231 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
232 (oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
233 "Device %s, asking for sparse allocation: inode %llu, "
234 "cpos %u, clusters %u\n",
236 (unsigned long long)oi->ip_blkno,
237 rec->e_cpos, oi->ip_clusters);
242 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
244 struct ocfs2_dinode *di = et->et_object;
246 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
247 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
252 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
254 struct ocfs2_dinode *di = et->et_object;
256 et->et_root_el = &di->id2.i_list;
260 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
262 struct ocfs2_xattr_value_buf *vb = et->et_object;
264 et->et_root_el = &vb->vb_xv->xr_list;
267 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
270 struct ocfs2_xattr_value_buf *vb = et->et_object;
272 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
275 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
277 struct ocfs2_xattr_value_buf *vb = et->et_object;
279 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
282 static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
285 struct ocfs2_xattr_value_buf *vb = et->et_object;
287 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
290 static const struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
291 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
292 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
293 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
294 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
297 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
299 struct ocfs2_xattr_block *xb = et->et_object;
301 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
304 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
306 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
307 et->et_max_leaf_clusters =
308 ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
311 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
314 struct ocfs2_xattr_block *xb = et->et_object;
315 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
317 xt->xt_last_eb_blk = cpu_to_le64(blkno);
320 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
322 struct ocfs2_xattr_block *xb = et->et_object;
323 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
325 return le64_to_cpu(xt->xt_last_eb_blk);
328 static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
331 struct ocfs2_xattr_block *xb = et->et_object;
333 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
336 static const struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
337 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
338 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
339 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
340 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
341 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
344 static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
347 struct ocfs2_dx_root_block *dx_root = et->et_object;
349 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
352 static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
354 struct ocfs2_dx_root_block *dx_root = et->et_object;
356 return le64_to_cpu(dx_root->dr_last_eb_blk);
359 static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
362 struct ocfs2_dx_root_block *dx_root = et->et_object;
364 le32_add_cpu(&dx_root->dr_clusters, clusters);
367 static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
369 struct ocfs2_dx_root_block *dx_root = et->et_object;
371 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
376 static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
378 struct ocfs2_dx_root_block *dx_root = et->et_object;
380 et->et_root_el = &dx_root->dr_list;
383 static const struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
384 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
385 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
386 .eo_update_clusters = ocfs2_dx_root_update_clusters,
387 .eo_sanity_check = ocfs2_dx_root_sanity_check,
388 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
391 static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et)
393 struct ocfs2_refcount_block *rb = et->et_object;
395 et->et_root_el = &rb->rf_list;
398 static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
401 struct ocfs2_refcount_block *rb = et->et_object;
403 rb->rf_last_eb_blk = cpu_to_le64(blkno);
406 static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
408 struct ocfs2_refcount_block *rb = et->et_object;
410 return le64_to_cpu(rb->rf_last_eb_blk);
413 static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et,
416 struct ocfs2_refcount_block *rb = et->et_object;
418 le32_add_cpu(&rb->rf_clusters, clusters);
421 static enum ocfs2_contig_type
422 ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et,
423 struct ocfs2_extent_rec *ext,
424 struct ocfs2_extent_rec *insert_rec)
429 static const struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = {
430 .eo_set_last_eb_blk = ocfs2_refcount_tree_set_last_eb_blk,
431 .eo_get_last_eb_blk = ocfs2_refcount_tree_get_last_eb_blk,
432 .eo_update_clusters = ocfs2_refcount_tree_update_clusters,
433 .eo_fill_root_el = ocfs2_refcount_tree_fill_root_el,
434 .eo_extent_contig = ocfs2_refcount_tree_extent_contig,
437 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
438 struct ocfs2_caching_info *ci,
439 struct buffer_head *bh,
440 ocfs2_journal_access_func access,
442 const struct ocfs2_extent_tree_operations *ops)
447 et->et_root_journal_access = access;
449 obj = (void *)bh->b_data;
452 et->et_ops->eo_fill_root_el(et);
453 if (!et->et_ops->eo_fill_max_leaf_clusters)
454 et->et_max_leaf_clusters = 0;
456 et->et_ops->eo_fill_max_leaf_clusters(et);
459 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
460 struct ocfs2_caching_info *ci,
461 struct buffer_head *bh)
463 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di,
464 NULL, &ocfs2_dinode_et_ops);
467 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
468 struct ocfs2_caching_info *ci,
469 struct buffer_head *bh)
471 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb,
472 NULL, &ocfs2_xattr_tree_et_ops);
475 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
476 struct ocfs2_caching_info *ci,
477 struct ocfs2_xattr_value_buf *vb)
479 __ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb,
480 &ocfs2_xattr_value_et_ops);
483 void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
484 struct ocfs2_caching_info *ci,
485 struct buffer_head *bh)
487 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr,
488 NULL, &ocfs2_dx_root_et_ops);
491 void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
492 struct ocfs2_caching_info *ci,
493 struct buffer_head *bh)
495 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb,
496 NULL, &ocfs2_refcount_tree_et_ops);
499 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
502 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
505 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
507 return et->et_ops->eo_get_last_eb_blk(et);
510 static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
513 et->et_ops->eo_update_clusters(et, clusters);
516 static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et,
517 struct ocfs2_extent_rec *rec)
519 if (et->et_ops->eo_extent_map_insert)
520 et->et_ops->eo_extent_map_insert(et, rec);
523 static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et,
526 if (et->et_ops->eo_extent_map_truncate)
527 et->et_ops->eo_extent_map_truncate(et, clusters);
530 static inline int ocfs2_et_root_journal_access(handle_t *handle,
531 struct ocfs2_extent_tree *et,
534 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
538 static inline enum ocfs2_contig_type
539 ocfs2_et_extent_contig(struct ocfs2_extent_tree *et,
540 struct ocfs2_extent_rec *rec,
541 struct ocfs2_extent_rec *insert_rec)
543 if (et->et_ops->eo_extent_contig)
544 return et->et_ops->eo_extent_contig(et, rec, insert_rec);
546 return ocfs2_extent_rec_contig(
547 ocfs2_metadata_cache_get_super(et->et_ci),
551 static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
552 struct ocfs2_extent_rec *rec)
556 if (et->et_ops->eo_insert_check)
557 ret = et->et_ops->eo_insert_check(et, rec);
561 static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
565 if (et->et_ops->eo_sanity_check)
566 ret = et->et_ops->eo_sanity_check(et);
570 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
571 struct ocfs2_extent_block *eb);
572 static void ocfs2_adjust_rightmost_records(handle_t *handle,
573 struct ocfs2_extent_tree *et,
574 struct ocfs2_path *path,
575 struct ocfs2_extent_rec *insert_rec);
577 * Reset the actual path elements so that we can re-use the structure
578 * to build another path. Generally, this involves freeing the buffer
581 void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
583 int i, start = 0, depth = 0;
584 struct ocfs2_path_item *node;
589 for(i = start; i < path_num_items(path); i++) {
590 node = &path->p_node[i];
598 * Tree depth may change during truncate, or insert. If we're
599 * keeping the root extent list, then make sure that our path
600 * structure reflects the proper depth.
603 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
605 path_root_access(path) = NULL;
607 path->p_tree_depth = depth;
610 void ocfs2_free_path(struct ocfs2_path *path)
613 ocfs2_reinit_path(path, 0);
619 * All the elements of src into dest. After this call, src could be freed
620 * without affecting dest.
622 * Both paths should have the same root. Any non-root elements of dest
625 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
629 BUG_ON(path_root_bh(dest) != path_root_bh(src));
630 BUG_ON(path_root_el(dest) != path_root_el(src));
631 BUG_ON(path_root_access(dest) != path_root_access(src));
633 ocfs2_reinit_path(dest, 1);
635 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
636 dest->p_node[i].bh = src->p_node[i].bh;
637 dest->p_node[i].el = src->p_node[i].el;
639 if (dest->p_node[i].bh)
640 get_bh(dest->p_node[i].bh);
645 * Make the *dest path the same as src and re-initialize src path to
648 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
652 BUG_ON(path_root_bh(dest) != path_root_bh(src));
653 BUG_ON(path_root_access(dest) != path_root_access(src));
655 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
656 brelse(dest->p_node[i].bh);
658 dest->p_node[i].bh = src->p_node[i].bh;
659 dest->p_node[i].el = src->p_node[i].el;
661 src->p_node[i].bh = NULL;
662 src->p_node[i].el = NULL;
667 * Insert an extent block at given index.
669 * This will not take an additional reference on eb_bh.
671 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
672 struct buffer_head *eb_bh)
674 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
677 * Right now, no root bh is an extent block, so this helps
678 * catch code errors with dinode trees. The assertion can be
679 * safely removed if we ever need to insert extent block
680 * structures at the root.
684 path->p_node[index].bh = eb_bh;
685 path->p_node[index].el = &eb->h_list;
688 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
689 struct ocfs2_extent_list *root_el,
690 ocfs2_journal_access_func access)
692 struct ocfs2_path *path;
694 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
696 path = kzalloc(sizeof(*path), GFP_NOFS);
698 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
700 path_root_bh(path) = root_bh;
701 path_root_el(path) = root_el;
702 path_root_access(path) = access;
708 struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
710 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
711 path_root_access(path));
714 struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
716 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
717 et->et_root_journal_access);
721 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
722 * otherwise it's the root_access function.
724 * I don't like the way this function's name looks next to
725 * ocfs2_journal_access_path(), but I don't have a better one.
727 int ocfs2_path_bh_journal_access(handle_t *handle,
728 struct ocfs2_caching_info *ci,
729 struct ocfs2_path *path,
732 ocfs2_journal_access_func access = path_root_access(path);
735 access = ocfs2_journal_access;
738 access = ocfs2_journal_access_eb;
740 return access(handle, ci, path->p_node[idx].bh,
741 OCFS2_JOURNAL_ACCESS_WRITE);
745 * Convenience function to journal all components in a path.
747 int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
749 struct ocfs2_path *path)
756 for(i = 0; i < path_num_items(path); i++) {
757 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
769 * Return the index of the extent record which contains cluster #v_cluster.
770 * -1 is returned if it was not found.
772 * Should work fine on interior and exterior nodes.
774 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
778 struct ocfs2_extent_rec *rec;
779 u32 rec_end, rec_start, clusters;
781 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
782 rec = &el->l_recs[i];
784 rec_start = le32_to_cpu(rec->e_cpos);
785 clusters = ocfs2_rec_clusters(el, rec);
787 rec_end = rec_start + clusters;
789 if (v_cluster >= rec_start && v_cluster < rec_end) {
799 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
800 * ocfs2_extent_rec_contig only work properly against leaf nodes!
802 static int ocfs2_block_extent_contig(struct super_block *sb,
803 struct ocfs2_extent_rec *ext,
806 u64 blk_end = le64_to_cpu(ext->e_blkno);
808 blk_end += ocfs2_clusters_to_blocks(sb,
809 le16_to_cpu(ext->e_leaf_clusters));
811 return blkno == blk_end;
814 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
815 struct ocfs2_extent_rec *right)
819 left_range = le32_to_cpu(left->e_cpos) +
820 le16_to_cpu(left->e_leaf_clusters);
822 return (left_range == le32_to_cpu(right->e_cpos));
825 static enum ocfs2_contig_type
826 ocfs2_extent_rec_contig(struct super_block *sb,
827 struct ocfs2_extent_rec *ext,
828 struct ocfs2_extent_rec *insert_rec)
830 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
833 * Refuse to coalesce extent records with different flag
834 * fields - we don't want to mix unwritten extents with user
837 if (ext->e_flags != insert_rec->e_flags)
840 if (ocfs2_extents_adjacent(ext, insert_rec) &&
841 ocfs2_block_extent_contig(sb, ext, blkno))
844 blkno = le64_to_cpu(ext->e_blkno);
845 if (ocfs2_extents_adjacent(insert_rec, ext) &&
846 ocfs2_block_extent_contig(sb, insert_rec, blkno))
853 * NOTE: We can have pretty much any combination of contiguousness and
856 * The usefulness of APPEND_TAIL is more in that it lets us know that
857 * we'll have to update the path to that leaf.
859 enum ocfs2_append_type {
864 enum ocfs2_split_type {
870 struct ocfs2_insert_type {
871 enum ocfs2_split_type ins_split;
872 enum ocfs2_append_type ins_appending;
873 enum ocfs2_contig_type ins_contig;
874 int ins_contig_index;
878 struct ocfs2_merge_ctxt {
879 enum ocfs2_contig_type c_contig_type;
880 int c_has_empty_extent;
881 int c_split_covers_rec;
884 static int ocfs2_validate_extent_block(struct super_block *sb,
885 struct buffer_head *bh)
888 struct ocfs2_extent_block *eb =
889 (struct ocfs2_extent_block *)bh->b_data;
891 trace_ocfs2_validate_extent_block((unsigned long long)bh->b_blocknr);
893 BUG_ON(!buffer_uptodate(bh));
896 * If the ecc fails, we return the error but otherwise
897 * leave the filesystem running. We know any error is
898 * local to this block.
900 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
902 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
903 (unsigned long long)bh->b_blocknr);
908 * Errors after here are fatal.
911 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
913 "Extent block #%llu has bad signature %.*s\n",
914 (unsigned long long)bh->b_blocknr, 7,
919 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
921 "Extent block #%llu has an invalid h_blkno of %llu\n",
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 h_fs_generation of #%u\n",
930 (unsigned long long)bh->b_blocknr,
931 le32_to_cpu(eb->h_fs_generation));
938 int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
939 struct buffer_head **bh)
942 struct buffer_head *tmp = *bh;
944 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
945 ocfs2_validate_extent_block);
947 /* If ocfs2_read_block() got us a new bh, pass it up. */
956 * How many free extents have we got before we need more meta data?
958 int ocfs2_num_free_extents(struct ocfs2_extent_tree *et)
961 struct ocfs2_extent_list *el = NULL;
962 struct ocfs2_extent_block *eb;
963 struct buffer_head *eb_bh = NULL;
967 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
970 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
976 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
980 BUG_ON(el->l_tree_depth != 0);
982 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
986 trace_ocfs2_num_free_extents(retval);
990 /* expects array to already be allocated
992 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
995 static int ocfs2_create_new_meta_bhs(handle_t *handle,
996 struct ocfs2_extent_tree *et,
998 struct ocfs2_alloc_context *meta_ac,
999 struct buffer_head *bhs[])
1001 int count, status, i;
1002 u16 suballoc_bit_start;
1004 u64 suballoc_loc, first_blkno;
1005 struct ocfs2_super *osb =
1006 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
1007 struct ocfs2_extent_block *eb;
1010 while (count < wanted) {
1011 status = ocfs2_claim_metadata(handle,
1015 &suballoc_bit_start,
1023 for(i = count; i < (num_got + count); i++) {
1024 bhs[i] = sb_getblk(osb->sb, first_blkno);
1025 if (bhs[i] == NULL) {
1030 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
1032 status = ocfs2_journal_access_eb(handle, et->et_ci,
1034 OCFS2_JOURNAL_ACCESS_CREATE);
1040 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
1041 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
1042 /* Ok, setup the minimal stuff here. */
1043 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
1044 eb->h_blkno = cpu_to_le64(first_blkno);
1045 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
1046 eb->h_suballoc_slot =
1047 cpu_to_le16(meta_ac->ac_alloc_slot);
1048 eb->h_suballoc_loc = cpu_to_le64(suballoc_loc);
1049 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1050 eb->h_list.l_count =
1051 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1053 suballoc_bit_start++;
1056 /* We'll also be dirtied by the caller, so
1057 * this isn't absolutely necessary. */
1058 ocfs2_journal_dirty(handle, bhs[i]);
1067 for(i = 0; i < wanted; i++) {
1077 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1079 * Returns the sum of the rightmost extent rec logical offset and
1082 * ocfs2_add_branch() uses this to determine what logical cluster
1083 * value should be populated into the leftmost new branch records.
1085 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1086 * value for the new topmost tree record.
1088 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1092 i = le16_to_cpu(el->l_next_free_rec) - 1;
1094 return le32_to_cpu(el->l_recs[i].e_cpos) +
1095 ocfs2_rec_clusters(el, &el->l_recs[i]);
1099 * Change range of the branches in the right most path according to the leaf
1100 * extent block's rightmost record.
1102 static int ocfs2_adjust_rightmost_branch(handle_t *handle,
1103 struct ocfs2_extent_tree *et)
1106 struct ocfs2_path *path = NULL;
1107 struct ocfs2_extent_list *el;
1108 struct ocfs2_extent_rec *rec;
1110 path = ocfs2_new_path_from_et(et);
1116 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
1122 status = ocfs2_extend_trans(handle, path_num_items(path));
1128 status = ocfs2_journal_access_path(et->et_ci, handle, path);
1134 el = path_leaf_el(path);
1135 rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec) - 1];
1137 ocfs2_adjust_rightmost_records(handle, et, path, rec);
1140 ocfs2_free_path(path);
1145 * Add an entire tree branch to our inode. eb_bh is the extent block
1146 * to start at, if we don't want to start the branch at the root
1149 * last_eb_bh is required as we have to update it's next_leaf pointer
1150 * for the new last extent block.
1152 * the new branch will be 'empty' in the sense that every block will
1153 * contain a single record with cluster count == 0.
1155 static int ocfs2_add_branch(handle_t *handle,
1156 struct ocfs2_extent_tree *et,
1157 struct buffer_head *eb_bh,
1158 struct buffer_head **last_eb_bh,
1159 struct ocfs2_alloc_context *meta_ac)
1161 int status, new_blocks, i;
1162 u64 next_blkno, new_last_eb_blk;
1163 struct buffer_head *bh;
1164 struct buffer_head **new_eb_bhs = NULL;
1165 struct ocfs2_extent_block *eb;
1166 struct ocfs2_extent_list *eb_el;
1167 struct ocfs2_extent_list *el;
1168 u32 new_cpos, root_end;
1170 BUG_ON(!last_eb_bh || !*last_eb_bh);
1173 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1176 el = et->et_root_el;
1178 /* we never add a branch to a leaf. */
1179 BUG_ON(!el->l_tree_depth);
1181 new_blocks = le16_to_cpu(el->l_tree_depth);
1183 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1184 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1185 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1188 * If there is a gap before the root end and the real end
1189 * of the righmost leaf block, we need to remove the gap
1190 * between new_cpos and root_end first so that the tree
1191 * is consistent after we add a new branch(it will start
1194 if (root_end > new_cpos) {
1195 trace_ocfs2_adjust_rightmost_branch(
1196 (unsigned long long)
1197 ocfs2_metadata_cache_owner(et->et_ci),
1198 root_end, new_cpos);
1200 status = ocfs2_adjust_rightmost_branch(handle, et);
1207 /* allocate the number of new eb blocks we need */
1208 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1216 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
1217 meta_ac, new_eb_bhs);
1223 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1224 * linked with the rest of the tree.
1225 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1227 * when we leave the loop, new_last_eb_blk will point to the
1228 * newest leaf, and next_blkno will point to the topmost extent
1230 next_blkno = new_last_eb_blk = 0;
1231 for(i = 0; i < new_blocks; i++) {
1233 eb = (struct ocfs2_extent_block *) bh->b_data;
1234 /* ocfs2_create_new_meta_bhs() should create it right! */
1235 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1236 eb_el = &eb->h_list;
1238 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
1239 OCFS2_JOURNAL_ACCESS_CREATE);
1245 eb->h_next_leaf_blk = 0;
1246 eb_el->l_tree_depth = cpu_to_le16(i);
1247 eb_el->l_next_free_rec = cpu_to_le16(1);
1249 * This actually counts as an empty extent as
1252 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
1253 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
1255 * eb_el isn't always an interior node, but even leaf
1256 * nodes want a zero'd flags and reserved field so
1257 * this gets the whole 32 bits regardless of use.
1259 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
1260 if (!eb_el->l_tree_depth)
1261 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1263 ocfs2_journal_dirty(handle, bh);
1264 next_blkno = le64_to_cpu(eb->h_blkno);
1267 /* This is a bit hairy. We want to update up to three blocks
1268 * here without leaving any of them in an inconsistent state
1269 * in case of error. We don't have to worry about
1270 * journal_dirty erroring as it won't unless we've aborted the
1271 * handle (in which case we would never be here) so reserving
1272 * the write with journal_access is all we need to do. */
1273 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
1274 OCFS2_JOURNAL_ACCESS_WRITE);
1279 status = ocfs2_et_root_journal_access(handle, et,
1280 OCFS2_JOURNAL_ACCESS_WRITE);
1286 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
1287 OCFS2_JOURNAL_ACCESS_WRITE);
1294 /* Link the new branch into the rest of the tree (el will
1295 * either be on the root_bh, or the extent block passed in. */
1296 i = le16_to_cpu(el->l_next_free_rec);
1297 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
1298 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1299 el->l_recs[i].e_int_clusters = 0;
1300 le16_add_cpu(&el->l_next_free_rec, 1);
1302 /* fe needs a new last extent block pointer, as does the
1303 * next_leaf on the previously last-extent-block. */
1304 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
1306 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
1307 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1309 ocfs2_journal_dirty(handle, *last_eb_bh);
1310 ocfs2_journal_dirty(handle, et->et_root_bh);
1312 ocfs2_journal_dirty(handle, eb_bh);
1315 * Some callers want to track the rightmost leaf so pass it
1318 brelse(*last_eb_bh);
1319 get_bh(new_eb_bhs[0]);
1320 *last_eb_bh = new_eb_bhs[0];
1325 for (i = 0; i < new_blocks; i++)
1326 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;
1414 * Should only be called when there is no space left in any of the
1415 * leaf nodes. What we want to do is find the lowest tree depth
1416 * non-leaf extent block with room for new records. There are three
1417 * valid results of this search:
1419 * 1) a lowest extent block is found, then we pass it back in
1420 * *lowest_eb_bh and return '0'
1422 * 2) the search fails to find anything, but the root_el has room. We
1423 * pass NULL back in *lowest_eb_bh, but still return '0'
1425 * 3) the search fails to find anything AND the root_el is full, in
1426 * which case we return > 0
1428 * return status < 0 indicates an error.
1430 static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
1431 struct buffer_head **target_bh)
1435 struct ocfs2_extent_block *eb;
1436 struct ocfs2_extent_list *el;
1437 struct buffer_head *bh = NULL;
1438 struct buffer_head *lowest_bh = NULL;
1442 el = et->et_root_el;
1444 while(le16_to_cpu(el->l_tree_depth) > 1) {
1445 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1446 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1447 "Owner %llu has empty extent list (next_free_rec == 0)\n",
1448 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
1452 i = le16_to_cpu(el->l_next_free_rec) - 1;
1453 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1455 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1456 "Owner %llu has extent list where extent # %d has no physical block start\n",
1457 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
1465 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
1471 eb = (struct ocfs2_extent_block *) bh->b_data;
1474 if (le16_to_cpu(el->l_next_free_rec) <
1475 le16_to_cpu(el->l_count)) {
1482 /* If we didn't find one and the fe doesn't have any room,
1483 * then return '1' */
1484 el = et->et_root_el;
1485 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1488 *target_bh = lowest_bh;
1496 * Grow a b-tree so that it has more records.
1498 * We might shift the tree depth in which case existing paths should
1499 * be considered invalid.
1501 * Tree depth after the grow is returned via *final_depth.
1503 * *last_eb_bh will be updated by ocfs2_add_branch().
1505 static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1506 int *final_depth, struct buffer_head **last_eb_bh,
1507 struct ocfs2_alloc_context *meta_ac)
1510 struct ocfs2_extent_list *el = et->et_root_el;
1511 int depth = le16_to_cpu(el->l_tree_depth);
1512 struct buffer_head *bh = NULL;
1514 BUG_ON(meta_ac == NULL);
1516 shift = ocfs2_find_branch_target(et, &bh);
1523 /* We traveled all the way to the bottom of the allocation tree
1524 * and didn't find room for any more extents - we need to add
1525 * another tree level */
1528 trace_ocfs2_grow_tree(
1529 (unsigned long long)
1530 ocfs2_metadata_cache_owner(et->et_ci),
1533 /* ocfs2_shift_tree_depth will return us a buffer with
1534 * the new extent block (so we can pass that to
1535 * ocfs2_add_branch). */
1536 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
1544 * Special case: we have room now if we shifted from
1545 * tree_depth 0, so no more work needs to be done.
1547 * We won't be calling add_branch, so pass
1548 * back *last_eb_bh as the new leaf. At depth
1549 * zero, it should always be null so there's
1550 * no reason to brelse.
1552 BUG_ON(*last_eb_bh);
1559 /* call ocfs2_add_branch to add the final part of the tree with
1561 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
1570 *final_depth = depth;
1576 * This function will discard the rightmost extent record.
1578 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1580 int next_free = le16_to_cpu(el->l_next_free_rec);
1581 int count = le16_to_cpu(el->l_count);
1582 unsigned int num_bytes;
1585 /* This will cause us to go off the end of our extent list. */
1586 BUG_ON(next_free >= count);
1588 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1590 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1593 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1594 struct ocfs2_extent_rec *insert_rec)
1596 int i, insert_index, next_free, has_empty, num_bytes;
1597 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1598 struct ocfs2_extent_rec *rec;
1600 next_free = le16_to_cpu(el->l_next_free_rec);
1601 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1605 /* The tree code before us didn't allow enough room in the leaf. */
1606 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1609 * The easiest way to approach this is to just remove the
1610 * empty extent and temporarily decrement next_free.
1614 * If next_free was 1 (only an empty extent), this
1615 * loop won't execute, which is fine. We still want
1616 * the decrement above to happen.
1618 for(i = 0; i < (next_free - 1); i++)
1619 el->l_recs[i] = el->l_recs[i+1];
1625 * Figure out what the new record index should be.
1627 for(i = 0; i < next_free; i++) {
1628 rec = &el->l_recs[i];
1630 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1635 trace_ocfs2_rotate_leaf(insert_cpos, insert_index,
1636 has_empty, next_free,
1637 le16_to_cpu(el->l_count));
1639 BUG_ON(insert_index < 0);
1640 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1641 BUG_ON(insert_index > next_free);
1644 * No need to memmove if we're just adding to the tail.
1646 if (insert_index != next_free) {
1647 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1649 num_bytes = next_free - insert_index;
1650 num_bytes *= sizeof(struct ocfs2_extent_rec);
1651 memmove(&el->l_recs[insert_index + 1],
1652 &el->l_recs[insert_index],
1657 * Either we had an empty extent, and need to re-increment or
1658 * there was no empty extent on a non full rightmost leaf node,
1659 * in which case we still need to increment.
1662 el->l_next_free_rec = cpu_to_le16(next_free);
1664 * Make sure none of the math above just messed up our tree.
1666 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1668 el->l_recs[insert_index] = *insert_rec;
1672 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1674 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1676 BUG_ON(num_recs == 0);
1678 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1680 size = num_recs * sizeof(struct ocfs2_extent_rec);
1681 memmove(&el->l_recs[0], &el->l_recs[1], size);
1682 memset(&el->l_recs[num_recs], 0,
1683 sizeof(struct ocfs2_extent_rec));
1684 el->l_next_free_rec = cpu_to_le16(num_recs);
1689 * Create an empty extent record .
1691 * l_next_free_rec may be updated.
1693 * If an empty extent already exists do nothing.
1695 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1697 int next_free = le16_to_cpu(el->l_next_free_rec);
1699 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1704 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1707 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1708 "Asked to create an empty extent in a full list:\n"
1709 "count = %u, tree depth = %u",
1710 le16_to_cpu(el->l_count),
1711 le16_to_cpu(el->l_tree_depth));
1713 ocfs2_shift_records_right(el);
1716 le16_add_cpu(&el->l_next_free_rec, 1);
1717 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1721 * For a rotation which involves two leaf nodes, the "root node" is
1722 * the lowest level tree node which contains a path to both leafs. This
1723 * resulting set of information can be used to form a complete "subtree"
1725 * This function is passed two full paths from the dinode down to a
1726 * pair of adjacent leaves. It's task is to figure out which path
1727 * index contains the subtree root - this can be the root index itself
1728 * in a worst-case rotation.
1730 * The array index of the subtree root is passed back.
1732 int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1733 struct ocfs2_path *left,
1734 struct ocfs2_path *right)
1739 * Check that the caller passed in two paths from the same tree.
1741 BUG_ON(path_root_bh(left) != path_root_bh(right));
1747 * The caller didn't pass two adjacent paths.
1749 mlog_bug_on_msg(i > left->p_tree_depth,
1750 "Owner %llu, left depth %u, right depth %u\n"
1751 "left leaf blk %llu, right leaf blk %llu\n",
1752 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1753 left->p_tree_depth, right->p_tree_depth,
1754 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1755 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1756 } while (left->p_node[i].bh->b_blocknr ==
1757 right->p_node[i].bh->b_blocknr);
1762 typedef void (path_insert_t)(void *, struct buffer_head *);
1765 * Traverse a btree path in search of cpos, starting at root_el.
1767 * This code can be called with a cpos larger than the tree, in which
1768 * case it will return the rightmost path.
1770 static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
1771 struct ocfs2_extent_list *root_el, u32 cpos,
1772 path_insert_t *func, void *data)
1777 struct buffer_head *bh = NULL;
1778 struct ocfs2_extent_block *eb;
1779 struct ocfs2_extent_list *el;
1780 struct ocfs2_extent_rec *rec;
1783 while (el->l_tree_depth) {
1784 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1785 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1786 "Owner %llu has empty extent list at depth %u\n",
1787 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1788 le16_to_cpu(el->l_tree_depth));
1794 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1795 rec = &el->l_recs[i];
1798 * In the case that cpos is off the allocation
1799 * tree, this should just wind up returning the
1802 range = le32_to_cpu(rec->e_cpos) +
1803 ocfs2_rec_clusters(el, rec);
1804 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1808 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1810 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1811 "Owner %llu has bad blkno in extent list at depth %u (index %d)\n",
1812 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1813 le16_to_cpu(el->l_tree_depth), i);
1820 ret = ocfs2_read_extent_block(ci, blkno, &bh);
1826 eb = (struct ocfs2_extent_block *) bh->b_data;
1829 if (le16_to_cpu(el->l_next_free_rec) >
1830 le16_to_cpu(el->l_count)) {
1831 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1832 "Owner %llu has bad count in extent list at block %llu (next free=%u, count=%u)\n",
1833 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1834 (unsigned long long)bh->b_blocknr,
1835 le16_to_cpu(el->l_next_free_rec),
1836 le16_to_cpu(el->l_count));
1847 * Catch any trailing bh that the loop didn't handle.
1855 * Given an initialized path (that is, it has a valid root extent
1856 * list), this function will traverse the btree in search of the path
1857 * which would contain cpos.
1859 * The path traveled is recorded in the path structure.
1861 * Note that this will not do any comparisons on leaf node extent
1862 * records, so it will work fine in the case that we just added a tree
1865 struct find_path_data {
1867 struct ocfs2_path *path;
1869 static void find_path_ins(void *data, struct buffer_head *bh)
1871 struct find_path_data *fp = data;
1874 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1877 int ocfs2_find_path(struct ocfs2_caching_info *ci,
1878 struct ocfs2_path *path, u32 cpos)
1880 struct find_path_data data;
1884 return __ocfs2_find_path(ci, path_root_el(path), cpos,
1885 find_path_ins, &data);
1888 static void find_leaf_ins(void *data, struct buffer_head *bh)
1890 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1891 struct ocfs2_extent_list *el = &eb->h_list;
1892 struct buffer_head **ret = data;
1894 /* We want to retain only the leaf block. */
1895 if (le16_to_cpu(el->l_tree_depth) == 0) {
1901 * Find the leaf block in the tree which would contain cpos. No
1902 * checking of the actual leaf is done.
1904 * Some paths want to call this instead of allocating a path structure
1905 * and calling ocfs2_find_path().
1907 * This function doesn't handle non btree extent lists.
1909 int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1910 struct ocfs2_extent_list *root_el, u32 cpos,
1911 struct buffer_head **leaf_bh)
1914 struct buffer_head *bh = NULL;
1916 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
1928 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1930 * Basically, we've moved stuff around at the bottom of the tree and
1931 * we need to fix up the extent records above the changes to reflect
1934 * left_rec: the record on the left.
1935 * right_rec: the record to the right of left_rec
1936 * right_child_el: is the child list pointed to by right_rec
1938 * By definition, this only works on interior nodes.
1940 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1941 struct ocfs2_extent_rec *right_rec,
1942 struct ocfs2_extent_list *right_child_el)
1944 u32 left_clusters, right_end;
1947 * Interior nodes never have holes. Their cpos is the cpos of
1948 * the leftmost record in their child list. Their cluster
1949 * count covers the full theoretical range of their child list
1950 * - the range between their cpos and the cpos of the record
1951 * immediately to their right.
1953 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1954 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1955 BUG_ON(right_child_el->l_tree_depth);
1956 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1957 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1959 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1960 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1963 * Calculate the rightmost cluster count boundary before
1964 * moving cpos - we will need to adjust clusters after
1965 * updating e_cpos to keep the same highest cluster count.
1967 right_end = le32_to_cpu(right_rec->e_cpos);
1968 right_end += le32_to_cpu(right_rec->e_int_clusters);
1970 right_rec->e_cpos = left_rec->e_cpos;
1971 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1973 right_end -= le32_to_cpu(right_rec->e_cpos);
1974 right_rec->e_int_clusters = cpu_to_le32(right_end);
1978 * Adjust the adjacent root node records involved in a
1979 * rotation. left_el_blkno is passed in as a key so that we can easily
1980 * find it's index in the root list.
1982 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1983 struct ocfs2_extent_list *left_el,
1984 struct ocfs2_extent_list *right_el,
1989 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1990 le16_to_cpu(left_el->l_tree_depth));
1992 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1993 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1998 * The path walking code should have never returned a root and
1999 * two paths which are not adjacent.
2001 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2003 ocfs2_adjust_adjacent_records(&root_el->l_recs[i],
2004 &root_el->l_recs[i + 1], right_el);
2008 * We've changed a leaf block (in right_path) and need to reflect that
2009 * change back up the subtree.
2011 * This happens in multiple places:
2012 * - When we've moved an extent record from the left path leaf to the right
2013 * path leaf to make room for an empty extent in the left path leaf.
2014 * - When our insert into the right path leaf is at the leftmost edge
2015 * and requires an update of the path immediately to it's left. This
2016 * can occur at the end of some types of rotation and appending inserts.
2017 * - When we've adjusted the last extent record in the left path leaf and the
2018 * 1st extent record in the right path leaf during cross extent block merge.
2020 static void ocfs2_complete_edge_insert(handle_t *handle,
2021 struct ocfs2_path *left_path,
2022 struct ocfs2_path *right_path,
2026 struct ocfs2_extent_list *el, *left_el, *right_el;
2027 struct ocfs2_extent_rec *left_rec, *right_rec;
2028 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2031 * Update the counts and position values within all the
2032 * interior nodes to reflect the leaf rotation we just did.
2034 * The root node is handled below the loop.
2036 * We begin the loop with right_el and left_el pointing to the
2037 * leaf lists and work our way up.
2039 * NOTE: within this loop, left_el and right_el always refer
2040 * to the *child* lists.
2042 left_el = path_leaf_el(left_path);
2043 right_el = path_leaf_el(right_path);
2044 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2045 trace_ocfs2_complete_edge_insert(i);
2048 * One nice property of knowing that all of these
2049 * nodes are below the root is that we only deal with
2050 * the leftmost right node record and the rightmost
2053 el = left_path->p_node[i].el;
2054 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2055 left_rec = &el->l_recs[idx];
2057 el = right_path->p_node[i].el;
2058 right_rec = &el->l_recs[0];
2060 ocfs2_adjust_adjacent_records(left_rec, right_rec, right_el);
2062 ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2063 ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2066 * Setup our list pointers now so that the current
2067 * parents become children in the next iteration.
2069 left_el = left_path->p_node[i].el;
2070 right_el = right_path->p_node[i].el;
2074 * At the root node, adjust the two adjacent records which
2075 * begin our path to the leaves.
2078 el = left_path->p_node[subtree_index].el;
2079 left_el = left_path->p_node[subtree_index + 1].el;
2080 right_el = right_path->p_node[subtree_index + 1].el;
2082 ocfs2_adjust_root_records(el, left_el, right_el,
2083 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2085 root_bh = left_path->p_node[subtree_index].bh;
2087 ocfs2_journal_dirty(handle, root_bh);
2090 static int ocfs2_rotate_subtree_right(handle_t *handle,
2091 struct ocfs2_extent_tree *et,
2092 struct ocfs2_path *left_path,
2093 struct ocfs2_path *right_path,
2097 struct buffer_head *right_leaf_bh;
2098 struct buffer_head *left_leaf_bh = NULL;
2099 struct buffer_head *root_bh;
2100 struct ocfs2_extent_list *right_el, *left_el;
2101 struct ocfs2_extent_rec move_rec;
2103 left_leaf_bh = path_leaf_bh(left_path);
2104 left_el = path_leaf_el(left_path);
2106 if (left_el->l_next_free_rec != left_el->l_count) {
2107 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
2108 "Inode %llu has non-full interior leaf node %llu (next free = %u)\n",
2109 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2110 (unsigned long long)left_leaf_bh->b_blocknr,
2111 le16_to_cpu(left_el->l_next_free_rec));
2116 * This extent block may already have an empty record, so we
2117 * return early if so.
2119 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2122 root_bh = left_path->p_node[subtree_index].bh;
2123 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2125 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2132 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2133 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2140 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2148 right_leaf_bh = path_leaf_bh(right_path);
2149 right_el = path_leaf_el(right_path);
2151 /* This is a code error, not a disk corruption. */
2152 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2153 "because rightmost leaf block %llu is empty\n",
2154 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2155 (unsigned long long)right_leaf_bh->b_blocknr);
2157 ocfs2_create_empty_extent(right_el);
2159 ocfs2_journal_dirty(handle, right_leaf_bh);
2161 /* Do the copy now. */
2162 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2163 move_rec = left_el->l_recs[i];
2164 right_el->l_recs[0] = move_rec;
2167 * Clear out the record we just copied and shift everything
2168 * over, leaving an empty extent in the left leaf.
2170 * We temporarily subtract from next_free_rec so that the
2171 * shift will lose the tail record (which is now defunct).
2173 le16_add_cpu(&left_el->l_next_free_rec, -1);
2174 ocfs2_shift_records_right(left_el);
2175 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2176 le16_add_cpu(&left_el->l_next_free_rec, 1);
2178 ocfs2_journal_dirty(handle, left_leaf_bh);
2180 ocfs2_complete_edge_insert(handle, left_path, right_path,
2188 * Given a full path, determine what cpos value would return us a path
2189 * containing the leaf immediately to the left of the current one.
2191 * Will return zero if the path passed in is already the leftmost path.
2193 int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2194 struct ocfs2_path *path, u32 *cpos)
2198 struct ocfs2_extent_list *el;
2200 BUG_ON(path->p_tree_depth == 0);
2204 blkno = path_leaf_bh(path)->b_blocknr;
2206 /* Start at the tree node just above the leaf and work our way up. */
2207 i = path->p_tree_depth - 1;
2209 el = path->p_node[i].el;
2212 * Find the extent record just before the one in our
2215 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2216 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2220 * We've determined that the
2221 * path specified is already
2222 * the leftmost one - return a
2228 * The leftmost record points to our
2229 * leaf - we need to travel up the
2235 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
2236 *cpos = *cpos + ocfs2_rec_clusters(el,
2237 &el->l_recs[j - 1]);
2244 * If we got here, we never found a valid node where
2245 * the tree indicated one should be.
2247 ocfs2_error(sb, "Invalid extent tree at extent block %llu\n",
2248 (unsigned long long)blkno);
2253 blkno = path->p_node[i].bh->b_blocknr;
2262 * Extend the transaction by enough credits to complete the rotation,
2263 * and still leave at least the original number of credits allocated
2264 * to this transaction.
2266 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
2268 struct ocfs2_path *path)
2271 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
2273 if (handle->h_buffer_credits < credits)
2274 ret = ocfs2_extend_trans(handle,
2275 credits - handle->h_buffer_credits);
2281 * Trap the case where we're inserting into the theoretical range past
2282 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2283 * whose cpos is less than ours into the right leaf.
2285 * It's only necessary to look at the rightmost record of the left
2286 * leaf because the logic that calls us should ensure that the
2287 * theoretical ranges in the path components above the leaves are
2290 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2293 struct ocfs2_extent_list *left_el;
2294 struct ocfs2_extent_rec *rec;
2297 left_el = path_leaf_el(left_path);
2298 next_free = le16_to_cpu(left_el->l_next_free_rec);
2299 rec = &left_el->l_recs[next_free - 1];
2301 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2306 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2308 int next_free = le16_to_cpu(el->l_next_free_rec);
2310 struct ocfs2_extent_rec *rec;
2315 rec = &el->l_recs[0];
2316 if (ocfs2_is_empty_extent(rec)) {
2320 rec = &el->l_recs[1];
2323 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2324 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2330 * Rotate all the records in a btree right one record, starting at insert_cpos.
2332 * The path to the rightmost leaf should be passed in.
2334 * The array is assumed to be large enough to hold an entire path (tree depth).
2336 * Upon successful return from this function:
2338 * - The 'right_path' array will contain a path to the leaf block
2339 * whose range contains e_cpos.
2340 * - That leaf block will have a single empty extent in list index 0.
2341 * - In the case that the rotation requires a post-insert update,
2342 * *ret_left_path will contain a valid path which can be passed to
2343 * ocfs2_insert_path().
2345 static int ocfs2_rotate_tree_right(handle_t *handle,
2346 struct ocfs2_extent_tree *et,
2347 enum ocfs2_split_type split,
2349 struct ocfs2_path *right_path,
2350 struct ocfs2_path **ret_left_path)
2352 int ret, start, orig_credits = handle->h_buffer_credits;
2354 struct ocfs2_path *left_path = NULL;
2355 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2357 *ret_left_path = NULL;
2359 left_path = ocfs2_new_path_from_path(right_path);
2366 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2372 trace_ocfs2_rotate_tree_right(
2373 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2377 * What we want to do here is:
2379 * 1) Start with the rightmost path.
2381 * 2) Determine a path to the leaf block directly to the left
2384 * 3) Determine the 'subtree root' - the lowest level tree node
2385 * which contains a path to both leaves.
2387 * 4) Rotate the subtree.
2389 * 5) Find the next subtree by considering the left path to be
2390 * the new right path.
2392 * The check at the top of this while loop also accepts
2393 * insert_cpos == cpos because cpos is only a _theoretical_
2394 * value to get us the left path - insert_cpos might very well
2395 * be filling that hole.
2397 * Stop at a cpos of '0' because we either started at the
2398 * leftmost branch (i.e., a tree with one branch and a
2399 * rotation inside of it), or we've gone as far as we can in
2400 * rotating subtrees.
2402 while (cpos && insert_cpos <= cpos) {
2403 trace_ocfs2_rotate_tree_right(
2404 (unsigned long long)
2405 ocfs2_metadata_cache_owner(et->et_ci),
2408 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
2414 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2415 path_leaf_bh(right_path),
2416 "Owner %llu: error during insert of %u "
2417 "(left path cpos %u) results in two identical "
2418 "paths ending at %llu\n",
2419 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2421 (unsigned long long)
2422 path_leaf_bh(left_path)->b_blocknr);
2424 if (split == SPLIT_NONE &&
2425 ocfs2_rotate_requires_path_adjustment(left_path,
2429 * We've rotated the tree as much as we
2430 * should. The rest is up to
2431 * ocfs2_insert_path() to complete, after the
2432 * record insertion. We indicate this
2433 * situation by returning the left path.
2435 * The reason we don't adjust the records here
2436 * before the record insert is that an error
2437 * later might break the rule where a parent
2438 * record e_cpos will reflect the actual
2439 * e_cpos of the 1st nonempty record of the
2442 *ret_left_path = left_path;
2446 start = ocfs2_find_subtree_root(et, left_path, right_path);
2448 trace_ocfs2_rotate_subtree(start,
2449 (unsigned long long)
2450 right_path->p_node[start].bh->b_blocknr,
2451 right_path->p_tree_depth);
2453 ret = ocfs2_extend_rotate_transaction(handle, start,
2454 orig_credits, right_path);
2460 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
2467 if (split != SPLIT_NONE &&
2468 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2471 * A rotate moves the rightmost left leaf
2472 * record over to the leftmost right leaf
2473 * slot. If we're doing an extent split
2474 * instead of a real insert, then we have to
2475 * check that the extent to be split wasn't
2476 * just moved over. If it was, then we can
2477 * exit here, passing left_path back -
2478 * ocfs2_split_extent() is smart enough to
2479 * search both leaves.
2481 *ret_left_path = left_path;
2486 * There is no need to re-read the next right path
2487 * as we know that it'll be our current left
2488 * path. Optimize by copying values instead.
2490 ocfs2_mv_path(right_path, left_path);
2492 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2500 ocfs2_free_path(left_path);
2506 static int ocfs2_update_edge_lengths(handle_t *handle,
2507 struct ocfs2_extent_tree *et,
2508 struct ocfs2_path *path)
2511 struct ocfs2_extent_rec *rec;
2512 struct ocfs2_extent_list *el;
2513 struct ocfs2_extent_block *eb;
2516 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
2522 /* Path should always be rightmost. */
2523 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2524 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2527 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2528 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2529 rec = &el->l_recs[idx];
2530 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2532 for (i = 0; i < path->p_tree_depth; i++) {
2533 el = path->p_node[i].el;
2534 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2535 rec = &el->l_recs[idx];
2537 rec->e_int_clusters = cpu_to_le32(range);
2538 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2540 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2546 static void ocfs2_unlink_path(handle_t *handle,
2547 struct ocfs2_extent_tree *et,
2548 struct ocfs2_cached_dealloc_ctxt *dealloc,
2549 struct ocfs2_path *path, int unlink_start)
2552 struct ocfs2_extent_block *eb;
2553 struct ocfs2_extent_list *el;
2554 struct buffer_head *bh;
2556 for(i = unlink_start; i < path_num_items(path); i++) {
2557 bh = path->p_node[i].bh;
2559 eb = (struct ocfs2_extent_block *)bh->b_data;
2561 * Not all nodes might have had their final count
2562 * decremented by the caller - handle this here.
2565 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2567 "Inode %llu, attempted to remove extent block "
2568 "%llu with %u records\n",
2569 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2570 (unsigned long long)le64_to_cpu(eb->h_blkno),
2571 le16_to_cpu(el->l_next_free_rec));
2573 ocfs2_journal_dirty(handle, bh);
2574 ocfs2_remove_from_cache(et->et_ci, bh);
2578 el->l_next_free_rec = 0;
2579 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2581 ocfs2_journal_dirty(handle, bh);
2583 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2587 ocfs2_remove_from_cache(et->et_ci, bh);
2591 static void ocfs2_unlink_subtree(handle_t *handle,
2592 struct ocfs2_extent_tree *et,
2593 struct ocfs2_path *left_path,
2594 struct ocfs2_path *right_path,
2596 struct ocfs2_cached_dealloc_ctxt *dealloc)
2599 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2600 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2601 struct ocfs2_extent_list *el;
2602 struct ocfs2_extent_block *eb;
2604 el = path_leaf_el(left_path);
2606 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2608 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2609 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2612 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2614 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2615 le16_add_cpu(&root_el->l_next_free_rec, -1);
2617 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2618 eb->h_next_leaf_blk = 0;
2620 ocfs2_journal_dirty(handle, root_bh);
2621 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2623 ocfs2_unlink_path(handle, et, dealloc, right_path,
2627 static int ocfs2_rotate_subtree_left(handle_t *handle,
2628 struct ocfs2_extent_tree *et,
2629 struct ocfs2_path *left_path,
2630 struct ocfs2_path *right_path,
2632 struct ocfs2_cached_dealloc_ctxt *dealloc,
2635 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2636 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2637 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2638 struct ocfs2_extent_block *eb;
2642 right_leaf_el = path_leaf_el(right_path);
2643 left_leaf_el = path_leaf_el(left_path);
2644 root_bh = left_path->p_node[subtree_index].bh;
2645 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2647 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2650 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2651 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2653 * It's legal for us to proceed if the right leaf is
2654 * the rightmost one and it has an empty extent. There
2655 * are two cases to handle - whether the leaf will be
2656 * empty after removal or not. If the leaf isn't empty
2657 * then just remove the empty extent up front. The
2658 * next block will handle empty leaves by flagging
2661 * Non rightmost leaves will throw -EAGAIN and the
2662 * caller can manually move the subtree and retry.
2665 if (eb->h_next_leaf_blk != 0ULL)
2668 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2669 ret = ocfs2_journal_access_eb(handle, et->et_ci,
2670 path_leaf_bh(right_path),
2671 OCFS2_JOURNAL_ACCESS_WRITE);
2677 ocfs2_remove_empty_extent(right_leaf_el);
2679 right_has_empty = 1;
2682 if (eb->h_next_leaf_blk == 0ULL &&
2683 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2685 * We have to update i_last_eb_blk during the meta
2688 ret = ocfs2_et_root_journal_access(handle, et,
2689 OCFS2_JOURNAL_ACCESS_WRITE);
2695 del_right_subtree = 1;
2699 * Getting here with an empty extent in the right path implies
2700 * that it's the rightmost path and will be deleted.
2702 BUG_ON(right_has_empty && !del_right_subtree);
2704 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2711 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2712 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2719 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2727 if (!right_has_empty) {
2729 * Only do this if we're moving a real
2730 * record. Otherwise, the action is delayed until
2731 * after removal of the right path in which case we
2732 * can do a simple shift to remove the empty extent.
2734 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2735 memset(&right_leaf_el->l_recs[0], 0,
2736 sizeof(struct ocfs2_extent_rec));
2738 if (eb->h_next_leaf_blk == 0ULL) {
2740 * Move recs over to get rid of empty extent, decrease
2741 * next_free. This is allowed to remove the last
2742 * extent in our leaf (setting l_next_free_rec to
2743 * zero) - the delete code below won't care.
2745 ocfs2_remove_empty_extent(right_leaf_el);
2748 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2749 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2751 if (del_right_subtree) {
2752 ocfs2_unlink_subtree(handle, et, left_path, right_path,
2753 subtree_index, dealloc);
2754 ret = ocfs2_update_edge_lengths(handle, et, left_path);
2760 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2761 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2764 * Removal of the extent in the left leaf was skipped
2765 * above so we could delete the right path
2768 if (right_has_empty)
2769 ocfs2_remove_empty_extent(left_leaf_el);
2771 ocfs2_journal_dirty(handle, et_root_bh);
2775 ocfs2_complete_edge_insert(handle, left_path, right_path,
2783 * Given a full path, determine what cpos value would return us a path
2784 * containing the leaf immediately to the right of the current one.
2786 * Will return zero if the path passed in is already the rightmost path.
2788 * This looks similar, but is subtly different to
2789 * ocfs2_find_cpos_for_left_leaf().
2791 int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2792 struct ocfs2_path *path, u32 *cpos)
2796 struct ocfs2_extent_list *el;
2800 if (path->p_tree_depth == 0)
2803 blkno = path_leaf_bh(path)->b_blocknr;
2805 /* Start at the tree node just above the leaf and work our way up. */
2806 i = path->p_tree_depth - 1;
2810 el = path->p_node[i].el;
2813 * Find the extent record just after the one in our
2816 next_free = le16_to_cpu(el->l_next_free_rec);
2817 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2818 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2819 if (j == (next_free - 1)) {
2822 * We've determined that the
2823 * path specified is already
2824 * the rightmost one - return a
2830 * The rightmost record points to our
2831 * leaf - we need to travel up the
2837 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2843 * If we got here, we never found a valid node where
2844 * the tree indicated one should be.
2846 ocfs2_error(sb, "Invalid extent tree at extent block %llu\n",
2847 (unsigned long long)blkno);
2852 blkno = path->p_node[i].bh->b_blocknr;
2860 static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2861 struct ocfs2_extent_tree *et,
2862 struct ocfs2_path *path)
2865 struct buffer_head *bh = path_leaf_bh(path);
2866 struct ocfs2_extent_list *el = path_leaf_el(path);
2868 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2871 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
2872 path_num_items(path) - 1);
2878 ocfs2_remove_empty_extent(el);
2879 ocfs2_journal_dirty(handle, bh);
2885 static int __ocfs2_rotate_tree_left(handle_t *handle,
2886 struct ocfs2_extent_tree *et,
2888 struct ocfs2_path *path,
2889 struct ocfs2_cached_dealloc_ctxt *dealloc,
2890 struct ocfs2_path **empty_extent_path)
2892 int ret, subtree_root, deleted;
2894 struct ocfs2_path *left_path = NULL;
2895 struct ocfs2_path *right_path = NULL;
2896 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2898 if (!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])))
2901 *empty_extent_path = NULL;
2903 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
2909 left_path = ocfs2_new_path_from_path(path);
2916 ocfs2_cp_path(left_path, path);
2918 right_path = ocfs2_new_path_from_path(path);
2925 while (right_cpos) {
2926 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
2932 subtree_root = ocfs2_find_subtree_root(et, left_path,
2935 trace_ocfs2_rotate_subtree(subtree_root,
2936 (unsigned long long)
2937 right_path->p_node[subtree_root].bh->b_blocknr,
2938 right_path->p_tree_depth);
2940 ret = ocfs2_extend_rotate_transaction(handle, 0,
2941 orig_credits, left_path);
2948 * Caller might still want to make changes to the
2949 * tree root, so re-add it to the journal here.
2951 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2958 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
2959 right_path, subtree_root,
2961 if (ret == -EAGAIN) {
2963 * The rotation has to temporarily stop due to
2964 * the right subtree having an empty
2965 * extent. Pass it back to the caller for a
2968 *empty_extent_path = right_path;
2978 * The subtree rotate might have removed records on
2979 * the rightmost edge. If so, then rotation is
2985 ocfs2_mv_path(left_path, right_path);
2987 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
2996 ocfs2_free_path(right_path);
2997 ocfs2_free_path(left_path);
3002 static int ocfs2_remove_rightmost_path(handle_t *handle,
3003 struct ocfs2_extent_tree *et,
3004 struct ocfs2_path *path,
3005 struct ocfs2_cached_dealloc_ctxt *dealloc)
3007 int ret, subtree_index;
3009 struct ocfs2_path *left_path = NULL;
3010 struct ocfs2_extent_block *eb;
3011 struct ocfs2_extent_list *el;
3013 ret = ocfs2_et_sanity_check(et);
3017 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
3023 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3032 * We have a path to the left of this one - it needs
3035 left_path = ocfs2_new_path_from_path(path);
3042 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
3048 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
3054 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
3056 ocfs2_unlink_subtree(handle, et, left_path, path,
3057 subtree_index, dealloc);
3058 ret = ocfs2_update_edge_lengths(handle, et, left_path);
3064 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
3065 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
3068 * 'path' is also the leftmost path which
3069 * means it must be the only one. This gets
3070 * handled differently because we want to
3071 * revert the root back to having extents
3074 ocfs2_unlink_path(handle, et, dealloc, path, 1);
3076 el = et->et_root_el;
3077 el->l_tree_depth = 0;
3078 el->l_next_free_rec = 0;
3079 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3081 ocfs2_et_set_last_eb_blk(et, 0);
3084 ocfs2_journal_dirty(handle, path_root_bh(path));
3087 ocfs2_free_path(left_path);
3091 static int ocfs2_remove_rightmost_empty_extent(struct ocfs2_super *osb,
3092 struct ocfs2_extent_tree *et,
3093 struct ocfs2_path *path,
3094 struct ocfs2_cached_dealloc_ctxt *dealloc)
3098 int credits = path->p_tree_depth * 2 + 1;
3100 handle = ocfs2_start_trans(osb, credits);
3101 if (IS_ERR(handle)) {
3102 ret = PTR_ERR(handle);
3107 ret = ocfs2_remove_rightmost_path(handle, et, path, dealloc);
3111 ocfs2_commit_trans(osb, handle);
3116 * Left rotation of btree records.
3118 * In many ways, this is (unsurprisingly) the opposite of right
3119 * rotation. We start at some non-rightmost path containing an empty
3120 * extent in the leaf block. The code works its way to the rightmost
3121 * path by rotating records to the left in every subtree.
3123 * This is used by any code which reduces the number of extent records
3124 * in a leaf. After removal, an empty record should be placed in the
3125 * leftmost list position.
3127 * This won't handle a length update of the rightmost path records if
3128 * the rightmost tree leaf record is removed so the caller is
3129 * responsible for detecting and correcting that.
3131 static int ocfs2_rotate_tree_left(handle_t *handle,
3132 struct ocfs2_extent_tree *et,
3133 struct ocfs2_path *path,
3134 struct ocfs2_cached_dealloc_ctxt *dealloc)
3136 int ret, orig_credits = handle->h_buffer_credits;
3137 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3138 struct ocfs2_extent_block *eb;
3139 struct ocfs2_extent_list *el;
3141 el = path_leaf_el(path);
3142 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3145 if (path->p_tree_depth == 0) {
3146 rightmost_no_delete:
3148 * Inline extents. This is trivially handled, so do
3151 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
3158 * Handle rightmost branch now. There's several cases:
3159 * 1) simple rotation leaving records in there. That's trivial.
3160 * 2) rotation requiring a branch delete - there's no more
3161 * records left. Two cases of this:
3162 * a) There are branches to the left.
3163 * b) This is also the leftmost (the only) branch.
3165 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3166 * 2a) we need the left branch so that we can update it with the unlink
3167 * 2b) we need to bring the root back to inline extents.
3170 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3172 if (eb->h_next_leaf_blk == 0) {
3174 * This gets a bit tricky if we're going to delete the
3175 * rightmost path. Get the other cases out of the way
3178 if (le16_to_cpu(el->l_next_free_rec) > 1)
3179 goto rightmost_no_delete;
3181 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3183 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3184 "Owner %llu has empty extent block at %llu\n",
3185 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
3186 (unsigned long long)le64_to_cpu(eb->h_blkno));
3191 * XXX: The caller can not trust "path" any more after
3192 * this as it will have been deleted. What do we do?
3194 * In theory the rotate-for-merge code will never get
3195 * here because it'll always ask for a rotate in a
3199 ret = ocfs2_remove_rightmost_path(handle, et, path,
3207 * Now we can loop, remembering the path we get from -EAGAIN
3208 * and restarting from there.
3211 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3212 dealloc, &restart_path);
3213 if (ret && ret != -EAGAIN) {
3218 while (ret == -EAGAIN) {
3219 tmp_path = restart_path;
3220 restart_path = NULL;
3222 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
3225 if (ret && ret != -EAGAIN) {
3230 ocfs2_free_path(tmp_path);
3238 ocfs2_free_path(tmp_path);
3239 ocfs2_free_path(restart_path);
3243 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3246 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3249 if (rec->e_leaf_clusters == 0) {
3251 * We consumed all of the merged-from record. An empty
3252 * extent cannot exist anywhere but the 1st array
3253 * position, so move things over if the merged-from
3254 * record doesn't occupy that position.
3256 * This creates a new empty extent so the caller
3257 * should be smart enough to have removed any existing
3261 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3262 size = index * sizeof(struct ocfs2_extent_rec);
3263 memmove(&el->l_recs[1], &el->l_recs[0], size);
3267 * Always memset - the caller doesn't check whether it
3268 * created an empty extent, so there could be junk in
3271 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3275 static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
3276 struct ocfs2_path *left_path,
3277 struct ocfs2_path **ret_right_path)
3281 struct ocfs2_path *right_path = NULL;
3282 struct ocfs2_extent_list *left_el;
3284 *ret_right_path = NULL;
3286 /* This function shouldn't be called for non-trees. */
3287 BUG_ON(left_path->p_tree_depth == 0);
3289 left_el = path_leaf_el(left_path);
3290 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3292 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3293 left_path, &right_cpos);
3299 /* This function shouldn't be called for the rightmost leaf. */
3300 BUG_ON(right_cpos == 0);
3302 right_path = ocfs2_new_path_from_path(left_path);
3309 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
3315 *ret_right_path = right_path;
3318 ocfs2_free_path(right_path);
3323 * Remove split_rec clusters from the record at index and merge them
3324 * onto the beginning of the record "next" to it.
3325 * For index < l_count - 1, the next means the extent rec at index + 1.
3326 * For index == l_count - 1, the "next" means the 1st extent rec of the
3327 * next extent block.
3329 static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
3331 struct ocfs2_extent_tree *et,
3332 struct ocfs2_extent_rec *split_rec,
3335 int ret, next_free, i;
3336 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3337 struct ocfs2_extent_rec *left_rec;
3338 struct ocfs2_extent_rec *right_rec;
3339 struct ocfs2_extent_list *right_el;
3340 struct ocfs2_path *right_path = NULL;
3341 int subtree_index = 0;
3342 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3343 struct buffer_head *bh = path_leaf_bh(left_path);
3344 struct buffer_head *root_bh = NULL;
3346 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3347 left_rec = &el->l_recs[index];
3349 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3350 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3351 /* we meet with a cross extent block merge. */
3352 ret = ocfs2_get_right_path(et, left_path, &right_path);
3358 right_el = path_leaf_el(right_path);
3359 next_free = le16_to_cpu(right_el->l_next_free_rec);
3360 BUG_ON(next_free <= 0);
3361 right_rec = &right_el->l_recs[0];
3362 if (ocfs2_is_empty_extent(right_rec)) {
3363 BUG_ON(next_free <= 1);
3364 right_rec = &right_el->l_recs[1];
3367 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3368 le16_to_cpu(left_rec->e_leaf_clusters) !=
3369 le32_to_cpu(right_rec->e_cpos));
3371 subtree_index = ocfs2_find_subtree_root(et, left_path,
3374 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3375 handle->h_buffer_credits,
3382 root_bh = left_path->p_node[subtree_index].bh;
3383 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3385 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3392 for (i = subtree_index + 1;
3393 i < path_num_items(right_path); i++) {
3394 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3401 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3410 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3411 right_rec = &el->l_recs[index + 1];
3414 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
3415 path_num_items(left_path) - 1);
3421 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3423 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3424 le64_add_cpu(&right_rec->e_blkno,
3425 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3427 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3429 ocfs2_cleanup_merge(el, index);
3431 ocfs2_journal_dirty(handle, bh);
3433 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3434 ocfs2_complete_edge_insert(handle, left_path, right_path,
3438 ocfs2_free_path(right_path);
3442 static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
3443 struct ocfs2_path *right_path,
3444 struct ocfs2_path **ret_left_path)
3448 struct ocfs2_path *left_path = NULL;
3450 *ret_left_path = NULL;
3452 /* This function shouldn't be called for non-trees. */
3453 BUG_ON(right_path->p_tree_depth == 0);
3455 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3456 right_path, &left_cpos);
3462 /* This function shouldn't be called for the leftmost leaf. */
3463 BUG_ON(left_cpos == 0);
3465 left_path = ocfs2_new_path_from_path(right_path);
3472 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
3478 *ret_left_path = left_path;
3481 ocfs2_free_path(left_path);
3486 * Remove split_rec clusters from the record at index and merge them
3487 * onto the tail of the record "before" it.
3488 * For index > 0, the "before" means the extent rec at index - 1.
3490 * For index == 0, the "before" means the last record of the previous
3491 * extent block. And there is also a situation that we may need to
3492 * remove the rightmost leaf extent block in the right_path and change
3493 * the right path to indicate the new rightmost path.
3495 static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
3497 struct ocfs2_extent_tree *et,
3498 struct ocfs2_extent_rec *split_rec,
3499 struct ocfs2_cached_dealloc_ctxt *dealloc,
3502 int ret, i, subtree_index = 0, has_empty_extent = 0;
3503 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3504 struct ocfs2_extent_rec *left_rec;
3505 struct ocfs2_extent_rec *right_rec;
3506 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3507 struct buffer_head *bh = path_leaf_bh(right_path);
3508 struct buffer_head *root_bh = NULL;
3509 struct ocfs2_path *left_path = NULL;
3510 struct ocfs2_extent_list *left_el;
3514 right_rec = &el->l_recs[index];
3516 /* we meet with a cross extent block merge. */
3517 ret = ocfs2_get_left_path(et, right_path, &left_path);
3523 left_el = path_leaf_el(left_path);
3524 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3525 le16_to_cpu(left_el->l_count));
3527 left_rec = &left_el->l_recs[
3528 le16_to_cpu(left_el->l_next_free_rec) - 1];
3529 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3530 le16_to_cpu(left_rec->e_leaf_clusters) !=
3531 le32_to_cpu(split_rec->e_cpos));
3533 subtree_index = ocfs2_find_subtree_root(et, left_path,
3536 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3537 handle->h_buffer_credits,
3544 root_bh = left_path->p_node[subtree_index].bh;
3545 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3547 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3554 for (i = subtree_index + 1;
3555 i < path_num_items(right_path); i++) {
3556 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3563 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3571 left_rec = &el->l_recs[index - 1];
3572 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3573 has_empty_extent = 1;
3576 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3577 path_num_items(right_path) - 1);
3583 if (has_empty_extent && index == 1) {
3585 * The easy case - we can just plop the record right in.
3587 *left_rec = *split_rec;
3589 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3591 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3592 le64_add_cpu(&right_rec->e_blkno,
3593 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3595 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3597 ocfs2_cleanup_merge(el, index);
3599 ocfs2_journal_dirty(handle, bh);
3601 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3604 * In the situation that the right_rec is empty and the extent
3605 * block is empty also, ocfs2_complete_edge_insert can't handle
3606 * it and we need to delete the right extent block.
3608 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3609 le16_to_cpu(el->l_next_free_rec) == 1) {
3610 /* extend credit for ocfs2_remove_rightmost_path */
3611 ret = ocfs2_extend_rotate_transaction(handle, 0,
3612 handle->h_buffer_credits,
3619 ret = ocfs2_remove_rightmost_path(handle, et,
3627 /* Now the rightmost extent block has been deleted.
3628 * So we use the new rightmost path.
3630 ocfs2_mv_path(right_path, left_path);
3633 ocfs2_complete_edge_insert(handle, left_path,
3634 right_path, subtree_index);
3637 ocfs2_free_path(left_path);
3641 static int ocfs2_try_to_merge_extent(handle_t *handle,
3642 struct ocfs2_extent_tree *et,
3643 struct ocfs2_path *path,
3645 struct ocfs2_extent_rec *split_rec,
3646 struct ocfs2_cached_dealloc_ctxt *dealloc,
3647 struct ocfs2_merge_ctxt *ctxt)
3650 struct ocfs2_extent_list *el = path_leaf_el(path);
3651 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3653 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3655 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3656 /* extend credit for ocfs2_remove_rightmost_path */
3657 ret = ocfs2_extend_rotate_transaction(handle, 0,
3658 handle->h_buffer_credits,
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 /* extend credit for ocfs2_remove_rightmost_path */
3713 ret = ocfs2_extend_rotate_transaction(handle, 0,
3714 handle->h_buffer_credits,
3721 /* The merge left us with an empty extent, remove it. */
3722 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3728 rec = &el->l_recs[split_index];
3731 * Note that we don't pass split_rec here on purpose -
3732 * we've merged it into the rec already.
3734 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3735 dealloc, split_index);
3742 /* extend credit for ocfs2_remove_rightmost_path */
3743 ret = ocfs2_extend_rotate_transaction(handle, 0,
3744 handle->h_buffer_credits,
3751 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3753 * Error from this last rotate is not critical, so
3754 * print but don't bubble it up.
3761 * Merge a record to the left or right.
3763 * 'contig_type' is relative to the existing record,
3764 * so for example, if we're "right contig", it's to
3765 * the record on the left (hence the left merge).
3767 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3768 ret = ocfs2_merge_rec_left(path, handle, et,
3776 ret = ocfs2_merge_rec_right(path, handle,
3785 if (ctxt->c_split_covers_rec) {
3786 /* extend credit for ocfs2_remove_rightmost_path */
3787 ret = ocfs2_extend_rotate_transaction(handle, 0,
3788 handle->h_buffer_credits,
3797 * The merge may have left an empty extent in
3798 * our leaf. Try to rotate it away.
3800 ret = ocfs2_rotate_tree_left(handle, et, path,
3812 static void ocfs2_subtract_from_rec(struct super_block *sb,
3813 enum ocfs2_split_type split,
3814 struct ocfs2_extent_rec *rec,
3815 struct ocfs2_extent_rec *split_rec)
3819 len_blocks = ocfs2_clusters_to_blocks(sb,
3820 le16_to_cpu(split_rec->e_leaf_clusters));
3822 if (split == SPLIT_LEFT) {
3824 * Region is on the left edge of the existing
3827 le32_add_cpu(&rec->e_cpos,
3828 le16_to_cpu(split_rec->e_leaf_clusters));
3829 le64_add_cpu(&rec->e_blkno, len_blocks);
3830 le16_add_cpu(&rec->e_leaf_clusters,
3831 -le16_to_cpu(split_rec->e_leaf_clusters));
3834 * Region is on the right edge of the existing
3837 le16_add_cpu(&rec->e_leaf_clusters,
3838 -le16_to_cpu(split_rec->e_leaf_clusters));
3843 * Do the final bits of extent record insertion at the target leaf
3844 * list. If this leaf is part of an allocation tree, it is assumed
3845 * that the tree above has been prepared.
3847 static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3848 struct ocfs2_extent_rec *insert_rec,
3849 struct ocfs2_extent_list *el,
3850 struct ocfs2_insert_type *insert)
3852 int i = insert->ins_contig_index;
3854 struct ocfs2_extent_rec *rec;
3856 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3858 if (insert->ins_split != SPLIT_NONE) {
3859 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3861 rec = &el->l_recs[i];
3862 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3863 insert->ins_split, rec,
3869 * Contiguous insert - either left or right.
3871 if (insert->ins_contig != CONTIG_NONE) {
3872 rec = &el->l_recs[i];
3873 if (insert->ins_contig == CONTIG_LEFT) {
3874 rec->e_blkno = insert_rec->e_blkno;
3875 rec->e_cpos = insert_rec->e_cpos;
3877 le16_add_cpu(&rec->e_leaf_clusters,
3878 le16_to_cpu(insert_rec->e_leaf_clusters));
3883 * Handle insert into an empty leaf.
3885 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3886 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3887 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3888 el->l_recs[0] = *insert_rec;
3889 el->l_next_free_rec = cpu_to_le16(1);
3896 if (insert->ins_appending == APPEND_TAIL) {
3897 i = le16_to_cpu(el->l_next_free_rec) - 1;
3898 rec = &el->l_recs[i];
3899 range = le32_to_cpu(rec->e_cpos)
3900 + le16_to_cpu(rec->e_leaf_clusters);
3901 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3903 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3904 le16_to_cpu(el->l_count),
3905 "owner %llu, depth %u, count %u, next free %u, "
3906 "rec.cpos %u, rec.clusters %u, "
3907 "insert.cpos %u, insert.clusters %u\n",
3908 ocfs2_metadata_cache_owner(et->et_ci),
3909 le16_to_cpu(el->l_tree_depth),
3910 le16_to_cpu(el->l_count),
3911 le16_to_cpu(el->l_next_free_rec),
3912 le32_to_cpu(el->l_recs[i].e_cpos),
3913 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3914 le32_to_cpu(insert_rec->e_cpos),
3915 le16_to_cpu(insert_rec->e_leaf_clusters));
3917 el->l_recs[i] = *insert_rec;
3918 le16_add_cpu(&el->l_next_free_rec, 1);
3924 * Ok, we have to rotate.
3926 * At this point, it is safe to assume that inserting into an
3927 * empty leaf and appending to a leaf have both been handled
3930 * This leaf needs to have space, either by the empty 1st
3931 * extent record, or by virtue of an l_next_rec < l_count.
3933 ocfs2_rotate_leaf(el, insert_rec);
3936 static void ocfs2_adjust_rightmost_records(handle_t *handle,
3937 struct ocfs2_extent_tree *et,
3938 struct ocfs2_path *path,
3939 struct ocfs2_extent_rec *insert_rec)
3941 int ret, i, next_free;
3942 struct buffer_head *bh;
3943 struct ocfs2_extent_list *el;
3944 struct ocfs2_extent_rec *rec;
3947 * Update everything except the leaf block.
3949 for (i = 0; i < path->p_tree_depth; i++) {
3950 bh = path->p_node[i].bh;
3951 el = path->p_node[i].el;
3953 next_free = le16_to_cpu(el->l_next_free_rec);
3954 if (next_free == 0) {
3955 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3956 "Owner %llu has a bad extent list\n",
3957 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
3962 rec = &el->l_recs[next_free - 1];
3964 rec->e_int_clusters = insert_rec->e_cpos;
3965 le32_add_cpu(&rec->e_int_clusters,
3966 le16_to_cpu(insert_rec->e_leaf_clusters));
3967 le32_add_cpu(&rec->e_int_clusters,
3968 -le32_to_cpu(rec->e_cpos));
3970 ocfs2_journal_dirty(handle, bh);
3974 static int ocfs2_append_rec_to_path(handle_t *handle,
3975 struct ocfs2_extent_tree *et,
3976 struct ocfs2_extent_rec *insert_rec,
3977 struct ocfs2_path *right_path,
3978 struct ocfs2_path **ret_left_path)
3981 struct ocfs2_extent_list *el;
3982 struct ocfs2_path *left_path = NULL;
3984 *ret_left_path = NULL;
3987 * This shouldn't happen for non-trees. The extent rec cluster
3988 * count manipulation below only works for interior nodes.
3990 BUG_ON(right_path->p_tree_depth == 0);
3993 * If our appending insert is at the leftmost edge of a leaf,
3994 * then we might need to update the rightmost records of the
3997 el = path_leaf_el(right_path);
3998 next_free = le16_to_cpu(el->l_next_free_rec);
3999 if (next_free == 0 ||
4000 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
4003 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
4004 right_path, &left_cpos);
4010 trace_ocfs2_append_rec_to_path(
4011 (unsigned long long)
4012 ocfs2_metadata_cache_owner(et->et_ci),
4013 le32_to_cpu(insert_rec->e_cpos),
4017 * No need to worry if the append is already in the
4021 left_path = ocfs2_new_path_from_path(right_path);
4028 ret = ocfs2_find_path(et->et_ci, left_path,
4036 * ocfs2_insert_path() will pass the left_path to the
4042 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4048 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
4050 *ret_left_path = left_path;
4054 ocfs2_free_path(left_path);
4059 static void ocfs2_split_record(struct ocfs2_extent_tree *et,
4060 struct ocfs2_path *left_path,
4061 struct ocfs2_path *right_path,
4062 struct ocfs2_extent_rec *split_rec,
4063 enum ocfs2_split_type split)
4066 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4067 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4068 struct ocfs2_extent_rec *rec, *tmprec;
4070 right_el = path_leaf_el(right_path);
4072 left_el = path_leaf_el(left_path);
4075 insert_el = right_el;
4076 index = ocfs2_search_extent_list(el, cpos);
4078 if (index == 0 && left_path) {
4079 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4082 * This typically means that the record
4083 * started in the left path but moved to the
4084 * right as a result of rotation. We either
4085 * move the existing record to the left, or we
4086 * do the later insert there.
4088 * In this case, the left path should always
4089 * exist as the rotate code will have passed
4090 * it back for a post-insert update.
4093 if (split == SPLIT_LEFT) {
4095 * It's a left split. Since we know
4096 * that the rotate code gave us an
4097 * empty extent in the left path, we
4098 * can just do the insert there.
4100 insert_el = left_el;
4103 * Right split - we have to move the
4104 * existing record over to the left
4105 * leaf. The insert will be into the
4106 * newly created empty extent in the
4109 tmprec = &right_el->l_recs[index];
4110 ocfs2_rotate_leaf(left_el, tmprec);
4113 memset(tmprec, 0, sizeof(*tmprec));
4114 index = ocfs2_search_extent_list(left_el, cpos);
4115 BUG_ON(index == -1);
4120 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4122 * Left path is easy - we can just allow the insert to
4126 insert_el = left_el;
4127 index = ocfs2_search_extent_list(el, cpos);
4128 BUG_ON(index == -1);
4131 rec = &el->l_recs[index];
4132 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4133 split, rec, split_rec);
4134 ocfs2_rotate_leaf(insert_el, split_rec);
4138 * This function only does inserts on an allocation b-tree. For tree
4139 * depth = 0, ocfs2_insert_at_leaf() is called directly.
4141 * right_path is the path we want to do the actual insert
4142 * in. left_path should only be passed in if we need to update that
4143 * portion of the tree after an edge insert.
4145 static int ocfs2_insert_path(handle_t *handle,
4146 struct ocfs2_extent_tree *et,
4147 struct ocfs2_path *left_path,
4148 struct ocfs2_path *right_path,
4149 struct ocfs2_extent_rec *insert_rec,
4150 struct ocfs2_insert_type *insert)
4152 int ret, subtree_index;
4153 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
4157 * There's a chance that left_path got passed back to
4158 * us without being accounted for in the
4159 * journal. Extend our transaction here to be sure we
4160 * can change those blocks.
4162 ret = ocfs2_extend_trans(handle, left_path->p_tree_depth);
4168 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
4176 * Pass both paths to the journal. The majority of inserts
4177 * will be touching all components anyway.
4179 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4185 if (insert->ins_split != SPLIT_NONE) {
4187 * We could call ocfs2_insert_at_leaf() for some types
4188 * of splits, but it's easier to just let one separate
4189 * function sort it all out.
4191 ocfs2_split_record(et, left_path, right_path,
4192 insert_rec, insert->ins_split);
4195 * Split might have modified either leaf and we don't
4196 * have a guarantee that the later edge insert will
4197 * dirty this for us.
4200 ocfs2_journal_dirty(handle,
4201 path_leaf_bh(left_path));
4203 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4206 ocfs2_journal_dirty(handle, leaf_bh);
4210 * The rotate code has indicated that we need to fix
4211 * up portions of the tree after the insert.
4213 * XXX: Should we extend the transaction here?
4215 subtree_index = ocfs2_find_subtree_root(et, left_path,
4217 ocfs2_complete_edge_insert(handle, left_path, right_path,
4226 static int ocfs2_do_insert_extent(handle_t *handle,
4227 struct ocfs2_extent_tree *et,
4228 struct ocfs2_extent_rec *insert_rec,
4229 struct ocfs2_insert_type *type)
4231 int ret, rotate = 0;
4233 struct ocfs2_path *right_path = NULL;
4234 struct ocfs2_path *left_path = NULL;
4235 struct ocfs2_extent_list *el;
4237 el = et->et_root_el;
4239 ret = ocfs2_et_root_journal_access(handle, et,
4240 OCFS2_JOURNAL_ACCESS_WRITE);
4246 if (le16_to_cpu(el->l_tree_depth) == 0) {
4247 ocfs2_insert_at_leaf(et, insert_rec, el, type);
4248 goto out_update_clusters;
4251 right_path = ocfs2_new_path_from_et(et);
4259 * Determine the path to start with. Rotations need the
4260 * rightmost path, everything else can go directly to the
4263 cpos = le32_to_cpu(insert_rec->e_cpos);
4264 if (type->ins_appending == APPEND_NONE &&
4265 type->ins_contig == CONTIG_NONE) {
4270 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
4277 * Rotations and appends need special treatment - they modify
4278 * parts of the tree's above them.
4280 * Both might pass back a path immediate to the left of the
4281 * one being inserted to. This will be cause
4282 * ocfs2_insert_path() to modify the rightmost records of
4283 * left_path to account for an edge insert.
4285 * XXX: When modifying this code, keep in mind that an insert
4286 * can wind up skipping both of these two special cases...
4289 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
4290 le32_to_cpu(insert_rec->e_cpos),
4291 right_path, &left_path);
4298 * ocfs2_rotate_tree_right() might have extended the
4299 * transaction without re-journaling our tree root.
4301 ret = ocfs2_et_root_journal_access(handle, et,
4302 OCFS2_JOURNAL_ACCESS_WRITE);
4307 } else if (type->ins_appending == APPEND_TAIL
4308 && type->ins_contig != CONTIG_LEFT) {
4309 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
4310 right_path, &left_path);
4317 ret = ocfs2_insert_path(handle, et, left_path, right_path,
4324 out_update_clusters:
4325 if (type->ins_split == SPLIT_NONE)
4326 ocfs2_et_update_clusters(et,
4327 le16_to_cpu(insert_rec->e_leaf_clusters));
4329 ocfs2_journal_dirty(handle, et->et_root_bh);
4332 ocfs2_free_path(left_path);
4333 ocfs2_free_path(right_path);
4338 static int ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4339 struct ocfs2_path *path,
4340 struct ocfs2_extent_list *el, int index,
4341 struct ocfs2_extent_rec *split_rec,
4342 struct ocfs2_merge_ctxt *ctxt)
4345 enum ocfs2_contig_type ret = CONTIG_NONE;
4346 u32 left_cpos, right_cpos;
4347 struct ocfs2_extent_rec *rec = NULL;
4348 struct ocfs2_extent_list *new_el;
4349 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4350 struct buffer_head *bh;
4351 struct ocfs2_extent_block *eb;
4352 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
4355 rec = &el->l_recs[index - 1];
4356 } else if (path->p_tree_depth > 0) {
4357 status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
4361 if (left_cpos != 0) {
4362 left_path = ocfs2_new_path_from_path(path);
4369 status = ocfs2_find_path(et->et_ci, left_path,
4372 goto free_left_path;
4374 new_el = path_leaf_el(left_path);
4376 if (le16_to_cpu(new_el->l_next_free_rec) !=
4377 le16_to_cpu(new_el->l_count)) {
4378 bh = path_leaf_bh(left_path);
4379 eb = (struct ocfs2_extent_block *)bh->b_data;
4381 "Extent block #%llu has an invalid l_next_free_rec of %d. It should have matched the l_count of %d\n",
4382 (unsigned long long)le64_to_cpu(eb->h_blkno),
4383 le16_to_cpu(new_el->l_next_free_rec),
4384 le16_to_cpu(new_el->l_count));
4386 goto free_left_path;
4388 rec = &new_el->l_recs[
4389 le16_to_cpu(new_el->l_next_free_rec) - 1];
4394 * We're careful to check for an empty extent record here -
4395 * the merge code will know what to do if it sees one.
4398 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4399 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4402 ret = ocfs2_et_extent_contig(et, rec, split_rec);
4407 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4408 rec = &el->l_recs[index + 1];
4409 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4410 path->p_tree_depth > 0) {
4411 status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
4413 goto free_left_path;
4415 if (right_cpos == 0)
4416 goto free_left_path;
4418 right_path = ocfs2_new_path_from_path(path);
4422 goto free_left_path;
4425 status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
4427 goto free_right_path;
4429 new_el = path_leaf_el(right_path);
4430 rec = &new_el->l_recs[0];
4431 if (ocfs2_is_empty_extent(rec)) {
4432 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4433 bh = path_leaf_bh(right_path);
4434 eb = (struct ocfs2_extent_block *)bh->b_data;
4436 "Extent block #%llu has an invalid l_next_free_rec of %d\n",
4437 (unsigned long long)le64_to_cpu(eb->h_blkno),
4438 le16_to_cpu(new_el->l_next_free_rec));
4440 goto free_right_path;
4442 rec = &new_el->l_recs[1];
4447 enum ocfs2_contig_type contig_type;
4449 contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
4451 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4452 ret = CONTIG_LEFTRIGHT;
4453 else if (ret == CONTIG_NONE)
4458 ocfs2_free_path(right_path);
4460 ocfs2_free_path(left_path);
4463 ctxt->c_contig_type = ret;
4468 static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
4469 struct ocfs2_insert_type *insert,
4470 struct ocfs2_extent_list *el,
4471 struct ocfs2_extent_rec *insert_rec)
4474 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4476 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4478 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4479 contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4481 if (contig_type != CONTIG_NONE) {
4482 insert->ins_contig_index = i;
4486 insert->ins_contig = contig_type;
4488 if (insert->ins_contig != CONTIG_NONE) {
4489 struct ocfs2_extent_rec *rec =
4490 &el->l_recs[insert->ins_contig_index];
4491 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4492 le16_to_cpu(insert_rec->e_leaf_clusters);
4495 * Caller might want us to limit the size of extents, don't
4496 * calculate contiguousness if we might exceed that limit.
4498 if (et->et_max_leaf_clusters &&
4499 (len > et->et_max_leaf_clusters))
4500 insert->ins_contig = CONTIG_NONE;
4505 * This should only be called against the righmost leaf extent list.
4507 * ocfs2_figure_appending_type() will figure out whether we'll have to
4508 * insert at the tail of the rightmost leaf.
4510 * This should also work against the root extent list for tree's with 0
4511 * depth. If we consider the root extent list to be the rightmost leaf node
4512 * then the logic here makes sense.
4514 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4515 struct ocfs2_extent_list *el,
4516 struct ocfs2_extent_rec *insert_rec)
4519 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4520 struct ocfs2_extent_rec *rec;
4522 insert->ins_appending = APPEND_NONE;
4524 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4526 if (!el->l_next_free_rec)
4527 goto set_tail_append;
4529 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4530 /* Were all records empty? */
4531 if (le16_to_cpu(el->l_next_free_rec) == 1)
4532 goto set_tail_append;
4535 i = le16_to_cpu(el->l_next_free_rec) - 1;
4536 rec = &el->l_recs[i];
4539 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4540 goto set_tail_append;
4545 insert->ins_appending = APPEND_TAIL;
4549 * Helper function called at the beginning of an insert.
4551 * This computes a few things that are commonly used in the process of
4552 * inserting into the btree:
4553 * - Whether the new extent is contiguous with an existing one.
4554 * - The current tree depth.
4555 * - Whether the insert is an appending one.
4556 * - The total # of free records in the tree.
4558 * All of the information is stored on the ocfs2_insert_type
4561 static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
4562 struct buffer_head **last_eb_bh,
4563 struct ocfs2_extent_rec *insert_rec,
4565 struct ocfs2_insert_type *insert)
4568 struct ocfs2_extent_block *eb;
4569 struct ocfs2_extent_list *el;
4570 struct ocfs2_path *path = NULL;
4571 struct buffer_head *bh = NULL;
4573 insert->ins_split = SPLIT_NONE;
4575 el = et->et_root_el;
4576 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4578 if (el->l_tree_depth) {
4580 * If we have tree depth, we read in the
4581 * rightmost extent block ahead of time as
4582 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4583 * may want it later.
4585 ret = ocfs2_read_extent_block(et->et_ci,
4586 ocfs2_et_get_last_eb_blk(et),
4592 eb = (struct ocfs2_extent_block *) bh->b_data;
4597 * Unless we have a contiguous insert, we'll need to know if
4598 * there is room left in our allocation tree for another
4601 * XXX: This test is simplistic, we can search for empty
4602 * extent records too.
4604 *free_records = le16_to_cpu(el->l_count) -
4605 le16_to_cpu(el->l_next_free_rec);
4607 if (!insert->ins_tree_depth) {
4608 ocfs2_figure_contig_type(et, insert, el, insert_rec);
4609 ocfs2_figure_appending_type(insert, el, insert_rec);
4613 path = ocfs2_new_path_from_et(et);
4621 * In the case that we're inserting past what the tree
4622 * currently accounts for, ocfs2_find_path() will return for
4623 * us the rightmost tree path. This is accounted for below in
4624 * the appending code.
4626 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
4632 el = path_leaf_el(path);
4635 * Now that we have the path, there's two things we want to determine:
4636 * 1) Contiguousness (also set contig_index if this is so)
4638 * 2) Are we doing an append? We can trivially break this up
4639 * into two types of appends: simple record append, or a
4640 * rotate inside the tail leaf.
4642 ocfs2_figure_contig_type(et, insert, el, insert_rec);
4645 * The insert code isn't quite ready to deal with all cases of
4646 * left contiguousness. Specifically, if it's an insert into
4647 * the 1st record in a leaf, it will require the adjustment of
4648 * cluster count on the last record of the path directly to it's
4649 * left. For now, just catch that case and fool the layers
4650 * above us. This works just fine for tree_depth == 0, which
4651 * is why we allow that above.
4653 if (insert->ins_contig == CONTIG_LEFT &&
4654 insert->ins_contig_index == 0)
4655 insert->ins_contig = CONTIG_NONE;
4658 * Ok, so we can simply compare against last_eb to figure out
4659 * whether the path doesn't exist. This will only happen in
4660 * the case that we're doing a tail append, so maybe we can
4661 * take advantage of that information somehow.
4663 if (ocfs2_et_get_last_eb_blk(et) ==
4664 path_leaf_bh(path)->b_blocknr) {
4666 * Ok, ocfs2_find_path() returned us the rightmost
4667 * tree path. This might be an appending insert. There are
4669 * 1) We're doing a true append at the tail:
4670 * -This might even be off the end of the leaf
4671 * 2) We're "appending" by rotating in the tail
4673 ocfs2_figure_appending_type(insert, el, insert_rec);
4677 ocfs2_free_path(path);
4687 * Insert an extent into a btree.
4689 * The caller needs to update the owning btree's cluster count.
4691 int ocfs2_insert_extent(handle_t *handle,
4692 struct ocfs2_extent_tree *et,
4697 struct ocfs2_alloc_context *meta_ac)
4700 int uninitialized_var(free_records);
4701 struct buffer_head *last_eb_bh = NULL;
4702 struct ocfs2_insert_type insert = {0, };
4703 struct ocfs2_extent_rec rec;
4705 trace_ocfs2_insert_extent_start(
4706 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
4707 cpos, new_clusters);
4709 memset(&rec, 0, sizeof(rec));
4710 rec.e_cpos = cpu_to_le32(cpos);
4711 rec.e_blkno = cpu_to_le64(start_blk);
4712 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4713 rec.e_flags = flags;
4714 status = ocfs2_et_insert_check(et, &rec);
4720 status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
4721 &free_records, &insert);
4727 trace_ocfs2_insert_extent(insert.ins_appending, insert.ins_contig,
4728 insert.ins_contig_index, free_records,
4729 insert.ins_tree_depth);
4731 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4732 status = ocfs2_grow_tree(handle, et,
4733 &insert.ins_tree_depth, &last_eb_bh,
4741 /* Finally, we can add clusters. This might rotate the tree for us. */
4742 status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
4746 ocfs2_et_extent_map_insert(et, &rec);
4755 * Allcate and add clusters into the extent b-tree.
4756 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4757 * The extent b-tree's root is specified by et, and
4758 * it is not limited to the file storage. Any extent tree can use this
4759 * function if it implements the proper ocfs2_extent_tree.
4761 int ocfs2_add_clusters_in_btree(handle_t *handle,
4762 struct ocfs2_extent_tree *et,
4763 u32 *logical_offset,
4764 u32 clusters_to_add,
4766 struct ocfs2_alloc_context *data_ac,
4767 struct ocfs2_alloc_context *meta_ac,
4768 enum ocfs2_alloc_restarted *reason_ret)
4770 int status = 0, err = 0;
4773 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4774 u32 bit_off, num_bits;
4777 struct ocfs2_super *osb =
4778 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
4780 BUG_ON(!clusters_to_add);
4783 flags = OCFS2_EXT_UNWRITTEN;
4785 free_extents = ocfs2_num_free_extents(et);
4786 if (free_extents < 0) {
4787 status = free_extents;
4792 /* there are two cases which could cause us to EAGAIN in the
4793 * we-need-more-metadata case:
4794 * 1) we haven't reserved *any*
4795 * 2) we are so fragmented, we've needed to add metadata too
4797 if (!free_extents && !meta_ac) {
4800 reason = RESTART_META;
4802 } else if ((!free_extents)
4803 && (ocfs2_alloc_context_bits_left(meta_ac)
4804 < ocfs2_extend_meta_needed(et->et_root_el))) {
4807 reason = RESTART_META;
4811 status = __ocfs2_claim_clusters(handle, data_ac, 1,
4812 clusters_to_add, &bit_off, &num_bits);
4814 if (status != -ENOSPC)
4819 BUG_ON(num_bits > clusters_to_add);
4821 /* reserve our write early -- insert_extent may update the tree root */
4822 status = ocfs2_et_root_journal_access(handle, et,
4823 OCFS2_JOURNAL_ACCESS_WRITE);
4830 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4831 trace_ocfs2_add_clusters_in_btree(
4832 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
4834 status = ocfs2_insert_extent(handle, et, *logical_offset, block,
4835 num_bits, flags, meta_ac);
4842 ocfs2_journal_dirty(handle, et->et_root_bh);
4844 clusters_to_add -= num_bits;
4845 *logical_offset += num_bits;
4847 if (clusters_to_add) {
4848 err = clusters_to_add;
4850 reason = RESTART_TRANS;
4855 if (data_ac->ac_which == OCFS2_AC_USE_LOCAL)
4856 ocfs2_free_local_alloc_bits(osb, handle, data_ac,
4859 ocfs2_free_clusters(handle,
4862 ocfs2_clusters_to_blocks(osb->sb, bit_off),
4868 *reason_ret = reason;
4869 trace_ocfs2_add_clusters_in_btree_ret(status, reason, err);
4873 static void ocfs2_make_right_split_rec(struct super_block *sb,
4874 struct ocfs2_extent_rec *split_rec,
4876 struct ocfs2_extent_rec *rec)
4878 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4879 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4881 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4883 split_rec->e_cpos = cpu_to_le32(cpos);
4884 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4886 split_rec->e_blkno = rec->e_blkno;
4887 le64_add_cpu(&split_rec->e_blkno,
4888 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4890 split_rec->e_flags = rec->e_flags;
4893 static int ocfs2_split_and_insert(handle_t *handle,
4894 struct ocfs2_extent_tree *et,
4895 struct ocfs2_path *path,
4896 struct buffer_head **last_eb_bh,
4898 struct ocfs2_extent_rec *orig_split_rec,
4899 struct ocfs2_alloc_context *meta_ac)
4902 unsigned int insert_range, rec_range, do_leftright = 0;
4903 struct ocfs2_extent_rec tmprec;
4904 struct ocfs2_extent_list *rightmost_el;
4905 struct ocfs2_extent_rec rec;
4906 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4907 struct ocfs2_insert_type insert;
4908 struct ocfs2_extent_block *eb;
4912 * Store a copy of the record on the stack - it might move
4913 * around as the tree is manipulated below.
4915 rec = path_leaf_el(path)->l_recs[split_index];
4917 rightmost_el = et->et_root_el;
4919 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4921 BUG_ON(!(*last_eb_bh));
4922 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4923 rightmost_el = &eb->h_list;
4926 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4927 le16_to_cpu(rightmost_el->l_count)) {
4928 ret = ocfs2_grow_tree(handle, et,
4929 &depth, last_eb_bh, meta_ac);
4936 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4937 insert.ins_appending = APPEND_NONE;
4938 insert.ins_contig = CONTIG_NONE;
4939 insert.ins_tree_depth = depth;
4941 insert_range = le32_to_cpu(split_rec.e_cpos) +
4942 le16_to_cpu(split_rec.e_leaf_clusters);
4943 rec_range = le32_to_cpu(rec.e_cpos) +
4944 le16_to_cpu(rec.e_leaf_clusters);
4946 if (split_rec.e_cpos == rec.e_cpos) {
4947 insert.ins_split = SPLIT_LEFT;
4948 } else if (insert_range == rec_range) {
4949 insert.ins_split = SPLIT_RIGHT;
4952 * Left/right split. We fake this as a right split
4953 * first and then make a second pass as a left split.
4955 insert.ins_split = SPLIT_RIGHT;
4957 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4958 &tmprec, insert_range, &rec);
4962 BUG_ON(do_leftright);
4966 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
4972 if (do_leftright == 1) {
4974 struct ocfs2_extent_list *el;
4977 split_rec = *orig_split_rec;
4979 ocfs2_reinit_path(path, 1);
4981 cpos = le32_to_cpu(split_rec.e_cpos);
4982 ret = ocfs2_find_path(et->et_ci, path, cpos);
4988 el = path_leaf_el(path);
4989 split_index = ocfs2_search_extent_list(el, cpos);
4990 if (split_index == -1) {
4991 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
4992 "Owner %llu has an extent at cpos %u which can no longer be found\n",
4993 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5005 static int ocfs2_replace_extent_rec(handle_t *handle,
5006 struct ocfs2_extent_tree *et,
5007 struct ocfs2_path *path,
5008 struct ocfs2_extent_list *el,
5010 struct ocfs2_extent_rec *split_rec)
5014 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
5015 path_num_items(path) - 1);
5021 el->l_recs[split_index] = *split_rec;
5023 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5029 * Split part or all of the extent record at split_index in the leaf
5030 * pointed to by path. Merge with the contiguous extent record if needed.
5032 * Care is taken to handle contiguousness so as to not grow the tree.
5034 * meta_ac is not strictly necessary - we only truly need it if growth
5035 * of the tree is required. All other cases will degrade into a less
5036 * optimal tree layout.
5038 * last_eb_bh should be the rightmost leaf block for any extent
5039 * btree. Since a split may grow the tree or a merge might shrink it,
5040 * the caller cannot trust the contents of that buffer after this call.
5042 * This code is optimized for readability - several passes might be
5043 * made over certain portions of the tree. All of those blocks will
5044 * have been brought into cache (and pinned via the journal), so the
5045 * extra overhead is not expressed in terms of disk reads.
5047 int ocfs2_split_extent(handle_t *handle,
5048 struct ocfs2_extent_tree *et,
5049 struct ocfs2_path *path,
5051 struct ocfs2_extent_rec *split_rec,
5052 struct ocfs2_alloc_context *meta_ac,
5053 struct ocfs2_cached_dealloc_ctxt *dealloc)
5056 struct ocfs2_extent_list *el = path_leaf_el(path);
5057 struct buffer_head *last_eb_bh = NULL;
5058 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5059 struct ocfs2_merge_ctxt ctxt;
5060 struct ocfs2_extent_list *rightmost_el;
5062 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5063 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5064 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5070 ret = ocfs2_figure_merge_contig_type(et, path, el,
5080 * The core merge / split code wants to know how much room is
5081 * left in this allocation tree, so we pass the
5082 * rightmost extent list.
5084 if (path->p_tree_depth) {
5085 struct ocfs2_extent_block *eb;
5087 ret = ocfs2_read_extent_block(et->et_ci,
5088 ocfs2_et_get_last_eb_blk(et),
5095 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5096 rightmost_el = &eb->h_list;
5098 rightmost_el = path_root_el(path);
5100 if (rec->e_cpos == split_rec->e_cpos &&
5101 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5102 ctxt.c_split_covers_rec = 1;
5104 ctxt.c_split_covers_rec = 0;
5106 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5108 trace_ocfs2_split_extent(split_index, ctxt.c_contig_type,
5109 ctxt.c_has_empty_extent,
5110 ctxt.c_split_covers_rec);
5112 if (ctxt.c_contig_type == CONTIG_NONE) {
5113 if (ctxt.c_split_covers_rec)
5114 ret = ocfs2_replace_extent_rec(handle, et, path, el,
5115 split_index, split_rec);
5117 ret = ocfs2_split_and_insert(handle, et, path,
5118 &last_eb_bh, split_index,
5119 split_rec, meta_ac);
5123 ret = ocfs2_try_to_merge_extent(handle, et, path,
5124 split_index, split_rec,
5136 * Change the flags of the already-existing extent at cpos for len clusters.
5138 * new_flags: the flags we want to set.
5139 * clear_flags: the flags we want to clear.
5140 * phys: the new physical offset we want this new extent starts from.
5142 * If the existing extent is larger than the request, initiate a
5143 * split. An attempt will be made at merging with adjacent extents.
5145 * The caller is responsible for passing down meta_ac if we'll need it.
5147 int ocfs2_change_extent_flag(handle_t *handle,
5148 struct ocfs2_extent_tree *et,
5149 u32 cpos, u32 len, u32 phys,
5150 struct ocfs2_alloc_context *meta_ac,
5151 struct ocfs2_cached_dealloc_ctxt *dealloc,
5152 int new_flags, int clear_flags)
5155 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5156 u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
5157 struct ocfs2_extent_rec split_rec;
5158 struct ocfs2_path *left_path = NULL;
5159 struct ocfs2_extent_list *el;
5160 struct ocfs2_extent_rec *rec;
5162 left_path = ocfs2_new_path_from_et(et);
5169 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
5174 el = path_leaf_el(left_path);
5176 index = ocfs2_search_extent_list(el, cpos);
5179 "Owner %llu has an extent at cpos %u which can no longer be found\n",
5180 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5187 rec = &el->l_recs[index];
5188 if (new_flags && (rec->e_flags & new_flags)) {
5189 mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
5190 "extent that already had them\n",
5191 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5196 if (clear_flags && !(rec->e_flags & clear_flags)) {
5197 mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
5198 "extent that didn't have them\n",
5199 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5204 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5205 split_rec.e_cpos = cpu_to_le32(cpos);
5206 split_rec.e_leaf_clusters = cpu_to_le16(len);
5207 split_rec.e_blkno = cpu_to_le64(start_blkno);
5208 split_rec.e_flags = rec->e_flags;
5210 split_rec.e_flags |= new_flags;
5212 split_rec.e_flags &= ~clear_flags;
5214 ret = ocfs2_split_extent(handle, et, left_path,
5215 index, &split_rec, meta_ac,
5221 ocfs2_free_path(left_path);
5227 * Mark the already-existing extent at cpos as written for len clusters.
5228 * This removes the unwritten extent flag.
5230 * If the existing extent is larger than the request, initiate a
5231 * split. An attempt will be made at merging with adjacent extents.
5233 * The caller is responsible for passing down meta_ac if we'll need it.
5235 int ocfs2_mark_extent_written(struct inode *inode,
5236 struct ocfs2_extent_tree *et,
5237 handle_t *handle, u32 cpos, u32 len, u32 phys,
5238 struct ocfs2_alloc_context *meta_ac,
5239 struct ocfs2_cached_dealloc_ctxt *dealloc)
5243 trace_ocfs2_mark_extent_written(
5244 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5247 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5248 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents that are being written to, but the feature bit is not set in the super block\n",
5249 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5255 * XXX: This should be fixed up so that we just re-insert the
5256 * next extent records.
5258 ocfs2_et_extent_map_truncate(et, 0);
5260 ret = ocfs2_change_extent_flag(handle, et, cpos,
5261 len, phys, meta_ac, dealloc,
5262 0, OCFS2_EXT_UNWRITTEN);
5270 static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5271 struct ocfs2_path *path,
5272 int index, u32 new_range,
5273 struct ocfs2_alloc_context *meta_ac)
5275 int ret, depth, credits;
5276 struct buffer_head *last_eb_bh = NULL;
5277 struct ocfs2_extent_block *eb;
5278 struct ocfs2_extent_list *rightmost_el, *el;
5279 struct ocfs2_extent_rec split_rec;
5280 struct ocfs2_extent_rec *rec;
5281 struct ocfs2_insert_type insert;
5284 * Setup the record to split before we grow the tree.
5286 el = path_leaf_el(path);
5287 rec = &el->l_recs[index];
5288 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5289 &split_rec, new_range, rec);
5291 depth = path->p_tree_depth;
5293 ret = ocfs2_read_extent_block(et->et_ci,
5294 ocfs2_et_get_last_eb_blk(et),
5301 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5302 rightmost_el = &eb->h_list;
5304 rightmost_el = path_leaf_el(path);
5306 credits = path->p_tree_depth +
5307 ocfs2_extend_meta_needed(et->et_root_el);
5308 ret = ocfs2_extend_trans(handle, credits);
5314 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5315 le16_to_cpu(rightmost_el->l_count)) {
5316 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
5324 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5325 insert.ins_appending = APPEND_NONE;
5326 insert.ins_contig = CONTIG_NONE;
5327 insert.ins_split = SPLIT_RIGHT;
5328 insert.ins_tree_depth = depth;
5330 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
5339 static int ocfs2_truncate_rec(handle_t *handle,
5340 struct ocfs2_extent_tree *et,
5341 struct ocfs2_path *path, int index,
5342 struct ocfs2_cached_dealloc_ctxt *dealloc,
5346 u32 left_cpos, rec_range, trunc_range;
5347 int is_rightmost_tree_rec = 0;
5348 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5349 struct ocfs2_path *left_path = NULL;
5350 struct ocfs2_extent_list *el = path_leaf_el(path);
5351 struct ocfs2_extent_rec *rec;
5352 struct ocfs2_extent_block *eb;
5354 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5355 /* extend credit for ocfs2_remove_rightmost_path */
5356 ret = ocfs2_extend_rotate_transaction(handle, 0,
5357 handle->h_buffer_credits,
5364 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5373 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5374 path->p_tree_depth) {
5376 * Check whether this is the rightmost tree record. If
5377 * we remove all of this record or part of its right
5378 * edge then an update of the record lengths above it
5381 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5382 if (eb->h_next_leaf_blk == 0)
5383 is_rightmost_tree_rec = 1;
5386 rec = &el->l_recs[index];
5387 if (index == 0 && path->p_tree_depth &&
5388 le32_to_cpu(rec->e_cpos) == cpos) {
5390 * Changing the leftmost offset (via partial or whole
5391 * record truncate) of an interior (or rightmost) path
5392 * means we have to update the subtree that is formed
5393 * by this leaf and the one to it's left.
5395 * There are two cases we can skip:
5396 * 1) Path is the leftmost one in our btree.
5397 * 2) The leaf is rightmost and will be empty after
5398 * we remove the extent record - the rotate code
5399 * knows how to update the newly formed edge.
5402 ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
5408 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5409 left_path = ocfs2_new_path_from_path(path);
5416 ret = ocfs2_find_path(et->et_ci, left_path,
5425 ret = ocfs2_extend_rotate_transaction(handle, 0,
5426 handle->h_buffer_credits,
5433 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
5439 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
5445 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5446 trunc_range = cpos + len;
5448 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5451 memset(rec, 0, sizeof(*rec));
5452 ocfs2_cleanup_merge(el, index);
5454 next_free = le16_to_cpu(el->l_next_free_rec);
5455 if (is_rightmost_tree_rec && next_free > 1) {
5457 * We skip the edge update if this path will
5458 * be deleted by the rotate code.
5460 rec = &el->l_recs[next_free - 1];
5461 ocfs2_adjust_rightmost_records(handle, et, path,
5464 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5465 /* Remove leftmost portion of the record. */
5466 le32_add_cpu(&rec->e_cpos, len);
5467 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5468 le16_add_cpu(&rec->e_leaf_clusters, -len);
5469 } else if (rec_range == trunc_range) {
5470 /* Remove rightmost portion of the record */
5471 le16_add_cpu(&rec->e_leaf_clusters, -len);
5472 if (is_rightmost_tree_rec)
5473 ocfs2_adjust_rightmost_records(handle, et, path, rec);
5475 /* Caller should have trapped this. */
5476 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5478 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5479 le32_to_cpu(rec->e_cpos),
5480 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5487 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
5488 ocfs2_complete_edge_insert(handle, left_path, path,
5492 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5494 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5501 ocfs2_free_path(left_path);
5505 int ocfs2_remove_extent(handle_t *handle,
5506 struct ocfs2_extent_tree *et,
5508 struct ocfs2_alloc_context *meta_ac,
5509 struct ocfs2_cached_dealloc_ctxt *dealloc)
5512 u32 rec_range, trunc_range;
5513 struct ocfs2_extent_rec *rec;
5514 struct ocfs2_extent_list *el;
5515 struct ocfs2_path *path = NULL;
5518 * XXX: Why are we truncating to 0 instead of wherever this
5521 ocfs2_et_extent_map_truncate(et, 0);
5523 path = ocfs2_new_path_from_et(et);
5530 ret = ocfs2_find_path(et->et_ci, path, cpos);
5536 el = path_leaf_el(path);
5537 index = ocfs2_search_extent_list(el, cpos);
5539 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5540 "Owner %llu has an extent at cpos %u which can no longer be found\n",
5541 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5548 * We have 3 cases of extent removal:
5549 * 1) Range covers the entire extent rec
5550 * 2) Range begins or ends on one edge of the extent rec
5551 * 3) Range is in the middle of the extent rec (no shared edges)
5553 * For case 1 we remove the extent rec and left rotate to
5556 * For case 2 we just shrink the existing extent rec, with a
5557 * tree update if the shrinking edge is also the edge of an
5560 * For case 3 we do a right split to turn the extent rec into
5561 * something case 2 can handle.
5563 rec = &el->l_recs[index];
5564 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5565 trunc_range = cpos + len;
5567 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5569 trace_ocfs2_remove_extent(
5570 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5571 cpos, len, index, le32_to_cpu(rec->e_cpos),
5572 ocfs2_rec_clusters(el, rec));
5574 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5575 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5582 ret = ocfs2_split_tree(handle, et, path, index,
5583 trunc_range, meta_ac);
5590 * The split could have manipulated the tree enough to
5591 * move the record location, so we have to look for it again.
5593 ocfs2_reinit_path(path, 1);
5595 ret = ocfs2_find_path(et->et_ci, path, cpos);
5601 el = path_leaf_el(path);
5602 index = ocfs2_search_extent_list(el, cpos);
5604 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5605 "Owner %llu: split at cpos %u lost record\n",
5606 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5613 * Double check our values here. If anything is fishy,
5614 * it's easier to catch it at the top level.
5616 rec = &el->l_recs[index];
5617 rec_range = le32_to_cpu(rec->e_cpos) +
5618 ocfs2_rec_clusters(el, rec);
5619 if (rec_range != trunc_range) {
5620 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5621 "Owner %llu: error after split at cpos %u trunc len %u, existing record is (%u,%u)\n",
5622 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5623 cpos, len, le32_to_cpu(rec->e_cpos),
5624 ocfs2_rec_clusters(el, rec));
5629 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5638 ocfs2_free_path(path);
5643 * ocfs2_reserve_blocks_for_rec_trunc() would look basically the
5644 * same as ocfs2_lock_alloctors(), except for it accepts a blocks
5645 * number to reserve some extra blocks, and it only handles meta
5648 * Currently, only ocfs2_remove_btree_range() uses it for truncating
5649 * and punching holes.
5651 static int ocfs2_reserve_blocks_for_rec_trunc(struct inode *inode,
5652 struct ocfs2_extent_tree *et,
5653 u32 extents_to_split,
5654 struct ocfs2_alloc_context **ac,
5657 int ret = 0, num_free_extents;
5658 unsigned int max_recs_needed = 2 * extents_to_split;
5659 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5663 num_free_extents = ocfs2_num_free_extents(et);
5664 if (num_free_extents < 0) {
5665 ret = num_free_extents;
5670 if (!num_free_extents ||
5671 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed))
5672 extra_blocks += ocfs2_extend_meta_needed(et->et_root_el);
5675 ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, ac);
5686 ocfs2_free_alloc_context(*ac);
5694 int ocfs2_remove_btree_range(struct inode *inode,
5695 struct ocfs2_extent_tree *et,
5696 u32 cpos, u32 phys_cpos, u32 len, int flags,
5697 struct ocfs2_cached_dealloc_ctxt *dealloc,
5698 u64 refcount_loc, bool refcount_tree_locked)
5700 int ret, credits = 0, extra_blocks = 0;
5701 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5702 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5703 struct inode *tl_inode = osb->osb_tl_inode;
5705 struct ocfs2_alloc_context *meta_ac = NULL;
5706 struct ocfs2_refcount_tree *ref_tree = NULL;
5708 if ((flags & OCFS2_EXT_REFCOUNTED) && len) {
5709 BUG_ON(!ocfs2_is_refcount_inode(inode));
5711 if (!refcount_tree_locked) {
5712 ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
5720 ret = ocfs2_prepare_refcount_change_for_del(inode,
5732 ret = ocfs2_reserve_blocks_for_rec_trunc(inode, et, 1, &meta_ac,
5739 inode_lock(tl_inode);
5741 if (ocfs2_truncate_log_needs_flush(osb)) {
5742 ret = __ocfs2_flush_truncate_log(osb);
5749 handle = ocfs2_start_trans(osb,
5750 ocfs2_remove_extent_credits(osb->sb) + credits);
5751 if (IS_ERR(handle)) {
5752 ret = PTR_ERR(handle);
5757 ret = ocfs2_et_root_journal_access(handle, et,
5758 OCFS2_JOURNAL_ACCESS_WRITE);
5764 dquot_free_space_nodirty(inode,
5765 ocfs2_clusters_to_bytes(inode->i_sb, len));
5767 ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
5773 ocfs2_et_update_clusters(et, -len);
5774 ocfs2_update_inode_fsync_trans(handle, inode, 1);
5776 ocfs2_journal_dirty(handle, et->et_root_bh);
5779 if (flags & OCFS2_EXT_REFCOUNTED)
5780 ret = ocfs2_decrease_refcount(inode, handle,
5781 ocfs2_blocks_to_clusters(osb->sb,
5786 ret = ocfs2_truncate_log_append(osb, handle,
5794 ocfs2_commit_trans(osb, handle);
5796 inode_unlock(tl_inode);
5799 ocfs2_free_alloc_context(meta_ac);
5802 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
5807 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5809 struct buffer_head *tl_bh = osb->osb_tl_bh;
5810 struct ocfs2_dinode *di;
5811 struct ocfs2_truncate_log *tl;
5813 di = (struct ocfs2_dinode *) tl_bh->b_data;
5814 tl = &di->id2.i_dealloc;
5816 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5817 "slot %d, invalid truncate log parameters: used = "
5818 "%u, count = %u\n", osb->slot_num,
5819 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5820 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5823 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5824 unsigned int new_start)
5826 unsigned int tail_index;
5827 unsigned int current_tail;
5829 /* No records, nothing to coalesce */
5830 if (!le16_to_cpu(tl->tl_used))
5833 tail_index = le16_to_cpu(tl->tl_used) - 1;
5834 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5835 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5837 return current_tail == new_start;
5840 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5843 unsigned int num_clusters)
5846 unsigned int start_cluster, tl_count;
5847 struct inode *tl_inode = osb->osb_tl_inode;
5848 struct buffer_head *tl_bh = osb->osb_tl_bh;
5849 struct ocfs2_dinode *di;
5850 struct ocfs2_truncate_log *tl;
5852 BUG_ON(inode_trylock(tl_inode));
5854 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5856 di = (struct ocfs2_dinode *) tl_bh->b_data;
5858 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5859 * by the underlying call to ocfs2_read_inode_block(), so any
5860 * corruption is a code bug */
5861 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5863 tl = &di->id2.i_dealloc;
5864 tl_count = le16_to_cpu(tl->tl_count);
5865 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5867 "Truncate record count on #%llu invalid "
5868 "wanted %u, actual %u\n",
5869 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5870 ocfs2_truncate_recs_per_inode(osb->sb),
5871 le16_to_cpu(tl->tl_count));
5873 /* Caller should have known to flush before calling us. */
5874 index = le16_to_cpu(tl->tl_used);
5875 if (index >= tl_count) {
5881 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5882 OCFS2_JOURNAL_ACCESS_WRITE);
5888 trace_ocfs2_truncate_log_append(
5889 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index,
5890 start_cluster, num_clusters);
5891 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5893 * Move index back to the record we are coalescing with.
5894 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5898 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5899 trace_ocfs2_truncate_log_append(
5900 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5901 index, le32_to_cpu(tl->tl_recs[index].t_start),
5904 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5905 tl->tl_used = cpu_to_le16(index + 1);
5907 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5909 ocfs2_journal_dirty(handle, tl_bh);
5911 osb->truncated_clusters += num_clusters;
5916 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5917 struct inode *data_alloc_inode,
5918 struct buffer_head *data_alloc_bh)
5922 unsigned int num_clusters;
5924 struct ocfs2_truncate_rec rec;
5925 struct ocfs2_dinode *di;
5926 struct ocfs2_truncate_log *tl;
5927 struct inode *tl_inode = osb->osb_tl_inode;
5928 struct buffer_head *tl_bh = osb->osb_tl_bh;
5931 di = (struct ocfs2_dinode *) tl_bh->b_data;
5932 tl = &di->id2.i_dealloc;
5933 i = le16_to_cpu(tl->tl_used) - 1;
5935 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5936 if (IS_ERR(handle)) {
5937 status = PTR_ERR(handle);
5942 /* Caller has given us at least enough credits to
5943 * update the truncate log dinode */
5944 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5945 OCFS2_JOURNAL_ACCESS_WRITE);
5951 tl->tl_used = cpu_to_le16(i);
5953 ocfs2_journal_dirty(handle, tl_bh);
5955 rec = tl->tl_recs[i];
5956 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5957 le32_to_cpu(rec.t_start));
5958 num_clusters = le32_to_cpu(rec.t_clusters);
5960 /* if start_blk is not set, we ignore the record as
5963 trace_ocfs2_replay_truncate_records(
5964 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5965 i, le32_to_cpu(rec.t_start), num_clusters);
5967 status = ocfs2_free_clusters(handle, data_alloc_inode,
5968 data_alloc_bh, start_blk,
5976 ocfs2_commit_trans(osb, handle);
5980 osb->truncated_clusters = 0;
5986 /* Expects you to already be holding tl_inode->i_mutex */
5987 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5990 unsigned int num_to_flush;
5991 struct inode *tl_inode = osb->osb_tl_inode;
5992 struct inode *data_alloc_inode = NULL;
5993 struct buffer_head *tl_bh = osb->osb_tl_bh;
5994 struct buffer_head *data_alloc_bh = NULL;
5995 struct ocfs2_dinode *di;
5996 struct ocfs2_truncate_log *tl;
5998 BUG_ON(inode_trylock(tl_inode));
6000 di = (struct ocfs2_dinode *) tl_bh->b_data;
6002 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
6003 * by the underlying call to ocfs2_read_inode_block(), so any
6004 * corruption is a code bug */
6005 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6007 tl = &di->id2.i_dealloc;
6008 num_to_flush = le16_to_cpu(tl->tl_used);
6009 trace_ocfs2_flush_truncate_log(
6010 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
6012 if (!num_to_flush) {
6017 data_alloc_inode = ocfs2_get_system_file_inode(osb,
6018 GLOBAL_BITMAP_SYSTEM_INODE,
6019 OCFS2_INVALID_SLOT);
6020 if (!data_alloc_inode) {
6022 mlog(ML_ERROR, "Could not get bitmap inode!\n");
6026 inode_lock(data_alloc_inode);
6028 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
6034 status = ocfs2_replay_truncate_records(osb, data_alloc_inode,
6039 brelse(data_alloc_bh);
6040 ocfs2_inode_unlock(data_alloc_inode, 1);
6043 inode_unlock(data_alloc_inode);
6044 iput(data_alloc_inode);
6050 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
6053 struct inode *tl_inode = osb->osb_tl_inode;
6055 inode_lock(tl_inode);
6056 status = __ocfs2_flush_truncate_log(osb);
6057 inode_unlock(tl_inode);
6062 static void ocfs2_truncate_log_worker(struct work_struct *work)
6065 struct ocfs2_super *osb =
6066 container_of(work, struct ocfs2_super,
6067 osb_truncate_log_wq.work);
6069 status = ocfs2_flush_truncate_log(osb);
6073 ocfs2_init_steal_slots(osb);
6076 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
6077 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
6080 if (osb->osb_tl_inode &&
6081 atomic_read(&osb->osb_tl_disable) == 0) {
6082 /* We want to push off log flushes while truncates are
6085 cancel_delayed_work(&osb->osb_truncate_log_wq);
6087 queue_delayed_work(osb->ocfs2_wq, &osb->osb_truncate_log_wq,
6088 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
6093 * Try to flush truncate logs if we can free enough clusters from it.
6094 * As for return value, "< 0" means error, "0" no space and "1" means
6095 * we have freed enough spaces and let the caller try to allocate again.
6097 int ocfs2_try_to_free_truncate_log(struct ocfs2_super *osb,
6098 unsigned int needed)
6102 unsigned int truncated_clusters;
6104 inode_lock(osb->osb_tl_inode);
6105 truncated_clusters = osb->truncated_clusters;
6106 inode_unlock(osb->osb_tl_inode);
6109 * Check whether we can succeed in allocating if we free
6112 if (truncated_clusters < needed)
6115 ret = ocfs2_flush_truncate_log(osb);
6121 if (jbd2_journal_start_commit(osb->journal->j_journal, &target)) {
6122 jbd2_log_wait_commit(osb->journal->j_journal, target);
6129 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
6131 struct inode **tl_inode,
6132 struct buffer_head **tl_bh)
6135 struct inode *inode = NULL;
6136 struct buffer_head *bh = NULL;
6138 inode = ocfs2_get_system_file_inode(osb,
6139 TRUNCATE_LOG_SYSTEM_INODE,
6143 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
6147 status = ocfs2_read_inode_block(inode, &bh);
6160 /* called during the 1st stage of node recovery. we stamp a clean
6161 * truncate log and pass back a copy for processing later. if the
6162 * truncate log does not require processing, a *tl_copy is set to
6164 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6166 struct ocfs2_dinode **tl_copy)
6169 struct inode *tl_inode = NULL;
6170 struct buffer_head *tl_bh = NULL;
6171 struct ocfs2_dinode *di;
6172 struct ocfs2_truncate_log *tl;
6176 trace_ocfs2_begin_truncate_log_recovery(slot_num);
6178 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6184 di = (struct ocfs2_dinode *) tl_bh->b_data;
6186 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6187 * validated by the underlying call to ocfs2_read_inode_block(),
6188 * so any corruption is a code bug */
6189 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6191 tl = &di->id2.i_dealloc;
6192 if (le16_to_cpu(tl->tl_used)) {
6193 trace_ocfs2_truncate_log_recovery_num(le16_to_cpu(tl->tl_used));
6195 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6202 /* Assuming the write-out below goes well, this copy
6203 * will be passed back to recovery for processing. */
6204 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6206 /* All we need to do to clear the truncate log is set
6210 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
6211 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
6231 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6232 struct ocfs2_dinode *tl_copy)
6236 unsigned int clusters, num_recs, start_cluster;
6239 struct inode *tl_inode = osb->osb_tl_inode;
6240 struct ocfs2_truncate_log *tl;
6242 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6243 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6247 tl = &tl_copy->id2.i_dealloc;
6248 num_recs = le16_to_cpu(tl->tl_used);
6249 trace_ocfs2_complete_truncate_log_recovery(
6250 (unsigned long long)le64_to_cpu(tl_copy->i_blkno),
6253 inode_lock(tl_inode);
6254 for(i = 0; i < num_recs; i++) {
6255 if (ocfs2_truncate_log_needs_flush(osb)) {
6256 status = __ocfs2_flush_truncate_log(osb);
6263 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6264 if (IS_ERR(handle)) {
6265 status = PTR_ERR(handle);
6270 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6271 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6272 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6274 status = ocfs2_truncate_log_append(osb, handle,
6275 start_blk, clusters);
6276 ocfs2_commit_trans(osb, handle);
6284 inode_unlock(tl_inode);
6289 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6292 struct inode *tl_inode = osb->osb_tl_inode;
6294 atomic_set(&osb->osb_tl_disable, 1);
6297 cancel_delayed_work(&osb->osb_truncate_log_wq);
6298 flush_workqueue(osb->ocfs2_wq);
6300 status = ocfs2_flush_truncate_log(osb);
6304 brelse(osb->osb_tl_bh);
6305 iput(osb->osb_tl_inode);
6309 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6312 struct inode *tl_inode = NULL;
6313 struct buffer_head *tl_bh = NULL;
6315 status = ocfs2_get_truncate_log_info(osb,
6322 /* ocfs2_truncate_log_shutdown keys on the existence of
6323 * osb->osb_tl_inode so we don't set any of the osb variables
6324 * until we're sure all is well. */
6325 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6326 ocfs2_truncate_log_worker);
6327 atomic_set(&osb->osb_tl_disable, 0);
6328 osb->osb_tl_bh = tl_bh;
6329 osb->osb_tl_inode = tl_inode;
6335 * Delayed de-allocation of suballocator blocks.
6337 * Some sets of block de-allocations might involve multiple suballocator inodes.
6339 * The locking for this can get extremely complicated, especially when
6340 * the suballocator inodes to delete from aren't known until deep
6341 * within an unrelated codepath.
6343 * ocfs2_extent_block structures are a good example of this - an inode
6344 * btree could have been grown by any number of nodes each allocating
6345 * out of their own suballoc inode.
6347 * These structures allow the delay of block de-allocation until a
6348 * later time, when locking of multiple cluster inodes won't cause
6353 * Describe a single bit freed from a suballocator. For the block
6354 * suballocators, it represents one block. For the global cluster
6355 * allocator, it represents some clusters and free_bit indicates
6358 struct ocfs2_cached_block_free {
6359 struct ocfs2_cached_block_free *free_next;
6362 unsigned int free_bit;
6365 struct ocfs2_per_slot_free_list {
6366 struct ocfs2_per_slot_free_list *f_next_suballocator;
6369 struct ocfs2_cached_block_free *f_first;
6372 static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6375 struct ocfs2_cached_block_free *head)
6380 struct inode *inode;
6381 struct buffer_head *di_bh = NULL;
6382 struct ocfs2_cached_block_free *tmp;
6384 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6393 ret = ocfs2_inode_lock(inode, &di_bh, 1);
6401 bg_blkno = head->free_bg;
6403 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6405 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6406 if (IS_ERR(handle)) {
6407 ret = PTR_ERR(handle);
6412 trace_ocfs2_free_cached_blocks(
6413 (unsigned long long)head->free_blk, head->free_bit);
6415 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6416 head->free_bit, bg_blkno, 1);
6420 ocfs2_commit_trans(osb, handle);
6423 head = head->free_next;
6428 ocfs2_inode_unlock(inode, 1);
6431 inode_unlock(inode);
6435 /* Premature exit may have left some dangling items. */
6437 head = head->free_next;
6444 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6445 u64 blkno, unsigned int bit)
6448 struct ocfs2_cached_block_free *item;
6450 item = kzalloc(sizeof(*item), GFP_NOFS);
6457 trace_ocfs2_cache_cluster_dealloc((unsigned long long)blkno, bit);
6459 item->free_blk = blkno;
6460 item->free_bit = bit;
6461 item->free_next = ctxt->c_global_allocator;
6463 ctxt->c_global_allocator = item;
6467 static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6468 struct ocfs2_cached_block_free *head)
6470 struct ocfs2_cached_block_free *tmp;
6471 struct inode *tl_inode = osb->osb_tl_inode;
6475 inode_lock(tl_inode);
6478 if (ocfs2_truncate_log_needs_flush(osb)) {
6479 ret = __ocfs2_flush_truncate_log(osb);
6486 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6487 if (IS_ERR(handle)) {
6488 ret = PTR_ERR(handle);
6493 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6496 ocfs2_commit_trans(osb, handle);
6498 head = head->free_next;
6507 inode_unlock(tl_inode);
6510 /* Premature exit may have left some dangling items. */
6512 head = head->free_next;
6519 int ocfs2_run_deallocs(struct ocfs2_super *osb,
6520 struct ocfs2_cached_dealloc_ctxt *ctxt)
6523 struct ocfs2_per_slot_free_list *fl;
6528 while (ctxt->c_first_suballocator) {
6529 fl = ctxt->c_first_suballocator;
6532 trace_ocfs2_run_deallocs(fl->f_inode_type,
6534 ret2 = ocfs2_free_cached_blocks(osb,
6544 ctxt->c_first_suballocator = fl->f_next_suballocator;
6548 if (ctxt->c_global_allocator) {
6549 ret2 = ocfs2_free_cached_clusters(osb,
6550 ctxt->c_global_allocator);
6556 ctxt->c_global_allocator = NULL;
6562 static struct ocfs2_per_slot_free_list *
6563 ocfs2_find_per_slot_free_list(int type,
6565 struct ocfs2_cached_dealloc_ctxt *ctxt)
6567 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6570 if (fl->f_inode_type == type && fl->f_slot == slot)
6573 fl = fl->f_next_suballocator;
6576 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6578 fl->f_inode_type = type;
6581 fl->f_next_suballocator = ctxt->c_first_suballocator;
6583 ctxt->c_first_suballocator = fl;
6588 int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6589 int type, int slot, u64 suballoc,
6590 u64 blkno, unsigned int bit)
6593 struct ocfs2_per_slot_free_list *fl;
6594 struct ocfs2_cached_block_free *item;
6596 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6603 item = kzalloc(sizeof(*item), GFP_NOFS);
6610 trace_ocfs2_cache_block_dealloc(type, slot,
6611 (unsigned long long)suballoc,
6612 (unsigned long long)blkno, bit);
6614 item->free_bg = suballoc;
6615 item->free_blk = blkno;
6616 item->free_bit = bit;
6617 item->free_next = fl->f_first;
6626 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6627 struct ocfs2_extent_block *eb)
6629 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6630 le16_to_cpu(eb->h_suballoc_slot),
6631 le64_to_cpu(eb->h_suballoc_loc),
6632 le64_to_cpu(eb->h_blkno),
6633 le16_to_cpu(eb->h_suballoc_bit));
6636 static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
6638 set_buffer_uptodate(bh);
6639 mark_buffer_dirty(bh);
6643 void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6644 unsigned int from, unsigned int to,
6645 struct page *page, int zero, u64 *phys)
6647 int ret, partial = 0;
6649 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6654 zero_user_segment(page, from, to);
6657 * Need to set the buffers we zero'd into uptodate
6658 * here if they aren't - ocfs2_map_page_blocks()
6659 * might've skipped some
6661 ret = walk_page_buffers(handle, page_buffers(page),
6666 else if (ocfs2_should_order_data(inode)) {
6667 ret = ocfs2_jbd2_file_inode(handle, inode);
6673 SetPageUptodate(page);
6675 flush_dcache_page(page);
6678 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6679 loff_t end, struct page **pages,
6680 int numpages, u64 phys, handle_t *handle)
6684 unsigned int from, to = PAGE_SIZE;
6685 struct super_block *sb = inode->i_sb;
6687 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6693 for(i = 0; i < numpages; i++) {
6696 from = start & (PAGE_SIZE - 1);
6697 if ((end >> PAGE_SHIFT) == page->index)
6698 to = end & (PAGE_SIZE - 1);
6700 BUG_ON(from > PAGE_SIZE);
6701 BUG_ON(to > PAGE_SIZE);
6703 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6706 start = (page->index + 1) << PAGE_SHIFT;
6710 ocfs2_unlock_and_free_pages(pages, numpages);
6713 int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
6714 struct page **pages, int *num)
6716 int numpages, ret = 0;
6717 struct address_space *mapping = inode->i_mapping;
6718 unsigned long index;
6719 loff_t last_page_bytes;
6721 BUG_ON(start > end);
6724 last_page_bytes = PAGE_ALIGN(end);
6725 index = start >> PAGE_SHIFT;
6727 pages[numpages] = find_or_create_page(mapping, index, GFP_NOFS);
6728 if (!pages[numpages]) {
6736 } while (index < (last_page_bytes >> PAGE_SHIFT));
6741 ocfs2_unlock_and_free_pages(pages, numpages);
6750 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6751 struct page **pages, int *num)
6753 struct super_block *sb = inode->i_sb;
6755 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6756 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6758 return ocfs2_grab_pages(inode, start, end, pages, num);
6762 * Zero the area past i_size but still within an allocated
6763 * cluster. This avoids exposing nonzero data on subsequent file
6766 * We need to call this before i_size is updated on the inode because
6767 * otherwise block_write_full_page() will skip writeout of pages past
6768 * i_size. The new_i_size parameter is passed for this reason.
6770 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6771 u64 range_start, u64 range_end)
6773 int ret = 0, numpages;
6774 struct page **pages = NULL;
6776 unsigned int ext_flags;
6777 struct super_block *sb = inode->i_sb;
6780 * File systems which don't support sparse files zero on every
6783 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6786 pages = kcalloc(ocfs2_pages_per_cluster(sb),
6787 sizeof(struct page *), GFP_NOFS);
6788 if (pages == NULL) {
6794 if (range_start == range_end)
6797 ret = ocfs2_extent_map_get_blocks(inode,
6798 range_start >> sb->s_blocksize_bits,
6799 &phys, NULL, &ext_flags);
6806 * Tail is a hole, or is marked unwritten. In either case, we
6807 * can count on read and write to return/push zero's.
6809 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
6812 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6819 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6820 numpages, phys, handle);
6823 * Initiate writeout of the pages we zero'd here. We don't
6824 * wait on them - the truncate_inode_pages() call later will
6827 ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
6838 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6839 struct ocfs2_dinode *di)
6841 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6842 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
6844 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6845 memset(&di->id2, 0, blocksize -
6846 offsetof(struct ocfs2_dinode, id2) -
6849 memset(&di->id2, 0, blocksize -
6850 offsetof(struct ocfs2_dinode, id2));
6853 void ocfs2_dinode_new_extent_list(struct inode *inode,
6854 struct ocfs2_dinode *di)
6856 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6857 di->id2.i_list.l_tree_depth = 0;
6858 di->id2.i_list.l_next_free_rec = 0;
6859 di->id2.i_list.l_count = cpu_to_le16(
6860 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
6863 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6865 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6866 struct ocfs2_inline_data *idata = &di->id2.i_data;
6868 spin_lock(&oi->ip_lock);
6869 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6870 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6871 spin_unlock(&oi->ip_lock);
6874 * We clear the entire i_data structure here so that all
6875 * fields can be properly initialized.
6877 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6879 idata->id_count = cpu_to_le16(
6880 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
6883 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6884 struct buffer_head *di_bh)
6886 int ret, i, has_data, num_pages = 0;
6890 u64 uninitialized_var(block);
6891 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6892 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6893 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6894 struct ocfs2_alloc_context *data_ac = NULL;
6895 struct page **pages = NULL;
6896 loff_t end = osb->s_clustersize;
6897 struct ocfs2_extent_tree et;
6900 has_data = i_size_read(inode) ? 1 : 0;
6903 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6904 sizeof(struct page *), GFP_NOFS);
6905 if (pages == NULL) {
6911 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6918 handle = ocfs2_start_trans(osb,
6919 ocfs2_inline_to_extents_credits(osb->sb));
6920 if (IS_ERR(handle)) {
6921 ret = PTR_ERR(handle);
6926 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
6927 OCFS2_JOURNAL_ACCESS_WRITE);
6934 unsigned int page_end;
6937 ret = dquot_alloc_space_nodirty(inode,
6938 ocfs2_clusters_to_bytes(osb->sb, 1));
6943 data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
6945 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
6953 * Save two copies, one for insert, and one that can
6954 * be changed by ocfs2_map_and_dirty_page() below.
6956 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6959 * Non sparse file systems zero on extend, so no need
6962 if (!ocfs2_sparse_alloc(osb) &&
6963 PAGE_SIZE < osb->s_clustersize)
6966 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6974 * This should populate the 1st page for us and mark
6977 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6984 page_end = PAGE_SIZE;
6985 if (PAGE_SIZE > osb->s_clustersize)
6986 page_end = osb->s_clustersize;
6988 for (i = 0; i < num_pages; i++)
6989 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6990 pages[i], i > 0, &phys);
6993 spin_lock(&oi->ip_lock);
6994 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6995 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6996 spin_unlock(&oi->ip_lock);
6998 ocfs2_update_inode_fsync_trans(handle, inode, 1);
6999 ocfs2_dinode_new_extent_list(inode, di);
7001 ocfs2_journal_dirty(handle, di_bh);
7005 * An error at this point should be extremely rare. If
7006 * this proves to be false, we could always re-build
7007 * the in-inode data from our pages.
7009 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
7010 ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
7017 inode->i_blocks = ocfs2_inode_sector_count(inode);
7022 ocfs2_unlock_and_free_pages(pages, num_pages);
7025 if (ret < 0 && did_quota)
7026 dquot_free_space_nodirty(inode,
7027 ocfs2_clusters_to_bytes(osb->sb, 1));
7030 if (data_ac->ac_which == OCFS2_AC_USE_LOCAL)
7031 ocfs2_free_local_alloc_bits(osb, handle, data_ac,
7034 ocfs2_free_clusters(handle,
7037 ocfs2_clusters_to_blocks(osb->sb, bit_off),
7041 ocfs2_commit_trans(osb, handle);
7045 ocfs2_free_alloc_context(data_ac);
7052 * It is expected, that by the time you call this function,
7053 * inode->i_size and fe->i_size have been adjusted.
7055 * WARNING: This will kfree the truncate context
7057 int ocfs2_commit_truncate(struct ocfs2_super *osb,
7058 struct inode *inode,
7059 struct buffer_head *di_bh)
7061 int status = 0, i, flags = 0;
7062 u32 new_highest_cpos, range, trunc_cpos, trunc_len, phys_cpos, coff;
7064 struct ocfs2_extent_list *el;
7065 struct ocfs2_extent_rec *rec;
7066 struct ocfs2_path *path = NULL;
7067 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7068 struct ocfs2_extent_list *root_el = &(di->id2.i_list);
7069 u64 refcount_loc = le64_to_cpu(di->i_refcount_loc);
7070 struct ocfs2_extent_tree et;
7071 struct ocfs2_cached_dealloc_ctxt dealloc;
7072 struct ocfs2_refcount_tree *ref_tree = NULL;
7074 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
7075 ocfs2_init_dealloc_ctxt(&dealloc);
7077 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
7078 i_size_read(inode));
7080 path = ocfs2_new_path(di_bh, &di->id2.i_list,
7081 ocfs2_journal_access_di);
7088 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7092 * Check that we still have allocation to delete.
7094 if (OCFS2_I(inode)->ip_clusters == 0) {
7100 * Truncate always works against the rightmost tree branch.
7102 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
7108 trace_ocfs2_commit_truncate(
7109 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7111 OCFS2_I(inode)->ip_clusters,
7112 path->p_tree_depth);
7115 * By now, el will point to the extent list on the bottom most
7116 * portion of this tree. Only the tail record is considered in
7119 * We handle the following cases, in order:
7120 * - empty extent: delete the remaining branch
7121 * - remove the entire record
7122 * - remove a partial record
7123 * - no record needs to be removed (truncate has completed)
7125 el = path_leaf_el(path);
7126 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7127 ocfs2_error(inode->i_sb,
7128 "Inode %llu has empty extent block at %llu\n",
7129 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7130 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7135 i = le16_to_cpu(el->l_next_free_rec) - 1;
7136 rec = &el->l_recs[i];
7137 flags = rec->e_flags;
7138 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
7140 if (i == 0 && ocfs2_is_empty_extent(rec)) {
7142 * Lower levels depend on this never happening, but it's best
7143 * to check it up here before changing the tree.
7145 if (root_el->l_tree_depth && rec->e_int_clusters == 0) {
7146 mlog(ML_ERROR, "Inode %lu has an empty "
7147 "extent record, depth %u\n", inode->i_ino,
7148 le16_to_cpu(root_el->l_tree_depth));
7149 status = ocfs2_remove_rightmost_empty_extent(osb,
7150 &et, path, &dealloc);
7156 ocfs2_reinit_path(path, 1);
7159 trunc_cpos = le32_to_cpu(rec->e_cpos);
7163 } else if (le32_to_cpu(rec->e_cpos) >= new_highest_cpos) {
7165 * Truncate entire record.
7167 trunc_cpos = le32_to_cpu(rec->e_cpos);
7168 trunc_len = ocfs2_rec_clusters(el, rec);
7169 blkno = le64_to_cpu(rec->e_blkno);
7170 } else if (range > new_highest_cpos) {
7172 * Partial truncate. it also should be
7173 * the last truncate we're doing.
7175 trunc_cpos = new_highest_cpos;
7176 trunc_len = range - new_highest_cpos;
7177 coff = new_highest_cpos - le32_to_cpu(rec->e_cpos);
7178 blkno = le64_to_cpu(rec->e_blkno) +
7179 ocfs2_clusters_to_blocks(inode->i_sb, coff);
7182 * Truncate completed, leave happily.
7188 phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
7190 if ((flags & OCFS2_EXT_REFCOUNTED) && trunc_len && !ref_tree) {
7191 status = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
7199 status = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
7200 phys_cpos, trunc_len, flags, &dealloc,
7201 refcount_loc, true);
7207 ocfs2_reinit_path(path, 1);
7210 * The check above will catch the case where we've truncated
7211 * away all allocation.
7217 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
7219 ocfs2_schedule_truncate_log_flush(osb, 1);
7221 ocfs2_run_deallocs(osb, &dealloc);
7223 ocfs2_free_path(path);
7229 * 'start' is inclusive, 'end' is not.
7231 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7232 unsigned int start, unsigned int end, int trunc)
7235 unsigned int numbytes;
7237 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7238 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7239 struct ocfs2_inline_data *idata = &di->id2.i_data;
7241 if (end > i_size_read(inode))
7242 end = i_size_read(inode);
7244 BUG_ON(start > end);
7246 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7247 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7248 !ocfs2_supports_inline_data(osb)) {
7249 ocfs2_error(inode->i_sb,
7250 "Inline data flags for inode %llu don't agree! Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7251 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7252 le16_to_cpu(di->i_dyn_features),
7253 OCFS2_I(inode)->ip_dyn_features,
7254 osb->s_feature_incompat);
7259 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7260 if (IS_ERR(handle)) {
7261 ret = PTR_ERR(handle);
7266 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7267 OCFS2_JOURNAL_ACCESS_WRITE);
7273 numbytes = end - start;
7274 memset(idata->id_data + start, 0, numbytes);
7277 * No need to worry about the data page here - it's been
7278 * truncated already and inline data doesn't need it for
7279 * pushing zero's to disk, so we'll let readpage pick it up
7283 i_size_write(inode, start);
7284 di->i_size = cpu_to_le64(start);
7287 inode->i_blocks = ocfs2_inode_sector_count(inode);
7288 inode->i_ctime = inode->i_mtime = current_time(inode);
7290 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7291 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7293 ocfs2_update_inode_fsync_trans(handle, inode, 1);
7294 ocfs2_journal_dirty(handle, di_bh);
7297 ocfs2_commit_trans(osb, handle);
7303 static int ocfs2_trim_extent(struct super_block *sb,
7304 struct ocfs2_group_desc *gd,
7305 u64 group, u32 start, u32 count)
7307 u64 discard, bcount;
7308 struct ocfs2_super *osb = OCFS2_SB(sb);
7310 bcount = ocfs2_clusters_to_blocks(sb, count);
7311 discard = ocfs2_clusters_to_blocks(sb, start);
7314 * For the first cluster group, the gd->bg_blkno is not at the start
7315 * of the group, but at an offset from the start. If we add it while
7316 * calculating discard for first group, we will wrongly start fstrim a
7317 * few blocks after the desried start block and the range can cross
7318 * over into the next cluster group. So, add it only if this is not
7319 * the first cluster group.
7321 if (group != osb->first_cluster_group_blkno)
7322 discard += le64_to_cpu(gd->bg_blkno);
7324 trace_ocfs2_trim_extent(sb, (unsigned long long)discard, bcount);
7326 return sb_issue_discard(sb, discard, bcount, GFP_NOFS, 0);
7329 static int ocfs2_trim_group(struct super_block *sb,
7330 struct ocfs2_group_desc *gd, u64 group,
7331 u32 start, u32 max, u32 minbits)
7333 int ret = 0, count = 0, next;
7334 void *bitmap = gd->bg_bitmap;
7336 if (le16_to_cpu(gd->bg_free_bits_count) < minbits)
7339 trace_ocfs2_trim_group((unsigned long long)le64_to_cpu(gd->bg_blkno),
7340 start, max, minbits);
7342 while (start < max) {
7343 start = ocfs2_find_next_zero_bit(bitmap, max, start);
7346 next = ocfs2_find_next_bit(bitmap, max, start);
7348 if ((next - start) >= minbits) {
7349 ret = ocfs2_trim_extent(sb, gd, group,
7350 start, next - start);
7355 count += next - start;
7359 if (fatal_signal_pending(current)) {
7360 count = -ERESTARTSYS;
7364 if ((le16_to_cpu(gd->bg_free_bits_count) - count) < minbits)
7374 int ocfs2_trim_fs(struct super_block *sb, struct fstrim_range *range)
7376 struct ocfs2_super *osb = OCFS2_SB(sb);
7377 u64 start, len, trimmed, first_group, last_group, group;
7379 u32 first_bit, last_bit, minlen;
7380 struct buffer_head *main_bm_bh = NULL;
7381 struct inode *main_bm_inode = NULL;
7382 struct buffer_head *gd_bh = NULL;
7383 struct ocfs2_dinode *main_bm;
7384 struct ocfs2_group_desc *gd = NULL;
7386 start = range->start >> osb->s_clustersize_bits;
7387 len = range->len >> osb->s_clustersize_bits;
7388 minlen = range->minlen >> osb->s_clustersize_bits;
7390 if (minlen >= osb->bitmap_cpg || range->len < sb->s_blocksize)
7393 main_bm_inode = ocfs2_get_system_file_inode(osb,
7394 GLOBAL_BITMAP_SYSTEM_INODE,
7395 OCFS2_INVALID_SLOT);
7396 if (!main_bm_inode) {
7402 inode_lock(main_bm_inode);
7404 ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 0);
7409 main_bm = (struct ocfs2_dinode *)main_bm_bh->b_data;
7411 if (start >= le32_to_cpu(main_bm->i_clusters)) {
7416 len = range->len >> osb->s_clustersize_bits;
7417 if (start + len > le32_to_cpu(main_bm->i_clusters))
7418 len = le32_to_cpu(main_bm->i_clusters) - start;
7420 trace_ocfs2_trim_fs(start, len, minlen);
7422 /* Determine first and last group to examine based on start and len */
7423 first_group = ocfs2_which_cluster_group(main_bm_inode, start);
7424 if (first_group == osb->first_cluster_group_blkno)
7427 first_bit = start - ocfs2_blocks_to_clusters(sb, first_group);
7428 last_group = ocfs2_which_cluster_group(main_bm_inode, start + len - 1);
7429 last_bit = osb->bitmap_cpg;
7432 for (group = first_group; group <= last_group;) {
7433 if (first_bit + len >= osb->bitmap_cpg)
7434 last_bit = osb->bitmap_cpg;
7436 last_bit = first_bit + len;
7438 ret = ocfs2_read_group_descriptor(main_bm_inode,
7446 gd = (struct ocfs2_group_desc *)gd_bh->b_data;
7447 cnt = ocfs2_trim_group(sb, gd, group,
7448 first_bit, last_bit, minlen);
7458 len -= osb->bitmap_cpg - first_bit;
7460 if (group == osb->first_cluster_group_blkno)
7461 group = ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg);
7463 group += ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg);
7465 range->len = trimmed * sb->s_blocksize;
7467 ocfs2_inode_unlock(main_bm_inode, 0);
7470 inode_unlock(main_bm_inode);
7471 iput(main_bm_inode);