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