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