blob: f9a3e8942669f436ad64fa00b550a46f34cd44be [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 Becker13723d02008-10-17 19:25:01 -0700355 et->et_root_journal_access = access;
Joel Beckerea5efa12008-08-20 16:57:27 -0700356 if (!obj)
357 obj = (void *)bh->b_data;
358 et->et_object = obj;
Tao Mae7d4cb62008-08-18 17:38:44 +0800359
Joel Becker0ce10102008-08-20 17:19:50 -0700360 et->et_ops->eo_fill_root_el(et);
Joel Becker943cced2008-08-20 17:31:10 -0700361 if (!et->et_ops->eo_fill_max_leaf_clusters)
362 et->et_max_leaf_clusters = 0;
363 else
364 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800365}
366
Joel Becker8d6220d2008-08-22 12:46:09 -0700367void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
368 struct inode *inode,
369 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700370{
Joel Becker13723d02008-10-17 19:25:01 -0700371 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_di,
372 NULL, &ocfs2_dinode_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700373}
374
Joel Becker8d6220d2008-08-22 12:46:09 -0700375void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700376 struct inode *inode,
Joel Becker8d6220d2008-08-22 12:46:09 -0700377 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700378{
Joel Becker13723d02008-10-17 19:25:01 -0700379 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_xb,
380 NULL, &ocfs2_xattr_tree_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700381}
382
Joel Becker8d6220d2008-08-22 12:46:09 -0700383void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
384 struct inode *inode,
Joel Becker2a50a742008-12-09 14:24:33 -0800385 struct ocfs2_xattr_value_buf *vb)
Tao Mae7d4cb62008-08-18 17:38:44 +0800386{
Joel Becker2a50a742008-12-09 14:24:33 -0800387 __ocfs2_init_extent_tree(et, inode, vb->vb_bh, vb->vb_access, vb,
Joel Becker8d6220d2008-08-22 12:46:09 -0700388 &ocfs2_xattr_value_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800389}
390
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800391void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
392 struct inode *inode,
393 struct buffer_head *bh)
394{
395 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_dr,
396 NULL, &ocfs2_dx_root_et_ops);
397}
398
Joel Becker35dc0aa2008-08-20 16:25:06 -0700399static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
400 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800401{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700402 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800403}
404
Joel Becker35dc0aa2008-08-20 16:25:06 -0700405static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800406{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700407 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800408}
409
Joel Becker35dc0aa2008-08-20 16:25:06 -0700410static inline void ocfs2_et_update_clusters(struct inode *inode,
411 struct ocfs2_extent_tree *et,
412 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800413{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700414 et->et_ops->eo_update_clusters(inode, et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700415}
416
Joel Becker13723d02008-10-17 19:25:01 -0700417static inline int ocfs2_et_root_journal_access(handle_t *handle,
418 struct inode *inode,
419 struct ocfs2_extent_tree *et,
420 int type)
421{
422 return et->et_root_journal_access(handle, inode, et->et_root_bh,
423 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,
636 struct inode *inode,
637 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
648 return access(handle, inode, path->p_node[idx].bh,
649 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 */
655static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
656 struct ocfs2_path *path)
657{
658 int i, ret = 0;
659
660 if (!path)
661 goto out;
662
663 for(i = 0; i < path_num_items(path); i++) {
Joel Becker13723d02008-10-17 19:25:01 -0700664 ret = ocfs2_path_bh_journal_access(handle, inode, path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -0800665 if (ret < 0) {
666 mlog_errno(ret);
667 goto out;
668 }
669 }
670
671out:
672 return ret;
673}
674
Mark Fasheh328d5752007-06-18 10:48:04 -0700675/*
676 * Return the index of the extent record which contains cluster #v_cluster.
677 * -1 is returned if it was not found.
678 *
679 * Should work fine on interior and exterior nodes.
680 */
681int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
682{
683 int ret = -1;
684 int i;
685 struct ocfs2_extent_rec *rec;
686 u32 rec_end, rec_start, clusters;
687
688 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
689 rec = &el->l_recs[i];
690
691 rec_start = le32_to_cpu(rec->e_cpos);
692 clusters = ocfs2_rec_clusters(el, rec);
693
694 rec_end = rec_start + clusters;
695
696 if (v_cluster >= rec_start && v_cluster < rec_end) {
697 ret = i;
698 break;
699 }
700 }
701
702 return ret;
703}
704
Mark Fashehdcd05382007-01-16 11:32:23 -0800705enum ocfs2_contig_type {
706 CONTIG_NONE = 0,
707 CONTIG_LEFT,
Mark Fasheh328d5752007-06-18 10:48:04 -0700708 CONTIG_RIGHT,
709 CONTIG_LEFTRIGHT,
Mark Fashehdcd05382007-01-16 11:32:23 -0800710};
711
Mark Fashehe48edee2007-03-07 16:46:57 -0800712
713/*
714 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
715 * ocfs2_extent_contig only work properly against leaf nodes!
716 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800717static int ocfs2_block_extent_contig(struct super_block *sb,
718 struct ocfs2_extent_rec *ext,
719 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800720{
Mark Fashehe48edee2007-03-07 16:46:57 -0800721 u64 blk_end = le64_to_cpu(ext->e_blkno);
722
723 blk_end += ocfs2_clusters_to_blocks(sb,
724 le16_to_cpu(ext->e_leaf_clusters));
725
726 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800727}
728
Mark Fashehdcd05382007-01-16 11:32:23 -0800729static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
730 struct ocfs2_extent_rec *right)
731{
Mark Fashehe48edee2007-03-07 16:46:57 -0800732 u32 left_range;
733
734 left_range = le32_to_cpu(left->e_cpos) +
735 le16_to_cpu(left->e_leaf_clusters);
736
737 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800738}
739
740static enum ocfs2_contig_type
741 ocfs2_extent_contig(struct inode *inode,
742 struct ocfs2_extent_rec *ext,
743 struct ocfs2_extent_rec *insert_rec)
744{
745 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
746
Mark Fasheh328d5752007-06-18 10:48:04 -0700747 /*
748 * Refuse to coalesce extent records with different flag
749 * fields - we don't want to mix unwritten extents with user
750 * data.
751 */
752 if (ext->e_flags != insert_rec->e_flags)
753 return CONTIG_NONE;
754
Mark Fashehdcd05382007-01-16 11:32:23 -0800755 if (ocfs2_extents_adjacent(ext, insert_rec) &&
756 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
757 return CONTIG_RIGHT;
758
759 blkno = le64_to_cpu(ext->e_blkno);
760 if (ocfs2_extents_adjacent(insert_rec, ext) &&
761 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
762 return CONTIG_LEFT;
763
764 return CONTIG_NONE;
765}
766
767/*
768 * NOTE: We can have pretty much any combination of contiguousness and
769 * appending.
770 *
771 * The usefulness of APPEND_TAIL is more in that it lets us know that
772 * we'll have to update the path to that leaf.
773 */
774enum ocfs2_append_type {
775 APPEND_NONE = 0,
776 APPEND_TAIL,
777};
778
Mark Fasheh328d5752007-06-18 10:48:04 -0700779enum ocfs2_split_type {
780 SPLIT_NONE = 0,
781 SPLIT_LEFT,
782 SPLIT_RIGHT,
783};
784
Mark Fashehdcd05382007-01-16 11:32:23 -0800785struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700786 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800787 enum ocfs2_append_type ins_appending;
788 enum ocfs2_contig_type ins_contig;
789 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800790 int ins_tree_depth;
791};
792
Mark Fasheh328d5752007-06-18 10:48:04 -0700793struct ocfs2_merge_ctxt {
794 enum ocfs2_contig_type c_contig_type;
795 int c_has_empty_extent;
796 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700797};
798
Joel Becker5e965812008-11-13 14:49:16 -0800799static int ocfs2_validate_extent_block(struct super_block *sb,
800 struct buffer_head *bh)
801{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700802 int rc;
Joel Becker5e965812008-11-13 14:49:16 -0800803 struct ocfs2_extent_block *eb =
804 (struct ocfs2_extent_block *)bh->b_data;
805
Joel Becker970e4932008-11-13 14:49:19 -0800806 mlog(0, "Validating extent block %llu\n",
807 (unsigned long long)bh->b_blocknr);
808
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700809 BUG_ON(!buffer_uptodate(bh));
810
811 /*
812 * If the ecc fails, we return the error but otherwise
813 * leave the filesystem running. We know any error is
814 * local to this block.
815 */
816 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
Joel Becker13723d02008-10-17 19:25:01 -0700817 if (rc) {
818 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
819 (unsigned long long)bh->b_blocknr);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700820 return rc;
Joel Becker13723d02008-10-17 19:25:01 -0700821 }
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700822
823 /*
824 * Errors after here are fatal.
825 */
826
Joel Becker5e965812008-11-13 14:49:16 -0800827 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
828 ocfs2_error(sb,
829 "Extent block #%llu has bad signature %.*s",
830 (unsigned long long)bh->b_blocknr, 7,
831 eb->h_signature);
832 return -EINVAL;
833 }
834
835 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
836 ocfs2_error(sb,
837 "Extent block #%llu has an invalid h_blkno "
838 "of %llu",
839 (unsigned long long)bh->b_blocknr,
840 (unsigned long long)le64_to_cpu(eb->h_blkno));
841 return -EINVAL;
842 }
843
844 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
845 ocfs2_error(sb,
846 "Extent block #%llu has an invalid "
847 "h_fs_generation of #%u",
848 (unsigned long long)bh->b_blocknr,
849 le32_to_cpu(eb->h_fs_generation));
850 return -EINVAL;
851 }
852
853 return 0;
854}
855
856int ocfs2_read_extent_block(struct inode *inode, u64 eb_blkno,
857 struct buffer_head **bh)
858{
859 int rc;
860 struct buffer_head *tmp = *bh;
861
Joel Becker970e4932008-11-13 14:49:19 -0800862 rc = ocfs2_read_block(inode, eb_blkno, &tmp,
863 ocfs2_validate_extent_block);
Joel Becker5e965812008-11-13 14:49:16 -0800864
865 /* If ocfs2_read_block() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -0800866 if (!rc && !*bh)
Joel Becker5e965812008-11-13 14:49:16 -0800867 *bh = tmp;
868
Joel Becker5e965812008-11-13 14:49:16 -0800869 return rc;
870}
871
872
Mark Fashehccd979b2005-12-15 14:31:24 -0800873/*
874 * How many free extents have we got before we need more meta data?
875 */
876int ocfs2_num_free_extents(struct ocfs2_super *osb,
877 struct inode *inode,
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 Becker5e965812008-11-13 14:49:16 -0800892 retval = ocfs2_read_extent_block(inode, last_eb_blk, &eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800893 if (retval < 0) {
894 mlog_errno(retval);
895 goto bail;
896 }
897 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
898 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800899 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800900
901 BUG_ON(el->l_tree_depth != 0);
902
903 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
904bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700905 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800906
907 mlog_exit(retval);
908 return retval;
909}
910
911/* expects array to already be allocated
912 *
913 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
914 * l_count for you
915 */
916static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700917 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800918 struct inode *inode,
919 int wanted,
920 struct ocfs2_alloc_context *meta_ac,
921 struct buffer_head *bhs[])
922{
923 int count, status, i;
924 u16 suballoc_bit_start;
925 u32 num_got;
926 u64 first_blkno;
927 struct ocfs2_extent_block *eb;
928
929 mlog_entry_void();
930
931 count = 0;
932 while (count < wanted) {
933 status = ocfs2_claim_metadata(osb,
934 handle,
935 meta_ac,
936 wanted - count,
937 &suballoc_bit_start,
938 &num_got,
939 &first_blkno);
940 if (status < 0) {
941 mlog_errno(status);
942 goto bail;
943 }
944
945 for(i = count; i < (num_got + count); i++) {
946 bhs[i] = sb_getblk(osb->sb, first_blkno);
947 if (bhs[i] == NULL) {
948 status = -EIO;
949 mlog_errno(status);
950 goto bail;
951 }
952 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
953
Joel Becker13723d02008-10-17 19:25:01 -0700954 status = ocfs2_journal_access_eb(handle, inode, bhs[i],
955 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800956 if (status < 0) {
957 mlog_errno(status);
958 goto bail;
959 }
960
961 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
962 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
963 /* Ok, setup the minimal stuff here. */
964 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
965 eb->h_blkno = cpu_to_le64(first_blkno);
966 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800967 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800968 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
969 eb->h_list.l_count =
970 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
971
972 suballoc_bit_start++;
973 first_blkno++;
974
975 /* We'll also be dirtied by the caller, so
976 * this isn't absolutely necessary. */
977 status = ocfs2_journal_dirty(handle, bhs[i]);
978 if (status < 0) {
979 mlog_errno(status);
980 goto bail;
981 }
982 }
983
984 count += num_got;
985 }
986
987 status = 0;
988bail:
989 if (status < 0) {
990 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -0700991 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800992 bhs[i] = NULL;
993 }
994 }
995 mlog_exit(status);
996 return status;
997}
998
999/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001000 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1001 *
1002 * Returns the sum of the rightmost extent rec logical offset and
1003 * cluster count.
1004 *
1005 * ocfs2_add_branch() uses this to determine what logical cluster
1006 * value should be populated into the leftmost new branch records.
1007 *
1008 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1009 * value for the new topmost tree record.
1010 */
1011static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1012{
1013 int i;
1014
1015 i = le16_to_cpu(el->l_next_free_rec) - 1;
1016
1017 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001018 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08001019}
1020
1021/*
Tao Ma6b791bc2009-06-12 14:18:36 +08001022 * Change range of the branches in the right most path according to the leaf
1023 * extent block's rightmost record.
1024 */
1025static int ocfs2_adjust_rightmost_branch(handle_t *handle,
1026 struct inode *inode,
1027 struct ocfs2_extent_tree *et)
1028{
1029 int status;
1030 struct ocfs2_path *path = NULL;
1031 struct ocfs2_extent_list *el;
1032 struct ocfs2_extent_rec *rec;
1033
1034 path = ocfs2_new_path_from_et(et);
1035 if (!path) {
1036 status = -ENOMEM;
1037 return status;
1038 }
1039
1040 status = ocfs2_find_path(inode, path, UINT_MAX);
1041 if (status < 0) {
1042 mlog_errno(status);
1043 goto out;
1044 }
1045
1046 status = ocfs2_extend_trans(handle, path_num_items(path) +
1047 handle->h_buffer_credits);
1048 if (status < 0) {
1049 mlog_errno(status);
1050 goto out;
1051 }
1052
1053 status = ocfs2_journal_access_path(inode, handle, path);
1054 if (status < 0) {
1055 mlog_errno(status);
1056 goto out;
1057 }
1058
1059 el = path_leaf_el(path);
1060 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1061
1062 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
1063
1064out:
1065 ocfs2_free_path(path);
1066 return status;
1067}
1068
1069/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001070 * Add an entire tree branch to our inode. eb_bh is the extent block
1071 * to start at, if we don't want to start the branch at the dinode
1072 * structure.
1073 *
1074 * last_eb_bh is required as we have to update it's next_leaf pointer
1075 * for the new last extent block.
1076 *
1077 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -08001078 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -08001079 */
1080static int ocfs2_add_branch(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001081 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001082 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001083 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001084 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07001085 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001086 struct ocfs2_alloc_context *meta_ac)
1087{
1088 int status, new_blocks, i;
1089 u64 next_blkno, new_last_eb_blk;
1090 struct buffer_head *bh;
1091 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001092 struct ocfs2_extent_block *eb;
1093 struct ocfs2_extent_list *eb_el;
1094 struct ocfs2_extent_list *el;
Tao Ma6b791bc2009-06-12 14:18:36 +08001095 u32 new_cpos, root_end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001096
1097 mlog_entry_void();
1098
Mark Fasheh328d5752007-06-18 10:48:04 -07001099 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001100
Mark Fashehccd979b2005-12-15 14:31:24 -08001101 if (eb_bh) {
1102 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1103 el = &eb->h_list;
1104 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001105 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001106
1107 /* we never add a branch to a leaf. */
1108 BUG_ON(!el->l_tree_depth);
1109
1110 new_blocks = le16_to_cpu(el->l_tree_depth);
1111
Tao Ma6b791bc2009-06-12 14:18:36 +08001112 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1113 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1114 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1115
1116 /*
1117 * If there is a gap before the root end and the real end
1118 * of the righmost leaf block, we need to remove the gap
1119 * between new_cpos and root_end first so that the tree
1120 * is consistent after we add a new branch(it will start
1121 * from new_cpos).
1122 */
1123 if (root_end > new_cpos) {
1124 mlog(0, "adjust the cluster end from %u to %u\n",
1125 root_end, new_cpos);
1126 status = ocfs2_adjust_rightmost_branch(handle, inode, et);
1127 if (status) {
1128 mlog_errno(status);
1129 goto bail;
1130 }
1131 }
1132
Mark Fashehccd979b2005-12-15 14:31:24 -08001133 /* allocate the number of new eb blocks we need */
1134 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1135 GFP_KERNEL);
1136 if (!new_eb_bhs) {
1137 status = -ENOMEM;
1138 mlog_errno(status);
1139 goto bail;
1140 }
1141
1142 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
1143 meta_ac, new_eb_bhs);
1144 if (status < 0) {
1145 mlog_errno(status);
1146 goto bail;
1147 }
1148
1149 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1150 * linked with the rest of the tree.
1151 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1152 *
1153 * when we leave the loop, new_last_eb_blk will point to the
1154 * newest leaf, and next_blkno will point to the topmost extent
1155 * block. */
1156 next_blkno = new_last_eb_blk = 0;
1157 for(i = 0; i < new_blocks; i++) {
1158 bh = new_eb_bhs[i];
1159 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001160 /* ocfs2_create_new_meta_bhs() should create it right! */
1161 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001162 eb_el = &eb->h_list;
1163
Joel Becker13723d02008-10-17 19:25:01 -07001164 status = ocfs2_journal_access_eb(handle, inode, bh,
1165 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001166 if (status < 0) {
1167 mlog_errno(status);
1168 goto bail;
1169 }
1170
1171 eb->h_next_leaf_blk = 0;
1172 eb_el->l_tree_depth = cpu_to_le16(i);
1173 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001174 /*
1175 * This actually counts as an empty extent as
1176 * c_clusters == 0
1177 */
1178 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001179 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001180 /*
1181 * eb_el isn't always an interior node, but even leaf
1182 * nodes want a zero'd flags and reserved field so
1183 * this gets the whole 32 bits regardless of use.
1184 */
1185 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001186 if (!eb_el->l_tree_depth)
1187 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1188
1189 status = ocfs2_journal_dirty(handle, bh);
1190 if (status < 0) {
1191 mlog_errno(status);
1192 goto bail;
1193 }
1194
1195 next_blkno = le64_to_cpu(eb->h_blkno);
1196 }
1197
1198 /* This is a bit hairy. We want to update up to three blocks
1199 * here without leaving any of them in an inconsistent state
1200 * in case of error. We don't have to worry about
1201 * journal_dirty erroring as it won't unless we've aborted the
1202 * handle (in which case we would never be here) so reserving
1203 * the write with journal_access is all we need to do. */
Joel Becker13723d02008-10-17 19:25:01 -07001204 status = ocfs2_journal_access_eb(handle, inode, *last_eb_bh,
1205 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001206 if (status < 0) {
1207 mlog_errno(status);
1208 goto bail;
1209 }
Joel Becker13723d02008-10-17 19:25:01 -07001210 status = ocfs2_et_root_journal_access(handle, inode, et,
1211 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001212 if (status < 0) {
1213 mlog_errno(status);
1214 goto bail;
1215 }
1216 if (eb_bh) {
Joel Becker13723d02008-10-17 19:25:01 -07001217 status = ocfs2_journal_access_eb(handle, inode, eb_bh,
1218 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001219 if (status < 0) {
1220 mlog_errno(status);
1221 goto bail;
1222 }
1223 }
1224
1225 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001226 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001227 i = le16_to_cpu(el->l_next_free_rec);
1228 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001229 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001230 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001231 le16_add_cpu(&el->l_next_free_rec, 1);
1232
1233 /* fe needs a new last extent block pointer, as does the
1234 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001235 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001236
Mark Fasheh328d5752007-06-18 10:48:04 -07001237 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001238 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1239
Mark Fasheh328d5752007-06-18 10:48:04 -07001240 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001241 if (status < 0)
1242 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001243 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001244 if (status < 0)
1245 mlog_errno(status);
1246 if (eb_bh) {
1247 status = ocfs2_journal_dirty(handle, eb_bh);
1248 if (status < 0)
1249 mlog_errno(status);
1250 }
1251
Mark Fasheh328d5752007-06-18 10:48:04 -07001252 /*
1253 * Some callers want to track the rightmost leaf so pass it
1254 * back here.
1255 */
1256 brelse(*last_eb_bh);
1257 get_bh(new_eb_bhs[0]);
1258 *last_eb_bh = new_eb_bhs[0];
1259
Mark Fashehccd979b2005-12-15 14:31:24 -08001260 status = 0;
1261bail:
1262 if (new_eb_bhs) {
1263 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001264 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001265 kfree(new_eb_bhs);
1266 }
1267
1268 mlog_exit(status);
1269 return status;
1270}
1271
1272/*
1273 * adds another level to the allocation tree.
1274 * returns back the new extent block so you can add a branch to it
1275 * after this call.
1276 */
1277static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001278 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001279 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001280 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001281 struct ocfs2_alloc_context *meta_ac,
1282 struct buffer_head **ret_new_eb_bh)
1283{
1284 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001285 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001286 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001287 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001288 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001289 struct ocfs2_extent_list *eb_el;
1290
1291 mlog_entry_void();
1292
1293 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
1294 &new_eb_bh);
1295 if (status < 0) {
1296 mlog_errno(status);
1297 goto bail;
1298 }
1299
1300 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001301 /* ocfs2_create_new_meta_bhs() should create it right! */
1302 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001303
1304 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001305 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001306
Joel Becker13723d02008-10-17 19:25:01 -07001307 status = ocfs2_journal_access_eb(handle, inode, new_eb_bh,
1308 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001309 if (status < 0) {
1310 mlog_errno(status);
1311 goto bail;
1312 }
1313
Tao Mae7d4cb62008-08-18 17:38:44 +08001314 /* copy the root extent list data into the new extent block */
1315 eb_el->l_tree_depth = root_el->l_tree_depth;
1316 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1317 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1318 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001319
1320 status = ocfs2_journal_dirty(handle, new_eb_bh);
1321 if (status < 0) {
1322 mlog_errno(status);
1323 goto bail;
1324 }
1325
Joel Becker13723d02008-10-17 19:25:01 -07001326 status = ocfs2_et_root_journal_access(handle, inode, et,
1327 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001328 if (status < 0) {
1329 mlog_errno(status);
1330 goto bail;
1331 }
1332
Mark Fashehdcd05382007-01-16 11:32:23 -08001333 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1334
Tao Mae7d4cb62008-08-18 17:38:44 +08001335 /* update root_bh now */
1336 le16_add_cpu(&root_el->l_tree_depth, 1);
1337 root_el->l_recs[0].e_cpos = 0;
1338 root_el->l_recs[0].e_blkno = eb->h_blkno;
1339 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1340 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1341 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1342 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001343
1344 /* If this is our 1st tree depth shift, then last_eb_blk
1345 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001346 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001347 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001348
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001349 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001350 if (status < 0) {
1351 mlog_errno(status);
1352 goto bail;
1353 }
1354
1355 *ret_new_eb_bh = new_eb_bh;
1356 new_eb_bh = NULL;
1357 status = 0;
1358bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001359 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001360
1361 mlog_exit(status);
1362 return status;
1363}
1364
1365/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001366 * Should only be called when there is no space left in any of the
1367 * leaf nodes. What we want to do is find the lowest tree depth
1368 * non-leaf extent block with room for new records. There are three
1369 * valid results of this search:
1370 *
1371 * 1) a lowest extent block is found, then we pass it back in
1372 * *lowest_eb_bh and return '0'
1373 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001374 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001375 * pass NULL back in *lowest_eb_bh, but still return '0'
1376 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001377 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001378 * which case we return > 0
1379 *
1380 * return status < 0 indicates an error.
1381 */
1382static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1383 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001384 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001385 struct buffer_head **target_bh)
1386{
1387 int status = 0, i;
1388 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001389 struct ocfs2_extent_block *eb;
1390 struct ocfs2_extent_list *el;
1391 struct buffer_head *bh = NULL;
1392 struct buffer_head *lowest_bh = NULL;
1393
1394 mlog_entry_void();
1395
1396 *target_bh = NULL;
1397
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001398 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001399
1400 while(le16_to_cpu(el->l_tree_depth) > 1) {
1401 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Mark Fashehb06970532006-03-03 10:24:33 -08001402 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001403 "extent list (next_free_rec == 0)",
Mark Fashehb06970532006-03-03 10:24:33 -08001404 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001405 status = -EIO;
1406 goto bail;
1407 }
1408 i = le16_to_cpu(el->l_next_free_rec) - 1;
1409 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1410 if (!blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -08001411 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001412 "list where extent # %d has no physical "
1413 "block start",
Mark Fashehb06970532006-03-03 10:24:33 -08001414 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001415 status = -EIO;
1416 goto bail;
1417 }
1418
Mark Fasheha81cb882008-10-07 14:25:16 -07001419 brelse(bh);
1420 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001421
Joel Becker5e965812008-11-13 14:49:16 -08001422 status = ocfs2_read_extent_block(inode, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001423 if (status < 0) {
1424 mlog_errno(status);
1425 goto bail;
1426 }
1427
1428 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001429 el = &eb->h_list;
1430
1431 if (le16_to_cpu(el->l_next_free_rec) <
1432 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001433 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001434 lowest_bh = bh;
1435 get_bh(lowest_bh);
1436 }
1437 }
1438
1439 /* If we didn't find one and the fe doesn't have any room,
1440 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001441 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001442 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001443 status = 1;
1444
1445 *target_bh = lowest_bh;
1446bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001447 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001448
1449 mlog_exit(status);
1450 return status;
1451}
1452
Mark Fashehe48edee2007-03-07 16:46:57 -08001453/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001454 * Grow a b-tree so that it has more records.
1455 *
1456 * We might shift the tree depth in which case existing paths should
1457 * be considered invalid.
1458 *
1459 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001460 *
1461 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001462 */
1463static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001464 struct ocfs2_extent_tree *et, int *final_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07001465 struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001466 struct ocfs2_alloc_context *meta_ac)
1467{
1468 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001469 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001470 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001471 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1472 struct buffer_head *bh = NULL;
1473
1474 BUG_ON(meta_ac == NULL);
1475
Tao Mae7d4cb62008-08-18 17:38:44 +08001476 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001477 if (shift < 0) {
1478 ret = shift;
1479 mlog_errno(ret);
1480 goto out;
1481 }
1482
1483 /* We traveled all the way to the bottom of the allocation tree
1484 * and didn't find room for any more extents - we need to add
1485 * another tree level */
1486 if (shift) {
1487 BUG_ON(bh);
1488 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1489
1490 /* ocfs2_shift_tree_depth will return us a buffer with
1491 * the new extent block (so we can pass that to
1492 * ocfs2_add_branch). */
Tao Mae7d4cb62008-08-18 17:38:44 +08001493 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001494 meta_ac, &bh);
1495 if (ret < 0) {
1496 mlog_errno(ret);
1497 goto out;
1498 }
1499 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001500 if (depth == 1) {
1501 /*
1502 * Special case: we have room now if we shifted from
1503 * tree_depth 0, so no more work needs to be done.
1504 *
1505 * We won't be calling add_branch, so pass
1506 * back *last_eb_bh as the new leaf. At depth
1507 * zero, it should always be null so there's
1508 * no reason to brelse.
1509 */
1510 BUG_ON(*last_eb_bh);
1511 get_bh(bh);
1512 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001513 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001514 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001515 }
1516
1517 /* call ocfs2_add_branch to add the final part of the tree with
1518 * the new data. */
1519 mlog(0, "add branch. bh = %p\n", bh);
Tao Mae7d4cb62008-08-18 17:38:44 +08001520 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001521 meta_ac);
1522 if (ret < 0) {
1523 mlog_errno(ret);
1524 goto out;
1525 }
1526
1527out:
1528 if (final_depth)
1529 *final_depth = depth;
1530 brelse(bh);
1531 return ret;
1532}
1533
1534/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001535 * This function will discard the rightmost extent record.
1536 */
1537static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1538{
1539 int next_free = le16_to_cpu(el->l_next_free_rec);
1540 int count = le16_to_cpu(el->l_count);
1541 unsigned int num_bytes;
1542
1543 BUG_ON(!next_free);
1544 /* This will cause us to go off the end of our extent list. */
1545 BUG_ON(next_free >= count);
1546
1547 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1548
1549 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1550}
1551
1552static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1553 struct ocfs2_extent_rec *insert_rec)
1554{
1555 int i, insert_index, next_free, has_empty, num_bytes;
1556 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1557 struct ocfs2_extent_rec *rec;
1558
1559 next_free = le16_to_cpu(el->l_next_free_rec);
1560 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1561
1562 BUG_ON(!next_free);
1563
1564 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001565 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001566
1567 /*
1568 * The easiest way to approach this is to just remove the
1569 * empty extent and temporarily decrement next_free.
1570 */
1571 if (has_empty) {
1572 /*
1573 * If next_free was 1 (only an empty extent), this
1574 * loop won't execute, which is fine. We still want
1575 * the decrement above to happen.
1576 */
1577 for(i = 0; i < (next_free - 1); i++)
1578 el->l_recs[i] = el->l_recs[i+1];
1579
1580 next_free--;
1581 }
1582
1583 /*
1584 * Figure out what the new record index should be.
1585 */
1586 for(i = 0; i < next_free; i++) {
1587 rec = &el->l_recs[i];
1588
1589 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1590 break;
1591 }
1592 insert_index = i;
1593
1594 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1595 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1596
1597 BUG_ON(insert_index < 0);
1598 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1599 BUG_ON(insert_index > next_free);
1600
1601 /*
1602 * No need to memmove if we're just adding to the tail.
1603 */
1604 if (insert_index != next_free) {
1605 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1606
1607 num_bytes = next_free - insert_index;
1608 num_bytes *= sizeof(struct ocfs2_extent_rec);
1609 memmove(&el->l_recs[insert_index + 1],
1610 &el->l_recs[insert_index],
1611 num_bytes);
1612 }
1613
1614 /*
1615 * Either we had an empty extent, and need to re-increment or
1616 * there was no empty extent on a non full rightmost leaf node,
1617 * in which case we still need to increment.
1618 */
1619 next_free++;
1620 el->l_next_free_rec = cpu_to_le16(next_free);
1621 /*
1622 * Make sure none of the math above just messed up our tree.
1623 */
1624 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1625
1626 el->l_recs[insert_index] = *insert_rec;
1627
1628}
1629
Mark Fasheh328d5752007-06-18 10:48:04 -07001630static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1631{
1632 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1633
1634 BUG_ON(num_recs == 0);
1635
1636 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1637 num_recs--;
1638 size = num_recs * sizeof(struct ocfs2_extent_rec);
1639 memmove(&el->l_recs[0], &el->l_recs[1], size);
1640 memset(&el->l_recs[num_recs], 0,
1641 sizeof(struct ocfs2_extent_rec));
1642 el->l_next_free_rec = cpu_to_le16(num_recs);
1643 }
1644}
1645
Mark Fashehdcd05382007-01-16 11:32:23 -08001646/*
1647 * Create an empty extent record .
1648 *
1649 * l_next_free_rec may be updated.
1650 *
1651 * If an empty extent already exists do nothing.
1652 */
1653static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1654{
1655 int next_free = le16_to_cpu(el->l_next_free_rec);
1656
Mark Fashehe48edee2007-03-07 16:46:57 -08001657 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1658
Mark Fashehdcd05382007-01-16 11:32:23 -08001659 if (next_free == 0)
1660 goto set_and_inc;
1661
1662 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1663 return;
1664
1665 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1666 "Asked to create an empty extent in a full list:\n"
1667 "count = %u, tree depth = %u",
1668 le16_to_cpu(el->l_count),
1669 le16_to_cpu(el->l_tree_depth));
1670
1671 ocfs2_shift_records_right(el);
1672
1673set_and_inc:
1674 le16_add_cpu(&el->l_next_free_rec, 1);
1675 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1676}
1677
1678/*
1679 * For a rotation which involves two leaf nodes, the "root node" is
1680 * the lowest level tree node which contains a path to both leafs. This
1681 * resulting set of information can be used to form a complete "subtree"
1682 *
1683 * This function is passed two full paths from the dinode down to a
1684 * pair of adjacent leaves. It's task is to figure out which path
1685 * index contains the subtree root - this can be the root index itself
1686 * in a worst-case rotation.
1687 *
1688 * The array index of the subtree root is passed back.
1689 */
1690static int ocfs2_find_subtree_root(struct inode *inode,
1691 struct ocfs2_path *left,
1692 struct ocfs2_path *right)
1693{
1694 int i = 0;
1695
1696 /*
1697 * Check that the caller passed in two paths from the same tree.
1698 */
1699 BUG_ON(path_root_bh(left) != path_root_bh(right));
1700
1701 do {
1702 i++;
1703
1704 /*
1705 * The caller didn't pass two adjacent paths.
1706 */
1707 mlog_bug_on_msg(i > left->p_tree_depth,
1708 "Inode %lu, left depth %u, right depth %u\n"
1709 "left leaf blk %llu, right leaf blk %llu\n",
1710 inode->i_ino, left->p_tree_depth,
1711 right->p_tree_depth,
1712 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1713 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1714 } while (left->p_node[i].bh->b_blocknr ==
1715 right->p_node[i].bh->b_blocknr);
1716
1717 return i - 1;
1718}
1719
1720typedef void (path_insert_t)(void *, struct buffer_head *);
1721
1722/*
1723 * Traverse a btree path in search of cpos, starting at root_el.
1724 *
1725 * This code can be called with a cpos larger than the tree, in which
1726 * case it will return the rightmost path.
1727 */
1728static int __ocfs2_find_path(struct inode *inode,
1729 struct ocfs2_extent_list *root_el, u32 cpos,
1730 path_insert_t *func, void *data)
1731{
1732 int i, ret = 0;
1733 u32 range;
1734 u64 blkno;
1735 struct buffer_head *bh = NULL;
1736 struct ocfs2_extent_block *eb;
1737 struct ocfs2_extent_list *el;
1738 struct ocfs2_extent_rec *rec;
1739 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1740
1741 el = root_el;
1742 while (el->l_tree_depth) {
1743 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1744 ocfs2_error(inode->i_sb,
1745 "Inode %llu has empty extent list at "
1746 "depth %u\n",
1747 (unsigned long long)oi->ip_blkno,
1748 le16_to_cpu(el->l_tree_depth));
1749 ret = -EROFS;
1750 goto out;
1751
1752 }
1753
1754 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1755 rec = &el->l_recs[i];
1756
1757 /*
1758 * In the case that cpos is off the allocation
1759 * tree, this should just wind up returning the
1760 * rightmost record.
1761 */
1762 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001763 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001764 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1765 break;
1766 }
1767
1768 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1769 if (blkno == 0) {
1770 ocfs2_error(inode->i_sb,
1771 "Inode %llu has bad blkno in extent list "
1772 "at depth %u (index %d)\n",
1773 (unsigned long long)oi->ip_blkno,
1774 le16_to_cpu(el->l_tree_depth), i);
1775 ret = -EROFS;
1776 goto out;
1777 }
1778
1779 brelse(bh);
1780 bh = NULL;
Joel Becker5e965812008-11-13 14:49:16 -08001781 ret = ocfs2_read_extent_block(inode, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001782 if (ret) {
1783 mlog_errno(ret);
1784 goto out;
1785 }
1786
1787 eb = (struct ocfs2_extent_block *) bh->b_data;
1788 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001789
1790 if (le16_to_cpu(el->l_next_free_rec) >
1791 le16_to_cpu(el->l_count)) {
1792 ocfs2_error(inode->i_sb,
1793 "Inode %llu has bad count in extent list "
1794 "at block %llu (next free=%u, count=%u)\n",
1795 (unsigned long long)oi->ip_blkno,
1796 (unsigned long long)bh->b_blocknr,
1797 le16_to_cpu(el->l_next_free_rec),
1798 le16_to_cpu(el->l_count));
1799 ret = -EROFS;
1800 goto out;
1801 }
1802
1803 if (func)
1804 func(data, bh);
1805 }
1806
1807out:
1808 /*
1809 * Catch any trailing bh that the loop didn't handle.
1810 */
1811 brelse(bh);
1812
1813 return ret;
1814}
1815
1816/*
1817 * Given an initialized path (that is, it has a valid root extent
1818 * list), this function will traverse the btree in search of the path
1819 * which would contain cpos.
1820 *
1821 * The path traveled is recorded in the path structure.
1822 *
1823 * Note that this will not do any comparisons on leaf node extent
1824 * records, so it will work fine in the case that we just added a tree
1825 * branch.
1826 */
1827struct find_path_data {
1828 int index;
1829 struct ocfs2_path *path;
1830};
1831static void find_path_ins(void *data, struct buffer_head *bh)
1832{
1833 struct find_path_data *fp = data;
1834
1835 get_bh(bh);
1836 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1837 fp->index++;
1838}
1839static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1840 u32 cpos)
1841{
1842 struct find_path_data data;
1843
1844 data.index = 1;
1845 data.path = path;
1846 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1847 find_path_ins, &data);
1848}
1849
1850static void find_leaf_ins(void *data, struct buffer_head *bh)
1851{
1852 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1853 struct ocfs2_extent_list *el = &eb->h_list;
1854 struct buffer_head **ret = data;
1855
1856 /* We want to retain only the leaf block. */
1857 if (le16_to_cpu(el->l_tree_depth) == 0) {
1858 get_bh(bh);
1859 *ret = bh;
1860 }
1861}
1862/*
1863 * Find the leaf block in the tree which would contain cpos. No
1864 * checking of the actual leaf is done.
1865 *
1866 * Some paths want to call this instead of allocating a path structure
1867 * and calling ocfs2_find_path().
1868 *
1869 * This function doesn't handle non btree extent lists.
1870 */
Mark Fasheh363041a2007-01-17 12:31:35 -08001871int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1872 u32 cpos, struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001873{
1874 int ret;
1875 struct buffer_head *bh = NULL;
1876
1877 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1878 if (ret) {
1879 mlog_errno(ret);
1880 goto out;
1881 }
1882
1883 *leaf_bh = bh;
1884out:
1885 return ret;
1886}
1887
1888/*
1889 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1890 *
1891 * Basically, we've moved stuff around at the bottom of the tree and
1892 * we need to fix up the extent records above the changes to reflect
1893 * the new changes.
1894 *
1895 * left_rec: the record on the left.
1896 * left_child_el: is the child list pointed to by left_rec
1897 * right_rec: the record to the right of left_rec
1898 * right_child_el: is the child list pointed to by right_rec
1899 *
1900 * By definition, this only works on interior nodes.
1901 */
1902static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1903 struct ocfs2_extent_list *left_child_el,
1904 struct ocfs2_extent_rec *right_rec,
1905 struct ocfs2_extent_list *right_child_el)
1906{
1907 u32 left_clusters, right_end;
1908
1909 /*
1910 * Interior nodes never have holes. Their cpos is the cpos of
1911 * the leftmost record in their child list. Their cluster
1912 * count covers the full theoretical range of their child list
1913 * - the range between their cpos and the cpos of the record
1914 * immediately to their right.
1915 */
1916 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08001917 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1918 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07001919 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1920 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1921 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001922 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001923 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001924
1925 /*
1926 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001927 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001928 * updating e_cpos to keep the same highest cluster count.
1929 */
1930 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001931 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001932
1933 right_rec->e_cpos = left_rec->e_cpos;
1934 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1935
1936 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001937 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001938}
1939
1940/*
1941 * Adjust the adjacent root node records involved in a
1942 * rotation. left_el_blkno is passed in as a key so that we can easily
1943 * find it's index in the root list.
1944 */
1945static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1946 struct ocfs2_extent_list *left_el,
1947 struct ocfs2_extent_list *right_el,
1948 u64 left_el_blkno)
1949{
1950 int i;
1951
1952 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1953 le16_to_cpu(left_el->l_tree_depth));
1954
1955 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1956 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1957 break;
1958 }
1959
1960 /*
1961 * The path walking code should have never returned a root and
1962 * two paths which are not adjacent.
1963 */
1964 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1965
1966 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1967 &root_el->l_recs[i + 1], right_el);
1968}
1969
1970/*
1971 * We've changed a leaf block (in right_path) and need to reflect that
1972 * change back up the subtree.
1973 *
1974 * This happens in multiple places:
1975 * - When we've moved an extent record from the left path leaf to the right
1976 * path leaf to make room for an empty extent in the left path leaf.
1977 * - When our insert into the right path leaf is at the leftmost edge
1978 * and requires an update of the path immediately to it's left. This
1979 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08001980 * - When we've adjusted the last extent record in the left path leaf and the
1981 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08001982 */
1983static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1984 struct ocfs2_path *left_path,
1985 struct ocfs2_path *right_path,
1986 int subtree_index)
1987{
1988 int ret, i, idx;
1989 struct ocfs2_extent_list *el, *left_el, *right_el;
1990 struct ocfs2_extent_rec *left_rec, *right_rec;
1991 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1992
1993 /*
1994 * Update the counts and position values within all the
1995 * interior nodes to reflect the leaf rotation we just did.
1996 *
1997 * The root node is handled below the loop.
1998 *
1999 * We begin the loop with right_el and left_el pointing to the
2000 * leaf lists and work our way up.
2001 *
2002 * NOTE: within this loop, left_el and right_el always refer
2003 * to the *child* lists.
2004 */
2005 left_el = path_leaf_el(left_path);
2006 right_el = path_leaf_el(right_path);
2007 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2008 mlog(0, "Adjust records at index %u\n", i);
2009
2010 /*
2011 * One nice property of knowing that all of these
2012 * nodes are below the root is that we only deal with
2013 * the leftmost right node record and the rightmost
2014 * left node record.
2015 */
2016 el = left_path->p_node[i].el;
2017 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2018 left_rec = &el->l_recs[idx];
2019
2020 el = right_path->p_node[i].el;
2021 right_rec = &el->l_recs[0];
2022
2023 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2024 right_el);
2025
2026 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2027 if (ret)
2028 mlog_errno(ret);
2029
2030 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2031 if (ret)
2032 mlog_errno(ret);
2033
2034 /*
2035 * Setup our list pointers now so that the current
2036 * parents become children in the next iteration.
2037 */
2038 left_el = left_path->p_node[i].el;
2039 right_el = right_path->p_node[i].el;
2040 }
2041
2042 /*
2043 * At the root node, adjust the two adjacent records which
2044 * begin our path to the leaves.
2045 */
2046
2047 el = left_path->p_node[subtree_index].el;
2048 left_el = left_path->p_node[subtree_index + 1].el;
2049 right_el = right_path->p_node[subtree_index + 1].el;
2050
2051 ocfs2_adjust_root_records(el, left_el, right_el,
2052 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2053
2054 root_bh = left_path->p_node[subtree_index].bh;
2055
2056 ret = ocfs2_journal_dirty(handle, root_bh);
2057 if (ret)
2058 mlog_errno(ret);
2059}
2060
2061static int ocfs2_rotate_subtree_right(struct inode *inode,
2062 handle_t *handle,
2063 struct ocfs2_path *left_path,
2064 struct ocfs2_path *right_path,
2065 int subtree_index)
2066{
2067 int ret, i;
2068 struct buffer_head *right_leaf_bh;
2069 struct buffer_head *left_leaf_bh = NULL;
2070 struct buffer_head *root_bh;
2071 struct ocfs2_extent_list *right_el, *left_el;
2072 struct ocfs2_extent_rec move_rec;
2073
2074 left_leaf_bh = path_leaf_bh(left_path);
2075 left_el = path_leaf_el(left_path);
2076
2077 if (left_el->l_next_free_rec != left_el->l_count) {
2078 ocfs2_error(inode->i_sb,
2079 "Inode %llu has non-full interior leaf node %llu"
2080 "(next free = %u)",
2081 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2082 (unsigned long long)left_leaf_bh->b_blocknr,
2083 le16_to_cpu(left_el->l_next_free_rec));
2084 return -EROFS;
2085 }
2086
2087 /*
2088 * This extent block may already have an empty record, so we
2089 * return early if so.
2090 */
2091 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2092 return 0;
2093
2094 root_bh = left_path->p_node[subtree_index].bh;
2095 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2096
Joel Becker13723d02008-10-17 19:25:01 -07002097 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
2098 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002099 if (ret) {
2100 mlog_errno(ret);
2101 goto out;
2102 }
2103
2104 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker13723d02008-10-17 19:25:01 -07002105 ret = ocfs2_path_bh_journal_access(handle, inode,
2106 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002107 if (ret) {
2108 mlog_errno(ret);
2109 goto out;
2110 }
2111
Joel Becker13723d02008-10-17 19:25:01 -07002112 ret = ocfs2_path_bh_journal_access(handle, inode,
2113 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002114 if (ret) {
2115 mlog_errno(ret);
2116 goto out;
2117 }
2118 }
2119
2120 right_leaf_bh = path_leaf_bh(right_path);
2121 right_el = path_leaf_el(right_path);
2122
2123 /* This is a code error, not a disk corruption. */
2124 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2125 "because rightmost leaf block %llu is empty\n",
2126 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2127 (unsigned long long)right_leaf_bh->b_blocknr);
2128
2129 ocfs2_create_empty_extent(right_el);
2130
2131 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2132 if (ret) {
2133 mlog_errno(ret);
2134 goto out;
2135 }
2136
2137 /* Do the copy now. */
2138 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2139 move_rec = left_el->l_recs[i];
2140 right_el->l_recs[0] = move_rec;
2141
2142 /*
2143 * Clear out the record we just copied and shift everything
2144 * over, leaving an empty extent in the left leaf.
2145 *
2146 * We temporarily subtract from next_free_rec so that the
2147 * shift will lose the tail record (which is now defunct).
2148 */
2149 le16_add_cpu(&left_el->l_next_free_rec, -1);
2150 ocfs2_shift_records_right(left_el);
2151 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2152 le16_add_cpu(&left_el->l_next_free_rec, 1);
2153
2154 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2155 if (ret) {
2156 mlog_errno(ret);
2157 goto out;
2158 }
2159
2160 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2161 subtree_index);
2162
2163out:
2164 return ret;
2165}
2166
2167/*
2168 * Given a full path, determine what cpos value would return us a path
2169 * containing the leaf immediately to the left of the current one.
2170 *
2171 * Will return zero if the path passed in is already the leftmost path.
2172 */
2173static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2174 struct ocfs2_path *path, u32 *cpos)
2175{
2176 int i, j, ret = 0;
2177 u64 blkno;
2178 struct ocfs2_extent_list *el;
2179
Mark Fashehe48edee2007-03-07 16:46:57 -08002180 BUG_ON(path->p_tree_depth == 0);
2181
Mark Fashehdcd05382007-01-16 11:32:23 -08002182 *cpos = 0;
2183
2184 blkno = path_leaf_bh(path)->b_blocknr;
2185
2186 /* Start at the tree node just above the leaf and work our way up. */
2187 i = path->p_tree_depth - 1;
2188 while (i >= 0) {
2189 el = path->p_node[i].el;
2190
2191 /*
2192 * Find the extent record just before the one in our
2193 * path.
2194 */
2195 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2196 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2197 if (j == 0) {
2198 if (i == 0) {
2199 /*
2200 * We've determined that the
2201 * path specified is already
2202 * the leftmost one - return a
2203 * cpos of zero.
2204 */
2205 goto out;
2206 }
2207 /*
2208 * The leftmost record points to our
2209 * leaf - we need to travel up the
2210 * tree one level.
2211 */
2212 goto next_node;
2213 }
2214
2215 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002216 *cpos = *cpos + ocfs2_rec_clusters(el,
2217 &el->l_recs[j - 1]);
2218 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002219 goto out;
2220 }
2221 }
2222
2223 /*
2224 * If we got here, we never found a valid node where
2225 * the tree indicated one should be.
2226 */
2227 ocfs2_error(sb,
2228 "Invalid extent tree at extent block %llu\n",
2229 (unsigned long long)blkno);
2230 ret = -EROFS;
2231 goto out;
2232
2233next_node:
2234 blkno = path->p_node[i].bh->b_blocknr;
2235 i--;
2236 }
2237
2238out:
2239 return ret;
2240}
2241
Mark Fasheh328d5752007-06-18 10:48:04 -07002242/*
2243 * Extend the transaction by enough credits to complete the rotation,
2244 * and still leave at least the original number of credits allocated
2245 * to this transaction.
2246 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002247static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002248 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002249 struct ocfs2_path *path)
2250{
Mark Fasheh328d5752007-06-18 10:48:04 -07002251 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002252
2253 if (handle->h_buffer_credits < credits)
2254 return ocfs2_extend_trans(handle, credits);
2255
2256 return 0;
2257}
2258
2259/*
2260 * Trap the case where we're inserting into the theoretical range past
2261 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2262 * whose cpos is less than ours into the right leaf.
2263 *
2264 * It's only necessary to look at the rightmost record of the left
2265 * leaf because the logic that calls us should ensure that the
2266 * theoretical ranges in the path components above the leaves are
2267 * correct.
2268 */
2269static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2270 u32 insert_cpos)
2271{
2272 struct ocfs2_extent_list *left_el;
2273 struct ocfs2_extent_rec *rec;
2274 int next_free;
2275
2276 left_el = path_leaf_el(left_path);
2277 next_free = le16_to_cpu(left_el->l_next_free_rec);
2278 rec = &left_el->l_recs[next_free - 1];
2279
2280 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2281 return 1;
2282 return 0;
2283}
2284
Mark Fasheh328d5752007-06-18 10:48:04 -07002285static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2286{
2287 int next_free = le16_to_cpu(el->l_next_free_rec);
2288 unsigned int range;
2289 struct ocfs2_extent_rec *rec;
2290
2291 if (next_free == 0)
2292 return 0;
2293
2294 rec = &el->l_recs[0];
2295 if (ocfs2_is_empty_extent(rec)) {
2296 /* Empty list. */
2297 if (next_free == 1)
2298 return 0;
2299 rec = &el->l_recs[1];
2300 }
2301
2302 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2303 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2304 return 1;
2305 return 0;
2306}
2307
Mark Fashehdcd05382007-01-16 11:32:23 -08002308/*
2309 * Rotate all the records in a btree right one record, starting at insert_cpos.
2310 *
2311 * The path to the rightmost leaf should be passed in.
2312 *
2313 * The array is assumed to be large enough to hold an entire path (tree depth).
2314 *
2315 * Upon succesful return from this function:
2316 *
2317 * - The 'right_path' array will contain a path to the leaf block
2318 * whose range contains e_cpos.
2319 * - That leaf block will have a single empty extent in list index 0.
2320 * - In the case that the rotation requires a post-insert update,
2321 * *ret_left_path will contain a valid path which can be passed to
2322 * ocfs2_insert_path().
2323 */
2324static int ocfs2_rotate_tree_right(struct inode *inode,
2325 handle_t *handle,
Mark Fasheh328d5752007-06-18 10:48:04 -07002326 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002327 u32 insert_cpos,
2328 struct ocfs2_path *right_path,
2329 struct ocfs2_path **ret_left_path)
2330{
Mark Fasheh328d5752007-06-18 10:48:04 -07002331 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002332 u32 cpos;
2333 struct ocfs2_path *left_path = NULL;
2334
2335 *ret_left_path = NULL;
2336
Joel Beckerffdd7a52008-10-17 22:32:01 -07002337 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002338 if (!left_path) {
2339 ret = -ENOMEM;
2340 mlog_errno(ret);
2341 goto out;
2342 }
2343
2344 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2345 if (ret) {
2346 mlog_errno(ret);
2347 goto out;
2348 }
2349
2350 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2351
2352 /*
2353 * What we want to do here is:
2354 *
2355 * 1) Start with the rightmost path.
2356 *
2357 * 2) Determine a path to the leaf block directly to the left
2358 * of that leaf.
2359 *
2360 * 3) Determine the 'subtree root' - the lowest level tree node
2361 * which contains a path to both leaves.
2362 *
2363 * 4) Rotate the subtree.
2364 *
2365 * 5) Find the next subtree by considering the left path to be
2366 * the new right path.
2367 *
2368 * The check at the top of this while loop also accepts
2369 * insert_cpos == cpos because cpos is only a _theoretical_
2370 * value to get us the left path - insert_cpos might very well
2371 * be filling that hole.
2372 *
2373 * Stop at a cpos of '0' because we either started at the
2374 * leftmost branch (i.e., a tree with one branch and a
2375 * rotation inside of it), or we've gone as far as we can in
2376 * rotating subtrees.
2377 */
2378 while (cpos && insert_cpos <= cpos) {
2379 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2380 insert_cpos, cpos);
2381
2382 ret = ocfs2_find_path(inode, left_path, cpos);
2383 if (ret) {
2384 mlog_errno(ret);
2385 goto out;
2386 }
2387
2388 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2389 path_leaf_bh(right_path),
2390 "Inode %lu: error during insert of %u "
2391 "(left path cpos %u) results in two identical "
2392 "paths ending at %llu\n",
2393 inode->i_ino, insert_cpos, cpos,
2394 (unsigned long long)
2395 path_leaf_bh(left_path)->b_blocknr);
2396
Mark Fasheh328d5752007-06-18 10:48:04 -07002397 if (split == SPLIT_NONE &&
2398 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002399 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002400
2401 /*
2402 * We've rotated the tree as much as we
2403 * should. The rest is up to
2404 * ocfs2_insert_path() to complete, after the
2405 * record insertion. We indicate this
2406 * situation by returning the left path.
2407 *
2408 * The reason we don't adjust the records here
2409 * before the record insert is that an error
2410 * later might break the rule where a parent
2411 * record e_cpos will reflect the actual
2412 * e_cpos of the 1st nonempty record of the
2413 * child list.
2414 */
2415 *ret_left_path = left_path;
2416 goto out_ret_path;
2417 }
2418
2419 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2420
2421 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2422 start,
2423 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2424 right_path->p_tree_depth);
2425
2426 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002427 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002428 if (ret) {
2429 mlog_errno(ret);
2430 goto out;
2431 }
2432
2433 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2434 right_path, start);
2435 if (ret) {
2436 mlog_errno(ret);
2437 goto out;
2438 }
2439
Mark Fasheh328d5752007-06-18 10:48:04 -07002440 if (split != SPLIT_NONE &&
2441 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2442 insert_cpos)) {
2443 /*
2444 * A rotate moves the rightmost left leaf
2445 * record over to the leftmost right leaf
2446 * slot. If we're doing an extent split
2447 * instead of a real insert, then we have to
2448 * check that the extent to be split wasn't
2449 * just moved over. If it was, then we can
2450 * exit here, passing left_path back -
2451 * ocfs2_split_extent() is smart enough to
2452 * search both leaves.
2453 */
2454 *ret_left_path = left_path;
2455 goto out_ret_path;
2456 }
2457
Mark Fashehdcd05382007-01-16 11:32:23 -08002458 /*
2459 * There is no need to re-read the next right path
2460 * as we know that it'll be our current left
2461 * path. Optimize by copying values instead.
2462 */
2463 ocfs2_mv_path(right_path, left_path);
2464
2465 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2466 &cpos);
2467 if (ret) {
2468 mlog_errno(ret);
2469 goto out;
2470 }
2471 }
2472
2473out:
2474 ocfs2_free_path(left_path);
2475
2476out_ret_path:
2477 return ret;
2478}
2479
Tao Ma3c5e1062009-07-21 15:42:05 +08002480static int ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2481 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002482{
Tao Ma3c5e1062009-07-21 15:42:05 +08002483 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002484 struct ocfs2_extent_rec *rec;
2485 struct ocfs2_extent_list *el;
2486 struct ocfs2_extent_block *eb;
2487 u32 range;
2488
Tao Ma3c5e1062009-07-21 15:42:05 +08002489 /*
2490 * In normal tree rotation process, we will never touch the
2491 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2492 * doesn't reserve the credits for them either.
2493 *
2494 * But we do have a special case here which will update the rightmost
2495 * records for all the bh in the path.
2496 * So we have to allocate extra credits and access them.
2497 */
2498 ret = ocfs2_extend_trans(handle,
2499 handle->h_buffer_credits + subtree_index);
2500 if (ret) {
2501 mlog_errno(ret);
2502 goto out;
2503 }
2504
2505 ret = ocfs2_journal_access_path(inode, handle, path);
2506 if (ret) {
2507 mlog_errno(ret);
2508 goto out;
2509 }
2510
Mark Fasheh328d5752007-06-18 10:48:04 -07002511 /* Path should always be rightmost. */
2512 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2513 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2514
2515 el = &eb->h_list;
2516 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2517 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2518 rec = &el->l_recs[idx];
2519 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2520
2521 for (i = 0; i < path->p_tree_depth; i++) {
2522 el = path->p_node[i].el;
2523 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2524 rec = &el->l_recs[idx];
2525
2526 rec->e_int_clusters = cpu_to_le32(range);
2527 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2528
2529 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2530 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002531out:
2532 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002533}
2534
2535static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2536 struct ocfs2_cached_dealloc_ctxt *dealloc,
2537 struct ocfs2_path *path, int unlink_start)
2538{
2539 int ret, i;
2540 struct ocfs2_extent_block *eb;
2541 struct ocfs2_extent_list *el;
2542 struct buffer_head *bh;
2543
2544 for(i = unlink_start; i < path_num_items(path); i++) {
2545 bh = path->p_node[i].bh;
2546
2547 eb = (struct ocfs2_extent_block *)bh->b_data;
2548 /*
2549 * Not all nodes might have had their final count
2550 * decremented by the caller - handle this here.
2551 */
2552 el = &eb->h_list;
2553 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2554 mlog(ML_ERROR,
2555 "Inode %llu, attempted to remove extent block "
2556 "%llu with %u records\n",
2557 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2558 (unsigned long long)le64_to_cpu(eb->h_blkno),
2559 le16_to_cpu(el->l_next_free_rec));
2560
2561 ocfs2_journal_dirty(handle, bh);
2562 ocfs2_remove_from_cache(inode, bh);
2563 continue;
2564 }
2565
2566 el->l_next_free_rec = 0;
2567 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2568
2569 ocfs2_journal_dirty(handle, bh);
2570
2571 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2572 if (ret)
2573 mlog_errno(ret);
2574
2575 ocfs2_remove_from_cache(inode, bh);
2576 }
2577}
2578
2579static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2580 struct ocfs2_path *left_path,
2581 struct ocfs2_path *right_path,
2582 int subtree_index,
2583 struct ocfs2_cached_dealloc_ctxt *dealloc)
2584{
2585 int i;
2586 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2587 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2588 struct ocfs2_extent_list *el;
2589 struct ocfs2_extent_block *eb;
2590
2591 el = path_leaf_el(left_path);
2592
2593 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2594
2595 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2596 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2597 break;
2598
2599 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2600
2601 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2602 le16_add_cpu(&root_el->l_next_free_rec, -1);
2603
2604 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2605 eb->h_next_leaf_blk = 0;
2606
2607 ocfs2_journal_dirty(handle, root_bh);
2608 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2609
2610 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2611 subtree_index + 1);
2612}
2613
2614static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2615 struct ocfs2_path *left_path,
2616 struct ocfs2_path *right_path,
2617 int subtree_index,
2618 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002619 int *deleted,
2620 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002621{
2622 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002623 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002624 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2625 struct ocfs2_extent_block *eb;
2626
2627 *deleted = 0;
2628
2629 right_leaf_el = path_leaf_el(right_path);
2630 left_leaf_el = path_leaf_el(left_path);
2631 root_bh = left_path->p_node[subtree_index].bh;
2632 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2633
2634 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2635 return 0;
2636
2637 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2638 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2639 /*
2640 * It's legal for us to proceed if the right leaf is
2641 * the rightmost one and it has an empty extent. There
2642 * are two cases to handle - whether the leaf will be
2643 * empty after removal or not. If the leaf isn't empty
2644 * then just remove the empty extent up front. The
2645 * next block will handle empty leaves by flagging
2646 * them for unlink.
2647 *
2648 * Non rightmost leaves will throw -EAGAIN and the
2649 * caller can manually move the subtree and retry.
2650 */
2651
2652 if (eb->h_next_leaf_blk != 0ULL)
2653 return -EAGAIN;
2654
2655 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker13723d02008-10-17 19:25:01 -07002656 ret = ocfs2_journal_access_eb(handle, inode,
2657 path_leaf_bh(right_path),
2658 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002659 if (ret) {
2660 mlog_errno(ret);
2661 goto out;
2662 }
2663
2664 ocfs2_remove_empty_extent(right_leaf_el);
2665 } else
2666 right_has_empty = 1;
2667 }
2668
2669 if (eb->h_next_leaf_blk == 0ULL &&
2670 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2671 /*
2672 * We have to update i_last_eb_blk during the meta
2673 * data delete.
2674 */
Joel Becker13723d02008-10-17 19:25:01 -07002675 ret = ocfs2_et_root_journal_access(handle, inode, et,
2676 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002677 if (ret) {
2678 mlog_errno(ret);
2679 goto out;
2680 }
2681
2682 del_right_subtree = 1;
2683 }
2684
2685 /*
2686 * Getting here with an empty extent in the right path implies
2687 * that it's the rightmost path and will be deleted.
2688 */
2689 BUG_ON(right_has_empty && !del_right_subtree);
2690
Joel Becker13723d02008-10-17 19:25:01 -07002691 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
2692 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002693 if (ret) {
2694 mlog_errno(ret);
2695 goto out;
2696 }
2697
2698 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker13723d02008-10-17 19:25:01 -07002699 ret = ocfs2_path_bh_journal_access(handle, inode,
2700 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002701 if (ret) {
2702 mlog_errno(ret);
2703 goto out;
2704 }
2705
Joel Becker13723d02008-10-17 19:25:01 -07002706 ret = ocfs2_path_bh_journal_access(handle, inode,
2707 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002708 if (ret) {
2709 mlog_errno(ret);
2710 goto out;
2711 }
2712 }
2713
2714 if (!right_has_empty) {
2715 /*
2716 * Only do this if we're moving a real
2717 * record. Otherwise, the action is delayed until
2718 * after removal of the right path in which case we
2719 * can do a simple shift to remove the empty extent.
2720 */
2721 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2722 memset(&right_leaf_el->l_recs[0], 0,
2723 sizeof(struct ocfs2_extent_rec));
2724 }
2725 if (eb->h_next_leaf_blk == 0ULL) {
2726 /*
2727 * Move recs over to get rid of empty extent, decrease
2728 * next_free. This is allowed to remove the last
2729 * extent in our leaf (setting l_next_free_rec to
2730 * zero) - the delete code below won't care.
2731 */
2732 ocfs2_remove_empty_extent(right_leaf_el);
2733 }
2734
2735 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2736 if (ret)
2737 mlog_errno(ret);
2738 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2739 if (ret)
2740 mlog_errno(ret);
2741
2742 if (del_right_subtree) {
2743 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2744 subtree_index, dealloc);
Tao Ma3c5e1062009-07-21 15:42:05 +08002745 ret = ocfs2_update_edge_lengths(inode, handle, subtree_index,
2746 left_path);
2747 if (ret) {
2748 mlog_errno(ret);
2749 goto out;
2750 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002751
2752 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002753 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002754
2755 /*
2756 * Removal of the extent in the left leaf was skipped
2757 * above so we could delete the right path
2758 * 1st.
2759 */
2760 if (right_has_empty)
2761 ocfs2_remove_empty_extent(left_leaf_el);
2762
Tao Mae7d4cb62008-08-18 17:38:44 +08002763 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002764 if (ret)
2765 mlog_errno(ret);
2766
2767 *deleted = 1;
2768 } else
2769 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2770 subtree_index);
2771
2772out:
2773 return ret;
2774}
2775
2776/*
2777 * Given a full path, determine what cpos value would return us a path
2778 * containing the leaf immediately to the right of the current one.
2779 *
2780 * Will return zero if the path passed in is already the rightmost path.
2781 *
2782 * This looks similar, but is subtly different to
2783 * ocfs2_find_cpos_for_left_leaf().
2784 */
2785static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2786 struct ocfs2_path *path, u32 *cpos)
2787{
2788 int i, j, ret = 0;
2789 u64 blkno;
2790 struct ocfs2_extent_list *el;
2791
2792 *cpos = 0;
2793
2794 if (path->p_tree_depth == 0)
2795 return 0;
2796
2797 blkno = path_leaf_bh(path)->b_blocknr;
2798
2799 /* Start at the tree node just above the leaf and work our way up. */
2800 i = path->p_tree_depth - 1;
2801 while (i >= 0) {
2802 int next_free;
2803
2804 el = path->p_node[i].el;
2805
2806 /*
2807 * Find the extent record just after the one in our
2808 * path.
2809 */
2810 next_free = le16_to_cpu(el->l_next_free_rec);
2811 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2812 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2813 if (j == (next_free - 1)) {
2814 if (i == 0) {
2815 /*
2816 * We've determined that the
2817 * path specified is already
2818 * the rightmost one - return a
2819 * cpos of zero.
2820 */
2821 goto out;
2822 }
2823 /*
2824 * The rightmost record points to our
2825 * leaf - we need to travel up the
2826 * tree one level.
2827 */
2828 goto next_node;
2829 }
2830
2831 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2832 goto out;
2833 }
2834 }
2835
2836 /*
2837 * If we got here, we never found a valid node where
2838 * the tree indicated one should be.
2839 */
2840 ocfs2_error(sb,
2841 "Invalid extent tree at extent block %llu\n",
2842 (unsigned long long)blkno);
2843 ret = -EROFS;
2844 goto out;
2845
2846next_node:
2847 blkno = path->p_node[i].bh->b_blocknr;
2848 i--;
2849 }
2850
2851out:
2852 return ret;
2853}
2854
2855static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2856 handle_t *handle,
Joel Becker13723d02008-10-17 19:25:01 -07002857 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002858{
2859 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002860 struct buffer_head *bh = path_leaf_bh(path);
2861 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002862
2863 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2864 return 0;
2865
Joel Becker13723d02008-10-17 19:25:01 -07002866 ret = ocfs2_path_bh_journal_access(handle, inode, path,
2867 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002868 if (ret) {
2869 mlog_errno(ret);
2870 goto out;
2871 }
2872
2873 ocfs2_remove_empty_extent(el);
2874
2875 ret = ocfs2_journal_dirty(handle, bh);
2876 if (ret)
2877 mlog_errno(ret);
2878
2879out:
2880 return ret;
2881}
2882
2883static int __ocfs2_rotate_tree_left(struct inode *inode,
2884 handle_t *handle, int orig_credits,
2885 struct ocfs2_path *path,
2886 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002887 struct ocfs2_path **empty_extent_path,
2888 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002889{
2890 int ret, subtree_root, deleted;
2891 u32 right_cpos;
2892 struct ocfs2_path *left_path = NULL;
2893 struct ocfs2_path *right_path = NULL;
2894
2895 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2896
2897 *empty_extent_path = NULL;
2898
2899 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2900 &right_cpos);
2901 if (ret) {
2902 mlog_errno(ret);
2903 goto out;
2904 }
2905
Joel Beckerffdd7a52008-10-17 22:32:01 -07002906 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002907 if (!left_path) {
2908 ret = -ENOMEM;
2909 mlog_errno(ret);
2910 goto out;
2911 }
2912
2913 ocfs2_cp_path(left_path, path);
2914
Joel Beckerffdd7a52008-10-17 22:32:01 -07002915 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002916 if (!right_path) {
2917 ret = -ENOMEM;
2918 mlog_errno(ret);
2919 goto out;
2920 }
2921
2922 while (right_cpos) {
2923 ret = ocfs2_find_path(inode, right_path, right_cpos);
2924 if (ret) {
2925 mlog_errno(ret);
2926 goto out;
2927 }
2928
2929 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2930 right_path);
2931
2932 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2933 subtree_root,
2934 (unsigned long long)
2935 right_path->p_node[subtree_root].bh->b_blocknr,
2936 right_path->p_tree_depth);
2937
2938 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2939 orig_credits, left_path);
2940 if (ret) {
2941 mlog_errno(ret);
2942 goto out;
2943 }
2944
Mark Fashehe8aed342007-12-03 16:43:01 -08002945 /*
2946 * Caller might still want to make changes to the
2947 * tree root, so re-add it to the journal here.
2948 */
Joel Becker13723d02008-10-17 19:25:01 -07002949 ret = ocfs2_path_bh_journal_access(handle, inode,
2950 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08002951 if (ret) {
2952 mlog_errno(ret);
2953 goto out;
2954 }
2955
Mark Fasheh328d5752007-06-18 10:48:04 -07002956 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2957 right_path, subtree_root,
Tao Mae7d4cb62008-08-18 17:38:44 +08002958 dealloc, &deleted, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002959 if (ret == -EAGAIN) {
2960 /*
2961 * The rotation has to temporarily stop due to
2962 * the right subtree having an empty
2963 * extent. Pass it back to the caller for a
2964 * fixup.
2965 */
2966 *empty_extent_path = right_path;
2967 right_path = NULL;
2968 goto out;
2969 }
2970 if (ret) {
2971 mlog_errno(ret);
2972 goto out;
2973 }
2974
2975 /*
2976 * The subtree rotate might have removed records on
2977 * the rightmost edge. If so, then rotation is
2978 * complete.
2979 */
2980 if (deleted)
2981 break;
2982
2983 ocfs2_mv_path(left_path, right_path);
2984
2985 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2986 &right_cpos);
2987 if (ret) {
2988 mlog_errno(ret);
2989 goto out;
2990 }
2991 }
2992
2993out:
2994 ocfs2_free_path(right_path);
2995 ocfs2_free_path(left_path);
2996
2997 return ret;
2998}
2999
3000static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003001 struct ocfs2_path *path,
3002 struct ocfs2_cached_dealloc_ctxt *dealloc,
3003 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003004{
3005 int ret, subtree_index;
3006 u32 cpos;
3007 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003008 struct ocfs2_extent_block *eb;
3009 struct ocfs2_extent_list *el;
3010
Mark Fasheh328d5752007-06-18 10:48:04 -07003011
Joel Becker35dc0aa2008-08-20 16:25:06 -07003012 ret = ocfs2_et_sanity_check(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003013 if (ret)
3014 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003015 /*
3016 * There's two ways we handle this depending on
3017 * whether path is the only existing one.
3018 */
3019 ret = ocfs2_extend_rotate_transaction(handle, 0,
3020 handle->h_buffer_credits,
3021 path);
3022 if (ret) {
3023 mlog_errno(ret);
3024 goto out;
3025 }
3026
3027 ret = ocfs2_journal_access_path(inode, handle, path);
3028 if (ret) {
3029 mlog_errno(ret);
3030 goto out;
3031 }
3032
3033 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
3034 if (ret) {
3035 mlog_errno(ret);
3036 goto out;
3037 }
3038
3039 if (cpos) {
3040 /*
3041 * We have a path to the left of this one - it needs
3042 * an update too.
3043 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003044 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003045 if (!left_path) {
3046 ret = -ENOMEM;
3047 mlog_errno(ret);
3048 goto out;
3049 }
3050
3051 ret = ocfs2_find_path(inode, left_path, cpos);
3052 if (ret) {
3053 mlog_errno(ret);
3054 goto out;
3055 }
3056
3057 ret = ocfs2_journal_access_path(inode, handle, left_path);
3058 if (ret) {
3059 mlog_errno(ret);
3060 goto out;
3061 }
3062
3063 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
3064
3065 ocfs2_unlink_subtree(inode, handle, left_path, path,
3066 subtree_index, dealloc);
Tao Ma3c5e1062009-07-21 15:42:05 +08003067 ret = ocfs2_update_edge_lengths(inode, handle, subtree_index,
3068 left_path);
3069 if (ret) {
3070 mlog_errno(ret);
3071 goto out;
3072 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003073
3074 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003075 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003076 } else {
3077 /*
3078 * 'path' is also the leftmost path which
3079 * means it must be the only one. This gets
3080 * handled differently because we want to
3081 * revert the inode back to having extents
3082 * in-line.
3083 */
3084 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
3085
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003086 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003087 el->l_tree_depth = 0;
3088 el->l_next_free_rec = 0;
3089 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3090
Joel Becker35dc0aa2008-08-20 16:25:06 -07003091 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003092 }
3093
3094 ocfs2_journal_dirty(handle, path_root_bh(path));
3095
3096out:
3097 ocfs2_free_path(left_path);
3098 return ret;
3099}
3100
3101/*
3102 * Left rotation of btree records.
3103 *
3104 * In many ways, this is (unsurprisingly) the opposite of right
3105 * rotation. We start at some non-rightmost path containing an empty
3106 * extent in the leaf block. The code works its way to the rightmost
3107 * path by rotating records to the left in every subtree.
3108 *
3109 * This is used by any code which reduces the number of extent records
3110 * in a leaf. After removal, an empty record should be placed in the
3111 * leftmost list position.
3112 *
3113 * This won't handle a length update of the rightmost path records if
3114 * the rightmost tree leaf record is removed so the caller is
3115 * responsible for detecting and correcting that.
3116 */
3117static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
3118 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003119 struct ocfs2_cached_dealloc_ctxt *dealloc,
3120 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003121{
3122 int ret, orig_credits = handle->h_buffer_credits;
3123 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3124 struct ocfs2_extent_block *eb;
3125 struct ocfs2_extent_list *el;
3126
3127 el = path_leaf_el(path);
3128 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3129 return 0;
3130
3131 if (path->p_tree_depth == 0) {
3132rightmost_no_delete:
3133 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003134 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003135 * it up front.
3136 */
3137 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
Joel Becker13723d02008-10-17 19:25:01 -07003138 path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003139 if (ret)
3140 mlog_errno(ret);
3141 goto out;
3142 }
3143
3144 /*
3145 * Handle rightmost branch now. There's several cases:
3146 * 1) simple rotation leaving records in there. That's trivial.
3147 * 2) rotation requiring a branch delete - there's no more
3148 * records left. Two cases of this:
3149 * a) There are branches to the left.
3150 * b) This is also the leftmost (the only) branch.
3151 *
3152 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3153 * 2a) we need the left branch so that we can update it with the unlink
3154 * 2b) we need to bring the inode back to inline extents.
3155 */
3156
3157 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3158 el = &eb->h_list;
3159 if (eb->h_next_leaf_blk == 0) {
3160 /*
3161 * This gets a bit tricky if we're going to delete the
3162 * rightmost path. Get the other cases out of the way
3163 * 1st.
3164 */
3165 if (le16_to_cpu(el->l_next_free_rec) > 1)
3166 goto rightmost_no_delete;
3167
3168 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3169 ret = -EIO;
3170 ocfs2_error(inode->i_sb,
3171 "Inode %llu has empty extent block at %llu",
3172 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3173 (unsigned long long)le64_to_cpu(eb->h_blkno));
3174 goto out;
3175 }
3176
3177 /*
3178 * XXX: The caller can not trust "path" any more after
3179 * this as it will have been deleted. What do we do?
3180 *
3181 * In theory the rotate-for-merge code will never get
3182 * here because it'll always ask for a rotate in a
3183 * nonempty list.
3184 */
3185
3186 ret = ocfs2_remove_rightmost_path(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003187 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003188 if (ret)
3189 mlog_errno(ret);
3190 goto out;
3191 }
3192
3193 /*
3194 * Now we can loop, remembering the path we get from -EAGAIN
3195 * and restarting from there.
3196 */
3197try_rotate:
3198 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003199 dealloc, &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003200 if (ret && ret != -EAGAIN) {
3201 mlog_errno(ret);
3202 goto out;
3203 }
3204
3205 while (ret == -EAGAIN) {
3206 tmp_path = restart_path;
3207 restart_path = NULL;
3208
3209 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
3210 tmp_path, dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003211 &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003212 if (ret && ret != -EAGAIN) {
3213 mlog_errno(ret);
3214 goto out;
3215 }
3216
3217 ocfs2_free_path(tmp_path);
3218 tmp_path = NULL;
3219
3220 if (ret == 0)
3221 goto try_rotate;
3222 }
3223
3224out:
3225 ocfs2_free_path(tmp_path);
3226 ocfs2_free_path(restart_path);
3227 return ret;
3228}
3229
3230static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3231 int index)
3232{
3233 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3234 unsigned int size;
3235
3236 if (rec->e_leaf_clusters == 0) {
3237 /*
3238 * We consumed all of the merged-from record. An empty
3239 * extent cannot exist anywhere but the 1st array
3240 * position, so move things over if the merged-from
3241 * record doesn't occupy that position.
3242 *
3243 * This creates a new empty extent so the caller
3244 * should be smart enough to have removed any existing
3245 * ones.
3246 */
3247 if (index > 0) {
3248 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3249 size = index * sizeof(struct ocfs2_extent_rec);
3250 memmove(&el->l_recs[1], &el->l_recs[0], size);
3251 }
3252
3253 /*
3254 * Always memset - the caller doesn't check whether it
3255 * created an empty extent, so there could be junk in
3256 * the other fields.
3257 */
3258 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3259 }
3260}
3261
Tao Ma677b9752008-01-30 14:21:05 +08003262static int ocfs2_get_right_path(struct inode *inode,
3263 struct ocfs2_path *left_path,
3264 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003265{
3266 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003267 u32 right_cpos;
3268 struct ocfs2_path *right_path = NULL;
3269 struct ocfs2_extent_list *left_el;
3270
3271 *ret_right_path = NULL;
3272
3273 /* This function shouldn't be called for non-trees. */
3274 BUG_ON(left_path->p_tree_depth == 0);
3275
3276 left_el = path_leaf_el(left_path);
3277 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3278
3279 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3280 &right_cpos);
3281 if (ret) {
3282 mlog_errno(ret);
3283 goto out;
3284 }
3285
3286 /* This function shouldn't be called for the rightmost leaf. */
3287 BUG_ON(right_cpos == 0);
3288
Joel Beckerffdd7a52008-10-17 22:32:01 -07003289 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003290 if (!right_path) {
3291 ret = -ENOMEM;
3292 mlog_errno(ret);
3293 goto out;
3294 }
3295
3296 ret = ocfs2_find_path(inode, right_path, right_cpos);
3297 if (ret) {
3298 mlog_errno(ret);
3299 goto out;
3300 }
3301
3302 *ret_right_path = right_path;
3303out:
3304 if (ret)
3305 ocfs2_free_path(right_path);
3306 return ret;
3307}
3308
3309/*
3310 * Remove split_rec clusters from the record at index and merge them
3311 * onto the beginning of the record "next" to it.
3312 * For index < l_count - 1, the next means the extent rec at index + 1.
3313 * For index == l_count - 1, the "next" means the 1st extent rec of the
3314 * next extent block.
3315 */
3316static int ocfs2_merge_rec_right(struct inode *inode,
3317 struct ocfs2_path *left_path,
3318 handle_t *handle,
3319 struct ocfs2_extent_rec *split_rec,
3320 int index)
3321{
3322 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003323 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3324 struct ocfs2_extent_rec *left_rec;
3325 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003326 struct ocfs2_extent_list *right_el;
3327 struct ocfs2_path *right_path = NULL;
3328 int subtree_index = 0;
3329 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3330 struct buffer_head *bh = path_leaf_bh(left_path);
3331 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003332
3333 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003334 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003335
Al Viro9d8df6a2008-05-21 06:32:11 +01003336 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003337 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3338 /* we meet with a cross extent block merge. */
3339 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3340 if (ret) {
3341 mlog_errno(ret);
3342 goto out;
3343 }
3344
3345 right_el = path_leaf_el(right_path);
3346 next_free = le16_to_cpu(right_el->l_next_free_rec);
3347 BUG_ON(next_free <= 0);
3348 right_rec = &right_el->l_recs[0];
3349 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003350 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003351 right_rec = &right_el->l_recs[1];
3352 }
3353
3354 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3355 le16_to_cpu(left_rec->e_leaf_clusters) !=
3356 le32_to_cpu(right_rec->e_cpos));
3357
3358 subtree_index = ocfs2_find_subtree_root(inode,
3359 left_path, right_path);
3360
3361 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3362 handle->h_buffer_credits,
3363 right_path);
3364 if (ret) {
3365 mlog_errno(ret);
3366 goto out;
3367 }
3368
3369 root_bh = left_path->p_node[subtree_index].bh;
3370 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3371
Joel Becker13723d02008-10-17 19:25:01 -07003372 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
3373 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003374 if (ret) {
3375 mlog_errno(ret);
3376 goto out;
3377 }
3378
3379 for (i = subtree_index + 1;
3380 i < path_num_items(right_path); i++) {
Joel Becker13723d02008-10-17 19:25:01 -07003381 ret = ocfs2_path_bh_journal_access(handle, inode,
3382 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003383 if (ret) {
3384 mlog_errno(ret);
3385 goto out;
3386 }
3387
Joel Becker13723d02008-10-17 19:25:01 -07003388 ret = ocfs2_path_bh_journal_access(handle, inode,
3389 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003390 if (ret) {
3391 mlog_errno(ret);
3392 goto out;
3393 }
3394 }
3395
3396 } else {
3397 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3398 right_rec = &el->l_recs[index + 1];
3399 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003400
Joel Becker13723d02008-10-17 19:25:01 -07003401 ret = ocfs2_path_bh_journal_access(handle, inode, left_path,
3402 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003403 if (ret) {
3404 mlog_errno(ret);
3405 goto out;
3406 }
3407
3408 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3409
3410 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3411 le64_add_cpu(&right_rec->e_blkno,
3412 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3413 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3414
3415 ocfs2_cleanup_merge(el, index);
3416
3417 ret = ocfs2_journal_dirty(handle, bh);
3418 if (ret)
3419 mlog_errno(ret);
3420
Tao Ma677b9752008-01-30 14:21:05 +08003421 if (right_path) {
3422 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3423 if (ret)
3424 mlog_errno(ret);
3425
3426 ocfs2_complete_edge_insert(inode, handle, left_path,
3427 right_path, subtree_index);
3428 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003429out:
Tao Ma677b9752008-01-30 14:21:05 +08003430 if (right_path)
3431 ocfs2_free_path(right_path);
3432 return ret;
3433}
3434
3435static int ocfs2_get_left_path(struct inode *inode,
3436 struct ocfs2_path *right_path,
3437 struct ocfs2_path **ret_left_path)
3438{
3439 int ret;
3440 u32 left_cpos;
3441 struct ocfs2_path *left_path = NULL;
3442
3443 *ret_left_path = NULL;
3444
3445 /* This function shouldn't be called for non-trees. */
3446 BUG_ON(right_path->p_tree_depth == 0);
3447
3448 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3449 right_path, &left_cpos);
3450 if (ret) {
3451 mlog_errno(ret);
3452 goto out;
3453 }
3454
3455 /* This function shouldn't be called for the leftmost leaf. */
3456 BUG_ON(left_cpos == 0);
3457
Joel Beckerffdd7a52008-10-17 22:32:01 -07003458 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003459 if (!left_path) {
3460 ret = -ENOMEM;
3461 mlog_errno(ret);
3462 goto out;
3463 }
3464
3465 ret = ocfs2_find_path(inode, left_path, left_cpos);
3466 if (ret) {
3467 mlog_errno(ret);
3468 goto out;
3469 }
3470
3471 *ret_left_path = left_path;
3472out:
3473 if (ret)
3474 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003475 return ret;
3476}
3477
3478/*
3479 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003480 * onto the tail of the record "before" it.
3481 * For index > 0, the "before" means the extent rec at index - 1.
3482 *
3483 * For index == 0, the "before" means the last record of the previous
3484 * extent block. And there is also a situation that we may need to
3485 * remove the rightmost leaf extent block in the right_path and change
3486 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003487 */
Tao Ma677b9752008-01-30 14:21:05 +08003488static int ocfs2_merge_rec_left(struct inode *inode,
3489 struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003490 handle_t *handle,
3491 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003492 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003493 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003494 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003495{
Tao Ma677b9752008-01-30 14:21:05 +08003496 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003497 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3498 struct ocfs2_extent_rec *left_rec;
3499 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003500 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3501 struct buffer_head *bh = path_leaf_bh(right_path);
3502 struct buffer_head *root_bh = NULL;
3503 struct ocfs2_path *left_path = NULL;
3504 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003505
Tao Ma677b9752008-01-30 14:21:05 +08003506 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003507
Mark Fasheh328d5752007-06-18 10:48:04 -07003508 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003509 if (index == 0) {
3510 /* we meet with a cross extent block merge. */
3511 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3512 if (ret) {
3513 mlog_errno(ret);
3514 goto out;
3515 }
3516
3517 left_el = path_leaf_el(left_path);
3518 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3519 le16_to_cpu(left_el->l_count));
3520
3521 left_rec = &left_el->l_recs[
3522 le16_to_cpu(left_el->l_next_free_rec) - 1];
3523 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3524 le16_to_cpu(left_rec->e_leaf_clusters) !=
3525 le32_to_cpu(split_rec->e_cpos));
3526
3527 subtree_index = ocfs2_find_subtree_root(inode,
3528 left_path, right_path);
3529
3530 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3531 handle->h_buffer_credits,
3532 left_path);
3533 if (ret) {
3534 mlog_errno(ret);
3535 goto out;
3536 }
3537
3538 root_bh = left_path->p_node[subtree_index].bh;
3539 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3540
Joel Becker13723d02008-10-17 19:25:01 -07003541 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
3542 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003543 if (ret) {
3544 mlog_errno(ret);
3545 goto out;
3546 }
3547
3548 for (i = subtree_index + 1;
3549 i < path_num_items(right_path); i++) {
Joel Becker13723d02008-10-17 19:25:01 -07003550 ret = ocfs2_path_bh_journal_access(handle, inode,
3551 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003552 if (ret) {
3553 mlog_errno(ret);
3554 goto out;
3555 }
3556
Joel Becker13723d02008-10-17 19:25:01 -07003557 ret = ocfs2_path_bh_journal_access(handle, inode,
3558 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003559 if (ret) {
3560 mlog_errno(ret);
3561 goto out;
3562 }
3563 }
3564 } else {
3565 left_rec = &el->l_recs[index - 1];
3566 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3567 has_empty_extent = 1;
3568 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003569
Tao Ma9047bea2009-01-05 14:45:24 +08003570 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
3571 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003572 if (ret) {
3573 mlog_errno(ret);
3574 goto out;
3575 }
3576
3577 if (has_empty_extent && index == 1) {
3578 /*
3579 * The easy case - we can just plop the record right in.
3580 */
3581 *left_rec = *split_rec;
3582
3583 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003584 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003585 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003586
3587 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3588 le64_add_cpu(&right_rec->e_blkno,
3589 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3590 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3591
3592 ocfs2_cleanup_merge(el, index);
3593
3594 ret = ocfs2_journal_dirty(handle, bh);
3595 if (ret)
3596 mlog_errno(ret);
3597
Tao Ma677b9752008-01-30 14:21:05 +08003598 if (left_path) {
3599 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3600 if (ret)
3601 mlog_errno(ret);
3602
3603 /*
3604 * In the situation that the right_rec is empty and the extent
3605 * block is empty also, ocfs2_complete_edge_insert can't handle
3606 * it and we need to delete the right extent block.
3607 */
3608 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3609 le16_to_cpu(el->l_next_free_rec) == 1) {
3610
3611 ret = ocfs2_remove_rightmost_path(inode, handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003612 right_path,
3613 dealloc, et);
Tao Ma677b9752008-01-30 14:21:05 +08003614 if (ret) {
3615 mlog_errno(ret);
3616 goto out;
3617 }
3618
3619 /* Now the rightmost extent block has been deleted.
3620 * So we use the new rightmost path.
3621 */
3622 ocfs2_mv_path(right_path, left_path);
3623 left_path = NULL;
3624 } else
3625 ocfs2_complete_edge_insert(inode, handle, left_path,
3626 right_path, subtree_index);
3627 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003628out:
Tao Ma677b9752008-01-30 14:21:05 +08003629 if (left_path)
3630 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003631 return ret;
3632}
3633
3634static int ocfs2_try_to_merge_extent(struct inode *inode,
3635 handle_t *handle,
Tao Ma677b9752008-01-30 14:21:05 +08003636 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003637 int split_index,
3638 struct ocfs2_extent_rec *split_rec,
3639 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003640 struct ocfs2_merge_ctxt *ctxt,
3641 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003642
3643{
Tao Mao518d7262007-08-28 17:25:35 -07003644 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003645 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003646 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3647
3648 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3649
Tao Mao518d7262007-08-28 17:25:35 -07003650 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3651 /*
3652 * The merge code will need to create an empty
3653 * extent to take the place of the newly
3654 * emptied slot. Remove any pre-existing empty
3655 * extents - having more than one in a leaf is
3656 * illegal.
3657 */
Tao Ma677b9752008-01-30 14:21:05 +08003658 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003659 dealloc, et);
Tao Mao518d7262007-08-28 17:25:35 -07003660 if (ret) {
3661 mlog_errno(ret);
3662 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003663 }
Tao Mao518d7262007-08-28 17:25:35 -07003664 split_index--;
3665 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003666 }
3667
3668 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3669 /*
3670 * Left-right contig implies this.
3671 */
3672 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003673
3674 /*
3675 * Since the leftright insert always covers the entire
3676 * extent, this call will delete the insert record
3677 * entirely, resulting in an empty extent record added to
3678 * the extent block.
3679 *
3680 * Since the adding of an empty extent shifts
3681 * everything back to the right, there's no need to
3682 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003683 *
3684 * When the split_index is zero, we need to merge it to the
3685 * prevoius extent block. It is more efficient and easier
3686 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003687 */
Tao Ma677b9752008-01-30 14:21:05 +08003688 ret = ocfs2_merge_rec_right(inode, path,
3689 handle, split_rec,
3690 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003691 if (ret) {
3692 mlog_errno(ret);
3693 goto out;
3694 }
3695
3696 /*
3697 * We can only get this from logic error above.
3698 */
3699 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3700
Tao Ma677b9752008-01-30 14:21:05 +08003701 /* The merge left us with an empty extent, remove it. */
Tao Mae7d4cb62008-08-18 17:38:44 +08003702 ret = ocfs2_rotate_tree_left(inode, handle, path,
3703 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003704 if (ret) {
3705 mlog_errno(ret);
3706 goto out;
3707 }
Tao Ma677b9752008-01-30 14:21:05 +08003708
Mark Fasheh328d5752007-06-18 10:48:04 -07003709 rec = &el->l_recs[split_index];
3710
3711 /*
3712 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003713 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003714 */
Tao Ma677b9752008-01-30 14:21:05 +08003715 ret = ocfs2_merge_rec_left(inode, path,
3716 handle, rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003717 dealloc, et,
Tao Ma677b9752008-01-30 14:21:05 +08003718 split_index);
3719
Mark Fasheh328d5752007-06-18 10:48:04 -07003720 if (ret) {
3721 mlog_errno(ret);
3722 goto out;
3723 }
3724
Tao Ma677b9752008-01-30 14:21:05 +08003725 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003726 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003727 /*
3728 * Error from this last rotate is not critical, so
3729 * print but don't bubble it up.
3730 */
3731 if (ret)
3732 mlog_errno(ret);
3733 ret = 0;
3734 } else {
3735 /*
3736 * Merge a record to the left or right.
3737 *
3738 * 'contig_type' is relative to the existing record,
3739 * so for example, if we're "right contig", it's to
3740 * the record on the left (hence the left merge).
3741 */
3742 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3743 ret = ocfs2_merge_rec_left(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003744 path,
3745 handle, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003746 dealloc, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003747 split_index);
3748 if (ret) {
3749 mlog_errno(ret);
3750 goto out;
3751 }
3752 } else {
3753 ret = ocfs2_merge_rec_right(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003754 path,
3755 handle, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003756 split_index);
3757 if (ret) {
3758 mlog_errno(ret);
3759 goto out;
3760 }
3761 }
3762
3763 if (ctxt->c_split_covers_rec) {
3764 /*
3765 * The merge may have left an empty extent in
3766 * our leaf. Try to rotate it away.
3767 */
Tao Ma677b9752008-01-30 14:21:05 +08003768 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003769 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003770 if (ret)
3771 mlog_errno(ret);
3772 ret = 0;
3773 }
3774 }
3775
3776out:
3777 return ret;
3778}
3779
3780static void ocfs2_subtract_from_rec(struct super_block *sb,
3781 enum ocfs2_split_type split,
3782 struct ocfs2_extent_rec *rec,
3783 struct ocfs2_extent_rec *split_rec)
3784{
3785 u64 len_blocks;
3786
3787 len_blocks = ocfs2_clusters_to_blocks(sb,
3788 le16_to_cpu(split_rec->e_leaf_clusters));
3789
3790 if (split == SPLIT_LEFT) {
3791 /*
3792 * Region is on the left edge of the existing
3793 * record.
3794 */
3795 le32_add_cpu(&rec->e_cpos,
3796 le16_to_cpu(split_rec->e_leaf_clusters));
3797 le64_add_cpu(&rec->e_blkno, len_blocks);
3798 le16_add_cpu(&rec->e_leaf_clusters,
3799 -le16_to_cpu(split_rec->e_leaf_clusters));
3800 } else {
3801 /*
3802 * Region is on the right edge of the existing
3803 * record.
3804 */
3805 le16_add_cpu(&rec->e_leaf_clusters,
3806 -le16_to_cpu(split_rec->e_leaf_clusters));
3807 }
3808}
3809
Mark Fashehdcd05382007-01-16 11:32:23 -08003810/*
3811 * Do the final bits of extent record insertion at the target leaf
3812 * list. If this leaf is part of an allocation tree, it is assumed
3813 * that the tree above has been prepared.
3814 */
3815static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3816 struct ocfs2_extent_list *el,
3817 struct ocfs2_insert_type *insert,
3818 struct inode *inode)
3819{
3820 int i = insert->ins_contig_index;
3821 unsigned int range;
3822 struct ocfs2_extent_rec *rec;
3823
Mark Fashehe48edee2007-03-07 16:46:57 -08003824 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003825
Mark Fasheh328d5752007-06-18 10:48:04 -07003826 if (insert->ins_split != SPLIT_NONE) {
3827 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3828 BUG_ON(i == -1);
3829 rec = &el->l_recs[i];
3830 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3831 insert_rec);
3832 goto rotate;
3833 }
3834
Mark Fashehdcd05382007-01-16 11:32:23 -08003835 /*
3836 * Contiguous insert - either left or right.
3837 */
3838 if (insert->ins_contig != CONTIG_NONE) {
3839 rec = &el->l_recs[i];
3840 if (insert->ins_contig == CONTIG_LEFT) {
3841 rec->e_blkno = insert_rec->e_blkno;
3842 rec->e_cpos = insert_rec->e_cpos;
3843 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003844 le16_add_cpu(&rec->e_leaf_clusters,
3845 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003846 return;
3847 }
3848
3849 /*
3850 * Handle insert into an empty leaf.
3851 */
3852 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3853 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3854 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3855 el->l_recs[0] = *insert_rec;
3856 el->l_next_free_rec = cpu_to_le16(1);
3857 return;
3858 }
3859
3860 /*
3861 * Appending insert.
3862 */
3863 if (insert->ins_appending == APPEND_TAIL) {
3864 i = le16_to_cpu(el->l_next_free_rec) - 1;
3865 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003866 range = le32_to_cpu(rec->e_cpos)
3867 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003868 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3869
3870 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3871 le16_to_cpu(el->l_count),
3872 "inode %lu, depth %u, count %u, next free %u, "
3873 "rec.cpos %u, rec.clusters %u, "
3874 "insert.cpos %u, insert.clusters %u\n",
3875 inode->i_ino,
3876 le16_to_cpu(el->l_tree_depth),
3877 le16_to_cpu(el->l_count),
3878 le16_to_cpu(el->l_next_free_rec),
3879 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003880 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003881 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003882 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003883 i++;
3884 el->l_recs[i] = *insert_rec;
3885 le16_add_cpu(&el->l_next_free_rec, 1);
3886 return;
3887 }
3888
Mark Fasheh328d5752007-06-18 10:48:04 -07003889rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003890 /*
3891 * Ok, we have to rotate.
3892 *
3893 * At this point, it is safe to assume that inserting into an
3894 * empty leaf and appending to a leaf have both been handled
3895 * above.
3896 *
3897 * This leaf needs to have space, either by the empty 1st
3898 * extent record, or by virtue of an l_next_rec < l_count.
3899 */
3900 ocfs2_rotate_leaf(el, insert_rec);
3901}
3902
Mark Fasheh328d5752007-06-18 10:48:04 -07003903static void ocfs2_adjust_rightmost_records(struct inode *inode,
3904 handle_t *handle,
3905 struct ocfs2_path *path,
3906 struct ocfs2_extent_rec *insert_rec)
3907{
3908 int ret, i, next_free;
3909 struct buffer_head *bh;
3910 struct ocfs2_extent_list *el;
3911 struct ocfs2_extent_rec *rec;
3912
3913 /*
3914 * Update everything except the leaf block.
3915 */
3916 for (i = 0; i < path->p_tree_depth; i++) {
3917 bh = path->p_node[i].bh;
3918 el = path->p_node[i].el;
3919
3920 next_free = le16_to_cpu(el->l_next_free_rec);
3921 if (next_free == 0) {
3922 ocfs2_error(inode->i_sb,
3923 "Dinode %llu has a bad extent list",
3924 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3925 ret = -EIO;
3926 return;
3927 }
3928
3929 rec = &el->l_recs[next_free - 1];
3930
3931 rec->e_int_clusters = insert_rec->e_cpos;
3932 le32_add_cpu(&rec->e_int_clusters,
3933 le16_to_cpu(insert_rec->e_leaf_clusters));
3934 le32_add_cpu(&rec->e_int_clusters,
3935 -le32_to_cpu(rec->e_cpos));
3936
3937 ret = ocfs2_journal_dirty(handle, bh);
3938 if (ret)
3939 mlog_errno(ret);
3940
3941 }
3942}
3943
Mark Fashehdcd05382007-01-16 11:32:23 -08003944static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3945 struct ocfs2_extent_rec *insert_rec,
3946 struct ocfs2_path *right_path,
3947 struct ocfs2_path **ret_left_path)
3948{
Mark Fasheh328d5752007-06-18 10:48:04 -07003949 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003950 struct ocfs2_extent_list *el;
3951 struct ocfs2_path *left_path = NULL;
3952
3953 *ret_left_path = NULL;
3954
3955 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003956 * This shouldn't happen for non-trees. The extent rec cluster
3957 * count manipulation below only works for interior nodes.
3958 */
3959 BUG_ON(right_path->p_tree_depth == 0);
3960
3961 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003962 * If our appending insert is at the leftmost edge of a leaf,
3963 * then we might need to update the rightmost records of the
3964 * neighboring path.
3965 */
3966 el = path_leaf_el(right_path);
3967 next_free = le16_to_cpu(el->l_next_free_rec);
3968 if (next_free == 0 ||
3969 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3970 u32 left_cpos;
3971
3972 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3973 &left_cpos);
3974 if (ret) {
3975 mlog_errno(ret);
3976 goto out;
3977 }
3978
3979 mlog(0, "Append may need a left path update. cpos: %u, "
3980 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3981 left_cpos);
3982
3983 /*
3984 * No need to worry if the append is already in the
3985 * leftmost leaf.
3986 */
3987 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07003988 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003989 if (!left_path) {
3990 ret = -ENOMEM;
3991 mlog_errno(ret);
3992 goto out;
3993 }
3994
3995 ret = ocfs2_find_path(inode, left_path, left_cpos);
3996 if (ret) {
3997 mlog_errno(ret);
3998 goto out;
3999 }
4000
4001 /*
4002 * ocfs2_insert_path() will pass the left_path to the
4003 * journal for us.
4004 */
4005 }
4006 }
4007
4008 ret = ocfs2_journal_access_path(inode, handle, right_path);
4009 if (ret) {
4010 mlog_errno(ret);
4011 goto out;
4012 }
4013
Mark Fasheh328d5752007-06-18 10:48:04 -07004014 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004015
4016 *ret_left_path = left_path;
4017 ret = 0;
4018out:
4019 if (ret != 0)
4020 ocfs2_free_path(left_path);
4021
4022 return ret;
4023}
4024
Mark Fasheh328d5752007-06-18 10:48:04 -07004025static void ocfs2_split_record(struct inode *inode,
4026 struct ocfs2_path *left_path,
4027 struct ocfs2_path *right_path,
4028 struct ocfs2_extent_rec *split_rec,
4029 enum ocfs2_split_type split)
4030{
4031 int index;
4032 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4033 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4034 struct ocfs2_extent_rec *rec, *tmprec;
4035
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004036 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004037 if (left_path)
4038 left_el = path_leaf_el(left_path);
4039
4040 el = right_el;
4041 insert_el = right_el;
4042 index = ocfs2_search_extent_list(el, cpos);
4043 if (index != -1) {
4044 if (index == 0 && left_path) {
4045 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4046
4047 /*
4048 * This typically means that the record
4049 * started in the left path but moved to the
4050 * right as a result of rotation. We either
4051 * move the existing record to the left, or we
4052 * do the later insert there.
4053 *
4054 * In this case, the left path should always
4055 * exist as the rotate code will have passed
4056 * it back for a post-insert update.
4057 */
4058
4059 if (split == SPLIT_LEFT) {
4060 /*
4061 * It's a left split. Since we know
4062 * that the rotate code gave us an
4063 * empty extent in the left path, we
4064 * can just do the insert there.
4065 */
4066 insert_el = left_el;
4067 } else {
4068 /*
4069 * Right split - we have to move the
4070 * existing record over to the left
4071 * leaf. The insert will be into the
4072 * newly created empty extent in the
4073 * right leaf.
4074 */
4075 tmprec = &right_el->l_recs[index];
4076 ocfs2_rotate_leaf(left_el, tmprec);
4077 el = left_el;
4078
4079 memset(tmprec, 0, sizeof(*tmprec));
4080 index = ocfs2_search_extent_list(left_el, cpos);
4081 BUG_ON(index == -1);
4082 }
4083 }
4084 } else {
4085 BUG_ON(!left_path);
4086 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4087 /*
4088 * Left path is easy - we can just allow the insert to
4089 * happen.
4090 */
4091 el = left_el;
4092 insert_el = left_el;
4093 index = ocfs2_search_extent_list(el, cpos);
4094 BUG_ON(index == -1);
4095 }
4096
4097 rec = &el->l_recs[index];
4098 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
4099 ocfs2_rotate_leaf(insert_el, split_rec);
4100}
4101
Mark Fashehdcd05382007-01-16 11:32:23 -08004102/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004103 * This function only does inserts on an allocation b-tree. For tree
4104 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004105 *
4106 * right_path is the path we want to do the actual insert
4107 * in. left_path should only be passed in if we need to update that
4108 * portion of the tree after an edge insert.
4109 */
4110static int ocfs2_insert_path(struct inode *inode,
4111 handle_t *handle,
4112 struct ocfs2_path *left_path,
4113 struct ocfs2_path *right_path,
4114 struct ocfs2_extent_rec *insert_rec,
4115 struct ocfs2_insert_type *insert)
4116{
4117 int ret, subtree_index;
4118 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004119
Mark Fashehdcd05382007-01-16 11:32:23 -08004120 if (left_path) {
4121 int credits = handle->h_buffer_credits;
4122
4123 /*
4124 * There's a chance that left_path got passed back to
4125 * us without being accounted for in the
4126 * journal. Extend our transaction here to be sure we
4127 * can change those blocks.
4128 */
4129 credits += left_path->p_tree_depth;
4130
4131 ret = ocfs2_extend_trans(handle, credits);
4132 if (ret < 0) {
4133 mlog_errno(ret);
4134 goto out;
4135 }
4136
4137 ret = ocfs2_journal_access_path(inode, handle, left_path);
4138 if (ret < 0) {
4139 mlog_errno(ret);
4140 goto out;
4141 }
4142 }
4143
Mark Fashehe8aed342007-12-03 16:43:01 -08004144 /*
4145 * Pass both paths to the journal. The majority of inserts
4146 * will be touching all components anyway.
4147 */
4148 ret = ocfs2_journal_access_path(inode, handle, right_path);
4149 if (ret < 0) {
4150 mlog_errno(ret);
4151 goto out;
4152 }
4153
Mark Fasheh328d5752007-06-18 10:48:04 -07004154 if (insert->ins_split != SPLIT_NONE) {
4155 /*
4156 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004157 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004158 * function sort it all out.
4159 */
4160 ocfs2_split_record(inode, left_path, right_path,
4161 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004162
4163 /*
4164 * Split might have modified either leaf and we don't
4165 * have a guarantee that the later edge insert will
4166 * dirty this for us.
4167 */
4168 if (left_path)
4169 ret = ocfs2_journal_dirty(handle,
4170 path_leaf_bh(left_path));
4171 if (ret)
4172 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07004173 } else
4174 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
4175 insert, inode);
Mark Fashehdcd05382007-01-16 11:32:23 -08004176
Mark Fashehdcd05382007-01-16 11:32:23 -08004177 ret = ocfs2_journal_dirty(handle, leaf_bh);
4178 if (ret)
4179 mlog_errno(ret);
4180
4181 if (left_path) {
4182 /*
4183 * The rotate code has indicated that we need to fix
4184 * up portions of the tree after the insert.
4185 *
4186 * XXX: Should we extend the transaction here?
4187 */
4188 subtree_index = ocfs2_find_subtree_root(inode, left_path,
4189 right_path);
4190 ocfs2_complete_edge_insert(inode, handle, left_path,
4191 right_path, subtree_index);
4192 }
4193
4194 ret = 0;
4195out:
4196 return ret;
4197}
4198
4199static int ocfs2_do_insert_extent(struct inode *inode,
4200 handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004201 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004202 struct ocfs2_extent_rec *insert_rec,
4203 struct ocfs2_insert_type *type)
4204{
4205 int ret, rotate = 0;
4206 u32 cpos;
4207 struct ocfs2_path *right_path = NULL;
4208 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004209 struct ocfs2_extent_list *el;
4210
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004211 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004212
Joel Becker13723d02008-10-17 19:25:01 -07004213 ret = ocfs2_et_root_journal_access(handle, inode, et,
4214 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004215 if (ret) {
4216 mlog_errno(ret);
4217 goto out;
4218 }
4219
4220 if (le16_to_cpu(el->l_tree_depth) == 0) {
4221 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4222 goto out_update_clusters;
4223 }
4224
Joel Beckerffdd7a52008-10-17 22:32:01 -07004225 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004226 if (!right_path) {
4227 ret = -ENOMEM;
4228 mlog_errno(ret);
4229 goto out;
4230 }
4231
4232 /*
4233 * Determine the path to start with. Rotations need the
4234 * rightmost path, everything else can go directly to the
4235 * target leaf.
4236 */
4237 cpos = le32_to_cpu(insert_rec->e_cpos);
4238 if (type->ins_appending == APPEND_NONE &&
4239 type->ins_contig == CONTIG_NONE) {
4240 rotate = 1;
4241 cpos = UINT_MAX;
4242 }
4243
4244 ret = ocfs2_find_path(inode, right_path, cpos);
4245 if (ret) {
4246 mlog_errno(ret);
4247 goto out;
4248 }
4249
4250 /*
4251 * Rotations and appends need special treatment - they modify
4252 * parts of the tree's above them.
4253 *
4254 * Both might pass back a path immediate to the left of the
4255 * one being inserted to. This will be cause
4256 * ocfs2_insert_path() to modify the rightmost records of
4257 * left_path to account for an edge insert.
4258 *
4259 * XXX: When modifying this code, keep in mind that an insert
4260 * can wind up skipping both of these two special cases...
4261 */
4262 if (rotate) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004263 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004264 le32_to_cpu(insert_rec->e_cpos),
4265 right_path, &left_path);
4266 if (ret) {
4267 mlog_errno(ret);
4268 goto out;
4269 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004270
4271 /*
4272 * ocfs2_rotate_tree_right() might have extended the
4273 * transaction without re-journaling our tree root.
4274 */
Joel Becker13723d02008-10-17 19:25:01 -07004275 ret = ocfs2_et_root_journal_access(handle, inode, et,
4276 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004277 if (ret) {
4278 mlog_errno(ret);
4279 goto out;
4280 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004281 } else if (type->ins_appending == APPEND_TAIL
4282 && type->ins_contig != CONTIG_LEFT) {
4283 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4284 right_path, &left_path);
4285 if (ret) {
4286 mlog_errno(ret);
4287 goto out;
4288 }
4289 }
4290
4291 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4292 insert_rec, type);
4293 if (ret) {
4294 mlog_errno(ret);
4295 goto out;
4296 }
4297
4298out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004299 if (type->ins_split == SPLIT_NONE)
Joel Becker35dc0aa2008-08-20 16:25:06 -07004300 ocfs2_et_update_clusters(inode, et,
4301 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004302
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004303 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004304 if (ret)
4305 mlog_errno(ret);
4306
4307out:
4308 ocfs2_free_path(left_path);
4309 ocfs2_free_path(right_path);
4310
4311 return ret;
4312}
4313
Mark Fasheh328d5752007-06-18 10:48:04 -07004314static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08004315ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004316 struct ocfs2_extent_list *el, int index,
4317 struct ocfs2_extent_rec *split_rec)
4318{
Tao Maad5a4d72008-01-30 14:21:32 +08004319 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004320 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004321 u32 left_cpos, right_cpos;
4322 struct ocfs2_extent_rec *rec = NULL;
4323 struct ocfs2_extent_list *new_el;
4324 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4325 struct buffer_head *bh;
4326 struct ocfs2_extent_block *eb;
4327
4328 if (index > 0) {
4329 rec = &el->l_recs[index - 1];
4330 } else if (path->p_tree_depth > 0) {
4331 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4332 path, &left_cpos);
4333 if (status)
4334 goto out;
4335
4336 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004337 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004338 if (!left_path)
4339 goto out;
4340
4341 status = ocfs2_find_path(inode, left_path, left_cpos);
4342 if (status)
4343 goto out;
4344
4345 new_el = path_leaf_el(left_path);
4346
4347 if (le16_to_cpu(new_el->l_next_free_rec) !=
4348 le16_to_cpu(new_el->l_count)) {
4349 bh = path_leaf_bh(left_path);
4350 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004351 ocfs2_error(inode->i_sb,
4352 "Extent block #%llu has an "
4353 "invalid l_next_free_rec of "
4354 "%d. It should have "
4355 "matched the l_count of %d",
4356 (unsigned long long)le64_to_cpu(eb->h_blkno),
4357 le16_to_cpu(new_el->l_next_free_rec),
4358 le16_to_cpu(new_el->l_count));
4359 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004360 goto out;
4361 }
4362 rec = &new_el->l_recs[
4363 le16_to_cpu(new_el->l_next_free_rec) - 1];
4364 }
4365 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004366
4367 /*
4368 * We're careful to check for an empty extent record here -
4369 * the merge code will know what to do if it sees one.
4370 */
Tao Maad5a4d72008-01-30 14:21:32 +08004371 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004372 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4373 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4374 ret = CONTIG_RIGHT;
4375 } else {
4376 ret = ocfs2_extent_contig(inode, rec, split_rec);
4377 }
4378 }
4379
Tao Maad5a4d72008-01-30 14:21:32 +08004380 rec = NULL;
4381 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4382 rec = &el->l_recs[index + 1];
4383 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4384 path->p_tree_depth > 0) {
4385 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4386 path, &right_cpos);
4387 if (status)
4388 goto out;
4389
4390 if (right_cpos == 0)
4391 goto out;
4392
Joel Beckerffdd7a52008-10-17 22:32:01 -07004393 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004394 if (!right_path)
4395 goto out;
4396
4397 status = ocfs2_find_path(inode, right_path, right_cpos);
4398 if (status)
4399 goto out;
4400
4401 new_el = path_leaf_el(right_path);
4402 rec = &new_el->l_recs[0];
4403 if (ocfs2_is_empty_extent(rec)) {
4404 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4405 bh = path_leaf_bh(right_path);
4406 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004407 ocfs2_error(inode->i_sb,
4408 "Extent block #%llu has an "
4409 "invalid l_next_free_rec of %d",
4410 (unsigned long long)le64_to_cpu(eb->h_blkno),
4411 le16_to_cpu(new_el->l_next_free_rec));
4412 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004413 goto out;
4414 }
4415 rec = &new_el->l_recs[1];
4416 }
4417 }
4418
4419 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004420 enum ocfs2_contig_type contig_type;
4421
Mark Fasheh328d5752007-06-18 10:48:04 -07004422 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4423
4424 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4425 ret = CONTIG_LEFTRIGHT;
4426 else if (ret == CONTIG_NONE)
4427 ret = contig_type;
4428 }
4429
Tao Maad5a4d72008-01-30 14:21:32 +08004430out:
4431 if (left_path)
4432 ocfs2_free_path(left_path);
4433 if (right_path)
4434 ocfs2_free_path(right_path);
4435
Mark Fasheh328d5752007-06-18 10:48:04 -07004436 return ret;
4437}
4438
Mark Fashehdcd05382007-01-16 11:32:23 -08004439static void ocfs2_figure_contig_type(struct inode *inode,
4440 struct ocfs2_insert_type *insert,
4441 struct ocfs2_extent_list *el,
Tao Maca12b7c2008-08-18 17:38:52 +08004442 struct ocfs2_extent_rec *insert_rec,
4443 struct ocfs2_extent_tree *et)
Mark Fashehdcd05382007-01-16 11:32:23 -08004444{
4445 int i;
4446 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4447
Mark Fashehe48edee2007-03-07 16:46:57 -08004448 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4449
Mark Fashehdcd05382007-01-16 11:32:23 -08004450 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4451 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4452 insert_rec);
4453 if (contig_type != CONTIG_NONE) {
4454 insert->ins_contig_index = i;
4455 break;
4456 }
4457 }
4458 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004459
4460 if (insert->ins_contig != CONTIG_NONE) {
4461 struct ocfs2_extent_rec *rec =
4462 &el->l_recs[insert->ins_contig_index];
4463 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4464 le16_to_cpu(insert_rec->e_leaf_clusters);
4465
4466 /*
4467 * Caller might want us to limit the size of extents, don't
4468 * calculate contiguousness if we might exceed that limit.
4469 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004470 if (et->et_max_leaf_clusters &&
4471 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004472 insert->ins_contig = CONTIG_NONE;
4473 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004474}
4475
4476/*
4477 * This should only be called against the righmost leaf extent list.
4478 *
4479 * ocfs2_figure_appending_type() will figure out whether we'll have to
4480 * insert at the tail of the rightmost leaf.
4481 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004482 * This should also work against the root extent list for tree's with 0
4483 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004484 * then the logic here makes sense.
4485 */
4486static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4487 struct ocfs2_extent_list *el,
4488 struct ocfs2_extent_rec *insert_rec)
4489{
4490 int i;
4491 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4492 struct ocfs2_extent_rec *rec;
4493
4494 insert->ins_appending = APPEND_NONE;
4495
Mark Fashehe48edee2007-03-07 16:46:57 -08004496 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004497
4498 if (!el->l_next_free_rec)
4499 goto set_tail_append;
4500
4501 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4502 /* Were all records empty? */
4503 if (le16_to_cpu(el->l_next_free_rec) == 1)
4504 goto set_tail_append;
4505 }
4506
4507 i = le16_to_cpu(el->l_next_free_rec) - 1;
4508 rec = &el->l_recs[i];
4509
Mark Fashehe48edee2007-03-07 16:46:57 -08004510 if (cpos >=
4511 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004512 goto set_tail_append;
4513
4514 return;
4515
4516set_tail_append:
4517 insert->ins_appending = APPEND_TAIL;
4518}
4519
4520/*
4521 * Helper function called at the begining of an insert.
4522 *
4523 * This computes a few things that are commonly used in the process of
4524 * inserting into the btree:
4525 * - Whether the new extent is contiguous with an existing one.
4526 * - The current tree depth.
4527 * - Whether the insert is an appending one.
4528 * - The total # of free records in the tree.
4529 *
4530 * All of the information is stored on the ocfs2_insert_type
4531 * structure.
4532 */
4533static int ocfs2_figure_insert_type(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004534 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004535 struct buffer_head **last_eb_bh,
4536 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004537 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004538 struct ocfs2_insert_type *insert)
4539{
4540 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004541 struct ocfs2_extent_block *eb;
4542 struct ocfs2_extent_list *el;
4543 struct ocfs2_path *path = NULL;
4544 struct buffer_head *bh = NULL;
4545
Mark Fasheh328d5752007-06-18 10:48:04 -07004546 insert->ins_split = SPLIT_NONE;
4547
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004548 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004549 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4550
4551 if (el->l_tree_depth) {
4552 /*
4553 * If we have tree depth, we read in the
4554 * rightmost extent block ahead of time as
4555 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4556 * may want it later.
4557 */
Joel Becker5e965812008-11-13 14:49:16 -08004558 ret = ocfs2_read_extent_block(inode,
4559 ocfs2_et_get_last_eb_blk(et),
4560 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004561 if (ret) {
4562 mlog_exit(ret);
4563 goto out;
4564 }
4565 eb = (struct ocfs2_extent_block *) bh->b_data;
4566 el = &eb->h_list;
4567 }
4568
4569 /*
4570 * Unless we have a contiguous insert, we'll need to know if
4571 * there is room left in our allocation tree for another
4572 * extent record.
4573 *
4574 * XXX: This test is simplistic, we can search for empty
4575 * extent records too.
4576 */
Tao Maoc77534f2007-08-28 17:22:33 -07004577 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004578 le16_to_cpu(el->l_next_free_rec);
4579
4580 if (!insert->ins_tree_depth) {
Tao Maca12b7c2008-08-18 17:38:52 +08004581 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004582 ocfs2_figure_appending_type(insert, el, insert_rec);
4583 return 0;
4584 }
4585
Joel Beckerffdd7a52008-10-17 22:32:01 -07004586 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004587 if (!path) {
4588 ret = -ENOMEM;
4589 mlog_errno(ret);
4590 goto out;
4591 }
4592
4593 /*
4594 * In the case that we're inserting past what the tree
4595 * currently accounts for, ocfs2_find_path() will return for
4596 * us the rightmost tree path. This is accounted for below in
4597 * the appending code.
4598 */
4599 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4600 if (ret) {
4601 mlog_errno(ret);
4602 goto out;
4603 }
4604
4605 el = path_leaf_el(path);
4606
4607 /*
4608 * Now that we have the path, there's two things we want to determine:
4609 * 1) Contiguousness (also set contig_index if this is so)
4610 *
4611 * 2) Are we doing an append? We can trivially break this up
4612 * into two types of appends: simple record append, or a
4613 * rotate inside the tail leaf.
4614 */
Tao Maca12b7c2008-08-18 17:38:52 +08004615 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004616
4617 /*
4618 * The insert code isn't quite ready to deal with all cases of
4619 * left contiguousness. Specifically, if it's an insert into
4620 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004621 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004622 * left. For now, just catch that case and fool the layers
4623 * above us. This works just fine for tree_depth == 0, which
4624 * is why we allow that above.
4625 */
4626 if (insert->ins_contig == CONTIG_LEFT &&
4627 insert->ins_contig_index == 0)
4628 insert->ins_contig = CONTIG_NONE;
4629
4630 /*
4631 * Ok, so we can simply compare against last_eb to figure out
4632 * whether the path doesn't exist. This will only happen in
4633 * the case that we're doing a tail append, so maybe we can
4634 * take advantage of that information somehow.
4635 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004636 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004637 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004638 /*
4639 * Ok, ocfs2_find_path() returned us the rightmost
4640 * tree path. This might be an appending insert. There are
4641 * two cases:
4642 * 1) We're doing a true append at the tail:
4643 * -This might even be off the end of the leaf
4644 * 2) We're "appending" by rotating in the tail
4645 */
4646 ocfs2_figure_appending_type(insert, el, insert_rec);
4647 }
4648
4649out:
4650 ocfs2_free_path(path);
4651
4652 if (ret == 0)
4653 *last_eb_bh = bh;
4654 else
4655 brelse(bh);
4656 return ret;
4657}
4658
4659/*
4660 * Insert an extent into an inode btree.
4661 *
4662 * The caller needs to update fe->i_clusters
4663 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004664int ocfs2_insert_extent(struct ocfs2_super *osb,
4665 handle_t *handle,
4666 struct inode *inode,
4667 struct ocfs2_extent_tree *et,
4668 u32 cpos,
4669 u64 start_blk,
4670 u32 new_clusters,
4671 u8 flags,
4672 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004673{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004674 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004675 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004676 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004677 struct ocfs2_insert_type insert = {0, };
4678 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004679
Mark Fashehdcd05382007-01-16 11:32:23 -08004680 mlog(0, "add %u clusters at position %u to inode %llu\n",
4681 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004682
Mark Fashehe48edee2007-03-07 16:46:57 -08004683 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004684 rec.e_cpos = cpu_to_le32(cpos);
4685 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004686 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004687 rec.e_flags = flags;
Joel Becker1e61ee72008-08-20 18:32:45 -07004688 status = ocfs2_et_insert_check(inode, et, &rec);
4689 if (status) {
4690 mlog_errno(status);
4691 goto bail;
4692 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004693
Tao Mae7d4cb62008-08-18 17:38:44 +08004694 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004695 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004696 if (status < 0) {
4697 mlog_errno(status);
4698 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004699 }
4700
Mark Fashehdcd05382007-01-16 11:32:23 -08004701 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4702 "Insert.contig_index: %d, Insert.free_records: %d, "
4703 "Insert.tree_depth: %d\n",
4704 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004705 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004706
Tao Maoc77534f2007-08-28 17:22:33 -07004707 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004708 status = ocfs2_grow_tree(inode, handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004709 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004710 meta_ac);
4711 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004712 mlog_errno(status);
4713 goto bail;
4714 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004715 }
4716
Mark Fashehdcd05382007-01-16 11:32:23 -08004717 /* Finally, we can add clusters. This might rotate the tree for us. */
Tao Mae7d4cb62008-08-18 17:38:44 +08004718 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004719 if (status < 0)
4720 mlog_errno(status);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004721 else if (et->et_ops == &ocfs2_dinode_et_ops)
Mark Fasheh83418972007-04-23 18:53:12 -07004722 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004723
4724bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004725 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004726
Tao Maf56654c2008-08-18 17:38:48 +08004727 mlog_exit(status);
4728 return status;
4729}
4730
Tao Ma0eb8d472008-08-18 17:38:45 +08004731/*
4732 * Allcate and add clusters into the extent b-tree.
4733 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004734 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004735 * it is not limited to the file storage. Any extent tree can use this
4736 * function if it implements the proper ocfs2_extent_tree.
4737 */
4738int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4739 struct inode *inode,
4740 u32 *logical_offset,
4741 u32 clusters_to_add,
4742 int mark_unwritten,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004743 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004744 handle_t *handle,
4745 struct ocfs2_alloc_context *data_ac,
4746 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004747 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004748{
4749 int status = 0;
4750 int free_extents;
4751 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4752 u32 bit_off, num_bits;
4753 u64 block;
4754 u8 flags = 0;
4755
4756 BUG_ON(!clusters_to_add);
4757
4758 if (mark_unwritten)
4759 flags = OCFS2_EXT_UNWRITTEN;
4760
Joel Beckerf99b9b72008-08-20 19:36:33 -07004761 free_extents = ocfs2_num_free_extents(osb, inode, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004762 if (free_extents < 0) {
4763 status = free_extents;
4764 mlog_errno(status);
4765 goto leave;
4766 }
4767
4768 /* there are two cases which could cause us to EAGAIN in the
4769 * we-need-more-metadata case:
4770 * 1) we haven't reserved *any*
4771 * 2) we are so fragmented, we've needed to add metadata too
4772 * many times. */
4773 if (!free_extents && !meta_ac) {
4774 mlog(0, "we haven't reserved any metadata!\n");
4775 status = -EAGAIN;
4776 reason = RESTART_META;
4777 goto leave;
4778 } else if ((!free_extents)
4779 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004780 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004781 mlog(0, "filesystem is really fragmented...\n");
4782 status = -EAGAIN;
4783 reason = RESTART_META;
4784 goto leave;
4785 }
4786
4787 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4788 clusters_to_add, &bit_off, &num_bits);
4789 if (status < 0) {
4790 if (status != -ENOSPC)
4791 mlog_errno(status);
4792 goto leave;
4793 }
4794
4795 BUG_ON(num_bits > clusters_to_add);
4796
Joel Becker13723d02008-10-17 19:25:01 -07004797 /* reserve our write early -- insert_extent may update the tree root */
4798 status = ocfs2_et_root_journal_access(handle, inode, et,
4799 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d472008-08-18 17:38:45 +08004800 if (status < 0) {
4801 mlog_errno(status);
4802 goto leave;
4803 }
4804
4805 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4806 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4807 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004808 status = ocfs2_insert_extent(osb, handle, inode, et,
4809 *logical_offset, block,
4810 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004811 if (status < 0) {
4812 mlog_errno(status);
4813 goto leave;
4814 }
4815
Joel Beckerf99b9b72008-08-20 19:36:33 -07004816 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004817 if (status < 0) {
4818 mlog_errno(status);
4819 goto leave;
4820 }
4821
4822 clusters_to_add -= num_bits;
4823 *logical_offset += num_bits;
4824
4825 if (clusters_to_add) {
4826 mlog(0, "need to alloc once more, wanted = %u\n",
4827 clusters_to_add);
4828 status = -EAGAIN;
4829 reason = RESTART_TRANS;
4830 }
4831
4832leave:
4833 mlog_exit(status);
4834 if (reason_ret)
4835 *reason_ret = reason;
4836 return status;
4837}
4838
Mark Fasheh328d5752007-06-18 10:48:04 -07004839static void ocfs2_make_right_split_rec(struct super_block *sb,
4840 struct ocfs2_extent_rec *split_rec,
4841 u32 cpos,
4842 struct ocfs2_extent_rec *rec)
4843{
4844 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4845 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4846
4847 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4848
4849 split_rec->e_cpos = cpu_to_le32(cpos);
4850 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4851
4852 split_rec->e_blkno = rec->e_blkno;
4853 le64_add_cpu(&split_rec->e_blkno,
4854 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4855
4856 split_rec->e_flags = rec->e_flags;
4857}
4858
4859static int ocfs2_split_and_insert(struct inode *inode,
4860 handle_t *handle,
4861 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004862 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004863 struct buffer_head **last_eb_bh,
4864 int split_index,
4865 struct ocfs2_extent_rec *orig_split_rec,
4866 struct ocfs2_alloc_context *meta_ac)
4867{
4868 int ret = 0, depth;
4869 unsigned int insert_range, rec_range, do_leftright = 0;
4870 struct ocfs2_extent_rec tmprec;
4871 struct ocfs2_extent_list *rightmost_el;
4872 struct ocfs2_extent_rec rec;
4873 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4874 struct ocfs2_insert_type insert;
4875 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004876
4877leftright:
4878 /*
4879 * Store a copy of the record on the stack - it might move
4880 * around as the tree is manipulated below.
4881 */
4882 rec = path_leaf_el(path)->l_recs[split_index];
4883
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004884 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004885
4886 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4887 if (depth) {
4888 BUG_ON(!(*last_eb_bh));
4889 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4890 rightmost_el = &eb->h_list;
4891 }
4892
4893 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4894 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004895 ret = ocfs2_grow_tree(inode, handle, et,
4896 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004897 if (ret) {
4898 mlog_errno(ret);
4899 goto out;
4900 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004901 }
4902
4903 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4904 insert.ins_appending = APPEND_NONE;
4905 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004906 insert.ins_tree_depth = depth;
4907
4908 insert_range = le32_to_cpu(split_rec.e_cpos) +
4909 le16_to_cpu(split_rec.e_leaf_clusters);
4910 rec_range = le32_to_cpu(rec.e_cpos) +
4911 le16_to_cpu(rec.e_leaf_clusters);
4912
4913 if (split_rec.e_cpos == rec.e_cpos) {
4914 insert.ins_split = SPLIT_LEFT;
4915 } else if (insert_range == rec_range) {
4916 insert.ins_split = SPLIT_RIGHT;
4917 } else {
4918 /*
4919 * Left/right split. We fake this as a right split
4920 * first and then make a second pass as a left split.
4921 */
4922 insert.ins_split = SPLIT_RIGHT;
4923
4924 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4925 &rec);
4926
4927 split_rec = tmprec;
4928
4929 BUG_ON(do_leftright);
4930 do_leftright = 1;
4931 }
4932
Tao Mae7d4cb62008-08-18 17:38:44 +08004933 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004934 if (ret) {
4935 mlog_errno(ret);
4936 goto out;
4937 }
4938
4939 if (do_leftright == 1) {
4940 u32 cpos;
4941 struct ocfs2_extent_list *el;
4942
4943 do_leftright++;
4944 split_rec = *orig_split_rec;
4945
4946 ocfs2_reinit_path(path, 1);
4947
4948 cpos = le32_to_cpu(split_rec.e_cpos);
4949 ret = ocfs2_find_path(inode, path, cpos);
4950 if (ret) {
4951 mlog_errno(ret);
4952 goto out;
4953 }
4954
4955 el = path_leaf_el(path);
4956 split_index = ocfs2_search_extent_list(el, cpos);
4957 goto leftright;
4958 }
4959out:
4960
4961 return ret;
4962}
4963
Tao Ma47be12e2009-01-09 07:32:48 +08004964static int ocfs2_replace_extent_rec(struct inode *inode,
4965 handle_t *handle,
4966 struct ocfs2_path *path,
4967 struct ocfs2_extent_list *el,
4968 int split_index,
4969 struct ocfs2_extent_rec *split_rec)
4970{
4971 int ret;
4972
4973 ret = ocfs2_path_bh_journal_access(handle, inode, path,
4974 path_num_items(path) - 1);
4975 if (ret) {
4976 mlog_errno(ret);
4977 goto out;
4978 }
4979
4980 el->l_recs[split_index] = *split_rec;
4981
4982 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4983out:
4984 return ret;
4985}
4986
Mark Fasheh328d5752007-06-18 10:48:04 -07004987/*
4988 * Mark part or all of the extent record at split_index in the leaf
4989 * pointed to by path as written. This removes the unwritten
4990 * extent flag.
4991 *
4992 * Care is taken to handle contiguousness so as to not grow the tree.
4993 *
4994 * meta_ac is not strictly necessary - we only truly need it if growth
4995 * of the tree is required. All other cases will degrade into a less
4996 * optimal tree layout.
4997 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004998 * last_eb_bh should be the rightmost leaf block for any extent
4999 * btree. Since a split may grow the tree or a merge might shrink it,
5000 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07005001 *
5002 * This code is optimized for readability - several passes might be
5003 * made over certain portions of the tree. All of those blocks will
5004 * have been brought into cache (and pinned via the journal), so the
5005 * extra overhead is not expressed in terms of disk reads.
5006 */
5007static int __ocfs2_mark_extent_written(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08005008 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005009 handle_t *handle,
5010 struct ocfs2_path *path,
5011 int split_index,
5012 struct ocfs2_extent_rec *split_rec,
5013 struct ocfs2_alloc_context *meta_ac,
5014 struct ocfs2_cached_dealloc_ctxt *dealloc)
5015{
5016 int ret = 0;
5017 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08005018 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07005019 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5020 struct ocfs2_merge_ctxt ctxt;
5021 struct ocfs2_extent_list *rightmost_el;
5022
Roel Kluin3cf0c502007-10-27 00:20:36 +02005023 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07005024 ret = -EIO;
5025 mlog_errno(ret);
5026 goto out;
5027 }
5028
5029 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5030 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5031 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5032 ret = -EIO;
5033 mlog_errno(ret);
5034 goto out;
5035 }
5036
Tao Maad5a4d72008-01-30 14:21:32 +08005037 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005038 split_index,
5039 split_rec);
5040
5041 /*
5042 * The core merge / split code wants to know how much room is
5043 * left in this inodes allocation tree, so we pass the
5044 * rightmost extent list.
5045 */
5046 if (path->p_tree_depth) {
5047 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005048
Joel Becker5e965812008-11-13 14:49:16 -08005049 ret = ocfs2_read_extent_block(inode,
5050 ocfs2_et_get_last_eb_blk(et),
5051 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005052 if (ret) {
5053 mlog_exit(ret);
5054 goto out;
5055 }
5056
5057 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005058 rightmost_el = &eb->h_list;
5059 } else
5060 rightmost_el = path_root_el(path);
5061
Mark Fasheh328d5752007-06-18 10:48:04 -07005062 if (rec->e_cpos == split_rec->e_cpos &&
5063 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5064 ctxt.c_split_covers_rec = 1;
5065 else
5066 ctxt.c_split_covers_rec = 0;
5067
5068 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5069
Mark Fasheh015452b2007-09-12 10:21:22 -07005070 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5071 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5072 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005073
5074 if (ctxt.c_contig_type == CONTIG_NONE) {
5075 if (ctxt.c_split_covers_rec)
Tao Ma47be12e2009-01-09 07:32:48 +08005076 ret = ocfs2_replace_extent_rec(inode, handle,
5077 path, el,
5078 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005079 else
Tao Mae7d4cb62008-08-18 17:38:44 +08005080 ret = ocfs2_split_and_insert(inode, handle, path, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005081 &last_eb_bh, split_index,
5082 split_rec, meta_ac);
5083 if (ret)
5084 mlog_errno(ret);
5085 } else {
5086 ret = ocfs2_try_to_merge_extent(inode, handle, path,
5087 split_index, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08005088 dealloc, &ctxt, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005089 if (ret)
5090 mlog_errno(ret);
5091 }
5092
Mark Fasheh328d5752007-06-18 10:48:04 -07005093out:
5094 brelse(last_eb_bh);
5095 return ret;
5096}
5097
5098/*
5099 * Mark the already-existing extent at cpos as written for len clusters.
5100 *
5101 * If the existing extent is larger than the request, initiate a
5102 * split. An attempt will be made at merging with adjacent extents.
5103 *
5104 * The caller is responsible for passing down meta_ac if we'll need it.
5105 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005106int ocfs2_mark_extent_written(struct inode *inode,
5107 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005108 handle_t *handle, u32 cpos, u32 len, u32 phys,
5109 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005110 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005111{
5112 int ret, index;
5113 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
5114 struct ocfs2_extent_rec split_rec;
5115 struct ocfs2_path *left_path = NULL;
5116 struct ocfs2_extent_list *el;
5117
5118 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5119 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
5120
5121 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5122 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5123 "that are being written to, but the feature bit "
5124 "is not set in the super block.",
5125 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5126 ret = -EROFS;
5127 goto out;
5128 }
5129
5130 /*
5131 * XXX: This should be fixed up so that we just re-insert the
5132 * next extent records.
Joel Beckerf99b9b72008-08-20 19:36:33 -07005133 *
5134 * XXX: This is a hack on the extent tree, maybe it should be
5135 * an op?
Mark Fasheh328d5752007-06-18 10:48:04 -07005136 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005137 if (et->et_ops == &ocfs2_dinode_et_ops)
Tao Mae7d4cb62008-08-18 17:38:44 +08005138 ocfs2_extent_map_trunc(inode, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07005139
Joel Beckerffdd7a52008-10-17 22:32:01 -07005140 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005141 if (!left_path) {
5142 ret = -ENOMEM;
5143 mlog_errno(ret);
5144 goto out;
5145 }
5146
5147 ret = ocfs2_find_path(inode, left_path, cpos);
5148 if (ret) {
5149 mlog_errno(ret);
5150 goto out;
5151 }
5152 el = path_leaf_el(left_path);
5153
5154 index = ocfs2_search_extent_list(el, cpos);
5155 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5156 ocfs2_error(inode->i_sb,
5157 "Inode %llu has an extent at cpos %u which can no "
5158 "longer be found.\n",
5159 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5160 ret = -EROFS;
5161 goto out;
5162 }
5163
5164 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5165 split_rec.e_cpos = cpu_to_le32(cpos);
5166 split_rec.e_leaf_clusters = cpu_to_le16(len);
5167 split_rec.e_blkno = cpu_to_le64(start_blkno);
5168 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
5169 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
5170
Joel Beckerf99b9b72008-08-20 19:36:33 -07005171 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
Tao Mae7d4cb62008-08-18 17:38:44 +08005172 index, &split_rec, meta_ac,
5173 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005174 if (ret)
5175 mlog_errno(ret);
5176
5177out:
5178 ocfs2_free_path(left_path);
5179 return ret;
5180}
5181
Tao Mae7d4cb62008-08-18 17:38:44 +08005182static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005183 handle_t *handle, struct ocfs2_path *path,
5184 int index, u32 new_range,
5185 struct ocfs2_alloc_context *meta_ac)
5186{
5187 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005188 struct buffer_head *last_eb_bh = NULL;
5189 struct ocfs2_extent_block *eb;
5190 struct ocfs2_extent_list *rightmost_el, *el;
5191 struct ocfs2_extent_rec split_rec;
5192 struct ocfs2_extent_rec *rec;
5193 struct ocfs2_insert_type insert;
5194
5195 /*
5196 * Setup the record to split before we grow the tree.
5197 */
5198 el = path_leaf_el(path);
5199 rec = &el->l_recs[index];
5200 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5201
5202 depth = path->p_tree_depth;
5203 if (depth > 0) {
Joel Becker5e965812008-11-13 14:49:16 -08005204 ret = ocfs2_read_extent_block(inode,
5205 ocfs2_et_get_last_eb_blk(et),
5206 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005207 if (ret < 0) {
5208 mlog_errno(ret);
5209 goto out;
5210 }
5211
5212 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5213 rightmost_el = &eb->h_list;
5214 } else
5215 rightmost_el = path_leaf_el(path);
5216
Tao Ma811f9332008-08-18 17:38:43 +08005217 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005218 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005219 ret = ocfs2_extend_trans(handle, credits);
5220 if (ret) {
5221 mlog_errno(ret);
5222 goto out;
5223 }
5224
5225 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5226 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005227 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005228 meta_ac);
5229 if (ret) {
5230 mlog_errno(ret);
5231 goto out;
5232 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005233 }
5234
5235 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5236 insert.ins_appending = APPEND_NONE;
5237 insert.ins_contig = CONTIG_NONE;
5238 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005239 insert.ins_tree_depth = depth;
5240
Tao Mae7d4cb62008-08-18 17:38:44 +08005241 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005242 if (ret)
5243 mlog_errno(ret);
5244
5245out:
5246 brelse(last_eb_bh);
5247 return ret;
5248}
5249
5250static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5251 struct ocfs2_path *path, int index,
5252 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08005253 u32 cpos, u32 len,
5254 struct ocfs2_extent_tree *et)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005255{
5256 int ret;
5257 u32 left_cpos, rec_range, trunc_range;
5258 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5259 struct super_block *sb = inode->i_sb;
5260 struct ocfs2_path *left_path = NULL;
5261 struct ocfs2_extent_list *el = path_leaf_el(path);
5262 struct ocfs2_extent_rec *rec;
5263 struct ocfs2_extent_block *eb;
5264
5265 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005266 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005267 if (ret) {
5268 mlog_errno(ret);
5269 goto out;
5270 }
5271
5272 index--;
5273 }
5274
5275 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5276 path->p_tree_depth) {
5277 /*
5278 * Check whether this is the rightmost tree record. If
5279 * we remove all of this record or part of its right
5280 * edge then an update of the record lengths above it
5281 * will be required.
5282 */
5283 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5284 if (eb->h_next_leaf_blk == 0)
5285 is_rightmost_tree_rec = 1;
5286 }
5287
5288 rec = &el->l_recs[index];
5289 if (index == 0 && path->p_tree_depth &&
5290 le32_to_cpu(rec->e_cpos) == cpos) {
5291 /*
5292 * Changing the leftmost offset (via partial or whole
5293 * record truncate) of an interior (or rightmost) path
5294 * means we have to update the subtree that is formed
5295 * by this leaf and the one to it's left.
5296 *
5297 * There are two cases we can skip:
5298 * 1) Path is the leftmost one in our inode tree.
5299 * 2) The leaf is rightmost and will be empty after
5300 * we remove the extent record - the rotate code
5301 * knows how to update the newly formed edge.
5302 */
5303
5304 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5305 &left_cpos);
5306 if (ret) {
5307 mlog_errno(ret);
5308 goto out;
5309 }
5310
5311 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005312 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005313 if (!left_path) {
5314 ret = -ENOMEM;
5315 mlog_errno(ret);
5316 goto out;
5317 }
5318
5319 ret = ocfs2_find_path(inode, left_path, left_cpos);
5320 if (ret) {
5321 mlog_errno(ret);
5322 goto out;
5323 }
5324 }
5325 }
5326
5327 ret = ocfs2_extend_rotate_transaction(handle, 0,
5328 handle->h_buffer_credits,
5329 path);
5330 if (ret) {
5331 mlog_errno(ret);
5332 goto out;
5333 }
5334
5335 ret = ocfs2_journal_access_path(inode, handle, path);
5336 if (ret) {
5337 mlog_errno(ret);
5338 goto out;
5339 }
5340
5341 ret = ocfs2_journal_access_path(inode, handle, left_path);
5342 if (ret) {
5343 mlog_errno(ret);
5344 goto out;
5345 }
5346
5347 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5348 trunc_range = cpos + len;
5349
5350 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5351 int next_free;
5352
5353 memset(rec, 0, sizeof(*rec));
5354 ocfs2_cleanup_merge(el, index);
5355 wants_rotate = 1;
5356
5357 next_free = le16_to_cpu(el->l_next_free_rec);
5358 if (is_rightmost_tree_rec && next_free > 1) {
5359 /*
5360 * We skip the edge update if this path will
5361 * be deleted by the rotate code.
5362 */
5363 rec = &el->l_recs[next_free - 1];
5364 ocfs2_adjust_rightmost_records(inode, handle, path,
5365 rec);
5366 }
5367 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5368 /* Remove leftmost portion of the record. */
5369 le32_add_cpu(&rec->e_cpos, len);
5370 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5371 le16_add_cpu(&rec->e_leaf_clusters, -len);
5372 } else if (rec_range == trunc_range) {
5373 /* Remove rightmost portion of the record */
5374 le16_add_cpu(&rec->e_leaf_clusters, -len);
5375 if (is_rightmost_tree_rec)
5376 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5377 } else {
5378 /* Caller should have trapped this. */
5379 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5380 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5381 le32_to_cpu(rec->e_cpos),
5382 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5383 BUG();
5384 }
5385
5386 if (left_path) {
5387 int subtree_index;
5388
5389 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5390 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5391 subtree_index);
5392 }
5393
5394 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5395
Tao Mae7d4cb62008-08-18 17:38:44 +08005396 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005397 if (ret) {
5398 mlog_errno(ret);
5399 goto out;
5400 }
5401
5402out:
5403 ocfs2_free_path(left_path);
5404 return ret;
5405}
5406
Joel Beckerf99b9b72008-08-20 19:36:33 -07005407int ocfs2_remove_extent(struct inode *inode,
5408 struct ocfs2_extent_tree *et,
Mark Fasheh063c4562007-07-03 13:34:11 -07005409 u32 cpos, u32 len, handle_t *handle,
5410 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005411 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005412{
5413 int ret, index;
5414 u32 rec_range, trunc_range;
5415 struct ocfs2_extent_rec *rec;
5416 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005417 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005418
5419 ocfs2_extent_map_trunc(inode, 0);
5420
Joel Beckerffdd7a52008-10-17 22:32:01 -07005421 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005422 if (!path) {
5423 ret = -ENOMEM;
5424 mlog_errno(ret);
5425 goto out;
5426 }
5427
5428 ret = ocfs2_find_path(inode, path, cpos);
5429 if (ret) {
5430 mlog_errno(ret);
5431 goto out;
5432 }
5433
5434 el = path_leaf_el(path);
5435 index = ocfs2_search_extent_list(el, cpos);
5436 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5437 ocfs2_error(inode->i_sb,
5438 "Inode %llu has an extent at cpos %u which can no "
5439 "longer be found.\n",
5440 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5441 ret = -EROFS;
5442 goto out;
5443 }
5444
5445 /*
5446 * We have 3 cases of extent removal:
5447 * 1) Range covers the entire extent rec
5448 * 2) Range begins or ends on one edge of the extent rec
5449 * 3) Range is in the middle of the extent rec (no shared edges)
5450 *
5451 * For case 1 we remove the extent rec and left rotate to
5452 * fill the hole.
5453 *
5454 * For case 2 we just shrink the existing extent rec, with a
5455 * tree update if the shrinking edge is also the edge of an
5456 * extent block.
5457 *
5458 * For case 3 we do a right split to turn the extent rec into
5459 * something case 2 can handle.
5460 */
5461 rec = &el->l_recs[index];
5462 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5463 trunc_range = cpos + len;
5464
5465 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5466
5467 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5468 "(cpos %u, len %u)\n",
5469 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5470 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5471
5472 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5473 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005474 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005475 if (ret) {
5476 mlog_errno(ret);
5477 goto out;
5478 }
5479 } else {
Joel Beckerf99b9b72008-08-20 19:36:33 -07005480 ret = ocfs2_split_tree(inode, et, handle, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005481 trunc_range, meta_ac);
5482 if (ret) {
5483 mlog_errno(ret);
5484 goto out;
5485 }
5486
5487 /*
5488 * The split could have manipulated the tree enough to
5489 * move the record location, so we have to look for it again.
5490 */
5491 ocfs2_reinit_path(path, 1);
5492
5493 ret = ocfs2_find_path(inode, path, cpos);
5494 if (ret) {
5495 mlog_errno(ret);
5496 goto out;
5497 }
5498
5499 el = path_leaf_el(path);
5500 index = ocfs2_search_extent_list(el, cpos);
5501 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5502 ocfs2_error(inode->i_sb,
5503 "Inode %llu: split at cpos %u lost record.",
5504 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5505 cpos);
5506 ret = -EROFS;
5507 goto out;
5508 }
5509
5510 /*
5511 * Double check our values here. If anything is fishy,
5512 * it's easier to catch it at the top level.
5513 */
5514 rec = &el->l_recs[index];
5515 rec_range = le32_to_cpu(rec->e_cpos) +
5516 ocfs2_rec_clusters(el, rec);
5517 if (rec_range != trunc_range) {
5518 ocfs2_error(inode->i_sb,
5519 "Inode %llu: error after split at cpos %u"
5520 "trunc len %u, existing record is (%u,%u)",
5521 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5522 cpos, len, le32_to_cpu(rec->e_cpos),
5523 ocfs2_rec_clusters(el, rec));
5524 ret = -EROFS;
5525 goto out;
5526 }
5527
5528 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005529 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005530 if (ret) {
5531 mlog_errno(ret);
5532 goto out;
5533 }
5534 }
5535
5536out:
5537 ocfs2_free_path(path);
5538 return ret;
5539}
5540
Mark Fashehfecc0112008-11-12 15:16:38 -08005541int ocfs2_remove_btree_range(struct inode *inode,
5542 struct ocfs2_extent_tree *et,
5543 u32 cpos, u32 phys_cpos, u32 len,
5544 struct ocfs2_cached_dealloc_ctxt *dealloc)
5545{
5546 int ret;
5547 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5548 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5549 struct inode *tl_inode = osb->osb_tl_inode;
5550 handle_t *handle;
5551 struct ocfs2_alloc_context *meta_ac = NULL;
5552
5553 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5554 if (ret) {
5555 mlog_errno(ret);
5556 return ret;
5557 }
5558
5559 mutex_lock(&tl_inode->i_mutex);
5560
5561 if (ocfs2_truncate_log_needs_flush(osb)) {
5562 ret = __ocfs2_flush_truncate_log(osb);
5563 if (ret < 0) {
5564 mlog_errno(ret);
5565 goto out;
5566 }
5567 }
5568
Jan Karaa90714c2008-10-09 19:38:40 +02005569 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005570 if (IS_ERR(handle)) {
5571 ret = PTR_ERR(handle);
5572 mlog_errno(ret);
5573 goto out;
5574 }
5575
Joel Becker13723d02008-10-17 19:25:01 -07005576 ret = ocfs2_et_root_journal_access(handle, inode, et,
5577 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005578 if (ret) {
5579 mlog_errno(ret);
5580 goto out;
5581 }
5582
Mark Fashehfd4ef232009-01-29 15:06:21 -08005583 vfs_dq_free_space_nodirty(inode,
5584 ocfs2_clusters_to_bytes(inode->i_sb, len));
5585
Mark Fashehfecc0112008-11-12 15:16:38 -08005586 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5587 dealloc);
5588 if (ret) {
5589 mlog_errno(ret);
5590 goto out_commit;
5591 }
5592
5593 ocfs2_et_update_clusters(inode, et, -len);
5594
5595 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5596 if (ret) {
5597 mlog_errno(ret);
5598 goto out_commit;
5599 }
5600
5601 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5602 if (ret)
5603 mlog_errno(ret);
5604
5605out_commit:
5606 ocfs2_commit_trans(osb, handle);
5607out:
5608 mutex_unlock(&tl_inode->i_mutex);
5609
5610 if (meta_ac)
5611 ocfs2_free_alloc_context(meta_ac);
5612
5613 return ret;
5614}
5615
Mark Fasheh063c4562007-07-03 13:34:11 -07005616int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005617{
5618 struct buffer_head *tl_bh = osb->osb_tl_bh;
5619 struct ocfs2_dinode *di;
5620 struct ocfs2_truncate_log *tl;
5621
5622 di = (struct ocfs2_dinode *) tl_bh->b_data;
5623 tl = &di->id2.i_dealloc;
5624
5625 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5626 "slot %d, invalid truncate log parameters: used = "
5627 "%u, count = %u\n", osb->slot_num,
5628 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5629 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5630}
5631
5632static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5633 unsigned int new_start)
5634{
5635 unsigned int tail_index;
5636 unsigned int current_tail;
5637
5638 /* No records, nothing to coalesce */
5639 if (!le16_to_cpu(tl->tl_used))
5640 return 0;
5641
5642 tail_index = le16_to_cpu(tl->tl_used) - 1;
5643 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5644 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5645
5646 return current_tail == new_start;
5647}
5648
Mark Fasheh063c4562007-07-03 13:34:11 -07005649int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5650 handle_t *handle,
5651 u64 start_blk,
5652 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005653{
5654 int status, index;
5655 unsigned int start_cluster, tl_count;
5656 struct inode *tl_inode = osb->osb_tl_inode;
5657 struct buffer_head *tl_bh = osb->osb_tl_bh;
5658 struct ocfs2_dinode *di;
5659 struct ocfs2_truncate_log *tl;
5660
Mark Fashehb06970532006-03-03 10:24:33 -08005661 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5662 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005663
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005664 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005665
5666 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5667
5668 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005669
Joel Becker10995aa2008-11-13 14:49:12 -08005670 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5671 * by the underlying call to ocfs2_read_inode_block(), so any
5672 * corruption is a code bug */
5673 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5674
5675 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005676 tl_count = le16_to_cpu(tl->tl_count);
5677 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5678 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005679 "Truncate record count on #%llu invalid "
5680 "wanted %u, actual %u\n",
5681 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005682 ocfs2_truncate_recs_per_inode(osb->sb),
5683 le16_to_cpu(tl->tl_count));
5684
5685 /* Caller should have known to flush before calling us. */
5686 index = le16_to_cpu(tl->tl_used);
5687 if (index >= tl_count) {
5688 status = -ENOSPC;
5689 mlog_errno(status);
5690 goto bail;
5691 }
5692
Joel Becker13723d02008-10-17 19:25:01 -07005693 status = ocfs2_journal_access_di(handle, tl_inode, tl_bh,
5694 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005695 if (status < 0) {
5696 mlog_errno(status);
5697 goto bail;
5698 }
5699
5700 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005701 "%llu (index = %d)\n", num_clusters, start_cluster,
5702 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005703
5704 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5705 /*
5706 * Move index back to the record we are coalescing with.
5707 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5708 */
5709 index--;
5710
5711 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5712 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5713 index, le32_to_cpu(tl->tl_recs[index].t_start),
5714 num_clusters);
5715 } else {
5716 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5717 tl->tl_used = cpu_to_le16(index + 1);
5718 }
5719 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5720
5721 status = ocfs2_journal_dirty(handle, tl_bh);
5722 if (status < 0) {
5723 mlog_errno(status);
5724 goto bail;
5725 }
5726
5727bail:
5728 mlog_exit(status);
5729 return status;
5730}
5731
5732static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005733 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005734 struct inode *data_alloc_inode,
5735 struct buffer_head *data_alloc_bh)
5736{
5737 int status = 0;
5738 int i;
5739 unsigned int num_clusters;
5740 u64 start_blk;
5741 struct ocfs2_truncate_rec rec;
5742 struct ocfs2_dinode *di;
5743 struct ocfs2_truncate_log *tl;
5744 struct inode *tl_inode = osb->osb_tl_inode;
5745 struct buffer_head *tl_bh = osb->osb_tl_bh;
5746
5747 mlog_entry_void();
5748
5749 di = (struct ocfs2_dinode *) tl_bh->b_data;
5750 tl = &di->id2.i_dealloc;
5751 i = le16_to_cpu(tl->tl_used) - 1;
5752 while (i >= 0) {
5753 /* Caller has given us at least enough credits to
5754 * update the truncate log dinode */
Joel Becker13723d02008-10-17 19:25:01 -07005755 status = ocfs2_journal_access_di(handle, tl_inode, tl_bh,
5756 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005757 if (status < 0) {
5758 mlog_errno(status);
5759 goto bail;
5760 }
5761
5762 tl->tl_used = cpu_to_le16(i);
5763
5764 status = ocfs2_journal_dirty(handle, tl_bh);
5765 if (status < 0) {
5766 mlog_errno(status);
5767 goto bail;
5768 }
5769
5770 /* TODO: Perhaps we can calculate the bulk of the
5771 * credits up front rather than extending like
5772 * this. */
5773 status = ocfs2_extend_trans(handle,
5774 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5775 if (status < 0) {
5776 mlog_errno(status);
5777 goto bail;
5778 }
5779
5780 rec = tl->tl_recs[i];
5781 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5782 le32_to_cpu(rec.t_start));
5783 num_clusters = le32_to_cpu(rec.t_clusters);
5784
5785 /* if start_blk is not set, we ignore the record as
5786 * invalid. */
5787 if (start_blk) {
5788 mlog(0, "free record %d, start = %u, clusters = %u\n",
5789 i, le32_to_cpu(rec.t_start), num_clusters);
5790
5791 status = ocfs2_free_clusters(handle, data_alloc_inode,
5792 data_alloc_bh, start_blk,
5793 num_clusters);
5794 if (status < 0) {
5795 mlog_errno(status);
5796 goto bail;
5797 }
5798 }
5799 i--;
5800 }
5801
5802bail:
5803 mlog_exit(status);
5804 return status;
5805}
5806
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005807/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005808int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005809{
5810 int status;
5811 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005812 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005813 struct inode *tl_inode = osb->osb_tl_inode;
5814 struct inode *data_alloc_inode = NULL;
5815 struct buffer_head *tl_bh = osb->osb_tl_bh;
5816 struct buffer_head *data_alloc_bh = NULL;
5817 struct ocfs2_dinode *di;
5818 struct ocfs2_truncate_log *tl;
5819
5820 mlog_entry_void();
5821
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005822 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005823
5824 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005825
Joel Becker10995aa2008-11-13 14:49:12 -08005826 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5827 * by the underlying call to ocfs2_read_inode_block(), so any
5828 * corruption is a code bug */
5829 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5830
5831 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005832 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005833 mlog(0, "Flush %u records from truncate log #%llu\n",
5834 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005835 if (!num_to_flush) {
5836 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005837 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005838 }
5839
5840 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5841 GLOBAL_BITMAP_SYSTEM_INODE,
5842 OCFS2_INVALID_SLOT);
5843 if (!data_alloc_inode) {
5844 status = -EINVAL;
5845 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005846 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005847 }
5848
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005849 mutex_lock(&data_alloc_inode->i_mutex);
5850
Mark Fashehe63aecb62007-10-18 15:30:42 -07005851 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005852 if (status < 0) {
5853 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005854 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005855 }
5856
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005857 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005858 if (IS_ERR(handle)) {
5859 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005860 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005861 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005862 }
5863
5864 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5865 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005866 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005867 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005868
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005869 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005870
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005871out_unlock:
5872 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005873 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005874
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005875out_mutex:
5876 mutex_unlock(&data_alloc_inode->i_mutex);
5877 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005878
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005879out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005880 mlog_exit(status);
5881 return status;
5882}
5883
5884int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5885{
5886 int status;
5887 struct inode *tl_inode = osb->osb_tl_inode;
5888
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005889 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005890 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005891 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005892
5893 return status;
5894}
5895
David Howellsc4028952006-11-22 14:57:56 +00005896static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005897{
5898 int status;
David Howellsc4028952006-11-22 14:57:56 +00005899 struct ocfs2_super *osb =
5900 container_of(work, struct ocfs2_super,
5901 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005902
5903 mlog_entry_void();
5904
5905 status = ocfs2_flush_truncate_log(osb);
5906 if (status < 0)
5907 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005908 else
5909 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005910
5911 mlog_exit(status);
5912}
5913
5914#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5915void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5916 int cancel)
5917{
5918 if (osb->osb_tl_inode) {
5919 /* We want to push off log flushes while truncates are
5920 * still running. */
5921 if (cancel)
5922 cancel_delayed_work(&osb->osb_truncate_log_wq);
5923
5924 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5925 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5926 }
5927}
5928
5929static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5930 int slot_num,
5931 struct inode **tl_inode,
5932 struct buffer_head **tl_bh)
5933{
5934 int status;
5935 struct inode *inode = NULL;
5936 struct buffer_head *bh = NULL;
5937
5938 inode = ocfs2_get_system_file_inode(osb,
5939 TRUNCATE_LOG_SYSTEM_INODE,
5940 slot_num);
5941 if (!inode) {
5942 status = -EINVAL;
5943 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5944 goto bail;
5945 }
5946
Joel Beckerb657c952008-11-13 14:49:11 -08005947 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005948 if (status < 0) {
5949 iput(inode);
5950 mlog_errno(status);
5951 goto bail;
5952 }
5953
5954 *tl_inode = inode;
5955 *tl_bh = bh;
5956bail:
5957 mlog_exit(status);
5958 return status;
5959}
5960
5961/* called during the 1st stage of node recovery. we stamp a clean
5962 * truncate log and pass back a copy for processing later. if the
5963 * truncate log does not require processing, a *tl_copy is set to
5964 * NULL. */
5965int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5966 int slot_num,
5967 struct ocfs2_dinode **tl_copy)
5968{
5969 int status;
5970 struct inode *tl_inode = NULL;
5971 struct buffer_head *tl_bh = NULL;
5972 struct ocfs2_dinode *di;
5973 struct ocfs2_truncate_log *tl;
5974
5975 *tl_copy = NULL;
5976
5977 mlog(0, "recover truncate log from slot %d\n", slot_num);
5978
5979 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5980 if (status < 0) {
5981 mlog_errno(status);
5982 goto bail;
5983 }
5984
5985 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005986
Joel Becker10995aa2008-11-13 14:49:12 -08005987 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5988 * validated by the underlying call to ocfs2_read_inode_block(),
5989 * so any corruption is a code bug */
5990 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5991
5992 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005993 if (le16_to_cpu(tl->tl_used)) {
5994 mlog(0, "We'll have %u logs to recover\n",
5995 le16_to_cpu(tl->tl_used));
5996
5997 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5998 if (!(*tl_copy)) {
5999 status = -ENOMEM;
6000 mlog_errno(status);
6001 goto bail;
6002 }
6003
6004 /* Assuming the write-out below goes well, this copy
6005 * will be passed back to recovery for processing. */
6006 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6007
6008 /* All we need to do to clear the truncate log is set
6009 * tl_used. */
6010 tl->tl_used = 0;
6011
Joel Becker13723d02008-10-17 19:25:01 -07006012 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Mark Fashehccd979b2005-12-15 14:31:24 -08006013 status = ocfs2_write_block(osb, tl_bh, tl_inode);
6014 if (status < 0) {
6015 mlog_errno(status);
6016 goto bail;
6017 }
6018 }
6019
6020bail:
6021 if (tl_inode)
6022 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006023 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006024
6025 if (status < 0 && (*tl_copy)) {
6026 kfree(*tl_copy);
6027 *tl_copy = NULL;
6028 }
6029
6030 mlog_exit(status);
6031 return status;
6032}
6033
6034int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6035 struct ocfs2_dinode *tl_copy)
6036{
6037 int status = 0;
6038 int i;
6039 unsigned int clusters, num_recs, start_cluster;
6040 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006041 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006042 struct inode *tl_inode = osb->osb_tl_inode;
6043 struct ocfs2_truncate_log *tl;
6044
6045 mlog_entry_void();
6046
6047 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6048 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6049 return -EINVAL;
6050 }
6051
6052 tl = &tl_copy->id2.i_dealloc;
6053 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08006054 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006055 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006056
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006057 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006058 for(i = 0; i < num_recs; i++) {
6059 if (ocfs2_truncate_log_needs_flush(osb)) {
6060 status = __ocfs2_flush_truncate_log(osb);
6061 if (status < 0) {
6062 mlog_errno(status);
6063 goto bail_up;
6064 }
6065 }
6066
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006067 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006068 if (IS_ERR(handle)) {
6069 status = PTR_ERR(handle);
6070 mlog_errno(status);
6071 goto bail_up;
6072 }
6073
6074 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6075 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6076 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6077
6078 status = ocfs2_truncate_log_append(osb, handle,
6079 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006080 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006081 if (status < 0) {
6082 mlog_errno(status);
6083 goto bail_up;
6084 }
6085 }
6086
6087bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006088 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006089
6090 mlog_exit(status);
6091 return status;
6092}
6093
6094void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6095{
6096 int status;
6097 struct inode *tl_inode = osb->osb_tl_inode;
6098
6099 mlog_entry_void();
6100
6101 if (tl_inode) {
6102 cancel_delayed_work(&osb->osb_truncate_log_wq);
6103 flush_workqueue(ocfs2_wq);
6104
6105 status = ocfs2_flush_truncate_log(osb);
6106 if (status < 0)
6107 mlog_errno(status);
6108
6109 brelse(osb->osb_tl_bh);
6110 iput(osb->osb_tl_inode);
6111 }
6112
6113 mlog_exit_void();
6114}
6115
6116int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6117{
6118 int status;
6119 struct inode *tl_inode = NULL;
6120 struct buffer_head *tl_bh = NULL;
6121
6122 mlog_entry_void();
6123
6124 status = ocfs2_get_truncate_log_info(osb,
6125 osb->slot_num,
6126 &tl_inode,
6127 &tl_bh);
6128 if (status < 0)
6129 mlog_errno(status);
6130
6131 /* ocfs2_truncate_log_shutdown keys on the existence of
6132 * osb->osb_tl_inode so we don't set any of the osb variables
6133 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006134 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6135 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006136 osb->osb_tl_bh = tl_bh;
6137 osb->osb_tl_inode = tl_inode;
6138
6139 mlog_exit(status);
6140 return status;
6141}
6142
Mark Fasheh2b604352007-06-22 15:45:27 -07006143/*
6144 * Delayed de-allocation of suballocator blocks.
6145 *
6146 * Some sets of block de-allocations might involve multiple suballocator inodes.
6147 *
6148 * The locking for this can get extremely complicated, especially when
6149 * the suballocator inodes to delete from aren't known until deep
6150 * within an unrelated codepath.
6151 *
6152 * ocfs2_extent_block structures are a good example of this - an inode
6153 * btree could have been grown by any number of nodes each allocating
6154 * out of their own suballoc inode.
6155 *
6156 * These structures allow the delay of block de-allocation until a
6157 * later time, when locking of multiple cluster inodes won't cause
6158 * deadlock.
6159 */
6160
6161/*
Tao Ma2891d292008-11-12 08:26:58 +08006162 * Describe a single bit freed from a suballocator. For the block
6163 * suballocators, it represents one block. For the global cluster
6164 * allocator, it represents some clusters and free_bit indicates
6165 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006166 */
6167struct ocfs2_cached_block_free {
6168 struct ocfs2_cached_block_free *free_next;
6169 u64 free_blk;
6170 unsigned int free_bit;
6171};
6172
6173struct ocfs2_per_slot_free_list {
6174 struct ocfs2_per_slot_free_list *f_next_suballocator;
6175 int f_inode_type;
6176 int f_slot;
6177 struct ocfs2_cached_block_free *f_first;
6178};
6179
Tao Ma2891d292008-11-12 08:26:58 +08006180static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6181 int sysfile_type,
6182 int slot,
6183 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006184{
6185 int ret;
6186 u64 bg_blkno;
6187 handle_t *handle;
6188 struct inode *inode;
6189 struct buffer_head *di_bh = NULL;
6190 struct ocfs2_cached_block_free *tmp;
6191
6192 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6193 if (!inode) {
6194 ret = -EINVAL;
6195 mlog_errno(ret);
6196 goto out;
6197 }
6198
6199 mutex_lock(&inode->i_mutex);
6200
Mark Fashehe63aecb62007-10-18 15:30:42 -07006201 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006202 if (ret) {
6203 mlog_errno(ret);
6204 goto out_mutex;
6205 }
6206
6207 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6208 if (IS_ERR(handle)) {
6209 ret = PTR_ERR(handle);
6210 mlog_errno(ret);
6211 goto out_unlock;
6212 }
6213
6214 while (head) {
6215 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6216 head->free_bit);
6217 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6218 head->free_bit, (unsigned long long)head->free_blk);
6219
6220 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6221 head->free_bit, bg_blkno, 1);
6222 if (ret) {
6223 mlog_errno(ret);
6224 goto out_journal;
6225 }
6226
6227 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6228 if (ret) {
6229 mlog_errno(ret);
6230 goto out_journal;
6231 }
6232
6233 tmp = head;
6234 head = head->free_next;
6235 kfree(tmp);
6236 }
6237
6238out_journal:
6239 ocfs2_commit_trans(osb, handle);
6240
6241out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006242 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006243 brelse(di_bh);
6244out_mutex:
6245 mutex_unlock(&inode->i_mutex);
6246 iput(inode);
6247out:
6248 while(head) {
6249 /* Premature exit may have left some dangling items. */
6250 tmp = head;
6251 head = head->free_next;
6252 kfree(tmp);
6253 }
6254
6255 return ret;
6256}
6257
Tao Ma2891d292008-11-12 08:26:58 +08006258int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6259 u64 blkno, unsigned int bit)
6260{
6261 int ret = 0;
6262 struct ocfs2_cached_block_free *item;
6263
6264 item = kmalloc(sizeof(*item), GFP_NOFS);
6265 if (item == NULL) {
6266 ret = -ENOMEM;
6267 mlog_errno(ret);
6268 return ret;
6269 }
6270
6271 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6272 bit, (unsigned long long)blkno);
6273
6274 item->free_blk = blkno;
6275 item->free_bit = bit;
6276 item->free_next = ctxt->c_global_allocator;
6277
6278 ctxt->c_global_allocator = item;
6279 return ret;
6280}
6281
6282static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6283 struct ocfs2_cached_block_free *head)
6284{
6285 struct ocfs2_cached_block_free *tmp;
6286 struct inode *tl_inode = osb->osb_tl_inode;
6287 handle_t *handle;
6288 int ret = 0;
6289
6290 mutex_lock(&tl_inode->i_mutex);
6291
6292 while (head) {
6293 if (ocfs2_truncate_log_needs_flush(osb)) {
6294 ret = __ocfs2_flush_truncate_log(osb);
6295 if (ret < 0) {
6296 mlog_errno(ret);
6297 break;
6298 }
6299 }
6300
6301 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6302 if (IS_ERR(handle)) {
6303 ret = PTR_ERR(handle);
6304 mlog_errno(ret);
6305 break;
6306 }
6307
6308 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6309 head->free_bit);
6310
6311 ocfs2_commit_trans(osb, handle);
6312 tmp = head;
6313 head = head->free_next;
6314 kfree(tmp);
6315
6316 if (ret < 0) {
6317 mlog_errno(ret);
6318 break;
6319 }
6320 }
6321
6322 mutex_unlock(&tl_inode->i_mutex);
6323
6324 while (head) {
6325 /* Premature exit may have left some dangling items. */
6326 tmp = head;
6327 head = head->free_next;
6328 kfree(tmp);
6329 }
6330
6331 return ret;
6332}
6333
Mark Fasheh2b604352007-06-22 15:45:27 -07006334int ocfs2_run_deallocs(struct ocfs2_super *osb,
6335 struct ocfs2_cached_dealloc_ctxt *ctxt)
6336{
6337 int ret = 0, ret2;
6338 struct ocfs2_per_slot_free_list *fl;
6339
6340 if (!ctxt)
6341 return 0;
6342
6343 while (ctxt->c_first_suballocator) {
6344 fl = ctxt->c_first_suballocator;
6345
6346 if (fl->f_first) {
6347 mlog(0, "Free items: (type %u, slot %d)\n",
6348 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006349 ret2 = ocfs2_free_cached_blocks(osb,
6350 fl->f_inode_type,
6351 fl->f_slot,
6352 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006353 if (ret2)
6354 mlog_errno(ret2);
6355 if (!ret)
6356 ret = ret2;
6357 }
6358
6359 ctxt->c_first_suballocator = fl->f_next_suballocator;
6360 kfree(fl);
6361 }
6362
Tao Ma2891d292008-11-12 08:26:58 +08006363 if (ctxt->c_global_allocator) {
6364 ret2 = ocfs2_free_cached_clusters(osb,
6365 ctxt->c_global_allocator);
6366 if (ret2)
6367 mlog_errno(ret2);
6368 if (!ret)
6369 ret = ret2;
6370
6371 ctxt->c_global_allocator = NULL;
6372 }
6373
Mark Fasheh2b604352007-06-22 15:45:27 -07006374 return ret;
6375}
6376
6377static struct ocfs2_per_slot_free_list *
6378ocfs2_find_per_slot_free_list(int type,
6379 int slot,
6380 struct ocfs2_cached_dealloc_ctxt *ctxt)
6381{
6382 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6383
6384 while (fl) {
6385 if (fl->f_inode_type == type && fl->f_slot == slot)
6386 return fl;
6387
6388 fl = fl->f_next_suballocator;
6389 }
6390
6391 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6392 if (fl) {
6393 fl->f_inode_type = type;
6394 fl->f_slot = slot;
6395 fl->f_first = NULL;
6396 fl->f_next_suballocator = ctxt->c_first_suballocator;
6397
6398 ctxt->c_first_suballocator = fl;
6399 }
6400 return fl;
6401}
6402
6403static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6404 int type, int slot, u64 blkno,
6405 unsigned int bit)
6406{
6407 int ret;
6408 struct ocfs2_per_slot_free_list *fl;
6409 struct ocfs2_cached_block_free *item;
6410
6411 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6412 if (fl == NULL) {
6413 ret = -ENOMEM;
6414 mlog_errno(ret);
6415 goto out;
6416 }
6417
6418 item = kmalloc(sizeof(*item), GFP_NOFS);
6419 if (item == NULL) {
6420 ret = -ENOMEM;
6421 mlog_errno(ret);
6422 goto out;
6423 }
6424
6425 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6426 type, slot, bit, (unsigned long long)blkno);
6427
6428 item->free_blk = blkno;
6429 item->free_bit = bit;
6430 item->free_next = fl->f_first;
6431
6432 fl->f_first = item;
6433
6434 ret = 0;
6435out:
6436 return ret;
6437}
6438
Mark Fasheh59a5e412007-06-22 15:52:36 -07006439static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6440 struct ocfs2_extent_block *eb)
6441{
6442 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6443 le16_to_cpu(eb->h_suballoc_slot),
6444 le64_to_cpu(eb->h_blkno),
6445 le16_to_cpu(eb->h_suballoc_bit));
6446}
6447
Mark Fashehccd979b2005-12-15 14:31:24 -08006448/* This function will figure out whether the currently last extent
6449 * block will be deleted, and if it will, what the new last extent
6450 * block will be so we can update his h_next_leaf_blk field, as well
6451 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006452static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006453 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006454 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006455 struct buffer_head **new_last_eb)
6456{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006457 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006458 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006459 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006460 struct ocfs2_extent_block *eb;
6461 struct ocfs2_extent_list *el;
6462 struct buffer_head *bh = NULL;
6463
6464 *new_last_eb = NULL;
6465
Mark Fashehccd979b2005-12-15 14:31:24 -08006466 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006467 if (!path->p_tree_depth)
6468 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006469
6470 /* trunc to zero special case - this makes tree_depth = 0
6471 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006472 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006473 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006474
Mark Fashehdcd05382007-01-16 11:32:23 -08006475 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006476 BUG_ON(!el->l_next_free_rec);
6477
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006478 /*
6479 * Make sure that this extent list will actually be empty
6480 * after we clear away the data. We can shortcut out if
6481 * there's more than one non-empty extent in the
6482 * list. Otherwise, a check of the remaining extent is
6483 * necessary.
6484 */
6485 next_free = le16_to_cpu(el->l_next_free_rec);
6486 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006487 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006488 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006489 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006490
6491 /* We may have a valid extent in index 1, check it. */
6492 if (next_free == 2)
6493 rec = &el->l_recs[1];
6494
6495 /*
6496 * Fall through - no more nonempty extents, so we want
6497 * to delete this leaf.
6498 */
6499 } else {
6500 if (next_free > 1)
6501 goto out;
6502
6503 rec = &el->l_recs[0];
6504 }
6505
6506 if (rec) {
6507 /*
6508 * Check it we'll only be trimming off the end of this
6509 * cluster.
6510 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006511 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006512 goto out;
6513 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006514
Mark Fashehdcd05382007-01-16 11:32:23 -08006515 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6516 if (ret) {
6517 mlog_errno(ret);
6518 goto out;
6519 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006520
Mark Fashehdcd05382007-01-16 11:32:23 -08006521 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6522 if (ret) {
6523 mlog_errno(ret);
6524 goto out;
6525 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006526
Mark Fashehdcd05382007-01-16 11:32:23 -08006527 eb = (struct ocfs2_extent_block *) bh->b_data;
6528 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006529
6530 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6531 * Any corruption is a code bug. */
6532 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006533
6534 *new_last_eb = bh;
6535 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006536 mlog(0, "returning block %llu, (cpos: %u)\n",
6537 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6538out:
6539 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006540
Mark Fashehdcd05382007-01-16 11:32:23 -08006541 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006542}
6543
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006544/*
6545 * Trim some clusters off the rightmost edge of a tree. Only called
6546 * during truncate.
6547 *
6548 * The caller needs to:
6549 * - start journaling of each path component.
6550 * - compute and fully set up any new last ext block
6551 */
6552static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6553 handle_t *handle, struct ocfs2_truncate_context *tc,
6554 u32 clusters_to_del, u64 *delete_start)
6555{
6556 int ret, i, index = path->p_tree_depth;
6557 u32 new_edge = 0;
6558 u64 deleted_eb = 0;
6559 struct buffer_head *bh;
6560 struct ocfs2_extent_list *el;
6561 struct ocfs2_extent_rec *rec;
6562
6563 *delete_start = 0;
6564
6565 while (index >= 0) {
6566 bh = path->p_node[index].bh;
6567 el = path->p_node[index].el;
6568
6569 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6570 index, (unsigned long long)bh->b_blocknr);
6571
6572 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6573
6574 if (index !=
6575 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6576 ocfs2_error(inode->i_sb,
6577 "Inode %lu has invalid ext. block %llu",
6578 inode->i_ino,
6579 (unsigned long long)bh->b_blocknr);
6580 ret = -EROFS;
6581 goto out;
6582 }
6583
6584find_tail_record:
6585 i = le16_to_cpu(el->l_next_free_rec) - 1;
6586 rec = &el->l_recs[i];
6587
6588 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6589 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006590 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006591 (unsigned long long)le64_to_cpu(rec->e_blkno),
6592 le16_to_cpu(el->l_next_free_rec));
6593
Mark Fashehe48edee2007-03-07 16:46:57 -08006594 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006595
6596 if (le16_to_cpu(el->l_tree_depth) == 0) {
6597 /*
6598 * If the leaf block contains a single empty
6599 * extent and no records, we can just remove
6600 * the block.
6601 */
6602 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6603 memset(rec, 0,
6604 sizeof(struct ocfs2_extent_rec));
6605 el->l_next_free_rec = cpu_to_le16(0);
6606
6607 goto delete;
6608 }
6609
6610 /*
6611 * Remove any empty extents by shifting things
6612 * left. That should make life much easier on
6613 * the code below. This condition is rare
6614 * enough that we shouldn't see a performance
6615 * hit.
6616 */
6617 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6618 le16_add_cpu(&el->l_next_free_rec, -1);
6619
6620 for(i = 0;
6621 i < le16_to_cpu(el->l_next_free_rec); i++)
6622 el->l_recs[i] = el->l_recs[i + 1];
6623
6624 memset(&el->l_recs[i], 0,
6625 sizeof(struct ocfs2_extent_rec));
6626
6627 /*
6628 * We've modified our extent list. The
6629 * simplest way to handle this change
6630 * is to being the search from the
6631 * start again.
6632 */
6633 goto find_tail_record;
6634 }
6635
Mark Fashehe48edee2007-03-07 16:46:57 -08006636 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006637
6638 /*
6639 * We'll use "new_edge" on our way back up the
6640 * tree to know what our rightmost cpos is.
6641 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006642 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006643 new_edge += le32_to_cpu(rec->e_cpos);
6644
6645 /*
6646 * The caller will use this to delete data blocks.
6647 */
6648 *delete_start = le64_to_cpu(rec->e_blkno)
6649 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006650 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006651
6652 /*
6653 * If it's now empty, remove this record.
6654 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006655 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006656 memset(rec, 0,
6657 sizeof(struct ocfs2_extent_rec));
6658 le16_add_cpu(&el->l_next_free_rec, -1);
6659 }
6660 } else {
6661 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6662 memset(rec, 0,
6663 sizeof(struct ocfs2_extent_rec));
6664 le16_add_cpu(&el->l_next_free_rec, -1);
6665
6666 goto delete;
6667 }
6668
6669 /* Can this actually happen? */
6670 if (le16_to_cpu(el->l_next_free_rec) == 0)
6671 goto delete;
6672
6673 /*
6674 * We never actually deleted any clusters
6675 * because our leaf was empty. There's no
6676 * reason to adjust the rightmost edge then.
6677 */
6678 if (new_edge == 0)
6679 goto delete;
6680
Mark Fashehe48edee2007-03-07 16:46:57 -08006681 rec->e_int_clusters = cpu_to_le32(new_edge);
6682 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006683 -le32_to_cpu(rec->e_cpos));
6684
6685 /*
6686 * A deleted child record should have been
6687 * caught above.
6688 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006689 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006690 }
6691
6692delete:
6693 ret = ocfs2_journal_dirty(handle, bh);
6694 if (ret) {
6695 mlog_errno(ret);
6696 goto out;
6697 }
6698
6699 mlog(0, "extent list container %llu, after: record %d: "
6700 "(%u, %u, %llu), next = %u.\n",
6701 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006702 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006703 (unsigned long long)le64_to_cpu(rec->e_blkno),
6704 le16_to_cpu(el->l_next_free_rec));
6705
6706 /*
6707 * We must be careful to only attempt delete of an
6708 * extent block (and not the root inode block).
6709 */
6710 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6711 struct ocfs2_extent_block *eb =
6712 (struct ocfs2_extent_block *)bh->b_data;
6713
6714 /*
6715 * Save this for use when processing the
6716 * parent block.
6717 */
6718 deleted_eb = le64_to_cpu(eb->h_blkno);
6719
6720 mlog(0, "deleting this extent block.\n");
6721
6722 ocfs2_remove_from_cache(inode, bh);
6723
Mark Fashehe48edee2007-03-07 16:46:57 -08006724 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006725 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6726 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6727
Mark Fasheh59a5e412007-06-22 15:52:36 -07006728 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6729 /* An error here is not fatal. */
6730 if (ret < 0)
6731 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006732 } else {
6733 deleted_eb = 0;
6734 }
6735
6736 index--;
6737 }
6738
6739 ret = 0;
6740out:
6741 return ret;
6742}
6743
Mark Fashehccd979b2005-12-15 14:31:24 -08006744static int ocfs2_do_truncate(struct ocfs2_super *osb,
6745 unsigned int clusters_to_del,
6746 struct inode *inode,
6747 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006748 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006749 struct ocfs2_truncate_context *tc,
6750 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006751{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006752 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006753 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006754 struct ocfs2_extent_block *last_eb = NULL;
6755 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006756 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006757 u64 delete_blk = 0;
6758
6759 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6760
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006761 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006762 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006763 if (status < 0) {
6764 mlog_errno(status);
6765 goto bail;
6766 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006767
Mark Fashehdcd05382007-01-16 11:32:23 -08006768 /*
6769 * Each component will be touched, so we might as well journal
6770 * here to avoid having to handle errors later.
6771 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006772 status = ocfs2_journal_access_path(inode, handle, path);
6773 if (status < 0) {
6774 mlog_errno(status);
6775 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006776 }
6777
6778 if (last_eb_bh) {
Joel Becker13723d02008-10-17 19:25:01 -07006779 status = ocfs2_journal_access_eb(handle, inode, last_eb_bh,
6780 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08006781 if (status < 0) {
6782 mlog_errno(status);
6783 goto bail;
6784 }
6785
6786 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6787 }
6788
6789 el = &(fe->id2.i_list);
6790
6791 /*
6792 * Lower levels depend on this never happening, but it's best
6793 * to check it up here before changing the tree.
6794 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006795 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006796 ocfs2_error(inode->i_sb,
6797 "Inode %lu has an empty extent record, depth %u\n",
6798 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006799 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006800 goto bail;
6801 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006802
Jan Karaa90714c2008-10-09 19:38:40 +02006803 vfs_dq_free_space_nodirty(inode,
6804 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006805 spin_lock(&OCFS2_I(inode)->ip_lock);
6806 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6807 clusters_to_del;
6808 spin_unlock(&OCFS2_I(inode)->ip_lock);
6809 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006810 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006811
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006812 status = ocfs2_trim_tree(inode, path, handle, tc,
6813 clusters_to_del, &delete_blk);
6814 if (status) {
6815 mlog_errno(status);
6816 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006817 }
6818
Mark Fashehdcd05382007-01-16 11:32:23 -08006819 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006820 /* trunc to zero is a special case. */
6821 el->l_tree_depth = 0;
6822 fe->i_last_eb_blk = 0;
6823 } else if (last_eb)
6824 fe->i_last_eb_blk = last_eb->h_blkno;
6825
6826 status = ocfs2_journal_dirty(handle, fe_bh);
6827 if (status < 0) {
6828 mlog_errno(status);
6829 goto bail;
6830 }
6831
6832 if (last_eb) {
6833 /* If there will be a new last extent block, then by
6834 * definition, there cannot be any leaves to the right of
6835 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006836 last_eb->h_next_leaf_blk = 0;
6837 status = ocfs2_journal_dirty(handle, last_eb_bh);
6838 if (status < 0) {
6839 mlog_errno(status);
6840 goto bail;
6841 }
6842 }
6843
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006844 if (delete_blk) {
6845 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6846 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006847 if (status < 0) {
6848 mlog_errno(status);
6849 goto bail;
6850 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006851 }
6852 status = 0;
6853bail:
Mark Fashehdcd05382007-01-16 11:32:23 -08006854
Mark Fashehccd979b2005-12-15 14:31:24 -08006855 mlog_exit(status);
6856 return status;
6857}
6858
Joel Becker2b4e30f2008-09-03 20:03:41 -07006859static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006860{
6861 set_buffer_uptodate(bh);
6862 mark_buffer_dirty(bh);
6863 return 0;
6864}
6865
Mark Fasheh1d410a62007-09-07 14:20:45 -07006866static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6867 unsigned int from, unsigned int to,
6868 struct page *page, int zero, u64 *phys)
6869{
6870 int ret, partial = 0;
6871
6872 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6873 if (ret)
6874 mlog_errno(ret);
6875
6876 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006877 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006878
6879 /*
6880 * Need to set the buffers we zero'd into uptodate
6881 * here if they aren't - ocfs2_map_page_blocks()
6882 * might've skipped some
6883 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006884 ret = walk_page_buffers(handle, page_buffers(page),
6885 from, to, &partial,
6886 ocfs2_zero_func);
6887 if (ret < 0)
6888 mlog_errno(ret);
6889 else if (ocfs2_should_order_data(inode)) {
6890 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006891 if (ret < 0)
6892 mlog_errno(ret);
6893 }
6894
6895 if (!partial)
6896 SetPageUptodate(page);
6897
6898 flush_dcache_page(page);
6899}
6900
Mark Fasheh35edec12007-07-06 14:41:18 -07006901static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6902 loff_t end, struct page **pages,
6903 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006904{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006905 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006906 struct page *page;
6907 unsigned int from, to = PAGE_CACHE_SIZE;
6908 struct super_block *sb = inode->i_sb;
6909
6910 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6911
6912 if (numpages == 0)
6913 goto out;
6914
Mark Fasheh35edec12007-07-06 14:41:18 -07006915 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006916 for(i = 0; i < numpages; i++) {
6917 page = pages[i];
6918
Mark Fasheh35edec12007-07-06 14:41:18 -07006919 from = start & (PAGE_CACHE_SIZE - 1);
6920 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6921 to = end & (PAGE_CACHE_SIZE - 1);
6922
Mark Fasheh60b11392007-02-16 11:46:50 -08006923 BUG_ON(from > PAGE_CACHE_SIZE);
6924 BUG_ON(to > PAGE_CACHE_SIZE);
6925
Mark Fasheh1d410a62007-09-07 14:20:45 -07006926 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6927 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006928
Mark Fasheh35edec12007-07-06 14:41:18 -07006929 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006930 }
6931out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006932 if (pages)
6933 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006934}
6935
Mark Fasheh35edec12007-07-06 14:41:18 -07006936static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006937 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006938{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006939 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006940 struct super_block *sb = inode->i_sb;
6941 struct address_space *mapping = inode->i_mapping;
6942 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006943 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006944
Mark Fasheh35edec12007-07-06 14:41:18 -07006945 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006946
Mark Fasheh35edec12007-07-06 14:41:18 -07006947 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6948 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6949
Mark Fasheh1d410a62007-09-07 14:20:45 -07006950 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006951 last_page_bytes = PAGE_ALIGN(end);
6952 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006953 do {
6954 pages[numpages] = grab_cache_page(mapping, index);
6955 if (!pages[numpages]) {
6956 ret = -ENOMEM;
6957 mlog_errno(ret);
6958 goto out;
6959 }
6960
6961 numpages++;
6962 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006963 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006964
6965out:
6966 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006967 if (pages)
6968 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006969 numpages = 0;
6970 }
6971
6972 *num = numpages;
6973
6974 return ret;
6975}
6976
6977/*
6978 * Zero the area past i_size but still within an allocated
6979 * cluster. This avoids exposing nonzero data on subsequent file
6980 * extends.
6981 *
6982 * We need to call this before i_size is updated on the inode because
6983 * otherwise block_write_full_page() will skip writeout of pages past
6984 * i_size. The new_i_size parameter is passed for this reason.
6985 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006986int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6987 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006988{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006989 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006990 struct page **pages = NULL;
6991 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006992 unsigned int ext_flags;
6993 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006994
6995 /*
6996 * File systems which don't support sparse files zero on every
6997 * extend.
6998 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07006999 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08007000 return 0;
7001
Mark Fasheh1d410a62007-09-07 14:20:45 -07007002 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08007003 sizeof(struct page *), GFP_NOFS);
7004 if (pages == NULL) {
7005 ret = -ENOMEM;
7006 mlog_errno(ret);
7007 goto out;
7008 }
7009
Mark Fasheh1d410a62007-09-07 14:20:45 -07007010 if (range_start == range_end)
7011 goto out;
7012
7013 ret = ocfs2_extent_map_get_blocks(inode,
7014 range_start >> sb->s_blocksize_bits,
7015 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08007016 if (ret) {
7017 mlog_errno(ret);
7018 goto out;
7019 }
7020
Mark Fasheh1d410a62007-09-07 14:20:45 -07007021 /*
7022 * Tail is a hole, or is marked unwritten. In either case, we
7023 * can count on read and write to return/push zero's.
7024 */
7025 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08007026 goto out;
7027
Mark Fasheh1d410a62007-09-07 14:20:45 -07007028 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7029 &numpages);
7030 if (ret) {
7031 mlog_errno(ret);
7032 goto out;
7033 }
7034
Mark Fasheh35edec12007-07-06 14:41:18 -07007035 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7036 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08007037
7038 /*
7039 * Initiate writeout of the pages we zero'd here. We don't
7040 * wait on them - the truncate_inode_pages() call later will
7041 * do that for us.
7042 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007043 ret = do_sync_mapping_range(inode->i_mapping, range_start,
7044 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08007045 if (ret)
7046 mlog_errno(ret);
7047
7048out:
7049 if (pages)
7050 kfree(pages);
7051
7052 return ret;
7053}
7054
Tiger Yangfdd77702008-08-18 17:08:55 +08007055static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7056 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007057{
7058 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08007059 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007060
Tiger Yangfdd77702008-08-18 17:08:55 +08007061 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7062 memset(&di->id2, 0, blocksize -
7063 offsetof(struct ocfs2_dinode, id2) -
7064 xattrsize);
7065 else
7066 memset(&di->id2, 0, blocksize -
7067 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007068}
7069
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007070void ocfs2_dinode_new_extent_list(struct inode *inode,
7071 struct ocfs2_dinode *di)
7072{
Tiger Yangfdd77702008-08-18 17:08:55 +08007073 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007074 di->id2.i_list.l_tree_depth = 0;
7075 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08007076 di->id2.i_list.l_count = cpu_to_le16(
7077 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007078}
7079
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007080void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7081{
7082 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7083 struct ocfs2_inline_data *idata = &di->id2.i_data;
7084
7085 spin_lock(&oi->ip_lock);
7086 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7087 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7088 spin_unlock(&oi->ip_lock);
7089
7090 /*
7091 * We clear the entire i_data structure here so that all
7092 * fields can be properly initialized.
7093 */
Tiger Yangfdd77702008-08-18 17:08:55 +08007094 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007095
Tiger Yangfdd77702008-08-18 17:08:55 +08007096 idata->id_count = cpu_to_le16(
7097 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007098}
7099
7100int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7101 struct buffer_head *di_bh)
7102{
7103 int ret, i, has_data, num_pages = 0;
7104 handle_t *handle;
7105 u64 uninitialized_var(block);
7106 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7107 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7108 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007109 struct ocfs2_alloc_context *data_ac = NULL;
7110 struct page **pages = NULL;
7111 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07007112 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02007113 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007114
7115 has_data = i_size_read(inode) ? 1 : 0;
7116
7117 if (has_data) {
7118 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7119 sizeof(struct page *), GFP_NOFS);
7120 if (pages == NULL) {
7121 ret = -ENOMEM;
7122 mlog_errno(ret);
7123 goto out;
7124 }
7125
7126 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7127 if (ret) {
7128 mlog_errno(ret);
7129 goto out;
7130 }
7131 }
7132
Jan Karaa90714c2008-10-09 19:38:40 +02007133 handle = ocfs2_start_trans(osb,
7134 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007135 if (IS_ERR(handle)) {
7136 ret = PTR_ERR(handle);
7137 mlog_errno(ret);
7138 goto out_unlock;
7139 }
7140
Joel Becker13723d02008-10-17 19:25:01 -07007141 ret = ocfs2_journal_access_di(handle, inode, di_bh,
7142 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007143 if (ret) {
7144 mlog_errno(ret);
7145 goto out_commit;
7146 }
7147
7148 if (has_data) {
7149 u32 bit_off, num;
7150 unsigned int page_end;
7151 u64 phys;
7152
Jan Karaa90714c2008-10-09 19:38:40 +02007153 if (vfs_dq_alloc_space_nodirty(inode,
7154 ocfs2_clusters_to_bytes(osb->sb, 1))) {
7155 ret = -EDQUOT;
7156 goto out_commit;
7157 }
7158 did_quota = 1;
7159
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007160 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7161 &num);
7162 if (ret) {
7163 mlog_errno(ret);
7164 goto out_commit;
7165 }
7166
7167 /*
7168 * Save two copies, one for insert, and one that can
7169 * be changed by ocfs2_map_and_dirty_page() below.
7170 */
7171 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7172
7173 /*
7174 * Non sparse file systems zero on extend, so no need
7175 * to do that now.
7176 */
7177 if (!ocfs2_sparse_alloc(osb) &&
7178 PAGE_CACHE_SIZE < osb->s_clustersize)
7179 end = PAGE_CACHE_SIZE;
7180
7181 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7182 if (ret) {
7183 mlog_errno(ret);
7184 goto out_commit;
7185 }
7186
7187 /*
7188 * This should populate the 1st page for us and mark
7189 * it up to date.
7190 */
7191 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7192 if (ret) {
7193 mlog_errno(ret);
7194 goto out_commit;
7195 }
7196
7197 page_end = PAGE_CACHE_SIZE;
7198 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7199 page_end = osb->s_clustersize;
7200
7201 for (i = 0; i < num_pages; i++)
7202 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7203 pages[i], i > 0, &phys);
7204 }
7205
7206 spin_lock(&oi->ip_lock);
7207 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7208 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7209 spin_unlock(&oi->ip_lock);
7210
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007211 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007212
7213 ocfs2_journal_dirty(handle, di_bh);
7214
7215 if (has_data) {
7216 /*
7217 * An error at this point should be extremely rare. If
7218 * this proves to be false, we could always re-build
7219 * the in-inode data from our pages.
7220 */
Joel Becker8d6220d2008-08-22 12:46:09 -07007221 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07007222 ret = ocfs2_insert_extent(osb, handle, inode, &et,
7223 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007224 if (ret) {
7225 mlog_errno(ret);
7226 goto out_commit;
7227 }
7228
7229 inode->i_blocks = ocfs2_inode_sector_count(inode);
7230 }
7231
7232out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007233 if (ret < 0 && did_quota)
7234 vfs_dq_free_space_nodirty(inode,
7235 ocfs2_clusters_to_bytes(osb->sb, 1));
7236
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007237 ocfs2_commit_trans(osb, handle);
7238
7239out_unlock:
7240 if (data_ac)
7241 ocfs2_free_alloc_context(data_ac);
7242
7243out:
7244 if (pages) {
7245 ocfs2_unlock_and_free_pages(pages, num_pages);
7246 kfree(pages);
7247 }
7248
7249 return ret;
7250}
7251
Mark Fashehccd979b2005-12-15 14:31:24 -08007252/*
7253 * It is expected, that by the time you call this function,
7254 * inode->i_size and fe->i_size have been adjusted.
7255 *
7256 * WARNING: This will kfree the truncate context
7257 */
7258int ocfs2_commit_truncate(struct ocfs2_super *osb,
7259 struct inode *inode,
7260 struct buffer_head *fe_bh,
7261 struct ocfs2_truncate_context *tc)
7262{
7263 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08007264 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08007265 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07007266 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007267 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08007268 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08007269 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007270
7271 mlog_entry_void();
7272
Mark Fashehdcd05382007-01-16 11:32:23 -08007273 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007274 i_size_read(inode));
7275
Joel Becker13723d02008-10-17 19:25:01 -07007276 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7277 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08007278 if (!path) {
7279 status = -ENOMEM;
7280 mlog_errno(status);
7281 goto bail;
7282 }
Mark Fasheh83418972007-04-23 18:53:12 -07007283
7284 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7285
Mark Fashehccd979b2005-12-15 14:31:24 -08007286start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007287 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007288 * Check that we still have allocation to delete.
7289 */
7290 if (OCFS2_I(inode)->ip_clusters == 0) {
7291 status = 0;
7292 goto bail;
7293 }
7294
7295 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007296 * Truncate always works against the rightmost tree branch.
7297 */
7298 status = ocfs2_find_path(inode, path, UINT_MAX);
7299 if (status) {
7300 mlog_errno(status);
7301 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007302 }
7303
Mark Fashehdcd05382007-01-16 11:32:23 -08007304 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7305 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7306
7307 /*
7308 * By now, el will point to the extent list on the bottom most
7309 * portion of this tree. Only the tail record is considered in
7310 * each pass.
7311 *
7312 * We handle the following cases, in order:
7313 * - empty extent: delete the remaining branch
7314 * - remove the entire record
7315 * - remove a partial record
7316 * - no record needs to be removed (truncate has completed)
7317 */
7318 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007319 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7320 ocfs2_error(inode->i_sb,
7321 "Inode %llu has empty extent block at %llu\n",
7322 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7323 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7324 status = -EROFS;
7325 goto bail;
7326 }
7327
Mark Fashehccd979b2005-12-15 14:31:24 -08007328 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08007329 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08007330 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007331 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7332 clusters_to_del = 0;
7333 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007334 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007335 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007336 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08007337 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08007338 new_highest_cpos;
7339 } else {
7340 status = 0;
7341 goto bail;
7342 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007343
Mark Fashehdcd05382007-01-16 11:32:23 -08007344 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7345 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7346
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007347 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007348 tl_sem = 1;
7349 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7350 * record is free for use. If there isn't any, we flush to get
7351 * an empty truncate log. */
7352 if (ocfs2_truncate_log_needs_flush(osb)) {
7353 status = __ocfs2_flush_truncate_log(osb);
7354 if (status < 0) {
7355 mlog_errno(status);
7356 goto bail;
7357 }
7358 }
7359
7360 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007361 (struct ocfs2_dinode *)fe_bh->b_data,
7362 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007363 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007364 if (IS_ERR(handle)) {
7365 status = PTR_ERR(handle);
7366 handle = NULL;
7367 mlog_errno(status);
7368 goto bail;
7369 }
7370
Mark Fashehdcd05382007-01-16 11:32:23 -08007371 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7372 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007373 if (status < 0) {
7374 mlog_errno(status);
7375 goto bail;
7376 }
7377
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007378 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007379 tl_sem = 0;
7380
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007381 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007382 handle = NULL;
7383
Mark Fashehdcd05382007-01-16 11:32:23 -08007384 ocfs2_reinit_path(path, 1);
7385
7386 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007387 * The check above will catch the case where we've truncated
7388 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007389 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007390 goto start;
7391
Mark Fashehccd979b2005-12-15 14:31:24 -08007392bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007393
7394 ocfs2_schedule_truncate_log_flush(osb, 1);
7395
7396 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007397 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007398
7399 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007400 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007401
Mark Fasheh59a5e412007-06-22 15:52:36 -07007402 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7403
Mark Fashehdcd05382007-01-16 11:32:23 -08007404 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007405
7406 /* This will drop the ext_alloc cluster lock for us */
7407 ocfs2_free_truncate_context(tc);
7408
7409 mlog_exit(status);
7410 return status;
7411}
7412
Mark Fashehccd979b2005-12-15 14:31:24 -08007413/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007414 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007415 */
7416int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7417 struct inode *inode,
7418 struct buffer_head *fe_bh,
7419 struct ocfs2_truncate_context **tc)
7420{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007421 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007422 unsigned int new_i_clusters;
7423 struct ocfs2_dinode *fe;
7424 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007425 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007426
7427 mlog_entry_void();
7428
7429 *tc = NULL;
7430
7431 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7432 i_size_read(inode));
7433 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7434
7435 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007436 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7437 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007438
Robert P. J. Daycd861282006-12-13 00:34:52 -08007439 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007440 if (!(*tc)) {
7441 status = -ENOMEM;
7442 mlog_errno(status);
7443 goto bail;
7444 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007445 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007446
Mark Fashehccd979b2005-12-15 14:31:24 -08007447 if (fe->id2.i_list.l_tree_depth) {
Joel Becker5e965812008-11-13 14:49:16 -08007448 status = ocfs2_read_extent_block(inode,
7449 le64_to_cpu(fe->i_last_eb_blk),
7450 &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007451 if (status < 0) {
7452 mlog_errno(status);
7453 goto bail;
7454 }
7455 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007456 }
7457
7458 (*tc)->tc_last_eb_bh = last_eb_bh;
7459
Mark Fashehccd979b2005-12-15 14:31:24 -08007460 status = 0;
7461bail:
7462 if (status < 0) {
7463 if (*tc)
7464 ocfs2_free_truncate_context(*tc);
7465 *tc = NULL;
7466 }
7467 mlog_exit_void();
7468 return status;
7469}
7470
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007471/*
7472 * 'start' is inclusive, 'end' is not.
7473 */
7474int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7475 unsigned int start, unsigned int end, int trunc)
7476{
7477 int ret;
7478 unsigned int numbytes;
7479 handle_t *handle;
7480 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7481 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7482 struct ocfs2_inline_data *idata = &di->id2.i_data;
7483
7484 if (end > i_size_read(inode))
7485 end = i_size_read(inode);
7486
7487 BUG_ON(start >= end);
7488
7489 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7490 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7491 !ocfs2_supports_inline_data(osb)) {
7492 ocfs2_error(inode->i_sb,
7493 "Inline data flags for inode %llu don't agree! "
7494 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7495 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7496 le16_to_cpu(di->i_dyn_features),
7497 OCFS2_I(inode)->ip_dyn_features,
7498 osb->s_feature_incompat);
7499 ret = -EROFS;
7500 goto out;
7501 }
7502
7503 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7504 if (IS_ERR(handle)) {
7505 ret = PTR_ERR(handle);
7506 mlog_errno(ret);
7507 goto out;
7508 }
7509
Joel Becker13723d02008-10-17 19:25:01 -07007510 ret = ocfs2_journal_access_di(handle, inode, di_bh,
7511 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007512 if (ret) {
7513 mlog_errno(ret);
7514 goto out_commit;
7515 }
7516
7517 numbytes = end - start;
7518 memset(idata->id_data + start, 0, numbytes);
7519
7520 /*
7521 * No need to worry about the data page here - it's been
7522 * truncated already and inline data doesn't need it for
7523 * pushing zero's to disk, so we'll let readpage pick it up
7524 * later.
7525 */
7526 if (trunc) {
7527 i_size_write(inode, start);
7528 di->i_size = cpu_to_le64(start);
7529 }
7530
7531 inode->i_blocks = ocfs2_inode_sector_count(inode);
7532 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7533
7534 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7535 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7536
7537 ocfs2_journal_dirty(handle, di_bh);
7538
7539out_commit:
7540 ocfs2_commit_trans(osb, handle);
7541
7542out:
7543 return ret;
7544}
7545
Mark Fashehccd979b2005-12-15 14:31:24 -08007546static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7547{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007548 /*
7549 * The caller is responsible for completing deallocation
7550 * before freeing the context.
7551 */
7552 if (tc->tc_dealloc.c_first_suballocator != NULL)
7553 mlog(ML_NOTICE,
7554 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007555
Mark Fasheha81cb882008-10-07 14:25:16 -07007556 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007557
7558 kfree(tc);
7559}