arm: ep93xx: Add basic interrupt info
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ocfs2 / alloc.c
blobe4984e259cb6b9eec82ed22ae376e51fad986029
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * alloc.c
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.
26 #include <linux/fs.h>
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>
36 #include "ocfs2.h"
38 #include "alloc.h"
39 #include "aops.h"
40 #include "blockcheck.h"
41 #include "dlmglue.h"
42 #include "extent_map.h"
43 #include "inode.h"
44 #include "journal.h"
45 #include "localalloc.h"
46 #include "suballoc.h"
47 #include "sysfile.h"
48 #include "file.h"
49 #include "super.h"
50 #include "uptodate.h"
51 #include "xattr.h"
52 #include "refcounttree.h"
54 #include "buffer_head_io.h"
56 enum ocfs2_contig_type {
57 CONTIG_NONE = 0,
58 CONTIG_LEFT,
59 CONTIG_RIGHT,
60 CONTIG_LEFTRIGHT,
63 static enum ocfs2_contig_type
64 ocfs2_extent_rec_contig(struct super_block *sb,
65 struct ocfs2_extent_rec *ext,
66 struct ocfs2_extent_rec *insert_rec);
68 * Operations for a specific extent tree type.
70 * To implement an on-disk btree (extent tree) type in ocfs2, add
71 * an ocfs2_extent_tree_operations structure and the matching
72 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
73 * for the allocation portion of the extent tree.
75 struct ocfs2_extent_tree_operations {
77 * last_eb_blk is the block number of the right most leaf extent
78 * block. Most on-disk structures containing an extent tree store
79 * this value for fast access. The ->eo_set_last_eb_blk() and
80 * ->eo_get_last_eb_blk() operations access this value. They are
81 * both required.
83 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
84 u64 blkno);
85 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
88 * The on-disk structure usually keeps track of how many total
89 * clusters are stored in this extent tree. This function updates
90 * that value. new_clusters is the delta, and must be
91 * added to the total. Required.
93 void (*eo_update_clusters)(struct ocfs2_extent_tree *et,
94 u32 new_clusters);
97 * If this extent tree is supported by an extent map, insert
98 * a record into the map.
100 void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et,
101 struct ocfs2_extent_rec *rec);
104 * If this extent tree is supported by an extent map, truncate the
105 * map to clusters,
107 void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et,
108 u32 clusters);
111 * If ->eo_insert_check() exists, it is called before rec is
112 * inserted into the extent tree. It is optional.
114 int (*eo_insert_check)(struct ocfs2_extent_tree *et,
115 struct ocfs2_extent_rec *rec);
116 int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
119 * --------------------------------------------------------------
120 * The remaining are internal to ocfs2_extent_tree and don't have
121 * accessor functions
125 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
126 * It is required.
128 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
131 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
132 * it exists. If it does not, et->et_max_leaf_clusters is set
133 * to 0 (unlimited). Optional.
135 void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
138 * ->eo_extent_contig test whether the 2 ocfs2_extent_rec
139 * are contiguous or not. Optional. Don't need to set it if use
140 * ocfs2_extent_rec as the tree leaf.
142 enum ocfs2_contig_type
143 (*eo_extent_contig)(struct ocfs2_extent_tree *et,
144 struct ocfs2_extent_rec *ext,
145 struct ocfs2_extent_rec *insert_rec);
150 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
151 * in the methods.
153 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
154 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
155 u64 blkno);
156 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
157 u32 clusters);
158 static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
159 struct ocfs2_extent_rec *rec);
160 static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
161 u32 clusters);
162 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
163 struct ocfs2_extent_rec *rec);
164 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
165 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
166 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
167 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
168 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
169 .eo_update_clusters = ocfs2_dinode_update_clusters,
170 .eo_extent_map_insert = ocfs2_dinode_extent_map_insert,
171 .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate,
172 .eo_insert_check = ocfs2_dinode_insert_check,
173 .eo_sanity_check = ocfs2_dinode_sanity_check,
174 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
177 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
178 u64 blkno)
180 struct ocfs2_dinode *di = et->et_object;
182 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
183 di->i_last_eb_blk = cpu_to_le64(blkno);
186 static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
188 struct ocfs2_dinode *di = et->et_object;
190 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
191 return le64_to_cpu(di->i_last_eb_blk);
194 static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
195 u32 clusters)
197 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
198 struct ocfs2_dinode *di = et->et_object;
200 le32_add_cpu(&di->i_clusters, clusters);
201 spin_lock(&oi->ip_lock);
202 oi->ip_clusters = le32_to_cpu(di->i_clusters);
203 spin_unlock(&oi->ip_lock);
206 static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
207 struct ocfs2_extent_rec *rec)
209 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
211 ocfs2_extent_map_insert_rec(inode, rec);
214 static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
215 u32 clusters)
217 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
219 ocfs2_extent_map_trunc(inode, clusters);
222 static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
223 struct ocfs2_extent_rec *rec)
225 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
226 struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
228 BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
229 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
230 (oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
231 "Device %s, asking for sparse allocation: inode %llu, "
232 "cpos %u, clusters %u\n",
233 osb->dev_str,
234 (unsigned long long)oi->ip_blkno,
235 rec->e_cpos, oi->ip_clusters);
237 return 0;
240 static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
242 struct ocfs2_dinode *di = et->et_object;
244 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
245 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
247 return 0;
250 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
252 struct ocfs2_dinode *di = et->et_object;
254 et->et_root_el = &di->id2.i_list;
258 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
260 struct ocfs2_xattr_value_buf *vb = et->et_object;
262 et->et_root_el = &vb->vb_xv->xr_list;
265 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
266 u64 blkno)
268 struct ocfs2_xattr_value_buf *vb = et->et_object;
270 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
273 static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
275 struct ocfs2_xattr_value_buf *vb = et->et_object;
277 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
280 static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
281 u32 clusters)
283 struct ocfs2_xattr_value_buf *vb = et->et_object;
285 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
288 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
289 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
290 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
291 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
292 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
295 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
297 struct ocfs2_xattr_block *xb = et->et_object;
299 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
302 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
304 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
305 et->et_max_leaf_clusters =
306 ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
309 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
310 u64 blkno)
312 struct ocfs2_xattr_block *xb = et->et_object;
313 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
315 xt->xt_last_eb_blk = cpu_to_le64(blkno);
318 static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
320 struct ocfs2_xattr_block *xb = et->et_object;
321 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
323 return le64_to_cpu(xt->xt_last_eb_blk);
326 static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
327 u32 clusters)
329 struct ocfs2_xattr_block *xb = et->et_object;
331 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
334 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
335 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
336 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
337 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
338 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
339 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
342 static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
343 u64 blkno)
345 struct ocfs2_dx_root_block *dx_root = et->et_object;
347 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
350 static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
352 struct ocfs2_dx_root_block *dx_root = et->et_object;
354 return le64_to_cpu(dx_root->dr_last_eb_blk);
357 static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
358 u32 clusters)
360 struct ocfs2_dx_root_block *dx_root = et->et_object;
362 le32_add_cpu(&dx_root->dr_clusters, clusters);
365 static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
367 struct ocfs2_dx_root_block *dx_root = et->et_object;
369 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
371 return 0;
374 static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
376 struct ocfs2_dx_root_block *dx_root = et->et_object;
378 et->et_root_el = &dx_root->dr_list;
381 static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
382 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
383 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
384 .eo_update_clusters = ocfs2_dx_root_update_clusters,
385 .eo_sanity_check = ocfs2_dx_root_sanity_check,
386 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
389 static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et)
391 struct ocfs2_refcount_block *rb = et->et_object;
393 et->et_root_el = &rb->rf_list;
396 static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
397 u64 blkno)
399 struct ocfs2_refcount_block *rb = et->et_object;
401 rb->rf_last_eb_blk = cpu_to_le64(blkno);
404 static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
406 struct ocfs2_refcount_block *rb = et->et_object;
408 return le64_to_cpu(rb->rf_last_eb_blk);
411 static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et,
412 u32 clusters)
414 struct ocfs2_refcount_block *rb = et->et_object;
416 le32_add_cpu(&rb->rf_clusters, clusters);
419 static enum ocfs2_contig_type
420 ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et,
421 struct ocfs2_extent_rec *ext,
422 struct ocfs2_extent_rec *insert_rec)
424 return CONTIG_NONE;
427 static struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = {
428 .eo_set_last_eb_blk = ocfs2_refcount_tree_set_last_eb_blk,
429 .eo_get_last_eb_blk = ocfs2_refcount_tree_get_last_eb_blk,
430 .eo_update_clusters = ocfs2_refcount_tree_update_clusters,
431 .eo_fill_root_el = ocfs2_refcount_tree_fill_root_el,
432 .eo_extent_contig = ocfs2_refcount_tree_extent_contig,
435 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
436 struct ocfs2_caching_info *ci,
437 struct buffer_head *bh,
438 ocfs2_journal_access_func access,
439 void *obj,
440 struct ocfs2_extent_tree_operations *ops)
442 et->et_ops = ops;
443 et->et_root_bh = bh;
444 et->et_ci = ci;
445 et->et_root_journal_access = access;
446 if (!obj)
447 obj = (void *)bh->b_data;
448 et->et_object = obj;
450 et->et_ops->eo_fill_root_el(et);
451 if (!et->et_ops->eo_fill_max_leaf_clusters)
452 et->et_max_leaf_clusters = 0;
453 else
454 et->et_ops->eo_fill_max_leaf_clusters(et);
457 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
458 struct ocfs2_caching_info *ci,
459 struct buffer_head *bh)
461 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di,
462 NULL, &ocfs2_dinode_et_ops);
465 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
466 struct ocfs2_caching_info *ci,
467 struct buffer_head *bh)
469 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb,
470 NULL, &ocfs2_xattr_tree_et_ops);
473 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
474 struct ocfs2_caching_info *ci,
475 struct ocfs2_xattr_value_buf *vb)
477 __ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb,
478 &ocfs2_xattr_value_et_ops);
481 void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
482 struct ocfs2_caching_info *ci,
483 struct buffer_head *bh)
485 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr,
486 NULL, &ocfs2_dx_root_et_ops);
489 void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
490 struct ocfs2_caching_info *ci,
491 struct buffer_head *bh)
493 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb,
494 NULL, &ocfs2_refcount_tree_et_ops);
497 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
498 u64 new_last_eb_blk)
500 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
503 static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
505 return et->et_ops->eo_get_last_eb_blk(et);
508 static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
509 u32 clusters)
511 et->et_ops->eo_update_clusters(et, clusters);
514 static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et,
515 struct ocfs2_extent_rec *rec)
517 if (et->et_ops->eo_extent_map_insert)
518 et->et_ops->eo_extent_map_insert(et, rec);
521 static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et,
522 u32 clusters)
524 if (et->et_ops->eo_extent_map_truncate)
525 et->et_ops->eo_extent_map_truncate(et, clusters);
528 static inline int ocfs2_et_root_journal_access(handle_t *handle,
529 struct ocfs2_extent_tree *et,
530 int type)
532 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
533 type);
536 static inline enum ocfs2_contig_type
537 ocfs2_et_extent_contig(struct ocfs2_extent_tree *et,
538 struct ocfs2_extent_rec *rec,
539 struct ocfs2_extent_rec *insert_rec)
541 if (et->et_ops->eo_extent_contig)
542 return et->et_ops->eo_extent_contig(et, rec, insert_rec);
544 return ocfs2_extent_rec_contig(
545 ocfs2_metadata_cache_get_super(et->et_ci),
546 rec, insert_rec);
549 static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
550 struct ocfs2_extent_rec *rec)
552 int ret = 0;
554 if (et->et_ops->eo_insert_check)
555 ret = et->et_ops->eo_insert_check(et, rec);
556 return ret;
559 static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
561 int ret = 0;
563 if (et->et_ops->eo_sanity_check)
564 ret = et->et_ops->eo_sanity_check(et);
565 return ret;
568 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
569 struct ocfs2_extent_block *eb);
570 static void ocfs2_adjust_rightmost_records(handle_t *handle,
571 struct ocfs2_extent_tree *et,
572 struct ocfs2_path *path,
573 struct ocfs2_extent_rec *insert_rec);
575 * Reset the actual path elements so that we can re-use the structure
576 * to build another path. Generally, this involves freeing the buffer
577 * heads.
579 void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
581 int i, start = 0, depth = 0;
582 struct ocfs2_path_item *node;
584 if (keep_root)
585 start = 1;
587 for(i = start; i < path_num_items(path); i++) {
588 node = &path->p_node[i];
590 brelse(node->bh);
591 node->bh = NULL;
592 node->el = NULL;
596 * Tree depth may change during truncate, or insert. If we're
597 * keeping the root extent list, then make sure that our path
598 * structure reflects the proper depth.
600 if (keep_root)
601 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
602 else
603 path_root_access(path) = NULL;
605 path->p_tree_depth = depth;
608 void ocfs2_free_path(struct ocfs2_path *path)
610 if (path) {
611 ocfs2_reinit_path(path, 0);
612 kfree(path);
617 * All the elements of src into dest. After this call, src could be freed
618 * without affecting dest.
620 * Both paths should have the same root. Any non-root elements of dest
621 * will be freed.
623 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
625 int i;
627 BUG_ON(path_root_bh(dest) != path_root_bh(src));
628 BUG_ON(path_root_el(dest) != path_root_el(src));
629 BUG_ON(path_root_access(dest) != path_root_access(src));
631 ocfs2_reinit_path(dest, 1);
633 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
634 dest->p_node[i].bh = src->p_node[i].bh;
635 dest->p_node[i].el = src->p_node[i].el;
637 if (dest->p_node[i].bh)
638 get_bh(dest->p_node[i].bh);
643 * Make the *dest path the same as src and re-initialize src path to
644 * have a root only.
646 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
648 int i;
650 BUG_ON(path_root_bh(dest) != path_root_bh(src));
651 BUG_ON(path_root_access(dest) != path_root_access(src));
653 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
654 brelse(dest->p_node[i].bh);
656 dest->p_node[i].bh = src->p_node[i].bh;
657 dest->p_node[i].el = src->p_node[i].el;
659 src->p_node[i].bh = NULL;
660 src->p_node[i].el = NULL;
665 * Insert an extent block at given index.
667 * This will not take an additional reference on eb_bh.
669 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
670 struct buffer_head *eb_bh)
672 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
675 * Right now, no root bh is an extent block, so this helps
676 * catch code errors with dinode trees. The assertion can be
677 * safely removed if we ever need to insert extent block
678 * structures at the root.
680 BUG_ON(index == 0);
682 path->p_node[index].bh = eb_bh;
683 path->p_node[index].el = &eb->h_list;
686 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
687 struct ocfs2_extent_list *root_el,
688 ocfs2_journal_access_func access)
690 struct ocfs2_path *path;
692 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
694 path = kzalloc(sizeof(*path), GFP_NOFS);
695 if (path) {
696 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
697 get_bh(root_bh);
698 path_root_bh(path) = root_bh;
699 path_root_el(path) = root_el;
700 path_root_access(path) = access;
703 return path;
706 struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
708 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
709 path_root_access(path));
712 struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
714 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
715 et->et_root_journal_access);
719 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
720 * otherwise it's the root_access function.
722 * I don't like the way this function's name looks next to
723 * ocfs2_journal_access_path(), but I don't have a better one.
725 int ocfs2_path_bh_journal_access(handle_t *handle,
726 struct ocfs2_caching_info *ci,
727 struct ocfs2_path *path,
728 int idx)
730 ocfs2_journal_access_func access = path_root_access(path);
732 if (!access)
733 access = ocfs2_journal_access;
735 if (idx)
736 access = ocfs2_journal_access_eb;
738 return access(handle, ci, path->p_node[idx].bh,
739 OCFS2_JOURNAL_ACCESS_WRITE);
743 * Convenience function to journal all components in a path.
745 int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
746 handle_t *handle,
747 struct ocfs2_path *path)
749 int i, ret = 0;
751 if (!path)
752 goto out;
754 for(i = 0; i < path_num_items(path); i++) {
755 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
756 if (ret < 0) {
757 mlog_errno(ret);
758 goto out;
762 out:
763 return ret;
767 * Return the index of the extent record which contains cluster #v_cluster.
768 * -1 is returned if it was not found.
770 * Should work fine on interior and exterior nodes.
772 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
774 int ret = -1;
775 int i;
776 struct ocfs2_extent_rec *rec;
777 u32 rec_end, rec_start, clusters;
779 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
780 rec = &el->l_recs[i];
782 rec_start = le32_to_cpu(rec->e_cpos);
783 clusters = ocfs2_rec_clusters(el, rec);
785 rec_end = rec_start + clusters;
787 if (v_cluster >= rec_start && v_cluster < rec_end) {
788 ret = i;
789 break;
793 return ret;
797 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
798 * ocfs2_extent_rec_contig only work properly against leaf nodes!
800 static int ocfs2_block_extent_contig(struct super_block *sb,
801 struct ocfs2_extent_rec *ext,
802 u64 blkno)
804 u64 blk_end = le64_to_cpu(ext->e_blkno);
806 blk_end += ocfs2_clusters_to_blocks(sb,
807 le16_to_cpu(ext->e_leaf_clusters));
809 return blkno == blk_end;
812 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
813 struct ocfs2_extent_rec *right)
815 u32 left_range;
817 left_range = le32_to_cpu(left->e_cpos) +
818 le16_to_cpu(left->e_leaf_clusters);
820 return (left_range == le32_to_cpu(right->e_cpos));
823 static enum ocfs2_contig_type
824 ocfs2_extent_rec_contig(struct super_block *sb,
825 struct ocfs2_extent_rec *ext,
826 struct ocfs2_extent_rec *insert_rec)
828 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
831 * Refuse to coalesce extent records with different flag
832 * fields - we don't want to mix unwritten extents with user
833 * data.
835 if (ext->e_flags != insert_rec->e_flags)
836 return CONTIG_NONE;
838 if (ocfs2_extents_adjacent(ext, insert_rec) &&
839 ocfs2_block_extent_contig(sb, ext, blkno))
840 return CONTIG_RIGHT;
842 blkno = le64_to_cpu(ext->e_blkno);
843 if (ocfs2_extents_adjacent(insert_rec, ext) &&
844 ocfs2_block_extent_contig(sb, insert_rec, blkno))
845 return CONTIG_LEFT;
847 return CONTIG_NONE;
851 * NOTE: We can have pretty much any combination of contiguousness and
852 * appending.
854 * The usefulness of APPEND_TAIL is more in that it lets us know that
855 * we'll have to update the path to that leaf.
857 enum ocfs2_append_type {
858 APPEND_NONE = 0,
859 APPEND_TAIL,
862 enum ocfs2_split_type {
863 SPLIT_NONE = 0,
864 SPLIT_LEFT,
865 SPLIT_RIGHT,
868 struct ocfs2_insert_type {
869 enum ocfs2_split_type ins_split;
870 enum ocfs2_append_type ins_appending;
871 enum ocfs2_contig_type ins_contig;
872 int ins_contig_index;
873 int ins_tree_depth;
876 struct ocfs2_merge_ctxt {
877 enum ocfs2_contig_type c_contig_type;
878 int c_has_empty_extent;
879 int c_split_covers_rec;
882 static int ocfs2_validate_extent_block(struct super_block *sb,
883 struct buffer_head *bh)
885 int rc;
886 struct ocfs2_extent_block *eb =
887 (struct ocfs2_extent_block *)bh->b_data;
889 mlog(0, "Validating extent block %llu\n",
890 (unsigned long long)bh->b_blocknr);
892 BUG_ON(!buffer_uptodate(bh));
895 * If the ecc fails, we return the error but otherwise
896 * leave the filesystem running. We know any error is
897 * local to this block.
899 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
900 if (rc) {
901 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
902 (unsigned long long)bh->b_blocknr);
903 return rc;
907 * Errors after here are fatal.
910 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
911 ocfs2_error(sb,
912 "Extent block #%llu has bad signature %.*s",
913 (unsigned long long)bh->b_blocknr, 7,
914 eb->h_signature);
915 return -EINVAL;
918 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
919 ocfs2_error(sb,
920 "Extent block #%llu has an invalid h_blkno "
921 "of %llu",
922 (unsigned long long)bh->b_blocknr,
923 (unsigned long long)le64_to_cpu(eb->h_blkno));
924 return -EINVAL;
927 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
928 ocfs2_error(sb,
929 "Extent block #%llu has an invalid "
930 "h_fs_generation of #%u",
931 (unsigned long long)bh->b_blocknr,
932 le32_to_cpu(eb->h_fs_generation));
933 return -EINVAL;
936 return 0;
939 int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
940 struct buffer_head **bh)
942 int rc;
943 struct buffer_head *tmp = *bh;
945 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
946 ocfs2_validate_extent_block);
948 /* If ocfs2_read_block() got us a new bh, pass it up. */
949 if (!rc && !*bh)
950 *bh = tmp;
952 return rc;
957 * How many free extents have we got before we need more meta data?
959 int ocfs2_num_free_extents(struct ocfs2_super *osb,
960 struct ocfs2_extent_tree *et)
962 int retval;
963 struct ocfs2_extent_list *el = NULL;
964 struct ocfs2_extent_block *eb;
965 struct buffer_head *eb_bh = NULL;
966 u64 last_eb_blk = 0;
968 mlog_entry_void();
970 el = et->et_root_el;
971 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
973 if (last_eb_blk) {
974 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
975 &eb_bh);
976 if (retval < 0) {
977 mlog_errno(retval);
978 goto bail;
980 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
981 el = &eb->h_list;
984 BUG_ON(el->l_tree_depth != 0);
986 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
987 bail:
988 brelse(eb_bh);
990 mlog_exit(retval);
991 return retval;
994 /* expects array to already be allocated
996 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
997 * l_count for you
999 static int ocfs2_create_new_meta_bhs(handle_t *handle,
1000 struct ocfs2_extent_tree *et,
1001 int wanted,
1002 struct ocfs2_alloc_context *meta_ac,
1003 struct buffer_head *bhs[])
1005 int count, status, i;
1006 u16 suballoc_bit_start;
1007 u32 num_got;
1008 u64 suballoc_loc, first_blkno;
1009 struct ocfs2_super *osb =
1010 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
1011 struct ocfs2_extent_block *eb;
1013 mlog_entry_void();
1015 count = 0;
1016 while (count < wanted) {
1017 status = ocfs2_claim_metadata(handle,
1018 meta_ac,
1019 wanted - count,
1020 &suballoc_loc,
1021 &suballoc_bit_start,
1022 &num_got,
1023 &first_blkno);
1024 if (status < 0) {
1025 mlog_errno(status);
1026 goto bail;
1029 for(i = count; i < (num_got + count); i++) {
1030 bhs[i] = sb_getblk(osb->sb, first_blkno);
1031 if (bhs[i] == NULL) {
1032 status = -EIO;
1033 mlog_errno(status);
1034 goto bail;
1036 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
1038 status = ocfs2_journal_access_eb(handle, et->et_ci,
1039 bhs[i],
1040 OCFS2_JOURNAL_ACCESS_CREATE);
1041 if (status < 0) {
1042 mlog_errno(status);
1043 goto bail;
1046 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
1047 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
1048 /* Ok, setup the minimal stuff here. */
1049 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
1050 eb->h_blkno = cpu_to_le64(first_blkno);
1051 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
1052 eb->h_suballoc_slot =
1053 cpu_to_le16(meta_ac->ac_alloc_slot);
1054 eb->h_suballoc_loc = cpu_to_le64(suballoc_loc);
1055 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1056 eb->h_list.l_count =
1057 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1059 suballoc_bit_start++;
1060 first_blkno++;
1062 /* We'll also be dirtied by the caller, so
1063 * this isn't absolutely necessary. */
1064 ocfs2_journal_dirty(handle, bhs[i]);
1067 count += num_got;
1070 status = 0;
1071 bail:
1072 if (status < 0) {
1073 for(i = 0; i < wanted; i++) {
1074 brelse(bhs[i]);
1075 bhs[i] = NULL;
1078 mlog_exit(status);
1079 return status;
1083 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1085 * Returns the sum of the rightmost extent rec logical offset and
1086 * cluster count.
1088 * ocfs2_add_branch() uses this to determine what logical cluster
1089 * value should be populated into the leftmost new branch records.
1091 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1092 * value for the new topmost tree record.
1094 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1096 int i;
1098 i = le16_to_cpu(el->l_next_free_rec) - 1;
1100 return le32_to_cpu(el->l_recs[i].e_cpos) +
1101 ocfs2_rec_clusters(el, &el->l_recs[i]);
1105 * Change range of the branches in the right most path according to the leaf
1106 * extent block's rightmost record.
1108 static int ocfs2_adjust_rightmost_branch(handle_t *handle,
1109 struct ocfs2_extent_tree *et)
1111 int status;
1112 struct ocfs2_path *path = NULL;
1113 struct ocfs2_extent_list *el;
1114 struct ocfs2_extent_rec *rec;
1116 path = ocfs2_new_path_from_et(et);
1117 if (!path) {
1118 status = -ENOMEM;
1119 return status;
1122 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
1123 if (status < 0) {
1124 mlog_errno(status);
1125 goto out;
1128 status = ocfs2_extend_trans(handle, path_num_items(path));
1129 if (status < 0) {
1130 mlog_errno(status);
1131 goto out;
1134 status = ocfs2_journal_access_path(et->et_ci, handle, path);
1135 if (status < 0) {
1136 mlog_errno(status);
1137 goto out;
1140 el = path_leaf_el(path);
1141 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1143 ocfs2_adjust_rightmost_records(handle, et, path, rec);
1145 out:
1146 ocfs2_free_path(path);
1147 return status;
1151 * Add an entire tree branch to our inode. eb_bh is the extent block
1152 * to start at, if we don't want to start the branch at the root
1153 * structure.
1155 * last_eb_bh is required as we have to update it's next_leaf pointer
1156 * for the new last extent block.
1158 * the new branch will be 'empty' in the sense that every block will
1159 * contain a single record with cluster count == 0.
1161 static int ocfs2_add_branch(handle_t *handle,
1162 struct ocfs2_extent_tree *et,
1163 struct buffer_head *eb_bh,
1164 struct buffer_head **last_eb_bh,
1165 struct ocfs2_alloc_context *meta_ac)
1167 int status, new_blocks, i;
1168 u64 next_blkno, new_last_eb_blk;
1169 struct buffer_head *bh;
1170 struct buffer_head **new_eb_bhs = NULL;
1171 struct ocfs2_extent_block *eb;
1172 struct ocfs2_extent_list *eb_el;
1173 struct ocfs2_extent_list *el;
1174 u32 new_cpos, root_end;
1176 mlog_entry_void();
1178 BUG_ON(!last_eb_bh || !*last_eb_bh);
1180 if (eb_bh) {
1181 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1182 el = &eb->h_list;
1183 } else
1184 el = et->et_root_el;
1186 /* we never add a branch to a leaf. */
1187 BUG_ON(!el->l_tree_depth);
1189 new_blocks = le16_to_cpu(el->l_tree_depth);
1191 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1192 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1193 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1196 * If there is a gap before the root end and the real end
1197 * of the righmost leaf block, we need to remove the gap
1198 * between new_cpos and root_end first so that the tree
1199 * is consistent after we add a new branch(it will start
1200 * from new_cpos).
1202 if (root_end > new_cpos) {
1203 mlog(0, "adjust the cluster end from %u to %u\n",
1204 root_end, new_cpos);
1205 status = ocfs2_adjust_rightmost_branch(handle, et);
1206 if (status) {
1207 mlog_errno(status);
1208 goto bail;
1212 /* allocate the number of new eb blocks we need */
1213 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1214 GFP_KERNEL);
1215 if (!new_eb_bhs) {
1216 status = -ENOMEM;
1217 mlog_errno(status);
1218 goto bail;
1221 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
1222 meta_ac, new_eb_bhs);
1223 if (status < 0) {
1224 mlog_errno(status);
1225 goto bail;
1228 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1229 * linked with the rest of the tree.
1230 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1232 * when we leave the loop, new_last_eb_blk will point to the
1233 * newest leaf, and next_blkno will point to the topmost extent
1234 * block. */
1235 next_blkno = new_last_eb_blk = 0;
1236 for(i = 0; i < new_blocks; i++) {
1237 bh = new_eb_bhs[i];
1238 eb = (struct ocfs2_extent_block *) bh->b_data;
1239 /* ocfs2_create_new_meta_bhs() should create it right! */
1240 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1241 eb_el = &eb->h_list;
1243 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
1244 OCFS2_JOURNAL_ACCESS_CREATE);
1245 if (status < 0) {
1246 mlog_errno(status);
1247 goto bail;
1250 eb->h_next_leaf_blk = 0;
1251 eb_el->l_tree_depth = cpu_to_le16(i);
1252 eb_el->l_next_free_rec = cpu_to_le16(1);
1254 * This actually counts as an empty extent as
1255 * c_clusters == 0
1257 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
1258 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
1260 * eb_el isn't always an interior node, but even leaf
1261 * nodes want a zero'd flags and reserved field so
1262 * this gets the whole 32 bits regardless of use.
1264 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
1265 if (!eb_el->l_tree_depth)
1266 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1268 ocfs2_journal_dirty(handle, bh);
1269 next_blkno = le64_to_cpu(eb->h_blkno);
1272 /* This is a bit hairy. We want to update up to three blocks
1273 * here without leaving any of them in an inconsistent state
1274 * in case of error. We don't have to worry about
1275 * journal_dirty erroring as it won't unless we've aborted the
1276 * handle (in which case we would never be here) so reserving
1277 * the write with journal_access is all we need to do. */
1278 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
1279 OCFS2_JOURNAL_ACCESS_WRITE);
1280 if (status < 0) {
1281 mlog_errno(status);
1282 goto bail;
1284 status = ocfs2_et_root_journal_access(handle, et,
1285 OCFS2_JOURNAL_ACCESS_WRITE);
1286 if (status < 0) {
1287 mlog_errno(status);
1288 goto bail;
1290 if (eb_bh) {
1291 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
1292 OCFS2_JOURNAL_ACCESS_WRITE);
1293 if (status < 0) {
1294 mlog_errno(status);
1295 goto bail;
1299 /* Link the new branch into the rest of the tree (el will
1300 * either be on the root_bh, or the extent block passed in. */
1301 i = le16_to_cpu(el->l_next_free_rec);
1302 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
1303 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1304 el->l_recs[i].e_int_clusters = 0;
1305 le16_add_cpu(&el->l_next_free_rec, 1);
1307 /* fe needs a new last extent block pointer, as does the
1308 * next_leaf on the previously last-extent-block. */
1309 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
1311 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
1312 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1314 ocfs2_journal_dirty(handle, *last_eb_bh);
1315 ocfs2_journal_dirty(handle, et->et_root_bh);
1316 if (eb_bh)
1317 ocfs2_journal_dirty(handle, eb_bh);
1320 * Some callers want to track the rightmost leaf so pass it
1321 * back here.
1323 brelse(*last_eb_bh);
1324 get_bh(new_eb_bhs[0]);
1325 *last_eb_bh = new_eb_bhs[0];
1327 status = 0;
1328 bail:
1329 if (new_eb_bhs) {
1330 for (i = 0; i < new_blocks; i++)
1331 brelse(new_eb_bhs[i]);
1332 kfree(new_eb_bhs);
1335 mlog_exit(status);
1336 return status;
1340 * adds another level to the allocation tree.
1341 * returns back the new extent block so you can add a branch to it
1342 * after this call.
1344 static int ocfs2_shift_tree_depth(handle_t *handle,
1345 struct ocfs2_extent_tree *et,
1346 struct ocfs2_alloc_context *meta_ac,
1347 struct buffer_head **ret_new_eb_bh)
1349 int status, i;
1350 u32 new_clusters;
1351 struct buffer_head *new_eb_bh = NULL;
1352 struct ocfs2_extent_block *eb;
1353 struct ocfs2_extent_list *root_el;
1354 struct ocfs2_extent_list *eb_el;
1356 mlog_entry_void();
1358 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
1359 &new_eb_bh);
1360 if (status < 0) {
1361 mlog_errno(status);
1362 goto bail;
1365 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1366 /* ocfs2_create_new_meta_bhs() should create it right! */
1367 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1369 eb_el = &eb->h_list;
1370 root_el = et->et_root_el;
1372 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
1373 OCFS2_JOURNAL_ACCESS_CREATE);
1374 if (status < 0) {
1375 mlog_errno(status);
1376 goto bail;
1379 /* copy the root extent list data into the new extent block */
1380 eb_el->l_tree_depth = root_el->l_tree_depth;
1381 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1382 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1383 eb_el->l_recs[i] = root_el->l_recs[i];
1385 ocfs2_journal_dirty(handle, new_eb_bh);
1387 status = ocfs2_et_root_journal_access(handle, et,
1388 OCFS2_JOURNAL_ACCESS_WRITE);
1389 if (status < 0) {
1390 mlog_errno(status);
1391 goto bail;
1394 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1396 /* update root_bh now */
1397 le16_add_cpu(&root_el->l_tree_depth, 1);
1398 root_el->l_recs[0].e_cpos = 0;
1399 root_el->l_recs[0].e_blkno = eb->h_blkno;
1400 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1401 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1402 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1403 root_el->l_next_free_rec = cpu_to_le16(1);
1405 /* If this is our 1st tree depth shift, then last_eb_blk
1406 * becomes the allocated extent block */
1407 if (root_el->l_tree_depth == cpu_to_le16(1))
1408 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1410 ocfs2_journal_dirty(handle, et->et_root_bh);
1412 *ret_new_eb_bh = new_eb_bh;
1413 new_eb_bh = NULL;
1414 status = 0;
1415 bail:
1416 brelse(new_eb_bh);
1418 mlog_exit(status);
1419 return status;
1423 * Should only be called when there is no space left in any of the
1424 * leaf nodes. What we want to do is find the lowest tree depth
1425 * non-leaf extent block with room for new records. There are three
1426 * valid results of this search:
1428 * 1) a lowest extent block is found, then we pass it back in
1429 * *lowest_eb_bh and return '0'
1431 * 2) the search fails to find anything, but the root_el has room. We
1432 * pass NULL back in *lowest_eb_bh, but still return '0'
1434 * 3) the search fails to find anything AND the root_el is full, in
1435 * which case we return > 0
1437 * return status < 0 indicates an error.
1439 static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
1440 struct buffer_head **target_bh)
1442 int status = 0, i;
1443 u64 blkno;
1444 struct ocfs2_extent_block *eb;
1445 struct ocfs2_extent_list *el;
1446 struct buffer_head *bh = NULL;
1447 struct buffer_head *lowest_bh = NULL;
1449 mlog_entry_void();
1451 *target_bh = NULL;
1453 el = et->et_root_el;
1455 while(le16_to_cpu(el->l_tree_depth) > 1) {
1456 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1457 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1458 "Owner %llu has empty "
1459 "extent list (next_free_rec == 0)",
1460 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
1461 status = -EIO;
1462 goto bail;
1464 i = le16_to_cpu(el->l_next_free_rec) - 1;
1465 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1466 if (!blkno) {
1467 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1468 "Owner %llu has extent "
1469 "list where extent # %d has no physical "
1470 "block start",
1471 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
1472 status = -EIO;
1473 goto bail;
1476 brelse(bh);
1477 bh = NULL;
1479 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
1480 if (status < 0) {
1481 mlog_errno(status);
1482 goto bail;
1485 eb = (struct ocfs2_extent_block *) bh->b_data;
1486 el = &eb->h_list;
1488 if (le16_to_cpu(el->l_next_free_rec) <
1489 le16_to_cpu(el->l_count)) {
1490 brelse(lowest_bh);
1491 lowest_bh = bh;
1492 get_bh(lowest_bh);
1496 /* If we didn't find one and the fe doesn't have any room,
1497 * then return '1' */
1498 el = et->et_root_el;
1499 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1500 status = 1;
1502 *target_bh = lowest_bh;
1503 bail:
1504 brelse(bh);
1506 mlog_exit(status);
1507 return status;
1511 * Grow a b-tree so that it has more records.
1513 * We might shift the tree depth in which case existing paths should
1514 * be considered invalid.
1516 * Tree depth after the grow is returned via *final_depth.
1518 * *last_eb_bh will be updated by ocfs2_add_branch().
1520 static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1521 int *final_depth, struct buffer_head **last_eb_bh,
1522 struct ocfs2_alloc_context *meta_ac)
1524 int ret, shift;
1525 struct ocfs2_extent_list *el = et->et_root_el;
1526 int depth = le16_to_cpu(el->l_tree_depth);
1527 struct buffer_head *bh = NULL;
1529 BUG_ON(meta_ac == NULL);
1531 shift = ocfs2_find_branch_target(et, &bh);
1532 if (shift < 0) {
1533 ret = shift;
1534 mlog_errno(ret);
1535 goto out;
1538 /* We traveled all the way to the bottom of the allocation tree
1539 * and didn't find room for any more extents - we need to add
1540 * another tree level */
1541 if (shift) {
1542 BUG_ON(bh);
1543 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1545 /* ocfs2_shift_tree_depth will return us a buffer with
1546 * the new extent block (so we can pass that to
1547 * ocfs2_add_branch). */
1548 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
1549 if (ret < 0) {
1550 mlog_errno(ret);
1551 goto out;
1553 depth++;
1554 if (depth == 1) {
1556 * Special case: we have room now if we shifted from
1557 * tree_depth 0, so no more work needs to be done.
1559 * We won't be calling add_branch, so pass
1560 * back *last_eb_bh as the new leaf. At depth
1561 * zero, it should always be null so there's
1562 * no reason to brelse.
1564 BUG_ON(*last_eb_bh);
1565 get_bh(bh);
1566 *last_eb_bh = bh;
1567 goto out;
1571 /* call ocfs2_add_branch to add the final part of the tree with
1572 * the new data. */
1573 mlog(0, "add branch. bh = %p\n", bh);
1574 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
1575 meta_ac);
1576 if (ret < 0) {
1577 mlog_errno(ret);
1578 goto out;
1581 out:
1582 if (final_depth)
1583 *final_depth = depth;
1584 brelse(bh);
1585 return ret;
1589 * This function will discard the rightmost extent record.
1591 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1593 int next_free = le16_to_cpu(el->l_next_free_rec);
1594 int count = le16_to_cpu(el->l_count);
1595 unsigned int num_bytes;
1597 BUG_ON(!next_free);
1598 /* This will cause us to go off the end of our extent list. */
1599 BUG_ON(next_free >= count);
1601 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1603 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1606 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1607 struct ocfs2_extent_rec *insert_rec)
1609 int i, insert_index, next_free, has_empty, num_bytes;
1610 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1611 struct ocfs2_extent_rec *rec;
1613 next_free = le16_to_cpu(el->l_next_free_rec);
1614 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1616 BUG_ON(!next_free);
1618 /* The tree code before us didn't allow enough room in the leaf. */
1619 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1622 * The easiest way to approach this is to just remove the
1623 * empty extent and temporarily decrement next_free.
1625 if (has_empty) {
1627 * If next_free was 1 (only an empty extent), this
1628 * loop won't execute, which is fine. We still want
1629 * the decrement above to happen.
1631 for(i = 0; i < (next_free - 1); i++)
1632 el->l_recs[i] = el->l_recs[i+1];
1634 next_free--;
1638 * Figure out what the new record index should be.
1640 for(i = 0; i < next_free; i++) {
1641 rec = &el->l_recs[i];
1643 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1644 break;
1646 insert_index = i;
1648 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1649 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1651 BUG_ON(insert_index < 0);
1652 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1653 BUG_ON(insert_index > next_free);
1656 * No need to memmove if we're just adding to the tail.
1658 if (insert_index != next_free) {
1659 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1661 num_bytes = next_free - insert_index;
1662 num_bytes *= sizeof(struct ocfs2_extent_rec);
1663 memmove(&el->l_recs[insert_index + 1],
1664 &el->l_recs[insert_index],
1665 num_bytes);
1669 * Either we had an empty extent, and need to re-increment or
1670 * there was no empty extent on a non full rightmost leaf node,
1671 * in which case we still need to increment.
1673 next_free++;
1674 el->l_next_free_rec = cpu_to_le16(next_free);
1676 * Make sure none of the math above just messed up our tree.
1678 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1680 el->l_recs[insert_index] = *insert_rec;
1684 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1686 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1688 BUG_ON(num_recs == 0);
1690 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1691 num_recs--;
1692 size = num_recs * sizeof(struct ocfs2_extent_rec);
1693 memmove(&el->l_recs[0], &el->l_recs[1], size);
1694 memset(&el->l_recs[num_recs], 0,
1695 sizeof(struct ocfs2_extent_rec));
1696 el->l_next_free_rec = cpu_to_le16(num_recs);
1701 * Create an empty extent record .
1703 * l_next_free_rec may be updated.
1705 * If an empty extent already exists do nothing.
1707 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1709 int next_free = le16_to_cpu(el->l_next_free_rec);
1711 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1713 if (next_free == 0)
1714 goto set_and_inc;
1716 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1717 return;
1719 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1720 "Asked to create an empty extent in a full list:\n"
1721 "count = %u, tree depth = %u",
1722 le16_to_cpu(el->l_count),
1723 le16_to_cpu(el->l_tree_depth));
1725 ocfs2_shift_records_right(el);
1727 set_and_inc:
1728 le16_add_cpu(&el->l_next_free_rec, 1);
1729 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1733 * For a rotation which involves two leaf nodes, the "root node" is
1734 * the lowest level tree node which contains a path to both leafs. This
1735 * resulting set of information can be used to form a complete "subtree"
1737 * This function is passed two full paths from the dinode down to a
1738 * pair of adjacent leaves. It's task is to figure out which path
1739 * index contains the subtree root - this can be the root index itself
1740 * in a worst-case rotation.
1742 * The array index of the subtree root is passed back.
1744 int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1745 struct ocfs2_path *left,
1746 struct ocfs2_path *right)
1748 int i = 0;
1751 * Check that the caller passed in two paths from the same tree.
1753 BUG_ON(path_root_bh(left) != path_root_bh(right));
1755 do {
1756 i++;
1759 * The caller didn't pass two adjacent paths.
1761 mlog_bug_on_msg(i > left->p_tree_depth,
1762 "Owner %llu, left depth %u, right depth %u\n"
1763 "left leaf blk %llu, right leaf blk %llu\n",
1764 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1765 left->p_tree_depth, right->p_tree_depth,
1766 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1767 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1768 } while (left->p_node[i].bh->b_blocknr ==
1769 right->p_node[i].bh->b_blocknr);
1771 return i - 1;
1774 typedef void (path_insert_t)(void *, struct buffer_head *);
1777 * Traverse a btree path in search of cpos, starting at root_el.
1779 * This code can be called with a cpos larger than the tree, in which
1780 * case it will return the rightmost path.
1782 static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
1783 struct ocfs2_extent_list *root_el, u32 cpos,
1784 path_insert_t *func, void *data)
1786 int i, ret = 0;
1787 u32 range;
1788 u64 blkno;
1789 struct buffer_head *bh = NULL;
1790 struct ocfs2_extent_block *eb;
1791 struct ocfs2_extent_list *el;
1792 struct ocfs2_extent_rec *rec;
1794 el = root_el;
1795 while (el->l_tree_depth) {
1796 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1797 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1798 "Owner %llu has empty extent list at "
1799 "depth %u\n",
1800 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1801 le16_to_cpu(el->l_tree_depth));
1802 ret = -EROFS;
1803 goto out;
1807 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1808 rec = &el->l_recs[i];
1811 * In the case that cpos is off the allocation
1812 * tree, this should just wind up returning the
1813 * rightmost record.
1815 range = le32_to_cpu(rec->e_cpos) +
1816 ocfs2_rec_clusters(el, rec);
1817 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1818 break;
1821 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1822 if (blkno == 0) {
1823 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1824 "Owner %llu has bad blkno in extent list "
1825 "at depth %u (index %d)\n",
1826 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1827 le16_to_cpu(el->l_tree_depth), i);
1828 ret = -EROFS;
1829 goto out;
1832 brelse(bh);
1833 bh = NULL;
1834 ret = ocfs2_read_extent_block(ci, blkno, &bh);
1835 if (ret) {
1836 mlog_errno(ret);
1837 goto out;
1840 eb = (struct ocfs2_extent_block *) bh->b_data;
1841 el = &eb->h_list;
1843 if (le16_to_cpu(el->l_next_free_rec) >
1844 le16_to_cpu(el->l_count)) {
1845 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1846 "Owner %llu has bad count in extent list "
1847 "at block %llu (next free=%u, count=%u)\n",
1848 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1849 (unsigned long long)bh->b_blocknr,
1850 le16_to_cpu(el->l_next_free_rec),
1851 le16_to_cpu(el->l_count));
1852 ret = -EROFS;
1853 goto out;
1856 if (func)
1857 func(data, bh);
1860 out:
1862 * Catch any trailing bh that the loop didn't handle.
1864 brelse(bh);
1866 return ret;
1870 * Given an initialized path (that is, it has a valid root extent
1871 * list), this function will traverse the btree in search of the path
1872 * which would contain cpos.
1874 * The path traveled is recorded in the path structure.
1876 * Note that this will not do any comparisons on leaf node extent
1877 * records, so it will work fine in the case that we just added a tree
1878 * branch.
1880 struct find_path_data {
1881 int index;
1882 struct ocfs2_path *path;
1884 static void find_path_ins(void *data, struct buffer_head *bh)
1886 struct find_path_data *fp = data;
1888 get_bh(bh);
1889 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1890 fp->index++;
1892 int ocfs2_find_path(struct ocfs2_caching_info *ci,
1893 struct ocfs2_path *path, u32 cpos)
1895 struct find_path_data data;
1897 data.index = 1;
1898 data.path = path;
1899 return __ocfs2_find_path(ci, path_root_el(path), cpos,
1900 find_path_ins, &data);
1903 static void find_leaf_ins(void *data, struct buffer_head *bh)
1905 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1906 struct ocfs2_extent_list *el = &eb->h_list;
1907 struct buffer_head **ret = data;
1909 /* We want to retain only the leaf block. */
1910 if (le16_to_cpu(el->l_tree_depth) == 0) {
1911 get_bh(bh);
1912 *ret = bh;
1916 * Find the leaf block in the tree which would contain cpos. No
1917 * checking of the actual leaf is done.
1919 * Some paths want to call this instead of allocating a path structure
1920 * and calling ocfs2_find_path().
1922 * This function doesn't handle non btree extent lists.
1924 int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1925 struct ocfs2_extent_list *root_el, u32 cpos,
1926 struct buffer_head **leaf_bh)
1928 int ret;
1929 struct buffer_head *bh = NULL;
1931 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
1932 if (ret) {
1933 mlog_errno(ret);
1934 goto out;
1937 *leaf_bh = bh;
1938 out:
1939 return ret;
1943 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1945 * Basically, we've moved stuff around at the bottom of the tree and
1946 * we need to fix up the extent records above the changes to reflect
1947 * the new changes.
1949 * left_rec: the record on the left.
1950 * left_child_el: is the child list pointed to by left_rec
1951 * right_rec: the record to the right of left_rec
1952 * right_child_el: is the child list pointed to by right_rec
1954 * By definition, this only works on interior nodes.
1956 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1957 struct ocfs2_extent_list *left_child_el,
1958 struct ocfs2_extent_rec *right_rec,
1959 struct ocfs2_extent_list *right_child_el)
1961 u32 left_clusters, right_end;
1964 * Interior nodes never have holes. Their cpos is the cpos of
1965 * the leftmost record in their child list. Their cluster
1966 * count covers the full theoretical range of their child list
1967 * - the range between their cpos and the cpos of the record
1968 * immediately to their right.
1970 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1971 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1972 BUG_ON(right_child_el->l_tree_depth);
1973 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1974 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1976 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1977 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1980 * Calculate the rightmost cluster count boundary before
1981 * moving cpos - we will need to adjust clusters after
1982 * updating e_cpos to keep the same highest cluster count.
1984 right_end = le32_to_cpu(right_rec->e_cpos);
1985 right_end += le32_to_cpu(right_rec->e_int_clusters);
1987 right_rec->e_cpos = left_rec->e_cpos;
1988 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1990 right_end -= le32_to_cpu(right_rec->e_cpos);
1991 right_rec->e_int_clusters = cpu_to_le32(right_end);
1995 * Adjust the adjacent root node records involved in a
1996 * rotation. left_el_blkno is passed in as a key so that we can easily
1997 * find it's index in the root list.
1999 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
2000 struct ocfs2_extent_list *left_el,
2001 struct ocfs2_extent_list *right_el,
2002 u64 left_el_blkno)
2004 int i;
2006 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
2007 le16_to_cpu(left_el->l_tree_depth));
2009 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
2010 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
2011 break;
2015 * The path walking code should have never returned a root and
2016 * two paths which are not adjacent.
2018 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2020 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
2021 &root_el->l_recs[i + 1], right_el);
2025 * We've changed a leaf block (in right_path) and need to reflect that
2026 * change back up the subtree.
2028 * This happens in multiple places:
2029 * - When we've moved an extent record from the left path leaf to the right
2030 * path leaf to make room for an empty extent in the left path leaf.
2031 * - When our insert into the right path leaf is at the leftmost edge
2032 * and requires an update of the path immediately to it's left. This
2033 * can occur at the end of some types of rotation and appending inserts.
2034 * - When we've adjusted the last extent record in the left path leaf and the
2035 * 1st extent record in the right path leaf during cross extent block merge.
2037 static void ocfs2_complete_edge_insert(handle_t *handle,
2038 struct ocfs2_path *left_path,
2039 struct ocfs2_path *right_path,
2040 int subtree_index)
2042 int i, idx;
2043 struct ocfs2_extent_list *el, *left_el, *right_el;
2044 struct ocfs2_extent_rec *left_rec, *right_rec;
2045 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2048 * Update the counts and position values within all the
2049 * interior nodes to reflect the leaf rotation we just did.
2051 * The root node is handled below the loop.
2053 * We begin the loop with right_el and left_el pointing to the
2054 * leaf lists and work our way up.
2056 * NOTE: within this loop, left_el and right_el always refer
2057 * to the *child* lists.
2059 left_el = path_leaf_el(left_path);
2060 right_el = path_leaf_el(right_path);
2061 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2062 mlog(0, "Adjust records at index %u\n", i);
2065 * One nice property of knowing that all of these
2066 * nodes are below the root is that we only deal with
2067 * the leftmost right node record and the rightmost
2068 * left node record.
2070 el = left_path->p_node[i].el;
2071 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2072 left_rec = &el->l_recs[idx];
2074 el = right_path->p_node[i].el;
2075 right_rec = &el->l_recs[0];
2077 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2078 right_el);
2080 ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2081 ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2084 * Setup our list pointers now so that the current
2085 * parents become children in the next iteration.
2087 left_el = left_path->p_node[i].el;
2088 right_el = right_path->p_node[i].el;
2092 * At the root node, adjust the two adjacent records which
2093 * begin our path to the leaves.
2096 el = left_path->p_node[subtree_index].el;
2097 left_el = left_path->p_node[subtree_index + 1].el;
2098 right_el = right_path->p_node[subtree_index + 1].el;
2100 ocfs2_adjust_root_records(el, left_el, right_el,
2101 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2103 root_bh = left_path->p_node[subtree_index].bh;
2105 ocfs2_journal_dirty(handle, root_bh);
2108 static int ocfs2_rotate_subtree_right(handle_t *handle,
2109 struct ocfs2_extent_tree *et,
2110 struct ocfs2_path *left_path,
2111 struct ocfs2_path *right_path,
2112 int subtree_index)
2114 int ret, i;
2115 struct buffer_head *right_leaf_bh;
2116 struct buffer_head *left_leaf_bh = NULL;
2117 struct buffer_head *root_bh;
2118 struct ocfs2_extent_list *right_el, *left_el;
2119 struct ocfs2_extent_rec move_rec;
2121 left_leaf_bh = path_leaf_bh(left_path);
2122 left_el = path_leaf_el(left_path);
2124 if (left_el->l_next_free_rec != left_el->l_count) {
2125 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
2126 "Inode %llu has non-full interior leaf node %llu"
2127 "(next free = %u)",
2128 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2129 (unsigned long long)left_leaf_bh->b_blocknr,
2130 le16_to_cpu(left_el->l_next_free_rec));
2131 return -EROFS;
2135 * This extent block may already have an empty record, so we
2136 * return early if so.
2138 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2139 return 0;
2141 root_bh = left_path->p_node[subtree_index].bh;
2142 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2144 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2145 subtree_index);
2146 if (ret) {
2147 mlog_errno(ret);
2148 goto out;
2151 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2152 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2153 right_path, i);
2154 if (ret) {
2155 mlog_errno(ret);
2156 goto out;
2159 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2160 left_path, i);
2161 if (ret) {
2162 mlog_errno(ret);
2163 goto out;
2167 right_leaf_bh = path_leaf_bh(right_path);
2168 right_el = path_leaf_el(right_path);
2170 /* This is a code error, not a disk corruption. */
2171 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2172 "because rightmost leaf block %llu is empty\n",
2173 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2174 (unsigned long long)right_leaf_bh->b_blocknr);
2176 ocfs2_create_empty_extent(right_el);
2178 ocfs2_journal_dirty(handle, right_leaf_bh);
2180 /* Do the copy now. */
2181 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2182 move_rec = left_el->l_recs[i];
2183 right_el->l_recs[0] = move_rec;
2186 * Clear out the record we just copied and shift everything
2187 * over, leaving an empty extent in the left leaf.
2189 * We temporarily subtract from next_free_rec so that the
2190 * shift will lose the tail record (which is now defunct).
2192 le16_add_cpu(&left_el->l_next_free_rec, -1);
2193 ocfs2_shift_records_right(left_el);
2194 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2195 le16_add_cpu(&left_el->l_next_free_rec, 1);
2197 ocfs2_journal_dirty(handle, left_leaf_bh);
2199 ocfs2_complete_edge_insert(handle, left_path, right_path,
2200 subtree_index);
2202 out:
2203 return ret;
2207 * Given a full path, determine what cpos value would return us a path
2208 * containing the leaf immediately to the left of the current one.
2210 * Will return zero if the path passed in is already the leftmost path.
2212 int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2213 struct ocfs2_path *path, u32 *cpos)
2215 int i, j, ret = 0;
2216 u64 blkno;
2217 struct ocfs2_extent_list *el;
2219 BUG_ON(path->p_tree_depth == 0);
2221 *cpos = 0;
2223 blkno = path_leaf_bh(path)->b_blocknr;
2225 /* Start at the tree node just above the leaf and work our way up. */
2226 i = path->p_tree_depth - 1;
2227 while (i >= 0) {
2228 el = path->p_node[i].el;
2231 * Find the extent record just before the one in our
2232 * path.
2234 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2235 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2236 if (j == 0) {
2237 if (i == 0) {
2239 * We've determined that the
2240 * path specified is already
2241 * the leftmost one - return a
2242 * cpos of zero.
2244 goto out;
2247 * The leftmost record points to our
2248 * leaf - we need to travel up the
2249 * tree one level.
2251 goto next_node;
2254 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
2255 *cpos = *cpos + ocfs2_rec_clusters(el,
2256 &el->l_recs[j - 1]);
2257 *cpos = *cpos - 1;
2258 goto out;
2263 * If we got here, we never found a valid node where
2264 * the tree indicated one should be.
2266 ocfs2_error(sb,
2267 "Invalid extent tree at extent block %llu\n",
2268 (unsigned long long)blkno);
2269 ret = -EROFS;
2270 goto out;
2272 next_node:
2273 blkno = path->p_node[i].bh->b_blocknr;
2274 i--;
2277 out:
2278 return ret;
2282 * Extend the transaction by enough credits to complete the rotation,
2283 * and still leave at least the original number of credits allocated
2284 * to this transaction.
2286 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
2287 int op_credits,
2288 struct ocfs2_path *path)
2290 int ret = 0;
2291 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
2293 if (handle->h_buffer_credits < credits)
2294 ret = ocfs2_extend_trans(handle,
2295 credits - handle->h_buffer_credits);
2297 return ret;
2301 * Trap the case where we're inserting into the theoretical range past
2302 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2303 * whose cpos is less than ours into the right leaf.
2305 * It's only necessary to look at the rightmost record of the left
2306 * leaf because the logic that calls us should ensure that the
2307 * theoretical ranges in the path components above the leaves are
2308 * correct.
2310 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2311 u32 insert_cpos)
2313 struct ocfs2_extent_list *left_el;
2314 struct ocfs2_extent_rec *rec;
2315 int next_free;
2317 left_el = path_leaf_el(left_path);
2318 next_free = le16_to_cpu(left_el->l_next_free_rec);
2319 rec = &left_el->l_recs[next_free - 1];
2321 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2322 return 1;
2323 return 0;
2326 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2328 int next_free = le16_to_cpu(el->l_next_free_rec);
2329 unsigned int range;
2330 struct ocfs2_extent_rec *rec;
2332 if (next_free == 0)
2333 return 0;
2335 rec = &el->l_recs[0];
2336 if (ocfs2_is_empty_extent(rec)) {
2337 /* Empty list. */
2338 if (next_free == 1)
2339 return 0;
2340 rec = &el->l_recs[1];
2343 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2344 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2345 return 1;
2346 return 0;
2350 * Rotate all the records in a btree right one record, starting at insert_cpos.
2352 * The path to the rightmost leaf should be passed in.
2354 * The array is assumed to be large enough to hold an entire path (tree depth).
2356 * Upon successful return from this function:
2358 * - The 'right_path' array will contain a path to the leaf block
2359 * whose range contains e_cpos.
2360 * - That leaf block will have a single empty extent in list index 0.
2361 * - In the case that the rotation requires a post-insert update,
2362 * *ret_left_path will contain a valid path which can be passed to
2363 * ocfs2_insert_path().
2365 static int ocfs2_rotate_tree_right(handle_t *handle,
2366 struct ocfs2_extent_tree *et,
2367 enum ocfs2_split_type split,
2368 u32 insert_cpos,
2369 struct ocfs2_path *right_path,
2370 struct ocfs2_path **ret_left_path)
2372 int ret, start, orig_credits = handle->h_buffer_credits;
2373 u32 cpos;
2374 struct ocfs2_path *left_path = NULL;
2375 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2377 *ret_left_path = NULL;
2379 left_path = ocfs2_new_path_from_path(right_path);
2380 if (!left_path) {
2381 ret = -ENOMEM;
2382 mlog_errno(ret);
2383 goto out;
2386 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2387 if (ret) {
2388 mlog_errno(ret);
2389 goto out;
2392 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2395 * What we want to do here is:
2397 * 1) Start with the rightmost path.
2399 * 2) Determine a path to the leaf block directly to the left
2400 * of that leaf.
2402 * 3) Determine the 'subtree root' - the lowest level tree node
2403 * which contains a path to both leaves.
2405 * 4) Rotate the subtree.
2407 * 5) Find the next subtree by considering the left path to be
2408 * the new right path.
2410 * The check at the top of this while loop also accepts
2411 * insert_cpos == cpos because cpos is only a _theoretical_
2412 * value to get us the left path - insert_cpos might very well
2413 * be filling that hole.
2415 * Stop at a cpos of '0' because we either started at the
2416 * leftmost branch (i.e., a tree with one branch and a
2417 * rotation inside of it), or we've gone as far as we can in
2418 * rotating subtrees.
2420 while (cpos && insert_cpos <= cpos) {
2421 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2422 insert_cpos, cpos);
2424 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
2425 if (ret) {
2426 mlog_errno(ret);
2427 goto out;
2430 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2431 path_leaf_bh(right_path),
2432 "Owner %llu: error during insert of %u "
2433 "(left path cpos %u) results in two identical "
2434 "paths ending at %llu\n",
2435 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2436 insert_cpos, cpos,
2437 (unsigned long long)
2438 path_leaf_bh(left_path)->b_blocknr);
2440 if (split == SPLIT_NONE &&
2441 ocfs2_rotate_requires_path_adjustment(left_path,
2442 insert_cpos)) {
2445 * We've rotated the tree as much as we
2446 * should. The rest is up to
2447 * ocfs2_insert_path() to complete, after the
2448 * record insertion. We indicate this
2449 * situation by returning the left path.
2451 * The reason we don't adjust the records here
2452 * before the record insert is that an error
2453 * later might break the rule where a parent
2454 * record e_cpos will reflect the actual
2455 * e_cpos of the 1st nonempty record of the
2456 * child list.
2458 *ret_left_path = left_path;
2459 goto out_ret_path;
2462 start = ocfs2_find_subtree_root(et, left_path, right_path);
2464 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2465 start,
2466 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2467 right_path->p_tree_depth);
2469 ret = ocfs2_extend_rotate_transaction(handle, start,
2470 orig_credits, right_path);
2471 if (ret) {
2472 mlog_errno(ret);
2473 goto out;
2476 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
2477 right_path, start);
2478 if (ret) {
2479 mlog_errno(ret);
2480 goto out;
2483 if (split != SPLIT_NONE &&
2484 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2485 insert_cpos)) {
2487 * A rotate moves the rightmost left leaf
2488 * record over to the leftmost right leaf
2489 * slot. If we're doing an extent split
2490 * instead of a real insert, then we have to
2491 * check that the extent to be split wasn't
2492 * just moved over. If it was, then we can
2493 * exit here, passing left_path back -
2494 * ocfs2_split_extent() is smart enough to
2495 * search both leaves.
2497 *ret_left_path = left_path;
2498 goto out_ret_path;
2502 * There is no need to re-read the next right path
2503 * as we know that it'll be our current left
2504 * path. Optimize by copying values instead.
2506 ocfs2_mv_path(right_path, left_path);
2508 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2509 if (ret) {
2510 mlog_errno(ret);
2511 goto out;
2515 out:
2516 ocfs2_free_path(left_path);
2518 out_ret_path:
2519 return ret;
2522 static int ocfs2_update_edge_lengths(handle_t *handle,
2523 struct ocfs2_extent_tree *et,
2524 int subtree_index, struct ocfs2_path *path)
2526 int i, idx, ret;
2527 struct ocfs2_extent_rec *rec;
2528 struct ocfs2_extent_list *el;
2529 struct ocfs2_extent_block *eb;
2530 u32 range;
2533 * In normal tree rotation process, we will never touch the
2534 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2535 * doesn't reserve the credits for them either.
2537 * But we do have a special case here which will update the rightmost
2538 * records for all the bh in the path.
2539 * So we have to allocate extra credits and access them.
2541 ret = ocfs2_extend_trans(handle, subtree_index);
2542 if (ret) {
2543 mlog_errno(ret);
2544 goto out;
2547 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
2548 if (ret) {
2549 mlog_errno(ret);
2550 goto out;
2553 /* Path should always be rightmost. */
2554 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2555 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2557 el = &eb->h_list;
2558 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2559 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2560 rec = &el->l_recs[idx];
2561 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2563 for (i = 0; i < path->p_tree_depth; i++) {
2564 el = path->p_node[i].el;
2565 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2566 rec = &el->l_recs[idx];
2568 rec->e_int_clusters = cpu_to_le32(range);
2569 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2571 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2573 out:
2574 return ret;
2577 static void ocfs2_unlink_path(handle_t *handle,
2578 struct ocfs2_extent_tree *et,
2579 struct ocfs2_cached_dealloc_ctxt *dealloc,
2580 struct ocfs2_path *path, int unlink_start)
2582 int ret, i;
2583 struct ocfs2_extent_block *eb;
2584 struct ocfs2_extent_list *el;
2585 struct buffer_head *bh;
2587 for(i = unlink_start; i < path_num_items(path); i++) {
2588 bh = path->p_node[i].bh;
2590 eb = (struct ocfs2_extent_block *)bh->b_data;
2592 * Not all nodes might have had their final count
2593 * decremented by the caller - handle this here.
2595 el = &eb->h_list;
2596 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2597 mlog(ML_ERROR,
2598 "Inode %llu, attempted to remove extent block "
2599 "%llu with %u records\n",
2600 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2601 (unsigned long long)le64_to_cpu(eb->h_blkno),
2602 le16_to_cpu(el->l_next_free_rec));
2604 ocfs2_journal_dirty(handle, bh);
2605 ocfs2_remove_from_cache(et->et_ci, bh);
2606 continue;
2609 el->l_next_free_rec = 0;
2610 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2612 ocfs2_journal_dirty(handle, bh);
2614 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2615 if (ret)
2616 mlog_errno(ret);
2618 ocfs2_remove_from_cache(et->et_ci, bh);
2622 static void ocfs2_unlink_subtree(handle_t *handle,
2623 struct ocfs2_extent_tree *et,
2624 struct ocfs2_path *left_path,
2625 struct ocfs2_path *right_path,
2626 int subtree_index,
2627 struct ocfs2_cached_dealloc_ctxt *dealloc)
2629 int i;
2630 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2631 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2632 struct ocfs2_extent_list *el;
2633 struct ocfs2_extent_block *eb;
2635 el = path_leaf_el(left_path);
2637 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2639 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2640 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2641 break;
2643 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2645 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2646 le16_add_cpu(&root_el->l_next_free_rec, -1);
2648 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2649 eb->h_next_leaf_blk = 0;
2651 ocfs2_journal_dirty(handle, root_bh);
2652 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2654 ocfs2_unlink_path(handle, et, dealloc, right_path,
2655 subtree_index + 1);
2658 static int ocfs2_rotate_subtree_left(handle_t *handle,
2659 struct ocfs2_extent_tree *et,
2660 struct ocfs2_path *left_path,
2661 struct ocfs2_path *right_path,
2662 int subtree_index,
2663 struct ocfs2_cached_dealloc_ctxt *dealloc,
2664 int *deleted)
2666 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2667 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2668 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2669 struct ocfs2_extent_block *eb;
2671 *deleted = 0;
2673 right_leaf_el = path_leaf_el(right_path);
2674 left_leaf_el = path_leaf_el(left_path);
2675 root_bh = left_path->p_node[subtree_index].bh;
2676 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2678 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2679 return 0;
2681 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2682 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2684 * It's legal for us to proceed if the right leaf is
2685 * the rightmost one and it has an empty extent. There
2686 * are two cases to handle - whether the leaf will be
2687 * empty after removal or not. If the leaf isn't empty
2688 * then just remove the empty extent up front. The
2689 * next block will handle empty leaves by flagging
2690 * them for unlink.
2692 * Non rightmost leaves will throw -EAGAIN and the
2693 * caller can manually move the subtree and retry.
2696 if (eb->h_next_leaf_blk != 0ULL)
2697 return -EAGAIN;
2699 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2700 ret = ocfs2_journal_access_eb(handle, et->et_ci,
2701 path_leaf_bh(right_path),
2702 OCFS2_JOURNAL_ACCESS_WRITE);
2703 if (ret) {
2704 mlog_errno(ret);
2705 goto out;
2708 ocfs2_remove_empty_extent(right_leaf_el);
2709 } else
2710 right_has_empty = 1;
2713 if (eb->h_next_leaf_blk == 0ULL &&
2714 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2716 * We have to update i_last_eb_blk during the meta
2717 * data delete.
2719 ret = ocfs2_et_root_journal_access(handle, et,
2720 OCFS2_JOURNAL_ACCESS_WRITE);
2721 if (ret) {
2722 mlog_errno(ret);
2723 goto out;
2726 del_right_subtree = 1;
2730 * Getting here with an empty extent in the right path implies
2731 * that it's the rightmost path and will be deleted.
2733 BUG_ON(right_has_empty && !del_right_subtree);
2735 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2736 subtree_index);
2737 if (ret) {
2738 mlog_errno(ret);
2739 goto out;
2742 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2743 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2744 right_path, i);
2745 if (ret) {
2746 mlog_errno(ret);
2747 goto out;
2750 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2751 left_path, i);
2752 if (ret) {
2753 mlog_errno(ret);
2754 goto out;
2758 if (!right_has_empty) {
2760 * Only do this if we're moving a real
2761 * record. Otherwise, the action is delayed until
2762 * after removal of the right path in which case we
2763 * can do a simple shift to remove the empty extent.
2765 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2766 memset(&right_leaf_el->l_recs[0], 0,
2767 sizeof(struct ocfs2_extent_rec));
2769 if (eb->h_next_leaf_blk == 0ULL) {
2771 * Move recs over to get rid of empty extent, decrease
2772 * next_free. This is allowed to remove the last
2773 * extent in our leaf (setting l_next_free_rec to
2774 * zero) - the delete code below won't care.
2776 ocfs2_remove_empty_extent(right_leaf_el);
2779 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2780 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2782 if (del_right_subtree) {
2783 ocfs2_unlink_subtree(handle, et, left_path, right_path,
2784 subtree_index, dealloc);
2785 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
2786 left_path);
2787 if (ret) {
2788 mlog_errno(ret);
2789 goto out;
2792 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2793 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2796 * Removal of the extent in the left leaf was skipped
2797 * above so we could delete the right path
2798 * 1st.
2800 if (right_has_empty)
2801 ocfs2_remove_empty_extent(left_leaf_el);
2803 ocfs2_journal_dirty(handle, et_root_bh);
2805 *deleted = 1;
2806 } else
2807 ocfs2_complete_edge_insert(handle, left_path, right_path,
2808 subtree_index);
2810 out:
2811 return ret;
2815 * Given a full path, determine what cpos value would return us a path
2816 * containing the leaf immediately to the right of the current one.
2818 * Will return zero if the path passed in is already the rightmost path.
2820 * This looks similar, but is subtly different to
2821 * ocfs2_find_cpos_for_left_leaf().
2823 int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2824 struct ocfs2_path *path, u32 *cpos)
2826 int i, j, ret = 0;
2827 u64 blkno;
2828 struct ocfs2_extent_list *el;
2830 *cpos = 0;
2832 if (path->p_tree_depth == 0)
2833 return 0;
2835 blkno = path_leaf_bh(path)->b_blocknr;
2837 /* Start at the tree node just above the leaf and work our way up. */
2838 i = path->p_tree_depth - 1;
2839 while (i >= 0) {
2840 int next_free;
2842 el = path->p_node[i].el;
2845 * Find the extent record just after the one in our
2846 * path.
2848 next_free = le16_to_cpu(el->l_next_free_rec);
2849 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2850 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2851 if (j == (next_free - 1)) {
2852 if (i == 0) {
2854 * We've determined that the
2855 * path specified is already
2856 * the rightmost one - return a
2857 * cpos of zero.
2859 goto out;
2862 * The rightmost record points to our
2863 * leaf - we need to travel up the
2864 * tree one level.
2866 goto next_node;
2869 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2870 goto out;
2875 * If we got here, we never found a valid node where
2876 * the tree indicated one should be.
2878 ocfs2_error(sb,
2879 "Invalid extent tree at extent block %llu\n",
2880 (unsigned long long)blkno);
2881 ret = -EROFS;
2882 goto out;
2884 next_node:
2885 blkno = path->p_node[i].bh->b_blocknr;
2886 i--;
2889 out:
2890 return ret;
2893 static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2894 struct ocfs2_extent_tree *et,
2895 struct ocfs2_path *path)
2897 int ret;
2898 struct buffer_head *bh = path_leaf_bh(path);
2899 struct ocfs2_extent_list *el = path_leaf_el(path);
2901 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2902 return 0;
2904 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
2905 path_num_items(path) - 1);
2906 if (ret) {
2907 mlog_errno(ret);
2908 goto out;
2911 ocfs2_remove_empty_extent(el);
2912 ocfs2_journal_dirty(handle, bh);
2914 out:
2915 return ret;
2918 static int __ocfs2_rotate_tree_left(handle_t *handle,
2919 struct ocfs2_extent_tree *et,
2920 int orig_credits,
2921 struct ocfs2_path *path,
2922 struct ocfs2_cached_dealloc_ctxt *dealloc,
2923 struct ocfs2_path **empty_extent_path)
2925 int ret, subtree_root, deleted;
2926 u32 right_cpos;
2927 struct ocfs2_path *left_path = NULL;
2928 struct ocfs2_path *right_path = NULL;
2929 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2931 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2933 *empty_extent_path = NULL;
2935 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
2936 if (ret) {
2937 mlog_errno(ret);
2938 goto out;
2941 left_path = ocfs2_new_path_from_path(path);
2942 if (!left_path) {
2943 ret = -ENOMEM;
2944 mlog_errno(ret);
2945 goto out;
2948 ocfs2_cp_path(left_path, path);
2950 right_path = ocfs2_new_path_from_path(path);
2951 if (!right_path) {
2952 ret = -ENOMEM;
2953 mlog_errno(ret);
2954 goto out;
2957 while (right_cpos) {
2958 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
2959 if (ret) {
2960 mlog_errno(ret);
2961 goto out;
2964 subtree_root = ocfs2_find_subtree_root(et, left_path,
2965 right_path);
2967 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2968 subtree_root,
2969 (unsigned long long)
2970 right_path->p_node[subtree_root].bh->b_blocknr,
2971 right_path->p_tree_depth);
2973 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2974 orig_credits, left_path);
2975 if (ret) {
2976 mlog_errno(ret);
2977 goto out;
2981 * Caller might still want to make changes to the
2982 * tree root, so re-add it to the journal here.
2984 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2985 left_path, 0);
2986 if (ret) {
2987 mlog_errno(ret);
2988 goto out;
2991 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
2992 right_path, subtree_root,
2993 dealloc, &deleted);
2994 if (ret == -EAGAIN) {
2996 * The rotation has to temporarily stop due to
2997 * the right subtree having an empty
2998 * extent. Pass it back to the caller for a
2999 * fixup.
3001 *empty_extent_path = right_path;
3002 right_path = NULL;
3003 goto out;
3005 if (ret) {
3006 mlog_errno(ret);
3007 goto out;
3011 * The subtree rotate might have removed records on
3012 * the rightmost edge. If so, then rotation is
3013 * complete.
3015 if (deleted)
3016 break;
3018 ocfs2_mv_path(left_path, right_path);
3020 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
3021 &right_cpos);
3022 if (ret) {
3023 mlog_errno(ret);
3024 goto out;
3028 out:
3029 ocfs2_free_path(right_path);
3030 ocfs2_free_path(left_path);
3032 return ret;
3035 static int ocfs2_remove_rightmost_path(handle_t *handle,
3036 struct ocfs2_extent_tree *et,
3037 struct ocfs2_path *path,
3038 struct ocfs2_cached_dealloc_ctxt *dealloc)
3040 int ret, subtree_index;
3041 u32 cpos;
3042 struct ocfs2_path *left_path = NULL;
3043 struct ocfs2_extent_block *eb;
3044 struct ocfs2_extent_list *el;
3047 ret = ocfs2_et_sanity_check(et);
3048 if (ret)
3049 goto out;
3051 * There's two ways we handle this depending on
3052 * whether path is the only existing one.
3054 ret = ocfs2_extend_rotate_transaction(handle, 0,
3055 handle->h_buffer_credits,
3056 path);
3057 if (ret) {
3058 mlog_errno(ret);
3059 goto out;
3062 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
3063 if (ret) {
3064 mlog_errno(ret);
3065 goto out;
3068 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3069 path, &cpos);
3070 if (ret) {
3071 mlog_errno(ret);
3072 goto out;
3075 if (cpos) {
3077 * We have a path to the left of this one - it needs
3078 * an update too.
3080 left_path = ocfs2_new_path_from_path(path);
3081 if (!left_path) {
3082 ret = -ENOMEM;
3083 mlog_errno(ret);
3084 goto out;
3087 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
3088 if (ret) {
3089 mlog_errno(ret);
3090 goto out;
3093 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
3094 if (ret) {
3095 mlog_errno(ret);
3096 goto out;
3099 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
3101 ocfs2_unlink_subtree(handle, et, left_path, path,
3102 subtree_index, dealloc);
3103 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
3104 left_path);
3105 if (ret) {
3106 mlog_errno(ret);
3107 goto out;
3110 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
3111 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
3112 } else {
3114 * 'path' is also the leftmost path which
3115 * means it must be the only one. This gets
3116 * handled differently because we want to
3117 * revert the root back to having extents
3118 * in-line.
3120 ocfs2_unlink_path(handle, et, dealloc, path, 1);
3122 el = et->et_root_el;
3123 el->l_tree_depth = 0;
3124 el->l_next_free_rec = 0;
3125 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3127 ocfs2_et_set_last_eb_blk(et, 0);
3130 ocfs2_journal_dirty(handle, path_root_bh(path));
3132 out:
3133 ocfs2_free_path(left_path);
3134 return ret;
3138 * Left rotation of btree records.
3140 * In many ways, this is (unsurprisingly) the opposite of right
3141 * rotation. We start at some non-rightmost path containing an empty
3142 * extent in the leaf block. The code works its way to the rightmost
3143 * path by rotating records to the left in every subtree.
3145 * This is used by any code which reduces the number of extent records
3146 * in a leaf. After removal, an empty record should be placed in the
3147 * leftmost list position.
3149 * This won't handle a length update of the rightmost path records if
3150 * the rightmost tree leaf record is removed so the caller is
3151 * responsible for detecting and correcting that.
3153 static int ocfs2_rotate_tree_left(handle_t *handle,
3154 struct ocfs2_extent_tree *et,
3155 struct ocfs2_path *path,
3156 struct ocfs2_cached_dealloc_ctxt *dealloc)
3158 int ret, orig_credits = handle->h_buffer_credits;
3159 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3160 struct ocfs2_extent_block *eb;
3161 struct ocfs2_extent_list *el;
3163 el = path_leaf_el(path);
3164 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3165 return 0;
3167 if (path->p_tree_depth == 0) {
3168 rightmost_no_delete:
3170 * Inline extents. This is trivially handled, so do
3171 * it up front.
3173 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
3174 if (ret)
3175 mlog_errno(ret);
3176 goto out;
3180 * Handle rightmost branch now. There's several cases:
3181 * 1) simple rotation leaving records in there. That's trivial.
3182 * 2) rotation requiring a branch delete - there's no more
3183 * records left. Two cases of this:
3184 * a) There are branches to the left.
3185 * b) This is also the leftmost (the only) branch.
3187 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3188 * 2a) we need the left branch so that we can update it with the unlink
3189 * 2b) we need to bring the root back to inline extents.
3192 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3193 el = &eb->h_list;
3194 if (eb->h_next_leaf_blk == 0) {
3196 * This gets a bit tricky if we're going to delete the
3197 * rightmost path. Get the other cases out of the way
3198 * 1st.
3200 if (le16_to_cpu(el->l_next_free_rec) > 1)
3201 goto rightmost_no_delete;
3203 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3204 ret = -EIO;
3205 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3206 "Owner %llu has empty extent block at %llu",
3207 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
3208 (unsigned long long)le64_to_cpu(eb->h_blkno));
3209 goto out;
3213 * XXX: The caller can not trust "path" any more after
3214 * this as it will have been deleted. What do we do?
3216 * In theory the rotate-for-merge code will never get
3217 * here because it'll always ask for a rotate in a
3218 * nonempty list.
3221 ret = ocfs2_remove_rightmost_path(handle, et, path,
3222 dealloc);
3223 if (ret)
3224 mlog_errno(ret);
3225 goto out;
3229 * Now we can loop, remembering the path we get from -EAGAIN
3230 * and restarting from there.
3232 try_rotate:
3233 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3234 dealloc, &restart_path);
3235 if (ret && ret != -EAGAIN) {
3236 mlog_errno(ret);
3237 goto out;
3240 while (ret == -EAGAIN) {
3241 tmp_path = restart_path;
3242 restart_path = NULL;
3244 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
3245 tmp_path, dealloc,
3246 &restart_path);
3247 if (ret && ret != -EAGAIN) {
3248 mlog_errno(ret);
3249 goto out;
3252 ocfs2_free_path(tmp_path);
3253 tmp_path = NULL;
3255 if (ret == 0)
3256 goto try_rotate;
3259 out:
3260 ocfs2_free_path(tmp_path);
3261 ocfs2_free_path(restart_path);
3262 return ret;
3265 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3266 int index)
3268 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3269 unsigned int size;
3271 if (rec->e_leaf_clusters == 0) {
3273 * We consumed all of the merged-from record. An empty
3274 * extent cannot exist anywhere but the 1st array
3275 * position, so move things over if the merged-from
3276 * record doesn't occupy that position.
3278 * This creates a new empty extent so the caller
3279 * should be smart enough to have removed any existing
3280 * ones.
3282 if (index > 0) {
3283 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3284 size = index * sizeof(struct ocfs2_extent_rec);
3285 memmove(&el->l_recs[1], &el->l_recs[0], size);
3289 * Always memset - the caller doesn't check whether it
3290 * created an empty extent, so there could be junk in
3291 * the other fields.
3293 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3297 static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
3298 struct ocfs2_path *left_path,
3299 struct ocfs2_path **ret_right_path)
3301 int ret;
3302 u32 right_cpos;
3303 struct ocfs2_path *right_path = NULL;
3304 struct ocfs2_extent_list *left_el;
3306 *ret_right_path = NULL;
3308 /* This function shouldn't be called for non-trees. */
3309 BUG_ON(left_path->p_tree_depth == 0);
3311 left_el = path_leaf_el(left_path);
3312 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3314 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3315 left_path, &right_cpos);
3316 if (ret) {
3317 mlog_errno(ret);
3318 goto out;
3321 /* This function shouldn't be called for the rightmost leaf. */
3322 BUG_ON(right_cpos == 0);
3324 right_path = ocfs2_new_path_from_path(left_path);
3325 if (!right_path) {
3326 ret = -ENOMEM;
3327 mlog_errno(ret);
3328 goto out;
3331 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
3332 if (ret) {
3333 mlog_errno(ret);
3334 goto out;
3337 *ret_right_path = right_path;
3338 out:
3339 if (ret)
3340 ocfs2_free_path(right_path);
3341 return ret;
3345 * Remove split_rec clusters from the record at index and merge them
3346 * onto the beginning of the record "next" to it.
3347 * For index < l_count - 1, the next means the extent rec at index + 1.
3348 * For index == l_count - 1, the "next" means the 1st extent rec of the
3349 * next extent block.
3351 static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
3352 handle_t *handle,
3353 struct ocfs2_extent_tree *et,
3354 struct ocfs2_extent_rec *split_rec,
3355 int index)
3357 int ret, next_free, i;
3358 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3359 struct ocfs2_extent_rec *left_rec;
3360 struct ocfs2_extent_rec *right_rec;
3361 struct ocfs2_extent_list *right_el;
3362 struct ocfs2_path *right_path = NULL;
3363 int subtree_index = 0;
3364 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3365 struct buffer_head *bh = path_leaf_bh(left_path);
3366 struct buffer_head *root_bh = NULL;
3368 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3369 left_rec = &el->l_recs[index];
3371 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3372 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3373 /* we meet with a cross extent block merge. */
3374 ret = ocfs2_get_right_path(et, left_path, &right_path);
3375 if (ret) {
3376 mlog_errno(ret);
3377 goto out;
3380 right_el = path_leaf_el(right_path);
3381 next_free = le16_to_cpu(right_el->l_next_free_rec);
3382 BUG_ON(next_free <= 0);
3383 right_rec = &right_el->l_recs[0];
3384 if (ocfs2_is_empty_extent(right_rec)) {
3385 BUG_ON(next_free <= 1);
3386 right_rec = &right_el->l_recs[1];
3389 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3390 le16_to_cpu(left_rec->e_leaf_clusters) !=
3391 le32_to_cpu(right_rec->e_cpos));
3393 subtree_index = ocfs2_find_subtree_root(et, left_path,
3394 right_path);
3396 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3397 handle->h_buffer_credits,
3398 right_path);
3399 if (ret) {
3400 mlog_errno(ret);
3401 goto out;
3404 root_bh = left_path->p_node[subtree_index].bh;
3405 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3407 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3408 subtree_index);
3409 if (ret) {
3410 mlog_errno(ret);
3411 goto out;
3414 for (i = subtree_index + 1;
3415 i < path_num_items(right_path); i++) {
3416 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3417 right_path, i);
3418 if (ret) {
3419 mlog_errno(ret);
3420 goto out;
3423 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3424 left_path, i);
3425 if (ret) {
3426 mlog_errno(ret);
3427 goto out;
3431 } else {
3432 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3433 right_rec = &el->l_recs[index + 1];
3436 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
3437 path_num_items(left_path) - 1);
3438 if (ret) {
3439 mlog_errno(ret);
3440 goto out;
3443 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3445 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3446 le64_add_cpu(&right_rec->e_blkno,
3447 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3448 split_clusters));
3449 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3451 ocfs2_cleanup_merge(el, index);
3453 ocfs2_journal_dirty(handle, bh);
3454 if (right_path) {
3455 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3456 ocfs2_complete_edge_insert(handle, left_path, right_path,
3457 subtree_index);
3459 out:
3460 if (right_path)
3461 ocfs2_free_path(right_path);
3462 return ret;
3465 static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
3466 struct ocfs2_path *right_path,
3467 struct ocfs2_path **ret_left_path)
3469 int ret;
3470 u32 left_cpos;
3471 struct ocfs2_path *left_path = NULL;
3473 *ret_left_path = NULL;
3475 /* This function shouldn't be called for non-trees. */
3476 BUG_ON(right_path->p_tree_depth == 0);
3478 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3479 right_path, &left_cpos);
3480 if (ret) {
3481 mlog_errno(ret);
3482 goto out;
3485 /* This function shouldn't be called for the leftmost leaf. */
3486 BUG_ON(left_cpos == 0);
3488 left_path = ocfs2_new_path_from_path(right_path);
3489 if (!left_path) {
3490 ret = -ENOMEM;
3491 mlog_errno(ret);
3492 goto out;
3495 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
3496 if (ret) {
3497 mlog_errno(ret);
3498 goto out;
3501 *ret_left_path = left_path;
3502 out:
3503 if (ret)
3504 ocfs2_free_path(left_path);
3505 return ret;
3509 * Remove split_rec clusters from the record at index and merge them
3510 * onto the tail of the record "before" it.
3511 * For index > 0, the "before" means the extent rec at index - 1.
3513 * For index == 0, the "before" means the last record of the previous
3514 * extent block. And there is also a situation that we may need to
3515 * remove the rightmost leaf extent block in the right_path and change
3516 * the right path to indicate the new rightmost path.
3518 static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
3519 handle_t *handle,
3520 struct ocfs2_extent_tree *et,
3521 struct ocfs2_extent_rec *split_rec,
3522 struct ocfs2_cached_dealloc_ctxt *dealloc,
3523 int index)
3525 int ret, i, subtree_index = 0, has_empty_extent = 0;
3526 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3527 struct ocfs2_extent_rec *left_rec;
3528 struct ocfs2_extent_rec *right_rec;
3529 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3530 struct buffer_head *bh = path_leaf_bh(right_path);
3531 struct buffer_head *root_bh = NULL;
3532 struct ocfs2_path *left_path = NULL;
3533 struct ocfs2_extent_list *left_el;
3535 BUG_ON(index < 0);
3537 right_rec = &el->l_recs[index];
3538 if (index == 0) {
3539 /* we meet with a cross extent block merge. */
3540 ret = ocfs2_get_left_path(et, right_path, &left_path);
3541 if (ret) {
3542 mlog_errno(ret);
3543 goto out;
3546 left_el = path_leaf_el(left_path);
3547 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3548 le16_to_cpu(left_el->l_count));
3550 left_rec = &left_el->l_recs[
3551 le16_to_cpu(left_el->l_next_free_rec) - 1];
3552 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3553 le16_to_cpu(left_rec->e_leaf_clusters) !=
3554 le32_to_cpu(split_rec->e_cpos));
3556 subtree_index = ocfs2_find_subtree_root(et, left_path,
3557 right_path);
3559 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3560 handle->h_buffer_credits,
3561 left_path);
3562 if (ret) {
3563 mlog_errno(ret);
3564 goto out;
3567 root_bh = left_path->p_node[subtree_index].bh;
3568 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3570 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3571 subtree_index);
3572 if (ret) {
3573 mlog_errno(ret);
3574 goto out;
3577 for (i = subtree_index + 1;
3578 i < path_num_items(right_path); i++) {
3579 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3580 right_path, i);
3581 if (ret) {
3582 mlog_errno(ret);
3583 goto out;
3586 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3587 left_path, i);
3588 if (ret) {
3589 mlog_errno(ret);
3590 goto out;
3593 } else {
3594 left_rec = &el->l_recs[index - 1];
3595 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3596 has_empty_extent = 1;
3599 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3600 path_num_items(right_path) - 1);
3601 if (ret) {
3602 mlog_errno(ret);
3603 goto out;
3606 if (has_empty_extent && index == 1) {
3608 * The easy case - we can just plop the record right in.
3610 *left_rec = *split_rec;
3612 has_empty_extent = 0;
3613 } else
3614 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3616 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3617 le64_add_cpu(&right_rec->e_blkno,
3618 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3619 split_clusters));
3620 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3622 ocfs2_cleanup_merge(el, index);
3624 ocfs2_journal_dirty(handle, bh);
3625 if (left_path) {
3626 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3629 * In the situation that the right_rec is empty and the extent
3630 * block is empty also, ocfs2_complete_edge_insert can't handle
3631 * it and we need to delete the right extent block.
3633 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3634 le16_to_cpu(el->l_next_free_rec) == 1) {
3636 ret = ocfs2_remove_rightmost_path(handle, et,
3637 right_path,
3638 dealloc);
3639 if (ret) {
3640 mlog_errno(ret);
3641 goto out;
3644 /* Now the rightmost extent block has been deleted.
3645 * So we use the new rightmost path.
3647 ocfs2_mv_path(right_path, left_path);
3648 left_path = NULL;
3649 } else
3650 ocfs2_complete_edge_insert(handle, left_path,
3651 right_path, subtree_index);
3653 out:
3654 if (left_path)
3655 ocfs2_free_path(left_path);
3656 return ret;
3659 static int ocfs2_try_to_merge_extent(handle_t *handle,
3660 struct ocfs2_extent_tree *et,
3661 struct ocfs2_path *path,
3662 int split_index,
3663 struct ocfs2_extent_rec *split_rec,
3664 struct ocfs2_cached_dealloc_ctxt *dealloc,
3665 struct ocfs2_merge_ctxt *ctxt)
3667 int ret = 0;
3668 struct ocfs2_extent_list *el = path_leaf_el(path);
3669 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3671 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3673 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3675 * The merge code will need to create an empty
3676 * extent to take the place of the newly
3677 * emptied slot. Remove any pre-existing empty
3678 * extents - having more than one in a leaf is
3679 * illegal.
3681 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3682 if (ret) {
3683 mlog_errno(ret);
3684 goto out;
3686 split_index--;
3687 rec = &el->l_recs[split_index];
3690 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3692 * Left-right contig implies this.
3694 BUG_ON(!ctxt->c_split_covers_rec);
3697 * Since the leftright insert always covers the entire
3698 * extent, this call will delete the insert record
3699 * entirely, resulting in an empty extent record added to
3700 * the extent block.
3702 * Since the adding of an empty extent shifts
3703 * everything back to the right, there's no need to
3704 * update split_index here.
3706 * When the split_index is zero, we need to merge it to the
3707 * prevoius extent block. It is more efficient and easier
3708 * if we do merge_right first and merge_left later.
3710 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
3711 split_index);
3712 if (ret) {
3713 mlog_errno(ret);
3714 goto out;
3718 * We can only get this from logic error above.
3720 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3722 /* The merge left us with an empty extent, remove it. */
3723 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3724 if (ret) {
3725 mlog_errno(ret);
3726 goto out;
3729 rec = &el->l_recs[split_index];
3732 * Note that we don't pass split_rec here on purpose -
3733 * we've merged it into the rec already.
3735 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3736 dealloc, split_index);
3738 if (ret) {
3739 mlog_errno(ret);
3740 goto out;
3743 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3745 * Error from this last rotate is not critical, so
3746 * print but don't bubble it up.
3748 if (ret)
3749 mlog_errno(ret);
3750 ret = 0;
3751 } else {
3753 * Merge a record to the left or right.
3755 * 'contig_type' is relative to the existing record,
3756 * so for example, if we're "right contig", it's to
3757 * the record on the left (hence the left merge).
3759 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3760 ret = ocfs2_merge_rec_left(path, handle, et,
3761 split_rec, dealloc,
3762 split_index);
3763 if (ret) {
3764 mlog_errno(ret);
3765 goto out;
3767 } else {
3768 ret = ocfs2_merge_rec_right(path, handle,
3769 et, split_rec,
3770 split_index);
3771 if (ret) {
3772 mlog_errno(ret);
3773 goto out;
3777 if (ctxt->c_split_covers_rec) {
3779 * The merge may have left an empty extent in
3780 * our leaf. Try to rotate it away.
3782 ret = ocfs2_rotate_tree_left(handle, et, path,
3783 dealloc);
3784 if (ret)
3785 mlog_errno(ret);
3786 ret = 0;
3790 out:
3791 return ret;
3794 static void ocfs2_subtract_from_rec(struct super_block *sb,
3795 enum ocfs2_split_type split,
3796 struct ocfs2_extent_rec *rec,
3797 struct ocfs2_extent_rec *split_rec)
3799 u64 len_blocks;
3801 len_blocks = ocfs2_clusters_to_blocks(sb,
3802 le16_to_cpu(split_rec->e_leaf_clusters));
3804 if (split == SPLIT_LEFT) {
3806 * Region is on the left edge of the existing
3807 * record.
3809 le32_add_cpu(&rec->e_cpos,
3810 le16_to_cpu(split_rec->e_leaf_clusters));
3811 le64_add_cpu(&rec->e_blkno, len_blocks);
3812 le16_add_cpu(&rec->e_leaf_clusters,
3813 -le16_to_cpu(split_rec->e_leaf_clusters));
3814 } else {
3816 * Region is on the right edge of the existing
3817 * record.
3819 le16_add_cpu(&rec->e_leaf_clusters,
3820 -le16_to_cpu(split_rec->e_leaf_clusters));
3825 * Do the final bits of extent record insertion at the target leaf
3826 * list. If this leaf is part of an allocation tree, it is assumed
3827 * that the tree above has been prepared.
3829 static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3830 struct ocfs2_extent_rec *insert_rec,
3831 struct ocfs2_extent_list *el,
3832 struct ocfs2_insert_type *insert)
3834 int i = insert->ins_contig_index;
3835 unsigned int range;
3836 struct ocfs2_extent_rec *rec;
3838 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3840 if (insert->ins_split != SPLIT_NONE) {
3841 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3842 BUG_ON(i == -1);
3843 rec = &el->l_recs[i];
3844 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3845 insert->ins_split, rec,
3846 insert_rec);
3847 goto rotate;
3851 * Contiguous insert - either left or right.
3853 if (insert->ins_contig != CONTIG_NONE) {
3854 rec = &el->l_recs[i];
3855 if (insert->ins_contig == CONTIG_LEFT) {
3856 rec->e_blkno = insert_rec->e_blkno;
3857 rec->e_cpos = insert_rec->e_cpos;
3859 le16_add_cpu(&rec->e_leaf_clusters,
3860 le16_to_cpu(insert_rec->e_leaf_clusters));
3861 return;
3865 * Handle insert into an empty leaf.
3867 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3868 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3869 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3870 el->l_recs[0] = *insert_rec;
3871 el->l_next_free_rec = cpu_to_le16(1);
3872 return;
3876 * Appending insert.
3878 if (insert->ins_appending == APPEND_TAIL) {
3879 i = le16_to_cpu(el->l_next_free_rec) - 1;
3880 rec = &el->l_recs[i];
3881 range = le32_to_cpu(rec->e_cpos)
3882 + le16_to_cpu(rec->e_leaf_clusters);
3883 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3885 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3886 le16_to_cpu(el->l_count),
3887 "owner %llu, depth %u, count %u, next free %u, "
3888 "rec.cpos %u, rec.clusters %u, "
3889 "insert.cpos %u, insert.clusters %u\n",
3890 ocfs2_metadata_cache_owner(et->et_ci),
3891 le16_to_cpu(el->l_tree_depth),
3892 le16_to_cpu(el->l_count),
3893 le16_to_cpu(el->l_next_free_rec),
3894 le32_to_cpu(el->l_recs[i].e_cpos),
3895 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3896 le32_to_cpu(insert_rec->e_cpos),
3897 le16_to_cpu(insert_rec->e_leaf_clusters));
3898 i++;
3899 el->l_recs[i] = *insert_rec;
3900 le16_add_cpu(&el->l_next_free_rec, 1);
3901 return;
3904 rotate:
3906 * Ok, we have to rotate.
3908 * At this point, it is safe to assume that inserting into an
3909 * empty leaf and appending to a leaf have both been handled
3910 * above.
3912 * This leaf needs to have space, either by the empty 1st
3913 * extent record, or by virtue of an l_next_rec < l_count.
3915 ocfs2_rotate_leaf(el, insert_rec);
3918 static void ocfs2_adjust_rightmost_records(handle_t *handle,
3919 struct ocfs2_extent_tree *et,
3920 struct ocfs2_path *path,
3921 struct ocfs2_extent_rec *insert_rec)
3923 int ret, i, next_free;
3924 struct buffer_head *bh;
3925 struct ocfs2_extent_list *el;
3926 struct ocfs2_extent_rec *rec;
3929 * Update everything except the leaf block.
3931 for (i = 0; i < path->p_tree_depth; i++) {
3932 bh = path->p_node[i].bh;
3933 el = path->p_node[i].el;
3935 next_free = le16_to_cpu(el->l_next_free_rec);
3936 if (next_free == 0) {
3937 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3938 "Owner %llu has a bad extent list",
3939 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
3940 ret = -EIO;
3941 return;
3944 rec = &el->l_recs[next_free - 1];
3946 rec->e_int_clusters = insert_rec->e_cpos;
3947 le32_add_cpu(&rec->e_int_clusters,
3948 le16_to_cpu(insert_rec->e_leaf_clusters));
3949 le32_add_cpu(&rec->e_int_clusters,
3950 -le32_to_cpu(rec->e_cpos));
3952 ocfs2_journal_dirty(handle, bh);
3956 static int ocfs2_append_rec_to_path(handle_t *handle,
3957 struct ocfs2_extent_tree *et,
3958 struct ocfs2_extent_rec *insert_rec,
3959 struct ocfs2_path *right_path,
3960 struct ocfs2_path **ret_left_path)
3962 int ret, next_free;
3963 struct ocfs2_extent_list *el;
3964 struct ocfs2_path *left_path = NULL;
3966 *ret_left_path = NULL;
3969 * This shouldn't happen for non-trees. The extent rec cluster
3970 * count manipulation below only works for interior nodes.
3972 BUG_ON(right_path->p_tree_depth == 0);
3975 * If our appending insert is at the leftmost edge of a leaf,
3976 * then we might need to update the rightmost records of the
3977 * neighboring path.
3979 el = path_leaf_el(right_path);
3980 next_free = le16_to_cpu(el->l_next_free_rec);
3981 if (next_free == 0 ||
3982 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3983 u32 left_cpos;
3985 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3986 right_path, &left_cpos);
3987 if (ret) {
3988 mlog_errno(ret);
3989 goto out;
3992 mlog(0, "Append may need a left path update. cpos: %u, "
3993 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3994 left_cpos);
3997 * No need to worry if the append is already in the
3998 * leftmost leaf.
4000 if (left_cpos) {
4001 left_path = ocfs2_new_path_from_path(right_path);
4002 if (!left_path) {
4003 ret = -ENOMEM;
4004 mlog_errno(ret);
4005 goto out;
4008 ret = ocfs2_find_path(et->et_ci, left_path,
4009 left_cpos);
4010 if (ret) {
4011 mlog_errno(ret);
4012 goto out;
4016 * ocfs2_insert_path() will pass the left_path to the
4017 * journal for us.
4022 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4023 if (ret) {
4024 mlog_errno(ret);
4025 goto out;
4028 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
4030 *ret_left_path = left_path;
4031 ret = 0;
4032 out:
4033 if (ret != 0)
4034 ocfs2_free_path(left_path);
4036 return ret;
4039 static void ocfs2_split_record(struct ocfs2_extent_tree *et,
4040 struct ocfs2_path *left_path,
4041 struct ocfs2_path *right_path,
4042 struct ocfs2_extent_rec *split_rec,
4043 enum ocfs2_split_type split)
4045 int index;
4046 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4047 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4048 struct ocfs2_extent_rec *rec, *tmprec;
4050 right_el = path_leaf_el(right_path);
4051 if (left_path)
4052 left_el = path_leaf_el(left_path);
4054 el = right_el;
4055 insert_el = right_el;
4056 index = ocfs2_search_extent_list(el, cpos);
4057 if (index != -1) {
4058 if (index == 0 && left_path) {
4059 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4062 * This typically means that the record
4063 * started in the left path but moved to the
4064 * right as a result of rotation. We either
4065 * move the existing record to the left, or we
4066 * do the later insert there.
4068 * In this case, the left path should always
4069 * exist as the rotate code will have passed
4070 * it back for a post-insert update.
4073 if (split == SPLIT_LEFT) {
4075 * It's a left split. Since we know
4076 * that the rotate code gave us an
4077 * empty extent in the left path, we
4078 * can just do the insert there.
4080 insert_el = left_el;
4081 } else {
4083 * Right split - we have to move the
4084 * existing record over to the left
4085 * leaf. The insert will be into the
4086 * newly created empty extent in the
4087 * right leaf.
4089 tmprec = &right_el->l_recs[index];
4090 ocfs2_rotate_leaf(left_el, tmprec);
4091 el = left_el;
4093 memset(tmprec, 0, sizeof(*tmprec));
4094 index = ocfs2_search_extent_list(left_el, cpos);
4095 BUG_ON(index == -1);
4098 } else {
4099 BUG_ON(!left_path);
4100 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4102 * Left path is easy - we can just allow the insert to
4103 * happen.
4105 el = left_el;
4106 insert_el = left_el;
4107 index = ocfs2_search_extent_list(el, cpos);
4108 BUG_ON(index == -1);
4111 rec = &el->l_recs[index];
4112 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4113 split, rec, split_rec);
4114 ocfs2_rotate_leaf(insert_el, split_rec);
4118 * This function only does inserts on an allocation b-tree. For tree
4119 * depth = 0, ocfs2_insert_at_leaf() is called directly.
4121 * right_path is the path we want to do the actual insert
4122 * in. left_path should only be passed in if we need to update that
4123 * portion of the tree after an edge insert.
4125 static int ocfs2_insert_path(handle_t *handle,
4126 struct ocfs2_extent_tree *et,
4127 struct ocfs2_path *left_path,
4128 struct ocfs2_path *right_path,
4129 struct ocfs2_extent_rec *insert_rec,
4130 struct ocfs2_insert_type *insert)
4132 int ret, subtree_index;
4133 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
4135 if (left_path) {
4137 * There's a chance that left_path got passed back to
4138 * us without being accounted for in the
4139 * journal. Extend our transaction here to be sure we
4140 * can change those blocks.
4142 ret = ocfs2_extend_trans(handle, left_path->p_tree_depth);
4143 if (ret < 0) {
4144 mlog_errno(ret);
4145 goto out;
4148 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
4149 if (ret < 0) {
4150 mlog_errno(ret);
4151 goto out;
4156 * Pass both paths to the journal. The majority of inserts
4157 * will be touching all components anyway.
4159 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4160 if (ret < 0) {
4161 mlog_errno(ret);
4162 goto out;
4165 if (insert->ins_split != SPLIT_NONE) {
4167 * We could call ocfs2_insert_at_leaf() for some types
4168 * of splits, but it's easier to just let one separate
4169 * function sort it all out.
4171 ocfs2_split_record(et, left_path, right_path,
4172 insert_rec, insert->ins_split);
4175 * Split might have modified either leaf and we don't
4176 * have a guarantee that the later edge insert will
4177 * dirty this for us.
4179 if (left_path)
4180 ocfs2_journal_dirty(handle,
4181 path_leaf_bh(left_path));
4182 } else
4183 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4184 insert);
4186 ocfs2_journal_dirty(handle, leaf_bh);
4188 if (left_path) {
4190 * The rotate code has indicated that we need to fix
4191 * up portions of the tree after the insert.
4193 * XXX: Should we extend the transaction here?
4195 subtree_index = ocfs2_find_subtree_root(et, left_path,
4196 right_path);
4197 ocfs2_complete_edge_insert(handle, left_path, right_path,
4198 subtree_index);
4201 ret = 0;
4202 out:
4203 return ret;
4206 static int ocfs2_do_insert_extent(handle_t *handle,
4207 struct ocfs2_extent_tree *et,
4208 struct ocfs2_extent_rec *insert_rec,
4209 struct ocfs2_insert_type *type)
4211 int ret, rotate = 0;
4212 u32 cpos;
4213 struct ocfs2_path *right_path = NULL;
4214 struct ocfs2_path *left_path = NULL;
4215 struct ocfs2_extent_list *el;
4217 el = et->et_root_el;
4219 ret = ocfs2_et_root_journal_access(handle, et,
4220 OCFS2_JOURNAL_ACCESS_WRITE);
4221 if (ret) {
4222 mlog_errno(ret);
4223 goto out;
4226 if (le16_to_cpu(el->l_tree_depth) == 0) {
4227 ocfs2_insert_at_leaf(et, insert_rec, el, type);
4228 goto out_update_clusters;
4231 right_path = ocfs2_new_path_from_et(et);
4232 if (!right_path) {
4233 ret = -ENOMEM;
4234 mlog_errno(ret);
4235 goto out;
4239 * Determine the path to start with. Rotations need the
4240 * rightmost path, everything else can go directly to the
4241 * target leaf.
4243 cpos = le32_to_cpu(insert_rec->e_cpos);
4244 if (type->ins_appending == APPEND_NONE &&
4245 type->ins_contig == CONTIG_NONE) {
4246 rotate = 1;
4247 cpos = UINT_MAX;
4250 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
4251 if (ret) {
4252 mlog_errno(ret);
4253 goto out;
4257 * Rotations and appends need special treatment - they modify
4258 * parts of the tree's above them.
4260 * Both might pass back a path immediate to the left of the
4261 * one being inserted to. This will be cause
4262 * ocfs2_insert_path() to modify the rightmost records of
4263 * left_path to account for an edge insert.
4265 * XXX: When modifying this code, keep in mind that an insert
4266 * can wind up skipping both of these two special cases...
4268 if (rotate) {
4269 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
4270 le32_to_cpu(insert_rec->e_cpos),
4271 right_path, &left_path);
4272 if (ret) {
4273 mlog_errno(ret);
4274 goto out;
4278 * ocfs2_rotate_tree_right() might have extended the
4279 * transaction without re-journaling our tree root.
4281 ret = ocfs2_et_root_journal_access(handle, et,
4282 OCFS2_JOURNAL_ACCESS_WRITE);
4283 if (ret) {
4284 mlog_errno(ret);
4285 goto out;
4287 } else if (type->ins_appending == APPEND_TAIL
4288 && type->ins_contig != CONTIG_LEFT) {
4289 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
4290 right_path, &left_path);
4291 if (ret) {
4292 mlog_errno(ret);
4293 goto out;
4297 ret = ocfs2_insert_path(handle, et, left_path, right_path,
4298 insert_rec, type);
4299 if (ret) {
4300 mlog_errno(ret);
4301 goto out;
4304 out_update_clusters:
4305 if (type->ins_split == SPLIT_NONE)
4306 ocfs2_et_update_clusters(et,
4307 le16_to_cpu(insert_rec->e_leaf_clusters));
4309 ocfs2_journal_dirty(handle, et->et_root_bh);
4311 out:
4312 ocfs2_free_path(left_path);
4313 ocfs2_free_path(right_path);
4315 return ret;
4318 static enum ocfs2_contig_type
4319 ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4320 struct ocfs2_path *path,
4321 struct ocfs2_extent_list *el, int index,
4322 struct ocfs2_extent_rec *split_rec)
4324 int status;
4325 enum ocfs2_contig_type ret = CONTIG_NONE;
4326 u32 left_cpos, right_cpos;
4327 struct ocfs2_extent_rec *rec = NULL;
4328 struct ocfs2_extent_list *new_el;
4329 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4330 struct buffer_head *bh;
4331 struct ocfs2_extent_block *eb;
4332 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
4334 if (index > 0) {
4335 rec = &el->l_recs[index - 1];
4336 } else if (path->p_tree_depth > 0) {
4337 status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
4338 if (status)
4339 goto out;
4341 if (left_cpos != 0) {
4342 left_path = ocfs2_new_path_from_path(path);
4343 if (!left_path)
4344 goto out;
4346 status = ocfs2_find_path(et->et_ci, left_path,
4347 left_cpos);
4348 if (status)
4349 goto out;
4351 new_el = path_leaf_el(left_path);
4353 if (le16_to_cpu(new_el->l_next_free_rec) !=
4354 le16_to_cpu(new_el->l_count)) {
4355 bh = path_leaf_bh(left_path);
4356 eb = (struct ocfs2_extent_block *)bh->b_data;
4357 ocfs2_error(sb,
4358 "Extent block #%llu has an "
4359 "invalid l_next_free_rec of "
4360 "%d. It should have "
4361 "matched the l_count of %d",
4362 (unsigned long long)le64_to_cpu(eb->h_blkno),
4363 le16_to_cpu(new_el->l_next_free_rec),
4364 le16_to_cpu(new_el->l_count));
4365 status = -EINVAL;
4366 goto out;
4368 rec = &new_el->l_recs[
4369 le16_to_cpu(new_el->l_next_free_rec) - 1];
4374 * We're careful to check for an empty extent record here -
4375 * the merge code will know what to do if it sees one.
4377 if (rec) {
4378 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4379 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4380 ret = CONTIG_RIGHT;
4381 } else {
4382 ret = ocfs2_et_extent_contig(et, rec, split_rec);
4386 rec = NULL;
4387 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4388 rec = &el->l_recs[index + 1];
4389 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4390 path->p_tree_depth > 0) {
4391 status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
4392 if (status)
4393 goto out;
4395 if (right_cpos == 0)
4396 goto out;
4398 right_path = ocfs2_new_path_from_path(path);
4399 if (!right_path)
4400 goto out;
4402 status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
4403 if (status)
4404 goto out;
4406 new_el = path_leaf_el(right_path);
4407 rec = &new_el->l_recs[0];
4408 if (ocfs2_is_empty_extent(rec)) {
4409 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4410 bh = path_leaf_bh(right_path);
4411 eb = (struct ocfs2_extent_block *)bh->b_data;
4412 ocfs2_error(sb,
4413 "Extent block #%llu has an "
4414 "invalid l_next_free_rec of %d",
4415 (unsigned long long)le64_to_cpu(eb->h_blkno),
4416 le16_to_cpu(new_el->l_next_free_rec));
4417 status = -EINVAL;
4418 goto out;
4420 rec = &new_el->l_recs[1];
4424 if (rec) {
4425 enum ocfs2_contig_type contig_type;
4427 contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
4429 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4430 ret = CONTIG_LEFTRIGHT;
4431 else if (ret == CONTIG_NONE)
4432 ret = contig_type;
4435 out:
4436 if (left_path)
4437 ocfs2_free_path(left_path);
4438 if (right_path)
4439 ocfs2_free_path(right_path);
4441 return ret;
4444 static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
4445 struct ocfs2_insert_type *insert,
4446 struct ocfs2_extent_list *el,
4447 struct ocfs2_extent_rec *insert_rec)
4449 int i;
4450 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4452 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4454 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4455 contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4456 insert_rec);
4457 if (contig_type != CONTIG_NONE) {
4458 insert->ins_contig_index = i;
4459 break;
4462 insert->ins_contig = contig_type;
4464 if (insert->ins_contig != CONTIG_NONE) {
4465 struct ocfs2_extent_rec *rec =
4466 &el->l_recs[insert->ins_contig_index];
4467 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4468 le16_to_cpu(insert_rec->e_leaf_clusters);
4471 * Caller might want us to limit the size of extents, don't
4472 * calculate contiguousness if we might exceed that limit.
4474 if (et->et_max_leaf_clusters &&
4475 (len > et->et_max_leaf_clusters))
4476 insert->ins_contig = CONTIG_NONE;
4481 * This should only be called against the righmost leaf extent list.
4483 * ocfs2_figure_appending_type() will figure out whether we'll have to
4484 * insert at the tail of the rightmost leaf.
4486 * This should also work against the root extent list for tree's with 0
4487 * depth. If we consider the root extent list to be the rightmost leaf node
4488 * then the logic here makes sense.
4490 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4491 struct ocfs2_extent_list *el,
4492 struct ocfs2_extent_rec *insert_rec)
4494 int i;
4495 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4496 struct ocfs2_extent_rec *rec;
4498 insert->ins_appending = APPEND_NONE;
4500 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4502 if (!el->l_next_free_rec)
4503 goto set_tail_append;
4505 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4506 /* Were all records empty? */
4507 if (le16_to_cpu(el->l_next_free_rec) == 1)
4508 goto set_tail_append;
4511 i = le16_to_cpu(el->l_next_free_rec) - 1;
4512 rec = &el->l_recs[i];
4514 if (cpos >=
4515 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4516 goto set_tail_append;
4518 return;
4520 set_tail_append:
4521 insert->ins_appending = APPEND_TAIL;
4525 * Helper function called at the begining of an insert.
4527 * This computes a few things that are commonly used in the process of
4528 * inserting into the btree:
4529 * - Whether the new extent is contiguous with an existing one.
4530 * - The current tree depth.
4531 * - Whether the insert is an appending one.
4532 * - The total # of free records in the tree.
4534 * All of the information is stored on the ocfs2_insert_type
4535 * structure.
4537 static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
4538 struct buffer_head **last_eb_bh,
4539 struct ocfs2_extent_rec *insert_rec,
4540 int *free_records,
4541 struct ocfs2_insert_type *insert)
4543 int ret;
4544 struct ocfs2_extent_block *eb;
4545 struct ocfs2_extent_list *el;
4546 struct ocfs2_path *path = NULL;
4547 struct buffer_head *bh = NULL;
4549 insert->ins_split = SPLIT_NONE;
4551 el = et->et_root_el;
4552 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4554 if (el->l_tree_depth) {
4556 * If we have tree depth, we read in the
4557 * rightmost extent block ahead of time as
4558 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4559 * may want it later.
4561 ret = ocfs2_read_extent_block(et->et_ci,
4562 ocfs2_et_get_last_eb_blk(et),
4563 &bh);
4564 if (ret) {
4565 mlog_exit(ret);
4566 goto out;
4568 eb = (struct ocfs2_extent_block *) bh->b_data;
4569 el = &eb->h_list;
4573 * Unless we have a contiguous insert, we'll need to know if
4574 * there is room left in our allocation tree for another
4575 * extent record.
4577 * XXX: This test is simplistic, we can search for empty
4578 * extent records too.
4580 *free_records = le16_to_cpu(el->l_count) -
4581 le16_to_cpu(el->l_next_free_rec);
4583 if (!insert->ins_tree_depth) {
4584 ocfs2_figure_contig_type(et, insert, el, insert_rec);
4585 ocfs2_figure_appending_type(insert, el, insert_rec);
4586 return 0;
4589 path = ocfs2_new_path_from_et(et);
4590 if (!path) {
4591 ret = -ENOMEM;
4592 mlog_errno(ret);
4593 goto out;
4597 * In the case that we're inserting past what the tree
4598 * currently accounts for, ocfs2_find_path() will return for
4599 * us the rightmost tree path. This is accounted for below in
4600 * the appending code.
4602 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
4603 if (ret) {
4604 mlog_errno(ret);
4605 goto out;
4608 el = path_leaf_el(path);
4611 * Now that we have the path, there's two things we want to determine:
4612 * 1) Contiguousness (also set contig_index if this is so)
4614 * 2) Are we doing an append? We can trivially break this up
4615 * into two types of appends: simple record append, or a
4616 * rotate inside the tail leaf.
4618 ocfs2_figure_contig_type(et, insert, el, insert_rec);
4621 * The insert code isn't quite ready to deal with all cases of
4622 * left contiguousness. Specifically, if it's an insert into
4623 * the 1st record in a leaf, it will require the adjustment of
4624 * cluster count on the last record of the path directly to it's
4625 * left. For now, just catch that case and fool the layers
4626 * above us. This works just fine for tree_depth == 0, which
4627 * is why we allow that above.
4629 if (insert->ins_contig == CONTIG_LEFT &&
4630 insert->ins_contig_index == 0)
4631 insert->ins_contig = CONTIG_NONE;
4634 * Ok, so we can simply compare against last_eb to figure out
4635 * whether the path doesn't exist. This will only happen in
4636 * the case that we're doing a tail append, so maybe we can
4637 * take advantage of that information somehow.
4639 if (ocfs2_et_get_last_eb_blk(et) ==
4640 path_leaf_bh(path)->b_blocknr) {
4642 * Ok, ocfs2_find_path() returned us the rightmost
4643 * tree path. This might be an appending insert. There are
4644 * two cases:
4645 * 1) We're doing a true append at the tail:
4646 * -This might even be off the end of the leaf
4647 * 2) We're "appending" by rotating in the tail
4649 ocfs2_figure_appending_type(insert, el, insert_rec);
4652 out:
4653 ocfs2_free_path(path);
4655 if (ret == 0)
4656 *last_eb_bh = bh;
4657 else
4658 brelse(bh);
4659 return ret;
4663 * Insert an extent into a btree.
4665 * The caller needs to update the owning btree's cluster count.
4667 int ocfs2_insert_extent(handle_t *handle,
4668 struct ocfs2_extent_tree *et,
4669 u32 cpos,
4670 u64 start_blk,
4671 u32 new_clusters,
4672 u8 flags,
4673 struct ocfs2_alloc_context *meta_ac)
4675 int status;
4676 int uninitialized_var(free_records);
4677 struct buffer_head *last_eb_bh = NULL;
4678 struct ocfs2_insert_type insert = {0, };
4679 struct ocfs2_extent_rec rec;
4681 mlog(0, "add %u clusters at position %u to owner %llu\n",
4682 new_clusters, cpos,
4683 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
4685 memset(&rec, 0, sizeof(rec));
4686 rec.e_cpos = cpu_to_le32(cpos);
4687 rec.e_blkno = cpu_to_le64(start_blk);
4688 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4689 rec.e_flags = flags;
4690 status = ocfs2_et_insert_check(et, &rec);
4691 if (status) {
4692 mlog_errno(status);
4693 goto bail;
4696 status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
4697 &free_records, &insert);
4698 if (status < 0) {
4699 mlog_errno(status);
4700 goto bail;
4703 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4704 "Insert.contig_index: %d, Insert.free_records: %d, "
4705 "Insert.tree_depth: %d\n",
4706 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4707 free_records, insert.ins_tree_depth);
4709 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4710 status = ocfs2_grow_tree(handle, et,
4711 &insert.ins_tree_depth, &last_eb_bh,
4712 meta_ac);
4713 if (status) {
4714 mlog_errno(status);
4715 goto bail;
4719 /* Finally, we can add clusters. This might rotate the tree for us. */
4720 status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
4721 if (status < 0)
4722 mlog_errno(status);
4723 else
4724 ocfs2_et_extent_map_insert(et, &rec);
4726 bail:
4727 brelse(last_eb_bh);
4729 mlog_exit(status);
4730 return status;
4734 * Allcate and add clusters into the extent b-tree.
4735 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4736 * The extent b-tree's root is specified by et, and
4737 * it is not limited to the file storage. Any extent tree can use this
4738 * function if it implements the proper ocfs2_extent_tree.
4740 int ocfs2_add_clusters_in_btree(handle_t *handle,
4741 struct ocfs2_extent_tree *et,
4742 u32 *logical_offset,
4743 u32 clusters_to_add,
4744 int mark_unwritten,
4745 struct ocfs2_alloc_context *data_ac,
4746 struct ocfs2_alloc_context *meta_ac,
4747 enum ocfs2_alloc_restarted *reason_ret)
4749 int status = 0;
4750 int free_extents;
4751 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4752 u32 bit_off, num_bits;
4753 u64 block;
4754 u8 flags = 0;
4755 struct ocfs2_super *osb =
4756 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
4758 BUG_ON(!clusters_to_add);
4760 if (mark_unwritten)
4761 flags = OCFS2_EXT_UNWRITTEN;
4763 free_extents = ocfs2_num_free_extents(osb, et);
4764 if (free_extents < 0) {
4765 status = free_extents;
4766 mlog_errno(status);
4767 goto leave;
4770 /* there are two cases which could cause us to EAGAIN in the
4771 * we-need-more-metadata case:
4772 * 1) we haven't reserved *any*
4773 * 2) we are so fragmented, we've needed to add metadata too
4774 * many times. */
4775 if (!free_extents && !meta_ac) {
4776 mlog(0, "we haven't reserved any metadata!\n");
4777 status = -EAGAIN;
4778 reason = RESTART_META;
4779 goto leave;
4780 } else if ((!free_extents)
4781 && (ocfs2_alloc_context_bits_left(meta_ac)
4782 < ocfs2_extend_meta_needed(et->et_root_el))) {
4783 mlog(0, "filesystem is really fragmented...\n");
4784 status = -EAGAIN;
4785 reason = RESTART_META;
4786 goto leave;
4789 status = __ocfs2_claim_clusters(handle, data_ac, 1,
4790 clusters_to_add, &bit_off, &num_bits);
4791 if (status < 0) {
4792 if (status != -ENOSPC)
4793 mlog_errno(status);
4794 goto leave;
4797 BUG_ON(num_bits > clusters_to_add);
4799 /* reserve our write early -- insert_extent may update the tree root */
4800 status = ocfs2_et_root_journal_access(handle, et,
4801 OCFS2_JOURNAL_ACCESS_WRITE);
4802 if (status < 0) {
4803 mlog_errno(status);
4804 goto leave;
4807 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4808 mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
4809 num_bits, bit_off,
4810 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
4811 status = ocfs2_insert_extent(handle, et, *logical_offset, block,
4812 num_bits, flags, meta_ac);
4813 if (status < 0) {
4814 mlog_errno(status);
4815 goto leave;
4818 ocfs2_journal_dirty(handle, et->et_root_bh);
4820 clusters_to_add -= num_bits;
4821 *logical_offset += num_bits;
4823 if (clusters_to_add) {
4824 mlog(0, "need to alloc once more, wanted = %u\n",
4825 clusters_to_add);
4826 status = -EAGAIN;
4827 reason = RESTART_TRANS;
4830 leave:
4831 mlog_exit(status);
4832 if (reason_ret)
4833 *reason_ret = reason;
4834 return status;
4837 static void ocfs2_make_right_split_rec(struct super_block *sb,
4838 struct ocfs2_extent_rec *split_rec,
4839 u32 cpos,
4840 struct ocfs2_extent_rec *rec)
4842 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4843 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4845 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4847 split_rec->e_cpos = cpu_to_le32(cpos);
4848 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4850 split_rec->e_blkno = rec->e_blkno;
4851 le64_add_cpu(&split_rec->e_blkno,
4852 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4854 split_rec->e_flags = rec->e_flags;
4857 static int ocfs2_split_and_insert(handle_t *handle,
4858 struct ocfs2_extent_tree *et,
4859 struct ocfs2_path *path,
4860 struct buffer_head **last_eb_bh,
4861 int split_index,
4862 struct ocfs2_extent_rec *orig_split_rec,
4863 struct ocfs2_alloc_context *meta_ac)
4865 int ret = 0, depth;
4866 unsigned int insert_range, rec_range, do_leftright = 0;
4867 struct ocfs2_extent_rec tmprec;
4868 struct ocfs2_extent_list *rightmost_el;
4869 struct ocfs2_extent_rec rec;
4870 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4871 struct ocfs2_insert_type insert;
4872 struct ocfs2_extent_block *eb;
4874 leftright:
4876 * Store a copy of the record on the stack - it might move
4877 * around as the tree is manipulated below.
4879 rec = path_leaf_el(path)->l_recs[split_index];
4881 rightmost_el = et->et_root_el;
4883 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4884 if (depth) {
4885 BUG_ON(!(*last_eb_bh));
4886 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4887 rightmost_el = &eb->h_list;
4890 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4891 le16_to_cpu(rightmost_el->l_count)) {
4892 ret = ocfs2_grow_tree(handle, et,
4893 &depth, last_eb_bh, meta_ac);
4894 if (ret) {
4895 mlog_errno(ret);
4896 goto out;
4900 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4901 insert.ins_appending = APPEND_NONE;
4902 insert.ins_contig = CONTIG_NONE;
4903 insert.ins_tree_depth = depth;
4905 insert_range = le32_to_cpu(split_rec.e_cpos) +
4906 le16_to_cpu(split_rec.e_leaf_clusters);
4907 rec_range = le32_to_cpu(rec.e_cpos) +
4908 le16_to_cpu(rec.e_leaf_clusters);
4910 if (split_rec.e_cpos == rec.e_cpos) {
4911 insert.ins_split = SPLIT_LEFT;
4912 } else if (insert_range == rec_range) {
4913 insert.ins_split = SPLIT_RIGHT;
4914 } else {
4916 * Left/right split. We fake this as a right split
4917 * first and then make a second pass as a left split.
4919 insert.ins_split = SPLIT_RIGHT;
4921 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4922 &tmprec, insert_range, &rec);
4924 split_rec = tmprec;
4926 BUG_ON(do_leftright);
4927 do_leftright = 1;
4930 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
4931 if (ret) {
4932 mlog_errno(ret);
4933 goto out;
4936 if (do_leftright == 1) {
4937 u32 cpos;
4938 struct ocfs2_extent_list *el;
4940 do_leftright++;
4941 split_rec = *orig_split_rec;
4943 ocfs2_reinit_path(path, 1);
4945 cpos = le32_to_cpu(split_rec.e_cpos);
4946 ret = ocfs2_find_path(et->et_ci, path, cpos);
4947 if (ret) {
4948 mlog_errno(ret);
4949 goto out;
4952 el = path_leaf_el(path);
4953 split_index = ocfs2_search_extent_list(el, cpos);
4954 goto leftright;
4956 out:
4958 return ret;
4961 static int ocfs2_replace_extent_rec(handle_t *handle,
4962 struct ocfs2_extent_tree *et,
4963 struct ocfs2_path *path,
4964 struct ocfs2_extent_list *el,
4965 int split_index,
4966 struct ocfs2_extent_rec *split_rec)
4968 int ret;
4970 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
4971 path_num_items(path) - 1);
4972 if (ret) {
4973 mlog_errno(ret);
4974 goto out;
4977 el->l_recs[split_index] = *split_rec;
4979 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4980 out:
4981 return ret;
4985 * Split part or all of the extent record at split_index in the leaf
4986 * pointed to by path. Merge with the contiguous extent record if needed.
4988 * Care is taken to handle contiguousness so as to not grow the tree.
4990 * meta_ac is not strictly necessary - we only truly need it if growth
4991 * of the tree is required. All other cases will degrade into a less
4992 * optimal tree layout.
4994 * last_eb_bh should be the rightmost leaf block for any extent
4995 * btree. Since a split may grow the tree or a merge might shrink it,
4996 * the caller cannot trust the contents of that buffer after this call.
4998 * This code is optimized for readability - several passes might be
4999 * made over certain portions of the tree. All of those blocks will
5000 * have been brought into cache (and pinned via the journal), so the
5001 * extra overhead is not expressed in terms of disk reads.
5003 int ocfs2_split_extent(handle_t *handle,
5004 struct ocfs2_extent_tree *et,
5005 struct ocfs2_path *path,
5006 int split_index,
5007 struct ocfs2_extent_rec *split_rec,
5008 struct ocfs2_alloc_context *meta_ac,
5009 struct ocfs2_cached_dealloc_ctxt *dealloc)
5011 int ret = 0;
5012 struct ocfs2_extent_list *el = path_leaf_el(path);
5013 struct buffer_head *last_eb_bh = NULL;
5014 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5015 struct ocfs2_merge_ctxt ctxt;
5016 struct ocfs2_extent_list *rightmost_el;
5018 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5019 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5020 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5021 ret = -EIO;
5022 mlog_errno(ret);
5023 goto out;
5026 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
5027 split_index,
5028 split_rec);
5031 * The core merge / split code wants to know how much room is
5032 * left in this allocation tree, so we pass the
5033 * rightmost extent list.
5035 if (path->p_tree_depth) {
5036 struct ocfs2_extent_block *eb;
5038 ret = ocfs2_read_extent_block(et->et_ci,
5039 ocfs2_et_get_last_eb_blk(et),
5040 &last_eb_bh);
5041 if (ret) {
5042 mlog_exit(ret);
5043 goto out;
5046 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5047 rightmost_el = &eb->h_list;
5048 } else
5049 rightmost_el = path_root_el(path);
5051 if (rec->e_cpos == split_rec->e_cpos &&
5052 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5053 ctxt.c_split_covers_rec = 1;
5054 else
5055 ctxt.c_split_covers_rec = 0;
5057 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5059 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5060 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5061 ctxt.c_split_covers_rec);
5063 if (ctxt.c_contig_type == CONTIG_NONE) {
5064 if (ctxt.c_split_covers_rec)
5065 ret = ocfs2_replace_extent_rec(handle, et, path, el,
5066 split_index, split_rec);
5067 else
5068 ret = ocfs2_split_and_insert(handle, et, path,
5069 &last_eb_bh, split_index,
5070 split_rec, meta_ac);
5071 if (ret)
5072 mlog_errno(ret);
5073 } else {
5074 ret = ocfs2_try_to_merge_extent(handle, et, path,
5075 split_index, split_rec,
5076 dealloc, &ctxt);
5077 if (ret)
5078 mlog_errno(ret);
5081 out:
5082 brelse(last_eb_bh);
5083 return ret;
5087 * Change the flags of the already-existing extent at cpos for len clusters.
5089 * new_flags: the flags we want to set.
5090 * clear_flags: the flags we want to clear.
5091 * phys: the new physical offset we want this new extent starts from.
5093 * If the existing extent is larger than the request, initiate a
5094 * split. An attempt will be made at merging with adjacent extents.
5096 * The caller is responsible for passing down meta_ac if we'll need it.
5098 int ocfs2_change_extent_flag(handle_t *handle,
5099 struct ocfs2_extent_tree *et,
5100 u32 cpos, u32 len, u32 phys,
5101 struct ocfs2_alloc_context *meta_ac,
5102 struct ocfs2_cached_dealloc_ctxt *dealloc,
5103 int new_flags, int clear_flags)
5105 int ret, index;
5106 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5107 u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
5108 struct ocfs2_extent_rec split_rec;
5109 struct ocfs2_path *left_path = NULL;
5110 struct ocfs2_extent_list *el;
5111 struct ocfs2_extent_rec *rec;
5113 left_path = ocfs2_new_path_from_et(et);
5114 if (!left_path) {
5115 ret = -ENOMEM;
5116 mlog_errno(ret);
5117 goto out;
5120 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
5121 if (ret) {
5122 mlog_errno(ret);
5123 goto out;
5125 el = path_leaf_el(left_path);
5127 index = ocfs2_search_extent_list(el, cpos);
5128 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5129 ocfs2_error(sb,
5130 "Owner %llu has an extent at cpos %u which can no "
5131 "longer be found.\n",
5132 (unsigned long long)
5133 ocfs2_metadata_cache_owner(et->et_ci), cpos);
5134 ret = -EROFS;
5135 goto out;
5138 ret = -EIO;
5139 rec = &el->l_recs[index];
5140 if (new_flags && (rec->e_flags & new_flags)) {
5141 mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
5142 "extent that already had them",
5143 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5144 new_flags);
5145 goto out;
5148 if (clear_flags && !(rec->e_flags & clear_flags)) {
5149 mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
5150 "extent that didn't have them",
5151 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5152 clear_flags);
5153 goto out;
5156 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5157 split_rec.e_cpos = cpu_to_le32(cpos);
5158 split_rec.e_leaf_clusters = cpu_to_le16(len);
5159 split_rec.e_blkno = cpu_to_le64(start_blkno);
5160 split_rec.e_flags = rec->e_flags;
5161 if (new_flags)
5162 split_rec.e_flags |= new_flags;
5163 if (clear_flags)
5164 split_rec.e_flags &= ~clear_flags;
5166 ret = ocfs2_split_extent(handle, et, left_path,
5167 index, &split_rec, meta_ac,
5168 dealloc);
5169 if (ret)
5170 mlog_errno(ret);
5172 out:
5173 ocfs2_free_path(left_path);
5174 return ret;
5179 * Mark the already-existing extent at cpos as written for len clusters.
5180 * This removes the unwritten extent flag.
5182 * If the existing extent is larger than the request, initiate a
5183 * split. An attempt will be made at merging with adjacent extents.
5185 * The caller is responsible for passing down meta_ac if we'll need it.
5187 int ocfs2_mark_extent_written(struct inode *inode,
5188 struct ocfs2_extent_tree *et,
5189 handle_t *handle, u32 cpos, u32 len, u32 phys,
5190 struct ocfs2_alloc_context *meta_ac,
5191 struct ocfs2_cached_dealloc_ctxt *dealloc)
5193 int ret;
5195 mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
5196 inode->i_ino, cpos, len, phys);
5198 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5199 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5200 "that are being written to, but the feature bit "
5201 "is not set in the super block.",
5202 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5203 ret = -EROFS;
5204 goto out;
5208 * XXX: This should be fixed up so that we just re-insert the
5209 * next extent records.
5211 ocfs2_et_extent_map_truncate(et, 0);
5213 ret = ocfs2_change_extent_flag(handle, et, cpos,
5214 len, phys, meta_ac, dealloc,
5215 0, OCFS2_EXT_UNWRITTEN);
5216 if (ret)
5217 mlog_errno(ret);
5219 out:
5220 return ret;
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;
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;
5245 if (depth > 0) {
5246 ret = ocfs2_read_extent_block(et->et_ci,
5247 ocfs2_et_get_last_eb_blk(et),
5248 &last_eb_bh);
5249 if (ret < 0) {
5250 mlog_errno(ret);
5251 goto out;
5254 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5255 rightmost_el = &eb->h_list;
5256 } else
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);
5262 if (ret) {
5263 mlog_errno(ret);
5264 goto out;
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,
5270 meta_ac);
5271 if (ret) {
5272 mlog_errno(ret);
5273 goto out;
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);
5284 if (ret)
5285 mlog_errno(ret);
5287 out:
5288 brelse(last_eb_bh);
5289 return ret;
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,
5296 u32 cpos, u32 len)
5298 int ret;
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);
5309 if (ret) {
5310 mlog_errno(ret);
5311 goto out;
5314 index--;
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
5323 * will be required.
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);
5347 if (ret) {
5348 mlog_errno(ret);
5349 goto out;
5352 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5353 left_path = ocfs2_new_path_from_path(path);
5354 if (!left_path) {
5355 ret = -ENOMEM;
5356 mlog_errno(ret);
5357 goto out;
5360 ret = ocfs2_find_path(et->et_ci, left_path,
5361 left_cpos);
5362 if (ret) {
5363 mlog_errno(ret);
5364 goto out;
5369 ret = ocfs2_extend_rotate_transaction(handle, 0,
5370 handle->h_buffer_credits,
5371 path);
5372 if (ret) {
5373 mlog_errno(ret);
5374 goto out;
5377 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
5378 if (ret) {
5379 mlog_errno(ret);
5380 goto out;
5383 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
5384 if (ret) {
5385 mlog_errno(ret);
5386 goto out;
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) {
5393 int next_free;
5395 memset(rec, 0, sizeof(*rec));
5396 ocfs2_cleanup_merge(el, index);
5397 wants_rotate = 1;
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,
5407 rec);
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);
5419 } else {
5420 /* Caller should have trapped this. */
5421 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5422 "(%u, %u)\n",
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);
5426 BUG();
5429 if (left_path) {
5430 int subtree_index;
5432 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
5433 ocfs2_complete_edge_insert(handle, left_path, path,
5434 subtree_index);
5437 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5439 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5440 if (ret) {
5441 mlog_errno(ret);
5442 goto out;
5445 out:
5446 ocfs2_free_path(left_path);
5447 return ret;
5450 int ocfs2_remove_extent(handle_t *handle,
5451 struct ocfs2_extent_tree *et,
5452 u32 cpos, u32 len,
5453 struct ocfs2_alloc_context *meta_ac,
5454 struct ocfs2_cached_dealloc_ctxt *dealloc)
5456 int ret, index;
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
5464 * affects us?
5466 ocfs2_et_extent_map_truncate(et, 0);
5468 path = ocfs2_new_path_from_et(et);
5469 if (!path) {
5470 ret = -ENOMEM;
5471 mlog_errno(ret);
5472 goto out;
5475 ret = ocfs2_find_path(et->et_ci, path, cpos);
5476 if (ret) {
5477 mlog_errno(ret);
5478 goto out;
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),
5488 cpos);
5489 ret = -EROFS;
5490 goto out;
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
5500 * fill the hole.
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
5504 * extent block.
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),
5518 cpos, len, index,
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,
5523 cpos, len);
5524 if (ret) {
5525 mlog_errno(ret);
5526 goto out;
5528 } else {
5529 ret = ocfs2_split_tree(handle, et, path, index,
5530 trunc_range, meta_ac);
5531 if (ret) {
5532 mlog_errno(ret);
5533 goto out;
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);
5543 if (ret) {
5544 mlog_errno(ret);
5545 goto out;
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),
5554 cpos);
5555 ret = -EROFS;
5556 goto out;
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));
5573 ret = -EROFS;
5574 goto out;
5577 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5578 cpos, len);
5579 if (ret) {
5580 mlog_errno(ret);
5581 goto out;
5585 out:
5586 ocfs2_free_path(path);
5587 return ret;
5591 * ocfs2_reserve_blocks_for_rec_trunc() would look basically the
5592 * same as ocfs2_lock_alloctors(), except for it accepts a blocks
5593 * number to reserve some extra blocks, and it only handles meta
5594 * data allocations.
5596 * Currently, only ocfs2_remove_btree_range() uses it for truncating
5597 * and punching holes.
5599 static int ocfs2_reserve_blocks_for_rec_trunc(struct inode *inode,
5600 struct ocfs2_extent_tree *et,
5601 u32 extents_to_split,
5602 struct ocfs2_alloc_context **ac,
5603 int extra_blocks)
5605 int ret = 0, num_free_extents;
5606 unsigned int max_recs_needed = 2 * extents_to_split;
5607 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5609 *ac = NULL;
5611 num_free_extents = ocfs2_num_free_extents(osb, et);
5612 if (num_free_extents < 0) {
5613 ret = num_free_extents;
5614 mlog_errno(ret);
5615 goto out;
5618 if (!num_free_extents ||
5619 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed))
5620 extra_blocks += ocfs2_extend_meta_needed(et->et_root_el);
5622 if (extra_blocks) {
5623 ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, ac);
5624 if (ret < 0) {
5625 if (ret != -ENOSPC)
5626 mlog_errno(ret);
5627 goto out;
5631 out:
5632 if (ret) {
5633 if (*ac) {
5634 ocfs2_free_alloc_context(*ac);
5635 *ac = NULL;
5639 return ret;
5642 int ocfs2_remove_btree_range(struct inode *inode,
5643 struct ocfs2_extent_tree *et,
5644 u32 cpos, u32 phys_cpos, u32 len, int flags,
5645 struct ocfs2_cached_dealloc_ctxt *dealloc,
5646 u64 refcount_loc)
5648 int ret, credits = 0, extra_blocks = 0;
5649 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5650 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5651 struct inode *tl_inode = osb->osb_tl_inode;
5652 handle_t *handle;
5653 struct ocfs2_alloc_context *meta_ac = NULL;
5654 struct ocfs2_refcount_tree *ref_tree = NULL;
5656 if ((flags & OCFS2_EXT_REFCOUNTED) && len) {
5657 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
5658 OCFS2_HAS_REFCOUNT_FL));
5660 ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
5661 &ref_tree, NULL);
5662 if (ret) {
5663 mlog_errno(ret);
5664 goto out;
5667 ret = ocfs2_prepare_refcount_change_for_del(inode,
5668 refcount_loc,
5669 phys_blkno,
5670 len,
5671 &credits,
5672 &extra_blocks);
5673 if (ret < 0) {
5674 mlog_errno(ret);
5675 goto out;
5679 ret = ocfs2_reserve_blocks_for_rec_trunc(inode, et, 1, &meta_ac,
5680 extra_blocks);
5681 if (ret) {
5682 mlog_errno(ret);
5683 return ret;
5686 mutex_lock(&tl_inode->i_mutex);
5688 if (ocfs2_truncate_log_needs_flush(osb)) {
5689 ret = __ocfs2_flush_truncate_log(osb);
5690 if (ret < 0) {
5691 mlog_errno(ret);
5692 goto out;
5696 handle = ocfs2_start_trans(osb,
5697 ocfs2_remove_extent_credits(osb->sb) + credits);
5698 if (IS_ERR(handle)) {
5699 ret = PTR_ERR(handle);
5700 mlog_errno(ret);
5701 goto out;
5704 ret = ocfs2_et_root_journal_access(handle, et,
5705 OCFS2_JOURNAL_ACCESS_WRITE);
5706 if (ret) {
5707 mlog_errno(ret);
5708 goto out;
5711 dquot_free_space_nodirty(inode,
5712 ocfs2_clusters_to_bytes(inode->i_sb, len));
5714 ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
5715 if (ret) {
5716 mlog_errno(ret);
5717 goto out_commit;
5720 ocfs2_et_update_clusters(et, -len);
5722 ocfs2_journal_dirty(handle, et->et_root_bh);
5724 if (phys_blkno) {
5725 if (flags & OCFS2_EXT_REFCOUNTED)
5726 ret = ocfs2_decrease_refcount(inode, handle,
5727 ocfs2_blocks_to_clusters(osb->sb,
5728 phys_blkno),
5729 len, meta_ac,
5730 dealloc, 1);
5731 else
5732 ret = ocfs2_truncate_log_append(osb, handle,
5733 phys_blkno, len);
5734 if (ret)
5735 mlog_errno(ret);
5739 out_commit:
5740 ocfs2_commit_trans(osb, handle);
5741 out:
5742 mutex_unlock(&tl_inode->i_mutex);
5744 if (meta_ac)
5745 ocfs2_free_alloc_context(meta_ac);
5747 if (ref_tree)
5748 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
5750 return ret;
5753 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5755 struct buffer_head *tl_bh = osb->osb_tl_bh;
5756 struct ocfs2_dinode *di;
5757 struct ocfs2_truncate_log *tl;
5759 di = (struct ocfs2_dinode *) tl_bh->b_data;
5760 tl = &di->id2.i_dealloc;
5762 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5763 "slot %d, invalid truncate log parameters: used = "
5764 "%u, count = %u\n", osb->slot_num,
5765 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5766 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5769 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5770 unsigned int new_start)
5772 unsigned int tail_index;
5773 unsigned int current_tail;
5775 /* No records, nothing to coalesce */
5776 if (!le16_to_cpu(tl->tl_used))
5777 return 0;
5779 tail_index = le16_to_cpu(tl->tl_used) - 1;
5780 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5781 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5783 return current_tail == new_start;
5786 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5787 handle_t *handle,
5788 u64 start_blk,
5789 unsigned int num_clusters)
5791 int status, index;
5792 unsigned int start_cluster, tl_count;
5793 struct inode *tl_inode = osb->osb_tl_inode;
5794 struct buffer_head *tl_bh = osb->osb_tl_bh;
5795 struct ocfs2_dinode *di;
5796 struct ocfs2_truncate_log *tl;
5798 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5799 (unsigned long long)start_blk, num_clusters);
5801 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5803 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5805 di = (struct ocfs2_dinode *) tl_bh->b_data;
5807 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5808 * by the underlying call to ocfs2_read_inode_block(), so any
5809 * corruption is a code bug */
5810 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5812 tl = &di->id2.i_dealloc;
5813 tl_count = le16_to_cpu(tl->tl_count);
5814 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5815 tl_count == 0,
5816 "Truncate record count on #%llu invalid "
5817 "wanted %u, actual %u\n",
5818 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5819 ocfs2_truncate_recs_per_inode(osb->sb),
5820 le16_to_cpu(tl->tl_count));
5822 /* Caller should have known to flush before calling us. */
5823 index = le16_to_cpu(tl->tl_used);
5824 if (index >= tl_count) {
5825 status = -ENOSPC;
5826 mlog_errno(status);
5827 goto bail;
5830 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5831 OCFS2_JOURNAL_ACCESS_WRITE);
5832 if (status < 0) {
5833 mlog_errno(status);
5834 goto bail;
5837 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5838 "%llu (index = %d)\n", num_clusters, start_cluster,
5839 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5841 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5843 * Move index back to the record we are coalescing with.
5844 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5846 index--;
5848 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5849 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5850 index, le32_to_cpu(tl->tl_recs[index].t_start),
5851 num_clusters);
5852 } else {
5853 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5854 tl->tl_used = cpu_to_le16(index + 1);
5856 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5858 ocfs2_journal_dirty(handle, tl_bh);
5860 osb->truncated_clusters += num_clusters;
5861 bail:
5862 mlog_exit(status);
5863 return status;
5866 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5867 handle_t *handle,
5868 struct inode *data_alloc_inode,
5869 struct buffer_head *data_alloc_bh)
5871 int status = 0;
5872 int i;
5873 unsigned int num_clusters;
5874 u64 start_blk;
5875 struct ocfs2_truncate_rec rec;
5876 struct ocfs2_dinode *di;
5877 struct ocfs2_truncate_log *tl;
5878 struct inode *tl_inode = osb->osb_tl_inode;
5879 struct buffer_head *tl_bh = osb->osb_tl_bh;
5881 mlog_entry_void();
5883 di = (struct ocfs2_dinode *) tl_bh->b_data;
5884 tl = &di->id2.i_dealloc;
5885 i = le16_to_cpu(tl->tl_used) - 1;
5886 while (i >= 0) {
5887 /* Caller has given us at least enough credits to
5888 * update the truncate log dinode */
5889 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5890 OCFS2_JOURNAL_ACCESS_WRITE);
5891 if (status < 0) {
5892 mlog_errno(status);
5893 goto bail;
5896 tl->tl_used = cpu_to_le16(i);
5898 ocfs2_journal_dirty(handle, tl_bh);
5900 /* TODO: Perhaps we can calculate the bulk of the
5901 * credits up front rather than extending like
5902 * this. */
5903 status = ocfs2_extend_trans(handle,
5904 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5905 if (status < 0) {
5906 mlog_errno(status);
5907 goto bail;
5910 rec = tl->tl_recs[i];
5911 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5912 le32_to_cpu(rec.t_start));
5913 num_clusters = le32_to_cpu(rec.t_clusters);
5915 /* if start_blk is not set, we ignore the record as
5916 * invalid. */
5917 if (start_blk) {
5918 mlog(0, "free record %d, start = %u, clusters = %u\n",
5919 i, le32_to_cpu(rec.t_start), num_clusters);
5921 status = ocfs2_free_clusters(handle, data_alloc_inode,
5922 data_alloc_bh, start_blk,
5923 num_clusters);
5924 if (status < 0) {
5925 mlog_errno(status);
5926 goto bail;
5929 i--;
5932 osb->truncated_clusters = 0;
5934 bail:
5935 mlog_exit(status);
5936 return status;
5939 /* Expects you to already be holding tl_inode->i_mutex */
5940 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5942 int status;
5943 unsigned int num_to_flush;
5944 handle_t *handle;
5945 struct inode *tl_inode = osb->osb_tl_inode;
5946 struct inode *data_alloc_inode = NULL;
5947 struct buffer_head *tl_bh = osb->osb_tl_bh;
5948 struct buffer_head *data_alloc_bh = NULL;
5949 struct ocfs2_dinode *di;
5950 struct ocfs2_truncate_log *tl;
5952 mlog_entry_void();
5954 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5956 di = (struct ocfs2_dinode *) tl_bh->b_data;
5958 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5959 * by the underlying call to ocfs2_read_inode_block(), so any
5960 * corruption is a code bug */
5961 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5963 tl = &di->id2.i_dealloc;
5964 num_to_flush = le16_to_cpu(tl->tl_used);
5965 mlog(0, "Flush %u records from truncate log #%llu\n",
5966 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5967 if (!num_to_flush) {
5968 status = 0;
5969 goto out;
5972 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5973 GLOBAL_BITMAP_SYSTEM_INODE,
5974 OCFS2_INVALID_SLOT);
5975 if (!data_alloc_inode) {
5976 status = -EINVAL;
5977 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5978 goto out;
5981 mutex_lock(&data_alloc_inode->i_mutex);
5983 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5984 if (status < 0) {
5985 mlog_errno(status);
5986 goto out_mutex;
5989 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5990 if (IS_ERR(handle)) {
5991 status = PTR_ERR(handle);
5992 mlog_errno(status);
5993 goto out_unlock;
5996 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5997 data_alloc_bh);
5998 if (status < 0)
5999 mlog_errno(status);
6001 ocfs2_commit_trans(osb, handle);
6003 out_unlock:
6004 brelse(data_alloc_bh);
6005 ocfs2_inode_unlock(data_alloc_inode, 1);
6007 out_mutex:
6008 mutex_unlock(&data_alloc_inode->i_mutex);
6009 iput(data_alloc_inode);
6011 out:
6012 mlog_exit(status);
6013 return status;
6016 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
6018 int status;
6019 struct inode *tl_inode = osb->osb_tl_inode;
6021 mutex_lock(&tl_inode->i_mutex);
6022 status = __ocfs2_flush_truncate_log(osb);
6023 mutex_unlock(&tl_inode->i_mutex);
6025 return status;
6028 static void ocfs2_truncate_log_worker(struct work_struct *work)
6030 int status;
6031 struct ocfs2_super *osb =
6032 container_of(work, struct ocfs2_super,
6033 osb_truncate_log_wq.work);
6035 mlog_entry_void();
6037 status = ocfs2_flush_truncate_log(osb);
6038 if (status < 0)
6039 mlog_errno(status);
6040 else
6041 ocfs2_init_steal_slots(osb);
6043 mlog_exit(status);
6046 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
6047 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
6048 int cancel)
6050 if (osb->osb_tl_inode) {
6051 /* We want to push off log flushes while truncates are
6052 * still running. */
6053 if (cancel)
6054 cancel_delayed_work(&osb->osb_truncate_log_wq);
6056 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
6057 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
6061 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
6062 int slot_num,
6063 struct inode **tl_inode,
6064 struct buffer_head **tl_bh)
6066 int status;
6067 struct inode *inode = NULL;
6068 struct buffer_head *bh = NULL;
6070 inode = ocfs2_get_system_file_inode(osb,
6071 TRUNCATE_LOG_SYSTEM_INODE,
6072 slot_num);
6073 if (!inode) {
6074 status = -EINVAL;
6075 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
6076 goto bail;
6079 status = ocfs2_read_inode_block(inode, &bh);
6080 if (status < 0) {
6081 iput(inode);
6082 mlog_errno(status);
6083 goto bail;
6086 *tl_inode = inode;
6087 *tl_bh = bh;
6088 bail:
6089 mlog_exit(status);
6090 return status;
6093 /* called during the 1st stage of node recovery. we stamp a clean
6094 * truncate log and pass back a copy for processing later. if the
6095 * truncate log does not require processing, a *tl_copy is set to
6096 * NULL. */
6097 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6098 int slot_num,
6099 struct ocfs2_dinode **tl_copy)
6101 int status;
6102 struct inode *tl_inode = NULL;
6103 struct buffer_head *tl_bh = NULL;
6104 struct ocfs2_dinode *di;
6105 struct ocfs2_truncate_log *tl;
6107 *tl_copy = NULL;
6109 mlog(0, "recover truncate log from slot %d\n", slot_num);
6111 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6112 if (status < 0) {
6113 mlog_errno(status);
6114 goto bail;
6117 di = (struct ocfs2_dinode *) tl_bh->b_data;
6119 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6120 * validated by the underlying call to ocfs2_read_inode_block(),
6121 * so any corruption is a code bug */
6122 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6124 tl = &di->id2.i_dealloc;
6125 if (le16_to_cpu(tl->tl_used)) {
6126 mlog(0, "We'll have %u logs to recover\n",
6127 le16_to_cpu(tl->tl_used));
6129 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6130 if (!(*tl_copy)) {
6131 status = -ENOMEM;
6132 mlog_errno(status);
6133 goto bail;
6136 /* Assuming the write-out below goes well, this copy
6137 * will be passed back to recovery for processing. */
6138 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6140 /* All we need to do to clear the truncate log is set
6141 * tl_used. */
6142 tl->tl_used = 0;
6144 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
6145 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
6146 if (status < 0) {
6147 mlog_errno(status);
6148 goto bail;
6152 bail:
6153 if (tl_inode)
6154 iput(tl_inode);
6155 brelse(tl_bh);
6157 if (status < 0 && (*tl_copy)) {
6158 kfree(*tl_copy);
6159 *tl_copy = NULL;
6162 mlog_exit(status);
6163 return status;
6166 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6167 struct ocfs2_dinode *tl_copy)
6169 int status = 0;
6170 int i;
6171 unsigned int clusters, num_recs, start_cluster;
6172 u64 start_blk;
6173 handle_t *handle;
6174 struct inode *tl_inode = osb->osb_tl_inode;
6175 struct ocfs2_truncate_log *tl;
6177 mlog_entry_void();
6179 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6180 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6181 return -EINVAL;
6184 tl = &tl_copy->id2.i_dealloc;
6185 num_recs = le16_to_cpu(tl->tl_used);
6186 mlog(0, "cleanup %u records from %llu\n", num_recs,
6187 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
6189 mutex_lock(&tl_inode->i_mutex);
6190 for(i = 0; i < num_recs; i++) {
6191 if (ocfs2_truncate_log_needs_flush(osb)) {
6192 status = __ocfs2_flush_truncate_log(osb);
6193 if (status < 0) {
6194 mlog_errno(status);
6195 goto bail_up;
6199 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6200 if (IS_ERR(handle)) {
6201 status = PTR_ERR(handle);
6202 mlog_errno(status);
6203 goto bail_up;
6206 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6207 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6208 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6210 status = ocfs2_truncate_log_append(osb, handle,
6211 start_blk, clusters);
6212 ocfs2_commit_trans(osb, handle);
6213 if (status < 0) {
6214 mlog_errno(status);
6215 goto bail_up;
6219 bail_up:
6220 mutex_unlock(&tl_inode->i_mutex);
6222 mlog_exit(status);
6223 return status;
6226 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6228 int status;
6229 struct inode *tl_inode = osb->osb_tl_inode;
6231 mlog_entry_void();
6233 if (tl_inode) {
6234 cancel_delayed_work(&osb->osb_truncate_log_wq);
6235 flush_workqueue(ocfs2_wq);
6237 status = ocfs2_flush_truncate_log(osb);
6238 if (status < 0)
6239 mlog_errno(status);
6241 brelse(osb->osb_tl_bh);
6242 iput(osb->osb_tl_inode);
6245 mlog_exit_void();
6248 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6250 int status;
6251 struct inode *tl_inode = NULL;
6252 struct buffer_head *tl_bh = NULL;
6254 mlog_entry_void();
6256 status = ocfs2_get_truncate_log_info(osb,
6257 osb->slot_num,
6258 &tl_inode,
6259 &tl_bh);
6260 if (status < 0)
6261 mlog_errno(status);
6263 /* ocfs2_truncate_log_shutdown keys on the existence of
6264 * osb->osb_tl_inode so we don't set any of the osb variables
6265 * until we're sure all is well. */
6266 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6267 ocfs2_truncate_log_worker);
6268 osb->osb_tl_bh = tl_bh;
6269 osb->osb_tl_inode = tl_inode;
6271 mlog_exit(status);
6272 return status;
6276 * Delayed de-allocation of suballocator blocks.
6278 * Some sets of block de-allocations might involve multiple suballocator inodes.
6280 * The locking for this can get extremely complicated, especially when
6281 * the suballocator inodes to delete from aren't known until deep
6282 * within an unrelated codepath.
6284 * ocfs2_extent_block structures are a good example of this - an inode
6285 * btree could have been grown by any number of nodes each allocating
6286 * out of their own suballoc inode.
6288 * These structures allow the delay of block de-allocation until a
6289 * later time, when locking of multiple cluster inodes won't cause
6290 * deadlock.
6294 * Describe a single bit freed from a suballocator. For the block
6295 * suballocators, it represents one block. For the global cluster
6296 * allocator, it represents some clusters and free_bit indicates
6297 * clusters number.
6299 struct ocfs2_cached_block_free {
6300 struct ocfs2_cached_block_free *free_next;
6301 u64 free_bg;
6302 u64 free_blk;
6303 unsigned int free_bit;
6306 struct ocfs2_per_slot_free_list {
6307 struct ocfs2_per_slot_free_list *f_next_suballocator;
6308 int f_inode_type;
6309 int f_slot;
6310 struct ocfs2_cached_block_free *f_first;
6313 static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6314 int sysfile_type,
6315 int slot,
6316 struct ocfs2_cached_block_free *head)
6318 int ret;
6319 u64 bg_blkno;
6320 handle_t *handle;
6321 struct inode *inode;
6322 struct buffer_head *di_bh = NULL;
6323 struct ocfs2_cached_block_free *tmp;
6325 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6326 if (!inode) {
6327 ret = -EINVAL;
6328 mlog_errno(ret);
6329 goto out;
6332 mutex_lock(&inode->i_mutex);
6334 ret = ocfs2_inode_lock(inode, &di_bh, 1);
6335 if (ret) {
6336 mlog_errno(ret);
6337 goto out_mutex;
6340 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6341 if (IS_ERR(handle)) {
6342 ret = PTR_ERR(handle);
6343 mlog_errno(ret);
6344 goto out_unlock;
6347 while (head) {
6348 if (head->free_bg)
6349 bg_blkno = head->free_bg;
6350 else
6351 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6352 head->free_bit);
6353 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6354 head->free_bit, (unsigned long long)head->free_blk);
6356 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6357 head->free_bit, bg_blkno, 1);
6358 if (ret) {
6359 mlog_errno(ret);
6360 goto out_journal;
6363 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6364 if (ret) {
6365 mlog_errno(ret);
6366 goto out_journal;
6369 tmp = head;
6370 head = head->free_next;
6371 kfree(tmp);
6374 out_journal:
6375 ocfs2_commit_trans(osb, handle);
6377 out_unlock:
6378 ocfs2_inode_unlock(inode, 1);
6379 brelse(di_bh);
6380 out_mutex:
6381 mutex_unlock(&inode->i_mutex);
6382 iput(inode);
6383 out:
6384 while(head) {
6385 /* Premature exit may have left some dangling items. */
6386 tmp = head;
6387 head = head->free_next;
6388 kfree(tmp);
6391 return ret;
6394 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6395 u64 blkno, unsigned int bit)
6397 int ret = 0;
6398 struct ocfs2_cached_block_free *item;
6400 item = kzalloc(sizeof(*item), GFP_NOFS);
6401 if (item == NULL) {
6402 ret = -ENOMEM;
6403 mlog_errno(ret);
6404 return ret;
6407 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6408 bit, (unsigned long long)blkno);
6410 item->free_blk = blkno;
6411 item->free_bit = bit;
6412 item->free_next = ctxt->c_global_allocator;
6414 ctxt->c_global_allocator = item;
6415 return ret;
6418 static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6419 struct ocfs2_cached_block_free *head)
6421 struct ocfs2_cached_block_free *tmp;
6422 struct inode *tl_inode = osb->osb_tl_inode;
6423 handle_t *handle;
6424 int ret = 0;
6426 mutex_lock(&tl_inode->i_mutex);
6428 while (head) {
6429 if (ocfs2_truncate_log_needs_flush(osb)) {
6430 ret = __ocfs2_flush_truncate_log(osb);
6431 if (ret < 0) {
6432 mlog_errno(ret);
6433 break;
6437 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6438 if (IS_ERR(handle)) {
6439 ret = PTR_ERR(handle);
6440 mlog_errno(ret);
6441 break;
6444 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6445 head->free_bit);
6447 ocfs2_commit_trans(osb, handle);
6448 tmp = head;
6449 head = head->free_next;
6450 kfree(tmp);
6452 if (ret < 0) {
6453 mlog_errno(ret);
6454 break;
6458 mutex_unlock(&tl_inode->i_mutex);
6460 while (head) {
6461 /* Premature exit may have left some dangling items. */
6462 tmp = head;
6463 head = head->free_next;
6464 kfree(tmp);
6467 return ret;
6470 int ocfs2_run_deallocs(struct ocfs2_super *osb,
6471 struct ocfs2_cached_dealloc_ctxt *ctxt)
6473 int ret = 0, ret2;
6474 struct ocfs2_per_slot_free_list *fl;
6476 if (!ctxt)
6477 return 0;
6479 while (ctxt->c_first_suballocator) {
6480 fl = ctxt->c_first_suballocator;
6482 if (fl->f_first) {
6483 mlog(0, "Free items: (type %u, slot %d)\n",
6484 fl->f_inode_type, fl->f_slot);
6485 ret2 = ocfs2_free_cached_blocks(osb,
6486 fl->f_inode_type,
6487 fl->f_slot,
6488 fl->f_first);
6489 if (ret2)
6490 mlog_errno(ret2);
6491 if (!ret)
6492 ret = ret2;
6495 ctxt->c_first_suballocator = fl->f_next_suballocator;
6496 kfree(fl);
6499 if (ctxt->c_global_allocator) {
6500 ret2 = ocfs2_free_cached_clusters(osb,
6501 ctxt->c_global_allocator);
6502 if (ret2)
6503 mlog_errno(ret2);
6504 if (!ret)
6505 ret = ret2;
6507 ctxt->c_global_allocator = NULL;
6510 return ret;
6513 static struct ocfs2_per_slot_free_list *
6514 ocfs2_find_per_slot_free_list(int type,
6515 int slot,
6516 struct ocfs2_cached_dealloc_ctxt *ctxt)
6518 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6520 while (fl) {
6521 if (fl->f_inode_type == type && fl->f_slot == slot)
6522 return fl;
6524 fl = fl->f_next_suballocator;
6527 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6528 if (fl) {
6529 fl->f_inode_type = type;
6530 fl->f_slot = slot;
6531 fl->f_first = NULL;
6532 fl->f_next_suballocator = ctxt->c_first_suballocator;
6534 ctxt->c_first_suballocator = fl;
6536 return fl;
6539 int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6540 int type, int slot, u64 suballoc,
6541 u64 blkno, unsigned int bit)
6543 int ret;
6544 struct ocfs2_per_slot_free_list *fl;
6545 struct ocfs2_cached_block_free *item;
6547 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6548 if (fl == NULL) {
6549 ret = -ENOMEM;
6550 mlog_errno(ret);
6551 goto out;
6554 item = kzalloc(sizeof(*item), GFP_NOFS);
6555 if (item == NULL) {
6556 ret = -ENOMEM;
6557 mlog_errno(ret);
6558 goto out;
6561 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6562 type, slot, bit, (unsigned long long)blkno);
6564 item->free_bg = suballoc;
6565 item->free_blk = blkno;
6566 item->free_bit = bit;
6567 item->free_next = fl->f_first;
6569 fl->f_first = item;
6571 ret = 0;
6572 out:
6573 return ret;
6576 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6577 struct ocfs2_extent_block *eb)
6579 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6580 le16_to_cpu(eb->h_suballoc_slot),
6581 le64_to_cpu(eb->h_suballoc_loc),
6582 le64_to_cpu(eb->h_blkno),
6583 le16_to_cpu(eb->h_suballoc_bit));
6586 static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
6588 set_buffer_uptodate(bh);
6589 mark_buffer_dirty(bh);
6590 return 0;
6593 void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6594 unsigned int from, unsigned int to,
6595 struct page *page, int zero, u64 *phys)
6597 int ret, partial = 0;
6599 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6600 if (ret)
6601 mlog_errno(ret);
6603 if (zero)
6604 zero_user_segment(page, from, to);
6607 * Need to set the buffers we zero'd into uptodate
6608 * here if they aren't - ocfs2_map_page_blocks()
6609 * might've skipped some
6611 ret = walk_page_buffers(handle, page_buffers(page),
6612 from, to, &partial,
6613 ocfs2_zero_func);
6614 if (ret < 0)
6615 mlog_errno(ret);
6616 else if (ocfs2_should_order_data(inode)) {
6617 ret = ocfs2_jbd2_file_inode(handle, inode);
6618 if (ret < 0)
6619 mlog_errno(ret);
6622 if (!partial)
6623 SetPageUptodate(page);
6625 flush_dcache_page(page);
6628 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6629 loff_t end, struct page **pages,
6630 int numpages, u64 phys, handle_t *handle)
6632 int i;
6633 struct page *page;
6634 unsigned int from, to = PAGE_CACHE_SIZE;
6635 struct super_block *sb = inode->i_sb;
6637 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6639 if (numpages == 0)
6640 goto out;
6642 to = PAGE_CACHE_SIZE;
6643 for(i = 0; i < numpages; i++) {
6644 page = pages[i];
6646 from = start & (PAGE_CACHE_SIZE - 1);
6647 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6648 to = end & (PAGE_CACHE_SIZE - 1);
6650 BUG_ON(from > PAGE_CACHE_SIZE);
6651 BUG_ON(to > PAGE_CACHE_SIZE);
6653 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6654 &phys);
6656 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6658 out:
6659 if (pages)
6660 ocfs2_unlock_and_free_pages(pages, numpages);
6663 int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
6664 struct page **pages, int *num)
6666 int numpages, ret = 0;
6667 struct address_space *mapping = inode->i_mapping;
6668 unsigned long index;
6669 loff_t last_page_bytes;
6671 BUG_ON(start > end);
6673 numpages = 0;
6674 last_page_bytes = PAGE_ALIGN(end);
6675 index = start >> PAGE_CACHE_SHIFT;
6676 do {
6677 pages[numpages] = find_or_create_page(mapping, index, GFP_NOFS);
6678 if (!pages[numpages]) {
6679 ret = -ENOMEM;
6680 mlog_errno(ret);
6681 goto out;
6684 numpages++;
6685 index++;
6686 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6688 out:
6689 if (ret != 0) {
6690 if (pages)
6691 ocfs2_unlock_and_free_pages(pages, numpages);
6692 numpages = 0;
6695 *num = numpages;
6697 return ret;
6700 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6701 struct page **pages, int *num)
6703 struct super_block *sb = inode->i_sb;
6705 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6706 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6708 return ocfs2_grab_pages(inode, start, end, pages, num);
6712 * Zero the area past i_size but still within an allocated
6713 * cluster. This avoids exposing nonzero data on subsequent file
6714 * extends.
6716 * We need to call this before i_size is updated on the inode because
6717 * otherwise block_write_full_page() will skip writeout of pages past
6718 * i_size. The new_i_size parameter is passed for this reason.
6720 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6721 u64 range_start, u64 range_end)
6723 int ret = 0, numpages;
6724 struct page **pages = NULL;
6725 u64 phys;
6726 unsigned int ext_flags;
6727 struct super_block *sb = inode->i_sb;
6730 * File systems which don't support sparse files zero on every
6731 * extend.
6733 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6734 return 0;
6736 pages = kcalloc(ocfs2_pages_per_cluster(sb),
6737 sizeof(struct page *), GFP_NOFS);
6738 if (pages == NULL) {
6739 ret = -ENOMEM;
6740 mlog_errno(ret);
6741 goto out;
6744 if (range_start == range_end)
6745 goto out;
6747 ret = ocfs2_extent_map_get_blocks(inode,
6748 range_start >> sb->s_blocksize_bits,
6749 &phys, NULL, &ext_flags);
6750 if (ret) {
6751 mlog_errno(ret);
6752 goto out;
6756 * Tail is a hole, or is marked unwritten. In either case, we
6757 * can count on read and write to return/push zero's.
6759 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
6760 goto out;
6762 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6763 &numpages);
6764 if (ret) {
6765 mlog_errno(ret);
6766 goto out;
6769 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6770 numpages, phys, handle);
6773 * Initiate writeout of the pages we zero'd here. We don't
6774 * wait on them - the truncate_inode_pages() call later will
6775 * do that for us.
6777 ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
6778 range_end - 1);
6779 if (ret)
6780 mlog_errno(ret);
6782 out:
6783 if (pages)
6784 kfree(pages);
6786 return ret;
6789 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6790 struct ocfs2_dinode *di)
6792 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6793 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
6795 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6796 memset(&di->id2, 0, blocksize -
6797 offsetof(struct ocfs2_dinode, id2) -
6798 xattrsize);
6799 else
6800 memset(&di->id2, 0, blocksize -
6801 offsetof(struct ocfs2_dinode, id2));
6804 void ocfs2_dinode_new_extent_list(struct inode *inode,
6805 struct ocfs2_dinode *di)
6807 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6808 di->id2.i_list.l_tree_depth = 0;
6809 di->id2.i_list.l_next_free_rec = 0;
6810 di->id2.i_list.l_count = cpu_to_le16(
6811 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
6814 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6816 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6817 struct ocfs2_inline_data *idata = &di->id2.i_data;
6819 spin_lock(&oi->ip_lock);
6820 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6821 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6822 spin_unlock(&oi->ip_lock);
6825 * We clear the entire i_data structure here so that all
6826 * fields can be properly initialized.
6828 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6830 idata->id_count = cpu_to_le16(
6831 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
6834 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6835 struct buffer_head *di_bh)
6837 int ret, i, has_data, num_pages = 0;
6838 handle_t *handle;
6839 u64 uninitialized_var(block);
6840 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6841 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6842 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6843 struct ocfs2_alloc_context *data_ac = NULL;
6844 struct page **pages = NULL;
6845 loff_t end = osb->s_clustersize;
6846 struct ocfs2_extent_tree et;
6847 int did_quota = 0;
6849 has_data = i_size_read(inode) ? 1 : 0;
6851 if (has_data) {
6852 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6853 sizeof(struct page *), GFP_NOFS);
6854 if (pages == NULL) {
6855 ret = -ENOMEM;
6856 mlog_errno(ret);
6857 goto out;
6860 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6861 if (ret) {
6862 mlog_errno(ret);
6863 goto out;
6867 handle = ocfs2_start_trans(osb,
6868 ocfs2_inline_to_extents_credits(osb->sb));
6869 if (IS_ERR(handle)) {
6870 ret = PTR_ERR(handle);
6871 mlog_errno(ret);
6872 goto out_unlock;
6875 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
6876 OCFS2_JOURNAL_ACCESS_WRITE);
6877 if (ret) {
6878 mlog_errno(ret);
6879 goto out_commit;
6882 if (has_data) {
6883 u32 bit_off, num;
6884 unsigned int page_end;
6885 u64 phys;
6887 ret = dquot_alloc_space_nodirty(inode,
6888 ocfs2_clusters_to_bytes(osb->sb, 1));
6889 if (ret)
6890 goto out_commit;
6891 did_quota = 1;
6893 data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
6895 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
6896 &num);
6897 if (ret) {
6898 mlog_errno(ret);
6899 goto out_commit;
6903 * Save two copies, one for insert, and one that can
6904 * be changed by ocfs2_map_and_dirty_page() below.
6906 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6909 * Non sparse file systems zero on extend, so no need
6910 * to do that now.
6912 if (!ocfs2_sparse_alloc(osb) &&
6913 PAGE_CACHE_SIZE < osb->s_clustersize)
6914 end = PAGE_CACHE_SIZE;
6916 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6917 if (ret) {
6918 mlog_errno(ret);
6919 goto out_commit;
6923 * This should populate the 1st page for us and mark
6924 * it up to date.
6926 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6927 if (ret) {
6928 mlog_errno(ret);
6929 goto out_commit;
6932 page_end = PAGE_CACHE_SIZE;
6933 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6934 page_end = osb->s_clustersize;
6936 for (i = 0; i < num_pages; i++)
6937 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6938 pages[i], i > 0, &phys);
6941 spin_lock(&oi->ip_lock);
6942 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6943 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6944 spin_unlock(&oi->ip_lock);
6946 ocfs2_dinode_new_extent_list(inode, di);
6948 ocfs2_journal_dirty(handle, di_bh);
6950 if (has_data) {
6952 * An error at this point should be extremely rare. If
6953 * this proves to be false, we could always re-build
6954 * the in-inode data from our pages.
6956 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
6957 ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
6958 if (ret) {
6959 mlog_errno(ret);
6960 goto out_commit;
6963 inode->i_blocks = ocfs2_inode_sector_count(inode);
6966 out_commit:
6967 if (ret < 0 && did_quota)
6968 dquot_free_space_nodirty(inode,
6969 ocfs2_clusters_to_bytes(osb->sb, 1));
6971 ocfs2_commit_trans(osb, handle);
6973 out_unlock:
6974 if (data_ac)
6975 ocfs2_free_alloc_context(data_ac);
6977 out:
6978 if (pages) {
6979 ocfs2_unlock_and_free_pages(pages, num_pages);
6980 kfree(pages);
6983 return ret;
6987 * It is expected, that by the time you call this function,
6988 * inode->i_size and fe->i_size have been adjusted.
6990 * WARNING: This will kfree the truncate context
6992 int ocfs2_commit_truncate(struct ocfs2_super *osb,
6993 struct inode *inode,
6994 struct buffer_head *di_bh)
6996 int status = 0, i, flags = 0;
6997 u32 new_highest_cpos, range, trunc_cpos, trunc_len, phys_cpos, coff;
6998 u64 blkno = 0;
6999 struct ocfs2_extent_list *el;
7000 struct ocfs2_extent_rec *rec;
7001 struct ocfs2_path *path = NULL;
7002 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7003 struct ocfs2_extent_list *root_el = &(di->id2.i_list);
7004 u64 refcount_loc = le64_to_cpu(di->i_refcount_loc);
7005 struct ocfs2_extent_tree et;
7006 struct ocfs2_cached_dealloc_ctxt dealloc;
7008 mlog_entry_void();
7010 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
7011 ocfs2_init_dealloc_ctxt(&dealloc);
7013 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
7014 i_size_read(inode));
7016 path = ocfs2_new_path(di_bh, &di->id2.i_list,
7017 ocfs2_journal_access_di);
7018 if (!path) {
7019 status = -ENOMEM;
7020 mlog_errno(status);
7021 goto bail;
7024 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7026 start:
7028 * Check that we still have allocation to delete.
7030 if (OCFS2_I(inode)->ip_clusters == 0) {
7031 status = 0;
7032 goto bail;
7036 * Truncate always works against the rightmost tree branch.
7038 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
7039 if (status) {
7040 mlog_errno(status);
7041 goto bail;
7044 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7045 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7048 * By now, el will point to the extent list on the bottom most
7049 * portion of this tree. Only the tail record is considered in
7050 * each pass.
7052 * We handle the following cases, in order:
7053 * - empty extent: delete the remaining branch
7054 * - remove the entire record
7055 * - remove a partial record
7056 * - no record needs to be removed (truncate has completed)
7058 el = path_leaf_el(path);
7059 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7060 ocfs2_error(inode->i_sb,
7061 "Inode %llu has empty extent block at %llu\n",
7062 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7063 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7064 status = -EROFS;
7065 goto bail;
7068 i = le16_to_cpu(el->l_next_free_rec) - 1;
7069 rec = &el->l_recs[i];
7070 flags = rec->e_flags;
7071 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
7073 if (i == 0 && ocfs2_is_empty_extent(rec)) {
7075 * Lower levels depend on this never happening, but it's best
7076 * to check it up here before changing the tree.
7078 if (root_el->l_tree_depth && rec->e_int_clusters == 0) {
7079 ocfs2_error(inode->i_sb, "Inode %lu has an empty "
7080 "extent record, depth %u\n", inode->i_ino,
7081 le16_to_cpu(root_el->l_tree_depth));
7082 status = -EROFS;
7083 goto bail;
7085 trunc_cpos = le32_to_cpu(rec->e_cpos);
7086 trunc_len = 0;
7087 blkno = 0;
7088 } else if (le32_to_cpu(rec->e_cpos) >= new_highest_cpos) {
7090 * Truncate entire record.
7092 trunc_cpos = le32_to_cpu(rec->e_cpos);
7093 trunc_len = ocfs2_rec_clusters(el, rec);
7094 blkno = le64_to_cpu(rec->e_blkno);
7095 } else if (range > new_highest_cpos) {
7097 * Partial truncate. it also should be
7098 * the last truncate we're doing.
7100 trunc_cpos = new_highest_cpos;
7101 trunc_len = range - new_highest_cpos;
7102 coff = new_highest_cpos - le32_to_cpu(rec->e_cpos);
7103 blkno = le64_to_cpu(rec->e_blkno) +
7104 ocfs2_clusters_to_blocks(inode->i_sb, coff);
7105 } else {
7107 * Truncate completed, leave happily.
7109 status = 0;
7110 goto bail;
7113 phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
7115 status = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
7116 phys_cpos, trunc_len, flags, &dealloc,
7117 refcount_loc);
7118 if (status < 0) {
7119 mlog_errno(status);
7120 goto bail;
7123 ocfs2_reinit_path(path, 1);
7126 * The check above will catch the case where we've truncated
7127 * away all allocation.
7129 goto start;
7131 bail:
7133 ocfs2_schedule_truncate_log_flush(osb, 1);
7135 ocfs2_run_deallocs(osb, &dealloc);
7137 ocfs2_free_path(path);
7139 mlog_exit(status);
7140 return status;
7144 * 'start' is inclusive, 'end' is not.
7146 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7147 unsigned int start, unsigned int end, int trunc)
7149 int ret;
7150 unsigned int numbytes;
7151 handle_t *handle;
7152 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7153 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7154 struct ocfs2_inline_data *idata = &di->id2.i_data;
7156 if (end > i_size_read(inode))
7157 end = i_size_read(inode);
7159 BUG_ON(start >= end);
7161 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7162 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7163 !ocfs2_supports_inline_data(osb)) {
7164 ocfs2_error(inode->i_sb,
7165 "Inline data flags for inode %llu don't agree! "
7166 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7167 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7168 le16_to_cpu(di->i_dyn_features),
7169 OCFS2_I(inode)->ip_dyn_features,
7170 osb->s_feature_incompat);
7171 ret = -EROFS;
7172 goto out;
7175 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7176 if (IS_ERR(handle)) {
7177 ret = PTR_ERR(handle);
7178 mlog_errno(ret);
7179 goto out;
7182 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7183 OCFS2_JOURNAL_ACCESS_WRITE);
7184 if (ret) {
7185 mlog_errno(ret);
7186 goto out_commit;
7189 numbytes = end - start;
7190 memset(idata->id_data + start, 0, numbytes);
7193 * No need to worry about the data page here - it's been
7194 * truncated already and inline data doesn't need it for
7195 * pushing zero's to disk, so we'll let readpage pick it up
7196 * later.
7198 if (trunc) {
7199 i_size_write(inode, start);
7200 di->i_size = cpu_to_le64(start);
7203 inode->i_blocks = ocfs2_inode_sector_count(inode);
7204 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7206 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7207 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7209 ocfs2_journal_dirty(handle, di_bh);
7211 out_commit:
7212 ocfs2_commit_trans(osb, handle);
7214 out:
7215 return ret;