blob: 1ff13d3958ddb7d18968372deb159a3af04babcf [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"
Joel Becker2a50a742008-12-09 14:24:33 -080051#include "xattr.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080052
53#include "buffer_head_io.h"
54
Tao Mae7d4cb62008-08-18 17:38:44 +080055
Joel Becker1625f8a2008-08-21 17:11:10 -070056/*
57 * Operations for a specific extent tree type.
58 *
59 * To implement an on-disk btree (extent tree) type in ocfs2, add
60 * an ocfs2_extent_tree_operations structure and the matching
Joel Becker8d6220d2008-08-22 12:46:09 -070061 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
Joel Becker1625f8a2008-08-21 17:11:10 -070062 * for the allocation portion of the extent tree.
63 */
Tao Mae7d4cb62008-08-18 17:38:44 +080064struct ocfs2_extent_tree_operations {
Joel Becker1625f8a2008-08-21 17:11:10 -070065 /*
66 * last_eb_blk is the block number of the right most leaf extent
67 * block. Most on-disk structures containing an extent tree store
68 * this value for fast access. The ->eo_set_last_eb_blk() and
69 * ->eo_get_last_eb_blk() operations access this value. They are
70 * both required.
71 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070072 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
73 u64 blkno);
74 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -070075
76 /*
77 * The on-disk structure usually keeps track of how many total
78 * clusters are stored in this extent tree. This function updates
79 * that value. new_clusters is the delta, and must be
80 * added to the total. Required.
81 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070082 void (*eo_update_clusters)(struct inode *inode,
83 struct ocfs2_extent_tree *et,
84 u32 new_clusters);
Joel Becker1625f8a2008-08-21 17:11:10 -070085
86 /*
87 * If ->eo_insert_check() exists, it is called before rec is
88 * inserted into the extent tree. It is optional.
89 */
Joel Becker1e61ee72008-08-20 18:32:45 -070090 int (*eo_insert_check)(struct inode *inode,
91 struct ocfs2_extent_tree *et,
92 struct ocfs2_extent_rec *rec);
Joel Becker35dc0aa2008-08-20 16:25:06 -070093 int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
Joel Becker0ce10102008-08-20 17:19:50 -070094
Joel Becker1625f8a2008-08-21 17:11:10 -070095 /*
96 * --------------------------------------------------------------
97 * The remaining are internal to ocfs2_extent_tree and don't have
98 * accessor functions
99 */
100
101 /*
102 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
103 * It is required.
104 */
Joel Becker0ce10102008-08-20 17:19:50 -0700105 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -0700106
107 /*
108 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
109 * it exists. If it does not, et->et_max_leaf_clusters is set
110 * to 0 (unlimited). Optional.
111 */
Joel Becker943cced2008-08-20 17:31:10 -0700112 void (*eo_fill_max_leaf_clusters)(struct inode *inode,
113 struct ocfs2_extent_tree *et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800114};
115
Joel Beckerf99b9b72008-08-20 19:36:33 -0700116
117/*
118 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
119 * in the methods.
120 */
121static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
122static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
123 u64 blkno);
124static void ocfs2_dinode_update_clusters(struct inode *inode,
125 struct ocfs2_extent_tree *et,
126 u32 clusters);
127static int ocfs2_dinode_insert_check(struct inode *inode,
128 struct ocfs2_extent_tree *et,
129 struct ocfs2_extent_rec *rec);
130static int ocfs2_dinode_sanity_check(struct inode *inode,
131 struct ocfs2_extent_tree *et);
132static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
133static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
134 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
135 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
136 .eo_update_clusters = ocfs2_dinode_update_clusters,
137 .eo_insert_check = ocfs2_dinode_insert_check,
138 .eo_sanity_check = ocfs2_dinode_sanity_check,
139 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
Tao Mae7d4cb62008-08-18 17:38:44 +0800140};
141
142static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
143 u64 blkno)
144{
Joel Beckerea5efa12008-08-20 16:57:27 -0700145 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800146
Joel Beckerf99b9b72008-08-20 19:36:33 -0700147 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800148 di->i_last_eb_blk = cpu_to_le64(blkno);
149}
150
151static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
152{
Joel Beckerea5efa12008-08-20 16:57:27 -0700153 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800154
Joel Beckerf99b9b72008-08-20 19:36:33 -0700155 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800156 return le64_to_cpu(di->i_last_eb_blk);
157}
158
159static void ocfs2_dinode_update_clusters(struct inode *inode,
160 struct ocfs2_extent_tree *et,
161 u32 clusters)
162{
Joel Beckerea5efa12008-08-20 16:57:27 -0700163 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800164
165 le32_add_cpu(&di->i_clusters, clusters);
166 spin_lock(&OCFS2_I(inode)->ip_lock);
167 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
168 spin_unlock(&OCFS2_I(inode)->ip_lock);
169}
170
Joel Becker1e61ee72008-08-20 18:32:45 -0700171static int ocfs2_dinode_insert_check(struct inode *inode,
172 struct ocfs2_extent_tree *et,
173 struct ocfs2_extent_rec *rec)
174{
175 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
176
177 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
178 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
Tao Ma74e77eb2009-03-12 06:24:23 +0800179 (OCFS2_I(inode)->ip_clusters !=
180 le32_to_cpu(rec->e_cpos)),
Joel Becker1e61ee72008-08-20 18:32:45 -0700181 "Device %s, asking for sparse allocation: inode %llu, "
182 "cpos %u, clusters %u\n",
183 osb->dev_str,
184 (unsigned long long)OCFS2_I(inode)->ip_blkno,
185 rec->e_cpos,
186 OCFS2_I(inode)->ip_clusters);
187
188 return 0;
189}
190
Tao Mae7d4cb62008-08-18 17:38:44 +0800191static int ocfs2_dinode_sanity_check(struct inode *inode,
192 struct ocfs2_extent_tree *et)
193{
Joel Becker10995aa2008-11-13 14:49:12 -0800194 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800195
Joel Beckerf99b9b72008-08-20 19:36:33 -0700196 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Joel Becker10995aa2008-11-13 14:49:12 -0800197 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
Tao Mae7d4cb62008-08-18 17:38:44 +0800198
Joel Becker10995aa2008-11-13 14:49:12 -0800199 return 0;
Tao Mae7d4cb62008-08-18 17:38:44 +0800200}
201
Joel Beckerf99b9b72008-08-20 19:36:33 -0700202static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
203{
204 struct ocfs2_dinode *di = et->et_object;
205
206 et->et_root_el = &di->id2.i_list;
207}
208
Tao Mae7d4cb62008-08-18 17:38:44 +0800209
Joel Becker0ce10102008-08-20 17:19:50 -0700210static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
211{
Joel Becker2a50a742008-12-09 14:24:33 -0800212 struct ocfs2_xattr_value_buf *vb = et->et_object;
Joel Becker0ce10102008-08-20 17:19:50 -0700213
Joel Becker2a50a742008-12-09 14:24:33 -0800214 et->et_root_el = &vb->vb_xv->xr_list;
Joel Becker0ce10102008-08-20 17:19:50 -0700215}
216
Tao Maf56654c2008-08-18 17:38:48 +0800217static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
218 u64 blkno)
219{
Joel Becker2a50a742008-12-09 14:24:33 -0800220 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800221
Joel Becker2a50a742008-12-09 14:24:33 -0800222 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
Tao Maf56654c2008-08-18 17:38:48 +0800223}
224
225static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
226{
Joel Becker2a50a742008-12-09 14:24:33 -0800227 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800228
Joel Becker2a50a742008-12-09 14:24:33 -0800229 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
Tao Maf56654c2008-08-18 17:38:48 +0800230}
231
232static void ocfs2_xattr_value_update_clusters(struct inode *inode,
233 struct ocfs2_extent_tree *et,
234 u32 clusters)
235{
Joel Becker2a50a742008-12-09 14:24:33 -0800236 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800237
Joel Becker2a50a742008-12-09 14:24:33 -0800238 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800239}
240
Joel Becker1a09f552008-08-20 17:44:24 -0700241static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700242 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
243 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
244 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700245 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800246};
247
Joel Becker0ce10102008-08-20 17:19:50 -0700248static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
249{
250 struct ocfs2_xattr_block *xb = et->et_object;
251
252 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
253}
254
Joel Becker943cced2008-08-20 17:31:10 -0700255static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode *inode,
256 struct ocfs2_extent_tree *et)
257{
258 et->et_max_leaf_clusters =
259 ocfs2_clusters_for_bytes(inode->i_sb,
260 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
261}
262
Tao Maba492612008-08-18 17:38:49 +0800263static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
264 u64 blkno)
265{
Joel Beckerea5efa12008-08-20 16:57:27 -0700266 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800267 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
268
269 xt->xt_last_eb_blk = cpu_to_le64(blkno);
270}
271
272static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
273{
Joel Beckerea5efa12008-08-20 16:57:27 -0700274 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800275 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
276
277 return le64_to_cpu(xt->xt_last_eb_blk);
278}
279
280static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
281 struct ocfs2_extent_tree *et,
282 u32 clusters)
283{
Joel Beckerea5efa12008-08-20 16:57:27 -0700284 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800285
286 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
287}
288
Tao Maba492612008-08-18 17:38:49 +0800289static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700290 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
291 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
292 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700293 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
Joel Becker943cced2008-08-20 17:31:10 -0700294 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
Tao Maba492612008-08-18 17:38:49 +0800295};
296
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800297static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
298 u64 blkno)
299{
300 struct ocfs2_dx_root_block *dx_root = et->et_object;
301
302 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
303}
304
305static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
306{
307 struct ocfs2_dx_root_block *dx_root = et->et_object;
308
309 return le64_to_cpu(dx_root->dr_last_eb_blk);
310}
311
312static void ocfs2_dx_root_update_clusters(struct inode *inode,
313 struct ocfs2_extent_tree *et,
314 u32 clusters)
315{
316 struct ocfs2_dx_root_block *dx_root = et->et_object;
317
318 le32_add_cpu(&dx_root->dr_clusters, clusters);
319}
320
321static int ocfs2_dx_root_sanity_check(struct inode *inode,
322 struct ocfs2_extent_tree *et)
323{
324 struct ocfs2_dx_root_block *dx_root = et->et_object;
325
326 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
327
328 return 0;
329}
330
331static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
332{
333 struct ocfs2_dx_root_block *dx_root = et->et_object;
334
335 et->et_root_el = &dx_root->dr_list;
336}
337
338static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
339 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
340 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
341 .eo_update_clusters = ocfs2_dx_root_update_clusters,
342 .eo_sanity_check = ocfs2_dx_root_sanity_check,
343 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
344};
345
Joel Becker8d6220d2008-08-22 12:46:09 -0700346static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
347 struct inode *inode,
348 struct buffer_head *bh,
Joel Becker13723d02008-10-17 19:25:01 -0700349 ocfs2_journal_access_func access,
Joel Becker8d6220d2008-08-22 12:46:09 -0700350 void *obj,
351 struct ocfs2_extent_tree_operations *ops)
Tao Mae7d4cb62008-08-18 17:38:44 +0800352{
Joel Becker1a09f552008-08-20 17:44:24 -0700353 et->et_ops = ops;
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700354 et->et_root_bh = bh;
Joel Beckerd9a0a1f2009-02-12 17:32:34 -0800355 et->et_ci = INODE_CACHE(inode);
Joel Becker13723d02008-10-17 19:25:01 -0700356 et->et_root_journal_access = access;
Joel Beckerea5efa12008-08-20 16:57:27 -0700357 if (!obj)
358 obj = (void *)bh->b_data;
359 et->et_object = obj;
Tao Mae7d4cb62008-08-18 17:38:44 +0800360
Joel Becker0ce10102008-08-20 17:19:50 -0700361 et->et_ops->eo_fill_root_el(et);
Joel Becker943cced2008-08-20 17:31:10 -0700362 if (!et->et_ops->eo_fill_max_leaf_clusters)
363 et->et_max_leaf_clusters = 0;
364 else
365 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800366}
367
Joel Becker8d6220d2008-08-22 12:46:09 -0700368void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
369 struct inode *inode,
370 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700371{
Joel Becker13723d02008-10-17 19:25:01 -0700372 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_di,
373 NULL, &ocfs2_dinode_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700374}
375
Joel Becker8d6220d2008-08-22 12:46:09 -0700376void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700377 struct inode *inode,
Joel Becker8d6220d2008-08-22 12:46:09 -0700378 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700379{
Joel Becker13723d02008-10-17 19:25:01 -0700380 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_xb,
381 NULL, &ocfs2_xattr_tree_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700382}
383
Joel Becker8d6220d2008-08-22 12:46:09 -0700384void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
385 struct inode *inode,
Joel Becker2a50a742008-12-09 14:24:33 -0800386 struct ocfs2_xattr_value_buf *vb)
Tao Mae7d4cb62008-08-18 17:38:44 +0800387{
Joel Becker2a50a742008-12-09 14:24:33 -0800388 __ocfs2_init_extent_tree(et, inode, vb->vb_bh, vb->vb_access, vb,
Joel Becker8d6220d2008-08-22 12:46:09 -0700389 &ocfs2_xattr_value_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800390}
391
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800392void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
393 struct inode *inode,
394 struct buffer_head *bh)
395{
396 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_dr,
397 NULL, &ocfs2_dx_root_et_ops);
398}
399
Joel Becker35dc0aa2008-08-20 16:25:06 -0700400static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
401 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800402{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700403 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800404}
405
Joel Becker35dc0aa2008-08-20 16:25:06 -0700406static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800407{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700408 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800409}
410
Joel Becker35dc0aa2008-08-20 16:25:06 -0700411static inline void ocfs2_et_update_clusters(struct inode *inode,
412 struct ocfs2_extent_tree *et,
413 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800414{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700415 et->et_ops->eo_update_clusters(inode, et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700416}
417
Joel Becker13723d02008-10-17 19:25:01 -0700418static inline int ocfs2_et_root_journal_access(handle_t *handle,
Joel Becker13723d02008-10-17 19:25:01 -0700419 struct ocfs2_extent_tree *et,
420 int type)
421{
Joel Beckerd9a0a1f2009-02-12 17:32:34 -0800422 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700423 type);
424}
425
Joel Becker1e61ee72008-08-20 18:32:45 -0700426static inline int ocfs2_et_insert_check(struct inode *inode,
427 struct ocfs2_extent_tree *et,
428 struct ocfs2_extent_rec *rec)
429{
430 int ret = 0;
431
432 if (et->et_ops->eo_insert_check)
433 ret = et->et_ops->eo_insert_check(inode, et, rec);
434 return ret;
435}
436
Joel Becker35dc0aa2008-08-20 16:25:06 -0700437static inline int ocfs2_et_sanity_check(struct inode *inode,
438 struct ocfs2_extent_tree *et)
439{
Joel Becker1e61ee72008-08-20 18:32:45 -0700440 int ret = 0;
441
442 if (et->et_ops->eo_sanity_check)
443 ret = et->et_ops->eo_sanity_check(inode, et);
444 return ret;
Tao Mae7d4cb62008-08-18 17:38:44 +0800445}
446
Mark Fashehccd979b2005-12-15 14:31:24 -0800447static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
Mark Fasheh59a5e412007-06-22 15:52:36 -0700448static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
449 struct ocfs2_extent_block *eb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800450
Mark Fashehdcd05382007-01-16 11:32:23 -0800451/*
452 * Structures which describe a path through a btree, and functions to
453 * manipulate them.
454 *
455 * The idea here is to be as generic as possible with the tree
456 * manipulation code.
457 */
458struct ocfs2_path_item {
459 struct buffer_head *bh;
460 struct ocfs2_extent_list *el;
461};
462
463#define OCFS2_MAX_PATH_DEPTH 5
464
465struct ocfs2_path {
Joel Becker13723d02008-10-17 19:25:01 -0700466 int p_tree_depth;
467 ocfs2_journal_access_func p_root_access;
468 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
Mark Fashehdcd05382007-01-16 11:32:23 -0800469};
470
471#define path_root_bh(_path) ((_path)->p_node[0].bh)
472#define path_root_el(_path) ((_path)->p_node[0].el)
Joel Becker13723d02008-10-17 19:25:01 -0700473#define path_root_access(_path)((_path)->p_root_access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800474#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
475#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
476#define path_num_items(_path) ((_path)->p_tree_depth + 1)
477
Tao Ma6b791bc2009-06-12 14:18:36 +0800478static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
479 u32 cpos);
480static void ocfs2_adjust_rightmost_records(struct inode *inode,
481 handle_t *handle,
482 struct ocfs2_path *path,
483 struct ocfs2_extent_rec *insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -0800484/*
485 * Reset the actual path elements so that we can re-use the structure
486 * to build another path. Generally, this involves freeing the buffer
487 * heads.
488 */
489static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
490{
491 int i, start = 0, depth = 0;
492 struct ocfs2_path_item *node;
493
494 if (keep_root)
495 start = 1;
496
497 for(i = start; i < path_num_items(path); i++) {
498 node = &path->p_node[i];
499
500 brelse(node->bh);
501 node->bh = NULL;
502 node->el = NULL;
503 }
504
505 /*
506 * Tree depth may change during truncate, or insert. If we're
507 * keeping the root extent list, then make sure that our path
508 * structure reflects the proper depth.
509 */
510 if (keep_root)
511 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
Joel Becker13723d02008-10-17 19:25:01 -0700512 else
513 path_root_access(path) = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -0800514
515 path->p_tree_depth = depth;
516}
517
518static void ocfs2_free_path(struct ocfs2_path *path)
519{
520 if (path) {
521 ocfs2_reinit_path(path, 0);
522 kfree(path);
523 }
524}
525
526/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700527 * All the elements of src into dest. After this call, src could be freed
528 * without affecting dest.
529 *
530 * Both paths should have the same root. Any non-root elements of dest
531 * will be freed.
532 */
533static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
534{
535 int i;
536
537 BUG_ON(path_root_bh(dest) != path_root_bh(src));
538 BUG_ON(path_root_el(dest) != path_root_el(src));
Joel Becker13723d02008-10-17 19:25:01 -0700539 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fasheh328d5752007-06-18 10:48:04 -0700540
541 ocfs2_reinit_path(dest, 1);
542
543 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
544 dest->p_node[i].bh = src->p_node[i].bh;
545 dest->p_node[i].el = src->p_node[i].el;
546
547 if (dest->p_node[i].bh)
548 get_bh(dest->p_node[i].bh);
549 }
550}
551
552/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800553 * Make the *dest path the same as src and re-initialize src path to
554 * have a root only.
555 */
556static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
557{
558 int i;
559
560 BUG_ON(path_root_bh(dest) != path_root_bh(src));
Joel Becker13723d02008-10-17 19:25:01 -0700561 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fashehdcd05382007-01-16 11:32:23 -0800562
563 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
564 brelse(dest->p_node[i].bh);
565
566 dest->p_node[i].bh = src->p_node[i].bh;
567 dest->p_node[i].el = src->p_node[i].el;
568
569 src->p_node[i].bh = NULL;
570 src->p_node[i].el = NULL;
571 }
572}
573
574/*
575 * Insert an extent block at given index.
576 *
577 * This will not take an additional reference on eb_bh.
578 */
579static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
580 struct buffer_head *eb_bh)
581{
582 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
583
584 /*
585 * Right now, no root bh is an extent block, so this helps
586 * catch code errors with dinode trees. The assertion can be
587 * safely removed if we ever need to insert extent block
588 * structures at the root.
589 */
590 BUG_ON(index == 0);
591
592 path->p_node[index].bh = eb_bh;
593 path->p_node[index].el = &eb->h_list;
594}
595
596static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700597 struct ocfs2_extent_list *root_el,
598 ocfs2_journal_access_func access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800599{
600 struct ocfs2_path *path;
601
602 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
603
604 path = kzalloc(sizeof(*path), GFP_NOFS);
605 if (path) {
606 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
607 get_bh(root_bh);
608 path_root_bh(path) = root_bh;
609 path_root_el(path) = root_el;
Joel Becker13723d02008-10-17 19:25:01 -0700610 path_root_access(path) = access;
Mark Fashehdcd05382007-01-16 11:32:23 -0800611 }
612
613 return path;
614}
615
Joel Beckerffdd7a52008-10-17 22:32:01 -0700616static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
617{
Joel Becker13723d02008-10-17 19:25:01 -0700618 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
619 path_root_access(path));
Joel Beckerffdd7a52008-10-17 22:32:01 -0700620}
621
622static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
623{
Joel Becker13723d02008-10-17 19:25:01 -0700624 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
625 et->et_root_journal_access);
626}
627
628/*
629 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
630 * otherwise it's the root_access function.
631 *
632 * I don't like the way this function's name looks next to
633 * ocfs2_journal_access_path(), but I don't have a better one.
634 */
635static int ocfs2_path_bh_journal_access(handle_t *handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800636 struct ocfs2_caching_info *ci,
Joel Becker13723d02008-10-17 19:25:01 -0700637 struct ocfs2_path *path,
638 int idx)
639{
640 ocfs2_journal_access_func access = path_root_access(path);
641
642 if (!access)
643 access = ocfs2_journal_access;
644
645 if (idx)
646 access = ocfs2_journal_access_eb;
647
Joel Becker0cf2f762009-02-12 16:41:25 -0800648 return access(handle, ci, path->p_node[idx].bh,
Joel Becker13723d02008-10-17 19:25:01 -0700649 OCFS2_JOURNAL_ACCESS_WRITE);
Joel Beckerffdd7a52008-10-17 22:32:01 -0700650}
651
Mark Fashehdcd05382007-01-16 11:32:23 -0800652/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800653 * Convenience function to journal all components in a path.
654 */
Joel Becker0cf2f762009-02-12 16:41:25 -0800655static int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
656 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -0800657 struct ocfs2_path *path)
658{
659 int i, ret = 0;
660
661 if (!path)
662 goto out;
663
664 for(i = 0; i < path_num_items(path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800665 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -0800666 if (ret < 0) {
667 mlog_errno(ret);
668 goto out;
669 }
670 }
671
672out:
673 return ret;
674}
675
Mark Fasheh328d5752007-06-18 10:48:04 -0700676/*
677 * Return the index of the extent record which contains cluster #v_cluster.
678 * -1 is returned if it was not found.
679 *
680 * Should work fine on interior and exterior nodes.
681 */
682int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
683{
684 int ret = -1;
685 int i;
686 struct ocfs2_extent_rec *rec;
687 u32 rec_end, rec_start, clusters;
688
689 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
690 rec = &el->l_recs[i];
691
692 rec_start = le32_to_cpu(rec->e_cpos);
693 clusters = ocfs2_rec_clusters(el, rec);
694
695 rec_end = rec_start + clusters;
696
697 if (v_cluster >= rec_start && v_cluster < rec_end) {
698 ret = i;
699 break;
700 }
701 }
702
703 return ret;
704}
705
Mark Fashehdcd05382007-01-16 11:32:23 -0800706enum ocfs2_contig_type {
707 CONTIG_NONE = 0,
708 CONTIG_LEFT,
Mark Fasheh328d5752007-06-18 10:48:04 -0700709 CONTIG_RIGHT,
710 CONTIG_LEFTRIGHT,
Mark Fashehdcd05382007-01-16 11:32:23 -0800711};
712
Mark Fashehe48edee2007-03-07 16:46:57 -0800713
714/*
715 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
716 * ocfs2_extent_contig only work properly against leaf nodes!
717 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800718static int ocfs2_block_extent_contig(struct super_block *sb,
719 struct ocfs2_extent_rec *ext,
720 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800721{
Mark Fashehe48edee2007-03-07 16:46:57 -0800722 u64 blk_end = le64_to_cpu(ext->e_blkno);
723
724 blk_end += ocfs2_clusters_to_blocks(sb,
725 le16_to_cpu(ext->e_leaf_clusters));
726
727 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800728}
729
Mark Fashehdcd05382007-01-16 11:32:23 -0800730static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
731 struct ocfs2_extent_rec *right)
732{
Mark Fashehe48edee2007-03-07 16:46:57 -0800733 u32 left_range;
734
735 left_range = le32_to_cpu(left->e_cpos) +
736 le16_to_cpu(left->e_leaf_clusters);
737
738 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800739}
740
741static enum ocfs2_contig_type
742 ocfs2_extent_contig(struct inode *inode,
743 struct ocfs2_extent_rec *ext,
744 struct ocfs2_extent_rec *insert_rec)
745{
746 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
747
Mark Fasheh328d5752007-06-18 10:48:04 -0700748 /*
749 * Refuse to coalesce extent records with different flag
750 * fields - we don't want to mix unwritten extents with user
751 * data.
752 */
753 if (ext->e_flags != insert_rec->e_flags)
754 return CONTIG_NONE;
755
Mark Fashehdcd05382007-01-16 11:32:23 -0800756 if (ocfs2_extents_adjacent(ext, insert_rec) &&
757 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
758 return CONTIG_RIGHT;
759
760 blkno = le64_to_cpu(ext->e_blkno);
761 if (ocfs2_extents_adjacent(insert_rec, ext) &&
762 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
763 return CONTIG_LEFT;
764
765 return CONTIG_NONE;
766}
767
768/*
769 * NOTE: We can have pretty much any combination of contiguousness and
770 * appending.
771 *
772 * The usefulness of APPEND_TAIL is more in that it lets us know that
773 * we'll have to update the path to that leaf.
774 */
775enum ocfs2_append_type {
776 APPEND_NONE = 0,
777 APPEND_TAIL,
778};
779
Mark Fasheh328d5752007-06-18 10:48:04 -0700780enum ocfs2_split_type {
781 SPLIT_NONE = 0,
782 SPLIT_LEFT,
783 SPLIT_RIGHT,
784};
785
Mark Fashehdcd05382007-01-16 11:32:23 -0800786struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700787 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800788 enum ocfs2_append_type ins_appending;
789 enum ocfs2_contig_type ins_contig;
790 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800791 int ins_tree_depth;
792};
793
Mark Fasheh328d5752007-06-18 10:48:04 -0700794struct ocfs2_merge_ctxt {
795 enum ocfs2_contig_type c_contig_type;
796 int c_has_empty_extent;
797 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700798};
799
Joel Becker5e965812008-11-13 14:49:16 -0800800static int ocfs2_validate_extent_block(struct super_block *sb,
801 struct buffer_head *bh)
802{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700803 int rc;
Joel Becker5e965812008-11-13 14:49:16 -0800804 struct ocfs2_extent_block *eb =
805 (struct ocfs2_extent_block *)bh->b_data;
806
Joel Becker970e4932008-11-13 14:49:19 -0800807 mlog(0, "Validating extent block %llu\n",
808 (unsigned long long)bh->b_blocknr);
809
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700810 BUG_ON(!buffer_uptodate(bh));
811
812 /*
813 * If the ecc fails, we return the error but otherwise
814 * leave the filesystem running. We know any error is
815 * local to this block.
816 */
817 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
Joel Becker13723d02008-10-17 19:25:01 -0700818 if (rc) {
819 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
820 (unsigned long long)bh->b_blocknr);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700821 return rc;
Joel Becker13723d02008-10-17 19:25:01 -0700822 }
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700823
824 /*
825 * Errors after here are fatal.
826 */
827
Joel Becker5e965812008-11-13 14:49:16 -0800828 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
829 ocfs2_error(sb,
830 "Extent block #%llu has bad signature %.*s",
831 (unsigned long long)bh->b_blocknr, 7,
832 eb->h_signature);
833 return -EINVAL;
834 }
835
836 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
837 ocfs2_error(sb,
838 "Extent block #%llu has an invalid h_blkno "
839 "of %llu",
840 (unsigned long long)bh->b_blocknr,
841 (unsigned long long)le64_to_cpu(eb->h_blkno));
842 return -EINVAL;
843 }
844
845 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
846 ocfs2_error(sb,
847 "Extent block #%llu has an invalid "
848 "h_fs_generation of #%u",
849 (unsigned long long)bh->b_blocknr,
850 le32_to_cpu(eb->h_fs_generation));
851 return -EINVAL;
852 }
853
854 return 0;
855}
856
Joel Becker3d03a302009-02-12 17:49:26 -0800857int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
Joel Becker5e965812008-11-13 14:49:16 -0800858 struct buffer_head **bh)
859{
860 int rc;
861 struct buffer_head *tmp = *bh;
862
Joel Becker3d03a302009-02-12 17:49:26 -0800863 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800864 ocfs2_validate_extent_block);
Joel Becker5e965812008-11-13 14:49:16 -0800865
866 /* If ocfs2_read_block() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -0800867 if (!rc && !*bh)
Joel Becker5e965812008-11-13 14:49:16 -0800868 *bh = tmp;
869
Joel Becker5e965812008-11-13 14:49:16 -0800870 return rc;
871}
872
873
Mark Fashehccd979b2005-12-15 14:31:24 -0800874/*
875 * How many free extents have we got before we need more meta data?
876 */
877int ocfs2_num_free_extents(struct ocfs2_super *osb,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700878 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -0800879{
880 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800881 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800882 struct ocfs2_extent_block *eb;
883 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800884 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800885
886 mlog_entry_void();
887
Joel Beckerf99b9b72008-08-20 19:36:33 -0700888 el = et->et_root_el;
889 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800890
Tao Mae7d4cb62008-08-18 17:38:44 +0800891 if (last_eb_blk) {
Joel Becker3d03a302009-02-12 17:49:26 -0800892 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
893 &eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800894 if (retval < 0) {
895 mlog_errno(retval);
896 goto bail;
897 }
898 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
899 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800900 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800901
902 BUG_ON(el->l_tree_depth != 0);
903
904 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
905bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700906 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800907
908 mlog_exit(retval);
909 return retval;
910}
911
912/* expects array to already be allocated
913 *
914 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
915 * l_count for you
916 */
917static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700918 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800919 struct inode *inode,
920 int wanted,
921 struct ocfs2_alloc_context *meta_ac,
922 struct buffer_head *bhs[])
923{
924 int count, status, i;
925 u16 suballoc_bit_start;
926 u32 num_got;
927 u64 first_blkno;
928 struct ocfs2_extent_block *eb;
929
930 mlog_entry_void();
931
932 count = 0;
933 while (count < wanted) {
934 status = ocfs2_claim_metadata(osb,
935 handle,
936 meta_ac,
937 wanted - count,
938 &suballoc_bit_start,
939 &num_got,
940 &first_blkno);
941 if (status < 0) {
942 mlog_errno(status);
943 goto bail;
944 }
945
946 for(i = count; i < (num_got + count); i++) {
947 bhs[i] = sb_getblk(osb->sb, first_blkno);
948 if (bhs[i] == NULL) {
949 status = -EIO;
950 mlog_errno(status);
951 goto bail;
952 }
Joel Becker8cb471e2009-02-10 20:00:41 -0800953 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode),
954 bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800955
Joel Becker0cf2f762009-02-12 16:41:25 -0800956 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), bhs[i],
Joel Becker13723d02008-10-17 19:25:01 -0700957 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800958 if (status < 0) {
959 mlog_errno(status);
960 goto bail;
961 }
962
963 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
964 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
965 /* Ok, setup the minimal stuff here. */
966 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
967 eb->h_blkno = cpu_to_le64(first_blkno);
968 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800969 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800970 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
971 eb->h_list.l_count =
972 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
973
974 suballoc_bit_start++;
975 first_blkno++;
976
977 /* We'll also be dirtied by the caller, so
978 * this isn't absolutely necessary. */
979 status = ocfs2_journal_dirty(handle, bhs[i]);
980 if (status < 0) {
981 mlog_errno(status);
982 goto bail;
983 }
984 }
985
986 count += num_got;
987 }
988
989 status = 0;
990bail:
991 if (status < 0) {
992 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -0700993 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800994 bhs[i] = NULL;
995 }
996 }
997 mlog_exit(status);
998 return status;
999}
1000
1001/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001002 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1003 *
1004 * Returns the sum of the rightmost extent rec logical offset and
1005 * cluster count.
1006 *
1007 * ocfs2_add_branch() uses this to determine what logical cluster
1008 * value should be populated into the leftmost new branch records.
1009 *
1010 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1011 * value for the new topmost tree record.
1012 */
1013static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1014{
1015 int i;
1016
1017 i = le16_to_cpu(el->l_next_free_rec) - 1;
1018
1019 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001020 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08001021}
1022
1023/*
Tao Ma6b791bc2009-06-12 14:18:36 +08001024 * Change range of the branches in the right most path according to the leaf
1025 * extent block's rightmost record.
1026 */
1027static int ocfs2_adjust_rightmost_branch(handle_t *handle,
1028 struct inode *inode,
1029 struct ocfs2_extent_tree *et)
1030{
1031 int status;
1032 struct ocfs2_path *path = NULL;
1033 struct ocfs2_extent_list *el;
1034 struct ocfs2_extent_rec *rec;
1035
1036 path = ocfs2_new_path_from_et(et);
1037 if (!path) {
1038 status = -ENOMEM;
1039 return status;
1040 }
1041
1042 status = ocfs2_find_path(inode, path, UINT_MAX);
1043 if (status < 0) {
1044 mlog_errno(status);
1045 goto out;
1046 }
1047
1048 status = ocfs2_extend_trans(handle, path_num_items(path) +
1049 handle->h_buffer_credits);
1050 if (status < 0) {
1051 mlog_errno(status);
1052 goto out;
1053 }
1054
Joel Becker0cf2f762009-02-12 16:41:25 -08001055 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Tao Ma6b791bc2009-06-12 14:18:36 +08001056 if (status < 0) {
1057 mlog_errno(status);
1058 goto out;
1059 }
1060
1061 el = path_leaf_el(path);
1062 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1063
1064 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
1065
1066out:
1067 ocfs2_free_path(path);
1068 return status;
1069}
1070
1071/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001072 * Add an entire tree branch to our inode. eb_bh is the extent block
1073 * to start at, if we don't want to start the branch at the dinode
1074 * structure.
1075 *
1076 * last_eb_bh is required as we have to update it's next_leaf pointer
1077 * for the new last extent block.
1078 *
1079 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -08001080 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -08001081 */
1082static int ocfs2_add_branch(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001083 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001084 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001085 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001086 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07001087 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001088 struct ocfs2_alloc_context *meta_ac)
1089{
1090 int status, new_blocks, i;
1091 u64 next_blkno, new_last_eb_blk;
1092 struct buffer_head *bh;
1093 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001094 struct ocfs2_extent_block *eb;
1095 struct ocfs2_extent_list *eb_el;
1096 struct ocfs2_extent_list *el;
Tao Ma6b791bc2009-06-12 14:18:36 +08001097 u32 new_cpos, root_end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001098
1099 mlog_entry_void();
1100
Mark Fasheh328d5752007-06-18 10:48:04 -07001101 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001102
Mark Fashehccd979b2005-12-15 14:31:24 -08001103 if (eb_bh) {
1104 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1105 el = &eb->h_list;
1106 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001107 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001108
1109 /* we never add a branch to a leaf. */
1110 BUG_ON(!el->l_tree_depth);
1111
1112 new_blocks = le16_to_cpu(el->l_tree_depth);
1113
Tao Ma6b791bc2009-06-12 14:18:36 +08001114 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1115 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1116 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1117
1118 /*
1119 * If there is a gap before the root end and the real end
1120 * of the righmost leaf block, we need to remove the gap
1121 * between new_cpos and root_end first so that the tree
1122 * is consistent after we add a new branch(it will start
1123 * from new_cpos).
1124 */
1125 if (root_end > new_cpos) {
1126 mlog(0, "adjust the cluster end from %u to %u\n",
1127 root_end, new_cpos);
1128 status = ocfs2_adjust_rightmost_branch(handle, inode, et);
1129 if (status) {
1130 mlog_errno(status);
1131 goto bail;
1132 }
1133 }
1134
Mark Fashehccd979b2005-12-15 14:31:24 -08001135 /* allocate the number of new eb blocks we need */
1136 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1137 GFP_KERNEL);
1138 if (!new_eb_bhs) {
1139 status = -ENOMEM;
1140 mlog_errno(status);
1141 goto bail;
1142 }
1143
1144 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
1145 meta_ac, new_eb_bhs);
1146 if (status < 0) {
1147 mlog_errno(status);
1148 goto bail;
1149 }
1150
1151 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1152 * linked with the rest of the tree.
1153 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1154 *
1155 * when we leave the loop, new_last_eb_blk will point to the
1156 * newest leaf, and next_blkno will point to the topmost extent
1157 * block. */
1158 next_blkno = new_last_eb_blk = 0;
1159 for(i = 0; i < new_blocks; i++) {
1160 bh = new_eb_bhs[i];
1161 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001162 /* ocfs2_create_new_meta_bhs() should create it right! */
1163 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001164 eb_el = &eb->h_list;
1165
Joel Becker0cf2f762009-02-12 16:41:25 -08001166 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), bh,
Joel Becker13723d02008-10-17 19:25:01 -07001167 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001168 if (status < 0) {
1169 mlog_errno(status);
1170 goto bail;
1171 }
1172
1173 eb->h_next_leaf_blk = 0;
1174 eb_el->l_tree_depth = cpu_to_le16(i);
1175 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001176 /*
1177 * This actually counts as an empty extent as
1178 * c_clusters == 0
1179 */
1180 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001181 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001182 /*
1183 * eb_el isn't always an interior node, but even leaf
1184 * nodes want a zero'd flags and reserved field so
1185 * this gets the whole 32 bits regardless of use.
1186 */
1187 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001188 if (!eb_el->l_tree_depth)
1189 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1190
1191 status = ocfs2_journal_dirty(handle, bh);
1192 if (status < 0) {
1193 mlog_errno(status);
1194 goto bail;
1195 }
1196
1197 next_blkno = le64_to_cpu(eb->h_blkno);
1198 }
1199
1200 /* This is a bit hairy. We want to update up to three blocks
1201 * here without leaving any of them in an inconsistent state
1202 * in case of error. We don't have to worry about
1203 * journal_dirty erroring as it won't unless we've aborted the
1204 * handle (in which case we would never be here) so reserving
1205 * the write with journal_access is all we need to do. */
Joel Becker0cf2f762009-02-12 16:41:25 -08001206 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), *last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001207 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001208 if (status < 0) {
1209 mlog_errno(status);
1210 goto bail;
1211 }
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001212 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001213 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001214 if (status < 0) {
1215 mlog_errno(status);
1216 goto bail;
1217 }
1218 if (eb_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08001219 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001220 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001221 if (status < 0) {
1222 mlog_errno(status);
1223 goto bail;
1224 }
1225 }
1226
1227 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001228 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001229 i = le16_to_cpu(el->l_next_free_rec);
1230 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001231 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001232 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001233 le16_add_cpu(&el->l_next_free_rec, 1);
1234
1235 /* fe needs a new last extent block pointer, as does the
1236 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001237 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001238
Mark Fasheh328d5752007-06-18 10:48:04 -07001239 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001240 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1241
Mark Fasheh328d5752007-06-18 10:48:04 -07001242 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001243 if (status < 0)
1244 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001245 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001246 if (status < 0)
1247 mlog_errno(status);
1248 if (eb_bh) {
1249 status = ocfs2_journal_dirty(handle, eb_bh);
1250 if (status < 0)
1251 mlog_errno(status);
1252 }
1253
Mark Fasheh328d5752007-06-18 10:48:04 -07001254 /*
1255 * Some callers want to track the rightmost leaf so pass it
1256 * back here.
1257 */
1258 brelse(*last_eb_bh);
1259 get_bh(new_eb_bhs[0]);
1260 *last_eb_bh = new_eb_bhs[0];
1261
Mark Fashehccd979b2005-12-15 14:31:24 -08001262 status = 0;
1263bail:
1264 if (new_eb_bhs) {
1265 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001266 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001267 kfree(new_eb_bhs);
1268 }
1269
1270 mlog_exit(status);
1271 return status;
1272}
1273
1274/*
1275 * adds another level to the allocation tree.
1276 * returns back the new extent block so you can add a branch to it
1277 * after this call.
1278 */
1279static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001280 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001281 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001282 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001283 struct ocfs2_alloc_context *meta_ac,
1284 struct buffer_head **ret_new_eb_bh)
1285{
1286 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001287 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001288 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001289 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001290 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001291 struct ocfs2_extent_list *eb_el;
1292
1293 mlog_entry_void();
1294
1295 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
1296 &new_eb_bh);
1297 if (status < 0) {
1298 mlog_errno(status);
1299 goto bail;
1300 }
1301
1302 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001303 /* ocfs2_create_new_meta_bhs() should create it right! */
1304 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001305
1306 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001307 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001308
Joel Becker0cf2f762009-02-12 16:41:25 -08001309 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), new_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001310 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001311 if (status < 0) {
1312 mlog_errno(status);
1313 goto bail;
1314 }
1315
Tao Mae7d4cb62008-08-18 17:38:44 +08001316 /* copy the root extent list data into the new extent block */
1317 eb_el->l_tree_depth = root_el->l_tree_depth;
1318 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1319 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1320 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001321
1322 status = ocfs2_journal_dirty(handle, new_eb_bh);
1323 if (status < 0) {
1324 mlog_errno(status);
1325 goto bail;
1326 }
1327
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001328 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001329 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001330 if (status < 0) {
1331 mlog_errno(status);
1332 goto bail;
1333 }
1334
Mark Fashehdcd05382007-01-16 11:32:23 -08001335 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1336
Tao Mae7d4cb62008-08-18 17:38:44 +08001337 /* update root_bh now */
1338 le16_add_cpu(&root_el->l_tree_depth, 1);
1339 root_el->l_recs[0].e_cpos = 0;
1340 root_el->l_recs[0].e_blkno = eb->h_blkno;
1341 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1342 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1343 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1344 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001345
1346 /* If this is our 1st tree depth shift, then last_eb_blk
1347 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001348 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001349 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001350
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001351 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001352 if (status < 0) {
1353 mlog_errno(status);
1354 goto bail;
1355 }
1356
1357 *ret_new_eb_bh = new_eb_bh;
1358 new_eb_bh = NULL;
1359 status = 0;
1360bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001361 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001362
1363 mlog_exit(status);
1364 return status;
1365}
1366
1367/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001368 * Should only be called when there is no space left in any of the
1369 * leaf nodes. What we want to do is find the lowest tree depth
1370 * non-leaf extent block with room for new records. There are three
1371 * valid results of this search:
1372 *
1373 * 1) a lowest extent block is found, then we pass it back in
1374 * *lowest_eb_bh and return '0'
1375 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001376 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001377 * pass NULL back in *lowest_eb_bh, but still return '0'
1378 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001379 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001380 * which case we return > 0
1381 *
1382 * return status < 0 indicates an error.
1383 */
1384static int ocfs2_find_branch_target(struct ocfs2_super *osb,
Tao Mae7d4cb62008-08-18 17:38:44 +08001385 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001386 struct buffer_head **target_bh)
1387{
1388 int status = 0, i;
1389 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001390 struct ocfs2_extent_block *eb;
1391 struct ocfs2_extent_list *el;
1392 struct buffer_head *bh = NULL;
1393 struct buffer_head *lowest_bh = NULL;
1394
1395 mlog_entry_void();
1396
1397 *target_bh = NULL;
1398
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001399 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001400
1401 while(le16_to_cpu(el->l_tree_depth) > 1) {
1402 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08001403 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1404 "Owner %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001405 "extent list (next_free_rec == 0)",
Joel Becker3d03a302009-02-12 17:49:26 -08001406 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001407 status = -EIO;
1408 goto bail;
1409 }
1410 i = le16_to_cpu(el->l_next_free_rec) - 1;
1411 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1412 if (!blkno) {
Joel Becker3d03a302009-02-12 17:49:26 -08001413 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1414 "Owner %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001415 "list where extent # %d has no physical "
1416 "block start",
Joel Becker3d03a302009-02-12 17:49:26 -08001417 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001418 status = -EIO;
1419 goto bail;
1420 }
1421
Mark Fasheha81cb882008-10-07 14:25:16 -07001422 brelse(bh);
1423 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001424
Joel Becker3d03a302009-02-12 17:49:26 -08001425 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001426 if (status < 0) {
1427 mlog_errno(status);
1428 goto bail;
1429 }
1430
1431 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001432 el = &eb->h_list;
1433
1434 if (le16_to_cpu(el->l_next_free_rec) <
1435 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001436 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001437 lowest_bh = bh;
1438 get_bh(lowest_bh);
1439 }
1440 }
1441
1442 /* If we didn't find one and the fe doesn't have any room,
1443 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001444 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001445 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001446 status = 1;
1447
1448 *target_bh = lowest_bh;
1449bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001450 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001451
1452 mlog_exit(status);
1453 return status;
1454}
1455
Mark Fashehe48edee2007-03-07 16:46:57 -08001456/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001457 * Grow a b-tree so that it has more records.
1458 *
1459 * We might shift the tree depth in which case existing paths should
1460 * be considered invalid.
1461 *
1462 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001463 *
1464 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001465 */
1466static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001467 struct ocfs2_extent_tree *et, int *final_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07001468 struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001469 struct ocfs2_alloc_context *meta_ac)
1470{
1471 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001472 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001473 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001474 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1475 struct buffer_head *bh = NULL;
1476
1477 BUG_ON(meta_ac == NULL);
1478
Joel Becker3d03a302009-02-12 17:49:26 -08001479 shift = ocfs2_find_branch_target(osb, et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001480 if (shift < 0) {
1481 ret = shift;
1482 mlog_errno(ret);
1483 goto out;
1484 }
1485
1486 /* We traveled all the way to the bottom of the allocation tree
1487 * and didn't find room for any more extents - we need to add
1488 * another tree level */
1489 if (shift) {
1490 BUG_ON(bh);
1491 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1492
1493 /* ocfs2_shift_tree_depth will return us a buffer with
1494 * the new extent block (so we can pass that to
1495 * ocfs2_add_branch). */
Tao Mae7d4cb62008-08-18 17:38:44 +08001496 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001497 meta_ac, &bh);
1498 if (ret < 0) {
1499 mlog_errno(ret);
1500 goto out;
1501 }
1502 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001503 if (depth == 1) {
1504 /*
1505 * Special case: we have room now if we shifted from
1506 * tree_depth 0, so no more work needs to be done.
1507 *
1508 * We won't be calling add_branch, so pass
1509 * back *last_eb_bh as the new leaf. At depth
1510 * zero, it should always be null so there's
1511 * no reason to brelse.
1512 */
1513 BUG_ON(*last_eb_bh);
1514 get_bh(bh);
1515 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001516 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001517 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001518 }
1519
1520 /* call ocfs2_add_branch to add the final part of the tree with
1521 * the new data. */
1522 mlog(0, "add branch. bh = %p\n", bh);
Tao Mae7d4cb62008-08-18 17:38:44 +08001523 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001524 meta_ac);
1525 if (ret < 0) {
1526 mlog_errno(ret);
1527 goto out;
1528 }
1529
1530out:
1531 if (final_depth)
1532 *final_depth = depth;
1533 brelse(bh);
1534 return ret;
1535}
1536
1537/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001538 * This function will discard the rightmost extent record.
1539 */
1540static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1541{
1542 int next_free = le16_to_cpu(el->l_next_free_rec);
1543 int count = le16_to_cpu(el->l_count);
1544 unsigned int num_bytes;
1545
1546 BUG_ON(!next_free);
1547 /* This will cause us to go off the end of our extent list. */
1548 BUG_ON(next_free >= count);
1549
1550 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1551
1552 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1553}
1554
1555static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1556 struct ocfs2_extent_rec *insert_rec)
1557{
1558 int i, insert_index, next_free, has_empty, num_bytes;
1559 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1560 struct ocfs2_extent_rec *rec;
1561
1562 next_free = le16_to_cpu(el->l_next_free_rec);
1563 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1564
1565 BUG_ON(!next_free);
1566
1567 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001568 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001569
1570 /*
1571 * The easiest way to approach this is to just remove the
1572 * empty extent and temporarily decrement next_free.
1573 */
1574 if (has_empty) {
1575 /*
1576 * If next_free was 1 (only an empty extent), this
1577 * loop won't execute, which is fine. We still want
1578 * the decrement above to happen.
1579 */
1580 for(i = 0; i < (next_free - 1); i++)
1581 el->l_recs[i] = el->l_recs[i+1];
1582
1583 next_free--;
1584 }
1585
1586 /*
1587 * Figure out what the new record index should be.
1588 */
1589 for(i = 0; i < next_free; i++) {
1590 rec = &el->l_recs[i];
1591
1592 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1593 break;
1594 }
1595 insert_index = i;
1596
1597 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1598 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1599
1600 BUG_ON(insert_index < 0);
1601 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1602 BUG_ON(insert_index > next_free);
1603
1604 /*
1605 * No need to memmove if we're just adding to the tail.
1606 */
1607 if (insert_index != next_free) {
1608 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1609
1610 num_bytes = next_free - insert_index;
1611 num_bytes *= sizeof(struct ocfs2_extent_rec);
1612 memmove(&el->l_recs[insert_index + 1],
1613 &el->l_recs[insert_index],
1614 num_bytes);
1615 }
1616
1617 /*
1618 * Either we had an empty extent, and need to re-increment or
1619 * there was no empty extent on a non full rightmost leaf node,
1620 * in which case we still need to increment.
1621 */
1622 next_free++;
1623 el->l_next_free_rec = cpu_to_le16(next_free);
1624 /*
1625 * Make sure none of the math above just messed up our tree.
1626 */
1627 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1628
1629 el->l_recs[insert_index] = *insert_rec;
1630
1631}
1632
Mark Fasheh328d5752007-06-18 10:48:04 -07001633static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1634{
1635 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1636
1637 BUG_ON(num_recs == 0);
1638
1639 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1640 num_recs--;
1641 size = num_recs * sizeof(struct ocfs2_extent_rec);
1642 memmove(&el->l_recs[0], &el->l_recs[1], size);
1643 memset(&el->l_recs[num_recs], 0,
1644 sizeof(struct ocfs2_extent_rec));
1645 el->l_next_free_rec = cpu_to_le16(num_recs);
1646 }
1647}
1648
Mark Fashehdcd05382007-01-16 11:32:23 -08001649/*
1650 * Create an empty extent record .
1651 *
1652 * l_next_free_rec may be updated.
1653 *
1654 * If an empty extent already exists do nothing.
1655 */
1656static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1657{
1658 int next_free = le16_to_cpu(el->l_next_free_rec);
1659
Mark Fashehe48edee2007-03-07 16:46:57 -08001660 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1661
Mark Fashehdcd05382007-01-16 11:32:23 -08001662 if (next_free == 0)
1663 goto set_and_inc;
1664
1665 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1666 return;
1667
1668 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1669 "Asked to create an empty extent in a full list:\n"
1670 "count = %u, tree depth = %u",
1671 le16_to_cpu(el->l_count),
1672 le16_to_cpu(el->l_tree_depth));
1673
1674 ocfs2_shift_records_right(el);
1675
1676set_and_inc:
1677 le16_add_cpu(&el->l_next_free_rec, 1);
1678 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1679}
1680
1681/*
1682 * For a rotation which involves two leaf nodes, the "root node" is
1683 * the lowest level tree node which contains a path to both leafs. This
1684 * resulting set of information can be used to form a complete "subtree"
1685 *
1686 * This function is passed two full paths from the dinode down to a
1687 * pair of adjacent leaves. It's task is to figure out which path
1688 * index contains the subtree root - this can be the root index itself
1689 * in a worst-case rotation.
1690 *
1691 * The array index of the subtree root is passed back.
1692 */
1693static int ocfs2_find_subtree_root(struct inode *inode,
1694 struct ocfs2_path *left,
1695 struct ocfs2_path *right)
1696{
1697 int i = 0;
1698
1699 /*
1700 * Check that the caller passed in two paths from the same tree.
1701 */
1702 BUG_ON(path_root_bh(left) != path_root_bh(right));
1703
1704 do {
1705 i++;
1706
1707 /*
1708 * The caller didn't pass two adjacent paths.
1709 */
1710 mlog_bug_on_msg(i > left->p_tree_depth,
1711 "Inode %lu, left depth %u, right depth %u\n"
1712 "left leaf blk %llu, right leaf blk %llu\n",
1713 inode->i_ino, left->p_tree_depth,
1714 right->p_tree_depth,
1715 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1716 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1717 } while (left->p_node[i].bh->b_blocknr ==
1718 right->p_node[i].bh->b_blocknr);
1719
1720 return i - 1;
1721}
1722
1723typedef void (path_insert_t)(void *, struct buffer_head *);
1724
1725/*
1726 * Traverse a btree path in search of cpos, starting at root_el.
1727 *
1728 * This code can be called with a cpos larger than the tree, in which
1729 * case it will return the rightmost path.
1730 */
1731static int __ocfs2_find_path(struct inode *inode,
1732 struct ocfs2_extent_list *root_el, u32 cpos,
1733 path_insert_t *func, void *data)
1734{
1735 int i, ret = 0;
1736 u32 range;
1737 u64 blkno;
1738 struct buffer_head *bh = NULL;
1739 struct ocfs2_extent_block *eb;
1740 struct ocfs2_extent_list *el;
1741 struct ocfs2_extent_rec *rec;
1742 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1743
1744 el = root_el;
1745 while (el->l_tree_depth) {
1746 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1747 ocfs2_error(inode->i_sb,
1748 "Inode %llu has empty extent list at "
1749 "depth %u\n",
1750 (unsigned long long)oi->ip_blkno,
1751 le16_to_cpu(el->l_tree_depth));
1752 ret = -EROFS;
1753 goto out;
1754
1755 }
1756
1757 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1758 rec = &el->l_recs[i];
1759
1760 /*
1761 * In the case that cpos is off the allocation
1762 * tree, this should just wind up returning the
1763 * rightmost record.
1764 */
1765 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001766 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001767 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1768 break;
1769 }
1770
1771 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1772 if (blkno == 0) {
1773 ocfs2_error(inode->i_sb,
1774 "Inode %llu has bad blkno in extent list "
1775 "at depth %u (index %d)\n",
1776 (unsigned long long)oi->ip_blkno,
1777 le16_to_cpu(el->l_tree_depth), i);
1778 ret = -EROFS;
1779 goto out;
1780 }
1781
1782 brelse(bh);
1783 bh = NULL;
Joel Becker3d03a302009-02-12 17:49:26 -08001784 ret = ocfs2_read_extent_block(INODE_CACHE(inode), blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001785 if (ret) {
1786 mlog_errno(ret);
1787 goto out;
1788 }
1789
1790 eb = (struct ocfs2_extent_block *) bh->b_data;
1791 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001792
1793 if (le16_to_cpu(el->l_next_free_rec) >
1794 le16_to_cpu(el->l_count)) {
1795 ocfs2_error(inode->i_sb,
1796 "Inode %llu has bad count in extent list "
1797 "at block %llu (next free=%u, count=%u)\n",
1798 (unsigned long long)oi->ip_blkno,
1799 (unsigned long long)bh->b_blocknr,
1800 le16_to_cpu(el->l_next_free_rec),
1801 le16_to_cpu(el->l_count));
1802 ret = -EROFS;
1803 goto out;
1804 }
1805
1806 if (func)
1807 func(data, bh);
1808 }
1809
1810out:
1811 /*
1812 * Catch any trailing bh that the loop didn't handle.
1813 */
1814 brelse(bh);
1815
1816 return ret;
1817}
1818
1819/*
1820 * Given an initialized path (that is, it has a valid root extent
1821 * list), this function will traverse the btree in search of the path
1822 * which would contain cpos.
1823 *
1824 * The path traveled is recorded in the path structure.
1825 *
1826 * Note that this will not do any comparisons on leaf node extent
1827 * records, so it will work fine in the case that we just added a tree
1828 * branch.
1829 */
1830struct find_path_data {
1831 int index;
1832 struct ocfs2_path *path;
1833};
1834static void find_path_ins(void *data, struct buffer_head *bh)
1835{
1836 struct find_path_data *fp = data;
1837
1838 get_bh(bh);
1839 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1840 fp->index++;
1841}
1842static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1843 u32 cpos)
1844{
1845 struct find_path_data data;
1846
1847 data.index = 1;
1848 data.path = path;
1849 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1850 find_path_ins, &data);
1851}
1852
1853static void find_leaf_ins(void *data, struct buffer_head *bh)
1854{
1855 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1856 struct ocfs2_extent_list *el = &eb->h_list;
1857 struct buffer_head **ret = data;
1858
1859 /* We want to retain only the leaf block. */
1860 if (le16_to_cpu(el->l_tree_depth) == 0) {
1861 get_bh(bh);
1862 *ret = bh;
1863 }
1864}
1865/*
1866 * Find the leaf block in the tree which would contain cpos. No
1867 * checking of the actual leaf is done.
1868 *
1869 * Some paths want to call this instead of allocating a path structure
1870 * and calling ocfs2_find_path().
1871 *
1872 * This function doesn't handle non btree extent lists.
1873 */
Mark Fasheh363041a2007-01-17 12:31:35 -08001874int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1875 u32 cpos, struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001876{
1877 int ret;
1878 struct buffer_head *bh = NULL;
1879
1880 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1881 if (ret) {
1882 mlog_errno(ret);
1883 goto out;
1884 }
1885
1886 *leaf_bh = bh;
1887out:
1888 return ret;
1889}
1890
1891/*
1892 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1893 *
1894 * Basically, we've moved stuff around at the bottom of the tree and
1895 * we need to fix up the extent records above the changes to reflect
1896 * the new changes.
1897 *
1898 * left_rec: the record on the left.
1899 * left_child_el: is the child list pointed to by left_rec
1900 * right_rec: the record to the right of left_rec
1901 * right_child_el: is the child list pointed to by right_rec
1902 *
1903 * By definition, this only works on interior nodes.
1904 */
1905static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1906 struct ocfs2_extent_list *left_child_el,
1907 struct ocfs2_extent_rec *right_rec,
1908 struct ocfs2_extent_list *right_child_el)
1909{
1910 u32 left_clusters, right_end;
1911
1912 /*
1913 * Interior nodes never have holes. Their cpos is the cpos of
1914 * the leftmost record in their child list. Their cluster
1915 * count covers the full theoretical range of their child list
1916 * - the range between their cpos and the cpos of the record
1917 * immediately to their right.
1918 */
1919 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08001920 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1921 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07001922 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1923 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1924 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001925 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001926 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001927
1928 /*
1929 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001930 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001931 * updating e_cpos to keep the same highest cluster count.
1932 */
1933 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001934 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001935
1936 right_rec->e_cpos = left_rec->e_cpos;
1937 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1938
1939 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001940 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001941}
1942
1943/*
1944 * Adjust the adjacent root node records involved in a
1945 * rotation. left_el_blkno is passed in as a key so that we can easily
1946 * find it's index in the root list.
1947 */
1948static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1949 struct ocfs2_extent_list *left_el,
1950 struct ocfs2_extent_list *right_el,
1951 u64 left_el_blkno)
1952{
1953 int i;
1954
1955 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1956 le16_to_cpu(left_el->l_tree_depth));
1957
1958 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1959 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1960 break;
1961 }
1962
1963 /*
1964 * The path walking code should have never returned a root and
1965 * two paths which are not adjacent.
1966 */
1967 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1968
1969 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1970 &root_el->l_recs[i + 1], right_el);
1971}
1972
1973/*
1974 * We've changed a leaf block (in right_path) and need to reflect that
1975 * change back up the subtree.
1976 *
1977 * This happens in multiple places:
1978 * - When we've moved an extent record from the left path leaf to the right
1979 * path leaf to make room for an empty extent in the left path leaf.
1980 * - When our insert into the right path leaf is at the leftmost edge
1981 * and requires an update of the path immediately to it's left. This
1982 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08001983 * - When we've adjusted the last extent record in the left path leaf and the
1984 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08001985 */
1986static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1987 struct ocfs2_path *left_path,
1988 struct ocfs2_path *right_path,
1989 int subtree_index)
1990{
1991 int ret, i, idx;
1992 struct ocfs2_extent_list *el, *left_el, *right_el;
1993 struct ocfs2_extent_rec *left_rec, *right_rec;
1994 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1995
1996 /*
1997 * Update the counts and position values within all the
1998 * interior nodes to reflect the leaf rotation we just did.
1999 *
2000 * The root node is handled below the loop.
2001 *
2002 * We begin the loop with right_el and left_el pointing to the
2003 * leaf lists and work our way up.
2004 *
2005 * NOTE: within this loop, left_el and right_el always refer
2006 * to the *child* lists.
2007 */
2008 left_el = path_leaf_el(left_path);
2009 right_el = path_leaf_el(right_path);
2010 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2011 mlog(0, "Adjust records at index %u\n", i);
2012
2013 /*
2014 * One nice property of knowing that all of these
2015 * nodes are below the root is that we only deal with
2016 * the leftmost right node record and the rightmost
2017 * left node record.
2018 */
2019 el = left_path->p_node[i].el;
2020 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2021 left_rec = &el->l_recs[idx];
2022
2023 el = right_path->p_node[i].el;
2024 right_rec = &el->l_recs[0];
2025
2026 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2027 right_el);
2028
2029 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2030 if (ret)
2031 mlog_errno(ret);
2032
2033 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2034 if (ret)
2035 mlog_errno(ret);
2036
2037 /*
2038 * Setup our list pointers now so that the current
2039 * parents become children in the next iteration.
2040 */
2041 left_el = left_path->p_node[i].el;
2042 right_el = right_path->p_node[i].el;
2043 }
2044
2045 /*
2046 * At the root node, adjust the two adjacent records which
2047 * begin our path to the leaves.
2048 */
2049
2050 el = left_path->p_node[subtree_index].el;
2051 left_el = left_path->p_node[subtree_index + 1].el;
2052 right_el = right_path->p_node[subtree_index + 1].el;
2053
2054 ocfs2_adjust_root_records(el, left_el, right_el,
2055 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2056
2057 root_bh = left_path->p_node[subtree_index].bh;
2058
2059 ret = ocfs2_journal_dirty(handle, root_bh);
2060 if (ret)
2061 mlog_errno(ret);
2062}
2063
2064static int ocfs2_rotate_subtree_right(struct inode *inode,
2065 handle_t *handle,
2066 struct ocfs2_path *left_path,
2067 struct ocfs2_path *right_path,
2068 int subtree_index)
2069{
2070 int ret, i;
2071 struct buffer_head *right_leaf_bh;
2072 struct buffer_head *left_leaf_bh = NULL;
2073 struct buffer_head *root_bh;
2074 struct ocfs2_extent_list *right_el, *left_el;
2075 struct ocfs2_extent_rec move_rec;
2076
2077 left_leaf_bh = path_leaf_bh(left_path);
2078 left_el = path_leaf_el(left_path);
2079
2080 if (left_el->l_next_free_rec != left_el->l_count) {
2081 ocfs2_error(inode->i_sb,
2082 "Inode %llu has non-full interior leaf node %llu"
2083 "(next free = %u)",
2084 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2085 (unsigned long long)left_leaf_bh->b_blocknr,
2086 le16_to_cpu(left_el->l_next_free_rec));
2087 return -EROFS;
2088 }
2089
2090 /*
2091 * This extent block may already have an empty record, so we
2092 * return early if so.
2093 */
2094 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2095 return 0;
2096
2097 root_bh = left_path->p_node[subtree_index].bh;
2098 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2099
Joel Becker0cf2f762009-02-12 16:41:25 -08002100 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002101 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002102 if (ret) {
2103 mlog_errno(ret);
2104 goto out;
2105 }
2106
2107 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -08002108 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002109 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002110 if (ret) {
2111 mlog_errno(ret);
2112 goto out;
2113 }
2114
Joel Becker0cf2f762009-02-12 16:41:25 -08002115 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002116 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002117 if (ret) {
2118 mlog_errno(ret);
2119 goto out;
2120 }
2121 }
2122
2123 right_leaf_bh = path_leaf_bh(right_path);
2124 right_el = path_leaf_el(right_path);
2125
2126 /* This is a code error, not a disk corruption. */
2127 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2128 "because rightmost leaf block %llu is empty\n",
2129 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2130 (unsigned long long)right_leaf_bh->b_blocknr);
2131
2132 ocfs2_create_empty_extent(right_el);
2133
2134 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2135 if (ret) {
2136 mlog_errno(ret);
2137 goto out;
2138 }
2139
2140 /* Do the copy now. */
2141 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2142 move_rec = left_el->l_recs[i];
2143 right_el->l_recs[0] = move_rec;
2144
2145 /*
2146 * Clear out the record we just copied and shift everything
2147 * over, leaving an empty extent in the left leaf.
2148 *
2149 * We temporarily subtract from next_free_rec so that the
2150 * shift will lose the tail record (which is now defunct).
2151 */
2152 le16_add_cpu(&left_el->l_next_free_rec, -1);
2153 ocfs2_shift_records_right(left_el);
2154 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2155 le16_add_cpu(&left_el->l_next_free_rec, 1);
2156
2157 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2158 if (ret) {
2159 mlog_errno(ret);
2160 goto out;
2161 }
2162
2163 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2164 subtree_index);
2165
2166out:
2167 return ret;
2168}
2169
2170/*
2171 * Given a full path, determine what cpos value would return us a path
2172 * containing the leaf immediately to the left of the current one.
2173 *
2174 * Will return zero if the path passed in is already the leftmost path.
2175 */
2176static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2177 struct ocfs2_path *path, u32 *cpos)
2178{
2179 int i, j, ret = 0;
2180 u64 blkno;
2181 struct ocfs2_extent_list *el;
2182
Mark Fashehe48edee2007-03-07 16:46:57 -08002183 BUG_ON(path->p_tree_depth == 0);
2184
Mark Fashehdcd05382007-01-16 11:32:23 -08002185 *cpos = 0;
2186
2187 blkno = path_leaf_bh(path)->b_blocknr;
2188
2189 /* Start at the tree node just above the leaf and work our way up. */
2190 i = path->p_tree_depth - 1;
2191 while (i >= 0) {
2192 el = path->p_node[i].el;
2193
2194 /*
2195 * Find the extent record just before the one in our
2196 * path.
2197 */
2198 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2199 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2200 if (j == 0) {
2201 if (i == 0) {
2202 /*
2203 * We've determined that the
2204 * path specified is already
2205 * the leftmost one - return a
2206 * cpos of zero.
2207 */
2208 goto out;
2209 }
2210 /*
2211 * The leftmost record points to our
2212 * leaf - we need to travel up the
2213 * tree one level.
2214 */
2215 goto next_node;
2216 }
2217
2218 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002219 *cpos = *cpos + ocfs2_rec_clusters(el,
2220 &el->l_recs[j - 1]);
2221 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002222 goto out;
2223 }
2224 }
2225
2226 /*
2227 * If we got here, we never found a valid node where
2228 * the tree indicated one should be.
2229 */
2230 ocfs2_error(sb,
2231 "Invalid extent tree at extent block %llu\n",
2232 (unsigned long long)blkno);
2233 ret = -EROFS;
2234 goto out;
2235
2236next_node:
2237 blkno = path->p_node[i].bh->b_blocknr;
2238 i--;
2239 }
2240
2241out:
2242 return ret;
2243}
2244
Mark Fasheh328d5752007-06-18 10:48:04 -07002245/*
2246 * Extend the transaction by enough credits to complete the rotation,
2247 * and still leave at least the original number of credits allocated
2248 * to this transaction.
2249 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002250static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002251 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002252 struct ocfs2_path *path)
2253{
Mark Fasheh328d5752007-06-18 10:48:04 -07002254 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002255
2256 if (handle->h_buffer_credits < credits)
2257 return ocfs2_extend_trans(handle, credits);
2258
2259 return 0;
2260}
2261
2262/*
2263 * Trap the case where we're inserting into the theoretical range past
2264 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2265 * whose cpos is less than ours into the right leaf.
2266 *
2267 * It's only necessary to look at the rightmost record of the left
2268 * leaf because the logic that calls us should ensure that the
2269 * theoretical ranges in the path components above the leaves are
2270 * correct.
2271 */
2272static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2273 u32 insert_cpos)
2274{
2275 struct ocfs2_extent_list *left_el;
2276 struct ocfs2_extent_rec *rec;
2277 int next_free;
2278
2279 left_el = path_leaf_el(left_path);
2280 next_free = le16_to_cpu(left_el->l_next_free_rec);
2281 rec = &left_el->l_recs[next_free - 1];
2282
2283 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2284 return 1;
2285 return 0;
2286}
2287
Mark Fasheh328d5752007-06-18 10:48:04 -07002288static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2289{
2290 int next_free = le16_to_cpu(el->l_next_free_rec);
2291 unsigned int range;
2292 struct ocfs2_extent_rec *rec;
2293
2294 if (next_free == 0)
2295 return 0;
2296
2297 rec = &el->l_recs[0];
2298 if (ocfs2_is_empty_extent(rec)) {
2299 /* Empty list. */
2300 if (next_free == 1)
2301 return 0;
2302 rec = &el->l_recs[1];
2303 }
2304
2305 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2306 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2307 return 1;
2308 return 0;
2309}
2310
Mark Fashehdcd05382007-01-16 11:32:23 -08002311/*
2312 * Rotate all the records in a btree right one record, starting at insert_cpos.
2313 *
2314 * The path to the rightmost leaf should be passed in.
2315 *
2316 * The array is assumed to be large enough to hold an entire path (tree depth).
2317 *
2318 * Upon succesful return from this function:
2319 *
2320 * - The 'right_path' array will contain a path to the leaf block
2321 * whose range contains e_cpos.
2322 * - That leaf block will have a single empty extent in list index 0.
2323 * - In the case that the rotation requires a post-insert update,
2324 * *ret_left_path will contain a valid path which can be passed to
2325 * ocfs2_insert_path().
2326 */
2327static int ocfs2_rotate_tree_right(struct inode *inode,
2328 handle_t *handle,
Mark Fasheh328d5752007-06-18 10:48:04 -07002329 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002330 u32 insert_cpos,
2331 struct ocfs2_path *right_path,
2332 struct ocfs2_path **ret_left_path)
2333{
Mark Fasheh328d5752007-06-18 10:48:04 -07002334 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002335 u32 cpos;
2336 struct ocfs2_path *left_path = NULL;
2337
2338 *ret_left_path = NULL;
2339
Joel Beckerffdd7a52008-10-17 22:32:01 -07002340 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002341 if (!left_path) {
2342 ret = -ENOMEM;
2343 mlog_errno(ret);
2344 goto out;
2345 }
2346
2347 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2348 if (ret) {
2349 mlog_errno(ret);
2350 goto out;
2351 }
2352
2353 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2354
2355 /*
2356 * What we want to do here is:
2357 *
2358 * 1) Start with the rightmost path.
2359 *
2360 * 2) Determine a path to the leaf block directly to the left
2361 * of that leaf.
2362 *
2363 * 3) Determine the 'subtree root' - the lowest level tree node
2364 * which contains a path to both leaves.
2365 *
2366 * 4) Rotate the subtree.
2367 *
2368 * 5) Find the next subtree by considering the left path to be
2369 * the new right path.
2370 *
2371 * The check at the top of this while loop also accepts
2372 * insert_cpos == cpos because cpos is only a _theoretical_
2373 * value to get us the left path - insert_cpos might very well
2374 * be filling that hole.
2375 *
2376 * Stop at a cpos of '0' because we either started at the
2377 * leftmost branch (i.e., a tree with one branch and a
2378 * rotation inside of it), or we've gone as far as we can in
2379 * rotating subtrees.
2380 */
2381 while (cpos && insert_cpos <= cpos) {
2382 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2383 insert_cpos, cpos);
2384
2385 ret = ocfs2_find_path(inode, left_path, cpos);
2386 if (ret) {
2387 mlog_errno(ret);
2388 goto out;
2389 }
2390
2391 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2392 path_leaf_bh(right_path),
2393 "Inode %lu: error during insert of %u "
2394 "(left path cpos %u) results in two identical "
2395 "paths ending at %llu\n",
2396 inode->i_ino, insert_cpos, cpos,
2397 (unsigned long long)
2398 path_leaf_bh(left_path)->b_blocknr);
2399
Mark Fasheh328d5752007-06-18 10:48:04 -07002400 if (split == SPLIT_NONE &&
2401 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002402 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002403
2404 /*
2405 * We've rotated the tree as much as we
2406 * should. The rest is up to
2407 * ocfs2_insert_path() to complete, after the
2408 * record insertion. We indicate this
2409 * situation by returning the left path.
2410 *
2411 * The reason we don't adjust the records here
2412 * before the record insert is that an error
2413 * later might break the rule where a parent
2414 * record e_cpos will reflect the actual
2415 * e_cpos of the 1st nonempty record of the
2416 * child list.
2417 */
2418 *ret_left_path = left_path;
2419 goto out_ret_path;
2420 }
2421
2422 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2423
2424 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2425 start,
2426 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2427 right_path->p_tree_depth);
2428
2429 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002430 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002431 if (ret) {
2432 mlog_errno(ret);
2433 goto out;
2434 }
2435
2436 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2437 right_path, start);
2438 if (ret) {
2439 mlog_errno(ret);
2440 goto out;
2441 }
2442
Mark Fasheh328d5752007-06-18 10:48:04 -07002443 if (split != SPLIT_NONE &&
2444 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2445 insert_cpos)) {
2446 /*
2447 * A rotate moves the rightmost left leaf
2448 * record over to the leftmost right leaf
2449 * slot. If we're doing an extent split
2450 * instead of a real insert, then we have to
2451 * check that the extent to be split wasn't
2452 * just moved over. If it was, then we can
2453 * exit here, passing left_path back -
2454 * ocfs2_split_extent() is smart enough to
2455 * search both leaves.
2456 */
2457 *ret_left_path = left_path;
2458 goto out_ret_path;
2459 }
2460
Mark Fashehdcd05382007-01-16 11:32:23 -08002461 /*
2462 * There is no need to re-read the next right path
2463 * as we know that it'll be our current left
2464 * path. Optimize by copying values instead.
2465 */
2466 ocfs2_mv_path(right_path, left_path);
2467
2468 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2469 &cpos);
2470 if (ret) {
2471 mlog_errno(ret);
2472 goto out;
2473 }
2474 }
2475
2476out:
2477 ocfs2_free_path(left_path);
2478
2479out_ret_path:
2480 return ret;
2481}
2482
Tao Ma3c5e1062009-07-21 15:42:05 +08002483static int ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2484 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002485{
Tao Ma3c5e1062009-07-21 15:42:05 +08002486 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002487 struct ocfs2_extent_rec *rec;
2488 struct ocfs2_extent_list *el;
2489 struct ocfs2_extent_block *eb;
2490 u32 range;
2491
Tao Ma3c5e1062009-07-21 15:42:05 +08002492 /*
2493 * In normal tree rotation process, we will never touch the
2494 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2495 * doesn't reserve the credits for them either.
2496 *
2497 * But we do have a special case here which will update the rightmost
2498 * records for all the bh in the path.
2499 * So we have to allocate extra credits and access them.
2500 */
2501 ret = ocfs2_extend_trans(handle,
2502 handle->h_buffer_credits + subtree_index);
2503 if (ret) {
2504 mlog_errno(ret);
2505 goto out;
2506 }
2507
Joel Becker0cf2f762009-02-12 16:41:25 -08002508 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002509 if (ret) {
2510 mlog_errno(ret);
2511 goto out;
2512 }
2513
Mark Fasheh328d5752007-06-18 10:48:04 -07002514 /* Path should always be rightmost. */
2515 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2516 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2517
2518 el = &eb->h_list;
2519 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2520 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2521 rec = &el->l_recs[idx];
2522 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2523
2524 for (i = 0; i < path->p_tree_depth; i++) {
2525 el = path->p_node[i].el;
2526 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2527 rec = &el->l_recs[idx];
2528
2529 rec->e_int_clusters = cpu_to_le32(range);
2530 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2531
2532 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2533 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002534out:
2535 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002536}
2537
2538static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2539 struct ocfs2_cached_dealloc_ctxt *dealloc,
2540 struct ocfs2_path *path, int unlink_start)
2541{
2542 int ret, i;
2543 struct ocfs2_extent_block *eb;
2544 struct ocfs2_extent_list *el;
2545 struct buffer_head *bh;
2546
2547 for(i = unlink_start; i < path_num_items(path); i++) {
2548 bh = path->p_node[i].bh;
2549
2550 eb = (struct ocfs2_extent_block *)bh->b_data;
2551 /*
2552 * Not all nodes might have had their final count
2553 * decremented by the caller - handle this here.
2554 */
2555 el = &eb->h_list;
2556 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2557 mlog(ML_ERROR,
2558 "Inode %llu, attempted to remove extent block "
2559 "%llu with %u records\n",
2560 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2561 (unsigned long long)le64_to_cpu(eb->h_blkno),
2562 le16_to_cpu(el->l_next_free_rec));
2563
2564 ocfs2_journal_dirty(handle, bh);
Joel Becker8cb471e2009-02-10 20:00:41 -08002565 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002566 continue;
2567 }
2568
2569 el->l_next_free_rec = 0;
2570 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2571
2572 ocfs2_journal_dirty(handle, bh);
2573
2574 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2575 if (ret)
2576 mlog_errno(ret);
2577
Joel Becker8cb471e2009-02-10 20:00:41 -08002578 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002579 }
2580}
2581
2582static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2583 struct ocfs2_path *left_path,
2584 struct ocfs2_path *right_path,
2585 int subtree_index,
2586 struct ocfs2_cached_dealloc_ctxt *dealloc)
2587{
2588 int i;
2589 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2590 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2591 struct ocfs2_extent_list *el;
2592 struct ocfs2_extent_block *eb;
2593
2594 el = path_leaf_el(left_path);
2595
2596 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2597
2598 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2599 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2600 break;
2601
2602 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2603
2604 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2605 le16_add_cpu(&root_el->l_next_free_rec, -1);
2606
2607 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2608 eb->h_next_leaf_blk = 0;
2609
2610 ocfs2_journal_dirty(handle, root_bh);
2611 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2612
2613 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2614 subtree_index + 1);
2615}
2616
2617static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2618 struct ocfs2_path *left_path,
2619 struct ocfs2_path *right_path,
2620 int subtree_index,
2621 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002622 int *deleted,
2623 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002624{
2625 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002626 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002627 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2628 struct ocfs2_extent_block *eb;
2629
2630 *deleted = 0;
2631
2632 right_leaf_el = path_leaf_el(right_path);
2633 left_leaf_el = path_leaf_el(left_path);
2634 root_bh = left_path->p_node[subtree_index].bh;
2635 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2636
2637 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2638 return 0;
2639
2640 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2641 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2642 /*
2643 * It's legal for us to proceed if the right leaf is
2644 * the rightmost one and it has an empty extent. There
2645 * are two cases to handle - whether the leaf will be
2646 * empty after removal or not. If the leaf isn't empty
2647 * then just remove the empty extent up front. The
2648 * next block will handle empty leaves by flagging
2649 * them for unlink.
2650 *
2651 * Non rightmost leaves will throw -EAGAIN and the
2652 * caller can manually move the subtree and retry.
2653 */
2654
2655 if (eb->h_next_leaf_blk != 0ULL)
2656 return -EAGAIN;
2657
2658 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker0cf2f762009-02-12 16:41:25 -08002659 ret = ocfs2_journal_access_eb(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002660 path_leaf_bh(right_path),
2661 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002662 if (ret) {
2663 mlog_errno(ret);
2664 goto out;
2665 }
2666
2667 ocfs2_remove_empty_extent(right_leaf_el);
2668 } else
2669 right_has_empty = 1;
2670 }
2671
2672 if (eb->h_next_leaf_blk == 0ULL &&
2673 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2674 /*
2675 * We have to update i_last_eb_blk during the meta
2676 * data delete.
2677 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08002678 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07002679 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002680 if (ret) {
2681 mlog_errno(ret);
2682 goto out;
2683 }
2684
2685 del_right_subtree = 1;
2686 }
2687
2688 /*
2689 * Getting here with an empty extent in the right path implies
2690 * that it's the rightmost path and will be deleted.
2691 */
2692 BUG_ON(right_has_empty && !del_right_subtree);
2693
Joel Becker0cf2f762009-02-12 16:41:25 -08002694 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002695 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002696 if (ret) {
2697 mlog_errno(ret);
2698 goto out;
2699 }
2700
2701 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -08002702 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002703 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002704 if (ret) {
2705 mlog_errno(ret);
2706 goto out;
2707 }
2708
Joel Becker0cf2f762009-02-12 16:41:25 -08002709 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002710 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002711 if (ret) {
2712 mlog_errno(ret);
2713 goto out;
2714 }
2715 }
2716
2717 if (!right_has_empty) {
2718 /*
2719 * Only do this if we're moving a real
2720 * record. Otherwise, the action is delayed until
2721 * after removal of the right path in which case we
2722 * can do a simple shift to remove the empty extent.
2723 */
2724 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2725 memset(&right_leaf_el->l_recs[0], 0,
2726 sizeof(struct ocfs2_extent_rec));
2727 }
2728 if (eb->h_next_leaf_blk == 0ULL) {
2729 /*
2730 * Move recs over to get rid of empty extent, decrease
2731 * next_free. This is allowed to remove the last
2732 * extent in our leaf (setting l_next_free_rec to
2733 * zero) - the delete code below won't care.
2734 */
2735 ocfs2_remove_empty_extent(right_leaf_el);
2736 }
2737
2738 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2739 if (ret)
2740 mlog_errno(ret);
2741 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2742 if (ret)
2743 mlog_errno(ret);
2744
2745 if (del_right_subtree) {
2746 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2747 subtree_index, dealloc);
Tao Ma3c5e1062009-07-21 15:42:05 +08002748 ret = ocfs2_update_edge_lengths(inode, handle, subtree_index,
2749 left_path);
2750 if (ret) {
2751 mlog_errno(ret);
2752 goto out;
2753 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002754
2755 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002756 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002757
2758 /*
2759 * Removal of the extent in the left leaf was skipped
2760 * above so we could delete the right path
2761 * 1st.
2762 */
2763 if (right_has_empty)
2764 ocfs2_remove_empty_extent(left_leaf_el);
2765
Tao Mae7d4cb62008-08-18 17:38:44 +08002766 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002767 if (ret)
2768 mlog_errno(ret);
2769
2770 *deleted = 1;
2771 } else
2772 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2773 subtree_index);
2774
2775out:
2776 return ret;
2777}
2778
2779/*
2780 * Given a full path, determine what cpos value would return us a path
2781 * containing the leaf immediately to the right of the current one.
2782 *
2783 * Will return zero if the path passed in is already the rightmost path.
2784 *
2785 * This looks similar, but is subtly different to
2786 * ocfs2_find_cpos_for_left_leaf().
2787 */
2788static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2789 struct ocfs2_path *path, u32 *cpos)
2790{
2791 int i, j, ret = 0;
2792 u64 blkno;
2793 struct ocfs2_extent_list *el;
2794
2795 *cpos = 0;
2796
2797 if (path->p_tree_depth == 0)
2798 return 0;
2799
2800 blkno = path_leaf_bh(path)->b_blocknr;
2801
2802 /* Start at the tree node just above the leaf and work our way up. */
2803 i = path->p_tree_depth - 1;
2804 while (i >= 0) {
2805 int next_free;
2806
2807 el = path->p_node[i].el;
2808
2809 /*
2810 * Find the extent record just after the one in our
2811 * path.
2812 */
2813 next_free = le16_to_cpu(el->l_next_free_rec);
2814 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2815 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2816 if (j == (next_free - 1)) {
2817 if (i == 0) {
2818 /*
2819 * We've determined that the
2820 * path specified is already
2821 * the rightmost one - return a
2822 * cpos of zero.
2823 */
2824 goto out;
2825 }
2826 /*
2827 * The rightmost record points to our
2828 * leaf - we need to travel up the
2829 * tree one level.
2830 */
2831 goto next_node;
2832 }
2833
2834 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2835 goto out;
2836 }
2837 }
2838
2839 /*
2840 * If we got here, we never found a valid node where
2841 * the tree indicated one should be.
2842 */
2843 ocfs2_error(sb,
2844 "Invalid extent tree at extent block %llu\n",
2845 (unsigned long long)blkno);
2846 ret = -EROFS;
2847 goto out;
2848
2849next_node:
2850 blkno = path->p_node[i].bh->b_blocknr;
2851 i--;
2852 }
2853
2854out:
2855 return ret;
2856}
2857
2858static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2859 handle_t *handle,
Joel Becker13723d02008-10-17 19:25:01 -07002860 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002861{
2862 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002863 struct buffer_head *bh = path_leaf_bh(path);
2864 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002865
2866 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2867 return 0;
2868
Joel Becker0cf2f762009-02-12 16:41:25 -08002869 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path,
Joel Becker13723d02008-10-17 19:25:01 -07002870 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002871 if (ret) {
2872 mlog_errno(ret);
2873 goto out;
2874 }
2875
2876 ocfs2_remove_empty_extent(el);
2877
2878 ret = ocfs2_journal_dirty(handle, bh);
2879 if (ret)
2880 mlog_errno(ret);
2881
2882out:
2883 return ret;
2884}
2885
2886static int __ocfs2_rotate_tree_left(struct inode *inode,
2887 handle_t *handle, int orig_credits,
2888 struct ocfs2_path *path,
2889 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002890 struct ocfs2_path **empty_extent_path,
2891 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002892{
2893 int ret, subtree_root, deleted;
2894 u32 right_cpos;
2895 struct ocfs2_path *left_path = NULL;
2896 struct ocfs2_path *right_path = NULL;
2897
2898 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2899
2900 *empty_extent_path = NULL;
2901
2902 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2903 &right_cpos);
2904 if (ret) {
2905 mlog_errno(ret);
2906 goto out;
2907 }
2908
Joel Beckerffdd7a52008-10-17 22:32:01 -07002909 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002910 if (!left_path) {
2911 ret = -ENOMEM;
2912 mlog_errno(ret);
2913 goto out;
2914 }
2915
2916 ocfs2_cp_path(left_path, path);
2917
Joel Beckerffdd7a52008-10-17 22:32:01 -07002918 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002919 if (!right_path) {
2920 ret = -ENOMEM;
2921 mlog_errno(ret);
2922 goto out;
2923 }
2924
2925 while (right_cpos) {
2926 ret = ocfs2_find_path(inode, right_path, right_cpos);
2927 if (ret) {
2928 mlog_errno(ret);
2929 goto out;
2930 }
2931
2932 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2933 right_path);
2934
2935 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2936 subtree_root,
2937 (unsigned long long)
2938 right_path->p_node[subtree_root].bh->b_blocknr,
2939 right_path->p_tree_depth);
2940
2941 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2942 orig_credits, left_path);
2943 if (ret) {
2944 mlog_errno(ret);
2945 goto out;
2946 }
2947
Mark Fashehe8aed342007-12-03 16:43:01 -08002948 /*
2949 * Caller might still want to make changes to the
2950 * tree root, so re-add it to the journal here.
2951 */
Joel Becker0cf2f762009-02-12 16:41:25 -08002952 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002953 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08002954 if (ret) {
2955 mlog_errno(ret);
2956 goto out;
2957 }
2958
Mark Fasheh328d5752007-06-18 10:48:04 -07002959 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2960 right_path, subtree_root,
Tao Mae7d4cb62008-08-18 17:38:44 +08002961 dealloc, &deleted, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002962 if (ret == -EAGAIN) {
2963 /*
2964 * The rotation has to temporarily stop due to
2965 * the right subtree having an empty
2966 * extent. Pass it back to the caller for a
2967 * fixup.
2968 */
2969 *empty_extent_path = right_path;
2970 right_path = NULL;
2971 goto out;
2972 }
2973 if (ret) {
2974 mlog_errno(ret);
2975 goto out;
2976 }
2977
2978 /*
2979 * The subtree rotate might have removed records on
2980 * the rightmost edge. If so, then rotation is
2981 * complete.
2982 */
2983 if (deleted)
2984 break;
2985
2986 ocfs2_mv_path(left_path, right_path);
2987
2988 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2989 &right_cpos);
2990 if (ret) {
2991 mlog_errno(ret);
2992 goto out;
2993 }
2994 }
2995
2996out:
2997 ocfs2_free_path(right_path);
2998 ocfs2_free_path(left_path);
2999
3000 return ret;
3001}
3002
3003static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003004 struct ocfs2_path *path,
3005 struct ocfs2_cached_dealloc_ctxt *dealloc,
3006 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003007{
3008 int ret, subtree_index;
3009 u32 cpos;
3010 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003011 struct ocfs2_extent_block *eb;
3012 struct ocfs2_extent_list *el;
3013
Mark Fasheh328d5752007-06-18 10:48:04 -07003014
Joel Becker35dc0aa2008-08-20 16:25:06 -07003015 ret = ocfs2_et_sanity_check(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003016 if (ret)
3017 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003018 /*
3019 * There's two ways we handle this depending on
3020 * whether path is the only existing one.
3021 */
3022 ret = ocfs2_extend_rotate_transaction(handle, 0,
3023 handle->h_buffer_credits,
3024 path);
3025 if (ret) {
3026 mlog_errno(ret);
3027 goto out;
3028 }
3029
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003030 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003031 if (ret) {
3032 mlog_errno(ret);
3033 goto out;
3034 }
3035
Joel Becker3d03a302009-02-12 17:49:26 -08003036 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3037 path, &cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003038 if (ret) {
3039 mlog_errno(ret);
3040 goto out;
3041 }
3042
3043 if (cpos) {
3044 /*
3045 * We have a path to the left of this one - it needs
3046 * an update too.
3047 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003048 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003049 if (!left_path) {
3050 ret = -ENOMEM;
3051 mlog_errno(ret);
3052 goto out;
3053 }
3054
3055 ret = ocfs2_find_path(inode, left_path, cpos);
3056 if (ret) {
3057 mlog_errno(ret);
3058 goto out;
3059 }
3060
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003061 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003062 if (ret) {
3063 mlog_errno(ret);
3064 goto out;
3065 }
3066
3067 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
3068
3069 ocfs2_unlink_subtree(inode, handle, left_path, path,
3070 subtree_index, dealloc);
Tao Ma3c5e1062009-07-21 15:42:05 +08003071 ret = ocfs2_update_edge_lengths(inode, handle, subtree_index,
3072 left_path);
3073 if (ret) {
3074 mlog_errno(ret);
3075 goto out;
3076 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003077
3078 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003079 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003080 } else {
3081 /*
3082 * 'path' is also the leftmost path which
3083 * means it must be the only one. This gets
3084 * handled differently because we want to
3085 * revert the inode back to having extents
3086 * in-line.
3087 */
3088 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
3089
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003090 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003091 el->l_tree_depth = 0;
3092 el->l_next_free_rec = 0;
3093 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3094
Joel Becker35dc0aa2008-08-20 16:25:06 -07003095 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003096 }
3097
3098 ocfs2_journal_dirty(handle, path_root_bh(path));
3099
3100out:
3101 ocfs2_free_path(left_path);
3102 return ret;
3103}
3104
3105/*
3106 * Left rotation of btree records.
3107 *
3108 * In many ways, this is (unsurprisingly) the opposite of right
3109 * rotation. We start at some non-rightmost path containing an empty
3110 * extent in the leaf block. The code works its way to the rightmost
3111 * path by rotating records to the left in every subtree.
3112 *
3113 * This is used by any code which reduces the number of extent records
3114 * in a leaf. After removal, an empty record should be placed in the
3115 * leftmost list position.
3116 *
3117 * This won't handle a length update of the rightmost path records if
3118 * the rightmost tree leaf record is removed so the caller is
3119 * responsible for detecting and correcting that.
3120 */
3121static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
3122 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003123 struct ocfs2_cached_dealloc_ctxt *dealloc,
3124 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003125{
3126 int ret, orig_credits = handle->h_buffer_credits;
3127 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3128 struct ocfs2_extent_block *eb;
3129 struct ocfs2_extent_list *el;
3130
3131 el = path_leaf_el(path);
3132 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3133 return 0;
3134
3135 if (path->p_tree_depth == 0) {
3136rightmost_no_delete:
3137 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003138 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003139 * it up front.
3140 */
3141 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
Joel Becker13723d02008-10-17 19:25:01 -07003142 path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003143 if (ret)
3144 mlog_errno(ret);
3145 goto out;
3146 }
3147
3148 /*
3149 * Handle rightmost branch now. There's several cases:
3150 * 1) simple rotation leaving records in there. That's trivial.
3151 * 2) rotation requiring a branch delete - there's no more
3152 * records left. Two cases of this:
3153 * a) There are branches to the left.
3154 * b) This is also the leftmost (the only) branch.
3155 *
3156 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3157 * 2a) we need the left branch so that we can update it with the unlink
3158 * 2b) we need to bring the inode back to inline extents.
3159 */
3160
3161 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3162 el = &eb->h_list;
3163 if (eb->h_next_leaf_blk == 0) {
3164 /*
3165 * This gets a bit tricky if we're going to delete the
3166 * rightmost path. Get the other cases out of the way
3167 * 1st.
3168 */
3169 if (le16_to_cpu(el->l_next_free_rec) > 1)
3170 goto rightmost_no_delete;
3171
3172 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3173 ret = -EIO;
3174 ocfs2_error(inode->i_sb,
3175 "Inode %llu has empty extent block at %llu",
3176 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3177 (unsigned long long)le64_to_cpu(eb->h_blkno));
3178 goto out;
3179 }
3180
3181 /*
3182 * XXX: The caller can not trust "path" any more after
3183 * this as it will have been deleted. What do we do?
3184 *
3185 * In theory the rotate-for-merge code will never get
3186 * here because it'll always ask for a rotate in a
3187 * nonempty list.
3188 */
3189
3190 ret = ocfs2_remove_rightmost_path(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003191 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003192 if (ret)
3193 mlog_errno(ret);
3194 goto out;
3195 }
3196
3197 /*
3198 * Now we can loop, remembering the path we get from -EAGAIN
3199 * and restarting from there.
3200 */
3201try_rotate:
3202 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003203 dealloc, &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003204 if (ret && ret != -EAGAIN) {
3205 mlog_errno(ret);
3206 goto out;
3207 }
3208
3209 while (ret == -EAGAIN) {
3210 tmp_path = restart_path;
3211 restart_path = NULL;
3212
3213 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
3214 tmp_path, dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003215 &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003216 if (ret && ret != -EAGAIN) {
3217 mlog_errno(ret);
3218 goto out;
3219 }
3220
3221 ocfs2_free_path(tmp_path);
3222 tmp_path = NULL;
3223
3224 if (ret == 0)
3225 goto try_rotate;
3226 }
3227
3228out:
3229 ocfs2_free_path(tmp_path);
3230 ocfs2_free_path(restart_path);
3231 return ret;
3232}
3233
3234static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3235 int index)
3236{
3237 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3238 unsigned int size;
3239
3240 if (rec->e_leaf_clusters == 0) {
3241 /*
3242 * We consumed all of the merged-from record. An empty
3243 * extent cannot exist anywhere but the 1st array
3244 * position, so move things over if the merged-from
3245 * record doesn't occupy that position.
3246 *
3247 * This creates a new empty extent so the caller
3248 * should be smart enough to have removed any existing
3249 * ones.
3250 */
3251 if (index > 0) {
3252 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3253 size = index * sizeof(struct ocfs2_extent_rec);
3254 memmove(&el->l_recs[1], &el->l_recs[0], size);
3255 }
3256
3257 /*
3258 * Always memset - the caller doesn't check whether it
3259 * created an empty extent, so there could be junk in
3260 * the other fields.
3261 */
3262 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3263 }
3264}
3265
Tao Ma677b9752008-01-30 14:21:05 +08003266static int ocfs2_get_right_path(struct inode *inode,
3267 struct ocfs2_path *left_path,
3268 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003269{
3270 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003271 u32 right_cpos;
3272 struct ocfs2_path *right_path = NULL;
3273 struct ocfs2_extent_list *left_el;
3274
3275 *ret_right_path = NULL;
3276
3277 /* This function shouldn't be called for non-trees. */
3278 BUG_ON(left_path->p_tree_depth == 0);
3279
3280 left_el = path_leaf_el(left_path);
3281 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3282
3283 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3284 &right_cpos);
3285 if (ret) {
3286 mlog_errno(ret);
3287 goto out;
3288 }
3289
3290 /* This function shouldn't be called for the rightmost leaf. */
3291 BUG_ON(right_cpos == 0);
3292
Joel Beckerffdd7a52008-10-17 22:32:01 -07003293 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003294 if (!right_path) {
3295 ret = -ENOMEM;
3296 mlog_errno(ret);
3297 goto out;
3298 }
3299
3300 ret = ocfs2_find_path(inode, right_path, right_cpos);
3301 if (ret) {
3302 mlog_errno(ret);
3303 goto out;
3304 }
3305
3306 *ret_right_path = right_path;
3307out:
3308 if (ret)
3309 ocfs2_free_path(right_path);
3310 return ret;
3311}
3312
3313/*
3314 * Remove split_rec clusters from the record at index and merge them
3315 * onto the beginning of the record "next" to it.
3316 * For index < l_count - 1, the next means the extent rec at index + 1.
3317 * For index == l_count - 1, the "next" means the 1st extent rec of the
3318 * next extent block.
3319 */
3320static int ocfs2_merge_rec_right(struct inode *inode,
3321 struct ocfs2_path *left_path,
3322 handle_t *handle,
3323 struct ocfs2_extent_rec *split_rec,
3324 int index)
3325{
3326 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003327 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3328 struct ocfs2_extent_rec *left_rec;
3329 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003330 struct ocfs2_extent_list *right_el;
3331 struct ocfs2_path *right_path = NULL;
3332 int subtree_index = 0;
3333 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3334 struct buffer_head *bh = path_leaf_bh(left_path);
3335 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003336
3337 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003338 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003339
Al Viro9d8df6a2008-05-21 06:32:11 +01003340 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003341 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3342 /* we meet with a cross extent block merge. */
3343 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3344 if (ret) {
3345 mlog_errno(ret);
3346 goto out;
3347 }
3348
3349 right_el = path_leaf_el(right_path);
3350 next_free = le16_to_cpu(right_el->l_next_free_rec);
3351 BUG_ON(next_free <= 0);
3352 right_rec = &right_el->l_recs[0];
3353 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003354 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003355 right_rec = &right_el->l_recs[1];
3356 }
3357
3358 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3359 le16_to_cpu(left_rec->e_leaf_clusters) !=
3360 le32_to_cpu(right_rec->e_cpos));
3361
3362 subtree_index = ocfs2_find_subtree_root(inode,
3363 left_path, right_path);
3364
3365 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3366 handle->h_buffer_credits,
3367 right_path);
3368 if (ret) {
3369 mlog_errno(ret);
3370 goto out;
3371 }
3372
3373 root_bh = left_path->p_node[subtree_index].bh;
3374 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3375
Joel Becker0cf2f762009-02-12 16:41:25 -08003376 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003377 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003378 if (ret) {
3379 mlog_errno(ret);
3380 goto out;
3381 }
3382
3383 for (i = subtree_index + 1;
3384 i < path_num_items(right_path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -08003385 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07003386 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003387 if (ret) {
3388 mlog_errno(ret);
3389 goto out;
3390 }
3391
Joel Becker0cf2f762009-02-12 16:41:25 -08003392 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07003393 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003394 if (ret) {
3395 mlog_errno(ret);
3396 goto out;
3397 }
3398 }
3399
3400 } else {
3401 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3402 right_rec = &el->l_recs[index + 1];
3403 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003404
Joel Becker0cf2f762009-02-12 16:41:25 -08003405 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), left_path,
Joel Becker13723d02008-10-17 19:25:01 -07003406 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003407 if (ret) {
3408 mlog_errno(ret);
3409 goto out;
3410 }
3411
3412 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3413
3414 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3415 le64_add_cpu(&right_rec->e_blkno,
3416 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3417 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3418
3419 ocfs2_cleanup_merge(el, index);
3420
3421 ret = ocfs2_journal_dirty(handle, bh);
3422 if (ret)
3423 mlog_errno(ret);
3424
Tao Ma677b9752008-01-30 14:21:05 +08003425 if (right_path) {
3426 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3427 if (ret)
3428 mlog_errno(ret);
3429
3430 ocfs2_complete_edge_insert(inode, handle, left_path,
3431 right_path, subtree_index);
3432 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003433out:
Tao Ma677b9752008-01-30 14:21:05 +08003434 if (right_path)
3435 ocfs2_free_path(right_path);
3436 return ret;
3437}
3438
3439static int ocfs2_get_left_path(struct inode *inode,
3440 struct ocfs2_path *right_path,
3441 struct ocfs2_path **ret_left_path)
3442{
3443 int ret;
3444 u32 left_cpos;
3445 struct ocfs2_path *left_path = NULL;
3446
3447 *ret_left_path = NULL;
3448
3449 /* This function shouldn't be called for non-trees. */
3450 BUG_ON(right_path->p_tree_depth == 0);
3451
3452 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3453 right_path, &left_cpos);
3454 if (ret) {
3455 mlog_errno(ret);
3456 goto out;
3457 }
3458
3459 /* This function shouldn't be called for the leftmost leaf. */
3460 BUG_ON(left_cpos == 0);
3461
Joel Beckerffdd7a52008-10-17 22:32:01 -07003462 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003463 if (!left_path) {
3464 ret = -ENOMEM;
3465 mlog_errno(ret);
3466 goto out;
3467 }
3468
3469 ret = ocfs2_find_path(inode, left_path, left_cpos);
3470 if (ret) {
3471 mlog_errno(ret);
3472 goto out;
3473 }
3474
3475 *ret_left_path = left_path;
3476out:
3477 if (ret)
3478 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003479 return ret;
3480}
3481
3482/*
3483 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003484 * onto the tail of the record "before" it.
3485 * For index > 0, the "before" means the extent rec at index - 1.
3486 *
3487 * For index == 0, the "before" means the last record of the previous
3488 * extent block. And there is also a situation that we may need to
3489 * remove the rightmost leaf extent block in the right_path and change
3490 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003491 */
Tao Ma677b9752008-01-30 14:21:05 +08003492static int ocfs2_merge_rec_left(struct inode *inode,
3493 struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003494 handle_t *handle,
3495 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003496 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003497 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003498 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003499{
Tao Ma677b9752008-01-30 14:21:05 +08003500 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003501 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3502 struct ocfs2_extent_rec *left_rec;
3503 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003504 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3505 struct buffer_head *bh = path_leaf_bh(right_path);
3506 struct buffer_head *root_bh = NULL;
3507 struct ocfs2_path *left_path = NULL;
3508 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003509
Tao Ma677b9752008-01-30 14:21:05 +08003510 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003511
Mark Fasheh328d5752007-06-18 10:48:04 -07003512 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003513 if (index == 0) {
3514 /* we meet with a cross extent block merge. */
3515 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3516 if (ret) {
3517 mlog_errno(ret);
3518 goto out;
3519 }
3520
3521 left_el = path_leaf_el(left_path);
3522 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3523 le16_to_cpu(left_el->l_count));
3524
3525 left_rec = &left_el->l_recs[
3526 le16_to_cpu(left_el->l_next_free_rec) - 1];
3527 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3528 le16_to_cpu(left_rec->e_leaf_clusters) !=
3529 le32_to_cpu(split_rec->e_cpos));
3530
3531 subtree_index = ocfs2_find_subtree_root(inode,
3532 left_path, right_path);
3533
3534 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3535 handle->h_buffer_credits,
3536 left_path);
3537 if (ret) {
3538 mlog_errno(ret);
3539 goto out;
3540 }
3541
3542 root_bh = left_path->p_node[subtree_index].bh;
3543 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3544
Joel Becker0cf2f762009-02-12 16:41:25 -08003545 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003546 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003547 if (ret) {
3548 mlog_errno(ret);
3549 goto out;
3550 }
3551
3552 for (i = subtree_index + 1;
3553 i < path_num_items(right_path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -08003554 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07003555 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003556 if (ret) {
3557 mlog_errno(ret);
3558 goto out;
3559 }
3560
Joel Becker0cf2f762009-02-12 16:41:25 -08003561 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07003562 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003563 if (ret) {
3564 mlog_errno(ret);
3565 goto out;
3566 }
3567 }
3568 } else {
3569 left_rec = &el->l_recs[index - 1];
3570 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3571 has_empty_extent = 1;
3572 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003573
Joel Becker0cf2f762009-02-12 16:41:25 -08003574 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
Tao Ma9047bea2009-01-05 14:45:24 +08003575 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003576 if (ret) {
3577 mlog_errno(ret);
3578 goto out;
3579 }
3580
3581 if (has_empty_extent && index == 1) {
3582 /*
3583 * The easy case - we can just plop the record right in.
3584 */
3585 *left_rec = *split_rec;
3586
3587 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003588 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003589 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003590
3591 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3592 le64_add_cpu(&right_rec->e_blkno,
3593 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3594 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3595
3596 ocfs2_cleanup_merge(el, index);
3597
3598 ret = ocfs2_journal_dirty(handle, bh);
3599 if (ret)
3600 mlog_errno(ret);
3601
Tao Ma677b9752008-01-30 14:21:05 +08003602 if (left_path) {
3603 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3604 if (ret)
3605 mlog_errno(ret);
3606
3607 /*
3608 * In the situation that the right_rec is empty and the extent
3609 * block is empty also, ocfs2_complete_edge_insert can't handle
3610 * it and we need to delete the right extent block.
3611 */
3612 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3613 le16_to_cpu(el->l_next_free_rec) == 1) {
3614
3615 ret = ocfs2_remove_rightmost_path(inode, handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003616 right_path,
3617 dealloc, et);
Tao Ma677b9752008-01-30 14:21:05 +08003618 if (ret) {
3619 mlog_errno(ret);
3620 goto out;
3621 }
3622
3623 /* Now the rightmost extent block has been deleted.
3624 * So we use the new rightmost path.
3625 */
3626 ocfs2_mv_path(right_path, left_path);
3627 left_path = NULL;
3628 } else
3629 ocfs2_complete_edge_insert(inode, handle, left_path,
3630 right_path, subtree_index);
3631 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003632out:
Tao Ma677b9752008-01-30 14:21:05 +08003633 if (left_path)
3634 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003635 return ret;
3636}
3637
3638static int ocfs2_try_to_merge_extent(struct inode *inode,
3639 handle_t *handle,
Tao Ma677b9752008-01-30 14:21:05 +08003640 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003641 int split_index,
3642 struct ocfs2_extent_rec *split_rec,
3643 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003644 struct ocfs2_merge_ctxt *ctxt,
3645 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003646
3647{
Tao Mao518d7262007-08-28 17:25:35 -07003648 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003649 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003650 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3651
3652 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3653
Tao Mao518d7262007-08-28 17:25:35 -07003654 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3655 /*
3656 * The merge code will need to create an empty
3657 * extent to take the place of the newly
3658 * emptied slot. Remove any pre-existing empty
3659 * extents - having more than one in a leaf is
3660 * illegal.
3661 */
Tao Ma677b9752008-01-30 14:21:05 +08003662 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003663 dealloc, et);
Tao Mao518d7262007-08-28 17:25:35 -07003664 if (ret) {
3665 mlog_errno(ret);
3666 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003667 }
Tao Mao518d7262007-08-28 17:25:35 -07003668 split_index--;
3669 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003670 }
3671
3672 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3673 /*
3674 * Left-right contig implies this.
3675 */
3676 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003677
3678 /*
3679 * Since the leftright insert always covers the entire
3680 * extent, this call will delete the insert record
3681 * entirely, resulting in an empty extent record added to
3682 * the extent block.
3683 *
3684 * Since the adding of an empty extent shifts
3685 * everything back to the right, there's no need to
3686 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003687 *
3688 * When the split_index is zero, we need to merge it to the
3689 * prevoius extent block. It is more efficient and easier
3690 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003691 */
Tao Ma677b9752008-01-30 14:21:05 +08003692 ret = ocfs2_merge_rec_right(inode, path,
3693 handle, split_rec,
3694 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003695 if (ret) {
3696 mlog_errno(ret);
3697 goto out;
3698 }
3699
3700 /*
3701 * We can only get this from logic error above.
3702 */
3703 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3704
Tao Ma677b9752008-01-30 14:21:05 +08003705 /* The merge left us with an empty extent, remove it. */
Tao Mae7d4cb62008-08-18 17:38:44 +08003706 ret = ocfs2_rotate_tree_left(inode, handle, path,
3707 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003708 if (ret) {
3709 mlog_errno(ret);
3710 goto out;
3711 }
Tao Ma677b9752008-01-30 14:21:05 +08003712
Mark Fasheh328d5752007-06-18 10:48:04 -07003713 rec = &el->l_recs[split_index];
3714
3715 /*
3716 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003717 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003718 */
Tao Ma677b9752008-01-30 14:21:05 +08003719 ret = ocfs2_merge_rec_left(inode, path,
3720 handle, rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003721 dealloc, et,
Tao Ma677b9752008-01-30 14:21:05 +08003722 split_index);
3723
Mark Fasheh328d5752007-06-18 10:48:04 -07003724 if (ret) {
3725 mlog_errno(ret);
3726 goto out;
3727 }
3728
Tao Ma677b9752008-01-30 14:21:05 +08003729 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003730 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003731 /*
3732 * Error from this last rotate is not critical, so
3733 * print but don't bubble it up.
3734 */
3735 if (ret)
3736 mlog_errno(ret);
3737 ret = 0;
3738 } else {
3739 /*
3740 * Merge a record to the left or right.
3741 *
3742 * 'contig_type' is relative to the existing record,
3743 * so for example, if we're "right contig", it's to
3744 * the record on the left (hence the left merge).
3745 */
3746 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3747 ret = ocfs2_merge_rec_left(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003748 path,
3749 handle, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003750 dealloc, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003751 split_index);
3752 if (ret) {
3753 mlog_errno(ret);
3754 goto out;
3755 }
3756 } else {
3757 ret = ocfs2_merge_rec_right(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003758 path,
3759 handle, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003760 split_index);
3761 if (ret) {
3762 mlog_errno(ret);
3763 goto out;
3764 }
3765 }
3766
3767 if (ctxt->c_split_covers_rec) {
3768 /*
3769 * The merge may have left an empty extent in
3770 * our leaf. Try to rotate it away.
3771 */
Tao Ma677b9752008-01-30 14:21:05 +08003772 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003773 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003774 if (ret)
3775 mlog_errno(ret);
3776 ret = 0;
3777 }
3778 }
3779
3780out:
3781 return ret;
3782}
3783
3784static void ocfs2_subtract_from_rec(struct super_block *sb,
3785 enum ocfs2_split_type split,
3786 struct ocfs2_extent_rec *rec,
3787 struct ocfs2_extent_rec *split_rec)
3788{
3789 u64 len_blocks;
3790
3791 len_blocks = ocfs2_clusters_to_blocks(sb,
3792 le16_to_cpu(split_rec->e_leaf_clusters));
3793
3794 if (split == SPLIT_LEFT) {
3795 /*
3796 * Region is on the left edge of the existing
3797 * record.
3798 */
3799 le32_add_cpu(&rec->e_cpos,
3800 le16_to_cpu(split_rec->e_leaf_clusters));
3801 le64_add_cpu(&rec->e_blkno, len_blocks);
3802 le16_add_cpu(&rec->e_leaf_clusters,
3803 -le16_to_cpu(split_rec->e_leaf_clusters));
3804 } else {
3805 /*
3806 * Region is on the right edge of the existing
3807 * record.
3808 */
3809 le16_add_cpu(&rec->e_leaf_clusters,
3810 -le16_to_cpu(split_rec->e_leaf_clusters));
3811 }
3812}
3813
Mark Fashehdcd05382007-01-16 11:32:23 -08003814/*
3815 * Do the final bits of extent record insertion at the target leaf
3816 * list. If this leaf is part of an allocation tree, it is assumed
3817 * that the tree above has been prepared.
3818 */
3819static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3820 struct ocfs2_extent_list *el,
3821 struct ocfs2_insert_type *insert,
3822 struct inode *inode)
3823{
3824 int i = insert->ins_contig_index;
3825 unsigned int range;
3826 struct ocfs2_extent_rec *rec;
3827
Mark Fashehe48edee2007-03-07 16:46:57 -08003828 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003829
Mark Fasheh328d5752007-06-18 10:48:04 -07003830 if (insert->ins_split != SPLIT_NONE) {
3831 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3832 BUG_ON(i == -1);
3833 rec = &el->l_recs[i];
3834 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3835 insert_rec);
3836 goto rotate;
3837 }
3838
Mark Fashehdcd05382007-01-16 11:32:23 -08003839 /*
3840 * Contiguous insert - either left or right.
3841 */
3842 if (insert->ins_contig != CONTIG_NONE) {
3843 rec = &el->l_recs[i];
3844 if (insert->ins_contig == CONTIG_LEFT) {
3845 rec->e_blkno = insert_rec->e_blkno;
3846 rec->e_cpos = insert_rec->e_cpos;
3847 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003848 le16_add_cpu(&rec->e_leaf_clusters,
3849 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003850 return;
3851 }
3852
3853 /*
3854 * Handle insert into an empty leaf.
3855 */
3856 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3857 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3858 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3859 el->l_recs[0] = *insert_rec;
3860 el->l_next_free_rec = cpu_to_le16(1);
3861 return;
3862 }
3863
3864 /*
3865 * Appending insert.
3866 */
3867 if (insert->ins_appending == APPEND_TAIL) {
3868 i = le16_to_cpu(el->l_next_free_rec) - 1;
3869 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003870 range = le32_to_cpu(rec->e_cpos)
3871 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003872 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3873
3874 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3875 le16_to_cpu(el->l_count),
3876 "inode %lu, depth %u, count %u, next free %u, "
3877 "rec.cpos %u, rec.clusters %u, "
3878 "insert.cpos %u, insert.clusters %u\n",
3879 inode->i_ino,
3880 le16_to_cpu(el->l_tree_depth),
3881 le16_to_cpu(el->l_count),
3882 le16_to_cpu(el->l_next_free_rec),
3883 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003884 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003885 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003886 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003887 i++;
3888 el->l_recs[i] = *insert_rec;
3889 le16_add_cpu(&el->l_next_free_rec, 1);
3890 return;
3891 }
3892
Mark Fasheh328d5752007-06-18 10:48:04 -07003893rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003894 /*
3895 * Ok, we have to rotate.
3896 *
3897 * At this point, it is safe to assume that inserting into an
3898 * empty leaf and appending to a leaf have both been handled
3899 * above.
3900 *
3901 * This leaf needs to have space, either by the empty 1st
3902 * extent record, or by virtue of an l_next_rec < l_count.
3903 */
3904 ocfs2_rotate_leaf(el, insert_rec);
3905}
3906
Mark Fasheh328d5752007-06-18 10:48:04 -07003907static void ocfs2_adjust_rightmost_records(struct inode *inode,
3908 handle_t *handle,
3909 struct ocfs2_path *path,
3910 struct ocfs2_extent_rec *insert_rec)
3911{
3912 int ret, i, next_free;
3913 struct buffer_head *bh;
3914 struct ocfs2_extent_list *el;
3915 struct ocfs2_extent_rec *rec;
3916
3917 /*
3918 * Update everything except the leaf block.
3919 */
3920 for (i = 0; i < path->p_tree_depth; i++) {
3921 bh = path->p_node[i].bh;
3922 el = path->p_node[i].el;
3923
3924 next_free = le16_to_cpu(el->l_next_free_rec);
3925 if (next_free == 0) {
3926 ocfs2_error(inode->i_sb,
3927 "Dinode %llu has a bad extent list",
3928 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3929 ret = -EIO;
3930 return;
3931 }
3932
3933 rec = &el->l_recs[next_free - 1];
3934
3935 rec->e_int_clusters = insert_rec->e_cpos;
3936 le32_add_cpu(&rec->e_int_clusters,
3937 le16_to_cpu(insert_rec->e_leaf_clusters));
3938 le32_add_cpu(&rec->e_int_clusters,
3939 -le32_to_cpu(rec->e_cpos));
3940
3941 ret = ocfs2_journal_dirty(handle, bh);
3942 if (ret)
3943 mlog_errno(ret);
3944
3945 }
3946}
3947
Mark Fashehdcd05382007-01-16 11:32:23 -08003948static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3949 struct ocfs2_extent_rec *insert_rec,
3950 struct ocfs2_path *right_path,
3951 struct ocfs2_path **ret_left_path)
3952{
Mark Fasheh328d5752007-06-18 10:48:04 -07003953 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003954 struct ocfs2_extent_list *el;
3955 struct ocfs2_path *left_path = NULL;
3956
3957 *ret_left_path = NULL;
3958
3959 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003960 * This shouldn't happen for non-trees. The extent rec cluster
3961 * count manipulation below only works for interior nodes.
3962 */
3963 BUG_ON(right_path->p_tree_depth == 0);
3964
3965 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003966 * If our appending insert is at the leftmost edge of a leaf,
3967 * then we might need to update the rightmost records of the
3968 * neighboring path.
3969 */
3970 el = path_leaf_el(right_path);
3971 next_free = le16_to_cpu(el->l_next_free_rec);
3972 if (next_free == 0 ||
3973 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3974 u32 left_cpos;
3975
3976 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3977 &left_cpos);
3978 if (ret) {
3979 mlog_errno(ret);
3980 goto out;
3981 }
3982
3983 mlog(0, "Append may need a left path update. cpos: %u, "
3984 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3985 left_cpos);
3986
3987 /*
3988 * No need to worry if the append is already in the
3989 * leftmost leaf.
3990 */
3991 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07003992 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003993 if (!left_path) {
3994 ret = -ENOMEM;
3995 mlog_errno(ret);
3996 goto out;
3997 }
3998
3999 ret = ocfs2_find_path(inode, left_path, left_cpos);
4000 if (ret) {
4001 mlog_errno(ret);
4002 goto out;
4003 }
4004
4005 /*
4006 * ocfs2_insert_path() will pass the left_path to the
4007 * journal for us.
4008 */
4009 }
4010 }
4011
Joel Becker0cf2f762009-02-12 16:41:25 -08004012 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004013 if (ret) {
4014 mlog_errno(ret);
4015 goto out;
4016 }
4017
Mark Fasheh328d5752007-06-18 10:48:04 -07004018 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004019
4020 *ret_left_path = left_path;
4021 ret = 0;
4022out:
4023 if (ret != 0)
4024 ocfs2_free_path(left_path);
4025
4026 return ret;
4027}
4028
Mark Fasheh328d5752007-06-18 10:48:04 -07004029static void ocfs2_split_record(struct inode *inode,
4030 struct ocfs2_path *left_path,
4031 struct ocfs2_path *right_path,
4032 struct ocfs2_extent_rec *split_rec,
4033 enum ocfs2_split_type split)
4034{
4035 int index;
4036 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4037 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4038 struct ocfs2_extent_rec *rec, *tmprec;
4039
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004040 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004041 if (left_path)
4042 left_el = path_leaf_el(left_path);
4043
4044 el = right_el;
4045 insert_el = right_el;
4046 index = ocfs2_search_extent_list(el, cpos);
4047 if (index != -1) {
4048 if (index == 0 && left_path) {
4049 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4050
4051 /*
4052 * This typically means that the record
4053 * started in the left path but moved to the
4054 * right as a result of rotation. We either
4055 * move the existing record to the left, or we
4056 * do the later insert there.
4057 *
4058 * In this case, the left path should always
4059 * exist as the rotate code will have passed
4060 * it back for a post-insert update.
4061 */
4062
4063 if (split == SPLIT_LEFT) {
4064 /*
4065 * It's a left split. Since we know
4066 * that the rotate code gave us an
4067 * empty extent in the left path, we
4068 * can just do the insert there.
4069 */
4070 insert_el = left_el;
4071 } else {
4072 /*
4073 * Right split - we have to move the
4074 * existing record over to the left
4075 * leaf. The insert will be into the
4076 * newly created empty extent in the
4077 * right leaf.
4078 */
4079 tmprec = &right_el->l_recs[index];
4080 ocfs2_rotate_leaf(left_el, tmprec);
4081 el = left_el;
4082
4083 memset(tmprec, 0, sizeof(*tmprec));
4084 index = ocfs2_search_extent_list(left_el, cpos);
4085 BUG_ON(index == -1);
4086 }
4087 }
4088 } else {
4089 BUG_ON(!left_path);
4090 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4091 /*
4092 * Left path is easy - we can just allow the insert to
4093 * happen.
4094 */
4095 el = left_el;
4096 insert_el = left_el;
4097 index = ocfs2_search_extent_list(el, cpos);
4098 BUG_ON(index == -1);
4099 }
4100
4101 rec = &el->l_recs[index];
4102 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
4103 ocfs2_rotate_leaf(insert_el, split_rec);
4104}
4105
Mark Fashehdcd05382007-01-16 11:32:23 -08004106/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004107 * This function only does inserts on an allocation b-tree. For tree
4108 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004109 *
4110 * right_path is the path we want to do the actual insert
4111 * in. left_path should only be passed in if we need to update that
4112 * portion of the tree after an edge insert.
4113 */
4114static int ocfs2_insert_path(struct inode *inode,
4115 handle_t *handle,
4116 struct ocfs2_path *left_path,
4117 struct ocfs2_path *right_path,
4118 struct ocfs2_extent_rec *insert_rec,
4119 struct ocfs2_insert_type *insert)
4120{
4121 int ret, subtree_index;
4122 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004123
Mark Fashehdcd05382007-01-16 11:32:23 -08004124 if (left_path) {
4125 int credits = handle->h_buffer_credits;
4126
4127 /*
4128 * There's a chance that left_path got passed back to
4129 * us without being accounted for in the
4130 * journal. Extend our transaction here to be sure we
4131 * can change those blocks.
4132 */
4133 credits += left_path->p_tree_depth;
4134
4135 ret = ocfs2_extend_trans(handle, credits);
4136 if (ret < 0) {
4137 mlog_errno(ret);
4138 goto out;
4139 }
4140
Joel Becker0cf2f762009-02-12 16:41:25 -08004141 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, left_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004142 if (ret < 0) {
4143 mlog_errno(ret);
4144 goto out;
4145 }
4146 }
4147
Mark Fashehe8aed342007-12-03 16:43:01 -08004148 /*
4149 * Pass both paths to the journal. The majority of inserts
4150 * will be touching all components anyway.
4151 */
Joel Becker0cf2f762009-02-12 16:41:25 -08004152 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, right_path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004153 if (ret < 0) {
4154 mlog_errno(ret);
4155 goto out;
4156 }
4157
Mark Fasheh328d5752007-06-18 10:48:04 -07004158 if (insert->ins_split != SPLIT_NONE) {
4159 /*
4160 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004161 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004162 * function sort it all out.
4163 */
4164 ocfs2_split_record(inode, left_path, right_path,
4165 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004166
4167 /*
4168 * Split might have modified either leaf and we don't
4169 * have a guarantee that the later edge insert will
4170 * dirty this for us.
4171 */
4172 if (left_path)
4173 ret = ocfs2_journal_dirty(handle,
4174 path_leaf_bh(left_path));
4175 if (ret)
4176 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07004177 } else
4178 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
4179 insert, inode);
Mark Fashehdcd05382007-01-16 11:32:23 -08004180
Mark Fashehdcd05382007-01-16 11:32:23 -08004181 ret = ocfs2_journal_dirty(handle, leaf_bh);
4182 if (ret)
4183 mlog_errno(ret);
4184
4185 if (left_path) {
4186 /*
4187 * The rotate code has indicated that we need to fix
4188 * up portions of the tree after the insert.
4189 *
4190 * XXX: Should we extend the transaction here?
4191 */
4192 subtree_index = ocfs2_find_subtree_root(inode, left_path,
4193 right_path);
4194 ocfs2_complete_edge_insert(inode, handle, left_path,
4195 right_path, subtree_index);
4196 }
4197
4198 ret = 0;
4199out:
4200 return ret;
4201}
4202
4203static int ocfs2_do_insert_extent(struct inode *inode,
4204 handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004205 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004206 struct ocfs2_extent_rec *insert_rec,
4207 struct ocfs2_insert_type *type)
4208{
4209 int ret, rotate = 0;
4210 u32 cpos;
4211 struct ocfs2_path *right_path = NULL;
4212 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004213 struct ocfs2_extent_list *el;
4214
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004215 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004216
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004217 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004218 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004219 if (ret) {
4220 mlog_errno(ret);
4221 goto out;
4222 }
4223
4224 if (le16_to_cpu(el->l_tree_depth) == 0) {
4225 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4226 goto out_update_clusters;
4227 }
4228
Joel Beckerffdd7a52008-10-17 22:32:01 -07004229 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004230 if (!right_path) {
4231 ret = -ENOMEM;
4232 mlog_errno(ret);
4233 goto out;
4234 }
4235
4236 /*
4237 * Determine the path to start with. Rotations need the
4238 * rightmost path, everything else can go directly to the
4239 * target leaf.
4240 */
4241 cpos = le32_to_cpu(insert_rec->e_cpos);
4242 if (type->ins_appending == APPEND_NONE &&
4243 type->ins_contig == CONTIG_NONE) {
4244 rotate = 1;
4245 cpos = UINT_MAX;
4246 }
4247
4248 ret = ocfs2_find_path(inode, right_path, cpos);
4249 if (ret) {
4250 mlog_errno(ret);
4251 goto out;
4252 }
4253
4254 /*
4255 * Rotations and appends need special treatment - they modify
4256 * parts of the tree's above them.
4257 *
4258 * Both might pass back a path immediate to the left of the
4259 * one being inserted to. This will be cause
4260 * ocfs2_insert_path() to modify the rightmost records of
4261 * left_path to account for an edge insert.
4262 *
4263 * XXX: When modifying this code, keep in mind that an insert
4264 * can wind up skipping both of these two special cases...
4265 */
4266 if (rotate) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004267 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004268 le32_to_cpu(insert_rec->e_cpos),
4269 right_path, &left_path);
4270 if (ret) {
4271 mlog_errno(ret);
4272 goto out;
4273 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004274
4275 /*
4276 * ocfs2_rotate_tree_right() might have extended the
4277 * transaction without re-journaling our tree root.
4278 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004279 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004280 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004281 if (ret) {
4282 mlog_errno(ret);
4283 goto out;
4284 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004285 } else if (type->ins_appending == APPEND_TAIL
4286 && type->ins_contig != CONTIG_LEFT) {
4287 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4288 right_path, &left_path);
4289 if (ret) {
4290 mlog_errno(ret);
4291 goto out;
4292 }
4293 }
4294
4295 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4296 insert_rec, type);
4297 if (ret) {
4298 mlog_errno(ret);
4299 goto out;
4300 }
4301
4302out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004303 if (type->ins_split == SPLIT_NONE)
Joel Becker35dc0aa2008-08-20 16:25:06 -07004304 ocfs2_et_update_clusters(inode, et,
4305 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004306
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004307 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004308 if (ret)
4309 mlog_errno(ret);
4310
4311out:
4312 ocfs2_free_path(left_path);
4313 ocfs2_free_path(right_path);
4314
4315 return ret;
4316}
4317
Mark Fasheh328d5752007-06-18 10:48:04 -07004318static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08004319ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004320 struct ocfs2_extent_list *el, int index,
4321 struct ocfs2_extent_rec *split_rec)
4322{
Tao Maad5a4d72008-01-30 14:21:32 +08004323 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004324 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004325 u32 left_cpos, right_cpos;
4326 struct ocfs2_extent_rec *rec = NULL;
4327 struct ocfs2_extent_list *new_el;
4328 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4329 struct buffer_head *bh;
4330 struct ocfs2_extent_block *eb;
4331
4332 if (index > 0) {
4333 rec = &el->l_recs[index - 1];
4334 } else if (path->p_tree_depth > 0) {
4335 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4336 path, &left_cpos);
4337 if (status)
4338 goto out;
4339
4340 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004341 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004342 if (!left_path)
4343 goto out;
4344
4345 status = ocfs2_find_path(inode, left_path, left_cpos);
4346 if (status)
4347 goto out;
4348
4349 new_el = path_leaf_el(left_path);
4350
4351 if (le16_to_cpu(new_el->l_next_free_rec) !=
4352 le16_to_cpu(new_el->l_count)) {
4353 bh = path_leaf_bh(left_path);
4354 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004355 ocfs2_error(inode->i_sb,
4356 "Extent block #%llu has an "
4357 "invalid l_next_free_rec of "
4358 "%d. It should have "
4359 "matched the l_count of %d",
4360 (unsigned long long)le64_to_cpu(eb->h_blkno),
4361 le16_to_cpu(new_el->l_next_free_rec),
4362 le16_to_cpu(new_el->l_count));
4363 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004364 goto out;
4365 }
4366 rec = &new_el->l_recs[
4367 le16_to_cpu(new_el->l_next_free_rec) - 1];
4368 }
4369 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004370
4371 /*
4372 * We're careful to check for an empty extent record here -
4373 * the merge code will know what to do if it sees one.
4374 */
Tao Maad5a4d72008-01-30 14:21:32 +08004375 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004376 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4377 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4378 ret = CONTIG_RIGHT;
4379 } else {
4380 ret = ocfs2_extent_contig(inode, rec, split_rec);
4381 }
4382 }
4383
Tao Maad5a4d72008-01-30 14:21:32 +08004384 rec = NULL;
4385 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4386 rec = &el->l_recs[index + 1];
4387 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4388 path->p_tree_depth > 0) {
4389 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4390 path, &right_cpos);
4391 if (status)
4392 goto out;
4393
4394 if (right_cpos == 0)
4395 goto out;
4396
Joel Beckerffdd7a52008-10-17 22:32:01 -07004397 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004398 if (!right_path)
4399 goto out;
4400
4401 status = ocfs2_find_path(inode, right_path, right_cpos);
4402 if (status)
4403 goto out;
4404
4405 new_el = path_leaf_el(right_path);
4406 rec = &new_el->l_recs[0];
4407 if (ocfs2_is_empty_extent(rec)) {
4408 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4409 bh = path_leaf_bh(right_path);
4410 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004411 ocfs2_error(inode->i_sb,
4412 "Extent block #%llu has an "
4413 "invalid l_next_free_rec of %d",
4414 (unsigned long long)le64_to_cpu(eb->h_blkno),
4415 le16_to_cpu(new_el->l_next_free_rec));
4416 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004417 goto out;
4418 }
4419 rec = &new_el->l_recs[1];
4420 }
4421 }
4422
4423 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004424 enum ocfs2_contig_type contig_type;
4425
Mark Fasheh328d5752007-06-18 10:48:04 -07004426 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4427
4428 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4429 ret = CONTIG_LEFTRIGHT;
4430 else if (ret == CONTIG_NONE)
4431 ret = contig_type;
4432 }
4433
Tao Maad5a4d72008-01-30 14:21:32 +08004434out:
4435 if (left_path)
4436 ocfs2_free_path(left_path);
4437 if (right_path)
4438 ocfs2_free_path(right_path);
4439
Mark Fasheh328d5752007-06-18 10:48:04 -07004440 return ret;
4441}
4442
Mark Fashehdcd05382007-01-16 11:32:23 -08004443static void ocfs2_figure_contig_type(struct inode *inode,
4444 struct ocfs2_insert_type *insert,
4445 struct ocfs2_extent_list *el,
Tao Maca12b7c2008-08-18 17:38:52 +08004446 struct ocfs2_extent_rec *insert_rec,
4447 struct ocfs2_extent_tree *et)
Mark Fashehdcd05382007-01-16 11:32:23 -08004448{
4449 int i;
4450 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4451
Mark Fashehe48edee2007-03-07 16:46:57 -08004452 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4453
Mark Fashehdcd05382007-01-16 11:32:23 -08004454 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4455 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4456 insert_rec);
4457 if (contig_type != CONTIG_NONE) {
4458 insert->ins_contig_index = i;
4459 break;
4460 }
4461 }
4462 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004463
4464 if (insert->ins_contig != CONTIG_NONE) {
4465 struct ocfs2_extent_rec *rec =
4466 &el->l_recs[insert->ins_contig_index];
4467 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4468 le16_to_cpu(insert_rec->e_leaf_clusters);
4469
4470 /*
4471 * Caller might want us to limit the size of extents, don't
4472 * calculate contiguousness if we might exceed that limit.
4473 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004474 if (et->et_max_leaf_clusters &&
4475 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004476 insert->ins_contig = CONTIG_NONE;
4477 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004478}
4479
4480/*
4481 * This should only be called against the righmost leaf extent list.
4482 *
4483 * ocfs2_figure_appending_type() will figure out whether we'll have to
4484 * insert at the tail of the rightmost leaf.
4485 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004486 * This should also work against the root extent list for tree's with 0
4487 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004488 * then the logic here makes sense.
4489 */
4490static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4491 struct ocfs2_extent_list *el,
4492 struct ocfs2_extent_rec *insert_rec)
4493{
4494 int i;
4495 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4496 struct ocfs2_extent_rec *rec;
4497
4498 insert->ins_appending = APPEND_NONE;
4499
Mark Fashehe48edee2007-03-07 16:46:57 -08004500 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004501
4502 if (!el->l_next_free_rec)
4503 goto set_tail_append;
4504
4505 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4506 /* Were all records empty? */
4507 if (le16_to_cpu(el->l_next_free_rec) == 1)
4508 goto set_tail_append;
4509 }
4510
4511 i = le16_to_cpu(el->l_next_free_rec) - 1;
4512 rec = &el->l_recs[i];
4513
Mark Fashehe48edee2007-03-07 16:46:57 -08004514 if (cpos >=
4515 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004516 goto set_tail_append;
4517
4518 return;
4519
4520set_tail_append:
4521 insert->ins_appending = APPEND_TAIL;
4522}
4523
4524/*
4525 * Helper function called at the begining of an insert.
4526 *
4527 * This computes a few things that are commonly used in the process of
4528 * inserting into the btree:
4529 * - Whether the new extent is contiguous with an existing one.
4530 * - The current tree depth.
4531 * - Whether the insert is an appending one.
4532 * - The total # of free records in the tree.
4533 *
4534 * All of the information is stored on the ocfs2_insert_type
4535 * structure.
4536 */
4537static int ocfs2_figure_insert_type(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004538 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004539 struct buffer_head **last_eb_bh,
4540 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004541 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004542 struct ocfs2_insert_type *insert)
4543{
4544 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004545 struct ocfs2_extent_block *eb;
4546 struct ocfs2_extent_list *el;
4547 struct ocfs2_path *path = NULL;
4548 struct buffer_head *bh = NULL;
4549
Mark Fasheh328d5752007-06-18 10:48:04 -07004550 insert->ins_split = SPLIT_NONE;
4551
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004552 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004553 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4554
4555 if (el->l_tree_depth) {
4556 /*
4557 * If we have tree depth, we read in the
4558 * rightmost extent block ahead of time as
4559 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4560 * may want it later.
4561 */
Joel Becker3d03a302009-02-12 17:49:26 -08004562 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08004563 ocfs2_et_get_last_eb_blk(et),
4564 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004565 if (ret) {
4566 mlog_exit(ret);
4567 goto out;
4568 }
4569 eb = (struct ocfs2_extent_block *) bh->b_data;
4570 el = &eb->h_list;
4571 }
4572
4573 /*
4574 * Unless we have a contiguous insert, we'll need to know if
4575 * there is room left in our allocation tree for another
4576 * extent record.
4577 *
4578 * XXX: This test is simplistic, we can search for empty
4579 * extent records too.
4580 */
Tao Maoc77534f2007-08-28 17:22:33 -07004581 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004582 le16_to_cpu(el->l_next_free_rec);
4583
4584 if (!insert->ins_tree_depth) {
Tao Maca12b7c2008-08-18 17:38:52 +08004585 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004586 ocfs2_figure_appending_type(insert, el, insert_rec);
4587 return 0;
4588 }
4589
Joel Beckerffdd7a52008-10-17 22:32:01 -07004590 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004591 if (!path) {
4592 ret = -ENOMEM;
4593 mlog_errno(ret);
4594 goto out;
4595 }
4596
4597 /*
4598 * In the case that we're inserting past what the tree
4599 * currently accounts for, ocfs2_find_path() will return for
4600 * us the rightmost tree path. This is accounted for below in
4601 * the appending code.
4602 */
4603 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4604 if (ret) {
4605 mlog_errno(ret);
4606 goto out;
4607 }
4608
4609 el = path_leaf_el(path);
4610
4611 /*
4612 * Now that we have the path, there's two things we want to determine:
4613 * 1) Contiguousness (also set contig_index if this is so)
4614 *
4615 * 2) Are we doing an append? We can trivially break this up
4616 * into two types of appends: simple record append, or a
4617 * rotate inside the tail leaf.
4618 */
Tao Maca12b7c2008-08-18 17:38:52 +08004619 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004620
4621 /*
4622 * The insert code isn't quite ready to deal with all cases of
4623 * left contiguousness. Specifically, if it's an insert into
4624 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004625 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004626 * left. For now, just catch that case and fool the layers
4627 * above us. This works just fine for tree_depth == 0, which
4628 * is why we allow that above.
4629 */
4630 if (insert->ins_contig == CONTIG_LEFT &&
4631 insert->ins_contig_index == 0)
4632 insert->ins_contig = CONTIG_NONE;
4633
4634 /*
4635 * Ok, so we can simply compare against last_eb to figure out
4636 * whether the path doesn't exist. This will only happen in
4637 * the case that we're doing a tail append, so maybe we can
4638 * take advantage of that information somehow.
4639 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004640 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004641 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004642 /*
4643 * Ok, ocfs2_find_path() returned us the rightmost
4644 * tree path. This might be an appending insert. There are
4645 * two cases:
4646 * 1) We're doing a true append at the tail:
4647 * -This might even be off the end of the leaf
4648 * 2) We're "appending" by rotating in the tail
4649 */
4650 ocfs2_figure_appending_type(insert, el, insert_rec);
4651 }
4652
4653out:
4654 ocfs2_free_path(path);
4655
4656 if (ret == 0)
4657 *last_eb_bh = bh;
4658 else
4659 brelse(bh);
4660 return ret;
4661}
4662
4663/*
4664 * Insert an extent into an inode btree.
4665 *
4666 * The caller needs to update fe->i_clusters
4667 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004668int ocfs2_insert_extent(struct ocfs2_super *osb,
4669 handle_t *handle,
4670 struct inode *inode,
4671 struct ocfs2_extent_tree *et,
4672 u32 cpos,
4673 u64 start_blk,
4674 u32 new_clusters,
4675 u8 flags,
4676 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004677{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004678 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004679 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004680 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004681 struct ocfs2_insert_type insert = {0, };
4682 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004683
Mark Fashehdcd05382007-01-16 11:32:23 -08004684 mlog(0, "add %u clusters at position %u to inode %llu\n",
4685 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004686
Mark Fashehe48edee2007-03-07 16:46:57 -08004687 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004688 rec.e_cpos = cpu_to_le32(cpos);
4689 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004690 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004691 rec.e_flags = flags;
Joel Becker1e61ee72008-08-20 18:32:45 -07004692 status = ocfs2_et_insert_check(inode, et, &rec);
4693 if (status) {
4694 mlog_errno(status);
4695 goto bail;
4696 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004697
Tao Mae7d4cb62008-08-18 17:38:44 +08004698 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004699 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004700 if (status < 0) {
4701 mlog_errno(status);
4702 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004703 }
4704
Mark Fashehdcd05382007-01-16 11:32:23 -08004705 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4706 "Insert.contig_index: %d, Insert.free_records: %d, "
4707 "Insert.tree_depth: %d\n",
4708 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004709 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004710
Tao Maoc77534f2007-08-28 17:22:33 -07004711 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004712 status = ocfs2_grow_tree(inode, handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004713 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004714 meta_ac);
4715 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004716 mlog_errno(status);
4717 goto bail;
4718 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004719 }
4720
Mark Fashehdcd05382007-01-16 11:32:23 -08004721 /* Finally, we can add clusters. This might rotate the tree for us. */
Tao Mae7d4cb62008-08-18 17:38:44 +08004722 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004723 if (status < 0)
4724 mlog_errno(status);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004725 else if (et->et_ops == &ocfs2_dinode_et_ops)
Mark Fasheh83418972007-04-23 18:53:12 -07004726 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004727
4728bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004729 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004730
Tao Maf56654c2008-08-18 17:38:48 +08004731 mlog_exit(status);
4732 return status;
4733}
4734
Tao Ma0eb8d472008-08-18 17:38:45 +08004735/*
4736 * Allcate and add clusters into the extent b-tree.
4737 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004738 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004739 * it is not limited to the file storage. Any extent tree can use this
4740 * function if it implements the proper ocfs2_extent_tree.
4741 */
4742int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4743 struct inode *inode,
4744 u32 *logical_offset,
4745 u32 clusters_to_add,
4746 int mark_unwritten,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004747 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004748 handle_t *handle,
4749 struct ocfs2_alloc_context *data_ac,
4750 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004751 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004752{
4753 int status = 0;
4754 int free_extents;
4755 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4756 u32 bit_off, num_bits;
4757 u64 block;
4758 u8 flags = 0;
4759
4760 BUG_ON(!clusters_to_add);
4761
4762 if (mark_unwritten)
4763 flags = OCFS2_EXT_UNWRITTEN;
4764
Joel Becker3d03a302009-02-12 17:49:26 -08004765 free_extents = ocfs2_num_free_extents(osb, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004766 if (free_extents < 0) {
4767 status = free_extents;
4768 mlog_errno(status);
4769 goto leave;
4770 }
4771
4772 /* there are two cases which could cause us to EAGAIN in the
4773 * we-need-more-metadata case:
4774 * 1) we haven't reserved *any*
4775 * 2) we are so fragmented, we've needed to add metadata too
4776 * many times. */
4777 if (!free_extents && !meta_ac) {
4778 mlog(0, "we haven't reserved any metadata!\n");
4779 status = -EAGAIN;
4780 reason = RESTART_META;
4781 goto leave;
4782 } else if ((!free_extents)
4783 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004784 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004785 mlog(0, "filesystem is really fragmented...\n");
4786 status = -EAGAIN;
4787 reason = RESTART_META;
4788 goto leave;
4789 }
4790
4791 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4792 clusters_to_add, &bit_off, &num_bits);
4793 if (status < 0) {
4794 if (status != -ENOSPC)
4795 mlog_errno(status);
4796 goto leave;
4797 }
4798
4799 BUG_ON(num_bits > clusters_to_add);
4800
Joel Becker13723d02008-10-17 19:25:01 -07004801 /* reserve our write early -- insert_extent may update the tree root */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004802 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004803 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d472008-08-18 17:38:45 +08004804 if (status < 0) {
4805 mlog_errno(status);
4806 goto leave;
4807 }
4808
4809 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4810 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4811 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004812 status = ocfs2_insert_extent(osb, handle, inode, et,
4813 *logical_offset, block,
4814 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004815 if (status < 0) {
4816 mlog_errno(status);
4817 goto leave;
4818 }
4819
Joel Beckerf99b9b72008-08-20 19:36:33 -07004820 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004821 if (status < 0) {
4822 mlog_errno(status);
4823 goto leave;
4824 }
4825
4826 clusters_to_add -= num_bits;
4827 *logical_offset += num_bits;
4828
4829 if (clusters_to_add) {
4830 mlog(0, "need to alloc once more, wanted = %u\n",
4831 clusters_to_add);
4832 status = -EAGAIN;
4833 reason = RESTART_TRANS;
4834 }
4835
4836leave:
4837 mlog_exit(status);
4838 if (reason_ret)
4839 *reason_ret = reason;
4840 return status;
4841}
4842
Mark Fasheh328d5752007-06-18 10:48:04 -07004843static void ocfs2_make_right_split_rec(struct super_block *sb,
4844 struct ocfs2_extent_rec *split_rec,
4845 u32 cpos,
4846 struct ocfs2_extent_rec *rec)
4847{
4848 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4849 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4850
4851 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4852
4853 split_rec->e_cpos = cpu_to_le32(cpos);
4854 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4855
4856 split_rec->e_blkno = rec->e_blkno;
4857 le64_add_cpu(&split_rec->e_blkno,
4858 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4859
4860 split_rec->e_flags = rec->e_flags;
4861}
4862
4863static int ocfs2_split_and_insert(struct inode *inode,
4864 handle_t *handle,
4865 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004866 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004867 struct buffer_head **last_eb_bh,
4868 int split_index,
4869 struct ocfs2_extent_rec *orig_split_rec,
4870 struct ocfs2_alloc_context *meta_ac)
4871{
4872 int ret = 0, depth;
4873 unsigned int insert_range, rec_range, do_leftright = 0;
4874 struct ocfs2_extent_rec tmprec;
4875 struct ocfs2_extent_list *rightmost_el;
4876 struct ocfs2_extent_rec rec;
4877 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4878 struct ocfs2_insert_type insert;
4879 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004880
4881leftright:
4882 /*
4883 * Store a copy of the record on the stack - it might move
4884 * around as the tree is manipulated below.
4885 */
4886 rec = path_leaf_el(path)->l_recs[split_index];
4887
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004888 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004889
4890 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4891 if (depth) {
4892 BUG_ON(!(*last_eb_bh));
4893 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4894 rightmost_el = &eb->h_list;
4895 }
4896
4897 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4898 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004899 ret = ocfs2_grow_tree(inode, handle, et,
4900 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004901 if (ret) {
4902 mlog_errno(ret);
4903 goto out;
4904 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004905 }
4906
4907 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4908 insert.ins_appending = APPEND_NONE;
4909 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004910 insert.ins_tree_depth = depth;
4911
4912 insert_range = le32_to_cpu(split_rec.e_cpos) +
4913 le16_to_cpu(split_rec.e_leaf_clusters);
4914 rec_range = le32_to_cpu(rec.e_cpos) +
4915 le16_to_cpu(rec.e_leaf_clusters);
4916
4917 if (split_rec.e_cpos == rec.e_cpos) {
4918 insert.ins_split = SPLIT_LEFT;
4919 } else if (insert_range == rec_range) {
4920 insert.ins_split = SPLIT_RIGHT;
4921 } else {
4922 /*
4923 * Left/right split. We fake this as a right split
4924 * first and then make a second pass as a left split.
4925 */
4926 insert.ins_split = SPLIT_RIGHT;
4927
4928 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4929 &rec);
4930
4931 split_rec = tmprec;
4932
4933 BUG_ON(do_leftright);
4934 do_leftright = 1;
4935 }
4936
Tao Mae7d4cb62008-08-18 17:38:44 +08004937 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004938 if (ret) {
4939 mlog_errno(ret);
4940 goto out;
4941 }
4942
4943 if (do_leftright == 1) {
4944 u32 cpos;
4945 struct ocfs2_extent_list *el;
4946
4947 do_leftright++;
4948 split_rec = *orig_split_rec;
4949
4950 ocfs2_reinit_path(path, 1);
4951
4952 cpos = le32_to_cpu(split_rec.e_cpos);
4953 ret = ocfs2_find_path(inode, path, cpos);
4954 if (ret) {
4955 mlog_errno(ret);
4956 goto out;
4957 }
4958
4959 el = path_leaf_el(path);
4960 split_index = ocfs2_search_extent_list(el, cpos);
4961 goto leftright;
4962 }
4963out:
4964
4965 return ret;
4966}
4967
Tao Ma47be12e2009-01-09 07:32:48 +08004968static int ocfs2_replace_extent_rec(struct inode *inode,
4969 handle_t *handle,
4970 struct ocfs2_path *path,
4971 struct ocfs2_extent_list *el,
4972 int split_index,
4973 struct ocfs2_extent_rec *split_rec)
4974{
4975 int ret;
4976
Joel Becker0cf2f762009-02-12 16:41:25 -08004977 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path,
Tao Ma47be12e2009-01-09 07:32:48 +08004978 path_num_items(path) - 1);
4979 if (ret) {
4980 mlog_errno(ret);
4981 goto out;
4982 }
4983
4984 el->l_recs[split_index] = *split_rec;
4985
4986 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4987out:
4988 return ret;
4989}
4990
Mark Fasheh328d5752007-06-18 10:48:04 -07004991/*
4992 * Mark part or all of the extent record at split_index in the leaf
4993 * pointed to by path as written. This removes the unwritten
4994 * extent flag.
4995 *
4996 * Care is taken to handle contiguousness so as to not grow the tree.
4997 *
4998 * meta_ac is not strictly necessary - we only truly need it if growth
4999 * of the tree is required. All other cases will degrade into a less
5000 * optimal tree layout.
5001 *
Tao Mae7d4cb62008-08-18 17:38:44 +08005002 * last_eb_bh should be the rightmost leaf block for any extent
5003 * btree. Since a split may grow the tree or a merge might shrink it,
5004 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07005005 *
5006 * This code is optimized for readability - several passes might be
5007 * made over certain portions of the tree. All of those blocks will
5008 * have been brought into cache (and pinned via the journal), so the
5009 * extra overhead is not expressed in terms of disk reads.
5010 */
5011static int __ocfs2_mark_extent_written(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08005012 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005013 handle_t *handle,
5014 struct ocfs2_path *path,
5015 int split_index,
5016 struct ocfs2_extent_rec *split_rec,
5017 struct ocfs2_alloc_context *meta_ac,
5018 struct ocfs2_cached_dealloc_ctxt *dealloc)
5019{
5020 int ret = 0;
5021 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08005022 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07005023 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5024 struct ocfs2_merge_ctxt ctxt;
5025 struct ocfs2_extent_list *rightmost_el;
5026
Roel Kluin3cf0c502007-10-27 00:20:36 +02005027 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07005028 ret = -EIO;
5029 mlog_errno(ret);
5030 goto out;
5031 }
5032
5033 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5034 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5035 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5036 ret = -EIO;
5037 mlog_errno(ret);
5038 goto out;
5039 }
5040
Tao Maad5a4d72008-01-30 14:21:32 +08005041 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005042 split_index,
5043 split_rec);
5044
5045 /*
5046 * The core merge / split code wants to know how much room is
5047 * left in this inodes allocation tree, so we pass the
5048 * rightmost extent list.
5049 */
5050 if (path->p_tree_depth) {
5051 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005052
Joel Becker3d03a302009-02-12 17:49:26 -08005053 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005054 ocfs2_et_get_last_eb_blk(et),
5055 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005056 if (ret) {
5057 mlog_exit(ret);
5058 goto out;
5059 }
5060
5061 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005062 rightmost_el = &eb->h_list;
5063 } else
5064 rightmost_el = path_root_el(path);
5065
Mark Fasheh328d5752007-06-18 10:48:04 -07005066 if (rec->e_cpos == split_rec->e_cpos &&
5067 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5068 ctxt.c_split_covers_rec = 1;
5069 else
5070 ctxt.c_split_covers_rec = 0;
5071
5072 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5073
Mark Fasheh015452b2007-09-12 10:21:22 -07005074 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5075 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5076 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005077
5078 if (ctxt.c_contig_type == CONTIG_NONE) {
5079 if (ctxt.c_split_covers_rec)
Tao Ma47be12e2009-01-09 07:32:48 +08005080 ret = ocfs2_replace_extent_rec(inode, handle,
5081 path, el,
5082 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005083 else
Tao Mae7d4cb62008-08-18 17:38:44 +08005084 ret = ocfs2_split_and_insert(inode, handle, path, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005085 &last_eb_bh, split_index,
5086 split_rec, meta_ac);
5087 if (ret)
5088 mlog_errno(ret);
5089 } else {
5090 ret = ocfs2_try_to_merge_extent(inode, handle, path,
5091 split_index, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08005092 dealloc, &ctxt, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005093 if (ret)
5094 mlog_errno(ret);
5095 }
5096
Mark Fasheh328d5752007-06-18 10:48:04 -07005097out:
5098 brelse(last_eb_bh);
5099 return ret;
5100}
5101
5102/*
5103 * Mark the already-existing extent at cpos as written for len clusters.
5104 *
5105 * If the existing extent is larger than the request, initiate a
5106 * split. An attempt will be made at merging with adjacent extents.
5107 *
5108 * The caller is responsible for passing down meta_ac if we'll need it.
5109 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005110int ocfs2_mark_extent_written(struct inode *inode,
5111 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005112 handle_t *handle, u32 cpos, u32 len, u32 phys,
5113 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005114 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005115{
5116 int ret, index;
5117 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
5118 struct ocfs2_extent_rec split_rec;
5119 struct ocfs2_path *left_path = NULL;
5120 struct ocfs2_extent_list *el;
5121
5122 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5123 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
5124
5125 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5126 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5127 "that are being written to, but the feature bit "
5128 "is not set in the super block.",
5129 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5130 ret = -EROFS;
5131 goto out;
5132 }
5133
5134 /*
5135 * XXX: This should be fixed up so that we just re-insert the
5136 * next extent records.
Joel Beckerf99b9b72008-08-20 19:36:33 -07005137 *
5138 * XXX: This is a hack on the extent tree, maybe it should be
5139 * an op?
Mark Fasheh328d5752007-06-18 10:48:04 -07005140 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005141 if (et->et_ops == &ocfs2_dinode_et_ops)
Tao Mae7d4cb62008-08-18 17:38:44 +08005142 ocfs2_extent_map_trunc(inode, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07005143
Joel Beckerffdd7a52008-10-17 22:32:01 -07005144 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005145 if (!left_path) {
5146 ret = -ENOMEM;
5147 mlog_errno(ret);
5148 goto out;
5149 }
5150
5151 ret = ocfs2_find_path(inode, left_path, cpos);
5152 if (ret) {
5153 mlog_errno(ret);
5154 goto out;
5155 }
5156 el = path_leaf_el(left_path);
5157
5158 index = ocfs2_search_extent_list(el, cpos);
5159 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5160 ocfs2_error(inode->i_sb,
5161 "Inode %llu has an extent at cpos %u which can no "
5162 "longer be found.\n",
5163 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5164 ret = -EROFS;
5165 goto out;
5166 }
5167
5168 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5169 split_rec.e_cpos = cpu_to_le32(cpos);
5170 split_rec.e_leaf_clusters = cpu_to_le16(len);
5171 split_rec.e_blkno = cpu_to_le64(start_blkno);
5172 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
5173 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
5174
Joel Beckerf99b9b72008-08-20 19:36:33 -07005175 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
Tao Mae7d4cb62008-08-18 17:38:44 +08005176 index, &split_rec, meta_ac,
5177 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005178 if (ret)
5179 mlog_errno(ret);
5180
5181out:
5182 ocfs2_free_path(left_path);
5183 return ret;
5184}
5185
Tao Mae7d4cb62008-08-18 17:38:44 +08005186static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005187 handle_t *handle, struct ocfs2_path *path,
5188 int index, u32 new_range,
5189 struct ocfs2_alloc_context *meta_ac)
5190{
5191 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005192 struct buffer_head *last_eb_bh = NULL;
5193 struct ocfs2_extent_block *eb;
5194 struct ocfs2_extent_list *rightmost_el, *el;
5195 struct ocfs2_extent_rec split_rec;
5196 struct ocfs2_extent_rec *rec;
5197 struct ocfs2_insert_type insert;
5198
5199 /*
5200 * Setup the record to split before we grow the tree.
5201 */
5202 el = path_leaf_el(path);
5203 rec = &el->l_recs[index];
5204 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5205
5206 depth = path->p_tree_depth;
5207 if (depth > 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08005208 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005209 ocfs2_et_get_last_eb_blk(et),
5210 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005211 if (ret < 0) {
5212 mlog_errno(ret);
5213 goto out;
5214 }
5215
5216 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5217 rightmost_el = &eb->h_list;
5218 } else
5219 rightmost_el = path_leaf_el(path);
5220
Tao Ma811f9332008-08-18 17:38:43 +08005221 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005222 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005223 ret = ocfs2_extend_trans(handle, credits);
5224 if (ret) {
5225 mlog_errno(ret);
5226 goto out;
5227 }
5228
5229 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5230 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005231 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005232 meta_ac);
5233 if (ret) {
5234 mlog_errno(ret);
5235 goto out;
5236 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005237 }
5238
5239 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5240 insert.ins_appending = APPEND_NONE;
5241 insert.ins_contig = CONTIG_NONE;
5242 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005243 insert.ins_tree_depth = depth;
5244
Tao Mae7d4cb62008-08-18 17:38:44 +08005245 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005246 if (ret)
5247 mlog_errno(ret);
5248
5249out:
5250 brelse(last_eb_bh);
5251 return ret;
5252}
5253
5254static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5255 struct ocfs2_path *path, int index,
5256 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08005257 u32 cpos, u32 len,
5258 struct ocfs2_extent_tree *et)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005259{
5260 int ret;
5261 u32 left_cpos, rec_range, trunc_range;
5262 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5263 struct super_block *sb = inode->i_sb;
5264 struct ocfs2_path *left_path = NULL;
5265 struct ocfs2_extent_list *el = path_leaf_el(path);
5266 struct ocfs2_extent_rec *rec;
5267 struct ocfs2_extent_block *eb;
5268
5269 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005270 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005271 if (ret) {
5272 mlog_errno(ret);
5273 goto out;
5274 }
5275
5276 index--;
5277 }
5278
5279 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5280 path->p_tree_depth) {
5281 /*
5282 * Check whether this is the rightmost tree record. If
5283 * we remove all of this record or part of its right
5284 * edge then an update of the record lengths above it
5285 * will be required.
5286 */
5287 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5288 if (eb->h_next_leaf_blk == 0)
5289 is_rightmost_tree_rec = 1;
5290 }
5291
5292 rec = &el->l_recs[index];
5293 if (index == 0 && path->p_tree_depth &&
5294 le32_to_cpu(rec->e_cpos) == cpos) {
5295 /*
5296 * Changing the leftmost offset (via partial or whole
5297 * record truncate) of an interior (or rightmost) path
5298 * means we have to update the subtree that is formed
5299 * by this leaf and the one to it's left.
5300 *
5301 * There are two cases we can skip:
5302 * 1) Path is the leftmost one in our inode tree.
5303 * 2) The leaf is rightmost and will be empty after
5304 * we remove the extent record - the rotate code
5305 * knows how to update the newly formed edge.
5306 */
5307
5308 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5309 &left_cpos);
5310 if (ret) {
5311 mlog_errno(ret);
5312 goto out;
5313 }
5314
5315 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005316 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005317 if (!left_path) {
5318 ret = -ENOMEM;
5319 mlog_errno(ret);
5320 goto out;
5321 }
5322
5323 ret = ocfs2_find_path(inode, left_path, left_cpos);
5324 if (ret) {
5325 mlog_errno(ret);
5326 goto out;
5327 }
5328 }
5329 }
5330
5331 ret = ocfs2_extend_rotate_transaction(handle, 0,
5332 handle->h_buffer_credits,
5333 path);
5334 if (ret) {
5335 mlog_errno(ret);
5336 goto out;
5337 }
5338
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005339 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005340 if (ret) {
5341 mlog_errno(ret);
5342 goto out;
5343 }
5344
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005345 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005346 if (ret) {
5347 mlog_errno(ret);
5348 goto out;
5349 }
5350
5351 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5352 trunc_range = cpos + len;
5353
5354 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5355 int next_free;
5356
5357 memset(rec, 0, sizeof(*rec));
5358 ocfs2_cleanup_merge(el, index);
5359 wants_rotate = 1;
5360
5361 next_free = le16_to_cpu(el->l_next_free_rec);
5362 if (is_rightmost_tree_rec && next_free > 1) {
5363 /*
5364 * We skip the edge update if this path will
5365 * be deleted by the rotate code.
5366 */
5367 rec = &el->l_recs[next_free - 1];
5368 ocfs2_adjust_rightmost_records(inode, handle, path,
5369 rec);
5370 }
5371 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5372 /* Remove leftmost portion of the record. */
5373 le32_add_cpu(&rec->e_cpos, len);
5374 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5375 le16_add_cpu(&rec->e_leaf_clusters, -len);
5376 } else if (rec_range == trunc_range) {
5377 /* Remove rightmost portion of the record */
5378 le16_add_cpu(&rec->e_leaf_clusters, -len);
5379 if (is_rightmost_tree_rec)
5380 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5381 } else {
5382 /* Caller should have trapped this. */
5383 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5384 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5385 le32_to_cpu(rec->e_cpos),
5386 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5387 BUG();
5388 }
5389
5390 if (left_path) {
5391 int subtree_index;
5392
5393 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5394 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5395 subtree_index);
5396 }
5397
5398 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5399
Tao Mae7d4cb62008-08-18 17:38:44 +08005400 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005401 if (ret) {
5402 mlog_errno(ret);
5403 goto out;
5404 }
5405
5406out:
5407 ocfs2_free_path(left_path);
5408 return ret;
5409}
5410
Joel Beckerf99b9b72008-08-20 19:36:33 -07005411int ocfs2_remove_extent(struct inode *inode,
5412 struct ocfs2_extent_tree *et,
Mark Fasheh063c4562007-07-03 13:34:11 -07005413 u32 cpos, u32 len, handle_t *handle,
5414 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005415 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005416{
5417 int ret, index;
5418 u32 rec_range, trunc_range;
5419 struct ocfs2_extent_rec *rec;
5420 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005421 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005422
5423 ocfs2_extent_map_trunc(inode, 0);
5424
Joel Beckerffdd7a52008-10-17 22:32:01 -07005425 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005426 if (!path) {
5427 ret = -ENOMEM;
5428 mlog_errno(ret);
5429 goto out;
5430 }
5431
5432 ret = ocfs2_find_path(inode, path, cpos);
5433 if (ret) {
5434 mlog_errno(ret);
5435 goto out;
5436 }
5437
5438 el = path_leaf_el(path);
5439 index = ocfs2_search_extent_list(el, cpos);
5440 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5441 ocfs2_error(inode->i_sb,
5442 "Inode %llu has an extent at cpos %u which can no "
5443 "longer be found.\n",
5444 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5445 ret = -EROFS;
5446 goto out;
5447 }
5448
5449 /*
5450 * We have 3 cases of extent removal:
5451 * 1) Range covers the entire extent rec
5452 * 2) Range begins or ends on one edge of the extent rec
5453 * 3) Range is in the middle of the extent rec (no shared edges)
5454 *
5455 * For case 1 we remove the extent rec and left rotate to
5456 * fill the hole.
5457 *
5458 * For case 2 we just shrink the existing extent rec, with a
5459 * tree update if the shrinking edge is also the edge of an
5460 * extent block.
5461 *
5462 * For case 3 we do a right split to turn the extent rec into
5463 * something case 2 can handle.
5464 */
5465 rec = &el->l_recs[index];
5466 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5467 trunc_range = cpos + len;
5468
5469 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5470
5471 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5472 "(cpos %u, len %u)\n",
5473 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5474 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5475
5476 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5477 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005478 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005479 if (ret) {
5480 mlog_errno(ret);
5481 goto out;
5482 }
5483 } else {
Joel Beckerf99b9b72008-08-20 19:36:33 -07005484 ret = ocfs2_split_tree(inode, et, handle, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005485 trunc_range, meta_ac);
5486 if (ret) {
5487 mlog_errno(ret);
5488 goto out;
5489 }
5490
5491 /*
5492 * The split could have manipulated the tree enough to
5493 * move the record location, so we have to look for it again.
5494 */
5495 ocfs2_reinit_path(path, 1);
5496
5497 ret = ocfs2_find_path(inode, path, cpos);
5498 if (ret) {
5499 mlog_errno(ret);
5500 goto out;
5501 }
5502
5503 el = path_leaf_el(path);
5504 index = ocfs2_search_extent_list(el, cpos);
5505 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5506 ocfs2_error(inode->i_sb,
5507 "Inode %llu: split at cpos %u lost record.",
5508 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5509 cpos);
5510 ret = -EROFS;
5511 goto out;
5512 }
5513
5514 /*
5515 * Double check our values here. If anything is fishy,
5516 * it's easier to catch it at the top level.
5517 */
5518 rec = &el->l_recs[index];
5519 rec_range = le32_to_cpu(rec->e_cpos) +
5520 ocfs2_rec_clusters(el, rec);
5521 if (rec_range != trunc_range) {
5522 ocfs2_error(inode->i_sb,
5523 "Inode %llu: error after split at cpos %u"
5524 "trunc len %u, existing record is (%u,%u)",
5525 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5526 cpos, len, le32_to_cpu(rec->e_cpos),
5527 ocfs2_rec_clusters(el, rec));
5528 ret = -EROFS;
5529 goto out;
5530 }
5531
5532 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005533 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005534 if (ret) {
5535 mlog_errno(ret);
5536 goto out;
5537 }
5538 }
5539
5540out:
5541 ocfs2_free_path(path);
5542 return ret;
5543}
5544
Mark Fashehfecc0112008-11-12 15:16:38 -08005545int ocfs2_remove_btree_range(struct inode *inode,
5546 struct ocfs2_extent_tree *et,
5547 u32 cpos, u32 phys_cpos, u32 len,
5548 struct ocfs2_cached_dealloc_ctxt *dealloc)
5549{
5550 int ret;
5551 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5552 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5553 struct inode *tl_inode = osb->osb_tl_inode;
5554 handle_t *handle;
5555 struct ocfs2_alloc_context *meta_ac = NULL;
5556
5557 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5558 if (ret) {
5559 mlog_errno(ret);
5560 return ret;
5561 }
5562
5563 mutex_lock(&tl_inode->i_mutex);
5564
5565 if (ocfs2_truncate_log_needs_flush(osb)) {
5566 ret = __ocfs2_flush_truncate_log(osb);
5567 if (ret < 0) {
5568 mlog_errno(ret);
5569 goto out;
5570 }
5571 }
5572
Jan Karaa90714c2008-10-09 19:38:40 +02005573 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005574 if (IS_ERR(handle)) {
5575 ret = PTR_ERR(handle);
5576 mlog_errno(ret);
5577 goto out;
5578 }
5579
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005580 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07005581 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005582 if (ret) {
5583 mlog_errno(ret);
5584 goto out;
5585 }
5586
Mark Fashehfd4ef232009-01-29 15:06:21 -08005587 vfs_dq_free_space_nodirty(inode,
5588 ocfs2_clusters_to_bytes(inode->i_sb, len));
5589
Mark Fashehfecc0112008-11-12 15:16:38 -08005590 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5591 dealloc);
5592 if (ret) {
5593 mlog_errno(ret);
5594 goto out_commit;
5595 }
5596
5597 ocfs2_et_update_clusters(inode, et, -len);
5598
5599 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5600 if (ret) {
5601 mlog_errno(ret);
5602 goto out_commit;
5603 }
5604
5605 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5606 if (ret)
5607 mlog_errno(ret);
5608
5609out_commit:
5610 ocfs2_commit_trans(osb, handle);
5611out:
5612 mutex_unlock(&tl_inode->i_mutex);
5613
5614 if (meta_ac)
5615 ocfs2_free_alloc_context(meta_ac);
5616
5617 return ret;
5618}
5619
Mark Fasheh063c4562007-07-03 13:34:11 -07005620int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005621{
5622 struct buffer_head *tl_bh = osb->osb_tl_bh;
5623 struct ocfs2_dinode *di;
5624 struct ocfs2_truncate_log *tl;
5625
5626 di = (struct ocfs2_dinode *) tl_bh->b_data;
5627 tl = &di->id2.i_dealloc;
5628
5629 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5630 "slot %d, invalid truncate log parameters: used = "
5631 "%u, count = %u\n", osb->slot_num,
5632 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5633 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5634}
5635
5636static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5637 unsigned int new_start)
5638{
5639 unsigned int tail_index;
5640 unsigned int current_tail;
5641
5642 /* No records, nothing to coalesce */
5643 if (!le16_to_cpu(tl->tl_used))
5644 return 0;
5645
5646 tail_index = le16_to_cpu(tl->tl_used) - 1;
5647 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5648 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5649
5650 return current_tail == new_start;
5651}
5652
Mark Fasheh063c4562007-07-03 13:34:11 -07005653int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5654 handle_t *handle,
5655 u64 start_blk,
5656 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005657{
5658 int status, index;
5659 unsigned int start_cluster, tl_count;
5660 struct inode *tl_inode = osb->osb_tl_inode;
5661 struct buffer_head *tl_bh = osb->osb_tl_bh;
5662 struct ocfs2_dinode *di;
5663 struct ocfs2_truncate_log *tl;
5664
Mark Fashehb0697052006-03-03 10:24:33 -08005665 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5666 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005667
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005668 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005669
5670 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5671
5672 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005673
Joel Becker10995aa2008-11-13 14:49:12 -08005674 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5675 * by the underlying call to ocfs2_read_inode_block(), so any
5676 * corruption is a code bug */
5677 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5678
5679 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005680 tl_count = le16_to_cpu(tl->tl_count);
5681 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5682 tl_count == 0,
Mark Fashehb0697052006-03-03 10:24:33 -08005683 "Truncate record count on #%llu invalid "
5684 "wanted %u, actual %u\n",
5685 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005686 ocfs2_truncate_recs_per_inode(osb->sb),
5687 le16_to_cpu(tl->tl_count));
5688
5689 /* Caller should have known to flush before calling us. */
5690 index = le16_to_cpu(tl->tl_used);
5691 if (index >= tl_count) {
5692 status = -ENOSPC;
5693 mlog_errno(status);
5694 goto bail;
5695 }
5696
Joel Becker0cf2f762009-02-12 16:41:25 -08005697 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005698 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005699 if (status < 0) {
5700 mlog_errno(status);
5701 goto bail;
5702 }
5703
5704 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb0697052006-03-03 10:24:33 -08005705 "%llu (index = %d)\n", num_clusters, start_cluster,
5706 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005707
5708 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5709 /*
5710 * Move index back to the record we are coalescing with.
5711 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5712 */
5713 index--;
5714
5715 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5716 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5717 index, le32_to_cpu(tl->tl_recs[index].t_start),
5718 num_clusters);
5719 } else {
5720 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5721 tl->tl_used = cpu_to_le16(index + 1);
5722 }
5723 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5724
5725 status = ocfs2_journal_dirty(handle, tl_bh);
5726 if (status < 0) {
5727 mlog_errno(status);
5728 goto bail;
5729 }
5730
5731bail:
5732 mlog_exit(status);
5733 return status;
5734}
5735
5736static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005737 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005738 struct inode *data_alloc_inode,
5739 struct buffer_head *data_alloc_bh)
5740{
5741 int status = 0;
5742 int i;
5743 unsigned int num_clusters;
5744 u64 start_blk;
5745 struct ocfs2_truncate_rec rec;
5746 struct ocfs2_dinode *di;
5747 struct ocfs2_truncate_log *tl;
5748 struct inode *tl_inode = osb->osb_tl_inode;
5749 struct buffer_head *tl_bh = osb->osb_tl_bh;
5750
5751 mlog_entry_void();
5752
5753 di = (struct ocfs2_dinode *) tl_bh->b_data;
5754 tl = &di->id2.i_dealloc;
5755 i = le16_to_cpu(tl->tl_used) - 1;
5756 while (i >= 0) {
5757 /* Caller has given us at least enough credits to
5758 * update the truncate log dinode */
Joel Becker0cf2f762009-02-12 16:41:25 -08005759 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005760 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005761 if (status < 0) {
5762 mlog_errno(status);
5763 goto bail;
5764 }
5765
5766 tl->tl_used = cpu_to_le16(i);
5767
5768 status = ocfs2_journal_dirty(handle, tl_bh);
5769 if (status < 0) {
5770 mlog_errno(status);
5771 goto bail;
5772 }
5773
5774 /* TODO: Perhaps we can calculate the bulk of the
5775 * credits up front rather than extending like
5776 * this. */
5777 status = ocfs2_extend_trans(handle,
5778 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5779 if (status < 0) {
5780 mlog_errno(status);
5781 goto bail;
5782 }
5783
5784 rec = tl->tl_recs[i];
5785 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5786 le32_to_cpu(rec.t_start));
5787 num_clusters = le32_to_cpu(rec.t_clusters);
5788
5789 /* if start_blk is not set, we ignore the record as
5790 * invalid. */
5791 if (start_blk) {
5792 mlog(0, "free record %d, start = %u, clusters = %u\n",
5793 i, le32_to_cpu(rec.t_start), num_clusters);
5794
5795 status = ocfs2_free_clusters(handle, data_alloc_inode,
5796 data_alloc_bh, start_blk,
5797 num_clusters);
5798 if (status < 0) {
5799 mlog_errno(status);
5800 goto bail;
5801 }
5802 }
5803 i--;
5804 }
5805
5806bail:
5807 mlog_exit(status);
5808 return status;
5809}
5810
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005811/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005812int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005813{
5814 int status;
5815 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005816 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005817 struct inode *tl_inode = osb->osb_tl_inode;
5818 struct inode *data_alloc_inode = NULL;
5819 struct buffer_head *tl_bh = osb->osb_tl_bh;
5820 struct buffer_head *data_alloc_bh = NULL;
5821 struct ocfs2_dinode *di;
5822 struct ocfs2_truncate_log *tl;
5823
5824 mlog_entry_void();
5825
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005826 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005827
5828 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005829
Joel Becker10995aa2008-11-13 14:49:12 -08005830 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5831 * by the underlying call to ocfs2_read_inode_block(), so any
5832 * corruption is a code bug */
5833 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5834
5835 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005836 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb0697052006-03-03 10:24:33 -08005837 mlog(0, "Flush %u records from truncate log #%llu\n",
5838 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005839 if (!num_to_flush) {
5840 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005841 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005842 }
5843
5844 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5845 GLOBAL_BITMAP_SYSTEM_INODE,
5846 OCFS2_INVALID_SLOT);
5847 if (!data_alloc_inode) {
5848 status = -EINVAL;
5849 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005850 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005851 }
5852
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005853 mutex_lock(&data_alloc_inode->i_mutex);
5854
Mark Fashehe63aecb62007-10-18 15:30:42 -07005855 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005856 if (status < 0) {
5857 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005858 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005859 }
5860
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005861 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005862 if (IS_ERR(handle)) {
5863 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005864 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005865 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005866 }
5867
5868 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5869 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005870 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005871 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005872
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005873 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005874
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005875out_unlock:
5876 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005877 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005878
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005879out_mutex:
5880 mutex_unlock(&data_alloc_inode->i_mutex);
5881 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005882
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005883out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005884 mlog_exit(status);
5885 return status;
5886}
5887
5888int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5889{
5890 int status;
5891 struct inode *tl_inode = osb->osb_tl_inode;
5892
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005893 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005894 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005895 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005896
5897 return status;
5898}
5899
David Howellsc4028952006-11-22 14:57:56 +00005900static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005901{
5902 int status;
David Howellsc4028952006-11-22 14:57:56 +00005903 struct ocfs2_super *osb =
5904 container_of(work, struct ocfs2_super,
5905 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005906
5907 mlog_entry_void();
5908
5909 status = ocfs2_flush_truncate_log(osb);
5910 if (status < 0)
5911 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005912 else
5913 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005914
5915 mlog_exit(status);
5916}
5917
5918#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5919void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5920 int cancel)
5921{
5922 if (osb->osb_tl_inode) {
5923 /* We want to push off log flushes while truncates are
5924 * still running. */
5925 if (cancel)
5926 cancel_delayed_work(&osb->osb_truncate_log_wq);
5927
5928 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5929 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5930 }
5931}
5932
5933static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5934 int slot_num,
5935 struct inode **tl_inode,
5936 struct buffer_head **tl_bh)
5937{
5938 int status;
5939 struct inode *inode = NULL;
5940 struct buffer_head *bh = NULL;
5941
5942 inode = ocfs2_get_system_file_inode(osb,
5943 TRUNCATE_LOG_SYSTEM_INODE,
5944 slot_num);
5945 if (!inode) {
5946 status = -EINVAL;
5947 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5948 goto bail;
5949 }
5950
Joel Beckerb657c952008-11-13 14:49:11 -08005951 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005952 if (status < 0) {
5953 iput(inode);
5954 mlog_errno(status);
5955 goto bail;
5956 }
5957
5958 *tl_inode = inode;
5959 *tl_bh = bh;
5960bail:
5961 mlog_exit(status);
5962 return status;
5963}
5964
5965/* called during the 1st stage of node recovery. we stamp a clean
5966 * truncate log and pass back a copy for processing later. if the
5967 * truncate log does not require processing, a *tl_copy is set to
5968 * NULL. */
5969int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5970 int slot_num,
5971 struct ocfs2_dinode **tl_copy)
5972{
5973 int status;
5974 struct inode *tl_inode = NULL;
5975 struct buffer_head *tl_bh = NULL;
5976 struct ocfs2_dinode *di;
5977 struct ocfs2_truncate_log *tl;
5978
5979 *tl_copy = NULL;
5980
5981 mlog(0, "recover truncate log from slot %d\n", slot_num);
5982
5983 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5984 if (status < 0) {
5985 mlog_errno(status);
5986 goto bail;
5987 }
5988
5989 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005990
Joel Becker10995aa2008-11-13 14:49:12 -08005991 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5992 * validated by the underlying call to ocfs2_read_inode_block(),
5993 * so any corruption is a code bug */
5994 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5995
5996 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005997 if (le16_to_cpu(tl->tl_used)) {
5998 mlog(0, "We'll have %u logs to recover\n",
5999 le16_to_cpu(tl->tl_used));
6000
6001 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6002 if (!(*tl_copy)) {
6003 status = -ENOMEM;
6004 mlog_errno(status);
6005 goto bail;
6006 }
6007
6008 /* Assuming the write-out below goes well, this copy
6009 * will be passed back to recovery for processing. */
6010 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6011
6012 /* All we need to do to clear the truncate log is set
6013 * tl_used. */
6014 tl->tl_used = 0;
6015
Joel Becker13723d02008-10-17 19:25:01 -07006016 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08006017 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08006018 if (status < 0) {
6019 mlog_errno(status);
6020 goto bail;
6021 }
6022 }
6023
6024bail:
6025 if (tl_inode)
6026 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006027 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006028
6029 if (status < 0 && (*tl_copy)) {
6030 kfree(*tl_copy);
6031 *tl_copy = NULL;
6032 }
6033
6034 mlog_exit(status);
6035 return status;
6036}
6037
6038int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6039 struct ocfs2_dinode *tl_copy)
6040{
6041 int status = 0;
6042 int i;
6043 unsigned int clusters, num_recs, start_cluster;
6044 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006045 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006046 struct inode *tl_inode = osb->osb_tl_inode;
6047 struct ocfs2_truncate_log *tl;
6048
6049 mlog_entry_void();
6050
6051 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6052 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6053 return -EINVAL;
6054 }
6055
6056 tl = &tl_copy->id2.i_dealloc;
6057 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb0697052006-03-03 10:24:33 -08006058 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006059 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006060
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006061 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006062 for(i = 0; i < num_recs; i++) {
6063 if (ocfs2_truncate_log_needs_flush(osb)) {
6064 status = __ocfs2_flush_truncate_log(osb);
6065 if (status < 0) {
6066 mlog_errno(status);
6067 goto bail_up;
6068 }
6069 }
6070
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006071 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006072 if (IS_ERR(handle)) {
6073 status = PTR_ERR(handle);
6074 mlog_errno(status);
6075 goto bail_up;
6076 }
6077
6078 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6079 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6080 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6081
6082 status = ocfs2_truncate_log_append(osb, handle,
6083 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006084 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006085 if (status < 0) {
6086 mlog_errno(status);
6087 goto bail_up;
6088 }
6089 }
6090
6091bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006092 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006093
6094 mlog_exit(status);
6095 return status;
6096}
6097
6098void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6099{
6100 int status;
6101 struct inode *tl_inode = osb->osb_tl_inode;
6102
6103 mlog_entry_void();
6104
6105 if (tl_inode) {
6106 cancel_delayed_work(&osb->osb_truncate_log_wq);
6107 flush_workqueue(ocfs2_wq);
6108
6109 status = ocfs2_flush_truncate_log(osb);
6110 if (status < 0)
6111 mlog_errno(status);
6112
6113 brelse(osb->osb_tl_bh);
6114 iput(osb->osb_tl_inode);
6115 }
6116
6117 mlog_exit_void();
6118}
6119
6120int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6121{
6122 int status;
6123 struct inode *tl_inode = NULL;
6124 struct buffer_head *tl_bh = NULL;
6125
6126 mlog_entry_void();
6127
6128 status = ocfs2_get_truncate_log_info(osb,
6129 osb->slot_num,
6130 &tl_inode,
6131 &tl_bh);
6132 if (status < 0)
6133 mlog_errno(status);
6134
6135 /* ocfs2_truncate_log_shutdown keys on the existence of
6136 * osb->osb_tl_inode so we don't set any of the osb variables
6137 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006138 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6139 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006140 osb->osb_tl_bh = tl_bh;
6141 osb->osb_tl_inode = tl_inode;
6142
6143 mlog_exit(status);
6144 return status;
6145}
6146
Mark Fasheh2b604352007-06-22 15:45:27 -07006147/*
6148 * Delayed de-allocation of suballocator blocks.
6149 *
6150 * Some sets of block de-allocations might involve multiple suballocator inodes.
6151 *
6152 * The locking for this can get extremely complicated, especially when
6153 * the suballocator inodes to delete from aren't known until deep
6154 * within an unrelated codepath.
6155 *
6156 * ocfs2_extent_block structures are a good example of this - an inode
6157 * btree could have been grown by any number of nodes each allocating
6158 * out of their own suballoc inode.
6159 *
6160 * These structures allow the delay of block de-allocation until a
6161 * later time, when locking of multiple cluster inodes won't cause
6162 * deadlock.
6163 */
6164
6165/*
Tao Ma2891d292008-11-12 08:26:58 +08006166 * Describe a single bit freed from a suballocator. For the block
6167 * suballocators, it represents one block. For the global cluster
6168 * allocator, it represents some clusters and free_bit indicates
6169 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006170 */
6171struct ocfs2_cached_block_free {
6172 struct ocfs2_cached_block_free *free_next;
6173 u64 free_blk;
6174 unsigned int free_bit;
6175};
6176
6177struct ocfs2_per_slot_free_list {
6178 struct ocfs2_per_slot_free_list *f_next_suballocator;
6179 int f_inode_type;
6180 int f_slot;
6181 struct ocfs2_cached_block_free *f_first;
6182};
6183
Tao Ma2891d292008-11-12 08:26:58 +08006184static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6185 int sysfile_type,
6186 int slot,
6187 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006188{
6189 int ret;
6190 u64 bg_blkno;
6191 handle_t *handle;
6192 struct inode *inode;
6193 struct buffer_head *di_bh = NULL;
6194 struct ocfs2_cached_block_free *tmp;
6195
6196 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6197 if (!inode) {
6198 ret = -EINVAL;
6199 mlog_errno(ret);
6200 goto out;
6201 }
6202
6203 mutex_lock(&inode->i_mutex);
6204
Mark Fashehe63aecb62007-10-18 15:30:42 -07006205 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006206 if (ret) {
6207 mlog_errno(ret);
6208 goto out_mutex;
6209 }
6210
6211 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6212 if (IS_ERR(handle)) {
6213 ret = PTR_ERR(handle);
6214 mlog_errno(ret);
6215 goto out_unlock;
6216 }
6217
6218 while (head) {
6219 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6220 head->free_bit);
6221 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6222 head->free_bit, (unsigned long long)head->free_blk);
6223
6224 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6225 head->free_bit, bg_blkno, 1);
6226 if (ret) {
6227 mlog_errno(ret);
6228 goto out_journal;
6229 }
6230
6231 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6232 if (ret) {
6233 mlog_errno(ret);
6234 goto out_journal;
6235 }
6236
6237 tmp = head;
6238 head = head->free_next;
6239 kfree(tmp);
6240 }
6241
6242out_journal:
6243 ocfs2_commit_trans(osb, handle);
6244
6245out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006246 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006247 brelse(di_bh);
6248out_mutex:
6249 mutex_unlock(&inode->i_mutex);
6250 iput(inode);
6251out:
6252 while(head) {
6253 /* Premature exit may have left some dangling items. */
6254 tmp = head;
6255 head = head->free_next;
6256 kfree(tmp);
6257 }
6258
6259 return ret;
6260}
6261
Tao Ma2891d292008-11-12 08:26:58 +08006262int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6263 u64 blkno, unsigned int bit)
6264{
6265 int ret = 0;
6266 struct ocfs2_cached_block_free *item;
6267
6268 item = kmalloc(sizeof(*item), GFP_NOFS);
6269 if (item == NULL) {
6270 ret = -ENOMEM;
6271 mlog_errno(ret);
6272 return ret;
6273 }
6274
6275 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6276 bit, (unsigned long long)blkno);
6277
6278 item->free_blk = blkno;
6279 item->free_bit = bit;
6280 item->free_next = ctxt->c_global_allocator;
6281
6282 ctxt->c_global_allocator = item;
6283 return ret;
6284}
6285
6286static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6287 struct ocfs2_cached_block_free *head)
6288{
6289 struct ocfs2_cached_block_free *tmp;
6290 struct inode *tl_inode = osb->osb_tl_inode;
6291 handle_t *handle;
6292 int ret = 0;
6293
6294 mutex_lock(&tl_inode->i_mutex);
6295
6296 while (head) {
6297 if (ocfs2_truncate_log_needs_flush(osb)) {
6298 ret = __ocfs2_flush_truncate_log(osb);
6299 if (ret < 0) {
6300 mlog_errno(ret);
6301 break;
6302 }
6303 }
6304
6305 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6306 if (IS_ERR(handle)) {
6307 ret = PTR_ERR(handle);
6308 mlog_errno(ret);
6309 break;
6310 }
6311
6312 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6313 head->free_bit);
6314
6315 ocfs2_commit_trans(osb, handle);
6316 tmp = head;
6317 head = head->free_next;
6318 kfree(tmp);
6319
6320 if (ret < 0) {
6321 mlog_errno(ret);
6322 break;
6323 }
6324 }
6325
6326 mutex_unlock(&tl_inode->i_mutex);
6327
6328 while (head) {
6329 /* Premature exit may have left some dangling items. */
6330 tmp = head;
6331 head = head->free_next;
6332 kfree(tmp);
6333 }
6334
6335 return ret;
6336}
6337
Mark Fasheh2b604352007-06-22 15:45:27 -07006338int ocfs2_run_deallocs(struct ocfs2_super *osb,
6339 struct ocfs2_cached_dealloc_ctxt *ctxt)
6340{
6341 int ret = 0, ret2;
6342 struct ocfs2_per_slot_free_list *fl;
6343
6344 if (!ctxt)
6345 return 0;
6346
6347 while (ctxt->c_first_suballocator) {
6348 fl = ctxt->c_first_suballocator;
6349
6350 if (fl->f_first) {
6351 mlog(0, "Free items: (type %u, slot %d)\n",
6352 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006353 ret2 = ocfs2_free_cached_blocks(osb,
6354 fl->f_inode_type,
6355 fl->f_slot,
6356 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006357 if (ret2)
6358 mlog_errno(ret2);
6359 if (!ret)
6360 ret = ret2;
6361 }
6362
6363 ctxt->c_first_suballocator = fl->f_next_suballocator;
6364 kfree(fl);
6365 }
6366
Tao Ma2891d292008-11-12 08:26:58 +08006367 if (ctxt->c_global_allocator) {
6368 ret2 = ocfs2_free_cached_clusters(osb,
6369 ctxt->c_global_allocator);
6370 if (ret2)
6371 mlog_errno(ret2);
6372 if (!ret)
6373 ret = ret2;
6374
6375 ctxt->c_global_allocator = NULL;
6376 }
6377
Mark Fasheh2b604352007-06-22 15:45:27 -07006378 return ret;
6379}
6380
6381static struct ocfs2_per_slot_free_list *
6382ocfs2_find_per_slot_free_list(int type,
6383 int slot,
6384 struct ocfs2_cached_dealloc_ctxt *ctxt)
6385{
6386 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6387
6388 while (fl) {
6389 if (fl->f_inode_type == type && fl->f_slot == slot)
6390 return fl;
6391
6392 fl = fl->f_next_suballocator;
6393 }
6394
6395 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6396 if (fl) {
6397 fl->f_inode_type = type;
6398 fl->f_slot = slot;
6399 fl->f_first = NULL;
6400 fl->f_next_suballocator = ctxt->c_first_suballocator;
6401
6402 ctxt->c_first_suballocator = fl;
6403 }
6404 return fl;
6405}
6406
6407static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6408 int type, int slot, u64 blkno,
6409 unsigned int bit)
6410{
6411 int ret;
6412 struct ocfs2_per_slot_free_list *fl;
6413 struct ocfs2_cached_block_free *item;
6414
6415 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6416 if (fl == NULL) {
6417 ret = -ENOMEM;
6418 mlog_errno(ret);
6419 goto out;
6420 }
6421
6422 item = kmalloc(sizeof(*item), GFP_NOFS);
6423 if (item == NULL) {
6424 ret = -ENOMEM;
6425 mlog_errno(ret);
6426 goto out;
6427 }
6428
6429 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6430 type, slot, bit, (unsigned long long)blkno);
6431
6432 item->free_blk = blkno;
6433 item->free_bit = bit;
6434 item->free_next = fl->f_first;
6435
6436 fl->f_first = item;
6437
6438 ret = 0;
6439out:
6440 return ret;
6441}
6442
Mark Fasheh59a5e412007-06-22 15:52:36 -07006443static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6444 struct ocfs2_extent_block *eb)
6445{
6446 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6447 le16_to_cpu(eb->h_suballoc_slot),
6448 le64_to_cpu(eb->h_blkno),
6449 le16_to_cpu(eb->h_suballoc_bit));
6450}
6451
Mark Fashehccd979b2005-12-15 14:31:24 -08006452/* This function will figure out whether the currently last extent
6453 * block will be deleted, and if it will, what the new last extent
6454 * block will be so we can update his h_next_leaf_blk field, as well
6455 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006456static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006457 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006458 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006459 struct buffer_head **new_last_eb)
6460{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006461 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006462 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006463 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006464 struct ocfs2_extent_block *eb;
6465 struct ocfs2_extent_list *el;
6466 struct buffer_head *bh = NULL;
6467
6468 *new_last_eb = NULL;
6469
Mark Fashehccd979b2005-12-15 14:31:24 -08006470 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006471 if (!path->p_tree_depth)
6472 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006473
6474 /* trunc to zero special case - this makes tree_depth = 0
6475 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006476 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006477 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006478
Mark Fashehdcd05382007-01-16 11:32:23 -08006479 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006480 BUG_ON(!el->l_next_free_rec);
6481
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006482 /*
6483 * Make sure that this extent list will actually be empty
6484 * after we clear away the data. We can shortcut out if
6485 * there's more than one non-empty extent in the
6486 * list. Otherwise, a check of the remaining extent is
6487 * necessary.
6488 */
6489 next_free = le16_to_cpu(el->l_next_free_rec);
6490 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006491 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006492 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006493 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006494
6495 /* We may have a valid extent in index 1, check it. */
6496 if (next_free == 2)
6497 rec = &el->l_recs[1];
6498
6499 /*
6500 * Fall through - no more nonempty extents, so we want
6501 * to delete this leaf.
6502 */
6503 } else {
6504 if (next_free > 1)
6505 goto out;
6506
6507 rec = &el->l_recs[0];
6508 }
6509
6510 if (rec) {
6511 /*
6512 * Check it we'll only be trimming off the end of this
6513 * cluster.
6514 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006515 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006516 goto out;
6517 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006518
Mark Fashehdcd05382007-01-16 11:32:23 -08006519 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6520 if (ret) {
6521 mlog_errno(ret);
6522 goto out;
6523 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006524
Mark Fashehdcd05382007-01-16 11:32:23 -08006525 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6526 if (ret) {
6527 mlog_errno(ret);
6528 goto out;
6529 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006530
Mark Fashehdcd05382007-01-16 11:32:23 -08006531 eb = (struct ocfs2_extent_block *) bh->b_data;
6532 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006533
6534 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6535 * Any corruption is a code bug. */
6536 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006537
6538 *new_last_eb = bh;
6539 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006540 mlog(0, "returning block %llu, (cpos: %u)\n",
6541 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6542out:
6543 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006544
Mark Fashehdcd05382007-01-16 11:32:23 -08006545 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006546}
6547
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006548/*
6549 * Trim some clusters off the rightmost edge of a tree. Only called
6550 * during truncate.
6551 *
6552 * The caller needs to:
6553 * - start journaling of each path component.
6554 * - compute and fully set up any new last ext block
6555 */
6556static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6557 handle_t *handle, struct ocfs2_truncate_context *tc,
6558 u32 clusters_to_del, u64 *delete_start)
6559{
6560 int ret, i, index = path->p_tree_depth;
6561 u32 new_edge = 0;
6562 u64 deleted_eb = 0;
6563 struct buffer_head *bh;
6564 struct ocfs2_extent_list *el;
6565 struct ocfs2_extent_rec *rec;
6566
6567 *delete_start = 0;
6568
6569 while (index >= 0) {
6570 bh = path->p_node[index].bh;
6571 el = path->p_node[index].el;
6572
6573 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6574 index, (unsigned long long)bh->b_blocknr);
6575
6576 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6577
6578 if (index !=
6579 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6580 ocfs2_error(inode->i_sb,
6581 "Inode %lu has invalid ext. block %llu",
6582 inode->i_ino,
6583 (unsigned long long)bh->b_blocknr);
6584 ret = -EROFS;
6585 goto out;
6586 }
6587
6588find_tail_record:
6589 i = le16_to_cpu(el->l_next_free_rec) - 1;
6590 rec = &el->l_recs[i];
6591
6592 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6593 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006594 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006595 (unsigned long long)le64_to_cpu(rec->e_blkno),
6596 le16_to_cpu(el->l_next_free_rec));
6597
Mark Fashehe48edee2007-03-07 16:46:57 -08006598 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006599
6600 if (le16_to_cpu(el->l_tree_depth) == 0) {
6601 /*
6602 * If the leaf block contains a single empty
6603 * extent and no records, we can just remove
6604 * the block.
6605 */
6606 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6607 memset(rec, 0,
6608 sizeof(struct ocfs2_extent_rec));
6609 el->l_next_free_rec = cpu_to_le16(0);
6610
6611 goto delete;
6612 }
6613
6614 /*
6615 * Remove any empty extents by shifting things
6616 * left. That should make life much easier on
6617 * the code below. This condition is rare
6618 * enough that we shouldn't see a performance
6619 * hit.
6620 */
6621 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6622 le16_add_cpu(&el->l_next_free_rec, -1);
6623
6624 for(i = 0;
6625 i < le16_to_cpu(el->l_next_free_rec); i++)
6626 el->l_recs[i] = el->l_recs[i + 1];
6627
6628 memset(&el->l_recs[i], 0,
6629 sizeof(struct ocfs2_extent_rec));
6630
6631 /*
6632 * We've modified our extent list. The
6633 * simplest way to handle this change
6634 * is to being the search from the
6635 * start again.
6636 */
6637 goto find_tail_record;
6638 }
6639
Mark Fashehe48edee2007-03-07 16:46:57 -08006640 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006641
6642 /*
6643 * We'll use "new_edge" on our way back up the
6644 * tree to know what our rightmost cpos is.
6645 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006646 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006647 new_edge += le32_to_cpu(rec->e_cpos);
6648
6649 /*
6650 * The caller will use this to delete data blocks.
6651 */
6652 *delete_start = le64_to_cpu(rec->e_blkno)
6653 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006654 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006655
6656 /*
6657 * If it's now empty, remove this record.
6658 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006659 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006660 memset(rec, 0,
6661 sizeof(struct ocfs2_extent_rec));
6662 le16_add_cpu(&el->l_next_free_rec, -1);
6663 }
6664 } else {
6665 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6666 memset(rec, 0,
6667 sizeof(struct ocfs2_extent_rec));
6668 le16_add_cpu(&el->l_next_free_rec, -1);
6669
6670 goto delete;
6671 }
6672
6673 /* Can this actually happen? */
6674 if (le16_to_cpu(el->l_next_free_rec) == 0)
6675 goto delete;
6676
6677 /*
6678 * We never actually deleted any clusters
6679 * because our leaf was empty. There's no
6680 * reason to adjust the rightmost edge then.
6681 */
6682 if (new_edge == 0)
6683 goto delete;
6684
Mark Fashehe48edee2007-03-07 16:46:57 -08006685 rec->e_int_clusters = cpu_to_le32(new_edge);
6686 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006687 -le32_to_cpu(rec->e_cpos));
6688
6689 /*
6690 * A deleted child record should have been
6691 * caught above.
6692 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006693 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006694 }
6695
6696delete:
6697 ret = ocfs2_journal_dirty(handle, bh);
6698 if (ret) {
6699 mlog_errno(ret);
6700 goto out;
6701 }
6702
6703 mlog(0, "extent list container %llu, after: record %d: "
6704 "(%u, %u, %llu), next = %u.\n",
6705 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006706 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006707 (unsigned long long)le64_to_cpu(rec->e_blkno),
6708 le16_to_cpu(el->l_next_free_rec));
6709
6710 /*
6711 * We must be careful to only attempt delete of an
6712 * extent block (and not the root inode block).
6713 */
6714 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6715 struct ocfs2_extent_block *eb =
6716 (struct ocfs2_extent_block *)bh->b_data;
6717
6718 /*
6719 * Save this for use when processing the
6720 * parent block.
6721 */
6722 deleted_eb = le64_to_cpu(eb->h_blkno);
6723
6724 mlog(0, "deleting this extent block.\n");
6725
Joel Becker8cb471e2009-02-10 20:00:41 -08006726 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006727
Mark Fashehe48edee2007-03-07 16:46:57 -08006728 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006729 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6730 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6731
Mark Fasheh59a5e412007-06-22 15:52:36 -07006732 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6733 /* An error here is not fatal. */
6734 if (ret < 0)
6735 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006736 } else {
6737 deleted_eb = 0;
6738 }
6739
6740 index--;
6741 }
6742
6743 ret = 0;
6744out:
6745 return ret;
6746}
6747
Mark Fashehccd979b2005-12-15 14:31:24 -08006748static int ocfs2_do_truncate(struct ocfs2_super *osb,
6749 unsigned int clusters_to_del,
6750 struct inode *inode,
6751 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006752 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006753 struct ocfs2_truncate_context *tc,
6754 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006755{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006756 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006757 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006758 struct ocfs2_extent_block *last_eb = NULL;
6759 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006760 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006761 u64 delete_blk = 0;
6762
6763 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6764
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006765 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006766 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006767 if (status < 0) {
6768 mlog_errno(status);
6769 goto bail;
6770 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006771
Mark Fashehdcd05382007-01-16 11:32:23 -08006772 /*
6773 * Each component will be touched, so we might as well journal
6774 * here to avoid having to handle errors later.
6775 */
Joel Becker0cf2f762009-02-12 16:41:25 -08006776 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006777 if (status < 0) {
6778 mlog_errno(status);
6779 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006780 }
6781
6782 if (last_eb_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08006783 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07006784 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08006785 if (status < 0) {
6786 mlog_errno(status);
6787 goto bail;
6788 }
6789
6790 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6791 }
6792
6793 el = &(fe->id2.i_list);
6794
6795 /*
6796 * Lower levels depend on this never happening, but it's best
6797 * to check it up here before changing the tree.
6798 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006799 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006800 ocfs2_error(inode->i_sb,
6801 "Inode %lu has an empty extent record, depth %u\n",
6802 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006803 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006804 goto bail;
6805 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006806
Jan Karaa90714c2008-10-09 19:38:40 +02006807 vfs_dq_free_space_nodirty(inode,
6808 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006809 spin_lock(&OCFS2_I(inode)->ip_lock);
6810 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6811 clusters_to_del;
6812 spin_unlock(&OCFS2_I(inode)->ip_lock);
6813 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006814 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006815
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006816 status = ocfs2_trim_tree(inode, path, handle, tc,
6817 clusters_to_del, &delete_blk);
6818 if (status) {
6819 mlog_errno(status);
6820 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006821 }
6822
Mark Fashehdcd05382007-01-16 11:32:23 -08006823 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006824 /* trunc to zero is a special case. */
6825 el->l_tree_depth = 0;
6826 fe->i_last_eb_blk = 0;
6827 } else if (last_eb)
6828 fe->i_last_eb_blk = last_eb->h_blkno;
6829
6830 status = ocfs2_journal_dirty(handle, fe_bh);
6831 if (status < 0) {
6832 mlog_errno(status);
6833 goto bail;
6834 }
6835
6836 if (last_eb) {
6837 /* If there will be a new last extent block, then by
6838 * definition, there cannot be any leaves to the right of
6839 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006840 last_eb->h_next_leaf_blk = 0;
6841 status = ocfs2_journal_dirty(handle, last_eb_bh);
6842 if (status < 0) {
6843 mlog_errno(status);
6844 goto bail;
6845 }
6846 }
6847
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006848 if (delete_blk) {
6849 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6850 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006851 if (status < 0) {
6852 mlog_errno(status);
6853 goto bail;
6854 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006855 }
6856 status = 0;
6857bail:
Tao Ma60e2ec42009-08-12 14:42:47 +08006858 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006859 mlog_exit(status);
6860 return status;
6861}
6862
Joel Becker2b4e30f2008-09-03 20:03:41 -07006863static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006864{
6865 set_buffer_uptodate(bh);
6866 mark_buffer_dirty(bh);
6867 return 0;
6868}
6869
Mark Fasheh1d410a62007-09-07 14:20:45 -07006870static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6871 unsigned int from, unsigned int to,
6872 struct page *page, int zero, u64 *phys)
6873{
6874 int ret, partial = 0;
6875
6876 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6877 if (ret)
6878 mlog_errno(ret);
6879
6880 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006881 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006882
6883 /*
6884 * Need to set the buffers we zero'd into uptodate
6885 * here if they aren't - ocfs2_map_page_blocks()
6886 * might've skipped some
6887 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006888 ret = walk_page_buffers(handle, page_buffers(page),
6889 from, to, &partial,
6890 ocfs2_zero_func);
6891 if (ret < 0)
6892 mlog_errno(ret);
6893 else if (ocfs2_should_order_data(inode)) {
6894 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006895 if (ret < 0)
6896 mlog_errno(ret);
6897 }
6898
6899 if (!partial)
6900 SetPageUptodate(page);
6901
6902 flush_dcache_page(page);
6903}
6904
Mark Fasheh35edec12007-07-06 14:41:18 -07006905static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6906 loff_t end, struct page **pages,
6907 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006908{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006909 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006910 struct page *page;
6911 unsigned int from, to = PAGE_CACHE_SIZE;
6912 struct super_block *sb = inode->i_sb;
6913
6914 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6915
6916 if (numpages == 0)
6917 goto out;
6918
Mark Fasheh35edec12007-07-06 14:41:18 -07006919 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006920 for(i = 0; i < numpages; i++) {
6921 page = pages[i];
6922
Mark Fasheh35edec12007-07-06 14:41:18 -07006923 from = start & (PAGE_CACHE_SIZE - 1);
6924 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6925 to = end & (PAGE_CACHE_SIZE - 1);
6926
Mark Fasheh60b11392007-02-16 11:46:50 -08006927 BUG_ON(from > PAGE_CACHE_SIZE);
6928 BUG_ON(to > PAGE_CACHE_SIZE);
6929
Mark Fasheh1d410a62007-09-07 14:20:45 -07006930 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6931 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006932
Mark Fasheh35edec12007-07-06 14:41:18 -07006933 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006934 }
6935out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006936 if (pages)
6937 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006938}
6939
Mark Fasheh35edec12007-07-06 14:41:18 -07006940static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006941 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006942{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006943 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006944 struct super_block *sb = inode->i_sb;
6945 struct address_space *mapping = inode->i_mapping;
6946 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006947 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006948
Mark Fasheh35edec12007-07-06 14:41:18 -07006949 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006950
Mark Fasheh35edec12007-07-06 14:41:18 -07006951 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6952 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6953
Mark Fasheh1d410a62007-09-07 14:20:45 -07006954 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006955 last_page_bytes = PAGE_ALIGN(end);
6956 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006957 do {
6958 pages[numpages] = grab_cache_page(mapping, index);
6959 if (!pages[numpages]) {
6960 ret = -ENOMEM;
6961 mlog_errno(ret);
6962 goto out;
6963 }
6964
6965 numpages++;
6966 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006967 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006968
6969out:
6970 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006971 if (pages)
6972 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006973 numpages = 0;
6974 }
6975
6976 *num = numpages;
6977
6978 return ret;
6979}
6980
6981/*
6982 * Zero the area past i_size but still within an allocated
6983 * cluster. This avoids exposing nonzero data on subsequent file
6984 * extends.
6985 *
6986 * We need to call this before i_size is updated on the inode because
6987 * otherwise block_write_full_page() will skip writeout of pages past
6988 * i_size. The new_i_size parameter is passed for this reason.
6989 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006990int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6991 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006992{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006993 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006994 struct page **pages = NULL;
6995 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006996 unsigned int ext_flags;
6997 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006998
6999 /*
7000 * File systems which don't support sparse files zero on every
7001 * extend.
7002 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07007003 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08007004 return 0;
7005
Mark Fasheh1d410a62007-09-07 14:20:45 -07007006 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08007007 sizeof(struct page *), GFP_NOFS);
7008 if (pages == NULL) {
7009 ret = -ENOMEM;
7010 mlog_errno(ret);
7011 goto out;
7012 }
7013
Mark Fasheh1d410a62007-09-07 14:20:45 -07007014 if (range_start == range_end)
7015 goto out;
7016
7017 ret = ocfs2_extent_map_get_blocks(inode,
7018 range_start >> sb->s_blocksize_bits,
7019 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08007020 if (ret) {
7021 mlog_errno(ret);
7022 goto out;
7023 }
7024
Mark Fasheh1d410a62007-09-07 14:20:45 -07007025 /*
7026 * Tail is a hole, or is marked unwritten. In either case, we
7027 * can count on read and write to return/push zero's.
7028 */
7029 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08007030 goto out;
7031
Mark Fasheh1d410a62007-09-07 14:20:45 -07007032 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7033 &numpages);
7034 if (ret) {
7035 mlog_errno(ret);
7036 goto out;
7037 }
7038
Mark Fasheh35edec12007-07-06 14:41:18 -07007039 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7040 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08007041
7042 /*
7043 * Initiate writeout of the pages we zero'd here. We don't
7044 * wait on them - the truncate_inode_pages() call later will
7045 * do that for us.
7046 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007047 ret = do_sync_mapping_range(inode->i_mapping, range_start,
7048 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08007049 if (ret)
7050 mlog_errno(ret);
7051
7052out:
7053 if (pages)
7054 kfree(pages);
7055
7056 return ret;
7057}
7058
Tiger Yangfdd77702008-08-18 17:08:55 +08007059static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7060 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007061{
7062 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08007063 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007064
Tiger Yangfdd77702008-08-18 17:08:55 +08007065 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7066 memset(&di->id2, 0, blocksize -
7067 offsetof(struct ocfs2_dinode, id2) -
7068 xattrsize);
7069 else
7070 memset(&di->id2, 0, blocksize -
7071 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007072}
7073
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007074void ocfs2_dinode_new_extent_list(struct inode *inode,
7075 struct ocfs2_dinode *di)
7076{
Tiger Yangfdd77702008-08-18 17:08:55 +08007077 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007078 di->id2.i_list.l_tree_depth = 0;
7079 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08007080 di->id2.i_list.l_count = cpu_to_le16(
7081 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007082}
7083
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007084void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7085{
7086 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7087 struct ocfs2_inline_data *idata = &di->id2.i_data;
7088
7089 spin_lock(&oi->ip_lock);
7090 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7091 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7092 spin_unlock(&oi->ip_lock);
7093
7094 /*
7095 * We clear the entire i_data structure here so that all
7096 * fields can be properly initialized.
7097 */
Tiger Yangfdd77702008-08-18 17:08:55 +08007098 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007099
Tiger Yangfdd77702008-08-18 17:08:55 +08007100 idata->id_count = cpu_to_le16(
7101 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007102}
7103
7104int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7105 struct buffer_head *di_bh)
7106{
7107 int ret, i, has_data, num_pages = 0;
7108 handle_t *handle;
7109 u64 uninitialized_var(block);
7110 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7111 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7112 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007113 struct ocfs2_alloc_context *data_ac = NULL;
7114 struct page **pages = NULL;
7115 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07007116 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02007117 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007118
7119 has_data = i_size_read(inode) ? 1 : 0;
7120
7121 if (has_data) {
7122 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7123 sizeof(struct page *), GFP_NOFS);
7124 if (pages == NULL) {
7125 ret = -ENOMEM;
7126 mlog_errno(ret);
7127 goto out;
7128 }
7129
7130 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7131 if (ret) {
7132 mlog_errno(ret);
7133 goto out;
7134 }
7135 }
7136
Jan Karaa90714c2008-10-09 19:38:40 +02007137 handle = ocfs2_start_trans(osb,
7138 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007139 if (IS_ERR(handle)) {
7140 ret = PTR_ERR(handle);
7141 mlog_errno(ret);
7142 goto out_unlock;
7143 }
7144
Joel Becker0cf2f762009-02-12 16:41:25 -08007145 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007146 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007147 if (ret) {
7148 mlog_errno(ret);
7149 goto out_commit;
7150 }
7151
7152 if (has_data) {
7153 u32 bit_off, num;
7154 unsigned int page_end;
7155 u64 phys;
7156
Jan Karaa90714c2008-10-09 19:38:40 +02007157 if (vfs_dq_alloc_space_nodirty(inode,
7158 ocfs2_clusters_to_bytes(osb->sb, 1))) {
7159 ret = -EDQUOT;
7160 goto out_commit;
7161 }
7162 did_quota = 1;
7163
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007164 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7165 &num);
7166 if (ret) {
7167 mlog_errno(ret);
7168 goto out_commit;
7169 }
7170
7171 /*
7172 * Save two copies, one for insert, and one that can
7173 * be changed by ocfs2_map_and_dirty_page() below.
7174 */
7175 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7176
7177 /*
7178 * Non sparse file systems zero on extend, so no need
7179 * to do that now.
7180 */
7181 if (!ocfs2_sparse_alloc(osb) &&
7182 PAGE_CACHE_SIZE < osb->s_clustersize)
7183 end = PAGE_CACHE_SIZE;
7184
7185 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7186 if (ret) {
7187 mlog_errno(ret);
7188 goto out_commit;
7189 }
7190
7191 /*
7192 * This should populate the 1st page for us and mark
7193 * it up to date.
7194 */
7195 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7196 if (ret) {
7197 mlog_errno(ret);
7198 goto out_commit;
7199 }
7200
7201 page_end = PAGE_CACHE_SIZE;
7202 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7203 page_end = osb->s_clustersize;
7204
7205 for (i = 0; i < num_pages; i++)
7206 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7207 pages[i], i > 0, &phys);
7208 }
7209
7210 spin_lock(&oi->ip_lock);
7211 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7212 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7213 spin_unlock(&oi->ip_lock);
7214
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007215 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007216
7217 ocfs2_journal_dirty(handle, di_bh);
7218
7219 if (has_data) {
7220 /*
7221 * An error at this point should be extremely rare. If
7222 * this proves to be false, we could always re-build
7223 * the in-inode data from our pages.
7224 */
Joel Becker8d6220d2008-08-22 12:46:09 -07007225 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07007226 ret = ocfs2_insert_extent(osb, handle, inode, &et,
7227 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007228 if (ret) {
7229 mlog_errno(ret);
7230 goto out_commit;
7231 }
7232
7233 inode->i_blocks = ocfs2_inode_sector_count(inode);
7234 }
7235
7236out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007237 if (ret < 0 && did_quota)
7238 vfs_dq_free_space_nodirty(inode,
7239 ocfs2_clusters_to_bytes(osb->sb, 1));
7240
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007241 ocfs2_commit_trans(osb, handle);
7242
7243out_unlock:
7244 if (data_ac)
7245 ocfs2_free_alloc_context(data_ac);
7246
7247out:
7248 if (pages) {
7249 ocfs2_unlock_and_free_pages(pages, num_pages);
7250 kfree(pages);
7251 }
7252
7253 return ret;
7254}
7255
Mark Fashehccd979b2005-12-15 14:31:24 -08007256/*
7257 * It is expected, that by the time you call this function,
7258 * inode->i_size and fe->i_size have been adjusted.
7259 *
7260 * WARNING: This will kfree the truncate context
7261 */
7262int ocfs2_commit_truncate(struct ocfs2_super *osb,
7263 struct inode *inode,
7264 struct buffer_head *fe_bh,
7265 struct ocfs2_truncate_context *tc)
7266{
7267 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08007268 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08007269 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07007270 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007271 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08007272 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08007273 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007274
7275 mlog_entry_void();
7276
Mark Fashehdcd05382007-01-16 11:32:23 -08007277 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007278 i_size_read(inode));
7279
Joel Becker13723d02008-10-17 19:25:01 -07007280 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7281 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08007282 if (!path) {
7283 status = -ENOMEM;
7284 mlog_errno(status);
7285 goto bail;
7286 }
Mark Fasheh83418972007-04-23 18:53:12 -07007287
7288 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7289
Mark Fashehccd979b2005-12-15 14:31:24 -08007290start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007291 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007292 * Check that we still have allocation to delete.
7293 */
7294 if (OCFS2_I(inode)->ip_clusters == 0) {
7295 status = 0;
7296 goto bail;
7297 }
7298
7299 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007300 * Truncate always works against the rightmost tree branch.
7301 */
7302 status = ocfs2_find_path(inode, path, UINT_MAX);
7303 if (status) {
7304 mlog_errno(status);
7305 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007306 }
7307
Mark Fashehdcd05382007-01-16 11:32:23 -08007308 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7309 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7310
7311 /*
7312 * By now, el will point to the extent list on the bottom most
7313 * portion of this tree. Only the tail record is considered in
7314 * each pass.
7315 *
7316 * We handle the following cases, in order:
7317 * - empty extent: delete the remaining branch
7318 * - remove the entire record
7319 * - remove a partial record
7320 * - no record needs to be removed (truncate has completed)
7321 */
7322 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007323 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7324 ocfs2_error(inode->i_sb,
7325 "Inode %llu has empty extent block at %llu\n",
7326 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7327 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7328 status = -EROFS;
7329 goto bail;
7330 }
7331
Mark Fashehccd979b2005-12-15 14:31:24 -08007332 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08007333 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08007334 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007335 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7336 clusters_to_del = 0;
7337 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007338 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007339 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007340 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08007341 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08007342 new_highest_cpos;
7343 } else {
7344 status = 0;
7345 goto bail;
7346 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007347
Mark Fashehdcd05382007-01-16 11:32:23 -08007348 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7349 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7350
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007351 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007352 tl_sem = 1;
7353 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7354 * record is free for use. If there isn't any, we flush to get
7355 * an empty truncate log. */
7356 if (ocfs2_truncate_log_needs_flush(osb)) {
7357 status = __ocfs2_flush_truncate_log(osb);
7358 if (status < 0) {
7359 mlog_errno(status);
7360 goto bail;
7361 }
7362 }
7363
7364 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007365 (struct ocfs2_dinode *)fe_bh->b_data,
7366 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007367 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007368 if (IS_ERR(handle)) {
7369 status = PTR_ERR(handle);
7370 handle = NULL;
7371 mlog_errno(status);
7372 goto bail;
7373 }
7374
Mark Fashehdcd05382007-01-16 11:32:23 -08007375 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7376 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007377 if (status < 0) {
7378 mlog_errno(status);
7379 goto bail;
7380 }
7381
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007382 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007383 tl_sem = 0;
7384
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007385 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007386 handle = NULL;
7387
Mark Fashehdcd05382007-01-16 11:32:23 -08007388 ocfs2_reinit_path(path, 1);
7389
7390 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007391 * The check above will catch the case where we've truncated
7392 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007393 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007394 goto start;
7395
Mark Fashehccd979b2005-12-15 14:31:24 -08007396bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007397
7398 ocfs2_schedule_truncate_log_flush(osb, 1);
7399
7400 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007401 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007402
7403 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007404 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007405
Mark Fasheh59a5e412007-06-22 15:52:36 -07007406 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7407
Mark Fashehdcd05382007-01-16 11:32:23 -08007408 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007409
7410 /* This will drop the ext_alloc cluster lock for us */
7411 ocfs2_free_truncate_context(tc);
7412
7413 mlog_exit(status);
7414 return status;
7415}
7416
Mark Fashehccd979b2005-12-15 14:31:24 -08007417/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007418 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007419 */
7420int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7421 struct inode *inode,
7422 struct buffer_head *fe_bh,
7423 struct ocfs2_truncate_context **tc)
7424{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007425 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007426 unsigned int new_i_clusters;
7427 struct ocfs2_dinode *fe;
7428 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007429 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007430
7431 mlog_entry_void();
7432
7433 *tc = NULL;
7434
7435 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7436 i_size_read(inode));
7437 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7438
7439 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007440 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7441 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007442
Robert P. J. Daycd861282006-12-13 00:34:52 -08007443 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007444 if (!(*tc)) {
7445 status = -ENOMEM;
7446 mlog_errno(status);
7447 goto bail;
7448 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007449 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007450
Mark Fashehccd979b2005-12-15 14:31:24 -08007451 if (fe->id2.i_list.l_tree_depth) {
Joel Becker3d03a302009-02-12 17:49:26 -08007452 status = ocfs2_read_extent_block(INODE_CACHE(inode),
Joel Becker5e965812008-11-13 14:49:16 -08007453 le64_to_cpu(fe->i_last_eb_blk),
7454 &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007455 if (status < 0) {
7456 mlog_errno(status);
7457 goto bail;
7458 }
7459 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007460 }
7461
7462 (*tc)->tc_last_eb_bh = last_eb_bh;
7463
Mark Fashehccd979b2005-12-15 14:31:24 -08007464 status = 0;
7465bail:
7466 if (status < 0) {
7467 if (*tc)
7468 ocfs2_free_truncate_context(*tc);
7469 *tc = NULL;
7470 }
7471 mlog_exit_void();
7472 return status;
7473}
7474
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007475/*
7476 * 'start' is inclusive, 'end' is not.
7477 */
7478int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7479 unsigned int start, unsigned int end, int trunc)
7480{
7481 int ret;
7482 unsigned int numbytes;
7483 handle_t *handle;
7484 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7485 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7486 struct ocfs2_inline_data *idata = &di->id2.i_data;
7487
7488 if (end > i_size_read(inode))
7489 end = i_size_read(inode);
7490
7491 BUG_ON(start >= end);
7492
7493 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7494 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7495 !ocfs2_supports_inline_data(osb)) {
7496 ocfs2_error(inode->i_sb,
7497 "Inline data flags for inode %llu don't agree! "
7498 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7499 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7500 le16_to_cpu(di->i_dyn_features),
7501 OCFS2_I(inode)->ip_dyn_features,
7502 osb->s_feature_incompat);
7503 ret = -EROFS;
7504 goto out;
7505 }
7506
7507 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7508 if (IS_ERR(handle)) {
7509 ret = PTR_ERR(handle);
7510 mlog_errno(ret);
7511 goto out;
7512 }
7513
Joel Becker0cf2f762009-02-12 16:41:25 -08007514 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007515 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007516 if (ret) {
7517 mlog_errno(ret);
7518 goto out_commit;
7519 }
7520
7521 numbytes = end - start;
7522 memset(idata->id_data + start, 0, numbytes);
7523
7524 /*
7525 * No need to worry about the data page here - it's been
7526 * truncated already and inline data doesn't need it for
7527 * pushing zero's to disk, so we'll let readpage pick it up
7528 * later.
7529 */
7530 if (trunc) {
7531 i_size_write(inode, start);
7532 di->i_size = cpu_to_le64(start);
7533 }
7534
7535 inode->i_blocks = ocfs2_inode_sector_count(inode);
7536 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7537
7538 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7539 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7540
7541 ocfs2_journal_dirty(handle, di_bh);
7542
7543out_commit:
7544 ocfs2_commit_trans(osb, handle);
7545
7546out:
7547 return ret;
7548}
7549
Mark Fashehccd979b2005-12-15 14:31:24 -08007550static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7551{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007552 /*
7553 * The caller is responsible for completing deallocation
7554 * before freeing the context.
7555 */
7556 if (tc->tc_dealloc.c_first_suballocator != NULL)
7557 mlog(ML_NOTICE,
7558 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007559
Mark Fasheha81cb882008-10-07 14:25:16 -07007560 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007561
7562 kfree(tc);
7563}