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