blob: 052c4cf7db954e3fc57146f35a8dd8201e238b73 [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:
Mark Fasheha81cb882008-10-07 14:25:16 -0700722 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800723
724 mlog_exit(retval);
725 return retval;
726}
727
728/* expects array to already be allocated
729 *
730 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
731 * l_count for you
732 */
733static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700734 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800735 struct inode *inode,
736 int wanted,
737 struct ocfs2_alloc_context *meta_ac,
738 struct buffer_head *bhs[])
739{
740 int count, status, i;
741 u16 suballoc_bit_start;
742 u32 num_got;
743 u64 first_blkno;
744 struct ocfs2_extent_block *eb;
745
746 mlog_entry_void();
747
748 count = 0;
749 while (count < wanted) {
750 status = ocfs2_claim_metadata(osb,
751 handle,
752 meta_ac,
753 wanted - count,
754 &suballoc_bit_start,
755 &num_got,
756 &first_blkno);
757 if (status < 0) {
758 mlog_errno(status);
759 goto bail;
760 }
761
762 for(i = count; i < (num_got + count); i++) {
763 bhs[i] = sb_getblk(osb->sb, first_blkno);
764 if (bhs[i] == NULL) {
765 status = -EIO;
766 mlog_errno(status);
767 goto bail;
768 }
769 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
770
771 status = ocfs2_journal_access(handle, inode, bhs[i],
772 OCFS2_JOURNAL_ACCESS_CREATE);
773 if (status < 0) {
774 mlog_errno(status);
775 goto bail;
776 }
777
778 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
779 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
780 /* Ok, setup the minimal stuff here. */
781 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
782 eb->h_blkno = cpu_to_le64(first_blkno);
783 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800784 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800785 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
786 eb->h_list.l_count =
787 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
788
789 suballoc_bit_start++;
790 first_blkno++;
791
792 /* We'll also be dirtied by the caller, so
793 * this isn't absolutely necessary. */
794 status = ocfs2_journal_dirty(handle, bhs[i]);
795 if (status < 0) {
796 mlog_errno(status);
797 goto bail;
798 }
799 }
800
801 count += num_got;
802 }
803
804 status = 0;
805bail:
806 if (status < 0) {
807 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -0700808 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800809 bhs[i] = NULL;
810 }
811 }
812 mlog_exit(status);
813 return status;
814}
815
816/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800817 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
818 *
819 * Returns the sum of the rightmost extent rec logical offset and
820 * cluster count.
821 *
822 * ocfs2_add_branch() uses this to determine what logical cluster
823 * value should be populated into the leftmost new branch records.
824 *
825 * ocfs2_shift_tree_depth() uses this to determine the # clusters
826 * value for the new topmost tree record.
827 */
828static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
829{
830 int i;
831
832 i = le16_to_cpu(el->l_next_free_rec) - 1;
833
834 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -0800835 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -0800836}
837
838/*
Mark Fashehccd979b2005-12-15 14:31:24 -0800839 * Add an entire tree branch to our inode. eb_bh is the extent block
840 * to start at, if we don't want to start the branch at the dinode
841 * structure.
842 *
843 * last_eb_bh is required as we have to update it's next_leaf pointer
844 * for the new last extent block.
845 *
846 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -0800847 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -0800848 */
849static int ocfs2_add_branch(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700850 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800851 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +0800852 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -0800853 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -0700854 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800855 struct ocfs2_alloc_context *meta_ac)
856{
857 int status, new_blocks, i;
858 u64 next_blkno, new_last_eb_blk;
859 struct buffer_head *bh;
860 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800861 struct ocfs2_extent_block *eb;
862 struct ocfs2_extent_list *eb_el;
863 struct ocfs2_extent_list *el;
Mark Fashehdcd05382007-01-16 11:32:23 -0800864 u32 new_cpos;
Mark Fashehccd979b2005-12-15 14:31:24 -0800865
866 mlog_entry_void();
867
Mark Fasheh328d5752007-06-18 10:48:04 -0700868 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800869
Mark Fashehccd979b2005-12-15 14:31:24 -0800870 if (eb_bh) {
871 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
872 el = &eb->h_list;
873 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700874 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -0800875
876 /* we never add a branch to a leaf. */
877 BUG_ON(!el->l_tree_depth);
878
879 new_blocks = le16_to_cpu(el->l_tree_depth);
880
881 /* allocate the number of new eb blocks we need */
882 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
883 GFP_KERNEL);
884 if (!new_eb_bhs) {
885 status = -ENOMEM;
886 mlog_errno(status);
887 goto bail;
888 }
889
890 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
891 meta_ac, new_eb_bhs);
892 if (status < 0) {
893 mlog_errno(status);
894 goto bail;
895 }
896
Mark Fasheh328d5752007-06-18 10:48:04 -0700897 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
Mark Fashehdcd05382007-01-16 11:32:23 -0800898 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
899
Mark Fashehccd979b2005-12-15 14:31:24 -0800900 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
901 * linked with the rest of the tree.
902 * conversly, new_eb_bhs[0] is the new bottommost leaf.
903 *
904 * when we leave the loop, new_last_eb_blk will point to the
905 * newest leaf, and next_blkno will point to the topmost extent
906 * block. */
907 next_blkno = new_last_eb_blk = 0;
908 for(i = 0; i < new_blocks; i++) {
909 bh = new_eb_bhs[i];
910 eb = (struct ocfs2_extent_block *) bh->b_data;
911 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
912 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
913 status = -EIO;
914 goto bail;
915 }
916 eb_el = &eb->h_list;
917
918 status = ocfs2_journal_access(handle, inode, bh,
919 OCFS2_JOURNAL_ACCESS_CREATE);
920 if (status < 0) {
921 mlog_errno(status);
922 goto bail;
923 }
924
925 eb->h_next_leaf_blk = 0;
926 eb_el->l_tree_depth = cpu_to_le16(i);
927 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -0800928 /*
929 * This actually counts as an empty extent as
930 * c_clusters == 0
931 */
932 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -0800933 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -0800934 /*
935 * eb_el isn't always an interior node, but even leaf
936 * nodes want a zero'd flags and reserved field so
937 * this gets the whole 32 bits regardless of use.
938 */
939 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800940 if (!eb_el->l_tree_depth)
941 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
942
943 status = ocfs2_journal_dirty(handle, bh);
944 if (status < 0) {
945 mlog_errno(status);
946 goto bail;
947 }
948
949 next_blkno = le64_to_cpu(eb->h_blkno);
950 }
951
952 /* This is a bit hairy. We want to update up to three blocks
953 * here without leaving any of them in an inconsistent state
954 * in case of error. We don't have to worry about
955 * journal_dirty erroring as it won't unless we've aborted the
956 * handle (in which case we would never be here) so reserving
957 * the write with journal_access is all we need to do. */
Mark Fasheh328d5752007-06-18 10:48:04 -0700958 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800959 OCFS2_JOURNAL_ACCESS_WRITE);
960 if (status < 0) {
961 mlog_errno(status);
962 goto bail;
963 }
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700964 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800965 OCFS2_JOURNAL_ACCESS_WRITE);
966 if (status < 0) {
967 mlog_errno(status);
968 goto bail;
969 }
970 if (eb_bh) {
971 status = ocfs2_journal_access(handle, inode, eb_bh,
972 OCFS2_JOURNAL_ACCESS_WRITE);
973 if (status < 0) {
974 mlog_errno(status);
975 goto bail;
976 }
977 }
978
979 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +0800980 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800981 i = le16_to_cpu(el->l_next_free_rec);
982 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -0800983 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -0800984 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800985 le16_add_cpu(&el->l_next_free_rec, 1);
986
987 /* fe needs a new last extent block pointer, as does the
988 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -0700989 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -0800990
Mark Fasheh328d5752007-06-18 10:48:04 -0700991 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800992 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
993
Mark Fasheh328d5752007-06-18 10:48:04 -0700994 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800995 if (status < 0)
996 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700997 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800998 if (status < 0)
999 mlog_errno(status);
1000 if (eb_bh) {
1001 status = ocfs2_journal_dirty(handle, eb_bh);
1002 if (status < 0)
1003 mlog_errno(status);
1004 }
1005
Mark Fasheh328d5752007-06-18 10:48:04 -07001006 /*
1007 * Some callers want to track the rightmost leaf so pass it
1008 * back here.
1009 */
1010 brelse(*last_eb_bh);
1011 get_bh(new_eb_bhs[0]);
1012 *last_eb_bh = new_eb_bhs[0];
1013
Mark Fashehccd979b2005-12-15 14:31:24 -08001014 status = 0;
1015bail:
1016 if (new_eb_bhs) {
1017 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001018 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001019 kfree(new_eb_bhs);
1020 }
1021
1022 mlog_exit(status);
1023 return status;
1024}
1025
1026/*
1027 * adds another level to the allocation tree.
1028 * returns back the new extent block so you can add a branch to it
1029 * after this call.
1030 */
1031static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001032 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001033 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001034 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001035 struct ocfs2_alloc_context *meta_ac,
1036 struct buffer_head **ret_new_eb_bh)
1037{
1038 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001039 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001040 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001041 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001042 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001043 struct ocfs2_extent_list *eb_el;
1044
1045 mlog_entry_void();
1046
1047 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
1048 &new_eb_bh);
1049 if (status < 0) {
1050 mlog_errno(status);
1051 goto bail;
1052 }
1053
1054 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1055 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1056 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1057 status = -EIO;
1058 goto bail;
1059 }
1060
1061 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001062 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001063
1064 status = ocfs2_journal_access(handle, inode, new_eb_bh,
1065 OCFS2_JOURNAL_ACCESS_CREATE);
1066 if (status < 0) {
1067 mlog_errno(status);
1068 goto bail;
1069 }
1070
Tao Mae7d4cb62008-08-18 17:38:44 +08001071 /* copy the root extent list data into the new extent block */
1072 eb_el->l_tree_depth = root_el->l_tree_depth;
1073 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1074 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1075 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001076
1077 status = ocfs2_journal_dirty(handle, new_eb_bh);
1078 if (status < 0) {
1079 mlog_errno(status);
1080 goto bail;
1081 }
1082
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001083 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001084 OCFS2_JOURNAL_ACCESS_WRITE);
1085 if (status < 0) {
1086 mlog_errno(status);
1087 goto bail;
1088 }
1089
Mark Fashehdcd05382007-01-16 11:32:23 -08001090 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1091
Tao Mae7d4cb62008-08-18 17:38:44 +08001092 /* update root_bh now */
1093 le16_add_cpu(&root_el->l_tree_depth, 1);
1094 root_el->l_recs[0].e_cpos = 0;
1095 root_el->l_recs[0].e_blkno = eb->h_blkno;
1096 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1097 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1098 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1099 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001100
1101 /* If this is our 1st tree depth shift, then last_eb_blk
1102 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001103 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001104 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001105
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001106 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001107 if (status < 0) {
1108 mlog_errno(status);
1109 goto bail;
1110 }
1111
1112 *ret_new_eb_bh = new_eb_bh;
1113 new_eb_bh = NULL;
1114 status = 0;
1115bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001116 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001117
1118 mlog_exit(status);
1119 return status;
1120}
1121
1122/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001123 * Should only be called when there is no space left in any of the
1124 * leaf nodes. What we want to do is find the lowest tree depth
1125 * non-leaf extent block with room for new records. There are three
1126 * valid results of this search:
1127 *
1128 * 1) a lowest extent block is found, then we pass it back in
1129 * *lowest_eb_bh and return '0'
1130 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001131 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001132 * pass NULL back in *lowest_eb_bh, but still return '0'
1133 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001134 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001135 * which case we return > 0
1136 *
1137 * return status < 0 indicates an error.
1138 */
1139static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1140 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001141 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001142 struct buffer_head **target_bh)
1143{
1144 int status = 0, i;
1145 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001146 struct ocfs2_extent_block *eb;
1147 struct ocfs2_extent_list *el;
1148 struct buffer_head *bh = NULL;
1149 struct buffer_head *lowest_bh = NULL;
1150
1151 mlog_entry_void();
1152
1153 *target_bh = NULL;
1154
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001155 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001156
1157 while(le16_to_cpu(el->l_tree_depth) > 1) {
1158 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Mark Fashehb06970532006-03-03 10:24:33 -08001159 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001160 "extent list (next_free_rec == 0)",
Mark Fashehb06970532006-03-03 10:24:33 -08001161 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001162 status = -EIO;
1163 goto bail;
1164 }
1165 i = le16_to_cpu(el->l_next_free_rec) - 1;
1166 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1167 if (!blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -08001168 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001169 "list where extent # %d has no physical "
1170 "block start",
Mark Fashehb06970532006-03-03 10:24:33 -08001171 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001172 status = -EIO;
1173 goto bail;
1174 }
1175
Mark Fasheha81cb882008-10-07 14:25:16 -07001176 brelse(bh);
1177 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001178
1179 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
1180 inode);
1181 if (status < 0) {
1182 mlog_errno(status);
1183 goto bail;
1184 }
1185
1186 eb = (struct ocfs2_extent_block *) bh->b_data;
1187 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1188 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1189 status = -EIO;
1190 goto bail;
1191 }
1192 el = &eb->h_list;
1193
1194 if (le16_to_cpu(el->l_next_free_rec) <
1195 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001196 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001197 lowest_bh = bh;
1198 get_bh(lowest_bh);
1199 }
1200 }
1201
1202 /* If we didn't find one and the fe doesn't have any room,
1203 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001204 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001205 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001206 status = 1;
1207
1208 *target_bh = lowest_bh;
1209bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001210 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001211
1212 mlog_exit(status);
1213 return status;
1214}
1215
Mark Fashehe48edee2007-03-07 16:46:57 -08001216/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001217 * Grow a b-tree so that it has more records.
1218 *
1219 * We might shift the tree depth in which case existing paths should
1220 * be considered invalid.
1221 *
1222 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001223 *
1224 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001225 */
1226static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001227 struct ocfs2_extent_tree *et, int *final_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07001228 struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001229 struct ocfs2_alloc_context *meta_ac)
1230{
1231 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001232 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001233 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001234 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1235 struct buffer_head *bh = NULL;
1236
1237 BUG_ON(meta_ac == NULL);
1238
Tao Mae7d4cb62008-08-18 17:38:44 +08001239 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001240 if (shift < 0) {
1241 ret = shift;
1242 mlog_errno(ret);
1243 goto out;
1244 }
1245
1246 /* We traveled all the way to the bottom of the allocation tree
1247 * and didn't find room for any more extents - we need to add
1248 * another tree level */
1249 if (shift) {
1250 BUG_ON(bh);
1251 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1252
1253 /* ocfs2_shift_tree_depth will return us a buffer with
1254 * the new extent block (so we can pass that to
1255 * ocfs2_add_branch). */
Tao Mae7d4cb62008-08-18 17:38:44 +08001256 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001257 meta_ac, &bh);
1258 if (ret < 0) {
1259 mlog_errno(ret);
1260 goto out;
1261 }
1262 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001263 if (depth == 1) {
1264 /*
1265 * Special case: we have room now if we shifted from
1266 * tree_depth 0, so no more work needs to be done.
1267 *
1268 * We won't be calling add_branch, so pass
1269 * back *last_eb_bh as the new leaf. At depth
1270 * zero, it should always be null so there's
1271 * no reason to brelse.
1272 */
1273 BUG_ON(*last_eb_bh);
1274 get_bh(bh);
1275 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001276 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001277 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001278 }
1279
1280 /* call ocfs2_add_branch to add the final part of the tree with
1281 * the new data. */
1282 mlog(0, "add branch. bh = %p\n", bh);
Tao Mae7d4cb62008-08-18 17:38:44 +08001283 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001284 meta_ac);
1285 if (ret < 0) {
1286 mlog_errno(ret);
1287 goto out;
1288 }
1289
1290out:
1291 if (final_depth)
1292 *final_depth = depth;
1293 brelse(bh);
1294 return ret;
1295}
1296
1297/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001298 * This function will discard the rightmost extent record.
1299 */
1300static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1301{
1302 int next_free = le16_to_cpu(el->l_next_free_rec);
1303 int count = le16_to_cpu(el->l_count);
1304 unsigned int num_bytes;
1305
1306 BUG_ON(!next_free);
1307 /* This will cause us to go off the end of our extent list. */
1308 BUG_ON(next_free >= count);
1309
1310 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1311
1312 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1313}
1314
1315static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1316 struct ocfs2_extent_rec *insert_rec)
1317{
1318 int i, insert_index, next_free, has_empty, num_bytes;
1319 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1320 struct ocfs2_extent_rec *rec;
1321
1322 next_free = le16_to_cpu(el->l_next_free_rec);
1323 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1324
1325 BUG_ON(!next_free);
1326
1327 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001328 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001329
1330 /*
1331 * The easiest way to approach this is to just remove the
1332 * empty extent and temporarily decrement next_free.
1333 */
1334 if (has_empty) {
1335 /*
1336 * If next_free was 1 (only an empty extent), this
1337 * loop won't execute, which is fine. We still want
1338 * the decrement above to happen.
1339 */
1340 for(i = 0; i < (next_free - 1); i++)
1341 el->l_recs[i] = el->l_recs[i+1];
1342
1343 next_free--;
1344 }
1345
1346 /*
1347 * Figure out what the new record index should be.
1348 */
1349 for(i = 0; i < next_free; i++) {
1350 rec = &el->l_recs[i];
1351
1352 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1353 break;
1354 }
1355 insert_index = i;
1356
1357 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1358 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1359
1360 BUG_ON(insert_index < 0);
1361 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1362 BUG_ON(insert_index > next_free);
1363
1364 /*
1365 * No need to memmove if we're just adding to the tail.
1366 */
1367 if (insert_index != next_free) {
1368 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1369
1370 num_bytes = next_free - insert_index;
1371 num_bytes *= sizeof(struct ocfs2_extent_rec);
1372 memmove(&el->l_recs[insert_index + 1],
1373 &el->l_recs[insert_index],
1374 num_bytes);
1375 }
1376
1377 /*
1378 * Either we had an empty extent, and need to re-increment or
1379 * there was no empty extent on a non full rightmost leaf node,
1380 * in which case we still need to increment.
1381 */
1382 next_free++;
1383 el->l_next_free_rec = cpu_to_le16(next_free);
1384 /*
1385 * Make sure none of the math above just messed up our tree.
1386 */
1387 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1388
1389 el->l_recs[insert_index] = *insert_rec;
1390
1391}
1392
Mark Fasheh328d5752007-06-18 10:48:04 -07001393static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1394{
1395 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1396
1397 BUG_ON(num_recs == 0);
1398
1399 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1400 num_recs--;
1401 size = num_recs * sizeof(struct ocfs2_extent_rec);
1402 memmove(&el->l_recs[0], &el->l_recs[1], size);
1403 memset(&el->l_recs[num_recs], 0,
1404 sizeof(struct ocfs2_extent_rec));
1405 el->l_next_free_rec = cpu_to_le16(num_recs);
1406 }
1407}
1408
Mark Fashehdcd05382007-01-16 11:32:23 -08001409/*
1410 * Create an empty extent record .
1411 *
1412 * l_next_free_rec may be updated.
1413 *
1414 * If an empty extent already exists do nothing.
1415 */
1416static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1417{
1418 int next_free = le16_to_cpu(el->l_next_free_rec);
1419
Mark Fashehe48edee2007-03-07 16:46:57 -08001420 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1421
Mark Fashehdcd05382007-01-16 11:32:23 -08001422 if (next_free == 0)
1423 goto set_and_inc;
1424
1425 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1426 return;
1427
1428 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1429 "Asked to create an empty extent in a full list:\n"
1430 "count = %u, tree depth = %u",
1431 le16_to_cpu(el->l_count),
1432 le16_to_cpu(el->l_tree_depth));
1433
1434 ocfs2_shift_records_right(el);
1435
1436set_and_inc:
1437 le16_add_cpu(&el->l_next_free_rec, 1);
1438 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1439}
1440
1441/*
1442 * For a rotation which involves two leaf nodes, the "root node" is
1443 * the lowest level tree node which contains a path to both leafs. This
1444 * resulting set of information can be used to form a complete "subtree"
1445 *
1446 * This function is passed two full paths from the dinode down to a
1447 * pair of adjacent leaves. It's task is to figure out which path
1448 * index contains the subtree root - this can be the root index itself
1449 * in a worst-case rotation.
1450 *
1451 * The array index of the subtree root is passed back.
1452 */
1453static int ocfs2_find_subtree_root(struct inode *inode,
1454 struct ocfs2_path *left,
1455 struct ocfs2_path *right)
1456{
1457 int i = 0;
1458
1459 /*
1460 * Check that the caller passed in two paths from the same tree.
1461 */
1462 BUG_ON(path_root_bh(left) != path_root_bh(right));
1463
1464 do {
1465 i++;
1466
1467 /*
1468 * The caller didn't pass two adjacent paths.
1469 */
1470 mlog_bug_on_msg(i > left->p_tree_depth,
1471 "Inode %lu, left depth %u, right depth %u\n"
1472 "left leaf blk %llu, right leaf blk %llu\n",
1473 inode->i_ino, left->p_tree_depth,
1474 right->p_tree_depth,
1475 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1476 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1477 } while (left->p_node[i].bh->b_blocknr ==
1478 right->p_node[i].bh->b_blocknr);
1479
1480 return i - 1;
1481}
1482
1483typedef void (path_insert_t)(void *, struct buffer_head *);
1484
1485/*
1486 * Traverse a btree path in search of cpos, starting at root_el.
1487 *
1488 * This code can be called with a cpos larger than the tree, in which
1489 * case it will return the rightmost path.
1490 */
1491static int __ocfs2_find_path(struct inode *inode,
1492 struct ocfs2_extent_list *root_el, u32 cpos,
1493 path_insert_t *func, void *data)
1494{
1495 int i, ret = 0;
1496 u32 range;
1497 u64 blkno;
1498 struct buffer_head *bh = NULL;
1499 struct ocfs2_extent_block *eb;
1500 struct ocfs2_extent_list *el;
1501 struct ocfs2_extent_rec *rec;
1502 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1503
1504 el = root_el;
1505 while (el->l_tree_depth) {
1506 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1507 ocfs2_error(inode->i_sb,
1508 "Inode %llu has empty extent list at "
1509 "depth %u\n",
1510 (unsigned long long)oi->ip_blkno,
1511 le16_to_cpu(el->l_tree_depth));
1512 ret = -EROFS;
1513 goto out;
1514
1515 }
1516
1517 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1518 rec = &el->l_recs[i];
1519
1520 /*
1521 * In the case that cpos is off the allocation
1522 * tree, this should just wind up returning the
1523 * rightmost record.
1524 */
1525 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001526 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001527 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1528 break;
1529 }
1530
1531 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1532 if (blkno == 0) {
1533 ocfs2_error(inode->i_sb,
1534 "Inode %llu has bad blkno in extent list "
1535 "at depth %u (index %d)\n",
1536 (unsigned long long)oi->ip_blkno,
1537 le16_to_cpu(el->l_tree_depth), i);
1538 ret = -EROFS;
1539 goto out;
1540 }
1541
1542 brelse(bh);
1543 bh = NULL;
1544 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1545 &bh, OCFS2_BH_CACHED, inode);
1546 if (ret) {
1547 mlog_errno(ret);
1548 goto out;
1549 }
1550
1551 eb = (struct ocfs2_extent_block *) bh->b_data;
1552 el = &eb->h_list;
1553 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1554 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1555 ret = -EIO;
1556 goto out;
1557 }
1558
1559 if (le16_to_cpu(el->l_next_free_rec) >
1560 le16_to_cpu(el->l_count)) {
1561 ocfs2_error(inode->i_sb,
1562 "Inode %llu has bad count in extent list "
1563 "at block %llu (next free=%u, count=%u)\n",
1564 (unsigned long long)oi->ip_blkno,
1565 (unsigned long long)bh->b_blocknr,
1566 le16_to_cpu(el->l_next_free_rec),
1567 le16_to_cpu(el->l_count));
1568 ret = -EROFS;
1569 goto out;
1570 }
1571
1572 if (func)
1573 func(data, bh);
1574 }
1575
1576out:
1577 /*
1578 * Catch any trailing bh that the loop didn't handle.
1579 */
1580 brelse(bh);
1581
1582 return ret;
1583}
1584
1585/*
1586 * Given an initialized path (that is, it has a valid root extent
1587 * list), this function will traverse the btree in search of the path
1588 * which would contain cpos.
1589 *
1590 * The path traveled is recorded in the path structure.
1591 *
1592 * Note that this will not do any comparisons on leaf node extent
1593 * records, so it will work fine in the case that we just added a tree
1594 * branch.
1595 */
1596struct find_path_data {
1597 int index;
1598 struct ocfs2_path *path;
1599};
1600static void find_path_ins(void *data, struct buffer_head *bh)
1601{
1602 struct find_path_data *fp = data;
1603
1604 get_bh(bh);
1605 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1606 fp->index++;
1607}
1608static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1609 u32 cpos)
1610{
1611 struct find_path_data data;
1612
1613 data.index = 1;
1614 data.path = path;
1615 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1616 find_path_ins, &data);
1617}
1618
1619static void find_leaf_ins(void *data, struct buffer_head *bh)
1620{
1621 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1622 struct ocfs2_extent_list *el = &eb->h_list;
1623 struct buffer_head **ret = data;
1624
1625 /* We want to retain only the leaf block. */
1626 if (le16_to_cpu(el->l_tree_depth) == 0) {
1627 get_bh(bh);
1628 *ret = bh;
1629 }
1630}
1631/*
1632 * Find the leaf block in the tree which would contain cpos. No
1633 * checking of the actual leaf is done.
1634 *
1635 * Some paths want to call this instead of allocating a path structure
1636 * and calling ocfs2_find_path().
1637 *
1638 * This function doesn't handle non btree extent lists.
1639 */
Mark Fasheh363041a2007-01-17 12:31:35 -08001640int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1641 u32 cpos, struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001642{
1643 int ret;
1644 struct buffer_head *bh = NULL;
1645
1646 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1647 if (ret) {
1648 mlog_errno(ret);
1649 goto out;
1650 }
1651
1652 *leaf_bh = bh;
1653out:
1654 return ret;
1655}
1656
1657/*
1658 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1659 *
1660 * Basically, we've moved stuff around at the bottom of the tree and
1661 * we need to fix up the extent records above the changes to reflect
1662 * the new changes.
1663 *
1664 * left_rec: the record on the left.
1665 * left_child_el: is the child list pointed to by left_rec
1666 * right_rec: the record to the right of left_rec
1667 * right_child_el: is the child list pointed to by right_rec
1668 *
1669 * By definition, this only works on interior nodes.
1670 */
1671static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1672 struct ocfs2_extent_list *left_child_el,
1673 struct ocfs2_extent_rec *right_rec,
1674 struct ocfs2_extent_list *right_child_el)
1675{
1676 u32 left_clusters, right_end;
1677
1678 /*
1679 * Interior nodes never have holes. Their cpos is the cpos of
1680 * the leftmost record in their child list. Their cluster
1681 * count covers the full theoretical range of their child list
1682 * - the range between their cpos and the cpos of the record
1683 * immediately to their right.
1684 */
1685 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07001686 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1687 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1688 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1689 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001690 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001691 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001692
1693 /*
1694 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001695 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001696 * updating e_cpos to keep the same highest cluster count.
1697 */
1698 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001699 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001700
1701 right_rec->e_cpos = left_rec->e_cpos;
1702 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1703
1704 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001705 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001706}
1707
1708/*
1709 * Adjust the adjacent root node records involved in a
1710 * rotation. left_el_blkno is passed in as a key so that we can easily
1711 * find it's index in the root list.
1712 */
1713static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1714 struct ocfs2_extent_list *left_el,
1715 struct ocfs2_extent_list *right_el,
1716 u64 left_el_blkno)
1717{
1718 int i;
1719
1720 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1721 le16_to_cpu(left_el->l_tree_depth));
1722
1723 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1724 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1725 break;
1726 }
1727
1728 /*
1729 * The path walking code should have never returned a root and
1730 * two paths which are not adjacent.
1731 */
1732 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1733
1734 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1735 &root_el->l_recs[i + 1], right_el);
1736}
1737
1738/*
1739 * We've changed a leaf block (in right_path) and need to reflect that
1740 * change back up the subtree.
1741 *
1742 * This happens in multiple places:
1743 * - When we've moved an extent record from the left path leaf to the right
1744 * path leaf to make room for an empty extent in the left path leaf.
1745 * - When our insert into the right path leaf is at the leftmost edge
1746 * and requires an update of the path immediately to it's left. This
1747 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08001748 * - When we've adjusted the last extent record in the left path leaf and the
1749 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08001750 */
1751static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1752 struct ocfs2_path *left_path,
1753 struct ocfs2_path *right_path,
1754 int subtree_index)
1755{
1756 int ret, i, idx;
1757 struct ocfs2_extent_list *el, *left_el, *right_el;
1758 struct ocfs2_extent_rec *left_rec, *right_rec;
1759 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1760
1761 /*
1762 * Update the counts and position values within all the
1763 * interior nodes to reflect the leaf rotation we just did.
1764 *
1765 * The root node is handled below the loop.
1766 *
1767 * We begin the loop with right_el and left_el pointing to the
1768 * leaf lists and work our way up.
1769 *
1770 * NOTE: within this loop, left_el and right_el always refer
1771 * to the *child* lists.
1772 */
1773 left_el = path_leaf_el(left_path);
1774 right_el = path_leaf_el(right_path);
1775 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1776 mlog(0, "Adjust records at index %u\n", i);
1777
1778 /*
1779 * One nice property of knowing that all of these
1780 * nodes are below the root is that we only deal with
1781 * the leftmost right node record and the rightmost
1782 * left node record.
1783 */
1784 el = left_path->p_node[i].el;
1785 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1786 left_rec = &el->l_recs[idx];
1787
1788 el = right_path->p_node[i].el;
1789 right_rec = &el->l_recs[0];
1790
1791 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1792 right_el);
1793
1794 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1795 if (ret)
1796 mlog_errno(ret);
1797
1798 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1799 if (ret)
1800 mlog_errno(ret);
1801
1802 /*
1803 * Setup our list pointers now so that the current
1804 * parents become children in the next iteration.
1805 */
1806 left_el = left_path->p_node[i].el;
1807 right_el = right_path->p_node[i].el;
1808 }
1809
1810 /*
1811 * At the root node, adjust the two adjacent records which
1812 * begin our path to the leaves.
1813 */
1814
1815 el = left_path->p_node[subtree_index].el;
1816 left_el = left_path->p_node[subtree_index + 1].el;
1817 right_el = right_path->p_node[subtree_index + 1].el;
1818
1819 ocfs2_adjust_root_records(el, left_el, right_el,
1820 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1821
1822 root_bh = left_path->p_node[subtree_index].bh;
1823
1824 ret = ocfs2_journal_dirty(handle, root_bh);
1825 if (ret)
1826 mlog_errno(ret);
1827}
1828
1829static int ocfs2_rotate_subtree_right(struct inode *inode,
1830 handle_t *handle,
1831 struct ocfs2_path *left_path,
1832 struct ocfs2_path *right_path,
1833 int subtree_index)
1834{
1835 int ret, i;
1836 struct buffer_head *right_leaf_bh;
1837 struct buffer_head *left_leaf_bh = NULL;
1838 struct buffer_head *root_bh;
1839 struct ocfs2_extent_list *right_el, *left_el;
1840 struct ocfs2_extent_rec move_rec;
1841
1842 left_leaf_bh = path_leaf_bh(left_path);
1843 left_el = path_leaf_el(left_path);
1844
1845 if (left_el->l_next_free_rec != left_el->l_count) {
1846 ocfs2_error(inode->i_sb,
1847 "Inode %llu has non-full interior leaf node %llu"
1848 "(next free = %u)",
1849 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1850 (unsigned long long)left_leaf_bh->b_blocknr,
1851 le16_to_cpu(left_el->l_next_free_rec));
1852 return -EROFS;
1853 }
1854
1855 /*
1856 * This extent block may already have an empty record, so we
1857 * return early if so.
1858 */
1859 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1860 return 0;
1861
1862 root_bh = left_path->p_node[subtree_index].bh;
1863 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1864
1865 ret = ocfs2_journal_access(handle, inode, root_bh,
1866 OCFS2_JOURNAL_ACCESS_WRITE);
1867 if (ret) {
1868 mlog_errno(ret);
1869 goto out;
1870 }
1871
1872 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1873 ret = ocfs2_journal_access(handle, inode,
1874 right_path->p_node[i].bh,
1875 OCFS2_JOURNAL_ACCESS_WRITE);
1876 if (ret) {
1877 mlog_errno(ret);
1878 goto out;
1879 }
1880
1881 ret = ocfs2_journal_access(handle, inode,
1882 left_path->p_node[i].bh,
1883 OCFS2_JOURNAL_ACCESS_WRITE);
1884 if (ret) {
1885 mlog_errno(ret);
1886 goto out;
1887 }
1888 }
1889
1890 right_leaf_bh = path_leaf_bh(right_path);
1891 right_el = path_leaf_el(right_path);
1892
1893 /* This is a code error, not a disk corruption. */
1894 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1895 "because rightmost leaf block %llu is empty\n",
1896 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1897 (unsigned long long)right_leaf_bh->b_blocknr);
1898
1899 ocfs2_create_empty_extent(right_el);
1900
1901 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1902 if (ret) {
1903 mlog_errno(ret);
1904 goto out;
1905 }
1906
1907 /* Do the copy now. */
1908 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1909 move_rec = left_el->l_recs[i];
1910 right_el->l_recs[0] = move_rec;
1911
1912 /*
1913 * Clear out the record we just copied and shift everything
1914 * over, leaving an empty extent in the left leaf.
1915 *
1916 * We temporarily subtract from next_free_rec so that the
1917 * shift will lose the tail record (which is now defunct).
1918 */
1919 le16_add_cpu(&left_el->l_next_free_rec, -1);
1920 ocfs2_shift_records_right(left_el);
1921 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1922 le16_add_cpu(&left_el->l_next_free_rec, 1);
1923
1924 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1925 if (ret) {
1926 mlog_errno(ret);
1927 goto out;
1928 }
1929
1930 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1931 subtree_index);
1932
1933out:
1934 return ret;
1935}
1936
1937/*
1938 * Given a full path, determine what cpos value would return us a path
1939 * containing the leaf immediately to the left of the current one.
1940 *
1941 * Will return zero if the path passed in is already the leftmost path.
1942 */
1943static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1944 struct ocfs2_path *path, u32 *cpos)
1945{
1946 int i, j, ret = 0;
1947 u64 blkno;
1948 struct ocfs2_extent_list *el;
1949
Mark Fashehe48edee2007-03-07 16:46:57 -08001950 BUG_ON(path->p_tree_depth == 0);
1951
Mark Fashehdcd05382007-01-16 11:32:23 -08001952 *cpos = 0;
1953
1954 blkno = path_leaf_bh(path)->b_blocknr;
1955
1956 /* Start at the tree node just above the leaf and work our way up. */
1957 i = path->p_tree_depth - 1;
1958 while (i >= 0) {
1959 el = path->p_node[i].el;
1960
1961 /*
1962 * Find the extent record just before the one in our
1963 * path.
1964 */
1965 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1966 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1967 if (j == 0) {
1968 if (i == 0) {
1969 /*
1970 * We've determined that the
1971 * path specified is already
1972 * the leftmost one - return a
1973 * cpos of zero.
1974 */
1975 goto out;
1976 }
1977 /*
1978 * The leftmost record points to our
1979 * leaf - we need to travel up the
1980 * tree one level.
1981 */
1982 goto next_node;
1983 }
1984
1985 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001986 *cpos = *cpos + ocfs2_rec_clusters(el,
1987 &el->l_recs[j - 1]);
1988 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08001989 goto out;
1990 }
1991 }
1992
1993 /*
1994 * If we got here, we never found a valid node where
1995 * the tree indicated one should be.
1996 */
1997 ocfs2_error(sb,
1998 "Invalid extent tree at extent block %llu\n",
1999 (unsigned long long)blkno);
2000 ret = -EROFS;
2001 goto out;
2002
2003next_node:
2004 blkno = path->p_node[i].bh->b_blocknr;
2005 i--;
2006 }
2007
2008out:
2009 return ret;
2010}
2011
Mark Fasheh328d5752007-06-18 10:48:04 -07002012/*
2013 * Extend the transaction by enough credits to complete the rotation,
2014 * and still leave at least the original number of credits allocated
2015 * to this transaction.
2016 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002017static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002018 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002019 struct ocfs2_path *path)
2020{
Mark Fasheh328d5752007-06-18 10:48:04 -07002021 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002022
2023 if (handle->h_buffer_credits < credits)
2024 return ocfs2_extend_trans(handle, credits);
2025
2026 return 0;
2027}
2028
2029/*
2030 * Trap the case where we're inserting into the theoretical range past
2031 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2032 * whose cpos is less than ours into the right leaf.
2033 *
2034 * It's only necessary to look at the rightmost record of the left
2035 * leaf because the logic that calls us should ensure that the
2036 * theoretical ranges in the path components above the leaves are
2037 * correct.
2038 */
2039static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2040 u32 insert_cpos)
2041{
2042 struct ocfs2_extent_list *left_el;
2043 struct ocfs2_extent_rec *rec;
2044 int next_free;
2045
2046 left_el = path_leaf_el(left_path);
2047 next_free = le16_to_cpu(left_el->l_next_free_rec);
2048 rec = &left_el->l_recs[next_free - 1];
2049
2050 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2051 return 1;
2052 return 0;
2053}
2054
Mark Fasheh328d5752007-06-18 10:48:04 -07002055static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2056{
2057 int next_free = le16_to_cpu(el->l_next_free_rec);
2058 unsigned int range;
2059 struct ocfs2_extent_rec *rec;
2060
2061 if (next_free == 0)
2062 return 0;
2063
2064 rec = &el->l_recs[0];
2065 if (ocfs2_is_empty_extent(rec)) {
2066 /* Empty list. */
2067 if (next_free == 1)
2068 return 0;
2069 rec = &el->l_recs[1];
2070 }
2071
2072 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2073 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2074 return 1;
2075 return 0;
2076}
2077
Mark Fashehdcd05382007-01-16 11:32:23 -08002078/*
2079 * Rotate all the records in a btree right one record, starting at insert_cpos.
2080 *
2081 * The path to the rightmost leaf should be passed in.
2082 *
2083 * The array is assumed to be large enough to hold an entire path (tree depth).
2084 *
2085 * Upon succesful return from this function:
2086 *
2087 * - The 'right_path' array will contain a path to the leaf block
2088 * whose range contains e_cpos.
2089 * - That leaf block will have a single empty extent in list index 0.
2090 * - In the case that the rotation requires a post-insert update,
2091 * *ret_left_path will contain a valid path which can be passed to
2092 * ocfs2_insert_path().
2093 */
2094static int ocfs2_rotate_tree_right(struct inode *inode,
2095 handle_t *handle,
Mark Fasheh328d5752007-06-18 10:48:04 -07002096 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002097 u32 insert_cpos,
2098 struct ocfs2_path *right_path,
2099 struct ocfs2_path **ret_left_path)
2100{
Mark Fasheh328d5752007-06-18 10:48:04 -07002101 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002102 u32 cpos;
2103 struct ocfs2_path *left_path = NULL;
2104
2105 *ret_left_path = NULL;
2106
2107 left_path = ocfs2_new_path(path_root_bh(right_path),
2108 path_root_el(right_path));
2109 if (!left_path) {
2110 ret = -ENOMEM;
2111 mlog_errno(ret);
2112 goto out;
2113 }
2114
2115 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2116 if (ret) {
2117 mlog_errno(ret);
2118 goto out;
2119 }
2120
2121 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2122
2123 /*
2124 * What we want to do here is:
2125 *
2126 * 1) Start with the rightmost path.
2127 *
2128 * 2) Determine a path to the leaf block directly to the left
2129 * of that leaf.
2130 *
2131 * 3) Determine the 'subtree root' - the lowest level tree node
2132 * which contains a path to both leaves.
2133 *
2134 * 4) Rotate the subtree.
2135 *
2136 * 5) Find the next subtree by considering the left path to be
2137 * the new right path.
2138 *
2139 * The check at the top of this while loop also accepts
2140 * insert_cpos == cpos because cpos is only a _theoretical_
2141 * value to get us the left path - insert_cpos might very well
2142 * be filling that hole.
2143 *
2144 * Stop at a cpos of '0' because we either started at the
2145 * leftmost branch (i.e., a tree with one branch and a
2146 * rotation inside of it), or we've gone as far as we can in
2147 * rotating subtrees.
2148 */
2149 while (cpos && insert_cpos <= cpos) {
2150 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2151 insert_cpos, cpos);
2152
2153 ret = ocfs2_find_path(inode, left_path, cpos);
2154 if (ret) {
2155 mlog_errno(ret);
2156 goto out;
2157 }
2158
2159 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2160 path_leaf_bh(right_path),
2161 "Inode %lu: error during insert of %u "
2162 "(left path cpos %u) results in two identical "
2163 "paths ending at %llu\n",
2164 inode->i_ino, insert_cpos, cpos,
2165 (unsigned long long)
2166 path_leaf_bh(left_path)->b_blocknr);
2167
Mark Fasheh328d5752007-06-18 10:48:04 -07002168 if (split == SPLIT_NONE &&
2169 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002170 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002171
2172 /*
2173 * We've rotated the tree as much as we
2174 * should. The rest is up to
2175 * ocfs2_insert_path() to complete, after the
2176 * record insertion. We indicate this
2177 * situation by returning the left path.
2178 *
2179 * The reason we don't adjust the records here
2180 * before the record insert is that an error
2181 * later might break the rule where a parent
2182 * record e_cpos will reflect the actual
2183 * e_cpos of the 1st nonempty record of the
2184 * child list.
2185 */
2186 *ret_left_path = left_path;
2187 goto out_ret_path;
2188 }
2189
2190 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2191
2192 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2193 start,
2194 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2195 right_path->p_tree_depth);
2196
2197 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002198 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002199 if (ret) {
2200 mlog_errno(ret);
2201 goto out;
2202 }
2203
2204 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2205 right_path, start);
2206 if (ret) {
2207 mlog_errno(ret);
2208 goto out;
2209 }
2210
Mark Fasheh328d5752007-06-18 10:48:04 -07002211 if (split != SPLIT_NONE &&
2212 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2213 insert_cpos)) {
2214 /*
2215 * A rotate moves the rightmost left leaf
2216 * record over to the leftmost right leaf
2217 * slot. If we're doing an extent split
2218 * instead of a real insert, then we have to
2219 * check that the extent to be split wasn't
2220 * just moved over. If it was, then we can
2221 * exit here, passing left_path back -
2222 * ocfs2_split_extent() is smart enough to
2223 * search both leaves.
2224 */
2225 *ret_left_path = left_path;
2226 goto out_ret_path;
2227 }
2228
Mark Fashehdcd05382007-01-16 11:32:23 -08002229 /*
2230 * There is no need to re-read the next right path
2231 * as we know that it'll be our current left
2232 * path. Optimize by copying values instead.
2233 */
2234 ocfs2_mv_path(right_path, left_path);
2235
2236 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2237 &cpos);
2238 if (ret) {
2239 mlog_errno(ret);
2240 goto out;
2241 }
2242 }
2243
2244out:
2245 ocfs2_free_path(left_path);
2246
2247out_ret_path:
2248 return ret;
2249}
2250
Mark Fasheh328d5752007-06-18 10:48:04 -07002251static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2252 struct ocfs2_path *path)
2253{
2254 int i, idx;
2255 struct ocfs2_extent_rec *rec;
2256 struct ocfs2_extent_list *el;
2257 struct ocfs2_extent_block *eb;
2258 u32 range;
2259
2260 /* Path should always be rightmost. */
2261 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2262 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2263
2264 el = &eb->h_list;
2265 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2266 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2267 rec = &el->l_recs[idx];
2268 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2269
2270 for (i = 0; i < path->p_tree_depth; i++) {
2271 el = path->p_node[i].el;
2272 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2273 rec = &el->l_recs[idx];
2274
2275 rec->e_int_clusters = cpu_to_le32(range);
2276 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2277
2278 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2279 }
2280}
2281
2282static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2283 struct ocfs2_cached_dealloc_ctxt *dealloc,
2284 struct ocfs2_path *path, int unlink_start)
2285{
2286 int ret, i;
2287 struct ocfs2_extent_block *eb;
2288 struct ocfs2_extent_list *el;
2289 struct buffer_head *bh;
2290
2291 for(i = unlink_start; i < path_num_items(path); i++) {
2292 bh = path->p_node[i].bh;
2293
2294 eb = (struct ocfs2_extent_block *)bh->b_data;
2295 /*
2296 * Not all nodes might have had their final count
2297 * decremented by the caller - handle this here.
2298 */
2299 el = &eb->h_list;
2300 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2301 mlog(ML_ERROR,
2302 "Inode %llu, attempted to remove extent block "
2303 "%llu with %u records\n",
2304 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2305 (unsigned long long)le64_to_cpu(eb->h_blkno),
2306 le16_to_cpu(el->l_next_free_rec));
2307
2308 ocfs2_journal_dirty(handle, bh);
2309 ocfs2_remove_from_cache(inode, bh);
2310 continue;
2311 }
2312
2313 el->l_next_free_rec = 0;
2314 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2315
2316 ocfs2_journal_dirty(handle, bh);
2317
2318 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2319 if (ret)
2320 mlog_errno(ret);
2321
2322 ocfs2_remove_from_cache(inode, bh);
2323 }
2324}
2325
2326static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2327 struct ocfs2_path *left_path,
2328 struct ocfs2_path *right_path,
2329 int subtree_index,
2330 struct ocfs2_cached_dealloc_ctxt *dealloc)
2331{
2332 int i;
2333 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2334 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2335 struct ocfs2_extent_list *el;
2336 struct ocfs2_extent_block *eb;
2337
2338 el = path_leaf_el(left_path);
2339
2340 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2341
2342 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2343 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2344 break;
2345
2346 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2347
2348 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2349 le16_add_cpu(&root_el->l_next_free_rec, -1);
2350
2351 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2352 eb->h_next_leaf_blk = 0;
2353
2354 ocfs2_journal_dirty(handle, root_bh);
2355 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2356
2357 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2358 subtree_index + 1);
2359}
2360
2361static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2362 struct ocfs2_path *left_path,
2363 struct ocfs2_path *right_path,
2364 int subtree_index,
2365 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002366 int *deleted,
2367 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002368{
2369 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002370 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002371 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2372 struct ocfs2_extent_block *eb;
2373
2374 *deleted = 0;
2375
2376 right_leaf_el = path_leaf_el(right_path);
2377 left_leaf_el = path_leaf_el(left_path);
2378 root_bh = left_path->p_node[subtree_index].bh;
2379 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2380
2381 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2382 return 0;
2383
2384 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2385 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2386 /*
2387 * It's legal for us to proceed if the right leaf is
2388 * the rightmost one and it has an empty extent. There
2389 * are two cases to handle - whether the leaf will be
2390 * empty after removal or not. If the leaf isn't empty
2391 * then just remove the empty extent up front. The
2392 * next block will handle empty leaves by flagging
2393 * them for unlink.
2394 *
2395 * Non rightmost leaves will throw -EAGAIN and the
2396 * caller can manually move the subtree and retry.
2397 */
2398
2399 if (eb->h_next_leaf_blk != 0ULL)
2400 return -EAGAIN;
2401
2402 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2403 ret = ocfs2_journal_access(handle, inode,
2404 path_leaf_bh(right_path),
2405 OCFS2_JOURNAL_ACCESS_WRITE);
2406 if (ret) {
2407 mlog_errno(ret);
2408 goto out;
2409 }
2410
2411 ocfs2_remove_empty_extent(right_leaf_el);
2412 } else
2413 right_has_empty = 1;
2414 }
2415
2416 if (eb->h_next_leaf_blk == 0ULL &&
2417 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2418 /*
2419 * We have to update i_last_eb_blk during the meta
2420 * data delete.
2421 */
Tao Mae7d4cb62008-08-18 17:38:44 +08002422 ret = ocfs2_journal_access(handle, inode, et_root_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07002423 OCFS2_JOURNAL_ACCESS_WRITE);
2424 if (ret) {
2425 mlog_errno(ret);
2426 goto out;
2427 }
2428
2429 del_right_subtree = 1;
2430 }
2431
2432 /*
2433 * Getting here with an empty extent in the right path implies
2434 * that it's the rightmost path and will be deleted.
2435 */
2436 BUG_ON(right_has_empty && !del_right_subtree);
2437
2438 ret = ocfs2_journal_access(handle, inode, root_bh,
2439 OCFS2_JOURNAL_ACCESS_WRITE);
2440 if (ret) {
2441 mlog_errno(ret);
2442 goto out;
2443 }
2444
2445 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2446 ret = ocfs2_journal_access(handle, inode,
2447 right_path->p_node[i].bh,
2448 OCFS2_JOURNAL_ACCESS_WRITE);
2449 if (ret) {
2450 mlog_errno(ret);
2451 goto out;
2452 }
2453
2454 ret = ocfs2_journal_access(handle, inode,
2455 left_path->p_node[i].bh,
2456 OCFS2_JOURNAL_ACCESS_WRITE);
2457 if (ret) {
2458 mlog_errno(ret);
2459 goto out;
2460 }
2461 }
2462
2463 if (!right_has_empty) {
2464 /*
2465 * Only do this if we're moving a real
2466 * record. Otherwise, the action is delayed until
2467 * after removal of the right path in which case we
2468 * can do a simple shift to remove the empty extent.
2469 */
2470 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2471 memset(&right_leaf_el->l_recs[0], 0,
2472 sizeof(struct ocfs2_extent_rec));
2473 }
2474 if (eb->h_next_leaf_blk == 0ULL) {
2475 /*
2476 * Move recs over to get rid of empty extent, decrease
2477 * next_free. This is allowed to remove the last
2478 * extent in our leaf (setting l_next_free_rec to
2479 * zero) - the delete code below won't care.
2480 */
2481 ocfs2_remove_empty_extent(right_leaf_el);
2482 }
2483
2484 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2485 if (ret)
2486 mlog_errno(ret);
2487 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2488 if (ret)
2489 mlog_errno(ret);
2490
2491 if (del_right_subtree) {
2492 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2493 subtree_index, dealloc);
2494 ocfs2_update_edge_lengths(inode, handle, left_path);
2495
2496 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002497 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002498
2499 /*
2500 * Removal of the extent in the left leaf was skipped
2501 * above so we could delete the right path
2502 * 1st.
2503 */
2504 if (right_has_empty)
2505 ocfs2_remove_empty_extent(left_leaf_el);
2506
Tao Mae7d4cb62008-08-18 17:38:44 +08002507 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002508 if (ret)
2509 mlog_errno(ret);
2510
2511 *deleted = 1;
2512 } else
2513 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2514 subtree_index);
2515
2516out:
2517 return ret;
2518}
2519
2520/*
2521 * Given a full path, determine what cpos value would return us a path
2522 * containing the leaf immediately to the right of the current one.
2523 *
2524 * Will return zero if the path passed in is already the rightmost path.
2525 *
2526 * This looks similar, but is subtly different to
2527 * ocfs2_find_cpos_for_left_leaf().
2528 */
2529static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2530 struct ocfs2_path *path, u32 *cpos)
2531{
2532 int i, j, ret = 0;
2533 u64 blkno;
2534 struct ocfs2_extent_list *el;
2535
2536 *cpos = 0;
2537
2538 if (path->p_tree_depth == 0)
2539 return 0;
2540
2541 blkno = path_leaf_bh(path)->b_blocknr;
2542
2543 /* Start at the tree node just above the leaf and work our way up. */
2544 i = path->p_tree_depth - 1;
2545 while (i >= 0) {
2546 int next_free;
2547
2548 el = path->p_node[i].el;
2549
2550 /*
2551 * Find the extent record just after the one in our
2552 * path.
2553 */
2554 next_free = le16_to_cpu(el->l_next_free_rec);
2555 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2556 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2557 if (j == (next_free - 1)) {
2558 if (i == 0) {
2559 /*
2560 * We've determined that the
2561 * path specified is already
2562 * the rightmost one - return a
2563 * cpos of zero.
2564 */
2565 goto out;
2566 }
2567 /*
2568 * The rightmost record points to our
2569 * leaf - we need to travel up the
2570 * tree one level.
2571 */
2572 goto next_node;
2573 }
2574
2575 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2576 goto out;
2577 }
2578 }
2579
2580 /*
2581 * If we got here, we never found a valid node where
2582 * the tree indicated one should be.
2583 */
2584 ocfs2_error(sb,
2585 "Invalid extent tree at extent block %llu\n",
2586 (unsigned long long)blkno);
2587 ret = -EROFS;
2588 goto out;
2589
2590next_node:
2591 blkno = path->p_node[i].bh->b_blocknr;
2592 i--;
2593 }
2594
2595out:
2596 return ret;
2597}
2598
2599static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2600 handle_t *handle,
2601 struct buffer_head *bh,
2602 struct ocfs2_extent_list *el)
2603{
2604 int ret;
2605
2606 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2607 return 0;
2608
2609 ret = ocfs2_journal_access(handle, inode, bh,
2610 OCFS2_JOURNAL_ACCESS_WRITE);
2611 if (ret) {
2612 mlog_errno(ret);
2613 goto out;
2614 }
2615
2616 ocfs2_remove_empty_extent(el);
2617
2618 ret = ocfs2_journal_dirty(handle, bh);
2619 if (ret)
2620 mlog_errno(ret);
2621
2622out:
2623 return ret;
2624}
2625
2626static int __ocfs2_rotate_tree_left(struct inode *inode,
2627 handle_t *handle, int orig_credits,
2628 struct ocfs2_path *path,
2629 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002630 struct ocfs2_path **empty_extent_path,
2631 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002632{
2633 int ret, subtree_root, deleted;
2634 u32 right_cpos;
2635 struct ocfs2_path *left_path = NULL;
2636 struct ocfs2_path *right_path = NULL;
2637
2638 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2639
2640 *empty_extent_path = NULL;
2641
2642 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2643 &right_cpos);
2644 if (ret) {
2645 mlog_errno(ret);
2646 goto out;
2647 }
2648
2649 left_path = ocfs2_new_path(path_root_bh(path),
2650 path_root_el(path));
2651 if (!left_path) {
2652 ret = -ENOMEM;
2653 mlog_errno(ret);
2654 goto out;
2655 }
2656
2657 ocfs2_cp_path(left_path, path);
2658
2659 right_path = ocfs2_new_path(path_root_bh(path),
2660 path_root_el(path));
2661 if (!right_path) {
2662 ret = -ENOMEM;
2663 mlog_errno(ret);
2664 goto out;
2665 }
2666
2667 while (right_cpos) {
2668 ret = ocfs2_find_path(inode, right_path, right_cpos);
2669 if (ret) {
2670 mlog_errno(ret);
2671 goto out;
2672 }
2673
2674 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2675 right_path);
2676
2677 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2678 subtree_root,
2679 (unsigned long long)
2680 right_path->p_node[subtree_root].bh->b_blocknr,
2681 right_path->p_tree_depth);
2682
2683 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2684 orig_credits, left_path);
2685 if (ret) {
2686 mlog_errno(ret);
2687 goto out;
2688 }
2689
Mark Fashehe8aed342007-12-03 16:43:01 -08002690 /*
2691 * Caller might still want to make changes to the
2692 * tree root, so re-add it to the journal here.
2693 */
2694 ret = ocfs2_journal_access(handle, inode,
2695 path_root_bh(left_path),
2696 OCFS2_JOURNAL_ACCESS_WRITE);
2697 if (ret) {
2698 mlog_errno(ret);
2699 goto out;
2700 }
2701
Mark Fasheh328d5752007-06-18 10:48:04 -07002702 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2703 right_path, subtree_root,
Tao Mae7d4cb62008-08-18 17:38:44 +08002704 dealloc, &deleted, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002705 if (ret == -EAGAIN) {
2706 /*
2707 * The rotation has to temporarily stop due to
2708 * the right subtree having an empty
2709 * extent. Pass it back to the caller for a
2710 * fixup.
2711 */
2712 *empty_extent_path = right_path;
2713 right_path = NULL;
2714 goto out;
2715 }
2716 if (ret) {
2717 mlog_errno(ret);
2718 goto out;
2719 }
2720
2721 /*
2722 * The subtree rotate might have removed records on
2723 * the rightmost edge. If so, then rotation is
2724 * complete.
2725 */
2726 if (deleted)
2727 break;
2728
2729 ocfs2_mv_path(left_path, right_path);
2730
2731 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2732 &right_cpos);
2733 if (ret) {
2734 mlog_errno(ret);
2735 goto out;
2736 }
2737 }
2738
2739out:
2740 ocfs2_free_path(right_path);
2741 ocfs2_free_path(left_path);
2742
2743 return ret;
2744}
2745
2746static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08002747 struct ocfs2_path *path,
2748 struct ocfs2_cached_dealloc_ctxt *dealloc,
2749 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002750{
2751 int ret, subtree_index;
2752 u32 cpos;
2753 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07002754 struct ocfs2_extent_block *eb;
2755 struct ocfs2_extent_list *el;
2756
Mark Fasheh328d5752007-06-18 10:48:04 -07002757
Joel Becker35dc0aa2008-08-20 16:25:06 -07002758 ret = ocfs2_et_sanity_check(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002759 if (ret)
2760 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07002761 /*
2762 * There's two ways we handle this depending on
2763 * whether path is the only existing one.
2764 */
2765 ret = ocfs2_extend_rotate_transaction(handle, 0,
2766 handle->h_buffer_credits,
2767 path);
2768 if (ret) {
2769 mlog_errno(ret);
2770 goto out;
2771 }
2772
2773 ret = ocfs2_journal_access_path(inode, handle, path);
2774 if (ret) {
2775 mlog_errno(ret);
2776 goto out;
2777 }
2778
2779 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2780 if (ret) {
2781 mlog_errno(ret);
2782 goto out;
2783 }
2784
2785 if (cpos) {
2786 /*
2787 * We have a path to the left of this one - it needs
2788 * an update too.
2789 */
2790 left_path = ocfs2_new_path(path_root_bh(path),
2791 path_root_el(path));
2792 if (!left_path) {
2793 ret = -ENOMEM;
2794 mlog_errno(ret);
2795 goto out;
2796 }
2797
2798 ret = ocfs2_find_path(inode, left_path, cpos);
2799 if (ret) {
2800 mlog_errno(ret);
2801 goto out;
2802 }
2803
2804 ret = ocfs2_journal_access_path(inode, handle, left_path);
2805 if (ret) {
2806 mlog_errno(ret);
2807 goto out;
2808 }
2809
2810 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2811
2812 ocfs2_unlink_subtree(inode, handle, left_path, path,
2813 subtree_index, dealloc);
2814 ocfs2_update_edge_lengths(inode, handle, left_path);
2815
2816 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002817 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002818 } else {
2819 /*
2820 * 'path' is also the leftmost path which
2821 * means it must be the only one. This gets
2822 * handled differently because we want to
2823 * revert the inode back to having extents
2824 * in-line.
2825 */
2826 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2827
Joel Beckerce1d9ea2008-08-20 16:30:07 -07002828 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07002829 el->l_tree_depth = 0;
2830 el->l_next_free_rec = 0;
2831 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2832
Joel Becker35dc0aa2008-08-20 16:25:06 -07002833 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07002834 }
2835
2836 ocfs2_journal_dirty(handle, path_root_bh(path));
2837
2838out:
2839 ocfs2_free_path(left_path);
2840 return ret;
2841}
2842
2843/*
2844 * Left rotation of btree records.
2845 *
2846 * In many ways, this is (unsurprisingly) the opposite of right
2847 * rotation. We start at some non-rightmost path containing an empty
2848 * extent in the leaf block. The code works its way to the rightmost
2849 * path by rotating records to the left in every subtree.
2850 *
2851 * This is used by any code which reduces the number of extent records
2852 * in a leaf. After removal, an empty record should be placed in the
2853 * leftmost list position.
2854 *
2855 * This won't handle a length update of the rightmost path records if
2856 * the rightmost tree leaf record is removed so the caller is
2857 * responsible for detecting and correcting that.
2858 */
2859static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2860 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002861 struct ocfs2_cached_dealloc_ctxt *dealloc,
2862 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002863{
2864 int ret, orig_credits = handle->h_buffer_credits;
2865 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2866 struct ocfs2_extent_block *eb;
2867 struct ocfs2_extent_list *el;
2868
2869 el = path_leaf_el(path);
2870 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2871 return 0;
2872
2873 if (path->p_tree_depth == 0) {
2874rightmost_no_delete:
2875 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08002876 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07002877 * it up front.
2878 */
2879 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2880 path_leaf_bh(path),
2881 path_leaf_el(path));
2882 if (ret)
2883 mlog_errno(ret);
2884 goto out;
2885 }
2886
2887 /*
2888 * Handle rightmost branch now. There's several cases:
2889 * 1) simple rotation leaving records in there. That's trivial.
2890 * 2) rotation requiring a branch delete - there's no more
2891 * records left. Two cases of this:
2892 * a) There are branches to the left.
2893 * b) This is also the leftmost (the only) branch.
2894 *
2895 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2896 * 2a) we need the left branch so that we can update it with the unlink
2897 * 2b) we need to bring the inode back to inline extents.
2898 */
2899
2900 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2901 el = &eb->h_list;
2902 if (eb->h_next_leaf_blk == 0) {
2903 /*
2904 * This gets a bit tricky if we're going to delete the
2905 * rightmost path. Get the other cases out of the way
2906 * 1st.
2907 */
2908 if (le16_to_cpu(el->l_next_free_rec) > 1)
2909 goto rightmost_no_delete;
2910
2911 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2912 ret = -EIO;
2913 ocfs2_error(inode->i_sb,
2914 "Inode %llu has empty extent block at %llu",
2915 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2916 (unsigned long long)le64_to_cpu(eb->h_blkno));
2917 goto out;
2918 }
2919
2920 /*
2921 * XXX: The caller can not trust "path" any more after
2922 * this as it will have been deleted. What do we do?
2923 *
2924 * In theory the rotate-for-merge code will never get
2925 * here because it'll always ask for a rotate in a
2926 * nonempty list.
2927 */
2928
2929 ret = ocfs2_remove_rightmost_path(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002930 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002931 if (ret)
2932 mlog_errno(ret);
2933 goto out;
2934 }
2935
2936 /*
2937 * Now we can loop, remembering the path we get from -EAGAIN
2938 * and restarting from there.
2939 */
2940try_rotate:
2941 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002942 dealloc, &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002943 if (ret && ret != -EAGAIN) {
2944 mlog_errno(ret);
2945 goto out;
2946 }
2947
2948 while (ret == -EAGAIN) {
2949 tmp_path = restart_path;
2950 restart_path = NULL;
2951
2952 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2953 tmp_path, dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002954 &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002955 if (ret && ret != -EAGAIN) {
2956 mlog_errno(ret);
2957 goto out;
2958 }
2959
2960 ocfs2_free_path(tmp_path);
2961 tmp_path = NULL;
2962
2963 if (ret == 0)
2964 goto try_rotate;
2965 }
2966
2967out:
2968 ocfs2_free_path(tmp_path);
2969 ocfs2_free_path(restart_path);
2970 return ret;
2971}
2972
2973static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
2974 int index)
2975{
2976 struct ocfs2_extent_rec *rec = &el->l_recs[index];
2977 unsigned int size;
2978
2979 if (rec->e_leaf_clusters == 0) {
2980 /*
2981 * We consumed all of the merged-from record. An empty
2982 * extent cannot exist anywhere but the 1st array
2983 * position, so move things over if the merged-from
2984 * record doesn't occupy that position.
2985 *
2986 * This creates a new empty extent so the caller
2987 * should be smart enough to have removed any existing
2988 * ones.
2989 */
2990 if (index > 0) {
2991 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
2992 size = index * sizeof(struct ocfs2_extent_rec);
2993 memmove(&el->l_recs[1], &el->l_recs[0], size);
2994 }
2995
2996 /*
2997 * Always memset - the caller doesn't check whether it
2998 * created an empty extent, so there could be junk in
2999 * the other fields.
3000 */
3001 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3002 }
3003}
3004
Tao Ma677b9752008-01-30 14:21:05 +08003005static int ocfs2_get_right_path(struct inode *inode,
3006 struct ocfs2_path *left_path,
3007 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003008{
3009 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003010 u32 right_cpos;
3011 struct ocfs2_path *right_path = NULL;
3012 struct ocfs2_extent_list *left_el;
3013
3014 *ret_right_path = NULL;
3015
3016 /* This function shouldn't be called for non-trees. */
3017 BUG_ON(left_path->p_tree_depth == 0);
3018
3019 left_el = path_leaf_el(left_path);
3020 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3021
3022 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3023 &right_cpos);
3024 if (ret) {
3025 mlog_errno(ret);
3026 goto out;
3027 }
3028
3029 /* This function shouldn't be called for the rightmost leaf. */
3030 BUG_ON(right_cpos == 0);
3031
3032 right_path = ocfs2_new_path(path_root_bh(left_path),
3033 path_root_el(left_path));
3034 if (!right_path) {
3035 ret = -ENOMEM;
3036 mlog_errno(ret);
3037 goto out;
3038 }
3039
3040 ret = ocfs2_find_path(inode, right_path, right_cpos);
3041 if (ret) {
3042 mlog_errno(ret);
3043 goto out;
3044 }
3045
3046 *ret_right_path = right_path;
3047out:
3048 if (ret)
3049 ocfs2_free_path(right_path);
3050 return ret;
3051}
3052
3053/*
3054 * Remove split_rec clusters from the record at index and merge them
3055 * onto the beginning of the record "next" to it.
3056 * For index < l_count - 1, the next means the extent rec at index + 1.
3057 * For index == l_count - 1, the "next" means the 1st extent rec of the
3058 * next extent block.
3059 */
3060static int ocfs2_merge_rec_right(struct inode *inode,
3061 struct ocfs2_path *left_path,
3062 handle_t *handle,
3063 struct ocfs2_extent_rec *split_rec,
3064 int index)
3065{
3066 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003067 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3068 struct ocfs2_extent_rec *left_rec;
3069 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003070 struct ocfs2_extent_list *right_el;
3071 struct ocfs2_path *right_path = NULL;
3072 int subtree_index = 0;
3073 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3074 struct buffer_head *bh = path_leaf_bh(left_path);
3075 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003076
3077 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003078 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003079
Al Viro9d8df6a2008-05-21 06:32:11 +01003080 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003081 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3082 /* we meet with a cross extent block merge. */
3083 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3084 if (ret) {
3085 mlog_errno(ret);
3086 goto out;
3087 }
3088
3089 right_el = path_leaf_el(right_path);
3090 next_free = le16_to_cpu(right_el->l_next_free_rec);
3091 BUG_ON(next_free <= 0);
3092 right_rec = &right_el->l_recs[0];
3093 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003094 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003095 right_rec = &right_el->l_recs[1];
3096 }
3097
3098 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3099 le16_to_cpu(left_rec->e_leaf_clusters) !=
3100 le32_to_cpu(right_rec->e_cpos));
3101
3102 subtree_index = ocfs2_find_subtree_root(inode,
3103 left_path, right_path);
3104
3105 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3106 handle->h_buffer_credits,
3107 right_path);
3108 if (ret) {
3109 mlog_errno(ret);
3110 goto out;
3111 }
3112
3113 root_bh = left_path->p_node[subtree_index].bh;
3114 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3115
3116 ret = ocfs2_journal_access(handle, inode, root_bh,
3117 OCFS2_JOURNAL_ACCESS_WRITE);
3118 if (ret) {
3119 mlog_errno(ret);
3120 goto out;
3121 }
3122
3123 for (i = subtree_index + 1;
3124 i < path_num_items(right_path); i++) {
3125 ret = ocfs2_journal_access(handle, inode,
3126 right_path->p_node[i].bh,
3127 OCFS2_JOURNAL_ACCESS_WRITE);
3128 if (ret) {
3129 mlog_errno(ret);
3130 goto out;
3131 }
3132
3133 ret = ocfs2_journal_access(handle, inode,
3134 left_path->p_node[i].bh,
3135 OCFS2_JOURNAL_ACCESS_WRITE);
3136 if (ret) {
3137 mlog_errno(ret);
3138 goto out;
3139 }
3140 }
3141
3142 } else {
3143 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3144 right_rec = &el->l_recs[index + 1];
3145 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003146
3147 ret = ocfs2_journal_access(handle, inode, bh,
3148 OCFS2_JOURNAL_ACCESS_WRITE);
3149 if (ret) {
3150 mlog_errno(ret);
3151 goto out;
3152 }
3153
3154 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3155
3156 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3157 le64_add_cpu(&right_rec->e_blkno,
3158 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3159 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3160
3161 ocfs2_cleanup_merge(el, index);
3162
3163 ret = ocfs2_journal_dirty(handle, bh);
3164 if (ret)
3165 mlog_errno(ret);
3166
Tao Ma677b9752008-01-30 14:21:05 +08003167 if (right_path) {
3168 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3169 if (ret)
3170 mlog_errno(ret);
3171
3172 ocfs2_complete_edge_insert(inode, handle, left_path,
3173 right_path, subtree_index);
3174 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003175out:
Tao Ma677b9752008-01-30 14:21:05 +08003176 if (right_path)
3177 ocfs2_free_path(right_path);
3178 return ret;
3179}
3180
3181static int ocfs2_get_left_path(struct inode *inode,
3182 struct ocfs2_path *right_path,
3183 struct ocfs2_path **ret_left_path)
3184{
3185 int ret;
3186 u32 left_cpos;
3187 struct ocfs2_path *left_path = NULL;
3188
3189 *ret_left_path = NULL;
3190
3191 /* This function shouldn't be called for non-trees. */
3192 BUG_ON(right_path->p_tree_depth == 0);
3193
3194 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3195 right_path, &left_cpos);
3196 if (ret) {
3197 mlog_errno(ret);
3198 goto out;
3199 }
3200
3201 /* This function shouldn't be called for the leftmost leaf. */
3202 BUG_ON(left_cpos == 0);
3203
3204 left_path = ocfs2_new_path(path_root_bh(right_path),
3205 path_root_el(right_path));
3206 if (!left_path) {
3207 ret = -ENOMEM;
3208 mlog_errno(ret);
3209 goto out;
3210 }
3211
3212 ret = ocfs2_find_path(inode, left_path, left_cpos);
3213 if (ret) {
3214 mlog_errno(ret);
3215 goto out;
3216 }
3217
3218 *ret_left_path = left_path;
3219out:
3220 if (ret)
3221 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003222 return ret;
3223}
3224
3225/*
3226 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003227 * onto the tail of the record "before" it.
3228 * For index > 0, the "before" means the extent rec at index - 1.
3229 *
3230 * For index == 0, the "before" means the last record of the previous
3231 * extent block. And there is also a situation that we may need to
3232 * remove the rightmost leaf extent block in the right_path and change
3233 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003234 */
Tao Ma677b9752008-01-30 14:21:05 +08003235static int ocfs2_merge_rec_left(struct inode *inode,
3236 struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003237 handle_t *handle,
3238 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003239 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003240 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003241 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003242{
Tao Ma677b9752008-01-30 14:21:05 +08003243 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003244 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3245 struct ocfs2_extent_rec *left_rec;
3246 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003247 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3248 struct buffer_head *bh = path_leaf_bh(right_path);
3249 struct buffer_head *root_bh = NULL;
3250 struct ocfs2_path *left_path = NULL;
3251 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003252
Tao Ma677b9752008-01-30 14:21:05 +08003253 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003254
Mark Fasheh328d5752007-06-18 10:48:04 -07003255 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003256 if (index == 0) {
3257 /* we meet with a cross extent block merge. */
3258 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3259 if (ret) {
3260 mlog_errno(ret);
3261 goto out;
3262 }
3263
3264 left_el = path_leaf_el(left_path);
3265 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3266 le16_to_cpu(left_el->l_count));
3267
3268 left_rec = &left_el->l_recs[
3269 le16_to_cpu(left_el->l_next_free_rec) - 1];
3270 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3271 le16_to_cpu(left_rec->e_leaf_clusters) !=
3272 le32_to_cpu(split_rec->e_cpos));
3273
3274 subtree_index = ocfs2_find_subtree_root(inode,
3275 left_path, right_path);
3276
3277 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3278 handle->h_buffer_credits,
3279 left_path);
3280 if (ret) {
3281 mlog_errno(ret);
3282 goto out;
3283 }
3284
3285 root_bh = left_path->p_node[subtree_index].bh;
3286 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3287
3288 ret = ocfs2_journal_access(handle, inode, root_bh,
3289 OCFS2_JOURNAL_ACCESS_WRITE);
3290 if (ret) {
3291 mlog_errno(ret);
3292 goto out;
3293 }
3294
3295 for (i = subtree_index + 1;
3296 i < path_num_items(right_path); i++) {
3297 ret = ocfs2_journal_access(handle, inode,
3298 right_path->p_node[i].bh,
3299 OCFS2_JOURNAL_ACCESS_WRITE);
3300 if (ret) {
3301 mlog_errno(ret);
3302 goto out;
3303 }
3304
3305 ret = ocfs2_journal_access(handle, inode,
3306 left_path->p_node[i].bh,
3307 OCFS2_JOURNAL_ACCESS_WRITE);
3308 if (ret) {
3309 mlog_errno(ret);
3310 goto out;
3311 }
3312 }
3313 } else {
3314 left_rec = &el->l_recs[index - 1];
3315 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3316 has_empty_extent = 1;
3317 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003318
3319 ret = ocfs2_journal_access(handle, inode, bh,
3320 OCFS2_JOURNAL_ACCESS_WRITE);
3321 if (ret) {
3322 mlog_errno(ret);
3323 goto out;
3324 }
3325
3326 if (has_empty_extent && index == 1) {
3327 /*
3328 * The easy case - we can just plop the record right in.
3329 */
3330 *left_rec = *split_rec;
3331
3332 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003333 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003334 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003335
3336 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3337 le64_add_cpu(&right_rec->e_blkno,
3338 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3339 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3340
3341 ocfs2_cleanup_merge(el, index);
3342
3343 ret = ocfs2_journal_dirty(handle, bh);
3344 if (ret)
3345 mlog_errno(ret);
3346
Tao Ma677b9752008-01-30 14:21:05 +08003347 if (left_path) {
3348 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3349 if (ret)
3350 mlog_errno(ret);
3351
3352 /*
3353 * In the situation that the right_rec is empty and the extent
3354 * block is empty also, ocfs2_complete_edge_insert can't handle
3355 * it and we need to delete the right extent block.
3356 */
3357 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3358 le16_to_cpu(el->l_next_free_rec) == 1) {
3359
3360 ret = ocfs2_remove_rightmost_path(inode, handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003361 right_path,
3362 dealloc, et);
Tao Ma677b9752008-01-30 14:21:05 +08003363 if (ret) {
3364 mlog_errno(ret);
3365 goto out;
3366 }
3367
3368 /* Now the rightmost extent block has been deleted.
3369 * So we use the new rightmost path.
3370 */
3371 ocfs2_mv_path(right_path, left_path);
3372 left_path = NULL;
3373 } else
3374 ocfs2_complete_edge_insert(inode, handle, left_path,
3375 right_path, subtree_index);
3376 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003377out:
Tao Ma677b9752008-01-30 14:21:05 +08003378 if (left_path)
3379 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003380 return ret;
3381}
3382
3383static int ocfs2_try_to_merge_extent(struct inode *inode,
3384 handle_t *handle,
Tao Ma677b9752008-01-30 14:21:05 +08003385 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003386 int split_index,
3387 struct ocfs2_extent_rec *split_rec,
3388 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003389 struct ocfs2_merge_ctxt *ctxt,
3390 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003391
3392{
Tao Mao518d7262007-08-28 17:25:35 -07003393 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003394 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003395 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3396
3397 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3398
Tao Mao518d7262007-08-28 17:25:35 -07003399 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3400 /*
3401 * The merge code will need to create an empty
3402 * extent to take the place of the newly
3403 * emptied slot. Remove any pre-existing empty
3404 * extents - having more than one in a leaf is
3405 * illegal.
3406 */
Tao Ma677b9752008-01-30 14:21:05 +08003407 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003408 dealloc, et);
Tao Mao518d7262007-08-28 17:25:35 -07003409 if (ret) {
3410 mlog_errno(ret);
3411 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003412 }
Tao Mao518d7262007-08-28 17:25:35 -07003413 split_index--;
3414 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003415 }
3416
3417 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3418 /*
3419 * Left-right contig implies this.
3420 */
3421 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003422
3423 /*
3424 * Since the leftright insert always covers the entire
3425 * extent, this call will delete the insert record
3426 * entirely, resulting in an empty extent record added to
3427 * the extent block.
3428 *
3429 * Since the adding of an empty extent shifts
3430 * everything back to the right, there's no need to
3431 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003432 *
3433 * When the split_index is zero, we need to merge it to the
3434 * prevoius extent block. It is more efficient and easier
3435 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003436 */
Tao Ma677b9752008-01-30 14:21:05 +08003437 ret = ocfs2_merge_rec_right(inode, path,
3438 handle, split_rec,
3439 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003440 if (ret) {
3441 mlog_errno(ret);
3442 goto out;
3443 }
3444
3445 /*
3446 * We can only get this from logic error above.
3447 */
3448 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3449
Tao Ma677b9752008-01-30 14:21:05 +08003450 /* The merge left us with an empty extent, remove it. */
Tao Mae7d4cb62008-08-18 17:38:44 +08003451 ret = ocfs2_rotate_tree_left(inode, handle, path,
3452 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003453 if (ret) {
3454 mlog_errno(ret);
3455 goto out;
3456 }
Tao Ma677b9752008-01-30 14:21:05 +08003457
Mark Fasheh328d5752007-06-18 10:48:04 -07003458 rec = &el->l_recs[split_index];
3459
3460 /*
3461 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003462 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003463 */
Tao Ma677b9752008-01-30 14:21:05 +08003464 ret = ocfs2_merge_rec_left(inode, path,
3465 handle, rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003466 dealloc, et,
Tao Ma677b9752008-01-30 14:21:05 +08003467 split_index);
3468
Mark Fasheh328d5752007-06-18 10:48:04 -07003469 if (ret) {
3470 mlog_errno(ret);
3471 goto out;
3472 }
3473
Tao Ma677b9752008-01-30 14:21:05 +08003474 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003475 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003476 /*
3477 * Error from this last rotate is not critical, so
3478 * print but don't bubble it up.
3479 */
3480 if (ret)
3481 mlog_errno(ret);
3482 ret = 0;
3483 } else {
3484 /*
3485 * Merge a record to the left or right.
3486 *
3487 * 'contig_type' is relative to the existing record,
3488 * so for example, if we're "right contig", it's to
3489 * the record on the left (hence the left merge).
3490 */
3491 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3492 ret = ocfs2_merge_rec_left(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003493 path,
3494 handle, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003495 dealloc, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003496 split_index);
3497 if (ret) {
3498 mlog_errno(ret);
3499 goto out;
3500 }
3501 } else {
3502 ret = ocfs2_merge_rec_right(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003503 path,
3504 handle, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003505 split_index);
3506 if (ret) {
3507 mlog_errno(ret);
3508 goto out;
3509 }
3510 }
3511
3512 if (ctxt->c_split_covers_rec) {
3513 /*
3514 * The merge may have left an empty extent in
3515 * our leaf. Try to rotate it away.
3516 */
Tao Ma677b9752008-01-30 14:21:05 +08003517 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003518 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003519 if (ret)
3520 mlog_errno(ret);
3521 ret = 0;
3522 }
3523 }
3524
3525out:
3526 return ret;
3527}
3528
3529static void ocfs2_subtract_from_rec(struct super_block *sb,
3530 enum ocfs2_split_type split,
3531 struct ocfs2_extent_rec *rec,
3532 struct ocfs2_extent_rec *split_rec)
3533{
3534 u64 len_blocks;
3535
3536 len_blocks = ocfs2_clusters_to_blocks(sb,
3537 le16_to_cpu(split_rec->e_leaf_clusters));
3538
3539 if (split == SPLIT_LEFT) {
3540 /*
3541 * Region is on the left edge of the existing
3542 * record.
3543 */
3544 le32_add_cpu(&rec->e_cpos,
3545 le16_to_cpu(split_rec->e_leaf_clusters));
3546 le64_add_cpu(&rec->e_blkno, len_blocks);
3547 le16_add_cpu(&rec->e_leaf_clusters,
3548 -le16_to_cpu(split_rec->e_leaf_clusters));
3549 } else {
3550 /*
3551 * Region is on the right edge of the existing
3552 * record.
3553 */
3554 le16_add_cpu(&rec->e_leaf_clusters,
3555 -le16_to_cpu(split_rec->e_leaf_clusters));
3556 }
3557}
3558
Mark Fashehdcd05382007-01-16 11:32:23 -08003559/*
3560 * Do the final bits of extent record insertion at the target leaf
3561 * list. If this leaf is part of an allocation tree, it is assumed
3562 * that the tree above has been prepared.
3563 */
3564static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3565 struct ocfs2_extent_list *el,
3566 struct ocfs2_insert_type *insert,
3567 struct inode *inode)
3568{
3569 int i = insert->ins_contig_index;
3570 unsigned int range;
3571 struct ocfs2_extent_rec *rec;
3572
Mark Fashehe48edee2007-03-07 16:46:57 -08003573 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003574
Mark Fasheh328d5752007-06-18 10:48:04 -07003575 if (insert->ins_split != SPLIT_NONE) {
3576 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3577 BUG_ON(i == -1);
3578 rec = &el->l_recs[i];
3579 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3580 insert_rec);
3581 goto rotate;
3582 }
3583
Mark Fashehdcd05382007-01-16 11:32:23 -08003584 /*
3585 * Contiguous insert - either left or right.
3586 */
3587 if (insert->ins_contig != CONTIG_NONE) {
3588 rec = &el->l_recs[i];
3589 if (insert->ins_contig == CONTIG_LEFT) {
3590 rec->e_blkno = insert_rec->e_blkno;
3591 rec->e_cpos = insert_rec->e_cpos;
3592 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003593 le16_add_cpu(&rec->e_leaf_clusters,
3594 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003595 return;
3596 }
3597
3598 /*
3599 * Handle insert into an empty leaf.
3600 */
3601 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3602 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3603 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3604 el->l_recs[0] = *insert_rec;
3605 el->l_next_free_rec = cpu_to_le16(1);
3606 return;
3607 }
3608
3609 /*
3610 * Appending insert.
3611 */
3612 if (insert->ins_appending == APPEND_TAIL) {
3613 i = le16_to_cpu(el->l_next_free_rec) - 1;
3614 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003615 range = le32_to_cpu(rec->e_cpos)
3616 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003617 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3618
3619 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3620 le16_to_cpu(el->l_count),
3621 "inode %lu, depth %u, count %u, next free %u, "
3622 "rec.cpos %u, rec.clusters %u, "
3623 "insert.cpos %u, insert.clusters %u\n",
3624 inode->i_ino,
3625 le16_to_cpu(el->l_tree_depth),
3626 le16_to_cpu(el->l_count),
3627 le16_to_cpu(el->l_next_free_rec),
3628 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003629 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003630 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003631 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003632 i++;
3633 el->l_recs[i] = *insert_rec;
3634 le16_add_cpu(&el->l_next_free_rec, 1);
3635 return;
3636 }
3637
Mark Fasheh328d5752007-06-18 10:48:04 -07003638rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003639 /*
3640 * Ok, we have to rotate.
3641 *
3642 * At this point, it is safe to assume that inserting into an
3643 * empty leaf and appending to a leaf have both been handled
3644 * above.
3645 *
3646 * This leaf needs to have space, either by the empty 1st
3647 * extent record, or by virtue of an l_next_rec < l_count.
3648 */
3649 ocfs2_rotate_leaf(el, insert_rec);
3650}
3651
Mark Fasheh328d5752007-06-18 10:48:04 -07003652static void ocfs2_adjust_rightmost_records(struct inode *inode,
3653 handle_t *handle,
3654 struct ocfs2_path *path,
3655 struct ocfs2_extent_rec *insert_rec)
3656{
3657 int ret, i, next_free;
3658 struct buffer_head *bh;
3659 struct ocfs2_extent_list *el;
3660 struct ocfs2_extent_rec *rec;
3661
3662 /*
3663 * Update everything except the leaf block.
3664 */
3665 for (i = 0; i < path->p_tree_depth; i++) {
3666 bh = path->p_node[i].bh;
3667 el = path->p_node[i].el;
3668
3669 next_free = le16_to_cpu(el->l_next_free_rec);
3670 if (next_free == 0) {
3671 ocfs2_error(inode->i_sb,
3672 "Dinode %llu has a bad extent list",
3673 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3674 ret = -EIO;
3675 return;
3676 }
3677
3678 rec = &el->l_recs[next_free - 1];
3679
3680 rec->e_int_clusters = insert_rec->e_cpos;
3681 le32_add_cpu(&rec->e_int_clusters,
3682 le16_to_cpu(insert_rec->e_leaf_clusters));
3683 le32_add_cpu(&rec->e_int_clusters,
3684 -le32_to_cpu(rec->e_cpos));
3685
3686 ret = ocfs2_journal_dirty(handle, bh);
3687 if (ret)
3688 mlog_errno(ret);
3689
3690 }
3691}
3692
Mark Fashehdcd05382007-01-16 11:32:23 -08003693static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3694 struct ocfs2_extent_rec *insert_rec,
3695 struct ocfs2_path *right_path,
3696 struct ocfs2_path **ret_left_path)
3697{
Mark Fasheh328d5752007-06-18 10:48:04 -07003698 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003699 struct ocfs2_extent_list *el;
3700 struct ocfs2_path *left_path = NULL;
3701
3702 *ret_left_path = NULL;
3703
3704 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003705 * This shouldn't happen for non-trees. The extent rec cluster
3706 * count manipulation below only works for interior nodes.
3707 */
3708 BUG_ON(right_path->p_tree_depth == 0);
3709
3710 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003711 * If our appending insert is at the leftmost edge of a leaf,
3712 * then we might need to update the rightmost records of the
3713 * neighboring path.
3714 */
3715 el = path_leaf_el(right_path);
3716 next_free = le16_to_cpu(el->l_next_free_rec);
3717 if (next_free == 0 ||
3718 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3719 u32 left_cpos;
3720
3721 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3722 &left_cpos);
3723 if (ret) {
3724 mlog_errno(ret);
3725 goto out;
3726 }
3727
3728 mlog(0, "Append may need a left path update. cpos: %u, "
3729 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3730 left_cpos);
3731
3732 /*
3733 * No need to worry if the append is already in the
3734 * leftmost leaf.
3735 */
3736 if (left_cpos) {
3737 left_path = ocfs2_new_path(path_root_bh(right_path),
3738 path_root_el(right_path));
3739 if (!left_path) {
3740 ret = -ENOMEM;
3741 mlog_errno(ret);
3742 goto out;
3743 }
3744
3745 ret = ocfs2_find_path(inode, left_path, left_cpos);
3746 if (ret) {
3747 mlog_errno(ret);
3748 goto out;
3749 }
3750
3751 /*
3752 * ocfs2_insert_path() will pass the left_path to the
3753 * journal for us.
3754 */
3755 }
3756 }
3757
3758 ret = ocfs2_journal_access_path(inode, handle, right_path);
3759 if (ret) {
3760 mlog_errno(ret);
3761 goto out;
3762 }
3763
Mark Fasheh328d5752007-06-18 10:48:04 -07003764 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08003765
3766 *ret_left_path = left_path;
3767 ret = 0;
3768out:
3769 if (ret != 0)
3770 ocfs2_free_path(left_path);
3771
3772 return ret;
3773}
3774
Mark Fasheh328d5752007-06-18 10:48:04 -07003775static void ocfs2_split_record(struct inode *inode,
3776 struct ocfs2_path *left_path,
3777 struct ocfs2_path *right_path,
3778 struct ocfs2_extent_rec *split_rec,
3779 enum ocfs2_split_type split)
3780{
3781 int index;
3782 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3783 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3784 struct ocfs2_extent_rec *rec, *tmprec;
3785
3786 right_el = path_leaf_el(right_path);;
3787 if (left_path)
3788 left_el = path_leaf_el(left_path);
3789
3790 el = right_el;
3791 insert_el = right_el;
3792 index = ocfs2_search_extent_list(el, cpos);
3793 if (index != -1) {
3794 if (index == 0 && left_path) {
3795 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3796
3797 /*
3798 * This typically means that the record
3799 * started in the left path but moved to the
3800 * right as a result of rotation. We either
3801 * move the existing record to the left, or we
3802 * do the later insert there.
3803 *
3804 * In this case, the left path should always
3805 * exist as the rotate code will have passed
3806 * it back for a post-insert update.
3807 */
3808
3809 if (split == SPLIT_LEFT) {
3810 /*
3811 * It's a left split. Since we know
3812 * that the rotate code gave us an
3813 * empty extent in the left path, we
3814 * can just do the insert there.
3815 */
3816 insert_el = left_el;
3817 } else {
3818 /*
3819 * Right split - we have to move the
3820 * existing record over to the left
3821 * leaf. The insert will be into the
3822 * newly created empty extent in the
3823 * right leaf.
3824 */
3825 tmprec = &right_el->l_recs[index];
3826 ocfs2_rotate_leaf(left_el, tmprec);
3827 el = left_el;
3828
3829 memset(tmprec, 0, sizeof(*tmprec));
3830 index = ocfs2_search_extent_list(left_el, cpos);
3831 BUG_ON(index == -1);
3832 }
3833 }
3834 } else {
3835 BUG_ON(!left_path);
3836 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3837 /*
3838 * Left path is easy - we can just allow the insert to
3839 * happen.
3840 */
3841 el = left_el;
3842 insert_el = left_el;
3843 index = ocfs2_search_extent_list(el, cpos);
3844 BUG_ON(index == -1);
3845 }
3846
3847 rec = &el->l_recs[index];
3848 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3849 ocfs2_rotate_leaf(insert_el, split_rec);
3850}
3851
Mark Fashehdcd05382007-01-16 11:32:23 -08003852/*
Tao Mae7d4cb62008-08-18 17:38:44 +08003853 * This function only does inserts on an allocation b-tree. For tree
3854 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08003855 *
3856 * right_path is the path we want to do the actual insert
3857 * in. left_path should only be passed in if we need to update that
3858 * portion of the tree after an edge insert.
3859 */
3860static int ocfs2_insert_path(struct inode *inode,
3861 handle_t *handle,
3862 struct ocfs2_path *left_path,
3863 struct ocfs2_path *right_path,
3864 struct ocfs2_extent_rec *insert_rec,
3865 struct ocfs2_insert_type *insert)
3866{
3867 int ret, subtree_index;
3868 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003869
Mark Fashehdcd05382007-01-16 11:32:23 -08003870 if (left_path) {
3871 int credits = handle->h_buffer_credits;
3872
3873 /*
3874 * There's a chance that left_path got passed back to
3875 * us without being accounted for in the
3876 * journal. Extend our transaction here to be sure we
3877 * can change those blocks.
3878 */
3879 credits += left_path->p_tree_depth;
3880
3881 ret = ocfs2_extend_trans(handle, credits);
3882 if (ret < 0) {
3883 mlog_errno(ret);
3884 goto out;
3885 }
3886
3887 ret = ocfs2_journal_access_path(inode, handle, left_path);
3888 if (ret < 0) {
3889 mlog_errno(ret);
3890 goto out;
3891 }
3892 }
3893
Mark Fashehe8aed342007-12-03 16:43:01 -08003894 /*
3895 * Pass both paths to the journal. The majority of inserts
3896 * will be touching all components anyway.
3897 */
3898 ret = ocfs2_journal_access_path(inode, handle, right_path);
3899 if (ret < 0) {
3900 mlog_errno(ret);
3901 goto out;
3902 }
3903
Mark Fasheh328d5752007-06-18 10:48:04 -07003904 if (insert->ins_split != SPLIT_NONE) {
3905 /*
3906 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02003907 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07003908 * function sort it all out.
3909 */
3910 ocfs2_split_record(inode, left_path, right_path,
3911 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08003912
3913 /*
3914 * Split might have modified either leaf and we don't
3915 * have a guarantee that the later edge insert will
3916 * dirty this for us.
3917 */
3918 if (left_path)
3919 ret = ocfs2_journal_dirty(handle,
3920 path_leaf_bh(left_path));
3921 if (ret)
3922 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07003923 } else
3924 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3925 insert, inode);
Mark Fashehdcd05382007-01-16 11:32:23 -08003926
Mark Fashehdcd05382007-01-16 11:32:23 -08003927 ret = ocfs2_journal_dirty(handle, leaf_bh);
3928 if (ret)
3929 mlog_errno(ret);
3930
3931 if (left_path) {
3932 /*
3933 * The rotate code has indicated that we need to fix
3934 * up portions of the tree after the insert.
3935 *
3936 * XXX: Should we extend the transaction here?
3937 */
3938 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3939 right_path);
3940 ocfs2_complete_edge_insert(inode, handle, left_path,
3941 right_path, subtree_index);
3942 }
3943
3944 ret = 0;
3945out:
3946 return ret;
3947}
3948
3949static int ocfs2_do_insert_extent(struct inode *inode,
3950 handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003951 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08003952 struct ocfs2_extent_rec *insert_rec,
3953 struct ocfs2_insert_type *type)
3954{
3955 int ret, rotate = 0;
3956 u32 cpos;
3957 struct ocfs2_path *right_path = NULL;
3958 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08003959 struct ocfs2_extent_list *el;
3960
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003961 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08003962
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003963 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehdcd05382007-01-16 11:32:23 -08003964 OCFS2_JOURNAL_ACCESS_WRITE);
3965 if (ret) {
3966 mlog_errno(ret);
3967 goto out;
3968 }
3969
3970 if (le16_to_cpu(el->l_tree_depth) == 0) {
3971 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
3972 goto out_update_clusters;
3973 }
3974
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003975 right_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fashehdcd05382007-01-16 11:32:23 -08003976 if (!right_path) {
3977 ret = -ENOMEM;
3978 mlog_errno(ret);
3979 goto out;
3980 }
3981
3982 /*
3983 * Determine the path to start with. Rotations need the
3984 * rightmost path, everything else can go directly to the
3985 * target leaf.
3986 */
3987 cpos = le32_to_cpu(insert_rec->e_cpos);
3988 if (type->ins_appending == APPEND_NONE &&
3989 type->ins_contig == CONTIG_NONE) {
3990 rotate = 1;
3991 cpos = UINT_MAX;
3992 }
3993
3994 ret = ocfs2_find_path(inode, right_path, cpos);
3995 if (ret) {
3996 mlog_errno(ret);
3997 goto out;
3998 }
3999
4000 /*
4001 * Rotations and appends need special treatment - they modify
4002 * parts of the tree's above them.
4003 *
4004 * Both might pass back a path immediate to the left of the
4005 * one being inserted to. This will be cause
4006 * ocfs2_insert_path() to modify the rightmost records of
4007 * left_path to account for an edge insert.
4008 *
4009 * XXX: When modifying this code, keep in mind that an insert
4010 * can wind up skipping both of these two special cases...
4011 */
4012 if (rotate) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004013 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004014 le32_to_cpu(insert_rec->e_cpos),
4015 right_path, &left_path);
4016 if (ret) {
4017 mlog_errno(ret);
4018 goto out;
4019 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004020
4021 /*
4022 * ocfs2_rotate_tree_right() might have extended the
4023 * transaction without re-journaling our tree root.
4024 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004025 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehe8aed342007-12-03 16:43:01 -08004026 OCFS2_JOURNAL_ACCESS_WRITE);
4027 if (ret) {
4028 mlog_errno(ret);
4029 goto out;
4030 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004031 } else if (type->ins_appending == APPEND_TAIL
4032 && type->ins_contig != CONTIG_LEFT) {
4033 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4034 right_path, &left_path);
4035 if (ret) {
4036 mlog_errno(ret);
4037 goto out;
4038 }
4039 }
4040
4041 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4042 insert_rec, type);
4043 if (ret) {
4044 mlog_errno(ret);
4045 goto out;
4046 }
4047
4048out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004049 if (type->ins_split == SPLIT_NONE)
Joel Becker35dc0aa2008-08-20 16:25:06 -07004050 ocfs2_et_update_clusters(inode, et,
4051 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004052
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004053 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004054 if (ret)
4055 mlog_errno(ret);
4056
4057out:
4058 ocfs2_free_path(left_path);
4059 ocfs2_free_path(right_path);
4060
4061 return ret;
4062}
4063
Mark Fasheh328d5752007-06-18 10:48:04 -07004064static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08004065ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004066 struct ocfs2_extent_list *el, int index,
4067 struct ocfs2_extent_rec *split_rec)
4068{
Tao Maad5a4d72008-01-30 14:21:32 +08004069 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004070 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004071 u32 left_cpos, right_cpos;
4072 struct ocfs2_extent_rec *rec = NULL;
4073 struct ocfs2_extent_list *new_el;
4074 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4075 struct buffer_head *bh;
4076 struct ocfs2_extent_block *eb;
4077
4078 if (index > 0) {
4079 rec = &el->l_recs[index - 1];
4080 } else if (path->p_tree_depth > 0) {
4081 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4082 path, &left_cpos);
4083 if (status)
4084 goto out;
4085
4086 if (left_cpos != 0) {
4087 left_path = ocfs2_new_path(path_root_bh(path),
4088 path_root_el(path));
4089 if (!left_path)
4090 goto out;
4091
4092 status = ocfs2_find_path(inode, left_path, left_cpos);
4093 if (status)
4094 goto out;
4095
4096 new_el = path_leaf_el(left_path);
4097
4098 if (le16_to_cpu(new_el->l_next_free_rec) !=
4099 le16_to_cpu(new_el->l_count)) {
4100 bh = path_leaf_bh(left_path);
4101 eb = (struct ocfs2_extent_block *)bh->b_data;
4102 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4103 eb);
4104 goto out;
4105 }
4106 rec = &new_el->l_recs[
4107 le16_to_cpu(new_el->l_next_free_rec) - 1];
4108 }
4109 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004110
4111 /*
4112 * We're careful to check for an empty extent record here -
4113 * the merge code will know what to do if it sees one.
4114 */
Tao Maad5a4d72008-01-30 14:21:32 +08004115 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004116 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4117 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4118 ret = CONTIG_RIGHT;
4119 } else {
4120 ret = ocfs2_extent_contig(inode, rec, split_rec);
4121 }
4122 }
4123
Tao Maad5a4d72008-01-30 14:21:32 +08004124 rec = NULL;
4125 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4126 rec = &el->l_recs[index + 1];
4127 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4128 path->p_tree_depth > 0) {
4129 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4130 path, &right_cpos);
4131 if (status)
4132 goto out;
4133
4134 if (right_cpos == 0)
4135 goto out;
4136
4137 right_path = ocfs2_new_path(path_root_bh(path),
4138 path_root_el(path));
4139 if (!right_path)
4140 goto out;
4141
4142 status = ocfs2_find_path(inode, right_path, right_cpos);
4143 if (status)
4144 goto out;
4145
4146 new_el = path_leaf_el(right_path);
4147 rec = &new_el->l_recs[0];
4148 if (ocfs2_is_empty_extent(rec)) {
4149 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4150 bh = path_leaf_bh(right_path);
4151 eb = (struct ocfs2_extent_block *)bh->b_data;
4152 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4153 eb);
4154 goto out;
4155 }
4156 rec = &new_el->l_recs[1];
4157 }
4158 }
4159
4160 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004161 enum ocfs2_contig_type contig_type;
4162
Mark Fasheh328d5752007-06-18 10:48:04 -07004163 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4164
4165 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4166 ret = CONTIG_LEFTRIGHT;
4167 else if (ret == CONTIG_NONE)
4168 ret = contig_type;
4169 }
4170
Tao Maad5a4d72008-01-30 14:21:32 +08004171out:
4172 if (left_path)
4173 ocfs2_free_path(left_path);
4174 if (right_path)
4175 ocfs2_free_path(right_path);
4176
Mark Fasheh328d5752007-06-18 10:48:04 -07004177 return ret;
4178}
4179
Mark Fashehdcd05382007-01-16 11:32:23 -08004180static void ocfs2_figure_contig_type(struct inode *inode,
4181 struct ocfs2_insert_type *insert,
4182 struct ocfs2_extent_list *el,
Tao Maca12b7c2008-08-18 17:38:52 +08004183 struct ocfs2_extent_rec *insert_rec,
4184 struct ocfs2_extent_tree *et)
Mark Fashehdcd05382007-01-16 11:32:23 -08004185{
4186 int i;
4187 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4188
Mark Fashehe48edee2007-03-07 16:46:57 -08004189 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4190
Mark Fashehdcd05382007-01-16 11:32:23 -08004191 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4192 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4193 insert_rec);
4194 if (contig_type != CONTIG_NONE) {
4195 insert->ins_contig_index = i;
4196 break;
4197 }
4198 }
4199 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004200
4201 if (insert->ins_contig != CONTIG_NONE) {
4202 struct ocfs2_extent_rec *rec =
4203 &el->l_recs[insert->ins_contig_index];
4204 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4205 le16_to_cpu(insert_rec->e_leaf_clusters);
4206
4207 /*
4208 * Caller might want us to limit the size of extents, don't
4209 * calculate contiguousness if we might exceed that limit.
4210 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004211 if (et->et_max_leaf_clusters &&
4212 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004213 insert->ins_contig = CONTIG_NONE;
4214 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004215}
4216
4217/*
4218 * This should only be called against the righmost leaf extent list.
4219 *
4220 * ocfs2_figure_appending_type() will figure out whether we'll have to
4221 * insert at the tail of the rightmost leaf.
4222 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004223 * This should also work against the root extent list for tree's with 0
4224 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004225 * then the logic here makes sense.
4226 */
4227static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4228 struct ocfs2_extent_list *el,
4229 struct ocfs2_extent_rec *insert_rec)
4230{
4231 int i;
4232 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4233 struct ocfs2_extent_rec *rec;
4234
4235 insert->ins_appending = APPEND_NONE;
4236
Mark Fashehe48edee2007-03-07 16:46:57 -08004237 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004238
4239 if (!el->l_next_free_rec)
4240 goto set_tail_append;
4241
4242 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4243 /* Were all records empty? */
4244 if (le16_to_cpu(el->l_next_free_rec) == 1)
4245 goto set_tail_append;
4246 }
4247
4248 i = le16_to_cpu(el->l_next_free_rec) - 1;
4249 rec = &el->l_recs[i];
4250
Mark Fashehe48edee2007-03-07 16:46:57 -08004251 if (cpos >=
4252 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004253 goto set_tail_append;
4254
4255 return;
4256
4257set_tail_append:
4258 insert->ins_appending = APPEND_TAIL;
4259}
4260
4261/*
4262 * Helper function called at the begining of an insert.
4263 *
4264 * This computes a few things that are commonly used in the process of
4265 * inserting into the btree:
4266 * - Whether the new extent is contiguous with an existing one.
4267 * - The current tree depth.
4268 * - Whether the insert is an appending one.
4269 * - The total # of free records in the tree.
4270 *
4271 * All of the information is stored on the ocfs2_insert_type
4272 * structure.
4273 */
4274static int ocfs2_figure_insert_type(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004275 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004276 struct buffer_head **last_eb_bh,
4277 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004278 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004279 struct ocfs2_insert_type *insert)
4280{
4281 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004282 struct ocfs2_extent_block *eb;
4283 struct ocfs2_extent_list *el;
4284 struct ocfs2_path *path = NULL;
4285 struct buffer_head *bh = NULL;
4286
Mark Fasheh328d5752007-06-18 10:48:04 -07004287 insert->ins_split = SPLIT_NONE;
4288
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004289 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004290 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4291
4292 if (el->l_tree_depth) {
4293 /*
4294 * If we have tree depth, we read in the
4295 * rightmost extent block ahead of time as
4296 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4297 * may want it later.
4298 */
4299 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
Joel Becker35dc0aa2008-08-20 16:25:06 -07004300 ocfs2_et_get_last_eb_blk(et), &bh,
Mark Fashehdcd05382007-01-16 11:32:23 -08004301 OCFS2_BH_CACHED, inode);
4302 if (ret) {
4303 mlog_exit(ret);
4304 goto out;
4305 }
4306 eb = (struct ocfs2_extent_block *) bh->b_data;
4307 el = &eb->h_list;
4308 }
4309
4310 /*
4311 * Unless we have a contiguous insert, we'll need to know if
4312 * there is room left in our allocation tree for another
4313 * extent record.
4314 *
4315 * XXX: This test is simplistic, we can search for empty
4316 * extent records too.
4317 */
Tao Maoc77534f2007-08-28 17:22:33 -07004318 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004319 le16_to_cpu(el->l_next_free_rec);
4320
4321 if (!insert->ins_tree_depth) {
Tao Maca12b7c2008-08-18 17:38:52 +08004322 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004323 ocfs2_figure_appending_type(insert, el, insert_rec);
4324 return 0;
4325 }
4326
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004327 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fashehdcd05382007-01-16 11:32:23 -08004328 if (!path) {
4329 ret = -ENOMEM;
4330 mlog_errno(ret);
4331 goto out;
4332 }
4333
4334 /*
4335 * In the case that we're inserting past what the tree
4336 * currently accounts for, ocfs2_find_path() will return for
4337 * us the rightmost tree path. This is accounted for below in
4338 * the appending code.
4339 */
4340 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4341 if (ret) {
4342 mlog_errno(ret);
4343 goto out;
4344 }
4345
4346 el = path_leaf_el(path);
4347
4348 /*
4349 * Now that we have the path, there's two things we want to determine:
4350 * 1) Contiguousness (also set contig_index if this is so)
4351 *
4352 * 2) Are we doing an append? We can trivially break this up
4353 * into two types of appends: simple record append, or a
4354 * rotate inside the tail leaf.
4355 */
Tao Maca12b7c2008-08-18 17:38:52 +08004356 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004357
4358 /*
4359 * The insert code isn't quite ready to deal with all cases of
4360 * left contiguousness. Specifically, if it's an insert into
4361 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004362 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004363 * left. For now, just catch that case and fool the layers
4364 * above us. This works just fine for tree_depth == 0, which
4365 * is why we allow that above.
4366 */
4367 if (insert->ins_contig == CONTIG_LEFT &&
4368 insert->ins_contig_index == 0)
4369 insert->ins_contig = CONTIG_NONE;
4370
4371 /*
4372 * Ok, so we can simply compare against last_eb to figure out
4373 * whether the path doesn't exist. This will only happen in
4374 * the case that we're doing a tail append, so maybe we can
4375 * take advantage of that information somehow.
4376 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004377 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004378 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004379 /*
4380 * Ok, ocfs2_find_path() returned us the rightmost
4381 * tree path. This might be an appending insert. There are
4382 * two cases:
4383 * 1) We're doing a true append at the tail:
4384 * -This might even be off the end of the leaf
4385 * 2) We're "appending" by rotating in the tail
4386 */
4387 ocfs2_figure_appending_type(insert, el, insert_rec);
4388 }
4389
4390out:
4391 ocfs2_free_path(path);
4392
4393 if (ret == 0)
4394 *last_eb_bh = bh;
4395 else
4396 brelse(bh);
4397 return ret;
4398}
4399
4400/*
4401 * Insert an extent into an inode btree.
4402 *
4403 * The caller needs to update fe->i_clusters
4404 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004405int ocfs2_insert_extent(struct ocfs2_super *osb,
4406 handle_t *handle,
4407 struct inode *inode,
4408 struct ocfs2_extent_tree *et,
4409 u32 cpos,
4410 u64 start_blk,
4411 u32 new_clusters,
4412 u8 flags,
4413 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004414{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004415 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004416 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004417 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004418 struct ocfs2_insert_type insert = {0, };
4419 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004420
Mark Fashehdcd05382007-01-16 11:32:23 -08004421 mlog(0, "add %u clusters at position %u to inode %llu\n",
4422 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004423
Mark Fashehe48edee2007-03-07 16:46:57 -08004424 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004425 rec.e_cpos = cpu_to_le32(cpos);
4426 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004427 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004428 rec.e_flags = flags;
Joel Becker1e61ee72008-08-20 18:32:45 -07004429 status = ocfs2_et_insert_check(inode, et, &rec);
4430 if (status) {
4431 mlog_errno(status);
4432 goto bail;
4433 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004434
Tao Mae7d4cb62008-08-18 17:38:44 +08004435 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004436 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004437 if (status < 0) {
4438 mlog_errno(status);
4439 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004440 }
4441
Mark Fashehdcd05382007-01-16 11:32:23 -08004442 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4443 "Insert.contig_index: %d, Insert.free_records: %d, "
4444 "Insert.tree_depth: %d\n",
4445 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004446 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004447
Tao Maoc77534f2007-08-28 17:22:33 -07004448 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004449 status = ocfs2_grow_tree(inode, handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004450 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004451 meta_ac);
4452 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004453 mlog_errno(status);
4454 goto bail;
4455 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004456 }
4457
Mark Fashehdcd05382007-01-16 11:32:23 -08004458 /* Finally, we can add clusters. This might rotate the tree for us. */
Tao Mae7d4cb62008-08-18 17:38:44 +08004459 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004460 if (status < 0)
4461 mlog_errno(status);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004462 else if (et->et_ops == &ocfs2_dinode_et_ops)
Mark Fasheh83418972007-04-23 18:53:12 -07004463 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004464
4465bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004466 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004467
Tao Maf56654c2008-08-18 17:38:48 +08004468 mlog_exit(status);
4469 return status;
4470}
4471
Tao Ma0eb8d472008-08-18 17:38:45 +08004472/*
4473 * Allcate and add clusters into the extent b-tree.
4474 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004475 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004476 * it is not limited to the file storage. Any extent tree can use this
4477 * function if it implements the proper ocfs2_extent_tree.
4478 */
4479int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4480 struct inode *inode,
4481 u32 *logical_offset,
4482 u32 clusters_to_add,
4483 int mark_unwritten,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004484 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004485 handle_t *handle,
4486 struct ocfs2_alloc_context *data_ac,
4487 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004488 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004489{
4490 int status = 0;
4491 int free_extents;
4492 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4493 u32 bit_off, num_bits;
4494 u64 block;
4495 u8 flags = 0;
4496
4497 BUG_ON(!clusters_to_add);
4498
4499 if (mark_unwritten)
4500 flags = OCFS2_EXT_UNWRITTEN;
4501
Joel Beckerf99b9b72008-08-20 19:36:33 -07004502 free_extents = ocfs2_num_free_extents(osb, inode, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004503 if (free_extents < 0) {
4504 status = free_extents;
4505 mlog_errno(status);
4506 goto leave;
4507 }
4508
4509 /* there are two cases which could cause us to EAGAIN in the
4510 * we-need-more-metadata case:
4511 * 1) we haven't reserved *any*
4512 * 2) we are so fragmented, we've needed to add metadata too
4513 * many times. */
4514 if (!free_extents && !meta_ac) {
4515 mlog(0, "we haven't reserved any metadata!\n");
4516 status = -EAGAIN;
4517 reason = RESTART_META;
4518 goto leave;
4519 } else if ((!free_extents)
4520 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004521 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004522 mlog(0, "filesystem is really fragmented...\n");
4523 status = -EAGAIN;
4524 reason = RESTART_META;
4525 goto leave;
4526 }
4527
4528 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4529 clusters_to_add, &bit_off, &num_bits);
4530 if (status < 0) {
4531 if (status != -ENOSPC)
4532 mlog_errno(status);
4533 goto leave;
4534 }
4535
4536 BUG_ON(num_bits > clusters_to_add);
4537
4538 /* reserve our write early -- insert_extent may update the inode */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004539 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Tao Ma0eb8d472008-08-18 17:38:45 +08004540 OCFS2_JOURNAL_ACCESS_WRITE);
4541 if (status < 0) {
4542 mlog_errno(status);
4543 goto leave;
4544 }
4545
4546 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4547 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4548 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004549 status = ocfs2_insert_extent(osb, handle, inode, et,
4550 *logical_offset, block,
4551 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004552 if (status < 0) {
4553 mlog_errno(status);
4554 goto leave;
4555 }
4556
Joel Beckerf99b9b72008-08-20 19:36:33 -07004557 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004558 if (status < 0) {
4559 mlog_errno(status);
4560 goto leave;
4561 }
4562
4563 clusters_to_add -= num_bits;
4564 *logical_offset += num_bits;
4565
4566 if (clusters_to_add) {
4567 mlog(0, "need to alloc once more, wanted = %u\n",
4568 clusters_to_add);
4569 status = -EAGAIN;
4570 reason = RESTART_TRANS;
4571 }
4572
4573leave:
4574 mlog_exit(status);
4575 if (reason_ret)
4576 *reason_ret = reason;
4577 return status;
4578}
4579
Mark Fasheh328d5752007-06-18 10:48:04 -07004580static void ocfs2_make_right_split_rec(struct super_block *sb,
4581 struct ocfs2_extent_rec *split_rec,
4582 u32 cpos,
4583 struct ocfs2_extent_rec *rec)
4584{
4585 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4586 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4587
4588 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4589
4590 split_rec->e_cpos = cpu_to_le32(cpos);
4591 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4592
4593 split_rec->e_blkno = rec->e_blkno;
4594 le64_add_cpu(&split_rec->e_blkno,
4595 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4596
4597 split_rec->e_flags = rec->e_flags;
4598}
4599
4600static int ocfs2_split_and_insert(struct inode *inode,
4601 handle_t *handle,
4602 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004603 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004604 struct buffer_head **last_eb_bh,
4605 int split_index,
4606 struct ocfs2_extent_rec *orig_split_rec,
4607 struct ocfs2_alloc_context *meta_ac)
4608{
4609 int ret = 0, depth;
4610 unsigned int insert_range, rec_range, do_leftright = 0;
4611 struct ocfs2_extent_rec tmprec;
4612 struct ocfs2_extent_list *rightmost_el;
4613 struct ocfs2_extent_rec rec;
4614 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4615 struct ocfs2_insert_type insert;
4616 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004617
4618leftright:
4619 /*
4620 * Store a copy of the record on the stack - it might move
4621 * around as the tree is manipulated below.
4622 */
4623 rec = path_leaf_el(path)->l_recs[split_index];
4624
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004625 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004626
4627 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4628 if (depth) {
4629 BUG_ON(!(*last_eb_bh));
4630 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4631 rightmost_el = &eb->h_list;
4632 }
4633
4634 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4635 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004636 ret = ocfs2_grow_tree(inode, handle, et,
4637 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004638 if (ret) {
4639 mlog_errno(ret);
4640 goto out;
4641 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004642 }
4643
4644 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4645 insert.ins_appending = APPEND_NONE;
4646 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004647 insert.ins_tree_depth = depth;
4648
4649 insert_range = le32_to_cpu(split_rec.e_cpos) +
4650 le16_to_cpu(split_rec.e_leaf_clusters);
4651 rec_range = le32_to_cpu(rec.e_cpos) +
4652 le16_to_cpu(rec.e_leaf_clusters);
4653
4654 if (split_rec.e_cpos == rec.e_cpos) {
4655 insert.ins_split = SPLIT_LEFT;
4656 } else if (insert_range == rec_range) {
4657 insert.ins_split = SPLIT_RIGHT;
4658 } else {
4659 /*
4660 * Left/right split. We fake this as a right split
4661 * first and then make a second pass as a left split.
4662 */
4663 insert.ins_split = SPLIT_RIGHT;
4664
4665 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4666 &rec);
4667
4668 split_rec = tmprec;
4669
4670 BUG_ON(do_leftright);
4671 do_leftright = 1;
4672 }
4673
Tao Mae7d4cb62008-08-18 17:38:44 +08004674 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004675 if (ret) {
4676 mlog_errno(ret);
4677 goto out;
4678 }
4679
4680 if (do_leftright == 1) {
4681 u32 cpos;
4682 struct ocfs2_extent_list *el;
4683
4684 do_leftright++;
4685 split_rec = *orig_split_rec;
4686
4687 ocfs2_reinit_path(path, 1);
4688
4689 cpos = le32_to_cpu(split_rec.e_cpos);
4690 ret = ocfs2_find_path(inode, path, cpos);
4691 if (ret) {
4692 mlog_errno(ret);
4693 goto out;
4694 }
4695
4696 el = path_leaf_el(path);
4697 split_index = ocfs2_search_extent_list(el, cpos);
4698 goto leftright;
4699 }
4700out:
4701
4702 return ret;
4703}
4704
4705/*
4706 * Mark part or all of the extent record at split_index in the leaf
4707 * pointed to by path as written. This removes the unwritten
4708 * extent flag.
4709 *
4710 * Care is taken to handle contiguousness so as to not grow the tree.
4711 *
4712 * meta_ac is not strictly necessary - we only truly need it if growth
4713 * of the tree is required. All other cases will degrade into a less
4714 * optimal tree layout.
4715 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004716 * last_eb_bh should be the rightmost leaf block for any extent
4717 * btree. Since a split may grow the tree or a merge might shrink it,
4718 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07004719 *
4720 * This code is optimized for readability - several passes might be
4721 * made over certain portions of the tree. All of those blocks will
4722 * have been brought into cache (and pinned via the journal), so the
4723 * extra overhead is not expressed in terms of disk reads.
4724 */
4725static int __ocfs2_mark_extent_written(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004726 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004727 handle_t *handle,
4728 struct ocfs2_path *path,
4729 int split_index,
4730 struct ocfs2_extent_rec *split_rec,
4731 struct ocfs2_alloc_context *meta_ac,
4732 struct ocfs2_cached_dealloc_ctxt *dealloc)
4733{
4734 int ret = 0;
4735 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004736 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07004737 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4738 struct ocfs2_merge_ctxt ctxt;
4739 struct ocfs2_extent_list *rightmost_el;
4740
Roel Kluin3cf0c502007-10-27 00:20:36 +02004741 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004742 ret = -EIO;
4743 mlog_errno(ret);
4744 goto out;
4745 }
4746
4747 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4748 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4749 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4750 ret = -EIO;
4751 mlog_errno(ret);
4752 goto out;
4753 }
4754
Tao Maad5a4d72008-01-30 14:21:32 +08004755 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07004756 split_index,
4757 split_rec);
4758
4759 /*
4760 * The core merge / split code wants to know how much room is
4761 * left in this inodes allocation tree, so we pass the
4762 * rightmost extent list.
4763 */
4764 if (path->p_tree_depth) {
4765 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004766
4767 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
Joel Becker35dc0aa2008-08-20 16:25:06 -07004768 ocfs2_et_get_last_eb_blk(et),
Mark Fasheh328d5752007-06-18 10:48:04 -07004769 &last_eb_bh, OCFS2_BH_CACHED, inode);
4770 if (ret) {
4771 mlog_exit(ret);
4772 goto out;
4773 }
4774
4775 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4776 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4777 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4778 ret = -EROFS;
4779 goto out;
4780 }
4781
4782 rightmost_el = &eb->h_list;
4783 } else
4784 rightmost_el = path_root_el(path);
4785
Mark Fasheh328d5752007-06-18 10:48:04 -07004786 if (rec->e_cpos == split_rec->e_cpos &&
4787 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4788 ctxt.c_split_covers_rec = 1;
4789 else
4790 ctxt.c_split_covers_rec = 0;
4791
4792 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4793
Mark Fasheh015452b2007-09-12 10:21:22 -07004794 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4795 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4796 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004797
4798 if (ctxt.c_contig_type == CONTIG_NONE) {
4799 if (ctxt.c_split_covers_rec)
4800 el->l_recs[split_index] = *split_rec;
4801 else
Tao Mae7d4cb62008-08-18 17:38:44 +08004802 ret = ocfs2_split_and_insert(inode, handle, path, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004803 &last_eb_bh, split_index,
4804 split_rec, meta_ac);
4805 if (ret)
4806 mlog_errno(ret);
4807 } else {
4808 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4809 split_index, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08004810 dealloc, &ctxt, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07004811 if (ret)
4812 mlog_errno(ret);
4813 }
4814
Mark Fasheh328d5752007-06-18 10:48:04 -07004815out:
4816 brelse(last_eb_bh);
4817 return ret;
4818}
4819
4820/*
4821 * Mark the already-existing extent at cpos as written for len clusters.
4822 *
4823 * If the existing extent is larger than the request, initiate a
4824 * split. An attempt will be made at merging with adjacent extents.
4825 *
4826 * The caller is responsible for passing down meta_ac if we'll need it.
4827 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004828int ocfs2_mark_extent_written(struct inode *inode,
4829 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004830 handle_t *handle, u32 cpos, u32 len, u32 phys,
4831 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004832 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07004833{
4834 int ret, index;
4835 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4836 struct ocfs2_extent_rec split_rec;
4837 struct ocfs2_path *left_path = NULL;
4838 struct ocfs2_extent_list *el;
4839
4840 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4841 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4842
4843 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4844 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4845 "that are being written to, but the feature bit "
4846 "is not set in the super block.",
4847 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4848 ret = -EROFS;
4849 goto out;
4850 }
4851
4852 /*
4853 * XXX: This should be fixed up so that we just re-insert the
4854 * next extent records.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004855 *
4856 * XXX: This is a hack on the extent tree, maybe it should be
4857 * an op?
Mark Fasheh328d5752007-06-18 10:48:04 -07004858 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004859 if (et->et_ops == &ocfs2_dinode_et_ops)
Tao Mae7d4cb62008-08-18 17:38:44 +08004860 ocfs2_extent_map_trunc(inode, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07004861
Joel Beckerf99b9b72008-08-20 19:36:33 -07004862 left_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fasheh328d5752007-06-18 10:48:04 -07004863 if (!left_path) {
4864 ret = -ENOMEM;
4865 mlog_errno(ret);
4866 goto out;
4867 }
4868
4869 ret = ocfs2_find_path(inode, left_path, cpos);
4870 if (ret) {
4871 mlog_errno(ret);
4872 goto out;
4873 }
4874 el = path_leaf_el(left_path);
4875
4876 index = ocfs2_search_extent_list(el, cpos);
4877 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4878 ocfs2_error(inode->i_sb,
4879 "Inode %llu has an extent at cpos %u which can no "
4880 "longer be found.\n",
4881 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4882 ret = -EROFS;
4883 goto out;
4884 }
4885
4886 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4887 split_rec.e_cpos = cpu_to_le32(cpos);
4888 split_rec.e_leaf_clusters = cpu_to_le16(len);
4889 split_rec.e_blkno = cpu_to_le64(start_blkno);
4890 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4891 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4892
Joel Beckerf99b9b72008-08-20 19:36:33 -07004893 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004894 index, &split_rec, meta_ac,
4895 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07004896 if (ret)
4897 mlog_errno(ret);
4898
4899out:
4900 ocfs2_free_path(left_path);
4901 return ret;
4902}
4903
Tao Mae7d4cb62008-08-18 17:38:44 +08004904static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07004905 handle_t *handle, struct ocfs2_path *path,
4906 int index, u32 new_range,
4907 struct ocfs2_alloc_context *meta_ac)
4908{
4909 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07004910 struct buffer_head *last_eb_bh = NULL;
4911 struct ocfs2_extent_block *eb;
4912 struct ocfs2_extent_list *rightmost_el, *el;
4913 struct ocfs2_extent_rec split_rec;
4914 struct ocfs2_extent_rec *rec;
4915 struct ocfs2_insert_type insert;
4916
4917 /*
4918 * Setup the record to split before we grow the tree.
4919 */
4920 el = path_leaf_el(path);
4921 rec = &el->l_recs[index];
4922 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4923
4924 depth = path->p_tree_depth;
4925 if (depth > 0) {
4926 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
Joel Becker35dc0aa2008-08-20 16:25:06 -07004927 ocfs2_et_get_last_eb_blk(et),
Mark Fashehd0c7d702007-07-03 13:27:22 -07004928 &last_eb_bh, OCFS2_BH_CACHED, inode);
4929 if (ret < 0) {
4930 mlog_errno(ret);
4931 goto out;
4932 }
4933
4934 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4935 rightmost_el = &eb->h_list;
4936 } else
4937 rightmost_el = path_leaf_el(path);
4938
Tao Ma811f9332008-08-18 17:38:43 +08004939 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004940 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07004941 ret = ocfs2_extend_trans(handle, credits);
4942 if (ret) {
4943 mlog_errno(ret);
4944 goto out;
4945 }
4946
4947 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4948 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004949 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07004950 meta_ac);
4951 if (ret) {
4952 mlog_errno(ret);
4953 goto out;
4954 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07004955 }
4956
4957 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4958 insert.ins_appending = APPEND_NONE;
4959 insert.ins_contig = CONTIG_NONE;
4960 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07004961 insert.ins_tree_depth = depth;
4962
Tao Mae7d4cb62008-08-18 17:38:44 +08004963 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07004964 if (ret)
4965 mlog_errno(ret);
4966
4967out:
4968 brelse(last_eb_bh);
4969 return ret;
4970}
4971
4972static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
4973 struct ocfs2_path *path, int index,
4974 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08004975 u32 cpos, u32 len,
4976 struct ocfs2_extent_tree *et)
Mark Fashehd0c7d702007-07-03 13:27:22 -07004977{
4978 int ret;
4979 u32 left_cpos, rec_range, trunc_range;
4980 int wants_rotate = 0, is_rightmost_tree_rec = 0;
4981 struct super_block *sb = inode->i_sb;
4982 struct ocfs2_path *left_path = NULL;
4983 struct ocfs2_extent_list *el = path_leaf_el(path);
4984 struct ocfs2_extent_rec *rec;
4985 struct ocfs2_extent_block *eb;
4986
4987 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004988 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07004989 if (ret) {
4990 mlog_errno(ret);
4991 goto out;
4992 }
4993
4994 index--;
4995 }
4996
4997 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
4998 path->p_tree_depth) {
4999 /*
5000 * Check whether this is the rightmost tree record. If
5001 * we remove all of this record or part of its right
5002 * edge then an update of the record lengths above it
5003 * will be required.
5004 */
5005 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5006 if (eb->h_next_leaf_blk == 0)
5007 is_rightmost_tree_rec = 1;
5008 }
5009
5010 rec = &el->l_recs[index];
5011 if (index == 0 && path->p_tree_depth &&
5012 le32_to_cpu(rec->e_cpos) == cpos) {
5013 /*
5014 * Changing the leftmost offset (via partial or whole
5015 * record truncate) of an interior (or rightmost) path
5016 * means we have to update the subtree that is formed
5017 * by this leaf and the one to it's left.
5018 *
5019 * There are two cases we can skip:
5020 * 1) Path is the leftmost one in our inode tree.
5021 * 2) The leaf is rightmost and will be empty after
5022 * we remove the extent record - the rotate code
5023 * knows how to update the newly formed edge.
5024 */
5025
5026 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5027 &left_cpos);
5028 if (ret) {
5029 mlog_errno(ret);
5030 goto out;
5031 }
5032
5033 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5034 left_path = ocfs2_new_path(path_root_bh(path),
5035 path_root_el(path));
5036 if (!left_path) {
5037 ret = -ENOMEM;
5038 mlog_errno(ret);
5039 goto out;
5040 }
5041
5042 ret = ocfs2_find_path(inode, left_path, left_cpos);
5043 if (ret) {
5044 mlog_errno(ret);
5045 goto out;
5046 }
5047 }
5048 }
5049
5050 ret = ocfs2_extend_rotate_transaction(handle, 0,
5051 handle->h_buffer_credits,
5052 path);
5053 if (ret) {
5054 mlog_errno(ret);
5055 goto out;
5056 }
5057
5058 ret = ocfs2_journal_access_path(inode, handle, path);
5059 if (ret) {
5060 mlog_errno(ret);
5061 goto out;
5062 }
5063
5064 ret = ocfs2_journal_access_path(inode, handle, left_path);
5065 if (ret) {
5066 mlog_errno(ret);
5067 goto out;
5068 }
5069
5070 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5071 trunc_range = cpos + len;
5072
5073 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5074 int next_free;
5075
5076 memset(rec, 0, sizeof(*rec));
5077 ocfs2_cleanup_merge(el, index);
5078 wants_rotate = 1;
5079
5080 next_free = le16_to_cpu(el->l_next_free_rec);
5081 if (is_rightmost_tree_rec && next_free > 1) {
5082 /*
5083 * We skip the edge update if this path will
5084 * be deleted by the rotate code.
5085 */
5086 rec = &el->l_recs[next_free - 1];
5087 ocfs2_adjust_rightmost_records(inode, handle, path,
5088 rec);
5089 }
5090 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5091 /* Remove leftmost portion of the record. */
5092 le32_add_cpu(&rec->e_cpos, len);
5093 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5094 le16_add_cpu(&rec->e_leaf_clusters, -len);
5095 } else if (rec_range == trunc_range) {
5096 /* Remove rightmost portion of the record */
5097 le16_add_cpu(&rec->e_leaf_clusters, -len);
5098 if (is_rightmost_tree_rec)
5099 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5100 } else {
5101 /* Caller should have trapped this. */
5102 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5103 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5104 le32_to_cpu(rec->e_cpos),
5105 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5106 BUG();
5107 }
5108
5109 if (left_path) {
5110 int subtree_index;
5111
5112 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5113 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5114 subtree_index);
5115 }
5116
5117 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5118
Tao Mae7d4cb62008-08-18 17:38:44 +08005119 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005120 if (ret) {
5121 mlog_errno(ret);
5122 goto out;
5123 }
5124
5125out:
5126 ocfs2_free_path(left_path);
5127 return ret;
5128}
5129
Joel Beckerf99b9b72008-08-20 19:36:33 -07005130int ocfs2_remove_extent(struct inode *inode,
5131 struct ocfs2_extent_tree *et,
Mark Fasheh063c4562007-07-03 13:34:11 -07005132 u32 cpos, u32 len, handle_t *handle,
5133 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005134 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005135{
5136 int ret, index;
5137 u32 rec_range, trunc_range;
5138 struct ocfs2_extent_rec *rec;
5139 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005140 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005141
5142 ocfs2_extent_map_trunc(inode, 0);
5143
Joel Beckerf99b9b72008-08-20 19:36:33 -07005144 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005145 if (!path) {
5146 ret = -ENOMEM;
5147 mlog_errno(ret);
5148 goto out;
5149 }
5150
5151 ret = ocfs2_find_path(inode, path, cpos);
5152 if (ret) {
5153 mlog_errno(ret);
5154 goto out;
5155 }
5156
5157 el = path_leaf_el(path);
5158 index = ocfs2_search_extent_list(el, cpos);
5159 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5160 ocfs2_error(inode->i_sb,
5161 "Inode %llu has an extent at cpos %u which can no "
5162 "longer be found.\n",
5163 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5164 ret = -EROFS;
5165 goto out;
5166 }
5167
5168 /*
5169 * We have 3 cases of extent removal:
5170 * 1) Range covers the entire extent rec
5171 * 2) Range begins or ends on one edge of the extent rec
5172 * 3) Range is in the middle of the extent rec (no shared edges)
5173 *
5174 * For case 1 we remove the extent rec and left rotate to
5175 * fill the hole.
5176 *
5177 * For case 2 we just shrink the existing extent rec, with a
5178 * tree update if the shrinking edge is also the edge of an
5179 * extent block.
5180 *
5181 * For case 3 we do a right split to turn the extent rec into
5182 * something case 2 can handle.
5183 */
5184 rec = &el->l_recs[index];
5185 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5186 trunc_range = cpos + len;
5187
5188 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5189
5190 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5191 "(cpos %u, len %u)\n",
5192 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5193 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5194
5195 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5196 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005197 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005198 if (ret) {
5199 mlog_errno(ret);
5200 goto out;
5201 }
5202 } else {
Joel Beckerf99b9b72008-08-20 19:36:33 -07005203 ret = ocfs2_split_tree(inode, et, handle, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005204 trunc_range, meta_ac);
5205 if (ret) {
5206 mlog_errno(ret);
5207 goto out;
5208 }
5209
5210 /*
5211 * The split could have manipulated the tree enough to
5212 * move the record location, so we have to look for it again.
5213 */
5214 ocfs2_reinit_path(path, 1);
5215
5216 ret = ocfs2_find_path(inode, path, cpos);
5217 if (ret) {
5218 mlog_errno(ret);
5219 goto out;
5220 }
5221
5222 el = path_leaf_el(path);
5223 index = ocfs2_search_extent_list(el, cpos);
5224 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5225 ocfs2_error(inode->i_sb,
5226 "Inode %llu: split at cpos %u lost record.",
5227 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5228 cpos);
5229 ret = -EROFS;
5230 goto out;
5231 }
5232
5233 /*
5234 * Double check our values here. If anything is fishy,
5235 * it's easier to catch it at the top level.
5236 */
5237 rec = &el->l_recs[index];
5238 rec_range = le32_to_cpu(rec->e_cpos) +
5239 ocfs2_rec_clusters(el, rec);
5240 if (rec_range != trunc_range) {
5241 ocfs2_error(inode->i_sb,
5242 "Inode %llu: error after split at cpos %u"
5243 "trunc len %u, existing record is (%u,%u)",
5244 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5245 cpos, len, le32_to_cpu(rec->e_cpos),
5246 ocfs2_rec_clusters(el, rec));
5247 ret = -EROFS;
5248 goto out;
5249 }
5250
5251 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005252 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005253 if (ret) {
5254 mlog_errno(ret);
5255 goto out;
5256 }
5257 }
5258
5259out:
5260 ocfs2_free_path(path);
5261 return ret;
5262}
5263
Mark Fasheh063c4562007-07-03 13:34:11 -07005264int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005265{
5266 struct buffer_head *tl_bh = osb->osb_tl_bh;
5267 struct ocfs2_dinode *di;
5268 struct ocfs2_truncate_log *tl;
5269
5270 di = (struct ocfs2_dinode *) tl_bh->b_data;
5271 tl = &di->id2.i_dealloc;
5272
5273 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5274 "slot %d, invalid truncate log parameters: used = "
5275 "%u, count = %u\n", osb->slot_num,
5276 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5277 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5278}
5279
5280static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5281 unsigned int new_start)
5282{
5283 unsigned int tail_index;
5284 unsigned int current_tail;
5285
5286 /* No records, nothing to coalesce */
5287 if (!le16_to_cpu(tl->tl_used))
5288 return 0;
5289
5290 tail_index = le16_to_cpu(tl->tl_used) - 1;
5291 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5292 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5293
5294 return current_tail == new_start;
5295}
5296
Mark Fasheh063c4562007-07-03 13:34:11 -07005297int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5298 handle_t *handle,
5299 u64 start_blk,
5300 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005301{
5302 int status, index;
5303 unsigned int start_cluster, tl_count;
5304 struct inode *tl_inode = osb->osb_tl_inode;
5305 struct buffer_head *tl_bh = osb->osb_tl_bh;
5306 struct ocfs2_dinode *di;
5307 struct ocfs2_truncate_log *tl;
5308
Mark Fashehb06970532006-03-03 10:24:33 -08005309 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5310 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005311
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005312 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005313
5314 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5315
5316 di = (struct ocfs2_dinode *) tl_bh->b_data;
5317 tl = &di->id2.i_dealloc;
5318 if (!OCFS2_IS_VALID_DINODE(di)) {
5319 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5320 status = -EIO;
5321 goto bail;
5322 }
5323
5324 tl_count = le16_to_cpu(tl->tl_count);
5325 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5326 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005327 "Truncate record count on #%llu invalid "
5328 "wanted %u, actual %u\n",
5329 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005330 ocfs2_truncate_recs_per_inode(osb->sb),
5331 le16_to_cpu(tl->tl_count));
5332
5333 /* Caller should have known to flush before calling us. */
5334 index = le16_to_cpu(tl->tl_used);
5335 if (index >= tl_count) {
5336 status = -ENOSPC;
5337 mlog_errno(status);
5338 goto bail;
5339 }
5340
5341 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5342 OCFS2_JOURNAL_ACCESS_WRITE);
5343 if (status < 0) {
5344 mlog_errno(status);
5345 goto bail;
5346 }
5347
5348 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005349 "%llu (index = %d)\n", num_clusters, start_cluster,
5350 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005351
5352 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5353 /*
5354 * Move index back to the record we are coalescing with.
5355 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5356 */
5357 index--;
5358
5359 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5360 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5361 index, le32_to_cpu(tl->tl_recs[index].t_start),
5362 num_clusters);
5363 } else {
5364 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5365 tl->tl_used = cpu_to_le16(index + 1);
5366 }
5367 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5368
5369 status = ocfs2_journal_dirty(handle, tl_bh);
5370 if (status < 0) {
5371 mlog_errno(status);
5372 goto bail;
5373 }
5374
5375bail:
5376 mlog_exit(status);
5377 return status;
5378}
5379
5380static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005381 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005382 struct inode *data_alloc_inode,
5383 struct buffer_head *data_alloc_bh)
5384{
5385 int status = 0;
5386 int i;
5387 unsigned int num_clusters;
5388 u64 start_blk;
5389 struct ocfs2_truncate_rec rec;
5390 struct ocfs2_dinode *di;
5391 struct ocfs2_truncate_log *tl;
5392 struct inode *tl_inode = osb->osb_tl_inode;
5393 struct buffer_head *tl_bh = osb->osb_tl_bh;
5394
5395 mlog_entry_void();
5396
5397 di = (struct ocfs2_dinode *) tl_bh->b_data;
5398 tl = &di->id2.i_dealloc;
5399 i = le16_to_cpu(tl->tl_used) - 1;
5400 while (i >= 0) {
5401 /* Caller has given us at least enough credits to
5402 * update the truncate log dinode */
5403 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5404 OCFS2_JOURNAL_ACCESS_WRITE);
5405 if (status < 0) {
5406 mlog_errno(status);
5407 goto bail;
5408 }
5409
5410 tl->tl_used = cpu_to_le16(i);
5411
5412 status = ocfs2_journal_dirty(handle, tl_bh);
5413 if (status < 0) {
5414 mlog_errno(status);
5415 goto bail;
5416 }
5417
5418 /* TODO: Perhaps we can calculate the bulk of the
5419 * credits up front rather than extending like
5420 * this. */
5421 status = ocfs2_extend_trans(handle,
5422 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5423 if (status < 0) {
5424 mlog_errno(status);
5425 goto bail;
5426 }
5427
5428 rec = tl->tl_recs[i];
5429 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5430 le32_to_cpu(rec.t_start));
5431 num_clusters = le32_to_cpu(rec.t_clusters);
5432
5433 /* if start_blk is not set, we ignore the record as
5434 * invalid. */
5435 if (start_blk) {
5436 mlog(0, "free record %d, start = %u, clusters = %u\n",
5437 i, le32_to_cpu(rec.t_start), num_clusters);
5438
5439 status = ocfs2_free_clusters(handle, data_alloc_inode,
5440 data_alloc_bh, start_blk,
5441 num_clusters);
5442 if (status < 0) {
5443 mlog_errno(status);
5444 goto bail;
5445 }
5446 }
5447 i--;
5448 }
5449
5450bail:
5451 mlog_exit(status);
5452 return status;
5453}
5454
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005455/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005456int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005457{
5458 int status;
5459 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005460 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005461 struct inode *tl_inode = osb->osb_tl_inode;
5462 struct inode *data_alloc_inode = NULL;
5463 struct buffer_head *tl_bh = osb->osb_tl_bh;
5464 struct buffer_head *data_alloc_bh = NULL;
5465 struct ocfs2_dinode *di;
5466 struct ocfs2_truncate_log *tl;
5467
5468 mlog_entry_void();
5469
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005470 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005471
5472 di = (struct ocfs2_dinode *) tl_bh->b_data;
5473 tl = &di->id2.i_dealloc;
5474 if (!OCFS2_IS_VALID_DINODE(di)) {
5475 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5476 status = -EIO;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005477 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005478 }
5479
5480 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005481 mlog(0, "Flush %u records from truncate log #%llu\n",
5482 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005483 if (!num_to_flush) {
5484 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005485 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005486 }
5487
5488 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5489 GLOBAL_BITMAP_SYSTEM_INODE,
5490 OCFS2_INVALID_SLOT);
5491 if (!data_alloc_inode) {
5492 status = -EINVAL;
5493 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005494 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005495 }
5496
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005497 mutex_lock(&data_alloc_inode->i_mutex);
5498
Mark Fashehe63aecb62007-10-18 15:30:42 -07005499 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005500 if (status < 0) {
5501 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005502 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005503 }
5504
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005505 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005506 if (IS_ERR(handle)) {
5507 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005508 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005509 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005510 }
5511
5512 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5513 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005514 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005515 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005516
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005517 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005518
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005519out_unlock:
5520 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005521 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005522
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005523out_mutex:
5524 mutex_unlock(&data_alloc_inode->i_mutex);
5525 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005526
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005527out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005528 mlog_exit(status);
5529 return status;
5530}
5531
5532int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5533{
5534 int status;
5535 struct inode *tl_inode = osb->osb_tl_inode;
5536
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005537 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005538 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005539 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005540
5541 return status;
5542}
5543
David Howellsc4028952006-11-22 14:57:56 +00005544static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005545{
5546 int status;
David Howellsc4028952006-11-22 14:57:56 +00005547 struct ocfs2_super *osb =
5548 container_of(work, struct ocfs2_super,
5549 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005550
5551 mlog_entry_void();
5552
5553 status = ocfs2_flush_truncate_log(osb);
5554 if (status < 0)
5555 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005556 else
5557 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005558
5559 mlog_exit(status);
5560}
5561
5562#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5563void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5564 int cancel)
5565{
5566 if (osb->osb_tl_inode) {
5567 /* We want to push off log flushes while truncates are
5568 * still running. */
5569 if (cancel)
5570 cancel_delayed_work(&osb->osb_truncate_log_wq);
5571
5572 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5573 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5574 }
5575}
5576
5577static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5578 int slot_num,
5579 struct inode **tl_inode,
5580 struct buffer_head **tl_bh)
5581{
5582 int status;
5583 struct inode *inode = NULL;
5584 struct buffer_head *bh = NULL;
5585
5586 inode = ocfs2_get_system_file_inode(osb,
5587 TRUNCATE_LOG_SYSTEM_INODE,
5588 slot_num);
5589 if (!inode) {
5590 status = -EINVAL;
5591 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5592 goto bail;
5593 }
5594
5595 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
5596 OCFS2_BH_CACHED, inode);
5597 if (status < 0) {
5598 iput(inode);
5599 mlog_errno(status);
5600 goto bail;
5601 }
5602
5603 *tl_inode = inode;
5604 *tl_bh = bh;
5605bail:
5606 mlog_exit(status);
5607 return status;
5608}
5609
5610/* called during the 1st stage of node recovery. we stamp a clean
5611 * truncate log and pass back a copy for processing later. if the
5612 * truncate log does not require processing, a *tl_copy is set to
5613 * NULL. */
5614int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5615 int slot_num,
5616 struct ocfs2_dinode **tl_copy)
5617{
5618 int status;
5619 struct inode *tl_inode = NULL;
5620 struct buffer_head *tl_bh = NULL;
5621 struct ocfs2_dinode *di;
5622 struct ocfs2_truncate_log *tl;
5623
5624 *tl_copy = NULL;
5625
5626 mlog(0, "recover truncate log from slot %d\n", slot_num);
5627
5628 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5629 if (status < 0) {
5630 mlog_errno(status);
5631 goto bail;
5632 }
5633
5634 di = (struct ocfs2_dinode *) tl_bh->b_data;
5635 tl = &di->id2.i_dealloc;
5636 if (!OCFS2_IS_VALID_DINODE(di)) {
5637 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
5638 status = -EIO;
5639 goto bail;
5640 }
5641
5642 if (le16_to_cpu(tl->tl_used)) {
5643 mlog(0, "We'll have %u logs to recover\n",
5644 le16_to_cpu(tl->tl_used));
5645
5646 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5647 if (!(*tl_copy)) {
5648 status = -ENOMEM;
5649 mlog_errno(status);
5650 goto bail;
5651 }
5652
5653 /* Assuming the write-out below goes well, this copy
5654 * will be passed back to recovery for processing. */
5655 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5656
5657 /* All we need to do to clear the truncate log is set
5658 * tl_used. */
5659 tl->tl_used = 0;
5660
5661 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5662 if (status < 0) {
5663 mlog_errno(status);
5664 goto bail;
5665 }
5666 }
5667
5668bail:
5669 if (tl_inode)
5670 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07005671 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005672
5673 if (status < 0 && (*tl_copy)) {
5674 kfree(*tl_copy);
5675 *tl_copy = NULL;
5676 }
5677
5678 mlog_exit(status);
5679 return status;
5680}
5681
5682int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5683 struct ocfs2_dinode *tl_copy)
5684{
5685 int status = 0;
5686 int i;
5687 unsigned int clusters, num_recs, start_cluster;
5688 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005689 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005690 struct inode *tl_inode = osb->osb_tl_inode;
5691 struct ocfs2_truncate_log *tl;
5692
5693 mlog_entry_void();
5694
5695 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5696 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5697 return -EINVAL;
5698 }
5699
5700 tl = &tl_copy->id2.i_dealloc;
5701 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005702 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07005703 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08005704
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005705 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005706 for(i = 0; i < num_recs; i++) {
5707 if (ocfs2_truncate_log_needs_flush(osb)) {
5708 status = __ocfs2_flush_truncate_log(osb);
5709 if (status < 0) {
5710 mlog_errno(status);
5711 goto bail_up;
5712 }
5713 }
5714
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005715 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005716 if (IS_ERR(handle)) {
5717 status = PTR_ERR(handle);
5718 mlog_errno(status);
5719 goto bail_up;
5720 }
5721
5722 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5723 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5724 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5725
5726 status = ocfs2_truncate_log_append(osb, handle,
5727 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005728 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005729 if (status < 0) {
5730 mlog_errno(status);
5731 goto bail_up;
5732 }
5733 }
5734
5735bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005736 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005737
5738 mlog_exit(status);
5739 return status;
5740}
5741
5742void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5743{
5744 int status;
5745 struct inode *tl_inode = osb->osb_tl_inode;
5746
5747 mlog_entry_void();
5748
5749 if (tl_inode) {
5750 cancel_delayed_work(&osb->osb_truncate_log_wq);
5751 flush_workqueue(ocfs2_wq);
5752
5753 status = ocfs2_flush_truncate_log(osb);
5754 if (status < 0)
5755 mlog_errno(status);
5756
5757 brelse(osb->osb_tl_bh);
5758 iput(osb->osb_tl_inode);
5759 }
5760
5761 mlog_exit_void();
5762}
5763
5764int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5765{
5766 int status;
5767 struct inode *tl_inode = NULL;
5768 struct buffer_head *tl_bh = NULL;
5769
5770 mlog_entry_void();
5771
5772 status = ocfs2_get_truncate_log_info(osb,
5773 osb->slot_num,
5774 &tl_inode,
5775 &tl_bh);
5776 if (status < 0)
5777 mlog_errno(status);
5778
5779 /* ocfs2_truncate_log_shutdown keys on the existence of
5780 * osb->osb_tl_inode so we don't set any of the osb variables
5781 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00005782 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5783 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08005784 osb->osb_tl_bh = tl_bh;
5785 osb->osb_tl_inode = tl_inode;
5786
5787 mlog_exit(status);
5788 return status;
5789}
5790
Mark Fasheh2b604352007-06-22 15:45:27 -07005791/*
5792 * Delayed de-allocation of suballocator blocks.
5793 *
5794 * Some sets of block de-allocations might involve multiple suballocator inodes.
5795 *
5796 * The locking for this can get extremely complicated, especially when
5797 * the suballocator inodes to delete from aren't known until deep
5798 * within an unrelated codepath.
5799 *
5800 * ocfs2_extent_block structures are a good example of this - an inode
5801 * btree could have been grown by any number of nodes each allocating
5802 * out of their own suballoc inode.
5803 *
5804 * These structures allow the delay of block de-allocation until a
5805 * later time, when locking of multiple cluster inodes won't cause
5806 * deadlock.
5807 */
5808
5809/*
5810 * Describes a single block free from a suballocator
5811 */
5812struct ocfs2_cached_block_free {
5813 struct ocfs2_cached_block_free *free_next;
5814 u64 free_blk;
5815 unsigned int free_bit;
5816};
5817
5818struct ocfs2_per_slot_free_list {
5819 struct ocfs2_per_slot_free_list *f_next_suballocator;
5820 int f_inode_type;
5821 int f_slot;
5822 struct ocfs2_cached_block_free *f_first;
5823};
5824
5825static int ocfs2_free_cached_items(struct ocfs2_super *osb,
5826 int sysfile_type,
5827 int slot,
5828 struct ocfs2_cached_block_free *head)
5829{
5830 int ret;
5831 u64 bg_blkno;
5832 handle_t *handle;
5833 struct inode *inode;
5834 struct buffer_head *di_bh = NULL;
5835 struct ocfs2_cached_block_free *tmp;
5836
5837 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5838 if (!inode) {
5839 ret = -EINVAL;
5840 mlog_errno(ret);
5841 goto out;
5842 }
5843
5844 mutex_lock(&inode->i_mutex);
5845
Mark Fashehe63aecb62007-10-18 15:30:42 -07005846 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07005847 if (ret) {
5848 mlog_errno(ret);
5849 goto out_mutex;
5850 }
5851
5852 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5853 if (IS_ERR(handle)) {
5854 ret = PTR_ERR(handle);
5855 mlog_errno(ret);
5856 goto out_unlock;
5857 }
5858
5859 while (head) {
5860 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5861 head->free_bit);
5862 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5863 head->free_bit, (unsigned long long)head->free_blk);
5864
5865 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5866 head->free_bit, bg_blkno, 1);
5867 if (ret) {
5868 mlog_errno(ret);
5869 goto out_journal;
5870 }
5871
5872 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
5873 if (ret) {
5874 mlog_errno(ret);
5875 goto out_journal;
5876 }
5877
5878 tmp = head;
5879 head = head->free_next;
5880 kfree(tmp);
5881 }
5882
5883out_journal:
5884 ocfs2_commit_trans(osb, handle);
5885
5886out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07005887 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07005888 brelse(di_bh);
5889out_mutex:
5890 mutex_unlock(&inode->i_mutex);
5891 iput(inode);
5892out:
5893 while(head) {
5894 /* Premature exit may have left some dangling items. */
5895 tmp = head;
5896 head = head->free_next;
5897 kfree(tmp);
5898 }
5899
5900 return ret;
5901}
5902
5903int ocfs2_run_deallocs(struct ocfs2_super *osb,
5904 struct ocfs2_cached_dealloc_ctxt *ctxt)
5905{
5906 int ret = 0, ret2;
5907 struct ocfs2_per_slot_free_list *fl;
5908
5909 if (!ctxt)
5910 return 0;
5911
5912 while (ctxt->c_first_suballocator) {
5913 fl = ctxt->c_first_suballocator;
5914
5915 if (fl->f_first) {
5916 mlog(0, "Free items: (type %u, slot %d)\n",
5917 fl->f_inode_type, fl->f_slot);
5918 ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
5919 fl->f_slot, fl->f_first);
5920 if (ret2)
5921 mlog_errno(ret2);
5922 if (!ret)
5923 ret = ret2;
5924 }
5925
5926 ctxt->c_first_suballocator = fl->f_next_suballocator;
5927 kfree(fl);
5928 }
5929
5930 return ret;
5931}
5932
5933static struct ocfs2_per_slot_free_list *
5934ocfs2_find_per_slot_free_list(int type,
5935 int slot,
5936 struct ocfs2_cached_dealloc_ctxt *ctxt)
5937{
5938 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
5939
5940 while (fl) {
5941 if (fl->f_inode_type == type && fl->f_slot == slot)
5942 return fl;
5943
5944 fl = fl->f_next_suballocator;
5945 }
5946
5947 fl = kmalloc(sizeof(*fl), GFP_NOFS);
5948 if (fl) {
5949 fl->f_inode_type = type;
5950 fl->f_slot = slot;
5951 fl->f_first = NULL;
5952 fl->f_next_suballocator = ctxt->c_first_suballocator;
5953
5954 ctxt->c_first_suballocator = fl;
5955 }
5956 return fl;
5957}
5958
5959static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
5960 int type, int slot, u64 blkno,
5961 unsigned int bit)
5962{
5963 int ret;
5964 struct ocfs2_per_slot_free_list *fl;
5965 struct ocfs2_cached_block_free *item;
5966
5967 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
5968 if (fl == NULL) {
5969 ret = -ENOMEM;
5970 mlog_errno(ret);
5971 goto out;
5972 }
5973
5974 item = kmalloc(sizeof(*item), GFP_NOFS);
5975 if (item == NULL) {
5976 ret = -ENOMEM;
5977 mlog_errno(ret);
5978 goto out;
5979 }
5980
5981 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
5982 type, slot, bit, (unsigned long long)blkno);
5983
5984 item->free_blk = blkno;
5985 item->free_bit = bit;
5986 item->free_next = fl->f_first;
5987
5988 fl->f_first = item;
5989
5990 ret = 0;
5991out:
5992 return ret;
5993}
5994
Mark Fasheh59a5e412007-06-22 15:52:36 -07005995static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
5996 struct ocfs2_extent_block *eb)
5997{
5998 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
5999 le16_to_cpu(eb->h_suballoc_slot),
6000 le64_to_cpu(eb->h_blkno),
6001 le16_to_cpu(eb->h_suballoc_bit));
6002}
6003
Mark Fashehccd979b2005-12-15 14:31:24 -08006004/* This function will figure out whether the currently last extent
6005 * block will be deleted, and if it will, what the new last extent
6006 * block will be so we can update his h_next_leaf_blk field, as well
6007 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006008static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006009 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006010 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006011 struct buffer_head **new_last_eb)
6012{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006013 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006014 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006015 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006016 struct ocfs2_extent_block *eb;
6017 struct ocfs2_extent_list *el;
6018 struct buffer_head *bh = NULL;
6019
6020 *new_last_eb = NULL;
6021
Mark Fashehccd979b2005-12-15 14:31:24 -08006022 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006023 if (!path->p_tree_depth)
6024 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006025
6026 /* trunc to zero special case - this makes tree_depth = 0
6027 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006028 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006029 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006030
Mark Fashehdcd05382007-01-16 11:32:23 -08006031 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006032 BUG_ON(!el->l_next_free_rec);
6033
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006034 /*
6035 * Make sure that this extent list will actually be empty
6036 * after we clear away the data. We can shortcut out if
6037 * there's more than one non-empty extent in the
6038 * list. Otherwise, a check of the remaining extent is
6039 * necessary.
6040 */
6041 next_free = le16_to_cpu(el->l_next_free_rec);
6042 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006043 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006044 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006045 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006046
6047 /* We may have a valid extent in index 1, check it. */
6048 if (next_free == 2)
6049 rec = &el->l_recs[1];
6050
6051 /*
6052 * Fall through - no more nonempty extents, so we want
6053 * to delete this leaf.
6054 */
6055 } else {
6056 if (next_free > 1)
6057 goto out;
6058
6059 rec = &el->l_recs[0];
6060 }
6061
6062 if (rec) {
6063 /*
6064 * Check it we'll only be trimming off the end of this
6065 * cluster.
6066 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006067 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006068 goto out;
6069 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006070
Mark Fashehdcd05382007-01-16 11:32:23 -08006071 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6072 if (ret) {
6073 mlog_errno(ret);
6074 goto out;
6075 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006076
Mark Fashehdcd05382007-01-16 11:32:23 -08006077 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6078 if (ret) {
6079 mlog_errno(ret);
6080 goto out;
6081 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006082
Mark Fashehdcd05382007-01-16 11:32:23 -08006083 eb = (struct ocfs2_extent_block *) bh->b_data;
6084 el = &eb->h_list;
6085 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
6086 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
6087 ret = -EROFS;
6088 goto out;
6089 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006090
6091 *new_last_eb = bh;
6092 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006093 mlog(0, "returning block %llu, (cpos: %u)\n",
6094 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6095out:
6096 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006097
Mark Fashehdcd05382007-01-16 11:32:23 -08006098 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006099}
6100
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006101/*
6102 * Trim some clusters off the rightmost edge of a tree. Only called
6103 * during truncate.
6104 *
6105 * The caller needs to:
6106 * - start journaling of each path component.
6107 * - compute and fully set up any new last ext block
6108 */
6109static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6110 handle_t *handle, struct ocfs2_truncate_context *tc,
6111 u32 clusters_to_del, u64 *delete_start)
6112{
6113 int ret, i, index = path->p_tree_depth;
6114 u32 new_edge = 0;
6115 u64 deleted_eb = 0;
6116 struct buffer_head *bh;
6117 struct ocfs2_extent_list *el;
6118 struct ocfs2_extent_rec *rec;
6119
6120 *delete_start = 0;
6121
6122 while (index >= 0) {
6123 bh = path->p_node[index].bh;
6124 el = path->p_node[index].el;
6125
6126 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6127 index, (unsigned long long)bh->b_blocknr);
6128
6129 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6130
6131 if (index !=
6132 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6133 ocfs2_error(inode->i_sb,
6134 "Inode %lu has invalid ext. block %llu",
6135 inode->i_ino,
6136 (unsigned long long)bh->b_blocknr);
6137 ret = -EROFS;
6138 goto out;
6139 }
6140
6141find_tail_record:
6142 i = le16_to_cpu(el->l_next_free_rec) - 1;
6143 rec = &el->l_recs[i];
6144
6145 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6146 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006147 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006148 (unsigned long long)le64_to_cpu(rec->e_blkno),
6149 le16_to_cpu(el->l_next_free_rec));
6150
Mark Fashehe48edee2007-03-07 16:46:57 -08006151 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006152
6153 if (le16_to_cpu(el->l_tree_depth) == 0) {
6154 /*
6155 * If the leaf block contains a single empty
6156 * extent and no records, we can just remove
6157 * the block.
6158 */
6159 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6160 memset(rec, 0,
6161 sizeof(struct ocfs2_extent_rec));
6162 el->l_next_free_rec = cpu_to_le16(0);
6163
6164 goto delete;
6165 }
6166
6167 /*
6168 * Remove any empty extents by shifting things
6169 * left. That should make life much easier on
6170 * the code below. This condition is rare
6171 * enough that we shouldn't see a performance
6172 * hit.
6173 */
6174 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6175 le16_add_cpu(&el->l_next_free_rec, -1);
6176
6177 for(i = 0;
6178 i < le16_to_cpu(el->l_next_free_rec); i++)
6179 el->l_recs[i] = el->l_recs[i + 1];
6180
6181 memset(&el->l_recs[i], 0,
6182 sizeof(struct ocfs2_extent_rec));
6183
6184 /*
6185 * We've modified our extent list. The
6186 * simplest way to handle this change
6187 * is to being the search from the
6188 * start again.
6189 */
6190 goto find_tail_record;
6191 }
6192
Mark Fashehe48edee2007-03-07 16:46:57 -08006193 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006194
6195 /*
6196 * We'll use "new_edge" on our way back up the
6197 * tree to know what our rightmost cpos is.
6198 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006199 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006200 new_edge += le32_to_cpu(rec->e_cpos);
6201
6202 /*
6203 * The caller will use this to delete data blocks.
6204 */
6205 *delete_start = le64_to_cpu(rec->e_blkno)
6206 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006207 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006208
6209 /*
6210 * If it's now empty, remove this record.
6211 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006212 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006213 memset(rec, 0,
6214 sizeof(struct ocfs2_extent_rec));
6215 le16_add_cpu(&el->l_next_free_rec, -1);
6216 }
6217 } else {
6218 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6219 memset(rec, 0,
6220 sizeof(struct ocfs2_extent_rec));
6221 le16_add_cpu(&el->l_next_free_rec, -1);
6222
6223 goto delete;
6224 }
6225
6226 /* Can this actually happen? */
6227 if (le16_to_cpu(el->l_next_free_rec) == 0)
6228 goto delete;
6229
6230 /*
6231 * We never actually deleted any clusters
6232 * because our leaf was empty. There's no
6233 * reason to adjust the rightmost edge then.
6234 */
6235 if (new_edge == 0)
6236 goto delete;
6237
Mark Fashehe48edee2007-03-07 16:46:57 -08006238 rec->e_int_clusters = cpu_to_le32(new_edge);
6239 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006240 -le32_to_cpu(rec->e_cpos));
6241
6242 /*
6243 * A deleted child record should have been
6244 * caught above.
6245 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006246 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006247 }
6248
6249delete:
6250 ret = ocfs2_journal_dirty(handle, bh);
6251 if (ret) {
6252 mlog_errno(ret);
6253 goto out;
6254 }
6255
6256 mlog(0, "extent list container %llu, after: record %d: "
6257 "(%u, %u, %llu), next = %u.\n",
6258 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006259 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006260 (unsigned long long)le64_to_cpu(rec->e_blkno),
6261 le16_to_cpu(el->l_next_free_rec));
6262
6263 /*
6264 * We must be careful to only attempt delete of an
6265 * extent block (and not the root inode block).
6266 */
6267 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6268 struct ocfs2_extent_block *eb =
6269 (struct ocfs2_extent_block *)bh->b_data;
6270
6271 /*
6272 * Save this for use when processing the
6273 * parent block.
6274 */
6275 deleted_eb = le64_to_cpu(eb->h_blkno);
6276
6277 mlog(0, "deleting this extent block.\n");
6278
6279 ocfs2_remove_from_cache(inode, bh);
6280
Mark Fashehe48edee2007-03-07 16:46:57 -08006281 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006282 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6283 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6284
Mark Fasheh59a5e412007-06-22 15:52:36 -07006285 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6286 /* An error here is not fatal. */
6287 if (ret < 0)
6288 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006289 } else {
6290 deleted_eb = 0;
6291 }
6292
6293 index--;
6294 }
6295
6296 ret = 0;
6297out:
6298 return ret;
6299}
6300
Mark Fashehccd979b2005-12-15 14:31:24 -08006301static int ocfs2_do_truncate(struct ocfs2_super *osb,
6302 unsigned int clusters_to_del,
6303 struct inode *inode,
6304 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006305 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006306 struct ocfs2_truncate_context *tc,
6307 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006308{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006309 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006310 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006311 struct ocfs2_extent_block *last_eb = NULL;
6312 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006313 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006314 u64 delete_blk = 0;
6315
6316 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6317
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006318 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006319 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006320 if (status < 0) {
6321 mlog_errno(status);
6322 goto bail;
6323 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006324
Mark Fashehdcd05382007-01-16 11:32:23 -08006325 /*
6326 * Each component will be touched, so we might as well journal
6327 * here to avoid having to handle errors later.
6328 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006329 status = ocfs2_journal_access_path(inode, handle, path);
6330 if (status < 0) {
6331 mlog_errno(status);
6332 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006333 }
6334
6335 if (last_eb_bh) {
6336 status = ocfs2_journal_access(handle, inode, last_eb_bh,
6337 OCFS2_JOURNAL_ACCESS_WRITE);
6338 if (status < 0) {
6339 mlog_errno(status);
6340 goto bail;
6341 }
6342
6343 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6344 }
6345
6346 el = &(fe->id2.i_list);
6347
6348 /*
6349 * Lower levels depend on this never happening, but it's best
6350 * to check it up here before changing the tree.
6351 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006352 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006353 ocfs2_error(inode->i_sb,
6354 "Inode %lu has an empty extent record, depth %u\n",
6355 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006356 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006357 goto bail;
6358 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006359
6360 spin_lock(&OCFS2_I(inode)->ip_lock);
6361 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6362 clusters_to_del;
6363 spin_unlock(&OCFS2_I(inode)->ip_lock);
6364 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006365 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006366
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006367 status = ocfs2_trim_tree(inode, path, handle, tc,
6368 clusters_to_del, &delete_blk);
6369 if (status) {
6370 mlog_errno(status);
6371 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006372 }
6373
Mark Fashehdcd05382007-01-16 11:32:23 -08006374 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006375 /* trunc to zero is a special case. */
6376 el->l_tree_depth = 0;
6377 fe->i_last_eb_blk = 0;
6378 } else if (last_eb)
6379 fe->i_last_eb_blk = last_eb->h_blkno;
6380
6381 status = ocfs2_journal_dirty(handle, fe_bh);
6382 if (status < 0) {
6383 mlog_errno(status);
6384 goto bail;
6385 }
6386
6387 if (last_eb) {
6388 /* If there will be a new last extent block, then by
6389 * definition, there cannot be any leaves to the right of
6390 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006391 last_eb->h_next_leaf_blk = 0;
6392 status = ocfs2_journal_dirty(handle, last_eb_bh);
6393 if (status < 0) {
6394 mlog_errno(status);
6395 goto bail;
6396 }
6397 }
6398
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006399 if (delete_blk) {
6400 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6401 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006402 if (status < 0) {
6403 mlog_errno(status);
6404 goto bail;
6405 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006406 }
6407 status = 0;
6408bail:
Mark Fashehdcd05382007-01-16 11:32:23 -08006409
Mark Fashehccd979b2005-12-15 14:31:24 -08006410 mlog_exit(status);
6411 return status;
6412}
6413
Joel Becker2b4e30f2008-09-03 20:03:41 -07006414static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006415{
6416 set_buffer_uptodate(bh);
6417 mark_buffer_dirty(bh);
6418 return 0;
6419}
6420
Mark Fasheh1d410a62007-09-07 14:20:45 -07006421static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6422 unsigned int from, unsigned int to,
6423 struct page *page, int zero, u64 *phys)
6424{
6425 int ret, partial = 0;
6426
6427 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6428 if (ret)
6429 mlog_errno(ret);
6430
6431 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006432 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006433
6434 /*
6435 * Need to set the buffers we zero'd into uptodate
6436 * here if they aren't - ocfs2_map_page_blocks()
6437 * might've skipped some
6438 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006439 ret = walk_page_buffers(handle, page_buffers(page),
6440 from, to, &partial,
6441 ocfs2_zero_func);
6442 if (ret < 0)
6443 mlog_errno(ret);
6444 else if (ocfs2_should_order_data(inode)) {
6445 ret = ocfs2_jbd2_file_inode(handle, inode);
6446#ifdef CONFIG_OCFS2_COMPAT_JBD
Mark Fasheh1d410a62007-09-07 14:20:45 -07006447 ret = walk_page_buffers(handle, page_buffers(page),
6448 from, to, &partial,
Joel Becker2b4e30f2008-09-03 20:03:41 -07006449 ocfs2_journal_dirty_data);
6450#endif
Mark Fasheh1d410a62007-09-07 14:20:45 -07006451 if (ret < 0)
6452 mlog_errno(ret);
6453 }
6454
6455 if (!partial)
6456 SetPageUptodate(page);
6457
6458 flush_dcache_page(page);
6459}
6460
Mark Fasheh35edec12007-07-06 14:41:18 -07006461static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6462 loff_t end, struct page **pages,
6463 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006464{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006465 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006466 struct page *page;
6467 unsigned int from, to = PAGE_CACHE_SIZE;
6468 struct super_block *sb = inode->i_sb;
6469
6470 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6471
6472 if (numpages == 0)
6473 goto out;
6474
Mark Fasheh35edec12007-07-06 14:41:18 -07006475 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006476 for(i = 0; i < numpages; i++) {
6477 page = pages[i];
6478
Mark Fasheh35edec12007-07-06 14:41:18 -07006479 from = start & (PAGE_CACHE_SIZE - 1);
6480 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6481 to = end & (PAGE_CACHE_SIZE - 1);
6482
Mark Fasheh60b11392007-02-16 11:46:50 -08006483 BUG_ON(from > PAGE_CACHE_SIZE);
6484 BUG_ON(to > PAGE_CACHE_SIZE);
6485
Mark Fasheh1d410a62007-09-07 14:20:45 -07006486 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6487 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006488
Mark Fasheh35edec12007-07-06 14:41:18 -07006489 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006490 }
6491out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006492 if (pages)
6493 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006494}
6495
Mark Fasheh35edec12007-07-06 14:41:18 -07006496static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006497 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006498{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006499 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006500 struct super_block *sb = inode->i_sb;
6501 struct address_space *mapping = inode->i_mapping;
6502 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006503 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006504
Mark Fasheh35edec12007-07-06 14:41:18 -07006505 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006506
Mark Fasheh35edec12007-07-06 14:41:18 -07006507 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6508 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6509
Mark Fasheh1d410a62007-09-07 14:20:45 -07006510 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006511 last_page_bytes = PAGE_ALIGN(end);
6512 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006513 do {
6514 pages[numpages] = grab_cache_page(mapping, index);
6515 if (!pages[numpages]) {
6516 ret = -ENOMEM;
6517 mlog_errno(ret);
6518 goto out;
6519 }
6520
6521 numpages++;
6522 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006523 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006524
6525out:
6526 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006527 if (pages)
6528 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006529 numpages = 0;
6530 }
6531
6532 *num = numpages;
6533
6534 return ret;
6535}
6536
6537/*
6538 * Zero the area past i_size but still within an allocated
6539 * cluster. This avoids exposing nonzero data on subsequent file
6540 * extends.
6541 *
6542 * We need to call this before i_size is updated on the inode because
6543 * otherwise block_write_full_page() will skip writeout of pages past
6544 * i_size. The new_i_size parameter is passed for this reason.
6545 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006546int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6547 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006548{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006549 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006550 struct page **pages = NULL;
6551 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006552 unsigned int ext_flags;
6553 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006554
6555 /*
6556 * File systems which don't support sparse files zero on every
6557 * extend.
6558 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07006559 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08006560 return 0;
6561
Mark Fasheh1d410a62007-09-07 14:20:45 -07006562 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08006563 sizeof(struct page *), GFP_NOFS);
6564 if (pages == NULL) {
6565 ret = -ENOMEM;
6566 mlog_errno(ret);
6567 goto out;
6568 }
6569
Mark Fasheh1d410a62007-09-07 14:20:45 -07006570 if (range_start == range_end)
6571 goto out;
6572
6573 ret = ocfs2_extent_map_get_blocks(inode,
6574 range_start >> sb->s_blocksize_bits,
6575 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08006576 if (ret) {
6577 mlog_errno(ret);
6578 goto out;
6579 }
6580
Mark Fasheh1d410a62007-09-07 14:20:45 -07006581 /*
6582 * Tail is a hole, or is marked unwritten. In either case, we
6583 * can count on read and write to return/push zero's.
6584 */
6585 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08006586 goto out;
6587
Mark Fasheh1d410a62007-09-07 14:20:45 -07006588 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6589 &numpages);
6590 if (ret) {
6591 mlog_errno(ret);
6592 goto out;
6593 }
6594
Mark Fasheh35edec12007-07-06 14:41:18 -07006595 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6596 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08006597
6598 /*
6599 * Initiate writeout of the pages we zero'd here. We don't
6600 * wait on them - the truncate_inode_pages() call later will
6601 * do that for us.
6602 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006603 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6604 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08006605 if (ret)
6606 mlog_errno(ret);
6607
6608out:
6609 if (pages)
6610 kfree(pages);
6611
6612 return ret;
6613}
6614
Tiger Yangfdd77702008-08-18 17:08:55 +08006615static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6616 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006617{
6618 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08006619 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006620
Tiger Yangfdd77702008-08-18 17:08:55 +08006621 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6622 memset(&di->id2, 0, blocksize -
6623 offsetof(struct ocfs2_dinode, id2) -
6624 xattrsize);
6625 else
6626 memset(&di->id2, 0, blocksize -
6627 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006628}
6629
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006630void ocfs2_dinode_new_extent_list(struct inode *inode,
6631 struct ocfs2_dinode *di)
6632{
Tiger Yangfdd77702008-08-18 17:08:55 +08006633 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006634 di->id2.i_list.l_tree_depth = 0;
6635 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08006636 di->id2.i_list.l_count = cpu_to_le16(
6637 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006638}
6639
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006640void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6641{
6642 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6643 struct ocfs2_inline_data *idata = &di->id2.i_data;
6644
6645 spin_lock(&oi->ip_lock);
6646 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6647 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6648 spin_unlock(&oi->ip_lock);
6649
6650 /*
6651 * We clear the entire i_data structure here so that all
6652 * fields can be properly initialized.
6653 */
Tiger Yangfdd77702008-08-18 17:08:55 +08006654 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006655
Tiger Yangfdd77702008-08-18 17:08:55 +08006656 idata->id_count = cpu_to_le16(
6657 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006658}
6659
6660int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6661 struct buffer_head *di_bh)
6662{
6663 int ret, i, has_data, num_pages = 0;
6664 handle_t *handle;
6665 u64 uninitialized_var(block);
6666 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6667 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6668 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006669 struct ocfs2_alloc_context *data_ac = NULL;
6670 struct page **pages = NULL;
6671 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07006672 struct ocfs2_extent_tree et;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006673
6674 has_data = i_size_read(inode) ? 1 : 0;
6675
6676 if (has_data) {
6677 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6678 sizeof(struct page *), GFP_NOFS);
6679 if (pages == NULL) {
6680 ret = -ENOMEM;
6681 mlog_errno(ret);
6682 goto out;
6683 }
6684
6685 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6686 if (ret) {
6687 mlog_errno(ret);
6688 goto out;
6689 }
6690 }
6691
6692 handle = ocfs2_start_trans(osb, OCFS2_INLINE_TO_EXTENTS_CREDITS);
6693 if (IS_ERR(handle)) {
6694 ret = PTR_ERR(handle);
6695 mlog_errno(ret);
6696 goto out_unlock;
6697 }
6698
6699 ret = ocfs2_journal_access(handle, inode, di_bh,
6700 OCFS2_JOURNAL_ACCESS_WRITE);
6701 if (ret) {
6702 mlog_errno(ret);
6703 goto out_commit;
6704 }
6705
6706 if (has_data) {
6707 u32 bit_off, num;
6708 unsigned int page_end;
6709 u64 phys;
6710
6711 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
6712 &num);
6713 if (ret) {
6714 mlog_errno(ret);
6715 goto out_commit;
6716 }
6717
6718 /*
6719 * Save two copies, one for insert, and one that can
6720 * be changed by ocfs2_map_and_dirty_page() below.
6721 */
6722 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6723
6724 /*
6725 * Non sparse file systems zero on extend, so no need
6726 * to do that now.
6727 */
6728 if (!ocfs2_sparse_alloc(osb) &&
6729 PAGE_CACHE_SIZE < osb->s_clustersize)
6730 end = PAGE_CACHE_SIZE;
6731
6732 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6733 if (ret) {
6734 mlog_errno(ret);
6735 goto out_commit;
6736 }
6737
6738 /*
6739 * This should populate the 1st page for us and mark
6740 * it up to date.
6741 */
6742 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6743 if (ret) {
6744 mlog_errno(ret);
6745 goto out_commit;
6746 }
6747
6748 page_end = PAGE_CACHE_SIZE;
6749 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6750 page_end = osb->s_clustersize;
6751
6752 for (i = 0; i < num_pages; i++)
6753 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6754 pages[i], i > 0, &phys);
6755 }
6756
6757 spin_lock(&oi->ip_lock);
6758 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6759 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6760 spin_unlock(&oi->ip_lock);
6761
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006762 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006763
6764 ocfs2_journal_dirty(handle, di_bh);
6765
6766 if (has_data) {
6767 /*
6768 * An error at this point should be extremely rare. If
6769 * this proves to be false, we could always re-build
6770 * the in-inode data from our pages.
6771 */
Joel Becker8d6220d2008-08-22 12:46:09 -07006772 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07006773 ret = ocfs2_insert_extent(osb, handle, inode, &et,
6774 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006775 if (ret) {
6776 mlog_errno(ret);
6777 goto out_commit;
6778 }
6779
6780 inode->i_blocks = ocfs2_inode_sector_count(inode);
6781 }
6782
6783out_commit:
6784 ocfs2_commit_trans(osb, handle);
6785
6786out_unlock:
6787 if (data_ac)
6788 ocfs2_free_alloc_context(data_ac);
6789
6790out:
6791 if (pages) {
6792 ocfs2_unlock_and_free_pages(pages, num_pages);
6793 kfree(pages);
6794 }
6795
6796 return ret;
6797}
6798
Mark Fashehccd979b2005-12-15 14:31:24 -08006799/*
6800 * It is expected, that by the time you call this function,
6801 * inode->i_size and fe->i_size have been adjusted.
6802 *
6803 * WARNING: This will kfree the truncate context
6804 */
6805int ocfs2_commit_truncate(struct ocfs2_super *osb,
6806 struct inode *inode,
6807 struct buffer_head *fe_bh,
6808 struct ocfs2_truncate_context *tc)
6809{
6810 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006811 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08006812 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006813 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006814 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08006815 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08006816 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08006817
6818 mlog_entry_void();
6819
Mark Fashehdcd05382007-01-16 11:32:23 -08006820 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08006821 i_size_read(inode));
6822
Tao Mae7d4cb62008-08-18 17:38:44 +08006823 path = ocfs2_new_path(fe_bh, &di->id2.i_list);
Mark Fashehdcd05382007-01-16 11:32:23 -08006824 if (!path) {
6825 status = -ENOMEM;
6826 mlog_errno(status);
6827 goto bail;
6828 }
Mark Fasheh83418972007-04-23 18:53:12 -07006829
6830 ocfs2_extent_map_trunc(inode, new_highest_cpos);
6831
Mark Fashehccd979b2005-12-15 14:31:24 -08006832start:
Mark Fashehdcd05382007-01-16 11:32:23 -08006833 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006834 * Check that we still have allocation to delete.
6835 */
6836 if (OCFS2_I(inode)->ip_clusters == 0) {
6837 status = 0;
6838 goto bail;
6839 }
6840
6841 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08006842 * Truncate always works against the rightmost tree branch.
6843 */
6844 status = ocfs2_find_path(inode, path, UINT_MAX);
6845 if (status) {
6846 mlog_errno(status);
6847 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006848 }
6849
Mark Fashehdcd05382007-01-16 11:32:23 -08006850 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
6851 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
6852
6853 /*
6854 * By now, el will point to the extent list on the bottom most
6855 * portion of this tree. Only the tail record is considered in
6856 * each pass.
6857 *
6858 * We handle the following cases, in order:
6859 * - empty extent: delete the remaining branch
6860 * - remove the entire record
6861 * - remove a partial record
6862 * - no record needs to be removed (truncate has completed)
6863 */
6864 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006865 if (le16_to_cpu(el->l_next_free_rec) == 0) {
6866 ocfs2_error(inode->i_sb,
6867 "Inode %llu has empty extent block at %llu\n",
6868 (unsigned long long)OCFS2_I(inode)->ip_blkno,
6869 (unsigned long long)path_leaf_bh(path)->b_blocknr);
6870 status = -EROFS;
6871 goto bail;
6872 }
6873
Mark Fashehccd979b2005-12-15 14:31:24 -08006874 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08006875 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08006876 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08006877 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
6878 clusters_to_del = 0;
6879 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08006880 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08006881 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08006882 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08006883 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08006884 new_highest_cpos;
6885 } else {
6886 status = 0;
6887 goto bail;
6888 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006889
Mark Fashehdcd05382007-01-16 11:32:23 -08006890 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
6891 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
6892
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006893 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006894 tl_sem = 1;
6895 /* ocfs2_truncate_log_needs_flush guarantees us at least one
6896 * record is free for use. If there isn't any, we flush to get
6897 * an empty truncate log. */
6898 if (ocfs2_truncate_log_needs_flush(osb)) {
6899 status = __ocfs2_flush_truncate_log(osb);
6900 if (status < 0) {
6901 mlog_errno(status);
6902 goto bail;
6903 }
6904 }
6905
6906 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006907 (struct ocfs2_dinode *)fe_bh->b_data,
6908 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006909 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08006910 if (IS_ERR(handle)) {
6911 status = PTR_ERR(handle);
6912 handle = NULL;
6913 mlog_errno(status);
6914 goto bail;
6915 }
6916
Mark Fashehdcd05382007-01-16 11:32:23 -08006917 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
6918 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006919 if (status < 0) {
6920 mlog_errno(status);
6921 goto bail;
6922 }
6923
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006924 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006925 tl_sem = 0;
6926
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006927 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006928 handle = NULL;
6929
Mark Fashehdcd05382007-01-16 11:32:23 -08006930 ocfs2_reinit_path(path, 1);
6931
6932 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006933 * The check above will catch the case where we've truncated
6934 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08006935 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006936 goto start;
6937
Mark Fashehccd979b2005-12-15 14:31:24 -08006938bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08006939
6940 ocfs2_schedule_truncate_log_flush(osb, 1);
6941
6942 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006943 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006944
6945 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006946 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006947
Mark Fasheh59a5e412007-06-22 15:52:36 -07006948 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
6949
Mark Fashehdcd05382007-01-16 11:32:23 -08006950 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006951
6952 /* This will drop the ext_alloc cluster lock for us */
6953 ocfs2_free_truncate_context(tc);
6954
6955 mlog_exit(status);
6956 return status;
6957}
6958
Mark Fashehccd979b2005-12-15 14:31:24 -08006959/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07006960 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08006961 */
6962int ocfs2_prepare_truncate(struct ocfs2_super *osb,
6963 struct inode *inode,
6964 struct buffer_head *fe_bh,
6965 struct ocfs2_truncate_context **tc)
6966{
Mark Fasheh59a5e412007-06-22 15:52:36 -07006967 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006968 unsigned int new_i_clusters;
6969 struct ocfs2_dinode *fe;
6970 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08006971 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006972
6973 mlog_entry_void();
6974
6975 *tc = NULL;
6976
6977 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
6978 i_size_read(inode));
6979 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6980
6981 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006982 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
6983 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08006984
Robert P. J. Daycd861282006-12-13 00:34:52 -08006985 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08006986 if (!(*tc)) {
6987 status = -ENOMEM;
6988 mlog_errno(status);
6989 goto bail;
6990 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07006991 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08006992
Mark Fashehccd979b2005-12-15 14:31:24 -08006993 if (fe->id2.i_list.l_tree_depth) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006994 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
6995 &last_eb_bh, OCFS2_BH_CACHED, inode);
6996 if (status < 0) {
6997 mlog_errno(status);
6998 goto bail;
6999 }
7000 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7001 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
7002 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
7003
7004 brelse(last_eb_bh);
7005 status = -EIO;
7006 goto bail;
7007 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007008 }
7009
7010 (*tc)->tc_last_eb_bh = last_eb_bh;
7011
Mark Fashehccd979b2005-12-15 14:31:24 -08007012 status = 0;
7013bail:
7014 if (status < 0) {
7015 if (*tc)
7016 ocfs2_free_truncate_context(*tc);
7017 *tc = NULL;
7018 }
7019 mlog_exit_void();
7020 return status;
7021}
7022
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007023/*
7024 * 'start' is inclusive, 'end' is not.
7025 */
7026int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7027 unsigned int start, unsigned int end, int trunc)
7028{
7029 int ret;
7030 unsigned int numbytes;
7031 handle_t *handle;
7032 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7033 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7034 struct ocfs2_inline_data *idata = &di->id2.i_data;
7035
7036 if (end > i_size_read(inode))
7037 end = i_size_read(inode);
7038
7039 BUG_ON(start >= end);
7040
7041 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7042 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7043 !ocfs2_supports_inline_data(osb)) {
7044 ocfs2_error(inode->i_sb,
7045 "Inline data flags for inode %llu don't agree! "
7046 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7047 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7048 le16_to_cpu(di->i_dyn_features),
7049 OCFS2_I(inode)->ip_dyn_features,
7050 osb->s_feature_incompat);
7051 ret = -EROFS;
7052 goto out;
7053 }
7054
7055 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7056 if (IS_ERR(handle)) {
7057 ret = PTR_ERR(handle);
7058 mlog_errno(ret);
7059 goto out;
7060 }
7061
7062 ret = ocfs2_journal_access(handle, inode, di_bh,
7063 OCFS2_JOURNAL_ACCESS_WRITE);
7064 if (ret) {
7065 mlog_errno(ret);
7066 goto out_commit;
7067 }
7068
7069 numbytes = end - start;
7070 memset(idata->id_data + start, 0, numbytes);
7071
7072 /*
7073 * No need to worry about the data page here - it's been
7074 * truncated already and inline data doesn't need it for
7075 * pushing zero's to disk, so we'll let readpage pick it up
7076 * later.
7077 */
7078 if (trunc) {
7079 i_size_write(inode, start);
7080 di->i_size = cpu_to_le64(start);
7081 }
7082
7083 inode->i_blocks = ocfs2_inode_sector_count(inode);
7084 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7085
7086 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7087 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7088
7089 ocfs2_journal_dirty(handle, di_bh);
7090
7091out_commit:
7092 ocfs2_commit_trans(osb, handle);
7093
7094out:
7095 return ret;
7096}
7097
Mark Fashehccd979b2005-12-15 14:31:24 -08007098static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7099{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007100 /*
7101 * The caller is responsible for completing deallocation
7102 * before freeing the context.
7103 */
7104 if (tc->tc_dealloc.c_first_suballocator != NULL)
7105 mlog(ML_NOTICE,
7106 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007107
Mark Fasheha81cb882008-10-07 14:25:16 -07007108 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007109
7110 kfree(tc);
7111}