blob: c22ff49b5e3310772e03dc5b1803b85a53d47bee [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
Joel Beckerffdd7a52008-10-17 22:32:01 -0700535static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
536{
537 return ocfs2_new_path(path_root_bh(path), path_root_el(path));
538}
539
540static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
541{
542 return ocfs2_new_path(et->et_root_bh, et->et_root_el);
543}
544
Mark Fashehdcd05382007-01-16 11:32:23 -0800545/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800546 * Convenience function to journal all components in a path.
547 */
548static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
549 struct ocfs2_path *path)
550{
551 int i, ret = 0;
552
553 if (!path)
554 goto out;
555
556 for(i = 0; i < path_num_items(path); i++) {
557 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
558 OCFS2_JOURNAL_ACCESS_WRITE);
559 if (ret < 0) {
560 mlog_errno(ret);
561 goto out;
562 }
563 }
564
565out:
566 return ret;
567}
568
Mark Fasheh328d5752007-06-18 10:48:04 -0700569/*
570 * Return the index of the extent record which contains cluster #v_cluster.
571 * -1 is returned if it was not found.
572 *
573 * Should work fine on interior and exterior nodes.
574 */
575int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
576{
577 int ret = -1;
578 int i;
579 struct ocfs2_extent_rec *rec;
580 u32 rec_end, rec_start, clusters;
581
582 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
583 rec = &el->l_recs[i];
584
585 rec_start = le32_to_cpu(rec->e_cpos);
586 clusters = ocfs2_rec_clusters(el, rec);
587
588 rec_end = rec_start + clusters;
589
590 if (v_cluster >= rec_start && v_cluster < rec_end) {
591 ret = i;
592 break;
593 }
594 }
595
596 return ret;
597}
598
Mark Fashehdcd05382007-01-16 11:32:23 -0800599enum ocfs2_contig_type {
600 CONTIG_NONE = 0,
601 CONTIG_LEFT,
Mark Fasheh328d5752007-06-18 10:48:04 -0700602 CONTIG_RIGHT,
603 CONTIG_LEFTRIGHT,
Mark Fashehdcd05382007-01-16 11:32:23 -0800604};
605
Mark Fashehe48edee2007-03-07 16:46:57 -0800606
607/*
608 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
609 * ocfs2_extent_contig only work properly against leaf nodes!
610 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800611static int ocfs2_block_extent_contig(struct super_block *sb,
612 struct ocfs2_extent_rec *ext,
613 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800614{
Mark Fashehe48edee2007-03-07 16:46:57 -0800615 u64 blk_end = le64_to_cpu(ext->e_blkno);
616
617 blk_end += ocfs2_clusters_to_blocks(sb,
618 le16_to_cpu(ext->e_leaf_clusters));
619
620 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800621}
622
Mark Fashehdcd05382007-01-16 11:32:23 -0800623static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
624 struct ocfs2_extent_rec *right)
625{
Mark Fashehe48edee2007-03-07 16:46:57 -0800626 u32 left_range;
627
628 left_range = le32_to_cpu(left->e_cpos) +
629 le16_to_cpu(left->e_leaf_clusters);
630
631 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800632}
633
634static enum ocfs2_contig_type
635 ocfs2_extent_contig(struct inode *inode,
636 struct ocfs2_extent_rec *ext,
637 struct ocfs2_extent_rec *insert_rec)
638{
639 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
640
Mark Fasheh328d5752007-06-18 10:48:04 -0700641 /*
642 * Refuse to coalesce extent records with different flag
643 * fields - we don't want to mix unwritten extents with user
644 * data.
645 */
646 if (ext->e_flags != insert_rec->e_flags)
647 return CONTIG_NONE;
648
Mark Fashehdcd05382007-01-16 11:32:23 -0800649 if (ocfs2_extents_adjacent(ext, insert_rec) &&
650 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
651 return CONTIG_RIGHT;
652
653 blkno = le64_to_cpu(ext->e_blkno);
654 if (ocfs2_extents_adjacent(insert_rec, ext) &&
655 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
656 return CONTIG_LEFT;
657
658 return CONTIG_NONE;
659}
660
661/*
662 * NOTE: We can have pretty much any combination of contiguousness and
663 * appending.
664 *
665 * The usefulness of APPEND_TAIL is more in that it lets us know that
666 * we'll have to update the path to that leaf.
667 */
668enum ocfs2_append_type {
669 APPEND_NONE = 0,
670 APPEND_TAIL,
671};
672
Mark Fasheh328d5752007-06-18 10:48:04 -0700673enum ocfs2_split_type {
674 SPLIT_NONE = 0,
675 SPLIT_LEFT,
676 SPLIT_RIGHT,
677};
678
Mark Fashehdcd05382007-01-16 11:32:23 -0800679struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700680 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800681 enum ocfs2_append_type ins_appending;
682 enum ocfs2_contig_type ins_contig;
683 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800684 int ins_tree_depth;
685};
686
Mark Fasheh328d5752007-06-18 10:48:04 -0700687struct ocfs2_merge_ctxt {
688 enum ocfs2_contig_type c_contig_type;
689 int c_has_empty_extent;
690 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700691};
692
Joel Becker5e965812008-11-13 14:49:16 -0800693static int ocfs2_validate_extent_block(struct super_block *sb,
694 struct buffer_head *bh)
695{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700696 int rc;
Joel Becker5e965812008-11-13 14:49:16 -0800697 struct ocfs2_extent_block *eb =
698 (struct ocfs2_extent_block *)bh->b_data;
699
Joel Becker970e4932008-11-13 14:49:19 -0800700 mlog(0, "Validating extent block %llu\n",
701 (unsigned long long)bh->b_blocknr);
702
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700703 BUG_ON(!buffer_uptodate(bh));
704
705 /*
706 * If the ecc fails, we return the error but otherwise
707 * leave the filesystem running. We know any error is
708 * local to this block.
709 */
710 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
711 if (rc)
712 return rc;
713
714 /*
715 * Errors after here are fatal.
716 */
717
Joel Becker5e965812008-11-13 14:49:16 -0800718 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
719 ocfs2_error(sb,
720 "Extent block #%llu has bad signature %.*s",
721 (unsigned long long)bh->b_blocknr, 7,
722 eb->h_signature);
723 return -EINVAL;
724 }
725
726 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
727 ocfs2_error(sb,
728 "Extent block #%llu has an invalid h_blkno "
729 "of %llu",
730 (unsigned long long)bh->b_blocknr,
731 (unsigned long long)le64_to_cpu(eb->h_blkno));
732 return -EINVAL;
733 }
734
735 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
736 ocfs2_error(sb,
737 "Extent block #%llu has an invalid "
738 "h_fs_generation of #%u",
739 (unsigned long long)bh->b_blocknr,
740 le32_to_cpu(eb->h_fs_generation));
741 return -EINVAL;
742 }
743
744 return 0;
745}
746
747int ocfs2_read_extent_block(struct inode *inode, u64 eb_blkno,
748 struct buffer_head **bh)
749{
750 int rc;
751 struct buffer_head *tmp = *bh;
752
Joel Becker970e4932008-11-13 14:49:19 -0800753 rc = ocfs2_read_block(inode, eb_blkno, &tmp,
754 ocfs2_validate_extent_block);
Joel Becker5e965812008-11-13 14:49:16 -0800755
756 /* If ocfs2_read_block() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -0800757 if (!rc && !*bh)
Joel Becker5e965812008-11-13 14:49:16 -0800758 *bh = tmp;
759
Joel Becker5e965812008-11-13 14:49:16 -0800760 return rc;
761}
762
763
Mark Fashehccd979b2005-12-15 14:31:24 -0800764/*
765 * How many free extents have we got before we need more meta data?
766 */
767int ocfs2_num_free_extents(struct ocfs2_super *osb,
768 struct inode *inode,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700769 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -0800770{
771 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800772 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800773 struct ocfs2_extent_block *eb;
774 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800775 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800776
777 mlog_entry_void();
778
Joel Beckerf99b9b72008-08-20 19:36:33 -0700779 el = et->et_root_el;
780 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800781
Tao Mae7d4cb62008-08-18 17:38:44 +0800782 if (last_eb_blk) {
Joel Becker5e965812008-11-13 14:49:16 -0800783 retval = ocfs2_read_extent_block(inode, last_eb_blk, &eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800784 if (retval < 0) {
785 mlog_errno(retval);
786 goto bail;
787 }
788 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
789 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800790 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800791
792 BUG_ON(el->l_tree_depth != 0);
793
794 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
795bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700796 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800797
798 mlog_exit(retval);
799 return retval;
800}
801
802/* expects array to already be allocated
803 *
804 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
805 * l_count for you
806 */
807static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700808 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800809 struct inode *inode,
810 int wanted,
811 struct ocfs2_alloc_context *meta_ac,
812 struct buffer_head *bhs[])
813{
814 int count, status, i;
815 u16 suballoc_bit_start;
816 u32 num_got;
817 u64 first_blkno;
818 struct ocfs2_extent_block *eb;
819
820 mlog_entry_void();
821
822 count = 0;
823 while (count < wanted) {
824 status = ocfs2_claim_metadata(osb,
825 handle,
826 meta_ac,
827 wanted - count,
828 &suballoc_bit_start,
829 &num_got,
830 &first_blkno);
831 if (status < 0) {
832 mlog_errno(status);
833 goto bail;
834 }
835
836 for(i = count; i < (num_got + count); i++) {
837 bhs[i] = sb_getblk(osb->sb, first_blkno);
838 if (bhs[i] == NULL) {
839 status = -EIO;
840 mlog_errno(status);
841 goto bail;
842 }
843 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
844
845 status = ocfs2_journal_access(handle, inode, bhs[i],
846 OCFS2_JOURNAL_ACCESS_CREATE);
847 if (status < 0) {
848 mlog_errno(status);
849 goto bail;
850 }
851
852 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
853 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
854 /* Ok, setup the minimal stuff here. */
855 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
856 eb->h_blkno = cpu_to_le64(first_blkno);
857 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800858 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800859 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
860 eb->h_list.l_count =
861 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
862
863 suballoc_bit_start++;
864 first_blkno++;
865
866 /* We'll also be dirtied by the caller, so
867 * this isn't absolutely necessary. */
868 status = ocfs2_journal_dirty(handle, bhs[i]);
869 if (status < 0) {
870 mlog_errno(status);
871 goto bail;
872 }
873 }
874
875 count += num_got;
876 }
877
878 status = 0;
879bail:
880 if (status < 0) {
881 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -0700882 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800883 bhs[i] = NULL;
884 }
885 }
886 mlog_exit(status);
887 return status;
888}
889
890/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800891 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
892 *
893 * Returns the sum of the rightmost extent rec logical offset and
894 * cluster count.
895 *
896 * ocfs2_add_branch() uses this to determine what logical cluster
897 * value should be populated into the leftmost new branch records.
898 *
899 * ocfs2_shift_tree_depth() uses this to determine the # clusters
900 * value for the new topmost tree record.
901 */
902static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
903{
904 int i;
905
906 i = le16_to_cpu(el->l_next_free_rec) - 1;
907
908 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -0800909 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -0800910}
911
912/*
Mark Fashehccd979b2005-12-15 14:31:24 -0800913 * Add an entire tree branch to our inode. eb_bh is the extent block
914 * to start at, if we don't want to start the branch at the dinode
915 * structure.
916 *
917 * last_eb_bh is required as we have to update it's next_leaf pointer
918 * for the new last extent block.
919 *
920 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -0800921 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -0800922 */
923static int ocfs2_add_branch(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700924 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800925 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +0800926 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -0800927 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -0700928 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800929 struct ocfs2_alloc_context *meta_ac)
930{
931 int status, new_blocks, i;
932 u64 next_blkno, new_last_eb_blk;
933 struct buffer_head *bh;
934 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800935 struct ocfs2_extent_block *eb;
936 struct ocfs2_extent_list *eb_el;
937 struct ocfs2_extent_list *el;
Mark Fashehdcd05382007-01-16 11:32:23 -0800938 u32 new_cpos;
Mark Fashehccd979b2005-12-15 14:31:24 -0800939
940 mlog_entry_void();
941
Mark Fasheh328d5752007-06-18 10:48:04 -0700942 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800943
Mark Fashehccd979b2005-12-15 14:31:24 -0800944 if (eb_bh) {
945 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
946 el = &eb->h_list;
947 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700948 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -0800949
950 /* we never add a branch to a leaf. */
951 BUG_ON(!el->l_tree_depth);
952
953 new_blocks = le16_to_cpu(el->l_tree_depth);
954
955 /* allocate the number of new eb blocks we need */
956 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
957 GFP_KERNEL);
958 if (!new_eb_bhs) {
959 status = -ENOMEM;
960 mlog_errno(status);
961 goto bail;
962 }
963
964 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
965 meta_ac, new_eb_bhs);
966 if (status < 0) {
967 mlog_errno(status);
968 goto bail;
969 }
970
Mark Fasheh328d5752007-06-18 10:48:04 -0700971 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
Mark Fashehdcd05382007-01-16 11:32:23 -0800972 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
973
Mark Fashehccd979b2005-12-15 14:31:24 -0800974 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
975 * linked with the rest of the tree.
976 * conversly, new_eb_bhs[0] is the new bottommost leaf.
977 *
978 * when we leave the loop, new_last_eb_blk will point to the
979 * newest leaf, and next_blkno will point to the topmost extent
980 * block. */
981 next_blkno = new_last_eb_blk = 0;
982 for(i = 0; i < new_blocks; i++) {
983 bh = new_eb_bhs[i];
984 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -0800985 /* ocfs2_create_new_meta_bhs() should create it right! */
986 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -0800987 eb_el = &eb->h_list;
988
989 status = ocfs2_journal_access(handle, inode, bh,
990 OCFS2_JOURNAL_ACCESS_CREATE);
991 if (status < 0) {
992 mlog_errno(status);
993 goto bail;
994 }
995
996 eb->h_next_leaf_blk = 0;
997 eb_el->l_tree_depth = cpu_to_le16(i);
998 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -0800999 /*
1000 * This actually counts as an empty extent as
1001 * c_clusters == 0
1002 */
1003 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001004 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001005 /*
1006 * eb_el isn't always an interior node, but even leaf
1007 * nodes want a zero'd flags and reserved field so
1008 * this gets the whole 32 bits regardless of use.
1009 */
1010 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001011 if (!eb_el->l_tree_depth)
1012 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1013
1014 status = ocfs2_journal_dirty(handle, bh);
1015 if (status < 0) {
1016 mlog_errno(status);
1017 goto bail;
1018 }
1019
1020 next_blkno = le64_to_cpu(eb->h_blkno);
1021 }
1022
1023 /* This is a bit hairy. We want to update up to three blocks
1024 * here without leaving any of them in an inconsistent state
1025 * in case of error. We don't have to worry about
1026 * journal_dirty erroring as it won't unless we've aborted the
1027 * handle (in which case we would never be here) so reserving
1028 * the write with journal_access is all we need to do. */
Mark Fasheh328d5752007-06-18 10:48:04 -07001029 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001030 OCFS2_JOURNAL_ACCESS_WRITE);
1031 if (status < 0) {
1032 mlog_errno(status);
1033 goto bail;
1034 }
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001035 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001036 OCFS2_JOURNAL_ACCESS_WRITE);
1037 if (status < 0) {
1038 mlog_errno(status);
1039 goto bail;
1040 }
1041 if (eb_bh) {
1042 status = ocfs2_journal_access(handle, inode, eb_bh,
1043 OCFS2_JOURNAL_ACCESS_WRITE);
1044 if (status < 0) {
1045 mlog_errno(status);
1046 goto bail;
1047 }
1048 }
1049
1050 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001051 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001052 i = le16_to_cpu(el->l_next_free_rec);
1053 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001054 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001055 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001056 le16_add_cpu(&el->l_next_free_rec, 1);
1057
1058 /* fe needs a new last extent block pointer, as does the
1059 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001060 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001061
Mark Fasheh328d5752007-06-18 10:48:04 -07001062 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001063 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1064
Mark Fasheh328d5752007-06-18 10:48:04 -07001065 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001066 if (status < 0)
1067 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001068 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001069 if (status < 0)
1070 mlog_errno(status);
1071 if (eb_bh) {
1072 status = ocfs2_journal_dirty(handle, eb_bh);
1073 if (status < 0)
1074 mlog_errno(status);
1075 }
1076
Mark Fasheh328d5752007-06-18 10:48:04 -07001077 /*
1078 * Some callers want to track the rightmost leaf so pass it
1079 * back here.
1080 */
1081 brelse(*last_eb_bh);
1082 get_bh(new_eb_bhs[0]);
1083 *last_eb_bh = new_eb_bhs[0];
1084
Mark Fashehccd979b2005-12-15 14:31:24 -08001085 status = 0;
1086bail:
1087 if (new_eb_bhs) {
1088 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001089 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001090 kfree(new_eb_bhs);
1091 }
1092
1093 mlog_exit(status);
1094 return status;
1095}
1096
1097/*
1098 * adds another level to the allocation tree.
1099 * returns back the new extent block so you can add a branch to it
1100 * after this call.
1101 */
1102static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001103 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001104 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001105 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001106 struct ocfs2_alloc_context *meta_ac,
1107 struct buffer_head **ret_new_eb_bh)
1108{
1109 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001110 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001111 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001112 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001113 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001114 struct ocfs2_extent_list *eb_el;
1115
1116 mlog_entry_void();
1117
1118 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
1119 &new_eb_bh);
1120 if (status < 0) {
1121 mlog_errno(status);
1122 goto bail;
1123 }
1124
1125 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001126 /* ocfs2_create_new_meta_bhs() should create it right! */
1127 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001128
1129 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001130 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001131
1132 status = ocfs2_journal_access(handle, inode, new_eb_bh,
1133 OCFS2_JOURNAL_ACCESS_CREATE);
1134 if (status < 0) {
1135 mlog_errno(status);
1136 goto bail;
1137 }
1138
Tao Mae7d4cb62008-08-18 17:38:44 +08001139 /* copy the root extent list data into the new extent block */
1140 eb_el->l_tree_depth = root_el->l_tree_depth;
1141 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1142 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1143 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001144
1145 status = ocfs2_journal_dirty(handle, new_eb_bh);
1146 if (status < 0) {
1147 mlog_errno(status);
1148 goto bail;
1149 }
1150
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001151 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001152 OCFS2_JOURNAL_ACCESS_WRITE);
1153 if (status < 0) {
1154 mlog_errno(status);
1155 goto bail;
1156 }
1157
Mark Fashehdcd05382007-01-16 11:32:23 -08001158 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1159
Tao Mae7d4cb62008-08-18 17:38:44 +08001160 /* update root_bh now */
1161 le16_add_cpu(&root_el->l_tree_depth, 1);
1162 root_el->l_recs[0].e_cpos = 0;
1163 root_el->l_recs[0].e_blkno = eb->h_blkno;
1164 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1165 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1166 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1167 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001168
1169 /* If this is our 1st tree depth shift, then last_eb_blk
1170 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001171 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001172 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001173
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001174 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001175 if (status < 0) {
1176 mlog_errno(status);
1177 goto bail;
1178 }
1179
1180 *ret_new_eb_bh = new_eb_bh;
1181 new_eb_bh = NULL;
1182 status = 0;
1183bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001184 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001185
1186 mlog_exit(status);
1187 return status;
1188}
1189
1190/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001191 * Should only be called when there is no space left in any of the
1192 * leaf nodes. What we want to do is find the lowest tree depth
1193 * non-leaf extent block with room for new records. There are three
1194 * valid results of this search:
1195 *
1196 * 1) a lowest extent block is found, then we pass it back in
1197 * *lowest_eb_bh and return '0'
1198 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001199 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001200 * pass NULL back in *lowest_eb_bh, but still return '0'
1201 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001202 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001203 * which case we return > 0
1204 *
1205 * return status < 0 indicates an error.
1206 */
1207static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1208 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001209 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001210 struct buffer_head **target_bh)
1211{
1212 int status = 0, i;
1213 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001214 struct ocfs2_extent_block *eb;
1215 struct ocfs2_extent_list *el;
1216 struct buffer_head *bh = NULL;
1217 struct buffer_head *lowest_bh = NULL;
1218
1219 mlog_entry_void();
1220
1221 *target_bh = NULL;
1222
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001223 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001224
1225 while(le16_to_cpu(el->l_tree_depth) > 1) {
1226 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Mark Fashehb06970532006-03-03 10:24:33 -08001227 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001228 "extent list (next_free_rec == 0)",
Mark Fashehb06970532006-03-03 10:24:33 -08001229 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001230 status = -EIO;
1231 goto bail;
1232 }
1233 i = le16_to_cpu(el->l_next_free_rec) - 1;
1234 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1235 if (!blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -08001236 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001237 "list where extent # %d has no physical "
1238 "block start",
Mark Fashehb06970532006-03-03 10:24:33 -08001239 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001240 status = -EIO;
1241 goto bail;
1242 }
1243
Mark Fasheha81cb882008-10-07 14:25:16 -07001244 brelse(bh);
1245 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001246
Joel Becker5e965812008-11-13 14:49:16 -08001247 status = ocfs2_read_extent_block(inode, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001248 if (status < 0) {
1249 mlog_errno(status);
1250 goto bail;
1251 }
1252
1253 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001254 el = &eb->h_list;
1255
1256 if (le16_to_cpu(el->l_next_free_rec) <
1257 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001258 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001259 lowest_bh = bh;
1260 get_bh(lowest_bh);
1261 }
1262 }
1263
1264 /* If we didn't find one and the fe doesn't have any room,
1265 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001266 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001267 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001268 status = 1;
1269
1270 *target_bh = lowest_bh;
1271bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001272 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001273
1274 mlog_exit(status);
1275 return status;
1276}
1277
Mark Fashehe48edee2007-03-07 16:46:57 -08001278/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001279 * Grow a b-tree so that it has more records.
1280 *
1281 * We might shift the tree depth in which case existing paths should
1282 * be considered invalid.
1283 *
1284 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001285 *
1286 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001287 */
1288static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001289 struct ocfs2_extent_tree *et, int *final_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07001290 struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001291 struct ocfs2_alloc_context *meta_ac)
1292{
1293 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001294 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001295 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001296 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1297 struct buffer_head *bh = NULL;
1298
1299 BUG_ON(meta_ac == NULL);
1300
Tao Mae7d4cb62008-08-18 17:38:44 +08001301 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001302 if (shift < 0) {
1303 ret = shift;
1304 mlog_errno(ret);
1305 goto out;
1306 }
1307
1308 /* We traveled all the way to the bottom of the allocation tree
1309 * and didn't find room for any more extents - we need to add
1310 * another tree level */
1311 if (shift) {
1312 BUG_ON(bh);
1313 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1314
1315 /* ocfs2_shift_tree_depth will return us a buffer with
1316 * the new extent block (so we can pass that to
1317 * ocfs2_add_branch). */
Tao Mae7d4cb62008-08-18 17:38:44 +08001318 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001319 meta_ac, &bh);
1320 if (ret < 0) {
1321 mlog_errno(ret);
1322 goto out;
1323 }
1324 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001325 if (depth == 1) {
1326 /*
1327 * Special case: we have room now if we shifted from
1328 * tree_depth 0, so no more work needs to be done.
1329 *
1330 * We won't be calling add_branch, so pass
1331 * back *last_eb_bh as the new leaf. At depth
1332 * zero, it should always be null so there's
1333 * no reason to brelse.
1334 */
1335 BUG_ON(*last_eb_bh);
1336 get_bh(bh);
1337 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001338 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001339 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001340 }
1341
1342 /* call ocfs2_add_branch to add the final part of the tree with
1343 * the new data. */
1344 mlog(0, "add branch. bh = %p\n", bh);
Tao Mae7d4cb62008-08-18 17:38:44 +08001345 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001346 meta_ac);
1347 if (ret < 0) {
1348 mlog_errno(ret);
1349 goto out;
1350 }
1351
1352out:
1353 if (final_depth)
1354 *final_depth = depth;
1355 brelse(bh);
1356 return ret;
1357}
1358
1359/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001360 * This function will discard the rightmost extent record.
1361 */
1362static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1363{
1364 int next_free = le16_to_cpu(el->l_next_free_rec);
1365 int count = le16_to_cpu(el->l_count);
1366 unsigned int num_bytes;
1367
1368 BUG_ON(!next_free);
1369 /* This will cause us to go off the end of our extent list. */
1370 BUG_ON(next_free >= count);
1371
1372 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1373
1374 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1375}
1376
1377static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1378 struct ocfs2_extent_rec *insert_rec)
1379{
1380 int i, insert_index, next_free, has_empty, num_bytes;
1381 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1382 struct ocfs2_extent_rec *rec;
1383
1384 next_free = le16_to_cpu(el->l_next_free_rec);
1385 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1386
1387 BUG_ON(!next_free);
1388
1389 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001390 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001391
1392 /*
1393 * The easiest way to approach this is to just remove the
1394 * empty extent and temporarily decrement next_free.
1395 */
1396 if (has_empty) {
1397 /*
1398 * If next_free was 1 (only an empty extent), this
1399 * loop won't execute, which is fine. We still want
1400 * the decrement above to happen.
1401 */
1402 for(i = 0; i < (next_free - 1); i++)
1403 el->l_recs[i] = el->l_recs[i+1];
1404
1405 next_free--;
1406 }
1407
1408 /*
1409 * Figure out what the new record index should be.
1410 */
1411 for(i = 0; i < next_free; i++) {
1412 rec = &el->l_recs[i];
1413
1414 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1415 break;
1416 }
1417 insert_index = i;
1418
1419 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1420 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1421
1422 BUG_ON(insert_index < 0);
1423 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1424 BUG_ON(insert_index > next_free);
1425
1426 /*
1427 * No need to memmove if we're just adding to the tail.
1428 */
1429 if (insert_index != next_free) {
1430 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1431
1432 num_bytes = next_free - insert_index;
1433 num_bytes *= sizeof(struct ocfs2_extent_rec);
1434 memmove(&el->l_recs[insert_index + 1],
1435 &el->l_recs[insert_index],
1436 num_bytes);
1437 }
1438
1439 /*
1440 * Either we had an empty extent, and need to re-increment or
1441 * there was no empty extent on a non full rightmost leaf node,
1442 * in which case we still need to increment.
1443 */
1444 next_free++;
1445 el->l_next_free_rec = cpu_to_le16(next_free);
1446 /*
1447 * Make sure none of the math above just messed up our tree.
1448 */
1449 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1450
1451 el->l_recs[insert_index] = *insert_rec;
1452
1453}
1454
Mark Fasheh328d5752007-06-18 10:48:04 -07001455static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1456{
1457 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1458
1459 BUG_ON(num_recs == 0);
1460
1461 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1462 num_recs--;
1463 size = num_recs * sizeof(struct ocfs2_extent_rec);
1464 memmove(&el->l_recs[0], &el->l_recs[1], size);
1465 memset(&el->l_recs[num_recs], 0,
1466 sizeof(struct ocfs2_extent_rec));
1467 el->l_next_free_rec = cpu_to_le16(num_recs);
1468 }
1469}
1470
Mark Fashehdcd05382007-01-16 11:32:23 -08001471/*
1472 * Create an empty extent record .
1473 *
1474 * l_next_free_rec may be updated.
1475 *
1476 * If an empty extent already exists do nothing.
1477 */
1478static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1479{
1480 int next_free = le16_to_cpu(el->l_next_free_rec);
1481
Mark Fashehe48edee2007-03-07 16:46:57 -08001482 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1483
Mark Fashehdcd05382007-01-16 11:32:23 -08001484 if (next_free == 0)
1485 goto set_and_inc;
1486
1487 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1488 return;
1489
1490 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1491 "Asked to create an empty extent in a full list:\n"
1492 "count = %u, tree depth = %u",
1493 le16_to_cpu(el->l_count),
1494 le16_to_cpu(el->l_tree_depth));
1495
1496 ocfs2_shift_records_right(el);
1497
1498set_and_inc:
1499 le16_add_cpu(&el->l_next_free_rec, 1);
1500 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1501}
1502
1503/*
1504 * For a rotation which involves two leaf nodes, the "root node" is
1505 * the lowest level tree node which contains a path to both leafs. This
1506 * resulting set of information can be used to form a complete "subtree"
1507 *
1508 * This function is passed two full paths from the dinode down to a
1509 * pair of adjacent leaves. It's task is to figure out which path
1510 * index contains the subtree root - this can be the root index itself
1511 * in a worst-case rotation.
1512 *
1513 * The array index of the subtree root is passed back.
1514 */
1515static int ocfs2_find_subtree_root(struct inode *inode,
1516 struct ocfs2_path *left,
1517 struct ocfs2_path *right)
1518{
1519 int i = 0;
1520
1521 /*
1522 * Check that the caller passed in two paths from the same tree.
1523 */
1524 BUG_ON(path_root_bh(left) != path_root_bh(right));
1525
1526 do {
1527 i++;
1528
1529 /*
1530 * The caller didn't pass two adjacent paths.
1531 */
1532 mlog_bug_on_msg(i > left->p_tree_depth,
1533 "Inode %lu, left depth %u, right depth %u\n"
1534 "left leaf blk %llu, right leaf blk %llu\n",
1535 inode->i_ino, left->p_tree_depth,
1536 right->p_tree_depth,
1537 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1538 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1539 } while (left->p_node[i].bh->b_blocknr ==
1540 right->p_node[i].bh->b_blocknr);
1541
1542 return i - 1;
1543}
1544
1545typedef void (path_insert_t)(void *, struct buffer_head *);
1546
1547/*
1548 * Traverse a btree path in search of cpos, starting at root_el.
1549 *
1550 * This code can be called with a cpos larger than the tree, in which
1551 * case it will return the rightmost path.
1552 */
1553static int __ocfs2_find_path(struct inode *inode,
1554 struct ocfs2_extent_list *root_el, u32 cpos,
1555 path_insert_t *func, void *data)
1556{
1557 int i, ret = 0;
1558 u32 range;
1559 u64 blkno;
1560 struct buffer_head *bh = NULL;
1561 struct ocfs2_extent_block *eb;
1562 struct ocfs2_extent_list *el;
1563 struct ocfs2_extent_rec *rec;
1564 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1565
1566 el = root_el;
1567 while (el->l_tree_depth) {
1568 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1569 ocfs2_error(inode->i_sb,
1570 "Inode %llu has empty extent list at "
1571 "depth %u\n",
1572 (unsigned long long)oi->ip_blkno,
1573 le16_to_cpu(el->l_tree_depth));
1574 ret = -EROFS;
1575 goto out;
1576
1577 }
1578
1579 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1580 rec = &el->l_recs[i];
1581
1582 /*
1583 * In the case that cpos is off the allocation
1584 * tree, this should just wind up returning the
1585 * rightmost record.
1586 */
1587 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001588 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001589 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1590 break;
1591 }
1592
1593 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1594 if (blkno == 0) {
1595 ocfs2_error(inode->i_sb,
1596 "Inode %llu has bad blkno in extent list "
1597 "at depth %u (index %d)\n",
1598 (unsigned long long)oi->ip_blkno,
1599 le16_to_cpu(el->l_tree_depth), i);
1600 ret = -EROFS;
1601 goto out;
1602 }
1603
1604 brelse(bh);
1605 bh = NULL;
Joel Becker5e965812008-11-13 14:49:16 -08001606 ret = ocfs2_read_extent_block(inode, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001607 if (ret) {
1608 mlog_errno(ret);
1609 goto out;
1610 }
1611
1612 eb = (struct ocfs2_extent_block *) bh->b_data;
1613 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001614
1615 if (le16_to_cpu(el->l_next_free_rec) >
1616 le16_to_cpu(el->l_count)) {
1617 ocfs2_error(inode->i_sb,
1618 "Inode %llu has bad count in extent list "
1619 "at block %llu (next free=%u, count=%u)\n",
1620 (unsigned long long)oi->ip_blkno,
1621 (unsigned long long)bh->b_blocknr,
1622 le16_to_cpu(el->l_next_free_rec),
1623 le16_to_cpu(el->l_count));
1624 ret = -EROFS;
1625 goto out;
1626 }
1627
1628 if (func)
1629 func(data, bh);
1630 }
1631
1632out:
1633 /*
1634 * Catch any trailing bh that the loop didn't handle.
1635 */
1636 brelse(bh);
1637
1638 return ret;
1639}
1640
1641/*
1642 * Given an initialized path (that is, it has a valid root extent
1643 * list), this function will traverse the btree in search of the path
1644 * which would contain cpos.
1645 *
1646 * The path traveled is recorded in the path structure.
1647 *
1648 * Note that this will not do any comparisons on leaf node extent
1649 * records, so it will work fine in the case that we just added a tree
1650 * branch.
1651 */
1652struct find_path_data {
1653 int index;
1654 struct ocfs2_path *path;
1655};
1656static void find_path_ins(void *data, struct buffer_head *bh)
1657{
1658 struct find_path_data *fp = data;
1659
1660 get_bh(bh);
1661 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1662 fp->index++;
1663}
1664static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1665 u32 cpos)
1666{
1667 struct find_path_data data;
1668
1669 data.index = 1;
1670 data.path = path;
1671 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1672 find_path_ins, &data);
1673}
1674
1675static void find_leaf_ins(void *data, struct buffer_head *bh)
1676{
1677 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1678 struct ocfs2_extent_list *el = &eb->h_list;
1679 struct buffer_head **ret = data;
1680
1681 /* We want to retain only the leaf block. */
1682 if (le16_to_cpu(el->l_tree_depth) == 0) {
1683 get_bh(bh);
1684 *ret = bh;
1685 }
1686}
1687/*
1688 * Find the leaf block in the tree which would contain cpos. No
1689 * checking of the actual leaf is done.
1690 *
1691 * Some paths want to call this instead of allocating a path structure
1692 * and calling ocfs2_find_path().
1693 *
1694 * This function doesn't handle non btree extent lists.
1695 */
Mark Fasheh363041a2007-01-17 12:31:35 -08001696int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1697 u32 cpos, struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001698{
1699 int ret;
1700 struct buffer_head *bh = NULL;
1701
1702 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1703 if (ret) {
1704 mlog_errno(ret);
1705 goto out;
1706 }
1707
1708 *leaf_bh = bh;
1709out:
1710 return ret;
1711}
1712
1713/*
1714 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1715 *
1716 * Basically, we've moved stuff around at the bottom of the tree and
1717 * we need to fix up the extent records above the changes to reflect
1718 * the new changes.
1719 *
1720 * left_rec: the record on the left.
1721 * left_child_el: is the child list pointed to by left_rec
1722 * right_rec: the record to the right of left_rec
1723 * right_child_el: is the child list pointed to by right_rec
1724 *
1725 * By definition, this only works on interior nodes.
1726 */
1727static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1728 struct ocfs2_extent_list *left_child_el,
1729 struct ocfs2_extent_rec *right_rec,
1730 struct ocfs2_extent_list *right_child_el)
1731{
1732 u32 left_clusters, right_end;
1733
1734 /*
1735 * Interior nodes never have holes. Their cpos is the cpos of
1736 * the leftmost record in their child list. Their cluster
1737 * count covers the full theoretical range of their child list
1738 * - the range between their cpos and the cpos of the record
1739 * immediately to their right.
1740 */
1741 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07001742 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1743 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1744 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1745 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001746 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001747 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001748
1749 /*
1750 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001751 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001752 * updating e_cpos to keep the same highest cluster count.
1753 */
1754 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001755 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001756
1757 right_rec->e_cpos = left_rec->e_cpos;
1758 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1759
1760 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001761 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001762}
1763
1764/*
1765 * Adjust the adjacent root node records involved in a
1766 * rotation. left_el_blkno is passed in as a key so that we can easily
1767 * find it's index in the root list.
1768 */
1769static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1770 struct ocfs2_extent_list *left_el,
1771 struct ocfs2_extent_list *right_el,
1772 u64 left_el_blkno)
1773{
1774 int i;
1775
1776 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1777 le16_to_cpu(left_el->l_tree_depth));
1778
1779 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1780 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1781 break;
1782 }
1783
1784 /*
1785 * The path walking code should have never returned a root and
1786 * two paths which are not adjacent.
1787 */
1788 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1789
1790 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1791 &root_el->l_recs[i + 1], right_el);
1792}
1793
1794/*
1795 * We've changed a leaf block (in right_path) and need to reflect that
1796 * change back up the subtree.
1797 *
1798 * This happens in multiple places:
1799 * - When we've moved an extent record from the left path leaf to the right
1800 * path leaf to make room for an empty extent in the left path leaf.
1801 * - When our insert into the right path leaf is at the leftmost edge
1802 * and requires an update of the path immediately to it's left. This
1803 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08001804 * - When we've adjusted the last extent record in the left path leaf and the
1805 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08001806 */
1807static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1808 struct ocfs2_path *left_path,
1809 struct ocfs2_path *right_path,
1810 int subtree_index)
1811{
1812 int ret, i, idx;
1813 struct ocfs2_extent_list *el, *left_el, *right_el;
1814 struct ocfs2_extent_rec *left_rec, *right_rec;
1815 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1816
1817 /*
1818 * Update the counts and position values within all the
1819 * interior nodes to reflect the leaf rotation we just did.
1820 *
1821 * The root node is handled below the loop.
1822 *
1823 * We begin the loop with right_el and left_el pointing to the
1824 * leaf lists and work our way up.
1825 *
1826 * NOTE: within this loop, left_el and right_el always refer
1827 * to the *child* lists.
1828 */
1829 left_el = path_leaf_el(left_path);
1830 right_el = path_leaf_el(right_path);
1831 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1832 mlog(0, "Adjust records at index %u\n", i);
1833
1834 /*
1835 * One nice property of knowing that all of these
1836 * nodes are below the root is that we only deal with
1837 * the leftmost right node record and the rightmost
1838 * left node record.
1839 */
1840 el = left_path->p_node[i].el;
1841 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1842 left_rec = &el->l_recs[idx];
1843
1844 el = right_path->p_node[i].el;
1845 right_rec = &el->l_recs[0];
1846
1847 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1848 right_el);
1849
1850 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1851 if (ret)
1852 mlog_errno(ret);
1853
1854 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1855 if (ret)
1856 mlog_errno(ret);
1857
1858 /*
1859 * Setup our list pointers now so that the current
1860 * parents become children in the next iteration.
1861 */
1862 left_el = left_path->p_node[i].el;
1863 right_el = right_path->p_node[i].el;
1864 }
1865
1866 /*
1867 * At the root node, adjust the two adjacent records which
1868 * begin our path to the leaves.
1869 */
1870
1871 el = left_path->p_node[subtree_index].el;
1872 left_el = left_path->p_node[subtree_index + 1].el;
1873 right_el = right_path->p_node[subtree_index + 1].el;
1874
1875 ocfs2_adjust_root_records(el, left_el, right_el,
1876 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1877
1878 root_bh = left_path->p_node[subtree_index].bh;
1879
1880 ret = ocfs2_journal_dirty(handle, root_bh);
1881 if (ret)
1882 mlog_errno(ret);
1883}
1884
1885static int ocfs2_rotate_subtree_right(struct inode *inode,
1886 handle_t *handle,
1887 struct ocfs2_path *left_path,
1888 struct ocfs2_path *right_path,
1889 int subtree_index)
1890{
1891 int ret, i;
1892 struct buffer_head *right_leaf_bh;
1893 struct buffer_head *left_leaf_bh = NULL;
1894 struct buffer_head *root_bh;
1895 struct ocfs2_extent_list *right_el, *left_el;
1896 struct ocfs2_extent_rec move_rec;
1897
1898 left_leaf_bh = path_leaf_bh(left_path);
1899 left_el = path_leaf_el(left_path);
1900
1901 if (left_el->l_next_free_rec != left_el->l_count) {
1902 ocfs2_error(inode->i_sb,
1903 "Inode %llu has non-full interior leaf node %llu"
1904 "(next free = %u)",
1905 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1906 (unsigned long long)left_leaf_bh->b_blocknr,
1907 le16_to_cpu(left_el->l_next_free_rec));
1908 return -EROFS;
1909 }
1910
1911 /*
1912 * This extent block may already have an empty record, so we
1913 * return early if so.
1914 */
1915 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1916 return 0;
1917
1918 root_bh = left_path->p_node[subtree_index].bh;
1919 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1920
1921 ret = ocfs2_journal_access(handle, inode, root_bh,
1922 OCFS2_JOURNAL_ACCESS_WRITE);
1923 if (ret) {
1924 mlog_errno(ret);
1925 goto out;
1926 }
1927
1928 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1929 ret = ocfs2_journal_access(handle, inode,
1930 right_path->p_node[i].bh,
1931 OCFS2_JOURNAL_ACCESS_WRITE);
1932 if (ret) {
1933 mlog_errno(ret);
1934 goto out;
1935 }
1936
1937 ret = ocfs2_journal_access(handle, inode,
1938 left_path->p_node[i].bh,
1939 OCFS2_JOURNAL_ACCESS_WRITE);
1940 if (ret) {
1941 mlog_errno(ret);
1942 goto out;
1943 }
1944 }
1945
1946 right_leaf_bh = path_leaf_bh(right_path);
1947 right_el = path_leaf_el(right_path);
1948
1949 /* This is a code error, not a disk corruption. */
1950 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1951 "because rightmost leaf block %llu is empty\n",
1952 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1953 (unsigned long long)right_leaf_bh->b_blocknr);
1954
1955 ocfs2_create_empty_extent(right_el);
1956
1957 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1958 if (ret) {
1959 mlog_errno(ret);
1960 goto out;
1961 }
1962
1963 /* Do the copy now. */
1964 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1965 move_rec = left_el->l_recs[i];
1966 right_el->l_recs[0] = move_rec;
1967
1968 /*
1969 * Clear out the record we just copied and shift everything
1970 * over, leaving an empty extent in the left leaf.
1971 *
1972 * We temporarily subtract from next_free_rec so that the
1973 * shift will lose the tail record (which is now defunct).
1974 */
1975 le16_add_cpu(&left_el->l_next_free_rec, -1);
1976 ocfs2_shift_records_right(left_el);
1977 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1978 le16_add_cpu(&left_el->l_next_free_rec, 1);
1979
1980 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1981 if (ret) {
1982 mlog_errno(ret);
1983 goto out;
1984 }
1985
1986 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1987 subtree_index);
1988
1989out:
1990 return ret;
1991}
1992
1993/*
1994 * Given a full path, determine what cpos value would return us a path
1995 * containing the leaf immediately to the left of the current one.
1996 *
1997 * Will return zero if the path passed in is already the leftmost path.
1998 */
1999static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2000 struct ocfs2_path *path, u32 *cpos)
2001{
2002 int i, j, ret = 0;
2003 u64 blkno;
2004 struct ocfs2_extent_list *el;
2005
Mark Fashehe48edee2007-03-07 16:46:57 -08002006 BUG_ON(path->p_tree_depth == 0);
2007
Mark Fashehdcd05382007-01-16 11:32:23 -08002008 *cpos = 0;
2009
2010 blkno = path_leaf_bh(path)->b_blocknr;
2011
2012 /* Start at the tree node just above the leaf and work our way up. */
2013 i = path->p_tree_depth - 1;
2014 while (i >= 0) {
2015 el = path->p_node[i].el;
2016
2017 /*
2018 * Find the extent record just before the one in our
2019 * path.
2020 */
2021 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2022 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2023 if (j == 0) {
2024 if (i == 0) {
2025 /*
2026 * We've determined that the
2027 * path specified is already
2028 * the leftmost one - return a
2029 * cpos of zero.
2030 */
2031 goto out;
2032 }
2033 /*
2034 * The leftmost record points to our
2035 * leaf - we need to travel up the
2036 * tree one level.
2037 */
2038 goto next_node;
2039 }
2040
2041 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002042 *cpos = *cpos + ocfs2_rec_clusters(el,
2043 &el->l_recs[j - 1]);
2044 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002045 goto out;
2046 }
2047 }
2048
2049 /*
2050 * If we got here, we never found a valid node where
2051 * the tree indicated one should be.
2052 */
2053 ocfs2_error(sb,
2054 "Invalid extent tree at extent block %llu\n",
2055 (unsigned long long)blkno);
2056 ret = -EROFS;
2057 goto out;
2058
2059next_node:
2060 blkno = path->p_node[i].bh->b_blocknr;
2061 i--;
2062 }
2063
2064out:
2065 return ret;
2066}
2067
Mark Fasheh328d5752007-06-18 10:48:04 -07002068/*
2069 * Extend the transaction by enough credits to complete the rotation,
2070 * and still leave at least the original number of credits allocated
2071 * to this transaction.
2072 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002073static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002074 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002075 struct ocfs2_path *path)
2076{
Mark Fasheh328d5752007-06-18 10:48:04 -07002077 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002078
2079 if (handle->h_buffer_credits < credits)
2080 return ocfs2_extend_trans(handle, credits);
2081
2082 return 0;
2083}
2084
2085/*
2086 * Trap the case where we're inserting into the theoretical range past
2087 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2088 * whose cpos is less than ours into the right leaf.
2089 *
2090 * It's only necessary to look at the rightmost record of the left
2091 * leaf because the logic that calls us should ensure that the
2092 * theoretical ranges in the path components above the leaves are
2093 * correct.
2094 */
2095static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2096 u32 insert_cpos)
2097{
2098 struct ocfs2_extent_list *left_el;
2099 struct ocfs2_extent_rec *rec;
2100 int next_free;
2101
2102 left_el = path_leaf_el(left_path);
2103 next_free = le16_to_cpu(left_el->l_next_free_rec);
2104 rec = &left_el->l_recs[next_free - 1];
2105
2106 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2107 return 1;
2108 return 0;
2109}
2110
Mark Fasheh328d5752007-06-18 10:48:04 -07002111static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2112{
2113 int next_free = le16_to_cpu(el->l_next_free_rec);
2114 unsigned int range;
2115 struct ocfs2_extent_rec *rec;
2116
2117 if (next_free == 0)
2118 return 0;
2119
2120 rec = &el->l_recs[0];
2121 if (ocfs2_is_empty_extent(rec)) {
2122 /* Empty list. */
2123 if (next_free == 1)
2124 return 0;
2125 rec = &el->l_recs[1];
2126 }
2127
2128 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2129 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2130 return 1;
2131 return 0;
2132}
2133
Mark Fashehdcd05382007-01-16 11:32:23 -08002134/*
2135 * Rotate all the records in a btree right one record, starting at insert_cpos.
2136 *
2137 * The path to the rightmost leaf should be passed in.
2138 *
2139 * The array is assumed to be large enough to hold an entire path (tree depth).
2140 *
2141 * Upon succesful return from this function:
2142 *
2143 * - The 'right_path' array will contain a path to the leaf block
2144 * whose range contains e_cpos.
2145 * - That leaf block will have a single empty extent in list index 0.
2146 * - In the case that the rotation requires a post-insert update,
2147 * *ret_left_path will contain a valid path which can be passed to
2148 * ocfs2_insert_path().
2149 */
2150static int ocfs2_rotate_tree_right(struct inode *inode,
2151 handle_t *handle,
Mark Fasheh328d5752007-06-18 10:48:04 -07002152 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002153 u32 insert_cpos,
2154 struct ocfs2_path *right_path,
2155 struct ocfs2_path **ret_left_path)
2156{
Mark Fasheh328d5752007-06-18 10:48:04 -07002157 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002158 u32 cpos;
2159 struct ocfs2_path *left_path = NULL;
2160
2161 *ret_left_path = NULL;
2162
Joel Beckerffdd7a52008-10-17 22:32:01 -07002163 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002164 if (!left_path) {
2165 ret = -ENOMEM;
2166 mlog_errno(ret);
2167 goto out;
2168 }
2169
2170 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2171 if (ret) {
2172 mlog_errno(ret);
2173 goto out;
2174 }
2175
2176 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2177
2178 /*
2179 * What we want to do here is:
2180 *
2181 * 1) Start with the rightmost path.
2182 *
2183 * 2) Determine a path to the leaf block directly to the left
2184 * of that leaf.
2185 *
2186 * 3) Determine the 'subtree root' - the lowest level tree node
2187 * which contains a path to both leaves.
2188 *
2189 * 4) Rotate the subtree.
2190 *
2191 * 5) Find the next subtree by considering the left path to be
2192 * the new right path.
2193 *
2194 * The check at the top of this while loop also accepts
2195 * insert_cpos == cpos because cpos is only a _theoretical_
2196 * value to get us the left path - insert_cpos might very well
2197 * be filling that hole.
2198 *
2199 * Stop at a cpos of '0' because we either started at the
2200 * leftmost branch (i.e., a tree with one branch and a
2201 * rotation inside of it), or we've gone as far as we can in
2202 * rotating subtrees.
2203 */
2204 while (cpos && insert_cpos <= cpos) {
2205 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2206 insert_cpos, cpos);
2207
2208 ret = ocfs2_find_path(inode, left_path, cpos);
2209 if (ret) {
2210 mlog_errno(ret);
2211 goto out;
2212 }
2213
2214 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2215 path_leaf_bh(right_path),
2216 "Inode %lu: error during insert of %u "
2217 "(left path cpos %u) results in two identical "
2218 "paths ending at %llu\n",
2219 inode->i_ino, insert_cpos, cpos,
2220 (unsigned long long)
2221 path_leaf_bh(left_path)->b_blocknr);
2222
Mark Fasheh328d5752007-06-18 10:48:04 -07002223 if (split == SPLIT_NONE &&
2224 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002225 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002226
2227 /*
2228 * We've rotated the tree as much as we
2229 * should. The rest is up to
2230 * ocfs2_insert_path() to complete, after the
2231 * record insertion. We indicate this
2232 * situation by returning the left path.
2233 *
2234 * The reason we don't adjust the records here
2235 * before the record insert is that an error
2236 * later might break the rule where a parent
2237 * record e_cpos will reflect the actual
2238 * e_cpos of the 1st nonempty record of the
2239 * child list.
2240 */
2241 *ret_left_path = left_path;
2242 goto out_ret_path;
2243 }
2244
2245 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2246
2247 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2248 start,
2249 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2250 right_path->p_tree_depth);
2251
2252 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002253 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002254 if (ret) {
2255 mlog_errno(ret);
2256 goto out;
2257 }
2258
2259 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2260 right_path, start);
2261 if (ret) {
2262 mlog_errno(ret);
2263 goto out;
2264 }
2265
Mark Fasheh328d5752007-06-18 10:48:04 -07002266 if (split != SPLIT_NONE &&
2267 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2268 insert_cpos)) {
2269 /*
2270 * A rotate moves the rightmost left leaf
2271 * record over to the leftmost right leaf
2272 * slot. If we're doing an extent split
2273 * instead of a real insert, then we have to
2274 * check that the extent to be split wasn't
2275 * just moved over. If it was, then we can
2276 * exit here, passing left_path back -
2277 * ocfs2_split_extent() is smart enough to
2278 * search both leaves.
2279 */
2280 *ret_left_path = left_path;
2281 goto out_ret_path;
2282 }
2283
Mark Fashehdcd05382007-01-16 11:32:23 -08002284 /*
2285 * There is no need to re-read the next right path
2286 * as we know that it'll be our current left
2287 * path. Optimize by copying values instead.
2288 */
2289 ocfs2_mv_path(right_path, left_path);
2290
2291 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2292 &cpos);
2293 if (ret) {
2294 mlog_errno(ret);
2295 goto out;
2296 }
2297 }
2298
2299out:
2300 ocfs2_free_path(left_path);
2301
2302out_ret_path:
2303 return ret;
2304}
2305
Mark Fasheh328d5752007-06-18 10:48:04 -07002306static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2307 struct ocfs2_path *path)
2308{
2309 int i, idx;
2310 struct ocfs2_extent_rec *rec;
2311 struct ocfs2_extent_list *el;
2312 struct ocfs2_extent_block *eb;
2313 u32 range;
2314
2315 /* Path should always be rightmost. */
2316 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2317 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2318
2319 el = &eb->h_list;
2320 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2321 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2322 rec = &el->l_recs[idx];
2323 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2324
2325 for (i = 0; i < path->p_tree_depth; i++) {
2326 el = path->p_node[i].el;
2327 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2328 rec = &el->l_recs[idx];
2329
2330 rec->e_int_clusters = cpu_to_le32(range);
2331 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2332
2333 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2334 }
2335}
2336
2337static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2338 struct ocfs2_cached_dealloc_ctxt *dealloc,
2339 struct ocfs2_path *path, int unlink_start)
2340{
2341 int ret, i;
2342 struct ocfs2_extent_block *eb;
2343 struct ocfs2_extent_list *el;
2344 struct buffer_head *bh;
2345
2346 for(i = unlink_start; i < path_num_items(path); i++) {
2347 bh = path->p_node[i].bh;
2348
2349 eb = (struct ocfs2_extent_block *)bh->b_data;
2350 /*
2351 * Not all nodes might have had their final count
2352 * decremented by the caller - handle this here.
2353 */
2354 el = &eb->h_list;
2355 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2356 mlog(ML_ERROR,
2357 "Inode %llu, attempted to remove extent block "
2358 "%llu with %u records\n",
2359 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2360 (unsigned long long)le64_to_cpu(eb->h_blkno),
2361 le16_to_cpu(el->l_next_free_rec));
2362
2363 ocfs2_journal_dirty(handle, bh);
2364 ocfs2_remove_from_cache(inode, bh);
2365 continue;
2366 }
2367
2368 el->l_next_free_rec = 0;
2369 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2370
2371 ocfs2_journal_dirty(handle, bh);
2372
2373 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2374 if (ret)
2375 mlog_errno(ret);
2376
2377 ocfs2_remove_from_cache(inode, bh);
2378 }
2379}
2380
2381static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2382 struct ocfs2_path *left_path,
2383 struct ocfs2_path *right_path,
2384 int subtree_index,
2385 struct ocfs2_cached_dealloc_ctxt *dealloc)
2386{
2387 int i;
2388 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2389 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2390 struct ocfs2_extent_list *el;
2391 struct ocfs2_extent_block *eb;
2392
2393 el = path_leaf_el(left_path);
2394
2395 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2396
2397 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2398 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2399 break;
2400
2401 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2402
2403 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2404 le16_add_cpu(&root_el->l_next_free_rec, -1);
2405
2406 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2407 eb->h_next_leaf_blk = 0;
2408
2409 ocfs2_journal_dirty(handle, root_bh);
2410 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2411
2412 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2413 subtree_index + 1);
2414}
2415
2416static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2417 struct ocfs2_path *left_path,
2418 struct ocfs2_path *right_path,
2419 int subtree_index,
2420 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002421 int *deleted,
2422 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002423{
2424 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002425 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002426 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2427 struct ocfs2_extent_block *eb;
2428
2429 *deleted = 0;
2430
2431 right_leaf_el = path_leaf_el(right_path);
2432 left_leaf_el = path_leaf_el(left_path);
2433 root_bh = left_path->p_node[subtree_index].bh;
2434 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2435
2436 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2437 return 0;
2438
2439 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2440 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2441 /*
2442 * It's legal for us to proceed if the right leaf is
2443 * the rightmost one and it has an empty extent. There
2444 * are two cases to handle - whether the leaf will be
2445 * empty after removal or not. If the leaf isn't empty
2446 * then just remove the empty extent up front. The
2447 * next block will handle empty leaves by flagging
2448 * them for unlink.
2449 *
2450 * Non rightmost leaves will throw -EAGAIN and the
2451 * caller can manually move the subtree and retry.
2452 */
2453
2454 if (eb->h_next_leaf_blk != 0ULL)
2455 return -EAGAIN;
2456
2457 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2458 ret = ocfs2_journal_access(handle, inode,
2459 path_leaf_bh(right_path),
2460 OCFS2_JOURNAL_ACCESS_WRITE);
2461 if (ret) {
2462 mlog_errno(ret);
2463 goto out;
2464 }
2465
2466 ocfs2_remove_empty_extent(right_leaf_el);
2467 } else
2468 right_has_empty = 1;
2469 }
2470
2471 if (eb->h_next_leaf_blk == 0ULL &&
2472 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2473 /*
2474 * We have to update i_last_eb_blk during the meta
2475 * data delete.
2476 */
Tao Mae7d4cb62008-08-18 17:38:44 +08002477 ret = ocfs2_journal_access(handle, inode, et_root_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07002478 OCFS2_JOURNAL_ACCESS_WRITE);
2479 if (ret) {
2480 mlog_errno(ret);
2481 goto out;
2482 }
2483
2484 del_right_subtree = 1;
2485 }
2486
2487 /*
2488 * Getting here with an empty extent in the right path implies
2489 * that it's the rightmost path and will be deleted.
2490 */
2491 BUG_ON(right_has_empty && !del_right_subtree);
2492
2493 ret = ocfs2_journal_access(handle, inode, root_bh,
2494 OCFS2_JOURNAL_ACCESS_WRITE);
2495 if (ret) {
2496 mlog_errno(ret);
2497 goto out;
2498 }
2499
2500 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2501 ret = ocfs2_journal_access(handle, inode,
2502 right_path->p_node[i].bh,
2503 OCFS2_JOURNAL_ACCESS_WRITE);
2504 if (ret) {
2505 mlog_errno(ret);
2506 goto out;
2507 }
2508
2509 ret = ocfs2_journal_access(handle, inode,
2510 left_path->p_node[i].bh,
2511 OCFS2_JOURNAL_ACCESS_WRITE);
2512 if (ret) {
2513 mlog_errno(ret);
2514 goto out;
2515 }
2516 }
2517
2518 if (!right_has_empty) {
2519 /*
2520 * Only do this if we're moving a real
2521 * record. Otherwise, the action is delayed until
2522 * after removal of the right path in which case we
2523 * can do a simple shift to remove the empty extent.
2524 */
2525 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2526 memset(&right_leaf_el->l_recs[0], 0,
2527 sizeof(struct ocfs2_extent_rec));
2528 }
2529 if (eb->h_next_leaf_blk == 0ULL) {
2530 /*
2531 * Move recs over to get rid of empty extent, decrease
2532 * next_free. This is allowed to remove the last
2533 * extent in our leaf (setting l_next_free_rec to
2534 * zero) - the delete code below won't care.
2535 */
2536 ocfs2_remove_empty_extent(right_leaf_el);
2537 }
2538
2539 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2540 if (ret)
2541 mlog_errno(ret);
2542 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2543 if (ret)
2544 mlog_errno(ret);
2545
2546 if (del_right_subtree) {
2547 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2548 subtree_index, dealloc);
2549 ocfs2_update_edge_lengths(inode, handle, left_path);
2550
2551 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002552 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002553
2554 /*
2555 * Removal of the extent in the left leaf was skipped
2556 * above so we could delete the right path
2557 * 1st.
2558 */
2559 if (right_has_empty)
2560 ocfs2_remove_empty_extent(left_leaf_el);
2561
Tao Mae7d4cb62008-08-18 17:38:44 +08002562 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002563 if (ret)
2564 mlog_errno(ret);
2565
2566 *deleted = 1;
2567 } else
2568 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2569 subtree_index);
2570
2571out:
2572 return ret;
2573}
2574
2575/*
2576 * Given a full path, determine what cpos value would return us a path
2577 * containing the leaf immediately to the right of the current one.
2578 *
2579 * Will return zero if the path passed in is already the rightmost path.
2580 *
2581 * This looks similar, but is subtly different to
2582 * ocfs2_find_cpos_for_left_leaf().
2583 */
2584static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2585 struct ocfs2_path *path, u32 *cpos)
2586{
2587 int i, j, ret = 0;
2588 u64 blkno;
2589 struct ocfs2_extent_list *el;
2590
2591 *cpos = 0;
2592
2593 if (path->p_tree_depth == 0)
2594 return 0;
2595
2596 blkno = path_leaf_bh(path)->b_blocknr;
2597
2598 /* Start at the tree node just above the leaf and work our way up. */
2599 i = path->p_tree_depth - 1;
2600 while (i >= 0) {
2601 int next_free;
2602
2603 el = path->p_node[i].el;
2604
2605 /*
2606 * Find the extent record just after the one in our
2607 * path.
2608 */
2609 next_free = le16_to_cpu(el->l_next_free_rec);
2610 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2611 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2612 if (j == (next_free - 1)) {
2613 if (i == 0) {
2614 /*
2615 * We've determined that the
2616 * path specified is already
2617 * the rightmost one - return a
2618 * cpos of zero.
2619 */
2620 goto out;
2621 }
2622 /*
2623 * The rightmost record points to our
2624 * leaf - we need to travel up the
2625 * tree one level.
2626 */
2627 goto next_node;
2628 }
2629
2630 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2631 goto out;
2632 }
2633 }
2634
2635 /*
2636 * If we got here, we never found a valid node where
2637 * the tree indicated one should be.
2638 */
2639 ocfs2_error(sb,
2640 "Invalid extent tree at extent block %llu\n",
2641 (unsigned long long)blkno);
2642 ret = -EROFS;
2643 goto out;
2644
2645next_node:
2646 blkno = path->p_node[i].bh->b_blocknr;
2647 i--;
2648 }
2649
2650out:
2651 return ret;
2652}
2653
2654static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2655 handle_t *handle,
2656 struct buffer_head *bh,
2657 struct ocfs2_extent_list *el)
2658{
2659 int ret;
2660
2661 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2662 return 0;
2663
2664 ret = ocfs2_journal_access(handle, inode, bh,
2665 OCFS2_JOURNAL_ACCESS_WRITE);
2666 if (ret) {
2667 mlog_errno(ret);
2668 goto out;
2669 }
2670
2671 ocfs2_remove_empty_extent(el);
2672
2673 ret = ocfs2_journal_dirty(handle, bh);
2674 if (ret)
2675 mlog_errno(ret);
2676
2677out:
2678 return ret;
2679}
2680
2681static int __ocfs2_rotate_tree_left(struct inode *inode,
2682 handle_t *handle, int orig_credits,
2683 struct ocfs2_path *path,
2684 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002685 struct ocfs2_path **empty_extent_path,
2686 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002687{
2688 int ret, subtree_root, deleted;
2689 u32 right_cpos;
2690 struct ocfs2_path *left_path = NULL;
2691 struct ocfs2_path *right_path = NULL;
2692
2693 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2694
2695 *empty_extent_path = NULL;
2696
2697 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2698 &right_cpos);
2699 if (ret) {
2700 mlog_errno(ret);
2701 goto out;
2702 }
2703
Joel Beckerffdd7a52008-10-17 22:32:01 -07002704 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002705 if (!left_path) {
2706 ret = -ENOMEM;
2707 mlog_errno(ret);
2708 goto out;
2709 }
2710
2711 ocfs2_cp_path(left_path, path);
2712
Joel Beckerffdd7a52008-10-17 22:32:01 -07002713 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002714 if (!right_path) {
2715 ret = -ENOMEM;
2716 mlog_errno(ret);
2717 goto out;
2718 }
2719
2720 while (right_cpos) {
2721 ret = ocfs2_find_path(inode, right_path, right_cpos);
2722 if (ret) {
2723 mlog_errno(ret);
2724 goto out;
2725 }
2726
2727 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2728 right_path);
2729
2730 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2731 subtree_root,
2732 (unsigned long long)
2733 right_path->p_node[subtree_root].bh->b_blocknr,
2734 right_path->p_tree_depth);
2735
2736 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2737 orig_credits, left_path);
2738 if (ret) {
2739 mlog_errno(ret);
2740 goto out;
2741 }
2742
Mark Fashehe8aed342007-12-03 16:43:01 -08002743 /*
2744 * Caller might still want to make changes to the
2745 * tree root, so re-add it to the journal here.
2746 */
2747 ret = ocfs2_journal_access(handle, inode,
2748 path_root_bh(left_path),
2749 OCFS2_JOURNAL_ACCESS_WRITE);
2750 if (ret) {
2751 mlog_errno(ret);
2752 goto out;
2753 }
2754
Mark Fasheh328d5752007-06-18 10:48:04 -07002755 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2756 right_path, subtree_root,
Tao Mae7d4cb62008-08-18 17:38:44 +08002757 dealloc, &deleted, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002758 if (ret == -EAGAIN) {
2759 /*
2760 * The rotation has to temporarily stop due to
2761 * the right subtree having an empty
2762 * extent. Pass it back to the caller for a
2763 * fixup.
2764 */
2765 *empty_extent_path = right_path;
2766 right_path = NULL;
2767 goto out;
2768 }
2769 if (ret) {
2770 mlog_errno(ret);
2771 goto out;
2772 }
2773
2774 /*
2775 * The subtree rotate might have removed records on
2776 * the rightmost edge. If so, then rotation is
2777 * complete.
2778 */
2779 if (deleted)
2780 break;
2781
2782 ocfs2_mv_path(left_path, right_path);
2783
2784 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2785 &right_cpos);
2786 if (ret) {
2787 mlog_errno(ret);
2788 goto out;
2789 }
2790 }
2791
2792out:
2793 ocfs2_free_path(right_path);
2794 ocfs2_free_path(left_path);
2795
2796 return ret;
2797}
2798
2799static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08002800 struct ocfs2_path *path,
2801 struct ocfs2_cached_dealloc_ctxt *dealloc,
2802 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002803{
2804 int ret, subtree_index;
2805 u32 cpos;
2806 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07002807 struct ocfs2_extent_block *eb;
2808 struct ocfs2_extent_list *el;
2809
Mark Fasheh328d5752007-06-18 10:48:04 -07002810
Joel Becker35dc0aa2008-08-20 16:25:06 -07002811 ret = ocfs2_et_sanity_check(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002812 if (ret)
2813 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07002814 /*
2815 * There's two ways we handle this depending on
2816 * whether path is the only existing one.
2817 */
2818 ret = ocfs2_extend_rotate_transaction(handle, 0,
2819 handle->h_buffer_credits,
2820 path);
2821 if (ret) {
2822 mlog_errno(ret);
2823 goto out;
2824 }
2825
2826 ret = ocfs2_journal_access_path(inode, handle, path);
2827 if (ret) {
2828 mlog_errno(ret);
2829 goto out;
2830 }
2831
2832 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2833 if (ret) {
2834 mlog_errno(ret);
2835 goto out;
2836 }
2837
2838 if (cpos) {
2839 /*
2840 * We have a path to the left of this one - it needs
2841 * an update too.
2842 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07002843 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002844 if (!left_path) {
2845 ret = -ENOMEM;
2846 mlog_errno(ret);
2847 goto out;
2848 }
2849
2850 ret = ocfs2_find_path(inode, left_path, cpos);
2851 if (ret) {
2852 mlog_errno(ret);
2853 goto out;
2854 }
2855
2856 ret = ocfs2_journal_access_path(inode, handle, left_path);
2857 if (ret) {
2858 mlog_errno(ret);
2859 goto out;
2860 }
2861
2862 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2863
2864 ocfs2_unlink_subtree(inode, handle, left_path, path,
2865 subtree_index, dealloc);
2866 ocfs2_update_edge_lengths(inode, handle, left_path);
2867
2868 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002869 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002870 } else {
2871 /*
2872 * 'path' is also the leftmost path which
2873 * means it must be the only one. This gets
2874 * handled differently because we want to
2875 * revert the inode back to having extents
2876 * in-line.
2877 */
2878 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2879
Joel Beckerce1d9ea2008-08-20 16:30:07 -07002880 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07002881 el->l_tree_depth = 0;
2882 el->l_next_free_rec = 0;
2883 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2884
Joel Becker35dc0aa2008-08-20 16:25:06 -07002885 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07002886 }
2887
2888 ocfs2_journal_dirty(handle, path_root_bh(path));
2889
2890out:
2891 ocfs2_free_path(left_path);
2892 return ret;
2893}
2894
2895/*
2896 * Left rotation of btree records.
2897 *
2898 * In many ways, this is (unsurprisingly) the opposite of right
2899 * rotation. We start at some non-rightmost path containing an empty
2900 * extent in the leaf block. The code works its way to the rightmost
2901 * path by rotating records to the left in every subtree.
2902 *
2903 * This is used by any code which reduces the number of extent records
2904 * in a leaf. After removal, an empty record should be placed in the
2905 * leftmost list position.
2906 *
2907 * This won't handle a length update of the rightmost path records if
2908 * the rightmost tree leaf record is removed so the caller is
2909 * responsible for detecting and correcting that.
2910 */
2911static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2912 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002913 struct ocfs2_cached_dealloc_ctxt *dealloc,
2914 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002915{
2916 int ret, orig_credits = handle->h_buffer_credits;
2917 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2918 struct ocfs2_extent_block *eb;
2919 struct ocfs2_extent_list *el;
2920
2921 el = path_leaf_el(path);
2922 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2923 return 0;
2924
2925 if (path->p_tree_depth == 0) {
2926rightmost_no_delete:
2927 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08002928 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07002929 * it up front.
2930 */
2931 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2932 path_leaf_bh(path),
2933 path_leaf_el(path));
2934 if (ret)
2935 mlog_errno(ret);
2936 goto out;
2937 }
2938
2939 /*
2940 * Handle rightmost branch now. There's several cases:
2941 * 1) simple rotation leaving records in there. That's trivial.
2942 * 2) rotation requiring a branch delete - there's no more
2943 * records left. Two cases of this:
2944 * a) There are branches to the left.
2945 * b) This is also the leftmost (the only) branch.
2946 *
2947 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2948 * 2a) we need the left branch so that we can update it with the unlink
2949 * 2b) we need to bring the inode back to inline extents.
2950 */
2951
2952 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2953 el = &eb->h_list;
2954 if (eb->h_next_leaf_blk == 0) {
2955 /*
2956 * This gets a bit tricky if we're going to delete the
2957 * rightmost path. Get the other cases out of the way
2958 * 1st.
2959 */
2960 if (le16_to_cpu(el->l_next_free_rec) > 1)
2961 goto rightmost_no_delete;
2962
2963 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2964 ret = -EIO;
2965 ocfs2_error(inode->i_sb,
2966 "Inode %llu has empty extent block at %llu",
2967 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2968 (unsigned long long)le64_to_cpu(eb->h_blkno));
2969 goto out;
2970 }
2971
2972 /*
2973 * XXX: The caller can not trust "path" any more after
2974 * this as it will have been deleted. What do we do?
2975 *
2976 * In theory the rotate-for-merge code will never get
2977 * here because it'll always ask for a rotate in a
2978 * nonempty list.
2979 */
2980
2981 ret = ocfs2_remove_rightmost_path(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002982 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002983 if (ret)
2984 mlog_errno(ret);
2985 goto out;
2986 }
2987
2988 /*
2989 * Now we can loop, remembering the path we get from -EAGAIN
2990 * and restarting from there.
2991 */
2992try_rotate:
2993 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002994 dealloc, &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002995 if (ret && ret != -EAGAIN) {
2996 mlog_errno(ret);
2997 goto out;
2998 }
2999
3000 while (ret == -EAGAIN) {
3001 tmp_path = restart_path;
3002 restart_path = NULL;
3003
3004 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
3005 tmp_path, dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003006 &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003007 if (ret && ret != -EAGAIN) {
3008 mlog_errno(ret);
3009 goto out;
3010 }
3011
3012 ocfs2_free_path(tmp_path);
3013 tmp_path = NULL;
3014
3015 if (ret == 0)
3016 goto try_rotate;
3017 }
3018
3019out:
3020 ocfs2_free_path(tmp_path);
3021 ocfs2_free_path(restart_path);
3022 return ret;
3023}
3024
3025static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3026 int index)
3027{
3028 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3029 unsigned int size;
3030
3031 if (rec->e_leaf_clusters == 0) {
3032 /*
3033 * We consumed all of the merged-from record. An empty
3034 * extent cannot exist anywhere but the 1st array
3035 * position, so move things over if the merged-from
3036 * record doesn't occupy that position.
3037 *
3038 * This creates a new empty extent so the caller
3039 * should be smart enough to have removed any existing
3040 * ones.
3041 */
3042 if (index > 0) {
3043 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3044 size = index * sizeof(struct ocfs2_extent_rec);
3045 memmove(&el->l_recs[1], &el->l_recs[0], size);
3046 }
3047
3048 /*
3049 * Always memset - the caller doesn't check whether it
3050 * created an empty extent, so there could be junk in
3051 * the other fields.
3052 */
3053 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3054 }
3055}
3056
Tao Ma677b9752008-01-30 14:21:05 +08003057static int ocfs2_get_right_path(struct inode *inode,
3058 struct ocfs2_path *left_path,
3059 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003060{
3061 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003062 u32 right_cpos;
3063 struct ocfs2_path *right_path = NULL;
3064 struct ocfs2_extent_list *left_el;
3065
3066 *ret_right_path = NULL;
3067
3068 /* This function shouldn't be called for non-trees. */
3069 BUG_ON(left_path->p_tree_depth == 0);
3070
3071 left_el = path_leaf_el(left_path);
3072 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3073
3074 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3075 &right_cpos);
3076 if (ret) {
3077 mlog_errno(ret);
3078 goto out;
3079 }
3080
3081 /* This function shouldn't be called for the rightmost leaf. */
3082 BUG_ON(right_cpos == 0);
3083
Joel Beckerffdd7a52008-10-17 22:32:01 -07003084 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003085 if (!right_path) {
3086 ret = -ENOMEM;
3087 mlog_errno(ret);
3088 goto out;
3089 }
3090
3091 ret = ocfs2_find_path(inode, right_path, right_cpos);
3092 if (ret) {
3093 mlog_errno(ret);
3094 goto out;
3095 }
3096
3097 *ret_right_path = right_path;
3098out:
3099 if (ret)
3100 ocfs2_free_path(right_path);
3101 return ret;
3102}
3103
3104/*
3105 * Remove split_rec clusters from the record at index and merge them
3106 * onto the beginning of the record "next" to it.
3107 * For index < l_count - 1, the next means the extent rec at index + 1.
3108 * For index == l_count - 1, the "next" means the 1st extent rec of the
3109 * next extent block.
3110 */
3111static int ocfs2_merge_rec_right(struct inode *inode,
3112 struct ocfs2_path *left_path,
3113 handle_t *handle,
3114 struct ocfs2_extent_rec *split_rec,
3115 int index)
3116{
3117 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003118 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3119 struct ocfs2_extent_rec *left_rec;
3120 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003121 struct ocfs2_extent_list *right_el;
3122 struct ocfs2_path *right_path = NULL;
3123 int subtree_index = 0;
3124 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3125 struct buffer_head *bh = path_leaf_bh(left_path);
3126 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003127
3128 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003129 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003130
Al Viro9d8df6a2008-05-21 06:32:11 +01003131 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003132 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3133 /* we meet with a cross extent block merge. */
3134 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3135 if (ret) {
3136 mlog_errno(ret);
3137 goto out;
3138 }
3139
3140 right_el = path_leaf_el(right_path);
3141 next_free = le16_to_cpu(right_el->l_next_free_rec);
3142 BUG_ON(next_free <= 0);
3143 right_rec = &right_el->l_recs[0];
3144 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003145 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003146 right_rec = &right_el->l_recs[1];
3147 }
3148
3149 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3150 le16_to_cpu(left_rec->e_leaf_clusters) !=
3151 le32_to_cpu(right_rec->e_cpos));
3152
3153 subtree_index = ocfs2_find_subtree_root(inode,
3154 left_path, right_path);
3155
3156 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3157 handle->h_buffer_credits,
3158 right_path);
3159 if (ret) {
3160 mlog_errno(ret);
3161 goto out;
3162 }
3163
3164 root_bh = left_path->p_node[subtree_index].bh;
3165 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3166
3167 ret = ocfs2_journal_access(handle, inode, root_bh,
3168 OCFS2_JOURNAL_ACCESS_WRITE);
3169 if (ret) {
3170 mlog_errno(ret);
3171 goto out;
3172 }
3173
3174 for (i = subtree_index + 1;
3175 i < path_num_items(right_path); i++) {
3176 ret = ocfs2_journal_access(handle, inode,
3177 right_path->p_node[i].bh,
3178 OCFS2_JOURNAL_ACCESS_WRITE);
3179 if (ret) {
3180 mlog_errno(ret);
3181 goto out;
3182 }
3183
3184 ret = ocfs2_journal_access(handle, inode,
3185 left_path->p_node[i].bh,
3186 OCFS2_JOURNAL_ACCESS_WRITE);
3187 if (ret) {
3188 mlog_errno(ret);
3189 goto out;
3190 }
3191 }
3192
3193 } else {
3194 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3195 right_rec = &el->l_recs[index + 1];
3196 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003197
3198 ret = ocfs2_journal_access(handle, inode, bh,
3199 OCFS2_JOURNAL_ACCESS_WRITE);
3200 if (ret) {
3201 mlog_errno(ret);
3202 goto out;
3203 }
3204
3205 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3206
3207 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3208 le64_add_cpu(&right_rec->e_blkno,
3209 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3210 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3211
3212 ocfs2_cleanup_merge(el, index);
3213
3214 ret = ocfs2_journal_dirty(handle, bh);
3215 if (ret)
3216 mlog_errno(ret);
3217
Tao Ma677b9752008-01-30 14:21:05 +08003218 if (right_path) {
3219 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3220 if (ret)
3221 mlog_errno(ret);
3222
3223 ocfs2_complete_edge_insert(inode, handle, left_path,
3224 right_path, subtree_index);
3225 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003226out:
Tao Ma677b9752008-01-30 14:21:05 +08003227 if (right_path)
3228 ocfs2_free_path(right_path);
3229 return ret;
3230}
3231
3232static int ocfs2_get_left_path(struct inode *inode,
3233 struct ocfs2_path *right_path,
3234 struct ocfs2_path **ret_left_path)
3235{
3236 int ret;
3237 u32 left_cpos;
3238 struct ocfs2_path *left_path = NULL;
3239
3240 *ret_left_path = NULL;
3241
3242 /* This function shouldn't be called for non-trees. */
3243 BUG_ON(right_path->p_tree_depth == 0);
3244
3245 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3246 right_path, &left_cpos);
3247 if (ret) {
3248 mlog_errno(ret);
3249 goto out;
3250 }
3251
3252 /* This function shouldn't be called for the leftmost leaf. */
3253 BUG_ON(left_cpos == 0);
3254
Joel Beckerffdd7a52008-10-17 22:32:01 -07003255 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003256 if (!left_path) {
3257 ret = -ENOMEM;
3258 mlog_errno(ret);
3259 goto out;
3260 }
3261
3262 ret = ocfs2_find_path(inode, left_path, left_cpos);
3263 if (ret) {
3264 mlog_errno(ret);
3265 goto out;
3266 }
3267
3268 *ret_left_path = left_path;
3269out:
3270 if (ret)
3271 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003272 return ret;
3273}
3274
3275/*
3276 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003277 * onto the tail of the record "before" it.
3278 * For index > 0, the "before" means the extent rec at index - 1.
3279 *
3280 * For index == 0, the "before" means the last record of the previous
3281 * extent block. And there is also a situation that we may need to
3282 * remove the rightmost leaf extent block in the right_path and change
3283 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003284 */
Tao Ma677b9752008-01-30 14:21:05 +08003285static int ocfs2_merge_rec_left(struct inode *inode,
3286 struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003287 handle_t *handle,
3288 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003289 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003290 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003291 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003292{
Tao Ma677b9752008-01-30 14:21:05 +08003293 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003294 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3295 struct ocfs2_extent_rec *left_rec;
3296 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003297 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3298 struct buffer_head *bh = path_leaf_bh(right_path);
3299 struct buffer_head *root_bh = NULL;
3300 struct ocfs2_path *left_path = NULL;
3301 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003302
Tao Ma677b9752008-01-30 14:21:05 +08003303 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003304
Mark Fasheh328d5752007-06-18 10:48:04 -07003305 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003306 if (index == 0) {
3307 /* we meet with a cross extent block merge. */
3308 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3309 if (ret) {
3310 mlog_errno(ret);
3311 goto out;
3312 }
3313
3314 left_el = path_leaf_el(left_path);
3315 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3316 le16_to_cpu(left_el->l_count));
3317
3318 left_rec = &left_el->l_recs[
3319 le16_to_cpu(left_el->l_next_free_rec) - 1];
3320 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3321 le16_to_cpu(left_rec->e_leaf_clusters) !=
3322 le32_to_cpu(split_rec->e_cpos));
3323
3324 subtree_index = ocfs2_find_subtree_root(inode,
3325 left_path, right_path);
3326
3327 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3328 handle->h_buffer_credits,
3329 left_path);
3330 if (ret) {
3331 mlog_errno(ret);
3332 goto out;
3333 }
3334
3335 root_bh = left_path->p_node[subtree_index].bh;
3336 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3337
3338 ret = ocfs2_journal_access(handle, inode, root_bh,
3339 OCFS2_JOURNAL_ACCESS_WRITE);
3340 if (ret) {
3341 mlog_errno(ret);
3342 goto out;
3343 }
3344
3345 for (i = subtree_index + 1;
3346 i < path_num_items(right_path); i++) {
3347 ret = ocfs2_journal_access(handle, inode,
3348 right_path->p_node[i].bh,
3349 OCFS2_JOURNAL_ACCESS_WRITE);
3350 if (ret) {
3351 mlog_errno(ret);
3352 goto out;
3353 }
3354
3355 ret = ocfs2_journal_access(handle, inode,
3356 left_path->p_node[i].bh,
3357 OCFS2_JOURNAL_ACCESS_WRITE);
3358 if (ret) {
3359 mlog_errno(ret);
3360 goto out;
3361 }
3362 }
3363 } else {
3364 left_rec = &el->l_recs[index - 1];
3365 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3366 has_empty_extent = 1;
3367 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003368
3369 ret = ocfs2_journal_access(handle, inode, bh,
3370 OCFS2_JOURNAL_ACCESS_WRITE);
3371 if (ret) {
3372 mlog_errno(ret);
3373 goto out;
3374 }
3375
3376 if (has_empty_extent && index == 1) {
3377 /*
3378 * The easy case - we can just plop the record right in.
3379 */
3380 *left_rec = *split_rec;
3381
3382 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003383 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003384 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003385
3386 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3387 le64_add_cpu(&right_rec->e_blkno,
3388 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3389 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3390
3391 ocfs2_cleanup_merge(el, index);
3392
3393 ret = ocfs2_journal_dirty(handle, bh);
3394 if (ret)
3395 mlog_errno(ret);
3396
Tao Ma677b9752008-01-30 14:21:05 +08003397 if (left_path) {
3398 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3399 if (ret)
3400 mlog_errno(ret);
3401
3402 /*
3403 * In the situation that the right_rec is empty and the extent
3404 * block is empty also, ocfs2_complete_edge_insert can't handle
3405 * it and we need to delete the right extent block.
3406 */
3407 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3408 le16_to_cpu(el->l_next_free_rec) == 1) {
3409
3410 ret = ocfs2_remove_rightmost_path(inode, handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003411 right_path,
3412 dealloc, et);
Tao Ma677b9752008-01-30 14:21:05 +08003413 if (ret) {
3414 mlog_errno(ret);
3415 goto out;
3416 }
3417
3418 /* Now the rightmost extent block has been deleted.
3419 * So we use the new rightmost path.
3420 */
3421 ocfs2_mv_path(right_path, left_path);
3422 left_path = NULL;
3423 } else
3424 ocfs2_complete_edge_insert(inode, handle, left_path,
3425 right_path, subtree_index);
3426 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003427out:
Tao Ma677b9752008-01-30 14:21:05 +08003428 if (left_path)
3429 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003430 return ret;
3431}
3432
3433static int ocfs2_try_to_merge_extent(struct inode *inode,
3434 handle_t *handle,
Tao Ma677b9752008-01-30 14:21:05 +08003435 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003436 int split_index,
3437 struct ocfs2_extent_rec *split_rec,
3438 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003439 struct ocfs2_merge_ctxt *ctxt,
3440 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003441
3442{
Tao Mao518d7262007-08-28 17:25:35 -07003443 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003444 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003445 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3446
3447 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3448
Tao Mao518d7262007-08-28 17:25:35 -07003449 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3450 /*
3451 * The merge code will need to create an empty
3452 * extent to take the place of the newly
3453 * emptied slot. Remove any pre-existing empty
3454 * extents - having more than one in a leaf is
3455 * illegal.
3456 */
Tao Ma677b9752008-01-30 14:21:05 +08003457 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003458 dealloc, et);
Tao Mao518d7262007-08-28 17:25:35 -07003459 if (ret) {
3460 mlog_errno(ret);
3461 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003462 }
Tao Mao518d7262007-08-28 17:25:35 -07003463 split_index--;
3464 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003465 }
3466
3467 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3468 /*
3469 * Left-right contig implies this.
3470 */
3471 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003472
3473 /*
3474 * Since the leftright insert always covers the entire
3475 * extent, this call will delete the insert record
3476 * entirely, resulting in an empty extent record added to
3477 * the extent block.
3478 *
3479 * Since the adding of an empty extent shifts
3480 * everything back to the right, there's no need to
3481 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003482 *
3483 * When the split_index is zero, we need to merge it to the
3484 * prevoius extent block. It is more efficient and easier
3485 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003486 */
Tao Ma677b9752008-01-30 14:21:05 +08003487 ret = ocfs2_merge_rec_right(inode, path,
3488 handle, split_rec,
3489 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003490 if (ret) {
3491 mlog_errno(ret);
3492 goto out;
3493 }
3494
3495 /*
3496 * We can only get this from logic error above.
3497 */
3498 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3499
Tao Ma677b9752008-01-30 14:21:05 +08003500 /* The merge left us with an empty extent, remove it. */
Tao Mae7d4cb62008-08-18 17:38:44 +08003501 ret = ocfs2_rotate_tree_left(inode, handle, path,
3502 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003503 if (ret) {
3504 mlog_errno(ret);
3505 goto out;
3506 }
Tao Ma677b9752008-01-30 14:21:05 +08003507
Mark Fasheh328d5752007-06-18 10:48:04 -07003508 rec = &el->l_recs[split_index];
3509
3510 /*
3511 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003512 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003513 */
Tao Ma677b9752008-01-30 14:21:05 +08003514 ret = ocfs2_merge_rec_left(inode, path,
3515 handle, rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003516 dealloc, et,
Tao Ma677b9752008-01-30 14:21:05 +08003517 split_index);
3518
Mark Fasheh328d5752007-06-18 10:48:04 -07003519 if (ret) {
3520 mlog_errno(ret);
3521 goto out;
3522 }
3523
Tao Ma677b9752008-01-30 14:21:05 +08003524 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003525 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003526 /*
3527 * Error from this last rotate is not critical, so
3528 * print but don't bubble it up.
3529 */
3530 if (ret)
3531 mlog_errno(ret);
3532 ret = 0;
3533 } else {
3534 /*
3535 * Merge a record to the left or right.
3536 *
3537 * 'contig_type' is relative to the existing record,
3538 * so for example, if we're "right contig", it's to
3539 * the record on the left (hence the left merge).
3540 */
3541 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3542 ret = ocfs2_merge_rec_left(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003543 path,
3544 handle, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003545 dealloc, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003546 split_index);
3547 if (ret) {
3548 mlog_errno(ret);
3549 goto out;
3550 }
3551 } else {
3552 ret = ocfs2_merge_rec_right(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003553 path,
3554 handle, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003555 split_index);
3556 if (ret) {
3557 mlog_errno(ret);
3558 goto out;
3559 }
3560 }
3561
3562 if (ctxt->c_split_covers_rec) {
3563 /*
3564 * The merge may have left an empty extent in
3565 * our leaf. Try to rotate it away.
3566 */
Tao Ma677b9752008-01-30 14:21:05 +08003567 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003568 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003569 if (ret)
3570 mlog_errno(ret);
3571 ret = 0;
3572 }
3573 }
3574
3575out:
3576 return ret;
3577}
3578
3579static void ocfs2_subtract_from_rec(struct super_block *sb,
3580 enum ocfs2_split_type split,
3581 struct ocfs2_extent_rec *rec,
3582 struct ocfs2_extent_rec *split_rec)
3583{
3584 u64 len_blocks;
3585
3586 len_blocks = ocfs2_clusters_to_blocks(sb,
3587 le16_to_cpu(split_rec->e_leaf_clusters));
3588
3589 if (split == SPLIT_LEFT) {
3590 /*
3591 * Region is on the left edge of the existing
3592 * record.
3593 */
3594 le32_add_cpu(&rec->e_cpos,
3595 le16_to_cpu(split_rec->e_leaf_clusters));
3596 le64_add_cpu(&rec->e_blkno, len_blocks);
3597 le16_add_cpu(&rec->e_leaf_clusters,
3598 -le16_to_cpu(split_rec->e_leaf_clusters));
3599 } else {
3600 /*
3601 * Region is on the right edge of the existing
3602 * record.
3603 */
3604 le16_add_cpu(&rec->e_leaf_clusters,
3605 -le16_to_cpu(split_rec->e_leaf_clusters));
3606 }
3607}
3608
Mark Fashehdcd05382007-01-16 11:32:23 -08003609/*
3610 * Do the final bits of extent record insertion at the target leaf
3611 * list. If this leaf is part of an allocation tree, it is assumed
3612 * that the tree above has been prepared.
3613 */
3614static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3615 struct ocfs2_extent_list *el,
3616 struct ocfs2_insert_type *insert,
3617 struct inode *inode)
3618{
3619 int i = insert->ins_contig_index;
3620 unsigned int range;
3621 struct ocfs2_extent_rec *rec;
3622
Mark Fashehe48edee2007-03-07 16:46:57 -08003623 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003624
Mark Fasheh328d5752007-06-18 10:48:04 -07003625 if (insert->ins_split != SPLIT_NONE) {
3626 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3627 BUG_ON(i == -1);
3628 rec = &el->l_recs[i];
3629 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3630 insert_rec);
3631 goto rotate;
3632 }
3633
Mark Fashehdcd05382007-01-16 11:32:23 -08003634 /*
3635 * Contiguous insert - either left or right.
3636 */
3637 if (insert->ins_contig != CONTIG_NONE) {
3638 rec = &el->l_recs[i];
3639 if (insert->ins_contig == CONTIG_LEFT) {
3640 rec->e_blkno = insert_rec->e_blkno;
3641 rec->e_cpos = insert_rec->e_cpos;
3642 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003643 le16_add_cpu(&rec->e_leaf_clusters,
3644 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003645 return;
3646 }
3647
3648 /*
3649 * Handle insert into an empty leaf.
3650 */
3651 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3652 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3653 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3654 el->l_recs[0] = *insert_rec;
3655 el->l_next_free_rec = cpu_to_le16(1);
3656 return;
3657 }
3658
3659 /*
3660 * Appending insert.
3661 */
3662 if (insert->ins_appending == APPEND_TAIL) {
3663 i = le16_to_cpu(el->l_next_free_rec) - 1;
3664 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003665 range = le32_to_cpu(rec->e_cpos)
3666 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003667 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3668
3669 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3670 le16_to_cpu(el->l_count),
3671 "inode %lu, depth %u, count %u, next free %u, "
3672 "rec.cpos %u, rec.clusters %u, "
3673 "insert.cpos %u, insert.clusters %u\n",
3674 inode->i_ino,
3675 le16_to_cpu(el->l_tree_depth),
3676 le16_to_cpu(el->l_count),
3677 le16_to_cpu(el->l_next_free_rec),
3678 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003679 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003680 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003681 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003682 i++;
3683 el->l_recs[i] = *insert_rec;
3684 le16_add_cpu(&el->l_next_free_rec, 1);
3685 return;
3686 }
3687
Mark Fasheh328d5752007-06-18 10:48:04 -07003688rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003689 /*
3690 * Ok, we have to rotate.
3691 *
3692 * At this point, it is safe to assume that inserting into an
3693 * empty leaf and appending to a leaf have both been handled
3694 * above.
3695 *
3696 * This leaf needs to have space, either by the empty 1st
3697 * extent record, or by virtue of an l_next_rec < l_count.
3698 */
3699 ocfs2_rotate_leaf(el, insert_rec);
3700}
3701
Mark Fasheh328d5752007-06-18 10:48:04 -07003702static void ocfs2_adjust_rightmost_records(struct inode *inode,
3703 handle_t *handle,
3704 struct ocfs2_path *path,
3705 struct ocfs2_extent_rec *insert_rec)
3706{
3707 int ret, i, next_free;
3708 struct buffer_head *bh;
3709 struct ocfs2_extent_list *el;
3710 struct ocfs2_extent_rec *rec;
3711
3712 /*
3713 * Update everything except the leaf block.
3714 */
3715 for (i = 0; i < path->p_tree_depth; i++) {
3716 bh = path->p_node[i].bh;
3717 el = path->p_node[i].el;
3718
3719 next_free = le16_to_cpu(el->l_next_free_rec);
3720 if (next_free == 0) {
3721 ocfs2_error(inode->i_sb,
3722 "Dinode %llu has a bad extent list",
3723 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3724 ret = -EIO;
3725 return;
3726 }
3727
3728 rec = &el->l_recs[next_free - 1];
3729
3730 rec->e_int_clusters = insert_rec->e_cpos;
3731 le32_add_cpu(&rec->e_int_clusters,
3732 le16_to_cpu(insert_rec->e_leaf_clusters));
3733 le32_add_cpu(&rec->e_int_clusters,
3734 -le32_to_cpu(rec->e_cpos));
3735
3736 ret = ocfs2_journal_dirty(handle, bh);
3737 if (ret)
3738 mlog_errno(ret);
3739
3740 }
3741}
3742
Mark Fashehdcd05382007-01-16 11:32:23 -08003743static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3744 struct ocfs2_extent_rec *insert_rec,
3745 struct ocfs2_path *right_path,
3746 struct ocfs2_path **ret_left_path)
3747{
Mark Fasheh328d5752007-06-18 10:48:04 -07003748 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003749 struct ocfs2_extent_list *el;
3750 struct ocfs2_path *left_path = NULL;
3751
3752 *ret_left_path = NULL;
3753
3754 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003755 * This shouldn't happen for non-trees. The extent rec cluster
3756 * count manipulation below only works for interior nodes.
3757 */
3758 BUG_ON(right_path->p_tree_depth == 0);
3759
3760 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003761 * If our appending insert is at the leftmost edge of a leaf,
3762 * then we might need to update the rightmost records of the
3763 * neighboring path.
3764 */
3765 el = path_leaf_el(right_path);
3766 next_free = le16_to_cpu(el->l_next_free_rec);
3767 if (next_free == 0 ||
3768 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3769 u32 left_cpos;
3770
3771 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3772 &left_cpos);
3773 if (ret) {
3774 mlog_errno(ret);
3775 goto out;
3776 }
3777
3778 mlog(0, "Append may need a left path update. cpos: %u, "
3779 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3780 left_cpos);
3781
3782 /*
3783 * No need to worry if the append is already in the
3784 * leftmost leaf.
3785 */
3786 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07003787 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003788 if (!left_path) {
3789 ret = -ENOMEM;
3790 mlog_errno(ret);
3791 goto out;
3792 }
3793
3794 ret = ocfs2_find_path(inode, left_path, left_cpos);
3795 if (ret) {
3796 mlog_errno(ret);
3797 goto out;
3798 }
3799
3800 /*
3801 * ocfs2_insert_path() will pass the left_path to the
3802 * journal for us.
3803 */
3804 }
3805 }
3806
3807 ret = ocfs2_journal_access_path(inode, handle, right_path);
3808 if (ret) {
3809 mlog_errno(ret);
3810 goto out;
3811 }
3812
Mark Fasheh328d5752007-06-18 10:48:04 -07003813 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08003814
3815 *ret_left_path = left_path;
3816 ret = 0;
3817out:
3818 if (ret != 0)
3819 ocfs2_free_path(left_path);
3820
3821 return ret;
3822}
3823
Mark Fasheh328d5752007-06-18 10:48:04 -07003824static void ocfs2_split_record(struct inode *inode,
3825 struct ocfs2_path *left_path,
3826 struct ocfs2_path *right_path,
3827 struct ocfs2_extent_rec *split_rec,
3828 enum ocfs2_split_type split)
3829{
3830 int index;
3831 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3832 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3833 struct ocfs2_extent_rec *rec, *tmprec;
3834
3835 right_el = path_leaf_el(right_path);;
3836 if (left_path)
3837 left_el = path_leaf_el(left_path);
3838
3839 el = right_el;
3840 insert_el = right_el;
3841 index = ocfs2_search_extent_list(el, cpos);
3842 if (index != -1) {
3843 if (index == 0 && left_path) {
3844 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3845
3846 /*
3847 * This typically means that the record
3848 * started in the left path but moved to the
3849 * right as a result of rotation. We either
3850 * move the existing record to the left, or we
3851 * do the later insert there.
3852 *
3853 * In this case, the left path should always
3854 * exist as the rotate code will have passed
3855 * it back for a post-insert update.
3856 */
3857
3858 if (split == SPLIT_LEFT) {
3859 /*
3860 * It's a left split. Since we know
3861 * that the rotate code gave us an
3862 * empty extent in the left path, we
3863 * can just do the insert there.
3864 */
3865 insert_el = left_el;
3866 } else {
3867 /*
3868 * Right split - we have to move the
3869 * existing record over to the left
3870 * leaf. The insert will be into the
3871 * newly created empty extent in the
3872 * right leaf.
3873 */
3874 tmprec = &right_el->l_recs[index];
3875 ocfs2_rotate_leaf(left_el, tmprec);
3876 el = left_el;
3877
3878 memset(tmprec, 0, sizeof(*tmprec));
3879 index = ocfs2_search_extent_list(left_el, cpos);
3880 BUG_ON(index == -1);
3881 }
3882 }
3883 } else {
3884 BUG_ON(!left_path);
3885 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3886 /*
3887 * Left path is easy - we can just allow the insert to
3888 * happen.
3889 */
3890 el = left_el;
3891 insert_el = left_el;
3892 index = ocfs2_search_extent_list(el, cpos);
3893 BUG_ON(index == -1);
3894 }
3895
3896 rec = &el->l_recs[index];
3897 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3898 ocfs2_rotate_leaf(insert_el, split_rec);
3899}
3900
Mark Fashehdcd05382007-01-16 11:32:23 -08003901/*
Tao Mae7d4cb62008-08-18 17:38:44 +08003902 * This function only does inserts on an allocation b-tree. For tree
3903 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08003904 *
3905 * right_path is the path we want to do the actual insert
3906 * in. left_path should only be passed in if we need to update that
3907 * portion of the tree after an edge insert.
3908 */
3909static int ocfs2_insert_path(struct inode *inode,
3910 handle_t *handle,
3911 struct ocfs2_path *left_path,
3912 struct ocfs2_path *right_path,
3913 struct ocfs2_extent_rec *insert_rec,
3914 struct ocfs2_insert_type *insert)
3915{
3916 int ret, subtree_index;
3917 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003918
Mark Fashehdcd05382007-01-16 11:32:23 -08003919 if (left_path) {
3920 int credits = handle->h_buffer_credits;
3921
3922 /*
3923 * There's a chance that left_path got passed back to
3924 * us without being accounted for in the
3925 * journal. Extend our transaction here to be sure we
3926 * can change those blocks.
3927 */
3928 credits += left_path->p_tree_depth;
3929
3930 ret = ocfs2_extend_trans(handle, credits);
3931 if (ret < 0) {
3932 mlog_errno(ret);
3933 goto out;
3934 }
3935
3936 ret = ocfs2_journal_access_path(inode, handle, left_path);
3937 if (ret < 0) {
3938 mlog_errno(ret);
3939 goto out;
3940 }
3941 }
3942
Mark Fashehe8aed342007-12-03 16:43:01 -08003943 /*
3944 * Pass both paths to the journal. The majority of inserts
3945 * will be touching all components anyway.
3946 */
3947 ret = ocfs2_journal_access_path(inode, handle, right_path);
3948 if (ret < 0) {
3949 mlog_errno(ret);
3950 goto out;
3951 }
3952
Mark Fasheh328d5752007-06-18 10:48:04 -07003953 if (insert->ins_split != SPLIT_NONE) {
3954 /*
3955 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02003956 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07003957 * function sort it all out.
3958 */
3959 ocfs2_split_record(inode, left_path, right_path,
3960 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08003961
3962 /*
3963 * Split might have modified either leaf and we don't
3964 * have a guarantee that the later edge insert will
3965 * dirty this for us.
3966 */
3967 if (left_path)
3968 ret = ocfs2_journal_dirty(handle,
3969 path_leaf_bh(left_path));
3970 if (ret)
3971 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07003972 } else
3973 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3974 insert, inode);
Mark Fashehdcd05382007-01-16 11:32:23 -08003975
Mark Fashehdcd05382007-01-16 11:32:23 -08003976 ret = ocfs2_journal_dirty(handle, leaf_bh);
3977 if (ret)
3978 mlog_errno(ret);
3979
3980 if (left_path) {
3981 /*
3982 * The rotate code has indicated that we need to fix
3983 * up portions of the tree after the insert.
3984 *
3985 * XXX: Should we extend the transaction here?
3986 */
3987 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3988 right_path);
3989 ocfs2_complete_edge_insert(inode, handle, left_path,
3990 right_path, subtree_index);
3991 }
3992
3993 ret = 0;
3994out:
3995 return ret;
3996}
3997
3998static int ocfs2_do_insert_extent(struct inode *inode,
3999 handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004000 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004001 struct ocfs2_extent_rec *insert_rec,
4002 struct ocfs2_insert_type *type)
4003{
4004 int ret, rotate = 0;
4005 u32 cpos;
4006 struct ocfs2_path *right_path = NULL;
4007 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004008 struct ocfs2_extent_list *el;
4009
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004010 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004011
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004012 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehdcd05382007-01-16 11:32:23 -08004013 OCFS2_JOURNAL_ACCESS_WRITE);
4014 if (ret) {
4015 mlog_errno(ret);
4016 goto out;
4017 }
4018
4019 if (le16_to_cpu(el->l_tree_depth) == 0) {
4020 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4021 goto out_update_clusters;
4022 }
4023
Joel Beckerffdd7a52008-10-17 22:32:01 -07004024 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004025 if (!right_path) {
4026 ret = -ENOMEM;
4027 mlog_errno(ret);
4028 goto out;
4029 }
4030
4031 /*
4032 * Determine the path to start with. Rotations need the
4033 * rightmost path, everything else can go directly to the
4034 * target leaf.
4035 */
4036 cpos = le32_to_cpu(insert_rec->e_cpos);
4037 if (type->ins_appending == APPEND_NONE &&
4038 type->ins_contig == CONTIG_NONE) {
4039 rotate = 1;
4040 cpos = UINT_MAX;
4041 }
4042
4043 ret = ocfs2_find_path(inode, right_path, cpos);
4044 if (ret) {
4045 mlog_errno(ret);
4046 goto out;
4047 }
4048
4049 /*
4050 * Rotations and appends need special treatment - they modify
4051 * parts of the tree's above them.
4052 *
4053 * Both might pass back a path immediate to the left of the
4054 * one being inserted to. This will be cause
4055 * ocfs2_insert_path() to modify the rightmost records of
4056 * left_path to account for an edge insert.
4057 *
4058 * XXX: When modifying this code, keep in mind that an insert
4059 * can wind up skipping both of these two special cases...
4060 */
4061 if (rotate) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004062 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004063 le32_to_cpu(insert_rec->e_cpos),
4064 right_path, &left_path);
4065 if (ret) {
4066 mlog_errno(ret);
4067 goto out;
4068 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004069
4070 /*
4071 * ocfs2_rotate_tree_right() might have extended the
4072 * transaction without re-journaling our tree root.
4073 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004074 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehe8aed342007-12-03 16:43:01 -08004075 OCFS2_JOURNAL_ACCESS_WRITE);
4076 if (ret) {
4077 mlog_errno(ret);
4078 goto out;
4079 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004080 } else if (type->ins_appending == APPEND_TAIL
4081 && type->ins_contig != CONTIG_LEFT) {
4082 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4083 right_path, &left_path);
4084 if (ret) {
4085 mlog_errno(ret);
4086 goto out;
4087 }
4088 }
4089
4090 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4091 insert_rec, type);
4092 if (ret) {
4093 mlog_errno(ret);
4094 goto out;
4095 }
4096
4097out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004098 if (type->ins_split == SPLIT_NONE)
Joel Becker35dc0aa2008-08-20 16:25:06 -07004099 ocfs2_et_update_clusters(inode, et,
4100 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004101
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004102 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004103 if (ret)
4104 mlog_errno(ret);
4105
4106out:
4107 ocfs2_free_path(left_path);
4108 ocfs2_free_path(right_path);
4109
4110 return ret;
4111}
4112
Mark Fasheh328d5752007-06-18 10:48:04 -07004113static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08004114ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004115 struct ocfs2_extent_list *el, int index,
4116 struct ocfs2_extent_rec *split_rec)
4117{
Tao Maad5a4d72008-01-30 14:21:32 +08004118 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004119 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004120 u32 left_cpos, right_cpos;
4121 struct ocfs2_extent_rec *rec = NULL;
4122 struct ocfs2_extent_list *new_el;
4123 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4124 struct buffer_head *bh;
4125 struct ocfs2_extent_block *eb;
4126
4127 if (index > 0) {
4128 rec = &el->l_recs[index - 1];
4129 } else if (path->p_tree_depth > 0) {
4130 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4131 path, &left_cpos);
4132 if (status)
4133 goto out;
4134
4135 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004136 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004137 if (!left_path)
4138 goto out;
4139
4140 status = ocfs2_find_path(inode, left_path, left_cpos);
4141 if (status)
4142 goto out;
4143
4144 new_el = path_leaf_el(left_path);
4145
4146 if (le16_to_cpu(new_el->l_next_free_rec) !=
4147 le16_to_cpu(new_el->l_count)) {
4148 bh = path_leaf_bh(left_path);
4149 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004150 ocfs2_error(inode->i_sb,
4151 "Extent block #%llu has an "
4152 "invalid l_next_free_rec of "
4153 "%d. It should have "
4154 "matched the l_count of %d",
4155 (unsigned long long)le64_to_cpu(eb->h_blkno),
4156 le16_to_cpu(new_el->l_next_free_rec),
4157 le16_to_cpu(new_el->l_count));
4158 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004159 goto out;
4160 }
4161 rec = &new_el->l_recs[
4162 le16_to_cpu(new_el->l_next_free_rec) - 1];
4163 }
4164 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004165
4166 /*
4167 * We're careful to check for an empty extent record here -
4168 * the merge code will know what to do if it sees one.
4169 */
Tao Maad5a4d72008-01-30 14:21:32 +08004170 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004171 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4172 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4173 ret = CONTIG_RIGHT;
4174 } else {
4175 ret = ocfs2_extent_contig(inode, rec, split_rec);
4176 }
4177 }
4178
Tao Maad5a4d72008-01-30 14:21:32 +08004179 rec = NULL;
4180 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4181 rec = &el->l_recs[index + 1];
4182 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4183 path->p_tree_depth > 0) {
4184 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4185 path, &right_cpos);
4186 if (status)
4187 goto out;
4188
4189 if (right_cpos == 0)
4190 goto out;
4191
Joel Beckerffdd7a52008-10-17 22:32:01 -07004192 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004193 if (!right_path)
4194 goto out;
4195
4196 status = ocfs2_find_path(inode, right_path, right_cpos);
4197 if (status)
4198 goto out;
4199
4200 new_el = path_leaf_el(right_path);
4201 rec = &new_el->l_recs[0];
4202 if (ocfs2_is_empty_extent(rec)) {
4203 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4204 bh = path_leaf_bh(right_path);
4205 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004206 ocfs2_error(inode->i_sb,
4207 "Extent block #%llu has an "
4208 "invalid l_next_free_rec of %d",
4209 (unsigned long long)le64_to_cpu(eb->h_blkno),
4210 le16_to_cpu(new_el->l_next_free_rec));
4211 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004212 goto out;
4213 }
4214 rec = &new_el->l_recs[1];
4215 }
4216 }
4217
4218 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004219 enum ocfs2_contig_type contig_type;
4220
Mark Fasheh328d5752007-06-18 10:48:04 -07004221 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4222
4223 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4224 ret = CONTIG_LEFTRIGHT;
4225 else if (ret == CONTIG_NONE)
4226 ret = contig_type;
4227 }
4228
Tao Maad5a4d72008-01-30 14:21:32 +08004229out:
4230 if (left_path)
4231 ocfs2_free_path(left_path);
4232 if (right_path)
4233 ocfs2_free_path(right_path);
4234
Mark Fasheh328d5752007-06-18 10:48:04 -07004235 return ret;
4236}
4237
Mark Fashehdcd05382007-01-16 11:32:23 -08004238static void ocfs2_figure_contig_type(struct inode *inode,
4239 struct ocfs2_insert_type *insert,
4240 struct ocfs2_extent_list *el,
Tao Maca12b7c2008-08-18 17:38:52 +08004241 struct ocfs2_extent_rec *insert_rec,
4242 struct ocfs2_extent_tree *et)
Mark Fashehdcd05382007-01-16 11:32:23 -08004243{
4244 int i;
4245 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4246
Mark Fashehe48edee2007-03-07 16:46:57 -08004247 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4248
Mark Fashehdcd05382007-01-16 11:32:23 -08004249 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4250 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4251 insert_rec);
4252 if (contig_type != CONTIG_NONE) {
4253 insert->ins_contig_index = i;
4254 break;
4255 }
4256 }
4257 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004258
4259 if (insert->ins_contig != CONTIG_NONE) {
4260 struct ocfs2_extent_rec *rec =
4261 &el->l_recs[insert->ins_contig_index];
4262 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4263 le16_to_cpu(insert_rec->e_leaf_clusters);
4264
4265 /*
4266 * Caller might want us to limit the size of extents, don't
4267 * calculate contiguousness if we might exceed that limit.
4268 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004269 if (et->et_max_leaf_clusters &&
4270 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004271 insert->ins_contig = CONTIG_NONE;
4272 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004273}
4274
4275/*
4276 * This should only be called against the righmost leaf extent list.
4277 *
4278 * ocfs2_figure_appending_type() will figure out whether we'll have to
4279 * insert at the tail of the rightmost leaf.
4280 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004281 * This should also work against the root extent list for tree's with 0
4282 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004283 * then the logic here makes sense.
4284 */
4285static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4286 struct ocfs2_extent_list *el,
4287 struct ocfs2_extent_rec *insert_rec)
4288{
4289 int i;
4290 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4291 struct ocfs2_extent_rec *rec;
4292
4293 insert->ins_appending = APPEND_NONE;
4294
Mark Fashehe48edee2007-03-07 16:46:57 -08004295 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004296
4297 if (!el->l_next_free_rec)
4298 goto set_tail_append;
4299
4300 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4301 /* Were all records empty? */
4302 if (le16_to_cpu(el->l_next_free_rec) == 1)
4303 goto set_tail_append;
4304 }
4305
4306 i = le16_to_cpu(el->l_next_free_rec) - 1;
4307 rec = &el->l_recs[i];
4308
Mark Fashehe48edee2007-03-07 16:46:57 -08004309 if (cpos >=
4310 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004311 goto set_tail_append;
4312
4313 return;
4314
4315set_tail_append:
4316 insert->ins_appending = APPEND_TAIL;
4317}
4318
4319/*
4320 * Helper function called at the begining of an insert.
4321 *
4322 * This computes a few things that are commonly used in the process of
4323 * inserting into the btree:
4324 * - Whether the new extent is contiguous with an existing one.
4325 * - The current tree depth.
4326 * - Whether the insert is an appending one.
4327 * - The total # of free records in the tree.
4328 *
4329 * All of the information is stored on the ocfs2_insert_type
4330 * structure.
4331 */
4332static int ocfs2_figure_insert_type(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004333 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004334 struct buffer_head **last_eb_bh,
4335 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004336 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004337 struct ocfs2_insert_type *insert)
4338{
4339 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004340 struct ocfs2_extent_block *eb;
4341 struct ocfs2_extent_list *el;
4342 struct ocfs2_path *path = NULL;
4343 struct buffer_head *bh = NULL;
4344
Mark Fasheh328d5752007-06-18 10:48:04 -07004345 insert->ins_split = SPLIT_NONE;
4346
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004347 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004348 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4349
4350 if (el->l_tree_depth) {
4351 /*
4352 * If we have tree depth, we read in the
4353 * rightmost extent block ahead of time as
4354 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4355 * may want it later.
4356 */
Joel Becker5e965812008-11-13 14:49:16 -08004357 ret = ocfs2_read_extent_block(inode,
4358 ocfs2_et_get_last_eb_blk(et),
4359 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004360 if (ret) {
4361 mlog_exit(ret);
4362 goto out;
4363 }
4364 eb = (struct ocfs2_extent_block *) bh->b_data;
4365 el = &eb->h_list;
4366 }
4367
4368 /*
4369 * Unless we have a contiguous insert, we'll need to know if
4370 * there is room left in our allocation tree for another
4371 * extent record.
4372 *
4373 * XXX: This test is simplistic, we can search for empty
4374 * extent records too.
4375 */
Tao Maoc77534f2007-08-28 17:22:33 -07004376 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004377 le16_to_cpu(el->l_next_free_rec);
4378
4379 if (!insert->ins_tree_depth) {
Tao Maca12b7c2008-08-18 17:38:52 +08004380 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004381 ocfs2_figure_appending_type(insert, el, insert_rec);
4382 return 0;
4383 }
4384
Joel Beckerffdd7a52008-10-17 22:32:01 -07004385 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004386 if (!path) {
4387 ret = -ENOMEM;
4388 mlog_errno(ret);
4389 goto out;
4390 }
4391
4392 /*
4393 * In the case that we're inserting past what the tree
4394 * currently accounts for, ocfs2_find_path() will return for
4395 * us the rightmost tree path. This is accounted for below in
4396 * the appending code.
4397 */
4398 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4399 if (ret) {
4400 mlog_errno(ret);
4401 goto out;
4402 }
4403
4404 el = path_leaf_el(path);
4405
4406 /*
4407 * Now that we have the path, there's two things we want to determine:
4408 * 1) Contiguousness (also set contig_index if this is so)
4409 *
4410 * 2) Are we doing an append? We can trivially break this up
4411 * into two types of appends: simple record append, or a
4412 * rotate inside the tail leaf.
4413 */
Tao Maca12b7c2008-08-18 17:38:52 +08004414 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004415
4416 /*
4417 * The insert code isn't quite ready to deal with all cases of
4418 * left contiguousness. Specifically, if it's an insert into
4419 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004420 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004421 * left. For now, just catch that case and fool the layers
4422 * above us. This works just fine for tree_depth == 0, which
4423 * is why we allow that above.
4424 */
4425 if (insert->ins_contig == CONTIG_LEFT &&
4426 insert->ins_contig_index == 0)
4427 insert->ins_contig = CONTIG_NONE;
4428
4429 /*
4430 * Ok, so we can simply compare against last_eb to figure out
4431 * whether the path doesn't exist. This will only happen in
4432 * the case that we're doing a tail append, so maybe we can
4433 * take advantage of that information somehow.
4434 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004435 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004436 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004437 /*
4438 * Ok, ocfs2_find_path() returned us the rightmost
4439 * tree path. This might be an appending insert. There are
4440 * two cases:
4441 * 1) We're doing a true append at the tail:
4442 * -This might even be off the end of the leaf
4443 * 2) We're "appending" by rotating in the tail
4444 */
4445 ocfs2_figure_appending_type(insert, el, insert_rec);
4446 }
4447
4448out:
4449 ocfs2_free_path(path);
4450
4451 if (ret == 0)
4452 *last_eb_bh = bh;
4453 else
4454 brelse(bh);
4455 return ret;
4456}
4457
4458/*
4459 * Insert an extent into an inode btree.
4460 *
4461 * The caller needs to update fe->i_clusters
4462 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004463int ocfs2_insert_extent(struct ocfs2_super *osb,
4464 handle_t *handle,
4465 struct inode *inode,
4466 struct ocfs2_extent_tree *et,
4467 u32 cpos,
4468 u64 start_blk,
4469 u32 new_clusters,
4470 u8 flags,
4471 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004472{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004473 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004474 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004475 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004476 struct ocfs2_insert_type insert = {0, };
4477 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004478
Mark Fashehdcd05382007-01-16 11:32:23 -08004479 mlog(0, "add %u clusters at position %u to inode %llu\n",
4480 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004481
Mark Fashehe48edee2007-03-07 16:46:57 -08004482 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004483 rec.e_cpos = cpu_to_le32(cpos);
4484 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004485 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004486 rec.e_flags = flags;
Joel Becker1e61ee72008-08-20 18:32:45 -07004487 status = ocfs2_et_insert_check(inode, et, &rec);
4488 if (status) {
4489 mlog_errno(status);
4490 goto bail;
4491 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004492
Tao Mae7d4cb62008-08-18 17:38:44 +08004493 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004494 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004495 if (status < 0) {
4496 mlog_errno(status);
4497 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004498 }
4499
Mark Fashehdcd05382007-01-16 11:32:23 -08004500 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4501 "Insert.contig_index: %d, Insert.free_records: %d, "
4502 "Insert.tree_depth: %d\n",
4503 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004504 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004505
Tao Maoc77534f2007-08-28 17:22:33 -07004506 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004507 status = ocfs2_grow_tree(inode, handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004508 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004509 meta_ac);
4510 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004511 mlog_errno(status);
4512 goto bail;
4513 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004514 }
4515
Mark Fashehdcd05382007-01-16 11:32:23 -08004516 /* Finally, we can add clusters. This might rotate the tree for us. */
Tao Mae7d4cb62008-08-18 17:38:44 +08004517 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004518 if (status < 0)
4519 mlog_errno(status);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004520 else if (et->et_ops == &ocfs2_dinode_et_ops)
Mark Fasheh83418972007-04-23 18:53:12 -07004521 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004522
4523bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004524 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004525
Tao Maf56654c2008-08-18 17:38:48 +08004526 mlog_exit(status);
4527 return status;
4528}
4529
Tao Ma0eb8d472008-08-18 17:38:45 +08004530/*
4531 * Allcate and add clusters into the extent b-tree.
4532 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004533 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004534 * it is not limited to the file storage. Any extent tree can use this
4535 * function if it implements the proper ocfs2_extent_tree.
4536 */
4537int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4538 struct inode *inode,
4539 u32 *logical_offset,
4540 u32 clusters_to_add,
4541 int mark_unwritten,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004542 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004543 handle_t *handle,
4544 struct ocfs2_alloc_context *data_ac,
4545 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004546 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004547{
4548 int status = 0;
4549 int free_extents;
4550 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4551 u32 bit_off, num_bits;
4552 u64 block;
4553 u8 flags = 0;
4554
4555 BUG_ON(!clusters_to_add);
4556
4557 if (mark_unwritten)
4558 flags = OCFS2_EXT_UNWRITTEN;
4559
Joel Beckerf99b9b72008-08-20 19:36:33 -07004560 free_extents = ocfs2_num_free_extents(osb, inode, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004561 if (free_extents < 0) {
4562 status = free_extents;
4563 mlog_errno(status);
4564 goto leave;
4565 }
4566
4567 /* there are two cases which could cause us to EAGAIN in the
4568 * we-need-more-metadata case:
4569 * 1) we haven't reserved *any*
4570 * 2) we are so fragmented, we've needed to add metadata too
4571 * many times. */
4572 if (!free_extents && !meta_ac) {
4573 mlog(0, "we haven't reserved any metadata!\n");
4574 status = -EAGAIN;
4575 reason = RESTART_META;
4576 goto leave;
4577 } else if ((!free_extents)
4578 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004579 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004580 mlog(0, "filesystem is really fragmented...\n");
4581 status = -EAGAIN;
4582 reason = RESTART_META;
4583 goto leave;
4584 }
4585
4586 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4587 clusters_to_add, &bit_off, &num_bits);
4588 if (status < 0) {
4589 if (status != -ENOSPC)
4590 mlog_errno(status);
4591 goto leave;
4592 }
4593
4594 BUG_ON(num_bits > clusters_to_add);
4595
4596 /* reserve our write early -- insert_extent may update the inode */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004597 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Tao Ma0eb8d472008-08-18 17:38:45 +08004598 OCFS2_JOURNAL_ACCESS_WRITE);
4599 if (status < 0) {
4600 mlog_errno(status);
4601 goto leave;
4602 }
4603
4604 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4605 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4606 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004607 status = ocfs2_insert_extent(osb, handle, inode, et,
4608 *logical_offset, block,
4609 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004610 if (status < 0) {
4611 mlog_errno(status);
4612 goto leave;
4613 }
4614
Joel Beckerf99b9b72008-08-20 19:36:33 -07004615 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004616 if (status < 0) {
4617 mlog_errno(status);
4618 goto leave;
4619 }
4620
4621 clusters_to_add -= num_bits;
4622 *logical_offset += num_bits;
4623
4624 if (clusters_to_add) {
4625 mlog(0, "need to alloc once more, wanted = %u\n",
4626 clusters_to_add);
4627 status = -EAGAIN;
4628 reason = RESTART_TRANS;
4629 }
4630
4631leave:
4632 mlog_exit(status);
4633 if (reason_ret)
4634 *reason_ret = reason;
4635 return status;
4636}
4637
Mark Fasheh328d5752007-06-18 10:48:04 -07004638static void ocfs2_make_right_split_rec(struct super_block *sb,
4639 struct ocfs2_extent_rec *split_rec,
4640 u32 cpos,
4641 struct ocfs2_extent_rec *rec)
4642{
4643 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4644 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4645
4646 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4647
4648 split_rec->e_cpos = cpu_to_le32(cpos);
4649 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4650
4651 split_rec->e_blkno = rec->e_blkno;
4652 le64_add_cpu(&split_rec->e_blkno,
4653 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4654
4655 split_rec->e_flags = rec->e_flags;
4656}
4657
4658static int ocfs2_split_and_insert(struct inode *inode,
4659 handle_t *handle,
4660 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004661 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004662 struct buffer_head **last_eb_bh,
4663 int split_index,
4664 struct ocfs2_extent_rec *orig_split_rec,
4665 struct ocfs2_alloc_context *meta_ac)
4666{
4667 int ret = 0, depth;
4668 unsigned int insert_range, rec_range, do_leftright = 0;
4669 struct ocfs2_extent_rec tmprec;
4670 struct ocfs2_extent_list *rightmost_el;
4671 struct ocfs2_extent_rec rec;
4672 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4673 struct ocfs2_insert_type insert;
4674 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004675
4676leftright:
4677 /*
4678 * Store a copy of the record on the stack - it might move
4679 * around as the tree is manipulated below.
4680 */
4681 rec = path_leaf_el(path)->l_recs[split_index];
4682
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004683 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004684
4685 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4686 if (depth) {
4687 BUG_ON(!(*last_eb_bh));
4688 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4689 rightmost_el = &eb->h_list;
4690 }
4691
4692 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4693 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004694 ret = ocfs2_grow_tree(inode, handle, et,
4695 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004696 if (ret) {
4697 mlog_errno(ret);
4698 goto out;
4699 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004700 }
4701
4702 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4703 insert.ins_appending = APPEND_NONE;
4704 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004705 insert.ins_tree_depth = depth;
4706
4707 insert_range = le32_to_cpu(split_rec.e_cpos) +
4708 le16_to_cpu(split_rec.e_leaf_clusters);
4709 rec_range = le32_to_cpu(rec.e_cpos) +
4710 le16_to_cpu(rec.e_leaf_clusters);
4711
4712 if (split_rec.e_cpos == rec.e_cpos) {
4713 insert.ins_split = SPLIT_LEFT;
4714 } else if (insert_range == rec_range) {
4715 insert.ins_split = SPLIT_RIGHT;
4716 } else {
4717 /*
4718 * Left/right split. We fake this as a right split
4719 * first and then make a second pass as a left split.
4720 */
4721 insert.ins_split = SPLIT_RIGHT;
4722
4723 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4724 &rec);
4725
4726 split_rec = tmprec;
4727
4728 BUG_ON(do_leftright);
4729 do_leftright = 1;
4730 }
4731
Tao Mae7d4cb62008-08-18 17:38:44 +08004732 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004733 if (ret) {
4734 mlog_errno(ret);
4735 goto out;
4736 }
4737
4738 if (do_leftright == 1) {
4739 u32 cpos;
4740 struct ocfs2_extent_list *el;
4741
4742 do_leftright++;
4743 split_rec = *orig_split_rec;
4744
4745 ocfs2_reinit_path(path, 1);
4746
4747 cpos = le32_to_cpu(split_rec.e_cpos);
4748 ret = ocfs2_find_path(inode, path, cpos);
4749 if (ret) {
4750 mlog_errno(ret);
4751 goto out;
4752 }
4753
4754 el = path_leaf_el(path);
4755 split_index = ocfs2_search_extent_list(el, cpos);
4756 goto leftright;
4757 }
4758out:
4759
4760 return ret;
4761}
4762
4763/*
4764 * Mark part or all of the extent record at split_index in the leaf
4765 * pointed to by path as written. This removes the unwritten
4766 * extent flag.
4767 *
4768 * Care is taken to handle contiguousness so as to not grow the tree.
4769 *
4770 * meta_ac is not strictly necessary - we only truly need it if growth
4771 * of the tree is required. All other cases will degrade into a less
4772 * optimal tree layout.
4773 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004774 * last_eb_bh should be the rightmost leaf block for any extent
4775 * btree. Since a split may grow the tree or a merge might shrink it,
4776 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07004777 *
4778 * This code is optimized for readability - several passes might be
4779 * made over certain portions of the tree. All of those blocks will
4780 * have been brought into cache (and pinned via the journal), so the
4781 * extra overhead is not expressed in terms of disk reads.
4782 */
4783static int __ocfs2_mark_extent_written(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004784 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004785 handle_t *handle,
4786 struct ocfs2_path *path,
4787 int split_index,
4788 struct ocfs2_extent_rec *split_rec,
4789 struct ocfs2_alloc_context *meta_ac,
4790 struct ocfs2_cached_dealloc_ctxt *dealloc)
4791{
4792 int ret = 0;
4793 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004794 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07004795 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4796 struct ocfs2_merge_ctxt ctxt;
4797 struct ocfs2_extent_list *rightmost_el;
4798
Roel Kluin3cf0c502007-10-27 00:20:36 +02004799 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004800 ret = -EIO;
4801 mlog_errno(ret);
4802 goto out;
4803 }
4804
4805 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4806 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4807 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4808 ret = -EIO;
4809 mlog_errno(ret);
4810 goto out;
4811 }
4812
Tao Maad5a4d72008-01-30 14:21:32 +08004813 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07004814 split_index,
4815 split_rec);
4816
4817 /*
4818 * The core merge / split code wants to know how much room is
4819 * left in this inodes allocation tree, so we pass the
4820 * rightmost extent list.
4821 */
4822 if (path->p_tree_depth) {
4823 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004824
Joel Becker5e965812008-11-13 14:49:16 -08004825 ret = ocfs2_read_extent_block(inode,
4826 ocfs2_et_get_last_eb_blk(et),
4827 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07004828 if (ret) {
4829 mlog_exit(ret);
4830 goto out;
4831 }
4832
4833 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07004834 rightmost_el = &eb->h_list;
4835 } else
4836 rightmost_el = path_root_el(path);
4837
Mark Fasheh328d5752007-06-18 10:48:04 -07004838 if (rec->e_cpos == split_rec->e_cpos &&
4839 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4840 ctxt.c_split_covers_rec = 1;
4841 else
4842 ctxt.c_split_covers_rec = 0;
4843
4844 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4845
Mark Fasheh015452b2007-09-12 10:21:22 -07004846 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4847 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4848 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004849
4850 if (ctxt.c_contig_type == CONTIG_NONE) {
4851 if (ctxt.c_split_covers_rec)
4852 el->l_recs[split_index] = *split_rec;
4853 else
Tao Mae7d4cb62008-08-18 17:38:44 +08004854 ret = ocfs2_split_and_insert(inode, handle, path, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004855 &last_eb_bh, split_index,
4856 split_rec, meta_ac);
4857 if (ret)
4858 mlog_errno(ret);
4859 } else {
4860 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4861 split_index, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08004862 dealloc, &ctxt, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07004863 if (ret)
4864 mlog_errno(ret);
4865 }
4866
Mark Fasheh328d5752007-06-18 10:48:04 -07004867out:
4868 brelse(last_eb_bh);
4869 return ret;
4870}
4871
4872/*
4873 * Mark the already-existing extent at cpos as written for len clusters.
4874 *
4875 * If the existing extent is larger than the request, initiate a
4876 * split. An attempt will be made at merging with adjacent extents.
4877 *
4878 * The caller is responsible for passing down meta_ac if we'll need it.
4879 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004880int ocfs2_mark_extent_written(struct inode *inode,
4881 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004882 handle_t *handle, u32 cpos, u32 len, u32 phys,
4883 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004884 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07004885{
4886 int ret, index;
4887 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4888 struct ocfs2_extent_rec split_rec;
4889 struct ocfs2_path *left_path = NULL;
4890 struct ocfs2_extent_list *el;
4891
4892 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4893 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4894
4895 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4896 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4897 "that are being written to, but the feature bit "
4898 "is not set in the super block.",
4899 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4900 ret = -EROFS;
4901 goto out;
4902 }
4903
4904 /*
4905 * XXX: This should be fixed up so that we just re-insert the
4906 * next extent records.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004907 *
4908 * XXX: This is a hack on the extent tree, maybe it should be
4909 * an op?
Mark Fasheh328d5752007-06-18 10:48:04 -07004910 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004911 if (et->et_ops == &ocfs2_dinode_et_ops)
Tao Mae7d4cb62008-08-18 17:38:44 +08004912 ocfs2_extent_map_trunc(inode, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07004913
Joel Beckerffdd7a52008-10-17 22:32:01 -07004914 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07004915 if (!left_path) {
4916 ret = -ENOMEM;
4917 mlog_errno(ret);
4918 goto out;
4919 }
4920
4921 ret = ocfs2_find_path(inode, left_path, cpos);
4922 if (ret) {
4923 mlog_errno(ret);
4924 goto out;
4925 }
4926 el = path_leaf_el(left_path);
4927
4928 index = ocfs2_search_extent_list(el, cpos);
4929 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4930 ocfs2_error(inode->i_sb,
4931 "Inode %llu has an extent at cpos %u which can no "
4932 "longer be found.\n",
4933 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4934 ret = -EROFS;
4935 goto out;
4936 }
4937
4938 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4939 split_rec.e_cpos = cpu_to_le32(cpos);
4940 split_rec.e_leaf_clusters = cpu_to_le16(len);
4941 split_rec.e_blkno = cpu_to_le64(start_blkno);
4942 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4943 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4944
Joel Beckerf99b9b72008-08-20 19:36:33 -07004945 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004946 index, &split_rec, meta_ac,
4947 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07004948 if (ret)
4949 mlog_errno(ret);
4950
4951out:
4952 ocfs2_free_path(left_path);
4953 return ret;
4954}
4955
Tao Mae7d4cb62008-08-18 17:38:44 +08004956static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07004957 handle_t *handle, struct ocfs2_path *path,
4958 int index, u32 new_range,
4959 struct ocfs2_alloc_context *meta_ac)
4960{
4961 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07004962 struct buffer_head *last_eb_bh = NULL;
4963 struct ocfs2_extent_block *eb;
4964 struct ocfs2_extent_list *rightmost_el, *el;
4965 struct ocfs2_extent_rec split_rec;
4966 struct ocfs2_extent_rec *rec;
4967 struct ocfs2_insert_type insert;
4968
4969 /*
4970 * Setup the record to split before we grow the tree.
4971 */
4972 el = path_leaf_el(path);
4973 rec = &el->l_recs[index];
4974 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4975
4976 depth = path->p_tree_depth;
4977 if (depth > 0) {
Joel Becker5e965812008-11-13 14:49:16 -08004978 ret = ocfs2_read_extent_block(inode,
4979 ocfs2_et_get_last_eb_blk(et),
4980 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07004981 if (ret < 0) {
4982 mlog_errno(ret);
4983 goto out;
4984 }
4985
4986 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4987 rightmost_el = &eb->h_list;
4988 } else
4989 rightmost_el = path_leaf_el(path);
4990
Tao Ma811f9332008-08-18 17:38:43 +08004991 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004992 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07004993 ret = ocfs2_extend_trans(handle, credits);
4994 if (ret) {
4995 mlog_errno(ret);
4996 goto out;
4997 }
4998
4999 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5000 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005001 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005002 meta_ac);
5003 if (ret) {
5004 mlog_errno(ret);
5005 goto out;
5006 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005007 }
5008
5009 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5010 insert.ins_appending = APPEND_NONE;
5011 insert.ins_contig = CONTIG_NONE;
5012 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005013 insert.ins_tree_depth = depth;
5014
Tao Mae7d4cb62008-08-18 17:38:44 +08005015 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005016 if (ret)
5017 mlog_errno(ret);
5018
5019out:
5020 brelse(last_eb_bh);
5021 return ret;
5022}
5023
5024static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5025 struct ocfs2_path *path, int index,
5026 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08005027 u32 cpos, u32 len,
5028 struct ocfs2_extent_tree *et)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005029{
5030 int ret;
5031 u32 left_cpos, rec_range, trunc_range;
5032 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5033 struct super_block *sb = inode->i_sb;
5034 struct ocfs2_path *left_path = NULL;
5035 struct ocfs2_extent_list *el = path_leaf_el(path);
5036 struct ocfs2_extent_rec *rec;
5037 struct ocfs2_extent_block *eb;
5038
5039 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005040 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005041 if (ret) {
5042 mlog_errno(ret);
5043 goto out;
5044 }
5045
5046 index--;
5047 }
5048
5049 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5050 path->p_tree_depth) {
5051 /*
5052 * Check whether this is the rightmost tree record. If
5053 * we remove all of this record or part of its right
5054 * edge then an update of the record lengths above it
5055 * will be required.
5056 */
5057 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5058 if (eb->h_next_leaf_blk == 0)
5059 is_rightmost_tree_rec = 1;
5060 }
5061
5062 rec = &el->l_recs[index];
5063 if (index == 0 && path->p_tree_depth &&
5064 le32_to_cpu(rec->e_cpos) == cpos) {
5065 /*
5066 * Changing the leftmost offset (via partial or whole
5067 * record truncate) of an interior (or rightmost) path
5068 * means we have to update the subtree that is formed
5069 * by this leaf and the one to it's left.
5070 *
5071 * There are two cases we can skip:
5072 * 1) Path is the leftmost one in our inode tree.
5073 * 2) The leaf is rightmost and will be empty after
5074 * we remove the extent record - the rotate code
5075 * knows how to update the newly formed edge.
5076 */
5077
5078 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5079 &left_cpos);
5080 if (ret) {
5081 mlog_errno(ret);
5082 goto out;
5083 }
5084
5085 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005086 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005087 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 Beckerffdd7a52008-10-17 22:32:01 -07005195 path = ocfs2_new_path_from_et(et);
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 Fashehb06970532006-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 Fashehb06970532006-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 Fashehb06970532006-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 Fashehb06970532006-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 Fashehb06970532006-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}