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