]>
Commit | Line | Data |
---|---|---|
ccd979bd MF |
1 | /* -*- mode: c; c-basic-offset: 8; -*- |
2 | * vim: noexpandtab sw=8 ts=8 sts=0: | |
3 | * | |
4 | * alloc.c | |
5 | * | |
6 | * Extent allocs and frees | |
7 | * | |
8 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. | |
9 | * | |
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. | |
14 | * | |
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. | |
19 | * | |
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. | |
24 | */ | |
25 | ||
26 | #include <linux/fs.h> | |
27 | #include <linux/types.h> | |
28 | #include <linux/slab.h> | |
29 | #include <linux/highmem.h> | |
60b11392 | 30 | #include <linux/swap.h> |
a90714c1 | 31 | #include <linux/quotaops.h> |
e80de36d | 32 | #include <linux/blkdev.h> |
174cd4b1 | 33 | #include <linux/sched/signal.h> |
ccd979bd | 34 | |
ccd979bd MF |
35 | #include <cluster/masklog.h> |
36 | ||
37 | #include "ocfs2.h" | |
38 | ||
39 | #include "alloc.h" | |
60b11392 | 40 | #include "aops.h" |
d6b32bbb | 41 | #include "blockcheck.h" |
ccd979bd MF |
42 | #include "dlmglue.h" |
43 | #include "extent_map.h" | |
44 | #include "inode.h" | |
45 | #include "journal.h" | |
46 | #include "localalloc.h" | |
47 | #include "suballoc.h" | |
48 | #include "sysfile.h" | |
49 | #include "file.h" | |
50 | #include "super.h" | |
51 | #include "uptodate.h" | |
2a50a743 | 52 | #include "xattr.h" |
bcbbb24a | 53 | #include "refcounttree.h" |
a09d09b8 | 54 | #include "ocfs2_trace.h" |
ccd979bd MF |
55 | |
56 | #include "buffer_head_io.h" | |
57 | ||
853a3a14 TM |
58 | enum ocfs2_contig_type { |
59 | CONTIG_NONE = 0, | |
60 | CONTIG_LEFT, | |
61 | CONTIG_RIGHT, | |
62 | CONTIG_LEFTRIGHT, | |
63 | }; | |
e7d4cb6b | 64 | |
853a3a14 TM |
65 | static enum ocfs2_contig_type |
66 | ocfs2_extent_rec_contig(struct super_block *sb, | |
67 | struct ocfs2_extent_rec *ext, | |
68 | struct ocfs2_extent_rec *insert_rec); | |
1625f8ac JB |
69 | /* |
70 | * Operations for a specific extent tree type. | |
71 | * | |
72 | * To implement an on-disk btree (extent tree) type in ocfs2, add | |
73 | * an ocfs2_extent_tree_operations structure and the matching | |
8d6220d6 | 74 | * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it |
1625f8ac JB |
75 | * for the allocation portion of the extent tree. |
76 | */ | |
e7d4cb6b | 77 | struct ocfs2_extent_tree_operations { |
1625f8ac JB |
78 | /* |
79 | * last_eb_blk is the block number of the right most leaf extent | |
80 | * block. Most on-disk structures containing an extent tree store | |
81 | * this value for fast access. The ->eo_set_last_eb_blk() and | |
82 | * ->eo_get_last_eb_blk() operations access this value. They are | |
83 | * both required. | |
84 | */ | |
35dc0aa3 JB |
85 | void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et, |
86 | u64 blkno); | |
87 | u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et); | |
1625f8ac JB |
88 | |
89 | /* | |
90 | * The on-disk structure usually keeps track of how many total | |
91 | * clusters are stored in this extent tree. This function updates | |
92 | * that value. new_clusters is the delta, and must be | |
93 | * added to the total. Required. | |
94 | */ | |
6136ca5f | 95 | void (*eo_update_clusters)(struct ocfs2_extent_tree *et, |
35dc0aa3 | 96 | u32 new_clusters); |
1625f8ac | 97 | |
92ba470c JB |
98 | /* |
99 | * If this extent tree is supported by an extent map, insert | |
100 | * a record into the map. | |
101 | */ | |
102 | void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et, | |
103 | struct ocfs2_extent_rec *rec); | |
104 | ||
4c911eef JB |
105 | /* |
106 | * If this extent tree is supported by an extent map, truncate the | |
107 | * map to clusters, | |
108 | */ | |
109 | void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et, | |
110 | u32 clusters); | |
111 | ||
1625f8ac JB |
112 | /* |
113 | * If ->eo_insert_check() exists, it is called before rec is | |
114 | * inserted into the extent tree. It is optional. | |
115 | */ | |
6136ca5f | 116 | int (*eo_insert_check)(struct ocfs2_extent_tree *et, |
1e61ee79 | 117 | struct ocfs2_extent_rec *rec); |
6136ca5f | 118 | int (*eo_sanity_check)(struct ocfs2_extent_tree *et); |
0ce1010f | 119 | |
1625f8ac JB |
120 | /* |
121 | * -------------------------------------------------------------- | |
122 | * The remaining are internal to ocfs2_extent_tree and don't have | |
123 | * accessor functions | |
124 | */ | |
125 | ||
126 | /* | |
127 | * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el. | |
128 | * It is required. | |
129 | */ | |
0ce1010f | 130 | void (*eo_fill_root_el)(struct ocfs2_extent_tree *et); |
1625f8ac JB |
131 | |
132 | /* | |
133 | * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if | |
134 | * it exists. If it does not, et->et_max_leaf_clusters is set | |
135 | * to 0 (unlimited). Optional. | |
136 | */ | |
6136ca5f | 137 | void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et); |
853a3a14 TM |
138 | |
139 | /* | |
140 | * ->eo_extent_contig test whether the 2 ocfs2_extent_rec | |
141 | * are contiguous or not. Optional. Don't need to set it if use | |
142 | * ocfs2_extent_rec as the tree leaf. | |
143 | */ | |
144 | enum ocfs2_contig_type | |
145 | (*eo_extent_contig)(struct ocfs2_extent_tree *et, | |
146 | struct ocfs2_extent_rec *ext, | |
147 | struct ocfs2_extent_rec *insert_rec); | |
e7d4cb6b TM |
148 | }; |
149 | ||
e7d4cb6b | 150 | |
f99b9b7c JB |
151 | /* |
152 | * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check | |
153 | * in the methods. | |
154 | */ | |
155 | static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et); | |
156 | static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, | |
157 | u64 blkno); | |
6136ca5f | 158 | static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et, |
f99b9b7c | 159 | u32 clusters); |
92ba470c JB |
160 | static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et, |
161 | struct ocfs2_extent_rec *rec); | |
4c911eef JB |
162 | static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et, |
163 | u32 clusters); | |
6136ca5f | 164 | static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et, |
f99b9b7c | 165 | struct ocfs2_extent_rec *rec); |
6136ca5f | 166 | static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et); |
f99b9b7c | 167 | static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et); |
71a36944 CG |
168 | |
169 | static int ocfs2_reuse_blk_from_dealloc(handle_t *handle, | |
170 | struct ocfs2_extent_tree *et, | |
171 | struct buffer_head **new_eb_bh, | |
172 | int blk_wanted, int *blk_given); | |
173 | static int ocfs2_is_dealloc_empty(struct ocfs2_extent_tree *et); | |
174 | ||
9e62dc09 | 175 | static const struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = { |
f99b9b7c JB |
176 | .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk, |
177 | .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk, | |
178 | .eo_update_clusters = ocfs2_dinode_update_clusters, | |
92ba470c | 179 | .eo_extent_map_insert = ocfs2_dinode_extent_map_insert, |
4c911eef | 180 | .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate, |
f99b9b7c JB |
181 | .eo_insert_check = ocfs2_dinode_insert_check, |
182 | .eo_sanity_check = ocfs2_dinode_sanity_check, | |
183 | .eo_fill_root_el = ocfs2_dinode_fill_root_el, | |
184 | }; | |
0ce1010f | 185 | |
e7d4cb6b TM |
186 | static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et, |
187 | u64 blkno) | |
188 | { | |
ea5efa15 | 189 | struct ocfs2_dinode *di = et->et_object; |
e7d4cb6b | 190 | |
f99b9b7c | 191 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
e7d4cb6b TM |
192 | di->i_last_eb_blk = cpu_to_le64(blkno); |
193 | } | |
194 | ||
195 | static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et) | |
196 | { | |
ea5efa15 | 197 | struct ocfs2_dinode *di = et->et_object; |
e7d4cb6b | 198 | |
f99b9b7c | 199 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
e7d4cb6b TM |
200 | return le64_to_cpu(di->i_last_eb_blk); |
201 | } | |
202 | ||
6136ca5f | 203 | static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et, |
e7d4cb6b TM |
204 | u32 clusters) |
205 | { | |
6136ca5f | 206 | struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci); |
ea5efa15 | 207 | struct ocfs2_dinode *di = et->et_object; |
e7d4cb6b TM |
208 | |
209 | le32_add_cpu(&di->i_clusters, clusters); | |
6136ca5f JB |
210 | spin_lock(&oi->ip_lock); |
211 | oi->ip_clusters = le32_to_cpu(di->i_clusters); | |
212 | spin_unlock(&oi->ip_lock); | |
e7d4cb6b TM |
213 | } |
214 | ||
92ba470c JB |
215 | static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et, |
216 | struct ocfs2_extent_rec *rec) | |
217 | { | |
218 | struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode; | |
219 | ||
220 | ocfs2_extent_map_insert_rec(inode, rec); | |
221 | } | |
222 | ||
4c911eef JB |
223 | static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et, |
224 | u32 clusters) | |
225 | { | |
226 | struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode; | |
227 | ||
228 | ocfs2_extent_map_trunc(inode, clusters); | |
229 | } | |
230 | ||
6136ca5f | 231 | static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et, |
1e61ee79 JB |
232 | struct ocfs2_extent_rec *rec) |
233 | { | |
6136ca5f JB |
234 | struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci); |
235 | struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb); | |
1e61ee79 | 236 | |
6136ca5f | 237 | BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL); |
1e61ee79 | 238 | mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) && |
6136ca5f | 239 | (oi->ip_clusters != le32_to_cpu(rec->e_cpos)), |
1e61ee79 JB |
240 | "Device %s, asking for sparse allocation: inode %llu, " |
241 | "cpos %u, clusters %u\n", | |
242 | osb->dev_str, | |
6136ca5f JB |
243 | (unsigned long long)oi->ip_blkno, |
244 | rec->e_cpos, oi->ip_clusters); | |
1e61ee79 JB |
245 | |
246 | return 0; | |
247 | } | |
248 | ||
6136ca5f | 249 | static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et) |
e7d4cb6b | 250 | { |
10995aa2 | 251 | struct ocfs2_dinode *di = et->et_object; |
e7d4cb6b | 252 | |
f99b9b7c | 253 | BUG_ON(et->et_ops != &ocfs2_dinode_et_ops); |
10995aa2 | 254 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); |
e7d4cb6b | 255 | |
10995aa2 | 256 | return 0; |
e7d4cb6b TM |
257 | } |
258 | ||
f99b9b7c JB |
259 | static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et) |
260 | { | |
261 | struct ocfs2_dinode *di = et->et_object; | |
262 | ||
263 | et->et_root_el = &di->id2.i_list; | |
264 | } | |
265 | ||
e7d4cb6b | 266 | |
0ce1010f JB |
267 | static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et) |
268 | { | |
2a50a743 | 269 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
0ce1010f | 270 | |
2a50a743 | 271 | et->et_root_el = &vb->vb_xv->xr_list; |
0ce1010f JB |
272 | } |
273 | ||
f56654c4 TM |
274 | static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et, |
275 | u64 blkno) | |
276 | { | |
2a50a743 | 277 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
f56654c4 | 278 | |
2a50a743 | 279 | vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno); |
f56654c4 TM |
280 | } |
281 | ||
282 | static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et) | |
283 | { | |
2a50a743 | 284 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
f56654c4 | 285 | |
2a50a743 | 286 | return le64_to_cpu(vb->vb_xv->xr_last_eb_blk); |
f56654c4 TM |
287 | } |
288 | ||
6136ca5f | 289 | static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et, |
f56654c4 TM |
290 | u32 clusters) |
291 | { | |
2a50a743 | 292 | struct ocfs2_xattr_value_buf *vb = et->et_object; |
f56654c4 | 293 | |
2a50a743 | 294 | le32_add_cpu(&vb->vb_xv->xr_clusters, clusters); |
f56654c4 TM |
295 | } |
296 | ||
9e62dc09 | 297 | static const struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = { |
35dc0aa3 JB |
298 | .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk, |
299 | .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk, | |
300 | .eo_update_clusters = ocfs2_xattr_value_update_clusters, | |
0ce1010f | 301 | .eo_fill_root_el = ocfs2_xattr_value_fill_root_el, |
f56654c4 TM |
302 | }; |
303 | ||
0ce1010f JB |
304 | static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et) |
305 | { | |
306 | struct ocfs2_xattr_block *xb = et->et_object; | |
307 | ||
308 | et->et_root_el = &xb->xb_attrs.xb_root.xt_list; | |
309 | } | |
310 | ||
6136ca5f | 311 | static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et) |
943cced3 | 312 | { |
6136ca5f | 313 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
943cced3 | 314 | et->et_max_leaf_clusters = |
6136ca5f | 315 | ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE); |
943cced3 JB |
316 | } |
317 | ||
ba492615 TM |
318 | static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et, |
319 | u64 blkno) | |
320 | { | |
ea5efa15 | 321 | struct ocfs2_xattr_block *xb = et->et_object; |
ba492615 TM |
322 | struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; |
323 | ||
324 | xt->xt_last_eb_blk = cpu_to_le64(blkno); | |
325 | } | |
326 | ||
327 | static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et) | |
328 | { | |
ea5efa15 | 329 | struct ocfs2_xattr_block *xb = et->et_object; |
ba492615 TM |
330 | struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root; |
331 | ||
332 | return le64_to_cpu(xt->xt_last_eb_blk); | |
333 | } | |
334 | ||
6136ca5f | 335 | static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et, |
ba492615 TM |
336 | u32 clusters) |
337 | { | |
ea5efa15 | 338 | struct ocfs2_xattr_block *xb = et->et_object; |
ba492615 TM |
339 | |
340 | le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters); | |
341 | } | |
342 | ||
9e62dc09 | 343 | static const struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = { |
35dc0aa3 JB |
344 | .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk, |
345 | .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk, | |
346 | .eo_update_clusters = ocfs2_xattr_tree_update_clusters, | |
0ce1010f | 347 | .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el, |
943cced3 | 348 | .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters, |
ba492615 TM |
349 | }; |
350 | ||
9b7895ef MF |
351 | static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et, |
352 | u64 blkno) | |
353 | { | |
354 | struct ocfs2_dx_root_block *dx_root = et->et_object; | |
355 | ||
356 | dx_root->dr_last_eb_blk = cpu_to_le64(blkno); | |
357 | } | |
358 | ||
359 | static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et) | |
360 | { | |
361 | struct ocfs2_dx_root_block *dx_root = et->et_object; | |
362 | ||
363 | return le64_to_cpu(dx_root->dr_last_eb_blk); | |
364 | } | |
365 | ||
6136ca5f | 366 | static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et, |
9b7895ef MF |
367 | u32 clusters) |
368 | { | |
369 | struct ocfs2_dx_root_block *dx_root = et->et_object; | |
370 | ||
371 | le32_add_cpu(&dx_root->dr_clusters, clusters); | |
372 | } | |
373 | ||
6136ca5f | 374 | static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et) |
9b7895ef MF |
375 | { |
376 | struct ocfs2_dx_root_block *dx_root = et->et_object; | |
377 | ||
378 | BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root)); | |
379 | ||
380 | return 0; | |
381 | } | |
382 | ||
383 | static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et) | |
384 | { | |
385 | struct ocfs2_dx_root_block *dx_root = et->et_object; | |
386 | ||
387 | et->et_root_el = &dx_root->dr_list; | |
388 | } | |
389 | ||
9e62dc09 | 390 | static const struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = { |
9b7895ef MF |
391 | .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk, |
392 | .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk, | |
393 | .eo_update_clusters = ocfs2_dx_root_update_clusters, | |
394 | .eo_sanity_check = ocfs2_dx_root_sanity_check, | |
395 | .eo_fill_root_el = ocfs2_dx_root_fill_root_el, | |
396 | }; | |
397 | ||
fe924415 TM |
398 | static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et) |
399 | { | |
400 | struct ocfs2_refcount_block *rb = et->et_object; | |
401 | ||
402 | et->et_root_el = &rb->rf_list; | |
403 | } | |
404 | ||
405 | static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et, | |
406 | u64 blkno) | |
407 | { | |
408 | struct ocfs2_refcount_block *rb = et->et_object; | |
409 | ||
410 | rb->rf_last_eb_blk = cpu_to_le64(blkno); | |
411 | } | |
412 | ||
413 | static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et) | |
414 | { | |
415 | struct ocfs2_refcount_block *rb = et->et_object; | |
416 | ||
417 | return le64_to_cpu(rb->rf_last_eb_blk); | |
418 | } | |
419 | ||
420 | static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et, | |
421 | u32 clusters) | |
422 | { | |
423 | struct ocfs2_refcount_block *rb = et->et_object; | |
424 | ||
425 | le32_add_cpu(&rb->rf_clusters, clusters); | |
426 | } | |
427 | ||
428 | static enum ocfs2_contig_type | |
429 | ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et, | |
430 | struct ocfs2_extent_rec *ext, | |
431 | struct ocfs2_extent_rec *insert_rec) | |
432 | { | |
433 | return CONTIG_NONE; | |
434 | } | |
435 | ||
9e62dc09 | 436 | static const struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = { |
fe924415 TM |
437 | .eo_set_last_eb_blk = ocfs2_refcount_tree_set_last_eb_blk, |
438 | .eo_get_last_eb_blk = ocfs2_refcount_tree_get_last_eb_blk, | |
439 | .eo_update_clusters = ocfs2_refcount_tree_update_clusters, | |
440 | .eo_fill_root_el = ocfs2_refcount_tree_fill_root_el, | |
441 | .eo_extent_contig = ocfs2_refcount_tree_extent_contig, | |
442 | }; | |
443 | ||
8d6220d6 | 444 | static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et, |
5e404e9e | 445 | struct ocfs2_caching_info *ci, |
8d6220d6 | 446 | struct buffer_head *bh, |
13723d00 | 447 | ocfs2_journal_access_func access, |
8d6220d6 | 448 | void *obj, |
9e62dc09 | 449 | const struct ocfs2_extent_tree_operations *ops) |
e7d4cb6b | 450 | { |
1a09f556 | 451 | et->et_ops = ops; |
ce1d9ea6 | 452 | et->et_root_bh = bh; |
5e404e9e | 453 | et->et_ci = ci; |
13723d00 | 454 | et->et_root_journal_access = access; |
ea5efa15 JB |
455 | if (!obj) |
456 | obj = (void *)bh->b_data; | |
457 | et->et_object = obj; | |
71a36944 | 458 | et->et_dealloc = NULL; |
e7d4cb6b | 459 | |
0ce1010f | 460 | et->et_ops->eo_fill_root_el(et); |
943cced3 JB |
461 | if (!et->et_ops->eo_fill_max_leaf_clusters) |
462 | et->et_max_leaf_clusters = 0; | |
463 | else | |
6136ca5f | 464 | et->et_ops->eo_fill_max_leaf_clusters(et); |
e7d4cb6b TM |
465 | } |
466 | ||
8d6220d6 | 467 | void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et, |
5e404e9e | 468 | struct ocfs2_caching_info *ci, |
8d6220d6 | 469 | struct buffer_head *bh) |
1a09f556 | 470 | { |
5e404e9e | 471 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di, |
13723d00 | 472 | NULL, &ocfs2_dinode_et_ops); |
1a09f556 JB |
473 | } |
474 | ||
8d6220d6 | 475 | void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et, |
5e404e9e | 476 | struct ocfs2_caching_info *ci, |
8d6220d6 | 477 | struct buffer_head *bh) |
1a09f556 | 478 | { |
5e404e9e | 479 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb, |
13723d00 | 480 | NULL, &ocfs2_xattr_tree_et_ops); |
1a09f556 JB |
481 | } |
482 | ||
8d6220d6 | 483 | void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et, |
5e404e9e | 484 | struct ocfs2_caching_info *ci, |
2a50a743 | 485 | struct ocfs2_xattr_value_buf *vb) |
e7d4cb6b | 486 | { |
5e404e9e | 487 | __ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb, |
8d6220d6 | 488 | &ocfs2_xattr_value_et_ops); |
e7d4cb6b TM |
489 | } |
490 | ||
9b7895ef | 491 | void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et, |
5e404e9e | 492 | struct ocfs2_caching_info *ci, |
9b7895ef MF |
493 | struct buffer_head *bh) |
494 | { | |
5e404e9e | 495 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr, |
9b7895ef MF |
496 | NULL, &ocfs2_dx_root_et_ops); |
497 | } | |
498 | ||
fe924415 TM |
499 | void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et, |
500 | struct ocfs2_caching_info *ci, | |
501 | struct buffer_head *bh) | |
502 | { | |
503 | __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb, | |
504 | NULL, &ocfs2_refcount_tree_et_ops); | |
505 | } | |
506 | ||
35dc0aa3 JB |
507 | static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et, |
508 | u64 new_last_eb_blk) | |
e7d4cb6b | 509 | { |
ce1d9ea6 | 510 | et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk); |
e7d4cb6b TM |
511 | } |
512 | ||
35dc0aa3 | 513 | static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et) |
e7d4cb6b | 514 | { |
ce1d9ea6 | 515 | return et->et_ops->eo_get_last_eb_blk(et); |
e7d4cb6b TM |
516 | } |
517 | ||
6136ca5f | 518 | static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et, |
35dc0aa3 JB |
519 | u32 clusters) |
520 | { | |
6136ca5f | 521 | et->et_ops->eo_update_clusters(et, clusters); |
35dc0aa3 JB |
522 | } |
523 | ||
92ba470c JB |
524 | static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et, |
525 | struct ocfs2_extent_rec *rec) | |
526 | { | |
527 | if (et->et_ops->eo_extent_map_insert) | |
528 | et->et_ops->eo_extent_map_insert(et, rec); | |
529 | } | |
530 | ||
4c911eef JB |
531 | static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et, |
532 | u32 clusters) | |
533 | { | |
534 | if (et->et_ops->eo_extent_map_truncate) | |
535 | et->et_ops->eo_extent_map_truncate(et, clusters); | |
536 | } | |
537 | ||
13723d00 | 538 | static inline int ocfs2_et_root_journal_access(handle_t *handle, |
13723d00 JB |
539 | struct ocfs2_extent_tree *et, |
540 | int type) | |
541 | { | |
d9a0a1f8 | 542 | return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh, |
13723d00 JB |
543 | type); |
544 | } | |
545 | ||
853a3a14 TM |
546 | static inline enum ocfs2_contig_type |
547 | ocfs2_et_extent_contig(struct ocfs2_extent_tree *et, | |
548 | struct ocfs2_extent_rec *rec, | |
549 | struct ocfs2_extent_rec *insert_rec) | |
550 | { | |
551 | if (et->et_ops->eo_extent_contig) | |
552 | return et->et_ops->eo_extent_contig(et, rec, insert_rec); | |
553 | ||
554 | return ocfs2_extent_rec_contig( | |
555 | ocfs2_metadata_cache_get_super(et->et_ci), | |
556 | rec, insert_rec); | |
557 | } | |
558 | ||
6136ca5f | 559 | static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et, |
1e61ee79 JB |
560 | struct ocfs2_extent_rec *rec) |
561 | { | |
562 | int ret = 0; | |
563 | ||
564 | if (et->et_ops->eo_insert_check) | |
6136ca5f | 565 | ret = et->et_ops->eo_insert_check(et, rec); |
1e61ee79 JB |
566 | return ret; |
567 | } | |
568 | ||
6136ca5f | 569 | static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et) |
e7d4cb6b | 570 | { |
1e61ee79 JB |
571 | int ret = 0; |
572 | ||
573 | if (et->et_ops->eo_sanity_check) | |
6136ca5f | 574 | ret = et->et_ops->eo_sanity_check(et); |
1e61ee79 | 575 | return ret; |
e7d4cb6b TM |
576 | } |
577 | ||
59a5e416 MF |
578 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
579 | struct ocfs2_extent_block *eb); | |
d401dc12 JB |
580 | static void ocfs2_adjust_rightmost_records(handle_t *handle, |
581 | struct ocfs2_extent_tree *et, | |
6b791bcc TM |
582 | struct ocfs2_path *path, |
583 | struct ocfs2_extent_rec *insert_rec); | |
dcd0538f MF |
584 | /* |
585 | * Reset the actual path elements so that we can re-use the structure | |
586 | * to build another path. Generally, this involves freeing the buffer | |
587 | * heads. | |
588 | */ | |
e2e9f608 | 589 | void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root) |
dcd0538f MF |
590 | { |
591 | int i, start = 0, depth = 0; | |
592 | struct ocfs2_path_item *node; | |
ccd979bd | 593 | |
dcd0538f MF |
594 | if (keep_root) |
595 | start = 1; | |
ccd979bd | 596 | |
dcd0538f MF |
597 | for(i = start; i < path_num_items(path); i++) { |
598 | node = &path->p_node[i]; | |
599 | ||
600 | brelse(node->bh); | |
601 | node->bh = NULL; | |
602 | node->el = NULL; | |
603 | } | |
604 | ||
605 | /* | |
606 | * Tree depth may change during truncate, or insert. If we're | |
607 | * keeping the root extent list, then make sure that our path | |
608 | * structure reflects the proper depth. | |
609 | */ | |
610 | if (keep_root) | |
611 | depth = le16_to_cpu(path_root_el(path)->l_tree_depth); | |
13723d00 JB |
612 | else |
613 | path_root_access(path) = NULL; | |
dcd0538f MF |
614 | |
615 | path->p_tree_depth = depth; | |
616 | } | |
617 | ||
e2e9f608 | 618 | void ocfs2_free_path(struct ocfs2_path *path) |
dcd0538f MF |
619 | { |
620 | if (path) { | |
621 | ocfs2_reinit_path(path, 0); | |
622 | kfree(path); | |
623 | } | |
624 | } | |
625 | ||
328d5752 MF |
626 | /* |
627 | * All the elements of src into dest. After this call, src could be freed | |
628 | * without affecting dest. | |
629 | * | |
630 | * Both paths should have the same root. Any non-root elements of dest | |
631 | * will be freed. | |
632 | */ | |
633 | static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src) | |
634 | { | |
635 | int i; | |
636 | ||
637 | BUG_ON(path_root_bh(dest) != path_root_bh(src)); | |
638 | BUG_ON(path_root_el(dest) != path_root_el(src)); | |
13723d00 | 639 | BUG_ON(path_root_access(dest) != path_root_access(src)); |
328d5752 MF |
640 | |
641 | ocfs2_reinit_path(dest, 1); | |
642 | ||
643 | for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { | |
644 | dest->p_node[i].bh = src->p_node[i].bh; | |
645 | dest->p_node[i].el = src->p_node[i].el; | |
646 | ||
647 | if (dest->p_node[i].bh) | |
648 | get_bh(dest->p_node[i].bh); | |
649 | } | |
650 | } | |
651 | ||
dcd0538f MF |
652 | /* |
653 | * Make the *dest path the same as src and re-initialize src path to | |
654 | * have a root only. | |
655 | */ | |
656 | static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src) | |
657 | { | |
658 | int i; | |
659 | ||
660 | BUG_ON(path_root_bh(dest) != path_root_bh(src)); | |
13723d00 | 661 | BUG_ON(path_root_access(dest) != path_root_access(src)); |
dcd0538f MF |
662 | |
663 | for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) { | |
664 | brelse(dest->p_node[i].bh); | |
665 | ||
666 | dest->p_node[i].bh = src->p_node[i].bh; | |
667 | dest->p_node[i].el = src->p_node[i].el; | |
668 | ||
669 | src->p_node[i].bh = NULL; | |
670 | src->p_node[i].el = NULL; | |
671 | } | |
672 | } | |
673 | ||
674 | /* | |
675 | * Insert an extent block at given index. | |
676 | * | |
677 | * This will not take an additional reference on eb_bh. | |
678 | */ | |
679 | static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index, | |
680 | struct buffer_head *eb_bh) | |
681 | { | |
682 | struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data; | |
683 | ||
684 | /* | |
685 | * Right now, no root bh is an extent block, so this helps | |
686 | * catch code errors with dinode trees. The assertion can be | |
687 | * safely removed if we ever need to insert extent block | |
688 | * structures at the root. | |
689 | */ | |
690 | BUG_ON(index == 0); | |
691 | ||
692 | path->p_node[index].bh = eb_bh; | |
693 | path->p_node[index].el = &eb->h_list; | |
694 | } | |
695 | ||
696 | static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh, | |
13723d00 JB |
697 | struct ocfs2_extent_list *root_el, |
698 | ocfs2_journal_access_func access) | |
dcd0538f MF |
699 | { |
700 | struct ocfs2_path *path; | |
ccd979bd | 701 | |
dcd0538f MF |
702 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH); |
703 | ||
704 | path = kzalloc(sizeof(*path), GFP_NOFS); | |
705 | if (path) { | |
706 | path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth); | |
707 | get_bh(root_bh); | |
708 | path_root_bh(path) = root_bh; | |
709 | path_root_el(path) = root_el; | |
13723d00 | 710 | path_root_access(path) = access; |
dcd0538f MF |
711 | } |
712 | ||
713 | return path; | |
714 | } | |
715 | ||
e2e9f608 | 716 | struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path) |
ffdd7a54 | 717 | { |
13723d00 JB |
718 | return ocfs2_new_path(path_root_bh(path), path_root_el(path), |
719 | path_root_access(path)); | |
ffdd7a54 JB |
720 | } |
721 | ||
e2e9f608 | 722 | struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et) |
ffdd7a54 | 723 | { |
13723d00 JB |
724 | return ocfs2_new_path(et->et_root_bh, et->et_root_el, |
725 | et->et_root_journal_access); | |
726 | } | |
727 | ||
728 | /* | |
729 | * Journal the buffer at depth idx. All idx>0 are extent_blocks, | |
730 | * otherwise it's the root_access function. | |
731 | * | |
732 | * I don't like the way this function's name looks next to | |
733 | * ocfs2_journal_access_path(), but I don't have a better one. | |
734 | */ | |
e2e9f608 TM |
735 | int ocfs2_path_bh_journal_access(handle_t *handle, |
736 | struct ocfs2_caching_info *ci, | |
737 | struct ocfs2_path *path, | |
738 | int idx) | |
13723d00 JB |
739 | { |
740 | ocfs2_journal_access_func access = path_root_access(path); | |
741 | ||
742 | if (!access) | |
743 | access = ocfs2_journal_access; | |
744 | ||
745 | if (idx) | |
746 | access = ocfs2_journal_access_eb; | |
747 | ||
0cf2f763 | 748 | return access(handle, ci, path->p_node[idx].bh, |
13723d00 | 749 | OCFS2_JOURNAL_ACCESS_WRITE); |
ffdd7a54 JB |
750 | } |
751 | ||
dcd0538f MF |
752 | /* |
753 | * Convenience function to journal all components in a path. | |
754 | */ | |
e2e9f608 TM |
755 | int ocfs2_journal_access_path(struct ocfs2_caching_info *ci, |
756 | handle_t *handle, | |
757 | struct ocfs2_path *path) | |
dcd0538f MF |
758 | { |
759 | int i, ret = 0; | |
760 | ||
761 | if (!path) | |
762 | goto out; | |
763 | ||
764 | for(i = 0; i < path_num_items(path); i++) { | |
0cf2f763 | 765 | ret = ocfs2_path_bh_journal_access(handle, ci, path, i); |
dcd0538f MF |
766 | if (ret < 0) { |
767 | mlog_errno(ret); | |
768 | goto out; | |
769 | } | |
770 | } | |
771 | ||
772 | out: | |
773 | return ret; | |
774 | } | |
775 | ||
328d5752 MF |
776 | /* |
777 | * Return the index of the extent record which contains cluster #v_cluster. | |
778 | * -1 is returned if it was not found. | |
779 | * | |
780 | * Should work fine on interior and exterior nodes. | |
781 | */ | |
782 | int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster) | |
783 | { | |
784 | int ret = -1; | |
785 | int i; | |
786 | struct ocfs2_extent_rec *rec; | |
787 | u32 rec_end, rec_start, clusters; | |
788 | ||
789 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { | |
790 | rec = &el->l_recs[i]; | |
791 | ||
792 | rec_start = le32_to_cpu(rec->e_cpos); | |
793 | clusters = ocfs2_rec_clusters(el, rec); | |
794 | ||
795 | rec_end = rec_start + clusters; | |
796 | ||
797 | if (v_cluster >= rec_start && v_cluster < rec_end) { | |
798 | ret = i; | |
799 | break; | |
800 | } | |
801 | } | |
802 | ||
803 | return ret; | |
804 | } | |
805 | ||
e48edee2 MF |
806 | /* |
807 | * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and | |
853a3a14 | 808 | * ocfs2_extent_rec_contig only work properly against leaf nodes! |
e48edee2 | 809 | */ |
dcd0538f MF |
810 | static int ocfs2_block_extent_contig(struct super_block *sb, |
811 | struct ocfs2_extent_rec *ext, | |
812 | u64 blkno) | |
ccd979bd | 813 | { |
e48edee2 MF |
814 | u64 blk_end = le64_to_cpu(ext->e_blkno); |
815 | ||
816 | blk_end += ocfs2_clusters_to_blocks(sb, | |
817 | le16_to_cpu(ext->e_leaf_clusters)); | |
818 | ||
819 | return blkno == blk_end; | |
ccd979bd MF |
820 | } |
821 | ||
dcd0538f MF |
822 | static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left, |
823 | struct ocfs2_extent_rec *right) | |
824 | { | |
e48edee2 MF |
825 | u32 left_range; |
826 | ||
827 | left_range = le32_to_cpu(left->e_cpos) + | |
828 | le16_to_cpu(left->e_leaf_clusters); | |
829 | ||
830 | return (left_range == le32_to_cpu(right->e_cpos)); | |
dcd0538f MF |
831 | } |
832 | ||
833 | static enum ocfs2_contig_type | |
853a3a14 TM |
834 | ocfs2_extent_rec_contig(struct super_block *sb, |
835 | struct ocfs2_extent_rec *ext, | |
836 | struct ocfs2_extent_rec *insert_rec) | |
dcd0538f MF |
837 | { |
838 | u64 blkno = le64_to_cpu(insert_rec->e_blkno); | |
839 | ||
328d5752 MF |
840 | /* |
841 | * Refuse to coalesce extent records with different flag | |
842 | * fields - we don't want to mix unwritten extents with user | |
843 | * data. | |
844 | */ | |
845 | if (ext->e_flags != insert_rec->e_flags) | |
846 | return CONTIG_NONE; | |
847 | ||
dcd0538f | 848 | if (ocfs2_extents_adjacent(ext, insert_rec) && |
b4a17651 | 849 | ocfs2_block_extent_contig(sb, ext, blkno)) |
dcd0538f MF |
850 | return CONTIG_RIGHT; |
851 | ||
852 | blkno = le64_to_cpu(ext->e_blkno); | |
853 | if (ocfs2_extents_adjacent(insert_rec, ext) && | |
b4a17651 | 854 | ocfs2_block_extent_contig(sb, insert_rec, blkno)) |
dcd0538f MF |
855 | return CONTIG_LEFT; |
856 | ||
857 | return CONTIG_NONE; | |
858 | } | |
859 | ||
860 | /* | |
861 | * NOTE: We can have pretty much any combination of contiguousness and | |
862 | * appending. | |
863 | * | |
864 | * The usefulness of APPEND_TAIL is more in that it lets us know that | |
865 | * we'll have to update the path to that leaf. | |
866 | */ | |
867 | enum ocfs2_append_type { | |
868 | APPEND_NONE = 0, | |
869 | APPEND_TAIL, | |
870 | }; | |
871 | ||
328d5752 MF |
872 | enum ocfs2_split_type { |
873 | SPLIT_NONE = 0, | |
874 | SPLIT_LEFT, | |
875 | SPLIT_RIGHT, | |
876 | }; | |
877 | ||
dcd0538f | 878 | struct ocfs2_insert_type { |
328d5752 | 879 | enum ocfs2_split_type ins_split; |
dcd0538f MF |
880 | enum ocfs2_append_type ins_appending; |
881 | enum ocfs2_contig_type ins_contig; | |
882 | int ins_contig_index; | |
dcd0538f MF |
883 | int ins_tree_depth; |
884 | }; | |
885 | ||
328d5752 MF |
886 | struct ocfs2_merge_ctxt { |
887 | enum ocfs2_contig_type c_contig_type; | |
888 | int c_has_empty_extent; | |
889 | int c_split_covers_rec; | |
328d5752 MF |
890 | }; |
891 | ||
5e96581a JB |
892 | static int ocfs2_validate_extent_block(struct super_block *sb, |
893 | struct buffer_head *bh) | |
894 | { | |
d6b32bbb | 895 | int rc; |
5e96581a JB |
896 | struct ocfs2_extent_block *eb = |
897 | (struct ocfs2_extent_block *)bh->b_data; | |
898 | ||
a09d09b8 | 899 | trace_ocfs2_validate_extent_block((unsigned long long)bh->b_blocknr); |
970e4936 | 900 | |
d6b32bbb JB |
901 | BUG_ON(!buffer_uptodate(bh)); |
902 | ||
903 | /* | |
904 | * If the ecc fails, we return the error but otherwise | |
905 | * leave the filesystem running. We know any error is | |
906 | * local to this block. | |
907 | */ | |
908 | rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check); | |
13723d00 JB |
909 | if (rc) { |
910 | mlog(ML_ERROR, "Checksum failed for extent block %llu\n", | |
911 | (unsigned long long)bh->b_blocknr); | |
d6b32bbb | 912 | return rc; |
13723d00 | 913 | } |
d6b32bbb JB |
914 | |
915 | /* | |
916 | * Errors after here are fatal. | |
917 | */ | |
918 | ||
5e96581a | 919 | if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) { |
17a5b9ab | 920 | rc = ocfs2_error(sb, |
7ecef14a JP |
921 | "Extent block #%llu has bad signature %.*s\n", |
922 | (unsigned long long)bh->b_blocknr, 7, | |
923 | eb->h_signature); | |
17a5b9ab | 924 | goto bail; |
5e96581a JB |
925 | } |
926 | ||
927 | if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) { | |
17a5b9ab | 928 | rc = ocfs2_error(sb, |
7ecef14a JP |
929 | "Extent block #%llu has an invalid h_blkno of %llu\n", |
930 | (unsigned long long)bh->b_blocknr, | |
931 | (unsigned long long)le64_to_cpu(eb->h_blkno)); | |
17a5b9ab | 932 | goto bail; |
5e96581a JB |
933 | } |
934 | ||
935 | if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) { | |
17a5b9ab | 936 | rc = ocfs2_error(sb, |
7ecef14a JP |
937 | "Extent block #%llu has an invalid h_fs_generation of #%u\n", |
938 | (unsigned long long)bh->b_blocknr, | |
939 | le32_to_cpu(eb->h_fs_generation)); | |
17a5b9ab | 940 | goto bail; |
5e96581a | 941 | } |
17a5b9ab GR |
942 | bail: |
943 | return rc; | |
5e96581a JB |
944 | } |
945 | ||
3d03a305 | 946 | int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno, |
5e96581a JB |
947 | struct buffer_head **bh) |
948 | { | |
949 | int rc; | |
950 | struct buffer_head *tmp = *bh; | |
951 | ||
3d03a305 | 952 | rc = ocfs2_read_block(ci, eb_blkno, &tmp, |
970e4936 | 953 | ocfs2_validate_extent_block); |
5e96581a JB |
954 | |
955 | /* If ocfs2_read_block() got us a new bh, pass it up. */ | |
970e4936 | 956 | if (!rc && !*bh) |
5e96581a JB |
957 | *bh = tmp; |
958 | ||
5e96581a JB |
959 | return rc; |
960 | } | |
961 | ||
962 | ||
ccd979bd MF |
963 | /* |
964 | * How many free extents have we got before we need more meta data? | |
965 | */ | |
964f14a0 | 966 | int ocfs2_num_free_extents(struct ocfs2_extent_tree *et) |
ccd979bd MF |
967 | { |
968 | int retval; | |
e7d4cb6b | 969 | struct ocfs2_extent_list *el = NULL; |
ccd979bd MF |
970 | struct ocfs2_extent_block *eb; |
971 | struct buffer_head *eb_bh = NULL; | |
e7d4cb6b | 972 | u64 last_eb_blk = 0; |
ccd979bd | 973 | |
f99b9b7c JB |
974 | el = et->et_root_el; |
975 | last_eb_blk = ocfs2_et_get_last_eb_blk(et); | |
ccd979bd | 976 | |
e7d4cb6b | 977 | if (last_eb_blk) { |
3d03a305 JB |
978 | retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk, |
979 | &eb_bh); | |
ccd979bd MF |
980 | if (retval < 0) { |
981 | mlog_errno(retval); | |
982 | goto bail; | |
983 | } | |
984 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; | |
985 | el = &eb->h_list; | |
e7d4cb6b | 986 | } |
ccd979bd MF |
987 | |
988 | BUG_ON(el->l_tree_depth != 0); | |
989 | ||
990 | retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec); | |
991 | bail: | |
a81cb88b | 992 | brelse(eb_bh); |
ccd979bd | 993 | |
a09d09b8 | 994 | trace_ocfs2_num_free_extents(retval); |
ccd979bd MF |
995 | return retval; |
996 | } | |
997 | ||
998 | /* expects array to already be allocated | |
999 | * | |
1000 | * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and | |
1001 | * l_count for you | |
1002 | */ | |
42a5a7a9 JB |
1003 | static int ocfs2_create_new_meta_bhs(handle_t *handle, |
1004 | struct ocfs2_extent_tree *et, | |
ccd979bd MF |
1005 | int wanted, |
1006 | struct ocfs2_alloc_context *meta_ac, | |
1007 | struct buffer_head *bhs[]) | |
1008 | { | |
1009 | int count, status, i; | |
1010 | u16 suballoc_bit_start; | |
1011 | u32 num_got; | |
2b6cb576 | 1012 | u64 suballoc_loc, first_blkno; |
42a5a7a9 JB |
1013 | struct ocfs2_super *osb = |
1014 | OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci)); | |
ccd979bd MF |
1015 | struct ocfs2_extent_block *eb; |
1016 | ||
ccd979bd MF |
1017 | count = 0; |
1018 | while (count < wanted) { | |
1ed9b777 | 1019 | status = ocfs2_claim_metadata(handle, |
ccd979bd MF |
1020 | meta_ac, |
1021 | wanted - count, | |
2b6cb576 | 1022 | &suballoc_loc, |
ccd979bd MF |
1023 | &suballoc_bit_start, |
1024 | &num_got, | |
1025 | &first_blkno); | |
1026 | if (status < 0) { | |
1027 | mlog_errno(status); | |
1028 | goto bail; | |
1029 | } | |
1030 | ||
1031 | for(i = count; i < (num_got + count); i++) { | |
1032 | bhs[i] = sb_getblk(osb->sb, first_blkno); | |
1033 | if (bhs[i] == NULL) { | |
7391a294 | 1034 | status = -ENOMEM; |
ccd979bd MF |
1035 | mlog_errno(status); |
1036 | goto bail; | |
1037 | } | |
42a5a7a9 | 1038 | ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]); |
ccd979bd | 1039 | |
42a5a7a9 JB |
1040 | status = ocfs2_journal_access_eb(handle, et->et_ci, |
1041 | bhs[i], | |
13723d00 | 1042 | OCFS2_JOURNAL_ACCESS_CREATE); |
ccd979bd MF |
1043 | if (status < 0) { |
1044 | mlog_errno(status); | |
1045 | goto bail; | |
1046 | } | |
1047 | ||
1048 | memset(bhs[i]->b_data, 0, osb->sb->s_blocksize); | |
1049 | eb = (struct ocfs2_extent_block *) bhs[i]->b_data; | |
1050 | /* Ok, setup the minimal stuff here. */ | |
1051 | strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE); | |
1052 | eb->h_blkno = cpu_to_le64(first_blkno); | |
1053 | eb->h_fs_generation = cpu_to_le32(osb->fs_generation); | |
b89c5428 TY |
1054 | eb->h_suballoc_slot = |
1055 | cpu_to_le16(meta_ac->ac_alloc_slot); | |
2b6cb576 | 1056 | eb->h_suballoc_loc = cpu_to_le64(suballoc_loc); |
ccd979bd MF |
1057 | eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start); |
1058 | eb->h_list.l_count = | |
1059 | cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb)); | |
1060 | ||
1061 | suballoc_bit_start++; | |
1062 | first_blkno++; | |
1063 | ||
1064 | /* We'll also be dirtied by the caller, so | |
1065 | * this isn't absolutely necessary. */ | |
ec20cec7 | 1066 | ocfs2_journal_dirty(handle, bhs[i]); |
ccd979bd MF |
1067 | } |
1068 | ||
1069 | count += num_got; | |
1070 | } | |
1071 | ||
1072 | status = 0; | |
1073 | bail: | |
1074 | if (status < 0) { | |
1075 | for(i = 0; i < wanted; i++) { | |
a81cb88b | 1076 | brelse(bhs[i]); |
ccd979bd MF |
1077 | bhs[i] = NULL; |
1078 | } | |
c1e8d35e | 1079 | mlog_errno(status); |
ccd979bd | 1080 | } |
ccd979bd MF |
1081 | return status; |
1082 | } | |
1083 | ||
dcd0538f MF |
1084 | /* |
1085 | * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth(). | |
1086 | * | |
1087 | * Returns the sum of the rightmost extent rec logical offset and | |
1088 | * cluster count. | |
1089 | * | |
1090 | * ocfs2_add_branch() uses this to determine what logical cluster | |
1091 | * value should be populated into the leftmost new branch records. | |
1092 | * | |
1093 | * ocfs2_shift_tree_depth() uses this to determine the # clusters | |
1094 | * value for the new topmost tree record. | |
1095 | */ | |
1096 | static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el) | |
1097 | { | |
1098 | int i; | |
1099 | ||
1100 | i = le16_to_cpu(el->l_next_free_rec) - 1; | |
1101 | ||
1102 | return le32_to_cpu(el->l_recs[i].e_cpos) + | |
e48edee2 | 1103 | ocfs2_rec_clusters(el, &el->l_recs[i]); |
dcd0538f MF |
1104 | } |
1105 | ||
6b791bcc TM |
1106 | /* |
1107 | * Change range of the branches in the right most path according to the leaf | |
1108 | * extent block's rightmost record. | |
1109 | */ | |
1110 | static int ocfs2_adjust_rightmost_branch(handle_t *handle, | |
6b791bcc TM |
1111 | struct ocfs2_extent_tree *et) |
1112 | { | |
1113 | int status; | |
1114 | struct ocfs2_path *path = NULL; | |
1115 | struct ocfs2_extent_list *el; | |
1116 | struct ocfs2_extent_rec *rec; | |
1117 | ||
1118 | path = ocfs2_new_path_from_et(et); | |
1119 | if (!path) { | |
1120 | status = -ENOMEM; | |
1121 | return status; | |
1122 | } | |
1123 | ||
facdb77f | 1124 | status = ocfs2_find_path(et->et_ci, path, UINT_MAX); |
6b791bcc TM |
1125 | if (status < 0) { |
1126 | mlog_errno(status); | |
1127 | goto out; | |
1128 | } | |
1129 | ||
c901fb00 | 1130 | status = ocfs2_extend_trans(handle, path_num_items(path)); |
6b791bcc TM |
1131 | if (status < 0) { |
1132 | mlog_errno(status); | |
1133 | goto out; | |
1134 | } | |
1135 | ||
d401dc12 | 1136 | status = ocfs2_journal_access_path(et->et_ci, handle, path); |
6b791bcc TM |
1137 | if (status < 0) { |
1138 | mlog_errno(status); | |
1139 | goto out; | |
1140 | } | |
1141 | ||
1142 | el = path_leaf_el(path); | |
3a251f04 | 1143 | rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec) - 1]; |
6b791bcc | 1144 | |
d401dc12 | 1145 | ocfs2_adjust_rightmost_records(handle, et, path, rec); |
6b791bcc TM |
1146 | |
1147 | out: | |
1148 | ocfs2_free_path(path); | |
1149 | return status; | |
1150 | } | |
1151 | ||
ccd979bd MF |
1152 | /* |
1153 | * Add an entire tree branch to our inode. eb_bh is the extent block | |
d401dc12 | 1154 | * to start at, if we don't want to start the branch at the root |
ccd979bd MF |
1155 | * structure. |
1156 | * | |
1157 | * last_eb_bh is required as we have to update it's next_leaf pointer | |
1158 | * for the new last extent block. | |
1159 | * | |
1160 | * the new branch will be 'empty' in the sense that every block will | |
e48edee2 | 1161 | * contain a single record with cluster count == 0. |
ccd979bd | 1162 | */ |
d401dc12 | 1163 | static int ocfs2_add_branch(handle_t *handle, |
e7d4cb6b | 1164 | struct ocfs2_extent_tree *et, |
ccd979bd | 1165 | struct buffer_head *eb_bh, |
328d5752 | 1166 | struct buffer_head **last_eb_bh, |
ccd979bd MF |
1167 | struct ocfs2_alloc_context *meta_ac) |
1168 | { | |
71a36944 | 1169 | int status, new_blocks, i, block_given = 0; |
ccd979bd MF |
1170 | u64 next_blkno, new_last_eb_blk; |
1171 | struct buffer_head *bh; | |
1172 | struct buffer_head **new_eb_bhs = NULL; | |
ccd979bd MF |
1173 | struct ocfs2_extent_block *eb; |
1174 | struct ocfs2_extent_list *eb_el; | |
1175 | struct ocfs2_extent_list *el; | |
6b791bcc | 1176 | u32 new_cpos, root_end; |
ccd979bd | 1177 | |
328d5752 | 1178 | BUG_ON(!last_eb_bh || !*last_eb_bh); |
ccd979bd | 1179 | |
ccd979bd MF |
1180 | if (eb_bh) { |
1181 | eb = (struct ocfs2_extent_block *) eb_bh->b_data; | |
1182 | el = &eb->h_list; | |
1183 | } else | |
ce1d9ea6 | 1184 | el = et->et_root_el; |
ccd979bd MF |
1185 | |
1186 | /* we never add a branch to a leaf. */ | |
1187 | BUG_ON(!el->l_tree_depth); | |
1188 | ||
1189 | new_blocks = le16_to_cpu(el->l_tree_depth); | |
1190 | ||
6b791bcc TM |
1191 | eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data; |
1192 | new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list); | |
1193 | root_end = ocfs2_sum_rightmost_rec(et->et_root_el); | |
1194 | ||
1195 | /* | |
1196 | * If there is a gap before the root end and the real end | |
1197 | * of the righmost leaf block, we need to remove the gap | |
1198 | * between new_cpos and root_end first so that the tree | |
1199 | * is consistent after we add a new branch(it will start | |
1200 | * from new_cpos). | |
1201 | */ | |
1202 | if (root_end > new_cpos) { | |
a09d09b8 TM |
1203 | trace_ocfs2_adjust_rightmost_branch( |
1204 | (unsigned long long) | |
1205 | ocfs2_metadata_cache_owner(et->et_ci), | |
1206 | root_end, new_cpos); | |
1207 | ||
d401dc12 | 1208 | status = ocfs2_adjust_rightmost_branch(handle, et); |
6b791bcc TM |
1209 | if (status) { |
1210 | mlog_errno(status); | |
1211 | goto bail; | |
1212 | } | |
1213 | } | |
1214 | ||
ccd979bd MF |
1215 | /* allocate the number of new eb blocks we need */ |
1216 | new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *), | |
1217 | GFP_KERNEL); | |
1218 | if (!new_eb_bhs) { | |
1219 | status = -ENOMEM; | |
1220 | mlog_errno(status); | |
1221 | goto bail; | |
1222 | } | |
1223 | ||
71a36944 CG |
1224 | /* Firstyly, try to reuse dealloc since we have already estimated how |
1225 | * many extent blocks we may use. | |
1226 | */ | |
1227 | if (!ocfs2_is_dealloc_empty(et)) { | |
1228 | status = ocfs2_reuse_blk_from_dealloc(handle, et, | |
1229 | new_eb_bhs, new_blocks, | |
1230 | &block_given); | |
1231 | if (status < 0) { | |
1232 | mlog_errno(status); | |
1233 | goto bail; | |
1234 | } | |
1235 | } | |
1236 | ||
1237 | BUG_ON(block_given > new_blocks); | |
1238 | ||
1239 | if (block_given < new_blocks) { | |
1240 | BUG_ON(!meta_ac); | |
1241 | status = ocfs2_create_new_meta_bhs(handle, et, | |
1242 | new_blocks - block_given, | |
1243 | meta_ac, | |
1244 | &new_eb_bhs[block_given]); | |
1245 | if (status < 0) { | |
1246 | mlog_errno(status); | |
1247 | goto bail; | |
1248 | } | |
ccd979bd MF |
1249 | } |
1250 | ||
1251 | /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be | |
1252 | * linked with the rest of the tree. | |
1253 | * conversly, new_eb_bhs[0] is the new bottommost leaf. | |
1254 | * | |
1255 | * when we leave the loop, new_last_eb_blk will point to the | |
1256 | * newest leaf, and next_blkno will point to the topmost extent | |
1257 | * block. */ | |
1258 | next_blkno = new_last_eb_blk = 0; | |
1259 | for(i = 0; i < new_blocks; i++) { | |
1260 | bh = new_eb_bhs[i]; | |
1261 | eb = (struct ocfs2_extent_block *) bh->b_data; | |
5e96581a JB |
1262 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
1263 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); | |
ccd979bd MF |
1264 | eb_el = &eb->h_list; |
1265 | ||
d401dc12 | 1266 | status = ocfs2_journal_access_eb(handle, et->et_ci, bh, |
13723d00 | 1267 | OCFS2_JOURNAL_ACCESS_CREATE); |
ccd979bd MF |
1268 | if (status < 0) { |
1269 | mlog_errno(status); | |
1270 | goto bail; | |
1271 | } | |
1272 | ||
1273 | eb->h_next_leaf_blk = 0; | |
1274 | eb_el->l_tree_depth = cpu_to_le16(i); | |
1275 | eb_el->l_next_free_rec = cpu_to_le16(1); | |
dcd0538f MF |
1276 | /* |
1277 | * This actually counts as an empty extent as | |
1278 | * c_clusters == 0 | |
1279 | */ | |
1280 | eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos); | |
ccd979bd | 1281 | eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno); |
e48edee2 MF |
1282 | /* |
1283 | * eb_el isn't always an interior node, but even leaf | |
1284 | * nodes want a zero'd flags and reserved field so | |
1285 | * this gets the whole 32 bits regardless of use. | |
1286 | */ | |
1287 | eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0); | |
ccd979bd MF |
1288 | if (!eb_el->l_tree_depth) |
1289 | new_last_eb_blk = le64_to_cpu(eb->h_blkno); | |
1290 | ||
ec20cec7 | 1291 | ocfs2_journal_dirty(handle, bh); |
ccd979bd MF |
1292 | next_blkno = le64_to_cpu(eb->h_blkno); |
1293 | } | |
1294 | ||
1295 | /* This is a bit hairy. We want to update up to three blocks | |
1296 | * here without leaving any of them in an inconsistent state | |
1297 | * in case of error. We don't have to worry about | |
1298 | * journal_dirty erroring as it won't unless we've aborted the | |
1299 | * handle (in which case we would never be here) so reserving | |
1300 | * the write with journal_access is all we need to do. */ | |
d401dc12 | 1301 | status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh, |
13723d00 | 1302 | OCFS2_JOURNAL_ACCESS_WRITE); |
ccd979bd MF |
1303 | if (status < 0) { |
1304 | mlog_errno(status); | |
1305 | goto bail; | |
1306 | } | |
d9a0a1f8 | 1307 | status = ocfs2_et_root_journal_access(handle, et, |
13723d00 | 1308 | OCFS2_JOURNAL_ACCESS_WRITE); |
ccd979bd MF |
1309 | if (status < 0) { |
1310 | mlog_errno(status); | |
1311 | goto bail; | |
1312 | } | |
1313 | if (eb_bh) { | |
d401dc12 | 1314 | status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh, |
13723d00 | 1315 | OCFS2_JOURNAL_ACCESS_WRITE); |
ccd979bd MF |
1316 | if (status < 0) { |
1317 | mlog_errno(status); | |
1318 | goto bail; | |
1319 | } | |
1320 | } | |
1321 | ||
1322 | /* Link the new branch into the rest of the tree (el will | |
e7d4cb6b | 1323 | * either be on the root_bh, or the extent block passed in. */ |
ccd979bd MF |
1324 | i = le16_to_cpu(el->l_next_free_rec); |
1325 | el->l_recs[i].e_blkno = cpu_to_le64(next_blkno); | |
dcd0538f | 1326 | el->l_recs[i].e_cpos = cpu_to_le32(new_cpos); |
e48edee2 | 1327 | el->l_recs[i].e_int_clusters = 0; |
ccd979bd MF |
1328 | le16_add_cpu(&el->l_next_free_rec, 1); |
1329 | ||
1330 | /* fe needs a new last extent block pointer, as does the | |
1331 | * next_leaf on the previously last-extent-block. */ | |
35dc0aa3 | 1332 | ocfs2_et_set_last_eb_blk(et, new_last_eb_blk); |
ccd979bd | 1333 | |
328d5752 | 1334 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; |
ccd979bd MF |
1335 | eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk); |
1336 | ||
ec20cec7 JB |
1337 | ocfs2_journal_dirty(handle, *last_eb_bh); |
1338 | ocfs2_journal_dirty(handle, et->et_root_bh); | |
1339 | if (eb_bh) | |
1340 | ocfs2_journal_dirty(handle, eb_bh); | |
ccd979bd | 1341 | |
328d5752 MF |
1342 | /* |
1343 | * Some callers want to track the rightmost leaf so pass it | |
1344 | * back here. | |
1345 | */ | |
1346 | brelse(*last_eb_bh); | |
1347 | get_bh(new_eb_bhs[0]); | |
1348 | *last_eb_bh = new_eb_bhs[0]; | |
1349 | ||
ccd979bd MF |
1350 | status = 0; |
1351 | bail: | |
1352 | if (new_eb_bhs) { | |
1353 | for (i = 0; i < new_blocks; i++) | |
a81cb88b | 1354 | brelse(new_eb_bhs[i]); |
ccd979bd MF |
1355 | kfree(new_eb_bhs); |
1356 | } | |
1357 | ||
ccd979bd MF |
1358 | return status; |
1359 | } | |
1360 | ||
1361 | /* | |
1362 | * adds another level to the allocation tree. | |
1363 | * returns back the new extent block so you can add a branch to it | |
1364 | * after this call. | |
1365 | */ | |
d401dc12 | 1366 | static int ocfs2_shift_tree_depth(handle_t *handle, |
e7d4cb6b | 1367 | struct ocfs2_extent_tree *et, |
ccd979bd MF |
1368 | struct ocfs2_alloc_context *meta_ac, |
1369 | struct buffer_head **ret_new_eb_bh) | |
1370 | { | |
71a36944 | 1371 | int status, i, block_given = 0; |
dcd0538f | 1372 | u32 new_clusters; |
ccd979bd | 1373 | struct buffer_head *new_eb_bh = NULL; |
ccd979bd | 1374 | struct ocfs2_extent_block *eb; |
e7d4cb6b | 1375 | struct ocfs2_extent_list *root_el; |
ccd979bd MF |
1376 | struct ocfs2_extent_list *eb_el; |
1377 | ||
71a36944 CG |
1378 | if (!ocfs2_is_dealloc_empty(et)) { |
1379 | status = ocfs2_reuse_blk_from_dealloc(handle, et, | |
1380 | &new_eb_bh, 1, | |
1381 | &block_given); | |
1382 | } else if (meta_ac) { | |
1383 | status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac, | |
1384 | &new_eb_bh); | |
1385 | ||
1386 | } else { | |
1387 | BUG(); | |
1388 | } | |
1389 | ||
ccd979bd MF |
1390 | if (status < 0) { |
1391 | mlog_errno(status); | |
1392 | goto bail; | |
1393 | } | |
1394 | ||
1395 | eb = (struct ocfs2_extent_block *) new_eb_bh->b_data; | |
5e96581a JB |
1396 | /* ocfs2_create_new_meta_bhs() should create it right! */ |
1397 | BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb)); | |
ccd979bd MF |
1398 | |
1399 | eb_el = &eb->h_list; | |
ce1d9ea6 | 1400 | root_el = et->et_root_el; |
ccd979bd | 1401 | |
d401dc12 | 1402 | status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh, |
13723d00 | 1403 | OCFS2_JOURNAL_ACCESS_CREATE); |
ccd979bd MF |
1404 | if (status < 0) { |
1405 | mlog_errno(status); | |
1406 | goto bail; | |
1407 | } | |
1408 | ||
e7d4cb6b TM |
1409 | /* copy the root extent list data into the new extent block */ |
1410 | eb_el->l_tree_depth = root_el->l_tree_depth; | |
1411 | eb_el->l_next_free_rec = root_el->l_next_free_rec; | |
1412 | for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++) | |
1413 | eb_el->l_recs[i] = root_el->l_recs[i]; | |
ccd979bd | 1414 | |
ec20cec7 | 1415 | ocfs2_journal_dirty(handle, new_eb_bh); |
ccd979bd | 1416 | |
d9a0a1f8 | 1417 | status = ocfs2_et_root_journal_access(handle, et, |
13723d00 | 1418 | OCFS2_JOURNAL_ACCESS_WRITE); |
ccd979bd MF |
1419 | if (status < 0) { |
1420 | mlog_errno(status); | |
1421 | goto bail; | |
1422 | } | |
1423 | ||
dcd0538f MF |
1424 | new_clusters = ocfs2_sum_rightmost_rec(eb_el); |
1425 | ||
e7d4cb6b TM |
1426 | /* update root_bh now */ |
1427 | le16_add_cpu(&root_el->l_tree_depth, 1); | |
1428 | root_el->l_recs[0].e_cpos = 0; | |
1429 | root_el->l_recs[0].e_blkno = eb->h_blkno; | |
1430 | root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters); | |
1431 | for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) | |
1432 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); | |
1433 | root_el->l_next_free_rec = cpu_to_le16(1); | |
ccd979bd MF |
1434 | |
1435 | /* If this is our 1st tree depth shift, then last_eb_blk | |
1436 | * becomes the allocated extent block */ | |
e7d4cb6b | 1437 | if (root_el->l_tree_depth == cpu_to_le16(1)) |
35dc0aa3 | 1438 | ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno)); |
ccd979bd | 1439 | |
ec20cec7 | 1440 | ocfs2_journal_dirty(handle, et->et_root_bh); |
ccd979bd MF |
1441 | |
1442 | *ret_new_eb_bh = new_eb_bh; | |
1443 | new_eb_bh = NULL; | |
1444 | status = 0; | |
1445 | bail: | |
a81cb88b | 1446 | brelse(new_eb_bh); |
ccd979bd | 1447 | |
ccd979bd MF |
1448 | return status; |
1449 | } | |
1450 | ||
ccd979bd MF |
1451 | /* |
1452 | * Should only be called when there is no space left in any of the | |
1453 | * leaf nodes. What we want to do is find the lowest tree depth | |
1454 | * non-leaf extent block with room for new records. There are three | |
1455 | * valid results of this search: | |
1456 | * | |
1457 | * 1) a lowest extent block is found, then we pass it back in | |
1458 | * *lowest_eb_bh and return '0' | |
1459 | * | |
e7d4cb6b | 1460 | * 2) the search fails to find anything, but the root_el has room. We |
ccd979bd MF |
1461 | * pass NULL back in *lowest_eb_bh, but still return '0' |
1462 | * | |
e7d4cb6b | 1463 | * 3) the search fails to find anything AND the root_el is full, in |
ccd979bd MF |
1464 | * which case we return > 0 |
1465 | * | |
1466 | * return status < 0 indicates an error. | |
1467 | */ | |
d401dc12 | 1468 | static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et, |
ccd979bd MF |
1469 | struct buffer_head **target_bh) |
1470 | { | |
1471 | int status = 0, i; | |
1472 | u64 blkno; | |
ccd979bd MF |
1473 | struct ocfs2_extent_block *eb; |
1474 | struct ocfs2_extent_list *el; | |
1475 | struct buffer_head *bh = NULL; | |
1476 | struct buffer_head *lowest_bh = NULL; | |
1477 | ||
ccd979bd MF |
1478 | *target_bh = NULL; |
1479 | ||
ce1d9ea6 | 1480 | el = et->et_root_el; |
ccd979bd MF |
1481 | |
1482 | while(le16_to_cpu(el->l_tree_depth) > 1) { | |
1483 | if (le16_to_cpu(el->l_next_free_rec) == 0) { | |
93f5920d JP |
1484 | status = ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
1485 | "Owner %llu has empty extent list (next_free_rec == 0)\n", | |
1486 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); | |
ccd979bd MF |
1487 | goto bail; |
1488 | } | |
1489 | i = le16_to_cpu(el->l_next_free_rec) - 1; | |
1490 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); | |
1491 | if (!blkno) { | |
93f5920d JP |
1492 | status = ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
1493 | "Owner %llu has extent list where extent # %d has no physical block start\n", | |
1494 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i); | |
ccd979bd MF |
1495 | goto bail; |
1496 | } | |
1497 | ||
a81cb88b MF |
1498 | brelse(bh); |
1499 | bh = NULL; | |
ccd979bd | 1500 | |
3d03a305 | 1501 | status = ocfs2_read_extent_block(et->et_ci, blkno, &bh); |
ccd979bd MF |
1502 | if (status < 0) { |
1503 | mlog_errno(status); | |
1504 | goto bail; | |
1505 | } | |
dcd0538f MF |
1506 | |
1507 | eb = (struct ocfs2_extent_block *) bh->b_data; | |
dcd0538f MF |
1508 | el = &eb->h_list; |
1509 | ||
1510 | if (le16_to_cpu(el->l_next_free_rec) < | |
1511 | le16_to_cpu(el->l_count)) { | |
a81cb88b | 1512 | brelse(lowest_bh); |
dcd0538f MF |
1513 | lowest_bh = bh; |
1514 | get_bh(lowest_bh); | |
1515 | } | |
1516 | } | |
1517 | ||
1518 | /* If we didn't find one and the fe doesn't have any room, | |
1519 | * then return '1' */ | |
ce1d9ea6 | 1520 | el = et->et_root_el; |
e7d4cb6b | 1521 | if (!lowest_bh && (el->l_next_free_rec == el->l_count)) |
dcd0538f MF |
1522 | status = 1; |
1523 | ||
1524 | *target_bh = lowest_bh; | |
1525 | bail: | |
a81cb88b | 1526 | brelse(bh); |
dcd0538f | 1527 | |
dcd0538f MF |
1528 | return status; |
1529 | } | |
1530 | ||
c3afcbb3 MF |
1531 | /* |
1532 | * Grow a b-tree so that it has more records. | |
1533 | * | |
1534 | * We might shift the tree depth in which case existing paths should | |
1535 | * be considered invalid. | |
1536 | * | |
1537 | * Tree depth after the grow is returned via *final_depth. | |
328d5752 MF |
1538 | * |
1539 | * *last_eb_bh will be updated by ocfs2_add_branch(). | |
c3afcbb3 | 1540 | */ |
d401dc12 JB |
1541 | static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et, |
1542 | int *final_depth, struct buffer_head **last_eb_bh, | |
c3afcbb3 MF |
1543 | struct ocfs2_alloc_context *meta_ac) |
1544 | { | |
1545 | int ret, shift; | |
ce1d9ea6 | 1546 | struct ocfs2_extent_list *el = et->et_root_el; |
e7d4cb6b | 1547 | int depth = le16_to_cpu(el->l_tree_depth); |
c3afcbb3 MF |
1548 | struct buffer_head *bh = NULL; |
1549 | ||
71a36944 | 1550 | BUG_ON(meta_ac == NULL && ocfs2_is_dealloc_empty(et)); |
c3afcbb3 | 1551 | |
d401dc12 | 1552 | shift = ocfs2_find_branch_target(et, &bh); |
c3afcbb3 MF |
1553 | if (shift < 0) { |
1554 | ret = shift; | |
1555 | mlog_errno(ret); | |
1556 | goto out; | |
1557 | } | |
1558 | ||
1559 | /* We traveled all the way to the bottom of the allocation tree | |
1560 | * and didn't find room for any more extents - we need to add | |
1561 | * another tree level */ | |
1562 | if (shift) { | |
1563 | BUG_ON(bh); | |
a09d09b8 TM |
1564 | trace_ocfs2_grow_tree( |
1565 | (unsigned long long) | |
1566 | ocfs2_metadata_cache_owner(et->et_ci), | |
1567 | depth); | |
c3afcbb3 MF |
1568 | |
1569 | /* ocfs2_shift_tree_depth will return us a buffer with | |
1570 | * the new extent block (so we can pass that to | |
1571 | * ocfs2_add_branch). */ | |
d401dc12 | 1572 | ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh); |
c3afcbb3 MF |
1573 | if (ret < 0) { |
1574 | mlog_errno(ret); | |
1575 | goto out; | |
1576 | } | |
1577 | depth++; | |
328d5752 MF |
1578 | if (depth == 1) { |
1579 | /* | |
1580 | * Special case: we have room now if we shifted from | |
1581 | * tree_depth 0, so no more work needs to be done. | |
1582 | * | |
1583 | * We won't be calling add_branch, so pass | |
1584 | * back *last_eb_bh as the new leaf. At depth | |
1585 | * zero, it should always be null so there's | |
1586 | * no reason to brelse. | |
1587 | */ | |
1588 | BUG_ON(*last_eb_bh); | |
1589 | get_bh(bh); | |
1590 | *last_eb_bh = bh; | |
c3afcbb3 | 1591 | goto out; |
328d5752 | 1592 | } |
c3afcbb3 MF |
1593 | } |
1594 | ||
1595 | /* call ocfs2_add_branch to add the final part of the tree with | |
1596 | * the new data. */ | |
d401dc12 | 1597 | ret = ocfs2_add_branch(handle, et, bh, last_eb_bh, |
c3afcbb3 MF |
1598 | meta_ac); |
1599 | if (ret < 0) { | |
1600 | mlog_errno(ret); | |
1601 | goto out; | |
1602 | } | |
1603 | ||
1604 | out: | |
1605 | if (final_depth) | |
1606 | *final_depth = depth; | |
1607 | brelse(bh); | |
1608 | return ret; | |
1609 | } | |
1610 | ||
dcd0538f MF |
1611 | /* |
1612 | * This function will discard the rightmost extent record. | |
1613 | */ | |
1614 | static void ocfs2_shift_records_right(struct ocfs2_extent_list *el) | |
1615 | { | |
1616 | int next_free = le16_to_cpu(el->l_next_free_rec); | |
1617 | int count = le16_to_cpu(el->l_count); | |
1618 | unsigned int num_bytes; | |
1619 | ||
1620 | BUG_ON(!next_free); | |
1621 | /* This will cause us to go off the end of our extent list. */ | |
1622 | BUG_ON(next_free >= count); | |
1623 | ||
1624 | num_bytes = sizeof(struct ocfs2_extent_rec) * next_free; | |
1625 | ||
1626 | memmove(&el->l_recs[1], &el->l_recs[0], num_bytes); | |
1627 | } | |
1628 | ||
1629 | static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el, | |
1630 | struct ocfs2_extent_rec *insert_rec) | |
1631 | { | |
1632 | int i, insert_index, next_free, has_empty, num_bytes; | |
1633 | u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos); | |
1634 | struct ocfs2_extent_rec *rec; | |
1635 | ||
1636 | next_free = le16_to_cpu(el->l_next_free_rec); | |
1637 | has_empty = ocfs2_is_empty_extent(&el->l_recs[0]); | |
1638 | ||
1639 | BUG_ON(!next_free); | |
1640 | ||
1641 | /* The tree code before us didn't allow enough room in the leaf. */ | |
b1f3550f | 1642 | BUG_ON(el->l_next_free_rec == el->l_count && !has_empty); |
dcd0538f MF |
1643 | |
1644 | /* | |
1645 | * The easiest way to approach this is to just remove the | |
1646 | * empty extent and temporarily decrement next_free. | |
1647 | */ | |
1648 | if (has_empty) { | |
1649 | /* | |
1650 | * If next_free was 1 (only an empty extent), this | |
1651 | * loop won't execute, which is fine. We still want | |
1652 | * the decrement above to happen. | |
1653 | */ | |
1654 | for(i = 0; i < (next_free - 1); i++) | |
1655 | el->l_recs[i] = el->l_recs[i+1]; | |
1656 | ||
1657 | next_free--; | |
1658 | } | |
1659 | ||
1660 | /* | |
1661 | * Figure out what the new record index should be. | |
1662 | */ | |
1663 | for(i = 0; i < next_free; i++) { | |
1664 | rec = &el->l_recs[i]; | |
1665 | ||
1666 | if (insert_cpos < le32_to_cpu(rec->e_cpos)) | |
1667 | break; | |
1668 | } | |
1669 | insert_index = i; | |
1670 | ||
a09d09b8 TM |
1671 | trace_ocfs2_rotate_leaf(insert_cpos, insert_index, |
1672 | has_empty, next_free, | |
1673 | le16_to_cpu(el->l_count)); | |
dcd0538f MF |
1674 | |
1675 | BUG_ON(insert_index < 0); | |
1676 | BUG_ON(insert_index >= le16_to_cpu(el->l_count)); | |
1677 | BUG_ON(insert_index > next_free); | |
1678 | ||
1679 | /* | |
1680 | * No need to memmove if we're just adding to the tail. | |
1681 | */ | |
1682 | if (insert_index != next_free) { | |
1683 | BUG_ON(next_free >= le16_to_cpu(el->l_count)); | |
1684 | ||
1685 | num_bytes = next_free - insert_index; | |
1686 | num_bytes *= sizeof(struct ocfs2_extent_rec); | |
1687 | memmove(&el->l_recs[insert_index + 1], | |
1688 | &el->l_recs[insert_index], | |
1689 | num_bytes); | |
1690 | } | |
1691 | ||
1692 | /* | |
1693 | * Either we had an empty extent, and need to re-increment or | |
1694 | * there was no empty extent on a non full rightmost leaf node, | |
1695 | * in which case we still need to increment. | |
1696 | */ | |
1697 | next_free++; | |
1698 | el->l_next_free_rec = cpu_to_le16(next_free); | |
1699 | /* | |
1700 | * Make sure none of the math above just messed up our tree. | |
1701 | */ | |
1702 | BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count)); | |
1703 | ||
1704 | el->l_recs[insert_index] = *insert_rec; | |
1705 | ||
1706 | } | |
1707 | ||
328d5752 MF |
1708 | static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el) |
1709 | { | |
1710 | int size, num_recs = le16_to_cpu(el->l_next_free_rec); | |
1711 | ||
1712 | BUG_ON(num_recs == 0); | |
1713 | ||
1714 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { | |
1715 | num_recs--; | |
1716 | size = num_recs * sizeof(struct ocfs2_extent_rec); | |
1717 | memmove(&el->l_recs[0], &el->l_recs[1], size); | |
1718 | memset(&el->l_recs[num_recs], 0, | |
1719 | sizeof(struct ocfs2_extent_rec)); | |
1720 | el->l_next_free_rec = cpu_to_le16(num_recs); | |
1721 | } | |
1722 | } | |
1723 | ||
dcd0538f MF |
1724 | /* |
1725 | * Create an empty extent record . | |
1726 | * | |
1727 | * l_next_free_rec may be updated. | |
1728 | * | |
1729 | * If an empty extent already exists do nothing. | |
1730 | */ | |
1731 | static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el) | |
1732 | { | |
1733 | int next_free = le16_to_cpu(el->l_next_free_rec); | |
1734 | ||
e48edee2 MF |
1735 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
1736 | ||
dcd0538f MF |
1737 | if (next_free == 0) |
1738 | goto set_and_inc; | |
1739 | ||
1740 | if (ocfs2_is_empty_extent(&el->l_recs[0])) | |
1741 | return; | |
1742 | ||
1743 | mlog_bug_on_msg(el->l_count == el->l_next_free_rec, | |
1744 | "Asked to create an empty extent in a full list:\n" | |
1745 | "count = %u, tree depth = %u", | |
1746 | le16_to_cpu(el->l_count), | |
1747 | le16_to_cpu(el->l_tree_depth)); | |
1748 | ||
1749 | ocfs2_shift_records_right(el); | |
1750 | ||
1751 | set_and_inc: | |
1752 | le16_add_cpu(&el->l_next_free_rec, 1); | |
1753 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); | |
1754 | } | |
1755 | ||
1756 | /* | |
1757 | * For a rotation which involves two leaf nodes, the "root node" is | |
1758 | * the lowest level tree node which contains a path to both leafs. This | |
1759 | * resulting set of information can be used to form a complete "subtree" | |
1760 | * | |
1761 | * This function is passed two full paths from the dinode down to a | |
1762 | * pair of adjacent leaves. It's task is to figure out which path | |
1763 | * index contains the subtree root - this can be the root index itself | |
1764 | * in a worst-case rotation. | |
1765 | * | |
1766 | * The array index of the subtree root is passed back. | |
1767 | */ | |
38a04e43 TM |
1768 | int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et, |
1769 | struct ocfs2_path *left, | |
1770 | struct ocfs2_path *right) | |
dcd0538f MF |
1771 | { |
1772 | int i = 0; | |
1773 | ||
1774 | /* | |
1775 | * Check that the caller passed in two paths from the same tree. | |
1776 | */ | |
1777 | BUG_ON(path_root_bh(left) != path_root_bh(right)); | |
1778 | ||
1779 | do { | |
1780 | i++; | |
1781 | ||
1782 | /* | |
1783 | * The caller didn't pass two adjacent paths. | |
1784 | */ | |
1785 | mlog_bug_on_msg(i > left->p_tree_depth, | |
7dc02805 | 1786 | "Owner %llu, left depth %u, right depth %u\n" |
dcd0538f | 1787 | "left leaf blk %llu, right leaf blk %llu\n", |
7dc02805 JB |
1788 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
1789 | left->p_tree_depth, right->p_tree_depth, | |
dcd0538f MF |
1790 | (unsigned long long)path_leaf_bh(left)->b_blocknr, |
1791 | (unsigned long long)path_leaf_bh(right)->b_blocknr); | |
1792 | } while (left->p_node[i].bh->b_blocknr == | |
1793 | right->p_node[i].bh->b_blocknr); | |
1794 | ||
1795 | return i - 1; | |
1796 | } | |
1797 | ||
1798 | typedef void (path_insert_t)(void *, struct buffer_head *); | |
1799 | ||
1800 | /* | |
1801 | * Traverse a btree path in search of cpos, starting at root_el. | |
1802 | * | |
1803 | * This code can be called with a cpos larger than the tree, in which | |
1804 | * case it will return the rightmost path. | |
1805 | */ | |
facdb77f | 1806 | static int __ocfs2_find_path(struct ocfs2_caching_info *ci, |
dcd0538f MF |
1807 | struct ocfs2_extent_list *root_el, u32 cpos, |
1808 | path_insert_t *func, void *data) | |
1809 | { | |
1810 | int i, ret = 0; | |
1811 | u32 range; | |
1812 | u64 blkno; | |
1813 | struct buffer_head *bh = NULL; | |
1814 | struct ocfs2_extent_block *eb; | |
1815 | struct ocfs2_extent_list *el; | |
1816 | struct ocfs2_extent_rec *rec; | |
dcd0538f MF |
1817 | |
1818 | el = root_el; | |
1819 | while (el->l_tree_depth) { | |
1820 | if (le16_to_cpu(el->l_next_free_rec) == 0) { | |
facdb77f | 1821 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
7ecef14a | 1822 | "Owner %llu has empty extent list at depth %u\n", |
facdb77f | 1823 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
dcd0538f MF |
1824 | le16_to_cpu(el->l_tree_depth)); |
1825 | ret = -EROFS; | |
1826 | goto out; | |
1827 | ||
1828 | } | |
1829 | ||
1830 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) { | |
1831 | rec = &el->l_recs[i]; | |
1832 | ||
1833 | /* | |
1834 | * In the case that cpos is off the allocation | |
1835 | * tree, this should just wind up returning the | |
1836 | * rightmost record. | |
1837 | */ | |
1838 | range = le32_to_cpu(rec->e_cpos) + | |
e48edee2 | 1839 | ocfs2_rec_clusters(el, rec); |
dcd0538f MF |
1840 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) |
1841 | break; | |
1842 | } | |
1843 | ||
1844 | blkno = le64_to_cpu(el->l_recs[i].e_blkno); | |
1845 | if (blkno == 0) { | |
facdb77f | 1846 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
7ecef14a | 1847 | "Owner %llu has bad blkno in extent list at depth %u (index %d)\n", |
facdb77f | 1848 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
dcd0538f MF |
1849 | le16_to_cpu(el->l_tree_depth), i); |
1850 | ret = -EROFS; | |
1851 | goto out; | |
1852 | } | |
1853 | ||
1854 | brelse(bh); | |
1855 | bh = NULL; | |
facdb77f | 1856 | ret = ocfs2_read_extent_block(ci, blkno, &bh); |
dcd0538f MF |
1857 | if (ret) { |
1858 | mlog_errno(ret); | |
1859 | goto out; | |
1860 | } | |
1861 | ||
1862 | eb = (struct ocfs2_extent_block *) bh->b_data; | |
1863 | el = &eb->h_list; | |
dcd0538f MF |
1864 | |
1865 | if (le16_to_cpu(el->l_next_free_rec) > | |
1866 | le16_to_cpu(el->l_count)) { | |
facdb77f | 1867 | ocfs2_error(ocfs2_metadata_cache_get_super(ci), |
7ecef14a | 1868 | "Owner %llu has bad count in extent list at block %llu (next free=%u, count=%u)\n", |
facdb77f | 1869 | (unsigned long long)ocfs2_metadata_cache_owner(ci), |
dcd0538f MF |
1870 | (unsigned long long)bh->b_blocknr, |
1871 | le16_to_cpu(el->l_next_free_rec), | |
1872 | le16_to_cpu(el->l_count)); | |
1873 | ret = -EROFS; | |
1874 | goto out; | |
1875 | } | |
1876 | ||
1877 | if (func) | |
1878 | func(data, bh); | |
1879 | } | |
1880 | ||
1881 | out: | |
1882 | /* | |
1883 | * Catch any trailing bh that the loop didn't handle. | |
1884 | */ | |
1885 | brelse(bh); | |
1886 | ||
1887 | return ret; | |
1888 | } | |
1889 | ||
1890 | /* | |
1891 | * Given an initialized path (that is, it has a valid root extent | |
1892 | * list), this function will traverse the btree in search of the path | |
1893 | * which would contain cpos. | |
1894 | * | |
1895 | * The path traveled is recorded in the path structure. | |
1896 | * | |
1897 | * Note that this will not do any comparisons on leaf node extent | |
1898 | * records, so it will work fine in the case that we just added a tree | |
1899 | * branch. | |
1900 | */ | |
1901 | struct find_path_data { | |
1902 | int index; | |
1903 | struct ocfs2_path *path; | |
1904 | }; | |
1905 | static void find_path_ins(void *data, struct buffer_head *bh) | |
1906 | { | |
1907 | struct find_path_data *fp = data; | |
1908 | ||
1909 | get_bh(bh); | |
1910 | ocfs2_path_insert_eb(fp->path, fp->index, bh); | |
1911 | fp->index++; | |
1912 | } | |
e2e9f608 TM |
1913 | int ocfs2_find_path(struct ocfs2_caching_info *ci, |
1914 | struct ocfs2_path *path, u32 cpos) | |
dcd0538f MF |
1915 | { |
1916 | struct find_path_data data; | |
1917 | ||
1918 | data.index = 1; | |
1919 | data.path = path; | |
facdb77f | 1920 | return __ocfs2_find_path(ci, path_root_el(path), cpos, |
dcd0538f MF |
1921 | find_path_ins, &data); |
1922 | } | |
1923 | ||
1924 | static void find_leaf_ins(void *data, struct buffer_head *bh) | |
1925 | { | |
1926 | struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data; | |
1927 | struct ocfs2_extent_list *el = &eb->h_list; | |
1928 | struct buffer_head **ret = data; | |
1929 | ||
1930 | /* We want to retain only the leaf block. */ | |
1931 | if (le16_to_cpu(el->l_tree_depth) == 0) { | |
1932 | get_bh(bh); | |
1933 | *ret = bh; | |
1934 | } | |
1935 | } | |
1936 | /* | |
1937 | * Find the leaf block in the tree which would contain cpos. No | |
1938 | * checking of the actual leaf is done. | |
1939 | * | |
1940 | * Some paths want to call this instead of allocating a path structure | |
1941 | * and calling ocfs2_find_path(). | |
1942 | * | |
1943 | * This function doesn't handle non btree extent lists. | |
1944 | */ | |
facdb77f JB |
1945 | int ocfs2_find_leaf(struct ocfs2_caching_info *ci, |
1946 | struct ocfs2_extent_list *root_el, u32 cpos, | |
1947 | struct buffer_head **leaf_bh) | |
dcd0538f MF |
1948 | { |
1949 | int ret; | |
1950 | struct buffer_head *bh = NULL; | |
1951 | ||
facdb77f | 1952 | ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh); |
dcd0538f MF |
1953 | if (ret) { |
1954 | mlog_errno(ret); | |
1955 | goto out; | |
1956 | } | |
1957 | ||
1958 | *leaf_bh = bh; | |
1959 | out: | |
1960 | return ret; | |
1961 | } | |
1962 | ||
1963 | /* | |
1964 | * Adjust the adjacent records (left_rec, right_rec) involved in a rotation. | |
1965 | * | |
1966 | * Basically, we've moved stuff around at the bottom of the tree and | |
1967 | * we need to fix up the extent records above the changes to reflect | |
1968 | * the new changes. | |
1969 | * | |
1970 | * left_rec: the record on the left. | |
dcd0538f MF |
1971 | * right_rec: the record to the right of left_rec |
1972 | * right_child_el: is the child list pointed to by right_rec | |
1973 | * | |
1974 | * By definition, this only works on interior nodes. | |
1975 | */ | |
1976 | static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec, | |
dcd0538f MF |
1977 | struct ocfs2_extent_rec *right_rec, |
1978 | struct ocfs2_extent_list *right_child_el) | |
1979 | { | |
1980 | u32 left_clusters, right_end; | |
1981 | ||
1982 | /* | |
1983 | * Interior nodes never have holes. Their cpos is the cpos of | |
1984 | * the leftmost record in their child list. Their cluster | |
1985 | * count covers the full theoretical range of their child list | |
1986 | * - the range between their cpos and the cpos of the record | |
1987 | * immediately to their right. | |
1988 | */ | |
1989 | left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos); | |
82e12644 TM |
1990 | if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) { |
1991 | BUG_ON(right_child_el->l_tree_depth); | |
328d5752 MF |
1992 | BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1); |
1993 | left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos); | |
1994 | } | |
dcd0538f | 1995 | left_clusters -= le32_to_cpu(left_rec->e_cpos); |
e48edee2 | 1996 | left_rec->e_int_clusters = cpu_to_le32(left_clusters); |
dcd0538f MF |
1997 | |
1998 | /* | |
1999 | * Calculate the rightmost cluster count boundary before | |
e48edee2 | 2000 | * moving cpos - we will need to adjust clusters after |
dcd0538f MF |
2001 | * updating e_cpos to keep the same highest cluster count. |
2002 | */ | |
2003 | right_end = le32_to_cpu(right_rec->e_cpos); | |
e48edee2 | 2004 | right_end += le32_to_cpu(right_rec->e_int_clusters); |
dcd0538f MF |
2005 | |
2006 | right_rec->e_cpos = left_rec->e_cpos; | |
2007 | le32_add_cpu(&right_rec->e_cpos, left_clusters); | |
2008 | ||
2009 | right_end -= le32_to_cpu(right_rec->e_cpos); | |
e48edee2 | 2010 | right_rec->e_int_clusters = cpu_to_le32(right_end); |
dcd0538f MF |
2011 | } |
2012 | ||
2013 | /* | |
2014 | * Adjust the adjacent root node records involved in a | |
2015 | * rotation. left_el_blkno is passed in as a key so that we can easily | |
2016 | * find it's index in the root list. | |
2017 | */ | |
2018 | static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el, | |
2019 | struct ocfs2_extent_list *left_el, | |
2020 | struct ocfs2_extent_list *right_el, | |
2021 | u64 left_el_blkno) | |
2022 | { | |
2023 | int i; | |
2024 | ||
2025 | BUG_ON(le16_to_cpu(root_el->l_tree_depth) <= | |
2026 | le16_to_cpu(left_el->l_tree_depth)); | |
2027 | ||
2028 | for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) { | |
2029 | if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno) | |
2030 | break; | |
2031 | } | |
2032 | ||
2033 | /* | |
2034 | * The path walking code should have never returned a root and | |
2035 | * two paths which are not adjacent. | |
2036 | */ | |
2037 | BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1)); | |
2038 | ||
964f14a0 | 2039 | ocfs2_adjust_adjacent_records(&root_el->l_recs[i], |
dcd0538f MF |
2040 | &root_el->l_recs[i + 1], right_el); |
2041 | } | |
2042 | ||
2043 | /* | |
2044 | * We've changed a leaf block (in right_path) and need to reflect that | |
2045 | * change back up the subtree. | |
2046 | * | |
2047 | * This happens in multiple places: | |
2048 | * - When we've moved an extent record from the left path leaf to the right | |
2049 | * path leaf to make room for an empty extent in the left path leaf. | |
2050 | * - When our insert into the right path leaf is at the leftmost edge | |
2051 | * and requires an update of the path immediately to it's left. This | |
2052 | * can occur at the end of some types of rotation and appending inserts. | |
677b9752 TM |
2053 | * - When we've adjusted the last extent record in the left path leaf and the |
2054 | * 1st extent record in the right path leaf during cross extent block merge. | |
dcd0538f | 2055 | */ |
4619c73e | 2056 | static void ocfs2_complete_edge_insert(handle_t *handle, |
dcd0538f MF |
2057 | struct ocfs2_path *left_path, |
2058 | struct ocfs2_path *right_path, | |
2059 | int subtree_index) | |
2060 | { | |
ec20cec7 | 2061 | int i, idx; |
dcd0538f MF |
2062 | struct ocfs2_extent_list *el, *left_el, *right_el; |
2063 | struct ocfs2_extent_rec *left_rec, *right_rec; | |
2064 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; | |
2065 | ||
2066 | /* | |
2067 | * Update the counts and position values within all the | |
2068 | * interior nodes to reflect the leaf rotation we just did. | |
2069 | * | |
2070 | * The root node is handled below the loop. | |
2071 | * | |
2072 | * We begin the loop with right_el and left_el pointing to the | |
2073 | * leaf lists and work our way up. | |
2074 | * | |
2075 | * NOTE: within this loop, left_el and right_el always refer | |
2076 | * to the *child* lists. | |
2077 | */ | |
2078 | left_el = path_leaf_el(left_path); | |
2079 | right_el = path_leaf_el(right_path); | |
2080 | for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) { | |
a09d09b8 | 2081 | trace_ocfs2_complete_edge_insert(i); |
dcd0538f MF |
2082 | |
2083 | /* | |
2084 | * One nice property of knowing that all of these | |
2085 | * nodes are below the root is that we only deal with | |
2086 | * the leftmost right node record and the rightmost | |
2087 | * left node record. | |
2088 | */ | |
2089 | el = left_path->p_node[i].el; | |
2090 | idx = le16_to_cpu(left_el->l_next_free_rec) - 1; | |
2091 | left_rec = &el->l_recs[idx]; | |
2092 | ||
2093 | el = right_path->p_node[i].el; | |
2094 | right_rec = &el->l_recs[0]; | |
2095 | ||
964f14a0 | 2096 | ocfs2_adjust_adjacent_records(left_rec, right_rec, right_el); |
dcd0538f | 2097 | |
ec20cec7 JB |
2098 | ocfs2_journal_dirty(handle, left_path->p_node[i].bh); |
2099 | ocfs2_journal_dirty(handle, right_path->p_node[i].bh); | |
dcd0538f MF |
2100 | |
2101 | /* | |
2102 | * Setup our list pointers now so that the current | |
2103 | * parents become children in the next iteration. | |
2104 | */ | |
2105 | left_el = left_path->p_node[i].el; | |
2106 | right_el = right_path->p_node[i].el; | |
2107 | } | |
2108 | ||
2109 | /* | |
2110 | * At the root node, adjust the two adjacent records which | |
2111 | * begin our path to the leaves. | |
2112 | */ | |
2113 | ||
2114 | el = left_path->p_node[subtree_index].el; | |
2115 | left_el = left_path->p_node[subtree_index + 1].el; | |
2116 | right_el = right_path->p_node[subtree_index + 1].el; | |
2117 | ||
2118 | ocfs2_adjust_root_records(el, left_el, right_el, | |
2119 | left_path->p_node[subtree_index + 1].bh->b_blocknr); | |
2120 | ||
2121 | root_bh = left_path->p_node[subtree_index].bh; | |
2122 | ||
ec20cec7 | 2123 | ocfs2_journal_dirty(handle, root_bh); |
dcd0538f MF |
2124 | } |
2125 | ||
5c601aba JB |
2126 | static int ocfs2_rotate_subtree_right(handle_t *handle, |
2127 | struct ocfs2_extent_tree *et, | |
dcd0538f MF |
2128 | struct ocfs2_path *left_path, |
2129 | struct ocfs2_path *right_path, | |
2130 | int subtree_index) | |
2131 | { | |
2132 | int ret, i; | |
2133 | struct buffer_head *right_leaf_bh; | |
2134 | struct buffer_head *left_leaf_bh = NULL; | |
2135 | struct buffer_head *root_bh; | |
2136 | struct ocfs2_extent_list *right_el, *left_el; | |
2137 | struct ocfs2_extent_rec move_rec; | |
2138 | ||
2139 | left_leaf_bh = path_leaf_bh(left_path); | |
2140 | left_el = path_leaf_el(left_path); | |
2141 | ||
2142 | if (left_el->l_next_free_rec != left_el->l_count) { | |
5c601aba | 2143 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
7ecef14a | 2144 | "Inode %llu has non-full interior leaf node %llu (next free = %u)\n", |
5c601aba | 2145 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
dcd0538f MF |
2146 | (unsigned long long)left_leaf_bh->b_blocknr, |
2147 | le16_to_cpu(left_el->l_next_free_rec)); | |
2148 | return -EROFS; | |
2149 | } | |
2150 | ||
2151 | /* | |
2152 | * This extent block may already have an empty record, so we | |
2153 | * return early if so. | |
2154 | */ | |
2155 | if (ocfs2_is_empty_extent(&left_el->l_recs[0])) | |
2156 | return 0; | |
2157 | ||
2158 | root_bh = left_path->p_node[subtree_index].bh; | |
2159 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); | |
2160 | ||
5c601aba | 2161 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
13723d00 | 2162 | subtree_index); |
dcd0538f MF |
2163 | if (ret) { |
2164 | mlog_errno(ret); | |
2165 | goto out; | |
2166 | } | |
2167 | ||
2168 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { | |
5c601aba | 2169 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
13723d00 | 2170 | right_path, i); |
dcd0538f MF |
2171 | if (ret) { |
2172 | mlog_errno(ret); | |
2173 | goto out; | |
2174 | } | |
2175 | ||
5c601aba | 2176 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
13723d00 | 2177 | left_path, i); |
dcd0538f MF |
2178 | if (ret) { |
2179 | mlog_errno(ret); | |
2180 | goto out; | |
2181 | } | |
2182 | } | |
2183 | ||
2184 | right_leaf_bh = path_leaf_bh(right_path); | |
2185 | right_el = path_leaf_el(right_path); | |
2186 | ||
2187 | /* This is a code error, not a disk corruption. */ | |
2188 | mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails " | |
2189 | "because rightmost leaf block %llu is empty\n", | |
5c601aba | 2190 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
dcd0538f MF |
2191 | (unsigned long long)right_leaf_bh->b_blocknr); |
2192 | ||
2193 | ocfs2_create_empty_extent(right_el); | |
2194 | ||
ec20cec7 | 2195 | ocfs2_journal_dirty(handle, right_leaf_bh); |
dcd0538f MF |
2196 | |
2197 | /* Do the copy now. */ | |
2198 | i = le16_to_cpu(left_el->l_next_free_rec) - 1; | |
2199 | move_rec = left_el->l_recs[i]; | |
2200 | right_el->l_recs[0] = move_rec; | |
2201 | ||
2202 | /* | |
2203 | * Clear out the record we just copied and shift everything | |
2204 | * over, leaving an empty extent in the left leaf. | |
2205 | * | |
2206 | * We temporarily subtract from next_free_rec so that the | |
2207 | * shift will lose the tail record (which is now defunct). | |
2208 | */ | |
2209 | le16_add_cpu(&left_el->l_next_free_rec, -1); | |
2210 | ocfs2_shift_records_right(left_el); | |
2211 | memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); | |
2212 | le16_add_cpu(&left_el->l_next_free_rec, 1); | |
2213 | ||
ec20cec7 | 2214 | ocfs2_journal_dirty(handle, left_leaf_bh); |
dcd0538f | 2215 | |
4619c73e JB |
2216 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
2217 | subtree_index); | |
dcd0538f MF |
2218 | |
2219 | out: | |
2220 | return ret; | |
2221 | } | |
2222 | ||
2223 | /* | |
2224 | * Given a full path, determine what cpos value would return us a path | |
2225 | * containing the leaf immediately to the left of the current one. | |
2226 | * | |
2227 | * Will return zero if the path passed in is already the leftmost path. | |
2228 | */ | |
ee149a7c TY |
2229 | int ocfs2_find_cpos_for_left_leaf(struct super_block *sb, |
2230 | struct ocfs2_path *path, u32 *cpos) | |
dcd0538f MF |
2231 | { |
2232 | int i, j, ret = 0; | |
2233 | u64 blkno; | |
2234 | struct ocfs2_extent_list *el; | |
2235 | ||
e48edee2 MF |
2236 | BUG_ON(path->p_tree_depth == 0); |
2237 | ||
dcd0538f MF |
2238 | *cpos = 0; |
2239 | ||
2240 | blkno = path_leaf_bh(path)->b_blocknr; | |
2241 | ||
2242 | /* Start at the tree node just above the leaf and work our way up. */ | |
2243 | i = path->p_tree_depth - 1; | |
2244 | while (i >= 0) { | |
2245 | el = path->p_node[i].el; | |
2246 | ||
2247 | /* | |
2248 | * Find the extent record just before the one in our | |
2249 | * path. | |
2250 | */ | |
2251 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { | |
2252 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { | |
2253 | if (j == 0) { | |
2254 | if (i == 0) { | |
2255 | /* | |
2256 | * We've determined that the | |
2257 | * path specified is already | |
2258 | * the leftmost one - return a | |
2259 | * cpos of zero. | |
2260 | */ | |
2261 | goto out; | |
2262 | } | |
2263 | /* | |
2264 | * The leftmost record points to our | |
2265 | * leaf - we need to travel up the | |
2266 | * tree one level. | |
2267 | */ | |
2268 | goto next_node; | |
2269 | } | |
2270 | ||
2271 | *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos); | |
e48edee2 MF |
2272 | *cpos = *cpos + ocfs2_rec_clusters(el, |
2273 | &el->l_recs[j - 1]); | |
2274 | *cpos = *cpos - 1; | |
dcd0538f MF |
2275 | goto out; |
2276 | } | |
2277 | } | |
2278 | ||
2279 | /* | |
2280 | * If we got here, we never found a valid node where | |
2281 | * the tree indicated one should be. | |
2282 | */ | |
7ecef14a | 2283 | ocfs2_error(sb, "Invalid extent tree at extent block %llu\n", |
dcd0538f MF |
2284 | (unsigned long long)blkno); |
2285 | ret = -EROFS; | |
2286 | goto out; | |
2287 | ||
2288 | next_node: | |
2289 | blkno = path->p_node[i].bh->b_blocknr; | |
2290 | i--; | |
2291 | } | |
2292 | ||
2293 | out: | |
2294 | return ret; | |
2295 | } | |
2296 | ||
328d5752 MF |
2297 | /* |
2298 | * Extend the transaction by enough credits to complete the rotation, | |
2299 | * and still leave at least the original number of credits allocated | |
2300 | * to this transaction. | |
2301 | */ | |
dcd0538f | 2302 | static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth, |
328d5752 | 2303 | int op_credits, |
dcd0538f MF |
2304 | struct ocfs2_path *path) |
2305 | { | |
c901fb00 | 2306 | int ret = 0; |
328d5752 | 2307 | int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits; |
dcd0538f | 2308 | |
c901fb00 | 2309 | if (handle->h_buffer_credits < credits) |
c18b812d TM |
2310 | ret = ocfs2_extend_trans(handle, |
2311 | credits - handle->h_buffer_credits); | |
dcd0538f | 2312 | |
c901fb00 | 2313 | return ret; |
dcd0538f MF |
2314 | } |
2315 | ||
2316 | /* | |
2317 | * Trap the case where we're inserting into the theoretical range past | |
2318 | * the _actual_ left leaf range. Otherwise, we'll rotate a record | |
2319 | * whose cpos is less than ours into the right leaf. | |
2320 | * | |
2321 | * It's only necessary to look at the rightmost record of the left | |
2322 | * leaf because the logic that calls us should ensure that the | |
2323 | * theoretical ranges in the path components above the leaves are | |
2324 | * correct. | |
2325 | */ | |
2326 | static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path, | |
2327 | u32 insert_cpos) | |
2328 | { | |
2329 | struct ocfs2_extent_list *left_el; | |
2330 | struct ocfs2_extent_rec *rec; | |
2331 | int next_free; | |
2332 | ||
2333 | left_el = path_leaf_el(left_path); | |
2334 | next_free = le16_to_cpu(left_el->l_next_free_rec); | |
2335 | rec = &left_el->l_recs[next_free - 1]; | |
2336 | ||
2337 | if (insert_cpos > le32_to_cpu(rec->e_cpos)) | |
2338 | return 1; | |
2339 | return 0; | |
2340 | } | |
2341 | ||
328d5752 MF |
2342 | static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos) |
2343 | { | |
2344 | int next_free = le16_to_cpu(el->l_next_free_rec); | |
2345 | unsigned int range; | |
2346 | struct ocfs2_extent_rec *rec; | |
2347 | ||
2348 | if (next_free == 0) | |
2349 | return 0; | |
2350 | ||
2351 | rec = &el->l_recs[0]; | |
2352 | if (ocfs2_is_empty_extent(rec)) { | |
2353 | /* Empty list. */ | |
2354 | if (next_free == 1) | |
2355 | return 0; | |
2356 | rec = &el->l_recs[1]; | |
2357 | } | |
2358 | ||
2359 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); | |
2360 | if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range) | |
2361 | return 1; | |
2362 | return 0; | |
2363 | } | |
2364 | ||
dcd0538f MF |
2365 | /* |
2366 | * Rotate all the records in a btree right one record, starting at insert_cpos. | |
2367 | * | |
2368 | * The path to the rightmost leaf should be passed in. | |
2369 | * | |
2370 | * The array is assumed to be large enough to hold an entire path (tree depth). | |
2371 | * | |
af901ca1 | 2372 | * Upon successful return from this function: |
dcd0538f MF |
2373 | * |
2374 | * - The 'right_path' array will contain a path to the leaf block | |
2375 | * whose range contains e_cpos. | |
2376 | * - That leaf block will have a single empty extent in list index 0. | |
2377 | * - In the case that the rotation requires a post-insert update, | |
2378 | * *ret_left_path will contain a valid path which can be passed to | |
2379 | * ocfs2_insert_path(). | |
2380 | */ | |
1bbf0b8d | 2381 | static int ocfs2_rotate_tree_right(handle_t *handle, |
5c601aba | 2382 | struct ocfs2_extent_tree *et, |
328d5752 | 2383 | enum ocfs2_split_type split, |
dcd0538f MF |
2384 | u32 insert_cpos, |
2385 | struct ocfs2_path *right_path, | |
2386 | struct ocfs2_path **ret_left_path) | |
2387 | { | |
328d5752 | 2388 | int ret, start, orig_credits = handle->h_buffer_credits; |
dcd0538f MF |
2389 | u32 cpos; |
2390 | struct ocfs2_path *left_path = NULL; | |
5c601aba | 2391 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
dcd0538f MF |
2392 | |
2393 | *ret_left_path = NULL; | |
2394 | ||
ffdd7a54 | 2395 | left_path = ocfs2_new_path_from_path(right_path); |
dcd0538f MF |
2396 | if (!left_path) { |
2397 | ret = -ENOMEM; | |
2398 | mlog_errno(ret); | |
2399 | goto out; | |
2400 | } | |
2401 | ||
5c601aba | 2402 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
dcd0538f MF |
2403 | if (ret) { |
2404 | mlog_errno(ret); | |
2405 | goto out; | |
2406 | } | |
2407 | ||
a09d09b8 TM |
2408 | trace_ocfs2_rotate_tree_right( |
2409 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), | |
2410 | insert_cpos, cpos); | |
dcd0538f MF |
2411 | |
2412 | /* | |
2413 | * What we want to do here is: | |
2414 | * | |
2415 | * 1) Start with the rightmost path. | |
2416 | * | |
2417 | * 2) Determine a path to the leaf block directly to the left | |
2418 | * of that leaf. | |
2419 | * | |
2420 | * 3) Determine the 'subtree root' - the lowest level tree node | |
2421 | * which contains a path to both leaves. | |
2422 | * | |
2423 | * 4) Rotate the subtree. | |
2424 | * | |
2425 | * 5) Find the next subtree by considering the left path to be | |
2426 | * the new right path. | |
2427 | * | |
2428 | * The check at the top of this while loop also accepts | |
2429 | * insert_cpos == cpos because cpos is only a _theoretical_ | |
2430 | * value to get us the left path - insert_cpos might very well | |
2431 | * be filling that hole. | |
2432 | * | |
2433 | * Stop at a cpos of '0' because we either started at the | |
2434 | * leftmost branch (i.e., a tree with one branch and a | |
2435 | * rotation inside of it), or we've gone as far as we can in | |
2436 | * rotating subtrees. | |
2437 | */ | |
2438 | while (cpos && insert_cpos <= cpos) { | |
a09d09b8 TM |
2439 | trace_ocfs2_rotate_tree_right( |
2440 | (unsigned long long) | |
2441 | ocfs2_metadata_cache_owner(et->et_ci), | |
2442 | insert_cpos, cpos); | |
dcd0538f | 2443 | |
5c601aba | 2444 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
dcd0538f MF |
2445 | if (ret) { |
2446 | mlog_errno(ret); | |
2447 | goto out; | |
2448 | } | |
2449 | ||
2450 | mlog_bug_on_msg(path_leaf_bh(left_path) == | |
2451 | path_leaf_bh(right_path), | |
5c601aba | 2452 | "Owner %llu: error during insert of %u " |
dcd0538f MF |
2453 | "(left path cpos %u) results in two identical " |
2454 | "paths ending at %llu\n", | |
5c601aba JB |
2455 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
2456 | insert_cpos, cpos, | |
dcd0538f MF |
2457 | (unsigned long long) |
2458 | path_leaf_bh(left_path)->b_blocknr); | |
2459 | ||
328d5752 MF |
2460 | if (split == SPLIT_NONE && |
2461 | ocfs2_rotate_requires_path_adjustment(left_path, | |
dcd0538f | 2462 | insert_cpos)) { |
dcd0538f MF |
2463 | |
2464 | /* | |
2465 | * We've rotated the tree as much as we | |
2466 | * should. The rest is up to | |
2467 | * ocfs2_insert_path() to complete, after the | |
2468 | * record insertion. We indicate this | |
2469 | * situation by returning the left path. | |
2470 | * | |
2471 | * The reason we don't adjust the records here | |
2472 | * before the record insert is that an error | |
2473 | * later might break the rule where a parent | |
2474 | * record e_cpos will reflect the actual | |
2475 | * e_cpos of the 1st nonempty record of the | |
2476 | * child list. | |
2477 | */ | |
2478 | *ret_left_path = left_path; | |
2479 | goto out_ret_path; | |
2480 | } | |
2481 | ||
7dc02805 | 2482 | start = ocfs2_find_subtree_root(et, left_path, right_path); |
dcd0538f | 2483 | |
a09d09b8 TM |
2484 | trace_ocfs2_rotate_subtree(start, |
2485 | (unsigned long long) | |
2486 | right_path->p_node[start].bh->b_blocknr, | |
2487 | right_path->p_tree_depth); | |
dcd0538f MF |
2488 | |
2489 | ret = ocfs2_extend_rotate_transaction(handle, start, | |
328d5752 | 2490 | orig_credits, right_path); |
dcd0538f MF |
2491 | if (ret) { |
2492 | mlog_errno(ret); | |
2493 | goto out; | |
2494 | } | |
2495 | ||
5c601aba | 2496 | ret = ocfs2_rotate_subtree_right(handle, et, left_path, |
dcd0538f MF |
2497 | right_path, start); |
2498 | if (ret) { | |
2499 | mlog_errno(ret); | |
2500 | goto out; | |
2501 | } | |
2502 | ||
328d5752 MF |
2503 | if (split != SPLIT_NONE && |
2504 | ocfs2_leftmost_rec_contains(path_leaf_el(right_path), | |
2505 | insert_cpos)) { | |
2506 | /* | |
2507 | * A rotate moves the rightmost left leaf | |
2508 | * record over to the leftmost right leaf | |
2509 | * slot. If we're doing an extent split | |
2510 | * instead of a real insert, then we have to | |
2511 | * check that the extent to be split wasn't | |
2512 | * just moved over. If it was, then we can | |
2513 | * exit here, passing left_path back - | |
2514 | * ocfs2_split_extent() is smart enough to | |
2515 | * search both leaves. | |
2516 | */ | |
2517 | *ret_left_path = left_path; | |
2518 | goto out_ret_path; | |
2519 | } | |
2520 | ||
dcd0538f MF |
2521 | /* |
2522 | * There is no need to re-read the next right path | |
2523 | * as we know that it'll be our current left | |
2524 | * path. Optimize by copying values instead. | |
2525 | */ | |
2526 | ocfs2_mv_path(right_path, left_path); | |
2527 | ||
5c601aba | 2528 | ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos); |
dcd0538f MF |
2529 | if (ret) { |
2530 | mlog_errno(ret); | |
2531 | goto out; | |
2532 | } | |
2533 | } | |
2534 | ||
2535 | out: | |
2536 | ocfs2_free_path(left_path); | |
2537 | ||
2538 | out_ret_path: | |
2539 | return ret; | |
2540 | } | |
2541 | ||
09106bae JB |
2542 | static int ocfs2_update_edge_lengths(handle_t *handle, |
2543 | struct ocfs2_extent_tree *et, | |
964f14a0 | 2544 | struct ocfs2_path *path) |
dcd0538f | 2545 | { |
3c5e1068 | 2546 | int i, idx, ret; |
dcd0538f | 2547 | struct ocfs2_extent_rec *rec; |
328d5752 MF |
2548 | struct ocfs2_extent_list *el; |
2549 | struct ocfs2_extent_block *eb; | |
2550 | u32 range; | |
dcd0538f | 2551 | |
09106bae | 2552 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
3c5e1068 TM |
2553 | if (ret) { |
2554 | mlog_errno(ret); | |
2555 | goto out; | |
2556 | } | |
2557 | ||
328d5752 MF |
2558 | /* Path should always be rightmost. */ |
2559 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; | |
2560 | BUG_ON(eb->h_next_leaf_blk != 0ULL); | |
dcd0538f | 2561 | |
328d5752 MF |
2562 | el = &eb->h_list; |
2563 | BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0); | |
2564 | idx = le16_to_cpu(el->l_next_free_rec) - 1; | |
2565 | rec = &el->l_recs[idx]; | |
2566 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); | |
dcd0538f | 2567 | |
328d5752 MF |
2568 | for (i = 0; i < path->p_tree_depth; i++) { |
2569 | el = path->p_node[i].el; | |
2570 | idx = le16_to_cpu(el->l_next_free_rec) - 1; | |
2571 | rec = &el->l_recs[idx]; | |
dcd0538f | 2572 | |
328d5752 MF |
2573 | rec->e_int_clusters = cpu_to_le32(range); |
2574 | le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos)); | |
dcd0538f | 2575 | |
328d5752 | 2576 | ocfs2_journal_dirty(handle, path->p_node[i].bh); |
dcd0538f | 2577 | } |
3c5e1068 TM |
2578 | out: |
2579 | return ret; | |
dcd0538f MF |
2580 | } |
2581 | ||
6641b0ce JB |
2582 | static void ocfs2_unlink_path(handle_t *handle, |
2583 | struct ocfs2_extent_tree *et, | |
328d5752 MF |
2584 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
2585 | struct ocfs2_path *path, int unlink_start) | |
dcd0538f | 2586 | { |
328d5752 MF |
2587 | int ret, i; |
2588 | struct ocfs2_extent_block *eb; | |
2589 | struct ocfs2_extent_list *el; | |
2590 | struct buffer_head *bh; | |
2591 | ||
2592 | for(i = unlink_start; i < path_num_items(path); i++) { | |
2593 | bh = path->p_node[i].bh; | |
2594 | ||
2595 | eb = (struct ocfs2_extent_block *)bh->b_data; | |
2596 | /* | |
2597 | * Not all nodes might have had their final count | |
2598 | * decremented by the caller - handle this here. | |
2599 | */ | |
2600 | el = &eb->h_list; | |
2601 | if (le16_to_cpu(el->l_next_free_rec) > 1) { | |
2602 | mlog(ML_ERROR, | |
2603 | "Inode %llu, attempted to remove extent block " | |
2604 | "%llu with %u records\n", | |
6641b0ce | 2605 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
328d5752 MF |
2606 | (unsigned long long)le64_to_cpu(eb->h_blkno), |
2607 | le16_to_cpu(el->l_next_free_rec)); | |
2608 | ||
2609 | ocfs2_journal_dirty(handle, bh); | |
6641b0ce | 2610 | ocfs2_remove_from_cache(et->et_ci, bh); |
328d5752 MF |
2611 | continue; |
2612 | } | |
2613 | ||
2614 | el->l_next_free_rec = 0; | |
2615 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); | |
2616 | ||
2617 | ocfs2_journal_dirty(handle, bh); | |
2618 | ||
2619 | ret = ocfs2_cache_extent_block_free(dealloc, eb); | |
2620 | if (ret) | |
2621 | mlog_errno(ret); | |
2622 | ||
6641b0ce | 2623 | ocfs2_remove_from_cache(et->et_ci, bh); |
328d5752 | 2624 | } |
dcd0538f MF |
2625 | } |
2626 | ||
6641b0ce JB |
2627 | static void ocfs2_unlink_subtree(handle_t *handle, |
2628 | struct ocfs2_extent_tree *et, | |
328d5752 MF |
2629 | struct ocfs2_path *left_path, |
2630 | struct ocfs2_path *right_path, | |
2631 | int subtree_index, | |
2632 | struct ocfs2_cached_dealloc_ctxt *dealloc) | |
dcd0538f | 2633 | { |
328d5752 MF |
2634 | int i; |
2635 | struct buffer_head *root_bh = left_path->p_node[subtree_index].bh; | |
2636 | struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el; | |
328d5752 | 2637 | struct ocfs2_extent_block *eb; |
dcd0538f | 2638 | |
328d5752 | 2639 | eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data; |
e48edee2 | 2640 | |
328d5752 MF |
2641 | for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++) |
2642 | if (root_el->l_recs[i].e_blkno == eb->h_blkno) | |
2643 | break; | |
dcd0538f | 2644 | |
328d5752 | 2645 | BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec)); |
dcd0538f | 2646 | |
328d5752 MF |
2647 | memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec)); |
2648 | le16_add_cpu(&root_el->l_next_free_rec, -1); | |
2649 | ||
2650 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; | |
2651 | eb->h_next_leaf_blk = 0; | |
2652 | ||
2653 | ocfs2_journal_dirty(handle, root_bh); | |
2654 | ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); | |
2655 | ||
6641b0ce | 2656 | ocfs2_unlink_path(handle, et, dealloc, right_path, |
328d5752 MF |
2657 | subtree_index + 1); |
2658 | } | |
2659 | ||
1e2dd63f JB |
2660 | static int ocfs2_rotate_subtree_left(handle_t *handle, |
2661 | struct ocfs2_extent_tree *et, | |
328d5752 MF |
2662 | struct ocfs2_path *left_path, |
2663 | struct ocfs2_path *right_path, | |
2664 | int subtree_index, | |
2665 | struct ocfs2_cached_dealloc_ctxt *dealloc, | |
1e2dd63f | 2666 | int *deleted) |
328d5752 MF |
2667 | { |
2668 | int ret, i, del_right_subtree = 0, right_has_empty = 0; | |
e7d4cb6b | 2669 | struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path); |
328d5752 MF |
2670 | struct ocfs2_extent_list *right_leaf_el, *left_leaf_el; |
2671 | struct ocfs2_extent_block *eb; | |
2672 | ||
2673 | *deleted = 0; | |
2674 | ||
2675 | right_leaf_el = path_leaf_el(right_path); | |
2676 | left_leaf_el = path_leaf_el(left_path); | |
2677 | root_bh = left_path->p_node[subtree_index].bh; | |
2678 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); | |
2679 | ||
2680 | if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0])) | |
2681 | return 0; | |
dcd0538f | 2682 | |
328d5752 MF |
2683 | eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data; |
2684 | if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) { | |
dcd0538f | 2685 | /* |
328d5752 MF |
2686 | * It's legal for us to proceed if the right leaf is |
2687 | * the rightmost one and it has an empty extent. There | |
2688 | * are two cases to handle - whether the leaf will be | |
2689 | * empty after removal or not. If the leaf isn't empty | |
2690 | * then just remove the empty extent up front. The | |
2691 | * next block will handle empty leaves by flagging | |
2692 | * them for unlink. | |
2693 | * | |
2694 | * Non rightmost leaves will throw -EAGAIN and the | |
2695 | * caller can manually move the subtree and retry. | |
dcd0538f | 2696 | */ |
dcd0538f | 2697 | |
328d5752 MF |
2698 | if (eb->h_next_leaf_blk != 0ULL) |
2699 | return -EAGAIN; | |
2700 | ||
2701 | if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) { | |
1e2dd63f | 2702 | ret = ocfs2_journal_access_eb(handle, et->et_ci, |
13723d00 JB |
2703 | path_leaf_bh(right_path), |
2704 | OCFS2_JOURNAL_ACCESS_WRITE); | |
dcd0538f MF |
2705 | if (ret) { |
2706 | mlog_errno(ret); | |
2707 | goto out; | |
2708 | } | |
2709 | ||
328d5752 MF |
2710 | ocfs2_remove_empty_extent(right_leaf_el); |
2711 | } else | |
2712 | right_has_empty = 1; | |
dcd0538f MF |
2713 | } |
2714 | ||
328d5752 MF |
2715 | if (eb->h_next_leaf_blk == 0ULL && |
2716 | le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) { | |
2717 | /* | |
2718 | * We have to update i_last_eb_blk during the meta | |
2719 | * data delete. | |
2720 | */ | |
d9a0a1f8 | 2721 | ret = ocfs2_et_root_journal_access(handle, et, |
13723d00 | 2722 | OCFS2_JOURNAL_ACCESS_WRITE); |
328d5752 MF |
2723 | if (ret) { |
2724 | mlog_errno(ret); | |
2725 | goto out; | |
2726 | } | |
2727 | ||
2728 | del_right_subtree = 1; | |
2729 | } | |
2730 | ||
2731 | /* | |
2732 | * Getting here with an empty extent in the right path implies | |
2733 | * that it's the rightmost path and will be deleted. | |
2734 | */ | |
2735 | BUG_ON(right_has_empty && !del_right_subtree); | |
2736 | ||
1e2dd63f | 2737 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
13723d00 | 2738 | subtree_index); |
328d5752 MF |
2739 | if (ret) { |
2740 | mlog_errno(ret); | |
2741 | goto out; | |
2742 | } | |
2743 | ||
2744 | for(i = subtree_index + 1; i < path_num_items(right_path); i++) { | |
1e2dd63f | 2745 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
13723d00 | 2746 | right_path, i); |
328d5752 MF |
2747 | if (ret) { |
2748 | mlog_errno(ret); | |
2749 | goto out; | |
2750 | } | |
2751 | ||
1e2dd63f | 2752 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
13723d00 | 2753 | left_path, i); |
328d5752 MF |
2754 | if (ret) { |
2755 | mlog_errno(ret); | |
2756 | goto out; | |
2757 | } | |
2758 | } | |
2759 | ||
2760 | if (!right_has_empty) { | |
2761 | /* | |
2762 | * Only do this if we're moving a real | |
2763 | * record. Otherwise, the action is delayed until | |
2764 | * after removal of the right path in which case we | |
2765 | * can do a simple shift to remove the empty extent. | |
2766 | */ | |
2767 | ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]); | |
2768 | memset(&right_leaf_el->l_recs[0], 0, | |
2769 | sizeof(struct ocfs2_extent_rec)); | |
2770 | } | |
2771 | if (eb->h_next_leaf_blk == 0ULL) { | |
2772 | /* | |
2773 | * Move recs over to get rid of empty extent, decrease | |
2774 | * next_free. This is allowed to remove the last | |
2775 | * extent in our leaf (setting l_next_free_rec to | |
2776 | * zero) - the delete code below won't care. | |
2777 | */ | |
2778 | ocfs2_remove_empty_extent(right_leaf_el); | |
2779 | } | |
2780 | ||
ec20cec7 JB |
2781 | ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
2782 | ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); | |
328d5752 MF |
2783 | |
2784 | if (del_right_subtree) { | |
6641b0ce | 2785 | ocfs2_unlink_subtree(handle, et, left_path, right_path, |
328d5752 | 2786 | subtree_index, dealloc); |
964f14a0 | 2787 | ret = ocfs2_update_edge_lengths(handle, et, left_path); |
3c5e1068 TM |
2788 | if (ret) { |
2789 | mlog_errno(ret); | |
2790 | goto out; | |
2791 | } | |
328d5752 MF |
2792 | |
2793 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; | |
35dc0aa3 | 2794 | ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno)); |
328d5752 MF |
2795 | |
2796 | /* | |
2797 | * Removal of the extent in the left leaf was skipped | |
2798 | * above so we could delete the right path | |
2799 | * 1st. | |
2800 | */ | |
2801 | if (right_has_empty) | |
2802 | ocfs2_remove_empty_extent(left_leaf_el); | |
2803 | ||
ec20cec7 | 2804 | ocfs2_journal_dirty(handle, et_root_bh); |
328d5752 MF |
2805 | |
2806 | *deleted = 1; | |
2807 | } else | |
4619c73e | 2808 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
328d5752 MF |
2809 | subtree_index); |
2810 | ||
2811 | out: | |
2812 | return ret; | |
2813 | } | |
2814 | ||
2815 | /* | |
2816 | * Given a full path, determine what cpos value would return us a path | |
2817 | * containing the leaf immediately to the right of the current one. | |
2818 | * | |
2819 | * Will return zero if the path passed in is already the rightmost path. | |
2820 | * | |
2821 | * This looks similar, but is subtly different to | |
2822 | * ocfs2_find_cpos_for_left_leaf(). | |
2823 | */ | |
38a04e43 TM |
2824 | int ocfs2_find_cpos_for_right_leaf(struct super_block *sb, |
2825 | struct ocfs2_path *path, u32 *cpos) | |
328d5752 MF |
2826 | { |
2827 | int i, j, ret = 0; | |
2828 | u64 blkno; | |
2829 | struct ocfs2_extent_list *el; | |
2830 | ||
2831 | *cpos = 0; | |
2832 | ||
2833 | if (path->p_tree_depth == 0) | |
2834 | return 0; | |
2835 | ||
2836 | blkno = path_leaf_bh(path)->b_blocknr; | |
2837 | ||
2838 | /* Start at the tree node just above the leaf and work our way up. */ | |
2839 | i = path->p_tree_depth - 1; | |
2840 | while (i >= 0) { | |
2841 | int next_free; | |
2842 | ||
2843 | el = path->p_node[i].el; | |
2844 | ||
2845 | /* | |
2846 | * Find the extent record just after the one in our | |
2847 | * path. | |
2848 | */ | |
2849 | next_free = le16_to_cpu(el->l_next_free_rec); | |
2850 | for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) { | |
2851 | if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) { | |
2852 | if (j == (next_free - 1)) { | |
2853 | if (i == 0) { | |
2854 | /* | |
2855 | * We've determined that the | |
2856 | * path specified is already | |
2857 | * the rightmost one - return a | |
2858 | * cpos of zero. | |
2859 | */ | |
2860 | goto out; | |
2861 | } | |
2862 | /* | |
2863 | * The rightmost record points to our | |
2864 | * leaf - we need to travel up the | |
2865 | * tree one level. | |
2866 | */ | |
2867 | goto next_node; | |
2868 | } | |
2869 | ||
2870 | *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos); | |
2871 | goto out; | |
2872 | } | |
2873 | } | |
2874 | ||
2875 | /* | |
2876 | * If we got here, we never found a valid node where | |
2877 | * the tree indicated one should be. | |
2878 | */ | |
7ecef14a | 2879 | ocfs2_error(sb, "Invalid extent tree at extent block %llu\n", |
328d5752 MF |
2880 | (unsigned long long)blkno); |
2881 | ret = -EROFS; | |
2882 | goto out; | |
2883 | ||
2884 | next_node: | |
2885 | blkno = path->p_node[i].bh->b_blocknr; | |
2886 | i--; | |
2887 | } | |
2888 | ||
2889 | out: | |
2890 | return ret; | |
2891 | } | |
2892 | ||
70f18c08 JB |
2893 | static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle, |
2894 | struct ocfs2_extent_tree *et, | |
13723d00 | 2895 | struct ocfs2_path *path) |
328d5752 MF |
2896 | { |
2897 | int ret; | |
13723d00 JB |
2898 | struct buffer_head *bh = path_leaf_bh(path); |
2899 | struct ocfs2_extent_list *el = path_leaf_el(path); | |
328d5752 MF |
2900 | |
2901 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) | |
2902 | return 0; | |
2903 | ||
70f18c08 | 2904 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path, |
13723d00 | 2905 | path_num_items(path) - 1); |
328d5752 MF |
2906 | if (ret) { |
2907 | mlog_errno(ret); | |
2908 | goto out; | |
2909 | } | |
2910 | ||
2911 | ocfs2_remove_empty_extent(el); | |
ec20cec7 | 2912 | ocfs2_journal_dirty(handle, bh); |
328d5752 MF |
2913 | |
2914 | out: | |
2915 | return ret; | |
2916 | } | |
2917 | ||
e46f74dc JB |
2918 | static int __ocfs2_rotate_tree_left(handle_t *handle, |
2919 | struct ocfs2_extent_tree *et, | |
2920 | int orig_credits, | |
328d5752 MF |
2921 | struct ocfs2_path *path, |
2922 | struct ocfs2_cached_dealloc_ctxt *dealloc, | |
e46f74dc | 2923 | struct ocfs2_path **empty_extent_path) |
328d5752 MF |
2924 | { |
2925 | int ret, subtree_root, deleted; | |
2926 | u32 right_cpos; | |
2927 | struct ocfs2_path *left_path = NULL; | |
2928 | struct ocfs2_path *right_path = NULL; | |
e46f74dc | 2929 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
328d5752 | 2930 | |
099768b0 X |
2931 | if (!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0]))) |
2932 | return 0; | |
328d5752 MF |
2933 | |
2934 | *empty_extent_path = NULL; | |
2935 | ||
e46f74dc | 2936 | ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos); |
328d5752 MF |
2937 | if (ret) { |
2938 | mlog_errno(ret); | |
2939 | goto out; | |
2940 | } | |
2941 | ||
ffdd7a54 | 2942 | left_path = ocfs2_new_path_from_path(path); |
328d5752 MF |
2943 | if (!left_path) { |
2944 | ret = -ENOMEM; | |
2945 | mlog_errno(ret); | |
2946 | goto out; | |
2947 | } | |
2948 | ||
2949 | ocfs2_cp_path(left_path, path); | |
2950 | ||
ffdd7a54 | 2951 | right_path = ocfs2_new_path_from_path(path); |
328d5752 MF |
2952 | if (!right_path) { |
2953 | ret = -ENOMEM; | |
2954 | mlog_errno(ret); | |
2955 | goto out; | |
2956 | } | |
2957 | ||
2958 | while (right_cpos) { | |
facdb77f | 2959 | ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
328d5752 MF |
2960 | if (ret) { |
2961 | mlog_errno(ret); | |
2962 | goto out; | |
2963 | } | |
2964 | ||
7dc02805 | 2965 | subtree_root = ocfs2_find_subtree_root(et, left_path, |
328d5752 MF |
2966 | right_path); |
2967 | ||
a09d09b8 | 2968 | trace_ocfs2_rotate_subtree(subtree_root, |
328d5752 MF |
2969 | (unsigned long long) |
2970 | right_path->p_node[subtree_root].bh->b_blocknr, | |
2971 | right_path->p_tree_depth); | |
2972 | ||
17215989 | 2973 | ret = ocfs2_extend_rotate_transaction(handle, 0, |
328d5752 MF |
2974 | orig_credits, left_path); |
2975 | if (ret) { | |
2976 | mlog_errno(ret); | |
2977 | goto out; | |
2978 | } | |
2979 | ||
e8aed345 MF |
2980 | /* |
2981 | * Caller might still want to make changes to the | |
2982 | * tree root, so re-add it to the journal here. | |
2983 | */ | |
e46f74dc | 2984 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
13723d00 | 2985 | left_path, 0); |
e8aed345 MF |
2986 | if (ret) { |
2987 | mlog_errno(ret); | |
2988 | goto out; | |
2989 | } | |
2990 | ||
1e2dd63f | 2991 | ret = ocfs2_rotate_subtree_left(handle, et, left_path, |
328d5752 | 2992 | right_path, subtree_root, |
1e2dd63f | 2993 | dealloc, &deleted); |
328d5752 MF |
2994 | if (ret == -EAGAIN) { |
2995 | /* | |
2996 | * The rotation has to temporarily stop due to | |
2997 | * the right subtree having an empty | |
2998 | * extent. Pass it back to the caller for a | |
2999 | * fixup. | |
3000 | */ | |
3001 | *empty_extent_path = right_path; | |
3002 | right_path = NULL; | |
3003 | goto out; | |
3004 | } | |
3005 | if (ret) { | |
3006 | mlog_errno(ret); | |
3007 | goto out; | |
3008 | } | |
3009 | ||
3010 | /* | |
3011 | * The subtree rotate might have removed records on | |
3012 | * the rightmost edge. If so, then rotation is | |
3013 | * complete. | |
3014 | */ | |
3015 | if (deleted) | |
3016 | break; | |
3017 | ||
3018 | ocfs2_mv_path(left_path, right_path); | |
3019 | ||
e46f74dc | 3020 | ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, |
328d5752 MF |
3021 | &right_cpos); |
3022 | if (ret) { | |
3023 | mlog_errno(ret); | |
3024 | goto out; | |
3025 | } | |
3026 | } | |
3027 | ||
3028 | out: | |
3029 | ocfs2_free_path(right_path); | |
3030 | ocfs2_free_path(left_path); | |
3031 | ||
3032 | return ret; | |
3033 | } | |
3034 | ||
70f18c08 JB |
3035 | static int ocfs2_remove_rightmost_path(handle_t *handle, |
3036 | struct ocfs2_extent_tree *et, | |
e7d4cb6b | 3037 | struct ocfs2_path *path, |
70f18c08 | 3038 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
328d5752 MF |
3039 | { |
3040 | int ret, subtree_index; | |
3041 | u32 cpos; | |
3042 | struct ocfs2_path *left_path = NULL; | |
328d5752 MF |
3043 | struct ocfs2_extent_block *eb; |
3044 | struct ocfs2_extent_list *el; | |
3045 | ||
6136ca5f | 3046 | ret = ocfs2_et_sanity_check(et); |
e7d4cb6b TM |
3047 | if (ret) |
3048 | goto out; | |
328d5752 | 3049 | |
d9a0a1f8 | 3050 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
328d5752 MF |
3051 | if (ret) { |
3052 | mlog_errno(ret); | |
3053 | goto out; | |
3054 | } | |
3055 | ||
3d03a305 JB |
3056 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
3057 | path, &cpos); | |
328d5752 MF |
3058 | if (ret) { |
3059 | mlog_errno(ret); | |
3060 | goto out; | |
3061 | } | |
3062 | ||
3063 | if (cpos) { | |
3064 | /* | |
3065 | * We have a path to the left of this one - it needs | |
3066 | * an update too. | |
3067 | */ | |
ffdd7a54 | 3068 | left_path = ocfs2_new_path_from_path(path); |
328d5752 MF |
3069 | if (!left_path) { |
3070 | ret = -ENOMEM; | |
3071 | mlog_errno(ret); | |
3072 | goto out; | |
3073 | } | |
3074 | ||
facdb77f | 3075 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
328d5752 MF |
3076 | if (ret) { |
3077 | mlog_errno(ret); | |
3078 | goto out; | |
3079 | } | |
3080 | ||
d9a0a1f8 | 3081 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
328d5752 MF |
3082 | if (ret) { |
3083 | mlog_errno(ret); | |
3084 | goto out; | |
3085 | } | |
3086 | ||
7dc02805 | 3087 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
328d5752 | 3088 | |
6641b0ce | 3089 | ocfs2_unlink_subtree(handle, et, left_path, path, |
328d5752 | 3090 | subtree_index, dealloc); |
964f14a0 | 3091 | ret = ocfs2_update_edge_lengths(handle, et, left_path); |
3c5e1068 TM |
3092 | if (ret) { |
3093 | mlog_errno(ret); | |
3094 | goto out; | |
3095 | } | |
328d5752 MF |
3096 | |
3097 | eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data; | |
35dc0aa3 | 3098 | ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno)); |
328d5752 MF |
3099 | } else { |
3100 | /* | |
3101 | * 'path' is also the leftmost path which | |
3102 | * means it must be the only one. This gets | |
3103 | * handled differently because we want to | |
70f18c08 | 3104 | * revert the root back to having extents |
328d5752 MF |
3105 | * in-line. |
3106 | */ | |
6641b0ce | 3107 | ocfs2_unlink_path(handle, et, dealloc, path, 1); |
328d5752 | 3108 | |
ce1d9ea6 | 3109 | el = et->et_root_el; |
328d5752 MF |
3110 | el->l_tree_depth = 0; |
3111 | el->l_next_free_rec = 0; | |
3112 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); | |
3113 | ||
35dc0aa3 | 3114 | ocfs2_et_set_last_eb_blk(et, 0); |
328d5752 MF |
3115 | } |
3116 | ||
3117 | ocfs2_journal_dirty(handle, path_root_bh(path)); | |
3118 | ||
3119 | out: | |
3120 | ocfs2_free_path(left_path); | |
3121 | return ret; | |
3122 | } | |
3123 | ||
d0c97d52 X |
3124 | static int ocfs2_remove_rightmost_empty_extent(struct ocfs2_super *osb, |
3125 | struct ocfs2_extent_tree *et, | |
3126 | struct ocfs2_path *path, | |
3127 | struct ocfs2_cached_dealloc_ctxt *dealloc) | |
3128 | { | |
3129 | handle_t *handle; | |
3130 | int ret; | |
3131 | int credits = path->p_tree_depth * 2 + 1; | |
3132 | ||
3133 | handle = ocfs2_start_trans(osb, credits); | |
3134 | if (IS_ERR(handle)) { | |
3135 | ret = PTR_ERR(handle); | |
3136 | mlog_errno(ret); | |
3137 | return ret; | |
3138 | } | |
3139 | ||
3140 | ret = ocfs2_remove_rightmost_path(handle, et, path, dealloc); | |
3141 | if (ret) | |
3142 | mlog_errno(ret); | |
3143 | ||
3144 | ocfs2_commit_trans(osb, handle); | |
3145 | return ret; | |
3146 | } | |
3147 | ||
328d5752 MF |
3148 | /* |
3149 | * Left rotation of btree records. | |
3150 | * | |
3151 | * In many ways, this is (unsurprisingly) the opposite of right | |
3152 | * rotation. We start at some non-rightmost path containing an empty | |
3153 | * extent in the leaf block. The code works its way to the rightmost | |
3154 | * path by rotating records to the left in every subtree. | |
3155 | * | |
3156 | * This is used by any code which reduces the number of extent records | |
3157 | * in a leaf. After removal, an empty record should be placed in the | |
3158 | * leftmost list position. | |
3159 | * | |
3160 | * This won't handle a length update of the rightmost path records if | |
3161 | * the rightmost tree leaf record is removed so the caller is | |
3162 | * responsible for detecting and correcting that. | |
3163 | */ | |
70f18c08 JB |
3164 | static int ocfs2_rotate_tree_left(handle_t *handle, |
3165 | struct ocfs2_extent_tree *et, | |
328d5752 | 3166 | struct ocfs2_path *path, |
70f18c08 | 3167 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
328d5752 MF |
3168 | { |
3169 | int ret, orig_credits = handle->h_buffer_credits; | |
3170 | struct ocfs2_path *tmp_path = NULL, *restart_path = NULL; | |
3171 | struct ocfs2_extent_block *eb; | |
3172 | struct ocfs2_extent_list *el; | |
3173 | ||
3174 | el = path_leaf_el(path); | |
3175 | if (!ocfs2_is_empty_extent(&el->l_recs[0])) | |
3176 | return 0; | |
3177 | ||
3178 | if (path->p_tree_depth == 0) { | |
3179 | rightmost_no_delete: | |
3180 | /* | |
e7d4cb6b | 3181 | * Inline extents. This is trivially handled, so do |
328d5752 MF |
3182 | * it up front. |
3183 | */ | |
70f18c08 | 3184 | ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path); |
328d5752 MF |
3185 | if (ret) |
3186 | mlog_errno(ret); | |
3187 | goto out; | |
3188 | } | |
3189 | ||
3190 | /* | |
3191 | * Handle rightmost branch now. There's several cases: | |
3192 | * 1) simple rotation leaving records in there. That's trivial. | |
3193 | * 2) rotation requiring a branch delete - there's no more | |
3194 | * records left. Two cases of this: | |
3195 | * a) There are branches to the left. | |
3196 | * b) This is also the leftmost (the only) branch. | |
3197 | * | |
3198 | * 1) is handled via ocfs2_rotate_rightmost_leaf_left() | |
3199 | * 2a) we need the left branch so that we can update it with the unlink | |
70f18c08 | 3200 | * 2b) we need to bring the root back to inline extents. |
328d5752 MF |
3201 | */ |
3202 | ||
3203 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; | |
3204 | el = &eb->h_list; | |
3205 | if (eb->h_next_leaf_blk == 0) { | |
3206 | /* | |
3207 | * This gets a bit tricky if we're going to delete the | |
3208 | * rightmost path. Get the other cases out of the way | |
3209 | * 1st. | |
3210 | */ | |
3211 | if (le16_to_cpu(el->l_next_free_rec) > 1) | |
3212 | goto rightmost_no_delete; | |
3213 | ||
3214 | if (le16_to_cpu(el->l_next_free_rec) == 0) { | |
93f5920d JP |
3215 | ret = ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
3216 | "Owner %llu has empty extent block at %llu\n", | |
3217 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), | |
3218 | (unsigned long long)le64_to_cpu(eb->h_blkno)); | |
328d5752 MF |
3219 | goto out; |
3220 | } | |
3221 | ||
3222 | /* | |
3223 | * XXX: The caller can not trust "path" any more after | |
3224 | * this as it will have been deleted. What do we do? | |
3225 | * | |
3226 | * In theory the rotate-for-merge code will never get | |
3227 | * here because it'll always ask for a rotate in a | |
3228 | * nonempty list. | |
3229 | */ | |
3230 | ||
70f18c08 JB |
3231 | ret = ocfs2_remove_rightmost_path(handle, et, path, |
3232 | dealloc); | |
328d5752 MF |
3233 | if (ret) |
3234 | mlog_errno(ret); | |
3235 | goto out; | |
3236 | } | |
3237 | ||
3238 | /* | |
3239 | * Now we can loop, remembering the path we get from -EAGAIN | |
3240 | * and restarting from there. | |
3241 | */ | |
3242 | try_rotate: | |
e46f74dc JB |
3243 | ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path, |
3244 | dealloc, &restart_path); | |
328d5752 MF |
3245 | if (ret && ret != -EAGAIN) { |
3246 | mlog_errno(ret); | |
3247 | goto out; | |
3248 | } | |
3249 | ||
3250 | while (ret == -EAGAIN) { | |
3251 | tmp_path = restart_path; | |
3252 | restart_path = NULL; | |
3253 | ||
e46f74dc | 3254 | ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, |
328d5752 | 3255 | tmp_path, dealloc, |
e46f74dc | 3256 | &restart_path); |
328d5752 MF |
3257 | if (ret && ret != -EAGAIN) { |
3258 | mlog_errno(ret); | |
3259 | goto out; | |
3260 | } | |
3261 | ||
3262 | ocfs2_free_path(tmp_path); | |
3263 | tmp_path = NULL; | |
3264 | ||
3265 | if (ret == 0) | |
3266 | goto try_rotate; | |
3267 | } | |
3268 | ||
3269 | out: | |
3270 | ocfs2_free_path(tmp_path); | |
3271 | ocfs2_free_path(restart_path); | |
3272 | return ret; | |
3273 | } | |
3274 | ||
3275 | static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el, | |
3276 | int index) | |
3277 | { | |
3278 | struct ocfs2_extent_rec *rec = &el->l_recs[index]; | |
3279 | unsigned int size; | |
3280 | ||
3281 | if (rec->e_leaf_clusters == 0) { | |
3282 | /* | |
3283 | * We consumed all of the merged-from record. An empty | |
3284 | * extent cannot exist anywhere but the 1st array | |
3285 | * position, so move things over if the merged-from | |
3286 | * record doesn't occupy that position. | |
3287 | * | |
3288 | * This creates a new empty extent so the caller | |
3289 | * should be smart enough to have removed any existing | |
3290 | * ones. | |
3291 | */ | |
3292 | if (index > 0) { | |
3293 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); | |
3294 | size = index * sizeof(struct ocfs2_extent_rec); | |
3295 | memmove(&el->l_recs[1], &el->l_recs[0], size); | |
3296 | } | |
3297 | ||
3298 | /* | |
3299 | * Always memset - the caller doesn't check whether it | |
3300 | * created an empty extent, so there could be junk in | |
3301 | * the other fields. | |
3302 | */ | |
3303 | memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec)); | |
3304 | } | |
3305 | } | |
3306 | ||
4fe82c31 | 3307 | static int ocfs2_get_right_path(struct ocfs2_extent_tree *et, |
677b9752 TM |
3308 | struct ocfs2_path *left_path, |
3309 | struct ocfs2_path **ret_right_path) | |
3310 | { | |
3311 | int ret; | |
3312 | u32 right_cpos; | |
3313 | struct ocfs2_path *right_path = NULL; | |
3314 | struct ocfs2_extent_list *left_el; | |
3315 | ||
3316 | *ret_right_path = NULL; | |
3317 | ||
3318 | /* This function shouldn't be called for non-trees. */ | |
3319 | BUG_ON(left_path->p_tree_depth == 0); | |
3320 | ||
3321 | left_el = path_leaf_el(left_path); | |
3322 | BUG_ON(left_el->l_next_free_rec != left_el->l_count); | |
3323 | ||
4fe82c31 JB |
3324 | ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
3325 | left_path, &right_cpos); | |
677b9752 TM |
3326 | if (ret) { |
3327 | mlog_errno(ret); | |
3328 | goto out; | |
3329 | } | |
3330 | ||
3331 | /* This function shouldn't be called for the rightmost leaf. */ | |
3332 | BUG_ON(right_cpos == 0); | |
3333 | ||
ffdd7a54 | 3334 | right_path = ocfs2_new_path_from_path(left_path); |
677b9752 TM |
3335 | if (!right_path) { |
3336 | ret = -ENOMEM; | |
3337 | mlog_errno(ret); | |
3338 | goto out; | |
3339 | } | |
3340 | ||
4fe82c31 | 3341 | ret = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
677b9752 TM |
3342 | if (ret) { |
3343 | mlog_errno(ret); | |
3344 | goto out; | |
3345 | } | |
3346 | ||
3347 | *ret_right_path = right_path; | |
3348 | out: | |
3349 | if (ret) | |
3350 | ocfs2_free_path(right_path); | |
3351 | return ret; | |
3352 | } | |
3353 | ||
328d5752 MF |
3354 | /* |
3355 | * Remove split_rec clusters from the record at index and merge them | |
677b9752 TM |
3356 | * onto the beginning of the record "next" to it. |
3357 | * For index < l_count - 1, the next means the extent rec at index + 1. | |
3358 | * For index == l_count - 1, the "next" means the 1st extent rec of the | |
3359 | * next extent block. | |
328d5752 | 3360 | */ |
4fe82c31 | 3361 | static int ocfs2_merge_rec_right(struct ocfs2_path *left_path, |
677b9752 | 3362 | handle_t *handle, |
7dc02805 | 3363 | struct ocfs2_extent_tree *et, |
677b9752 TM |
3364 | struct ocfs2_extent_rec *split_rec, |
3365 | int index) | |
328d5752 | 3366 | { |
677b9752 | 3367 | int ret, next_free, i; |
328d5752 MF |
3368 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
3369 | struct ocfs2_extent_rec *left_rec; | |
3370 | struct ocfs2_extent_rec *right_rec; | |
677b9752 TM |
3371 | struct ocfs2_extent_list *right_el; |
3372 | struct ocfs2_path *right_path = NULL; | |
3373 | int subtree_index = 0; | |
3374 | struct ocfs2_extent_list *el = path_leaf_el(left_path); | |
3375 | struct buffer_head *bh = path_leaf_bh(left_path); | |
3376 | struct buffer_head *root_bh = NULL; | |
328d5752 MF |
3377 | |
3378 | BUG_ON(index >= le16_to_cpu(el->l_next_free_rec)); | |
328d5752 | 3379 | left_rec = &el->l_recs[index]; |
677b9752 | 3380 | |
9d8df6aa | 3381 | if (index == le16_to_cpu(el->l_next_free_rec) - 1 && |
677b9752 TM |
3382 | le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) { |
3383 | /* we meet with a cross extent block merge. */ | |
4fe82c31 | 3384 | ret = ocfs2_get_right_path(et, left_path, &right_path); |
677b9752 TM |
3385 | if (ret) { |
3386 | mlog_errno(ret); | |
629a3b5f | 3387 | return ret; |
677b9752 TM |
3388 | } |
3389 | ||
3390 | right_el = path_leaf_el(right_path); | |
3391 | next_free = le16_to_cpu(right_el->l_next_free_rec); | |
3392 | BUG_ON(next_free <= 0); | |
3393 | right_rec = &right_el->l_recs[0]; | |
3394 | if (ocfs2_is_empty_extent(right_rec)) { | |
9d8df6aa | 3395 | BUG_ON(next_free <= 1); |
677b9752 TM |
3396 | right_rec = &right_el->l_recs[1]; |
3397 | } | |
3398 | ||
3399 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + | |
3400 | le16_to_cpu(left_rec->e_leaf_clusters) != | |
3401 | le32_to_cpu(right_rec->e_cpos)); | |
3402 | ||
7dc02805 JB |
3403 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
3404 | right_path); | |
677b9752 TM |
3405 | |
3406 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, | |
3407 | handle->h_buffer_credits, | |
3408 | right_path); | |
3409 | if (ret) { | |
3410 | mlog_errno(ret); | |
3411 | goto out; | |
3412 | } | |
3413 | ||
3414 | root_bh = left_path->p_node[subtree_index].bh; | |
3415 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); | |
3416 | ||
7dc02805 | 3417 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
13723d00 | 3418 | subtree_index); |
677b9752 TM |
3419 | if (ret) { |
3420 | mlog_errno(ret); | |
3421 | goto out; | |
3422 | } | |
3423 | ||
3424 | for (i = subtree_index + 1; | |
3425 | i < path_num_items(right_path); i++) { | |
7dc02805 | 3426 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
13723d00 | 3427 | right_path, i); |
677b9752 TM |
3428 | if (ret) { |
3429 | mlog_errno(ret); | |
3430 | goto out; | |
3431 | } | |
3432 | ||
7dc02805 | 3433 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
13723d00 | 3434 | left_path, i); |
677b9752 TM |
3435 | if (ret) { |
3436 | mlog_errno(ret); | |
3437 | goto out; | |
3438 | } | |
3439 | } | |
3440 | ||
3441 | } else { | |
3442 | BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1); | |
3443 | right_rec = &el->l_recs[index + 1]; | |
3444 | } | |
328d5752 | 3445 | |
7dc02805 | 3446 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path, |
13723d00 | 3447 | path_num_items(left_path) - 1); |
328d5752 MF |
3448 | if (ret) { |
3449 | mlog_errno(ret); | |
3450 | goto out; | |
3451 | } | |
3452 | ||
3453 | le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters); | |
3454 | ||
3455 | le32_add_cpu(&right_rec->e_cpos, -split_clusters); | |
3456 | le64_add_cpu(&right_rec->e_blkno, | |
7dc02805 JB |
3457 | -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), |
3458 | split_clusters)); | |
328d5752 MF |
3459 | le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters); |
3460 | ||
3461 | ocfs2_cleanup_merge(el, index); | |
3462 | ||
ec20cec7 | 3463 | ocfs2_journal_dirty(handle, bh); |
677b9752 | 3464 | if (right_path) { |
ec20cec7 | 3465 | ocfs2_journal_dirty(handle, path_leaf_bh(right_path)); |
4619c73e JB |
3466 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
3467 | subtree_index); | |
677b9752 TM |
3468 | } |
3469 | out: | |
fd90d4df | 3470 | ocfs2_free_path(right_path); |
677b9752 TM |
3471 | return ret; |
3472 | } | |
3473 | ||
4fe82c31 | 3474 | static int ocfs2_get_left_path(struct ocfs2_extent_tree *et, |
677b9752 TM |
3475 | struct ocfs2_path *right_path, |
3476 | struct ocfs2_path **ret_left_path) | |
3477 | { | |
3478 | int ret; | |
3479 | u32 left_cpos; | |
3480 | struct ocfs2_path *left_path = NULL; | |
3481 | ||
3482 | *ret_left_path = NULL; | |
3483 | ||
3484 | /* This function shouldn't be called for non-trees. */ | |
3485 | BUG_ON(right_path->p_tree_depth == 0); | |
3486 | ||
4fe82c31 | 3487 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
677b9752 TM |
3488 | right_path, &left_cpos); |
3489 | if (ret) { | |
3490 | mlog_errno(ret); | |
3491 | goto out; | |
3492 | } | |
3493 | ||
3494 | /* This function shouldn't be called for the leftmost leaf. */ | |
3495 | BUG_ON(left_cpos == 0); | |
3496 | ||
ffdd7a54 | 3497 | left_path = ocfs2_new_path_from_path(right_path); |
677b9752 TM |
3498 | if (!left_path) { |
3499 | ret = -ENOMEM; | |
3500 | mlog_errno(ret); | |
3501 | goto out; | |
3502 | } | |
3503 | ||
4fe82c31 | 3504 | ret = ocfs2_find_path(et->et_ci, left_path, left_cpos); |
677b9752 TM |
3505 | if (ret) { |
3506 | mlog_errno(ret); | |
3507 | goto out; | |
3508 | } | |
3509 | ||
3510 | *ret_left_path = left_path; | |
328d5752 | 3511 | out: |
677b9752 TM |
3512 | if (ret) |
3513 | ocfs2_free_path(left_path); | |
328d5752 MF |
3514 | return ret; |
3515 | } | |
3516 | ||
3517 | /* | |
3518 | * Remove split_rec clusters from the record at index and merge them | |
677b9752 TM |
3519 | * onto the tail of the record "before" it. |
3520 | * For index > 0, the "before" means the extent rec at index - 1. | |
3521 | * | |
3522 | * For index == 0, the "before" means the last record of the previous | |
3523 | * extent block. And there is also a situation that we may need to | |
3524 | * remove the rightmost leaf extent block in the right_path and change | |
3525 | * the right path to indicate the new rightmost path. | |
328d5752 | 3526 | */ |
4fe82c31 | 3527 | static int ocfs2_merge_rec_left(struct ocfs2_path *right_path, |
328d5752 | 3528 | handle_t *handle, |
4fe82c31 | 3529 | struct ocfs2_extent_tree *et, |
328d5752 | 3530 | struct ocfs2_extent_rec *split_rec, |
677b9752 TM |
3531 | struct ocfs2_cached_dealloc_ctxt *dealloc, |
3532 | int index) | |
328d5752 | 3533 | { |
677b9752 | 3534 | int ret, i, subtree_index = 0, has_empty_extent = 0; |
328d5752 MF |
3535 | unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters); |
3536 | struct ocfs2_extent_rec *left_rec; | |
3537 | struct ocfs2_extent_rec *right_rec; | |
677b9752 TM |
3538 | struct ocfs2_extent_list *el = path_leaf_el(right_path); |
3539 | struct buffer_head *bh = path_leaf_bh(right_path); | |
3540 | struct buffer_head *root_bh = NULL; | |
3541 | struct ocfs2_path *left_path = NULL; | |
3542 | struct ocfs2_extent_list *left_el; | |
328d5752 | 3543 | |
677b9752 | 3544 | BUG_ON(index < 0); |
328d5752 | 3545 | |
328d5752 | 3546 | right_rec = &el->l_recs[index]; |
677b9752 TM |
3547 | if (index == 0) { |
3548 | /* we meet with a cross extent block merge. */ | |
4fe82c31 | 3549 | ret = ocfs2_get_left_path(et, right_path, &left_path); |
677b9752 TM |
3550 | if (ret) { |
3551 | mlog_errno(ret); | |
992ef6e7 | 3552 | return ret; |
677b9752 TM |
3553 | } |
3554 | ||
3555 | left_el = path_leaf_el(left_path); | |
3556 | BUG_ON(le16_to_cpu(left_el->l_next_free_rec) != | |
3557 | le16_to_cpu(left_el->l_count)); | |
3558 | ||
3559 | left_rec = &left_el->l_recs[ | |
3560 | le16_to_cpu(left_el->l_next_free_rec) - 1]; | |
3561 | BUG_ON(le32_to_cpu(left_rec->e_cpos) + | |
3562 | le16_to_cpu(left_rec->e_leaf_clusters) != | |
3563 | le32_to_cpu(split_rec->e_cpos)); | |
3564 | ||
7dc02805 JB |
3565 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
3566 | right_path); | |
677b9752 TM |
3567 | |
3568 | ret = ocfs2_extend_rotate_transaction(handle, subtree_index, | |
3569 | handle->h_buffer_credits, | |
3570 | left_path); | |
3571 | if (ret) { | |
3572 | mlog_errno(ret); | |
3573 | goto out; | |
3574 | } | |
3575 | ||
3576 | root_bh = left_path->p_node[subtree_index].bh; | |
3577 | BUG_ON(root_bh != right_path->p_node[subtree_index].bh); | |
3578 | ||
4fe82c31 | 3579 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
13723d00 | 3580 | subtree_index); |
677b9752 TM |
3581 | if (ret) { |
3582 | mlog_errno(ret); | |
3583 | goto out; | |
3584 | } | |
3585 | ||
3586 | for (i = subtree_index + 1; | |
3587 | i < path_num_items(right_path); i++) { | |
4fe82c31 | 3588 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
13723d00 | 3589 | right_path, i); |
677b9752 TM |
3590 | if (ret) { |
3591 | mlog_errno(ret); | |
3592 | goto out; | |
3593 | } | |
3594 | ||
4fe82c31 | 3595 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, |
13723d00 | 3596 | left_path, i); |
677b9752 TM |
3597 | if (ret) { |
3598 | mlog_errno(ret); | |
3599 | goto out; | |
3600 | } | |
3601 | } | |
3602 | } else { | |
3603 | left_rec = &el->l_recs[index - 1]; | |
3604 | if (ocfs2_is_empty_extent(&el->l_recs[0])) | |
3605 | has_empty_extent = 1; | |
3606 | } | |
328d5752 | 3607 | |
4fe82c31 | 3608 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path, |
9047beab | 3609 | path_num_items(right_path) - 1); |
328d5752 MF |
3610 | if (ret) { |
3611 | mlog_errno(ret); | |
3612 | goto out; | |
3613 | } | |
3614 | ||
3615 | if (has_empty_extent && index == 1) { | |
3616 | /* | |
3617 | * The easy case - we can just plop the record right in. | |
3618 | */ | |
3619 | *left_rec = *split_rec; | |
677b9752 | 3620 | } else |
328d5752 | 3621 | le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters); |
328d5752 MF |
3622 | |
3623 | le32_add_cpu(&right_rec->e_cpos, split_clusters); | |
3624 | le64_add_cpu(&right_rec->e_blkno, | |
4fe82c31 JB |
3625 | ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci), |
3626 | split_clusters)); | |
328d5752 MF |
3627 | le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters); |
3628 | ||
3629 | ocfs2_cleanup_merge(el, index); | |
3630 | ||
ec20cec7 | 3631 | ocfs2_journal_dirty(handle, bh); |
677b9752 | 3632 | if (left_path) { |
ec20cec7 | 3633 | ocfs2_journal_dirty(handle, path_leaf_bh(left_path)); |
677b9752 TM |
3634 | |
3635 | /* | |
3636 | * In the situation that the right_rec is empty and the extent | |
3637 | * block is empty also, ocfs2_complete_edge_insert can't handle | |
3638 | * it and we need to delete the right extent block. | |
3639 | */ | |
3640 | if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 && | |
3641 | le16_to_cpu(el->l_next_free_rec) == 1) { | |
17215989 X |
3642 | /* extend credit for ocfs2_remove_rightmost_path */ |
3643 | ret = ocfs2_extend_rotate_transaction(handle, 0, | |
3644 | handle->h_buffer_credits, | |
3645 | right_path); | |
3646 | if (ret) { | |
3647 | mlog_errno(ret); | |
3648 | goto out; | |
3649 | } | |
677b9752 | 3650 | |
70f18c08 | 3651 | ret = ocfs2_remove_rightmost_path(handle, et, |
e7d4cb6b | 3652 | right_path, |
70f18c08 | 3653 | dealloc); |
677b9752 TM |
3654 | if (ret) { |
3655 | mlog_errno(ret); | |
3656 | goto out; | |
3657 | } | |
3658 | ||
3659 | /* Now the rightmost extent block has been deleted. | |
3660 | * So we use the new rightmost path. | |
3661 | */ | |
3662 | ocfs2_mv_path(right_path, left_path); | |
3663 | left_path = NULL; | |
3664 | } else | |
4619c73e | 3665 | ocfs2_complete_edge_insert(handle, left_path, |
677b9752 TM |
3666 | right_path, subtree_index); |
3667 | } | |
328d5752 | 3668 | out: |
fd90d4df | 3669 | ocfs2_free_path(left_path); |
328d5752 MF |
3670 | return ret; |
3671 | } | |
3672 | ||
c495dd24 JB |
3673 | static int ocfs2_try_to_merge_extent(handle_t *handle, |
3674 | struct ocfs2_extent_tree *et, | |
677b9752 | 3675 | struct ocfs2_path *path, |
328d5752 MF |
3676 | int split_index, |
3677 | struct ocfs2_extent_rec *split_rec, | |
3678 | struct ocfs2_cached_dealloc_ctxt *dealloc, | |
c495dd24 | 3679 | struct ocfs2_merge_ctxt *ctxt) |
328d5752 | 3680 | { |
518d7269 | 3681 | int ret = 0; |
677b9752 | 3682 | struct ocfs2_extent_list *el = path_leaf_el(path); |
328d5752 MF |
3683 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
3684 | ||
3685 | BUG_ON(ctxt->c_contig_type == CONTIG_NONE); | |
3686 | ||
518d7269 | 3687 | if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) { |
17215989 X |
3688 | /* extend credit for ocfs2_remove_rightmost_path */ |
3689 | ret = ocfs2_extend_rotate_transaction(handle, 0, | |
3690 | handle->h_buffer_credits, | |
3691 | path); | |
3692 | if (ret) { | |
3693 | mlog_errno(ret); | |
3694 | goto out; | |
3695 | } | |
518d7269 TM |
3696 | /* |
3697 | * The merge code will need to create an empty | |
3698 | * extent to take the place of the newly | |
3699 | * emptied slot. Remove any pre-existing empty | |
3700 | * extents - having more than one in a leaf is | |
3701 | * illegal. | |
3702 | */ | |
70f18c08 | 3703 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
518d7269 TM |
3704 | if (ret) { |
3705 | mlog_errno(ret); | |
3706 | goto out; | |
328d5752 | 3707 | } |
518d7269 TM |
3708 | split_index--; |
3709 | rec = &el->l_recs[split_index]; | |
328d5752 MF |
3710 | } |
3711 | ||
3712 | if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) { | |
3713 | /* | |
3714 | * Left-right contig implies this. | |
3715 | */ | |
3716 | BUG_ON(!ctxt->c_split_covers_rec); | |
328d5752 MF |
3717 | |
3718 | /* | |
3719 | * Since the leftright insert always covers the entire | |
3720 | * extent, this call will delete the insert record | |
3721 | * entirely, resulting in an empty extent record added to | |
3722 | * the extent block. | |
3723 | * | |
3724 | * Since the adding of an empty extent shifts | |
3725 | * everything back to the right, there's no need to | |
3726 | * update split_index here. | |
677b9752 TM |
3727 | * |
3728 | * When the split_index is zero, we need to merge it to the | |
3729 | * prevoius extent block. It is more efficient and easier | |
3730 | * if we do merge_right first and merge_left later. | |
328d5752 | 3731 | */ |
4fe82c31 | 3732 | ret = ocfs2_merge_rec_right(path, handle, et, split_rec, |
677b9752 | 3733 | split_index); |
328d5752 MF |
3734 | if (ret) { |
3735 | mlog_errno(ret); | |
3736 | goto out; | |
3737 | } | |
3738 | ||
3739 | /* | |
3740 | * We can only get this from logic error above. | |
3741 | */ | |
3742 | BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0])); | |
3743 | ||
17215989 X |
3744 | /* extend credit for ocfs2_remove_rightmost_path */ |
3745 | ret = ocfs2_extend_rotate_transaction(handle, 0, | |
3746 | handle->h_buffer_credits, | |
3747 | path); | |
3748 | if (ret) { | |
3749 | mlog_errno(ret); | |
3750 | goto out; | |
3751 | } | |
3752 | ||
677b9752 | 3753 | /* The merge left us with an empty extent, remove it. */ |
70f18c08 | 3754 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
328d5752 MF |
3755 | if (ret) { |
3756 | mlog_errno(ret); | |
3757 | goto out; | |
3758 | } | |
677b9752 | 3759 | |
328d5752 MF |
3760 | rec = &el->l_recs[split_index]; |
3761 | ||
3762 | /* | |
3763 | * Note that we don't pass split_rec here on purpose - | |
677b9752 | 3764 | * we've merged it into the rec already. |
328d5752 | 3765 | */ |
4fe82c31 JB |
3766 | ret = ocfs2_merge_rec_left(path, handle, et, rec, |
3767 | dealloc, split_index); | |
677b9752 | 3768 | |
328d5752 MF |
3769 | if (ret) { |
3770 | mlog_errno(ret); | |
3771 | goto out; | |
3772 | } | |
3773 | ||
17215989 X |
3774 | /* extend credit for ocfs2_remove_rightmost_path */ |
3775 | ret = ocfs2_extend_rotate_transaction(handle, 0, | |
3776 | handle->h_buffer_credits, | |
3777 | path); | |
3778 | if (ret) { | |
3779 | mlog_errno(ret); | |
3780 | goto out; | |
3781 | } | |
3782 | ||
70f18c08 | 3783 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
328d5752 MF |
3784 | /* |
3785 | * Error from this last rotate is not critical, so | |
3786 | * print but don't bubble it up. | |
3787 | */ | |
3788 | if (ret) | |
3789 | mlog_errno(ret); | |
3790 | ret = 0; | |
3791 | } else { | |
3792 | /* | |
3793 | * Merge a record to the left or right. | |
3794 | * | |
3795 | * 'contig_type' is relative to the existing record, | |
3796 | * so for example, if we're "right contig", it's to | |
3797 | * the record on the left (hence the left merge). | |
3798 | */ | |
3799 | if (ctxt->c_contig_type == CONTIG_RIGHT) { | |
4fe82c31 JB |
3800 | ret = ocfs2_merge_rec_left(path, handle, et, |
3801 | split_rec, dealloc, | |
328d5752 MF |
3802 | split_index); |
3803 | if (ret) { | |
3804 | mlog_errno(ret); | |
3805 | goto out; | |
3806 | } | |
3807 | } else { | |
4fe82c31 | 3808 | ret = ocfs2_merge_rec_right(path, handle, |
7dc02805 | 3809 | et, split_rec, |
328d5752 MF |
3810 | split_index); |
3811 | if (ret) { | |
3812 | mlog_errno(ret); | |
3813 | goto out; | |
3814 | } | |
3815 | } | |
3816 | ||
3817 | if (ctxt->c_split_covers_rec) { | |
17215989 X |
3818 | /* extend credit for ocfs2_remove_rightmost_path */ |
3819 | ret = ocfs2_extend_rotate_transaction(handle, 0, | |
3820 | handle->h_buffer_credits, | |
3821 | path); | |
3822 | if (ret) { | |
3823 | mlog_errno(ret); | |
3824 | ret = 0; | |
3825 | goto out; | |
3826 | } | |
3827 | ||
328d5752 MF |
3828 | /* |
3829 | * The merge may have left an empty extent in | |
3830 | * our leaf. Try to rotate it away. | |
3831 | */ | |
70f18c08 JB |
3832 | ret = ocfs2_rotate_tree_left(handle, et, path, |
3833 | dealloc); | |
328d5752 MF |
3834 | if (ret) |
3835 | mlog_errno(ret); | |
3836 | ret = 0; | |
3837 | } | |
3838 | } | |
3839 | ||
3840 | out: | |
3841 | return ret; | |
3842 | } | |
3843 | ||
3844 | static void ocfs2_subtract_from_rec(struct super_block *sb, | |
3845 | enum ocfs2_split_type split, | |
3846 | struct ocfs2_extent_rec *rec, | |
3847 | struct ocfs2_extent_rec *split_rec) | |
3848 | { | |
3849 | u64 len_blocks; | |
3850 | ||
3851 | len_blocks = ocfs2_clusters_to_blocks(sb, | |
3852 | le16_to_cpu(split_rec->e_leaf_clusters)); | |
3853 | ||
3854 | if (split == SPLIT_LEFT) { | |
3855 | /* | |
3856 | * Region is on the left edge of the existing | |
3857 | * record. | |
3858 | */ | |
3859 | le32_add_cpu(&rec->e_cpos, | |
3860 | le16_to_cpu(split_rec->e_leaf_clusters)); | |
3861 | le64_add_cpu(&rec->e_blkno, len_blocks); | |
3862 | le16_add_cpu(&rec->e_leaf_clusters, | |
3863 | -le16_to_cpu(split_rec->e_leaf_clusters)); | |
3864 | } else { | |
3865 | /* | |
3866 | * Region is on the right edge of the existing | |
3867 | * record. | |
3868 | */ | |
3869 | le16_add_cpu(&rec->e_leaf_clusters, | |
3870 | -le16_to_cpu(split_rec->e_leaf_clusters)); | |
3871 | } | |
3872 | } | |
3873 | ||
3874 | /* | |
3875 | * Do the final bits of extent record insertion at the target leaf | |
3876 | * list. If this leaf is part of an allocation tree, it is assumed | |
3877 | * that the tree above has been prepared. | |
3878 | */ | |
d5628623 JB |
3879 | static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et, |
3880 | struct ocfs2_extent_rec *insert_rec, | |
328d5752 | 3881 | struct ocfs2_extent_list *el, |
d5628623 | 3882 | struct ocfs2_insert_type *insert) |
328d5752 MF |
3883 | { |
3884 | int i = insert->ins_contig_index; | |
3885 | unsigned int range; | |
3886 | struct ocfs2_extent_rec *rec; | |
3887 | ||
3888 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); | |
3889 | ||
3890 | if (insert->ins_split != SPLIT_NONE) { | |
3891 | i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos)); | |
3892 | BUG_ON(i == -1); | |
3893 | rec = &el->l_recs[i]; | |
d5628623 JB |
3894 | ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
3895 | insert->ins_split, rec, | |
328d5752 MF |
3896 | insert_rec); |
3897 | goto rotate; | |
3898 | } | |
3899 | ||
3900 | /* | |
3901 | * Contiguous insert - either left or right. | |
3902 | */ | |
3903 | if (insert->ins_contig != CONTIG_NONE) { | |
3904 | rec = &el->l_recs[i]; | |
3905 | if (insert->ins_contig == CONTIG_LEFT) { | |
3906 | rec->e_blkno = insert_rec->e_blkno; | |
3907 | rec->e_cpos = insert_rec->e_cpos; | |
3908 | } | |
3909 | le16_add_cpu(&rec->e_leaf_clusters, | |
3910 | le16_to_cpu(insert_rec->e_leaf_clusters)); | |
3911 | return; | |
3912 | } | |
3913 | ||
3914 | /* | |
3915 | * Handle insert into an empty leaf. | |
3916 | */ | |
3917 | if (le16_to_cpu(el->l_next_free_rec) == 0 || | |
3918 | ((le16_to_cpu(el->l_next_free_rec) == 1) && | |
3919 | ocfs2_is_empty_extent(&el->l_recs[0]))) { | |
3920 | el->l_recs[0] = *insert_rec; | |
3921 | el->l_next_free_rec = cpu_to_le16(1); | |
3922 | return; | |
3923 | } | |
3924 | ||
3925 | /* | |
3926 | * Appending insert. | |
3927 | */ | |
3928 | if (insert->ins_appending == APPEND_TAIL) { | |
3929 | i = le16_to_cpu(el->l_next_free_rec) - 1; | |
3930 | rec = &el->l_recs[i]; | |
3931 | range = le32_to_cpu(rec->e_cpos) | |
3932 | + le16_to_cpu(rec->e_leaf_clusters); | |
3933 | BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range); | |
3934 | ||
3935 | mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >= | |
3936 | le16_to_cpu(el->l_count), | |
d5628623 | 3937 | "owner %llu, depth %u, count %u, next free %u, " |
328d5752 MF |
3938 | "rec.cpos %u, rec.clusters %u, " |
3939 | "insert.cpos %u, insert.clusters %u\n", | |
d5628623 | 3940 | ocfs2_metadata_cache_owner(et->et_ci), |
328d5752 MF |
3941 | le16_to_cpu(el->l_tree_depth), |
3942 | le16_to_cpu(el->l_count), | |
3943 | le16_to_cpu(el->l_next_free_rec), | |
3944 | le32_to_cpu(el->l_recs[i].e_cpos), | |
3945 | le16_to_cpu(el->l_recs[i].e_leaf_clusters), | |
3946 | le32_to_cpu(insert_rec->e_cpos), | |
3947 | le16_to_cpu(insert_rec->e_leaf_clusters)); | |
3948 | i++; | |
3949 | el->l_recs[i] = *insert_rec; | |
3950 | le16_add_cpu(&el->l_next_free_rec, 1); | |
3951 | return; | |
3952 | } | |
3953 | ||
3954 | rotate: | |
3955 | /* | |
3956 | * Ok, we have to rotate. | |
3957 | * | |
3958 | * At this point, it is safe to assume that inserting into an | |
3959 | * empty leaf and appending to a leaf have both been handled | |
3960 | * above. | |
3961 | * | |
3962 | * This leaf needs to have space, either by the empty 1st | |
3963 | * extent record, or by virtue of an l_next_rec < l_count. | |
3964 | */ | |
3965 | ocfs2_rotate_leaf(el, insert_rec); | |
3966 | } | |
3967 | ||
d401dc12 JB |
3968 | static void ocfs2_adjust_rightmost_records(handle_t *handle, |
3969 | struct ocfs2_extent_tree *et, | |
328d5752 MF |
3970 | struct ocfs2_path *path, |
3971 | struct ocfs2_extent_rec *insert_rec) | |
3972 | { | |
d22aa615 | 3973 | int i, next_free; |
328d5752 MF |
3974 | struct buffer_head *bh; |
3975 | struct ocfs2_extent_list *el; | |
3976 | struct ocfs2_extent_rec *rec; | |
3977 | ||
3978 | /* | |
3979 | * Update everything except the leaf block. | |
3980 | */ | |
3981 | for (i = 0; i < path->p_tree_depth; i++) { | |
3982 | bh = path->p_node[i].bh; | |
3983 | el = path->p_node[i].el; | |
3984 | ||
dcd0538f MF |
3985 | next_free = le16_to_cpu(el->l_next_free_rec); |
3986 | if (next_free == 0) { | |
d401dc12 | 3987 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
7ecef14a | 3988 | "Owner %llu has a bad extent list\n", |
d401dc12 | 3989 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci)); |
328d5752 MF |
3990 | return; |
3991 | } | |
3992 | ||
3993 | rec = &el->l_recs[next_free - 1]; | |
3994 | ||
3995 | rec->e_int_clusters = insert_rec->e_cpos; | |
3996 | le32_add_cpu(&rec->e_int_clusters, | |
3997 | le16_to_cpu(insert_rec->e_leaf_clusters)); | |
3998 | le32_add_cpu(&rec->e_int_clusters, | |
3999 | -le32_to_cpu(rec->e_cpos)); | |
4000 | ||
ec20cec7 | 4001 | ocfs2_journal_dirty(handle, bh); |
328d5752 MF |
4002 | } |
4003 | } | |
4004 | ||
d401dc12 JB |
4005 | static int ocfs2_append_rec_to_path(handle_t *handle, |
4006 | struct ocfs2_extent_tree *et, | |
328d5752 MF |
4007 | struct ocfs2_extent_rec *insert_rec, |
4008 | struct ocfs2_path *right_path, | |
4009 | struct ocfs2_path **ret_left_path) | |
4010 | { | |
4011 | int ret, next_free; | |
4012 | struct ocfs2_extent_list *el; | |
4013 | struct ocfs2_path *left_path = NULL; | |
4014 | ||
4015 | *ret_left_path = NULL; | |
4016 | ||
4017 | /* | |
4018 | * This shouldn't happen for non-trees. The extent rec cluster | |
4019 | * count manipulation below only works for interior nodes. | |
4020 | */ | |
4021 | BUG_ON(right_path->p_tree_depth == 0); | |
4022 | ||
4023 | /* | |
4024 | * If our appending insert is at the leftmost edge of a leaf, | |
4025 | * then we might need to update the rightmost records of the | |
4026 | * neighboring path. | |
4027 | */ | |
4028 | el = path_leaf_el(right_path); | |
4029 | next_free = le16_to_cpu(el->l_next_free_rec); | |
4030 | if (next_free == 0 || | |
4031 | (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) { | |
4032 | u32 left_cpos; | |
4033 | ||
d401dc12 JB |
4034 | ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci), |
4035 | right_path, &left_cpos); | |
328d5752 MF |
4036 | if (ret) { |
4037 | mlog_errno(ret); | |
dcd0538f MF |
4038 | goto out; |
4039 | } | |
4040 | ||
a09d09b8 TM |
4041 | trace_ocfs2_append_rec_to_path( |
4042 | (unsigned long long) | |
4043 | ocfs2_metadata_cache_owner(et->et_ci), | |
4044 | le32_to_cpu(insert_rec->e_cpos), | |
4045 | left_cpos); | |
e48edee2 | 4046 | |
328d5752 MF |
4047 | /* |
4048 | * No need to worry if the append is already in the | |
4049 | * leftmost leaf. | |
4050 | */ | |
4051 | if (left_cpos) { | |
ffdd7a54 | 4052 | left_path = ocfs2_new_path_from_path(right_path); |
328d5752 MF |
4053 | if (!left_path) { |
4054 | ret = -ENOMEM; | |
4055 | mlog_errno(ret); | |
4056 | goto out; | |
4057 | } | |
dcd0538f | 4058 | |
d401dc12 | 4059 | ret = ocfs2_find_path(et->et_ci, left_path, |
facdb77f | 4060 | left_cpos); |
328d5752 MF |
4061 | if (ret) { |
4062 | mlog_errno(ret); | |
4063 | goto out; | |
4064 | } | |
dcd0538f | 4065 | |
328d5752 MF |
4066 | /* |
4067 | * ocfs2_insert_path() will pass the left_path to the | |
4068 | * journal for us. | |
4069 | */ | |
4070 | } | |
4071 | } | |
dcd0538f | 4072 | |
d401dc12 | 4073 | ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); |
328d5752 MF |
4074 | if (ret) { |
4075 | mlog_errno(ret); | |
4076 | goto out; | |
dcd0538f MF |
4077 | } |
4078 | ||
d401dc12 | 4079 | ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec); |
328d5752 | 4080 | |
dcd0538f MF |
4081 | *ret_left_path = left_path; |
4082 | ret = 0; | |
4083 | out: | |
4084 | if (ret != 0) | |
4085 | ocfs2_free_path(left_path); | |
4086 | ||
4087 | return ret; | |
4088 | } | |
4089 | ||
c38e52bb | 4090 | static void ocfs2_split_record(struct ocfs2_extent_tree *et, |
328d5752 MF |
4091 | struct ocfs2_path *left_path, |
4092 | struct ocfs2_path *right_path, | |
4093 | struct ocfs2_extent_rec *split_rec, | |
4094 | enum ocfs2_split_type split) | |
4095 | { | |
4096 | int index; | |
4097 | u32 cpos = le32_to_cpu(split_rec->e_cpos); | |
4098 | struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el; | |
4099 | struct ocfs2_extent_rec *rec, *tmprec; | |
4100 | ||
c19a28e1 | 4101 | right_el = path_leaf_el(right_path); |
328d5752 MF |
4102 | if (left_path) |
4103 | left_el = path_leaf_el(left_path); | |
4104 | ||
4105 | el = right_el; | |
4106 | insert_el = right_el; | |
4107 | index = ocfs2_search_extent_list(el, cpos); | |
4108 | if (index != -1) { | |
4109 | if (index == 0 && left_path) { | |
4110 | BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0])); | |
4111 | ||
4112 | /* | |
4113 | * This typically means that the record | |
4114 | * started in the left path but moved to the | |
4115 | * right as a result of rotation. We either | |
4116 | * move the existing record to the left, or we | |
4117 | * do the later insert there. | |
4118 | * | |
4119 | * In this case, the left path should always | |
4120 | * exist as the rotate code will have passed | |
4121 | * it back for a post-insert update. | |
4122 | */ | |
4123 | ||
4124 | if (split == SPLIT_LEFT) { | |
4125 | /* | |
4126 | * It's a left split. Since we know | |
4127 | * that the rotate code gave us an | |
4128 | * empty extent in the left path, we | |
4129 | * can just do the insert there. | |
4130 | */ | |
4131 | insert_el = left_el; | |
4132 | } else { | |
4133 | /* | |
4134 | * Right split - we have to move the | |
4135 | * existing record over to the left | |
4136 | * leaf. The insert will be into the | |
4137 | * newly created empty extent in the | |
4138 | * right leaf. | |
4139 | */ | |
4140 | tmprec = &right_el->l_recs[index]; | |
4141 | ocfs2_rotate_leaf(left_el, tmprec); | |
4142 | el = left_el; | |
4143 | ||
4144 | memset(tmprec, 0, sizeof(*tmprec)); | |
4145 | index = ocfs2_search_extent_list(left_el, cpos); | |
4146 | BUG_ON(index == -1); | |
4147 | } | |
4148 | } | |
4149 | } else { | |
4150 | BUG_ON(!left_path); | |
4151 | BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0])); | |
4152 | /* | |
4153 | * Left path is easy - we can just allow the insert to | |
4154 | * happen. | |
4155 | */ | |
4156 | el = left_el; | |
4157 | insert_el = left_el; | |
4158 | index = ocfs2_search_extent_list(el, cpos); | |
4159 | BUG_ON(index == -1); | |
4160 | } | |
4161 | ||
4162 | rec = &el->l_recs[index]; | |
c38e52bb JB |
4163 | ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
4164 | split, rec, split_rec); | |
328d5752 MF |
4165 | ocfs2_rotate_leaf(insert_el, split_rec); |
4166 | } | |
4167 | ||
dcd0538f | 4168 | /* |
e7d4cb6b TM |
4169 | * This function only does inserts on an allocation b-tree. For tree |
4170 | * depth = 0, ocfs2_insert_at_leaf() is called directly. | |
dcd0538f MF |
4171 | * |
4172 | * right_path is the path we want to do the actual insert | |
4173 | * in. left_path should only be passed in if we need to update that | |
4174 | * portion of the tree after an edge insert. | |
4175 | */ | |
3505bec0 | 4176 | static int ocfs2_insert_path(handle_t *handle, |
7dc02805 | 4177 | struct ocfs2_extent_tree *et, |
dcd0538f MF |
4178 | struct ocfs2_path *left_path, |
4179 | struct ocfs2_path *right_path, | |
4180 | struct ocfs2_extent_rec *insert_rec, | |
4181 | struct ocfs2_insert_type *insert) | |
4182 | { | |
4183 | int ret, subtree_index; | |
4184 | struct buffer_head *leaf_bh = path_leaf_bh(right_path); | |
dcd0538f | 4185 | |
dcd0538f | 4186 | if (left_path) { |
dcd0538f MF |
4187 | /* |
4188 | * There's a chance that left_path got passed back to | |
4189 | * us without being accounted for in the | |
4190 | * journal. Extend our transaction here to be sure we | |
4191 | * can change those blocks. | |
4192 | */ | |
c901fb00 | 4193 | ret = ocfs2_extend_trans(handle, left_path->p_tree_depth); |
dcd0538f MF |
4194 | if (ret < 0) { |
4195 | mlog_errno(ret); | |
4196 | goto out; | |
4197 | } | |
4198 | ||
7dc02805 | 4199 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
dcd0538f MF |
4200 | if (ret < 0) { |
4201 | mlog_errno(ret); | |
4202 | goto out; | |
4203 | } | |
4204 | } | |
4205 | ||
e8aed345 MF |
4206 | /* |
4207 | * Pass both paths to the journal. The majority of inserts | |
4208 | * will be touching all components anyway. | |
4209 | */ | |
7dc02805 | 4210 | ret = ocfs2_journal_access_path(et->et_ci, handle, right_path); |
e8aed345 MF |
4211 | if (ret < 0) { |
4212 | mlog_errno(ret); | |
4213 | goto out; | |
4214 | } | |
4215 | ||
328d5752 MF |
4216 | if (insert->ins_split != SPLIT_NONE) { |
4217 | /* | |
4218 | * We could call ocfs2_insert_at_leaf() for some types | |
c78bad11 | 4219 | * of splits, but it's easier to just let one separate |
328d5752 MF |
4220 | * function sort it all out. |
4221 | */ | |
c38e52bb | 4222 | ocfs2_split_record(et, left_path, right_path, |
328d5752 | 4223 | insert_rec, insert->ins_split); |
e8aed345 MF |
4224 | |
4225 | /* | |
4226 | * Split might have modified either leaf and we don't | |
4227 | * have a guarantee that the later edge insert will | |
4228 | * dirty this for us. | |
4229 | */ | |
4230 | if (left_path) | |
ec20cec7 JB |
4231 | ocfs2_journal_dirty(handle, |
4232 | path_leaf_bh(left_path)); | |
328d5752 | 4233 | } else |
d5628623 JB |
4234 | ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path), |
4235 | insert); | |
dcd0538f | 4236 | |
ec20cec7 | 4237 | ocfs2_journal_dirty(handle, leaf_bh); |
dcd0538f MF |
4238 | |
4239 | if (left_path) { | |
4240 | /* | |
4241 | * The rotate code has indicated that we need to fix | |
4242 | * up portions of the tree after the insert. | |
4243 | * | |
4244 | * XXX: Should we extend the transaction here? | |
4245 | */ | |
7dc02805 | 4246 | subtree_index = ocfs2_find_subtree_root(et, left_path, |
dcd0538f | 4247 | right_path); |
4619c73e JB |
4248 | ocfs2_complete_edge_insert(handle, left_path, right_path, |
4249 | subtree_index); | |
dcd0538f MF |
4250 | } |
4251 | ||
4252 | ret = 0; | |
4253 | out: | |
4254 | return ret; | |
4255 | } | |
4256 | ||
3505bec0 | 4257 | static int ocfs2_do_insert_extent(handle_t *handle, |
e7d4cb6b | 4258 | struct ocfs2_extent_tree *et, |
dcd0538f MF |
4259 | struct ocfs2_extent_rec *insert_rec, |
4260 | struct ocfs2_insert_type *type) | |
4261 | { | |
4262 | int ret, rotate = 0; | |
4263 | u32 cpos; | |
4264 | struct ocfs2_path *right_path = NULL; | |
4265 | struct ocfs2_path *left_path = NULL; | |
dcd0538f MF |
4266 | struct ocfs2_extent_list *el; |
4267 | ||
ce1d9ea6 | 4268 | el = et->et_root_el; |
dcd0538f | 4269 | |
d9a0a1f8 | 4270 | ret = ocfs2_et_root_journal_access(handle, et, |
13723d00 | 4271 | OCFS2_JOURNAL_ACCESS_WRITE); |
dcd0538f MF |
4272 | if (ret) { |
4273 | mlog_errno(ret); | |
4274 | goto out; | |
4275 | } | |
4276 | ||
4277 | if (le16_to_cpu(el->l_tree_depth) == 0) { | |
d5628623 | 4278 | ocfs2_insert_at_leaf(et, insert_rec, el, type); |
dcd0538f MF |
4279 | goto out_update_clusters; |
4280 | } | |
4281 | ||
ffdd7a54 | 4282 | right_path = ocfs2_new_path_from_et(et); |
dcd0538f MF |
4283 | if (!right_path) { |
4284 | ret = -ENOMEM; | |
4285 | mlog_errno(ret); | |
4286 | goto out; | |
4287 | } | |
4288 | ||
4289 | /* | |
4290 | * Determine the path to start with. Rotations need the | |
4291 | * rightmost path, everything else can go directly to the | |
4292 | * target leaf. | |
4293 | */ | |
4294 | cpos = le32_to_cpu(insert_rec->e_cpos); | |
4295 | if (type->ins_appending == APPEND_NONE && | |
4296 | type->ins_contig == CONTIG_NONE) { | |
4297 | rotate = 1; | |
4298 | cpos = UINT_MAX; | |
4299 | } | |
4300 | ||
facdb77f | 4301 | ret = ocfs2_find_path(et->et_ci, right_path, cpos); |
dcd0538f MF |
4302 | if (ret) { |
4303 | mlog_errno(ret); | |
4304 | goto out; | |
4305 | } | |
4306 | ||
4307 | /* | |
4308 | * Rotations and appends need special treatment - they modify | |
4309 | * parts of the tree's above them. | |
4310 | * | |
4311 | * Both might pass back a path immediate to the left of the | |
4312 | * one being inserted to. This will be cause | |
4313 | * ocfs2_insert_path() to modify the rightmost records of | |
4314 | * left_path to account for an edge insert. | |
4315 | * | |
4316 | * XXX: When modifying this code, keep in mind that an insert | |
4317 | * can wind up skipping both of these two special cases... | |
4318 | */ | |
4319 | if (rotate) { | |
1bbf0b8d | 4320 | ret = ocfs2_rotate_tree_right(handle, et, type->ins_split, |
dcd0538f MF |
4321 | le32_to_cpu(insert_rec->e_cpos), |
4322 | right_path, &left_path); | |
4323 | if (ret) { | |
4324 | mlog_errno(ret); | |
4325 | goto out; | |
4326 | } | |
e8aed345 MF |
4327 | |
4328 | /* | |
4329 | * ocfs2_rotate_tree_right() might have extended the | |
4330 | * transaction without re-journaling our tree root. | |
4331 | */ | |
d9a0a1f8 | 4332 | ret = ocfs2_et_root_journal_access(handle, et, |
13723d00 | 4333 | OCFS2_JOURNAL_ACCESS_WRITE); |
e8aed345 MF |
4334 | if (ret) { |
4335 | mlog_errno(ret); | |
4336 | goto out; | |
4337 | } | |
dcd0538f MF |
4338 | } else if (type->ins_appending == APPEND_TAIL |
4339 | && type->ins_contig != CONTIG_LEFT) { | |
d401dc12 | 4340 | ret = ocfs2_append_rec_to_path(handle, et, insert_rec, |
dcd0538f MF |
4341 | right_path, &left_path); |
4342 | if (ret) { | |
4343 | mlog_errno(ret); | |
4344 | goto out; | |
4345 | } | |
4346 | } | |
4347 | ||
3505bec0 | 4348 | ret = ocfs2_insert_path(handle, et, left_path, right_path, |
dcd0538f MF |
4349 | insert_rec, type); |
4350 | if (ret) { | |
4351 | mlog_errno(ret); | |
4352 | goto out; | |
4353 | } | |
4354 | ||
4355 | out_update_clusters: | |
328d5752 | 4356 | if (type->ins_split == SPLIT_NONE) |
6136ca5f | 4357 | ocfs2_et_update_clusters(et, |
35dc0aa3 | 4358 | le16_to_cpu(insert_rec->e_leaf_clusters)); |
dcd0538f | 4359 | |
ec20cec7 | 4360 | ocfs2_journal_dirty(handle, et->et_root_bh); |
dcd0538f MF |
4361 | |
4362 | out: | |
4363 | ocfs2_free_path(left_path); | |
4364 | ocfs2_free_path(right_path); | |
4365 | ||
4366 | return ret; | |
4367 | } | |
4368 | ||
9f99ad08 | 4369 | static int ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et, |
a2970291 | 4370 | struct ocfs2_path *path, |
328d5752 | 4371 | struct ocfs2_extent_list *el, int index, |
9f99ad08 X |
4372 | struct ocfs2_extent_rec *split_rec, |
4373 | struct ocfs2_merge_ctxt *ctxt) | |
328d5752 | 4374 | { |
9f99ad08 | 4375 | int status = 0; |
328d5752 | 4376 | enum ocfs2_contig_type ret = CONTIG_NONE; |
ad5a4d70 TM |
4377 | u32 left_cpos, right_cpos; |
4378 | struct ocfs2_extent_rec *rec = NULL; | |
4379 | struct ocfs2_extent_list *new_el; | |
4380 | struct ocfs2_path *left_path = NULL, *right_path = NULL; | |
4381 | struct buffer_head *bh; | |
4382 | struct ocfs2_extent_block *eb; | |
a2970291 | 4383 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
ad5a4d70 TM |
4384 | |
4385 | if (index > 0) { | |
4386 | rec = &el->l_recs[index - 1]; | |
4387 | } else if (path->p_tree_depth > 0) { | |
a2970291 | 4388 | status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos); |
ad5a4d70 | 4389 | if (status) |
06a269cc | 4390 | goto exit; |
ad5a4d70 TM |
4391 | |
4392 | if (left_cpos != 0) { | |
ffdd7a54 | 4393 | left_path = ocfs2_new_path_from_path(path); |
9f99ad08 X |
4394 | if (!left_path) { |
4395 | status = -ENOMEM; | |
4396 | mlog_errno(status); | |
06a269cc | 4397 | goto exit; |
9f99ad08 | 4398 | } |
ad5a4d70 | 4399 | |
a2970291 JB |
4400 | status = ocfs2_find_path(et->et_ci, left_path, |
4401 | left_cpos); | |
ad5a4d70 | 4402 | if (status) |
06a269cc | 4403 | goto free_left_path; |
ad5a4d70 TM |
4404 | |
4405 | new_el = path_leaf_el(left_path); | |
4406 | ||
4407 | if (le16_to_cpu(new_el->l_next_free_rec) != | |
4408 | le16_to_cpu(new_el->l_count)) { | |
4409 | bh = path_leaf_bh(left_path); | |
4410 | eb = (struct ocfs2_extent_block *)bh->b_data; | |
93f5920d JP |
4411 | status = ocfs2_error(sb, |
4412 | "Extent block #%llu has an invalid l_next_free_rec of %d. It should have matched the l_count of %d\n", | |
4413 | (unsigned long long)le64_to_cpu(eb->h_blkno), | |
4414 | le16_to_cpu(new_el->l_next_free_rec), | |
4415 | le16_to_cpu(new_el->l_count)); | |
06a269cc | 4416 | goto free_left_path; |
ad5a4d70 TM |
4417 | } |
4418 | rec = &new_el->l_recs[ | |
4419 | le16_to_cpu(new_el->l_next_free_rec) - 1]; | |
4420 | } | |
4421 | } | |
328d5752 MF |
4422 | |
4423 | /* | |
4424 | * We're careful to check for an empty extent record here - | |
4425 | * the merge code will know what to do if it sees one. | |
4426 | */ | |
ad5a4d70 | 4427 | if (rec) { |
328d5752 MF |
4428 | if (index == 1 && ocfs2_is_empty_extent(rec)) { |
4429 | if (split_rec->e_cpos == el->l_recs[index].e_cpos) | |
4430 | ret = CONTIG_RIGHT; | |
4431 | } else { | |
853a3a14 | 4432 | ret = ocfs2_et_extent_contig(et, rec, split_rec); |
328d5752 MF |
4433 | } |
4434 | } | |
4435 | ||
ad5a4d70 TM |
4436 | rec = NULL; |
4437 | if (index < (le16_to_cpu(el->l_next_free_rec) - 1)) | |
4438 | rec = &el->l_recs[index + 1]; | |
4439 | else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) && | |
4440 | path->p_tree_depth > 0) { | |
a2970291 | 4441 | status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos); |
ad5a4d70 | 4442 | if (status) |
06a269cc | 4443 | goto free_left_path; |
ad5a4d70 TM |
4444 | |
4445 | if (right_cpos == 0) | |
06a269cc | 4446 | goto free_left_path; |
ad5a4d70 | 4447 | |
ffdd7a54 | 4448 | right_path = ocfs2_new_path_from_path(path); |
9f99ad08 X |
4449 | if (!right_path) { |
4450 | status = -ENOMEM; | |
4451 | mlog_errno(status); | |
06a269cc | 4452 | goto free_left_path; |
9f99ad08 | 4453 | } |
ad5a4d70 | 4454 | |
a2970291 | 4455 | status = ocfs2_find_path(et->et_ci, right_path, right_cpos); |
ad5a4d70 | 4456 | if (status) |
06a269cc | 4457 | goto free_right_path; |
ad5a4d70 TM |
4458 | |
4459 | new_el = path_leaf_el(right_path); | |
4460 | rec = &new_el->l_recs[0]; | |
4461 | if (ocfs2_is_empty_extent(rec)) { | |
4462 | if (le16_to_cpu(new_el->l_next_free_rec) <= 1) { | |
4463 | bh = path_leaf_bh(right_path); | |
4464 | eb = (struct ocfs2_extent_block *)bh->b_data; | |
93f5920d JP |
4465 | status = ocfs2_error(sb, |
4466 | "Extent block #%llu has an invalid l_next_free_rec of %d\n", | |
4467 | (unsigned long long)le64_to_cpu(eb->h_blkno), | |
4468 | le16_to_cpu(new_el->l_next_free_rec)); | |
06a269cc | 4469 | goto free_right_path; |
ad5a4d70 TM |
4470 | } |
4471 | rec = &new_el->l_recs[1]; | |
4472 | } | |
4473 | } | |
4474 | ||
4475 | if (rec) { | |
328d5752 MF |
4476 | enum ocfs2_contig_type contig_type; |
4477 | ||
853a3a14 | 4478 | contig_type = ocfs2_et_extent_contig(et, rec, split_rec); |
328d5752 MF |
4479 | |
4480 | if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT) | |
4481 | ret = CONTIG_LEFTRIGHT; | |
4482 | else if (ret == CONTIG_NONE) | |
4483 | ret = contig_type; | |
4484 | } | |
4485 | ||
06a269cc | 4486 | free_right_path: |
fd90d4df | 4487 | ocfs2_free_path(right_path); |
06a269cc ME |
4488 | free_left_path: |
4489 | ocfs2_free_path(left_path); | |
4490 | exit: | |
9f99ad08 X |
4491 | if (status == 0) |
4492 | ctxt->c_contig_type = ret; | |
4493 | ||
4494 | return status; | |
328d5752 MF |
4495 | } |
4496 | ||
1ef61b33 | 4497 | static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et, |
dcd0538f MF |
4498 | struct ocfs2_insert_type *insert, |
4499 | struct ocfs2_extent_list *el, | |
1ef61b33 | 4500 | struct ocfs2_extent_rec *insert_rec) |
dcd0538f MF |
4501 | { |
4502 | int i; | |
4503 | enum ocfs2_contig_type contig_type = CONTIG_NONE; | |
4504 | ||
e48edee2 MF |
4505 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
4506 | ||
dcd0538f | 4507 | for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) { |
853a3a14 TM |
4508 | contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i], |
4509 | insert_rec); | |
dcd0538f MF |
4510 | if (contig_type != CONTIG_NONE) { |
4511 | insert->ins_contig_index = i; | |
4512 | break; | |
4513 | } | |
4514 | } | |
4515 | insert->ins_contig = contig_type; | |
ca12b7c4 TM |
4516 | |
4517 | if (insert->ins_contig != CONTIG_NONE) { | |
4518 | struct ocfs2_extent_rec *rec = | |
4519 | &el->l_recs[insert->ins_contig_index]; | |
4520 | unsigned int len = le16_to_cpu(rec->e_leaf_clusters) + | |
4521 | le16_to_cpu(insert_rec->e_leaf_clusters); | |
4522 | ||
4523 | /* | |
4524 | * Caller might want us to limit the size of extents, don't | |
4525 | * calculate contiguousness if we might exceed that limit. | |
4526 | */ | |
ce1d9ea6 JB |
4527 | if (et->et_max_leaf_clusters && |
4528 | (len > et->et_max_leaf_clusters)) | |
ca12b7c4 TM |
4529 | insert->ins_contig = CONTIG_NONE; |
4530 | } | |
dcd0538f MF |
4531 | } |
4532 | ||
4533 | /* | |
4534 | * This should only be called against the righmost leaf extent list. | |
4535 | * | |
4536 | * ocfs2_figure_appending_type() will figure out whether we'll have to | |
4537 | * insert at the tail of the rightmost leaf. | |
4538 | * | |
e7d4cb6b TM |
4539 | * This should also work against the root extent list for tree's with 0 |
4540 | * depth. If we consider the root extent list to be the rightmost leaf node | |
dcd0538f MF |
4541 | * then the logic here makes sense. |
4542 | */ | |
4543 | static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert, | |
4544 | struct ocfs2_extent_list *el, | |
4545 | struct ocfs2_extent_rec *insert_rec) | |
4546 | { | |
4547 | int i; | |
4548 | u32 cpos = le32_to_cpu(insert_rec->e_cpos); | |
4549 | struct ocfs2_extent_rec *rec; | |
4550 | ||
4551 | insert->ins_appending = APPEND_NONE; | |
4552 | ||
e48edee2 | 4553 | BUG_ON(le16_to_cpu(el->l_tree_depth) != 0); |
dcd0538f MF |
4554 | |
4555 | if (!el->l_next_free_rec) | |
4556 | goto set_tail_append; | |
4557 | ||
4558 | if (ocfs2_is_empty_extent(&el->l_recs[0])) { | |
4559 | /* Were all records empty? */ | |
4560 | if (le16_to_cpu(el->l_next_free_rec) == 1) | |
4561 | goto set_tail_append; | |
4562 | } | |
4563 | ||
4564 | i = le16_to_cpu(el->l_next_free_rec) - 1; | |
4565 | rec = &el->l_recs[i]; | |
4566 | ||
e48edee2 MF |
4567 | if (cpos >= |
4568 | (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters))) | |
dcd0538f MF |
4569 | goto set_tail_append; |
4570 | ||
4571 | return; | |
4572 | ||
4573 | set_tail_append: | |
4574 | insert->ins_appending = APPEND_TAIL; | |
4575 | } | |
4576 | ||
4577 | /* | |
25985edc | 4578 | * Helper function called at the beginning of an insert. |
dcd0538f MF |
4579 | * |
4580 | * This computes a few things that are commonly used in the process of | |
4581 | * inserting into the btree: | |
4582 | * - Whether the new extent is contiguous with an existing one. | |
4583 | * - The current tree depth. | |
4584 | * - Whether the insert is an appending one. | |
4585 | * - The total # of free records in the tree. | |
4586 | * | |
4587 | * All of the information is stored on the ocfs2_insert_type | |
4588 | * structure. | |
4589 | */ | |
627961b7 | 4590 | static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et, |
dcd0538f MF |
4591 | struct buffer_head **last_eb_bh, |
4592 | struct ocfs2_extent_rec *insert_rec, | |
c77534f6 | 4593 | int *free_records, |
dcd0538f MF |
4594 | struct ocfs2_insert_type *insert) |
4595 | { | |
4596 | int ret; | |
dcd0538f MF |
4597 | struct ocfs2_extent_block *eb; |
4598 | struct ocfs2_extent_list *el; | |
4599 | struct ocfs2_path *path = NULL; | |
4600 | struct buffer_head *bh = NULL; | |
4601 | ||
328d5752 MF |
4602 | insert->ins_split = SPLIT_NONE; |
4603 | ||
ce1d9ea6 | 4604 | el = et->et_root_el; |
dcd0538f MF |
4605 | insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth); |
4606 | ||
4607 | if (el->l_tree_depth) { | |
4608 | /* | |
4609 | * If we have tree depth, we read in the | |
4610 | * rightmost extent block ahead of time as | |
4611 | * ocfs2_figure_insert_type() and ocfs2_add_branch() | |
4612 | * may want it later. | |
4613 | */ | |
3d03a305 | 4614 | ret = ocfs2_read_extent_block(et->et_ci, |
5e96581a JB |
4615 | ocfs2_et_get_last_eb_blk(et), |
4616 | &bh); | |
dcd0538f | 4617 | if (ret) { |
c1e8d35e | 4618 | mlog_errno(ret); |
dcd0538f MF |
4619 | goto out; |
4620 | } | |
ccd979bd | 4621 | eb = (struct ocfs2_extent_block *) bh->b_data; |
ccd979bd | 4622 | el = &eb->h_list; |
dcd0538f | 4623 | } |
ccd979bd | 4624 | |
dcd0538f MF |
4625 | /* |
4626 | * Unless we have a contiguous insert, we'll need to know if | |
4627 | * there is room left in our allocation tree for another | |
4628 | * extent record. | |
4629 | * | |
4630 | * XXX: This test is simplistic, we can search for empty | |
4631 | * extent records too. | |
4632 | */ | |
c77534f6 | 4633 | *free_records = le16_to_cpu(el->l_count) - |
dcd0538f MF |
4634 | le16_to_cpu(el->l_next_free_rec); |
4635 | ||
4636 | if (!insert->ins_tree_depth) { | |
1ef61b33 | 4637 | ocfs2_figure_contig_type(et, insert, el, insert_rec); |
dcd0538f MF |
4638 | ocfs2_figure_appending_type(insert, el, insert_rec); |
4639 | return 0; | |
ccd979bd MF |
4640 | } |
4641 | ||
ffdd7a54 | 4642 | path = ocfs2_new_path_from_et(et); |
dcd0538f MF |
4643 | if (!path) { |
4644 | ret = -ENOMEM; | |
4645 | mlog_errno(ret); | |
4646 | goto out; | |
4647 | } | |
ccd979bd | 4648 | |
dcd0538f MF |
4649 | /* |
4650 | * In the case that we're inserting past what the tree | |
4651 | * currently accounts for, ocfs2_find_path() will return for | |
4652 | * us the rightmost tree path. This is accounted for below in | |
4653 | * the appending code. | |
4654 | */ | |
facdb77f | 4655 | ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos)); |
dcd0538f MF |
4656 | if (ret) { |
4657 | mlog_errno(ret); | |
4658 | goto out; | |
4659 | } | |
ccd979bd | 4660 | |
dcd0538f MF |
4661 | el = path_leaf_el(path); |
4662 | ||
4663 | /* | |
4664 | * Now that we have the path, there's two things we want to determine: | |
4665 | * 1) Contiguousness (also set contig_index if this is so) | |
4666 | * | |
4667 | * 2) Are we doing an append? We can trivially break this up | |
4668 | * into two types of appends: simple record append, or a | |
4669 | * rotate inside the tail leaf. | |
4670 | */ | |
1ef61b33 | 4671 | ocfs2_figure_contig_type(et, insert, el, insert_rec); |
dcd0538f MF |
4672 | |
4673 | /* | |
4674 | * The insert code isn't quite ready to deal with all cases of | |
4675 | * left contiguousness. Specifically, if it's an insert into | |
4676 | * the 1st record in a leaf, it will require the adjustment of | |
e48edee2 | 4677 | * cluster count on the last record of the path directly to it's |
dcd0538f MF |
4678 | * left. For now, just catch that case and fool the layers |
4679 | * above us. This works just fine for tree_depth == 0, which | |
4680 | * is why we allow that above. | |
4681 | */ | |
4682 | if (insert->ins_contig == CONTIG_LEFT && | |
4683 | insert->ins_contig_index == 0) | |
4684 | insert->ins_contig = CONTIG_NONE; | |
4685 | ||
4686 | /* | |
4687 | * Ok, so we can simply compare against last_eb to figure out | |
4688 | * whether the path doesn't exist. This will only happen in | |
4689 | * the case that we're doing a tail append, so maybe we can | |
4690 | * take advantage of that information somehow. | |
4691 | */ | |
35dc0aa3 | 4692 | if (ocfs2_et_get_last_eb_blk(et) == |
e7d4cb6b | 4693 | path_leaf_bh(path)->b_blocknr) { |
dcd0538f MF |
4694 | /* |
4695 | * Ok, ocfs2_find_path() returned us the rightmost | |
4696 | * tree path. This might be an appending insert. There are | |
4697 | * two cases: | |
4698 | * 1) We're doing a true append at the tail: | |
4699 | * -This might even be off the end of the leaf | |
4700 | * 2) We're "appending" by rotating in the tail | |
4701 | */ | |
4702 | ocfs2_figure_appending_type(insert, el, insert_rec); | |
4703 | } | |
4704 | ||
4705 | out: | |
4706 | ocfs2_free_path(path); | |
4707 | ||
4708 | if (ret == 0) | |
4709 | *last_eb_bh = bh; | |
4710 | else | |
4711 | brelse(bh); | |
4712 | return ret; | |
ccd979bd MF |
4713 | } |
4714 | ||
dcd0538f | 4715 | /* |
cc79d8c1 | 4716 | * Insert an extent into a btree. |
dcd0538f | 4717 | * |
cc79d8c1 | 4718 | * The caller needs to update the owning btree's cluster count. |
dcd0538f | 4719 | */ |
cc79d8c1 | 4720 | int ocfs2_insert_extent(handle_t *handle, |
f99b9b7c JB |
4721 | struct ocfs2_extent_tree *et, |
4722 | u32 cpos, | |
4723 | u64 start_blk, | |
4724 | u32 new_clusters, | |
4725 | u8 flags, | |
4726 | struct ocfs2_alloc_context *meta_ac) | |
ccd979bd | 4727 | { |
c3afcbb3 | 4728 | int status; |
c77534f6 | 4729 | int uninitialized_var(free_records); |
ccd979bd | 4730 | struct buffer_head *last_eb_bh = NULL; |
dcd0538f MF |
4731 | struct ocfs2_insert_type insert = {0, }; |
4732 | struct ocfs2_extent_rec rec; | |
4733 | ||
a09d09b8 TM |
4734 | trace_ocfs2_insert_extent_start( |
4735 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), | |
4736 | cpos, new_clusters); | |
dcd0538f | 4737 | |
e48edee2 | 4738 | memset(&rec, 0, sizeof(rec)); |
dcd0538f MF |
4739 | rec.e_cpos = cpu_to_le32(cpos); |
4740 | rec.e_blkno = cpu_to_le64(start_blk); | |
e48edee2 | 4741 | rec.e_leaf_clusters = cpu_to_le16(new_clusters); |
2ae99a60 | 4742 | rec.e_flags = flags; |
6136ca5f | 4743 | status = ocfs2_et_insert_check(et, &rec); |
1e61ee79 JB |
4744 | if (status) { |
4745 | mlog_errno(status); | |
4746 | goto bail; | |
4747 | } | |
dcd0538f | 4748 | |
627961b7 | 4749 | status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec, |
c77534f6 | 4750 | &free_records, &insert); |
dcd0538f MF |
4751 | if (status < 0) { |
4752 | mlog_errno(status); | |
4753 | goto bail; | |
ccd979bd MF |
4754 | } |
4755 | ||
a09d09b8 TM |
4756 | trace_ocfs2_insert_extent(insert.ins_appending, insert.ins_contig, |
4757 | insert.ins_contig_index, free_records, | |
4758 | insert.ins_tree_depth); | |
ccd979bd | 4759 | |
c77534f6 | 4760 | if (insert.ins_contig == CONTIG_NONE && free_records == 0) { |
d401dc12 | 4761 | status = ocfs2_grow_tree(handle, et, |
328d5752 | 4762 | &insert.ins_tree_depth, &last_eb_bh, |
c3afcbb3 MF |
4763 | meta_ac); |
4764 | if (status) { | |
ccd979bd MF |
4765 | mlog_errno(status); |
4766 | goto bail; | |
4767 | } | |
ccd979bd MF |
4768 | } |
4769 | ||
dcd0538f | 4770 | /* Finally, we can add clusters. This might rotate the tree for us. */ |
3505bec0 | 4771 | status = ocfs2_do_insert_extent(handle, et, &rec, &insert); |
ccd979bd MF |
4772 | if (status < 0) |
4773 | mlog_errno(status); | |
92ba470c JB |
4774 | else |
4775 | ocfs2_et_extent_map_insert(et, &rec); | |
ccd979bd MF |
4776 | |
4777 | bail: | |
a81cb88b | 4778 | brelse(last_eb_bh); |
ccd979bd | 4779 | |
f56654c4 TM |
4780 | return status; |
4781 | } | |
4782 | ||
0eb8d47e TM |
4783 | /* |
4784 | * Allcate and add clusters into the extent b-tree. | |
4785 | * The new clusters(clusters_to_add) will be inserted at logical_offset. | |
f99b9b7c | 4786 | * The extent b-tree's root is specified by et, and |
0eb8d47e TM |
4787 | * it is not limited to the file storage. Any extent tree can use this |
4788 | * function if it implements the proper ocfs2_extent_tree. | |
4789 | */ | |
cbee7e1a JB |
4790 | int ocfs2_add_clusters_in_btree(handle_t *handle, |
4791 | struct ocfs2_extent_tree *et, | |
0eb8d47e TM |
4792 | u32 *logical_offset, |
4793 | u32 clusters_to_add, | |
4794 | int mark_unwritten, | |
0eb8d47e TM |
4795 | struct ocfs2_alloc_context *data_ac, |
4796 | struct ocfs2_alloc_context *meta_ac, | |
f99b9b7c | 4797 | enum ocfs2_alloc_restarted *reason_ret) |
0eb8d47e | 4798 | { |
a09d09b8 | 4799 | int status = 0, err = 0; |
fb951eb5 | 4800 | int need_free = 0; |
0eb8d47e TM |
4801 | int free_extents; |
4802 | enum ocfs2_alloc_restarted reason = RESTART_NONE; | |
4803 | u32 bit_off, num_bits; | |
4804 | u64 block; | |
4805 | u8 flags = 0; | |
cbee7e1a JB |
4806 | struct ocfs2_super *osb = |
4807 | OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci)); | |
0eb8d47e TM |
4808 | |
4809 | BUG_ON(!clusters_to_add); | |
4810 | ||
4811 | if (mark_unwritten) | |
4812 | flags = OCFS2_EXT_UNWRITTEN; | |
4813 | ||
964f14a0 | 4814 | free_extents = ocfs2_num_free_extents(et); |
0eb8d47e TM |
4815 | if (free_extents < 0) { |
4816 | status = free_extents; | |
4817 | mlog_errno(status); | |
4818 | goto leave; | |
4819 | } | |
4820 | ||
4821 | /* there are two cases which could cause us to EAGAIN in the | |
4822 | * we-need-more-metadata case: | |
4823 | * 1) we haven't reserved *any* | |
4824 | * 2) we are so fragmented, we've needed to add metadata too | |
4825 | * many times. */ | |
4826 | if (!free_extents && !meta_ac) { | |
a09d09b8 | 4827 | err = -1; |
0eb8d47e TM |
4828 | status = -EAGAIN; |
4829 | reason = RESTART_META; | |
4830 | goto leave; | |
4831 | } else if ((!free_extents) | |
4832 | && (ocfs2_alloc_context_bits_left(meta_ac) | |
f99b9b7c | 4833 | < ocfs2_extend_meta_needed(et->et_root_el))) { |
a09d09b8 | 4834 | err = -2; |
0eb8d47e TM |
4835 | status = -EAGAIN; |
4836 | reason = RESTART_META; | |
4837 | goto leave; | |
4838 | } | |
4839 | ||
1ed9b777 | 4840 | status = __ocfs2_claim_clusters(handle, data_ac, 1, |
0eb8d47e TM |
4841 | clusters_to_add, &bit_off, &num_bits); |
4842 | if (status < 0) { | |
4843 | if (status != -ENOSPC) | |
4844 | mlog_errno(status); | |
4845 | goto leave; | |
4846 | } | |
4847 | ||
4848 | BUG_ON(num_bits > clusters_to_add); | |
4849 | ||
13723d00 | 4850 | /* reserve our write early -- insert_extent may update the tree root */ |
d9a0a1f8 | 4851 | status = ocfs2_et_root_journal_access(handle, et, |
13723d00 | 4852 | OCFS2_JOURNAL_ACCESS_WRITE); |
0eb8d47e TM |
4853 | if (status < 0) { |
4854 | mlog_errno(status); | |
fb951eb5 ZW |
4855 | need_free = 1; |
4856 | goto bail; | |
0eb8d47e TM |
4857 | } |
4858 | ||
4859 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); | |
a09d09b8 TM |
4860 | trace_ocfs2_add_clusters_in_btree( |
4861 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), | |
4862 | bit_off, num_bits); | |
cc79d8c1 | 4863 | status = ocfs2_insert_extent(handle, et, *logical_offset, block, |
f99b9b7c | 4864 | num_bits, flags, meta_ac); |
0eb8d47e TM |
4865 | if (status < 0) { |
4866 | mlog_errno(status); | |
fb951eb5 ZW |
4867 | need_free = 1; |
4868 | goto bail; | |
0eb8d47e TM |
4869 | } |
4870 | ||
ec20cec7 | 4871 | ocfs2_journal_dirty(handle, et->et_root_bh); |
0eb8d47e TM |
4872 | |
4873 | clusters_to_add -= num_bits; | |
4874 | *logical_offset += num_bits; | |
4875 | ||
4876 | if (clusters_to_add) { | |
a09d09b8 | 4877 | err = clusters_to_add; |
0eb8d47e TM |
4878 | status = -EAGAIN; |
4879 | reason = RESTART_TRANS; | |
4880 | } | |
4881 | ||
fb951eb5 ZW |
4882 | bail: |
4883 | if (need_free) { | |
4884 | if (data_ac->ac_which == OCFS2_AC_USE_LOCAL) | |
4885 | ocfs2_free_local_alloc_bits(osb, handle, data_ac, | |
4886 | bit_off, num_bits); | |
4887 | else | |
4888 | ocfs2_free_clusters(handle, | |
4889 | data_ac->ac_inode, | |
4890 | data_ac->ac_bh, | |
4891 | ocfs2_clusters_to_blocks(osb->sb, bit_off), | |
4892 | num_bits); | |
4893 | } | |
4894 | ||
0eb8d47e | 4895 | leave: |
0eb8d47e TM |
4896 | if (reason_ret) |
4897 | *reason_ret = reason; | |
a09d09b8 | 4898 | trace_ocfs2_add_clusters_in_btree_ret(status, reason, err); |
0eb8d47e TM |
4899 | return status; |
4900 | } | |
4901 | ||
328d5752 MF |
4902 | static void ocfs2_make_right_split_rec(struct super_block *sb, |
4903 | struct ocfs2_extent_rec *split_rec, | |
4904 | u32 cpos, | |
4905 | struct ocfs2_extent_rec *rec) | |
4906 | { | |
4907 | u32 rec_cpos = le32_to_cpu(rec->e_cpos); | |
4908 | u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters); | |
4909 | ||
4910 | memset(split_rec, 0, sizeof(struct ocfs2_extent_rec)); | |
4911 | ||
4912 | split_rec->e_cpos = cpu_to_le32(cpos); | |
4913 | split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos); | |
4914 | ||
4915 | split_rec->e_blkno = rec->e_blkno; | |
4916 | le64_add_cpu(&split_rec->e_blkno, | |
4917 | ocfs2_clusters_to_blocks(sb, cpos - rec_cpos)); | |
4918 | ||
4919 | split_rec->e_flags = rec->e_flags; | |
4920 | } | |
4921 | ||
d231129f | 4922 | static int ocfs2_split_and_insert(handle_t *handle, |
e7d4cb6b | 4923 | struct ocfs2_extent_tree *et, |
d231129f | 4924 | struct ocfs2_path *path, |
328d5752 MF |
4925 | struct buffer_head **last_eb_bh, |
4926 | int split_index, | |
4927 | struct ocfs2_extent_rec *orig_split_rec, | |
4928 | struct ocfs2_alloc_context *meta_ac) | |
4929 | { | |
4930 | int ret = 0, depth; | |
4931 | unsigned int insert_range, rec_range, do_leftright = 0; | |
4932 | struct ocfs2_extent_rec tmprec; | |
4933 | struct ocfs2_extent_list *rightmost_el; | |
4934 | struct ocfs2_extent_rec rec; | |
4935 | struct ocfs2_extent_rec split_rec = *orig_split_rec; | |
4936 | struct ocfs2_insert_type insert; | |
4937 | struct ocfs2_extent_block *eb; | |
328d5752 MF |
4938 | |
4939 | leftright: | |
4940 | /* | |
4941 | * Store a copy of the record on the stack - it might move | |
4942 | * around as the tree is manipulated below. | |
4943 | */ | |
4944 | rec = path_leaf_el(path)->l_recs[split_index]; | |
4945 | ||
ce1d9ea6 | 4946 | rightmost_el = et->et_root_el; |
328d5752 MF |
4947 | |
4948 | depth = le16_to_cpu(rightmost_el->l_tree_depth); | |
4949 | if (depth) { | |
4950 | BUG_ON(!(*last_eb_bh)); | |
4951 | eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data; | |
4952 | rightmost_el = &eb->h_list; | |
4953 | } | |
4954 | ||
4955 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == | |
4956 | le16_to_cpu(rightmost_el->l_count)) { | |
d401dc12 | 4957 | ret = ocfs2_grow_tree(handle, et, |
e7d4cb6b | 4958 | &depth, last_eb_bh, meta_ac); |
328d5752 MF |
4959 | if (ret) { |
4960 | mlog_errno(ret); | |
4961 | goto out; | |
4962 | } | |
328d5752 MF |
4963 | } |
4964 | ||
4965 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); | |
4966 | insert.ins_appending = APPEND_NONE; | |
4967 | insert.ins_contig = CONTIG_NONE; | |
328d5752 MF |
4968 | insert.ins_tree_depth = depth; |
4969 | ||
4970 | insert_range = le32_to_cpu(split_rec.e_cpos) + | |
4971 | le16_to_cpu(split_rec.e_leaf_clusters); | |
4972 | rec_range = le32_to_cpu(rec.e_cpos) + | |
4973 | le16_to_cpu(rec.e_leaf_clusters); | |
4974 | ||
4975 | if (split_rec.e_cpos == rec.e_cpos) { | |
4976 | insert.ins_split = SPLIT_LEFT; | |
4977 | } else if (insert_range == rec_range) { | |
4978 | insert.ins_split = SPLIT_RIGHT; | |
4979 | } else { | |
4980 | /* | |
4981 | * Left/right split. We fake this as a right split | |
4982 | * first and then make a second pass as a left split. | |
4983 | */ | |
4984 | insert.ins_split = SPLIT_RIGHT; | |
4985 | ||
d231129f JB |
4986 | ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
4987 | &tmprec, insert_range, &rec); | |
328d5752 MF |
4988 | |
4989 | split_rec = tmprec; | |
4990 | ||
4991 | BUG_ON(do_leftright); | |
4992 | do_leftright = 1; | |
4993 | } | |
4994 | ||
3505bec0 | 4995 | ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert); |
328d5752 MF |
4996 | if (ret) { |
4997 | mlog_errno(ret); | |
4998 | goto out; | |
4999 | } | |
5000 | ||
5001 | if (do_leftright == 1) { | |
5002 | u32 cpos; | |
5003 | struct ocfs2_extent_list *el; | |
5004 | ||
5005 | do_leftright++; | |
5006 | split_rec = *orig_split_rec; | |
5007 | ||
5008 | ocfs2_reinit_path(path, 1); | |
5009 | ||
5010 | cpos = le32_to_cpu(split_rec.e_cpos); | |
facdb77f | 5011 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
328d5752 MF |
5012 | if (ret) { |
5013 | mlog_errno(ret); | |
5014 | goto out; | |
5015 | } | |
5016 | ||
5017 | el = path_leaf_el(path); | |
5018 | split_index = ocfs2_search_extent_list(el, cpos); | |
981035b4 YX |
5019 | if (split_index == -1) { |
5020 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), | |
7ecef14a JP |
5021 | "Owner %llu has an extent at cpos %u which can no longer be found\n", |
5022 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), | |
5023 | cpos); | |
981035b4 YX |
5024 | ret = -EROFS; |
5025 | goto out; | |
5026 | } | |
328d5752 MF |
5027 | goto leftright; |
5028 | } | |
5029 | out: | |
5030 | ||
5031 | return ret; | |
5032 | } | |
5033 | ||
f3868d0f JB |
5034 | static int ocfs2_replace_extent_rec(handle_t *handle, |
5035 | struct ocfs2_extent_tree *et, | |
47be12e4 TM |
5036 | struct ocfs2_path *path, |
5037 | struct ocfs2_extent_list *el, | |
5038 | int split_index, | |
5039 | struct ocfs2_extent_rec *split_rec) | |
5040 | { | |
5041 | int ret; | |
5042 | ||
f3868d0f | 5043 | ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path, |
47be12e4 TM |
5044 | path_num_items(path) - 1); |
5045 | if (ret) { | |
5046 | mlog_errno(ret); | |
5047 | goto out; | |
5048 | } | |
5049 | ||
5050 | el->l_recs[split_index] = *split_rec; | |
5051 | ||
5052 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); | |
5053 | out: | |
5054 | return ret; | |
5055 | } | |
5056 | ||
328d5752 | 5057 | /* |
555936bf TM |
5058 | * Split part or all of the extent record at split_index in the leaf |
5059 | * pointed to by path. Merge with the contiguous extent record if needed. | |
328d5752 MF |
5060 | * |
5061 | * Care is taken to handle contiguousness so as to not grow the tree. | |
5062 | * | |
5063 | * meta_ac is not strictly necessary - we only truly need it if growth | |
5064 | * of the tree is required. All other cases will degrade into a less | |
5065 | * optimal tree layout. | |
5066 | * | |
e7d4cb6b TM |
5067 | * last_eb_bh should be the rightmost leaf block for any extent |
5068 | * btree. Since a split may grow the tree or a merge might shrink it, | |
5069 | * the caller cannot trust the contents of that buffer after this call. | |
328d5752 MF |
5070 | * |
5071 | * This code is optimized for readability - several passes might be | |
5072 | * made over certain portions of the tree. All of those blocks will | |
5073 | * have been brought into cache (and pinned via the journal), so the | |
5074 | * extra overhead is not expressed in terms of disk reads. | |
5075 | */ | |
e2e9f608 TM |
5076 | int ocfs2_split_extent(handle_t *handle, |
5077 | struct ocfs2_extent_tree *et, | |
5078 | struct ocfs2_path *path, | |
5079 | int split_index, | |
5080 | struct ocfs2_extent_rec *split_rec, | |
5081 | struct ocfs2_alloc_context *meta_ac, | |
5082 | struct ocfs2_cached_dealloc_ctxt *dealloc) | |
328d5752 MF |
5083 | { |
5084 | int ret = 0; | |
5085 | struct ocfs2_extent_list *el = path_leaf_el(path); | |
e8aed345 | 5086 | struct buffer_head *last_eb_bh = NULL; |
328d5752 MF |
5087 | struct ocfs2_extent_rec *rec = &el->l_recs[split_index]; |
5088 | struct ocfs2_merge_ctxt ctxt; | |
328d5752 | 5089 | |
328d5752 MF |
5090 | if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) || |
5091 | ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) < | |
5092 | (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) { | |
5093 | ret = -EIO; | |
5094 | mlog_errno(ret); | |
5095 | goto out; | |
5096 | } | |
5097 | ||
9f99ad08 X |
5098 | ret = ocfs2_figure_merge_contig_type(et, path, el, |
5099 | split_index, | |
5100 | split_rec, | |
5101 | &ctxt); | |
5102 | if (ret) { | |
5103 | mlog_errno(ret); | |
5104 | goto out; | |
5105 | } | |
328d5752 MF |
5106 | |
5107 | /* | |
5108 | * The core merge / split code wants to know how much room is | |
a1cf076b | 5109 | * left in this allocation tree, so we pass the |
328d5752 MF |
5110 | * rightmost extent list. |
5111 | */ | |
5112 | if (path->p_tree_depth) { | |
5113 | struct ocfs2_extent_block *eb; | |
328d5752 | 5114 | |
3d03a305 | 5115 | ret = ocfs2_read_extent_block(et->et_ci, |
5e96581a JB |
5116 | ocfs2_et_get_last_eb_blk(et), |
5117 | &last_eb_bh); | |
328d5752 | 5118 | if (ret) { |
c1e8d35e | 5119 | mlog_errno(ret); |
328d5752 MF |
5120 | goto out; |
5121 | } | |
5122 | ||
5123 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; | |
d22aa615 | 5124 | } |
328d5752 | 5125 | |
328d5752 MF |
5126 | if (rec->e_cpos == split_rec->e_cpos && |
5127 | rec->e_leaf_clusters == split_rec->e_leaf_clusters) | |
5128 | ctxt.c_split_covers_rec = 1; | |
5129 | else | |
5130 | ctxt.c_split_covers_rec = 0; | |
5131 | ||
5132 | ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]); | |
5133 | ||
a09d09b8 TM |
5134 | trace_ocfs2_split_extent(split_index, ctxt.c_contig_type, |
5135 | ctxt.c_has_empty_extent, | |
5136 | ctxt.c_split_covers_rec); | |
328d5752 MF |
5137 | |
5138 | if (ctxt.c_contig_type == CONTIG_NONE) { | |
5139 | if (ctxt.c_split_covers_rec) | |
f3868d0f | 5140 | ret = ocfs2_replace_extent_rec(handle, et, path, el, |
47be12e4 | 5141 | split_index, split_rec); |
328d5752 | 5142 | else |
d231129f | 5143 | ret = ocfs2_split_and_insert(handle, et, path, |
328d5752 MF |
5144 | &last_eb_bh, split_index, |
5145 | split_rec, meta_ac); | |
5146 | if (ret) | |
5147 | mlog_errno(ret); | |
5148 | } else { | |
c495dd24 | 5149 | ret = ocfs2_try_to_merge_extent(handle, et, path, |
328d5752 | 5150 | split_index, split_rec, |
c495dd24 | 5151 | dealloc, &ctxt); |
328d5752 MF |
5152 | if (ret) |
5153 | mlog_errno(ret); | |
5154 | } | |
5155 | ||
328d5752 MF |
5156 | out: |
5157 | brelse(last_eb_bh); | |
5158 | return ret; | |
5159 | } | |
5160 | ||
5161 | /* | |
555936bf TM |
5162 | * Change the flags of the already-existing extent at cpos for len clusters. |
5163 | * | |
5164 | * new_flags: the flags we want to set. | |
5165 | * clear_flags: the flags we want to clear. | |
5166 | * phys: the new physical offset we want this new extent starts from. | |
328d5752 MF |
5167 | * |
5168 | * If the existing extent is larger than the request, initiate a | |
5169 | * split. An attempt will be made at merging with adjacent extents. | |
5170 | * | |
5171 | * The caller is responsible for passing down meta_ac if we'll need it. | |
5172 | */ | |
1aa75fea TM |
5173 | int ocfs2_change_extent_flag(handle_t *handle, |
5174 | struct ocfs2_extent_tree *et, | |
5175 | u32 cpos, u32 len, u32 phys, | |
5176 | struct ocfs2_alloc_context *meta_ac, | |
5177 | struct ocfs2_cached_dealloc_ctxt *dealloc, | |
5178 | int new_flags, int clear_flags) | |
328d5752 MF |
5179 | { |
5180 | int ret, index; | |
555936bf TM |
5181 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
5182 | u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys); | |
328d5752 MF |
5183 | struct ocfs2_extent_rec split_rec; |
5184 | struct ocfs2_path *left_path = NULL; | |
5185 | struct ocfs2_extent_list *el; | |
555936bf | 5186 | struct ocfs2_extent_rec *rec; |
328d5752 | 5187 | |
ffdd7a54 | 5188 | left_path = ocfs2_new_path_from_et(et); |
328d5752 MF |
5189 | if (!left_path) { |
5190 | ret = -ENOMEM; | |
5191 | mlog_errno(ret); | |
5192 | goto out; | |
5193 | } | |
5194 | ||
facdb77f | 5195 | ret = ocfs2_find_path(et->et_ci, left_path, cpos); |
328d5752 MF |
5196 | if (ret) { |
5197 | mlog_errno(ret); | |
5198 | goto out; | |
5199 | } | |
5200 | el = path_leaf_el(left_path); | |
5201 | ||
5202 | index = ocfs2_search_extent_list(el, cpos); | |
981035b4 | 5203 | if (index == -1) { |
555936bf | 5204 | ocfs2_error(sb, |
7ecef14a JP |
5205 | "Owner %llu has an extent at cpos %u which can no longer be found\n", |
5206 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), | |
5207 | cpos); | |
328d5752 MF |
5208 | ret = -EROFS; |
5209 | goto out; | |
5210 | } | |
5211 | ||
555936bf TM |
5212 | ret = -EIO; |
5213 | rec = &el->l_recs[index]; | |
5214 | if (new_flags && (rec->e_flags & new_flags)) { | |
5215 | mlog(ML_ERROR, "Owner %llu tried to set %d flags on an " | |
86544fbd | 5216 | "extent that already had them\n", |
555936bf TM |
5217 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
5218 | new_flags); | |
5219 | goto out; | |
5220 | } | |
5221 | ||
5222 | if (clear_flags && !(rec->e_flags & clear_flags)) { | |
5223 | mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an " | |
86544fbd | 5224 | "extent that didn't have them\n", |
555936bf TM |
5225 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
5226 | clear_flags); | |
5227 | goto out; | |
5228 | } | |
5229 | ||
328d5752 MF |
5230 | memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec)); |
5231 | split_rec.e_cpos = cpu_to_le32(cpos); | |
5232 | split_rec.e_leaf_clusters = cpu_to_le16(len); | |
5233 | split_rec.e_blkno = cpu_to_le64(start_blkno); | |
555936bf TM |
5234 | split_rec.e_flags = rec->e_flags; |
5235 | if (new_flags) | |
5236 | split_rec.e_flags |= new_flags; | |
5237 | if (clear_flags) | |
5238 | split_rec.e_flags &= ~clear_flags; | |
5239 | ||
e2e9f608 TM |
5240 | ret = ocfs2_split_extent(handle, et, left_path, |
5241 | index, &split_rec, meta_ac, | |
5242 | dealloc); | |
328d5752 MF |
5243 | if (ret) |
5244 | mlog_errno(ret); | |
5245 | ||
5246 | out: | |
5247 | ocfs2_free_path(left_path); | |
5248 | return ret; | |
555936bf TM |
5249 | |
5250 | } | |
5251 | ||
5252 | /* | |
5253 | * Mark the already-existing extent at cpos as written for len clusters. | |
5254 | * This removes the unwritten extent flag. | |
5255 | * | |
5256 | * If the existing extent is larger than the request, initiate a | |
5257 | * split. An attempt will be made at merging with adjacent extents. | |
5258 | * | |
5259 | * The caller is responsible for passing down meta_ac if we'll need it. | |
5260 | */ | |
5261 | int ocfs2_mark_extent_written(struct inode *inode, | |
5262 | struct ocfs2_extent_tree *et, | |
5263 | handle_t *handle, u32 cpos, u32 len, u32 phys, | |
5264 | struct ocfs2_alloc_context *meta_ac, | |
5265 | struct ocfs2_cached_dealloc_ctxt *dealloc) | |
5266 | { | |
5267 | int ret; | |
5268 | ||
a09d09b8 TM |
5269 | trace_ocfs2_mark_extent_written( |
5270 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
5271 | cpos, len, phys); | |
555936bf TM |
5272 | |
5273 | if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) { | |
7ecef14a | 5274 | ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents that are being written to, but the feature bit is not set in the super block\n", |
555936bf TM |
5275 | (unsigned long long)OCFS2_I(inode)->ip_blkno); |
5276 | ret = -EROFS; | |
5277 | goto out; | |
5278 | } | |
5279 | ||
5280 | /* | |
5281 | * XXX: This should be fixed up so that we just re-insert the | |
5282 | * next extent records. | |
5283 | */ | |
5284 | ocfs2_et_extent_map_truncate(et, 0); | |
5285 | ||
5286 | ret = ocfs2_change_extent_flag(handle, et, cpos, | |
5287 | len, phys, meta_ac, dealloc, | |
5288 | 0, OCFS2_EXT_UNWRITTEN); | |
5289 | if (ret) | |
5290 | mlog_errno(ret); | |
5291 | ||
5292 | out: | |
5293 | return ret; | |
328d5752 MF |
5294 | } |
5295 | ||
dbdcf6a4 JB |
5296 | static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et, |
5297 | struct ocfs2_path *path, | |
d0c7d708 MF |
5298 | int index, u32 new_range, |
5299 | struct ocfs2_alloc_context *meta_ac) | |
5300 | { | |
c901fb00 | 5301 | int ret, depth, credits; |
d0c7d708 MF |
5302 | struct buffer_head *last_eb_bh = NULL; |
5303 | struct ocfs2_extent_block *eb; | |
5304 | struct ocfs2_extent_list *rightmost_el, *el; | |
5305 | struct ocfs2_extent_rec split_rec; | |
5306 | struct ocfs2_extent_rec *rec; | |
5307 | struct ocfs2_insert_type insert; | |
5308 | ||
5309 | /* | |
5310 | * Setup the record to split before we grow the tree. | |
5311 | */ | |
5312 | el = path_leaf_el(path); | |
5313 | rec = &el->l_recs[index]; | |
dbdcf6a4 JB |
5314 | ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci), |
5315 | &split_rec, new_range, rec); | |
d0c7d708 MF |
5316 | |
5317 | depth = path->p_tree_depth; | |
5318 | if (depth > 0) { | |
3d03a305 | 5319 | ret = ocfs2_read_extent_block(et->et_ci, |
5e96581a JB |
5320 | ocfs2_et_get_last_eb_blk(et), |
5321 | &last_eb_bh); | |
d0c7d708 MF |
5322 | if (ret < 0) { |
5323 | mlog_errno(ret); | |
5324 | goto out; | |
5325 | } | |
5326 | ||
5327 | eb = (struct ocfs2_extent_block *) last_eb_bh->b_data; | |
5328 | rightmost_el = &eb->h_list; | |
5329 | } else | |
5330 | rightmost_el = path_leaf_el(path); | |
5331 | ||
c901fb00 TM |
5332 | credits = path->p_tree_depth + |
5333 | ocfs2_extend_meta_needed(et->et_root_el); | |
d0c7d708 MF |
5334 | ret = ocfs2_extend_trans(handle, credits); |
5335 | if (ret) { | |
5336 | mlog_errno(ret); | |
5337 | goto out; | |
5338 | } | |
5339 | ||
5340 | if (le16_to_cpu(rightmost_el->l_next_free_rec) == | |
5341 | le16_to_cpu(rightmost_el->l_count)) { | |
d401dc12 | 5342 | ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh, |
d0c7d708 MF |
5343 | meta_ac); |
5344 | if (ret) { | |
5345 | mlog_errno(ret); | |
5346 | goto out; | |
5347 | } | |
d0c7d708 MF |
5348 | } |
5349 | ||
5350 | memset(&insert, 0, sizeof(struct ocfs2_insert_type)); | |
5351 | insert.ins_appending = APPEND_NONE; | |
5352 | insert.ins_contig = CONTIG_NONE; | |
5353 | insert.ins_split = SPLIT_RIGHT; | |
d0c7d708 MF |
5354 | insert.ins_tree_depth = depth; |
5355 | ||
3505bec0 | 5356 | ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert); |
d0c7d708 MF |
5357 | if (ret) |
5358 | mlog_errno(ret); | |
5359 | ||
5360 | out: | |
5361 | brelse(last_eb_bh); | |
5362 | return ret; | |
5363 | } | |
5364 | ||
043beebb JB |
5365 | static int ocfs2_truncate_rec(handle_t *handle, |
5366 | struct ocfs2_extent_tree *et, | |
d0c7d708 MF |
5367 | struct ocfs2_path *path, int index, |
5368 | struct ocfs2_cached_dealloc_ctxt *dealloc, | |
043beebb | 5369 | u32 cpos, u32 len) |
d0c7d708 MF |
5370 | { |
5371 | int ret; | |
5372 | u32 left_cpos, rec_range, trunc_range; | |
c14688ea | 5373 | int is_rightmost_tree_rec = 0; |
043beebb | 5374 | struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci); |
d0c7d708 MF |
5375 | struct ocfs2_path *left_path = NULL; |
5376 | struct ocfs2_extent_list *el = path_leaf_el(path); | |
5377 | struct ocfs2_extent_rec *rec; | |
5378 | struct ocfs2_extent_block *eb; | |
5379 | ||
5380 | if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) { | |
17215989 X |
5381 | /* extend credit for ocfs2_remove_rightmost_path */ |
5382 | ret = ocfs2_extend_rotate_transaction(handle, 0, | |
5383 | handle->h_buffer_credits, | |
5384 | path); | |
5385 | if (ret) { | |
5386 | mlog_errno(ret); | |
5387 | goto out; | |
5388 | } | |
5389 | ||
70f18c08 | 5390 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
d0c7d708 MF |
5391 | if (ret) { |
5392 | mlog_errno(ret); | |
5393 | goto out; | |
5394 | } | |
5395 | ||
5396 | index--; | |
5397 | } | |
5398 | ||
5399 | if (index == (le16_to_cpu(el->l_next_free_rec) - 1) && | |
5400 | path->p_tree_depth) { | |
5401 | /* | |
5402 | * Check whether this is the rightmost tree record. If | |
5403 | * we remove all of this record or part of its right | |
5404 | * edge then an update of the record lengths above it | |
5405 | * will be required. | |
5406 | */ | |
5407 | eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data; | |
5408 | if (eb->h_next_leaf_blk == 0) | |
5409 | is_rightmost_tree_rec = 1; | |
5410 | } | |
5411 | ||
5412 | rec = &el->l_recs[index]; | |
5413 | if (index == 0 && path->p_tree_depth && | |
5414 | le32_to_cpu(rec->e_cpos) == cpos) { | |
5415 | /* | |
5416 | * Changing the leftmost offset (via partial or whole | |
5417 | * record truncate) of an interior (or rightmost) path | |
5418 | * means we have to update the subtree that is formed | |
5419 | * by this leaf and the one to it's left. | |
5420 | * | |
5421 | * There are two cases we can skip: | |
043beebb | 5422 | * 1) Path is the leftmost one in our btree. |
d0c7d708 MF |
5423 | * 2) The leaf is rightmost and will be empty after |
5424 | * we remove the extent record - the rotate code | |
5425 | * knows how to update the newly formed edge. | |
5426 | */ | |
5427 | ||
043beebb | 5428 | ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos); |
d0c7d708 MF |
5429 | if (ret) { |
5430 | mlog_errno(ret); | |
5431 | goto out; | |
5432 | } | |
5433 | ||
5434 | if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) { | |
ffdd7a54 | 5435 | left_path = ocfs2_new_path_from_path(path); |
d0c7d708 MF |
5436 | if (!left_path) { |
5437 | ret = -ENOMEM; | |
5438 | mlog_errno(ret); | |
5439 | goto out; | |
5440 | } | |
5441 | ||
facdb77f JB |
5442 | ret = ocfs2_find_path(et->et_ci, left_path, |
5443 | left_cpos); | |
d0c7d708 MF |
5444 | if (ret) { |
5445 | mlog_errno(ret); | |
5446 | goto out; | |
5447 | } | |
5448 | } | |
5449 | } | |
5450 | ||
5451 | ret = ocfs2_extend_rotate_transaction(handle, 0, | |
5452 | handle->h_buffer_credits, | |
5453 | path); | |
5454 | if (ret) { | |
5455 | mlog_errno(ret); | |
5456 | goto out; | |
5457 | } | |
5458 | ||
d9a0a1f8 | 5459 | ret = ocfs2_journal_access_path(et->et_ci, handle, path); |
d0c7d708 MF |
5460 | if (ret) { |
5461 | mlog_errno(ret); | |
5462 | goto out; | |
5463 | } | |
5464 | ||
d9a0a1f8 | 5465 | ret = ocfs2_journal_access_path(et->et_ci, handle, left_path); |
d0c7d708 MF |
5466 | if (ret) { |
5467 | mlog_errno(ret); | |
5468 | goto out; | |
5469 | } | |
5470 | ||
5471 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); | |
5472 | trunc_range = cpos + len; | |
5473 | ||
5474 | if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) { | |
5475 | int next_free; | |
5476 | ||
5477 | memset(rec, 0, sizeof(*rec)); | |
5478 | ocfs2_cleanup_merge(el, index); | |
d0c7d708 MF |
5479 | |
5480 | next_free = le16_to_cpu(el->l_next_free_rec); | |
5481 | if (is_rightmost_tree_rec && next_free > 1) { | |
5482 | /* | |
5483 | * We skip the edge update if this path will | |
5484 | * be deleted by the rotate code. | |
5485 | */ | |
5486 | rec = &el->l_recs[next_free - 1]; | |
d401dc12 | 5487 | ocfs2_adjust_rightmost_records(handle, et, path, |
d0c7d708 MF |
5488 | rec); |
5489 | } | |
5490 | } else if (le32_to_cpu(rec->e_cpos) == cpos) { | |
5491 | /* Remove leftmost portion of the record. */ | |
5492 | le32_add_cpu(&rec->e_cpos, len); | |
5493 | le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len)); | |
5494 | le16_add_cpu(&rec->e_leaf_clusters, -len); | |
5495 | } else if (rec_range == trunc_range) { | |
5496 | /* Remove rightmost portion of the record */ | |
5497 | le16_add_cpu(&rec->e_leaf_clusters, -len); | |
5498 | if (is_rightmost_tree_rec) | |
d401dc12 | 5499 | ocfs2_adjust_rightmost_records(handle, et, path, rec); |
d0c7d708 MF |
5500 | } else { |
5501 | /* Caller should have trapped this. */ | |
043beebb JB |
5502 | mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) " |
5503 | "(%u, %u)\n", | |
5504 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), | |
d0c7d708 MF |
5505 | le32_to_cpu(rec->e_cpos), |
5506 | le16_to_cpu(rec->e_leaf_clusters), cpos, len); | |
5507 | BUG(); | |
5508 | } | |
5509 | ||
5510 | if (left_path) { | |
5511 | int subtree_index; | |
5512 | ||
7dc02805 | 5513 | subtree_index = ocfs2_find_subtree_root(et, left_path, path); |
4619c73e | 5514 | ocfs2_complete_edge_insert(handle, left_path, path, |
d0c7d708 MF |
5515 | subtree_index); |
5516 | } | |
5517 | ||
5518 | ocfs2_journal_dirty(handle, path_leaf_bh(path)); | |
5519 | ||
70f18c08 | 5520 | ret = ocfs2_rotate_tree_left(handle, et, path, dealloc); |
d0c7d708 MF |
5521 | if (ret) { |
5522 | mlog_errno(ret); | |
5523 | goto out; | |
5524 | } | |
5525 | ||
5526 | out: | |
5527 | ocfs2_free_path(left_path); | |
5528 | return ret; | |
5529 | } | |
5530 | ||
dbdcf6a4 | 5531 | int ocfs2_remove_extent(handle_t *handle, |
f99b9b7c | 5532 | struct ocfs2_extent_tree *et, |
dbdcf6a4 | 5533 | u32 cpos, u32 len, |
063c4561 | 5534 | struct ocfs2_alloc_context *meta_ac, |
f99b9b7c | 5535 | struct ocfs2_cached_dealloc_ctxt *dealloc) |
d0c7d708 MF |
5536 | { |
5537 | int ret, index; | |
5538 | u32 rec_range, trunc_range; | |
5539 | struct ocfs2_extent_rec *rec; | |
5540 | struct ocfs2_extent_list *el; | |
e7d4cb6b | 5541 | struct ocfs2_path *path = NULL; |
d0c7d708 | 5542 | |
4c911eef JB |
5543 | /* |
5544 | * XXX: Why are we truncating to 0 instead of wherever this | |
5545 | * affects us? | |
5546 | */ | |
5547 | ocfs2_et_extent_map_truncate(et, 0); | |
d0c7d708 | 5548 | |
ffdd7a54 | 5549 | path = ocfs2_new_path_from_et(et); |
d0c7d708 MF |
5550 | if (!path) { |
5551 | ret = -ENOMEM; | |
5552 | mlog_errno(ret); | |
5553 | goto out; | |
5554 | } | |
5555 | ||
facdb77f | 5556 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
d0c7d708 MF |
5557 | if (ret) { |
5558 | mlog_errno(ret); | |
5559 | goto out; | |
5560 | } | |
5561 | ||
5562 | el = path_leaf_el(path); | |
5563 | index = ocfs2_search_extent_list(el, cpos); | |
981035b4 | 5564 | if (index == -1) { |
dbdcf6a4 | 5565 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
7ecef14a | 5566 | "Owner %llu has an extent at cpos %u which can no longer be found\n", |
dbdcf6a4 JB |
5567 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
5568 | cpos); | |
d0c7d708 MF |
5569 | ret = -EROFS; |
5570 | goto out; | |
5571 | } | |
5572 | ||
5573 | /* | |
5574 | * We have 3 cases of extent removal: | |
5575 | * 1) Range covers the entire extent rec | |
5576 | * 2) Range begins or ends on one edge of the extent rec | |
5577 | * 3) Range is in the middle of the extent rec (no shared edges) | |
5578 | * | |
5579 | * For case 1 we remove the extent rec and left rotate to | |
5580 | * fill the hole. | |
5581 | * | |
5582 | * For case 2 we just shrink the existing extent rec, with a | |
5583 | * tree update if the shrinking edge is also the edge of an | |
5584 | * extent block. | |
5585 | * | |
5586 | * For case 3 we do a right split to turn the extent rec into | |
5587 | * something case 2 can handle. | |
5588 | */ | |
5589 | rec = &el->l_recs[index]; | |
5590 | rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); | |
5591 | trunc_range = cpos + len; | |
5592 | ||
5593 | BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range); | |
5594 | ||
a09d09b8 TM |
5595 | trace_ocfs2_remove_extent( |
5596 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), | |
5597 | cpos, len, index, le32_to_cpu(rec->e_cpos), | |
5598 | ocfs2_rec_clusters(el, rec)); | |
d0c7d708 MF |
5599 | |
5600 | if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) { | |
043beebb JB |
5601 | ret = ocfs2_truncate_rec(handle, et, path, index, dealloc, |
5602 | cpos, len); | |
d0c7d708 MF |
5603 | if (ret) { |
5604 | mlog_errno(ret); | |
5605 | goto out; | |
5606 | } | |
5607 | } else { | |
dbdcf6a4 | 5608 | ret = ocfs2_split_tree(handle, et, path, index, |
d0c7d708 MF |
5609 | trunc_range, meta_ac); |
5610 | if (ret) { | |
5611 | mlog_errno(ret); | |
5612 | goto out; | |
5613 | } | |
5614 | ||
5615 | /* | |
5616 | * The split could have manipulated the tree enough to | |
5617 | * move the record location, so we have to look for it again. | |
5618 | */ | |
5619 | ocfs2_reinit_path(path, 1); | |
5620 | ||
facdb77f | 5621 | ret = ocfs2_find_path(et->et_ci, path, cpos); |
d0c7d708 MF |
5622 | if (ret) { |
5623 | mlog_errno(ret); | |
5624 | goto out; | |
5625 | } | |
5626 | ||
5627 | el = path_leaf_el(path); | |
5628 | index = ocfs2_search_extent_list(el, cpos); | |
981035b4 | 5629 | if (index == -1) { |
dbdcf6a4 | 5630 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
7ecef14a | 5631 | "Owner %llu: split at cpos %u lost record\n", |
dbdcf6a4 | 5632 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
d0c7d708 MF |
5633 | cpos); |
5634 | ret = -EROFS; | |
5635 | goto out; | |
5636 | } | |
5637 | ||
5638 | /* | |
5639 | * Double check our values here. If anything is fishy, | |
5640 | * it's easier to catch it at the top level. | |
5641 | */ | |
5642 | rec = &el->l_recs[index]; | |
5643 | rec_range = le32_to_cpu(rec->e_cpos) + | |
5644 | ocfs2_rec_clusters(el, rec); | |
5645 | if (rec_range != trunc_range) { | |
dbdcf6a4 | 5646 | ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci), |
7ecef14a | 5647 | "Owner %llu: error after split at cpos %u trunc len %u, existing record is (%u,%u)\n", |
dbdcf6a4 | 5648 | (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), |
d0c7d708 MF |
5649 | cpos, len, le32_to_cpu(rec->e_cpos), |
5650 | ocfs2_rec_clusters(el, rec)); | |
5651 | ret = -EROFS; | |
5652 | goto out; | |
5653 | } | |
5654 | ||
043beebb JB |
5655 | ret = ocfs2_truncate_rec(handle, et, path, index, dealloc, |
5656 | cpos, len); | |
d0c7d708 MF |
5657 | if (ret) { |
5658 | mlog_errno(ret); | |
5659 | goto out; | |
5660 | } | |
5661 | } | |
5662 | ||
5663 | out: | |
5664 | ocfs2_free_path(path); | |
5665 | return ret; | |
5666 | } | |
5667 | ||
78f94673 TY |
5668 | /* |
5669 | * ocfs2_reserve_blocks_for_rec_trunc() would look basically the | |
5670 | * same as ocfs2_lock_alloctors(), except for it accepts a blocks | |
5671 | * number to reserve some extra blocks, and it only handles meta | |
5672 | * data allocations. | |
5673 | * | |
5674 | * Currently, only ocfs2_remove_btree_range() uses it for truncating | |
5675 | * and punching holes. | |
5676 | */ | |
5677 | static int ocfs2_reserve_blocks_for_rec_trunc(struct inode *inode, | |
5678 | struct ocfs2_extent_tree *et, | |
5679 | u32 extents_to_split, | |
5680 | struct ocfs2_alloc_context **ac, | |
5681 | int extra_blocks) | |
5682 | { | |
5683 | int ret = 0, num_free_extents; | |
5684 | unsigned int max_recs_needed = 2 * extents_to_split; | |
5685 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
5686 | ||
5687 | *ac = NULL; | |
5688 | ||
964f14a0 | 5689 | num_free_extents = ocfs2_num_free_extents(et); |
78f94673 TY |
5690 | if (num_free_extents < 0) { |
5691 | ret = num_free_extents; | |
5692 | mlog_errno(ret); | |
5693 | goto out; | |
5694 | } | |
5695 | ||
5696 | if (!num_free_extents || | |
5697 | (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) | |
5698 | extra_blocks += ocfs2_extend_meta_needed(et->et_root_el); | |
5699 | ||
5700 | if (extra_blocks) { | |
5701 | ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, ac); | |
5702 | if (ret < 0) { | |
5703 | if (ret != -ENOSPC) | |
5704 | mlog_errno(ret); | |
5705 | goto out; | |
5706 | } | |
5707 | } | |
5708 | ||
5709 | out: | |
5710 | if (ret) { | |
5711 | if (*ac) { | |
5712 | ocfs2_free_alloc_context(*ac); | |
5713 | *ac = NULL; | |
5714 | } | |
5715 | } | |
5716 | ||
5717 | return ret; | |
5718 | } | |
5719 | ||
fecc0112 MF |
5720 | int ocfs2_remove_btree_range(struct inode *inode, |
5721 | struct ocfs2_extent_tree *et, | |
78f94673 TY |
5722 | u32 cpos, u32 phys_cpos, u32 len, int flags, |
5723 | struct ocfs2_cached_dealloc_ctxt *dealloc, | |
f62f12b3 | 5724 | u64 refcount_loc, bool refcount_tree_locked) |
fecc0112 | 5725 | { |
78f94673 | 5726 | int ret, credits = 0, extra_blocks = 0; |
fecc0112 MF |
5727 | u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); |
5728 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
5729 | struct inode *tl_inode = osb->osb_tl_inode; | |
5730 | handle_t *handle; | |
5731 | struct ocfs2_alloc_context *meta_ac = NULL; | |
78f94673 TY |
5732 | struct ocfs2_refcount_tree *ref_tree = NULL; |
5733 | ||
5734 | if ((flags & OCFS2_EXT_REFCOUNTED) && len) { | |
84e40080 | 5735 | BUG_ON(!ocfs2_is_refcount_inode(inode)); |
78f94673 | 5736 | |
f62f12b3 JB |
5737 | if (!refcount_tree_locked) { |
5738 | ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1, | |
5739 | &ref_tree, NULL); | |
5740 | if (ret) { | |
5741 | mlog_errno(ret); | |
5742 | goto bail; | |
5743 | } | |
78f94673 TY |
5744 | } |
5745 | ||
5746 | ret = ocfs2_prepare_refcount_change_for_del(inode, | |
5747 | refcount_loc, | |
5748 | phys_blkno, | |
5749 | len, | |
5750 | &credits, | |
5751 | &extra_blocks); | |
5752 | if (ret < 0) { | |
5753 | mlog_errno(ret); | |
33add0e3 | 5754 | goto bail; |
78f94673 TY |
5755 | } |
5756 | } | |
fecc0112 | 5757 | |
78f94673 TY |
5758 | ret = ocfs2_reserve_blocks_for_rec_trunc(inode, et, 1, &meta_ac, |
5759 | extra_blocks); | |
fecc0112 MF |
5760 | if (ret) { |
5761 | mlog_errno(ret); | |
33add0e3 | 5762 | goto bail; |
fecc0112 MF |
5763 | } |
5764 | ||
5955102c | 5765 | inode_lock(tl_inode); |
fecc0112 MF |
5766 | |
5767 | if (ocfs2_truncate_log_needs_flush(osb)) { | |
5768 | ret = __ocfs2_flush_truncate_log(osb); | |
5769 | if (ret < 0) { | |
5770 | mlog_errno(ret); | |
5771 | goto out; | |
5772 | } | |
5773 | } | |
5774 | ||
78f94673 TY |
5775 | handle = ocfs2_start_trans(osb, |
5776 | ocfs2_remove_extent_credits(osb->sb) + credits); | |
fecc0112 MF |
5777 | if (IS_ERR(handle)) { |
5778 | ret = PTR_ERR(handle); | |
5779 | mlog_errno(ret); | |
5780 | goto out; | |
5781 | } | |
5782 | ||
d9a0a1f8 | 5783 | ret = ocfs2_et_root_journal_access(handle, et, |
13723d00 | 5784 | OCFS2_JOURNAL_ACCESS_WRITE); |
fecc0112 MF |
5785 | if (ret) { |
5786 | mlog_errno(ret); | |
b8a0ae57 | 5787 | goto out_commit; |
fecc0112 MF |
5788 | } |
5789 | ||
5dd4056d | 5790 | dquot_free_space_nodirty(inode, |
fd4ef231 MF |
5791 | ocfs2_clusters_to_bytes(inode->i_sb, len)); |
5792 | ||
dbdcf6a4 | 5793 | ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc); |
fecc0112 MF |
5794 | if (ret) { |
5795 | mlog_errno(ret); | |
5796 | goto out_commit; | |
5797 | } | |
5798 | ||
6136ca5f | 5799 | ocfs2_et_update_clusters(et, -len); |
6fdb702d | 5800 | ocfs2_update_inode_fsync_trans(handle, inode, 1); |
fecc0112 | 5801 | |
ec20cec7 | 5802 | ocfs2_journal_dirty(handle, et->et_root_bh); |
fecc0112 | 5803 | |
78f94673 TY |
5804 | if (phys_blkno) { |
5805 | if (flags & OCFS2_EXT_REFCOUNTED) | |
5806 | ret = ocfs2_decrease_refcount(inode, handle, | |
5807 | ocfs2_blocks_to_clusters(osb->sb, | |
5808 | phys_blkno), | |
5809 | len, meta_ac, | |
5810 | dealloc, 1); | |
5811 | else | |
5812 | ret = ocfs2_truncate_log_append(osb, handle, | |
5813 | phys_blkno, len); | |
5814 | if (ret) | |
5815 | mlog_errno(ret); | |
5816 | ||
5817 | } | |
fecc0112 MF |
5818 | |
5819 | out_commit: | |
5820 | ocfs2_commit_trans(osb, handle); | |
5821 | out: | |
5955102c | 5822 | inode_unlock(tl_inode); |
33add0e3 | 5823 | bail: |
fecc0112 MF |
5824 | if (meta_ac) |
5825 | ocfs2_free_alloc_context(meta_ac); | |
5826 | ||
78f94673 TY |
5827 | if (ref_tree) |
5828 | ocfs2_unlock_refcount_tree(osb, ref_tree, 1); | |
5829 | ||
fecc0112 MF |
5830 | return ret; |
5831 | } | |
5832 | ||
063c4561 | 5833 | int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb) |
ccd979bd MF |
5834 | { |
5835 | struct buffer_head *tl_bh = osb->osb_tl_bh; | |
5836 | struct ocfs2_dinode *di; | |
5837 | struct ocfs2_truncate_log *tl; | |
5838 | ||
5839 | di = (struct ocfs2_dinode *) tl_bh->b_data; | |
5840 | tl = &di->id2.i_dealloc; | |
5841 | ||
5842 | mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count), | |
5843 | "slot %d, invalid truncate log parameters: used = " | |
5844 | "%u, count = %u\n", osb->slot_num, | |
5845 | le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count)); | |
5846 | return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count); | |
5847 | } | |
5848 | ||
5849 | static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl, | |
5850 | unsigned int new_start) | |
5851 | { | |
5852 | unsigned int tail_index; | |
5853 | unsigned int current_tail; | |
5854 | ||
5855 | /* No records, nothing to coalesce */ | |
5856 | if (!le16_to_cpu(tl->tl_used)) | |
5857 | return 0; | |
5858 | ||
5859 | tail_index = le16_to_cpu(tl->tl_used) - 1; | |
5860 | current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start); | |
5861 | current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters); | |
5862 | ||
5863 | return current_tail == new_start; | |
5864 | } | |
5865 | ||
063c4561 MF |
5866 | int ocfs2_truncate_log_append(struct ocfs2_super *osb, |
5867 | handle_t *handle, | |
5868 | u64 start_blk, | |
5869 | unsigned int num_clusters) | |
ccd979bd MF |
5870 | { |
5871 | int status, index; | |
5872 | unsigned int start_cluster, tl_count; | |
5873 | struct inode *tl_inode = osb->osb_tl_inode; | |
5874 | struct buffer_head *tl_bh = osb->osb_tl_bh; | |
5875 | struct ocfs2_dinode *di; | |
5876 | struct ocfs2_truncate_log *tl; | |
5877 | ||
5955102c | 5878 | BUG_ON(inode_trylock(tl_inode)); |
ccd979bd MF |
5879 | |
5880 | start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk); | |
5881 | ||
5882 | di = (struct ocfs2_dinode *) tl_bh->b_data; | |
ccd979bd | 5883 | |
10995aa2 JB |
5884 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
5885 | * by the underlying call to ocfs2_read_inode_block(), so any | |
5886 | * corruption is a code bug */ | |
5887 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); | |
5888 | ||
5889 | tl = &di->id2.i_dealloc; | |
ccd979bd MF |
5890 | tl_count = le16_to_cpu(tl->tl_count); |
5891 | mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) || | |
5892 | tl_count == 0, | |
b0697053 MF |
5893 | "Truncate record count on #%llu invalid " |
5894 | "wanted %u, actual %u\n", | |
5895 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, | |
ccd979bd MF |
5896 | ocfs2_truncate_recs_per_inode(osb->sb), |
5897 | le16_to_cpu(tl->tl_count)); | |
5898 | ||
5899 | /* Caller should have known to flush before calling us. */ | |
5900 | index = le16_to_cpu(tl->tl_used); | |
5901 | if (index >= tl_count) { | |
5902 | status = -ENOSPC; | |
5903 | mlog_errno(status); | |
5904 | goto bail; | |
5905 | } | |
5906 | ||
0cf2f763 | 5907 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
13723d00 | 5908 | OCFS2_JOURNAL_ACCESS_WRITE); |
ccd979bd MF |
5909 | if (status < 0) { |
5910 | mlog_errno(status); | |
5911 | goto bail; | |
5912 | } | |
5913 | ||
a09d09b8 TM |
5914 | trace_ocfs2_truncate_log_append( |
5915 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index, | |
5916 | start_cluster, num_clusters); | |
ccd979bd MF |
5917 | if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) { |
5918 | /* | |
5919 | * Move index back to the record we are coalescing with. | |
5920 | * ocfs2_truncate_log_can_coalesce() guarantees nonzero | |
5921 | */ | |
5922 | index--; | |
5923 | ||
5924 | num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters); | |
a09d09b8 TM |
5925 | trace_ocfs2_truncate_log_append( |
5926 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, | |
5927 | index, le32_to_cpu(tl->tl_recs[index].t_start), | |
5928 | num_clusters); | |
ccd979bd MF |
5929 | } else { |
5930 | tl->tl_recs[index].t_start = cpu_to_le32(start_cluster); | |
5931 | tl->tl_used = cpu_to_le16(index + 1); | |
5932 | } | |
5933 | tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters); | |
5934 | ||
ec20cec7 | 5935 | ocfs2_journal_dirty(handle, tl_bh); |
ccd979bd | 5936 | |
50308d81 | 5937 | osb->truncated_clusters += num_clusters; |
ccd979bd | 5938 | bail: |
ccd979bd MF |
5939 | return status; |
5940 | } | |
5941 | ||
5942 | static int ocfs2_replay_truncate_records(struct ocfs2_super *osb, | |
ccd979bd MF |
5943 | struct inode *data_alloc_inode, |
5944 | struct buffer_head *data_alloc_bh) | |
5945 | { | |
5946 | int status = 0; | |
5947 | int i; | |
5948 | unsigned int num_clusters; | |
5949 | u64 start_blk; | |
5950 | struct ocfs2_truncate_rec rec; | |
5951 | struct ocfs2_dinode *di; | |
5952 | struct ocfs2_truncate_log *tl; | |
5953 | struct inode *tl_inode = osb->osb_tl_inode; | |
5954 | struct buffer_head *tl_bh = osb->osb_tl_bh; | |
2b0ad008 | 5955 | handle_t *handle; |
ccd979bd | 5956 | |
ccd979bd MF |
5957 | di = (struct ocfs2_dinode *) tl_bh->b_data; |
5958 | tl = &di->id2.i_dealloc; | |
5959 | i = le16_to_cpu(tl->tl_used) - 1; | |
5960 | while (i >= 0) { | |
2b0ad008 JB |
5961 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC); |
5962 | if (IS_ERR(handle)) { | |
5963 | status = PTR_ERR(handle); | |
5964 | mlog_errno(status); | |
5965 | goto bail; | |
5966 | } | |
5967 | ||
ccd979bd MF |
5968 | /* Caller has given us at least enough credits to |
5969 | * update the truncate log dinode */ | |
0cf2f763 | 5970 | status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh, |
13723d00 | 5971 | OCFS2_JOURNAL_ACCESS_WRITE); |
ccd979bd MF |
5972 | if (status < 0) { |
5973 | mlog_errno(status); | |
5974 | goto bail; | |
5975 | } | |
5976 | ||
5977 | tl->tl_used = cpu_to_le16(i); | |
5978 | ||
ec20cec7 | 5979 | ocfs2_journal_dirty(handle, tl_bh); |
ccd979bd | 5980 | |
ccd979bd MF |
5981 | rec = tl->tl_recs[i]; |
5982 | start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb, | |
5983 | le32_to_cpu(rec.t_start)); | |
5984 | num_clusters = le32_to_cpu(rec.t_clusters); | |
5985 | ||
5986 | /* if start_blk is not set, we ignore the record as | |
5987 | * invalid. */ | |
5988 | if (start_blk) { | |
a09d09b8 TM |
5989 | trace_ocfs2_replay_truncate_records( |
5990 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, | |
5991 | i, le32_to_cpu(rec.t_start), num_clusters); | |
ccd979bd MF |
5992 | |
5993 | status = ocfs2_free_clusters(handle, data_alloc_inode, | |
5994 | data_alloc_bh, start_blk, | |
5995 | num_clusters); | |
5996 | if (status < 0) { | |
5997 | mlog_errno(status); | |
5998 | goto bail; | |
5999 | } | |
6000 | } | |
102c2595 | 6001 | |
2b0ad008 | 6002 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
6003 | i--; |
6004 | } | |
6005 | ||
50308d81 TM |
6006 | osb->truncated_clusters = 0; |
6007 | ||
ccd979bd | 6008 | bail: |
ccd979bd MF |
6009 | return status; |
6010 | } | |
6011 | ||
1b1dcc1b | 6012 | /* Expects you to already be holding tl_inode->i_mutex */ |
063c4561 | 6013 | int __ocfs2_flush_truncate_log(struct ocfs2_super *osb) |
ccd979bd MF |
6014 | { |
6015 | int status; | |
6016 | unsigned int num_to_flush; | |
ccd979bd MF |
6017 | struct inode *tl_inode = osb->osb_tl_inode; |
6018 | struct inode *data_alloc_inode = NULL; | |
6019 | struct buffer_head *tl_bh = osb->osb_tl_bh; | |
6020 | struct buffer_head *data_alloc_bh = NULL; | |
6021 | struct ocfs2_dinode *di; | |
6022 | struct ocfs2_truncate_log *tl; | |
6023 | ||
5955102c | 6024 | BUG_ON(inode_trylock(tl_inode)); |
ccd979bd MF |
6025 | |
6026 | di = (struct ocfs2_dinode *) tl_bh->b_data; | |
ccd979bd | 6027 | |
10995aa2 JB |
6028 | /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated |
6029 | * by the underlying call to ocfs2_read_inode_block(), so any | |
6030 | * corruption is a code bug */ | |
6031 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); | |
6032 | ||
6033 | tl = &di->id2.i_dealloc; | |
ccd979bd | 6034 | num_to_flush = le16_to_cpu(tl->tl_used); |
a09d09b8 TM |
6035 | trace_ocfs2_flush_truncate_log( |
6036 | (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, | |
6037 | num_to_flush); | |
ccd979bd MF |
6038 | if (!num_to_flush) { |
6039 | status = 0; | |
e08dc8b9 | 6040 | goto out; |
ccd979bd MF |
6041 | } |
6042 | ||
6043 | data_alloc_inode = ocfs2_get_system_file_inode(osb, | |
6044 | GLOBAL_BITMAP_SYSTEM_INODE, | |
6045 | OCFS2_INVALID_SLOT); | |
6046 | if (!data_alloc_inode) { | |
6047 | status = -EINVAL; | |
6048 | mlog(ML_ERROR, "Could not get bitmap inode!\n"); | |
e08dc8b9 | 6049 | goto out; |
ccd979bd MF |
6050 | } |
6051 | ||
5955102c | 6052 | inode_lock(data_alloc_inode); |
e08dc8b9 | 6053 | |
e63aecb6 | 6054 | status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1); |
ccd979bd MF |
6055 | if (status < 0) { |
6056 | mlog_errno(status); | |
e08dc8b9 | 6057 | goto out_mutex; |
ccd979bd MF |
6058 | } |
6059 | ||
2b0ad008 | 6060 | status = ocfs2_replay_truncate_records(osb, data_alloc_inode, |
ccd979bd | 6061 | data_alloc_bh); |
e08dc8b9 | 6062 | if (status < 0) |
ccd979bd | 6063 | mlog_errno(status); |
ccd979bd | 6064 | |
e08dc8b9 | 6065 | brelse(data_alloc_bh); |
e63aecb6 | 6066 | ocfs2_inode_unlock(data_alloc_inode, 1); |
ccd979bd | 6067 | |
e08dc8b9 | 6068 | out_mutex: |
5955102c | 6069 | inode_unlock(data_alloc_inode); |
e08dc8b9 | 6070 | iput(data_alloc_inode); |
ccd979bd | 6071 | |
e08dc8b9 | 6072 | out: |
ccd979bd MF |
6073 | return status; |
6074 | } | |
6075 | ||
6076 | int ocfs2_flush_truncate_log(struct ocfs2_super *osb) | |
6077 | { | |
6078 | int status; | |
6079 | struct inode *tl_inode = osb->osb_tl_inode; | |
6080 | ||
5955102c | 6081 | inode_lock(tl_inode); |
ccd979bd | 6082 | status = __ocfs2_flush_truncate_log(osb); |
5955102c | 6083 | inode_unlock(tl_inode); |
ccd979bd MF |
6084 | |
6085 | return status; | |
6086 | } | |
6087 | ||
c4028958 | 6088 | static void ocfs2_truncate_log_worker(struct work_struct *work) |
ccd979bd MF |
6089 | { |
6090 | int status; | |
c4028958 DH |
6091 | struct ocfs2_super *osb = |
6092 | container_of(work, struct ocfs2_super, | |
6093 | osb_truncate_log_wq.work); | |
ccd979bd | 6094 | |
ccd979bd MF |
6095 | status = ocfs2_flush_truncate_log(osb); |
6096 | if (status < 0) | |
6097 | mlog_errno(status); | |
4d0ddb2c | 6098 | else |
b89c5428 | 6099 | ocfs2_init_steal_slots(osb); |
ccd979bd MF |
6100 | } |
6101 | ||
6102 | #define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ) | |
6103 | void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb, | |
6104 | int cancel) | |
6105 | { | |
a9e9acae X |
6106 | if (osb->osb_tl_inode && |
6107 | atomic_read(&osb->osb_tl_disable) == 0) { | |
ccd979bd MF |
6108 | /* We want to push off log flushes while truncates are |
6109 | * still running. */ | |
6110 | if (cancel) | |
6111 | cancel_delayed_work(&osb->osb_truncate_log_wq); | |
6112 | ||
35ddf78e | 6113 | queue_delayed_work(osb->ocfs2_wq, &osb->osb_truncate_log_wq, |
ccd979bd MF |
6114 | OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL); |
6115 | } | |
6116 | } | |
6117 | ||
2070ad1a ER |
6118 | /* |
6119 | * Try to flush truncate logs if we can free enough clusters from it. | |
6120 | * As for return value, "< 0" means error, "0" no space and "1" means | |
6121 | * we have freed enough spaces and let the caller try to allocate again. | |
6122 | */ | |
6123 | int ocfs2_try_to_free_truncate_log(struct ocfs2_super *osb, | |
6124 | unsigned int needed) | |
6125 | { | |
6126 | tid_t target; | |
6127 | int ret = 0; | |
6128 | unsigned int truncated_clusters; | |
6129 | ||
6130 | inode_lock(osb->osb_tl_inode); | |
6131 | truncated_clusters = osb->truncated_clusters; | |
6132 | inode_unlock(osb->osb_tl_inode); | |
6133 | ||
6134 | /* | |
6135 | * Check whether we can succeed in allocating if we free | |
6136 | * the truncate log. | |
6137 | */ | |
6138 | if (truncated_clusters < needed) | |
6139 | goto out; | |
6140 | ||
6141 | ret = ocfs2_flush_truncate_log(osb); | |
6142 | if (ret) { | |
6143 | mlog_errno(ret); | |
6144 | goto out; | |
6145 | } | |
6146 | ||
6147 | if (jbd2_journal_start_commit(osb->journal->j_journal, &target)) { | |
6148 | jbd2_log_wait_commit(osb->journal->j_journal, target); | |
6149 | ret = 1; | |
6150 | } | |
6151 | out: | |
6152 | return ret; | |
6153 | } | |
6154 | ||
ccd979bd MF |
6155 | static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb, |
6156 | int slot_num, | |
6157 | struct inode **tl_inode, | |
6158 | struct buffer_head **tl_bh) | |
6159 | { | |
6160 | int status; | |
6161 | struct inode *inode = NULL; | |
6162 | struct buffer_head *bh = NULL; | |
6163 | ||
6164 | inode = ocfs2_get_system_file_inode(osb, | |
6165 | TRUNCATE_LOG_SYSTEM_INODE, | |
6166 | slot_num); | |
6167 | if (!inode) { | |
6168 | status = -EINVAL; | |
6169 | mlog(ML_ERROR, "Could not get load truncate log inode!\n"); | |
6170 | goto bail; | |
6171 | } | |
6172 | ||
b657c95c | 6173 | status = ocfs2_read_inode_block(inode, &bh); |
ccd979bd MF |
6174 | if (status < 0) { |
6175 | iput(inode); | |
6176 | mlog_errno(status); | |
6177 | goto bail; | |
6178 | } | |
6179 | ||
6180 | *tl_inode = inode; | |
6181 | *tl_bh = bh; | |
6182 | bail: | |
ccd979bd MF |
6183 | return status; |
6184 | } | |
6185 | ||
6186 | /* called during the 1st stage of node recovery. we stamp a clean | |
6187 | * truncate log and pass back a copy for processing later. if the | |
6188 | * truncate log does not require processing, a *tl_copy is set to | |
6189 | * NULL. */ | |
6190 | int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb, | |
6191 | int slot_num, | |
6192 | struct ocfs2_dinode **tl_copy) | |
6193 | { | |
6194 | int status; | |
6195 | struct inode *tl_inode = NULL; | |
6196 | struct buffer_head *tl_bh = NULL; | |
6197 | struct ocfs2_dinode *di; | |
6198 | struct ocfs2_truncate_log *tl; | |
6199 | ||
6200 | *tl_copy = NULL; | |
6201 | ||
a09d09b8 | 6202 | trace_ocfs2_begin_truncate_log_recovery(slot_num); |
ccd979bd MF |
6203 | |
6204 | status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh); | |
6205 | if (status < 0) { | |
6206 | mlog_errno(status); | |
6207 | goto bail; | |
6208 | } | |
6209 | ||
6210 | di = (struct ocfs2_dinode *) tl_bh->b_data; | |
ccd979bd | 6211 | |
10995aa2 JB |
6212 | /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's |
6213 | * validated by the underlying call to ocfs2_read_inode_block(), | |
6214 | * so any corruption is a code bug */ | |
6215 | BUG_ON(!OCFS2_IS_VALID_DINODE(di)); | |
6216 | ||
6217 | tl = &di->id2.i_dealloc; | |
ccd979bd | 6218 | if (le16_to_cpu(tl->tl_used)) { |
a09d09b8 | 6219 | trace_ocfs2_truncate_log_recovery_num(le16_to_cpu(tl->tl_used)); |
ccd979bd MF |
6220 | |
6221 | *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL); | |
6222 | if (!(*tl_copy)) { | |
6223 | status = -ENOMEM; | |
6224 | mlog_errno(status); | |
6225 | goto bail; | |
6226 | } | |
6227 | ||
6228 | /* Assuming the write-out below goes well, this copy | |
6229 | * will be passed back to recovery for processing. */ | |
6230 | memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size); | |
6231 | ||
6232 | /* All we need to do to clear the truncate log is set | |
6233 | * tl_used. */ | |
6234 | tl->tl_used = 0; | |
6235 | ||
13723d00 | 6236 | ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check); |
8cb471e8 | 6237 | status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode)); |
ccd979bd MF |
6238 | if (status < 0) { |
6239 | mlog_errno(status); | |
6240 | goto bail; | |
6241 | } | |
6242 | } | |
6243 | ||
6244 | bail: | |
72865d92 | 6245 | iput(tl_inode); |
a81cb88b | 6246 | brelse(tl_bh); |
ccd979bd | 6247 | |
46359295 | 6248 | if (status < 0) { |
ccd979bd MF |
6249 | kfree(*tl_copy); |
6250 | *tl_copy = NULL; | |
c1e8d35e | 6251 | mlog_errno(status); |
ccd979bd MF |
6252 | } |
6253 | ||
ccd979bd MF |
6254 | return status; |
6255 | } | |
6256 | ||
6257 | int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb, | |
6258 | struct ocfs2_dinode *tl_copy) | |
6259 | { | |
6260 | int status = 0; | |
6261 | int i; | |
6262 | unsigned int clusters, num_recs, start_cluster; | |
6263 | u64 start_blk; | |
1fabe148 | 6264 | handle_t *handle; |
ccd979bd MF |
6265 | struct inode *tl_inode = osb->osb_tl_inode; |
6266 | struct ocfs2_truncate_log *tl; | |
6267 | ||
ccd979bd MF |
6268 | if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) { |
6269 | mlog(ML_ERROR, "Asked to recover my own truncate log!\n"); | |
6270 | return -EINVAL; | |
6271 | } | |
6272 | ||
6273 | tl = &tl_copy->id2.i_dealloc; | |
6274 | num_recs = le16_to_cpu(tl->tl_used); | |
a09d09b8 TM |
6275 | trace_ocfs2_complete_truncate_log_recovery( |
6276 | (unsigned long long)le64_to_cpu(tl_copy->i_blkno), | |
6277 | num_recs); | |
ccd979bd | 6278 | |
5955102c | 6279 | inode_lock(tl_inode); |
ccd979bd MF |
6280 | for(i = 0; i < num_recs; i++) { |
6281 | if (ocfs2_truncate_log_needs_flush(osb)) { | |
6282 | status = __ocfs2_flush_truncate_log(osb); | |
6283 | if (status < 0) { | |
6284 | mlog_errno(status); | |
6285 | goto bail_up; | |
6286 | } | |
6287 | } | |
6288 | ||
65eff9cc | 6289 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); |
ccd979bd MF |
6290 | if (IS_ERR(handle)) { |
6291 | status = PTR_ERR(handle); | |
6292 | mlog_errno(status); | |
6293 | goto bail_up; | |
6294 | } | |
6295 | ||
6296 | clusters = le32_to_cpu(tl->tl_recs[i].t_clusters); | |
6297 | start_cluster = le32_to_cpu(tl->tl_recs[i].t_start); | |
6298 | start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster); | |
6299 | ||
6300 | status = ocfs2_truncate_log_append(osb, handle, | |
6301 | start_blk, clusters); | |
02dc1af4 | 6302 | ocfs2_commit_trans(osb, handle); |
ccd979bd MF |
6303 | if (status < 0) { |
6304 | mlog_errno(status); | |
6305 | goto bail_up; | |
6306 | } | |
6307 | } | |
6308 | ||
6309 | bail_up: | |
5955102c | 6310 | inode_unlock(tl_inode); |
ccd979bd | 6311 | |
ccd979bd MF |
6312 | return status; |
6313 | } | |
6314 | ||
6315 | void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb) | |
6316 | { | |
6317 | int status; | |
6318 | struct inode *tl_inode = osb->osb_tl_inode; | |
6319 | ||
a9e9acae X |
6320 | atomic_set(&osb->osb_tl_disable, 1); |
6321 | ||
ccd979bd MF |
6322 | if (tl_inode) { |
6323 | cancel_delayed_work(&osb->osb_truncate_log_wq); | |
35ddf78e | 6324 | flush_workqueue(osb->ocfs2_wq); |
ccd979bd MF |
6325 | |
6326 | status = ocfs2_flush_truncate_log(osb); | |
6327 | if (status < 0) | |
6328 | mlog_errno(status); | |
6329 | ||
6330 | brelse(osb->osb_tl_bh); | |
6331 | iput(osb->osb_tl_inode); | |
6332 | } | |
ccd979bd MF |
6333 | } |
6334 | ||
6335 | int ocfs2_truncate_log_init(struct ocfs2_super *osb) | |
6336 | { | |
6337 | int status; | |
6338 | struct inode *tl_inode = NULL; | |
6339 | struct buffer_head *tl_bh = NULL; | |
6340 | ||
ccd979bd MF |
6341 | status = ocfs2_get_truncate_log_info(osb, |
6342 | osb->slot_num, | |
6343 | &tl_inode, | |
6344 | &tl_bh); | |
6345 | if (status < 0) | |
6346 | mlog_errno(status); | |
6347 | ||
6348 | /* ocfs2_truncate_log_shutdown keys on the existence of | |
6349 | * osb->osb_tl_inode so we don't set any of the osb variables | |
6350 | * until we're sure all is well. */ | |
c4028958 DH |
6351 | INIT_DELAYED_WORK(&osb->osb_truncate_log_wq, |
6352 | ocfs2_truncate_log_worker); | |
a9e9acae | 6353 | atomic_set(&osb->osb_tl_disable, 0); |
ccd979bd MF |
6354 | osb->osb_tl_bh = tl_bh; |
6355 | osb->osb_tl_inode = tl_inode; | |
6356 | ||
ccd979bd MF |
6357 | return status; |
6358 | } | |
6359 | ||
2b604351 MF |
6360 | /* |
6361 | * Delayed de-allocation of suballocator blocks. | |
6362 | * | |
6363 | * Some sets of block de-allocations might involve multiple suballocator inodes. | |
6364 | * | |
6365 | * The locking for this can get extremely complicated, especially when | |
6366 | * the suballocator inodes to delete from aren't known until deep | |
6367 | * within an unrelated codepath. | |
6368 | * | |
6369 | * ocfs2_extent_block structures are a good example of this - an inode | |
6370 | * btree could have been grown by any number of nodes each allocating | |
6371 | * out of their own suballoc inode. | |
6372 | * | |
6373 | * These structures allow the delay of block de-allocation until a | |
6374 | * later time, when locking of multiple cluster inodes won't cause | |
6375 | * deadlock. | |
6376 | */ | |
6377 | ||
6378 | /* | |
2891d290 TM |
6379 | * Describe a single bit freed from a suballocator. For the block |
6380 | * suballocators, it represents one block. For the global cluster | |
6381 | * allocator, it represents some clusters and free_bit indicates | |
6382 | * clusters number. | |
2b604351 MF |
6383 | */ |
6384 | struct ocfs2_cached_block_free { | |
6385 | struct ocfs2_cached_block_free *free_next; | |
74380c47 | 6386 | u64 free_bg; |
2b604351 MF |
6387 | u64 free_blk; |
6388 | unsigned int free_bit; | |
6389 | }; | |
6390 | ||
6391 | struct ocfs2_per_slot_free_list { | |
6392 | struct ocfs2_per_slot_free_list *f_next_suballocator; | |
6393 | int f_inode_type; | |
6394 | int f_slot; | |
6395 | struct ocfs2_cached_block_free *f_first; | |
6396 | }; | |
6397 | ||
2891d290 TM |
6398 | static int ocfs2_free_cached_blocks(struct ocfs2_super *osb, |
6399 | int sysfile_type, | |
6400 | int slot, | |
6401 | struct ocfs2_cached_block_free *head) | |
2b604351 MF |
6402 | { |
6403 | int ret; | |
6404 | u64 bg_blkno; | |
6405 | handle_t *handle; | |
6406 | struct inode *inode; | |
6407 | struct buffer_head *di_bh = NULL; | |
6408 | struct ocfs2_cached_block_free *tmp; | |
6409 | ||
6410 | inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot); | |
6411 | if (!inode) { | |
6412 | ret = -EINVAL; | |
6413 | mlog_errno(ret); | |
6414 | goto out; | |
6415 | } | |
6416 | ||
5955102c | 6417 | inode_lock(inode); |
2b604351 | 6418 | |
e63aecb6 | 6419 | ret = ocfs2_inode_lock(inode, &di_bh, 1); |
2b604351 MF |
6420 | if (ret) { |
6421 | mlog_errno(ret); | |
6422 | goto out_mutex; | |
6423 | } | |
6424 | ||
2b604351 | 6425 | while (head) { |
74380c47 TM |
6426 | if (head->free_bg) |
6427 | bg_blkno = head->free_bg; | |
6428 | else | |
6429 | bg_blkno = ocfs2_which_suballoc_group(head->free_blk, | |
6430 | head->free_bit); | |
d5bf1418 JB |
6431 | handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE); |
6432 | if (IS_ERR(handle)) { | |
6433 | ret = PTR_ERR(handle); | |
6434 | mlog_errno(ret); | |
6435 | goto out_unlock; | |
6436 | } | |
6437 | ||
a09d09b8 TM |
6438 | trace_ocfs2_free_cached_blocks( |
6439 | (unsigned long long)head->free_blk, head->free_bit); | |
2b604351 MF |
6440 | |
6441 | ret = ocfs2_free_suballoc_bits(handle, inode, di_bh, | |
6442 | head->free_bit, bg_blkno, 1); | |
d5bf1418 | 6443 | if (ret) |
2b604351 | 6444 | mlog_errno(ret); |
2b604351 | 6445 | |
d5bf1418 | 6446 | ocfs2_commit_trans(osb, handle); |
2b604351 MF |
6447 | |
6448 | tmp = head; | |
6449 | head = head->free_next; | |
6450 | kfree(tmp); | |
6451 | } | |
6452 | ||
2b604351 | 6453 | out_unlock: |
e63aecb6 | 6454 | ocfs2_inode_unlock(inode, 1); |
2b604351 MF |
6455 | brelse(di_bh); |
6456 | out_mutex: | |
5955102c | 6457 | inode_unlock(inode); |
2b604351 MF |
6458 | iput(inode); |
6459 | out: | |
6460 | while(head) { | |
6461 | /* Premature exit may have left some dangling items. */ | |
6462 | tmp = head; | |
6463 | head = head->free_next; | |
6464 | kfree(tmp); | |
6465 | } | |
6466 | ||
6467 | return ret; | |
6468 | } | |
6469 | ||
2891d290 TM |
6470 | int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
6471 | u64 blkno, unsigned int bit) | |
6472 | { | |
6473 | int ret = 0; | |
6474 | struct ocfs2_cached_block_free *item; | |
6475 | ||
74380c47 | 6476 | item = kzalloc(sizeof(*item), GFP_NOFS); |
2891d290 TM |
6477 | if (item == NULL) { |
6478 | ret = -ENOMEM; | |
6479 | mlog_errno(ret); | |
6480 | return ret; | |
6481 | } | |
6482 | ||
a09d09b8 | 6483 | trace_ocfs2_cache_cluster_dealloc((unsigned long long)blkno, bit); |
2891d290 TM |
6484 | |
6485 | item->free_blk = blkno; | |
6486 | item->free_bit = bit; | |
6487 | item->free_next = ctxt->c_global_allocator; | |
6488 | ||
6489 | ctxt->c_global_allocator = item; | |
6490 | return ret; | |
6491 | } | |
6492 | ||
6493 | static int ocfs2_free_cached_clusters(struct ocfs2_super *osb, | |
6494 | struct ocfs2_cached_block_free *head) | |
6495 | { | |
6496 | struct ocfs2_cached_block_free *tmp; | |
6497 | struct inode *tl_inode = osb->osb_tl_inode; | |
6498 | handle_t *handle; | |
6499 | int ret = 0; | |
6500 | ||
5955102c | 6501 | inode_lock(tl_inode); |
2891d290 TM |
6502 | |
6503 | while (head) { | |
6504 | if (ocfs2_truncate_log_needs_flush(osb)) { | |
6505 | ret = __ocfs2_flush_truncate_log(osb); | |
6506 | if (ret < 0) { | |
6507 | mlog_errno(ret); | |
6508 | break; | |
6509 | } | |
6510 | } | |
6511 | ||
6512 | handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE); | |
6513 | if (IS_ERR(handle)) { | |
6514 | ret = PTR_ERR(handle); | |
6515 | mlog_errno(ret); | |
6516 | break; | |
6517 | } | |
6518 | ||
6519 | ret = ocfs2_truncate_log_append(osb, handle, head->free_blk, | |
6520 | head->free_bit); | |
6521 | ||
6522 | ocfs2_commit_trans(osb, handle); | |
6523 | tmp = head; | |
6524 | head = head->free_next; | |
6525 | kfree(tmp); | |
6526 | ||
6527 | if (ret < 0) { | |
6528 | mlog_errno(ret); | |
6529 | break; | |
6530 | } | |
6531 | } | |
6532 | ||
5955102c | 6533 | inode_unlock(tl_inode); |
2891d290 TM |
6534 | |
6535 | while (head) { | |
6536 | /* Premature exit may have left some dangling items. */ | |
6537 | tmp = head; | |
6538 | head = head->free_next; | |
6539 | kfree(tmp); | |
6540 | } | |
6541 | ||
6542 | return ret; | |
6543 | } | |
6544 | ||
2b604351 MF |
6545 | int ocfs2_run_deallocs(struct ocfs2_super *osb, |
6546 | struct ocfs2_cached_dealloc_ctxt *ctxt) | |
6547 | { | |
6548 | int ret = 0, ret2; | |
6549 | struct ocfs2_per_slot_free_list *fl; | |
6550 | ||
6551 | if (!ctxt) | |
6552 | return 0; | |
6553 | ||
6554 | while (ctxt->c_first_suballocator) { | |
6555 | fl = ctxt->c_first_suballocator; | |
6556 | ||
6557 | if (fl->f_first) { | |
a09d09b8 TM |
6558 | trace_ocfs2_run_deallocs(fl->f_inode_type, |
6559 | fl->f_slot); | |
2891d290 TM |
6560 | ret2 = ocfs2_free_cached_blocks(osb, |
6561 | fl->f_inode_type, | |
6562 | fl->f_slot, | |
6563 | fl->f_first); | |
2b604351 MF |
6564 | if (ret2) |
6565 | mlog_errno(ret2); | |
6566 | if (!ret) | |
6567 | ret = ret2; | |
6568 | } | |
6569 | ||
6570 | ctxt->c_first_suballocator = fl->f_next_suballocator; | |
6571 | kfree(fl); | |
6572 | } | |
6573 | ||
2891d290 TM |
6574 | if (ctxt->c_global_allocator) { |
6575 | ret2 = ocfs2_free_cached_clusters(osb, | |
6576 | ctxt->c_global_allocator); | |
6577 | if (ret2) | |
6578 | mlog_errno(ret2); | |
6579 | if (!ret) | |
6580 | ret = ret2; | |
6581 | ||
6582 | ctxt->c_global_allocator = NULL; | |
6583 | } | |
6584 | ||
2b604351 MF |
6585 | return ret; |
6586 | } | |
6587 | ||
6588 | static struct ocfs2_per_slot_free_list * | |
6589 | ocfs2_find_per_slot_free_list(int type, | |
6590 | int slot, | |
6591 | struct ocfs2_cached_dealloc_ctxt *ctxt) | |
6592 | { | |
6593 | struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator; | |
6594 | ||
6595 | while (fl) { | |
6596 | if (fl->f_inode_type == type && fl->f_slot == slot) | |
6597 | return fl; | |
6598 | ||
6599 | fl = fl->f_next_suballocator; | |
6600 | } | |
6601 | ||
6602 | fl = kmalloc(sizeof(*fl), GFP_NOFS); | |
6603 | if (fl) { | |
6604 | fl->f_inode_type = type; | |
6605 | fl->f_slot = slot; | |
6606 | fl->f_first = NULL; | |
6607 | fl->f_next_suballocator = ctxt->c_first_suballocator; | |
6608 | ||
6609 | ctxt->c_first_suballocator = fl; | |
6610 | } | |
6611 | return fl; | |
6612 | } | |
6613 | ||
71a36944 CG |
6614 | static struct ocfs2_per_slot_free_list * |
6615 | ocfs2_find_preferred_free_list(int type, | |
6616 | int preferred_slot, | |
6617 | int *real_slot, | |
6618 | struct ocfs2_cached_dealloc_ctxt *ctxt) | |
6619 | { | |
6620 | struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator; | |
6621 | ||
6622 | while (fl) { | |
6623 | if (fl->f_inode_type == type && fl->f_slot == preferred_slot) { | |
6624 | *real_slot = fl->f_slot; | |
6625 | return fl; | |
6626 | } | |
6627 | ||
6628 | fl = fl->f_next_suballocator; | |
6629 | } | |
6630 | ||
6631 | /* If we can't find any free list matching preferred slot, just use | |
6632 | * the first one. | |
6633 | */ | |
6634 | fl = ctxt->c_first_suballocator; | |
6635 | *real_slot = fl->f_slot; | |
6636 | ||
6637 | return fl; | |
6638 | } | |
6639 | ||
6640 | /* Return Value 1 indicates empty */ | |
6641 | static int ocfs2_is_dealloc_empty(struct ocfs2_extent_tree *et) | |
6642 | { | |
6643 | struct ocfs2_per_slot_free_list *fl = NULL; | |
6644 | ||
6645 | if (!et->et_dealloc) | |
6646 | return 1; | |
6647 | ||
6648 | fl = et->et_dealloc->c_first_suballocator; | |
6649 | if (!fl) | |
6650 | return 1; | |
6651 | ||
6652 | if (!fl->f_first) | |
6653 | return 1; | |
6654 | ||
6655 | return 0; | |
6656 | } | |
6657 | ||
6658 | /* If extent was deleted from tree due to extent rotation and merging, and | |
6659 | * no metadata is reserved ahead of time. Try to reuse some extents | |
6660 | * just deleted. This is only used to reuse extent blocks. | |
6661 | * It is supposed to find enough extent blocks in dealloc if our estimation | |
6662 | * on metadata is accurate. | |
6663 | */ | |
6664 | static int ocfs2_reuse_blk_from_dealloc(handle_t *handle, | |
6665 | struct ocfs2_extent_tree *et, | |
6666 | struct buffer_head **new_eb_bh, | |
6667 | int blk_wanted, int *blk_given) | |
6668 | { | |
6669 | int i, status = 0, real_slot; | |
6670 | struct ocfs2_cached_dealloc_ctxt *dealloc; | |
6671 | struct ocfs2_per_slot_free_list *fl; | |
6672 | struct ocfs2_cached_block_free *bf; | |
6673 | struct ocfs2_extent_block *eb; | |
6674 | struct ocfs2_super *osb = | |
6675 | OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci)); | |
6676 | ||
6677 | *blk_given = 0; | |
6678 | ||
6679 | /* If extent tree doesn't have a dealloc, this is not faulty. Just | |
6680 | * tell upper caller dealloc can't provide any block and it should | |
6681 | * ask for alloc to claim more space. | |
6682 | */ | |
6683 | dealloc = et->et_dealloc; | |
6684 | if (!dealloc) | |
6685 | goto bail; | |
6686 | ||
6687 | for (i = 0; i < blk_wanted; i++) { | |
6688 | /* Prefer to use local slot */ | |
6689 | fl = ocfs2_find_preferred_free_list(EXTENT_ALLOC_SYSTEM_INODE, | |
6690 | osb->slot_num, &real_slot, | |
6691 | dealloc); | |
6692 | /* If no more block can be reused, we should claim more | |
6693 | * from alloc. Just return here normally. | |
6694 | */ | |
6695 | if (!fl) { | |
6696 | status = 0; | |
6697 | break; | |
6698 | } | |
6699 | ||
6700 | bf = fl->f_first; | |
6701 | fl->f_first = bf->free_next; | |
6702 | ||
6703 | new_eb_bh[i] = sb_getblk(osb->sb, bf->free_blk); | |
6704 | if (new_eb_bh[i] == NULL) { | |
6705 | status = -ENOMEM; | |
6706 | mlog_errno(status); | |
6707 | goto bail; | |
6708 | } | |
6709 | ||
6710 | mlog(0, "Reusing block(%llu) from " | |
6711 | "dealloc(local slot:%d, real slot:%d)\n", | |
6712 | bf->free_blk, osb->slot_num, real_slot); | |
6713 | ||
6714 | ocfs2_set_new_buffer_uptodate(et->et_ci, new_eb_bh[i]); | |
6715 | ||
6716 | status = ocfs2_journal_access_eb(handle, et->et_ci, | |
6717 | new_eb_bh[i], | |
6718 | OCFS2_JOURNAL_ACCESS_CREATE); | |
6719 | if (status < 0) { | |
6720 | mlog_errno(status); | |
6721 | goto bail; | |
6722 | } | |
6723 | ||
6724 | memset(new_eb_bh[i]->b_data, 0, osb->sb->s_blocksize); | |
6725 | eb = (struct ocfs2_extent_block *) new_eb_bh[i]->b_data; | |
6726 | ||
6727 | /* We can't guarantee that buffer head is still cached, so | |
6728 | * polutlate the extent block again. | |
6729 | */ | |
6730 | strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE); | |
6731 | eb->h_blkno = cpu_to_le64(bf->free_blk); | |
6732 | eb->h_fs_generation = cpu_to_le32(osb->fs_generation); | |
6733 | eb->h_suballoc_slot = cpu_to_le16(real_slot); | |
6734 | eb->h_suballoc_loc = cpu_to_le64(bf->free_bg); | |
6735 | eb->h_suballoc_bit = cpu_to_le16(bf->free_bit); | |
6736 | eb->h_list.l_count = | |
6737 | cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb)); | |
6738 | ||
6739 | /* We'll also be dirtied by the caller, so | |
6740 | * this isn't absolutely necessary. | |
6741 | */ | |
6742 | ocfs2_journal_dirty(handle, new_eb_bh[i]); | |
6743 | ||
6744 | if (!fl->f_first) { | |
6745 | dealloc->c_first_suballocator = fl->f_next_suballocator; | |
6746 | kfree(fl); | |
6747 | } | |
6748 | kfree(bf); | |
6749 | } | |
6750 | ||
6751 | *blk_given = i; | |
6752 | ||
6753 | bail: | |
6754 | if (unlikely(status < 0)) { | |
6755 | for (i = 0; i < blk_wanted; i++) | |
6756 | brelse(new_eb_bh[i]); | |
6757 | } | |
6758 | ||
6759 | return status; | |
6760 | } | |
6761 | ||
1823cb0b | 6762 | int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt, |
74380c47 TM |
6763 | int type, int slot, u64 suballoc, |
6764 | u64 blkno, unsigned int bit) | |
2b604351 MF |
6765 | { |
6766 | int ret; | |
6767 | struct ocfs2_per_slot_free_list *fl; | |
6768 | struct ocfs2_cached_block_free *item; | |
6769 | ||
6770 | fl = ocfs2_find_per_slot_free_list(type, slot, ctxt); | |
6771 | if (fl == NULL) { | |
6772 | ret = -ENOMEM; | |
6773 | mlog_errno(ret); | |
6774 | goto out; | |
6775 | } | |
6776 | ||
74380c47 | 6777 | item = kzalloc(sizeof(*item), GFP_NOFS); |
2b604351 MF |
6778 | if (item == NULL) { |
6779 | ret = -ENOMEM; | |
6780 | mlog_errno(ret); | |
6781 | goto out; | |
6782 | } | |
6783 | ||
a09d09b8 TM |
6784 | trace_ocfs2_cache_block_dealloc(type, slot, |
6785 | (unsigned long long)suballoc, | |
6786 | (unsigned long long)blkno, bit); | |
2b604351 | 6787 | |
74380c47 | 6788 | item->free_bg = suballoc; |
2b604351 MF |
6789 | item->free_blk = blkno; |
6790 | item->free_bit = bit; | |
6791 | item->free_next = fl->f_first; | |
6792 | ||
6793 | fl->f_first = item; | |
6794 | ||
6795 | ret = 0; | |
6796 | out: | |
6797 | return ret; | |
6798 | } | |
6799 | ||
59a5e416 MF |
6800 | static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt, |
6801 | struct ocfs2_extent_block *eb) | |
6802 | { | |
6803 | return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE, | |
6804 | le16_to_cpu(eb->h_suballoc_slot), | |
74380c47 | 6805 | le64_to_cpu(eb->h_suballoc_loc), |
59a5e416 MF |
6806 | le64_to_cpu(eb->h_blkno), |
6807 | le16_to_cpu(eb->h_suballoc_bit)); | |
6808 | } | |
6809 | ||
2b4e30fb | 6810 | static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh) |
60b11392 MF |
6811 | { |
6812 | set_buffer_uptodate(bh); | |
6813 | mark_buffer_dirty(bh); | |
6814 | return 0; | |
6815 | } | |
6816 | ||
6f70fa51 TM |
6817 | void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle, |
6818 | unsigned int from, unsigned int to, | |
6819 | struct page *page, int zero, u64 *phys) | |
1d410a6e MF |
6820 | { |
6821 | int ret, partial = 0; | |
6822 | ||
6823 | ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0); | |
6824 | if (ret) | |
6825 | mlog_errno(ret); | |
6826 | ||
6827 | if (zero) | |
eebd2aa3 | 6828 | zero_user_segment(page, from, to); |
1d410a6e MF |
6829 | |
6830 | /* | |
6831 | * Need to set the buffers we zero'd into uptodate | |
6832 | * here if they aren't - ocfs2_map_page_blocks() | |
6833 | * might've skipped some | |
6834 | */ | |
2b4e30fb JB |
6835 | ret = walk_page_buffers(handle, page_buffers(page), |
6836 | from, to, &partial, | |
6837 | ocfs2_zero_func); | |
6838 | if (ret < 0) | |
6839 | mlog_errno(ret); | |
6840 | else if (ocfs2_should_order_data(inode)) { | |
6841 | ret = ocfs2_jbd2_file_inode(handle, inode); | |
1d410a6e MF |
6842 | if (ret < 0) |
6843 | mlog_errno(ret); | |
6844 | } | |
6845 | ||
6846 | if (!partial) | |
6847 | SetPageUptodate(page); | |
6848 | ||
6849 | flush_dcache_page(page); | |
6850 | } | |
6851 | ||
35edec1d MF |
6852 | static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start, |
6853 | loff_t end, struct page **pages, | |
6854 | int numpages, u64 phys, handle_t *handle) | |
60b11392 | 6855 | { |
1d410a6e | 6856 | int i; |
60b11392 | 6857 | struct page *page; |
09cbfeaf | 6858 | unsigned int from, to = PAGE_SIZE; |
60b11392 MF |
6859 | struct super_block *sb = inode->i_sb; |
6860 | ||
6861 | BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb))); | |
6862 | ||
6863 | if (numpages == 0) | |
6864 | goto out; | |
6865 | ||
09cbfeaf | 6866 | to = PAGE_SIZE; |
60b11392 MF |
6867 | for(i = 0; i < numpages; i++) { |
6868 | page = pages[i]; | |
6869 | ||
09cbfeaf KS |
6870 | from = start & (PAGE_SIZE - 1); |
6871 | if ((end >> PAGE_SHIFT) == page->index) | |
6872 | to = end & (PAGE_SIZE - 1); | |
35edec1d | 6873 | |
09cbfeaf KS |
6874 | BUG_ON(from > PAGE_SIZE); |
6875 | BUG_ON(to > PAGE_SIZE); | |
60b11392 | 6876 | |
1d410a6e MF |
6877 | ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1, |
6878 | &phys); | |
60b11392 | 6879 | |
09cbfeaf | 6880 | start = (page->index + 1) << PAGE_SHIFT; |
60b11392 MF |
6881 | } |
6882 | out: | |
1d410a6e MF |
6883 | if (pages) |
6884 | ocfs2_unlock_and_free_pages(pages, numpages); | |
60b11392 MF |
6885 | } |
6886 | ||
6f70fa51 TM |
6887 | int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end, |
6888 | struct page **pages, int *num) | |
60b11392 | 6889 | { |
1d410a6e | 6890 | int numpages, ret = 0; |
60b11392 MF |
6891 | struct address_space *mapping = inode->i_mapping; |
6892 | unsigned long index; | |
35edec1d | 6893 | loff_t last_page_bytes; |
60b11392 | 6894 | |
35edec1d | 6895 | BUG_ON(start > end); |
60b11392 | 6896 | |
1d410a6e | 6897 | numpages = 0; |
35edec1d | 6898 | last_page_bytes = PAGE_ALIGN(end); |
09cbfeaf | 6899 | index = start >> PAGE_SHIFT; |
60b11392 | 6900 | do { |
9b4c0ff3 | 6901 | pages[numpages] = find_or_create_page(mapping, index, GFP_NOFS); |
60b11392 MF |
6902 | if (!pages[numpages]) { |
6903 | ret = -ENOMEM; | |
6904 | mlog_errno(ret); | |
6905 | goto out; | |
6906 | } | |
6907 | ||
6908 | numpages++; | |
6909 | index++; | |
09cbfeaf | 6910 | } while (index < (last_page_bytes >> PAGE_SHIFT)); |
60b11392 MF |
6911 | |
6912 | out: | |
6913 | if (ret != 0) { | |
1d410a6e MF |
6914 | if (pages) |
6915 | ocfs2_unlock_and_free_pages(pages, numpages); | |
60b11392 MF |
6916 | numpages = 0; |
6917 | } | |
6918 | ||
6919 | *num = numpages; | |
6920 | ||
6921 | return ret; | |
6922 | } | |
6923 | ||
6f70fa51 TM |
6924 | static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end, |
6925 | struct page **pages, int *num) | |
6926 | { | |
6927 | struct super_block *sb = inode->i_sb; | |
6928 | ||
6929 | BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits != | |
6930 | (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits); | |
6931 | ||
6932 | return ocfs2_grab_pages(inode, start, end, pages, num); | |
6933 | } | |
6934 | ||
60b11392 MF |
6935 | /* |
6936 | * Zero the area past i_size but still within an allocated | |
6937 | * cluster. This avoids exposing nonzero data on subsequent file | |
6938 | * extends. | |
6939 | * | |
6940 | * We need to call this before i_size is updated on the inode because | |
6941 | * otherwise block_write_full_page() will skip writeout of pages past | |
6942 | * i_size. The new_i_size parameter is passed for this reason. | |
6943 | */ | |
35edec1d MF |
6944 | int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle, |
6945 | u64 range_start, u64 range_end) | |
60b11392 | 6946 | { |
1d410a6e | 6947 | int ret = 0, numpages; |
60b11392 MF |
6948 | struct page **pages = NULL; |
6949 | u64 phys; | |
1d410a6e MF |
6950 | unsigned int ext_flags; |
6951 | struct super_block *sb = inode->i_sb; | |
60b11392 MF |
6952 | |
6953 | /* | |
6954 | * File systems which don't support sparse files zero on every | |
6955 | * extend. | |
6956 | */ | |
1d410a6e | 6957 | if (!ocfs2_sparse_alloc(OCFS2_SB(sb))) |
60b11392 MF |
6958 | return 0; |
6959 | ||
1d410a6e | 6960 | pages = kcalloc(ocfs2_pages_per_cluster(sb), |
60b11392 MF |
6961 | sizeof(struct page *), GFP_NOFS); |
6962 | if (pages == NULL) { | |
6963 | ret = -ENOMEM; | |
6964 | mlog_errno(ret); | |
6965 | goto out; | |
6966 | } | |
6967 | ||
1d410a6e MF |
6968 | if (range_start == range_end) |
6969 | goto out; | |
6970 | ||
6971 | ret = ocfs2_extent_map_get_blocks(inode, | |
6972 | range_start >> sb->s_blocksize_bits, | |
6973 | &phys, NULL, &ext_flags); | |
60b11392 MF |
6974 | if (ret) { |
6975 | mlog_errno(ret); | |
6976 | goto out; | |
6977 | } | |
6978 | ||
1d410a6e MF |
6979 | /* |
6980 | * Tail is a hole, or is marked unwritten. In either case, we | |
6981 | * can count on read and write to return/push zero's. | |
6982 | */ | |
6983 | if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN) | |
60b11392 MF |
6984 | goto out; |
6985 | ||
1d410a6e MF |
6986 | ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages, |
6987 | &numpages); | |
6988 | if (ret) { | |
6989 | mlog_errno(ret); | |
6990 | goto out; | |
6991 | } | |
6992 | ||
35edec1d MF |
6993 | ocfs2_zero_cluster_pages(inode, range_start, range_end, pages, |
6994 | numpages, phys, handle); | |
60b11392 MF |
6995 | |
6996 | /* | |
6997 | * Initiate writeout of the pages we zero'd here. We don't | |
6998 | * wait on them - the truncate_inode_pages() call later will | |
6999 | * do that for us. | |
7000 | */ | |
2cfd30ad CH |
7001 | ret = filemap_fdatawrite_range(inode->i_mapping, range_start, |
7002 | range_end - 1); | |
60b11392 MF |
7003 | if (ret) |
7004 | mlog_errno(ret); | |
7005 | ||
7006 | out: | |
d787ab09 | 7007 | kfree(pages); |
60b11392 MF |
7008 | |
7009 | return ret; | |
7010 | } | |
7011 | ||
fdd77704 TY |
7012 | static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode, |
7013 | struct ocfs2_dinode *di) | |
1afc32b9 MF |
7014 | { |
7015 | unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits; | |
fdd77704 | 7016 | unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size); |
1afc32b9 | 7017 | |
fdd77704 TY |
7018 | if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL) |
7019 | memset(&di->id2, 0, blocksize - | |
7020 | offsetof(struct ocfs2_dinode, id2) - | |
7021 | xattrsize); | |
7022 | else | |
7023 | memset(&di->id2, 0, blocksize - | |
7024 | offsetof(struct ocfs2_dinode, id2)); | |
1afc32b9 MF |
7025 | } |
7026 | ||
5b6a3a2b MF |
7027 | void ocfs2_dinode_new_extent_list(struct inode *inode, |
7028 | struct ocfs2_dinode *di) | |
7029 | { | |
fdd77704 | 7030 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
5b6a3a2b MF |
7031 | di->id2.i_list.l_tree_depth = 0; |
7032 | di->id2.i_list.l_next_free_rec = 0; | |
fdd77704 TY |
7033 | di->id2.i_list.l_count = cpu_to_le16( |
7034 | ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di)); | |
5b6a3a2b MF |
7035 | } |
7036 | ||
1afc32b9 MF |
7037 | void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di) |
7038 | { | |
7039 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
7040 | struct ocfs2_inline_data *idata = &di->id2.i_data; | |
7041 | ||
7042 | spin_lock(&oi->ip_lock); | |
7043 | oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL; | |
7044 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); | |
7045 | spin_unlock(&oi->ip_lock); | |
7046 | ||
7047 | /* | |
7048 | * We clear the entire i_data structure here so that all | |
7049 | * fields can be properly initialized. | |
7050 | */ | |
fdd77704 | 7051 | ocfs2_zero_dinode_id2_with_xattr(inode, di); |
1afc32b9 | 7052 | |
fdd77704 TY |
7053 | idata->id_count = cpu_to_le16( |
7054 | ocfs2_max_inline_data_with_xattr(inode->i_sb, di)); | |
1afc32b9 MF |
7055 | } |
7056 | ||
7057 | int ocfs2_convert_inline_data_to_extents(struct inode *inode, | |
7058 | struct buffer_head *di_bh) | |
7059 | { | |
7060 | int ret, i, has_data, num_pages = 0; | |
fb951eb5 ZW |
7061 | int need_free = 0; |
7062 | u32 bit_off, num; | |
1afc32b9 MF |
7063 | handle_t *handle; |
7064 | u64 uninitialized_var(block); | |
7065 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | |
7066 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
7067 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | |
1afc32b9 MF |
7068 | struct ocfs2_alloc_context *data_ac = NULL; |
7069 | struct page **pages = NULL; | |
7070 | loff_t end = osb->s_clustersize; | |
f99b9b7c | 7071 | struct ocfs2_extent_tree et; |
a90714c1 | 7072 | int did_quota = 0; |
1afc32b9 MF |
7073 | |
7074 | has_data = i_size_read(inode) ? 1 : 0; | |
7075 | ||
7076 | if (has_data) { | |
7077 | pages = kcalloc(ocfs2_pages_per_cluster(osb->sb), | |
7078 | sizeof(struct page *), GFP_NOFS); | |
7079 | if (pages == NULL) { | |
7080 | ret = -ENOMEM; | |
7081 | mlog_errno(ret); | |
3cc79b79 | 7082 | return ret; |
1afc32b9 MF |
7083 | } |
7084 | ||
7085 | ret = ocfs2_reserve_clusters(osb, 1, &data_ac); | |
7086 | if (ret) { | |
7087 | mlog_errno(ret); | |
3cc79b79 | 7088 | goto free_pages; |
1afc32b9 MF |
7089 | } |
7090 | } | |
7091 | ||
a90714c1 JK |
7092 | handle = ocfs2_start_trans(osb, |
7093 | ocfs2_inline_to_extents_credits(osb->sb)); | |
1afc32b9 MF |
7094 | if (IS_ERR(handle)) { |
7095 | ret = PTR_ERR(handle); | |
7096 | mlog_errno(ret); | |
15eba0fe | 7097 | goto out; |
1afc32b9 MF |
7098 | } |
7099 | ||
0cf2f763 | 7100 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
13723d00 | 7101 | OCFS2_JOURNAL_ACCESS_WRITE); |
1afc32b9 MF |
7102 | if (ret) { |
7103 | mlog_errno(ret); | |
7104 | goto out_commit; | |
7105 | } | |
7106 | ||
7107 | if (has_data) { | |
1afc32b9 MF |
7108 | unsigned int page_end; |
7109 | u64 phys; | |
7110 | ||
5dd4056d CH |
7111 | ret = dquot_alloc_space_nodirty(inode, |
7112 | ocfs2_clusters_to_bytes(osb->sb, 1)); | |
7113 | if (ret) | |
a90714c1 | 7114 | goto out_commit; |
a90714c1 JK |
7115 | did_quota = 1; |
7116 | ||
d324cd4c | 7117 | data_ac->ac_resv = &oi->ip_la_data_resv; |
4fe370af | 7118 | |
1ed9b777 | 7119 | ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off, |
1afc32b9 MF |
7120 | &num); |
7121 | if (ret) { | |
7122 | mlog_errno(ret); | |
7123 | goto out_commit; | |
7124 | } | |
7125 | ||
7126 | /* | |
7127 | * Save two copies, one for insert, and one that can | |
7128 | * be changed by ocfs2_map_and_dirty_page() below. | |
7129 | */ | |
7130 | block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off); | |
7131 | ||
7132 | /* | |
7133 | * Non sparse file systems zero on extend, so no need | |
7134 | * to do that now. | |
7135 | */ | |
7136 | if (!ocfs2_sparse_alloc(osb) && | |
09cbfeaf KS |
7137 | PAGE_SIZE < osb->s_clustersize) |
7138 | end = PAGE_SIZE; | |
1afc32b9 MF |
7139 | |
7140 | ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages); | |
7141 | if (ret) { | |
7142 | mlog_errno(ret); | |
fb951eb5 | 7143 | need_free = 1; |
1afc32b9 MF |
7144 | goto out_commit; |
7145 | } | |
7146 | ||
7147 | /* | |
7148 | * This should populate the 1st page for us and mark | |
7149 | * it up to date. | |
7150 | */ | |
7151 | ret = ocfs2_read_inline_data(inode, pages[0], di_bh); | |
7152 | if (ret) { | |
7153 | mlog_errno(ret); | |
fb951eb5 | 7154 | need_free = 1; |
15eba0fe | 7155 | goto out_unlock; |
1afc32b9 MF |
7156 | } |
7157 | ||
09cbfeaf KS |
7158 | page_end = PAGE_SIZE; |
7159 | if (PAGE_SIZE > osb->s_clustersize) | |
1afc32b9 MF |
7160 | page_end = osb->s_clustersize; |
7161 | ||
7162 | for (i = 0; i < num_pages; i++) | |
7163 | ocfs2_map_and_dirty_page(inode, handle, 0, page_end, | |
7164 | pages[i], i > 0, &phys); | |
7165 | } | |
7166 | ||
7167 | spin_lock(&oi->ip_lock); | |
7168 | oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL; | |
7169 | di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features); | |
7170 | spin_unlock(&oi->ip_lock); | |
7171 | ||
2931cdcb | 7172 | ocfs2_update_inode_fsync_trans(handle, inode, 1); |
5b6a3a2b | 7173 | ocfs2_dinode_new_extent_list(inode, di); |
1afc32b9 MF |
7174 | |
7175 | ocfs2_journal_dirty(handle, di_bh); | |
7176 | ||
7177 | if (has_data) { | |
7178 | /* | |
7179 | * An error at this point should be extremely rare. If | |
7180 | * this proves to be false, we could always re-build | |
7181 | * the in-inode data from our pages. | |
7182 | */ | |
5e404e9e | 7183 | ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh); |
cc79d8c1 | 7184 | ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL); |
1afc32b9 MF |
7185 | if (ret) { |
7186 | mlog_errno(ret); | |
fb951eb5 | 7187 | need_free = 1; |
15eba0fe | 7188 | goto out_unlock; |
1afc32b9 MF |
7189 | } |
7190 | ||
7191 | inode->i_blocks = ocfs2_inode_sector_count(inode); | |
7192 | } | |
7193 | ||
15eba0fe | 7194 | out_unlock: |
7195 | if (pages) | |
7196 | ocfs2_unlock_and_free_pages(pages, num_pages); | |
7197 | ||
1afc32b9 | 7198 | out_commit: |
a90714c1 | 7199 | if (ret < 0 && did_quota) |
5dd4056d | 7200 | dquot_free_space_nodirty(inode, |
a90714c1 JK |
7201 | ocfs2_clusters_to_bytes(osb->sb, 1)); |
7202 | ||
fb951eb5 ZW |
7203 | if (need_free) { |
7204 | if (data_ac->ac_which == OCFS2_AC_USE_LOCAL) | |
7205 | ocfs2_free_local_alloc_bits(osb, handle, data_ac, | |
7206 | bit_off, num); | |
7207 | else | |
7208 | ocfs2_free_clusters(handle, | |
7209 | data_ac->ac_inode, | |
7210 | data_ac->ac_bh, | |
7211 | ocfs2_clusters_to_blocks(osb->sb, bit_off), | |
7212 | num); | |
7213 | } | |
7214 | ||
1afc32b9 MF |
7215 | ocfs2_commit_trans(osb, handle); |
7216 | ||
15eba0fe | 7217 | out: |
1afc32b9 MF |
7218 | if (data_ac) |
7219 | ocfs2_free_alloc_context(data_ac); | |
3cc79b79 | 7220 | free_pages: |
fd90d4df | 7221 | kfree(pages); |
1afc32b9 MF |
7222 | return ret; |
7223 | } | |
7224 | ||
ccd979bd MF |
7225 | /* |
7226 | * It is expected, that by the time you call this function, | |
7227 | * inode->i_size and fe->i_size have been adjusted. | |
7228 | * | |
7229 | * WARNING: This will kfree the truncate context | |
7230 | */ | |
7231 | int ocfs2_commit_truncate(struct ocfs2_super *osb, | |
7232 | struct inode *inode, | |
78f94673 | 7233 | struct buffer_head *di_bh) |
ccd979bd | 7234 | { |
78f94673 TY |
7235 | int status = 0, i, flags = 0; |
7236 | u32 new_highest_cpos, range, trunc_cpos, trunc_len, phys_cpos, coff; | |
bcbbb24a | 7237 | u64 blkno = 0; |
ccd979bd | 7238 | struct ocfs2_extent_list *el; |
78f94673 | 7239 | struct ocfs2_extent_rec *rec; |
dcd0538f | 7240 | struct ocfs2_path *path = NULL; |
78f94673 TY |
7241 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; |
7242 | struct ocfs2_extent_list *root_el = &(di->id2.i_list); | |
7243 | u64 refcount_loc = le64_to_cpu(di->i_refcount_loc); | |
7244 | struct ocfs2_extent_tree et; | |
7245 | struct ocfs2_cached_dealloc_ctxt dealloc; | |
f62f12b3 | 7246 | struct ocfs2_refcount_tree *ref_tree = NULL; |
ccd979bd | 7247 | |
78f94673 TY |
7248 | ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh); |
7249 | ocfs2_init_dealloc_ctxt(&dealloc); | |
7250 | ||
dcd0538f | 7251 | new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb, |
ccd979bd MF |
7252 | i_size_read(inode)); |
7253 | ||
78f94673 | 7254 | path = ocfs2_new_path(di_bh, &di->id2.i_list, |
13723d00 | 7255 | ocfs2_journal_access_di); |
dcd0538f MF |
7256 | if (!path) { |
7257 | status = -ENOMEM; | |
7258 | mlog_errno(status); | |
7259 | goto bail; | |
7260 | } | |
83418978 MF |
7261 | |
7262 | ocfs2_extent_map_trunc(inode, new_highest_cpos); | |
7263 | ||
ccd979bd | 7264 | start: |
3a0782d0 MF |
7265 | /* |
7266 | * Check that we still have allocation to delete. | |
7267 | */ | |
7268 | if (OCFS2_I(inode)->ip_clusters == 0) { | |
7269 | status = 0; | |
7270 | goto bail; | |
7271 | } | |
7272 | ||
dcd0538f MF |
7273 | /* |
7274 | * Truncate always works against the rightmost tree branch. | |
7275 | */ | |
facdb77f | 7276 | status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX); |
dcd0538f MF |
7277 | if (status) { |
7278 | mlog_errno(status); | |
7279 | goto bail; | |
ccd979bd MF |
7280 | } |
7281 | ||
a09d09b8 TM |
7282 | trace_ocfs2_commit_truncate( |
7283 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
7284 | new_highest_cpos, | |
7285 | OCFS2_I(inode)->ip_clusters, | |
7286 | path->p_tree_depth); | |
dcd0538f MF |
7287 | |
7288 | /* | |
7289 | * By now, el will point to the extent list on the bottom most | |
7290 | * portion of this tree. Only the tail record is considered in | |
7291 | * each pass. | |
7292 | * | |
7293 | * We handle the following cases, in order: | |
7294 | * - empty extent: delete the remaining branch | |
7295 | * - remove the entire record | |
7296 | * - remove a partial record | |
7297 | * - no record needs to be removed (truncate has completed) | |
7298 | */ | |
7299 | el = path_leaf_el(path); | |
3a0782d0 MF |
7300 | if (le16_to_cpu(el->l_next_free_rec) == 0) { |
7301 | ocfs2_error(inode->i_sb, | |
7302 | "Inode %llu has empty extent block at %llu\n", | |
7303 | (unsigned long long)OCFS2_I(inode)->ip_blkno, | |
7304 | (unsigned long long)path_leaf_bh(path)->b_blocknr); | |
7305 | status = -EROFS; | |
7306 | goto bail; | |
7307 | } | |
7308 | ||
ccd979bd | 7309 | i = le16_to_cpu(el->l_next_free_rec) - 1; |
78f94673 TY |
7310 | rec = &el->l_recs[i]; |
7311 | flags = rec->e_flags; | |
7312 | range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec); | |
7313 | ||
7314 | if (i == 0 && ocfs2_is_empty_extent(rec)) { | |
7315 | /* | |
7316 | * Lower levels depend on this never happening, but it's best | |
7317 | * to check it up here before changing the tree. | |
7318 | */ | |
7319 | if (root_el->l_tree_depth && rec->e_int_clusters == 0) { | |
d0c97d52 | 7320 | mlog(ML_ERROR, "Inode %lu has an empty " |
78f94673 TY |
7321 | "extent record, depth %u\n", inode->i_ino, |
7322 | le16_to_cpu(root_el->l_tree_depth)); | |
d0c97d52 X |
7323 | status = ocfs2_remove_rightmost_empty_extent(osb, |
7324 | &et, path, &dealloc); | |
7325 | if (status) { | |
7326 | mlog_errno(status); | |
7327 | goto bail; | |
7328 | } | |
7329 | ||
7330 | ocfs2_reinit_path(path, 1); | |
7331 | goto start; | |
7332 | } else { | |
7333 | trunc_cpos = le32_to_cpu(rec->e_cpos); | |
7334 | trunc_len = 0; | |
7335 | blkno = 0; | |
78f94673 | 7336 | } |
78f94673 TY |
7337 | } else if (le32_to_cpu(rec->e_cpos) >= new_highest_cpos) { |
7338 | /* | |
7339 | * Truncate entire record. | |
7340 | */ | |
7341 | trunc_cpos = le32_to_cpu(rec->e_cpos); | |
7342 | trunc_len = ocfs2_rec_clusters(el, rec); | |
7343 | blkno = le64_to_cpu(rec->e_blkno); | |
dcd0538f | 7344 | } else if (range > new_highest_cpos) { |
78f94673 TY |
7345 | /* |
7346 | * Partial truncate. it also should be | |
7347 | * the last truncate we're doing. | |
7348 | */ | |
7349 | trunc_cpos = new_highest_cpos; | |
7350 | trunc_len = range - new_highest_cpos; | |
7351 | coff = new_highest_cpos - le32_to_cpu(rec->e_cpos); | |
7352 | blkno = le64_to_cpu(rec->e_blkno) + | |
7353 | ocfs2_clusters_to_blocks(inode->i_sb, coff); | |
dcd0538f | 7354 | } else { |
78f94673 TY |
7355 | /* |
7356 | * Truncate completed, leave happily. | |
7357 | */ | |
dcd0538f MF |
7358 | status = 0; |
7359 | goto bail; | |
7360 | } | |
ccd979bd | 7361 | |
78f94673 | 7362 | phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno); |
bcbbb24a | 7363 | |
f62f12b3 JB |
7364 | if ((flags & OCFS2_EXT_REFCOUNTED) && trunc_len && !ref_tree) { |
7365 | status = ocfs2_lock_refcount_tree(osb, refcount_loc, 1, | |
7366 | &ref_tree, NULL); | |
7367 | if (status) { | |
7368 | mlog_errno(status); | |
7369 | goto bail; | |
7370 | } | |
7371 | } | |
7372 | ||
78f94673 TY |
7373 | status = ocfs2_remove_btree_range(inode, &et, trunc_cpos, |
7374 | phys_cpos, trunc_len, flags, &dealloc, | |
f62f12b3 | 7375 | refcount_loc, true); |
ccd979bd MF |
7376 | if (status < 0) { |
7377 | mlog_errno(status); | |
7378 | goto bail; | |
7379 | } | |
7380 | ||
dcd0538f MF |
7381 | ocfs2_reinit_path(path, 1); |
7382 | ||
7383 | /* | |
3a0782d0 MF |
7384 | * The check above will catch the case where we've truncated |
7385 | * away all allocation. | |
dcd0538f | 7386 | */ |
3a0782d0 MF |
7387 | goto start; |
7388 | ||
ccd979bd | 7389 | bail: |
f62f12b3 JB |
7390 | if (ref_tree) |
7391 | ocfs2_unlock_refcount_tree(osb, ref_tree, 1); | |
ccd979bd MF |
7392 | |
7393 | ocfs2_schedule_truncate_log_flush(osb, 1); | |
7394 | ||
78f94673 | 7395 | ocfs2_run_deallocs(osb, &dealloc); |
59a5e416 | 7396 | |
dcd0538f | 7397 | ocfs2_free_path(path); |
ccd979bd | 7398 | |
ccd979bd MF |
7399 | return status; |
7400 | } | |
7401 | ||
1afc32b9 MF |
7402 | /* |
7403 | * 'start' is inclusive, 'end' is not. | |
7404 | */ | |
7405 | int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh, | |
7406 | unsigned int start, unsigned int end, int trunc) | |
7407 | { | |
7408 | int ret; | |
7409 | unsigned int numbytes; | |
7410 | handle_t *handle; | |
7411 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | |
7412 | struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; | |
7413 | struct ocfs2_inline_data *idata = &di->id2.i_data; | |
7414 | ||
7415 | if (end > i_size_read(inode)) | |
7416 | end = i_size_read(inode); | |
7417 | ||
d62e74be | 7418 | BUG_ON(start > end); |
1afc32b9 MF |
7419 | |
7420 | if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) || | |
7421 | !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) || | |
7422 | !ocfs2_supports_inline_data(osb)) { | |
7423 | ocfs2_error(inode->i_sb, | |
7ecef14a | 7424 | "Inline data flags for inode %llu don't agree! Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n", |
1afc32b9 MF |
7425 | (unsigned long long)OCFS2_I(inode)->ip_blkno, |
7426 | le16_to_cpu(di->i_dyn_features), | |
7427 | OCFS2_I(inode)->ip_dyn_features, | |
7428 | osb->s_feature_incompat); | |
7429 | ret = -EROFS; | |
7430 | goto out; | |
7431 | } | |
7432 | ||
7433 | handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); | |
7434 | if (IS_ERR(handle)) { | |
7435 | ret = PTR_ERR(handle); | |
7436 | mlog_errno(ret); | |
7437 | goto out; | |
7438 | } | |
7439 | ||
0cf2f763 | 7440 | ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, |
13723d00 | 7441 | OCFS2_JOURNAL_ACCESS_WRITE); |
1afc32b9 MF |
7442 | if (ret) { |
7443 | mlog_errno(ret); | |
7444 | goto out_commit; | |
7445 | } | |
7446 | ||
7447 | numbytes = end - start; | |
7448 | memset(idata->id_data + start, 0, numbytes); | |
7449 | ||
7450 | /* | |
7451 | * No need to worry about the data page here - it's been | |
7452 | * truncated already and inline data doesn't need it for | |
7453 | * pushing zero's to disk, so we'll let readpage pick it up | |
7454 | * later. | |
7455 | */ | |
7456 | if (trunc) { | |
7457 | i_size_write(inode, start); | |
7458 | di->i_size = cpu_to_le64(start); | |
7459 | } | |
7460 | ||
7461 | inode->i_blocks = ocfs2_inode_sector_count(inode); | |
078cd827 | 7462 | inode->i_ctime = inode->i_mtime = current_time(inode); |
1afc32b9 MF |
7463 | |
7464 | di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec); | |
7465 | di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec); | |
7466 | ||
6fdb702d | 7467 | ocfs2_update_inode_fsync_trans(handle, inode, 1); |
1afc32b9 MF |
7468 | ocfs2_journal_dirty(handle, di_bh); |
7469 | ||
7470 | out_commit: | |
7471 | ocfs2_commit_trans(osb, handle); | |
7472 | ||
7473 | out: | |
7474 | return ret; | |
7475 | } | |
e80de36d TM |
7476 | |
7477 | static int ocfs2_trim_extent(struct super_block *sb, | |
7478 | struct ocfs2_group_desc *gd, | |
105ddc93 | 7479 | u64 group, u32 start, u32 count) |
e80de36d TM |
7480 | { |
7481 | u64 discard, bcount; | |
105ddc93 | 7482 | struct ocfs2_super *osb = OCFS2_SB(sb); |
e80de36d TM |
7483 | |
7484 | bcount = ocfs2_clusters_to_blocks(sb, count); | |
105ddc93 AS |
7485 | discard = ocfs2_clusters_to_blocks(sb, start); |
7486 | ||
7487 | /* | |
7488 | * For the first cluster group, the gd->bg_blkno is not at the start | |
7489 | * of the group, but at an offset from the start. If we add it while | |
7490 | * calculating discard for first group, we will wrongly start fstrim a | |
7491 | * few blocks after the desried start block and the range can cross | |
7492 | * over into the next cluster group. So, add it only if this is not | |
7493 | * the first cluster group. | |
7494 | */ | |
7495 | if (group != osb->first_cluster_group_blkno) | |
7496 | discard += le64_to_cpu(gd->bg_blkno); | |
e80de36d | 7497 | |
10fca35f TM |
7498 | trace_ocfs2_trim_extent(sb, (unsigned long long)discard, bcount); |
7499 | ||
e80de36d TM |
7500 | return sb_issue_discard(sb, discard, bcount, GFP_NOFS, 0); |
7501 | } | |
7502 | ||
7503 | static int ocfs2_trim_group(struct super_block *sb, | |
105ddc93 | 7504 | struct ocfs2_group_desc *gd, u64 group, |
e80de36d TM |
7505 | u32 start, u32 max, u32 minbits) |
7506 | { | |
7507 | int ret = 0, count = 0, next; | |
7508 | void *bitmap = gd->bg_bitmap; | |
7509 | ||
7510 | if (le16_to_cpu(gd->bg_free_bits_count) < minbits) | |
7511 | return 0; | |
7512 | ||
10fca35f TM |
7513 | trace_ocfs2_trim_group((unsigned long long)le64_to_cpu(gd->bg_blkno), |
7514 | start, max, minbits); | |
7515 | ||
e80de36d TM |
7516 | while (start < max) { |
7517 | start = ocfs2_find_next_zero_bit(bitmap, max, start); | |
7518 | if (start >= max) | |
7519 | break; | |
7520 | next = ocfs2_find_next_bit(bitmap, max, start); | |
7521 | ||
7522 | if ((next - start) >= minbits) { | |
105ddc93 | 7523 | ret = ocfs2_trim_extent(sb, gd, group, |
e80de36d TM |
7524 | start, next - start); |
7525 | if (ret < 0) { | |
7526 | mlog_errno(ret); | |
7527 | break; | |
7528 | } | |
7529 | count += next - start; | |
7530 | } | |
7531 | start = next + 1; | |
7532 | ||
7533 | if (fatal_signal_pending(current)) { | |
7534 | count = -ERESTARTSYS; | |
7535 | break; | |
7536 | } | |
7537 | ||
7538 | if ((le16_to_cpu(gd->bg_free_bits_count) - count) < minbits) | |
7539 | break; | |
7540 | } | |
7541 | ||
7542 | if (ret < 0) | |
7543 | count = ret; | |
7544 | ||
7545 | return count; | |
7546 | } | |
7547 | ||
7548 | int ocfs2_trim_fs(struct super_block *sb, struct fstrim_range *range) | |
7549 | { | |
7550 | struct ocfs2_super *osb = OCFS2_SB(sb); | |
7551 | u64 start, len, trimmed, first_group, last_group, group; | |
7552 | int ret, cnt; | |
7553 | u32 first_bit, last_bit, minlen; | |
7554 | struct buffer_head *main_bm_bh = NULL; | |
7555 | struct inode *main_bm_inode = NULL; | |
7556 | struct buffer_head *gd_bh = NULL; | |
7557 | struct ocfs2_dinode *main_bm; | |
7558 | struct ocfs2_group_desc *gd = NULL; | |
637dd20c | 7559 | struct ocfs2_trim_fs_info info, *pinfo = NULL; |
e80de36d TM |
7560 | |
7561 | start = range->start >> osb->s_clustersize_bits; | |
7562 | len = range->len >> osb->s_clustersize_bits; | |
7563 | minlen = range->minlen >> osb->s_clustersize_bits; | |
e80de36d | 7564 | |
aa89762c | 7565 | if (minlen >= osb->bitmap_cpg || range->len < sb->s_blocksize) |
e80de36d TM |
7566 | return -EINVAL; |
7567 | ||
7568 | main_bm_inode = ocfs2_get_system_file_inode(osb, | |
7569 | GLOBAL_BITMAP_SYSTEM_INODE, | |
7570 | OCFS2_INVALID_SLOT); | |
7571 | if (!main_bm_inode) { | |
7572 | ret = -EIO; | |
7573 | mlog_errno(ret); | |
7574 | goto out; | |
7575 | } | |
7576 | ||
5955102c | 7577 | inode_lock(main_bm_inode); |
e80de36d TM |
7578 | |
7579 | ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 0); | |
7580 | if (ret < 0) { | |
7581 | mlog_errno(ret); | |
7582 | goto out_mutex; | |
7583 | } | |
7584 | main_bm = (struct ocfs2_dinode *)main_bm_bh->b_data; | |
7585 | ||
7586 | if (start >= le32_to_cpu(main_bm->i_clusters)) { | |
7587 | ret = -EINVAL; | |
7588 | goto out_unlock; | |
7589 | } | |
7590 | ||
aa89762c | 7591 | len = range->len >> osb->s_clustersize_bits; |
e80de36d TM |
7592 | if (start + len > le32_to_cpu(main_bm->i_clusters)) |
7593 | len = le32_to_cpu(main_bm->i_clusters) - start; | |
7594 | ||
10fca35f TM |
7595 | trace_ocfs2_trim_fs(start, len, minlen); |
7596 | ||
637dd20c GH |
7597 | ocfs2_trim_fs_lock_res_init(osb); |
7598 | ret = ocfs2_trim_fs_lock(osb, NULL, 1); | |
7599 | if (ret < 0) { | |
7600 | if (ret != -EAGAIN) { | |
7601 | mlog_errno(ret); | |
7602 | ocfs2_trim_fs_lock_res_uninit(osb); | |
7603 | goto out_unlock; | |
7604 | } | |
7605 | ||
7606 | mlog(ML_NOTICE, "Wait for trim on device (%s) to " | |
7607 | "finish, which is running from another node.\n", | |
7608 | osb->dev_str); | |
7609 | ret = ocfs2_trim_fs_lock(osb, &info, 0); | |
7610 | if (ret < 0) { | |
7611 | mlog_errno(ret); | |
7612 | ocfs2_trim_fs_lock_res_uninit(osb); | |
7613 | goto out_unlock; | |
7614 | } | |
7615 | ||
7616 | if (info.tf_valid && info.tf_success && | |
7617 | info.tf_start == start && info.tf_len == len && | |
7618 | info.tf_minlen == minlen) { | |
7619 | /* Avoid sending duplicated trim to a shared device */ | |
7620 | mlog(ML_NOTICE, "The same trim on device (%s) was " | |
7621 | "just done from node (%u), return.\n", | |
7622 | osb->dev_str, info.tf_nodenum); | |
7623 | range->len = info.tf_trimlen; | |
7624 | goto out_trimunlock; | |
7625 | } | |
7626 | } | |
7627 | ||
7628 | info.tf_nodenum = osb->node_num; | |
7629 | info.tf_start = start; | |
7630 | info.tf_len = len; | |
7631 | info.tf_minlen = minlen; | |
7632 | ||
e80de36d TM |
7633 | /* Determine first and last group to examine based on start and len */ |
7634 | first_group = ocfs2_which_cluster_group(main_bm_inode, start); | |
7635 | if (first_group == osb->first_cluster_group_blkno) | |
7636 | first_bit = start; | |
7637 | else | |
7638 | first_bit = start - ocfs2_blocks_to_clusters(sb, first_group); | |
7639 | last_group = ocfs2_which_cluster_group(main_bm_inode, start + len - 1); | |
7640 | last_bit = osb->bitmap_cpg; | |
7641 | ||
aa89762c | 7642 | trimmed = 0; |
e80de36d TM |
7643 | for (group = first_group; group <= last_group;) { |
7644 | if (first_bit + len >= osb->bitmap_cpg) | |
7645 | last_bit = osb->bitmap_cpg; | |
7646 | else | |
7647 | last_bit = first_bit + len; | |
7648 | ||
7649 | ret = ocfs2_read_group_descriptor(main_bm_inode, | |
7650 | main_bm, group, | |
7651 | &gd_bh); | |
7652 | if (ret < 0) { | |
7653 | mlog_errno(ret); | |
7654 | break; | |
7655 | } | |
7656 | ||
7657 | gd = (struct ocfs2_group_desc *)gd_bh->b_data; | |
105ddc93 AS |
7658 | cnt = ocfs2_trim_group(sb, gd, group, |
7659 | first_bit, last_bit, minlen); | |
e80de36d TM |
7660 | brelse(gd_bh); |
7661 | gd_bh = NULL; | |
7662 | if (cnt < 0) { | |
7663 | ret = cnt; | |
7664 | mlog_errno(ret); | |
7665 | break; | |
7666 | } | |
7667 | ||
7668 | trimmed += cnt; | |
7669 | len -= osb->bitmap_cpg - first_bit; | |
7670 | first_bit = 0; | |
7671 | if (group == osb->first_cluster_group_blkno) | |
7672 | group = ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg); | |
7673 | else | |
7674 | group += ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg); | |
7675 | } | |
7676 | range->len = trimmed * sb->s_blocksize; | |
637dd20c GH |
7677 | |
7678 | info.tf_trimlen = range->len; | |
7679 | info.tf_success = (ret ? 0 : 1); | |
7680 | pinfo = &info; | |
7681 | out_trimunlock: | |
7682 | ocfs2_trim_fs_unlock(osb, pinfo); | |
7683 | ocfs2_trim_fs_lock_res_uninit(osb); | |
e80de36d TM |
7684 | out_unlock: |
7685 | ocfs2_inode_unlock(main_bm_inode, 0); | |
7686 | brelse(main_bm_bh); | |
7687 | out_mutex: | |
5955102c | 7688 | inode_unlock(main_bm_inode); |
e80de36d TM |
7689 | iput(main_bm_inode); |
7690 | out: | |
7691 | return ret; | |
7692 | } |