blob: ce54730e18ff284f65b1d4cff8eea365680c6f22 [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/*
53 * ocfs2_extent_tree and ocfs2_extent_tree_operations are used to abstract
54 * the b-tree operations in ocfs2. Now all the b-tree operations are not
55 * limited to ocfs2_dinode only. Any data which need to allocate clusters
56 * to store can use b-tree. And it only needs to implement its ocfs2_extent_tree
57 * and operation.
58 *
59 * ocfs2_extent_tree contains info for the root of the b-tree, it must have a
60 * root ocfs2_extent_list and a root_bh so that they can be used in the b-tree
61 * functions.
62 * ocfs2_extent_tree_operations abstract the normal operations we do for
63 * the root of extent b-tree.
64 */
65struct ocfs2_extent_tree;
66
67struct ocfs2_extent_tree_operations {
Joel Becker35dc0aa2008-08-20 16:25:06 -070068 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
69 u64 blkno);
70 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
71 void (*eo_update_clusters)(struct inode *inode,
72 struct ocfs2_extent_tree *et,
73 u32 new_clusters);
Joel Becker1e61ee72008-08-20 18:32:45 -070074 int (*eo_insert_check)(struct inode *inode,
75 struct ocfs2_extent_tree *et,
76 struct ocfs2_extent_rec *rec);
Joel Becker35dc0aa2008-08-20 16:25:06 -070077 int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
Joel Becker0ce10102008-08-20 17:19:50 -070078
79 /* These are internal to ocfs2_extent_tree and don't have
80 * accessor functions */
81 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
Joel Becker943cced2008-08-20 17:31:10 -070082 void (*eo_fill_max_leaf_clusters)(struct inode *inode,
83 struct ocfs2_extent_tree *et);
Tao Mae7d4cb62008-08-18 17:38:44 +080084};
85
86struct ocfs2_extent_tree {
Joel Beckerce1d9ea2008-08-20 16:30:07 -070087 enum ocfs2_extent_tree_type et_type;
88 struct ocfs2_extent_tree_operations *et_ops;
89 struct buffer_head *et_root_bh;
90 struct ocfs2_extent_list *et_root_el;
Joel Beckerea5efa12008-08-20 16:57:27 -070091 void *et_object;
Joel Beckerce1d9ea2008-08-20 16:30:07 -070092 unsigned int et_max_leaf_clusters;
Tao Mae7d4cb62008-08-18 17:38:44 +080093};
94
Joel Becker0ce10102008-08-20 17:19:50 -070095static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
96{
97 struct ocfs2_dinode *di = et->et_object;
98
99 et->et_root_el = &di->id2.i_list;
100}
101
Tao Mae7d4cb62008-08-18 17:38:44 +0800102static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
103 u64 blkno)
104{
Joel Beckerea5efa12008-08-20 16:57:27 -0700105 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800106
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700107 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
Tao Mae7d4cb62008-08-18 17:38:44 +0800108 di->i_last_eb_blk = cpu_to_le64(blkno);
109}
110
111static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
112{
Joel Beckerea5efa12008-08-20 16:57:27 -0700113 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800114
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700115 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
Tao Mae7d4cb62008-08-18 17:38:44 +0800116 return le64_to_cpu(di->i_last_eb_blk);
117}
118
119static void ocfs2_dinode_update_clusters(struct inode *inode,
120 struct ocfs2_extent_tree *et,
121 u32 clusters)
122{
Joel Beckerea5efa12008-08-20 16:57:27 -0700123 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800124
125 le32_add_cpu(&di->i_clusters, clusters);
126 spin_lock(&OCFS2_I(inode)->ip_lock);
127 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
128 spin_unlock(&OCFS2_I(inode)->ip_lock);
129}
130
Joel Becker1e61ee72008-08-20 18:32:45 -0700131static int ocfs2_dinode_insert_check(struct inode *inode,
132 struct ocfs2_extent_tree *et,
133 struct ocfs2_extent_rec *rec)
134{
135 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
136
137 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
138 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
139 (OCFS2_I(inode)->ip_clusters != rec->e_cpos),
140 "Device %s, asking for sparse allocation: inode %llu, "
141 "cpos %u, clusters %u\n",
142 osb->dev_str,
143 (unsigned long long)OCFS2_I(inode)->ip_blkno,
144 rec->e_cpos,
145 OCFS2_I(inode)->ip_clusters);
146
147 return 0;
148}
149
Tao Mae7d4cb62008-08-18 17:38:44 +0800150static int ocfs2_dinode_sanity_check(struct inode *inode,
151 struct ocfs2_extent_tree *et)
152{
153 int ret = 0;
154 struct ocfs2_dinode *di;
155
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700156 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
Tao Mae7d4cb62008-08-18 17:38:44 +0800157
Joel Beckerea5efa12008-08-20 16:57:27 -0700158 di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800159 if (!OCFS2_IS_VALID_DINODE(di)) {
160 ret = -EIO;
161 ocfs2_error(inode->i_sb,
162 "Inode %llu has invalid path root",
163 (unsigned long long)OCFS2_I(inode)->ip_blkno);
164 }
165
166 return ret;
167}
168
169static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700170 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
171 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
172 .eo_update_clusters = ocfs2_dinode_update_clusters,
Joel Becker1e61ee72008-08-20 18:32:45 -0700173 .eo_insert_check = ocfs2_dinode_insert_check,
Joel Becker35dc0aa2008-08-20 16:25:06 -0700174 .eo_sanity_check = ocfs2_dinode_sanity_check,
Joel Becker0ce10102008-08-20 17:19:50 -0700175 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
Tao Mae7d4cb62008-08-18 17:38:44 +0800176};
177
Joel Becker0ce10102008-08-20 17:19:50 -0700178static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
179{
180 struct ocfs2_xattr_value_root *xv = et->et_object;
181
182 et->et_root_el = &xv->xr_list;
183}
184
Tao Maf56654c2008-08-18 17:38:48 +0800185static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
186 u64 blkno)
187{
188 struct ocfs2_xattr_value_root *xv =
Joel Beckerea5efa12008-08-20 16:57:27 -0700189 (struct ocfs2_xattr_value_root *)et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800190
191 xv->xr_last_eb_blk = cpu_to_le64(blkno);
192}
193
194static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
195{
196 struct ocfs2_xattr_value_root *xv =
Joel Beckerea5efa12008-08-20 16:57:27 -0700197 (struct ocfs2_xattr_value_root *) et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800198
199 return le64_to_cpu(xv->xr_last_eb_blk);
200}
201
202static void ocfs2_xattr_value_update_clusters(struct inode *inode,
203 struct ocfs2_extent_tree *et,
204 u32 clusters)
205{
206 struct ocfs2_xattr_value_root *xv =
Joel Beckerea5efa12008-08-20 16:57:27 -0700207 (struct ocfs2_xattr_value_root *)et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800208
209 le32_add_cpu(&xv->xr_clusters, clusters);
210}
211
Joel Becker1a09f552008-08-20 17:44:24 -0700212static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700213 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
214 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
215 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700216 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800217};
218
Joel Becker0ce10102008-08-20 17:19:50 -0700219static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
220{
221 struct ocfs2_xattr_block *xb = et->et_object;
222
223 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
224}
225
Joel Becker943cced2008-08-20 17:31:10 -0700226static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode *inode,
227 struct ocfs2_extent_tree *et)
228{
229 et->et_max_leaf_clusters =
230 ocfs2_clusters_for_bytes(inode->i_sb,
231 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
232}
233
Tao Maba492612008-08-18 17:38:49 +0800234static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
235 u64 blkno)
236{
Joel Beckerea5efa12008-08-20 16:57:27 -0700237 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800238 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
239
240 xt->xt_last_eb_blk = cpu_to_le64(blkno);
241}
242
243static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
244{
Joel Beckerea5efa12008-08-20 16:57:27 -0700245 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800246 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
247
248 return le64_to_cpu(xt->xt_last_eb_blk);
249}
250
251static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
252 struct ocfs2_extent_tree *et,
253 u32 clusters)
254{
Joel Beckerea5efa12008-08-20 16:57:27 -0700255 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800256
257 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
258}
259
Tao Maba492612008-08-18 17:38:49 +0800260static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700261 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
262 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
263 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700264 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
Joel Becker943cced2008-08-20 17:31:10 -0700265 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
Tao Maba492612008-08-18 17:38:49 +0800266};
267
Joel Becker1a09f552008-08-20 17:44:24 -0700268static void __ocfs2_get_extent_tree(struct ocfs2_extent_tree *et,
269 struct inode *inode,
270 struct buffer_head *bh,
271 void *obj,
272 enum ocfs2_extent_tree_type et_type,
273 struct ocfs2_extent_tree_operations *ops)
Tao Mae7d4cb62008-08-18 17:38:44 +0800274{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700275 et->et_type = et_type;
Joel Becker1a09f552008-08-20 17:44:24 -0700276 et->et_ops = ops;
Tao Mae7d4cb62008-08-18 17:38:44 +0800277 get_bh(bh);
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700278 et->et_root_bh = bh;
Joel Beckerea5efa12008-08-20 16:57:27 -0700279 if (!obj)
280 obj = (void *)bh->b_data;
281 et->et_object = obj;
Tao Mae7d4cb62008-08-18 17:38:44 +0800282
Joel Becker0ce10102008-08-20 17:19:50 -0700283 et->et_ops->eo_fill_root_el(et);
Joel Becker943cced2008-08-20 17:31:10 -0700284 if (!et->et_ops->eo_fill_max_leaf_clusters)
285 et->et_max_leaf_clusters = 0;
286 else
287 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800288}
289
Joel Becker1a09f552008-08-20 17:44:24 -0700290static void ocfs2_get_dinode_extent_tree(struct ocfs2_extent_tree *et,
291 struct inode *inode,
292 struct buffer_head *bh)
293{
294 __ocfs2_get_extent_tree(et, inode, bh, NULL, OCFS2_DINODE_EXTENT,
295 &ocfs2_dinode_et_ops);
296}
297
298static void ocfs2_get_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
299 struct inode *inode,
300 struct buffer_head *bh)
301{
302 __ocfs2_get_extent_tree(et, inode, bh, NULL,
303 OCFS2_XATTR_TREE_EXTENT,
304 &ocfs2_xattr_tree_et_ops);
305}
306
307static void ocfs2_get_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
308 struct inode *inode,
309 struct buffer_head *bh,
310 struct ocfs2_xattr_value_root *xv)
311{
312 __ocfs2_get_extent_tree(et, inode, bh, xv,
313 OCFS2_XATTR_VALUE_EXTENT,
314 &ocfs2_xattr_value_et_ops);
315}
316
317static void ocfs2_get_extent_tree(struct ocfs2_extent_tree *et,
318 struct inode *inode,
319 struct buffer_head *bh,
320 enum ocfs2_extent_tree_type et_type,
321 void *obj)
322{
323 if (et_type == OCFS2_DINODE_EXTENT)
324 ocfs2_get_dinode_extent_tree(et, inode, bh);
325 else if (et_type == OCFS2_XATTR_VALUE_EXTENT)
326 ocfs2_get_xattr_tree_extent_tree(et, inode, bh);
327 else if (et_type == OCFS2_XATTR_TREE_EXTENT)
328 ocfs2_get_xattr_value_extent_tree(et, inode, bh, obj);
329 else
330 BUG();
331}
332
Joel Beckerdc0ce612008-08-20 16:48:35 -0700333static void ocfs2_put_extent_tree(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800334{
Joel Beckerdc0ce612008-08-20 16:48:35 -0700335 brelse(et->et_root_bh);
Tao Mae7d4cb62008-08-18 17:38:44 +0800336}
337
Joel Becker35dc0aa2008-08-20 16:25:06 -0700338static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
339 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800340{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700341 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800342}
343
Joel Becker35dc0aa2008-08-20 16:25:06 -0700344static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800345{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700346 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800347}
348
Joel Becker35dc0aa2008-08-20 16:25:06 -0700349static inline void ocfs2_et_update_clusters(struct inode *inode,
350 struct ocfs2_extent_tree *et,
351 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800352{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700353 et->et_ops->eo_update_clusters(inode, et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700354}
355
Joel Becker1e61ee72008-08-20 18:32:45 -0700356static inline int ocfs2_et_insert_check(struct inode *inode,
357 struct ocfs2_extent_tree *et,
358 struct ocfs2_extent_rec *rec)
359{
360 int ret = 0;
361
362 if (et->et_ops->eo_insert_check)
363 ret = et->et_ops->eo_insert_check(inode, et, rec);
364 return ret;
365}
366
Joel Becker35dc0aa2008-08-20 16:25:06 -0700367static inline int ocfs2_et_sanity_check(struct inode *inode,
368 struct ocfs2_extent_tree *et)
369{
Joel Becker1e61ee72008-08-20 18:32:45 -0700370 int ret = 0;
371
372 if (et->et_ops->eo_sanity_check)
373 ret = et->et_ops->eo_sanity_check(inode, et);
374 return ret;
Tao Mae7d4cb62008-08-18 17:38:44 +0800375}
376
Mark Fashehccd979b2005-12-15 14:31:24 -0800377static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
Mark Fasheh59a5e412007-06-22 15:52:36 -0700378static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
379 struct ocfs2_extent_block *eb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800380
Mark Fashehdcd05382007-01-16 11:32:23 -0800381/*
382 * Structures which describe a path through a btree, and functions to
383 * manipulate them.
384 *
385 * The idea here is to be as generic as possible with the tree
386 * manipulation code.
387 */
388struct ocfs2_path_item {
389 struct buffer_head *bh;
390 struct ocfs2_extent_list *el;
391};
392
393#define OCFS2_MAX_PATH_DEPTH 5
394
395struct ocfs2_path {
396 int p_tree_depth;
397 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
398};
399
400#define path_root_bh(_path) ((_path)->p_node[0].bh)
401#define path_root_el(_path) ((_path)->p_node[0].el)
402#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
403#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
404#define path_num_items(_path) ((_path)->p_tree_depth + 1)
405
406/*
407 * Reset the actual path elements so that we can re-use the structure
408 * to build another path. Generally, this involves freeing the buffer
409 * heads.
410 */
411static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
412{
413 int i, start = 0, depth = 0;
414 struct ocfs2_path_item *node;
415
416 if (keep_root)
417 start = 1;
418
419 for(i = start; i < path_num_items(path); i++) {
420 node = &path->p_node[i];
421
422 brelse(node->bh);
423 node->bh = NULL;
424 node->el = NULL;
425 }
426
427 /*
428 * Tree depth may change during truncate, or insert. If we're
429 * keeping the root extent list, then make sure that our path
430 * structure reflects the proper depth.
431 */
432 if (keep_root)
433 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
434
435 path->p_tree_depth = depth;
436}
437
438static void ocfs2_free_path(struct ocfs2_path *path)
439{
440 if (path) {
441 ocfs2_reinit_path(path, 0);
442 kfree(path);
443 }
444}
445
446/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700447 * All the elements of src into dest. After this call, src could be freed
448 * without affecting dest.
449 *
450 * Both paths should have the same root. Any non-root elements of dest
451 * will be freed.
452 */
453static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
454{
455 int i;
456
457 BUG_ON(path_root_bh(dest) != path_root_bh(src));
458 BUG_ON(path_root_el(dest) != path_root_el(src));
459
460 ocfs2_reinit_path(dest, 1);
461
462 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
463 dest->p_node[i].bh = src->p_node[i].bh;
464 dest->p_node[i].el = src->p_node[i].el;
465
466 if (dest->p_node[i].bh)
467 get_bh(dest->p_node[i].bh);
468 }
469}
470
471/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800472 * Make the *dest path the same as src and re-initialize src path to
473 * have a root only.
474 */
475static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
476{
477 int i;
478
479 BUG_ON(path_root_bh(dest) != path_root_bh(src));
480
481 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
482 brelse(dest->p_node[i].bh);
483
484 dest->p_node[i].bh = src->p_node[i].bh;
485 dest->p_node[i].el = src->p_node[i].el;
486
487 src->p_node[i].bh = NULL;
488 src->p_node[i].el = NULL;
489 }
490}
491
492/*
493 * Insert an extent block at given index.
494 *
495 * This will not take an additional reference on eb_bh.
496 */
497static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
498 struct buffer_head *eb_bh)
499{
500 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
501
502 /*
503 * Right now, no root bh is an extent block, so this helps
504 * catch code errors with dinode trees. The assertion can be
505 * safely removed if we ever need to insert extent block
506 * structures at the root.
507 */
508 BUG_ON(index == 0);
509
510 path->p_node[index].bh = eb_bh;
511 path->p_node[index].el = &eb->h_list;
512}
513
514static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
515 struct ocfs2_extent_list *root_el)
516{
517 struct ocfs2_path *path;
518
519 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
520
521 path = kzalloc(sizeof(*path), GFP_NOFS);
522 if (path) {
523 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
524 get_bh(root_bh);
525 path_root_bh(path) = root_bh;
526 path_root_el(path) = root_el;
527 }
528
529 return path;
530}
531
532/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800533 * Convenience function to journal all components in a path.
534 */
535static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
536 struct ocfs2_path *path)
537{
538 int i, ret = 0;
539
540 if (!path)
541 goto out;
542
543 for(i = 0; i < path_num_items(path); i++) {
544 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
545 OCFS2_JOURNAL_ACCESS_WRITE);
546 if (ret < 0) {
547 mlog_errno(ret);
548 goto out;
549 }
550 }
551
552out:
553 return ret;
554}
555
Mark Fasheh328d5752007-06-18 10:48:04 -0700556/*
557 * Return the index of the extent record which contains cluster #v_cluster.
558 * -1 is returned if it was not found.
559 *
560 * Should work fine on interior and exterior nodes.
561 */
562int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
563{
564 int ret = -1;
565 int i;
566 struct ocfs2_extent_rec *rec;
567 u32 rec_end, rec_start, clusters;
568
569 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
570 rec = &el->l_recs[i];
571
572 rec_start = le32_to_cpu(rec->e_cpos);
573 clusters = ocfs2_rec_clusters(el, rec);
574
575 rec_end = rec_start + clusters;
576
577 if (v_cluster >= rec_start && v_cluster < rec_end) {
578 ret = i;
579 break;
580 }
581 }
582
583 return ret;
584}
585
Mark Fashehdcd05382007-01-16 11:32:23 -0800586enum ocfs2_contig_type {
587 CONTIG_NONE = 0,
588 CONTIG_LEFT,
Mark Fasheh328d5752007-06-18 10:48:04 -0700589 CONTIG_RIGHT,
590 CONTIG_LEFTRIGHT,
Mark Fashehdcd05382007-01-16 11:32:23 -0800591};
592
Mark Fashehe48edee2007-03-07 16:46:57 -0800593
594/*
595 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
596 * ocfs2_extent_contig only work properly against leaf nodes!
597 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800598static int ocfs2_block_extent_contig(struct super_block *sb,
599 struct ocfs2_extent_rec *ext,
600 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800601{
Mark Fashehe48edee2007-03-07 16:46:57 -0800602 u64 blk_end = le64_to_cpu(ext->e_blkno);
603
604 blk_end += ocfs2_clusters_to_blocks(sb,
605 le16_to_cpu(ext->e_leaf_clusters));
606
607 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800608}
609
Mark Fashehdcd05382007-01-16 11:32:23 -0800610static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
611 struct ocfs2_extent_rec *right)
612{
Mark Fashehe48edee2007-03-07 16:46:57 -0800613 u32 left_range;
614
615 left_range = le32_to_cpu(left->e_cpos) +
616 le16_to_cpu(left->e_leaf_clusters);
617
618 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800619}
620
621static enum ocfs2_contig_type
622 ocfs2_extent_contig(struct inode *inode,
623 struct ocfs2_extent_rec *ext,
624 struct ocfs2_extent_rec *insert_rec)
625{
626 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
627
Mark Fasheh328d5752007-06-18 10:48:04 -0700628 /*
629 * Refuse to coalesce extent records with different flag
630 * fields - we don't want to mix unwritten extents with user
631 * data.
632 */
633 if (ext->e_flags != insert_rec->e_flags)
634 return CONTIG_NONE;
635
Mark Fashehdcd05382007-01-16 11:32:23 -0800636 if (ocfs2_extents_adjacent(ext, insert_rec) &&
637 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
638 return CONTIG_RIGHT;
639
640 blkno = le64_to_cpu(ext->e_blkno);
641 if (ocfs2_extents_adjacent(insert_rec, ext) &&
642 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
643 return CONTIG_LEFT;
644
645 return CONTIG_NONE;
646}
647
648/*
649 * NOTE: We can have pretty much any combination of contiguousness and
650 * appending.
651 *
652 * The usefulness of APPEND_TAIL is more in that it lets us know that
653 * we'll have to update the path to that leaf.
654 */
655enum ocfs2_append_type {
656 APPEND_NONE = 0,
657 APPEND_TAIL,
658};
659
Mark Fasheh328d5752007-06-18 10:48:04 -0700660enum ocfs2_split_type {
661 SPLIT_NONE = 0,
662 SPLIT_LEFT,
663 SPLIT_RIGHT,
664};
665
Mark Fashehdcd05382007-01-16 11:32:23 -0800666struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700667 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800668 enum ocfs2_append_type ins_appending;
669 enum ocfs2_contig_type ins_contig;
670 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800671 int ins_tree_depth;
672};
673
Mark Fasheh328d5752007-06-18 10:48:04 -0700674struct ocfs2_merge_ctxt {
675 enum ocfs2_contig_type c_contig_type;
676 int c_has_empty_extent;
677 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700678};
679
Mark Fashehccd979b2005-12-15 14:31:24 -0800680/*
681 * How many free extents have we got before we need more meta data?
682 */
683int ocfs2_num_free_extents(struct ocfs2_super *osb,
684 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +0800685 struct buffer_head *root_bh,
Tao Maf56654c2008-08-18 17:38:48 +0800686 enum ocfs2_extent_tree_type type,
Joel Beckerea5efa12008-08-20 16:57:27 -0700687 void *obj)
Mark Fashehccd979b2005-12-15 14:31:24 -0800688{
689 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800690 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800691 struct ocfs2_extent_block *eb;
692 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800693 u64 last_eb_blk = 0;
Joel Becker1c25d932008-08-20 17:09:42 -0700694 struct ocfs2_extent_tree et;
Mark Fashehccd979b2005-12-15 14:31:24 -0800695
696 mlog_entry_void();
697
Joel Becker1c25d932008-08-20 17:09:42 -0700698 ocfs2_get_extent_tree(&et, inode, root_bh, type, obj);
699 el = et.et_root_el;
700 last_eb_blk = ocfs2_et_get_last_eb_blk(&et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800701
Tao Mae7d4cb62008-08-18 17:38:44 +0800702 if (last_eb_blk) {
703 retval = ocfs2_read_block(osb, last_eb_blk,
Mark Fashehccd979b2005-12-15 14:31:24 -0800704 &eb_bh, OCFS2_BH_CACHED, inode);
705 if (retval < 0) {
706 mlog_errno(retval);
707 goto bail;
708 }
709 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
710 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800711 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800712
713 BUG_ON(el->l_tree_depth != 0);
714
715 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
716bail:
717 if (eb_bh)
718 brelse(eb_bh);
719
Joel Becker1c25d932008-08-20 17:09:42 -0700720 ocfs2_put_extent_tree(&et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800721 mlog_exit(retval);
722 return retval;
723}
724
725/* expects array to already be allocated
726 *
727 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
728 * l_count for you
729 */
730static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700731 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800732 struct inode *inode,
733 int wanted,
734 struct ocfs2_alloc_context *meta_ac,
735 struct buffer_head *bhs[])
736{
737 int count, status, i;
738 u16 suballoc_bit_start;
739 u32 num_got;
740 u64 first_blkno;
741 struct ocfs2_extent_block *eb;
742
743 mlog_entry_void();
744
745 count = 0;
746 while (count < wanted) {
747 status = ocfs2_claim_metadata(osb,
748 handle,
749 meta_ac,
750 wanted - count,
751 &suballoc_bit_start,
752 &num_got,
753 &first_blkno);
754 if (status < 0) {
755 mlog_errno(status);
756 goto bail;
757 }
758
759 for(i = count; i < (num_got + count); i++) {
760 bhs[i] = sb_getblk(osb->sb, first_blkno);
761 if (bhs[i] == NULL) {
762 status = -EIO;
763 mlog_errno(status);
764 goto bail;
765 }
766 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
767
768 status = ocfs2_journal_access(handle, inode, bhs[i],
769 OCFS2_JOURNAL_ACCESS_CREATE);
770 if (status < 0) {
771 mlog_errno(status);
772 goto bail;
773 }
774
775 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
776 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
777 /* Ok, setup the minimal stuff here. */
778 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
779 eb->h_blkno = cpu_to_le64(first_blkno);
780 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800781 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800782 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
783 eb->h_list.l_count =
784 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
785
786 suballoc_bit_start++;
787 first_blkno++;
788
789 /* We'll also be dirtied by the caller, so
790 * this isn't absolutely necessary. */
791 status = ocfs2_journal_dirty(handle, bhs[i]);
792 if (status < 0) {
793 mlog_errno(status);
794 goto bail;
795 }
796 }
797
798 count += num_got;
799 }
800
801 status = 0;
802bail:
803 if (status < 0) {
804 for(i = 0; i < wanted; i++) {
805 if (bhs[i])
806 brelse(bhs[i]);
807 bhs[i] = NULL;
808 }
809 }
810 mlog_exit(status);
811 return status;
812}
813
814/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800815 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
816 *
817 * Returns the sum of the rightmost extent rec logical offset and
818 * cluster count.
819 *
820 * ocfs2_add_branch() uses this to determine what logical cluster
821 * value should be populated into the leftmost new branch records.
822 *
823 * ocfs2_shift_tree_depth() uses this to determine the # clusters
824 * value for the new topmost tree record.
825 */
826static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
827{
828 int i;
829
830 i = le16_to_cpu(el->l_next_free_rec) - 1;
831
832 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -0800833 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -0800834}
835
836/*
Mark Fashehccd979b2005-12-15 14:31:24 -0800837 * Add an entire tree branch to our inode. eb_bh is the extent block
838 * to start at, if we don't want to start the branch at the dinode
839 * structure.
840 *
841 * last_eb_bh is required as we have to update it's next_leaf pointer
842 * for the new last extent block.
843 *
844 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -0800845 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -0800846 */
847static int ocfs2_add_branch(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700848 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800849 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +0800850 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -0800851 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -0700852 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800853 struct ocfs2_alloc_context *meta_ac)
854{
855 int status, new_blocks, i;
856 u64 next_blkno, new_last_eb_blk;
857 struct buffer_head *bh;
858 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800859 struct ocfs2_extent_block *eb;
860 struct ocfs2_extent_list *eb_el;
861 struct ocfs2_extent_list *el;
Mark Fashehdcd05382007-01-16 11:32:23 -0800862 u32 new_cpos;
Mark Fashehccd979b2005-12-15 14:31:24 -0800863
864 mlog_entry_void();
865
Mark Fasheh328d5752007-06-18 10:48:04 -0700866 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800867
Mark Fashehccd979b2005-12-15 14:31:24 -0800868 if (eb_bh) {
869 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
870 el = &eb->h_list;
871 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700872 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -0800873
874 /* we never add a branch to a leaf. */
875 BUG_ON(!el->l_tree_depth);
876
877 new_blocks = le16_to_cpu(el->l_tree_depth);
878
879 /* allocate the number of new eb blocks we need */
880 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
881 GFP_KERNEL);
882 if (!new_eb_bhs) {
883 status = -ENOMEM;
884 mlog_errno(status);
885 goto bail;
886 }
887
888 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
889 meta_ac, new_eb_bhs);
890 if (status < 0) {
891 mlog_errno(status);
892 goto bail;
893 }
894
Mark Fasheh328d5752007-06-18 10:48:04 -0700895 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
Mark Fashehdcd05382007-01-16 11:32:23 -0800896 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
897
Mark Fashehccd979b2005-12-15 14:31:24 -0800898 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
899 * linked with the rest of the tree.
900 * conversly, new_eb_bhs[0] is the new bottommost leaf.
901 *
902 * when we leave the loop, new_last_eb_blk will point to the
903 * newest leaf, and next_blkno will point to the topmost extent
904 * block. */
905 next_blkno = new_last_eb_blk = 0;
906 for(i = 0; i < new_blocks; i++) {
907 bh = new_eb_bhs[i];
908 eb = (struct ocfs2_extent_block *) bh->b_data;
909 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
910 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
911 status = -EIO;
912 goto bail;
913 }
914 eb_el = &eb->h_list;
915
916 status = ocfs2_journal_access(handle, inode, bh,
917 OCFS2_JOURNAL_ACCESS_CREATE);
918 if (status < 0) {
919 mlog_errno(status);
920 goto bail;
921 }
922
923 eb->h_next_leaf_blk = 0;
924 eb_el->l_tree_depth = cpu_to_le16(i);
925 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -0800926 /*
927 * This actually counts as an empty extent as
928 * c_clusters == 0
929 */
930 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -0800931 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -0800932 /*
933 * eb_el isn't always an interior node, but even leaf
934 * nodes want a zero'd flags and reserved field so
935 * this gets the whole 32 bits regardless of use.
936 */
937 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800938 if (!eb_el->l_tree_depth)
939 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
940
941 status = ocfs2_journal_dirty(handle, bh);
942 if (status < 0) {
943 mlog_errno(status);
944 goto bail;
945 }
946
947 next_blkno = le64_to_cpu(eb->h_blkno);
948 }
949
950 /* This is a bit hairy. We want to update up to three blocks
951 * here without leaving any of them in an inconsistent state
952 * in case of error. We don't have to worry about
953 * journal_dirty erroring as it won't unless we've aborted the
954 * handle (in which case we would never be here) so reserving
955 * the write with journal_access is all we need to do. */
Mark Fasheh328d5752007-06-18 10:48:04 -0700956 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800957 OCFS2_JOURNAL_ACCESS_WRITE);
958 if (status < 0) {
959 mlog_errno(status);
960 goto bail;
961 }
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700962 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800963 OCFS2_JOURNAL_ACCESS_WRITE);
964 if (status < 0) {
965 mlog_errno(status);
966 goto bail;
967 }
968 if (eb_bh) {
969 status = ocfs2_journal_access(handle, inode, eb_bh,
970 OCFS2_JOURNAL_ACCESS_WRITE);
971 if (status < 0) {
972 mlog_errno(status);
973 goto bail;
974 }
975 }
976
977 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +0800978 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800979 i = le16_to_cpu(el->l_next_free_rec);
980 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -0800981 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -0800982 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800983 le16_add_cpu(&el->l_next_free_rec, 1);
984
985 /* fe needs a new last extent block pointer, as does the
986 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -0700987 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -0800988
Mark Fasheh328d5752007-06-18 10:48:04 -0700989 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800990 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
991
Mark Fasheh328d5752007-06-18 10:48:04 -0700992 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800993 if (status < 0)
994 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700995 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800996 if (status < 0)
997 mlog_errno(status);
998 if (eb_bh) {
999 status = ocfs2_journal_dirty(handle, eb_bh);
1000 if (status < 0)
1001 mlog_errno(status);
1002 }
1003
Mark Fasheh328d5752007-06-18 10:48:04 -07001004 /*
1005 * Some callers want to track the rightmost leaf so pass it
1006 * back here.
1007 */
1008 brelse(*last_eb_bh);
1009 get_bh(new_eb_bhs[0]);
1010 *last_eb_bh = new_eb_bhs[0];
1011
Mark Fashehccd979b2005-12-15 14:31:24 -08001012 status = 0;
1013bail:
1014 if (new_eb_bhs) {
1015 for (i = 0; i < new_blocks; i++)
1016 if (new_eb_bhs[i])
1017 brelse(new_eb_bhs[i]);
1018 kfree(new_eb_bhs);
1019 }
1020
1021 mlog_exit(status);
1022 return status;
1023}
1024
1025/*
1026 * adds another level to the allocation tree.
1027 * returns back the new extent block so you can add a branch to it
1028 * after this call.
1029 */
1030static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001031 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001032 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001033 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001034 struct ocfs2_alloc_context *meta_ac,
1035 struct buffer_head **ret_new_eb_bh)
1036{
1037 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001038 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001039 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001040 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001041 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001042 struct ocfs2_extent_list *eb_el;
1043
1044 mlog_entry_void();
1045
1046 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
1047 &new_eb_bh);
1048 if (status < 0) {
1049 mlog_errno(status);
1050 goto bail;
1051 }
1052
1053 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
1054 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1055 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1056 status = -EIO;
1057 goto bail;
1058 }
1059
1060 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001061 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001062
1063 status = ocfs2_journal_access(handle, inode, new_eb_bh,
1064 OCFS2_JOURNAL_ACCESS_CREATE);
1065 if (status < 0) {
1066 mlog_errno(status);
1067 goto bail;
1068 }
1069
Tao Mae7d4cb62008-08-18 17:38:44 +08001070 /* copy the root extent list data into the new extent block */
1071 eb_el->l_tree_depth = root_el->l_tree_depth;
1072 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1073 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1074 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001075
1076 status = ocfs2_journal_dirty(handle, new_eb_bh);
1077 if (status < 0) {
1078 mlog_errno(status);
1079 goto bail;
1080 }
1081
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001082 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001083 OCFS2_JOURNAL_ACCESS_WRITE);
1084 if (status < 0) {
1085 mlog_errno(status);
1086 goto bail;
1087 }
1088
Mark Fashehdcd05382007-01-16 11:32:23 -08001089 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1090
Tao Mae7d4cb62008-08-18 17:38:44 +08001091 /* update root_bh now */
1092 le16_add_cpu(&root_el->l_tree_depth, 1);
1093 root_el->l_recs[0].e_cpos = 0;
1094 root_el->l_recs[0].e_blkno = eb->h_blkno;
1095 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1096 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1097 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1098 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001099
1100 /* If this is our 1st tree depth shift, then last_eb_blk
1101 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001102 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001103 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001104
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001105 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001106 if (status < 0) {
1107 mlog_errno(status);
1108 goto bail;
1109 }
1110
1111 *ret_new_eb_bh = new_eb_bh;
1112 new_eb_bh = NULL;
1113 status = 0;
1114bail:
1115 if (new_eb_bh)
1116 brelse(new_eb_bh);
1117
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
1176 if (bh) {
1177 brelse(bh);
1178 bh = NULL;
1179 }
1180
1181 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
1182 inode);
1183 if (status < 0) {
1184 mlog_errno(status);
1185 goto bail;
1186 }
1187
1188 eb = (struct ocfs2_extent_block *) bh->b_data;
1189 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1190 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1191 status = -EIO;
1192 goto bail;
1193 }
1194 el = &eb->h_list;
1195
1196 if (le16_to_cpu(el->l_next_free_rec) <
1197 le16_to_cpu(el->l_count)) {
1198 if (lowest_bh)
1199 brelse(lowest_bh);
1200 lowest_bh = bh;
1201 get_bh(lowest_bh);
1202 }
1203 }
1204
1205 /* If we didn't find one and the fe doesn't have any room,
1206 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001207 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001208 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001209 status = 1;
1210
1211 *target_bh = lowest_bh;
1212bail:
1213 if (bh)
1214 brelse(bh);
1215
1216 mlog_exit(status);
1217 return status;
1218}
1219
Mark Fashehe48edee2007-03-07 16:46:57 -08001220/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001221 * Grow a b-tree so that it has more records.
1222 *
1223 * We might shift the tree depth in which case existing paths should
1224 * be considered invalid.
1225 *
1226 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001227 *
1228 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001229 */
1230static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001231 struct ocfs2_extent_tree *et, int *final_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07001232 struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001233 struct ocfs2_alloc_context *meta_ac)
1234{
1235 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001236 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001237 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001238 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1239 struct buffer_head *bh = NULL;
1240
1241 BUG_ON(meta_ac == NULL);
1242
Tao Mae7d4cb62008-08-18 17:38:44 +08001243 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001244 if (shift < 0) {
1245 ret = shift;
1246 mlog_errno(ret);
1247 goto out;
1248 }
1249
1250 /* We traveled all the way to the bottom of the allocation tree
1251 * and didn't find room for any more extents - we need to add
1252 * another tree level */
1253 if (shift) {
1254 BUG_ON(bh);
1255 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1256
1257 /* ocfs2_shift_tree_depth will return us a buffer with
1258 * the new extent block (so we can pass that to
1259 * ocfs2_add_branch). */
Tao Mae7d4cb62008-08-18 17:38:44 +08001260 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001261 meta_ac, &bh);
1262 if (ret < 0) {
1263 mlog_errno(ret);
1264 goto out;
1265 }
1266 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001267 if (depth == 1) {
1268 /*
1269 * Special case: we have room now if we shifted from
1270 * tree_depth 0, so no more work needs to be done.
1271 *
1272 * We won't be calling add_branch, so pass
1273 * back *last_eb_bh as the new leaf. At depth
1274 * zero, it should always be null so there's
1275 * no reason to brelse.
1276 */
1277 BUG_ON(*last_eb_bh);
1278 get_bh(bh);
1279 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001280 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001281 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001282 }
1283
1284 /* call ocfs2_add_branch to add the final part of the tree with
1285 * the new data. */
1286 mlog(0, "add branch. bh = %p\n", bh);
Tao Mae7d4cb62008-08-18 17:38:44 +08001287 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001288 meta_ac);
1289 if (ret < 0) {
1290 mlog_errno(ret);
1291 goto out;
1292 }
1293
1294out:
1295 if (final_depth)
1296 *final_depth = depth;
1297 brelse(bh);
1298 return ret;
1299}
1300
1301/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001302 * This function will discard the rightmost extent record.
1303 */
1304static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1305{
1306 int next_free = le16_to_cpu(el->l_next_free_rec);
1307 int count = le16_to_cpu(el->l_count);
1308 unsigned int num_bytes;
1309
1310 BUG_ON(!next_free);
1311 /* This will cause us to go off the end of our extent list. */
1312 BUG_ON(next_free >= count);
1313
1314 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1315
1316 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1317}
1318
1319static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1320 struct ocfs2_extent_rec *insert_rec)
1321{
1322 int i, insert_index, next_free, has_empty, num_bytes;
1323 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1324 struct ocfs2_extent_rec *rec;
1325
1326 next_free = le16_to_cpu(el->l_next_free_rec);
1327 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1328
1329 BUG_ON(!next_free);
1330
1331 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001332 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001333
1334 /*
1335 * The easiest way to approach this is to just remove the
1336 * empty extent and temporarily decrement next_free.
1337 */
1338 if (has_empty) {
1339 /*
1340 * If next_free was 1 (only an empty extent), this
1341 * loop won't execute, which is fine. We still want
1342 * the decrement above to happen.
1343 */
1344 for(i = 0; i < (next_free - 1); i++)
1345 el->l_recs[i] = el->l_recs[i+1];
1346
1347 next_free--;
1348 }
1349
1350 /*
1351 * Figure out what the new record index should be.
1352 */
1353 for(i = 0; i < next_free; i++) {
1354 rec = &el->l_recs[i];
1355
1356 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1357 break;
1358 }
1359 insert_index = i;
1360
1361 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1362 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1363
1364 BUG_ON(insert_index < 0);
1365 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1366 BUG_ON(insert_index > next_free);
1367
1368 /*
1369 * No need to memmove if we're just adding to the tail.
1370 */
1371 if (insert_index != next_free) {
1372 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1373
1374 num_bytes = next_free - insert_index;
1375 num_bytes *= sizeof(struct ocfs2_extent_rec);
1376 memmove(&el->l_recs[insert_index + 1],
1377 &el->l_recs[insert_index],
1378 num_bytes);
1379 }
1380
1381 /*
1382 * Either we had an empty extent, and need to re-increment or
1383 * there was no empty extent on a non full rightmost leaf node,
1384 * in which case we still need to increment.
1385 */
1386 next_free++;
1387 el->l_next_free_rec = cpu_to_le16(next_free);
1388 /*
1389 * Make sure none of the math above just messed up our tree.
1390 */
1391 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1392
1393 el->l_recs[insert_index] = *insert_rec;
1394
1395}
1396
Mark Fasheh328d5752007-06-18 10:48:04 -07001397static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1398{
1399 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1400
1401 BUG_ON(num_recs == 0);
1402
1403 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1404 num_recs--;
1405 size = num_recs * sizeof(struct ocfs2_extent_rec);
1406 memmove(&el->l_recs[0], &el->l_recs[1], size);
1407 memset(&el->l_recs[num_recs], 0,
1408 sizeof(struct ocfs2_extent_rec));
1409 el->l_next_free_rec = cpu_to_le16(num_recs);
1410 }
1411}
1412
Mark Fashehdcd05382007-01-16 11:32:23 -08001413/*
1414 * Create an empty extent record .
1415 *
1416 * l_next_free_rec may be updated.
1417 *
1418 * If an empty extent already exists do nothing.
1419 */
1420static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1421{
1422 int next_free = le16_to_cpu(el->l_next_free_rec);
1423
Mark Fashehe48edee2007-03-07 16:46:57 -08001424 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1425
Mark Fashehdcd05382007-01-16 11:32:23 -08001426 if (next_free == 0)
1427 goto set_and_inc;
1428
1429 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1430 return;
1431
1432 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1433 "Asked to create an empty extent in a full list:\n"
1434 "count = %u, tree depth = %u",
1435 le16_to_cpu(el->l_count),
1436 le16_to_cpu(el->l_tree_depth));
1437
1438 ocfs2_shift_records_right(el);
1439
1440set_and_inc:
1441 le16_add_cpu(&el->l_next_free_rec, 1);
1442 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1443}
1444
1445/*
1446 * For a rotation which involves two leaf nodes, the "root node" is
1447 * the lowest level tree node which contains a path to both leafs. This
1448 * resulting set of information can be used to form a complete "subtree"
1449 *
1450 * This function is passed two full paths from the dinode down to a
1451 * pair of adjacent leaves. It's task is to figure out which path
1452 * index contains the subtree root - this can be the root index itself
1453 * in a worst-case rotation.
1454 *
1455 * The array index of the subtree root is passed back.
1456 */
1457static int ocfs2_find_subtree_root(struct inode *inode,
1458 struct ocfs2_path *left,
1459 struct ocfs2_path *right)
1460{
1461 int i = 0;
1462
1463 /*
1464 * Check that the caller passed in two paths from the same tree.
1465 */
1466 BUG_ON(path_root_bh(left) != path_root_bh(right));
1467
1468 do {
1469 i++;
1470
1471 /*
1472 * The caller didn't pass two adjacent paths.
1473 */
1474 mlog_bug_on_msg(i > left->p_tree_depth,
1475 "Inode %lu, left depth %u, right depth %u\n"
1476 "left leaf blk %llu, right leaf blk %llu\n",
1477 inode->i_ino, left->p_tree_depth,
1478 right->p_tree_depth,
1479 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1480 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1481 } while (left->p_node[i].bh->b_blocknr ==
1482 right->p_node[i].bh->b_blocknr);
1483
1484 return i - 1;
1485}
1486
1487typedef void (path_insert_t)(void *, struct buffer_head *);
1488
1489/*
1490 * Traverse a btree path in search of cpos, starting at root_el.
1491 *
1492 * This code can be called with a cpos larger than the tree, in which
1493 * case it will return the rightmost path.
1494 */
1495static int __ocfs2_find_path(struct inode *inode,
1496 struct ocfs2_extent_list *root_el, u32 cpos,
1497 path_insert_t *func, void *data)
1498{
1499 int i, ret = 0;
1500 u32 range;
1501 u64 blkno;
1502 struct buffer_head *bh = NULL;
1503 struct ocfs2_extent_block *eb;
1504 struct ocfs2_extent_list *el;
1505 struct ocfs2_extent_rec *rec;
1506 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1507
1508 el = root_el;
1509 while (el->l_tree_depth) {
1510 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1511 ocfs2_error(inode->i_sb,
1512 "Inode %llu has empty extent list at "
1513 "depth %u\n",
1514 (unsigned long long)oi->ip_blkno,
1515 le16_to_cpu(el->l_tree_depth));
1516 ret = -EROFS;
1517 goto out;
1518
1519 }
1520
1521 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1522 rec = &el->l_recs[i];
1523
1524 /*
1525 * In the case that cpos is off the allocation
1526 * tree, this should just wind up returning the
1527 * rightmost record.
1528 */
1529 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001530 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001531 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1532 break;
1533 }
1534
1535 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1536 if (blkno == 0) {
1537 ocfs2_error(inode->i_sb,
1538 "Inode %llu has bad blkno in extent list "
1539 "at depth %u (index %d)\n",
1540 (unsigned long long)oi->ip_blkno,
1541 le16_to_cpu(el->l_tree_depth), i);
1542 ret = -EROFS;
1543 goto out;
1544 }
1545
1546 brelse(bh);
1547 bh = NULL;
1548 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1549 &bh, OCFS2_BH_CACHED, inode);
1550 if (ret) {
1551 mlog_errno(ret);
1552 goto out;
1553 }
1554
1555 eb = (struct ocfs2_extent_block *) bh->b_data;
1556 el = &eb->h_list;
1557 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1558 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1559 ret = -EIO;
1560 goto out;
1561 }
1562
1563 if (le16_to_cpu(el->l_next_free_rec) >
1564 le16_to_cpu(el->l_count)) {
1565 ocfs2_error(inode->i_sb,
1566 "Inode %llu has bad count in extent list "
1567 "at block %llu (next free=%u, count=%u)\n",
1568 (unsigned long long)oi->ip_blkno,
1569 (unsigned long long)bh->b_blocknr,
1570 le16_to_cpu(el->l_next_free_rec),
1571 le16_to_cpu(el->l_count));
1572 ret = -EROFS;
1573 goto out;
1574 }
1575
1576 if (func)
1577 func(data, bh);
1578 }
1579
1580out:
1581 /*
1582 * Catch any trailing bh that the loop didn't handle.
1583 */
1584 brelse(bh);
1585
1586 return ret;
1587}
1588
1589/*
1590 * Given an initialized path (that is, it has a valid root extent
1591 * list), this function will traverse the btree in search of the path
1592 * which would contain cpos.
1593 *
1594 * The path traveled is recorded in the path structure.
1595 *
1596 * Note that this will not do any comparisons on leaf node extent
1597 * records, so it will work fine in the case that we just added a tree
1598 * branch.
1599 */
1600struct find_path_data {
1601 int index;
1602 struct ocfs2_path *path;
1603};
1604static void find_path_ins(void *data, struct buffer_head *bh)
1605{
1606 struct find_path_data *fp = data;
1607
1608 get_bh(bh);
1609 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1610 fp->index++;
1611}
1612static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1613 u32 cpos)
1614{
1615 struct find_path_data data;
1616
1617 data.index = 1;
1618 data.path = path;
1619 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1620 find_path_ins, &data);
1621}
1622
1623static void find_leaf_ins(void *data, struct buffer_head *bh)
1624{
1625 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1626 struct ocfs2_extent_list *el = &eb->h_list;
1627 struct buffer_head **ret = data;
1628
1629 /* We want to retain only the leaf block. */
1630 if (le16_to_cpu(el->l_tree_depth) == 0) {
1631 get_bh(bh);
1632 *ret = bh;
1633 }
1634}
1635/*
1636 * Find the leaf block in the tree which would contain cpos. No
1637 * checking of the actual leaf is done.
1638 *
1639 * Some paths want to call this instead of allocating a path structure
1640 * and calling ocfs2_find_path().
1641 *
1642 * This function doesn't handle non btree extent lists.
1643 */
Mark Fasheh363041a2007-01-17 12:31:35 -08001644int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1645 u32 cpos, struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001646{
1647 int ret;
1648 struct buffer_head *bh = NULL;
1649
1650 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1651 if (ret) {
1652 mlog_errno(ret);
1653 goto out;
1654 }
1655
1656 *leaf_bh = bh;
1657out:
1658 return ret;
1659}
1660
1661/*
1662 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1663 *
1664 * Basically, we've moved stuff around at the bottom of the tree and
1665 * we need to fix up the extent records above the changes to reflect
1666 * the new changes.
1667 *
1668 * left_rec: the record on the left.
1669 * left_child_el: is the child list pointed to by left_rec
1670 * right_rec: the record to the right of left_rec
1671 * right_child_el: is the child list pointed to by right_rec
1672 *
1673 * By definition, this only works on interior nodes.
1674 */
1675static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1676 struct ocfs2_extent_list *left_child_el,
1677 struct ocfs2_extent_rec *right_rec,
1678 struct ocfs2_extent_list *right_child_el)
1679{
1680 u32 left_clusters, right_end;
1681
1682 /*
1683 * Interior nodes never have holes. Their cpos is the cpos of
1684 * the leftmost record in their child list. Their cluster
1685 * count covers the full theoretical range of their child list
1686 * - the range between their cpos and the cpos of the record
1687 * immediately to their right.
1688 */
1689 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07001690 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1691 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1692 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1693 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001694 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001695 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001696
1697 /*
1698 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001699 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001700 * updating e_cpos to keep the same highest cluster count.
1701 */
1702 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001703 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001704
1705 right_rec->e_cpos = left_rec->e_cpos;
1706 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1707
1708 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001709 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001710}
1711
1712/*
1713 * Adjust the adjacent root node records involved in a
1714 * rotation. left_el_blkno is passed in as a key so that we can easily
1715 * find it's index in the root list.
1716 */
1717static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1718 struct ocfs2_extent_list *left_el,
1719 struct ocfs2_extent_list *right_el,
1720 u64 left_el_blkno)
1721{
1722 int i;
1723
1724 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1725 le16_to_cpu(left_el->l_tree_depth));
1726
1727 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1728 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1729 break;
1730 }
1731
1732 /*
1733 * The path walking code should have never returned a root and
1734 * two paths which are not adjacent.
1735 */
1736 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1737
1738 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1739 &root_el->l_recs[i + 1], right_el);
1740}
1741
1742/*
1743 * We've changed a leaf block (in right_path) and need to reflect that
1744 * change back up the subtree.
1745 *
1746 * This happens in multiple places:
1747 * - When we've moved an extent record from the left path leaf to the right
1748 * path leaf to make room for an empty extent in the left path leaf.
1749 * - When our insert into the right path leaf is at the leftmost edge
1750 * and requires an update of the path immediately to it's left. This
1751 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08001752 * - When we've adjusted the last extent record in the left path leaf and the
1753 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08001754 */
1755static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1756 struct ocfs2_path *left_path,
1757 struct ocfs2_path *right_path,
1758 int subtree_index)
1759{
1760 int ret, i, idx;
1761 struct ocfs2_extent_list *el, *left_el, *right_el;
1762 struct ocfs2_extent_rec *left_rec, *right_rec;
1763 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1764
1765 /*
1766 * Update the counts and position values within all the
1767 * interior nodes to reflect the leaf rotation we just did.
1768 *
1769 * The root node is handled below the loop.
1770 *
1771 * We begin the loop with right_el and left_el pointing to the
1772 * leaf lists and work our way up.
1773 *
1774 * NOTE: within this loop, left_el and right_el always refer
1775 * to the *child* lists.
1776 */
1777 left_el = path_leaf_el(left_path);
1778 right_el = path_leaf_el(right_path);
1779 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1780 mlog(0, "Adjust records at index %u\n", i);
1781
1782 /*
1783 * One nice property of knowing that all of these
1784 * nodes are below the root is that we only deal with
1785 * the leftmost right node record and the rightmost
1786 * left node record.
1787 */
1788 el = left_path->p_node[i].el;
1789 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1790 left_rec = &el->l_recs[idx];
1791
1792 el = right_path->p_node[i].el;
1793 right_rec = &el->l_recs[0];
1794
1795 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1796 right_el);
1797
1798 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1799 if (ret)
1800 mlog_errno(ret);
1801
1802 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1803 if (ret)
1804 mlog_errno(ret);
1805
1806 /*
1807 * Setup our list pointers now so that the current
1808 * parents become children in the next iteration.
1809 */
1810 left_el = left_path->p_node[i].el;
1811 right_el = right_path->p_node[i].el;
1812 }
1813
1814 /*
1815 * At the root node, adjust the two adjacent records which
1816 * begin our path to the leaves.
1817 */
1818
1819 el = left_path->p_node[subtree_index].el;
1820 left_el = left_path->p_node[subtree_index + 1].el;
1821 right_el = right_path->p_node[subtree_index + 1].el;
1822
1823 ocfs2_adjust_root_records(el, left_el, right_el,
1824 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1825
1826 root_bh = left_path->p_node[subtree_index].bh;
1827
1828 ret = ocfs2_journal_dirty(handle, root_bh);
1829 if (ret)
1830 mlog_errno(ret);
1831}
1832
1833static int ocfs2_rotate_subtree_right(struct inode *inode,
1834 handle_t *handle,
1835 struct ocfs2_path *left_path,
1836 struct ocfs2_path *right_path,
1837 int subtree_index)
1838{
1839 int ret, i;
1840 struct buffer_head *right_leaf_bh;
1841 struct buffer_head *left_leaf_bh = NULL;
1842 struct buffer_head *root_bh;
1843 struct ocfs2_extent_list *right_el, *left_el;
1844 struct ocfs2_extent_rec move_rec;
1845
1846 left_leaf_bh = path_leaf_bh(left_path);
1847 left_el = path_leaf_el(left_path);
1848
1849 if (left_el->l_next_free_rec != left_el->l_count) {
1850 ocfs2_error(inode->i_sb,
1851 "Inode %llu has non-full interior leaf node %llu"
1852 "(next free = %u)",
1853 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1854 (unsigned long long)left_leaf_bh->b_blocknr,
1855 le16_to_cpu(left_el->l_next_free_rec));
1856 return -EROFS;
1857 }
1858
1859 /*
1860 * This extent block may already have an empty record, so we
1861 * return early if so.
1862 */
1863 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1864 return 0;
1865
1866 root_bh = left_path->p_node[subtree_index].bh;
1867 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1868
1869 ret = ocfs2_journal_access(handle, inode, root_bh,
1870 OCFS2_JOURNAL_ACCESS_WRITE);
1871 if (ret) {
1872 mlog_errno(ret);
1873 goto out;
1874 }
1875
1876 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1877 ret = ocfs2_journal_access(handle, inode,
1878 right_path->p_node[i].bh,
1879 OCFS2_JOURNAL_ACCESS_WRITE);
1880 if (ret) {
1881 mlog_errno(ret);
1882 goto out;
1883 }
1884
1885 ret = ocfs2_journal_access(handle, inode,
1886 left_path->p_node[i].bh,
1887 OCFS2_JOURNAL_ACCESS_WRITE);
1888 if (ret) {
1889 mlog_errno(ret);
1890 goto out;
1891 }
1892 }
1893
1894 right_leaf_bh = path_leaf_bh(right_path);
1895 right_el = path_leaf_el(right_path);
1896
1897 /* This is a code error, not a disk corruption. */
1898 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1899 "because rightmost leaf block %llu is empty\n",
1900 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1901 (unsigned long long)right_leaf_bh->b_blocknr);
1902
1903 ocfs2_create_empty_extent(right_el);
1904
1905 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1906 if (ret) {
1907 mlog_errno(ret);
1908 goto out;
1909 }
1910
1911 /* Do the copy now. */
1912 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1913 move_rec = left_el->l_recs[i];
1914 right_el->l_recs[0] = move_rec;
1915
1916 /*
1917 * Clear out the record we just copied and shift everything
1918 * over, leaving an empty extent in the left leaf.
1919 *
1920 * We temporarily subtract from next_free_rec so that the
1921 * shift will lose the tail record (which is now defunct).
1922 */
1923 le16_add_cpu(&left_el->l_next_free_rec, -1);
1924 ocfs2_shift_records_right(left_el);
1925 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1926 le16_add_cpu(&left_el->l_next_free_rec, 1);
1927
1928 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1929 if (ret) {
1930 mlog_errno(ret);
1931 goto out;
1932 }
1933
1934 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1935 subtree_index);
1936
1937out:
1938 return ret;
1939}
1940
1941/*
1942 * Given a full path, determine what cpos value would return us a path
1943 * containing the leaf immediately to the left of the current one.
1944 *
1945 * Will return zero if the path passed in is already the leftmost path.
1946 */
1947static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1948 struct ocfs2_path *path, u32 *cpos)
1949{
1950 int i, j, ret = 0;
1951 u64 blkno;
1952 struct ocfs2_extent_list *el;
1953
Mark Fashehe48edee2007-03-07 16:46:57 -08001954 BUG_ON(path->p_tree_depth == 0);
1955
Mark Fashehdcd05382007-01-16 11:32:23 -08001956 *cpos = 0;
1957
1958 blkno = path_leaf_bh(path)->b_blocknr;
1959
1960 /* Start at the tree node just above the leaf and work our way up. */
1961 i = path->p_tree_depth - 1;
1962 while (i >= 0) {
1963 el = path->p_node[i].el;
1964
1965 /*
1966 * Find the extent record just before the one in our
1967 * path.
1968 */
1969 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1970 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1971 if (j == 0) {
1972 if (i == 0) {
1973 /*
1974 * We've determined that the
1975 * path specified is already
1976 * the leftmost one - return a
1977 * cpos of zero.
1978 */
1979 goto out;
1980 }
1981 /*
1982 * The leftmost record points to our
1983 * leaf - we need to travel up the
1984 * tree one level.
1985 */
1986 goto next_node;
1987 }
1988
1989 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001990 *cpos = *cpos + ocfs2_rec_clusters(el,
1991 &el->l_recs[j - 1]);
1992 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08001993 goto out;
1994 }
1995 }
1996
1997 /*
1998 * If we got here, we never found a valid node where
1999 * the tree indicated one should be.
2000 */
2001 ocfs2_error(sb,
2002 "Invalid extent tree at extent block %llu\n",
2003 (unsigned long long)blkno);
2004 ret = -EROFS;
2005 goto out;
2006
2007next_node:
2008 blkno = path->p_node[i].bh->b_blocknr;
2009 i--;
2010 }
2011
2012out:
2013 return ret;
2014}
2015
Mark Fasheh328d5752007-06-18 10:48:04 -07002016/*
2017 * Extend the transaction by enough credits to complete the rotation,
2018 * and still leave at least the original number of credits allocated
2019 * to this transaction.
2020 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002021static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002022 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002023 struct ocfs2_path *path)
2024{
Mark Fasheh328d5752007-06-18 10:48:04 -07002025 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002026
2027 if (handle->h_buffer_credits < credits)
2028 return ocfs2_extend_trans(handle, credits);
2029
2030 return 0;
2031}
2032
2033/*
2034 * Trap the case where we're inserting into the theoretical range past
2035 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2036 * whose cpos is less than ours into the right leaf.
2037 *
2038 * It's only necessary to look at the rightmost record of the left
2039 * leaf because the logic that calls us should ensure that the
2040 * theoretical ranges in the path components above the leaves are
2041 * correct.
2042 */
2043static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2044 u32 insert_cpos)
2045{
2046 struct ocfs2_extent_list *left_el;
2047 struct ocfs2_extent_rec *rec;
2048 int next_free;
2049
2050 left_el = path_leaf_el(left_path);
2051 next_free = le16_to_cpu(left_el->l_next_free_rec);
2052 rec = &left_el->l_recs[next_free - 1];
2053
2054 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2055 return 1;
2056 return 0;
2057}
2058
Mark Fasheh328d5752007-06-18 10:48:04 -07002059static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2060{
2061 int next_free = le16_to_cpu(el->l_next_free_rec);
2062 unsigned int range;
2063 struct ocfs2_extent_rec *rec;
2064
2065 if (next_free == 0)
2066 return 0;
2067
2068 rec = &el->l_recs[0];
2069 if (ocfs2_is_empty_extent(rec)) {
2070 /* Empty list. */
2071 if (next_free == 1)
2072 return 0;
2073 rec = &el->l_recs[1];
2074 }
2075
2076 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2077 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2078 return 1;
2079 return 0;
2080}
2081
Mark Fashehdcd05382007-01-16 11:32:23 -08002082/*
2083 * Rotate all the records in a btree right one record, starting at insert_cpos.
2084 *
2085 * The path to the rightmost leaf should be passed in.
2086 *
2087 * The array is assumed to be large enough to hold an entire path (tree depth).
2088 *
2089 * Upon succesful return from this function:
2090 *
2091 * - The 'right_path' array will contain a path to the leaf block
2092 * whose range contains e_cpos.
2093 * - That leaf block will have a single empty extent in list index 0.
2094 * - In the case that the rotation requires a post-insert update,
2095 * *ret_left_path will contain a valid path which can be passed to
2096 * ocfs2_insert_path().
2097 */
2098static int ocfs2_rotate_tree_right(struct inode *inode,
2099 handle_t *handle,
Mark Fasheh328d5752007-06-18 10:48:04 -07002100 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002101 u32 insert_cpos,
2102 struct ocfs2_path *right_path,
2103 struct ocfs2_path **ret_left_path)
2104{
Mark Fasheh328d5752007-06-18 10:48:04 -07002105 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002106 u32 cpos;
2107 struct ocfs2_path *left_path = NULL;
2108
2109 *ret_left_path = NULL;
2110
2111 left_path = ocfs2_new_path(path_root_bh(right_path),
2112 path_root_el(right_path));
2113 if (!left_path) {
2114 ret = -ENOMEM;
2115 mlog_errno(ret);
2116 goto out;
2117 }
2118
2119 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2120 if (ret) {
2121 mlog_errno(ret);
2122 goto out;
2123 }
2124
2125 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2126
2127 /*
2128 * What we want to do here is:
2129 *
2130 * 1) Start with the rightmost path.
2131 *
2132 * 2) Determine a path to the leaf block directly to the left
2133 * of that leaf.
2134 *
2135 * 3) Determine the 'subtree root' - the lowest level tree node
2136 * which contains a path to both leaves.
2137 *
2138 * 4) Rotate the subtree.
2139 *
2140 * 5) Find the next subtree by considering the left path to be
2141 * the new right path.
2142 *
2143 * The check at the top of this while loop also accepts
2144 * insert_cpos == cpos because cpos is only a _theoretical_
2145 * value to get us the left path - insert_cpos might very well
2146 * be filling that hole.
2147 *
2148 * Stop at a cpos of '0' because we either started at the
2149 * leftmost branch (i.e., a tree with one branch and a
2150 * rotation inside of it), or we've gone as far as we can in
2151 * rotating subtrees.
2152 */
2153 while (cpos && insert_cpos <= cpos) {
2154 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2155 insert_cpos, cpos);
2156
2157 ret = ocfs2_find_path(inode, left_path, cpos);
2158 if (ret) {
2159 mlog_errno(ret);
2160 goto out;
2161 }
2162
2163 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2164 path_leaf_bh(right_path),
2165 "Inode %lu: error during insert of %u "
2166 "(left path cpos %u) results in two identical "
2167 "paths ending at %llu\n",
2168 inode->i_ino, insert_cpos, cpos,
2169 (unsigned long long)
2170 path_leaf_bh(left_path)->b_blocknr);
2171
Mark Fasheh328d5752007-06-18 10:48:04 -07002172 if (split == SPLIT_NONE &&
2173 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002174 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002175
2176 /*
2177 * We've rotated the tree as much as we
2178 * should. The rest is up to
2179 * ocfs2_insert_path() to complete, after the
2180 * record insertion. We indicate this
2181 * situation by returning the left path.
2182 *
2183 * The reason we don't adjust the records here
2184 * before the record insert is that an error
2185 * later might break the rule where a parent
2186 * record e_cpos will reflect the actual
2187 * e_cpos of the 1st nonempty record of the
2188 * child list.
2189 */
2190 *ret_left_path = left_path;
2191 goto out_ret_path;
2192 }
2193
2194 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2195
2196 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2197 start,
2198 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2199 right_path->p_tree_depth);
2200
2201 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002202 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002203 if (ret) {
2204 mlog_errno(ret);
2205 goto out;
2206 }
2207
2208 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2209 right_path, start);
2210 if (ret) {
2211 mlog_errno(ret);
2212 goto out;
2213 }
2214
Mark Fasheh328d5752007-06-18 10:48:04 -07002215 if (split != SPLIT_NONE &&
2216 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2217 insert_cpos)) {
2218 /*
2219 * A rotate moves the rightmost left leaf
2220 * record over to the leftmost right leaf
2221 * slot. If we're doing an extent split
2222 * instead of a real insert, then we have to
2223 * check that the extent to be split wasn't
2224 * just moved over. If it was, then we can
2225 * exit here, passing left_path back -
2226 * ocfs2_split_extent() is smart enough to
2227 * search both leaves.
2228 */
2229 *ret_left_path = left_path;
2230 goto out_ret_path;
2231 }
2232
Mark Fashehdcd05382007-01-16 11:32:23 -08002233 /*
2234 * There is no need to re-read the next right path
2235 * as we know that it'll be our current left
2236 * path. Optimize by copying values instead.
2237 */
2238 ocfs2_mv_path(right_path, left_path);
2239
2240 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2241 &cpos);
2242 if (ret) {
2243 mlog_errno(ret);
2244 goto out;
2245 }
2246 }
2247
2248out:
2249 ocfs2_free_path(left_path);
2250
2251out_ret_path:
2252 return ret;
2253}
2254
Mark Fasheh328d5752007-06-18 10:48:04 -07002255static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2256 struct ocfs2_path *path)
2257{
2258 int i, idx;
2259 struct ocfs2_extent_rec *rec;
2260 struct ocfs2_extent_list *el;
2261 struct ocfs2_extent_block *eb;
2262 u32 range;
2263
2264 /* Path should always be rightmost. */
2265 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2266 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2267
2268 el = &eb->h_list;
2269 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2270 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2271 rec = &el->l_recs[idx];
2272 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2273
2274 for (i = 0; i < path->p_tree_depth; i++) {
2275 el = path->p_node[i].el;
2276 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2277 rec = &el->l_recs[idx];
2278
2279 rec->e_int_clusters = cpu_to_le32(range);
2280 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2281
2282 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2283 }
2284}
2285
2286static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2287 struct ocfs2_cached_dealloc_ctxt *dealloc,
2288 struct ocfs2_path *path, int unlink_start)
2289{
2290 int ret, i;
2291 struct ocfs2_extent_block *eb;
2292 struct ocfs2_extent_list *el;
2293 struct buffer_head *bh;
2294
2295 for(i = unlink_start; i < path_num_items(path); i++) {
2296 bh = path->p_node[i].bh;
2297
2298 eb = (struct ocfs2_extent_block *)bh->b_data;
2299 /*
2300 * Not all nodes might have had their final count
2301 * decremented by the caller - handle this here.
2302 */
2303 el = &eb->h_list;
2304 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2305 mlog(ML_ERROR,
2306 "Inode %llu, attempted to remove extent block "
2307 "%llu with %u records\n",
2308 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2309 (unsigned long long)le64_to_cpu(eb->h_blkno),
2310 le16_to_cpu(el->l_next_free_rec));
2311
2312 ocfs2_journal_dirty(handle, bh);
2313 ocfs2_remove_from_cache(inode, bh);
2314 continue;
2315 }
2316
2317 el->l_next_free_rec = 0;
2318 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2319
2320 ocfs2_journal_dirty(handle, bh);
2321
2322 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2323 if (ret)
2324 mlog_errno(ret);
2325
2326 ocfs2_remove_from_cache(inode, bh);
2327 }
2328}
2329
2330static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2331 struct ocfs2_path *left_path,
2332 struct ocfs2_path *right_path,
2333 int subtree_index,
2334 struct ocfs2_cached_dealloc_ctxt *dealloc)
2335{
2336 int i;
2337 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2338 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2339 struct ocfs2_extent_list *el;
2340 struct ocfs2_extent_block *eb;
2341
2342 el = path_leaf_el(left_path);
2343
2344 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2345
2346 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2347 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2348 break;
2349
2350 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2351
2352 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2353 le16_add_cpu(&root_el->l_next_free_rec, -1);
2354
2355 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2356 eb->h_next_leaf_blk = 0;
2357
2358 ocfs2_journal_dirty(handle, root_bh);
2359 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2360
2361 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2362 subtree_index + 1);
2363}
2364
2365static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2366 struct ocfs2_path *left_path,
2367 struct ocfs2_path *right_path,
2368 int subtree_index,
2369 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002370 int *deleted,
2371 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002372{
2373 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002374 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002375 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2376 struct ocfs2_extent_block *eb;
2377
2378 *deleted = 0;
2379
2380 right_leaf_el = path_leaf_el(right_path);
2381 left_leaf_el = path_leaf_el(left_path);
2382 root_bh = left_path->p_node[subtree_index].bh;
2383 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2384
2385 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2386 return 0;
2387
2388 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2389 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2390 /*
2391 * It's legal for us to proceed if the right leaf is
2392 * the rightmost one and it has an empty extent. There
2393 * are two cases to handle - whether the leaf will be
2394 * empty after removal or not. If the leaf isn't empty
2395 * then just remove the empty extent up front. The
2396 * next block will handle empty leaves by flagging
2397 * them for unlink.
2398 *
2399 * Non rightmost leaves will throw -EAGAIN and the
2400 * caller can manually move the subtree and retry.
2401 */
2402
2403 if (eb->h_next_leaf_blk != 0ULL)
2404 return -EAGAIN;
2405
2406 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2407 ret = ocfs2_journal_access(handle, inode,
2408 path_leaf_bh(right_path),
2409 OCFS2_JOURNAL_ACCESS_WRITE);
2410 if (ret) {
2411 mlog_errno(ret);
2412 goto out;
2413 }
2414
2415 ocfs2_remove_empty_extent(right_leaf_el);
2416 } else
2417 right_has_empty = 1;
2418 }
2419
2420 if (eb->h_next_leaf_blk == 0ULL &&
2421 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2422 /*
2423 * We have to update i_last_eb_blk during the meta
2424 * data delete.
2425 */
Tao Mae7d4cb62008-08-18 17:38:44 +08002426 ret = ocfs2_journal_access(handle, inode, et_root_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07002427 OCFS2_JOURNAL_ACCESS_WRITE);
2428 if (ret) {
2429 mlog_errno(ret);
2430 goto out;
2431 }
2432
2433 del_right_subtree = 1;
2434 }
2435
2436 /*
2437 * Getting here with an empty extent in the right path implies
2438 * that it's the rightmost path and will be deleted.
2439 */
2440 BUG_ON(right_has_empty && !del_right_subtree);
2441
2442 ret = ocfs2_journal_access(handle, inode, root_bh,
2443 OCFS2_JOURNAL_ACCESS_WRITE);
2444 if (ret) {
2445 mlog_errno(ret);
2446 goto out;
2447 }
2448
2449 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2450 ret = ocfs2_journal_access(handle, inode,
2451 right_path->p_node[i].bh,
2452 OCFS2_JOURNAL_ACCESS_WRITE);
2453 if (ret) {
2454 mlog_errno(ret);
2455 goto out;
2456 }
2457
2458 ret = ocfs2_journal_access(handle, inode,
2459 left_path->p_node[i].bh,
2460 OCFS2_JOURNAL_ACCESS_WRITE);
2461 if (ret) {
2462 mlog_errno(ret);
2463 goto out;
2464 }
2465 }
2466
2467 if (!right_has_empty) {
2468 /*
2469 * Only do this if we're moving a real
2470 * record. Otherwise, the action is delayed until
2471 * after removal of the right path in which case we
2472 * can do a simple shift to remove the empty extent.
2473 */
2474 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2475 memset(&right_leaf_el->l_recs[0], 0,
2476 sizeof(struct ocfs2_extent_rec));
2477 }
2478 if (eb->h_next_leaf_blk == 0ULL) {
2479 /*
2480 * Move recs over to get rid of empty extent, decrease
2481 * next_free. This is allowed to remove the last
2482 * extent in our leaf (setting l_next_free_rec to
2483 * zero) - the delete code below won't care.
2484 */
2485 ocfs2_remove_empty_extent(right_leaf_el);
2486 }
2487
2488 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2489 if (ret)
2490 mlog_errno(ret);
2491 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2492 if (ret)
2493 mlog_errno(ret);
2494
2495 if (del_right_subtree) {
2496 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2497 subtree_index, dealloc);
2498 ocfs2_update_edge_lengths(inode, handle, left_path);
2499
2500 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002501 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002502
2503 /*
2504 * Removal of the extent in the left leaf was skipped
2505 * above so we could delete the right path
2506 * 1st.
2507 */
2508 if (right_has_empty)
2509 ocfs2_remove_empty_extent(left_leaf_el);
2510
Tao Mae7d4cb62008-08-18 17:38:44 +08002511 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002512 if (ret)
2513 mlog_errno(ret);
2514
2515 *deleted = 1;
2516 } else
2517 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2518 subtree_index);
2519
2520out:
2521 return ret;
2522}
2523
2524/*
2525 * Given a full path, determine what cpos value would return us a path
2526 * containing the leaf immediately to the right of the current one.
2527 *
2528 * Will return zero if the path passed in is already the rightmost path.
2529 *
2530 * This looks similar, but is subtly different to
2531 * ocfs2_find_cpos_for_left_leaf().
2532 */
2533static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2534 struct ocfs2_path *path, u32 *cpos)
2535{
2536 int i, j, ret = 0;
2537 u64 blkno;
2538 struct ocfs2_extent_list *el;
2539
2540 *cpos = 0;
2541
2542 if (path->p_tree_depth == 0)
2543 return 0;
2544
2545 blkno = path_leaf_bh(path)->b_blocknr;
2546
2547 /* Start at the tree node just above the leaf and work our way up. */
2548 i = path->p_tree_depth - 1;
2549 while (i >= 0) {
2550 int next_free;
2551
2552 el = path->p_node[i].el;
2553
2554 /*
2555 * Find the extent record just after the one in our
2556 * path.
2557 */
2558 next_free = le16_to_cpu(el->l_next_free_rec);
2559 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2560 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2561 if (j == (next_free - 1)) {
2562 if (i == 0) {
2563 /*
2564 * We've determined that the
2565 * path specified is already
2566 * the rightmost one - return a
2567 * cpos of zero.
2568 */
2569 goto out;
2570 }
2571 /*
2572 * The rightmost record points to our
2573 * leaf - we need to travel up the
2574 * tree one level.
2575 */
2576 goto next_node;
2577 }
2578
2579 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2580 goto out;
2581 }
2582 }
2583
2584 /*
2585 * If we got here, we never found a valid node where
2586 * the tree indicated one should be.
2587 */
2588 ocfs2_error(sb,
2589 "Invalid extent tree at extent block %llu\n",
2590 (unsigned long long)blkno);
2591 ret = -EROFS;
2592 goto out;
2593
2594next_node:
2595 blkno = path->p_node[i].bh->b_blocknr;
2596 i--;
2597 }
2598
2599out:
2600 return ret;
2601}
2602
2603static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2604 handle_t *handle,
2605 struct buffer_head *bh,
2606 struct ocfs2_extent_list *el)
2607{
2608 int ret;
2609
2610 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2611 return 0;
2612
2613 ret = ocfs2_journal_access(handle, inode, bh,
2614 OCFS2_JOURNAL_ACCESS_WRITE);
2615 if (ret) {
2616 mlog_errno(ret);
2617 goto out;
2618 }
2619
2620 ocfs2_remove_empty_extent(el);
2621
2622 ret = ocfs2_journal_dirty(handle, bh);
2623 if (ret)
2624 mlog_errno(ret);
2625
2626out:
2627 return ret;
2628}
2629
2630static int __ocfs2_rotate_tree_left(struct inode *inode,
2631 handle_t *handle, int orig_credits,
2632 struct ocfs2_path *path,
2633 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002634 struct ocfs2_path **empty_extent_path,
2635 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002636{
2637 int ret, subtree_root, deleted;
2638 u32 right_cpos;
2639 struct ocfs2_path *left_path = NULL;
2640 struct ocfs2_path *right_path = NULL;
2641
2642 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2643
2644 *empty_extent_path = NULL;
2645
2646 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2647 &right_cpos);
2648 if (ret) {
2649 mlog_errno(ret);
2650 goto out;
2651 }
2652
2653 left_path = ocfs2_new_path(path_root_bh(path),
2654 path_root_el(path));
2655 if (!left_path) {
2656 ret = -ENOMEM;
2657 mlog_errno(ret);
2658 goto out;
2659 }
2660
2661 ocfs2_cp_path(left_path, path);
2662
2663 right_path = ocfs2_new_path(path_root_bh(path),
2664 path_root_el(path));
2665 if (!right_path) {
2666 ret = -ENOMEM;
2667 mlog_errno(ret);
2668 goto out;
2669 }
2670
2671 while (right_cpos) {
2672 ret = ocfs2_find_path(inode, right_path, right_cpos);
2673 if (ret) {
2674 mlog_errno(ret);
2675 goto out;
2676 }
2677
2678 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2679 right_path);
2680
2681 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2682 subtree_root,
2683 (unsigned long long)
2684 right_path->p_node[subtree_root].bh->b_blocknr,
2685 right_path->p_tree_depth);
2686
2687 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2688 orig_credits, left_path);
2689 if (ret) {
2690 mlog_errno(ret);
2691 goto out;
2692 }
2693
Mark Fashehe8aed342007-12-03 16:43:01 -08002694 /*
2695 * Caller might still want to make changes to the
2696 * tree root, so re-add it to the journal here.
2697 */
2698 ret = ocfs2_journal_access(handle, inode,
2699 path_root_bh(left_path),
2700 OCFS2_JOURNAL_ACCESS_WRITE);
2701 if (ret) {
2702 mlog_errno(ret);
2703 goto out;
2704 }
2705
Mark Fasheh328d5752007-06-18 10:48:04 -07002706 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2707 right_path, subtree_root,
Tao Mae7d4cb62008-08-18 17:38:44 +08002708 dealloc, &deleted, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002709 if (ret == -EAGAIN) {
2710 /*
2711 * The rotation has to temporarily stop due to
2712 * the right subtree having an empty
2713 * extent. Pass it back to the caller for a
2714 * fixup.
2715 */
2716 *empty_extent_path = right_path;
2717 right_path = NULL;
2718 goto out;
2719 }
2720 if (ret) {
2721 mlog_errno(ret);
2722 goto out;
2723 }
2724
2725 /*
2726 * The subtree rotate might have removed records on
2727 * the rightmost edge. If so, then rotation is
2728 * complete.
2729 */
2730 if (deleted)
2731 break;
2732
2733 ocfs2_mv_path(left_path, right_path);
2734
2735 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2736 &right_cpos);
2737 if (ret) {
2738 mlog_errno(ret);
2739 goto out;
2740 }
2741 }
2742
2743out:
2744 ocfs2_free_path(right_path);
2745 ocfs2_free_path(left_path);
2746
2747 return ret;
2748}
2749
2750static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08002751 struct ocfs2_path *path,
2752 struct ocfs2_cached_dealloc_ctxt *dealloc,
2753 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002754{
2755 int ret, subtree_index;
2756 u32 cpos;
2757 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07002758 struct ocfs2_extent_block *eb;
2759 struct ocfs2_extent_list *el;
2760
Mark Fasheh328d5752007-06-18 10:48:04 -07002761
Joel Becker35dc0aa2008-08-20 16:25:06 -07002762 ret = ocfs2_et_sanity_check(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002763 if (ret)
2764 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07002765 /*
2766 * There's two ways we handle this depending on
2767 * whether path is the only existing one.
2768 */
2769 ret = ocfs2_extend_rotate_transaction(handle, 0,
2770 handle->h_buffer_credits,
2771 path);
2772 if (ret) {
2773 mlog_errno(ret);
2774 goto out;
2775 }
2776
2777 ret = ocfs2_journal_access_path(inode, handle, path);
2778 if (ret) {
2779 mlog_errno(ret);
2780 goto out;
2781 }
2782
2783 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2784 if (ret) {
2785 mlog_errno(ret);
2786 goto out;
2787 }
2788
2789 if (cpos) {
2790 /*
2791 * We have a path to the left of this one - it needs
2792 * an update too.
2793 */
2794 left_path = ocfs2_new_path(path_root_bh(path),
2795 path_root_el(path));
2796 if (!left_path) {
2797 ret = -ENOMEM;
2798 mlog_errno(ret);
2799 goto out;
2800 }
2801
2802 ret = ocfs2_find_path(inode, left_path, cpos);
2803 if (ret) {
2804 mlog_errno(ret);
2805 goto out;
2806 }
2807
2808 ret = ocfs2_journal_access_path(inode, handle, left_path);
2809 if (ret) {
2810 mlog_errno(ret);
2811 goto out;
2812 }
2813
2814 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2815
2816 ocfs2_unlink_subtree(inode, handle, left_path, path,
2817 subtree_index, dealloc);
2818 ocfs2_update_edge_lengths(inode, handle, left_path);
2819
2820 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002821 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002822 } else {
2823 /*
2824 * 'path' is also the leftmost path which
2825 * means it must be the only one. This gets
2826 * handled differently because we want to
2827 * revert the inode back to having extents
2828 * in-line.
2829 */
2830 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2831
Joel Beckerce1d9ea2008-08-20 16:30:07 -07002832 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07002833 el->l_tree_depth = 0;
2834 el->l_next_free_rec = 0;
2835 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2836
Joel Becker35dc0aa2008-08-20 16:25:06 -07002837 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07002838 }
2839
2840 ocfs2_journal_dirty(handle, path_root_bh(path));
2841
2842out:
2843 ocfs2_free_path(left_path);
2844 return ret;
2845}
2846
2847/*
2848 * Left rotation of btree records.
2849 *
2850 * In many ways, this is (unsurprisingly) the opposite of right
2851 * rotation. We start at some non-rightmost path containing an empty
2852 * extent in the leaf block. The code works its way to the rightmost
2853 * path by rotating records to the left in every subtree.
2854 *
2855 * This is used by any code which reduces the number of extent records
2856 * in a leaf. After removal, an empty record should be placed in the
2857 * leftmost list position.
2858 *
2859 * This won't handle a length update of the rightmost path records if
2860 * the rightmost tree leaf record is removed so the caller is
2861 * responsible for detecting and correcting that.
2862 */
2863static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2864 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002865 struct ocfs2_cached_dealloc_ctxt *dealloc,
2866 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002867{
2868 int ret, orig_credits = handle->h_buffer_credits;
2869 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2870 struct ocfs2_extent_block *eb;
2871 struct ocfs2_extent_list *el;
2872
2873 el = path_leaf_el(path);
2874 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2875 return 0;
2876
2877 if (path->p_tree_depth == 0) {
2878rightmost_no_delete:
2879 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08002880 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07002881 * it up front.
2882 */
2883 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2884 path_leaf_bh(path),
2885 path_leaf_el(path));
2886 if (ret)
2887 mlog_errno(ret);
2888 goto out;
2889 }
2890
2891 /*
2892 * Handle rightmost branch now. There's several cases:
2893 * 1) simple rotation leaving records in there. That's trivial.
2894 * 2) rotation requiring a branch delete - there's no more
2895 * records left. Two cases of this:
2896 * a) There are branches to the left.
2897 * b) This is also the leftmost (the only) branch.
2898 *
2899 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2900 * 2a) we need the left branch so that we can update it with the unlink
2901 * 2b) we need to bring the inode back to inline extents.
2902 */
2903
2904 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2905 el = &eb->h_list;
2906 if (eb->h_next_leaf_blk == 0) {
2907 /*
2908 * This gets a bit tricky if we're going to delete the
2909 * rightmost path. Get the other cases out of the way
2910 * 1st.
2911 */
2912 if (le16_to_cpu(el->l_next_free_rec) > 1)
2913 goto rightmost_no_delete;
2914
2915 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2916 ret = -EIO;
2917 ocfs2_error(inode->i_sb,
2918 "Inode %llu has empty extent block at %llu",
2919 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2920 (unsigned long long)le64_to_cpu(eb->h_blkno));
2921 goto out;
2922 }
2923
2924 /*
2925 * XXX: The caller can not trust "path" any more after
2926 * this as it will have been deleted. What do we do?
2927 *
2928 * In theory the rotate-for-merge code will never get
2929 * here because it'll always ask for a rotate in a
2930 * nonempty list.
2931 */
2932
2933 ret = ocfs2_remove_rightmost_path(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002934 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002935 if (ret)
2936 mlog_errno(ret);
2937 goto out;
2938 }
2939
2940 /*
2941 * Now we can loop, remembering the path we get from -EAGAIN
2942 * and restarting from there.
2943 */
2944try_rotate:
2945 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002946 dealloc, &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002947 if (ret && ret != -EAGAIN) {
2948 mlog_errno(ret);
2949 goto out;
2950 }
2951
2952 while (ret == -EAGAIN) {
2953 tmp_path = restart_path;
2954 restart_path = NULL;
2955
2956 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2957 tmp_path, dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002958 &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002959 if (ret && ret != -EAGAIN) {
2960 mlog_errno(ret);
2961 goto out;
2962 }
2963
2964 ocfs2_free_path(tmp_path);
2965 tmp_path = NULL;
2966
2967 if (ret == 0)
2968 goto try_rotate;
2969 }
2970
2971out:
2972 ocfs2_free_path(tmp_path);
2973 ocfs2_free_path(restart_path);
2974 return ret;
2975}
2976
2977static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
2978 int index)
2979{
2980 struct ocfs2_extent_rec *rec = &el->l_recs[index];
2981 unsigned int size;
2982
2983 if (rec->e_leaf_clusters == 0) {
2984 /*
2985 * We consumed all of the merged-from record. An empty
2986 * extent cannot exist anywhere but the 1st array
2987 * position, so move things over if the merged-from
2988 * record doesn't occupy that position.
2989 *
2990 * This creates a new empty extent so the caller
2991 * should be smart enough to have removed any existing
2992 * ones.
2993 */
2994 if (index > 0) {
2995 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
2996 size = index * sizeof(struct ocfs2_extent_rec);
2997 memmove(&el->l_recs[1], &el->l_recs[0], size);
2998 }
2999
3000 /*
3001 * Always memset - the caller doesn't check whether it
3002 * created an empty extent, so there could be junk in
3003 * the other fields.
3004 */
3005 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3006 }
3007}
3008
Tao Ma677b9752008-01-30 14:21:05 +08003009static int ocfs2_get_right_path(struct inode *inode,
3010 struct ocfs2_path *left_path,
3011 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003012{
3013 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003014 u32 right_cpos;
3015 struct ocfs2_path *right_path = NULL;
3016 struct ocfs2_extent_list *left_el;
3017
3018 *ret_right_path = NULL;
3019
3020 /* This function shouldn't be called for non-trees. */
3021 BUG_ON(left_path->p_tree_depth == 0);
3022
3023 left_el = path_leaf_el(left_path);
3024 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3025
3026 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3027 &right_cpos);
3028 if (ret) {
3029 mlog_errno(ret);
3030 goto out;
3031 }
3032
3033 /* This function shouldn't be called for the rightmost leaf. */
3034 BUG_ON(right_cpos == 0);
3035
3036 right_path = ocfs2_new_path(path_root_bh(left_path),
3037 path_root_el(left_path));
3038 if (!right_path) {
3039 ret = -ENOMEM;
3040 mlog_errno(ret);
3041 goto out;
3042 }
3043
3044 ret = ocfs2_find_path(inode, right_path, right_cpos);
3045 if (ret) {
3046 mlog_errno(ret);
3047 goto out;
3048 }
3049
3050 *ret_right_path = right_path;
3051out:
3052 if (ret)
3053 ocfs2_free_path(right_path);
3054 return ret;
3055}
3056
3057/*
3058 * Remove split_rec clusters from the record at index and merge them
3059 * onto the beginning of the record "next" to it.
3060 * For index < l_count - 1, the next means the extent rec at index + 1.
3061 * For index == l_count - 1, the "next" means the 1st extent rec of the
3062 * next extent block.
3063 */
3064static int ocfs2_merge_rec_right(struct inode *inode,
3065 struct ocfs2_path *left_path,
3066 handle_t *handle,
3067 struct ocfs2_extent_rec *split_rec,
3068 int index)
3069{
3070 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003071 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3072 struct ocfs2_extent_rec *left_rec;
3073 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003074 struct ocfs2_extent_list *right_el;
3075 struct ocfs2_path *right_path = NULL;
3076 int subtree_index = 0;
3077 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3078 struct buffer_head *bh = path_leaf_bh(left_path);
3079 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003080
3081 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003082 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003083
Al Viro9d8df6a2008-05-21 06:32:11 +01003084 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003085 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3086 /* we meet with a cross extent block merge. */
3087 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3088 if (ret) {
3089 mlog_errno(ret);
3090 goto out;
3091 }
3092
3093 right_el = path_leaf_el(right_path);
3094 next_free = le16_to_cpu(right_el->l_next_free_rec);
3095 BUG_ON(next_free <= 0);
3096 right_rec = &right_el->l_recs[0];
3097 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003098 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003099 right_rec = &right_el->l_recs[1];
3100 }
3101
3102 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3103 le16_to_cpu(left_rec->e_leaf_clusters) !=
3104 le32_to_cpu(right_rec->e_cpos));
3105
3106 subtree_index = ocfs2_find_subtree_root(inode,
3107 left_path, right_path);
3108
3109 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3110 handle->h_buffer_credits,
3111 right_path);
3112 if (ret) {
3113 mlog_errno(ret);
3114 goto out;
3115 }
3116
3117 root_bh = left_path->p_node[subtree_index].bh;
3118 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3119
3120 ret = ocfs2_journal_access(handle, inode, root_bh,
3121 OCFS2_JOURNAL_ACCESS_WRITE);
3122 if (ret) {
3123 mlog_errno(ret);
3124 goto out;
3125 }
3126
3127 for (i = subtree_index + 1;
3128 i < path_num_items(right_path); i++) {
3129 ret = ocfs2_journal_access(handle, inode,
3130 right_path->p_node[i].bh,
3131 OCFS2_JOURNAL_ACCESS_WRITE);
3132 if (ret) {
3133 mlog_errno(ret);
3134 goto out;
3135 }
3136
3137 ret = ocfs2_journal_access(handle, inode,
3138 left_path->p_node[i].bh,
3139 OCFS2_JOURNAL_ACCESS_WRITE);
3140 if (ret) {
3141 mlog_errno(ret);
3142 goto out;
3143 }
3144 }
3145
3146 } else {
3147 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3148 right_rec = &el->l_recs[index + 1];
3149 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003150
3151 ret = ocfs2_journal_access(handle, inode, bh,
3152 OCFS2_JOURNAL_ACCESS_WRITE);
3153 if (ret) {
3154 mlog_errno(ret);
3155 goto out;
3156 }
3157
3158 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3159
3160 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3161 le64_add_cpu(&right_rec->e_blkno,
3162 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3163 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3164
3165 ocfs2_cleanup_merge(el, index);
3166
3167 ret = ocfs2_journal_dirty(handle, bh);
3168 if (ret)
3169 mlog_errno(ret);
3170
Tao Ma677b9752008-01-30 14:21:05 +08003171 if (right_path) {
3172 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3173 if (ret)
3174 mlog_errno(ret);
3175
3176 ocfs2_complete_edge_insert(inode, handle, left_path,
3177 right_path, subtree_index);
3178 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003179out:
Tao Ma677b9752008-01-30 14:21:05 +08003180 if (right_path)
3181 ocfs2_free_path(right_path);
3182 return ret;
3183}
3184
3185static int ocfs2_get_left_path(struct inode *inode,
3186 struct ocfs2_path *right_path,
3187 struct ocfs2_path **ret_left_path)
3188{
3189 int ret;
3190 u32 left_cpos;
3191 struct ocfs2_path *left_path = NULL;
3192
3193 *ret_left_path = NULL;
3194
3195 /* This function shouldn't be called for non-trees. */
3196 BUG_ON(right_path->p_tree_depth == 0);
3197
3198 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3199 right_path, &left_cpos);
3200 if (ret) {
3201 mlog_errno(ret);
3202 goto out;
3203 }
3204
3205 /* This function shouldn't be called for the leftmost leaf. */
3206 BUG_ON(left_cpos == 0);
3207
3208 left_path = ocfs2_new_path(path_root_bh(right_path),
3209 path_root_el(right_path));
3210 if (!left_path) {
3211 ret = -ENOMEM;
3212 mlog_errno(ret);
3213 goto out;
3214 }
3215
3216 ret = ocfs2_find_path(inode, left_path, left_cpos);
3217 if (ret) {
3218 mlog_errno(ret);
3219 goto out;
3220 }
3221
3222 *ret_left_path = left_path;
3223out:
3224 if (ret)
3225 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003226 return ret;
3227}
3228
3229/*
3230 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003231 * onto the tail of the record "before" it.
3232 * For index > 0, the "before" means the extent rec at index - 1.
3233 *
3234 * For index == 0, the "before" means the last record of the previous
3235 * extent block. And there is also a situation that we may need to
3236 * remove the rightmost leaf extent block in the right_path and change
3237 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003238 */
Tao Ma677b9752008-01-30 14:21:05 +08003239static int ocfs2_merge_rec_left(struct inode *inode,
3240 struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003241 handle_t *handle,
3242 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003243 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003244 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003245 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003246{
Tao Ma677b9752008-01-30 14:21:05 +08003247 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003248 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3249 struct ocfs2_extent_rec *left_rec;
3250 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003251 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3252 struct buffer_head *bh = path_leaf_bh(right_path);
3253 struct buffer_head *root_bh = NULL;
3254 struct ocfs2_path *left_path = NULL;
3255 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003256
Tao Ma677b9752008-01-30 14:21:05 +08003257 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003258
Mark Fasheh328d5752007-06-18 10:48:04 -07003259 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003260 if (index == 0) {
3261 /* we meet with a cross extent block merge. */
3262 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3263 if (ret) {
3264 mlog_errno(ret);
3265 goto out;
3266 }
3267
3268 left_el = path_leaf_el(left_path);
3269 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3270 le16_to_cpu(left_el->l_count));
3271
3272 left_rec = &left_el->l_recs[
3273 le16_to_cpu(left_el->l_next_free_rec) - 1];
3274 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3275 le16_to_cpu(left_rec->e_leaf_clusters) !=
3276 le32_to_cpu(split_rec->e_cpos));
3277
3278 subtree_index = ocfs2_find_subtree_root(inode,
3279 left_path, right_path);
3280
3281 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3282 handle->h_buffer_credits,
3283 left_path);
3284 if (ret) {
3285 mlog_errno(ret);
3286 goto out;
3287 }
3288
3289 root_bh = left_path->p_node[subtree_index].bh;
3290 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3291
3292 ret = ocfs2_journal_access(handle, inode, root_bh,
3293 OCFS2_JOURNAL_ACCESS_WRITE);
3294 if (ret) {
3295 mlog_errno(ret);
3296 goto out;
3297 }
3298
3299 for (i = subtree_index + 1;
3300 i < path_num_items(right_path); i++) {
3301 ret = ocfs2_journal_access(handle, inode,
3302 right_path->p_node[i].bh,
3303 OCFS2_JOURNAL_ACCESS_WRITE);
3304 if (ret) {
3305 mlog_errno(ret);
3306 goto out;
3307 }
3308
3309 ret = ocfs2_journal_access(handle, inode,
3310 left_path->p_node[i].bh,
3311 OCFS2_JOURNAL_ACCESS_WRITE);
3312 if (ret) {
3313 mlog_errno(ret);
3314 goto out;
3315 }
3316 }
3317 } else {
3318 left_rec = &el->l_recs[index - 1];
3319 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3320 has_empty_extent = 1;
3321 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003322
3323 ret = ocfs2_journal_access(handle, inode, bh,
3324 OCFS2_JOURNAL_ACCESS_WRITE);
3325 if (ret) {
3326 mlog_errno(ret);
3327 goto out;
3328 }
3329
3330 if (has_empty_extent && index == 1) {
3331 /*
3332 * The easy case - we can just plop the record right in.
3333 */
3334 *left_rec = *split_rec;
3335
3336 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003337 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003338 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003339
3340 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3341 le64_add_cpu(&right_rec->e_blkno,
3342 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3343 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3344
3345 ocfs2_cleanup_merge(el, index);
3346
3347 ret = ocfs2_journal_dirty(handle, bh);
3348 if (ret)
3349 mlog_errno(ret);
3350
Tao Ma677b9752008-01-30 14:21:05 +08003351 if (left_path) {
3352 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3353 if (ret)
3354 mlog_errno(ret);
3355
3356 /*
3357 * In the situation that the right_rec is empty and the extent
3358 * block is empty also, ocfs2_complete_edge_insert can't handle
3359 * it and we need to delete the right extent block.
3360 */
3361 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3362 le16_to_cpu(el->l_next_free_rec) == 1) {
3363
3364 ret = ocfs2_remove_rightmost_path(inode, handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003365 right_path,
3366 dealloc, et);
Tao Ma677b9752008-01-30 14:21:05 +08003367 if (ret) {
3368 mlog_errno(ret);
3369 goto out;
3370 }
3371
3372 /* Now the rightmost extent block has been deleted.
3373 * So we use the new rightmost path.
3374 */
3375 ocfs2_mv_path(right_path, left_path);
3376 left_path = NULL;
3377 } else
3378 ocfs2_complete_edge_insert(inode, handle, left_path,
3379 right_path, subtree_index);
3380 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003381out:
Tao Ma677b9752008-01-30 14:21:05 +08003382 if (left_path)
3383 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003384 return ret;
3385}
3386
3387static int ocfs2_try_to_merge_extent(struct inode *inode,
3388 handle_t *handle,
Tao Ma677b9752008-01-30 14:21:05 +08003389 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003390 int split_index,
3391 struct ocfs2_extent_rec *split_rec,
3392 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003393 struct ocfs2_merge_ctxt *ctxt,
3394 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003395
3396{
Tao Mao518d7262007-08-28 17:25:35 -07003397 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003398 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003399 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3400
3401 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3402
Tao Mao518d7262007-08-28 17:25:35 -07003403 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3404 /*
3405 * The merge code will need to create an empty
3406 * extent to take the place of the newly
3407 * emptied slot. Remove any pre-existing empty
3408 * extents - having more than one in a leaf is
3409 * illegal.
3410 */
Tao Ma677b9752008-01-30 14:21:05 +08003411 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003412 dealloc, et);
Tao Mao518d7262007-08-28 17:25:35 -07003413 if (ret) {
3414 mlog_errno(ret);
3415 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003416 }
Tao Mao518d7262007-08-28 17:25:35 -07003417 split_index--;
3418 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003419 }
3420
3421 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3422 /*
3423 * Left-right contig implies this.
3424 */
3425 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003426
3427 /*
3428 * Since the leftright insert always covers the entire
3429 * extent, this call will delete the insert record
3430 * entirely, resulting in an empty extent record added to
3431 * the extent block.
3432 *
3433 * Since the adding of an empty extent shifts
3434 * everything back to the right, there's no need to
3435 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003436 *
3437 * When the split_index is zero, we need to merge it to the
3438 * prevoius extent block. It is more efficient and easier
3439 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003440 */
Tao Ma677b9752008-01-30 14:21:05 +08003441 ret = ocfs2_merge_rec_right(inode, path,
3442 handle, split_rec,
3443 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003444 if (ret) {
3445 mlog_errno(ret);
3446 goto out;
3447 }
3448
3449 /*
3450 * We can only get this from logic error above.
3451 */
3452 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3453
Tao Ma677b9752008-01-30 14:21:05 +08003454 /* The merge left us with an empty extent, remove it. */
Tao Mae7d4cb62008-08-18 17:38:44 +08003455 ret = ocfs2_rotate_tree_left(inode, handle, path,
3456 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003457 if (ret) {
3458 mlog_errno(ret);
3459 goto out;
3460 }
Tao Ma677b9752008-01-30 14:21:05 +08003461
Mark Fasheh328d5752007-06-18 10:48:04 -07003462 rec = &el->l_recs[split_index];
3463
3464 /*
3465 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003466 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003467 */
Tao Ma677b9752008-01-30 14:21:05 +08003468 ret = ocfs2_merge_rec_left(inode, path,
3469 handle, rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003470 dealloc, et,
Tao Ma677b9752008-01-30 14:21:05 +08003471 split_index);
3472
Mark Fasheh328d5752007-06-18 10:48:04 -07003473 if (ret) {
3474 mlog_errno(ret);
3475 goto out;
3476 }
3477
Tao Ma677b9752008-01-30 14:21:05 +08003478 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003479 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003480 /*
3481 * Error from this last rotate is not critical, so
3482 * print but don't bubble it up.
3483 */
3484 if (ret)
3485 mlog_errno(ret);
3486 ret = 0;
3487 } else {
3488 /*
3489 * Merge a record to the left or right.
3490 *
3491 * 'contig_type' is relative to the existing record,
3492 * so for example, if we're "right contig", it's to
3493 * the record on the left (hence the left merge).
3494 */
3495 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3496 ret = ocfs2_merge_rec_left(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003497 path,
3498 handle, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003499 dealloc, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003500 split_index);
3501 if (ret) {
3502 mlog_errno(ret);
3503 goto out;
3504 }
3505 } else {
3506 ret = ocfs2_merge_rec_right(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003507 path,
3508 handle, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003509 split_index);
3510 if (ret) {
3511 mlog_errno(ret);
3512 goto out;
3513 }
3514 }
3515
3516 if (ctxt->c_split_covers_rec) {
3517 /*
3518 * The merge may have left an empty extent in
3519 * our leaf. Try to rotate it away.
3520 */
Tao Ma677b9752008-01-30 14:21:05 +08003521 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003522 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003523 if (ret)
3524 mlog_errno(ret);
3525 ret = 0;
3526 }
3527 }
3528
3529out:
3530 return ret;
3531}
3532
3533static void ocfs2_subtract_from_rec(struct super_block *sb,
3534 enum ocfs2_split_type split,
3535 struct ocfs2_extent_rec *rec,
3536 struct ocfs2_extent_rec *split_rec)
3537{
3538 u64 len_blocks;
3539
3540 len_blocks = ocfs2_clusters_to_blocks(sb,
3541 le16_to_cpu(split_rec->e_leaf_clusters));
3542
3543 if (split == SPLIT_LEFT) {
3544 /*
3545 * Region is on the left edge of the existing
3546 * record.
3547 */
3548 le32_add_cpu(&rec->e_cpos,
3549 le16_to_cpu(split_rec->e_leaf_clusters));
3550 le64_add_cpu(&rec->e_blkno, len_blocks);
3551 le16_add_cpu(&rec->e_leaf_clusters,
3552 -le16_to_cpu(split_rec->e_leaf_clusters));
3553 } else {
3554 /*
3555 * Region is on the right edge of the existing
3556 * record.
3557 */
3558 le16_add_cpu(&rec->e_leaf_clusters,
3559 -le16_to_cpu(split_rec->e_leaf_clusters));
3560 }
3561}
3562
Mark Fashehdcd05382007-01-16 11:32:23 -08003563/*
3564 * Do the final bits of extent record insertion at the target leaf
3565 * list. If this leaf is part of an allocation tree, it is assumed
3566 * that the tree above has been prepared.
3567 */
3568static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3569 struct ocfs2_extent_list *el,
3570 struct ocfs2_insert_type *insert,
3571 struct inode *inode)
3572{
3573 int i = insert->ins_contig_index;
3574 unsigned int range;
3575 struct ocfs2_extent_rec *rec;
3576
Mark Fashehe48edee2007-03-07 16:46:57 -08003577 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003578
Mark Fasheh328d5752007-06-18 10:48:04 -07003579 if (insert->ins_split != SPLIT_NONE) {
3580 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3581 BUG_ON(i == -1);
3582 rec = &el->l_recs[i];
3583 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3584 insert_rec);
3585 goto rotate;
3586 }
3587
Mark Fashehdcd05382007-01-16 11:32:23 -08003588 /*
3589 * Contiguous insert - either left or right.
3590 */
3591 if (insert->ins_contig != CONTIG_NONE) {
3592 rec = &el->l_recs[i];
3593 if (insert->ins_contig == CONTIG_LEFT) {
3594 rec->e_blkno = insert_rec->e_blkno;
3595 rec->e_cpos = insert_rec->e_cpos;
3596 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003597 le16_add_cpu(&rec->e_leaf_clusters,
3598 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003599 return;
3600 }
3601
3602 /*
3603 * Handle insert into an empty leaf.
3604 */
3605 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3606 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3607 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3608 el->l_recs[0] = *insert_rec;
3609 el->l_next_free_rec = cpu_to_le16(1);
3610 return;
3611 }
3612
3613 /*
3614 * Appending insert.
3615 */
3616 if (insert->ins_appending == APPEND_TAIL) {
3617 i = le16_to_cpu(el->l_next_free_rec) - 1;
3618 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003619 range = le32_to_cpu(rec->e_cpos)
3620 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003621 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3622
3623 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3624 le16_to_cpu(el->l_count),
3625 "inode %lu, depth %u, count %u, next free %u, "
3626 "rec.cpos %u, rec.clusters %u, "
3627 "insert.cpos %u, insert.clusters %u\n",
3628 inode->i_ino,
3629 le16_to_cpu(el->l_tree_depth),
3630 le16_to_cpu(el->l_count),
3631 le16_to_cpu(el->l_next_free_rec),
3632 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003633 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003634 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003635 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003636 i++;
3637 el->l_recs[i] = *insert_rec;
3638 le16_add_cpu(&el->l_next_free_rec, 1);
3639 return;
3640 }
3641
Mark Fasheh328d5752007-06-18 10:48:04 -07003642rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003643 /*
3644 * Ok, we have to rotate.
3645 *
3646 * At this point, it is safe to assume that inserting into an
3647 * empty leaf and appending to a leaf have both been handled
3648 * above.
3649 *
3650 * This leaf needs to have space, either by the empty 1st
3651 * extent record, or by virtue of an l_next_rec < l_count.
3652 */
3653 ocfs2_rotate_leaf(el, insert_rec);
3654}
3655
Mark Fasheh328d5752007-06-18 10:48:04 -07003656static void ocfs2_adjust_rightmost_records(struct inode *inode,
3657 handle_t *handle,
3658 struct ocfs2_path *path,
3659 struct ocfs2_extent_rec *insert_rec)
3660{
3661 int ret, i, next_free;
3662 struct buffer_head *bh;
3663 struct ocfs2_extent_list *el;
3664 struct ocfs2_extent_rec *rec;
3665
3666 /*
3667 * Update everything except the leaf block.
3668 */
3669 for (i = 0; i < path->p_tree_depth; i++) {
3670 bh = path->p_node[i].bh;
3671 el = path->p_node[i].el;
3672
3673 next_free = le16_to_cpu(el->l_next_free_rec);
3674 if (next_free == 0) {
3675 ocfs2_error(inode->i_sb,
3676 "Dinode %llu has a bad extent list",
3677 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3678 ret = -EIO;
3679 return;
3680 }
3681
3682 rec = &el->l_recs[next_free - 1];
3683
3684 rec->e_int_clusters = insert_rec->e_cpos;
3685 le32_add_cpu(&rec->e_int_clusters,
3686 le16_to_cpu(insert_rec->e_leaf_clusters));
3687 le32_add_cpu(&rec->e_int_clusters,
3688 -le32_to_cpu(rec->e_cpos));
3689
3690 ret = ocfs2_journal_dirty(handle, bh);
3691 if (ret)
3692 mlog_errno(ret);
3693
3694 }
3695}
3696
Mark Fashehdcd05382007-01-16 11:32:23 -08003697static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3698 struct ocfs2_extent_rec *insert_rec,
3699 struct ocfs2_path *right_path,
3700 struct ocfs2_path **ret_left_path)
3701{
Mark Fasheh328d5752007-06-18 10:48:04 -07003702 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003703 struct ocfs2_extent_list *el;
3704 struct ocfs2_path *left_path = NULL;
3705
3706 *ret_left_path = NULL;
3707
3708 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003709 * This shouldn't happen for non-trees. The extent rec cluster
3710 * count manipulation below only works for interior nodes.
3711 */
3712 BUG_ON(right_path->p_tree_depth == 0);
3713
3714 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003715 * If our appending insert is at the leftmost edge of a leaf,
3716 * then we might need to update the rightmost records of the
3717 * neighboring path.
3718 */
3719 el = path_leaf_el(right_path);
3720 next_free = le16_to_cpu(el->l_next_free_rec);
3721 if (next_free == 0 ||
3722 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3723 u32 left_cpos;
3724
3725 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3726 &left_cpos);
3727 if (ret) {
3728 mlog_errno(ret);
3729 goto out;
3730 }
3731
3732 mlog(0, "Append may need a left path update. cpos: %u, "
3733 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3734 left_cpos);
3735
3736 /*
3737 * No need to worry if the append is already in the
3738 * leftmost leaf.
3739 */
3740 if (left_cpos) {
3741 left_path = ocfs2_new_path(path_root_bh(right_path),
3742 path_root_el(right_path));
3743 if (!left_path) {
3744 ret = -ENOMEM;
3745 mlog_errno(ret);
3746 goto out;
3747 }
3748
3749 ret = ocfs2_find_path(inode, left_path, left_cpos);
3750 if (ret) {
3751 mlog_errno(ret);
3752 goto out;
3753 }
3754
3755 /*
3756 * ocfs2_insert_path() will pass the left_path to the
3757 * journal for us.
3758 */
3759 }
3760 }
3761
3762 ret = ocfs2_journal_access_path(inode, handle, right_path);
3763 if (ret) {
3764 mlog_errno(ret);
3765 goto out;
3766 }
3767
Mark Fasheh328d5752007-06-18 10:48:04 -07003768 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08003769
3770 *ret_left_path = left_path;
3771 ret = 0;
3772out:
3773 if (ret != 0)
3774 ocfs2_free_path(left_path);
3775
3776 return ret;
3777}
3778
Mark Fasheh328d5752007-06-18 10:48:04 -07003779static void ocfs2_split_record(struct inode *inode,
3780 struct ocfs2_path *left_path,
3781 struct ocfs2_path *right_path,
3782 struct ocfs2_extent_rec *split_rec,
3783 enum ocfs2_split_type split)
3784{
3785 int index;
3786 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3787 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3788 struct ocfs2_extent_rec *rec, *tmprec;
3789
3790 right_el = path_leaf_el(right_path);;
3791 if (left_path)
3792 left_el = path_leaf_el(left_path);
3793
3794 el = right_el;
3795 insert_el = right_el;
3796 index = ocfs2_search_extent_list(el, cpos);
3797 if (index != -1) {
3798 if (index == 0 && left_path) {
3799 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3800
3801 /*
3802 * This typically means that the record
3803 * started in the left path but moved to the
3804 * right as a result of rotation. We either
3805 * move the existing record to the left, or we
3806 * do the later insert there.
3807 *
3808 * In this case, the left path should always
3809 * exist as the rotate code will have passed
3810 * it back for a post-insert update.
3811 */
3812
3813 if (split == SPLIT_LEFT) {
3814 /*
3815 * It's a left split. Since we know
3816 * that the rotate code gave us an
3817 * empty extent in the left path, we
3818 * can just do the insert there.
3819 */
3820 insert_el = left_el;
3821 } else {
3822 /*
3823 * Right split - we have to move the
3824 * existing record over to the left
3825 * leaf. The insert will be into the
3826 * newly created empty extent in the
3827 * right leaf.
3828 */
3829 tmprec = &right_el->l_recs[index];
3830 ocfs2_rotate_leaf(left_el, tmprec);
3831 el = left_el;
3832
3833 memset(tmprec, 0, sizeof(*tmprec));
3834 index = ocfs2_search_extent_list(left_el, cpos);
3835 BUG_ON(index == -1);
3836 }
3837 }
3838 } else {
3839 BUG_ON(!left_path);
3840 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3841 /*
3842 * Left path is easy - we can just allow the insert to
3843 * happen.
3844 */
3845 el = left_el;
3846 insert_el = left_el;
3847 index = ocfs2_search_extent_list(el, cpos);
3848 BUG_ON(index == -1);
3849 }
3850
3851 rec = &el->l_recs[index];
3852 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3853 ocfs2_rotate_leaf(insert_el, split_rec);
3854}
3855
Mark Fashehdcd05382007-01-16 11:32:23 -08003856/*
Tao Mae7d4cb62008-08-18 17:38:44 +08003857 * This function only does inserts on an allocation b-tree. For tree
3858 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08003859 *
3860 * right_path is the path we want to do the actual insert
3861 * in. left_path should only be passed in if we need to update that
3862 * portion of the tree after an edge insert.
3863 */
3864static int ocfs2_insert_path(struct inode *inode,
3865 handle_t *handle,
3866 struct ocfs2_path *left_path,
3867 struct ocfs2_path *right_path,
3868 struct ocfs2_extent_rec *insert_rec,
3869 struct ocfs2_insert_type *insert)
3870{
3871 int ret, subtree_index;
3872 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003873
Mark Fashehdcd05382007-01-16 11:32:23 -08003874 if (left_path) {
3875 int credits = handle->h_buffer_credits;
3876
3877 /*
3878 * There's a chance that left_path got passed back to
3879 * us without being accounted for in the
3880 * journal. Extend our transaction here to be sure we
3881 * can change those blocks.
3882 */
3883 credits += left_path->p_tree_depth;
3884
3885 ret = ocfs2_extend_trans(handle, credits);
3886 if (ret < 0) {
3887 mlog_errno(ret);
3888 goto out;
3889 }
3890
3891 ret = ocfs2_journal_access_path(inode, handle, left_path);
3892 if (ret < 0) {
3893 mlog_errno(ret);
3894 goto out;
3895 }
3896 }
3897
Mark Fashehe8aed342007-12-03 16:43:01 -08003898 /*
3899 * Pass both paths to the journal. The majority of inserts
3900 * will be touching all components anyway.
3901 */
3902 ret = ocfs2_journal_access_path(inode, handle, right_path);
3903 if (ret < 0) {
3904 mlog_errno(ret);
3905 goto out;
3906 }
3907
Mark Fasheh328d5752007-06-18 10:48:04 -07003908 if (insert->ins_split != SPLIT_NONE) {
3909 /*
3910 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02003911 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07003912 * function sort it all out.
3913 */
3914 ocfs2_split_record(inode, left_path, right_path,
3915 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08003916
3917 /*
3918 * Split might have modified either leaf and we don't
3919 * have a guarantee that the later edge insert will
3920 * dirty this for us.
3921 */
3922 if (left_path)
3923 ret = ocfs2_journal_dirty(handle,
3924 path_leaf_bh(left_path));
3925 if (ret)
3926 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07003927 } else
3928 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3929 insert, inode);
Mark Fashehdcd05382007-01-16 11:32:23 -08003930
Mark Fashehdcd05382007-01-16 11:32:23 -08003931 ret = ocfs2_journal_dirty(handle, leaf_bh);
3932 if (ret)
3933 mlog_errno(ret);
3934
3935 if (left_path) {
3936 /*
3937 * The rotate code has indicated that we need to fix
3938 * up portions of the tree after the insert.
3939 *
3940 * XXX: Should we extend the transaction here?
3941 */
3942 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3943 right_path);
3944 ocfs2_complete_edge_insert(inode, handle, left_path,
3945 right_path, subtree_index);
3946 }
3947
3948 ret = 0;
3949out:
3950 return ret;
3951}
3952
3953static int ocfs2_do_insert_extent(struct inode *inode,
3954 handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003955 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08003956 struct ocfs2_extent_rec *insert_rec,
3957 struct ocfs2_insert_type *type)
3958{
3959 int ret, rotate = 0;
3960 u32 cpos;
3961 struct ocfs2_path *right_path = NULL;
3962 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08003963 struct ocfs2_extent_list *el;
3964
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003965 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08003966
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003967 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehdcd05382007-01-16 11:32:23 -08003968 OCFS2_JOURNAL_ACCESS_WRITE);
3969 if (ret) {
3970 mlog_errno(ret);
3971 goto out;
3972 }
3973
3974 if (le16_to_cpu(el->l_tree_depth) == 0) {
3975 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
3976 goto out_update_clusters;
3977 }
3978
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003979 right_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fashehdcd05382007-01-16 11:32:23 -08003980 if (!right_path) {
3981 ret = -ENOMEM;
3982 mlog_errno(ret);
3983 goto out;
3984 }
3985
3986 /*
3987 * Determine the path to start with. Rotations need the
3988 * rightmost path, everything else can go directly to the
3989 * target leaf.
3990 */
3991 cpos = le32_to_cpu(insert_rec->e_cpos);
3992 if (type->ins_appending == APPEND_NONE &&
3993 type->ins_contig == CONTIG_NONE) {
3994 rotate = 1;
3995 cpos = UINT_MAX;
3996 }
3997
3998 ret = ocfs2_find_path(inode, right_path, cpos);
3999 if (ret) {
4000 mlog_errno(ret);
4001 goto out;
4002 }
4003
4004 /*
4005 * Rotations and appends need special treatment - they modify
4006 * parts of the tree's above them.
4007 *
4008 * Both might pass back a path immediate to the left of the
4009 * one being inserted to. This will be cause
4010 * ocfs2_insert_path() to modify the rightmost records of
4011 * left_path to account for an edge insert.
4012 *
4013 * XXX: When modifying this code, keep in mind that an insert
4014 * can wind up skipping both of these two special cases...
4015 */
4016 if (rotate) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004017 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004018 le32_to_cpu(insert_rec->e_cpos),
4019 right_path, &left_path);
4020 if (ret) {
4021 mlog_errno(ret);
4022 goto out;
4023 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004024
4025 /*
4026 * ocfs2_rotate_tree_right() might have extended the
4027 * transaction without re-journaling our tree root.
4028 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004029 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehe8aed342007-12-03 16:43:01 -08004030 OCFS2_JOURNAL_ACCESS_WRITE);
4031 if (ret) {
4032 mlog_errno(ret);
4033 goto out;
4034 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004035 } else if (type->ins_appending == APPEND_TAIL
4036 && type->ins_contig != CONTIG_LEFT) {
4037 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4038 right_path, &left_path);
4039 if (ret) {
4040 mlog_errno(ret);
4041 goto out;
4042 }
4043 }
4044
4045 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4046 insert_rec, type);
4047 if (ret) {
4048 mlog_errno(ret);
4049 goto out;
4050 }
4051
4052out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004053 if (type->ins_split == SPLIT_NONE)
Joel Becker35dc0aa2008-08-20 16:25:06 -07004054 ocfs2_et_update_clusters(inode, et,
4055 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004056
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004057 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004058 if (ret)
4059 mlog_errno(ret);
4060
4061out:
4062 ocfs2_free_path(left_path);
4063 ocfs2_free_path(right_path);
4064
4065 return ret;
4066}
4067
Mark Fasheh328d5752007-06-18 10:48:04 -07004068static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08004069ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004070 struct ocfs2_extent_list *el, int index,
4071 struct ocfs2_extent_rec *split_rec)
4072{
Tao Maad5a4d72008-01-30 14:21:32 +08004073 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004074 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004075 u32 left_cpos, right_cpos;
4076 struct ocfs2_extent_rec *rec = NULL;
4077 struct ocfs2_extent_list *new_el;
4078 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4079 struct buffer_head *bh;
4080 struct ocfs2_extent_block *eb;
4081
4082 if (index > 0) {
4083 rec = &el->l_recs[index - 1];
4084 } else if (path->p_tree_depth > 0) {
4085 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4086 path, &left_cpos);
4087 if (status)
4088 goto out;
4089
4090 if (left_cpos != 0) {
4091 left_path = ocfs2_new_path(path_root_bh(path),
4092 path_root_el(path));
4093 if (!left_path)
4094 goto out;
4095
4096 status = ocfs2_find_path(inode, left_path, left_cpos);
4097 if (status)
4098 goto out;
4099
4100 new_el = path_leaf_el(left_path);
4101
4102 if (le16_to_cpu(new_el->l_next_free_rec) !=
4103 le16_to_cpu(new_el->l_count)) {
4104 bh = path_leaf_bh(left_path);
4105 eb = (struct ocfs2_extent_block *)bh->b_data;
4106 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4107 eb);
4108 goto out;
4109 }
4110 rec = &new_el->l_recs[
4111 le16_to_cpu(new_el->l_next_free_rec) - 1];
4112 }
4113 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004114
4115 /*
4116 * We're careful to check for an empty extent record here -
4117 * the merge code will know what to do if it sees one.
4118 */
Tao Maad5a4d72008-01-30 14:21:32 +08004119 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004120 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4121 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4122 ret = CONTIG_RIGHT;
4123 } else {
4124 ret = ocfs2_extent_contig(inode, rec, split_rec);
4125 }
4126 }
4127
Tao Maad5a4d72008-01-30 14:21:32 +08004128 rec = NULL;
4129 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4130 rec = &el->l_recs[index + 1];
4131 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4132 path->p_tree_depth > 0) {
4133 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4134 path, &right_cpos);
4135 if (status)
4136 goto out;
4137
4138 if (right_cpos == 0)
4139 goto out;
4140
4141 right_path = ocfs2_new_path(path_root_bh(path),
4142 path_root_el(path));
4143 if (!right_path)
4144 goto out;
4145
4146 status = ocfs2_find_path(inode, right_path, right_cpos);
4147 if (status)
4148 goto out;
4149
4150 new_el = path_leaf_el(right_path);
4151 rec = &new_el->l_recs[0];
4152 if (ocfs2_is_empty_extent(rec)) {
4153 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4154 bh = path_leaf_bh(right_path);
4155 eb = (struct ocfs2_extent_block *)bh->b_data;
4156 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4157 eb);
4158 goto out;
4159 }
4160 rec = &new_el->l_recs[1];
4161 }
4162 }
4163
4164 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004165 enum ocfs2_contig_type contig_type;
4166
Mark Fasheh328d5752007-06-18 10:48:04 -07004167 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4168
4169 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4170 ret = CONTIG_LEFTRIGHT;
4171 else if (ret == CONTIG_NONE)
4172 ret = contig_type;
4173 }
4174
Tao Maad5a4d72008-01-30 14:21:32 +08004175out:
4176 if (left_path)
4177 ocfs2_free_path(left_path);
4178 if (right_path)
4179 ocfs2_free_path(right_path);
4180
Mark Fasheh328d5752007-06-18 10:48:04 -07004181 return ret;
4182}
4183
Mark Fashehdcd05382007-01-16 11:32:23 -08004184static void ocfs2_figure_contig_type(struct inode *inode,
4185 struct ocfs2_insert_type *insert,
4186 struct ocfs2_extent_list *el,
Tao Maca12b7c2008-08-18 17:38:52 +08004187 struct ocfs2_extent_rec *insert_rec,
4188 struct ocfs2_extent_tree *et)
Mark Fashehdcd05382007-01-16 11:32:23 -08004189{
4190 int i;
4191 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4192
Mark Fashehe48edee2007-03-07 16:46:57 -08004193 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4194
Mark Fashehdcd05382007-01-16 11:32:23 -08004195 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4196 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4197 insert_rec);
4198 if (contig_type != CONTIG_NONE) {
4199 insert->ins_contig_index = i;
4200 break;
4201 }
4202 }
4203 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004204
4205 if (insert->ins_contig != CONTIG_NONE) {
4206 struct ocfs2_extent_rec *rec =
4207 &el->l_recs[insert->ins_contig_index];
4208 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4209 le16_to_cpu(insert_rec->e_leaf_clusters);
4210
4211 /*
4212 * Caller might want us to limit the size of extents, don't
4213 * calculate contiguousness if we might exceed that limit.
4214 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004215 if (et->et_max_leaf_clusters &&
4216 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004217 insert->ins_contig = CONTIG_NONE;
4218 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004219}
4220
4221/*
4222 * This should only be called against the righmost leaf extent list.
4223 *
4224 * ocfs2_figure_appending_type() will figure out whether we'll have to
4225 * insert at the tail of the rightmost leaf.
4226 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004227 * This should also work against the root extent list for tree's with 0
4228 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004229 * then the logic here makes sense.
4230 */
4231static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4232 struct ocfs2_extent_list *el,
4233 struct ocfs2_extent_rec *insert_rec)
4234{
4235 int i;
4236 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4237 struct ocfs2_extent_rec *rec;
4238
4239 insert->ins_appending = APPEND_NONE;
4240
Mark Fashehe48edee2007-03-07 16:46:57 -08004241 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004242
4243 if (!el->l_next_free_rec)
4244 goto set_tail_append;
4245
4246 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4247 /* Were all records empty? */
4248 if (le16_to_cpu(el->l_next_free_rec) == 1)
4249 goto set_tail_append;
4250 }
4251
4252 i = le16_to_cpu(el->l_next_free_rec) - 1;
4253 rec = &el->l_recs[i];
4254
Mark Fashehe48edee2007-03-07 16:46:57 -08004255 if (cpos >=
4256 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004257 goto set_tail_append;
4258
4259 return;
4260
4261set_tail_append:
4262 insert->ins_appending = APPEND_TAIL;
4263}
4264
4265/*
4266 * Helper function called at the begining of an insert.
4267 *
4268 * This computes a few things that are commonly used in the process of
4269 * inserting into the btree:
4270 * - Whether the new extent is contiguous with an existing one.
4271 * - The current tree depth.
4272 * - Whether the insert is an appending one.
4273 * - The total # of free records in the tree.
4274 *
4275 * All of the information is stored on the ocfs2_insert_type
4276 * structure.
4277 */
4278static int ocfs2_figure_insert_type(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004279 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004280 struct buffer_head **last_eb_bh,
4281 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004282 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004283 struct ocfs2_insert_type *insert)
4284{
4285 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004286 struct ocfs2_extent_block *eb;
4287 struct ocfs2_extent_list *el;
4288 struct ocfs2_path *path = NULL;
4289 struct buffer_head *bh = NULL;
4290
Mark Fasheh328d5752007-06-18 10:48:04 -07004291 insert->ins_split = SPLIT_NONE;
4292
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004293 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004294 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4295
4296 if (el->l_tree_depth) {
4297 /*
4298 * If we have tree depth, we read in the
4299 * rightmost extent block ahead of time as
4300 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4301 * may want it later.
4302 */
4303 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
Joel Becker35dc0aa2008-08-20 16:25:06 -07004304 ocfs2_et_get_last_eb_blk(et), &bh,
Mark Fashehdcd05382007-01-16 11:32:23 -08004305 OCFS2_BH_CACHED, inode);
4306 if (ret) {
4307 mlog_exit(ret);
4308 goto out;
4309 }
4310 eb = (struct ocfs2_extent_block *) bh->b_data;
4311 el = &eb->h_list;
4312 }
4313
4314 /*
4315 * Unless we have a contiguous insert, we'll need to know if
4316 * there is room left in our allocation tree for another
4317 * extent record.
4318 *
4319 * XXX: This test is simplistic, we can search for empty
4320 * extent records too.
4321 */
Tao Maoc77534f2007-08-28 17:22:33 -07004322 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004323 le16_to_cpu(el->l_next_free_rec);
4324
4325 if (!insert->ins_tree_depth) {
Tao Maca12b7c2008-08-18 17:38:52 +08004326 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004327 ocfs2_figure_appending_type(insert, el, insert_rec);
4328 return 0;
4329 }
4330
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004331 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fashehdcd05382007-01-16 11:32:23 -08004332 if (!path) {
4333 ret = -ENOMEM;
4334 mlog_errno(ret);
4335 goto out;
4336 }
4337
4338 /*
4339 * In the case that we're inserting past what the tree
4340 * currently accounts for, ocfs2_find_path() will return for
4341 * us the rightmost tree path. This is accounted for below in
4342 * the appending code.
4343 */
4344 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4345 if (ret) {
4346 mlog_errno(ret);
4347 goto out;
4348 }
4349
4350 el = path_leaf_el(path);
4351
4352 /*
4353 * Now that we have the path, there's two things we want to determine:
4354 * 1) Contiguousness (also set contig_index if this is so)
4355 *
4356 * 2) Are we doing an append? We can trivially break this up
4357 * into two types of appends: simple record append, or a
4358 * rotate inside the tail leaf.
4359 */
Tao Maca12b7c2008-08-18 17:38:52 +08004360 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004361
4362 /*
4363 * The insert code isn't quite ready to deal with all cases of
4364 * left contiguousness. Specifically, if it's an insert into
4365 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004366 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004367 * left. For now, just catch that case and fool the layers
4368 * above us. This works just fine for tree_depth == 0, which
4369 * is why we allow that above.
4370 */
4371 if (insert->ins_contig == CONTIG_LEFT &&
4372 insert->ins_contig_index == 0)
4373 insert->ins_contig = CONTIG_NONE;
4374
4375 /*
4376 * Ok, so we can simply compare against last_eb to figure out
4377 * whether the path doesn't exist. This will only happen in
4378 * the case that we're doing a tail append, so maybe we can
4379 * take advantage of that information somehow.
4380 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004381 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004382 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004383 /*
4384 * Ok, ocfs2_find_path() returned us the rightmost
4385 * tree path. This might be an appending insert. There are
4386 * two cases:
4387 * 1) We're doing a true append at the tail:
4388 * -This might even be off the end of the leaf
4389 * 2) We're "appending" by rotating in the tail
4390 */
4391 ocfs2_figure_appending_type(insert, el, insert_rec);
4392 }
4393
4394out:
4395 ocfs2_free_path(path);
4396
4397 if (ret == 0)
4398 *last_eb_bh = bh;
4399 else
4400 brelse(bh);
4401 return ret;
4402}
4403
4404/*
4405 * Insert an extent into an inode btree.
4406 *
4407 * The caller needs to update fe->i_clusters
4408 */
Tao Maf56654c2008-08-18 17:38:48 +08004409static int ocfs2_insert_extent(struct ocfs2_super *osb,
4410 handle_t *handle,
4411 struct inode *inode,
4412 struct buffer_head *root_bh,
4413 u32 cpos,
4414 u64 start_blk,
4415 u32 new_clusters,
4416 u8 flags,
4417 struct ocfs2_alloc_context *meta_ac,
4418 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -08004419{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004420 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004421 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004422 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004423 struct ocfs2_insert_type insert = {0, };
4424 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004425
Mark Fashehdcd05382007-01-16 11:32:23 -08004426 mlog(0, "add %u clusters at position %u to inode %llu\n",
4427 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004428
Mark Fashehe48edee2007-03-07 16:46:57 -08004429 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004430 rec.e_cpos = cpu_to_le32(cpos);
4431 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004432 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004433 rec.e_flags = flags;
Joel Becker1e61ee72008-08-20 18:32:45 -07004434 status = ocfs2_et_insert_check(inode, et, &rec);
4435 if (status) {
4436 mlog_errno(status);
4437 goto bail;
4438 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004439
Tao Mae7d4cb62008-08-18 17:38:44 +08004440 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004441 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004442 if (status < 0) {
4443 mlog_errno(status);
4444 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004445 }
4446
Mark Fashehdcd05382007-01-16 11:32:23 -08004447 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4448 "Insert.contig_index: %d, Insert.free_records: %d, "
4449 "Insert.tree_depth: %d\n",
4450 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004451 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004452
Tao Maoc77534f2007-08-28 17:22:33 -07004453 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004454 status = ocfs2_grow_tree(inode, handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004455 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004456 meta_ac);
4457 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004458 mlog_errno(status);
4459 goto bail;
4460 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004461 }
4462
Mark Fashehdcd05382007-01-16 11:32:23 -08004463 /* Finally, we can add clusters. This might rotate the tree for us. */
Tao Mae7d4cb62008-08-18 17:38:44 +08004464 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004465 if (status < 0)
4466 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004467 else if (et->et_type == OCFS2_DINODE_EXTENT)
Mark Fasheh83418972007-04-23 18:53:12 -07004468 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004469
4470bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08004471 if (last_eb_bh)
4472 brelse(last_eb_bh);
4473
Tao Maf56654c2008-08-18 17:38:48 +08004474 mlog_exit(status);
4475 return status;
4476}
4477
4478int ocfs2_dinode_insert_extent(struct ocfs2_super *osb,
4479 handle_t *handle,
4480 struct inode *inode,
4481 struct buffer_head *root_bh,
4482 u32 cpos,
4483 u64 start_blk,
4484 u32 new_clusters,
4485 u8 flags,
4486 struct ocfs2_alloc_context *meta_ac)
4487{
4488 int status;
Joel Beckerdc0ce612008-08-20 16:48:35 -07004489 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +08004490
Joel Becker1a09f552008-08-20 17:44:24 -07004491 ocfs2_get_dinode_extent_tree(&et, inode, root_bh);
Tao Maf56654c2008-08-18 17:38:48 +08004492 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4493 cpos, start_blk, new_clusters,
Joel Beckerdc0ce612008-08-20 16:48:35 -07004494 flags, meta_ac, &et);
4495 ocfs2_put_extent_tree(&et);
Tao Maf56654c2008-08-18 17:38:48 +08004496
Tao Maf56654c2008-08-18 17:38:48 +08004497 return status;
4498}
4499
4500int ocfs2_xattr_value_insert_extent(struct ocfs2_super *osb,
4501 handle_t *handle,
4502 struct inode *inode,
4503 struct buffer_head *root_bh,
4504 u32 cpos,
4505 u64 start_blk,
4506 u32 new_clusters,
4507 u8 flags,
4508 struct ocfs2_alloc_context *meta_ac,
Joel Becker1a09f552008-08-20 17:44:24 -07004509 struct ocfs2_xattr_value_root *xv)
Tao Maf56654c2008-08-18 17:38:48 +08004510{
4511 int status;
Joel Beckerdc0ce612008-08-20 16:48:35 -07004512 struct ocfs2_extent_tree et;
Tao Maf56654c2008-08-18 17:38:48 +08004513
Joel Becker1a09f552008-08-20 17:44:24 -07004514 ocfs2_get_xattr_value_extent_tree(&et, inode, root_bh, xv);
Tao Maf56654c2008-08-18 17:38:48 +08004515 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4516 cpos, start_blk, new_clusters,
Joel Beckerdc0ce612008-08-20 16:48:35 -07004517 flags, meta_ac, &et);
4518 ocfs2_put_extent_tree(&et);
Tao Maf56654c2008-08-18 17:38:48 +08004519
Mark Fashehccd979b2005-12-15 14:31:24 -08004520 return status;
4521}
4522
Tao Maba492612008-08-18 17:38:49 +08004523int ocfs2_xattr_tree_insert_extent(struct ocfs2_super *osb,
4524 handle_t *handle,
4525 struct inode *inode,
4526 struct buffer_head *root_bh,
4527 u32 cpos,
4528 u64 start_blk,
4529 u32 new_clusters,
4530 u8 flags,
4531 struct ocfs2_alloc_context *meta_ac)
4532{
4533 int status;
Joel Beckerdc0ce612008-08-20 16:48:35 -07004534 struct ocfs2_extent_tree et;
Tao Maba492612008-08-18 17:38:49 +08004535
Joel Becker1a09f552008-08-20 17:44:24 -07004536 ocfs2_get_xattr_tree_extent_tree(&et, inode, root_bh);
Tao Maba492612008-08-18 17:38:49 +08004537 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4538 cpos, start_blk, new_clusters,
Joel Beckerdc0ce612008-08-20 16:48:35 -07004539 flags, meta_ac, &et);
4540 ocfs2_put_extent_tree(&et);
Tao Maba492612008-08-18 17:38:49 +08004541
Tao Maba492612008-08-18 17:38:49 +08004542 return status;
4543}
4544
Tao Ma0eb8d472008-08-18 17:38:45 +08004545/*
4546 * Allcate and add clusters into the extent b-tree.
4547 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4548 * The extent b-tree's root is root_el and it should be in root_bh, and
4549 * it is not limited to the file storage. Any extent tree can use this
4550 * function if it implements the proper ocfs2_extent_tree.
4551 */
4552int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4553 struct inode *inode,
4554 u32 *logical_offset,
4555 u32 clusters_to_add,
4556 int mark_unwritten,
4557 struct buffer_head *root_bh,
4558 struct ocfs2_extent_list *root_el,
4559 handle_t *handle,
4560 struct ocfs2_alloc_context *data_ac,
4561 struct ocfs2_alloc_context *meta_ac,
4562 enum ocfs2_alloc_restarted *reason_ret,
Tao Maf56654c2008-08-18 17:38:48 +08004563 enum ocfs2_extent_tree_type type,
Joel Beckerea5efa12008-08-20 16:57:27 -07004564 void *obj)
Tao Ma0eb8d472008-08-18 17:38:45 +08004565{
4566 int status = 0;
4567 int free_extents;
4568 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4569 u32 bit_off, num_bits;
4570 u64 block;
4571 u8 flags = 0;
4572
4573 BUG_ON(!clusters_to_add);
4574
4575 if (mark_unwritten)
4576 flags = OCFS2_EXT_UNWRITTEN;
4577
Tao Maf56654c2008-08-18 17:38:48 +08004578 free_extents = ocfs2_num_free_extents(osb, inode, root_bh, type,
Joel Beckerea5efa12008-08-20 16:57:27 -07004579 obj);
Tao Ma0eb8d472008-08-18 17:38:45 +08004580 if (free_extents < 0) {
4581 status = free_extents;
4582 mlog_errno(status);
4583 goto leave;
4584 }
4585
4586 /* there are two cases which could cause us to EAGAIN in the
4587 * we-need-more-metadata case:
4588 * 1) we haven't reserved *any*
4589 * 2) we are so fragmented, we've needed to add metadata too
4590 * many times. */
4591 if (!free_extents && !meta_ac) {
4592 mlog(0, "we haven't reserved any metadata!\n");
4593 status = -EAGAIN;
4594 reason = RESTART_META;
4595 goto leave;
4596 } else if ((!free_extents)
4597 && (ocfs2_alloc_context_bits_left(meta_ac)
4598 < ocfs2_extend_meta_needed(root_el))) {
4599 mlog(0, "filesystem is really fragmented...\n");
4600 status = -EAGAIN;
4601 reason = RESTART_META;
4602 goto leave;
4603 }
4604
4605 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4606 clusters_to_add, &bit_off, &num_bits);
4607 if (status < 0) {
4608 if (status != -ENOSPC)
4609 mlog_errno(status);
4610 goto leave;
4611 }
4612
4613 BUG_ON(num_bits > clusters_to_add);
4614
4615 /* reserve our write early -- insert_extent may update the inode */
4616 status = ocfs2_journal_access(handle, inode, root_bh,
4617 OCFS2_JOURNAL_ACCESS_WRITE);
4618 if (status < 0) {
4619 mlog_errno(status);
4620 goto leave;
4621 }
4622
4623 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4624 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4625 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Tao Maf56654c2008-08-18 17:38:48 +08004626 if (type == OCFS2_DINODE_EXTENT)
4627 status = ocfs2_dinode_insert_extent(osb, handle, inode, root_bh,
4628 *logical_offset, block,
4629 num_bits, flags, meta_ac);
Tao Maba492612008-08-18 17:38:49 +08004630 else if (type == OCFS2_XATTR_TREE_EXTENT)
4631 status = ocfs2_xattr_tree_insert_extent(osb, handle,
4632 inode, root_bh,
4633 *logical_offset,
4634 block, num_bits, flags,
4635 meta_ac);
Tao Maf56654c2008-08-18 17:38:48 +08004636 else
4637 status = ocfs2_xattr_value_insert_extent(osb, handle,
4638 inode, root_bh,
4639 *logical_offset,
4640 block, num_bits, flags,
Joel Beckerea5efa12008-08-20 16:57:27 -07004641 meta_ac, obj);
Tao Ma0eb8d472008-08-18 17:38:45 +08004642 if (status < 0) {
4643 mlog_errno(status);
4644 goto leave;
4645 }
4646
4647 status = ocfs2_journal_dirty(handle, root_bh);
4648 if (status < 0) {
4649 mlog_errno(status);
4650 goto leave;
4651 }
4652
4653 clusters_to_add -= num_bits;
4654 *logical_offset += num_bits;
4655
4656 if (clusters_to_add) {
4657 mlog(0, "need to alloc once more, wanted = %u\n",
4658 clusters_to_add);
4659 status = -EAGAIN;
4660 reason = RESTART_TRANS;
4661 }
4662
4663leave:
4664 mlog_exit(status);
4665 if (reason_ret)
4666 *reason_ret = reason;
4667 return status;
4668}
4669
Mark Fasheh328d5752007-06-18 10:48:04 -07004670static void ocfs2_make_right_split_rec(struct super_block *sb,
4671 struct ocfs2_extent_rec *split_rec,
4672 u32 cpos,
4673 struct ocfs2_extent_rec *rec)
4674{
4675 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4676 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4677
4678 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4679
4680 split_rec->e_cpos = cpu_to_le32(cpos);
4681 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4682
4683 split_rec->e_blkno = rec->e_blkno;
4684 le64_add_cpu(&split_rec->e_blkno,
4685 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4686
4687 split_rec->e_flags = rec->e_flags;
4688}
4689
4690static int ocfs2_split_and_insert(struct inode *inode,
4691 handle_t *handle,
4692 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004693 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004694 struct buffer_head **last_eb_bh,
4695 int split_index,
4696 struct ocfs2_extent_rec *orig_split_rec,
4697 struct ocfs2_alloc_context *meta_ac)
4698{
4699 int ret = 0, depth;
4700 unsigned int insert_range, rec_range, do_leftright = 0;
4701 struct ocfs2_extent_rec tmprec;
4702 struct ocfs2_extent_list *rightmost_el;
4703 struct ocfs2_extent_rec rec;
4704 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4705 struct ocfs2_insert_type insert;
4706 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004707
4708leftright:
4709 /*
4710 * Store a copy of the record on the stack - it might move
4711 * around as the tree is manipulated below.
4712 */
4713 rec = path_leaf_el(path)->l_recs[split_index];
4714
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004715 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004716
4717 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4718 if (depth) {
4719 BUG_ON(!(*last_eb_bh));
4720 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4721 rightmost_el = &eb->h_list;
4722 }
4723
4724 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4725 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004726 ret = ocfs2_grow_tree(inode, handle, et,
4727 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004728 if (ret) {
4729 mlog_errno(ret);
4730 goto out;
4731 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004732 }
4733
4734 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4735 insert.ins_appending = APPEND_NONE;
4736 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004737 insert.ins_tree_depth = depth;
4738
4739 insert_range = le32_to_cpu(split_rec.e_cpos) +
4740 le16_to_cpu(split_rec.e_leaf_clusters);
4741 rec_range = le32_to_cpu(rec.e_cpos) +
4742 le16_to_cpu(rec.e_leaf_clusters);
4743
4744 if (split_rec.e_cpos == rec.e_cpos) {
4745 insert.ins_split = SPLIT_LEFT;
4746 } else if (insert_range == rec_range) {
4747 insert.ins_split = SPLIT_RIGHT;
4748 } else {
4749 /*
4750 * Left/right split. We fake this as a right split
4751 * first and then make a second pass as a left split.
4752 */
4753 insert.ins_split = SPLIT_RIGHT;
4754
4755 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4756 &rec);
4757
4758 split_rec = tmprec;
4759
4760 BUG_ON(do_leftright);
4761 do_leftright = 1;
4762 }
4763
Tao Mae7d4cb62008-08-18 17:38:44 +08004764 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004765 if (ret) {
4766 mlog_errno(ret);
4767 goto out;
4768 }
4769
4770 if (do_leftright == 1) {
4771 u32 cpos;
4772 struct ocfs2_extent_list *el;
4773
4774 do_leftright++;
4775 split_rec = *orig_split_rec;
4776
4777 ocfs2_reinit_path(path, 1);
4778
4779 cpos = le32_to_cpu(split_rec.e_cpos);
4780 ret = ocfs2_find_path(inode, path, cpos);
4781 if (ret) {
4782 mlog_errno(ret);
4783 goto out;
4784 }
4785
4786 el = path_leaf_el(path);
4787 split_index = ocfs2_search_extent_list(el, cpos);
4788 goto leftright;
4789 }
4790out:
4791
4792 return ret;
4793}
4794
4795/*
4796 * Mark part or all of the extent record at split_index in the leaf
4797 * pointed to by path as written. This removes the unwritten
4798 * extent flag.
4799 *
4800 * Care is taken to handle contiguousness so as to not grow the tree.
4801 *
4802 * meta_ac is not strictly necessary - we only truly need it if growth
4803 * of the tree is required. All other cases will degrade into a less
4804 * optimal tree layout.
4805 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004806 * last_eb_bh should be the rightmost leaf block for any extent
4807 * btree. Since a split may grow the tree or a merge might shrink it,
4808 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07004809 *
4810 * This code is optimized for readability - several passes might be
4811 * made over certain portions of the tree. All of those blocks will
4812 * have been brought into cache (and pinned via the journal), so the
4813 * extra overhead is not expressed in terms of disk reads.
4814 */
4815static int __ocfs2_mark_extent_written(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004816 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004817 handle_t *handle,
4818 struct ocfs2_path *path,
4819 int split_index,
4820 struct ocfs2_extent_rec *split_rec,
4821 struct ocfs2_alloc_context *meta_ac,
4822 struct ocfs2_cached_dealloc_ctxt *dealloc)
4823{
4824 int ret = 0;
4825 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004826 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07004827 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4828 struct ocfs2_merge_ctxt ctxt;
4829 struct ocfs2_extent_list *rightmost_el;
4830
Roel Kluin3cf0c502007-10-27 00:20:36 +02004831 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004832 ret = -EIO;
4833 mlog_errno(ret);
4834 goto out;
4835 }
4836
4837 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4838 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4839 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4840 ret = -EIO;
4841 mlog_errno(ret);
4842 goto out;
4843 }
4844
Tao Maad5a4d72008-01-30 14:21:32 +08004845 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07004846 split_index,
4847 split_rec);
4848
4849 /*
4850 * The core merge / split code wants to know how much room is
4851 * left in this inodes allocation tree, so we pass the
4852 * rightmost extent list.
4853 */
4854 if (path->p_tree_depth) {
4855 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004856
4857 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
Joel Becker35dc0aa2008-08-20 16:25:06 -07004858 ocfs2_et_get_last_eb_blk(et),
Mark Fasheh328d5752007-06-18 10:48:04 -07004859 &last_eb_bh, OCFS2_BH_CACHED, inode);
4860 if (ret) {
4861 mlog_exit(ret);
4862 goto out;
4863 }
4864
4865 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4866 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4867 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4868 ret = -EROFS;
4869 goto out;
4870 }
4871
4872 rightmost_el = &eb->h_list;
4873 } else
4874 rightmost_el = path_root_el(path);
4875
Mark Fasheh328d5752007-06-18 10:48:04 -07004876 if (rec->e_cpos == split_rec->e_cpos &&
4877 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4878 ctxt.c_split_covers_rec = 1;
4879 else
4880 ctxt.c_split_covers_rec = 0;
4881
4882 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4883
Mark Fasheh015452b2007-09-12 10:21:22 -07004884 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4885 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4886 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004887
4888 if (ctxt.c_contig_type == CONTIG_NONE) {
4889 if (ctxt.c_split_covers_rec)
4890 el->l_recs[split_index] = *split_rec;
4891 else
Tao Mae7d4cb62008-08-18 17:38:44 +08004892 ret = ocfs2_split_and_insert(inode, handle, path, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004893 &last_eb_bh, split_index,
4894 split_rec, meta_ac);
4895 if (ret)
4896 mlog_errno(ret);
4897 } else {
4898 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4899 split_index, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08004900 dealloc, &ctxt, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07004901 if (ret)
4902 mlog_errno(ret);
4903 }
4904
Mark Fasheh328d5752007-06-18 10:48:04 -07004905out:
4906 brelse(last_eb_bh);
4907 return ret;
4908}
4909
4910/*
4911 * Mark the already-existing extent at cpos as written for len clusters.
4912 *
4913 * If the existing extent is larger than the request, initiate a
4914 * split. An attempt will be made at merging with adjacent extents.
4915 *
4916 * The caller is responsible for passing down meta_ac if we'll need it.
4917 */
Tao Mae7d4cb62008-08-18 17:38:44 +08004918int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07004919 handle_t *handle, u32 cpos, u32 len, u32 phys,
4920 struct ocfs2_alloc_context *meta_ac,
Tao Mae7d4cb62008-08-18 17:38:44 +08004921 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Maf56654c2008-08-18 17:38:48 +08004922 enum ocfs2_extent_tree_type et_type,
Joel Beckerea5efa12008-08-20 16:57:27 -07004923 void *obj)
Mark Fasheh328d5752007-06-18 10:48:04 -07004924{
4925 int ret, index;
4926 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4927 struct ocfs2_extent_rec split_rec;
4928 struct ocfs2_path *left_path = NULL;
4929 struct ocfs2_extent_list *el;
Joel Beckerdc0ce612008-08-20 16:48:35 -07004930 struct ocfs2_extent_tree et;
Mark Fasheh328d5752007-06-18 10:48:04 -07004931
4932 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4933 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4934
Joel Beckerea5efa12008-08-20 16:57:27 -07004935 ocfs2_get_extent_tree(&et, inode, root_bh, et_type, obj);
Joel Beckerdc0ce612008-08-20 16:48:35 -07004936
Mark Fasheh328d5752007-06-18 10:48:04 -07004937 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4938 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4939 "that are being written to, but the feature bit "
4940 "is not set in the super block.",
4941 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4942 ret = -EROFS;
4943 goto out;
4944 }
4945
4946 /*
4947 * XXX: This should be fixed up so that we just re-insert the
4948 * next extent records.
4949 */
Tao Mae7d4cb62008-08-18 17:38:44 +08004950 if (et_type == OCFS2_DINODE_EXTENT)
4951 ocfs2_extent_map_trunc(inode, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07004952
Joel Beckerdc0ce612008-08-20 16:48:35 -07004953 left_path = ocfs2_new_path(et.et_root_bh, et.et_root_el);
Mark Fasheh328d5752007-06-18 10:48:04 -07004954 if (!left_path) {
4955 ret = -ENOMEM;
4956 mlog_errno(ret);
4957 goto out;
4958 }
4959
4960 ret = ocfs2_find_path(inode, left_path, cpos);
4961 if (ret) {
4962 mlog_errno(ret);
4963 goto out;
4964 }
4965 el = path_leaf_el(left_path);
4966
4967 index = ocfs2_search_extent_list(el, cpos);
4968 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4969 ocfs2_error(inode->i_sb,
4970 "Inode %llu has an extent at cpos %u which can no "
4971 "longer be found.\n",
4972 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4973 ret = -EROFS;
4974 goto out;
4975 }
4976
4977 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4978 split_rec.e_cpos = cpu_to_le32(cpos);
4979 split_rec.e_leaf_clusters = cpu_to_le16(len);
4980 split_rec.e_blkno = cpu_to_le64(start_blkno);
4981 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4982 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4983
Joel Beckerdc0ce612008-08-20 16:48:35 -07004984 ret = __ocfs2_mark_extent_written(inode, &et, handle, left_path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004985 index, &split_rec, meta_ac,
4986 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07004987 if (ret)
4988 mlog_errno(ret);
4989
4990out:
4991 ocfs2_free_path(left_path);
Joel Beckerdc0ce612008-08-20 16:48:35 -07004992 ocfs2_put_extent_tree(&et);
Mark Fasheh328d5752007-06-18 10:48:04 -07004993 return ret;
4994}
4995
Tao Mae7d4cb62008-08-18 17:38:44 +08004996static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07004997 handle_t *handle, struct ocfs2_path *path,
4998 int index, u32 new_range,
4999 struct ocfs2_alloc_context *meta_ac)
5000{
5001 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005002 struct buffer_head *last_eb_bh = NULL;
5003 struct ocfs2_extent_block *eb;
5004 struct ocfs2_extent_list *rightmost_el, *el;
5005 struct ocfs2_extent_rec split_rec;
5006 struct ocfs2_extent_rec *rec;
5007 struct ocfs2_insert_type insert;
5008
5009 /*
5010 * Setup the record to split before we grow the tree.
5011 */
5012 el = path_leaf_el(path);
5013 rec = &el->l_recs[index];
5014 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5015
5016 depth = path->p_tree_depth;
5017 if (depth > 0) {
5018 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
Joel Becker35dc0aa2008-08-20 16:25:06 -07005019 ocfs2_et_get_last_eb_blk(et),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005020 &last_eb_bh, OCFS2_BH_CACHED, inode);
5021 if (ret < 0) {
5022 mlog_errno(ret);
5023 goto out;
5024 }
5025
5026 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5027 rightmost_el = &eb->h_list;
5028 } else
5029 rightmost_el = path_leaf_el(path);
5030
Tao Ma811f9332008-08-18 17:38:43 +08005031 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005032 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005033 ret = ocfs2_extend_trans(handle, credits);
5034 if (ret) {
5035 mlog_errno(ret);
5036 goto out;
5037 }
5038
5039 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5040 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005041 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005042 meta_ac);
5043 if (ret) {
5044 mlog_errno(ret);
5045 goto out;
5046 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005047 }
5048
5049 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5050 insert.ins_appending = APPEND_NONE;
5051 insert.ins_contig = CONTIG_NONE;
5052 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005053 insert.ins_tree_depth = depth;
5054
Tao Mae7d4cb62008-08-18 17:38:44 +08005055 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005056 if (ret)
5057 mlog_errno(ret);
5058
5059out:
5060 brelse(last_eb_bh);
5061 return ret;
5062}
5063
5064static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5065 struct ocfs2_path *path, int index,
5066 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08005067 u32 cpos, u32 len,
5068 struct ocfs2_extent_tree *et)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005069{
5070 int ret;
5071 u32 left_cpos, rec_range, trunc_range;
5072 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5073 struct super_block *sb = inode->i_sb;
5074 struct ocfs2_path *left_path = NULL;
5075 struct ocfs2_extent_list *el = path_leaf_el(path);
5076 struct ocfs2_extent_rec *rec;
5077 struct ocfs2_extent_block *eb;
5078
5079 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005080 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005081 if (ret) {
5082 mlog_errno(ret);
5083 goto out;
5084 }
5085
5086 index--;
5087 }
5088
5089 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5090 path->p_tree_depth) {
5091 /*
5092 * Check whether this is the rightmost tree record. If
5093 * we remove all of this record or part of its right
5094 * edge then an update of the record lengths above it
5095 * will be required.
5096 */
5097 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5098 if (eb->h_next_leaf_blk == 0)
5099 is_rightmost_tree_rec = 1;
5100 }
5101
5102 rec = &el->l_recs[index];
5103 if (index == 0 && path->p_tree_depth &&
5104 le32_to_cpu(rec->e_cpos) == cpos) {
5105 /*
5106 * Changing the leftmost offset (via partial or whole
5107 * record truncate) of an interior (or rightmost) path
5108 * means we have to update the subtree that is formed
5109 * by this leaf and the one to it's left.
5110 *
5111 * There are two cases we can skip:
5112 * 1) Path is the leftmost one in our inode tree.
5113 * 2) The leaf is rightmost and will be empty after
5114 * we remove the extent record - the rotate code
5115 * knows how to update the newly formed edge.
5116 */
5117
5118 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5119 &left_cpos);
5120 if (ret) {
5121 mlog_errno(ret);
5122 goto out;
5123 }
5124
5125 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5126 left_path = ocfs2_new_path(path_root_bh(path),
5127 path_root_el(path));
5128 if (!left_path) {
5129 ret = -ENOMEM;
5130 mlog_errno(ret);
5131 goto out;
5132 }
5133
5134 ret = ocfs2_find_path(inode, left_path, left_cpos);
5135 if (ret) {
5136 mlog_errno(ret);
5137 goto out;
5138 }
5139 }
5140 }
5141
5142 ret = ocfs2_extend_rotate_transaction(handle, 0,
5143 handle->h_buffer_credits,
5144 path);
5145 if (ret) {
5146 mlog_errno(ret);
5147 goto out;
5148 }
5149
5150 ret = ocfs2_journal_access_path(inode, handle, path);
5151 if (ret) {
5152 mlog_errno(ret);
5153 goto out;
5154 }
5155
5156 ret = ocfs2_journal_access_path(inode, handle, left_path);
5157 if (ret) {
5158 mlog_errno(ret);
5159 goto out;
5160 }
5161
5162 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5163 trunc_range = cpos + len;
5164
5165 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5166 int next_free;
5167
5168 memset(rec, 0, sizeof(*rec));
5169 ocfs2_cleanup_merge(el, index);
5170 wants_rotate = 1;
5171
5172 next_free = le16_to_cpu(el->l_next_free_rec);
5173 if (is_rightmost_tree_rec && next_free > 1) {
5174 /*
5175 * We skip the edge update if this path will
5176 * be deleted by the rotate code.
5177 */
5178 rec = &el->l_recs[next_free - 1];
5179 ocfs2_adjust_rightmost_records(inode, handle, path,
5180 rec);
5181 }
5182 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5183 /* Remove leftmost portion of the record. */
5184 le32_add_cpu(&rec->e_cpos, len);
5185 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5186 le16_add_cpu(&rec->e_leaf_clusters, -len);
5187 } else if (rec_range == trunc_range) {
5188 /* Remove rightmost portion of the record */
5189 le16_add_cpu(&rec->e_leaf_clusters, -len);
5190 if (is_rightmost_tree_rec)
5191 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5192 } else {
5193 /* Caller should have trapped this. */
5194 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5195 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5196 le32_to_cpu(rec->e_cpos),
5197 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5198 BUG();
5199 }
5200
5201 if (left_path) {
5202 int subtree_index;
5203
5204 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5205 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5206 subtree_index);
5207 }
5208
5209 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5210
Tao Mae7d4cb62008-08-18 17:38:44 +08005211 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005212 if (ret) {
5213 mlog_errno(ret);
5214 goto out;
5215 }
5216
5217out:
5218 ocfs2_free_path(left_path);
5219 return ret;
5220}
5221
Tao Mae7d4cb62008-08-18 17:38:44 +08005222int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
Mark Fasheh063c4562007-07-03 13:34:11 -07005223 u32 cpos, u32 len, handle_t *handle,
5224 struct ocfs2_alloc_context *meta_ac,
Tao Mae7d4cb62008-08-18 17:38:44 +08005225 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Maf56654c2008-08-18 17:38:48 +08005226 enum ocfs2_extent_tree_type et_type,
Joel Beckerea5efa12008-08-20 16:57:27 -07005227 void *obj)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005228{
5229 int ret, index;
5230 u32 rec_range, trunc_range;
5231 struct ocfs2_extent_rec *rec;
5232 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005233 struct ocfs2_path *path = NULL;
Joel Beckerdc0ce612008-08-20 16:48:35 -07005234 struct ocfs2_extent_tree et;
Tao Mae7d4cb62008-08-18 17:38:44 +08005235
Joel Beckerea5efa12008-08-20 16:57:27 -07005236 ocfs2_get_extent_tree(&et, inode, root_bh, et_type, obj);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005237
5238 ocfs2_extent_map_trunc(inode, 0);
5239
Joel Beckerdc0ce612008-08-20 16:48:35 -07005240 path = ocfs2_new_path(et.et_root_bh, et.et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005241 if (!path) {
5242 ret = -ENOMEM;
5243 mlog_errno(ret);
5244 goto out;
5245 }
5246
5247 ret = ocfs2_find_path(inode, path, cpos);
5248 if (ret) {
5249 mlog_errno(ret);
5250 goto out;
5251 }
5252
5253 el = path_leaf_el(path);
5254 index = ocfs2_search_extent_list(el, cpos);
5255 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5256 ocfs2_error(inode->i_sb,
5257 "Inode %llu has an extent at cpos %u which can no "
5258 "longer be found.\n",
5259 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5260 ret = -EROFS;
5261 goto out;
5262 }
5263
5264 /*
5265 * We have 3 cases of extent removal:
5266 * 1) Range covers the entire extent rec
5267 * 2) Range begins or ends on one edge of the extent rec
5268 * 3) Range is in the middle of the extent rec (no shared edges)
5269 *
5270 * For case 1 we remove the extent rec and left rotate to
5271 * fill the hole.
5272 *
5273 * For case 2 we just shrink the existing extent rec, with a
5274 * tree update if the shrinking edge is also the edge of an
5275 * extent block.
5276 *
5277 * For case 3 we do a right split to turn the extent rec into
5278 * something case 2 can handle.
5279 */
5280 rec = &el->l_recs[index];
5281 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5282 trunc_range = cpos + len;
5283
5284 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5285
5286 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5287 "(cpos %u, len %u)\n",
5288 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5289 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5290
5291 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5292 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerdc0ce612008-08-20 16:48:35 -07005293 cpos, len, &et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005294 if (ret) {
5295 mlog_errno(ret);
5296 goto out;
5297 }
5298 } else {
Joel Beckerdc0ce612008-08-20 16:48:35 -07005299 ret = ocfs2_split_tree(inode, &et, handle, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005300 trunc_range, meta_ac);
5301 if (ret) {
5302 mlog_errno(ret);
5303 goto out;
5304 }
5305
5306 /*
5307 * The split could have manipulated the tree enough to
5308 * move the record location, so we have to look for it again.
5309 */
5310 ocfs2_reinit_path(path, 1);
5311
5312 ret = ocfs2_find_path(inode, path, cpos);
5313 if (ret) {
5314 mlog_errno(ret);
5315 goto out;
5316 }
5317
5318 el = path_leaf_el(path);
5319 index = ocfs2_search_extent_list(el, cpos);
5320 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5321 ocfs2_error(inode->i_sb,
5322 "Inode %llu: split at cpos %u lost record.",
5323 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5324 cpos);
5325 ret = -EROFS;
5326 goto out;
5327 }
5328
5329 /*
5330 * Double check our values here. If anything is fishy,
5331 * it's easier to catch it at the top level.
5332 */
5333 rec = &el->l_recs[index];
5334 rec_range = le32_to_cpu(rec->e_cpos) +
5335 ocfs2_rec_clusters(el, rec);
5336 if (rec_range != trunc_range) {
5337 ocfs2_error(inode->i_sb,
5338 "Inode %llu: error after split at cpos %u"
5339 "trunc len %u, existing record is (%u,%u)",
5340 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5341 cpos, len, le32_to_cpu(rec->e_cpos),
5342 ocfs2_rec_clusters(el, rec));
5343 ret = -EROFS;
5344 goto out;
5345 }
5346
5347 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerdc0ce612008-08-20 16:48:35 -07005348 cpos, len, &et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005349 if (ret) {
5350 mlog_errno(ret);
5351 goto out;
5352 }
5353 }
5354
5355out:
5356 ocfs2_free_path(path);
Joel Beckerdc0ce612008-08-20 16:48:35 -07005357 ocfs2_put_extent_tree(&et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005358 return ret;
5359}
5360
Mark Fasheh063c4562007-07-03 13:34:11 -07005361int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005362{
5363 struct buffer_head *tl_bh = osb->osb_tl_bh;
5364 struct ocfs2_dinode *di;
5365 struct ocfs2_truncate_log *tl;
5366
5367 di = (struct ocfs2_dinode *) tl_bh->b_data;
5368 tl = &di->id2.i_dealloc;
5369
5370 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5371 "slot %d, invalid truncate log parameters: used = "
5372 "%u, count = %u\n", osb->slot_num,
5373 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5374 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5375}
5376
5377static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5378 unsigned int new_start)
5379{
5380 unsigned int tail_index;
5381 unsigned int current_tail;
5382
5383 /* No records, nothing to coalesce */
5384 if (!le16_to_cpu(tl->tl_used))
5385 return 0;
5386
5387 tail_index = le16_to_cpu(tl->tl_used) - 1;
5388 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5389 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5390
5391 return current_tail == new_start;
5392}
5393
Mark Fasheh063c4562007-07-03 13:34:11 -07005394int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5395 handle_t *handle,
5396 u64 start_blk,
5397 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005398{
5399 int status, index;
5400 unsigned int start_cluster, tl_count;
5401 struct inode *tl_inode = osb->osb_tl_inode;
5402 struct buffer_head *tl_bh = osb->osb_tl_bh;
5403 struct ocfs2_dinode *di;
5404 struct ocfs2_truncate_log *tl;
5405
Mark Fashehb06970532006-03-03 10:24:33 -08005406 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5407 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005408
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005409 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005410
5411 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5412
5413 di = (struct ocfs2_dinode *) tl_bh->b_data;
5414 tl = &di->id2.i_dealloc;
5415 if (!OCFS2_IS_VALID_DINODE(di)) {
5416 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5417 status = -EIO;
5418 goto bail;
5419 }
5420
5421 tl_count = le16_to_cpu(tl->tl_count);
5422 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5423 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005424 "Truncate record count on #%llu invalid "
5425 "wanted %u, actual %u\n",
5426 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005427 ocfs2_truncate_recs_per_inode(osb->sb),
5428 le16_to_cpu(tl->tl_count));
5429
5430 /* Caller should have known to flush before calling us. */
5431 index = le16_to_cpu(tl->tl_used);
5432 if (index >= tl_count) {
5433 status = -ENOSPC;
5434 mlog_errno(status);
5435 goto bail;
5436 }
5437
5438 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5439 OCFS2_JOURNAL_ACCESS_WRITE);
5440 if (status < 0) {
5441 mlog_errno(status);
5442 goto bail;
5443 }
5444
5445 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005446 "%llu (index = %d)\n", num_clusters, start_cluster,
5447 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005448
5449 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5450 /*
5451 * Move index back to the record we are coalescing with.
5452 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5453 */
5454 index--;
5455
5456 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5457 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5458 index, le32_to_cpu(tl->tl_recs[index].t_start),
5459 num_clusters);
5460 } else {
5461 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5462 tl->tl_used = cpu_to_le16(index + 1);
5463 }
5464 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5465
5466 status = ocfs2_journal_dirty(handle, tl_bh);
5467 if (status < 0) {
5468 mlog_errno(status);
5469 goto bail;
5470 }
5471
5472bail:
5473 mlog_exit(status);
5474 return status;
5475}
5476
5477static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005478 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005479 struct inode *data_alloc_inode,
5480 struct buffer_head *data_alloc_bh)
5481{
5482 int status = 0;
5483 int i;
5484 unsigned int num_clusters;
5485 u64 start_blk;
5486 struct ocfs2_truncate_rec rec;
5487 struct ocfs2_dinode *di;
5488 struct ocfs2_truncate_log *tl;
5489 struct inode *tl_inode = osb->osb_tl_inode;
5490 struct buffer_head *tl_bh = osb->osb_tl_bh;
5491
5492 mlog_entry_void();
5493
5494 di = (struct ocfs2_dinode *) tl_bh->b_data;
5495 tl = &di->id2.i_dealloc;
5496 i = le16_to_cpu(tl->tl_used) - 1;
5497 while (i >= 0) {
5498 /* Caller has given us at least enough credits to
5499 * update the truncate log dinode */
5500 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5501 OCFS2_JOURNAL_ACCESS_WRITE);
5502 if (status < 0) {
5503 mlog_errno(status);
5504 goto bail;
5505 }
5506
5507 tl->tl_used = cpu_to_le16(i);
5508
5509 status = ocfs2_journal_dirty(handle, tl_bh);
5510 if (status < 0) {
5511 mlog_errno(status);
5512 goto bail;
5513 }
5514
5515 /* TODO: Perhaps we can calculate the bulk of the
5516 * credits up front rather than extending like
5517 * this. */
5518 status = ocfs2_extend_trans(handle,
5519 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5520 if (status < 0) {
5521 mlog_errno(status);
5522 goto bail;
5523 }
5524
5525 rec = tl->tl_recs[i];
5526 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5527 le32_to_cpu(rec.t_start));
5528 num_clusters = le32_to_cpu(rec.t_clusters);
5529
5530 /* if start_blk is not set, we ignore the record as
5531 * invalid. */
5532 if (start_blk) {
5533 mlog(0, "free record %d, start = %u, clusters = %u\n",
5534 i, le32_to_cpu(rec.t_start), num_clusters);
5535
5536 status = ocfs2_free_clusters(handle, data_alloc_inode,
5537 data_alloc_bh, start_blk,
5538 num_clusters);
5539 if (status < 0) {
5540 mlog_errno(status);
5541 goto bail;
5542 }
5543 }
5544 i--;
5545 }
5546
5547bail:
5548 mlog_exit(status);
5549 return status;
5550}
5551
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005552/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005553int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005554{
5555 int status;
5556 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005557 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005558 struct inode *tl_inode = osb->osb_tl_inode;
5559 struct inode *data_alloc_inode = NULL;
5560 struct buffer_head *tl_bh = osb->osb_tl_bh;
5561 struct buffer_head *data_alloc_bh = NULL;
5562 struct ocfs2_dinode *di;
5563 struct ocfs2_truncate_log *tl;
5564
5565 mlog_entry_void();
5566
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005567 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005568
5569 di = (struct ocfs2_dinode *) tl_bh->b_data;
5570 tl = &di->id2.i_dealloc;
5571 if (!OCFS2_IS_VALID_DINODE(di)) {
5572 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5573 status = -EIO;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005574 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005575 }
5576
5577 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005578 mlog(0, "Flush %u records from truncate log #%llu\n",
5579 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005580 if (!num_to_flush) {
5581 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005582 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005583 }
5584
5585 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5586 GLOBAL_BITMAP_SYSTEM_INODE,
5587 OCFS2_INVALID_SLOT);
5588 if (!data_alloc_inode) {
5589 status = -EINVAL;
5590 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005591 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005592 }
5593
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005594 mutex_lock(&data_alloc_inode->i_mutex);
5595
Mark Fashehe63aecb62007-10-18 15:30:42 -07005596 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005597 if (status < 0) {
5598 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005599 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005600 }
5601
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005602 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005603 if (IS_ERR(handle)) {
5604 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005605 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005606 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005607 }
5608
5609 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5610 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005611 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005612 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005613
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005614 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005615
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005616out_unlock:
5617 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005618 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005619
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005620out_mutex:
5621 mutex_unlock(&data_alloc_inode->i_mutex);
5622 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005623
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005624out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005625 mlog_exit(status);
5626 return status;
5627}
5628
5629int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5630{
5631 int status;
5632 struct inode *tl_inode = osb->osb_tl_inode;
5633
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005634 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005635 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005636 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005637
5638 return status;
5639}
5640
David Howellsc4028952006-11-22 14:57:56 +00005641static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005642{
5643 int status;
David Howellsc4028952006-11-22 14:57:56 +00005644 struct ocfs2_super *osb =
5645 container_of(work, struct ocfs2_super,
5646 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005647
5648 mlog_entry_void();
5649
5650 status = ocfs2_flush_truncate_log(osb);
5651 if (status < 0)
5652 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005653 else
5654 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005655
5656 mlog_exit(status);
5657}
5658
5659#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5660void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5661 int cancel)
5662{
5663 if (osb->osb_tl_inode) {
5664 /* We want to push off log flushes while truncates are
5665 * still running. */
5666 if (cancel)
5667 cancel_delayed_work(&osb->osb_truncate_log_wq);
5668
5669 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5670 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5671 }
5672}
5673
5674static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5675 int slot_num,
5676 struct inode **tl_inode,
5677 struct buffer_head **tl_bh)
5678{
5679 int status;
5680 struct inode *inode = NULL;
5681 struct buffer_head *bh = NULL;
5682
5683 inode = ocfs2_get_system_file_inode(osb,
5684 TRUNCATE_LOG_SYSTEM_INODE,
5685 slot_num);
5686 if (!inode) {
5687 status = -EINVAL;
5688 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5689 goto bail;
5690 }
5691
5692 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
5693 OCFS2_BH_CACHED, inode);
5694 if (status < 0) {
5695 iput(inode);
5696 mlog_errno(status);
5697 goto bail;
5698 }
5699
5700 *tl_inode = inode;
5701 *tl_bh = bh;
5702bail:
5703 mlog_exit(status);
5704 return status;
5705}
5706
5707/* called during the 1st stage of node recovery. we stamp a clean
5708 * truncate log and pass back a copy for processing later. if the
5709 * truncate log does not require processing, a *tl_copy is set to
5710 * NULL. */
5711int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5712 int slot_num,
5713 struct ocfs2_dinode **tl_copy)
5714{
5715 int status;
5716 struct inode *tl_inode = NULL;
5717 struct buffer_head *tl_bh = NULL;
5718 struct ocfs2_dinode *di;
5719 struct ocfs2_truncate_log *tl;
5720
5721 *tl_copy = NULL;
5722
5723 mlog(0, "recover truncate log from slot %d\n", slot_num);
5724
5725 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5726 if (status < 0) {
5727 mlog_errno(status);
5728 goto bail;
5729 }
5730
5731 di = (struct ocfs2_dinode *) tl_bh->b_data;
5732 tl = &di->id2.i_dealloc;
5733 if (!OCFS2_IS_VALID_DINODE(di)) {
5734 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
5735 status = -EIO;
5736 goto bail;
5737 }
5738
5739 if (le16_to_cpu(tl->tl_used)) {
5740 mlog(0, "We'll have %u logs to recover\n",
5741 le16_to_cpu(tl->tl_used));
5742
5743 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5744 if (!(*tl_copy)) {
5745 status = -ENOMEM;
5746 mlog_errno(status);
5747 goto bail;
5748 }
5749
5750 /* Assuming the write-out below goes well, this copy
5751 * will be passed back to recovery for processing. */
5752 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5753
5754 /* All we need to do to clear the truncate log is set
5755 * tl_used. */
5756 tl->tl_used = 0;
5757
5758 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5759 if (status < 0) {
5760 mlog_errno(status);
5761 goto bail;
5762 }
5763 }
5764
5765bail:
5766 if (tl_inode)
5767 iput(tl_inode);
5768 if (tl_bh)
5769 brelse(tl_bh);
5770
5771 if (status < 0 && (*tl_copy)) {
5772 kfree(*tl_copy);
5773 *tl_copy = NULL;
5774 }
5775
5776 mlog_exit(status);
5777 return status;
5778}
5779
5780int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5781 struct ocfs2_dinode *tl_copy)
5782{
5783 int status = 0;
5784 int i;
5785 unsigned int clusters, num_recs, start_cluster;
5786 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005787 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005788 struct inode *tl_inode = osb->osb_tl_inode;
5789 struct ocfs2_truncate_log *tl;
5790
5791 mlog_entry_void();
5792
5793 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5794 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5795 return -EINVAL;
5796 }
5797
5798 tl = &tl_copy->id2.i_dealloc;
5799 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005800 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07005801 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08005802
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005803 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005804 for(i = 0; i < num_recs; i++) {
5805 if (ocfs2_truncate_log_needs_flush(osb)) {
5806 status = __ocfs2_flush_truncate_log(osb);
5807 if (status < 0) {
5808 mlog_errno(status);
5809 goto bail_up;
5810 }
5811 }
5812
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005813 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005814 if (IS_ERR(handle)) {
5815 status = PTR_ERR(handle);
5816 mlog_errno(status);
5817 goto bail_up;
5818 }
5819
5820 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5821 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5822 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5823
5824 status = ocfs2_truncate_log_append(osb, handle,
5825 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005826 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005827 if (status < 0) {
5828 mlog_errno(status);
5829 goto bail_up;
5830 }
5831 }
5832
5833bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005834 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005835
5836 mlog_exit(status);
5837 return status;
5838}
5839
5840void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5841{
5842 int status;
5843 struct inode *tl_inode = osb->osb_tl_inode;
5844
5845 mlog_entry_void();
5846
5847 if (tl_inode) {
5848 cancel_delayed_work(&osb->osb_truncate_log_wq);
5849 flush_workqueue(ocfs2_wq);
5850
5851 status = ocfs2_flush_truncate_log(osb);
5852 if (status < 0)
5853 mlog_errno(status);
5854
5855 brelse(osb->osb_tl_bh);
5856 iput(osb->osb_tl_inode);
5857 }
5858
5859 mlog_exit_void();
5860}
5861
5862int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5863{
5864 int status;
5865 struct inode *tl_inode = NULL;
5866 struct buffer_head *tl_bh = NULL;
5867
5868 mlog_entry_void();
5869
5870 status = ocfs2_get_truncate_log_info(osb,
5871 osb->slot_num,
5872 &tl_inode,
5873 &tl_bh);
5874 if (status < 0)
5875 mlog_errno(status);
5876
5877 /* ocfs2_truncate_log_shutdown keys on the existence of
5878 * osb->osb_tl_inode so we don't set any of the osb variables
5879 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00005880 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5881 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08005882 osb->osb_tl_bh = tl_bh;
5883 osb->osb_tl_inode = tl_inode;
5884
5885 mlog_exit(status);
5886 return status;
5887}
5888
Mark Fasheh2b604352007-06-22 15:45:27 -07005889/*
5890 * Delayed de-allocation of suballocator blocks.
5891 *
5892 * Some sets of block de-allocations might involve multiple suballocator inodes.
5893 *
5894 * The locking for this can get extremely complicated, especially when
5895 * the suballocator inodes to delete from aren't known until deep
5896 * within an unrelated codepath.
5897 *
5898 * ocfs2_extent_block structures are a good example of this - an inode
5899 * btree could have been grown by any number of nodes each allocating
5900 * out of their own suballoc inode.
5901 *
5902 * These structures allow the delay of block de-allocation until a
5903 * later time, when locking of multiple cluster inodes won't cause
5904 * deadlock.
5905 */
5906
5907/*
5908 * Describes a single block free from a suballocator
5909 */
5910struct ocfs2_cached_block_free {
5911 struct ocfs2_cached_block_free *free_next;
5912 u64 free_blk;
5913 unsigned int free_bit;
5914};
5915
5916struct ocfs2_per_slot_free_list {
5917 struct ocfs2_per_slot_free_list *f_next_suballocator;
5918 int f_inode_type;
5919 int f_slot;
5920 struct ocfs2_cached_block_free *f_first;
5921};
5922
5923static int ocfs2_free_cached_items(struct ocfs2_super *osb,
5924 int sysfile_type,
5925 int slot,
5926 struct ocfs2_cached_block_free *head)
5927{
5928 int ret;
5929 u64 bg_blkno;
5930 handle_t *handle;
5931 struct inode *inode;
5932 struct buffer_head *di_bh = NULL;
5933 struct ocfs2_cached_block_free *tmp;
5934
5935 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5936 if (!inode) {
5937 ret = -EINVAL;
5938 mlog_errno(ret);
5939 goto out;
5940 }
5941
5942 mutex_lock(&inode->i_mutex);
5943
Mark Fashehe63aecb62007-10-18 15:30:42 -07005944 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07005945 if (ret) {
5946 mlog_errno(ret);
5947 goto out_mutex;
5948 }
5949
5950 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5951 if (IS_ERR(handle)) {
5952 ret = PTR_ERR(handle);
5953 mlog_errno(ret);
5954 goto out_unlock;
5955 }
5956
5957 while (head) {
5958 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5959 head->free_bit);
5960 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5961 head->free_bit, (unsigned long long)head->free_blk);
5962
5963 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5964 head->free_bit, bg_blkno, 1);
5965 if (ret) {
5966 mlog_errno(ret);
5967 goto out_journal;
5968 }
5969
5970 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
5971 if (ret) {
5972 mlog_errno(ret);
5973 goto out_journal;
5974 }
5975
5976 tmp = head;
5977 head = head->free_next;
5978 kfree(tmp);
5979 }
5980
5981out_journal:
5982 ocfs2_commit_trans(osb, handle);
5983
5984out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07005985 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07005986 brelse(di_bh);
5987out_mutex:
5988 mutex_unlock(&inode->i_mutex);
5989 iput(inode);
5990out:
5991 while(head) {
5992 /* Premature exit may have left some dangling items. */
5993 tmp = head;
5994 head = head->free_next;
5995 kfree(tmp);
5996 }
5997
5998 return ret;
5999}
6000
6001int ocfs2_run_deallocs(struct ocfs2_super *osb,
6002 struct ocfs2_cached_dealloc_ctxt *ctxt)
6003{
6004 int ret = 0, ret2;
6005 struct ocfs2_per_slot_free_list *fl;
6006
6007 if (!ctxt)
6008 return 0;
6009
6010 while (ctxt->c_first_suballocator) {
6011 fl = ctxt->c_first_suballocator;
6012
6013 if (fl->f_first) {
6014 mlog(0, "Free items: (type %u, slot %d)\n",
6015 fl->f_inode_type, fl->f_slot);
6016 ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
6017 fl->f_slot, fl->f_first);
6018 if (ret2)
6019 mlog_errno(ret2);
6020 if (!ret)
6021 ret = ret2;
6022 }
6023
6024 ctxt->c_first_suballocator = fl->f_next_suballocator;
6025 kfree(fl);
6026 }
6027
6028 return ret;
6029}
6030
6031static struct ocfs2_per_slot_free_list *
6032ocfs2_find_per_slot_free_list(int type,
6033 int slot,
6034 struct ocfs2_cached_dealloc_ctxt *ctxt)
6035{
6036 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6037
6038 while (fl) {
6039 if (fl->f_inode_type == type && fl->f_slot == slot)
6040 return fl;
6041
6042 fl = fl->f_next_suballocator;
6043 }
6044
6045 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6046 if (fl) {
6047 fl->f_inode_type = type;
6048 fl->f_slot = slot;
6049 fl->f_first = NULL;
6050 fl->f_next_suballocator = ctxt->c_first_suballocator;
6051
6052 ctxt->c_first_suballocator = fl;
6053 }
6054 return fl;
6055}
6056
6057static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6058 int type, int slot, u64 blkno,
6059 unsigned int bit)
6060{
6061 int ret;
6062 struct ocfs2_per_slot_free_list *fl;
6063 struct ocfs2_cached_block_free *item;
6064
6065 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6066 if (fl == NULL) {
6067 ret = -ENOMEM;
6068 mlog_errno(ret);
6069 goto out;
6070 }
6071
6072 item = kmalloc(sizeof(*item), GFP_NOFS);
6073 if (item == NULL) {
6074 ret = -ENOMEM;
6075 mlog_errno(ret);
6076 goto out;
6077 }
6078
6079 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6080 type, slot, bit, (unsigned long long)blkno);
6081
6082 item->free_blk = blkno;
6083 item->free_bit = bit;
6084 item->free_next = fl->f_first;
6085
6086 fl->f_first = item;
6087
6088 ret = 0;
6089out:
6090 return ret;
6091}
6092
Mark Fasheh59a5e412007-06-22 15:52:36 -07006093static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6094 struct ocfs2_extent_block *eb)
6095{
6096 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6097 le16_to_cpu(eb->h_suballoc_slot),
6098 le64_to_cpu(eb->h_blkno),
6099 le16_to_cpu(eb->h_suballoc_bit));
6100}
6101
Mark Fashehccd979b2005-12-15 14:31:24 -08006102/* This function will figure out whether the currently last extent
6103 * block will be deleted, and if it will, what the new last extent
6104 * block will be so we can update his h_next_leaf_blk field, as well
6105 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006106static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006107 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006108 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006109 struct buffer_head **new_last_eb)
6110{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006111 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006112 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006113 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006114 struct ocfs2_extent_block *eb;
6115 struct ocfs2_extent_list *el;
6116 struct buffer_head *bh = NULL;
6117
6118 *new_last_eb = NULL;
6119
Mark Fashehccd979b2005-12-15 14:31:24 -08006120 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006121 if (!path->p_tree_depth)
6122 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006123
6124 /* trunc to zero special case - this makes tree_depth = 0
6125 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006126 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006127 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006128
Mark Fashehdcd05382007-01-16 11:32:23 -08006129 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006130 BUG_ON(!el->l_next_free_rec);
6131
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006132 /*
6133 * Make sure that this extent list will actually be empty
6134 * after we clear away the data. We can shortcut out if
6135 * there's more than one non-empty extent in the
6136 * list. Otherwise, a check of the remaining extent is
6137 * necessary.
6138 */
6139 next_free = le16_to_cpu(el->l_next_free_rec);
6140 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006141 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006142 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006143 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006144
6145 /* We may have a valid extent in index 1, check it. */
6146 if (next_free == 2)
6147 rec = &el->l_recs[1];
6148
6149 /*
6150 * Fall through - no more nonempty extents, so we want
6151 * to delete this leaf.
6152 */
6153 } else {
6154 if (next_free > 1)
6155 goto out;
6156
6157 rec = &el->l_recs[0];
6158 }
6159
6160 if (rec) {
6161 /*
6162 * Check it we'll only be trimming off the end of this
6163 * cluster.
6164 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006165 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006166 goto out;
6167 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006168
Mark Fashehdcd05382007-01-16 11:32:23 -08006169 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6170 if (ret) {
6171 mlog_errno(ret);
6172 goto out;
6173 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006174
Mark Fashehdcd05382007-01-16 11:32:23 -08006175 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6176 if (ret) {
6177 mlog_errno(ret);
6178 goto out;
6179 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006180
Mark Fashehdcd05382007-01-16 11:32:23 -08006181 eb = (struct ocfs2_extent_block *) bh->b_data;
6182 el = &eb->h_list;
6183 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
6184 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
6185 ret = -EROFS;
6186 goto out;
6187 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006188
6189 *new_last_eb = bh;
6190 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006191 mlog(0, "returning block %llu, (cpos: %u)\n",
6192 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6193out:
6194 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006195
Mark Fashehdcd05382007-01-16 11:32:23 -08006196 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006197}
6198
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006199/*
6200 * Trim some clusters off the rightmost edge of a tree. Only called
6201 * during truncate.
6202 *
6203 * The caller needs to:
6204 * - start journaling of each path component.
6205 * - compute and fully set up any new last ext block
6206 */
6207static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6208 handle_t *handle, struct ocfs2_truncate_context *tc,
6209 u32 clusters_to_del, u64 *delete_start)
6210{
6211 int ret, i, index = path->p_tree_depth;
6212 u32 new_edge = 0;
6213 u64 deleted_eb = 0;
6214 struct buffer_head *bh;
6215 struct ocfs2_extent_list *el;
6216 struct ocfs2_extent_rec *rec;
6217
6218 *delete_start = 0;
6219
6220 while (index >= 0) {
6221 bh = path->p_node[index].bh;
6222 el = path->p_node[index].el;
6223
6224 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6225 index, (unsigned long long)bh->b_blocknr);
6226
6227 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6228
6229 if (index !=
6230 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6231 ocfs2_error(inode->i_sb,
6232 "Inode %lu has invalid ext. block %llu",
6233 inode->i_ino,
6234 (unsigned long long)bh->b_blocknr);
6235 ret = -EROFS;
6236 goto out;
6237 }
6238
6239find_tail_record:
6240 i = le16_to_cpu(el->l_next_free_rec) - 1;
6241 rec = &el->l_recs[i];
6242
6243 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6244 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006245 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006246 (unsigned long long)le64_to_cpu(rec->e_blkno),
6247 le16_to_cpu(el->l_next_free_rec));
6248
Mark Fashehe48edee2007-03-07 16:46:57 -08006249 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006250
6251 if (le16_to_cpu(el->l_tree_depth) == 0) {
6252 /*
6253 * If the leaf block contains a single empty
6254 * extent and no records, we can just remove
6255 * the block.
6256 */
6257 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6258 memset(rec, 0,
6259 sizeof(struct ocfs2_extent_rec));
6260 el->l_next_free_rec = cpu_to_le16(0);
6261
6262 goto delete;
6263 }
6264
6265 /*
6266 * Remove any empty extents by shifting things
6267 * left. That should make life much easier on
6268 * the code below. This condition is rare
6269 * enough that we shouldn't see a performance
6270 * hit.
6271 */
6272 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6273 le16_add_cpu(&el->l_next_free_rec, -1);
6274
6275 for(i = 0;
6276 i < le16_to_cpu(el->l_next_free_rec); i++)
6277 el->l_recs[i] = el->l_recs[i + 1];
6278
6279 memset(&el->l_recs[i], 0,
6280 sizeof(struct ocfs2_extent_rec));
6281
6282 /*
6283 * We've modified our extent list. The
6284 * simplest way to handle this change
6285 * is to being the search from the
6286 * start again.
6287 */
6288 goto find_tail_record;
6289 }
6290
Mark Fashehe48edee2007-03-07 16:46:57 -08006291 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006292
6293 /*
6294 * We'll use "new_edge" on our way back up the
6295 * tree to know what our rightmost cpos is.
6296 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006297 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006298 new_edge += le32_to_cpu(rec->e_cpos);
6299
6300 /*
6301 * The caller will use this to delete data blocks.
6302 */
6303 *delete_start = le64_to_cpu(rec->e_blkno)
6304 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006305 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006306
6307 /*
6308 * If it's now empty, remove this record.
6309 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006310 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006311 memset(rec, 0,
6312 sizeof(struct ocfs2_extent_rec));
6313 le16_add_cpu(&el->l_next_free_rec, -1);
6314 }
6315 } else {
6316 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6317 memset(rec, 0,
6318 sizeof(struct ocfs2_extent_rec));
6319 le16_add_cpu(&el->l_next_free_rec, -1);
6320
6321 goto delete;
6322 }
6323
6324 /* Can this actually happen? */
6325 if (le16_to_cpu(el->l_next_free_rec) == 0)
6326 goto delete;
6327
6328 /*
6329 * We never actually deleted any clusters
6330 * because our leaf was empty. There's no
6331 * reason to adjust the rightmost edge then.
6332 */
6333 if (new_edge == 0)
6334 goto delete;
6335
Mark Fashehe48edee2007-03-07 16:46:57 -08006336 rec->e_int_clusters = cpu_to_le32(new_edge);
6337 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006338 -le32_to_cpu(rec->e_cpos));
6339
6340 /*
6341 * A deleted child record should have been
6342 * caught above.
6343 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006344 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006345 }
6346
6347delete:
6348 ret = ocfs2_journal_dirty(handle, bh);
6349 if (ret) {
6350 mlog_errno(ret);
6351 goto out;
6352 }
6353
6354 mlog(0, "extent list container %llu, after: record %d: "
6355 "(%u, %u, %llu), next = %u.\n",
6356 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006357 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006358 (unsigned long long)le64_to_cpu(rec->e_blkno),
6359 le16_to_cpu(el->l_next_free_rec));
6360
6361 /*
6362 * We must be careful to only attempt delete of an
6363 * extent block (and not the root inode block).
6364 */
6365 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6366 struct ocfs2_extent_block *eb =
6367 (struct ocfs2_extent_block *)bh->b_data;
6368
6369 /*
6370 * Save this for use when processing the
6371 * parent block.
6372 */
6373 deleted_eb = le64_to_cpu(eb->h_blkno);
6374
6375 mlog(0, "deleting this extent block.\n");
6376
6377 ocfs2_remove_from_cache(inode, bh);
6378
Mark Fashehe48edee2007-03-07 16:46:57 -08006379 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006380 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6381 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6382
Mark Fasheh59a5e412007-06-22 15:52:36 -07006383 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6384 /* An error here is not fatal. */
6385 if (ret < 0)
6386 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006387 } else {
6388 deleted_eb = 0;
6389 }
6390
6391 index--;
6392 }
6393
6394 ret = 0;
6395out:
6396 return ret;
6397}
6398
Mark Fashehccd979b2005-12-15 14:31:24 -08006399static int ocfs2_do_truncate(struct ocfs2_super *osb,
6400 unsigned int clusters_to_del,
6401 struct inode *inode,
6402 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006403 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006404 struct ocfs2_truncate_context *tc,
6405 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006406{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006407 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006408 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006409 struct ocfs2_extent_block *last_eb = NULL;
6410 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006411 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006412 u64 delete_blk = 0;
6413
6414 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6415
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006416 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006417 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006418 if (status < 0) {
6419 mlog_errno(status);
6420 goto bail;
6421 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006422
Mark Fashehdcd05382007-01-16 11:32:23 -08006423 /*
6424 * Each component will be touched, so we might as well journal
6425 * here to avoid having to handle errors later.
6426 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006427 status = ocfs2_journal_access_path(inode, handle, path);
6428 if (status < 0) {
6429 mlog_errno(status);
6430 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006431 }
6432
6433 if (last_eb_bh) {
6434 status = ocfs2_journal_access(handle, inode, last_eb_bh,
6435 OCFS2_JOURNAL_ACCESS_WRITE);
6436 if (status < 0) {
6437 mlog_errno(status);
6438 goto bail;
6439 }
6440
6441 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6442 }
6443
6444 el = &(fe->id2.i_list);
6445
6446 /*
6447 * Lower levels depend on this never happening, but it's best
6448 * to check it up here before changing the tree.
6449 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006450 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006451 ocfs2_error(inode->i_sb,
6452 "Inode %lu has an empty extent record, depth %u\n",
6453 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006454 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006455 goto bail;
6456 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006457
6458 spin_lock(&OCFS2_I(inode)->ip_lock);
6459 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6460 clusters_to_del;
6461 spin_unlock(&OCFS2_I(inode)->ip_lock);
6462 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006463 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006464
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006465 status = ocfs2_trim_tree(inode, path, handle, tc,
6466 clusters_to_del, &delete_blk);
6467 if (status) {
6468 mlog_errno(status);
6469 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006470 }
6471
Mark Fashehdcd05382007-01-16 11:32:23 -08006472 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006473 /* trunc to zero is a special case. */
6474 el->l_tree_depth = 0;
6475 fe->i_last_eb_blk = 0;
6476 } else if (last_eb)
6477 fe->i_last_eb_blk = last_eb->h_blkno;
6478
6479 status = ocfs2_journal_dirty(handle, fe_bh);
6480 if (status < 0) {
6481 mlog_errno(status);
6482 goto bail;
6483 }
6484
6485 if (last_eb) {
6486 /* If there will be a new last extent block, then by
6487 * definition, there cannot be any leaves to the right of
6488 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006489 last_eb->h_next_leaf_blk = 0;
6490 status = ocfs2_journal_dirty(handle, last_eb_bh);
6491 if (status < 0) {
6492 mlog_errno(status);
6493 goto bail;
6494 }
6495 }
6496
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006497 if (delete_blk) {
6498 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6499 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006500 if (status < 0) {
6501 mlog_errno(status);
6502 goto bail;
6503 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006504 }
6505 status = 0;
6506bail:
Mark Fashehdcd05382007-01-16 11:32:23 -08006507
Mark Fashehccd979b2005-12-15 14:31:24 -08006508 mlog_exit(status);
6509 return status;
6510}
6511
Mark Fasheh60b11392007-02-16 11:46:50 -08006512static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
6513{
6514 set_buffer_uptodate(bh);
6515 mark_buffer_dirty(bh);
6516 return 0;
6517}
6518
6519static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
6520{
6521 set_buffer_uptodate(bh);
6522 mark_buffer_dirty(bh);
6523 return ocfs2_journal_dirty_data(handle, bh);
6524}
6525
Mark Fasheh1d410a62007-09-07 14:20:45 -07006526static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6527 unsigned int from, unsigned int to,
6528 struct page *page, int zero, u64 *phys)
6529{
6530 int ret, partial = 0;
6531
6532 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6533 if (ret)
6534 mlog_errno(ret);
6535
6536 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006537 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006538
6539 /*
6540 * Need to set the buffers we zero'd into uptodate
6541 * here if they aren't - ocfs2_map_page_blocks()
6542 * might've skipped some
6543 */
6544 if (ocfs2_should_order_data(inode)) {
6545 ret = walk_page_buffers(handle,
6546 page_buffers(page),
6547 from, to, &partial,
6548 ocfs2_ordered_zero_func);
6549 if (ret < 0)
6550 mlog_errno(ret);
6551 } else {
6552 ret = walk_page_buffers(handle, page_buffers(page),
6553 from, to, &partial,
6554 ocfs2_writeback_zero_func);
6555 if (ret < 0)
6556 mlog_errno(ret);
6557 }
6558
6559 if (!partial)
6560 SetPageUptodate(page);
6561
6562 flush_dcache_page(page);
6563}
6564
Mark Fasheh35edec12007-07-06 14:41:18 -07006565static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6566 loff_t end, struct page **pages,
6567 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006568{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006569 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006570 struct page *page;
6571 unsigned int from, to = PAGE_CACHE_SIZE;
6572 struct super_block *sb = inode->i_sb;
6573
6574 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6575
6576 if (numpages == 0)
6577 goto out;
6578
Mark Fasheh35edec12007-07-06 14:41:18 -07006579 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006580 for(i = 0; i < numpages; i++) {
6581 page = pages[i];
6582
Mark Fasheh35edec12007-07-06 14:41:18 -07006583 from = start & (PAGE_CACHE_SIZE - 1);
6584 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6585 to = end & (PAGE_CACHE_SIZE - 1);
6586
Mark Fasheh60b11392007-02-16 11:46:50 -08006587 BUG_ON(from > PAGE_CACHE_SIZE);
6588 BUG_ON(to > PAGE_CACHE_SIZE);
6589
Mark Fasheh1d410a62007-09-07 14:20:45 -07006590 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6591 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006592
Mark Fasheh35edec12007-07-06 14:41:18 -07006593 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006594 }
6595out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006596 if (pages)
6597 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006598}
6599
Mark Fasheh35edec12007-07-06 14:41:18 -07006600static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006601 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006602{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006603 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006604 struct super_block *sb = inode->i_sb;
6605 struct address_space *mapping = inode->i_mapping;
6606 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006607 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006608
Mark Fasheh35edec12007-07-06 14:41:18 -07006609 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006610
Mark Fasheh35edec12007-07-06 14:41:18 -07006611 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6612 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6613
Mark Fasheh1d410a62007-09-07 14:20:45 -07006614 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006615 last_page_bytes = PAGE_ALIGN(end);
6616 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006617 do {
6618 pages[numpages] = grab_cache_page(mapping, index);
6619 if (!pages[numpages]) {
6620 ret = -ENOMEM;
6621 mlog_errno(ret);
6622 goto out;
6623 }
6624
6625 numpages++;
6626 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006627 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006628
6629out:
6630 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006631 if (pages)
6632 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006633 numpages = 0;
6634 }
6635
6636 *num = numpages;
6637
6638 return ret;
6639}
6640
6641/*
6642 * Zero the area past i_size but still within an allocated
6643 * cluster. This avoids exposing nonzero data on subsequent file
6644 * extends.
6645 *
6646 * We need to call this before i_size is updated on the inode because
6647 * otherwise block_write_full_page() will skip writeout of pages past
6648 * i_size. The new_i_size parameter is passed for this reason.
6649 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006650int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6651 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006652{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006653 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006654 struct page **pages = NULL;
6655 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006656 unsigned int ext_flags;
6657 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006658
6659 /*
6660 * File systems which don't support sparse files zero on every
6661 * extend.
6662 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07006663 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08006664 return 0;
6665
Mark Fasheh1d410a62007-09-07 14:20:45 -07006666 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08006667 sizeof(struct page *), GFP_NOFS);
6668 if (pages == NULL) {
6669 ret = -ENOMEM;
6670 mlog_errno(ret);
6671 goto out;
6672 }
6673
Mark Fasheh1d410a62007-09-07 14:20:45 -07006674 if (range_start == range_end)
6675 goto out;
6676
6677 ret = ocfs2_extent_map_get_blocks(inode,
6678 range_start >> sb->s_blocksize_bits,
6679 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08006680 if (ret) {
6681 mlog_errno(ret);
6682 goto out;
6683 }
6684
Mark Fasheh1d410a62007-09-07 14:20:45 -07006685 /*
6686 * Tail is a hole, or is marked unwritten. In either case, we
6687 * can count on read and write to return/push zero's.
6688 */
6689 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08006690 goto out;
6691
Mark Fasheh1d410a62007-09-07 14:20:45 -07006692 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6693 &numpages);
6694 if (ret) {
6695 mlog_errno(ret);
6696 goto out;
6697 }
6698
Mark Fasheh35edec12007-07-06 14:41:18 -07006699 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6700 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08006701
6702 /*
6703 * Initiate writeout of the pages we zero'd here. We don't
6704 * wait on them - the truncate_inode_pages() call later will
6705 * do that for us.
6706 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006707 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6708 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08006709 if (ret)
6710 mlog_errno(ret);
6711
6712out:
6713 if (pages)
6714 kfree(pages);
6715
6716 return ret;
6717}
6718
Tiger Yangfdd77702008-08-18 17:08:55 +08006719static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6720 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006721{
6722 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08006723 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006724
Tiger Yangfdd77702008-08-18 17:08:55 +08006725 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6726 memset(&di->id2, 0, blocksize -
6727 offsetof(struct ocfs2_dinode, id2) -
6728 xattrsize);
6729 else
6730 memset(&di->id2, 0, blocksize -
6731 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006732}
6733
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006734void ocfs2_dinode_new_extent_list(struct inode *inode,
6735 struct ocfs2_dinode *di)
6736{
Tiger Yangfdd77702008-08-18 17:08:55 +08006737 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006738 di->id2.i_list.l_tree_depth = 0;
6739 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08006740 di->id2.i_list.l_count = cpu_to_le16(
6741 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006742}
6743
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006744void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6745{
6746 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6747 struct ocfs2_inline_data *idata = &di->id2.i_data;
6748
6749 spin_lock(&oi->ip_lock);
6750 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6751 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6752 spin_unlock(&oi->ip_lock);
6753
6754 /*
6755 * We clear the entire i_data structure here so that all
6756 * fields can be properly initialized.
6757 */
Tiger Yangfdd77702008-08-18 17:08:55 +08006758 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006759
Tiger Yangfdd77702008-08-18 17:08:55 +08006760 idata->id_count = cpu_to_le16(
6761 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006762}
6763
6764int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6765 struct buffer_head *di_bh)
6766{
6767 int ret, i, has_data, num_pages = 0;
6768 handle_t *handle;
6769 u64 uninitialized_var(block);
6770 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6771 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6772 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006773 struct ocfs2_alloc_context *data_ac = NULL;
6774 struct page **pages = NULL;
6775 loff_t end = osb->s_clustersize;
6776
6777 has_data = i_size_read(inode) ? 1 : 0;
6778
6779 if (has_data) {
6780 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6781 sizeof(struct page *), GFP_NOFS);
6782 if (pages == NULL) {
6783 ret = -ENOMEM;
6784 mlog_errno(ret);
6785 goto out;
6786 }
6787
6788 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6789 if (ret) {
6790 mlog_errno(ret);
6791 goto out;
6792 }
6793 }
6794
6795 handle = ocfs2_start_trans(osb, OCFS2_INLINE_TO_EXTENTS_CREDITS);
6796 if (IS_ERR(handle)) {
6797 ret = PTR_ERR(handle);
6798 mlog_errno(ret);
6799 goto out_unlock;
6800 }
6801
6802 ret = ocfs2_journal_access(handle, inode, di_bh,
6803 OCFS2_JOURNAL_ACCESS_WRITE);
6804 if (ret) {
6805 mlog_errno(ret);
6806 goto out_commit;
6807 }
6808
6809 if (has_data) {
6810 u32 bit_off, num;
6811 unsigned int page_end;
6812 u64 phys;
6813
6814 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
6815 &num);
6816 if (ret) {
6817 mlog_errno(ret);
6818 goto out_commit;
6819 }
6820
6821 /*
6822 * Save two copies, one for insert, and one that can
6823 * be changed by ocfs2_map_and_dirty_page() below.
6824 */
6825 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6826
6827 /*
6828 * Non sparse file systems zero on extend, so no need
6829 * to do that now.
6830 */
6831 if (!ocfs2_sparse_alloc(osb) &&
6832 PAGE_CACHE_SIZE < osb->s_clustersize)
6833 end = PAGE_CACHE_SIZE;
6834
6835 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6836 if (ret) {
6837 mlog_errno(ret);
6838 goto out_commit;
6839 }
6840
6841 /*
6842 * This should populate the 1st page for us and mark
6843 * it up to date.
6844 */
6845 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6846 if (ret) {
6847 mlog_errno(ret);
6848 goto out_commit;
6849 }
6850
6851 page_end = PAGE_CACHE_SIZE;
6852 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6853 page_end = osb->s_clustersize;
6854
6855 for (i = 0; i < num_pages; i++)
6856 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6857 pages[i], i > 0, &phys);
6858 }
6859
6860 spin_lock(&oi->ip_lock);
6861 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6862 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6863 spin_unlock(&oi->ip_lock);
6864
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006865 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006866
6867 ocfs2_journal_dirty(handle, di_bh);
6868
6869 if (has_data) {
6870 /*
6871 * An error at this point should be extremely rare. If
6872 * this proves to be false, we could always re-build
6873 * the in-inode data from our pages.
6874 */
Tao Maf56654c2008-08-18 17:38:48 +08006875 ret = ocfs2_dinode_insert_extent(osb, handle, inode, di_bh,
6876 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006877 if (ret) {
6878 mlog_errno(ret);
6879 goto out_commit;
6880 }
6881
6882 inode->i_blocks = ocfs2_inode_sector_count(inode);
6883 }
6884
6885out_commit:
6886 ocfs2_commit_trans(osb, handle);
6887
6888out_unlock:
6889 if (data_ac)
6890 ocfs2_free_alloc_context(data_ac);
6891
6892out:
6893 if (pages) {
6894 ocfs2_unlock_and_free_pages(pages, num_pages);
6895 kfree(pages);
6896 }
6897
6898 return ret;
6899}
6900
Mark Fashehccd979b2005-12-15 14:31:24 -08006901/*
6902 * It is expected, that by the time you call this function,
6903 * inode->i_size and fe->i_size have been adjusted.
6904 *
6905 * WARNING: This will kfree the truncate context
6906 */
6907int ocfs2_commit_truncate(struct ocfs2_super *osb,
6908 struct inode *inode,
6909 struct buffer_head *fe_bh,
6910 struct ocfs2_truncate_context *tc)
6911{
6912 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006913 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08006914 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006915 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006916 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08006917 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08006918 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08006919
6920 mlog_entry_void();
6921
Mark Fashehdcd05382007-01-16 11:32:23 -08006922 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08006923 i_size_read(inode));
6924
Tao Mae7d4cb62008-08-18 17:38:44 +08006925 path = ocfs2_new_path(fe_bh, &di->id2.i_list);
Mark Fashehdcd05382007-01-16 11:32:23 -08006926 if (!path) {
6927 status = -ENOMEM;
6928 mlog_errno(status);
6929 goto bail;
6930 }
Mark Fasheh83418972007-04-23 18:53:12 -07006931
6932 ocfs2_extent_map_trunc(inode, new_highest_cpos);
6933
Mark Fashehccd979b2005-12-15 14:31:24 -08006934start:
Mark Fashehdcd05382007-01-16 11:32:23 -08006935 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006936 * Check that we still have allocation to delete.
6937 */
6938 if (OCFS2_I(inode)->ip_clusters == 0) {
6939 status = 0;
6940 goto bail;
6941 }
6942
6943 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08006944 * Truncate always works against the rightmost tree branch.
6945 */
6946 status = ocfs2_find_path(inode, path, UINT_MAX);
6947 if (status) {
6948 mlog_errno(status);
6949 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006950 }
6951
Mark Fashehdcd05382007-01-16 11:32:23 -08006952 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
6953 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
6954
6955 /*
6956 * By now, el will point to the extent list on the bottom most
6957 * portion of this tree. Only the tail record is considered in
6958 * each pass.
6959 *
6960 * We handle the following cases, in order:
6961 * - empty extent: delete the remaining branch
6962 * - remove the entire record
6963 * - remove a partial record
6964 * - no record needs to be removed (truncate has completed)
6965 */
6966 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006967 if (le16_to_cpu(el->l_next_free_rec) == 0) {
6968 ocfs2_error(inode->i_sb,
6969 "Inode %llu has empty extent block at %llu\n",
6970 (unsigned long long)OCFS2_I(inode)->ip_blkno,
6971 (unsigned long long)path_leaf_bh(path)->b_blocknr);
6972 status = -EROFS;
6973 goto bail;
6974 }
6975
Mark Fashehccd979b2005-12-15 14:31:24 -08006976 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08006977 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08006978 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08006979 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
6980 clusters_to_del = 0;
6981 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08006982 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08006983 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08006984 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08006985 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08006986 new_highest_cpos;
6987 } else {
6988 status = 0;
6989 goto bail;
6990 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006991
Mark Fashehdcd05382007-01-16 11:32:23 -08006992 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
6993 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
6994
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006995 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006996 tl_sem = 1;
6997 /* ocfs2_truncate_log_needs_flush guarantees us at least one
6998 * record is free for use. If there isn't any, we flush to get
6999 * an empty truncate log. */
7000 if (ocfs2_truncate_log_needs_flush(osb)) {
7001 status = __ocfs2_flush_truncate_log(osb);
7002 if (status < 0) {
7003 mlog_errno(status);
7004 goto bail;
7005 }
7006 }
7007
7008 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007009 (struct ocfs2_dinode *)fe_bh->b_data,
7010 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007011 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007012 if (IS_ERR(handle)) {
7013 status = PTR_ERR(handle);
7014 handle = NULL;
7015 mlog_errno(status);
7016 goto bail;
7017 }
7018
Mark Fashehdcd05382007-01-16 11:32:23 -08007019 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7020 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007021 if (status < 0) {
7022 mlog_errno(status);
7023 goto bail;
7024 }
7025
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007026 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007027 tl_sem = 0;
7028
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007029 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007030 handle = NULL;
7031
Mark Fashehdcd05382007-01-16 11:32:23 -08007032 ocfs2_reinit_path(path, 1);
7033
7034 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007035 * The check above will catch the case where we've truncated
7036 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007037 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007038 goto start;
7039
Mark Fashehccd979b2005-12-15 14:31:24 -08007040bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007041
7042 ocfs2_schedule_truncate_log_flush(osb, 1);
7043
7044 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007045 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007046
7047 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007048 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007049
Mark Fasheh59a5e412007-06-22 15:52:36 -07007050 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7051
Mark Fashehdcd05382007-01-16 11:32:23 -08007052 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007053
7054 /* This will drop the ext_alloc cluster lock for us */
7055 ocfs2_free_truncate_context(tc);
7056
7057 mlog_exit(status);
7058 return status;
7059}
7060
Mark Fashehccd979b2005-12-15 14:31:24 -08007061/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007062 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007063 */
7064int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7065 struct inode *inode,
7066 struct buffer_head *fe_bh,
7067 struct ocfs2_truncate_context **tc)
7068{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007069 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007070 unsigned int new_i_clusters;
7071 struct ocfs2_dinode *fe;
7072 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007073 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007074
7075 mlog_entry_void();
7076
7077 *tc = NULL;
7078
7079 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7080 i_size_read(inode));
7081 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7082
7083 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007084 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7085 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007086
Robert P. J. Daycd861282006-12-13 00:34:52 -08007087 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007088 if (!(*tc)) {
7089 status = -ENOMEM;
7090 mlog_errno(status);
7091 goto bail;
7092 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007093 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007094
Mark Fashehccd979b2005-12-15 14:31:24 -08007095 if (fe->id2.i_list.l_tree_depth) {
Mark Fashehccd979b2005-12-15 14:31:24 -08007096 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
7097 &last_eb_bh, OCFS2_BH_CACHED, inode);
7098 if (status < 0) {
7099 mlog_errno(status);
7100 goto bail;
7101 }
7102 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7103 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
7104 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
7105
7106 brelse(last_eb_bh);
7107 status = -EIO;
7108 goto bail;
7109 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007110 }
7111
7112 (*tc)->tc_last_eb_bh = last_eb_bh;
7113
Mark Fashehccd979b2005-12-15 14:31:24 -08007114 status = 0;
7115bail:
7116 if (status < 0) {
7117 if (*tc)
7118 ocfs2_free_truncate_context(*tc);
7119 *tc = NULL;
7120 }
7121 mlog_exit_void();
7122 return status;
7123}
7124
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007125/*
7126 * 'start' is inclusive, 'end' is not.
7127 */
7128int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7129 unsigned int start, unsigned int end, int trunc)
7130{
7131 int ret;
7132 unsigned int numbytes;
7133 handle_t *handle;
7134 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7135 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7136 struct ocfs2_inline_data *idata = &di->id2.i_data;
7137
7138 if (end > i_size_read(inode))
7139 end = i_size_read(inode);
7140
7141 BUG_ON(start >= end);
7142
7143 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7144 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7145 !ocfs2_supports_inline_data(osb)) {
7146 ocfs2_error(inode->i_sb,
7147 "Inline data flags for inode %llu don't agree! "
7148 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7149 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7150 le16_to_cpu(di->i_dyn_features),
7151 OCFS2_I(inode)->ip_dyn_features,
7152 osb->s_feature_incompat);
7153 ret = -EROFS;
7154 goto out;
7155 }
7156
7157 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7158 if (IS_ERR(handle)) {
7159 ret = PTR_ERR(handle);
7160 mlog_errno(ret);
7161 goto out;
7162 }
7163
7164 ret = ocfs2_journal_access(handle, inode, di_bh,
7165 OCFS2_JOURNAL_ACCESS_WRITE);
7166 if (ret) {
7167 mlog_errno(ret);
7168 goto out_commit;
7169 }
7170
7171 numbytes = end - start;
7172 memset(idata->id_data + start, 0, numbytes);
7173
7174 /*
7175 * No need to worry about the data page here - it's been
7176 * truncated already and inline data doesn't need it for
7177 * pushing zero's to disk, so we'll let readpage pick it up
7178 * later.
7179 */
7180 if (trunc) {
7181 i_size_write(inode, start);
7182 di->i_size = cpu_to_le64(start);
7183 }
7184
7185 inode->i_blocks = ocfs2_inode_sector_count(inode);
7186 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7187
7188 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7189 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7190
7191 ocfs2_journal_dirty(handle, di_bh);
7192
7193out_commit:
7194 ocfs2_commit_trans(osb, handle);
7195
7196out:
7197 return ret;
7198}
7199
Mark Fashehccd979b2005-12-15 14:31:24 -08007200static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7201{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007202 /*
7203 * The caller is responsible for completing deallocation
7204 * before freeing the context.
7205 */
7206 if (tc->tc_dealloc.c_first_suballocator != NULL)
7207 mlog(ML_NOTICE,
7208 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007209
7210 if (tc->tc_last_eb_bh)
7211 brelse(tc->tc_last_eb_bh);
7212
7213 kfree(tc);
7214}