blob: ebfe36ab2d5e0a8ea436be80264f1f44de3c2992 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- 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>
Mark Fasheh60b11392007-02-16 11:46:50 -080030#include <linux/swap.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080031
32#define MLOG_MASK_PREFIX ML_DISK_ALLOC
33#include <cluster/masklog.h>
34
35#include "ocfs2.h"
36
37#include "alloc.h"
Mark Fasheh60b11392007-02-16 11:46:50 -080038#include "aops.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080039#include "dlmglue.h"
40#include "extent_map.h"
41#include "inode.h"
42#include "journal.h"
43#include "localalloc.h"
44#include "suballoc.h"
45#include "sysfile.h"
46#include "file.h"
47#include "super.h"
48#include "uptodate.h"
49
50#include "buffer_head_io.h"
51
Tao Mae7d4cb62008-08-18 17:38:44 +080052
Joel Becker1625f8a2008-08-21 17:11:10 -070053/*
54 * Operations for a specific extent tree type.
55 *
56 * To implement an on-disk btree (extent tree) type in ocfs2, add
57 * an ocfs2_extent_tree_operations structure and the matching
Joel Becker8d6220d2008-08-22 12:46:09 -070058 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
Joel Becker1625f8a2008-08-21 17:11:10 -070059 * for the allocation portion of the extent tree.
60 */
Tao Mae7d4cb62008-08-18 17:38:44 +080061struct ocfs2_extent_tree_operations {
Joel Becker1625f8a2008-08-21 17:11:10 -070062 /*
63 * last_eb_blk is the block number of the right most leaf extent
64 * block. Most on-disk structures containing an extent tree store
65 * this value for fast access. The ->eo_set_last_eb_blk() and
66 * ->eo_get_last_eb_blk() operations access this value. They are
67 * both required.
68 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070069 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
70 u64 blkno);
71 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -070072
73 /*
74 * The on-disk structure usually keeps track of how many total
75 * clusters are stored in this extent tree. This function updates
76 * that value. new_clusters is the delta, and must be
77 * added to the total. Required.
78 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070079 void (*eo_update_clusters)(struct inode *inode,
80 struct ocfs2_extent_tree *et,
81 u32 new_clusters);
Joel Becker1625f8a2008-08-21 17:11:10 -070082
83 /*
84 * If ->eo_insert_check() exists, it is called before rec is
85 * inserted into the extent tree. It is optional.
86 */
Joel Becker1e61ee72008-08-20 18:32:45 -070087 int (*eo_insert_check)(struct inode *inode,
88 struct ocfs2_extent_tree *et,
89 struct ocfs2_extent_rec *rec);
Joel Becker35dc0aa2008-08-20 16:25:06 -070090 int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
Joel Becker0ce10102008-08-20 17:19:50 -070091
Joel Becker1625f8a2008-08-21 17:11:10 -070092 /*
93 * --------------------------------------------------------------
94 * The remaining are internal to ocfs2_extent_tree and don't have
95 * accessor functions
96 */
97
98 /*
99 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
100 * It is required.
101 */
Joel Becker0ce10102008-08-20 17:19:50 -0700102 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -0700103
104 /*
105 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
106 * it exists. If it does not, et->et_max_leaf_clusters is set
107 * to 0 (unlimited). Optional.
108 */
Joel Becker943cced2008-08-20 17:31:10 -0700109 void (*eo_fill_max_leaf_clusters)(struct inode *inode,
110 struct ocfs2_extent_tree *et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800111};
112
Joel Beckerf99b9b72008-08-20 19:36:33 -0700113
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);
121static void ocfs2_dinode_update_clusters(struct inode *inode,
122 struct ocfs2_extent_tree *et,
123 u32 clusters);
124static int ocfs2_dinode_insert_check(struct inode *inode,
125 struct ocfs2_extent_tree *et,
126 struct ocfs2_extent_rec *rec);
127static int ocfs2_dinode_sanity_check(struct inode *inode,
128 struct ocfs2_extent_tree *et);
129static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
130static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
131 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
132 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
133 .eo_update_clusters = ocfs2_dinode_update_clusters,
134 .eo_insert_check = ocfs2_dinode_insert_check,
135 .eo_sanity_check = ocfs2_dinode_sanity_check,
136 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
Tao Mae7d4cb62008-08-18 17:38:44 +0800137};
138
139static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
140 u64 blkno)
141{
Joel Beckerea5efa12008-08-20 16:57:27 -0700142 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800143
Joel Beckerf99b9b72008-08-20 19:36:33 -0700144 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800145 di->i_last_eb_blk = cpu_to_le64(blkno);
146}
147
148static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
149{
Joel Beckerea5efa12008-08-20 16:57:27 -0700150 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800151
Joel Beckerf99b9b72008-08-20 19:36:33 -0700152 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800153 return le64_to_cpu(di->i_last_eb_blk);
154}
155
156static void ocfs2_dinode_update_clusters(struct inode *inode,
157 struct ocfs2_extent_tree *et,
158 u32 clusters)
159{
Joel Beckerea5efa12008-08-20 16:57:27 -0700160 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800161
162 le32_add_cpu(&di->i_clusters, clusters);
163 spin_lock(&OCFS2_I(inode)->ip_lock);
164 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
165 spin_unlock(&OCFS2_I(inode)->ip_lock);
166}
167
Joel Becker1e61ee72008-08-20 18:32:45 -0700168static int ocfs2_dinode_insert_check(struct inode *inode,
169 struct ocfs2_extent_tree *et,
170 struct ocfs2_extent_rec *rec)
171{
172 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
173
174 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
175 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
176 (OCFS2_I(inode)->ip_clusters != rec->e_cpos),
177 "Device %s, asking for sparse allocation: inode %llu, "
178 "cpos %u, clusters %u\n",
179 osb->dev_str,
180 (unsigned long long)OCFS2_I(inode)->ip_blkno,
181 rec->e_cpos,
182 OCFS2_I(inode)->ip_clusters);
183
184 return 0;
185}
186
Tao Mae7d4cb62008-08-18 17:38:44 +0800187static int ocfs2_dinode_sanity_check(struct inode *inode,
188 struct ocfs2_extent_tree *et)
189{
190 int ret = 0;
191 struct ocfs2_dinode *di;
192
Joel Beckerf99b9b72008-08-20 19:36:33 -0700193 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800194
Joel Beckerea5efa12008-08-20 16:57:27 -0700195 di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800196 if (!OCFS2_IS_VALID_DINODE(di)) {
197 ret = -EIO;
198 ocfs2_error(inode->i_sb,
199 "Inode %llu has invalid path root",
200 (unsigned long long)OCFS2_I(inode)->ip_blkno);
201 }
202
203 return ret;
204}
205
Joel Beckerf99b9b72008-08-20 19:36:33 -0700206static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
207{
208 struct ocfs2_dinode *di = et->et_object;
209
210 et->et_root_el = &di->id2.i_list;
211}
212
Tao Mae7d4cb62008-08-18 17:38:44 +0800213
Joel Becker0ce10102008-08-20 17:19:50 -0700214static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
215{
216 struct ocfs2_xattr_value_root *xv = et->et_object;
217
218 et->et_root_el = &xv->xr_list;
219}
220
Tao Maf56654c2008-08-18 17:38:48 +0800221static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
222 u64 blkno)
223{
224 struct ocfs2_xattr_value_root *xv =
Joel Beckerea5efa12008-08-20 16:57:27 -0700225 (struct ocfs2_xattr_value_root *)et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800226
227 xv->xr_last_eb_blk = cpu_to_le64(blkno);
228}
229
230static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
231{
232 struct ocfs2_xattr_value_root *xv =
Joel Beckerea5efa12008-08-20 16:57:27 -0700233 (struct ocfs2_xattr_value_root *) et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800234
235 return le64_to_cpu(xv->xr_last_eb_blk);
236}
237
238static void ocfs2_xattr_value_update_clusters(struct inode *inode,
239 struct ocfs2_extent_tree *et,
240 u32 clusters)
241{
242 struct ocfs2_xattr_value_root *xv =
Joel Beckerea5efa12008-08-20 16:57:27 -0700243 (struct ocfs2_xattr_value_root *)et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800244
245 le32_add_cpu(&xv->xr_clusters, clusters);
246}
247
Joel Becker1a09f552008-08-20 17:44:24 -0700248static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700249 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
250 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
251 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700252 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800253};
254
Joel Becker0ce10102008-08-20 17:19:50 -0700255static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
256{
257 struct ocfs2_xattr_block *xb = et->et_object;
258
259 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
260}
261
Joel Becker943cced2008-08-20 17:31:10 -0700262static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode *inode,
263 struct ocfs2_extent_tree *et)
264{
265 et->et_max_leaf_clusters =
266 ocfs2_clusters_for_bytes(inode->i_sb,
267 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
268}
269
Tao Maba492612008-08-18 17:38:49 +0800270static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
271 u64 blkno)
272{
Joel Beckerea5efa12008-08-20 16:57:27 -0700273 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800274 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
275
276 xt->xt_last_eb_blk = cpu_to_le64(blkno);
277}
278
279static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
280{
Joel Beckerea5efa12008-08-20 16:57:27 -0700281 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800282 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
283
284 return le64_to_cpu(xt->xt_last_eb_blk);
285}
286
287static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
288 struct ocfs2_extent_tree *et,
289 u32 clusters)
290{
Joel Beckerea5efa12008-08-20 16:57:27 -0700291 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800292
293 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
294}
295
Tao Maba492612008-08-18 17:38:49 +0800296static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700297 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
298 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
299 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700300 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
Joel Becker943cced2008-08-20 17:31:10 -0700301 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
Tao Maba492612008-08-18 17:38:49 +0800302};
303
Joel Becker8d6220d2008-08-22 12:46:09 -0700304static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
305 struct inode *inode,
306 struct buffer_head *bh,
307 void *obj,
308 struct ocfs2_extent_tree_operations *ops)
Tao Mae7d4cb62008-08-18 17:38:44 +0800309{
Joel Becker1a09f552008-08-20 17:44:24 -0700310 et->et_ops = ops;
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700311 et->et_root_bh = bh;
Joel Beckerea5efa12008-08-20 16:57:27 -0700312 if (!obj)
313 obj = (void *)bh->b_data;
314 et->et_object = obj;
Tao Mae7d4cb62008-08-18 17:38:44 +0800315
Joel Becker0ce10102008-08-20 17:19:50 -0700316 et->et_ops->eo_fill_root_el(et);
Joel Becker943cced2008-08-20 17:31:10 -0700317 if (!et->et_ops->eo_fill_max_leaf_clusters)
318 et->et_max_leaf_clusters = 0;
319 else
320 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800321}
322
Joel Becker8d6220d2008-08-22 12:46:09 -0700323void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
324 struct inode *inode,
325 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700326{
Joel Becker8d6220d2008-08-22 12:46:09 -0700327 __ocfs2_init_extent_tree(et, inode, bh, NULL, &ocfs2_dinode_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700328}
329
Joel Becker8d6220d2008-08-22 12:46:09 -0700330void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700331 struct inode *inode,
Joel Becker8d6220d2008-08-22 12:46:09 -0700332 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700333{
Joel Becker8d6220d2008-08-22 12:46:09 -0700334 __ocfs2_init_extent_tree(et, inode, bh, NULL,
335 &ocfs2_xattr_tree_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700336}
337
Joel Becker8d6220d2008-08-22 12:46:09 -0700338void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
339 struct inode *inode,
340 struct buffer_head *bh,
341 struct ocfs2_xattr_value_root *xv)
Tao Mae7d4cb62008-08-18 17:38:44 +0800342{
Joel Becker8d6220d2008-08-22 12:46:09 -0700343 __ocfs2_init_extent_tree(et, inode, bh, xv,
344 &ocfs2_xattr_value_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800345}
346
Joel Becker35dc0aa2008-08-20 16:25:06 -0700347static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
348 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800349{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700350 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800351}
352
Joel Becker35dc0aa2008-08-20 16:25:06 -0700353static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800354{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700355 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800356}
357
Joel Becker35dc0aa2008-08-20 16:25:06 -0700358static inline void ocfs2_et_update_clusters(struct inode *inode,
359 struct ocfs2_extent_tree *et,
360 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800361{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700362 et->et_ops->eo_update_clusters(inode, et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700363}
364
Joel Becker1e61ee72008-08-20 18:32:45 -0700365static inline int ocfs2_et_insert_check(struct inode *inode,
366 struct ocfs2_extent_tree *et,
367 struct ocfs2_extent_rec *rec)
368{
369 int ret = 0;
370
371 if (et->et_ops->eo_insert_check)
372 ret = et->et_ops->eo_insert_check(inode, et, rec);
373 return ret;
374}
375
Joel Becker35dc0aa2008-08-20 16:25:06 -0700376static inline int ocfs2_et_sanity_check(struct inode *inode,
377 struct ocfs2_extent_tree *et)
378{
Joel Becker1e61ee72008-08-20 18:32:45 -0700379 int ret = 0;
380
381 if (et->et_ops->eo_sanity_check)
382 ret = et->et_ops->eo_sanity_check(inode, et);
383 return ret;
Tao Mae7d4cb62008-08-18 17:38:44 +0800384}
385
Mark Fashehccd979b2005-12-15 14:31:24 -0800386static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
Mark Fasheh59a5e412007-06-22 15:52:36 -0700387static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
388 struct ocfs2_extent_block *eb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800389
Mark Fashehdcd05382007-01-16 11:32:23 -0800390/*
391 * Structures which describe a path through a btree, and functions to
392 * manipulate them.
393 *
394 * The idea here is to be as generic as possible with the tree
395 * manipulation code.
396 */
397struct ocfs2_path_item {
398 struct buffer_head *bh;
399 struct ocfs2_extent_list *el;
400};
401
402#define OCFS2_MAX_PATH_DEPTH 5
403
404struct ocfs2_path {
405 int p_tree_depth;
406 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
407};
408
409#define path_root_bh(_path) ((_path)->p_node[0].bh)
410#define path_root_el(_path) ((_path)->p_node[0].el)
411#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
412#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
413#define path_num_items(_path) ((_path)->p_tree_depth + 1)
414
415/*
416 * Reset the actual path elements so that we can re-use the structure
417 * to build another path. Generally, this involves freeing the buffer
418 * heads.
419 */
420static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
421{
422 int i, start = 0, depth = 0;
423 struct ocfs2_path_item *node;
424
425 if (keep_root)
426 start = 1;
427
428 for(i = start; i < path_num_items(path); i++) {
429 node = &path->p_node[i];
430
431 brelse(node->bh);
432 node->bh = NULL;
433 node->el = NULL;
434 }
435
436 /*
437 * Tree depth may change during truncate, or insert. If we're
438 * keeping the root extent list, then make sure that our path
439 * structure reflects the proper depth.
440 */
441 if (keep_root)
442 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
443
444 path->p_tree_depth = depth;
445}
446
447static void ocfs2_free_path(struct ocfs2_path *path)
448{
449 if (path) {
450 ocfs2_reinit_path(path, 0);
451 kfree(path);
452 }
453}
454
455/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700456 * All the elements of src into dest. After this call, src could be freed
457 * without affecting dest.
458 *
459 * Both paths should have the same root. Any non-root elements of dest
460 * will be freed.
461 */
462static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
463{
464 int i;
465
466 BUG_ON(path_root_bh(dest) != path_root_bh(src));
467 BUG_ON(path_root_el(dest) != path_root_el(src));
468
469 ocfs2_reinit_path(dest, 1);
470
471 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
472 dest->p_node[i].bh = src->p_node[i].bh;
473 dest->p_node[i].el = src->p_node[i].el;
474
475 if (dest->p_node[i].bh)
476 get_bh(dest->p_node[i].bh);
477 }
478}
479
480/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800481 * Make the *dest path the same as src and re-initialize src path to
482 * have a root only.
483 */
484static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
485{
486 int i;
487
488 BUG_ON(path_root_bh(dest) != path_root_bh(src));
489
490 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
491 brelse(dest->p_node[i].bh);
492
493 dest->p_node[i].bh = src->p_node[i].bh;
494 dest->p_node[i].el = src->p_node[i].el;
495
496 src->p_node[i].bh = NULL;
497 src->p_node[i].el = NULL;
498 }
499}
500
501/*
502 * Insert an extent block at given index.
503 *
504 * This will not take an additional reference on eb_bh.
505 */
506static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
507 struct buffer_head *eb_bh)
508{
509 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
510
511 /*
512 * Right now, no root bh is an extent block, so this helps
513 * catch code errors with dinode trees. The assertion can be
514 * safely removed if we ever need to insert extent block
515 * structures at the root.
516 */
517 BUG_ON(index == 0);
518
519 path->p_node[index].bh = eb_bh;
520 path->p_node[index].el = &eb->h_list;
521}
522
523static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
524 struct ocfs2_extent_list *root_el)
525{
526 struct ocfs2_path *path;
527
528 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
529
530 path = kzalloc(sizeof(*path), GFP_NOFS);
531 if (path) {
532 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
533 get_bh(root_bh);
534 path_root_bh(path) = root_bh;
535 path_root_el(path) = root_el;
536 }
537
538 return path;
539}
540
541/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800542 * Convenience function to journal all components in a path.
543 */
544static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
545 struct ocfs2_path *path)
546{
547 int i, ret = 0;
548
549 if (!path)
550 goto out;
551
552 for(i = 0; i < path_num_items(path); i++) {
553 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
554 OCFS2_JOURNAL_ACCESS_WRITE);
555 if (ret < 0) {
556 mlog_errno(ret);
557 goto out;
558 }
559 }
560
561out:
562 return ret;
563}
564
Mark Fasheh328d5752007-06-18 10:48:04 -0700565/*
566 * Return the index of the extent record which contains cluster #v_cluster.
567 * -1 is returned if it was not found.
568 *
569 * Should work fine on interior and exterior nodes.
570 */
571int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
572{
573 int ret = -1;
574 int i;
575 struct ocfs2_extent_rec *rec;
576 u32 rec_end, rec_start, clusters;
577
578 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
579 rec = &el->l_recs[i];
580
581 rec_start = le32_to_cpu(rec->e_cpos);
582 clusters = ocfs2_rec_clusters(el, rec);
583
584 rec_end = rec_start + clusters;
585
586 if (v_cluster >= rec_start && v_cluster < rec_end) {
587 ret = i;
588 break;
589 }
590 }
591
592 return ret;
593}
594
Mark Fashehdcd05382007-01-16 11:32:23 -0800595enum ocfs2_contig_type {
596 CONTIG_NONE = 0,
597 CONTIG_LEFT,
Mark Fasheh328d5752007-06-18 10:48:04 -0700598 CONTIG_RIGHT,
599 CONTIG_LEFTRIGHT,
Mark Fashehdcd05382007-01-16 11:32:23 -0800600};
601
Mark Fashehe48edee2007-03-07 16:46:57 -0800602
603/*
604 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
605 * ocfs2_extent_contig only work properly against leaf nodes!
606 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800607static int ocfs2_block_extent_contig(struct super_block *sb,
608 struct ocfs2_extent_rec *ext,
609 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800610{
Mark Fashehe48edee2007-03-07 16:46:57 -0800611 u64 blk_end = le64_to_cpu(ext->e_blkno);
612
613 blk_end += ocfs2_clusters_to_blocks(sb,
614 le16_to_cpu(ext->e_leaf_clusters));
615
616 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800617}
618
Mark Fashehdcd05382007-01-16 11:32:23 -0800619static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
620 struct ocfs2_extent_rec *right)
621{
Mark Fashehe48edee2007-03-07 16:46:57 -0800622 u32 left_range;
623
624 left_range = le32_to_cpu(left->e_cpos) +
625 le16_to_cpu(left->e_leaf_clusters);
626
627 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800628}
629
630static enum ocfs2_contig_type
631 ocfs2_extent_contig(struct inode *inode,
632 struct ocfs2_extent_rec *ext,
633 struct ocfs2_extent_rec *insert_rec)
634{
635 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
636
Mark Fasheh328d5752007-06-18 10:48:04 -0700637 /*
638 * Refuse to coalesce extent records with different flag
639 * fields - we don't want to mix unwritten extents with user
640 * data.
641 */
642 if (ext->e_flags != insert_rec->e_flags)
643 return CONTIG_NONE;
644
Mark Fashehdcd05382007-01-16 11:32:23 -0800645 if (ocfs2_extents_adjacent(ext, insert_rec) &&
646 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
647 return CONTIG_RIGHT;
648
649 blkno = le64_to_cpu(ext->e_blkno);
650 if (ocfs2_extents_adjacent(insert_rec, ext) &&
651 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
652 return CONTIG_LEFT;
653
654 return CONTIG_NONE;
655}
656
657/*
658 * NOTE: We can have pretty much any combination of contiguousness and
659 * appending.
660 *
661 * The usefulness of APPEND_TAIL is more in that it lets us know that
662 * we'll have to update the path to that leaf.
663 */
664enum ocfs2_append_type {
665 APPEND_NONE = 0,
666 APPEND_TAIL,
667};
668
Mark Fasheh328d5752007-06-18 10:48:04 -0700669enum ocfs2_split_type {
670 SPLIT_NONE = 0,
671 SPLIT_LEFT,
672 SPLIT_RIGHT,
673};
674
Mark Fashehdcd05382007-01-16 11:32:23 -0800675struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700676 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800677 enum ocfs2_append_type ins_appending;
678 enum ocfs2_contig_type ins_contig;
679 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800680 int ins_tree_depth;
681};
682
Mark Fasheh328d5752007-06-18 10:48:04 -0700683struct ocfs2_merge_ctxt {
684 enum ocfs2_contig_type c_contig_type;
685 int c_has_empty_extent;
686 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700687};
688
Mark Fashehccd979b2005-12-15 14:31:24 -0800689/*
690 * How many free extents have we got before we need more meta data?
691 */
692int ocfs2_num_free_extents(struct ocfs2_super *osb,
693 struct inode *inode,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700694 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -0800695{
696 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800697 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800698 struct ocfs2_extent_block *eb;
699 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800700 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800701
702 mlog_entry_void();
703
Joel Beckerf99b9b72008-08-20 19:36:33 -0700704 el = et->et_root_el;
705 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800706
Tao Mae7d4cb62008-08-18 17:38:44 +0800707 if (last_eb_blk) {
708 retval = ocfs2_read_block(osb, last_eb_blk,
Mark Fashehccd979b2005-12-15 14:31:24 -0800709 &eb_bh, OCFS2_BH_CACHED, inode);
710 if (retval < 0) {
711 mlog_errno(retval);
712 goto bail;
713 }
714 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
715 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800716 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800717
718 BUG_ON(el->l_tree_depth != 0);
719
720 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
721bail:
722 if (eb_bh)
723 brelse(eb_bh);
724
725 mlog_exit(retval);
726 return retval;
727}
728
729/* expects array to already be allocated
730 *
731 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
732 * l_count for you
733 */
734static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700735 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800736 struct inode *inode,
737 int wanted,
738 struct ocfs2_alloc_context *meta_ac,
739 struct buffer_head *bhs[])
740{
741 int count, status, i;
742 u16 suballoc_bit_start;
743 u32 num_got;
744 u64 first_blkno;
745 struct ocfs2_extent_block *eb;
746
747 mlog_entry_void();
748
749 count = 0;
750 while (count < wanted) {
751 status = ocfs2_claim_metadata(osb,
752 handle,
753 meta_ac,
754 wanted - count,
755 &suballoc_bit_start,
756 &num_got,
757 &first_blkno);
758 if (status < 0) {
759 mlog_errno(status);
760 goto bail;
761 }
762
763 for(i = count; i < (num_got + count); i++) {
764 bhs[i] = sb_getblk(osb->sb, first_blkno);
765 if (bhs[i] == NULL) {
766 status = -EIO;
767 mlog_errno(status);
768 goto bail;
769 }
770 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
771
772 status = ocfs2_journal_access(handle, inode, bhs[i],
773 OCFS2_JOURNAL_ACCESS_CREATE);
774 if (status < 0) {
775 mlog_errno(status);
776 goto bail;
777 }
778
779 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
780 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
781 /* Ok, setup the minimal stuff here. */
782 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
783 eb->h_blkno = cpu_to_le64(first_blkno);
784 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800785 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800786 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
787 eb->h_list.l_count =
788 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
789
790 suballoc_bit_start++;
791 first_blkno++;
792
793 /* We'll also be dirtied by the caller, so
794 * this isn't absolutely necessary. */
795 status = ocfs2_journal_dirty(handle, bhs[i]);
796 if (status < 0) {
797 mlog_errno(status);
798 goto bail;
799 }
800 }
801
802 count += num_got;
803 }
804
805 status = 0;
806bail:
807 if (status < 0) {
808 for(i = 0; i < wanted; i++) {
809 if (bhs[i])
810 brelse(bhs[i]);
811 bhs[i] = NULL;
812 }
813 }
814 mlog_exit(status);
815 return status;
816}
817
818/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800819 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
820 *
821 * Returns the sum of the rightmost extent rec logical offset and
822 * cluster count.
823 *
824 * ocfs2_add_branch() uses this to determine what logical cluster
825 * value should be populated into the leftmost new branch records.
826 *
827 * ocfs2_shift_tree_depth() uses this to determine the # clusters
828 * value for the new topmost tree record.
829 */
830static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
831{
832 int i;
833
834 i = le16_to_cpu(el->l_next_free_rec) - 1;
835
836 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -0800837 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -0800838}
839
840/*
Mark Fashehccd979b2005-12-15 14:31:24 -0800841 * Add an entire tree branch to our inode. eb_bh is the extent block
842 * to start at, if we don't want to start the branch at the dinode
843 * structure.
844 *
845 * last_eb_bh is required as we have to update it's next_leaf pointer
846 * for the new last extent block.
847 *
848 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -0800849 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -0800850 */
851static int ocfs2_add_branch(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700852 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800853 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +0800854 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -0800855 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -0700856 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800857 struct ocfs2_alloc_context *meta_ac)
858{
859 int status, new_blocks, i;
860 u64 next_blkno, new_last_eb_blk;
861 struct buffer_head *bh;
862 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800863 struct ocfs2_extent_block *eb;
864 struct ocfs2_extent_list *eb_el;
865 struct ocfs2_extent_list *el;
Mark Fashehdcd05382007-01-16 11:32:23 -0800866 u32 new_cpos;
Mark Fashehccd979b2005-12-15 14:31:24 -0800867
868 mlog_entry_void();
869
Mark Fasheh328d5752007-06-18 10:48:04 -0700870 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800871
Mark Fashehccd979b2005-12-15 14:31:24 -0800872 if (eb_bh) {
873 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
874 el = &eb->h_list;
875 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700876 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -0800877
878 /* we never add a branch to a leaf. */
879 BUG_ON(!el->l_tree_depth);
880
881 new_blocks = le16_to_cpu(el->l_tree_depth);
882
883 /* allocate the number of new eb blocks we need */
884 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
885 GFP_KERNEL);
886 if (!new_eb_bhs) {
887 status = -ENOMEM;
888 mlog_errno(status);
889 goto bail;
890 }
891
892 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
893 meta_ac, new_eb_bhs);
894 if (status < 0) {
895 mlog_errno(status);
896 goto bail;
897 }
898
Mark Fasheh328d5752007-06-18 10:48:04 -0700899 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
Mark Fashehdcd05382007-01-16 11:32:23 -0800900 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
901
Mark Fashehccd979b2005-12-15 14:31:24 -0800902 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
903 * linked with the rest of the tree.
904 * conversly, new_eb_bhs[0] is the new bottommost leaf.
905 *
906 * when we leave the loop, new_last_eb_blk will point to the
907 * newest leaf, and next_blkno will point to the topmost extent
908 * block. */
909 next_blkno = new_last_eb_blk = 0;
910 for(i = 0; i < new_blocks; i++) {
911 bh = new_eb_bhs[i];
912 eb = (struct ocfs2_extent_block *) bh->b_data;
913 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
914 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
915 status = -EIO;
916 goto bail;
917 }
918 eb_el = &eb->h_list;
919
920 status = ocfs2_journal_access(handle, inode, bh,
921 OCFS2_JOURNAL_ACCESS_CREATE);
922 if (status < 0) {
923 mlog_errno(status);
924 goto bail;
925 }
926
927 eb->h_next_leaf_blk = 0;
928 eb_el->l_tree_depth = cpu_to_le16(i);
929 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -0800930 /*
931 * This actually counts as an empty extent as
932 * c_clusters == 0
933 */
934 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -0800935 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -0800936 /*
937 * eb_el isn't always an interior node, but even leaf
938 * nodes want a zero'd flags and reserved field so
939 * this gets the whole 32 bits regardless of use.
940 */
941 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800942 if (!eb_el->l_tree_depth)
943 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
944
945 status = ocfs2_journal_dirty(handle, bh);
946 if (status < 0) {
947 mlog_errno(status);
948 goto bail;
949 }
950
951 next_blkno = le64_to_cpu(eb->h_blkno);
952 }
953
954 /* This is a bit hairy. We want to update up to three blocks
955 * here without leaving any of them in an inconsistent state
956 * in case of error. We don't have to worry about
957 * journal_dirty erroring as it won't unless we've aborted the
958 * handle (in which case we would never be here) so reserving
959 * the write with journal_access is all we need to do. */
Mark Fasheh328d5752007-06-18 10:48:04 -0700960 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800961 OCFS2_JOURNAL_ACCESS_WRITE);
962 if (status < 0) {
963 mlog_errno(status);
964 goto bail;
965 }
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700966 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800967 OCFS2_JOURNAL_ACCESS_WRITE);
968 if (status < 0) {
969 mlog_errno(status);
970 goto bail;
971 }
972 if (eb_bh) {
973 status = ocfs2_journal_access(handle, inode, eb_bh,
974 OCFS2_JOURNAL_ACCESS_WRITE);
975 if (status < 0) {
976 mlog_errno(status);
977 goto bail;
978 }
979 }
980
981 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +0800982 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800983 i = le16_to_cpu(el->l_next_free_rec);
984 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -0800985 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -0800986 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800987 le16_add_cpu(&el->l_next_free_rec, 1);
988
989 /* fe needs a new last extent block pointer, as does the
990 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -0700991 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -0800992
Mark Fasheh328d5752007-06-18 10:48:04 -0700993 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800994 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
995
Mark Fasheh328d5752007-06-18 10:48:04 -0700996 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800997 if (status < 0)
998 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700999 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001000 if (status < 0)
1001 mlog_errno(status);
1002 if (eb_bh) {
1003 status = ocfs2_journal_dirty(handle, eb_bh);
1004 if (status < 0)
1005 mlog_errno(status);
1006 }
1007
Mark Fasheh328d5752007-06-18 10:48:04 -07001008 /*
1009 * Some callers want to track the rightmost leaf so pass it
1010 * back here.
1011 */
1012 brelse(*last_eb_bh);
1013 get_bh(new_eb_bhs[0]);
1014 *last_eb_bh = new_eb_bhs[0];
1015
Mark Fashehccd979b2005-12-15 14:31:24 -08001016 status = 0;
1017bail:
1018 if (new_eb_bhs) {
1019 for (i = 0; i < new_blocks; i++)
1020 if (new_eb_bhs[i])
1021 brelse(new_eb_bhs[i]);
1022 kfree(new_eb_bhs);
1023 }
1024
1025 mlog_exit(status);
1026 return status;
1027}
1028
1029/*
1030 * adds another level to the allocation tree.
1031 * returns back the new extent block so you can add a branch to it
1032 * after this call.
1033 */
1034static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001035 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001036 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001037 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001038 struct ocfs2_alloc_context *meta_ac,
1039 struct buffer_head **ret_new_eb_bh)
1040{
1041 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001042 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001043 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001044 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001045 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001046 struct ocfs2_extent_list *eb_el;
1047
1048 mlog_entry_void();
1049
1050 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
1051 &new_eb_bh);
1052 if (status < 0) {
1053 mlog_errno(status);
1054 goto bail;
1055 }
1056
1057 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1058 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1059 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1060 status = -EIO;
1061 goto bail;
1062 }
1063
1064 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001065 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001066
1067 status = ocfs2_journal_access(handle, inode, new_eb_bh,
1068 OCFS2_JOURNAL_ACCESS_CREATE);
1069 if (status < 0) {
1070 mlog_errno(status);
1071 goto bail;
1072 }
1073
Tao Mae7d4cb62008-08-18 17:38:44 +08001074 /* copy the root extent list data into the new extent block */
1075 eb_el->l_tree_depth = root_el->l_tree_depth;
1076 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1077 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1078 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001079
1080 status = ocfs2_journal_dirty(handle, new_eb_bh);
1081 if (status < 0) {
1082 mlog_errno(status);
1083 goto bail;
1084 }
1085
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001086 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001087 OCFS2_JOURNAL_ACCESS_WRITE);
1088 if (status < 0) {
1089 mlog_errno(status);
1090 goto bail;
1091 }
1092
Mark Fashehdcd05382007-01-16 11:32:23 -08001093 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1094
Tao Mae7d4cb62008-08-18 17:38:44 +08001095 /* update root_bh now */
1096 le16_add_cpu(&root_el->l_tree_depth, 1);
1097 root_el->l_recs[0].e_cpos = 0;
1098 root_el->l_recs[0].e_blkno = eb->h_blkno;
1099 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1100 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1101 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1102 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001103
1104 /* If this is our 1st tree depth shift, then last_eb_blk
1105 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001106 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001107 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001108
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001109 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001110 if (status < 0) {
1111 mlog_errno(status);
1112 goto bail;
1113 }
1114
1115 *ret_new_eb_bh = new_eb_bh;
1116 new_eb_bh = NULL;
1117 status = 0;
1118bail:
1119 if (new_eb_bh)
1120 brelse(new_eb_bh);
1121
1122 mlog_exit(status);
1123 return status;
1124}
1125
1126/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001127 * Should only be called when there is no space left in any of the
1128 * leaf nodes. What we want to do is find the lowest tree depth
1129 * non-leaf extent block with room for new records. There are three
1130 * valid results of this search:
1131 *
1132 * 1) a lowest extent block is found, then we pass it back in
1133 * *lowest_eb_bh and return '0'
1134 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001135 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001136 * pass NULL back in *lowest_eb_bh, but still return '0'
1137 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001138 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001139 * which case we return > 0
1140 *
1141 * return status < 0 indicates an error.
1142 */
1143static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1144 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001145 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001146 struct buffer_head **target_bh)
1147{
1148 int status = 0, i;
1149 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001150 struct ocfs2_extent_block *eb;
1151 struct ocfs2_extent_list *el;
1152 struct buffer_head *bh = NULL;
1153 struct buffer_head *lowest_bh = NULL;
1154
1155 mlog_entry_void();
1156
1157 *target_bh = NULL;
1158
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001159 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001160
1161 while(le16_to_cpu(el->l_tree_depth) > 1) {
1162 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Mark Fashehb06970532006-03-03 10:24:33 -08001163 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001164 "extent list (next_free_rec == 0)",
Mark Fashehb06970532006-03-03 10:24:33 -08001165 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001166 status = -EIO;
1167 goto bail;
1168 }
1169 i = le16_to_cpu(el->l_next_free_rec) - 1;
1170 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1171 if (!blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -08001172 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001173 "list where extent # %d has no physical "
1174 "block start",
Mark Fashehb06970532006-03-03 10:24:33 -08001175 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001176 status = -EIO;
1177 goto bail;
1178 }
1179
1180 if (bh) {
1181 brelse(bh);
1182 bh = NULL;
1183 }
1184
1185 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
1186 inode);
1187 if (status < 0) {
1188 mlog_errno(status);
1189 goto bail;
1190 }
1191
1192 eb = (struct ocfs2_extent_block *) bh->b_data;
1193 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1194 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1195 status = -EIO;
1196 goto bail;
1197 }
1198 el = &eb->h_list;
1199
1200 if (le16_to_cpu(el->l_next_free_rec) <
1201 le16_to_cpu(el->l_count)) {
1202 if (lowest_bh)
1203 brelse(lowest_bh);
1204 lowest_bh = bh;
1205 get_bh(lowest_bh);
1206 }
1207 }
1208
1209 /* If we didn't find one and the fe doesn't have any room,
1210 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001211 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001212 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001213 status = 1;
1214
1215 *target_bh = lowest_bh;
1216bail:
1217 if (bh)
1218 brelse(bh);
1219
1220 mlog_exit(status);
1221 return status;
1222}
1223
Mark Fashehe48edee2007-03-07 16:46:57 -08001224/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001225 * Grow a b-tree so that it has more records.
1226 *
1227 * We might shift the tree depth in which case existing paths should
1228 * be considered invalid.
1229 *
1230 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001231 *
1232 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001233 */
1234static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001235 struct ocfs2_extent_tree *et, int *final_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07001236 struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001237 struct ocfs2_alloc_context *meta_ac)
1238{
1239 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001240 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001241 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001242 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1243 struct buffer_head *bh = NULL;
1244
1245 BUG_ON(meta_ac == NULL);
1246
Tao Mae7d4cb62008-08-18 17:38:44 +08001247 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001248 if (shift < 0) {
1249 ret = shift;
1250 mlog_errno(ret);
1251 goto out;
1252 }
1253
1254 /* We traveled all the way to the bottom of the allocation tree
1255 * and didn't find room for any more extents - we need to add
1256 * another tree level */
1257 if (shift) {
1258 BUG_ON(bh);
1259 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1260
1261 /* ocfs2_shift_tree_depth will return us a buffer with
1262 * the new extent block (so we can pass that to
1263 * ocfs2_add_branch). */
Tao Mae7d4cb62008-08-18 17:38:44 +08001264 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001265 meta_ac, &bh);
1266 if (ret < 0) {
1267 mlog_errno(ret);
1268 goto out;
1269 }
1270 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001271 if (depth == 1) {
1272 /*
1273 * Special case: we have room now if we shifted from
1274 * tree_depth 0, so no more work needs to be done.
1275 *
1276 * We won't be calling add_branch, so pass
1277 * back *last_eb_bh as the new leaf. At depth
1278 * zero, it should always be null so there's
1279 * no reason to brelse.
1280 */
1281 BUG_ON(*last_eb_bh);
1282 get_bh(bh);
1283 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001284 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001285 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001286 }
1287
1288 /* call ocfs2_add_branch to add the final part of the tree with
1289 * the new data. */
1290 mlog(0, "add branch. bh = %p\n", bh);
Tao Mae7d4cb62008-08-18 17:38:44 +08001291 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001292 meta_ac);
1293 if (ret < 0) {
1294 mlog_errno(ret);
1295 goto out;
1296 }
1297
1298out:
1299 if (final_depth)
1300 *final_depth = depth;
1301 brelse(bh);
1302 return ret;
1303}
1304
1305/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001306 * This function will discard the rightmost extent record.
1307 */
1308static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1309{
1310 int next_free = le16_to_cpu(el->l_next_free_rec);
1311 int count = le16_to_cpu(el->l_count);
1312 unsigned int num_bytes;
1313
1314 BUG_ON(!next_free);
1315 /* This will cause us to go off the end of our extent list. */
1316 BUG_ON(next_free >= count);
1317
1318 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1319
1320 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1321}
1322
1323static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1324 struct ocfs2_extent_rec *insert_rec)
1325{
1326 int i, insert_index, next_free, has_empty, num_bytes;
1327 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1328 struct ocfs2_extent_rec *rec;
1329
1330 next_free = le16_to_cpu(el->l_next_free_rec);
1331 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1332
1333 BUG_ON(!next_free);
1334
1335 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001336 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001337
1338 /*
1339 * The easiest way to approach this is to just remove the
1340 * empty extent and temporarily decrement next_free.
1341 */
1342 if (has_empty) {
1343 /*
1344 * If next_free was 1 (only an empty extent), this
1345 * loop won't execute, which is fine. We still want
1346 * the decrement above to happen.
1347 */
1348 for(i = 0; i < (next_free - 1); i++)
1349 el->l_recs[i] = el->l_recs[i+1];
1350
1351 next_free--;
1352 }
1353
1354 /*
1355 * Figure out what the new record index should be.
1356 */
1357 for(i = 0; i < next_free; i++) {
1358 rec = &el->l_recs[i];
1359
1360 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1361 break;
1362 }
1363 insert_index = i;
1364
1365 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1366 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1367
1368 BUG_ON(insert_index < 0);
1369 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1370 BUG_ON(insert_index > next_free);
1371
1372 /*
1373 * No need to memmove if we're just adding to the tail.
1374 */
1375 if (insert_index != next_free) {
1376 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1377
1378 num_bytes = next_free - insert_index;
1379 num_bytes *= sizeof(struct ocfs2_extent_rec);
1380 memmove(&el->l_recs[insert_index + 1],
1381 &el->l_recs[insert_index],
1382 num_bytes);
1383 }
1384
1385 /*
1386 * Either we had an empty extent, and need to re-increment or
1387 * there was no empty extent on a non full rightmost leaf node,
1388 * in which case we still need to increment.
1389 */
1390 next_free++;
1391 el->l_next_free_rec = cpu_to_le16(next_free);
1392 /*
1393 * Make sure none of the math above just messed up our tree.
1394 */
1395 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1396
1397 el->l_recs[insert_index] = *insert_rec;
1398
1399}
1400
Mark Fasheh328d5752007-06-18 10:48:04 -07001401static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1402{
1403 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1404
1405 BUG_ON(num_recs == 0);
1406
1407 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1408 num_recs--;
1409 size = num_recs * sizeof(struct ocfs2_extent_rec);
1410 memmove(&el->l_recs[0], &el->l_recs[1], size);
1411 memset(&el->l_recs[num_recs], 0,
1412 sizeof(struct ocfs2_extent_rec));
1413 el->l_next_free_rec = cpu_to_le16(num_recs);
1414 }
1415}
1416
Mark Fashehdcd05382007-01-16 11:32:23 -08001417/*
1418 * Create an empty extent record .
1419 *
1420 * l_next_free_rec may be updated.
1421 *
1422 * If an empty extent already exists do nothing.
1423 */
1424static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1425{
1426 int next_free = le16_to_cpu(el->l_next_free_rec);
1427
Mark Fashehe48edee2007-03-07 16:46:57 -08001428 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1429
Mark Fashehdcd05382007-01-16 11:32:23 -08001430 if (next_free == 0)
1431 goto set_and_inc;
1432
1433 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1434 return;
1435
1436 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1437 "Asked to create an empty extent in a full list:\n"
1438 "count = %u, tree depth = %u",
1439 le16_to_cpu(el->l_count),
1440 le16_to_cpu(el->l_tree_depth));
1441
1442 ocfs2_shift_records_right(el);
1443
1444set_and_inc:
1445 le16_add_cpu(&el->l_next_free_rec, 1);
1446 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1447}
1448
1449/*
1450 * For a rotation which involves two leaf nodes, the "root node" is
1451 * the lowest level tree node which contains a path to both leafs. This
1452 * resulting set of information can be used to form a complete "subtree"
1453 *
1454 * This function is passed two full paths from the dinode down to a
1455 * pair of adjacent leaves. It's task is to figure out which path
1456 * index contains the subtree root - this can be the root index itself
1457 * in a worst-case rotation.
1458 *
1459 * The array index of the subtree root is passed back.
1460 */
1461static int ocfs2_find_subtree_root(struct inode *inode,
1462 struct ocfs2_path *left,
1463 struct ocfs2_path *right)
1464{
1465 int i = 0;
1466
1467 /*
1468 * Check that the caller passed in two paths from the same tree.
1469 */
1470 BUG_ON(path_root_bh(left) != path_root_bh(right));
1471
1472 do {
1473 i++;
1474
1475 /*
1476 * The caller didn't pass two adjacent paths.
1477 */
1478 mlog_bug_on_msg(i > left->p_tree_depth,
1479 "Inode %lu, left depth %u, right depth %u\n"
1480 "left leaf blk %llu, right leaf blk %llu\n",
1481 inode->i_ino, left->p_tree_depth,
1482 right->p_tree_depth,
1483 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1484 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1485 } while (left->p_node[i].bh->b_blocknr ==
1486 right->p_node[i].bh->b_blocknr);
1487
1488 return i - 1;
1489}
1490
1491typedef void (path_insert_t)(void *, struct buffer_head *);
1492
1493/*
1494 * Traverse a btree path in search of cpos, starting at root_el.
1495 *
1496 * This code can be called with a cpos larger than the tree, in which
1497 * case it will return the rightmost path.
1498 */
1499static int __ocfs2_find_path(struct inode *inode,
1500 struct ocfs2_extent_list *root_el, u32 cpos,
1501 path_insert_t *func, void *data)
1502{
1503 int i, ret = 0;
1504 u32 range;
1505 u64 blkno;
1506 struct buffer_head *bh = NULL;
1507 struct ocfs2_extent_block *eb;
1508 struct ocfs2_extent_list *el;
1509 struct ocfs2_extent_rec *rec;
1510 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1511
1512 el = root_el;
1513 while (el->l_tree_depth) {
1514 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1515 ocfs2_error(inode->i_sb,
1516 "Inode %llu has empty extent list at "
1517 "depth %u\n",
1518 (unsigned long long)oi->ip_blkno,
1519 le16_to_cpu(el->l_tree_depth));
1520 ret = -EROFS;
1521 goto out;
1522
1523 }
1524
1525 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1526 rec = &el->l_recs[i];
1527
1528 /*
1529 * In the case that cpos is off the allocation
1530 * tree, this should just wind up returning the
1531 * rightmost record.
1532 */
1533 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001534 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001535 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1536 break;
1537 }
1538
1539 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1540 if (blkno == 0) {
1541 ocfs2_error(inode->i_sb,
1542 "Inode %llu has bad blkno in extent list "
1543 "at depth %u (index %d)\n",
1544 (unsigned long long)oi->ip_blkno,
1545 le16_to_cpu(el->l_tree_depth), i);
1546 ret = -EROFS;
1547 goto out;
1548 }
1549
1550 brelse(bh);
1551 bh = NULL;
1552 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1553 &bh, OCFS2_BH_CACHED, inode);
1554 if (ret) {
1555 mlog_errno(ret);
1556 goto out;
1557 }
1558
1559 eb = (struct ocfs2_extent_block *) bh->b_data;
1560 el = &eb->h_list;
1561 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1562 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1563 ret = -EIO;
1564 goto out;
1565 }
1566
1567 if (le16_to_cpu(el->l_next_free_rec) >
1568 le16_to_cpu(el->l_count)) {
1569 ocfs2_error(inode->i_sb,
1570 "Inode %llu has bad count in extent list "
1571 "at block %llu (next free=%u, count=%u)\n",
1572 (unsigned long long)oi->ip_blkno,
1573 (unsigned long long)bh->b_blocknr,
1574 le16_to_cpu(el->l_next_free_rec),
1575 le16_to_cpu(el->l_count));
1576 ret = -EROFS;
1577 goto out;
1578 }
1579
1580 if (func)
1581 func(data, bh);
1582 }
1583
1584out:
1585 /*
1586 * Catch any trailing bh that the loop didn't handle.
1587 */
1588 brelse(bh);
1589
1590 return ret;
1591}
1592
1593/*
1594 * Given an initialized path (that is, it has a valid root extent
1595 * list), this function will traverse the btree in search of the path
1596 * which would contain cpos.
1597 *
1598 * The path traveled is recorded in the path structure.
1599 *
1600 * Note that this will not do any comparisons on leaf node extent
1601 * records, so it will work fine in the case that we just added a tree
1602 * branch.
1603 */
1604struct find_path_data {
1605 int index;
1606 struct ocfs2_path *path;
1607};
1608static void find_path_ins(void *data, struct buffer_head *bh)
1609{
1610 struct find_path_data *fp = data;
1611
1612 get_bh(bh);
1613 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1614 fp->index++;
1615}
1616static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1617 u32 cpos)
1618{
1619 struct find_path_data data;
1620
1621 data.index = 1;
1622 data.path = path;
1623 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1624 find_path_ins, &data);
1625}
1626
1627static void find_leaf_ins(void *data, struct buffer_head *bh)
1628{
1629 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1630 struct ocfs2_extent_list *el = &eb->h_list;
1631 struct buffer_head **ret = data;
1632
1633 /* We want to retain only the leaf block. */
1634 if (le16_to_cpu(el->l_tree_depth) == 0) {
1635 get_bh(bh);
1636 *ret = bh;
1637 }
1638}
1639/*
1640 * Find the leaf block in the tree which would contain cpos. No
1641 * checking of the actual leaf is done.
1642 *
1643 * Some paths want to call this instead of allocating a path structure
1644 * and calling ocfs2_find_path().
1645 *
1646 * This function doesn't handle non btree extent lists.
1647 */
Mark Fasheh363041a2007-01-17 12:31:35 -08001648int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1649 u32 cpos, struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001650{
1651 int ret;
1652 struct buffer_head *bh = NULL;
1653
1654 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1655 if (ret) {
1656 mlog_errno(ret);
1657 goto out;
1658 }
1659
1660 *leaf_bh = bh;
1661out:
1662 return ret;
1663}
1664
1665/*
1666 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1667 *
1668 * Basically, we've moved stuff around at the bottom of the tree and
1669 * we need to fix up the extent records above the changes to reflect
1670 * the new changes.
1671 *
1672 * left_rec: the record on the left.
1673 * left_child_el: is the child list pointed to by left_rec
1674 * right_rec: the record to the right of left_rec
1675 * right_child_el: is the child list pointed to by right_rec
1676 *
1677 * By definition, this only works on interior nodes.
1678 */
1679static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1680 struct ocfs2_extent_list *left_child_el,
1681 struct ocfs2_extent_rec *right_rec,
1682 struct ocfs2_extent_list *right_child_el)
1683{
1684 u32 left_clusters, right_end;
1685
1686 /*
1687 * Interior nodes never have holes. Their cpos is the cpos of
1688 * the leftmost record in their child list. Their cluster
1689 * count covers the full theoretical range of their child list
1690 * - the range between their cpos and the cpos of the record
1691 * immediately to their right.
1692 */
1693 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07001694 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1695 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1696 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1697 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001698 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001699 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001700
1701 /*
1702 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001703 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001704 * updating e_cpos to keep the same highest cluster count.
1705 */
1706 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001707 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001708
1709 right_rec->e_cpos = left_rec->e_cpos;
1710 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1711
1712 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001713 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001714}
1715
1716/*
1717 * Adjust the adjacent root node records involved in a
1718 * rotation. left_el_blkno is passed in as a key so that we can easily
1719 * find it's index in the root list.
1720 */
1721static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1722 struct ocfs2_extent_list *left_el,
1723 struct ocfs2_extent_list *right_el,
1724 u64 left_el_blkno)
1725{
1726 int i;
1727
1728 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1729 le16_to_cpu(left_el->l_tree_depth));
1730
1731 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1732 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1733 break;
1734 }
1735
1736 /*
1737 * The path walking code should have never returned a root and
1738 * two paths which are not adjacent.
1739 */
1740 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1741
1742 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1743 &root_el->l_recs[i + 1], right_el);
1744}
1745
1746/*
1747 * We've changed a leaf block (in right_path) and need to reflect that
1748 * change back up the subtree.
1749 *
1750 * This happens in multiple places:
1751 * - When we've moved an extent record from the left path leaf to the right
1752 * path leaf to make room for an empty extent in the left path leaf.
1753 * - When our insert into the right path leaf is at the leftmost edge
1754 * and requires an update of the path immediately to it's left. This
1755 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08001756 * - When we've adjusted the last extent record in the left path leaf and the
1757 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08001758 */
1759static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1760 struct ocfs2_path *left_path,
1761 struct ocfs2_path *right_path,
1762 int subtree_index)
1763{
1764 int ret, i, idx;
1765 struct ocfs2_extent_list *el, *left_el, *right_el;
1766 struct ocfs2_extent_rec *left_rec, *right_rec;
1767 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1768
1769 /*
1770 * Update the counts and position values within all the
1771 * interior nodes to reflect the leaf rotation we just did.
1772 *
1773 * The root node is handled below the loop.
1774 *
1775 * We begin the loop with right_el and left_el pointing to the
1776 * leaf lists and work our way up.
1777 *
1778 * NOTE: within this loop, left_el and right_el always refer
1779 * to the *child* lists.
1780 */
1781 left_el = path_leaf_el(left_path);
1782 right_el = path_leaf_el(right_path);
1783 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1784 mlog(0, "Adjust records at index %u\n", i);
1785
1786 /*
1787 * One nice property of knowing that all of these
1788 * nodes are below the root is that we only deal with
1789 * the leftmost right node record and the rightmost
1790 * left node record.
1791 */
1792 el = left_path->p_node[i].el;
1793 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1794 left_rec = &el->l_recs[idx];
1795
1796 el = right_path->p_node[i].el;
1797 right_rec = &el->l_recs[0];
1798
1799 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1800 right_el);
1801
1802 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1803 if (ret)
1804 mlog_errno(ret);
1805
1806 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1807 if (ret)
1808 mlog_errno(ret);
1809
1810 /*
1811 * Setup our list pointers now so that the current
1812 * parents become children in the next iteration.
1813 */
1814 left_el = left_path->p_node[i].el;
1815 right_el = right_path->p_node[i].el;
1816 }
1817
1818 /*
1819 * At the root node, adjust the two adjacent records which
1820 * begin our path to the leaves.
1821 */
1822
1823 el = left_path->p_node[subtree_index].el;
1824 left_el = left_path->p_node[subtree_index + 1].el;
1825 right_el = right_path->p_node[subtree_index + 1].el;
1826
1827 ocfs2_adjust_root_records(el, left_el, right_el,
1828 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1829
1830 root_bh = left_path->p_node[subtree_index].bh;
1831
1832 ret = ocfs2_journal_dirty(handle, root_bh);
1833 if (ret)
1834 mlog_errno(ret);
1835}
1836
1837static int ocfs2_rotate_subtree_right(struct inode *inode,
1838 handle_t *handle,
1839 struct ocfs2_path *left_path,
1840 struct ocfs2_path *right_path,
1841 int subtree_index)
1842{
1843 int ret, i;
1844 struct buffer_head *right_leaf_bh;
1845 struct buffer_head *left_leaf_bh = NULL;
1846 struct buffer_head *root_bh;
1847 struct ocfs2_extent_list *right_el, *left_el;
1848 struct ocfs2_extent_rec move_rec;
1849
1850 left_leaf_bh = path_leaf_bh(left_path);
1851 left_el = path_leaf_el(left_path);
1852
1853 if (left_el->l_next_free_rec != left_el->l_count) {
1854 ocfs2_error(inode->i_sb,
1855 "Inode %llu has non-full interior leaf node %llu"
1856 "(next free = %u)",
1857 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1858 (unsigned long long)left_leaf_bh->b_blocknr,
1859 le16_to_cpu(left_el->l_next_free_rec));
1860 return -EROFS;
1861 }
1862
1863 /*
1864 * This extent block may already have an empty record, so we
1865 * return early if so.
1866 */
1867 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1868 return 0;
1869
1870 root_bh = left_path->p_node[subtree_index].bh;
1871 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1872
1873 ret = ocfs2_journal_access(handle, inode, root_bh,
1874 OCFS2_JOURNAL_ACCESS_WRITE);
1875 if (ret) {
1876 mlog_errno(ret);
1877 goto out;
1878 }
1879
1880 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1881 ret = ocfs2_journal_access(handle, inode,
1882 right_path->p_node[i].bh,
1883 OCFS2_JOURNAL_ACCESS_WRITE);
1884 if (ret) {
1885 mlog_errno(ret);
1886 goto out;
1887 }
1888
1889 ret = ocfs2_journal_access(handle, inode,
1890 left_path->p_node[i].bh,
1891 OCFS2_JOURNAL_ACCESS_WRITE);
1892 if (ret) {
1893 mlog_errno(ret);
1894 goto out;
1895 }
1896 }
1897
1898 right_leaf_bh = path_leaf_bh(right_path);
1899 right_el = path_leaf_el(right_path);
1900
1901 /* This is a code error, not a disk corruption. */
1902 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1903 "because rightmost leaf block %llu is empty\n",
1904 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1905 (unsigned long long)right_leaf_bh->b_blocknr);
1906
1907 ocfs2_create_empty_extent(right_el);
1908
1909 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1910 if (ret) {
1911 mlog_errno(ret);
1912 goto out;
1913 }
1914
1915 /* Do the copy now. */
1916 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1917 move_rec = left_el->l_recs[i];
1918 right_el->l_recs[0] = move_rec;
1919
1920 /*
1921 * Clear out the record we just copied and shift everything
1922 * over, leaving an empty extent in the left leaf.
1923 *
1924 * We temporarily subtract from next_free_rec so that the
1925 * shift will lose the tail record (which is now defunct).
1926 */
1927 le16_add_cpu(&left_el->l_next_free_rec, -1);
1928 ocfs2_shift_records_right(left_el);
1929 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1930 le16_add_cpu(&left_el->l_next_free_rec, 1);
1931
1932 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1933 if (ret) {
1934 mlog_errno(ret);
1935 goto out;
1936 }
1937
1938 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1939 subtree_index);
1940
1941out:
1942 return ret;
1943}
1944
1945/*
1946 * Given a full path, determine what cpos value would return us a path
1947 * containing the leaf immediately to the left of the current one.
1948 *
1949 * Will return zero if the path passed in is already the leftmost path.
1950 */
1951static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1952 struct ocfs2_path *path, u32 *cpos)
1953{
1954 int i, j, ret = 0;
1955 u64 blkno;
1956 struct ocfs2_extent_list *el;
1957
Mark Fashehe48edee2007-03-07 16:46:57 -08001958 BUG_ON(path->p_tree_depth == 0);
1959
Mark Fashehdcd05382007-01-16 11:32:23 -08001960 *cpos = 0;
1961
1962 blkno = path_leaf_bh(path)->b_blocknr;
1963
1964 /* Start at the tree node just above the leaf and work our way up. */
1965 i = path->p_tree_depth - 1;
1966 while (i >= 0) {
1967 el = path->p_node[i].el;
1968
1969 /*
1970 * Find the extent record just before the one in our
1971 * path.
1972 */
1973 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1974 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1975 if (j == 0) {
1976 if (i == 0) {
1977 /*
1978 * We've determined that the
1979 * path specified is already
1980 * the leftmost one - return a
1981 * cpos of zero.
1982 */
1983 goto out;
1984 }
1985 /*
1986 * The leftmost record points to our
1987 * leaf - we need to travel up the
1988 * tree one level.
1989 */
1990 goto next_node;
1991 }
1992
1993 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001994 *cpos = *cpos + ocfs2_rec_clusters(el,
1995 &el->l_recs[j - 1]);
1996 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08001997 goto out;
1998 }
1999 }
2000
2001 /*
2002 * If we got here, we never found a valid node where
2003 * the tree indicated one should be.
2004 */
2005 ocfs2_error(sb,
2006 "Invalid extent tree at extent block %llu\n",
2007 (unsigned long long)blkno);
2008 ret = -EROFS;
2009 goto out;
2010
2011next_node:
2012 blkno = path->p_node[i].bh->b_blocknr;
2013 i--;
2014 }
2015
2016out:
2017 return ret;
2018}
2019
Mark Fasheh328d5752007-06-18 10:48:04 -07002020/*
2021 * Extend the transaction by enough credits to complete the rotation,
2022 * and still leave at least the original number of credits allocated
2023 * to this transaction.
2024 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002025static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002026 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002027 struct ocfs2_path *path)
2028{
Mark Fasheh328d5752007-06-18 10:48:04 -07002029 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002030
2031 if (handle->h_buffer_credits < credits)
2032 return ocfs2_extend_trans(handle, credits);
2033
2034 return 0;
2035}
2036
2037/*
2038 * Trap the case where we're inserting into the theoretical range past
2039 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2040 * whose cpos is less than ours into the right leaf.
2041 *
2042 * It's only necessary to look at the rightmost record of the left
2043 * leaf because the logic that calls us should ensure that the
2044 * theoretical ranges in the path components above the leaves are
2045 * correct.
2046 */
2047static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2048 u32 insert_cpos)
2049{
2050 struct ocfs2_extent_list *left_el;
2051 struct ocfs2_extent_rec *rec;
2052 int next_free;
2053
2054 left_el = path_leaf_el(left_path);
2055 next_free = le16_to_cpu(left_el->l_next_free_rec);
2056 rec = &left_el->l_recs[next_free - 1];
2057
2058 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2059 return 1;
2060 return 0;
2061}
2062
Mark Fasheh328d5752007-06-18 10:48:04 -07002063static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2064{
2065 int next_free = le16_to_cpu(el->l_next_free_rec);
2066 unsigned int range;
2067 struct ocfs2_extent_rec *rec;
2068
2069 if (next_free == 0)
2070 return 0;
2071
2072 rec = &el->l_recs[0];
2073 if (ocfs2_is_empty_extent(rec)) {
2074 /* Empty list. */
2075 if (next_free == 1)
2076 return 0;
2077 rec = &el->l_recs[1];
2078 }
2079
2080 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2081 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2082 return 1;
2083 return 0;
2084}
2085
Mark Fashehdcd05382007-01-16 11:32:23 -08002086/*
2087 * Rotate all the records in a btree right one record, starting at insert_cpos.
2088 *
2089 * The path to the rightmost leaf should be passed in.
2090 *
2091 * The array is assumed to be large enough to hold an entire path (tree depth).
2092 *
2093 * Upon succesful return from this function:
2094 *
2095 * - The 'right_path' array will contain a path to the leaf block
2096 * whose range contains e_cpos.
2097 * - That leaf block will have a single empty extent in list index 0.
2098 * - In the case that the rotation requires a post-insert update,
2099 * *ret_left_path will contain a valid path which can be passed to
2100 * ocfs2_insert_path().
2101 */
2102static int ocfs2_rotate_tree_right(struct inode *inode,
2103 handle_t *handle,
Mark Fasheh328d5752007-06-18 10:48:04 -07002104 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002105 u32 insert_cpos,
2106 struct ocfs2_path *right_path,
2107 struct ocfs2_path **ret_left_path)
2108{
Mark Fasheh328d5752007-06-18 10:48:04 -07002109 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002110 u32 cpos;
2111 struct ocfs2_path *left_path = NULL;
2112
2113 *ret_left_path = NULL;
2114
2115 left_path = ocfs2_new_path(path_root_bh(right_path),
2116 path_root_el(right_path));
2117 if (!left_path) {
2118 ret = -ENOMEM;
2119 mlog_errno(ret);
2120 goto out;
2121 }
2122
2123 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2124 if (ret) {
2125 mlog_errno(ret);
2126 goto out;
2127 }
2128
2129 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2130
2131 /*
2132 * What we want to do here is:
2133 *
2134 * 1) Start with the rightmost path.
2135 *
2136 * 2) Determine a path to the leaf block directly to the left
2137 * of that leaf.
2138 *
2139 * 3) Determine the 'subtree root' - the lowest level tree node
2140 * which contains a path to both leaves.
2141 *
2142 * 4) Rotate the subtree.
2143 *
2144 * 5) Find the next subtree by considering the left path to be
2145 * the new right path.
2146 *
2147 * The check at the top of this while loop also accepts
2148 * insert_cpos == cpos because cpos is only a _theoretical_
2149 * value to get us the left path - insert_cpos might very well
2150 * be filling that hole.
2151 *
2152 * Stop at a cpos of '0' because we either started at the
2153 * leftmost branch (i.e., a tree with one branch and a
2154 * rotation inside of it), or we've gone as far as we can in
2155 * rotating subtrees.
2156 */
2157 while (cpos && insert_cpos <= cpos) {
2158 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2159 insert_cpos, cpos);
2160
2161 ret = ocfs2_find_path(inode, left_path, cpos);
2162 if (ret) {
2163 mlog_errno(ret);
2164 goto out;
2165 }
2166
2167 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2168 path_leaf_bh(right_path),
2169 "Inode %lu: error during insert of %u "
2170 "(left path cpos %u) results in two identical "
2171 "paths ending at %llu\n",
2172 inode->i_ino, insert_cpos, cpos,
2173 (unsigned long long)
2174 path_leaf_bh(left_path)->b_blocknr);
2175
Mark Fasheh328d5752007-06-18 10:48:04 -07002176 if (split == SPLIT_NONE &&
2177 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002178 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002179
2180 /*
2181 * We've rotated the tree as much as we
2182 * should. The rest is up to
2183 * ocfs2_insert_path() to complete, after the
2184 * record insertion. We indicate this
2185 * situation by returning the left path.
2186 *
2187 * The reason we don't adjust the records here
2188 * before the record insert is that an error
2189 * later might break the rule where a parent
2190 * record e_cpos will reflect the actual
2191 * e_cpos of the 1st nonempty record of the
2192 * child list.
2193 */
2194 *ret_left_path = left_path;
2195 goto out_ret_path;
2196 }
2197
2198 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2199
2200 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2201 start,
2202 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2203 right_path->p_tree_depth);
2204
2205 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002206 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002207 if (ret) {
2208 mlog_errno(ret);
2209 goto out;
2210 }
2211
2212 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2213 right_path, start);
2214 if (ret) {
2215 mlog_errno(ret);
2216 goto out;
2217 }
2218
Mark Fasheh328d5752007-06-18 10:48:04 -07002219 if (split != SPLIT_NONE &&
2220 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2221 insert_cpos)) {
2222 /*
2223 * A rotate moves the rightmost left leaf
2224 * record over to the leftmost right leaf
2225 * slot. If we're doing an extent split
2226 * instead of a real insert, then we have to
2227 * check that the extent to be split wasn't
2228 * just moved over. If it was, then we can
2229 * exit here, passing left_path back -
2230 * ocfs2_split_extent() is smart enough to
2231 * search both leaves.
2232 */
2233 *ret_left_path = left_path;
2234 goto out_ret_path;
2235 }
2236
Mark Fashehdcd05382007-01-16 11:32:23 -08002237 /*
2238 * There is no need to re-read the next right path
2239 * as we know that it'll be our current left
2240 * path. Optimize by copying values instead.
2241 */
2242 ocfs2_mv_path(right_path, left_path);
2243
2244 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2245 &cpos);
2246 if (ret) {
2247 mlog_errno(ret);
2248 goto out;
2249 }
2250 }
2251
2252out:
2253 ocfs2_free_path(left_path);
2254
2255out_ret_path:
2256 return ret;
2257}
2258
Mark Fasheh328d5752007-06-18 10:48:04 -07002259static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2260 struct ocfs2_path *path)
2261{
2262 int i, idx;
2263 struct ocfs2_extent_rec *rec;
2264 struct ocfs2_extent_list *el;
2265 struct ocfs2_extent_block *eb;
2266 u32 range;
2267
2268 /* Path should always be rightmost. */
2269 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2270 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2271
2272 el = &eb->h_list;
2273 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2274 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2275 rec = &el->l_recs[idx];
2276 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2277
2278 for (i = 0; i < path->p_tree_depth; i++) {
2279 el = path->p_node[i].el;
2280 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2281 rec = &el->l_recs[idx];
2282
2283 rec->e_int_clusters = cpu_to_le32(range);
2284 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2285
2286 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2287 }
2288}
2289
2290static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2291 struct ocfs2_cached_dealloc_ctxt *dealloc,
2292 struct ocfs2_path *path, int unlink_start)
2293{
2294 int ret, i;
2295 struct ocfs2_extent_block *eb;
2296 struct ocfs2_extent_list *el;
2297 struct buffer_head *bh;
2298
2299 for(i = unlink_start; i < path_num_items(path); i++) {
2300 bh = path->p_node[i].bh;
2301
2302 eb = (struct ocfs2_extent_block *)bh->b_data;
2303 /*
2304 * Not all nodes might have had their final count
2305 * decremented by the caller - handle this here.
2306 */
2307 el = &eb->h_list;
2308 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2309 mlog(ML_ERROR,
2310 "Inode %llu, attempted to remove extent block "
2311 "%llu with %u records\n",
2312 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2313 (unsigned long long)le64_to_cpu(eb->h_blkno),
2314 le16_to_cpu(el->l_next_free_rec));
2315
2316 ocfs2_journal_dirty(handle, bh);
2317 ocfs2_remove_from_cache(inode, bh);
2318 continue;
2319 }
2320
2321 el->l_next_free_rec = 0;
2322 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2323
2324 ocfs2_journal_dirty(handle, bh);
2325
2326 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2327 if (ret)
2328 mlog_errno(ret);
2329
2330 ocfs2_remove_from_cache(inode, bh);
2331 }
2332}
2333
2334static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2335 struct ocfs2_path *left_path,
2336 struct ocfs2_path *right_path,
2337 int subtree_index,
2338 struct ocfs2_cached_dealloc_ctxt *dealloc)
2339{
2340 int i;
2341 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2342 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2343 struct ocfs2_extent_list *el;
2344 struct ocfs2_extent_block *eb;
2345
2346 el = path_leaf_el(left_path);
2347
2348 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2349
2350 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2351 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2352 break;
2353
2354 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2355
2356 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2357 le16_add_cpu(&root_el->l_next_free_rec, -1);
2358
2359 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2360 eb->h_next_leaf_blk = 0;
2361
2362 ocfs2_journal_dirty(handle, root_bh);
2363 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2364
2365 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2366 subtree_index + 1);
2367}
2368
2369static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2370 struct ocfs2_path *left_path,
2371 struct ocfs2_path *right_path,
2372 int subtree_index,
2373 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002374 int *deleted,
2375 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002376{
2377 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002378 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002379 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2380 struct ocfs2_extent_block *eb;
2381
2382 *deleted = 0;
2383
2384 right_leaf_el = path_leaf_el(right_path);
2385 left_leaf_el = path_leaf_el(left_path);
2386 root_bh = left_path->p_node[subtree_index].bh;
2387 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2388
2389 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2390 return 0;
2391
2392 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2393 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2394 /*
2395 * It's legal for us to proceed if the right leaf is
2396 * the rightmost one and it has an empty extent. There
2397 * are two cases to handle - whether the leaf will be
2398 * empty after removal or not. If the leaf isn't empty
2399 * then just remove the empty extent up front. The
2400 * next block will handle empty leaves by flagging
2401 * them for unlink.
2402 *
2403 * Non rightmost leaves will throw -EAGAIN and the
2404 * caller can manually move the subtree and retry.
2405 */
2406
2407 if (eb->h_next_leaf_blk != 0ULL)
2408 return -EAGAIN;
2409
2410 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2411 ret = ocfs2_journal_access(handle, inode,
2412 path_leaf_bh(right_path),
2413 OCFS2_JOURNAL_ACCESS_WRITE);
2414 if (ret) {
2415 mlog_errno(ret);
2416 goto out;
2417 }
2418
2419 ocfs2_remove_empty_extent(right_leaf_el);
2420 } else
2421 right_has_empty = 1;
2422 }
2423
2424 if (eb->h_next_leaf_blk == 0ULL &&
2425 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2426 /*
2427 * We have to update i_last_eb_blk during the meta
2428 * data delete.
2429 */
Tao Mae7d4cb62008-08-18 17:38:44 +08002430 ret = ocfs2_journal_access(handle, inode, et_root_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07002431 OCFS2_JOURNAL_ACCESS_WRITE);
2432 if (ret) {
2433 mlog_errno(ret);
2434 goto out;
2435 }
2436
2437 del_right_subtree = 1;
2438 }
2439
2440 /*
2441 * Getting here with an empty extent in the right path implies
2442 * that it's the rightmost path and will be deleted.
2443 */
2444 BUG_ON(right_has_empty && !del_right_subtree);
2445
2446 ret = ocfs2_journal_access(handle, inode, root_bh,
2447 OCFS2_JOURNAL_ACCESS_WRITE);
2448 if (ret) {
2449 mlog_errno(ret);
2450 goto out;
2451 }
2452
2453 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2454 ret = ocfs2_journal_access(handle, inode,
2455 right_path->p_node[i].bh,
2456 OCFS2_JOURNAL_ACCESS_WRITE);
2457 if (ret) {
2458 mlog_errno(ret);
2459 goto out;
2460 }
2461
2462 ret = ocfs2_journal_access(handle, inode,
2463 left_path->p_node[i].bh,
2464 OCFS2_JOURNAL_ACCESS_WRITE);
2465 if (ret) {
2466 mlog_errno(ret);
2467 goto out;
2468 }
2469 }
2470
2471 if (!right_has_empty) {
2472 /*
2473 * Only do this if we're moving a real
2474 * record. Otherwise, the action is delayed until
2475 * after removal of the right path in which case we
2476 * can do a simple shift to remove the empty extent.
2477 */
2478 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2479 memset(&right_leaf_el->l_recs[0], 0,
2480 sizeof(struct ocfs2_extent_rec));
2481 }
2482 if (eb->h_next_leaf_blk == 0ULL) {
2483 /*
2484 * Move recs over to get rid of empty extent, decrease
2485 * next_free. This is allowed to remove the last
2486 * extent in our leaf (setting l_next_free_rec to
2487 * zero) - the delete code below won't care.
2488 */
2489 ocfs2_remove_empty_extent(right_leaf_el);
2490 }
2491
2492 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2493 if (ret)
2494 mlog_errno(ret);
2495 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2496 if (ret)
2497 mlog_errno(ret);
2498
2499 if (del_right_subtree) {
2500 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2501 subtree_index, dealloc);
2502 ocfs2_update_edge_lengths(inode, handle, left_path);
2503
2504 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002505 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002506
2507 /*
2508 * Removal of the extent in the left leaf was skipped
2509 * above so we could delete the right path
2510 * 1st.
2511 */
2512 if (right_has_empty)
2513 ocfs2_remove_empty_extent(left_leaf_el);
2514
Tao Mae7d4cb62008-08-18 17:38:44 +08002515 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002516 if (ret)
2517 mlog_errno(ret);
2518
2519 *deleted = 1;
2520 } else
2521 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2522 subtree_index);
2523
2524out:
2525 return ret;
2526}
2527
2528/*
2529 * Given a full path, determine what cpos value would return us a path
2530 * containing the leaf immediately to the right of the current one.
2531 *
2532 * Will return zero if the path passed in is already the rightmost path.
2533 *
2534 * This looks similar, but is subtly different to
2535 * ocfs2_find_cpos_for_left_leaf().
2536 */
2537static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2538 struct ocfs2_path *path, u32 *cpos)
2539{
2540 int i, j, ret = 0;
2541 u64 blkno;
2542 struct ocfs2_extent_list *el;
2543
2544 *cpos = 0;
2545
2546 if (path->p_tree_depth == 0)
2547 return 0;
2548
2549 blkno = path_leaf_bh(path)->b_blocknr;
2550
2551 /* Start at the tree node just above the leaf and work our way up. */
2552 i = path->p_tree_depth - 1;
2553 while (i >= 0) {
2554 int next_free;
2555
2556 el = path->p_node[i].el;
2557
2558 /*
2559 * Find the extent record just after the one in our
2560 * path.
2561 */
2562 next_free = le16_to_cpu(el->l_next_free_rec);
2563 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2564 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2565 if (j == (next_free - 1)) {
2566 if (i == 0) {
2567 /*
2568 * We've determined that the
2569 * path specified is already
2570 * the rightmost one - return a
2571 * cpos of zero.
2572 */
2573 goto out;
2574 }
2575 /*
2576 * The rightmost record points to our
2577 * leaf - we need to travel up the
2578 * tree one level.
2579 */
2580 goto next_node;
2581 }
2582
2583 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2584 goto out;
2585 }
2586 }
2587
2588 /*
2589 * If we got here, we never found a valid node where
2590 * the tree indicated one should be.
2591 */
2592 ocfs2_error(sb,
2593 "Invalid extent tree at extent block %llu\n",
2594 (unsigned long long)blkno);
2595 ret = -EROFS;
2596 goto out;
2597
2598next_node:
2599 blkno = path->p_node[i].bh->b_blocknr;
2600 i--;
2601 }
2602
2603out:
2604 return ret;
2605}
2606
2607static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2608 handle_t *handle,
2609 struct buffer_head *bh,
2610 struct ocfs2_extent_list *el)
2611{
2612 int ret;
2613
2614 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2615 return 0;
2616
2617 ret = ocfs2_journal_access(handle, inode, bh,
2618 OCFS2_JOURNAL_ACCESS_WRITE);
2619 if (ret) {
2620 mlog_errno(ret);
2621 goto out;
2622 }
2623
2624 ocfs2_remove_empty_extent(el);
2625
2626 ret = ocfs2_journal_dirty(handle, bh);
2627 if (ret)
2628 mlog_errno(ret);
2629
2630out:
2631 return ret;
2632}
2633
2634static int __ocfs2_rotate_tree_left(struct inode *inode,
2635 handle_t *handle, int orig_credits,
2636 struct ocfs2_path *path,
2637 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002638 struct ocfs2_path **empty_extent_path,
2639 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002640{
2641 int ret, subtree_root, deleted;
2642 u32 right_cpos;
2643 struct ocfs2_path *left_path = NULL;
2644 struct ocfs2_path *right_path = NULL;
2645
2646 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2647
2648 *empty_extent_path = NULL;
2649
2650 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2651 &right_cpos);
2652 if (ret) {
2653 mlog_errno(ret);
2654 goto out;
2655 }
2656
2657 left_path = ocfs2_new_path(path_root_bh(path),
2658 path_root_el(path));
2659 if (!left_path) {
2660 ret = -ENOMEM;
2661 mlog_errno(ret);
2662 goto out;
2663 }
2664
2665 ocfs2_cp_path(left_path, path);
2666
2667 right_path = ocfs2_new_path(path_root_bh(path),
2668 path_root_el(path));
2669 if (!right_path) {
2670 ret = -ENOMEM;
2671 mlog_errno(ret);
2672 goto out;
2673 }
2674
2675 while (right_cpos) {
2676 ret = ocfs2_find_path(inode, right_path, right_cpos);
2677 if (ret) {
2678 mlog_errno(ret);
2679 goto out;
2680 }
2681
2682 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2683 right_path);
2684
2685 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2686 subtree_root,
2687 (unsigned long long)
2688 right_path->p_node[subtree_root].bh->b_blocknr,
2689 right_path->p_tree_depth);
2690
2691 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2692 orig_credits, left_path);
2693 if (ret) {
2694 mlog_errno(ret);
2695 goto out;
2696 }
2697
Mark Fashehe8aed342007-12-03 16:43:01 -08002698 /*
2699 * Caller might still want to make changes to the
2700 * tree root, so re-add it to the journal here.
2701 */
2702 ret = ocfs2_journal_access(handle, inode,
2703 path_root_bh(left_path),
2704 OCFS2_JOURNAL_ACCESS_WRITE);
2705 if (ret) {
2706 mlog_errno(ret);
2707 goto out;
2708 }
2709
Mark Fasheh328d5752007-06-18 10:48:04 -07002710 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2711 right_path, subtree_root,
Tao Mae7d4cb62008-08-18 17:38:44 +08002712 dealloc, &deleted, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002713 if (ret == -EAGAIN) {
2714 /*
2715 * The rotation has to temporarily stop due to
2716 * the right subtree having an empty
2717 * extent. Pass it back to the caller for a
2718 * fixup.
2719 */
2720 *empty_extent_path = right_path;
2721 right_path = NULL;
2722 goto out;
2723 }
2724 if (ret) {
2725 mlog_errno(ret);
2726 goto out;
2727 }
2728
2729 /*
2730 * The subtree rotate might have removed records on
2731 * the rightmost edge. If so, then rotation is
2732 * complete.
2733 */
2734 if (deleted)
2735 break;
2736
2737 ocfs2_mv_path(left_path, right_path);
2738
2739 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2740 &right_cpos);
2741 if (ret) {
2742 mlog_errno(ret);
2743 goto out;
2744 }
2745 }
2746
2747out:
2748 ocfs2_free_path(right_path);
2749 ocfs2_free_path(left_path);
2750
2751 return ret;
2752}
2753
2754static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08002755 struct ocfs2_path *path,
2756 struct ocfs2_cached_dealloc_ctxt *dealloc,
2757 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002758{
2759 int ret, subtree_index;
2760 u32 cpos;
2761 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07002762 struct ocfs2_extent_block *eb;
2763 struct ocfs2_extent_list *el;
2764
Mark Fasheh328d5752007-06-18 10:48:04 -07002765
Joel Becker35dc0aa2008-08-20 16:25:06 -07002766 ret = ocfs2_et_sanity_check(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002767 if (ret)
2768 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07002769 /*
2770 * There's two ways we handle this depending on
2771 * whether path is the only existing one.
2772 */
2773 ret = ocfs2_extend_rotate_transaction(handle, 0,
2774 handle->h_buffer_credits,
2775 path);
2776 if (ret) {
2777 mlog_errno(ret);
2778 goto out;
2779 }
2780
2781 ret = ocfs2_journal_access_path(inode, handle, path);
2782 if (ret) {
2783 mlog_errno(ret);
2784 goto out;
2785 }
2786
2787 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2788 if (ret) {
2789 mlog_errno(ret);
2790 goto out;
2791 }
2792
2793 if (cpos) {
2794 /*
2795 * We have a path to the left of this one - it needs
2796 * an update too.
2797 */
2798 left_path = ocfs2_new_path(path_root_bh(path),
2799 path_root_el(path));
2800 if (!left_path) {
2801 ret = -ENOMEM;
2802 mlog_errno(ret);
2803 goto out;
2804 }
2805
2806 ret = ocfs2_find_path(inode, left_path, cpos);
2807 if (ret) {
2808 mlog_errno(ret);
2809 goto out;
2810 }
2811
2812 ret = ocfs2_journal_access_path(inode, handle, left_path);
2813 if (ret) {
2814 mlog_errno(ret);
2815 goto out;
2816 }
2817
2818 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2819
2820 ocfs2_unlink_subtree(inode, handle, left_path, path,
2821 subtree_index, dealloc);
2822 ocfs2_update_edge_lengths(inode, handle, left_path);
2823
2824 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002825 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002826 } else {
2827 /*
2828 * 'path' is also the leftmost path which
2829 * means it must be the only one. This gets
2830 * handled differently because we want to
2831 * revert the inode back to having extents
2832 * in-line.
2833 */
2834 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2835
Joel Beckerce1d9ea2008-08-20 16:30:07 -07002836 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07002837 el->l_tree_depth = 0;
2838 el->l_next_free_rec = 0;
2839 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2840
Joel Becker35dc0aa2008-08-20 16:25:06 -07002841 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07002842 }
2843
2844 ocfs2_journal_dirty(handle, path_root_bh(path));
2845
2846out:
2847 ocfs2_free_path(left_path);
2848 return ret;
2849}
2850
2851/*
2852 * Left rotation of btree records.
2853 *
2854 * In many ways, this is (unsurprisingly) the opposite of right
2855 * rotation. We start at some non-rightmost path containing an empty
2856 * extent in the leaf block. The code works its way to the rightmost
2857 * path by rotating records to the left in every subtree.
2858 *
2859 * This is used by any code which reduces the number of extent records
2860 * in a leaf. After removal, an empty record should be placed in the
2861 * leftmost list position.
2862 *
2863 * This won't handle a length update of the rightmost path records if
2864 * the rightmost tree leaf record is removed so the caller is
2865 * responsible for detecting and correcting that.
2866 */
2867static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2868 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002869 struct ocfs2_cached_dealloc_ctxt *dealloc,
2870 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002871{
2872 int ret, orig_credits = handle->h_buffer_credits;
2873 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2874 struct ocfs2_extent_block *eb;
2875 struct ocfs2_extent_list *el;
2876
2877 el = path_leaf_el(path);
2878 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2879 return 0;
2880
2881 if (path->p_tree_depth == 0) {
2882rightmost_no_delete:
2883 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08002884 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07002885 * it up front.
2886 */
2887 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2888 path_leaf_bh(path),
2889 path_leaf_el(path));
2890 if (ret)
2891 mlog_errno(ret);
2892 goto out;
2893 }
2894
2895 /*
2896 * Handle rightmost branch now. There's several cases:
2897 * 1) simple rotation leaving records in there. That's trivial.
2898 * 2) rotation requiring a branch delete - there's no more
2899 * records left. Two cases of this:
2900 * a) There are branches to the left.
2901 * b) This is also the leftmost (the only) branch.
2902 *
2903 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2904 * 2a) we need the left branch so that we can update it with the unlink
2905 * 2b) we need to bring the inode back to inline extents.
2906 */
2907
2908 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2909 el = &eb->h_list;
2910 if (eb->h_next_leaf_blk == 0) {
2911 /*
2912 * This gets a bit tricky if we're going to delete the
2913 * rightmost path. Get the other cases out of the way
2914 * 1st.
2915 */
2916 if (le16_to_cpu(el->l_next_free_rec) > 1)
2917 goto rightmost_no_delete;
2918
2919 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2920 ret = -EIO;
2921 ocfs2_error(inode->i_sb,
2922 "Inode %llu has empty extent block at %llu",
2923 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2924 (unsigned long long)le64_to_cpu(eb->h_blkno));
2925 goto out;
2926 }
2927
2928 /*
2929 * XXX: The caller can not trust "path" any more after
2930 * this as it will have been deleted. What do we do?
2931 *
2932 * In theory the rotate-for-merge code will never get
2933 * here because it'll always ask for a rotate in a
2934 * nonempty list.
2935 */
2936
2937 ret = ocfs2_remove_rightmost_path(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002938 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002939 if (ret)
2940 mlog_errno(ret);
2941 goto out;
2942 }
2943
2944 /*
2945 * Now we can loop, remembering the path we get from -EAGAIN
2946 * and restarting from there.
2947 */
2948try_rotate:
2949 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002950 dealloc, &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002951 if (ret && ret != -EAGAIN) {
2952 mlog_errno(ret);
2953 goto out;
2954 }
2955
2956 while (ret == -EAGAIN) {
2957 tmp_path = restart_path;
2958 restart_path = NULL;
2959
2960 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2961 tmp_path, dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002962 &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002963 if (ret && ret != -EAGAIN) {
2964 mlog_errno(ret);
2965 goto out;
2966 }
2967
2968 ocfs2_free_path(tmp_path);
2969 tmp_path = NULL;
2970
2971 if (ret == 0)
2972 goto try_rotate;
2973 }
2974
2975out:
2976 ocfs2_free_path(tmp_path);
2977 ocfs2_free_path(restart_path);
2978 return ret;
2979}
2980
2981static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
2982 int index)
2983{
2984 struct ocfs2_extent_rec *rec = &el->l_recs[index];
2985 unsigned int size;
2986
2987 if (rec->e_leaf_clusters == 0) {
2988 /*
2989 * We consumed all of the merged-from record. An empty
2990 * extent cannot exist anywhere but the 1st array
2991 * position, so move things over if the merged-from
2992 * record doesn't occupy that position.
2993 *
2994 * This creates a new empty extent so the caller
2995 * should be smart enough to have removed any existing
2996 * ones.
2997 */
2998 if (index > 0) {
2999 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3000 size = index * sizeof(struct ocfs2_extent_rec);
3001 memmove(&el->l_recs[1], &el->l_recs[0], size);
3002 }
3003
3004 /*
3005 * Always memset - the caller doesn't check whether it
3006 * created an empty extent, so there could be junk in
3007 * the other fields.
3008 */
3009 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3010 }
3011}
3012
Tao Ma677b9752008-01-30 14:21:05 +08003013static int ocfs2_get_right_path(struct inode *inode,
3014 struct ocfs2_path *left_path,
3015 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003016{
3017 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003018 u32 right_cpos;
3019 struct ocfs2_path *right_path = NULL;
3020 struct ocfs2_extent_list *left_el;
3021
3022 *ret_right_path = NULL;
3023
3024 /* This function shouldn't be called for non-trees. */
3025 BUG_ON(left_path->p_tree_depth == 0);
3026
3027 left_el = path_leaf_el(left_path);
3028 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3029
3030 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3031 &right_cpos);
3032 if (ret) {
3033 mlog_errno(ret);
3034 goto out;
3035 }
3036
3037 /* This function shouldn't be called for the rightmost leaf. */
3038 BUG_ON(right_cpos == 0);
3039
3040 right_path = ocfs2_new_path(path_root_bh(left_path),
3041 path_root_el(left_path));
3042 if (!right_path) {
3043 ret = -ENOMEM;
3044 mlog_errno(ret);
3045 goto out;
3046 }
3047
3048 ret = ocfs2_find_path(inode, right_path, right_cpos);
3049 if (ret) {
3050 mlog_errno(ret);
3051 goto out;
3052 }
3053
3054 *ret_right_path = right_path;
3055out:
3056 if (ret)
3057 ocfs2_free_path(right_path);
3058 return ret;
3059}
3060
3061/*
3062 * Remove split_rec clusters from the record at index and merge them
3063 * onto the beginning of the record "next" to it.
3064 * For index < l_count - 1, the next means the extent rec at index + 1.
3065 * For index == l_count - 1, the "next" means the 1st extent rec of the
3066 * next extent block.
3067 */
3068static int ocfs2_merge_rec_right(struct inode *inode,
3069 struct ocfs2_path *left_path,
3070 handle_t *handle,
3071 struct ocfs2_extent_rec *split_rec,
3072 int index)
3073{
3074 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003075 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3076 struct ocfs2_extent_rec *left_rec;
3077 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003078 struct ocfs2_extent_list *right_el;
3079 struct ocfs2_path *right_path = NULL;
3080 int subtree_index = 0;
3081 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3082 struct buffer_head *bh = path_leaf_bh(left_path);
3083 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003084
3085 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003086 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003087
Al Viro9d8df6a2008-05-21 06:32:11 +01003088 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003089 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3090 /* we meet with a cross extent block merge. */
3091 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3092 if (ret) {
3093 mlog_errno(ret);
3094 goto out;
3095 }
3096
3097 right_el = path_leaf_el(right_path);
3098 next_free = le16_to_cpu(right_el->l_next_free_rec);
3099 BUG_ON(next_free <= 0);
3100 right_rec = &right_el->l_recs[0];
3101 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003102 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003103 right_rec = &right_el->l_recs[1];
3104 }
3105
3106 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3107 le16_to_cpu(left_rec->e_leaf_clusters) !=
3108 le32_to_cpu(right_rec->e_cpos));
3109
3110 subtree_index = ocfs2_find_subtree_root(inode,
3111 left_path, right_path);
3112
3113 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3114 handle->h_buffer_credits,
3115 right_path);
3116 if (ret) {
3117 mlog_errno(ret);
3118 goto out;
3119 }
3120
3121 root_bh = left_path->p_node[subtree_index].bh;
3122 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3123
3124 ret = ocfs2_journal_access(handle, inode, root_bh,
3125 OCFS2_JOURNAL_ACCESS_WRITE);
3126 if (ret) {
3127 mlog_errno(ret);
3128 goto out;
3129 }
3130
3131 for (i = subtree_index + 1;
3132 i < path_num_items(right_path); i++) {
3133 ret = ocfs2_journal_access(handle, inode,
3134 right_path->p_node[i].bh,
3135 OCFS2_JOURNAL_ACCESS_WRITE);
3136 if (ret) {
3137 mlog_errno(ret);
3138 goto out;
3139 }
3140
3141 ret = ocfs2_journal_access(handle, inode,
3142 left_path->p_node[i].bh,
3143 OCFS2_JOURNAL_ACCESS_WRITE);
3144 if (ret) {
3145 mlog_errno(ret);
3146 goto out;
3147 }
3148 }
3149
3150 } else {
3151 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3152 right_rec = &el->l_recs[index + 1];
3153 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003154
3155 ret = ocfs2_journal_access(handle, inode, bh,
3156 OCFS2_JOURNAL_ACCESS_WRITE);
3157 if (ret) {
3158 mlog_errno(ret);
3159 goto out;
3160 }
3161
3162 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3163
3164 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3165 le64_add_cpu(&right_rec->e_blkno,
3166 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3167 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3168
3169 ocfs2_cleanup_merge(el, index);
3170
3171 ret = ocfs2_journal_dirty(handle, bh);
3172 if (ret)
3173 mlog_errno(ret);
3174
Tao Ma677b9752008-01-30 14:21:05 +08003175 if (right_path) {
3176 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3177 if (ret)
3178 mlog_errno(ret);
3179
3180 ocfs2_complete_edge_insert(inode, handle, left_path,
3181 right_path, subtree_index);
3182 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003183out:
Tao Ma677b9752008-01-30 14:21:05 +08003184 if (right_path)
3185 ocfs2_free_path(right_path);
3186 return ret;
3187}
3188
3189static int ocfs2_get_left_path(struct inode *inode,
3190 struct ocfs2_path *right_path,
3191 struct ocfs2_path **ret_left_path)
3192{
3193 int ret;
3194 u32 left_cpos;
3195 struct ocfs2_path *left_path = NULL;
3196
3197 *ret_left_path = NULL;
3198
3199 /* This function shouldn't be called for non-trees. */
3200 BUG_ON(right_path->p_tree_depth == 0);
3201
3202 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3203 right_path, &left_cpos);
3204 if (ret) {
3205 mlog_errno(ret);
3206 goto out;
3207 }
3208
3209 /* This function shouldn't be called for the leftmost leaf. */
3210 BUG_ON(left_cpos == 0);
3211
3212 left_path = ocfs2_new_path(path_root_bh(right_path),
3213 path_root_el(right_path));
3214 if (!left_path) {
3215 ret = -ENOMEM;
3216 mlog_errno(ret);
3217 goto out;
3218 }
3219
3220 ret = ocfs2_find_path(inode, left_path, left_cpos);
3221 if (ret) {
3222 mlog_errno(ret);
3223 goto out;
3224 }
3225
3226 *ret_left_path = left_path;
3227out:
3228 if (ret)
3229 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003230 return ret;
3231}
3232
3233/*
3234 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003235 * onto the tail of the record "before" it.
3236 * For index > 0, the "before" means the extent rec at index - 1.
3237 *
3238 * For index == 0, the "before" means the last record of the previous
3239 * extent block. And there is also a situation that we may need to
3240 * remove the rightmost leaf extent block in the right_path and change
3241 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003242 */
Tao Ma677b9752008-01-30 14:21:05 +08003243static int ocfs2_merge_rec_left(struct inode *inode,
3244 struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003245 handle_t *handle,
3246 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003247 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003248 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003249 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003250{
Tao Ma677b9752008-01-30 14:21:05 +08003251 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003252 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3253 struct ocfs2_extent_rec *left_rec;
3254 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003255 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3256 struct buffer_head *bh = path_leaf_bh(right_path);
3257 struct buffer_head *root_bh = NULL;
3258 struct ocfs2_path *left_path = NULL;
3259 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003260
Tao Ma677b9752008-01-30 14:21:05 +08003261 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003262
Mark Fasheh328d5752007-06-18 10:48:04 -07003263 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003264 if (index == 0) {
3265 /* we meet with a cross extent block merge. */
3266 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3267 if (ret) {
3268 mlog_errno(ret);
3269 goto out;
3270 }
3271
3272 left_el = path_leaf_el(left_path);
3273 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3274 le16_to_cpu(left_el->l_count));
3275
3276 left_rec = &left_el->l_recs[
3277 le16_to_cpu(left_el->l_next_free_rec) - 1];
3278 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3279 le16_to_cpu(left_rec->e_leaf_clusters) !=
3280 le32_to_cpu(split_rec->e_cpos));
3281
3282 subtree_index = ocfs2_find_subtree_root(inode,
3283 left_path, right_path);
3284
3285 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3286 handle->h_buffer_credits,
3287 left_path);
3288 if (ret) {
3289 mlog_errno(ret);
3290 goto out;
3291 }
3292
3293 root_bh = left_path->p_node[subtree_index].bh;
3294 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3295
3296 ret = ocfs2_journal_access(handle, inode, root_bh,
3297 OCFS2_JOURNAL_ACCESS_WRITE);
3298 if (ret) {
3299 mlog_errno(ret);
3300 goto out;
3301 }
3302
3303 for (i = subtree_index + 1;
3304 i < path_num_items(right_path); i++) {
3305 ret = ocfs2_journal_access(handle, inode,
3306 right_path->p_node[i].bh,
3307 OCFS2_JOURNAL_ACCESS_WRITE);
3308 if (ret) {
3309 mlog_errno(ret);
3310 goto out;
3311 }
3312
3313 ret = ocfs2_journal_access(handle, inode,
3314 left_path->p_node[i].bh,
3315 OCFS2_JOURNAL_ACCESS_WRITE);
3316 if (ret) {
3317 mlog_errno(ret);
3318 goto out;
3319 }
3320 }
3321 } else {
3322 left_rec = &el->l_recs[index - 1];
3323 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3324 has_empty_extent = 1;
3325 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003326
3327 ret = ocfs2_journal_access(handle, inode, bh,
3328 OCFS2_JOURNAL_ACCESS_WRITE);
3329 if (ret) {
3330 mlog_errno(ret);
3331 goto out;
3332 }
3333
3334 if (has_empty_extent && index == 1) {
3335 /*
3336 * The easy case - we can just plop the record right in.
3337 */
3338 *left_rec = *split_rec;
3339
3340 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003341 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003342 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003343
3344 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3345 le64_add_cpu(&right_rec->e_blkno,
3346 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3347 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3348
3349 ocfs2_cleanup_merge(el, index);
3350
3351 ret = ocfs2_journal_dirty(handle, bh);
3352 if (ret)
3353 mlog_errno(ret);
3354
Tao Ma677b9752008-01-30 14:21:05 +08003355 if (left_path) {
3356 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3357 if (ret)
3358 mlog_errno(ret);
3359
3360 /*
3361 * In the situation that the right_rec is empty and the extent
3362 * block is empty also, ocfs2_complete_edge_insert can't handle
3363 * it and we need to delete the right extent block.
3364 */
3365 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3366 le16_to_cpu(el->l_next_free_rec) == 1) {
3367
3368 ret = ocfs2_remove_rightmost_path(inode, handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003369 right_path,
3370 dealloc, et);
Tao Ma677b9752008-01-30 14:21:05 +08003371 if (ret) {
3372 mlog_errno(ret);
3373 goto out;
3374 }
3375
3376 /* Now the rightmost extent block has been deleted.
3377 * So we use the new rightmost path.
3378 */
3379 ocfs2_mv_path(right_path, left_path);
3380 left_path = NULL;
3381 } else
3382 ocfs2_complete_edge_insert(inode, handle, left_path,
3383 right_path, subtree_index);
3384 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003385out:
Tao Ma677b9752008-01-30 14:21:05 +08003386 if (left_path)
3387 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003388 return ret;
3389}
3390
3391static int ocfs2_try_to_merge_extent(struct inode *inode,
3392 handle_t *handle,
Tao Ma677b9752008-01-30 14:21:05 +08003393 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003394 int split_index,
3395 struct ocfs2_extent_rec *split_rec,
3396 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003397 struct ocfs2_merge_ctxt *ctxt,
3398 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003399
3400{
Tao Mao518d7262007-08-28 17:25:35 -07003401 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003402 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003403 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3404
3405 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3406
Tao Mao518d7262007-08-28 17:25:35 -07003407 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3408 /*
3409 * The merge code will need to create an empty
3410 * extent to take the place of the newly
3411 * emptied slot. Remove any pre-existing empty
3412 * extents - having more than one in a leaf is
3413 * illegal.
3414 */
Tao Ma677b9752008-01-30 14:21:05 +08003415 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003416 dealloc, et);
Tao Mao518d7262007-08-28 17:25:35 -07003417 if (ret) {
3418 mlog_errno(ret);
3419 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003420 }
Tao Mao518d7262007-08-28 17:25:35 -07003421 split_index--;
3422 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003423 }
3424
3425 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3426 /*
3427 * Left-right contig implies this.
3428 */
3429 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003430
3431 /*
3432 * Since the leftright insert always covers the entire
3433 * extent, this call will delete the insert record
3434 * entirely, resulting in an empty extent record added to
3435 * the extent block.
3436 *
3437 * Since the adding of an empty extent shifts
3438 * everything back to the right, there's no need to
3439 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003440 *
3441 * When the split_index is zero, we need to merge it to the
3442 * prevoius extent block. It is more efficient and easier
3443 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003444 */
Tao Ma677b9752008-01-30 14:21:05 +08003445 ret = ocfs2_merge_rec_right(inode, path,
3446 handle, split_rec,
3447 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003448 if (ret) {
3449 mlog_errno(ret);
3450 goto out;
3451 }
3452
3453 /*
3454 * We can only get this from logic error above.
3455 */
3456 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3457
Tao Ma677b9752008-01-30 14:21:05 +08003458 /* The merge left us with an empty extent, remove it. */
Tao Mae7d4cb62008-08-18 17:38:44 +08003459 ret = ocfs2_rotate_tree_left(inode, handle, path,
3460 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003461 if (ret) {
3462 mlog_errno(ret);
3463 goto out;
3464 }
Tao Ma677b9752008-01-30 14:21:05 +08003465
Mark Fasheh328d5752007-06-18 10:48:04 -07003466 rec = &el->l_recs[split_index];
3467
3468 /*
3469 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003470 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003471 */
Tao Ma677b9752008-01-30 14:21:05 +08003472 ret = ocfs2_merge_rec_left(inode, path,
3473 handle, rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003474 dealloc, et,
Tao Ma677b9752008-01-30 14:21:05 +08003475 split_index);
3476
Mark Fasheh328d5752007-06-18 10:48:04 -07003477 if (ret) {
3478 mlog_errno(ret);
3479 goto out;
3480 }
3481
Tao Ma677b9752008-01-30 14:21:05 +08003482 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003483 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003484 /*
3485 * Error from this last rotate is not critical, so
3486 * print but don't bubble it up.
3487 */
3488 if (ret)
3489 mlog_errno(ret);
3490 ret = 0;
3491 } else {
3492 /*
3493 * Merge a record to the left or right.
3494 *
3495 * 'contig_type' is relative to the existing record,
3496 * so for example, if we're "right contig", it's to
3497 * the record on the left (hence the left merge).
3498 */
3499 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3500 ret = ocfs2_merge_rec_left(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003501 path,
3502 handle, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003503 dealloc, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003504 split_index);
3505 if (ret) {
3506 mlog_errno(ret);
3507 goto out;
3508 }
3509 } else {
3510 ret = ocfs2_merge_rec_right(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003511 path,
3512 handle, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003513 split_index);
3514 if (ret) {
3515 mlog_errno(ret);
3516 goto out;
3517 }
3518 }
3519
3520 if (ctxt->c_split_covers_rec) {
3521 /*
3522 * The merge may have left an empty extent in
3523 * our leaf. Try to rotate it away.
3524 */
Tao Ma677b9752008-01-30 14:21:05 +08003525 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003526 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003527 if (ret)
3528 mlog_errno(ret);
3529 ret = 0;
3530 }
3531 }
3532
3533out:
3534 return ret;
3535}
3536
3537static void ocfs2_subtract_from_rec(struct super_block *sb,
3538 enum ocfs2_split_type split,
3539 struct ocfs2_extent_rec *rec,
3540 struct ocfs2_extent_rec *split_rec)
3541{
3542 u64 len_blocks;
3543
3544 len_blocks = ocfs2_clusters_to_blocks(sb,
3545 le16_to_cpu(split_rec->e_leaf_clusters));
3546
3547 if (split == SPLIT_LEFT) {
3548 /*
3549 * Region is on the left edge of the existing
3550 * record.
3551 */
3552 le32_add_cpu(&rec->e_cpos,
3553 le16_to_cpu(split_rec->e_leaf_clusters));
3554 le64_add_cpu(&rec->e_blkno, len_blocks);
3555 le16_add_cpu(&rec->e_leaf_clusters,
3556 -le16_to_cpu(split_rec->e_leaf_clusters));
3557 } else {
3558 /*
3559 * Region is on the right edge of the existing
3560 * record.
3561 */
3562 le16_add_cpu(&rec->e_leaf_clusters,
3563 -le16_to_cpu(split_rec->e_leaf_clusters));
3564 }
3565}
3566
Mark Fashehdcd05382007-01-16 11:32:23 -08003567/*
3568 * Do the final bits of extent record insertion at the target leaf
3569 * list. If this leaf is part of an allocation tree, it is assumed
3570 * that the tree above has been prepared.
3571 */
3572static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3573 struct ocfs2_extent_list *el,
3574 struct ocfs2_insert_type *insert,
3575 struct inode *inode)
3576{
3577 int i = insert->ins_contig_index;
3578 unsigned int range;
3579 struct ocfs2_extent_rec *rec;
3580
Mark Fashehe48edee2007-03-07 16:46:57 -08003581 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003582
Mark Fasheh328d5752007-06-18 10:48:04 -07003583 if (insert->ins_split != SPLIT_NONE) {
3584 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3585 BUG_ON(i == -1);
3586 rec = &el->l_recs[i];
3587 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3588 insert_rec);
3589 goto rotate;
3590 }
3591
Mark Fashehdcd05382007-01-16 11:32:23 -08003592 /*
3593 * Contiguous insert - either left or right.
3594 */
3595 if (insert->ins_contig != CONTIG_NONE) {
3596 rec = &el->l_recs[i];
3597 if (insert->ins_contig == CONTIG_LEFT) {
3598 rec->e_blkno = insert_rec->e_blkno;
3599 rec->e_cpos = insert_rec->e_cpos;
3600 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003601 le16_add_cpu(&rec->e_leaf_clusters,
3602 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003603 return;
3604 }
3605
3606 /*
3607 * Handle insert into an empty leaf.
3608 */
3609 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3610 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3611 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3612 el->l_recs[0] = *insert_rec;
3613 el->l_next_free_rec = cpu_to_le16(1);
3614 return;
3615 }
3616
3617 /*
3618 * Appending insert.
3619 */
3620 if (insert->ins_appending == APPEND_TAIL) {
3621 i = le16_to_cpu(el->l_next_free_rec) - 1;
3622 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003623 range = le32_to_cpu(rec->e_cpos)
3624 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003625 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3626
3627 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3628 le16_to_cpu(el->l_count),
3629 "inode %lu, depth %u, count %u, next free %u, "
3630 "rec.cpos %u, rec.clusters %u, "
3631 "insert.cpos %u, insert.clusters %u\n",
3632 inode->i_ino,
3633 le16_to_cpu(el->l_tree_depth),
3634 le16_to_cpu(el->l_count),
3635 le16_to_cpu(el->l_next_free_rec),
3636 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003637 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003638 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003639 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003640 i++;
3641 el->l_recs[i] = *insert_rec;
3642 le16_add_cpu(&el->l_next_free_rec, 1);
3643 return;
3644 }
3645
Mark Fasheh328d5752007-06-18 10:48:04 -07003646rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003647 /*
3648 * Ok, we have to rotate.
3649 *
3650 * At this point, it is safe to assume that inserting into an
3651 * empty leaf and appending to a leaf have both been handled
3652 * above.
3653 *
3654 * This leaf needs to have space, either by the empty 1st
3655 * extent record, or by virtue of an l_next_rec < l_count.
3656 */
3657 ocfs2_rotate_leaf(el, insert_rec);
3658}
3659
Mark Fasheh328d5752007-06-18 10:48:04 -07003660static void ocfs2_adjust_rightmost_records(struct inode *inode,
3661 handle_t *handle,
3662 struct ocfs2_path *path,
3663 struct ocfs2_extent_rec *insert_rec)
3664{
3665 int ret, i, next_free;
3666 struct buffer_head *bh;
3667 struct ocfs2_extent_list *el;
3668 struct ocfs2_extent_rec *rec;
3669
3670 /*
3671 * Update everything except the leaf block.
3672 */
3673 for (i = 0; i < path->p_tree_depth; i++) {
3674 bh = path->p_node[i].bh;
3675 el = path->p_node[i].el;
3676
3677 next_free = le16_to_cpu(el->l_next_free_rec);
3678 if (next_free == 0) {
3679 ocfs2_error(inode->i_sb,
3680 "Dinode %llu has a bad extent list",
3681 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3682 ret = -EIO;
3683 return;
3684 }
3685
3686 rec = &el->l_recs[next_free - 1];
3687
3688 rec->e_int_clusters = insert_rec->e_cpos;
3689 le32_add_cpu(&rec->e_int_clusters,
3690 le16_to_cpu(insert_rec->e_leaf_clusters));
3691 le32_add_cpu(&rec->e_int_clusters,
3692 -le32_to_cpu(rec->e_cpos));
3693
3694 ret = ocfs2_journal_dirty(handle, bh);
3695 if (ret)
3696 mlog_errno(ret);
3697
3698 }
3699}
3700
Mark Fashehdcd05382007-01-16 11:32:23 -08003701static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3702 struct ocfs2_extent_rec *insert_rec,
3703 struct ocfs2_path *right_path,
3704 struct ocfs2_path **ret_left_path)
3705{
Mark Fasheh328d5752007-06-18 10:48:04 -07003706 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003707 struct ocfs2_extent_list *el;
3708 struct ocfs2_path *left_path = NULL;
3709
3710 *ret_left_path = NULL;
3711
3712 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003713 * This shouldn't happen for non-trees. The extent rec cluster
3714 * count manipulation below only works for interior nodes.
3715 */
3716 BUG_ON(right_path->p_tree_depth == 0);
3717
3718 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003719 * If our appending insert is at the leftmost edge of a leaf,
3720 * then we might need to update the rightmost records of the
3721 * neighboring path.
3722 */
3723 el = path_leaf_el(right_path);
3724 next_free = le16_to_cpu(el->l_next_free_rec);
3725 if (next_free == 0 ||
3726 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3727 u32 left_cpos;
3728
3729 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3730 &left_cpos);
3731 if (ret) {
3732 mlog_errno(ret);
3733 goto out;
3734 }
3735
3736 mlog(0, "Append may need a left path update. cpos: %u, "
3737 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3738 left_cpos);
3739
3740 /*
3741 * No need to worry if the append is already in the
3742 * leftmost leaf.
3743 */
3744 if (left_cpos) {
3745 left_path = ocfs2_new_path(path_root_bh(right_path),
3746 path_root_el(right_path));
3747 if (!left_path) {
3748 ret = -ENOMEM;
3749 mlog_errno(ret);
3750 goto out;
3751 }
3752
3753 ret = ocfs2_find_path(inode, left_path, left_cpos);
3754 if (ret) {
3755 mlog_errno(ret);
3756 goto out;
3757 }
3758
3759 /*
3760 * ocfs2_insert_path() will pass the left_path to the
3761 * journal for us.
3762 */
3763 }
3764 }
3765
3766 ret = ocfs2_journal_access_path(inode, handle, right_path);
3767 if (ret) {
3768 mlog_errno(ret);
3769 goto out;
3770 }
3771
Mark Fasheh328d5752007-06-18 10:48:04 -07003772 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08003773
3774 *ret_left_path = left_path;
3775 ret = 0;
3776out:
3777 if (ret != 0)
3778 ocfs2_free_path(left_path);
3779
3780 return ret;
3781}
3782
Mark Fasheh328d5752007-06-18 10:48:04 -07003783static void ocfs2_split_record(struct inode *inode,
3784 struct ocfs2_path *left_path,
3785 struct ocfs2_path *right_path,
3786 struct ocfs2_extent_rec *split_rec,
3787 enum ocfs2_split_type split)
3788{
3789 int index;
3790 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3791 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3792 struct ocfs2_extent_rec *rec, *tmprec;
3793
3794 right_el = path_leaf_el(right_path);;
3795 if (left_path)
3796 left_el = path_leaf_el(left_path);
3797
3798 el = right_el;
3799 insert_el = right_el;
3800 index = ocfs2_search_extent_list(el, cpos);
3801 if (index != -1) {
3802 if (index == 0 && left_path) {
3803 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3804
3805 /*
3806 * This typically means that the record
3807 * started in the left path but moved to the
3808 * right as a result of rotation. We either
3809 * move the existing record to the left, or we
3810 * do the later insert there.
3811 *
3812 * In this case, the left path should always
3813 * exist as the rotate code will have passed
3814 * it back for a post-insert update.
3815 */
3816
3817 if (split == SPLIT_LEFT) {
3818 /*
3819 * It's a left split. Since we know
3820 * that the rotate code gave us an
3821 * empty extent in the left path, we
3822 * can just do the insert there.
3823 */
3824 insert_el = left_el;
3825 } else {
3826 /*
3827 * Right split - we have to move the
3828 * existing record over to the left
3829 * leaf. The insert will be into the
3830 * newly created empty extent in the
3831 * right leaf.
3832 */
3833 tmprec = &right_el->l_recs[index];
3834 ocfs2_rotate_leaf(left_el, tmprec);
3835 el = left_el;
3836
3837 memset(tmprec, 0, sizeof(*tmprec));
3838 index = ocfs2_search_extent_list(left_el, cpos);
3839 BUG_ON(index == -1);
3840 }
3841 }
3842 } else {
3843 BUG_ON(!left_path);
3844 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3845 /*
3846 * Left path is easy - we can just allow the insert to
3847 * happen.
3848 */
3849 el = left_el;
3850 insert_el = left_el;
3851 index = ocfs2_search_extent_list(el, cpos);
3852 BUG_ON(index == -1);
3853 }
3854
3855 rec = &el->l_recs[index];
3856 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3857 ocfs2_rotate_leaf(insert_el, split_rec);
3858}
3859
Mark Fashehdcd05382007-01-16 11:32:23 -08003860/*
Tao Mae7d4cb62008-08-18 17:38:44 +08003861 * This function only does inserts on an allocation b-tree. For tree
3862 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08003863 *
3864 * right_path is the path we want to do the actual insert
3865 * in. left_path should only be passed in if we need to update that
3866 * portion of the tree after an edge insert.
3867 */
3868static int ocfs2_insert_path(struct inode *inode,
3869 handle_t *handle,
3870 struct ocfs2_path *left_path,
3871 struct ocfs2_path *right_path,
3872 struct ocfs2_extent_rec *insert_rec,
3873 struct ocfs2_insert_type *insert)
3874{
3875 int ret, subtree_index;
3876 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003877
Mark Fashehdcd05382007-01-16 11:32:23 -08003878 if (left_path) {
3879 int credits = handle->h_buffer_credits;
3880
3881 /*
3882 * There's a chance that left_path got passed back to
3883 * us without being accounted for in the
3884 * journal. Extend our transaction here to be sure we
3885 * can change those blocks.
3886 */
3887 credits += left_path->p_tree_depth;
3888
3889 ret = ocfs2_extend_trans(handle, credits);
3890 if (ret < 0) {
3891 mlog_errno(ret);
3892 goto out;
3893 }
3894
3895 ret = ocfs2_journal_access_path(inode, handle, left_path);
3896 if (ret < 0) {
3897 mlog_errno(ret);
3898 goto out;
3899 }
3900 }
3901
Mark Fashehe8aed342007-12-03 16:43:01 -08003902 /*
3903 * Pass both paths to the journal. The majority of inserts
3904 * will be touching all components anyway.
3905 */
3906 ret = ocfs2_journal_access_path(inode, handle, right_path);
3907 if (ret < 0) {
3908 mlog_errno(ret);
3909 goto out;
3910 }
3911
Mark Fasheh328d5752007-06-18 10:48:04 -07003912 if (insert->ins_split != SPLIT_NONE) {
3913 /*
3914 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02003915 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07003916 * function sort it all out.
3917 */
3918 ocfs2_split_record(inode, left_path, right_path,
3919 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08003920
3921 /*
3922 * Split might have modified either leaf and we don't
3923 * have a guarantee that the later edge insert will
3924 * dirty this for us.
3925 */
3926 if (left_path)
3927 ret = ocfs2_journal_dirty(handle,
3928 path_leaf_bh(left_path));
3929 if (ret)
3930 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07003931 } else
3932 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3933 insert, inode);
Mark Fashehdcd05382007-01-16 11:32:23 -08003934
Mark Fashehdcd05382007-01-16 11:32:23 -08003935 ret = ocfs2_journal_dirty(handle, leaf_bh);
3936 if (ret)
3937 mlog_errno(ret);
3938
3939 if (left_path) {
3940 /*
3941 * The rotate code has indicated that we need to fix
3942 * up portions of the tree after the insert.
3943 *
3944 * XXX: Should we extend the transaction here?
3945 */
3946 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3947 right_path);
3948 ocfs2_complete_edge_insert(inode, handle, left_path,
3949 right_path, subtree_index);
3950 }
3951
3952 ret = 0;
3953out:
3954 return ret;
3955}
3956
3957static int ocfs2_do_insert_extent(struct inode *inode,
3958 handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003959 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08003960 struct ocfs2_extent_rec *insert_rec,
3961 struct ocfs2_insert_type *type)
3962{
3963 int ret, rotate = 0;
3964 u32 cpos;
3965 struct ocfs2_path *right_path = NULL;
3966 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08003967 struct ocfs2_extent_list *el;
3968
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003969 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08003970
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003971 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehdcd05382007-01-16 11:32:23 -08003972 OCFS2_JOURNAL_ACCESS_WRITE);
3973 if (ret) {
3974 mlog_errno(ret);
3975 goto out;
3976 }
3977
3978 if (le16_to_cpu(el->l_tree_depth) == 0) {
3979 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
3980 goto out_update_clusters;
3981 }
3982
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003983 right_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fashehdcd05382007-01-16 11:32:23 -08003984 if (!right_path) {
3985 ret = -ENOMEM;
3986 mlog_errno(ret);
3987 goto out;
3988 }
3989
3990 /*
3991 * Determine the path to start with. Rotations need the
3992 * rightmost path, everything else can go directly to the
3993 * target leaf.
3994 */
3995 cpos = le32_to_cpu(insert_rec->e_cpos);
3996 if (type->ins_appending == APPEND_NONE &&
3997 type->ins_contig == CONTIG_NONE) {
3998 rotate = 1;
3999 cpos = UINT_MAX;
4000 }
4001
4002 ret = ocfs2_find_path(inode, right_path, cpos);
4003 if (ret) {
4004 mlog_errno(ret);
4005 goto out;
4006 }
4007
4008 /*
4009 * Rotations and appends need special treatment - they modify
4010 * parts of the tree's above them.
4011 *
4012 * Both might pass back a path immediate to the left of the
4013 * one being inserted to. This will be cause
4014 * ocfs2_insert_path() to modify the rightmost records of
4015 * left_path to account for an edge insert.
4016 *
4017 * XXX: When modifying this code, keep in mind that an insert
4018 * can wind up skipping both of these two special cases...
4019 */
4020 if (rotate) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004021 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004022 le32_to_cpu(insert_rec->e_cpos),
4023 right_path, &left_path);
4024 if (ret) {
4025 mlog_errno(ret);
4026 goto out;
4027 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004028
4029 /*
4030 * ocfs2_rotate_tree_right() might have extended the
4031 * transaction without re-journaling our tree root.
4032 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004033 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehe8aed342007-12-03 16:43:01 -08004034 OCFS2_JOURNAL_ACCESS_WRITE);
4035 if (ret) {
4036 mlog_errno(ret);
4037 goto out;
4038 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004039 } else if (type->ins_appending == APPEND_TAIL
4040 && type->ins_contig != CONTIG_LEFT) {
4041 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4042 right_path, &left_path);
4043 if (ret) {
4044 mlog_errno(ret);
4045 goto out;
4046 }
4047 }
4048
4049 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4050 insert_rec, type);
4051 if (ret) {
4052 mlog_errno(ret);
4053 goto out;
4054 }
4055
4056out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004057 if (type->ins_split == SPLIT_NONE)
Joel Becker35dc0aa2008-08-20 16:25:06 -07004058 ocfs2_et_update_clusters(inode, et,
4059 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004060
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004061 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004062 if (ret)
4063 mlog_errno(ret);
4064
4065out:
4066 ocfs2_free_path(left_path);
4067 ocfs2_free_path(right_path);
4068
4069 return ret;
4070}
4071
Mark Fasheh328d5752007-06-18 10:48:04 -07004072static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08004073ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004074 struct ocfs2_extent_list *el, int index,
4075 struct ocfs2_extent_rec *split_rec)
4076{
Tao Maad5a4d72008-01-30 14:21:32 +08004077 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004078 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004079 u32 left_cpos, right_cpos;
4080 struct ocfs2_extent_rec *rec = NULL;
4081 struct ocfs2_extent_list *new_el;
4082 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4083 struct buffer_head *bh;
4084 struct ocfs2_extent_block *eb;
4085
4086 if (index > 0) {
4087 rec = &el->l_recs[index - 1];
4088 } else if (path->p_tree_depth > 0) {
4089 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4090 path, &left_cpos);
4091 if (status)
4092 goto out;
4093
4094 if (left_cpos != 0) {
4095 left_path = ocfs2_new_path(path_root_bh(path),
4096 path_root_el(path));
4097 if (!left_path)
4098 goto out;
4099
4100 status = ocfs2_find_path(inode, left_path, left_cpos);
4101 if (status)
4102 goto out;
4103
4104 new_el = path_leaf_el(left_path);
4105
4106 if (le16_to_cpu(new_el->l_next_free_rec) !=
4107 le16_to_cpu(new_el->l_count)) {
4108 bh = path_leaf_bh(left_path);
4109 eb = (struct ocfs2_extent_block *)bh->b_data;
4110 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4111 eb);
4112 goto out;
4113 }
4114 rec = &new_el->l_recs[
4115 le16_to_cpu(new_el->l_next_free_rec) - 1];
4116 }
4117 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004118
4119 /*
4120 * We're careful to check for an empty extent record here -
4121 * the merge code will know what to do if it sees one.
4122 */
Tao Maad5a4d72008-01-30 14:21:32 +08004123 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004124 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4125 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4126 ret = CONTIG_RIGHT;
4127 } else {
4128 ret = ocfs2_extent_contig(inode, rec, split_rec);
4129 }
4130 }
4131
Tao Maad5a4d72008-01-30 14:21:32 +08004132 rec = NULL;
4133 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4134 rec = &el->l_recs[index + 1];
4135 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4136 path->p_tree_depth > 0) {
4137 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4138 path, &right_cpos);
4139 if (status)
4140 goto out;
4141
4142 if (right_cpos == 0)
4143 goto out;
4144
4145 right_path = ocfs2_new_path(path_root_bh(path),
4146 path_root_el(path));
4147 if (!right_path)
4148 goto out;
4149
4150 status = ocfs2_find_path(inode, right_path, right_cpos);
4151 if (status)
4152 goto out;
4153
4154 new_el = path_leaf_el(right_path);
4155 rec = &new_el->l_recs[0];
4156 if (ocfs2_is_empty_extent(rec)) {
4157 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4158 bh = path_leaf_bh(right_path);
4159 eb = (struct ocfs2_extent_block *)bh->b_data;
4160 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4161 eb);
4162 goto out;
4163 }
4164 rec = &new_el->l_recs[1];
4165 }
4166 }
4167
4168 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004169 enum ocfs2_contig_type contig_type;
4170
Mark Fasheh328d5752007-06-18 10:48:04 -07004171 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4172
4173 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4174 ret = CONTIG_LEFTRIGHT;
4175 else if (ret == CONTIG_NONE)
4176 ret = contig_type;
4177 }
4178
Tao Maad5a4d72008-01-30 14:21:32 +08004179out:
4180 if (left_path)
4181 ocfs2_free_path(left_path);
4182 if (right_path)
4183 ocfs2_free_path(right_path);
4184
Mark Fasheh328d5752007-06-18 10:48:04 -07004185 return ret;
4186}
4187
Mark Fashehdcd05382007-01-16 11:32:23 -08004188static void ocfs2_figure_contig_type(struct inode *inode,
4189 struct ocfs2_insert_type *insert,
4190 struct ocfs2_extent_list *el,
Tao Maca12b7c2008-08-18 17:38:52 +08004191 struct ocfs2_extent_rec *insert_rec,
4192 struct ocfs2_extent_tree *et)
Mark Fashehdcd05382007-01-16 11:32:23 -08004193{
4194 int i;
4195 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4196
Mark Fashehe48edee2007-03-07 16:46:57 -08004197 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4198
Mark Fashehdcd05382007-01-16 11:32:23 -08004199 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4200 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4201 insert_rec);
4202 if (contig_type != CONTIG_NONE) {
4203 insert->ins_contig_index = i;
4204 break;
4205 }
4206 }
4207 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004208
4209 if (insert->ins_contig != CONTIG_NONE) {
4210 struct ocfs2_extent_rec *rec =
4211 &el->l_recs[insert->ins_contig_index];
4212 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4213 le16_to_cpu(insert_rec->e_leaf_clusters);
4214
4215 /*
4216 * Caller might want us to limit the size of extents, don't
4217 * calculate contiguousness if we might exceed that limit.
4218 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004219 if (et->et_max_leaf_clusters &&
4220 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004221 insert->ins_contig = CONTIG_NONE;
4222 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004223}
4224
4225/*
4226 * This should only be called against the righmost leaf extent list.
4227 *
4228 * ocfs2_figure_appending_type() will figure out whether we'll have to
4229 * insert at the tail of the rightmost leaf.
4230 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004231 * This should also work against the root extent list for tree's with 0
4232 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004233 * then the logic here makes sense.
4234 */
4235static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4236 struct ocfs2_extent_list *el,
4237 struct ocfs2_extent_rec *insert_rec)
4238{
4239 int i;
4240 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4241 struct ocfs2_extent_rec *rec;
4242
4243 insert->ins_appending = APPEND_NONE;
4244
Mark Fashehe48edee2007-03-07 16:46:57 -08004245 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004246
4247 if (!el->l_next_free_rec)
4248 goto set_tail_append;
4249
4250 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4251 /* Were all records empty? */
4252 if (le16_to_cpu(el->l_next_free_rec) == 1)
4253 goto set_tail_append;
4254 }
4255
4256 i = le16_to_cpu(el->l_next_free_rec) - 1;
4257 rec = &el->l_recs[i];
4258
Mark Fashehe48edee2007-03-07 16:46:57 -08004259 if (cpos >=
4260 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004261 goto set_tail_append;
4262
4263 return;
4264
4265set_tail_append:
4266 insert->ins_appending = APPEND_TAIL;
4267}
4268
4269/*
4270 * Helper function called at the begining of an insert.
4271 *
4272 * This computes a few things that are commonly used in the process of
4273 * inserting into the btree:
4274 * - Whether the new extent is contiguous with an existing one.
4275 * - The current tree depth.
4276 * - Whether the insert is an appending one.
4277 * - The total # of free records in the tree.
4278 *
4279 * All of the information is stored on the ocfs2_insert_type
4280 * structure.
4281 */
4282static int ocfs2_figure_insert_type(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004283 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004284 struct buffer_head **last_eb_bh,
4285 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004286 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004287 struct ocfs2_insert_type *insert)
4288{
4289 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004290 struct ocfs2_extent_block *eb;
4291 struct ocfs2_extent_list *el;
4292 struct ocfs2_path *path = NULL;
4293 struct buffer_head *bh = NULL;
4294
Mark Fasheh328d5752007-06-18 10:48:04 -07004295 insert->ins_split = SPLIT_NONE;
4296
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004297 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004298 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4299
4300 if (el->l_tree_depth) {
4301 /*
4302 * If we have tree depth, we read in the
4303 * rightmost extent block ahead of time as
4304 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4305 * may want it later.
4306 */
4307 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
Joel Becker35dc0aa2008-08-20 16:25:06 -07004308 ocfs2_et_get_last_eb_blk(et), &bh,
Mark Fashehdcd05382007-01-16 11:32:23 -08004309 OCFS2_BH_CACHED, inode);
4310 if (ret) {
4311 mlog_exit(ret);
4312 goto out;
4313 }
4314 eb = (struct ocfs2_extent_block *) bh->b_data;
4315 el = &eb->h_list;
4316 }
4317
4318 /*
4319 * Unless we have a contiguous insert, we'll need to know if
4320 * there is room left in our allocation tree for another
4321 * extent record.
4322 *
4323 * XXX: This test is simplistic, we can search for empty
4324 * extent records too.
4325 */
Tao Maoc77534f2007-08-28 17:22:33 -07004326 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004327 le16_to_cpu(el->l_next_free_rec);
4328
4329 if (!insert->ins_tree_depth) {
Tao Maca12b7c2008-08-18 17:38:52 +08004330 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004331 ocfs2_figure_appending_type(insert, el, insert_rec);
4332 return 0;
4333 }
4334
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004335 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fashehdcd05382007-01-16 11:32:23 -08004336 if (!path) {
4337 ret = -ENOMEM;
4338 mlog_errno(ret);
4339 goto out;
4340 }
4341
4342 /*
4343 * In the case that we're inserting past what the tree
4344 * currently accounts for, ocfs2_find_path() will return for
4345 * us the rightmost tree path. This is accounted for below in
4346 * the appending code.
4347 */
4348 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4349 if (ret) {
4350 mlog_errno(ret);
4351 goto out;
4352 }
4353
4354 el = path_leaf_el(path);
4355
4356 /*
4357 * Now that we have the path, there's two things we want to determine:
4358 * 1) Contiguousness (also set contig_index if this is so)
4359 *
4360 * 2) Are we doing an append? We can trivially break this up
4361 * into two types of appends: simple record append, or a
4362 * rotate inside the tail leaf.
4363 */
Tao Maca12b7c2008-08-18 17:38:52 +08004364 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004365
4366 /*
4367 * The insert code isn't quite ready to deal with all cases of
4368 * left contiguousness. Specifically, if it's an insert into
4369 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004370 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004371 * left. For now, just catch that case and fool the layers
4372 * above us. This works just fine for tree_depth == 0, which
4373 * is why we allow that above.
4374 */
4375 if (insert->ins_contig == CONTIG_LEFT &&
4376 insert->ins_contig_index == 0)
4377 insert->ins_contig = CONTIG_NONE;
4378
4379 /*
4380 * Ok, so we can simply compare against last_eb to figure out
4381 * whether the path doesn't exist. This will only happen in
4382 * the case that we're doing a tail append, so maybe we can
4383 * take advantage of that information somehow.
4384 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004385 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004386 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004387 /*
4388 * Ok, ocfs2_find_path() returned us the rightmost
4389 * tree path. This might be an appending insert. There are
4390 * two cases:
4391 * 1) We're doing a true append at the tail:
4392 * -This might even be off the end of the leaf
4393 * 2) We're "appending" by rotating in the tail
4394 */
4395 ocfs2_figure_appending_type(insert, el, insert_rec);
4396 }
4397
4398out:
4399 ocfs2_free_path(path);
4400
4401 if (ret == 0)
4402 *last_eb_bh = bh;
4403 else
4404 brelse(bh);
4405 return ret;
4406}
4407
4408/*
4409 * Insert an extent into an inode btree.
4410 *
4411 * The caller needs to update fe->i_clusters
4412 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004413int ocfs2_insert_extent(struct ocfs2_super *osb,
4414 handle_t *handle,
4415 struct inode *inode,
4416 struct ocfs2_extent_tree *et,
4417 u32 cpos,
4418 u64 start_blk,
4419 u32 new_clusters,
4420 u8 flags,
4421 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004422{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004423 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004424 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004425 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004426 struct ocfs2_insert_type insert = {0, };
4427 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004428
Mark Fashehdcd05382007-01-16 11:32:23 -08004429 mlog(0, "add %u clusters at position %u to inode %llu\n",
4430 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004431
Mark Fashehe48edee2007-03-07 16:46:57 -08004432 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004433 rec.e_cpos = cpu_to_le32(cpos);
4434 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004435 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004436 rec.e_flags = flags;
Joel Becker1e61ee72008-08-20 18:32:45 -07004437 status = ocfs2_et_insert_check(inode, et, &rec);
4438 if (status) {
4439 mlog_errno(status);
4440 goto bail;
4441 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004442
Tao Mae7d4cb62008-08-18 17:38:44 +08004443 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004444 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004445 if (status < 0) {
4446 mlog_errno(status);
4447 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004448 }
4449
Mark Fashehdcd05382007-01-16 11:32:23 -08004450 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4451 "Insert.contig_index: %d, Insert.free_records: %d, "
4452 "Insert.tree_depth: %d\n",
4453 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004454 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004455
Tao Maoc77534f2007-08-28 17:22:33 -07004456 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004457 status = ocfs2_grow_tree(inode, handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004458 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004459 meta_ac);
4460 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004461 mlog_errno(status);
4462 goto bail;
4463 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004464 }
4465
Mark Fashehdcd05382007-01-16 11:32:23 -08004466 /* Finally, we can add clusters. This might rotate the tree for us. */
Tao Mae7d4cb62008-08-18 17:38:44 +08004467 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004468 if (status < 0)
4469 mlog_errno(status);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004470 else if (et->et_ops == &ocfs2_dinode_et_ops)
Mark Fasheh83418972007-04-23 18:53:12 -07004471 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004472
4473bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08004474 if (last_eb_bh)
4475 brelse(last_eb_bh);
4476
Tao Maf56654c2008-08-18 17:38:48 +08004477 mlog_exit(status);
4478 return status;
4479}
4480
Tao Ma0eb8d472008-08-18 17:38:45 +08004481/*
4482 * Allcate and add clusters into the extent b-tree.
4483 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004484 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004485 * it is not limited to the file storage. Any extent tree can use this
4486 * function if it implements the proper ocfs2_extent_tree.
4487 */
4488int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4489 struct inode *inode,
4490 u32 *logical_offset,
4491 u32 clusters_to_add,
4492 int mark_unwritten,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004493 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004494 handle_t *handle,
4495 struct ocfs2_alloc_context *data_ac,
4496 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004497 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004498{
4499 int status = 0;
4500 int free_extents;
4501 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4502 u32 bit_off, num_bits;
4503 u64 block;
4504 u8 flags = 0;
4505
4506 BUG_ON(!clusters_to_add);
4507
4508 if (mark_unwritten)
4509 flags = OCFS2_EXT_UNWRITTEN;
4510
Joel Beckerf99b9b72008-08-20 19:36:33 -07004511 free_extents = ocfs2_num_free_extents(osb, inode, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004512 if (free_extents < 0) {
4513 status = free_extents;
4514 mlog_errno(status);
4515 goto leave;
4516 }
4517
4518 /* there are two cases which could cause us to EAGAIN in the
4519 * we-need-more-metadata case:
4520 * 1) we haven't reserved *any*
4521 * 2) we are so fragmented, we've needed to add metadata too
4522 * many times. */
4523 if (!free_extents && !meta_ac) {
4524 mlog(0, "we haven't reserved any metadata!\n");
4525 status = -EAGAIN;
4526 reason = RESTART_META;
4527 goto leave;
4528 } else if ((!free_extents)
4529 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004530 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004531 mlog(0, "filesystem is really fragmented...\n");
4532 status = -EAGAIN;
4533 reason = RESTART_META;
4534 goto leave;
4535 }
4536
4537 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4538 clusters_to_add, &bit_off, &num_bits);
4539 if (status < 0) {
4540 if (status != -ENOSPC)
4541 mlog_errno(status);
4542 goto leave;
4543 }
4544
4545 BUG_ON(num_bits > clusters_to_add);
4546
4547 /* reserve our write early -- insert_extent may update the inode */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004548 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Tao Ma0eb8d472008-08-18 17:38:45 +08004549 OCFS2_JOURNAL_ACCESS_WRITE);
4550 if (status < 0) {
4551 mlog_errno(status);
4552 goto leave;
4553 }
4554
4555 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4556 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4557 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004558 status = ocfs2_insert_extent(osb, handle, inode, et,
4559 *logical_offset, block,
4560 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004561 if (status < 0) {
4562 mlog_errno(status);
4563 goto leave;
4564 }
4565
Joel Beckerf99b9b72008-08-20 19:36:33 -07004566 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004567 if (status < 0) {
4568 mlog_errno(status);
4569 goto leave;
4570 }
4571
4572 clusters_to_add -= num_bits;
4573 *logical_offset += num_bits;
4574
4575 if (clusters_to_add) {
4576 mlog(0, "need to alloc once more, wanted = %u\n",
4577 clusters_to_add);
4578 status = -EAGAIN;
4579 reason = RESTART_TRANS;
4580 }
4581
4582leave:
4583 mlog_exit(status);
4584 if (reason_ret)
4585 *reason_ret = reason;
4586 return status;
4587}
4588
Mark Fasheh328d5752007-06-18 10:48:04 -07004589static void ocfs2_make_right_split_rec(struct super_block *sb,
4590 struct ocfs2_extent_rec *split_rec,
4591 u32 cpos,
4592 struct ocfs2_extent_rec *rec)
4593{
4594 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4595 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4596
4597 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4598
4599 split_rec->e_cpos = cpu_to_le32(cpos);
4600 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4601
4602 split_rec->e_blkno = rec->e_blkno;
4603 le64_add_cpu(&split_rec->e_blkno,
4604 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4605
4606 split_rec->e_flags = rec->e_flags;
4607}
4608
4609static int ocfs2_split_and_insert(struct inode *inode,
4610 handle_t *handle,
4611 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004612 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004613 struct buffer_head **last_eb_bh,
4614 int split_index,
4615 struct ocfs2_extent_rec *orig_split_rec,
4616 struct ocfs2_alloc_context *meta_ac)
4617{
4618 int ret = 0, depth;
4619 unsigned int insert_range, rec_range, do_leftright = 0;
4620 struct ocfs2_extent_rec tmprec;
4621 struct ocfs2_extent_list *rightmost_el;
4622 struct ocfs2_extent_rec rec;
4623 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4624 struct ocfs2_insert_type insert;
4625 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004626
4627leftright:
4628 /*
4629 * Store a copy of the record on the stack - it might move
4630 * around as the tree is manipulated below.
4631 */
4632 rec = path_leaf_el(path)->l_recs[split_index];
4633
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004634 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004635
4636 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4637 if (depth) {
4638 BUG_ON(!(*last_eb_bh));
4639 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4640 rightmost_el = &eb->h_list;
4641 }
4642
4643 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4644 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004645 ret = ocfs2_grow_tree(inode, handle, et,
4646 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004647 if (ret) {
4648 mlog_errno(ret);
4649 goto out;
4650 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004651 }
4652
4653 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4654 insert.ins_appending = APPEND_NONE;
4655 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004656 insert.ins_tree_depth = depth;
4657
4658 insert_range = le32_to_cpu(split_rec.e_cpos) +
4659 le16_to_cpu(split_rec.e_leaf_clusters);
4660 rec_range = le32_to_cpu(rec.e_cpos) +
4661 le16_to_cpu(rec.e_leaf_clusters);
4662
4663 if (split_rec.e_cpos == rec.e_cpos) {
4664 insert.ins_split = SPLIT_LEFT;
4665 } else if (insert_range == rec_range) {
4666 insert.ins_split = SPLIT_RIGHT;
4667 } else {
4668 /*
4669 * Left/right split. We fake this as a right split
4670 * first and then make a second pass as a left split.
4671 */
4672 insert.ins_split = SPLIT_RIGHT;
4673
4674 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4675 &rec);
4676
4677 split_rec = tmprec;
4678
4679 BUG_ON(do_leftright);
4680 do_leftright = 1;
4681 }
4682
Tao Mae7d4cb62008-08-18 17:38:44 +08004683 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004684 if (ret) {
4685 mlog_errno(ret);
4686 goto out;
4687 }
4688
4689 if (do_leftright == 1) {
4690 u32 cpos;
4691 struct ocfs2_extent_list *el;
4692
4693 do_leftright++;
4694 split_rec = *orig_split_rec;
4695
4696 ocfs2_reinit_path(path, 1);
4697
4698 cpos = le32_to_cpu(split_rec.e_cpos);
4699 ret = ocfs2_find_path(inode, path, cpos);
4700 if (ret) {
4701 mlog_errno(ret);
4702 goto out;
4703 }
4704
4705 el = path_leaf_el(path);
4706 split_index = ocfs2_search_extent_list(el, cpos);
4707 goto leftright;
4708 }
4709out:
4710
4711 return ret;
4712}
4713
4714/*
4715 * Mark part or all of the extent record at split_index in the leaf
4716 * pointed to by path as written. This removes the unwritten
4717 * extent flag.
4718 *
4719 * Care is taken to handle contiguousness so as to not grow the tree.
4720 *
4721 * meta_ac is not strictly necessary - we only truly need it if growth
4722 * of the tree is required. All other cases will degrade into a less
4723 * optimal tree layout.
4724 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004725 * last_eb_bh should be the rightmost leaf block for any extent
4726 * btree. Since a split may grow the tree or a merge might shrink it,
4727 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07004728 *
4729 * This code is optimized for readability - several passes might be
4730 * made over certain portions of the tree. All of those blocks will
4731 * have been brought into cache (and pinned via the journal), so the
4732 * extra overhead is not expressed in terms of disk reads.
4733 */
4734static int __ocfs2_mark_extent_written(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004735 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004736 handle_t *handle,
4737 struct ocfs2_path *path,
4738 int split_index,
4739 struct ocfs2_extent_rec *split_rec,
4740 struct ocfs2_alloc_context *meta_ac,
4741 struct ocfs2_cached_dealloc_ctxt *dealloc)
4742{
4743 int ret = 0;
4744 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004745 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07004746 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4747 struct ocfs2_merge_ctxt ctxt;
4748 struct ocfs2_extent_list *rightmost_el;
4749
Roel Kluin3cf0c502007-10-27 00:20:36 +02004750 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004751 ret = -EIO;
4752 mlog_errno(ret);
4753 goto out;
4754 }
4755
4756 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4757 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4758 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4759 ret = -EIO;
4760 mlog_errno(ret);
4761 goto out;
4762 }
4763
Tao Maad5a4d72008-01-30 14:21:32 +08004764 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07004765 split_index,
4766 split_rec);
4767
4768 /*
4769 * The core merge / split code wants to know how much room is
4770 * left in this inodes allocation tree, so we pass the
4771 * rightmost extent list.
4772 */
4773 if (path->p_tree_depth) {
4774 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004775
4776 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
Joel Becker35dc0aa2008-08-20 16:25:06 -07004777 ocfs2_et_get_last_eb_blk(et),
Mark Fasheh328d5752007-06-18 10:48:04 -07004778 &last_eb_bh, OCFS2_BH_CACHED, inode);
4779 if (ret) {
4780 mlog_exit(ret);
4781 goto out;
4782 }
4783
4784 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4785 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4786 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4787 ret = -EROFS;
4788 goto out;
4789 }
4790
4791 rightmost_el = &eb->h_list;
4792 } else
4793 rightmost_el = path_root_el(path);
4794
Mark Fasheh328d5752007-06-18 10:48:04 -07004795 if (rec->e_cpos == split_rec->e_cpos &&
4796 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4797 ctxt.c_split_covers_rec = 1;
4798 else
4799 ctxt.c_split_covers_rec = 0;
4800
4801 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4802
Mark Fasheh015452b2007-09-12 10:21:22 -07004803 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4804 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4805 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004806
4807 if (ctxt.c_contig_type == CONTIG_NONE) {
4808 if (ctxt.c_split_covers_rec)
4809 el->l_recs[split_index] = *split_rec;
4810 else
Tao Mae7d4cb62008-08-18 17:38:44 +08004811 ret = ocfs2_split_and_insert(inode, handle, path, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004812 &last_eb_bh, split_index,
4813 split_rec, meta_ac);
4814 if (ret)
4815 mlog_errno(ret);
4816 } else {
4817 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4818 split_index, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08004819 dealloc, &ctxt, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07004820 if (ret)
4821 mlog_errno(ret);
4822 }
4823
Mark Fasheh328d5752007-06-18 10:48:04 -07004824out:
4825 brelse(last_eb_bh);
4826 return ret;
4827}
4828
4829/*
4830 * Mark the already-existing extent at cpos as written for len clusters.
4831 *
4832 * If the existing extent is larger than the request, initiate a
4833 * split. An attempt will be made at merging with adjacent extents.
4834 *
4835 * The caller is responsible for passing down meta_ac if we'll need it.
4836 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004837int ocfs2_mark_extent_written(struct inode *inode,
4838 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004839 handle_t *handle, u32 cpos, u32 len, u32 phys,
4840 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004841 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07004842{
4843 int ret, index;
4844 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4845 struct ocfs2_extent_rec split_rec;
4846 struct ocfs2_path *left_path = NULL;
4847 struct ocfs2_extent_list *el;
4848
4849 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4850 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4851
4852 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4853 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4854 "that are being written to, but the feature bit "
4855 "is not set in the super block.",
4856 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4857 ret = -EROFS;
4858 goto out;
4859 }
4860
4861 /*
4862 * XXX: This should be fixed up so that we just re-insert the
4863 * next extent records.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004864 *
4865 * XXX: This is a hack on the extent tree, maybe it should be
4866 * an op?
Mark Fasheh328d5752007-06-18 10:48:04 -07004867 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004868 if (et->et_ops == &ocfs2_dinode_et_ops)
Tao Mae7d4cb62008-08-18 17:38:44 +08004869 ocfs2_extent_map_trunc(inode, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07004870
Joel Beckerf99b9b72008-08-20 19:36:33 -07004871 left_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fasheh328d5752007-06-18 10:48:04 -07004872 if (!left_path) {
4873 ret = -ENOMEM;
4874 mlog_errno(ret);
4875 goto out;
4876 }
4877
4878 ret = ocfs2_find_path(inode, left_path, cpos);
4879 if (ret) {
4880 mlog_errno(ret);
4881 goto out;
4882 }
4883 el = path_leaf_el(left_path);
4884
4885 index = ocfs2_search_extent_list(el, cpos);
4886 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4887 ocfs2_error(inode->i_sb,
4888 "Inode %llu has an extent at cpos %u which can no "
4889 "longer be found.\n",
4890 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4891 ret = -EROFS;
4892 goto out;
4893 }
4894
4895 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4896 split_rec.e_cpos = cpu_to_le32(cpos);
4897 split_rec.e_leaf_clusters = cpu_to_le16(len);
4898 split_rec.e_blkno = cpu_to_le64(start_blkno);
4899 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4900 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4901
Joel Beckerf99b9b72008-08-20 19:36:33 -07004902 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004903 index, &split_rec, meta_ac,
4904 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07004905 if (ret)
4906 mlog_errno(ret);
4907
4908out:
4909 ocfs2_free_path(left_path);
4910 return ret;
4911}
4912
Tao Mae7d4cb62008-08-18 17:38:44 +08004913static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07004914 handle_t *handle, struct ocfs2_path *path,
4915 int index, u32 new_range,
4916 struct ocfs2_alloc_context *meta_ac)
4917{
4918 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07004919 struct buffer_head *last_eb_bh = NULL;
4920 struct ocfs2_extent_block *eb;
4921 struct ocfs2_extent_list *rightmost_el, *el;
4922 struct ocfs2_extent_rec split_rec;
4923 struct ocfs2_extent_rec *rec;
4924 struct ocfs2_insert_type insert;
4925
4926 /*
4927 * Setup the record to split before we grow the tree.
4928 */
4929 el = path_leaf_el(path);
4930 rec = &el->l_recs[index];
4931 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4932
4933 depth = path->p_tree_depth;
4934 if (depth > 0) {
4935 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
Joel Becker35dc0aa2008-08-20 16:25:06 -07004936 ocfs2_et_get_last_eb_blk(et),
Mark Fashehd0c7d702007-07-03 13:27:22 -07004937 &last_eb_bh, OCFS2_BH_CACHED, inode);
4938 if (ret < 0) {
4939 mlog_errno(ret);
4940 goto out;
4941 }
4942
4943 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4944 rightmost_el = &eb->h_list;
4945 } else
4946 rightmost_el = path_leaf_el(path);
4947
Tao Ma811f9332008-08-18 17:38:43 +08004948 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004949 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07004950 ret = ocfs2_extend_trans(handle, credits);
4951 if (ret) {
4952 mlog_errno(ret);
4953 goto out;
4954 }
4955
4956 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4957 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004958 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07004959 meta_ac);
4960 if (ret) {
4961 mlog_errno(ret);
4962 goto out;
4963 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07004964 }
4965
4966 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4967 insert.ins_appending = APPEND_NONE;
4968 insert.ins_contig = CONTIG_NONE;
4969 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07004970 insert.ins_tree_depth = depth;
4971
Tao Mae7d4cb62008-08-18 17:38:44 +08004972 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07004973 if (ret)
4974 mlog_errno(ret);
4975
4976out:
4977 brelse(last_eb_bh);
4978 return ret;
4979}
4980
4981static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
4982 struct ocfs2_path *path, int index,
4983 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08004984 u32 cpos, u32 len,
4985 struct ocfs2_extent_tree *et)
Mark Fashehd0c7d702007-07-03 13:27:22 -07004986{
4987 int ret;
4988 u32 left_cpos, rec_range, trunc_range;
4989 int wants_rotate = 0, is_rightmost_tree_rec = 0;
4990 struct super_block *sb = inode->i_sb;
4991 struct ocfs2_path *left_path = NULL;
4992 struct ocfs2_extent_list *el = path_leaf_el(path);
4993 struct ocfs2_extent_rec *rec;
4994 struct ocfs2_extent_block *eb;
4995
4996 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004997 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07004998 if (ret) {
4999 mlog_errno(ret);
5000 goto out;
5001 }
5002
5003 index--;
5004 }
5005
5006 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5007 path->p_tree_depth) {
5008 /*
5009 * Check whether this is the rightmost tree record. If
5010 * we remove all of this record or part of its right
5011 * edge then an update of the record lengths above it
5012 * will be required.
5013 */
5014 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5015 if (eb->h_next_leaf_blk == 0)
5016 is_rightmost_tree_rec = 1;
5017 }
5018
5019 rec = &el->l_recs[index];
5020 if (index == 0 && path->p_tree_depth &&
5021 le32_to_cpu(rec->e_cpos) == cpos) {
5022 /*
5023 * Changing the leftmost offset (via partial or whole
5024 * record truncate) of an interior (or rightmost) path
5025 * means we have to update the subtree that is formed
5026 * by this leaf and the one to it's left.
5027 *
5028 * There are two cases we can skip:
5029 * 1) Path is the leftmost one in our inode tree.
5030 * 2) The leaf is rightmost and will be empty after
5031 * we remove the extent record - the rotate code
5032 * knows how to update the newly formed edge.
5033 */
5034
5035 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5036 &left_cpos);
5037 if (ret) {
5038 mlog_errno(ret);
5039 goto out;
5040 }
5041
5042 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5043 left_path = ocfs2_new_path(path_root_bh(path),
5044 path_root_el(path));
5045 if (!left_path) {
5046 ret = -ENOMEM;
5047 mlog_errno(ret);
5048 goto out;
5049 }
5050
5051 ret = ocfs2_find_path(inode, left_path, left_cpos);
5052 if (ret) {
5053 mlog_errno(ret);
5054 goto out;
5055 }
5056 }
5057 }
5058
5059 ret = ocfs2_extend_rotate_transaction(handle, 0,
5060 handle->h_buffer_credits,
5061 path);
5062 if (ret) {
5063 mlog_errno(ret);
5064 goto out;
5065 }
5066
5067 ret = ocfs2_journal_access_path(inode, handle, path);
5068 if (ret) {
5069 mlog_errno(ret);
5070 goto out;
5071 }
5072
5073 ret = ocfs2_journal_access_path(inode, handle, left_path);
5074 if (ret) {
5075 mlog_errno(ret);
5076 goto out;
5077 }
5078
5079 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5080 trunc_range = cpos + len;
5081
5082 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5083 int next_free;
5084
5085 memset(rec, 0, sizeof(*rec));
5086 ocfs2_cleanup_merge(el, index);
5087 wants_rotate = 1;
5088
5089 next_free = le16_to_cpu(el->l_next_free_rec);
5090 if (is_rightmost_tree_rec && next_free > 1) {
5091 /*
5092 * We skip the edge update if this path will
5093 * be deleted by the rotate code.
5094 */
5095 rec = &el->l_recs[next_free - 1];
5096 ocfs2_adjust_rightmost_records(inode, handle, path,
5097 rec);
5098 }
5099 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5100 /* Remove leftmost portion of the record. */
5101 le32_add_cpu(&rec->e_cpos, len);
5102 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5103 le16_add_cpu(&rec->e_leaf_clusters, -len);
5104 } else if (rec_range == trunc_range) {
5105 /* Remove rightmost portion of the record */
5106 le16_add_cpu(&rec->e_leaf_clusters, -len);
5107 if (is_rightmost_tree_rec)
5108 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5109 } else {
5110 /* Caller should have trapped this. */
5111 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5112 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5113 le32_to_cpu(rec->e_cpos),
5114 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5115 BUG();
5116 }
5117
5118 if (left_path) {
5119 int subtree_index;
5120
5121 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5122 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5123 subtree_index);
5124 }
5125
5126 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5127
Tao Mae7d4cb62008-08-18 17:38:44 +08005128 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005129 if (ret) {
5130 mlog_errno(ret);
5131 goto out;
5132 }
5133
5134out:
5135 ocfs2_free_path(left_path);
5136 return ret;
5137}
5138
Joel Beckerf99b9b72008-08-20 19:36:33 -07005139int ocfs2_remove_extent(struct inode *inode,
5140 struct ocfs2_extent_tree *et,
Mark Fasheh063c4562007-07-03 13:34:11 -07005141 u32 cpos, u32 len, handle_t *handle,
5142 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005143 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005144{
5145 int ret, index;
5146 u32 rec_range, trunc_range;
5147 struct ocfs2_extent_rec *rec;
5148 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005149 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005150
5151 ocfs2_extent_map_trunc(inode, 0);
5152
Joel Beckerf99b9b72008-08-20 19:36:33 -07005153 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005154 if (!path) {
5155 ret = -ENOMEM;
5156 mlog_errno(ret);
5157 goto out;
5158 }
5159
5160 ret = ocfs2_find_path(inode, path, cpos);
5161 if (ret) {
5162 mlog_errno(ret);
5163 goto out;
5164 }
5165
5166 el = path_leaf_el(path);
5167 index = ocfs2_search_extent_list(el, cpos);
5168 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5169 ocfs2_error(inode->i_sb,
5170 "Inode %llu has an extent at cpos %u which can no "
5171 "longer be found.\n",
5172 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5173 ret = -EROFS;
5174 goto out;
5175 }
5176
5177 /*
5178 * We have 3 cases of extent removal:
5179 * 1) Range covers the entire extent rec
5180 * 2) Range begins or ends on one edge of the extent rec
5181 * 3) Range is in the middle of the extent rec (no shared edges)
5182 *
5183 * For case 1 we remove the extent rec and left rotate to
5184 * fill the hole.
5185 *
5186 * For case 2 we just shrink the existing extent rec, with a
5187 * tree update if the shrinking edge is also the edge of an
5188 * extent block.
5189 *
5190 * For case 3 we do a right split to turn the extent rec into
5191 * something case 2 can handle.
5192 */
5193 rec = &el->l_recs[index];
5194 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5195 trunc_range = cpos + len;
5196
5197 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5198
5199 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5200 "(cpos %u, len %u)\n",
5201 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5202 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5203
5204 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5205 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005206 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005207 if (ret) {
5208 mlog_errno(ret);
5209 goto out;
5210 }
5211 } else {
Joel Beckerf99b9b72008-08-20 19:36:33 -07005212 ret = ocfs2_split_tree(inode, et, handle, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005213 trunc_range, meta_ac);
5214 if (ret) {
5215 mlog_errno(ret);
5216 goto out;
5217 }
5218
5219 /*
5220 * The split could have manipulated the tree enough to
5221 * move the record location, so we have to look for it again.
5222 */
5223 ocfs2_reinit_path(path, 1);
5224
5225 ret = ocfs2_find_path(inode, path, cpos);
5226 if (ret) {
5227 mlog_errno(ret);
5228 goto out;
5229 }
5230
5231 el = path_leaf_el(path);
5232 index = ocfs2_search_extent_list(el, cpos);
5233 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5234 ocfs2_error(inode->i_sb,
5235 "Inode %llu: split at cpos %u lost record.",
5236 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5237 cpos);
5238 ret = -EROFS;
5239 goto out;
5240 }
5241
5242 /*
5243 * Double check our values here. If anything is fishy,
5244 * it's easier to catch it at the top level.
5245 */
5246 rec = &el->l_recs[index];
5247 rec_range = le32_to_cpu(rec->e_cpos) +
5248 ocfs2_rec_clusters(el, rec);
5249 if (rec_range != trunc_range) {
5250 ocfs2_error(inode->i_sb,
5251 "Inode %llu: error after split at cpos %u"
5252 "trunc len %u, existing record is (%u,%u)",
5253 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5254 cpos, len, le32_to_cpu(rec->e_cpos),
5255 ocfs2_rec_clusters(el, rec));
5256 ret = -EROFS;
5257 goto out;
5258 }
5259
5260 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005261 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005262 if (ret) {
5263 mlog_errno(ret);
5264 goto out;
5265 }
5266 }
5267
5268out:
5269 ocfs2_free_path(path);
5270 return ret;
5271}
5272
Mark Fasheh063c4562007-07-03 13:34:11 -07005273int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005274{
5275 struct buffer_head *tl_bh = osb->osb_tl_bh;
5276 struct ocfs2_dinode *di;
5277 struct ocfs2_truncate_log *tl;
5278
5279 di = (struct ocfs2_dinode *) tl_bh->b_data;
5280 tl = &di->id2.i_dealloc;
5281
5282 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5283 "slot %d, invalid truncate log parameters: used = "
5284 "%u, count = %u\n", osb->slot_num,
5285 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5286 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5287}
5288
5289static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5290 unsigned int new_start)
5291{
5292 unsigned int tail_index;
5293 unsigned int current_tail;
5294
5295 /* No records, nothing to coalesce */
5296 if (!le16_to_cpu(tl->tl_used))
5297 return 0;
5298
5299 tail_index = le16_to_cpu(tl->tl_used) - 1;
5300 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5301 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5302
5303 return current_tail == new_start;
5304}
5305
Mark Fasheh063c4562007-07-03 13:34:11 -07005306int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5307 handle_t *handle,
5308 u64 start_blk,
5309 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005310{
5311 int status, index;
5312 unsigned int start_cluster, tl_count;
5313 struct inode *tl_inode = osb->osb_tl_inode;
5314 struct buffer_head *tl_bh = osb->osb_tl_bh;
5315 struct ocfs2_dinode *di;
5316 struct ocfs2_truncate_log *tl;
5317
Mark Fashehb06970532006-03-03 10:24:33 -08005318 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5319 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005320
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005321 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005322
5323 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5324
5325 di = (struct ocfs2_dinode *) tl_bh->b_data;
5326 tl = &di->id2.i_dealloc;
5327 if (!OCFS2_IS_VALID_DINODE(di)) {
5328 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5329 status = -EIO;
5330 goto bail;
5331 }
5332
5333 tl_count = le16_to_cpu(tl->tl_count);
5334 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5335 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005336 "Truncate record count on #%llu invalid "
5337 "wanted %u, actual %u\n",
5338 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005339 ocfs2_truncate_recs_per_inode(osb->sb),
5340 le16_to_cpu(tl->tl_count));
5341
5342 /* Caller should have known to flush before calling us. */
5343 index = le16_to_cpu(tl->tl_used);
5344 if (index >= tl_count) {
5345 status = -ENOSPC;
5346 mlog_errno(status);
5347 goto bail;
5348 }
5349
5350 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5351 OCFS2_JOURNAL_ACCESS_WRITE);
5352 if (status < 0) {
5353 mlog_errno(status);
5354 goto bail;
5355 }
5356
5357 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005358 "%llu (index = %d)\n", num_clusters, start_cluster,
5359 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005360
5361 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5362 /*
5363 * Move index back to the record we are coalescing with.
5364 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5365 */
5366 index--;
5367
5368 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5369 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5370 index, le32_to_cpu(tl->tl_recs[index].t_start),
5371 num_clusters);
5372 } else {
5373 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5374 tl->tl_used = cpu_to_le16(index + 1);
5375 }
5376 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5377
5378 status = ocfs2_journal_dirty(handle, tl_bh);
5379 if (status < 0) {
5380 mlog_errno(status);
5381 goto bail;
5382 }
5383
5384bail:
5385 mlog_exit(status);
5386 return status;
5387}
5388
5389static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005390 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005391 struct inode *data_alloc_inode,
5392 struct buffer_head *data_alloc_bh)
5393{
5394 int status = 0;
5395 int i;
5396 unsigned int num_clusters;
5397 u64 start_blk;
5398 struct ocfs2_truncate_rec rec;
5399 struct ocfs2_dinode *di;
5400 struct ocfs2_truncate_log *tl;
5401 struct inode *tl_inode = osb->osb_tl_inode;
5402 struct buffer_head *tl_bh = osb->osb_tl_bh;
5403
5404 mlog_entry_void();
5405
5406 di = (struct ocfs2_dinode *) tl_bh->b_data;
5407 tl = &di->id2.i_dealloc;
5408 i = le16_to_cpu(tl->tl_used) - 1;
5409 while (i >= 0) {
5410 /* Caller has given us at least enough credits to
5411 * update the truncate log dinode */
5412 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5413 OCFS2_JOURNAL_ACCESS_WRITE);
5414 if (status < 0) {
5415 mlog_errno(status);
5416 goto bail;
5417 }
5418
5419 tl->tl_used = cpu_to_le16(i);
5420
5421 status = ocfs2_journal_dirty(handle, tl_bh);
5422 if (status < 0) {
5423 mlog_errno(status);
5424 goto bail;
5425 }
5426
5427 /* TODO: Perhaps we can calculate the bulk of the
5428 * credits up front rather than extending like
5429 * this. */
5430 status = ocfs2_extend_trans(handle,
5431 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5432 if (status < 0) {
5433 mlog_errno(status);
5434 goto bail;
5435 }
5436
5437 rec = tl->tl_recs[i];
5438 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5439 le32_to_cpu(rec.t_start));
5440 num_clusters = le32_to_cpu(rec.t_clusters);
5441
5442 /* if start_blk is not set, we ignore the record as
5443 * invalid. */
5444 if (start_blk) {
5445 mlog(0, "free record %d, start = %u, clusters = %u\n",
5446 i, le32_to_cpu(rec.t_start), num_clusters);
5447
5448 status = ocfs2_free_clusters(handle, data_alloc_inode,
5449 data_alloc_bh, start_blk,
5450 num_clusters);
5451 if (status < 0) {
5452 mlog_errno(status);
5453 goto bail;
5454 }
5455 }
5456 i--;
5457 }
5458
5459bail:
5460 mlog_exit(status);
5461 return status;
5462}
5463
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005464/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005465int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005466{
5467 int status;
5468 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005469 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005470 struct inode *tl_inode = osb->osb_tl_inode;
5471 struct inode *data_alloc_inode = NULL;
5472 struct buffer_head *tl_bh = osb->osb_tl_bh;
5473 struct buffer_head *data_alloc_bh = NULL;
5474 struct ocfs2_dinode *di;
5475 struct ocfs2_truncate_log *tl;
5476
5477 mlog_entry_void();
5478
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005479 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005480
5481 di = (struct ocfs2_dinode *) tl_bh->b_data;
5482 tl = &di->id2.i_dealloc;
5483 if (!OCFS2_IS_VALID_DINODE(di)) {
5484 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5485 status = -EIO;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005486 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005487 }
5488
5489 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005490 mlog(0, "Flush %u records from truncate log #%llu\n",
5491 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005492 if (!num_to_flush) {
5493 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005494 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005495 }
5496
5497 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5498 GLOBAL_BITMAP_SYSTEM_INODE,
5499 OCFS2_INVALID_SLOT);
5500 if (!data_alloc_inode) {
5501 status = -EINVAL;
5502 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005503 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005504 }
5505
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005506 mutex_lock(&data_alloc_inode->i_mutex);
5507
Mark Fashehe63aecb62007-10-18 15:30:42 -07005508 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005509 if (status < 0) {
5510 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005511 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005512 }
5513
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005514 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005515 if (IS_ERR(handle)) {
5516 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005517 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005518 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005519 }
5520
5521 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5522 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005523 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005524 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005525
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005526 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005527
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005528out_unlock:
5529 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005530 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005531
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005532out_mutex:
5533 mutex_unlock(&data_alloc_inode->i_mutex);
5534 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005535
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005536out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005537 mlog_exit(status);
5538 return status;
5539}
5540
5541int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5542{
5543 int status;
5544 struct inode *tl_inode = osb->osb_tl_inode;
5545
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005546 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005547 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005548 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005549
5550 return status;
5551}
5552
David Howellsc4028952006-11-22 14:57:56 +00005553static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005554{
5555 int status;
David Howellsc4028952006-11-22 14:57:56 +00005556 struct ocfs2_super *osb =
5557 container_of(work, struct ocfs2_super,
5558 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005559
5560 mlog_entry_void();
5561
5562 status = ocfs2_flush_truncate_log(osb);
5563 if (status < 0)
5564 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005565 else
5566 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005567
5568 mlog_exit(status);
5569}
5570
5571#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5572void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5573 int cancel)
5574{
5575 if (osb->osb_tl_inode) {
5576 /* We want to push off log flushes while truncates are
5577 * still running. */
5578 if (cancel)
5579 cancel_delayed_work(&osb->osb_truncate_log_wq);
5580
5581 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5582 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5583 }
5584}
5585
5586static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5587 int slot_num,
5588 struct inode **tl_inode,
5589 struct buffer_head **tl_bh)
5590{
5591 int status;
5592 struct inode *inode = NULL;
5593 struct buffer_head *bh = NULL;
5594
5595 inode = ocfs2_get_system_file_inode(osb,
5596 TRUNCATE_LOG_SYSTEM_INODE,
5597 slot_num);
5598 if (!inode) {
5599 status = -EINVAL;
5600 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5601 goto bail;
5602 }
5603
5604 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
5605 OCFS2_BH_CACHED, inode);
5606 if (status < 0) {
5607 iput(inode);
5608 mlog_errno(status);
5609 goto bail;
5610 }
5611
5612 *tl_inode = inode;
5613 *tl_bh = bh;
5614bail:
5615 mlog_exit(status);
5616 return status;
5617}
5618
5619/* called during the 1st stage of node recovery. we stamp a clean
5620 * truncate log and pass back a copy for processing later. if the
5621 * truncate log does not require processing, a *tl_copy is set to
5622 * NULL. */
5623int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5624 int slot_num,
5625 struct ocfs2_dinode **tl_copy)
5626{
5627 int status;
5628 struct inode *tl_inode = NULL;
5629 struct buffer_head *tl_bh = NULL;
5630 struct ocfs2_dinode *di;
5631 struct ocfs2_truncate_log *tl;
5632
5633 *tl_copy = NULL;
5634
5635 mlog(0, "recover truncate log from slot %d\n", slot_num);
5636
5637 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5638 if (status < 0) {
5639 mlog_errno(status);
5640 goto bail;
5641 }
5642
5643 di = (struct ocfs2_dinode *) tl_bh->b_data;
5644 tl = &di->id2.i_dealloc;
5645 if (!OCFS2_IS_VALID_DINODE(di)) {
5646 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
5647 status = -EIO;
5648 goto bail;
5649 }
5650
5651 if (le16_to_cpu(tl->tl_used)) {
5652 mlog(0, "We'll have %u logs to recover\n",
5653 le16_to_cpu(tl->tl_used));
5654
5655 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5656 if (!(*tl_copy)) {
5657 status = -ENOMEM;
5658 mlog_errno(status);
5659 goto bail;
5660 }
5661
5662 /* Assuming the write-out below goes well, this copy
5663 * will be passed back to recovery for processing. */
5664 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5665
5666 /* All we need to do to clear the truncate log is set
5667 * tl_used. */
5668 tl->tl_used = 0;
5669
5670 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5671 if (status < 0) {
5672 mlog_errno(status);
5673 goto bail;
5674 }
5675 }
5676
5677bail:
5678 if (tl_inode)
5679 iput(tl_inode);
5680 if (tl_bh)
5681 brelse(tl_bh);
5682
5683 if (status < 0 && (*tl_copy)) {
5684 kfree(*tl_copy);
5685 *tl_copy = NULL;
5686 }
5687
5688 mlog_exit(status);
5689 return status;
5690}
5691
5692int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5693 struct ocfs2_dinode *tl_copy)
5694{
5695 int status = 0;
5696 int i;
5697 unsigned int clusters, num_recs, start_cluster;
5698 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005699 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005700 struct inode *tl_inode = osb->osb_tl_inode;
5701 struct ocfs2_truncate_log *tl;
5702
5703 mlog_entry_void();
5704
5705 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5706 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5707 return -EINVAL;
5708 }
5709
5710 tl = &tl_copy->id2.i_dealloc;
5711 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005712 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07005713 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08005714
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005715 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005716 for(i = 0; i < num_recs; i++) {
5717 if (ocfs2_truncate_log_needs_flush(osb)) {
5718 status = __ocfs2_flush_truncate_log(osb);
5719 if (status < 0) {
5720 mlog_errno(status);
5721 goto bail_up;
5722 }
5723 }
5724
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005725 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005726 if (IS_ERR(handle)) {
5727 status = PTR_ERR(handle);
5728 mlog_errno(status);
5729 goto bail_up;
5730 }
5731
5732 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5733 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5734 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5735
5736 status = ocfs2_truncate_log_append(osb, handle,
5737 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005738 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005739 if (status < 0) {
5740 mlog_errno(status);
5741 goto bail_up;
5742 }
5743 }
5744
5745bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005746 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005747
5748 mlog_exit(status);
5749 return status;
5750}
5751
5752void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5753{
5754 int status;
5755 struct inode *tl_inode = osb->osb_tl_inode;
5756
5757 mlog_entry_void();
5758
5759 if (tl_inode) {
5760 cancel_delayed_work(&osb->osb_truncate_log_wq);
5761 flush_workqueue(ocfs2_wq);
5762
5763 status = ocfs2_flush_truncate_log(osb);
5764 if (status < 0)
5765 mlog_errno(status);
5766
5767 brelse(osb->osb_tl_bh);
5768 iput(osb->osb_tl_inode);
5769 }
5770
5771 mlog_exit_void();
5772}
5773
5774int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5775{
5776 int status;
5777 struct inode *tl_inode = NULL;
5778 struct buffer_head *tl_bh = NULL;
5779
5780 mlog_entry_void();
5781
5782 status = ocfs2_get_truncate_log_info(osb,
5783 osb->slot_num,
5784 &tl_inode,
5785 &tl_bh);
5786 if (status < 0)
5787 mlog_errno(status);
5788
5789 /* ocfs2_truncate_log_shutdown keys on the existence of
5790 * osb->osb_tl_inode so we don't set any of the osb variables
5791 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00005792 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5793 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08005794 osb->osb_tl_bh = tl_bh;
5795 osb->osb_tl_inode = tl_inode;
5796
5797 mlog_exit(status);
5798 return status;
5799}
5800
Mark Fasheh2b604352007-06-22 15:45:27 -07005801/*
5802 * Delayed de-allocation of suballocator blocks.
5803 *
5804 * Some sets of block de-allocations might involve multiple suballocator inodes.
5805 *
5806 * The locking for this can get extremely complicated, especially when
5807 * the suballocator inodes to delete from aren't known until deep
5808 * within an unrelated codepath.
5809 *
5810 * ocfs2_extent_block structures are a good example of this - an inode
5811 * btree could have been grown by any number of nodes each allocating
5812 * out of their own suballoc inode.
5813 *
5814 * These structures allow the delay of block de-allocation until a
5815 * later time, when locking of multiple cluster inodes won't cause
5816 * deadlock.
5817 */
5818
5819/*
5820 * Describes a single block free from a suballocator
5821 */
5822struct ocfs2_cached_block_free {
5823 struct ocfs2_cached_block_free *free_next;
5824 u64 free_blk;
5825 unsigned int free_bit;
5826};
5827
5828struct ocfs2_per_slot_free_list {
5829 struct ocfs2_per_slot_free_list *f_next_suballocator;
5830 int f_inode_type;
5831 int f_slot;
5832 struct ocfs2_cached_block_free *f_first;
5833};
5834
5835static int ocfs2_free_cached_items(struct ocfs2_super *osb,
5836 int sysfile_type,
5837 int slot,
5838 struct ocfs2_cached_block_free *head)
5839{
5840 int ret;
5841 u64 bg_blkno;
5842 handle_t *handle;
5843 struct inode *inode;
5844 struct buffer_head *di_bh = NULL;
5845 struct ocfs2_cached_block_free *tmp;
5846
5847 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5848 if (!inode) {
5849 ret = -EINVAL;
5850 mlog_errno(ret);
5851 goto out;
5852 }
5853
5854 mutex_lock(&inode->i_mutex);
5855
Mark Fashehe63aecb62007-10-18 15:30:42 -07005856 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07005857 if (ret) {
5858 mlog_errno(ret);
5859 goto out_mutex;
5860 }
5861
5862 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5863 if (IS_ERR(handle)) {
5864 ret = PTR_ERR(handle);
5865 mlog_errno(ret);
5866 goto out_unlock;
5867 }
5868
5869 while (head) {
5870 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5871 head->free_bit);
5872 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5873 head->free_bit, (unsigned long long)head->free_blk);
5874
5875 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5876 head->free_bit, bg_blkno, 1);
5877 if (ret) {
5878 mlog_errno(ret);
5879 goto out_journal;
5880 }
5881
5882 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
5883 if (ret) {
5884 mlog_errno(ret);
5885 goto out_journal;
5886 }
5887
5888 tmp = head;
5889 head = head->free_next;
5890 kfree(tmp);
5891 }
5892
5893out_journal:
5894 ocfs2_commit_trans(osb, handle);
5895
5896out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07005897 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07005898 brelse(di_bh);
5899out_mutex:
5900 mutex_unlock(&inode->i_mutex);
5901 iput(inode);
5902out:
5903 while(head) {
5904 /* Premature exit may have left some dangling items. */
5905 tmp = head;
5906 head = head->free_next;
5907 kfree(tmp);
5908 }
5909
5910 return ret;
5911}
5912
5913int ocfs2_run_deallocs(struct ocfs2_super *osb,
5914 struct ocfs2_cached_dealloc_ctxt *ctxt)
5915{
5916 int ret = 0, ret2;
5917 struct ocfs2_per_slot_free_list *fl;
5918
5919 if (!ctxt)
5920 return 0;
5921
5922 while (ctxt->c_first_suballocator) {
5923 fl = ctxt->c_first_suballocator;
5924
5925 if (fl->f_first) {
5926 mlog(0, "Free items: (type %u, slot %d)\n",
5927 fl->f_inode_type, fl->f_slot);
5928 ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
5929 fl->f_slot, fl->f_first);
5930 if (ret2)
5931 mlog_errno(ret2);
5932 if (!ret)
5933 ret = ret2;
5934 }
5935
5936 ctxt->c_first_suballocator = fl->f_next_suballocator;
5937 kfree(fl);
5938 }
5939
5940 return ret;
5941}
5942
5943static struct ocfs2_per_slot_free_list *
5944ocfs2_find_per_slot_free_list(int type,
5945 int slot,
5946 struct ocfs2_cached_dealloc_ctxt *ctxt)
5947{
5948 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
5949
5950 while (fl) {
5951 if (fl->f_inode_type == type && fl->f_slot == slot)
5952 return fl;
5953
5954 fl = fl->f_next_suballocator;
5955 }
5956
5957 fl = kmalloc(sizeof(*fl), GFP_NOFS);
5958 if (fl) {
5959 fl->f_inode_type = type;
5960 fl->f_slot = slot;
5961 fl->f_first = NULL;
5962 fl->f_next_suballocator = ctxt->c_first_suballocator;
5963
5964 ctxt->c_first_suballocator = fl;
5965 }
5966 return fl;
5967}
5968
5969static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
5970 int type, int slot, u64 blkno,
5971 unsigned int bit)
5972{
5973 int ret;
5974 struct ocfs2_per_slot_free_list *fl;
5975 struct ocfs2_cached_block_free *item;
5976
5977 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
5978 if (fl == NULL) {
5979 ret = -ENOMEM;
5980 mlog_errno(ret);
5981 goto out;
5982 }
5983
5984 item = kmalloc(sizeof(*item), GFP_NOFS);
5985 if (item == NULL) {
5986 ret = -ENOMEM;
5987 mlog_errno(ret);
5988 goto out;
5989 }
5990
5991 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
5992 type, slot, bit, (unsigned long long)blkno);
5993
5994 item->free_blk = blkno;
5995 item->free_bit = bit;
5996 item->free_next = fl->f_first;
5997
5998 fl->f_first = item;
5999
6000 ret = 0;
6001out:
6002 return ret;
6003}
6004
Mark Fasheh59a5e412007-06-22 15:52:36 -07006005static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6006 struct ocfs2_extent_block *eb)
6007{
6008 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6009 le16_to_cpu(eb->h_suballoc_slot),
6010 le64_to_cpu(eb->h_blkno),
6011 le16_to_cpu(eb->h_suballoc_bit));
6012}
6013
Mark Fashehccd979b2005-12-15 14:31:24 -08006014/* This function will figure out whether the currently last extent
6015 * block will be deleted, and if it will, what the new last extent
6016 * block will be so we can update his h_next_leaf_blk field, as well
6017 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006018static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006019 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006020 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006021 struct buffer_head **new_last_eb)
6022{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006023 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006024 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006025 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006026 struct ocfs2_extent_block *eb;
6027 struct ocfs2_extent_list *el;
6028 struct buffer_head *bh = NULL;
6029
6030 *new_last_eb = NULL;
6031
Mark Fashehccd979b2005-12-15 14:31:24 -08006032 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006033 if (!path->p_tree_depth)
6034 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006035
6036 /* trunc to zero special case - this makes tree_depth = 0
6037 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006038 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006039 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006040
Mark Fashehdcd05382007-01-16 11:32:23 -08006041 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006042 BUG_ON(!el->l_next_free_rec);
6043
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006044 /*
6045 * Make sure that this extent list will actually be empty
6046 * after we clear away the data. We can shortcut out if
6047 * there's more than one non-empty extent in the
6048 * list. Otherwise, a check of the remaining extent is
6049 * necessary.
6050 */
6051 next_free = le16_to_cpu(el->l_next_free_rec);
6052 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006053 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006054 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006055 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006056
6057 /* We may have a valid extent in index 1, check it. */
6058 if (next_free == 2)
6059 rec = &el->l_recs[1];
6060
6061 /*
6062 * Fall through - no more nonempty extents, so we want
6063 * to delete this leaf.
6064 */
6065 } else {
6066 if (next_free > 1)
6067 goto out;
6068
6069 rec = &el->l_recs[0];
6070 }
6071
6072 if (rec) {
6073 /*
6074 * Check it we'll only be trimming off the end of this
6075 * cluster.
6076 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006077 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006078 goto out;
6079 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006080
Mark Fashehdcd05382007-01-16 11:32:23 -08006081 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6082 if (ret) {
6083 mlog_errno(ret);
6084 goto out;
6085 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006086
Mark Fashehdcd05382007-01-16 11:32:23 -08006087 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6088 if (ret) {
6089 mlog_errno(ret);
6090 goto out;
6091 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006092
Mark Fashehdcd05382007-01-16 11:32:23 -08006093 eb = (struct ocfs2_extent_block *) bh->b_data;
6094 el = &eb->h_list;
6095 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
6096 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
6097 ret = -EROFS;
6098 goto out;
6099 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006100
6101 *new_last_eb = bh;
6102 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006103 mlog(0, "returning block %llu, (cpos: %u)\n",
6104 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6105out:
6106 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006107
Mark Fashehdcd05382007-01-16 11:32:23 -08006108 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006109}
6110
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006111/*
6112 * Trim some clusters off the rightmost edge of a tree. Only called
6113 * during truncate.
6114 *
6115 * The caller needs to:
6116 * - start journaling of each path component.
6117 * - compute and fully set up any new last ext block
6118 */
6119static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6120 handle_t *handle, struct ocfs2_truncate_context *tc,
6121 u32 clusters_to_del, u64 *delete_start)
6122{
6123 int ret, i, index = path->p_tree_depth;
6124 u32 new_edge = 0;
6125 u64 deleted_eb = 0;
6126 struct buffer_head *bh;
6127 struct ocfs2_extent_list *el;
6128 struct ocfs2_extent_rec *rec;
6129
6130 *delete_start = 0;
6131
6132 while (index >= 0) {
6133 bh = path->p_node[index].bh;
6134 el = path->p_node[index].el;
6135
6136 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6137 index, (unsigned long long)bh->b_blocknr);
6138
6139 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6140
6141 if (index !=
6142 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6143 ocfs2_error(inode->i_sb,
6144 "Inode %lu has invalid ext. block %llu",
6145 inode->i_ino,
6146 (unsigned long long)bh->b_blocknr);
6147 ret = -EROFS;
6148 goto out;
6149 }
6150
6151find_tail_record:
6152 i = le16_to_cpu(el->l_next_free_rec) - 1;
6153 rec = &el->l_recs[i];
6154
6155 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6156 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006157 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006158 (unsigned long long)le64_to_cpu(rec->e_blkno),
6159 le16_to_cpu(el->l_next_free_rec));
6160
Mark Fashehe48edee2007-03-07 16:46:57 -08006161 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006162
6163 if (le16_to_cpu(el->l_tree_depth) == 0) {
6164 /*
6165 * If the leaf block contains a single empty
6166 * extent and no records, we can just remove
6167 * the block.
6168 */
6169 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6170 memset(rec, 0,
6171 sizeof(struct ocfs2_extent_rec));
6172 el->l_next_free_rec = cpu_to_le16(0);
6173
6174 goto delete;
6175 }
6176
6177 /*
6178 * Remove any empty extents by shifting things
6179 * left. That should make life much easier on
6180 * the code below. This condition is rare
6181 * enough that we shouldn't see a performance
6182 * hit.
6183 */
6184 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6185 le16_add_cpu(&el->l_next_free_rec, -1);
6186
6187 for(i = 0;
6188 i < le16_to_cpu(el->l_next_free_rec); i++)
6189 el->l_recs[i] = el->l_recs[i + 1];
6190
6191 memset(&el->l_recs[i], 0,
6192 sizeof(struct ocfs2_extent_rec));
6193
6194 /*
6195 * We've modified our extent list. The
6196 * simplest way to handle this change
6197 * is to being the search from the
6198 * start again.
6199 */
6200 goto find_tail_record;
6201 }
6202
Mark Fashehe48edee2007-03-07 16:46:57 -08006203 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006204
6205 /*
6206 * We'll use "new_edge" on our way back up the
6207 * tree to know what our rightmost cpos is.
6208 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006209 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006210 new_edge += le32_to_cpu(rec->e_cpos);
6211
6212 /*
6213 * The caller will use this to delete data blocks.
6214 */
6215 *delete_start = le64_to_cpu(rec->e_blkno)
6216 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006217 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006218
6219 /*
6220 * If it's now empty, remove this record.
6221 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006222 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006223 memset(rec, 0,
6224 sizeof(struct ocfs2_extent_rec));
6225 le16_add_cpu(&el->l_next_free_rec, -1);
6226 }
6227 } else {
6228 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6229 memset(rec, 0,
6230 sizeof(struct ocfs2_extent_rec));
6231 le16_add_cpu(&el->l_next_free_rec, -1);
6232
6233 goto delete;
6234 }
6235
6236 /* Can this actually happen? */
6237 if (le16_to_cpu(el->l_next_free_rec) == 0)
6238 goto delete;
6239
6240 /*
6241 * We never actually deleted any clusters
6242 * because our leaf was empty. There's no
6243 * reason to adjust the rightmost edge then.
6244 */
6245 if (new_edge == 0)
6246 goto delete;
6247
Mark Fashehe48edee2007-03-07 16:46:57 -08006248 rec->e_int_clusters = cpu_to_le32(new_edge);
6249 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006250 -le32_to_cpu(rec->e_cpos));
6251
6252 /*
6253 * A deleted child record should have been
6254 * caught above.
6255 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006256 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006257 }
6258
6259delete:
6260 ret = ocfs2_journal_dirty(handle, bh);
6261 if (ret) {
6262 mlog_errno(ret);
6263 goto out;
6264 }
6265
6266 mlog(0, "extent list container %llu, after: record %d: "
6267 "(%u, %u, %llu), next = %u.\n",
6268 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006269 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006270 (unsigned long long)le64_to_cpu(rec->e_blkno),
6271 le16_to_cpu(el->l_next_free_rec));
6272
6273 /*
6274 * We must be careful to only attempt delete of an
6275 * extent block (and not the root inode block).
6276 */
6277 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6278 struct ocfs2_extent_block *eb =
6279 (struct ocfs2_extent_block *)bh->b_data;
6280
6281 /*
6282 * Save this for use when processing the
6283 * parent block.
6284 */
6285 deleted_eb = le64_to_cpu(eb->h_blkno);
6286
6287 mlog(0, "deleting this extent block.\n");
6288
6289 ocfs2_remove_from_cache(inode, bh);
6290
Mark Fashehe48edee2007-03-07 16:46:57 -08006291 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006292 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6293 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6294
Mark Fasheh59a5e412007-06-22 15:52:36 -07006295 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6296 /* An error here is not fatal. */
6297 if (ret < 0)
6298 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006299 } else {
6300 deleted_eb = 0;
6301 }
6302
6303 index--;
6304 }
6305
6306 ret = 0;
6307out:
6308 return ret;
6309}
6310
Mark Fashehccd979b2005-12-15 14:31:24 -08006311static int ocfs2_do_truncate(struct ocfs2_super *osb,
6312 unsigned int clusters_to_del,
6313 struct inode *inode,
6314 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006315 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006316 struct ocfs2_truncate_context *tc,
6317 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006318{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006319 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006320 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006321 struct ocfs2_extent_block *last_eb = NULL;
6322 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006323 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006324 u64 delete_blk = 0;
6325
6326 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6327
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006328 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006329 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006330 if (status < 0) {
6331 mlog_errno(status);
6332 goto bail;
6333 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006334
Mark Fashehdcd05382007-01-16 11:32:23 -08006335 /*
6336 * Each component will be touched, so we might as well journal
6337 * here to avoid having to handle errors later.
6338 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006339 status = ocfs2_journal_access_path(inode, handle, path);
6340 if (status < 0) {
6341 mlog_errno(status);
6342 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006343 }
6344
6345 if (last_eb_bh) {
6346 status = ocfs2_journal_access(handle, inode, last_eb_bh,
6347 OCFS2_JOURNAL_ACCESS_WRITE);
6348 if (status < 0) {
6349 mlog_errno(status);
6350 goto bail;
6351 }
6352
6353 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6354 }
6355
6356 el = &(fe->id2.i_list);
6357
6358 /*
6359 * Lower levels depend on this never happening, but it's best
6360 * to check it up here before changing the tree.
6361 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006362 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006363 ocfs2_error(inode->i_sb,
6364 "Inode %lu has an empty extent record, depth %u\n",
6365 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006366 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006367 goto bail;
6368 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006369
6370 spin_lock(&OCFS2_I(inode)->ip_lock);
6371 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6372 clusters_to_del;
6373 spin_unlock(&OCFS2_I(inode)->ip_lock);
6374 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006375 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006376
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006377 status = ocfs2_trim_tree(inode, path, handle, tc,
6378 clusters_to_del, &delete_blk);
6379 if (status) {
6380 mlog_errno(status);
6381 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006382 }
6383
Mark Fashehdcd05382007-01-16 11:32:23 -08006384 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006385 /* trunc to zero is a special case. */
6386 el->l_tree_depth = 0;
6387 fe->i_last_eb_blk = 0;
6388 } else if (last_eb)
6389 fe->i_last_eb_blk = last_eb->h_blkno;
6390
6391 status = ocfs2_journal_dirty(handle, fe_bh);
6392 if (status < 0) {
6393 mlog_errno(status);
6394 goto bail;
6395 }
6396
6397 if (last_eb) {
6398 /* If there will be a new last extent block, then by
6399 * definition, there cannot be any leaves to the right of
6400 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006401 last_eb->h_next_leaf_blk = 0;
6402 status = ocfs2_journal_dirty(handle, last_eb_bh);
6403 if (status < 0) {
6404 mlog_errno(status);
6405 goto bail;
6406 }
6407 }
6408
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006409 if (delete_blk) {
6410 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6411 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006412 if (status < 0) {
6413 mlog_errno(status);
6414 goto bail;
6415 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006416 }
6417 status = 0;
6418bail:
Mark Fashehdcd05382007-01-16 11:32:23 -08006419
Mark Fashehccd979b2005-12-15 14:31:24 -08006420 mlog_exit(status);
6421 return status;
6422}
6423
Joel Becker2b4e30f2008-09-03 20:03:41 -07006424static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006425{
6426 set_buffer_uptodate(bh);
6427 mark_buffer_dirty(bh);
6428 return 0;
6429}
6430
Mark Fasheh1d410a62007-09-07 14:20:45 -07006431static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6432 unsigned int from, unsigned int to,
6433 struct page *page, int zero, u64 *phys)
6434{
6435 int ret, partial = 0;
6436
6437 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6438 if (ret)
6439 mlog_errno(ret);
6440
6441 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006442 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006443
6444 /*
6445 * Need to set the buffers we zero'd into uptodate
6446 * here if they aren't - ocfs2_map_page_blocks()
6447 * might've skipped some
6448 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006449 ret = walk_page_buffers(handle, page_buffers(page),
6450 from, to, &partial,
6451 ocfs2_zero_func);
6452 if (ret < 0)
6453 mlog_errno(ret);
6454 else if (ocfs2_should_order_data(inode)) {
6455 ret = ocfs2_jbd2_file_inode(handle, inode);
6456#ifdef CONFIG_OCFS2_COMPAT_JBD
Mark Fasheh1d410a62007-09-07 14:20:45 -07006457 ret = walk_page_buffers(handle, page_buffers(page),
6458 from, to, &partial,
Joel Becker2b4e30f2008-09-03 20:03:41 -07006459 ocfs2_journal_dirty_data);
6460#endif
Mark Fasheh1d410a62007-09-07 14:20:45 -07006461 if (ret < 0)
6462 mlog_errno(ret);
6463 }
6464
6465 if (!partial)
6466 SetPageUptodate(page);
6467
6468 flush_dcache_page(page);
6469}
6470
Mark Fasheh35edec12007-07-06 14:41:18 -07006471static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6472 loff_t end, struct page **pages,
6473 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006474{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006475 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006476 struct page *page;
6477 unsigned int from, to = PAGE_CACHE_SIZE;
6478 struct super_block *sb = inode->i_sb;
6479
6480 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6481
6482 if (numpages == 0)
6483 goto out;
6484
Mark Fasheh35edec12007-07-06 14:41:18 -07006485 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006486 for(i = 0; i < numpages; i++) {
6487 page = pages[i];
6488
Mark Fasheh35edec12007-07-06 14:41:18 -07006489 from = start & (PAGE_CACHE_SIZE - 1);
6490 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6491 to = end & (PAGE_CACHE_SIZE - 1);
6492
Mark Fasheh60b11392007-02-16 11:46:50 -08006493 BUG_ON(from > PAGE_CACHE_SIZE);
6494 BUG_ON(to > PAGE_CACHE_SIZE);
6495
Mark Fasheh1d410a62007-09-07 14:20:45 -07006496 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6497 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006498
Mark Fasheh35edec12007-07-06 14:41:18 -07006499 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006500 }
6501out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006502 if (pages)
6503 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006504}
6505
Mark Fasheh35edec12007-07-06 14:41:18 -07006506static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006507 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006508{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006509 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006510 struct super_block *sb = inode->i_sb;
6511 struct address_space *mapping = inode->i_mapping;
6512 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006513 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006514
Mark Fasheh35edec12007-07-06 14:41:18 -07006515 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006516
Mark Fasheh35edec12007-07-06 14:41:18 -07006517 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6518 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6519
Mark Fasheh1d410a62007-09-07 14:20:45 -07006520 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006521 last_page_bytes = PAGE_ALIGN(end);
6522 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006523 do {
6524 pages[numpages] = grab_cache_page(mapping, index);
6525 if (!pages[numpages]) {
6526 ret = -ENOMEM;
6527 mlog_errno(ret);
6528 goto out;
6529 }
6530
6531 numpages++;
6532 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006533 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006534
6535out:
6536 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006537 if (pages)
6538 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006539 numpages = 0;
6540 }
6541
6542 *num = numpages;
6543
6544 return ret;
6545}
6546
6547/*
6548 * Zero the area past i_size but still within an allocated
6549 * cluster. This avoids exposing nonzero data on subsequent file
6550 * extends.
6551 *
6552 * We need to call this before i_size is updated on the inode because
6553 * otherwise block_write_full_page() will skip writeout of pages past
6554 * i_size. The new_i_size parameter is passed for this reason.
6555 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006556int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6557 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006558{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006559 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006560 struct page **pages = NULL;
6561 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006562 unsigned int ext_flags;
6563 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006564
6565 /*
6566 * File systems which don't support sparse files zero on every
6567 * extend.
6568 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07006569 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08006570 return 0;
6571
Mark Fasheh1d410a62007-09-07 14:20:45 -07006572 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08006573 sizeof(struct page *), GFP_NOFS);
6574 if (pages == NULL) {
6575 ret = -ENOMEM;
6576 mlog_errno(ret);
6577 goto out;
6578 }
6579
Mark Fasheh1d410a62007-09-07 14:20:45 -07006580 if (range_start == range_end)
6581 goto out;
6582
6583 ret = ocfs2_extent_map_get_blocks(inode,
6584 range_start >> sb->s_blocksize_bits,
6585 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08006586 if (ret) {
6587 mlog_errno(ret);
6588 goto out;
6589 }
6590
Mark Fasheh1d410a62007-09-07 14:20:45 -07006591 /*
6592 * Tail is a hole, or is marked unwritten. In either case, we
6593 * can count on read and write to return/push zero's.
6594 */
6595 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08006596 goto out;
6597
Mark Fasheh1d410a62007-09-07 14:20:45 -07006598 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6599 &numpages);
6600 if (ret) {
6601 mlog_errno(ret);
6602 goto out;
6603 }
6604
Mark Fasheh35edec12007-07-06 14:41:18 -07006605 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6606 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08006607
6608 /*
6609 * Initiate writeout of the pages we zero'd here. We don't
6610 * wait on them - the truncate_inode_pages() call later will
6611 * do that for us.
6612 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006613 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6614 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08006615 if (ret)
6616 mlog_errno(ret);
6617
6618out:
6619 if (pages)
6620 kfree(pages);
6621
6622 return ret;
6623}
6624
Tiger Yangfdd77702008-08-18 17:08:55 +08006625static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6626 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006627{
6628 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08006629 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006630
Tiger Yangfdd77702008-08-18 17:08:55 +08006631 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6632 memset(&di->id2, 0, blocksize -
6633 offsetof(struct ocfs2_dinode, id2) -
6634 xattrsize);
6635 else
6636 memset(&di->id2, 0, blocksize -
6637 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006638}
6639
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006640void ocfs2_dinode_new_extent_list(struct inode *inode,
6641 struct ocfs2_dinode *di)
6642{
Tiger Yangfdd77702008-08-18 17:08:55 +08006643 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006644 di->id2.i_list.l_tree_depth = 0;
6645 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08006646 di->id2.i_list.l_count = cpu_to_le16(
6647 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006648}
6649
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006650void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6651{
6652 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6653 struct ocfs2_inline_data *idata = &di->id2.i_data;
6654
6655 spin_lock(&oi->ip_lock);
6656 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6657 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6658 spin_unlock(&oi->ip_lock);
6659
6660 /*
6661 * We clear the entire i_data structure here so that all
6662 * fields can be properly initialized.
6663 */
Tiger Yangfdd77702008-08-18 17:08:55 +08006664 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006665
Tiger Yangfdd77702008-08-18 17:08:55 +08006666 idata->id_count = cpu_to_le16(
6667 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006668}
6669
6670int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6671 struct buffer_head *di_bh)
6672{
6673 int ret, i, has_data, num_pages = 0;
6674 handle_t *handle;
6675 u64 uninitialized_var(block);
6676 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6677 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6678 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006679 struct ocfs2_alloc_context *data_ac = NULL;
6680 struct page **pages = NULL;
6681 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07006682 struct ocfs2_extent_tree et;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006683
6684 has_data = i_size_read(inode) ? 1 : 0;
6685
6686 if (has_data) {
6687 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6688 sizeof(struct page *), GFP_NOFS);
6689 if (pages == NULL) {
6690 ret = -ENOMEM;
6691 mlog_errno(ret);
6692 goto out;
6693 }
6694
6695 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6696 if (ret) {
6697 mlog_errno(ret);
6698 goto out;
6699 }
6700 }
6701
6702 handle = ocfs2_start_trans(osb, OCFS2_INLINE_TO_EXTENTS_CREDITS);
6703 if (IS_ERR(handle)) {
6704 ret = PTR_ERR(handle);
6705 mlog_errno(ret);
6706 goto out_unlock;
6707 }
6708
6709 ret = ocfs2_journal_access(handle, inode, di_bh,
6710 OCFS2_JOURNAL_ACCESS_WRITE);
6711 if (ret) {
6712 mlog_errno(ret);
6713 goto out_commit;
6714 }
6715
6716 if (has_data) {
6717 u32 bit_off, num;
6718 unsigned int page_end;
6719 u64 phys;
6720
6721 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
6722 &num);
6723 if (ret) {
6724 mlog_errno(ret);
6725 goto out_commit;
6726 }
6727
6728 /*
6729 * Save two copies, one for insert, and one that can
6730 * be changed by ocfs2_map_and_dirty_page() below.
6731 */
6732 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6733
6734 /*
6735 * Non sparse file systems zero on extend, so no need
6736 * to do that now.
6737 */
6738 if (!ocfs2_sparse_alloc(osb) &&
6739 PAGE_CACHE_SIZE < osb->s_clustersize)
6740 end = PAGE_CACHE_SIZE;
6741
6742 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6743 if (ret) {
6744 mlog_errno(ret);
6745 goto out_commit;
6746 }
6747
6748 /*
6749 * This should populate the 1st page for us and mark
6750 * it up to date.
6751 */
6752 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6753 if (ret) {
6754 mlog_errno(ret);
6755 goto out_commit;
6756 }
6757
6758 page_end = PAGE_CACHE_SIZE;
6759 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6760 page_end = osb->s_clustersize;
6761
6762 for (i = 0; i < num_pages; i++)
6763 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6764 pages[i], i > 0, &phys);
6765 }
6766
6767 spin_lock(&oi->ip_lock);
6768 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6769 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6770 spin_unlock(&oi->ip_lock);
6771
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006772 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006773
6774 ocfs2_journal_dirty(handle, di_bh);
6775
6776 if (has_data) {
6777 /*
6778 * An error at this point should be extremely rare. If
6779 * this proves to be false, we could always re-build
6780 * the in-inode data from our pages.
6781 */
Joel Becker8d6220d2008-08-22 12:46:09 -07006782 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07006783 ret = ocfs2_insert_extent(osb, handle, inode, &et,
6784 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006785 if (ret) {
6786 mlog_errno(ret);
6787 goto out_commit;
6788 }
6789
6790 inode->i_blocks = ocfs2_inode_sector_count(inode);
6791 }
6792
6793out_commit:
6794 ocfs2_commit_trans(osb, handle);
6795
6796out_unlock:
6797 if (data_ac)
6798 ocfs2_free_alloc_context(data_ac);
6799
6800out:
6801 if (pages) {
6802 ocfs2_unlock_and_free_pages(pages, num_pages);
6803 kfree(pages);
6804 }
6805
6806 return ret;
6807}
6808
Mark Fashehccd979b2005-12-15 14:31:24 -08006809/*
6810 * It is expected, that by the time you call this function,
6811 * inode->i_size and fe->i_size have been adjusted.
6812 *
6813 * WARNING: This will kfree the truncate context
6814 */
6815int ocfs2_commit_truncate(struct ocfs2_super *osb,
6816 struct inode *inode,
6817 struct buffer_head *fe_bh,
6818 struct ocfs2_truncate_context *tc)
6819{
6820 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006821 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08006822 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006823 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006824 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08006825 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08006826 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08006827
6828 mlog_entry_void();
6829
Mark Fashehdcd05382007-01-16 11:32:23 -08006830 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08006831 i_size_read(inode));
6832
Tao Mae7d4cb62008-08-18 17:38:44 +08006833 path = ocfs2_new_path(fe_bh, &di->id2.i_list);
Mark Fashehdcd05382007-01-16 11:32:23 -08006834 if (!path) {
6835 status = -ENOMEM;
6836 mlog_errno(status);
6837 goto bail;
6838 }
Mark Fasheh83418972007-04-23 18:53:12 -07006839
6840 ocfs2_extent_map_trunc(inode, new_highest_cpos);
6841
Mark Fashehccd979b2005-12-15 14:31:24 -08006842start:
Mark Fashehdcd05382007-01-16 11:32:23 -08006843 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006844 * Check that we still have allocation to delete.
6845 */
6846 if (OCFS2_I(inode)->ip_clusters == 0) {
6847 status = 0;
6848 goto bail;
6849 }
6850
6851 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08006852 * Truncate always works against the rightmost tree branch.
6853 */
6854 status = ocfs2_find_path(inode, path, UINT_MAX);
6855 if (status) {
6856 mlog_errno(status);
6857 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006858 }
6859
Mark Fashehdcd05382007-01-16 11:32:23 -08006860 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
6861 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
6862
6863 /*
6864 * By now, el will point to the extent list on the bottom most
6865 * portion of this tree. Only the tail record is considered in
6866 * each pass.
6867 *
6868 * We handle the following cases, in order:
6869 * - empty extent: delete the remaining branch
6870 * - remove the entire record
6871 * - remove a partial record
6872 * - no record needs to be removed (truncate has completed)
6873 */
6874 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006875 if (le16_to_cpu(el->l_next_free_rec) == 0) {
6876 ocfs2_error(inode->i_sb,
6877 "Inode %llu has empty extent block at %llu\n",
6878 (unsigned long long)OCFS2_I(inode)->ip_blkno,
6879 (unsigned long long)path_leaf_bh(path)->b_blocknr);
6880 status = -EROFS;
6881 goto bail;
6882 }
6883
Mark Fashehccd979b2005-12-15 14:31:24 -08006884 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08006885 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08006886 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08006887 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
6888 clusters_to_del = 0;
6889 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08006890 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08006891 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08006892 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08006893 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08006894 new_highest_cpos;
6895 } else {
6896 status = 0;
6897 goto bail;
6898 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006899
Mark Fashehdcd05382007-01-16 11:32:23 -08006900 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
6901 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
6902
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006903 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006904 tl_sem = 1;
6905 /* ocfs2_truncate_log_needs_flush guarantees us at least one
6906 * record is free for use. If there isn't any, we flush to get
6907 * an empty truncate log. */
6908 if (ocfs2_truncate_log_needs_flush(osb)) {
6909 status = __ocfs2_flush_truncate_log(osb);
6910 if (status < 0) {
6911 mlog_errno(status);
6912 goto bail;
6913 }
6914 }
6915
6916 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006917 (struct ocfs2_dinode *)fe_bh->b_data,
6918 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006919 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08006920 if (IS_ERR(handle)) {
6921 status = PTR_ERR(handle);
6922 handle = NULL;
6923 mlog_errno(status);
6924 goto bail;
6925 }
6926
Mark Fashehdcd05382007-01-16 11:32:23 -08006927 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
6928 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006929 if (status < 0) {
6930 mlog_errno(status);
6931 goto bail;
6932 }
6933
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006934 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006935 tl_sem = 0;
6936
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006937 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006938 handle = NULL;
6939
Mark Fashehdcd05382007-01-16 11:32:23 -08006940 ocfs2_reinit_path(path, 1);
6941
6942 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006943 * The check above will catch the case where we've truncated
6944 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08006945 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006946 goto start;
6947
Mark Fashehccd979b2005-12-15 14:31:24 -08006948bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08006949
6950 ocfs2_schedule_truncate_log_flush(osb, 1);
6951
6952 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006953 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006954
6955 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006956 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006957
Mark Fasheh59a5e412007-06-22 15:52:36 -07006958 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
6959
Mark Fashehdcd05382007-01-16 11:32:23 -08006960 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006961
6962 /* This will drop the ext_alloc cluster lock for us */
6963 ocfs2_free_truncate_context(tc);
6964
6965 mlog_exit(status);
6966 return status;
6967}
6968
Mark Fashehccd979b2005-12-15 14:31:24 -08006969/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07006970 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08006971 */
6972int ocfs2_prepare_truncate(struct ocfs2_super *osb,
6973 struct inode *inode,
6974 struct buffer_head *fe_bh,
6975 struct ocfs2_truncate_context **tc)
6976{
Mark Fasheh59a5e412007-06-22 15:52:36 -07006977 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006978 unsigned int new_i_clusters;
6979 struct ocfs2_dinode *fe;
6980 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08006981 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006982
6983 mlog_entry_void();
6984
6985 *tc = NULL;
6986
6987 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
6988 i_size_read(inode));
6989 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6990
6991 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006992 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
6993 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08006994
Robert P. J. Daycd861282006-12-13 00:34:52 -08006995 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08006996 if (!(*tc)) {
6997 status = -ENOMEM;
6998 mlog_errno(status);
6999 goto bail;
7000 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007001 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007002
Mark Fashehccd979b2005-12-15 14:31:24 -08007003 if (fe->id2.i_list.l_tree_depth) {
Mark Fashehccd979b2005-12-15 14:31:24 -08007004 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
7005 &last_eb_bh, OCFS2_BH_CACHED, inode);
7006 if (status < 0) {
7007 mlog_errno(status);
7008 goto bail;
7009 }
7010 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7011 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
7012 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
7013
7014 brelse(last_eb_bh);
7015 status = -EIO;
7016 goto bail;
7017 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007018 }
7019
7020 (*tc)->tc_last_eb_bh = last_eb_bh;
7021
Mark Fashehccd979b2005-12-15 14:31:24 -08007022 status = 0;
7023bail:
7024 if (status < 0) {
7025 if (*tc)
7026 ocfs2_free_truncate_context(*tc);
7027 *tc = NULL;
7028 }
7029 mlog_exit_void();
7030 return status;
7031}
7032
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007033/*
7034 * 'start' is inclusive, 'end' is not.
7035 */
7036int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7037 unsigned int start, unsigned int end, int trunc)
7038{
7039 int ret;
7040 unsigned int numbytes;
7041 handle_t *handle;
7042 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7043 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7044 struct ocfs2_inline_data *idata = &di->id2.i_data;
7045
7046 if (end > i_size_read(inode))
7047 end = i_size_read(inode);
7048
7049 BUG_ON(start >= end);
7050
7051 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7052 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7053 !ocfs2_supports_inline_data(osb)) {
7054 ocfs2_error(inode->i_sb,
7055 "Inline data flags for inode %llu don't agree! "
7056 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7057 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7058 le16_to_cpu(di->i_dyn_features),
7059 OCFS2_I(inode)->ip_dyn_features,
7060 osb->s_feature_incompat);
7061 ret = -EROFS;
7062 goto out;
7063 }
7064
7065 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7066 if (IS_ERR(handle)) {
7067 ret = PTR_ERR(handle);
7068 mlog_errno(ret);
7069 goto out;
7070 }
7071
7072 ret = ocfs2_journal_access(handle, inode, di_bh,
7073 OCFS2_JOURNAL_ACCESS_WRITE);
7074 if (ret) {
7075 mlog_errno(ret);
7076 goto out_commit;
7077 }
7078
7079 numbytes = end - start;
7080 memset(idata->id_data + start, 0, numbytes);
7081
7082 /*
7083 * No need to worry about the data page here - it's been
7084 * truncated already and inline data doesn't need it for
7085 * pushing zero's to disk, so we'll let readpage pick it up
7086 * later.
7087 */
7088 if (trunc) {
7089 i_size_write(inode, start);
7090 di->i_size = cpu_to_le64(start);
7091 }
7092
7093 inode->i_blocks = ocfs2_inode_sector_count(inode);
7094 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7095
7096 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7097 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7098
7099 ocfs2_journal_dirty(handle, di_bh);
7100
7101out_commit:
7102 ocfs2_commit_trans(osb, handle);
7103
7104out:
7105 return ret;
7106}
7107
Mark Fashehccd979b2005-12-15 14:31:24 -08007108static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7109{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007110 /*
7111 * The caller is responsible for completing deallocation
7112 * before freeing the context.
7113 */
7114 if (tc->tc_dealloc.c_first_suballocator != NULL)
7115 mlog(ML_NOTICE,
7116 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007117
7118 if (tc->tc_last_eb_bh)
7119 brelse(tc->tc_last_eb_bh);
7120
7121 kfree(tc);
7122}