1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
6 * Extent allocs and frees
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>
30 #include <linux/swap.h>
31 #include <linux/quotaops.h>
33 #define MLOG_MASK_PREFIX ML_DISK_ALLOC
34 #include <cluster/masklog.h>
40 #include "blockcheck.h"
42 #include "extent_map.h"
45 #include "localalloc.h"
53 #include "buffer_head_io.h"
57 * Operations for a specific extent tree type.
59 * To implement an on-disk btree (extent tree) type in ocfs2, add
60 * an ocfs2_extent_tree_operations structure and the matching
61 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
62 * for the allocation portion of the extent tree.
64 struct ocfs2_extent_tree_operations
{
66 * last_eb_blk is the block number of the right most leaf extent
67 * block. Most on-disk structures containing an extent tree store
68 * this value for fast access. The ->eo_set_last_eb_blk() and
69 * ->eo_get_last_eb_blk() operations access this value. They are
72 void (*eo_set_last_eb_blk
)(struct ocfs2_extent_tree
*et
,
74 u64 (*eo_get_last_eb_blk
)(struct ocfs2_extent_tree
*et
);
77 * The on-disk structure usually keeps track of how many total
78 * clusters are stored in this extent tree. This function updates
79 * that value. new_clusters is the delta, and must be
80 * added to the total. Required.
82 void (*eo_update_clusters
)(struct inode
*inode
,
83 struct ocfs2_extent_tree
*et
,
87 * If ->eo_insert_check() exists, it is called before rec is
88 * inserted into the extent tree. It is optional.
90 int (*eo_insert_check
)(struct inode
*inode
,
91 struct ocfs2_extent_tree
*et
,
92 struct ocfs2_extent_rec
*rec
);
93 int (*eo_sanity_check
)(struct inode
*inode
, struct ocfs2_extent_tree
*et
);
96 * --------------------------------------------------------------
97 * The remaining are internal to ocfs2_extent_tree and don't have
102 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
105 void (*eo_fill_root_el
)(struct ocfs2_extent_tree
*et
);
108 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
109 * it exists. If it does not, et->et_max_leaf_clusters is set
110 * to 0 (unlimited). Optional.
112 void (*eo_fill_max_leaf_clusters
)(struct inode
*inode
,
113 struct ocfs2_extent_tree
*et
);
118 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
121 static u64
ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree
*et
);
122 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
124 static void ocfs2_dinode_update_clusters(struct inode
*inode
,
125 struct ocfs2_extent_tree
*et
,
127 static int ocfs2_dinode_insert_check(struct inode
*inode
,
128 struct ocfs2_extent_tree
*et
,
129 struct ocfs2_extent_rec
*rec
);
130 static int ocfs2_dinode_sanity_check(struct inode
*inode
,
131 struct ocfs2_extent_tree
*et
);
132 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree
*et
);
133 static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops
= {
134 .eo_set_last_eb_blk
= ocfs2_dinode_set_last_eb_blk
,
135 .eo_get_last_eb_blk
= ocfs2_dinode_get_last_eb_blk
,
136 .eo_update_clusters
= ocfs2_dinode_update_clusters
,
137 .eo_insert_check
= ocfs2_dinode_insert_check
,
138 .eo_sanity_check
= ocfs2_dinode_sanity_check
,
139 .eo_fill_root_el
= ocfs2_dinode_fill_root_el
,
142 static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
145 struct ocfs2_dinode
*di
= et
->et_object
;
147 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
148 di
->i_last_eb_blk
= cpu_to_le64(blkno
);
151 static u64
ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
153 struct ocfs2_dinode
*di
= et
->et_object
;
155 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
156 return le64_to_cpu(di
->i_last_eb_blk
);
159 static void ocfs2_dinode_update_clusters(struct inode
*inode
,
160 struct ocfs2_extent_tree
*et
,
163 struct ocfs2_dinode
*di
= et
->et_object
;
165 le32_add_cpu(&di
->i_clusters
, clusters
);
166 spin_lock(&OCFS2_I(inode
)->ip_lock
);
167 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(di
->i_clusters
);
168 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
171 static int ocfs2_dinode_insert_check(struct inode
*inode
,
172 struct ocfs2_extent_tree
*et
,
173 struct ocfs2_extent_rec
*rec
)
175 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
177 BUG_ON(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
);
178 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb
) &&
179 (OCFS2_I(inode
)->ip_clusters
!=
180 le32_to_cpu(rec
->e_cpos
)),
181 "Device %s, asking for sparse allocation: inode %llu, "
182 "cpos %u, clusters %u\n",
184 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
186 OCFS2_I(inode
)->ip_clusters
);
191 static int ocfs2_dinode_sanity_check(struct inode
*inode
,
192 struct ocfs2_extent_tree
*et
)
194 struct ocfs2_dinode
*di
= et
->et_object
;
196 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
197 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
202 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree
*et
)
204 struct ocfs2_dinode
*di
= et
->et_object
;
206 et
->et_root_el
= &di
->id2
.i_list
;
210 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree
*et
)
212 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
214 et
->et_root_el
= &vb
->vb_xv
->xr_list
;
217 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
220 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
222 vb
->vb_xv
->xr_last_eb_blk
= cpu_to_le64(blkno
);
225 static u64
ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
227 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
229 return le64_to_cpu(vb
->vb_xv
->xr_last_eb_blk
);
232 static void ocfs2_xattr_value_update_clusters(struct inode
*inode
,
233 struct ocfs2_extent_tree
*et
,
236 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
238 le32_add_cpu(&vb
->vb_xv
->xr_clusters
, clusters
);
241 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops
= {
242 .eo_set_last_eb_blk
= ocfs2_xattr_value_set_last_eb_blk
,
243 .eo_get_last_eb_blk
= ocfs2_xattr_value_get_last_eb_blk
,
244 .eo_update_clusters
= ocfs2_xattr_value_update_clusters
,
245 .eo_fill_root_el
= ocfs2_xattr_value_fill_root_el
,
248 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree
*et
)
250 struct ocfs2_xattr_block
*xb
= et
->et_object
;
252 et
->et_root_el
= &xb
->xb_attrs
.xb_root
.xt_list
;
255 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode
*inode
,
256 struct ocfs2_extent_tree
*et
)
258 et
->et_max_leaf_clusters
=
259 ocfs2_clusters_for_bytes(inode
->i_sb
,
260 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
);
263 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
266 struct ocfs2_xattr_block
*xb
= et
->et_object
;
267 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
269 xt
->xt_last_eb_blk
= cpu_to_le64(blkno
);
272 static u64
ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
274 struct ocfs2_xattr_block
*xb
= et
->et_object
;
275 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
277 return le64_to_cpu(xt
->xt_last_eb_blk
);
280 static void ocfs2_xattr_tree_update_clusters(struct inode
*inode
,
281 struct ocfs2_extent_tree
*et
,
284 struct ocfs2_xattr_block
*xb
= et
->et_object
;
286 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, clusters
);
289 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops
= {
290 .eo_set_last_eb_blk
= ocfs2_xattr_tree_set_last_eb_blk
,
291 .eo_get_last_eb_blk
= ocfs2_xattr_tree_get_last_eb_blk
,
292 .eo_update_clusters
= ocfs2_xattr_tree_update_clusters
,
293 .eo_fill_root_el
= ocfs2_xattr_tree_fill_root_el
,
294 .eo_fill_max_leaf_clusters
= ocfs2_xattr_tree_fill_max_leaf_clusters
,
297 static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
300 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
302 dx_root
->dr_last_eb_blk
= cpu_to_le64(blkno
);
305 static u64
ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
307 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
309 return le64_to_cpu(dx_root
->dr_last_eb_blk
);
312 static void ocfs2_dx_root_update_clusters(struct inode
*inode
,
313 struct ocfs2_extent_tree
*et
,
316 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
318 le32_add_cpu(&dx_root
->dr_clusters
, clusters
);
321 static int ocfs2_dx_root_sanity_check(struct inode
*inode
,
322 struct ocfs2_extent_tree
*et
)
324 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
326 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root
));
331 static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree
*et
)
333 struct ocfs2_dx_root_block
*dx_root
= et
->et_object
;
335 et
->et_root_el
= &dx_root
->dr_list
;
338 static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops
= {
339 .eo_set_last_eb_blk
= ocfs2_dx_root_set_last_eb_blk
,
340 .eo_get_last_eb_blk
= ocfs2_dx_root_get_last_eb_blk
,
341 .eo_update_clusters
= ocfs2_dx_root_update_clusters
,
342 .eo_sanity_check
= ocfs2_dx_root_sanity_check
,
343 .eo_fill_root_el
= ocfs2_dx_root_fill_root_el
,
346 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree
*et
,
348 struct buffer_head
*bh
,
349 ocfs2_journal_access_func access
,
351 struct ocfs2_extent_tree_operations
*ops
)
355 et
->et_root_journal_access
= access
;
357 obj
= (void *)bh
->b_data
;
360 et
->et_ops
->eo_fill_root_el(et
);
361 if (!et
->et_ops
->eo_fill_max_leaf_clusters
)
362 et
->et_max_leaf_clusters
= 0;
364 et
->et_ops
->eo_fill_max_leaf_clusters(inode
, et
);
367 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree
*et
,
369 struct buffer_head
*bh
)
371 __ocfs2_init_extent_tree(et
, inode
, bh
, ocfs2_journal_access_di
,
372 NULL
, &ocfs2_dinode_et_ops
);
375 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree
*et
,
377 struct buffer_head
*bh
)
379 __ocfs2_init_extent_tree(et
, inode
, bh
, ocfs2_journal_access_xb
,
380 NULL
, &ocfs2_xattr_tree_et_ops
);
383 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree
*et
,
385 struct ocfs2_xattr_value_buf
*vb
)
387 __ocfs2_init_extent_tree(et
, inode
, vb
->vb_bh
, vb
->vb_access
, vb
,
388 &ocfs2_xattr_value_et_ops
);
391 void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree
*et
,
393 struct buffer_head
*bh
)
395 __ocfs2_init_extent_tree(et
, inode
, bh
, ocfs2_journal_access_dr
,
396 NULL
, &ocfs2_dx_root_et_ops
);
399 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
402 et
->et_ops
->eo_set_last_eb_blk(et
, new_last_eb_blk
);
405 static inline u64
ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
407 return et
->et_ops
->eo_get_last_eb_blk(et
);
410 static inline void ocfs2_et_update_clusters(struct inode
*inode
,
411 struct ocfs2_extent_tree
*et
,
414 et
->et_ops
->eo_update_clusters(inode
, et
, clusters
);
417 static inline int ocfs2_et_root_journal_access(handle_t
*handle
,
419 struct ocfs2_extent_tree
*et
,
422 return et
->et_root_journal_access(handle
, inode
, et
->et_root_bh
,
426 static inline int ocfs2_et_insert_check(struct inode
*inode
,
427 struct ocfs2_extent_tree
*et
,
428 struct ocfs2_extent_rec
*rec
)
432 if (et
->et_ops
->eo_insert_check
)
433 ret
= et
->et_ops
->eo_insert_check(inode
, et
, rec
);
437 static inline int ocfs2_et_sanity_check(struct inode
*inode
,
438 struct ocfs2_extent_tree
*et
)
442 if (et
->et_ops
->eo_sanity_check
)
443 ret
= et
->et_ops
->eo_sanity_check(inode
, et
);
447 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
);
448 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
449 struct ocfs2_extent_block
*eb
);
452 * Structures which describe a path through a btree, and functions to
455 * The idea here is to be as generic as possible with the tree
458 struct ocfs2_path_item
{
459 struct buffer_head
*bh
;
460 struct ocfs2_extent_list
*el
;
463 #define OCFS2_MAX_PATH_DEPTH 5
467 ocfs2_journal_access_func p_root_access
;
468 struct ocfs2_path_item p_node
[OCFS2_MAX_PATH_DEPTH
];
471 #define path_root_bh(_path) ((_path)->p_node[0].bh)
472 #define path_root_el(_path) ((_path)->p_node[0].el)
473 #define path_root_access(_path)((_path)->p_root_access)
474 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
475 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
476 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
479 * Reset the actual path elements so that we can re-use the structure
480 * to build another path. Generally, this involves freeing the buffer
483 static void ocfs2_reinit_path(struct ocfs2_path
*path
, int keep_root
)
485 int i
, start
= 0, depth
= 0;
486 struct ocfs2_path_item
*node
;
491 for(i
= start
; i
< path_num_items(path
); i
++) {
492 node
= &path
->p_node
[i
];
500 * Tree depth may change during truncate, or insert. If we're
501 * keeping the root extent list, then make sure that our path
502 * structure reflects the proper depth.
505 depth
= le16_to_cpu(path_root_el(path
)->l_tree_depth
);
507 path_root_access(path
) = NULL
;
509 path
->p_tree_depth
= depth
;
512 static void ocfs2_free_path(struct ocfs2_path
*path
)
515 ocfs2_reinit_path(path
, 0);
521 * All the elements of src into dest. After this call, src could be freed
522 * without affecting dest.
524 * Both paths should have the same root. Any non-root elements of dest
527 static void ocfs2_cp_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
531 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
532 BUG_ON(path_root_el(dest
) != path_root_el(src
));
533 BUG_ON(path_root_access(dest
) != path_root_access(src
));
535 ocfs2_reinit_path(dest
, 1);
537 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
538 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
539 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
541 if (dest
->p_node
[i
].bh
)
542 get_bh(dest
->p_node
[i
].bh
);
547 * Make the *dest path the same as src and re-initialize src path to
550 static void ocfs2_mv_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
554 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
555 BUG_ON(path_root_access(dest
) != path_root_access(src
));
557 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
558 brelse(dest
->p_node
[i
].bh
);
560 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
561 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
563 src
->p_node
[i
].bh
= NULL
;
564 src
->p_node
[i
].el
= NULL
;
569 * Insert an extent block at given index.
571 * This will not take an additional reference on eb_bh.
573 static inline void ocfs2_path_insert_eb(struct ocfs2_path
*path
, int index
,
574 struct buffer_head
*eb_bh
)
576 struct ocfs2_extent_block
*eb
= (struct ocfs2_extent_block
*)eb_bh
->b_data
;
579 * Right now, no root bh is an extent block, so this helps
580 * catch code errors with dinode trees. The assertion can be
581 * safely removed if we ever need to insert extent block
582 * structures at the root.
586 path
->p_node
[index
].bh
= eb_bh
;
587 path
->p_node
[index
].el
= &eb
->h_list
;
590 static struct ocfs2_path
*ocfs2_new_path(struct buffer_head
*root_bh
,
591 struct ocfs2_extent_list
*root_el
,
592 ocfs2_journal_access_func access
)
594 struct ocfs2_path
*path
;
596 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) >= OCFS2_MAX_PATH_DEPTH
);
598 path
= kzalloc(sizeof(*path
), GFP_NOFS
);
600 path
->p_tree_depth
= le16_to_cpu(root_el
->l_tree_depth
);
602 path_root_bh(path
) = root_bh
;
603 path_root_el(path
) = root_el
;
604 path_root_access(path
) = access
;
610 static struct ocfs2_path
*ocfs2_new_path_from_path(struct ocfs2_path
*path
)
612 return ocfs2_new_path(path_root_bh(path
), path_root_el(path
),
613 path_root_access(path
));
616 static struct ocfs2_path
*ocfs2_new_path_from_et(struct ocfs2_extent_tree
*et
)
618 return ocfs2_new_path(et
->et_root_bh
, et
->et_root_el
,
619 et
->et_root_journal_access
);
623 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
624 * otherwise it's the root_access function.
626 * I don't like the way this function's name looks next to
627 * ocfs2_journal_access_path(), but I don't have a better one.
629 static int ocfs2_path_bh_journal_access(handle_t
*handle
,
631 struct ocfs2_path
*path
,
634 ocfs2_journal_access_func access
= path_root_access(path
);
637 access
= ocfs2_journal_access
;
640 access
= ocfs2_journal_access_eb
;
642 return access(handle
, inode
, path
->p_node
[idx
].bh
,
643 OCFS2_JOURNAL_ACCESS_WRITE
);
647 * Convenience function to journal all components in a path.
649 static int ocfs2_journal_access_path(struct inode
*inode
, handle_t
*handle
,
650 struct ocfs2_path
*path
)
657 for(i
= 0; i
< path_num_items(path
); i
++) {
658 ret
= ocfs2_path_bh_journal_access(handle
, inode
, path
, i
);
670 * Return the index of the extent record which contains cluster #v_cluster.
671 * -1 is returned if it was not found.
673 * Should work fine on interior and exterior nodes.
675 int ocfs2_search_extent_list(struct ocfs2_extent_list
*el
, u32 v_cluster
)
679 struct ocfs2_extent_rec
*rec
;
680 u32 rec_end
, rec_start
, clusters
;
682 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
683 rec
= &el
->l_recs
[i
];
685 rec_start
= le32_to_cpu(rec
->e_cpos
);
686 clusters
= ocfs2_rec_clusters(el
, rec
);
688 rec_end
= rec_start
+ clusters
;
690 if (v_cluster
>= rec_start
&& v_cluster
< rec_end
) {
699 enum ocfs2_contig_type
{
708 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
709 * ocfs2_extent_contig only work properly against leaf nodes!
711 static int ocfs2_block_extent_contig(struct super_block
*sb
,
712 struct ocfs2_extent_rec
*ext
,
715 u64 blk_end
= le64_to_cpu(ext
->e_blkno
);
717 blk_end
+= ocfs2_clusters_to_blocks(sb
,
718 le16_to_cpu(ext
->e_leaf_clusters
));
720 return blkno
== blk_end
;
723 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec
*left
,
724 struct ocfs2_extent_rec
*right
)
728 left_range
= le32_to_cpu(left
->e_cpos
) +
729 le16_to_cpu(left
->e_leaf_clusters
);
731 return (left_range
== le32_to_cpu(right
->e_cpos
));
734 static enum ocfs2_contig_type
735 ocfs2_extent_contig(struct inode
*inode
,
736 struct ocfs2_extent_rec
*ext
,
737 struct ocfs2_extent_rec
*insert_rec
)
739 u64 blkno
= le64_to_cpu(insert_rec
->e_blkno
);
742 * Refuse to coalesce extent records with different flag
743 * fields - we don't want to mix unwritten extents with user
746 if (ext
->e_flags
!= insert_rec
->e_flags
)
749 if (ocfs2_extents_adjacent(ext
, insert_rec
) &&
750 ocfs2_block_extent_contig(inode
->i_sb
, ext
, blkno
))
753 blkno
= le64_to_cpu(ext
->e_blkno
);
754 if (ocfs2_extents_adjacent(insert_rec
, ext
) &&
755 ocfs2_block_extent_contig(inode
->i_sb
, insert_rec
, blkno
))
762 * NOTE: We can have pretty much any combination of contiguousness and
765 * The usefulness of APPEND_TAIL is more in that it lets us know that
766 * we'll have to update the path to that leaf.
768 enum ocfs2_append_type
{
773 enum ocfs2_split_type
{
779 struct ocfs2_insert_type
{
780 enum ocfs2_split_type ins_split
;
781 enum ocfs2_append_type ins_appending
;
782 enum ocfs2_contig_type ins_contig
;
783 int ins_contig_index
;
787 struct ocfs2_merge_ctxt
{
788 enum ocfs2_contig_type c_contig_type
;
789 int c_has_empty_extent
;
790 int c_split_covers_rec
;
793 static int ocfs2_validate_extent_block(struct super_block
*sb
,
794 struct buffer_head
*bh
)
797 struct ocfs2_extent_block
*eb
=
798 (struct ocfs2_extent_block
*)bh
->b_data
;
800 mlog(0, "Validating extent block %llu\n",
801 (unsigned long long)bh
->b_blocknr
);
803 BUG_ON(!buffer_uptodate(bh
));
806 * If the ecc fails, we return the error but otherwise
807 * leave the filesystem running. We know any error is
808 * local to this block.
810 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &eb
->h_check
);
812 mlog(ML_ERROR
, "Checksum failed for extent block %llu\n",
813 (unsigned long long)bh
->b_blocknr
);
818 * Errors after here are fatal.
821 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
823 "Extent block #%llu has bad signature %.*s",
824 (unsigned long long)bh
->b_blocknr
, 7,
829 if (le64_to_cpu(eb
->h_blkno
) != bh
->b_blocknr
) {
831 "Extent block #%llu has an invalid h_blkno "
833 (unsigned long long)bh
->b_blocknr
,
834 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
838 if (le32_to_cpu(eb
->h_fs_generation
) != OCFS2_SB(sb
)->fs_generation
) {
840 "Extent block #%llu has an invalid "
841 "h_fs_generation of #%u",
842 (unsigned long long)bh
->b_blocknr
,
843 le32_to_cpu(eb
->h_fs_generation
));
850 int ocfs2_read_extent_block(struct inode
*inode
, u64 eb_blkno
,
851 struct buffer_head
**bh
)
854 struct buffer_head
*tmp
= *bh
;
856 rc
= ocfs2_read_block(inode
, eb_blkno
, &tmp
,
857 ocfs2_validate_extent_block
);
859 /* If ocfs2_read_block() got us a new bh, pass it up. */
868 * How many free extents have we got before we need more meta data?
870 int ocfs2_num_free_extents(struct ocfs2_super
*osb
,
872 struct ocfs2_extent_tree
*et
)
875 struct ocfs2_extent_list
*el
= NULL
;
876 struct ocfs2_extent_block
*eb
;
877 struct buffer_head
*eb_bh
= NULL
;
883 last_eb_blk
= ocfs2_et_get_last_eb_blk(et
);
886 retval
= ocfs2_read_extent_block(inode
, last_eb_blk
, &eb_bh
);
891 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
895 BUG_ON(el
->l_tree_depth
!= 0);
897 retval
= le16_to_cpu(el
->l_count
) - le16_to_cpu(el
->l_next_free_rec
);
905 /* expects array to already be allocated
907 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
910 static int ocfs2_create_new_meta_bhs(struct ocfs2_super
*osb
,
914 struct ocfs2_alloc_context
*meta_ac
,
915 struct buffer_head
*bhs
[])
917 int count
, status
, i
;
918 u16 suballoc_bit_start
;
921 struct ocfs2_extent_block
*eb
;
926 while (count
< wanted
) {
927 status
= ocfs2_claim_metadata(osb
,
939 for(i
= count
; i
< (num_got
+ count
); i
++) {
940 bhs
[i
] = sb_getblk(osb
->sb
, first_blkno
);
941 if (bhs
[i
] == NULL
) {
946 ocfs2_set_new_buffer_uptodate(inode
, bhs
[i
]);
948 status
= ocfs2_journal_access_eb(handle
, inode
, bhs
[i
],
949 OCFS2_JOURNAL_ACCESS_CREATE
);
955 memset(bhs
[i
]->b_data
, 0, osb
->sb
->s_blocksize
);
956 eb
= (struct ocfs2_extent_block
*) bhs
[i
]->b_data
;
957 /* Ok, setup the minimal stuff here. */
958 strcpy(eb
->h_signature
, OCFS2_EXTENT_BLOCK_SIGNATURE
);
959 eb
->h_blkno
= cpu_to_le64(first_blkno
);
960 eb
->h_fs_generation
= cpu_to_le32(osb
->fs_generation
);
961 eb
->h_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
962 eb
->h_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
964 cpu_to_le16(ocfs2_extent_recs_per_eb(osb
->sb
));
966 suballoc_bit_start
++;
969 /* We'll also be dirtied by the caller, so
970 * this isn't absolutely necessary. */
971 status
= ocfs2_journal_dirty(handle
, bhs
[i
]);
984 for(i
= 0; i
< wanted
; i
++) {
994 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
996 * Returns the sum of the rightmost extent rec logical offset and
999 * ocfs2_add_branch() uses this to determine what logical cluster
1000 * value should be populated into the leftmost new branch records.
1002 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1003 * value for the new topmost tree record.
1005 static inline u32
ocfs2_sum_rightmost_rec(struct ocfs2_extent_list
*el
)
1009 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
1011 return le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
1012 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
1016 * Add an entire tree branch to our inode. eb_bh is the extent block
1017 * to start at, if we don't want to start the branch at the dinode
1020 * last_eb_bh is required as we have to update it's next_leaf pointer
1021 * for the new last extent block.
1023 * the new branch will be 'empty' in the sense that every block will
1024 * contain a single record with cluster count == 0.
1026 static int ocfs2_add_branch(struct ocfs2_super
*osb
,
1028 struct inode
*inode
,
1029 struct ocfs2_extent_tree
*et
,
1030 struct buffer_head
*eb_bh
,
1031 struct buffer_head
**last_eb_bh
,
1032 struct ocfs2_alloc_context
*meta_ac
)
1034 int status
, new_blocks
, i
;
1035 u64 next_blkno
, new_last_eb_blk
;
1036 struct buffer_head
*bh
;
1037 struct buffer_head
**new_eb_bhs
= NULL
;
1038 struct ocfs2_extent_block
*eb
;
1039 struct ocfs2_extent_list
*eb_el
;
1040 struct ocfs2_extent_list
*el
;
1045 BUG_ON(!last_eb_bh
|| !*last_eb_bh
);
1048 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
1051 el
= et
->et_root_el
;
1053 /* we never add a branch to a leaf. */
1054 BUG_ON(!el
->l_tree_depth
);
1056 new_blocks
= le16_to_cpu(el
->l_tree_depth
);
1058 /* allocate the number of new eb blocks we need */
1059 new_eb_bhs
= kcalloc(new_blocks
, sizeof(struct buffer_head
*),
1067 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, new_blocks
,
1068 meta_ac
, new_eb_bhs
);
1074 eb
= (struct ocfs2_extent_block
*)(*last_eb_bh
)->b_data
;
1075 new_cpos
= ocfs2_sum_rightmost_rec(&eb
->h_list
);
1077 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1078 * linked with the rest of the tree.
1079 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1081 * when we leave the loop, new_last_eb_blk will point to the
1082 * newest leaf, and next_blkno will point to the topmost extent
1084 next_blkno
= new_last_eb_blk
= 0;
1085 for(i
= 0; i
< new_blocks
; i
++) {
1087 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1088 /* ocfs2_create_new_meta_bhs() should create it right! */
1089 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
1090 eb_el
= &eb
->h_list
;
1092 status
= ocfs2_journal_access_eb(handle
, inode
, bh
,
1093 OCFS2_JOURNAL_ACCESS_CREATE
);
1099 eb
->h_next_leaf_blk
= 0;
1100 eb_el
->l_tree_depth
= cpu_to_le16(i
);
1101 eb_el
->l_next_free_rec
= cpu_to_le16(1);
1103 * This actually counts as an empty extent as
1106 eb_el
->l_recs
[0].e_cpos
= cpu_to_le32(new_cpos
);
1107 eb_el
->l_recs
[0].e_blkno
= cpu_to_le64(next_blkno
);
1109 * eb_el isn't always an interior node, but even leaf
1110 * nodes want a zero'd flags and reserved field so
1111 * this gets the whole 32 bits regardless of use.
1113 eb_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(0);
1114 if (!eb_el
->l_tree_depth
)
1115 new_last_eb_blk
= le64_to_cpu(eb
->h_blkno
);
1117 status
= ocfs2_journal_dirty(handle
, bh
);
1123 next_blkno
= le64_to_cpu(eb
->h_blkno
);
1126 /* This is a bit hairy. We want to update up to three blocks
1127 * here without leaving any of them in an inconsistent state
1128 * in case of error. We don't have to worry about
1129 * journal_dirty erroring as it won't unless we've aborted the
1130 * handle (in which case we would never be here) so reserving
1131 * the write with journal_access is all we need to do. */
1132 status
= ocfs2_journal_access_eb(handle
, inode
, *last_eb_bh
,
1133 OCFS2_JOURNAL_ACCESS_WRITE
);
1138 status
= ocfs2_et_root_journal_access(handle
, inode
, et
,
1139 OCFS2_JOURNAL_ACCESS_WRITE
);
1145 status
= ocfs2_journal_access_eb(handle
, inode
, eb_bh
,
1146 OCFS2_JOURNAL_ACCESS_WRITE
);
1153 /* Link the new branch into the rest of the tree (el will
1154 * either be on the root_bh, or the extent block passed in. */
1155 i
= le16_to_cpu(el
->l_next_free_rec
);
1156 el
->l_recs
[i
].e_blkno
= cpu_to_le64(next_blkno
);
1157 el
->l_recs
[i
].e_cpos
= cpu_to_le32(new_cpos
);
1158 el
->l_recs
[i
].e_int_clusters
= 0;
1159 le16_add_cpu(&el
->l_next_free_rec
, 1);
1161 /* fe needs a new last extent block pointer, as does the
1162 * next_leaf on the previously last-extent-block. */
1163 ocfs2_et_set_last_eb_blk(et
, new_last_eb_blk
);
1165 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
1166 eb
->h_next_leaf_blk
= cpu_to_le64(new_last_eb_blk
);
1168 status
= ocfs2_journal_dirty(handle
, *last_eb_bh
);
1171 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
1175 status
= ocfs2_journal_dirty(handle
, eb_bh
);
1181 * Some callers want to track the rightmost leaf so pass it
1184 brelse(*last_eb_bh
);
1185 get_bh(new_eb_bhs
[0]);
1186 *last_eb_bh
= new_eb_bhs
[0];
1191 for (i
= 0; i
< new_blocks
; i
++)
1192 brelse(new_eb_bhs
[i
]);
1201 * adds another level to the allocation tree.
1202 * returns back the new extent block so you can add a branch to it
1205 static int ocfs2_shift_tree_depth(struct ocfs2_super
*osb
,
1207 struct inode
*inode
,
1208 struct ocfs2_extent_tree
*et
,
1209 struct ocfs2_alloc_context
*meta_ac
,
1210 struct buffer_head
**ret_new_eb_bh
)
1214 struct buffer_head
*new_eb_bh
= NULL
;
1215 struct ocfs2_extent_block
*eb
;
1216 struct ocfs2_extent_list
*root_el
;
1217 struct ocfs2_extent_list
*eb_el
;
1221 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, 1, meta_ac
,
1228 eb
= (struct ocfs2_extent_block
*) new_eb_bh
->b_data
;
1229 /* ocfs2_create_new_meta_bhs() should create it right! */
1230 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
1232 eb_el
= &eb
->h_list
;
1233 root_el
= et
->et_root_el
;
1235 status
= ocfs2_journal_access_eb(handle
, inode
, new_eb_bh
,
1236 OCFS2_JOURNAL_ACCESS_CREATE
);
1242 /* copy the root extent list data into the new extent block */
1243 eb_el
->l_tree_depth
= root_el
->l_tree_depth
;
1244 eb_el
->l_next_free_rec
= root_el
->l_next_free_rec
;
1245 for (i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1246 eb_el
->l_recs
[i
] = root_el
->l_recs
[i
];
1248 status
= ocfs2_journal_dirty(handle
, new_eb_bh
);
1254 status
= ocfs2_et_root_journal_access(handle
, inode
, et
,
1255 OCFS2_JOURNAL_ACCESS_WRITE
);
1261 new_clusters
= ocfs2_sum_rightmost_rec(eb_el
);
1263 /* update root_bh now */
1264 le16_add_cpu(&root_el
->l_tree_depth
, 1);
1265 root_el
->l_recs
[0].e_cpos
= 0;
1266 root_el
->l_recs
[0].e_blkno
= eb
->h_blkno
;
1267 root_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(new_clusters
);
1268 for (i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1269 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
1270 root_el
->l_next_free_rec
= cpu_to_le16(1);
1272 /* If this is our 1st tree depth shift, then last_eb_blk
1273 * becomes the allocated extent block */
1274 if (root_el
->l_tree_depth
== cpu_to_le16(1))
1275 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
1277 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
1283 *ret_new_eb_bh
= new_eb_bh
;
1294 * Should only be called when there is no space left in any of the
1295 * leaf nodes. What we want to do is find the lowest tree depth
1296 * non-leaf extent block with room for new records. There are three
1297 * valid results of this search:
1299 * 1) a lowest extent block is found, then we pass it back in
1300 * *lowest_eb_bh and return '0'
1302 * 2) the search fails to find anything, but the root_el has room. We
1303 * pass NULL back in *lowest_eb_bh, but still return '0'
1305 * 3) the search fails to find anything AND the root_el is full, in
1306 * which case we return > 0
1308 * return status < 0 indicates an error.
1310 static int ocfs2_find_branch_target(struct ocfs2_super
*osb
,
1311 struct inode
*inode
,
1312 struct ocfs2_extent_tree
*et
,
1313 struct buffer_head
**target_bh
)
1317 struct ocfs2_extent_block
*eb
;
1318 struct ocfs2_extent_list
*el
;
1319 struct buffer_head
*bh
= NULL
;
1320 struct buffer_head
*lowest_bh
= NULL
;
1326 el
= et
->et_root_el
;
1328 while(le16_to_cpu(el
->l_tree_depth
) > 1) {
1329 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1330 ocfs2_error(inode
->i_sb
, "Dinode %llu has empty "
1331 "extent list (next_free_rec == 0)",
1332 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1336 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
1337 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1339 ocfs2_error(inode
->i_sb
, "Dinode %llu has extent "
1340 "list where extent # %d has no physical "
1342 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, i
);
1350 status
= ocfs2_read_extent_block(inode
, blkno
, &bh
);
1356 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1359 if (le16_to_cpu(el
->l_next_free_rec
) <
1360 le16_to_cpu(el
->l_count
)) {
1367 /* If we didn't find one and the fe doesn't have any room,
1368 * then return '1' */
1369 el
= et
->et_root_el
;
1370 if (!lowest_bh
&& (el
->l_next_free_rec
== el
->l_count
))
1373 *target_bh
= lowest_bh
;
1382 * Grow a b-tree so that it has more records.
1384 * We might shift the tree depth in which case existing paths should
1385 * be considered invalid.
1387 * Tree depth after the grow is returned via *final_depth.
1389 * *last_eb_bh will be updated by ocfs2_add_branch().
1391 static int ocfs2_grow_tree(struct inode
*inode
, handle_t
*handle
,
1392 struct ocfs2_extent_tree
*et
, int *final_depth
,
1393 struct buffer_head
**last_eb_bh
,
1394 struct ocfs2_alloc_context
*meta_ac
)
1397 struct ocfs2_extent_list
*el
= et
->et_root_el
;
1398 int depth
= le16_to_cpu(el
->l_tree_depth
);
1399 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1400 struct buffer_head
*bh
= NULL
;
1402 BUG_ON(meta_ac
== NULL
);
1404 shift
= ocfs2_find_branch_target(osb
, inode
, et
, &bh
);
1411 /* We traveled all the way to the bottom of the allocation tree
1412 * and didn't find room for any more extents - we need to add
1413 * another tree level */
1416 mlog(0, "need to shift tree depth (current = %d)\n", depth
);
1418 /* ocfs2_shift_tree_depth will return us a buffer with
1419 * the new extent block (so we can pass that to
1420 * ocfs2_add_branch). */
1421 ret
= ocfs2_shift_tree_depth(osb
, handle
, inode
, et
,
1430 * Special case: we have room now if we shifted from
1431 * tree_depth 0, so no more work needs to be done.
1433 * We won't be calling add_branch, so pass
1434 * back *last_eb_bh as the new leaf. At depth
1435 * zero, it should always be null so there's
1436 * no reason to brelse.
1438 BUG_ON(*last_eb_bh
);
1445 /* call ocfs2_add_branch to add the final part of the tree with
1447 mlog(0, "add branch. bh = %p\n", bh
);
1448 ret
= ocfs2_add_branch(osb
, handle
, inode
, et
, bh
, last_eb_bh
,
1457 *final_depth
= depth
;
1463 * This function will discard the rightmost extent record.
1465 static void ocfs2_shift_records_right(struct ocfs2_extent_list
*el
)
1467 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1468 int count
= le16_to_cpu(el
->l_count
);
1469 unsigned int num_bytes
;
1472 /* This will cause us to go off the end of our extent list. */
1473 BUG_ON(next_free
>= count
);
1475 num_bytes
= sizeof(struct ocfs2_extent_rec
) * next_free
;
1477 memmove(&el
->l_recs
[1], &el
->l_recs
[0], num_bytes
);
1480 static void ocfs2_rotate_leaf(struct ocfs2_extent_list
*el
,
1481 struct ocfs2_extent_rec
*insert_rec
)
1483 int i
, insert_index
, next_free
, has_empty
, num_bytes
;
1484 u32 insert_cpos
= le32_to_cpu(insert_rec
->e_cpos
);
1485 struct ocfs2_extent_rec
*rec
;
1487 next_free
= le16_to_cpu(el
->l_next_free_rec
);
1488 has_empty
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
1492 /* The tree code before us didn't allow enough room in the leaf. */
1493 BUG_ON(el
->l_next_free_rec
== el
->l_count
&& !has_empty
);
1496 * The easiest way to approach this is to just remove the
1497 * empty extent and temporarily decrement next_free.
1501 * If next_free was 1 (only an empty extent), this
1502 * loop won't execute, which is fine. We still want
1503 * the decrement above to happen.
1505 for(i
= 0; i
< (next_free
- 1); i
++)
1506 el
->l_recs
[i
] = el
->l_recs
[i
+1];
1512 * Figure out what the new record index should be.
1514 for(i
= 0; i
< next_free
; i
++) {
1515 rec
= &el
->l_recs
[i
];
1517 if (insert_cpos
< le32_to_cpu(rec
->e_cpos
))
1522 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1523 insert_cpos
, insert_index
, has_empty
, next_free
, le16_to_cpu(el
->l_count
));
1525 BUG_ON(insert_index
< 0);
1526 BUG_ON(insert_index
>= le16_to_cpu(el
->l_count
));
1527 BUG_ON(insert_index
> next_free
);
1530 * No need to memmove if we're just adding to the tail.
1532 if (insert_index
!= next_free
) {
1533 BUG_ON(next_free
>= le16_to_cpu(el
->l_count
));
1535 num_bytes
= next_free
- insert_index
;
1536 num_bytes
*= sizeof(struct ocfs2_extent_rec
);
1537 memmove(&el
->l_recs
[insert_index
+ 1],
1538 &el
->l_recs
[insert_index
],
1543 * Either we had an empty extent, and need to re-increment or
1544 * there was no empty extent on a non full rightmost leaf node,
1545 * in which case we still need to increment.
1548 el
->l_next_free_rec
= cpu_to_le16(next_free
);
1550 * Make sure none of the math above just messed up our tree.
1552 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) > le16_to_cpu(el
->l_count
));
1554 el
->l_recs
[insert_index
] = *insert_rec
;
1558 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list
*el
)
1560 int size
, num_recs
= le16_to_cpu(el
->l_next_free_rec
);
1562 BUG_ON(num_recs
== 0);
1564 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
1566 size
= num_recs
* sizeof(struct ocfs2_extent_rec
);
1567 memmove(&el
->l_recs
[0], &el
->l_recs
[1], size
);
1568 memset(&el
->l_recs
[num_recs
], 0,
1569 sizeof(struct ocfs2_extent_rec
));
1570 el
->l_next_free_rec
= cpu_to_le16(num_recs
);
1575 * Create an empty extent record .
1577 * l_next_free_rec may be updated.
1579 * If an empty extent already exists do nothing.
1581 static void ocfs2_create_empty_extent(struct ocfs2_extent_list
*el
)
1583 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1585 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
1590 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
1593 mlog_bug_on_msg(el
->l_count
== el
->l_next_free_rec
,
1594 "Asked to create an empty extent in a full list:\n"
1595 "count = %u, tree depth = %u",
1596 le16_to_cpu(el
->l_count
),
1597 le16_to_cpu(el
->l_tree_depth
));
1599 ocfs2_shift_records_right(el
);
1602 le16_add_cpu(&el
->l_next_free_rec
, 1);
1603 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
1607 * For a rotation which involves two leaf nodes, the "root node" is
1608 * the lowest level tree node which contains a path to both leafs. This
1609 * resulting set of information can be used to form a complete "subtree"
1611 * This function is passed two full paths from the dinode down to a
1612 * pair of adjacent leaves. It's task is to figure out which path
1613 * index contains the subtree root - this can be the root index itself
1614 * in a worst-case rotation.
1616 * The array index of the subtree root is passed back.
1618 static int ocfs2_find_subtree_root(struct inode
*inode
,
1619 struct ocfs2_path
*left
,
1620 struct ocfs2_path
*right
)
1625 * Check that the caller passed in two paths from the same tree.
1627 BUG_ON(path_root_bh(left
) != path_root_bh(right
));
1633 * The caller didn't pass two adjacent paths.
1635 mlog_bug_on_msg(i
> left
->p_tree_depth
,
1636 "Inode %lu, left depth %u, right depth %u\n"
1637 "left leaf blk %llu, right leaf blk %llu\n",
1638 inode
->i_ino
, left
->p_tree_depth
,
1639 right
->p_tree_depth
,
1640 (unsigned long long)path_leaf_bh(left
)->b_blocknr
,
1641 (unsigned long long)path_leaf_bh(right
)->b_blocknr
);
1642 } while (left
->p_node
[i
].bh
->b_blocknr
==
1643 right
->p_node
[i
].bh
->b_blocknr
);
1648 typedef void (path_insert_t
)(void *, struct buffer_head
*);
1651 * Traverse a btree path in search of cpos, starting at root_el.
1653 * This code can be called with a cpos larger than the tree, in which
1654 * case it will return the rightmost path.
1656 static int __ocfs2_find_path(struct inode
*inode
,
1657 struct ocfs2_extent_list
*root_el
, u32 cpos
,
1658 path_insert_t
*func
, void *data
)
1663 struct buffer_head
*bh
= NULL
;
1664 struct ocfs2_extent_block
*eb
;
1665 struct ocfs2_extent_list
*el
;
1666 struct ocfs2_extent_rec
*rec
;
1667 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1670 while (el
->l_tree_depth
) {
1671 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1672 ocfs2_error(inode
->i_sb
,
1673 "Inode %llu has empty extent list at "
1675 (unsigned long long)oi
->ip_blkno
,
1676 le16_to_cpu(el
->l_tree_depth
));
1682 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
) - 1; i
++) {
1683 rec
= &el
->l_recs
[i
];
1686 * In the case that cpos is off the allocation
1687 * tree, this should just wind up returning the
1690 range
= le32_to_cpu(rec
->e_cpos
) +
1691 ocfs2_rec_clusters(el
, rec
);
1692 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
1696 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1698 ocfs2_error(inode
->i_sb
,
1699 "Inode %llu has bad blkno in extent list "
1700 "at depth %u (index %d)\n",
1701 (unsigned long long)oi
->ip_blkno
,
1702 le16_to_cpu(el
->l_tree_depth
), i
);
1709 ret
= ocfs2_read_extent_block(inode
, blkno
, &bh
);
1715 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1718 if (le16_to_cpu(el
->l_next_free_rec
) >
1719 le16_to_cpu(el
->l_count
)) {
1720 ocfs2_error(inode
->i_sb
,
1721 "Inode %llu has bad count in extent list "
1722 "at block %llu (next free=%u, count=%u)\n",
1723 (unsigned long long)oi
->ip_blkno
,
1724 (unsigned long long)bh
->b_blocknr
,
1725 le16_to_cpu(el
->l_next_free_rec
),
1726 le16_to_cpu(el
->l_count
));
1737 * Catch any trailing bh that the loop didn't handle.
1745 * Given an initialized path (that is, it has a valid root extent
1746 * list), this function will traverse the btree in search of the path
1747 * which would contain cpos.
1749 * The path traveled is recorded in the path structure.
1751 * Note that this will not do any comparisons on leaf node extent
1752 * records, so it will work fine in the case that we just added a tree
1755 struct find_path_data
{
1757 struct ocfs2_path
*path
;
1759 static void find_path_ins(void *data
, struct buffer_head
*bh
)
1761 struct find_path_data
*fp
= data
;
1764 ocfs2_path_insert_eb(fp
->path
, fp
->index
, bh
);
1767 static int ocfs2_find_path(struct inode
*inode
, struct ocfs2_path
*path
,
1770 struct find_path_data data
;
1774 return __ocfs2_find_path(inode
, path_root_el(path
), cpos
,
1775 find_path_ins
, &data
);
1778 static void find_leaf_ins(void *data
, struct buffer_head
*bh
)
1780 struct ocfs2_extent_block
*eb
=(struct ocfs2_extent_block
*)bh
->b_data
;
1781 struct ocfs2_extent_list
*el
= &eb
->h_list
;
1782 struct buffer_head
**ret
= data
;
1784 /* We want to retain only the leaf block. */
1785 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
1791 * Find the leaf block in the tree which would contain cpos. No
1792 * checking of the actual leaf is done.
1794 * Some paths want to call this instead of allocating a path structure
1795 * and calling ocfs2_find_path().
1797 * This function doesn't handle non btree extent lists.
1799 int ocfs2_find_leaf(struct inode
*inode
, struct ocfs2_extent_list
*root_el
,
1800 u32 cpos
, struct buffer_head
**leaf_bh
)
1803 struct buffer_head
*bh
= NULL
;
1805 ret
= __ocfs2_find_path(inode
, root_el
, cpos
, find_leaf_ins
, &bh
);
1817 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1819 * Basically, we've moved stuff around at the bottom of the tree and
1820 * we need to fix up the extent records above the changes to reflect
1823 * left_rec: the record on the left.
1824 * left_child_el: is the child list pointed to by left_rec
1825 * right_rec: the record to the right of left_rec
1826 * right_child_el: is the child list pointed to by right_rec
1828 * By definition, this only works on interior nodes.
1830 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec
*left_rec
,
1831 struct ocfs2_extent_list
*left_child_el
,
1832 struct ocfs2_extent_rec
*right_rec
,
1833 struct ocfs2_extent_list
*right_child_el
)
1835 u32 left_clusters
, right_end
;
1838 * Interior nodes never have holes. Their cpos is the cpos of
1839 * the leftmost record in their child list. Their cluster
1840 * count covers the full theoretical range of their child list
1841 * - the range between their cpos and the cpos of the record
1842 * immediately to their right.
1844 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[0].e_cpos
);
1845 if (ocfs2_is_empty_extent(&right_child_el
->l_recs
[0])) {
1846 BUG_ON(le16_to_cpu(right_child_el
->l_next_free_rec
) <= 1);
1847 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[1].e_cpos
);
1849 left_clusters
-= le32_to_cpu(left_rec
->e_cpos
);
1850 left_rec
->e_int_clusters
= cpu_to_le32(left_clusters
);
1853 * Calculate the rightmost cluster count boundary before
1854 * moving cpos - we will need to adjust clusters after
1855 * updating e_cpos to keep the same highest cluster count.
1857 right_end
= le32_to_cpu(right_rec
->e_cpos
);
1858 right_end
+= le32_to_cpu(right_rec
->e_int_clusters
);
1860 right_rec
->e_cpos
= left_rec
->e_cpos
;
1861 le32_add_cpu(&right_rec
->e_cpos
, left_clusters
);
1863 right_end
-= le32_to_cpu(right_rec
->e_cpos
);
1864 right_rec
->e_int_clusters
= cpu_to_le32(right_end
);
1868 * Adjust the adjacent root node records involved in a
1869 * rotation. left_el_blkno is passed in as a key so that we can easily
1870 * find it's index in the root list.
1872 static void ocfs2_adjust_root_records(struct ocfs2_extent_list
*root_el
,
1873 struct ocfs2_extent_list
*left_el
,
1874 struct ocfs2_extent_list
*right_el
,
1879 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) <=
1880 le16_to_cpu(left_el
->l_tree_depth
));
1882 for(i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
) - 1; i
++) {
1883 if (le64_to_cpu(root_el
->l_recs
[i
].e_blkno
) == left_el_blkno
)
1888 * The path walking code should have never returned a root and
1889 * two paths which are not adjacent.
1891 BUG_ON(i
>= (le16_to_cpu(root_el
->l_next_free_rec
) - 1));
1893 ocfs2_adjust_adjacent_records(&root_el
->l_recs
[i
], left_el
,
1894 &root_el
->l_recs
[i
+ 1], right_el
);
1898 * We've changed a leaf block (in right_path) and need to reflect that
1899 * change back up the subtree.
1901 * This happens in multiple places:
1902 * - When we've moved an extent record from the left path leaf to the right
1903 * path leaf to make room for an empty extent in the left path leaf.
1904 * - When our insert into the right path leaf is at the leftmost edge
1905 * and requires an update of the path immediately to it's left. This
1906 * can occur at the end of some types of rotation and appending inserts.
1907 * - When we've adjusted the last extent record in the left path leaf and the
1908 * 1st extent record in the right path leaf during cross extent block merge.
1910 static void ocfs2_complete_edge_insert(struct inode
*inode
, handle_t
*handle
,
1911 struct ocfs2_path
*left_path
,
1912 struct ocfs2_path
*right_path
,
1916 struct ocfs2_extent_list
*el
, *left_el
, *right_el
;
1917 struct ocfs2_extent_rec
*left_rec
, *right_rec
;
1918 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
1921 * Update the counts and position values within all the
1922 * interior nodes to reflect the leaf rotation we just did.
1924 * The root node is handled below the loop.
1926 * We begin the loop with right_el and left_el pointing to the
1927 * leaf lists and work our way up.
1929 * NOTE: within this loop, left_el and right_el always refer
1930 * to the *child* lists.
1932 left_el
= path_leaf_el(left_path
);
1933 right_el
= path_leaf_el(right_path
);
1934 for(i
= left_path
->p_tree_depth
- 1; i
> subtree_index
; i
--) {
1935 mlog(0, "Adjust records at index %u\n", i
);
1938 * One nice property of knowing that all of these
1939 * nodes are below the root is that we only deal with
1940 * the leftmost right node record and the rightmost
1943 el
= left_path
->p_node
[i
].el
;
1944 idx
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
1945 left_rec
= &el
->l_recs
[idx
];
1947 el
= right_path
->p_node
[i
].el
;
1948 right_rec
= &el
->l_recs
[0];
1950 ocfs2_adjust_adjacent_records(left_rec
, left_el
, right_rec
,
1953 ret
= ocfs2_journal_dirty(handle
, left_path
->p_node
[i
].bh
);
1957 ret
= ocfs2_journal_dirty(handle
, right_path
->p_node
[i
].bh
);
1962 * Setup our list pointers now so that the current
1963 * parents become children in the next iteration.
1965 left_el
= left_path
->p_node
[i
].el
;
1966 right_el
= right_path
->p_node
[i
].el
;
1970 * At the root node, adjust the two adjacent records which
1971 * begin our path to the leaves.
1974 el
= left_path
->p_node
[subtree_index
].el
;
1975 left_el
= left_path
->p_node
[subtree_index
+ 1].el
;
1976 right_el
= right_path
->p_node
[subtree_index
+ 1].el
;
1978 ocfs2_adjust_root_records(el
, left_el
, right_el
,
1979 left_path
->p_node
[subtree_index
+ 1].bh
->b_blocknr
);
1981 root_bh
= left_path
->p_node
[subtree_index
].bh
;
1983 ret
= ocfs2_journal_dirty(handle
, root_bh
);
1988 static int ocfs2_rotate_subtree_right(struct inode
*inode
,
1990 struct ocfs2_path
*left_path
,
1991 struct ocfs2_path
*right_path
,
1995 struct buffer_head
*right_leaf_bh
;
1996 struct buffer_head
*left_leaf_bh
= NULL
;
1997 struct buffer_head
*root_bh
;
1998 struct ocfs2_extent_list
*right_el
, *left_el
;
1999 struct ocfs2_extent_rec move_rec
;
2001 left_leaf_bh
= path_leaf_bh(left_path
);
2002 left_el
= path_leaf_el(left_path
);
2004 if (left_el
->l_next_free_rec
!= left_el
->l_count
) {
2005 ocfs2_error(inode
->i_sb
,
2006 "Inode %llu has non-full interior leaf node %llu"
2008 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2009 (unsigned long long)left_leaf_bh
->b_blocknr
,
2010 le16_to_cpu(left_el
->l_next_free_rec
));
2015 * This extent block may already have an empty record, so we
2016 * return early if so.
2018 if (ocfs2_is_empty_extent(&left_el
->l_recs
[0]))
2021 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2022 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2024 ret
= ocfs2_path_bh_journal_access(handle
, inode
, right_path
,
2031 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
2032 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
2039 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
2047 right_leaf_bh
= path_leaf_bh(right_path
);
2048 right_el
= path_leaf_el(right_path
);
2050 /* This is a code error, not a disk corruption. */
2051 mlog_bug_on_msg(!right_el
->l_next_free_rec
, "Inode %llu: Rotate fails "
2052 "because rightmost leaf block %llu is empty\n",
2053 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2054 (unsigned long long)right_leaf_bh
->b_blocknr
);
2056 ocfs2_create_empty_extent(right_el
);
2058 ret
= ocfs2_journal_dirty(handle
, right_leaf_bh
);
2064 /* Do the copy now. */
2065 i
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
2066 move_rec
= left_el
->l_recs
[i
];
2067 right_el
->l_recs
[0] = move_rec
;
2070 * Clear out the record we just copied and shift everything
2071 * over, leaving an empty extent in the left leaf.
2073 * We temporarily subtract from next_free_rec so that the
2074 * shift will lose the tail record (which is now defunct).
2076 le16_add_cpu(&left_el
->l_next_free_rec
, -1);
2077 ocfs2_shift_records_right(left_el
);
2078 memset(&left_el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2079 le16_add_cpu(&left_el
->l_next_free_rec
, 1);
2081 ret
= ocfs2_journal_dirty(handle
, left_leaf_bh
);
2087 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
2095 * Given a full path, determine what cpos value would return us a path
2096 * containing the leaf immediately to the left of the current one.
2098 * Will return zero if the path passed in is already the leftmost path.
2100 static int ocfs2_find_cpos_for_left_leaf(struct super_block
*sb
,
2101 struct ocfs2_path
*path
, u32
*cpos
)
2105 struct ocfs2_extent_list
*el
;
2107 BUG_ON(path
->p_tree_depth
== 0);
2111 blkno
= path_leaf_bh(path
)->b_blocknr
;
2113 /* Start at the tree node just above the leaf and work our way up. */
2114 i
= path
->p_tree_depth
- 1;
2116 el
= path
->p_node
[i
].el
;
2119 * Find the extent record just before the one in our
2122 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
2123 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
2127 * We've determined that the
2128 * path specified is already
2129 * the leftmost one - return a
2135 * The leftmost record points to our
2136 * leaf - we need to travel up the
2142 *cpos
= le32_to_cpu(el
->l_recs
[j
- 1].e_cpos
);
2143 *cpos
= *cpos
+ ocfs2_rec_clusters(el
,
2144 &el
->l_recs
[j
- 1]);
2151 * If we got here, we never found a valid node where
2152 * the tree indicated one should be.
2155 "Invalid extent tree at extent block %llu\n",
2156 (unsigned long long)blkno
);
2161 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2170 * Extend the transaction by enough credits to complete the rotation,
2171 * and still leave at least the original number of credits allocated
2172 * to this transaction.
2174 static int ocfs2_extend_rotate_transaction(handle_t
*handle
, int subtree_depth
,
2176 struct ocfs2_path
*path
)
2178 int credits
= (path
->p_tree_depth
- subtree_depth
) * 2 + 1 + op_credits
;
2180 if (handle
->h_buffer_credits
< credits
)
2181 return ocfs2_extend_trans(handle
, credits
);
2187 * Trap the case where we're inserting into the theoretical range past
2188 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2189 * whose cpos is less than ours into the right leaf.
2191 * It's only necessary to look at the rightmost record of the left
2192 * leaf because the logic that calls us should ensure that the
2193 * theoretical ranges in the path components above the leaves are
2196 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path
*left_path
,
2199 struct ocfs2_extent_list
*left_el
;
2200 struct ocfs2_extent_rec
*rec
;
2203 left_el
= path_leaf_el(left_path
);
2204 next_free
= le16_to_cpu(left_el
->l_next_free_rec
);
2205 rec
= &left_el
->l_recs
[next_free
- 1];
2207 if (insert_cpos
> le32_to_cpu(rec
->e_cpos
))
2212 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list
*el
, u32 cpos
)
2214 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
2216 struct ocfs2_extent_rec
*rec
;
2221 rec
= &el
->l_recs
[0];
2222 if (ocfs2_is_empty_extent(rec
)) {
2226 rec
= &el
->l_recs
[1];
2229 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2230 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
2236 * Rotate all the records in a btree right one record, starting at insert_cpos.
2238 * The path to the rightmost leaf should be passed in.
2240 * The array is assumed to be large enough to hold an entire path (tree depth).
2242 * Upon succesful return from this function:
2244 * - The 'right_path' array will contain a path to the leaf block
2245 * whose range contains e_cpos.
2246 * - That leaf block will have a single empty extent in list index 0.
2247 * - In the case that the rotation requires a post-insert update,
2248 * *ret_left_path will contain a valid path which can be passed to
2249 * ocfs2_insert_path().
2251 static int ocfs2_rotate_tree_right(struct inode
*inode
,
2253 enum ocfs2_split_type split
,
2255 struct ocfs2_path
*right_path
,
2256 struct ocfs2_path
**ret_left_path
)
2258 int ret
, start
, orig_credits
= handle
->h_buffer_credits
;
2260 struct ocfs2_path
*left_path
= NULL
;
2262 *ret_left_path
= NULL
;
2264 left_path
= ocfs2_new_path_from_path(right_path
);
2271 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
, &cpos
);
2277 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos
, cpos
);
2280 * What we want to do here is:
2282 * 1) Start with the rightmost path.
2284 * 2) Determine a path to the leaf block directly to the left
2287 * 3) Determine the 'subtree root' - the lowest level tree node
2288 * which contains a path to both leaves.
2290 * 4) Rotate the subtree.
2292 * 5) Find the next subtree by considering the left path to be
2293 * the new right path.
2295 * The check at the top of this while loop also accepts
2296 * insert_cpos == cpos because cpos is only a _theoretical_
2297 * value to get us the left path - insert_cpos might very well
2298 * be filling that hole.
2300 * Stop at a cpos of '0' because we either started at the
2301 * leftmost branch (i.e., a tree with one branch and a
2302 * rotation inside of it), or we've gone as far as we can in
2303 * rotating subtrees.
2305 while (cpos
&& insert_cpos
<= cpos
) {
2306 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2309 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
2315 mlog_bug_on_msg(path_leaf_bh(left_path
) ==
2316 path_leaf_bh(right_path
),
2317 "Inode %lu: error during insert of %u "
2318 "(left path cpos %u) results in two identical "
2319 "paths ending at %llu\n",
2320 inode
->i_ino
, insert_cpos
, cpos
,
2321 (unsigned long long)
2322 path_leaf_bh(left_path
)->b_blocknr
);
2324 if (split
== SPLIT_NONE
&&
2325 ocfs2_rotate_requires_path_adjustment(left_path
,
2329 * We've rotated the tree as much as we
2330 * should. The rest is up to
2331 * ocfs2_insert_path() to complete, after the
2332 * record insertion. We indicate this
2333 * situation by returning the left path.
2335 * The reason we don't adjust the records here
2336 * before the record insert is that an error
2337 * later might break the rule where a parent
2338 * record e_cpos will reflect the actual
2339 * e_cpos of the 1st nonempty record of the
2342 *ret_left_path
= left_path
;
2346 start
= ocfs2_find_subtree_root(inode
, left_path
, right_path
);
2348 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2350 (unsigned long long) right_path
->p_node
[start
].bh
->b_blocknr
,
2351 right_path
->p_tree_depth
);
2353 ret
= ocfs2_extend_rotate_transaction(handle
, start
,
2354 orig_credits
, right_path
);
2360 ret
= ocfs2_rotate_subtree_right(inode
, handle
, left_path
,
2367 if (split
!= SPLIT_NONE
&&
2368 ocfs2_leftmost_rec_contains(path_leaf_el(right_path
),
2371 * A rotate moves the rightmost left leaf
2372 * record over to the leftmost right leaf
2373 * slot. If we're doing an extent split
2374 * instead of a real insert, then we have to
2375 * check that the extent to be split wasn't
2376 * just moved over. If it was, then we can
2377 * exit here, passing left_path back -
2378 * ocfs2_split_extent() is smart enough to
2379 * search both leaves.
2381 *ret_left_path
= left_path
;
2386 * There is no need to re-read the next right path
2387 * as we know that it'll be our current left
2388 * path. Optimize by copying values instead.
2390 ocfs2_mv_path(right_path
, left_path
);
2392 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
2401 ocfs2_free_path(left_path
);
2407 static void ocfs2_update_edge_lengths(struct inode
*inode
, handle_t
*handle
,
2408 struct ocfs2_path
*path
)
2411 struct ocfs2_extent_rec
*rec
;
2412 struct ocfs2_extent_list
*el
;
2413 struct ocfs2_extent_block
*eb
;
2416 /* Path should always be rightmost. */
2417 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
2418 BUG_ON(eb
->h_next_leaf_blk
!= 0ULL);
2421 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
2422 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2423 rec
= &el
->l_recs
[idx
];
2424 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2426 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
2427 el
= path
->p_node
[i
].el
;
2428 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2429 rec
= &el
->l_recs
[idx
];
2431 rec
->e_int_clusters
= cpu_to_le32(range
);
2432 le32_add_cpu(&rec
->e_int_clusters
, -le32_to_cpu(rec
->e_cpos
));
2434 ocfs2_journal_dirty(handle
, path
->p_node
[i
].bh
);
2438 static void ocfs2_unlink_path(struct inode
*inode
, handle_t
*handle
,
2439 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2440 struct ocfs2_path
*path
, int unlink_start
)
2443 struct ocfs2_extent_block
*eb
;
2444 struct ocfs2_extent_list
*el
;
2445 struct buffer_head
*bh
;
2447 for(i
= unlink_start
; i
< path_num_items(path
); i
++) {
2448 bh
= path
->p_node
[i
].bh
;
2450 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
2452 * Not all nodes might have had their final count
2453 * decremented by the caller - handle this here.
2456 if (le16_to_cpu(el
->l_next_free_rec
) > 1) {
2458 "Inode %llu, attempted to remove extent block "
2459 "%llu with %u records\n",
2460 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2461 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
2462 le16_to_cpu(el
->l_next_free_rec
));
2464 ocfs2_journal_dirty(handle
, bh
);
2465 ocfs2_remove_from_cache(inode
, bh
);
2469 el
->l_next_free_rec
= 0;
2470 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2472 ocfs2_journal_dirty(handle
, bh
);
2474 ret
= ocfs2_cache_extent_block_free(dealloc
, eb
);
2478 ocfs2_remove_from_cache(inode
, bh
);
2482 static void ocfs2_unlink_subtree(struct inode
*inode
, handle_t
*handle
,
2483 struct ocfs2_path
*left_path
,
2484 struct ocfs2_path
*right_path
,
2486 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2489 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
2490 struct ocfs2_extent_list
*root_el
= left_path
->p_node
[subtree_index
].el
;
2491 struct ocfs2_extent_list
*el
;
2492 struct ocfs2_extent_block
*eb
;
2494 el
= path_leaf_el(left_path
);
2496 eb
= (struct ocfs2_extent_block
*)right_path
->p_node
[subtree_index
+ 1].bh
->b_data
;
2498 for(i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
2499 if (root_el
->l_recs
[i
].e_blkno
== eb
->h_blkno
)
2502 BUG_ON(i
>= le16_to_cpu(root_el
->l_next_free_rec
));
2504 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
2505 le16_add_cpu(&root_el
->l_next_free_rec
, -1);
2507 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2508 eb
->h_next_leaf_blk
= 0;
2510 ocfs2_journal_dirty(handle
, root_bh
);
2511 ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2513 ocfs2_unlink_path(inode
, handle
, dealloc
, right_path
,
2517 static int ocfs2_rotate_subtree_left(struct inode
*inode
, handle_t
*handle
,
2518 struct ocfs2_path
*left_path
,
2519 struct ocfs2_path
*right_path
,
2521 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2523 struct ocfs2_extent_tree
*et
)
2525 int ret
, i
, del_right_subtree
= 0, right_has_empty
= 0;
2526 struct buffer_head
*root_bh
, *et_root_bh
= path_root_bh(right_path
);
2527 struct ocfs2_extent_list
*right_leaf_el
, *left_leaf_el
;
2528 struct ocfs2_extent_block
*eb
;
2532 right_leaf_el
= path_leaf_el(right_path
);
2533 left_leaf_el
= path_leaf_el(left_path
);
2534 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2535 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2537 if (!ocfs2_is_empty_extent(&left_leaf_el
->l_recs
[0]))
2540 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(right_path
)->b_data
;
2541 if (ocfs2_is_empty_extent(&right_leaf_el
->l_recs
[0])) {
2543 * It's legal for us to proceed if the right leaf is
2544 * the rightmost one and it has an empty extent. There
2545 * are two cases to handle - whether the leaf will be
2546 * empty after removal or not. If the leaf isn't empty
2547 * then just remove the empty extent up front. The
2548 * next block will handle empty leaves by flagging
2551 * Non rightmost leaves will throw -EAGAIN and the
2552 * caller can manually move the subtree and retry.
2555 if (eb
->h_next_leaf_blk
!= 0ULL)
2558 if (le16_to_cpu(right_leaf_el
->l_next_free_rec
) > 1) {
2559 ret
= ocfs2_journal_access_eb(handle
, inode
,
2560 path_leaf_bh(right_path
),
2561 OCFS2_JOURNAL_ACCESS_WRITE
);
2567 ocfs2_remove_empty_extent(right_leaf_el
);
2569 right_has_empty
= 1;
2572 if (eb
->h_next_leaf_blk
== 0ULL &&
2573 le16_to_cpu(right_leaf_el
->l_next_free_rec
) == 1) {
2575 * We have to update i_last_eb_blk during the meta
2578 ret
= ocfs2_et_root_journal_access(handle
, inode
, et
,
2579 OCFS2_JOURNAL_ACCESS_WRITE
);
2585 del_right_subtree
= 1;
2589 * Getting here with an empty extent in the right path implies
2590 * that it's the rightmost path and will be deleted.
2592 BUG_ON(right_has_empty
&& !del_right_subtree
);
2594 ret
= ocfs2_path_bh_journal_access(handle
, inode
, right_path
,
2601 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
2602 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
2609 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
2617 if (!right_has_empty
) {
2619 * Only do this if we're moving a real
2620 * record. Otherwise, the action is delayed until
2621 * after removal of the right path in which case we
2622 * can do a simple shift to remove the empty extent.
2624 ocfs2_rotate_leaf(left_leaf_el
, &right_leaf_el
->l_recs
[0]);
2625 memset(&right_leaf_el
->l_recs
[0], 0,
2626 sizeof(struct ocfs2_extent_rec
));
2628 if (eb
->h_next_leaf_blk
== 0ULL) {
2630 * Move recs over to get rid of empty extent, decrease
2631 * next_free. This is allowed to remove the last
2632 * extent in our leaf (setting l_next_free_rec to
2633 * zero) - the delete code below won't care.
2635 ocfs2_remove_empty_extent(right_leaf_el
);
2638 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2641 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
2645 if (del_right_subtree
) {
2646 ocfs2_unlink_subtree(inode
, handle
, left_path
, right_path
,
2647 subtree_index
, dealloc
);
2648 ocfs2_update_edge_lengths(inode
, handle
, left_path
);
2650 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2651 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
2654 * Removal of the extent in the left leaf was skipped
2655 * above so we could delete the right path
2658 if (right_has_empty
)
2659 ocfs2_remove_empty_extent(left_leaf_el
);
2661 ret
= ocfs2_journal_dirty(handle
, et_root_bh
);
2667 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
2675 * Given a full path, determine what cpos value would return us a path
2676 * containing the leaf immediately to the right of the current one.
2678 * Will return zero if the path passed in is already the rightmost path.
2680 * This looks similar, but is subtly different to
2681 * ocfs2_find_cpos_for_left_leaf().
2683 static int ocfs2_find_cpos_for_right_leaf(struct super_block
*sb
,
2684 struct ocfs2_path
*path
, u32
*cpos
)
2688 struct ocfs2_extent_list
*el
;
2692 if (path
->p_tree_depth
== 0)
2695 blkno
= path_leaf_bh(path
)->b_blocknr
;
2697 /* Start at the tree node just above the leaf and work our way up. */
2698 i
= path
->p_tree_depth
- 1;
2702 el
= path
->p_node
[i
].el
;
2705 * Find the extent record just after the one in our
2708 next_free
= le16_to_cpu(el
->l_next_free_rec
);
2709 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
2710 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
2711 if (j
== (next_free
- 1)) {
2714 * We've determined that the
2715 * path specified is already
2716 * the rightmost one - return a
2722 * The rightmost record points to our
2723 * leaf - we need to travel up the
2729 *cpos
= le32_to_cpu(el
->l_recs
[j
+ 1].e_cpos
);
2735 * If we got here, we never found a valid node where
2736 * the tree indicated one should be.
2739 "Invalid extent tree at extent block %llu\n",
2740 (unsigned long long)blkno
);
2745 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2753 static int ocfs2_rotate_rightmost_leaf_left(struct inode
*inode
,
2755 struct ocfs2_path
*path
)
2758 struct buffer_head
*bh
= path_leaf_bh(path
);
2759 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
2761 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2764 ret
= ocfs2_path_bh_journal_access(handle
, inode
, path
,
2765 path_num_items(path
) - 1);
2771 ocfs2_remove_empty_extent(el
);
2773 ret
= ocfs2_journal_dirty(handle
, bh
);
2781 static int __ocfs2_rotate_tree_left(struct inode
*inode
,
2782 handle_t
*handle
, int orig_credits
,
2783 struct ocfs2_path
*path
,
2784 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2785 struct ocfs2_path
**empty_extent_path
,
2786 struct ocfs2_extent_tree
*et
)
2788 int ret
, subtree_root
, deleted
;
2790 struct ocfs2_path
*left_path
= NULL
;
2791 struct ocfs2_path
*right_path
= NULL
;
2793 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path
)->l_recs
[0])));
2795 *empty_extent_path
= NULL
;
2797 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, path
,
2804 left_path
= ocfs2_new_path_from_path(path
);
2811 ocfs2_cp_path(left_path
, path
);
2813 right_path
= ocfs2_new_path_from_path(path
);
2820 while (right_cpos
) {
2821 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
2827 subtree_root
= ocfs2_find_subtree_root(inode
, left_path
,
2830 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2832 (unsigned long long)
2833 right_path
->p_node
[subtree_root
].bh
->b_blocknr
,
2834 right_path
->p_tree_depth
);
2836 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_root
,
2837 orig_credits
, left_path
);
2844 * Caller might still want to make changes to the
2845 * tree root, so re-add it to the journal here.
2847 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
2854 ret
= ocfs2_rotate_subtree_left(inode
, handle
, left_path
,
2855 right_path
, subtree_root
,
2856 dealloc
, &deleted
, et
);
2857 if (ret
== -EAGAIN
) {
2859 * The rotation has to temporarily stop due to
2860 * the right subtree having an empty
2861 * extent. Pass it back to the caller for a
2864 *empty_extent_path
= right_path
;
2874 * The subtree rotate might have removed records on
2875 * the rightmost edge. If so, then rotation is
2881 ocfs2_mv_path(left_path
, right_path
);
2883 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
2892 ocfs2_free_path(right_path
);
2893 ocfs2_free_path(left_path
);
2898 static int ocfs2_remove_rightmost_path(struct inode
*inode
, handle_t
*handle
,
2899 struct ocfs2_path
*path
,
2900 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2901 struct ocfs2_extent_tree
*et
)
2903 int ret
, subtree_index
;
2905 struct ocfs2_path
*left_path
= NULL
;
2906 struct ocfs2_extent_block
*eb
;
2907 struct ocfs2_extent_list
*el
;
2910 ret
= ocfs2_et_sanity_check(inode
, et
);
2914 * There's two ways we handle this depending on
2915 * whether path is the only existing one.
2917 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
2918 handle
->h_buffer_credits
,
2925 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
2931 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
2939 * We have a path to the left of this one - it needs
2942 left_path
= ocfs2_new_path_from_path(path
);
2949 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
2955 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
2961 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
2963 ocfs2_unlink_subtree(inode
, handle
, left_path
, path
,
2964 subtree_index
, dealloc
);
2965 ocfs2_update_edge_lengths(inode
, handle
, left_path
);
2967 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2968 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
2971 * 'path' is also the leftmost path which
2972 * means it must be the only one. This gets
2973 * handled differently because we want to
2974 * revert the inode back to having extents
2977 ocfs2_unlink_path(inode
, handle
, dealloc
, path
, 1);
2979 el
= et
->et_root_el
;
2980 el
->l_tree_depth
= 0;
2981 el
->l_next_free_rec
= 0;
2982 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2984 ocfs2_et_set_last_eb_blk(et
, 0);
2987 ocfs2_journal_dirty(handle
, path_root_bh(path
));
2990 ocfs2_free_path(left_path
);
2995 * Left rotation of btree records.
2997 * In many ways, this is (unsurprisingly) the opposite of right
2998 * rotation. We start at some non-rightmost path containing an empty
2999 * extent in the leaf block. The code works its way to the rightmost
3000 * path by rotating records to the left in every subtree.
3002 * This is used by any code which reduces the number of extent records
3003 * in a leaf. After removal, an empty record should be placed in the
3004 * leftmost list position.
3006 * This won't handle a length update of the rightmost path records if
3007 * the rightmost tree leaf record is removed so the caller is
3008 * responsible for detecting and correcting that.
3010 static int ocfs2_rotate_tree_left(struct inode
*inode
, handle_t
*handle
,
3011 struct ocfs2_path
*path
,
3012 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3013 struct ocfs2_extent_tree
*et
)
3015 int ret
, orig_credits
= handle
->h_buffer_credits
;
3016 struct ocfs2_path
*tmp_path
= NULL
, *restart_path
= NULL
;
3017 struct ocfs2_extent_block
*eb
;
3018 struct ocfs2_extent_list
*el
;
3020 el
= path_leaf_el(path
);
3021 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
3024 if (path
->p_tree_depth
== 0) {
3025 rightmost_no_delete
:
3027 * Inline extents. This is trivially handled, so do
3030 ret
= ocfs2_rotate_rightmost_leaf_left(inode
, handle
,
3038 * Handle rightmost branch now. There's several cases:
3039 * 1) simple rotation leaving records in there. That's trivial.
3040 * 2) rotation requiring a branch delete - there's no more
3041 * records left. Two cases of this:
3042 * a) There are branches to the left.
3043 * b) This is also the leftmost (the only) branch.
3045 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3046 * 2a) we need the left branch so that we can update it with the unlink
3047 * 2b) we need to bring the inode back to inline extents.
3050 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
3052 if (eb
->h_next_leaf_blk
== 0) {
3054 * This gets a bit tricky if we're going to delete the
3055 * rightmost path. Get the other cases out of the way
3058 if (le16_to_cpu(el
->l_next_free_rec
) > 1)
3059 goto rightmost_no_delete
;
3061 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
3063 ocfs2_error(inode
->i_sb
,
3064 "Inode %llu has empty extent block at %llu",
3065 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
3066 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
3071 * XXX: The caller can not trust "path" any more after
3072 * this as it will have been deleted. What do we do?
3074 * In theory the rotate-for-merge code will never get
3075 * here because it'll always ask for a rotate in a
3079 ret
= ocfs2_remove_rightmost_path(inode
, handle
, path
,
3087 * Now we can loop, remembering the path we get from -EAGAIN
3088 * and restarting from there.
3091 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
, path
,
3092 dealloc
, &restart_path
, et
);
3093 if (ret
&& ret
!= -EAGAIN
) {
3098 while (ret
== -EAGAIN
) {
3099 tmp_path
= restart_path
;
3100 restart_path
= NULL
;
3102 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
,
3105 if (ret
&& ret
!= -EAGAIN
) {
3110 ocfs2_free_path(tmp_path
);
3118 ocfs2_free_path(tmp_path
);
3119 ocfs2_free_path(restart_path
);
3123 static void ocfs2_cleanup_merge(struct ocfs2_extent_list
*el
,
3126 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[index
];
3129 if (rec
->e_leaf_clusters
== 0) {
3131 * We consumed all of the merged-from record. An empty
3132 * extent cannot exist anywhere but the 1st array
3133 * position, so move things over if the merged-from
3134 * record doesn't occupy that position.
3136 * This creates a new empty extent so the caller
3137 * should be smart enough to have removed any existing
3141 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
3142 size
= index
* sizeof(struct ocfs2_extent_rec
);
3143 memmove(&el
->l_recs
[1], &el
->l_recs
[0], size
);
3147 * Always memset - the caller doesn't check whether it
3148 * created an empty extent, so there could be junk in
3151 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
3155 static int ocfs2_get_right_path(struct inode
*inode
,
3156 struct ocfs2_path
*left_path
,
3157 struct ocfs2_path
**ret_right_path
)
3161 struct ocfs2_path
*right_path
= NULL
;
3162 struct ocfs2_extent_list
*left_el
;
3164 *ret_right_path
= NULL
;
3166 /* This function shouldn't be called for non-trees. */
3167 BUG_ON(left_path
->p_tree_depth
== 0);
3169 left_el
= path_leaf_el(left_path
);
3170 BUG_ON(left_el
->l_next_free_rec
!= left_el
->l_count
);
3172 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
3179 /* This function shouldn't be called for the rightmost leaf. */
3180 BUG_ON(right_cpos
== 0);
3182 right_path
= ocfs2_new_path_from_path(left_path
);
3189 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
3195 *ret_right_path
= right_path
;
3198 ocfs2_free_path(right_path
);
3203 * Remove split_rec clusters from the record at index and merge them
3204 * onto the beginning of the record "next" to it.
3205 * For index < l_count - 1, the next means the extent rec at index + 1.
3206 * For index == l_count - 1, the "next" means the 1st extent rec of the
3207 * next extent block.
3209 static int ocfs2_merge_rec_right(struct inode
*inode
,
3210 struct ocfs2_path
*left_path
,
3212 struct ocfs2_extent_rec
*split_rec
,
3215 int ret
, next_free
, i
;
3216 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3217 struct ocfs2_extent_rec
*left_rec
;
3218 struct ocfs2_extent_rec
*right_rec
;
3219 struct ocfs2_extent_list
*right_el
;
3220 struct ocfs2_path
*right_path
= NULL
;
3221 int subtree_index
= 0;
3222 struct ocfs2_extent_list
*el
= path_leaf_el(left_path
);
3223 struct buffer_head
*bh
= path_leaf_bh(left_path
);
3224 struct buffer_head
*root_bh
= NULL
;
3226 BUG_ON(index
>= le16_to_cpu(el
->l_next_free_rec
));
3227 left_rec
= &el
->l_recs
[index
];
3229 if (index
== le16_to_cpu(el
->l_next_free_rec
) - 1 &&
3230 le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
)) {
3231 /* we meet with a cross extent block merge. */
3232 ret
= ocfs2_get_right_path(inode
, left_path
, &right_path
);
3238 right_el
= path_leaf_el(right_path
);
3239 next_free
= le16_to_cpu(right_el
->l_next_free_rec
);
3240 BUG_ON(next_free
<= 0);
3241 right_rec
= &right_el
->l_recs
[0];
3242 if (ocfs2_is_empty_extent(right_rec
)) {
3243 BUG_ON(next_free
<= 1);
3244 right_rec
= &right_el
->l_recs
[1];
3247 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3248 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3249 le32_to_cpu(right_rec
->e_cpos
));
3251 subtree_index
= ocfs2_find_subtree_root(inode
,
3252 left_path
, right_path
);
3254 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3255 handle
->h_buffer_credits
,
3262 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3263 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3265 ret
= ocfs2_path_bh_journal_access(handle
, inode
, right_path
,
3272 for (i
= subtree_index
+ 1;
3273 i
< path_num_items(right_path
); i
++) {
3274 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
3281 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
3290 BUG_ON(index
== le16_to_cpu(el
->l_next_free_rec
) - 1);
3291 right_rec
= &el
->l_recs
[index
+ 1];
3294 ret
= ocfs2_path_bh_journal_access(handle
, inode
, left_path
,
3295 path_num_items(left_path
) - 1);
3301 le16_add_cpu(&left_rec
->e_leaf_clusters
, -split_clusters
);
3303 le32_add_cpu(&right_rec
->e_cpos
, -split_clusters
);
3304 le64_add_cpu(&right_rec
->e_blkno
,
3305 -ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
3306 le16_add_cpu(&right_rec
->e_leaf_clusters
, split_clusters
);
3308 ocfs2_cleanup_merge(el
, index
);
3310 ret
= ocfs2_journal_dirty(handle
, bh
);
3315 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
3319 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3320 right_path
, subtree_index
);
3324 ocfs2_free_path(right_path
);
3328 static int ocfs2_get_left_path(struct inode
*inode
,
3329 struct ocfs2_path
*right_path
,
3330 struct ocfs2_path
**ret_left_path
)
3334 struct ocfs2_path
*left_path
= NULL
;
3336 *ret_left_path
= NULL
;
3338 /* This function shouldn't be called for non-trees. */
3339 BUG_ON(right_path
->p_tree_depth
== 0);
3341 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
3342 right_path
, &left_cpos
);
3348 /* This function shouldn't be called for the leftmost leaf. */
3349 BUG_ON(left_cpos
== 0);
3351 left_path
= ocfs2_new_path_from_path(right_path
);
3358 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
3364 *ret_left_path
= left_path
;
3367 ocfs2_free_path(left_path
);
3372 * Remove split_rec clusters from the record at index and merge them
3373 * onto the tail of the record "before" it.
3374 * For index > 0, the "before" means the extent rec at index - 1.
3376 * For index == 0, the "before" means the last record of the previous
3377 * extent block. And there is also a situation that we may need to
3378 * remove the rightmost leaf extent block in the right_path and change
3379 * the right path to indicate the new rightmost path.
3381 static int ocfs2_merge_rec_left(struct inode
*inode
,
3382 struct ocfs2_path
*right_path
,
3384 struct ocfs2_extent_rec
*split_rec
,
3385 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3386 struct ocfs2_extent_tree
*et
,
3389 int ret
, i
, subtree_index
= 0, has_empty_extent
= 0;
3390 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3391 struct ocfs2_extent_rec
*left_rec
;
3392 struct ocfs2_extent_rec
*right_rec
;
3393 struct ocfs2_extent_list
*el
= path_leaf_el(right_path
);
3394 struct buffer_head
*bh
= path_leaf_bh(right_path
);
3395 struct buffer_head
*root_bh
= NULL
;
3396 struct ocfs2_path
*left_path
= NULL
;
3397 struct ocfs2_extent_list
*left_el
;
3401 right_rec
= &el
->l_recs
[index
];
3403 /* we meet with a cross extent block merge. */
3404 ret
= ocfs2_get_left_path(inode
, right_path
, &left_path
);
3410 left_el
= path_leaf_el(left_path
);
3411 BUG_ON(le16_to_cpu(left_el
->l_next_free_rec
) !=
3412 le16_to_cpu(left_el
->l_count
));
3414 left_rec
= &left_el
->l_recs
[
3415 le16_to_cpu(left_el
->l_next_free_rec
) - 1];
3416 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3417 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3418 le32_to_cpu(split_rec
->e_cpos
));
3420 subtree_index
= ocfs2_find_subtree_root(inode
,
3421 left_path
, right_path
);
3423 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3424 handle
->h_buffer_credits
,
3431 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3432 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3434 ret
= ocfs2_path_bh_journal_access(handle
, inode
, right_path
,
3441 for (i
= subtree_index
+ 1;
3442 i
< path_num_items(right_path
); i
++) {
3443 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
3450 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
3458 left_rec
= &el
->l_recs
[index
- 1];
3459 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
3460 has_empty_extent
= 1;
3463 ret
= ocfs2_path_bh_journal_access(handle
, inode
, right_path
,
3464 path_num_items(right_path
) - 1);
3470 if (has_empty_extent
&& index
== 1) {
3472 * The easy case - we can just plop the record right in.
3474 *left_rec
= *split_rec
;
3476 has_empty_extent
= 0;
3478 le16_add_cpu(&left_rec
->e_leaf_clusters
, split_clusters
);
3480 le32_add_cpu(&right_rec
->e_cpos
, split_clusters
);
3481 le64_add_cpu(&right_rec
->e_blkno
,
3482 ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
3483 le16_add_cpu(&right_rec
->e_leaf_clusters
, -split_clusters
);
3485 ocfs2_cleanup_merge(el
, index
);
3487 ret
= ocfs2_journal_dirty(handle
, bh
);
3492 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
3497 * In the situation that the right_rec is empty and the extent
3498 * block is empty also, ocfs2_complete_edge_insert can't handle
3499 * it and we need to delete the right extent block.
3501 if (le16_to_cpu(right_rec
->e_leaf_clusters
) == 0 &&
3502 le16_to_cpu(el
->l_next_free_rec
) == 1) {
3504 ret
= ocfs2_remove_rightmost_path(inode
, handle
,
3512 /* Now the rightmost extent block has been deleted.
3513 * So we use the new rightmost path.
3515 ocfs2_mv_path(right_path
, left_path
);
3518 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3519 right_path
, subtree_index
);
3523 ocfs2_free_path(left_path
);
3527 static int ocfs2_try_to_merge_extent(struct inode
*inode
,
3529 struct ocfs2_path
*path
,
3531 struct ocfs2_extent_rec
*split_rec
,
3532 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3533 struct ocfs2_merge_ctxt
*ctxt
,
3534 struct ocfs2_extent_tree
*et
)
3538 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
3539 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
3541 BUG_ON(ctxt
->c_contig_type
== CONTIG_NONE
);
3543 if (ctxt
->c_split_covers_rec
&& ctxt
->c_has_empty_extent
) {
3545 * The merge code will need to create an empty
3546 * extent to take the place of the newly
3547 * emptied slot. Remove any pre-existing empty
3548 * extents - having more than one in a leaf is
3551 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3558 rec
= &el
->l_recs
[split_index
];
3561 if (ctxt
->c_contig_type
== CONTIG_LEFTRIGHT
) {
3563 * Left-right contig implies this.
3565 BUG_ON(!ctxt
->c_split_covers_rec
);
3568 * Since the leftright insert always covers the entire
3569 * extent, this call will delete the insert record
3570 * entirely, resulting in an empty extent record added to
3573 * Since the adding of an empty extent shifts
3574 * everything back to the right, there's no need to
3575 * update split_index here.
3577 * When the split_index is zero, we need to merge it to the
3578 * prevoius extent block. It is more efficient and easier
3579 * if we do merge_right first and merge_left later.
3581 ret
= ocfs2_merge_rec_right(inode
, path
,
3590 * We can only get this from logic error above.
3592 BUG_ON(!ocfs2_is_empty_extent(&el
->l_recs
[0]));
3594 /* The merge left us with an empty extent, remove it. */
3595 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3602 rec
= &el
->l_recs
[split_index
];
3605 * Note that we don't pass split_rec here on purpose -
3606 * we've merged it into the rec already.
3608 ret
= ocfs2_merge_rec_left(inode
, path
,
3618 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3621 * Error from this last rotate is not critical, so
3622 * print but don't bubble it up.
3629 * Merge a record to the left or right.
3631 * 'contig_type' is relative to the existing record,
3632 * so for example, if we're "right contig", it's to
3633 * the record on the left (hence the left merge).
3635 if (ctxt
->c_contig_type
== CONTIG_RIGHT
) {
3636 ret
= ocfs2_merge_rec_left(inode
,
3646 ret
= ocfs2_merge_rec_right(inode
,
3656 if (ctxt
->c_split_covers_rec
) {
3658 * The merge may have left an empty extent in
3659 * our leaf. Try to rotate it away.
3661 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3673 static void ocfs2_subtract_from_rec(struct super_block
*sb
,
3674 enum ocfs2_split_type split
,
3675 struct ocfs2_extent_rec
*rec
,
3676 struct ocfs2_extent_rec
*split_rec
)
3680 len_blocks
= ocfs2_clusters_to_blocks(sb
,
3681 le16_to_cpu(split_rec
->e_leaf_clusters
));
3683 if (split
== SPLIT_LEFT
) {
3685 * Region is on the left edge of the existing
3688 le32_add_cpu(&rec
->e_cpos
,
3689 le16_to_cpu(split_rec
->e_leaf_clusters
));
3690 le64_add_cpu(&rec
->e_blkno
, len_blocks
);
3691 le16_add_cpu(&rec
->e_leaf_clusters
,
3692 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3695 * Region is on the right edge of the existing
3698 le16_add_cpu(&rec
->e_leaf_clusters
,
3699 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3704 * Do the final bits of extent record insertion at the target leaf
3705 * list. If this leaf is part of an allocation tree, it is assumed
3706 * that the tree above has been prepared.
3708 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec
*insert_rec
,
3709 struct ocfs2_extent_list
*el
,
3710 struct ocfs2_insert_type
*insert
,
3711 struct inode
*inode
)
3713 int i
= insert
->ins_contig_index
;
3715 struct ocfs2_extent_rec
*rec
;
3717 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
3719 if (insert
->ins_split
!= SPLIT_NONE
) {
3720 i
= ocfs2_search_extent_list(el
, le32_to_cpu(insert_rec
->e_cpos
));
3722 rec
= &el
->l_recs
[i
];
3723 ocfs2_subtract_from_rec(inode
->i_sb
, insert
->ins_split
, rec
,
3729 * Contiguous insert - either left or right.
3731 if (insert
->ins_contig
!= CONTIG_NONE
) {
3732 rec
= &el
->l_recs
[i
];
3733 if (insert
->ins_contig
== CONTIG_LEFT
) {
3734 rec
->e_blkno
= insert_rec
->e_blkno
;
3735 rec
->e_cpos
= insert_rec
->e_cpos
;
3737 le16_add_cpu(&rec
->e_leaf_clusters
,
3738 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3743 * Handle insert into an empty leaf.
3745 if (le16_to_cpu(el
->l_next_free_rec
) == 0 ||
3746 ((le16_to_cpu(el
->l_next_free_rec
) == 1) &&
3747 ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3748 el
->l_recs
[0] = *insert_rec
;
3749 el
->l_next_free_rec
= cpu_to_le16(1);
3756 if (insert
->ins_appending
== APPEND_TAIL
) {
3757 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
3758 rec
= &el
->l_recs
[i
];
3759 range
= le32_to_cpu(rec
->e_cpos
)
3760 + le16_to_cpu(rec
->e_leaf_clusters
);
3761 BUG_ON(le32_to_cpu(insert_rec
->e_cpos
) < range
);
3763 mlog_bug_on_msg(le16_to_cpu(el
->l_next_free_rec
) >=
3764 le16_to_cpu(el
->l_count
),
3765 "inode %lu, depth %u, count %u, next free %u, "
3766 "rec.cpos %u, rec.clusters %u, "
3767 "insert.cpos %u, insert.clusters %u\n",
3769 le16_to_cpu(el
->l_tree_depth
),
3770 le16_to_cpu(el
->l_count
),
3771 le16_to_cpu(el
->l_next_free_rec
),
3772 le32_to_cpu(el
->l_recs
[i
].e_cpos
),
3773 le16_to_cpu(el
->l_recs
[i
].e_leaf_clusters
),
3774 le32_to_cpu(insert_rec
->e_cpos
),
3775 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3777 el
->l_recs
[i
] = *insert_rec
;
3778 le16_add_cpu(&el
->l_next_free_rec
, 1);
3784 * Ok, we have to rotate.
3786 * At this point, it is safe to assume that inserting into an
3787 * empty leaf and appending to a leaf have both been handled
3790 * This leaf needs to have space, either by the empty 1st
3791 * extent record, or by virtue of an l_next_rec < l_count.
3793 ocfs2_rotate_leaf(el
, insert_rec
);
3796 static void ocfs2_adjust_rightmost_records(struct inode
*inode
,
3798 struct ocfs2_path
*path
,
3799 struct ocfs2_extent_rec
*insert_rec
)
3801 int ret
, i
, next_free
;
3802 struct buffer_head
*bh
;
3803 struct ocfs2_extent_list
*el
;
3804 struct ocfs2_extent_rec
*rec
;
3807 * Update everything except the leaf block.
3809 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
3810 bh
= path
->p_node
[i
].bh
;
3811 el
= path
->p_node
[i
].el
;
3813 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3814 if (next_free
== 0) {
3815 ocfs2_error(inode
->i_sb
,
3816 "Dinode %llu has a bad extent list",
3817 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3822 rec
= &el
->l_recs
[next_free
- 1];
3824 rec
->e_int_clusters
= insert_rec
->e_cpos
;
3825 le32_add_cpu(&rec
->e_int_clusters
,
3826 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3827 le32_add_cpu(&rec
->e_int_clusters
,
3828 -le32_to_cpu(rec
->e_cpos
));
3830 ret
= ocfs2_journal_dirty(handle
, bh
);
3837 static int ocfs2_append_rec_to_path(struct inode
*inode
, handle_t
*handle
,
3838 struct ocfs2_extent_rec
*insert_rec
,
3839 struct ocfs2_path
*right_path
,
3840 struct ocfs2_path
**ret_left_path
)
3843 struct ocfs2_extent_list
*el
;
3844 struct ocfs2_path
*left_path
= NULL
;
3846 *ret_left_path
= NULL
;
3849 * This shouldn't happen for non-trees. The extent rec cluster
3850 * count manipulation below only works for interior nodes.
3852 BUG_ON(right_path
->p_tree_depth
== 0);
3855 * If our appending insert is at the leftmost edge of a leaf,
3856 * then we might need to update the rightmost records of the
3859 el
= path_leaf_el(right_path
);
3860 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3861 if (next_free
== 0 ||
3862 (next_free
== 1 && ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3865 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
3872 mlog(0, "Append may need a left path update. cpos: %u, "
3873 "left_cpos: %u\n", le32_to_cpu(insert_rec
->e_cpos
),
3877 * No need to worry if the append is already in the
3881 left_path
= ocfs2_new_path_from_path(right_path
);
3888 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
3895 * ocfs2_insert_path() will pass the left_path to the
3901 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
3907 ocfs2_adjust_rightmost_records(inode
, handle
, right_path
, insert_rec
);
3909 *ret_left_path
= left_path
;
3913 ocfs2_free_path(left_path
);
3918 static void ocfs2_split_record(struct inode
*inode
,
3919 struct ocfs2_path
*left_path
,
3920 struct ocfs2_path
*right_path
,
3921 struct ocfs2_extent_rec
*split_rec
,
3922 enum ocfs2_split_type split
)
3925 u32 cpos
= le32_to_cpu(split_rec
->e_cpos
);
3926 struct ocfs2_extent_list
*left_el
= NULL
, *right_el
, *insert_el
, *el
;
3927 struct ocfs2_extent_rec
*rec
, *tmprec
;
3929 right_el
= path_leaf_el(right_path
);
3931 left_el
= path_leaf_el(left_path
);
3934 insert_el
= right_el
;
3935 index
= ocfs2_search_extent_list(el
, cpos
);
3937 if (index
== 0 && left_path
) {
3938 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
3941 * This typically means that the record
3942 * started in the left path but moved to the
3943 * right as a result of rotation. We either
3944 * move the existing record to the left, or we
3945 * do the later insert there.
3947 * In this case, the left path should always
3948 * exist as the rotate code will have passed
3949 * it back for a post-insert update.
3952 if (split
== SPLIT_LEFT
) {
3954 * It's a left split. Since we know
3955 * that the rotate code gave us an
3956 * empty extent in the left path, we
3957 * can just do the insert there.
3959 insert_el
= left_el
;
3962 * Right split - we have to move the
3963 * existing record over to the left
3964 * leaf. The insert will be into the
3965 * newly created empty extent in the
3968 tmprec
= &right_el
->l_recs
[index
];
3969 ocfs2_rotate_leaf(left_el
, tmprec
);
3972 memset(tmprec
, 0, sizeof(*tmprec
));
3973 index
= ocfs2_search_extent_list(left_el
, cpos
);
3974 BUG_ON(index
== -1);
3979 BUG_ON(!ocfs2_is_empty_extent(&left_el
->l_recs
[0]));
3981 * Left path is easy - we can just allow the insert to
3985 insert_el
= left_el
;
3986 index
= ocfs2_search_extent_list(el
, cpos
);
3987 BUG_ON(index
== -1);
3990 rec
= &el
->l_recs
[index
];
3991 ocfs2_subtract_from_rec(inode
->i_sb
, split
, rec
, split_rec
);
3992 ocfs2_rotate_leaf(insert_el
, split_rec
);
3996 * This function only does inserts on an allocation b-tree. For tree
3997 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3999 * right_path is the path we want to do the actual insert
4000 * in. left_path should only be passed in if we need to update that
4001 * portion of the tree after an edge insert.
4003 static int ocfs2_insert_path(struct inode
*inode
,
4005 struct ocfs2_path
*left_path
,
4006 struct ocfs2_path
*right_path
,
4007 struct ocfs2_extent_rec
*insert_rec
,
4008 struct ocfs2_insert_type
*insert
)
4010 int ret
, subtree_index
;
4011 struct buffer_head
*leaf_bh
= path_leaf_bh(right_path
);
4014 int credits
= handle
->h_buffer_credits
;
4017 * There's a chance that left_path got passed back to
4018 * us without being accounted for in the
4019 * journal. Extend our transaction here to be sure we
4020 * can change those blocks.
4022 credits
+= left_path
->p_tree_depth
;
4024 ret
= ocfs2_extend_trans(handle
, credits
);
4030 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
4038 * Pass both paths to the journal. The majority of inserts
4039 * will be touching all components anyway.
4041 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
4047 if (insert
->ins_split
!= SPLIT_NONE
) {
4049 * We could call ocfs2_insert_at_leaf() for some types
4050 * of splits, but it's easier to just let one separate
4051 * function sort it all out.
4053 ocfs2_split_record(inode
, left_path
, right_path
,
4054 insert_rec
, insert
->ins_split
);
4057 * Split might have modified either leaf and we don't
4058 * have a guarantee that the later edge insert will
4059 * dirty this for us.
4062 ret
= ocfs2_journal_dirty(handle
,
4063 path_leaf_bh(left_path
));
4067 ocfs2_insert_at_leaf(insert_rec
, path_leaf_el(right_path
),
4070 ret
= ocfs2_journal_dirty(handle
, leaf_bh
);
4076 * The rotate code has indicated that we need to fix
4077 * up portions of the tree after the insert.
4079 * XXX: Should we extend the transaction here?
4081 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
,
4083 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
4084 right_path
, subtree_index
);
4092 static int ocfs2_do_insert_extent(struct inode
*inode
,
4094 struct ocfs2_extent_tree
*et
,
4095 struct ocfs2_extent_rec
*insert_rec
,
4096 struct ocfs2_insert_type
*type
)
4098 int ret
, rotate
= 0;
4100 struct ocfs2_path
*right_path
= NULL
;
4101 struct ocfs2_path
*left_path
= NULL
;
4102 struct ocfs2_extent_list
*el
;
4104 el
= et
->et_root_el
;
4106 ret
= ocfs2_et_root_journal_access(handle
, inode
, et
,
4107 OCFS2_JOURNAL_ACCESS_WRITE
);
4113 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
4114 ocfs2_insert_at_leaf(insert_rec
, el
, type
, inode
);
4115 goto out_update_clusters
;
4118 right_path
= ocfs2_new_path_from_et(et
);
4126 * Determine the path to start with. Rotations need the
4127 * rightmost path, everything else can go directly to the
4130 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4131 if (type
->ins_appending
== APPEND_NONE
&&
4132 type
->ins_contig
== CONTIG_NONE
) {
4137 ret
= ocfs2_find_path(inode
, right_path
, cpos
);
4144 * Rotations and appends need special treatment - they modify
4145 * parts of the tree's above them.
4147 * Both might pass back a path immediate to the left of the
4148 * one being inserted to. This will be cause
4149 * ocfs2_insert_path() to modify the rightmost records of
4150 * left_path to account for an edge insert.
4152 * XXX: When modifying this code, keep in mind that an insert
4153 * can wind up skipping both of these two special cases...
4156 ret
= ocfs2_rotate_tree_right(inode
, handle
, type
->ins_split
,
4157 le32_to_cpu(insert_rec
->e_cpos
),
4158 right_path
, &left_path
);
4165 * ocfs2_rotate_tree_right() might have extended the
4166 * transaction without re-journaling our tree root.
4168 ret
= ocfs2_et_root_journal_access(handle
, inode
, et
,
4169 OCFS2_JOURNAL_ACCESS_WRITE
);
4174 } else if (type
->ins_appending
== APPEND_TAIL
4175 && type
->ins_contig
!= CONTIG_LEFT
) {
4176 ret
= ocfs2_append_rec_to_path(inode
, handle
, insert_rec
,
4177 right_path
, &left_path
);
4184 ret
= ocfs2_insert_path(inode
, handle
, left_path
, right_path
,
4191 out_update_clusters
:
4192 if (type
->ins_split
== SPLIT_NONE
)
4193 ocfs2_et_update_clusters(inode
, et
,
4194 le16_to_cpu(insert_rec
->e_leaf_clusters
));
4196 ret
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
4201 ocfs2_free_path(left_path
);
4202 ocfs2_free_path(right_path
);
4207 static enum ocfs2_contig_type
4208 ocfs2_figure_merge_contig_type(struct inode
*inode
, struct ocfs2_path
*path
,
4209 struct ocfs2_extent_list
*el
, int index
,
4210 struct ocfs2_extent_rec
*split_rec
)
4213 enum ocfs2_contig_type ret
= CONTIG_NONE
;
4214 u32 left_cpos
, right_cpos
;
4215 struct ocfs2_extent_rec
*rec
= NULL
;
4216 struct ocfs2_extent_list
*new_el
;
4217 struct ocfs2_path
*left_path
= NULL
, *right_path
= NULL
;
4218 struct buffer_head
*bh
;
4219 struct ocfs2_extent_block
*eb
;
4222 rec
= &el
->l_recs
[index
- 1];
4223 } else if (path
->p_tree_depth
> 0) {
4224 status
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
4229 if (left_cpos
!= 0) {
4230 left_path
= ocfs2_new_path_from_path(path
);
4234 status
= ocfs2_find_path(inode
, left_path
, left_cpos
);
4238 new_el
= path_leaf_el(left_path
);
4240 if (le16_to_cpu(new_el
->l_next_free_rec
) !=
4241 le16_to_cpu(new_el
->l_count
)) {
4242 bh
= path_leaf_bh(left_path
);
4243 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4244 ocfs2_error(inode
->i_sb
,
4245 "Extent block #%llu has an "
4246 "invalid l_next_free_rec of "
4247 "%d. It should have "
4248 "matched the l_count of %d",
4249 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
4250 le16_to_cpu(new_el
->l_next_free_rec
),
4251 le16_to_cpu(new_el
->l_count
));
4255 rec
= &new_el
->l_recs
[
4256 le16_to_cpu(new_el
->l_next_free_rec
) - 1];
4261 * We're careful to check for an empty extent record here -
4262 * the merge code will know what to do if it sees one.
4265 if (index
== 1 && ocfs2_is_empty_extent(rec
)) {
4266 if (split_rec
->e_cpos
== el
->l_recs
[index
].e_cpos
)
4269 ret
= ocfs2_extent_contig(inode
, rec
, split_rec
);
4274 if (index
< (le16_to_cpu(el
->l_next_free_rec
) - 1))
4275 rec
= &el
->l_recs
[index
+ 1];
4276 else if (le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
) &&
4277 path
->p_tree_depth
> 0) {
4278 status
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
,
4283 if (right_cpos
== 0)
4286 right_path
= ocfs2_new_path_from_path(path
);
4290 status
= ocfs2_find_path(inode
, right_path
, right_cpos
);
4294 new_el
= path_leaf_el(right_path
);
4295 rec
= &new_el
->l_recs
[0];
4296 if (ocfs2_is_empty_extent(rec
)) {
4297 if (le16_to_cpu(new_el
->l_next_free_rec
) <= 1) {
4298 bh
= path_leaf_bh(right_path
);
4299 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4300 ocfs2_error(inode
->i_sb
,
4301 "Extent block #%llu has an "
4302 "invalid l_next_free_rec of %d",
4303 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
4304 le16_to_cpu(new_el
->l_next_free_rec
));
4308 rec
= &new_el
->l_recs
[1];
4313 enum ocfs2_contig_type contig_type
;
4315 contig_type
= ocfs2_extent_contig(inode
, rec
, split_rec
);
4317 if (contig_type
== CONTIG_LEFT
&& ret
== CONTIG_RIGHT
)
4318 ret
= CONTIG_LEFTRIGHT
;
4319 else if (ret
== CONTIG_NONE
)
4325 ocfs2_free_path(left_path
);
4327 ocfs2_free_path(right_path
);
4332 static void ocfs2_figure_contig_type(struct inode
*inode
,
4333 struct ocfs2_insert_type
*insert
,
4334 struct ocfs2_extent_list
*el
,
4335 struct ocfs2_extent_rec
*insert_rec
,
4336 struct ocfs2_extent_tree
*et
)
4339 enum ocfs2_contig_type contig_type
= CONTIG_NONE
;
4341 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4343 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
4344 contig_type
= ocfs2_extent_contig(inode
, &el
->l_recs
[i
],
4346 if (contig_type
!= CONTIG_NONE
) {
4347 insert
->ins_contig_index
= i
;
4351 insert
->ins_contig
= contig_type
;
4353 if (insert
->ins_contig
!= CONTIG_NONE
) {
4354 struct ocfs2_extent_rec
*rec
=
4355 &el
->l_recs
[insert
->ins_contig_index
];
4356 unsigned int len
= le16_to_cpu(rec
->e_leaf_clusters
) +
4357 le16_to_cpu(insert_rec
->e_leaf_clusters
);
4360 * Caller might want us to limit the size of extents, don't
4361 * calculate contiguousness if we might exceed that limit.
4363 if (et
->et_max_leaf_clusters
&&
4364 (len
> et
->et_max_leaf_clusters
))
4365 insert
->ins_contig
= CONTIG_NONE
;
4370 * This should only be called against the righmost leaf extent list.
4372 * ocfs2_figure_appending_type() will figure out whether we'll have to
4373 * insert at the tail of the rightmost leaf.
4375 * This should also work against the root extent list for tree's with 0
4376 * depth. If we consider the root extent list to be the rightmost leaf node
4377 * then the logic here makes sense.
4379 static void ocfs2_figure_appending_type(struct ocfs2_insert_type
*insert
,
4380 struct ocfs2_extent_list
*el
,
4381 struct ocfs2_extent_rec
*insert_rec
)
4384 u32 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4385 struct ocfs2_extent_rec
*rec
;
4387 insert
->ins_appending
= APPEND_NONE
;
4389 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4391 if (!el
->l_next_free_rec
)
4392 goto set_tail_append
;
4394 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
4395 /* Were all records empty? */
4396 if (le16_to_cpu(el
->l_next_free_rec
) == 1)
4397 goto set_tail_append
;
4400 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
4401 rec
= &el
->l_recs
[i
];
4404 (le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)))
4405 goto set_tail_append
;
4410 insert
->ins_appending
= APPEND_TAIL
;
4414 * Helper function called at the begining of an insert.
4416 * This computes a few things that are commonly used in the process of
4417 * inserting into the btree:
4418 * - Whether the new extent is contiguous with an existing one.
4419 * - The current tree depth.
4420 * - Whether the insert is an appending one.
4421 * - The total # of free records in the tree.
4423 * All of the information is stored on the ocfs2_insert_type
4426 static int ocfs2_figure_insert_type(struct inode
*inode
,
4427 struct ocfs2_extent_tree
*et
,
4428 struct buffer_head
**last_eb_bh
,
4429 struct ocfs2_extent_rec
*insert_rec
,
4431 struct ocfs2_insert_type
*insert
)
4434 struct ocfs2_extent_block
*eb
;
4435 struct ocfs2_extent_list
*el
;
4436 struct ocfs2_path
*path
= NULL
;
4437 struct buffer_head
*bh
= NULL
;
4439 insert
->ins_split
= SPLIT_NONE
;
4441 el
= et
->et_root_el
;
4442 insert
->ins_tree_depth
= le16_to_cpu(el
->l_tree_depth
);
4444 if (el
->l_tree_depth
) {
4446 * If we have tree depth, we read in the
4447 * rightmost extent block ahead of time as
4448 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4449 * may want it later.
4451 ret
= ocfs2_read_extent_block(inode
,
4452 ocfs2_et_get_last_eb_blk(et
),
4458 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
4463 * Unless we have a contiguous insert, we'll need to know if
4464 * there is room left in our allocation tree for another
4467 * XXX: This test is simplistic, we can search for empty
4468 * extent records too.
4470 *free_records
= le16_to_cpu(el
->l_count
) -
4471 le16_to_cpu(el
->l_next_free_rec
);
4473 if (!insert
->ins_tree_depth
) {
4474 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4475 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4479 path
= ocfs2_new_path_from_et(et
);
4487 * In the case that we're inserting past what the tree
4488 * currently accounts for, ocfs2_find_path() will return for
4489 * us the rightmost tree path. This is accounted for below in
4490 * the appending code.
4492 ret
= ocfs2_find_path(inode
, path
, le32_to_cpu(insert_rec
->e_cpos
));
4498 el
= path_leaf_el(path
);
4501 * Now that we have the path, there's two things we want to determine:
4502 * 1) Contiguousness (also set contig_index if this is so)
4504 * 2) Are we doing an append? We can trivially break this up
4505 * into two types of appends: simple record append, or a
4506 * rotate inside the tail leaf.
4508 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4511 * The insert code isn't quite ready to deal with all cases of
4512 * left contiguousness. Specifically, if it's an insert into
4513 * the 1st record in a leaf, it will require the adjustment of
4514 * cluster count on the last record of the path directly to it's
4515 * left. For now, just catch that case and fool the layers
4516 * above us. This works just fine for tree_depth == 0, which
4517 * is why we allow that above.
4519 if (insert
->ins_contig
== CONTIG_LEFT
&&
4520 insert
->ins_contig_index
== 0)
4521 insert
->ins_contig
= CONTIG_NONE
;
4524 * Ok, so we can simply compare against last_eb to figure out
4525 * whether the path doesn't exist. This will only happen in
4526 * the case that we're doing a tail append, so maybe we can
4527 * take advantage of that information somehow.
4529 if (ocfs2_et_get_last_eb_blk(et
) ==
4530 path_leaf_bh(path
)->b_blocknr
) {
4532 * Ok, ocfs2_find_path() returned us the rightmost
4533 * tree path. This might be an appending insert. There are
4535 * 1) We're doing a true append at the tail:
4536 * -This might even be off the end of the leaf
4537 * 2) We're "appending" by rotating in the tail
4539 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4543 ocfs2_free_path(path
);
4553 * Insert an extent into an inode btree.
4555 * The caller needs to update fe->i_clusters
4557 int ocfs2_insert_extent(struct ocfs2_super
*osb
,
4559 struct inode
*inode
,
4560 struct ocfs2_extent_tree
*et
,
4565 struct ocfs2_alloc_context
*meta_ac
)
4568 int uninitialized_var(free_records
);
4569 struct buffer_head
*last_eb_bh
= NULL
;
4570 struct ocfs2_insert_type insert
= {0, };
4571 struct ocfs2_extent_rec rec
;
4573 mlog(0, "add %u clusters at position %u to inode %llu\n",
4574 new_clusters
, cpos
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4576 memset(&rec
, 0, sizeof(rec
));
4577 rec
.e_cpos
= cpu_to_le32(cpos
);
4578 rec
.e_blkno
= cpu_to_le64(start_blk
);
4579 rec
.e_leaf_clusters
= cpu_to_le16(new_clusters
);
4580 rec
.e_flags
= flags
;
4581 status
= ocfs2_et_insert_check(inode
, et
, &rec
);
4587 status
= ocfs2_figure_insert_type(inode
, et
, &last_eb_bh
, &rec
,
4588 &free_records
, &insert
);
4594 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4595 "Insert.contig_index: %d, Insert.free_records: %d, "
4596 "Insert.tree_depth: %d\n",
4597 insert
.ins_appending
, insert
.ins_contig
, insert
.ins_contig_index
,
4598 free_records
, insert
.ins_tree_depth
);
4600 if (insert
.ins_contig
== CONTIG_NONE
&& free_records
== 0) {
4601 status
= ocfs2_grow_tree(inode
, handle
, et
,
4602 &insert
.ins_tree_depth
, &last_eb_bh
,
4610 /* Finally, we can add clusters. This might rotate the tree for us. */
4611 status
= ocfs2_do_insert_extent(inode
, handle
, et
, &rec
, &insert
);
4614 else if (et
->et_ops
== &ocfs2_dinode_et_ops
)
4615 ocfs2_extent_map_insert_rec(inode
, &rec
);
4625 * Allcate and add clusters into the extent b-tree.
4626 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4627 * The extent b-tree's root is specified by et, and
4628 * it is not limited to the file storage. Any extent tree can use this
4629 * function if it implements the proper ocfs2_extent_tree.
4631 int ocfs2_add_clusters_in_btree(struct ocfs2_super
*osb
,
4632 struct inode
*inode
,
4633 u32
*logical_offset
,
4634 u32 clusters_to_add
,
4636 struct ocfs2_extent_tree
*et
,
4638 struct ocfs2_alloc_context
*data_ac
,
4639 struct ocfs2_alloc_context
*meta_ac
,
4640 enum ocfs2_alloc_restarted
*reason_ret
)
4644 enum ocfs2_alloc_restarted reason
= RESTART_NONE
;
4645 u32 bit_off
, num_bits
;
4649 BUG_ON(!clusters_to_add
);
4652 flags
= OCFS2_EXT_UNWRITTEN
;
4654 free_extents
= ocfs2_num_free_extents(osb
, inode
, et
);
4655 if (free_extents
< 0) {
4656 status
= free_extents
;
4661 /* there are two cases which could cause us to EAGAIN in the
4662 * we-need-more-metadata case:
4663 * 1) we haven't reserved *any*
4664 * 2) we are so fragmented, we've needed to add metadata too
4666 if (!free_extents
&& !meta_ac
) {
4667 mlog(0, "we haven't reserved any metadata!\n");
4669 reason
= RESTART_META
;
4671 } else if ((!free_extents
)
4672 && (ocfs2_alloc_context_bits_left(meta_ac
)
4673 < ocfs2_extend_meta_needed(et
->et_root_el
))) {
4674 mlog(0, "filesystem is really fragmented...\n");
4676 reason
= RESTART_META
;
4680 status
= __ocfs2_claim_clusters(osb
, handle
, data_ac
, 1,
4681 clusters_to_add
, &bit_off
, &num_bits
);
4683 if (status
!= -ENOSPC
)
4688 BUG_ON(num_bits
> clusters_to_add
);
4690 /* reserve our write early -- insert_extent may update the tree root */
4691 status
= ocfs2_et_root_journal_access(handle
, inode
, et
,
4692 OCFS2_JOURNAL_ACCESS_WRITE
);
4698 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
4699 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4700 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4701 status
= ocfs2_insert_extent(osb
, handle
, inode
, et
,
4702 *logical_offset
, block
,
4703 num_bits
, flags
, meta_ac
);
4709 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
4715 clusters_to_add
-= num_bits
;
4716 *logical_offset
+= num_bits
;
4718 if (clusters_to_add
) {
4719 mlog(0, "need to alloc once more, wanted = %u\n",
4722 reason
= RESTART_TRANS
;
4728 *reason_ret
= reason
;
4732 static void ocfs2_make_right_split_rec(struct super_block
*sb
,
4733 struct ocfs2_extent_rec
*split_rec
,
4735 struct ocfs2_extent_rec
*rec
)
4737 u32 rec_cpos
= le32_to_cpu(rec
->e_cpos
);
4738 u32 rec_range
= rec_cpos
+ le16_to_cpu(rec
->e_leaf_clusters
);
4740 memset(split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
4742 split_rec
->e_cpos
= cpu_to_le32(cpos
);
4743 split_rec
->e_leaf_clusters
= cpu_to_le16(rec_range
- cpos
);
4745 split_rec
->e_blkno
= rec
->e_blkno
;
4746 le64_add_cpu(&split_rec
->e_blkno
,
4747 ocfs2_clusters_to_blocks(sb
, cpos
- rec_cpos
));
4749 split_rec
->e_flags
= rec
->e_flags
;
4752 static int ocfs2_split_and_insert(struct inode
*inode
,
4754 struct ocfs2_path
*path
,
4755 struct ocfs2_extent_tree
*et
,
4756 struct buffer_head
**last_eb_bh
,
4758 struct ocfs2_extent_rec
*orig_split_rec
,
4759 struct ocfs2_alloc_context
*meta_ac
)
4762 unsigned int insert_range
, rec_range
, do_leftright
= 0;
4763 struct ocfs2_extent_rec tmprec
;
4764 struct ocfs2_extent_list
*rightmost_el
;
4765 struct ocfs2_extent_rec rec
;
4766 struct ocfs2_extent_rec split_rec
= *orig_split_rec
;
4767 struct ocfs2_insert_type insert
;
4768 struct ocfs2_extent_block
*eb
;
4772 * Store a copy of the record on the stack - it might move
4773 * around as the tree is manipulated below.
4775 rec
= path_leaf_el(path
)->l_recs
[split_index
];
4777 rightmost_el
= et
->et_root_el
;
4779 depth
= le16_to_cpu(rightmost_el
->l_tree_depth
);
4781 BUG_ON(!(*last_eb_bh
));
4782 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
4783 rightmost_el
= &eb
->h_list
;
4786 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
4787 le16_to_cpu(rightmost_el
->l_count
)) {
4788 ret
= ocfs2_grow_tree(inode
, handle
, et
,
4789 &depth
, last_eb_bh
, meta_ac
);
4796 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
4797 insert
.ins_appending
= APPEND_NONE
;
4798 insert
.ins_contig
= CONTIG_NONE
;
4799 insert
.ins_tree_depth
= depth
;
4801 insert_range
= le32_to_cpu(split_rec
.e_cpos
) +
4802 le16_to_cpu(split_rec
.e_leaf_clusters
);
4803 rec_range
= le32_to_cpu(rec
.e_cpos
) +
4804 le16_to_cpu(rec
.e_leaf_clusters
);
4806 if (split_rec
.e_cpos
== rec
.e_cpos
) {
4807 insert
.ins_split
= SPLIT_LEFT
;
4808 } else if (insert_range
== rec_range
) {
4809 insert
.ins_split
= SPLIT_RIGHT
;
4812 * Left/right split. We fake this as a right split
4813 * first and then make a second pass as a left split.
4815 insert
.ins_split
= SPLIT_RIGHT
;
4817 ocfs2_make_right_split_rec(inode
->i_sb
, &tmprec
, insert_range
,
4822 BUG_ON(do_leftright
);
4826 ret
= ocfs2_do_insert_extent(inode
, handle
, et
, &split_rec
, &insert
);
4832 if (do_leftright
== 1) {
4834 struct ocfs2_extent_list
*el
;
4837 split_rec
= *orig_split_rec
;
4839 ocfs2_reinit_path(path
, 1);
4841 cpos
= le32_to_cpu(split_rec
.e_cpos
);
4842 ret
= ocfs2_find_path(inode
, path
, cpos
);
4848 el
= path_leaf_el(path
);
4849 split_index
= ocfs2_search_extent_list(el
, cpos
);
4857 static int ocfs2_replace_extent_rec(struct inode
*inode
,
4859 struct ocfs2_path
*path
,
4860 struct ocfs2_extent_list
*el
,
4862 struct ocfs2_extent_rec
*split_rec
)
4866 ret
= ocfs2_path_bh_journal_access(handle
, inode
, path
,
4867 path_num_items(path
) - 1);
4873 el
->l_recs
[split_index
] = *split_rec
;
4875 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
4881 * Mark part or all of the extent record at split_index in the leaf
4882 * pointed to by path as written. This removes the unwritten
4885 * Care is taken to handle contiguousness so as to not grow the tree.
4887 * meta_ac is not strictly necessary - we only truly need it if growth
4888 * of the tree is required. All other cases will degrade into a less
4889 * optimal tree layout.
4891 * last_eb_bh should be the rightmost leaf block for any extent
4892 * btree. Since a split may grow the tree or a merge might shrink it,
4893 * the caller cannot trust the contents of that buffer after this call.
4895 * This code is optimized for readability - several passes might be
4896 * made over certain portions of the tree. All of those blocks will
4897 * have been brought into cache (and pinned via the journal), so the
4898 * extra overhead is not expressed in terms of disk reads.
4900 static int __ocfs2_mark_extent_written(struct inode
*inode
,
4901 struct ocfs2_extent_tree
*et
,
4903 struct ocfs2_path
*path
,
4905 struct ocfs2_extent_rec
*split_rec
,
4906 struct ocfs2_alloc_context
*meta_ac
,
4907 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
4910 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
4911 struct buffer_head
*last_eb_bh
= NULL
;
4912 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
4913 struct ocfs2_merge_ctxt ctxt
;
4914 struct ocfs2_extent_list
*rightmost_el
;
4916 if (!(rec
->e_flags
& OCFS2_EXT_UNWRITTEN
)) {
4922 if (le32_to_cpu(rec
->e_cpos
) > le32_to_cpu(split_rec
->e_cpos
) ||
4923 ((le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)) <
4924 (le32_to_cpu(split_rec
->e_cpos
) + le16_to_cpu(split_rec
->e_leaf_clusters
)))) {
4930 ctxt
.c_contig_type
= ocfs2_figure_merge_contig_type(inode
, path
, el
,
4935 * The core merge / split code wants to know how much room is
4936 * left in this inodes allocation tree, so we pass the
4937 * rightmost extent list.
4939 if (path
->p_tree_depth
) {
4940 struct ocfs2_extent_block
*eb
;
4942 ret
= ocfs2_read_extent_block(inode
,
4943 ocfs2_et_get_last_eb_blk(et
),
4950 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
4951 rightmost_el
= &eb
->h_list
;
4953 rightmost_el
= path_root_el(path
);
4955 if (rec
->e_cpos
== split_rec
->e_cpos
&&
4956 rec
->e_leaf_clusters
== split_rec
->e_leaf_clusters
)
4957 ctxt
.c_split_covers_rec
= 1;
4959 ctxt
.c_split_covers_rec
= 0;
4961 ctxt
.c_has_empty_extent
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
4963 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4964 split_index
, ctxt
.c_contig_type
, ctxt
.c_has_empty_extent
,
4965 ctxt
.c_split_covers_rec
);
4967 if (ctxt
.c_contig_type
== CONTIG_NONE
) {
4968 if (ctxt
.c_split_covers_rec
)
4969 ret
= ocfs2_replace_extent_rec(inode
, handle
,
4971 split_index
, split_rec
);
4973 ret
= ocfs2_split_and_insert(inode
, handle
, path
, et
,
4974 &last_eb_bh
, split_index
,
4975 split_rec
, meta_ac
);
4979 ret
= ocfs2_try_to_merge_extent(inode
, handle
, path
,
4980 split_index
, split_rec
,
4981 dealloc
, &ctxt
, et
);
4992 * Mark the already-existing extent at cpos as written for len clusters.
4994 * If the existing extent is larger than the request, initiate a
4995 * split. An attempt will be made at merging with adjacent extents.
4997 * The caller is responsible for passing down meta_ac if we'll need it.
4999 int ocfs2_mark_extent_written(struct inode
*inode
,
5000 struct ocfs2_extent_tree
*et
,
5001 handle_t
*handle
, u32 cpos
, u32 len
, u32 phys
,
5002 struct ocfs2_alloc_context
*meta_ac
,
5003 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5006 u64 start_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys
);
5007 struct ocfs2_extent_rec split_rec
;
5008 struct ocfs2_path
*left_path
= NULL
;
5009 struct ocfs2_extent_list
*el
;
5011 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5012 inode
->i_ino
, cpos
, len
, phys
, (unsigned long long)start_blkno
);
5014 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode
->i_sb
))) {
5015 ocfs2_error(inode
->i_sb
, "Inode %llu has unwritten extents "
5016 "that are being written to, but the feature bit "
5017 "is not set in the super block.",
5018 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
5024 * XXX: This should be fixed up so that we just re-insert the
5025 * next extent records.
5027 * XXX: This is a hack on the extent tree, maybe it should be
5030 if (et
->et_ops
== &ocfs2_dinode_et_ops
)
5031 ocfs2_extent_map_trunc(inode
, 0);
5033 left_path
= ocfs2_new_path_from_et(et
);
5040 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
5045 el
= path_leaf_el(left_path
);
5047 index
= ocfs2_search_extent_list(el
, cpos
);
5048 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5049 ocfs2_error(inode
->i_sb
,
5050 "Inode %llu has an extent at cpos %u which can no "
5051 "longer be found.\n",
5052 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
5057 memset(&split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
5058 split_rec
.e_cpos
= cpu_to_le32(cpos
);
5059 split_rec
.e_leaf_clusters
= cpu_to_le16(len
);
5060 split_rec
.e_blkno
= cpu_to_le64(start_blkno
);
5061 split_rec
.e_flags
= path_leaf_el(left_path
)->l_recs
[index
].e_flags
;
5062 split_rec
.e_flags
&= ~OCFS2_EXT_UNWRITTEN
;
5064 ret
= __ocfs2_mark_extent_written(inode
, et
, handle
, left_path
,
5065 index
, &split_rec
, meta_ac
,
5071 ocfs2_free_path(left_path
);
5075 static int ocfs2_split_tree(struct inode
*inode
, struct ocfs2_extent_tree
*et
,
5076 handle_t
*handle
, struct ocfs2_path
*path
,
5077 int index
, u32 new_range
,
5078 struct ocfs2_alloc_context
*meta_ac
)
5080 int ret
, depth
, credits
= handle
->h_buffer_credits
;
5081 struct buffer_head
*last_eb_bh
= NULL
;
5082 struct ocfs2_extent_block
*eb
;
5083 struct ocfs2_extent_list
*rightmost_el
, *el
;
5084 struct ocfs2_extent_rec split_rec
;
5085 struct ocfs2_extent_rec
*rec
;
5086 struct ocfs2_insert_type insert
;
5089 * Setup the record to split before we grow the tree.
5091 el
= path_leaf_el(path
);
5092 rec
= &el
->l_recs
[index
];
5093 ocfs2_make_right_split_rec(inode
->i_sb
, &split_rec
, new_range
, rec
);
5095 depth
= path
->p_tree_depth
;
5097 ret
= ocfs2_read_extent_block(inode
,
5098 ocfs2_et_get_last_eb_blk(et
),
5105 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
5106 rightmost_el
= &eb
->h_list
;
5108 rightmost_el
= path_leaf_el(path
);
5110 credits
+= path
->p_tree_depth
+
5111 ocfs2_extend_meta_needed(et
->et_root_el
);
5112 ret
= ocfs2_extend_trans(handle
, credits
);
5118 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
5119 le16_to_cpu(rightmost_el
->l_count
)) {
5120 ret
= ocfs2_grow_tree(inode
, handle
, et
, &depth
, &last_eb_bh
,
5128 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
5129 insert
.ins_appending
= APPEND_NONE
;
5130 insert
.ins_contig
= CONTIG_NONE
;
5131 insert
.ins_split
= SPLIT_RIGHT
;
5132 insert
.ins_tree_depth
= depth
;
5134 ret
= ocfs2_do_insert_extent(inode
, handle
, et
, &split_rec
, &insert
);
5143 static int ocfs2_truncate_rec(struct inode
*inode
, handle_t
*handle
,
5144 struct ocfs2_path
*path
, int index
,
5145 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5147 struct ocfs2_extent_tree
*et
)
5150 u32 left_cpos
, rec_range
, trunc_range
;
5151 int wants_rotate
= 0, is_rightmost_tree_rec
= 0;
5152 struct super_block
*sb
= inode
->i_sb
;
5153 struct ocfs2_path
*left_path
= NULL
;
5154 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
5155 struct ocfs2_extent_rec
*rec
;
5156 struct ocfs2_extent_block
*eb
;
5158 if (ocfs2_is_empty_extent(&el
->l_recs
[0]) && index
> 0) {
5159 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
, et
);
5168 if (index
== (le16_to_cpu(el
->l_next_free_rec
) - 1) &&
5169 path
->p_tree_depth
) {
5171 * Check whether this is the rightmost tree record. If
5172 * we remove all of this record or part of its right
5173 * edge then an update of the record lengths above it
5176 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
5177 if (eb
->h_next_leaf_blk
== 0)
5178 is_rightmost_tree_rec
= 1;
5181 rec
= &el
->l_recs
[index
];
5182 if (index
== 0 && path
->p_tree_depth
&&
5183 le32_to_cpu(rec
->e_cpos
) == cpos
) {
5185 * Changing the leftmost offset (via partial or whole
5186 * record truncate) of an interior (or rightmost) path
5187 * means we have to update the subtree that is formed
5188 * by this leaf and the one to it's left.
5190 * There are two cases we can skip:
5191 * 1) Path is the leftmost one in our inode tree.
5192 * 2) The leaf is rightmost and will be empty after
5193 * we remove the extent record - the rotate code
5194 * knows how to update the newly formed edge.
5197 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
,
5204 if (left_cpos
&& le16_to_cpu(el
->l_next_free_rec
) > 1) {
5205 left_path
= ocfs2_new_path_from_path(path
);
5212 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
5220 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
5221 handle
->h_buffer_credits
,
5228 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
5234 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
5240 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5241 trunc_range
= cpos
+ len
;
5243 if (le32_to_cpu(rec
->e_cpos
) == cpos
&& rec_range
== trunc_range
) {
5246 memset(rec
, 0, sizeof(*rec
));
5247 ocfs2_cleanup_merge(el
, index
);
5250 next_free
= le16_to_cpu(el
->l_next_free_rec
);
5251 if (is_rightmost_tree_rec
&& next_free
> 1) {
5253 * We skip the edge update if this path will
5254 * be deleted by the rotate code.
5256 rec
= &el
->l_recs
[next_free
- 1];
5257 ocfs2_adjust_rightmost_records(inode
, handle
, path
,
5260 } else if (le32_to_cpu(rec
->e_cpos
) == cpos
) {
5261 /* Remove leftmost portion of the record. */
5262 le32_add_cpu(&rec
->e_cpos
, len
);
5263 le64_add_cpu(&rec
->e_blkno
, ocfs2_clusters_to_blocks(sb
, len
));
5264 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5265 } else if (rec_range
== trunc_range
) {
5266 /* Remove rightmost portion of the record */
5267 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5268 if (is_rightmost_tree_rec
)
5269 ocfs2_adjust_rightmost_records(inode
, handle
, path
, rec
);
5271 /* Caller should have trapped this. */
5272 mlog(ML_ERROR
, "Inode %llu: Invalid record truncate: (%u, %u) "
5273 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5274 le32_to_cpu(rec
->e_cpos
),
5275 le16_to_cpu(rec
->e_leaf_clusters
), cpos
, len
);
5282 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
5283 ocfs2_complete_edge_insert(inode
, handle
, left_path
, path
,
5287 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
5289 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
, et
);
5296 ocfs2_free_path(left_path
);
5300 int ocfs2_remove_extent(struct inode
*inode
,
5301 struct ocfs2_extent_tree
*et
,
5302 u32 cpos
, u32 len
, handle_t
*handle
,
5303 struct ocfs2_alloc_context
*meta_ac
,
5304 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5307 u32 rec_range
, trunc_range
;
5308 struct ocfs2_extent_rec
*rec
;
5309 struct ocfs2_extent_list
*el
;
5310 struct ocfs2_path
*path
= NULL
;
5312 ocfs2_extent_map_trunc(inode
, 0);
5314 path
= ocfs2_new_path_from_et(et
);
5321 ret
= ocfs2_find_path(inode
, path
, cpos
);
5327 el
= path_leaf_el(path
);
5328 index
= ocfs2_search_extent_list(el
, cpos
);
5329 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5330 ocfs2_error(inode
->i_sb
,
5331 "Inode %llu has an extent at cpos %u which can no "
5332 "longer be found.\n",
5333 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
5339 * We have 3 cases of extent removal:
5340 * 1) Range covers the entire extent rec
5341 * 2) Range begins or ends on one edge of the extent rec
5342 * 3) Range is in the middle of the extent rec (no shared edges)
5344 * For case 1 we remove the extent rec and left rotate to
5347 * For case 2 we just shrink the existing extent rec, with a
5348 * tree update if the shrinking edge is also the edge of an
5351 * For case 3 we do a right split to turn the extent rec into
5352 * something case 2 can handle.
5354 rec
= &el
->l_recs
[index
];
5355 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5356 trunc_range
= cpos
+ len
;
5358 BUG_ON(cpos
< le32_to_cpu(rec
->e_cpos
) || trunc_range
> rec_range
);
5360 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5361 "(cpos %u, len %u)\n",
5362 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
, len
, index
,
5363 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
));
5365 if (le32_to_cpu(rec
->e_cpos
) == cpos
|| rec_range
== trunc_range
) {
5366 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
5373 ret
= ocfs2_split_tree(inode
, et
, handle
, path
, index
,
5374 trunc_range
, meta_ac
);
5381 * The split could have manipulated the tree enough to
5382 * move the record location, so we have to look for it again.
5384 ocfs2_reinit_path(path
, 1);
5386 ret
= ocfs2_find_path(inode
, path
, cpos
);
5392 el
= path_leaf_el(path
);
5393 index
= ocfs2_search_extent_list(el
, cpos
);
5394 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5395 ocfs2_error(inode
->i_sb
,
5396 "Inode %llu: split at cpos %u lost record.",
5397 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5404 * Double check our values here. If anything is fishy,
5405 * it's easier to catch it at the top level.
5407 rec
= &el
->l_recs
[index
];
5408 rec_range
= le32_to_cpu(rec
->e_cpos
) +
5409 ocfs2_rec_clusters(el
, rec
);
5410 if (rec_range
!= trunc_range
) {
5411 ocfs2_error(inode
->i_sb
,
5412 "Inode %llu: error after split at cpos %u"
5413 "trunc len %u, existing record is (%u,%u)",
5414 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5415 cpos
, len
, le32_to_cpu(rec
->e_cpos
),
5416 ocfs2_rec_clusters(el
, rec
));
5421 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
5430 ocfs2_free_path(path
);
5434 int ocfs2_remove_btree_range(struct inode
*inode
,
5435 struct ocfs2_extent_tree
*et
,
5436 u32 cpos
, u32 phys_cpos
, u32 len
,
5437 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5440 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
5441 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5442 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5444 struct ocfs2_alloc_context
*meta_ac
= NULL
;
5446 ret
= ocfs2_lock_allocators(inode
, et
, 0, 1, NULL
, &meta_ac
);
5452 mutex_lock(&tl_inode
->i_mutex
);
5454 if (ocfs2_truncate_log_needs_flush(osb
)) {
5455 ret
= __ocfs2_flush_truncate_log(osb
);
5462 handle
= ocfs2_start_trans(osb
, ocfs2_remove_extent_credits(osb
->sb
));
5463 if (IS_ERR(handle
)) {
5464 ret
= PTR_ERR(handle
);
5469 ret
= ocfs2_et_root_journal_access(handle
, inode
, et
,
5470 OCFS2_JOURNAL_ACCESS_WRITE
);
5476 vfs_dq_free_space_nodirty(inode
,
5477 ocfs2_clusters_to_bytes(inode
->i_sb
, len
));
5479 ret
= ocfs2_remove_extent(inode
, et
, cpos
, len
, handle
, meta_ac
,
5486 ocfs2_et_update_clusters(inode
, et
, -len
);
5488 ret
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
5494 ret
= ocfs2_truncate_log_append(osb
, handle
, phys_blkno
, len
);
5499 ocfs2_commit_trans(osb
, handle
);
5501 mutex_unlock(&tl_inode
->i_mutex
);
5504 ocfs2_free_alloc_context(meta_ac
);
5509 int ocfs2_truncate_log_needs_flush(struct ocfs2_super
*osb
)
5511 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5512 struct ocfs2_dinode
*di
;
5513 struct ocfs2_truncate_log
*tl
;
5515 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5516 tl
= &di
->id2
.i_dealloc
;
5518 mlog_bug_on_msg(le16_to_cpu(tl
->tl_used
) > le16_to_cpu(tl
->tl_count
),
5519 "slot %d, invalid truncate log parameters: used = "
5520 "%u, count = %u\n", osb
->slot_num
,
5521 le16_to_cpu(tl
->tl_used
), le16_to_cpu(tl
->tl_count
));
5522 return le16_to_cpu(tl
->tl_used
) == le16_to_cpu(tl
->tl_count
);
5525 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log
*tl
,
5526 unsigned int new_start
)
5528 unsigned int tail_index
;
5529 unsigned int current_tail
;
5531 /* No records, nothing to coalesce */
5532 if (!le16_to_cpu(tl
->tl_used
))
5535 tail_index
= le16_to_cpu(tl
->tl_used
) - 1;
5536 current_tail
= le32_to_cpu(tl
->tl_recs
[tail_index
].t_start
);
5537 current_tail
+= le32_to_cpu(tl
->tl_recs
[tail_index
].t_clusters
);
5539 return current_tail
== new_start
;
5542 int ocfs2_truncate_log_append(struct ocfs2_super
*osb
,
5545 unsigned int num_clusters
)
5548 unsigned int start_cluster
, tl_count
;
5549 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5550 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5551 struct ocfs2_dinode
*di
;
5552 struct ocfs2_truncate_log
*tl
;
5554 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5555 (unsigned long long)start_blk
, num_clusters
);
5557 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5559 start_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, start_blk
);
5561 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5563 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5564 * by the underlying call to ocfs2_read_inode_block(), so any
5565 * corruption is a code bug */
5566 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5568 tl
= &di
->id2
.i_dealloc
;
5569 tl_count
= le16_to_cpu(tl
->tl_count
);
5570 mlog_bug_on_msg(tl_count
> ocfs2_truncate_recs_per_inode(osb
->sb
) ||
5572 "Truncate record count on #%llu invalid "
5573 "wanted %u, actual %u\n",
5574 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
,
5575 ocfs2_truncate_recs_per_inode(osb
->sb
),
5576 le16_to_cpu(tl
->tl_count
));
5578 /* Caller should have known to flush before calling us. */
5579 index
= le16_to_cpu(tl
->tl_used
);
5580 if (index
>= tl_count
) {
5586 status
= ocfs2_journal_access_di(handle
, tl_inode
, tl_bh
,
5587 OCFS2_JOURNAL_ACCESS_WRITE
);
5593 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5594 "%llu (index = %d)\n", num_clusters
, start_cluster
,
5595 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
, index
);
5597 if (ocfs2_truncate_log_can_coalesce(tl
, start_cluster
)) {
5599 * Move index back to the record we are coalescing with.
5600 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5604 num_clusters
+= le32_to_cpu(tl
->tl_recs
[index
].t_clusters
);
5605 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5606 index
, le32_to_cpu(tl
->tl_recs
[index
].t_start
),
5609 tl
->tl_recs
[index
].t_start
= cpu_to_le32(start_cluster
);
5610 tl
->tl_used
= cpu_to_le16(index
+ 1);
5612 tl
->tl_recs
[index
].t_clusters
= cpu_to_le32(num_clusters
);
5614 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5625 static int ocfs2_replay_truncate_records(struct ocfs2_super
*osb
,
5627 struct inode
*data_alloc_inode
,
5628 struct buffer_head
*data_alloc_bh
)
5632 unsigned int num_clusters
;
5634 struct ocfs2_truncate_rec rec
;
5635 struct ocfs2_dinode
*di
;
5636 struct ocfs2_truncate_log
*tl
;
5637 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5638 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5642 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5643 tl
= &di
->id2
.i_dealloc
;
5644 i
= le16_to_cpu(tl
->tl_used
) - 1;
5646 /* Caller has given us at least enough credits to
5647 * update the truncate log dinode */
5648 status
= ocfs2_journal_access_di(handle
, tl_inode
, tl_bh
,
5649 OCFS2_JOURNAL_ACCESS_WRITE
);
5655 tl
->tl_used
= cpu_to_le16(i
);
5657 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5663 /* TODO: Perhaps we can calculate the bulk of the
5664 * credits up front rather than extending like
5666 status
= ocfs2_extend_trans(handle
,
5667 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC
);
5673 rec
= tl
->tl_recs
[i
];
5674 start_blk
= ocfs2_clusters_to_blocks(data_alloc_inode
->i_sb
,
5675 le32_to_cpu(rec
.t_start
));
5676 num_clusters
= le32_to_cpu(rec
.t_clusters
);
5678 /* if start_blk is not set, we ignore the record as
5681 mlog(0, "free record %d, start = %u, clusters = %u\n",
5682 i
, le32_to_cpu(rec
.t_start
), num_clusters
);
5684 status
= ocfs2_free_clusters(handle
, data_alloc_inode
,
5685 data_alloc_bh
, start_blk
,
5700 /* Expects you to already be holding tl_inode->i_mutex */
5701 int __ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5704 unsigned int num_to_flush
;
5706 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5707 struct inode
*data_alloc_inode
= NULL
;
5708 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5709 struct buffer_head
*data_alloc_bh
= NULL
;
5710 struct ocfs2_dinode
*di
;
5711 struct ocfs2_truncate_log
*tl
;
5715 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5717 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5719 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5720 * by the underlying call to ocfs2_read_inode_block(), so any
5721 * corruption is a code bug */
5722 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5724 tl
= &di
->id2
.i_dealloc
;
5725 num_to_flush
= le16_to_cpu(tl
->tl_used
);
5726 mlog(0, "Flush %u records from truncate log #%llu\n",
5727 num_to_flush
, (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
);
5728 if (!num_to_flush
) {
5733 data_alloc_inode
= ocfs2_get_system_file_inode(osb
,
5734 GLOBAL_BITMAP_SYSTEM_INODE
,
5735 OCFS2_INVALID_SLOT
);
5736 if (!data_alloc_inode
) {
5738 mlog(ML_ERROR
, "Could not get bitmap inode!\n");
5742 mutex_lock(&data_alloc_inode
->i_mutex
);
5744 status
= ocfs2_inode_lock(data_alloc_inode
, &data_alloc_bh
, 1);
5750 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5751 if (IS_ERR(handle
)) {
5752 status
= PTR_ERR(handle
);
5757 status
= ocfs2_replay_truncate_records(osb
, handle
, data_alloc_inode
,
5762 ocfs2_commit_trans(osb
, handle
);
5765 brelse(data_alloc_bh
);
5766 ocfs2_inode_unlock(data_alloc_inode
, 1);
5769 mutex_unlock(&data_alloc_inode
->i_mutex
);
5770 iput(data_alloc_inode
);
5777 int ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5780 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5782 mutex_lock(&tl_inode
->i_mutex
);
5783 status
= __ocfs2_flush_truncate_log(osb
);
5784 mutex_unlock(&tl_inode
->i_mutex
);
5789 static void ocfs2_truncate_log_worker(struct work_struct
*work
)
5792 struct ocfs2_super
*osb
=
5793 container_of(work
, struct ocfs2_super
,
5794 osb_truncate_log_wq
.work
);
5798 status
= ocfs2_flush_truncate_log(osb
);
5802 ocfs2_init_inode_steal_slot(osb
);
5807 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5808 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super
*osb
,
5811 if (osb
->osb_tl_inode
) {
5812 /* We want to push off log flushes while truncates are
5815 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5817 queue_delayed_work(ocfs2_wq
, &osb
->osb_truncate_log_wq
,
5818 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL
);
5822 static int ocfs2_get_truncate_log_info(struct ocfs2_super
*osb
,
5824 struct inode
**tl_inode
,
5825 struct buffer_head
**tl_bh
)
5828 struct inode
*inode
= NULL
;
5829 struct buffer_head
*bh
= NULL
;
5831 inode
= ocfs2_get_system_file_inode(osb
,
5832 TRUNCATE_LOG_SYSTEM_INODE
,
5836 mlog(ML_ERROR
, "Could not get load truncate log inode!\n");
5840 status
= ocfs2_read_inode_block(inode
, &bh
);
5854 /* called during the 1st stage of node recovery. we stamp a clean
5855 * truncate log and pass back a copy for processing later. if the
5856 * truncate log does not require processing, a *tl_copy is set to
5858 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super
*osb
,
5860 struct ocfs2_dinode
**tl_copy
)
5863 struct inode
*tl_inode
= NULL
;
5864 struct buffer_head
*tl_bh
= NULL
;
5865 struct ocfs2_dinode
*di
;
5866 struct ocfs2_truncate_log
*tl
;
5870 mlog(0, "recover truncate log from slot %d\n", slot_num
);
5872 status
= ocfs2_get_truncate_log_info(osb
, slot_num
, &tl_inode
, &tl_bh
);
5878 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5880 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5881 * validated by the underlying call to ocfs2_read_inode_block(),
5882 * so any corruption is a code bug */
5883 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5885 tl
= &di
->id2
.i_dealloc
;
5886 if (le16_to_cpu(tl
->tl_used
)) {
5887 mlog(0, "We'll have %u logs to recover\n",
5888 le16_to_cpu(tl
->tl_used
));
5890 *tl_copy
= kmalloc(tl_bh
->b_size
, GFP_KERNEL
);
5897 /* Assuming the write-out below goes well, this copy
5898 * will be passed back to recovery for processing. */
5899 memcpy(*tl_copy
, tl_bh
->b_data
, tl_bh
->b_size
);
5901 /* All we need to do to clear the truncate log is set
5905 ocfs2_compute_meta_ecc(osb
->sb
, tl_bh
->b_data
, &di
->i_check
);
5906 status
= ocfs2_write_block(osb
, tl_bh
, tl_inode
);
5918 if (status
< 0 && (*tl_copy
)) {
5927 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super
*osb
,
5928 struct ocfs2_dinode
*tl_copy
)
5932 unsigned int clusters
, num_recs
, start_cluster
;
5935 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5936 struct ocfs2_truncate_log
*tl
;
5940 if (OCFS2_I(tl_inode
)->ip_blkno
== le64_to_cpu(tl_copy
->i_blkno
)) {
5941 mlog(ML_ERROR
, "Asked to recover my own truncate log!\n");
5945 tl
= &tl_copy
->id2
.i_dealloc
;
5946 num_recs
= le16_to_cpu(tl
->tl_used
);
5947 mlog(0, "cleanup %u records from %llu\n", num_recs
,
5948 (unsigned long long)le64_to_cpu(tl_copy
->i_blkno
));
5950 mutex_lock(&tl_inode
->i_mutex
);
5951 for(i
= 0; i
< num_recs
; i
++) {
5952 if (ocfs2_truncate_log_needs_flush(osb
)) {
5953 status
= __ocfs2_flush_truncate_log(osb
);
5960 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5961 if (IS_ERR(handle
)) {
5962 status
= PTR_ERR(handle
);
5967 clusters
= le32_to_cpu(tl
->tl_recs
[i
].t_clusters
);
5968 start_cluster
= le32_to_cpu(tl
->tl_recs
[i
].t_start
);
5969 start_blk
= ocfs2_clusters_to_blocks(osb
->sb
, start_cluster
);
5971 status
= ocfs2_truncate_log_append(osb
, handle
,
5972 start_blk
, clusters
);
5973 ocfs2_commit_trans(osb
, handle
);
5981 mutex_unlock(&tl_inode
->i_mutex
);
5987 void ocfs2_truncate_log_shutdown(struct ocfs2_super
*osb
)
5990 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5995 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5996 flush_workqueue(ocfs2_wq
);
5998 status
= ocfs2_flush_truncate_log(osb
);
6002 brelse(osb
->osb_tl_bh
);
6003 iput(osb
->osb_tl_inode
);
6009 int ocfs2_truncate_log_init(struct ocfs2_super
*osb
)
6012 struct inode
*tl_inode
= NULL
;
6013 struct buffer_head
*tl_bh
= NULL
;
6017 status
= ocfs2_get_truncate_log_info(osb
,
6024 /* ocfs2_truncate_log_shutdown keys on the existence of
6025 * osb->osb_tl_inode so we don't set any of the osb variables
6026 * until we're sure all is well. */
6027 INIT_DELAYED_WORK(&osb
->osb_truncate_log_wq
,
6028 ocfs2_truncate_log_worker
);
6029 osb
->osb_tl_bh
= tl_bh
;
6030 osb
->osb_tl_inode
= tl_inode
;
6037 * Delayed de-allocation of suballocator blocks.
6039 * Some sets of block de-allocations might involve multiple suballocator inodes.
6041 * The locking for this can get extremely complicated, especially when
6042 * the suballocator inodes to delete from aren't known until deep
6043 * within an unrelated codepath.
6045 * ocfs2_extent_block structures are a good example of this - an inode
6046 * btree could have been grown by any number of nodes each allocating
6047 * out of their own suballoc inode.
6049 * These structures allow the delay of block de-allocation until a
6050 * later time, when locking of multiple cluster inodes won't cause
6055 * Describe a single bit freed from a suballocator. For the block
6056 * suballocators, it represents one block. For the global cluster
6057 * allocator, it represents some clusters and free_bit indicates
6060 struct ocfs2_cached_block_free
{
6061 struct ocfs2_cached_block_free
*free_next
;
6063 unsigned int free_bit
;
6066 struct ocfs2_per_slot_free_list
{
6067 struct ocfs2_per_slot_free_list
*f_next_suballocator
;
6070 struct ocfs2_cached_block_free
*f_first
;
6073 static int ocfs2_free_cached_blocks(struct ocfs2_super
*osb
,
6076 struct ocfs2_cached_block_free
*head
)
6081 struct inode
*inode
;
6082 struct buffer_head
*di_bh
= NULL
;
6083 struct ocfs2_cached_block_free
*tmp
;
6085 inode
= ocfs2_get_system_file_inode(osb
, sysfile_type
, slot
);
6092 mutex_lock(&inode
->i_mutex
);
6094 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
6100 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
6101 if (IS_ERR(handle
)) {
6102 ret
= PTR_ERR(handle
);
6108 bg_blkno
= ocfs2_which_suballoc_group(head
->free_blk
,
6110 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6111 head
->free_bit
, (unsigned long long)head
->free_blk
);
6113 ret
= ocfs2_free_suballoc_bits(handle
, inode
, di_bh
,
6114 head
->free_bit
, bg_blkno
, 1);
6120 ret
= ocfs2_extend_trans(handle
, OCFS2_SUBALLOC_FREE
);
6127 head
= head
->free_next
;
6132 ocfs2_commit_trans(osb
, handle
);
6135 ocfs2_inode_unlock(inode
, 1);
6138 mutex_unlock(&inode
->i_mutex
);
6142 /* Premature exit may have left some dangling items. */
6144 head
= head
->free_next
;
6151 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6152 u64 blkno
, unsigned int bit
)
6155 struct ocfs2_cached_block_free
*item
;
6157 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6164 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6165 bit
, (unsigned long long)blkno
);
6167 item
->free_blk
= blkno
;
6168 item
->free_bit
= bit
;
6169 item
->free_next
= ctxt
->c_global_allocator
;
6171 ctxt
->c_global_allocator
= item
;
6175 static int ocfs2_free_cached_clusters(struct ocfs2_super
*osb
,
6176 struct ocfs2_cached_block_free
*head
)
6178 struct ocfs2_cached_block_free
*tmp
;
6179 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6183 mutex_lock(&tl_inode
->i_mutex
);
6186 if (ocfs2_truncate_log_needs_flush(osb
)) {
6187 ret
= __ocfs2_flush_truncate_log(osb
);
6194 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
6195 if (IS_ERR(handle
)) {
6196 ret
= PTR_ERR(handle
);
6201 ret
= ocfs2_truncate_log_append(osb
, handle
, head
->free_blk
,
6204 ocfs2_commit_trans(osb
, handle
);
6206 head
= head
->free_next
;
6215 mutex_unlock(&tl_inode
->i_mutex
);
6218 /* Premature exit may have left some dangling items. */
6220 head
= head
->free_next
;
6227 int ocfs2_run_deallocs(struct ocfs2_super
*osb
,
6228 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
6231 struct ocfs2_per_slot_free_list
*fl
;
6236 while (ctxt
->c_first_suballocator
) {
6237 fl
= ctxt
->c_first_suballocator
;
6240 mlog(0, "Free items: (type %u, slot %d)\n",
6241 fl
->f_inode_type
, fl
->f_slot
);
6242 ret2
= ocfs2_free_cached_blocks(osb
,
6252 ctxt
->c_first_suballocator
= fl
->f_next_suballocator
;
6256 if (ctxt
->c_global_allocator
) {
6257 ret2
= ocfs2_free_cached_clusters(osb
,
6258 ctxt
->c_global_allocator
);
6264 ctxt
->c_global_allocator
= NULL
;
6270 static struct ocfs2_per_slot_free_list
*
6271 ocfs2_find_per_slot_free_list(int type
,
6273 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
6275 struct ocfs2_per_slot_free_list
*fl
= ctxt
->c_first_suballocator
;
6278 if (fl
->f_inode_type
== type
&& fl
->f_slot
== slot
)
6281 fl
= fl
->f_next_suballocator
;
6284 fl
= kmalloc(sizeof(*fl
), GFP_NOFS
);
6286 fl
->f_inode_type
= type
;
6289 fl
->f_next_suballocator
= ctxt
->c_first_suballocator
;
6291 ctxt
->c_first_suballocator
= fl
;
6296 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6297 int type
, int slot
, u64 blkno
,
6301 struct ocfs2_per_slot_free_list
*fl
;
6302 struct ocfs2_cached_block_free
*item
;
6304 fl
= ocfs2_find_per_slot_free_list(type
, slot
, ctxt
);
6311 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6318 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6319 type
, slot
, bit
, (unsigned long long)blkno
);
6321 item
->free_blk
= blkno
;
6322 item
->free_bit
= bit
;
6323 item
->free_next
= fl
->f_first
;
6332 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6333 struct ocfs2_extent_block
*eb
)
6335 return ocfs2_cache_block_dealloc(ctxt
, EXTENT_ALLOC_SYSTEM_INODE
,
6336 le16_to_cpu(eb
->h_suballoc_slot
),
6337 le64_to_cpu(eb
->h_blkno
),
6338 le16_to_cpu(eb
->h_suballoc_bit
));
6341 /* This function will figure out whether the currently last extent
6342 * block will be deleted, and if it will, what the new last extent
6343 * block will be so we can update his h_next_leaf_blk field, as well
6344 * as the dinodes i_last_eb_blk */
6345 static int ocfs2_find_new_last_ext_blk(struct inode
*inode
,
6346 unsigned int clusters_to_del
,
6347 struct ocfs2_path
*path
,
6348 struct buffer_head
**new_last_eb
)
6350 int next_free
, ret
= 0;
6352 struct ocfs2_extent_rec
*rec
;
6353 struct ocfs2_extent_block
*eb
;
6354 struct ocfs2_extent_list
*el
;
6355 struct buffer_head
*bh
= NULL
;
6357 *new_last_eb
= NULL
;
6359 /* we have no tree, so of course, no last_eb. */
6360 if (!path
->p_tree_depth
)
6363 /* trunc to zero special case - this makes tree_depth = 0
6364 * regardless of what it is. */
6365 if (OCFS2_I(inode
)->ip_clusters
== clusters_to_del
)
6368 el
= path_leaf_el(path
);
6369 BUG_ON(!el
->l_next_free_rec
);
6372 * Make sure that this extent list will actually be empty
6373 * after we clear away the data. We can shortcut out if
6374 * there's more than one non-empty extent in the
6375 * list. Otherwise, a check of the remaining extent is
6378 next_free
= le16_to_cpu(el
->l_next_free_rec
);
6380 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6384 /* We may have a valid extent in index 1, check it. */
6386 rec
= &el
->l_recs
[1];
6389 * Fall through - no more nonempty extents, so we want
6390 * to delete this leaf.
6396 rec
= &el
->l_recs
[0];
6401 * Check it we'll only be trimming off the end of this
6404 if (le16_to_cpu(rec
->e_leaf_clusters
) > clusters_to_del
)
6408 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
6414 ret
= ocfs2_find_leaf(inode
, path_root_el(path
), cpos
, &bh
);
6420 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
6423 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6424 * Any corruption is a code bug. */
6425 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
6428 get_bh(*new_last_eb
);
6429 mlog(0, "returning block %llu, (cpos: %u)\n",
6430 (unsigned long long)le64_to_cpu(eb
->h_blkno
), cpos
);
6438 * Trim some clusters off the rightmost edge of a tree. Only called
6441 * The caller needs to:
6442 * - start journaling of each path component.
6443 * - compute and fully set up any new last ext block
6445 static int ocfs2_trim_tree(struct inode
*inode
, struct ocfs2_path
*path
,
6446 handle_t
*handle
, struct ocfs2_truncate_context
*tc
,
6447 u32 clusters_to_del
, u64
*delete_start
)
6449 int ret
, i
, index
= path
->p_tree_depth
;
6452 struct buffer_head
*bh
;
6453 struct ocfs2_extent_list
*el
;
6454 struct ocfs2_extent_rec
*rec
;
6458 while (index
>= 0) {
6459 bh
= path
->p_node
[index
].bh
;
6460 el
= path
->p_node
[index
].el
;
6462 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6463 index
, (unsigned long long)bh
->b_blocknr
);
6465 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
6468 (path
->p_tree_depth
- le16_to_cpu(el
->l_tree_depth
))) {
6469 ocfs2_error(inode
->i_sb
,
6470 "Inode %lu has invalid ext. block %llu",
6472 (unsigned long long)bh
->b_blocknr
);
6478 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
6479 rec
= &el
->l_recs
[i
];
6481 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6482 "next = %u\n", i
, le32_to_cpu(rec
->e_cpos
),
6483 ocfs2_rec_clusters(el
, rec
),
6484 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6485 le16_to_cpu(el
->l_next_free_rec
));
6487 BUG_ON(ocfs2_rec_clusters(el
, rec
) < clusters_to_del
);
6489 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
6491 * If the leaf block contains a single empty
6492 * extent and no records, we can just remove
6495 if (i
== 0 && ocfs2_is_empty_extent(rec
)) {
6497 sizeof(struct ocfs2_extent_rec
));
6498 el
->l_next_free_rec
= cpu_to_le16(0);
6504 * Remove any empty extents by shifting things
6505 * left. That should make life much easier on
6506 * the code below. This condition is rare
6507 * enough that we shouldn't see a performance
6510 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6511 le16_add_cpu(&el
->l_next_free_rec
, -1);
6514 i
< le16_to_cpu(el
->l_next_free_rec
); i
++)
6515 el
->l_recs
[i
] = el
->l_recs
[i
+ 1];
6517 memset(&el
->l_recs
[i
], 0,
6518 sizeof(struct ocfs2_extent_rec
));
6521 * We've modified our extent list. The
6522 * simplest way to handle this change
6523 * is to being the search from the
6526 goto find_tail_record
;
6529 le16_add_cpu(&rec
->e_leaf_clusters
, -clusters_to_del
);
6532 * We'll use "new_edge" on our way back up the
6533 * tree to know what our rightmost cpos is.
6535 new_edge
= le16_to_cpu(rec
->e_leaf_clusters
);
6536 new_edge
+= le32_to_cpu(rec
->e_cpos
);
6539 * The caller will use this to delete data blocks.
6541 *delete_start
= le64_to_cpu(rec
->e_blkno
)
6542 + ocfs2_clusters_to_blocks(inode
->i_sb
,
6543 le16_to_cpu(rec
->e_leaf_clusters
));
6546 * If it's now empty, remove this record.
6548 if (le16_to_cpu(rec
->e_leaf_clusters
) == 0) {
6550 sizeof(struct ocfs2_extent_rec
));
6551 le16_add_cpu(&el
->l_next_free_rec
, -1);
6554 if (le64_to_cpu(rec
->e_blkno
) == deleted_eb
) {
6556 sizeof(struct ocfs2_extent_rec
));
6557 le16_add_cpu(&el
->l_next_free_rec
, -1);
6562 /* Can this actually happen? */
6563 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
6567 * We never actually deleted any clusters
6568 * because our leaf was empty. There's no
6569 * reason to adjust the rightmost edge then.
6574 rec
->e_int_clusters
= cpu_to_le32(new_edge
);
6575 le32_add_cpu(&rec
->e_int_clusters
,
6576 -le32_to_cpu(rec
->e_cpos
));
6579 * A deleted child record should have been
6582 BUG_ON(le32_to_cpu(rec
->e_int_clusters
) == 0);
6586 ret
= ocfs2_journal_dirty(handle
, bh
);
6592 mlog(0, "extent list container %llu, after: record %d: "
6593 "(%u, %u, %llu), next = %u.\n",
6594 (unsigned long long)bh
->b_blocknr
, i
,
6595 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
),
6596 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6597 le16_to_cpu(el
->l_next_free_rec
));
6600 * We must be careful to only attempt delete of an
6601 * extent block (and not the root inode block).
6603 if (index
> 0 && le16_to_cpu(el
->l_next_free_rec
) == 0) {
6604 struct ocfs2_extent_block
*eb
=
6605 (struct ocfs2_extent_block
*)bh
->b_data
;
6608 * Save this for use when processing the
6611 deleted_eb
= le64_to_cpu(eb
->h_blkno
);
6613 mlog(0, "deleting this extent block.\n");
6615 ocfs2_remove_from_cache(inode
, bh
);
6617 BUG_ON(ocfs2_rec_clusters(el
, &el
->l_recs
[0]));
6618 BUG_ON(le32_to_cpu(el
->l_recs
[0].e_cpos
));
6619 BUG_ON(le64_to_cpu(el
->l_recs
[0].e_blkno
));
6621 ret
= ocfs2_cache_extent_block_free(&tc
->tc_dealloc
, eb
);
6622 /* An error here is not fatal. */
6637 static int ocfs2_do_truncate(struct ocfs2_super
*osb
,
6638 unsigned int clusters_to_del
,
6639 struct inode
*inode
,
6640 struct buffer_head
*fe_bh
,
6642 struct ocfs2_truncate_context
*tc
,
6643 struct ocfs2_path
*path
)
6646 struct ocfs2_dinode
*fe
;
6647 struct ocfs2_extent_block
*last_eb
= NULL
;
6648 struct ocfs2_extent_list
*el
;
6649 struct buffer_head
*last_eb_bh
= NULL
;
6652 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
6654 status
= ocfs2_find_new_last_ext_blk(inode
, clusters_to_del
,
6662 * Each component will be touched, so we might as well journal
6663 * here to avoid having to handle errors later.
6665 status
= ocfs2_journal_access_path(inode
, handle
, path
);
6672 status
= ocfs2_journal_access_eb(handle
, inode
, last_eb_bh
,
6673 OCFS2_JOURNAL_ACCESS_WRITE
);
6679 last_eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
6682 el
= &(fe
->id2
.i_list
);
6685 * Lower levels depend on this never happening, but it's best
6686 * to check it up here before changing the tree.
6688 if (el
->l_tree_depth
&& el
->l_recs
[0].e_int_clusters
== 0) {
6689 ocfs2_error(inode
->i_sb
,
6690 "Inode %lu has an empty extent record, depth %u\n",
6691 inode
->i_ino
, le16_to_cpu(el
->l_tree_depth
));
6696 vfs_dq_free_space_nodirty(inode
,
6697 ocfs2_clusters_to_bytes(osb
->sb
, clusters_to_del
));
6698 spin_lock(&OCFS2_I(inode
)->ip_lock
);
6699 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
) -
6701 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
6702 le32_add_cpu(&fe
->i_clusters
, -clusters_to_del
);
6703 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
6705 status
= ocfs2_trim_tree(inode
, path
, handle
, tc
,
6706 clusters_to_del
, &delete_blk
);
6712 if (le32_to_cpu(fe
->i_clusters
) == 0) {
6713 /* trunc to zero is a special case. */
6714 el
->l_tree_depth
= 0;
6715 fe
->i_last_eb_blk
= 0;
6717 fe
->i_last_eb_blk
= last_eb
->h_blkno
;
6719 status
= ocfs2_journal_dirty(handle
, fe_bh
);
6726 /* If there will be a new last extent block, then by
6727 * definition, there cannot be any leaves to the right of
6729 last_eb
->h_next_leaf_blk
= 0;
6730 status
= ocfs2_journal_dirty(handle
, last_eb_bh
);
6738 status
= ocfs2_truncate_log_append(osb
, handle
, delete_blk
,
6752 static int ocfs2_zero_func(handle_t
*handle
, struct buffer_head
*bh
)
6754 set_buffer_uptodate(bh
);
6755 mark_buffer_dirty(bh
);
6759 static void ocfs2_map_and_dirty_page(struct inode
*inode
, handle_t
*handle
,
6760 unsigned int from
, unsigned int to
,
6761 struct page
*page
, int zero
, u64
*phys
)
6763 int ret
, partial
= 0;
6765 ret
= ocfs2_map_page_blocks(page
, phys
, inode
, from
, to
, 0);
6770 zero_user_segment(page
, from
, to
);
6773 * Need to set the buffers we zero'd into uptodate
6774 * here if they aren't - ocfs2_map_page_blocks()
6775 * might've skipped some
6777 ret
= walk_page_buffers(handle
, page_buffers(page
),
6782 else if (ocfs2_should_order_data(inode
)) {
6783 ret
= ocfs2_jbd2_file_inode(handle
, inode
);
6789 SetPageUptodate(page
);
6791 flush_dcache_page(page
);
6794 static void ocfs2_zero_cluster_pages(struct inode
*inode
, loff_t start
,
6795 loff_t end
, struct page
**pages
,
6796 int numpages
, u64 phys
, handle_t
*handle
)
6800 unsigned int from
, to
= PAGE_CACHE_SIZE
;
6801 struct super_block
*sb
= inode
->i_sb
;
6803 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb
)));
6808 to
= PAGE_CACHE_SIZE
;
6809 for(i
= 0; i
< numpages
; i
++) {
6812 from
= start
& (PAGE_CACHE_SIZE
- 1);
6813 if ((end
>> PAGE_CACHE_SHIFT
) == page
->index
)
6814 to
= end
& (PAGE_CACHE_SIZE
- 1);
6816 BUG_ON(from
> PAGE_CACHE_SIZE
);
6817 BUG_ON(to
> PAGE_CACHE_SIZE
);
6819 ocfs2_map_and_dirty_page(inode
, handle
, from
, to
, page
, 1,
6822 start
= (page
->index
+ 1) << PAGE_CACHE_SHIFT
;
6826 ocfs2_unlock_and_free_pages(pages
, numpages
);
6829 static int ocfs2_grab_eof_pages(struct inode
*inode
, loff_t start
, loff_t end
,
6830 struct page
**pages
, int *num
)
6832 int numpages
, ret
= 0;
6833 struct super_block
*sb
= inode
->i_sb
;
6834 struct address_space
*mapping
= inode
->i_mapping
;
6835 unsigned long index
;
6836 loff_t last_page_bytes
;
6838 BUG_ON(start
> end
);
6840 BUG_ON(start
>> OCFS2_SB(sb
)->s_clustersize_bits
!=
6841 (end
- 1) >> OCFS2_SB(sb
)->s_clustersize_bits
);
6844 last_page_bytes
= PAGE_ALIGN(end
);
6845 index
= start
>> PAGE_CACHE_SHIFT
;
6847 pages
[numpages
] = grab_cache_page(mapping
, index
);
6848 if (!pages
[numpages
]) {
6856 } while (index
< (last_page_bytes
>> PAGE_CACHE_SHIFT
));
6861 ocfs2_unlock_and_free_pages(pages
, numpages
);
6871 * Zero the area past i_size but still within an allocated
6872 * cluster. This avoids exposing nonzero data on subsequent file
6875 * We need to call this before i_size is updated on the inode because
6876 * otherwise block_write_full_page() will skip writeout of pages past
6877 * i_size. The new_i_size parameter is passed for this reason.
6879 int ocfs2_zero_range_for_truncate(struct inode
*inode
, handle_t
*handle
,
6880 u64 range_start
, u64 range_end
)
6882 int ret
= 0, numpages
;
6883 struct page
**pages
= NULL
;
6885 unsigned int ext_flags
;
6886 struct super_block
*sb
= inode
->i_sb
;
6889 * File systems which don't support sparse files zero on every
6892 if (!ocfs2_sparse_alloc(OCFS2_SB(sb
)))
6895 pages
= kcalloc(ocfs2_pages_per_cluster(sb
),
6896 sizeof(struct page
*), GFP_NOFS
);
6897 if (pages
== NULL
) {
6903 if (range_start
== range_end
)
6906 ret
= ocfs2_extent_map_get_blocks(inode
,
6907 range_start
>> sb
->s_blocksize_bits
,
6908 &phys
, NULL
, &ext_flags
);
6915 * Tail is a hole, or is marked unwritten. In either case, we
6916 * can count on read and write to return/push zero's.
6918 if (phys
== 0 || ext_flags
& OCFS2_EXT_UNWRITTEN
)
6921 ret
= ocfs2_grab_eof_pages(inode
, range_start
, range_end
, pages
,
6928 ocfs2_zero_cluster_pages(inode
, range_start
, range_end
, pages
,
6929 numpages
, phys
, handle
);
6932 * Initiate writeout of the pages we zero'd here. We don't
6933 * wait on them - the truncate_inode_pages() call later will
6936 ret
= do_sync_mapping_range(inode
->i_mapping
, range_start
,
6937 range_end
- 1, SYNC_FILE_RANGE_WRITE
);
6948 static void ocfs2_zero_dinode_id2_with_xattr(struct inode
*inode
,
6949 struct ocfs2_dinode
*di
)
6951 unsigned int blocksize
= 1 << inode
->i_sb
->s_blocksize_bits
;
6952 unsigned int xattrsize
= le16_to_cpu(di
->i_xattr_inline_size
);
6954 if (le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_XATTR_FL
)
6955 memset(&di
->id2
, 0, blocksize
-
6956 offsetof(struct ocfs2_dinode
, id2
) -
6959 memset(&di
->id2
, 0, blocksize
-
6960 offsetof(struct ocfs2_dinode
, id2
));
6963 void ocfs2_dinode_new_extent_list(struct inode
*inode
,
6964 struct ocfs2_dinode
*di
)
6966 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
6967 di
->id2
.i_list
.l_tree_depth
= 0;
6968 di
->id2
.i_list
.l_next_free_rec
= 0;
6969 di
->id2
.i_list
.l_count
= cpu_to_le16(
6970 ocfs2_extent_recs_per_inode_with_xattr(inode
->i_sb
, di
));
6973 void ocfs2_set_inode_data_inline(struct inode
*inode
, struct ocfs2_dinode
*di
)
6975 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6976 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
6978 spin_lock(&oi
->ip_lock
);
6979 oi
->ip_dyn_features
|= OCFS2_INLINE_DATA_FL
;
6980 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
6981 spin_unlock(&oi
->ip_lock
);
6984 * We clear the entire i_data structure here so that all
6985 * fields can be properly initialized.
6987 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
6989 idata
->id_count
= cpu_to_le16(
6990 ocfs2_max_inline_data_with_xattr(inode
->i_sb
, di
));
6993 int ocfs2_convert_inline_data_to_extents(struct inode
*inode
,
6994 struct buffer_head
*di_bh
)
6996 int ret
, i
, has_data
, num_pages
= 0;
6998 u64
uninitialized_var(block
);
6999 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
7000 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
7001 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
7002 struct ocfs2_alloc_context
*data_ac
= NULL
;
7003 struct page
**pages
= NULL
;
7004 loff_t end
= osb
->s_clustersize
;
7005 struct ocfs2_extent_tree et
;
7008 has_data
= i_size_read(inode
) ? 1 : 0;
7011 pages
= kcalloc(ocfs2_pages_per_cluster(osb
->sb
),
7012 sizeof(struct page
*), GFP_NOFS
);
7013 if (pages
== NULL
) {
7019 ret
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
7026 handle
= ocfs2_start_trans(osb
,
7027 ocfs2_inline_to_extents_credits(osb
->sb
));
7028 if (IS_ERR(handle
)) {
7029 ret
= PTR_ERR(handle
);
7034 ret
= ocfs2_journal_access_di(handle
, inode
, di_bh
,
7035 OCFS2_JOURNAL_ACCESS_WRITE
);
7043 unsigned int page_end
;
7046 if (vfs_dq_alloc_space_nodirty(inode
,
7047 ocfs2_clusters_to_bytes(osb
->sb
, 1))) {
7053 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
,
7061 * Save two copies, one for insert, and one that can
7062 * be changed by ocfs2_map_and_dirty_page() below.
7064 block
= phys
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
7067 * Non sparse file systems zero on extend, so no need
7070 if (!ocfs2_sparse_alloc(osb
) &&
7071 PAGE_CACHE_SIZE
< osb
->s_clustersize
)
7072 end
= PAGE_CACHE_SIZE
;
7074 ret
= ocfs2_grab_eof_pages(inode
, 0, end
, pages
, &num_pages
);
7081 * This should populate the 1st page for us and mark
7084 ret
= ocfs2_read_inline_data(inode
, pages
[0], di_bh
);
7090 page_end
= PAGE_CACHE_SIZE
;
7091 if (PAGE_CACHE_SIZE
> osb
->s_clustersize
)
7092 page_end
= osb
->s_clustersize
;
7094 for (i
= 0; i
< num_pages
; i
++)
7095 ocfs2_map_and_dirty_page(inode
, handle
, 0, page_end
,
7096 pages
[i
], i
> 0, &phys
);
7099 spin_lock(&oi
->ip_lock
);
7100 oi
->ip_dyn_features
&= ~OCFS2_INLINE_DATA_FL
;
7101 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
7102 spin_unlock(&oi
->ip_lock
);
7104 ocfs2_dinode_new_extent_list(inode
, di
);
7106 ocfs2_journal_dirty(handle
, di_bh
);
7110 * An error at this point should be extremely rare. If
7111 * this proves to be false, we could always re-build
7112 * the in-inode data from our pages.
7114 ocfs2_init_dinode_extent_tree(&et
, inode
, di_bh
);
7115 ret
= ocfs2_insert_extent(osb
, handle
, inode
, &et
,
7116 0, block
, 1, 0, NULL
);
7122 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
7126 if (ret
< 0 && did_quota
)
7127 vfs_dq_free_space_nodirty(inode
,
7128 ocfs2_clusters_to_bytes(osb
->sb
, 1));
7130 ocfs2_commit_trans(osb
, handle
);
7134 ocfs2_free_alloc_context(data_ac
);
7138 ocfs2_unlock_and_free_pages(pages
, num_pages
);
7146 * It is expected, that by the time you call this function,
7147 * inode->i_size and fe->i_size have been adjusted.
7149 * WARNING: This will kfree the truncate context
7151 int ocfs2_commit_truncate(struct ocfs2_super
*osb
,
7152 struct inode
*inode
,
7153 struct buffer_head
*fe_bh
,
7154 struct ocfs2_truncate_context
*tc
)
7156 int status
, i
, credits
, tl_sem
= 0;
7157 u32 clusters_to_del
, new_highest_cpos
, range
;
7158 struct ocfs2_extent_list
*el
;
7159 handle_t
*handle
= NULL
;
7160 struct inode
*tl_inode
= osb
->osb_tl_inode
;
7161 struct ocfs2_path
*path
= NULL
;
7162 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
7166 new_highest_cpos
= ocfs2_clusters_for_bytes(osb
->sb
,
7167 i_size_read(inode
));
7169 path
= ocfs2_new_path(fe_bh
, &di
->id2
.i_list
,
7170 ocfs2_journal_access_di
);
7177 ocfs2_extent_map_trunc(inode
, new_highest_cpos
);
7181 * Check that we still have allocation to delete.
7183 if (OCFS2_I(inode
)->ip_clusters
== 0) {
7189 * Truncate always works against the rightmost tree branch.
7191 status
= ocfs2_find_path(inode
, path
, UINT_MAX
);
7197 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7198 OCFS2_I(inode
)->ip_clusters
, path
->p_tree_depth
);
7201 * By now, el will point to the extent list on the bottom most
7202 * portion of this tree. Only the tail record is considered in
7205 * We handle the following cases, in order:
7206 * - empty extent: delete the remaining branch
7207 * - remove the entire record
7208 * - remove a partial record
7209 * - no record needs to be removed (truncate has completed)
7211 el
= path_leaf_el(path
);
7212 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
7213 ocfs2_error(inode
->i_sb
,
7214 "Inode %llu has empty extent block at %llu\n",
7215 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7216 (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
7221 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
7222 range
= le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
7223 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
7224 if (i
== 0 && ocfs2_is_empty_extent(&el
->l_recs
[i
])) {
7225 clusters_to_del
= 0;
7226 } else if (le32_to_cpu(el
->l_recs
[i
].e_cpos
) >= new_highest_cpos
) {
7227 clusters_to_del
= ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
7228 } else if (range
> new_highest_cpos
) {
7229 clusters_to_del
= (ocfs2_rec_clusters(el
, &el
->l_recs
[i
]) +
7230 le32_to_cpu(el
->l_recs
[i
].e_cpos
)) -
7237 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7238 clusters_to_del
, (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
7240 mutex_lock(&tl_inode
->i_mutex
);
7242 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7243 * record is free for use. If there isn't any, we flush to get
7244 * an empty truncate log. */
7245 if (ocfs2_truncate_log_needs_flush(osb
)) {
7246 status
= __ocfs2_flush_truncate_log(osb
);
7253 credits
= ocfs2_calc_tree_trunc_credits(osb
->sb
, clusters_to_del
,
7254 (struct ocfs2_dinode
*)fe_bh
->b_data
,
7256 handle
= ocfs2_start_trans(osb
, credits
);
7257 if (IS_ERR(handle
)) {
7258 status
= PTR_ERR(handle
);
7264 status
= ocfs2_do_truncate(osb
, clusters_to_del
, inode
, fe_bh
, handle
,
7271 mutex_unlock(&tl_inode
->i_mutex
);
7274 ocfs2_commit_trans(osb
, handle
);
7277 ocfs2_reinit_path(path
, 1);
7280 * The check above will catch the case where we've truncated
7281 * away all allocation.
7287 ocfs2_schedule_truncate_log_flush(osb
, 1);
7290 mutex_unlock(&tl_inode
->i_mutex
);
7293 ocfs2_commit_trans(osb
, handle
);
7295 ocfs2_run_deallocs(osb
, &tc
->tc_dealloc
);
7297 ocfs2_free_path(path
);
7299 /* This will drop the ext_alloc cluster lock for us */
7300 ocfs2_free_truncate_context(tc
);
7307 * Expects the inode to already be locked.
7309 int ocfs2_prepare_truncate(struct ocfs2_super
*osb
,
7310 struct inode
*inode
,
7311 struct buffer_head
*fe_bh
,
7312 struct ocfs2_truncate_context
**tc
)
7315 unsigned int new_i_clusters
;
7316 struct ocfs2_dinode
*fe
;
7317 struct ocfs2_extent_block
*eb
;
7318 struct buffer_head
*last_eb_bh
= NULL
;
7324 new_i_clusters
= ocfs2_clusters_for_bytes(osb
->sb
,
7325 i_size_read(inode
));
7326 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
7328 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7329 "%llu\n", le32_to_cpu(fe
->i_clusters
), new_i_clusters
,
7330 (unsigned long long)le64_to_cpu(fe
->i_size
));
7332 *tc
= kzalloc(sizeof(struct ocfs2_truncate_context
), GFP_KERNEL
);
7338 ocfs2_init_dealloc_ctxt(&(*tc
)->tc_dealloc
);
7340 if (fe
->id2
.i_list
.l_tree_depth
) {
7341 status
= ocfs2_read_extent_block(inode
,
7342 le64_to_cpu(fe
->i_last_eb_blk
),
7348 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
7351 (*tc
)->tc_last_eb_bh
= last_eb_bh
;
7357 ocfs2_free_truncate_context(*tc
);
7365 * 'start' is inclusive, 'end' is not.
7367 int ocfs2_truncate_inline(struct inode
*inode
, struct buffer_head
*di_bh
,
7368 unsigned int start
, unsigned int end
, int trunc
)
7371 unsigned int numbytes
;
7373 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
7374 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
7375 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
7377 if (end
> i_size_read(inode
))
7378 end
= i_size_read(inode
);
7380 BUG_ON(start
>= end
);
7382 if (!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) ||
7383 !(le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_DATA_FL
) ||
7384 !ocfs2_supports_inline_data(osb
)) {
7385 ocfs2_error(inode
->i_sb
,
7386 "Inline data flags for inode %llu don't agree! "
7387 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7388 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7389 le16_to_cpu(di
->i_dyn_features
),
7390 OCFS2_I(inode
)->ip_dyn_features
,
7391 osb
->s_feature_incompat
);
7396 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
7397 if (IS_ERR(handle
)) {
7398 ret
= PTR_ERR(handle
);
7403 ret
= ocfs2_journal_access_di(handle
, inode
, di_bh
,
7404 OCFS2_JOURNAL_ACCESS_WRITE
);
7410 numbytes
= end
- start
;
7411 memset(idata
->id_data
+ start
, 0, numbytes
);
7414 * No need to worry about the data page here - it's been
7415 * truncated already and inline data doesn't need it for
7416 * pushing zero's to disk, so we'll let readpage pick it up
7420 i_size_write(inode
, start
);
7421 di
->i_size
= cpu_to_le64(start
);
7424 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
7425 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
7427 di
->i_ctime
= di
->i_mtime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
7428 di
->i_ctime_nsec
= di
->i_mtime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
7430 ocfs2_journal_dirty(handle
, di_bh
);
7433 ocfs2_commit_trans(osb
, handle
);
7439 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
)
7442 * The caller is responsible for completing deallocation
7443 * before freeing the context.
7445 if (tc
->tc_dealloc
.c_first_suballocator
!= NULL
)
7447 "Truncate completion has non-empty dealloc context\n");
7449 brelse(tc
->tc_last_eb_bh
);