ocfs2: use allocation reservations during file write
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / ocfs2 / alloc.c
bloba74ea700ffdc30d75a42e0b8297fde4f3b76cad8
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 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(osb,
1019 handle,
1020 meta_ac,
1021 wanted - count,
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_bit = cpu_to_le16(suballoc_bit_start);
1056 eb->h_list.l_count =
1057 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1059 suballoc_bit_start++;
1060 first_blkno++;
1062 /* We'll also be dirtied by the caller, so
1063 * this isn't absolutely necessary. */
1064 ocfs2_journal_dirty(handle, bhs[i]);
1067 count += num_got;
1070 status = 0;
1071 bail:
1072 if (status < 0) {
1073 for(i = 0; i < wanted; i++) {
1074 brelse(bhs[i]);
1075 bhs[i] = NULL;
1078 mlog_exit(status);
1079 return status;
1083 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1085 * Returns the sum of the rightmost extent rec logical offset and
1086 * cluster count.
1088 * ocfs2_add_branch() uses this to determine what logical cluster
1089 * value should be populated into the leftmost new branch records.
1091 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1092 * value for the new topmost tree record.
1094 static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1096 int i;
1098 i = le16_to_cpu(el->l_next_free_rec) - 1;
1100 return le32_to_cpu(el->l_recs[i].e_cpos) +
1101 ocfs2_rec_clusters(el, &el->l_recs[i]);
1105 * Change range of the branches in the right most path according to the leaf
1106 * extent block's rightmost record.
1108 static int ocfs2_adjust_rightmost_branch(handle_t *handle,
1109 struct ocfs2_extent_tree *et)
1111 int status;
1112 struct ocfs2_path *path = NULL;
1113 struct ocfs2_extent_list *el;
1114 struct ocfs2_extent_rec *rec;
1116 path = ocfs2_new_path_from_et(et);
1117 if (!path) {
1118 status = -ENOMEM;
1119 return status;
1122 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
1123 if (status < 0) {
1124 mlog_errno(status);
1125 goto out;
1128 status = ocfs2_extend_trans(handle, path_num_items(path) +
1129 handle->h_buffer_credits);
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 static 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;
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);
2297 if (ret)
2298 return ret;
2300 if (unlikely(handle->h_buffer_credits < credits))
2301 return ocfs2_extend_trans(handle, credits);
2304 return 0;
2308 * Trap the case where we're inserting into the theoretical range past
2309 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2310 * whose cpos is less than ours into the right leaf.
2312 * It's only necessary to look at the rightmost record of the left
2313 * leaf because the logic that calls us should ensure that the
2314 * theoretical ranges in the path components above the leaves are
2315 * correct.
2317 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2318 u32 insert_cpos)
2320 struct ocfs2_extent_list *left_el;
2321 struct ocfs2_extent_rec *rec;
2322 int next_free;
2324 left_el = path_leaf_el(left_path);
2325 next_free = le16_to_cpu(left_el->l_next_free_rec);
2326 rec = &left_el->l_recs[next_free - 1];
2328 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2329 return 1;
2330 return 0;
2333 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2335 int next_free = le16_to_cpu(el->l_next_free_rec);
2336 unsigned int range;
2337 struct ocfs2_extent_rec *rec;
2339 if (next_free == 0)
2340 return 0;
2342 rec = &el->l_recs[0];
2343 if (ocfs2_is_empty_extent(rec)) {
2344 /* Empty list. */
2345 if (next_free == 1)
2346 return 0;
2347 rec = &el->l_recs[1];
2350 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2351 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2352 return 1;
2353 return 0;
2357 * Rotate all the records in a btree right one record, starting at insert_cpos.
2359 * The path to the rightmost leaf should be passed in.
2361 * The array is assumed to be large enough to hold an entire path (tree depth).
2363 * Upon successful return from this function:
2365 * - The 'right_path' array will contain a path to the leaf block
2366 * whose range contains e_cpos.
2367 * - That leaf block will have a single empty extent in list index 0.
2368 * - In the case that the rotation requires a post-insert update,
2369 * *ret_left_path will contain a valid path which can be passed to
2370 * ocfs2_insert_path().
2372 static int ocfs2_rotate_tree_right(handle_t *handle,
2373 struct ocfs2_extent_tree *et,
2374 enum ocfs2_split_type split,
2375 u32 insert_cpos,
2376 struct ocfs2_path *right_path,
2377 struct ocfs2_path **ret_left_path)
2379 int ret, start, orig_credits = handle->h_buffer_credits;
2380 u32 cpos;
2381 struct ocfs2_path *left_path = NULL;
2382 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2384 *ret_left_path = NULL;
2386 left_path = ocfs2_new_path_from_path(right_path);
2387 if (!left_path) {
2388 ret = -ENOMEM;
2389 mlog_errno(ret);
2390 goto out;
2393 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2394 if (ret) {
2395 mlog_errno(ret);
2396 goto out;
2399 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2402 * What we want to do here is:
2404 * 1) Start with the rightmost path.
2406 * 2) Determine a path to the leaf block directly to the left
2407 * of that leaf.
2409 * 3) Determine the 'subtree root' - the lowest level tree node
2410 * which contains a path to both leaves.
2412 * 4) Rotate the subtree.
2414 * 5) Find the next subtree by considering the left path to be
2415 * the new right path.
2417 * The check at the top of this while loop also accepts
2418 * insert_cpos == cpos because cpos is only a _theoretical_
2419 * value to get us the left path - insert_cpos might very well
2420 * be filling that hole.
2422 * Stop at a cpos of '0' because we either started at the
2423 * leftmost branch (i.e., a tree with one branch and a
2424 * rotation inside of it), or we've gone as far as we can in
2425 * rotating subtrees.
2427 while (cpos && insert_cpos <= cpos) {
2428 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2429 insert_cpos, cpos);
2431 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
2432 if (ret) {
2433 mlog_errno(ret);
2434 goto out;
2437 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2438 path_leaf_bh(right_path),
2439 "Owner %llu: error during insert of %u "
2440 "(left path cpos %u) results in two identical "
2441 "paths ending at %llu\n",
2442 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2443 insert_cpos, cpos,
2444 (unsigned long long)
2445 path_leaf_bh(left_path)->b_blocknr);
2447 if (split == SPLIT_NONE &&
2448 ocfs2_rotate_requires_path_adjustment(left_path,
2449 insert_cpos)) {
2452 * We've rotated the tree as much as we
2453 * should. The rest is up to
2454 * ocfs2_insert_path() to complete, after the
2455 * record insertion. We indicate this
2456 * situation by returning the left path.
2458 * The reason we don't adjust the records here
2459 * before the record insert is that an error
2460 * later might break the rule where a parent
2461 * record e_cpos will reflect the actual
2462 * e_cpos of the 1st nonempty record of the
2463 * child list.
2465 *ret_left_path = left_path;
2466 goto out_ret_path;
2469 start = ocfs2_find_subtree_root(et, left_path, right_path);
2471 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2472 start,
2473 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2474 right_path->p_tree_depth);
2476 ret = ocfs2_extend_rotate_transaction(handle, start,
2477 orig_credits, right_path);
2478 if (ret) {
2479 mlog_errno(ret);
2480 goto out;
2483 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
2484 right_path, start);
2485 if (ret) {
2486 mlog_errno(ret);
2487 goto out;
2490 if (split != SPLIT_NONE &&
2491 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2492 insert_cpos)) {
2494 * A rotate moves the rightmost left leaf
2495 * record over to the leftmost right leaf
2496 * slot. If we're doing an extent split
2497 * instead of a real insert, then we have to
2498 * check that the extent to be split wasn't
2499 * just moved over. If it was, then we can
2500 * exit here, passing left_path back -
2501 * ocfs2_split_extent() is smart enough to
2502 * search both leaves.
2504 *ret_left_path = left_path;
2505 goto out_ret_path;
2509 * There is no need to re-read the next right path
2510 * as we know that it'll be our current left
2511 * path. Optimize by copying values instead.
2513 ocfs2_mv_path(right_path, left_path);
2515 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
2516 if (ret) {
2517 mlog_errno(ret);
2518 goto out;
2522 out:
2523 ocfs2_free_path(left_path);
2525 out_ret_path:
2526 return ret;
2529 static int ocfs2_update_edge_lengths(handle_t *handle,
2530 struct ocfs2_extent_tree *et,
2531 int subtree_index, struct ocfs2_path *path)
2533 int i, idx, ret;
2534 struct ocfs2_extent_rec *rec;
2535 struct ocfs2_extent_list *el;
2536 struct ocfs2_extent_block *eb;
2537 u32 range;
2540 * In normal tree rotation process, we will never touch the
2541 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2542 * doesn't reserve the credits for them either.
2544 * But we do have a special case here which will update the rightmost
2545 * records for all the bh in the path.
2546 * So we have to allocate extra credits and access them.
2548 ret = ocfs2_extend_trans(handle,
2549 handle->h_buffer_credits + subtree_index);
2550 if (ret) {
2551 mlog_errno(ret);
2552 goto out;
2555 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
2556 if (ret) {
2557 mlog_errno(ret);
2558 goto out;
2561 /* Path should always be rightmost. */
2562 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2563 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2565 el = &eb->h_list;
2566 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2567 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2568 rec = &el->l_recs[idx];
2569 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2571 for (i = 0; i < path->p_tree_depth; i++) {
2572 el = path->p_node[i].el;
2573 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2574 rec = &el->l_recs[idx];
2576 rec->e_int_clusters = cpu_to_le32(range);
2577 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2579 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2581 out:
2582 return ret;
2585 static void ocfs2_unlink_path(handle_t *handle,
2586 struct ocfs2_extent_tree *et,
2587 struct ocfs2_cached_dealloc_ctxt *dealloc,
2588 struct ocfs2_path *path, int unlink_start)
2590 int ret, i;
2591 struct ocfs2_extent_block *eb;
2592 struct ocfs2_extent_list *el;
2593 struct buffer_head *bh;
2595 for(i = unlink_start; i < path_num_items(path); i++) {
2596 bh = path->p_node[i].bh;
2598 eb = (struct ocfs2_extent_block *)bh->b_data;
2600 * Not all nodes might have had their final count
2601 * decremented by the caller - handle this here.
2603 el = &eb->h_list;
2604 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2605 mlog(ML_ERROR,
2606 "Inode %llu, attempted to remove extent block "
2607 "%llu with %u records\n",
2608 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2609 (unsigned long long)le64_to_cpu(eb->h_blkno),
2610 le16_to_cpu(el->l_next_free_rec));
2612 ocfs2_journal_dirty(handle, bh);
2613 ocfs2_remove_from_cache(et->et_ci, bh);
2614 continue;
2617 el->l_next_free_rec = 0;
2618 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2620 ocfs2_journal_dirty(handle, bh);
2622 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2623 if (ret)
2624 mlog_errno(ret);
2626 ocfs2_remove_from_cache(et->et_ci, bh);
2630 static void ocfs2_unlink_subtree(handle_t *handle,
2631 struct ocfs2_extent_tree *et,
2632 struct ocfs2_path *left_path,
2633 struct ocfs2_path *right_path,
2634 int subtree_index,
2635 struct ocfs2_cached_dealloc_ctxt *dealloc)
2637 int i;
2638 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2639 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2640 struct ocfs2_extent_list *el;
2641 struct ocfs2_extent_block *eb;
2643 el = path_leaf_el(left_path);
2645 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2647 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2648 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2649 break;
2651 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2653 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2654 le16_add_cpu(&root_el->l_next_free_rec, -1);
2656 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2657 eb->h_next_leaf_blk = 0;
2659 ocfs2_journal_dirty(handle, root_bh);
2660 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2662 ocfs2_unlink_path(handle, et, dealloc, right_path,
2663 subtree_index + 1);
2666 static int ocfs2_rotate_subtree_left(handle_t *handle,
2667 struct ocfs2_extent_tree *et,
2668 struct ocfs2_path *left_path,
2669 struct ocfs2_path *right_path,
2670 int subtree_index,
2671 struct ocfs2_cached_dealloc_ctxt *dealloc,
2672 int *deleted)
2674 int ret, i, del_right_subtree = 0, right_has_empty = 0;
2675 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
2676 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2677 struct ocfs2_extent_block *eb;
2679 *deleted = 0;
2681 right_leaf_el = path_leaf_el(right_path);
2682 left_leaf_el = path_leaf_el(left_path);
2683 root_bh = left_path->p_node[subtree_index].bh;
2684 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2686 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2687 return 0;
2689 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2690 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2692 * It's legal for us to proceed if the right leaf is
2693 * the rightmost one and it has an empty extent. There
2694 * are two cases to handle - whether the leaf will be
2695 * empty after removal or not. If the leaf isn't empty
2696 * then just remove the empty extent up front. The
2697 * next block will handle empty leaves by flagging
2698 * them for unlink.
2700 * Non rightmost leaves will throw -EAGAIN and the
2701 * caller can manually move the subtree and retry.
2704 if (eb->h_next_leaf_blk != 0ULL)
2705 return -EAGAIN;
2707 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2708 ret = ocfs2_journal_access_eb(handle, et->et_ci,
2709 path_leaf_bh(right_path),
2710 OCFS2_JOURNAL_ACCESS_WRITE);
2711 if (ret) {
2712 mlog_errno(ret);
2713 goto out;
2716 ocfs2_remove_empty_extent(right_leaf_el);
2717 } else
2718 right_has_empty = 1;
2721 if (eb->h_next_leaf_blk == 0ULL &&
2722 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2724 * We have to update i_last_eb_blk during the meta
2725 * data delete.
2727 ret = ocfs2_et_root_journal_access(handle, et,
2728 OCFS2_JOURNAL_ACCESS_WRITE);
2729 if (ret) {
2730 mlog_errno(ret);
2731 goto out;
2734 del_right_subtree = 1;
2738 * Getting here with an empty extent in the right path implies
2739 * that it's the rightmost path and will be deleted.
2741 BUG_ON(right_has_empty && !del_right_subtree);
2743 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
2744 subtree_index);
2745 if (ret) {
2746 mlog_errno(ret);
2747 goto out;
2750 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2751 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2752 right_path, i);
2753 if (ret) {
2754 mlog_errno(ret);
2755 goto out;
2758 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2759 left_path, i);
2760 if (ret) {
2761 mlog_errno(ret);
2762 goto out;
2766 if (!right_has_empty) {
2768 * Only do this if we're moving a real
2769 * record. Otherwise, the action is delayed until
2770 * after removal of the right path in which case we
2771 * can do a simple shift to remove the empty extent.
2773 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2774 memset(&right_leaf_el->l_recs[0], 0,
2775 sizeof(struct ocfs2_extent_rec));
2777 if (eb->h_next_leaf_blk == 0ULL) {
2779 * Move recs over to get rid of empty extent, decrease
2780 * next_free. This is allowed to remove the last
2781 * extent in our leaf (setting l_next_free_rec to
2782 * zero) - the delete code below won't care.
2784 ocfs2_remove_empty_extent(right_leaf_el);
2787 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2788 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2790 if (del_right_subtree) {
2791 ocfs2_unlink_subtree(handle, et, left_path, right_path,
2792 subtree_index, dealloc);
2793 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
2794 left_path);
2795 if (ret) {
2796 mlog_errno(ret);
2797 goto out;
2800 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2801 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
2804 * Removal of the extent in the left leaf was skipped
2805 * above so we could delete the right path
2806 * 1st.
2808 if (right_has_empty)
2809 ocfs2_remove_empty_extent(left_leaf_el);
2811 ocfs2_journal_dirty(handle, et_root_bh);
2813 *deleted = 1;
2814 } else
2815 ocfs2_complete_edge_insert(handle, left_path, right_path,
2816 subtree_index);
2818 out:
2819 return ret;
2823 * Given a full path, determine what cpos value would return us a path
2824 * containing the leaf immediately to the right of the current one.
2826 * Will return zero if the path passed in is already the rightmost path.
2828 * This looks similar, but is subtly different to
2829 * ocfs2_find_cpos_for_left_leaf().
2831 int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2832 struct ocfs2_path *path, u32 *cpos)
2834 int i, j, ret = 0;
2835 u64 blkno;
2836 struct ocfs2_extent_list *el;
2838 *cpos = 0;
2840 if (path->p_tree_depth == 0)
2841 return 0;
2843 blkno = path_leaf_bh(path)->b_blocknr;
2845 /* Start at the tree node just above the leaf and work our way up. */
2846 i = path->p_tree_depth - 1;
2847 while (i >= 0) {
2848 int next_free;
2850 el = path->p_node[i].el;
2853 * Find the extent record just after the one in our
2854 * path.
2856 next_free = le16_to_cpu(el->l_next_free_rec);
2857 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2858 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2859 if (j == (next_free - 1)) {
2860 if (i == 0) {
2862 * We've determined that the
2863 * path specified is already
2864 * the rightmost one - return a
2865 * cpos of zero.
2867 goto out;
2870 * The rightmost record points to our
2871 * leaf - we need to travel up the
2872 * tree one level.
2874 goto next_node;
2877 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2878 goto out;
2883 * If we got here, we never found a valid node where
2884 * the tree indicated one should be.
2886 ocfs2_error(sb,
2887 "Invalid extent tree at extent block %llu\n",
2888 (unsigned long long)blkno);
2889 ret = -EROFS;
2890 goto out;
2892 next_node:
2893 blkno = path->p_node[i].bh->b_blocknr;
2894 i--;
2897 out:
2898 return ret;
2901 static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2902 struct ocfs2_extent_tree *et,
2903 struct ocfs2_path *path)
2905 int ret;
2906 struct buffer_head *bh = path_leaf_bh(path);
2907 struct ocfs2_extent_list *el = path_leaf_el(path);
2909 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2910 return 0;
2912 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
2913 path_num_items(path) - 1);
2914 if (ret) {
2915 mlog_errno(ret);
2916 goto out;
2919 ocfs2_remove_empty_extent(el);
2920 ocfs2_journal_dirty(handle, bh);
2922 out:
2923 return ret;
2926 static int __ocfs2_rotate_tree_left(handle_t *handle,
2927 struct ocfs2_extent_tree *et,
2928 int orig_credits,
2929 struct ocfs2_path *path,
2930 struct ocfs2_cached_dealloc_ctxt *dealloc,
2931 struct ocfs2_path **empty_extent_path)
2933 int ret, subtree_root, deleted;
2934 u32 right_cpos;
2935 struct ocfs2_path *left_path = NULL;
2936 struct ocfs2_path *right_path = NULL;
2937 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
2939 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2941 *empty_extent_path = NULL;
2943 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
2944 if (ret) {
2945 mlog_errno(ret);
2946 goto out;
2949 left_path = ocfs2_new_path_from_path(path);
2950 if (!left_path) {
2951 ret = -ENOMEM;
2952 mlog_errno(ret);
2953 goto out;
2956 ocfs2_cp_path(left_path, path);
2958 right_path = ocfs2_new_path_from_path(path);
2959 if (!right_path) {
2960 ret = -ENOMEM;
2961 mlog_errno(ret);
2962 goto out;
2965 while (right_cpos) {
2966 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
2967 if (ret) {
2968 mlog_errno(ret);
2969 goto out;
2972 subtree_root = ocfs2_find_subtree_root(et, left_path,
2973 right_path);
2975 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2976 subtree_root,
2977 (unsigned long long)
2978 right_path->p_node[subtree_root].bh->b_blocknr,
2979 right_path->p_tree_depth);
2981 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2982 orig_credits, left_path);
2983 if (ret) {
2984 mlog_errno(ret);
2985 goto out;
2989 * Caller might still want to make changes to the
2990 * tree root, so re-add it to the journal here.
2992 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
2993 left_path, 0);
2994 if (ret) {
2995 mlog_errno(ret);
2996 goto out;
2999 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
3000 right_path, subtree_root,
3001 dealloc, &deleted);
3002 if (ret == -EAGAIN) {
3004 * The rotation has to temporarily stop due to
3005 * the right subtree having an empty
3006 * extent. Pass it back to the caller for a
3007 * fixup.
3009 *empty_extent_path = right_path;
3010 right_path = NULL;
3011 goto out;
3013 if (ret) {
3014 mlog_errno(ret);
3015 goto out;
3019 * The subtree rotate might have removed records on
3020 * the rightmost edge. If so, then rotation is
3021 * complete.
3023 if (deleted)
3024 break;
3026 ocfs2_mv_path(left_path, right_path);
3028 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
3029 &right_cpos);
3030 if (ret) {
3031 mlog_errno(ret);
3032 goto out;
3036 out:
3037 ocfs2_free_path(right_path);
3038 ocfs2_free_path(left_path);
3040 return ret;
3043 static int ocfs2_remove_rightmost_path(handle_t *handle,
3044 struct ocfs2_extent_tree *et,
3045 struct ocfs2_path *path,
3046 struct ocfs2_cached_dealloc_ctxt *dealloc)
3048 int ret, subtree_index;
3049 u32 cpos;
3050 struct ocfs2_path *left_path = NULL;
3051 struct ocfs2_extent_block *eb;
3052 struct ocfs2_extent_list *el;
3055 ret = ocfs2_et_sanity_check(et);
3056 if (ret)
3057 goto out;
3059 * There's two ways we handle this depending on
3060 * whether path is the only existing one.
3062 ret = ocfs2_extend_rotate_transaction(handle, 0,
3063 handle->h_buffer_credits,
3064 path);
3065 if (ret) {
3066 mlog_errno(ret);
3067 goto out;
3070 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
3071 if (ret) {
3072 mlog_errno(ret);
3073 goto out;
3076 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3077 path, &cpos);
3078 if (ret) {
3079 mlog_errno(ret);
3080 goto out;
3083 if (cpos) {
3085 * We have a path to the left of this one - it needs
3086 * an update too.
3088 left_path = ocfs2_new_path_from_path(path);
3089 if (!left_path) {
3090 ret = -ENOMEM;
3091 mlog_errno(ret);
3092 goto out;
3095 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
3096 if (ret) {
3097 mlog_errno(ret);
3098 goto out;
3101 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
3102 if (ret) {
3103 mlog_errno(ret);
3104 goto out;
3107 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
3109 ocfs2_unlink_subtree(handle, et, left_path, path,
3110 subtree_index, dealloc);
3111 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
3112 left_path);
3113 if (ret) {
3114 mlog_errno(ret);
3115 goto out;
3118 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
3119 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
3120 } else {
3122 * 'path' is also the leftmost path which
3123 * means it must be the only one. This gets
3124 * handled differently because we want to
3125 * revert the root back to having extents
3126 * in-line.
3128 ocfs2_unlink_path(handle, et, dealloc, path, 1);
3130 el = et->et_root_el;
3131 el->l_tree_depth = 0;
3132 el->l_next_free_rec = 0;
3133 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3135 ocfs2_et_set_last_eb_blk(et, 0);
3138 ocfs2_journal_dirty(handle, path_root_bh(path));
3140 out:
3141 ocfs2_free_path(left_path);
3142 return ret;
3146 * Left rotation of btree records.
3148 * In many ways, this is (unsurprisingly) the opposite of right
3149 * rotation. We start at some non-rightmost path containing an empty
3150 * extent in the leaf block. The code works its way to the rightmost
3151 * path by rotating records to the left in every subtree.
3153 * This is used by any code which reduces the number of extent records
3154 * in a leaf. After removal, an empty record should be placed in the
3155 * leftmost list position.
3157 * This won't handle a length update of the rightmost path records if
3158 * the rightmost tree leaf record is removed so the caller is
3159 * responsible for detecting and correcting that.
3161 static int ocfs2_rotate_tree_left(handle_t *handle,
3162 struct ocfs2_extent_tree *et,
3163 struct ocfs2_path *path,
3164 struct ocfs2_cached_dealloc_ctxt *dealloc)
3166 int ret, orig_credits = handle->h_buffer_credits;
3167 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3168 struct ocfs2_extent_block *eb;
3169 struct ocfs2_extent_list *el;
3171 el = path_leaf_el(path);
3172 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3173 return 0;
3175 if (path->p_tree_depth == 0) {
3176 rightmost_no_delete:
3178 * Inline extents. This is trivially handled, so do
3179 * it up front.
3181 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
3182 if (ret)
3183 mlog_errno(ret);
3184 goto out;
3188 * Handle rightmost branch now. There's several cases:
3189 * 1) simple rotation leaving records in there. That's trivial.
3190 * 2) rotation requiring a branch delete - there's no more
3191 * records left. Two cases of this:
3192 * a) There are branches to the left.
3193 * b) This is also the leftmost (the only) branch.
3195 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3196 * 2a) we need the left branch so that we can update it with the unlink
3197 * 2b) we need to bring the root back to inline extents.
3200 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3201 el = &eb->h_list;
3202 if (eb->h_next_leaf_blk == 0) {
3204 * This gets a bit tricky if we're going to delete the
3205 * rightmost path. Get the other cases out of the way
3206 * 1st.
3208 if (le16_to_cpu(el->l_next_free_rec) > 1)
3209 goto rightmost_no_delete;
3211 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3212 ret = -EIO;
3213 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3214 "Owner %llu has empty extent block at %llu",
3215 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
3216 (unsigned long long)le64_to_cpu(eb->h_blkno));
3217 goto out;
3221 * XXX: The caller can not trust "path" any more after
3222 * this as it will have been deleted. What do we do?
3224 * In theory the rotate-for-merge code will never get
3225 * here because it'll always ask for a rotate in a
3226 * nonempty list.
3229 ret = ocfs2_remove_rightmost_path(handle, et, path,
3230 dealloc);
3231 if (ret)
3232 mlog_errno(ret);
3233 goto out;
3237 * Now we can loop, remembering the path we get from -EAGAIN
3238 * and restarting from there.
3240 try_rotate:
3241 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3242 dealloc, &restart_path);
3243 if (ret && ret != -EAGAIN) {
3244 mlog_errno(ret);
3245 goto out;
3248 while (ret == -EAGAIN) {
3249 tmp_path = restart_path;
3250 restart_path = NULL;
3252 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
3253 tmp_path, dealloc,
3254 &restart_path);
3255 if (ret && ret != -EAGAIN) {
3256 mlog_errno(ret);
3257 goto out;
3260 ocfs2_free_path(tmp_path);
3261 tmp_path = NULL;
3263 if (ret == 0)
3264 goto try_rotate;
3267 out:
3268 ocfs2_free_path(tmp_path);
3269 ocfs2_free_path(restart_path);
3270 return ret;
3273 static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3274 int index)
3276 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3277 unsigned int size;
3279 if (rec->e_leaf_clusters == 0) {
3281 * We consumed all of the merged-from record. An empty
3282 * extent cannot exist anywhere but the 1st array
3283 * position, so move things over if the merged-from
3284 * record doesn't occupy that position.
3286 * This creates a new empty extent so the caller
3287 * should be smart enough to have removed any existing
3288 * ones.
3290 if (index > 0) {
3291 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3292 size = index * sizeof(struct ocfs2_extent_rec);
3293 memmove(&el->l_recs[1], &el->l_recs[0], size);
3297 * Always memset - the caller doesn't check whether it
3298 * created an empty extent, so there could be junk in
3299 * the other fields.
3301 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3305 static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
3306 struct ocfs2_path *left_path,
3307 struct ocfs2_path **ret_right_path)
3309 int ret;
3310 u32 right_cpos;
3311 struct ocfs2_path *right_path = NULL;
3312 struct ocfs2_extent_list *left_el;
3314 *ret_right_path = NULL;
3316 /* This function shouldn't be called for non-trees. */
3317 BUG_ON(left_path->p_tree_depth == 0);
3319 left_el = path_leaf_el(left_path);
3320 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3322 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3323 left_path, &right_cpos);
3324 if (ret) {
3325 mlog_errno(ret);
3326 goto out;
3329 /* This function shouldn't be called for the rightmost leaf. */
3330 BUG_ON(right_cpos == 0);
3332 right_path = ocfs2_new_path_from_path(left_path);
3333 if (!right_path) {
3334 ret = -ENOMEM;
3335 mlog_errno(ret);
3336 goto out;
3339 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
3340 if (ret) {
3341 mlog_errno(ret);
3342 goto out;
3345 *ret_right_path = right_path;
3346 out:
3347 if (ret)
3348 ocfs2_free_path(right_path);
3349 return ret;
3353 * Remove split_rec clusters from the record at index and merge them
3354 * onto the beginning of the record "next" to it.
3355 * For index < l_count - 1, the next means the extent rec at index + 1.
3356 * For index == l_count - 1, the "next" means the 1st extent rec of the
3357 * next extent block.
3359 static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
3360 handle_t *handle,
3361 struct ocfs2_extent_tree *et,
3362 struct ocfs2_extent_rec *split_rec,
3363 int index)
3365 int ret, next_free, i;
3366 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3367 struct ocfs2_extent_rec *left_rec;
3368 struct ocfs2_extent_rec *right_rec;
3369 struct ocfs2_extent_list *right_el;
3370 struct ocfs2_path *right_path = NULL;
3371 int subtree_index = 0;
3372 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3373 struct buffer_head *bh = path_leaf_bh(left_path);
3374 struct buffer_head *root_bh = NULL;
3376 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
3377 left_rec = &el->l_recs[index];
3379 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
3380 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3381 /* we meet with a cross extent block merge. */
3382 ret = ocfs2_get_right_path(et, left_path, &right_path);
3383 if (ret) {
3384 mlog_errno(ret);
3385 goto out;
3388 right_el = path_leaf_el(right_path);
3389 next_free = le16_to_cpu(right_el->l_next_free_rec);
3390 BUG_ON(next_free <= 0);
3391 right_rec = &right_el->l_recs[0];
3392 if (ocfs2_is_empty_extent(right_rec)) {
3393 BUG_ON(next_free <= 1);
3394 right_rec = &right_el->l_recs[1];
3397 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3398 le16_to_cpu(left_rec->e_leaf_clusters) !=
3399 le32_to_cpu(right_rec->e_cpos));
3401 subtree_index = ocfs2_find_subtree_root(et, left_path,
3402 right_path);
3404 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3405 handle->h_buffer_credits,
3406 right_path);
3407 if (ret) {
3408 mlog_errno(ret);
3409 goto out;
3412 root_bh = left_path->p_node[subtree_index].bh;
3413 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3415 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3416 subtree_index);
3417 if (ret) {
3418 mlog_errno(ret);
3419 goto out;
3422 for (i = subtree_index + 1;
3423 i < path_num_items(right_path); i++) {
3424 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3425 right_path, i);
3426 if (ret) {
3427 mlog_errno(ret);
3428 goto out;
3431 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3432 left_path, i);
3433 if (ret) {
3434 mlog_errno(ret);
3435 goto out;
3439 } else {
3440 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3441 right_rec = &el->l_recs[index + 1];
3444 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
3445 path_num_items(left_path) - 1);
3446 if (ret) {
3447 mlog_errno(ret);
3448 goto out;
3451 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3453 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3454 le64_add_cpu(&right_rec->e_blkno,
3455 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3456 split_clusters));
3457 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3459 ocfs2_cleanup_merge(el, index);
3461 ocfs2_journal_dirty(handle, bh);
3462 if (right_path) {
3463 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3464 ocfs2_complete_edge_insert(handle, left_path, right_path,
3465 subtree_index);
3467 out:
3468 if (right_path)
3469 ocfs2_free_path(right_path);
3470 return ret;
3473 static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
3474 struct ocfs2_path *right_path,
3475 struct ocfs2_path **ret_left_path)
3477 int ret;
3478 u32 left_cpos;
3479 struct ocfs2_path *left_path = NULL;
3481 *ret_left_path = NULL;
3483 /* This function shouldn't be called for non-trees. */
3484 BUG_ON(right_path->p_tree_depth == 0);
3486 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3487 right_path, &left_cpos);
3488 if (ret) {
3489 mlog_errno(ret);
3490 goto out;
3493 /* This function shouldn't be called for the leftmost leaf. */
3494 BUG_ON(left_cpos == 0);
3496 left_path = ocfs2_new_path_from_path(right_path);
3497 if (!left_path) {
3498 ret = -ENOMEM;
3499 mlog_errno(ret);
3500 goto out;
3503 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
3504 if (ret) {
3505 mlog_errno(ret);
3506 goto out;
3509 *ret_left_path = left_path;
3510 out:
3511 if (ret)
3512 ocfs2_free_path(left_path);
3513 return ret;
3517 * Remove split_rec clusters from the record at index and merge them
3518 * onto the tail of the record "before" it.
3519 * For index > 0, the "before" means the extent rec at index - 1.
3521 * For index == 0, the "before" means the last record of the previous
3522 * extent block. And there is also a situation that we may need to
3523 * remove the rightmost leaf extent block in the right_path and change
3524 * the right path to indicate the new rightmost path.
3526 static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
3527 handle_t *handle,
3528 struct ocfs2_extent_tree *et,
3529 struct ocfs2_extent_rec *split_rec,
3530 struct ocfs2_cached_dealloc_ctxt *dealloc,
3531 int index)
3533 int ret, i, subtree_index = 0, has_empty_extent = 0;
3534 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3535 struct ocfs2_extent_rec *left_rec;
3536 struct ocfs2_extent_rec *right_rec;
3537 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3538 struct buffer_head *bh = path_leaf_bh(right_path);
3539 struct buffer_head *root_bh = NULL;
3540 struct ocfs2_path *left_path = NULL;
3541 struct ocfs2_extent_list *left_el;
3543 BUG_ON(index < 0);
3545 right_rec = &el->l_recs[index];
3546 if (index == 0) {
3547 /* we meet with a cross extent block merge. */
3548 ret = ocfs2_get_left_path(et, right_path, &left_path);
3549 if (ret) {
3550 mlog_errno(ret);
3551 goto out;
3554 left_el = path_leaf_el(left_path);
3555 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3556 le16_to_cpu(left_el->l_count));
3558 left_rec = &left_el->l_recs[
3559 le16_to_cpu(left_el->l_next_free_rec) - 1];
3560 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3561 le16_to_cpu(left_rec->e_leaf_clusters) !=
3562 le32_to_cpu(split_rec->e_cpos));
3564 subtree_index = ocfs2_find_subtree_root(et, left_path,
3565 right_path);
3567 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3568 handle->h_buffer_credits,
3569 left_path);
3570 if (ret) {
3571 mlog_errno(ret);
3572 goto out;
3575 root_bh = left_path->p_node[subtree_index].bh;
3576 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3578 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3579 subtree_index);
3580 if (ret) {
3581 mlog_errno(ret);
3582 goto out;
3585 for (i = subtree_index + 1;
3586 i < path_num_items(right_path); i++) {
3587 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3588 right_path, i);
3589 if (ret) {
3590 mlog_errno(ret);
3591 goto out;
3594 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
3595 left_path, i);
3596 if (ret) {
3597 mlog_errno(ret);
3598 goto out;
3601 } else {
3602 left_rec = &el->l_recs[index - 1];
3603 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3604 has_empty_extent = 1;
3607 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
3608 path_num_items(right_path) - 1);
3609 if (ret) {
3610 mlog_errno(ret);
3611 goto out;
3614 if (has_empty_extent && index == 1) {
3616 * The easy case - we can just plop the record right in.
3618 *left_rec = *split_rec;
3620 has_empty_extent = 0;
3621 } else
3622 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
3624 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3625 le64_add_cpu(&right_rec->e_blkno,
3626 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3627 split_clusters));
3628 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3630 ocfs2_cleanup_merge(el, index);
3632 ocfs2_journal_dirty(handle, bh);
3633 if (left_path) {
3634 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3637 * In the situation that the right_rec is empty and the extent
3638 * block is empty also, ocfs2_complete_edge_insert can't handle
3639 * it and we need to delete the right extent block.
3641 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3642 le16_to_cpu(el->l_next_free_rec) == 1) {
3644 ret = ocfs2_remove_rightmost_path(handle, et,
3645 right_path,
3646 dealloc);
3647 if (ret) {
3648 mlog_errno(ret);
3649 goto out;
3652 /* Now the rightmost extent block has been deleted.
3653 * So we use the new rightmost path.
3655 ocfs2_mv_path(right_path, left_path);
3656 left_path = NULL;
3657 } else
3658 ocfs2_complete_edge_insert(handle, left_path,
3659 right_path, subtree_index);
3661 out:
3662 if (left_path)
3663 ocfs2_free_path(left_path);
3664 return ret;
3667 static int ocfs2_try_to_merge_extent(handle_t *handle,
3668 struct ocfs2_extent_tree *et,
3669 struct ocfs2_path *path,
3670 int split_index,
3671 struct ocfs2_extent_rec *split_rec,
3672 struct ocfs2_cached_dealloc_ctxt *dealloc,
3673 struct ocfs2_merge_ctxt *ctxt)
3675 int ret = 0;
3676 struct ocfs2_extent_list *el = path_leaf_el(path);
3677 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3679 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3681 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3683 * The merge code will need to create an empty
3684 * extent to take the place of the newly
3685 * emptied slot. Remove any pre-existing empty
3686 * extents - having more than one in a leaf is
3687 * illegal.
3689 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3690 if (ret) {
3691 mlog_errno(ret);
3692 goto out;
3694 split_index--;
3695 rec = &el->l_recs[split_index];
3698 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3700 * Left-right contig implies this.
3702 BUG_ON(!ctxt->c_split_covers_rec);
3705 * Since the leftright insert always covers the entire
3706 * extent, this call will delete the insert record
3707 * entirely, resulting in an empty extent record added to
3708 * the extent block.
3710 * Since the adding of an empty extent shifts
3711 * everything back to the right, there's no need to
3712 * update split_index here.
3714 * When the split_index is zero, we need to merge it to the
3715 * prevoius extent block. It is more efficient and easier
3716 * if we do merge_right first and merge_left later.
3718 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
3719 split_index);
3720 if (ret) {
3721 mlog_errno(ret);
3722 goto out;
3726 * We can only get this from logic error above.
3728 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3730 /* The merge left us with an empty extent, remove it. */
3731 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3732 if (ret) {
3733 mlog_errno(ret);
3734 goto out;
3737 rec = &el->l_recs[split_index];
3740 * Note that we don't pass split_rec here on purpose -
3741 * we've merged it into the rec already.
3743 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3744 dealloc, split_index);
3746 if (ret) {
3747 mlog_errno(ret);
3748 goto out;
3751 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
3753 * Error from this last rotate is not critical, so
3754 * print but don't bubble it up.
3756 if (ret)
3757 mlog_errno(ret);
3758 ret = 0;
3759 } else {
3761 * Merge a record to the left or right.
3763 * 'contig_type' is relative to the existing record,
3764 * so for example, if we're "right contig", it's to
3765 * the record on the left (hence the left merge).
3767 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3768 ret = ocfs2_merge_rec_left(path, handle, et,
3769 split_rec, dealloc,
3770 split_index);
3771 if (ret) {
3772 mlog_errno(ret);
3773 goto out;
3775 } else {
3776 ret = ocfs2_merge_rec_right(path, handle,
3777 et, split_rec,
3778 split_index);
3779 if (ret) {
3780 mlog_errno(ret);
3781 goto out;
3785 if (ctxt->c_split_covers_rec) {
3787 * The merge may have left an empty extent in
3788 * our leaf. Try to rotate it away.
3790 ret = ocfs2_rotate_tree_left(handle, et, path,
3791 dealloc);
3792 if (ret)
3793 mlog_errno(ret);
3794 ret = 0;
3798 out:
3799 return ret;
3802 static void ocfs2_subtract_from_rec(struct super_block *sb,
3803 enum ocfs2_split_type split,
3804 struct ocfs2_extent_rec *rec,
3805 struct ocfs2_extent_rec *split_rec)
3807 u64 len_blocks;
3809 len_blocks = ocfs2_clusters_to_blocks(sb,
3810 le16_to_cpu(split_rec->e_leaf_clusters));
3812 if (split == SPLIT_LEFT) {
3814 * Region is on the left edge of the existing
3815 * record.
3817 le32_add_cpu(&rec->e_cpos,
3818 le16_to_cpu(split_rec->e_leaf_clusters));
3819 le64_add_cpu(&rec->e_blkno, len_blocks);
3820 le16_add_cpu(&rec->e_leaf_clusters,
3821 -le16_to_cpu(split_rec->e_leaf_clusters));
3822 } else {
3824 * Region is on the right edge of the existing
3825 * record.
3827 le16_add_cpu(&rec->e_leaf_clusters,
3828 -le16_to_cpu(split_rec->e_leaf_clusters));
3833 * Do the final bits of extent record insertion at the target leaf
3834 * list. If this leaf is part of an allocation tree, it is assumed
3835 * that the tree above has been prepared.
3837 static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3838 struct ocfs2_extent_rec *insert_rec,
3839 struct ocfs2_extent_list *el,
3840 struct ocfs2_insert_type *insert)
3842 int i = insert->ins_contig_index;
3843 unsigned int range;
3844 struct ocfs2_extent_rec *rec;
3846 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
3848 if (insert->ins_split != SPLIT_NONE) {
3849 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3850 BUG_ON(i == -1);
3851 rec = &el->l_recs[i];
3852 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3853 insert->ins_split, rec,
3854 insert_rec);
3855 goto rotate;
3859 * Contiguous insert - either left or right.
3861 if (insert->ins_contig != CONTIG_NONE) {
3862 rec = &el->l_recs[i];
3863 if (insert->ins_contig == CONTIG_LEFT) {
3864 rec->e_blkno = insert_rec->e_blkno;
3865 rec->e_cpos = insert_rec->e_cpos;
3867 le16_add_cpu(&rec->e_leaf_clusters,
3868 le16_to_cpu(insert_rec->e_leaf_clusters));
3869 return;
3873 * Handle insert into an empty leaf.
3875 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3876 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3877 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3878 el->l_recs[0] = *insert_rec;
3879 el->l_next_free_rec = cpu_to_le16(1);
3880 return;
3884 * Appending insert.
3886 if (insert->ins_appending == APPEND_TAIL) {
3887 i = le16_to_cpu(el->l_next_free_rec) - 1;
3888 rec = &el->l_recs[i];
3889 range = le32_to_cpu(rec->e_cpos)
3890 + le16_to_cpu(rec->e_leaf_clusters);
3891 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3893 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3894 le16_to_cpu(el->l_count),
3895 "owner %llu, depth %u, count %u, next free %u, "
3896 "rec.cpos %u, rec.clusters %u, "
3897 "insert.cpos %u, insert.clusters %u\n",
3898 ocfs2_metadata_cache_owner(et->et_ci),
3899 le16_to_cpu(el->l_tree_depth),
3900 le16_to_cpu(el->l_count),
3901 le16_to_cpu(el->l_next_free_rec),
3902 le32_to_cpu(el->l_recs[i].e_cpos),
3903 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
3904 le32_to_cpu(insert_rec->e_cpos),
3905 le16_to_cpu(insert_rec->e_leaf_clusters));
3906 i++;
3907 el->l_recs[i] = *insert_rec;
3908 le16_add_cpu(&el->l_next_free_rec, 1);
3909 return;
3912 rotate:
3914 * Ok, we have to rotate.
3916 * At this point, it is safe to assume that inserting into an
3917 * empty leaf and appending to a leaf have both been handled
3918 * above.
3920 * This leaf needs to have space, either by the empty 1st
3921 * extent record, or by virtue of an l_next_rec < l_count.
3923 ocfs2_rotate_leaf(el, insert_rec);
3926 static void ocfs2_adjust_rightmost_records(handle_t *handle,
3927 struct ocfs2_extent_tree *et,
3928 struct ocfs2_path *path,
3929 struct ocfs2_extent_rec *insert_rec)
3931 int ret, i, next_free;
3932 struct buffer_head *bh;
3933 struct ocfs2_extent_list *el;
3934 struct ocfs2_extent_rec *rec;
3937 * Update everything except the leaf block.
3939 for (i = 0; i < path->p_tree_depth; i++) {
3940 bh = path->p_node[i].bh;
3941 el = path->p_node[i].el;
3943 next_free = le16_to_cpu(el->l_next_free_rec);
3944 if (next_free == 0) {
3945 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3946 "Owner %llu has a bad extent list",
3947 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
3948 ret = -EIO;
3949 return;
3952 rec = &el->l_recs[next_free - 1];
3954 rec->e_int_clusters = insert_rec->e_cpos;
3955 le32_add_cpu(&rec->e_int_clusters,
3956 le16_to_cpu(insert_rec->e_leaf_clusters));
3957 le32_add_cpu(&rec->e_int_clusters,
3958 -le32_to_cpu(rec->e_cpos));
3960 ocfs2_journal_dirty(handle, bh);
3964 static int ocfs2_append_rec_to_path(handle_t *handle,
3965 struct ocfs2_extent_tree *et,
3966 struct ocfs2_extent_rec *insert_rec,
3967 struct ocfs2_path *right_path,
3968 struct ocfs2_path **ret_left_path)
3970 int ret, next_free;
3971 struct ocfs2_extent_list *el;
3972 struct ocfs2_path *left_path = NULL;
3974 *ret_left_path = NULL;
3977 * This shouldn't happen for non-trees. The extent rec cluster
3978 * count manipulation below only works for interior nodes.
3980 BUG_ON(right_path->p_tree_depth == 0);
3983 * If our appending insert is at the leftmost edge of a leaf,
3984 * then we might need to update the rightmost records of the
3985 * neighboring path.
3987 el = path_leaf_el(right_path);
3988 next_free = le16_to_cpu(el->l_next_free_rec);
3989 if (next_free == 0 ||
3990 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3991 u32 left_cpos;
3993 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3994 right_path, &left_cpos);
3995 if (ret) {
3996 mlog_errno(ret);
3997 goto out;
4000 mlog(0, "Append may need a left path update. cpos: %u, "
4001 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
4002 left_cpos);
4005 * No need to worry if the append is already in the
4006 * leftmost leaf.
4008 if (left_cpos) {
4009 left_path = ocfs2_new_path_from_path(right_path);
4010 if (!left_path) {
4011 ret = -ENOMEM;
4012 mlog_errno(ret);
4013 goto out;
4016 ret = ocfs2_find_path(et->et_ci, left_path,
4017 left_cpos);
4018 if (ret) {
4019 mlog_errno(ret);
4020 goto out;
4024 * ocfs2_insert_path() will pass the left_path to the
4025 * journal for us.
4030 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4031 if (ret) {
4032 mlog_errno(ret);
4033 goto out;
4036 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
4038 *ret_left_path = left_path;
4039 ret = 0;
4040 out:
4041 if (ret != 0)
4042 ocfs2_free_path(left_path);
4044 return ret;
4047 static void ocfs2_split_record(struct ocfs2_extent_tree *et,
4048 struct ocfs2_path *left_path,
4049 struct ocfs2_path *right_path,
4050 struct ocfs2_extent_rec *split_rec,
4051 enum ocfs2_split_type split)
4053 int index;
4054 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4055 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4056 struct ocfs2_extent_rec *rec, *tmprec;
4058 right_el = path_leaf_el(right_path);
4059 if (left_path)
4060 left_el = path_leaf_el(left_path);
4062 el = right_el;
4063 insert_el = right_el;
4064 index = ocfs2_search_extent_list(el, cpos);
4065 if (index != -1) {
4066 if (index == 0 && left_path) {
4067 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4070 * This typically means that the record
4071 * started in the left path but moved to the
4072 * right as a result of rotation. We either
4073 * move the existing record to the left, or we
4074 * do the later insert there.
4076 * In this case, the left path should always
4077 * exist as the rotate code will have passed
4078 * it back for a post-insert update.
4081 if (split == SPLIT_LEFT) {
4083 * It's a left split. Since we know
4084 * that the rotate code gave us an
4085 * empty extent in the left path, we
4086 * can just do the insert there.
4088 insert_el = left_el;
4089 } else {
4091 * Right split - we have to move the
4092 * existing record over to the left
4093 * leaf. The insert will be into the
4094 * newly created empty extent in the
4095 * right leaf.
4097 tmprec = &right_el->l_recs[index];
4098 ocfs2_rotate_leaf(left_el, tmprec);
4099 el = left_el;
4101 memset(tmprec, 0, sizeof(*tmprec));
4102 index = ocfs2_search_extent_list(left_el, cpos);
4103 BUG_ON(index == -1);
4106 } else {
4107 BUG_ON(!left_path);
4108 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4110 * Left path is easy - we can just allow the insert to
4111 * happen.
4113 el = left_el;
4114 insert_el = left_el;
4115 index = ocfs2_search_extent_list(el, cpos);
4116 BUG_ON(index == -1);
4119 rec = &el->l_recs[index];
4120 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4121 split, rec, split_rec);
4122 ocfs2_rotate_leaf(insert_el, split_rec);
4126 * This function only does inserts on an allocation b-tree. For tree
4127 * depth = 0, ocfs2_insert_at_leaf() is called directly.
4129 * right_path is the path we want to do the actual insert
4130 * in. left_path should only be passed in if we need to update that
4131 * portion of the tree after an edge insert.
4133 static int ocfs2_insert_path(handle_t *handle,
4134 struct ocfs2_extent_tree *et,
4135 struct ocfs2_path *left_path,
4136 struct ocfs2_path *right_path,
4137 struct ocfs2_extent_rec *insert_rec,
4138 struct ocfs2_insert_type *insert)
4140 int ret, subtree_index;
4141 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
4143 if (left_path) {
4144 int credits = handle->h_buffer_credits;
4147 * There's a chance that left_path got passed back to
4148 * us without being accounted for in the
4149 * journal. Extend our transaction here to be sure we
4150 * can change those blocks.
4152 credits += left_path->p_tree_depth;
4154 ret = ocfs2_extend_trans(handle, credits);
4155 if (ret < 0) {
4156 mlog_errno(ret);
4157 goto out;
4160 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
4161 if (ret < 0) {
4162 mlog_errno(ret);
4163 goto out;
4168 * Pass both paths to the journal. The majority of inserts
4169 * will be touching all components anyway.
4171 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
4172 if (ret < 0) {
4173 mlog_errno(ret);
4174 goto out;
4177 if (insert->ins_split != SPLIT_NONE) {
4179 * We could call ocfs2_insert_at_leaf() for some types
4180 * of splits, but it's easier to just let one separate
4181 * function sort it all out.
4183 ocfs2_split_record(et, left_path, right_path,
4184 insert_rec, insert->ins_split);
4187 * Split might have modified either leaf and we don't
4188 * have a guarantee that the later edge insert will
4189 * dirty this for us.
4191 if (left_path)
4192 ocfs2_journal_dirty(handle,
4193 path_leaf_bh(left_path));
4194 } else
4195 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4196 insert);
4198 ocfs2_journal_dirty(handle, leaf_bh);
4200 if (left_path) {
4202 * The rotate code has indicated that we need to fix
4203 * up portions of the tree after the insert.
4205 * XXX: Should we extend the transaction here?
4207 subtree_index = ocfs2_find_subtree_root(et, left_path,
4208 right_path);
4209 ocfs2_complete_edge_insert(handle, left_path, right_path,
4210 subtree_index);
4213 ret = 0;
4214 out:
4215 return ret;
4218 static int ocfs2_do_insert_extent(handle_t *handle,
4219 struct ocfs2_extent_tree *et,
4220 struct ocfs2_extent_rec *insert_rec,
4221 struct ocfs2_insert_type *type)
4223 int ret, rotate = 0;
4224 u32 cpos;
4225 struct ocfs2_path *right_path = NULL;
4226 struct ocfs2_path *left_path = NULL;
4227 struct ocfs2_extent_list *el;
4229 el = et->et_root_el;
4231 ret = ocfs2_et_root_journal_access(handle, et,
4232 OCFS2_JOURNAL_ACCESS_WRITE);
4233 if (ret) {
4234 mlog_errno(ret);
4235 goto out;
4238 if (le16_to_cpu(el->l_tree_depth) == 0) {
4239 ocfs2_insert_at_leaf(et, insert_rec, el, type);
4240 goto out_update_clusters;
4243 right_path = ocfs2_new_path_from_et(et);
4244 if (!right_path) {
4245 ret = -ENOMEM;
4246 mlog_errno(ret);
4247 goto out;
4251 * Determine the path to start with. Rotations need the
4252 * rightmost path, everything else can go directly to the
4253 * target leaf.
4255 cpos = le32_to_cpu(insert_rec->e_cpos);
4256 if (type->ins_appending == APPEND_NONE &&
4257 type->ins_contig == CONTIG_NONE) {
4258 rotate = 1;
4259 cpos = UINT_MAX;
4262 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
4263 if (ret) {
4264 mlog_errno(ret);
4265 goto out;
4269 * Rotations and appends need special treatment - they modify
4270 * parts of the tree's above them.
4272 * Both might pass back a path immediate to the left of the
4273 * one being inserted to. This will be cause
4274 * ocfs2_insert_path() to modify the rightmost records of
4275 * left_path to account for an edge insert.
4277 * XXX: When modifying this code, keep in mind that an insert
4278 * can wind up skipping both of these two special cases...
4280 if (rotate) {
4281 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
4282 le32_to_cpu(insert_rec->e_cpos),
4283 right_path, &left_path);
4284 if (ret) {
4285 mlog_errno(ret);
4286 goto out;
4290 * ocfs2_rotate_tree_right() might have extended the
4291 * transaction without re-journaling our tree root.
4293 ret = ocfs2_et_root_journal_access(handle, et,
4294 OCFS2_JOURNAL_ACCESS_WRITE);
4295 if (ret) {
4296 mlog_errno(ret);
4297 goto out;
4299 } else if (type->ins_appending == APPEND_TAIL
4300 && type->ins_contig != CONTIG_LEFT) {
4301 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
4302 right_path, &left_path);
4303 if (ret) {
4304 mlog_errno(ret);
4305 goto out;
4309 ret = ocfs2_insert_path(handle, et, left_path, right_path,
4310 insert_rec, type);
4311 if (ret) {
4312 mlog_errno(ret);
4313 goto out;
4316 out_update_clusters:
4317 if (type->ins_split == SPLIT_NONE)
4318 ocfs2_et_update_clusters(et,
4319 le16_to_cpu(insert_rec->e_leaf_clusters));
4321 ocfs2_journal_dirty(handle, et->et_root_bh);
4323 out:
4324 ocfs2_free_path(left_path);
4325 ocfs2_free_path(right_path);
4327 return ret;
4330 static enum ocfs2_contig_type
4331 ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4332 struct ocfs2_path *path,
4333 struct ocfs2_extent_list *el, int index,
4334 struct ocfs2_extent_rec *split_rec)
4336 int status;
4337 enum ocfs2_contig_type ret = CONTIG_NONE;
4338 u32 left_cpos, right_cpos;
4339 struct ocfs2_extent_rec *rec = NULL;
4340 struct ocfs2_extent_list *new_el;
4341 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4342 struct buffer_head *bh;
4343 struct ocfs2_extent_block *eb;
4344 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
4346 if (index > 0) {
4347 rec = &el->l_recs[index - 1];
4348 } else if (path->p_tree_depth > 0) {
4349 status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
4350 if (status)
4351 goto out;
4353 if (left_cpos != 0) {
4354 left_path = ocfs2_new_path_from_path(path);
4355 if (!left_path)
4356 goto out;
4358 status = ocfs2_find_path(et->et_ci, left_path,
4359 left_cpos);
4360 if (status)
4361 goto out;
4363 new_el = path_leaf_el(left_path);
4365 if (le16_to_cpu(new_el->l_next_free_rec) !=
4366 le16_to_cpu(new_el->l_count)) {
4367 bh = path_leaf_bh(left_path);
4368 eb = (struct ocfs2_extent_block *)bh->b_data;
4369 ocfs2_error(sb,
4370 "Extent block #%llu has an "
4371 "invalid l_next_free_rec of "
4372 "%d. It should have "
4373 "matched the l_count of %d",
4374 (unsigned long long)le64_to_cpu(eb->h_blkno),
4375 le16_to_cpu(new_el->l_next_free_rec),
4376 le16_to_cpu(new_el->l_count));
4377 status = -EINVAL;
4378 goto out;
4380 rec = &new_el->l_recs[
4381 le16_to_cpu(new_el->l_next_free_rec) - 1];
4386 * We're careful to check for an empty extent record here -
4387 * the merge code will know what to do if it sees one.
4389 if (rec) {
4390 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4391 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4392 ret = CONTIG_RIGHT;
4393 } else {
4394 ret = ocfs2_et_extent_contig(et, rec, split_rec);
4398 rec = NULL;
4399 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4400 rec = &el->l_recs[index + 1];
4401 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4402 path->p_tree_depth > 0) {
4403 status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
4404 if (status)
4405 goto out;
4407 if (right_cpos == 0)
4408 goto out;
4410 right_path = ocfs2_new_path_from_path(path);
4411 if (!right_path)
4412 goto out;
4414 status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
4415 if (status)
4416 goto out;
4418 new_el = path_leaf_el(right_path);
4419 rec = &new_el->l_recs[0];
4420 if (ocfs2_is_empty_extent(rec)) {
4421 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4422 bh = path_leaf_bh(right_path);
4423 eb = (struct ocfs2_extent_block *)bh->b_data;
4424 ocfs2_error(sb,
4425 "Extent block #%llu has an "
4426 "invalid l_next_free_rec of %d",
4427 (unsigned long long)le64_to_cpu(eb->h_blkno),
4428 le16_to_cpu(new_el->l_next_free_rec));
4429 status = -EINVAL;
4430 goto out;
4432 rec = &new_el->l_recs[1];
4436 if (rec) {
4437 enum ocfs2_contig_type contig_type;
4439 contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
4441 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4442 ret = CONTIG_LEFTRIGHT;
4443 else if (ret == CONTIG_NONE)
4444 ret = contig_type;
4447 out:
4448 if (left_path)
4449 ocfs2_free_path(left_path);
4450 if (right_path)
4451 ocfs2_free_path(right_path);
4453 return ret;
4456 static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
4457 struct ocfs2_insert_type *insert,
4458 struct ocfs2_extent_list *el,
4459 struct ocfs2_extent_rec *insert_rec)
4461 int i;
4462 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4464 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4466 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4467 contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4468 insert_rec);
4469 if (contig_type != CONTIG_NONE) {
4470 insert->ins_contig_index = i;
4471 break;
4474 insert->ins_contig = contig_type;
4476 if (insert->ins_contig != CONTIG_NONE) {
4477 struct ocfs2_extent_rec *rec =
4478 &el->l_recs[insert->ins_contig_index];
4479 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4480 le16_to_cpu(insert_rec->e_leaf_clusters);
4483 * Caller might want us to limit the size of extents, don't
4484 * calculate contiguousness if we might exceed that limit.
4486 if (et->et_max_leaf_clusters &&
4487 (len > et->et_max_leaf_clusters))
4488 insert->ins_contig = CONTIG_NONE;
4493 * This should only be called against the righmost leaf extent list.
4495 * ocfs2_figure_appending_type() will figure out whether we'll have to
4496 * insert at the tail of the rightmost leaf.
4498 * This should also work against the root extent list for tree's with 0
4499 * depth. If we consider the root extent list to be the rightmost leaf node
4500 * then the logic here makes sense.
4502 static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4503 struct ocfs2_extent_list *el,
4504 struct ocfs2_extent_rec *insert_rec)
4506 int i;
4507 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4508 struct ocfs2_extent_rec *rec;
4510 insert->ins_appending = APPEND_NONE;
4512 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4514 if (!el->l_next_free_rec)
4515 goto set_tail_append;
4517 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4518 /* Were all records empty? */
4519 if (le16_to_cpu(el->l_next_free_rec) == 1)
4520 goto set_tail_append;
4523 i = le16_to_cpu(el->l_next_free_rec) - 1;
4524 rec = &el->l_recs[i];
4526 if (cpos >=
4527 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
4528 goto set_tail_append;
4530 return;
4532 set_tail_append:
4533 insert->ins_appending = APPEND_TAIL;
4537 * Helper function called at the begining of an insert.
4539 * This computes a few things that are commonly used in the process of
4540 * inserting into the btree:
4541 * - Whether the new extent is contiguous with an existing one.
4542 * - The current tree depth.
4543 * - Whether the insert is an appending one.
4544 * - The total # of free records in the tree.
4546 * All of the information is stored on the ocfs2_insert_type
4547 * structure.
4549 static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
4550 struct buffer_head **last_eb_bh,
4551 struct ocfs2_extent_rec *insert_rec,
4552 int *free_records,
4553 struct ocfs2_insert_type *insert)
4555 int ret;
4556 struct ocfs2_extent_block *eb;
4557 struct ocfs2_extent_list *el;
4558 struct ocfs2_path *path = NULL;
4559 struct buffer_head *bh = NULL;
4561 insert->ins_split = SPLIT_NONE;
4563 el = et->et_root_el;
4564 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4566 if (el->l_tree_depth) {
4568 * If we have tree depth, we read in the
4569 * rightmost extent block ahead of time as
4570 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4571 * may want it later.
4573 ret = ocfs2_read_extent_block(et->et_ci,
4574 ocfs2_et_get_last_eb_blk(et),
4575 &bh);
4576 if (ret) {
4577 mlog_exit(ret);
4578 goto out;
4580 eb = (struct ocfs2_extent_block *) bh->b_data;
4581 el = &eb->h_list;
4585 * Unless we have a contiguous insert, we'll need to know if
4586 * there is room left in our allocation tree for another
4587 * extent record.
4589 * XXX: This test is simplistic, we can search for empty
4590 * extent records too.
4592 *free_records = le16_to_cpu(el->l_count) -
4593 le16_to_cpu(el->l_next_free_rec);
4595 if (!insert->ins_tree_depth) {
4596 ocfs2_figure_contig_type(et, insert, el, insert_rec);
4597 ocfs2_figure_appending_type(insert, el, insert_rec);
4598 return 0;
4601 path = ocfs2_new_path_from_et(et);
4602 if (!path) {
4603 ret = -ENOMEM;
4604 mlog_errno(ret);
4605 goto out;
4609 * In the case that we're inserting past what the tree
4610 * currently accounts for, ocfs2_find_path() will return for
4611 * us the rightmost tree path. This is accounted for below in
4612 * the appending code.
4614 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
4615 if (ret) {
4616 mlog_errno(ret);
4617 goto out;
4620 el = path_leaf_el(path);
4623 * Now that we have the path, there's two things we want to determine:
4624 * 1) Contiguousness (also set contig_index if this is so)
4626 * 2) Are we doing an append? We can trivially break this up
4627 * into two types of appends: simple record append, or a
4628 * rotate inside the tail leaf.
4630 ocfs2_figure_contig_type(et, insert, el, insert_rec);
4633 * The insert code isn't quite ready to deal with all cases of
4634 * left contiguousness. Specifically, if it's an insert into
4635 * the 1st record in a leaf, it will require the adjustment of
4636 * cluster count on the last record of the path directly to it's
4637 * left. For now, just catch that case and fool the layers
4638 * above us. This works just fine for tree_depth == 0, which
4639 * is why we allow that above.
4641 if (insert->ins_contig == CONTIG_LEFT &&
4642 insert->ins_contig_index == 0)
4643 insert->ins_contig = CONTIG_NONE;
4646 * Ok, so we can simply compare against last_eb to figure out
4647 * whether the path doesn't exist. This will only happen in
4648 * the case that we're doing a tail append, so maybe we can
4649 * take advantage of that information somehow.
4651 if (ocfs2_et_get_last_eb_blk(et) ==
4652 path_leaf_bh(path)->b_blocknr) {
4654 * Ok, ocfs2_find_path() returned us the rightmost
4655 * tree path. This might be an appending insert. There are
4656 * two cases:
4657 * 1) We're doing a true append at the tail:
4658 * -This might even be off the end of the leaf
4659 * 2) We're "appending" by rotating in the tail
4661 ocfs2_figure_appending_type(insert, el, insert_rec);
4664 out:
4665 ocfs2_free_path(path);
4667 if (ret == 0)
4668 *last_eb_bh = bh;
4669 else
4670 brelse(bh);
4671 return ret;
4675 * Insert an extent into a btree.
4677 * The caller needs to update the owning btree's cluster count.
4679 int ocfs2_insert_extent(handle_t *handle,
4680 struct ocfs2_extent_tree *et,
4681 u32 cpos,
4682 u64 start_blk,
4683 u32 new_clusters,
4684 u8 flags,
4685 struct ocfs2_alloc_context *meta_ac)
4687 int status;
4688 int uninitialized_var(free_records);
4689 struct buffer_head *last_eb_bh = NULL;
4690 struct ocfs2_insert_type insert = {0, };
4691 struct ocfs2_extent_rec rec;
4693 mlog(0, "add %u clusters at position %u to owner %llu\n",
4694 new_clusters, cpos,
4695 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
4697 memset(&rec, 0, sizeof(rec));
4698 rec.e_cpos = cpu_to_le32(cpos);
4699 rec.e_blkno = cpu_to_le64(start_blk);
4700 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
4701 rec.e_flags = flags;
4702 status = ocfs2_et_insert_check(et, &rec);
4703 if (status) {
4704 mlog_errno(status);
4705 goto bail;
4708 status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
4709 &free_records, &insert);
4710 if (status < 0) {
4711 mlog_errno(status);
4712 goto bail;
4715 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4716 "Insert.contig_index: %d, Insert.free_records: %d, "
4717 "Insert.tree_depth: %d\n",
4718 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
4719 free_records, insert.ins_tree_depth);
4721 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
4722 status = ocfs2_grow_tree(handle, et,
4723 &insert.ins_tree_depth, &last_eb_bh,
4724 meta_ac);
4725 if (status) {
4726 mlog_errno(status);
4727 goto bail;
4731 /* Finally, we can add clusters. This might rotate the tree for us. */
4732 status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
4733 if (status < 0)
4734 mlog_errno(status);
4735 else
4736 ocfs2_et_extent_map_insert(et, &rec);
4738 bail:
4739 brelse(last_eb_bh);
4741 mlog_exit(status);
4742 return status;
4746 * Allcate and add clusters into the extent b-tree.
4747 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4748 * The extent b-tree's root is specified by et, and
4749 * it is not limited to the file storage. Any extent tree can use this
4750 * function if it implements the proper ocfs2_extent_tree.
4752 int ocfs2_add_clusters_in_btree(handle_t *handle,
4753 struct ocfs2_extent_tree *et,
4754 u32 *logical_offset,
4755 u32 clusters_to_add,
4756 int mark_unwritten,
4757 struct ocfs2_alloc_context *data_ac,
4758 struct ocfs2_alloc_context *meta_ac,
4759 enum ocfs2_alloc_restarted *reason_ret)
4761 int status = 0;
4762 int free_extents;
4763 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4764 u32 bit_off, num_bits;
4765 u64 block;
4766 u8 flags = 0;
4767 struct ocfs2_super *osb =
4768 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
4770 BUG_ON(!clusters_to_add);
4772 if (mark_unwritten)
4773 flags = OCFS2_EXT_UNWRITTEN;
4775 free_extents = ocfs2_num_free_extents(osb, et);
4776 if (free_extents < 0) {
4777 status = free_extents;
4778 mlog_errno(status);
4779 goto leave;
4782 /* there are two cases which could cause us to EAGAIN in the
4783 * we-need-more-metadata case:
4784 * 1) we haven't reserved *any*
4785 * 2) we are so fragmented, we've needed to add metadata too
4786 * many times. */
4787 if (!free_extents && !meta_ac) {
4788 mlog(0, "we haven't reserved any metadata!\n");
4789 status = -EAGAIN;
4790 reason = RESTART_META;
4791 goto leave;
4792 } else if ((!free_extents)
4793 && (ocfs2_alloc_context_bits_left(meta_ac)
4794 < ocfs2_extend_meta_needed(et->et_root_el))) {
4795 mlog(0, "filesystem is really fragmented...\n");
4796 status = -EAGAIN;
4797 reason = RESTART_META;
4798 goto leave;
4801 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4802 clusters_to_add, &bit_off, &num_bits);
4803 if (status < 0) {
4804 if (status != -ENOSPC)
4805 mlog_errno(status);
4806 goto leave;
4809 BUG_ON(num_bits > clusters_to_add);
4811 /* reserve our write early -- insert_extent may update the tree root */
4812 status = ocfs2_et_root_journal_access(handle, et,
4813 OCFS2_JOURNAL_ACCESS_WRITE);
4814 if (status < 0) {
4815 mlog_errno(status);
4816 goto leave;
4819 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4820 mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
4821 num_bits, bit_off,
4822 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
4823 status = ocfs2_insert_extent(handle, et, *logical_offset, block,
4824 num_bits, flags, meta_ac);
4825 if (status < 0) {
4826 mlog_errno(status);
4827 goto leave;
4830 ocfs2_journal_dirty(handle, et->et_root_bh);
4832 clusters_to_add -= num_bits;
4833 *logical_offset += num_bits;
4835 if (clusters_to_add) {
4836 mlog(0, "need to alloc once more, wanted = %u\n",
4837 clusters_to_add);
4838 status = -EAGAIN;
4839 reason = RESTART_TRANS;
4842 leave:
4843 mlog_exit(status);
4844 if (reason_ret)
4845 *reason_ret = reason;
4846 return status;
4849 static void ocfs2_make_right_split_rec(struct super_block *sb,
4850 struct ocfs2_extent_rec *split_rec,
4851 u32 cpos,
4852 struct ocfs2_extent_rec *rec)
4854 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4855 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4857 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4859 split_rec->e_cpos = cpu_to_le32(cpos);
4860 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4862 split_rec->e_blkno = rec->e_blkno;
4863 le64_add_cpu(&split_rec->e_blkno,
4864 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4866 split_rec->e_flags = rec->e_flags;
4869 static int ocfs2_split_and_insert(handle_t *handle,
4870 struct ocfs2_extent_tree *et,
4871 struct ocfs2_path *path,
4872 struct buffer_head **last_eb_bh,
4873 int split_index,
4874 struct ocfs2_extent_rec *orig_split_rec,
4875 struct ocfs2_alloc_context *meta_ac)
4877 int ret = 0, depth;
4878 unsigned int insert_range, rec_range, do_leftright = 0;
4879 struct ocfs2_extent_rec tmprec;
4880 struct ocfs2_extent_list *rightmost_el;
4881 struct ocfs2_extent_rec rec;
4882 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4883 struct ocfs2_insert_type insert;
4884 struct ocfs2_extent_block *eb;
4886 leftright:
4888 * Store a copy of the record on the stack - it might move
4889 * around as the tree is manipulated below.
4891 rec = path_leaf_el(path)->l_recs[split_index];
4893 rightmost_el = et->et_root_el;
4895 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4896 if (depth) {
4897 BUG_ON(!(*last_eb_bh));
4898 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4899 rightmost_el = &eb->h_list;
4902 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4903 le16_to_cpu(rightmost_el->l_count)) {
4904 ret = ocfs2_grow_tree(handle, et,
4905 &depth, last_eb_bh, meta_ac);
4906 if (ret) {
4907 mlog_errno(ret);
4908 goto out;
4912 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4913 insert.ins_appending = APPEND_NONE;
4914 insert.ins_contig = CONTIG_NONE;
4915 insert.ins_tree_depth = depth;
4917 insert_range = le32_to_cpu(split_rec.e_cpos) +
4918 le16_to_cpu(split_rec.e_leaf_clusters);
4919 rec_range = le32_to_cpu(rec.e_cpos) +
4920 le16_to_cpu(rec.e_leaf_clusters);
4922 if (split_rec.e_cpos == rec.e_cpos) {
4923 insert.ins_split = SPLIT_LEFT;
4924 } else if (insert_range == rec_range) {
4925 insert.ins_split = SPLIT_RIGHT;
4926 } else {
4928 * Left/right split. We fake this as a right split
4929 * first and then make a second pass as a left split.
4931 insert.ins_split = SPLIT_RIGHT;
4933 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4934 &tmprec, insert_range, &rec);
4936 split_rec = tmprec;
4938 BUG_ON(do_leftright);
4939 do_leftright = 1;
4942 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
4943 if (ret) {
4944 mlog_errno(ret);
4945 goto out;
4948 if (do_leftright == 1) {
4949 u32 cpos;
4950 struct ocfs2_extent_list *el;
4952 do_leftright++;
4953 split_rec = *orig_split_rec;
4955 ocfs2_reinit_path(path, 1);
4957 cpos = le32_to_cpu(split_rec.e_cpos);
4958 ret = ocfs2_find_path(et->et_ci, path, cpos);
4959 if (ret) {
4960 mlog_errno(ret);
4961 goto out;
4964 el = path_leaf_el(path);
4965 split_index = ocfs2_search_extent_list(el, cpos);
4966 goto leftright;
4968 out:
4970 return ret;
4973 static int ocfs2_replace_extent_rec(handle_t *handle,
4974 struct ocfs2_extent_tree *et,
4975 struct ocfs2_path *path,
4976 struct ocfs2_extent_list *el,
4977 int split_index,
4978 struct ocfs2_extent_rec *split_rec)
4980 int ret;
4982 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
4983 path_num_items(path) - 1);
4984 if (ret) {
4985 mlog_errno(ret);
4986 goto out;
4989 el->l_recs[split_index] = *split_rec;
4991 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4992 out:
4993 return ret;
4997 * Split part or all of the extent record at split_index in the leaf
4998 * pointed to by path. Merge with the contiguous extent record if needed.
5000 * Care is taken to handle contiguousness so as to not grow the tree.
5002 * meta_ac is not strictly necessary - we only truly need it if growth
5003 * of the tree is required. All other cases will degrade into a less
5004 * optimal tree layout.
5006 * last_eb_bh should be the rightmost leaf block for any extent
5007 * btree. Since a split may grow the tree or a merge might shrink it,
5008 * the caller cannot trust the contents of that buffer after this call.
5010 * This code is optimized for readability - several passes might be
5011 * made over certain portions of the tree. All of those blocks will
5012 * have been brought into cache (and pinned via the journal), so the
5013 * extra overhead is not expressed in terms of disk reads.
5015 int ocfs2_split_extent(handle_t *handle,
5016 struct ocfs2_extent_tree *et,
5017 struct ocfs2_path *path,
5018 int split_index,
5019 struct ocfs2_extent_rec *split_rec,
5020 struct ocfs2_alloc_context *meta_ac,
5021 struct ocfs2_cached_dealloc_ctxt *dealloc)
5023 int ret = 0;
5024 struct ocfs2_extent_list *el = path_leaf_el(path);
5025 struct buffer_head *last_eb_bh = NULL;
5026 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5027 struct ocfs2_merge_ctxt ctxt;
5028 struct ocfs2_extent_list *rightmost_el;
5030 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5031 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5032 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5033 ret = -EIO;
5034 mlog_errno(ret);
5035 goto out;
5038 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
5039 split_index,
5040 split_rec);
5043 * The core merge / split code wants to know how much room is
5044 * left in this allocation tree, so we pass the
5045 * rightmost extent list.
5047 if (path->p_tree_depth) {
5048 struct ocfs2_extent_block *eb;
5050 ret = ocfs2_read_extent_block(et->et_ci,
5051 ocfs2_et_get_last_eb_blk(et),
5052 &last_eb_bh);
5053 if (ret) {
5054 mlog_exit(ret);
5055 goto out;
5058 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5059 rightmost_el = &eb->h_list;
5060 } else
5061 rightmost_el = path_root_el(path);
5063 if (rec->e_cpos == split_rec->e_cpos &&
5064 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5065 ctxt.c_split_covers_rec = 1;
5066 else
5067 ctxt.c_split_covers_rec = 0;
5069 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5071 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5072 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5073 ctxt.c_split_covers_rec);
5075 if (ctxt.c_contig_type == CONTIG_NONE) {
5076 if (ctxt.c_split_covers_rec)
5077 ret = ocfs2_replace_extent_rec(handle, et, path, el,
5078 split_index, split_rec);
5079 else
5080 ret = ocfs2_split_and_insert(handle, et, path,
5081 &last_eb_bh, split_index,
5082 split_rec, meta_ac);
5083 if (ret)
5084 mlog_errno(ret);
5085 } else {
5086 ret = ocfs2_try_to_merge_extent(handle, et, path,
5087 split_index, split_rec,
5088 dealloc, &ctxt);
5089 if (ret)
5090 mlog_errno(ret);
5093 out:
5094 brelse(last_eb_bh);
5095 return ret;
5099 * Change the flags of the already-existing extent at cpos for len clusters.
5101 * new_flags: the flags we want to set.
5102 * clear_flags: the flags we want to clear.
5103 * phys: the new physical offset we want this new extent starts from.
5105 * If the existing extent is larger than the request, initiate a
5106 * split. An attempt will be made at merging with adjacent extents.
5108 * The caller is responsible for passing down meta_ac if we'll need it.
5110 int ocfs2_change_extent_flag(handle_t *handle,
5111 struct ocfs2_extent_tree *et,
5112 u32 cpos, u32 len, u32 phys,
5113 struct ocfs2_alloc_context *meta_ac,
5114 struct ocfs2_cached_dealloc_ctxt *dealloc,
5115 int new_flags, int clear_flags)
5117 int ret, index;
5118 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5119 u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
5120 struct ocfs2_extent_rec split_rec;
5121 struct ocfs2_path *left_path = NULL;
5122 struct ocfs2_extent_list *el;
5123 struct ocfs2_extent_rec *rec;
5125 left_path = ocfs2_new_path_from_et(et);
5126 if (!left_path) {
5127 ret = -ENOMEM;
5128 mlog_errno(ret);
5129 goto out;
5132 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
5133 if (ret) {
5134 mlog_errno(ret);
5135 goto out;
5137 el = path_leaf_el(left_path);
5139 index = ocfs2_search_extent_list(el, cpos);
5140 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5141 ocfs2_error(sb,
5142 "Owner %llu has an extent at cpos %u which can no "
5143 "longer be found.\n",
5144 (unsigned long long)
5145 ocfs2_metadata_cache_owner(et->et_ci), cpos);
5146 ret = -EROFS;
5147 goto out;
5150 ret = -EIO;
5151 rec = &el->l_recs[index];
5152 if (new_flags && (rec->e_flags & new_flags)) {
5153 mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
5154 "extent that already had them",
5155 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5156 new_flags);
5157 goto out;
5160 if (clear_flags && !(rec->e_flags & clear_flags)) {
5161 mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
5162 "extent that didn't have them",
5163 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5164 clear_flags);
5165 goto out;
5168 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5169 split_rec.e_cpos = cpu_to_le32(cpos);
5170 split_rec.e_leaf_clusters = cpu_to_le16(len);
5171 split_rec.e_blkno = cpu_to_le64(start_blkno);
5172 split_rec.e_flags = rec->e_flags;
5173 if (new_flags)
5174 split_rec.e_flags |= new_flags;
5175 if (clear_flags)
5176 split_rec.e_flags &= ~clear_flags;
5178 ret = ocfs2_split_extent(handle, et, left_path,
5179 index, &split_rec, meta_ac,
5180 dealloc);
5181 if (ret)
5182 mlog_errno(ret);
5184 out:
5185 ocfs2_free_path(left_path);
5186 return ret;
5191 * Mark the already-existing extent at cpos as written for len clusters.
5192 * This removes the unwritten extent flag.
5194 * If the existing extent is larger than the request, initiate a
5195 * split. An attempt will be made at merging with adjacent extents.
5197 * The caller is responsible for passing down meta_ac if we'll need it.
5199 int ocfs2_mark_extent_written(struct inode *inode,
5200 struct ocfs2_extent_tree *et,
5201 handle_t *handle, u32 cpos, u32 len, u32 phys,
5202 struct ocfs2_alloc_context *meta_ac,
5203 struct ocfs2_cached_dealloc_ctxt *dealloc)
5205 int ret;
5207 mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
5208 inode->i_ino, cpos, len, phys);
5210 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5211 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5212 "that are being written to, but the feature bit "
5213 "is not set in the super block.",
5214 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5215 ret = -EROFS;
5216 goto out;
5220 * XXX: This should be fixed up so that we just re-insert the
5221 * next extent records.
5223 ocfs2_et_extent_map_truncate(et, 0);
5225 ret = ocfs2_change_extent_flag(handle, et, cpos,
5226 len, phys, meta_ac, dealloc,
5227 0, OCFS2_EXT_UNWRITTEN);
5228 if (ret)
5229 mlog_errno(ret);
5231 out:
5232 return ret;
5235 static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5236 struct ocfs2_path *path,
5237 int index, u32 new_range,
5238 struct ocfs2_alloc_context *meta_ac)
5240 int ret, depth, credits = handle->h_buffer_credits;
5241 struct buffer_head *last_eb_bh = NULL;
5242 struct ocfs2_extent_block *eb;
5243 struct ocfs2_extent_list *rightmost_el, *el;
5244 struct ocfs2_extent_rec split_rec;
5245 struct ocfs2_extent_rec *rec;
5246 struct ocfs2_insert_type insert;
5249 * Setup the record to split before we grow the tree.
5251 el = path_leaf_el(path);
5252 rec = &el->l_recs[index];
5253 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5254 &split_rec, new_range, rec);
5256 depth = path->p_tree_depth;
5257 if (depth > 0) {
5258 ret = ocfs2_read_extent_block(et->et_ci,
5259 ocfs2_et_get_last_eb_blk(et),
5260 &last_eb_bh);
5261 if (ret < 0) {
5262 mlog_errno(ret);
5263 goto out;
5266 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5267 rightmost_el = &eb->h_list;
5268 } else
5269 rightmost_el = path_leaf_el(path);
5271 credits += path->p_tree_depth +
5272 ocfs2_extend_meta_needed(et->et_root_el);
5273 ret = ocfs2_extend_trans(handle, credits);
5274 if (ret) {
5275 mlog_errno(ret);
5276 goto out;
5279 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5280 le16_to_cpu(rightmost_el->l_count)) {
5281 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
5282 meta_ac);
5283 if (ret) {
5284 mlog_errno(ret);
5285 goto out;
5289 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5290 insert.ins_appending = APPEND_NONE;
5291 insert.ins_contig = CONTIG_NONE;
5292 insert.ins_split = SPLIT_RIGHT;
5293 insert.ins_tree_depth = depth;
5295 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
5296 if (ret)
5297 mlog_errno(ret);
5299 out:
5300 brelse(last_eb_bh);
5301 return ret;
5304 static int ocfs2_truncate_rec(handle_t *handle,
5305 struct ocfs2_extent_tree *et,
5306 struct ocfs2_path *path, int index,
5307 struct ocfs2_cached_dealloc_ctxt *dealloc,
5308 u32 cpos, u32 len)
5310 int ret;
5311 u32 left_cpos, rec_range, trunc_range;
5312 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5313 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5314 struct ocfs2_path *left_path = NULL;
5315 struct ocfs2_extent_list *el = path_leaf_el(path);
5316 struct ocfs2_extent_rec *rec;
5317 struct ocfs2_extent_block *eb;
5319 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
5320 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5321 if (ret) {
5322 mlog_errno(ret);
5323 goto out;
5326 index--;
5329 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5330 path->p_tree_depth) {
5332 * Check whether this is the rightmost tree record. If
5333 * we remove all of this record or part of its right
5334 * edge then an update of the record lengths above it
5335 * will be required.
5337 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5338 if (eb->h_next_leaf_blk == 0)
5339 is_rightmost_tree_rec = 1;
5342 rec = &el->l_recs[index];
5343 if (index == 0 && path->p_tree_depth &&
5344 le32_to_cpu(rec->e_cpos) == cpos) {
5346 * Changing the leftmost offset (via partial or whole
5347 * record truncate) of an interior (or rightmost) path
5348 * means we have to update the subtree that is formed
5349 * by this leaf and the one to it's left.
5351 * There are two cases we can skip:
5352 * 1) Path is the leftmost one in our btree.
5353 * 2) The leaf is rightmost and will be empty after
5354 * we remove the extent record - the rotate code
5355 * knows how to update the newly formed edge.
5358 ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
5359 if (ret) {
5360 mlog_errno(ret);
5361 goto out;
5364 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5365 left_path = ocfs2_new_path_from_path(path);
5366 if (!left_path) {
5367 ret = -ENOMEM;
5368 mlog_errno(ret);
5369 goto out;
5372 ret = ocfs2_find_path(et->et_ci, left_path,
5373 left_cpos);
5374 if (ret) {
5375 mlog_errno(ret);
5376 goto out;
5381 ret = ocfs2_extend_rotate_transaction(handle, 0,
5382 handle->h_buffer_credits,
5383 path);
5384 if (ret) {
5385 mlog_errno(ret);
5386 goto out;
5389 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
5390 if (ret) {
5391 mlog_errno(ret);
5392 goto out;
5395 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
5396 if (ret) {
5397 mlog_errno(ret);
5398 goto out;
5401 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5402 trunc_range = cpos + len;
5404 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5405 int next_free;
5407 memset(rec, 0, sizeof(*rec));
5408 ocfs2_cleanup_merge(el, index);
5409 wants_rotate = 1;
5411 next_free = le16_to_cpu(el->l_next_free_rec);
5412 if (is_rightmost_tree_rec && next_free > 1) {
5414 * We skip the edge update if this path will
5415 * be deleted by the rotate code.
5417 rec = &el->l_recs[next_free - 1];
5418 ocfs2_adjust_rightmost_records(handle, et, path,
5419 rec);
5421 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5422 /* Remove leftmost portion of the record. */
5423 le32_add_cpu(&rec->e_cpos, len);
5424 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5425 le16_add_cpu(&rec->e_leaf_clusters, -len);
5426 } else if (rec_range == trunc_range) {
5427 /* Remove rightmost portion of the record */
5428 le16_add_cpu(&rec->e_leaf_clusters, -len);
5429 if (is_rightmost_tree_rec)
5430 ocfs2_adjust_rightmost_records(handle, et, path, rec);
5431 } else {
5432 /* Caller should have trapped this. */
5433 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5434 "(%u, %u)\n",
5435 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5436 le32_to_cpu(rec->e_cpos),
5437 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5438 BUG();
5441 if (left_path) {
5442 int subtree_index;
5444 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
5445 ocfs2_complete_edge_insert(handle, left_path, path,
5446 subtree_index);
5449 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5451 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
5452 if (ret) {
5453 mlog_errno(ret);
5454 goto out;
5457 out:
5458 ocfs2_free_path(left_path);
5459 return ret;
5462 int ocfs2_remove_extent(handle_t *handle,
5463 struct ocfs2_extent_tree *et,
5464 u32 cpos, u32 len,
5465 struct ocfs2_alloc_context *meta_ac,
5466 struct ocfs2_cached_dealloc_ctxt *dealloc)
5468 int ret, index;
5469 u32 rec_range, trunc_range;
5470 struct ocfs2_extent_rec *rec;
5471 struct ocfs2_extent_list *el;
5472 struct ocfs2_path *path = NULL;
5475 * XXX: Why are we truncating to 0 instead of wherever this
5476 * affects us?
5478 ocfs2_et_extent_map_truncate(et, 0);
5480 path = ocfs2_new_path_from_et(et);
5481 if (!path) {
5482 ret = -ENOMEM;
5483 mlog_errno(ret);
5484 goto out;
5487 ret = ocfs2_find_path(et->et_ci, path, cpos);
5488 if (ret) {
5489 mlog_errno(ret);
5490 goto out;
5493 el = path_leaf_el(path);
5494 index = ocfs2_search_extent_list(el, cpos);
5495 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5496 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5497 "Owner %llu has an extent at cpos %u which can no "
5498 "longer be found.\n",
5499 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5500 cpos);
5501 ret = -EROFS;
5502 goto out;
5506 * We have 3 cases of extent removal:
5507 * 1) Range covers the entire extent rec
5508 * 2) Range begins or ends on one edge of the extent rec
5509 * 3) Range is in the middle of the extent rec (no shared edges)
5511 * For case 1 we remove the extent rec and left rotate to
5512 * fill the hole.
5514 * For case 2 we just shrink the existing extent rec, with a
5515 * tree update if the shrinking edge is also the edge of an
5516 * extent block.
5518 * For case 3 we do a right split to turn the extent rec into
5519 * something case 2 can handle.
5521 rec = &el->l_recs[index];
5522 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5523 trunc_range = cpos + len;
5525 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5527 mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
5528 "(cpos %u, len %u)\n",
5529 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5530 cpos, len, index,
5531 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5533 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5534 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5535 cpos, len);
5536 if (ret) {
5537 mlog_errno(ret);
5538 goto out;
5540 } else {
5541 ret = ocfs2_split_tree(handle, et, path, index,
5542 trunc_range, meta_ac);
5543 if (ret) {
5544 mlog_errno(ret);
5545 goto out;
5549 * The split could have manipulated the tree enough to
5550 * move the record location, so we have to look for it again.
5552 ocfs2_reinit_path(path, 1);
5554 ret = ocfs2_find_path(et->et_ci, path, cpos);
5555 if (ret) {
5556 mlog_errno(ret);
5557 goto out;
5560 el = path_leaf_el(path);
5561 index = ocfs2_search_extent_list(el, cpos);
5562 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5563 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5564 "Owner %llu: split at cpos %u lost record.",
5565 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5566 cpos);
5567 ret = -EROFS;
5568 goto out;
5572 * Double check our values here. If anything is fishy,
5573 * it's easier to catch it at the top level.
5575 rec = &el->l_recs[index];
5576 rec_range = le32_to_cpu(rec->e_cpos) +
5577 ocfs2_rec_clusters(el, rec);
5578 if (rec_range != trunc_range) {
5579 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5580 "Owner %llu: error after split at cpos %u"
5581 "trunc len %u, existing record is (%u,%u)",
5582 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5583 cpos, len, le32_to_cpu(rec->e_cpos),
5584 ocfs2_rec_clusters(el, rec));
5585 ret = -EROFS;
5586 goto out;
5589 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5590 cpos, len);
5591 if (ret) {
5592 mlog_errno(ret);
5593 goto out;
5597 out:
5598 ocfs2_free_path(path);
5599 return ret;
5602 int ocfs2_remove_btree_range(struct inode *inode,
5603 struct ocfs2_extent_tree *et,
5604 u32 cpos, u32 phys_cpos, u32 len,
5605 struct ocfs2_cached_dealloc_ctxt *dealloc)
5607 int ret;
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;
5614 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5615 if (ret) {
5616 mlog_errno(ret);
5617 return ret;
5620 mutex_lock(&tl_inode->i_mutex);
5622 if (ocfs2_truncate_log_needs_flush(osb)) {
5623 ret = __ocfs2_flush_truncate_log(osb);
5624 if (ret < 0) {
5625 mlog_errno(ret);
5626 goto out;
5630 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
5631 if (IS_ERR(handle)) {
5632 ret = PTR_ERR(handle);
5633 mlog_errno(ret);
5634 goto out;
5637 ret = ocfs2_et_root_journal_access(handle, et,
5638 OCFS2_JOURNAL_ACCESS_WRITE);
5639 if (ret) {
5640 mlog_errno(ret);
5641 goto out;
5644 dquot_free_space_nodirty(inode,
5645 ocfs2_clusters_to_bytes(inode->i_sb, len));
5647 ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
5648 if (ret) {
5649 mlog_errno(ret);
5650 goto out_commit;
5653 ocfs2_et_update_clusters(et, -len);
5655 ocfs2_journal_dirty(handle, et->et_root_bh);
5657 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5658 if (ret)
5659 mlog_errno(ret);
5661 out_commit:
5662 ocfs2_commit_trans(osb, handle);
5663 out:
5664 mutex_unlock(&tl_inode->i_mutex);
5666 if (meta_ac)
5667 ocfs2_free_alloc_context(meta_ac);
5669 return ret;
5672 int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
5674 struct buffer_head *tl_bh = osb->osb_tl_bh;
5675 struct ocfs2_dinode *di;
5676 struct ocfs2_truncate_log *tl;
5678 di = (struct ocfs2_dinode *) tl_bh->b_data;
5679 tl = &di->id2.i_dealloc;
5681 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5682 "slot %d, invalid truncate log parameters: used = "
5683 "%u, count = %u\n", osb->slot_num,
5684 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5685 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5688 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5689 unsigned int new_start)
5691 unsigned int tail_index;
5692 unsigned int current_tail;
5694 /* No records, nothing to coalesce */
5695 if (!le16_to_cpu(tl->tl_used))
5696 return 0;
5698 tail_index = le16_to_cpu(tl->tl_used) - 1;
5699 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5700 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5702 return current_tail == new_start;
5705 int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5706 handle_t *handle,
5707 u64 start_blk,
5708 unsigned int num_clusters)
5710 int status, index;
5711 unsigned int start_cluster, tl_count;
5712 struct inode *tl_inode = osb->osb_tl_inode;
5713 struct buffer_head *tl_bh = osb->osb_tl_bh;
5714 struct ocfs2_dinode *di;
5715 struct ocfs2_truncate_log *tl;
5717 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5718 (unsigned long long)start_blk, num_clusters);
5720 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5722 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5724 di = (struct ocfs2_dinode *) tl_bh->b_data;
5726 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5727 * by the underlying call to ocfs2_read_inode_block(), so any
5728 * corruption is a code bug */
5729 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5731 tl = &di->id2.i_dealloc;
5732 tl_count = le16_to_cpu(tl->tl_count);
5733 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5734 tl_count == 0,
5735 "Truncate record count on #%llu invalid "
5736 "wanted %u, actual %u\n",
5737 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5738 ocfs2_truncate_recs_per_inode(osb->sb),
5739 le16_to_cpu(tl->tl_count));
5741 /* Caller should have known to flush before calling us. */
5742 index = le16_to_cpu(tl->tl_used);
5743 if (index >= tl_count) {
5744 status = -ENOSPC;
5745 mlog_errno(status);
5746 goto bail;
5749 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5750 OCFS2_JOURNAL_ACCESS_WRITE);
5751 if (status < 0) {
5752 mlog_errno(status);
5753 goto bail;
5756 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5757 "%llu (index = %d)\n", num_clusters, start_cluster,
5758 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
5760 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5762 * Move index back to the record we are coalescing with.
5763 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5765 index--;
5767 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5768 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5769 index, le32_to_cpu(tl->tl_recs[index].t_start),
5770 num_clusters);
5771 } else {
5772 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5773 tl->tl_used = cpu_to_le16(index + 1);
5775 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5777 ocfs2_journal_dirty(handle, tl_bh);
5779 bail:
5780 mlog_exit(status);
5781 return status;
5784 static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
5785 handle_t *handle,
5786 struct inode *data_alloc_inode,
5787 struct buffer_head *data_alloc_bh)
5789 int status = 0;
5790 int i;
5791 unsigned int num_clusters;
5792 u64 start_blk;
5793 struct ocfs2_truncate_rec rec;
5794 struct ocfs2_dinode *di;
5795 struct ocfs2_truncate_log *tl;
5796 struct inode *tl_inode = osb->osb_tl_inode;
5797 struct buffer_head *tl_bh = osb->osb_tl_bh;
5799 mlog_entry_void();
5801 di = (struct ocfs2_dinode *) tl_bh->b_data;
5802 tl = &di->id2.i_dealloc;
5803 i = le16_to_cpu(tl->tl_used) - 1;
5804 while (i >= 0) {
5805 /* Caller has given us at least enough credits to
5806 * update the truncate log dinode */
5807 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
5808 OCFS2_JOURNAL_ACCESS_WRITE);
5809 if (status < 0) {
5810 mlog_errno(status);
5811 goto bail;
5814 tl->tl_used = cpu_to_le16(i);
5816 ocfs2_journal_dirty(handle, tl_bh);
5818 /* TODO: Perhaps we can calculate the bulk of the
5819 * credits up front rather than extending like
5820 * this. */
5821 status = ocfs2_extend_trans(handle,
5822 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5823 if (status < 0) {
5824 mlog_errno(status);
5825 goto bail;
5828 rec = tl->tl_recs[i];
5829 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5830 le32_to_cpu(rec.t_start));
5831 num_clusters = le32_to_cpu(rec.t_clusters);
5833 /* if start_blk is not set, we ignore the record as
5834 * invalid. */
5835 if (start_blk) {
5836 mlog(0, "free record %d, start = %u, clusters = %u\n",
5837 i, le32_to_cpu(rec.t_start), num_clusters);
5839 status = ocfs2_free_clusters(handle, data_alloc_inode,
5840 data_alloc_bh, start_blk,
5841 num_clusters);
5842 if (status < 0) {
5843 mlog_errno(status);
5844 goto bail;
5847 i--;
5850 bail:
5851 mlog_exit(status);
5852 return status;
5855 /* Expects you to already be holding tl_inode->i_mutex */
5856 int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5858 int status;
5859 unsigned int num_to_flush;
5860 handle_t *handle;
5861 struct inode *tl_inode = osb->osb_tl_inode;
5862 struct inode *data_alloc_inode = NULL;
5863 struct buffer_head *tl_bh = osb->osb_tl_bh;
5864 struct buffer_head *data_alloc_bh = NULL;
5865 struct ocfs2_dinode *di;
5866 struct ocfs2_truncate_log *tl;
5868 mlog_entry_void();
5870 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
5872 di = (struct ocfs2_dinode *) tl_bh->b_data;
5874 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5875 * by the underlying call to ocfs2_read_inode_block(), so any
5876 * corruption is a code bug */
5877 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5879 tl = &di->id2.i_dealloc;
5880 num_to_flush = le16_to_cpu(tl->tl_used);
5881 mlog(0, "Flush %u records from truncate log #%llu\n",
5882 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
5883 if (!num_to_flush) {
5884 status = 0;
5885 goto out;
5888 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5889 GLOBAL_BITMAP_SYSTEM_INODE,
5890 OCFS2_INVALID_SLOT);
5891 if (!data_alloc_inode) {
5892 status = -EINVAL;
5893 mlog(ML_ERROR, "Could not get bitmap inode!\n");
5894 goto out;
5897 mutex_lock(&data_alloc_inode->i_mutex);
5899 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
5900 if (status < 0) {
5901 mlog_errno(status);
5902 goto out_mutex;
5905 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
5906 if (IS_ERR(handle)) {
5907 status = PTR_ERR(handle);
5908 mlog_errno(status);
5909 goto out_unlock;
5912 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5913 data_alloc_bh);
5914 if (status < 0)
5915 mlog_errno(status);
5917 ocfs2_commit_trans(osb, handle);
5919 out_unlock:
5920 brelse(data_alloc_bh);
5921 ocfs2_inode_unlock(data_alloc_inode, 1);
5923 out_mutex:
5924 mutex_unlock(&data_alloc_inode->i_mutex);
5925 iput(data_alloc_inode);
5927 out:
5928 mlog_exit(status);
5929 return status;
5932 int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5934 int status;
5935 struct inode *tl_inode = osb->osb_tl_inode;
5937 mutex_lock(&tl_inode->i_mutex);
5938 status = __ocfs2_flush_truncate_log(osb);
5939 mutex_unlock(&tl_inode->i_mutex);
5941 return status;
5944 static void ocfs2_truncate_log_worker(struct work_struct *work)
5946 int status;
5947 struct ocfs2_super *osb =
5948 container_of(work, struct ocfs2_super,
5949 osb_truncate_log_wq.work);
5951 mlog_entry_void();
5953 status = ocfs2_flush_truncate_log(osb);
5954 if (status < 0)
5955 mlog_errno(status);
5956 else
5957 ocfs2_init_steal_slots(osb);
5959 mlog_exit(status);
5962 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5963 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5964 int cancel)
5966 if (osb->osb_tl_inode) {
5967 /* We want to push off log flushes while truncates are
5968 * still running. */
5969 if (cancel)
5970 cancel_delayed_work(&osb->osb_truncate_log_wq);
5972 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5973 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5977 static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5978 int slot_num,
5979 struct inode **tl_inode,
5980 struct buffer_head **tl_bh)
5982 int status;
5983 struct inode *inode = NULL;
5984 struct buffer_head *bh = NULL;
5986 inode = ocfs2_get_system_file_inode(osb,
5987 TRUNCATE_LOG_SYSTEM_INODE,
5988 slot_num);
5989 if (!inode) {
5990 status = -EINVAL;
5991 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5992 goto bail;
5995 status = ocfs2_read_inode_block(inode, &bh);
5996 if (status < 0) {
5997 iput(inode);
5998 mlog_errno(status);
5999 goto bail;
6002 *tl_inode = inode;
6003 *tl_bh = bh;
6004 bail:
6005 mlog_exit(status);
6006 return status;
6009 /* called during the 1st stage of node recovery. we stamp a clean
6010 * truncate log and pass back a copy for processing later. if the
6011 * truncate log does not require processing, a *tl_copy is set to
6012 * NULL. */
6013 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6014 int slot_num,
6015 struct ocfs2_dinode **tl_copy)
6017 int status;
6018 struct inode *tl_inode = NULL;
6019 struct buffer_head *tl_bh = NULL;
6020 struct ocfs2_dinode *di;
6021 struct ocfs2_truncate_log *tl;
6023 *tl_copy = NULL;
6025 mlog(0, "recover truncate log from slot %d\n", slot_num);
6027 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6028 if (status < 0) {
6029 mlog_errno(status);
6030 goto bail;
6033 di = (struct ocfs2_dinode *) tl_bh->b_data;
6035 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6036 * validated by the underlying call to ocfs2_read_inode_block(),
6037 * so any corruption is a code bug */
6038 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6040 tl = &di->id2.i_dealloc;
6041 if (le16_to_cpu(tl->tl_used)) {
6042 mlog(0, "We'll have %u logs to recover\n",
6043 le16_to_cpu(tl->tl_used));
6045 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6046 if (!(*tl_copy)) {
6047 status = -ENOMEM;
6048 mlog_errno(status);
6049 goto bail;
6052 /* Assuming the write-out below goes well, this copy
6053 * will be passed back to recovery for processing. */
6054 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6056 /* All we need to do to clear the truncate log is set
6057 * tl_used. */
6058 tl->tl_used = 0;
6060 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
6061 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
6062 if (status < 0) {
6063 mlog_errno(status);
6064 goto bail;
6068 bail:
6069 if (tl_inode)
6070 iput(tl_inode);
6071 brelse(tl_bh);
6073 if (status < 0 && (*tl_copy)) {
6074 kfree(*tl_copy);
6075 *tl_copy = NULL;
6078 mlog_exit(status);
6079 return status;
6082 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6083 struct ocfs2_dinode *tl_copy)
6085 int status = 0;
6086 int i;
6087 unsigned int clusters, num_recs, start_cluster;
6088 u64 start_blk;
6089 handle_t *handle;
6090 struct inode *tl_inode = osb->osb_tl_inode;
6091 struct ocfs2_truncate_log *tl;
6093 mlog_entry_void();
6095 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6096 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6097 return -EINVAL;
6100 tl = &tl_copy->id2.i_dealloc;
6101 num_recs = le16_to_cpu(tl->tl_used);
6102 mlog(0, "cleanup %u records from %llu\n", num_recs,
6103 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
6105 mutex_lock(&tl_inode->i_mutex);
6106 for(i = 0; i < num_recs; i++) {
6107 if (ocfs2_truncate_log_needs_flush(osb)) {
6108 status = __ocfs2_flush_truncate_log(osb);
6109 if (status < 0) {
6110 mlog_errno(status);
6111 goto bail_up;
6115 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6116 if (IS_ERR(handle)) {
6117 status = PTR_ERR(handle);
6118 mlog_errno(status);
6119 goto bail_up;
6122 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6123 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6124 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6126 status = ocfs2_truncate_log_append(osb, handle,
6127 start_blk, clusters);
6128 ocfs2_commit_trans(osb, handle);
6129 if (status < 0) {
6130 mlog_errno(status);
6131 goto bail_up;
6135 bail_up:
6136 mutex_unlock(&tl_inode->i_mutex);
6138 mlog_exit(status);
6139 return status;
6142 void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6144 int status;
6145 struct inode *tl_inode = osb->osb_tl_inode;
6147 mlog_entry_void();
6149 if (tl_inode) {
6150 cancel_delayed_work(&osb->osb_truncate_log_wq);
6151 flush_workqueue(ocfs2_wq);
6153 status = ocfs2_flush_truncate_log(osb);
6154 if (status < 0)
6155 mlog_errno(status);
6157 brelse(osb->osb_tl_bh);
6158 iput(osb->osb_tl_inode);
6161 mlog_exit_void();
6164 int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6166 int status;
6167 struct inode *tl_inode = NULL;
6168 struct buffer_head *tl_bh = NULL;
6170 mlog_entry_void();
6172 status = ocfs2_get_truncate_log_info(osb,
6173 osb->slot_num,
6174 &tl_inode,
6175 &tl_bh);
6176 if (status < 0)
6177 mlog_errno(status);
6179 /* ocfs2_truncate_log_shutdown keys on the existence of
6180 * osb->osb_tl_inode so we don't set any of the osb variables
6181 * until we're sure all is well. */
6182 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6183 ocfs2_truncate_log_worker);
6184 osb->osb_tl_bh = tl_bh;
6185 osb->osb_tl_inode = tl_inode;
6187 mlog_exit(status);
6188 return status;
6192 * Delayed de-allocation of suballocator blocks.
6194 * Some sets of block de-allocations might involve multiple suballocator inodes.
6196 * The locking for this can get extremely complicated, especially when
6197 * the suballocator inodes to delete from aren't known until deep
6198 * within an unrelated codepath.
6200 * ocfs2_extent_block structures are a good example of this - an inode
6201 * btree could have been grown by any number of nodes each allocating
6202 * out of their own suballoc inode.
6204 * These structures allow the delay of block de-allocation until a
6205 * later time, when locking of multiple cluster inodes won't cause
6206 * deadlock.
6210 * Describe a single bit freed from a suballocator. For the block
6211 * suballocators, it represents one block. For the global cluster
6212 * allocator, it represents some clusters and free_bit indicates
6213 * clusters number.
6215 struct ocfs2_cached_block_free {
6216 struct ocfs2_cached_block_free *free_next;
6217 u64 free_blk;
6218 unsigned int free_bit;
6221 struct ocfs2_per_slot_free_list {
6222 struct ocfs2_per_slot_free_list *f_next_suballocator;
6223 int f_inode_type;
6224 int f_slot;
6225 struct ocfs2_cached_block_free *f_first;
6228 static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6229 int sysfile_type,
6230 int slot,
6231 struct ocfs2_cached_block_free *head)
6233 int ret;
6234 u64 bg_blkno;
6235 handle_t *handle;
6236 struct inode *inode;
6237 struct buffer_head *di_bh = NULL;
6238 struct ocfs2_cached_block_free *tmp;
6240 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6241 if (!inode) {
6242 ret = -EINVAL;
6243 mlog_errno(ret);
6244 goto out;
6247 mutex_lock(&inode->i_mutex);
6249 ret = ocfs2_inode_lock(inode, &di_bh, 1);
6250 if (ret) {
6251 mlog_errno(ret);
6252 goto out_mutex;
6255 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6256 if (IS_ERR(handle)) {
6257 ret = PTR_ERR(handle);
6258 mlog_errno(ret);
6259 goto out_unlock;
6262 while (head) {
6263 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6264 head->free_bit);
6265 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6266 head->free_bit, (unsigned long long)head->free_blk);
6268 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6269 head->free_bit, bg_blkno, 1);
6270 if (ret) {
6271 mlog_errno(ret);
6272 goto out_journal;
6275 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6276 if (ret) {
6277 mlog_errno(ret);
6278 goto out_journal;
6281 tmp = head;
6282 head = head->free_next;
6283 kfree(tmp);
6286 out_journal:
6287 ocfs2_commit_trans(osb, handle);
6289 out_unlock:
6290 ocfs2_inode_unlock(inode, 1);
6291 brelse(di_bh);
6292 out_mutex:
6293 mutex_unlock(&inode->i_mutex);
6294 iput(inode);
6295 out:
6296 while(head) {
6297 /* Premature exit may have left some dangling items. */
6298 tmp = head;
6299 head = head->free_next;
6300 kfree(tmp);
6303 return ret;
6306 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6307 u64 blkno, unsigned int bit)
6309 int ret = 0;
6310 struct ocfs2_cached_block_free *item;
6312 item = kmalloc(sizeof(*item), GFP_NOFS);
6313 if (item == NULL) {
6314 ret = -ENOMEM;
6315 mlog_errno(ret);
6316 return ret;
6319 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6320 bit, (unsigned long long)blkno);
6322 item->free_blk = blkno;
6323 item->free_bit = bit;
6324 item->free_next = ctxt->c_global_allocator;
6326 ctxt->c_global_allocator = item;
6327 return ret;
6330 static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6331 struct ocfs2_cached_block_free *head)
6333 struct ocfs2_cached_block_free *tmp;
6334 struct inode *tl_inode = osb->osb_tl_inode;
6335 handle_t *handle;
6336 int ret = 0;
6338 mutex_lock(&tl_inode->i_mutex);
6340 while (head) {
6341 if (ocfs2_truncate_log_needs_flush(osb)) {
6342 ret = __ocfs2_flush_truncate_log(osb);
6343 if (ret < 0) {
6344 mlog_errno(ret);
6345 break;
6349 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6350 if (IS_ERR(handle)) {
6351 ret = PTR_ERR(handle);
6352 mlog_errno(ret);
6353 break;
6356 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6357 head->free_bit);
6359 ocfs2_commit_trans(osb, handle);
6360 tmp = head;
6361 head = head->free_next;
6362 kfree(tmp);
6364 if (ret < 0) {
6365 mlog_errno(ret);
6366 break;
6370 mutex_unlock(&tl_inode->i_mutex);
6372 while (head) {
6373 /* Premature exit may have left some dangling items. */
6374 tmp = head;
6375 head = head->free_next;
6376 kfree(tmp);
6379 return ret;
6382 int ocfs2_run_deallocs(struct ocfs2_super *osb,
6383 struct ocfs2_cached_dealloc_ctxt *ctxt)
6385 int ret = 0, ret2;
6386 struct ocfs2_per_slot_free_list *fl;
6388 if (!ctxt)
6389 return 0;
6391 while (ctxt->c_first_suballocator) {
6392 fl = ctxt->c_first_suballocator;
6394 if (fl->f_first) {
6395 mlog(0, "Free items: (type %u, slot %d)\n",
6396 fl->f_inode_type, fl->f_slot);
6397 ret2 = ocfs2_free_cached_blocks(osb,
6398 fl->f_inode_type,
6399 fl->f_slot,
6400 fl->f_first);
6401 if (ret2)
6402 mlog_errno(ret2);
6403 if (!ret)
6404 ret = ret2;
6407 ctxt->c_first_suballocator = fl->f_next_suballocator;
6408 kfree(fl);
6411 if (ctxt->c_global_allocator) {
6412 ret2 = ocfs2_free_cached_clusters(osb,
6413 ctxt->c_global_allocator);
6414 if (ret2)
6415 mlog_errno(ret2);
6416 if (!ret)
6417 ret = ret2;
6419 ctxt->c_global_allocator = NULL;
6422 return ret;
6425 static struct ocfs2_per_slot_free_list *
6426 ocfs2_find_per_slot_free_list(int type,
6427 int slot,
6428 struct ocfs2_cached_dealloc_ctxt *ctxt)
6430 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6432 while (fl) {
6433 if (fl->f_inode_type == type && fl->f_slot == slot)
6434 return fl;
6436 fl = fl->f_next_suballocator;
6439 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6440 if (fl) {
6441 fl->f_inode_type = type;
6442 fl->f_slot = slot;
6443 fl->f_first = NULL;
6444 fl->f_next_suballocator = ctxt->c_first_suballocator;
6446 ctxt->c_first_suballocator = fl;
6448 return fl;
6451 int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6452 int type, int slot, u64 blkno,
6453 unsigned int bit)
6455 int ret;
6456 struct ocfs2_per_slot_free_list *fl;
6457 struct ocfs2_cached_block_free *item;
6459 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6460 if (fl == NULL) {
6461 ret = -ENOMEM;
6462 mlog_errno(ret);
6463 goto out;
6466 item = kmalloc(sizeof(*item), GFP_NOFS);
6467 if (item == NULL) {
6468 ret = -ENOMEM;
6469 mlog_errno(ret);
6470 goto out;
6473 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6474 type, slot, bit, (unsigned long long)blkno);
6476 item->free_blk = blkno;
6477 item->free_bit = bit;
6478 item->free_next = fl->f_first;
6480 fl->f_first = item;
6482 ret = 0;
6483 out:
6484 return ret;
6487 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6488 struct ocfs2_extent_block *eb)
6490 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6491 le16_to_cpu(eb->h_suballoc_slot),
6492 le64_to_cpu(eb->h_blkno),
6493 le16_to_cpu(eb->h_suballoc_bit));
6496 /* This function will figure out whether the currently last extent
6497 * block will be deleted, and if it will, what the new last extent
6498 * block will be so we can update his h_next_leaf_blk field, as well
6499 * as the dinodes i_last_eb_blk */
6500 static int ocfs2_find_new_last_ext_blk(struct inode *inode,
6501 unsigned int clusters_to_del,
6502 struct ocfs2_path *path,
6503 struct buffer_head **new_last_eb)
6505 int next_free, ret = 0;
6506 u32 cpos;
6507 struct ocfs2_extent_rec *rec;
6508 struct ocfs2_extent_block *eb;
6509 struct ocfs2_extent_list *el;
6510 struct buffer_head *bh = NULL;
6512 *new_last_eb = NULL;
6514 /* we have no tree, so of course, no last_eb. */
6515 if (!path->p_tree_depth)
6516 goto out;
6518 /* trunc to zero special case - this makes tree_depth = 0
6519 * regardless of what it is. */
6520 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
6521 goto out;
6523 el = path_leaf_el(path);
6524 BUG_ON(!el->l_next_free_rec);
6527 * Make sure that this extent list will actually be empty
6528 * after we clear away the data. We can shortcut out if
6529 * there's more than one non-empty extent in the
6530 * list. Otherwise, a check of the remaining extent is
6531 * necessary.
6533 next_free = le16_to_cpu(el->l_next_free_rec);
6534 rec = NULL;
6535 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6536 if (next_free > 2)
6537 goto out;
6539 /* We may have a valid extent in index 1, check it. */
6540 if (next_free == 2)
6541 rec = &el->l_recs[1];
6544 * Fall through - no more nonempty extents, so we want
6545 * to delete this leaf.
6547 } else {
6548 if (next_free > 1)
6549 goto out;
6551 rec = &el->l_recs[0];
6554 if (rec) {
6556 * Check it we'll only be trimming off the end of this
6557 * cluster.
6559 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
6560 goto out;
6563 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6564 if (ret) {
6565 mlog_errno(ret);
6566 goto out;
6569 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
6570 if (ret) {
6571 mlog_errno(ret);
6572 goto out;
6575 eb = (struct ocfs2_extent_block *) bh->b_data;
6576 el = &eb->h_list;
6578 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6579 * Any corruption is a code bug. */
6580 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
6582 *new_last_eb = bh;
6583 get_bh(*new_last_eb);
6584 mlog(0, "returning block %llu, (cpos: %u)\n",
6585 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6586 out:
6587 brelse(bh);
6589 return ret;
6593 * Trim some clusters off the rightmost edge of a tree. Only called
6594 * during truncate.
6596 * The caller needs to:
6597 * - start journaling of each path component.
6598 * - compute and fully set up any new last ext block
6600 static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6601 handle_t *handle, struct ocfs2_truncate_context *tc,
6602 u32 clusters_to_del, u64 *delete_start, u8 *flags)
6604 int ret, i, index = path->p_tree_depth;
6605 u32 new_edge = 0;
6606 u64 deleted_eb = 0;
6607 struct buffer_head *bh;
6608 struct ocfs2_extent_list *el;
6609 struct ocfs2_extent_rec *rec;
6611 *delete_start = 0;
6612 *flags = 0;
6614 while (index >= 0) {
6615 bh = path->p_node[index].bh;
6616 el = path->p_node[index].el;
6618 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6619 index, (unsigned long long)bh->b_blocknr);
6621 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6623 if (index !=
6624 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6625 ocfs2_error(inode->i_sb,
6626 "Inode %lu has invalid ext. block %llu",
6627 inode->i_ino,
6628 (unsigned long long)bh->b_blocknr);
6629 ret = -EROFS;
6630 goto out;
6633 find_tail_record:
6634 i = le16_to_cpu(el->l_next_free_rec) - 1;
6635 rec = &el->l_recs[i];
6637 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6638 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
6639 ocfs2_rec_clusters(el, rec),
6640 (unsigned long long)le64_to_cpu(rec->e_blkno),
6641 le16_to_cpu(el->l_next_free_rec));
6643 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
6645 if (le16_to_cpu(el->l_tree_depth) == 0) {
6647 * If the leaf block contains a single empty
6648 * extent and no records, we can just remove
6649 * the block.
6651 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6652 memset(rec, 0,
6653 sizeof(struct ocfs2_extent_rec));
6654 el->l_next_free_rec = cpu_to_le16(0);
6656 goto delete;
6660 * Remove any empty extents by shifting things
6661 * left. That should make life much easier on
6662 * the code below. This condition is rare
6663 * enough that we shouldn't see a performance
6664 * hit.
6666 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6667 le16_add_cpu(&el->l_next_free_rec, -1);
6669 for(i = 0;
6670 i < le16_to_cpu(el->l_next_free_rec); i++)
6671 el->l_recs[i] = el->l_recs[i + 1];
6673 memset(&el->l_recs[i], 0,
6674 sizeof(struct ocfs2_extent_rec));
6677 * We've modified our extent list. The
6678 * simplest way to handle this change
6679 * is to being the search from the
6680 * start again.
6682 goto find_tail_record;
6685 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
6688 * We'll use "new_edge" on our way back up the
6689 * tree to know what our rightmost cpos is.
6691 new_edge = le16_to_cpu(rec->e_leaf_clusters);
6692 new_edge += le32_to_cpu(rec->e_cpos);
6695 * The caller will use this to delete data blocks.
6697 *delete_start = le64_to_cpu(rec->e_blkno)
6698 + ocfs2_clusters_to_blocks(inode->i_sb,
6699 le16_to_cpu(rec->e_leaf_clusters));
6700 *flags = rec->e_flags;
6703 * If it's now empty, remove this record.
6705 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
6706 memset(rec, 0,
6707 sizeof(struct ocfs2_extent_rec));
6708 le16_add_cpu(&el->l_next_free_rec, -1);
6710 } else {
6711 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6712 memset(rec, 0,
6713 sizeof(struct ocfs2_extent_rec));
6714 le16_add_cpu(&el->l_next_free_rec, -1);
6716 goto delete;
6719 /* Can this actually happen? */
6720 if (le16_to_cpu(el->l_next_free_rec) == 0)
6721 goto delete;
6724 * We never actually deleted any clusters
6725 * because our leaf was empty. There's no
6726 * reason to adjust the rightmost edge then.
6728 if (new_edge == 0)
6729 goto delete;
6731 rec->e_int_clusters = cpu_to_le32(new_edge);
6732 le32_add_cpu(&rec->e_int_clusters,
6733 -le32_to_cpu(rec->e_cpos));
6736 * A deleted child record should have been
6737 * caught above.
6739 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
6742 delete:
6743 ocfs2_journal_dirty(handle, bh);
6745 mlog(0, "extent list container %llu, after: record %d: "
6746 "(%u, %u, %llu), next = %u.\n",
6747 (unsigned long long)bh->b_blocknr, i,
6748 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
6749 (unsigned long long)le64_to_cpu(rec->e_blkno),
6750 le16_to_cpu(el->l_next_free_rec));
6753 * We must be careful to only attempt delete of an
6754 * extent block (and not the root inode block).
6756 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6757 struct ocfs2_extent_block *eb =
6758 (struct ocfs2_extent_block *)bh->b_data;
6761 * Save this for use when processing the
6762 * parent block.
6764 deleted_eb = le64_to_cpu(eb->h_blkno);
6766 mlog(0, "deleting this extent block.\n");
6768 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
6770 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
6771 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6772 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6774 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6775 /* An error here is not fatal. */
6776 if (ret < 0)
6777 mlog_errno(ret);
6778 } else {
6779 deleted_eb = 0;
6782 index--;
6785 ret = 0;
6786 out:
6787 return ret;
6790 static int ocfs2_do_truncate(struct ocfs2_super *osb,
6791 unsigned int clusters_to_del,
6792 struct inode *inode,
6793 struct buffer_head *fe_bh,
6794 handle_t *handle,
6795 struct ocfs2_truncate_context *tc,
6796 struct ocfs2_path *path,
6797 struct ocfs2_alloc_context *meta_ac)
6799 int status;
6800 struct ocfs2_dinode *fe;
6801 struct ocfs2_extent_block *last_eb = NULL;
6802 struct ocfs2_extent_list *el;
6803 struct buffer_head *last_eb_bh = NULL;
6804 u64 delete_blk = 0;
6805 u8 rec_flags;
6807 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6809 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
6810 path, &last_eb_bh);
6811 if (status < 0) {
6812 mlog_errno(status);
6813 goto bail;
6817 * Each component will be touched, so we might as well journal
6818 * here to avoid having to handle errors later.
6820 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
6821 if (status < 0) {
6822 mlog_errno(status);
6823 goto bail;
6826 if (last_eb_bh) {
6827 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
6828 OCFS2_JOURNAL_ACCESS_WRITE);
6829 if (status < 0) {
6830 mlog_errno(status);
6831 goto bail;
6834 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6837 el = &(fe->id2.i_list);
6840 * Lower levels depend on this never happening, but it's best
6841 * to check it up here before changing the tree.
6843 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
6844 ocfs2_error(inode->i_sb,
6845 "Inode %lu has an empty extent record, depth %u\n",
6846 inode->i_ino, le16_to_cpu(el->l_tree_depth));
6847 status = -EROFS;
6848 goto bail;
6851 dquot_free_space_nodirty(inode,
6852 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
6853 spin_lock(&OCFS2_I(inode)->ip_lock);
6854 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6855 clusters_to_del;
6856 spin_unlock(&OCFS2_I(inode)->ip_lock);
6857 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
6858 inode->i_blocks = ocfs2_inode_sector_count(inode);
6860 status = ocfs2_trim_tree(inode, path, handle, tc,
6861 clusters_to_del, &delete_blk, &rec_flags);
6862 if (status) {
6863 mlog_errno(status);
6864 goto bail;
6867 if (le32_to_cpu(fe->i_clusters) == 0) {
6868 /* trunc to zero is a special case. */
6869 el->l_tree_depth = 0;
6870 fe->i_last_eb_blk = 0;
6871 } else if (last_eb)
6872 fe->i_last_eb_blk = last_eb->h_blkno;
6874 ocfs2_journal_dirty(handle, fe_bh);
6876 if (last_eb) {
6877 /* If there will be a new last extent block, then by
6878 * definition, there cannot be any leaves to the right of
6879 * him. */
6880 last_eb->h_next_leaf_blk = 0;
6881 ocfs2_journal_dirty(handle, last_eb_bh);
6884 if (delete_blk) {
6885 if (rec_flags & OCFS2_EXT_REFCOUNTED)
6886 status = ocfs2_decrease_refcount(inode, handle,
6887 ocfs2_blocks_to_clusters(osb->sb,
6888 delete_blk),
6889 clusters_to_del, meta_ac,
6890 &tc->tc_dealloc, 1);
6891 else
6892 status = ocfs2_truncate_log_append(osb, handle,
6893 delete_blk,
6894 clusters_to_del);
6895 if (status < 0) {
6896 mlog_errno(status);
6897 goto bail;
6900 status = 0;
6901 bail:
6902 brelse(last_eb_bh);
6903 mlog_exit(status);
6904 return status;
6907 static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
6909 set_buffer_uptodate(bh);
6910 mark_buffer_dirty(bh);
6911 return 0;
6914 void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6915 unsigned int from, unsigned int to,
6916 struct page *page, int zero, u64 *phys)
6918 int ret, partial = 0;
6920 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6921 if (ret)
6922 mlog_errno(ret);
6924 if (zero)
6925 zero_user_segment(page, from, to);
6928 * Need to set the buffers we zero'd into uptodate
6929 * here if they aren't - ocfs2_map_page_blocks()
6930 * might've skipped some
6932 ret = walk_page_buffers(handle, page_buffers(page),
6933 from, to, &partial,
6934 ocfs2_zero_func);
6935 if (ret < 0)
6936 mlog_errno(ret);
6937 else if (ocfs2_should_order_data(inode)) {
6938 ret = ocfs2_jbd2_file_inode(handle, inode);
6939 if (ret < 0)
6940 mlog_errno(ret);
6943 if (!partial)
6944 SetPageUptodate(page);
6946 flush_dcache_page(page);
6949 static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6950 loff_t end, struct page **pages,
6951 int numpages, u64 phys, handle_t *handle)
6953 int i;
6954 struct page *page;
6955 unsigned int from, to = PAGE_CACHE_SIZE;
6956 struct super_block *sb = inode->i_sb;
6958 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6960 if (numpages == 0)
6961 goto out;
6963 to = PAGE_CACHE_SIZE;
6964 for(i = 0; i < numpages; i++) {
6965 page = pages[i];
6967 from = start & (PAGE_CACHE_SIZE - 1);
6968 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6969 to = end & (PAGE_CACHE_SIZE - 1);
6971 BUG_ON(from > PAGE_CACHE_SIZE);
6972 BUG_ON(to > PAGE_CACHE_SIZE);
6974 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6975 &phys);
6977 start = (page->index + 1) << PAGE_CACHE_SHIFT;
6979 out:
6980 if (pages)
6981 ocfs2_unlock_and_free_pages(pages, numpages);
6984 int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
6985 struct page **pages, int *num)
6987 int numpages, ret = 0;
6988 struct address_space *mapping = inode->i_mapping;
6989 unsigned long index;
6990 loff_t last_page_bytes;
6992 BUG_ON(start > end);
6994 numpages = 0;
6995 last_page_bytes = PAGE_ALIGN(end);
6996 index = start >> PAGE_CACHE_SHIFT;
6997 do {
6998 pages[numpages] = grab_cache_page(mapping, index);
6999 if (!pages[numpages]) {
7000 ret = -ENOMEM;
7001 mlog_errno(ret);
7002 goto out;
7005 numpages++;
7006 index++;
7007 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
7009 out:
7010 if (ret != 0) {
7011 if (pages)
7012 ocfs2_unlock_and_free_pages(pages, numpages);
7013 numpages = 0;
7016 *num = numpages;
7018 return ret;
7021 static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
7022 struct page **pages, int *num)
7024 struct super_block *sb = inode->i_sb;
7026 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
7027 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
7029 return ocfs2_grab_pages(inode, start, end, pages, num);
7033 * Zero the area past i_size but still within an allocated
7034 * cluster. This avoids exposing nonzero data on subsequent file
7035 * extends.
7037 * We need to call this before i_size is updated on the inode because
7038 * otherwise block_write_full_page() will skip writeout of pages past
7039 * i_size. The new_i_size parameter is passed for this reason.
7041 int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
7042 u64 range_start, u64 range_end)
7044 int ret = 0, numpages;
7045 struct page **pages = NULL;
7046 u64 phys;
7047 unsigned int ext_flags;
7048 struct super_block *sb = inode->i_sb;
7051 * File systems which don't support sparse files zero on every
7052 * extend.
7054 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
7055 return 0;
7057 pages = kcalloc(ocfs2_pages_per_cluster(sb),
7058 sizeof(struct page *), GFP_NOFS);
7059 if (pages == NULL) {
7060 ret = -ENOMEM;
7061 mlog_errno(ret);
7062 goto out;
7065 if (range_start == range_end)
7066 goto out;
7068 ret = ocfs2_extent_map_get_blocks(inode,
7069 range_start >> sb->s_blocksize_bits,
7070 &phys, NULL, &ext_flags);
7071 if (ret) {
7072 mlog_errno(ret);
7073 goto out;
7077 * Tail is a hole, or is marked unwritten. In either case, we
7078 * can count on read and write to return/push zero's.
7080 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
7081 goto out;
7083 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7084 &numpages);
7085 if (ret) {
7086 mlog_errno(ret);
7087 goto out;
7090 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7091 numpages, phys, handle);
7094 * Initiate writeout of the pages we zero'd here. We don't
7095 * wait on them - the truncate_inode_pages() call later will
7096 * do that for us.
7098 ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
7099 range_end - 1);
7100 if (ret)
7101 mlog_errno(ret);
7103 out:
7104 if (pages)
7105 kfree(pages);
7107 return ret;
7110 static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7111 struct ocfs2_dinode *di)
7113 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
7114 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
7116 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7117 memset(&di->id2, 0, blocksize -
7118 offsetof(struct ocfs2_dinode, id2) -
7119 xattrsize);
7120 else
7121 memset(&di->id2, 0, blocksize -
7122 offsetof(struct ocfs2_dinode, id2));
7125 void ocfs2_dinode_new_extent_list(struct inode *inode,
7126 struct ocfs2_dinode *di)
7128 ocfs2_zero_dinode_id2_with_xattr(inode, di);
7129 di->id2.i_list.l_tree_depth = 0;
7130 di->id2.i_list.l_next_free_rec = 0;
7131 di->id2.i_list.l_count = cpu_to_le16(
7132 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
7135 void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7137 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7138 struct ocfs2_inline_data *idata = &di->id2.i_data;
7140 spin_lock(&oi->ip_lock);
7141 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7142 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7143 spin_unlock(&oi->ip_lock);
7146 * We clear the entire i_data structure here so that all
7147 * fields can be properly initialized.
7149 ocfs2_zero_dinode_id2_with_xattr(inode, di);
7151 idata->id_count = cpu_to_le16(
7152 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
7155 int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7156 struct buffer_head *di_bh)
7158 int ret, i, has_data, num_pages = 0;
7159 handle_t *handle;
7160 u64 uninitialized_var(block);
7161 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7162 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7163 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7164 struct ocfs2_alloc_context *data_ac = NULL;
7165 struct page **pages = NULL;
7166 loff_t end = osb->s_clustersize;
7167 struct ocfs2_extent_tree et;
7168 int did_quota = 0;
7170 has_data = i_size_read(inode) ? 1 : 0;
7172 if (has_data) {
7173 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7174 sizeof(struct page *), GFP_NOFS);
7175 if (pages == NULL) {
7176 ret = -ENOMEM;
7177 mlog_errno(ret);
7178 goto out;
7181 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7182 if (ret) {
7183 mlog_errno(ret);
7184 goto out;
7188 handle = ocfs2_start_trans(osb,
7189 ocfs2_inline_to_extents_credits(osb->sb));
7190 if (IS_ERR(handle)) {
7191 ret = PTR_ERR(handle);
7192 mlog_errno(ret);
7193 goto out_unlock;
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 if (has_data) {
7204 u32 bit_off, num;
7205 unsigned int page_end;
7206 u64 phys;
7208 ret = dquot_alloc_space_nodirty(inode,
7209 ocfs2_clusters_to_bytes(osb->sb, 1));
7210 if (ret)
7211 goto out_commit;
7212 did_quota = 1;
7214 data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
7216 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7217 &num);
7218 if (ret) {
7219 mlog_errno(ret);
7220 goto out_commit;
7224 * Save two copies, one for insert, and one that can
7225 * be changed by ocfs2_map_and_dirty_page() below.
7227 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7230 * Non sparse file systems zero on extend, so no need
7231 * to do that now.
7233 if (!ocfs2_sparse_alloc(osb) &&
7234 PAGE_CACHE_SIZE < osb->s_clustersize)
7235 end = PAGE_CACHE_SIZE;
7237 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7238 if (ret) {
7239 mlog_errno(ret);
7240 goto out_commit;
7244 * This should populate the 1st page for us and mark
7245 * it up to date.
7247 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7248 if (ret) {
7249 mlog_errno(ret);
7250 goto out_commit;
7253 page_end = PAGE_CACHE_SIZE;
7254 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7255 page_end = osb->s_clustersize;
7257 for (i = 0; i < num_pages; i++)
7258 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7259 pages[i], i > 0, &phys);
7262 spin_lock(&oi->ip_lock);
7263 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7264 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7265 spin_unlock(&oi->ip_lock);
7267 ocfs2_dinode_new_extent_list(inode, di);
7269 ocfs2_journal_dirty(handle, di_bh);
7271 if (has_data) {
7273 * An error at this point should be extremely rare. If
7274 * this proves to be false, we could always re-build
7275 * the in-inode data from our pages.
7277 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
7278 ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
7279 if (ret) {
7280 mlog_errno(ret);
7281 goto out_commit;
7284 inode->i_blocks = ocfs2_inode_sector_count(inode);
7287 out_commit:
7288 if (ret < 0 && did_quota)
7289 dquot_free_space_nodirty(inode,
7290 ocfs2_clusters_to_bytes(osb->sb, 1));
7292 ocfs2_commit_trans(osb, handle);
7294 out_unlock:
7295 if (data_ac)
7296 ocfs2_free_alloc_context(data_ac);
7298 out:
7299 if (pages) {
7300 ocfs2_unlock_and_free_pages(pages, num_pages);
7301 kfree(pages);
7304 return ret;
7308 * It is expected, that by the time you call this function,
7309 * inode->i_size and fe->i_size have been adjusted.
7311 * WARNING: This will kfree the truncate context
7313 int ocfs2_commit_truncate(struct ocfs2_super *osb,
7314 struct inode *inode,
7315 struct buffer_head *fe_bh,
7316 struct ocfs2_truncate_context *tc)
7318 int status, i, credits, tl_sem = 0;
7319 u32 clusters_to_del, new_highest_cpos, range;
7320 u64 blkno = 0;
7321 struct ocfs2_extent_list *el;
7322 handle_t *handle = NULL;
7323 struct inode *tl_inode = osb->osb_tl_inode;
7324 struct ocfs2_path *path = NULL;
7325 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
7326 struct ocfs2_alloc_context *meta_ac = NULL;
7327 struct ocfs2_refcount_tree *ref_tree = NULL;
7329 mlog_entry_void();
7331 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
7332 i_size_read(inode));
7334 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7335 ocfs2_journal_access_di);
7336 if (!path) {
7337 status = -ENOMEM;
7338 mlog_errno(status);
7339 goto bail;
7342 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7344 start:
7346 * Check that we still have allocation to delete.
7348 if (OCFS2_I(inode)->ip_clusters == 0) {
7349 status = 0;
7350 goto bail;
7353 credits = 0;
7356 * Truncate always works against the rightmost tree branch.
7358 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
7359 if (status) {
7360 mlog_errno(status);
7361 goto bail;
7364 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7365 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7368 * By now, el will point to the extent list on the bottom most
7369 * portion of this tree. Only the tail record is considered in
7370 * each pass.
7372 * We handle the following cases, in order:
7373 * - empty extent: delete the remaining branch
7374 * - remove the entire record
7375 * - remove a partial record
7376 * - no record needs to be removed (truncate has completed)
7378 el = path_leaf_el(path);
7379 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7380 ocfs2_error(inode->i_sb,
7381 "Inode %llu has empty extent block at %llu\n",
7382 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7383 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7384 status = -EROFS;
7385 goto bail;
7388 i = le16_to_cpu(el->l_next_free_rec) - 1;
7389 range = le32_to_cpu(el->l_recs[i].e_cpos) +
7390 ocfs2_rec_clusters(el, &el->l_recs[i]);
7391 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7392 clusters_to_del = 0;
7393 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
7394 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
7395 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
7396 } else if (range > new_highest_cpos) {
7397 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
7398 le32_to_cpu(el->l_recs[i].e_cpos)) -
7399 new_highest_cpos;
7400 blkno = le64_to_cpu(el->l_recs[i].e_blkno) +
7401 ocfs2_clusters_to_blocks(inode->i_sb,
7402 ocfs2_rec_clusters(el, &el->l_recs[i]) -
7403 clusters_to_del);
7404 } else {
7405 status = 0;
7406 goto bail;
7409 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7410 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7412 if (el->l_recs[i].e_flags & OCFS2_EXT_REFCOUNTED && clusters_to_del) {
7413 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
7414 OCFS2_HAS_REFCOUNT_FL));
7416 status = ocfs2_lock_refcount_tree(osb,
7417 le64_to_cpu(di->i_refcount_loc),
7418 1, &ref_tree, NULL);
7419 if (status) {
7420 mlog_errno(status);
7421 goto bail;
7424 status = ocfs2_prepare_refcount_change_for_del(inode, fe_bh,
7425 blkno,
7426 clusters_to_del,
7427 &credits,
7428 &meta_ac);
7429 if (status < 0) {
7430 mlog_errno(status);
7431 goto bail;
7435 mutex_lock(&tl_inode->i_mutex);
7436 tl_sem = 1;
7437 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7438 * record is free for use. If there isn't any, we flush to get
7439 * an empty truncate log. */
7440 if (ocfs2_truncate_log_needs_flush(osb)) {
7441 status = __ocfs2_flush_truncate_log(osb);
7442 if (status < 0) {
7443 mlog_errno(status);
7444 goto bail;
7448 credits += ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
7449 (struct ocfs2_dinode *)fe_bh->b_data,
7450 el);
7451 handle = ocfs2_start_trans(osb, credits);
7452 if (IS_ERR(handle)) {
7453 status = PTR_ERR(handle);
7454 handle = NULL;
7455 mlog_errno(status);
7456 goto bail;
7459 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7460 tc, path, meta_ac);
7461 if (status < 0) {
7462 mlog_errno(status);
7463 goto bail;
7466 mutex_unlock(&tl_inode->i_mutex);
7467 tl_sem = 0;
7469 ocfs2_commit_trans(osb, handle);
7470 handle = NULL;
7472 ocfs2_reinit_path(path, 1);
7474 if (meta_ac) {
7475 ocfs2_free_alloc_context(meta_ac);
7476 meta_ac = NULL;
7479 if (ref_tree) {
7480 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
7481 ref_tree = NULL;
7485 * The check above will catch the case where we've truncated
7486 * away all allocation.
7488 goto start;
7490 bail:
7492 ocfs2_schedule_truncate_log_flush(osb, 1);
7494 if (tl_sem)
7495 mutex_unlock(&tl_inode->i_mutex);
7497 if (handle)
7498 ocfs2_commit_trans(osb, handle);
7500 if (meta_ac)
7501 ocfs2_free_alloc_context(meta_ac);
7503 if (ref_tree)
7504 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
7506 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7508 ocfs2_free_path(path);
7510 /* This will drop the ext_alloc cluster lock for us */
7511 ocfs2_free_truncate_context(tc);
7513 mlog_exit(status);
7514 return status;
7518 * Expects the inode to already be locked.
7520 int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7521 struct inode *inode,
7522 struct buffer_head *fe_bh,
7523 struct ocfs2_truncate_context **tc)
7525 int status;
7526 unsigned int new_i_clusters;
7527 struct ocfs2_dinode *fe;
7528 struct ocfs2_extent_block *eb;
7529 struct buffer_head *last_eb_bh = NULL;
7531 mlog_entry_void();
7533 *tc = NULL;
7535 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7536 i_size_read(inode));
7537 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7539 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7540 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7541 (unsigned long long)le64_to_cpu(fe->i_size));
7543 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
7544 if (!(*tc)) {
7545 status = -ENOMEM;
7546 mlog_errno(status);
7547 goto bail;
7549 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
7551 if (fe->id2.i_list.l_tree_depth) {
7552 status = ocfs2_read_extent_block(INODE_CACHE(inode),
7553 le64_to_cpu(fe->i_last_eb_blk),
7554 &last_eb_bh);
7555 if (status < 0) {
7556 mlog_errno(status);
7557 goto bail;
7559 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7562 (*tc)->tc_last_eb_bh = last_eb_bh;
7564 status = 0;
7565 bail:
7566 if (status < 0) {
7567 if (*tc)
7568 ocfs2_free_truncate_context(*tc);
7569 *tc = NULL;
7571 mlog_exit_void();
7572 return status;
7576 * 'start' is inclusive, 'end' is not.
7578 int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7579 unsigned int start, unsigned int end, int trunc)
7581 int ret;
7582 unsigned int numbytes;
7583 handle_t *handle;
7584 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7585 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7586 struct ocfs2_inline_data *idata = &di->id2.i_data;
7588 if (end > i_size_read(inode))
7589 end = i_size_read(inode);
7591 BUG_ON(start >= end);
7593 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7594 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7595 !ocfs2_supports_inline_data(osb)) {
7596 ocfs2_error(inode->i_sb,
7597 "Inline data flags for inode %llu don't agree! "
7598 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7599 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7600 le16_to_cpu(di->i_dyn_features),
7601 OCFS2_I(inode)->ip_dyn_features,
7602 osb->s_feature_incompat);
7603 ret = -EROFS;
7604 goto out;
7607 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7608 if (IS_ERR(handle)) {
7609 ret = PTR_ERR(handle);
7610 mlog_errno(ret);
7611 goto out;
7614 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7615 OCFS2_JOURNAL_ACCESS_WRITE);
7616 if (ret) {
7617 mlog_errno(ret);
7618 goto out_commit;
7621 numbytes = end - start;
7622 memset(idata->id_data + start, 0, numbytes);
7625 * No need to worry about the data page here - it's been
7626 * truncated already and inline data doesn't need it for
7627 * pushing zero's to disk, so we'll let readpage pick it up
7628 * later.
7630 if (trunc) {
7631 i_size_write(inode, start);
7632 di->i_size = cpu_to_le64(start);
7635 inode->i_blocks = ocfs2_inode_sector_count(inode);
7636 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7638 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7639 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7641 ocfs2_journal_dirty(handle, di_bh);
7643 out_commit:
7644 ocfs2_commit_trans(osb, handle);
7646 out:
7647 return ret;
7650 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7653 * The caller is responsible for completing deallocation
7654 * before freeing the context.
7656 if (tc->tc_dealloc.c_first_suballocator != NULL)
7657 mlog(ML_NOTICE,
7658 "Truncate completion has non-empty dealloc context\n");
7660 brelse(tc->tc_last_eb_bh);
7662 kfree(tc);