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