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