blob: 6b27f74bb346baa6bb665f10eb09082728f78cd9 [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>
Jan Karaa90714c2008-10-09 19:38:40 +020031#include <linux/quotaops.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080032
33#define MLOG_MASK_PREFIX ML_DISK_ALLOC
34#include <cluster/masklog.h>
35
36#include "ocfs2.h"
37
38#include "alloc.h"
Mark Fasheh60b11392007-02-16 11:46:50 -080039#include "aops.h"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070040#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080041#include "dlmglue.h"
42#include "extent_map.h"
43#include "inode.h"
44#include "journal.h"
45#include "localalloc.h"
46#include "suballoc.h"
47#include "sysfile.h"
48#include "file.h"
49#include "super.h"
50#include "uptodate.h"
51
52#include "buffer_head_io.h"
53
Tao Mae7d4cb62008-08-18 17:38:44 +080054
Joel Becker1625f8a2008-08-21 17:11:10 -070055/*
56 * Operations for a specific extent tree type.
57 *
58 * To implement an on-disk btree (extent tree) type in ocfs2, add
59 * an ocfs2_extent_tree_operations structure and the matching
Joel Becker8d6220d2008-08-22 12:46:09 -070060 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
Joel Becker1625f8a2008-08-21 17:11:10 -070061 * for the allocation portion of the extent tree.
62 */
Tao Mae7d4cb62008-08-18 17:38:44 +080063struct ocfs2_extent_tree_operations {
Joel Becker1625f8a2008-08-21 17:11:10 -070064 /*
65 * last_eb_blk is the block number of the right most leaf extent
66 * block. Most on-disk structures containing an extent tree store
67 * this value for fast access. The ->eo_set_last_eb_blk() and
68 * ->eo_get_last_eb_blk() operations access this value. They are
69 * both required.
70 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070071 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
72 u64 blkno);
73 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -070074
75 /*
76 * The on-disk structure usually keeps track of how many total
77 * clusters are stored in this extent tree. This function updates
78 * that value. new_clusters is the delta, and must be
79 * added to the total. Required.
80 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070081 void (*eo_update_clusters)(struct inode *inode,
82 struct ocfs2_extent_tree *et,
83 u32 new_clusters);
Joel Becker1625f8a2008-08-21 17:11:10 -070084
85 /*
86 * If ->eo_insert_check() exists, it is called before rec is
87 * inserted into the extent tree. It is optional.
88 */
Joel Becker1e61ee72008-08-20 18:32:45 -070089 int (*eo_insert_check)(struct inode *inode,
90 struct ocfs2_extent_tree *et,
91 struct ocfs2_extent_rec *rec);
Joel Becker35dc0aa2008-08-20 16:25:06 -070092 int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
Joel Becker0ce10102008-08-20 17:19:50 -070093
Joel Becker1625f8a2008-08-21 17:11:10 -070094 /*
95 * --------------------------------------------------------------
96 * The remaining are internal to ocfs2_extent_tree and don't have
97 * accessor functions
98 */
99
100 /*
101 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
102 * It is required.
103 */
Joel Becker0ce10102008-08-20 17:19:50 -0700104 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -0700105
106 /*
107 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
108 * it exists. If it does not, et->et_max_leaf_clusters is set
109 * to 0 (unlimited). Optional.
110 */
Joel Becker943cced2008-08-20 17:31:10 -0700111 void (*eo_fill_max_leaf_clusters)(struct inode *inode,
112 struct ocfs2_extent_tree *et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800113};
114
Joel Beckerf99b9b72008-08-20 19:36:33 -0700115
116/*
117 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
118 * in the methods.
119 */
120static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
121static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
122 u64 blkno);
123static void ocfs2_dinode_update_clusters(struct inode *inode,
124 struct ocfs2_extent_tree *et,
125 u32 clusters);
126static int ocfs2_dinode_insert_check(struct inode *inode,
127 struct ocfs2_extent_tree *et,
128 struct ocfs2_extent_rec *rec);
129static int ocfs2_dinode_sanity_check(struct inode *inode,
130 struct ocfs2_extent_tree *et);
131static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
132static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
133 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
134 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
135 .eo_update_clusters = ocfs2_dinode_update_clusters,
136 .eo_insert_check = ocfs2_dinode_insert_check,
137 .eo_sanity_check = ocfs2_dinode_sanity_check,
138 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
Tao Mae7d4cb62008-08-18 17:38:44 +0800139};
140
141static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
142 u64 blkno)
143{
Joel Beckerea5efa12008-08-20 16:57:27 -0700144 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800145
Joel Beckerf99b9b72008-08-20 19:36:33 -0700146 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800147 di->i_last_eb_blk = cpu_to_le64(blkno);
148}
149
150static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
151{
Joel Beckerea5efa12008-08-20 16:57:27 -0700152 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800153
Joel Beckerf99b9b72008-08-20 19:36:33 -0700154 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800155 return le64_to_cpu(di->i_last_eb_blk);
156}
157
158static void ocfs2_dinode_update_clusters(struct inode *inode,
159 struct ocfs2_extent_tree *et,
160 u32 clusters)
161{
Joel Beckerea5efa12008-08-20 16:57:27 -0700162 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800163
164 le32_add_cpu(&di->i_clusters, clusters);
165 spin_lock(&OCFS2_I(inode)->ip_lock);
166 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
167 spin_unlock(&OCFS2_I(inode)->ip_lock);
168}
169
Joel Becker1e61ee72008-08-20 18:32:45 -0700170static int ocfs2_dinode_insert_check(struct inode *inode,
171 struct ocfs2_extent_tree *et,
172 struct ocfs2_extent_rec *rec)
173{
174 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
175
176 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
177 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
178 (OCFS2_I(inode)->ip_clusters != rec->e_cpos),
179 "Device %s, asking for sparse allocation: inode %llu, "
180 "cpos %u, clusters %u\n",
181 osb->dev_str,
182 (unsigned long long)OCFS2_I(inode)->ip_blkno,
183 rec->e_cpos,
184 OCFS2_I(inode)->ip_clusters);
185
186 return 0;
187}
188
Tao Mae7d4cb62008-08-18 17:38:44 +0800189static int ocfs2_dinode_sanity_check(struct inode *inode,
190 struct ocfs2_extent_tree *et)
191{
Joel Becker10995aa2008-11-13 14:49:12 -0800192 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800193
Joel Beckerf99b9b72008-08-20 19:36:33 -0700194 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Joel Becker10995aa2008-11-13 14:49:12 -0800195 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
Tao Mae7d4cb62008-08-18 17:38:44 +0800196
Joel Becker10995aa2008-11-13 14:49:12 -0800197 return 0;
Tao Mae7d4cb62008-08-18 17:38:44 +0800198}
199
Joel Beckerf99b9b72008-08-20 19:36:33 -0700200static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
201{
202 struct ocfs2_dinode *di = et->et_object;
203
204 et->et_root_el = &di->id2.i_list;
205}
206
Tao Mae7d4cb62008-08-18 17:38:44 +0800207
Joel Becker0ce10102008-08-20 17:19:50 -0700208static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
209{
210 struct ocfs2_xattr_value_root *xv = et->et_object;
211
212 et->et_root_el = &xv->xr_list;
213}
214
Tao Maf56654c2008-08-18 17:38:48 +0800215static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
216 u64 blkno)
217{
218 struct ocfs2_xattr_value_root *xv =
Joel Beckerea5efa12008-08-20 16:57:27 -0700219 (struct ocfs2_xattr_value_root *)et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800220
221 xv->xr_last_eb_blk = cpu_to_le64(blkno);
222}
223
224static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
225{
226 struct ocfs2_xattr_value_root *xv =
Joel Beckerea5efa12008-08-20 16:57:27 -0700227 (struct ocfs2_xattr_value_root *) et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800228
229 return le64_to_cpu(xv->xr_last_eb_blk);
230}
231
232static void ocfs2_xattr_value_update_clusters(struct inode *inode,
233 struct ocfs2_extent_tree *et,
234 u32 clusters)
235{
236 struct ocfs2_xattr_value_root *xv =
Joel Beckerea5efa12008-08-20 16:57:27 -0700237 (struct ocfs2_xattr_value_root *)et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800238
239 le32_add_cpu(&xv->xr_clusters, clusters);
240}
241
Joel Becker1a09f552008-08-20 17:44:24 -0700242static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700243 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
244 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
245 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700246 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800247};
248
Joel Becker0ce10102008-08-20 17:19:50 -0700249static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
250{
251 struct ocfs2_xattr_block *xb = et->et_object;
252
253 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
254}
255
Joel Becker943cced2008-08-20 17:31:10 -0700256static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode *inode,
257 struct ocfs2_extent_tree *et)
258{
259 et->et_max_leaf_clusters =
260 ocfs2_clusters_for_bytes(inode->i_sb,
261 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
262}
263
Tao Maba492612008-08-18 17:38:49 +0800264static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
265 u64 blkno)
266{
Joel Beckerea5efa12008-08-20 16:57:27 -0700267 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800268 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
269
270 xt->xt_last_eb_blk = cpu_to_le64(blkno);
271}
272
273static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
274{
Joel Beckerea5efa12008-08-20 16:57:27 -0700275 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800276 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
277
278 return le64_to_cpu(xt->xt_last_eb_blk);
279}
280
281static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
282 struct ocfs2_extent_tree *et,
283 u32 clusters)
284{
Joel Beckerea5efa12008-08-20 16:57:27 -0700285 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800286
287 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
288}
289
Tao Maba492612008-08-18 17:38:49 +0800290static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700291 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
292 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
293 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700294 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
Joel Becker943cced2008-08-20 17:31:10 -0700295 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
Tao Maba492612008-08-18 17:38:49 +0800296};
297
Joel Becker8d6220d2008-08-22 12:46:09 -0700298static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
299 struct inode *inode,
300 struct buffer_head *bh,
301 void *obj,
302 struct ocfs2_extent_tree_operations *ops)
Tao Mae7d4cb62008-08-18 17:38:44 +0800303{
Joel Becker1a09f552008-08-20 17:44:24 -0700304 et->et_ops = ops;
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700305 et->et_root_bh = bh;
Joel Beckerea5efa12008-08-20 16:57:27 -0700306 if (!obj)
307 obj = (void *)bh->b_data;
308 et->et_object = obj;
Tao Mae7d4cb62008-08-18 17:38:44 +0800309
Joel Becker0ce10102008-08-20 17:19:50 -0700310 et->et_ops->eo_fill_root_el(et);
Joel Becker943cced2008-08-20 17:31:10 -0700311 if (!et->et_ops->eo_fill_max_leaf_clusters)
312 et->et_max_leaf_clusters = 0;
313 else
314 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800315}
316
Joel Becker8d6220d2008-08-22 12:46:09 -0700317void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
318 struct inode *inode,
319 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700320{
Joel Becker8d6220d2008-08-22 12:46:09 -0700321 __ocfs2_init_extent_tree(et, inode, bh, NULL, &ocfs2_dinode_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700322}
323
Joel Becker8d6220d2008-08-22 12:46:09 -0700324void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700325 struct inode *inode,
Joel Becker8d6220d2008-08-22 12:46:09 -0700326 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700327{
Joel Becker8d6220d2008-08-22 12:46:09 -0700328 __ocfs2_init_extent_tree(et, inode, bh, NULL,
329 &ocfs2_xattr_tree_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700330}
331
Joel Becker8d6220d2008-08-22 12:46:09 -0700332void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
333 struct inode *inode,
334 struct buffer_head *bh,
335 struct ocfs2_xattr_value_root *xv)
Tao Mae7d4cb62008-08-18 17:38:44 +0800336{
Joel Becker8d6220d2008-08-22 12:46:09 -0700337 __ocfs2_init_extent_tree(et, inode, bh, xv,
338 &ocfs2_xattr_value_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800339}
340
Joel Becker35dc0aa2008-08-20 16:25:06 -0700341static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
342 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800343{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700344 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800345}
346
Joel Becker35dc0aa2008-08-20 16:25:06 -0700347static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800348{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700349 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800350}
351
Joel Becker35dc0aa2008-08-20 16:25:06 -0700352static inline void ocfs2_et_update_clusters(struct inode *inode,
353 struct ocfs2_extent_tree *et,
354 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800355{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700356 et->et_ops->eo_update_clusters(inode, et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700357}
358
Joel Becker1e61ee72008-08-20 18:32:45 -0700359static inline int ocfs2_et_insert_check(struct inode *inode,
360 struct ocfs2_extent_tree *et,
361 struct ocfs2_extent_rec *rec)
362{
363 int ret = 0;
364
365 if (et->et_ops->eo_insert_check)
366 ret = et->et_ops->eo_insert_check(inode, et, rec);
367 return ret;
368}
369
Joel Becker35dc0aa2008-08-20 16:25:06 -0700370static inline int ocfs2_et_sanity_check(struct inode *inode,
371 struct ocfs2_extent_tree *et)
372{
Joel Becker1e61ee72008-08-20 18:32:45 -0700373 int ret = 0;
374
375 if (et->et_ops->eo_sanity_check)
376 ret = et->et_ops->eo_sanity_check(inode, et);
377 return ret;
Tao Mae7d4cb62008-08-18 17:38:44 +0800378}
379
Mark Fashehccd979b2005-12-15 14:31:24 -0800380static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
Mark Fasheh59a5e412007-06-22 15:52:36 -0700381static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
382 struct ocfs2_extent_block *eb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800383
Mark Fashehdcd05382007-01-16 11:32:23 -0800384/*
385 * Structures which describe a path through a btree, and functions to
386 * manipulate them.
387 *
388 * The idea here is to be as generic as possible with the tree
389 * manipulation code.
390 */
391struct ocfs2_path_item {
392 struct buffer_head *bh;
393 struct ocfs2_extent_list *el;
394};
395
396#define OCFS2_MAX_PATH_DEPTH 5
397
398struct ocfs2_path {
399 int p_tree_depth;
400 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
401};
402
403#define path_root_bh(_path) ((_path)->p_node[0].bh)
404#define path_root_el(_path) ((_path)->p_node[0].el)
405#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
406#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
407#define path_num_items(_path) ((_path)->p_tree_depth + 1)
408
409/*
410 * Reset the actual path elements so that we can re-use the structure
411 * to build another path. Generally, this involves freeing the buffer
412 * heads.
413 */
414static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
415{
416 int i, start = 0, depth = 0;
417 struct ocfs2_path_item *node;
418
419 if (keep_root)
420 start = 1;
421
422 for(i = start; i < path_num_items(path); i++) {
423 node = &path->p_node[i];
424
425 brelse(node->bh);
426 node->bh = NULL;
427 node->el = NULL;
428 }
429
430 /*
431 * Tree depth may change during truncate, or insert. If we're
432 * keeping the root extent list, then make sure that our path
433 * structure reflects the proper depth.
434 */
435 if (keep_root)
436 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
437
438 path->p_tree_depth = depth;
439}
440
441static void ocfs2_free_path(struct ocfs2_path *path)
442{
443 if (path) {
444 ocfs2_reinit_path(path, 0);
445 kfree(path);
446 }
447}
448
449/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700450 * All the elements of src into dest. After this call, src could be freed
451 * without affecting dest.
452 *
453 * Both paths should have the same root. Any non-root elements of dest
454 * will be freed.
455 */
456static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
457{
458 int i;
459
460 BUG_ON(path_root_bh(dest) != path_root_bh(src));
461 BUG_ON(path_root_el(dest) != path_root_el(src));
462
463 ocfs2_reinit_path(dest, 1);
464
465 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
466 dest->p_node[i].bh = src->p_node[i].bh;
467 dest->p_node[i].el = src->p_node[i].el;
468
469 if (dest->p_node[i].bh)
470 get_bh(dest->p_node[i].bh);
471 }
472}
473
474/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800475 * Make the *dest path the same as src and re-initialize src path to
476 * have a root only.
477 */
478static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
479{
480 int i;
481
482 BUG_ON(path_root_bh(dest) != path_root_bh(src));
483
484 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
485 brelse(dest->p_node[i].bh);
486
487 dest->p_node[i].bh = src->p_node[i].bh;
488 dest->p_node[i].el = src->p_node[i].el;
489
490 src->p_node[i].bh = NULL;
491 src->p_node[i].el = NULL;
492 }
493}
494
495/*
496 * Insert an extent block at given index.
497 *
498 * This will not take an additional reference on eb_bh.
499 */
500static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
501 struct buffer_head *eb_bh)
502{
503 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
504
505 /*
506 * Right now, no root bh is an extent block, so this helps
507 * catch code errors with dinode trees. The assertion can be
508 * safely removed if we ever need to insert extent block
509 * structures at the root.
510 */
511 BUG_ON(index == 0);
512
513 path->p_node[index].bh = eb_bh;
514 path->p_node[index].el = &eb->h_list;
515}
516
517static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
518 struct ocfs2_extent_list *root_el)
519{
520 struct ocfs2_path *path;
521
522 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
523
524 path = kzalloc(sizeof(*path), GFP_NOFS);
525 if (path) {
526 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
527 get_bh(root_bh);
528 path_root_bh(path) = root_bh;
529 path_root_el(path) = root_el;
530 }
531
532 return path;
533}
534
535/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800536 * Convenience function to journal all components in a path.
537 */
538static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
539 struct ocfs2_path *path)
540{
541 int i, ret = 0;
542
543 if (!path)
544 goto out;
545
546 for(i = 0; i < path_num_items(path); i++) {
547 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
548 OCFS2_JOURNAL_ACCESS_WRITE);
549 if (ret < 0) {
550 mlog_errno(ret);
551 goto out;
552 }
553 }
554
555out:
556 return ret;
557}
558
Mark Fasheh328d5752007-06-18 10:48:04 -0700559/*
560 * Return the index of the extent record which contains cluster #v_cluster.
561 * -1 is returned if it was not found.
562 *
563 * Should work fine on interior and exterior nodes.
564 */
565int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
566{
567 int ret = -1;
568 int i;
569 struct ocfs2_extent_rec *rec;
570 u32 rec_end, rec_start, clusters;
571
572 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
573 rec = &el->l_recs[i];
574
575 rec_start = le32_to_cpu(rec->e_cpos);
576 clusters = ocfs2_rec_clusters(el, rec);
577
578 rec_end = rec_start + clusters;
579
580 if (v_cluster >= rec_start && v_cluster < rec_end) {
581 ret = i;
582 break;
583 }
584 }
585
586 return ret;
587}
588
Mark Fashehdcd05382007-01-16 11:32:23 -0800589enum ocfs2_contig_type {
590 CONTIG_NONE = 0,
591 CONTIG_LEFT,
Mark Fasheh328d5752007-06-18 10:48:04 -0700592 CONTIG_RIGHT,
593 CONTIG_LEFTRIGHT,
Mark Fashehdcd05382007-01-16 11:32:23 -0800594};
595
Mark Fashehe48edee2007-03-07 16:46:57 -0800596
597/*
598 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
599 * ocfs2_extent_contig only work properly against leaf nodes!
600 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800601static int ocfs2_block_extent_contig(struct super_block *sb,
602 struct ocfs2_extent_rec *ext,
603 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800604{
Mark Fashehe48edee2007-03-07 16:46:57 -0800605 u64 blk_end = le64_to_cpu(ext->e_blkno);
606
607 blk_end += ocfs2_clusters_to_blocks(sb,
608 le16_to_cpu(ext->e_leaf_clusters));
609
610 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800611}
612
Mark Fashehdcd05382007-01-16 11:32:23 -0800613static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
614 struct ocfs2_extent_rec *right)
615{
Mark Fashehe48edee2007-03-07 16:46:57 -0800616 u32 left_range;
617
618 left_range = le32_to_cpu(left->e_cpos) +
619 le16_to_cpu(left->e_leaf_clusters);
620
621 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800622}
623
624static enum ocfs2_contig_type
625 ocfs2_extent_contig(struct inode *inode,
626 struct ocfs2_extent_rec *ext,
627 struct ocfs2_extent_rec *insert_rec)
628{
629 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
630
Mark Fasheh328d5752007-06-18 10:48:04 -0700631 /*
632 * Refuse to coalesce extent records with different flag
633 * fields - we don't want to mix unwritten extents with user
634 * data.
635 */
636 if (ext->e_flags != insert_rec->e_flags)
637 return CONTIG_NONE;
638
Mark Fashehdcd05382007-01-16 11:32:23 -0800639 if (ocfs2_extents_adjacent(ext, insert_rec) &&
640 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
641 return CONTIG_RIGHT;
642
643 blkno = le64_to_cpu(ext->e_blkno);
644 if (ocfs2_extents_adjacent(insert_rec, ext) &&
645 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
646 return CONTIG_LEFT;
647
648 return CONTIG_NONE;
649}
650
651/*
652 * NOTE: We can have pretty much any combination of contiguousness and
653 * appending.
654 *
655 * The usefulness of APPEND_TAIL is more in that it lets us know that
656 * we'll have to update the path to that leaf.
657 */
658enum ocfs2_append_type {
659 APPEND_NONE = 0,
660 APPEND_TAIL,
661};
662
Mark Fasheh328d5752007-06-18 10:48:04 -0700663enum ocfs2_split_type {
664 SPLIT_NONE = 0,
665 SPLIT_LEFT,
666 SPLIT_RIGHT,
667};
668
Mark Fashehdcd05382007-01-16 11:32:23 -0800669struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700670 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800671 enum ocfs2_append_type ins_appending;
672 enum ocfs2_contig_type ins_contig;
673 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800674 int ins_tree_depth;
675};
676
Mark Fasheh328d5752007-06-18 10:48:04 -0700677struct ocfs2_merge_ctxt {
678 enum ocfs2_contig_type c_contig_type;
679 int c_has_empty_extent;
680 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700681};
682
Joel Becker5e965812008-11-13 14:49:16 -0800683static int ocfs2_validate_extent_block(struct super_block *sb,
684 struct buffer_head *bh)
685{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700686 int rc;
Joel Becker5e965812008-11-13 14:49:16 -0800687 struct ocfs2_extent_block *eb =
688 (struct ocfs2_extent_block *)bh->b_data;
689
Joel Becker970e4932008-11-13 14:49:19 -0800690 mlog(0, "Validating extent block %llu\n",
691 (unsigned long long)bh->b_blocknr);
692
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700693 BUG_ON(!buffer_uptodate(bh));
694
695 /*
696 * If the ecc fails, we return the error but otherwise
697 * leave the filesystem running. We know any error is
698 * local to this block.
699 */
700 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
701 if (rc)
702 return rc;
703
704 /*
705 * Errors after here are fatal.
706 */
707
Joel Becker5e965812008-11-13 14:49:16 -0800708 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
709 ocfs2_error(sb,
710 "Extent block #%llu has bad signature %.*s",
711 (unsigned long long)bh->b_blocknr, 7,
712 eb->h_signature);
713 return -EINVAL;
714 }
715
716 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
717 ocfs2_error(sb,
718 "Extent block #%llu has an invalid h_blkno "
719 "of %llu",
720 (unsigned long long)bh->b_blocknr,
721 (unsigned long long)le64_to_cpu(eb->h_blkno));
722 return -EINVAL;
723 }
724
725 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
726 ocfs2_error(sb,
727 "Extent block #%llu has an invalid "
728 "h_fs_generation of #%u",
729 (unsigned long long)bh->b_blocknr,
730 le32_to_cpu(eb->h_fs_generation));
731 return -EINVAL;
732 }
733
734 return 0;
735}
736
737int ocfs2_read_extent_block(struct inode *inode, u64 eb_blkno,
738 struct buffer_head **bh)
739{
740 int rc;
741 struct buffer_head *tmp = *bh;
742
Joel Becker970e4932008-11-13 14:49:19 -0800743 rc = ocfs2_read_block(inode, eb_blkno, &tmp,
744 ocfs2_validate_extent_block);
Joel Becker5e965812008-11-13 14:49:16 -0800745
746 /* If ocfs2_read_block() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -0800747 if (!rc && !*bh)
Joel Becker5e965812008-11-13 14:49:16 -0800748 *bh = tmp;
749
Joel Becker5e965812008-11-13 14:49:16 -0800750 return rc;
751}
752
753
Mark Fashehccd979b2005-12-15 14:31:24 -0800754/*
755 * How many free extents have we got before we need more meta data?
756 */
757int ocfs2_num_free_extents(struct ocfs2_super *osb,
758 struct inode *inode,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700759 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -0800760{
761 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800762 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800763 struct ocfs2_extent_block *eb;
764 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800765 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800766
767 mlog_entry_void();
768
Joel Beckerf99b9b72008-08-20 19:36:33 -0700769 el = et->et_root_el;
770 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800771
Tao Mae7d4cb62008-08-18 17:38:44 +0800772 if (last_eb_blk) {
Joel Becker5e965812008-11-13 14:49:16 -0800773 retval = ocfs2_read_extent_block(inode, last_eb_blk, &eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800774 if (retval < 0) {
775 mlog_errno(retval);
776 goto bail;
777 }
778 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
779 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800780 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800781
782 BUG_ON(el->l_tree_depth != 0);
783
784 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
785bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700786 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800787
788 mlog_exit(retval);
789 return retval;
790}
791
792/* expects array to already be allocated
793 *
794 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
795 * l_count for you
796 */
797static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700798 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800799 struct inode *inode,
800 int wanted,
801 struct ocfs2_alloc_context *meta_ac,
802 struct buffer_head *bhs[])
803{
804 int count, status, i;
805 u16 suballoc_bit_start;
806 u32 num_got;
807 u64 first_blkno;
808 struct ocfs2_extent_block *eb;
809
810 mlog_entry_void();
811
812 count = 0;
813 while (count < wanted) {
814 status = ocfs2_claim_metadata(osb,
815 handle,
816 meta_ac,
817 wanted - count,
818 &suballoc_bit_start,
819 &num_got,
820 &first_blkno);
821 if (status < 0) {
822 mlog_errno(status);
823 goto bail;
824 }
825
826 for(i = count; i < (num_got + count); i++) {
827 bhs[i] = sb_getblk(osb->sb, first_blkno);
828 if (bhs[i] == NULL) {
829 status = -EIO;
830 mlog_errno(status);
831 goto bail;
832 }
833 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
834
835 status = ocfs2_journal_access(handle, inode, bhs[i],
836 OCFS2_JOURNAL_ACCESS_CREATE);
837 if (status < 0) {
838 mlog_errno(status);
839 goto bail;
840 }
841
842 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
843 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
844 /* Ok, setup the minimal stuff here. */
845 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
846 eb->h_blkno = cpu_to_le64(first_blkno);
847 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800848 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800849 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
850 eb->h_list.l_count =
851 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
852
853 suballoc_bit_start++;
854 first_blkno++;
855
856 /* We'll also be dirtied by the caller, so
857 * this isn't absolutely necessary. */
858 status = ocfs2_journal_dirty(handle, bhs[i]);
859 if (status < 0) {
860 mlog_errno(status);
861 goto bail;
862 }
863 }
864
865 count += num_got;
866 }
867
868 status = 0;
869bail:
870 if (status < 0) {
871 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -0700872 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800873 bhs[i] = NULL;
874 }
875 }
876 mlog_exit(status);
877 return status;
878}
879
880/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800881 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
882 *
883 * Returns the sum of the rightmost extent rec logical offset and
884 * cluster count.
885 *
886 * ocfs2_add_branch() uses this to determine what logical cluster
887 * value should be populated into the leftmost new branch records.
888 *
889 * ocfs2_shift_tree_depth() uses this to determine the # clusters
890 * value for the new topmost tree record.
891 */
892static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
893{
894 int i;
895
896 i = le16_to_cpu(el->l_next_free_rec) - 1;
897
898 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -0800899 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -0800900}
901
902/*
Mark Fashehccd979b2005-12-15 14:31:24 -0800903 * Add an entire tree branch to our inode. eb_bh is the extent block
904 * to start at, if we don't want to start the branch at the dinode
905 * structure.
906 *
907 * last_eb_bh is required as we have to update it's next_leaf pointer
908 * for the new last extent block.
909 *
910 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -0800911 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -0800912 */
913static int ocfs2_add_branch(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700914 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800915 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +0800916 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -0800917 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -0700918 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800919 struct ocfs2_alloc_context *meta_ac)
920{
921 int status, new_blocks, i;
922 u64 next_blkno, new_last_eb_blk;
923 struct buffer_head *bh;
924 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800925 struct ocfs2_extent_block *eb;
926 struct ocfs2_extent_list *eb_el;
927 struct ocfs2_extent_list *el;
Mark Fashehdcd05382007-01-16 11:32:23 -0800928 u32 new_cpos;
Mark Fashehccd979b2005-12-15 14:31:24 -0800929
930 mlog_entry_void();
931
Mark Fasheh328d5752007-06-18 10:48:04 -0700932 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800933
Mark Fashehccd979b2005-12-15 14:31:24 -0800934 if (eb_bh) {
935 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
936 el = &eb->h_list;
937 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700938 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -0800939
940 /* we never add a branch to a leaf. */
941 BUG_ON(!el->l_tree_depth);
942
943 new_blocks = le16_to_cpu(el->l_tree_depth);
944
945 /* allocate the number of new eb blocks we need */
946 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
947 GFP_KERNEL);
948 if (!new_eb_bhs) {
949 status = -ENOMEM;
950 mlog_errno(status);
951 goto bail;
952 }
953
954 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
955 meta_ac, new_eb_bhs);
956 if (status < 0) {
957 mlog_errno(status);
958 goto bail;
959 }
960
Mark Fasheh328d5752007-06-18 10:48:04 -0700961 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
Mark Fashehdcd05382007-01-16 11:32:23 -0800962 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
963
Mark Fashehccd979b2005-12-15 14:31:24 -0800964 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
965 * linked with the rest of the tree.
966 * conversly, new_eb_bhs[0] is the new bottommost leaf.
967 *
968 * when we leave the loop, new_last_eb_blk will point to the
969 * newest leaf, and next_blkno will point to the topmost extent
970 * block. */
971 next_blkno = new_last_eb_blk = 0;
972 for(i = 0; i < new_blocks; i++) {
973 bh = new_eb_bhs[i];
974 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -0800975 /* ocfs2_create_new_meta_bhs() should create it right! */
976 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -0800977 eb_el = &eb->h_list;
978
979 status = ocfs2_journal_access(handle, inode, bh,
980 OCFS2_JOURNAL_ACCESS_CREATE);
981 if (status < 0) {
982 mlog_errno(status);
983 goto bail;
984 }
985
986 eb->h_next_leaf_blk = 0;
987 eb_el->l_tree_depth = cpu_to_le16(i);
988 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -0800989 /*
990 * This actually counts as an empty extent as
991 * c_clusters == 0
992 */
993 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -0800994 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -0800995 /*
996 * eb_el isn't always an interior node, but even leaf
997 * nodes want a zero'd flags and reserved field so
998 * this gets the whole 32 bits regardless of use.
999 */
1000 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001001 if (!eb_el->l_tree_depth)
1002 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1003
1004 status = ocfs2_journal_dirty(handle, bh);
1005 if (status < 0) {
1006 mlog_errno(status);
1007 goto bail;
1008 }
1009
1010 next_blkno = le64_to_cpu(eb->h_blkno);
1011 }
1012
1013 /* This is a bit hairy. We want to update up to three blocks
1014 * here without leaving any of them in an inconsistent state
1015 * in case of error. We don't have to worry about
1016 * journal_dirty erroring as it won't unless we've aborted the
1017 * handle (in which case we would never be here) so reserving
1018 * the write with journal_access is all we need to do. */
Mark Fasheh328d5752007-06-18 10:48:04 -07001019 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001020 OCFS2_JOURNAL_ACCESS_WRITE);
1021 if (status < 0) {
1022 mlog_errno(status);
1023 goto bail;
1024 }
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001025 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001026 OCFS2_JOURNAL_ACCESS_WRITE);
1027 if (status < 0) {
1028 mlog_errno(status);
1029 goto bail;
1030 }
1031 if (eb_bh) {
1032 status = ocfs2_journal_access(handle, inode, eb_bh,
1033 OCFS2_JOURNAL_ACCESS_WRITE);
1034 if (status < 0) {
1035 mlog_errno(status);
1036 goto bail;
1037 }
1038 }
1039
1040 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001041 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001042 i = le16_to_cpu(el->l_next_free_rec);
1043 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001044 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001045 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001046 le16_add_cpu(&el->l_next_free_rec, 1);
1047
1048 /* fe needs a new last extent block pointer, as does the
1049 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001050 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001051
Mark Fasheh328d5752007-06-18 10:48:04 -07001052 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001053 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1054
Mark Fasheh328d5752007-06-18 10:48:04 -07001055 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001056 if (status < 0)
1057 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001058 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001059 if (status < 0)
1060 mlog_errno(status);
1061 if (eb_bh) {
1062 status = ocfs2_journal_dirty(handle, eb_bh);
1063 if (status < 0)
1064 mlog_errno(status);
1065 }
1066
Mark Fasheh328d5752007-06-18 10:48:04 -07001067 /*
1068 * Some callers want to track the rightmost leaf so pass it
1069 * back here.
1070 */
1071 brelse(*last_eb_bh);
1072 get_bh(new_eb_bhs[0]);
1073 *last_eb_bh = new_eb_bhs[0];
1074
Mark Fashehccd979b2005-12-15 14:31:24 -08001075 status = 0;
1076bail:
1077 if (new_eb_bhs) {
1078 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001079 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001080 kfree(new_eb_bhs);
1081 }
1082
1083 mlog_exit(status);
1084 return status;
1085}
1086
1087/*
1088 * adds another level to the allocation tree.
1089 * returns back the new extent block so you can add a branch to it
1090 * after this call.
1091 */
1092static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001093 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001094 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001095 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001096 struct ocfs2_alloc_context *meta_ac,
1097 struct buffer_head **ret_new_eb_bh)
1098{
1099 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001100 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001101 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001102 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001103 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001104 struct ocfs2_extent_list *eb_el;
1105
1106 mlog_entry_void();
1107
1108 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
1109 &new_eb_bh);
1110 if (status < 0) {
1111 mlog_errno(status);
1112 goto bail;
1113 }
1114
1115 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001116 /* ocfs2_create_new_meta_bhs() should create it right! */
1117 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001118
1119 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001120 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001121
1122 status = ocfs2_journal_access(handle, inode, new_eb_bh,
1123 OCFS2_JOURNAL_ACCESS_CREATE);
1124 if (status < 0) {
1125 mlog_errno(status);
1126 goto bail;
1127 }
1128
Tao Mae7d4cb62008-08-18 17:38:44 +08001129 /* copy the root extent list data into the new extent block */
1130 eb_el->l_tree_depth = root_el->l_tree_depth;
1131 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1132 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1133 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001134
1135 status = ocfs2_journal_dirty(handle, new_eb_bh);
1136 if (status < 0) {
1137 mlog_errno(status);
1138 goto bail;
1139 }
1140
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001141 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001142 OCFS2_JOURNAL_ACCESS_WRITE);
1143 if (status < 0) {
1144 mlog_errno(status);
1145 goto bail;
1146 }
1147
Mark Fashehdcd05382007-01-16 11:32:23 -08001148 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1149
Tao Mae7d4cb62008-08-18 17:38:44 +08001150 /* update root_bh now */
1151 le16_add_cpu(&root_el->l_tree_depth, 1);
1152 root_el->l_recs[0].e_cpos = 0;
1153 root_el->l_recs[0].e_blkno = eb->h_blkno;
1154 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1155 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1156 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1157 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001158
1159 /* If this is our 1st tree depth shift, then last_eb_blk
1160 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001161 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001162 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001163
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001164 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001165 if (status < 0) {
1166 mlog_errno(status);
1167 goto bail;
1168 }
1169
1170 *ret_new_eb_bh = new_eb_bh;
1171 new_eb_bh = NULL;
1172 status = 0;
1173bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001174 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001175
1176 mlog_exit(status);
1177 return status;
1178}
1179
1180/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001181 * Should only be called when there is no space left in any of the
1182 * leaf nodes. What we want to do is find the lowest tree depth
1183 * non-leaf extent block with room for new records. There are three
1184 * valid results of this search:
1185 *
1186 * 1) a lowest extent block is found, then we pass it back in
1187 * *lowest_eb_bh and return '0'
1188 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001189 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001190 * pass NULL back in *lowest_eb_bh, but still return '0'
1191 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001192 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001193 * which case we return > 0
1194 *
1195 * return status < 0 indicates an error.
1196 */
1197static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1198 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001199 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001200 struct buffer_head **target_bh)
1201{
1202 int status = 0, i;
1203 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001204 struct ocfs2_extent_block *eb;
1205 struct ocfs2_extent_list *el;
1206 struct buffer_head *bh = NULL;
1207 struct buffer_head *lowest_bh = NULL;
1208
1209 mlog_entry_void();
1210
1211 *target_bh = NULL;
1212
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001213 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001214
1215 while(le16_to_cpu(el->l_tree_depth) > 1) {
1216 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Mark Fashehb0697052006-03-03 10:24:33 -08001217 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001218 "extent list (next_free_rec == 0)",
Mark Fashehb0697052006-03-03 10:24:33 -08001219 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001220 status = -EIO;
1221 goto bail;
1222 }
1223 i = le16_to_cpu(el->l_next_free_rec) - 1;
1224 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1225 if (!blkno) {
Mark Fashehb0697052006-03-03 10:24:33 -08001226 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001227 "list where extent # %d has no physical "
1228 "block start",
Mark Fashehb0697052006-03-03 10:24:33 -08001229 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001230 status = -EIO;
1231 goto bail;
1232 }
1233
Mark Fasheha81cb882008-10-07 14:25:16 -07001234 brelse(bh);
1235 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001236
Joel Becker5e965812008-11-13 14:49:16 -08001237 status = ocfs2_read_extent_block(inode, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001238 if (status < 0) {
1239 mlog_errno(status);
1240 goto bail;
1241 }
1242
1243 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001244 el = &eb->h_list;
1245
1246 if (le16_to_cpu(el->l_next_free_rec) <
1247 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001248 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001249 lowest_bh = bh;
1250 get_bh(lowest_bh);
1251 }
1252 }
1253
1254 /* If we didn't find one and the fe doesn't have any room,
1255 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001256 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001257 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001258 status = 1;
1259
1260 *target_bh = lowest_bh;
1261bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001262 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001263
1264 mlog_exit(status);
1265 return status;
1266}
1267
Mark Fashehe48edee2007-03-07 16:46:57 -08001268/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001269 * Grow a b-tree so that it has more records.
1270 *
1271 * We might shift the tree depth in which case existing paths should
1272 * be considered invalid.
1273 *
1274 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001275 *
1276 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001277 */
1278static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001279 struct ocfs2_extent_tree *et, int *final_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07001280 struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001281 struct ocfs2_alloc_context *meta_ac)
1282{
1283 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001284 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001285 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001286 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1287 struct buffer_head *bh = NULL;
1288
1289 BUG_ON(meta_ac == NULL);
1290
Tao Mae7d4cb62008-08-18 17:38:44 +08001291 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001292 if (shift < 0) {
1293 ret = shift;
1294 mlog_errno(ret);
1295 goto out;
1296 }
1297
1298 /* We traveled all the way to the bottom of the allocation tree
1299 * and didn't find room for any more extents - we need to add
1300 * another tree level */
1301 if (shift) {
1302 BUG_ON(bh);
1303 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1304
1305 /* ocfs2_shift_tree_depth will return us a buffer with
1306 * the new extent block (so we can pass that to
1307 * ocfs2_add_branch). */
Tao Mae7d4cb62008-08-18 17:38:44 +08001308 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001309 meta_ac, &bh);
1310 if (ret < 0) {
1311 mlog_errno(ret);
1312 goto out;
1313 }
1314 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001315 if (depth == 1) {
1316 /*
1317 * Special case: we have room now if we shifted from
1318 * tree_depth 0, so no more work needs to be done.
1319 *
1320 * We won't be calling add_branch, so pass
1321 * back *last_eb_bh as the new leaf. At depth
1322 * zero, it should always be null so there's
1323 * no reason to brelse.
1324 */
1325 BUG_ON(*last_eb_bh);
1326 get_bh(bh);
1327 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001328 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001329 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001330 }
1331
1332 /* call ocfs2_add_branch to add the final part of the tree with
1333 * the new data. */
1334 mlog(0, "add branch. bh = %p\n", bh);
Tao Mae7d4cb62008-08-18 17:38:44 +08001335 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001336 meta_ac);
1337 if (ret < 0) {
1338 mlog_errno(ret);
1339 goto out;
1340 }
1341
1342out:
1343 if (final_depth)
1344 *final_depth = depth;
1345 brelse(bh);
1346 return ret;
1347}
1348
1349/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001350 * This function will discard the rightmost extent record.
1351 */
1352static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1353{
1354 int next_free = le16_to_cpu(el->l_next_free_rec);
1355 int count = le16_to_cpu(el->l_count);
1356 unsigned int num_bytes;
1357
1358 BUG_ON(!next_free);
1359 /* This will cause us to go off the end of our extent list. */
1360 BUG_ON(next_free >= count);
1361
1362 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1363
1364 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1365}
1366
1367static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1368 struct ocfs2_extent_rec *insert_rec)
1369{
1370 int i, insert_index, next_free, has_empty, num_bytes;
1371 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1372 struct ocfs2_extent_rec *rec;
1373
1374 next_free = le16_to_cpu(el->l_next_free_rec);
1375 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1376
1377 BUG_ON(!next_free);
1378
1379 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001380 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001381
1382 /*
1383 * The easiest way to approach this is to just remove the
1384 * empty extent and temporarily decrement next_free.
1385 */
1386 if (has_empty) {
1387 /*
1388 * If next_free was 1 (only an empty extent), this
1389 * loop won't execute, which is fine. We still want
1390 * the decrement above to happen.
1391 */
1392 for(i = 0; i < (next_free - 1); i++)
1393 el->l_recs[i] = el->l_recs[i+1];
1394
1395 next_free--;
1396 }
1397
1398 /*
1399 * Figure out what the new record index should be.
1400 */
1401 for(i = 0; i < next_free; i++) {
1402 rec = &el->l_recs[i];
1403
1404 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1405 break;
1406 }
1407 insert_index = i;
1408
1409 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1410 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1411
1412 BUG_ON(insert_index < 0);
1413 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1414 BUG_ON(insert_index > next_free);
1415
1416 /*
1417 * No need to memmove if we're just adding to the tail.
1418 */
1419 if (insert_index != next_free) {
1420 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1421
1422 num_bytes = next_free - insert_index;
1423 num_bytes *= sizeof(struct ocfs2_extent_rec);
1424 memmove(&el->l_recs[insert_index + 1],
1425 &el->l_recs[insert_index],
1426 num_bytes);
1427 }
1428
1429 /*
1430 * Either we had an empty extent, and need to re-increment or
1431 * there was no empty extent on a non full rightmost leaf node,
1432 * in which case we still need to increment.
1433 */
1434 next_free++;
1435 el->l_next_free_rec = cpu_to_le16(next_free);
1436 /*
1437 * Make sure none of the math above just messed up our tree.
1438 */
1439 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1440
1441 el->l_recs[insert_index] = *insert_rec;
1442
1443}
1444
Mark Fasheh328d5752007-06-18 10:48:04 -07001445static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1446{
1447 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1448
1449 BUG_ON(num_recs == 0);
1450
1451 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1452 num_recs--;
1453 size = num_recs * sizeof(struct ocfs2_extent_rec);
1454 memmove(&el->l_recs[0], &el->l_recs[1], size);
1455 memset(&el->l_recs[num_recs], 0,
1456 sizeof(struct ocfs2_extent_rec));
1457 el->l_next_free_rec = cpu_to_le16(num_recs);
1458 }
1459}
1460
Mark Fashehdcd05382007-01-16 11:32:23 -08001461/*
1462 * Create an empty extent record .
1463 *
1464 * l_next_free_rec may be updated.
1465 *
1466 * If an empty extent already exists do nothing.
1467 */
1468static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1469{
1470 int next_free = le16_to_cpu(el->l_next_free_rec);
1471
Mark Fashehe48edee2007-03-07 16:46:57 -08001472 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1473
Mark Fashehdcd05382007-01-16 11:32:23 -08001474 if (next_free == 0)
1475 goto set_and_inc;
1476
1477 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1478 return;
1479
1480 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1481 "Asked to create an empty extent in a full list:\n"
1482 "count = %u, tree depth = %u",
1483 le16_to_cpu(el->l_count),
1484 le16_to_cpu(el->l_tree_depth));
1485
1486 ocfs2_shift_records_right(el);
1487
1488set_and_inc:
1489 le16_add_cpu(&el->l_next_free_rec, 1);
1490 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1491}
1492
1493/*
1494 * For a rotation which involves two leaf nodes, the "root node" is
1495 * the lowest level tree node which contains a path to both leafs. This
1496 * resulting set of information can be used to form a complete "subtree"
1497 *
1498 * This function is passed two full paths from the dinode down to a
1499 * pair of adjacent leaves. It's task is to figure out which path
1500 * index contains the subtree root - this can be the root index itself
1501 * in a worst-case rotation.
1502 *
1503 * The array index of the subtree root is passed back.
1504 */
1505static int ocfs2_find_subtree_root(struct inode *inode,
1506 struct ocfs2_path *left,
1507 struct ocfs2_path *right)
1508{
1509 int i = 0;
1510
1511 /*
1512 * Check that the caller passed in two paths from the same tree.
1513 */
1514 BUG_ON(path_root_bh(left) != path_root_bh(right));
1515
1516 do {
1517 i++;
1518
1519 /*
1520 * The caller didn't pass two adjacent paths.
1521 */
1522 mlog_bug_on_msg(i > left->p_tree_depth,
1523 "Inode %lu, left depth %u, right depth %u\n"
1524 "left leaf blk %llu, right leaf blk %llu\n",
1525 inode->i_ino, left->p_tree_depth,
1526 right->p_tree_depth,
1527 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1528 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1529 } while (left->p_node[i].bh->b_blocknr ==
1530 right->p_node[i].bh->b_blocknr);
1531
1532 return i - 1;
1533}
1534
1535typedef void (path_insert_t)(void *, struct buffer_head *);
1536
1537/*
1538 * Traverse a btree path in search of cpos, starting at root_el.
1539 *
1540 * This code can be called with a cpos larger than the tree, in which
1541 * case it will return the rightmost path.
1542 */
1543static int __ocfs2_find_path(struct inode *inode,
1544 struct ocfs2_extent_list *root_el, u32 cpos,
1545 path_insert_t *func, void *data)
1546{
1547 int i, ret = 0;
1548 u32 range;
1549 u64 blkno;
1550 struct buffer_head *bh = NULL;
1551 struct ocfs2_extent_block *eb;
1552 struct ocfs2_extent_list *el;
1553 struct ocfs2_extent_rec *rec;
1554 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1555
1556 el = root_el;
1557 while (el->l_tree_depth) {
1558 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1559 ocfs2_error(inode->i_sb,
1560 "Inode %llu has empty extent list at "
1561 "depth %u\n",
1562 (unsigned long long)oi->ip_blkno,
1563 le16_to_cpu(el->l_tree_depth));
1564 ret = -EROFS;
1565 goto out;
1566
1567 }
1568
1569 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1570 rec = &el->l_recs[i];
1571
1572 /*
1573 * In the case that cpos is off the allocation
1574 * tree, this should just wind up returning the
1575 * rightmost record.
1576 */
1577 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001578 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001579 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1580 break;
1581 }
1582
1583 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1584 if (blkno == 0) {
1585 ocfs2_error(inode->i_sb,
1586 "Inode %llu has bad blkno in extent list "
1587 "at depth %u (index %d)\n",
1588 (unsigned long long)oi->ip_blkno,
1589 le16_to_cpu(el->l_tree_depth), i);
1590 ret = -EROFS;
1591 goto out;
1592 }
1593
1594 brelse(bh);
1595 bh = NULL;
Joel Becker5e965812008-11-13 14:49:16 -08001596 ret = ocfs2_read_extent_block(inode, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001597 if (ret) {
1598 mlog_errno(ret);
1599 goto out;
1600 }
1601
1602 eb = (struct ocfs2_extent_block *) bh->b_data;
1603 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001604
1605 if (le16_to_cpu(el->l_next_free_rec) >
1606 le16_to_cpu(el->l_count)) {
1607 ocfs2_error(inode->i_sb,
1608 "Inode %llu has bad count in extent list "
1609 "at block %llu (next free=%u, count=%u)\n",
1610 (unsigned long long)oi->ip_blkno,
1611 (unsigned long long)bh->b_blocknr,
1612 le16_to_cpu(el->l_next_free_rec),
1613 le16_to_cpu(el->l_count));
1614 ret = -EROFS;
1615 goto out;
1616 }
1617
1618 if (func)
1619 func(data, bh);
1620 }
1621
1622out:
1623 /*
1624 * Catch any trailing bh that the loop didn't handle.
1625 */
1626 brelse(bh);
1627
1628 return ret;
1629}
1630
1631/*
1632 * Given an initialized path (that is, it has a valid root extent
1633 * list), this function will traverse the btree in search of the path
1634 * which would contain cpos.
1635 *
1636 * The path traveled is recorded in the path structure.
1637 *
1638 * Note that this will not do any comparisons on leaf node extent
1639 * records, so it will work fine in the case that we just added a tree
1640 * branch.
1641 */
1642struct find_path_data {
1643 int index;
1644 struct ocfs2_path *path;
1645};
1646static void find_path_ins(void *data, struct buffer_head *bh)
1647{
1648 struct find_path_data *fp = data;
1649
1650 get_bh(bh);
1651 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1652 fp->index++;
1653}
1654static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1655 u32 cpos)
1656{
1657 struct find_path_data data;
1658
1659 data.index = 1;
1660 data.path = path;
1661 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1662 find_path_ins, &data);
1663}
1664
1665static void find_leaf_ins(void *data, struct buffer_head *bh)
1666{
1667 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1668 struct ocfs2_extent_list *el = &eb->h_list;
1669 struct buffer_head **ret = data;
1670
1671 /* We want to retain only the leaf block. */
1672 if (le16_to_cpu(el->l_tree_depth) == 0) {
1673 get_bh(bh);
1674 *ret = bh;
1675 }
1676}
1677/*
1678 * Find the leaf block in the tree which would contain cpos. No
1679 * checking of the actual leaf is done.
1680 *
1681 * Some paths want to call this instead of allocating a path structure
1682 * and calling ocfs2_find_path().
1683 *
1684 * This function doesn't handle non btree extent lists.
1685 */
Mark Fasheh363041a2007-01-17 12:31:35 -08001686int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1687 u32 cpos, struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001688{
1689 int ret;
1690 struct buffer_head *bh = NULL;
1691
1692 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1693 if (ret) {
1694 mlog_errno(ret);
1695 goto out;
1696 }
1697
1698 *leaf_bh = bh;
1699out:
1700 return ret;
1701}
1702
1703/*
1704 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1705 *
1706 * Basically, we've moved stuff around at the bottom of the tree and
1707 * we need to fix up the extent records above the changes to reflect
1708 * the new changes.
1709 *
1710 * left_rec: the record on the left.
1711 * left_child_el: is the child list pointed to by left_rec
1712 * right_rec: the record to the right of left_rec
1713 * right_child_el: is the child list pointed to by right_rec
1714 *
1715 * By definition, this only works on interior nodes.
1716 */
1717static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1718 struct ocfs2_extent_list *left_child_el,
1719 struct ocfs2_extent_rec *right_rec,
1720 struct ocfs2_extent_list *right_child_el)
1721{
1722 u32 left_clusters, right_end;
1723
1724 /*
1725 * Interior nodes never have holes. Their cpos is the cpos of
1726 * the leftmost record in their child list. Their cluster
1727 * count covers the full theoretical range of their child list
1728 * - the range between their cpos and the cpos of the record
1729 * immediately to their right.
1730 */
1731 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07001732 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1733 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1734 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1735 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001736 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001737 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001738
1739 /*
1740 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001741 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001742 * updating e_cpos to keep the same highest cluster count.
1743 */
1744 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001745 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001746
1747 right_rec->e_cpos = left_rec->e_cpos;
1748 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1749
1750 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001751 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001752}
1753
1754/*
1755 * Adjust the adjacent root node records involved in a
1756 * rotation. left_el_blkno is passed in as a key so that we can easily
1757 * find it's index in the root list.
1758 */
1759static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1760 struct ocfs2_extent_list *left_el,
1761 struct ocfs2_extent_list *right_el,
1762 u64 left_el_blkno)
1763{
1764 int i;
1765
1766 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1767 le16_to_cpu(left_el->l_tree_depth));
1768
1769 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1770 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1771 break;
1772 }
1773
1774 /*
1775 * The path walking code should have never returned a root and
1776 * two paths which are not adjacent.
1777 */
1778 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1779
1780 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1781 &root_el->l_recs[i + 1], right_el);
1782}
1783
1784/*
1785 * We've changed a leaf block (in right_path) and need to reflect that
1786 * change back up the subtree.
1787 *
1788 * This happens in multiple places:
1789 * - When we've moved an extent record from the left path leaf to the right
1790 * path leaf to make room for an empty extent in the left path leaf.
1791 * - When our insert into the right path leaf is at the leftmost edge
1792 * and requires an update of the path immediately to it's left. This
1793 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08001794 * - When we've adjusted the last extent record in the left path leaf and the
1795 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08001796 */
1797static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1798 struct ocfs2_path *left_path,
1799 struct ocfs2_path *right_path,
1800 int subtree_index)
1801{
1802 int ret, i, idx;
1803 struct ocfs2_extent_list *el, *left_el, *right_el;
1804 struct ocfs2_extent_rec *left_rec, *right_rec;
1805 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1806
1807 /*
1808 * Update the counts and position values within all the
1809 * interior nodes to reflect the leaf rotation we just did.
1810 *
1811 * The root node is handled below the loop.
1812 *
1813 * We begin the loop with right_el and left_el pointing to the
1814 * leaf lists and work our way up.
1815 *
1816 * NOTE: within this loop, left_el and right_el always refer
1817 * to the *child* lists.
1818 */
1819 left_el = path_leaf_el(left_path);
1820 right_el = path_leaf_el(right_path);
1821 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1822 mlog(0, "Adjust records at index %u\n", i);
1823
1824 /*
1825 * One nice property of knowing that all of these
1826 * nodes are below the root is that we only deal with
1827 * the leftmost right node record and the rightmost
1828 * left node record.
1829 */
1830 el = left_path->p_node[i].el;
1831 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1832 left_rec = &el->l_recs[idx];
1833
1834 el = right_path->p_node[i].el;
1835 right_rec = &el->l_recs[0];
1836
1837 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1838 right_el);
1839
1840 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1841 if (ret)
1842 mlog_errno(ret);
1843
1844 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1845 if (ret)
1846 mlog_errno(ret);
1847
1848 /*
1849 * Setup our list pointers now so that the current
1850 * parents become children in the next iteration.
1851 */
1852 left_el = left_path->p_node[i].el;
1853 right_el = right_path->p_node[i].el;
1854 }
1855
1856 /*
1857 * At the root node, adjust the two adjacent records which
1858 * begin our path to the leaves.
1859 */
1860
1861 el = left_path->p_node[subtree_index].el;
1862 left_el = left_path->p_node[subtree_index + 1].el;
1863 right_el = right_path->p_node[subtree_index + 1].el;
1864
1865 ocfs2_adjust_root_records(el, left_el, right_el,
1866 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1867
1868 root_bh = left_path->p_node[subtree_index].bh;
1869
1870 ret = ocfs2_journal_dirty(handle, root_bh);
1871 if (ret)
1872 mlog_errno(ret);
1873}
1874
1875static int ocfs2_rotate_subtree_right(struct inode *inode,
1876 handle_t *handle,
1877 struct ocfs2_path *left_path,
1878 struct ocfs2_path *right_path,
1879 int subtree_index)
1880{
1881 int ret, i;
1882 struct buffer_head *right_leaf_bh;
1883 struct buffer_head *left_leaf_bh = NULL;
1884 struct buffer_head *root_bh;
1885 struct ocfs2_extent_list *right_el, *left_el;
1886 struct ocfs2_extent_rec move_rec;
1887
1888 left_leaf_bh = path_leaf_bh(left_path);
1889 left_el = path_leaf_el(left_path);
1890
1891 if (left_el->l_next_free_rec != left_el->l_count) {
1892 ocfs2_error(inode->i_sb,
1893 "Inode %llu has non-full interior leaf node %llu"
1894 "(next free = %u)",
1895 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1896 (unsigned long long)left_leaf_bh->b_blocknr,
1897 le16_to_cpu(left_el->l_next_free_rec));
1898 return -EROFS;
1899 }
1900
1901 /*
1902 * This extent block may already have an empty record, so we
1903 * return early if so.
1904 */
1905 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1906 return 0;
1907
1908 root_bh = left_path->p_node[subtree_index].bh;
1909 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1910
1911 ret = ocfs2_journal_access(handle, inode, root_bh,
1912 OCFS2_JOURNAL_ACCESS_WRITE);
1913 if (ret) {
1914 mlog_errno(ret);
1915 goto out;
1916 }
1917
1918 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1919 ret = ocfs2_journal_access(handle, inode,
1920 right_path->p_node[i].bh,
1921 OCFS2_JOURNAL_ACCESS_WRITE);
1922 if (ret) {
1923 mlog_errno(ret);
1924 goto out;
1925 }
1926
1927 ret = ocfs2_journal_access(handle, inode,
1928 left_path->p_node[i].bh,
1929 OCFS2_JOURNAL_ACCESS_WRITE);
1930 if (ret) {
1931 mlog_errno(ret);
1932 goto out;
1933 }
1934 }
1935
1936 right_leaf_bh = path_leaf_bh(right_path);
1937 right_el = path_leaf_el(right_path);
1938
1939 /* This is a code error, not a disk corruption. */
1940 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1941 "because rightmost leaf block %llu is empty\n",
1942 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1943 (unsigned long long)right_leaf_bh->b_blocknr);
1944
1945 ocfs2_create_empty_extent(right_el);
1946
1947 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1948 if (ret) {
1949 mlog_errno(ret);
1950 goto out;
1951 }
1952
1953 /* Do the copy now. */
1954 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1955 move_rec = left_el->l_recs[i];
1956 right_el->l_recs[0] = move_rec;
1957
1958 /*
1959 * Clear out the record we just copied and shift everything
1960 * over, leaving an empty extent in the left leaf.
1961 *
1962 * We temporarily subtract from next_free_rec so that the
1963 * shift will lose the tail record (which is now defunct).
1964 */
1965 le16_add_cpu(&left_el->l_next_free_rec, -1);
1966 ocfs2_shift_records_right(left_el);
1967 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1968 le16_add_cpu(&left_el->l_next_free_rec, 1);
1969
1970 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1971 if (ret) {
1972 mlog_errno(ret);
1973 goto out;
1974 }
1975
1976 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1977 subtree_index);
1978
1979out:
1980 return ret;
1981}
1982
1983/*
1984 * Given a full path, determine what cpos value would return us a path
1985 * containing the leaf immediately to the left of the current one.
1986 *
1987 * Will return zero if the path passed in is already the leftmost path.
1988 */
1989static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1990 struct ocfs2_path *path, u32 *cpos)
1991{
1992 int i, j, ret = 0;
1993 u64 blkno;
1994 struct ocfs2_extent_list *el;
1995
Mark Fashehe48edee2007-03-07 16:46:57 -08001996 BUG_ON(path->p_tree_depth == 0);
1997
Mark Fashehdcd05382007-01-16 11:32:23 -08001998 *cpos = 0;
1999
2000 blkno = path_leaf_bh(path)->b_blocknr;
2001
2002 /* Start at the tree node just above the leaf and work our way up. */
2003 i = path->p_tree_depth - 1;
2004 while (i >= 0) {
2005 el = path->p_node[i].el;
2006
2007 /*
2008 * Find the extent record just before the one in our
2009 * path.
2010 */
2011 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2012 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2013 if (j == 0) {
2014 if (i == 0) {
2015 /*
2016 * We've determined that the
2017 * path specified is already
2018 * the leftmost one - return a
2019 * cpos of zero.
2020 */
2021 goto out;
2022 }
2023 /*
2024 * The leftmost record points to our
2025 * leaf - we need to travel up the
2026 * tree one level.
2027 */
2028 goto next_node;
2029 }
2030
2031 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002032 *cpos = *cpos + ocfs2_rec_clusters(el,
2033 &el->l_recs[j - 1]);
2034 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002035 goto out;
2036 }
2037 }
2038
2039 /*
2040 * If we got here, we never found a valid node where
2041 * the tree indicated one should be.
2042 */
2043 ocfs2_error(sb,
2044 "Invalid extent tree at extent block %llu\n",
2045 (unsigned long long)blkno);
2046 ret = -EROFS;
2047 goto out;
2048
2049next_node:
2050 blkno = path->p_node[i].bh->b_blocknr;
2051 i--;
2052 }
2053
2054out:
2055 return ret;
2056}
2057
Mark Fasheh328d5752007-06-18 10:48:04 -07002058/*
2059 * Extend the transaction by enough credits to complete the rotation,
2060 * and still leave at least the original number of credits allocated
2061 * to this transaction.
2062 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002063static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002064 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002065 struct ocfs2_path *path)
2066{
Mark Fasheh328d5752007-06-18 10:48:04 -07002067 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002068
2069 if (handle->h_buffer_credits < credits)
2070 return ocfs2_extend_trans(handle, credits);
2071
2072 return 0;
2073}
2074
2075/*
2076 * Trap the case where we're inserting into the theoretical range past
2077 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2078 * whose cpos is less than ours into the right leaf.
2079 *
2080 * It's only necessary to look at the rightmost record of the left
2081 * leaf because the logic that calls us should ensure that the
2082 * theoretical ranges in the path components above the leaves are
2083 * correct.
2084 */
2085static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2086 u32 insert_cpos)
2087{
2088 struct ocfs2_extent_list *left_el;
2089 struct ocfs2_extent_rec *rec;
2090 int next_free;
2091
2092 left_el = path_leaf_el(left_path);
2093 next_free = le16_to_cpu(left_el->l_next_free_rec);
2094 rec = &left_el->l_recs[next_free - 1];
2095
2096 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2097 return 1;
2098 return 0;
2099}
2100
Mark Fasheh328d5752007-06-18 10:48:04 -07002101static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2102{
2103 int next_free = le16_to_cpu(el->l_next_free_rec);
2104 unsigned int range;
2105 struct ocfs2_extent_rec *rec;
2106
2107 if (next_free == 0)
2108 return 0;
2109
2110 rec = &el->l_recs[0];
2111 if (ocfs2_is_empty_extent(rec)) {
2112 /* Empty list. */
2113 if (next_free == 1)
2114 return 0;
2115 rec = &el->l_recs[1];
2116 }
2117
2118 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2119 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2120 return 1;
2121 return 0;
2122}
2123
Mark Fashehdcd05382007-01-16 11:32:23 -08002124/*
2125 * Rotate all the records in a btree right one record, starting at insert_cpos.
2126 *
2127 * The path to the rightmost leaf should be passed in.
2128 *
2129 * The array is assumed to be large enough to hold an entire path (tree depth).
2130 *
2131 * Upon succesful return from this function:
2132 *
2133 * - The 'right_path' array will contain a path to the leaf block
2134 * whose range contains e_cpos.
2135 * - That leaf block will have a single empty extent in list index 0.
2136 * - In the case that the rotation requires a post-insert update,
2137 * *ret_left_path will contain a valid path which can be passed to
2138 * ocfs2_insert_path().
2139 */
2140static int ocfs2_rotate_tree_right(struct inode *inode,
2141 handle_t *handle,
Mark Fasheh328d5752007-06-18 10:48:04 -07002142 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002143 u32 insert_cpos,
2144 struct ocfs2_path *right_path,
2145 struct ocfs2_path **ret_left_path)
2146{
Mark Fasheh328d5752007-06-18 10:48:04 -07002147 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002148 u32 cpos;
2149 struct ocfs2_path *left_path = NULL;
2150
2151 *ret_left_path = NULL;
2152
2153 left_path = ocfs2_new_path(path_root_bh(right_path),
2154 path_root_el(right_path));
2155 if (!left_path) {
2156 ret = -ENOMEM;
2157 mlog_errno(ret);
2158 goto out;
2159 }
2160
2161 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2162 if (ret) {
2163 mlog_errno(ret);
2164 goto out;
2165 }
2166
2167 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2168
2169 /*
2170 * What we want to do here is:
2171 *
2172 * 1) Start with the rightmost path.
2173 *
2174 * 2) Determine a path to the leaf block directly to the left
2175 * of that leaf.
2176 *
2177 * 3) Determine the 'subtree root' - the lowest level tree node
2178 * which contains a path to both leaves.
2179 *
2180 * 4) Rotate the subtree.
2181 *
2182 * 5) Find the next subtree by considering the left path to be
2183 * the new right path.
2184 *
2185 * The check at the top of this while loop also accepts
2186 * insert_cpos == cpos because cpos is only a _theoretical_
2187 * value to get us the left path - insert_cpos might very well
2188 * be filling that hole.
2189 *
2190 * Stop at a cpos of '0' because we either started at the
2191 * leftmost branch (i.e., a tree with one branch and a
2192 * rotation inside of it), or we've gone as far as we can in
2193 * rotating subtrees.
2194 */
2195 while (cpos && insert_cpos <= cpos) {
2196 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2197 insert_cpos, cpos);
2198
2199 ret = ocfs2_find_path(inode, left_path, cpos);
2200 if (ret) {
2201 mlog_errno(ret);
2202 goto out;
2203 }
2204
2205 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2206 path_leaf_bh(right_path),
2207 "Inode %lu: error during insert of %u "
2208 "(left path cpos %u) results in two identical "
2209 "paths ending at %llu\n",
2210 inode->i_ino, insert_cpos, cpos,
2211 (unsigned long long)
2212 path_leaf_bh(left_path)->b_blocknr);
2213
Mark Fasheh328d5752007-06-18 10:48:04 -07002214 if (split == SPLIT_NONE &&
2215 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002216 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002217
2218 /*
2219 * We've rotated the tree as much as we
2220 * should. The rest is up to
2221 * ocfs2_insert_path() to complete, after the
2222 * record insertion. We indicate this
2223 * situation by returning the left path.
2224 *
2225 * The reason we don't adjust the records here
2226 * before the record insert is that an error
2227 * later might break the rule where a parent
2228 * record e_cpos will reflect the actual
2229 * e_cpos of the 1st nonempty record of the
2230 * child list.
2231 */
2232 *ret_left_path = left_path;
2233 goto out_ret_path;
2234 }
2235
2236 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2237
2238 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2239 start,
2240 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2241 right_path->p_tree_depth);
2242
2243 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002244 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002245 if (ret) {
2246 mlog_errno(ret);
2247 goto out;
2248 }
2249
2250 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2251 right_path, start);
2252 if (ret) {
2253 mlog_errno(ret);
2254 goto out;
2255 }
2256
Mark Fasheh328d5752007-06-18 10:48:04 -07002257 if (split != SPLIT_NONE &&
2258 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2259 insert_cpos)) {
2260 /*
2261 * A rotate moves the rightmost left leaf
2262 * record over to the leftmost right leaf
2263 * slot. If we're doing an extent split
2264 * instead of a real insert, then we have to
2265 * check that the extent to be split wasn't
2266 * just moved over. If it was, then we can
2267 * exit here, passing left_path back -
2268 * ocfs2_split_extent() is smart enough to
2269 * search both leaves.
2270 */
2271 *ret_left_path = left_path;
2272 goto out_ret_path;
2273 }
2274
Mark Fashehdcd05382007-01-16 11:32:23 -08002275 /*
2276 * There is no need to re-read the next right path
2277 * as we know that it'll be our current left
2278 * path. Optimize by copying values instead.
2279 */
2280 ocfs2_mv_path(right_path, left_path);
2281
2282 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2283 &cpos);
2284 if (ret) {
2285 mlog_errno(ret);
2286 goto out;
2287 }
2288 }
2289
2290out:
2291 ocfs2_free_path(left_path);
2292
2293out_ret_path:
2294 return ret;
2295}
2296
Mark Fasheh328d5752007-06-18 10:48:04 -07002297static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2298 struct ocfs2_path *path)
2299{
2300 int i, idx;
2301 struct ocfs2_extent_rec *rec;
2302 struct ocfs2_extent_list *el;
2303 struct ocfs2_extent_block *eb;
2304 u32 range;
2305
2306 /* Path should always be rightmost. */
2307 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2308 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2309
2310 el = &eb->h_list;
2311 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2312 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2313 rec = &el->l_recs[idx];
2314 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2315
2316 for (i = 0; i < path->p_tree_depth; i++) {
2317 el = path->p_node[i].el;
2318 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2319 rec = &el->l_recs[idx];
2320
2321 rec->e_int_clusters = cpu_to_le32(range);
2322 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2323
2324 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2325 }
2326}
2327
2328static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2329 struct ocfs2_cached_dealloc_ctxt *dealloc,
2330 struct ocfs2_path *path, int unlink_start)
2331{
2332 int ret, i;
2333 struct ocfs2_extent_block *eb;
2334 struct ocfs2_extent_list *el;
2335 struct buffer_head *bh;
2336
2337 for(i = unlink_start; i < path_num_items(path); i++) {
2338 bh = path->p_node[i].bh;
2339
2340 eb = (struct ocfs2_extent_block *)bh->b_data;
2341 /*
2342 * Not all nodes might have had their final count
2343 * decremented by the caller - handle this here.
2344 */
2345 el = &eb->h_list;
2346 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2347 mlog(ML_ERROR,
2348 "Inode %llu, attempted to remove extent block "
2349 "%llu with %u records\n",
2350 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2351 (unsigned long long)le64_to_cpu(eb->h_blkno),
2352 le16_to_cpu(el->l_next_free_rec));
2353
2354 ocfs2_journal_dirty(handle, bh);
2355 ocfs2_remove_from_cache(inode, bh);
2356 continue;
2357 }
2358
2359 el->l_next_free_rec = 0;
2360 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2361
2362 ocfs2_journal_dirty(handle, bh);
2363
2364 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2365 if (ret)
2366 mlog_errno(ret);
2367
2368 ocfs2_remove_from_cache(inode, bh);
2369 }
2370}
2371
2372static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2373 struct ocfs2_path *left_path,
2374 struct ocfs2_path *right_path,
2375 int subtree_index,
2376 struct ocfs2_cached_dealloc_ctxt *dealloc)
2377{
2378 int i;
2379 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2380 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2381 struct ocfs2_extent_list *el;
2382 struct ocfs2_extent_block *eb;
2383
2384 el = path_leaf_el(left_path);
2385
2386 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2387
2388 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2389 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2390 break;
2391
2392 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2393
2394 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2395 le16_add_cpu(&root_el->l_next_free_rec, -1);
2396
2397 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2398 eb->h_next_leaf_blk = 0;
2399
2400 ocfs2_journal_dirty(handle, root_bh);
2401 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2402
2403 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2404 subtree_index + 1);
2405}
2406
2407static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2408 struct ocfs2_path *left_path,
2409 struct ocfs2_path *right_path,
2410 int subtree_index,
2411 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002412 int *deleted,
2413 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002414{
2415 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002416 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002417 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2418 struct ocfs2_extent_block *eb;
2419
2420 *deleted = 0;
2421
2422 right_leaf_el = path_leaf_el(right_path);
2423 left_leaf_el = path_leaf_el(left_path);
2424 root_bh = left_path->p_node[subtree_index].bh;
2425 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2426
2427 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2428 return 0;
2429
2430 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2431 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2432 /*
2433 * It's legal for us to proceed if the right leaf is
2434 * the rightmost one and it has an empty extent. There
2435 * are two cases to handle - whether the leaf will be
2436 * empty after removal or not. If the leaf isn't empty
2437 * then just remove the empty extent up front. The
2438 * next block will handle empty leaves by flagging
2439 * them for unlink.
2440 *
2441 * Non rightmost leaves will throw -EAGAIN and the
2442 * caller can manually move the subtree and retry.
2443 */
2444
2445 if (eb->h_next_leaf_blk != 0ULL)
2446 return -EAGAIN;
2447
2448 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2449 ret = ocfs2_journal_access(handle, inode,
2450 path_leaf_bh(right_path),
2451 OCFS2_JOURNAL_ACCESS_WRITE);
2452 if (ret) {
2453 mlog_errno(ret);
2454 goto out;
2455 }
2456
2457 ocfs2_remove_empty_extent(right_leaf_el);
2458 } else
2459 right_has_empty = 1;
2460 }
2461
2462 if (eb->h_next_leaf_blk == 0ULL &&
2463 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2464 /*
2465 * We have to update i_last_eb_blk during the meta
2466 * data delete.
2467 */
Tao Mae7d4cb62008-08-18 17:38:44 +08002468 ret = ocfs2_journal_access(handle, inode, et_root_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07002469 OCFS2_JOURNAL_ACCESS_WRITE);
2470 if (ret) {
2471 mlog_errno(ret);
2472 goto out;
2473 }
2474
2475 del_right_subtree = 1;
2476 }
2477
2478 /*
2479 * Getting here with an empty extent in the right path implies
2480 * that it's the rightmost path and will be deleted.
2481 */
2482 BUG_ON(right_has_empty && !del_right_subtree);
2483
2484 ret = ocfs2_journal_access(handle, inode, root_bh,
2485 OCFS2_JOURNAL_ACCESS_WRITE);
2486 if (ret) {
2487 mlog_errno(ret);
2488 goto out;
2489 }
2490
2491 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2492 ret = ocfs2_journal_access(handle, inode,
2493 right_path->p_node[i].bh,
2494 OCFS2_JOURNAL_ACCESS_WRITE);
2495 if (ret) {
2496 mlog_errno(ret);
2497 goto out;
2498 }
2499
2500 ret = ocfs2_journal_access(handle, inode,
2501 left_path->p_node[i].bh,
2502 OCFS2_JOURNAL_ACCESS_WRITE);
2503 if (ret) {
2504 mlog_errno(ret);
2505 goto out;
2506 }
2507 }
2508
2509 if (!right_has_empty) {
2510 /*
2511 * Only do this if we're moving a real
2512 * record. Otherwise, the action is delayed until
2513 * after removal of the right path in which case we
2514 * can do a simple shift to remove the empty extent.
2515 */
2516 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2517 memset(&right_leaf_el->l_recs[0], 0,
2518 sizeof(struct ocfs2_extent_rec));
2519 }
2520 if (eb->h_next_leaf_blk == 0ULL) {
2521 /*
2522 * Move recs over to get rid of empty extent, decrease
2523 * next_free. This is allowed to remove the last
2524 * extent in our leaf (setting l_next_free_rec to
2525 * zero) - the delete code below won't care.
2526 */
2527 ocfs2_remove_empty_extent(right_leaf_el);
2528 }
2529
2530 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2531 if (ret)
2532 mlog_errno(ret);
2533 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2534 if (ret)
2535 mlog_errno(ret);
2536
2537 if (del_right_subtree) {
2538 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2539 subtree_index, dealloc);
2540 ocfs2_update_edge_lengths(inode, handle, left_path);
2541
2542 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002543 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002544
2545 /*
2546 * Removal of the extent in the left leaf was skipped
2547 * above so we could delete the right path
2548 * 1st.
2549 */
2550 if (right_has_empty)
2551 ocfs2_remove_empty_extent(left_leaf_el);
2552
Tao Mae7d4cb62008-08-18 17:38:44 +08002553 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002554 if (ret)
2555 mlog_errno(ret);
2556
2557 *deleted = 1;
2558 } else
2559 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2560 subtree_index);
2561
2562out:
2563 return ret;
2564}
2565
2566/*
2567 * Given a full path, determine what cpos value would return us a path
2568 * containing the leaf immediately to the right of the current one.
2569 *
2570 * Will return zero if the path passed in is already the rightmost path.
2571 *
2572 * This looks similar, but is subtly different to
2573 * ocfs2_find_cpos_for_left_leaf().
2574 */
2575static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2576 struct ocfs2_path *path, u32 *cpos)
2577{
2578 int i, j, ret = 0;
2579 u64 blkno;
2580 struct ocfs2_extent_list *el;
2581
2582 *cpos = 0;
2583
2584 if (path->p_tree_depth == 0)
2585 return 0;
2586
2587 blkno = path_leaf_bh(path)->b_blocknr;
2588
2589 /* Start at the tree node just above the leaf and work our way up. */
2590 i = path->p_tree_depth - 1;
2591 while (i >= 0) {
2592 int next_free;
2593
2594 el = path->p_node[i].el;
2595
2596 /*
2597 * Find the extent record just after the one in our
2598 * path.
2599 */
2600 next_free = le16_to_cpu(el->l_next_free_rec);
2601 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2602 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2603 if (j == (next_free - 1)) {
2604 if (i == 0) {
2605 /*
2606 * We've determined that the
2607 * path specified is already
2608 * the rightmost one - return a
2609 * cpos of zero.
2610 */
2611 goto out;
2612 }
2613 /*
2614 * The rightmost record points to our
2615 * leaf - we need to travel up the
2616 * tree one level.
2617 */
2618 goto next_node;
2619 }
2620
2621 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2622 goto out;
2623 }
2624 }
2625
2626 /*
2627 * If we got here, we never found a valid node where
2628 * the tree indicated one should be.
2629 */
2630 ocfs2_error(sb,
2631 "Invalid extent tree at extent block %llu\n",
2632 (unsigned long long)blkno);
2633 ret = -EROFS;
2634 goto out;
2635
2636next_node:
2637 blkno = path->p_node[i].bh->b_blocknr;
2638 i--;
2639 }
2640
2641out:
2642 return ret;
2643}
2644
2645static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2646 handle_t *handle,
2647 struct buffer_head *bh,
2648 struct ocfs2_extent_list *el)
2649{
2650 int ret;
2651
2652 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2653 return 0;
2654
2655 ret = ocfs2_journal_access(handle, inode, bh,
2656 OCFS2_JOURNAL_ACCESS_WRITE);
2657 if (ret) {
2658 mlog_errno(ret);
2659 goto out;
2660 }
2661
2662 ocfs2_remove_empty_extent(el);
2663
2664 ret = ocfs2_journal_dirty(handle, bh);
2665 if (ret)
2666 mlog_errno(ret);
2667
2668out:
2669 return ret;
2670}
2671
2672static int __ocfs2_rotate_tree_left(struct inode *inode,
2673 handle_t *handle, int orig_credits,
2674 struct ocfs2_path *path,
2675 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002676 struct ocfs2_path **empty_extent_path,
2677 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002678{
2679 int ret, subtree_root, deleted;
2680 u32 right_cpos;
2681 struct ocfs2_path *left_path = NULL;
2682 struct ocfs2_path *right_path = NULL;
2683
2684 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2685
2686 *empty_extent_path = NULL;
2687
2688 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2689 &right_cpos);
2690 if (ret) {
2691 mlog_errno(ret);
2692 goto out;
2693 }
2694
2695 left_path = ocfs2_new_path(path_root_bh(path),
2696 path_root_el(path));
2697 if (!left_path) {
2698 ret = -ENOMEM;
2699 mlog_errno(ret);
2700 goto out;
2701 }
2702
2703 ocfs2_cp_path(left_path, path);
2704
2705 right_path = ocfs2_new_path(path_root_bh(path),
2706 path_root_el(path));
2707 if (!right_path) {
2708 ret = -ENOMEM;
2709 mlog_errno(ret);
2710 goto out;
2711 }
2712
2713 while (right_cpos) {
2714 ret = ocfs2_find_path(inode, right_path, right_cpos);
2715 if (ret) {
2716 mlog_errno(ret);
2717 goto out;
2718 }
2719
2720 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2721 right_path);
2722
2723 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2724 subtree_root,
2725 (unsigned long long)
2726 right_path->p_node[subtree_root].bh->b_blocknr,
2727 right_path->p_tree_depth);
2728
2729 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2730 orig_credits, left_path);
2731 if (ret) {
2732 mlog_errno(ret);
2733 goto out;
2734 }
2735
Mark Fashehe8aed342007-12-03 16:43:01 -08002736 /*
2737 * Caller might still want to make changes to the
2738 * tree root, so re-add it to the journal here.
2739 */
2740 ret = ocfs2_journal_access(handle, inode,
2741 path_root_bh(left_path),
2742 OCFS2_JOURNAL_ACCESS_WRITE);
2743 if (ret) {
2744 mlog_errno(ret);
2745 goto out;
2746 }
2747
Mark Fasheh328d5752007-06-18 10:48:04 -07002748 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2749 right_path, subtree_root,
Tao Mae7d4cb62008-08-18 17:38:44 +08002750 dealloc, &deleted, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002751 if (ret == -EAGAIN) {
2752 /*
2753 * The rotation has to temporarily stop due to
2754 * the right subtree having an empty
2755 * extent. Pass it back to the caller for a
2756 * fixup.
2757 */
2758 *empty_extent_path = right_path;
2759 right_path = NULL;
2760 goto out;
2761 }
2762 if (ret) {
2763 mlog_errno(ret);
2764 goto out;
2765 }
2766
2767 /*
2768 * The subtree rotate might have removed records on
2769 * the rightmost edge. If so, then rotation is
2770 * complete.
2771 */
2772 if (deleted)
2773 break;
2774
2775 ocfs2_mv_path(left_path, right_path);
2776
2777 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2778 &right_cpos);
2779 if (ret) {
2780 mlog_errno(ret);
2781 goto out;
2782 }
2783 }
2784
2785out:
2786 ocfs2_free_path(right_path);
2787 ocfs2_free_path(left_path);
2788
2789 return ret;
2790}
2791
2792static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08002793 struct ocfs2_path *path,
2794 struct ocfs2_cached_dealloc_ctxt *dealloc,
2795 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002796{
2797 int ret, subtree_index;
2798 u32 cpos;
2799 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07002800 struct ocfs2_extent_block *eb;
2801 struct ocfs2_extent_list *el;
2802
Mark Fasheh328d5752007-06-18 10:48:04 -07002803
Joel Becker35dc0aa2008-08-20 16:25:06 -07002804 ret = ocfs2_et_sanity_check(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002805 if (ret)
2806 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07002807 /*
2808 * There's two ways we handle this depending on
2809 * whether path is the only existing one.
2810 */
2811 ret = ocfs2_extend_rotate_transaction(handle, 0,
2812 handle->h_buffer_credits,
2813 path);
2814 if (ret) {
2815 mlog_errno(ret);
2816 goto out;
2817 }
2818
2819 ret = ocfs2_journal_access_path(inode, handle, path);
2820 if (ret) {
2821 mlog_errno(ret);
2822 goto out;
2823 }
2824
2825 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2826 if (ret) {
2827 mlog_errno(ret);
2828 goto out;
2829 }
2830
2831 if (cpos) {
2832 /*
2833 * We have a path to the left of this one - it needs
2834 * an update too.
2835 */
2836 left_path = ocfs2_new_path(path_root_bh(path),
2837 path_root_el(path));
2838 if (!left_path) {
2839 ret = -ENOMEM;
2840 mlog_errno(ret);
2841 goto out;
2842 }
2843
2844 ret = ocfs2_find_path(inode, left_path, cpos);
2845 if (ret) {
2846 mlog_errno(ret);
2847 goto out;
2848 }
2849
2850 ret = ocfs2_journal_access_path(inode, handle, left_path);
2851 if (ret) {
2852 mlog_errno(ret);
2853 goto out;
2854 }
2855
2856 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2857
2858 ocfs2_unlink_subtree(inode, handle, left_path, path,
2859 subtree_index, dealloc);
2860 ocfs2_update_edge_lengths(inode, handle, left_path);
2861
2862 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002863 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002864 } else {
2865 /*
2866 * 'path' is also the leftmost path which
2867 * means it must be the only one. This gets
2868 * handled differently because we want to
2869 * revert the inode back to having extents
2870 * in-line.
2871 */
2872 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2873
Joel Beckerce1d9ea2008-08-20 16:30:07 -07002874 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07002875 el->l_tree_depth = 0;
2876 el->l_next_free_rec = 0;
2877 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2878
Joel Becker35dc0aa2008-08-20 16:25:06 -07002879 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07002880 }
2881
2882 ocfs2_journal_dirty(handle, path_root_bh(path));
2883
2884out:
2885 ocfs2_free_path(left_path);
2886 return ret;
2887}
2888
2889/*
2890 * Left rotation of btree records.
2891 *
2892 * In many ways, this is (unsurprisingly) the opposite of right
2893 * rotation. We start at some non-rightmost path containing an empty
2894 * extent in the leaf block. The code works its way to the rightmost
2895 * path by rotating records to the left in every subtree.
2896 *
2897 * This is used by any code which reduces the number of extent records
2898 * in a leaf. After removal, an empty record should be placed in the
2899 * leftmost list position.
2900 *
2901 * This won't handle a length update of the rightmost path records if
2902 * the rightmost tree leaf record is removed so the caller is
2903 * responsible for detecting and correcting that.
2904 */
2905static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2906 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002907 struct ocfs2_cached_dealloc_ctxt *dealloc,
2908 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002909{
2910 int ret, orig_credits = handle->h_buffer_credits;
2911 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2912 struct ocfs2_extent_block *eb;
2913 struct ocfs2_extent_list *el;
2914
2915 el = path_leaf_el(path);
2916 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2917 return 0;
2918
2919 if (path->p_tree_depth == 0) {
2920rightmost_no_delete:
2921 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08002922 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07002923 * it up front.
2924 */
2925 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2926 path_leaf_bh(path),
2927 path_leaf_el(path));
2928 if (ret)
2929 mlog_errno(ret);
2930 goto out;
2931 }
2932
2933 /*
2934 * Handle rightmost branch now. There's several cases:
2935 * 1) simple rotation leaving records in there. That's trivial.
2936 * 2) rotation requiring a branch delete - there's no more
2937 * records left. Two cases of this:
2938 * a) There are branches to the left.
2939 * b) This is also the leftmost (the only) branch.
2940 *
2941 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2942 * 2a) we need the left branch so that we can update it with the unlink
2943 * 2b) we need to bring the inode back to inline extents.
2944 */
2945
2946 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2947 el = &eb->h_list;
2948 if (eb->h_next_leaf_blk == 0) {
2949 /*
2950 * This gets a bit tricky if we're going to delete the
2951 * rightmost path. Get the other cases out of the way
2952 * 1st.
2953 */
2954 if (le16_to_cpu(el->l_next_free_rec) > 1)
2955 goto rightmost_no_delete;
2956
2957 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2958 ret = -EIO;
2959 ocfs2_error(inode->i_sb,
2960 "Inode %llu has empty extent block at %llu",
2961 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2962 (unsigned long long)le64_to_cpu(eb->h_blkno));
2963 goto out;
2964 }
2965
2966 /*
2967 * XXX: The caller can not trust "path" any more after
2968 * this as it will have been deleted. What do we do?
2969 *
2970 * In theory the rotate-for-merge code will never get
2971 * here because it'll always ask for a rotate in a
2972 * nonempty list.
2973 */
2974
2975 ret = ocfs2_remove_rightmost_path(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002976 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002977 if (ret)
2978 mlog_errno(ret);
2979 goto out;
2980 }
2981
2982 /*
2983 * Now we can loop, remembering the path we get from -EAGAIN
2984 * and restarting from there.
2985 */
2986try_rotate:
2987 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002988 dealloc, &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002989 if (ret && ret != -EAGAIN) {
2990 mlog_errno(ret);
2991 goto out;
2992 }
2993
2994 while (ret == -EAGAIN) {
2995 tmp_path = restart_path;
2996 restart_path = NULL;
2997
2998 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2999 tmp_path, dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003000 &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003001 if (ret && ret != -EAGAIN) {
3002 mlog_errno(ret);
3003 goto out;
3004 }
3005
3006 ocfs2_free_path(tmp_path);
3007 tmp_path = NULL;
3008
3009 if (ret == 0)
3010 goto try_rotate;
3011 }
3012
3013out:
3014 ocfs2_free_path(tmp_path);
3015 ocfs2_free_path(restart_path);
3016 return ret;
3017}
3018
3019static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3020 int index)
3021{
3022 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3023 unsigned int size;
3024
3025 if (rec->e_leaf_clusters == 0) {
3026 /*
3027 * We consumed all of the merged-from record. An empty
3028 * extent cannot exist anywhere but the 1st array
3029 * position, so move things over if the merged-from
3030 * record doesn't occupy that position.
3031 *
3032 * This creates a new empty extent so the caller
3033 * should be smart enough to have removed any existing
3034 * ones.
3035 */
3036 if (index > 0) {
3037 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3038 size = index * sizeof(struct ocfs2_extent_rec);
3039 memmove(&el->l_recs[1], &el->l_recs[0], size);
3040 }
3041
3042 /*
3043 * Always memset - the caller doesn't check whether it
3044 * created an empty extent, so there could be junk in
3045 * the other fields.
3046 */
3047 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3048 }
3049}
3050
Tao Ma677b9752008-01-30 14:21:05 +08003051static int ocfs2_get_right_path(struct inode *inode,
3052 struct ocfs2_path *left_path,
3053 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003054{
3055 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003056 u32 right_cpos;
3057 struct ocfs2_path *right_path = NULL;
3058 struct ocfs2_extent_list *left_el;
3059
3060 *ret_right_path = NULL;
3061
3062 /* This function shouldn't be called for non-trees. */
3063 BUG_ON(left_path->p_tree_depth == 0);
3064
3065 left_el = path_leaf_el(left_path);
3066 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3067
3068 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3069 &right_cpos);
3070 if (ret) {
3071 mlog_errno(ret);
3072 goto out;
3073 }
3074
3075 /* This function shouldn't be called for the rightmost leaf. */
3076 BUG_ON(right_cpos == 0);
3077
3078 right_path = ocfs2_new_path(path_root_bh(left_path),
3079 path_root_el(left_path));
3080 if (!right_path) {
3081 ret = -ENOMEM;
3082 mlog_errno(ret);
3083 goto out;
3084 }
3085
3086 ret = ocfs2_find_path(inode, right_path, right_cpos);
3087 if (ret) {
3088 mlog_errno(ret);
3089 goto out;
3090 }
3091
3092 *ret_right_path = right_path;
3093out:
3094 if (ret)
3095 ocfs2_free_path(right_path);
3096 return ret;
3097}
3098
3099/*
3100 * Remove split_rec clusters from the record at index and merge them
3101 * onto the beginning of the record "next" to it.
3102 * For index < l_count - 1, the next means the extent rec at index + 1.
3103 * For index == l_count - 1, the "next" means the 1st extent rec of the
3104 * next extent block.
3105 */
3106static int ocfs2_merge_rec_right(struct inode *inode,
3107 struct ocfs2_path *left_path,
3108 handle_t *handle,
3109 struct ocfs2_extent_rec *split_rec,
3110 int index)
3111{
3112 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003113 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3114 struct ocfs2_extent_rec *left_rec;
3115 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003116 struct ocfs2_extent_list *right_el;
3117 struct ocfs2_path *right_path = NULL;
3118 int subtree_index = 0;
3119 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3120 struct buffer_head *bh = path_leaf_bh(left_path);
3121 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003122
3123 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003124 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003125
Al Viro9d8df6a2008-05-21 06:32:11 +01003126 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003127 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3128 /* we meet with a cross extent block merge. */
3129 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3130 if (ret) {
3131 mlog_errno(ret);
3132 goto out;
3133 }
3134
3135 right_el = path_leaf_el(right_path);
3136 next_free = le16_to_cpu(right_el->l_next_free_rec);
3137 BUG_ON(next_free <= 0);
3138 right_rec = &right_el->l_recs[0];
3139 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003140 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003141 right_rec = &right_el->l_recs[1];
3142 }
3143
3144 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3145 le16_to_cpu(left_rec->e_leaf_clusters) !=
3146 le32_to_cpu(right_rec->e_cpos));
3147
3148 subtree_index = ocfs2_find_subtree_root(inode,
3149 left_path, right_path);
3150
3151 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3152 handle->h_buffer_credits,
3153 right_path);
3154 if (ret) {
3155 mlog_errno(ret);
3156 goto out;
3157 }
3158
3159 root_bh = left_path->p_node[subtree_index].bh;
3160 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3161
3162 ret = ocfs2_journal_access(handle, inode, root_bh,
3163 OCFS2_JOURNAL_ACCESS_WRITE);
3164 if (ret) {
3165 mlog_errno(ret);
3166 goto out;
3167 }
3168
3169 for (i = subtree_index + 1;
3170 i < path_num_items(right_path); i++) {
3171 ret = ocfs2_journal_access(handle, inode,
3172 right_path->p_node[i].bh,
3173 OCFS2_JOURNAL_ACCESS_WRITE);
3174 if (ret) {
3175 mlog_errno(ret);
3176 goto out;
3177 }
3178
3179 ret = ocfs2_journal_access(handle, inode,
3180 left_path->p_node[i].bh,
3181 OCFS2_JOURNAL_ACCESS_WRITE);
3182 if (ret) {
3183 mlog_errno(ret);
3184 goto out;
3185 }
3186 }
3187
3188 } else {
3189 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3190 right_rec = &el->l_recs[index + 1];
3191 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003192
3193 ret = ocfs2_journal_access(handle, inode, bh,
3194 OCFS2_JOURNAL_ACCESS_WRITE);
3195 if (ret) {
3196 mlog_errno(ret);
3197 goto out;
3198 }
3199
3200 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3201
3202 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3203 le64_add_cpu(&right_rec->e_blkno,
3204 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3205 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3206
3207 ocfs2_cleanup_merge(el, index);
3208
3209 ret = ocfs2_journal_dirty(handle, bh);
3210 if (ret)
3211 mlog_errno(ret);
3212
Tao Ma677b9752008-01-30 14:21:05 +08003213 if (right_path) {
3214 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3215 if (ret)
3216 mlog_errno(ret);
3217
3218 ocfs2_complete_edge_insert(inode, handle, left_path,
3219 right_path, subtree_index);
3220 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003221out:
Tao Ma677b9752008-01-30 14:21:05 +08003222 if (right_path)
3223 ocfs2_free_path(right_path);
3224 return ret;
3225}
3226
3227static int ocfs2_get_left_path(struct inode *inode,
3228 struct ocfs2_path *right_path,
3229 struct ocfs2_path **ret_left_path)
3230{
3231 int ret;
3232 u32 left_cpos;
3233 struct ocfs2_path *left_path = NULL;
3234
3235 *ret_left_path = NULL;
3236
3237 /* This function shouldn't be called for non-trees. */
3238 BUG_ON(right_path->p_tree_depth == 0);
3239
3240 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3241 right_path, &left_cpos);
3242 if (ret) {
3243 mlog_errno(ret);
3244 goto out;
3245 }
3246
3247 /* This function shouldn't be called for the leftmost leaf. */
3248 BUG_ON(left_cpos == 0);
3249
3250 left_path = ocfs2_new_path(path_root_bh(right_path),
3251 path_root_el(right_path));
3252 if (!left_path) {
3253 ret = -ENOMEM;
3254 mlog_errno(ret);
3255 goto out;
3256 }
3257
3258 ret = ocfs2_find_path(inode, left_path, left_cpos);
3259 if (ret) {
3260 mlog_errno(ret);
3261 goto out;
3262 }
3263
3264 *ret_left_path = left_path;
3265out:
3266 if (ret)
3267 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003268 return ret;
3269}
3270
3271/*
3272 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003273 * onto the tail of the record "before" it.
3274 * For index > 0, the "before" means the extent rec at index - 1.
3275 *
3276 * For index == 0, the "before" means the last record of the previous
3277 * extent block. And there is also a situation that we may need to
3278 * remove the rightmost leaf extent block in the right_path and change
3279 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003280 */
Tao Ma677b9752008-01-30 14:21:05 +08003281static int ocfs2_merge_rec_left(struct inode *inode,
3282 struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003283 handle_t *handle,
3284 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003285 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003286 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003287 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003288{
Tao Ma677b9752008-01-30 14:21:05 +08003289 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003290 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3291 struct ocfs2_extent_rec *left_rec;
3292 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003293 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3294 struct buffer_head *bh = path_leaf_bh(right_path);
3295 struct buffer_head *root_bh = NULL;
3296 struct ocfs2_path *left_path = NULL;
3297 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003298
Tao Ma677b9752008-01-30 14:21:05 +08003299 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003300
Mark Fasheh328d5752007-06-18 10:48:04 -07003301 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003302 if (index == 0) {
3303 /* we meet with a cross extent block merge. */
3304 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3305 if (ret) {
3306 mlog_errno(ret);
3307 goto out;
3308 }
3309
3310 left_el = path_leaf_el(left_path);
3311 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3312 le16_to_cpu(left_el->l_count));
3313
3314 left_rec = &left_el->l_recs[
3315 le16_to_cpu(left_el->l_next_free_rec) - 1];
3316 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3317 le16_to_cpu(left_rec->e_leaf_clusters) !=
3318 le32_to_cpu(split_rec->e_cpos));
3319
3320 subtree_index = ocfs2_find_subtree_root(inode,
3321 left_path, right_path);
3322
3323 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3324 handle->h_buffer_credits,
3325 left_path);
3326 if (ret) {
3327 mlog_errno(ret);
3328 goto out;
3329 }
3330
3331 root_bh = left_path->p_node[subtree_index].bh;
3332 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3333
3334 ret = ocfs2_journal_access(handle, inode, root_bh,
3335 OCFS2_JOURNAL_ACCESS_WRITE);
3336 if (ret) {
3337 mlog_errno(ret);
3338 goto out;
3339 }
3340
3341 for (i = subtree_index + 1;
3342 i < path_num_items(right_path); i++) {
3343 ret = ocfs2_journal_access(handle, inode,
3344 right_path->p_node[i].bh,
3345 OCFS2_JOURNAL_ACCESS_WRITE);
3346 if (ret) {
3347 mlog_errno(ret);
3348 goto out;
3349 }
3350
3351 ret = ocfs2_journal_access(handle, inode,
3352 left_path->p_node[i].bh,
3353 OCFS2_JOURNAL_ACCESS_WRITE);
3354 if (ret) {
3355 mlog_errno(ret);
3356 goto out;
3357 }
3358 }
3359 } else {
3360 left_rec = &el->l_recs[index - 1];
3361 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3362 has_empty_extent = 1;
3363 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003364
3365 ret = ocfs2_journal_access(handle, inode, bh,
3366 OCFS2_JOURNAL_ACCESS_WRITE);
3367 if (ret) {
3368 mlog_errno(ret);
3369 goto out;
3370 }
3371
3372 if (has_empty_extent && index == 1) {
3373 /*
3374 * The easy case - we can just plop the record right in.
3375 */
3376 *left_rec = *split_rec;
3377
3378 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003379 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003380 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003381
3382 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3383 le64_add_cpu(&right_rec->e_blkno,
3384 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3385 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3386
3387 ocfs2_cleanup_merge(el, index);
3388
3389 ret = ocfs2_journal_dirty(handle, bh);
3390 if (ret)
3391 mlog_errno(ret);
3392
Tao Ma677b9752008-01-30 14:21:05 +08003393 if (left_path) {
3394 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3395 if (ret)
3396 mlog_errno(ret);
3397
3398 /*
3399 * In the situation that the right_rec is empty and the extent
3400 * block is empty also, ocfs2_complete_edge_insert can't handle
3401 * it and we need to delete the right extent block.
3402 */
3403 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3404 le16_to_cpu(el->l_next_free_rec) == 1) {
3405
3406 ret = ocfs2_remove_rightmost_path(inode, handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003407 right_path,
3408 dealloc, et);
Tao Ma677b9752008-01-30 14:21:05 +08003409 if (ret) {
3410 mlog_errno(ret);
3411 goto out;
3412 }
3413
3414 /* Now the rightmost extent block has been deleted.
3415 * So we use the new rightmost path.
3416 */
3417 ocfs2_mv_path(right_path, left_path);
3418 left_path = NULL;
3419 } else
3420 ocfs2_complete_edge_insert(inode, handle, left_path,
3421 right_path, subtree_index);
3422 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003423out:
Tao Ma677b9752008-01-30 14:21:05 +08003424 if (left_path)
3425 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003426 return ret;
3427}
3428
3429static int ocfs2_try_to_merge_extent(struct inode *inode,
3430 handle_t *handle,
Tao Ma677b9752008-01-30 14:21:05 +08003431 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003432 int split_index,
3433 struct ocfs2_extent_rec *split_rec,
3434 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003435 struct ocfs2_merge_ctxt *ctxt,
3436 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003437
3438{
Tao Mao518d7262007-08-28 17:25:35 -07003439 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003440 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003441 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3442
3443 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3444
Tao Mao518d7262007-08-28 17:25:35 -07003445 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3446 /*
3447 * The merge code will need to create an empty
3448 * extent to take the place of the newly
3449 * emptied slot. Remove any pre-existing empty
3450 * extents - having more than one in a leaf is
3451 * illegal.
3452 */
Tao Ma677b9752008-01-30 14:21:05 +08003453 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003454 dealloc, et);
Tao Mao518d7262007-08-28 17:25:35 -07003455 if (ret) {
3456 mlog_errno(ret);
3457 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003458 }
Tao Mao518d7262007-08-28 17:25:35 -07003459 split_index--;
3460 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003461 }
3462
3463 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3464 /*
3465 * Left-right contig implies this.
3466 */
3467 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003468
3469 /*
3470 * Since the leftright insert always covers the entire
3471 * extent, this call will delete the insert record
3472 * entirely, resulting in an empty extent record added to
3473 * the extent block.
3474 *
3475 * Since the adding of an empty extent shifts
3476 * everything back to the right, there's no need to
3477 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003478 *
3479 * When the split_index is zero, we need to merge it to the
3480 * prevoius extent block. It is more efficient and easier
3481 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003482 */
Tao Ma677b9752008-01-30 14:21:05 +08003483 ret = ocfs2_merge_rec_right(inode, path,
3484 handle, split_rec,
3485 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003486 if (ret) {
3487 mlog_errno(ret);
3488 goto out;
3489 }
3490
3491 /*
3492 * We can only get this from logic error above.
3493 */
3494 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3495
Tao Ma677b9752008-01-30 14:21:05 +08003496 /* The merge left us with an empty extent, remove it. */
Tao Mae7d4cb62008-08-18 17:38:44 +08003497 ret = ocfs2_rotate_tree_left(inode, handle, path,
3498 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003499 if (ret) {
3500 mlog_errno(ret);
3501 goto out;
3502 }
Tao Ma677b9752008-01-30 14:21:05 +08003503
Mark Fasheh328d5752007-06-18 10:48:04 -07003504 rec = &el->l_recs[split_index];
3505
3506 /*
3507 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003508 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003509 */
Tao Ma677b9752008-01-30 14:21:05 +08003510 ret = ocfs2_merge_rec_left(inode, path,
3511 handle, rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003512 dealloc, et,
Tao Ma677b9752008-01-30 14:21:05 +08003513 split_index);
3514
Mark Fasheh328d5752007-06-18 10:48:04 -07003515 if (ret) {
3516 mlog_errno(ret);
3517 goto out;
3518 }
3519
Tao Ma677b9752008-01-30 14:21:05 +08003520 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003521 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003522 /*
3523 * Error from this last rotate is not critical, so
3524 * print but don't bubble it up.
3525 */
3526 if (ret)
3527 mlog_errno(ret);
3528 ret = 0;
3529 } else {
3530 /*
3531 * Merge a record to the left or right.
3532 *
3533 * 'contig_type' is relative to the existing record,
3534 * so for example, if we're "right contig", it's to
3535 * the record on the left (hence the left merge).
3536 */
3537 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3538 ret = ocfs2_merge_rec_left(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003539 path,
3540 handle, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003541 dealloc, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003542 split_index);
3543 if (ret) {
3544 mlog_errno(ret);
3545 goto out;
3546 }
3547 } else {
3548 ret = ocfs2_merge_rec_right(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003549 path,
3550 handle, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003551 split_index);
3552 if (ret) {
3553 mlog_errno(ret);
3554 goto out;
3555 }
3556 }
3557
3558 if (ctxt->c_split_covers_rec) {
3559 /*
3560 * The merge may have left an empty extent in
3561 * our leaf. Try to rotate it away.
3562 */
Tao Ma677b9752008-01-30 14:21:05 +08003563 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003564 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003565 if (ret)
3566 mlog_errno(ret);
3567 ret = 0;
3568 }
3569 }
3570
3571out:
3572 return ret;
3573}
3574
3575static void ocfs2_subtract_from_rec(struct super_block *sb,
3576 enum ocfs2_split_type split,
3577 struct ocfs2_extent_rec *rec,
3578 struct ocfs2_extent_rec *split_rec)
3579{
3580 u64 len_blocks;
3581
3582 len_blocks = ocfs2_clusters_to_blocks(sb,
3583 le16_to_cpu(split_rec->e_leaf_clusters));
3584
3585 if (split == SPLIT_LEFT) {
3586 /*
3587 * Region is on the left edge of the existing
3588 * record.
3589 */
3590 le32_add_cpu(&rec->e_cpos,
3591 le16_to_cpu(split_rec->e_leaf_clusters));
3592 le64_add_cpu(&rec->e_blkno, len_blocks);
3593 le16_add_cpu(&rec->e_leaf_clusters,
3594 -le16_to_cpu(split_rec->e_leaf_clusters));
3595 } else {
3596 /*
3597 * Region is on the right edge of the existing
3598 * record.
3599 */
3600 le16_add_cpu(&rec->e_leaf_clusters,
3601 -le16_to_cpu(split_rec->e_leaf_clusters));
3602 }
3603}
3604
Mark Fashehdcd05382007-01-16 11:32:23 -08003605/*
3606 * Do the final bits of extent record insertion at the target leaf
3607 * list. If this leaf is part of an allocation tree, it is assumed
3608 * that the tree above has been prepared.
3609 */
3610static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3611 struct ocfs2_extent_list *el,
3612 struct ocfs2_insert_type *insert,
3613 struct inode *inode)
3614{
3615 int i = insert->ins_contig_index;
3616 unsigned int range;
3617 struct ocfs2_extent_rec *rec;
3618
Mark Fashehe48edee2007-03-07 16:46:57 -08003619 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003620
Mark Fasheh328d5752007-06-18 10:48:04 -07003621 if (insert->ins_split != SPLIT_NONE) {
3622 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3623 BUG_ON(i == -1);
3624 rec = &el->l_recs[i];
3625 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3626 insert_rec);
3627 goto rotate;
3628 }
3629
Mark Fashehdcd05382007-01-16 11:32:23 -08003630 /*
3631 * Contiguous insert - either left or right.
3632 */
3633 if (insert->ins_contig != CONTIG_NONE) {
3634 rec = &el->l_recs[i];
3635 if (insert->ins_contig == CONTIG_LEFT) {
3636 rec->e_blkno = insert_rec->e_blkno;
3637 rec->e_cpos = insert_rec->e_cpos;
3638 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003639 le16_add_cpu(&rec->e_leaf_clusters,
3640 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003641 return;
3642 }
3643
3644 /*
3645 * Handle insert into an empty leaf.
3646 */
3647 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3648 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3649 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3650 el->l_recs[0] = *insert_rec;
3651 el->l_next_free_rec = cpu_to_le16(1);
3652 return;
3653 }
3654
3655 /*
3656 * Appending insert.
3657 */
3658 if (insert->ins_appending == APPEND_TAIL) {
3659 i = le16_to_cpu(el->l_next_free_rec) - 1;
3660 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003661 range = le32_to_cpu(rec->e_cpos)
3662 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003663 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3664
3665 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3666 le16_to_cpu(el->l_count),
3667 "inode %lu, depth %u, count %u, next free %u, "
3668 "rec.cpos %u, rec.clusters %u, "
3669 "insert.cpos %u, insert.clusters %u\n",
3670 inode->i_ino,
3671 le16_to_cpu(el->l_tree_depth),
3672 le16_to_cpu(el->l_count),
3673 le16_to_cpu(el->l_next_free_rec),
3674 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003675 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003676 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003677 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003678 i++;
3679 el->l_recs[i] = *insert_rec;
3680 le16_add_cpu(&el->l_next_free_rec, 1);
3681 return;
3682 }
3683
Mark Fasheh328d5752007-06-18 10:48:04 -07003684rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003685 /*
3686 * Ok, we have to rotate.
3687 *
3688 * At this point, it is safe to assume that inserting into an
3689 * empty leaf and appending to a leaf have both been handled
3690 * above.
3691 *
3692 * This leaf needs to have space, either by the empty 1st
3693 * extent record, or by virtue of an l_next_rec < l_count.
3694 */
3695 ocfs2_rotate_leaf(el, insert_rec);
3696}
3697
Mark Fasheh328d5752007-06-18 10:48:04 -07003698static void ocfs2_adjust_rightmost_records(struct inode *inode,
3699 handle_t *handle,
3700 struct ocfs2_path *path,
3701 struct ocfs2_extent_rec *insert_rec)
3702{
3703 int ret, i, next_free;
3704 struct buffer_head *bh;
3705 struct ocfs2_extent_list *el;
3706 struct ocfs2_extent_rec *rec;
3707
3708 /*
3709 * Update everything except the leaf block.
3710 */
3711 for (i = 0; i < path->p_tree_depth; i++) {
3712 bh = path->p_node[i].bh;
3713 el = path->p_node[i].el;
3714
3715 next_free = le16_to_cpu(el->l_next_free_rec);
3716 if (next_free == 0) {
3717 ocfs2_error(inode->i_sb,
3718 "Dinode %llu has a bad extent list",
3719 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3720 ret = -EIO;
3721 return;
3722 }
3723
3724 rec = &el->l_recs[next_free - 1];
3725
3726 rec->e_int_clusters = insert_rec->e_cpos;
3727 le32_add_cpu(&rec->e_int_clusters,
3728 le16_to_cpu(insert_rec->e_leaf_clusters));
3729 le32_add_cpu(&rec->e_int_clusters,
3730 -le32_to_cpu(rec->e_cpos));
3731
3732 ret = ocfs2_journal_dirty(handle, bh);
3733 if (ret)
3734 mlog_errno(ret);
3735
3736 }
3737}
3738
Mark Fashehdcd05382007-01-16 11:32:23 -08003739static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3740 struct ocfs2_extent_rec *insert_rec,
3741 struct ocfs2_path *right_path,
3742 struct ocfs2_path **ret_left_path)
3743{
Mark Fasheh328d5752007-06-18 10:48:04 -07003744 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003745 struct ocfs2_extent_list *el;
3746 struct ocfs2_path *left_path = NULL;
3747
3748 *ret_left_path = NULL;
3749
3750 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003751 * This shouldn't happen for non-trees. The extent rec cluster
3752 * count manipulation below only works for interior nodes.
3753 */
3754 BUG_ON(right_path->p_tree_depth == 0);
3755
3756 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003757 * If our appending insert is at the leftmost edge of a leaf,
3758 * then we might need to update the rightmost records of the
3759 * neighboring path.
3760 */
3761 el = path_leaf_el(right_path);
3762 next_free = le16_to_cpu(el->l_next_free_rec);
3763 if (next_free == 0 ||
3764 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3765 u32 left_cpos;
3766
3767 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3768 &left_cpos);
3769 if (ret) {
3770 mlog_errno(ret);
3771 goto out;
3772 }
3773
3774 mlog(0, "Append may need a left path update. cpos: %u, "
3775 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3776 left_cpos);
3777
3778 /*
3779 * No need to worry if the append is already in the
3780 * leftmost leaf.
3781 */
3782 if (left_cpos) {
3783 left_path = ocfs2_new_path(path_root_bh(right_path),
3784 path_root_el(right_path));
3785 if (!left_path) {
3786 ret = -ENOMEM;
3787 mlog_errno(ret);
3788 goto out;
3789 }
3790
3791 ret = ocfs2_find_path(inode, left_path, left_cpos);
3792 if (ret) {
3793 mlog_errno(ret);
3794 goto out;
3795 }
3796
3797 /*
3798 * ocfs2_insert_path() will pass the left_path to the
3799 * journal for us.
3800 */
3801 }
3802 }
3803
3804 ret = ocfs2_journal_access_path(inode, handle, right_path);
3805 if (ret) {
3806 mlog_errno(ret);
3807 goto out;
3808 }
3809
Mark Fasheh328d5752007-06-18 10:48:04 -07003810 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08003811
3812 *ret_left_path = left_path;
3813 ret = 0;
3814out:
3815 if (ret != 0)
3816 ocfs2_free_path(left_path);
3817
3818 return ret;
3819}
3820
Mark Fasheh328d5752007-06-18 10:48:04 -07003821static void ocfs2_split_record(struct inode *inode,
3822 struct ocfs2_path *left_path,
3823 struct ocfs2_path *right_path,
3824 struct ocfs2_extent_rec *split_rec,
3825 enum ocfs2_split_type split)
3826{
3827 int index;
3828 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3829 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3830 struct ocfs2_extent_rec *rec, *tmprec;
3831
3832 right_el = path_leaf_el(right_path);;
3833 if (left_path)
3834 left_el = path_leaf_el(left_path);
3835
3836 el = right_el;
3837 insert_el = right_el;
3838 index = ocfs2_search_extent_list(el, cpos);
3839 if (index != -1) {
3840 if (index == 0 && left_path) {
3841 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3842
3843 /*
3844 * This typically means that the record
3845 * started in the left path but moved to the
3846 * right as a result of rotation. We either
3847 * move the existing record to the left, or we
3848 * do the later insert there.
3849 *
3850 * In this case, the left path should always
3851 * exist as the rotate code will have passed
3852 * it back for a post-insert update.
3853 */
3854
3855 if (split == SPLIT_LEFT) {
3856 /*
3857 * It's a left split. Since we know
3858 * that the rotate code gave us an
3859 * empty extent in the left path, we
3860 * can just do the insert there.
3861 */
3862 insert_el = left_el;
3863 } else {
3864 /*
3865 * Right split - we have to move the
3866 * existing record over to the left
3867 * leaf. The insert will be into the
3868 * newly created empty extent in the
3869 * right leaf.
3870 */
3871 tmprec = &right_el->l_recs[index];
3872 ocfs2_rotate_leaf(left_el, tmprec);
3873 el = left_el;
3874
3875 memset(tmprec, 0, sizeof(*tmprec));
3876 index = ocfs2_search_extent_list(left_el, cpos);
3877 BUG_ON(index == -1);
3878 }
3879 }
3880 } else {
3881 BUG_ON(!left_path);
3882 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3883 /*
3884 * Left path is easy - we can just allow the insert to
3885 * happen.
3886 */
3887 el = left_el;
3888 insert_el = left_el;
3889 index = ocfs2_search_extent_list(el, cpos);
3890 BUG_ON(index == -1);
3891 }
3892
3893 rec = &el->l_recs[index];
3894 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3895 ocfs2_rotate_leaf(insert_el, split_rec);
3896}
3897
Mark Fashehdcd05382007-01-16 11:32:23 -08003898/*
Tao Mae7d4cb62008-08-18 17:38:44 +08003899 * This function only does inserts on an allocation b-tree. For tree
3900 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08003901 *
3902 * right_path is the path we want to do the actual insert
3903 * in. left_path should only be passed in if we need to update that
3904 * portion of the tree after an edge insert.
3905 */
3906static int ocfs2_insert_path(struct inode *inode,
3907 handle_t *handle,
3908 struct ocfs2_path *left_path,
3909 struct ocfs2_path *right_path,
3910 struct ocfs2_extent_rec *insert_rec,
3911 struct ocfs2_insert_type *insert)
3912{
3913 int ret, subtree_index;
3914 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003915
Mark Fashehdcd05382007-01-16 11:32:23 -08003916 if (left_path) {
3917 int credits = handle->h_buffer_credits;
3918
3919 /*
3920 * There's a chance that left_path got passed back to
3921 * us without being accounted for in the
3922 * journal. Extend our transaction here to be sure we
3923 * can change those blocks.
3924 */
3925 credits += left_path->p_tree_depth;
3926
3927 ret = ocfs2_extend_trans(handle, credits);
3928 if (ret < 0) {
3929 mlog_errno(ret);
3930 goto out;
3931 }
3932
3933 ret = ocfs2_journal_access_path(inode, handle, left_path);
3934 if (ret < 0) {
3935 mlog_errno(ret);
3936 goto out;
3937 }
3938 }
3939
Mark Fashehe8aed342007-12-03 16:43:01 -08003940 /*
3941 * Pass both paths to the journal. The majority of inserts
3942 * will be touching all components anyway.
3943 */
3944 ret = ocfs2_journal_access_path(inode, handle, right_path);
3945 if (ret < 0) {
3946 mlog_errno(ret);
3947 goto out;
3948 }
3949
Mark Fasheh328d5752007-06-18 10:48:04 -07003950 if (insert->ins_split != SPLIT_NONE) {
3951 /*
3952 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02003953 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07003954 * function sort it all out.
3955 */
3956 ocfs2_split_record(inode, left_path, right_path,
3957 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08003958
3959 /*
3960 * Split might have modified either leaf and we don't
3961 * have a guarantee that the later edge insert will
3962 * dirty this for us.
3963 */
3964 if (left_path)
3965 ret = ocfs2_journal_dirty(handle,
3966 path_leaf_bh(left_path));
3967 if (ret)
3968 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07003969 } else
3970 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3971 insert, inode);
Mark Fashehdcd05382007-01-16 11:32:23 -08003972
Mark Fashehdcd05382007-01-16 11:32:23 -08003973 ret = ocfs2_journal_dirty(handle, leaf_bh);
3974 if (ret)
3975 mlog_errno(ret);
3976
3977 if (left_path) {
3978 /*
3979 * The rotate code has indicated that we need to fix
3980 * up portions of the tree after the insert.
3981 *
3982 * XXX: Should we extend the transaction here?
3983 */
3984 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3985 right_path);
3986 ocfs2_complete_edge_insert(inode, handle, left_path,
3987 right_path, subtree_index);
3988 }
3989
3990 ret = 0;
3991out:
3992 return ret;
3993}
3994
3995static int ocfs2_do_insert_extent(struct inode *inode,
3996 handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003997 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08003998 struct ocfs2_extent_rec *insert_rec,
3999 struct ocfs2_insert_type *type)
4000{
4001 int ret, rotate = 0;
4002 u32 cpos;
4003 struct ocfs2_path *right_path = NULL;
4004 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004005 struct ocfs2_extent_list *el;
4006
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004007 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004008
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004009 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehdcd05382007-01-16 11:32:23 -08004010 OCFS2_JOURNAL_ACCESS_WRITE);
4011 if (ret) {
4012 mlog_errno(ret);
4013 goto out;
4014 }
4015
4016 if (le16_to_cpu(el->l_tree_depth) == 0) {
4017 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4018 goto out_update_clusters;
4019 }
4020
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004021 right_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fashehdcd05382007-01-16 11:32:23 -08004022 if (!right_path) {
4023 ret = -ENOMEM;
4024 mlog_errno(ret);
4025 goto out;
4026 }
4027
4028 /*
4029 * Determine the path to start with. Rotations need the
4030 * rightmost path, everything else can go directly to the
4031 * target leaf.
4032 */
4033 cpos = le32_to_cpu(insert_rec->e_cpos);
4034 if (type->ins_appending == APPEND_NONE &&
4035 type->ins_contig == CONTIG_NONE) {
4036 rotate = 1;
4037 cpos = UINT_MAX;
4038 }
4039
4040 ret = ocfs2_find_path(inode, right_path, cpos);
4041 if (ret) {
4042 mlog_errno(ret);
4043 goto out;
4044 }
4045
4046 /*
4047 * Rotations and appends need special treatment - they modify
4048 * parts of the tree's above them.
4049 *
4050 * Both might pass back a path immediate to the left of the
4051 * one being inserted to. This will be cause
4052 * ocfs2_insert_path() to modify the rightmost records of
4053 * left_path to account for an edge insert.
4054 *
4055 * XXX: When modifying this code, keep in mind that an insert
4056 * can wind up skipping both of these two special cases...
4057 */
4058 if (rotate) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004059 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004060 le32_to_cpu(insert_rec->e_cpos),
4061 right_path, &left_path);
4062 if (ret) {
4063 mlog_errno(ret);
4064 goto out;
4065 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004066
4067 /*
4068 * ocfs2_rotate_tree_right() might have extended the
4069 * transaction without re-journaling our tree root.
4070 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004071 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehe8aed342007-12-03 16:43:01 -08004072 OCFS2_JOURNAL_ACCESS_WRITE);
4073 if (ret) {
4074 mlog_errno(ret);
4075 goto out;
4076 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004077 } else if (type->ins_appending == APPEND_TAIL
4078 && type->ins_contig != CONTIG_LEFT) {
4079 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4080 right_path, &left_path);
4081 if (ret) {
4082 mlog_errno(ret);
4083 goto out;
4084 }
4085 }
4086
4087 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4088 insert_rec, type);
4089 if (ret) {
4090 mlog_errno(ret);
4091 goto out;
4092 }
4093
4094out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004095 if (type->ins_split == SPLIT_NONE)
Joel Becker35dc0aa2008-08-20 16:25:06 -07004096 ocfs2_et_update_clusters(inode, et,
4097 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004098
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004099 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004100 if (ret)
4101 mlog_errno(ret);
4102
4103out:
4104 ocfs2_free_path(left_path);
4105 ocfs2_free_path(right_path);
4106
4107 return ret;
4108}
4109
Mark Fasheh328d5752007-06-18 10:48:04 -07004110static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08004111ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004112 struct ocfs2_extent_list *el, int index,
4113 struct ocfs2_extent_rec *split_rec)
4114{
Tao Maad5a4d72008-01-30 14:21:32 +08004115 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004116 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004117 u32 left_cpos, right_cpos;
4118 struct ocfs2_extent_rec *rec = NULL;
4119 struct ocfs2_extent_list *new_el;
4120 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4121 struct buffer_head *bh;
4122 struct ocfs2_extent_block *eb;
4123
4124 if (index > 0) {
4125 rec = &el->l_recs[index - 1];
4126 } else if (path->p_tree_depth > 0) {
4127 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4128 path, &left_cpos);
4129 if (status)
4130 goto out;
4131
4132 if (left_cpos != 0) {
4133 left_path = ocfs2_new_path(path_root_bh(path),
4134 path_root_el(path));
4135 if (!left_path)
4136 goto out;
4137
4138 status = ocfs2_find_path(inode, left_path, left_cpos);
4139 if (status)
4140 goto out;
4141
4142 new_el = path_leaf_el(left_path);
4143
4144 if (le16_to_cpu(new_el->l_next_free_rec) !=
4145 le16_to_cpu(new_el->l_count)) {
4146 bh = path_leaf_bh(left_path);
4147 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004148 ocfs2_error(inode->i_sb,
4149 "Extent block #%llu has an "
4150 "invalid l_next_free_rec of "
4151 "%d. It should have "
4152 "matched the l_count of %d",
4153 (unsigned long long)le64_to_cpu(eb->h_blkno),
4154 le16_to_cpu(new_el->l_next_free_rec),
4155 le16_to_cpu(new_el->l_count));
4156 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004157 goto out;
4158 }
4159 rec = &new_el->l_recs[
4160 le16_to_cpu(new_el->l_next_free_rec) - 1];
4161 }
4162 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004163
4164 /*
4165 * We're careful to check for an empty extent record here -
4166 * the merge code will know what to do if it sees one.
4167 */
Tao Maad5a4d72008-01-30 14:21:32 +08004168 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004169 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4170 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4171 ret = CONTIG_RIGHT;
4172 } else {
4173 ret = ocfs2_extent_contig(inode, rec, split_rec);
4174 }
4175 }
4176
Tao Maad5a4d72008-01-30 14:21:32 +08004177 rec = NULL;
4178 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4179 rec = &el->l_recs[index + 1];
4180 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4181 path->p_tree_depth > 0) {
4182 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4183 path, &right_cpos);
4184 if (status)
4185 goto out;
4186
4187 if (right_cpos == 0)
4188 goto out;
4189
4190 right_path = ocfs2_new_path(path_root_bh(path),
4191 path_root_el(path));
4192 if (!right_path)
4193 goto out;
4194
4195 status = ocfs2_find_path(inode, right_path, right_cpos);
4196 if (status)
4197 goto out;
4198
4199 new_el = path_leaf_el(right_path);
4200 rec = &new_el->l_recs[0];
4201 if (ocfs2_is_empty_extent(rec)) {
4202 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4203 bh = path_leaf_bh(right_path);
4204 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004205 ocfs2_error(inode->i_sb,
4206 "Extent block #%llu has an "
4207 "invalid l_next_free_rec of %d",
4208 (unsigned long long)le64_to_cpu(eb->h_blkno),
4209 le16_to_cpu(new_el->l_next_free_rec));
4210 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004211 goto out;
4212 }
4213 rec = &new_el->l_recs[1];
4214 }
4215 }
4216
4217 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004218 enum ocfs2_contig_type contig_type;
4219
Mark Fasheh328d5752007-06-18 10:48:04 -07004220 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4221
4222 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4223 ret = CONTIG_LEFTRIGHT;
4224 else if (ret == CONTIG_NONE)
4225 ret = contig_type;
4226 }
4227
Tao Maad5a4d72008-01-30 14:21:32 +08004228out:
4229 if (left_path)
4230 ocfs2_free_path(left_path);
4231 if (right_path)
4232 ocfs2_free_path(right_path);
4233
Mark Fasheh328d5752007-06-18 10:48:04 -07004234 return ret;
4235}
4236
Mark Fashehdcd05382007-01-16 11:32:23 -08004237static void ocfs2_figure_contig_type(struct inode *inode,
4238 struct ocfs2_insert_type *insert,
4239 struct ocfs2_extent_list *el,
Tao Maca12b7c2008-08-18 17:38:52 +08004240 struct ocfs2_extent_rec *insert_rec,
4241 struct ocfs2_extent_tree *et)
Mark Fashehdcd05382007-01-16 11:32:23 -08004242{
4243 int i;
4244 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4245
Mark Fashehe48edee2007-03-07 16:46:57 -08004246 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4247
Mark Fashehdcd05382007-01-16 11:32:23 -08004248 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4249 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4250 insert_rec);
4251 if (contig_type != CONTIG_NONE) {
4252 insert->ins_contig_index = i;
4253 break;
4254 }
4255 }
4256 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004257
4258 if (insert->ins_contig != CONTIG_NONE) {
4259 struct ocfs2_extent_rec *rec =
4260 &el->l_recs[insert->ins_contig_index];
4261 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4262 le16_to_cpu(insert_rec->e_leaf_clusters);
4263
4264 /*
4265 * Caller might want us to limit the size of extents, don't
4266 * calculate contiguousness if we might exceed that limit.
4267 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004268 if (et->et_max_leaf_clusters &&
4269 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004270 insert->ins_contig = CONTIG_NONE;
4271 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004272}
4273
4274/*
4275 * This should only be called against the righmost leaf extent list.
4276 *
4277 * ocfs2_figure_appending_type() will figure out whether we'll have to
4278 * insert at the tail of the rightmost leaf.
4279 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004280 * This should also work against the root extent list for tree's with 0
4281 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004282 * then the logic here makes sense.
4283 */
4284static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4285 struct ocfs2_extent_list *el,
4286 struct ocfs2_extent_rec *insert_rec)
4287{
4288 int i;
4289 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4290 struct ocfs2_extent_rec *rec;
4291
4292 insert->ins_appending = APPEND_NONE;
4293
Mark Fashehe48edee2007-03-07 16:46:57 -08004294 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004295
4296 if (!el->l_next_free_rec)
4297 goto set_tail_append;
4298
4299 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4300 /* Were all records empty? */
4301 if (le16_to_cpu(el->l_next_free_rec) == 1)
4302 goto set_tail_append;
4303 }
4304
4305 i = le16_to_cpu(el->l_next_free_rec) - 1;
4306 rec = &el->l_recs[i];
4307
Mark Fashehe48edee2007-03-07 16:46:57 -08004308 if (cpos >=
4309 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004310 goto set_tail_append;
4311
4312 return;
4313
4314set_tail_append:
4315 insert->ins_appending = APPEND_TAIL;
4316}
4317
4318/*
4319 * Helper function called at the begining of an insert.
4320 *
4321 * This computes a few things that are commonly used in the process of
4322 * inserting into the btree:
4323 * - Whether the new extent is contiguous with an existing one.
4324 * - The current tree depth.
4325 * - Whether the insert is an appending one.
4326 * - The total # of free records in the tree.
4327 *
4328 * All of the information is stored on the ocfs2_insert_type
4329 * structure.
4330 */
4331static int ocfs2_figure_insert_type(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004332 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004333 struct buffer_head **last_eb_bh,
4334 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004335 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004336 struct ocfs2_insert_type *insert)
4337{
4338 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004339 struct ocfs2_extent_block *eb;
4340 struct ocfs2_extent_list *el;
4341 struct ocfs2_path *path = NULL;
4342 struct buffer_head *bh = NULL;
4343
Mark Fasheh328d5752007-06-18 10:48:04 -07004344 insert->ins_split = SPLIT_NONE;
4345
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004346 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004347 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4348
4349 if (el->l_tree_depth) {
4350 /*
4351 * If we have tree depth, we read in the
4352 * rightmost extent block ahead of time as
4353 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4354 * may want it later.
4355 */
Joel Becker5e965812008-11-13 14:49:16 -08004356 ret = ocfs2_read_extent_block(inode,
4357 ocfs2_et_get_last_eb_blk(et),
4358 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004359 if (ret) {
4360 mlog_exit(ret);
4361 goto out;
4362 }
4363 eb = (struct ocfs2_extent_block *) bh->b_data;
4364 el = &eb->h_list;
4365 }
4366
4367 /*
4368 * Unless we have a contiguous insert, we'll need to know if
4369 * there is room left in our allocation tree for another
4370 * extent record.
4371 *
4372 * XXX: This test is simplistic, we can search for empty
4373 * extent records too.
4374 */
Tao Maoc77534f2007-08-28 17:22:33 -07004375 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004376 le16_to_cpu(el->l_next_free_rec);
4377
4378 if (!insert->ins_tree_depth) {
Tao Maca12b7c2008-08-18 17:38:52 +08004379 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004380 ocfs2_figure_appending_type(insert, el, insert_rec);
4381 return 0;
4382 }
4383
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004384 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fashehdcd05382007-01-16 11:32:23 -08004385 if (!path) {
4386 ret = -ENOMEM;
4387 mlog_errno(ret);
4388 goto out;
4389 }
4390
4391 /*
4392 * In the case that we're inserting past what the tree
4393 * currently accounts for, ocfs2_find_path() will return for
4394 * us the rightmost tree path. This is accounted for below in
4395 * the appending code.
4396 */
4397 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4398 if (ret) {
4399 mlog_errno(ret);
4400 goto out;
4401 }
4402
4403 el = path_leaf_el(path);
4404
4405 /*
4406 * Now that we have the path, there's two things we want to determine:
4407 * 1) Contiguousness (also set contig_index if this is so)
4408 *
4409 * 2) Are we doing an append? We can trivially break this up
4410 * into two types of appends: simple record append, or a
4411 * rotate inside the tail leaf.
4412 */
Tao Maca12b7c2008-08-18 17:38:52 +08004413 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004414
4415 /*
4416 * The insert code isn't quite ready to deal with all cases of
4417 * left contiguousness. Specifically, if it's an insert into
4418 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004419 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004420 * left. For now, just catch that case and fool the layers
4421 * above us. This works just fine for tree_depth == 0, which
4422 * is why we allow that above.
4423 */
4424 if (insert->ins_contig == CONTIG_LEFT &&
4425 insert->ins_contig_index == 0)
4426 insert->ins_contig = CONTIG_NONE;
4427
4428 /*
4429 * Ok, so we can simply compare against last_eb to figure out
4430 * whether the path doesn't exist. This will only happen in
4431 * the case that we're doing a tail append, so maybe we can
4432 * take advantage of that information somehow.
4433 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004434 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004435 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004436 /*
4437 * Ok, ocfs2_find_path() returned us the rightmost
4438 * tree path. This might be an appending insert. There are
4439 * two cases:
4440 * 1) We're doing a true append at the tail:
4441 * -This might even be off the end of the leaf
4442 * 2) We're "appending" by rotating in the tail
4443 */
4444 ocfs2_figure_appending_type(insert, el, insert_rec);
4445 }
4446
4447out:
4448 ocfs2_free_path(path);
4449
4450 if (ret == 0)
4451 *last_eb_bh = bh;
4452 else
4453 brelse(bh);
4454 return ret;
4455}
4456
4457/*
4458 * Insert an extent into an inode btree.
4459 *
4460 * The caller needs to update fe->i_clusters
4461 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004462int ocfs2_insert_extent(struct ocfs2_super *osb,
4463 handle_t *handle,
4464 struct inode *inode,
4465 struct ocfs2_extent_tree *et,
4466 u32 cpos,
4467 u64 start_blk,
4468 u32 new_clusters,
4469 u8 flags,
4470 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004471{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004472 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004473 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004474 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004475 struct ocfs2_insert_type insert = {0, };
4476 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004477
Mark Fashehdcd05382007-01-16 11:32:23 -08004478 mlog(0, "add %u clusters at position %u to inode %llu\n",
4479 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004480
Mark Fashehe48edee2007-03-07 16:46:57 -08004481 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004482 rec.e_cpos = cpu_to_le32(cpos);
4483 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004484 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004485 rec.e_flags = flags;
Joel Becker1e61ee72008-08-20 18:32:45 -07004486 status = ocfs2_et_insert_check(inode, et, &rec);
4487 if (status) {
4488 mlog_errno(status);
4489 goto bail;
4490 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004491
Tao Mae7d4cb62008-08-18 17:38:44 +08004492 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004493 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004494 if (status < 0) {
4495 mlog_errno(status);
4496 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004497 }
4498
Mark Fashehdcd05382007-01-16 11:32:23 -08004499 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4500 "Insert.contig_index: %d, Insert.free_records: %d, "
4501 "Insert.tree_depth: %d\n",
4502 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004503 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004504
Tao Maoc77534f2007-08-28 17:22:33 -07004505 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004506 status = ocfs2_grow_tree(inode, handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004507 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004508 meta_ac);
4509 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004510 mlog_errno(status);
4511 goto bail;
4512 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004513 }
4514
Mark Fashehdcd05382007-01-16 11:32:23 -08004515 /* Finally, we can add clusters. This might rotate the tree for us. */
Tao Mae7d4cb62008-08-18 17:38:44 +08004516 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004517 if (status < 0)
4518 mlog_errno(status);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004519 else if (et->et_ops == &ocfs2_dinode_et_ops)
Mark Fasheh83418972007-04-23 18:53:12 -07004520 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004521
4522bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004523 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004524
Tao Maf56654c2008-08-18 17:38:48 +08004525 mlog_exit(status);
4526 return status;
4527}
4528
Tao Ma0eb8d472008-08-18 17:38:45 +08004529/*
4530 * Allcate and add clusters into the extent b-tree.
4531 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004532 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004533 * it is not limited to the file storage. Any extent tree can use this
4534 * function if it implements the proper ocfs2_extent_tree.
4535 */
4536int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4537 struct inode *inode,
4538 u32 *logical_offset,
4539 u32 clusters_to_add,
4540 int mark_unwritten,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004541 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004542 handle_t *handle,
4543 struct ocfs2_alloc_context *data_ac,
4544 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004545 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004546{
4547 int status = 0;
4548 int free_extents;
4549 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4550 u32 bit_off, num_bits;
4551 u64 block;
4552 u8 flags = 0;
4553
4554 BUG_ON(!clusters_to_add);
4555
4556 if (mark_unwritten)
4557 flags = OCFS2_EXT_UNWRITTEN;
4558
Joel Beckerf99b9b72008-08-20 19:36:33 -07004559 free_extents = ocfs2_num_free_extents(osb, inode, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004560 if (free_extents < 0) {
4561 status = free_extents;
4562 mlog_errno(status);
4563 goto leave;
4564 }
4565
4566 /* there are two cases which could cause us to EAGAIN in the
4567 * we-need-more-metadata case:
4568 * 1) we haven't reserved *any*
4569 * 2) we are so fragmented, we've needed to add metadata too
4570 * many times. */
4571 if (!free_extents && !meta_ac) {
4572 mlog(0, "we haven't reserved any metadata!\n");
4573 status = -EAGAIN;
4574 reason = RESTART_META;
4575 goto leave;
4576 } else if ((!free_extents)
4577 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004578 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004579 mlog(0, "filesystem is really fragmented...\n");
4580 status = -EAGAIN;
4581 reason = RESTART_META;
4582 goto leave;
4583 }
4584
4585 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4586 clusters_to_add, &bit_off, &num_bits);
4587 if (status < 0) {
4588 if (status != -ENOSPC)
4589 mlog_errno(status);
4590 goto leave;
4591 }
4592
4593 BUG_ON(num_bits > clusters_to_add);
4594
4595 /* reserve our write early -- insert_extent may update the inode */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004596 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Tao Ma0eb8d472008-08-18 17:38:45 +08004597 OCFS2_JOURNAL_ACCESS_WRITE);
4598 if (status < 0) {
4599 mlog_errno(status);
4600 goto leave;
4601 }
4602
4603 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4604 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4605 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004606 status = ocfs2_insert_extent(osb, handle, inode, et,
4607 *logical_offset, block,
4608 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004609 if (status < 0) {
4610 mlog_errno(status);
4611 goto leave;
4612 }
4613
Joel Beckerf99b9b72008-08-20 19:36:33 -07004614 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004615 if (status < 0) {
4616 mlog_errno(status);
4617 goto leave;
4618 }
4619
4620 clusters_to_add -= num_bits;
4621 *logical_offset += num_bits;
4622
4623 if (clusters_to_add) {
4624 mlog(0, "need to alloc once more, wanted = %u\n",
4625 clusters_to_add);
4626 status = -EAGAIN;
4627 reason = RESTART_TRANS;
4628 }
4629
4630leave:
4631 mlog_exit(status);
4632 if (reason_ret)
4633 *reason_ret = reason;
4634 return status;
4635}
4636
Mark Fasheh328d5752007-06-18 10:48:04 -07004637static void ocfs2_make_right_split_rec(struct super_block *sb,
4638 struct ocfs2_extent_rec *split_rec,
4639 u32 cpos,
4640 struct ocfs2_extent_rec *rec)
4641{
4642 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4643 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4644
4645 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4646
4647 split_rec->e_cpos = cpu_to_le32(cpos);
4648 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4649
4650 split_rec->e_blkno = rec->e_blkno;
4651 le64_add_cpu(&split_rec->e_blkno,
4652 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4653
4654 split_rec->e_flags = rec->e_flags;
4655}
4656
4657static int ocfs2_split_and_insert(struct inode *inode,
4658 handle_t *handle,
4659 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004660 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004661 struct buffer_head **last_eb_bh,
4662 int split_index,
4663 struct ocfs2_extent_rec *orig_split_rec,
4664 struct ocfs2_alloc_context *meta_ac)
4665{
4666 int ret = 0, depth;
4667 unsigned int insert_range, rec_range, do_leftright = 0;
4668 struct ocfs2_extent_rec tmprec;
4669 struct ocfs2_extent_list *rightmost_el;
4670 struct ocfs2_extent_rec rec;
4671 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4672 struct ocfs2_insert_type insert;
4673 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004674
4675leftright:
4676 /*
4677 * Store a copy of the record on the stack - it might move
4678 * around as the tree is manipulated below.
4679 */
4680 rec = path_leaf_el(path)->l_recs[split_index];
4681
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004682 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004683
4684 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4685 if (depth) {
4686 BUG_ON(!(*last_eb_bh));
4687 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4688 rightmost_el = &eb->h_list;
4689 }
4690
4691 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4692 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004693 ret = ocfs2_grow_tree(inode, handle, et,
4694 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004695 if (ret) {
4696 mlog_errno(ret);
4697 goto out;
4698 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004699 }
4700
4701 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4702 insert.ins_appending = APPEND_NONE;
4703 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004704 insert.ins_tree_depth = depth;
4705
4706 insert_range = le32_to_cpu(split_rec.e_cpos) +
4707 le16_to_cpu(split_rec.e_leaf_clusters);
4708 rec_range = le32_to_cpu(rec.e_cpos) +
4709 le16_to_cpu(rec.e_leaf_clusters);
4710
4711 if (split_rec.e_cpos == rec.e_cpos) {
4712 insert.ins_split = SPLIT_LEFT;
4713 } else if (insert_range == rec_range) {
4714 insert.ins_split = SPLIT_RIGHT;
4715 } else {
4716 /*
4717 * Left/right split. We fake this as a right split
4718 * first and then make a second pass as a left split.
4719 */
4720 insert.ins_split = SPLIT_RIGHT;
4721
4722 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4723 &rec);
4724
4725 split_rec = tmprec;
4726
4727 BUG_ON(do_leftright);
4728 do_leftright = 1;
4729 }
4730
Tao Mae7d4cb62008-08-18 17:38:44 +08004731 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004732 if (ret) {
4733 mlog_errno(ret);
4734 goto out;
4735 }
4736
4737 if (do_leftright == 1) {
4738 u32 cpos;
4739 struct ocfs2_extent_list *el;
4740
4741 do_leftright++;
4742 split_rec = *orig_split_rec;
4743
4744 ocfs2_reinit_path(path, 1);
4745
4746 cpos = le32_to_cpu(split_rec.e_cpos);
4747 ret = ocfs2_find_path(inode, path, cpos);
4748 if (ret) {
4749 mlog_errno(ret);
4750 goto out;
4751 }
4752
4753 el = path_leaf_el(path);
4754 split_index = ocfs2_search_extent_list(el, cpos);
4755 goto leftright;
4756 }
4757out:
4758
4759 return ret;
4760}
4761
4762/*
4763 * Mark part or all of the extent record at split_index in the leaf
4764 * pointed to by path as written. This removes the unwritten
4765 * extent flag.
4766 *
4767 * Care is taken to handle contiguousness so as to not grow the tree.
4768 *
4769 * meta_ac is not strictly necessary - we only truly need it if growth
4770 * of the tree is required. All other cases will degrade into a less
4771 * optimal tree layout.
4772 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004773 * last_eb_bh should be the rightmost leaf block for any extent
4774 * btree. Since a split may grow the tree or a merge might shrink it,
4775 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07004776 *
4777 * This code is optimized for readability - several passes might be
4778 * made over certain portions of the tree. All of those blocks will
4779 * have been brought into cache (and pinned via the journal), so the
4780 * extra overhead is not expressed in terms of disk reads.
4781 */
4782static int __ocfs2_mark_extent_written(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004783 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004784 handle_t *handle,
4785 struct ocfs2_path *path,
4786 int split_index,
4787 struct ocfs2_extent_rec *split_rec,
4788 struct ocfs2_alloc_context *meta_ac,
4789 struct ocfs2_cached_dealloc_ctxt *dealloc)
4790{
4791 int ret = 0;
4792 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004793 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07004794 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4795 struct ocfs2_merge_ctxt ctxt;
4796 struct ocfs2_extent_list *rightmost_el;
4797
Roel Kluin3cf0c502007-10-27 00:20:36 +02004798 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004799 ret = -EIO;
4800 mlog_errno(ret);
4801 goto out;
4802 }
4803
4804 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4805 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4806 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4807 ret = -EIO;
4808 mlog_errno(ret);
4809 goto out;
4810 }
4811
Tao Maad5a4d72008-01-30 14:21:32 +08004812 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07004813 split_index,
4814 split_rec);
4815
4816 /*
4817 * The core merge / split code wants to know how much room is
4818 * left in this inodes allocation tree, so we pass the
4819 * rightmost extent list.
4820 */
4821 if (path->p_tree_depth) {
4822 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004823
Joel Becker5e965812008-11-13 14:49:16 -08004824 ret = ocfs2_read_extent_block(inode,
4825 ocfs2_et_get_last_eb_blk(et),
4826 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07004827 if (ret) {
4828 mlog_exit(ret);
4829 goto out;
4830 }
4831
4832 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07004833 rightmost_el = &eb->h_list;
4834 } else
4835 rightmost_el = path_root_el(path);
4836
Mark Fasheh328d5752007-06-18 10:48:04 -07004837 if (rec->e_cpos == split_rec->e_cpos &&
4838 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4839 ctxt.c_split_covers_rec = 1;
4840 else
4841 ctxt.c_split_covers_rec = 0;
4842
4843 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4844
Mark Fasheh015452b2007-09-12 10:21:22 -07004845 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4846 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4847 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004848
4849 if (ctxt.c_contig_type == CONTIG_NONE) {
4850 if (ctxt.c_split_covers_rec)
4851 el->l_recs[split_index] = *split_rec;
4852 else
Tao Mae7d4cb62008-08-18 17:38:44 +08004853 ret = ocfs2_split_and_insert(inode, handle, path, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004854 &last_eb_bh, split_index,
4855 split_rec, meta_ac);
4856 if (ret)
4857 mlog_errno(ret);
4858 } else {
4859 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4860 split_index, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08004861 dealloc, &ctxt, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07004862 if (ret)
4863 mlog_errno(ret);
4864 }
4865
Mark Fasheh328d5752007-06-18 10:48:04 -07004866out:
4867 brelse(last_eb_bh);
4868 return ret;
4869}
4870
4871/*
4872 * Mark the already-existing extent at cpos as written for len clusters.
4873 *
4874 * If the existing extent is larger than the request, initiate a
4875 * split. An attempt will be made at merging with adjacent extents.
4876 *
4877 * The caller is responsible for passing down meta_ac if we'll need it.
4878 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004879int ocfs2_mark_extent_written(struct inode *inode,
4880 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004881 handle_t *handle, u32 cpos, u32 len, u32 phys,
4882 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004883 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07004884{
4885 int ret, index;
4886 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4887 struct ocfs2_extent_rec split_rec;
4888 struct ocfs2_path *left_path = NULL;
4889 struct ocfs2_extent_list *el;
4890
4891 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4892 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4893
4894 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4895 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4896 "that are being written to, but the feature bit "
4897 "is not set in the super block.",
4898 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4899 ret = -EROFS;
4900 goto out;
4901 }
4902
4903 /*
4904 * XXX: This should be fixed up so that we just re-insert the
4905 * next extent records.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004906 *
4907 * XXX: This is a hack on the extent tree, maybe it should be
4908 * an op?
Mark Fasheh328d5752007-06-18 10:48:04 -07004909 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004910 if (et->et_ops == &ocfs2_dinode_et_ops)
Tao Mae7d4cb62008-08-18 17:38:44 +08004911 ocfs2_extent_map_trunc(inode, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07004912
Joel Beckerf99b9b72008-08-20 19:36:33 -07004913 left_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fasheh328d5752007-06-18 10:48:04 -07004914 if (!left_path) {
4915 ret = -ENOMEM;
4916 mlog_errno(ret);
4917 goto out;
4918 }
4919
4920 ret = ocfs2_find_path(inode, left_path, cpos);
4921 if (ret) {
4922 mlog_errno(ret);
4923 goto out;
4924 }
4925 el = path_leaf_el(left_path);
4926
4927 index = ocfs2_search_extent_list(el, cpos);
4928 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4929 ocfs2_error(inode->i_sb,
4930 "Inode %llu has an extent at cpos %u which can no "
4931 "longer be found.\n",
4932 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4933 ret = -EROFS;
4934 goto out;
4935 }
4936
4937 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4938 split_rec.e_cpos = cpu_to_le32(cpos);
4939 split_rec.e_leaf_clusters = cpu_to_le16(len);
4940 split_rec.e_blkno = cpu_to_le64(start_blkno);
4941 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4942 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4943
Joel Beckerf99b9b72008-08-20 19:36:33 -07004944 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004945 index, &split_rec, meta_ac,
4946 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07004947 if (ret)
4948 mlog_errno(ret);
4949
4950out:
4951 ocfs2_free_path(left_path);
4952 return ret;
4953}
4954
Tao Mae7d4cb62008-08-18 17:38:44 +08004955static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07004956 handle_t *handle, struct ocfs2_path *path,
4957 int index, u32 new_range,
4958 struct ocfs2_alloc_context *meta_ac)
4959{
4960 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07004961 struct buffer_head *last_eb_bh = NULL;
4962 struct ocfs2_extent_block *eb;
4963 struct ocfs2_extent_list *rightmost_el, *el;
4964 struct ocfs2_extent_rec split_rec;
4965 struct ocfs2_extent_rec *rec;
4966 struct ocfs2_insert_type insert;
4967
4968 /*
4969 * Setup the record to split before we grow the tree.
4970 */
4971 el = path_leaf_el(path);
4972 rec = &el->l_recs[index];
4973 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4974
4975 depth = path->p_tree_depth;
4976 if (depth > 0) {
Joel Becker5e965812008-11-13 14:49:16 -08004977 ret = ocfs2_read_extent_block(inode,
4978 ocfs2_et_get_last_eb_blk(et),
4979 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07004980 if (ret < 0) {
4981 mlog_errno(ret);
4982 goto out;
4983 }
4984
4985 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4986 rightmost_el = &eb->h_list;
4987 } else
4988 rightmost_el = path_leaf_el(path);
4989
Tao Ma811f9332008-08-18 17:38:43 +08004990 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004991 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07004992 ret = ocfs2_extend_trans(handle, credits);
4993 if (ret) {
4994 mlog_errno(ret);
4995 goto out;
4996 }
4997
4998 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4999 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005000 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005001 meta_ac);
5002 if (ret) {
5003 mlog_errno(ret);
5004 goto out;
5005 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005006 }
5007
5008 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5009 insert.ins_appending = APPEND_NONE;
5010 insert.ins_contig = CONTIG_NONE;
5011 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005012 insert.ins_tree_depth = depth;
5013
Tao Mae7d4cb62008-08-18 17:38:44 +08005014 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005015 if (ret)
5016 mlog_errno(ret);
5017
5018out:
5019 brelse(last_eb_bh);
5020 return ret;
5021}
5022
5023static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5024 struct ocfs2_path *path, int index,
5025 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08005026 u32 cpos, u32 len,
5027 struct ocfs2_extent_tree *et)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005028{
5029 int ret;
5030 u32 left_cpos, rec_range, trunc_range;
5031 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5032 struct super_block *sb = inode->i_sb;
5033 struct ocfs2_path *left_path = NULL;
5034 struct ocfs2_extent_list *el = path_leaf_el(path);
5035 struct ocfs2_extent_rec *rec;
5036 struct ocfs2_extent_block *eb;
5037
5038 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005039 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005040 if (ret) {
5041 mlog_errno(ret);
5042 goto out;
5043 }
5044
5045 index--;
5046 }
5047
5048 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5049 path->p_tree_depth) {
5050 /*
5051 * Check whether this is the rightmost tree record. If
5052 * we remove all of this record or part of its right
5053 * edge then an update of the record lengths above it
5054 * will be required.
5055 */
5056 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5057 if (eb->h_next_leaf_blk == 0)
5058 is_rightmost_tree_rec = 1;
5059 }
5060
5061 rec = &el->l_recs[index];
5062 if (index == 0 && path->p_tree_depth &&
5063 le32_to_cpu(rec->e_cpos) == cpos) {
5064 /*
5065 * Changing the leftmost offset (via partial or whole
5066 * record truncate) of an interior (or rightmost) path
5067 * means we have to update the subtree that is formed
5068 * by this leaf and the one to it's left.
5069 *
5070 * There are two cases we can skip:
5071 * 1) Path is the leftmost one in our inode tree.
5072 * 2) The leaf is rightmost and will be empty after
5073 * we remove the extent record - the rotate code
5074 * knows how to update the newly formed edge.
5075 */
5076
5077 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5078 &left_cpos);
5079 if (ret) {
5080 mlog_errno(ret);
5081 goto out;
5082 }
5083
5084 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5085 left_path = ocfs2_new_path(path_root_bh(path),
5086 path_root_el(path));
5087 if (!left_path) {
5088 ret = -ENOMEM;
5089 mlog_errno(ret);
5090 goto out;
5091 }
5092
5093 ret = ocfs2_find_path(inode, left_path, left_cpos);
5094 if (ret) {
5095 mlog_errno(ret);
5096 goto out;
5097 }
5098 }
5099 }
5100
5101 ret = ocfs2_extend_rotate_transaction(handle, 0,
5102 handle->h_buffer_credits,
5103 path);
5104 if (ret) {
5105 mlog_errno(ret);
5106 goto out;
5107 }
5108
5109 ret = ocfs2_journal_access_path(inode, handle, path);
5110 if (ret) {
5111 mlog_errno(ret);
5112 goto out;
5113 }
5114
5115 ret = ocfs2_journal_access_path(inode, handle, left_path);
5116 if (ret) {
5117 mlog_errno(ret);
5118 goto out;
5119 }
5120
5121 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5122 trunc_range = cpos + len;
5123
5124 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5125 int next_free;
5126
5127 memset(rec, 0, sizeof(*rec));
5128 ocfs2_cleanup_merge(el, index);
5129 wants_rotate = 1;
5130
5131 next_free = le16_to_cpu(el->l_next_free_rec);
5132 if (is_rightmost_tree_rec && next_free > 1) {
5133 /*
5134 * We skip the edge update if this path will
5135 * be deleted by the rotate code.
5136 */
5137 rec = &el->l_recs[next_free - 1];
5138 ocfs2_adjust_rightmost_records(inode, handle, path,
5139 rec);
5140 }
5141 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5142 /* Remove leftmost portion of the record. */
5143 le32_add_cpu(&rec->e_cpos, len);
5144 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5145 le16_add_cpu(&rec->e_leaf_clusters, -len);
5146 } else if (rec_range == trunc_range) {
5147 /* Remove rightmost portion of the record */
5148 le16_add_cpu(&rec->e_leaf_clusters, -len);
5149 if (is_rightmost_tree_rec)
5150 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5151 } else {
5152 /* Caller should have trapped this. */
5153 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5154 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5155 le32_to_cpu(rec->e_cpos),
5156 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5157 BUG();
5158 }
5159
5160 if (left_path) {
5161 int subtree_index;
5162
5163 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5164 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5165 subtree_index);
5166 }
5167
5168 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5169
Tao Mae7d4cb62008-08-18 17:38:44 +08005170 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005171 if (ret) {
5172 mlog_errno(ret);
5173 goto out;
5174 }
5175
5176out:
5177 ocfs2_free_path(left_path);
5178 return ret;
5179}
5180
Joel Beckerf99b9b72008-08-20 19:36:33 -07005181int ocfs2_remove_extent(struct inode *inode,
5182 struct ocfs2_extent_tree *et,
Mark Fasheh063c4562007-07-03 13:34:11 -07005183 u32 cpos, u32 len, handle_t *handle,
5184 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005185 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005186{
5187 int ret, index;
5188 u32 rec_range, trunc_range;
5189 struct ocfs2_extent_rec *rec;
5190 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005191 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005192
5193 ocfs2_extent_map_trunc(inode, 0);
5194
Joel Beckerf99b9b72008-08-20 19:36:33 -07005195 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005196 if (!path) {
5197 ret = -ENOMEM;
5198 mlog_errno(ret);
5199 goto out;
5200 }
5201
5202 ret = ocfs2_find_path(inode, path, cpos);
5203 if (ret) {
5204 mlog_errno(ret);
5205 goto out;
5206 }
5207
5208 el = path_leaf_el(path);
5209 index = ocfs2_search_extent_list(el, cpos);
5210 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5211 ocfs2_error(inode->i_sb,
5212 "Inode %llu has an extent at cpos %u which can no "
5213 "longer be found.\n",
5214 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5215 ret = -EROFS;
5216 goto out;
5217 }
5218
5219 /*
5220 * We have 3 cases of extent removal:
5221 * 1) Range covers the entire extent rec
5222 * 2) Range begins or ends on one edge of the extent rec
5223 * 3) Range is in the middle of the extent rec (no shared edges)
5224 *
5225 * For case 1 we remove the extent rec and left rotate to
5226 * fill the hole.
5227 *
5228 * For case 2 we just shrink the existing extent rec, with a
5229 * tree update if the shrinking edge is also the edge of an
5230 * extent block.
5231 *
5232 * For case 3 we do a right split to turn the extent rec into
5233 * something case 2 can handle.
5234 */
5235 rec = &el->l_recs[index];
5236 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5237 trunc_range = cpos + len;
5238
5239 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5240
5241 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5242 "(cpos %u, len %u)\n",
5243 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5244 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5245
5246 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5247 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005248 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005249 if (ret) {
5250 mlog_errno(ret);
5251 goto out;
5252 }
5253 } else {
Joel Beckerf99b9b72008-08-20 19:36:33 -07005254 ret = ocfs2_split_tree(inode, et, handle, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005255 trunc_range, meta_ac);
5256 if (ret) {
5257 mlog_errno(ret);
5258 goto out;
5259 }
5260
5261 /*
5262 * The split could have manipulated the tree enough to
5263 * move the record location, so we have to look for it again.
5264 */
5265 ocfs2_reinit_path(path, 1);
5266
5267 ret = ocfs2_find_path(inode, path, cpos);
5268 if (ret) {
5269 mlog_errno(ret);
5270 goto out;
5271 }
5272
5273 el = path_leaf_el(path);
5274 index = ocfs2_search_extent_list(el, cpos);
5275 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5276 ocfs2_error(inode->i_sb,
5277 "Inode %llu: split at cpos %u lost record.",
5278 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5279 cpos);
5280 ret = -EROFS;
5281 goto out;
5282 }
5283
5284 /*
5285 * Double check our values here. If anything is fishy,
5286 * it's easier to catch it at the top level.
5287 */
5288 rec = &el->l_recs[index];
5289 rec_range = le32_to_cpu(rec->e_cpos) +
5290 ocfs2_rec_clusters(el, rec);
5291 if (rec_range != trunc_range) {
5292 ocfs2_error(inode->i_sb,
5293 "Inode %llu: error after split at cpos %u"
5294 "trunc len %u, existing record is (%u,%u)",
5295 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5296 cpos, len, le32_to_cpu(rec->e_cpos),
5297 ocfs2_rec_clusters(el, rec));
5298 ret = -EROFS;
5299 goto out;
5300 }
5301
5302 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005303 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005304 if (ret) {
5305 mlog_errno(ret);
5306 goto out;
5307 }
5308 }
5309
5310out:
5311 ocfs2_free_path(path);
5312 return ret;
5313}
5314
Mark Fashehfecc0112008-11-12 15:16:38 -08005315int ocfs2_remove_btree_range(struct inode *inode,
5316 struct ocfs2_extent_tree *et,
5317 u32 cpos, u32 phys_cpos, u32 len,
5318 struct ocfs2_cached_dealloc_ctxt *dealloc)
5319{
5320 int ret;
5321 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5322 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5323 struct inode *tl_inode = osb->osb_tl_inode;
5324 handle_t *handle;
5325 struct ocfs2_alloc_context *meta_ac = NULL;
5326
5327 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5328 if (ret) {
5329 mlog_errno(ret);
5330 return ret;
5331 }
5332
5333 mutex_lock(&tl_inode->i_mutex);
5334
5335 if (ocfs2_truncate_log_needs_flush(osb)) {
5336 ret = __ocfs2_flush_truncate_log(osb);
5337 if (ret < 0) {
5338 mlog_errno(ret);
5339 goto out;
5340 }
5341 }
5342
Jan Karaa90714c2008-10-09 19:38:40 +02005343 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005344 if (IS_ERR(handle)) {
5345 ret = PTR_ERR(handle);
5346 mlog_errno(ret);
5347 goto out;
5348 }
5349
5350 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
5351 OCFS2_JOURNAL_ACCESS_WRITE);
5352 if (ret) {
5353 mlog_errno(ret);
5354 goto out;
5355 }
5356
5357 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5358 dealloc);
5359 if (ret) {
5360 mlog_errno(ret);
5361 goto out_commit;
5362 }
5363
5364 ocfs2_et_update_clusters(inode, et, -len);
5365
5366 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5367 if (ret) {
5368 mlog_errno(ret);
5369 goto out_commit;
5370 }
5371
5372 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5373 if (ret)
5374 mlog_errno(ret);
5375
5376out_commit:
5377 ocfs2_commit_trans(osb, handle);
5378out:
5379 mutex_unlock(&tl_inode->i_mutex);
5380
5381 if (meta_ac)
5382 ocfs2_free_alloc_context(meta_ac);
5383
5384 return ret;
5385}
5386
Mark Fasheh063c4562007-07-03 13:34:11 -07005387int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005388{
5389 struct buffer_head *tl_bh = osb->osb_tl_bh;
5390 struct ocfs2_dinode *di;
5391 struct ocfs2_truncate_log *tl;
5392
5393 di = (struct ocfs2_dinode *) tl_bh->b_data;
5394 tl = &di->id2.i_dealloc;
5395
5396 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5397 "slot %d, invalid truncate log parameters: used = "
5398 "%u, count = %u\n", osb->slot_num,
5399 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5400 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5401}
5402
5403static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5404 unsigned int new_start)
5405{
5406 unsigned int tail_index;
5407 unsigned int current_tail;
5408
5409 /* No records, nothing to coalesce */
5410 if (!le16_to_cpu(tl->tl_used))
5411 return 0;
5412
5413 tail_index = le16_to_cpu(tl->tl_used) - 1;
5414 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5415 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5416
5417 return current_tail == new_start;
5418}
5419
Mark Fasheh063c4562007-07-03 13:34:11 -07005420int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5421 handle_t *handle,
5422 u64 start_blk,
5423 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005424{
5425 int status, index;
5426 unsigned int start_cluster, tl_count;
5427 struct inode *tl_inode = osb->osb_tl_inode;
5428 struct buffer_head *tl_bh = osb->osb_tl_bh;
5429 struct ocfs2_dinode *di;
5430 struct ocfs2_truncate_log *tl;
5431
Mark Fashehb0697052006-03-03 10:24:33 -08005432 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5433 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005434
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005435 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005436
5437 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5438
5439 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005440
Joel Becker10995aa2008-11-13 14:49:12 -08005441 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5442 * by the underlying call to ocfs2_read_inode_block(), so any
5443 * corruption is a code bug */
5444 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5445
5446 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005447 tl_count = le16_to_cpu(tl->tl_count);
5448 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5449 tl_count == 0,
Mark Fashehb0697052006-03-03 10:24:33 -08005450 "Truncate record count on #%llu invalid "
5451 "wanted %u, actual %u\n",
5452 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005453 ocfs2_truncate_recs_per_inode(osb->sb),
5454 le16_to_cpu(tl->tl_count));
5455
5456 /* Caller should have known to flush before calling us. */
5457 index = le16_to_cpu(tl->tl_used);
5458 if (index >= tl_count) {
5459 status = -ENOSPC;
5460 mlog_errno(status);
5461 goto bail;
5462 }
5463
5464 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5465 OCFS2_JOURNAL_ACCESS_WRITE);
5466 if (status < 0) {
5467 mlog_errno(status);
5468 goto bail;
5469 }
5470
5471 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb0697052006-03-03 10:24:33 -08005472 "%llu (index = %d)\n", num_clusters, start_cluster,
5473 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005474
5475 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5476 /*
5477 * Move index back to the record we are coalescing with.
5478 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5479 */
5480 index--;
5481
5482 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5483 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5484 index, le32_to_cpu(tl->tl_recs[index].t_start),
5485 num_clusters);
5486 } else {
5487 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5488 tl->tl_used = cpu_to_le16(index + 1);
5489 }
5490 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5491
5492 status = ocfs2_journal_dirty(handle, tl_bh);
5493 if (status < 0) {
5494 mlog_errno(status);
5495 goto bail;
5496 }
5497
5498bail:
5499 mlog_exit(status);
5500 return status;
5501}
5502
5503static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005504 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005505 struct inode *data_alloc_inode,
5506 struct buffer_head *data_alloc_bh)
5507{
5508 int status = 0;
5509 int i;
5510 unsigned int num_clusters;
5511 u64 start_blk;
5512 struct ocfs2_truncate_rec rec;
5513 struct ocfs2_dinode *di;
5514 struct ocfs2_truncate_log *tl;
5515 struct inode *tl_inode = osb->osb_tl_inode;
5516 struct buffer_head *tl_bh = osb->osb_tl_bh;
5517
5518 mlog_entry_void();
5519
5520 di = (struct ocfs2_dinode *) tl_bh->b_data;
5521 tl = &di->id2.i_dealloc;
5522 i = le16_to_cpu(tl->tl_used) - 1;
5523 while (i >= 0) {
5524 /* Caller has given us at least enough credits to
5525 * update the truncate log dinode */
5526 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5527 OCFS2_JOURNAL_ACCESS_WRITE);
5528 if (status < 0) {
5529 mlog_errno(status);
5530 goto bail;
5531 }
5532
5533 tl->tl_used = cpu_to_le16(i);
5534
5535 status = ocfs2_journal_dirty(handle, tl_bh);
5536 if (status < 0) {
5537 mlog_errno(status);
5538 goto bail;
5539 }
5540
5541 /* TODO: Perhaps we can calculate the bulk of the
5542 * credits up front rather than extending like
5543 * this. */
5544 status = ocfs2_extend_trans(handle,
5545 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5546 if (status < 0) {
5547 mlog_errno(status);
5548 goto bail;
5549 }
5550
5551 rec = tl->tl_recs[i];
5552 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5553 le32_to_cpu(rec.t_start));
5554 num_clusters = le32_to_cpu(rec.t_clusters);
5555
5556 /* if start_blk is not set, we ignore the record as
5557 * invalid. */
5558 if (start_blk) {
5559 mlog(0, "free record %d, start = %u, clusters = %u\n",
5560 i, le32_to_cpu(rec.t_start), num_clusters);
5561
5562 status = ocfs2_free_clusters(handle, data_alloc_inode,
5563 data_alloc_bh, start_blk,
5564 num_clusters);
5565 if (status < 0) {
5566 mlog_errno(status);
5567 goto bail;
5568 }
5569 }
5570 i--;
5571 }
5572
5573bail:
5574 mlog_exit(status);
5575 return status;
5576}
5577
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005578/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005579int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005580{
5581 int status;
5582 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005583 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005584 struct inode *tl_inode = osb->osb_tl_inode;
5585 struct inode *data_alloc_inode = NULL;
5586 struct buffer_head *tl_bh = osb->osb_tl_bh;
5587 struct buffer_head *data_alloc_bh = NULL;
5588 struct ocfs2_dinode *di;
5589 struct ocfs2_truncate_log *tl;
5590
5591 mlog_entry_void();
5592
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005593 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005594
5595 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005596
Joel Becker10995aa2008-11-13 14:49:12 -08005597 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5598 * by the underlying call to ocfs2_read_inode_block(), so any
5599 * corruption is a code bug */
5600 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5601
5602 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005603 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb0697052006-03-03 10:24:33 -08005604 mlog(0, "Flush %u records from truncate log #%llu\n",
5605 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005606 if (!num_to_flush) {
5607 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005608 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005609 }
5610
5611 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5612 GLOBAL_BITMAP_SYSTEM_INODE,
5613 OCFS2_INVALID_SLOT);
5614 if (!data_alloc_inode) {
5615 status = -EINVAL;
5616 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005617 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005618 }
5619
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005620 mutex_lock(&data_alloc_inode->i_mutex);
5621
Mark Fashehe63aecb62007-10-18 15:30:42 -07005622 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005623 if (status < 0) {
5624 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005625 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005626 }
5627
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005628 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005629 if (IS_ERR(handle)) {
5630 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005631 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005632 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005633 }
5634
5635 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5636 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005637 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005638 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005639
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005640 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005641
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005642out_unlock:
5643 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005644 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005645
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005646out_mutex:
5647 mutex_unlock(&data_alloc_inode->i_mutex);
5648 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005649
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005650out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005651 mlog_exit(status);
5652 return status;
5653}
5654
5655int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5656{
5657 int status;
5658 struct inode *tl_inode = osb->osb_tl_inode;
5659
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005660 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005661 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005662 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005663
5664 return status;
5665}
5666
David Howellsc4028952006-11-22 14:57:56 +00005667static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005668{
5669 int status;
David Howellsc4028952006-11-22 14:57:56 +00005670 struct ocfs2_super *osb =
5671 container_of(work, struct ocfs2_super,
5672 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005673
5674 mlog_entry_void();
5675
5676 status = ocfs2_flush_truncate_log(osb);
5677 if (status < 0)
5678 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005679 else
5680 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005681
5682 mlog_exit(status);
5683}
5684
5685#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5686void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5687 int cancel)
5688{
5689 if (osb->osb_tl_inode) {
5690 /* We want to push off log flushes while truncates are
5691 * still running. */
5692 if (cancel)
5693 cancel_delayed_work(&osb->osb_truncate_log_wq);
5694
5695 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5696 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5697 }
5698}
5699
5700static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5701 int slot_num,
5702 struct inode **tl_inode,
5703 struct buffer_head **tl_bh)
5704{
5705 int status;
5706 struct inode *inode = NULL;
5707 struct buffer_head *bh = NULL;
5708
5709 inode = ocfs2_get_system_file_inode(osb,
5710 TRUNCATE_LOG_SYSTEM_INODE,
5711 slot_num);
5712 if (!inode) {
5713 status = -EINVAL;
5714 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5715 goto bail;
5716 }
5717
Joel Beckerb657c952008-11-13 14:49:11 -08005718 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005719 if (status < 0) {
5720 iput(inode);
5721 mlog_errno(status);
5722 goto bail;
5723 }
5724
5725 *tl_inode = inode;
5726 *tl_bh = bh;
5727bail:
5728 mlog_exit(status);
5729 return status;
5730}
5731
5732/* called during the 1st stage of node recovery. we stamp a clean
5733 * truncate log and pass back a copy for processing later. if the
5734 * truncate log does not require processing, a *tl_copy is set to
5735 * NULL. */
5736int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5737 int slot_num,
5738 struct ocfs2_dinode **tl_copy)
5739{
5740 int status;
5741 struct inode *tl_inode = NULL;
5742 struct buffer_head *tl_bh = NULL;
5743 struct ocfs2_dinode *di;
5744 struct ocfs2_truncate_log *tl;
5745
5746 *tl_copy = NULL;
5747
5748 mlog(0, "recover truncate log from slot %d\n", slot_num);
5749
5750 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5751 if (status < 0) {
5752 mlog_errno(status);
5753 goto bail;
5754 }
5755
5756 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005757
Joel Becker10995aa2008-11-13 14:49:12 -08005758 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5759 * validated by the underlying call to ocfs2_read_inode_block(),
5760 * so any corruption is a code bug */
5761 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5762
5763 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005764 if (le16_to_cpu(tl->tl_used)) {
5765 mlog(0, "We'll have %u logs to recover\n",
5766 le16_to_cpu(tl->tl_used));
5767
5768 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5769 if (!(*tl_copy)) {
5770 status = -ENOMEM;
5771 mlog_errno(status);
5772 goto bail;
5773 }
5774
5775 /* Assuming the write-out below goes well, this copy
5776 * will be passed back to recovery for processing. */
5777 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5778
5779 /* All we need to do to clear the truncate log is set
5780 * tl_used. */
5781 tl->tl_used = 0;
5782
5783 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5784 if (status < 0) {
5785 mlog_errno(status);
5786 goto bail;
5787 }
5788 }
5789
5790bail:
5791 if (tl_inode)
5792 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07005793 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005794
5795 if (status < 0 && (*tl_copy)) {
5796 kfree(*tl_copy);
5797 *tl_copy = NULL;
5798 }
5799
5800 mlog_exit(status);
5801 return status;
5802}
5803
5804int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5805 struct ocfs2_dinode *tl_copy)
5806{
5807 int status = 0;
5808 int i;
5809 unsigned int clusters, num_recs, start_cluster;
5810 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005811 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005812 struct inode *tl_inode = osb->osb_tl_inode;
5813 struct ocfs2_truncate_log *tl;
5814
5815 mlog_entry_void();
5816
5817 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5818 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5819 return -EINVAL;
5820 }
5821
5822 tl = &tl_copy->id2.i_dealloc;
5823 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb0697052006-03-03 10:24:33 -08005824 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07005825 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08005826
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005827 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005828 for(i = 0; i < num_recs; i++) {
5829 if (ocfs2_truncate_log_needs_flush(osb)) {
5830 status = __ocfs2_flush_truncate_log(osb);
5831 if (status < 0) {
5832 mlog_errno(status);
5833 goto bail_up;
5834 }
5835 }
5836
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005837 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005838 if (IS_ERR(handle)) {
5839 status = PTR_ERR(handle);
5840 mlog_errno(status);
5841 goto bail_up;
5842 }
5843
5844 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5845 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5846 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5847
5848 status = ocfs2_truncate_log_append(osb, handle,
5849 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005850 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005851 if (status < 0) {
5852 mlog_errno(status);
5853 goto bail_up;
5854 }
5855 }
5856
5857bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005858 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005859
5860 mlog_exit(status);
5861 return status;
5862}
5863
5864void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5865{
5866 int status;
5867 struct inode *tl_inode = osb->osb_tl_inode;
5868
5869 mlog_entry_void();
5870
5871 if (tl_inode) {
5872 cancel_delayed_work(&osb->osb_truncate_log_wq);
5873 flush_workqueue(ocfs2_wq);
5874
5875 status = ocfs2_flush_truncate_log(osb);
5876 if (status < 0)
5877 mlog_errno(status);
5878
5879 brelse(osb->osb_tl_bh);
5880 iput(osb->osb_tl_inode);
5881 }
5882
5883 mlog_exit_void();
5884}
5885
5886int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5887{
5888 int status;
5889 struct inode *tl_inode = NULL;
5890 struct buffer_head *tl_bh = NULL;
5891
5892 mlog_entry_void();
5893
5894 status = ocfs2_get_truncate_log_info(osb,
5895 osb->slot_num,
5896 &tl_inode,
5897 &tl_bh);
5898 if (status < 0)
5899 mlog_errno(status);
5900
5901 /* ocfs2_truncate_log_shutdown keys on the existence of
5902 * osb->osb_tl_inode so we don't set any of the osb variables
5903 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00005904 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5905 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08005906 osb->osb_tl_bh = tl_bh;
5907 osb->osb_tl_inode = tl_inode;
5908
5909 mlog_exit(status);
5910 return status;
5911}
5912
Mark Fasheh2b604352007-06-22 15:45:27 -07005913/*
5914 * Delayed de-allocation of suballocator blocks.
5915 *
5916 * Some sets of block de-allocations might involve multiple suballocator inodes.
5917 *
5918 * The locking for this can get extremely complicated, especially when
5919 * the suballocator inodes to delete from aren't known until deep
5920 * within an unrelated codepath.
5921 *
5922 * ocfs2_extent_block structures are a good example of this - an inode
5923 * btree could have been grown by any number of nodes each allocating
5924 * out of their own suballoc inode.
5925 *
5926 * These structures allow the delay of block de-allocation until a
5927 * later time, when locking of multiple cluster inodes won't cause
5928 * deadlock.
5929 */
5930
5931/*
Tao Ma2891d292008-11-12 08:26:58 +08005932 * Describe a single bit freed from a suballocator. For the block
5933 * suballocators, it represents one block. For the global cluster
5934 * allocator, it represents some clusters and free_bit indicates
5935 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07005936 */
5937struct ocfs2_cached_block_free {
5938 struct ocfs2_cached_block_free *free_next;
5939 u64 free_blk;
5940 unsigned int free_bit;
5941};
5942
5943struct ocfs2_per_slot_free_list {
5944 struct ocfs2_per_slot_free_list *f_next_suballocator;
5945 int f_inode_type;
5946 int f_slot;
5947 struct ocfs2_cached_block_free *f_first;
5948};
5949
Tao Ma2891d292008-11-12 08:26:58 +08005950static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
5951 int sysfile_type,
5952 int slot,
5953 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07005954{
5955 int ret;
5956 u64 bg_blkno;
5957 handle_t *handle;
5958 struct inode *inode;
5959 struct buffer_head *di_bh = NULL;
5960 struct ocfs2_cached_block_free *tmp;
5961
5962 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5963 if (!inode) {
5964 ret = -EINVAL;
5965 mlog_errno(ret);
5966 goto out;
5967 }
5968
5969 mutex_lock(&inode->i_mutex);
5970
Mark Fashehe63aecb62007-10-18 15:30:42 -07005971 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07005972 if (ret) {
5973 mlog_errno(ret);
5974 goto out_mutex;
5975 }
5976
5977 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5978 if (IS_ERR(handle)) {
5979 ret = PTR_ERR(handle);
5980 mlog_errno(ret);
5981 goto out_unlock;
5982 }
5983
5984 while (head) {
5985 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5986 head->free_bit);
5987 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5988 head->free_bit, (unsigned long long)head->free_blk);
5989
5990 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5991 head->free_bit, bg_blkno, 1);
5992 if (ret) {
5993 mlog_errno(ret);
5994 goto out_journal;
5995 }
5996
5997 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
5998 if (ret) {
5999 mlog_errno(ret);
6000 goto out_journal;
6001 }
6002
6003 tmp = head;
6004 head = head->free_next;
6005 kfree(tmp);
6006 }
6007
6008out_journal:
6009 ocfs2_commit_trans(osb, handle);
6010
6011out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006012 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006013 brelse(di_bh);
6014out_mutex:
6015 mutex_unlock(&inode->i_mutex);
6016 iput(inode);
6017out:
6018 while(head) {
6019 /* Premature exit may have left some dangling items. */
6020 tmp = head;
6021 head = head->free_next;
6022 kfree(tmp);
6023 }
6024
6025 return ret;
6026}
6027
Tao Ma2891d292008-11-12 08:26:58 +08006028int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6029 u64 blkno, unsigned int bit)
6030{
6031 int ret = 0;
6032 struct ocfs2_cached_block_free *item;
6033
6034 item = kmalloc(sizeof(*item), GFP_NOFS);
6035 if (item == NULL) {
6036 ret = -ENOMEM;
6037 mlog_errno(ret);
6038 return ret;
6039 }
6040
6041 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6042 bit, (unsigned long long)blkno);
6043
6044 item->free_blk = blkno;
6045 item->free_bit = bit;
6046 item->free_next = ctxt->c_global_allocator;
6047
6048 ctxt->c_global_allocator = item;
6049 return ret;
6050}
6051
6052static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6053 struct ocfs2_cached_block_free *head)
6054{
6055 struct ocfs2_cached_block_free *tmp;
6056 struct inode *tl_inode = osb->osb_tl_inode;
6057 handle_t *handle;
6058 int ret = 0;
6059
6060 mutex_lock(&tl_inode->i_mutex);
6061
6062 while (head) {
6063 if (ocfs2_truncate_log_needs_flush(osb)) {
6064 ret = __ocfs2_flush_truncate_log(osb);
6065 if (ret < 0) {
6066 mlog_errno(ret);
6067 break;
6068 }
6069 }
6070
6071 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6072 if (IS_ERR(handle)) {
6073 ret = PTR_ERR(handle);
6074 mlog_errno(ret);
6075 break;
6076 }
6077
6078 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6079 head->free_bit);
6080
6081 ocfs2_commit_trans(osb, handle);
6082 tmp = head;
6083 head = head->free_next;
6084 kfree(tmp);
6085
6086 if (ret < 0) {
6087 mlog_errno(ret);
6088 break;
6089 }
6090 }
6091
6092 mutex_unlock(&tl_inode->i_mutex);
6093
6094 while (head) {
6095 /* Premature exit may have left some dangling items. */
6096 tmp = head;
6097 head = head->free_next;
6098 kfree(tmp);
6099 }
6100
6101 return ret;
6102}
6103
Mark Fasheh2b604352007-06-22 15:45:27 -07006104int ocfs2_run_deallocs(struct ocfs2_super *osb,
6105 struct ocfs2_cached_dealloc_ctxt *ctxt)
6106{
6107 int ret = 0, ret2;
6108 struct ocfs2_per_slot_free_list *fl;
6109
6110 if (!ctxt)
6111 return 0;
6112
6113 while (ctxt->c_first_suballocator) {
6114 fl = ctxt->c_first_suballocator;
6115
6116 if (fl->f_first) {
6117 mlog(0, "Free items: (type %u, slot %d)\n",
6118 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006119 ret2 = ocfs2_free_cached_blocks(osb,
6120 fl->f_inode_type,
6121 fl->f_slot,
6122 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006123 if (ret2)
6124 mlog_errno(ret2);
6125 if (!ret)
6126 ret = ret2;
6127 }
6128
6129 ctxt->c_first_suballocator = fl->f_next_suballocator;
6130 kfree(fl);
6131 }
6132
Tao Ma2891d292008-11-12 08:26:58 +08006133 if (ctxt->c_global_allocator) {
6134 ret2 = ocfs2_free_cached_clusters(osb,
6135 ctxt->c_global_allocator);
6136 if (ret2)
6137 mlog_errno(ret2);
6138 if (!ret)
6139 ret = ret2;
6140
6141 ctxt->c_global_allocator = NULL;
6142 }
6143
Mark Fasheh2b604352007-06-22 15:45:27 -07006144 return ret;
6145}
6146
6147static struct ocfs2_per_slot_free_list *
6148ocfs2_find_per_slot_free_list(int type,
6149 int slot,
6150 struct ocfs2_cached_dealloc_ctxt *ctxt)
6151{
6152 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6153
6154 while (fl) {
6155 if (fl->f_inode_type == type && fl->f_slot == slot)
6156 return fl;
6157
6158 fl = fl->f_next_suballocator;
6159 }
6160
6161 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6162 if (fl) {
6163 fl->f_inode_type = type;
6164 fl->f_slot = slot;
6165 fl->f_first = NULL;
6166 fl->f_next_suballocator = ctxt->c_first_suballocator;
6167
6168 ctxt->c_first_suballocator = fl;
6169 }
6170 return fl;
6171}
6172
6173static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6174 int type, int slot, u64 blkno,
6175 unsigned int bit)
6176{
6177 int ret;
6178 struct ocfs2_per_slot_free_list *fl;
6179 struct ocfs2_cached_block_free *item;
6180
6181 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6182 if (fl == NULL) {
6183 ret = -ENOMEM;
6184 mlog_errno(ret);
6185 goto out;
6186 }
6187
6188 item = kmalloc(sizeof(*item), GFP_NOFS);
6189 if (item == NULL) {
6190 ret = -ENOMEM;
6191 mlog_errno(ret);
6192 goto out;
6193 }
6194
6195 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6196 type, slot, bit, (unsigned long long)blkno);
6197
6198 item->free_blk = blkno;
6199 item->free_bit = bit;
6200 item->free_next = fl->f_first;
6201
6202 fl->f_first = item;
6203
6204 ret = 0;
6205out:
6206 return ret;
6207}
6208
Mark Fasheh59a5e412007-06-22 15:52:36 -07006209static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6210 struct ocfs2_extent_block *eb)
6211{
6212 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6213 le16_to_cpu(eb->h_suballoc_slot),
6214 le64_to_cpu(eb->h_blkno),
6215 le16_to_cpu(eb->h_suballoc_bit));
6216}
6217
Mark Fashehccd979b2005-12-15 14:31:24 -08006218/* This function will figure out whether the currently last extent
6219 * block will be deleted, and if it will, what the new last extent
6220 * block will be so we can update his h_next_leaf_blk field, as well
6221 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006222static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006223 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006224 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006225 struct buffer_head **new_last_eb)
6226{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006227 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006228 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006229 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006230 struct ocfs2_extent_block *eb;
6231 struct ocfs2_extent_list *el;
6232 struct buffer_head *bh = NULL;
6233
6234 *new_last_eb = NULL;
6235
Mark Fashehccd979b2005-12-15 14:31:24 -08006236 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006237 if (!path->p_tree_depth)
6238 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006239
6240 /* trunc to zero special case - this makes tree_depth = 0
6241 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006242 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006243 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006244
Mark Fashehdcd05382007-01-16 11:32:23 -08006245 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006246 BUG_ON(!el->l_next_free_rec);
6247
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006248 /*
6249 * Make sure that this extent list will actually be empty
6250 * after we clear away the data. We can shortcut out if
6251 * there's more than one non-empty extent in the
6252 * list. Otherwise, a check of the remaining extent is
6253 * necessary.
6254 */
6255 next_free = le16_to_cpu(el->l_next_free_rec);
6256 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006257 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006258 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006259 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006260
6261 /* We may have a valid extent in index 1, check it. */
6262 if (next_free == 2)
6263 rec = &el->l_recs[1];
6264
6265 /*
6266 * Fall through - no more nonempty extents, so we want
6267 * to delete this leaf.
6268 */
6269 } else {
6270 if (next_free > 1)
6271 goto out;
6272
6273 rec = &el->l_recs[0];
6274 }
6275
6276 if (rec) {
6277 /*
6278 * Check it we'll only be trimming off the end of this
6279 * cluster.
6280 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006281 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006282 goto out;
6283 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006284
Mark Fashehdcd05382007-01-16 11:32:23 -08006285 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6286 if (ret) {
6287 mlog_errno(ret);
6288 goto out;
6289 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006290
Mark Fashehdcd05382007-01-16 11:32:23 -08006291 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6292 if (ret) {
6293 mlog_errno(ret);
6294 goto out;
6295 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006296
Mark Fashehdcd05382007-01-16 11:32:23 -08006297 eb = (struct ocfs2_extent_block *) bh->b_data;
6298 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006299
6300 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6301 * Any corruption is a code bug. */
6302 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006303
6304 *new_last_eb = bh;
6305 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006306 mlog(0, "returning block %llu, (cpos: %u)\n",
6307 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6308out:
6309 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006310
Mark Fashehdcd05382007-01-16 11:32:23 -08006311 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006312}
6313
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006314/*
6315 * Trim some clusters off the rightmost edge of a tree. Only called
6316 * during truncate.
6317 *
6318 * The caller needs to:
6319 * - start journaling of each path component.
6320 * - compute and fully set up any new last ext block
6321 */
6322static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6323 handle_t *handle, struct ocfs2_truncate_context *tc,
6324 u32 clusters_to_del, u64 *delete_start)
6325{
6326 int ret, i, index = path->p_tree_depth;
6327 u32 new_edge = 0;
6328 u64 deleted_eb = 0;
6329 struct buffer_head *bh;
6330 struct ocfs2_extent_list *el;
6331 struct ocfs2_extent_rec *rec;
6332
6333 *delete_start = 0;
6334
6335 while (index >= 0) {
6336 bh = path->p_node[index].bh;
6337 el = path->p_node[index].el;
6338
6339 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6340 index, (unsigned long long)bh->b_blocknr);
6341
6342 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6343
6344 if (index !=
6345 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6346 ocfs2_error(inode->i_sb,
6347 "Inode %lu has invalid ext. block %llu",
6348 inode->i_ino,
6349 (unsigned long long)bh->b_blocknr);
6350 ret = -EROFS;
6351 goto out;
6352 }
6353
6354find_tail_record:
6355 i = le16_to_cpu(el->l_next_free_rec) - 1;
6356 rec = &el->l_recs[i];
6357
6358 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6359 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006360 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006361 (unsigned long long)le64_to_cpu(rec->e_blkno),
6362 le16_to_cpu(el->l_next_free_rec));
6363
Mark Fashehe48edee2007-03-07 16:46:57 -08006364 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006365
6366 if (le16_to_cpu(el->l_tree_depth) == 0) {
6367 /*
6368 * If the leaf block contains a single empty
6369 * extent and no records, we can just remove
6370 * the block.
6371 */
6372 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6373 memset(rec, 0,
6374 sizeof(struct ocfs2_extent_rec));
6375 el->l_next_free_rec = cpu_to_le16(0);
6376
6377 goto delete;
6378 }
6379
6380 /*
6381 * Remove any empty extents by shifting things
6382 * left. That should make life much easier on
6383 * the code below. This condition is rare
6384 * enough that we shouldn't see a performance
6385 * hit.
6386 */
6387 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6388 le16_add_cpu(&el->l_next_free_rec, -1);
6389
6390 for(i = 0;
6391 i < le16_to_cpu(el->l_next_free_rec); i++)
6392 el->l_recs[i] = el->l_recs[i + 1];
6393
6394 memset(&el->l_recs[i], 0,
6395 sizeof(struct ocfs2_extent_rec));
6396
6397 /*
6398 * We've modified our extent list. The
6399 * simplest way to handle this change
6400 * is to being the search from the
6401 * start again.
6402 */
6403 goto find_tail_record;
6404 }
6405
Mark Fashehe48edee2007-03-07 16:46:57 -08006406 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006407
6408 /*
6409 * We'll use "new_edge" on our way back up the
6410 * tree to know what our rightmost cpos is.
6411 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006412 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006413 new_edge += le32_to_cpu(rec->e_cpos);
6414
6415 /*
6416 * The caller will use this to delete data blocks.
6417 */
6418 *delete_start = le64_to_cpu(rec->e_blkno)
6419 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006420 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006421
6422 /*
6423 * If it's now empty, remove this record.
6424 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006425 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006426 memset(rec, 0,
6427 sizeof(struct ocfs2_extent_rec));
6428 le16_add_cpu(&el->l_next_free_rec, -1);
6429 }
6430 } else {
6431 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6432 memset(rec, 0,
6433 sizeof(struct ocfs2_extent_rec));
6434 le16_add_cpu(&el->l_next_free_rec, -1);
6435
6436 goto delete;
6437 }
6438
6439 /* Can this actually happen? */
6440 if (le16_to_cpu(el->l_next_free_rec) == 0)
6441 goto delete;
6442
6443 /*
6444 * We never actually deleted any clusters
6445 * because our leaf was empty. There's no
6446 * reason to adjust the rightmost edge then.
6447 */
6448 if (new_edge == 0)
6449 goto delete;
6450
Mark Fashehe48edee2007-03-07 16:46:57 -08006451 rec->e_int_clusters = cpu_to_le32(new_edge);
6452 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006453 -le32_to_cpu(rec->e_cpos));
6454
6455 /*
6456 * A deleted child record should have been
6457 * caught above.
6458 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006459 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006460 }
6461
6462delete:
6463 ret = ocfs2_journal_dirty(handle, bh);
6464 if (ret) {
6465 mlog_errno(ret);
6466 goto out;
6467 }
6468
6469 mlog(0, "extent list container %llu, after: record %d: "
6470 "(%u, %u, %llu), next = %u.\n",
6471 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006472 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006473 (unsigned long long)le64_to_cpu(rec->e_blkno),
6474 le16_to_cpu(el->l_next_free_rec));
6475
6476 /*
6477 * We must be careful to only attempt delete of an
6478 * extent block (and not the root inode block).
6479 */
6480 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6481 struct ocfs2_extent_block *eb =
6482 (struct ocfs2_extent_block *)bh->b_data;
6483
6484 /*
6485 * Save this for use when processing the
6486 * parent block.
6487 */
6488 deleted_eb = le64_to_cpu(eb->h_blkno);
6489
6490 mlog(0, "deleting this extent block.\n");
6491
6492 ocfs2_remove_from_cache(inode, bh);
6493
Mark Fashehe48edee2007-03-07 16:46:57 -08006494 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006495 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6496 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6497
Mark Fasheh59a5e412007-06-22 15:52:36 -07006498 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6499 /* An error here is not fatal. */
6500 if (ret < 0)
6501 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006502 } else {
6503 deleted_eb = 0;
6504 }
6505
6506 index--;
6507 }
6508
6509 ret = 0;
6510out:
6511 return ret;
6512}
6513
Mark Fashehccd979b2005-12-15 14:31:24 -08006514static int ocfs2_do_truncate(struct ocfs2_super *osb,
6515 unsigned int clusters_to_del,
6516 struct inode *inode,
6517 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006518 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006519 struct ocfs2_truncate_context *tc,
6520 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006521{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006522 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006523 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006524 struct ocfs2_extent_block *last_eb = NULL;
6525 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006526 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006527 u64 delete_blk = 0;
6528
6529 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6530
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006531 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006532 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006533 if (status < 0) {
6534 mlog_errno(status);
6535 goto bail;
6536 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006537
Mark Fashehdcd05382007-01-16 11:32:23 -08006538 /*
6539 * Each component will be touched, so we might as well journal
6540 * here to avoid having to handle errors later.
6541 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006542 status = ocfs2_journal_access_path(inode, handle, path);
6543 if (status < 0) {
6544 mlog_errno(status);
6545 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006546 }
6547
6548 if (last_eb_bh) {
6549 status = ocfs2_journal_access(handle, inode, last_eb_bh,
6550 OCFS2_JOURNAL_ACCESS_WRITE);
6551 if (status < 0) {
6552 mlog_errno(status);
6553 goto bail;
6554 }
6555
6556 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6557 }
6558
6559 el = &(fe->id2.i_list);
6560
6561 /*
6562 * Lower levels depend on this never happening, but it's best
6563 * to check it up here before changing the tree.
6564 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006565 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006566 ocfs2_error(inode->i_sb,
6567 "Inode %lu has an empty extent record, depth %u\n",
6568 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006569 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006570 goto bail;
6571 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006572
Jan Karaa90714c2008-10-09 19:38:40 +02006573 vfs_dq_free_space_nodirty(inode,
6574 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006575 spin_lock(&OCFS2_I(inode)->ip_lock);
6576 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6577 clusters_to_del;
6578 spin_unlock(&OCFS2_I(inode)->ip_lock);
6579 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006580 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006581
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006582 status = ocfs2_trim_tree(inode, path, handle, tc,
6583 clusters_to_del, &delete_blk);
6584 if (status) {
6585 mlog_errno(status);
6586 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006587 }
6588
Mark Fashehdcd05382007-01-16 11:32:23 -08006589 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006590 /* trunc to zero is a special case. */
6591 el->l_tree_depth = 0;
6592 fe->i_last_eb_blk = 0;
6593 } else if (last_eb)
6594 fe->i_last_eb_blk = last_eb->h_blkno;
6595
6596 status = ocfs2_journal_dirty(handle, fe_bh);
6597 if (status < 0) {
6598 mlog_errno(status);
6599 goto bail;
6600 }
6601
6602 if (last_eb) {
6603 /* If there will be a new last extent block, then by
6604 * definition, there cannot be any leaves to the right of
6605 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006606 last_eb->h_next_leaf_blk = 0;
6607 status = ocfs2_journal_dirty(handle, last_eb_bh);
6608 if (status < 0) {
6609 mlog_errno(status);
6610 goto bail;
6611 }
6612 }
6613
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006614 if (delete_blk) {
6615 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6616 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006617 if (status < 0) {
6618 mlog_errno(status);
6619 goto bail;
6620 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006621 }
6622 status = 0;
6623bail:
Mark Fashehdcd05382007-01-16 11:32:23 -08006624
Mark Fashehccd979b2005-12-15 14:31:24 -08006625 mlog_exit(status);
6626 return status;
6627}
6628
Joel Becker2b4e30f2008-09-03 20:03:41 -07006629static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006630{
6631 set_buffer_uptodate(bh);
6632 mark_buffer_dirty(bh);
6633 return 0;
6634}
6635
Mark Fasheh1d410a62007-09-07 14:20:45 -07006636static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6637 unsigned int from, unsigned int to,
6638 struct page *page, int zero, u64 *phys)
6639{
6640 int ret, partial = 0;
6641
6642 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6643 if (ret)
6644 mlog_errno(ret);
6645
6646 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006647 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006648
6649 /*
6650 * Need to set the buffers we zero'd into uptodate
6651 * here if they aren't - ocfs2_map_page_blocks()
6652 * might've skipped some
6653 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006654 ret = walk_page_buffers(handle, page_buffers(page),
6655 from, to, &partial,
6656 ocfs2_zero_func);
6657 if (ret < 0)
6658 mlog_errno(ret);
6659 else if (ocfs2_should_order_data(inode)) {
6660 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006661 if (ret < 0)
6662 mlog_errno(ret);
6663 }
6664
6665 if (!partial)
6666 SetPageUptodate(page);
6667
6668 flush_dcache_page(page);
6669}
6670
Mark Fasheh35edec12007-07-06 14:41:18 -07006671static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6672 loff_t end, struct page **pages,
6673 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006674{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006675 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006676 struct page *page;
6677 unsigned int from, to = PAGE_CACHE_SIZE;
6678 struct super_block *sb = inode->i_sb;
6679
6680 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6681
6682 if (numpages == 0)
6683 goto out;
6684
Mark Fasheh35edec12007-07-06 14:41:18 -07006685 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006686 for(i = 0; i < numpages; i++) {
6687 page = pages[i];
6688
Mark Fasheh35edec12007-07-06 14:41:18 -07006689 from = start & (PAGE_CACHE_SIZE - 1);
6690 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6691 to = end & (PAGE_CACHE_SIZE - 1);
6692
Mark Fasheh60b11392007-02-16 11:46:50 -08006693 BUG_ON(from > PAGE_CACHE_SIZE);
6694 BUG_ON(to > PAGE_CACHE_SIZE);
6695
Mark Fasheh1d410a62007-09-07 14:20:45 -07006696 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6697 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006698
Mark Fasheh35edec12007-07-06 14:41:18 -07006699 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006700 }
6701out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006702 if (pages)
6703 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006704}
6705
Mark Fasheh35edec12007-07-06 14:41:18 -07006706static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006707 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006708{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006709 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006710 struct super_block *sb = inode->i_sb;
6711 struct address_space *mapping = inode->i_mapping;
6712 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006713 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006714
Mark Fasheh35edec12007-07-06 14:41:18 -07006715 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006716
Mark Fasheh35edec12007-07-06 14:41:18 -07006717 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6718 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6719
Mark Fasheh1d410a62007-09-07 14:20:45 -07006720 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006721 last_page_bytes = PAGE_ALIGN(end);
6722 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006723 do {
6724 pages[numpages] = grab_cache_page(mapping, index);
6725 if (!pages[numpages]) {
6726 ret = -ENOMEM;
6727 mlog_errno(ret);
6728 goto out;
6729 }
6730
6731 numpages++;
6732 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006733 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006734
6735out:
6736 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006737 if (pages)
6738 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006739 numpages = 0;
6740 }
6741
6742 *num = numpages;
6743
6744 return ret;
6745}
6746
6747/*
6748 * Zero the area past i_size but still within an allocated
6749 * cluster. This avoids exposing nonzero data on subsequent file
6750 * extends.
6751 *
6752 * We need to call this before i_size is updated on the inode because
6753 * otherwise block_write_full_page() will skip writeout of pages past
6754 * i_size. The new_i_size parameter is passed for this reason.
6755 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006756int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6757 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006758{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006759 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006760 struct page **pages = NULL;
6761 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006762 unsigned int ext_flags;
6763 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006764
6765 /*
6766 * File systems which don't support sparse files zero on every
6767 * extend.
6768 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07006769 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08006770 return 0;
6771
Mark Fasheh1d410a62007-09-07 14:20:45 -07006772 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08006773 sizeof(struct page *), GFP_NOFS);
6774 if (pages == NULL) {
6775 ret = -ENOMEM;
6776 mlog_errno(ret);
6777 goto out;
6778 }
6779
Mark Fasheh1d410a62007-09-07 14:20:45 -07006780 if (range_start == range_end)
6781 goto out;
6782
6783 ret = ocfs2_extent_map_get_blocks(inode,
6784 range_start >> sb->s_blocksize_bits,
6785 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08006786 if (ret) {
6787 mlog_errno(ret);
6788 goto out;
6789 }
6790
Mark Fasheh1d410a62007-09-07 14:20:45 -07006791 /*
6792 * Tail is a hole, or is marked unwritten. In either case, we
6793 * can count on read and write to return/push zero's.
6794 */
6795 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08006796 goto out;
6797
Mark Fasheh1d410a62007-09-07 14:20:45 -07006798 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6799 &numpages);
6800 if (ret) {
6801 mlog_errno(ret);
6802 goto out;
6803 }
6804
Mark Fasheh35edec12007-07-06 14:41:18 -07006805 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6806 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08006807
6808 /*
6809 * Initiate writeout of the pages we zero'd here. We don't
6810 * wait on them - the truncate_inode_pages() call later will
6811 * do that for us.
6812 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006813 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6814 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08006815 if (ret)
6816 mlog_errno(ret);
6817
6818out:
6819 if (pages)
6820 kfree(pages);
6821
6822 return ret;
6823}
6824
Tiger Yangfdd77702008-08-18 17:08:55 +08006825static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6826 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006827{
6828 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08006829 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006830
Tiger Yangfdd77702008-08-18 17:08:55 +08006831 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6832 memset(&di->id2, 0, blocksize -
6833 offsetof(struct ocfs2_dinode, id2) -
6834 xattrsize);
6835 else
6836 memset(&di->id2, 0, blocksize -
6837 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006838}
6839
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006840void ocfs2_dinode_new_extent_list(struct inode *inode,
6841 struct ocfs2_dinode *di)
6842{
Tiger Yangfdd77702008-08-18 17:08:55 +08006843 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006844 di->id2.i_list.l_tree_depth = 0;
6845 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08006846 di->id2.i_list.l_count = cpu_to_le16(
6847 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006848}
6849
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006850void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6851{
6852 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6853 struct ocfs2_inline_data *idata = &di->id2.i_data;
6854
6855 spin_lock(&oi->ip_lock);
6856 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6857 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6858 spin_unlock(&oi->ip_lock);
6859
6860 /*
6861 * We clear the entire i_data structure here so that all
6862 * fields can be properly initialized.
6863 */
Tiger Yangfdd77702008-08-18 17:08:55 +08006864 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006865
Tiger Yangfdd77702008-08-18 17:08:55 +08006866 idata->id_count = cpu_to_le16(
6867 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006868}
6869
6870int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6871 struct buffer_head *di_bh)
6872{
6873 int ret, i, has_data, num_pages = 0;
6874 handle_t *handle;
6875 u64 uninitialized_var(block);
6876 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6877 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6878 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006879 struct ocfs2_alloc_context *data_ac = NULL;
6880 struct page **pages = NULL;
6881 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07006882 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02006883 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006884
6885 has_data = i_size_read(inode) ? 1 : 0;
6886
6887 if (has_data) {
6888 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6889 sizeof(struct page *), GFP_NOFS);
6890 if (pages == NULL) {
6891 ret = -ENOMEM;
6892 mlog_errno(ret);
6893 goto out;
6894 }
6895
6896 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6897 if (ret) {
6898 mlog_errno(ret);
6899 goto out;
6900 }
6901 }
6902
Jan Karaa90714c2008-10-09 19:38:40 +02006903 handle = ocfs2_start_trans(osb,
6904 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006905 if (IS_ERR(handle)) {
6906 ret = PTR_ERR(handle);
6907 mlog_errno(ret);
6908 goto out_unlock;
6909 }
6910
6911 ret = ocfs2_journal_access(handle, inode, di_bh,
6912 OCFS2_JOURNAL_ACCESS_WRITE);
6913 if (ret) {
6914 mlog_errno(ret);
6915 goto out_commit;
6916 }
6917
6918 if (has_data) {
6919 u32 bit_off, num;
6920 unsigned int page_end;
6921 u64 phys;
6922
Jan Karaa90714c2008-10-09 19:38:40 +02006923 if (vfs_dq_alloc_space_nodirty(inode,
6924 ocfs2_clusters_to_bytes(osb->sb, 1))) {
6925 ret = -EDQUOT;
6926 goto out_commit;
6927 }
6928 did_quota = 1;
6929
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006930 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
6931 &num);
6932 if (ret) {
6933 mlog_errno(ret);
6934 goto out_commit;
6935 }
6936
6937 /*
6938 * Save two copies, one for insert, and one that can
6939 * be changed by ocfs2_map_and_dirty_page() below.
6940 */
6941 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6942
6943 /*
6944 * Non sparse file systems zero on extend, so no need
6945 * to do that now.
6946 */
6947 if (!ocfs2_sparse_alloc(osb) &&
6948 PAGE_CACHE_SIZE < osb->s_clustersize)
6949 end = PAGE_CACHE_SIZE;
6950
6951 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6952 if (ret) {
6953 mlog_errno(ret);
6954 goto out_commit;
6955 }
6956
6957 /*
6958 * This should populate the 1st page for us and mark
6959 * it up to date.
6960 */
6961 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6962 if (ret) {
6963 mlog_errno(ret);
6964 goto out_commit;
6965 }
6966
6967 page_end = PAGE_CACHE_SIZE;
6968 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6969 page_end = osb->s_clustersize;
6970
6971 for (i = 0; i < num_pages; i++)
6972 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6973 pages[i], i > 0, &phys);
6974 }
6975
6976 spin_lock(&oi->ip_lock);
6977 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6978 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6979 spin_unlock(&oi->ip_lock);
6980
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006981 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006982
6983 ocfs2_journal_dirty(handle, di_bh);
6984
6985 if (has_data) {
6986 /*
6987 * An error at this point should be extremely rare. If
6988 * this proves to be false, we could always re-build
6989 * the in-inode data from our pages.
6990 */
Joel Becker8d6220d2008-08-22 12:46:09 -07006991 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07006992 ret = ocfs2_insert_extent(osb, handle, inode, &et,
6993 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006994 if (ret) {
6995 mlog_errno(ret);
6996 goto out_commit;
6997 }
6998
6999 inode->i_blocks = ocfs2_inode_sector_count(inode);
7000 }
7001
7002out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007003 if (ret < 0 && did_quota)
7004 vfs_dq_free_space_nodirty(inode,
7005 ocfs2_clusters_to_bytes(osb->sb, 1));
7006
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007007 ocfs2_commit_trans(osb, handle);
7008
7009out_unlock:
7010 if (data_ac)
7011 ocfs2_free_alloc_context(data_ac);
7012
7013out:
7014 if (pages) {
7015 ocfs2_unlock_and_free_pages(pages, num_pages);
7016 kfree(pages);
7017 }
7018
7019 return ret;
7020}
7021
Mark Fashehccd979b2005-12-15 14:31:24 -08007022/*
7023 * It is expected, that by the time you call this function,
7024 * inode->i_size and fe->i_size have been adjusted.
7025 *
7026 * WARNING: This will kfree the truncate context
7027 */
7028int ocfs2_commit_truncate(struct ocfs2_super *osb,
7029 struct inode *inode,
7030 struct buffer_head *fe_bh,
7031 struct ocfs2_truncate_context *tc)
7032{
7033 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08007034 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08007035 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07007036 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007037 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08007038 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08007039 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007040
7041 mlog_entry_void();
7042
Mark Fashehdcd05382007-01-16 11:32:23 -08007043 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007044 i_size_read(inode));
7045
Tao Mae7d4cb62008-08-18 17:38:44 +08007046 path = ocfs2_new_path(fe_bh, &di->id2.i_list);
Mark Fashehdcd05382007-01-16 11:32:23 -08007047 if (!path) {
7048 status = -ENOMEM;
7049 mlog_errno(status);
7050 goto bail;
7051 }
Mark Fasheh83418972007-04-23 18:53:12 -07007052
7053 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7054
Mark Fashehccd979b2005-12-15 14:31:24 -08007055start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007056 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007057 * Check that we still have allocation to delete.
7058 */
7059 if (OCFS2_I(inode)->ip_clusters == 0) {
7060 status = 0;
7061 goto bail;
7062 }
7063
7064 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007065 * Truncate always works against the rightmost tree branch.
7066 */
7067 status = ocfs2_find_path(inode, path, UINT_MAX);
7068 if (status) {
7069 mlog_errno(status);
7070 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007071 }
7072
Mark Fashehdcd05382007-01-16 11:32:23 -08007073 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7074 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7075
7076 /*
7077 * By now, el will point to the extent list on the bottom most
7078 * portion of this tree. Only the tail record is considered in
7079 * each pass.
7080 *
7081 * We handle the following cases, in order:
7082 * - empty extent: delete the remaining branch
7083 * - remove the entire record
7084 * - remove a partial record
7085 * - no record needs to be removed (truncate has completed)
7086 */
7087 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007088 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7089 ocfs2_error(inode->i_sb,
7090 "Inode %llu has empty extent block at %llu\n",
7091 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7092 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7093 status = -EROFS;
7094 goto bail;
7095 }
7096
Mark Fashehccd979b2005-12-15 14:31:24 -08007097 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08007098 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08007099 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007100 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7101 clusters_to_del = 0;
7102 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007103 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007104 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007105 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08007106 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08007107 new_highest_cpos;
7108 } else {
7109 status = 0;
7110 goto bail;
7111 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007112
Mark Fashehdcd05382007-01-16 11:32:23 -08007113 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7114 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7115
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007116 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007117 tl_sem = 1;
7118 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7119 * record is free for use. If there isn't any, we flush to get
7120 * an empty truncate log. */
7121 if (ocfs2_truncate_log_needs_flush(osb)) {
7122 status = __ocfs2_flush_truncate_log(osb);
7123 if (status < 0) {
7124 mlog_errno(status);
7125 goto bail;
7126 }
7127 }
7128
7129 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007130 (struct ocfs2_dinode *)fe_bh->b_data,
7131 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007132 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007133 if (IS_ERR(handle)) {
7134 status = PTR_ERR(handle);
7135 handle = NULL;
7136 mlog_errno(status);
7137 goto bail;
7138 }
7139
Mark Fashehdcd05382007-01-16 11:32:23 -08007140 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7141 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007142 if (status < 0) {
7143 mlog_errno(status);
7144 goto bail;
7145 }
7146
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007147 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007148 tl_sem = 0;
7149
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007150 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007151 handle = NULL;
7152
Mark Fashehdcd05382007-01-16 11:32:23 -08007153 ocfs2_reinit_path(path, 1);
7154
7155 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007156 * The check above will catch the case where we've truncated
7157 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007158 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007159 goto start;
7160
Mark Fashehccd979b2005-12-15 14:31:24 -08007161bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007162
7163 ocfs2_schedule_truncate_log_flush(osb, 1);
7164
7165 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007166 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007167
7168 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007169 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007170
Mark Fasheh59a5e412007-06-22 15:52:36 -07007171 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7172
Mark Fashehdcd05382007-01-16 11:32:23 -08007173 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007174
7175 /* This will drop the ext_alloc cluster lock for us */
7176 ocfs2_free_truncate_context(tc);
7177
7178 mlog_exit(status);
7179 return status;
7180}
7181
Mark Fashehccd979b2005-12-15 14:31:24 -08007182/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007183 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007184 */
7185int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7186 struct inode *inode,
7187 struct buffer_head *fe_bh,
7188 struct ocfs2_truncate_context **tc)
7189{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007190 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007191 unsigned int new_i_clusters;
7192 struct ocfs2_dinode *fe;
7193 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007194 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007195
7196 mlog_entry_void();
7197
7198 *tc = NULL;
7199
7200 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7201 i_size_read(inode));
7202 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7203
7204 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007205 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7206 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007207
Robert P. J. Daycd861282006-12-13 00:34:52 -08007208 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007209 if (!(*tc)) {
7210 status = -ENOMEM;
7211 mlog_errno(status);
7212 goto bail;
7213 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007214 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007215
Mark Fashehccd979b2005-12-15 14:31:24 -08007216 if (fe->id2.i_list.l_tree_depth) {
Joel Becker5e965812008-11-13 14:49:16 -08007217 status = ocfs2_read_extent_block(inode,
7218 le64_to_cpu(fe->i_last_eb_blk),
7219 &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007220 if (status < 0) {
7221 mlog_errno(status);
7222 goto bail;
7223 }
7224 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007225 }
7226
7227 (*tc)->tc_last_eb_bh = last_eb_bh;
7228
Mark Fashehccd979b2005-12-15 14:31:24 -08007229 status = 0;
7230bail:
7231 if (status < 0) {
7232 if (*tc)
7233 ocfs2_free_truncate_context(*tc);
7234 *tc = NULL;
7235 }
7236 mlog_exit_void();
7237 return status;
7238}
7239
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007240/*
7241 * 'start' is inclusive, 'end' is not.
7242 */
7243int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7244 unsigned int start, unsigned int end, int trunc)
7245{
7246 int ret;
7247 unsigned int numbytes;
7248 handle_t *handle;
7249 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7250 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7251 struct ocfs2_inline_data *idata = &di->id2.i_data;
7252
7253 if (end > i_size_read(inode))
7254 end = i_size_read(inode);
7255
7256 BUG_ON(start >= end);
7257
7258 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7259 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7260 !ocfs2_supports_inline_data(osb)) {
7261 ocfs2_error(inode->i_sb,
7262 "Inline data flags for inode %llu don't agree! "
7263 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7264 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7265 le16_to_cpu(di->i_dyn_features),
7266 OCFS2_I(inode)->ip_dyn_features,
7267 osb->s_feature_incompat);
7268 ret = -EROFS;
7269 goto out;
7270 }
7271
7272 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7273 if (IS_ERR(handle)) {
7274 ret = PTR_ERR(handle);
7275 mlog_errno(ret);
7276 goto out;
7277 }
7278
7279 ret = ocfs2_journal_access(handle, inode, di_bh,
7280 OCFS2_JOURNAL_ACCESS_WRITE);
7281 if (ret) {
7282 mlog_errno(ret);
7283 goto out_commit;
7284 }
7285
7286 numbytes = end - start;
7287 memset(idata->id_data + start, 0, numbytes);
7288
7289 /*
7290 * No need to worry about the data page here - it's been
7291 * truncated already and inline data doesn't need it for
7292 * pushing zero's to disk, so we'll let readpage pick it up
7293 * later.
7294 */
7295 if (trunc) {
7296 i_size_write(inode, start);
7297 di->i_size = cpu_to_le64(start);
7298 }
7299
7300 inode->i_blocks = ocfs2_inode_sector_count(inode);
7301 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7302
7303 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7304 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7305
7306 ocfs2_journal_dirty(handle, di_bh);
7307
7308out_commit:
7309 ocfs2_commit_trans(osb, handle);
7310
7311out:
7312 return ret;
7313}
7314
Mark Fashehccd979b2005-12-15 14:31:24 -08007315static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7316{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007317 /*
7318 * The caller is responsible for completing deallocation
7319 * before freeing the context.
7320 */
7321 if (tc->tc_dealloc.c_first_suballocator != NULL)
7322 mlog(ML_NOTICE,
7323 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007324
Mark Fasheha81cb882008-10-07 14:25:16 -07007325 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007326
7327 kfree(tc);
7328}