GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / fs / ocfs2 / alloc.c
blob2064cc8e196323fcf25c07dd3651ed26b4ccc10a
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 void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
569 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
570 struct ocfs2_extent_block *eb);
571 static void ocfs2_adjust_rightmost_records(handle_t *handle,
572 struct ocfs2_extent_tree *et,
573 struct ocfs2_path *path,
574 struct ocfs2_extent_rec *insert_rec);
576 * Reset the actual path elements so that we can re-use the structure
577 * to build another path. Generally, this involves freeing the buffer
578 * heads.
580 void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
582 int i, start = 0, depth = 0;
583 struct ocfs2_path_item *node;
585 if (keep_root)
586 start = 1;
588 for(i = start; i < path_num_items(path); i++) {
589 node = &path->p_node[i];
591 brelse(node->bh);
592 node->bh = NULL;
593 node->el = NULL;
597 * Tree depth may change during truncate, or insert. If we're
598 * keeping the root extent list, then make sure that our path
599 * structure reflects the proper depth.
601 if (keep_root)
602 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
603 else
604 path_root_access(path) = NULL;
606 path->p_tree_depth = depth;
609 void ocfs2_free_path(struct ocfs2_path *path)
611 if (path) {
612 ocfs2_reinit_path(path, 0);
613 kfree(path);
618 * All the elements of src into dest. After this call, src could be freed
619 * without affecting dest.
621 * Both paths should have the same root. Any non-root elements of dest
622 * will be freed.
624 static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
626 int i;
628 BUG_ON(path_root_bh(dest) != path_root_bh(src));
629 BUG_ON(path_root_el(dest) != path_root_el(src));
630 BUG_ON(path_root_access(dest) != path_root_access(src));
632 ocfs2_reinit_path(dest, 1);
634 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
635 dest->p_node[i].bh = src->p_node[i].bh;
636 dest->p_node[i].el = src->p_node[i].el;
638 if (dest->p_node[i].bh)
639 get_bh(dest->p_node[i].bh);
644 * Make the *dest path the same as src and re-initialize src path to
645 * have a root only.
647 static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
649 int i;
651 BUG_ON(path_root_bh(dest) != path_root_bh(src));
652 BUG_ON(path_root_access(dest) != path_root_access(src));
654 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
655 brelse(dest->p_node[i].bh);
657 dest->p_node[i].bh = src->p_node[i].bh;
658 dest->p_node[i].el = src->p_node[i].el;
660 src->p_node[i].bh = NULL;
661 src->p_node[i].el = NULL;
666 * Insert an extent block at given index.
668 * This will not take an additional reference on eb_bh.
670 static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
671 struct buffer_head *eb_bh)
673 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
676 * Right now, no root bh is an extent block, so this helps
677 * catch code errors with dinode trees. The assertion can be
678 * safely removed if we ever need to insert extent block
679 * structures at the root.
681 BUG_ON(index == 0);
683 path->p_node[index].bh = eb_bh;
684 path->p_node[index].el = &eb->h_list;
687 static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
688 struct ocfs2_extent_list *root_el,
689 ocfs2_journal_access_func access)
691 struct ocfs2_path *path;
693 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
695 path = kzalloc(sizeof(*path), GFP_NOFS);
696 if (path) {
697 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
698 get_bh(root_bh);
699 path_root_bh(path) = root_bh;
700 path_root_el(path) = root_el;
701 path_root_access(path) = access;
704 return path;
707 struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
709 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
710 path_root_access(path));
713 struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
715 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
716 et->et_root_journal_access);
720 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
721 * otherwise it's the root_access function.
723 * I don't like the way this function's name looks next to
724 * ocfs2_journal_access_path(), but I don't have a better one.
726 int ocfs2_path_bh_journal_access(handle_t *handle,
727 struct ocfs2_caching_info *ci,
728 struct ocfs2_path *path,
729 int idx)
731 ocfs2_journal_access_func access = path_root_access(path);
733 if (!access)
734 access = ocfs2_journal_access;
736 if (idx)
737 access = ocfs2_journal_access_eb;
739 return access(handle, ci, path->p_node[idx].bh,
740 OCFS2_JOURNAL_ACCESS_WRITE);
744 * Convenience function to journal all components in a path.
746 int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
747 handle_t *handle,
748 struct ocfs2_path *path)
750 int i, ret = 0;
752 if (!path)
753 goto out;
755 for(i = 0; i < path_num_items(path); i++) {
756 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
757 if (ret < 0) {
758 mlog_errno(ret);
759 goto out;
763 out:
764 return ret;
768 * Return the index of the extent record which contains cluster #v_cluster.
769 * -1 is returned if it was not found.
771 * Should work fine on interior and exterior nodes.
773 int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
775 int ret = -1;
776 int i;
777 struct ocfs2_extent_rec *rec;
778 u32 rec_end, rec_start, clusters;
780 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
781 rec = &el->l_recs[i];
783 rec_start = le32_to_cpu(rec->e_cpos);
784 clusters = ocfs2_rec_clusters(el, rec);
786 rec_end = rec_start + clusters;
788 if (v_cluster >= rec_start && v_cluster < rec_end) {
789 ret = i;
790 break;
794 return ret;
798 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
799 * ocfs2_extent_rec_contig only work properly against leaf nodes!
801 static int ocfs2_block_extent_contig(struct super_block *sb,
802 struct ocfs2_extent_rec *ext,
803 u64 blkno)
805 u64 blk_end = le64_to_cpu(ext->e_blkno);
807 blk_end += ocfs2_clusters_to_blocks(sb,
808 le16_to_cpu(ext->e_leaf_clusters));
810 return blkno == blk_end;
813 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
814 struct ocfs2_extent_rec *right)
816 u32 left_range;
818 left_range = le32_to_cpu(left->e_cpos) +
819 le16_to_cpu(left->e_leaf_clusters);
821 return (left_range == le32_to_cpu(right->e_cpos));
824 static enum ocfs2_contig_type
825 ocfs2_extent_rec_contig(struct super_block *sb,
826 struct ocfs2_extent_rec *ext,
827 struct ocfs2_extent_rec *insert_rec)
829 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
832 * Refuse to coalesce extent records with different flag
833 * fields - we don't want to mix unwritten extents with user
834 * data.
836 if (ext->e_flags != insert_rec->e_flags)
837 return CONTIG_NONE;
839 if (ocfs2_extents_adjacent(ext, insert_rec) &&
840 ocfs2_block_extent_contig(sb, ext, blkno))
841 return CONTIG_RIGHT;
843 blkno = le64_to_cpu(ext->e_blkno);
844 if (ocfs2_extents_adjacent(insert_rec, ext) &&
845 ocfs2_block_extent_contig(sb, insert_rec, blkno))
846 return CONTIG_LEFT;
848 return CONTIG_NONE;
852 * NOTE: We can have pretty much any combination of contiguousness and
853 * appending.
855 * The usefulness of APPEND_TAIL is more in that it lets us know that
856 * we'll have to update the path to that leaf.
858 enum ocfs2_append_type {
859 APPEND_NONE = 0,
860 APPEND_TAIL,
863 enum ocfs2_split_type {
864 SPLIT_NONE = 0,
865 SPLIT_LEFT,
866 SPLIT_RIGHT,
869 struct ocfs2_insert_type {
870 enum ocfs2_split_type ins_split;
871 enum ocfs2_append_type ins_appending;
872 enum ocfs2_contig_type ins_contig;
873 int ins_contig_index;
874 int ins_tree_depth;
877 struct ocfs2_merge_ctxt {
878 enum ocfs2_contig_type c_contig_type;
879 int c_has_empty_extent;
880 int c_split_covers_rec;
883 static int ocfs2_validate_extent_block(struct super_block *sb,
884 struct buffer_head *bh)
886 int rc;
887 struct ocfs2_extent_block *eb =
888 (struct ocfs2_extent_block *)bh->b_data;
890 mlog(0, "Validating extent block %llu\n",
891 (unsigned long long)bh->b_blocknr);
893 BUG_ON(!buffer_uptodate(bh));
896 * If the ecc fails, we return the error but otherwise
897 * leave the filesystem running. We know any error is
898 * local to this block.
900 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
901 if (rc) {
902 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
903 (unsigned long long)bh->b_blocknr);
904 return rc;
908 * Errors after here are fatal.
911 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
912 ocfs2_error(sb,
913 "Extent block #%llu has bad signature %.*s",
914 (unsigned long long)bh->b_blocknr, 7,
915 eb->h_signature);
916 return -EINVAL;
919 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
920 ocfs2_error(sb,
921 "Extent block #%llu has an invalid h_blkno "
922 "of %llu",
923 (unsigned long long)bh->b_blocknr,
924 (unsigned long long)le64_to_cpu(eb->h_blkno));
925 return -EINVAL;
928 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
929 ocfs2_error(sb,
930 "Extent block #%llu has an invalid "
931 "h_fs_generation of #%u",
932 (unsigned long long)bh->b_blocknr,
933 le32_to_cpu(eb->h_fs_generation));
934 return -EINVAL;
937 return 0;
940 int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
941 struct buffer_head **bh)
943 int rc;
944 struct buffer_head *tmp = *bh;
946 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
947 ocfs2_validate_extent_block);
949 /* If ocfs2_read_block() got us a new bh, pass it up. */
950 if (!rc && !*bh)
951 *bh = tmp;
953 return rc;
958 * How many free extents have we got before we need more meta data?
960 int ocfs2_num_free_extents(struct ocfs2_super *osb,
961 struct ocfs2_extent_tree *et)
963 int retval;
964 struct ocfs2_extent_list *el = NULL;
965 struct ocfs2_extent_block *eb;
966 struct buffer_head *eb_bh = NULL;
967 u64 last_eb_blk = 0;
969 mlog_entry_void();
971 el = et->et_root_el;
972 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
974 if (last_eb_blk) {
975 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
976 &eb_bh);
977 if (retval < 0) {
978 mlog_errno(retval);
979 goto bail;
981 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
982 el = &eb->h_list;
985 BUG_ON(el->l_tree_depth != 0);
987 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
988 bail:
989 brelse(eb_bh);
991 mlog_exit(retval);
992 return retval;
995 /* expects array to already be allocated
997 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
998 * l_count for you
1000 static int ocfs2_create_new_meta_bhs(handle_t *handle,
1001 struct ocfs2_extent_tree *et,
1002 int wanted,
1003 struct ocfs2_alloc_context *meta_ac,
1004 struct buffer_head *bhs[])
1006 int count, status, i;
1007 u16 suballoc_bit_start;
1008 u32 num_got;
1009 u64 suballoc_loc, first_blkno;
1010 struct ocfs2_super *osb =
1011 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
1012 struct ocfs2_extent_block *eb;
1014 mlog_entry_void();
1016 count = 0;
1017 while (count < wanted) {
1018 status = ocfs2_claim_metadata(handle,
1019 meta_ac,
1020 wanted - count,
1021 &suballoc_loc,
1022 &suballoc_bit_start,
1023 &num_got,
1024 &first_blkno);
1025 if (status < 0) {
1026 mlog_errno(status);
1027 goto bail;
1030 for(i = count; i < (num_got + count); i++) {
1031 bhs[i] = sb_getblk(osb->sb, first_blkno);
1032 if (bhs[i] == NULL) {
1033 status = -EIO;
1034 mlog_errno(status);
1035 goto bail;
1037 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
1039 status = ocfs2_journal_access_eb(handle, et->et_ci,
1040 bhs[i],
1041 OCFS2_JOURNAL_ACCESS_CREATE);
1042 if (status < 0) {
1043 mlog_errno(status);
1044 goto bail;
1047 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
1048 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
1049 /* Ok, setup the minimal stuff here. */
1050 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
1051 eb->h_blkno = cpu_to_le64(first_blkno);
1052 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
1053 eb->h_suballoc_slot =
1054 cpu_to_le16(meta_ac->ac_alloc_slot);
1055 eb->h_suballoc_loc = cpu_to_le64(suballoc_loc);
1056 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1057 eb->h_list.l_count =
1058 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1060 suballoc_bit_start++;
1061 first_blkno++;
1063 /* We'll also be dirtied by the caller, so
1064 * this isn't absolutely necessary. */
1065 ocfs2_journal_dirty(handle, bhs[i]);
1068 count += num_got;
1071 status = 0;
1072 bail:
1073 if (status < 0) {
1074 for(i = 0; i < wanted; i++) {
1075 brelse(bhs[i]);
1076 bhs[i] = NULL;
1079 mlog_exit(status);
1080 return status;
1084 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1086 * Returns the sum of the rightmost extent rec logical offset and
1087 * cluster count.
1089 * ocfs2_add_branch() uses this to determine what logical cluster
1090 * value should be populated into the leftmost new branch records.
1092 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1093 * value for the new topmost tree record.
1095 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1097 int i;
1099 i = le16_to_cpu(el->l_next_free_rec) - 1;
1101 return le32_to_cpu(el->l_recs[i].e_cpos) +
1102 ocfs2_rec_clusters(el, &el->l_recs[i]);
1106 * Change range of the branches in the right most path according to the leaf
1107 * extent block's rightmost record.
1109 static int ocfs2_adjust_rightmost_branch(handle_t *handle,
1110 struct ocfs2_extent_tree *et)
1112 int status;
1113 struct ocfs2_path *path = NULL;
1114 struct ocfs2_extent_list *el;
1115 struct ocfs2_extent_rec *rec;
1117 path = ocfs2_new_path_from_et(et);
1118 if (!path) {
1119 status = -ENOMEM;
1120 return status;
1123 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
1124 if (status < 0) {
1125 mlog_errno(status);
1126 goto out;
1129 status = ocfs2_extend_trans(handle, path_num_items(path));
1130 if (status < 0) {
1131 mlog_errno(status);
1132 goto out;
1135 status = ocfs2_journal_access_path(et->et_ci, handle, path);
1136 if (status < 0) {
1137 mlog_errno(status);
1138 goto out;
1141 el = path_leaf_el(path);
1142 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1144 ocfs2_adjust_rightmost_records(handle, et, path, rec);
1146 out:
1147 ocfs2_free_path(path);
1148 return status;
1152 * Add an entire tree branch to our inode. eb_bh is the extent block
1153 * to start at, if we don't want to start the branch at the root
1154 * structure.
1156 * last_eb_bh is required as we have to update it's next_leaf pointer
1157 * for the new last extent block.
1159 * the new branch will be 'empty' in the sense that every block will
1160 * contain a single record with cluster count == 0.
1162 static int ocfs2_add_branch(handle_t *handle,
1163 struct ocfs2_extent_tree *et,
1164 struct buffer_head *eb_bh,
1165 struct buffer_head **last_eb_bh,
1166 struct ocfs2_alloc_context *meta_ac)
1168 int status, new_blocks, i;
1169 u64 next_blkno, new_last_eb_blk;
1170 struct buffer_head *bh;
1171 struct buffer_head **new_eb_bhs = NULL;
1172 struct ocfs2_extent_block *eb;
1173 struct ocfs2_extent_list *eb_el;
1174 struct ocfs2_extent_list *el;
1175 u32 new_cpos, root_end;
1177 mlog_entry_void();
1179 BUG_ON(!last_eb_bh || !*last_eb_bh);
1181 if (eb_bh) {
1182 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1183 el = &eb->h_list;
1184 } else
1185 el = et->et_root_el;
1187 /* we never add a branch to a leaf. */
1188 BUG_ON(!el->l_tree_depth);
1190 new_blocks = le16_to_cpu(el->l_tree_depth);
1192 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1193 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1194 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1197 * If there is a gap before the root end and the real end
1198 * of the righmost leaf block, we need to remove the gap
1199 * between new_cpos and root_end first so that the tree
1200 * is consistent after we add a new branch(it will start
1201 * from new_cpos).
1203 if (root_end > new_cpos) {
1204 mlog(0, "adjust the cluster end from %u to %u\n",
1205 root_end, new_cpos);
1206 status = ocfs2_adjust_rightmost_branch(handle, et);
1207 if (status) {
1208 mlog_errno(status);
1209 goto bail;
1213 /* allocate the number of new eb blocks we need */
1214 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1215 GFP_KERNEL);
1216 if (!new_eb_bhs) {
1217 status = -ENOMEM;
1218 mlog_errno(status);
1219 goto bail;
1222 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
1223 meta_ac, new_eb_bhs);
1224 if (status < 0) {
1225 mlog_errno(status);
1226 goto bail;
1229 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1230 * linked with the rest of the tree.
1231 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1233 * when we leave the loop, new_last_eb_blk will point to the
1234 * newest leaf, and next_blkno will point to the topmost extent
1235 * block. */
1236 next_blkno = new_last_eb_blk = 0;
1237 for(i = 0; i < new_blocks; i++) {
1238 bh = new_eb_bhs[i];
1239 eb = (struct ocfs2_extent_block *) bh->b_data;
1240 /* ocfs2_create_new_meta_bhs() should create it right! */
1241 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1242 eb_el = &eb->h_list;
1244 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
1245 OCFS2_JOURNAL_ACCESS_CREATE);
1246 if (status < 0) {
1247 mlog_errno(status);
1248 goto bail;
1251 eb->h_next_leaf_blk = 0;
1252 eb_el->l_tree_depth = cpu_to_le16(i);
1253 eb_el->l_next_free_rec = cpu_to_le16(1);
1255 * This actually counts as an empty extent as
1256 * c_clusters == 0
1258 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
1259 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
1261 * eb_el isn't always an interior node, but even leaf
1262 * nodes want a zero'd flags and reserved field so
1263 * this gets the whole 32 bits regardless of use.
1265 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
1266 if (!eb_el->l_tree_depth)
1267 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1269 ocfs2_journal_dirty(handle, bh);
1270 next_blkno = le64_to_cpu(eb->h_blkno);
1273 /* This is a bit hairy. We want to update up to three blocks
1274 * here without leaving any of them in an inconsistent state
1275 * in case of error. We don't have to worry about
1276 * journal_dirty erroring as it won't unless we've aborted the
1277 * handle (in which case we would never be here) so reserving
1278 * the write with journal_access is all we need to do. */
1279 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
1280 OCFS2_JOURNAL_ACCESS_WRITE);
1281 if (status < 0) {
1282 mlog_errno(status);
1283 goto bail;
1285 status = ocfs2_et_root_journal_access(handle, et,
1286 OCFS2_JOURNAL_ACCESS_WRITE);
1287 if (status < 0) {
1288 mlog_errno(status);
1289 goto bail;
1291 if (eb_bh) {
1292 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
1293 OCFS2_JOURNAL_ACCESS_WRITE);
1294 if (status < 0) {
1295 mlog_errno(status);
1296 goto bail;
1300 /* Link the new branch into the rest of the tree (el will
1301 * either be on the root_bh, or the extent block passed in. */
1302 i = le16_to_cpu(el->l_next_free_rec);
1303 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
1304 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1305 el->l_recs[i].e_int_clusters = 0;
1306 le16_add_cpu(&el->l_next_free_rec, 1);
1308 /* fe needs a new last extent block pointer, as does the
1309 * next_leaf on the previously last-extent-block. */
1310 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
1312 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
1313 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1315 ocfs2_journal_dirty(handle, *last_eb_bh);
1316 ocfs2_journal_dirty(handle, et->et_root_bh);
1317 if (eb_bh)
1318 ocfs2_journal_dirty(handle, eb_bh);
1321 * Some callers want to track the rightmost leaf so pass it
1322 * back here.
1324 brelse(*last_eb_bh);
1325 get_bh(new_eb_bhs[0]);
1326 *last_eb_bh = new_eb_bhs[0];
1328 status = 0;
1329 bail:
1330 if (new_eb_bhs) {
1331 for (i = 0; i < new_blocks; i++)
1332 brelse(new_eb_bhs[i]);
1333 kfree(new_eb_bhs);
1336 mlog_exit(status);
1337 return status;
1341 * adds another level to the allocation tree.
1342 * returns back the new extent block so you can add a branch to it
1343 * after this call.
1345 static int ocfs2_shift_tree_depth(handle_t *handle,
1346 struct ocfs2_extent_tree *et,
1347 struct ocfs2_alloc_context *meta_ac,
1348 struct buffer_head **ret_new_eb_bh)
1350 int status, i;
1351 u32 new_clusters;
1352 struct buffer_head *new_eb_bh = NULL;
1353 struct ocfs2_extent_block *eb;
1354 struct ocfs2_extent_list *root_el;
1355 struct ocfs2_extent_list *eb_el;
1357 mlog_entry_void();
1359 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
1360 &new_eb_bh);
1361 if (status < 0) {
1362 mlog_errno(status);
1363 goto bail;
1366 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1367 /* ocfs2_create_new_meta_bhs() should create it right! */
1368 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
1370 eb_el = &eb->h_list;
1371 root_el = et->et_root_el;
1373 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
1374 OCFS2_JOURNAL_ACCESS_CREATE);
1375 if (status < 0) {
1376 mlog_errno(status);
1377 goto bail;
1380 /* copy the root extent list data into the new extent block */
1381 eb_el->l_tree_depth = root_el->l_tree_depth;
1382 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1383 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1384 eb_el->l_recs[i] = root_el->l_recs[i];
1386 ocfs2_journal_dirty(handle, new_eb_bh);
1388 status = ocfs2_et_root_journal_access(handle, et,
1389 OCFS2_JOURNAL_ACCESS_WRITE);
1390 if (status < 0) {
1391 mlog_errno(status);
1392 goto bail;
1395 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1397 /* update root_bh now */
1398 le16_add_cpu(&root_el->l_tree_depth, 1);
1399 root_el->l_recs[0].e_cpos = 0;
1400 root_el->l_recs[0].e_blkno = eb->h_blkno;
1401 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1402 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1403 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1404 root_el->l_next_free_rec = cpu_to_le16(1);
1406 /* If this is our 1st tree depth shift, then last_eb_blk
1407 * becomes the allocated extent block */
1408 if (root_el->l_tree_depth == cpu_to_le16(1))
1409 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
1411 ocfs2_journal_dirty(handle, et->et_root_bh);
1413 *ret_new_eb_bh = new_eb_bh;
1414 new_eb_bh = NULL;
1415 status = 0;
1416 bail:
1417 brelse(new_eb_bh);
1419 mlog_exit(status);
1420 return status;
1424 * Should only be called when there is no space left in any of the
1425 * leaf nodes. What we want to do is find the lowest tree depth
1426 * non-leaf extent block with room for new records. There are three
1427 * valid results of this search:
1429 * 1) a lowest extent block is found, then we pass it back in
1430 * *lowest_eb_bh and return '0'
1432 * 2) the search fails to find anything, but the root_el has room. We
1433 * pass NULL back in *lowest_eb_bh, but still return '0'
1435 * 3) the search fails to find anything AND the root_el is full, in
1436 * which case we return > 0
1438 * return status < 0 indicates an error.
1440 static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
1441 struct buffer_head **target_bh)
1443 int status = 0, i;
1444 u64 blkno;
1445 struct ocfs2_extent_block *eb;
1446 struct ocfs2_extent_list *el;
1447 struct buffer_head *bh = NULL;
1448 struct buffer_head *lowest_bh = NULL;
1450 mlog_entry_void();
1452 *target_bh = NULL;
1454 el = et->et_root_el;
1456 while(le16_to_cpu(el->l_tree_depth) > 1) {
1457 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1458 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1459 "Owner %llu has empty "
1460 "extent list (next_free_rec == 0)",
1461 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
1462 status = -EIO;
1463 goto bail;
1465 i = le16_to_cpu(el->l_next_free_rec) - 1;
1466 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1467 if (!blkno) {
1468 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1469 "Owner %llu has extent "
1470 "list where extent # %d has no physical "
1471 "block start",
1472 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
1473 status = -EIO;
1474 goto bail;
1477 brelse(bh);
1478 bh = NULL;
1480 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
1481 if (status < 0) {
1482 mlog_errno(status);
1483 goto bail;
1486 eb = (struct ocfs2_extent_block *) bh->b_data;
1487 el = &eb->h_list;
1489 if (le16_to_cpu(el->l_next_free_rec) <
1490 le16_to_cpu(el->l_count)) {
1491 brelse(lowest_bh);
1492 lowest_bh = bh;
1493 get_bh(lowest_bh);
1497 /* If we didn't find one and the fe doesn't have any room,
1498 * then return '1' */
1499 el = et->et_root_el;
1500 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
1501 status = 1;
1503 *target_bh = lowest_bh;
1504 bail:
1505 brelse(bh);
1507 mlog_exit(status);
1508 return status;
1512 * Grow a b-tree so that it has more records.
1514 * We might shift the tree depth in which case existing paths should
1515 * be considered invalid.
1517 * Tree depth after the grow is returned via *final_depth.
1519 * *last_eb_bh will be updated by ocfs2_add_branch().
1521 static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1522 int *final_depth, struct buffer_head **last_eb_bh,
1523 struct ocfs2_alloc_context *meta_ac)
1525 int ret, shift;
1526 struct ocfs2_extent_list *el = et->et_root_el;
1527 int depth = le16_to_cpu(el->l_tree_depth);
1528 struct buffer_head *bh = NULL;
1530 BUG_ON(meta_ac == NULL);
1532 shift = ocfs2_find_branch_target(et, &bh);
1533 if (shift < 0) {
1534 ret = shift;
1535 mlog_errno(ret);
1536 goto out;
1539 /* We traveled all the way to the bottom of the allocation tree
1540 * and didn't find room for any more extents - we need to add
1541 * another tree level */
1542 if (shift) {
1543 BUG_ON(bh);
1544 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1546 /* ocfs2_shift_tree_depth will return us a buffer with
1547 * the new extent block (so we can pass that to
1548 * ocfs2_add_branch). */
1549 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
1550 if (ret < 0) {
1551 mlog_errno(ret);
1552 goto out;
1554 depth++;
1555 if (depth == 1) {
1557 * Special case: we have room now if we shifted from
1558 * tree_depth 0, so no more work needs to be done.
1560 * We won't be calling add_branch, so pass
1561 * back *last_eb_bh as the new leaf. At depth
1562 * zero, it should always be null so there's
1563 * no reason to brelse.
1565 BUG_ON(*last_eb_bh);
1566 get_bh(bh);
1567 *last_eb_bh = bh;
1568 goto out;
1572 /* call ocfs2_add_branch to add the final part of the tree with
1573 * the new data. */
1574 mlog(0, "add branch. bh = %p\n", bh);
1575 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
1576 meta_ac);
1577 if (ret < 0) {
1578 mlog_errno(ret);
1579 goto out;
1582 out:
1583 if (final_depth)
1584 *final_depth = depth;
1585 brelse(bh);
1586 return ret;
1590 * This function will discard the rightmost extent record.
1592 static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1594 int next_free = le16_to_cpu(el->l_next_free_rec);
1595 int count = le16_to_cpu(el->l_count);
1596 unsigned int num_bytes;
1598 BUG_ON(!next_free);
1599 /* This will cause us to go off the end of our extent list. */
1600 BUG_ON(next_free >= count);
1602 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1604 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1607 static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1608 struct ocfs2_extent_rec *insert_rec)
1610 int i, insert_index, next_free, has_empty, num_bytes;
1611 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1612 struct ocfs2_extent_rec *rec;
1614 next_free = le16_to_cpu(el->l_next_free_rec);
1615 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1617 BUG_ON(!next_free);
1619 /* The tree code before us didn't allow enough room in the leaf. */
1620 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
1623 * The easiest way to approach this is to just remove the
1624 * empty extent and temporarily decrement next_free.
1626 if (has_empty) {
1628 * If next_free was 1 (only an empty extent), this
1629 * loop won't execute, which is fine. We still want
1630 * the decrement above to happen.
1632 for(i = 0; i < (next_free - 1); i++)
1633 el->l_recs[i] = el->l_recs[i+1];
1635 next_free--;
1639 * Figure out what the new record index should be.
1641 for(i = 0; i < next_free; i++) {
1642 rec = &el->l_recs[i];
1644 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1645 break;
1647 insert_index = i;
1649 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1650 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1652 BUG_ON(insert_index < 0);
1653 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1654 BUG_ON(insert_index > next_free);
1657 * No need to memmove if we're just adding to the tail.
1659 if (insert_index != next_free) {
1660 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1662 num_bytes = next_free - insert_index;
1663 num_bytes *= sizeof(struct ocfs2_extent_rec);
1664 memmove(&el->l_recs[insert_index + 1],
1665 &el->l_recs[insert_index],
1666 num_bytes);
1670 * Either we had an empty extent, and need to re-increment or
1671 * there was no empty extent on a non full rightmost leaf node,
1672 * in which case we still need to increment.
1674 next_free++;
1675 el->l_next_free_rec = cpu_to_le16(next_free);
1677 * Make sure none of the math above just messed up our tree.
1679 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1681 el->l_recs[insert_index] = *insert_rec;
1685 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1687 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1689 BUG_ON(num_recs == 0);
1691 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1692 num_recs--;
1693 size = num_recs * sizeof(struct ocfs2_extent_rec);
1694 memmove(&el->l_recs[0], &el->l_recs[1], size);
1695 memset(&el->l_recs[num_recs], 0,
1696 sizeof(struct ocfs2_extent_rec));
1697 el->l_next_free_rec = cpu_to_le16(num_recs);
1702 * Create an empty extent record .
1704 * l_next_free_rec may be updated.
1706 * If an empty extent already exists do nothing.
1708 static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1710 int next_free = le16_to_cpu(el->l_next_free_rec);
1712 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1714 if (next_free == 0)
1715 goto set_and_inc;
1717 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1718 return;
1720 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1721 "Asked to create an empty extent in a full list:\n"
1722 "count = %u, tree depth = %u",
1723 le16_to_cpu(el->l_count),
1724 le16_to_cpu(el->l_tree_depth));
1726 ocfs2_shift_records_right(el);
1728 set_and_inc:
1729 le16_add_cpu(&el->l_next_free_rec, 1);
1730 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1734 * For a rotation which involves two leaf nodes, the "root node" is
1735 * the lowest level tree node which contains a path to both leafs. This
1736 * resulting set of information can be used to form a complete "subtree"
1738 * This function is passed two full paths from the dinode down to a
1739 * pair of adjacent leaves. It's task is to figure out which path
1740 * index contains the subtree root - this can be the root index itself
1741 * in a worst-case rotation.
1743 * The array index of the subtree root is passed back.
1745 int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1746 struct ocfs2_path *left,
1747 struct ocfs2_path *right)
1749 int i = 0;
1752 * Check that the caller passed in two paths from the same tree.
1754 BUG_ON(path_root_bh(left) != path_root_bh(right));
1756 do {
1757 i++;
1760 * The caller didn't pass two adjacent paths.
1762 mlog_bug_on_msg(i > left->p_tree_depth,
1763 "Owner %llu, left depth %u, right depth %u\n"
1764 "left leaf blk %llu, right leaf blk %llu\n",
1765 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1766 left->p_tree_depth, right->p_tree_depth,
1767 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1768 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1769 } while (left->p_node[i].bh->b_blocknr ==
1770 right->p_node[i].bh->b_blocknr);
1772 return i - 1;
1775 typedef void (path_insert_t)(void *, struct buffer_head *);
1778 * Traverse a btree path in search of cpos, starting at root_el.
1780 * This code can be called with a cpos larger than the tree, in which
1781 * case it will return the rightmost path.
1783 static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
1784 struct ocfs2_extent_list *root_el, u32 cpos,
1785 path_insert_t *func, void *data)
1787 int i, ret = 0;
1788 u32 range;
1789 u64 blkno;
1790 struct buffer_head *bh = NULL;
1791 struct ocfs2_extent_block *eb;
1792 struct ocfs2_extent_list *el;
1793 struct ocfs2_extent_rec *rec;
1795 el = root_el;
1796 while (el->l_tree_depth) {
1797 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1798 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1799 "Owner %llu has empty extent list at "
1800 "depth %u\n",
1801 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1802 le16_to_cpu(el->l_tree_depth));
1803 ret = -EROFS;
1804 goto out;
1808 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1809 rec = &el->l_recs[i];
1812 * In the case that cpos is off the allocation
1813 * tree, this should just wind up returning the
1814 * rightmost record.
1816 range = le32_to_cpu(rec->e_cpos) +
1817 ocfs2_rec_clusters(el, rec);
1818 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1819 break;
1822 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1823 if (blkno == 0) {
1824 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1825 "Owner %llu has bad blkno in extent list "
1826 "at depth %u (index %d)\n",
1827 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1828 le16_to_cpu(el->l_tree_depth), i);
1829 ret = -EROFS;
1830 goto out;
1833 brelse(bh);
1834 bh = NULL;
1835 ret = ocfs2_read_extent_block(ci, blkno, &bh);
1836 if (ret) {
1837 mlog_errno(ret);
1838 goto out;
1841 eb = (struct ocfs2_extent_block *) bh->b_data;
1842 el = &eb->h_list;
1844 if (le16_to_cpu(el->l_next_free_rec) >
1845 le16_to_cpu(el->l_count)) {
1846 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1847 "Owner %llu has bad count in extent list "
1848 "at block %llu (next free=%u, count=%u)\n",
1849 (unsigned long long)ocfs2_metadata_cache_owner(ci),
1850 (unsigned long long)bh->b_blocknr,
1851 le16_to_cpu(el->l_next_free_rec),
1852 le16_to_cpu(el->l_count));
1853 ret = -EROFS;
1854 goto out;
1857 if (func)
1858 func(data, bh);
1861 out:
1863 * Catch any trailing bh that the loop didn't handle.
1865 brelse(bh);
1867 return ret;
1871 * Given an initialized path (that is, it has a valid root extent
1872 * list), this function will traverse the btree in search of the path
1873 * which would contain cpos.
1875 * The path traveled is recorded in the path structure.
1877 * Note that this will not do any comparisons on leaf node extent
1878 * records, so it will work fine in the case that we just added a tree
1879 * branch.
1881 struct find_path_data {
1882 int index;
1883 struct ocfs2_path *path;
1885 static void find_path_ins(void *data, struct buffer_head *bh)
1887 struct find_path_data *fp = data;
1889 get_bh(bh);
1890 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1891 fp->index++;
1893 int ocfs2_find_path(struct ocfs2_caching_info *ci,
1894 struct ocfs2_path *path, u32 cpos)
1896 struct find_path_data data;
1898 data.index = 1;
1899 data.path = path;
1900 return __ocfs2_find_path(ci, path_root_el(path), cpos,
1901 find_path_ins, &data);
1904 static void find_leaf_ins(void *data, struct buffer_head *bh)
1906 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1907 struct ocfs2_extent_list *el = &eb->h_list;
1908 struct buffer_head **ret = data;
1910 /* We want to retain only the leaf block. */
1911 if (le16_to_cpu(el->l_tree_depth) == 0) {
1912 get_bh(bh);
1913 *ret = bh;
1917 * Find the leaf block in the tree which would contain cpos. No
1918 * checking of the actual leaf is done.
1920 * Some paths want to call this instead of allocating a path structure
1921 * and calling ocfs2_find_path().
1923 * This function doesn't handle non btree extent lists.
1925 int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1926 struct ocfs2_extent_list *root_el, u32 cpos,
1927 struct buffer_head **leaf_bh)
1929 int ret;
1930 struct buffer_head *bh = NULL;
1932 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
1933 if (ret) {
1934 mlog_errno(ret);
1935 goto out;
1938 *leaf_bh = bh;
1939 out:
1940 return ret;
1944 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1946 * Basically, we've moved stuff around at the bottom of the tree and
1947 * we need to fix up the extent records above the changes to reflect
1948 * the new changes.
1950 * left_rec: the record on the left.
1951 * left_child_el: is the child list pointed to by left_rec
1952 * right_rec: the record to the right of left_rec
1953 * right_child_el: is the child list pointed to by right_rec
1955 * By definition, this only works on interior nodes.
1957 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1958 struct ocfs2_extent_list *left_child_el,
1959 struct ocfs2_extent_rec *right_rec,
1960 struct ocfs2_extent_list *right_child_el)
1962 u32 left_clusters, right_end;
1965 * Interior nodes never have holes. Their cpos is the cpos of
1966 * the leftmost record in their child list. Their cluster
1967 * count covers the full theoretical range of their child list
1968 * - the range between their cpos and the cpos of the record
1969 * immediately to their right.
1971 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
1972 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1973 BUG_ON(right_child_el->l_tree_depth);
1974 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1975 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1977 left_clusters -= le32_to_cpu(left_rec->e_cpos);
1978 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
1981 * Calculate the rightmost cluster count boundary before
1982 * moving cpos - we will need to adjust clusters after
1983 * updating e_cpos to keep the same highest cluster count.
1985 right_end = le32_to_cpu(right_rec->e_cpos);
1986 right_end += le32_to_cpu(right_rec->e_int_clusters);
1988 right_rec->e_cpos = left_rec->e_cpos;
1989 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1991 right_end -= le32_to_cpu(right_rec->e_cpos);
1992 right_rec->e_int_clusters = cpu_to_le32(right_end);
1996 * Adjust the adjacent root node records involved in a
1997 * rotation. left_el_blkno is passed in as a key so that we can easily
1998 * find it's index in the root list.
2000 static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
2001 struct ocfs2_extent_list *left_el,
2002 struct ocfs2_extent_list *right_el,
2003 u64 left_el_blkno)
2005 int i;
2007 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
2008 le16_to_cpu(left_el->l_tree_depth));
2010 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
2011 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
2012 break;
2016 * The path walking code should have never returned a root and
2017 * two paths which are not adjacent.
2019 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2021 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
2022 &root_el->l_recs[i + 1], right_el);
2026 * We've changed a leaf block (in right_path) and need to reflect that
2027 * change back up the subtree.
2029 * This happens in multiple places:
2030 * - When we've moved an extent record from the left path leaf to the right
2031 * path leaf to make room for an empty extent in the left path leaf.
2032 * - When our insert into the right path leaf is at the leftmost edge
2033 * and requires an update of the path immediately to it's left. This
2034 * can occur at the end of some types of rotation and appending inserts.
2035 * - When we've adjusted the last extent record in the left path leaf and the
2036 * 1st extent record in the right path leaf during cross extent block merge.
2038 static void ocfs2_complete_edge_insert(handle_t *handle,
2039 struct ocfs2_path *left_path,
2040 struct ocfs2_path *right_path,
2041 int subtree_index)
2043 int i, idx;
2044 struct ocfs2_extent_list *el, *left_el, *right_el;
2045 struct ocfs2_extent_rec *left_rec, *right_rec;
2046 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2049 * Update the counts and position values within all the
2050 * interior nodes to reflect the leaf rotation we just did.
2052 * The root node is handled below the loop.
2054 * We begin the loop with right_el and left_el pointing to the
2055 * leaf lists and work our way up.
2057 * NOTE: within this loop, left_el and right_el always refer
2058 * to the *child* lists.
2060 left_el = path_leaf_el(left_path);
2061 right_el = path_leaf_el(right_path);
2062 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2063 mlog(0, "Adjust records at index %u\n", i);
2066 * One nice property of knowing that all of these
2067 * nodes are below the root is that we only deal with
2068 * the leftmost right node record and the rightmost
2069 * left node record.
2071 el = left_path->p_node[i].el;
2072 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2073 left_rec = &el->l_recs[idx];
2075 el = right_path->p_node[i].el;
2076 right_rec = &el->l_recs[0];
2078 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2079 right_el);
2081 ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2082 ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2085 * Setup our list pointers now so that the current
2086 * parents become children in the next iteration.
2088 left_el = left_path->p_node[i].el;
2089 right_el = right_path->p_node[i].el;
2093 * At the root node, adjust the two adjacent records which
2094 * begin our path to the leaves.
2097 el = left_path->p_node[subtree_index].el;
2098 left_el = left_path->p_node[subtree_index + 1].el;
2099 right_el = right_path->p_node[subtree_index + 1].el;
2101 ocfs2_adjust_root_records(el, left_el, right_el,
2102 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2104 root_bh = left_path->p_node[subtree_index].bh;
2106 ocfs2_journal_dirty(handle, root_bh);
2109 static int ocfs2_rotate_subtree_right(handle_t *handle,
2110 struct ocfs2_extent_tree *et,
2111 struct ocfs2_path *left_path,
2112 struct ocfs2_path *right_path,
2113 int subtree_index)
2115 int ret, i;
2116 struct buffer_head *right_leaf_bh;
2117 struct buffer_head *left_leaf_bh = NULL;
2118 struct buffer_head *root_bh;
2119 struct ocfs2_extent_list *right_el, *left_el;
2120 struct ocfs2_extent_rec move_rec;
2122 left_leaf_bh = path_leaf_bh(left_path);
2123 left_el = path_leaf_el(left_path);
2125 if (left_el->l_next_free_rec != left_el->l_count) {
2126 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
2127 "Inode %llu has non-full interior leaf node %llu"
2128 "(next free = %u)",
2129 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2130 (unsigned long long)left_leaf_bh->b_blocknr,
2131 le16_to_cpu(left_el->l_next_free_rec));
2132 return -EROFS;
2136 * This extent block may already have an empty record, so we
2137 * return early if so.
2139 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2140 return 0;
2142 root_bh = left_path->p_node[subtree_index].bh;
2143 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2145 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2146 subtree_index);
2147 if (ret) {
2148 mlog_errno(ret);
2149 goto out;
2152 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2153 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2154 right_path, i);
2155 if (ret) {
2156 mlog_errno(ret);
2157 goto out;
2160 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2161 left_path, i);
2162 if (ret) {
2163 mlog_errno(ret);
2164 goto out;
2168 right_leaf_bh = path_leaf_bh(right_path);
2169 right_el = path_leaf_el(right_path);
2171 /* This is a code error, not a disk corruption. */
2172 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2173 "because rightmost leaf block %llu is empty\n",
2174 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2175 (unsigned long long)right_leaf_bh->b_blocknr);
2177 ocfs2_create_empty_extent(right_el);
2179 ocfs2_journal_dirty(handle, right_leaf_bh);
2181 /* Do the copy now. */
2182 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2183 move_rec = left_el->l_recs[i];
2184 right_el->l_recs[0] = move_rec;
2187 * Clear out the record we just copied and shift everything
2188 * over, leaving an empty extent in the left leaf.
2190 * We temporarily subtract from next_free_rec so that the
2191 * shift will lose the tail record (which is now defunct).
2193 le16_add_cpu(&left_el->l_next_free_rec, -1);
2194 ocfs2_shift_records_right(left_el);
2195 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2196 le16_add_cpu(&left_el->l_next_free_rec, 1);
2198 ocfs2_journal_dirty(handle, left_leaf_bh);
2200 ocfs2_complete_edge_insert(handle, left_path, right_path,
2201 subtree_index);
2203 out:
2204 return ret;
2208 * Given a full path, determine what cpos value would return us a path
2209 * containing the leaf immediately to the left of the current one.
2211 * Will return zero if the path passed in is already the leftmost path.
2213 int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2214 struct ocfs2_path *path, u32 *cpos)
2216 int i, j, ret = 0;
2217 u64 blkno;
2218 struct ocfs2_extent_list *el;
2220 BUG_ON(path->p_tree_depth == 0);
2222 *cpos = 0;
2224 blkno = path_leaf_bh(path)->b_blocknr;
2226 /* Start at the tree node just above the leaf and work our way up. */
2227 i = path->p_tree_depth - 1;
2228 while (i >= 0) {
2229 el = path->p_node[i].el;
2232 * Find the extent record just before the one in our
2233 * path.
2235 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2236 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2237 if (j == 0) {
2238 if (i == 0) {
2240 * We've determined that the
2241 * path specified is already
2242 * the leftmost one - return a
2243 * cpos of zero.
2245 goto out;
2248 * The leftmost record points to our
2249 * leaf - we need to travel up the
2250 * tree one level.
2252 goto next_node;
2255 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
2256 *cpos = *cpos + ocfs2_rec_clusters(el,
2257 &el->l_recs[j - 1]);
2258 *cpos = *cpos - 1;
2259 goto out;
2264 * If we got here, we never found a valid node where
2265 * the tree indicated one should be.
2267 ocfs2_error(sb,
2268 "Invalid extent tree at extent block %llu\n",
2269 (unsigned long long)blkno);
2270 ret = -EROFS;
2271 goto out;
2273 next_node:
2274 blkno = path->p_node[i].bh->b_blocknr;
2275 i--;
2278 out:
2279 return ret;
2283 * Extend the transaction by enough credits to complete the rotation,
2284 * and still leave at least the original number of credits allocated
2285 * to this transaction.
2287 static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
2288 int op_credits,
2289 struct ocfs2_path *path)
2291 int ret = 0;
2292 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
2294 if (handle->h_buffer_credits < credits)
2295 ret = ocfs2_extend_trans(handle,
2296 credits - handle->h_buffer_credits);
2298 return ret;
2302 * Trap the case where we're inserting into the theoretical range past
2303 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2304 * whose cpos is less than ours into the right leaf.
2306 * It's only necessary to look at the rightmost record of the left
2307 * leaf because the logic that calls us should ensure that the
2308 * theoretical ranges in the path components above the leaves are
2309 * correct.
2311 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2312 u32 insert_cpos)
2314 struct ocfs2_extent_list *left_el;
2315 struct ocfs2_extent_rec *rec;
2316 int next_free;
2318 left_el = path_leaf_el(left_path);
2319 next_free = le16_to_cpu(left_el->l_next_free_rec);
2320 rec = &left_el->l_recs[next_free - 1];
2322 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2323 return 1;
2324 return 0;
2327 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2329 int next_free = le16_to_cpu(el->l_next_free_rec);
2330 unsigned int range;
2331 struct ocfs2_extent_rec *rec;
2333 if (next_free == 0)
2334 return 0;
2336 rec = &el->l_recs[0];
2337 if (ocfs2_is_empty_extent(rec)) {
2338 /* Empty list. */
2339 if (next_free == 1)
2340 return 0;
2341 rec = &el->l_recs[1];
2344 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2345 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2346 return 1;
2347 return 0;
2351 * Rotate all the records in a btree right one record, starting at insert_cpos.
2353 * The path to the rightmost leaf should be passed in.
2355 * The array is assumed to be large enough to hold an entire path (tree depth).
2357 * Upon successful return from this function:
2359 * - The 'right_path' array will contain a path to the leaf block
2360 * whose range contains e_cpos.
2361 * - That leaf block will have a single empty extent in list index 0.
2362 * - In the case that the rotation requires a post-insert update,
2363 * *ret_left_path will contain a valid path which can be passed to
2364 * ocfs2_insert_path().
2366 static int ocfs2_rotate_tree_right(handle_t *handle,
2367 struct ocfs2_extent_tree *et,
2368 enum ocfs2_split_type split,
2369 u32 insert_cpos,
2370 struct ocfs2_path *right_path,
2371 struct ocfs2_path **ret_left_path)
2373 int ret, start, orig_credits = handle->h_buffer_credits;
2374 u32 cpos;
2375 struct ocfs2_path *left_path = NULL;
2376 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2378 *ret_left_path = NULL;
2380 left_path = ocfs2_new_path_from_path(right_path);
2381 if (!left_path) {
2382 ret = -ENOMEM;
2383 mlog_errno(ret);
2384 goto out;
2387 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2388 if (ret) {
2389 mlog_errno(ret);
2390 goto out;
2393 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2396 * What we want to do here is:
2398 * 1) Start with the rightmost path.
2400 * 2) Determine a path to the leaf block directly to the left
2401 * of that leaf.
2403 * 3) Determine the 'subtree root' - the lowest level tree node
2404 * which contains a path to both leaves.
2406 * 4) Rotate the subtree.
2408 * 5) Find the next subtree by considering the left path to be
2409 * the new right path.
2411 * The check at the top of this while loop also accepts
2412 * insert_cpos == cpos because cpos is only a _theoretical_
2413 * value to get us the left path - insert_cpos might very well
2414 * be filling that hole.
2416 * Stop at a cpos of '0' because we either started at the
2417 * leftmost branch (i.e., a tree with one branch and a
2418 * rotation inside of it), or we've gone as far as we can in
2419 * rotating subtrees.
2421 while (cpos && insert_cpos <= cpos) {
2422 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2423 insert_cpos, cpos);
2425 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
2426 if (ret) {
2427 mlog_errno(ret);
2428 goto out;
2431 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2432 path_leaf_bh(right_path),
2433 "Owner %llu: error during insert of %u "
2434 "(left path cpos %u) results in two identical "
2435 "paths ending at %llu\n",
2436 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2437 insert_cpos, cpos,
2438 (unsigned long long)
2439 path_leaf_bh(left_path)->b_blocknr);
2441 if (split == SPLIT_NONE &&
2442 ocfs2_rotate_requires_path_adjustment(left_path,
2443 insert_cpos)) {
2446 * We've rotated the tree as much as we
2447 * should. The rest is up to
2448 * ocfs2_insert_path() to complete, after the
2449 * record insertion. We indicate this
2450 * situation by returning the left path.
2452 * The reason we don't adjust the records here
2453 * before the record insert is that an error
2454 * later might break the rule where a parent
2455 * record e_cpos will reflect the actual
2456 * e_cpos of the 1st nonempty record of the
2457 * child list.
2459 *ret_left_path = left_path;
2460 goto out_ret_path;
2463 start = ocfs2_find_subtree_root(et, left_path, right_path);
2465 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2466 start,
2467 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2468 right_path->p_tree_depth);
2470 ret = ocfs2_extend_rotate_transaction(handle, start,
2471 orig_credits, right_path);
2472 if (ret) {
2473 mlog_errno(ret);
2474 goto out;
2477 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
2478 right_path, start);
2479 if (ret) {
2480 mlog_errno(ret);
2481 goto out;
2484 if (split != SPLIT_NONE &&
2485 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2486 insert_cpos)) {
2488 * A rotate moves the rightmost left leaf
2489 * record over to the leftmost right leaf
2490 * slot. If we're doing an extent split
2491 * instead of a real insert, then we have to
2492 * check that the extent to be split wasn't
2493 * just moved over. If it was, then we can
2494 * exit here, passing left_path back -
2495 * ocfs2_split_extent() is smart enough to
2496 * search both leaves.
2498 *ret_left_path = left_path;
2499 goto out_ret_path;
2503 * There is no need to re-read the next right path
2504 * as we know that it'll be our current left
2505 * path. Optimize by copying values instead.
2507 ocfs2_mv_path(right_path, left_path);
2509 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2510 if (ret) {
2511 mlog_errno(ret);
2512 goto out;
2516 out:
2517 ocfs2_free_path(left_path);
2519 out_ret_path:
2520 return ret;
2523 static int ocfs2_update_edge_lengths(handle_t *handle,
2524 struct ocfs2_extent_tree *et,
2525 int subtree_index, struct ocfs2_path *path)
2527 int i, idx, ret;
2528 struct ocfs2_extent_rec *rec;
2529 struct ocfs2_extent_list *el;
2530 struct ocfs2_extent_block *eb;
2531 u32 range;
2534 * In normal tree rotation process, we will never touch the
2535 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2536 * doesn't reserve the credits for them either.
2538 * But we do have a special case here which will update the rightmost
2539 * records for all the bh in the path.
2540 * So we have to allocate extra credits and access them.
2542 ret = ocfs2_extend_trans(handle, subtree_index);
2543 if (ret) {
2544 mlog_errno(ret);
2545 goto out;
2548 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
2549 if (ret) {
2550 mlog_errno(ret);
2551 goto out;
2554 /* Path should always be rightmost. */
2555 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2556 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2558 el = &eb->h_list;
2559 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2560 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2561 rec = &el->l_recs[idx];
2562 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2564 for (i = 0; i < path->p_tree_depth; i++) {
2565 el = path->p_node[i].el;
2566 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2567 rec = &el->l_recs[idx];
2569 rec->e_int_clusters = cpu_to_le32(range);
2570 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2572 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2574 out:
2575 return ret;
2578 static void ocfs2_unlink_path(handle_t *handle,
2579 struct ocfs2_extent_tree *et,
2580 struct ocfs2_cached_dealloc_ctxt *dealloc,
2581 struct ocfs2_path *path, int unlink_start)
2583 int ret, i;
2584 struct ocfs2_extent_block *eb;
2585 struct ocfs2_extent_list *el;
2586 struct buffer_head *bh;
2588 for(i = unlink_start; i < path_num_items(path); i++) {
2589 bh = path->p_node[i].bh;
2591 eb = (struct ocfs2_extent_block *)bh->b_data;
2593 * Not all nodes might have had their final count
2594 * decremented by the caller - handle this here.
2596 el = &eb->h_list;
2597 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2598 mlog(ML_ERROR,
2599 "Inode %llu, attempted to remove extent block "
2600 "%llu with %u records\n",
2601 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2602 (unsigned long long)le64_to_cpu(eb->h_blkno),
2603 le16_to_cpu(el->l_next_free_rec));
2605 ocfs2_journal_dirty(handle, bh);
2606 ocfs2_remove_from_cache(et->et_ci, bh);
2607 continue;
2610 el->l_next_free_rec = 0;
2611 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2613 ocfs2_journal_dirty(handle, bh);
2615 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2616 if (ret)
2617 mlog_errno(ret);
2619 ocfs2_remove_from_cache(et->et_ci, bh);
2623 static void ocfs2_unlink_subtree(handle_t *handle,
2624 struct ocfs2_extent_tree *et,
2625 struct ocfs2_path *left_path,
2626 struct ocfs2_path *right_path,
2627 int subtree_index,
2628 struct ocfs2_cached_dealloc_ctxt *dealloc)
2630 int i;
2631 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2632 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2633 struct ocfs2_extent_list *el;
2634 struct ocfs2_extent_block *eb;
2636 el = path_leaf_el(left_path);
2638 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2640 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2641 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2642 break;
2644 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2646 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2647 le16_add_cpu(&root_el->l_next_free_rec, -1);
2649 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2650 eb->h_next_leaf_blk = 0;
2652 ocfs2_journal_dirty(handle, root_bh);
2653 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2655 ocfs2_unlink_path(handle, et, dealloc, right_path,
2656 subtree_index + 1);
2659 static int ocfs2_rotate_subtree_left(handle_t *handle,
2660 struct ocfs2_extent_tree *et,
2661 struct ocfs2_path *left_path,
2662 struct ocfs2_path *right_path,
2663 int subtree_index,
2664 struct ocfs2_cached_dealloc_ctxt *dealloc,
2665 int *deleted)
2667 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2668 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2669 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2670 struct ocfs2_extent_block *eb;
2672 *deleted = 0;
2674 right_leaf_el = path_leaf_el(right_path);
2675 left_leaf_el = path_leaf_el(left_path);
2676 root_bh = left_path->p_node[subtree_index].bh;
2677 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2679 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2680 return 0;
2682 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2683 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2685 * It's legal for us to proceed if the right leaf is
2686 * the rightmost one and it has an empty extent. There
2687 * are two cases to handle - whether the leaf will be
2688 * empty after removal or not. If the leaf isn't empty
2689 * then just remove the empty extent up front. The
2690 * next block will handle empty leaves by flagging
2691 * them for unlink.
2693 * Non rightmost leaves will throw -EAGAIN and the
2694 * caller can manually move the subtree and retry.
2697 if (eb->h_next_leaf_blk != 0ULL)
2698 return -EAGAIN;
2700 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2701 ret = ocfs2_journal_access_eb(handle, et->et_ci,
2702 path_leaf_bh(right_path),
2703 OCFS2_JOURNAL_ACCESS_WRITE);
2704 if (ret) {
2705 mlog_errno(ret);
2706 goto out;
2709 ocfs2_remove_empty_extent(right_leaf_el);
2710 } else
2711 right_has_empty = 1;
2714 if (eb->h_next_leaf_blk == 0ULL &&
2715 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2717 * We have to update i_last_eb_blk during the meta
2718 * data delete.
2720 ret = ocfs2_et_root_journal_access(handle, et,
2721 OCFS2_JOURNAL_ACCESS_WRITE);
2722 if (ret) {
2723 mlog_errno(ret);
2724 goto out;
2727 del_right_subtree = 1;
2731 * Getting here with an empty extent in the right path implies
2732 * that it's the rightmost path and will be deleted.
2734 BUG_ON(right_has_empty && !del_right_subtree);
2736 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2737 subtree_index);
2738 if (ret) {
2739 mlog_errno(ret);
2740 goto out;
2743 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2744 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2745 right_path, i);
2746 if (ret) {
2747 mlog_errno(ret);
2748 goto out;
2751 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2752 left_path, i);
2753 if (ret) {
2754 mlog_errno(ret);
2755 goto out;
2759 if (!right_has_empty) {
2761 * Only do this if we're moving a real
2762 * record. Otherwise, the action is delayed until
2763 * after removal of the right path in which case we
2764 * can do a simple shift to remove the empty extent.
2766 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2767 memset(&right_leaf_el->l_recs[0], 0,
2768 sizeof(struct ocfs2_extent_rec));
2770 if (eb->h_next_leaf_blk == 0ULL) {
2772 * Move recs over to get rid of empty extent, decrease
2773 * next_free. This is allowed to remove the last
2774 * extent in our leaf (setting l_next_free_rec to
2775 * zero) - the delete code below won't care.
2777 ocfs2_remove_empty_extent(right_leaf_el);
2780 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2781 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2783 if (del_right_subtree) {
2784 ocfs2_unlink_subtree(handle, et, left_path, right_path,
2785 subtree_index, dealloc);
2786 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
2787 left_path);
2788 if (ret) {
2789 mlog_errno(ret);
2790 goto out;
2793 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2794 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2797 * Removal of the extent in the left leaf was skipped
2798 * above so we could delete the right path
2799 * 1st.
2801 if (right_has_empty)
2802 ocfs2_remove_empty_extent(left_leaf_el);
2804 ocfs2_journal_dirty(handle, et_root_bh);
2806 *deleted = 1;
2807 } else
2808 ocfs2_complete_edge_insert(handle, left_path, right_path,
2809 subtree_index);
2811 out:
2812 return ret;
2816 * Given a full path, determine what cpos value would return us a path
2817 * containing the leaf immediately to the right of the current one.
2819 * Will return zero if the path passed in is already the rightmost path.
2821 * This looks similar, but is subtly different to
2822 * ocfs2_find_cpos_for_left_leaf().
2824 int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2825 struct ocfs2_path *path, u32 *cpos)
2827 int i, j, ret = 0;
2828 u64 blkno;
2829 struct ocfs2_extent_list *el;
2831 *cpos = 0;
2833 if (path->p_tree_depth == 0)
2834 return 0;
2836 blkno = path_leaf_bh(path)->b_blocknr;
2838 /* Start at the tree node just above the leaf and work our way up. */
2839 i = path->p_tree_depth - 1;
2840 while (i >= 0) {
2841 int next_free;
2843 el = path->p_node[i].el;
2846 * Find the extent record just after the one in our
2847 * path.
2849 next_free = le16_to_cpu(el->l_next_free_rec);
2850 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2851 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2852 if (j == (next_free - 1)) {
2853 if (i == 0) {
2855 * We've determined that the
2856 * path specified is already
2857 * the rightmost one - return a
2858 * cpos of zero.
2860 goto out;
2863 * The rightmost record points to our
2864 * leaf - we need to travel up the
2865 * tree one level.
2867 goto next_node;
2870 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2871 goto out;
2876 * If we got here, we never found a valid node where
2877 * the tree indicated one should be.
2879 ocfs2_error(sb,
2880 "Invalid extent tree at extent block %llu\n",
2881 (unsigned long long)blkno);
2882 ret = -EROFS;
2883 goto out;
2885 next_node:
2886 blkno = path->p_node[i].bh->b_blocknr;
2887 i--;
2890 out:
2891 return ret;
2894 static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2895 struct ocfs2_extent_tree *et,
2896 struct ocfs2_path *path)
2898 int ret;
2899 struct buffer_head *bh = path_leaf_bh(path);
2900 struct ocfs2_extent_list *el = path_leaf_el(path);
2902 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2903 return 0;
2905 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
2906 path_num_items(path) - 1);
2907 if (ret) {
2908 mlog_errno(ret);
2909 goto out;
2912 ocfs2_remove_empty_extent(el);
2913 ocfs2_journal_dirty(handle, bh);
2915 out:
2916 return ret;
2919 static int __ocfs2_rotate_tree_left(handle_t *handle,
2920 struct ocfs2_extent_tree *et,
2921 int orig_credits,
2922 struct ocfs2_path *path,
2923 struct ocfs2_cached_dealloc_ctxt *dealloc,
2924 struct ocfs2_path **empty_extent_path)
2926 int ret, subtree_root, deleted;
2927 u32 right_cpos;
2928 struct ocfs2_path *left_path = NULL;
2929 struct ocfs2_path *right_path = NULL;
2930 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2932 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2934 *empty_extent_path = NULL;
2936 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
2937 if (ret) {
2938 mlog_errno(ret);
2939 goto out;
2942 left_path = ocfs2_new_path_from_path(path);
2943 if (!left_path) {
2944 ret = -ENOMEM;
2945 mlog_errno(ret);
2946 goto out;
2949 ocfs2_cp_path(left_path, path);
2951 right_path = ocfs2_new_path_from_path(path);
2952 if (!right_path) {
2953 ret = -ENOMEM;
2954 mlog_errno(ret);
2955 goto out;
2958 while (right_cpos) {
2959 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
2960 if (ret) {
2961 mlog_errno(ret);
2962 goto out;
2965 subtree_root = ocfs2_find_subtree_root(et, left_path,
2966 right_path);
2968 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2969 subtree_root,
2970 (unsigned long long)
2971 right_path->p_node[subtree_root].bh->b_blocknr,
2972 right_path->p_tree_depth);
2974 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2975 orig_credits, left_path);
2976 if (ret) {
2977 mlog_errno(ret);
2978 goto out;
2982 * Caller might still want to make changes to the
2983 * tree root, so re-add it to the journal here.
2985 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2986 left_path, 0);
2987 if (ret) {
2988 mlog_errno(ret);
2989 goto out;
2992 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
2993 right_path, subtree_root,
2994 dealloc, &deleted);
2995 if (ret == -EAGAIN) {
2997 * The rotation has to temporarily stop due to
2998 * the right subtree having an empty
2999 * extent. Pass it back to the caller for a
3000 * fixup.
3002 *empty_extent_path = right_path;
3003 right_path = NULL;
3004 goto out;
3006 if (ret) {
3007 mlog_errno(ret);
3008 goto out;
3012 * The subtree rotate might have removed records on
3013 * the rightmost edge. If so, then rotation is
3014 * complete.
3016 if (deleted)
3017 break;
3019 ocfs2_mv_path(left_path, right_path);
3021 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
3022 &right_cpos);
3023 if (ret) {
3024 mlog_errno(ret);
3025 goto out;
3029 out:
3030 ocfs2_free_path(right_path);
3031 ocfs2_free_path(left_path);
3033 return ret;
3036 static int ocfs2_remove_rightmost_path(handle_t *handle,
3037 struct ocfs2_extent_tree *et,
3038 struct ocfs2_path *path,
3039 struct ocfs2_cached_dealloc_ctxt *dealloc)
3041 int ret, subtree_index;
3042 u32 cpos;
3043 struct ocfs2_path *left_path = NULL;
3044 struct ocfs2_extent_block *eb;
3045 struct ocfs2_extent_list *el;
3048 ret = ocfs2_et_sanity_check(et);
3049 if (ret)
3050 goto out;
3052 * There's two ways we handle this depending on
3053 * whether path is the only existing one.
3055 ret = ocfs2_extend_rotate_transaction(handle, 0,
3056 handle->h_buffer_credits,
3057 path);
3058 if (ret) {
3059 mlog_errno(ret);
3060 goto out;
3063 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
3064 if (ret) {
3065 mlog_errno(ret);
3066 goto out;
3069 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3070 path, &cpos);
3071 if (ret) {
3072 mlog_errno(ret);
3073 goto out;
3076 if (cpos) {
3078 * We have a path to the left of this one - it needs
3079 * an update too.
3081 left_path = ocfs2_new_path_from_path(path);
3082 if (!left_path) {
3083 ret = -ENOMEM;
3084 mlog_errno(ret);
3085 goto out;
3088 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
3089 if (ret) {
3090 mlog_errno(ret);
3091 goto out;
3094 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
3095 if (ret) {
3096 mlog_errno(ret);
3097 goto out;
3100 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
3102 ocfs2_unlink_subtree(handle, et, left_path, path,
3103 subtree_index, dealloc);
3104 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
3105 left_path);
3106 if (ret) {
3107 mlog_errno(ret);
3108 goto out;
3111 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
3112 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
3113 } else {
3115 * 'path' is also the leftmost path which
3116 * means it must be the only one. This gets
3117 * handled differently because we want to
3118 * revert the root back to having extents
3119 * in-line.
3121 ocfs2_unlink_path(handle, et, dealloc, path, 1);
3123 el = et->et_root_el;
3124 el->l_tree_depth = 0;
3125 el->l_next_free_rec = 0;
3126 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3128 ocfs2_et_set_last_eb_blk(et, 0);
3131 ocfs2_journal_dirty(handle, path_root_bh(path));
3133 out:
3134 ocfs2_free_path(left_path);
3135 return ret;
3139 * Left rotation of btree records.
3141 * In many ways, this is (unsurprisingly) the opposite of right
3142 * rotation. We start at some non-rightmost path containing an empty
3143 * extent in the leaf block. The code works its way to the rightmost
3144 * path by rotating records to the left in every subtree.
3146 * This is used by any code which reduces the number of extent records
3147 * in a leaf. After removal, an empty record should be placed in the
3148 * leftmost list position.
3150 * This won't handle a length update of the rightmost path records if
3151 * the rightmost tree leaf record is removed so the caller is
3152 * responsible for detecting and correcting that.
3154 static int ocfs2_rotate_tree_left(handle_t *handle,
3155 struct ocfs2_extent_tree *et,
3156 struct ocfs2_path *path,
3157 struct ocfs2_cached_dealloc_ctxt *dealloc)
3159 int ret, orig_credits = handle->h_buffer_credits;
3160 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3161 struct ocfs2_extent_block *eb;
3162 struct ocfs2_extent_list *el;
3164 el = path_leaf_el(path);
3165 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3166 return 0;
3168 if (path->p_tree_depth == 0) {
3169 rightmost_no_delete:
3171 * Inline extents. This is trivially handled, so do
3172 * it up front.
3174 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
3175 if (ret)
3176 mlog_errno(ret);
3177 goto out;
3181 * Handle rightmost branch now. There's several cases:
3182 * 1) simple rotation leaving records in there. That's trivial.
3183 * 2) rotation requiring a branch delete - there's no more
3184 * records left. Two cases of this:
3185 * a) There are branches to the left.
3186 * b) This is also the leftmost (the only) branch.
3188 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3189 * 2a) we need the left branch so that we can update it with the unlink
3190 * 2b) we need to bring the root back to inline extents.
3193 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3194 el = &eb->h_list;
3195 if (eb->h_next_leaf_blk == 0) {
3197 * This gets a bit tricky if we're going to delete the
3198 * rightmost path. Get the other cases out of the way
3199 * 1st.
3201 if (le16_to_cpu(el->l_next_free_rec) > 1)
3202 goto rightmost_no_delete;
3204 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3205 ret = -EIO;
3206 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3207 "Owner %llu has empty extent block at %llu",
3208 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
3209 (unsigned long long)le64_to_cpu(eb->h_blkno));
3210 goto out;
3214 ret = ocfs2_remove_rightmost_path(handle, et, path,
3215 dealloc);
3216 if (ret)
3217 mlog_errno(ret);
3218 goto out;
3222 * Now we can loop, remembering the path we get from -EAGAIN
3223 * and restarting from there.
3225 try_rotate:
3226 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3227 dealloc, &restart_path);
3228 if (ret && ret != -EAGAIN) {
3229 mlog_errno(ret);
3230 goto out;
3233 while (ret == -EAGAIN) {
3234 tmp_path = restart_path;
3235 restart_path = NULL;
3237 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
3238 tmp_path, dealloc,
3239 &restart_path);
3240 if (ret && ret != -EAGAIN) {
3241 mlog_errno(ret);
3242 goto out;
3245 ocfs2_free_path(tmp_path);
3246 tmp_path = NULL;
3248 if (ret == 0)
3249 goto try_rotate;
3252 out:
3253 ocfs2_free_path(tmp_path);
3254 ocfs2_free_path(restart_path);
3255 return ret;
3258 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3259 int index)
3261 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3262 unsigned int size;
3264 if (rec->e_leaf_clusters == 0) {
3266 * We consumed all of the merged-from record. An empty
3267 * extent cannot exist anywhere but the 1st array
3268 * position, so move things over if the merged-from
3269 * record doesn't occupy that position.
3271 * This creates a new empty extent so the caller
3272 * should be smart enough to have removed any existing
3273 * ones.
3275 if (index > 0) {
3276 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3277 size = index * sizeof(struct ocfs2_extent_rec);
3278 memmove(&el->l_recs[1], &el->l_recs[0], size);
3282 * Always memset - the caller doesn't check whether it
3283 * created an empty extent, so there could be junk in
3284 * the other fields.
3286 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3290 static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
3291 struct ocfs2_path *left_path,
3292 struct ocfs2_path **ret_right_path)
3294 int ret;
3295 u32 right_cpos;
3296 struct ocfs2_path *right_path = NULL;
3297 struct ocfs2_extent_list *left_el;
3299 *ret_right_path = NULL;
3301 /* This function shouldn't be called for non-trees. */
3302 BUG_ON(left_path->p_tree_depth == 0);
3304 left_el = path_leaf_el(left_path);
3305 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3307 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3308 left_path, &right_cpos);
3309 if (ret) {
3310 mlog_errno(ret);
3311 goto out;
3314 /* This function shouldn't be called for the rightmost leaf. */
3315 BUG_ON(right_cpos == 0);
3317 right_path = ocfs2_new_path_from_path(left_path);
3318 if (!right_path) {
3319 ret = -ENOMEM;
3320 mlog_errno(ret);
3321 goto out;
3324 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
3325 if (ret) {
3326 mlog_errno(ret);
3327 goto out;
3330 *ret_right_path = right_path;
3331 out:
3332 if (ret)
3333 ocfs2_free_path(right_path);
3334 return ret;
3338 * Remove split_rec clusters from the record at index and merge them
3339 * onto the beginning of the record "next" to it.
3340 * For index < l_count - 1, the next means the extent rec at index + 1.
3341 * For index == l_count - 1, the "next" means the 1st extent rec of the
3342 * next extent block.
3344 static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
3345 handle_t *handle,
3346 struct ocfs2_extent_tree *et,
3347 struct ocfs2_extent_rec *split_rec,
3348 int index)
3350 int ret, next_free, i;
3351 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3352 struct ocfs2_extent_rec *left_rec;
3353 struct ocfs2_extent_rec *right_rec;
3354 struct ocfs2_extent_list *right_el;
3355 struct ocfs2_path *right_path = NULL;
3356 int subtree_index = 0;
3357 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3358 struct buffer_head *bh = path_leaf_bh(left_path);
3359 struct buffer_head *root_bh = NULL;
3361 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3362 left_rec = &el->l_recs[index];
3364 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3365 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3366 /* we meet with a cross extent block merge. */
3367 ret = ocfs2_get_right_path(et, left_path, &right_path);
3368 if (ret) {
3369 mlog_errno(ret);
3370 goto out;
3373 right_el = path_leaf_el(right_path);
3374 next_free = le16_to_cpu(right_el->l_next_free_rec);
3375 BUG_ON(next_free <= 0);
3376 right_rec = &right_el->l_recs[0];
3377 if (ocfs2_is_empty_extent(right_rec)) {
3378 BUG_ON(next_free <= 1);
3379 right_rec = &right_el->l_recs[1];
3382 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3383 le16_to_cpu(left_rec->e_leaf_clusters) !=
3384 le32_to_cpu(right_rec->e_cpos));
3386 subtree_index = ocfs2_find_subtree_root(et, left_path,
3387 right_path);
3389 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3390 handle->h_buffer_credits,
3391 right_path);
3392 if (ret) {
3393 mlog_errno(ret);
3394 goto out;
3397 root_bh = left_path->p_node[subtree_index].bh;
3398 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3400 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3401 subtree_index);
3402 if (ret) {
3403 mlog_errno(ret);
3404 goto out;
3407 for (i = subtree_index + 1;
3408 i < path_num_items(right_path); i++) {
3409 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3410 right_path, i);
3411 if (ret) {
3412 mlog_errno(ret);
3413 goto out;
3416 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3417 left_path, i);
3418 if (ret) {
3419 mlog_errno(ret);
3420 goto out;
3424 } else {
3425 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3426 right_rec = &el->l_recs[index + 1];
3429 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
3430 path_num_items(left_path) - 1);
3431 if (ret) {
3432 mlog_errno(ret);
3433 goto out;
3436 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3438 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3439 le64_add_cpu(&right_rec->e_blkno,
3440 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3441 split_clusters));
3442 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3444 ocfs2_cleanup_merge(el, index);
3446 ocfs2_journal_dirty(handle, bh);
3447 if (right_path) {
3448 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3449 ocfs2_complete_edge_insert(handle, left_path, right_path,
3450 subtree_index);
3452 out:
3453 if (right_path)
3454 ocfs2_free_path(right_path);
3455 return ret;
3458 static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
3459 struct ocfs2_path *right_path,
3460 struct ocfs2_path **ret_left_path)
3462 int ret;
3463 u32 left_cpos;
3464 struct ocfs2_path *left_path = NULL;
3466 *ret_left_path = NULL;
3468 /* This function shouldn't be called for non-trees. */
3469 BUG_ON(right_path->p_tree_depth == 0);
3471 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3472 right_path, &left_cpos);
3473 if (ret) {
3474 mlog_errno(ret);
3475 goto out;
3478 /* This function shouldn't be called for the leftmost leaf. */
3479 BUG_ON(left_cpos == 0);
3481 left_path = ocfs2_new_path_from_path(right_path);
3482 if (!left_path) {
3483 ret = -ENOMEM;
3484 mlog_errno(ret);
3485 goto out;
3488 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
3489 if (ret) {
3490 mlog_errno(ret);
3491 goto out;
3494 *ret_left_path = left_path;
3495 out:
3496 if (ret)
3497 ocfs2_free_path(left_path);
3498 return ret;
3502 * Remove split_rec clusters from the record at index and merge them
3503 * onto the tail of the record "before" it.
3504 * For index > 0, the "before" means the extent rec at index - 1.
3506 * For index == 0, the "before" means the last record of the previous
3507 * extent block. And there is also a situation that we may need to
3508 * remove the rightmost leaf extent block in the right_path and change
3509 * the right path to indicate the new rightmost path.
3511 static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
3512 handle_t *handle,
3513 struct ocfs2_extent_tree *et,
3514 struct ocfs2_extent_rec *split_rec,
3515 struct ocfs2_cached_dealloc_ctxt *dealloc,
3516 int index)
3518 int ret, i, subtree_index = 0, has_empty_extent = 0;
3519 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3520 struct ocfs2_extent_rec *left_rec;
3521 struct ocfs2_extent_rec *right_rec;
3522 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3523 struct buffer_head *bh = path_leaf_bh(right_path);
3524 struct buffer_head *root_bh = NULL;
3525 struct ocfs2_path *left_path = NULL;
3526 struct ocfs2_extent_list *left_el;
3528 BUG_ON(index < 0);
3530 right_rec = &el->l_recs[index];
3531 if (index == 0) {
3532 /* we meet with a cross extent block merge. */
3533 ret = ocfs2_get_left_path(et, right_path, &left_path);
3534 if (ret) {
3535 mlog_errno(ret);
3536 goto out;
3539 left_el = path_leaf_el(left_path);
3540 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3541 le16_to_cpu(left_el->l_count));
3543 left_rec = &left_el->l_recs[
3544 le16_to_cpu(left_el->l_next_free_rec) - 1];
3545 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3546 le16_to_cpu(left_rec->e_leaf_clusters) !=
3547 le32_to_cpu(split_rec->e_cpos));
3549 subtree_index = ocfs2_find_subtree_root(et, left_path,
3550 right_path);
3552 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3553 handle->h_buffer_credits,
3554 left_path);
3555 if (ret) {
3556 mlog_errno(ret);
3557 goto out;
3560 root_bh = left_path->p_node[subtree_index].bh;
3561 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3563 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3564 subtree_index);
3565 if (ret) {
3566 mlog_errno(ret);
3567 goto out;
3570 for (i = subtree_index + 1;
3571 i < path_num_items(right_path); i++) {
3572 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3573 right_path, i);
3574 if (ret) {
3575 mlog_errno(ret);
3576 goto out;
3579 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3580 left_path, i);
3581 if (ret) {
3582 mlog_errno(ret);
3583 goto out;
3586 } else {
3587 left_rec = &el->l_recs[index - 1];
3588 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3589 has_empty_extent = 1;
3592 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3593 path_num_items(right_path) - 1);
3594 if (ret) {
3595 mlog_errno(ret);
3596 goto out;
3599 if (has_empty_extent && index == 1) {
3601 * The easy case - we can just plop the record right in.
3603 *left_rec = *split_rec;
3605 has_empty_extent = 0;
3606 } else
3607 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3609 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3610 le64_add_cpu(&right_rec->e_blkno,
3611 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3612 split_clusters));
3613 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3615 ocfs2_cleanup_merge(el, index);
3617 ocfs2_journal_dirty(handle, bh);
3618 if (left_path) {
3619 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3622 * In the situation that the right_rec is empty and the extent
3623 * block is empty also, ocfs2_complete_edge_insert can't handle
3624 * it and we need to delete the right extent block.
3626 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3627 le16_to_cpu(el->l_next_free_rec) == 1) {
3629 ret = ocfs2_remove_rightmost_path(handle, et,
3630 right_path,
3631 dealloc);
3632 if (ret) {
3633 mlog_errno(ret);
3634 goto out;
3637 /* Now the rightmost extent block has been deleted.
3638 * So we use the new rightmost path.
3640 ocfs2_mv_path(right_path, left_path);
3641 left_path = NULL;
3642 } else
3643 ocfs2_complete_edge_insert(handle, left_path,
3644 right_path, subtree_index);
3646 out:
3647 if (left_path)
3648 ocfs2_free_path(left_path);
3649 return ret;
3652 static int ocfs2_try_to_merge_extent(handle_t *handle,
3653 struct ocfs2_extent_tree *et,
3654 struct ocfs2_path *path,
3655 int split_index,
3656 struct ocfs2_extent_rec *split_rec,
3657 struct ocfs2_cached_dealloc_ctxt *dealloc,
3658 struct ocfs2_merge_ctxt *ctxt)
3660 int ret = 0;
3661 struct ocfs2_extent_list *el = path_leaf_el(path);
3662 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3664 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3666 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3668 * The merge code will need to create an empty
3669 * extent to take the place of the newly
3670 * emptied slot. Remove any pre-existing empty
3671 * extents - having more than one in a leaf is
3672 * illegal.
3674 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3675 if (ret) {
3676 mlog_errno(ret);
3677 goto out;
3679 split_index--;
3680 rec = &el->l_recs[split_index];
3683 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3685 * Left-right contig implies this.
3687 BUG_ON(!ctxt->c_split_covers_rec);
3690 * Since the leftright insert always covers the entire
3691 * extent, this call will delete the insert record
3692 * entirely, resulting in an empty extent record added to
3693 * the extent block.
3695 * Since the adding of an empty extent shifts
3696 * everything back to the right, there's no need to
3697 * update split_index here.
3699 * When the split_index is zero, we need to merge it to the
3700 * prevoius extent block. It is more efficient and easier
3701 * if we do merge_right first and merge_left later.
3703 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
3704 split_index);
3705 if (ret) {
3706 mlog_errno(ret);
3707 goto out;
3711 * We can only get this from logic error above.
3713 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3715 /* The merge left us with an empty extent, remove it. */
3716 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3717 if (ret) {
3718 mlog_errno(ret);
3719 goto out;
3722 rec = &el->l_recs[split_index];
3725 * Note that we don't pass split_rec here on purpose -
3726 * we've merged it into the rec already.
3728 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3729 dealloc, split_index);
3731 if (ret) {
3732 mlog_errno(ret);
3733 goto out;
3736 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3738 * Error from this last rotate is not critical, so
3739 * print but don't bubble it up.
3741 if (ret)
3742 mlog_errno(ret);
3743 ret = 0;
3744 } else {
3746 * Merge a record to the left or right.
3748 * 'contig_type' is relative to the existing record,
3749 * so for example, if we're "right contig", it's to
3750 * the record on the left (hence the left merge).
3752 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3753 ret = ocfs2_merge_rec_left(path, handle, et,
3754 split_rec, dealloc,
3755 split_index);
3756 if (ret) {
3757 mlog_errno(ret);
3758 goto out;
3760 } else {
3761 ret = ocfs2_merge_rec_right(path, handle,
3762 et, split_rec,
3763 split_index);
3764 if (ret) {
3765 mlog_errno(ret);
3766 goto out;
3770 if (ctxt->c_split_covers_rec) {
3772 * The merge may have left an empty extent in
3773 * our leaf. Try to rotate it away.
3775 ret = ocfs2_rotate_tree_left(handle, et, path,
3776 dealloc);
3777 if (ret)
3778 mlog_errno(ret);
3779 ret = 0;
3783 out:
3784 return ret;
3787 static void ocfs2_subtract_from_rec(struct super_block *sb,
3788 enum ocfs2_split_type split,
3789 struct ocfs2_extent_rec *rec,
3790 struct ocfs2_extent_rec *split_rec)
3792 u64 len_blocks;
3794 len_blocks = ocfs2_clusters_to_blocks(sb,
3795 le16_to_cpu(split_rec->e_leaf_clusters));
3797 if (split == SPLIT_LEFT) {
3799 * Region is on the left edge of the existing
3800 * record.
3802 le32_add_cpu(&rec->e_cpos,
3803 le16_to_cpu(split_rec->e_leaf_clusters));
3804 le64_add_cpu(&rec->e_blkno, len_blocks);
3805 le16_add_cpu(&rec->e_leaf_clusters,
3806 -le16_to_cpu(split_rec->e_leaf_clusters));
3807 } else {
3809 * Region is on the right edge of the existing
3810 * record.
3812 le16_add_cpu(&rec->e_leaf_clusters,
3813 -le16_to_cpu(split_rec->e_leaf_clusters));
3818 * Do the final bits of extent record insertion at the target leaf
3819 * list. If this leaf is part of an allocation tree, it is assumed
3820 * that the tree above has been prepared.
3822 static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3823 struct ocfs2_extent_rec *insert_rec,
3824 struct ocfs2_extent_list *el,
3825 struct ocfs2_insert_type *insert)
3827 int i = insert->ins_contig_index;
3828 unsigned int range;
3829 struct ocfs2_extent_rec *rec;
3831 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3833 if (insert->ins_split != SPLIT_NONE) {
3834 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3835 BUG_ON(i == -1);
3836 rec = &el->l_recs[i];
3837 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3838 insert->ins_split, rec,
3839 insert_rec);
3840 goto rotate;
3844 * Contiguous insert - either left or right.
3846 if (insert->ins_contig != CONTIG_NONE) {
3847 rec = &el->l_recs[i];
3848 if (insert->ins_contig == CONTIG_LEFT) {
3849 rec->e_blkno = insert_rec->e_blkno;
3850 rec->e_cpos = insert_rec->e_cpos;
3852 le16_add_cpu(&rec->e_leaf_clusters,
3853 le16_to_cpu(insert_rec->e_leaf_clusters));
3854 return;
3858 * Handle insert into an empty leaf.
3860 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3861 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3862 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3863 el->l_recs[0] = *insert_rec;
3864 el->l_next_free_rec = cpu_to_le16(1);
3865 return;
3869 * Appending insert.
3871 if (insert->ins_appending == APPEND_TAIL) {
3872 i = le16_to_cpu(el->l_next_free_rec) - 1;
3873 rec = &el->l_recs[i];
3874 range = le32_to_cpu(rec->e_cpos)
3875 + le16_to_cpu(rec->e_leaf_clusters);
3876 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3878 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3879 le16_to_cpu(el->l_count),
3880 "owner %llu, depth %u, count %u, next free %u, "
3881 "rec.cpos %u, rec.clusters %u, "
3882 "insert.cpos %u, insert.clusters %u\n",
3883 ocfs2_metadata_cache_owner(et->et_ci),
3884 le16_to_cpu(el->l_tree_depth),
3885 le16_to_cpu(el->l_count),
3886 le16_to_cpu(el->l_next_free_rec),
3887 le32_to_cpu(el->l_recs[i].e_cpos),
3888 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3889 le32_to_cpu(insert_rec->e_cpos),
3890 le16_to_cpu(insert_rec->e_leaf_clusters));
3891 i++;
3892 el->l_recs[i] = *insert_rec;
3893 le16_add_cpu(&el->l_next_free_rec, 1);
3894 return;
3897 rotate:
3899 * Ok, we have to rotate.
3901 * At this point, it is safe to assume that inserting into an
3902 * empty leaf and appending to a leaf have both been handled
3903 * above.
3905 * This leaf needs to have space, either by the empty 1st
3906 * extent record, or by virtue of an l_next_rec < l_count.
3908 ocfs2_rotate_leaf(el, insert_rec);
3911 static void ocfs2_adjust_rightmost_records(handle_t *handle,
3912 struct ocfs2_extent_tree *et,
3913 struct ocfs2_path *path,
3914 struct ocfs2_extent_rec *insert_rec)
3916 int ret, i, next_free;
3917 struct buffer_head *bh;
3918 struct ocfs2_extent_list *el;
3919 struct ocfs2_extent_rec *rec;
3922 * Update everything except the leaf block.
3924 for (i = 0; i < path->p_tree_depth; i++) {
3925 bh = path->p_node[i].bh;
3926 el = path->p_node[i].el;
3928 next_free = le16_to_cpu(el->l_next_free_rec);
3929 if (next_free == 0) {
3930 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3931 "Owner %llu has a bad extent list",
3932 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
3933 ret = -EIO;
3934 return;
3937 rec = &el->l_recs[next_free - 1];
3939 rec->e_int_clusters = insert_rec->e_cpos;
3940 le32_add_cpu(&rec->e_int_clusters,
3941 le16_to_cpu(insert_rec->e_leaf_clusters));
3942 le32_add_cpu(&rec->e_int_clusters,
3943 -le32_to_cpu(rec->e_cpos));
3945 ocfs2_journal_dirty(handle, bh);
3949 static int ocfs2_append_rec_to_path(handle_t *handle,
3950 struct ocfs2_extent_tree *et,
3951 struct ocfs2_extent_rec *insert_rec,
3952 struct ocfs2_path *right_path,
3953 struct ocfs2_path **ret_left_path)
3955 int ret, next_free;
3956 struct ocfs2_extent_list *el;
3957 struct ocfs2_path *left_path = NULL;
3959 *ret_left_path = NULL;
3962 * This shouldn't happen for non-trees. The extent rec cluster
3963 * count manipulation below only works for interior nodes.
3965 BUG_ON(right_path->p_tree_depth == 0);
3968 * If our appending insert is at the leftmost edge of a leaf,
3969 * then we might need to update the rightmost records of the
3970 * neighboring path.
3972 el = path_leaf_el(right_path);
3973 next_free = le16_to_cpu(el->l_next_free_rec);
3974 if (next_free == 0 ||
3975 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3976 u32 left_cpos;
3978 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3979 right_path, &left_cpos);
3980 if (ret) {
3981 mlog_errno(ret);
3982 goto out;
3985 mlog(0, "Append may need a left path update. cpos: %u, "
3986 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3987 left_cpos);
3990 * No need to worry if the append is already in the
3991 * leftmost leaf.
3993 if (left_cpos) {
3994 left_path = ocfs2_new_path_from_path(right_path);
3995 if (!left_path) {
3996 ret = -ENOMEM;
3997 mlog_errno(ret);
3998 goto out;
4001 ret = ocfs2_find_path(et->et_ci, left_path,
4002 left_cpos);
4003 if (ret) {
4004 mlog_errno(ret);
4005 goto out;
4009 * ocfs2_insert_path() will pass the left_path to the
4010 * journal for us.
4015 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4016 if (ret) {
4017 mlog_errno(ret);
4018 goto out;
4021 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
4023 *ret_left_path = left_path;
4024 ret = 0;
4025 out:
4026 if (ret != 0)
4027 ocfs2_free_path(left_path);
4029 return ret;
4032 static void ocfs2_split_record(struct ocfs2_extent_tree *et,
4033 struct ocfs2_path *left_path,
4034 struct ocfs2_path *right_path,
4035 struct ocfs2_extent_rec *split_rec,
4036 enum ocfs2_split_type split)
4038 int index;
4039 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4040 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4041 struct ocfs2_extent_rec *rec, *tmprec;
4043 right_el = path_leaf_el(right_path);
4044 if (left_path)
4045 left_el = path_leaf_el(left_path);
4047 el = right_el;
4048 insert_el = right_el;
4049 index = ocfs2_search_extent_list(el, cpos);
4050 if (index != -1) {
4051 if (index == 0 && left_path) {
4052 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4055 * This typically means that the record
4056 * started in the left path but moved to the
4057 * right as a result of rotation. We either
4058 * move the existing record to the left, or we
4059 * do the later insert there.
4061 * In this case, the left path should always
4062 * exist as the rotate code will have passed
4063 * it back for a post-insert update.
4066 if (split == SPLIT_LEFT) {
4068 * It's a left split. Since we know
4069 * that the rotate code gave us an
4070 * empty extent in the left path, we
4071 * can just do the insert there.
4073 insert_el = left_el;
4074 } else {
4076 * Right split - we have to move the
4077 * existing record over to the left
4078 * leaf. The insert will be into the
4079 * newly created empty extent in the
4080 * right leaf.
4082 tmprec = &right_el->l_recs[index];
4083 ocfs2_rotate_leaf(left_el, tmprec);
4084 el = left_el;
4086 memset(tmprec, 0, sizeof(*tmprec));
4087 index = ocfs2_search_extent_list(left_el, cpos);
4088 BUG_ON(index == -1);
4091 } else {
4092 BUG_ON(!left_path);
4093 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4095 * Left path is easy - we can just allow the insert to
4096 * happen.
4098 el = left_el;
4099 insert_el = left_el;
4100 index = ocfs2_search_extent_list(el, cpos);
4101 BUG_ON(index == -1);
4104 rec = &el->l_recs[index];
4105 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4106 split, rec, split_rec);
4107 ocfs2_rotate_leaf(insert_el, split_rec);
4111 * This function only does inserts on an allocation b-tree. For tree
4112 * depth = 0, ocfs2_insert_at_leaf() is called directly.
4114 * right_path is the path we want to do the actual insert
4115 * in. left_path should only be passed in if we need to update that
4116 * portion of the tree after an edge insert.
4118 static int ocfs2_insert_path(handle_t *handle,
4119 struct ocfs2_extent_tree *et,
4120 struct ocfs2_path *left_path,
4121 struct ocfs2_path *right_path,
4122 struct ocfs2_extent_rec *insert_rec,
4123 struct ocfs2_insert_type *insert)
4125 int ret, subtree_index;
4126 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
4128 if (left_path) {
4130 * There's a chance that left_path got passed back to
4131 * us without being accounted for in the
4132 * journal. Extend our transaction here to be sure we
4133 * can change those blocks.
4135 ret = ocfs2_extend_trans(handle, left_path->p_tree_depth);
4136 if (ret < 0) {
4137 mlog_errno(ret);
4138 goto out;
4141 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
4142 if (ret < 0) {
4143 mlog_errno(ret);
4144 goto out;
4149 * Pass both paths to the journal. The majority of inserts
4150 * will be touching all components anyway.
4152 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4153 if (ret < 0) {
4154 mlog_errno(ret);
4155 goto out;
4158 if (insert->ins_split != SPLIT_NONE) {
4160 * We could call ocfs2_insert_at_leaf() for some types
4161 * of splits, but it's easier to just let one separate
4162 * function sort it all out.
4164 ocfs2_split_record(et, left_path, right_path,
4165 insert_rec, insert->ins_split);
4168 * Split might have modified either leaf and we don't
4169 * have a guarantee that the later edge insert will
4170 * dirty this for us.
4172 if (left_path)
4173 ocfs2_journal_dirty(handle,
4174 path_leaf_bh(left_path));
4175 } else
4176 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4177 insert);
4179 ocfs2_journal_dirty(handle, leaf_bh);
4181 if (left_path) {
4182 subtree_index = ocfs2_find_subtree_root(et, left_path,
4183 right_path);
4184 ocfs2_complete_edge_insert(handle, left_path, right_path,
4185 subtree_index);
4188 ret = 0;
4189 out:
4190 return ret;
4193 static int ocfs2_do_insert_extent(handle_t *handle,
4194 struct ocfs2_extent_tree *et,
4195 struct ocfs2_extent_rec *insert_rec,
4196 struct ocfs2_insert_type *type)
4198 int ret, rotate = 0;
4199 u32 cpos;
4200 struct ocfs2_path *right_path = NULL;
4201 struct ocfs2_path *left_path = NULL;
4202 struct ocfs2_extent_list *el;
4204 el = et->et_root_el;
4206 ret = ocfs2_et_root_journal_access(handle, et,
4207 OCFS2_JOURNAL_ACCESS_WRITE);
4208 if (ret) {
4209 mlog_errno(ret);
4210 goto out;
4213 if (le16_to_cpu(el->l_tree_depth) == 0) {
4214 ocfs2_insert_at_leaf(et, insert_rec, el, type);
4215 goto out_update_clusters;
4218 right_path = ocfs2_new_path_from_et(et);
4219 if (!right_path) {
4220 ret = -ENOMEM;
4221 mlog_errno(ret);
4222 goto out;
4226 * Determine the path to start with. Rotations need the
4227 * rightmost path, everything else can go directly to the
4228 * target leaf.
4230 cpos = le32_to_cpu(insert_rec->e_cpos);
4231 if (type->ins_appending == APPEND_NONE &&
4232 type->ins_contig == CONTIG_NONE) {
4233 rotate = 1;
4234 cpos = UINT_MAX;
4237 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
4238 if (ret) {
4239 mlog_errno(ret);
4240 goto out;
4243 if (rotate) {
4244 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
4245 le32_to_cpu(insert_rec->e_cpos),
4246 right_path, &left_path);
4247 if (ret) {
4248 mlog_errno(ret);
4249 goto out;
4253 * ocfs2_rotate_tree_right() might have extended the
4254 * transaction without re-journaling our tree root.
4256 ret = ocfs2_et_root_journal_access(handle, et,
4257 OCFS2_JOURNAL_ACCESS_WRITE);
4258 if (ret) {
4259 mlog_errno(ret);
4260 goto out;
4262 } else if (type->ins_appending == APPEND_TAIL
4263 && type->ins_contig != CONTIG_LEFT) {
4264 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
4265 right_path, &left_path);
4266 if (ret) {
4267 mlog_errno(ret);
4268 goto out;
4272 ret = ocfs2_insert_path(handle, et, left_path, right_path,
4273 insert_rec, type);
4274 if (ret) {
4275 mlog_errno(ret);
4276 goto out;
4279 out_update_clusters:
4280 if (type->ins_split == SPLIT_NONE)
4281 ocfs2_et_update_clusters(et,
4282 le16_to_cpu(insert_rec->e_leaf_clusters));
4284 ocfs2_journal_dirty(handle, et->et_root_bh);
4286 out:
4287 ocfs2_free_path(left_path);
4288 ocfs2_free_path(right_path);
4290 return ret;
4293 static enum ocfs2_contig_type
4294 ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4295 struct ocfs2_path *path,
4296 struct ocfs2_extent_list *el, int index,
4297 struct ocfs2_extent_rec *split_rec)
4299 int status;
4300 enum ocfs2_contig_type ret = CONTIG_NONE;
4301 u32 left_cpos, right_cpos;
4302 struct ocfs2_extent_rec *rec = NULL;
4303 struct ocfs2_extent_list *new_el;
4304 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4305 struct buffer_head *bh;
4306 struct ocfs2_extent_block *eb;
4307 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
4309 if (index > 0) {
4310 rec = &el->l_recs[index - 1];
4311 } else if (path->p_tree_depth > 0) {
4312 status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
4313 if (status)
4314 goto out;
4316 if (left_cpos != 0) {
4317 left_path = ocfs2_new_path_from_path(path);
4318 if (!left_path)
4319 goto out;
4321 status = ocfs2_find_path(et->et_ci, left_path,
4322 left_cpos);
4323 if (status)
4324 goto out;
4326 new_el = path_leaf_el(left_path);
4328 if (le16_to_cpu(new_el->l_next_free_rec) !=
4329 le16_to_cpu(new_el->l_count)) {
4330 bh = path_leaf_bh(left_path);
4331 eb = (struct ocfs2_extent_block *)bh->b_data;
4332 ocfs2_error(sb,
4333 "Extent block #%llu has an "
4334 "invalid l_next_free_rec of "
4335 "%d. It should have "
4336 "matched the l_count of %d",
4337 (unsigned long long)le64_to_cpu(eb->h_blkno),
4338 le16_to_cpu(new_el->l_next_free_rec),
4339 le16_to_cpu(new_el->l_count));
4340 status = -EINVAL;
4341 goto out;
4343 rec = &new_el->l_recs[
4344 le16_to_cpu(new_el->l_next_free_rec) - 1];
4349 * We're careful to check for an empty extent record here -
4350 * the merge code will know what to do if it sees one.
4352 if (rec) {
4353 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4354 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4355 ret = CONTIG_RIGHT;
4356 } else {
4357 ret = ocfs2_et_extent_contig(et, rec, split_rec);
4361 rec = NULL;
4362 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4363 rec = &el->l_recs[index + 1];
4364 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4365 path->p_tree_depth > 0) {
4366 status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
4367 if (status)
4368 goto out;
4370 if (right_cpos == 0)
4371 goto out;
4373 right_path = ocfs2_new_path_from_path(path);
4374 if (!right_path)
4375 goto out;
4377 status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
4378 if (status)
4379 goto out;
4381 new_el = path_leaf_el(right_path);
4382 rec = &new_el->l_recs[0];
4383 if (ocfs2_is_empty_extent(rec)) {
4384 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4385 bh = path_leaf_bh(right_path);
4386 eb = (struct ocfs2_extent_block *)bh->b_data;
4387 ocfs2_error(sb,
4388 "Extent block #%llu has an "
4389 "invalid l_next_free_rec of %d",
4390 (unsigned long long)le64_to_cpu(eb->h_blkno),
4391 le16_to_cpu(new_el->l_next_free_rec));
4392 status = -EINVAL;
4393 goto out;
4395 rec = &new_el->l_recs[1];
4399 if (rec) {
4400 enum ocfs2_contig_type contig_type;
4402 contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
4404 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4405 ret = CONTIG_LEFTRIGHT;
4406 else if (ret == CONTIG_NONE)
4407 ret = contig_type;
4410 out:
4411 if (left_path)
4412 ocfs2_free_path(left_path);
4413 if (right_path)
4414 ocfs2_free_path(right_path);
4416 return ret;
4419 static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
4420 struct ocfs2_insert_type *insert,
4421 struct ocfs2_extent_list *el,
4422 struct ocfs2_extent_rec *insert_rec)
4424 int i;
4425 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4427 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4429 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4430 contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4431 insert_rec);
4432 if (contig_type != CONTIG_NONE) {
4433 insert->ins_contig_index = i;
4434 break;
4437 insert->ins_contig = contig_type;
4439 if (insert->ins_contig != CONTIG_NONE) {
4440 struct ocfs2_extent_rec *rec =
4441 &el->l_recs[insert->ins_contig_index];
4442 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4443 le16_to_cpu(insert_rec->e_leaf_clusters);
4446 * Caller might want us to limit the size of extents, don't
4447 * calculate contiguousness if we might exceed that limit.
4449 if (et->et_max_leaf_clusters &&
4450 (len > et->et_max_leaf_clusters))
4451 insert->ins_contig = CONTIG_NONE;
4456 * This should only be called against the righmost leaf extent list.
4458 * ocfs2_figure_appending_type() will figure out whether we'll have to
4459 * insert at the tail of the rightmost leaf.
4461 * This should also work against the root extent list for tree's with 0
4462 * depth. If we consider the root extent list to be the rightmost leaf node
4463 * then the logic here makes sense.
4465 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4466 struct ocfs2_extent_list *el,
4467 struct ocfs2_extent_rec *insert_rec)
4469 int i;
4470 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4471 struct ocfs2_extent_rec *rec;
4473 insert->ins_appending = APPEND_NONE;
4475 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4477 if (!el->l_next_free_rec)
4478 goto set_tail_append;
4480 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4481 /* Were all records empty? */
4482 if (le16_to_cpu(el->l_next_free_rec) == 1)
4483 goto set_tail_append;
4486 i = le16_to_cpu(el->l_next_free_rec) - 1;
4487 rec = &el->l_recs[i];
4489 if (cpos >=
4490 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4491 goto set_tail_append;
4493 return;
4495 set_tail_append:
4496 insert->ins_appending = APPEND_TAIL;
4500 * Helper function called at the begining of an insert.
4502 * This computes a few things that are commonly used in the process of
4503 * inserting into the btree:
4504 * - Whether the new extent is contiguous with an existing one.
4505 * - The current tree depth.
4506 * - Whether the insert is an appending one.
4507 * - The total # of free records in the tree.
4509 * All of the information is stored on the ocfs2_insert_type
4510 * structure.
4512 static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
4513 struct buffer_head **last_eb_bh,
4514 struct ocfs2_extent_rec *insert_rec,
4515 int *free_records,
4516 struct ocfs2_insert_type *insert)
4518 int ret;
4519 struct ocfs2_extent_block *eb;
4520 struct ocfs2_extent_list *el;
4521 struct ocfs2_path *path = NULL;
4522 struct buffer_head *bh = NULL;
4524 insert->ins_split = SPLIT_NONE;
4526 el = et->et_root_el;
4527 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4529 if (el->l_tree_depth) {
4531 * If we have tree depth, we read in the
4532 * rightmost extent block ahead of time as
4533 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4534 * may want it later.
4536 ret = ocfs2_read_extent_block(et->et_ci,
4537 ocfs2_et_get_last_eb_blk(et),
4538 &bh);
4539 if (ret) {
4540 mlog_exit(ret);
4541 goto out;
4543 eb = (struct ocfs2_extent_block *) bh->b_data;
4544 el = &eb->h_list;
4547 *free_records = le16_to_cpu(el->l_count) -
4548 le16_to_cpu(el->l_next_free_rec);
4550 if (!insert->ins_tree_depth) {
4551 ocfs2_figure_contig_type(et, insert, el, insert_rec);
4552 ocfs2_figure_appending_type(insert, el, insert_rec);
4553 return 0;
4556 path = ocfs2_new_path_from_et(et);
4557 if (!path) {
4558 ret = -ENOMEM;
4559 mlog_errno(ret);
4560 goto out;
4564 * In the case that we're inserting past what the tree
4565 * currently accounts for, ocfs2_find_path() will return for
4566 * us the rightmost tree path. This is accounted for below in
4567 * the appending code.
4569 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
4570 if (ret) {
4571 mlog_errno(ret);
4572 goto out;
4575 el = path_leaf_el(path);
4578 * Now that we have the path, there's two things we want to determine:
4579 * 1) Contiguousness (also set contig_index if this is so)
4581 * 2) Are we doing an append? We can trivially break this up
4582 * into two types of appends: simple record append, or a
4583 * rotate inside the tail leaf.
4585 ocfs2_figure_contig_type(et, insert, el, insert_rec);
4588 * The insert code isn't quite ready to deal with all cases of
4589 * left contiguousness. Specifically, if it's an insert into
4590 * the 1st record in a leaf, it will require the adjustment of
4591 * cluster count on the last record of the path directly to it's
4592 * left. For now, just catch that case and fool the layers
4593 * above us. This works just fine for tree_depth == 0, which
4594 * is why we allow that above.
4596 if (insert->ins_contig == CONTIG_LEFT &&
4597 insert->ins_contig_index == 0)
4598 insert->ins_contig = CONTIG_NONE;
4601 * Ok, so we can simply compare against last_eb to figure out
4602 * whether the path doesn't exist. This will only happen in
4603 * the case that we're doing a tail append, so maybe we can
4604 * take advantage of that information somehow.
4606 if (ocfs2_et_get_last_eb_blk(et) ==
4607 path_leaf_bh(path)->b_blocknr) {
4609 * Ok, ocfs2_find_path() returned us the rightmost
4610 * tree path. This might be an appending insert. There are
4611 * two cases:
4612 * 1) We're doing a true append at the tail:
4613 * -This might even be off the end of the leaf
4614 * 2) We're "appending" by rotating in the tail
4616 ocfs2_figure_appending_type(insert, el, insert_rec);
4619 out:
4620 ocfs2_free_path(path);
4622 if (ret == 0)
4623 *last_eb_bh = bh;
4624 else
4625 brelse(bh);
4626 return ret;
4630 * Insert an extent into a btree.
4632 * The caller needs to update the owning btree's cluster count.
4634 int ocfs2_insert_extent(handle_t *handle,
4635 struct ocfs2_extent_tree *et,
4636 u32 cpos,
4637 u64 start_blk,
4638 u32 new_clusters,
4639 u8 flags,
4640 struct ocfs2_alloc_context *meta_ac)
4642 int status;
4643 int uninitialized_var(free_records);
4644 struct buffer_head *last_eb_bh = NULL;
4645 struct ocfs2_insert_type insert = {0, };
4646 struct ocfs2_extent_rec rec;
4648 mlog(0, "add %u clusters at position %u to owner %llu\n",
4649 new_clusters, cpos,
4650 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
4652 memset(&rec, 0, sizeof(rec));
4653 rec.e_cpos = cpu_to_le32(cpos);
4654 rec.e_blkno = cpu_to_le64(start_blk);
4655 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4656 rec.e_flags = flags;
4657 status = ocfs2_et_insert_check(et, &rec);
4658 if (status) {
4659 mlog_errno(status);
4660 goto bail;
4663 status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
4664 &free_records, &insert);
4665 if (status < 0) {
4666 mlog_errno(status);
4667 goto bail;
4670 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4671 "Insert.contig_index: %d, Insert.free_records: %d, "
4672 "Insert.tree_depth: %d\n",
4673 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4674 free_records, insert.ins_tree_depth);
4676 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4677 status = ocfs2_grow_tree(handle, et,
4678 &insert.ins_tree_depth, &last_eb_bh,
4679 meta_ac);
4680 if (status) {
4681 mlog_errno(status);
4682 goto bail;
4686 /* Finally, we can add clusters. This might rotate the tree for us. */
4687 status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
4688 if (status < 0)
4689 mlog_errno(status);
4690 else
4691 ocfs2_et_extent_map_insert(et, &rec);
4693 bail:
4694 brelse(last_eb_bh);
4696 mlog_exit(status);
4697 return status;
4701 * Allcate and add clusters into the extent b-tree.
4702 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4703 * The extent b-tree's root is specified by et, and
4704 * it is not limited to the file storage. Any extent tree can use this
4705 * function if it implements the proper ocfs2_extent_tree.
4707 int ocfs2_add_clusters_in_btree(handle_t *handle,
4708 struct ocfs2_extent_tree *et,
4709 u32 *logical_offset,
4710 u32 clusters_to_add,
4711 int mark_unwritten,
4712 struct ocfs2_alloc_context *data_ac,
4713 struct ocfs2_alloc_context *meta_ac,
4714 enum ocfs2_alloc_restarted *reason_ret)
4716 int status = 0;
4717 int free_extents;
4718 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4719 u32 bit_off, num_bits;
4720 u64 block;
4721 u8 flags = 0;
4722 struct ocfs2_super *osb =
4723 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
4725 BUG_ON(!clusters_to_add);
4727 if (mark_unwritten)
4728 flags = OCFS2_EXT_UNWRITTEN;
4730 free_extents = ocfs2_num_free_extents(osb, et);
4731 if (free_extents < 0) {
4732 status = free_extents;
4733 mlog_errno(status);
4734 goto leave;
4737 /* there are two cases which could cause us to EAGAIN in the
4738 * we-need-more-metadata case:
4739 * 1) we haven't reserved *any*
4740 * 2) we are so fragmented, we've needed to add metadata too
4741 * many times. */
4742 if (!free_extents && !meta_ac) {
4743 mlog(0, "we haven't reserved any metadata!\n");
4744 status = -EAGAIN;
4745 reason = RESTART_META;
4746 goto leave;
4747 } else if ((!free_extents)
4748 && (ocfs2_alloc_context_bits_left(meta_ac)
4749 < ocfs2_extend_meta_needed(et->et_root_el))) {
4750 mlog(0, "filesystem is really fragmented...\n");
4751 status = -EAGAIN;
4752 reason = RESTART_META;
4753 goto leave;
4756 status = __ocfs2_claim_clusters(handle, data_ac, 1,
4757 clusters_to_add, &bit_off, &num_bits);
4758 if (status < 0) {
4759 if (status != -ENOSPC)
4760 mlog_errno(status);
4761 goto leave;
4764 BUG_ON(num_bits > clusters_to_add);
4766 /* reserve our write early -- insert_extent may update the tree root */
4767 status = ocfs2_et_root_journal_access(handle, et,
4768 OCFS2_JOURNAL_ACCESS_WRITE);
4769 if (status < 0) {
4770 mlog_errno(status);
4771 goto leave;
4774 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4775 mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
4776 num_bits, bit_off,
4777 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
4778 status = ocfs2_insert_extent(handle, et, *logical_offset, block,
4779 num_bits, flags, meta_ac);
4780 if (status < 0) {
4781 mlog_errno(status);
4782 goto leave;
4785 ocfs2_journal_dirty(handle, et->et_root_bh);
4787 clusters_to_add -= num_bits;
4788 *logical_offset += num_bits;
4790 if (clusters_to_add) {
4791 mlog(0, "need to alloc once more, wanted = %u\n",
4792 clusters_to_add);
4793 status = -EAGAIN;
4794 reason = RESTART_TRANS;
4797 leave:
4798 mlog_exit(status);
4799 if (reason_ret)
4800 *reason_ret = reason;
4801 return status;
4804 static void ocfs2_make_right_split_rec(struct super_block *sb,
4805 struct ocfs2_extent_rec *split_rec,
4806 u32 cpos,
4807 struct ocfs2_extent_rec *rec)
4809 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4810 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4812 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4814 split_rec->e_cpos = cpu_to_le32(cpos);
4815 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4817 split_rec->e_blkno = rec->e_blkno;
4818 le64_add_cpu(&split_rec->e_blkno,
4819 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4821 split_rec->e_flags = rec->e_flags;
4824 static int ocfs2_split_and_insert(handle_t *handle,
4825 struct ocfs2_extent_tree *et,
4826 struct ocfs2_path *path,
4827 struct buffer_head **last_eb_bh,
4828 int split_index,
4829 struct ocfs2_extent_rec *orig_split_rec,
4830 struct ocfs2_alloc_context *meta_ac)
4832 int ret = 0, depth;
4833 unsigned int insert_range, rec_range, do_leftright = 0;
4834 struct ocfs2_extent_rec tmprec;
4835 struct ocfs2_extent_list *rightmost_el;
4836 struct ocfs2_extent_rec rec;
4837 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4838 struct ocfs2_insert_type insert;
4839 struct ocfs2_extent_block *eb;
4841 leftright:
4843 * Store a copy of the record on the stack - it might move
4844 * around as the tree is manipulated below.
4846 rec = path_leaf_el(path)->l_recs[split_index];
4848 rightmost_el = et->et_root_el;
4850 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4851 if (depth) {
4852 BUG_ON(!(*last_eb_bh));
4853 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4854 rightmost_el = &eb->h_list;
4857 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4858 le16_to_cpu(rightmost_el->l_count)) {
4859 ret = ocfs2_grow_tree(handle, et,
4860 &depth, last_eb_bh, meta_ac);
4861 if (ret) {
4862 mlog_errno(ret);
4863 goto out;
4867 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4868 insert.ins_appending = APPEND_NONE;
4869 insert.ins_contig = CONTIG_NONE;
4870 insert.ins_tree_depth = depth;
4872 insert_range = le32_to_cpu(split_rec.e_cpos) +
4873 le16_to_cpu(split_rec.e_leaf_clusters);
4874 rec_range = le32_to_cpu(rec.e_cpos) +
4875 le16_to_cpu(rec.e_leaf_clusters);
4877 if (split_rec.e_cpos == rec.e_cpos) {
4878 insert.ins_split = SPLIT_LEFT;
4879 } else if (insert_range == rec_range) {
4880 insert.ins_split = SPLIT_RIGHT;
4881 } else {
4883 * Left/right split. We fake this as a right split
4884 * first and then make a second pass as a left split.
4886 insert.ins_split = SPLIT_RIGHT;
4888 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4889 &tmprec, insert_range, &rec);
4891 split_rec = tmprec;
4893 BUG_ON(do_leftright);
4894 do_leftright = 1;
4897 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
4898 if (ret) {
4899 mlog_errno(ret);
4900 goto out;
4903 if (do_leftright == 1) {
4904 u32 cpos;
4905 struct ocfs2_extent_list *el;
4907 do_leftright++;
4908 split_rec = *orig_split_rec;
4910 ocfs2_reinit_path(path, 1);
4912 cpos = le32_to_cpu(split_rec.e_cpos);
4913 ret = ocfs2_find_path(et->et_ci, path, cpos);
4914 if (ret) {
4915 mlog_errno(ret);
4916 goto out;
4919 el = path_leaf_el(path);
4920 split_index = ocfs2_search_extent_list(el, cpos);
4921 goto leftright;
4923 out:
4925 return ret;
4928 static int ocfs2_replace_extent_rec(handle_t *handle,
4929 struct ocfs2_extent_tree *et,
4930 struct ocfs2_path *path,
4931 struct ocfs2_extent_list *el,
4932 int split_index,
4933 struct ocfs2_extent_rec *split_rec)
4935 int ret;
4937 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
4938 path_num_items(path) - 1);
4939 if (ret) {
4940 mlog_errno(ret);
4941 goto out;
4944 el->l_recs[split_index] = *split_rec;
4946 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4947 out:
4948 return ret;
4952 * Split part or all of the extent record at split_index in the leaf
4953 * pointed to by path. Merge with the contiguous extent record if needed.
4955 * Care is taken to handle contiguousness so as to not grow the tree.
4957 * meta_ac is not strictly necessary - we only truly need it if growth
4958 * of the tree is required. All other cases will degrade into a less
4959 * optimal tree layout.
4961 * last_eb_bh should be the rightmost leaf block for any extent
4962 * btree. Since a split may grow the tree or a merge might shrink it,
4963 * the caller cannot trust the contents of that buffer after this call.
4965 * This code is optimized for readability - several passes might be
4966 * made over certain portions of the tree. All of those blocks will
4967 * have been brought into cache (and pinned via the journal), so the
4968 * extra overhead is not expressed in terms of disk reads.
4970 int ocfs2_split_extent(handle_t *handle,
4971 struct ocfs2_extent_tree *et,
4972 struct ocfs2_path *path,
4973 int split_index,
4974 struct ocfs2_extent_rec *split_rec,
4975 struct ocfs2_alloc_context *meta_ac,
4976 struct ocfs2_cached_dealloc_ctxt *dealloc)
4978 int ret = 0;
4979 struct ocfs2_extent_list *el = path_leaf_el(path);
4980 struct buffer_head *last_eb_bh = NULL;
4981 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4982 struct ocfs2_merge_ctxt ctxt;
4983 struct ocfs2_extent_list *rightmost_el;
4985 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4986 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4987 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4988 ret = -EIO;
4989 mlog_errno(ret);
4990 goto out;
4993 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
4994 split_index,
4995 split_rec);
4998 * The core merge / split code wants to know how much room is
4999 * left in this allocation tree, so we pass the
5000 * rightmost extent list.
5002 if (path->p_tree_depth) {
5003 struct ocfs2_extent_block *eb;
5005 ret = ocfs2_read_extent_block(et->et_ci,
5006 ocfs2_et_get_last_eb_blk(et),
5007 &last_eb_bh);
5008 if (ret) {
5009 mlog_exit(ret);
5010 goto out;
5013 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5014 rightmost_el = &eb->h_list;
5015 } else
5016 rightmost_el = path_root_el(path);
5018 if (rec->e_cpos == split_rec->e_cpos &&
5019 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5020 ctxt.c_split_covers_rec = 1;
5021 else
5022 ctxt.c_split_covers_rec = 0;
5024 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5026 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5027 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5028 ctxt.c_split_covers_rec);
5030 if (ctxt.c_contig_type == CONTIG_NONE) {
5031 if (ctxt.c_split_covers_rec)
5032 ret = ocfs2_replace_extent_rec(handle, et, path, el,
5033 split_index, split_rec);
5034 else
5035 ret = ocfs2_split_and_insert(handle, et, path,
5036 &last_eb_bh, split_index,
5037 split_rec, meta_ac);
5038 if (ret)
5039 mlog_errno(ret);
5040 } else {
5041 ret = ocfs2_try_to_merge_extent(handle, et, path,
5042 split_index, split_rec,
5043 dealloc, &ctxt);
5044 if (ret)
5045 mlog_errno(ret);
5048 out:
5049 brelse(last_eb_bh);
5050 return ret;
5054 * Change the flags of the already-existing extent at cpos for len clusters.
5056 * new_flags: the flags we want to set.
5057 * clear_flags: the flags we want to clear.
5058 * phys: the new physical offset we want this new extent starts from.
5060 * If the existing extent is larger than the request, initiate a
5061 * split. An attempt will be made at merging with adjacent extents.
5063 * The caller is responsible for passing down meta_ac if we'll need it.
5065 int ocfs2_change_extent_flag(handle_t *handle,
5066 struct ocfs2_extent_tree *et,
5067 u32 cpos, u32 len, u32 phys,
5068 struct ocfs2_alloc_context *meta_ac,
5069 struct ocfs2_cached_dealloc_ctxt *dealloc,
5070 int new_flags, int clear_flags)
5072 int ret, index;
5073 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5074 u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
5075 struct ocfs2_extent_rec split_rec;
5076 struct ocfs2_path *left_path = NULL;
5077 struct ocfs2_extent_list *el;
5078 struct ocfs2_extent_rec *rec;
5080 left_path = ocfs2_new_path_from_et(et);
5081 if (!left_path) {
5082 ret = -ENOMEM;
5083 mlog_errno(ret);
5084 goto out;
5087 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
5088 if (ret) {
5089 mlog_errno(ret);
5090 goto out;
5092 el = path_leaf_el(left_path);
5094 index = ocfs2_search_extent_list(el, cpos);
5095 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5096 ocfs2_error(sb,
5097 "Owner %llu has an extent at cpos %u which can no "
5098 "longer be found.\n",
5099 (unsigned long long)
5100 ocfs2_metadata_cache_owner(et->et_ci), cpos);
5101 ret = -EROFS;
5102 goto out;
5105 ret = -EIO;
5106 rec = &el->l_recs[index];
5107 if (new_flags && (rec->e_flags & new_flags)) {
5108 mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
5109 "extent that already had them",
5110 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5111 new_flags);
5112 goto out;
5115 if (clear_flags && !(rec->e_flags & clear_flags)) {
5116 mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
5117 "extent that didn't have them",
5118 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5119 clear_flags);
5120 goto out;
5123 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5124 split_rec.e_cpos = cpu_to_le32(cpos);
5125 split_rec.e_leaf_clusters = cpu_to_le16(len);
5126 split_rec.e_blkno = cpu_to_le64(start_blkno);
5127 split_rec.e_flags = rec->e_flags;
5128 if (new_flags)
5129 split_rec.e_flags |= new_flags;
5130 if (clear_flags)
5131 split_rec.e_flags &= ~clear_flags;
5133 ret = ocfs2_split_extent(handle, et, left_path,
5134 index, &split_rec, meta_ac,
5135 dealloc);
5136 if (ret)
5137 mlog_errno(ret);
5139 out:
5140 ocfs2_free_path(left_path);
5141 return ret;
5146 * Mark the already-existing extent at cpos as written for len clusters.
5147 * This removes the unwritten extent flag.
5149 * If the existing extent is larger than the request, initiate a
5150 * split. An attempt will be made at merging with adjacent extents.
5152 * The caller is responsible for passing down meta_ac if we'll need it.
5154 int ocfs2_mark_extent_written(struct inode *inode,
5155 struct ocfs2_extent_tree *et,
5156 handle_t *handle, u32 cpos, u32 len, u32 phys,
5157 struct ocfs2_alloc_context *meta_ac,
5158 struct ocfs2_cached_dealloc_ctxt *dealloc)
5160 int ret;
5162 mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
5163 inode->i_ino, cpos, len, phys);
5165 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5166 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5167 "that are being written to, but the feature bit "
5168 "is not set in the super block.",
5169 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5170 ret = -EROFS;
5171 goto out;
5174 ocfs2_et_extent_map_truncate(et, 0);
5176 ret = ocfs2_change_extent_flag(handle, et, cpos,
5177 len, phys, meta_ac, dealloc,
5178 0, OCFS2_EXT_UNWRITTEN);
5179 if (ret)
5180 mlog_errno(ret);
5182 out:
5183 return ret;
5186 static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5187 struct ocfs2_path *path,
5188 int index, u32 new_range,
5189 struct ocfs2_alloc_context *meta_ac)
5191 int ret, depth, credits;
5192 struct buffer_head *last_eb_bh = NULL;
5193 struct ocfs2_extent_block *eb;
5194 struct ocfs2_extent_list *rightmost_el, *el;
5195 struct ocfs2_extent_rec split_rec;
5196 struct ocfs2_extent_rec *rec;
5197 struct ocfs2_insert_type insert;
5200 * Setup the record to split before we grow the tree.
5202 el = path_leaf_el(path);
5203 rec = &el->l_recs[index];
5204 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5205 &split_rec, new_range, rec);
5207 depth = path->p_tree_depth;
5208 if (depth > 0) {
5209 ret = ocfs2_read_extent_block(et->et_ci,
5210 ocfs2_et_get_last_eb_blk(et),
5211 &last_eb_bh);
5212 if (ret < 0) {
5213 mlog_errno(ret);
5214 goto out;
5217 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5218 rightmost_el = &eb->h_list;
5219 } else
5220 rightmost_el = path_leaf_el(path);
5222 credits = path->p_tree_depth +
5223 ocfs2_extend_meta_needed(et->et_root_el);
5224 ret = ocfs2_extend_trans(handle, credits);
5225 if (ret) {
5226 mlog_errno(ret);
5227 goto out;
5230 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5231 le16_to_cpu(rightmost_el->l_count)) {
5232 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
5233 meta_ac);
5234 if (ret) {
5235 mlog_errno(ret);
5236 goto out;
5240 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5241 insert.ins_appending = APPEND_NONE;
5242 insert.ins_contig = CONTIG_NONE;
5243 insert.ins_split = SPLIT_RIGHT;
5244 insert.ins_tree_depth = depth;
5246 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
5247 if (ret)
5248 mlog_errno(ret);
5250 out:
5251 brelse(last_eb_bh);
5252 return ret;
5255 static int ocfs2_truncate_rec(handle_t *handle,
5256 struct ocfs2_extent_tree *et,
5257 struct ocfs2_path *path, int index,
5258 struct ocfs2_cached_dealloc_ctxt *dealloc,
5259 u32 cpos, u32 len)
5261 int ret;
5262 u32 left_cpos, rec_range, trunc_range;
5263 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5264 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5265 struct ocfs2_path *left_path = NULL;
5266 struct ocfs2_extent_list *el = path_leaf_el(path);
5267 struct ocfs2_extent_rec *rec;
5268 struct ocfs2_extent_block *eb;
5270 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5271 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5272 if (ret) {
5273 mlog_errno(ret);
5274 goto out;
5277 index--;
5280 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5281 path->p_tree_depth) {
5283 * Check whether this is the rightmost tree record. If
5284 * we remove all of this record or part of its right
5285 * edge then an update of the record lengths above it
5286 * will be required.
5288 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5289 if (eb->h_next_leaf_blk == 0)
5290 is_rightmost_tree_rec = 1;
5293 rec = &el->l_recs[index];
5294 if (index == 0 && path->p_tree_depth &&
5295 le32_to_cpu(rec->e_cpos) == cpos) {
5297 * Changing the leftmost offset (via partial or whole
5298 * record truncate) of an interior (or rightmost) path
5299 * means we have to update the subtree that is formed
5300 * by this leaf and the one to it's left.
5302 * There are two cases we can skip:
5303 * 1) Path is the leftmost one in our btree.
5304 * 2) The leaf is rightmost and will be empty after
5305 * we remove the extent record - the rotate code
5306 * knows how to update the newly formed edge.
5309 ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
5310 if (ret) {
5311 mlog_errno(ret);
5312 goto out;
5315 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5316 left_path = ocfs2_new_path_from_path(path);
5317 if (!left_path) {
5318 ret = -ENOMEM;
5319 mlog_errno(ret);
5320 goto out;
5323 ret = ocfs2_find_path(et->et_ci, left_path,
5324 left_cpos);
5325 if (ret) {
5326 mlog_errno(ret);
5327 goto out;
5332 ret = ocfs2_extend_rotate_transaction(handle, 0,
5333 handle->h_buffer_credits,
5334 path);
5335 if (ret) {
5336 mlog_errno(ret);
5337 goto out;
5340 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
5341 if (ret) {
5342 mlog_errno(ret);
5343 goto out;
5346 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
5347 if (ret) {
5348 mlog_errno(ret);
5349 goto out;
5352 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5353 trunc_range = cpos + len;
5355 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5356 int next_free;
5358 memset(rec, 0, sizeof(*rec));
5359 ocfs2_cleanup_merge(el, index);
5360 wants_rotate = 1;
5362 next_free = le16_to_cpu(el->l_next_free_rec);
5363 if (is_rightmost_tree_rec && next_free > 1) {
5365 * We skip the edge update if this path will
5366 * be deleted by the rotate code.
5368 rec = &el->l_recs[next_free - 1];
5369 ocfs2_adjust_rightmost_records(handle, et, path,
5370 rec);
5372 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5373 /* Remove leftmost portion of the record. */
5374 le32_add_cpu(&rec->e_cpos, len);
5375 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5376 le16_add_cpu(&rec->e_leaf_clusters, -len);
5377 } else if (rec_range == trunc_range) {
5378 /* Remove rightmost portion of the record */
5379 le16_add_cpu(&rec->e_leaf_clusters, -len);
5380 if (is_rightmost_tree_rec)
5381 ocfs2_adjust_rightmost_records(handle, et, path, rec);
5382 } else {
5383 /* Caller should have trapped this. */
5384 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5385 "(%u, %u)\n",
5386 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5387 le32_to_cpu(rec->e_cpos),
5388 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5389 BUG();
5392 if (left_path) {
5393 int subtree_index;
5395 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
5396 ocfs2_complete_edge_insert(handle, left_path, path,
5397 subtree_index);
5400 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5402 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5403 if (ret) {
5404 mlog_errno(ret);
5405 goto out;
5408 out:
5409 ocfs2_free_path(left_path);
5410 return ret;
5413 int ocfs2_remove_extent(handle_t *handle,
5414 struct ocfs2_extent_tree *et,
5415 u32 cpos, u32 len,
5416 struct ocfs2_alloc_context *meta_ac,
5417 struct ocfs2_cached_dealloc_ctxt *dealloc)
5419 int ret, index;
5420 u32 rec_range, trunc_range;
5421 struct ocfs2_extent_rec *rec;
5422 struct ocfs2_extent_list *el;
5423 struct ocfs2_path *path = NULL;
5425 ocfs2_et_extent_map_truncate(et, 0);
5427 path = ocfs2_new_path_from_et(et);
5428 if (!path) {
5429 ret = -ENOMEM;
5430 mlog_errno(ret);
5431 goto out;
5434 ret = ocfs2_find_path(et->et_ci, path, cpos);
5435 if (ret) {
5436 mlog_errno(ret);
5437 goto out;
5440 el = path_leaf_el(path);
5441 index = ocfs2_search_extent_list(el, cpos);
5442 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5443 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5444 "Owner %llu has an extent at cpos %u which can no "
5445 "longer be found.\n",
5446 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5447 cpos);
5448 ret = -EROFS;
5449 goto out;
5453 * We have 3 cases of extent removal:
5454 * 1) Range covers the entire extent rec
5455 * 2) Range begins or ends on one edge of the extent rec
5456 * 3) Range is in the middle of the extent rec (no shared edges)
5458 * For case 1 we remove the extent rec and left rotate to
5459 * fill the hole.
5461 * For case 2 we just shrink the existing extent rec, with a
5462 * tree update if the shrinking edge is also the edge of an
5463 * extent block.
5465 * For case 3 we do a right split to turn the extent rec into
5466 * something case 2 can handle.
5468 rec = &el->l_recs[index];
5469 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5470 trunc_range = cpos + len;
5472 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5474 mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
5475 "(cpos %u, len %u)\n",
5476 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5477 cpos, len, index,
5478 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5480 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5481 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5482 cpos, len);
5483 if (ret) {
5484 mlog_errno(ret);
5485 goto out;
5487 } else {
5488 ret = ocfs2_split_tree(handle, et, path, index,
5489 trunc_range, meta_ac);
5490 if (ret) {
5491 mlog_errno(ret);
5492 goto out;
5496 * The split could have manipulated the tree enough to
5497 * move the record location, so we have to look for it again.
5499 ocfs2_reinit_path(path, 1);
5501 ret = ocfs2_find_path(et->et_ci, path, cpos);
5502 if (ret) {
5503 mlog_errno(ret);
5504 goto out;
5507 el = path_leaf_el(path);
5508 index = ocfs2_search_extent_list(el, cpos);
5509 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5510 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5511 "Owner %llu: split at cpos %u lost record.",
5512 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5513 cpos);
5514 ret = -EROFS;
5515 goto out;
5519 * Double check our values here. If anything is fishy,
5520 * it's easier to catch it at the top level.
5522 rec = &el->l_recs[index];
5523 rec_range = le32_to_cpu(rec->e_cpos) +
5524 ocfs2_rec_clusters(el, rec);
5525 if (rec_range != trunc_range) {
5526 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5527 "Owner %llu: error after split at cpos %u"
5528 "trunc len %u, existing record is (%u,%u)",
5529 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5530 cpos, len, le32_to_cpu(rec->e_cpos),
5531 ocfs2_rec_clusters(el, rec));
5532 ret = -EROFS;
5533 goto out;
5536 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5537 cpos, len);
5538 if (ret) {
5539 mlog_errno(ret);
5540 goto out;
5544 out:
5545 ocfs2_free_path(path);
5546 return ret;
5550 * ocfs2_reserve_blocks_for_rec_trunc() would look basically the
5551 * same as ocfs2_lock_alloctors(), except for it accepts a blocks
5552 * number to reserve some extra blocks, and it only handles meta
5553 * data allocations.
5555 * Currently, only ocfs2_remove_btree_range() uses it for truncating
5556 * and punching holes.
5558 static int ocfs2_reserve_blocks_for_rec_trunc(struct inode *inode,
5559 struct ocfs2_extent_tree *et,
5560 u32 extents_to_split,
5561 struct ocfs2_alloc_context **ac,
5562 int extra_blocks)
5564 int ret = 0, num_free_extents;
5565 unsigned int max_recs_needed = 2 * extents_to_split;
5566 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5568 *ac = NULL;
5570 num_free_extents = ocfs2_num_free_extents(osb, et);
5571 if (num_free_extents < 0) {
5572 ret = num_free_extents;
5573 mlog_errno(ret);
5574 goto out;
5577 if (!num_free_extents ||
5578 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed))
5579 extra_blocks += ocfs2_extend_meta_needed(et->et_root_el);
5581 if (extra_blocks) {
5582 ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, ac);
5583 if (ret < 0) {
5584 if (ret != -ENOSPC)
5585 mlog_errno(ret);
5586 goto out;
5590 out:
5591 if (ret) {
5592 if (*ac) {
5593 ocfs2_free_alloc_context(*ac);
5594 *ac = NULL;
5598 return ret;
5601 int ocfs2_remove_btree_range(struct inode *inode,
5602 struct ocfs2_extent_tree *et,
5603 u32 cpos, u32 phys_cpos, u32 len, int flags,
5604 struct ocfs2_cached_dealloc_ctxt *dealloc,
5605 u64 refcount_loc)
5607 int ret, credits = 0, extra_blocks = 0;
5608 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5609 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5610 struct inode *tl_inode = osb->osb_tl_inode;
5611 handle_t *handle;
5612 struct ocfs2_alloc_context *meta_ac = NULL;
5613 struct ocfs2_refcount_tree *ref_tree = NULL;
5615 if ((flags & OCFS2_EXT_REFCOUNTED) && len) {
5616 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
5617 OCFS2_HAS_REFCOUNT_FL));
5619 ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
5620 &ref_tree, NULL);
5621 if (ret) {
5622 mlog_errno(ret);
5623 goto out;
5626 ret = ocfs2_prepare_refcount_change_for_del(inode,
5627 refcount_loc,
5628 phys_blkno,
5629 len,
5630 &credits,
5631 &extra_blocks);
5632 if (ret < 0) {
5633 mlog_errno(ret);
5634 goto out;
5638 ret = ocfs2_reserve_blocks_for_rec_trunc(inode, et, 1, &meta_ac,
5639 extra_blocks);
5640 if (ret) {
5641 mlog_errno(ret);
5642 return ret;
5645 mutex_lock(&tl_inode->i_mutex);
5647 if (ocfs2_truncate_log_needs_flush(osb)) {
5648 ret = __ocfs2_flush_truncate_log(osb);
5649 if (ret < 0) {
5650 mlog_errno(ret);
5651 goto out;
5655 handle = ocfs2_start_trans(osb,
5656 ocfs2_remove_extent_credits(osb->sb) + credits);
5657 if (IS_ERR(handle)) {
5658 ret = PTR_ERR(handle);
5659 mlog_errno(ret);
5660 goto out;
5663 ret = ocfs2_et_root_journal_access(handle, et,
5664 OCFS2_JOURNAL_ACCESS_WRITE);
5665 if (ret) {
5666 mlog_errno(ret);
5667 goto out;
5670 dquot_free_space_nodirty(inode,
5671 ocfs2_clusters_to_bytes(inode->i_sb, len));
5673 ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
5674 if (ret) {
5675 mlog_errno(ret);
5676 goto out_commit;
5679 ocfs2_et_update_clusters(et, -len);
5681 ocfs2_journal_dirty(handle, et->et_root_bh);
5683 if (phys_blkno) {
5684 if (flags & OCFS2_EXT_REFCOUNTED)
5685 ret = ocfs2_decrease_refcount(inode, handle,
5686 ocfs2_blocks_to_clusters(osb->sb,
5687 phys_blkno),
5688 len, meta_ac,
5689 dealloc, 1);
5690 else
5691 ret = ocfs2_truncate_log_append(osb, handle,
5692 phys_blkno, len);
5693 if (ret)
5694 mlog_errno(ret);
5698 out_commit:
5699 ocfs2_commit_trans(osb, handle);
5700 out:
5701 mutex_unlock(&tl_inode->i_mutex);
5703 if (meta_ac)
5704 ocfs2_free_alloc_context(meta_ac);
5706 if (ref_tree)
5707 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
5709 return ret;
5712 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5714 struct buffer_head *tl_bh = osb->osb_tl_bh;
5715 struct ocfs2_dinode *di;
5716 struct ocfs2_truncate_log *tl;
5718 di = (struct ocfs2_dinode *) tl_bh->b_data;
5719 tl = &di->id2.i_dealloc;
5721 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5722 "slot %d, invalid truncate log parameters: used = "
5723 "%u, count = %u\n", osb->slot_num,
5724 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5725 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5728 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5729 unsigned int new_start)
5731 unsigned int tail_index;
5732 unsigned int current_tail;
5734 /* No records, nothing to coalesce */
5735 if (!le16_to_cpu(tl->tl_used))
5736 return 0;
5738 tail_index = le16_to_cpu(tl->tl_used) - 1;
5739 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5740 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5742 return current_tail == new_start;
5745 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5746 handle_t *handle,
5747 u64 start_blk,
5748 unsigned int num_clusters)
5750 int status, index;
5751 unsigned int start_cluster, tl_count;
5752 struct inode *tl_inode = osb->osb_tl_inode;
5753 struct buffer_head *tl_bh = osb->osb_tl_bh;
5754 struct ocfs2_dinode *di;
5755 struct ocfs2_truncate_log *tl;
5757 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5758 (unsigned long long)start_blk, num_clusters);
5760 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5762 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5764 di = (struct ocfs2_dinode *) tl_bh->b_data;
5766 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5767 * by the underlying call to ocfs2_read_inode_block(), so any
5768 * corruption is a code bug */
5769 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5771 tl = &di->id2.i_dealloc;
5772 tl_count = le16_to_cpu(tl->tl_count);
5773 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5774 tl_count == 0,
5775 "Truncate record count on #%llu invalid "
5776 "wanted %u, actual %u\n",
5777 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5778 ocfs2_truncate_recs_per_inode(osb->sb),
5779 le16_to_cpu(tl->tl_count));
5781 /* Caller should have known to flush before calling us. */
5782 index = le16_to_cpu(tl->tl_used);
5783 if (index >= tl_count) {
5784 status = -ENOSPC;
5785 mlog_errno(status);
5786 goto bail;
5789 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5790 OCFS2_JOURNAL_ACCESS_WRITE);
5791 if (status < 0) {
5792 mlog_errno(status);
5793 goto bail;
5796 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5797 "%llu (index = %d)\n", num_clusters, start_cluster,
5798 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5800 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5802 * Move index back to the record we are coalescing with.
5803 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5805 index--;
5807 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5808 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5809 index, le32_to_cpu(tl->tl_recs[index].t_start),
5810 num_clusters);
5811 } else {
5812 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5813 tl->tl_used = cpu_to_le16(index + 1);
5815 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5817 ocfs2_journal_dirty(handle, tl_bh);
5819 bail:
5820 mlog_exit(status);
5821 return status;
5824 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5825 handle_t *handle,
5826 struct inode *data_alloc_inode,
5827 struct buffer_head *data_alloc_bh)
5829 int status = 0;
5830 int i;
5831 unsigned int num_clusters;
5832 u64 start_blk;
5833 struct ocfs2_truncate_rec rec;
5834 struct ocfs2_dinode *di;
5835 struct ocfs2_truncate_log *tl;
5836 struct inode *tl_inode = osb->osb_tl_inode;
5837 struct buffer_head *tl_bh = osb->osb_tl_bh;
5839 mlog_entry_void();
5841 di = (struct ocfs2_dinode *) tl_bh->b_data;
5842 tl = &di->id2.i_dealloc;
5843 i = le16_to_cpu(tl->tl_used) - 1;
5844 while (i >= 0) {
5845 /* Caller has given us at least enough credits to
5846 * update the truncate log dinode */
5847 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5848 OCFS2_JOURNAL_ACCESS_WRITE);
5849 if (status < 0) {
5850 mlog_errno(status);
5851 goto bail;
5854 tl->tl_used = cpu_to_le16(i);
5856 ocfs2_journal_dirty(handle, tl_bh);
5858 /* TODO: Perhaps we can calculate the bulk of the
5859 * credits up front rather than extending like
5860 * this. */
5861 status = ocfs2_extend_trans(handle,
5862 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5863 if (status < 0) {
5864 mlog_errno(status);
5865 goto bail;
5868 rec = tl->tl_recs[i];
5869 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5870 le32_to_cpu(rec.t_start));
5871 num_clusters = le32_to_cpu(rec.t_clusters);
5873 /* if start_blk is not set, we ignore the record as
5874 * invalid. */
5875 if (start_blk) {
5876 mlog(0, "free record %d, start = %u, clusters = %u\n",
5877 i, le32_to_cpu(rec.t_start), num_clusters);
5879 status = ocfs2_free_clusters(handle, data_alloc_inode,
5880 data_alloc_bh, start_blk,
5881 num_clusters);
5882 if (status < 0) {
5883 mlog_errno(status);
5884 goto bail;
5887 i--;
5890 bail:
5891 mlog_exit(status);
5892 return status;
5895 /* Expects you to already be holding tl_inode->i_mutex */
5896 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5898 int status;
5899 unsigned int num_to_flush;
5900 handle_t *handle;
5901 struct inode *tl_inode = osb->osb_tl_inode;
5902 struct inode *data_alloc_inode = NULL;
5903 struct buffer_head *tl_bh = osb->osb_tl_bh;
5904 struct buffer_head *data_alloc_bh = NULL;
5905 struct ocfs2_dinode *di;
5906 struct ocfs2_truncate_log *tl;
5908 mlog_entry_void();
5910 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5912 di = (struct ocfs2_dinode *) tl_bh->b_data;
5914 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5915 * by the underlying call to ocfs2_read_inode_block(), so any
5916 * corruption is a code bug */
5917 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5919 tl = &di->id2.i_dealloc;
5920 num_to_flush = le16_to_cpu(tl->tl_used);
5921 mlog(0, "Flush %u records from truncate log #%llu\n",
5922 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5923 if (!num_to_flush) {
5924 status = 0;
5925 goto out;
5928 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5929 GLOBAL_BITMAP_SYSTEM_INODE,
5930 OCFS2_INVALID_SLOT);
5931 if (!data_alloc_inode) {
5932 status = -EINVAL;
5933 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5934 goto out;
5937 mutex_lock(&data_alloc_inode->i_mutex);
5939 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5940 if (status < 0) {
5941 mlog_errno(status);
5942 goto out_mutex;
5945 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5946 if (IS_ERR(handle)) {
5947 status = PTR_ERR(handle);
5948 mlog_errno(status);
5949 goto out_unlock;
5952 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5953 data_alloc_bh);
5954 if (status < 0)
5955 mlog_errno(status);
5957 ocfs2_commit_trans(osb, handle);
5959 out_unlock:
5960 brelse(data_alloc_bh);
5961 ocfs2_inode_unlock(data_alloc_inode, 1);
5963 out_mutex:
5964 mutex_unlock(&data_alloc_inode->i_mutex);
5965 iput(data_alloc_inode);
5967 out:
5968 mlog_exit(status);
5969 return status;
5972 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5974 int status;
5975 struct inode *tl_inode = osb->osb_tl_inode;
5977 mutex_lock(&tl_inode->i_mutex);
5978 status = __ocfs2_flush_truncate_log(osb);
5979 mutex_unlock(&tl_inode->i_mutex);
5981 return status;
5984 static void ocfs2_truncate_log_worker(struct work_struct *work)
5986 int status;
5987 struct ocfs2_super *osb =
5988 container_of(work, struct ocfs2_super,
5989 osb_truncate_log_wq.work);
5991 mlog_entry_void();
5993 status = ocfs2_flush_truncate_log(osb);
5994 if (status < 0)
5995 mlog_errno(status);
5996 else
5997 ocfs2_init_steal_slots(osb);
5999 mlog_exit(status);
6002 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
6003 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
6004 int cancel)
6006 if (osb->osb_tl_inode) {
6007 /* We want to push off log flushes while truncates are
6008 * still running. */
6009 if (cancel)
6010 cancel_delayed_work(&osb->osb_truncate_log_wq);
6012 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
6013 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
6017 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
6018 int slot_num,
6019 struct inode **tl_inode,
6020 struct buffer_head **tl_bh)
6022 int status;
6023 struct inode *inode = NULL;
6024 struct buffer_head *bh = NULL;
6026 inode = ocfs2_get_system_file_inode(osb,
6027 TRUNCATE_LOG_SYSTEM_INODE,
6028 slot_num);
6029 if (!inode) {
6030 status = -EINVAL;
6031 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
6032 goto bail;
6035 status = ocfs2_read_inode_block(inode, &bh);
6036 if (status < 0) {
6037 iput(inode);
6038 mlog_errno(status);
6039 goto bail;
6042 *tl_inode = inode;
6043 *tl_bh = bh;
6044 bail:
6045 mlog_exit(status);
6046 return status;
6049 /* called during the 1st stage of node recovery. we stamp a clean
6050 * truncate log and pass back a copy for processing later. if the
6051 * truncate log does not require processing, a *tl_copy is set to
6052 * NULL. */
6053 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6054 int slot_num,
6055 struct ocfs2_dinode **tl_copy)
6057 int status;
6058 struct inode *tl_inode = NULL;
6059 struct buffer_head *tl_bh = NULL;
6060 struct ocfs2_dinode *di;
6061 struct ocfs2_truncate_log *tl;
6063 *tl_copy = NULL;
6065 mlog(0, "recover truncate log from slot %d\n", slot_num);
6067 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6068 if (status < 0) {
6069 mlog_errno(status);
6070 goto bail;
6073 di = (struct ocfs2_dinode *) tl_bh->b_data;
6075 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6076 * validated by the underlying call to ocfs2_read_inode_block(),
6077 * so any corruption is a code bug */
6078 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6080 tl = &di->id2.i_dealloc;
6081 if (le16_to_cpu(tl->tl_used)) {
6082 mlog(0, "We'll have %u logs to recover\n",
6083 le16_to_cpu(tl->tl_used));
6085 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6086 if (!(*tl_copy)) {
6087 status = -ENOMEM;
6088 mlog_errno(status);
6089 goto bail;
6092 /* Assuming the write-out below goes well, this copy
6093 * will be passed back to recovery for processing. */
6094 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6096 /* All we need to do to clear the truncate log is set
6097 * tl_used. */
6098 tl->tl_used = 0;
6100 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
6101 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
6102 if (status < 0) {
6103 mlog_errno(status);
6104 goto bail;
6108 bail:
6109 if (tl_inode)
6110 iput(tl_inode);
6111 brelse(tl_bh);
6113 if (status < 0 && (*tl_copy)) {
6114 kfree(*tl_copy);
6115 *tl_copy = NULL;
6118 mlog_exit(status);
6119 return status;
6122 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6123 struct ocfs2_dinode *tl_copy)
6125 int status = 0;
6126 int i;
6127 unsigned int clusters, num_recs, start_cluster;
6128 u64 start_blk;
6129 handle_t *handle;
6130 struct inode *tl_inode = osb->osb_tl_inode;
6131 struct ocfs2_truncate_log *tl;
6133 mlog_entry_void();
6135 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6136 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6137 return -EINVAL;
6140 tl = &tl_copy->id2.i_dealloc;
6141 num_recs = le16_to_cpu(tl->tl_used);
6142 mlog(0, "cleanup %u records from %llu\n", num_recs,
6143 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
6145 mutex_lock(&tl_inode->i_mutex);
6146 for(i = 0; i < num_recs; i++) {
6147 if (ocfs2_truncate_log_needs_flush(osb)) {
6148 status = __ocfs2_flush_truncate_log(osb);
6149 if (status < 0) {
6150 mlog_errno(status);
6151 goto bail_up;
6155 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6156 if (IS_ERR(handle)) {
6157 status = PTR_ERR(handle);
6158 mlog_errno(status);
6159 goto bail_up;
6162 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6163 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6164 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6166 status = ocfs2_truncate_log_append(osb, handle,
6167 start_blk, clusters);
6168 ocfs2_commit_trans(osb, handle);
6169 if (status < 0) {
6170 mlog_errno(status);
6171 goto bail_up;
6175 bail_up:
6176 mutex_unlock(&tl_inode->i_mutex);
6178 mlog_exit(status);
6179 return status;
6182 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6184 int status;
6185 struct inode *tl_inode = osb->osb_tl_inode;
6187 mlog_entry_void();
6189 if (tl_inode) {
6190 cancel_delayed_work(&osb->osb_truncate_log_wq);
6191 flush_workqueue(ocfs2_wq);
6193 status = ocfs2_flush_truncate_log(osb);
6194 if (status < 0)
6195 mlog_errno(status);
6197 brelse(osb->osb_tl_bh);
6198 iput(osb->osb_tl_inode);
6201 mlog_exit_void();
6204 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6206 int status;
6207 struct inode *tl_inode = NULL;
6208 struct buffer_head *tl_bh = NULL;
6210 mlog_entry_void();
6212 status = ocfs2_get_truncate_log_info(osb,
6213 osb->slot_num,
6214 &tl_inode,
6215 &tl_bh);
6216 if (status < 0)
6217 mlog_errno(status);
6219 /* ocfs2_truncate_log_shutdown keys on the existence of
6220 * osb->osb_tl_inode so we don't set any of the osb variables
6221 * until we're sure all is well. */
6222 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6223 ocfs2_truncate_log_worker);
6224 osb->osb_tl_bh = tl_bh;
6225 osb->osb_tl_inode = tl_inode;
6227 mlog_exit(status);
6228 return status;
6232 * Delayed de-allocation of suballocator blocks.
6234 * Some sets of block de-allocations might involve multiple suballocator inodes.
6236 * The locking for this can get extremely complicated, especially when
6237 * the suballocator inodes to delete from aren't known until deep
6238 * within an unrelated codepath.
6240 * ocfs2_extent_block structures are a good example of this - an inode
6241 * btree could have been grown by any number of nodes each allocating
6242 * out of their own suballoc inode.
6244 * These structures allow the delay of block de-allocation until a
6245 * later time, when locking of multiple cluster inodes won't cause
6246 * deadlock.
6250 * Describe a single bit freed from a suballocator. For the block
6251 * suballocators, it represents one block. For the global cluster
6252 * allocator, it represents some clusters and free_bit indicates
6253 * clusters number.
6255 struct ocfs2_cached_block_free {
6256 struct ocfs2_cached_block_free *free_next;
6257 u64 free_bg;
6258 u64 free_blk;
6259 unsigned int free_bit;
6262 struct ocfs2_per_slot_free_list {
6263 struct ocfs2_per_slot_free_list *f_next_suballocator;
6264 int f_inode_type;
6265 int f_slot;
6266 struct ocfs2_cached_block_free *f_first;
6269 static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6270 int sysfile_type,
6271 int slot,
6272 struct ocfs2_cached_block_free *head)
6274 int ret;
6275 u64 bg_blkno;
6276 handle_t *handle;
6277 struct inode *inode;
6278 struct buffer_head *di_bh = NULL;
6279 struct ocfs2_cached_block_free *tmp;
6281 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6282 if (!inode) {
6283 ret = -EINVAL;
6284 mlog_errno(ret);
6285 goto out;
6288 mutex_lock(&inode->i_mutex);
6290 ret = ocfs2_inode_lock(inode, &di_bh, 1);
6291 if (ret) {
6292 mlog_errno(ret);
6293 goto out_mutex;
6296 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6297 if (IS_ERR(handle)) {
6298 ret = PTR_ERR(handle);
6299 mlog_errno(ret);
6300 goto out_unlock;
6303 while (head) {
6304 if (head->free_bg)
6305 bg_blkno = head->free_bg;
6306 else
6307 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6308 head->free_bit);
6309 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6310 head->free_bit, (unsigned long long)head->free_blk);
6312 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6313 head->free_bit, bg_blkno, 1);
6314 if (ret) {
6315 mlog_errno(ret);
6316 goto out_journal;
6319 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6320 if (ret) {
6321 mlog_errno(ret);
6322 goto out_journal;
6325 tmp = head;
6326 head = head->free_next;
6327 kfree(tmp);
6330 out_journal:
6331 ocfs2_commit_trans(osb, handle);
6333 out_unlock:
6334 ocfs2_inode_unlock(inode, 1);
6335 brelse(di_bh);
6336 out_mutex:
6337 mutex_unlock(&inode->i_mutex);
6338 iput(inode);
6339 out:
6340 while(head) {
6341 /* Premature exit may have left some dangling items. */
6342 tmp = head;
6343 head = head->free_next;
6344 kfree(tmp);
6347 return ret;
6350 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6351 u64 blkno, unsigned int bit)
6353 int ret = 0;
6354 struct ocfs2_cached_block_free *item;
6356 item = kzalloc(sizeof(*item), GFP_NOFS);
6357 if (item == NULL) {
6358 ret = -ENOMEM;
6359 mlog_errno(ret);
6360 return ret;
6363 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6364 bit, (unsigned long long)blkno);
6366 item->free_blk = blkno;
6367 item->free_bit = bit;
6368 item->free_next = ctxt->c_global_allocator;
6370 ctxt->c_global_allocator = item;
6371 return ret;
6374 static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6375 struct ocfs2_cached_block_free *head)
6377 struct ocfs2_cached_block_free *tmp;
6378 struct inode *tl_inode = osb->osb_tl_inode;
6379 handle_t *handle;
6380 int ret = 0;
6382 mutex_lock(&tl_inode->i_mutex);
6384 while (head) {
6385 if (ocfs2_truncate_log_needs_flush(osb)) {
6386 ret = __ocfs2_flush_truncate_log(osb);
6387 if (ret < 0) {
6388 mlog_errno(ret);
6389 break;
6393 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6394 if (IS_ERR(handle)) {
6395 ret = PTR_ERR(handle);
6396 mlog_errno(ret);
6397 break;
6400 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6401 head->free_bit);
6403 ocfs2_commit_trans(osb, handle);
6404 tmp = head;
6405 head = head->free_next;
6406 kfree(tmp);
6408 if (ret < 0) {
6409 mlog_errno(ret);
6410 break;
6414 mutex_unlock(&tl_inode->i_mutex);
6416 while (head) {
6417 /* Premature exit may have left some dangling items. */
6418 tmp = head;
6419 head = head->free_next;
6420 kfree(tmp);
6423 return ret;
6426 int ocfs2_run_deallocs(struct ocfs2_super *osb,
6427 struct ocfs2_cached_dealloc_ctxt *ctxt)
6429 int ret = 0, ret2;
6430 struct ocfs2_per_slot_free_list *fl;
6432 if (!ctxt)
6433 return 0;
6435 while (ctxt->c_first_suballocator) {
6436 fl = ctxt->c_first_suballocator;
6438 if (fl->f_first) {
6439 mlog(0, "Free items: (type %u, slot %d)\n",
6440 fl->f_inode_type, fl->f_slot);
6441 ret2 = ocfs2_free_cached_blocks(osb,
6442 fl->f_inode_type,
6443 fl->f_slot,
6444 fl->f_first);
6445 if (ret2)
6446 mlog_errno(ret2);
6447 if (!ret)
6448 ret = ret2;
6451 ctxt->c_first_suballocator = fl->f_next_suballocator;
6452 kfree(fl);
6455 if (ctxt->c_global_allocator) {
6456 ret2 = ocfs2_free_cached_clusters(osb,
6457 ctxt->c_global_allocator);
6458 if (ret2)
6459 mlog_errno(ret2);
6460 if (!ret)
6461 ret = ret2;
6463 ctxt->c_global_allocator = NULL;
6466 return ret;
6469 static struct ocfs2_per_slot_free_list *
6470 ocfs2_find_per_slot_free_list(int type,
6471 int slot,
6472 struct ocfs2_cached_dealloc_ctxt *ctxt)
6474 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6476 while (fl) {
6477 if (fl->f_inode_type == type && fl->f_slot == slot)
6478 return fl;
6480 fl = fl->f_next_suballocator;
6483 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6484 if (fl) {
6485 fl->f_inode_type = type;
6486 fl->f_slot = slot;
6487 fl->f_first = NULL;
6488 fl->f_next_suballocator = ctxt->c_first_suballocator;
6490 ctxt->c_first_suballocator = fl;
6492 return fl;
6495 int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6496 int type, int slot, u64 suballoc,
6497 u64 blkno, unsigned int bit)
6499 int ret;
6500 struct ocfs2_per_slot_free_list *fl;
6501 struct ocfs2_cached_block_free *item;
6503 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6504 if (fl == NULL) {
6505 ret = -ENOMEM;
6506 mlog_errno(ret);
6507 goto out;
6510 item = kzalloc(sizeof(*item), GFP_NOFS);
6511 if (item == NULL) {
6512 ret = -ENOMEM;
6513 mlog_errno(ret);
6514 goto out;
6517 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6518 type, slot, bit, (unsigned long long)blkno);
6520 item->free_bg = suballoc;
6521 item->free_blk = blkno;
6522 item->free_bit = bit;
6523 item->free_next = fl->f_first;
6525 fl->f_first = item;
6527 ret = 0;
6528 out:
6529 return ret;
6532 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6533 struct ocfs2_extent_block *eb)
6535 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6536 le16_to_cpu(eb->h_suballoc_slot),
6537 le64_to_cpu(eb->h_suballoc_loc),
6538 le64_to_cpu(eb->h_blkno),
6539 le16_to_cpu(eb->h_suballoc_bit));
6542 static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
6544 set_buffer_uptodate(bh);
6545 mark_buffer_dirty(bh);
6546 return 0;
6549 void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6550 unsigned int from, unsigned int to,
6551 struct page *page, int zero, u64 *phys)
6553 int ret, partial = 0;
6555 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6556 if (ret)
6557 mlog_errno(ret);
6559 if (zero)
6560 zero_user_segment(page, from, to);
6563 * Need to set the buffers we zero'd into uptodate
6564 * here if they aren't - ocfs2_map_page_blocks()
6565 * might've skipped some
6567 ret = walk_page_buffers(handle, page_buffers(page),
6568 from, to, &partial,
6569 ocfs2_zero_func);
6570 if (ret < 0)
6571 mlog_errno(ret);
6572 else if (ocfs2_should_order_data(inode)) {
6573 ret = ocfs2_jbd2_file_inode(handle, inode);
6574 if (ret < 0)
6575 mlog_errno(ret);
6578 if (!partial)
6579 SetPageUptodate(page);
6581 flush_dcache_page(page);
6584 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6585 loff_t end, struct page **pages,
6586 int numpages, u64 phys, handle_t *handle)
6588 int i;
6589 struct page *page;
6590 unsigned int from, to = PAGE_CACHE_SIZE;
6591 struct super_block *sb = inode->i_sb;
6593 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6595 if (numpages == 0)
6596 goto out;
6598 to = PAGE_CACHE_SIZE;
6599 for(i = 0; i < numpages; i++) {
6600 page = pages[i];
6602 from = start & (PAGE_CACHE_SIZE - 1);
6603 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6604 to = end & (PAGE_CACHE_SIZE - 1);
6606 BUG_ON(from > PAGE_CACHE_SIZE);
6607 BUG_ON(to > PAGE_CACHE_SIZE);
6609 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6610 &phys);
6612 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6614 out:
6615 if (pages)
6616 ocfs2_unlock_and_free_pages(pages, numpages);
6619 int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
6620 struct page **pages, int *num)
6622 int numpages, ret = 0;
6623 struct address_space *mapping = inode->i_mapping;
6624 unsigned long index;
6625 loff_t last_page_bytes;
6627 BUG_ON(start > end);
6629 numpages = 0;
6630 last_page_bytes = PAGE_ALIGN(end);
6631 index = start >> PAGE_CACHE_SHIFT;
6632 do {
6633 pages[numpages] = find_or_create_page(mapping, index, GFP_NOFS);
6634 if (!pages[numpages]) {
6635 ret = -ENOMEM;
6636 mlog_errno(ret);
6637 goto out;
6640 numpages++;
6641 index++;
6642 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
6644 out:
6645 if (ret != 0) {
6646 if (pages)
6647 ocfs2_unlock_and_free_pages(pages, numpages);
6648 numpages = 0;
6651 *num = numpages;
6653 return ret;
6656 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6657 struct page **pages, int *num)
6659 struct super_block *sb = inode->i_sb;
6661 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6662 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6664 return ocfs2_grab_pages(inode, start, end, pages, num);
6668 * Zero the area past i_size but still within an allocated
6669 * cluster. This avoids exposing nonzero data on subsequent file
6670 * extends.
6672 * We need to call this before i_size is updated on the inode because
6673 * otherwise block_write_full_page() will skip writeout of pages past
6674 * i_size. The new_i_size parameter is passed for this reason.
6676 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6677 u64 range_start, u64 range_end)
6679 int ret = 0, numpages;
6680 struct page **pages = NULL;
6681 u64 phys;
6682 unsigned int ext_flags;
6683 struct super_block *sb = inode->i_sb;
6686 * File systems which don't support sparse files zero on every
6687 * extend.
6689 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
6690 return 0;
6692 pages = kcalloc(ocfs2_pages_per_cluster(sb),
6693 sizeof(struct page *), GFP_NOFS);
6694 if (pages == NULL) {
6695 ret = -ENOMEM;
6696 mlog_errno(ret);
6697 goto out;
6700 if (range_start == range_end)
6701 goto out;
6703 ret = ocfs2_extent_map_get_blocks(inode,
6704 range_start >> sb->s_blocksize_bits,
6705 &phys, NULL, &ext_flags);
6706 if (ret) {
6707 mlog_errno(ret);
6708 goto out;
6712 * Tail is a hole, or is marked unwritten. In either case, we
6713 * can count on read and write to return/push zero's.
6715 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
6716 goto out;
6718 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6719 &numpages);
6720 if (ret) {
6721 mlog_errno(ret);
6722 goto out;
6725 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6726 numpages, phys, handle);
6729 * Initiate writeout of the pages we zero'd here. We don't
6730 * wait on them - the truncate_inode_pages() call later will
6731 * do that for us.
6733 ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
6734 range_end - 1);
6735 if (ret)
6736 mlog_errno(ret);
6738 out:
6739 if (pages)
6740 kfree(pages);
6742 return ret;
6745 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6746 struct ocfs2_dinode *di)
6748 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
6749 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
6751 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6752 memset(&di->id2, 0, blocksize -
6753 offsetof(struct ocfs2_dinode, id2) -
6754 xattrsize);
6755 else
6756 memset(&di->id2, 0, blocksize -
6757 offsetof(struct ocfs2_dinode, id2));
6760 void ocfs2_dinode_new_extent_list(struct inode *inode,
6761 struct ocfs2_dinode *di)
6763 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6764 di->id2.i_list.l_tree_depth = 0;
6765 di->id2.i_list.l_next_free_rec = 0;
6766 di->id2.i_list.l_count = cpu_to_le16(
6767 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
6770 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6772 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6773 struct ocfs2_inline_data *idata = &di->id2.i_data;
6775 spin_lock(&oi->ip_lock);
6776 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6777 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6778 spin_unlock(&oi->ip_lock);
6781 * We clear the entire i_data structure here so that all
6782 * fields can be properly initialized.
6784 ocfs2_zero_dinode_id2_with_xattr(inode, di);
6786 idata->id_count = cpu_to_le16(
6787 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
6790 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6791 struct buffer_head *di_bh)
6793 int ret, i, has_data, num_pages = 0;
6794 handle_t *handle;
6795 u64 uninitialized_var(block);
6796 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6797 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6798 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6799 struct ocfs2_alloc_context *data_ac = NULL;
6800 struct page **pages = NULL;
6801 loff_t end = osb->s_clustersize;
6802 struct ocfs2_extent_tree et;
6803 int did_quota = 0;
6805 has_data = i_size_read(inode) ? 1 : 0;
6807 if (has_data) {
6808 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6809 sizeof(struct page *), GFP_NOFS);
6810 if (pages == NULL) {
6811 ret = -ENOMEM;
6812 mlog_errno(ret);
6813 goto out;
6816 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6817 if (ret) {
6818 mlog_errno(ret);
6819 goto out;
6823 handle = ocfs2_start_trans(osb,
6824 ocfs2_inline_to_extents_credits(osb->sb));
6825 if (IS_ERR(handle)) {
6826 ret = PTR_ERR(handle);
6827 mlog_errno(ret);
6828 goto out_unlock;
6831 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
6832 OCFS2_JOURNAL_ACCESS_WRITE);
6833 if (ret) {
6834 mlog_errno(ret);
6835 goto out_commit;
6838 if (has_data) {
6839 u32 bit_off, num;
6840 unsigned int page_end;
6841 u64 phys;
6843 ret = dquot_alloc_space_nodirty(inode,
6844 ocfs2_clusters_to_bytes(osb->sb, 1));
6845 if (ret)
6846 goto out_commit;
6847 did_quota = 1;
6849 data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
6851 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
6852 &num);
6853 if (ret) {
6854 mlog_errno(ret);
6855 goto out_commit;
6859 * Save two copies, one for insert, and one that can
6860 * be changed by ocfs2_map_and_dirty_page() below.
6862 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6865 * Non sparse file systems zero on extend, so no need
6866 * to do that now.
6868 if (!ocfs2_sparse_alloc(osb) &&
6869 PAGE_CACHE_SIZE < osb->s_clustersize)
6870 end = PAGE_CACHE_SIZE;
6872 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6873 if (ret) {
6874 mlog_errno(ret);
6875 goto out_commit;
6879 * This should populate the 1st page for us and mark
6880 * it up to date.
6882 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6883 if (ret) {
6884 mlog_errno(ret);
6885 goto out_commit;
6888 page_end = PAGE_CACHE_SIZE;
6889 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6890 page_end = osb->s_clustersize;
6892 for (i = 0; i < num_pages; i++)
6893 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6894 pages[i], i > 0, &phys);
6897 spin_lock(&oi->ip_lock);
6898 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6899 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6900 spin_unlock(&oi->ip_lock);
6902 ocfs2_dinode_new_extent_list(inode, di);
6904 ocfs2_journal_dirty(handle, di_bh);
6906 if (has_data) {
6908 * An error at this point should be extremely rare. If
6909 * this proves to be false, we could always re-build
6910 * the in-inode data from our pages.
6912 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
6913 ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
6914 if (ret) {
6915 mlog_errno(ret);
6916 goto out_commit;
6919 inode->i_blocks = ocfs2_inode_sector_count(inode);
6922 out_commit:
6923 if (ret < 0 && did_quota)
6924 dquot_free_space_nodirty(inode,
6925 ocfs2_clusters_to_bytes(osb->sb, 1));
6927 ocfs2_commit_trans(osb, handle);
6929 out_unlock:
6930 if (data_ac)
6931 ocfs2_free_alloc_context(data_ac);
6933 out:
6934 if (pages) {
6935 ocfs2_unlock_and_free_pages(pages, num_pages);
6936 kfree(pages);
6939 return ret;
6943 * It is expected, that by the time you call this function,
6944 * inode->i_size and fe->i_size have been adjusted.
6946 * WARNING: This will kfree the truncate context
6948 int ocfs2_commit_truncate(struct ocfs2_super *osb,
6949 struct inode *inode,
6950 struct buffer_head *di_bh)
6952 int status = 0, i, flags = 0;
6953 u32 new_highest_cpos, range, trunc_cpos, trunc_len, phys_cpos, coff;
6954 u64 blkno = 0;
6955 struct ocfs2_extent_list *el;
6956 struct ocfs2_extent_rec *rec;
6957 struct ocfs2_path *path = NULL;
6958 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6959 struct ocfs2_extent_list *root_el = &(di->id2.i_list);
6960 u64 refcount_loc = le64_to_cpu(di->i_refcount_loc);
6961 struct ocfs2_extent_tree et;
6962 struct ocfs2_cached_dealloc_ctxt dealloc;
6964 mlog_entry_void();
6966 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
6967 ocfs2_init_dealloc_ctxt(&dealloc);
6969 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
6970 i_size_read(inode));
6972 path = ocfs2_new_path(di_bh, &di->id2.i_list,
6973 ocfs2_journal_access_di);
6974 if (!path) {
6975 status = -ENOMEM;
6976 mlog_errno(status);
6977 goto bail;
6980 ocfs2_extent_map_trunc(inode, new_highest_cpos);
6982 start:
6984 * Check that we still have allocation to delete.
6986 if (OCFS2_I(inode)->ip_clusters == 0) {
6987 status = 0;
6988 goto bail;
6992 * Truncate always works against the rightmost tree branch.
6994 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
6995 if (status) {
6996 mlog_errno(status);
6997 goto bail;
7000 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7001 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7004 * By now, el will point to the extent list on the bottom most
7005 * portion of this tree. Only the tail record is considered in
7006 * each pass.
7008 * We handle the following cases, in order:
7009 * - empty extent: delete the remaining branch
7010 * - remove the entire record
7011 * - remove a partial record
7012 * - no record needs to be removed (truncate has completed)
7014 el = path_leaf_el(path);
7015 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7016 ocfs2_error(inode->i_sb,
7017 "Inode %llu has empty extent block at %llu\n",
7018 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7019 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7020 status = -EROFS;
7021 goto bail;
7024 i = le16_to_cpu(el->l_next_free_rec) - 1;
7025 rec = &el->l_recs[i];
7026 flags = rec->e_flags;
7027 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
7029 if (i == 0 && ocfs2_is_empty_extent(rec)) {
7031 * Lower levels depend on this never happening, but it's best
7032 * to check it up here before changing the tree.
7034 if (root_el->l_tree_depth && rec->e_int_clusters == 0) {
7035 ocfs2_error(inode->i_sb, "Inode %lu has an empty "
7036 "extent record, depth %u\n", inode->i_ino,
7037 le16_to_cpu(root_el->l_tree_depth));
7038 status = -EROFS;
7039 goto bail;
7041 trunc_cpos = le32_to_cpu(rec->e_cpos);
7042 trunc_len = 0;
7043 blkno = 0;
7044 } else if (le32_to_cpu(rec->e_cpos) >= new_highest_cpos) {
7046 * Truncate entire record.
7048 trunc_cpos = le32_to_cpu(rec->e_cpos);
7049 trunc_len = ocfs2_rec_clusters(el, rec);
7050 blkno = le64_to_cpu(rec->e_blkno);
7051 } else if (range > new_highest_cpos) {
7053 * Partial truncate. it also should be
7054 * the last truncate we're doing.
7056 trunc_cpos = new_highest_cpos;
7057 trunc_len = range - new_highest_cpos;
7058 coff = new_highest_cpos - le32_to_cpu(rec->e_cpos);
7059 blkno = le64_to_cpu(rec->e_blkno) +
7060 ocfs2_clusters_to_blocks(inode->i_sb, coff);
7061 } else {
7063 * Truncate completed, leave happily.
7065 status = 0;
7066 goto bail;
7069 phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
7071 status = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
7072 phys_cpos, trunc_len, flags, &dealloc,
7073 refcount_loc);
7074 if (status < 0) {
7075 mlog_errno(status);
7076 goto bail;
7079 ocfs2_reinit_path(path, 1);
7082 * The check above will catch the case where we've truncated
7083 * away all allocation.
7085 goto start;
7087 bail:
7089 ocfs2_schedule_truncate_log_flush(osb, 1);
7091 ocfs2_run_deallocs(osb, &dealloc);
7093 ocfs2_free_path(path);
7095 mlog_exit(status);
7096 return status;
7100 * Expects the inode to already be locked.
7102 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7103 struct inode *inode,
7104 struct buffer_head *fe_bh,
7105 struct ocfs2_truncate_context **tc)
7107 int status;
7108 unsigned int new_i_clusters;
7109 struct ocfs2_dinode *fe;
7110 struct ocfs2_extent_block *eb;
7111 struct buffer_head *last_eb_bh = NULL;
7113 mlog_entry_void();
7115 *tc = NULL;
7117 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7118 i_size_read(inode));
7119 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7121 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7122 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7123 (unsigned long long)le64_to_cpu(fe->i_size));
7125 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7126 if (!(*tc)) {
7127 status = -ENOMEM;
7128 mlog_errno(status);
7129 goto bail;
7131 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7133 if (fe->id2.i_list.l_tree_depth) {
7134 status = ocfs2_read_extent_block(INODE_CACHE(inode),
7135 le64_to_cpu(fe->i_last_eb_blk),
7136 &last_eb_bh);
7137 if (status < 0) {
7138 mlog_errno(status);
7139 goto bail;
7141 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7144 (*tc)->tc_last_eb_bh = last_eb_bh;
7146 status = 0;
7147 bail:
7148 if (status < 0) {
7149 if (*tc)
7150 ocfs2_free_truncate_context(*tc);
7151 *tc = NULL;
7153 mlog_exit_void();
7154 return status;
7158 * 'start' is inclusive, 'end' is not.
7160 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7161 unsigned int start, unsigned int end, int trunc)
7163 int ret;
7164 unsigned int numbytes;
7165 handle_t *handle;
7166 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7167 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7168 struct ocfs2_inline_data *idata = &di->id2.i_data;
7170 if (end > i_size_read(inode))
7171 end = i_size_read(inode);
7173 BUG_ON(start >= end);
7175 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7176 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7177 !ocfs2_supports_inline_data(osb)) {
7178 ocfs2_error(inode->i_sb,
7179 "Inline data flags for inode %llu don't agree! "
7180 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7181 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7182 le16_to_cpu(di->i_dyn_features),
7183 OCFS2_I(inode)->ip_dyn_features,
7184 osb->s_feature_incompat);
7185 ret = -EROFS;
7186 goto out;
7189 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7190 if (IS_ERR(handle)) {
7191 ret = PTR_ERR(handle);
7192 mlog_errno(ret);
7193 goto out;
7196 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7197 OCFS2_JOURNAL_ACCESS_WRITE);
7198 if (ret) {
7199 mlog_errno(ret);
7200 goto out_commit;
7203 numbytes = end - start;
7204 memset(idata->id_data + start, 0, numbytes);
7207 * No need to worry about the data page here - it's been
7208 * truncated already and inline data doesn't need it for
7209 * pushing zero's to disk, so we'll let readpage pick it up
7210 * later.
7212 if (trunc) {
7213 i_size_write(inode, start);
7214 di->i_size = cpu_to_le64(start);
7217 inode->i_blocks = ocfs2_inode_sector_count(inode);
7218 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7220 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7221 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7223 ocfs2_journal_dirty(handle, di_bh);
7225 out_commit:
7226 ocfs2_commit_trans(osb, handle);
7228 out:
7229 return ret;
7232 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7235 * The caller is responsible for completing deallocation
7236 * before freeing the context.
7238 if (tc->tc_dealloc.c_first_suballocator != NULL)
7239 mlog(ML_NOTICE,
7240 "Truncate completion has non-empty dealloc context\n");
7242 brelse(tc->tc_last_eb_bh);
7244 kfree(tc);