blob: ad41eabd8b74da945f192734e24197cb9bfa760a [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * alloc.c
5 *
6 * Extent allocs and frees
7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/fs.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/highmem.h>
Mark Fasheh60b11392007-02-16 11:46:50 -080030#include <linux/swap.h>
Jan Karaa90714c2008-10-09 19:38:40 +020031#include <linux/quotaops.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080032
33#define MLOG_MASK_PREFIX ML_DISK_ALLOC
34#include <cluster/masklog.h>
35
36#include "ocfs2.h"
37
38#include "alloc.h"
Mark Fasheh60b11392007-02-16 11:46:50 -080039#include "aops.h"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070040#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080041#include "dlmglue.h"
42#include "extent_map.h"
43#include "inode.h"
44#include "journal.h"
45#include "localalloc.h"
46#include "suballoc.h"
47#include "sysfile.h"
48#include "file.h"
49#include "super.h"
50#include "uptodate.h"
Joel Becker2a50a742008-12-09 14:24:33 -080051#include "xattr.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080052
53#include "buffer_head_io.h"
54
Tao Mae7d4cb62008-08-18 17:38:44 +080055
Joel Becker1625f8a2008-08-21 17:11:10 -070056/*
57 * Operations for a specific extent tree type.
58 *
59 * To implement an on-disk btree (extent tree) type in ocfs2, add
60 * an ocfs2_extent_tree_operations structure and the matching
Joel Becker8d6220d2008-08-22 12:46:09 -070061 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
Joel Becker1625f8a2008-08-21 17:11:10 -070062 * for the allocation portion of the extent tree.
63 */
Tao Mae7d4cb62008-08-18 17:38:44 +080064struct ocfs2_extent_tree_operations {
Joel Becker1625f8a2008-08-21 17:11:10 -070065 /*
66 * last_eb_blk is the block number of the right most leaf extent
67 * block. Most on-disk structures containing an extent tree store
68 * this value for fast access. The ->eo_set_last_eb_blk() and
69 * ->eo_get_last_eb_blk() operations access this value. They are
70 * both required.
71 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070072 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
73 u64 blkno);
74 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -070075
76 /*
77 * The on-disk structure usually keeps track of how many total
78 * clusters are stored in this extent tree. This function updates
79 * that value. new_clusters is the delta, and must be
80 * added to the total. Required.
81 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070082 void (*eo_update_clusters)(struct inode *inode,
83 struct ocfs2_extent_tree *et,
84 u32 new_clusters);
Joel Becker1625f8a2008-08-21 17:11:10 -070085
86 /*
87 * If ->eo_insert_check() exists, it is called before rec is
88 * inserted into the extent tree. It is optional.
89 */
Joel Becker1e61ee72008-08-20 18:32:45 -070090 int (*eo_insert_check)(struct inode *inode,
91 struct ocfs2_extent_tree *et,
92 struct ocfs2_extent_rec *rec);
Joel Becker35dc0aa2008-08-20 16:25:06 -070093 int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
Joel Becker0ce10102008-08-20 17:19:50 -070094
Joel Becker1625f8a2008-08-21 17:11:10 -070095 /*
96 * --------------------------------------------------------------
97 * The remaining are internal to ocfs2_extent_tree and don't have
98 * accessor functions
99 */
100
101 /*
102 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
103 * It is required.
104 */
Joel Becker0ce10102008-08-20 17:19:50 -0700105 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -0700106
107 /*
108 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
109 * it exists. If it does not, et->et_max_leaf_clusters is set
110 * to 0 (unlimited). Optional.
111 */
Joel Becker943cced2008-08-20 17:31:10 -0700112 void (*eo_fill_max_leaf_clusters)(struct inode *inode,
113 struct ocfs2_extent_tree *et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800114};
115
Joel Beckerf99b9b72008-08-20 19:36:33 -0700116
117/*
118 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
119 * in the methods.
120 */
121static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
122static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
123 u64 blkno);
124static void ocfs2_dinode_update_clusters(struct inode *inode,
125 struct ocfs2_extent_tree *et,
126 u32 clusters);
127static int ocfs2_dinode_insert_check(struct inode *inode,
128 struct ocfs2_extent_tree *et,
129 struct ocfs2_extent_rec *rec);
130static int ocfs2_dinode_sanity_check(struct inode *inode,
131 struct ocfs2_extent_tree *et);
132static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
133static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
134 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
135 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
136 .eo_update_clusters = ocfs2_dinode_update_clusters,
137 .eo_insert_check = ocfs2_dinode_insert_check,
138 .eo_sanity_check = ocfs2_dinode_sanity_check,
139 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
Tao Mae7d4cb62008-08-18 17:38:44 +0800140};
141
142static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
143 u64 blkno)
144{
Joel Beckerea5efa12008-08-20 16:57:27 -0700145 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800146
Joel Beckerf99b9b72008-08-20 19:36:33 -0700147 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800148 di->i_last_eb_blk = cpu_to_le64(blkno);
149}
150
151static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
152{
Joel Beckerea5efa12008-08-20 16:57:27 -0700153 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800154
Joel Beckerf99b9b72008-08-20 19:36:33 -0700155 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800156 return le64_to_cpu(di->i_last_eb_blk);
157}
158
159static void ocfs2_dinode_update_clusters(struct inode *inode,
160 struct ocfs2_extent_tree *et,
161 u32 clusters)
162{
Joel Beckerea5efa12008-08-20 16:57:27 -0700163 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800164
165 le32_add_cpu(&di->i_clusters, clusters);
166 spin_lock(&OCFS2_I(inode)->ip_lock);
167 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
168 spin_unlock(&OCFS2_I(inode)->ip_lock);
169}
170
Joel Becker1e61ee72008-08-20 18:32:45 -0700171static int ocfs2_dinode_insert_check(struct inode *inode,
172 struct ocfs2_extent_tree *et,
173 struct ocfs2_extent_rec *rec)
174{
175 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
176
177 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
178 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
Tao Ma74e77eb2009-03-12 06:24:23 +0800179 (OCFS2_I(inode)->ip_clusters !=
180 le32_to_cpu(rec->e_cpos)),
Joel Becker1e61ee72008-08-20 18:32:45 -0700181 "Device %s, asking for sparse allocation: inode %llu, "
182 "cpos %u, clusters %u\n",
183 osb->dev_str,
184 (unsigned long long)OCFS2_I(inode)->ip_blkno,
185 rec->e_cpos,
186 OCFS2_I(inode)->ip_clusters);
187
188 return 0;
189}
190
Tao Mae7d4cb62008-08-18 17:38:44 +0800191static int ocfs2_dinode_sanity_check(struct inode *inode,
192 struct ocfs2_extent_tree *et)
193{
Joel Becker10995aa2008-11-13 14:49:12 -0800194 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800195
Joel Beckerf99b9b72008-08-20 19:36:33 -0700196 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Joel Becker10995aa2008-11-13 14:49:12 -0800197 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
Tao Mae7d4cb62008-08-18 17:38:44 +0800198
Joel Becker10995aa2008-11-13 14:49:12 -0800199 return 0;
Tao Mae7d4cb62008-08-18 17:38:44 +0800200}
201
Joel Beckerf99b9b72008-08-20 19:36:33 -0700202static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
203{
204 struct ocfs2_dinode *di = et->et_object;
205
206 et->et_root_el = &di->id2.i_list;
207}
208
Tao Mae7d4cb62008-08-18 17:38:44 +0800209
Joel Becker0ce10102008-08-20 17:19:50 -0700210static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
211{
Joel Becker2a50a742008-12-09 14:24:33 -0800212 struct ocfs2_xattr_value_buf *vb = et->et_object;
Joel Becker0ce10102008-08-20 17:19:50 -0700213
Joel Becker2a50a742008-12-09 14:24:33 -0800214 et->et_root_el = &vb->vb_xv->xr_list;
Joel Becker0ce10102008-08-20 17:19:50 -0700215}
216
Tao Maf56654c2008-08-18 17:38:48 +0800217static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
218 u64 blkno)
219{
Joel Becker2a50a742008-12-09 14:24:33 -0800220 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800221
Joel Becker2a50a742008-12-09 14:24:33 -0800222 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
Tao Maf56654c2008-08-18 17:38:48 +0800223}
224
225static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
226{
Joel Becker2a50a742008-12-09 14:24:33 -0800227 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800228
Joel Becker2a50a742008-12-09 14:24:33 -0800229 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
Tao Maf56654c2008-08-18 17:38:48 +0800230}
231
232static void ocfs2_xattr_value_update_clusters(struct inode *inode,
233 struct ocfs2_extent_tree *et,
234 u32 clusters)
235{
Joel Becker2a50a742008-12-09 14:24:33 -0800236 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800237
Joel Becker2a50a742008-12-09 14:24:33 -0800238 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800239}
240
Joel Becker1a09f552008-08-20 17:44:24 -0700241static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700242 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
243 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
244 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700245 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800246};
247
Joel Becker0ce10102008-08-20 17:19:50 -0700248static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
249{
250 struct ocfs2_xattr_block *xb = et->et_object;
251
252 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
253}
254
Joel Becker943cced2008-08-20 17:31:10 -0700255static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode *inode,
256 struct ocfs2_extent_tree *et)
257{
258 et->et_max_leaf_clusters =
259 ocfs2_clusters_for_bytes(inode->i_sb,
260 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
261}
262
Tao Maba492612008-08-18 17:38:49 +0800263static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
264 u64 blkno)
265{
Joel Beckerea5efa12008-08-20 16:57:27 -0700266 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800267 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
268
269 xt->xt_last_eb_blk = cpu_to_le64(blkno);
270}
271
272static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
273{
Joel Beckerea5efa12008-08-20 16:57:27 -0700274 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800275 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
276
277 return le64_to_cpu(xt->xt_last_eb_blk);
278}
279
280static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
281 struct ocfs2_extent_tree *et,
282 u32 clusters)
283{
Joel Beckerea5efa12008-08-20 16:57:27 -0700284 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800285
286 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
287}
288
Tao Maba492612008-08-18 17:38:49 +0800289static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700290 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
291 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
292 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700293 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
Joel Becker943cced2008-08-20 17:31:10 -0700294 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
Tao Maba492612008-08-18 17:38:49 +0800295};
296
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800297static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
298 u64 blkno)
299{
300 struct ocfs2_dx_root_block *dx_root = et->et_object;
301
302 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
303}
304
305static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
306{
307 struct ocfs2_dx_root_block *dx_root = et->et_object;
308
309 return le64_to_cpu(dx_root->dr_last_eb_blk);
310}
311
312static void ocfs2_dx_root_update_clusters(struct inode *inode,
313 struct ocfs2_extent_tree *et,
314 u32 clusters)
315{
316 struct ocfs2_dx_root_block *dx_root = et->et_object;
317
318 le32_add_cpu(&dx_root->dr_clusters, clusters);
319}
320
321static int ocfs2_dx_root_sanity_check(struct inode *inode,
322 struct ocfs2_extent_tree *et)
323{
324 struct ocfs2_dx_root_block *dx_root = et->et_object;
325
326 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
327
328 return 0;
329}
330
331static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
332{
333 struct ocfs2_dx_root_block *dx_root = et->et_object;
334
335 et->et_root_el = &dx_root->dr_list;
336}
337
338static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
339 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
340 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
341 .eo_update_clusters = ocfs2_dx_root_update_clusters,
342 .eo_sanity_check = ocfs2_dx_root_sanity_check,
343 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
344};
345
Joel Becker8d6220d2008-08-22 12:46:09 -0700346static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
347 struct inode *inode,
348 struct buffer_head *bh,
Joel Becker13723d02008-10-17 19:25:01 -0700349 ocfs2_journal_access_func access,
Joel Becker8d6220d2008-08-22 12:46:09 -0700350 void *obj,
351 struct ocfs2_extent_tree_operations *ops)
Tao Mae7d4cb62008-08-18 17:38:44 +0800352{
Joel Becker1a09f552008-08-20 17:44:24 -0700353 et->et_ops = ops;
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700354 et->et_root_bh = bh;
Joel Beckerd9a0a1f2009-02-12 17:32:34 -0800355 et->et_ci = INODE_CACHE(inode);
Joel Becker13723d02008-10-17 19:25:01 -0700356 et->et_root_journal_access = access;
Joel Beckerea5efa12008-08-20 16:57:27 -0700357 if (!obj)
358 obj = (void *)bh->b_data;
359 et->et_object = obj;
Tao Mae7d4cb62008-08-18 17:38:44 +0800360
Joel Becker0ce10102008-08-20 17:19:50 -0700361 et->et_ops->eo_fill_root_el(et);
Joel Becker943cced2008-08-20 17:31:10 -0700362 if (!et->et_ops->eo_fill_max_leaf_clusters)
363 et->et_max_leaf_clusters = 0;
364 else
365 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800366}
367
Joel Becker8d6220d2008-08-22 12:46:09 -0700368void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
369 struct inode *inode,
370 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700371{
Joel Becker13723d02008-10-17 19:25:01 -0700372 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_di,
373 NULL, &ocfs2_dinode_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700374}
375
Joel Becker8d6220d2008-08-22 12:46:09 -0700376void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700377 struct inode *inode,
Joel Becker8d6220d2008-08-22 12:46:09 -0700378 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700379{
Joel Becker13723d02008-10-17 19:25:01 -0700380 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_xb,
381 NULL, &ocfs2_xattr_tree_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700382}
383
Joel Becker8d6220d2008-08-22 12:46:09 -0700384void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
385 struct inode *inode,
Joel Becker2a50a742008-12-09 14:24:33 -0800386 struct ocfs2_xattr_value_buf *vb)
Tao Mae7d4cb62008-08-18 17:38:44 +0800387{
Joel Becker2a50a742008-12-09 14:24:33 -0800388 __ocfs2_init_extent_tree(et, inode, vb->vb_bh, vb->vb_access, vb,
Joel Becker8d6220d2008-08-22 12:46:09 -0700389 &ocfs2_xattr_value_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800390}
391
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800392void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
393 struct inode *inode,
394 struct buffer_head *bh)
395{
396 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_dr,
397 NULL, &ocfs2_dx_root_et_ops);
398}
399
Joel Becker35dc0aa2008-08-20 16:25:06 -0700400static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
401 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800402{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700403 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800404}
405
Joel Becker35dc0aa2008-08-20 16:25:06 -0700406static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800407{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700408 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800409}
410
Joel Becker35dc0aa2008-08-20 16:25:06 -0700411static inline void ocfs2_et_update_clusters(struct inode *inode,
412 struct ocfs2_extent_tree *et,
413 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800414{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700415 et->et_ops->eo_update_clusters(inode, et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700416}
417
Joel Becker13723d02008-10-17 19:25:01 -0700418static inline int ocfs2_et_root_journal_access(handle_t *handle,
Joel Becker13723d02008-10-17 19:25:01 -0700419 struct ocfs2_extent_tree *et,
420 int type)
421{
Joel Beckerd9a0a1f2009-02-12 17:32:34 -0800422 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700423 type);
424}
425
Joel Becker1e61ee72008-08-20 18:32:45 -0700426static inline int ocfs2_et_insert_check(struct inode *inode,
427 struct ocfs2_extent_tree *et,
428 struct ocfs2_extent_rec *rec)
429{
430 int ret = 0;
431
432 if (et->et_ops->eo_insert_check)
433 ret = et->et_ops->eo_insert_check(inode, et, rec);
434 return ret;
435}
436
Joel Becker35dc0aa2008-08-20 16:25:06 -0700437static inline int ocfs2_et_sanity_check(struct inode *inode,
438 struct ocfs2_extent_tree *et)
439{
Joel Becker1e61ee72008-08-20 18:32:45 -0700440 int ret = 0;
441
442 if (et->et_ops->eo_sanity_check)
443 ret = et->et_ops->eo_sanity_check(inode, et);
444 return ret;
Tao Mae7d4cb62008-08-18 17:38:44 +0800445}
446
Mark Fashehccd979b2005-12-15 14:31:24 -0800447static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
Mark Fasheh59a5e412007-06-22 15:52:36 -0700448static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
449 struct ocfs2_extent_block *eb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800450
Mark Fashehdcd05382007-01-16 11:32:23 -0800451/*
452 * Structures which describe a path through a btree, and functions to
453 * manipulate them.
454 *
455 * The idea here is to be as generic as possible with the tree
456 * manipulation code.
457 */
458struct ocfs2_path_item {
459 struct buffer_head *bh;
460 struct ocfs2_extent_list *el;
461};
462
463#define OCFS2_MAX_PATH_DEPTH 5
464
465struct ocfs2_path {
Joel Becker13723d02008-10-17 19:25:01 -0700466 int p_tree_depth;
467 ocfs2_journal_access_func p_root_access;
468 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
Mark Fashehdcd05382007-01-16 11:32:23 -0800469};
470
471#define path_root_bh(_path) ((_path)->p_node[0].bh)
472#define path_root_el(_path) ((_path)->p_node[0].el)
Joel Becker13723d02008-10-17 19:25:01 -0700473#define path_root_access(_path)((_path)->p_root_access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800474#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
475#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
476#define path_num_items(_path) ((_path)->p_tree_depth + 1)
477
Joel Beckerfacdb772009-02-12 18:08:48 -0800478static int ocfs2_find_path(struct ocfs2_caching_info *ci,
479 struct ocfs2_path *path, u32 cpos);
Tao Ma6b791bc2009-06-12 14:18:36 +0800480static void ocfs2_adjust_rightmost_records(struct inode *inode,
481 handle_t *handle,
482 struct ocfs2_path *path,
483 struct ocfs2_extent_rec *insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -0800484/*
485 * Reset the actual path elements so that we can re-use the structure
486 * to build another path. Generally, this involves freeing the buffer
487 * heads.
488 */
489static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
490{
491 int i, start = 0, depth = 0;
492 struct ocfs2_path_item *node;
493
494 if (keep_root)
495 start = 1;
496
497 for(i = start; i < path_num_items(path); i++) {
498 node = &path->p_node[i];
499
500 brelse(node->bh);
501 node->bh = NULL;
502 node->el = NULL;
503 }
504
505 /*
506 * Tree depth may change during truncate, or insert. If we're
507 * keeping the root extent list, then make sure that our path
508 * structure reflects the proper depth.
509 */
510 if (keep_root)
511 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
Joel Becker13723d02008-10-17 19:25:01 -0700512 else
513 path_root_access(path) = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -0800514
515 path->p_tree_depth = depth;
516}
517
518static void ocfs2_free_path(struct ocfs2_path *path)
519{
520 if (path) {
521 ocfs2_reinit_path(path, 0);
522 kfree(path);
523 }
524}
525
526/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700527 * All the elements of src into dest. After this call, src could be freed
528 * without affecting dest.
529 *
530 * Both paths should have the same root. Any non-root elements of dest
531 * will be freed.
532 */
533static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
534{
535 int i;
536
537 BUG_ON(path_root_bh(dest) != path_root_bh(src));
538 BUG_ON(path_root_el(dest) != path_root_el(src));
Joel Becker13723d02008-10-17 19:25:01 -0700539 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fasheh328d5752007-06-18 10:48:04 -0700540
541 ocfs2_reinit_path(dest, 1);
542
543 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
544 dest->p_node[i].bh = src->p_node[i].bh;
545 dest->p_node[i].el = src->p_node[i].el;
546
547 if (dest->p_node[i].bh)
548 get_bh(dest->p_node[i].bh);
549 }
550}
551
552/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800553 * Make the *dest path the same as src and re-initialize src path to
554 * have a root only.
555 */
556static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
557{
558 int i;
559
560 BUG_ON(path_root_bh(dest) != path_root_bh(src));
Joel Becker13723d02008-10-17 19:25:01 -0700561 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fashehdcd05382007-01-16 11:32:23 -0800562
563 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
564 brelse(dest->p_node[i].bh);
565
566 dest->p_node[i].bh = src->p_node[i].bh;
567 dest->p_node[i].el = src->p_node[i].el;
568
569 src->p_node[i].bh = NULL;
570 src->p_node[i].el = NULL;
571 }
572}
573
574/*
575 * Insert an extent block at given index.
576 *
577 * This will not take an additional reference on eb_bh.
578 */
579static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
580 struct buffer_head *eb_bh)
581{
582 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
583
584 /*
585 * Right now, no root bh is an extent block, so this helps
586 * catch code errors with dinode trees. The assertion can be
587 * safely removed if we ever need to insert extent block
588 * structures at the root.
589 */
590 BUG_ON(index == 0);
591
592 path->p_node[index].bh = eb_bh;
593 path->p_node[index].el = &eb->h_list;
594}
595
596static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700597 struct ocfs2_extent_list *root_el,
598 ocfs2_journal_access_func access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800599{
600 struct ocfs2_path *path;
601
602 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
603
604 path = kzalloc(sizeof(*path), GFP_NOFS);
605 if (path) {
606 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
607 get_bh(root_bh);
608 path_root_bh(path) = root_bh;
609 path_root_el(path) = root_el;
Joel Becker13723d02008-10-17 19:25:01 -0700610 path_root_access(path) = access;
Mark Fashehdcd05382007-01-16 11:32:23 -0800611 }
612
613 return path;
614}
615
Joel Beckerffdd7a52008-10-17 22:32:01 -0700616static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
617{
Joel Becker13723d02008-10-17 19:25:01 -0700618 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
619 path_root_access(path));
Joel Beckerffdd7a52008-10-17 22:32:01 -0700620}
621
622static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
623{
Joel Becker13723d02008-10-17 19:25:01 -0700624 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
625 et->et_root_journal_access);
626}
627
628/*
629 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
630 * otherwise it's the root_access function.
631 *
632 * I don't like the way this function's name looks next to
633 * ocfs2_journal_access_path(), but I don't have a better one.
634 */
635static int ocfs2_path_bh_journal_access(handle_t *handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800636 struct ocfs2_caching_info *ci,
Joel Becker13723d02008-10-17 19:25:01 -0700637 struct ocfs2_path *path,
638 int idx)
639{
640 ocfs2_journal_access_func access = path_root_access(path);
641
642 if (!access)
643 access = ocfs2_journal_access;
644
645 if (idx)
646 access = ocfs2_journal_access_eb;
647
Joel Becker0cf2f762009-02-12 16:41:25 -0800648 return access(handle, ci, path->p_node[idx].bh,
Joel Becker13723d02008-10-17 19:25:01 -0700649 OCFS2_JOURNAL_ACCESS_WRITE);
Joel Beckerffdd7a52008-10-17 22:32:01 -0700650}
651
Mark Fashehdcd05382007-01-16 11:32:23 -0800652/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800653 * Convenience function to journal all components in a path.
654 */
Joel Becker0cf2f762009-02-12 16:41:25 -0800655static int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
656 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -0800657 struct ocfs2_path *path)
658{
659 int i, ret = 0;
660
661 if (!path)
662 goto out;
663
664 for(i = 0; i < path_num_items(path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800665 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -0800666 if (ret < 0) {
667 mlog_errno(ret);
668 goto out;
669 }
670 }
671
672out:
673 return ret;
674}
675
Mark Fasheh328d5752007-06-18 10:48:04 -0700676/*
677 * Return the index of the extent record which contains cluster #v_cluster.
678 * -1 is returned if it was not found.
679 *
680 * Should work fine on interior and exterior nodes.
681 */
682int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
683{
684 int ret = -1;
685 int i;
686 struct ocfs2_extent_rec *rec;
687 u32 rec_end, rec_start, clusters;
688
689 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
690 rec = &el->l_recs[i];
691
692 rec_start = le32_to_cpu(rec->e_cpos);
693 clusters = ocfs2_rec_clusters(el, rec);
694
695 rec_end = rec_start + clusters;
696
697 if (v_cluster >= rec_start && v_cluster < rec_end) {
698 ret = i;
699 break;
700 }
701 }
702
703 return ret;
704}
705
Mark Fashehdcd05382007-01-16 11:32:23 -0800706enum ocfs2_contig_type {
707 CONTIG_NONE = 0,
708 CONTIG_LEFT,
Mark Fasheh328d5752007-06-18 10:48:04 -0700709 CONTIG_RIGHT,
710 CONTIG_LEFTRIGHT,
Mark Fashehdcd05382007-01-16 11:32:23 -0800711};
712
Mark Fashehe48edee2007-03-07 16:46:57 -0800713
714/*
715 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
716 * ocfs2_extent_contig only work properly against leaf nodes!
717 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800718static int ocfs2_block_extent_contig(struct super_block *sb,
719 struct ocfs2_extent_rec *ext,
720 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800721{
Mark Fashehe48edee2007-03-07 16:46:57 -0800722 u64 blk_end = le64_to_cpu(ext->e_blkno);
723
724 blk_end += ocfs2_clusters_to_blocks(sb,
725 le16_to_cpu(ext->e_leaf_clusters));
726
727 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800728}
729
Mark Fashehdcd05382007-01-16 11:32:23 -0800730static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
731 struct ocfs2_extent_rec *right)
732{
Mark Fashehe48edee2007-03-07 16:46:57 -0800733 u32 left_range;
734
735 left_range = le32_to_cpu(left->e_cpos) +
736 le16_to_cpu(left->e_leaf_clusters);
737
738 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800739}
740
741static enum ocfs2_contig_type
742 ocfs2_extent_contig(struct inode *inode,
743 struct ocfs2_extent_rec *ext,
744 struct ocfs2_extent_rec *insert_rec)
745{
746 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
747
Mark Fasheh328d5752007-06-18 10:48:04 -0700748 /*
749 * Refuse to coalesce extent records with different flag
750 * fields - we don't want to mix unwritten extents with user
751 * data.
752 */
753 if (ext->e_flags != insert_rec->e_flags)
754 return CONTIG_NONE;
755
Mark Fashehdcd05382007-01-16 11:32:23 -0800756 if (ocfs2_extents_adjacent(ext, insert_rec) &&
757 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
758 return CONTIG_RIGHT;
759
760 blkno = le64_to_cpu(ext->e_blkno);
761 if (ocfs2_extents_adjacent(insert_rec, ext) &&
762 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
763 return CONTIG_LEFT;
764
765 return CONTIG_NONE;
766}
767
768/*
769 * NOTE: We can have pretty much any combination of contiguousness and
770 * appending.
771 *
772 * The usefulness of APPEND_TAIL is more in that it lets us know that
773 * we'll have to update the path to that leaf.
774 */
775enum ocfs2_append_type {
776 APPEND_NONE = 0,
777 APPEND_TAIL,
778};
779
Mark Fasheh328d5752007-06-18 10:48:04 -0700780enum ocfs2_split_type {
781 SPLIT_NONE = 0,
782 SPLIT_LEFT,
783 SPLIT_RIGHT,
784};
785
Mark Fashehdcd05382007-01-16 11:32:23 -0800786struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700787 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800788 enum ocfs2_append_type ins_appending;
789 enum ocfs2_contig_type ins_contig;
790 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800791 int ins_tree_depth;
792};
793
Mark Fasheh328d5752007-06-18 10:48:04 -0700794struct ocfs2_merge_ctxt {
795 enum ocfs2_contig_type c_contig_type;
796 int c_has_empty_extent;
797 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700798};
799
Joel Becker5e965812008-11-13 14:49:16 -0800800static int ocfs2_validate_extent_block(struct super_block *sb,
801 struct buffer_head *bh)
802{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700803 int rc;
Joel Becker5e965812008-11-13 14:49:16 -0800804 struct ocfs2_extent_block *eb =
805 (struct ocfs2_extent_block *)bh->b_data;
806
Joel Becker970e4932008-11-13 14:49:19 -0800807 mlog(0, "Validating extent block %llu\n",
808 (unsigned long long)bh->b_blocknr);
809
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700810 BUG_ON(!buffer_uptodate(bh));
811
812 /*
813 * If the ecc fails, we return the error but otherwise
814 * leave the filesystem running. We know any error is
815 * local to this block.
816 */
817 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
Joel Becker13723d02008-10-17 19:25:01 -0700818 if (rc) {
819 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
820 (unsigned long long)bh->b_blocknr);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700821 return rc;
Joel Becker13723d02008-10-17 19:25:01 -0700822 }
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700823
824 /*
825 * Errors after here are fatal.
826 */
827
Joel Becker5e965812008-11-13 14:49:16 -0800828 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
829 ocfs2_error(sb,
830 "Extent block #%llu has bad signature %.*s",
831 (unsigned long long)bh->b_blocknr, 7,
832 eb->h_signature);
833 return -EINVAL;
834 }
835
836 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
837 ocfs2_error(sb,
838 "Extent block #%llu has an invalid h_blkno "
839 "of %llu",
840 (unsigned long long)bh->b_blocknr,
841 (unsigned long long)le64_to_cpu(eb->h_blkno));
842 return -EINVAL;
843 }
844
845 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
846 ocfs2_error(sb,
847 "Extent block #%llu has an invalid "
848 "h_fs_generation of #%u",
849 (unsigned long long)bh->b_blocknr,
850 le32_to_cpu(eb->h_fs_generation));
851 return -EINVAL;
852 }
853
854 return 0;
855}
856
Joel Becker3d03a302009-02-12 17:49:26 -0800857int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
Joel Becker5e965812008-11-13 14:49:16 -0800858 struct buffer_head **bh)
859{
860 int rc;
861 struct buffer_head *tmp = *bh;
862
Joel Becker3d03a302009-02-12 17:49:26 -0800863 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800864 ocfs2_validate_extent_block);
Joel Becker5e965812008-11-13 14:49:16 -0800865
866 /* If ocfs2_read_block() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -0800867 if (!rc && !*bh)
Joel Becker5e965812008-11-13 14:49:16 -0800868 *bh = tmp;
869
Joel Becker5e965812008-11-13 14:49:16 -0800870 return rc;
871}
872
873
Mark Fashehccd979b2005-12-15 14:31:24 -0800874/*
875 * How many free extents have we got before we need more meta data?
876 */
877int ocfs2_num_free_extents(struct ocfs2_super *osb,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700878 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -0800879{
880 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800881 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800882 struct ocfs2_extent_block *eb;
883 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800884 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800885
886 mlog_entry_void();
887
Joel Beckerf99b9b72008-08-20 19:36:33 -0700888 el = et->et_root_el;
889 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800890
Tao Mae7d4cb62008-08-18 17:38:44 +0800891 if (last_eb_blk) {
Joel Becker3d03a302009-02-12 17:49:26 -0800892 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
893 &eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800894 if (retval < 0) {
895 mlog_errno(retval);
896 goto bail;
897 }
898 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
899 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800900 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800901
902 BUG_ON(el->l_tree_depth != 0);
903
904 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
905bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700906 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800907
908 mlog_exit(retval);
909 return retval;
910}
911
912/* expects array to already be allocated
913 *
914 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
915 * l_count for you
916 */
Joel Becker42a5a7a2009-02-12 18:49:19 -0800917static int ocfs2_create_new_meta_bhs(handle_t *handle,
918 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -0800919 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;
Joel Becker42a5a7a2009-02-12 18:49:19 -0800927 struct ocfs2_super *osb =
928 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -0800929 struct ocfs2_extent_block *eb;
930
931 mlog_entry_void();
932
933 count = 0;
934 while (count < wanted) {
935 status = ocfs2_claim_metadata(osb,
936 handle,
937 meta_ac,
938 wanted - count,
939 &suballoc_bit_start,
940 &num_got,
941 &first_blkno);
942 if (status < 0) {
943 mlog_errno(status);
944 goto bail;
945 }
946
947 for(i = count; i < (num_got + count); i++) {
948 bhs[i] = sb_getblk(osb->sb, first_blkno);
949 if (bhs[i] == NULL) {
950 status = -EIO;
951 mlog_errno(status);
952 goto bail;
953 }
Joel Becker42a5a7a2009-02-12 18:49:19 -0800954 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800955
Joel Becker42a5a7a2009-02-12 18:49:19 -0800956 status = ocfs2_journal_access_eb(handle, et->et_ci,
957 bhs[i],
Joel Becker13723d02008-10-17 19:25:01 -0700958 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800959 if (status < 0) {
960 mlog_errno(status);
961 goto bail;
962 }
963
964 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
965 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
966 /* Ok, setup the minimal stuff here. */
967 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
968 eb->h_blkno = cpu_to_le64(first_blkno);
969 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800970 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800971 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
972 eb->h_list.l_count =
973 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
974
975 suballoc_bit_start++;
976 first_blkno++;
977
978 /* We'll also be dirtied by the caller, so
979 * this isn't absolutely necessary. */
980 status = ocfs2_journal_dirty(handle, bhs[i]);
981 if (status < 0) {
982 mlog_errno(status);
983 goto bail;
984 }
985 }
986
987 count += num_got;
988 }
989
990 status = 0;
991bail:
992 if (status < 0) {
993 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -0700994 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800995 bhs[i] = NULL;
996 }
997 }
998 mlog_exit(status);
999 return status;
1000}
1001
1002/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001003 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1004 *
1005 * Returns the sum of the rightmost extent rec logical offset and
1006 * cluster count.
1007 *
1008 * ocfs2_add_branch() uses this to determine what logical cluster
1009 * value should be populated into the leftmost new branch records.
1010 *
1011 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1012 * value for the new topmost tree record.
1013 */
1014static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1015{
1016 int i;
1017
1018 i = le16_to_cpu(el->l_next_free_rec) - 1;
1019
1020 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001021 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08001022}
1023
1024/*
Tao Ma6b791bc2009-06-12 14:18:36 +08001025 * Change range of the branches in the right most path according to the leaf
1026 * extent block's rightmost record.
1027 */
1028static int ocfs2_adjust_rightmost_branch(handle_t *handle,
1029 struct inode *inode,
1030 struct ocfs2_extent_tree *et)
1031{
1032 int status;
1033 struct ocfs2_path *path = NULL;
1034 struct ocfs2_extent_list *el;
1035 struct ocfs2_extent_rec *rec;
1036
1037 path = ocfs2_new_path_from_et(et);
1038 if (!path) {
1039 status = -ENOMEM;
1040 return status;
1041 }
1042
Joel Beckerfacdb772009-02-12 18:08:48 -08001043 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
Tao Ma6b791bc2009-06-12 14:18:36 +08001044 if (status < 0) {
1045 mlog_errno(status);
1046 goto out;
1047 }
1048
1049 status = ocfs2_extend_trans(handle, path_num_items(path) +
1050 handle->h_buffer_credits);
1051 if (status < 0) {
1052 mlog_errno(status);
1053 goto out;
1054 }
1055
Joel Becker0cf2f762009-02-12 16:41:25 -08001056 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Tao Ma6b791bc2009-06-12 14:18:36 +08001057 if (status < 0) {
1058 mlog_errno(status);
1059 goto out;
1060 }
1061
1062 el = path_leaf_el(path);
1063 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1064
1065 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
1066
1067out:
1068 ocfs2_free_path(path);
1069 return status;
1070}
1071
1072/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001073 * Add an entire tree branch to our inode. eb_bh is the extent block
1074 * to start at, if we don't want to start the branch at the dinode
1075 * structure.
1076 *
1077 * last_eb_bh is required as we have to update it's next_leaf pointer
1078 * for the new last extent block.
1079 *
1080 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -08001081 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -08001082 */
1083static int ocfs2_add_branch(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001084 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001085 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001086 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001087 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07001088 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001089 struct ocfs2_alloc_context *meta_ac)
1090{
1091 int status, new_blocks, i;
1092 u64 next_blkno, new_last_eb_blk;
1093 struct buffer_head *bh;
1094 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001095 struct ocfs2_extent_block *eb;
1096 struct ocfs2_extent_list *eb_el;
1097 struct ocfs2_extent_list *el;
Tao Ma6b791bc2009-06-12 14:18:36 +08001098 u32 new_cpos, root_end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001099
1100 mlog_entry_void();
1101
Mark Fasheh328d5752007-06-18 10:48:04 -07001102 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001103
Mark Fashehccd979b2005-12-15 14:31:24 -08001104 if (eb_bh) {
1105 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1106 el = &eb->h_list;
1107 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001108 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001109
1110 /* we never add a branch to a leaf. */
1111 BUG_ON(!el->l_tree_depth);
1112
1113 new_blocks = le16_to_cpu(el->l_tree_depth);
1114
Tao Ma6b791bc2009-06-12 14:18:36 +08001115 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1116 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1117 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1118
1119 /*
1120 * If there is a gap before the root end and the real end
1121 * of the righmost leaf block, we need to remove the gap
1122 * between new_cpos and root_end first so that the tree
1123 * is consistent after we add a new branch(it will start
1124 * from new_cpos).
1125 */
1126 if (root_end > new_cpos) {
1127 mlog(0, "adjust the cluster end from %u to %u\n",
1128 root_end, new_cpos);
1129 status = ocfs2_adjust_rightmost_branch(handle, inode, et);
1130 if (status) {
1131 mlog_errno(status);
1132 goto bail;
1133 }
1134 }
1135
Mark Fashehccd979b2005-12-15 14:31:24 -08001136 /* allocate the number of new eb blocks we need */
1137 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1138 GFP_KERNEL);
1139 if (!new_eb_bhs) {
1140 status = -ENOMEM;
1141 mlog_errno(status);
1142 goto bail;
1143 }
1144
Joel Becker42a5a7a2009-02-12 18:49:19 -08001145 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
Mark Fashehccd979b2005-12-15 14:31:24 -08001146 meta_ac, new_eb_bhs);
1147 if (status < 0) {
1148 mlog_errno(status);
1149 goto bail;
1150 }
1151
1152 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1153 * linked with the rest of the tree.
1154 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1155 *
1156 * when we leave the loop, new_last_eb_blk will point to the
1157 * newest leaf, and next_blkno will point to the topmost extent
1158 * block. */
1159 next_blkno = new_last_eb_blk = 0;
1160 for(i = 0; i < new_blocks; i++) {
1161 bh = new_eb_bhs[i];
1162 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001163 /* ocfs2_create_new_meta_bhs() should create it right! */
1164 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001165 eb_el = &eb->h_list;
1166
Joel Becker0cf2f762009-02-12 16:41:25 -08001167 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), bh,
Joel Becker13723d02008-10-17 19:25:01 -07001168 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001169 if (status < 0) {
1170 mlog_errno(status);
1171 goto bail;
1172 }
1173
1174 eb->h_next_leaf_blk = 0;
1175 eb_el->l_tree_depth = cpu_to_le16(i);
1176 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001177 /*
1178 * This actually counts as an empty extent as
1179 * c_clusters == 0
1180 */
1181 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001182 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001183 /*
1184 * eb_el isn't always an interior node, but even leaf
1185 * nodes want a zero'd flags and reserved field so
1186 * this gets the whole 32 bits regardless of use.
1187 */
1188 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001189 if (!eb_el->l_tree_depth)
1190 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1191
1192 status = ocfs2_journal_dirty(handle, bh);
1193 if (status < 0) {
1194 mlog_errno(status);
1195 goto bail;
1196 }
1197
1198 next_blkno = le64_to_cpu(eb->h_blkno);
1199 }
1200
1201 /* This is a bit hairy. We want to update up to three blocks
1202 * here without leaving any of them in an inconsistent state
1203 * in case of error. We don't have to worry about
1204 * journal_dirty erroring as it won't unless we've aborted the
1205 * handle (in which case we would never be here) so reserving
1206 * the write with journal_access is all we need to do. */
Joel Becker0cf2f762009-02-12 16:41:25 -08001207 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), *last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001208 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001209 if (status < 0) {
1210 mlog_errno(status);
1211 goto bail;
1212 }
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001213 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001214 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001215 if (status < 0) {
1216 mlog_errno(status);
1217 goto bail;
1218 }
1219 if (eb_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08001220 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001221 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001222 if (status < 0) {
1223 mlog_errno(status);
1224 goto bail;
1225 }
1226 }
1227
1228 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001229 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001230 i = le16_to_cpu(el->l_next_free_rec);
1231 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001232 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001233 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001234 le16_add_cpu(&el->l_next_free_rec, 1);
1235
1236 /* fe needs a new last extent block pointer, as does the
1237 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001238 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001239
Mark Fasheh328d5752007-06-18 10:48:04 -07001240 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001241 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1242
Mark Fasheh328d5752007-06-18 10:48:04 -07001243 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001244 if (status < 0)
1245 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001246 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001247 if (status < 0)
1248 mlog_errno(status);
1249 if (eb_bh) {
1250 status = ocfs2_journal_dirty(handle, eb_bh);
1251 if (status < 0)
1252 mlog_errno(status);
1253 }
1254
Mark Fasheh328d5752007-06-18 10:48:04 -07001255 /*
1256 * Some callers want to track the rightmost leaf so pass it
1257 * back here.
1258 */
1259 brelse(*last_eb_bh);
1260 get_bh(new_eb_bhs[0]);
1261 *last_eb_bh = new_eb_bhs[0];
1262
Mark Fashehccd979b2005-12-15 14:31:24 -08001263 status = 0;
1264bail:
1265 if (new_eb_bhs) {
1266 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001267 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001268 kfree(new_eb_bhs);
1269 }
1270
1271 mlog_exit(status);
1272 return status;
1273}
1274
1275/*
1276 * adds another level to the allocation tree.
1277 * returns back the new extent block so you can add a branch to it
1278 * after this call.
1279 */
1280static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001281 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001282 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001283 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001284 struct ocfs2_alloc_context *meta_ac,
1285 struct buffer_head **ret_new_eb_bh)
1286{
1287 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001288 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001289 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001290 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001291 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001292 struct ocfs2_extent_list *eb_el;
1293
1294 mlog_entry_void();
1295
Joel Becker42a5a7a2009-02-12 18:49:19 -08001296 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
Mark Fashehccd979b2005-12-15 14:31:24 -08001297 &new_eb_bh);
1298 if (status < 0) {
1299 mlog_errno(status);
1300 goto bail;
1301 }
1302
1303 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001304 /* ocfs2_create_new_meta_bhs() should create it right! */
1305 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001306
1307 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001308 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001309
Joel Becker0cf2f762009-02-12 16:41:25 -08001310 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), new_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001311 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001312 if (status < 0) {
1313 mlog_errno(status);
1314 goto bail;
1315 }
1316
Tao Mae7d4cb62008-08-18 17:38:44 +08001317 /* copy the root extent list data into the new extent block */
1318 eb_el->l_tree_depth = root_el->l_tree_depth;
1319 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1320 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1321 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001322
1323 status = ocfs2_journal_dirty(handle, new_eb_bh);
1324 if (status < 0) {
1325 mlog_errno(status);
1326 goto bail;
1327 }
1328
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001329 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001330 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001331 if (status < 0) {
1332 mlog_errno(status);
1333 goto bail;
1334 }
1335
Mark Fashehdcd05382007-01-16 11:32:23 -08001336 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1337
Tao Mae7d4cb62008-08-18 17:38:44 +08001338 /* update root_bh now */
1339 le16_add_cpu(&root_el->l_tree_depth, 1);
1340 root_el->l_recs[0].e_cpos = 0;
1341 root_el->l_recs[0].e_blkno = eb->h_blkno;
1342 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1343 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1344 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1345 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001346
1347 /* If this is our 1st tree depth shift, then last_eb_blk
1348 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001349 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001350 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001351
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001352 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001353 if (status < 0) {
1354 mlog_errno(status);
1355 goto bail;
1356 }
1357
1358 *ret_new_eb_bh = new_eb_bh;
1359 new_eb_bh = NULL;
1360 status = 0;
1361bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001362 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001363
1364 mlog_exit(status);
1365 return status;
1366}
1367
1368/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001369 * Should only be called when there is no space left in any of the
1370 * leaf nodes. What we want to do is find the lowest tree depth
1371 * non-leaf extent block with room for new records. There are three
1372 * valid results of this search:
1373 *
1374 * 1) a lowest extent block is found, then we pass it back in
1375 * *lowest_eb_bh and return '0'
1376 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001377 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001378 * pass NULL back in *lowest_eb_bh, but still return '0'
1379 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001380 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001381 * which case we return > 0
1382 *
1383 * return status < 0 indicates an error.
1384 */
1385static int ocfs2_find_branch_target(struct ocfs2_super *osb,
Tao Mae7d4cb62008-08-18 17:38:44 +08001386 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001387 struct buffer_head **target_bh)
1388{
1389 int status = 0, i;
1390 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001391 struct ocfs2_extent_block *eb;
1392 struct ocfs2_extent_list *el;
1393 struct buffer_head *bh = NULL;
1394 struct buffer_head *lowest_bh = NULL;
1395
1396 mlog_entry_void();
1397
1398 *target_bh = NULL;
1399
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001400 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001401
1402 while(le16_to_cpu(el->l_tree_depth) > 1) {
1403 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08001404 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1405 "Owner %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001406 "extent list (next_free_rec == 0)",
Joel Becker3d03a302009-02-12 17:49:26 -08001407 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001408 status = -EIO;
1409 goto bail;
1410 }
1411 i = le16_to_cpu(el->l_next_free_rec) - 1;
1412 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1413 if (!blkno) {
Joel Becker3d03a302009-02-12 17:49:26 -08001414 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1415 "Owner %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001416 "list where extent # %d has no physical "
1417 "block start",
Joel Becker3d03a302009-02-12 17:49:26 -08001418 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001419 status = -EIO;
1420 goto bail;
1421 }
1422
Mark Fasheha81cb882008-10-07 14:25:16 -07001423 brelse(bh);
1424 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001425
Joel Becker3d03a302009-02-12 17:49:26 -08001426 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001427 if (status < 0) {
1428 mlog_errno(status);
1429 goto bail;
1430 }
1431
1432 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001433 el = &eb->h_list;
1434
1435 if (le16_to_cpu(el->l_next_free_rec) <
1436 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001437 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001438 lowest_bh = bh;
1439 get_bh(lowest_bh);
1440 }
1441 }
1442
1443 /* If we didn't find one and the fe doesn't have any room,
1444 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001445 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001446 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001447 status = 1;
1448
1449 *target_bh = lowest_bh;
1450bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001451 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001452
1453 mlog_exit(status);
1454 return status;
1455}
1456
Mark Fashehe48edee2007-03-07 16:46:57 -08001457/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001458 * Grow a b-tree so that it has more records.
1459 *
1460 * We might shift the tree depth in which case existing paths should
1461 * be considered invalid.
1462 *
1463 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001464 *
1465 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001466 */
1467static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001468 struct ocfs2_extent_tree *et, int *final_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07001469 struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001470 struct ocfs2_alloc_context *meta_ac)
1471{
1472 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001473 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001474 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001475 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1476 struct buffer_head *bh = NULL;
1477
1478 BUG_ON(meta_ac == NULL);
1479
Joel Becker3d03a302009-02-12 17:49:26 -08001480 shift = ocfs2_find_branch_target(osb, et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001481 if (shift < 0) {
1482 ret = shift;
1483 mlog_errno(ret);
1484 goto out;
1485 }
1486
1487 /* We traveled all the way to the bottom of the allocation tree
1488 * and didn't find room for any more extents - we need to add
1489 * another tree level */
1490 if (shift) {
1491 BUG_ON(bh);
1492 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1493
1494 /* ocfs2_shift_tree_depth will return us a buffer with
1495 * the new extent block (so we can pass that to
1496 * ocfs2_add_branch). */
Tao Mae7d4cb62008-08-18 17:38:44 +08001497 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001498 meta_ac, &bh);
1499 if (ret < 0) {
1500 mlog_errno(ret);
1501 goto out;
1502 }
1503 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001504 if (depth == 1) {
1505 /*
1506 * Special case: we have room now if we shifted from
1507 * tree_depth 0, so no more work needs to be done.
1508 *
1509 * We won't be calling add_branch, so pass
1510 * back *last_eb_bh as the new leaf. At depth
1511 * zero, it should always be null so there's
1512 * no reason to brelse.
1513 */
1514 BUG_ON(*last_eb_bh);
1515 get_bh(bh);
1516 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001517 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001518 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001519 }
1520
1521 /* call ocfs2_add_branch to add the final part of the tree with
1522 * the new data. */
1523 mlog(0, "add branch. bh = %p\n", bh);
Tao Mae7d4cb62008-08-18 17:38:44 +08001524 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001525 meta_ac);
1526 if (ret < 0) {
1527 mlog_errno(ret);
1528 goto out;
1529 }
1530
1531out:
1532 if (final_depth)
1533 *final_depth = depth;
1534 brelse(bh);
1535 return ret;
1536}
1537
1538/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001539 * This function will discard the rightmost extent record.
1540 */
1541static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1542{
1543 int next_free = le16_to_cpu(el->l_next_free_rec);
1544 int count = le16_to_cpu(el->l_count);
1545 unsigned int num_bytes;
1546
1547 BUG_ON(!next_free);
1548 /* This will cause us to go off the end of our extent list. */
1549 BUG_ON(next_free >= count);
1550
1551 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1552
1553 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1554}
1555
1556static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1557 struct ocfs2_extent_rec *insert_rec)
1558{
1559 int i, insert_index, next_free, has_empty, num_bytes;
1560 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1561 struct ocfs2_extent_rec *rec;
1562
1563 next_free = le16_to_cpu(el->l_next_free_rec);
1564 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1565
1566 BUG_ON(!next_free);
1567
1568 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001569 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001570
1571 /*
1572 * The easiest way to approach this is to just remove the
1573 * empty extent and temporarily decrement next_free.
1574 */
1575 if (has_empty) {
1576 /*
1577 * If next_free was 1 (only an empty extent), this
1578 * loop won't execute, which is fine. We still want
1579 * the decrement above to happen.
1580 */
1581 for(i = 0; i < (next_free - 1); i++)
1582 el->l_recs[i] = el->l_recs[i+1];
1583
1584 next_free--;
1585 }
1586
1587 /*
1588 * Figure out what the new record index should be.
1589 */
1590 for(i = 0; i < next_free; i++) {
1591 rec = &el->l_recs[i];
1592
1593 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1594 break;
1595 }
1596 insert_index = i;
1597
1598 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1599 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1600
1601 BUG_ON(insert_index < 0);
1602 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1603 BUG_ON(insert_index > next_free);
1604
1605 /*
1606 * No need to memmove if we're just adding to the tail.
1607 */
1608 if (insert_index != next_free) {
1609 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1610
1611 num_bytes = next_free - insert_index;
1612 num_bytes *= sizeof(struct ocfs2_extent_rec);
1613 memmove(&el->l_recs[insert_index + 1],
1614 &el->l_recs[insert_index],
1615 num_bytes);
1616 }
1617
1618 /*
1619 * Either we had an empty extent, and need to re-increment or
1620 * there was no empty extent on a non full rightmost leaf node,
1621 * in which case we still need to increment.
1622 */
1623 next_free++;
1624 el->l_next_free_rec = cpu_to_le16(next_free);
1625 /*
1626 * Make sure none of the math above just messed up our tree.
1627 */
1628 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1629
1630 el->l_recs[insert_index] = *insert_rec;
1631
1632}
1633
Mark Fasheh328d5752007-06-18 10:48:04 -07001634static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1635{
1636 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1637
1638 BUG_ON(num_recs == 0);
1639
1640 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1641 num_recs--;
1642 size = num_recs * sizeof(struct ocfs2_extent_rec);
1643 memmove(&el->l_recs[0], &el->l_recs[1], size);
1644 memset(&el->l_recs[num_recs], 0,
1645 sizeof(struct ocfs2_extent_rec));
1646 el->l_next_free_rec = cpu_to_le16(num_recs);
1647 }
1648}
1649
Mark Fashehdcd05382007-01-16 11:32:23 -08001650/*
1651 * Create an empty extent record .
1652 *
1653 * l_next_free_rec may be updated.
1654 *
1655 * If an empty extent already exists do nothing.
1656 */
1657static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1658{
1659 int next_free = le16_to_cpu(el->l_next_free_rec);
1660
Mark Fashehe48edee2007-03-07 16:46:57 -08001661 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1662
Mark Fashehdcd05382007-01-16 11:32:23 -08001663 if (next_free == 0)
1664 goto set_and_inc;
1665
1666 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1667 return;
1668
1669 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1670 "Asked to create an empty extent in a full list:\n"
1671 "count = %u, tree depth = %u",
1672 le16_to_cpu(el->l_count),
1673 le16_to_cpu(el->l_tree_depth));
1674
1675 ocfs2_shift_records_right(el);
1676
1677set_and_inc:
1678 le16_add_cpu(&el->l_next_free_rec, 1);
1679 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1680}
1681
1682/*
1683 * For a rotation which involves two leaf nodes, the "root node" is
1684 * the lowest level tree node which contains a path to both leafs. This
1685 * resulting set of information can be used to form a complete "subtree"
1686 *
1687 * This function is passed two full paths from the dinode down to a
1688 * pair of adjacent leaves. It's task is to figure out which path
1689 * index contains the subtree root - this can be the root index itself
1690 * in a worst-case rotation.
1691 *
1692 * The array index of the subtree root is passed back.
1693 */
1694static int ocfs2_find_subtree_root(struct inode *inode,
1695 struct ocfs2_path *left,
1696 struct ocfs2_path *right)
1697{
1698 int i = 0;
1699
1700 /*
1701 * Check that the caller passed in two paths from the same tree.
1702 */
1703 BUG_ON(path_root_bh(left) != path_root_bh(right));
1704
1705 do {
1706 i++;
1707
1708 /*
1709 * The caller didn't pass two adjacent paths.
1710 */
1711 mlog_bug_on_msg(i > left->p_tree_depth,
1712 "Inode %lu, left depth %u, right depth %u\n"
1713 "left leaf blk %llu, right leaf blk %llu\n",
1714 inode->i_ino, left->p_tree_depth,
1715 right->p_tree_depth,
1716 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1717 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1718 } while (left->p_node[i].bh->b_blocknr ==
1719 right->p_node[i].bh->b_blocknr);
1720
1721 return i - 1;
1722}
1723
1724typedef void (path_insert_t)(void *, struct buffer_head *);
1725
1726/*
1727 * Traverse a btree path in search of cpos, starting at root_el.
1728 *
1729 * This code can be called with a cpos larger than the tree, in which
1730 * case it will return the rightmost path.
1731 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001732static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
Mark Fashehdcd05382007-01-16 11:32:23 -08001733 struct ocfs2_extent_list *root_el, u32 cpos,
1734 path_insert_t *func, void *data)
1735{
1736 int i, ret = 0;
1737 u32 range;
1738 u64 blkno;
1739 struct buffer_head *bh = NULL;
1740 struct ocfs2_extent_block *eb;
1741 struct ocfs2_extent_list *el;
1742 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08001743
1744 el = root_el;
1745 while (el->l_tree_depth) {
1746 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001747 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1748 "Owner %llu has empty extent list at "
Mark Fashehdcd05382007-01-16 11:32:23 -08001749 "depth %u\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001750 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001751 le16_to_cpu(el->l_tree_depth));
1752 ret = -EROFS;
1753 goto out;
1754
1755 }
1756
1757 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1758 rec = &el->l_recs[i];
1759
1760 /*
1761 * In the case that cpos is off the allocation
1762 * tree, this should just wind up returning the
1763 * rightmost record.
1764 */
1765 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001766 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001767 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1768 break;
1769 }
1770
1771 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1772 if (blkno == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001773 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1774 "Owner %llu has bad blkno in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001775 "at depth %u (index %d)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001776 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001777 le16_to_cpu(el->l_tree_depth), i);
1778 ret = -EROFS;
1779 goto out;
1780 }
1781
1782 brelse(bh);
1783 bh = NULL;
Joel Beckerfacdb772009-02-12 18:08:48 -08001784 ret = ocfs2_read_extent_block(ci, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001785 if (ret) {
1786 mlog_errno(ret);
1787 goto out;
1788 }
1789
1790 eb = (struct ocfs2_extent_block *) bh->b_data;
1791 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001792
1793 if (le16_to_cpu(el->l_next_free_rec) >
1794 le16_to_cpu(el->l_count)) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001795 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1796 "Owner %llu has bad count in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001797 "at block %llu (next free=%u, count=%u)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001798 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001799 (unsigned long long)bh->b_blocknr,
1800 le16_to_cpu(el->l_next_free_rec),
1801 le16_to_cpu(el->l_count));
1802 ret = -EROFS;
1803 goto out;
1804 }
1805
1806 if (func)
1807 func(data, bh);
1808 }
1809
1810out:
1811 /*
1812 * Catch any trailing bh that the loop didn't handle.
1813 */
1814 brelse(bh);
1815
1816 return ret;
1817}
1818
1819/*
1820 * Given an initialized path (that is, it has a valid root extent
1821 * list), this function will traverse the btree in search of the path
1822 * which would contain cpos.
1823 *
1824 * The path traveled is recorded in the path structure.
1825 *
1826 * Note that this will not do any comparisons on leaf node extent
1827 * records, so it will work fine in the case that we just added a tree
1828 * branch.
1829 */
1830struct find_path_data {
1831 int index;
1832 struct ocfs2_path *path;
1833};
1834static void find_path_ins(void *data, struct buffer_head *bh)
1835{
1836 struct find_path_data *fp = data;
1837
1838 get_bh(bh);
1839 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1840 fp->index++;
1841}
Joel Beckerfacdb772009-02-12 18:08:48 -08001842static int ocfs2_find_path(struct ocfs2_caching_info *ci,
1843 struct ocfs2_path *path, u32 cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08001844{
1845 struct find_path_data data;
1846
1847 data.index = 1;
1848 data.path = path;
Joel Beckerfacdb772009-02-12 18:08:48 -08001849 return __ocfs2_find_path(ci, path_root_el(path), cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08001850 find_path_ins, &data);
1851}
1852
1853static void find_leaf_ins(void *data, struct buffer_head *bh)
1854{
1855 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1856 struct ocfs2_extent_list *el = &eb->h_list;
1857 struct buffer_head **ret = data;
1858
1859 /* We want to retain only the leaf block. */
1860 if (le16_to_cpu(el->l_tree_depth) == 0) {
1861 get_bh(bh);
1862 *ret = bh;
1863 }
1864}
1865/*
1866 * Find the leaf block in the tree which would contain cpos. No
1867 * checking of the actual leaf is done.
1868 *
1869 * Some paths want to call this instead of allocating a path structure
1870 * and calling ocfs2_find_path().
1871 *
1872 * This function doesn't handle non btree extent lists.
1873 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001874int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1875 struct ocfs2_extent_list *root_el, u32 cpos,
1876 struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001877{
1878 int ret;
1879 struct buffer_head *bh = NULL;
1880
Joel Beckerfacdb772009-02-12 18:08:48 -08001881 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001882 if (ret) {
1883 mlog_errno(ret);
1884 goto out;
1885 }
1886
1887 *leaf_bh = bh;
1888out:
1889 return ret;
1890}
1891
1892/*
1893 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1894 *
1895 * Basically, we've moved stuff around at the bottom of the tree and
1896 * we need to fix up the extent records above the changes to reflect
1897 * the new changes.
1898 *
1899 * left_rec: the record on the left.
1900 * left_child_el: is the child list pointed to by left_rec
1901 * right_rec: the record to the right of left_rec
1902 * right_child_el: is the child list pointed to by right_rec
1903 *
1904 * By definition, this only works on interior nodes.
1905 */
1906static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1907 struct ocfs2_extent_list *left_child_el,
1908 struct ocfs2_extent_rec *right_rec,
1909 struct ocfs2_extent_list *right_child_el)
1910{
1911 u32 left_clusters, right_end;
1912
1913 /*
1914 * Interior nodes never have holes. Their cpos is the cpos of
1915 * the leftmost record in their child list. Their cluster
1916 * count covers the full theoretical range of their child list
1917 * - the range between their cpos and the cpos of the record
1918 * immediately to their right.
1919 */
1920 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08001921 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1922 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07001923 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1924 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1925 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001926 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001927 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001928
1929 /*
1930 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001931 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001932 * updating e_cpos to keep the same highest cluster count.
1933 */
1934 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001935 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001936
1937 right_rec->e_cpos = left_rec->e_cpos;
1938 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1939
1940 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001941 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001942}
1943
1944/*
1945 * Adjust the adjacent root node records involved in a
1946 * rotation. left_el_blkno is passed in as a key so that we can easily
1947 * find it's index in the root list.
1948 */
1949static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1950 struct ocfs2_extent_list *left_el,
1951 struct ocfs2_extent_list *right_el,
1952 u64 left_el_blkno)
1953{
1954 int i;
1955
1956 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1957 le16_to_cpu(left_el->l_tree_depth));
1958
1959 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1960 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1961 break;
1962 }
1963
1964 /*
1965 * The path walking code should have never returned a root and
1966 * two paths which are not adjacent.
1967 */
1968 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1969
1970 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1971 &root_el->l_recs[i + 1], right_el);
1972}
1973
1974/*
1975 * We've changed a leaf block (in right_path) and need to reflect that
1976 * change back up the subtree.
1977 *
1978 * This happens in multiple places:
1979 * - When we've moved an extent record from the left path leaf to the right
1980 * path leaf to make room for an empty extent in the left path leaf.
1981 * - When our insert into the right path leaf is at the leftmost edge
1982 * and requires an update of the path immediately to it's left. This
1983 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08001984 * - When we've adjusted the last extent record in the left path leaf and the
1985 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08001986 */
1987static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1988 struct ocfs2_path *left_path,
1989 struct ocfs2_path *right_path,
1990 int subtree_index)
1991{
1992 int ret, i, idx;
1993 struct ocfs2_extent_list *el, *left_el, *right_el;
1994 struct ocfs2_extent_rec *left_rec, *right_rec;
1995 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1996
1997 /*
1998 * Update the counts and position values within all the
1999 * interior nodes to reflect the leaf rotation we just did.
2000 *
2001 * The root node is handled below the loop.
2002 *
2003 * We begin the loop with right_el and left_el pointing to the
2004 * leaf lists and work our way up.
2005 *
2006 * NOTE: within this loop, left_el and right_el always refer
2007 * to the *child* lists.
2008 */
2009 left_el = path_leaf_el(left_path);
2010 right_el = path_leaf_el(right_path);
2011 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2012 mlog(0, "Adjust records at index %u\n", i);
2013
2014 /*
2015 * One nice property of knowing that all of these
2016 * nodes are below the root is that we only deal with
2017 * the leftmost right node record and the rightmost
2018 * left node record.
2019 */
2020 el = left_path->p_node[i].el;
2021 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2022 left_rec = &el->l_recs[idx];
2023
2024 el = right_path->p_node[i].el;
2025 right_rec = &el->l_recs[0];
2026
2027 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2028 right_el);
2029
2030 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2031 if (ret)
2032 mlog_errno(ret);
2033
2034 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2035 if (ret)
2036 mlog_errno(ret);
2037
2038 /*
2039 * Setup our list pointers now so that the current
2040 * parents become children in the next iteration.
2041 */
2042 left_el = left_path->p_node[i].el;
2043 right_el = right_path->p_node[i].el;
2044 }
2045
2046 /*
2047 * At the root node, adjust the two adjacent records which
2048 * begin our path to the leaves.
2049 */
2050
2051 el = left_path->p_node[subtree_index].el;
2052 left_el = left_path->p_node[subtree_index + 1].el;
2053 right_el = right_path->p_node[subtree_index + 1].el;
2054
2055 ocfs2_adjust_root_records(el, left_el, right_el,
2056 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2057
2058 root_bh = left_path->p_node[subtree_index].bh;
2059
2060 ret = ocfs2_journal_dirty(handle, root_bh);
2061 if (ret)
2062 mlog_errno(ret);
2063}
2064
2065static int ocfs2_rotate_subtree_right(struct inode *inode,
2066 handle_t *handle,
2067 struct ocfs2_path *left_path,
2068 struct ocfs2_path *right_path,
2069 int subtree_index)
2070{
2071 int ret, i;
2072 struct buffer_head *right_leaf_bh;
2073 struct buffer_head *left_leaf_bh = NULL;
2074 struct buffer_head *root_bh;
2075 struct ocfs2_extent_list *right_el, *left_el;
2076 struct ocfs2_extent_rec move_rec;
2077
2078 left_leaf_bh = path_leaf_bh(left_path);
2079 left_el = path_leaf_el(left_path);
2080
2081 if (left_el->l_next_free_rec != left_el->l_count) {
2082 ocfs2_error(inode->i_sb,
2083 "Inode %llu has non-full interior leaf node %llu"
2084 "(next free = %u)",
2085 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2086 (unsigned long long)left_leaf_bh->b_blocknr,
2087 le16_to_cpu(left_el->l_next_free_rec));
2088 return -EROFS;
2089 }
2090
2091 /*
2092 * This extent block may already have an empty record, so we
2093 * return early if so.
2094 */
2095 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2096 return 0;
2097
2098 root_bh = left_path->p_node[subtree_index].bh;
2099 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2100
Joel Becker0cf2f762009-02-12 16:41:25 -08002101 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002102 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002103 if (ret) {
2104 mlog_errno(ret);
2105 goto out;
2106 }
2107
2108 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -08002109 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002110 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002111 if (ret) {
2112 mlog_errno(ret);
2113 goto out;
2114 }
2115
Joel Becker0cf2f762009-02-12 16:41:25 -08002116 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002117 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002118 if (ret) {
2119 mlog_errno(ret);
2120 goto out;
2121 }
2122 }
2123
2124 right_leaf_bh = path_leaf_bh(right_path);
2125 right_el = path_leaf_el(right_path);
2126
2127 /* This is a code error, not a disk corruption. */
2128 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2129 "because rightmost leaf block %llu is empty\n",
2130 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2131 (unsigned long long)right_leaf_bh->b_blocknr);
2132
2133 ocfs2_create_empty_extent(right_el);
2134
2135 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2136 if (ret) {
2137 mlog_errno(ret);
2138 goto out;
2139 }
2140
2141 /* Do the copy now. */
2142 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2143 move_rec = left_el->l_recs[i];
2144 right_el->l_recs[0] = move_rec;
2145
2146 /*
2147 * Clear out the record we just copied and shift everything
2148 * over, leaving an empty extent in the left leaf.
2149 *
2150 * We temporarily subtract from next_free_rec so that the
2151 * shift will lose the tail record (which is now defunct).
2152 */
2153 le16_add_cpu(&left_el->l_next_free_rec, -1);
2154 ocfs2_shift_records_right(left_el);
2155 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2156 le16_add_cpu(&left_el->l_next_free_rec, 1);
2157
2158 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2159 if (ret) {
2160 mlog_errno(ret);
2161 goto out;
2162 }
2163
2164 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2165 subtree_index);
2166
2167out:
2168 return ret;
2169}
2170
2171/*
2172 * Given a full path, determine what cpos value would return us a path
2173 * containing the leaf immediately to the left of the current one.
2174 *
2175 * Will return zero if the path passed in is already the leftmost path.
2176 */
2177static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2178 struct ocfs2_path *path, u32 *cpos)
2179{
2180 int i, j, ret = 0;
2181 u64 blkno;
2182 struct ocfs2_extent_list *el;
2183
Mark Fashehe48edee2007-03-07 16:46:57 -08002184 BUG_ON(path->p_tree_depth == 0);
2185
Mark Fashehdcd05382007-01-16 11:32:23 -08002186 *cpos = 0;
2187
2188 blkno = path_leaf_bh(path)->b_blocknr;
2189
2190 /* Start at the tree node just above the leaf and work our way up. */
2191 i = path->p_tree_depth - 1;
2192 while (i >= 0) {
2193 el = path->p_node[i].el;
2194
2195 /*
2196 * Find the extent record just before the one in our
2197 * path.
2198 */
2199 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2200 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2201 if (j == 0) {
2202 if (i == 0) {
2203 /*
2204 * We've determined that the
2205 * path specified is already
2206 * the leftmost one - return a
2207 * cpos of zero.
2208 */
2209 goto out;
2210 }
2211 /*
2212 * The leftmost record points to our
2213 * leaf - we need to travel up the
2214 * tree one level.
2215 */
2216 goto next_node;
2217 }
2218
2219 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002220 *cpos = *cpos + ocfs2_rec_clusters(el,
2221 &el->l_recs[j - 1]);
2222 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002223 goto out;
2224 }
2225 }
2226
2227 /*
2228 * If we got here, we never found a valid node where
2229 * the tree indicated one should be.
2230 */
2231 ocfs2_error(sb,
2232 "Invalid extent tree at extent block %llu\n",
2233 (unsigned long long)blkno);
2234 ret = -EROFS;
2235 goto out;
2236
2237next_node:
2238 blkno = path->p_node[i].bh->b_blocknr;
2239 i--;
2240 }
2241
2242out:
2243 return ret;
2244}
2245
Mark Fasheh328d5752007-06-18 10:48:04 -07002246/*
2247 * Extend the transaction by enough credits to complete the rotation,
2248 * and still leave at least the original number of credits allocated
2249 * to this transaction.
2250 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002251static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002252 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002253 struct ocfs2_path *path)
2254{
Mark Fasheh328d5752007-06-18 10:48:04 -07002255 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002256
2257 if (handle->h_buffer_credits < credits)
2258 return ocfs2_extend_trans(handle, credits);
2259
2260 return 0;
2261}
2262
2263/*
2264 * Trap the case where we're inserting into the theoretical range past
2265 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2266 * whose cpos is less than ours into the right leaf.
2267 *
2268 * It's only necessary to look at the rightmost record of the left
2269 * leaf because the logic that calls us should ensure that the
2270 * theoretical ranges in the path components above the leaves are
2271 * correct.
2272 */
2273static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2274 u32 insert_cpos)
2275{
2276 struct ocfs2_extent_list *left_el;
2277 struct ocfs2_extent_rec *rec;
2278 int next_free;
2279
2280 left_el = path_leaf_el(left_path);
2281 next_free = le16_to_cpu(left_el->l_next_free_rec);
2282 rec = &left_el->l_recs[next_free - 1];
2283
2284 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2285 return 1;
2286 return 0;
2287}
2288
Mark Fasheh328d5752007-06-18 10:48:04 -07002289static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2290{
2291 int next_free = le16_to_cpu(el->l_next_free_rec);
2292 unsigned int range;
2293 struct ocfs2_extent_rec *rec;
2294
2295 if (next_free == 0)
2296 return 0;
2297
2298 rec = &el->l_recs[0];
2299 if (ocfs2_is_empty_extent(rec)) {
2300 /* Empty list. */
2301 if (next_free == 1)
2302 return 0;
2303 rec = &el->l_recs[1];
2304 }
2305
2306 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2307 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2308 return 1;
2309 return 0;
2310}
2311
Mark Fashehdcd05382007-01-16 11:32:23 -08002312/*
2313 * Rotate all the records in a btree right one record, starting at insert_cpos.
2314 *
2315 * The path to the rightmost leaf should be passed in.
2316 *
2317 * The array is assumed to be large enough to hold an entire path (tree depth).
2318 *
2319 * Upon succesful return from this function:
2320 *
2321 * - The 'right_path' array will contain a path to the leaf block
2322 * whose range contains e_cpos.
2323 * - That leaf block will have a single empty extent in list index 0.
2324 * - In the case that the rotation requires a post-insert update,
2325 * *ret_left_path will contain a valid path which can be passed to
2326 * ocfs2_insert_path().
2327 */
2328static int ocfs2_rotate_tree_right(struct inode *inode,
2329 handle_t *handle,
Mark Fasheh328d5752007-06-18 10:48:04 -07002330 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002331 u32 insert_cpos,
2332 struct ocfs2_path *right_path,
2333 struct ocfs2_path **ret_left_path)
2334{
Mark Fasheh328d5752007-06-18 10:48:04 -07002335 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002336 u32 cpos;
2337 struct ocfs2_path *left_path = NULL;
2338
2339 *ret_left_path = NULL;
2340
Joel Beckerffdd7a52008-10-17 22:32:01 -07002341 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002342 if (!left_path) {
2343 ret = -ENOMEM;
2344 mlog_errno(ret);
2345 goto out;
2346 }
2347
2348 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2349 if (ret) {
2350 mlog_errno(ret);
2351 goto out;
2352 }
2353
2354 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2355
2356 /*
2357 * What we want to do here is:
2358 *
2359 * 1) Start with the rightmost path.
2360 *
2361 * 2) Determine a path to the leaf block directly to the left
2362 * of that leaf.
2363 *
2364 * 3) Determine the 'subtree root' - the lowest level tree node
2365 * which contains a path to both leaves.
2366 *
2367 * 4) Rotate the subtree.
2368 *
2369 * 5) Find the next subtree by considering the left path to be
2370 * the new right path.
2371 *
2372 * The check at the top of this while loop also accepts
2373 * insert_cpos == cpos because cpos is only a _theoretical_
2374 * value to get us the left path - insert_cpos might very well
2375 * be filling that hole.
2376 *
2377 * Stop at a cpos of '0' because we either started at the
2378 * leftmost branch (i.e., a tree with one branch and a
2379 * rotation inside of it), or we've gone as far as we can in
2380 * rotating subtrees.
2381 */
2382 while (cpos && insert_cpos <= cpos) {
2383 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2384 insert_cpos, cpos);
2385
Joel Beckerfacdb772009-02-12 18:08:48 -08002386 ret = ocfs2_find_path(INODE_CACHE(inode), left_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002387 if (ret) {
2388 mlog_errno(ret);
2389 goto out;
2390 }
2391
2392 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2393 path_leaf_bh(right_path),
2394 "Inode %lu: error during insert of %u "
2395 "(left path cpos %u) results in two identical "
2396 "paths ending at %llu\n",
2397 inode->i_ino, insert_cpos, cpos,
2398 (unsigned long long)
2399 path_leaf_bh(left_path)->b_blocknr);
2400
Mark Fasheh328d5752007-06-18 10:48:04 -07002401 if (split == SPLIT_NONE &&
2402 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002403 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002404
2405 /*
2406 * We've rotated the tree as much as we
2407 * should. The rest is up to
2408 * ocfs2_insert_path() to complete, after the
2409 * record insertion. We indicate this
2410 * situation by returning the left path.
2411 *
2412 * The reason we don't adjust the records here
2413 * before the record insert is that an error
2414 * later might break the rule where a parent
2415 * record e_cpos will reflect the actual
2416 * e_cpos of the 1st nonempty record of the
2417 * child list.
2418 */
2419 *ret_left_path = left_path;
2420 goto out_ret_path;
2421 }
2422
2423 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2424
2425 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2426 start,
2427 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2428 right_path->p_tree_depth);
2429
2430 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002431 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002432 if (ret) {
2433 mlog_errno(ret);
2434 goto out;
2435 }
2436
2437 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2438 right_path, start);
2439 if (ret) {
2440 mlog_errno(ret);
2441 goto out;
2442 }
2443
Mark Fasheh328d5752007-06-18 10:48:04 -07002444 if (split != SPLIT_NONE &&
2445 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2446 insert_cpos)) {
2447 /*
2448 * A rotate moves the rightmost left leaf
2449 * record over to the leftmost right leaf
2450 * slot. If we're doing an extent split
2451 * instead of a real insert, then we have to
2452 * check that the extent to be split wasn't
2453 * just moved over. If it was, then we can
2454 * exit here, passing left_path back -
2455 * ocfs2_split_extent() is smart enough to
2456 * search both leaves.
2457 */
2458 *ret_left_path = left_path;
2459 goto out_ret_path;
2460 }
2461
Mark Fashehdcd05382007-01-16 11:32:23 -08002462 /*
2463 * There is no need to re-read the next right path
2464 * as we know that it'll be our current left
2465 * path. Optimize by copying values instead.
2466 */
2467 ocfs2_mv_path(right_path, left_path);
2468
2469 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2470 &cpos);
2471 if (ret) {
2472 mlog_errno(ret);
2473 goto out;
2474 }
2475 }
2476
2477out:
2478 ocfs2_free_path(left_path);
2479
2480out_ret_path:
2481 return ret;
2482}
2483
Tao Ma3c5e1062009-07-21 15:42:05 +08002484static int ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2485 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002486{
Tao Ma3c5e1062009-07-21 15:42:05 +08002487 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002488 struct ocfs2_extent_rec *rec;
2489 struct ocfs2_extent_list *el;
2490 struct ocfs2_extent_block *eb;
2491 u32 range;
2492
Tao Ma3c5e1062009-07-21 15:42:05 +08002493 /*
2494 * In normal tree rotation process, we will never touch the
2495 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2496 * doesn't reserve the credits for them either.
2497 *
2498 * But we do have a special case here which will update the rightmost
2499 * records for all the bh in the path.
2500 * So we have to allocate extra credits and access them.
2501 */
2502 ret = ocfs2_extend_trans(handle,
2503 handle->h_buffer_credits + subtree_index);
2504 if (ret) {
2505 mlog_errno(ret);
2506 goto out;
2507 }
2508
Joel Becker0cf2f762009-02-12 16:41:25 -08002509 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002510 if (ret) {
2511 mlog_errno(ret);
2512 goto out;
2513 }
2514
Mark Fasheh328d5752007-06-18 10:48:04 -07002515 /* Path should always be rightmost. */
2516 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2517 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2518
2519 el = &eb->h_list;
2520 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2521 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2522 rec = &el->l_recs[idx];
2523 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2524
2525 for (i = 0; i < path->p_tree_depth; i++) {
2526 el = path->p_node[i].el;
2527 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2528 rec = &el->l_recs[idx];
2529
2530 rec->e_int_clusters = cpu_to_le32(range);
2531 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2532
2533 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2534 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002535out:
2536 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002537}
2538
2539static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2540 struct ocfs2_cached_dealloc_ctxt *dealloc,
2541 struct ocfs2_path *path, int unlink_start)
2542{
2543 int ret, i;
2544 struct ocfs2_extent_block *eb;
2545 struct ocfs2_extent_list *el;
2546 struct buffer_head *bh;
2547
2548 for(i = unlink_start; i < path_num_items(path); i++) {
2549 bh = path->p_node[i].bh;
2550
2551 eb = (struct ocfs2_extent_block *)bh->b_data;
2552 /*
2553 * Not all nodes might have had their final count
2554 * decremented by the caller - handle this here.
2555 */
2556 el = &eb->h_list;
2557 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2558 mlog(ML_ERROR,
2559 "Inode %llu, attempted to remove extent block "
2560 "%llu with %u records\n",
2561 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2562 (unsigned long long)le64_to_cpu(eb->h_blkno),
2563 le16_to_cpu(el->l_next_free_rec));
2564
2565 ocfs2_journal_dirty(handle, bh);
Joel Becker8cb471e2009-02-10 20:00:41 -08002566 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002567 continue;
2568 }
2569
2570 el->l_next_free_rec = 0;
2571 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2572
2573 ocfs2_journal_dirty(handle, bh);
2574
2575 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2576 if (ret)
2577 mlog_errno(ret);
2578
Joel Becker8cb471e2009-02-10 20:00:41 -08002579 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002580 }
2581}
2582
2583static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2584 struct ocfs2_path *left_path,
2585 struct ocfs2_path *right_path,
2586 int subtree_index,
2587 struct ocfs2_cached_dealloc_ctxt *dealloc)
2588{
2589 int i;
2590 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2591 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2592 struct ocfs2_extent_list *el;
2593 struct ocfs2_extent_block *eb;
2594
2595 el = path_leaf_el(left_path);
2596
2597 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2598
2599 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2600 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2601 break;
2602
2603 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2604
2605 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2606 le16_add_cpu(&root_el->l_next_free_rec, -1);
2607
2608 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2609 eb->h_next_leaf_blk = 0;
2610
2611 ocfs2_journal_dirty(handle, root_bh);
2612 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2613
2614 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2615 subtree_index + 1);
2616}
2617
2618static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2619 struct ocfs2_path *left_path,
2620 struct ocfs2_path *right_path,
2621 int subtree_index,
2622 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002623 int *deleted,
2624 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002625{
2626 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002627 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002628 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2629 struct ocfs2_extent_block *eb;
2630
2631 *deleted = 0;
2632
2633 right_leaf_el = path_leaf_el(right_path);
2634 left_leaf_el = path_leaf_el(left_path);
2635 root_bh = left_path->p_node[subtree_index].bh;
2636 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2637
2638 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2639 return 0;
2640
2641 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2642 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2643 /*
2644 * It's legal for us to proceed if the right leaf is
2645 * the rightmost one and it has an empty extent. There
2646 * are two cases to handle - whether the leaf will be
2647 * empty after removal or not. If the leaf isn't empty
2648 * then just remove the empty extent up front. The
2649 * next block will handle empty leaves by flagging
2650 * them for unlink.
2651 *
2652 * Non rightmost leaves will throw -EAGAIN and the
2653 * caller can manually move the subtree and retry.
2654 */
2655
2656 if (eb->h_next_leaf_blk != 0ULL)
2657 return -EAGAIN;
2658
2659 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker0cf2f762009-02-12 16:41:25 -08002660 ret = ocfs2_journal_access_eb(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002661 path_leaf_bh(right_path),
2662 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002663 if (ret) {
2664 mlog_errno(ret);
2665 goto out;
2666 }
2667
2668 ocfs2_remove_empty_extent(right_leaf_el);
2669 } else
2670 right_has_empty = 1;
2671 }
2672
2673 if (eb->h_next_leaf_blk == 0ULL &&
2674 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2675 /*
2676 * We have to update i_last_eb_blk during the meta
2677 * data delete.
2678 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08002679 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07002680 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002681 if (ret) {
2682 mlog_errno(ret);
2683 goto out;
2684 }
2685
2686 del_right_subtree = 1;
2687 }
2688
2689 /*
2690 * Getting here with an empty extent in the right path implies
2691 * that it's the rightmost path and will be deleted.
2692 */
2693 BUG_ON(right_has_empty && !del_right_subtree);
2694
Joel Becker0cf2f762009-02-12 16:41:25 -08002695 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002696 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002697 if (ret) {
2698 mlog_errno(ret);
2699 goto out;
2700 }
2701
2702 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -08002703 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002704 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002705 if (ret) {
2706 mlog_errno(ret);
2707 goto out;
2708 }
2709
Joel Becker0cf2f762009-02-12 16:41:25 -08002710 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002711 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002712 if (ret) {
2713 mlog_errno(ret);
2714 goto out;
2715 }
2716 }
2717
2718 if (!right_has_empty) {
2719 /*
2720 * Only do this if we're moving a real
2721 * record. Otherwise, the action is delayed until
2722 * after removal of the right path in which case we
2723 * can do a simple shift to remove the empty extent.
2724 */
2725 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2726 memset(&right_leaf_el->l_recs[0], 0,
2727 sizeof(struct ocfs2_extent_rec));
2728 }
2729 if (eb->h_next_leaf_blk == 0ULL) {
2730 /*
2731 * Move recs over to get rid of empty extent, decrease
2732 * next_free. This is allowed to remove the last
2733 * extent in our leaf (setting l_next_free_rec to
2734 * zero) - the delete code below won't care.
2735 */
2736 ocfs2_remove_empty_extent(right_leaf_el);
2737 }
2738
2739 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2740 if (ret)
2741 mlog_errno(ret);
2742 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2743 if (ret)
2744 mlog_errno(ret);
2745
2746 if (del_right_subtree) {
2747 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2748 subtree_index, dealloc);
Tao Ma3c5e1062009-07-21 15:42:05 +08002749 ret = ocfs2_update_edge_lengths(inode, handle, subtree_index,
2750 left_path);
2751 if (ret) {
2752 mlog_errno(ret);
2753 goto out;
2754 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002755
2756 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002757 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002758
2759 /*
2760 * Removal of the extent in the left leaf was skipped
2761 * above so we could delete the right path
2762 * 1st.
2763 */
2764 if (right_has_empty)
2765 ocfs2_remove_empty_extent(left_leaf_el);
2766
Tao Mae7d4cb62008-08-18 17:38:44 +08002767 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002768 if (ret)
2769 mlog_errno(ret);
2770
2771 *deleted = 1;
2772 } else
2773 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2774 subtree_index);
2775
2776out:
2777 return ret;
2778}
2779
2780/*
2781 * Given a full path, determine what cpos value would return us a path
2782 * containing the leaf immediately to the right of the current one.
2783 *
2784 * Will return zero if the path passed in is already the rightmost path.
2785 *
2786 * This looks similar, but is subtly different to
2787 * ocfs2_find_cpos_for_left_leaf().
2788 */
2789static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2790 struct ocfs2_path *path, u32 *cpos)
2791{
2792 int i, j, ret = 0;
2793 u64 blkno;
2794 struct ocfs2_extent_list *el;
2795
2796 *cpos = 0;
2797
2798 if (path->p_tree_depth == 0)
2799 return 0;
2800
2801 blkno = path_leaf_bh(path)->b_blocknr;
2802
2803 /* Start at the tree node just above the leaf and work our way up. */
2804 i = path->p_tree_depth - 1;
2805 while (i >= 0) {
2806 int next_free;
2807
2808 el = path->p_node[i].el;
2809
2810 /*
2811 * Find the extent record just after the one in our
2812 * path.
2813 */
2814 next_free = le16_to_cpu(el->l_next_free_rec);
2815 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2816 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2817 if (j == (next_free - 1)) {
2818 if (i == 0) {
2819 /*
2820 * We've determined that the
2821 * path specified is already
2822 * the rightmost one - return a
2823 * cpos of zero.
2824 */
2825 goto out;
2826 }
2827 /*
2828 * The rightmost record points to our
2829 * leaf - we need to travel up the
2830 * tree one level.
2831 */
2832 goto next_node;
2833 }
2834
2835 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2836 goto out;
2837 }
2838 }
2839
2840 /*
2841 * If we got here, we never found a valid node where
2842 * the tree indicated one should be.
2843 */
2844 ocfs2_error(sb,
2845 "Invalid extent tree at extent block %llu\n",
2846 (unsigned long long)blkno);
2847 ret = -EROFS;
2848 goto out;
2849
2850next_node:
2851 blkno = path->p_node[i].bh->b_blocknr;
2852 i--;
2853 }
2854
2855out:
2856 return ret;
2857}
2858
2859static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2860 handle_t *handle,
Joel Becker13723d02008-10-17 19:25:01 -07002861 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002862{
2863 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002864 struct buffer_head *bh = path_leaf_bh(path);
2865 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002866
2867 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2868 return 0;
2869
Joel Becker0cf2f762009-02-12 16:41:25 -08002870 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path,
Joel Becker13723d02008-10-17 19:25:01 -07002871 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002872 if (ret) {
2873 mlog_errno(ret);
2874 goto out;
2875 }
2876
2877 ocfs2_remove_empty_extent(el);
2878
2879 ret = ocfs2_journal_dirty(handle, bh);
2880 if (ret)
2881 mlog_errno(ret);
2882
2883out:
2884 return ret;
2885}
2886
2887static int __ocfs2_rotate_tree_left(struct inode *inode,
2888 handle_t *handle, int orig_credits,
2889 struct ocfs2_path *path,
2890 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002891 struct ocfs2_path **empty_extent_path,
2892 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002893{
2894 int ret, subtree_root, deleted;
2895 u32 right_cpos;
2896 struct ocfs2_path *left_path = NULL;
2897 struct ocfs2_path *right_path = NULL;
2898
2899 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2900
2901 *empty_extent_path = NULL;
2902
2903 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2904 &right_cpos);
2905 if (ret) {
2906 mlog_errno(ret);
2907 goto out;
2908 }
2909
Joel Beckerffdd7a52008-10-17 22:32:01 -07002910 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002911 if (!left_path) {
2912 ret = -ENOMEM;
2913 mlog_errno(ret);
2914 goto out;
2915 }
2916
2917 ocfs2_cp_path(left_path, path);
2918
Joel Beckerffdd7a52008-10-17 22:32:01 -07002919 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002920 if (!right_path) {
2921 ret = -ENOMEM;
2922 mlog_errno(ret);
2923 goto out;
2924 }
2925
2926 while (right_cpos) {
Joel Beckerfacdb772009-02-12 18:08:48 -08002927 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002928 if (ret) {
2929 mlog_errno(ret);
2930 goto out;
2931 }
2932
2933 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2934 right_path);
2935
2936 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2937 subtree_root,
2938 (unsigned long long)
2939 right_path->p_node[subtree_root].bh->b_blocknr,
2940 right_path->p_tree_depth);
2941
2942 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2943 orig_credits, left_path);
2944 if (ret) {
2945 mlog_errno(ret);
2946 goto out;
2947 }
2948
Mark Fashehe8aed342007-12-03 16:43:01 -08002949 /*
2950 * Caller might still want to make changes to the
2951 * tree root, so re-add it to the journal here.
2952 */
Joel Becker0cf2f762009-02-12 16:41:25 -08002953 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002954 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08002955 if (ret) {
2956 mlog_errno(ret);
2957 goto out;
2958 }
2959
Mark Fasheh328d5752007-06-18 10:48:04 -07002960 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2961 right_path, subtree_root,
Tao Mae7d4cb62008-08-18 17:38:44 +08002962 dealloc, &deleted, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002963 if (ret == -EAGAIN) {
2964 /*
2965 * The rotation has to temporarily stop due to
2966 * the right subtree having an empty
2967 * extent. Pass it back to the caller for a
2968 * fixup.
2969 */
2970 *empty_extent_path = right_path;
2971 right_path = NULL;
2972 goto out;
2973 }
2974 if (ret) {
2975 mlog_errno(ret);
2976 goto out;
2977 }
2978
2979 /*
2980 * The subtree rotate might have removed records on
2981 * the rightmost edge. If so, then rotation is
2982 * complete.
2983 */
2984 if (deleted)
2985 break;
2986
2987 ocfs2_mv_path(left_path, right_path);
2988
2989 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2990 &right_cpos);
2991 if (ret) {
2992 mlog_errno(ret);
2993 goto out;
2994 }
2995 }
2996
2997out:
2998 ocfs2_free_path(right_path);
2999 ocfs2_free_path(left_path);
3000
3001 return ret;
3002}
3003
3004static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003005 struct ocfs2_path *path,
3006 struct ocfs2_cached_dealloc_ctxt *dealloc,
3007 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003008{
3009 int ret, subtree_index;
3010 u32 cpos;
3011 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003012 struct ocfs2_extent_block *eb;
3013 struct ocfs2_extent_list *el;
3014
Mark Fasheh328d5752007-06-18 10:48:04 -07003015
Joel Becker35dc0aa2008-08-20 16:25:06 -07003016 ret = ocfs2_et_sanity_check(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003017 if (ret)
3018 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003019 /*
3020 * There's two ways we handle this depending on
3021 * whether path is the only existing one.
3022 */
3023 ret = ocfs2_extend_rotate_transaction(handle, 0,
3024 handle->h_buffer_credits,
3025 path);
3026 if (ret) {
3027 mlog_errno(ret);
3028 goto out;
3029 }
3030
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003031 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003032 if (ret) {
3033 mlog_errno(ret);
3034 goto out;
3035 }
3036
Joel Becker3d03a302009-02-12 17:49:26 -08003037 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3038 path, &cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003039 if (ret) {
3040 mlog_errno(ret);
3041 goto out;
3042 }
3043
3044 if (cpos) {
3045 /*
3046 * We have a path to the left of this one - it needs
3047 * an update too.
3048 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003049 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003050 if (!left_path) {
3051 ret = -ENOMEM;
3052 mlog_errno(ret);
3053 goto out;
3054 }
3055
Joel Beckerfacdb772009-02-12 18:08:48 -08003056 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003057 if (ret) {
3058 mlog_errno(ret);
3059 goto out;
3060 }
3061
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003062 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003063 if (ret) {
3064 mlog_errno(ret);
3065 goto out;
3066 }
3067
3068 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
3069
3070 ocfs2_unlink_subtree(inode, handle, left_path, path,
3071 subtree_index, dealloc);
Tao Ma3c5e1062009-07-21 15:42:05 +08003072 ret = ocfs2_update_edge_lengths(inode, handle, subtree_index,
3073 left_path);
3074 if (ret) {
3075 mlog_errno(ret);
3076 goto out;
3077 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003078
3079 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003080 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003081 } else {
3082 /*
3083 * 'path' is also the leftmost path which
3084 * means it must be the only one. This gets
3085 * handled differently because we want to
3086 * revert the inode back to having extents
3087 * in-line.
3088 */
3089 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
3090
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003091 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003092 el->l_tree_depth = 0;
3093 el->l_next_free_rec = 0;
3094 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3095
Joel Becker35dc0aa2008-08-20 16:25:06 -07003096 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003097 }
3098
3099 ocfs2_journal_dirty(handle, path_root_bh(path));
3100
3101out:
3102 ocfs2_free_path(left_path);
3103 return ret;
3104}
3105
3106/*
3107 * Left rotation of btree records.
3108 *
3109 * In many ways, this is (unsurprisingly) the opposite of right
3110 * rotation. We start at some non-rightmost path containing an empty
3111 * extent in the leaf block. The code works its way to the rightmost
3112 * path by rotating records to the left in every subtree.
3113 *
3114 * This is used by any code which reduces the number of extent records
3115 * in a leaf. After removal, an empty record should be placed in the
3116 * leftmost list position.
3117 *
3118 * This won't handle a length update of the rightmost path records if
3119 * the rightmost tree leaf record is removed so the caller is
3120 * responsible for detecting and correcting that.
3121 */
3122static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
3123 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003124 struct ocfs2_cached_dealloc_ctxt *dealloc,
3125 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003126{
3127 int ret, orig_credits = handle->h_buffer_credits;
3128 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3129 struct ocfs2_extent_block *eb;
3130 struct ocfs2_extent_list *el;
3131
3132 el = path_leaf_el(path);
3133 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3134 return 0;
3135
3136 if (path->p_tree_depth == 0) {
3137rightmost_no_delete:
3138 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003139 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003140 * it up front.
3141 */
3142 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
Joel Becker13723d02008-10-17 19:25:01 -07003143 path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003144 if (ret)
3145 mlog_errno(ret);
3146 goto out;
3147 }
3148
3149 /*
3150 * Handle rightmost branch now. There's several cases:
3151 * 1) simple rotation leaving records in there. That's trivial.
3152 * 2) rotation requiring a branch delete - there's no more
3153 * records left. Two cases of this:
3154 * a) There are branches to the left.
3155 * b) This is also the leftmost (the only) branch.
3156 *
3157 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3158 * 2a) we need the left branch so that we can update it with the unlink
3159 * 2b) we need to bring the inode back to inline extents.
3160 */
3161
3162 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3163 el = &eb->h_list;
3164 if (eb->h_next_leaf_blk == 0) {
3165 /*
3166 * This gets a bit tricky if we're going to delete the
3167 * rightmost path. Get the other cases out of the way
3168 * 1st.
3169 */
3170 if (le16_to_cpu(el->l_next_free_rec) > 1)
3171 goto rightmost_no_delete;
3172
3173 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3174 ret = -EIO;
3175 ocfs2_error(inode->i_sb,
3176 "Inode %llu has empty extent block at %llu",
3177 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3178 (unsigned long long)le64_to_cpu(eb->h_blkno));
3179 goto out;
3180 }
3181
3182 /*
3183 * XXX: The caller can not trust "path" any more after
3184 * this as it will have been deleted. What do we do?
3185 *
3186 * In theory the rotate-for-merge code will never get
3187 * here because it'll always ask for a rotate in a
3188 * nonempty list.
3189 */
3190
3191 ret = ocfs2_remove_rightmost_path(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003192 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003193 if (ret)
3194 mlog_errno(ret);
3195 goto out;
3196 }
3197
3198 /*
3199 * Now we can loop, remembering the path we get from -EAGAIN
3200 * and restarting from there.
3201 */
3202try_rotate:
3203 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003204 dealloc, &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003205 if (ret && ret != -EAGAIN) {
3206 mlog_errno(ret);
3207 goto out;
3208 }
3209
3210 while (ret == -EAGAIN) {
3211 tmp_path = restart_path;
3212 restart_path = NULL;
3213
3214 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
3215 tmp_path, dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003216 &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003217 if (ret && ret != -EAGAIN) {
3218 mlog_errno(ret);
3219 goto out;
3220 }
3221
3222 ocfs2_free_path(tmp_path);
3223 tmp_path = NULL;
3224
3225 if (ret == 0)
3226 goto try_rotate;
3227 }
3228
3229out:
3230 ocfs2_free_path(tmp_path);
3231 ocfs2_free_path(restart_path);
3232 return ret;
3233}
3234
3235static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3236 int index)
3237{
3238 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3239 unsigned int size;
3240
3241 if (rec->e_leaf_clusters == 0) {
3242 /*
3243 * We consumed all of the merged-from record. An empty
3244 * extent cannot exist anywhere but the 1st array
3245 * position, so move things over if the merged-from
3246 * record doesn't occupy that position.
3247 *
3248 * This creates a new empty extent so the caller
3249 * should be smart enough to have removed any existing
3250 * ones.
3251 */
3252 if (index > 0) {
3253 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3254 size = index * sizeof(struct ocfs2_extent_rec);
3255 memmove(&el->l_recs[1], &el->l_recs[0], size);
3256 }
3257
3258 /*
3259 * Always memset - the caller doesn't check whether it
3260 * created an empty extent, so there could be junk in
3261 * the other fields.
3262 */
3263 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3264 }
3265}
3266
Tao Ma677b9752008-01-30 14:21:05 +08003267static int ocfs2_get_right_path(struct inode *inode,
3268 struct ocfs2_path *left_path,
3269 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003270{
3271 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003272 u32 right_cpos;
3273 struct ocfs2_path *right_path = NULL;
3274 struct ocfs2_extent_list *left_el;
3275
3276 *ret_right_path = NULL;
3277
3278 /* This function shouldn't be called for non-trees. */
3279 BUG_ON(left_path->p_tree_depth == 0);
3280
3281 left_el = path_leaf_el(left_path);
3282 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3283
3284 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3285 &right_cpos);
3286 if (ret) {
3287 mlog_errno(ret);
3288 goto out;
3289 }
3290
3291 /* This function shouldn't be called for the rightmost leaf. */
3292 BUG_ON(right_cpos == 0);
3293
Joel Beckerffdd7a52008-10-17 22:32:01 -07003294 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003295 if (!right_path) {
3296 ret = -ENOMEM;
3297 mlog_errno(ret);
3298 goto out;
3299 }
3300
Joel Beckerfacdb772009-02-12 18:08:48 -08003301 ret = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003302 if (ret) {
3303 mlog_errno(ret);
3304 goto out;
3305 }
3306
3307 *ret_right_path = right_path;
3308out:
3309 if (ret)
3310 ocfs2_free_path(right_path);
3311 return ret;
3312}
3313
3314/*
3315 * Remove split_rec clusters from the record at index and merge them
3316 * onto the beginning of the record "next" to it.
3317 * For index < l_count - 1, the next means the extent rec at index + 1.
3318 * For index == l_count - 1, the "next" means the 1st extent rec of the
3319 * next extent block.
3320 */
3321static int ocfs2_merge_rec_right(struct inode *inode,
3322 struct ocfs2_path *left_path,
3323 handle_t *handle,
3324 struct ocfs2_extent_rec *split_rec,
3325 int index)
3326{
3327 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003328 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3329 struct ocfs2_extent_rec *left_rec;
3330 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003331 struct ocfs2_extent_list *right_el;
3332 struct ocfs2_path *right_path = NULL;
3333 int subtree_index = 0;
3334 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3335 struct buffer_head *bh = path_leaf_bh(left_path);
3336 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003337
3338 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003339 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003340
Al Viro9d8df6a2008-05-21 06:32:11 +01003341 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003342 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3343 /* we meet with a cross extent block merge. */
3344 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3345 if (ret) {
3346 mlog_errno(ret);
3347 goto out;
3348 }
3349
3350 right_el = path_leaf_el(right_path);
3351 next_free = le16_to_cpu(right_el->l_next_free_rec);
3352 BUG_ON(next_free <= 0);
3353 right_rec = &right_el->l_recs[0];
3354 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003355 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003356 right_rec = &right_el->l_recs[1];
3357 }
3358
3359 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3360 le16_to_cpu(left_rec->e_leaf_clusters) !=
3361 le32_to_cpu(right_rec->e_cpos));
3362
3363 subtree_index = ocfs2_find_subtree_root(inode,
3364 left_path, right_path);
3365
3366 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3367 handle->h_buffer_credits,
3368 right_path);
3369 if (ret) {
3370 mlog_errno(ret);
3371 goto out;
3372 }
3373
3374 root_bh = left_path->p_node[subtree_index].bh;
3375 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3376
Joel Becker0cf2f762009-02-12 16:41:25 -08003377 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003378 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003379 if (ret) {
3380 mlog_errno(ret);
3381 goto out;
3382 }
3383
3384 for (i = subtree_index + 1;
3385 i < path_num_items(right_path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -08003386 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07003387 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003388 if (ret) {
3389 mlog_errno(ret);
3390 goto out;
3391 }
3392
Joel Becker0cf2f762009-02-12 16:41:25 -08003393 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07003394 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003395 if (ret) {
3396 mlog_errno(ret);
3397 goto out;
3398 }
3399 }
3400
3401 } else {
3402 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3403 right_rec = &el->l_recs[index + 1];
3404 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003405
Joel Becker0cf2f762009-02-12 16:41:25 -08003406 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), left_path,
Joel Becker13723d02008-10-17 19:25:01 -07003407 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003408 if (ret) {
3409 mlog_errno(ret);
3410 goto out;
3411 }
3412
3413 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3414
3415 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3416 le64_add_cpu(&right_rec->e_blkno,
3417 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3418 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3419
3420 ocfs2_cleanup_merge(el, index);
3421
3422 ret = ocfs2_journal_dirty(handle, bh);
3423 if (ret)
3424 mlog_errno(ret);
3425
Tao Ma677b9752008-01-30 14:21:05 +08003426 if (right_path) {
3427 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3428 if (ret)
3429 mlog_errno(ret);
3430
3431 ocfs2_complete_edge_insert(inode, handle, left_path,
3432 right_path, subtree_index);
3433 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003434out:
Tao Ma677b9752008-01-30 14:21:05 +08003435 if (right_path)
3436 ocfs2_free_path(right_path);
3437 return ret;
3438}
3439
3440static int ocfs2_get_left_path(struct inode *inode,
3441 struct ocfs2_path *right_path,
3442 struct ocfs2_path **ret_left_path)
3443{
3444 int ret;
3445 u32 left_cpos;
3446 struct ocfs2_path *left_path = NULL;
3447
3448 *ret_left_path = NULL;
3449
3450 /* This function shouldn't be called for non-trees. */
3451 BUG_ON(right_path->p_tree_depth == 0);
3452
3453 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3454 right_path, &left_cpos);
3455 if (ret) {
3456 mlog_errno(ret);
3457 goto out;
3458 }
3459
3460 /* This function shouldn't be called for the leftmost leaf. */
3461 BUG_ON(left_cpos == 0);
3462
Joel Beckerffdd7a52008-10-17 22:32:01 -07003463 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003464 if (!left_path) {
3465 ret = -ENOMEM;
3466 mlog_errno(ret);
3467 goto out;
3468 }
3469
Joel Beckerfacdb772009-02-12 18:08:48 -08003470 ret = ocfs2_find_path(INODE_CACHE(inode), left_path, left_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003471 if (ret) {
3472 mlog_errno(ret);
3473 goto out;
3474 }
3475
3476 *ret_left_path = left_path;
3477out:
3478 if (ret)
3479 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003480 return ret;
3481}
3482
3483/*
3484 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003485 * onto the tail of the record "before" it.
3486 * For index > 0, the "before" means the extent rec at index - 1.
3487 *
3488 * For index == 0, the "before" means the last record of the previous
3489 * extent block. And there is also a situation that we may need to
3490 * remove the rightmost leaf extent block in the right_path and change
3491 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003492 */
Tao Ma677b9752008-01-30 14:21:05 +08003493static int ocfs2_merge_rec_left(struct inode *inode,
3494 struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003495 handle_t *handle,
3496 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003497 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003498 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003499 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003500{
Tao Ma677b9752008-01-30 14:21:05 +08003501 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003502 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3503 struct ocfs2_extent_rec *left_rec;
3504 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003505 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3506 struct buffer_head *bh = path_leaf_bh(right_path);
3507 struct buffer_head *root_bh = NULL;
3508 struct ocfs2_path *left_path = NULL;
3509 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003510
Tao Ma677b9752008-01-30 14:21:05 +08003511 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003512
Mark Fasheh328d5752007-06-18 10:48:04 -07003513 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003514 if (index == 0) {
3515 /* we meet with a cross extent block merge. */
3516 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3517 if (ret) {
3518 mlog_errno(ret);
3519 goto out;
3520 }
3521
3522 left_el = path_leaf_el(left_path);
3523 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3524 le16_to_cpu(left_el->l_count));
3525
3526 left_rec = &left_el->l_recs[
3527 le16_to_cpu(left_el->l_next_free_rec) - 1];
3528 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3529 le16_to_cpu(left_rec->e_leaf_clusters) !=
3530 le32_to_cpu(split_rec->e_cpos));
3531
3532 subtree_index = ocfs2_find_subtree_root(inode,
3533 left_path, right_path);
3534
3535 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3536 handle->h_buffer_credits,
3537 left_path);
3538 if (ret) {
3539 mlog_errno(ret);
3540 goto out;
3541 }
3542
3543 root_bh = left_path->p_node[subtree_index].bh;
3544 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3545
Joel Becker0cf2f762009-02-12 16:41:25 -08003546 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003547 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003548 if (ret) {
3549 mlog_errno(ret);
3550 goto out;
3551 }
3552
3553 for (i = subtree_index + 1;
3554 i < path_num_items(right_path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -08003555 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07003556 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003557 if (ret) {
3558 mlog_errno(ret);
3559 goto out;
3560 }
3561
Joel Becker0cf2f762009-02-12 16:41:25 -08003562 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07003563 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003564 if (ret) {
3565 mlog_errno(ret);
3566 goto out;
3567 }
3568 }
3569 } else {
3570 left_rec = &el->l_recs[index - 1];
3571 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3572 has_empty_extent = 1;
3573 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003574
Joel Becker0cf2f762009-02-12 16:41:25 -08003575 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
Tao Ma9047bea2009-01-05 14:45:24 +08003576 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003577 if (ret) {
3578 mlog_errno(ret);
3579 goto out;
3580 }
3581
3582 if (has_empty_extent && index == 1) {
3583 /*
3584 * The easy case - we can just plop the record right in.
3585 */
3586 *left_rec = *split_rec;
3587
3588 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003589 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003590 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003591
3592 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3593 le64_add_cpu(&right_rec->e_blkno,
3594 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3595 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3596
3597 ocfs2_cleanup_merge(el, index);
3598
3599 ret = ocfs2_journal_dirty(handle, bh);
3600 if (ret)
3601 mlog_errno(ret);
3602
Tao Ma677b9752008-01-30 14:21:05 +08003603 if (left_path) {
3604 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3605 if (ret)
3606 mlog_errno(ret);
3607
3608 /*
3609 * In the situation that the right_rec is empty and the extent
3610 * block is empty also, ocfs2_complete_edge_insert can't handle
3611 * it and we need to delete the right extent block.
3612 */
3613 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3614 le16_to_cpu(el->l_next_free_rec) == 1) {
3615
3616 ret = ocfs2_remove_rightmost_path(inode, handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003617 right_path,
3618 dealloc, et);
Tao Ma677b9752008-01-30 14:21:05 +08003619 if (ret) {
3620 mlog_errno(ret);
3621 goto out;
3622 }
3623
3624 /* Now the rightmost extent block has been deleted.
3625 * So we use the new rightmost path.
3626 */
3627 ocfs2_mv_path(right_path, left_path);
3628 left_path = NULL;
3629 } else
3630 ocfs2_complete_edge_insert(inode, handle, left_path,
3631 right_path, subtree_index);
3632 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003633out:
Tao Ma677b9752008-01-30 14:21:05 +08003634 if (left_path)
3635 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003636 return ret;
3637}
3638
3639static int ocfs2_try_to_merge_extent(struct inode *inode,
3640 handle_t *handle,
Tao Ma677b9752008-01-30 14:21:05 +08003641 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003642 int split_index,
3643 struct ocfs2_extent_rec *split_rec,
3644 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003645 struct ocfs2_merge_ctxt *ctxt,
3646 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003647
3648{
Tao Mao518d7262007-08-28 17:25:35 -07003649 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003650 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003651 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3652
3653 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3654
Tao Mao518d7262007-08-28 17:25:35 -07003655 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3656 /*
3657 * The merge code will need to create an empty
3658 * extent to take the place of the newly
3659 * emptied slot. Remove any pre-existing empty
3660 * extents - having more than one in a leaf is
3661 * illegal.
3662 */
Tao Ma677b9752008-01-30 14:21:05 +08003663 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003664 dealloc, et);
Tao Mao518d7262007-08-28 17:25:35 -07003665 if (ret) {
3666 mlog_errno(ret);
3667 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003668 }
Tao Mao518d7262007-08-28 17:25:35 -07003669 split_index--;
3670 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003671 }
3672
3673 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3674 /*
3675 * Left-right contig implies this.
3676 */
3677 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003678
3679 /*
3680 * Since the leftright insert always covers the entire
3681 * extent, this call will delete the insert record
3682 * entirely, resulting in an empty extent record added to
3683 * the extent block.
3684 *
3685 * Since the adding of an empty extent shifts
3686 * everything back to the right, there's no need to
3687 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003688 *
3689 * When the split_index is zero, we need to merge it to the
3690 * prevoius extent block. It is more efficient and easier
3691 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003692 */
Tao Ma677b9752008-01-30 14:21:05 +08003693 ret = ocfs2_merge_rec_right(inode, path,
3694 handle, split_rec,
3695 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003696 if (ret) {
3697 mlog_errno(ret);
3698 goto out;
3699 }
3700
3701 /*
3702 * We can only get this from logic error above.
3703 */
3704 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3705
Tao Ma677b9752008-01-30 14:21:05 +08003706 /* The merge left us with an empty extent, remove it. */
Tao Mae7d4cb62008-08-18 17:38:44 +08003707 ret = ocfs2_rotate_tree_left(inode, handle, path,
3708 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003709 if (ret) {
3710 mlog_errno(ret);
3711 goto out;
3712 }
Tao Ma677b9752008-01-30 14:21:05 +08003713
Mark Fasheh328d5752007-06-18 10:48:04 -07003714 rec = &el->l_recs[split_index];
3715
3716 /*
3717 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003718 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003719 */
Tao Ma677b9752008-01-30 14:21:05 +08003720 ret = ocfs2_merge_rec_left(inode, path,
3721 handle, rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003722 dealloc, et,
Tao Ma677b9752008-01-30 14:21:05 +08003723 split_index);
3724
Mark Fasheh328d5752007-06-18 10:48:04 -07003725 if (ret) {
3726 mlog_errno(ret);
3727 goto out;
3728 }
3729
Tao Ma677b9752008-01-30 14:21:05 +08003730 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003731 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003732 /*
3733 * Error from this last rotate is not critical, so
3734 * print but don't bubble it up.
3735 */
3736 if (ret)
3737 mlog_errno(ret);
3738 ret = 0;
3739 } else {
3740 /*
3741 * Merge a record to the left or right.
3742 *
3743 * 'contig_type' is relative to the existing record,
3744 * so for example, if we're "right contig", it's to
3745 * the record on the left (hence the left merge).
3746 */
3747 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3748 ret = ocfs2_merge_rec_left(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003749 path,
3750 handle, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003751 dealloc, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003752 split_index);
3753 if (ret) {
3754 mlog_errno(ret);
3755 goto out;
3756 }
3757 } else {
3758 ret = ocfs2_merge_rec_right(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003759 path,
3760 handle, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003761 split_index);
3762 if (ret) {
3763 mlog_errno(ret);
3764 goto out;
3765 }
3766 }
3767
3768 if (ctxt->c_split_covers_rec) {
3769 /*
3770 * The merge may have left an empty extent in
3771 * our leaf. Try to rotate it away.
3772 */
Tao Ma677b9752008-01-30 14:21:05 +08003773 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003774 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003775 if (ret)
3776 mlog_errno(ret);
3777 ret = 0;
3778 }
3779 }
3780
3781out:
3782 return ret;
3783}
3784
3785static void ocfs2_subtract_from_rec(struct super_block *sb,
3786 enum ocfs2_split_type split,
3787 struct ocfs2_extent_rec *rec,
3788 struct ocfs2_extent_rec *split_rec)
3789{
3790 u64 len_blocks;
3791
3792 len_blocks = ocfs2_clusters_to_blocks(sb,
3793 le16_to_cpu(split_rec->e_leaf_clusters));
3794
3795 if (split == SPLIT_LEFT) {
3796 /*
3797 * Region is on the left edge of the existing
3798 * record.
3799 */
3800 le32_add_cpu(&rec->e_cpos,
3801 le16_to_cpu(split_rec->e_leaf_clusters));
3802 le64_add_cpu(&rec->e_blkno, len_blocks);
3803 le16_add_cpu(&rec->e_leaf_clusters,
3804 -le16_to_cpu(split_rec->e_leaf_clusters));
3805 } else {
3806 /*
3807 * Region is on the right edge of the existing
3808 * record.
3809 */
3810 le16_add_cpu(&rec->e_leaf_clusters,
3811 -le16_to_cpu(split_rec->e_leaf_clusters));
3812 }
3813}
3814
Mark Fashehdcd05382007-01-16 11:32:23 -08003815/*
3816 * Do the final bits of extent record insertion at the target leaf
3817 * list. If this leaf is part of an allocation tree, it is assumed
3818 * that the tree above has been prepared.
3819 */
3820static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3821 struct ocfs2_extent_list *el,
3822 struct ocfs2_insert_type *insert,
3823 struct inode *inode)
3824{
3825 int i = insert->ins_contig_index;
3826 unsigned int range;
3827 struct ocfs2_extent_rec *rec;
3828
Mark Fashehe48edee2007-03-07 16:46:57 -08003829 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003830
Mark Fasheh328d5752007-06-18 10:48:04 -07003831 if (insert->ins_split != SPLIT_NONE) {
3832 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3833 BUG_ON(i == -1);
3834 rec = &el->l_recs[i];
3835 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3836 insert_rec);
3837 goto rotate;
3838 }
3839
Mark Fashehdcd05382007-01-16 11:32:23 -08003840 /*
3841 * Contiguous insert - either left or right.
3842 */
3843 if (insert->ins_contig != CONTIG_NONE) {
3844 rec = &el->l_recs[i];
3845 if (insert->ins_contig == CONTIG_LEFT) {
3846 rec->e_blkno = insert_rec->e_blkno;
3847 rec->e_cpos = insert_rec->e_cpos;
3848 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003849 le16_add_cpu(&rec->e_leaf_clusters,
3850 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003851 return;
3852 }
3853
3854 /*
3855 * Handle insert into an empty leaf.
3856 */
3857 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3858 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3859 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3860 el->l_recs[0] = *insert_rec;
3861 el->l_next_free_rec = cpu_to_le16(1);
3862 return;
3863 }
3864
3865 /*
3866 * Appending insert.
3867 */
3868 if (insert->ins_appending == APPEND_TAIL) {
3869 i = le16_to_cpu(el->l_next_free_rec) - 1;
3870 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003871 range = le32_to_cpu(rec->e_cpos)
3872 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003873 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3874
3875 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3876 le16_to_cpu(el->l_count),
3877 "inode %lu, depth %u, count %u, next free %u, "
3878 "rec.cpos %u, rec.clusters %u, "
3879 "insert.cpos %u, insert.clusters %u\n",
3880 inode->i_ino,
3881 le16_to_cpu(el->l_tree_depth),
3882 le16_to_cpu(el->l_count),
3883 le16_to_cpu(el->l_next_free_rec),
3884 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003885 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003886 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003887 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003888 i++;
3889 el->l_recs[i] = *insert_rec;
3890 le16_add_cpu(&el->l_next_free_rec, 1);
3891 return;
3892 }
3893
Mark Fasheh328d5752007-06-18 10:48:04 -07003894rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003895 /*
3896 * Ok, we have to rotate.
3897 *
3898 * At this point, it is safe to assume that inserting into an
3899 * empty leaf and appending to a leaf have both been handled
3900 * above.
3901 *
3902 * This leaf needs to have space, either by the empty 1st
3903 * extent record, or by virtue of an l_next_rec < l_count.
3904 */
3905 ocfs2_rotate_leaf(el, insert_rec);
3906}
3907
Mark Fasheh328d5752007-06-18 10:48:04 -07003908static void ocfs2_adjust_rightmost_records(struct inode *inode,
3909 handle_t *handle,
3910 struct ocfs2_path *path,
3911 struct ocfs2_extent_rec *insert_rec)
3912{
3913 int ret, i, next_free;
3914 struct buffer_head *bh;
3915 struct ocfs2_extent_list *el;
3916 struct ocfs2_extent_rec *rec;
3917
3918 /*
3919 * Update everything except the leaf block.
3920 */
3921 for (i = 0; i < path->p_tree_depth; i++) {
3922 bh = path->p_node[i].bh;
3923 el = path->p_node[i].el;
3924
3925 next_free = le16_to_cpu(el->l_next_free_rec);
3926 if (next_free == 0) {
3927 ocfs2_error(inode->i_sb,
3928 "Dinode %llu has a bad extent list",
3929 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3930 ret = -EIO;
3931 return;
3932 }
3933
3934 rec = &el->l_recs[next_free - 1];
3935
3936 rec->e_int_clusters = insert_rec->e_cpos;
3937 le32_add_cpu(&rec->e_int_clusters,
3938 le16_to_cpu(insert_rec->e_leaf_clusters));
3939 le32_add_cpu(&rec->e_int_clusters,
3940 -le32_to_cpu(rec->e_cpos));
3941
3942 ret = ocfs2_journal_dirty(handle, bh);
3943 if (ret)
3944 mlog_errno(ret);
3945
3946 }
3947}
3948
Mark Fashehdcd05382007-01-16 11:32:23 -08003949static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3950 struct ocfs2_extent_rec *insert_rec,
3951 struct ocfs2_path *right_path,
3952 struct ocfs2_path **ret_left_path)
3953{
Mark Fasheh328d5752007-06-18 10:48:04 -07003954 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003955 struct ocfs2_extent_list *el;
3956 struct ocfs2_path *left_path = NULL;
3957
3958 *ret_left_path = NULL;
3959
3960 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003961 * This shouldn't happen for non-trees. The extent rec cluster
3962 * count manipulation below only works for interior nodes.
3963 */
3964 BUG_ON(right_path->p_tree_depth == 0);
3965
3966 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003967 * If our appending insert is at the leftmost edge of a leaf,
3968 * then we might need to update the rightmost records of the
3969 * neighboring path.
3970 */
3971 el = path_leaf_el(right_path);
3972 next_free = le16_to_cpu(el->l_next_free_rec);
3973 if (next_free == 0 ||
3974 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3975 u32 left_cpos;
3976
3977 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3978 &left_cpos);
3979 if (ret) {
3980 mlog_errno(ret);
3981 goto out;
3982 }
3983
3984 mlog(0, "Append may need a left path update. cpos: %u, "
3985 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3986 left_cpos);
3987
3988 /*
3989 * No need to worry if the append is already in the
3990 * leftmost leaf.
3991 */
3992 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07003993 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003994 if (!left_path) {
3995 ret = -ENOMEM;
3996 mlog_errno(ret);
3997 goto out;
3998 }
3999
Joel Beckerfacdb772009-02-12 18:08:48 -08004000 ret = ocfs2_find_path(INODE_CACHE(inode), left_path,
4001 left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004002 if (ret) {
4003 mlog_errno(ret);
4004 goto out;
4005 }
4006
4007 /*
4008 * ocfs2_insert_path() will pass the left_path to the
4009 * journal for us.
4010 */
4011 }
4012 }
4013
Joel Becker0cf2f762009-02-12 16:41:25 -08004014 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004015 if (ret) {
4016 mlog_errno(ret);
4017 goto out;
4018 }
4019
Mark Fasheh328d5752007-06-18 10:48:04 -07004020 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004021
4022 *ret_left_path = left_path;
4023 ret = 0;
4024out:
4025 if (ret != 0)
4026 ocfs2_free_path(left_path);
4027
4028 return ret;
4029}
4030
Mark Fasheh328d5752007-06-18 10:48:04 -07004031static void ocfs2_split_record(struct inode *inode,
4032 struct ocfs2_path *left_path,
4033 struct ocfs2_path *right_path,
4034 struct ocfs2_extent_rec *split_rec,
4035 enum ocfs2_split_type split)
4036{
4037 int index;
4038 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4039 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4040 struct ocfs2_extent_rec *rec, *tmprec;
4041
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004042 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004043 if (left_path)
4044 left_el = path_leaf_el(left_path);
4045
4046 el = right_el;
4047 insert_el = right_el;
4048 index = ocfs2_search_extent_list(el, cpos);
4049 if (index != -1) {
4050 if (index == 0 && left_path) {
4051 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4052
4053 /*
4054 * This typically means that the record
4055 * started in the left path but moved to the
4056 * right as a result of rotation. We either
4057 * move the existing record to the left, or we
4058 * do the later insert there.
4059 *
4060 * In this case, the left path should always
4061 * exist as the rotate code will have passed
4062 * it back for a post-insert update.
4063 */
4064
4065 if (split == SPLIT_LEFT) {
4066 /*
4067 * It's a left split. Since we know
4068 * that the rotate code gave us an
4069 * empty extent in the left path, we
4070 * can just do the insert there.
4071 */
4072 insert_el = left_el;
4073 } else {
4074 /*
4075 * Right split - we have to move the
4076 * existing record over to the left
4077 * leaf. The insert will be into the
4078 * newly created empty extent in the
4079 * right leaf.
4080 */
4081 tmprec = &right_el->l_recs[index];
4082 ocfs2_rotate_leaf(left_el, tmprec);
4083 el = left_el;
4084
4085 memset(tmprec, 0, sizeof(*tmprec));
4086 index = ocfs2_search_extent_list(left_el, cpos);
4087 BUG_ON(index == -1);
4088 }
4089 }
4090 } else {
4091 BUG_ON(!left_path);
4092 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4093 /*
4094 * Left path is easy - we can just allow the insert to
4095 * happen.
4096 */
4097 el = left_el;
4098 insert_el = left_el;
4099 index = ocfs2_search_extent_list(el, cpos);
4100 BUG_ON(index == -1);
4101 }
4102
4103 rec = &el->l_recs[index];
4104 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
4105 ocfs2_rotate_leaf(insert_el, split_rec);
4106}
4107
Mark Fashehdcd05382007-01-16 11:32:23 -08004108/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004109 * This function only does inserts on an allocation b-tree. For tree
4110 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004111 *
4112 * right_path is the path we want to do the actual insert
4113 * in. left_path should only be passed in if we need to update that
4114 * portion of the tree after an edge insert.
4115 */
4116static int ocfs2_insert_path(struct inode *inode,
4117 handle_t *handle,
4118 struct ocfs2_path *left_path,
4119 struct ocfs2_path *right_path,
4120 struct ocfs2_extent_rec *insert_rec,
4121 struct ocfs2_insert_type *insert)
4122{
4123 int ret, subtree_index;
4124 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004125
Mark Fashehdcd05382007-01-16 11:32:23 -08004126 if (left_path) {
4127 int credits = handle->h_buffer_credits;
4128
4129 /*
4130 * There's a chance that left_path got passed back to
4131 * us without being accounted for in the
4132 * journal. Extend our transaction here to be sure we
4133 * can change those blocks.
4134 */
4135 credits += left_path->p_tree_depth;
4136
4137 ret = ocfs2_extend_trans(handle, credits);
4138 if (ret < 0) {
4139 mlog_errno(ret);
4140 goto out;
4141 }
4142
Joel Becker0cf2f762009-02-12 16:41:25 -08004143 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, left_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004144 if (ret < 0) {
4145 mlog_errno(ret);
4146 goto out;
4147 }
4148 }
4149
Mark Fashehe8aed342007-12-03 16:43:01 -08004150 /*
4151 * Pass both paths to the journal. The majority of inserts
4152 * will be touching all components anyway.
4153 */
Joel Becker0cf2f762009-02-12 16:41:25 -08004154 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, right_path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004155 if (ret < 0) {
4156 mlog_errno(ret);
4157 goto out;
4158 }
4159
Mark Fasheh328d5752007-06-18 10:48:04 -07004160 if (insert->ins_split != SPLIT_NONE) {
4161 /*
4162 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004163 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004164 * function sort it all out.
4165 */
4166 ocfs2_split_record(inode, left_path, right_path,
4167 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004168
4169 /*
4170 * Split might have modified either leaf and we don't
4171 * have a guarantee that the later edge insert will
4172 * dirty this for us.
4173 */
4174 if (left_path)
4175 ret = ocfs2_journal_dirty(handle,
4176 path_leaf_bh(left_path));
4177 if (ret)
4178 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07004179 } else
4180 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
4181 insert, inode);
Mark Fashehdcd05382007-01-16 11:32:23 -08004182
Mark Fashehdcd05382007-01-16 11:32:23 -08004183 ret = ocfs2_journal_dirty(handle, leaf_bh);
4184 if (ret)
4185 mlog_errno(ret);
4186
4187 if (left_path) {
4188 /*
4189 * The rotate code has indicated that we need to fix
4190 * up portions of the tree after the insert.
4191 *
4192 * XXX: Should we extend the transaction here?
4193 */
4194 subtree_index = ocfs2_find_subtree_root(inode, left_path,
4195 right_path);
4196 ocfs2_complete_edge_insert(inode, handle, left_path,
4197 right_path, subtree_index);
4198 }
4199
4200 ret = 0;
4201out:
4202 return ret;
4203}
4204
4205static int ocfs2_do_insert_extent(struct inode *inode,
4206 handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004207 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004208 struct ocfs2_extent_rec *insert_rec,
4209 struct ocfs2_insert_type *type)
4210{
4211 int ret, rotate = 0;
4212 u32 cpos;
4213 struct ocfs2_path *right_path = NULL;
4214 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004215 struct ocfs2_extent_list *el;
4216
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004217 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004218
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004219 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004220 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004221 if (ret) {
4222 mlog_errno(ret);
4223 goto out;
4224 }
4225
4226 if (le16_to_cpu(el->l_tree_depth) == 0) {
4227 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4228 goto out_update_clusters;
4229 }
4230
Joel Beckerffdd7a52008-10-17 22:32:01 -07004231 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004232 if (!right_path) {
4233 ret = -ENOMEM;
4234 mlog_errno(ret);
4235 goto out;
4236 }
4237
4238 /*
4239 * Determine the path to start with. Rotations need the
4240 * rightmost path, everything else can go directly to the
4241 * target leaf.
4242 */
4243 cpos = le32_to_cpu(insert_rec->e_cpos);
4244 if (type->ins_appending == APPEND_NONE &&
4245 type->ins_contig == CONTIG_NONE) {
4246 rotate = 1;
4247 cpos = UINT_MAX;
4248 }
4249
Joel Beckerfacdb772009-02-12 18:08:48 -08004250 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004251 if (ret) {
4252 mlog_errno(ret);
4253 goto out;
4254 }
4255
4256 /*
4257 * Rotations and appends need special treatment - they modify
4258 * parts of the tree's above them.
4259 *
4260 * Both might pass back a path immediate to the left of the
4261 * one being inserted to. This will be cause
4262 * ocfs2_insert_path() to modify the rightmost records of
4263 * left_path to account for an edge insert.
4264 *
4265 * XXX: When modifying this code, keep in mind that an insert
4266 * can wind up skipping both of these two special cases...
4267 */
4268 if (rotate) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004269 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004270 le32_to_cpu(insert_rec->e_cpos),
4271 right_path, &left_path);
4272 if (ret) {
4273 mlog_errno(ret);
4274 goto out;
4275 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004276
4277 /*
4278 * ocfs2_rotate_tree_right() might have extended the
4279 * transaction without re-journaling our tree root.
4280 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004281 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004282 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004283 if (ret) {
4284 mlog_errno(ret);
4285 goto out;
4286 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004287 } else if (type->ins_appending == APPEND_TAIL
4288 && type->ins_contig != CONTIG_LEFT) {
4289 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4290 right_path, &left_path);
4291 if (ret) {
4292 mlog_errno(ret);
4293 goto out;
4294 }
4295 }
4296
4297 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4298 insert_rec, type);
4299 if (ret) {
4300 mlog_errno(ret);
4301 goto out;
4302 }
4303
4304out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004305 if (type->ins_split == SPLIT_NONE)
Joel Becker35dc0aa2008-08-20 16:25:06 -07004306 ocfs2_et_update_clusters(inode, et,
4307 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004308
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004309 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004310 if (ret)
4311 mlog_errno(ret);
4312
4313out:
4314 ocfs2_free_path(left_path);
4315 ocfs2_free_path(right_path);
4316
4317 return ret;
4318}
4319
Mark Fasheh328d5752007-06-18 10:48:04 -07004320static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08004321ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004322 struct ocfs2_extent_list *el, int index,
4323 struct ocfs2_extent_rec *split_rec)
4324{
Tao Maad5a4d72008-01-30 14:21:32 +08004325 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004326 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004327 u32 left_cpos, right_cpos;
4328 struct ocfs2_extent_rec *rec = NULL;
4329 struct ocfs2_extent_list *new_el;
4330 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4331 struct buffer_head *bh;
4332 struct ocfs2_extent_block *eb;
4333
4334 if (index > 0) {
4335 rec = &el->l_recs[index - 1];
4336 } else if (path->p_tree_depth > 0) {
4337 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4338 path, &left_cpos);
4339 if (status)
4340 goto out;
4341
4342 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004343 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004344 if (!left_path)
4345 goto out;
4346
Joel Beckerfacdb772009-02-12 18:08:48 -08004347 status = ocfs2_find_path(INODE_CACHE(inode),
4348 left_path, left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004349 if (status)
4350 goto out;
4351
4352 new_el = path_leaf_el(left_path);
4353
4354 if (le16_to_cpu(new_el->l_next_free_rec) !=
4355 le16_to_cpu(new_el->l_count)) {
4356 bh = path_leaf_bh(left_path);
4357 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004358 ocfs2_error(inode->i_sb,
4359 "Extent block #%llu has an "
4360 "invalid l_next_free_rec of "
4361 "%d. It should have "
4362 "matched the l_count of %d",
4363 (unsigned long long)le64_to_cpu(eb->h_blkno),
4364 le16_to_cpu(new_el->l_next_free_rec),
4365 le16_to_cpu(new_el->l_count));
4366 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004367 goto out;
4368 }
4369 rec = &new_el->l_recs[
4370 le16_to_cpu(new_el->l_next_free_rec) - 1];
4371 }
4372 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004373
4374 /*
4375 * We're careful to check for an empty extent record here -
4376 * the merge code will know what to do if it sees one.
4377 */
Tao Maad5a4d72008-01-30 14:21:32 +08004378 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004379 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4380 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4381 ret = CONTIG_RIGHT;
4382 } else {
4383 ret = ocfs2_extent_contig(inode, rec, split_rec);
4384 }
4385 }
4386
Tao Maad5a4d72008-01-30 14:21:32 +08004387 rec = NULL;
4388 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4389 rec = &el->l_recs[index + 1];
4390 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4391 path->p_tree_depth > 0) {
4392 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4393 path, &right_cpos);
4394 if (status)
4395 goto out;
4396
4397 if (right_cpos == 0)
4398 goto out;
4399
Joel Beckerffdd7a52008-10-17 22:32:01 -07004400 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004401 if (!right_path)
4402 goto out;
4403
Joel Beckerfacdb772009-02-12 18:08:48 -08004404 status = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004405 if (status)
4406 goto out;
4407
4408 new_el = path_leaf_el(right_path);
4409 rec = &new_el->l_recs[0];
4410 if (ocfs2_is_empty_extent(rec)) {
4411 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4412 bh = path_leaf_bh(right_path);
4413 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004414 ocfs2_error(inode->i_sb,
4415 "Extent block #%llu has an "
4416 "invalid l_next_free_rec of %d",
4417 (unsigned long long)le64_to_cpu(eb->h_blkno),
4418 le16_to_cpu(new_el->l_next_free_rec));
4419 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004420 goto out;
4421 }
4422 rec = &new_el->l_recs[1];
4423 }
4424 }
4425
4426 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004427 enum ocfs2_contig_type contig_type;
4428
Mark Fasheh328d5752007-06-18 10:48:04 -07004429 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4430
4431 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4432 ret = CONTIG_LEFTRIGHT;
4433 else if (ret == CONTIG_NONE)
4434 ret = contig_type;
4435 }
4436
Tao Maad5a4d72008-01-30 14:21:32 +08004437out:
4438 if (left_path)
4439 ocfs2_free_path(left_path);
4440 if (right_path)
4441 ocfs2_free_path(right_path);
4442
Mark Fasheh328d5752007-06-18 10:48:04 -07004443 return ret;
4444}
4445
Mark Fashehdcd05382007-01-16 11:32:23 -08004446static void ocfs2_figure_contig_type(struct inode *inode,
4447 struct ocfs2_insert_type *insert,
4448 struct ocfs2_extent_list *el,
Tao Maca12b7c2008-08-18 17:38:52 +08004449 struct ocfs2_extent_rec *insert_rec,
4450 struct ocfs2_extent_tree *et)
Mark Fashehdcd05382007-01-16 11:32:23 -08004451{
4452 int i;
4453 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4454
Mark Fashehe48edee2007-03-07 16:46:57 -08004455 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4456
Mark Fashehdcd05382007-01-16 11:32:23 -08004457 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4458 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4459 insert_rec);
4460 if (contig_type != CONTIG_NONE) {
4461 insert->ins_contig_index = i;
4462 break;
4463 }
4464 }
4465 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004466
4467 if (insert->ins_contig != CONTIG_NONE) {
4468 struct ocfs2_extent_rec *rec =
4469 &el->l_recs[insert->ins_contig_index];
4470 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4471 le16_to_cpu(insert_rec->e_leaf_clusters);
4472
4473 /*
4474 * Caller might want us to limit the size of extents, don't
4475 * calculate contiguousness if we might exceed that limit.
4476 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004477 if (et->et_max_leaf_clusters &&
4478 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004479 insert->ins_contig = CONTIG_NONE;
4480 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004481}
4482
4483/*
4484 * This should only be called against the righmost leaf extent list.
4485 *
4486 * ocfs2_figure_appending_type() will figure out whether we'll have to
4487 * insert at the tail of the rightmost leaf.
4488 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004489 * This should also work against the root extent list for tree's with 0
4490 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004491 * then the logic here makes sense.
4492 */
4493static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4494 struct ocfs2_extent_list *el,
4495 struct ocfs2_extent_rec *insert_rec)
4496{
4497 int i;
4498 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4499 struct ocfs2_extent_rec *rec;
4500
4501 insert->ins_appending = APPEND_NONE;
4502
Mark Fashehe48edee2007-03-07 16:46:57 -08004503 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004504
4505 if (!el->l_next_free_rec)
4506 goto set_tail_append;
4507
4508 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4509 /* Were all records empty? */
4510 if (le16_to_cpu(el->l_next_free_rec) == 1)
4511 goto set_tail_append;
4512 }
4513
4514 i = le16_to_cpu(el->l_next_free_rec) - 1;
4515 rec = &el->l_recs[i];
4516
Mark Fashehe48edee2007-03-07 16:46:57 -08004517 if (cpos >=
4518 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004519 goto set_tail_append;
4520
4521 return;
4522
4523set_tail_append:
4524 insert->ins_appending = APPEND_TAIL;
4525}
4526
4527/*
4528 * Helper function called at the begining of an insert.
4529 *
4530 * This computes a few things that are commonly used in the process of
4531 * inserting into the btree:
4532 * - Whether the new extent is contiguous with an existing one.
4533 * - The current tree depth.
4534 * - Whether the insert is an appending one.
4535 * - The total # of free records in the tree.
4536 *
4537 * All of the information is stored on the ocfs2_insert_type
4538 * structure.
4539 */
4540static int ocfs2_figure_insert_type(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004541 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004542 struct buffer_head **last_eb_bh,
4543 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004544 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004545 struct ocfs2_insert_type *insert)
4546{
4547 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004548 struct ocfs2_extent_block *eb;
4549 struct ocfs2_extent_list *el;
4550 struct ocfs2_path *path = NULL;
4551 struct buffer_head *bh = NULL;
4552
Mark Fasheh328d5752007-06-18 10:48:04 -07004553 insert->ins_split = SPLIT_NONE;
4554
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004555 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004556 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4557
4558 if (el->l_tree_depth) {
4559 /*
4560 * If we have tree depth, we read in the
4561 * rightmost extent block ahead of time as
4562 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4563 * may want it later.
4564 */
Joel Becker3d03a302009-02-12 17:49:26 -08004565 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08004566 ocfs2_et_get_last_eb_blk(et),
4567 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004568 if (ret) {
4569 mlog_exit(ret);
4570 goto out;
4571 }
4572 eb = (struct ocfs2_extent_block *) bh->b_data;
4573 el = &eb->h_list;
4574 }
4575
4576 /*
4577 * Unless we have a contiguous insert, we'll need to know if
4578 * there is room left in our allocation tree for another
4579 * extent record.
4580 *
4581 * XXX: This test is simplistic, we can search for empty
4582 * extent records too.
4583 */
Tao Maoc77534f2007-08-28 17:22:33 -07004584 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004585 le16_to_cpu(el->l_next_free_rec);
4586
4587 if (!insert->ins_tree_depth) {
Tao Maca12b7c2008-08-18 17:38:52 +08004588 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004589 ocfs2_figure_appending_type(insert, el, insert_rec);
4590 return 0;
4591 }
4592
Joel Beckerffdd7a52008-10-17 22:32:01 -07004593 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004594 if (!path) {
4595 ret = -ENOMEM;
4596 mlog_errno(ret);
4597 goto out;
4598 }
4599
4600 /*
4601 * In the case that we're inserting past what the tree
4602 * currently accounts for, ocfs2_find_path() will return for
4603 * us the rightmost tree path. This is accounted for below in
4604 * the appending code.
4605 */
Joel Beckerfacdb772009-02-12 18:08:48 -08004606 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -08004607 if (ret) {
4608 mlog_errno(ret);
4609 goto out;
4610 }
4611
4612 el = path_leaf_el(path);
4613
4614 /*
4615 * Now that we have the path, there's two things we want to determine:
4616 * 1) Contiguousness (also set contig_index if this is so)
4617 *
4618 * 2) Are we doing an append? We can trivially break this up
4619 * into two types of appends: simple record append, or a
4620 * rotate inside the tail leaf.
4621 */
Tao Maca12b7c2008-08-18 17:38:52 +08004622 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004623
4624 /*
4625 * The insert code isn't quite ready to deal with all cases of
4626 * left contiguousness. Specifically, if it's an insert into
4627 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004628 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004629 * left. For now, just catch that case and fool the layers
4630 * above us. This works just fine for tree_depth == 0, which
4631 * is why we allow that above.
4632 */
4633 if (insert->ins_contig == CONTIG_LEFT &&
4634 insert->ins_contig_index == 0)
4635 insert->ins_contig = CONTIG_NONE;
4636
4637 /*
4638 * Ok, so we can simply compare against last_eb to figure out
4639 * whether the path doesn't exist. This will only happen in
4640 * the case that we're doing a tail append, so maybe we can
4641 * take advantage of that information somehow.
4642 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004643 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004644 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004645 /*
4646 * Ok, ocfs2_find_path() returned us the rightmost
4647 * tree path. This might be an appending insert. There are
4648 * two cases:
4649 * 1) We're doing a true append at the tail:
4650 * -This might even be off the end of the leaf
4651 * 2) We're "appending" by rotating in the tail
4652 */
4653 ocfs2_figure_appending_type(insert, el, insert_rec);
4654 }
4655
4656out:
4657 ocfs2_free_path(path);
4658
4659 if (ret == 0)
4660 *last_eb_bh = bh;
4661 else
4662 brelse(bh);
4663 return ret;
4664}
4665
4666/*
4667 * Insert an extent into an inode btree.
4668 *
4669 * The caller needs to update fe->i_clusters
4670 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004671int ocfs2_insert_extent(struct ocfs2_super *osb,
4672 handle_t *handle,
4673 struct inode *inode,
4674 struct ocfs2_extent_tree *et,
4675 u32 cpos,
4676 u64 start_blk,
4677 u32 new_clusters,
4678 u8 flags,
4679 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004680{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004681 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004682 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004683 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004684 struct ocfs2_insert_type insert = {0, };
4685 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004686
Mark Fashehdcd05382007-01-16 11:32:23 -08004687 mlog(0, "add %u clusters at position %u to inode %llu\n",
4688 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004689
Mark Fashehe48edee2007-03-07 16:46:57 -08004690 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004691 rec.e_cpos = cpu_to_le32(cpos);
4692 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004693 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004694 rec.e_flags = flags;
Joel Becker1e61ee72008-08-20 18:32:45 -07004695 status = ocfs2_et_insert_check(inode, et, &rec);
4696 if (status) {
4697 mlog_errno(status);
4698 goto bail;
4699 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004700
Tao Mae7d4cb62008-08-18 17:38:44 +08004701 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004702 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004703 if (status < 0) {
4704 mlog_errno(status);
4705 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004706 }
4707
Mark Fashehdcd05382007-01-16 11:32:23 -08004708 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4709 "Insert.contig_index: %d, Insert.free_records: %d, "
4710 "Insert.tree_depth: %d\n",
4711 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004712 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004713
Tao Maoc77534f2007-08-28 17:22:33 -07004714 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004715 status = ocfs2_grow_tree(inode, handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004716 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004717 meta_ac);
4718 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004719 mlog_errno(status);
4720 goto bail;
4721 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004722 }
4723
Mark Fashehdcd05382007-01-16 11:32:23 -08004724 /* Finally, we can add clusters. This might rotate the tree for us. */
Tao Mae7d4cb62008-08-18 17:38:44 +08004725 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004726 if (status < 0)
4727 mlog_errno(status);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004728 else if (et->et_ops == &ocfs2_dinode_et_ops)
Mark Fasheh83418972007-04-23 18:53:12 -07004729 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004730
4731bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004732 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004733
Tao Maf56654c2008-08-18 17:38:48 +08004734 mlog_exit(status);
4735 return status;
4736}
4737
Tao Ma0eb8d472008-08-18 17:38:45 +08004738/*
4739 * Allcate and add clusters into the extent b-tree.
4740 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004741 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004742 * it is not limited to the file storage. Any extent tree can use this
4743 * function if it implements the proper ocfs2_extent_tree.
4744 */
4745int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4746 struct inode *inode,
4747 u32 *logical_offset,
4748 u32 clusters_to_add,
4749 int mark_unwritten,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004750 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004751 handle_t *handle,
4752 struct ocfs2_alloc_context *data_ac,
4753 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004754 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004755{
4756 int status = 0;
4757 int free_extents;
4758 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4759 u32 bit_off, num_bits;
4760 u64 block;
4761 u8 flags = 0;
4762
4763 BUG_ON(!clusters_to_add);
4764
4765 if (mark_unwritten)
4766 flags = OCFS2_EXT_UNWRITTEN;
4767
Joel Becker3d03a302009-02-12 17:49:26 -08004768 free_extents = ocfs2_num_free_extents(osb, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004769 if (free_extents < 0) {
4770 status = free_extents;
4771 mlog_errno(status);
4772 goto leave;
4773 }
4774
4775 /* there are two cases which could cause us to EAGAIN in the
4776 * we-need-more-metadata case:
4777 * 1) we haven't reserved *any*
4778 * 2) we are so fragmented, we've needed to add metadata too
4779 * many times. */
4780 if (!free_extents && !meta_ac) {
4781 mlog(0, "we haven't reserved any metadata!\n");
4782 status = -EAGAIN;
4783 reason = RESTART_META;
4784 goto leave;
4785 } else if ((!free_extents)
4786 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004787 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004788 mlog(0, "filesystem is really fragmented...\n");
4789 status = -EAGAIN;
4790 reason = RESTART_META;
4791 goto leave;
4792 }
4793
4794 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4795 clusters_to_add, &bit_off, &num_bits);
4796 if (status < 0) {
4797 if (status != -ENOSPC)
4798 mlog_errno(status);
4799 goto leave;
4800 }
4801
4802 BUG_ON(num_bits > clusters_to_add);
4803
Joel Becker13723d02008-10-17 19:25:01 -07004804 /* reserve our write early -- insert_extent may update the tree root */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004805 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004806 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d472008-08-18 17:38:45 +08004807 if (status < 0) {
4808 mlog_errno(status);
4809 goto leave;
4810 }
4811
4812 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4813 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4814 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004815 status = ocfs2_insert_extent(osb, handle, inode, et,
4816 *logical_offset, block,
4817 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004818 if (status < 0) {
4819 mlog_errno(status);
4820 goto leave;
4821 }
4822
Joel Beckerf99b9b72008-08-20 19:36:33 -07004823 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004824 if (status < 0) {
4825 mlog_errno(status);
4826 goto leave;
4827 }
4828
4829 clusters_to_add -= num_bits;
4830 *logical_offset += num_bits;
4831
4832 if (clusters_to_add) {
4833 mlog(0, "need to alloc once more, wanted = %u\n",
4834 clusters_to_add);
4835 status = -EAGAIN;
4836 reason = RESTART_TRANS;
4837 }
4838
4839leave:
4840 mlog_exit(status);
4841 if (reason_ret)
4842 *reason_ret = reason;
4843 return status;
4844}
4845
Mark Fasheh328d5752007-06-18 10:48:04 -07004846static void ocfs2_make_right_split_rec(struct super_block *sb,
4847 struct ocfs2_extent_rec *split_rec,
4848 u32 cpos,
4849 struct ocfs2_extent_rec *rec)
4850{
4851 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4852 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4853
4854 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4855
4856 split_rec->e_cpos = cpu_to_le32(cpos);
4857 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4858
4859 split_rec->e_blkno = rec->e_blkno;
4860 le64_add_cpu(&split_rec->e_blkno,
4861 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4862
4863 split_rec->e_flags = rec->e_flags;
4864}
4865
4866static int ocfs2_split_and_insert(struct inode *inode,
4867 handle_t *handle,
4868 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004869 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004870 struct buffer_head **last_eb_bh,
4871 int split_index,
4872 struct ocfs2_extent_rec *orig_split_rec,
4873 struct ocfs2_alloc_context *meta_ac)
4874{
4875 int ret = 0, depth;
4876 unsigned int insert_range, rec_range, do_leftright = 0;
4877 struct ocfs2_extent_rec tmprec;
4878 struct ocfs2_extent_list *rightmost_el;
4879 struct ocfs2_extent_rec rec;
4880 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4881 struct ocfs2_insert_type insert;
4882 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004883
4884leftright:
4885 /*
4886 * Store a copy of the record on the stack - it might move
4887 * around as the tree is manipulated below.
4888 */
4889 rec = path_leaf_el(path)->l_recs[split_index];
4890
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004891 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004892
4893 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4894 if (depth) {
4895 BUG_ON(!(*last_eb_bh));
4896 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4897 rightmost_el = &eb->h_list;
4898 }
4899
4900 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4901 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004902 ret = ocfs2_grow_tree(inode, handle, et,
4903 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004904 if (ret) {
4905 mlog_errno(ret);
4906 goto out;
4907 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004908 }
4909
4910 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4911 insert.ins_appending = APPEND_NONE;
4912 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004913 insert.ins_tree_depth = depth;
4914
4915 insert_range = le32_to_cpu(split_rec.e_cpos) +
4916 le16_to_cpu(split_rec.e_leaf_clusters);
4917 rec_range = le32_to_cpu(rec.e_cpos) +
4918 le16_to_cpu(rec.e_leaf_clusters);
4919
4920 if (split_rec.e_cpos == rec.e_cpos) {
4921 insert.ins_split = SPLIT_LEFT;
4922 } else if (insert_range == rec_range) {
4923 insert.ins_split = SPLIT_RIGHT;
4924 } else {
4925 /*
4926 * Left/right split. We fake this as a right split
4927 * first and then make a second pass as a left split.
4928 */
4929 insert.ins_split = SPLIT_RIGHT;
4930
4931 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4932 &rec);
4933
4934 split_rec = tmprec;
4935
4936 BUG_ON(do_leftright);
4937 do_leftright = 1;
4938 }
4939
Tao Mae7d4cb62008-08-18 17:38:44 +08004940 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004941 if (ret) {
4942 mlog_errno(ret);
4943 goto out;
4944 }
4945
4946 if (do_leftright == 1) {
4947 u32 cpos;
4948 struct ocfs2_extent_list *el;
4949
4950 do_leftright++;
4951 split_rec = *orig_split_rec;
4952
4953 ocfs2_reinit_path(path, 1);
4954
4955 cpos = le32_to_cpu(split_rec.e_cpos);
Joel Beckerfacdb772009-02-12 18:08:48 -08004956 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07004957 if (ret) {
4958 mlog_errno(ret);
4959 goto out;
4960 }
4961
4962 el = path_leaf_el(path);
4963 split_index = ocfs2_search_extent_list(el, cpos);
4964 goto leftright;
4965 }
4966out:
4967
4968 return ret;
4969}
4970
Tao Ma47be12e2009-01-09 07:32:48 +08004971static int ocfs2_replace_extent_rec(struct inode *inode,
4972 handle_t *handle,
4973 struct ocfs2_path *path,
4974 struct ocfs2_extent_list *el,
4975 int split_index,
4976 struct ocfs2_extent_rec *split_rec)
4977{
4978 int ret;
4979
Joel Becker0cf2f762009-02-12 16:41:25 -08004980 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path,
Tao Ma47be12e2009-01-09 07:32:48 +08004981 path_num_items(path) - 1);
4982 if (ret) {
4983 mlog_errno(ret);
4984 goto out;
4985 }
4986
4987 el->l_recs[split_index] = *split_rec;
4988
4989 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4990out:
4991 return ret;
4992}
4993
Mark Fasheh328d5752007-06-18 10:48:04 -07004994/*
4995 * Mark part or all of the extent record at split_index in the leaf
4996 * pointed to by path as written. This removes the unwritten
4997 * extent flag.
4998 *
4999 * Care is taken to handle contiguousness so as to not grow the tree.
5000 *
5001 * meta_ac is not strictly necessary - we only truly need it if growth
5002 * of the tree is required. All other cases will degrade into a less
5003 * optimal tree layout.
5004 *
Tao Mae7d4cb62008-08-18 17:38:44 +08005005 * last_eb_bh should be the rightmost leaf block for any extent
5006 * btree. Since a split may grow the tree or a merge might shrink it,
5007 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07005008 *
5009 * This code is optimized for readability - several passes might be
5010 * made over certain portions of the tree. All of those blocks will
5011 * have been brought into cache (and pinned via the journal), so the
5012 * extra overhead is not expressed in terms of disk reads.
5013 */
5014static int __ocfs2_mark_extent_written(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08005015 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005016 handle_t *handle,
5017 struct ocfs2_path *path,
5018 int split_index,
5019 struct ocfs2_extent_rec *split_rec,
5020 struct ocfs2_alloc_context *meta_ac,
5021 struct ocfs2_cached_dealloc_ctxt *dealloc)
5022{
5023 int ret = 0;
5024 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08005025 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07005026 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5027 struct ocfs2_merge_ctxt ctxt;
5028 struct ocfs2_extent_list *rightmost_el;
5029
Roel Kluin3cf0c502007-10-27 00:20:36 +02005030 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07005031 ret = -EIO;
5032 mlog_errno(ret);
5033 goto out;
5034 }
5035
5036 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5037 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5038 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5039 ret = -EIO;
5040 mlog_errno(ret);
5041 goto out;
5042 }
5043
Tao Maad5a4d72008-01-30 14:21:32 +08005044 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005045 split_index,
5046 split_rec);
5047
5048 /*
5049 * The core merge / split code wants to know how much room is
5050 * left in this inodes allocation tree, so we pass the
5051 * rightmost extent list.
5052 */
5053 if (path->p_tree_depth) {
5054 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005055
Joel Becker3d03a302009-02-12 17:49:26 -08005056 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005057 ocfs2_et_get_last_eb_blk(et),
5058 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005059 if (ret) {
5060 mlog_exit(ret);
5061 goto out;
5062 }
5063
5064 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005065 rightmost_el = &eb->h_list;
5066 } else
5067 rightmost_el = path_root_el(path);
5068
Mark Fasheh328d5752007-06-18 10:48:04 -07005069 if (rec->e_cpos == split_rec->e_cpos &&
5070 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5071 ctxt.c_split_covers_rec = 1;
5072 else
5073 ctxt.c_split_covers_rec = 0;
5074
5075 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5076
Mark Fasheh015452b2007-09-12 10:21:22 -07005077 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5078 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5079 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005080
5081 if (ctxt.c_contig_type == CONTIG_NONE) {
5082 if (ctxt.c_split_covers_rec)
Tao Ma47be12e2009-01-09 07:32:48 +08005083 ret = ocfs2_replace_extent_rec(inode, handle,
5084 path, el,
5085 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005086 else
Tao Mae7d4cb62008-08-18 17:38:44 +08005087 ret = ocfs2_split_and_insert(inode, handle, path, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005088 &last_eb_bh, split_index,
5089 split_rec, meta_ac);
5090 if (ret)
5091 mlog_errno(ret);
5092 } else {
5093 ret = ocfs2_try_to_merge_extent(inode, handle, path,
5094 split_index, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08005095 dealloc, &ctxt, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005096 if (ret)
5097 mlog_errno(ret);
5098 }
5099
Mark Fasheh328d5752007-06-18 10:48:04 -07005100out:
5101 brelse(last_eb_bh);
5102 return ret;
5103}
5104
5105/*
5106 * Mark the already-existing extent at cpos as written for len clusters.
5107 *
5108 * If the existing extent is larger than the request, initiate a
5109 * split. An attempt will be made at merging with adjacent extents.
5110 *
5111 * The caller is responsible for passing down meta_ac if we'll need it.
5112 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005113int ocfs2_mark_extent_written(struct inode *inode,
5114 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005115 handle_t *handle, u32 cpos, u32 len, u32 phys,
5116 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005117 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005118{
5119 int ret, index;
5120 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
5121 struct ocfs2_extent_rec split_rec;
5122 struct ocfs2_path *left_path = NULL;
5123 struct ocfs2_extent_list *el;
5124
5125 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5126 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
5127
5128 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5129 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5130 "that are being written to, but the feature bit "
5131 "is not set in the super block.",
5132 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5133 ret = -EROFS;
5134 goto out;
5135 }
5136
5137 /*
5138 * XXX: This should be fixed up so that we just re-insert the
5139 * next extent records.
Joel Beckerf99b9b72008-08-20 19:36:33 -07005140 *
5141 * XXX: This is a hack on the extent tree, maybe it should be
5142 * an op?
Mark Fasheh328d5752007-06-18 10:48:04 -07005143 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005144 if (et->et_ops == &ocfs2_dinode_et_ops)
Tao Mae7d4cb62008-08-18 17:38:44 +08005145 ocfs2_extent_map_trunc(inode, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07005146
Joel Beckerffdd7a52008-10-17 22:32:01 -07005147 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005148 if (!left_path) {
5149 ret = -ENOMEM;
5150 mlog_errno(ret);
5151 goto out;
5152 }
5153
Joel Beckerfacdb772009-02-12 18:08:48 -08005154 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005155 if (ret) {
5156 mlog_errno(ret);
5157 goto out;
5158 }
5159 el = path_leaf_el(left_path);
5160
5161 index = ocfs2_search_extent_list(el, cpos);
5162 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5163 ocfs2_error(inode->i_sb,
5164 "Inode %llu has an extent at cpos %u which can no "
5165 "longer be found.\n",
5166 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5167 ret = -EROFS;
5168 goto out;
5169 }
5170
5171 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5172 split_rec.e_cpos = cpu_to_le32(cpos);
5173 split_rec.e_leaf_clusters = cpu_to_le16(len);
5174 split_rec.e_blkno = cpu_to_le64(start_blkno);
5175 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
5176 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
5177
Joel Beckerf99b9b72008-08-20 19:36:33 -07005178 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
Tao Mae7d4cb62008-08-18 17:38:44 +08005179 index, &split_rec, meta_ac,
5180 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005181 if (ret)
5182 mlog_errno(ret);
5183
5184out:
5185 ocfs2_free_path(left_path);
5186 return ret;
5187}
5188
Tao Mae7d4cb62008-08-18 17:38:44 +08005189static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005190 handle_t *handle, struct ocfs2_path *path,
5191 int index, u32 new_range,
5192 struct ocfs2_alloc_context *meta_ac)
5193{
5194 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005195 struct buffer_head *last_eb_bh = NULL;
5196 struct ocfs2_extent_block *eb;
5197 struct ocfs2_extent_list *rightmost_el, *el;
5198 struct ocfs2_extent_rec split_rec;
5199 struct ocfs2_extent_rec *rec;
5200 struct ocfs2_insert_type insert;
5201
5202 /*
5203 * Setup the record to split before we grow the tree.
5204 */
5205 el = path_leaf_el(path);
5206 rec = &el->l_recs[index];
5207 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5208
5209 depth = path->p_tree_depth;
5210 if (depth > 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08005211 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005212 ocfs2_et_get_last_eb_blk(et),
5213 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005214 if (ret < 0) {
5215 mlog_errno(ret);
5216 goto out;
5217 }
5218
5219 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5220 rightmost_el = &eb->h_list;
5221 } else
5222 rightmost_el = path_leaf_el(path);
5223
Tao Ma811f9332008-08-18 17:38:43 +08005224 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005225 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005226 ret = ocfs2_extend_trans(handle, credits);
5227 if (ret) {
5228 mlog_errno(ret);
5229 goto out;
5230 }
5231
5232 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5233 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005234 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005235 meta_ac);
5236 if (ret) {
5237 mlog_errno(ret);
5238 goto out;
5239 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005240 }
5241
5242 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5243 insert.ins_appending = APPEND_NONE;
5244 insert.ins_contig = CONTIG_NONE;
5245 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005246 insert.ins_tree_depth = depth;
5247
Tao Mae7d4cb62008-08-18 17:38:44 +08005248 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005249 if (ret)
5250 mlog_errno(ret);
5251
5252out:
5253 brelse(last_eb_bh);
5254 return ret;
5255}
5256
5257static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5258 struct ocfs2_path *path, int index,
5259 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08005260 u32 cpos, u32 len,
5261 struct ocfs2_extent_tree *et)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005262{
5263 int ret;
5264 u32 left_cpos, rec_range, trunc_range;
5265 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5266 struct super_block *sb = inode->i_sb;
5267 struct ocfs2_path *left_path = NULL;
5268 struct ocfs2_extent_list *el = path_leaf_el(path);
5269 struct ocfs2_extent_rec *rec;
5270 struct ocfs2_extent_block *eb;
5271
5272 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005273 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005274 if (ret) {
5275 mlog_errno(ret);
5276 goto out;
5277 }
5278
5279 index--;
5280 }
5281
5282 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5283 path->p_tree_depth) {
5284 /*
5285 * Check whether this is the rightmost tree record. If
5286 * we remove all of this record or part of its right
5287 * edge then an update of the record lengths above it
5288 * will be required.
5289 */
5290 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5291 if (eb->h_next_leaf_blk == 0)
5292 is_rightmost_tree_rec = 1;
5293 }
5294
5295 rec = &el->l_recs[index];
5296 if (index == 0 && path->p_tree_depth &&
5297 le32_to_cpu(rec->e_cpos) == cpos) {
5298 /*
5299 * Changing the leftmost offset (via partial or whole
5300 * record truncate) of an interior (or rightmost) path
5301 * means we have to update the subtree that is formed
5302 * by this leaf and the one to it's left.
5303 *
5304 * There are two cases we can skip:
5305 * 1) Path is the leftmost one in our inode tree.
5306 * 2) The leaf is rightmost and will be empty after
5307 * we remove the extent record - the rotate code
5308 * knows how to update the newly formed edge.
5309 */
5310
5311 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5312 &left_cpos);
5313 if (ret) {
5314 mlog_errno(ret);
5315 goto out;
5316 }
5317
5318 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005319 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005320 if (!left_path) {
5321 ret = -ENOMEM;
5322 mlog_errno(ret);
5323 goto out;
5324 }
5325
Joel Beckerfacdb772009-02-12 18:08:48 -08005326 ret = ocfs2_find_path(et->et_ci, left_path,
5327 left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005328 if (ret) {
5329 mlog_errno(ret);
5330 goto out;
5331 }
5332 }
5333 }
5334
5335 ret = ocfs2_extend_rotate_transaction(handle, 0,
5336 handle->h_buffer_credits,
5337 path);
5338 if (ret) {
5339 mlog_errno(ret);
5340 goto out;
5341 }
5342
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005343 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005344 if (ret) {
5345 mlog_errno(ret);
5346 goto out;
5347 }
5348
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005349 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005350 if (ret) {
5351 mlog_errno(ret);
5352 goto out;
5353 }
5354
5355 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5356 trunc_range = cpos + len;
5357
5358 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5359 int next_free;
5360
5361 memset(rec, 0, sizeof(*rec));
5362 ocfs2_cleanup_merge(el, index);
5363 wants_rotate = 1;
5364
5365 next_free = le16_to_cpu(el->l_next_free_rec);
5366 if (is_rightmost_tree_rec && next_free > 1) {
5367 /*
5368 * We skip the edge update if this path will
5369 * be deleted by the rotate code.
5370 */
5371 rec = &el->l_recs[next_free - 1];
5372 ocfs2_adjust_rightmost_records(inode, handle, path,
5373 rec);
5374 }
5375 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5376 /* Remove leftmost portion of the record. */
5377 le32_add_cpu(&rec->e_cpos, len);
5378 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5379 le16_add_cpu(&rec->e_leaf_clusters, -len);
5380 } else if (rec_range == trunc_range) {
5381 /* Remove rightmost portion of the record */
5382 le16_add_cpu(&rec->e_leaf_clusters, -len);
5383 if (is_rightmost_tree_rec)
5384 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5385 } else {
5386 /* Caller should have trapped this. */
5387 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5388 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5389 le32_to_cpu(rec->e_cpos),
5390 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5391 BUG();
5392 }
5393
5394 if (left_path) {
5395 int subtree_index;
5396
5397 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5398 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5399 subtree_index);
5400 }
5401
5402 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5403
Tao Mae7d4cb62008-08-18 17:38:44 +08005404 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005405 if (ret) {
5406 mlog_errno(ret);
5407 goto out;
5408 }
5409
5410out:
5411 ocfs2_free_path(left_path);
5412 return ret;
5413}
5414
Joel Beckerf99b9b72008-08-20 19:36:33 -07005415int ocfs2_remove_extent(struct inode *inode,
5416 struct ocfs2_extent_tree *et,
Mark Fasheh063c4562007-07-03 13:34:11 -07005417 u32 cpos, u32 len, handle_t *handle,
5418 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005419 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005420{
5421 int ret, index;
5422 u32 rec_range, trunc_range;
5423 struct ocfs2_extent_rec *rec;
5424 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005425 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005426
5427 ocfs2_extent_map_trunc(inode, 0);
5428
Joel Beckerffdd7a52008-10-17 22:32:01 -07005429 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005430 if (!path) {
5431 ret = -ENOMEM;
5432 mlog_errno(ret);
5433 goto out;
5434 }
5435
Joel Beckerfacdb772009-02-12 18:08:48 -08005436 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005437 if (ret) {
5438 mlog_errno(ret);
5439 goto out;
5440 }
5441
5442 el = path_leaf_el(path);
5443 index = ocfs2_search_extent_list(el, cpos);
5444 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5445 ocfs2_error(inode->i_sb,
5446 "Inode %llu has an extent at cpos %u which can no "
5447 "longer be found.\n",
5448 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5449 ret = -EROFS;
5450 goto out;
5451 }
5452
5453 /*
5454 * We have 3 cases of extent removal:
5455 * 1) Range covers the entire extent rec
5456 * 2) Range begins or ends on one edge of the extent rec
5457 * 3) Range is in the middle of the extent rec (no shared edges)
5458 *
5459 * For case 1 we remove the extent rec and left rotate to
5460 * fill the hole.
5461 *
5462 * For case 2 we just shrink the existing extent rec, with a
5463 * tree update if the shrinking edge is also the edge of an
5464 * extent block.
5465 *
5466 * For case 3 we do a right split to turn the extent rec into
5467 * something case 2 can handle.
5468 */
5469 rec = &el->l_recs[index];
5470 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5471 trunc_range = cpos + len;
5472
5473 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5474
5475 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5476 "(cpos %u, len %u)\n",
5477 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5478 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5479
5480 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5481 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005482 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005483 if (ret) {
5484 mlog_errno(ret);
5485 goto out;
5486 }
5487 } else {
Joel Beckerf99b9b72008-08-20 19:36:33 -07005488 ret = ocfs2_split_tree(inode, et, handle, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005489 trunc_range, meta_ac);
5490 if (ret) {
5491 mlog_errno(ret);
5492 goto out;
5493 }
5494
5495 /*
5496 * The split could have manipulated the tree enough to
5497 * move the record location, so we have to look for it again.
5498 */
5499 ocfs2_reinit_path(path, 1);
5500
Joel Beckerfacdb772009-02-12 18:08:48 -08005501 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005502 if (ret) {
5503 mlog_errno(ret);
5504 goto out;
5505 }
5506
5507 el = path_leaf_el(path);
5508 index = ocfs2_search_extent_list(el, cpos);
5509 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5510 ocfs2_error(inode->i_sb,
5511 "Inode %llu: split at cpos %u lost record.",
5512 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5513 cpos);
5514 ret = -EROFS;
5515 goto out;
5516 }
5517
5518 /*
5519 * Double check our values here. If anything is fishy,
5520 * it's easier to catch it at the top level.
5521 */
5522 rec = &el->l_recs[index];
5523 rec_range = le32_to_cpu(rec->e_cpos) +
5524 ocfs2_rec_clusters(el, rec);
5525 if (rec_range != trunc_range) {
5526 ocfs2_error(inode->i_sb,
5527 "Inode %llu: error after split at cpos %u"
5528 "trunc len %u, existing record is (%u,%u)",
5529 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5530 cpos, len, le32_to_cpu(rec->e_cpos),
5531 ocfs2_rec_clusters(el, rec));
5532 ret = -EROFS;
5533 goto out;
5534 }
5535
5536 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005537 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005538 if (ret) {
5539 mlog_errno(ret);
5540 goto out;
5541 }
5542 }
5543
5544out:
5545 ocfs2_free_path(path);
5546 return ret;
5547}
5548
Mark Fashehfecc0112008-11-12 15:16:38 -08005549int ocfs2_remove_btree_range(struct inode *inode,
5550 struct ocfs2_extent_tree *et,
5551 u32 cpos, u32 phys_cpos, u32 len,
5552 struct ocfs2_cached_dealloc_ctxt *dealloc)
5553{
5554 int ret;
5555 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5556 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5557 struct inode *tl_inode = osb->osb_tl_inode;
5558 handle_t *handle;
5559 struct ocfs2_alloc_context *meta_ac = NULL;
5560
5561 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5562 if (ret) {
5563 mlog_errno(ret);
5564 return ret;
5565 }
5566
5567 mutex_lock(&tl_inode->i_mutex);
5568
5569 if (ocfs2_truncate_log_needs_flush(osb)) {
5570 ret = __ocfs2_flush_truncate_log(osb);
5571 if (ret < 0) {
5572 mlog_errno(ret);
5573 goto out;
5574 }
5575 }
5576
Jan Karaa90714c2008-10-09 19:38:40 +02005577 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005578 if (IS_ERR(handle)) {
5579 ret = PTR_ERR(handle);
5580 mlog_errno(ret);
5581 goto out;
5582 }
5583
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005584 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07005585 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005586 if (ret) {
5587 mlog_errno(ret);
5588 goto out;
5589 }
5590
Mark Fashehfd4ef232009-01-29 15:06:21 -08005591 vfs_dq_free_space_nodirty(inode,
5592 ocfs2_clusters_to_bytes(inode->i_sb, len));
5593
Mark Fashehfecc0112008-11-12 15:16:38 -08005594 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5595 dealloc);
5596 if (ret) {
5597 mlog_errno(ret);
5598 goto out_commit;
5599 }
5600
5601 ocfs2_et_update_clusters(inode, et, -len);
5602
5603 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5604 if (ret) {
5605 mlog_errno(ret);
5606 goto out_commit;
5607 }
5608
5609 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5610 if (ret)
5611 mlog_errno(ret);
5612
5613out_commit:
5614 ocfs2_commit_trans(osb, handle);
5615out:
5616 mutex_unlock(&tl_inode->i_mutex);
5617
5618 if (meta_ac)
5619 ocfs2_free_alloc_context(meta_ac);
5620
5621 return ret;
5622}
5623
Mark Fasheh063c4562007-07-03 13:34:11 -07005624int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005625{
5626 struct buffer_head *tl_bh = osb->osb_tl_bh;
5627 struct ocfs2_dinode *di;
5628 struct ocfs2_truncate_log *tl;
5629
5630 di = (struct ocfs2_dinode *) tl_bh->b_data;
5631 tl = &di->id2.i_dealloc;
5632
5633 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5634 "slot %d, invalid truncate log parameters: used = "
5635 "%u, count = %u\n", osb->slot_num,
5636 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5637 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5638}
5639
5640static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5641 unsigned int new_start)
5642{
5643 unsigned int tail_index;
5644 unsigned int current_tail;
5645
5646 /* No records, nothing to coalesce */
5647 if (!le16_to_cpu(tl->tl_used))
5648 return 0;
5649
5650 tail_index = le16_to_cpu(tl->tl_used) - 1;
5651 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5652 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5653
5654 return current_tail == new_start;
5655}
5656
Mark Fasheh063c4562007-07-03 13:34:11 -07005657int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5658 handle_t *handle,
5659 u64 start_blk,
5660 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005661{
5662 int status, index;
5663 unsigned int start_cluster, tl_count;
5664 struct inode *tl_inode = osb->osb_tl_inode;
5665 struct buffer_head *tl_bh = osb->osb_tl_bh;
5666 struct ocfs2_dinode *di;
5667 struct ocfs2_truncate_log *tl;
5668
Mark Fashehb06970532006-03-03 10:24:33 -08005669 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5670 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005671
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005672 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005673
5674 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5675
5676 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005677
Joel Becker10995aa2008-11-13 14:49:12 -08005678 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5679 * by the underlying call to ocfs2_read_inode_block(), so any
5680 * corruption is a code bug */
5681 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5682
5683 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005684 tl_count = le16_to_cpu(tl->tl_count);
5685 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5686 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005687 "Truncate record count on #%llu invalid "
5688 "wanted %u, actual %u\n",
5689 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005690 ocfs2_truncate_recs_per_inode(osb->sb),
5691 le16_to_cpu(tl->tl_count));
5692
5693 /* Caller should have known to flush before calling us. */
5694 index = le16_to_cpu(tl->tl_used);
5695 if (index >= tl_count) {
5696 status = -ENOSPC;
5697 mlog_errno(status);
5698 goto bail;
5699 }
5700
Joel Becker0cf2f762009-02-12 16:41:25 -08005701 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005702 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005703 if (status < 0) {
5704 mlog_errno(status);
5705 goto bail;
5706 }
5707
5708 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005709 "%llu (index = %d)\n", num_clusters, start_cluster,
5710 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005711
5712 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5713 /*
5714 * Move index back to the record we are coalescing with.
5715 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5716 */
5717 index--;
5718
5719 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5720 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5721 index, le32_to_cpu(tl->tl_recs[index].t_start),
5722 num_clusters);
5723 } else {
5724 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5725 tl->tl_used = cpu_to_le16(index + 1);
5726 }
5727 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5728
5729 status = ocfs2_journal_dirty(handle, tl_bh);
5730 if (status < 0) {
5731 mlog_errno(status);
5732 goto bail;
5733 }
5734
5735bail:
5736 mlog_exit(status);
5737 return status;
5738}
5739
5740static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005741 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005742 struct inode *data_alloc_inode,
5743 struct buffer_head *data_alloc_bh)
5744{
5745 int status = 0;
5746 int i;
5747 unsigned int num_clusters;
5748 u64 start_blk;
5749 struct ocfs2_truncate_rec rec;
5750 struct ocfs2_dinode *di;
5751 struct ocfs2_truncate_log *tl;
5752 struct inode *tl_inode = osb->osb_tl_inode;
5753 struct buffer_head *tl_bh = osb->osb_tl_bh;
5754
5755 mlog_entry_void();
5756
5757 di = (struct ocfs2_dinode *) tl_bh->b_data;
5758 tl = &di->id2.i_dealloc;
5759 i = le16_to_cpu(tl->tl_used) - 1;
5760 while (i >= 0) {
5761 /* Caller has given us at least enough credits to
5762 * update the truncate log dinode */
Joel Becker0cf2f762009-02-12 16:41:25 -08005763 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005764 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005765 if (status < 0) {
5766 mlog_errno(status);
5767 goto bail;
5768 }
5769
5770 tl->tl_used = cpu_to_le16(i);
5771
5772 status = ocfs2_journal_dirty(handle, tl_bh);
5773 if (status < 0) {
5774 mlog_errno(status);
5775 goto bail;
5776 }
5777
5778 /* TODO: Perhaps we can calculate the bulk of the
5779 * credits up front rather than extending like
5780 * this. */
5781 status = ocfs2_extend_trans(handle,
5782 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5783 if (status < 0) {
5784 mlog_errno(status);
5785 goto bail;
5786 }
5787
5788 rec = tl->tl_recs[i];
5789 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5790 le32_to_cpu(rec.t_start));
5791 num_clusters = le32_to_cpu(rec.t_clusters);
5792
5793 /* if start_blk is not set, we ignore the record as
5794 * invalid. */
5795 if (start_blk) {
5796 mlog(0, "free record %d, start = %u, clusters = %u\n",
5797 i, le32_to_cpu(rec.t_start), num_clusters);
5798
5799 status = ocfs2_free_clusters(handle, data_alloc_inode,
5800 data_alloc_bh, start_blk,
5801 num_clusters);
5802 if (status < 0) {
5803 mlog_errno(status);
5804 goto bail;
5805 }
5806 }
5807 i--;
5808 }
5809
5810bail:
5811 mlog_exit(status);
5812 return status;
5813}
5814
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005815/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005816int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005817{
5818 int status;
5819 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005820 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005821 struct inode *tl_inode = osb->osb_tl_inode;
5822 struct inode *data_alloc_inode = NULL;
5823 struct buffer_head *tl_bh = osb->osb_tl_bh;
5824 struct buffer_head *data_alloc_bh = NULL;
5825 struct ocfs2_dinode *di;
5826 struct ocfs2_truncate_log *tl;
5827
5828 mlog_entry_void();
5829
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005830 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005831
5832 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005833
Joel Becker10995aa2008-11-13 14:49:12 -08005834 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5835 * by the underlying call to ocfs2_read_inode_block(), so any
5836 * corruption is a code bug */
5837 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5838
5839 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005840 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005841 mlog(0, "Flush %u records from truncate log #%llu\n",
5842 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005843 if (!num_to_flush) {
5844 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005845 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005846 }
5847
5848 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5849 GLOBAL_BITMAP_SYSTEM_INODE,
5850 OCFS2_INVALID_SLOT);
5851 if (!data_alloc_inode) {
5852 status = -EINVAL;
5853 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005854 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005855 }
5856
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005857 mutex_lock(&data_alloc_inode->i_mutex);
5858
Mark Fashehe63aecb62007-10-18 15:30:42 -07005859 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005860 if (status < 0) {
5861 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005862 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005863 }
5864
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005865 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005866 if (IS_ERR(handle)) {
5867 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005868 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005869 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005870 }
5871
5872 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5873 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005874 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005875 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005876
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005877 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005878
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005879out_unlock:
5880 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005881 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005882
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005883out_mutex:
5884 mutex_unlock(&data_alloc_inode->i_mutex);
5885 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005886
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005887out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005888 mlog_exit(status);
5889 return status;
5890}
5891
5892int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5893{
5894 int status;
5895 struct inode *tl_inode = osb->osb_tl_inode;
5896
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005897 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005898 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005899 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005900
5901 return status;
5902}
5903
David Howellsc4028952006-11-22 14:57:56 +00005904static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005905{
5906 int status;
David Howellsc4028952006-11-22 14:57:56 +00005907 struct ocfs2_super *osb =
5908 container_of(work, struct ocfs2_super,
5909 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005910
5911 mlog_entry_void();
5912
5913 status = ocfs2_flush_truncate_log(osb);
5914 if (status < 0)
5915 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005916 else
5917 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005918
5919 mlog_exit(status);
5920}
5921
5922#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5923void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5924 int cancel)
5925{
5926 if (osb->osb_tl_inode) {
5927 /* We want to push off log flushes while truncates are
5928 * still running. */
5929 if (cancel)
5930 cancel_delayed_work(&osb->osb_truncate_log_wq);
5931
5932 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5933 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5934 }
5935}
5936
5937static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5938 int slot_num,
5939 struct inode **tl_inode,
5940 struct buffer_head **tl_bh)
5941{
5942 int status;
5943 struct inode *inode = NULL;
5944 struct buffer_head *bh = NULL;
5945
5946 inode = ocfs2_get_system_file_inode(osb,
5947 TRUNCATE_LOG_SYSTEM_INODE,
5948 slot_num);
5949 if (!inode) {
5950 status = -EINVAL;
5951 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5952 goto bail;
5953 }
5954
Joel Beckerb657c952008-11-13 14:49:11 -08005955 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005956 if (status < 0) {
5957 iput(inode);
5958 mlog_errno(status);
5959 goto bail;
5960 }
5961
5962 *tl_inode = inode;
5963 *tl_bh = bh;
5964bail:
5965 mlog_exit(status);
5966 return status;
5967}
5968
5969/* called during the 1st stage of node recovery. we stamp a clean
5970 * truncate log and pass back a copy for processing later. if the
5971 * truncate log does not require processing, a *tl_copy is set to
5972 * NULL. */
5973int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5974 int slot_num,
5975 struct ocfs2_dinode **tl_copy)
5976{
5977 int status;
5978 struct inode *tl_inode = NULL;
5979 struct buffer_head *tl_bh = NULL;
5980 struct ocfs2_dinode *di;
5981 struct ocfs2_truncate_log *tl;
5982
5983 *tl_copy = NULL;
5984
5985 mlog(0, "recover truncate log from slot %d\n", slot_num);
5986
5987 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5988 if (status < 0) {
5989 mlog_errno(status);
5990 goto bail;
5991 }
5992
5993 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005994
Joel Becker10995aa2008-11-13 14:49:12 -08005995 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5996 * validated by the underlying call to ocfs2_read_inode_block(),
5997 * so any corruption is a code bug */
5998 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5999
6000 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08006001 if (le16_to_cpu(tl->tl_used)) {
6002 mlog(0, "We'll have %u logs to recover\n",
6003 le16_to_cpu(tl->tl_used));
6004
6005 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6006 if (!(*tl_copy)) {
6007 status = -ENOMEM;
6008 mlog_errno(status);
6009 goto bail;
6010 }
6011
6012 /* Assuming the write-out below goes well, this copy
6013 * will be passed back to recovery for processing. */
6014 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6015
6016 /* All we need to do to clear the truncate log is set
6017 * tl_used. */
6018 tl->tl_used = 0;
6019
Joel Becker13723d02008-10-17 19:25:01 -07006020 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08006021 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08006022 if (status < 0) {
6023 mlog_errno(status);
6024 goto bail;
6025 }
6026 }
6027
6028bail:
6029 if (tl_inode)
6030 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006031 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006032
6033 if (status < 0 && (*tl_copy)) {
6034 kfree(*tl_copy);
6035 *tl_copy = NULL;
6036 }
6037
6038 mlog_exit(status);
6039 return status;
6040}
6041
6042int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6043 struct ocfs2_dinode *tl_copy)
6044{
6045 int status = 0;
6046 int i;
6047 unsigned int clusters, num_recs, start_cluster;
6048 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006049 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006050 struct inode *tl_inode = osb->osb_tl_inode;
6051 struct ocfs2_truncate_log *tl;
6052
6053 mlog_entry_void();
6054
6055 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6056 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6057 return -EINVAL;
6058 }
6059
6060 tl = &tl_copy->id2.i_dealloc;
6061 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08006062 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006063 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006064
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006065 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006066 for(i = 0; i < num_recs; i++) {
6067 if (ocfs2_truncate_log_needs_flush(osb)) {
6068 status = __ocfs2_flush_truncate_log(osb);
6069 if (status < 0) {
6070 mlog_errno(status);
6071 goto bail_up;
6072 }
6073 }
6074
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006075 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006076 if (IS_ERR(handle)) {
6077 status = PTR_ERR(handle);
6078 mlog_errno(status);
6079 goto bail_up;
6080 }
6081
6082 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6083 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6084 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6085
6086 status = ocfs2_truncate_log_append(osb, handle,
6087 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006088 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006089 if (status < 0) {
6090 mlog_errno(status);
6091 goto bail_up;
6092 }
6093 }
6094
6095bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006096 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006097
6098 mlog_exit(status);
6099 return status;
6100}
6101
6102void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6103{
6104 int status;
6105 struct inode *tl_inode = osb->osb_tl_inode;
6106
6107 mlog_entry_void();
6108
6109 if (tl_inode) {
6110 cancel_delayed_work(&osb->osb_truncate_log_wq);
6111 flush_workqueue(ocfs2_wq);
6112
6113 status = ocfs2_flush_truncate_log(osb);
6114 if (status < 0)
6115 mlog_errno(status);
6116
6117 brelse(osb->osb_tl_bh);
6118 iput(osb->osb_tl_inode);
6119 }
6120
6121 mlog_exit_void();
6122}
6123
6124int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6125{
6126 int status;
6127 struct inode *tl_inode = NULL;
6128 struct buffer_head *tl_bh = NULL;
6129
6130 mlog_entry_void();
6131
6132 status = ocfs2_get_truncate_log_info(osb,
6133 osb->slot_num,
6134 &tl_inode,
6135 &tl_bh);
6136 if (status < 0)
6137 mlog_errno(status);
6138
6139 /* ocfs2_truncate_log_shutdown keys on the existence of
6140 * osb->osb_tl_inode so we don't set any of the osb variables
6141 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006142 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6143 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006144 osb->osb_tl_bh = tl_bh;
6145 osb->osb_tl_inode = tl_inode;
6146
6147 mlog_exit(status);
6148 return status;
6149}
6150
Mark Fasheh2b604352007-06-22 15:45:27 -07006151/*
6152 * Delayed de-allocation of suballocator blocks.
6153 *
6154 * Some sets of block de-allocations might involve multiple suballocator inodes.
6155 *
6156 * The locking for this can get extremely complicated, especially when
6157 * the suballocator inodes to delete from aren't known until deep
6158 * within an unrelated codepath.
6159 *
6160 * ocfs2_extent_block structures are a good example of this - an inode
6161 * btree could have been grown by any number of nodes each allocating
6162 * out of their own suballoc inode.
6163 *
6164 * These structures allow the delay of block de-allocation until a
6165 * later time, when locking of multiple cluster inodes won't cause
6166 * deadlock.
6167 */
6168
6169/*
Tao Ma2891d292008-11-12 08:26:58 +08006170 * Describe a single bit freed from a suballocator. For the block
6171 * suballocators, it represents one block. For the global cluster
6172 * allocator, it represents some clusters and free_bit indicates
6173 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006174 */
6175struct ocfs2_cached_block_free {
6176 struct ocfs2_cached_block_free *free_next;
6177 u64 free_blk;
6178 unsigned int free_bit;
6179};
6180
6181struct ocfs2_per_slot_free_list {
6182 struct ocfs2_per_slot_free_list *f_next_suballocator;
6183 int f_inode_type;
6184 int f_slot;
6185 struct ocfs2_cached_block_free *f_first;
6186};
6187
Tao Ma2891d292008-11-12 08:26:58 +08006188static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6189 int sysfile_type,
6190 int slot,
6191 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006192{
6193 int ret;
6194 u64 bg_blkno;
6195 handle_t *handle;
6196 struct inode *inode;
6197 struct buffer_head *di_bh = NULL;
6198 struct ocfs2_cached_block_free *tmp;
6199
6200 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6201 if (!inode) {
6202 ret = -EINVAL;
6203 mlog_errno(ret);
6204 goto out;
6205 }
6206
6207 mutex_lock(&inode->i_mutex);
6208
Mark Fashehe63aecb62007-10-18 15:30:42 -07006209 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006210 if (ret) {
6211 mlog_errno(ret);
6212 goto out_mutex;
6213 }
6214
6215 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6216 if (IS_ERR(handle)) {
6217 ret = PTR_ERR(handle);
6218 mlog_errno(ret);
6219 goto out_unlock;
6220 }
6221
6222 while (head) {
6223 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6224 head->free_bit);
6225 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6226 head->free_bit, (unsigned long long)head->free_blk);
6227
6228 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6229 head->free_bit, bg_blkno, 1);
6230 if (ret) {
6231 mlog_errno(ret);
6232 goto out_journal;
6233 }
6234
6235 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6236 if (ret) {
6237 mlog_errno(ret);
6238 goto out_journal;
6239 }
6240
6241 tmp = head;
6242 head = head->free_next;
6243 kfree(tmp);
6244 }
6245
6246out_journal:
6247 ocfs2_commit_trans(osb, handle);
6248
6249out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006250 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006251 brelse(di_bh);
6252out_mutex:
6253 mutex_unlock(&inode->i_mutex);
6254 iput(inode);
6255out:
6256 while(head) {
6257 /* Premature exit may have left some dangling items. */
6258 tmp = head;
6259 head = head->free_next;
6260 kfree(tmp);
6261 }
6262
6263 return ret;
6264}
6265
Tao Ma2891d292008-11-12 08:26:58 +08006266int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6267 u64 blkno, unsigned int bit)
6268{
6269 int ret = 0;
6270 struct ocfs2_cached_block_free *item;
6271
6272 item = kmalloc(sizeof(*item), GFP_NOFS);
6273 if (item == NULL) {
6274 ret = -ENOMEM;
6275 mlog_errno(ret);
6276 return ret;
6277 }
6278
6279 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6280 bit, (unsigned long long)blkno);
6281
6282 item->free_blk = blkno;
6283 item->free_bit = bit;
6284 item->free_next = ctxt->c_global_allocator;
6285
6286 ctxt->c_global_allocator = item;
6287 return ret;
6288}
6289
6290static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6291 struct ocfs2_cached_block_free *head)
6292{
6293 struct ocfs2_cached_block_free *tmp;
6294 struct inode *tl_inode = osb->osb_tl_inode;
6295 handle_t *handle;
6296 int ret = 0;
6297
6298 mutex_lock(&tl_inode->i_mutex);
6299
6300 while (head) {
6301 if (ocfs2_truncate_log_needs_flush(osb)) {
6302 ret = __ocfs2_flush_truncate_log(osb);
6303 if (ret < 0) {
6304 mlog_errno(ret);
6305 break;
6306 }
6307 }
6308
6309 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6310 if (IS_ERR(handle)) {
6311 ret = PTR_ERR(handle);
6312 mlog_errno(ret);
6313 break;
6314 }
6315
6316 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6317 head->free_bit);
6318
6319 ocfs2_commit_trans(osb, handle);
6320 tmp = head;
6321 head = head->free_next;
6322 kfree(tmp);
6323
6324 if (ret < 0) {
6325 mlog_errno(ret);
6326 break;
6327 }
6328 }
6329
6330 mutex_unlock(&tl_inode->i_mutex);
6331
6332 while (head) {
6333 /* Premature exit may have left some dangling items. */
6334 tmp = head;
6335 head = head->free_next;
6336 kfree(tmp);
6337 }
6338
6339 return ret;
6340}
6341
Mark Fasheh2b604352007-06-22 15:45:27 -07006342int ocfs2_run_deallocs(struct ocfs2_super *osb,
6343 struct ocfs2_cached_dealloc_ctxt *ctxt)
6344{
6345 int ret = 0, ret2;
6346 struct ocfs2_per_slot_free_list *fl;
6347
6348 if (!ctxt)
6349 return 0;
6350
6351 while (ctxt->c_first_suballocator) {
6352 fl = ctxt->c_first_suballocator;
6353
6354 if (fl->f_first) {
6355 mlog(0, "Free items: (type %u, slot %d)\n",
6356 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006357 ret2 = ocfs2_free_cached_blocks(osb,
6358 fl->f_inode_type,
6359 fl->f_slot,
6360 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006361 if (ret2)
6362 mlog_errno(ret2);
6363 if (!ret)
6364 ret = ret2;
6365 }
6366
6367 ctxt->c_first_suballocator = fl->f_next_suballocator;
6368 kfree(fl);
6369 }
6370
Tao Ma2891d292008-11-12 08:26:58 +08006371 if (ctxt->c_global_allocator) {
6372 ret2 = ocfs2_free_cached_clusters(osb,
6373 ctxt->c_global_allocator);
6374 if (ret2)
6375 mlog_errno(ret2);
6376 if (!ret)
6377 ret = ret2;
6378
6379 ctxt->c_global_allocator = NULL;
6380 }
6381
Mark Fasheh2b604352007-06-22 15:45:27 -07006382 return ret;
6383}
6384
6385static struct ocfs2_per_slot_free_list *
6386ocfs2_find_per_slot_free_list(int type,
6387 int slot,
6388 struct ocfs2_cached_dealloc_ctxt *ctxt)
6389{
6390 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6391
6392 while (fl) {
6393 if (fl->f_inode_type == type && fl->f_slot == slot)
6394 return fl;
6395
6396 fl = fl->f_next_suballocator;
6397 }
6398
6399 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6400 if (fl) {
6401 fl->f_inode_type = type;
6402 fl->f_slot = slot;
6403 fl->f_first = NULL;
6404 fl->f_next_suballocator = ctxt->c_first_suballocator;
6405
6406 ctxt->c_first_suballocator = fl;
6407 }
6408 return fl;
6409}
6410
6411static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6412 int type, int slot, u64 blkno,
6413 unsigned int bit)
6414{
6415 int ret;
6416 struct ocfs2_per_slot_free_list *fl;
6417 struct ocfs2_cached_block_free *item;
6418
6419 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6420 if (fl == NULL) {
6421 ret = -ENOMEM;
6422 mlog_errno(ret);
6423 goto out;
6424 }
6425
6426 item = kmalloc(sizeof(*item), GFP_NOFS);
6427 if (item == NULL) {
6428 ret = -ENOMEM;
6429 mlog_errno(ret);
6430 goto out;
6431 }
6432
6433 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6434 type, slot, bit, (unsigned long long)blkno);
6435
6436 item->free_blk = blkno;
6437 item->free_bit = bit;
6438 item->free_next = fl->f_first;
6439
6440 fl->f_first = item;
6441
6442 ret = 0;
6443out:
6444 return ret;
6445}
6446
Mark Fasheh59a5e412007-06-22 15:52:36 -07006447static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6448 struct ocfs2_extent_block *eb)
6449{
6450 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6451 le16_to_cpu(eb->h_suballoc_slot),
6452 le64_to_cpu(eb->h_blkno),
6453 le16_to_cpu(eb->h_suballoc_bit));
6454}
6455
Mark Fashehccd979b2005-12-15 14:31:24 -08006456/* This function will figure out whether the currently last extent
6457 * block will be deleted, and if it will, what the new last extent
6458 * block will be so we can update his h_next_leaf_blk field, as well
6459 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006460static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006461 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006462 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006463 struct buffer_head **new_last_eb)
6464{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006465 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006466 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006467 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006468 struct ocfs2_extent_block *eb;
6469 struct ocfs2_extent_list *el;
6470 struct buffer_head *bh = NULL;
6471
6472 *new_last_eb = NULL;
6473
Mark Fashehccd979b2005-12-15 14:31:24 -08006474 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006475 if (!path->p_tree_depth)
6476 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006477
6478 /* trunc to zero special case - this makes tree_depth = 0
6479 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006480 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006481 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006482
Mark Fashehdcd05382007-01-16 11:32:23 -08006483 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006484 BUG_ON(!el->l_next_free_rec);
6485
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006486 /*
6487 * Make sure that this extent list will actually be empty
6488 * after we clear away the data. We can shortcut out if
6489 * there's more than one non-empty extent in the
6490 * list. Otherwise, a check of the remaining extent is
6491 * necessary.
6492 */
6493 next_free = le16_to_cpu(el->l_next_free_rec);
6494 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006495 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006496 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006497 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006498
6499 /* We may have a valid extent in index 1, check it. */
6500 if (next_free == 2)
6501 rec = &el->l_recs[1];
6502
6503 /*
6504 * Fall through - no more nonempty extents, so we want
6505 * to delete this leaf.
6506 */
6507 } else {
6508 if (next_free > 1)
6509 goto out;
6510
6511 rec = &el->l_recs[0];
6512 }
6513
6514 if (rec) {
6515 /*
6516 * Check it we'll only be trimming off the end of this
6517 * cluster.
6518 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006519 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006520 goto out;
6521 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006522
Mark Fashehdcd05382007-01-16 11:32:23 -08006523 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6524 if (ret) {
6525 mlog_errno(ret);
6526 goto out;
6527 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006528
Joel Beckerfacdb772009-02-12 18:08:48 -08006529 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08006530 if (ret) {
6531 mlog_errno(ret);
6532 goto out;
6533 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006534
Mark Fashehdcd05382007-01-16 11:32:23 -08006535 eb = (struct ocfs2_extent_block *) bh->b_data;
6536 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006537
6538 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6539 * Any corruption is a code bug. */
6540 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006541
6542 *new_last_eb = bh;
6543 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006544 mlog(0, "returning block %llu, (cpos: %u)\n",
6545 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6546out:
6547 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006548
Mark Fashehdcd05382007-01-16 11:32:23 -08006549 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006550}
6551
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006552/*
6553 * Trim some clusters off the rightmost edge of a tree. Only called
6554 * during truncate.
6555 *
6556 * The caller needs to:
6557 * - start journaling of each path component.
6558 * - compute and fully set up any new last ext block
6559 */
6560static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6561 handle_t *handle, struct ocfs2_truncate_context *tc,
6562 u32 clusters_to_del, u64 *delete_start)
6563{
6564 int ret, i, index = path->p_tree_depth;
6565 u32 new_edge = 0;
6566 u64 deleted_eb = 0;
6567 struct buffer_head *bh;
6568 struct ocfs2_extent_list *el;
6569 struct ocfs2_extent_rec *rec;
6570
6571 *delete_start = 0;
6572
6573 while (index >= 0) {
6574 bh = path->p_node[index].bh;
6575 el = path->p_node[index].el;
6576
6577 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6578 index, (unsigned long long)bh->b_blocknr);
6579
6580 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6581
6582 if (index !=
6583 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6584 ocfs2_error(inode->i_sb,
6585 "Inode %lu has invalid ext. block %llu",
6586 inode->i_ino,
6587 (unsigned long long)bh->b_blocknr);
6588 ret = -EROFS;
6589 goto out;
6590 }
6591
6592find_tail_record:
6593 i = le16_to_cpu(el->l_next_free_rec) - 1;
6594 rec = &el->l_recs[i];
6595
6596 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6597 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006598 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006599 (unsigned long long)le64_to_cpu(rec->e_blkno),
6600 le16_to_cpu(el->l_next_free_rec));
6601
Mark Fashehe48edee2007-03-07 16:46:57 -08006602 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006603
6604 if (le16_to_cpu(el->l_tree_depth) == 0) {
6605 /*
6606 * If the leaf block contains a single empty
6607 * extent and no records, we can just remove
6608 * the block.
6609 */
6610 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6611 memset(rec, 0,
6612 sizeof(struct ocfs2_extent_rec));
6613 el->l_next_free_rec = cpu_to_le16(0);
6614
6615 goto delete;
6616 }
6617
6618 /*
6619 * Remove any empty extents by shifting things
6620 * left. That should make life much easier on
6621 * the code below. This condition is rare
6622 * enough that we shouldn't see a performance
6623 * hit.
6624 */
6625 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6626 le16_add_cpu(&el->l_next_free_rec, -1);
6627
6628 for(i = 0;
6629 i < le16_to_cpu(el->l_next_free_rec); i++)
6630 el->l_recs[i] = el->l_recs[i + 1];
6631
6632 memset(&el->l_recs[i], 0,
6633 sizeof(struct ocfs2_extent_rec));
6634
6635 /*
6636 * We've modified our extent list. The
6637 * simplest way to handle this change
6638 * is to being the search from the
6639 * start again.
6640 */
6641 goto find_tail_record;
6642 }
6643
Mark Fashehe48edee2007-03-07 16:46:57 -08006644 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006645
6646 /*
6647 * We'll use "new_edge" on our way back up the
6648 * tree to know what our rightmost cpos is.
6649 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006650 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006651 new_edge += le32_to_cpu(rec->e_cpos);
6652
6653 /*
6654 * The caller will use this to delete data blocks.
6655 */
6656 *delete_start = le64_to_cpu(rec->e_blkno)
6657 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006658 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006659
6660 /*
6661 * If it's now empty, remove this record.
6662 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006663 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006664 memset(rec, 0,
6665 sizeof(struct ocfs2_extent_rec));
6666 le16_add_cpu(&el->l_next_free_rec, -1);
6667 }
6668 } else {
6669 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6670 memset(rec, 0,
6671 sizeof(struct ocfs2_extent_rec));
6672 le16_add_cpu(&el->l_next_free_rec, -1);
6673
6674 goto delete;
6675 }
6676
6677 /* Can this actually happen? */
6678 if (le16_to_cpu(el->l_next_free_rec) == 0)
6679 goto delete;
6680
6681 /*
6682 * We never actually deleted any clusters
6683 * because our leaf was empty. There's no
6684 * reason to adjust the rightmost edge then.
6685 */
6686 if (new_edge == 0)
6687 goto delete;
6688
Mark Fashehe48edee2007-03-07 16:46:57 -08006689 rec->e_int_clusters = cpu_to_le32(new_edge);
6690 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006691 -le32_to_cpu(rec->e_cpos));
6692
6693 /*
6694 * A deleted child record should have been
6695 * caught above.
6696 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006697 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006698 }
6699
6700delete:
6701 ret = ocfs2_journal_dirty(handle, bh);
6702 if (ret) {
6703 mlog_errno(ret);
6704 goto out;
6705 }
6706
6707 mlog(0, "extent list container %llu, after: record %d: "
6708 "(%u, %u, %llu), next = %u.\n",
6709 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006710 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006711 (unsigned long long)le64_to_cpu(rec->e_blkno),
6712 le16_to_cpu(el->l_next_free_rec));
6713
6714 /*
6715 * We must be careful to only attempt delete of an
6716 * extent block (and not the root inode block).
6717 */
6718 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6719 struct ocfs2_extent_block *eb =
6720 (struct ocfs2_extent_block *)bh->b_data;
6721
6722 /*
6723 * Save this for use when processing the
6724 * parent block.
6725 */
6726 deleted_eb = le64_to_cpu(eb->h_blkno);
6727
6728 mlog(0, "deleting this extent block.\n");
6729
Joel Becker8cb471e2009-02-10 20:00:41 -08006730 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006731
Mark Fashehe48edee2007-03-07 16:46:57 -08006732 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006733 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6734 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6735
Mark Fasheh59a5e412007-06-22 15:52:36 -07006736 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6737 /* An error here is not fatal. */
6738 if (ret < 0)
6739 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006740 } else {
6741 deleted_eb = 0;
6742 }
6743
6744 index--;
6745 }
6746
6747 ret = 0;
6748out:
6749 return ret;
6750}
6751
Mark Fashehccd979b2005-12-15 14:31:24 -08006752static int ocfs2_do_truncate(struct ocfs2_super *osb,
6753 unsigned int clusters_to_del,
6754 struct inode *inode,
6755 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006756 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006757 struct ocfs2_truncate_context *tc,
6758 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006759{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006760 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006761 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006762 struct ocfs2_extent_block *last_eb = NULL;
6763 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006764 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006765 u64 delete_blk = 0;
6766
6767 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6768
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006769 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006770 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006771 if (status < 0) {
6772 mlog_errno(status);
6773 goto bail;
6774 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006775
Mark Fashehdcd05382007-01-16 11:32:23 -08006776 /*
6777 * Each component will be touched, so we might as well journal
6778 * here to avoid having to handle errors later.
6779 */
Joel Becker0cf2f762009-02-12 16:41:25 -08006780 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006781 if (status < 0) {
6782 mlog_errno(status);
6783 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006784 }
6785
6786 if (last_eb_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08006787 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07006788 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08006789 if (status < 0) {
6790 mlog_errno(status);
6791 goto bail;
6792 }
6793
6794 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6795 }
6796
6797 el = &(fe->id2.i_list);
6798
6799 /*
6800 * Lower levels depend on this never happening, but it's best
6801 * to check it up here before changing the tree.
6802 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006803 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006804 ocfs2_error(inode->i_sb,
6805 "Inode %lu has an empty extent record, depth %u\n",
6806 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006807 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006808 goto bail;
6809 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006810
Jan Karaa90714c2008-10-09 19:38:40 +02006811 vfs_dq_free_space_nodirty(inode,
6812 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006813 spin_lock(&OCFS2_I(inode)->ip_lock);
6814 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6815 clusters_to_del;
6816 spin_unlock(&OCFS2_I(inode)->ip_lock);
6817 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006818 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006819
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006820 status = ocfs2_trim_tree(inode, path, handle, tc,
6821 clusters_to_del, &delete_blk);
6822 if (status) {
6823 mlog_errno(status);
6824 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006825 }
6826
Mark Fashehdcd05382007-01-16 11:32:23 -08006827 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006828 /* trunc to zero is a special case. */
6829 el->l_tree_depth = 0;
6830 fe->i_last_eb_blk = 0;
6831 } else if (last_eb)
6832 fe->i_last_eb_blk = last_eb->h_blkno;
6833
6834 status = ocfs2_journal_dirty(handle, fe_bh);
6835 if (status < 0) {
6836 mlog_errno(status);
6837 goto bail;
6838 }
6839
6840 if (last_eb) {
6841 /* If there will be a new last extent block, then by
6842 * definition, there cannot be any leaves to the right of
6843 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006844 last_eb->h_next_leaf_blk = 0;
6845 status = ocfs2_journal_dirty(handle, last_eb_bh);
6846 if (status < 0) {
6847 mlog_errno(status);
6848 goto bail;
6849 }
6850 }
6851
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006852 if (delete_blk) {
6853 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6854 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006855 if (status < 0) {
6856 mlog_errno(status);
6857 goto bail;
6858 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006859 }
6860 status = 0;
6861bail:
Tao Ma60e2ec42009-08-12 14:42:47 +08006862 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006863 mlog_exit(status);
6864 return status;
6865}
6866
Joel Becker2b4e30f2008-09-03 20:03:41 -07006867static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006868{
6869 set_buffer_uptodate(bh);
6870 mark_buffer_dirty(bh);
6871 return 0;
6872}
6873
Mark Fasheh1d410a62007-09-07 14:20:45 -07006874static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6875 unsigned int from, unsigned int to,
6876 struct page *page, int zero, u64 *phys)
6877{
6878 int ret, partial = 0;
6879
6880 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6881 if (ret)
6882 mlog_errno(ret);
6883
6884 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006885 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006886
6887 /*
6888 * Need to set the buffers we zero'd into uptodate
6889 * here if they aren't - ocfs2_map_page_blocks()
6890 * might've skipped some
6891 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006892 ret = walk_page_buffers(handle, page_buffers(page),
6893 from, to, &partial,
6894 ocfs2_zero_func);
6895 if (ret < 0)
6896 mlog_errno(ret);
6897 else if (ocfs2_should_order_data(inode)) {
6898 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006899 if (ret < 0)
6900 mlog_errno(ret);
6901 }
6902
6903 if (!partial)
6904 SetPageUptodate(page);
6905
6906 flush_dcache_page(page);
6907}
6908
Mark Fasheh35edec12007-07-06 14:41:18 -07006909static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6910 loff_t end, struct page **pages,
6911 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006912{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006913 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006914 struct page *page;
6915 unsigned int from, to = PAGE_CACHE_SIZE;
6916 struct super_block *sb = inode->i_sb;
6917
6918 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6919
6920 if (numpages == 0)
6921 goto out;
6922
Mark Fasheh35edec12007-07-06 14:41:18 -07006923 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006924 for(i = 0; i < numpages; i++) {
6925 page = pages[i];
6926
Mark Fasheh35edec12007-07-06 14:41:18 -07006927 from = start & (PAGE_CACHE_SIZE - 1);
6928 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6929 to = end & (PAGE_CACHE_SIZE - 1);
6930
Mark Fasheh60b11392007-02-16 11:46:50 -08006931 BUG_ON(from > PAGE_CACHE_SIZE);
6932 BUG_ON(to > PAGE_CACHE_SIZE);
6933
Mark Fasheh1d410a62007-09-07 14:20:45 -07006934 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6935 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006936
Mark Fasheh35edec12007-07-06 14:41:18 -07006937 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006938 }
6939out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006940 if (pages)
6941 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006942}
6943
Mark Fasheh35edec12007-07-06 14:41:18 -07006944static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006945 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006946{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006947 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006948 struct super_block *sb = inode->i_sb;
6949 struct address_space *mapping = inode->i_mapping;
6950 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006951 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006952
Mark Fasheh35edec12007-07-06 14:41:18 -07006953 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006954
Mark Fasheh35edec12007-07-06 14:41:18 -07006955 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6956 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6957
Mark Fasheh1d410a62007-09-07 14:20:45 -07006958 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006959 last_page_bytes = PAGE_ALIGN(end);
6960 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006961 do {
6962 pages[numpages] = grab_cache_page(mapping, index);
6963 if (!pages[numpages]) {
6964 ret = -ENOMEM;
6965 mlog_errno(ret);
6966 goto out;
6967 }
6968
6969 numpages++;
6970 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006971 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006972
6973out:
6974 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006975 if (pages)
6976 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006977 numpages = 0;
6978 }
6979
6980 *num = numpages;
6981
6982 return ret;
6983}
6984
6985/*
6986 * Zero the area past i_size but still within an allocated
6987 * cluster. This avoids exposing nonzero data on subsequent file
6988 * extends.
6989 *
6990 * We need to call this before i_size is updated on the inode because
6991 * otherwise block_write_full_page() will skip writeout of pages past
6992 * i_size. The new_i_size parameter is passed for this reason.
6993 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006994int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6995 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006996{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006997 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006998 struct page **pages = NULL;
6999 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07007000 unsigned int ext_flags;
7001 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08007002
7003 /*
7004 * File systems which don't support sparse files zero on every
7005 * extend.
7006 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07007007 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08007008 return 0;
7009
Mark Fasheh1d410a62007-09-07 14:20:45 -07007010 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08007011 sizeof(struct page *), GFP_NOFS);
7012 if (pages == NULL) {
7013 ret = -ENOMEM;
7014 mlog_errno(ret);
7015 goto out;
7016 }
7017
Mark Fasheh1d410a62007-09-07 14:20:45 -07007018 if (range_start == range_end)
7019 goto out;
7020
7021 ret = ocfs2_extent_map_get_blocks(inode,
7022 range_start >> sb->s_blocksize_bits,
7023 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08007024 if (ret) {
7025 mlog_errno(ret);
7026 goto out;
7027 }
7028
Mark Fasheh1d410a62007-09-07 14:20:45 -07007029 /*
7030 * Tail is a hole, or is marked unwritten. In either case, we
7031 * can count on read and write to return/push zero's.
7032 */
7033 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08007034 goto out;
7035
Mark Fasheh1d410a62007-09-07 14:20:45 -07007036 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7037 &numpages);
7038 if (ret) {
7039 mlog_errno(ret);
7040 goto out;
7041 }
7042
Mark Fasheh35edec12007-07-06 14:41:18 -07007043 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7044 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08007045
7046 /*
7047 * Initiate writeout of the pages we zero'd here. We don't
7048 * wait on them - the truncate_inode_pages() call later will
7049 * do that for us.
7050 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007051 ret = do_sync_mapping_range(inode->i_mapping, range_start,
7052 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08007053 if (ret)
7054 mlog_errno(ret);
7055
7056out:
7057 if (pages)
7058 kfree(pages);
7059
7060 return ret;
7061}
7062
Tiger Yangfdd77702008-08-18 17:08:55 +08007063static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7064 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007065{
7066 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08007067 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007068
Tiger Yangfdd77702008-08-18 17:08:55 +08007069 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7070 memset(&di->id2, 0, blocksize -
7071 offsetof(struct ocfs2_dinode, id2) -
7072 xattrsize);
7073 else
7074 memset(&di->id2, 0, blocksize -
7075 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007076}
7077
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007078void ocfs2_dinode_new_extent_list(struct inode *inode,
7079 struct ocfs2_dinode *di)
7080{
Tiger Yangfdd77702008-08-18 17:08:55 +08007081 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007082 di->id2.i_list.l_tree_depth = 0;
7083 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08007084 di->id2.i_list.l_count = cpu_to_le16(
7085 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007086}
7087
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007088void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7089{
7090 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7091 struct ocfs2_inline_data *idata = &di->id2.i_data;
7092
7093 spin_lock(&oi->ip_lock);
7094 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7095 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7096 spin_unlock(&oi->ip_lock);
7097
7098 /*
7099 * We clear the entire i_data structure here so that all
7100 * fields can be properly initialized.
7101 */
Tiger Yangfdd77702008-08-18 17:08:55 +08007102 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007103
Tiger Yangfdd77702008-08-18 17:08:55 +08007104 idata->id_count = cpu_to_le16(
7105 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007106}
7107
7108int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7109 struct buffer_head *di_bh)
7110{
7111 int ret, i, has_data, num_pages = 0;
7112 handle_t *handle;
7113 u64 uninitialized_var(block);
7114 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7115 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7116 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007117 struct ocfs2_alloc_context *data_ac = NULL;
7118 struct page **pages = NULL;
7119 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07007120 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02007121 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007122
7123 has_data = i_size_read(inode) ? 1 : 0;
7124
7125 if (has_data) {
7126 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7127 sizeof(struct page *), GFP_NOFS);
7128 if (pages == NULL) {
7129 ret = -ENOMEM;
7130 mlog_errno(ret);
7131 goto out;
7132 }
7133
7134 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7135 if (ret) {
7136 mlog_errno(ret);
7137 goto out;
7138 }
7139 }
7140
Jan Karaa90714c2008-10-09 19:38:40 +02007141 handle = ocfs2_start_trans(osb,
7142 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007143 if (IS_ERR(handle)) {
7144 ret = PTR_ERR(handle);
7145 mlog_errno(ret);
7146 goto out_unlock;
7147 }
7148
Joel Becker0cf2f762009-02-12 16:41:25 -08007149 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007150 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007151 if (ret) {
7152 mlog_errno(ret);
7153 goto out_commit;
7154 }
7155
7156 if (has_data) {
7157 u32 bit_off, num;
7158 unsigned int page_end;
7159 u64 phys;
7160
Jan Karaa90714c2008-10-09 19:38:40 +02007161 if (vfs_dq_alloc_space_nodirty(inode,
7162 ocfs2_clusters_to_bytes(osb->sb, 1))) {
7163 ret = -EDQUOT;
7164 goto out_commit;
7165 }
7166 did_quota = 1;
7167
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007168 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7169 &num);
7170 if (ret) {
7171 mlog_errno(ret);
7172 goto out_commit;
7173 }
7174
7175 /*
7176 * Save two copies, one for insert, and one that can
7177 * be changed by ocfs2_map_and_dirty_page() below.
7178 */
7179 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7180
7181 /*
7182 * Non sparse file systems zero on extend, so no need
7183 * to do that now.
7184 */
7185 if (!ocfs2_sparse_alloc(osb) &&
7186 PAGE_CACHE_SIZE < osb->s_clustersize)
7187 end = PAGE_CACHE_SIZE;
7188
7189 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7190 if (ret) {
7191 mlog_errno(ret);
7192 goto out_commit;
7193 }
7194
7195 /*
7196 * This should populate the 1st page for us and mark
7197 * it up to date.
7198 */
7199 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7200 if (ret) {
7201 mlog_errno(ret);
7202 goto out_commit;
7203 }
7204
7205 page_end = PAGE_CACHE_SIZE;
7206 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7207 page_end = osb->s_clustersize;
7208
7209 for (i = 0; i < num_pages; i++)
7210 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7211 pages[i], i > 0, &phys);
7212 }
7213
7214 spin_lock(&oi->ip_lock);
7215 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7216 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7217 spin_unlock(&oi->ip_lock);
7218
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007219 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007220
7221 ocfs2_journal_dirty(handle, di_bh);
7222
7223 if (has_data) {
7224 /*
7225 * An error at this point should be extremely rare. If
7226 * this proves to be false, we could always re-build
7227 * the in-inode data from our pages.
7228 */
Joel Becker8d6220d2008-08-22 12:46:09 -07007229 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07007230 ret = ocfs2_insert_extent(osb, handle, inode, &et,
7231 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007232 if (ret) {
7233 mlog_errno(ret);
7234 goto out_commit;
7235 }
7236
7237 inode->i_blocks = ocfs2_inode_sector_count(inode);
7238 }
7239
7240out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007241 if (ret < 0 && did_quota)
7242 vfs_dq_free_space_nodirty(inode,
7243 ocfs2_clusters_to_bytes(osb->sb, 1));
7244
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007245 ocfs2_commit_trans(osb, handle);
7246
7247out_unlock:
7248 if (data_ac)
7249 ocfs2_free_alloc_context(data_ac);
7250
7251out:
7252 if (pages) {
7253 ocfs2_unlock_and_free_pages(pages, num_pages);
7254 kfree(pages);
7255 }
7256
7257 return ret;
7258}
7259
Mark Fashehccd979b2005-12-15 14:31:24 -08007260/*
7261 * It is expected, that by the time you call this function,
7262 * inode->i_size and fe->i_size have been adjusted.
7263 *
7264 * WARNING: This will kfree the truncate context
7265 */
7266int ocfs2_commit_truncate(struct ocfs2_super *osb,
7267 struct inode *inode,
7268 struct buffer_head *fe_bh,
7269 struct ocfs2_truncate_context *tc)
7270{
7271 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08007272 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08007273 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07007274 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007275 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08007276 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08007277 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007278
7279 mlog_entry_void();
7280
Mark Fashehdcd05382007-01-16 11:32:23 -08007281 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007282 i_size_read(inode));
7283
Joel Becker13723d02008-10-17 19:25:01 -07007284 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7285 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08007286 if (!path) {
7287 status = -ENOMEM;
7288 mlog_errno(status);
7289 goto bail;
7290 }
Mark Fasheh83418972007-04-23 18:53:12 -07007291
7292 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7293
Mark Fashehccd979b2005-12-15 14:31:24 -08007294start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007295 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007296 * Check that we still have allocation to delete.
7297 */
7298 if (OCFS2_I(inode)->ip_clusters == 0) {
7299 status = 0;
7300 goto bail;
7301 }
7302
7303 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007304 * Truncate always works against the rightmost tree branch.
7305 */
Joel Beckerfacdb772009-02-12 18:08:48 -08007306 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
Mark Fashehdcd05382007-01-16 11:32:23 -08007307 if (status) {
7308 mlog_errno(status);
7309 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007310 }
7311
Mark Fashehdcd05382007-01-16 11:32:23 -08007312 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7313 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7314
7315 /*
7316 * By now, el will point to the extent list on the bottom most
7317 * portion of this tree. Only the tail record is considered in
7318 * each pass.
7319 *
7320 * We handle the following cases, in order:
7321 * - empty extent: delete the remaining branch
7322 * - remove the entire record
7323 * - remove a partial record
7324 * - no record needs to be removed (truncate has completed)
7325 */
7326 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007327 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7328 ocfs2_error(inode->i_sb,
7329 "Inode %llu has empty extent block at %llu\n",
7330 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7331 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7332 status = -EROFS;
7333 goto bail;
7334 }
7335
Mark Fashehccd979b2005-12-15 14:31:24 -08007336 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08007337 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08007338 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007339 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7340 clusters_to_del = 0;
7341 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007342 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007343 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007344 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08007345 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08007346 new_highest_cpos;
7347 } else {
7348 status = 0;
7349 goto bail;
7350 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007351
Mark Fashehdcd05382007-01-16 11:32:23 -08007352 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7353 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7354
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007355 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007356 tl_sem = 1;
7357 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7358 * record is free for use. If there isn't any, we flush to get
7359 * an empty truncate log. */
7360 if (ocfs2_truncate_log_needs_flush(osb)) {
7361 status = __ocfs2_flush_truncate_log(osb);
7362 if (status < 0) {
7363 mlog_errno(status);
7364 goto bail;
7365 }
7366 }
7367
7368 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007369 (struct ocfs2_dinode *)fe_bh->b_data,
7370 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007371 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007372 if (IS_ERR(handle)) {
7373 status = PTR_ERR(handle);
7374 handle = NULL;
7375 mlog_errno(status);
7376 goto bail;
7377 }
7378
Mark Fashehdcd05382007-01-16 11:32:23 -08007379 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7380 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007381 if (status < 0) {
7382 mlog_errno(status);
7383 goto bail;
7384 }
7385
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007386 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007387 tl_sem = 0;
7388
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007389 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007390 handle = NULL;
7391
Mark Fashehdcd05382007-01-16 11:32:23 -08007392 ocfs2_reinit_path(path, 1);
7393
7394 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007395 * The check above will catch the case where we've truncated
7396 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007397 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007398 goto start;
7399
Mark Fashehccd979b2005-12-15 14:31:24 -08007400bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007401
7402 ocfs2_schedule_truncate_log_flush(osb, 1);
7403
7404 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007405 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007406
7407 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007408 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007409
Mark Fasheh59a5e412007-06-22 15:52:36 -07007410 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7411
Mark Fashehdcd05382007-01-16 11:32:23 -08007412 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007413
7414 /* This will drop the ext_alloc cluster lock for us */
7415 ocfs2_free_truncate_context(tc);
7416
7417 mlog_exit(status);
7418 return status;
7419}
7420
Mark Fashehccd979b2005-12-15 14:31:24 -08007421/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007422 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007423 */
7424int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7425 struct inode *inode,
7426 struct buffer_head *fe_bh,
7427 struct ocfs2_truncate_context **tc)
7428{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007429 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007430 unsigned int new_i_clusters;
7431 struct ocfs2_dinode *fe;
7432 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007433 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007434
7435 mlog_entry_void();
7436
7437 *tc = NULL;
7438
7439 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7440 i_size_read(inode));
7441 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7442
7443 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007444 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7445 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007446
Robert P. J. Daycd861282006-12-13 00:34:52 -08007447 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007448 if (!(*tc)) {
7449 status = -ENOMEM;
7450 mlog_errno(status);
7451 goto bail;
7452 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007453 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007454
Mark Fashehccd979b2005-12-15 14:31:24 -08007455 if (fe->id2.i_list.l_tree_depth) {
Joel Becker3d03a302009-02-12 17:49:26 -08007456 status = ocfs2_read_extent_block(INODE_CACHE(inode),
Joel Becker5e965812008-11-13 14:49:16 -08007457 le64_to_cpu(fe->i_last_eb_blk),
7458 &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007459 if (status < 0) {
7460 mlog_errno(status);
7461 goto bail;
7462 }
7463 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007464 }
7465
7466 (*tc)->tc_last_eb_bh = last_eb_bh;
7467
Mark Fashehccd979b2005-12-15 14:31:24 -08007468 status = 0;
7469bail:
7470 if (status < 0) {
7471 if (*tc)
7472 ocfs2_free_truncate_context(*tc);
7473 *tc = NULL;
7474 }
7475 mlog_exit_void();
7476 return status;
7477}
7478
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007479/*
7480 * 'start' is inclusive, 'end' is not.
7481 */
7482int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7483 unsigned int start, unsigned int end, int trunc)
7484{
7485 int ret;
7486 unsigned int numbytes;
7487 handle_t *handle;
7488 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7489 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7490 struct ocfs2_inline_data *idata = &di->id2.i_data;
7491
7492 if (end > i_size_read(inode))
7493 end = i_size_read(inode);
7494
7495 BUG_ON(start >= end);
7496
7497 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7498 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7499 !ocfs2_supports_inline_data(osb)) {
7500 ocfs2_error(inode->i_sb,
7501 "Inline data flags for inode %llu don't agree! "
7502 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7503 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7504 le16_to_cpu(di->i_dyn_features),
7505 OCFS2_I(inode)->ip_dyn_features,
7506 osb->s_feature_incompat);
7507 ret = -EROFS;
7508 goto out;
7509 }
7510
7511 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7512 if (IS_ERR(handle)) {
7513 ret = PTR_ERR(handle);
7514 mlog_errno(ret);
7515 goto out;
7516 }
7517
Joel Becker0cf2f762009-02-12 16:41:25 -08007518 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007519 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007520 if (ret) {
7521 mlog_errno(ret);
7522 goto out_commit;
7523 }
7524
7525 numbytes = end - start;
7526 memset(idata->id_data + start, 0, numbytes);
7527
7528 /*
7529 * No need to worry about the data page here - it's been
7530 * truncated already and inline data doesn't need it for
7531 * pushing zero's to disk, so we'll let readpage pick it up
7532 * later.
7533 */
7534 if (trunc) {
7535 i_size_write(inode, start);
7536 di->i_size = cpu_to_le64(start);
7537 }
7538
7539 inode->i_blocks = ocfs2_inode_sector_count(inode);
7540 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7541
7542 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7543 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7544
7545 ocfs2_journal_dirty(handle, di_bh);
7546
7547out_commit:
7548 ocfs2_commit_trans(osb, handle);
7549
7550out:
7551 return ret;
7552}
7553
Mark Fashehccd979b2005-12-15 14:31:24 -08007554static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7555{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007556 /*
7557 * The caller is responsible for completing deallocation
7558 * before freeing the context.
7559 */
7560 if (tc->tc_dealloc.c_first_suballocator != NULL)
7561 mlog(ML_NOTICE,
7562 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007563
Mark Fasheha81cb882008-10-07 14:25:16 -07007564 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007565
7566 kfree(tc);
7567}