1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Extent allocs and frees
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>
30 #include <linux/swap.h>
31 #include <linux/quotaops.h>
33 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
34 #include <cluster/masklog.h>
40 #include "blockcheck.h"
42 #include "extent_map.h"
45 #include "localalloc.h"
53 #include "buffer_head_io.h"
55 enum ocfs2_contig_type {
62 static enum ocfs2_contig_type
63 ocfs2_extent_rec_contig(struct super_block *sb,
64 struct ocfs2_extent_rec *ext,
65 struct ocfs2_extent_rec *insert_rec);
67 * Operations for a specific extent tree type.
69 * To implement an on-disk btree (extent tree) type in ocfs2, add
70 * an ocfs2_extent_tree_operations structure and the matching
71 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
72 * for the allocation portion of the extent tree.
74 struct ocfs2_extent_tree_operations {
76 * last_eb_blk is the block number of the right most leaf extent
77 * block. Most on-disk structures containing an extent tree store
78 * this value for fast access. The ->eo_set_last_eb_blk() and
79 * ->eo_get_last_eb_blk() operations access this value. They are
82 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
84 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
87 * The on-disk structure usually keeps track of how many total
88 * clusters are stored in this extent tree. This function updates
89 * that value. new_clusters is the delta, and must be
90 * added to the total. Required.
92 void (*eo_update_clusters)(struct ocfs2_extent_tree *et,
96 * If this extent tree is supported by an extent map, insert
97 * a record into the map.
99 void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et,
100 struct ocfs2_extent_rec *rec);
103 * If this extent tree is supported by an extent map, truncate the
106 void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et,
110 * If ->eo_insert_check() exists, it is called before rec is
111 * inserted into the extent tree. It is optional.
113 int (*eo_insert_check)(struct ocfs2_extent_tree *et,
114 struct ocfs2_extent_rec *rec);
115 int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
118 * --------------------------------------------------------------
119 * The remaining are internal to ocfs2_extent_tree and don't have
124 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
127 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
130 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
131 * it exists. If it does not, et->et_max_leaf_clusters is set
132 * to 0 (unlimited). Optional.
134 void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
137 * ->eo_extent_contig test whether the 2 ocfs2_extent_rec
138 * are contiguous or not. Optional. Don't need to set it if use
139 * ocfs2_extent_rec as the tree leaf.
141 enum ocfs2_contig_type
142 (*eo_extent_contig)(struct ocfs2_extent_tree *et,
143 struct ocfs2_extent_rec *ext,
144 struct ocfs2_extent_rec *insert_rec);
149 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
152 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
153 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
155 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
157 static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
158 struct ocfs2_extent_rec *rec);
159 static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
161 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
162 struct ocfs2_extent_rec *rec);
163 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
164 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
165 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
166 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
167 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
168 .eo_update_clusters = ocfs2_dinode_update_clusters,
169 .eo_extent_map_insert = ocfs2_dinode_extent_map_insert,
170 .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate,
171 .eo_insert_check = ocfs2_dinode_insert_check,
172 .eo_sanity_check = ocfs2_dinode_sanity_check,
173 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
176 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
179 struct ocfs2_dinode *di = et->et_object;
181 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
182 di->i_last_eb_blk = cpu_to_le64(blkno);
185 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
187 struct ocfs2_dinode *di = et->et_object;
189 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
190 return le64_to_cpu(di->i_last_eb_blk);
193 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
196 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
197 struct ocfs2_dinode *di = et->et_object;
199 le32_add_cpu(&di->i_clusters, clusters);
200 spin_lock(&oi->ip_lock);
201 oi->ip_clusters = le32_to_cpu(di->i_clusters);
202 spin_unlock(&oi->ip_lock);
205 static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
206 struct ocfs2_extent_rec *rec)
208 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
210 ocfs2_extent_map_insert_rec(inode, rec);
213 static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
216 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
218 ocfs2_extent_map_trunc(inode, clusters);
221 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
222 struct ocfs2_extent_rec *rec)
224 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
225 struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
227 BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
228 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
229 (oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
230 "Device %s, asking for sparse allocation: inode %llu, "
231 "cpos %u, clusters %u\n",
233 (unsigned long long)oi->ip_blkno,
234 rec->e_cpos, oi->ip_clusters);
239 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
241 struct ocfs2_dinode *di = et->et_object;
243 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
244 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
249 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
251 struct ocfs2_dinode *di = et->et_object;
253 et->et_root_el = &di->id2.i_list;
257 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
259 struct ocfs2_xattr_value_buf *vb = et->et_object;
261 et->et_root_el = &vb->vb_xv->xr_list;
264 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
267 struct ocfs2_xattr_value_buf *vb = et->et_object;
269 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
272 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
274 struct ocfs2_xattr_value_buf *vb = et->et_object;
276 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
279 static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
282 struct ocfs2_xattr_value_buf *vb = et->et_object;
284 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
287 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
288 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
289 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
290 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
291 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
294 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
296 struct ocfs2_xattr_block *xb = et->et_object;
298 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
301 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
303 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
304 et->et_max_leaf_clusters =
305 ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
308 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
311 struct ocfs2_xattr_block *xb = et->et_object;
312 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
314 xt->xt_last_eb_blk = cpu_to_le64(blkno);
317 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
319 struct ocfs2_xattr_block *xb = et->et_object;
320 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
322 return le64_to_cpu(xt->xt_last_eb_blk);
325 static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
328 struct ocfs2_xattr_block *xb = et->et_object;
330 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
333 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
334 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
335 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
336 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
337 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
338 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
341 static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
344 struct ocfs2_dx_root_block *dx_root = et->et_object;
346 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
349 static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
351 struct ocfs2_dx_root_block *dx_root = et->et_object;
353 return le64_to_cpu(dx_root->dr_last_eb_blk);
356 static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
359 struct ocfs2_dx_root_block *dx_root = et->et_object;
361 le32_add_cpu(&dx_root->dr_clusters, clusters);
364 static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
366 struct ocfs2_dx_root_block *dx_root = et->et_object;
368 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
373 static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
375 struct ocfs2_dx_root_block *dx_root = et->et_object;
377 et->et_root_el = &dx_root->dr_list;
380 static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
381 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
382 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
383 .eo_update_clusters = ocfs2_dx_root_update_clusters,
384 .eo_sanity_check = ocfs2_dx_root_sanity_check,
385 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
388 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
389 struct ocfs2_caching_info *ci,
390 struct buffer_head *bh,
391 ocfs2_journal_access_func access,
393 struct ocfs2_extent_tree_operations *ops)
398 et->et_root_journal_access = access;
400 obj = (void *)bh->b_data;
403 et->et_ops->eo_fill_root_el(et);
404 if (!et->et_ops->eo_fill_max_leaf_clusters)
405 et->et_max_leaf_clusters = 0;
407 et->et_ops->eo_fill_max_leaf_clusters(et);
410 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
411 struct ocfs2_caching_info *ci,
412 struct buffer_head *bh)
414 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di,
415 NULL, &ocfs2_dinode_et_ops);
418 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
419 struct ocfs2_caching_info *ci,
420 struct buffer_head *bh)
422 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb,
423 NULL, &ocfs2_xattr_tree_et_ops);
426 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
427 struct ocfs2_caching_info *ci,
428 struct ocfs2_xattr_value_buf *vb)
430 __ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb,
431 &ocfs2_xattr_value_et_ops);
434 void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
435 struct ocfs2_caching_info *ci,
436 struct buffer_head *bh)
438 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr,
439 NULL, &ocfs2_dx_root_et_ops);
442 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
445 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
448 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
450 return et->et_ops->eo_get_last_eb_blk(et);
453 static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
456 et->et_ops->eo_update_clusters(et, clusters);
459 static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et,
460 struct ocfs2_extent_rec *rec)
462 if (et->et_ops->eo_extent_map_insert)
463 et->et_ops->eo_extent_map_insert(et, rec);
466 static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et,
469 if (et->et_ops->eo_extent_map_truncate)
470 et->et_ops->eo_extent_map_truncate(et, clusters);
473 static inline int ocfs2_et_root_journal_access(handle_t *handle,
474 struct ocfs2_extent_tree *et,
477 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
481 static inline enum ocfs2_contig_type
482 ocfs2_et_extent_contig(struct ocfs2_extent_tree *et,
483 struct ocfs2_extent_rec *rec,
484 struct ocfs2_extent_rec *insert_rec)
486 if (et->et_ops->eo_extent_contig)
487 return et->et_ops->eo_extent_contig(et, rec, insert_rec);
489 return ocfs2_extent_rec_contig(
490 ocfs2_metadata_cache_get_super(et->et_ci),
494 static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
495 struct ocfs2_extent_rec *rec)
499 if (et->et_ops->eo_insert_check)
500 ret = et->et_ops->eo_insert_check(et, rec);
504 static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
508 if (et->et_ops->eo_sanity_check)
509 ret = et->et_ops->eo_sanity_check(et);
513 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
514 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
515 struct ocfs2_extent_block *eb);
518 * Structures which describe a path through a btree, and functions to
521 * The idea here is to be as generic as possible with the tree
524 struct ocfs2_path_item {
525 struct buffer_head *bh;
526 struct ocfs2_extent_list *el;
529 #define OCFS2_MAX_PATH_DEPTH 5
533 ocfs2_journal_access_func p_root_access;
534 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
537 #define path_root_bh(_path) ((_path)->p_node[0].bh)
538 #define path_root_el(_path) ((_path)->p_node[0].el)
539 #define path_root_access(_path)((_path)->p_root_access)
540 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
541 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
542 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
544 static int ocfs2_find_path(struct ocfs2_caching_info *ci,
545 struct ocfs2_path *path, u32 cpos);
546 static void ocfs2_adjust_rightmost_records(handle_t *handle,
547 struct ocfs2_extent_tree *et,
548 struct ocfs2_path *path,
549 struct ocfs2_extent_rec *insert_rec);
551 * Reset the actual path elements so that we can re-use the structure
552 * to build another path. Generally, this involves freeing the buffer
555 static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
557 int i, start = 0, depth = 0;
558 struct ocfs2_path_item *node;
563 for(i = start; i < path_num_items(path); i++) {
564 node = &path->p_node[i];
572 * Tree depth may change during truncate, or insert. If we're
573 * keeping the root extent list, then make sure that our path
574 * structure reflects the proper depth.
577 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
579 path_root_access(path) = NULL;
581 path->p_tree_depth = depth;
584 static void ocfs2_free_path(struct ocfs2_path *path)
587 ocfs2_reinit_path(path, 0);
593 * All the elements of src into dest. After this call, src could be freed
594 * without affecting dest.
596 * Both paths should have the same root. Any non-root elements of dest
599 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
603 BUG_ON(path_root_bh(dest) != path_root_bh(src));
604 BUG_ON(path_root_el(dest) != path_root_el(src));
605 BUG_ON(path_root_access(dest) != path_root_access(src));
607 ocfs2_reinit_path(dest, 1);
609 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
610 dest->p_node[i].bh = src->p_node[i].bh;
611 dest->p_node[i].el = src->p_node[i].el;
613 if (dest->p_node[i].bh)
614 get_bh(dest->p_node[i].bh);
619 * Make the *dest path the same as src and re-initialize src path to
622 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
626 BUG_ON(path_root_bh(dest) != path_root_bh(src));
627 BUG_ON(path_root_access(dest) != path_root_access(src));
629 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
630 brelse(dest->p_node[i].bh);
632 dest->p_node[i].bh = src->p_node[i].bh;
633 dest->p_node[i].el = src->p_node[i].el;
635 src->p_node[i].bh = NULL;
636 src->p_node[i].el = NULL;
641 * Insert an extent block at given index.
643 * This will not take an additional reference on eb_bh.
645 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
646 struct buffer_head *eb_bh)
648 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
651 * Right now, no root bh is an extent block, so this helps
652 * catch code errors with dinode trees. The assertion can be
653 * safely removed if we ever need to insert extent block
654 * structures at the root.
658 path->p_node[index].bh = eb_bh;
659 path->p_node[index].el = &eb->h_list;
662 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
663 struct ocfs2_extent_list *root_el,
664 ocfs2_journal_access_func access)
666 struct ocfs2_path *path;
668 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
670 path = kzalloc(sizeof(*path), GFP_NOFS);
672 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
674 path_root_bh(path) = root_bh;
675 path_root_el(path) = root_el;
676 path_root_access(path) = access;
682 static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
684 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
685 path_root_access(path));
688 static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
690 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
691 et->et_root_journal_access);
695 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
696 * otherwise it's the root_access function.
698 * I don't like the way this function's name looks next to
699 * ocfs2_journal_access_path(), but I don't have a better one.
701 static int ocfs2_path_bh_journal_access(handle_t *handle,
702 struct ocfs2_caching_info *ci,
703 struct ocfs2_path *path,
706 ocfs2_journal_access_func access = path_root_access(path);
709 access = ocfs2_journal_access;
712 access = ocfs2_journal_access_eb;
714 return access(handle, ci, path->p_node[idx].bh,
715 OCFS2_JOURNAL_ACCESS_WRITE);
719 * Convenience function to journal all components in a path.
721 static int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
723 struct ocfs2_path *path)
730 for(i = 0; i < path_num_items(path); i++) {
731 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
743 * Return the index of the extent record which contains cluster #v_cluster.
744 * -1 is returned if it was not found.
746 * Should work fine on interior and exterior nodes.
748 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
752 struct ocfs2_extent_rec *rec;
753 u32 rec_end, rec_start, clusters;
755 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
756 rec = &el->l_recs[i];
758 rec_start = le32_to_cpu(rec->e_cpos);
759 clusters = ocfs2_rec_clusters(el, rec);
761 rec_end = rec_start + clusters;
763 if (v_cluster >= rec_start && v_cluster < rec_end) {
773 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
774 * ocfs2_extent_rec_contig only work properly against leaf nodes!
776 static int ocfs2_block_extent_contig(struct super_block *sb,
777 struct ocfs2_extent_rec *ext,
780 u64 blk_end = le64_to_cpu(ext->e_blkno);
782 blk_end += ocfs2_clusters_to_blocks(sb,
783 le16_to_cpu(ext->e_leaf_clusters));
785 return blkno == blk_end;
788 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
789 struct ocfs2_extent_rec *right)
793 left_range = le32_to_cpu(left->e_cpos) +
794 le16_to_cpu(left->e_leaf_clusters);
796 return (left_range == le32_to_cpu(right->e_cpos));
799 static enum ocfs2_contig_type
800 ocfs2_extent_rec_contig(struct super_block *sb,
801 struct ocfs2_extent_rec *ext,
802 struct ocfs2_extent_rec *insert_rec)
804 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
807 * Refuse to coalesce extent records with different flag
808 * fields - we don't want to mix unwritten extents with user
811 if (ext->e_flags != insert_rec->e_flags)
814 if (ocfs2_extents_adjacent(ext, insert_rec) &&
815 ocfs2_block_extent_contig(sb, ext, blkno))
818 blkno = le64_to_cpu(ext->e_blkno);
819 if (ocfs2_extents_adjacent(insert_rec, ext) &&
820 ocfs2_block_extent_contig(sb, insert_rec, blkno))
827 * NOTE: We can have pretty much any combination of contiguousness and
830 * The usefulness of APPEND_TAIL is more in that it lets us know that
831 * we'll have to update the path to that leaf.
833 enum ocfs2_append_type {
838 enum ocfs2_split_type {
844 struct ocfs2_insert_type {
845 enum ocfs2_split_type ins_split;
846 enum ocfs2_append_type ins_appending;
847 enum ocfs2_contig_type ins_contig;
848 int ins_contig_index;
852 struct ocfs2_merge_ctxt {
853 enum ocfs2_contig_type c_contig_type;
854 int c_has_empty_extent;
855 int c_split_covers_rec;
858 static int ocfs2_validate_extent_block(struct super_block *sb,
859 struct buffer_head *bh)
862 struct ocfs2_extent_block *eb =
863 (struct ocfs2_extent_block *)bh->b_data;
865 mlog(0, "Validating extent block %llu\n",
866 (unsigned long long)bh->b_blocknr);
868 BUG_ON(!buffer_uptodate(bh));
871 * If the ecc fails, we return the error but otherwise
872 * leave the filesystem running. We know any error is
873 * local to this block.
875 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
877 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
878 (unsigned long long)bh->b_blocknr);
883 * Errors after here are fatal.
886 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
888 "Extent block #%llu has bad signature %.*s",
889 (unsigned long long)bh->b_blocknr, 7,
894 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
896 "Extent block #%llu has an invalid h_blkno "
898 (unsigned long long)bh->b_blocknr,
899 (unsigned long long)le64_to_cpu(eb->h_blkno));
903 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
905 "Extent block #%llu has an invalid "
906 "h_fs_generation of #%u",
907 (unsigned long long)bh->b_blocknr,
908 le32_to_cpu(eb->h_fs_generation));
915 int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
916 struct buffer_head **bh)
919 struct buffer_head *tmp = *bh;
921 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
922 ocfs2_validate_extent_block);
924 /* If ocfs2_read_block() got us a new bh, pass it up. */
933 * How many free extents have we got before we need more meta data?
935 int ocfs2_num_free_extents(struct ocfs2_super *osb,
936 struct ocfs2_extent_tree *et)
939 struct ocfs2_extent_list *el = NULL;
940 struct ocfs2_extent_block *eb;
941 struct buffer_head *eb_bh = NULL;
947 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
950 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
956 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
960 BUG_ON(el->l_tree_depth != 0);
962 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
970 /* expects array to already be allocated
972 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
975 static int ocfs2_create_new_meta_bhs(handle_t *handle,
976 struct ocfs2_extent_tree *et,
978 struct ocfs2_alloc_context *meta_ac,
979 struct buffer_head *bhs[])
981 int count, status, i;
982 u16 suballoc_bit_start;
985 struct ocfs2_super *osb =
986 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
987 struct ocfs2_extent_block *eb;
992 while (count < wanted) {
993 status = ocfs2_claim_metadata(osb,
1005 for(i = count; i < (num_got + count); i++) {
1006 bhs[i] = sb_getblk(osb->sb, first_blkno);
1007 if (bhs[i] == NULL) {
1012 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
1014 status = ocfs2_journal_access_eb(handle, et->et_ci,
1016 OCFS2_JOURNAL_ACCESS_CREATE);
1022 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
1023 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
1024 /* Ok, setup the minimal stuff here. */
1025 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
1026 eb->h_blkno = cpu_to_le64(first_blkno);
1027 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
1028 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
1029 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1030 eb->h_list.l_count =
1031 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1033 suballoc_bit_start++;
1036 /* We'll also be dirtied by the caller, so
1037 * this isn't absolutely necessary. */
1038 status = ocfs2_journal_dirty(handle, bhs[i]);
1051 for(i = 0; i < wanted; i++) {
1061 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1063 * Returns the sum of the rightmost extent rec logical offset and
1066 * ocfs2_add_branch() uses this to determine what logical cluster
1067 * value should be populated into the leftmost new branch records.
1069 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1070 * value for the new topmost tree record.
1072 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1076 i = le16_to_cpu(el->l_next_free_rec) - 1;
1078 return le32_to_cpu(el->l_recs[i].e_cpos) +
1079 ocfs2_rec_clusters(el, &el->l_recs[i]);
1083 * Change range of the branches in the right most path according to the leaf
1084 * extent block's rightmost record.
1086 static int ocfs2_adjust_rightmost_branch(handle_t *handle,
1087 struct ocfs2_extent_tree *et)
1090 struct ocfs2_path *path = NULL;
1091 struct ocfs2_extent_list *el;
1092 struct ocfs2_extent_rec *rec;
1094 path = ocfs2_new_path_from_et(et);
1100 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
1106 status = ocfs2_extend_trans(handle, path_num_items(path) +
1107 handle->h_buffer_credits);
1113 status = ocfs2_journal_access_path(et->et_ci, handle, path);
1119 el = path_leaf_el(path);
1120 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1122 ocfs2_adjust_rightmost_records(handle, et, path, rec);
1125 ocfs2_free_path(path);
1130 * Add an entire tree branch to our inode. eb_bh is the extent block
1131 * to start at, if we don't want to start the branch at the root
1134 * last_eb_bh is required as we have to update it's next_leaf pointer
1135 * for the new last extent block.
1137 * the new branch will be 'empty' in the sense that every block will
1138 * contain a single record with cluster count == 0.
1140 static int ocfs2_add_branch(handle_t *handle,
1141 struct ocfs2_extent_tree *et,
1142 struct buffer_head *eb_bh,
1143 struct buffer_head **last_eb_bh,
1144 struct ocfs2_alloc_context *meta_ac)
1146 int status, new_blocks, i;
1147 u64 next_blkno, new_last_eb_blk;
1148 struct buffer_head *bh;
1149 struct buffer_head **new_eb_bhs = NULL;
1150 struct ocfs2_extent_block *eb;
1151 struct ocfs2_extent_list *eb_el;
1152 struct ocfs2_extent_list *el;
1153 u32 new_cpos, root_end;
1157 BUG_ON(!last_eb_bh || !*last_eb_bh);
1160 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1163 el = et->et_root_el;
1165 /* we never add a branch to a leaf. */
1166 BUG_ON(!el->l_tree_depth);
1168 new_blocks = le16_to_cpu(el->l_tree_depth);
1170 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1171 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1172 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1175 * If there is a gap before the root end and the real end
1176 * of the righmost leaf block, we need to remove the gap
1177 * between new_cpos and root_end first so that the tree
1178 * is consistent after we add a new branch(it will start
1181 if (root_end > new_cpos) {
1182 mlog(0, "adjust the cluster end from %u to %u\n",
1183 root_end, new_cpos);
1184 status = ocfs2_adjust_rightmost_branch(handle, et);
1191 /* allocate the number of new eb blocks we need */
1192 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1200 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
1201 meta_ac, new_eb_bhs);
1207 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1208 * linked with the rest of the tree.
1209 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1211 * when we leave the loop, new_last_eb_blk will point to the
1212 * newest leaf, and next_blkno will point to the topmost extent
1214 next_blkno = new_last_eb_blk = 0;
1215 for(i = 0; i < new_blocks; i++) {
1217 eb = (struct ocfs2_extent_block *) bh->b_data;
1218 /* ocfs2_create_new_meta_bhs() should create it right! */
1219 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1220 eb_el = &eb->h_list;
1222 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
1223 OCFS2_JOURNAL_ACCESS_CREATE);
1229 eb->h_next_leaf_blk = 0;
1230 eb_el->l_tree_depth = cpu_to_le16(i);
1231 eb_el->l_next_free_rec = cpu_to_le16(1);
1233 * This actually counts as an empty extent as
1236 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
1237 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
1239 * eb_el isn't always an interior node, but even leaf
1240 * nodes want a zero'd flags and reserved field so
1241 * this gets the whole 32 bits regardless of use.
1243 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
1244 if (!eb_el->l_tree_depth)
1245 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1247 status = ocfs2_journal_dirty(handle, bh);
1253 next_blkno = le64_to_cpu(eb->h_blkno);
1256 /* This is a bit hairy. We want to update up to three blocks
1257 * here without leaving any of them in an inconsistent state
1258 * in case of error. We don't have to worry about
1259 * journal_dirty erroring as it won't unless we've aborted the
1260 * handle (in which case we would never be here) so reserving
1261 * the write with journal_access is all we need to do. */
1262 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
1263 OCFS2_JOURNAL_ACCESS_WRITE);
1268 status = ocfs2_et_root_journal_access(handle, et,
1269 OCFS2_JOURNAL_ACCESS_WRITE);
1275 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
1276 OCFS2_JOURNAL_ACCESS_WRITE);
1283 /* Link the new branch into the rest of the tree (el will
1284 * either be on the root_bh, or the extent block passed in. */
1285 i = le16_to_cpu(el->l_next_free_rec);
1286 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
1287 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1288 el->l_recs[i].e_int_clusters = 0;
1289 le16_add_cpu(&el->l_next_free_rec, 1);
1291 /* fe needs a new last extent block pointer, as does the
1292 * next_leaf on the previously last-extent-block. */
1293 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
1295 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
1296 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1298 status = ocfs2_journal_dirty(handle, *last_eb_bh);
1301 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1305 status = ocfs2_journal_dirty(handle, eb_bh);
1311 * Some callers want to track the rightmost leaf so pass it
1314 brelse(*last_eb_bh);
1315 get_bh(new_eb_bhs[0]);
1316 *last_eb_bh = new_eb_bhs[0];
1321 for (i = 0; i < new_blocks; i++)
1322 brelse(new_eb_bhs[i]);
1331 * adds another level to the allocation tree.
1332 * returns back the new extent block so you can add a branch to it
1335 static int ocfs2_shift_tree_depth(handle_t *handle,
1336 struct ocfs2_extent_tree *et,
1337 struct ocfs2_alloc_context *meta_ac,
1338 struct buffer_head **ret_new_eb_bh)
1342 struct buffer_head *new_eb_bh = NULL;
1343 struct ocfs2_extent_block *eb;
1344 struct ocfs2_extent_list *root_el;
1345 struct ocfs2_extent_list *eb_el;
1349 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
1356 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1357 /* ocfs2_create_new_meta_bhs() should create it right! */
1358 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1360 eb_el = &eb->h_list;
1361 root_el = et->et_root_el;
1363 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
1364 OCFS2_JOURNAL_ACCESS_CREATE);
1370 /* copy the root extent list data into the new extent block */
1371 eb_el->l_tree_depth = root_el->l_tree_depth;
1372 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1373 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1374 eb_el->l_recs[i] = root_el->l_recs[i];
1376 status = ocfs2_journal_dirty(handle, new_eb_bh);
1382 status = ocfs2_et_root_journal_access(handle, et,
1383 OCFS2_JOURNAL_ACCESS_WRITE);
1389 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1391 /* update root_bh now */
1392 le16_add_cpu(&root_el->l_tree_depth, 1);
1393 root_el->l_recs[0].e_cpos = 0;
1394 root_el->l_recs[0].e_blkno = eb->h_blkno;
1395 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1396 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1397 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1398 root_el->l_next_free_rec = cpu_to_le16(1);
1400 /* If this is our 1st tree depth shift, then last_eb_blk
1401 * becomes the allocated extent block */
1402 if (root_el->l_tree_depth == cpu_to_le16(1))
1403 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1405 status = ocfs2_journal_dirty(handle, et->et_root_bh);
1411 *ret_new_eb_bh = new_eb_bh;
1422 * Should only be called when there is no space left in any of the
1423 * leaf nodes. What we want to do is find the lowest tree depth
1424 * non-leaf extent block with room for new records. There are three
1425 * valid results of this search:
1427 * 1) a lowest extent block is found, then we pass it back in
1428 * *lowest_eb_bh and return '0'
1430 * 2) the search fails to find anything, but the root_el has room. We
1431 * pass NULL back in *lowest_eb_bh, but still return '0'
1433 * 3) the search fails to find anything AND the root_el is full, in
1434 * which case we return > 0
1436 * return status < 0 indicates an error.
1438 static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
1439 struct buffer_head **target_bh)
1443 struct ocfs2_extent_block *eb;
1444 struct ocfs2_extent_list *el;
1445 struct buffer_head *bh = NULL;
1446 struct buffer_head *lowest_bh = NULL;
1452 el = et->et_root_el;
1454 while(le16_to_cpu(el->l_tree_depth) > 1) {
1455 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1456 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1457 "Owner %llu has empty "
1458 "extent list (next_free_rec == 0)",
1459 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
1463 i = le16_to_cpu(el->l_next_free_rec) - 1;
1464 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1466 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1467 "Owner %llu has extent "
1468 "list where extent # %d has no physical "
1470 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
1478 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
1484 eb = (struct ocfs2_extent_block *) bh->b_data;
1487 if (le16_to_cpu(el->l_next_free_rec) <
1488 le16_to_cpu(el->l_count)) {
1495 /* If we didn't find one and the fe doesn't have any room,
1496 * then return '1' */
1497 el = et->et_root_el;
1498 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1501 *target_bh = lowest_bh;
1510 * Grow a b-tree so that it has more records.
1512 * We might shift the tree depth in which case existing paths should
1513 * be considered invalid.
1515 * Tree depth after the grow is returned via *final_depth.
1517 * *last_eb_bh will be updated by ocfs2_add_branch().
1519 static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1520 int *final_depth, struct buffer_head **last_eb_bh,
1521 struct ocfs2_alloc_context *meta_ac)
1524 struct ocfs2_extent_list *el = et->et_root_el;
1525 int depth = le16_to_cpu(el->l_tree_depth);
1526 struct buffer_head *bh = NULL;
1528 BUG_ON(meta_ac == NULL);
1530 shift = ocfs2_find_branch_target(et, &bh);
1537 /* We traveled all the way to the bottom of the allocation tree
1538 * and didn't find room for any more extents - we need to add
1539 * another tree level */
1542 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1544 /* ocfs2_shift_tree_depth will return us a buffer with
1545 * the new extent block (so we can pass that to
1546 * ocfs2_add_branch). */
1547 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
1555 * Special case: we have room now if we shifted from
1556 * tree_depth 0, so no more work needs to be done.
1558 * We won't be calling add_branch, so pass
1559 * back *last_eb_bh as the new leaf. At depth
1560 * zero, it should always be null so there's
1561 * no reason to brelse.
1563 BUG_ON(*last_eb_bh);
1570 /* call ocfs2_add_branch to add the final part of the tree with
1572 mlog(0, "add branch. bh = %p\n", bh);
1573 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
1582 *final_depth = depth;
1588 * This function will discard the rightmost extent record.
1590 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1592 int next_free = le16_to_cpu(el->l_next_free_rec);
1593 int count = le16_to_cpu(el->l_count);
1594 unsigned int num_bytes;
1597 /* This will cause us to go off the end of our extent list. */
1598 BUG_ON(next_free >= count);
1600 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1602 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1605 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1606 struct ocfs2_extent_rec *insert_rec)
1608 int i, insert_index, next_free, has_empty, num_bytes;
1609 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1610 struct ocfs2_extent_rec *rec;
1612 next_free = le16_to_cpu(el->l_next_free_rec);
1613 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1617 /* The tree code before us didn't allow enough room in the leaf. */
1618 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1621 * The easiest way to approach this is to just remove the
1622 * empty extent and temporarily decrement next_free.
1626 * If next_free was 1 (only an empty extent), this
1627 * loop won't execute, which is fine. We still want
1628 * the decrement above to happen.
1630 for(i = 0; i < (next_free - 1); i++)
1631 el->l_recs[i] = el->l_recs[i+1];
1637 * Figure out what the new record index should be.
1639 for(i = 0; i < next_free; i++) {
1640 rec = &el->l_recs[i];
1642 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1647 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1648 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1650 BUG_ON(insert_index < 0);
1651 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1652 BUG_ON(insert_index > next_free);
1655 * No need to memmove if we're just adding to the tail.
1657 if (insert_index != next_free) {
1658 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1660 num_bytes = next_free - insert_index;
1661 num_bytes *= sizeof(struct ocfs2_extent_rec);
1662 memmove(&el->l_recs[insert_index + 1],
1663 &el->l_recs[insert_index],
1668 * Either we had an empty extent, and need to re-increment or
1669 * there was no empty extent on a non full rightmost leaf node,
1670 * in which case we still need to increment.
1673 el->l_next_free_rec = cpu_to_le16(next_free);
1675 * Make sure none of the math above just messed up our tree.
1677 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1679 el->l_recs[insert_index] = *insert_rec;
1683 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1685 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1687 BUG_ON(num_recs == 0);
1689 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1691 size = num_recs * sizeof(struct ocfs2_extent_rec);
1692 memmove(&el->l_recs[0], &el->l_recs[1], size);
1693 memset(&el->l_recs[num_recs], 0,
1694 sizeof(struct ocfs2_extent_rec));
1695 el->l_next_free_rec = cpu_to_le16(num_recs);
1700 * Create an empty extent record .
1702 * l_next_free_rec may be updated.
1704 * If an empty extent already exists do nothing.
1706 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1708 int next_free = le16_to_cpu(el->l_next_free_rec);
1710 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1715 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1718 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1719 "Asked to create an empty extent in a full list:\n"
1720 "count = %u, tree depth = %u",
1721 le16_to_cpu(el->l_count),
1722 le16_to_cpu(el->l_tree_depth));
1724 ocfs2_shift_records_right(el);
1727 le16_add_cpu(&el->l_next_free_rec, 1);
1728 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1732 * For a rotation which involves two leaf nodes, the "root node" is
1733 * the lowest level tree node which contains a path to both leafs. This
1734 * resulting set of information can be used to form a complete "subtree"
1736 * This function is passed two full paths from the dinode down to a
1737 * pair of adjacent leaves. It's task is to figure out which path
1738 * index contains the subtree root - this can be the root index itself
1739 * in a worst-case rotation.
1741 * The array index of the subtree root is passed back.
1743 static int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1744 struct ocfs2_path *left,
1745 struct ocfs2_path *right)
1750 * Check that the caller passed in two paths from the same tree.
1752 BUG_ON(path_root_bh(left) != path_root_bh(right));
1758 * The caller didn't pass two adjacent paths.
1760 mlog_bug_on_msg(i > left->p_tree_depth,
1761 "Owner %llu, left depth %u, right depth %u\n"
1762 "left leaf blk %llu, right leaf blk %llu\n",
1763 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1764 left->p_tree_depth, right->p_tree_depth,
1765 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1766 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1767 } while (left->p_node[i].bh->b_blocknr ==
1768 right->p_node[i].bh->b_blocknr);
1773 typedef void (path_insert_t)(void *, struct buffer_head *);
1776 * Traverse a btree path in search of cpos, starting at root_el.
1778 * This code can be called with a cpos larger than the tree, in which
1779 * case it will return the rightmost path.
1781 static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
1782 struct ocfs2_extent_list *root_el, u32 cpos,
1783 path_insert_t *func, void *data)
1788 struct buffer_head *bh = NULL;
1789 struct ocfs2_extent_block *eb;
1790 struct ocfs2_extent_list *el;
1791 struct ocfs2_extent_rec *rec;
1794 while (el->l_tree_depth) {
1795 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1796 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1797 "Owner %llu has empty extent list at "
1799 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1800 le16_to_cpu(el->l_tree_depth));
1806 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1807 rec = &el->l_recs[i];
1810 * In the case that cpos is off the allocation
1811 * tree, this should just wind up returning the
1814 range = le32_to_cpu(rec->e_cpos) +
1815 ocfs2_rec_clusters(el, rec);
1816 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1820 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1822 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1823 "Owner %llu has bad blkno in extent list "
1824 "at depth %u (index %d)\n",
1825 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1826 le16_to_cpu(el->l_tree_depth), i);
1833 ret = ocfs2_read_extent_block(ci, blkno, &bh);
1839 eb = (struct ocfs2_extent_block *) bh->b_data;
1842 if (le16_to_cpu(el->l_next_free_rec) >
1843 le16_to_cpu(el->l_count)) {
1844 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1845 "Owner %llu has bad count in extent list "
1846 "at block %llu (next free=%u, count=%u)\n",
1847 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1848 (unsigned long long)bh->b_blocknr,
1849 le16_to_cpu(el->l_next_free_rec),
1850 le16_to_cpu(el->l_count));
1861 * Catch any trailing bh that the loop didn't handle.
1869 * Given an initialized path (that is, it has a valid root extent
1870 * list), this function will traverse the btree in search of the path
1871 * which would contain cpos.
1873 * The path traveled is recorded in the path structure.
1875 * Note that this will not do any comparisons on leaf node extent
1876 * records, so it will work fine in the case that we just added a tree
1879 struct find_path_data {
1881 struct ocfs2_path *path;
1883 static void find_path_ins(void *data, struct buffer_head *bh)
1885 struct find_path_data *fp = data;
1888 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1891 static int ocfs2_find_path(struct ocfs2_caching_info *ci,
1892 struct ocfs2_path *path, u32 cpos)
1894 struct find_path_data data;
1898 return __ocfs2_find_path(ci, path_root_el(path), cpos,
1899 find_path_ins, &data);
1902 static void find_leaf_ins(void *data, struct buffer_head *bh)
1904 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1905 struct ocfs2_extent_list *el = &eb->h_list;
1906 struct buffer_head **ret = data;
1908 /* We want to retain only the leaf block. */
1909 if (le16_to_cpu(el->l_tree_depth) == 0) {
1915 * Find the leaf block in the tree which would contain cpos. No
1916 * checking of the actual leaf is done.
1918 * Some paths want to call this instead of allocating a path structure
1919 * and calling ocfs2_find_path().
1921 * This function doesn't handle non btree extent lists.
1923 int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1924 struct ocfs2_extent_list *root_el, u32 cpos,
1925 struct buffer_head **leaf_bh)
1928 struct buffer_head *bh = NULL;
1930 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
1942 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1944 * Basically, we've moved stuff around at the bottom of the tree and
1945 * we need to fix up the extent records above the changes to reflect
1948 * left_rec: the record on the left.
1949 * left_child_el: is the child list pointed to by left_rec
1950 * right_rec: the record to the right of left_rec
1951 * right_child_el: is the child list pointed to by right_rec
1953 * By definition, this only works on interior nodes.
1955 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1956 struct ocfs2_extent_list *left_child_el,
1957 struct ocfs2_extent_rec *right_rec,
1958 struct ocfs2_extent_list *right_child_el)
1960 u32 left_clusters, right_end;
1963 * Interior nodes never have holes. Their cpos is the cpos of
1964 * the leftmost record in their child list. Their cluster
1965 * count covers the full theoretical range of their child list
1966 * - the range between their cpos and the cpos of the record
1967 * immediately to their right.
1969 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1970 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1971 BUG_ON(right_child_el->l_tree_depth);
1972 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1973 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1975 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1976 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1979 * Calculate the rightmost cluster count boundary before
1980 * moving cpos - we will need to adjust clusters after
1981 * updating e_cpos to keep the same highest cluster count.
1983 right_end = le32_to_cpu(right_rec->e_cpos);
1984 right_end += le32_to_cpu(right_rec->e_int_clusters);
1986 right_rec->e_cpos = left_rec->e_cpos;
1987 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1989 right_end -= le32_to_cpu(right_rec->e_cpos);
1990 right_rec->e_int_clusters = cpu_to_le32(right_end);
1994 * Adjust the adjacent root node records involved in a
1995 * rotation. left_el_blkno is passed in as a key so that we can easily
1996 * find it's index in the root list.
1998 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1999 struct ocfs2_extent_list *left_el,
2000 struct ocfs2_extent_list *right_el,
2005 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
2006 le16_to_cpu(left_el->l_tree_depth));
2008 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
2009 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
2014 * The path walking code should have never returned a root and
2015 * two paths which are not adjacent.
2017 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2019 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
2020 &root_el->l_recs[i + 1], right_el);
2024 * We've changed a leaf block (in right_path) and need to reflect that
2025 * change back up the subtree.
2027 * This happens in multiple places:
2028 * - When we've moved an extent record from the left path leaf to the right
2029 * path leaf to make room for an empty extent in the left path leaf.
2030 * - When our insert into the right path leaf is at the leftmost edge
2031 * and requires an update of the path immediately to it's left. This
2032 * can occur at the end of some types of rotation and appending inserts.
2033 * - When we've adjusted the last extent record in the left path leaf and the
2034 * 1st extent record in the right path leaf during cross extent block merge.
2036 static void ocfs2_complete_edge_insert(handle_t *handle,
2037 struct ocfs2_path *left_path,
2038 struct ocfs2_path *right_path,
2042 struct ocfs2_extent_list *el, *left_el, *right_el;
2043 struct ocfs2_extent_rec *left_rec, *right_rec;
2044 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2047 * Update the counts and position values within all the
2048 * interior nodes to reflect the leaf rotation we just did.
2050 * The root node is handled below the loop.
2052 * We begin the loop with right_el and left_el pointing to the
2053 * leaf lists and work our way up.
2055 * NOTE: within this loop, left_el and right_el always refer
2056 * to the *child* lists.
2058 left_el = path_leaf_el(left_path);
2059 right_el = path_leaf_el(right_path);
2060 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2061 mlog(0, "Adjust records at index %u\n", i);
2064 * One nice property of knowing that all of these
2065 * nodes are below the root is that we only deal with
2066 * the leftmost right node record and the rightmost
2069 el = left_path->p_node[i].el;
2070 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2071 left_rec = &el->l_recs[idx];
2073 el = right_path->p_node[i].el;
2074 right_rec = &el->l_recs[0];
2076 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2079 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2083 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2088 * Setup our list pointers now so that the current
2089 * parents become children in the next iteration.
2091 left_el = left_path->p_node[i].el;
2092 right_el = right_path->p_node[i].el;
2096 * At the root node, adjust the two adjacent records which
2097 * begin our path to the leaves.
2100 el = left_path->p_node[subtree_index].el;
2101 left_el = left_path->p_node[subtree_index + 1].el;
2102 right_el = right_path->p_node[subtree_index + 1].el;
2104 ocfs2_adjust_root_records(el, left_el, right_el,
2105 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2107 root_bh = left_path->p_node[subtree_index].bh;
2109 ret = ocfs2_journal_dirty(handle, root_bh);
2114 static int ocfs2_rotate_subtree_right(handle_t *handle,
2115 struct ocfs2_extent_tree *et,
2116 struct ocfs2_path *left_path,
2117 struct ocfs2_path *right_path,
2121 struct buffer_head *right_leaf_bh;
2122 struct buffer_head *left_leaf_bh = NULL;
2123 struct buffer_head *root_bh;
2124 struct ocfs2_extent_list *right_el, *left_el;
2125 struct ocfs2_extent_rec move_rec;
2127 left_leaf_bh = path_leaf_bh(left_path);
2128 left_el = path_leaf_el(left_path);
2130 if (left_el->l_next_free_rec != left_el->l_count) {
2131 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
2132 "Inode %llu has non-full interior leaf node %llu"
2134 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2135 (unsigned long long)left_leaf_bh->b_blocknr,
2136 le16_to_cpu(left_el->l_next_free_rec));
2141 * This extent block may already have an empty record, so we
2142 * return early if so.
2144 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2147 root_bh = left_path->p_node[subtree_index].bh;
2148 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2150 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2157 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2158 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2165 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2173 right_leaf_bh = path_leaf_bh(right_path);
2174 right_el = path_leaf_el(right_path);
2176 /* This is a code error, not a disk corruption. */
2177 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2178 "because rightmost leaf block %llu is empty\n",
2179 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2180 (unsigned long long)right_leaf_bh->b_blocknr);
2182 ocfs2_create_empty_extent(right_el);
2184 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2190 /* Do the copy now. */
2191 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2192 move_rec = left_el->l_recs[i];
2193 right_el->l_recs[0] = move_rec;
2196 * Clear out the record we just copied and shift everything
2197 * over, leaving an empty extent in the left leaf.
2199 * We temporarily subtract from next_free_rec so that the
2200 * shift will lose the tail record (which is now defunct).
2202 le16_add_cpu(&left_el->l_next_free_rec, -1);
2203 ocfs2_shift_records_right(left_el);
2204 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2205 le16_add_cpu(&left_el->l_next_free_rec, 1);
2207 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2213 ocfs2_complete_edge_insert(handle, left_path, right_path,
2221 * Given a full path, determine what cpos value would return us a path
2222 * containing the leaf immediately to the left of the current one.
2224 * Will return zero if the path passed in is already the leftmost path.
2226 static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2227 struct ocfs2_path *path, u32 *cpos)
2231 struct ocfs2_extent_list *el;
2233 BUG_ON(path->p_tree_depth == 0);
2237 blkno = path_leaf_bh(path)->b_blocknr;
2239 /* Start at the tree node just above the leaf and work our way up. */
2240 i = path->p_tree_depth - 1;
2242 el = path->p_node[i].el;
2245 * Find the extent record just before the one in our
2248 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2249 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2253 * We've determined that the
2254 * path specified is already
2255 * the leftmost one - return a
2261 * The leftmost record points to our
2262 * leaf - we need to travel up the
2268 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
2269 *cpos = *cpos + ocfs2_rec_clusters(el,
2270 &el->l_recs[j - 1]);
2277 * If we got here, we never found a valid node where
2278 * the tree indicated one should be.
2281 "Invalid extent tree at extent block %llu\n",
2282 (unsigned long long)blkno);
2287 blkno = path->p_node[i].bh->b_blocknr;
2296 * Extend the transaction by enough credits to complete the rotation,
2297 * and still leave at least the original number of credits allocated
2298 * to this transaction.
2300 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
2302 struct ocfs2_path *path)
2304 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
2306 if (handle->h_buffer_credits < credits)
2307 return ocfs2_extend_trans(handle, credits);
2313 * Trap the case where we're inserting into the theoretical range past
2314 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2315 * whose cpos is less than ours into the right leaf.
2317 * It's only necessary to look at the rightmost record of the left
2318 * leaf because the logic that calls us should ensure that the
2319 * theoretical ranges in the path components above the leaves are
2322 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2325 struct ocfs2_extent_list *left_el;
2326 struct ocfs2_extent_rec *rec;
2329 left_el = path_leaf_el(left_path);
2330 next_free = le16_to_cpu(left_el->l_next_free_rec);
2331 rec = &left_el->l_recs[next_free - 1];
2333 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2338 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2340 int next_free = le16_to_cpu(el->l_next_free_rec);
2342 struct ocfs2_extent_rec *rec;
2347 rec = &el->l_recs[0];
2348 if (ocfs2_is_empty_extent(rec)) {
2352 rec = &el->l_recs[1];
2355 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2356 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2362 * Rotate all the records in a btree right one record, starting at insert_cpos.
2364 * The path to the rightmost leaf should be passed in.
2366 * The array is assumed to be large enough to hold an entire path (tree depth).
2368 * Upon succesful return from this function:
2370 * - The 'right_path' array will contain a path to the leaf block
2371 * whose range contains e_cpos.
2372 * - That leaf block will have a single empty extent in list index 0.
2373 * - In the case that the rotation requires a post-insert update,
2374 * *ret_left_path will contain a valid path which can be passed to
2375 * ocfs2_insert_path().
2377 static int ocfs2_rotate_tree_right(handle_t *handle,
2378 struct ocfs2_extent_tree *et,
2379 enum ocfs2_split_type split,
2381 struct ocfs2_path *right_path,
2382 struct ocfs2_path **ret_left_path)
2384 int ret, start, orig_credits = handle->h_buffer_credits;
2386 struct ocfs2_path *left_path = NULL;
2387 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2389 *ret_left_path = NULL;
2391 left_path = ocfs2_new_path_from_path(right_path);
2398 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2404 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2407 * What we want to do here is:
2409 * 1) Start with the rightmost path.
2411 * 2) Determine a path to the leaf block directly to the left
2414 * 3) Determine the 'subtree root' - the lowest level tree node
2415 * which contains a path to both leaves.
2417 * 4) Rotate the subtree.
2419 * 5) Find the next subtree by considering the left path to be
2420 * the new right path.
2422 * The check at the top of this while loop also accepts
2423 * insert_cpos == cpos because cpos is only a _theoretical_
2424 * value to get us the left path - insert_cpos might very well
2425 * be filling that hole.
2427 * Stop at a cpos of '0' because we either started at the
2428 * leftmost branch (i.e., a tree with one branch and a
2429 * rotation inside of it), or we've gone as far as we can in
2430 * rotating subtrees.
2432 while (cpos && insert_cpos <= cpos) {
2433 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2436 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
2442 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2443 path_leaf_bh(right_path),
2444 "Owner %llu: error during insert of %u "
2445 "(left path cpos %u) results in two identical "
2446 "paths ending at %llu\n",
2447 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2449 (unsigned long long)
2450 path_leaf_bh(left_path)->b_blocknr);
2452 if (split == SPLIT_NONE &&
2453 ocfs2_rotate_requires_path_adjustment(left_path,
2457 * We've rotated the tree as much as we
2458 * should. The rest is up to
2459 * ocfs2_insert_path() to complete, after the
2460 * record insertion. We indicate this
2461 * situation by returning the left path.
2463 * The reason we don't adjust the records here
2464 * before the record insert is that an error
2465 * later might break the rule where a parent
2466 * record e_cpos will reflect the actual
2467 * e_cpos of the 1st nonempty record of the
2470 *ret_left_path = left_path;
2474 start = ocfs2_find_subtree_root(et, left_path, right_path);
2476 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2478 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2479 right_path->p_tree_depth);
2481 ret = ocfs2_extend_rotate_transaction(handle, start,
2482 orig_credits, right_path);
2488 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
2495 if (split != SPLIT_NONE &&
2496 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2499 * A rotate moves the rightmost left leaf
2500 * record over to the leftmost right leaf
2501 * slot. If we're doing an extent split
2502 * instead of a real insert, then we have to
2503 * check that the extent to be split wasn't
2504 * just moved over. If it was, then we can
2505 * exit here, passing left_path back -
2506 * ocfs2_split_extent() is smart enough to
2507 * search both leaves.
2509 *ret_left_path = left_path;
2514 * There is no need to re-read the next right path
2515 * as we know that it'll be our current left
2516 * path. Optimize by copying values instead.
2518 ocfs2_mv_path(right_path, left_path);
2520 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2528 ocfs2_free_path(left_path);
2534 static int ocfs2_update_edge_lengths(handle_t *handle,
2535 struct ocfs2_extent_tree *et,
2536 int subtree_index, struct ocfs2_path *path)
2539 struct ocfs2_extent_rec *rec;
2540 struct ocfs2_extent_list *el;
2541 struct ocfs2_extent_block *eb;
2545 * In normal tree rotation process, we will never touch the
2546 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2547 * doesn't reserve the credits for them either.
2549 * But we do have a special case here which will update the rightmost
2550 * records for all the bh in the path.
2551 * So we have to allocate extra credits and access them.
2553 ret = ocfs2_extend_trans(handle,
2554 handle->h_buffer_credits + subtree_index);
2560 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
2566 /* Path should always be rightmost. */
2567 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2568 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2571 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2572 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2573 rec = &el->l_recs[idx];
2574 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2576 for (i = 0; i < path->p_tree_depth; i++) {
2577 el = path->p_node[i].el;
2578 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2579 rec = &el->l_recs[idx];
2581 rec->e_int_clusters = cpu_to_le32(range);
2582 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2584 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2590 static void ocfs2_unlink_path(handle_t *handle,
2591 struct ocfs2_extent_tree *et,
2592 struct ocfs2_cached_dealloc_ctxt *dealloc,
2593 struct ocfs2_path *path, int unlink_start)
2596 struct ocfs2_extent_block *eb;
2597 struct ocfs2_extent_list *el;
2598 struct buffer_head *bh;
2600 for(i = unlink_start; i < path_num_items(path); i++) {
2601 bh = path->p_node[i].bh;
2603 eb = (struct ocfs2_extent_block *)bh->b_data;
2605 * Not all nodes might have had their final count
2606 * decremented by the caller - handle this here.
2609 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2611 "Inode %llu, attempted to remove extent block "
2612 "%llu with %u records\n",
2613 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2614 (unsigned long long)le64_to_cpu(eb->h_blkno),
2615 le16_to_cpu(el->l_next_free_rec));
2617 ocfs2_journal_dirty(handle, bh);
2618 ocfs2_remove_from_cache(et->et_ci, bh);
2622 el->l_next_free_rec = 0;
2623 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2625 ocfs2_journal_dirty(handle, bh);
2627 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2631 ocfs2_remove_from_cache(et->et_ci, bh);
2635 static void ocfs2_unlink_subtree(handle_t *handle,
2636 struct ocfs2_extent_tree *et,
2637 struct ocfs2_path *left_path,
2638 struct ocfs2_path *right_path,
2640 struct ocfs2_cached_dealloc_ctxt *dealloc)
2643 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2644 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2645 struct ocfs2_extent_list *el;
2646 struct ocfs2_extent_block *eb;
2648 el = path_leaf_el(left_path);
2650 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2652 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2653 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2656 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2658 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2659 le16_add_cpu(&root_el->l_next_free_rec, -1);
2661 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2662 eb->h_next_leaf_blk = 0;
2664 ocfs2_journal_dirty(handle, root_bh);
2665 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2667 ocfs2_unlink_path(handle, et, dealloc, right_path,
2671 static int ocfs2_rotate_subtree_left(handle_t *handle,
2672 struct ocfs2_extent_tree *et,
2673 struct ocfs2_path *left_path,
2674 struct ocfs2_path *right_path,
2676 struct ocfs2_cached_dealloc_ctxt *dealloc,
2679 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2680 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2681 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2682 struct ocfs2_extent_block *eb;
2686 right_leaf_el = path_leaf_el(right_path);
2687 left_leaf_el = path_leaf_el(left_path);
2688 root_bh = left_path->p_node[subtree_index].bh;
2689 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2691 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2694 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2695 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2697 * It's legal for us to proceed if the right leaf is
2698 * the rightmost one and it has an empty extent. There
2699 * are two cases to handle - whether the leaf will be
2700 * empty after removal or not. If the leaf isn't empty
2701 * then just remove the empty extent up front. The
2702 * next block will handle empty leaves by flagging
2705 * Non rightmost leaves will throw -EAGAIN and the
2706 * caller can manually move the subtree and retry.
2709 if (eb->h_next_leaf_blk != 0ULL)
2712 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2713 ret = ocfs2_journal_access_eb(handle, et->et_ci,
2714 path_leaf_bh(right_path),
2715 OCFS2_JOURNAL_ACCESS_WRITE);
2721 ocfs2_remove_empty_extent(right_leaf_el);
2723 right_has_empty = 1;
2726 if (eb->h_next_leaf_blk == 0ULL &&
2727 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2729 * We have to update i_last_eb_blk during the meta
2732 ret = ocfs2_et_root_journal_access(handle, et,
2733 OCFS2_JOURNAL_ACCESS_WRITE);
2739 del_right_subtree = 1;
2743 * Getting here with an empty extent in the right path implies
2744 * that it's the rightmost path and will be deleted.
2746 BUG_ON(right_has_empty && !del_right_subtree);
2748 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2755 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2756 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2763 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2771 if (!right_has_empty) {
2773 * Only do this if we're moving a real
2774 * record. Otherwise, the action is delayed until
2775 * after removal of the right path in which case we
2776 * can do a simple shift to remove the empty extent.
2778 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2779 memset(&right_leaf_el->l_recs[0], 0,
2780 sizeof(struct ocfs2_extent_rec));
2782 if (eb->h_next_leaf_blk == 0ULL) {
2784 * Move recs over to get rid of empty extent, decrease
2785 * next_free. This is allowed to remove the last
2786 * extent in our leaf (setting l_next_free_rec to
2787 * zero) - the delete code below won't care.
2789 ocfs2_remove_empty_extent(right_leaf_el);
2792 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2795 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2799 if (del_right_subtree) {
2800 ocfs2_unlink_subtree(handle, et, left_path, right_path,
2801 subtree_index, dealloc);
2802 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
2809 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2810 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2813 * Removal of the extent in the left leaf was skipped
2814 * above so we could delete the right path
2817 if (right_has_empty)
2818 ocfs2_remove_empty_extent(left_leaf_el);
2820 ret = ocfs2_journal_dirty(handle, et_root_bh);
2826 ocfs2_complete_edge_insert(handle, left_path, right_path,
2834 * Given a full path, determine what cpos value would return us a path
2835 * containing the leaf immediately to the right of the current one.
2837 * Will return zero if the path passed in is already the rightmost path.
2839 * This looks similar, but is subtly different to
2840 * ocfs2_find_cpos_for_left_leaf().
2842 static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2843 struct ocfs2_path *path, u32 *cpos)
2847 struct ocfs2_extent_list *el;
2851 if (path->p_tree_depth == 0)
2854 blkno = path_leaf_bh(path)->b_blocknr;
2856 /* Start at the tree node just above the leaf and work our way up. */
2857 i = path->p_tree_depth - 1;
2861 el = path->p_node[i].el;
2864 * Find the extent record just after the one in our
2867 next_free = le16_to_cpu(el->l_next_free_rec);
2868 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2869 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2870 if (j == (next_free - 1)) {
2873 * We've determined that the
2874 * path specified is already
2875 * the rightmost one - return a
2881 * The rightmost record points to our
2882 * leaf - we need to travel up the
2888 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2894 * If we got here, we never found a valid node where
2895 * the tree indicated one should be.
2898 "Invalid extent tree at extent block %llu\n",
2899 (unsigned long long)blkno);
2904 blkno = path->p_node[i].bh->b_blocknr;
2912 static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2913 struct ocfs2_extent_tree *et,
2914 struct ocfs2_path *path)
2917 struct buffer_head *bh = path_leaf_bh(path);
2918 struct ocfs2_extent_list *el = path_leaf_el(path);
2920 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2923 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
2924 path_num_items(path) - 1);
2930 ocfs2_remove_empty_extent(el);
2932 ret = ocfs2_journal_dirty(handle, bh);
2940 static int __ocfs2_rotate_tree_left(handle_t *handle,
2941 struct ocfs2_extent_tree *et,
2943 struct ocfs2_path *path,
2944 struct ocfs2_cached_dealloc_ctxt *dealloc,
2945 struct ocfs2_path **empty_extent_path)
2947 int ret, subtree_root, deleted;
2949 struct ocfs2_path *left_path = NULL;
2950 struct ocfs2_path *right_path = NULL;
2951 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2953 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2955 *empty_extent_path = NULL;
2957 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
2963 left_path = ocfs2_new_path_from_path(path);
2970 ocfs2_cp_path(left_path, path);
2972 right_path = ocfs2_new_path_from_path(path);
2979 while (right_cpos) {
2980 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
2986 subtree_root = ocfs2_find_subtree_root(et, left_path,
2989 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2991 (unsigned long long)
2992 right_path->p_node[subtree_root].bh->b_blocknr,
2993 right_path->p_tree_depth);
2995 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2996 orig_credits, left_path);
3003 * Caller might still want to make changes to the
3004 * tree root, so re-add it to the journal here.
3006 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3013 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
3014 right_path, subtree_root,
3016 if (ret == -EAGAIN) {
3018 * The rotation has to temporarily stop due to
3019 * the right subtree having an empty
3020 * extent. Pass it back to the caller for a
3023 *empty_extent_path = right_path;
3033 * The subtree rotate might have removed records on
3034 * the rightmost edge. If so, then rotation is
3040 ocfs2_mv_path(left_path, right_path);
3042 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
3051 ocfs2_free_path(right_path);
3052 ocfs2_free_path(left_path);
3057 static int ocfs2_remove_rightmost_path(handle_t *handle,
3058 struct ocfs2_extent_tree *et,
3059 struct ocfs2_path *path,
3060 struct ocfs2_cached_dealloc_ctxt *dealloc)
3062 int ret, subtree_index;
3064 struct ocfs2_path *left_path = NULL;
3065 struct ocfs2_extent_block *eb;
3066 struct ocfs2_extent_list *el;
3069 ret = ocfs2_et_sanity_check(et);
3073 * There's two ways we handle this depending on
3074 * whether path is the only existing one.
3076 ret = ocfs2_extend_rotate_transaction(handle, 0,
3077 handle->h_buffer_credits,
3084 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
3090 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3099 * We have a path to the left of this one - it needs
3102 left_path = ocfs2_new_path_from_path(path);
3109 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
3115 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
3121 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
3123 ocfs2_unlink_subtree(handle, et, left_path, path,
3124 subtree_index, dealloc);
3125 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
3132 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
3133 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
3136 * 'path' is also the leftmost path which
3137 * means it must be the only one. This gets
3138 * handled differently because we want to
3139 * revert the root back to having extents
3142 ocfs2_unlink_path(handle, et, dealloc, path, 1);
3144 el = et->et_root_el;
3145 el->l_tree_depth = 0;
3146 el->l_next_free_rec = 0;
3147 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3149 ocfs2_et_set_last_eb_blk(et, 0);
3152 ocfs2_journal_dirty(handle, path_root_bh(path));
3155 ocfs2_free_path(left_path);
3160 * Left rotation of btree records.
3162 * In many ways, this is (unsurprisingly) the opposite of right
3163 * rotation. We start at some non-rightmost path containing an empty
3164 * extent in the leaf block. The code works its way to the rightmost
3165 * path by rotating records to the left in every subtree.
3167 * This is used by any code which reduces the number of extent records
3168 * in a leaf. After removal, an empty record should be placed in the
3169 * leftmost list position.
3171 * This won't handle a length update of the rightmost path records if
3172 * the rightmost tree leaf record is removed so the caller is
3173 * responsible for detecting and correcting that.
3175 static int ocfs2_rotate_tree_left(handle_t *handle,
3176 struct ocfs2_extent_tree *et,
3177 struct ocfs2_path *path,
3178 struct ocfs2_cached_dealloc_ctxt *dealloc)
3180 int ret, orig_credits = handle->h_buffer_credits;
3181 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3182 struct ocfs2_extent_block *eb;
3183 struct ocfs2_extent_list *el;
3185 el = path_leaf_el(path);
3186 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3189 if (path->p_tree_depth == 0) {
3190 rightmost_no_delete:
3192 * Inline extents. This is trivially handled, so do
3195 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
3202 * Handle rightmost branch now. There's several cases:
3203 * 1) simple rotation leaving records in there. That's trivial.
3204 * 2) rotation requiring a branch delete - there's no more
3205 * records left. Two cases of this:
3206 * a) There are branches to the left.
3207 * b) This is also the leftmost (the only) branch.
3209 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3210 * 2a) we need the left branch so that we can update it with the unlink
3211 * 2b) we need to bring the root back to inline extents.
3214 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3216 if (eb->h_next_leaf_blk == 0) {
3218 * This gets a bit tricky if we're going to delete the
3219 * rightmost path. Get the other cases out of the way
3222 if (le16_to_cpu(el->l_next_free_rec) > 1)
3223 goto rightmost_no_delete;
3225 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3227 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3228 "Owner %llu has empty extent block at %llu",
3229 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
3230 (unsigned long long)le64_to_cpu(eb->h_blkno));
3235 * XXX: The caller can not trust "path" any more after
3236 * this as it will have been deleted. What do we do?
3238 * In theory the rotate-for-merge code will never get
3239 * here because it'll always ask for a rotate in a
3243 ret = ocfs2_remove_rightmost_path(handle, et, path,
3251 * Now we can loop, remembering the path we get from -EAGAIN
3252 * and restarting from there.
3255 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3256 dealloc, &restart_path);
3257 if (ret && ret != -EAGAIN) {
3262 while (ret == -EAGAIN) {
3263 tmp_path = restart_path;
3264 restart_path = NULL;
3266 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
3269 if (ret && ret != -EAGAIN) {
3274 ocfs2_free_path(tmp_path);
3282 ocfs2_free_path(tmp_path);
3283 ocfs2_free_path(restart_path);
3287 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3290 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3293 if (rec->e_leaf_clusters == 0) {
3295 * We consumed all of the merged-from record. An empty
3296 * extent cannot exist anywhere but the 1st array
3297 * position, so move things over if the merged-from
3298 * record doesn't occupy that position.
3300 * This creates a new empty extent so the caller
3301 * should be smart enough to have removed any existing
3305 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3306 size = index * sizeof(struct ocfs2_extent_rec);
3307 memmove(&el->l_recs[1], &el->l_recs[0], size);
3311 * Always memset - the caller doesn't check whether it
3312 * created an empty extent, so there could be junk in
3315 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3319 static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
3320 struct ocfs2_path *left_path,
3321 struct ocfs2_path **ret_right_path)
3325 struct ocfs2_path *right_path = NULL;
3326 struct ocfs2_extent_list *left_el;
3328 *ret_right_path = NULL;
3330 /* This function shouldn't be called for non-trees. */
3331 BUG_ON(left_path->p_tree_depth == 0);
3333 left_el = path_leaf_el(left_path);
3334 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3336 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3337 left_path, &right_cpos);
3343 /* This function shouldn't be called for the rightmost leaf. */
3344 BUG_ON(right_cpos == 0);
3346 right_path = ocfs2_new_path_from_path(left_path);
3353 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
3359 *ret_right_path = right_path;
3362 ocfs2_free_path(right_path);
3367 * Remove split_rec clusters from the record at index and merge them
3368 * onto the beginning of the record "next" to it.
3369 * For index < l_count - 1, the next means the extent rec at index + 1.
3370 * For index == l_count - 1, the "next" means the 1st extent rec of the
3371 * next extent block.
3373 static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
3375 struct ocfs2_extent_tree *et,
3376 struct ocfs2_extent_rec *split_rec,
3379 int ret, next_free, i;
3380 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3381 struct ocfs2_extent_rec *left_rec;
3382 struct ocfs2_extent_rec *right_rec;
3383 struct ocfs2_extent_list *right_el;
3384 struct ocfs2_path *right_path = NULL;
3385 int subtree_index = 0;
3386 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3387 struct buffer_head *bh = path_leaf_bh(left_path);
3388 struct buffer_head *root_bh = NULL;
3390 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3391 left_rec = &el->l_recs[index];
3393 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3394 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3395 /* we meet with a cross extent block merge. */
3396 ret = ocfs2_get_right_path(et, left_path, &right_path);
3402 right_el = path_leaf_el(right_path);
3403 next_free = le16_to_cpu(right_el->l_next_free_rec);
3404 BUG_ON(next_free <= 0);
3405 right_rec = &right_el->l_recs[0];
3406 if (ocfs2_is_empty_extent(right_rec)) {
3407 BUG_ON(next_free <= 1);
3408 right_rec = &right_el->l_recs[1];
3411 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3412 le16_to_cpu(left_rec->e_leaf_clusters) !=
3413 le32_to_cpu(right_rec->e_cpos));
3415 subtree_index = ocfs2_find_subtree_root(et, left_path,
3418 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3419 handle->h_buffer_credits,
3426 root_bh = left_path->p_node[subtree_index].bh;
3427 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3429 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3436 for (i = subtree_index + 1;
3437 i < path_num_items(right_path); i++) {
3438 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3445 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3454 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3455 right_rec = &el->l_recs[index + 1];
3458 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
3459 path_num_items(left_path) - 1);
3465 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3467 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3468 le64_add_cpu(&right_rec->e_blkno,
3469 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3471 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3473 ocfs2_cleanup_merge(el, index);
3475 ret = ocfs2_journal_dirty(handle, bh);
3480 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3484 ocfs2_complete_edge_insert(handle, left_path, right_path,
3489 ocfs2_free_path(right_path);
3493 static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
3494 struct ocfs2_path *right_path,
3495 struct ocfs2_path **ret_left_path)
3499 struct ocfs2_path *left_path = NULL;
3501 *ret_left_path = NULL;
3503 /* This function shouldn't be called for non-trees. */
3504 BUG_ON(right_path->p_tree_depth == 0);
3506 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3507 right_path, &left_cpos);
3513 /* This function shouldn't be called for the leftmost leaf. */
3514 BUG_ON(left_cpos == 0);
3516 left_path = ocfs2_new_path_from_path(right_path);
3523 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
3529 *ret_left_path = left_path;
3532 ocfs2_free_path(left_path);
3537 * Remove split_rec clusters from the record at index and merge them
3538 * onto the tail of the record "before" it.
3539 * For index > 0, the "before" means the extent rec at index - 1.
3541 * For index == 0, the "before" means the last record of the previous
3542 * extent block. And there is also a situation that we may need to
3543 * remove the rightmost leaf extent block in the right_path and change
3544 * the right path to indicate the new rightmost path.
3546 static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
3548 struct ocfs2_extent_tree *et,
3549 struct ocfs2_extent_rec *split_rec,
3550 struct ocfs2_cached_dealloc_ctxt *dealloc,
3553 int ret, i, subtree_index = 0, has_empty_extent = 0;
3554 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3555 struct ocfs2_extent_rec *left_rec;
3556 struct ocfs2_extent_rec *right_rec;
3557 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3558 struct buffer_head *bh = path_leaf_bh(right_path);
3559 struct buffer_head *root_bh = NULL;
3560 struct ocfs2_path *left_path = NULL;
3561 struct ocfs2_extent_list *left_el;
3565 right_rec = &el->l_recs[index];
3567 /* we meet with a cross extent block merge. */
3568 ret = ocfs2_get_left_path(et, right_path, &left_path);
3574 left_el = path_leaf_el(left_path);
3575 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3576 le16_to_cpu(left_el->l_count));
3578 left_rec = &left_el->l_recs[
3579 le16_to_cpu(left_el->l_next_free_rec) - 1];
3580 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3581 le16_to_cpu(left_rec->e_leaf_clusters) !=
3582 le32_to_cpu(split_rec->e_cpos));
3584 subtree_index = ocfs2_find_subtree_root(et, left_path,
3587 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3588 handle->h_buffer_credits,
3595 root_bh = left_path->p_node[subtree_index].bh;
3596 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3598 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3605 for (i = subtree_index + 1;
3606 i < path_num_items(right_path); i++) {
3607 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3614 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3622 left_rec = &el->l_recs[index - 1];
3623 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3624 has_empty_extent = 1;
3627 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3628 path_num_items(right_path) - 1);
3634 if (has_empty_extent && index == 1) {
3636 * The easy case - we can just plop the record right in.
3638 *left_rec = *split_rec;
3640 has_empty_extent = 0;
3642 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3644 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3645 le64_add_cpu(&right_rec->e_blkno,
3646 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3648 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3650 ocfs2_cleanup_merge(el, index);
3652 ret = ocfs2_journal_dirty(handle, bh);
3657 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3662 * In the situation that the right_rec is empty and the extent
3663 * block is empty also, ocfs2_complete_edge_insert can't handle
3664 * it and we need to delete the right extent block.
3666 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3667 le16_to_cpu(el->l_next_free_rec) == 1) {
3669 ret = ocfs2_remove_rightmost_path(handle, et,
3677 /* Now the rightmost extent block has been deleted.
3678 * So we use the new rightmost path.
3680 ocfs2_mv_path(right_path, left_path);
3683 ocfs2_complete_edge_insert(handle, left_path,
3684 right_path, subtree_index);
3688 ocfs2_free_path(left_path);
3692 static int ocfs2_try_to_merge_extent(handle_t *handle,
3693 struct ocfs2_extent_tree *et,
3694 struct ocfs2_path *path,
3696 struct ocfs2_extent_rec *split_rec,
3697 struct ocfs2_cached_dealloc_ctxt *dealloc,
3698 struct ocfs2_merge_ctxt *ctxt)
3701 struct ocfs2_extent_list *el = path_leaf_el(path);
3702 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3704 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3706 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3708 * The merge code will need to create an empty
3709 * extent to take the place of the newly
3710 * emptied slot. Remove any pre-existing empty
3711 * extents - having more than one in a leaf is
3714 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3720 rec = &el->l_recs[split_index];
3723 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3725 * Left-right contig implies this.
3727 BUG_ON(!ctxt->c_split_covers_rec);
3730 * Since the leftright insert always covers the entire
3731 * extent, this call will delete the insert record
3732 * entirely, resulting in an empty extent record added to
3735 * Since the adding of an empty extent shifts
3736 * everything back to the right, there's no need to
3737 * update split_index here.
3739 * When the split_index is zero, we need to merge it to the
3740 * prevoius extent block. It is more efficient and easier
3741 * if we do merge_right first and merge_left later.
3743 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
3751 * We can only get this from logic error above.
3753 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3755 /* The merge left us with an empty extent, remove it. */
3756 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3762 rec = &el->l_recs[split_index];
3765 * Note that we don't pass split_rec here on purpose -
3766 * we've merged it into the rec already.
3768 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3769 dealloc, split_index);
3776 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3778 * Error from this last rotate is not critical, so
3779 * print but don't bubble it up.
3786 * Merge a record to the left or right.
3788 * 'contig_type' is relative to the existing record,
3789 * so for example, if we're "right contig", it's to
3790 * the record on the left (hence the left merge).
3792 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3793 ret = ocfs2_merge_rec_left(path, handle, et,
3801 ret = ocfs2_merge_rec_right(path, handle,
3810 if (ctxt->c_split_covers_rec) {
3812 * The merge may have left an empty extent in
3813 * our leaf. Try to rotate it away.
3815 ret = ocfs2_rotate_tree_left(handle, et, path,
3827 static void ocfs2_subtract_from_rec(struct super_block *sb,
3828 enum ocfs2_split_type split,
3829 struct ocfs2_extent_rec *rec,
3830 struct ocfs2_extent_rec *split_rec)
3834 len_blocks = ocfs2_clusters_to_blocks(sb,
3835 le16_to_cpu(split_rec->e_leaf_clusters));
3837 if (split == SPLIT_LEFT) {
3839 * Region is on the left edge of the existing
3842 le32_add_cpu(&rec->e_cpos,
3843 le16_to_cpu(split_rec->e_leaf_clusters));
3844 le64_add_cpu(&rec->e_blkno, len_blocks);
3845 le16_add_cpu(&rec->e_leaf_clusters,
3846 -le16_to_cpu(split_rec->e_leaf_clusters));
3849 * Region is on the right edge of the existing
3852 le16_add_cpu(&rec->e_leaf_clusters,
3853 -le16_to_cpu(split_rec->e_leaf_clusters));
3858 * Do the final bits of extent record insertion at the target leaf
3859 * list. If this leaf is part of an allocation tree, it is assumed
3860 * that the tree above has been prepared.
3862 static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3863 struct ocfs2_extent_rec *insert_rec,
3864 struct ocfs2_extent_list *el,
3865 struct ocfs2_insert_type *insert)
3867 int i = insert->ins_contig_index;
3869 struct ocfs2_extent_rec *rec;
3871 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3873 if (insert->ins_split != SPLIT_NONE) {
3874 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3876 rec = &el->l_recs[i];
3877 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3878 insert->ins_split, rec,
3884 * Contiguous insert - either left or right.
3886 if (insert->ins_contig != CONTIG_NONE) {
3887 rec = &el->l_recs[i];
3888 if (insert->ins_contig == CONTIG_LEFT) {
3889 rec->e_blkno = insert_rec->e_blkno;
3890 rec->e_cpos = insert_rec->e_cpos;
3892 le16_add_cpu(&rec->e_leaf_clusters,
3893 le16_to_cpu(insert_rec->e_leaf_clusters));
3898 * Handle insert into an empty leaf.
3900 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3901 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3902 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3903 el->l_recs[0] = *insert_rec;
3904 el->l_next_free_rec = cpu_to_le16(1);
3911 if (insert->ins_appending == APPEND_TAIL) {
3912 i = le16_to_cpu(el->l_next_free_rec) - 1;
3913 rec = &el->l_recs[i];
3914 range = le32_to_cpu(rec->e_cpos)
3915 + le16_to_cpu(rec->e_leaf_clusters);
3916 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3918 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3919 le16_to_cpu(el->l_count),
3920 "owner %llu, depth %u, count %u, next free %u, "
3921 "rec.cpos %u, rec.clusters %u, "
3922 "insert.cpos %u, insert.clusters %u\n",
3923 ocfs2_metadata_cache_owner(et->et_ci),
3924 le16_to_cpu(el->l_tree_depth),
3925 le16_to_cpu(el->l_count),
3926 le16_to_cpu(el->l_next_free_rec),
3927 le32_to_cpu(el->l_recs[i].e_cpos),
3928 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3929 le32_to_cpu(insert_rec->e_cpos),
3930 le16_to_cpu(insert_rec->e_leaf_clusters));
3932 el->l_recs[i] = *insert_rec;
3933 le16_add_cpu(&el->l_next_free_rec, 1);
3939 * Ok, we have to rotate.
3941 * At this point, it is safe to assume that inserting into an
3942 * empty leaf and appending to a leaf have both been handled
3945 * This leaf needs to have space, either by the empty 1st
3946 * extent record, or by virtue of an l_next_rec < l_count.
3948 ocfs2_rotate_leaf(el, insert_rec);
3951 static void ocfs2_adjust_rightmost_records(handle_t *handle,
3952 struct ocfs2_extent_tree *et,
3953 struct ocfs2_path *path,
3954 struct ocfs2_extent_rec *insert_rec)
3956 int ret, i, next_free;
3957 struct buffer_head *bh;
3958 struct ocfs2_extent_list *el;
3959 struct ocfs2_extent_rec *rec;
3962 * Update everything except the leaf block.
3964 for (i = 0; i < path->p_tree_depth; i++) {
3965 bh = path->p_node[i].bh;
3966 el = path->p_node[i].el;
3968 next_free = le16_to_cpu(el->l_next_free_rec);
3969 if (next_free == 0) {
3970 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3971 "Owner %llu has a bad extent list",
3972 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
3977 rec = &el->l_recs[next_free - 1];
3979 rec->e_int_clusters = insert_rec->e_cpos;
3980 le32_add_cpu(&rec->e_int_clusters,
3981 le16_to_cpu(insert_rec->e_leaf_clusters));
3982 le32_add_cpu(&rec->e_int_clusters,
3983 -le32_to_cpu(rec->e_cpos));
3985 ret = ocfs2_journal_dirty(handle, bh);
3992 static int ocfs2_append_rec_to_path(handle_t *handle,
3993 struct ocfs2_extent_tree *et,
3994 struct ocfs2_extent_rec *insert_rec,
3995 struct ocfs2_path *right_path,
3996 struct ocfs2_path **ret_left_path)
3999 struct ocfs2_extent_list *el;
4000 struct ocfs2_path *left_path = NULL;
4002 *ret_left_path = NULL;
4005 * This shouldn't happen for non-trees. The extent rec cluster
4006 * count manipulation below only works for interior nodes.
4008 BUG_ON(right_path->p_tree_depth == 0);
4011 * If our appending insert is at the leftmost edge of a leaf,
4012 * then we might need to update the rightmost records of the
4015 el = path_leaf_el(right_path);
4016 next_free = le16_to_cpu(el->l_next_free_rec);
4017 if (next_free == 0 ||
4018 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
4021 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
4022 right_path, &left_cpos);
4028 mlog(0, "Append may need a left path update. cpos: %u, "
4029 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
4033 * No need to worry if the append is already in the
4037 left_path = ocfs2_new_path_from_path(right_path);
4044 ret = ocfs2_find_path(et->et_ci, left_path,
4052 * ocfs2_insert_path() will pass the left_path to the
4058 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4064 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
4066 *ret_left_path = left_path;
4070 ocfs2_free_path(left_path);
4075 static void ocfs2_split_record(struct ocfs2_extent_tree *et,
4076 struct ocfs2_path *left_path,
4077 struct ocfs2_path *right_path,
4078 struct ocfs2_extent_rec *split_rec,
4079 enum ocfs2_split_type split)
4082 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4083 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4084 struct ocfs2_extent_rec *rec, *tmprec;
4086 right_el = path_leaf_el(right_path);
4088 left_el = path_leaf_el(left_path);
4091 insert_el = right_el;
4092 index = ocfs2_search_extent_list(el, cpos);
4094 if (index == 0 && left_path) {
4095 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4098 * This typically means that the record
4099 * started in the left path but moved to the
4100 * right as a result of rotation. We either
4101 * move the existing record to the left, or we
4102 * do the later insert there.
4104 * In this case, the left path should always
4105 * exist as the rotate code will have passed
4106 * it back for a post-insert update.
4109 if (split == SPLIT_LEFT) {
4111 * It's a left split. Since we know
4112 * that the rotate code gave us an
4113 * empty extent in the left path, we
4114 * can just do the insert there.
4116 insert_el = left_el;
4119 * Right split - we have to move the
4120 * existing record over to the left
4121 * leaf. The insert will be into the
4122 * newly created empty extent in the
4125 tmprec = &right_el->l_recs[index];
4126 ocfs2_rotate_leaf(left_el, tmprec);
4129 memset(tmprec, 0, sizeof(*tmprec));
4130 index = ocfs2_search_extent_list(left_el, cpos);
4131 BUG_ON(index == -1);
4136 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4138 * Left path is easy - we can just allow the insert to
4142 insert_el = left_el;
4143 index = ocfs2_search_extent_list(el, cpos);
4144 BUG_ON(index == -1);
4147 rec = &el->l_recs[index];
4148 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4149 split, rec, split_rec);
4150 ocfs2_rotate_leaf(insert_el, split_rec);
4154 * This function only does inserts on an allocation b-tree. For tree
4155 * depth = 0, ocfs2_insert_at_leaf() is called directly.
4157 * right_path is the path we want to do the actual insert
4158 * in. left_path should only be passed in if we need to update that
4159 * portion of the tree after an edge insert.
4161 static int ocfs2_insert_path(handle_t *handle,
4162 struct ocfs2_extent_tree *et,
4163 struct ocfs2_path *left_path,
4164 struct ocfs2_path *right_path,
4165 struct ocfs2_extent_rec *insert_rec,
4166 struct ocfs2_insert_type *insert)
4168 int ret, subtree_index;
4169 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
4172 int credits = handle->h_buffer_credits;
4175 * There's a chance that left_path got passed back to
4176 * us without being accounted for in the
4177 * journal. Extend our transaction here to be sure we
4178 * can change those blocks.
4180 credits += left_path->p_tree_depth;
4182 ret = ocfs2_extend_trans(handle, credits);
4188 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
4196 * Pass both paths to the journal. The majority of inserts
4197 * will be touching all components anyway.
4199 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4205 if (insert->ins_split != SPLIT_NONE) {
4207 * We could call ocfs2_insert_at_leaf() for some types
4208 * of splits, but it's easier to just let one separate
4209 * function sort it all out.
4211 ocfs2_split_record(et, left_path, right_path,
4212 insert_rec, insert->ins_split);
4215 * Split might have modified either leaf and we don't
4216 * have a guarantee that the later edge insert will
4217 * dirty this for us.
4220 ret = ocfs2_journal_dirty(handle,
4221 path_leaf_bh(left_path));
4225 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4228 ret = ocfs2_journal_dirty(handle, leaf_bh);
4234 * The rotate code has indicated that we need to fix
4235 * up portions of the tree after the insert.
4237 * XXX: Should we extend the transaction here?
4239 subtree_index = ocfs2_find_subtree_root(et, left_path,
4241 ocfs2_complete_edge_insert(handle, left_path, right_path,
4250 static int ocfs2_do_insert_extent(handle_t *handle,
4251 struct ocfs2_extent_tree *et,
4252 struct ocfs2_extent_rec *insert_rec,
4253 struct ocfs2_insert_type *type)
4255 int ret, rotate = 0;
4257 struct ocfs2_path *right_path = NULL;
4258 struct ocfs2_path *left_path = NULL;
4259 struct ocfs2_extent_list *el;
4261 el = et->et_root_el;
4263 ret = ocfs2_et_root_journal_access(handle, et,
4264 OCFS2_JOURNAL_ACCESS_WRITE);
4270 if (le16_to_cpu(el->l_tree_depth) == 0) {
4271 ocfs2_insert_at_leaf(et, insert_rec, el, type);
4272 goto out_update_clusters;
4275 right_path = ocfs2_new_path_from_et(et);
4283 * Determine the path to start with. Rotations need the
4284 * rightmost path, everything else can go directly to the
4287 cpos = le32_to_cpu(insert_rec->e_cpos);
4288 if (type->ins_appending == APPEND_NONE &&
4289 type->ins_contig == CONTIG_NONE) {
4294 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
4301 * Rotations and appends need special treatment - they modify
4302 * parts of the tree's above them.
4304 * Both might pass back a path immediate to the left of the
4305 * one being inserted to. This will be cause
4306 * ocfs2_insert_path() to modify the rightmost records of
4307 * left_path to account for an edge insert.
4309 * XXX: When modifying this code, keep in mind that an insert
4310 * can wind up skipping both of these two special cases...
4313 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
4314 le32_to_cpu(insert_rec->e_cpos),
4315 right_path, &left_path);
4322 * ocfs2_rotate_tree_right() might have extended the
4323 * transaction without re-journaling our tree root.
4325 ret = ocfs2_et_root_journal_access(handle, et,
4326 OCFS2_JOURNAL_ACCESS_WRITE);
4331 } else if (type->ins_appending == APPEND_TAIL
4332 && type->ins_contig != CONTIG_LEFT) {
4333 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
4334 right_path, &left_path);
4341 ret = ocfs2_insert_path(handle, et, left_path, right_path,
4348 out_update_clusters:
4349 if (type->ins_split == SPLIT_NONE)
4350 ocfs2_et_update_clusters(et,
4351 le16_to_cpu(insert_rec->e_leaf_clusters));
4353 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
4358 ocfs2_free_path(left_path);
4359 ocfs2_free_path(right_path);
4364 static enum ocfs2_contig_type
4365 ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4366 struct ocfs2_path *path,
4367 struct ocfs2_extent_list *el, int index,
4368 struct ocfs2_extent_rec *split_rec)
4371 enum ocfs2_contig_type ret = CONTIG_NONE;
4372 u32 left_cpos, right_cpos;
4373 struct ocfs2_extent_rec *rec = NULL;
4374 struct ocfs2_extent_list *new_el;
4375 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4376 struct buffer_head *bh;
4377 struct ocfs2_extent_block *eb;
4378 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
4381 rec = &el->l_recs[index - 1];
4382 } else if (path->p_tree_depth > 0) {
4383 status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
4387 if (left_cpos != 0) {
4388 left_path = ocfs2_new_path_from_path(path);
4392 status = ocfs2_find_path(et->et_ci, left_path,
4397 new_el = path_leaf_el(left_path);
4399 if (le16_to_cpu(new_el->l_next_free_rec) !=
4400 le16_to_cpu(new_el->l_count)) {
4401 bh = path_leaf_bh(left_path);
4402 eb = (struct ocfs2_extent_block *)bh->b_data;
4404 "Extent block #%llu has an "
4405 "invalid l_next_free_rec of "
4406 "%d. It should have "
4407 "matched the l_count of %d",
4408 (unsigned long long)le64_to_cpu(eb->h_blkno),
4409 le16_to_cpu(new_el->l_next_free_rec),
4410 le16_to_cpu(new_el->l_count));
4414 rec = &new_el->l_recs[
4415 le16_to_cpu(new_el->l_next_free_rec) - 1];
4420 * We're careful to check for an empty extent record here -
4421 * the merge code will know what to do if it sees one.
4424 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4425 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4428 ret = ocfs2_et_extent_contig(et, rec, split_rec);
4433 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4434 rec = &el->l_recs[index + 1];
4435 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4436 path->p_tree_depth > 0) {
4437 status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
4441 if (right_cpos == 0)
4444 right_path = ocfs2_new_path_from_path(path);
4448 status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
4452 new_el = path_leaf_el(right_path);
4453 rec = &new_el->l_recs[0];
4454 if (ocfs2_is_empty_extent(rec)) {
4455 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4456 bh = path_leaf_bh(right_path);
4457 eb = (struct ocfs2_extent_block *)bh->b_data;
4459 "Extent block #%llu has an "
4460 "invalid l_next_free_rec of %d",
4461 (unsigned long long)le64_to_cpu(eb->h_blkno),
4462 le16_to_cpu(new_el->l_next_free_rec));
4466 rec = &new_el->l_recs[1];
4471 enum ocfs2_contig_type contig_type;
4473 contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
4475 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4476 ret = CONTIG_LEFTRIGHT;
4477 else if (ret == CONTIG_NONE)
4483 ocfs2_free_path(left_path);
4485 ocfs2_free_path(right_path);
4490 static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
4491 struct ocfs2_insert_type *insert,
4492 struct ocfs2_extent_list *el,
4493 struct ocfs2_extent_rec *insert_rec)
4496 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4498 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4500 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4501 contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4503 if (contig_type != CONTIG_NONE) {
4504 insert->ins_contig_index = i;
4508 insert->ins_contig = contig_type;
4510 if (insert->ins_contig != CONTIG_NONE) {
4511 struct ocfs2_extent_rec *rec =
4512 &el->l_recs[insert->ins_contig_index];
4513 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4514 le16_to_cpu(insert_rec->e_leaf_clusters);
4517 * Caller might want us to limit the size of extents, don't
4518 * calculate contiguousness if we might exceed that limit.
4520 if (et->et_max_leaf_clusters &&
4521 (len > et->et_max_leaf_clusters))
4522 insert->ins_contig = CONTIG_NONE;
4527 * This should only be called against the righmost leaf extent list.
4529 * ocfs2_figure_appending_type() will figure out whether we'll have to
4530 * insert at the tail of the rightmost leaf.
4532 * This should also work against the root extent list for tree's with 0
4533 * depth. If we consider the root extent list to be the rightmost leaf node
4534 * then the logic here makes sense.
4536 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4537 struct ocfs2_extent_list *el,
4538 struct ocfs2_extent_rec *insert_rec)
4541 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4542 struct ocfs2_extent_rec *rec;
4544 insert->ins_appending = APPEND_NONE;
4546 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4548 if (!el->l_next_free_rec)
4549 goto set_tail_append;
4551 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4552 /* Were all records empty? */
4553 if (le16_to_cpu(el->l_next_free_rec) == 1)
4554 goto set_tail_append;
4557 i = le16_to_cpu(el->l_next_free_rec) - 1;
4558 rec = &el->l_recs[i];
4561 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4562 goto set_tail_append;
4567 insert->ins_appending = APPEND_TAIL;
4571 * Helper function called at the begining of an insert.
4573 * This computes a few things that are commonly used in the process of
4574 * inserting into the btree:
4575 * - Whether the new extent is contiguous with an existing one.
4576 * - The current tree depth.
4577 * - Whether the insert is an appending one.
4578 * - The total # of free records in the tree.
4580 * All of the information is stored on the ocfs2_insert_type
4583 static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
4584 struct buffer_head **last_eb_bh,
4585 struct ocfs2_extent_rec *insert_rec,
4587 struct ocfs2_insert_type *insert)
4590 struct ocfs2_extent_block *eb;
4591 struct ocfs2_extent_list *el;
4592 struct ocfs2_path *path = NULL;
4593 struct buffer_head *bh = NULL;
4595 insert->ins_split = SPLIT_NONE;
4597 el = et->et_root_el;
4598 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4600 if (el->l_tree_depth) {
4602 * If we have tree depth, we read in the
4603 * rightmost extent block ahead of time as
4604 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4605 * may want it later.
4607 ret = ocfs2_read_extent_block(et->et_ci,
4608 ocfs2_et_get_last_eb_blk(et),
4614 eb = (struct ocfs2_extent_block *) bh->b_data;
4619 * Unless we have a contiguous insert, we'll need to know if
4620 * there is room left in our allocation tree for another
4623 * XXX: This test is simplistic, we can search for empty
4624 * extent records too.
4626 *free_records = le16_to_cpu(el->l_count) -
4627 le16_to_cpu(el->l_next_free_rec);
4629 if (!insert->ins_tree_depth) {
4630 ocfs2_figure_contig_type(et, insert, el, insert_rec);
4631 ocfs2_figure_appending_type(insert, el, insert_rec);
4635 path = ocfs2_new_path_from_et(et);
4643 * In the case that we're inserting past what the tree
4644 * currently accounts for, ocfs2_find_path() will return for
4645 * us the rightmost tree path. This is accounted for below in
4646 * the appending code.
4648 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
4654 el = path_leaf_el(path);
4657 * Now that we have the path, there's two things we want to determine:
4658 * 1) Contiguousness (also set contig_index if this is so)
4660 * 2) Are we doing an append? We can trivially break this up
4661 * into two types of appends: simple record append, or a
4662 * rotate inside the tail leaf.
4664 ocfs2_figure_contig_type(et, insert, el, insert_rec);
4667 * The insert code isn't quite ready to deal with all cases of
4668 * left contiguousness. Specifically, if it's an insert into
4669 * the 1st record in a leaf, it will require the adjustment of
4670 * cluster count on the last record of the path directly to it's
4671 * left. For now, just catch that case and fool the layers
4672 * above us. This works just fine for tree_depth == 0, which
4673 * is why we allow that above.
4675 if (insert->ins_contig == CONTIG_LEFT &&
4676 insert->ins_contig_index == 0)
4677 insert->ins_contig = CONTIG_NONE;
4680 * Ok, so we can simply compare against last_eb to figure out
4681 * whether the path doesn't exist. This will only happen in
4682 * the case that we're doing a tail append, so maybe we can
4683 * take advantage of that information somehow.
4685 if (ocfs2_et_get_last_eb_blk(et) ==
4686 path_leaf_bh(path)->b_blocknr) {
4688 * Ok, ocfs2_find_path() returned us the rightmost
4689 * tree path. This might be an appending insert. There are
4691 * 1) We're doing a true append at the tail:
4692 * -This might even be off the end of the leaf
4693 * 2) We're "appending" by rotating in the tail
4695 ocfs2_figure_appending_type(insert, el, insert_rec);
4699 ocfs2_free_path(path);
4709 * Insert an extent into a btree.
4711 * The caller needs to update the owning btree's cluster count.
4713 int ocfs2_insert_extent(handle_t *handle,
4714 struct ocfs2_extent_tree *et,
4719 struct ocfs2_alloc_context *meta_ac)
4722 int uninitialized_var(free_records);
4723 struct buffer_head *last_eb_bh = NULL;
4724 struct ocfs2_insert_type insert = {0, };
4725 struct ocfs2_extent_rec rec;
4727 mlog(0, "add %u clusters at position %u to owner %llu\n",
4729 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
4731 memset(&rec, 0, sizeof(rec));
4732 rec.e_cpos = cpu_to_le32(cpos);
4733 rec.e_blkno = cpu_to_le64(start_blk);
4734 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4735 rec.e_flags = flags;
4736 status = ocfs2_et_insert_check(et, &rec);
4742 status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
4743 &free_records, &insert);
4749 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4750 "Insert.contig_index: %d, Insert.free_records: %d, "
4751 "Insert.tree_depth: %d\n",
4752 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4753 free_records, insert.ins_tree_depth);
4755 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4756 status = ocfs2_grow_tree(handle, et,
4757 &insert.ins_tree_depth, &last_eb_bh,
4765 /* Finally, we can add clusters. This might rotate the tree for us. */
4766 status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
4770 ocfs2_et_extent_map_insert(et, &rec);
4780 * Allcate and add clusters into the extent b-tree.
4781 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4782 * The extent b-tree's root is specified by et, and
4783 * it is not limited to the file storage. Any extent tree can use this
4784 * function if it implements the proper ocfs2_extent_tree.
4786 int ocfs2_add_clusters_in_btree(handle_t *handle,
4787 struct ocfs2_extent_tree *et,
4788 u32 *logical_offset,
4789 u32 clusters_to_add,
4791 struct ocfs2_alloc_context *data_ac,
4792 struct ocfs2_alloc_context *meta_ac,
4793 enum ocfs2_alloc_restarted *reason_ret)
4797 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4798 u32 bit_off, num_bits;
4801 struct ocfs2_super *osb =
4802 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
4804 BUG_ON(!clusters_to_add);
4807 flags = OCFS2_EXT_UNWRITTEN;
4809 free_extents = ocfs2_num_free_extents(osb, et);
4810 if (free_extents < 0) {
4811 status = free_extents;
4816 /* there are two cases which could cause us to EAGAIN in the
4817 * we-need-more-metadata case:
4818 * 1) we haven't reserved *any*
4819 * 2) we are so fragmented, we've needed to add metadata too
4821 if (!free_extents && !meta_ac) {
4822 mlog(0, "we haven't reserved any metadata!\n");
4824 reason = RESTART_META;
4826 } else if ((!free_extents)
4827 && (ocfs2_alloc_context_bits_left(meta_ac)
4828 < ocfs2_extend_meta_needed(et->et_root_el))) {
4829 mlog(0, "filesystem is really fragmented...\n");
4831 reason = RESTART_META;
4835 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4836 clusters_to_add, &bit_off, &num_bits);
4838 if (status != -ENOSPC)
4843 BUG_ON(num_bits > clusters_to_add);
4845 /* reserve our write early -- insert_extent may update the tree root */
4846 status = ocfs2_et_root_journal_access(handle, et,
4847 OCFS2_JOURNAL_ACCESS_WRITE);
4853 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4854 mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
4856 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
4857 status = ocfs2_insert_extent(handle, et, *logical_offset, block,
4858 num_bits, flags, meta_ac);
4864 status = ocfs2_journal_dirty(handle, et->et_root_bh);
4870 clusters_to_add -= num_bits;
4871 *logical_offset += num_bits;
4873 if (clusters_to_add) {
4874 mlog(0, "need to alloc once more, wanted = %u\n",
4877 reason = RESTART_TRANS;
4883 *reason_ret = reason;
4887 static void ocfs2_make_right_split_rec(struct super_block *sb,
4888 struct ocfs2_extent_rec *split_rec,
4890 struct ocfs2_extent_rec *rec)
4892 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4893 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4895 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4897 split_rec->e_cpos = cpu_to_le32(cpos);
4898 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4900 split_rec->e_blkno = rec->e_blkno;
4901 le64_add_cpu(&split_rec->e_blkno,
4902 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4904 split_rec->e_flags = rec->e_flags;
4907 static int ocfs2_split_and_insert(handle_t *handle,
4908 struct ocfs2_extent_tree *et,
4909 struct ocfs2_path *path,
4910 struct buffer_head **last_eb_bh,
4912 struct ocfs2_extent_rec *orig_split_rec,
4913 struct ocfs2_alloc_context *meta_ac)
4916 unsigned int insert_range, rec_range, do_leftright = 0;
4917 struct ocfs2_extent_rec tmprec;
4918 struct ocfs2_extent_list *rightmost_el;
4919 struct ocfs2_extent_rec rec;
4920 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4921 struct ocfs2_insert_type insert;
4922 struct ocfs2_extent_block *eb;
4926 * Store a copy of the record on the stack - it might move
4927 * around as the tree is manipulated below.
4929 rec = path_leaf_el(path)->l_recs[split_index];
4931 rightmost_el = et->et_root_el;
4933 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4935 BUG_ON(!(*last_eb_bh));
4936 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4937 rightmost_el = &eb->h_list;
4940 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4941 le16_to_cpu(rightmost_el->l_count)) {
4942 ret = ocfs2_grow_tree(handle, et,
4943 &depth, last_eb_bh, meta_ac);
4950 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4951 insert.ins_appending = APPEND_NONE;
4952 insert.ins_contig = CONTIG_NONE;
4953 insert.ins_tree_depth = depth;
4955 insert_range = le32_to_cpu(split_rec.e_cpos) +
4956 le16_to_cpu(split_rec.e_leaf_clusters);
4957 rec_range = le32_to_cpu(rec.e_cpos) +
4958 le16_to_cpu(rec.e_leaf_clusters);
4960 if (split_rec.e_cpos == rec.e_cpos) {
4961 insert.ins_split = SPLIT_LEFT;
4962 } else if (insert_range == rec_range) {
4963 insert.ins_split = SPLIT_RIGHT;
4966 * Left/right split. We fake this as a right split
4967 * first and then make a second pass as a left split.
4969 insert.ins_split = SPLIT_RIGHT;
4971 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4972 &tmprec, insert_range, &rec);
4976 BUG_ON(do_leftright);
4980 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
4986 if (do_leftright == 1) {
4988 struct ocfs2_extent_list *el;
4991 split_rec = *orig_split_rec;
4993 ocfs2_reinit_path(path, 1);
4995 cpos = le32_to_cpu(split_rec.e_cpos);
4996 ret = ocfs2_find_path(et->et_ci, path, cpos);
5002 el = path_leaf_el(path);
5003 split_index = ocfs2_search_extent_list(el, cpos);
5011 static int ocfs2_replace_extent_rec(handle_t *handle,
5012 struct ocfs2_extent_tree *et,
5013 struct ocfs2_path *path,
5014 struct ocfs2_extent_list *el,
5016 struct ocfs2_extent_rec *split_rec)
5020 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
5021 path_num_items(path) - 1);
5027 el->l_recs[split_index] = *split_rec;
5029 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5035 * Mark part or all of the extent record at split_index in the leaf
5036 * pointed to by path as written. This removes the unwritten
5039 * Care is taken to handle contiguousness so as to not grow the tree.
5041 * meta_ac is not strictly necessary - we only truly need it if growth
5042 * of the tree is required. All other cases will degrade into a less
5043 * optimal tree layout.
5045 * last_eb_bh should be the rightmost leaf block for any extent
5046 * btree. Since a split may grow the tree or a merge might shrink it,
5047 * the caller cannot trust the contents of that buffer after this call.
5049 * This code is optimized for readability - several passes might be
5050 * made over certain portions of the tree. All of those blocks will
5051 * have been brought into cache (and pinned via the journal), so the
5052 * extra overhead is not expressed in terms of disk reads.
5054 static int __ocfs2_mark_extent_written(handle_t *handle,
5055 struct ocfs2_extent_tree *et,
5056 struct ocfs2_path *path,
5058 struct ocfs2_extent_rec *split_rec,
5059 struct ocfs2_alloc_context *meta_ac,
5060 struct ocfs2_cached_dealloc_ctxt *dealloc)
5063 struct ocfs2_extent_list *el = path_leaf_el(path);
5064 struct buffer_head *last_eb_bh = NULL;
5065 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5066 struct ocfs2_merge_ctxt ctxt;
5067 struct ocfs2_extent_list *rightmost_el;
5069 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
5075 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5076 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5077 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5083 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
5088 * The core merge / split code wants to know how much room is
5089 * left in this allocation tree, so we pass the
5090 * rightmost extent list.
5092 if (path->p_tree_depth) {
5093 struct ocfs2_extent_block *eb;
5095 ret = ocfs2_read_extent_block(et->et_ci,
5096 ocfs2_et_get_last_eb_blk(et),
5103 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5104 rightmost_el = &eb->h_list;
5106 rightmost_el = path_root_el(path);
5108 if (rec->e_cpos == split_rec->e_cpos &&
5109 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5110 ctxt.c_split_covers_rec = 1;
5112 ctxt.c_split_covers_rec = 0;
5114 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5116 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5117 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5118 ctxt.c_split_covers_rec);
5120 if (ctxt.c_contig_type == CONTIG_NONE) {
5121 if (ctxt.c_split_covers_rec)
5122 ret = ocfs2_replace_extent_rec(handle, et, path, el,
5123 split_index, split_rec);
5125 ret = ocfs2_split_and_insert(handle, et, path,
5126 &last_eb_bh, split_index,
5127 split_rec, meta_ac);
5131 ret = ocfs2_try_to_merge_extent(handle, et, path,
5132 split_index, split_rec,
5144 * Mark the already-existing extent at cpos as written for len clusters.
5146 * If the existing extent is larger than the request, initiate a
5147 * split. An attempt will be made at merging with adjacent extents.
5149 * The caller is responsible for passing down meta_ac if we'll need it.
5151 int ocfs2_mark_extent_written(struct inode *inode,
5152 struct ocfs2_extent_tree *et,
5153 handle_t *handle, u32 cpos, u32 len, u32 phys,
5154 struct ocfs2_alloc_context *meta_ac,
5155 struct ocfs2_cached_dealloc_ctxt *dealloc)
5158 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
5159 struct ocfs2_extent_rec split_rec;
5160 struct ocfs2_path *left_path = NULL;
5161 struct ocfs2_extent_list *el;
5163 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5164 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
5166 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5167 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5168 "that are being written to, but the feature bit "
5169 "is not set in the super block.",
5170 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5176 * XXX: This should be fixed up so that we just re-insert the
5177 * next extent records.
5179 ocfs2_et_extent_map_truncate(et, 0);
5181 left_path = ocfs2_new_path_from_et(et);
5188 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
5193 el = path_leaf_el(left_path);
5195 index = ocfs2_search_extent_list(el, cpos);
5196 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5197 ocfs2_error(inode->i_sb,
5198 "Inode %llu has an extent at cpos %u which can no "
5199 "longer be found.\n",
5200 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5205 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5206 split_rec.e_cpos = cpu_to_le32(cpos);
5207 split_rec.e_leaf_clusters = cpu_to_le16(len);
5208 split_rec.e_blkno = cpu_to_le64(start_blkno);
5209 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
5210 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
5212 ret = __ocfs2_mark_extent_written(handle, et, left_path,
5213 index, &split_rec, meta_ac,
5219 ocfs2_free_path(left_path);
5223 static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5224 struct ocfs2_path *path,
5225 int index, u32 new_range,
5226 struct ocfs2_alloc_context *meta_ac)
5228 int ret, depth, credits = handle->h_buffer_credits;
5229 struct buffer_head *last_eb_bh = NULL;
5230 struct ocfs2_extent_block *eb;
5231 struct ocfs2_extent_list *rightmost_el, *el;
5232 struct ocfs2_extent_rec split_rec;
5233 struct ocfs2_extent_rec *rec;
5234 struct ocfs2_insert_type insert;
5237 * Setup the record to split before we grow the tree.
5239 el = path_leaf_el(path);
5240 rec = &el->l_recs[index];
5241 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5242 &split_rec, new_range, rec);
5244 depth = path->p_tree_depth;
5246 ret = ocfs2_read_extent_block(et->et_ci,
5247 ocfs2_et_get_last_eb_blk(et),
5254 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5255 rightmost_el = &eb->h_list;
5257 rightmost_el = path_leaf_el(path);
5259 credits += path->p_tree_depth +
5260 ocfs2_extend_meta_needed(et->et_root_el);
5261 ret = ocfs2_extend_trans(handle, credits);
5267 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5268 le16_to_cpu(rightmost_el->l_count)) {
5269 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
5277 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5278 insert.ins_appending = APPEND_NONE;
5279 insert.ins_contig = CONTIG_NONE;
5280 insert.ins_split = SPLIT_RIGHT;
5281 insert.ins_tree_depth = depth;
5283 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
5292 static int ocfs2_truncate_rec(handle_t *handle,
5293 struct ocfs2_extent_tree *et,
5294 struct ocfs2_path *path, int index,
5295 struct ocfs2_cached_dealloc_ctxt *dealloc,
5299 u32 left_cpos, rec_range, trunc_range;
5300 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5301 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5302 struct ocfs2_path *left_path = NULL;
5303 struct ocfs2_extent_list *el = path_leaf_el(path);
5304 struct ocfs2_extent_rec *rec;
5305 struct ocfs2_extent_block *eb;
5307 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5308 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5317 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5318 path->p_tree_depth) {
5320 * Check whether this is the rightmost tree record. If
5321 * we remove all of this record or part of its right
5322 * edge then an update of the record lengths above it
5325 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5326 if (eb->h_next_leaf_blk == 0)
5327 is_rightmost_tree_rec = 1;
5330 rec = &el->l_recs[index];
5331 if (index == 0 && path->p_tree_depth &&
5332 le32_to_cpu(rec->e_cpos) == cpos) {
5334 * Changing the leftmost offset (via partial or whole
5335 * record truncate) of an interior (or rightmost) path
5336 * means we have to update the subtree that is formed
5337 * by this leaf and the one to it's left.
5339 * There are two cases we can skip:
5340 * 1) Path is the leftmost one in our btree.
5341 * 2) The leaf is rightmost and will be empty after
5342 * we remove the extent record - the rotate code
5343 * knows how to update the newly formed edge.
5346 ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
5352 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5353 left_path = ocfs2_new_path_from_path(path);
5360 ret = ocfs2_find_path(et->et_ci, left_path,
5369 ret = ocfs2_extend_rotate_transaction(handle, 0,
5370 handle->h_buffer_credits,
5377 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
5383 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
5389 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5390 trunc_range = cpos + len;
5392 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5395 memset(rec, 0, sizeof(*rec));
5396 ocfs2_cleanup_merge(el, index);
5399 next_free = le16_to_cpu(el->l_next_free_rec);
5400 if (is_rightmost_tree_rec && next_free > 1) {
5402 * We skip the edge update if this path will
5403 * be deleted by the rotate code.
5405 rec = &el->l_recs[next_free - 1];
5406 ocfs2_adjust_rightmost_records(handle, et, path,
5409 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5410 /* Remove leftmost portion of the record. */
5411 le32_add_cpu(&rec->e_cpos, len);
5412 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5413 le16_add_cpu(&rec->e_leaf_clusters, -len);
5414 } else if (rec_range == trunc_range) {
5415 /* Remove rightmost portion of the record */
5416 le16_add_cpu(&rec->e_leaf_clusters, -len);
5417 if (is_rightmost_tree_rec)
5418 ocfs2_adjust_rightmost_records(handle, et, path, rec);
5420 /* Caller should have trapped this. */
5421 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5423 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5424 le32_to_cpu(rec->e_cpos),
5425 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5432 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
5433 ocfs2_complete_edge_insert(handle, left_path, path,
5437 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5439 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5446 ocfs2_free_path(left_path);
5450 int ocfs2_remove_extent(handle_t *handle,
5451 struct ocfs2_extent_tree *et,
5453 struct ocfs2_alloc_context *meta_ac,
5454 struct ocfs2_cached_dealloc_ctxt *dealloc)
5457 u32 rec_range, trunc_range;
5458 struct ocfs2_extent_rec *rec;
5459 struct ocfs2_extent_list *el;
5460 struct ocfs2_path *path = NULL;
5463 * XXX: Why are we truncating to 0 instead of wherever this
5466 ocfs2_et_extent_map_truncate(et, 0);
5468 path = ocfs2_new_path_from_et(et);
5475 ret = ocfs2_find_path(et->et_ci, path, cpos);
5481 el = path_leaf_el(path);
5482 index = ocfs2_search_extent_list(el, cpos);
5483 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5484 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5485 "Owner %llu has an extent at cpos %u which can no "
5486 "longer be found.\n",
5487 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5494 * We have 3 cases of extent removal:
5495 * 1) Range covers the entire extent rec
5496 * 2) Range begins or ends on one edge of the extent rec
5497 * 3) Range is in the middle of the extent rec (no shared edges)
5499 * For case 1 we remove the extent rec and left rotate to
5502 * For case 2 we just shrink the existing extent rec, with a
5503 * tree update if the shrinking edge is also the edge of an
5506 * For case 3 we do a right split to turn the extent rec into
5507 * something case 2 can handle.
5509 rec = &el->l_recs[index];
5510 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5511 trunc_range = cpos + len;
5513 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5515 mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
5516 "(cpos %u, len %u)\n",
5517 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5519 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5521 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5522 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5529 ret = ocfs2_split_tree(handle, et, path, index,
5530 trunc_range, meta_ac);
5537 * The split could have manipulated the tree enough to
5538 * move the record location, so we have to look for it again.
5540 ocfs2_reinit_path(path, 1);
5542 ret = ocfs2_find_path(et->et_ci, path, cpos);
5548 el = path_leaf_el(path);
5549 index = ocfs2_search_extent_list(el, cpos);
5550 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5551 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5552 "Owner %llu: split at cpos %u lost record.",
5553 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5560 * Double check our values here. If anything is fishy,
5561 * it's easier to catch it at the top level.
5563 rec = &el->l_recs[index];
5564 rec_range = le32_to_cpu(rec->e_cpos) +
5565 ocfs2_rec_clusters(el, rec);
5566 if (rec_range != trunc_range) {
5567 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5568 "Owner %llu: error after split at cpos %u"
5569 "trunc len %u, existing record is (%u,%u)",
5570 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5571 cpos, len, le32_to_cpu(rec->e_cpos),
5572 ocfs2_rec_clusters(el, rec));
5577 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5586 ocfs2_free_path(path);
5590 int ocfs2_remove_btree_range(struct inode *inode,
5591 struct ocfs2_extent_tree *et,
5592 u32 cpos, u32 phys_cpos, u32 len,
5593 struct ocfs2_cached_dealloc_ctxt *dealloc)
5596 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5597 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5598 struct inode *tl_inode = osb->osb_tl_inode;
5600 struct ocfs2_alloc_context *meta_ac = NULL;
5602 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5608 mutex_lock(&tl_inode->i_mutex);
5610 if (ocfs2_truncate_log_needs_flush(osb)) {
5611 ret = __ocfs2_flush_truncate_log(osb);
5618 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5619 if (IS_ERR(handle)) {
5620 ret = PTR_ERR(handle);
5625 ret = ocfs2_et_root_journal_access(handle, et,
5626 OCFS2_JOURNAL_ACCESS_WRITE);
5632 vfs_dq_free_space_nodirty(inode,
5633 ocfs2_clusters_to_bytes(inode->i_sb, len));
5635 ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
5641 ocfs2_et_update_clusters(et, -len);
5643 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5649 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5654 ocfs2_commit_trans(osb, handle);
5656 mutex_unlock(&tl_inode->i_mutex);
5659 ocfs2_free_alloc_context(meta_ac);
5664 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5666 struct buffer_head *tl_bh = osb->osb_tl_bh;
5667 struct ocfs2_dinode *di;
5668 struct ocfs2_truncate_log *tl;
5670 di = (struct ocfs2_dinode *) tl_bh->b_data;
5671 tl = &di->id2.i_dealloc;
5673 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5674 "slot %d, invalid truncate log parameters: used = "
5675 "%u, count = %u\n", osb->slot_num,
5676 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5677 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5680 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5681 unsigned int new_start)
5683 unsigned int tail_index;
5684 unsigned int current_tail;
5686 /* No records, nothing to coalesce */
5687 if (!le16_to_cpu(tl->tl_used))
5690 tail_index = le16_to_cpu(tl->tl_used) - 1;
5691 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5692 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5694 return current_tail == new_start;
5697 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5700 unsigned int num_clusters)
5703 unsigned int start_cluster, tl_count;
5704 struct inode *tl_inode = osb->osb_tl_inode;
5705 struct buffer_head *tl_bh = osb->osb_tl_bh;
5706 struct ocfs2_dinode *di;
5707 struct ocfs2_truncate_log *tl;
5709 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5710 (unsigned long long)start_blk, num_clusters);
5712 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5714 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5716 di = (struct ocfs2_dinode *) tl_bh->b_data;
5718 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5719 * by the underlying call to ocfs2_read_inode_block(), so any
5720 * corruption is a code bug */
5721 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5723 tl = &di->id2.i_dealloc;
5724 tl_count = le16_to_cpu(tl->tl_count);
5725 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5727 "Truncate record count on #%llu invalid "
5728 "wanted %u, actual %u\n",
5729 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5730 ocfs2_truncate_recs_per_inode(osb->sb),
5731 le16_to_cpu(tl->tl_count));
5733 /* Caller should have known to flush before calling us. */
5734 index = le16_to_cpu(tl->tl_used);
5735 if (index >= tl_count) {
5741 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5742 OCFS2_JOURNAL_ACCESS_WRITE);
5748 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5749 "%llu (index = %d)\n", num_clusters, start_cluster,
5750 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5752 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5754 * Move index back to the record we are coalescing with.
5755 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5759 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5760 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5761 index, le32_to_cpu(tl->tl_recs[index].t_start),
5764 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5765 tl->tl_used = cpu_to_le16(index + 1);
5767 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5769 status = ocfs2_journal_dirty(handle, tl_bh);
5780 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5782 struct inode *data_alloc_inode,
5783 struct buffer_head *data_alloc_bh)
5787 unsigned int num_clusters;
5789 struct ocfs2_truncate_rec rec;
5790 struct ocfs2_dinode *di;
5791 struct ocfs2_truncate_log *tl;
5792 struct inode *tl_inode = osb->osb_tl_inode;
5793 struct buffer_head *tl_bh = osb->osb_tl_bh;
5797 di = (struct ocfs2_dinode *) tl_bh->b_data;
5798 tl = &di->id2.i_dealloc;
5799 i = le16_to_cpu(tl->tl_used) - 1;
5801 /* Caller has given us at least enough credits to
5802 * update the truncate log dinode */
5803 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5804 OCFS2_JOURNAL_ACCESS_WRITE);
5810 tl->tl_used = cpu_to_le16(i);
5812 status = ocfs2_journal_dirty(handle, tl_bh);
5818 /* TODO: Perhaps we can calculate the bulk of the
5819 * credits up front rather than extending like
5821 status = ocfs2_extend_trans(handle,
5822 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5828 rec = tl->tl_recs[i];
5829 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5830 le32_to_cpu(rec.t_start));
5831 num_clusters = le32_to_cpu(rec.t_clusters);
5833 /* if start_blk is not set, we ignore the record as
5836 mlog(0, "free record %d, start = %u, clusters = %u\n",
5837 i, le32_to_cpu(rec.t_start), num_clusters);
5839 status = ocfs2_free_clusters(handle, data_alloc_inode,
5840 data_alloc_bh, start_blk,
5855 /* Expects you to already be holding tl_inode->i_mutex */
5856 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5859 unsigned int num_to_flush;
5861 struct inode *tl_inode = osb->osb_tl_inode;
5862 struct inode *data_alloc_inode = NULL;
5863 struct buffer_head *tl_bh = osb->osb_tl_bh;
5864 struct buffer_head *data_alloc_bh = NULL;
5865 struct ocfs2_dinode *di;
5866 struct ocfs2_truncate_log *tl;
5870 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5872 di = (struct ocfs2_dinode *) tl_bh->b_data;
5874 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5875 * by the underlying call to ocfs2_read_inode_block(), so any
5876 * corruption is a code bug */
5877 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5879 tl = &di->id2.i_dealloc;
5880 num_to_flush = le16_to_cpu(tl->tl_used);
5881 mlog(0, "Flush %u records from truncate log #%llu\n",
5882 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5883 if (!num_to_flush) {
5888 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5889 GLOBAL_BITMAP_SYSTEM_INODE,
5890 OCFS2_INVALID_SLOT);
5891 if (!data_alloc_inode) {
5893 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5897 mutex_lock(&data_alloc_inode->i_mutex);
5899 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5905 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5906 if (IS_ERR(handle)) {
5907 status = PTR_ERR(handle);
5912 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5917 ocfs2_commit_trans(osb, handle);
5920 brelse(data_alloc_bh);
5921 ocfs2_inode_unlock(data_alloc_inode, 1);
5924 mutex_unlock(&data_alloc_inode->i_mutex);
5925 iput(data_alloc_inode);
5932 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5935 struct inode *tl_inode = osb->osb_tl_inode;
5937 mutex_lock(&tl_inode->i_mutex);
5938 status = __ocfs2_flush_truncate_log(osb);
5939 mutex_unlock(&tl_inode->i_mutex);
5944 static void ocfs2_truncate_log_worker(struct work_struct *work)
5947 struct ocfs2_super *osb =
5948 container_of(work, struct ocfs2_super,
5949 osb_truncate_log_wq.work);
5953 status = ocfs2_flush_truncate_log(osb);
5957 ocfs2_init_inode_steal_slot(osb);
5962 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5963 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5966 if (osb->osb_tl_inode) {
5967 /* We want to push off log flushes while truncates are
5970 cancel_delayed_work(&osb->osb_truncate_log_wq);
5972 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5973 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5977 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5979 struct inode **tl_inode,
5980 struct buffer_head **tl_bh)
5983 struct inode *inode = NULL;
5984 struct buffer_head *bh = NULL;
5986 inode = ocfs2_get_system_file_inode(osb,
5987 TRUNCATE_LOG_SYSTEM_INODE,
5991 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5995 status = ocfs2_read_inode_block(inode, &bh);
6009 /* called during the 1st stage of node recovery. we stamp a clean
6010 * truncate log and pass back a copy for processing later. if the
6011 * truncate log does not require processing, a *tl_copy is set to
6013 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6015 struct ocfs2_dinode **tl_copy)
6018 struct inode *tl_inode = NULL;
6019 struct buffer_head *tl_bh = NULL;
6020 struct ocfs2_dinode *di;
6021 struct ocfs2_truncate_log *tl;
6025 mlog(0, "recover truncate log from slot %d\n", slot_num);
6027 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6033 di = (struct ocfs2_dinode *) tl_bh->b_data;
6035 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6036 * validated by the underlying call to ocfs2_read_inode_block(),
6037 * so any corruption is a code bug */
6038 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6040 tl = &di->id2.i_dealloc;
6041 if (le16_to_cpu(tl->tl_used)) {
6042 mlog(0, "We'll have %u logs to recover\n",
6043 le16_to_cpu(tl->tl_used));
6045 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6052 /* Assuming the write-out below goes well, this copy
6053 * will be passed back to recovery for processing. */
6054 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6056 /* All we need to do to clear the truncate log is set
6060 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
6061 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
6073 if (status < 0 && (*tl_copy)) {
6082 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6083 struct ocfs2_dinode *tl_copy)
6087 unsigned int clusters, num_recs, start_cluster;
6090 struct inode *tl_inode = osb->osb_tl_inode;
6091 struct ocfs2_truncate_log *tl;
6095 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6096 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6100 tl = &tl_copy->id2.i_dealloc;
6101 num_recs = le16_to_cpu(tl->tl_used);
6102 mlog(0, "cleanup %u records from %llu\n", num_recs,
6103 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
6105 mutex_lock(&tl_inode->i_mutex);
6106 for(i = 0; i < num_recs; i++) {
6107 if (ocfs2_truncate_log_needs_flush(osb)) {
6108 status = __ocfs2_flush_truncate_log(osb);
6115 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6116 if (IS_ERR(handle)) {
6117 status = PTR_ERR(handle);
6122 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6123 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6124 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6126 status = ocfs2_truncate_log_append(osb, handle,
6127 start_blk, clusters);
6128 ocfs2_commit_trans(osb, handle);
6136 mutex_unlock(&tl_inode->i_mutex);
6142 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6145 struct inode *tl_inode = osb->osb_tl_inode;
6150 cancel_delayed_work(&osb->osb_truncate_log_wq);
6151 flush_workqueue(ocfs2_wq);
6153 status = ocfs2_flush_truncate_log(osb);
6157 brelse(osb->osb_tl_bh);
6158 iput(osb->osb_tl_inode);
6164 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6167 struct inode *tl_inode = NULL;
6168 struct buffer_head *tl_bh = NULL;
6172 status = ocfs2_get_truncate_log_info(osb,
6179 /* ocfs2_truncate_log_shutdown keys on the existence of
6180 * osb->osb_tl_inode so we don't set any of the osb variables
6181 * until we're sure all is well. */
6182 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6183 ocfs2_truncate_log_worker);
6184 osb->osb_tl_bh = tl_bh;
6185 osb->osb_tl_inode = tl_inode;
6192 * Delayed de-allocation of suballocator blocks.
6194 * Some sets of block de-allocations might involve multiple suballocator inodes.
6196 * The locking for this can get extremely complicated, especially when
6197 * the suballocator inodes to delete from aren't known until deep
6198 * within an unrelated codepath.
6200 * ocfs2_extent_block structures are a good example of this - an inode
6201 * btree could have been grown by any number of nodes each allocating
6202 * out of their own suballoc inode.
6204 * These structures allow the delay of block de-allocation until a
6205 * later time, when locking of multiple cluster inodes won't cause
6210 * Describe a single bit freed from a suballocator. For the block
6211 * suballocators, it represents one block. For the global cluster
6212 * allocator, it represents some clusters and free_bit indicates
6215 struct ocfs2_cached_block_free {
6216 struct ocfs2_cached_block_free *free_next;
6218 unsigned int free_bit;
6221 struct ocfs2_per_slot_free_list {
6222 struct ocfs2_per_slot_free_list *f_next_suballocator;
6225 struct ocfs2_cached_block_free *f_first;
6228 static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6231 struct ocfs2_cached_block_free *head)
6236 struct inode *inode;
6237 struct buffer_head *di_bh = NULL;
6238 struct ocfs2_cached_block_free *tmp;
6240 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6247 mutex_lock(&inode->i_mutex);
6249 ret = ocfs2_inode_lock(inode, &di_bh, 1);
6255 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6256 if (IS_ERR(handle)) {
6257 ret = PTR_ERR(handle);
6263 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6265 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6266 head->free_bit, (unsigned long long)head->free_blk);
6268 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6269 head->free_bit, bg_blkno, 1);
6275 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6282 head = head->free_next;
6287 ocfs2_commit_trans(osb, handle);
6290 ocfs2_inode_unlock(inode, 1);
6293 mutex_unlock(&inode->i_mutex);
6297 /* Premature exit may have left some dangling items. */
6299 head = head->free_next;
6306 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6307 u64 blkno, unsigned int bit)
6310 struct ocfs2_cached_block_free *item;
6312 item = kmalloc(sizeof(*item), GFP_NOFS);
6319 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6320 bit, (unsigned long long)blkno);
6322 item->free_blk = blkno;
6323 item->free_bit = bit;
6324 item->free_next = ctxt->c_global_allocator;
6326 ctxt->c_global_allocator = item;
6330 static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6331 struct ocfs2_cached_block_free *head)
6333 struct ocfs2_cached_block_free *tmp;
6334 struct inode *tl_inode = osb->osb_tl_inode;
6338 mutex_lock(&tl_inode->i_mutex);
6341 if (ocfs2_truncate_log_needs_flush(osb)) {
6342 ret = __ocfs2_flush_truncate_log(osb);
6349 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6350 if (IS_ERR(handle)) {
6351 ret = PTR_ERR(handle);
6356 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6359 ocfs2_commit_trans(osb, handle);
6361 head = head->free_next;
6370 mutex_unlock(&tl_inode->i_mutex);
6373 /* Premature exit may have left some dangling items. */
6375 head = head->free_next;
6382 int ocfs2_run_deallocs(struct ocfs2_super *osb,
6383 struct ocfs2_cached_dealloc_ctxt *ctxt)
6386 struct ocfs2_per_slot_free_list *fl;
6391 while (ctxt->c_first_suballocator) {
6392 fl = ctxt->c_first_suballocator;
6395 mlog(0, "Free items: (type %u, slot %d)\n",
6396 fl->f_inode_type, fl->f_slot);
6397 ret2 = ocfs2_free_cached_blocks(osb,
6407 ctxt->c_first_suballocator = fl->f_next_suballocator;
6411 if (ctxt->c_global_allocator) {
6412 ret2 = ocfs2_free_cached_clusters(osb,
6413 ctxt->c_global_allocator);
6419 ctxt->c_global_allocator = NULL;
6425 static struct ocfs2_per_slot_free_list *
6426 ocfs2_find_per_slot_free_list(int type,
6428 struct ocfs2_cached_dealloc_ctxt *ctxt)
6430 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6433 if (fl->f_inode_type == type && fl->f_slot == slot)
6436 fl = fl->f_next_suballocator;
6439 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6441 fl->f_inode_type = type;
6444 fl->f_next_suballocator = ctxt->c_first_suballocator;
6446 ctxt->c_first_suballocator = fl;
6451 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6452 int type, int slot, u64 blkno,
6456 struct ocfs2_per_slot_free_list *fl;
6457 struct ocfs2_cached_block_free *item;
6459 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6466 item = kmalloc(sizeof(*item), GFP_NOFS);
6473 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6474 type, slot, bit, (unsigned long long)blkno);
6476 item->free_blk = blkno;
6477 item->free_bit = bit;
6478 item->free_next = fl->f_first;
6487 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6488 struct ocfs2_extent_block *eb)
6490 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6491 le16_to_cpu(eb->h_suballoc_slot),
6492 le64_to_cpu(eb->h_blkno),
6493 le16_to_cpu(eb->h_suballoc_bit));
6496 /* This function will figure out whether the currently last extent
6497 * block will be deleted, and if it will, what the new last extent
6498 * block will be so we can update his h_next_leaf_blk field, as well
6499 * as the dinodes i_last_eb_blk */
6500 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
6501 unsigned int clusters_to_del,
6502 struct ocfs2_path *path,
6503 struct buffer_head **new_last_eb)
6505 int next_free, ret = 0;
6507 struct ocfs2_extent_rec *rec;
6508 struct ocfs2_extent_block *eb;
6509 struct ocfs2_extent_list *el;
6510 struct buffer_head *bh = NULL;
6512 *new_last_eb = NULL;
6514 /* we have no tree, so of course, no last_eb. */
6515 if (!path->p_tree_depth)
6518 /* trunc to zero special case - this makes tree_depth = 0
6519 * regardless of what it is. */
6520 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
6523 el = path_leaf_el(path);
6524 BUG_ON(!el->l_next_free_rec);
6527 * Make sure that this extent list will actually be empty
6528 * after we clear away the data. We can shortcut out if
6529 * there's more than one non-empty extent in the
6530 * list. Otherwise, a check of the remaining extent is
6533 next_free = le16_to_cpu(el->l_next_free_rec);
6535 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6539 /* We may have a valid extent in index 1, check it. */
6541 rec = &el->l_recs[1];
6544 * Fall through - no more nonempty extents, so we want
6545 * to delete this leaf.
6551 rec = &el->l_recs[0];
6556 * Check it we'll only be trimming off the end of this
6559 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
6563 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6569 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
6575 eb = (struct ocfs2_extent_block *) bh->b_data;
6578 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6579 * Any corruption is a code bug. */
6580 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
6583 get_bh(*new_last_eb);
6584 mlog(0, "returning block %llu, (cpos: %u)\n",
6585 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6593 * Trim some clusters off the rightmost edge of a tree. Only called
6596 * The caller needs to:
6597 * - start journaling of each path component.
6598 * - compute and fully set up any new last ext block
6600 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6601 handle_t *handle, struct ocfs2_truncate_context *tc,
6602 u32 clusters_to_del, u64 *delete_start)
6604 int ret, i, index = path->p_tree_depth;
6607 struct buffer_head *bh;
6608 struct ocfs2_extent_list *el;
6609 struct ocfs2_extent_rec *rec;
6613 while (index >= 0) {
6614 bh = path->p_node[index].bh;
6615 el = path->p_node[index].el;
6617 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6618 index, (unsigned long long)bh->b_blocknr);
6620 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6623 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6624 ocfs2_error(inode->i_sb,
6625 "Inode %lu has invalid ext. block %llu",
6627 (unsigned long long)bh->b_blocknr);
6633 i = le16_to_cpu(el->l_next_free_rec) - 1;
6634 rec = &el->l_recs[i];
6636 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6637 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
6638 ocfs2_rec_clusters(el, rec),
6639 (unsigned long long)le64_to_cpu(rec->e_blkno),
6640 le16_to_cpu(el->l_next_free_rec));
6642 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
6644 if (le16_to_cpu(el->l_tree_depth) == 0) {
6646 * If the leaf block contains a single empty
6647 * extent and no records, we can just remove
6650 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6652 sizeof(struct ocfs2_extent_rec));
6653 el->l_next_free_rec = cpu_to_le16(0);
6659 * Remove any empty extents by shifting things
6660 * left. That should make life much easier on
6661 * the code below. This condition is rare
6662 * enough that we shouldn't see a performance
6665 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6666 le16_add_cpu(&el->l_next_free_rec, -1);
6669 i < le16_to_cpu(el->l_next_free_rec); i++)
6670 el->l_recs[i] = el->l_recs[i + 1];
6672 memset(&el->l_recs[i], 0,
6673 sizeof(struct ocfs2_extent_rec));
6676 * We've modified our extent list. The
6677 * simplest way to handle this change
6678 * is to being the search from the
6681 goto find_tail_record;
6684 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
6687 * We'll use "new_edge" on our way back up the
6688 * tree to know what our rightmost cpos is.
6690 new_edge = le16_to_cpu(rec->e_leaf_clusters);
6691 new_edge += le32_to_cpu(rec->e_cpos);
6694 * The caller will use this to delete data blocks.
6696 *delete_start = le64_to_cpu(rec->e_blkno)
6697 + ocfs2_clusters_to_blocks(inode->i_sb,
6698 le16_to_cpu(rec->e_leaf_clusters));
6701 * If it's now empty, remove this record.
6703 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
6705 sizeof(struct ocfs2_extent_rec));
6706 le16_add_cpu(&el->l_next_free_rec, -1);
6709 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6711 sizeof(struct ocfs2_extent_rec));
6712 le16_add_cpu(&el->l_next_free_rec, -1);
6717 /* Can this actually happen? */
6718 if (le16_to_cpu(el->l_next_free_rec) == 0)
6722 * We never actually deleted any clusters
6723 * because our leaf was empty. There's no
6724 * reason to adjust the rightmost edge then.
6729 rec->e_int_clusters = cpu_to_le32(new_edge);
6730 le32_add_cpu(&rec->e_int_clusters,
6731 -le32_to_cpu(rec->e_cpos));
6734 * A deleted child record should have been
6737 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
6741 ret = ocfs2_journal_dirty(handle, bh);
6747 mlog(0, "extent list container %llu, after: record %d: "
6748 "(%u, %u, %llu), next = %u.\n",
6749 (unsigned long long)bh->b_blocknr, i,
6750 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
6751 (unsigned long long)le64_to_cpu(rec->e_blkno),
6752 le16_to_cpu(el->l_next_free_rec));
6755 * We must be careful to only attempt delete of an
6756 * extent block (and not the root inode block).
6758 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6759 struct ocfs2_extent_block *eb =
6760 (struct ocfs2_extent_block *)bh->b_data;
6763 * Save this for use when processing the
6766 deleted_eb = le64_to_cpu(eb->h_blkno);
6768 mlog(0, "deleting this extent block.\n");
6770 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
6772 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
6773 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6774 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6776 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6777 /* An error here is not fatal. */
6792 static int ocfs2_do_truncate(struct ocfs2_super *osb,
6793 unsigned int clusters_to_del,
6794 struct inode *inode,
6795 struct buffer_head *fe_bh,
6797 struct ocfs2_truncate_context *tc,
6798 struct ocfs2_path *path)
6801 struct ocfs2_dinode *fe;
6802 struct ocfs2_extent_block *last_eb = NULL;
6803 struct ocfs2_extent_list *el;
6804 struct buffer_head *last_eb_bh = NULL;
6807 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6809 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
6817 * Each component will be touched, so we might as well journal
6818 * here to avoid having to handle errors later.
6820 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
6827 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
6828 OCFS2_JOURNAL_ACCESS_WRITE);
6834 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6837 el = &(fe->id2.i_list);
6840 * Lower levels depend on this never happening, but it's best
6841 * to check it up here before changing the tree.
6843 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
6844 ocfs2_error(inode->i_sb,
6845 "Inode %lu has an empty extent record, depth %u\n",
6846 inode->i_ino, le16_to_cpu(el->l_tree_depth));
6851 vfs_dq_free_space_nodirty(inode,
6852 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
6853 spin_lock(&OCFS2_I(inode)->ip_lock);
6854 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6856 spin_unlock(&OCFS2_I(inode)->ip_lock);
6857 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
6858 inode->i_blocks = ocfs2_inode_sector_count(inode);
6860 status = ocfs2_trim_tree(inode, path, handle, tc,
6861 clusters_to_del, &delete_blk);
6867 if (le32_to_cpu(fe->i_clusters) == 0) {
6868 /* trunc to zero is a special case. */
6869 el->l_tree_depth = 0;
6870 fe->i_last_eb_blk = 0;
6872 fe->i_last_eb_blk = last_eb->h_blkno;
6874 status = ocfs2_journal_dirty(handle, fe_bh);
6881 /* If there will be a new last extent block, then by
6882 * definition, there cannot be any leaves to the right of
6884 last_eb->h_next_leaf_blk = 0;
6885 status = ocfs2_journal_dirty(handle, last_eb_bh);
6893 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6907 static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
6909 set_buffer_uptodate(bh);
6910 mark_buffer_dirty(bh);
6914 static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6915 unsigned int from, unsigned int to,
6916 struct page *page, int zero, u64 *phys)
6918 int ret, partial = 0;
6920 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6925 zero_user_segment(page, from, to);
6928 * Need to set the buffers we zero'd into uptodate
6929 * here if they aren't - ocfs2_map_page_blocks()
6930 * might've skipped some
6932 ret = walk_page_buffers(handle, page_buffers(page),
6937 else if (ocfs2_should_order_data(inode)) {
6938 ret = ocfs2_jbd2_file_inode(handle, inode);
6944 SetPageUptodate(page);
6946 flush_dcache_page(page);
6949 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6950 loff_t end, struct page **pages,
6951 int numpages, u64 phys, handle_t *handle)
6955 unsigned int from, to = PAGE_CACHE_SIZE;
6956 struct super_block *sb = inode->i_sb;
6958 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6963 to = PAGE_CACHE_SIZE;
6964 for(i = 0; i < numpages; i++) {
6967 from = start & (PAGE_CACHE_SIZE - 1);
6968 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6969 to = end & (PAGE_CACHE_SIZE - 1);
6971 BUG_ON(from > PAGE_CACHE_SIZE);
6972 BUG_ON(to > PAGE_CACHE_SIZE);
6974 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6977 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6981 ocfs2_unlock_and_free_pages(pages, numpages);
6984 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6985 struct page **pages, int *num)
6987 int numpages, ret = 0;
6988 struct super_block *sb = inode->i_sb;
6989 struct address_space *mapping = inode->i_mapping;
6990 unsigned long index;
6991 loff_t last_page_bytes;
6993 BUG_ON(start > end);
6995 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6996 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6999 last_page_bytes = PAGE_ALIGN(end);
7000 index = start >> PAGE_CACHE_SHIFT;
7002 pages[numpages] = grab_cache_page(mapping, index);
7003 if (!pages[numpages]) {
7011 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
7016 ocfs2_unlock_and_free_pages(pages, numpages);
7026 * Zero the area past i_size but still within an allocated
7027 * cluster. This avoids exposing nonzero data on subsequent file
7030 * We need to call this before i_size is updated on the inode because
7031 * otherwise block_write_full_page() will skip writeout of pages past
7032 * i_size. The new_i_size parameter is passed for this reason.
7034 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
7035 u64 range_start, u64 range_end)
7037 int ret = 0, numpages;
7038 struct page **pages = NULL;
7040 unsigned int ext_flags;
7041 struct super_block *sb = inode->i_sb;
7044 * File systems which don't support sparse files zero on every
7047 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
7050 pages = kcalloc(ocfs2_pages_per_cluster(sb),
7051 sizeof(struct page *), GFP_NOFS);
7052 if (pages == NULL) {
7058 if (range_start == range_end)
7061 ret = ocfs2_extent_map_get_blocks(inode,
7062 range_start >> sb->s_blocksize_bits,
7063 &phys, NULL, &ext_flags);
7070 * Tail is a hole, or is marked unwritten. In either case, we
7071 * can count on read and write to return/push zero's.
7073 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
7076 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7083 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7084 numpages, phys, handle);
7087 * Initiate writeout of the pages we zero'd here. We don't
7088 * wait on them - the truncate_inode_pages() call later will
7091 ret = do_sync_mapping_range(inode->i_mapping, range_start,
7092 range_end - 1, SYNC_FILE_RANGE_WRITE);
7103 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7104 struct ocfs2_dinode *di)
7106 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
7107 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
7109 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7110 memset(&di->id2, 0, blocksize -
7111 offsetof(struct ocfs2_dinode, id2) -
7114 memset(&di->id2, 0, blocksize -
7115 offsetof(struct ocfs2_dinode, id2));
7118 void ocfs2_dinode_new_extent_list(struct inode *inode,
7119 struct ocfs2_dinode *di)
7121 ocfs2_zero_dinode_id2_with_xattr(inode, di);
7122 di->id2.i_list.l_tree_depth = 0;
7123 di->id2.i_list.l_next_free_rec = 0;
7124 di->id2.i_list.l_count = cpu_to_le16(
7125 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
7128 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7130 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7131 struct ocfs2_inline_data *idata = &di->id2.i_data;
7133 spin_lock(&oi->ip_lock);
7134 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7135 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7136 spin_unlock(&oi->ip_lock);
7139 * We clear the entire i_data structure here so that all
7140 * fields can be properly initialized.
7142 ocfs2_zero_dinode_id2_with_xattr(inode, di);
7144 idata->id_count = cpu_to_le16(
7145 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
7148 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7149 struct buffer_head *di_bh)
7151 int ret, i, has_data, num_pages = 0;
7153 u64 uninitialized_var(block);
7154 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7155 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7156 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7157 struct ocfs2_alloc_context *data_ac = NULL;
7158 struct page **pages = NULL;
7159 loff_t end = osb->s_clustersize;
7160 struct ocfs2_extent_tree et;
7163 has_data = i_size_read(inode) ? 1 : 0;
7166 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7167 sizeof(struct page *), GFP_NOFS);
7168 if (pages == NULL) {
7174 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7181 handle = ocfs2_start_trans(osb,
7182 ocfs2_inline_to_extents_credits(osb->sb));
7183 if (IS_ERR(handle)) {
7184 ret = PTR_ERR(handle);
7189 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7190 OCFS2_JOURNAL_ACCESS_WRITE);
7198 unsigned int page_end;
7201 if (vfs_dq_alloc_space_nodirty(inode,
7202 ocfs2_clusters_to_bytes(osb->sb, 1))) {
7208 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7216 * Save two copies, one for insert, and one that can
7217 * be changed by ocfs2_map_and_dirty_page() below.
7219 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7222 * Non sparse file systems zero on extend, so no need
7225 if (!ocfs2_sparse_alloc(osb) &&
7226 PAGE_CACHE_SIZE < osb->s_clustersize)
7227 end = PAGE_CACHE_SIZE;
7229 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7236 * This should populate the 1st page for us and mark
7239 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7245 page_end = PAGE_CACHE_SIZE;
7246 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7247 page_end = osb->s_clustersize;
7249 for (i = 0; i < num_pages; i++)
7250 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7251 pages[i], i > 0, &phys);
7254 spin_lock(&oi->ip_lock);
7255 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7256 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7257 spin_unlock(&oi->ip_lock);
7259 ocfs2_dinode_new_extent_list(inode, di);
7261 ocfs2_journal_dirty(handle, di_bh);
7265 * An error at this point should be extremely rare. If
7266 * this proves to be false, we could always re-build
7267 * the in-inode data from our pages.
7269 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
7270 ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
7276 inode->i_blocks = ocfs2_inode_sector_count(inode);
7280 if (ret < 0 && did_quota)
7281 vfs_dq_free_space_nodirty(inode,
7282 ocfs2_clusters_to_bytes(osb->sb, 1));
7284 ocfs2_commit_trans(osb, handle);
7288 ocfs2_free_alloc_context(data_ac);
7292 ocfs2_unlock_and_free_pages(pages, num_pages);
7300 * It is expected, that by the time you call this function,
7301 * inode->i_size and fe->i_size have been adjusted.
7303 * WARNING: This will kfree the truncate context
7305 int ocfs2_commit_truncate(struct ocfs2_super *osb,
7306 struct inode *inode,
7307 struct buffer_head *fe_bh,
7308 struct ocfs2_truncate_context *tc)
7310 int status, i, credits, tl_sem = 0;
7311 u32 clusters_to_del, new_highest_cpos, range;
7312 struct ocfs2_extent_list *el;
7313 handle_t *handle = NULL;
7314 struct inode *tl_inode = osb->osb_tl_inode;
7315 struct ocfs2_path *path = NULL;
7316 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
7320 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
7321 i_size_read(inode));
7323 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7324 ocfs2_journal_access_di);
7331 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7335 * Check that we still have allocation to delete.
7337 if (OCFS2_I(inode)->ip_clusters == 0) {
7343 * Truncate always works against the rightmost tree branch.
7345 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
7351 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7352 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7355 * By now, el will point to the extent list on the bottom most
7356 * portion of this tree. Only the tail record is considered in
7359 * We handle the following cases, in order:
7360 * - empty extent: delete the remaining branch
7361 * - remove the entire record
7362 * - remove a partial record
7363 * - no record needs to be removed (truncate has completed)
7365 el = path_leaf_el(path);
7366 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7367 ocfs2_error(inode->i_sb,
7368 "Inode %llu has empty extent block at %llu\n",
7369 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7370 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7375 i = le16_to_cpu(el->l_next_free_rec) - 1;
7376 range = le32_to_cpu(el->l_recs[i].e_cpos) +
7377 ocfs2_rec_clusters(el, &el->l_recs[i]);
7378 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7379 clusters_to_del = 0;
7380 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
7381 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
7382 } else if (range > new_highest_cpos) {
7383 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
7384 le32_to_cpu(el->l_recs[i].e_cpos)) -
7391 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7392 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7394 mutex_lock(&tl_inode->i_mutex);
7396 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7397 * record is free for use. If there isn't any, we flush to get
7398 * an empty truncate log. */
7399 if (ocfs2_truncate_log_needs_flush(osb)) {
7400 status = __ocfs2_flush_truncate_log(osb);
7407 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
7408 (struct ocfs2_dinode *)fe_bh->b_data,
7410 handle = ocfs2_start_trans(osb, credits);
7411 if (IS_ERR(handle)) {
7412 status = PTR_ERR(handle);
7418 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7425 mutex_unlock(&tl_inode->i_mutex);
7428 ocfs2_commit_trans(osb, handle);
7431 ocfs2_reinit_path(path, 1);
7434 * The check above will catch the case where we've truncated
7435 * away all allocation.
7441 ocfs2_schedule_truncate_log_flush(osb, 1);
7444 mutex_unlock(&tl_inode->i_mutex);
7447 ocfs2_commit_trans(osb, handle);
7449 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7451 ocfs2_free_path(path);
7453 /* This will drop the ext_alloc cluster lock for us */
7454 ocfs2_free_truncate_context(tc);
7461 * Expects the inode to already be locked.
7463 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7464 struct inode *inode,
7465 struct buffer_head *fe_bh,
7466 struct ocfs2_truncate_context **tc)
7469 unsigned int new_i_clusters;
7470 struct ocfs2_dinode *fe;
7471 struct ocfs2_extent_block *eb;
7472 struct buffer_head *last_eb_bh = NULL;
7478 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7479 i_size_read(inode));
7480 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7482 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7483 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7484 (unsigned long long)le64_to_cpu(fe->i_size));
7486 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7492 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7494 if (fe->id2.i_list.l_tree_depth) {
7495 status = ocfs2_read_extent_block(INODE_CACHE(inode),
7496 le64_to_cpu(fe->i_last_eb_blk),
7502 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7505 (*tc)->tc_last_eb_bh = last_eb_bh;
7511 ocfs2_free_truncate_context(*tc);
7519 * 'start' is inclusive, 'end' is not.
7521 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7522 unsigned int start, unsigned int end, int trunc)
7525 unsigned int numbytes;
7527 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7528 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7529 struct ocfs2_inline_data *idata = &di->id2.i_data;
7531 if (end > i_size_read(inode))
7532 end = i_size_read(inode);
7534 BUG_ON(start >= end);
7536 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7537 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7538 !ocfs2_supports_inline_data(osb)) {
7539 ocfs2_error(inode->i_sb,
7540 "Inline data flags for inode %llu don't agree! "
7541 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7542 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7543 le16_to_cpu(di->i_dyn_features),
7544 OCFS2_I(inode)->ip_dyn_features,
7545 osb->s_feature_incompat);
7550 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7551 if (IS_ERR(handle)) {
7552 ret = PTR_ERR(handle);
7557 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7558 OCFS2_JOURNAL_ACCESS_WRITE);
7564 numbytes = end - start;
7565 memset(idata->id_data + start, 0, numbytes);
7568 * No need to worry about the data page here - it's been
7569 * truncated already and inline data doesn't need it for
7570 * pushing zero's to disk, so we'll let readpage pick it up
7574 i_size_write(inode, start);
7575 di->i_size = cpu_to_le64(start);
7578 inode->i_blocks = ocfs2_inode_sector_count(inode);
7579 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7581 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7582 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7584 ocfs2_journal_dirty(handle, di_bh);
7587 ocfs2_commit_trans(osb, handle);
7593 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7596 * The caller is responsible for completing deallocation
7597 * before freeing the context.
7599 if (tc->tc_dealloc.c_first_suballocator != NULL)
7601 "Truncate completion has non-empty dealloc context\n");
7603 brelse(tc->tc_last_eb_bh);