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
!= rec
->e_cpos
),
180 "Device %s, asking for sparse allocation: inode %llu, "
181 "cpos %u, clusters %u\n",
183 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
185 OCFS2_I(inode
)->ip_clusters
);
190 static int ocfs2_dinode_sanity_check(struct inode
*inode
,
191 struct ocfs2_extent_tree
*et
)
193 struct ocfs2_dinode
*di
= et
->et_object
;
195 BUG_ON(et
->et_ops
!= &ocfs2_dinode_et_ops
);
196 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
201 static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree
*et
)
203 struct ocfs2_dinode
*di
= et
->et_object
;
205 et
->et_root_el
= &di
->id2
.i_list
;
209 static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree
*et
)
211 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
213 et
->et_root_el
= &vb
->vb_xv
->xr_list
;
216 static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
219 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
221 vb
->vb_xv
->xr_last_eb_blk
= cpu_to_le64(blkno
);
224 static u64
ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
226 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
228 return le64_to_cpu(vb
->vb_xv
->xr_last_eb_blk
);
231 static void ocfs2_xattr_value_update_clusters(struct inode
*inode
,
232 struct ocfs2_extent_tree
*et
,
235 struct ocfs2_xattr_value_buf
*vb
= et
->et_object
;
237 le32_add_cpu(&vb
->vb_xv
->xr_clusters
, clusters
);
240 static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops
= {
241 .eo_set_last_eb_blk
= ocfs2_xattr_value_set_last_eb_blk
,
242 .eo_get_last_eb_blk
= ocfs2_xattr_value_get_last_eb_blk
,
243 .eo_update_clusters
= ocfs2_xattr_value_update_clusters
,
244 .eo_fill_root_el
= ocfs2_xattr_value_fill_root_el
,
247 static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree
*et
)
249 struct ocfs2_xattr_block
*xb
= et
->et_object
;
251 et
->et_root_el
= &xb
->xb_attrs
.xb_root
.xt_list
;
254 static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode
*inode
,
255 struct ocfs2_extent_tree
*et
)
257 et
->et_max_leaf_clusters
=
258 ocfs2_clusters_for_bytes(inode
->i_sb
,
259 OCFS2_MAX_XATTR_TREE_LEAF_SIZE
);
262 static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
265 struct ocfs2_xattr_block
*xb
= et
->et_object
;
266 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
268 xt
->xt_last_eb_blk
= cpu_to_le64(blkno
);
271 static u64
ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
273 struct ocfs2_xattr_block
*xb
= et
->et_object
;
274 struct ocfs2_xattr_tree_root
*xt
= &xb
->xb_attrs
.xb_root
;
276 return le64_to_cpu(xt
->xt_last_eb_blk
);
279 static void ocfs2_xattr_tree_update_clusters(struct inode
*inode
,
280 struct ocfs2_extent_tree
*et
,
283 struct ocfs2_xattr_block
*xb
= et
->et_object
;
285 le32_add_cpu(&xb
->xb_attrs
.xb_root
.xt_clusters
, clusters
);
288 static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops
= {
289 .eo_set_last_eb_blk
= ocfs2_xattr_tree_set_last_eb_blk
,
290 .eo_get_last_eb_blk
= ocfs2_xattr_tree_get_last_eb_blk
,
291 .eo_update_clusters
= ocfs2_xattr_tree_update_clusters
,
292 .eo_fill_root_el
= ocfs2_xattr_tree_fill_root_el
,
293 .eo_fill_max_leaf_clusters
= ocfs2_xattr_tree_fill_max_leaf_clusters
,
296 static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree
*et
,
298 struct buffer_head
*bh
,
299 ocfs2_journal_access_func access
,
301 struct ocfs2_extent_tree_operations
*ops
)
305 et
->et_root_journal_access
= access
;
307 obj
= (void *)bh
->b_data
;
310 et
->et_ops
->eo_fill_root_el(et
);
311 if (!et
->et_ops
->eo_fill_max_leaf_clusters
)
312 et
->et_max_leaf_clusters
= 0;
314 et
->et_ops
->eo_fill_max_leaf_clusters(inode
, et
);
317 void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree
*et
,
319 struct buffer_head
*bh
)
321 __ocfs2_init_extent_tree(et
, inode
, bh
, ocfs2_journal_access_di
,
322 NULL
, &ocfs2_dinode_et_ops
);
325 void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree
*et
,
327 struct buffer_head
*bh
)
329 __ocfs2_init_extent_tree(et
, inode
, bh
, ocfs2_journal_access_xb
,
330 NULL
, &ocfs2_xattr_tree_et_ops
);
333 void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree
*et
,
335 struct ocfs2_xattr_value_buf
*vb
)
337 __ocfs2_init_extent_tree(et
, inode
, vb
->vb_bh
, vb
->vb_access
, vb
,
338 &ocfs2_xattr_value_et_ops
);
341 static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree
*et
,
344 et
->et_ops
->eo_set_last_eb_blk(et
, new_last_eb_blk
);
347 static inline u64
ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree
*et
)
349 return et
->et_ops
->eo_get_last_eb_blk(et
);
352 static inline void ocfs2_et_update_clusters(struct inode
*inode
,
353 struct ocfs2_extent_tree
*et
,
356 et
->et_ops
->eo_update_clusters(inode
, et
, clusters
);
359 static inline int ocfs2_et_root_journal_access(handle_t
*handle
,
361 struct ocfs2_extent_tree
*et
,
364 return et
->et_root_journal_access(handle
, inode
, et
->et_root_bh
,
368 static inline int ocfs2_et_insert_check(struct inode
*inode
,
369 struct ocfs2_extent_tree
*et
,
370 struct ocfs2_extent_rec
*rec
)
374 if (et
->et_ops
->eo_insert_check
)
375 ret
= et
->et_ops
->eo_insert_check(inode
, et
, rec
);
379 static inline int ocfs2_et_sanity_check(struct inode
*inode
,
380 struct ocfs2_extent_tree
*et
)
384 if (et
->et_ops
->eo_sanity_check
)
385 ret
= et
->et_ops
->eo_sanity_check(inode
, et
);
389 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
);
390 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
391 struct ocfs2_extent_block
*eb
);
394 * Structures which describe a path through a btree, and functions to
397 * The idea here is to be as generic as possible with the tree
400 struct ocfs2_path_item
{
401 struct buffer_head
*bh
;
402 struct ocfs2_extent_list
*el
;
405 #define OCFS2_MAX_PATH_DEPTH 5
409 ocfs2_journal_access_func p_root_access
;
410 struct ocfs2_path_item p_node
[OCFS2_MAX_PATH_DEPTH
];
413 #define path_root_bh(_path) ((_path)->p_node[0].bh)
414 #define path_root_el(_path) ((_path)->p_node[0].el)
415 #define path_root_access(_path)((_path)->p_root_access)
416 #define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
417 #define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
418 #define path_num_items(_path) ((_path)->p_tree_depth + 1)
421 * Reset the actual path elements so that we can re-use the structure
422 * to build another path. Generally, this involves freeing the buffer
425 static void ocfs2_reinit_path(struct ocfs2_path
*path
, int keep_root
)
427 int i
, start
= 0, depth
= 0;
428 struct ocfs2_path_item
*node
;
433 for(i
= start
; i
< path_num_items(path
); i
++) {
434 node
= &path
->p_node
[i
];
442 * Tree depth may change during truncate, or insert. If we're
443 * keeping the root extent list, then make sure that our path
444 * structure reflects the proper depth.
447 depth
= le16_to_cpu(path_root_el(path
)->l_tree_depth
);
449 path_root_access(path
) = NULL
;
451 path
->p_tree_depth
= depth
;
454 static void ocfs2_free_path(struct ocfs2_path
*path
)
457 ocfs2_reinit_path(path
, 0);
463 * All the elements of src into dest. After this call, src could be freed
464 * without affecting dest.
466 * Both paths should have the same root. Any non-root elements of dest
469 static void ocfs2_cp_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
473 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
474 BUG_ON(path_root_el(dest
) != path_root_el(src
));
475 BUG_ON(path_root_access(dest
) != path_root_access(src
));
477 ocfs2_reinit_path(dest
, 1);
479 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
480 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
481 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
483 if (dest
->p_node
[i
].bh
)
484 get_bh(dest
->p_node
[i
].bh
);
489 * Make the *dest path the same as src and re-initialize src path to
492 static void ocfs2_mv_path(struct ocfs2_path
*dest
, struct ocfs2_path
*src
)
496 BUG_ON(path_root_bh(dest
) != path_root_bh(src
));
497 BUG_ON(path_root_access(dest
) != path_root_access(src
));
499 for(i
= 1; i
< OCFS2_MAX_PATH_DEPTH
; i
++) {
500 brelse(dest
->p_node
[i
].bh
);
502 dest
->p_node
[i
].bh
= src
->p_node
[i
].bh
;
503 dest
->p_node
[i
].el
= src
->p_node
[i
].el
;
505 src
->p_node
[i
].bh
= NULL
;
506 src
->p_node
[i
].el
= NULL
;
511 * Insert an extent block at given index.
513 * This will not take an additional reference on eb_bh.
515 static inline void ocfs2_path_insert_eb(struct ocfs2_path
*path
, int index
,
516 struct buffer_head
*eb_bh
)
518 struct ocfs2_extent_block
*eb
= (struct ocfs2_extent_block
*)eb_bh
->b_data
;
521 * Right now, no root bh is an extent block, so this helps
522 * catch code errors with dinode trees. The assertion can be
523 * safely removed if we ever need to insert extent block
524 * structures at the root.
528 path
->p_node
[index
].bh
= eb_bh
;
529 path
->p_node
[index
].el
= &eb
->h_list
;
532 static struct ocfs2_path
*ocfs2_new_path(struct buffer_head
*root_bh
,
533 struct ocfs2_extent_list
*root_el
,
534 ocfs2_journal_access_func access
)
536 struct ocfs2_path
*path
;
538 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) >= OCFS2_MAX_PATH_DEPTH
);
540 path
= kzalloc(sizeof(*path
), GFP_NOFS
);
542 path
->p_tree_depth
= le16_to_cpu(root_el
->l_tree_depth
);
544 path_root_bh(path
) = root_bh
;
545 path_root_el(path
) = root_el
;
546 path_root_access(path
) = access
;
552 static struct ocfs2_path
*ocfs2_new_path_from_path(struct ocfs2_path
*path
)
554 return ocfs2_new_path(path_root_bh(path
), path_root_el(path
),
555 path_root_access(path
));
558 static struct ocfs2_path
*ocfs2_new_path_from_et(struct ocfs2_extent_tree
*et
)
560 return ocfs2_new_path(et
->et_root_bh
, et
->et_root_el
,
561 et
->et_root_journal_access
);
565 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
566 * otherwise it's the root_access function.
568 * I don't like the way this function's name looks next to
569 * ocfs2_journal_access_path(), but I don't have a better one.
571 static int ocfs2_path_bh_journal_access(handle_t
*handle
,
573 struct ocfs2_path
*path
,
576 ocfs2_journal_access_func access
= path_root_access(path
);
579 access
= ocfs2_journal_access
;
582 access
= ocfs2_journal_access_eb
;
584 return access(handle
, inode
, path
->p_node
[idx
].bh
,
585 OCFS2_JOURNAL_ACCESS_WRITE
);
589 * Convenience function to journal all components in a path.
591 static int ocfs2_journal_access_path(struct inode
*inode
, handle_t
*handle
,
592 struct ocfs2_path
*path
)
599 for(i
= 0; i
< path_num_items(path
); i
++) {
600 ret
= ocfs2_path_bh_journal_access(handle
, inode
, path
, i
);
612 * Return the index of the extent record which contains cluster #v_cluster.
613 * -1 is returned if it was not found.
615 * Should work fine on interior and exterior nodes.
617 int ocfs2_search_extent_list(struct ocfs2_extent_list
*el
, u32 v_cluster
)
621 struct ocfs2_extent_rec
*rec
;
622 u32 rec_end
, rec_start
, clusters
;
624 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
625 rec
= &el
->l_recs
[i
];
627 rec_start
= le32_to_cpu(rec
->e_cpos
);
628 clusters
= ocfs2_rec_clusters(el
, rec
);
630 rec_end
= rec_start
+ clusters
;
632 if (v_cluster
>= rec_start
&& v_cluster
< rec_end
) {
641 enum ocfs2_contig_type
{
650 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
651 * ocfs2_extent_contig only work properly against leaf nodes!
653 static int ocfs2_block_extent_contig(struct super_block
*sb
,
654 struct ocfs2_extent_rec
*ext
,
657 u64 blk_end
= le64_to_cpu(ext
->e_blkno
);
659 blk_end
+= ocfs2_clusters_to_blocks(sb
,
660 le16_to_cpu(ext
->e_leaf_clusters
));
662 return blkno
== blk_end
;
665 static int ocfs2_extents_adjacent(struct ocfs2_extent_rec
*left
,
666 struct ocfs2_extent_rec
*right
)
670 left_range
= le32_to_cpu(left
->e_cpos
) +
671 le16_to_cpu(left
->e_leaf_clusters
);
673 return (left_range
== le32_to_cpu(right
->e_cpos
));
676 static enum ocfs2_contig_type
677 ocfs2_extent_contig(struct inode
*inode
,
678 struct ocfs2_extent_rec
*ext
,
679 struct ocfs2_extent_rec
*insert_rec
)
681 u64 blkno
= le64_to_cpu(insert_rec
->e_blkno
);
684 * Refuse to coalesce extent records with different flag
685 * fields - we don't want to mix unwritten extents with user
688 if (ext
->e_flags
!= insert_rec
->e_flags
)
691 if (ocfs2_extents_adjacent(ext
, insert_rec
) &&
692 ocfs2_block_extent_contig(inode
->i_sb
, ext
, blkno
))
695 blkno
= le64_to_cpu(ext
->e_blkno
);
696 if (ocfs2_extents_adjacent(insert_rec
, ext
) &&
697 ocfs2_block_extent_contig(inode
->i_sb
, insert_rec
, blkno
))
704 * NOTE: We can have pretty much any combination of contiguousness and
707 * The usefulness of APPEND_TAIL is more in that it lets us know that
708 * we'll have to update the path to that leaf.
710 enum ocfs2_append_type
{
715 enum ocfs2_split_type
{
721 struct ocfs2_insert_type
{
722 enum ocfs2_split_type ins_split
;
723 enum ocfs2_append_type ins_appending
;
724 enum ocfs2_contig_type ins_contig
;
725 int ins_contig_index
;
729 struct ocfs2_merge_ctxt
{
730 enum ocfs2_contig_type c_contig_type
;
731 int c_has_empty_extent
;
732 int c_split_covers_rec
;
735 static int ocfs2_validate_extent_block(struct super_block
*sb
,
736 struct buffer_head
*bh
)
739 struct ocfs2_extent_block
*eb
=
740 (struct ocfs2_extent_block
*)bh
->b_data
;
742 mlog(0, "Validating extent block %llu\n",
743 (unsigned long long)bh
->b_blocknr
);
745 BUG_ON(!buffer_uptodate(bh
));
748 * If the ecc fails, we return the error but otherwise
749 * leave the filesystem running. We know any error is
750 * local to this block.
752 rc
= ocfs2_validate_meta_ecc(sb
, bh
->b_data
, &eb
->h_check
);
754 mlog(ML_ERROR
, "Checksum failed for extent block %llu\n",
755 (unsigned long long)bh
->b_blocknr
);
760 * Errors after here are fatal.
763 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb
)) {
765 "Extent block #%llu has bad signature %.*s",
766 (unsigned long long)bh
->b_blocknr
, 7,
771 if (le64_to_cpu(eb
->h_blkno
) != bh
->b_blocknr
) {
773 "Extent block #%llu has an invalid h_blkno "
775 (unsigned long long)bh
->b_blocknr
,
776 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
780 if (le32_to_cpu(eb
->h_fs_generation
) != OCFS2_SB(sb
)->fs_generation
) {
782 "Extent block #%llu has an invalid "
783 "h_fs_generation of #%u",
784 (unsigned long long)bh
->b_blocknr
,
785 le32_to_cpu(eb
->h_fs_generation
));
792 int ocfs2_read_extent_block(struct inode
*inode
, u64 eb_blkno
,
793 struct buffer_head
**bh
)
796 struct buffer_head
*tmp
= *bh
;
798 rc
= ocfs2_read_block(inode
, eb_blkno
, &tmp
,
799 ocfs2_validate_extent_block
);
801 /* If ocfs2_read_block() got us a new bh, pass it up. */
810 * How many free extents have we got before we need more meta data?
812 int ocfs2_num_free_extents(struct ocfs2_super
*osb
,
814 struct ocfs2_extent_tree
*et
)
817 struct ocfs2_extent_list
*el
= NULL
;
818 struct ocfs2_extent_block
*eb
;
819 struct buffer_head
*eb_bh
= NULL
;
825 last_eb_blk
= ocfs2_et_get_last_eb_blk(et
);
828 retval
= ocfs2_read_extent_block(inode
, last_eb_blk
, &eb_bh
);
833 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
837 BUG_ON(el
->l_tree_depth
!= 0);
839 retval
= le16_to_cpu(el
->l_count
) - le16_to_cpu(el
->l_next_free_rec
);
847 /* expects array to already be allocated
849 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
852 static int ocfs2_create_new_meta_bhs(struct ocfs2_super
*osb
,
856 struct ocfs2_alloc_context
*meta_ac
,
857 struct buffer_head
*bhs
[])
859 int count
, status
, i
;
860 u16 suballoc_bit_start
;
863 struct ocfs2_extent_block
*eb
;
868 while (count
< wanted
) {
869 status
= ocfs2_claim_metadata(osb
,
881 for(i
= count
; i
< (num_got
+ count
); i
++) {
882 bhs
[i
] = sb_getblk(osb
->sb
, first_blkno
);
883 if (bhs
[i
] == NULL
) {
888 ocfs2_set_new_buffer_uptodate(inode
, bhs
[i
]);
890 status
= ocfs2_journal_access_eb(handle
, inode
, bhs
[i
],
891 OCFS2_JOURNAL_ACCESS_CREATE
);
897 memset(bhs
[i
]->b_data
, 0, osb
->sb
->s_blocksize
);
898 eb
= (struct ocfs2_extent_block
*) bhs
[i
]->b_data
;
899 /* Ok, setup the minimal stuff here. */
900 strcpy(eb
->h_signature
, OCFS2_EXTENT_BLOCK_SIGNATURE
);
901 eb
->h_blkno
= cpu_to_le64(first_blkno
);
902 eb
->h_fs_generation
= cpu_to_le32(osb
->fs_generation
);
903 eb
->h_suballoc_slot
= cpu_to_le16(osb
->slot_num
);
904 eb
->h_suballoc_bit
= cpu_to_le16(suballoc_bit_start
);
906 cpu_to_le16(ocfs2_extent_recs_per_eb(osb
->sb
));
908 suballoc_bit_start
++;
911 /* We'll also be dirtied by the caller, so
912 * this isn't absolutely necessary. */
913 status
= ocfs2_journal_dirty(handle
, bhs
[i
]);
926 for(i
= 0; i
< wanted
; i
++) {
936 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
938 * Returns the sum of the rightmost extent rec logical offset and
941 * ocfs2_add_branch() uses this to determine what logical cluster
942 * value should be populated into the leftmost new branch records.
944 * ocfs2_shift_tree_depth() uses this to determine the # clusters
945 * value for the new topmost tree record.
947 static inline u32
ocfs2_sum_rightmost_rec(struct ocfs2_extent_list
*el
)
951 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
953 return le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
954 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
958 * Add an entire tree branch to our inode. eb_bh is the extent block
959 * to start at, if we don't want to start the branch at the dinode
962 * last_eb_bh is required as we have to update it's next_leaf pointer
963 * for the new last extent block.
965 * the new branch will be 'empty' in the sense that every block will
966 * contain a single record with cluster count == 0.
968 static int ocfs2_add_branch(struct ocfs2_super
*osb
,
971 struct ocfs2_extent_tree
*et
,
972 struct buffer_head
*eb_bh
,
973 struct buffer_head
**last_eb_bh
,
974 struct ocfs2_alloc_context
*meta_ac
)
976 int status
, new_blocks
, i
;
977 u64 next_blkno
, new_last_eb_blk
;
978 struct buffer_head
*bh
;
979 struct buffer_head
**new_eb_bhs
= NULL
;
980 struct ocfs2_extent_block
*eb
;
981 struct ocfs2_extent_list
*eb_el
;
982 struct ocfs2_extent_list
*el
;
987 BUG_ON(!last_eb_bh
|| !*last_eb_bh
);
990 eb
= (struct ocfs2_extent_block
*) eb_bh
->b_data
;
995 /* we never add a branch to a leaf. */
996 BUG_ON(!el
->l_tree_depth
);
998 new_blocks
= le16_to_cpu(el
->l_tree_depth
);
1000 /* allocate the number of new eb blocks we need */
1001 new_eb_bhs
= kcalloc(new_blocks
, sizeof(struct buffer_head
*),
1009 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, new_blocks
,
1010 meta_ac
, new_eb_bhs
);
1016 eb
= (struct ocfs2_extent_block
*)(*last_eb_bh
)->b_data
;
1017 new_cpos
= ocfs2_sum_rightmost_rec(&eb
->h_list
);
1019 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1020 * linked with the rest of the tree.
1021 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1023 * when we leave the loop, new_last_eb_blk will point to the
1024 * newest leaf, and next_blkno will point to the topmost extent
1026 next_blkno
= new_last_eb_blk
= 0;
1027 for(i
= 0; i
< new_blocks
; i
++) {
1029 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1030 /* ocfs2_create_new_meta_bhs() should create it right! */
1031 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
1032 eb_el
= &eb
->h_list
;
1034 status
= ocfs2_journal_access_eb(handle
, inode
, bh
,
1035 OCFS2_JOURNAL_ACCESS_CREATE
);
1041 eb
->h_next_leaf_blk
= 0;
1042 eb_el
->l_tree_depth
= cpu_to_le16(i
);
1043 eb_el
->l_next_free_rec
= cpu_to_le16(1);
1045 * This actually counts as an empty extent as
1048 eb_el
->l_recs
[0].e_cpos
= cpu_to_le32(new_cpos
);
1049 eb_el
->l_recs
[0].e_blkno
= cpu_to_le64(next_blkno
);
1051 * eb_el isn't always an interior node, but even leaf
1052 * nodes want a zero'd flags and reserved field so
1053 * this gets the whole 32 bits regardless of use.
1055 eb_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(0);
1056 if (!eb_el
->l_tree_depth
)
1057 new_last_eb_blk
= le64_to_cpu(eb
->h_blkno
);
1059 status
= ocfs2_journal_dirty(handle
, bh
);
1065 next_blkno
= le64_to_cpu(eb
->h_blkno
);
1068 /* This is a bit hairy. We want to update up to three blocks
1069 * here without leaving any of them in an inconsistent state
1070 * in case of error. We don't have to worry about
1071 * journal_dirty erroring as it won't unless we've aborted the
1072 * handle (in which case we would never be here) so reserving
1073 * the write with journal_access is all we need to do. */
1074 status
= ocfs2_journal_access_eb(handle
, inode
, *last_eb_bh
,
1075 OCFS2_JOURNAL_ACCESS_WRITE
);
1080 status
= ocfs2_et_root_journal_access(handle
, inode
, et
,
1081 OCFS2_JOURNAL_ACCESS_WRITE
);
1087 status
= ocfs2_journal_access_eb(handle
, inode
, eb_bh
,
1088 OCFS2_JOURNAL_ACCESS_WRITE
);
1095 /* Link the new branch into the rest of the tree (el will
1096 * either be on the root_bh, or the extent block passed in. */
1097 i
= le16_to_cpu(el
->l_next_free_rec
);
1098 el
->l_recs
[i
].e_blkno
= cpu_to_le64(next_blkno
);
1099 el
->l_recs
[i
].e_cpos
= cpu_to_le32(new_cpos
);
1100 el
->l_recs
[i
].e_int_clusters
= 0;
1101 le16_add_cpu(&el
->l_next_free_rec
, 1);
1103 /* fe needs a new last extent block pointer, as does the
1104 * next_leaf on the previously last-extent-block. */
1105 ocfs2_et_set_last_eb_blk(et
, new_last_eb_blk
);
1107 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
1108 eb
->h_next_leaf_blk
= cpu_to_le64(new_last_eb_blk
);
1110 status
= ocfs2_journal_dirty(handle
, *last_eb_bh
);
1113 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
1117 status
= ocfs2_journal_dirty(handle
, eb_bh
);
1123 * Some callers want to track the rightmost leaf so pass it
1126 brelse(*last_eb_bh
);
1127 get_bh(new_eb_bhs
[0]);
1128 *last_eb_bh
= new_eb_bhs
[0];
1133 for (i
= 0; i
< new_blocks
; i
++)
1134 brelse(new_eb_bhs
[i
]);
1143 * adds another level to the allocation tree.
1144 * returns back the new extent block so you can add a branch to it
1147 static int ocfs2_shift_tree_depth(struct ocfs2_super
*osb
,
1149 struct inode
*inode
,
1150 struct ocfs2_extent_tree
*et
,
1151 struct ocfs2_alloc_context
*meta_ac
,
1152 struct buffer_head
**ret_new_eb_bh
)
1156 struct buffer_head
*new_eb_bh
= NULL
;
1157 struct ocfs2_extent_block
*eb
;
1158 struct ocfs2_extent_list
*root_el
;
1159 struct ocfs2_extent_list
*eb_el
;
1163 status
= ocfs2_create_new_meta_bhs(osb
, handle
, inode
, 1, meta_ac
,
1170 eb
= (struct ocfs2_extent_block
*) new_eb_bh
->b_data
;
1171 /* ocfs2_create_new_meta_bhs() should create it right! */
1172 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
1174 eb_el
= &eb
->h_list
;
1175 root_el
= et
->et_root_el
;
1177 status
= ocfs2_journal_access_eb(handle
, inode
, new_eb_bh
,
1178 OCFS2_JOURNAL_ACCESS_CREATE
);
1184 /* copy the root extent list data into the new extent block */
1185 eb_el
->l_tree_depth
= root_el
->l_tree_depth
;
1186 eb_el
->l_next_free_rec
= root_el
->l_next_free_rec
;
1187 for (i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1188 eb_el
->l_recs
[i
] = root_el
->l_recs
[i
];
1190 status
= ocfs2_journal_dirty(handle
, new_eb_bh
);
1196 status
= ocfs2_et_root_journal_access(handle
, inode
, et
,
1197 OCFS2_JOURNAL_ACCESS_WRITE
);
1203 new_clusters
= ocfs2_sum_rightmost_rec(eb_el
);
1205 /* update root_bh now */
1206 le16_add_cpu(&root_el
->l_tree_depth
, 1);
1207 root_el
->l_recs
[0].e_cpos
= 0;
1208 root_el
->l_recs
[0].e_blkno
= eb
->h_blkno
;
1209 root_el
->l_recs
[0].e_int_clusters
= cpu_to_le32(new_clusters
);
1210 for (i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
1211 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
1212 root_el
->l_next_free_rec
= cpu_to_le16(1);
1214 /* If this is our 1st tree depth shift, then last_eb_blk
1215 * becomes the allocated extent block */
1216 if (root_el
->l_tree_depth
== cpu_to_le16(1))
1217 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
1219 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
1225 *ret_new_eb_bh
= new_eb_bh
;
1236 * Should only be called when there is no space left in any of the
1237 * leaf nodes. What we want to do is find the lowest tree depth
1238 * non-leaf extent block with room for new records. There are three
1239 * valid results of this search:
1241 * 1) a lowest extent block is found, then we pass it back in
1242 * *lowest_eb_bh and return '0'
1244 * 2) the search fails to find anything, but the root_el has room. We
1245 * pass NULL back in *lowest_eb_bh, but still return '0'
1247 * 3) the search fails to find anything AND the root_el is full, in
1248 * which case we return > 0
1250 * return status < 0 indicates an error.
1252 static int ocfs2_find_branch_target(struct ocfs2_super
*osb
,
1253 struct inode
*inode
,
1254 struct ocfs2_extent_tree
*et
,
1255 struct buffer_head
**target_bh
)
1259 struct ocfs2_extent_block
*eb
;
1260 struct ocfs2_extent_list
*el
;
1261 struct buffer_head
*bh
= NULL
;
1262 struct buffer_head
*lowest_bh
= NULL
;
1268 el
= et
->et_root_el
;
1270 while(le16_to_cpu(el
->l_tree_depth
) > 1) {
1271 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1272 ocfs2_error(inode
->i_sb
, "Dinode %llu has empty "
1273 "extent list (next_free_rec == 0)",
1274 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
1278 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
1279 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1281 ocfs2_error(inode
->i_sb
, "Dinode %llu has extent "
1282 "list where extent # %d has no physical "
1284 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, i
);
1292 status
= ocfs2_read_extent_block(inode
, blkno
, &bh
);
1298 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1301 if (le16_to_cpu(el
->l_next_free_rec
) <
1302 le16_to_cpu(el
->l_count
)) {
1309 /* If we didn't find one and the fe doesn't have any room,
1310 * then return '1' */
1311 el
= et
->et_root_el
;
1312 if (!lowest_bh
&& (el
->l_next_free_rec
== el
->l_count
))
1315 *target_bh
= lowest_bh
;
1324 * Grow a b-tree so that it has more records.
1326 * We might shift the tree depth in which case existing paths should
1327 * be considered invalid.
1329 * Tree depth after the grow is returned via *final_depth.
1331 * *last_eb_bh will be updated by ocfs2_add_branch().
1333 static int ocfs2_grow_tree(struct inode
*inode
, handle_t
*handle
,
1334 struct ocfs2_extent_tree
*et
, int *final_depth
,
1335 struct buffer_head
**last_eb_bh
,
1336 struct ocfs2_alloc_context
*meta_ac
)
1339 struct ocfs2_extent_list
*el
= et
->et_root_el
;
1340 int depth
= le16_to_cpu(el
->l_tree_depth
);
1341 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
1342 struct buffer_head
*bh
= NULL
;
1344 BUG_ON(meta_ac
== NULL
);
1346 shift
= ocfs2_find_branch_target(osb
, inode
, et
, &bh
);
1353 /* We traveled all the way to the bottom of the allocation tree
1354 * and didn't find room for any more extents - we need to add
1355 * another tree level */
1358 mlog(0, "need to shift tree depth (current = %d)\n", depth
);
1360 /* ocfs2_shift_tree_depth will return us a buffer with
1361 * the new extent block (so we can pass that to
1362 * ocfs2_add_branch). */
1363 ret
= ocfs2_shift_tree_depth(osb
, handle
, inode
, et
,
1372 * Special case: we have room now if we shifted from
1373 * tree_depth 0, so no more work needs to be done.
1375 * We won't be calling add_branch, so pass
1376 * back *last_eb_bh as the new leaf. At depth
1377 * zero, it should always be null so there's
1378 * no reason to brelse.
1380 BUG_ON(*last_eb_bh
);
1387 /* call ocfs2_add_branch to add the final part of the tree with
1389 mlog(0, "add branch. bh = %p\n", bh
);
1390 ret
= ocfs2_add_branch(osb
, handle
, inode
, et
, bh
, last_eb_bh
,
1399 *final_depth
= depth
;
1405 * This function will discard the rightmost extent record.
1407 static void ocfs2_shift_records_right(struct ocfs2_extent_list
*el
)
1409 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1410 int count
= le16_to_cpu(el
->l_count
);
1411 unsigned int num_bytes
;
1414 /* This will cause us to go off the end of our extent list. */
1415 BUG_ON(next_free
>= count
);
1417 num_bytes
= sizeof(struct ocfs2_extent_rec
) * next_free
;
1419 memmove(&el
->l_recs
[1], &el
->l_recs
[0], num_bytes
);
1422 static void ocfs2_rotate_leaf(struct ocfs2_extent_list
*el
,
1423 struct ocfs2_extent_rec
*insert_rec
)
1425 int i
, insert_index
, next_free
, has_empty
, num_bytes
;
1426 u32 insert_cpos
= le32_to_cpu(insert_rec
->e_cpos
);
1427 struct ocfs2_extent_rec
*rec
;
1429 next_free
= le16_to_cpu(el
->l_next_free_rec
);
1430 has_empty
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
1434 /* The tree code before us didn't allow enough room in the leaf. */
1435 BUG_ON(el
->l_next_free_rec
== el
->l_count
&& !has_empty
);
1438 * The easiest way to approach this is to just remove the
1439 * empty extent and temporarily decrement next_free.
1443 * If next_free was 1 (only an empty extent), this
1444 * loop won't execute, which is fine. We still want
1445 * the decrement above to happen.
1447 for(i
= 0; i
< (next_free
- 1); i
++)
1448 el
->l_recs
[i
] = el
->l_recs
[i
+1];
1454 * Figure out what the new record index should be.
1456 for(i
= 0; i
< next_free
; i
++) {
1457 rec
= &el
->l_recs
[i
];
1459 if (insert_cpos
< le32_to_cpu(rec
->e_cpos
))
1464 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1465 insert_cpos
, insert_index
, has_empty
, next_free
, le16_to_cpu(el
->l_count
));
1467 BUG_ON(insert_index
< 0);
1468 BUG_ON(insert_index
>= le16_to_cpu(el
->l_count
));
1469 BUG_ON(insert_index
> next_free
);
1472 * No need to memmove if we're just adding to the tail.
1474 if (insert_index
!= next_free
) {
1475 BUG_ON(next_free
>= le16_to_cpu(el
->l_count
));
1477 num_bytes
= next_free
- insert_index
;
1478 num_bytes
*= sizeof(struct ocfs2_extent_rec
);
1479 memmove(&el
->l_recs
[insert_index
+ 1],
1480 &el
->l_recs
[insert_index
],
1485 * Either we had an empty extent, and need to re-increment or
1486 * there was no empty extent on a non full rightmost leaf node,
1487 * in which case we still need to increment.
1490 el
->l_next_free_rec
= cpu_to_le16(next_free
);
1492 * Make sure none of the math above just messed up our tree.
1494 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) > le16_to_cpu(el
->l_count
));
1496 el
->l_recs
[insert_index
] = *insert_rec
;
1500 static void ocfs2_remove_empty_extent(struct ocfs2_extent_list
*el
)
1502 int size
, num_recs
= le16_to_cpu(el
->l_next_free_rec
);
1504 BUG_ON(num_recs
== 0);
1506 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
1508 size
= num_recs
* sizeof(struct ocfs2_extent_rec
);
1509 memmove(&el
->l_recs
[0], &el
->l_recs
[1], size
);
1510 memset(&el
->l_recs
[num_recs
], 0,
1511 sizeof(struct ocfs2_extent_rec
));
1512 el
->l_next_free_rec
= cpu_to_le16(num_recs
);
1517 * Create an empty extent record .
1519 * l_next_free_rec may be updated.
1521 * If an empty extent already exists do nothing.
1523 static void ocfs2_create_empty_extent(struct ocfs2_extent_list
*el
)
1525 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
1527 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
1532 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
1535 mlog_bug_on_msg(el
->l_count
== el
->l_next_free_rec
,
1536 "Asked to create an empty extent in a full list:\n"
1537 "count = %u, tree depth = %u",
1538 le16_to_cpu(el
->l_count
),
1539 le16_to_cpu(el
->l_tree_depth
));
1541 ocfs2_shift_records_right(el
);
1544 le16_add_cpu(&el
->l_next_free_rec
, 1);
1545 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
1549 * For a rotation which involves two leaf nodes, the "root node" is
1550 * the lowest level tree node which contains a path to both leafs. This
1551 * resulting set of information can be used to form a complete "subtree"
1553 * This function is passed two full paths from the dinode down to a
1554 * pair of adjacent leaves. It's task is to figure out which path
1555 * index contains the subtree root - this can be the root index itself
1556 * in a worst-case rotation.
1558 * The array index of the subtree root is passed back.
1560 static int ocfs2_find_subtree_root(struct inode
*inode
,
1561 struct ocfs2_path
*left
,
1562 struct ocfs2_path
*right
)
1567 * Check that the caller passed in two paths from the same tree.
1569 BUG_ON(path_root_bh(left
) != path_root_bh(right
));
1575 * The caller didn't pass two adjacent paths.
1577 mlog_bug_on_msg(i
> left
->p_tree_depth
,
1578 "Inode %lu, left depth %u, right depth %u\n"
1579 "left leaf blk %llu, right leaf blk %llu\n",
1580 inode
->i_ino
, left
->p_tree_depth
,
1581 right
->p_tree_depth
,
1582 (unsigned long long)path_leaf_bh(left
)->b_blocknr
,
1583 (unsigned long long)path_leaf_bh(right
)->b_blocknr
);
1584 } while (left
->p_node
[i
].bh
->b_blocknr
==
1585 right
->p_node
[i
].bh
->b_blocknr
);
1590 typedef void (path_insert_t
)(void *, struct buffer_head
*);
1593 * Traverse a btree path in search of cpos, starting at root_el.
1595 * This code can be called with a cpos larger than the tree, in which
1596 * case it will return the rightmost path.
1598 static int __ocfs2_find_path(struct inode
*inode
,
1599 struct ocfs2_extent_list
*root_el
, u32 cpos
,
1600 path_insert_t
*func
, void *data
)
1605 struct buffer_head
*bh
= NULL
;
1606 struct ocfs2_extent_block
*eb
;
1607 struct ocfs2_extent_list
*el
;
1608 struct ocfs2_extent_rec
*rec
;
1609 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
1612 while (el
->l_tree_depth
) {
1613 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
1614 ocfs2_error(inode
->i_sb
,
1615 "Inode %llu has empty extent list at "
1617 (unsigned long long)oi
->ip_blkno
,
1618 le16_to_cpu(el
->l_tree_depth
));
1624 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
) - 1; i
++) {
1625 rec
= &el
->l_recs
[i
];
1628 * In the case that cpos is off the allocation
1629 * tree, this should just wind up returning the
1632 range
= le32_to_cpu(rec
->e_cpos
) +
1633 ocfs2_rec_clusters(el
, rec
);
1634 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
1638 blkno
= le64_to_cpu(el
->l_recs
[i
].e_blkno
);
1640 ocfs2_error(inode
->i_sb
,
1641 "Inode %llu has bad blkno in extent list "
1642 "at depth %u (index %d)\n",
1643 (unsigned long long)oi
->ip_blkno
,
1644 le16_to_cpu(el
->l_tree_depth
), i
);
1651 ret
= ocfs2_read_extent_block(inode
, blkno
, &bh
);
1657 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
1660 if (le16_to_cpu(el
->l_next_free_rec
) >
1661 le16_to_cpu(el
->l_count
)) {
1662 ocfs2_error(inode
->i_sb
,
1663 "Inode %llu has bad count in extent list "
1664 "at block %llu (next free=%u, count=%u)\n",
1665 (unsigned long long)oi
->ip_blkno
,
1666 (unsigned long long)bh
->b_blocknr
,
1667 le16_to_cpu(el
->l_next_free_rec
),
1668 le16_to_cpu(el
->l_count
));
1679 * Catch any trailing bh that the loop didn't handle.
1687 * Given an initialized path (that is, it has a valid root extent
1688 * list), this function will traverse the btree in search of the path
1689 * which would contain cpos.
1691 * The path traveled is recorded in the path structure.
1693 * Note that this will not do any comparisons on leaf node extent
1694 * records, so it will work fine in the case that we just added a tree
1697 struct find_path_data
{
1699 struct ocfs2_path
*path
;
1701 static void find_path_ins(void *data
, struct buffer_head
*bh
)
1703 struct find_path_data
*fp
= data
;
1706 ocfs2_path_insert_eb(fp
->path
, fp
->index
, bh
);
1709 static int ocfs2_find_path(struct inode
*inode
, struct ocfs2_path
*path
,
1712 struct find_path_data data
;
1716 return __ocfs2_find_path(inode
, path_root_el(path
), cpos
,
1717 find_path_ins
, &data
);
1720 static void find_leaf_ins(void *data
, struct buffer_head
*bh
)
1722 struct ocfs2_extent_block
*eb
=(struct ocfs2_extent_block
*)bh
->b_data
;
1723 struct ocfs2_extent_list
*el
= &eb
->h_list
;
1724 struct buffer_head
**ret
= data
;
1726 /* We want to retain only the leaf block. */
1727 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
1733 * Find the leaf block in the tree which would contain cpos. No
1734 * checking of the actual leaf is done.
1736 * Some paths want to call this instead of allocating a path structure
1737 * and calling ocfs2_find_path().
1739 * This function doesn't handle non btree extent lists.
1741 int ocfs2_find_leaf(struct inode
*inode
, struct ocfs2_extent_list
*root_el
,
1742 u32 cpos
, struct buffer_head
**leaf_bh
)
1745 struct buffer_head
*bh
= NULL
;
1747 ret
= __ocfs2_find_path(inode
, root_el
, cpos
, find_leaf_ins
, &bh
);
1759 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1761 * Basically, we've moved stuff around at the bottom of the tree and
1762 * we need to fix up the extent records above the changes to reflect
1765 * left_rec: the record on the left.
1766 * left_child_el: is the child list pointed to by left_rec
1767 * right_rec: the record to the right of left_rec
1768 * right_child_el: is the child list pointed to by right_rec
1770 * By definition, this only works on interior nodes.
1772 static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec
*left_rec
,
1773 struct ocfs2_extent_list
*left_child_el
,
1774 struct ocfs2_extent_rec
*right_rec
,
1775 struct ocfs2_extent_list
*right_child_el
)
1777 u32 left_clusters
, right_end
;
1780 * Interior nodes never have holes. Their cpos is the cpos of
1781 * the leftmost record in their child list. Their cluster
1782 * count covers the full theoretical range of their child list
1783 * - the range between their cpos and the cpos of the record
1784 * immediately to their right.
1786 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[0].e_cpos
);
1787 if (ocfs2_is_empty_extent(&right_child_el
->l_recs
[0])) {
1788 BUG_ON(le16_to_cpu(right_child_el
->l_next_free_rec
) <= 1);
1789 left_clusters
= le32_to_cpu(right_child_el
->l_recs
[1].e_cpos
);
1791 left_clusters
-= le32_to_cpu(left_rec
->e_cpos
);
1792 left_rec
->e_int_clusters
= cpu_to_le32(left_clusters
);
1795 * Calculate the rightmost cluster count boundary before
1796 * moving cpos - we will need to adjust clusters after
1797 * updating e_cpos to keep the same highest cluster count.
1799 right_end
= le32_to_cpu(right_rec
->e_cpos
);
1800 right_end
+= le32_to_cpu(right_rec
->e_int_clusters
);
1802 right_rec
->e_cpos
= left_rec
->e_cpos
;
1803 le32_add_cpu(&right_rec
->e_cpos
, left_clusters
);
1805 right_end
-= le32_to_cpu(right_rec
->e_cpos
);
1806 right_rec
->e_int_clusters
= cpu_to_le32(right_end
);
1810 * Adjust the adjacent root node records involved in a
1811 * rotation. left_el_blkno is passed in as a key so that we can easily
1812 * find it's index in the root list.
1814 static void ocfs2_adjust_root_records(struct ocfs2_extent_list
*root_el
,
1815 struct ocfs2_extent_list
*left_el
,
1816 struct ocfs2_extent_list
*right_el
,
1821 BUG_ON(le16_to_cpu(root_el
->l_tree_depth
) <=
1822 le16_to_cpu(left_el
->l_tree_depth
));
1824 for(i
= 0; i
< le16_to_cpu(root_el
->l_next_free_rec
) - 1; i
++) {
1825 if (le64_to_cpu(root_el
->l_recs
[i
].e_blkno
) == left_el_blkno
)
1830 * The path walking code should have never returned a root and
1831 * two paths which are not adjacent.
1833 BUG_ON(i
>= (le16_to_cpu(root_el
->l_next_free_rec
) - 1));
1835 ocfs2_adjust_adjacent_records(&root_el
->l_recs
[i
], left_el
,
1836 &root_el
->l_recs
[i
+ 1], right_el
);
1840 * We've changed a leaf block (in right_path) and need to reflect that
1841 * change back up the subtree.
1843 * This happens in multiple places:
1844 * - When we've moved an extent record from the left path leaf to the right
1845 * path leaf to make room for an empty extent in the left path leaf.
1846 * - When our insert into the right path leaf is at the leftmost edge
1847 * and requires an update of the path immediately to it's left. This
1848 * can occur at the end of some types of rotation and appending inserts.
1849 * - When we've adjusted the last extent record in the left path leaf and the
1850 * 1st extent record in the right path leaf during cross extent block merge.
1852 static void ocfs2_complete_edge_insert(struct inode
*inode
, handle_t
*handle
,
1853 struct ocfs2_path
*left_path
,
1854 struct ocfs2_path
*right_path
,
1858 struct ocfs2_extent_list
*el
, *left_el
, *right_el
;
1859 struct ocfs2_extent_rec
*left_rec
, *right_rec
;
1860 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
1863 * Update the counts and position values within all the
1864 * interior nodes to reflect the leaf rotation we just did.
1866 * The root node is handled below the loop.
1868 * We begin the loop with right_el and left_el pointing to the
1869 * leaf lists and work our way up.
1871 * NOTE: within this loop, left_el and right_el always refer
1872 * to the *child* lists.
1874 left_el
= path_leaf_el(left_path
);
1875 right_el
= path_leaf_el(right_path
);
1876 for(i
= left_path
->p_tree_depth
- 1; i
> subtree_index
; i
--) {
1877 mlog(0, "Adjust records at index %u\n", i
);
1880 * One nice property of knowing that all of these
1881 * nodes are below the root is that we only deal with
1882 * the leftmost right node record and the rightmost
1885 el
= left_path
->p_node
[i
].el
;
1886 idx
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
1887 left_rec
= &el
->l_recs
[idx
];
1889 el
= right_path
->p_node
[i
].el
;
1890 right_rec
= &el
->l_recs
[0];
1892 ocfs2_adjust_adjacent_records(left_rec
, left_el
, right_rec
,
1895 ret
= ocfs2_journal_dirty(handle
, left_path
->p_node
[i
].bh
);
1899 ret
= ocfs2_journal_dirty(handle
, right_path
->p_node
[i
].bh
);
1904 * Setup our list pointers now so that the current
1905 * parents become children in the next iteration.
1907 left_el
= left_path
->p_node
[i
].el
;
1908 right_el
= right_path
->p_node
[i
].el
;
1912 * At the root node, adjust the two adjacent records which
1913 * begin our path to the leaves.
1916 el
= left_path
->p_node
[subtree_index
].el
;
1917 left_el
= left_path
->p_node
[subtree_index
+ 1].el
;
1918 right_el
= right_path
->p_node
[subtree_index
+ 1].el
;
1920 ocfs2_adjust_root_records(el
, left_el
, right_el
,
1921 left_path
->p_node
[subtree_index
+ 1].bh
->b_blocknr
);
1923 root_bh
= left_path
->p_node
[subtree_index
].bh
;
1925 ret
= ocfs2_journal_dirty(handle
, root_bh
);
1930 static int ocfs2_rotate_subtree_right(struct inode
*inode
,
1932 struct ocfs2_path
*left_path
,
1933 struct ocfs2_path
*right_path
,
1937 struct buffer_head
*right_leaf_bh
;
1938 struct buffer_head
*left_leaf_bh
= NULL
;
1939 struct buffer_head
*root_bh
;
1940 struct ocfs2_extent_list
*right_el
, *left_el
;
1941 struct ocfs2_extent_rec move_rec
;
1943 left_leaf_bh
= path_leaf_bh(left_path
);
1944 left_el
= path_leaf_el(left_path
);
1946 if (left_el
->l_next_free_rec
!= left_el
->l_count
) {
1947 ocfs2_error(inode
->i_sb
,
1948 "Inode %llu has non-full interior leaf node %llu"
1950 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1951 (unsigned long long)left_leaf_bh
->b_blocknr
,
1952 le16_to_cpu(left_el
->l_next_free_rec
));
1957 * This extent block may already have an empty record, so we
1958 * return early if so.
1960 if (ocfs2_is_empty_extent(&left_el
->l_recs
[0]))
1963 root_bh
= left_path
->p_node
[subtree_index
].bh
;
1964 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
1966 ret
= ocfs2_path_bh_journal_access(handle
, inode
, right_path
,
1973 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
1974 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
1981 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
1989 right_leaf_bh
= path_leaf_bh(right_path
);
1990 right_el
= path_leaf_el(right_path
);
1992 /* This is a code error, not a disk corruption. */
1993 mlog_bug_on_msg(!right_el
->l_next_free_rec
, "Inode %llu: Rotate fails "
1994 "because rightmost leaf block %llu is empty\n",
1995 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
1996 (unsigned long long)right_leaf_bh
->b_blocknr
);
1998 ocfs2_create_empty_extent(right_el
);
2000 ret
= ocfs2_journal_dirty(handle
, right_leaf_bh
);
2006 /* Do the copy now. */
2007 i
= le16_to_cpu(left_el
->l_next_free_rec
) - 1;
2008 move_rec
= left_el
->l_recs
[i
];
2009 right_el
->l_recs
[0] = move_rec
;
2012 * Clear out the record we just copied and shift everything
2013 * over, leaving an empty extent in the left leaf.
2015 * We temporarily subtract from next_free_rec so that the
2016 * shift will lose the tail record (which is now defunct).
2018 le16_add_cpu(&left_el
->l_next_free_rec
, -1);
2019 ocfs2_shift_records_right(left_el
);
2020 memset(&left_el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2021 le16_add_cpu(&left_el
->l_next_free_rec
, 1);
2023 ret
= ocfs2_journal_dirty(handle
, left_leaf_bh
);
2029 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
2037 * Given a full path, determine what cpos value would return us a path
2038 * containing the leaf immediately to the left of the current one.
2040 * Will return zero if the path passed in is already the leftmost path.
2042 static int ocfs2_find_cpos_for_left_leaf(struct super_block
*sb
,
2043 struct ocfs2_path
*path
, u32
*cpos
)
2047 struct ocfs2_extent_list
*el
;
2049 BUG_ON(path
->p_tree_depth
== 0);
2053 blkno
= path_leaf_bh(path
)->b_blocknr
;
2055 /* Start at the tree node just above the leaf and work our way up. */
2056 i
= path
->p_tree_depth
- 1;
2058 el
= path
->p_node
[i
].el
;
2061 * Find the extent record just before the one in our
2064 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
2065 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
2069 * We've determined that the
2070 * path specified is already
2071 * the leftmost one - return a
2077 * The leftmost record points to our
2078 * leaf - we need to travel up the
2084 *cpos
= le32_to_cpu(el
->l_recs
[j
- 1].e_cpos
);
2085 *cpos
= *cpos
+ ocfs2_rec_clusters(el
,
2086 &el
->l_recs
[j
- 1]);
2093 * If we got here, we never found a valid node where
2094 * the tree indicated one should be.
2097 "Invalid extent tree at extent block %llu\n",
2098 (unsigned long long)blkno
);
2103 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2112 * Extend the transaction by enough credits to complete the rotation,
2113 * and still leave at least the original number of credits allocated
2114 * to this transaction.
2116 static int ocfs2_extend_rotate_transaction(handle_t
*handle
, int subtree_depth
,
2118 struct ocfs2_path
*path
)
2120 int credits
= (path
->p_tree_depth
- subtree_depth
) * 2 + 1 + op_credits
;
2122 if (handle
->h_buffer_credits
< credits
)
2123 return ocfs2_extend_trans(handle
, credits
);
2129 * Trap the case where we're inserting into the theoretical range past
2130 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2131 * whose cpos is less than ours into the right leaf.
2133 * It's only necessary to look at the rightmost record of the left
2134 * leaf because the logic that calls us should ensure that the
2135 * theoretical ranges in the path components above the leaves are
2138 static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path
*left_path
,
2141 struct ocfs2_extent_list
*left_el
;
2142 struct ocfs2_extent_rec
*rec
;
2145 left_el
= path_leaf_el(left_path
);
2146 next_free
= le16_to_cpu(left_el
->l_next_free_rec
);
2147 rec
= &left_el
->l_recs
[next_free
- 1];
2149 if (insert_cpos
> le32_to_cpu(rec
->e_cpos
))
2154 static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list
*el
, u32 cpos
)
2156 int next_free
= le16_to_cpu(el
->l_next_free_rec
);
2158 struct ocfs2_extent_rec
*rec
;
2163 rec
= &el
->l_recs
[0];
2164 if (ocfs2_is_empty_extent(rec
)) {
2168 rec
= &el
->l_recs
[1];
2171 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2172 if (cpos
>= le32_to_cpu(rec
->e_cpos
) && cpos
< range
)
2178 * Rotate all the records in a btree right one record, starting at insert_cpos.
2180 * The path to the rightmost leaf should be passed in.
2182 * The array is assumed to be large enough to hold an entire path (tree depth).
2184 * Upon succesful return from this function:
2186 * - The 'right_path' array will contain a path to the leaf block
2187 * whose range contains e_cpos.
2188 * - That leaf block will have a single empty extent in list index 0.
2189 * - In the case that the rotation requires a post-insert update,
2190 * *ret_left_path will contain a valid path which can be passed to
2191 * ocfs2_insert_path().
2193 static int ocfs2_rotate_tree_right(struct inode
*inode
,
2195 enum ocfs2_split_type split
,
2197 struct ocfs2_path
*right_path
,
2198 struct ocfs2_path
**ret_left_path
)
2200 int ret
, start
, orig_credits
= handle
->h_buffer_credits
;
2202 struct ocfs2_path
*left_path
= NULL
;
2204 *ret_left_path
= NULL
;
2206 left_path
= ocfs2_new_path_from_path(right_path
);
2213 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
, &cpos
);
2219 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos
, cpos
);
2222 * What we want to do here is:
2224 * 1) Start with the rightmost path.
2226 * 2) Determine a path to the leaf block directly to the left
2229 * 3) Determine the 'subtree root' - the lowest level tree node
2230 * which contains a path to both leaves.
2232 * 4) Rotate the subtree.
2234 * 5) Find the next subtree by considering the left path to be
2235 * the new right path.
2237 * The check at the top of this while loop also accepts
2238 * insert_cpos == cpos because cpos is only a _theoretical_
2239 * value to get us the left path - insert_cpos might very well
2240 * be filling that hole.
2242 * Stop at a cpos of '0' because we either started at the
2243 * leftmost branch (i.e., a tree with one branch and a
2244 * rotation inside of it), or we've gone as far as we can in
2245 * rotating subtrees.
2247 while (cpos
&& insert_cpos
<= cpos
) {
2248 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2251 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
2257 mlog_bug_on_msg(path_leaf_bh(left_path
) ==
2258 path_leaf_bh(right_path
),
2259 "Inode %lu: error during insert of %u "
2260 "(left path cpos %u) results in two identical "
2261 "paths ending at %llu\n",
2262 inode
->i_ino
, insert_cpos
, cpos
,
2263 (unsigned long long)
2264 path_leaf_bh(left_path
)->b_blocknr
);
2266 if (split
== SPLIT_NONE
&&
2267 ocfs2_rotate_requires_path_adjustment(left_path
,
2271 * We've rotated the tree as much as we
2272 * should. The rest is up to
2273 * ocfs2_insert_path() to complete, after the
2274 * record insertion. We indicate this
2275 * situation by returning the left path.
2277 * The reason we don't adjust the records here
2278 * before the record insert is that an error
2279 * later might break the rule where a parent
2280 * record e_cpos will reflect the actual
2281 * e_cpos of the 1st nonempty record of the
2284 *ret_left_path
= left_path
;
2288 start
= ocfs2_find_subtree_root(inode
, left_path
, right_path
);
2290 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2292 (unsigned long long) right_path
->p_node
[start
].bh
->b_blocknr
,
2293 right_path
->p_tree_depth
);
2295 ret
= ocfs2_extend_rotate_transaction(handle
, start
,
2296 orig_credits
, right_path
);
2302 ret
= ocfs2_rotate_subtree_right(inode
, handle
, left_path
,
2309 if (split
!= SPLIT_NONE
&&
2310 ocfs2_leftmost_rec_contains(path_leaf_el(right_path
),
2313 * A rotate moves the rightmost left leaf
2314 * record over to the leftmost right leaf
2315 * slot. If we're doing an extent split
2316 * instead of a real insert, then we have to
2317 * check that the extent to be split wasn't
2318 * just moved over. If it was, then we can
2319 * exit here, passing left_path back -
2320 * ocfs2_split_extent() is smart enough to
2321 * search both leaves.
2323 *ret_left_path
= left_path
;
2328 * There is no need to re-read the next right path
2329 * as we know that it'll be our current left
2330 * path. Optimize by copying values instead.
2332 ocfs2_mv_path(right_path
, left_path
);
2334 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
2343 ocfs2_free_path(left_path
);
2349 static void ocfs2_update_edge_lengths(struct inode
*inode
, handle_t
*handle
,
2350 struct ocfs2_path
*path
)
2353 struct ocfs2_extent_rec
*rec
;
2354 struct ocfs2_extent_list
*el
;
2355 struct ocfs2_extent_block
*eb
;
2358 /* Path should always be rightmost. */
2359 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
2360 BUG_ON(eb
->h_next_leaf_blk
!= 0ULL);
2363 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
2364 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2365 rec
= &el
->l_recs
[idx
];
2366 range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
2368 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
2369 el
= path
->p_node
[i
].el
;
2370 idx
= le16_to_cpu(el
->l_next_free_rec
) - 1;
2371 rec
= &el
->l_recs
[idx
];
2373 rec
->e_int_clusters
= cpu_to_le32(range
);
2374 le32_add_cpu(&rec
->e_int_clusters
, -le32_to_cpu(rec
->e_cpos
));
2376 ocfs2_journal_dirty(handle
, path
->p_node
[i
].bh
);
2380 static void ocfs2_unlink_path(struct inode
*inode
, handle_t
*handle
,
2381 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2382 struct ocfs2_path
*path
, int unlink_start
)
2385 struct ocfs2_extent_block
*eb
;
2386 struct ocfs2_extent_list
*el
;
2387 struct buffer_head
*bh
;
2389 for(i
= unlink_start
; i
< path_num_items(path
); i
++) {
2390 bh
= path
->p_node
[i
].bh
;
2392 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
2394 * Not all nodes might have had their final count
2395 * decremented by the caller - handle this here.
2398 if (le16_to_cpu(el
->l_next_free_rec
) > 1) {
2400 "Inode %llu, attempted to remove extent block "
2401 "%llu with %u records\n",
2402 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
2403 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
2404 le16_to_cpu(el
->l_next_free_rec
));
2406 ocfs2_journal_dirty(handle
, bh
);
2407 ocfs2_remove_from_cache(inode
, bh
);
2411 el
->l_next_free_rec
= 0;
2412 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2414 ocfs2_journal_dirty(handle
, bh
);
2416 ret
= ocfs2_cache_extent_block_free(dealloc
, eb
);
2420 ocfs2_remove_from_cache(inode
, bh
);
2424 static void ocfs2_unlink_subtree(struct inode
*inode
, handle_t
*handle
,
2425 struct ocfs2_path
*left_path
,
2426 struct ocfs2_path
*right_path
,
2428 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
2431 struct buffer_head
*root_bh
= left_path
->p_node
[subtree_index
].bh
;
2432 struct ocfs2_extent_list
*root_el
= left_path
->p_node
[subtree_index
].el
;
2433 struct ocfs2_extent_list
*el
;
2434 struct ocfs2_extent_block
*eb
;
2436 el
= path_leaf_el(left_path
);
2438 eb
= (struct ocfs2_extent_block
*)right_path
->p_node
[subtree_index
+ 1].bh
->b_data
;
2440 for(i
= 1; i
< le16_to_cpu(root_el
->l_next_free_rec
); i
++)
2441 if (root_el
->l_recs
[i
].e_blkno
== eb
->h_blkno
)
2444 BUG_ON(i
>= le16_to_cpu(root_el
->l_next_free_rec
));
2446 memset(&root_el
->l_recs
[i
], 0, sizeof(struct ocfs2_extent_rec
));
2447 le16_add_cpu(&root_el
->l_next_free_rec
, -1);
2449 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2450 eb
->h_next_leaf_blk
= 0;
2452 ocfs2_journal_dirty(handle
, root_bh
);
2453 ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2455 ocfs2_unlink_path(inode
, handle
, dealloc
, right_path
,
2459 static int ocfs2_rotate_subtree_left(struct inode
*inode
, handle_t
*handle
,
2460 struct ocfs2_path
*left_path
,
2461 struct ocfs2_path
*right_path
,
2463 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2465 struct ocfs2_extent_tree
*et
)
2467 int ret
, i
, del_right_subtree
= 0, right_has_empty
= 0;
2468 struct buffer_head
*root_bh
, *et_root_bh
= path_root_bh(right_path
);
2469 struct ocfs2_extent_list
*right_leaf_el
, *left_leaf_el
;
2470 struct ocfs2_extent_block
*eb
;
2474 right_leaf_el
= path_leaf_el(right_path
);
2475 left_leaf_el
= path_leaf_el(left_path
);
2476 root_bh
= left_path
->p_node
[subtree_index
].bh
;
2477 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
2479 if (!ocfs2_is_empty_extent(&left_leaf_el
->l_recs
[0]))
2482 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(right_path
)->b_data
;
2483 if (ocfs2_is_empty_extent(&right_leaf_el
->l_recs
[0])) {
2485 * It's legal for us to proceed if the right leaf is
2486 * the rightmost one and it has an empty extent. There
2487 * are two cases to handle - whether the leaf will be
2488 * empty after removal or not. If the leaf isn't empty
2489 * then just remove the empty extent up front. The
2490 * next block will handle empty leaves by flagging
2493 * Non rightmost leaves will throw -EAGAIN and the
2494 * caller can manually move the subtree and retry.
2497 if (eb
->h_next_leaf_blk
!= 0ULL)
2500 if (le16_to_cpu(right_leaf_el
->l_next_free_rec
) > 1) {
2501 ret
= ocfs2_journal_access_eb(handle
, inode
,
2502 path_leaf_bh(right_path
),
2503 OCFS2_JOURNAL_ACCESS_WRITE
);
2509 ocfs2_remove_empty_extent(right_leaf_el
);
2511 right_has_empty
= 1;
2514 if (eb
->h_next_leaf_blk
== 0ULL &&
2515 le16_to_cpu(right_leaf_el
->l_next_free_rec
) == 1) {
2517 * We have to update i_last_eb_blk during the meta
2520 ret
= ocfs2_et_root_journal_access(handle
, inode
, et
,
2521 OCFS2_JOURNAL_ACCESS_WRITE
);
2527 del_right_subtree
= 1;
2531 * Getting here with an empty extent in the right path implies
2532 * that it's the rightmost path and will be deleted.
2534 BUG_ON(right_has_empty
&& !del_right_subtree
);
2536 ret
= ocfs2_path_bh_journal_access(handle
, inode
, right_path
,
2543 for(i
= subtree_index
+ 1; i
< path_num_items(right_path
); i
++) {
2544 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
2551 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
2559 if (!right_has_empty
) {
2561 * Only do this if we're moving a real
2562 * record. Otherwise, the action is delayed until
2563 * after removal of the right path in which case we
2564 * can do a simple shift to remove the empty extent.
2566 ocfs2_rotate_leaf(left_leaf_el
, &right_leaf_el
->l_recs
[0]);
2567 memset(&right_leaf_el
->l_recs
[0], 0,
2568 sizeof(struct ocfs2_extent_rec
));
2570 if (eb
->h_next_leaf_blk
== 0ULL) {
2572 * Move recs over to get rid of empty extent, decrease
2573 * next_free. This is allowed to remove the last
2574 * extent in our leaf (setting l_next_free_rec to
2575 * zero) - the delete code below won't care.
2577 ocfs2_remove_empty_extent(right_leaf_el
);
2580 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
2583 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
2587 if (del_right_subtree
) {
2588 ocfs2_unlink_subtree(inode
, handle
, left_path
, right_path
,
2589 subtree_index
, dealloc
);
2590 ocfs2_update_edge_lengths(inode
, handle
, left_path
);
2592 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2593 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
2596 * Removal of the extent in the left leaf was skipped
2597 * above so we could delete the right path
2600 if (right_has_empty
)
2601 ocfs2_remove_empty_extent(left_leaf_el
);
2603 ret
= ocfs2_journal_dirty(handle
, et_root_bh
);
2609 ocfs2_complete_edge_insert(inode
, handle
, left_path
, right_path
,
2617 * Given a full path, determine what cpos value would return us a path
2618 * containing the leaf immediately to the right of the current one.
2620 * Will return zero if the path passed in is already the rightmost path.
2622 * This looks similar, but is subtly different to
2623 * ocfs2_find_cpos_for_left_leaf().
2625 static int ocfs2_find_cpos_for_right_leaf(struct super_block
*sb
,
2626 struct ocfs2_path
*path
, u32
*cpos
)
2630 struct ocfs2_extent_list
*el
;
2634 if (path
->p_tree_depth
== 0)
2637 blkno
= path_leaf_bh(path
)->b_blocknr
;
2639 /* Start at the tree node just above the leaf and work our way up. */
2640 i
= path
->p_tree_depth
- 1;
2644 el
= path
->p_node
[i
].el
;
2647 * Find the extent record just after the one in our
2650 next_free
= le16_to_cpu(el
->l_next_free_rec
);
2651 for(j
= 0; j
< le16_to_cpu(el
->l_next_free_rec
); j
++) {
2652 if (le64_to_cpu(el
->l_recs
[j
].e_blkno
) == blkno
) {
2653 if (j
== (next_free
- 1)) {
2656 * We've determined that the
2657 * path specified is already
2658 * the rightmost one - return a
2664 * The rightmost record points to our
2665 * leaf - we need to travel up the
2671 *cpos
= le32_to_cpu(el
->l_recs
[j
+ 1].e_cpos
);
2677 * If we got here, we never found a valid node where
2678 * the tree indicated one should be.
2681 "Invalid extent tree at extent block %llu\n",
2682 (unsigned long long)blkno
);
2687 blkno
= path
->p_node
[i
].bh
->b_blocknr
;
2695 static int ocfs2_rotate_rightmost_leaf_left(struct inode
*inode
,
2697 struct ocfs2_path
*path
)
2700 struct buffer_head
*bh
= path_leaf_bh(path
);
2701 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
2703 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2706 ret
= ocfs2_path_bh_journal_access(handle
, inode
, path
,
2707 path_num_items(path
) - 1);
2713 ocfs2_remove_empty_extent(el
);
2715 ret
= ocfs2_journal_dirty(handle
, bh
);
2723 static int __ocfs2_rotate_tree_left(struct inode
*inode
,
2724 handle_t
*handle
, int orig_credits
,
2725 struct ocfs2_path
*path
,
2726 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2727 struct ocfs2_path
**empty_extent_path
,
2728 struct ocfs2_extent_tree
*et
)
2730 int ret
, subtree_root
, deleted
;
2732 struct ocfs2_path
*left_path
= NULL
;
2733 struct ocfs2_path
*right_path
= NULL
;
2735 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path
)->l_recs
[0])));
2737 *empty_extent_path
= NULL
;
2739 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, path
,
2746 left_path
= ocfs2_new_path_from_path(path
);
2753 ocfs2_cp_path(left_path
, path
);
2755 right_path
= ocfs2_new_path_from_path(path
);
2762 while (right_cpos
) {
2763 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
2769 subtree_root
= ocfs2_find_subtree_root(inode
, left_path
,
2772 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2774 (unsigned long long)
2775 right_path
->p_node
[subtree_root
].bh
->b_blocknr
,
2776 right_path
->p_tree_depth
);
2778 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_root
,
2779 orig_credits
, left_path
);
2786 * Caller might still want to make changes to the
2787 * tree root, so re-add it to the journal here.
2789 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
2796 ret
= ocfs2_rotate_subtree_left(inode
, handle
, left_path
,
2797 right_path
, subtree_root
,
2798 dealloc
, &deleted
, et
);
2799 if (ret
== -EAGAIN
) {
2801 * The rotation has to temporarily stop due to
2802 * the right subtree having an empty
2803 * extent. Pass it back to the caller for a
2806 *empty_extent_path
= right_path
;
2816 * The subtree rotate might have removed records on
2817 * the rightmost edge. If so, then rotation is
2823 ocfs2_mv_path(left_path
, right_path
);
2825 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
2834 ocfs2_free_path(right_path
);
2835 ocfs2_free_path(left_path
);
2840 static int ocfs2_remove_rightmost_path(struct inode
*inode
, handle_t
*handle
,
2841 struct ocfs2_path
*path
,
2842 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2843 struct ocfs2_extent_tree
*et
)
2845 int ret
, subtree_index
;
2847 struct ocfs2_path
*left_path
= NULL
;
2848 struct ocfs2_extent_block
*eb
;
2849 struct ocfs2_extent_list
*el
;
2852 ret
= ocfs2_et_sanity_check(inode
, et
);
2856 * There's two ways we handle this depending on
2857 * whether path is the only existing one.
2859 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
2860 handle
->h_buffer_credits
,
2867 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
2873 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
2881 * We have a path to the left of this one - it needs
2884 left_path
= ocfs2_new_path_from_path(path
);
2891 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
2897 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
2903 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
2905 ocfs2_unlink_subtree(inode
, handle
, left_path
, path
,
2906 subtree_index
, dealloc
);
2907 ocfs2_update_edge_lengths(inode
, handle
, left_path
);
2909 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(left_path
)->b_data
;
2910 ocfs2_et_set_last_eb_blk(et
, le64_to_cpu(eb
->h_blkno
));
2913 * 'path' is also the leftmost path which
2914 * means it must be the only one. This gets
2915 * handled differently because we want to
2916 * revert the inode back to having extents
2919 ocfs2_unlink_path(inode
, handle
, dealloc
, path
, 1);
2921 el
= et
->et_root_el
;
2922 el
->l_tree_depth
= 0;
2923 el
->l_next_free_rec
= 0;
2924 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
2926 ocfs2_et_set_last_eb_blk(et
, 0);
2929 ocfs2_journal_dirty(handle
, path_root_bh(path
));
2932 ocfs2_free_path(left_path
);
2937 * Left rotation of btree records.
2939 * In many ways, this is (unsurprisingly) the opposite of right
2940 * rotation. We start at some non-rightmost path containing an empty
2941 * extent in the leaf block. The code works its way to the rightmost
2942 * path by rotating records to the left in every subtree.
2944 * This is used by any code which reduces the number of extent records
2945 * in a leaf. After removal, an empty record should be placed in the
2946 * leftmost list position.
2948 * This won't handle a length update of the rightmost path records if
2949 * the rightmost tree leaf record is removed so the caller is
2950 * responsible for detecting and correcting that.
2952 static int ocfs2_rotate_tree_left(struct inode
*inode
, handle_t
*handle
,
2953 struct ocfs2_path
*path
,
2954 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
2955 struct ocfs2_extent_tree
*et
)
2957 int ret
, orig_credits
= handle
->h_buffer_credits
;
2958 struct ocfs2_path
*tmp_path
= NULL
, *restart_path
= NULL
;
2959 struct ocfs2_extent_block
*eb
;
2960 struct ocfs2_extent_list
*el
;
2962 el
= path_leaf_el(path
);
2963 if (!ocfs2_is_empty_extent(&el
->l_recs
[0]))
2966 if (path
->p_tree_depth
== 0) {
2967 rightmost_no_delete
:
2969 * Inline extents. This is trivially handled, so do
2972 ret
= ocfs2_rotate_rightmost_leaf_left(inode
, handle
,
2980 * Handle rightmost branch now. There's several cases:
2981 * 1) simple rotation leaving records in there. That's trivial.
2982 * 2) rotation requiring a branch delete - there's no more
2983 * records left. Two cases of this:
2984 * a) There are branches to the left.
2985 * b) This is also the leftmost (the only) branch.
2987 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2988 * 2a) we need the left branch so that we can update it with the unlink
2989 * 2b) we need to bring the inode back to inline extents.
2992 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
2994 if (eb
->h_next_leaf_blk
== 0) {
2996 * This gets a bit tricky if we're going to delete the
2997 * rightmost path. Get the other cases out of the way
3000 if (le16_to_cpu(el
->l_next_free_rec
) > 1)
3001 goto rightmost_no_delete
;
3003 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
3005 ocfs2_error(inode
->i_sb
,
3006 "Inode %llu has empty extent block at %llu",
3007 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
3008 (unsigned long long)le64_to_cpu(eb
->h_blkno
));
3013 * XXX: The caller can not trust "path" any more after
3014 * this as it will have been deleted. What do we do?
3016 * In theory the rotate-for-merge code will never get
3017 * here because it'll always ask for a rotate in a
3021 ret
= ocfs2_remove_rightmost_path(inode
, handle
, path
,
3029 * Now we can loop, remembering the path we get from -EAGAIN
3030 * and restarting from there.
3033 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
, path
,
3034 dealloc
, &restart_path
, et
);
3035 if (ret
&& ret
!= -EAGAIN
) {
3040 while (ret
== -EAGAIN
) {
3041 tmp_path
= restart_path
;
3042 restart_path
= NULL
;
3044 ret
= __ocfs2_rotate_tree_left(inode
, handle
, orig_credits
,
3047 if (ret
&& ret
!= -EAGAIN
) {
3052 ocfs2_free_path(tmp_path
);
3060 ocfs2_free_path(tmp_path
);
3061 ocfs2_free_path(restart_path
);
3065 static void ocfs2_cleanup_merge(struct ocfs2_extent_list
*el
,
3068 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[index
];
3071 if (rec
->e_leaf_clusters
== 0) {
3073 * We consumed all of the merged-from record. An empty
3074 * extent cannot exist anywhere but the 1st array
3075 * position, so move things over if the merged-from
3076 * record doesn't occupy that position.
3078 * This creates a new empty extent so the caller
3079 * should be smart enough to have removed any existing
3083 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
3084 size
= index
* sizeof(struct ocfs2_extent_rec
);
3085 memmove(&el
->l_recs
[1], &el
->l_recs
[0], size
);
3089 * Always memset - the caller doesn't check whether it
3090 * created an empty extent, so there could be junk in
3093 memset(&el
->l_recs
[0], 0, sizeof(struct ocfs2_extent_rec
));
3097 static int ocfs2_get_right_path(struct inode
*inode
,
3098 struct ocfs2_path
*left_path
,
3099 struct ocfs2_path
**ret_right_path
)
3103 struct ocfs2_path
*right_path
= NULL
;
3104 struct ocfs2_extent_list
*left_el
;
3106 *ret_right_path
= NULL
;
3108 /* This function shouldn't be called for non-trees. */
3109 BUG_ON(left_path
->p_tree_depth
== 0);
3111 left_el
= path_leaf_el(left_path
);
3112 BUG_ON(left_el
->l_next_free_rec
!= left_el
->l_count
);
3114 ret
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
, left_path
,
3121 /* This function shouldn't be called for the rightmost leaf. */
3122 BUG_ON(right_cpos
== 0);
3124 right_path
= ocfs2_new_path_from_path(left_path
);
3131 ret
= ocfs2_find_path(inode
, right_path
, right_cpos
);
3137 *ret_right_path
= right_path
;
3140 ocfs2_free_path(right_path
);
3145 * Remove split_rec clusters from the record at index and merge them
3146 * onto the beginning of the record "next" to it.
3147 * For index < l_count - 1, the next means the extent rec at index + 1.
3148 * For index == l_count - 1, the "next" means the 1st extent rec of the
3149 * next extent block.
3151 static int ocfs2_merge_rec_right(struct inode
*inode
,
3152 struct ocfs2_path
*left_path
,
3154 struct ocfs2_extent_rec
*split_rec
,
3157 int ret
, next_free
, i
;
3158 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3159 struct ocfs2_extent_rec
*left_rec
;
3160 struct ocfs2_extent_rec
*right_rec
;
3161 struct ocfs2_extent_list
*right_el
;
3162 struct ocfs2_path
*right_path
= NULL
;
3163 int subtree_index
= 0;
3164 struct ocfs2_extent_list
*el
= path_leaf_el(left_path
);
3165 struct buffer_head
*bh
= path_leaf_bh(left_path
);
3166 struct buffer_head
*root_bh
= NULL
;
3168 BUG_ON(index
>= le16_to_cpu(el
->l_next_free_rec
));
3169 left_rec
= &el
->l_recs
[index
];
3171 if (index
== le16_to_cpu(el
->l_next_free_rec
) - 1 &&
3172 le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
)) {
3173 /* we meet with a cross extent block merge. */
3174 ret
= ocfs2_get_right_path(inode
, left_path
, &right_path
);
3180 right_el
= path_leaf_el(right_path
);
3181 next_free
= le16_to_cpu(right_el
->l_next_free_rec
);
3182 BUG_ON(next_free
<= 0);
3183 right_rec
= &right_el
->l_recs
[0];
3184 if (ocfs2_is_empty_extent(right_rec
)) {
3185 BUG_ON(next_free
<= 1);
3186 right_rec
= &right_el
->l_recs
[1];
3189 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3190 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3191 le32_to_cpu(right_rec
->e_cpos
));
3193 subtree_index
= ocfs2_find_subtree_root(inode
,
3194 left_path
, right_path
);
3196 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3197 handle
->h_buffer_credits
,
3204 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3205 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3207 ret
= ocfs2_path_bh_journal_access(handle
, inode
, right_path
,
3214 for (i
= subtree_index
+ 1;
3215 i
< path_num_items(right_path
); i
++) {
3216 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
3223 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
3232 BUG_ON(index
== le16_to_cpu(el
->l_next_free_rec
) - 1);
3233 right_rec
= &el
->l_recs
[index
+ 1];
3236 ret
= ocfs2_path_bh_journal_access(handle
, inode
, left_path
,
3237 path_num_items(left_path
) - 1);
3243 le16_add_cpu(&left_rec
->e_leaf_clusters
, -split_clusters
);
3245 le32_add_cpu(&right_rec
->e_cpos
, -split_clusters
);
3246 le64_add_cpu(&right_rec
->e_blkno
,
3247 -ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
3248 le16_add_cpu(&right_rec
->e_leaf_clusters
, split_clusters
);
3250 ocfs2_cleanup_merge(el
, index
);
3252 ret
= ocfs2_journal_dirty(handle
, bh
);
3257 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(right_path
));
3261 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3262 right_path
, subtree_index
);
3266 ocfs2_free_path(right_path
);
3270 static int ocfs2_get_left_path(struct inode
*inode
,
3271 struct ocfs2_path
*right_path
,
3272 struct ocfs2_path
**ret_left_path
)
3276 struct ocfs2_path
*left_path
= NULL
;
3278 *ret_left_path
= NULL
;
3280 /* This function shouldn't be called for non-trees. */
3281 BUG_ON(right_path
->p_tree_depth
== 0);
3283 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
3284 right_path
, &left_cpos
);
3290 /* This function shouldn't be called for the leftmost leaf. */
3291 BUG_ON(left_cpos
== 0);
3293 left_path
= ocfs2_new_path_from_path(right_path
);
3300 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
3306 *ret_left_path
= left_path
;
3309 ocfs2_free_path(left_path
);
3314 * Remove split_rec clusters from the record at index and merge them
3315 * onto the tail of the record "before" it.
3316 * For index > 0, the "before" means the extent rec at index - 1.
3318 * For index == 0, the "before" means the last record of the previous
3319 * extent block. And there is also a situation that we may need to
3320 * remove the rightmost leaf extent block in the right_path and change
3321 * the right path to indicate the new rightmost path.
3323 static int ocfs2_merge_rec_left(struct inode
*inode
,
3324 struct ocfs2_path
*right_path
,
3326 struct ocfs2_extent_rec
*split_rec
,
3327 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3328 struct ocfs2_extent_tree
*et
,
3331 int ret
, i
, subtree_index
= 0, has_empty_extent
= 0;
3332 unsigned int split_clusters
= le16_to_cpu(split_rec
->e_leaf_clusters
);
3333 struct ocfs2_extent_rec
*left_rec
;
3334 struct ocfs2_extent_rec
*right_rec
;
3335 struct ocfs2_extent_list
*el
= path_leaf_el(right_path
);
3336 struct buffer_head
*bh
= path_leaf_bh(right_path
);
3337 struct buffer_head
*root_bh
= NULL
;
3338 struct ocfs2_path
*left_path
= NULL
;
3339 struct ocfs2_extent_list
*left_el
;
3343 right_rec
= &el
->l_recs
[index
];
3345 /* we meet with a cross extent block merge. */
3346 ret
= ocfs2_get_left_path(inode
, right_path
, &left_path
);
3352 left_el
= path_leaf_el(left_path
);
3353 BUG_ON(le16_to_cpu(left_el
->l_next_free_rec
) !=
3354 le16_to_cpu(left_el
->l_count
));
3356 left_rec
= &left_el
->l_recs
[
3357 le16_to_cpu(left_el
->l_next_free_rec
) - 1];
3358 BUG_ON(le32_to_cpu(left_rec
->e_cpos
) +
3359 le16_to_cpu(left_rec
->e_leaf_clusters
) !=
3360 le32_to_cpu(split_rec
->e_cpos
));
3362 subtree_index
= ocfs2_find_subtree_root(inode
,
3363 left_path
, right_path
);
3365 ret
= ocfs2_extend_rotate_transaction(handle
, subtree_index
,
3366 handle
->h_buffer_credits
,
3373 root_bh
= left_path
->p_node
[subtree_index
].bh
;
3374 BUG_ON(root_bh
!= right_path
->p_node
[subtree_index
].bh
);
3376 ret
= ocfs2_path_bh_journal_access(handle
, inode
, right_path
,
3383 for (i
= subtree_index
+ 1;
3384 i
< path_num_items(right_path
); i
++) {
3385 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
3392 ret
= ocfs2_path_bh_journal_access(handle
, inode
,
3400 left_rec
= &el
->l_recs
[index
- 1];
3401 if (ocfs2_is_empty_extent(&el
->l_recs
[0]))
3402 has_empty_extent
= 1;
3405 ret
= ocfs2_path_bh_journal_access(handle
, inode
, right_path
,
3406 path_num_items(right_path
) - 1);
3412 if (has_empty_extent
&& index
== 1) {
3414 * The easy case - we can just plop the record right in.
3416 *left_rec
= *split_rec
;
3418 has_empty_extent
= 0;
3420 le16_add_cpu(&left_rec
->e_leaf_clusters
, split_clusters
);
3422 le32_add_cpu(&right_rec
->e_cpos
, split_clusters
);
3423 le64_add_cpu(&right_rec
->e_blkno
,
3424 ocfs2_clusters_to_blocks(inode
->i_sb
, split_clusters
));
3425 le16_add_cpu(&right_rec
->e_leaf_clusters
, -split_clusters
);
3427 ocfs2_cleanup_merge(el
, index
);
3429 ret
= ocfs2_journal_dirty(handle
, bh
);
3434 ret
= ocfs2_journal_dirty(handle
, path_leaf_bh(left_path
));
3439 * In the situation that the right_rec is empty and the extent
3440 * block is empty also, ocfs2_complete_edge_insert can't handle
3441 * it and we need to delete the right extent block.
3443 if (le16_to_cpu(right_rec
->e_leaf_clusters
) == 0 &&
3444 le16_to_cpu(el
->l_next_free_rec
) == 1) {
3446 ret
= ocfs2_remove_rightmost_path(inode
, handle
,
3454 /* Now the rightmost extent block has been deleted.
3455 * So we use the new rightmost path.
3457 ocfs2_mv_path(right_path
, left_path
);
3460 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
3461 right_path
, subtree_index
);
3465 ocfs2_free_path(left_path
);
3469 static int ocfs2_try_to_merge_extent(struct inode
*inode
,
3471 struct ocfs2_path
*path
,
3473 struct ocfs2_extent_rec
*split_rec
,
3474 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
3475 struct ocfs2_merge_ctxt
*ctxt
,
3476 struct ocfs2_extent_tree
*et
)
3480 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
3481 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
3483 BUG_ON(ctxt
->c_contig_type
== CONTIG_NONE
);
3485 if (ctxt
->c_split_covers_rec
&& ctxt
->c_has_empty_extent
) {
3487 * The merge code will need to create an empty
3488 * extent to take the place of the newly
3489 * emptied slot. Remove any pre-existing empty
3490 * extents - having more than one in a leaf is
3493 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3500 rec
= &el
->l_recs
[split_index
];
3503 if (ctxt
->c_contig_type
== CONTIG_LEFTRIGHT
) {
3505 * Left-right contig implies this.
3507 BUG_ON(!ctxt
->c_split_covers_rec
);
3510 * Since the leftright insert always covers the entire
3511 * extent, this call will delete the insert record
3512 * entirely, resulting in an empty extent record added to
3515 * Since the adding of an empty extent shifts
3516 * everything back to the right, there's no need to
3517 * update split_index here.
3519 * When the split_index is zero, we need to merge it to the
3520 * prevoius extent block. It is more efficient and easier
3521 * if we do merge_right first and merge_left later.
3523 ret
= ocfs2_merge_rec_right(inode
, path
,
3532 * We can only get this from logic error above.
3534 BUG_ON(!ocfs2_is_empty_extent(&el
->l_recs
[0]));
3536 /* The merge left us with an empty extent, remove it. */
3537 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3544 rec
= &el
->l_recs
[split_index
];
3547 * Note that we don't pass split_rec here on purpose -
3548 * we've merged it into the rec already.
3550 ret
= ocfs2_merge_rec_left(inode
, path
,
3560 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3563 * Error from this last rotate is not critical, so
3564 * print but don't bubble it up.
3571 * Merge a record to the left or right.
3573 * 'contig_type' is relative to the existing record,
3574 * so for example, if we're "right contig", it's to
3575 * the record on the left (hence the left merge).
3577 if (ctxt
->c_contig_type
== CONTIG_RIGHT
) {
3578 ret
= ocfs2_merge_rec_left(inode
,
3588 ret
= ocfs2_merge_rec_right(inode
,
3598 if (ctxt
->c_split_covers_rec
) {
3600 * The merge may have left an empty extent in
3601 * our leaf. Try to rotate it away.
3603 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
,
3615 static void ocfs2_subtract_from_rec(struct super_block
*sb
,
3616 enum ocfs2_split_type split
,
3617 struct ocfs2_extent_rec
*rec
,
3618 struct ocfs2_extent_rec
*split_rec
)
3622 len_blocks
= ocfs2_clusters_to_blocks(sb
,
3623 le16_to_cpu(split_rec
->e_leaf_clusters
));
3625 if (split
== SPLIT_LEFT
) {
3627 * Region is on the left edge of the existing
3630 le32_add_cpu(&rec
->e_cpos
,
3631 le16_to_cpu(split_rec
->e_leaf_clusters
));
3632 le64_add_cpu(&rec
->e_blkno
, len_blocks
);
3633 le16_add_cpu(&rec
->e_leaf_clusters
,
3634 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3637 * Region is on the right edge of the existing
3640 le16_add_cpu(&rec
->e_leaf_clusters
,
3641 -le16_to_cpu(split_rec
->e_leaf_clusters
));
3646 * Do the final bits of extent record insertion at the target leaf
3647 * list. If this leaf is part of an allocation tree, it is assumed
3648 * that the tree above has been prepared.
3650 static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec
*insert_rec
,
3651 struct ocfs2_extent_list
*el
,
3652 struct ocfs2_insert_type
*insert
,
3653 struct inode
*inode
)
3655 int i
= insert
->ins_contig_index
;
3657 struct ocfs2_extent_rec
*rec
;
3659 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
3661 if (insert
->ins_split
!= SPLIT_NONE
) {
3662 i
= ocfs2_search_extent_list(el
, le32_to_cpu(insert_rec
->e_cpos
));
3664 rec
= &el
->l_recs
[i
];
3665 ocfs2_subtract_from_rec(inode
->i_sb
, insert
->ins_split
, rec
,
3671 * Contiguous insert - either left or right.
3673 if (insert
->ins_contig
!= CONTIG_NONE
) {
3674 rec
= &el
->l_recs
[i
];
3675 if (insert
->ins_contig
== CONTIG_LEFT
) {
3676 rec
->e_blkno
= insert_rec
->e_blkno
;
3677 rec
->e_cpos
= insert_rec
->e_cpos
;
3679 le16_add_cpu(&rec
->e_leaf_clusters
,
3680 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3685 * Handle insert into an empty leaf.
3687 if (le16_to_cpu(el
->l_next_free_rec
) == 0 ||
3688 ((le16_to_cpu(el
->l_next_free_rec
) == 1) &&
3689 ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3690 el
->l_recs
[0] = *insert_rec
;
3691 el
->l_next_free_rec
= cpu_to_le16(1);
3698 if (insert
->ins_appending
== APPEND_TAIL
) {
3699 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
3700 rec
= &el
->l_recs
[i
];
3701 range
= le32_to_cpu(rec
->e_cpos
)
3702 + le16_to_cpu(rec
->e_leaf_clusters
);
3703 BUG_ON(le32_to_cpu(insert_rec
->e_cpos
) < range
);
3705 mlog_bug_on_msg(le16_to_cpu(el
->l_next_free_rec
) >=
3706 le16_to_cpu(el
->l_count
),
3707 "inode %lu, depth %u, count %u, next free %u, "
3708 "rec.cpos %u, rec.clusters %u, "
3709 "insert.cpos %u, insert.clusters %u\n",
3711 le16_to_cpu(el
->l_tree_depth
),
3712 le16_to_cpu(el
->l_count
),
3713 le16_to_cpu(el
->l_next_free_rec
),
3714 le32_to_cpu(el
->l_recs
[i
].e_cpos
),
3715 le16_to_cpu(el
->l_recs
[i
].e_leaf_clusters
),
3716 le32_to_cpu(insert_rec
->e_cpos
),
3717 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3719 el
->l_recs
[i
] = *insert_rec
;
3720 le16_add_cpu(&el
->l_next_free_rec
, 1);
3726 * Ok, we have to rotate.
3728 * At this point, it is safe to assume that inserting into an
3729 * empty leaf and appending to a leaf have both been handled
3732 * This leaf needs to have space, either by the empty 1st
3733 * extent record, or by virtue of an l_next_rec < l_count.
3735 ocfs2_rotate_leaf(el
, insert_rec
);
3738 static void ocfs2_adjust_rightmost_records(struct inode
*inode
,
3740 struct ocfs2_path
*path
,
3741 struct ocfs2_extent_rec
*insert_rec
)
3743 int ret
, i
, next_free
;
3744 struct buffer_head
*bh
;
3745 struct ocfs2_extent_list
*el
;
3746 struct ocfs2_extent_rec
*rec
;
3749 * Update everything except the leaf block.
3751 for (i
= 0; i
< path
->p_tree_depth
; i
++) {
3752 bh
= path
->p_node
[i
].bh
;
3753 el
= path
->p_node
[i
].el
;
3755 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3756 if (next_free
== 0) {
3757 ocfs2_error(inode
->i_sb
,
3758 "Dinode %llu has a bad extent list",
3759 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
3764 rec
= &el
->l_recs
[next_free
- 1];
3766 rec
->e_int_clusters
= insert_rec
->e_cpos
;
3767 le32_add_cpu(&rec
->e_int_clusters
,
3768 le16_to_cpu(insert_rec
->e_leaf_clusters
));
3769 le32_add_cpu(&rec
->e_int_clusters
,
3770 -le32_to_cpu(rec
->e_cpos
));
3772 ret
= ocfs2_journal_dirty(handle
, bh
);
3779 static int ocfs2_append_rec_to_path(struct inode
*inode
, handle_t
*handle
,
3780 struct ocfs2_extent_rec
*insert_rec
,
3781 struct ocfs2_path
*right_path
,
3782 struct ocfs2_path
**ret_left_path
)
3785 struct ocfs2_extent_list
*el
;
3786 struct ocfs2_path
*left_path
= NULL
;
3788 *ret_left_path
= NULL
;
3791 * This shouldn't happen for non-trees. The extent rec cluster
3792 * count manipulation below only works for interior nodes.
3794 BUG_ON(right_path
->p_tree_depth
== 0);
3797 * If our appending insert is at the leftmost edge of a leaf,
3798 * then we might need to update the rightmost records of the
3801 el
= path_leaf_el(right_path
);
3802 next_free
= le16_to_cpu(el
->l_next_free_rec
);
3803 if (next_free
== 0 ||
3804 (next_free
== 1 && ocfs2_is_empty_extent(&el
->l_recs
[0]))) {
3807 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, right_path
,
3814 mlog(0, "Append may need a left path update. cpos: %u, "
3815 "left_cpos: %u\n", le32_to_cpu(insert_rec
->e_cpos
),
3819 * No need to worry if the append is already in the
3823 left_path
= ocfs2_new_path_from_path(right_path
);
3830 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
3837 * ocfs2_insert_path() will pass the left_path to the
3843 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
3849 ocfs2_adjust_rightmost_records(inode
, handle
, right_path
, insert_rec
);
3851 *ret_left_path
= left_path
;
3855 ocfs2_free_path(left_path
);
3860 static void ocfs2_split_record(struct inode
*inode
,
3861 struct ocfs2_path
*left_path
,
3862 struct ocfs2_path
*right_path
,
3863 struct ocfs2_extent_rec
*split_rec
,
3864 enum ocfs2_split_type split
)
3867 u32 cpos
= le32_to_cpu(split_rec
->e_cpos
);
3868 struct ocfs2_extent_list
*left_el
= NULL
, *right_el
, *insert_el
, *el
;
3869 struct ocfs2_extent_rec
*rec
, *tmprec
;
3871 right_el
= path_leaf_el(right_path
);
3873 left_el
= path_leaf_el(left_path
);
3876 insert_el
= right_el
;
3877 index
= ocfs2_search_extent_list(el
, cpos
);
3879 if (index
== 0 && left_path
) {
3880 BUG_ON(ocfs2_is_empty_extent(&el
->l_recs
[0]));
3883 * This typically means that the record
3884 * started in the left path but moved to the
3885 * right as a result of rotation. We either
3886 * move the existing record to the left, or we
3887 * do the later insert there.
3889 * In this case, the left path should always
3890 * exist as the rotate code will have passed
3891 * it back for a post-insert update.
3894 if (split
== SPLIT_LEFT
) {
3896 * It's a left split. Since we know
3897 * that the rotate code gave us an
3898 * empty extent in the left path, we
3899 * can just do the insert there.
3901 insert_el
= left_el
;
3904 * Right split - we have to move the
3905 * existing record over to the left
3906 * leaf. The insert will be into the
3907 * newly created empty extent in the
3910 tmprec
= &right_el
->l_recs
[index
];
3911 ocfs2_rotate_leaf(left_el
, tmprec
);
3914 memset(tmprec
, 0, sizeof(*tmprec
));
3915 index
= ocfs2_search_extent_list(left_el
, cpos
);
3916 BUG_ON(index
== -1);
3921 BUG_ON(!ocfs2_is_empty_extent(&left_el
->l_recs
[0]));
3923 * Left path is easy - we can just allow the insert to
3927 insert_el
= left_el
;
3928 index
= ocfs2_search_extent_list(el
, cpos
);
3929 BUG_ON(index
== -1);
3932 rec
= &el
->l_recs
[index
];
3933 ocfs2_subtract_from_rec(inode
->i_sb
, split
, rec
, split_rec
);
3934 ocfs2_rotate_leaf(insert_el
, split_rec
);
3938 * This function only does inserts on an allocation b-tree. For tree
3939 * depth = 0, ocfs2_insert_at_leaf() is called directly.
3941 * right_path is the path we want to do the actual insert
3942 * in. left_path should only be passed in if we need to update that
3943 * portion of the tree after an edge insert.
3945 static int ocfs2_insert_path(struct inode
*inode
,
3947 struct ocfs2_path
*left_path
,
3948 struct ocfs2_path
*right_path
,
3949 struct ocfs2_extent_rec
*insert_rec
,
3950 struct ocfs2_insert_type
*insert
)
3952 int ret
, subtree_index
;
3953 struct buffer_head
*leaf_bh
= path_leaf_bh(right_path
);
3956 int credits
= handle
->h_buffer_credits
;
3959 * There's a chance that left_path got passed back to
3960 * us without being accounted for in the
3961 * journal. Extend our transaction here to be sure we
3962 * can change those blocks.
3964 credits
+= left_path
->p_tree_depth
;
3966 ret
= ocfs2_extend_trans(handle
, credits
);
3972 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
3980 * Pass both paths to the journal. The majority of inserts
3981 * will be touching all components anyway.
3983 ret
= ocfs2_journal_access_path(inode
, handle
, right_path
);
3989 if (insert
->ins_split
!= SPLIT_NONE
) {
3991 * We could call ocfs2_insert_at_leaf() for some types
3992 * of splits, but it's easier to just let one separate
3993 * function sort it all out.
3995 ocfs2_split_record(inode
, left_path
, right_path
,
3996 insert_rec
, insert
->ins_split
);
3999 * Split might have modified either leaf and we don't
4000 * have a guarantee that the later edge insert will
4001 * dirty this for us.
4004 ret
= ocfs2_journal_dirty(handle
,
4005 path_leaf_bh(left_path
));
4009 ocfs2_insert_at_leaf(insert_rec
, path_leaf_el(right_path
),
4012 ret
= ocfs2_journal_dirty(handle
, leaf_bh
);
4018 * The rotate code has indicated that we need to fix
4019 * up portions of the tree after the insert.
4021 * XXX: Should we extend the transaction here?
4023 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
,
4025 ocfs2_complete_edge_insert(inode
, handle
, left_path
,
4026 right_path
, subtree_index
);
4034 static int ocfs2_do_insert_extent(struct inode
*inode
,
4036 struct ocfs2_extent_tree
*et
,
4037 struct ocfs2_extent_rec
*insert_rec
,
4038 struct ocfs2_insert_type
*type
)
4040 int ret
, rotate
= 0;
4042 struct ocfs2_path
*right_path
= NULL
;
4043 struct ocfs2_path
*left_path
= NULL
;
4044 struct ocfs2_extent_list
*el
;
4046 el
= et
->et_root_el
;
4048 ret
= ocfs2_et_root_journal_access(handle
, inode
, et
,
4049 OCFS2_JOURNAL_ACCESS_WRITE
);
4055 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
4056 ocfs2_insert_at_leaf(insert_rec
, el
, type
, inode
);
4057 goto out_update_clusters
;
4060 right_path
= ocfs2_new_path_from_et(et
);
4068 * Determine the path to start with. Rotations need the
4069 * rightmost path, everything else can go directly to the
4072 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4073 if (type
->ins_appending
== APPEND_NONE
&&
4074 type
->ins_contig
== CONTIG_NONE
) {
4079 ret
= ocfs2_find_path(inode
, right_path
, cpos
);
4086 * Rotations and appends need special treatment - they modify
4087 * parts of the tree's above them.
4089 * Both might pass back a path immediate to the left of the
4090 * one being inserted to. This will be cause
4091 * ocfs2_insert_path() to modify the rightmost records of
4092 * left_path to account for an edge insert.
4094 * XXX: When modifying this code, keep in mind that an insert
4095 * can wind up skipping both of these two special cases...
4098 ret
= ocfs2_rotate_tree_right(inode
, handle
, type
->ins_split
,
4099 le32_to_cpu(insert_rec
->e_cpos
),
4100 right_path
, &left_path
);
4107 * ocfs2_rotate_tree_right() might have extended the
4108 * transaction without re-journaling our tree root.
4110 ret
= ocfs2_et_root_journal_access(handle
, inode
, et
,
4111 OCFS2_JOURNAL_ACCESS_WRITE
);
4116 } else if (type
->ins_appending
== APPEND_TAIL
4117 && type
->ins_contig
!= CONTIG_LEFT
) {
4118 ret
= ocfs2_append_rec_to_path(inode
, handle
, insert_rec
,
4119 right_path
, &left_path
);
4126 ret
= ocfs2_insert_path(inode
, handle
, left_path
, right_path
,
4133 out_update_clusters
:
4134 if (type
->ins_split
== SPLIT_NONE
)
4135 ocfs2_et_update_clusters(inode
, et
,
4136 le16_to_cpu(insert_rec
->e_leaf_clusters
));
4138 ret
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
4143 ocfs2_free_path(left_path
);
4144 ocfs2_free_path(right_path
);
4149 static enum ocfs2_contig_type
4150 ocfs2_figure_merge_contig_type(struct inode
*inode
, struct ocfs2_path
*path
,
4151 struct ocfs2_extent_list
*el
, int index
,
4152 struct ocfs2_extent_rec
*split_rec
)
4155 enum ocfs2_contig_type ret
= CONTIG_NONE
;
4156 u32 left_cpos
, right_cpos
;
4157 struct ocfs2_extent_rec
*rec
= NULL
;
4158 struct ocfs2_extent_list
*new_el
;
4159 struct ocfs2_path
*left_path
= NULL
, *right_path
= NULL
;
4160 struct buffer_head
*bh
;
4161 struct ocfs2_extent_block
*eb
;
4164 rec
= &el
->l_recs
[index
- 1];
4165 } else if (path
->p_tree_depth
> 0) {
4166 status
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
,
4171 if (left_cpos
!= 0) {
4172 left_path
= ocfs2_new_path_from_path(path
);
4176 status
= ocfs2_find_path(inode
, left_path
, left_cpos
);
4180 new_el
= path_leaf_el(left_path
);
4182 if (le16_to_cpu(new_el
->l_next_free_rec
) !=
4183 le16_to_cpu(new_el
->l_count
)) {
4184 bh
= path_leaf_bh(left_path
);
4185 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4186 ocfs2_error(inode
->i_sb
,
4187 "Extent block #%llu has an "
4188 "invalid l_next_free_rec of "
4189 "%d. It should have "
4190 "matched the l_count of %d",
4191 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
4192 le16_to_cpu(new_el
->l_next_free_rec
),
4193 le16_to_cpu(new_el
->l_count
));
4197 rec
= &new_el
->l_recs
[
4198 le16_to_cpu(new_el
->l_next_free_rec
) - 1];
4203 * We're careful to check for an empty extent record here -
4204 * the merge code will know what to do if it sees one.
4207 if (index
== 1 && ocfs2_is_empty_extent(rec
)) {
4208 if (split_rec
->e_cpos
== el
->l_recs
[index
].e_cpos
)
4211 ret
= ocfs2_extent_contig(inode
, rec
, split_rec
);
4216 if (index
< (le16_to_cpu(el
->l_next_free_rec
) - 1))
4217 rec
= &el
->l_recs
[index
+ 1];
4218 else if (le16_to_cpu(el
->l_next_free_rec
) == le16_to_cpu(el
->l_count
) &&
4219 path
->p_tree_depth
> 0) {
4220 status
= ocfs2_find_cpos_for_right_leaf(inode
->i_sb
,
4225 if (right_cpos
== 0)
4228 right_path
= ocfs2_new_path_from_path(path
);
4232 status
= ocfs2_find_path(inode
, right_path
, right_cpos
);
4236 new_el
= path_leaf_el(right_path
);
4237 rec
= &new_el
->l_recs
[0];
4238 if (ocfs2_is_empty_extent(rec
)) {
4239 if (le16_to_cpu(new_el
->l_next_free_rec
) <= 1) {
4240 bh
= path_leaf_bh(right_path
);
4241 eb
= (struct ocfs2_extent_block
*)bh
->b_data
;
4242 ocfs2_error(inode
->i_sb
,
4243 "Extent block #%llu has an "
4244 "invalid l_next_free_rec of %d",
4245 (unsigned long long)le64_to_cpu(eb
->h_blkno
),
4246 le16_to_cpu(new_el
->l_next_free_rec
));
4250 rec
= &new_el
->l_recs
[1];
4255 enum ocfs2_contig_type contig_type
;
4257 contig_type
= ocfs2_extent_contig(inode
, rec
, split_rec
);
4259 if (contig_type
== CONTIG_LEFT
&& ret
== CONTIG_RIGHT
)
4260 ret
= CONTIG_LEFTRIGHT
;
4261 else if (ret
== CONTIG_NONE
)
4267 ocfs2_free_path(left_path
);
4269 ocfs2_free_path(right_path
);
4274 static void ocfs2_figure_contig_type(struct inode
*inode
,
4275 struct ocfs2_insert_type
*insert
,
4276 struct ocfs2_extent_list
*el
,
4277 struct ocfs2_extent_rec
*insert_rec
,
4278 struct ocfs2_extent_tree
*et
)
4281 enum ocfs2_contig_type contig_type
= CONTIG_NONE
;
4283 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4285 for(i
= 0; i
< le16_to_cpu(el
->l_next_free_rec
); i
++) {
4286 contig_type
= ocfs2_extent_contig(inode
, &el
->l_recs
[i
],
4288 if (contig_type
!= CONTIG_NONE
) {
4289 insert
->ins_contig_index
= i
;
4293 insert
->ins_contig
= contig_type
;
4295 if (insert
->ins_contig
!= CONTIG_NONE
) {
4296 struct ocfs2_extent_rec
*rec
=
4297 &el
->l_recs
[insert
->ins_contig_index
];
4298 unsigned int len
= le16_to_cpu(rec
->e_leaf_clusters
) +
4299 le16_to_cpu(insert_rec
->e_leaf_clusters
);
4302 * Caller might want us to limit the size of extents, don't
4303 * calculate contiguousness if we might exceed that limit.
4305 if (et
->et_max_leaf_clusters
&&
4306 (len
> et
->et_max_leaf_clusters
))
4307 insert
->ins_contig
= CONTIG_NONE
;
4312 * This should only be called against the righmost leaf extent list.
4314 * ocfs2_figure_appending_type() will figure out whether we'll have to
4315 * insert at the tail of the rightmost leaf.
4317 * This should also work against the root extent list for tree's with 0
4318 * depth. If we consider the root extent list to be the rightmost leaf node
4319 * then the logic here makes sense.
4321 static void ocfs2_figure_appending_type(struct ocfs2_insert_type
*insert
,
4322 struct ocfs2_extent_list
*el
,
4323 struct ocfs2_extent_rec
*insert_rec
)
4326 u32 cpos
= le32_to_cpu(insert_rec
->e_cpos
);
4327 struct ocfs2_extent_rec
*rec
;
4329 insert
->ins_appending
= APPEND_NONE
;
4331 BUG_ON(le16_to_cpu(el
->l_tree_depth
) != 0);
4333 if (!el
->l_next_free_rec
)
4334 goto set_tail_append
;
4336 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
4337 /* Were all records empty? */
4338 if (le16_to_cpu(el
->l_next_free_rec
) == 1)
4339 goto set_tail_append
;
4342 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
4343 rec
= &el
->l_recs
[i
];
4346 (le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)))
4347 goto set_tail_append
;
4352 insert
->ins_appending
= APPEND_TAIL
;
4356 * Helper function called at the begining of an insert.
4358 * This computes a few things that are commonly used in the process of
4359 * inserting into the btree:
4360 * - Whether the new extent is contiguous with an existing one.
4361 * - The current tree depth.
4362 * - Whether the insert is an appending one.
4363 * - The total # of free records in the tree.
4365 * All of the information is stored on the ocfs2_insert_type
4368 static int ocfs2_figure_insert_type(struct inode
*inode
,
4369 struct ocfs2_extent_tree
*et
,
4370 struct buffer_head
**last_eb_bh
,
4371 struct ocfs2_extent_rec
*insert_rec
,
4373 struct ocfs2_insert_type
*insert
)
4376 struct ocfs2_extent_block
*eb
;
4377 struct ocfs2_extent_list
*el
;
4378 struct ocfs2_path
*path
= NULL
;
4379 struct buffer_head
*bh
= NULL
;
4381 insert
->ins_split
= SPLIT_NONE
;
4383 el
= et
->et_root_el
;
4384 insert
->ins_tree_depth
= le16_to_cpu(el
->l_tree_depth
);
4386 if (el
->l_tree_depth
) {
4388 * If we have tree depth, we read in the
4389 * rightmost extent block ahead of time as
4390 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4391 * may want it later.
4393 ret
= ocfs2_read_extent_block(inode
,
4394 ocfs2_et_get_last_eb_blk(et
),
4400 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
4405 * Unless we have a contiguous insert, we'll need to know if
4406 * there is room left in our allocation tree for another
4409 * XXX: This test is simplistic, we can search for empty
4410 * extent records too.
4412 *free_records
= le16_to_cpu(el
->l_count
) -
4413 le16_to_cpu(el
->l_next_free_rec
);
4415 if (!insert
->ins_tree_depth
) {
4416 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4417 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4421 path
= ocfs2_new_path_from_et(et
);
4429 * In the case that we're inserting past what the tree
4430 * currently accounts for, ocfs2_find_path() will return for
4431 * us the rightmost tree path. This is accounted for below in
4432 * the appending code.
4434 ret
= ocfs2_find_path(inode
, path
, le32_to_cpu(insert_rec
->e_cpos
));
4440 el
= path_leaf_el(path
);
4443 * Now that we have the path, there's two things we want to determine:
4444 * 1) Contiguousness (also set contig_index if this is so)
4446 * 2) Are we doing an append? We can trivially break this up
4447 * into two types of appends: simple record append, or a
4448 * rotate inside the tail leaf.
4450 ocfs2_figure_contig_type(inode
, insert
, el
, insert_rec
, et
);
4453 * The insert code isn't quite ready to deal with all cases of
4454 * left contiguousness. Specifically, if it's an insert into
4455 * the 1st record in a leaf, it will require the adjustment of
4456 * cluster count on the last record of the path directly to it's
4457 * left. For now, just catch that case and fool the layers
4458 * above us. This works just fine for tree_depth == 0, which
4459 * is why we allow that above.
4461 if (insert
->ins_contig
== CONTIG_LEFT
&&
4462 insert
->ins_contig_index
== 0)
4463 insert
->ins_contig
= CONTIG_NONE
;
4466 * Ok, so we can simply compare against last_eb to figure out
4467 * whether the path doesn't exist. This will only happen in
4468 * the case that we're doing a tail append, so maybe we can
4469 * take advantage of that information somehow.
4471 if (ocfs2_et_get_last_eb_blk(et
) ==
4472 path_leaf_bh(path
)->b_blocknr
) {
4474 * Ok, ocfs2_find_path() returned us the rightmost
4475 * tree path. This might be an appending insert. There are
4477 * 1) We're doing a true append at the tail:
4478 * -This might even be off the end of the leaf
4479 * 2) We're "appending" by rotating in the tail
4481 ocfs2_figure_appending_type(insert
, el
, insert_rec
);
4485 ocfs2_free_path(path
);
4495 * Insert an extent into an inode btree.
4497 * The caller needs to update fe->i_clusters
4499 int ocfs2_insert_extent(struct ocfs2_super
*osb
,
4501 struct inode
*inode
,
4502 struct ocfs2_extent_tree
*et
,
4507 struct ocfs2_alloc_context
*meta_ac
)
4510 int uninitialized_var(free_records
);
4511 struct buffer_head
*last_eb_bh
= NULL
;
4512 struct ocfs2_insert_type insert
= {0, };
4513 struct ocfs2_extent_rec rec
;
4515 mlog(0, "add %u clusters at position %u to inode %llu\n",
4516 new_clusters
, cpos
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4518 memset(&rec
, 0, sizeof(rec
));
4519 rec
.e_cpos
= cpu_to_le32(cpos
);
4520 rec
.e_blkno
= cpu_to_le64(start_blk
);
4521 rec
.e_leaf_clusters
= cpu_to_le16(new_clusters
);
4522 rec
.e_flags
= flags
;
4523 status
= ocfs2_et_insert_check(inode
, et
, &rec
);
4529 status
= ocfs2_figure_insert_type(inode
, et
, &last_eb_bh
, &rec
,
4530 &free_records
, &insert
);
4536 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4537 "Insert.contig_index: %d, Insert.free_records: %d, "
4538 "Insert.tree_depth: %d\n",
4539 insert
.ins_appending
, insert
.ins_contig
, insert
.ins_contig_index
,
4540 free_records
, insert
.ins_tree_depth
);
4542 if (insert
.ins_contig
== CONTIG_NONE
&& free_records
== 0) {
4543 status
= ocfs2_grow_tree(inode
, handle
, et
,
4544 &insert
.ins_tree_depth
, &last_eb_bh
,
4552 /* Finally, we can add clusters. This might rotate the tree for us. */
4553 status
= ocfs2_do_insert_extent(inode
, handle
, et
, &rec
, &insert
);
4556 else if (et
->et_ops
== &ocfs2_dinode_et_ops
)
4557 ocfs2_extent_map_insert_rec(inode
, &rec
);
4567 * Allcate and add clusters into the extent b-tree.
4568 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4569 * The extent b-tree's root is specified by et, and
4570 * it is not limited to the file storage. Any extent tree can use this
4571 * function if it implements the proper ocfs2_extent_tree.
4573 int ocfs2_add_clusters_in_btree(struct ocfs2_super
*osb
,
4574 struct inode
*inode
,
4575 u32
*logical_offset
,
4576 u32 clusters_to_add
,
4578 struct ocfs2_extent_tree
*et
,
4580 struct ocfs2_alloc_context
*data_ac
,
4581 struct ocfs2_alloc_context
*meta_ac
,
4582 enum ocfs2_alloc_restarted
*reason_ret
)
4586 enum ocfs2_alloc_restarted reason
= RESTART_NONE
;
4587 u32 bit_off
, num_bits
;
4591 BUG_ON(!clusters_to_add
);
4594 flags
= OCFS2_EXT_UNWRITTEN
;
4596 free_extents
= ocfs2_num_free_extents(osb
, inode
, et
);
4597 if (free_extents
< 0) {
4598 status
= free_extents
;
4603 /* there are two cases which could cause us to EAGAIN in the
4604 * we-need-more-metadata case:
4605 * 1) we haven't reserved *any*
4606 * 2) we are so fragmented, we've needed to add metadata too
4608 if (!free_extents
&& !meta_ac
) {
4609 mlog(0, "we haven't reserved any metadata!\n");
4611 reason
= RESTART_META
;
4613 } else if ((!free_extents
)
4614 && (ocfs2_alloc_context_bits_left(meta_ac
)
4615 < ocfs2_extend_meta_needed(et
->et_root_el
))) {
4616 mlog(0, "filesystem is really fragmented...\n");
4618 reason
= RESTART_META
;
4622 status
= __ocfs2_claim_clusters(osb
, handle
, data_ac
, 1,
4623 clusters_to_add
, &bit_off
, &num_bits
);
4625 if (status
!= -ENOSPC
)
4630 BUG_ON(num_bits
> clusters_to_add
);
4632 /* reserve our write early -- insert_extent may update the tree root */
4633 status
= ocfs2_et_root_journal_access(handle
, inode
, et
,
4634 OCFS2_JOURNAL_ACCESS_WRITE
);
4640 block
= ocfs2_clusters_to_blocks(osb
->sb
, bit_off
);
4641 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4642 num_bits
, bit_off
, (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4643 status
= ocfs2_insert_extent(osb
, handle
, inode
, et
,
4644 *logical_offset
, block
,
4645 num_bits
, flags
, meta_ac
);
4651 status
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
4657 clusters_to_add
-= num_bits
;
4658 *logical_offset
+= num_bits
;
4660 if (clusters_to_add
) {
4661 mlog(0, "need to alloc once more, wanted = %u\n",
4664 reason
= RESTART_TRANS
;
4670 *reason_ret
= reason
;
4674 static void ocfs2_make_right_split_rec(struct super_block
*sb
,
4675 struct ocfs2_extent_rec
*split_rec
,
4677 struct ocfs2_extent_rec
*rec
)
4679 u32 rec_cpos
= le32_to_cpu(rec
->e_cpos
);
4680 u32 rec_range
= rec_cpos
+ le16_to_cpu(rec
->e_leaf_clusters
);
4682 memset(split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
4684 split_rec
->e_cpos
= cpu_to_le32(cpos
);
4685 split_rec
->e_leaf_clusters
= cpu_to_le16(rec_range
- cpos
);
4687 split_rec
->e_blkno
= rec
->e_blkno
;
4688 le64_add_cpu(&split_rec
->e_blkno
,
4689 ocfs2_clusters_to_blocks(sb
, cpos
- rec_cpos
));
4691 split_rec
->e_flags
= rec
->e_flags
;
4694 static int ocfs2_split_and_insert(struct inode
*inode
,
4696 struct ocfs2_path
*path
,
4697 struct ocfs2_extent_tree
*et
,
4698 struct buffer_head
**last_eb_bh
,
4700 struct ocfs2_extent_rec
*orig_split_rec
,
4701 struct ocfs2_alloc_context
*meta_ac
)
4704 unsigned int insert_range
, rec_range
, do_leftright
= 0;
4705 struct ocfs2_extent_rec tmprec
;
4706 struct ocfs2_extent_list
*rightmost_el
;
4707 struct ocfs2_extent_rec rec
;
4708 struct ocfs2_extent_rec split_rec
= *orig_split_rec
;
4709 struct ocfs2_insert_type insert
;
4710 struct ocfs2_extent_block
*eb
;
4714 * Store a copy of the record on the stack - it might move
4715 * around as the tree is manipulated below.
4717 rec
= path_leaf_el(path
)->l_recs
[split_index
];
4719 rightmost_el
= et
->et_root_el
;
4721 depth
= le16_to_cpu(rightmost_el
->l_tree_depth
);
4723 BUG_ON(!(*last_eb_bh
));
4724 eb
= (struct ocfs2_extent_block
*) (*last_eb_bh
)->b_data
;
4725 rightmost_el
= &eb
->h_list
;
4728 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
4729 le16_to_cpu(rightmost_el
->l_count
)) {
4730 ret
= ocfs2_grow_tree(inode
, handle
, et
,
4731 &depth
, last_eb_bh
, meta_ac
);
4738 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
4739 insert
.ins_appending
= APPEND_NONE
;
4740 insert
.ins_contig
= CONTIG_NONE
;
4741 insert
.ins_tree_depth
= depth
;
4743 insert_range
= le32_to_cpu(split_rec
.e_cpos
) +
4744 le16_to_cpu(split_rec
.e_leaf_clusters
);
4745 rec_range
= le32_to_cpu(rec
.e_cpos
) +
4746 le16_to_cpu(rec
.e_leaf_clusters
);
4748 if (split_rec
.e_cpos
== rec
.e_cpos
) {
4749 insert
.ins_split
= SPLIT_LEFT
;
4750 } else if (insert_range
== rec_range
) {
4751 insert
.ins_split
= SPLIT_RIGHT
;
4754 * Left/right split. We fake this as a right split
4755 * first and then make a second pass as a left split.
4757 insert
.ins_split
= SPLIT_RIGHT
;
4759 ocfs2_make_right_split_rec(inode
->i_sb
, &tmprec
, insert_range
,
4764 BUG_ON(do_leftright
);
4768 ret
= ocfs2_do_insert_extent(inode
, handle
, et
, &split_rec
, &insert
);
4774 if (do_leftright
== 1) {
4776 struct ocfs2_extent_list
*el
;
4779 split_rec
= *orig_split_rec
;
4781 ocfs2_reinit_path(path
, 1);
4783 cpos
= le32_to_cpu(split_rec
.e_cpos
);
4784 ret
= ocfs2_find_path(inode
, path
, cpos
);
4790 el
= path_leaf_el(path
);
4791 split_index
= ocfs2_search_extent_list(el
, cpos
);
4799 static int ocfs2_replace_extent_rec(struct inode
*inode
,
4801 struct ocfs2_path
*path
,
4802 struct ocfs2_extent_list
*el
,
4804 struct ocfs2_extent_rec
*split_rec
)
4808 ret
= ocfs2_path_bh_journal_access(handle
, inode
, path
,
4809 path_num_items(path
) - 1);
4815 el
->l_recs
[split_index
] = *split_rec
;
4817 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
4823 * Mark part or all of the extent record at split_index in the leaf
4824 * pointed to by path as written. This removes the unwritten
4827 * Care is taken to handle contiguousness so as to not grow the tree.
4829 * meta_ac is not strictly necessary - we only truly need it if growth
4830 * of the tree is required. All other cases will degrade into a less
4831 * optimal tree layout.
4833 * last_eb_bh should be the rightmost leaf block for any extent
4834 * btree. Since a split may grow the tree or a merge might shrink it,
4835 * the caller cannot trust the contents of that buffer after this call.
4837 * This code is optimized for readability - several passes might be
4838 * made over certain portions of the tree. All of those blocks will
4839 * have been brought into cache (and pinned via the journal), so the
4840 * extra overhead is not expressed in terms of disk reads.
4842 static int __ocfs2_mark_extent_written(struct inode
*inode
,
4843 struct ocfs2_extent_tree
*et
,
4845 struct ocfs2_path
*path
,
4847 struct ocfs2_extent_rec
*split_rec
,
4848 struct ocfs2_alloc_context
*meta_ac
,
4849 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
4852 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
4853 struct buffer_head
*last_eb_bh
= NULL
;
4854 struct ocfs2_extent_rec
*rec
= &el
->l_recs
[split_index
];
4855 struct ocfs2_merge_ctxt ctxt
;
4856 struct ocfs2_extent_list
*rightmost_el
;
4858 if (!(rec
->e_flags
& OCFS2_EXT_UNWRITTEN
)) {
4864 if (le32_to_cpu(rec
->e_cpos
) > le32_to_cpu(split_rec
->e_cpos
) ||
4865 ((le32_to_cpu(rec
->e_cpos
) + le16_to_cpu(rec
->e_leaf_clusters
)) <
4866 (le32_to_cpu(split_rec
->e_cpos
) + le16_to_cpu(split_rec
->e_leaf_clusters
)))) {
4872 ctxt
.c_contig_type
= ocfs2_figure_merge_contig_type(inode
, path
, el
,
4877 * The core merge / split code wants to know how much room is
4878 * left in this inodes allocation tree, so we pass the
4879 * rightmost extent list.
4881 if (path
->p_tree_depth
) {
4882 struct ocfs2_extent_block
*eb
;
4884 ret
= ocfs2_read_extent_block(inode
,
4885 ocfs2_et_get_last_eb_blk(et
),
4892 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
4893 rightmost_el
= &eb
->h_list
;
4895 rightmost_el
= path_root_el(path
);
4897 if (rec
->e_cpos
== split_rec
->e_cpos
&&
4898 rec
->e_leaf_clusters
== split_rec
->e_leaf_clusters
)
4899 ctxt
.c_split_covers_rec
= 1;
4901 ctxt
.c_split_covers_rec
= 0;
4903 ctxt
.c_has_empty_extent
= ocfs2_is_empty_extent(&el
->l_recs
[0]);
4905 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4906 split_index
, ctxt
.c_contig_type
, ctxt
.c_has_empty_extent
,
4907 ctxt
.c_split_covers_rec
);
4909 if (ctxt
.c_contig_type
== CONTIG_NONE
) {
4910 if (ctxt
.c_split_covers_rec
)
4911 ret
= ocfs2_replace_extent_rec(inode
, handle
,
4913 split_index
, split_rec
);
4915 ret
= ocfs2_split_and_insert(inode
, handle
, path
, et
,
4916 &last_eb_bh
, split_index
,
4917 split_rec
, meta_ac
);
4921 ret
= ocfs2_try_to_merge_extent(inode
, handle
, path
,
4922 split_index
, split_rec
,
4923 dealloc
, &ctxt
, et
);
4934 * Mark the already-existing extent at cpos as written for len clusters.
4936 * If the existing extent is larger than the request, initiate a
4937 * split. An attempt will be made at merging with adjacent extents.
4939 * The caller is responsible for passing down meta_ac if we'll need it.
4941 int ocfs2_mark_extent_written(struct inode
*inode
,
4942 struct ocfs2_extent_tree
*et
,
4943 handle_t
*handle
, u32 cpos
, u32 len
, u32 phys
,
4944 struct ocfs2_alloc_context
*meta_ac
,
4945 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
4948 u64 start_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys
);
4949 struct ocfs2_extent_rec split_rec
;
4950 struct ocfs2_path
*left_path
= NULL
;
4951 struct ocfs2_extent_list
*el
;
4953 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4954 inode
->i_ino
, cpos
, len
, phys
, (unsigned long long)start_blkno
);
4956 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode
->i_sb
))) {
4957 ocfs2_error(inode
->i_sb
, "Inode %llu has unwritten extents "
4958 "that are being written to, but the feature bit "
4959 "is not set in the super block.",
4960 (unsigned long long)OCFS2_I(inode
)->ip_blkno
);
4966 * XXX: This should be fixed up so that we just re-insert the
4967 * next extent records.
4969 * XXX: This is a hack on the extent tree, maybe it should be
4972 if (et
->et_ops
== &ocfs2_dinode_et_ops
)
4973 ocfs2_extent_map_trunc(inode
, 0);
4975 left_path
= ocfs2_new_path_from_et(et
);
4982 ret
= ocfs2_find_path(inode
, left_path
, cpos
);
4987 el
= path_leaf_el(left_path
);
4989 index
= ocfs2_search_extent_list(el
, cpos
);
4990 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
4991 ocfs2_error(inode
->i_sb
,
4992 "Inode %llu has an extent at cpos %u which can no "
4993 "longer be found.\n",
4994 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
4999 memset(&split_rec
, 0, sizeof(struct ocfs2_extent_rec
));
5000 split_rec
.e_cpos
= cpu_to_le32(cpos
);
5001 split_rec
.e_leaf_clusters
= cpu_to_le16(len
);
5002 split_rec
.e_blkno
= cpu_to_le64(start_blkno
);
5003 split_rec
.e_flags
= path_leaf_el(left_path
)->l_recs
[index
].e_flags
;
5004 split_rec
.e_flags
&= ~OCFS2_EXT_UNWRITTEN
;
5006 ret
= __ocfs2_mark_extent_written(inode
, et
, handle
, left_path
,
5007 index
, &split_rec
, meta_ac
,
5013 ocfs2_free_path(left_path
);
5017 static int ocfs2_split_tree(struct inode
*inode
, struct ocfs2_extent_tree
*et
,
5018 handle_t
*handle
, struct ocfs2_path
*path
,
5019 int index
, u32 new_range
,
5020 struct ocfs2_alloc_context
*meta_ac
)
5022 int ret
, depth
, credits
= handle
->h_buffer_credits
;
5023 struct buffer_head
*last_eb_bh
= NULL
;
5024 struct ocfs2_extent_block
*eb
;
5025 struct ocfs2_extent_list
*rightmost_el
, *el
;
5026 struct ocfs2_extent_rec split_rec
;
5027 struct ocfs2_extent_rec
*rec
;
5028 struct ocfs2_insert_type insert
;
5031 * Setup the record to split before we grow the tree.
5033 el
= path_leaf_el(path
);
5034 rec
= &el
->l_recs
[index
];
5035 ocfs2_make_right_split_rec(inode
->i_sb
, &split_rec
, new_range
, rec
);
5037 depth
= path
->p_tree_depth
;
5039 ret
= ocfs2_read_extent_block(inode
,
5040 ocfs2_et_get_last_eb_blk(et
),
5047 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
5048 rightmost_el
= &eb
->h_list
;
5050 rightmost_el
= path_leaf_el(path
);
5052 credits
+= path
->p_tree_depth
+
5053 ocfs2_extend_meta_needed(et
->et_root_el
);
5054 ret
= ocfs2_extend_trans(handle
, credits
);
5060 if (le16_to_cpu(rightmost_el
->l_next_free_rec
) ==
5061 le16_to_cpu(rightmost_el
->l_count
)) {
5062 ret
= ocfs2_grow_tree(inode
, handle
, et
, &depth
, &last_eb_bh
,
5070 memset(&insert
, 0, sizeof(struct ocfs2_insert_type
));
5071 insert
.ins_appending
= APPEND_NONE
;
5072 insert
.ins_contig
= CONTIG_NONE
;
5073 insert
.ins_split
= SPLIT_RIGHT
;
5074 insert
.ins_tree_depth
= depth
;
5076 ret
= ocfs2_do_insert_extent(inode
, handle
, et
, &split_rec
, &insert
);
5085 static int ocfs2_truncate_rec(struct inode
*inode
, handle_t
*handle
,
5086 struct ocfs2_path
*path
, int index
,
5087 struct ocfs2_cached_dealloc_ctxt
*dealloc
,
5089 struct ocfs2_extent_tree
*et
)
5092 u32 left_cpos
, rec_range
, trunc_range
;
5093 int wants_rotate
= 0, is_rightmost_tree_rec
= 0;
5094 struct super_block
*sb
= inode
->i_sb
;
5095 struct ocfs2_path
*left_path
= NULL
;
5096 struct ocfs2_extent_list
*el
= path_leaf_el(path
);
5097 struct ocfs2_extent_rec
*rec
;
5098 struct ocfs2_extent_block
*eb
;
5100 if (ocfs2_is_empty_extent(&el
->l_recs
[0]) && index
> 0) {
5101 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
, et
);
5110 if (index
== (le16_to_cpu(el
->l_next_free_rec
) - 1) &&
5111 path
->p_tree_depth
) {
5113 * Check whether this is the rightmost tree record. If
5114 * we remove all of this record or part of its right
5115 * edge then an update of the record lengths above it
5118 eb
= (struct ocfs2_extent_block
*)path_leaf_bh(path
)->b_data
;
5119 if (eb
->h_next_leaf_blk
== 0)
5120 is_rightmost_tree_rec
= 1;
5123 rec
= &el
->l_recs
[index
];
5124 if (index
== 0 && path
->p_tree_depth
&&
5125 le32_to_cpu(rec
->e_cpos
) == cpos
) {
5127 * Changing the leftmost offset (via partial or whole
5128 * record truncate) of an interior (or rightmost) path
5129 * means we have to update the subtree that is formed
5130 * by this leaf and the one to it's left.
5132 * There are two cases we can skip:
5133 * 1) Path is the leftmost one in our inode tree.
5134 * 2) The leaf is rightmost and will be empty after
5135 * we remove the extent record - the rotate code
5136 * knows how to update the newly formed edge.
5139 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
,
5146 if (left_cpos
&& le16_to_cpu(el
->l_next_free_rec
) > 1) {
5147 left_path
= ocfs2_new_path_from_path(path
);
5154 ret
= ocfs2_find_path(inode
, left_path
, left_cpos
);
5162 ret
= ocfs2_extend_rotate_transaction(handle
, 0,
5163 handle
->h_buffer_credits
,
5170 ret
= ocfs2_journal_access_path(inode
, handle
, path
);
5176 ret
= ocfs2_journal_access_path(inode
, handle
, left_path
);
5182 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5183 trunc_range
= cpos
+ len
;
5185 if (le32_to_cpu(rec
->e_cpos
) == cpos
&& rec_range
== trunc_range
) {
5188 memset(rec
, 0, sizeof(*rec
));
5189 ocfs2_cleanup_merge(el
, index
);
5192 next_free
= le16_to_cpu(el
->l_next_free_rec
);
5193 if (is_rightmost_tree_rec
&& next_free
> 1) {
5195 * We skip the edge update if this path will
5196 * be deleted by the rotate code.
5198 rec
= &el
->l_recs
[next_free
- 1];
5199 ocfs2_adjust_rightmost_records(inode
, handle
, path
,
5202 } else if (le32_to_cpu(rec
->e_cpos
) == cpos
) {
5203 /* Remove leftmost portion of the record. */
5204 le32_add_cpu(&rec
->e_cpos
, len
);
5205 le64_add_cpu(&rec
->e_blkno
, ocfs2_clusters_to_blocks(sb
, len
));
5206 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5207 } else if (rec_range
== trunc_range
) {
5208 /* Remove rightmost portion of the record */
5209 le16_add_cpu(&rec
->e_leaf_clusters
, -len
);
5210 if (is_rightmost_tree_rec
)
5211 ocfs2_adjust_rightmost_records(inode
, handle
, path
, rec
);
5213 /* Caller should have trapped this. */
5214 mlog(ML_ERROR
, "Inode %llu: Invalid record truncate: (%u, %u) "
5215 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5216 le32_to_cpu(rec
->e_cpos
),
5217 le16_to_cpu(rec
->e_leaf_clusters
), cpos
, len
);
5224 subtree_index
= ocfs2_find_subtree_root(inode
, left_path
, path
);
5225 ocfs2_complete_edge_insert(inode
, handle
, left_path
, path
,
5229 ocfs2_journal_dirty(handle
, path_leaf_bh(path
));
5231 ret
= ocfs2_rotate_tree_left(inode
, handle
, path
, dealloc
, et
);
5238 ocfs2_free_path(left_path
);
5242 int ocfs2_remove_extent(struct inode
*inode
,
5243 struct ocfs2_extent_tree
*et
,
5244 u32 cpos
, u32 len
, handle_t
*handle
,
5245 struct ocfs2_alloc_context
*meta_ac
,
5246 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5249 u32 rec_range
, trunc_range
;
5250 struct ocfs2_extent_rec
*rec
;
5251 struct ocfs2_extent_list
*el
;
5252 struct ocfs2_path
*path
= NULL
;
5254 ocfs2_extent_map_trunc(inode
, 0);
5256 path
= ocfs2_new_path_from_et(et
);
5263 ret
= ocfs2_find_path(inode
, path
, cpos
);
5269 el
= path_leaf_el(path
);
5270 index
= ocfs2_search_extent_list(el
, cpos
);
5271 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5272 ocfs2_error(inode
->i_sb
,
5273 "Inode %llu has an extent at cpos %u which can no "
5274 "longer be found.\n",
5275 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
);
5281 * We have 3 cases of extent removal:
5282 * 1) Range covers the entire extent rec
5283 * 2) Range begins or ends on one edge of the extent rec
5284 * 3) Range is in the middle of the extent rec (no shared edges)
5286 * For case 1 we remove the extent rec and left rotate to
5289 * For case 2 we just shrink the existing extent rec, with a
5290 * tree update if the shrinking edge is also the edge of an
5293 * For case 3 we do a right split to turn the extent rec into
5294 * something case 2 can handle.
5296 rec
= &el
->l_recs
[index
];
5297 rec_range
= le32_to_cpu(rec
->e_cpos
) + ocfs2_rec_clusters(el
, rec
);
5298 trunc_range
= cpos
+ len
;
5300 BUG_ON(cpos
< le32_to_cpu(rec
->e_cpos
) || trunc_range
> rec_range
);
5302 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5303 "(cpos %u, len %u)\n",
5304 (unsigned long long)OCFS2_I(inode
)->ip_blkno
, cpos
, len
, index
,
5305 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
));
5307 if (le32_to_cpu(rec
->e_cpos
) == cpos
|| rec_range
== trunc_range
) {
5308 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
5315 ret
= ocfs2_split_tree(inode
, et
, handle
, path
, index
,
5316 trunc_range
, meta_ac
);
5323 * The split could have manipulated the tree enough to
5324 * move the record location, so we have to look for it again.
5326 ocfs2_reinit_path(path
, 1);
5328 ret
= ocfs2_find_path(inode
, path
, cpos
);
5334 el
= path_leaf_el(path
);
5335 index
= ocfs2_search_extent_list(el
, cpos
);
5336 if (index
== -1 || index
>= le16_to_cpu(el
->l_next_free_rec
)) {
5337 ocfs2_error(inode
->i_sb
,
5338 "Inode %llu: split at cpos %u lost record.",
5339 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5346 * Double check our values here. If anything is fishy,
5347 * it's easier to catch it at the top level.
5349 rec
= &el
->l_recs
[index
];
5350 rec_range
= le32_to_cpu(rec
->e_cpos
) +
5351 ocfs2_rec_clusters(el
, rec
);
5352 if (rec_range
!= trunc_range
) {
5353 ocfs2_error(inode
->i_sb
,
5354 "Inode %llu: error after split at cpos %u"
5355 "trunc len %u, existing record is (%u,%u)",
5356 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
5357 cpos
, len
, le32_to_cpu(rec
->e_cpos
),
5358 ocfs2_rec_clusters(el
, rec
));
5363 ret
= ocfs2_truncate_rec(inode
, handle
, path
, index
, dealloc
,
5372 ocfs2_free_path(path
);
5376 int ocfs2_remove_btree_range(struct inode
*inode
,
5377 struct ocfs2_extent_tree
*et
,
5378 u32 cpos
, u32 phys_cpos
, u32 len
,
5379 struct ocfs2_cached_dealloc_ctxt
*dealloc
)
5382 u64 phys_blkno
= ocfs2_clusters_to_blocks(inode
->i_sb
, phys_cpos
);
5383 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
5384 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5386 struct ocfs2_alloc_context
*meta_ac
= NULL
;
5388 ret
= ocfs2_lock_allocators(inode
, et
, 0, 1, NULL
, &meta_ac
);
5394 mutex_lock(&tl_inode
->i_mutex
);
5396 if (ocfs2_truncate_log_needs_flush(osb
)) {
5397 ret
= __ocfs2_flush_truncate_log(osb
);
5404 handle
= ocfs2_start_trans(osb
, ocfs2_remove_extent_credits(osb
->sb
));
5405 if (IS_ERR(handle
)) {
5406 ret
= PTR_ERR(handle
);
5411 ret
= ocfs2_et_root_journal_access(handle
, inode
, et
,
5412 OCFS2_JOURNAL_ACCESS_WRITE
);
5418 vfs_dq_free_space_nodirty(inode
,
5419 ocfs2_clusters_to_bytes(inode
->i_sb
, len
));
5421 ret
= ocfs2_remove_extent(inode
, et
, cpos
, len
, handle
, meta_ac
,
5428 ocfs2_et_update_clusters(inode
, et
, -len
);
5430 ret
= ocfs2_journal_dirty(handle
, et
->et_root_bh
);
5436 ret
= ocfs2_truncate_log_append(osb
, handle
, phys_blkno
, len
);
5441 ocfs2_commit_trans(osb
, handle
);
5443 mutex_unlock(&tl_inode
->i_mutex
);
5446 ocfs2_free_alloc_context(meta_ac
);
5451 int ocfs2_truncate_log_needs_flush(struct ocfs2_super
*osb
)
5453 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5454 struct ocfs2_dinode
*di
;
5455 struct ocfs2_truncate_log
*tl
;
5457 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5458 tl
= &di
->id2
.i_dealloc
;
5460 mlog_bug_on_msg(le16_to_cpu(tl
->tl_used
) > le16_to_cpu(tl
->tl_count
),
5461 "slot %d, invalid truncate log parameters: used = "
5462 "%u, count = %u\n", osb
->slot_num
,
5463 le16_to_cpu(tl
->tl_used
), le16_to_cpu(tl
->tl_count
));
5464 return le16_to_cpu(tl
->tl_used
) == le16_to_cpu(tl
->tl_count
);
5467 static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log
*tl
,
5468 unsigned int new_start
)
5470 unsigned int tail_index
;
5471 unsigned int current_tail
;
5473 /* No records, nothing to coalesce */
5474 if (!le16_to_cpu(tl
->tl_used
))
5477 tail_index
= le16_to_cpu(tl
->tl_used
) - 1;
5478 current_tail
= le32_to_cpu(tl
->tl_recs
[tail_index
].t_start
);
5479 current_tail
+= le32_to_cpu(tl
->tl_recs
[tail_index
].t_clusters
);
5481 return current_tail
== new_start
;
5484 int ocfs2_truncate_log_append(struct ocfs2_super
*osb
,
5487 unsigned int num_clusters
)
5490 unsigned int start_cluster
, tl_count
;
5491 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5492 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5493 struct ocfs2_dinode
*di
;
5494 struct ocfs2_truncate_log
*tl
;
5496 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5497 (unsigned long long)start_blk
, num_clusters
);
5499 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5501 start_cluster
= ocfs2_blocks_to_clusters(osb
->sb
, start_blk
);
5503 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5505 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5506 * by the underlying call to ocfs2_read_inode_block(), so any
5507 * corruption is a code bug */
5508 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5510 tl
= &di
->id2
.i_dealloc
;
5511 tl_count
= le16_to_cpu(tl
->tl_count
);
5512 mlog_bug_on_msg(tl_count
> ocfs2_truncate_recs_per_inode(osb
->sb
) ||
5514 "Truncate record count on #%llu invalid "
5515 "wanted %u, actual %u\n",
5516 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
,
5517 ocfs2_truncate_recs_per_inode(osb
->sb
),
5518 le16_to_cpu(tl
->tl_count
));
5520 /* Caller should have known to flush before calling us. */
5521 index
= le16_to_cpu(tl
->tl_used
);
5522 if (index
>= tl_count
) {
5528 status
= ocfs2_journal_access_di(handle
, tl_inode
, tl_bh
,
5529 OCFS2_JOURNAL_ACCESS_WRITE
);
5535 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
5536 "%llu (index = %d)\n", num_clusters
, start_cluster
,
5537 (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
, index
);
5539 if (ocfs2_truncate_log_can_coalesce(tl
, start_cluster
)) {
5541 * Move index back to the record we are coalescing with.
5542 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5546 num_clusters
+= le32_to_cpu(tl
->tl_recs
[index
].t_clusters
);
5547 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5548 index
, le32_to_cpu(tl
->tl_recs
[index
].t_start
),
5551 tl
->tl_recs
[index
].t_start
= cpu_to_le32(start_cluster
);
5552 tl
->tl_used
= cpu_to_le16(index
+ 1);
5554 tl
->tl_recs
[index
].t_clusters
= cpu_to_le32(num_clusters
);
5556 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5567 static int ocfs2_replay_truncate_records(struct ocfs2_super
*osb
,
5569 struct inode
*data_alloc_inode
,
5570 struct buffer_head
*data_alloc_bh
)
5574 unsigned int num_clusters
;
5576 struct ocfs2_truncate_rec rec
;
5577 struct ocfs2_dinode
*di
;
5578 struct ocfs2_truncate_log
*tl
;
5579 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5580 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5584 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5585 tl
= &di
->id2
.i_dealloc
;
5586 i
= le16_to_cpu(tl
->tl_used
) - 1;
5588 /* Caller has given us at least enough credits to
5589 * update the truncate log dinode */
5590 status
= ocfs2_journal_access_di(handle
, tl_inode
, tl_bh
,
5591 OCFS2_JOURNAL_ACCESS_WRITE
);
5597 tl
->tl_used
= cpu_to_le16(i
);
5599 status
= ocfs2_journal_dirty(handle
, tl_bh
);
5605 /* TODO: Perhaps we can calculate the bulk of the
5606 * credits up front rather than extending like
5608 status
= ocfs2_extend_trans(handle
,
5609 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC
);
5615 rec
= tl
->tl_recs
[i
];
5616 start_blk
= ocfs2_clusters_to_blocks(data_alloc_inode
->i_sb
,
5617 le32_to_cpu(rec
.t_start
));
5618 num_clusters
= le32_to_cpu(rec
.t_clusters
);
5620 /* if start_blk is not set, we ignore the record as
5623 mlog(0, "free record %d, start = %u, clusters = %u\n",
5624 i
, le32_to_cpu(rec
.t_start
), num_clusters
);
5626 status
= ocfs2_free_clusters(handle
, data_alloc_inode
,
5627 data_alloc_bh
, start_blk
,
5642 /* Expects you to already be holding tl_inode->i_mutex */
5643 int __ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5646 unsigned int num_to_flush
;
5648 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5649 struct inode
*data_alloc_inode
= NULL
;
5650 struct buffer_head
*tl_bh
= osb
->osb_tl_bh
;
5651 struct buffer_head
*data_alloc_bh
= NULL
;
5652 struct ocfs2_dinode
*di
;
5653 struct ocfs2_truncate_log
*tl
;
5657 BUG_ON(mutex_trylock(&tl_inode
->i_mutex
));
5659 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5661 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5662 * by the underlying call to ocfs2_read_inode_block(), so any
5663 * corruption is a code bug */
5664 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5666 tl
= &di
->id2
.i_dealloc
;
5667 num_to_flush
= le16_to_cpu(tl
->tl_used
);
5668 mlog(0, "Flush %u records from truncate log #%llu\n",
5669 num_to_flush
, (unsigned long long)OCFS2_I(tl_inode
)->ip_blkno
);
5670 if (!num_to_flush
) {
5675 data_alloc_inode
= ocfs2_get_system_file_inode(osb
,
5676 GLOBAL_BITMAP_SYSTEM_INODE
,
5677 OCFS2_INVALID_SLOT
);
5678 if (!data_alloc_inode
) {
5680 mlog(ML_ERROR
, "Could not get bitmap inode!\n");
5684 mutex_lock(&data_alloc_inode
->i_mutex
);
5686 status
= ocfs2_inode_lock(data_alloc_inode
, &data_alloc_bh
, 1);
5692 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5693 if (IS_ERR(handle
)) {
5694 status
= PTR_ERR(handle
);
5699 status
= ocfs2_replay_truncate_records(osb
, handle
, data_alloc_inode
,
5704 ocfs2_commit_trans(osb
, handle
);
5707 brelse(data_alloc_bh
);
5708 ocfs2_inode_unlock(data_alloc_inode
, 1);
5711 mutex_unlock(&data_alloc_inode
->i_mutex
);
5712 iput(data_alloc_inode
);
5719 int ocfs2_flush_truncate_log(struct ocfs2_super
*osb
)
5722 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5724 mutex_lock(&tl_inode
->i_mutex
);
5725 status
= __ocfs2_flush_truncate_log(osb
);
5726 mutex_unlock(&tl_inode
->i_mutex
);
5731 static void ocfs2_truncate_log_worker(struct work_struct
*work
)
5734 struct ocfs2_super
*osb
=
5735 container_of(work
, struct ocfs2_super
,
5736 osb_truncate_log_wq
.work
);
5740 status
= ocfs2_flush_truncate_log(osb
);
5744 ocfs2_init_inode_steal_slot(osb
);
5749 #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5750 void ocfs2_schedule_truncate_log_flush(struct ocfs2_super
*osb
,
5753 if (osb
->osb_tl_inode
) {
5754 /* We want to push off log flushes while truncates are
5757 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5759 queue_delayed_work(ocfs2_wq
, &osb
->osb_truncate_log_wq
,
5760 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL
);
5764 static int ocfs2_get_truncate_log_info(struct ocfs2_super
*osb
,
5766 struct inode
**tl_inode
,
5767 struct buffer_head
**tl_bh
)
5770 struct inode
*inode
= NULL
;
5771 struct buffer_head
*bh
= NULL
;
5773 inode
= ocfs2_get_system_file_inode(osb
,
5774 TRUNCATE_LOG_SYSTEM_INODE
,
5778 mlog(ML_ERROR
, "Could not get load truncate log inode!\n");
5782 status
= ocfs2_read_inode_block(inode
, &bh
);
5796 /* called during the 1st stage of node recovery. we stamp a clean
5797 * truncate log and pass back a copy for processing later. if the
5798 * truncate log does not require processing, a *tl_copy is set to
5800 int ocfs2_begin_truncate_log_recovery(struct ocfs2_super
*osb
,
5802 struct ocfs2_dinode
**tl_copy
)
5805 struct inode
*tl_inode
= NULL
;
5806 struct buffer_head
*tl_bh
= NULL
;
5807 struct ocfs2_dinode
*di
;
5808 struct ocfs2_truncate_log
*tl
;
5812 mlog(0, "recover truncate log from slot %d\n", slot_num
);
5814 status
= ocfs2_get_truncate_log_info(osb
, slot_num
, &tl_inode
, &tl_bh
);
5820 di
= (struct ocfs2_dinode
*) tl_bh
->b_data
;
5822 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5823 * validated by the underlying call to ocfs2_read_inode_block(),
5824 * so any corruption is a code bug */
5825 BUG_ON(!OCFS2_IS_VALID_DINODE(di
));
5827 tl
= &di
->id2
.i_dealloc
;
5828 if (le16_to_cpu(tl
->tl_used
)) {
5829 mlog(0, "We'll have %u logs to recover\n",
5830 le16_to_cpu(tl
->tl_used
));
5832 *tl_copy
= kmalloc(tl_bh
->b_size
, GFP_KERNEL
);
5839 /* Assuming the write-out below goes well, this copy
5840 * will be passed back to recovery for processing. */
5841 memcpy(*tl_copy
, tl_bh
->b_data
, tl_bh
->b_size
);
5843 /* All we need to do to clear the truncate log is set
5847 ocfs2_compute_meta_ecc(osb
->sb
, tl_bh
->b_data
, &di
->i_check
);
5848 status
= ocfs2_write_block(osb
, tl_bh
, tl_inode
);
5860 if (status
< 0 && (*tl_copy
)) {
5869 int ocfs2_complete_truncate_log_recovery(struct ocfs2_super
*osb
,
5870 struct ocfs2_dinode
*tl_copy
)
5874 unsigned int clusters
, num_recs
, start_cluster
;
5877 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5878 struct ocfs2_truncate_log
*tl
;
5882 if (OCFS2_I(tl_inode
)->ip_blkno
== le64_to_cpu(tl_copy
->i_blkno
)) {
5883 mlog(ML_ERROR
, "Asked to recover my own truncate log!\n");
5887 tl
= &tl_copy
->id2
.i_dealloc
;
5888 num_recs
= le16_to_cpu(tl
->tl_used
);
5889 mlog(0, "cleanup %u records from %llu\n", num_recs
,
5890 (unsigned long long)le64_to_cpu(tl_copy
->i_blkno
));
5892 mutex_lock(&tl_inode
->i_mutex
);
5893 for(i
= 0; i
< num_recs
; i
++) {
5894 if (ocfs2_truncate_log_needs_flush(osb
)) {
5895 status
= __ocfs2_flush_truncate_log(osb
);
5902 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
5903 if (IS_ERR(handle
)) {
5904 status
= PTR_ERR(handle
);
5909 clusters
= le32_to_cpu(tl
->tl_recs
[i
].t_clusters
);
5910 start_cluster
= le32_to_cpu(tl
->tl_recs
[i
].t_start
);
5911 start_blk
= ocfs2_clusters_to_blocks(osb
->sb
, start_cluster
);
5913 status
= ocfs2_truncate_log_append(osb
, handle
,
5914 start_blk
, clusters
);
5915 ocfs2_commit_trans(osb
, handle
);
5923 mutex_unlock(&tl_inode
->i_mutex
);
5929 void ocfs2_truncate_log_shutdown(struct ocfs2_super
*osb
)
5932 struct inode
*tl_inode
= osb
->osb_tl_inode
;
5937 cancel_delayed_work(&osb
->osb_truncate_log_wq
);
5938 flush_workqueue(ocfs2_wq
);
5940 status
= ocfs2_flush_truncate_log(osb
);
5944 brelse(osb
->osb_tl_bh
);
5945 iput(osb
->osb_tl_inode
);
5951 int ocfs2_truncate_log_init(struct ocfs2_super
*osb
)
5954 struct inode
*tl_inode
= NULL
;
5955 struct buffer_head
*tl_bh
= NULL
;
5959 status
= ocfs2_get_truncate_log_info(osb
,
5966 /* ocfs2_truncate_log_shutdown keys on the existence of
5967 * osb->osb_tl_inode so we don't set any of the osb variables
5968 * until we're sure all is well. */
5969 INIT_DELAYED_WORK(&osb
->osb_truncate_log_wq
,
5970 ocfs2_truncate_log_worker
);
5971 osb
->osb_tl_bh
= tl_bh
;
5972 osb
->osb_tl_inode
= tl_inode
;
5979 * Delayed de-allocation of suballocator blocks.
5981 * Some sets of block de-allocations might involve multiple suballocator inodes.
5983 * The locking for this can get extremely complicated, especially when
5984 * the suballocator inodes to delete from aren't known until deep
5985 * within an unrelated codepath.
5987 * ocfs2_extent_block structures are a good example of this - an inode
5988 * btree could have been grown by any number of nodes each allocating
5989 * out of their own suballoc inode.
5991 * These structures allow the delay of block de-allocation until a
5992 * later time, when locking of multiple cluster inodes won't cause
5997 * Describe a single bit freed from a suballocator. For the block
5998 * suballocators, it represents one block. For the global cluster
5999 * allocator, it represents some clusters and free_bit indicates
6002 struct ocfs2_cached_block_free
{
6003 struct ocfs2_cached_block_free
*free_next
;
6005 unsigned int free_bit
;
6008 struct ocfs2_per_slot_free_list
{
6009 struct ocfs2_per_slot_free_list
*f_next_suballocator
;
6012 struct ocfs2_cached_block_free
*f_first
;
6015 static int ocfs2_free_cached_blocks(struct ocfs2_super
*osb
,
6018 struct ocfs2_cached_block_free
*head
)
6023 struct inode
*inode
;
6024 struct buffer_head
*di_bh
= NULL
;
6025 struct ocfs2_cached_block_free
*tmp
;
6027 inode
= ocfs2_get_system_file_inode(osb
, sysfile_type
, slot
);
6034 mutex_lock(&inode
->i_mutex
);
6036 ret
= ocfs2_inode_lock(inode
, &di_bh
, 1);
6042 handle
= ocfs2_start_trans(osb
, OCFS2_SUBALLOC_FREE
);
6043 if (IS_ERR(handle
)) {
6044 ret
= PTR_ERR(handle
);
6050 bg_blkno
= ocfs2_which_suballoc_group(head
->free_blk
,
6052 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6053 head
->free_bit
, (unsigned long long)head
->free_blk
);
6055 ret
= ocfs2_free_suballoc_bits(handle
, inode
, di_bh
,
6056 head
->free_bit
, bg_blkno
, 1);
6062 ret
= ocfs2_extend_trans(handle
, OCFS2_SUBALLOC_FREE
);
6069 head
= head
->free_next
;
6074 ocfs2_commit_trans(osb
, handle
);
6077 ocfs2_inode_unlock(inode
, 1);
6080 mutex_unlock(&inode
->i_mutex
);
6084 /* Premature exit may have left some dangling items. */
6086 head
= head
->free_next
;
6093 int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6094 u64 blkno
, unsigned int bit
)
6097 struct ocfs2_cached_block_free
*item
;
6099 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6106 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6107 bit
, (unsigned long long)blkno
);
6109 item
->free_blk
= blkno
;
6110 item
->free_bit
= bit
;
6111 item
->free_next
= ctxt
->c_global_allocator
;
6113 ctxt
->c_global_allocator
= item
;
6117 static int ocfs2_free_cached_clusters(struct ocfs2_super
*osb
,
6118 struct ocfs2_cached_block_free
*head
)
6120 struct ocfs2_cached_block_free
*tmp
;
6121 struct inode
*tl_inode
= osb
->osb_tl_inode
;
6125 mutex_lock(&tl_inode
->i_mutex
);
6128 if (ocfs2_truncate_log_needs_flush(osb
)) {
6129 ret
= __ocfs2_flush_truncate_log(osb
);
6136 handle
= ocfs2_start_trans(osb
, OCFS2_TRUNCATE_LOG_UPDATE
);
6137 if (IS_ERR(handle
)) {
6138 ret
= PTR_ERR(handle
);
6143 ret
= ocfs2_truncate_log_append(osb
, handle
, head
->free_blk
,
6146 ocfs2_commit_trans(osb
, handle
);
6148 head
= head
->free_next
;
6157 mutex_unlock(&tl_inode
->i_mutex
);
6160 /* Premature exit may have left some dangling items. */
6162 head
= head
->free_next
;
6169 int ocfs2_run_deallocs(struct ocfs2_super
*osb
,
6170 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
6173 struct ocfs2_per_slot_free_list
*fl
;
6178 while (ctxt
->c_first_suballocator
) {
6179 fl
= ctxt
->c_first_suballocator
;
6182 mlog(0, "Free items: (type %u, slot %d)\n",
6183 fl
->f_inode_type
, fl
->f_slot
);
6184 ret2
= ocfs2_free_cached_blocks(osb
,
6194 ctxt
->c_first_suballocator
= fl
->f_next_suballocator
;
6198 if (ctxt
->c_global_allocator
) {
6199 ret2
= ocfs2_free_cached_clusters(osb
,
6200 ctxt
->c_global_allocator
);
6206 ctxt
->c_global_allocator
= NULL
;
6212 static struct ocfs2_per_slot_free_list
*
6213 ocfs2_find_per_slot_free_list(int type
,
6215 struct ocfs2_cached_dealloc_ctxt
*ctxt
)
6217 struct ocfs2_per_slot_free_list
*fl
= ctxt
->c_first_suballocator
;
6220 if (fl
->f_inode_type
== type
&& fl
->f_slot
== slot
)
6223 fl
= fl
->f_next_suballocator
;
6226 fl
= kmalloc(sizeof(*fl
), GFP_NOFS
);
6228 fl
->f_inode_type
= type
;
6231 fl
->f_next_suballocator
= ctxt
->c_first_suballocator
;
6233 ctxt
->c_first_suballocator
= fl
;
6238 static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6239 int type
, int slot
, u64 blkno
,
6243 struct ocfs2_per_slot_free_list
*fl
;
6244 struct ocfs2_cached_block_free
*item
;
6246 fl
= ocfs2_find_per_slot_free_list(type
, slot
, ctxt
);
6253 item
= kmalloc(sizeof(*item
), GFP_NOFS
);
6260 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6261 type
, slot
, bit
, (unsigned long long)blkno
);
6263 item
->free_blk
= blkno
;
6264 item
->free_bit
= bit
;
6265 item
->free_next
= fl
->f_first
;
6274 static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt
*ctxt
,
6275 struct ocfs2_extent_block
*eb
)
6277 return ocfs2_cache_block_dealloc(ctxt
, EXTENT_ALLOC_SYSTEM_INODE
,
6278 le16_to_cpu(eb
->h_suballoc_slot
),
6279 le64_to_cpu(eb
->h_blkno
),
6280 le16_to_cpu(eb
->h_suballoc_bit
));
6283 /* This function will figure out whether the currently last extent
6284 * block will be deleted, and if it will, what the new last extent
6285 * block will be so we can update his h_next_leaf_blk field, as well
6286 * as the dinodes i_last_eb_blk */
6287 static int ocfs2_find_new_last_ext_blk(struct inode
*inode
,
6288 unsigned int clusters_to_del
,
6289 struct ocfs2_path
*path
,
6290 struct buffer_head
**new_last_eb
)
6292 int next_free
, ret
= 0;
6294 struct ocfs2_extent_rec
*rec
;
6295 struct ocfs2_extent_block
*eb
;
6296 struct ocfs2_extent_list
*el
;
6297 struct buffer_head
*bh
= NULL
;
6299 *new_last_eb
= NULL
;
6301 /* we have no tree, so of course, no last_eb. */
6302 if (!path
->p_tree_depth
)
6305 /* trunc to zero special case - this makes tree_depth = 0
6306 * regardless of what it is. */
6307 if (OCFS2_I(inode
)->ip_clusters
== clusters_to_del
)
6310 el
= path_leaf_el(path
);
6311 BUG_ON(!el
->l_next_free_rec
);
6314 * Make sure that this extent list will actually be empty
6315 * after we clear away the data. We can shortcut out if
6316 * there's more than one non-empty extent in the
6317 * list. Otherwise, a check of the remaining extent is
6320 next_free
= le16_to_cpu(el
->l_next_free_rec
);
6322 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6326 /* We may have a valid extent in index 1, check it. */
6328 rec
= &el
->l_recs
[1];
6331 * Fall through - no more nonempty extents, so we want
6332 * to delete this leaf.
6338 rec
= &el
->l_recs
[0];
6343 * Check it we'll only be trimming off the end of this
6346 if (le16_to_cpu(rec
->e_leaf_clusters
) > clusters_to_del
)
6350 ret
= ocfs2_find_cpos_for_left_leaf(inode
->i_sb
, path
, &cpos
);
6356 ret
= ocfs2_find_leaf(inode
, path_root_el(path
), cpos
, &bh
);
6362 eb
= (struct ocfs2_extent_block
*) bh
->b_data
;
6365 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6366 * Any corruption is a code bug. */
6367 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb
));
6370 get_bh(*new_last_eb
);
6371 mlog(0, "returning block %llu, (cpos: %u)\n",
6372 (unsigned long long)le64_to_cpu(eb
->h_blkno
), cpos
);
6380 * Trim some clusters off the rightmost edge of a tree. Only called
6383 * The caller needs to:
6384 * - start journaling of each path component.
6385 * - compute and fully set up any new last ext block
6387 static int ocfs2_trim_tree(struct inode
*inode
, struct ocfs2_path
*path
,
6388 handle_t
*handle
, struct ocfs2_truncate_context
*tc
,
6389 u32 clusters_to_del
, u64
*delete_start
)
6391 int ret
, i
, index
= path
->p_tree_depth
;
6394 struct buffer_head
*bh
;
6395 struct ocfs2_extent_list
*el
;
6396 struct ocfs2_extent_rec
*rec
;
6400 while (index
>= 0) {
6401 bh
= path
->p_node
[index
].bh
;
6402 el
= path
->p_node
[index
].el
;
6404 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6405 index
, (unsigned long long)bh
->b_blocknr
);
6407 BUG_ON(le16_to_cpu(el
->l_next_free_rec
) == 0);
6410 (path
->p_tree_depth
- le16_to_cpu(el
->l_tree_depth
))) {
6411 ocfs2_error(inode
->i_sb
,
6412 "Inode %lu has invalid ext. block %llu",
6414 (unsigned long long)bh
->b_blocknr
);
6420 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
6421 rec
= &el
->l_recs
[i
];
6423 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6424 "next = %u\n", i
, le32_to_cpu(rec
->e_cpos
),
6425 ocfs2_rec_clusters(el
, rec
),
6426 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6427 le16_to_cpu(el
->l_next_free_rec
));
6429 BUG_ON(ocfs2_rec_clusters(el
, rec
) < clusters_to_del
);
6431 if (le16_to_cpu(el
->l_tree_depth
) == 0) {
6433 * If the leaf block contains a single empty
6434 * extent and no records, we can just remove
6437 if (i
== 0 && ocfs2_is_empty_extent(rec
)) {
6439 sizeof(struct ocfs2_extent_rec
));
6440 el
->l_next_free_rec
= cpu_to_le16(0);
6446 * Remove any empty extents by shifting things
6447 * left. That should make life much easier on
6448 * the code below. This condition is rare
6449 * enough that we shouldn't see a performance
6452 if (ocfs2_is_empty_extent(&el
->l_recs
[0])) {
6453 le16_add_cpu(&el
->l_next_free_rec
, -1);
6456 i
< le16_to_cpu(el
->l_next_free_rec
); i
++)
6457 el
->l_recs
[i
] = el
->l_recs
[i
+ 1];
6459 memset(&el
->l_recs
[i
], 0,
6460 sizeof(struct ocfs2_extent_rec
));
6463 * We've modified our extent list. The
6464 * simplest way to handle this change
6465 * is to being the search from the
6468 goto find_tail_record
;
6471 le16_add_cpu(&rec
->e_leaf_clusters
, -clusters_to_del
);
6474 * We'll use "new_edge" on our way back up the
6475 * tree to know what our rightmost cpos is.
6477 new_edge
= le16_to_cpu(rec
->e_leaf_clusters
);
6478 new_edge
+= le32_to_cpu(rec
->e_cpos
);
6481 * The caller will use this to delete data blocks.
6483 *delete_start
= le64_to_cpu(rec
->e_blkno
)
6484 + ocfs2_clusters_to_blocks(inode
->i_sb
,
6485 le16_to_cpu(rec
->e_leaf_clusters
));
6488 * If it's now empty, remove this record.
6490 if (le16_to_cpu(rec
->e_leaf_clusters
) == 0) {
6492 sizeof(struct ocfs2_extent_rec
));
6493 le16_add_cpu(&el
->l_next_free_rec
, -1);
6496 if (le64_to_cpu(rec
->e_blkno
) == deleted_eb
) {
6498 sizeof(struct ocfs2_extent_rec
));
6499 le16_add_cpu(&el
->l_next_free_rec
, -1);
6504 /* Can this actually happen? */
6505 if (le16_to_cpu(el
->l_next_free_rec
) == 0)
6509 * We never actually deleted any clusters
6510 * because our leaf was empty. There's no
6511 * reason to adjust the rightmost edge then.
6516 rec
->e_int_clusters
= cpu_to_le32(new_edge
);
6517 le32_add_cpu(&rec
->e_int_clusters
,
6518 -le32_to_cpu(rec
->e_cpos
));
6521 * A deleted child record should have been
6524 BUG_ON(le32_to_cpu(rec
->e_int_clusters
) == 0);
6528 ret
= ocfs2_journal_dirty(handle
, bh
);
6534 mlog(0, "extent list container %llu, after: record %d: "
6535 "(%u, %u, %llu), next = %u.\n",
6536 (unsigned long long)bh
->b_blocknr
, i
,
6537 le32_to_cpu(rec
->e_cpos
), ocfs2_rec_clusters(el
, rec
),
6538 (unsigned long long)le64_to_cpu(rec
->e_blkno
),
6539 le16_to_cpu(el
->l_next_free_rec
));
6542 * We must be careful to only attempt delete of an
6543 * extent block (and not the root inode block).
6545 if (index
> 0 && le16_to_cpu(el
->l_next_free_rec
) == 0) {
6546 struct ocfs2_extent_block
*eb
=
6547 (struct ocfs2_extent_block
*)bh
->b_data
;
6550 * Save this for use when processing the
6553 deleted_eb
= le64_to_cpu(eb
->h_blkno
);
6555 mlog(0, "deleting this extent block.\n");
6557 ocfs2_remove_from_cache(inode
, bh
);
6559 BUG_ON(ocfs2_rec_clusters(el
, &el
->l_recs
[0]));
6560 BUG_ON(le32_to_cpu(el
->l_recs
[0].e_cpos
));
6561 BUG_ON(le64_to_cpu(el
->l_recs
[0].e_blkno
));
6563 ret
= ocfs2_cache_extent_block_free(&tc
->tc_dealloc
, eb
);
6564 /* An error here is not fatal. */
6579 static int ocfs2_do_truncate(struct ocfs2_super
*osb
,
6580 unsigned int clusters_to_del
,
6581 struct inode
*inode
,
6582 struct buffer_head
*fe_bh
,
6584 struct ocfs2_truncate_context
*tc
,
6585 struct ocfs2_path
*path
)
6588 struct ocfs2_dinode
*fe
;
6589 struct ocfs2_extent_block
*last_eb
= NULL
;
6590 struct ocfs2_extent_list
*el
;
6591 struct buffer_head
*last_eb_bh
= NULL
;
6594 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
6596 status
= ocfs2_find_new_last_ext_blk(inode
, clusters_to_del
,
6604 * Each component will be touched, so we might as well journal
6605 * here to avoid having to handle errors later.
6607 status
= ocfs2_journal_access_path(inode
, handle
, path
);
6614 status
= ocfs2_journal_access_eb(handle
, inode
, last_eb_bh
,
6615 OCFS2_JOURNAL_ACCESS_WRITE
);
6621 last_eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
6624 el
= &(fe
->id2
.i_list
);
6627 * Lower levels depend on this never happening, but it's best
6628 * to check it up here before changing the tree.
6630 if (el
->l_tree_depth
&& el
->l_recs
[0].e_int_clusters
== 0) {
6631 ocfs2_error(inode
->i_sb
,
6632 "Inode %lu has an empty extent record, depth %u\n",
6633 inode
->i_ino
, le16_to_cpu(el
->l_tree_depth
));
6638 vfs_dq_free_space_nodirty(inode
,
6639 ocfs2_clusters_to_bytes(osb
->sb
, clusters_to_del
));
6640 spin_lock(&OCFS2_I(inode
)->ip_lock
);
6641 OCFS2_I(inode
)->ip_clusters
= le32_to_cpu(fe
->i_clusters
) -
6643 spin_unlock(&OCFS2_I(inode
)->ip_lock
);
6644 le32_add_cpu(&fe
->i_clusters
, -clusters_to_del
);
6645 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
6647 status
= ocfs2_trim_tree(inode
, path
, handle
, tc
,
6648 clusters_to_del
, &delete_blk
);
6654 if (le32_to_cpu(fe
->i_clusters
) == 0) {
6655 /* trunc to zero is a special case. */
6656 el
->l_tree_depth
= 0;
6657 fe
->i_last_eb_blk
= 0;
6659 fe
->i_last_eb_blk
= last_eb
->h_blkno
;
6661 status
= ocfs2_journal_dirty(handle
, fe_bh
);
6668 /* If there will be a new last extent block, then by
6669 * definition, there cannot be any leaves to the right of
6671 last_eb
->h_next_leaf_blk
= 0;
6672 status
= ocfs2_journal_dirty(handle
, last_eb_bh
);
6680 status
= ocfs2_truncate_log_append(osb
, handle
, delete_blk
,
6694 static int ocfs2_zero_func(handle_t
*handle
, struct buffer_head
*bh
)
6696 set_buffer_uptodate(bh
);
6697 mark_buffer_dirty(bh
);
6701 static void ocfs2_map_and_dirty_page(struct inode
*inode
, handle_t
*handle
,
6702 unsigned int from
, unsigned int to
,
6703 struct page
*page
, int zero
, u64
*phys
)
6705 int ret
, partial
= 0;
6707 ret
= ocfs2_map_page_blocks(page
, phys
, inode
, from
, to
, 0);
6712 zero_user_segment(page
, from
, to
);
6715 * Need to set the buffers we zero'd into uptodate
6716 * here if they aren't - ocfs2_map_page_blocks()
6717 * might've skipped some
6719 ret
= walk_page_buffers(handle
, page_buffers(page
),
6724 else if (ocfs2_should_order_data(inode
)) {
6725 ret
= ocfs2_jbd2_file_inode(handle
, inode
);
6731 SetPageUptodate(page
);
6733 flush_dcache_page(page
);
6736 static void ocfs2_zero_cluster_pages(struct inode
*inode
, loff_t start
,
6737 loff_t end
, struct page
**pages
,
6738 int numpages
, u64 phys
, handle_t
*handle
)
6742 unsigned int from
, to
= PAGE_CACHE_SIZE
;
6743 struct super_block
*sb
= inode
->i_sb
;
6745 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb
)));
6750 to
= PAGE_CACHE_SIZE
;
6751 for(i
= 0; i
< numpages
; i
++) {
6754 from
= start
& (PAGE_CACHE_SIZE
- 1);
6755 if ((end
>> PAGE_CACHE_SHIFT
) == page
->index
)
6756 to
= end
& (PAGE_CACHE_SIZE
- 1);
6758 BUG_ON(from
> PAGE_CACHE_SIZE
);
6759 BUG_ON(to
> PAGE_CACHE_SIZE
);
6761 ocfs2_map_and_dirty_page(inode
, handle
, from
, to
, page
, 1,
6764 start
= (page
->index
+ 1) << PAGE_CACHE_SHIFT
;
6768 ocfs2_unlock_and_free_pages(pages
, numpages
);
6771 static int ocfs2_grab_eof_pages(struct inode
*inode
, loff_t start
, loff_t end
,
6772 struct page
**pages
, int *num
)
6774 int numpages
, ret
= 0;
6775 struct super_block
*sb
= inode
->i_sb
;
6776 struct address_space
*mapping
= inode
->i_mapping
;
6777 unsigned long index
;
6778 loff_t last_page_bytes
;
6780 BUG_ON(start
> end
);
6782 BUG_ON(start
>> OCFS2_SB(sb
)->s_clustersize_bits
!=
6783 (end
- 1) >> OCFS2_SB(sb
)->s_clustersize_bits
);
6786 last_page_bytes
= PAGE_ALIGN(end
);
6787 index
= start
>> PAGE_CACHE_SHIFT
;
6789 pages
[numpages
] = grab_cache_page(mapping
, index
);
6790 if (!pages
[numpages
]) {
6798 } while (index
< (last_page_bytes
>> PAGE_CACHE_SHIFT
));
6803 ocfs2_unlock_and_free_pages(pages
, numpages
);
6813 * Zero the area past i_size but still within an allocated
6814 * cluster. This avoids exposing nonzero data on subsequent file
6817 * We need to call this before i_size is updated on the inode because
6818 * otherwise block_write_full_page() will skip writeout of pages past
6819 * i_size. The new_i_size parameter is passed for this reason.
6821 int ocfs2_zero_range_for_truncate(struct inode
*inode
, handle_t
*handle
,
6822 u64 range_start
, u64 range_end
)
6824 int ret
= 0, numpages
;
6825 struct page
**pages
= NULL
;
6827 unsigned int ext_flags
;
6828 struct super_block
*sb
= inode
->i_sb
;
6831 * File systems which don't support sparse files zero on every
6834 if (!ocfs2_sparse_alloc(OCFS2_SB(sb
)))
6837 pages
= kcalloc(ocfs2_pages_per_cluster(sb
),
6838 sizeof(struct page
*), GFP_NOFS
);
6839 if (pages
== NULL
) {
6845 if (range_start
== range_end
)
6848 ret
= ocfs2_extent_map_get_blocks(inode
,
6849 range_start
>> sb
->s_blocksize_bits
,
6850 &phys
, NULL
, &ext_flags
);
6857 * Tail is a hole, or is marked unwritten. In either case, we
6858 * can count on read and write to return/push zero's.
6860 if (phys
== 0 || ext_flags
& OCFS2_EXT_UNWRITTEN
)
6863 ret
= ocfs2_grab_eof_pages(inode
, range_start
, range_end
, pages
,
6870 ocfs2_zero_cluster_pages(inode
, range_start
, range_end
, pages
,
6871 numpages
, phys
, handle
);
6874 * Initiate writeout of the pages we zero'd here. We don't
6875 * wait on them - the truncate_inode_pages() call later will
6878 ret
= do_sync_mapping_range(inode
->i_mapping
, range_start
,
6879 range_end
- 1, SYNC_FILE_RANGE_WRITE
);
6890 static void ocfs2_zero_dinode_id2_with_xattr(struct inode
*inode
,
6891 struct ocfs2_dinode
*di
)
6893 unsigned int blocksize
= 1 << inode
->i_sb
->s_blocksize_bits
;
6894 unsigned int xattrsize
= le16_to_cpu(di
->i_xattr_inline_size
);
6896 if (le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_XATTR_FL
)
6897 memset(&di
->id2
, 0, blocksize
-
6898 offsetof(struct ocfs2_dinode
, id2
) -
6901 memset(&di
->id2
, 0, blocksize
-
6902 offsetof(struct ocfs2_dinode
, id2
));
6905 void ocfs2_dinode_new_extent_list(struct inode
*inode
,
6906 struct ocfs2_dinode
*di
)
6908 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
6909 di
->id2
.i_list
.l_tree_depth
= 0;
6910 di
->id2
.i_list
.l_next_free_rec
= 0;
6911 di
->id2
.i_list
.l_count
= cpu_to_le16(
6912 ocfs2_extent_recs_per_inode_with_xattr(inode
->i_sb
, di
));
6915 void ocfs2_set_inode_data_inline(struct inode
*inode
, struct ocfs2_dinode
*di
)
6917 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6918 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
6920 spin_lock(&oi
->ip_lock
);
6921 oi
->ip_dyn_features
|= OCFS2_INLINE_DATA_FL
;
6922 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
6923 spin_unlock(&oi
->ip_lock
);
6926 * We clear the entire i_data structure here so that all
6927 * fields can be properly initialized.
6929 ocfs2_zero_dinode_id2_with_xattr(inode
, di
);
6931 idata
->id_count
= cpu_to_le16(
6932 ocfs2_max_inline_data_with_xattr(inode
->i_sb
, di
));
6935 int ocfs2_convert_inline_data_to_extents(struct inode
*inode
,
6936 struct buffer_head
*di_bh
)
6938 int ret
, i
, has_data
, num_pages
= 0;
6940 u64
uninitialized_var(block
);
6941 struct ocfs2_inode_info
*oi
= OCFS2_I(inode
);
6942 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
6943 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
6944 struct ocfs2_alloc_context
*data_ac
= NULL
;
6945 struct page
**pages
= NULL
;
6946 loff_t end
= osb
->s_clustersize
;
6947 struct ocfs2_extent_tree et
;
6950 has_data
= i_size_read(inode
) ? 1 : 0;
6953 pages
= kcalloc(ocfs2_pages_per_cluster(osb
->sb
),
6954 sizeof(struct page
*), GFP_NOFS
);
6955 if (pages
== NULL
) {
6961 ret
= ocfs2_reserve_clusters(osb
, 1, &data_ac
);
6968 handle
= ocfs2_start_trans(osb
,
6969 ocfs2_inline_to_extents_credits(osb
->sb
));
6970 if (IS_ERR(handle
)) {
6971 ret
= PTR_ERR(handle
);
6976 ret
= ocfs2_journal_access_di(handle
, inode
, di_bh
,
6977 OCFS2_JOURNAL_ACCESS_WRITE
);
6985 unsigned int page_end
;
6988 if (vfs_dq_alloc_space_nodirty(inode
,
6989 ocfs2_clusters_to_bytes(osb
->sb
, 1))) {
6995 ret
= ocfs2_claim_clusters(osb
, handle
, data_ac
, 1, &bit_off
,
7003 * Save two copies, one for insert, and one that can
7004 * be changed by ocfs2_map_and_dirty_page() below.
7006 block
= phys
= ocfs2_clusters_to_blocks(inode
->i_sb
, bit_off
);
7009 * Non sparse file systems zero on extend, so no need
7012 if (!ocfs2_sparse_alloc(osb
) &&
7013 PAGE_CACHE_SIZE
< osb
->s_clustersize
)
7014 end
= PAGE_CACHE_SIZE
;
7016 ret
= ocfs2_grab_eof_pages(inode
, 0, end
, pages
, &num_pages
);
7023 * This should populate the 1st page for us and mark
7026 ret
= ocfs2_read_inline_data(inode
, pages
[0], di_bh
);
7032 page_end
= PAGE_CACHE_SIZE
;
7033 if (PAGE_CACHE_SIZE
> osb
->s_clustersize
)
7034 page_end
= osb
->s_clustersize
;
7036 for (i
= 0; i
< num_pages
; i
++)
7037 ocfs2_map_and_dirty_page(inode
, handle
, 0, page_end
,
7038 pages
[i
], i
> 0, &phys
);
7041 spin_lock(&oi
->ip_lock
);
7042 oi
->ip_dyn_features
&= ~OCFS2_INLINE_DATA_FL
;
7043 di
->i_dyn_features
= cpu_to_le16(oi
->ip_dyn_features
);
7044 spin_unlock(&oi
->ip_lock
);
7046 ocfs2_dinode_new_extent_list(inode
, di
);
7048 ocfs2_journal_dirty(handle
, di_bh
);
7052 * An error at this point should be extremely rare. If
7053 * this proves to be false, we could always re-build
7054 * the in-inode data from our pages.
7056 ocfs2_init_dinode_extent_tree(&et
, inode
, di_bh
);
7057 ret
= ocfs2_insert_extent(osb
, handle
, inode
, &et
,
7058 0, block
, 1, 0, NULL
);
7064 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
7068 if (ret
< 0 && did_quota
)
7069 vfs_dq_free_space_nodirty(inode
,
7070 ocfs2_clusters_to_bytes(osb
->sb
, 1));
7072 ocfs2_commit_trans(osb
, handle
);
7076 ocfs2_free_alloc_context(data_ac
);
7080 ocfs2_unlock_and_free_pages(pages
, num_pages
);
7088 * It is expected, that by the time you call this function,
7089 * inode->i_size and fe->i_size have been adjusted.
7091 * WARNING: This will kfree the truncate context
7093 int ocfs2_commit_truncate(struct ocfs2_super
*osb
,
7094 struct inode
*inode
,
7095 struct buffer_head
*fe_bh
,
7096 struct ocfs2_truncate_context
*tc
)
7098 int status
, i
, credits
, tl_sem
= 0;
7099 u32 clusters_to_del
, new_highest_cpos
, range
;
7100 struct ocfs2_extent_list
*el
;
7101 handle_t
*handle
= NULL
;
7102 struct inode
*tl_inode
= osb
->osb_tl_inode
;
7103 struct ocfs2_path
*path
= NULL
;
7104 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)fe_bh
->b_data
;
7108 new_highest_cpos
= ocfs2_clusters_for_bytes(osb
->sb
,
7109 i_size_read(inode
));
7111 path
= ocfs2_new_path(fe_bh
, &di
->id2
.i_list
,
7112 ocfs2_journal_access_di
);
7119 ocfs2_extent_map_trunc(inode
, new_highest_cpos
);
7123 * Check that we still have allocation to delete.
7125 if (OCFS2_I(inode
)->ip_clusters
== 0) {
7131 * Truncate always works against the rightmost tree branch.
7133 status
= ocfs2_find_path(inode
, path
, UINT_MAX
);
7139 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7140 OCFS2_I(inode
)->ip_clusters
, path
->p_tree_depth
);
7143 * By now, el will point to the extent list on the bottom most
7144 * portion of this tree. Only the tail record is considered in
7147 * We handle the following cases, in order:
7148 * - empty extent: delete the remaining branch
7149 * - remove the entire record
7150 * - remove a partial record
7151 * - no record needs to be removed (truncate has completed)
7153 el
= path_leaf_el(path
);
7154 if (le16_to_cpu(el
->l_next_free_rec
) == 0) {
7155 ocfs2_error(inode
->i_sb
,
7156 "Inode %llu has empty extent block at %llu\n",
7157 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7158 (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
7163 i
= le16_to_cpu(el
->l_next_free_rec
) - 1;
7164 range
= le32_to_cpu(el
->l_recs
[i
].e_cpos
) +
7165 ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
7166 if (i
== 0 && ocfs2_is_empty_extent(&el
->l_recs
[i
])) {
7167 clusters_to_del
= 0;
7168 } else if (le32_to_cpu(el
->l_recs
[i
].e_cpos
) >= new_highest_cpos
) {
7169 clusters_to_del
= ocfs2_rec_clusters(el
, &el
->l_recs
[i
]);
7170 } else if (range
> new_highest_cpos
) {
7171 clusters_to_del
= (ocfs2_rec_clusters(el
, &el
->l_recs
[i
]) +
7172 le32_to_cpu(el
->l_recs
[i
].e_cpos
)) -
7179 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7180 clusters_to_del
, (unsigned long long)path_leaf_bh(path
)->b_blocknr
);
7182 mutex_lock(&tl_inode
->i_mutex
);
7184 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7185 * record is free for use. If there isn't any, we flush to get
7186 * an empty truncate log. */
7187 if (ocfs2_truncate_log_needs_flush(osb
)) {
7188 status
= __ocfs2_flush_truncate_log(osb
);
7195 credits
= ocfs2_calc_tree_trunc_credits(osb
->sb
, clusters_to_del
,
7196 (struct ocfs2_dinode
*)fe_bh
->b_data
,
7198 handle
= ocfs2_start_trans(osb
, credits
);
7199 if (IS_ERR(handle
)) {
7200 status
= PTR_ERR(handle
);
7206 status
= ocfs2_do_truncate(osb
, clusters_to_del
, inode
, fe_bh
, handle
,
7213 mutex_unlock(&tl_inode
->i_mutex
);
7216 ocfs2_commit_trans(osb
, handle
);
7219 ocfs2_reinit_path(path
, 1);
7222 * The check above will catch the case where we've truncated
7223 * away all allocation.
7229 ocfs2_schedule_truncate_log_flush(osb
, 1);
7232 mutex_unlock(&tl_inode
->i_mutex
);
7235 ocfs2_commit_trans(osb
, handle
);
7237 ocfs2_run_deallocs(osb
, &tc
->tc_dealloc
);
7239 ocfs2_free_path(path
);
7241 /* This will drop the ext_alloc cluster lock for us */
7242 ocfs2_free_truncate_context(tc
);
7249 * Expects the inode to already be locked.
7251 int ocfs2_prepare_truncate(struct ocfs2_super
*osb
,
7252 struct inode
*inode
,
7253 struct buffer_head
*fe_bh
,
7254 struct ocfs2_truncate_context
**tc
)
7257 unsigned int new_i_clusters
;
7258 struct ocfs2_dinode
*fe
;
7259 struct ocfs2_extent_block
*eb
;
7260 struct buffer_head
*last_eb_bh
= NULL
;
7266 new_i_clusters
= ocfs2_clusters_for_bytes(osb
->sb
,
7267 i_size_read(inode
));
7268 fe
= (struct ocfs2_dinode
*) fe_bh
->b_data
;
7270 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
7271 "%llu\n", le32_to_cpu(fe
->i_clusters
), new_i_clusters
,
7272 (unsigned long long)le64_to_cpu(fe
->i_size
));
7274 *tc
= kzalloc(sizeof(struct ocfs2_truncate_context
), GFP_KERNEL
);
7280 ocfs2_init_dealloc_ctxt(&(*tc
)->tc_dealloc
);
7282 if (fe
->id2
.i_list
.l_tree_depth
) {
7283 status
= ocfs2_read_extent_block(inode
,
7284 le64_to_cpu(fe
->i_last_eb_blk
),
7290 eb
= (struct ocfs2_extent_block
*) last_eb_bh
->b_data
;
7293 (*tc
)->tc_last_eb_bh
= last_eb_bh
;
7299 ocfs2_free_truncate_context(*tc
);
7307 * 'start' is inclusive, 'end' is not.
7309 int ocfs2_truncate_inline(struct inode
*inode
, struct buffer_head
*di_bh
,
7310 unsigned int start
, unsigned int end
, int trunc
)
7313 unsigned int numbytes
;
7315 struct ocfs2_super
*osb
= OCFS2_SB(inode
->i_sb
);
7316 struct ocfs2_dinode
*di
= (struct ocfs2_dinode
*)di_bh
->b_data
;
7317 struct ocfs2_inline_data
*idata
= &di
->id2
.i_data
;
7319 if (end
> i_size_read(inode
))
7320 end
= i_size_read(inode
);
7322 BUG_ON(start
>= end
);
7324 if (!(OCFS2_I(inode
)->ip_dyn_features
& OCFS2_INLINE_DATA_FL
) ||
7325 !(le16_to_cpu(di
->i_dyn_features
) & OCFS2_INLINE_DATA_FL
) ||
7326 !ocfs2_supports_inline_data(osb
)) {
7327 ocfs2_error(inode
->i_sb
,
7328 "Inline data flags for inode %llu don't agree! "
7329 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7330 (unsigned long long)OCFS2_I(inode
)->ip_blkno
,
7331 le16_to_cpu(di
->i_dyn_features
),
7332 OCFS2_I(inode
)->ip_dyn_features
,
7333 osb
->s_feature_incompat
);
7338 handle
= ocfs2_start_trans(osb
, OCFS2_INODE_UPDATE_CREDITS
);
7339 if (IS_ERR(handle
)) {
7340 ret
= PTR_ERR(handle
);
7345 ret
= ocfs2_journal_access_di(handle
, inode
, di_bh
,
7346 OCFS2_JOURNAL_ACCESS_WRITE
);
7352 numbytes
= end
- start
;
7353 memset(idata
->id_data
+ start
, 0, numbytes
);
7356 * No need to worry about the data page here - it's been
7357 * truncated already and inline data doesn't need it for
7358 * pushing zero's to disk, so we'll let readpage pick it up
7362 i_size_write(inode
, start
);
7363 di
->i_size
= cpu_to_le64(start
);
7366 inode
->i_blocks
= ocfs2_inode_sector_count(inode
);
7367 inode
->i_ctime
= inode
->i_mtime
= CURRENT_TIME
;
7369 di
->i_ctime
= di
->i_mtime
= cpu_to_le64(inode
->i_ctime
.tv_sec
);
7370 di
->i_ctime_nsec
= di
->i_mtime_nsec
= cpu_to_le32(inode
->i_ctime
.tv_nsec
);
7372 ocfs2_journal_dirty(handle
, di_bh
);
7375 ocfs2_commit_trans(osb
, handle
);
7381 static void ocfs2_free_truncate_context(struct ocfs2_truncate_context
*tc
)
7384 * The caller is responsible for completing deallocation
7385 * before freeing the context.
7387 if (tc
->tc_dealloc
.c_first_suballocator
!= NULL
)
7389 "Truncate completion has non-empty dealloc context\n");
7391 brelse(tc
->tc_last_eb_bh
);