blob: d5dffcfa192a4654ccfc443ec149469728077127 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * alloc.c
5 *
6 * Extent allocs and frees
7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/fs.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/highmem.h>
Mark Fasheh60b11392007-02-16 11:46:50 -080030#include <linux/swap.h>
Jan Karaa90714c2008-10-09 19:38:40 +020031#include <linux/quotaops.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080032
33#define MLOG_MASK_PREFIX ML_DISK_ALLOC
34#include <cluster/masklog.h>
35
36#include "ocfs2.h"
37
38#include "alloc.h"
Mark Fasheh60b11392007-02-16 11:46:50 -080039#include "aops.h"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070040#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080041#include "dlmglue.h"
42#include "extent_map.h"
43#include "inode.h"
44#include "journal.h"
45#include "localalloc.h"
46#include "suballoc.h"
47#include "sysfile.h"
48#include "file.h"
49#include "super.h"
50#include "uptodate.h"
Joel Becker2a50a742008-12-09 14:24:33 -080051#include "xattr.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080052
53#include "buffer_head_io.h"
54
Tao Mae7d4cb62008-08-18 17:38:44 +080055
Joel Becker1625f8a2008-08-21 17:11:10 -070056/*
57 * Operations for a specific extent tree type.
58 *
59 * To implement an on-disk btree (extent tree) type in ocfs2, add
60 * an ocfs2_extent_tree_operations structure and the matching
Joel Becker8d6220d2008-08-22 12:46:09 -070061 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
Joel Becker1625f8a2008-08-21 17:11:10 -070062 * for the allocation portion of the extent tree.
63 */
Tao Mae7d4cb62008-08-18 17:38:44 +080064struct ocfs2_extent_tree_operations {
Joel Becker1625f8a2008-08-21 17:11:10 -070065 /*
66 * last_eb_blk is the block number of the right most leaf extent
67 * block. Most on-disk structures containing an extent tree store
68 * this value for fast access. The ->eo_set_last_eb_blk() and
69 * ->eo_get_last_eb_blk() operations access this value. They are
70 * both required.
71 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070072 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
73 u64 blkno);
74 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -070075
76 /*
77 * The on-disk structure usually keeps track of how many total
78 * clusters are stored in this extent tree. This function updates
79 * that value. new_clusters is the delta, and must be
80 * added to the total. Required.
81 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070082 void (*eo_update_clusters)(struct inode *inode,
83 struct ocfs2_extent_tree *et,
84 u32 new_clusters);
Joel Becker1625f8a2008-08-21 17:11:10 -070085
86 /*
87 * If ->eo_insert_check() exists, it is called before rec is
88 * inserted into the extent tree. It is optional.
89 */
Joel Becker1e61ee72008-08-20 18:32:45 -070090 int (*eo_insert_check)(struct inode *inode,
91 struct ocfs2_extent_tree *et,
92 struct ocfs2_extent_rec *rec);
Joel Becker35dc0aa2008-08-20 16:25:06 -070093 int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
Joel Becker0ce10102008-08-20 17:19:50 -070094
Joel Becker1625f8a2008-08-21 17:11:10 -070095 /*
96 * --------------------------------------------------------------
97 * The remaining are internal to ocfs2_extent_tree and don't have
98 * accessor functions
99 */
100
101 /*
102 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
103 * It is required.
104 */
Joel Becker0ce10102008-08-20 17:19:50 -0700105 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -0700106
107 /*
108 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
109 * it exists. If it does not, et->et_max_leaf_clusters is set
110 * to 0 (unlimited). Optional.
111 */
Joel Becker943cced2008-08-20 17:31:10 -0700112 void (*eo_fill_max_leaf_clusters)(struct inode *inode,
113 struct ocfs2_extent_tree *et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800114};
115
Joel Beckerf99b9b72008-08-20 19:36:33 -0700116
117/*
118 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
119 * in the methods.
120 */
121static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
122static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
123 u64 blkno);
124static void ocfs2_dinode_update_clusters(struct inode *inode,
125 struct ocfs2_extent_tree *et,
126 u32 clusters);
127static int ocfs2_dinode_insert_check(struct inode *inode,
128 struct ocfs2_extent_tree *et,
129 struct ocfs2_extent_rec *rec);
130static int ocfs2_dinode_sanity_check(struct inode *inode,
131 struct ocfs2_extent_tree *et);
132static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
133static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
134 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
135 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
136 .eo_update_clusters = ocfs2_dinode_update_clusters,
137 .eo_insert_check = ocfs2_dinode_insert_check,
138 .eo_sanity_check = ocfs2_dinode_sanity_check,
139 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
Tao Mae7d4cb62008-08-18 17:38:44 +0800140};
141
142static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
143 u64 blkno)
144{
Joel Beckerea5efa12008-08-20 16:57:27 -0700145 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800146
Joel Beckerf99b9b72008-08-20 19:36:33 -0700147 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800148 di->i_last_eb_blk = cpu_to_le64(blkno);
149}
150
151static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
152{
Joel Beckerea5efa12008-08-20 16:57:27 -0700153 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800154
Joel Beckerf99b9b72008-08-20 19:36:33 -0700155 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800156 return le64_to_cpu(di->i_last_eb_blk);
157}
158
159static void ocfs2_dinode_update_clusters(struct inode *inode,
160 struct ocfs2_extent_tree *et,
161 u32 clusters)
162{
Joel Beckerea5efa12008-08-20 16:57:27 -0700163 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800164
165 le32_add_cpu(&di->i_clusters, clusters);
166 spin_lock(&OCFS2_I(inode)->ip_lock);
167 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
168 spin_unlock(&OCFS2_I(inode)->ip_lock);
169}
170
Joel Becker1e61ee72008-08-20 18:32:45 -0700171static int ocfs2_dinode_insert_check(struct inode *inode,
172 struct ocfs2_extent_tree *et,
173 struct ocfs2_extent_rec *rec)
174{
175 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
176
177 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
178 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
Tao Ma74e77eb2009-03-12 06:24:23 +0800179 (OCFS2_I(inode)->ip_clusters !=
180 le32_to_cpu(rec->e_cpos)),
Joel Becker1e61ee72008-08-20 18:32:45 -0700181 "Device %s, asking for sparse allocation: inode %llu, "
182 "cpos %u, clusters %u\n",
183 osb->dev_str,
184 (unsigned long long)OCFS2_I(inode)->ip_blkno,
185 rec->e_cpos,
186 OCFS2_I(inode)->ip_clusters);
187
188 return 0;
189}
190
Tao Mae7d4cb62008-08-18 17:38:44 +0800191static int ocfs2_dinode_sanity_check(struct inode *inode,
192 struct ocfs2_extent_tree *et)
193{
Joel Becker10995aa2008-11-13 14:49:12 -0800194 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800195
Joel Beckerf99b9b72008-08-20 19:36:33 -0700196 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Joel Becker10995aa2008-11-13 14:49:12 -0800197 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
Tao Mae7d4cb62008-08-18 17:38:44 +0800198
Joel Becker10995aa2008-11-13 14:49:12 -0800199 return 0;
Tao Mae7d4cb62008-08-18 17:38:44 +0800200}
201
Joel Beckerf99b9b72008-08-20 19:36:33 -0700202static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
203{
204 struct ocfs2_dinode *di = et->et_object;
205
206 et->et_root_el = &di->id2.i_list;
207}
208
Tao Mae7d4cb62008-08-18 17:38:44 +0800209
Joel Becker0ce10102008-08-20 17:19:50 -0700210static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
211{
Joel Becker2a50a742008-12-09 14:24:33 -0800212 struct ocfs2_xattr_value_buf *vb = et->et_object;
Joel Becker0ce10102008-08-20 17:19:50 -0700213
Joel Becker2a50a742008-12-09 14:24:33 -0800214 et->et_root_el = &vb->vb_xv->xr_list;
Joel Becker0ce10102008-08-20 17:19:50 -0700215}
216
Tao Maf56654c2008-08-18 17:38:48 +0800217static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
218 u64 blkno)
219{
Joel Becker2a50a742008-12-09 14:24:33 -0800220 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800221
Joel Becker2a50a742008-12-09 14:24:33 -0800222 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
Tao Maf56654c2008-08-18 17:38:48 +0800223}
224
225static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
226{
Joel Becker2a50a742008-12-09 14:24:33 -0800227 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800228
Joel Becker2a50a742008-12-09 14:24:33 -0800229 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
Tao Maf56654c2008-08-18 17:38:48 +0800230}
231
232static void ocfs2_xattr_value_update_clusters(struct inode *inode,
233 struct ocfs2_extent_tree *et,
234 u32 clusters)
235{
Joel Becker2a50a742008-12-09 14:24:33 -0800236 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800237
Joel Becker2a50a742008-12-09 14:24:33 -0800238 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800239}
240
Joel Becker1a09f552008-08-20 17:44:24 -0700241static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700242 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
243 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
244 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700245 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800246};
247
Joel Becker0ce10102008-08-20 17:19:50 -0700248static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
249{
250 struct ocfs2_xattr_block *xb = et->et_object;
251
252 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
253}
254
Joel Becker943cced2008-08-20 17:31:10 -0700255static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode *inode,
256 struct ocfs2_extent_tree *et)
257{
258 et->et_max_leaf_clusters =
259 ocfs2_clusters_for_bytes(inode->i_sb,
260 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
261}
262
Tao Maba492612008-08-18 17:38:49 +0800263static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
264 u64 blkno)
265{
Joel Beckerea5efa12008-08-20 16:57:27 -0700266 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800267 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
268
269 xt->xt_last_eb_blk = cpu_to_le64(blkno);
270}
271
272static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
273{
Joel Beckerea5efa12008-08-20 16:57:27 -0700274 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800275 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
276
277 return le64_to_cpu(xt->xt_last_eb_blk);
278}
279
280static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
281 struct ocfs2_extent_tree *et,
282 u32 clusters)
283{
Joel Beckerea5efa12008-08-20 16:57:27 -0700284 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800285
286 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
287}
288
Tao Maba492612008-08-18 17:38:49 +0800289static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700290 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
291 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
292 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700293 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
Joel Becker943cced2008-08-20 17:31:10 -0700294 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
Tao Maba492612008-08-18 17:38:49 +0800295};
296
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800297static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
298 u64 blkno)
299{
300 struct ocfs2_dx_root_block *dx_root = et->et_object;
301
302 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
303}
304
305static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
306{
307 struct ocfs2_dx_root_block *dx_root = et->et_object;
308
309 return le64_to_cpu(dx_root->dr_last_eb_blk);
310}
311
312static void ocfs2_dx_root_update_clusters(struct inode *inode,
313 struct ocfs2_extent_tree *et,
314 u32 clusters)
315{
316 struct ocfs2_dx_root_block *dx_root = et->et_object;
317
318 le32_add_cpu(&dx_root->dr_clusters, clusters);
319}
320
321static int ocfs2_dx_root_sanity_check(struct inode *inode,
322 struct ocfs2_extent_tree *et)
323{
324 struct ocfs2_dx_root_block *dx_root = et->et_object;
325
326 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
327
328 return 0;
329}
330
331static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
332{
333 struct ocfs2_dx_root_block *dx_root = et->et_object;
334
335 et->et_root_el = &dx_root->dr_list;
336}
337
338static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
339 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
340 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
341 .eo_update_clusters = ocfs2_dx_root_update_clusters,
342 .eo_sanity_check = ocfs2_dx_root_sanity_check,
343 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
344};
345
Joel Becker8d6220d2008-08-22 12:46:09 -0700346static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
347 struct inode *inode,
348 struct buffer_head *bh,
Joel Becker13723d02008-10-17 19:25:01 -0700349 ocfs2_journal_access_func access,
Joel Becker8d6220d2008-08-22 12:46:09 -0700350 void *obj,
351 struct ocfs2_extent_tree_operations *ops)
Tao Mae7d4cb62008-08-18 17:38:44 +0800352{
Joel Becker1a09f552008-08-20 17:44:24 -0700353 et->et_ops = ops;
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700354 et->et_root_bh = bh;
Joel Becker13723d02008-10-17 19:25:01 -0700355 et->et_root_journal_access = access;
Joel Beckerea5efa12008-08-20 16:57:27 -0700356 if (!obj)
357 obj = (void *)bh->b_data;
358 et->et_object = obj;
Tao Mae7d4cb62008-08-18 17:38:44 +0800359
Joel Becker0ce10102008-08-20 17:19:50 -0700360 et->et_ops->eo_fill_root_el(et);
Joel Becker943cced2008-08-20 17:31:10 -0700361 if (!et->et_ops->eo_fill_max_leaf_clusters)
362 et->et_max_leaf_clusters = 0;
363 else
364 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800365}
366
Joel Becker8d6220d2008-08-22 12:46:09 -0700367void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
368 struct inode *inode,
369 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700370{
Joel Becker13723d02008-10-17 19:25:01 -0700371 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_di,
372 NULL, &ocfs2_dinode_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700373}
374
Joel Becker8d6220d2008-08-22 12:46:09 -0700375void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700376 struct inode *inode,
Joel Becker8d6220d2008-08-22 12:46:09 -0700377 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700378{
Joel Becker13723d02008-10-17 19:25:01 -0700379 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_xb,
380 NULL, &ocfs2_xattr_tree_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700381}
382
Joel Becker8d6220d2008-08-22 12:46:09 -0700383void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
384 struct inode *inode,
Joel Becker2a50a742008-12-09 14:24:33 -0800385 struct ocfs2_xattr_value_buf *vb)
Tao Mae7d4cb62008-08-18 17:38:44 +0800386{
Joel Becker2a50a742008-12-09 14:24:33 -0800387 __ocfs2_init_extent_tree(et, inode, vb->vb_bh, vb->vb_access, vb,
Joel Becker8d6220d2008-08-22 12:46:09 -0700388 &ocfs2_xattr_value_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800389}
390
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800391void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
392 struct inode *inode,
393 struct buffer_head *bh)
394{
395 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_dr,
396 NULL, &ocfs2_dx_root_et_ops);
397}
398
Joel Becker35dc0aa2008-08-20 16:25:06 -0700399static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
400 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800401{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700402 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800403}
404
Joel Becker35dc0aa2008-08-20 16:25:06 -0700405static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800406{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700407 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800408}
409
Joel Becker35dc0aa2008-08-20 16:25:06 -0700410static inline void ocfs2_et_update_clusters(struct inode *inode,
411 struct ocfs2_extent_tree *et,
412 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800413{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700414 et->et_ops->eo_update_clusters(inode, et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700415}
416
Joel Becker13723d02008-10-17 19:25:01 -0700417static inline int ocfs2_et_root_journal_access(handle_t *handle,
418 struct inode *inode,
419 struct ocfs2_extent_tree *et,
420 int type)
421{
422 return et->et_root_journal_access(handle, inode, et->et_root_bh,
423 type);
424}
425
Joel Becker1e61ee72008-08-20 18:32:45 -0700426static inline int ocfs2_et_insert_check(struct inode *inode,
427 struct ocfs2_extent_tree *et,
428 struct ocfs2_extent_rec *rec)
429{
430 int ret = 0;
431
432 if (et->et_ops->eo_insert_check)
433 ret = et->et_ops->eo_insert_check(inode, et, rec);
434 return ret;
435}
436
Joel Becker35dc0aa2008-08-20 16:25:06 -0700437static inline int ocfs2_et_sanity_check(struct inode *inode,
438 struct ocfs2_extent_tree *et)
439{
Joel Becker1e61ee72008-08-20 18:32:45 -0700440 int ret = 0;
441
442 if (et->et_ops->eo_sanity_check)
443 ret = et->et_ops->eo_sanity_check(inode, et);
444 return ret;
Tao Mae7d4cb62008-08-18 17:38:44 +0800445}
446
Mark Fashehccd979b2005-12-15 14:31:24 -0800447static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
Mark Fasheh59a5e412007-06-22 15:52:36 -0700448static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
449 struct ocfs2_extent_block *eb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800450
Mark Fashehdcd05382007-01-16 11:32:23 -0800451/*
452 * Structures which describe a path through a btree, and functions to
453 * manipulate them.
454 *
455 * The idea here is to be as generic as possible with the tree
456 * manipulation code.
457 */
458struct ocfs2_path_item {
459 struct buffer_head *bh;
460 struct ocfs2_extent_list *el;
461};
462
463#define OCFS2_MAX_PATH_DEPTH 5
464
465struct ocfs2_path {
Joel Becker13723d02008-10-17 19:25:01 -0700466 int p_tree_depth;
467 ocfs2_journal_access_func p_root_access;
468 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
Mark Fashehdcd05382007-01-16 11:32:23 -0800469};
470
471#define path_root_bh(_path) ((_path)->p_node[0].bh)
472#define path_root_el(_path) ((_path)->p_node[0].el)
Joel Becker13723d02008-10-17 19:25:01 -0700473#define path_root_access(_path)((_path)->p_root_access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800474#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
475#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
476#define path_num_items(_path) ((_path)->p_tree_depth + 1)
477
Tao Ma6b791bc2009-06-12 14:18:36 +0800478static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
479 u32 cpos);
480static void ocfs2_adjust_rightmost_records(struct inode *inode,
481 handle_t *handle,
482 struct ocfs2_path *path,
483 struct ocfs2_extent_rec *insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -0800484/*
485 * Reset the actual path elements so that we can re-use the structure
486 * to build another path. Generally, this involves freeing the buffer
487 * heads.
488 */
489static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
490{
491 int i, start = 0, depth = 0;
492 struct ocfs2_path_item *node;
493
494 if (keep_root)
495 start = 1;
496
497 for(i = start; i < path_num_items(path); i++) {
498 node = &path->p_node[i];
499
500 brelse(node->bh);
501 node->bh = NULL;
502 node->el = NULL;
503 }
504
505 /*
506 * Tree depth may change during truncate, or insert. If we're
507 * keeping the root extent list, then make sure that our path
508 * structure reflects the proper depth.
509 */
510 if (keep_root)
511 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
Joel Becker13723d02008-10-17 19:25:01 -0700512 else
513 path_root_access(path) = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -0800514
515 path->p_tree_depth = depth;
516}
517
518static void ocfs2_free_path(struct ocfs2_path *path)
519{
520 if (path) {
521 ocfs2_reinit_path(path, 0);
522 kfree(path);
523 }
524}
525
526/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700527 * All the elements of src into dest. After this call, src could be freed
528 * without affecting dest.
529 *
530 * Both paths should have the same root. Any non-root elements of dest
531 * will be freed.
532 */
533static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
534{
535 int i;
536
537 BUG_ON(path_root_bh(dest) != path_root_bh(src));
538 BUG_ON(path_root_el(dest) != path_root_el(src));
Joel Becker13723d02008-10-17 19:25:01 -0700539 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fasheh328d5752007-06-18 10:48:04 -0700540
541 ocfs2_reinit_path(dest, 1);
542
543 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
544 dest->p_node[i].bh = src->p_node[i].bh;
545 dest->p_node[i].el = src->p_node[i].el;
546
547 if (dest->p_node[i].bh)
548 get_bh(dest->p_node[i].bh);
549 }
550}
551
552/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800553 * Make the *dest path the same as src and re-initialize src path to
554 * have a root only.
555 */
556static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
557{
558 int i;
559
560 BUG_ON(path_root_bh(dest) != path_root_bh(src));
Joel Becker13723d02008-10-17 19:25:01 -0700561 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fashehdcd05382007-01-16 11:32:23 -0800562
563 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
564 brelse(dest->p_node[i].bh);
565
566 dest->p_node[i].bh = src->p_node[i].bh;
567 dest->p_node[i].el = src->p_node[i].el;
568
569 src->p_node[i].bh = NULL;
570 src->p_node[i].el = NULL;
571 }
572}
573
574/*
575 * Insert an extent block at given index.
576 *
577 * This will not take an additional reference on eb_bh.
578 */
579static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
580 struct buffer_head *eb_bh)
581{
582 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
583
584 /*
585 * Right now, no root bh is an extent block, so this helps
586 * catch code errors with dinode trees. The assertion can be
587 * safely removed if we ever need to insert extent block
588 * structures at the root.
589 */
590 BUG_ON(index == 0);
591
592 path->p_node[index].bh = eb_bh;
593 path->p_node[index].el = &eb->h_list;
594}
595
596static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700597 struct ocfs2_extent_list *root_el,
598 ocfs2_journal_access_func access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800599{
600 struct ocfs2_path *path;
601
602 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
603
604 path = kzalloc(sizeof(*path), GFP_NOFS);
605 if (path) {
606 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
607 get_bh(root_bh);
608 path_root_bh(path) = root_bh;
609 path_root_el(path) = root_el;
Joel Becker13723d02008-10-17 19:25:01 -0700610 path_root_access(path) = access;
Mark Fashehdcd05382007-01-16 11:32:23 -0800611 }
612
613 return path;
614}
615
Joel Beckerffdd7a52008-10-17 22:32:01 -0700616static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
617{
Joel Becker13723d02008-10-17 19:25:01 -0700618 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
619 path_root_access(path));
Joel Beckerffdd7a52008-10-17 22:32:01 -0700620}
621
622static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
623{
Joel Becker13723d02008-10-17 19:25:01 -0700624 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
625 et->et_root_journal_access);
626}
627
628/*
629 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
630 * otherwise it's the root_access function.
631 *
632 * I don't like the way this function's name looks next to
633 * ocfs2_journal_access_path(), but I don't have a better one.
634 */
635static int ocfs2_path_bh_journal_access(handle_t *handle,
636 struct inode *inode,
637 struct ocfs2_path *path,
638 int idx)
639{
640 ocfs2_journal_access_func access = path_root_access(path);
641
642 if (!access)
643 access = ocfs2_journal_access;
644
645 if (idx)
646 access = ocfs2_journal_access_eb;
647
648 return access(handle, inode, path->p_node[idx].bh,
649 OCFS2_JOURNAL_ACCESS_WRITE);
Joel Beckerffdd7a52008-10-17 22:32:01 -0700650}
651
Mark Fashehdcd05382007-01-16 11:32:23 -0800652/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800653 * Convenience function to journal all components in a path.
654 */
655static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
656 struct ocfs2_path *path)
657{
658 int i, ret = 0;
659
660 if (!path)
661 goto out;
662
663 for(i = 0; i < path_num_items(path); i++) {
Joel Becker13723d02008-10-17 19:25:01 -0700664 ret = ocfs2_path_bh_journal_access(handle, inode, path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -0800665 if (ret < 0) {
666 mlog_errno(ret);
667 goto out;
668 }
669 }
670
671out:
672 return ret;
673}
674
Mark Fasheh328d5752007-06-18 10:48:04 -0700675/*
676 * Return the index of the extent record which contains cluster #v_cluster.
677 * -1 is returned if it was not found.
678 *
679 * Should work fine on interior and exterior nodes.
680 */
681int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
682{
683 int ret = -1;
684 int i;
685 struct ocfs2_extent_rec *rec;
686 u32 rec_end, rec_start, clusters;
687
688 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
689 rec = &el->l_recs[i];
690
691 rec_start = le32_to_cpu(rec->e_cpos);
692 clusters = ocfs2_rec_clusters(el, rec);
693
694 rec_end = rec_start + clusters;
695
696 if (v_cluster >= rec_start && v_cluster < rec_end) {
697 ret = i;
698 break;
699 }
700 }
701
702 return ret;
703}
704
Mark Fashehdcd05382007-01-16 11:32:23 -0800705enum ocfs2_contig_type {
706 CONTIG_NONE = 0,
707 CONTIG_LEFT,
Mark Fasheh328d5752007-06-18 10:48:04 -0700708 CONTIG_RIGHT,
709 CONTIG_LEFTRIGHT,
Mark Fashehdcd05382007-01-16 11:32:23 -0800710};
711
Mark Fashehe48edee2007-03-07 16:46:57 -0800712
713/*
714 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
715 * ocfs2_extent_contig only work properly against leaf nodes!
716 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800717static int ocfs2_block_extent_contig(struct super_block *sb,
718 struct ocfs2_extent_rec *ext,
719 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800720{
Mark Fashehe48edee2007-03-07 16:46:57 -0800721 u64 blk_end = le64_to_cpu(ext->e_blkno);
722
723 blk_end += ocfs2_clusters_to_blocks(sb,
724 le16_to_cpu(ext->e_leaf_clusters));
725
726 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800727}
728
Mark Fashehdcd05382007-01-16 11:32:23 -0800729static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
730 struct ocfs2_extent_rec *right)
731{
Mark Fashehe48edee2007-03-07 16:46:57 -0800732 u32 left_range;
733
734 left_range = le32_to_cpu(left->e_cpos) +
735 le16_to_cpu(left->e_leaf_clusters);
736
737 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800738}
739
740static enum ocfs2_contig_type
741 ocfs2_extent_contig(struct inode *inode,
742 struct ocfs2_extent_rec *ext,
743 struct ocfs2_extent_rec *insert_rec)
744{
745 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
746
Mark Fasheh328d5752007-06-18 10:48:04 -0700747 /*
748 * Refuse to coalesce extent records with different flag
749 * fields - we don't want to mix unwritten extents with user
750 * data.
751 */
752 if (ext->e_flags != insert_rec->e_flags)
753 return CONTIG_NONE;
754
Mark Fashehdcd05382007-01-16 11:32:23 -0800755 if (ocfs2_extents_adjacent(ext, insert_rec) &&
756 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
757 return CONTIG_RIGHT;
758
759 blkno = le64_to_cpu(ext->e_blkno);
760 if (ocfs2_extents_adjacent(insert_rec, ext) &&
761 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
762 return CONTIG_LEFT;
763
764 return CONTIG_NONE;
765}
766
767/*
768 * NOTE: We can have pretty much any combination of contiguousness and
769 * appending.
770 *
771 * The usefulness of APPEND_TAIL is more in that it lets us know that
772 * we'll have to update the path to that leaf.
773 */
774enum ocfs2_append_type {
775 APPEND_NONE = 0,
776 APPEND_TAIL,
777};
778
Mark Fasheh328d5752007-06-18 10:48:04 -0700779enum ocfs2_split_type {
780 SPLIT_NONE = 0,
781 SPLIT_LEFT,
782 SPLIT_RIGHT,
783};
784
Mark Fashehdcd05382007-01-16 11:32:23 -0800785struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700786 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800787 enum ocfs2_append_type ins_appending;
788 enum ocfs2_contig_type ins_contig;
789 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800790 int ins_tree_depth;
791};
792
Mark Fasheh328d5752007-06-18 10:48:04 -0700793struct ocfs2_merge_ctxt {
794 enum ocfs2_contig_type c_contig_type;
795 int c_has_empty_extent;
796 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700797};
798
Joel Becker5e965812008-11-13 14:49:16 -0800799static int ocfs2_validate_extent_block(struct super_block *sb,
800 struct buffer_head *bh)
801{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700802 int rc;
Joel Becker5e965812008-11-13 14:49:16 -0800803 struct ocfs2_extent_block *eb =
804 (struct ocfs2_extent_block *)bh->b_data;
805
Joel Becker970e4932008-11-13 14:49:19 -0800806 mlog(0, "Validating extent block %llu\n",
807 (unsigned long long)bh->b_blocknr);
808
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700809 BUG_ON(!buffer_uptodate(bh));
810
811 /*
812 * If the ecc fails, we return the error but otherwise
813 * leave the filesystem running. We know any error is
814 * local to this block.
815 */
816 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
Joel Becker13723d02008-10-17 19:25:01 -0700817 if (rc) {
818 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
819 (unsigned long long)bh->b_blocknr);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700820 return rc;
Joel Becker13723d02008-10-17 19:25:01 -0700821 }
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700822
823 /*
824 * Errors after here are fatal.
825 */
826
Joel Becker5e965812008-11-13 14:49:16 -0800827 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
828 ocfs2_error(sb,
829 "Extent block #%llu has bad signature %.*s",
830 (unsigned long long)bh->b_blocknr, 7,
831 eb->h_signature);
832 return -EINVAL;
833 }
834
835 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
836 ocfs2_error(sb,
837 "Extent block #%llu has an invalid h_blkno "
838 "of %llu",
839 (unsigned long long)bh->b_blocknr,
840 (unsigned long long)le64_to_cpu(eb->h_blkno));
841 return -EINVAL;
842 }
843
844 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
845 ocfs2_error(sb,
846 "Extent block #%llu has an invalid "
847 "h_fs_generation of #%u",
848 (unsigned long long)bh->b_blocknr,
849 le32_to_cpu(eb->h_fs_generation));
850 return -EINVAL;
851 }
852
853 return 0;
854}
855
856int ocfs2_read_extent_block(struct inode *inode, u64 eb_blkno,
857 struct buffer_head **bh)
858{
859 int rc;
860 struct buffer_head *tmp = *bh;
861
Joel Becker8cb471e2009-02-10 20:00:41 -0800862 rc = ocfs2_read_block(INODE_CACHE(inode), eb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800863 ocfs2_validate_extent_block);
Joel Becker5e965812008-11-13 14:49:16 -0800864
865 /* If ocfs2_read_block() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -0800866 if (!rc && !*bh)
Joel Becker5e965812008-11-13 14:49:16 -0800867 *bh = tmp;
868
Joel Becker5e965812008-11-13 14:49:16 -0800869 return rc;
870}
871
872
Mark Fashehccd979b2005-12-15 14:31:24 -0800873/*
874 * How many free extents have we got before we need more meta data?
875 */
876int ocfs2_num_free_extents(struct ocfs2_super *osb,
877 struct inode *inode,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700878 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -0800879{
880 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800881 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800882 struct ocfs2_extent_block *eb;
883 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800884 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800885
886 mlog_entry_void();
887
Joel Beckerf99b9b72008-08-20 19:36:33 -0700888 el = et->et_root_el;
889 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800890
Tao Mae7d4cb62008-08-18 17:38:44 +0800891 if (last_eb_blk) {
Joel Becker5e965812008-11-13 14:49:16 -0800892 retval = ocfs2_read_extent_block(inode, last_eb_blk, &eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800893 if (retval < 0) {
894 mlog_errno(retval);
895 goto bail;
896 }
897 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
898 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800899 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800900
901 BUG_ON(el->l_tree_depth != 0);
902
903 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
904bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700905 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800906
907 mlog_exit(retval);
908 return retval;
909}
910
911/* expects array to already be allocated
912 *
913 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
914 * l_count for you
915 */
916static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700917 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800918 struct inode *inode,
919 int wanted,
920 struct ocfs2_alloc_context *meta_ac,
921 struct buffer_head *bhs[])
922{
923 int count, status, i;
924 u16 suballoc_bit_start;
925 u32 num_got;
926 u64 first_blkno;
927 struct ocfs2_extent_block *eb;
928
929 mlog_entry_void();
930
931 count = 0;
932 while (count < wanted) {
933 status = ocfs2_claim_metadata(osb,
934 handle,
935 meta_ac,
936 wanted - count,
937 &suballoc_bit_start,
938 &num_got,
939 &first_blkno);
940 if (status < 0) {
941 mlog_errno(status);
942 goto bail;
943 }
944
945 for(i = count; i < (num_got + count); i++) {
946 bhs[i] = sb_getblk(osb->sb, first_blkno);
947 if (bhs[i] == NULL) {
948 status = -EIO;
949 mlog_errno(status);
950 goto bail;
951 }
Joel Becker8cb471e2009-02-10 20:00:41 -0800952 ocfs2_set_new_buffer_uptodate(INODE_CACHE(inode),
953 bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800954
Joel Becker13723d02008-10-17 19:25:01 -0700955 status = ocfs2_journal_access_eb(handle, inode, bhs[i],
956 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800957 if (status < 0) {
958 mlog_errno(status);
959 goto bail;
960 }
961
962 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
963 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
964 /* Ok, setup the minimal stuff here. */
965 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
966 eb->h_blkno = cpu_to_le64(first_blkno);
967 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800968 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800969 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
970 eb->h_list.l_count =
971 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
972
973 suballoc_bit_start++;
974 first_blkno++;
975
976 /* We'll also be dirtied by the caller, so
977 * this isn't absolutely necessary. */
978 status = ocfs2_journal_dirty(handle, bhs[i]);
979 if (status < 0) {
980 mlog_errno(status);
981 goto bail;
982 }
983 }
984
985 count += num_got;
986 }
987
988 status = 0;
989bail:
990 if (status < 0) {
991 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -0700992 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800993 bhs[i] = NULL;
994 }
995 }
996 mlog_exit(status);
997 return status;
998}
999
1000/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001001 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1002 *
1003 * Returns the sum of the rightmost extent rec logical offset and
1004 * cluster count.
1005 *
1006 * ocfs2_add_branch() uses this to determine what logical cluster
1007 * value should be populated into the leftmost new branch records.
1008 *
1009 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1010 * value for the new topmost tree record.
1011 */
1012static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1013{
1014 int i;
1015
1016 i = le16_to_cpu(el->l_next_free_rec) - 1;
1017
1018 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001019 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08001020}
1021
1022/*
Tao Ma6b791bc2009-06-12 14:18:36 +08001023 * Change range of the branches in the right most path according to the leaf
1024 * extent block's rightmost record.
1025 */
1026static int ocfs2_adjust_rightmost_branch(handle_t *handle,
1027 struct inode *inode,
1028 struct ocfs2_extent_tree *et)
1029{
1030 int status;
1031 struct ocfs2_path *path = NULL;
1032 struct ocfs2_extent_list *el;
1033 struct ocfs2_extent_rec *rec;
1034
1035 path = ocfs2_new_path_from_et(et);
1036 if (!path) {
1037 status = -ENOMEM;
1038 return status;
1039 }
1040
1041 status = ocfs2_find_path(inode, path, UINT_MAX);
1042 if (status < 0) {
1043 mlog_errno(status);
1044 goto out;
1045 }
1046
1047 status = ocfs2_extend_trans(handle, path_num_items(path) +
1048 handle->h_buffer_credits);
1049 if (status < 0) {
1050 mlog_errno(status);
1051 goto out;
1052 }
1053
1054 status = ocfs2_journal_access_path(inode, handle, path);
1055 if (status < 0) {
1056 mlog_errno(status);
1057 goto out;
1058 }
1059
1060 el = path_leaf_el(path);
1061 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1062
1063 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
1064
1065out:
1066 ocfs2_free_path(path);
1067 return status;
1068}
1069
1070/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001071 * Add an entire tree branch to our inode. eb_bh is the extent block
1072 * to start at, if we don't want to start the branch at the dinode
1073 * structure.
1074 *
1075 * last_eb_bh is required as we have to update it's next_leaf pointer
1076 * for the new last extent block.
1077 *
1078 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -08001079 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -08001080 */
1081static int ocfs2_add_branch(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001082 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001083 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001084 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001085 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07001086 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001087 struct ocfs2_alloc_context *meta_ac)
1088{
1089 int status, new_blocks, i;
1090 u64 next_blkno, new_last_eb_blk;
1091 struct buffer_head *bh;
1092 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001093 struct ocfs2_extent_block *eb;
1094 struct ocfs2_extent_list *eb_el;
1095 struct ocfs2_extent_list *el;
Tao Ma6b791bc2009-06-12 14:18:36 +08001096 u32 new_cpos, root_end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001097
1098 mlog_entry_void();
1099
Mark Fasheh328d5752007-06-18 10:48:04 -07001100 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001101
Mark Fashehccd979b2005-12-15 14:31:24 -08001102 if (eb_bh) {
1103 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1104 el = &eb->h_list;
1105 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001106 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001107
1108 /* we never add a branch to a leaf. */
1109 BUG_ON(!el->l_tree_depth);
1110
1111 new_blocks = le16_to_cpu(el->l_tree_depth);
1112
Tao Ma6b791bc2009-06-12 14:18:36 +08001113 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1114 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1115 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1116
1117 /*
1118 * If there is a gap before the root end and the real end
1119 * of the righmost leaf block, we need to remove the gap
1120 * between new_cpos and root_end first so that the tree
1121 * is consistent after we add a new branch(it will start
1122 * from new_cpos).
1123 */
1124 if (root_end > new_cpos) {
1125 mlog(0, "adjust the cluster end from %u to %u\n",
1126 root_end, new_cpos);
1127 status = ocfs2_adjust_rightmost_branch(handle, inode, et);
1128 if (status) {
1129 mlog_errno(status);
1130 goto bail;
1131 }
1132 }
1133
Mark Fashehccd979b2005-12-15 14:31:24 -08001134 /* allocate the number of new eb blocks we need */
1135 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1136 GFP_KERNEL);
1137 if (!new_eb_bhs) {
1138 status = -ENOMEM;
1139 mlog_errno(status);
1140 goto bail;
1141 }
1142
1143 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
1144 meta_ac, new_eb_bhs);
1145 if (status < 0) {
1146 mlog_errno(status);
1147 goto bail;
1148 }
1149
1150 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1151 * linked with the rest of the tree.
1152 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1153 *
1154 * when we leave the loop, new_last_eb_blk will point to the
1155 * newest leaf, and next_blkno will point to the topmost extent
1156 * block. */
1157 next_blkno = new_last_eb_blk = 0;
1158 for(i = 0; i < new_blocks; i++) {
1159 bh = new_eb_bhs[i];
1160 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001161 /* ocfs2_create_new_meta_bhs() should create it right! */
1162 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001163 eb_el = &eb->h_list;
1164
Joel Becker13723d02008-10-17 19:25:01 -07001165 status = ocfs2_journal_access_eb(handle, inode, bh,
1166 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001167 if (status < 0) {
1168 mlog_errno(status);
1169 goto bail;
1170 }
1171
1172 eb->h_next_leaf_blk = 0;
1173 eb_el->l_tree_depth = cpu_to_le16(i);
1174 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001175 /*
1176 * This actually counts as an empty extent as
1177 * c_clusters == 0
1178 */
1179 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001180 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001181 /*
1182 * eb_el isn't always an interior node, but even leaf
1183 * nodes want a zero'd flags and reserved field so
1184 * this gets the whole 32 bits regardless of use.
1185 */
1186 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001187 if (!eb_el->l_tree_depth)
1188 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1189
1190 status = ocfs2_journal_dirty(handle, bh);
1191 if (status < 0) {
1192 mlog_errno(status);
1193 goto bail;
1194 }
1195
1196 next_blkno = le64_to_cpu(eb->h_blkno);
1197 }
1198
1199 /* This is a bit hairy. We want to update up to three blocks
1200 * here without leaving any of them in an inconsistent state
1201 * in case of error. We don't have to worry about
1202 * journal_dirty erroring as it won't unless we've aborted the
1203 * handle (in which case we would never be here) so reserving
1204 * the write with journal_access is all we need to do. */
Joel Becker13723d02008-10-17 19:25:01 -07001205 status = ocfs2_journal_access_eb(handle, inode, *last_eb_bh,
1206 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001207 if (status < 0) {
1208 mlog_errno(status);
1209 goto bail;
1210 }
Joel Becker13723d02008-10-17 19:25:01 -07001211 status = ocfs2_et_root_journal_access(handle, inode, et,
1212 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001213 if (status < 0) {
1214 mlog_errno(status);
1215 goto bail;
1216 }
1217 if (eb_bh) {
Joel Becker13723d02008-10-17 19:25:01 -07001218 status = ocfs2_journal_access_eb(handle, inode, eb_bh,
1219 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001220 if (status < 0) {
1221 mlog_errno(status);
1222 goto bail;
1223 }
1224 }
1225
1226 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001227 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001228 i = le16_to_cpu(el->l_next_free_rec);
1229 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001230 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001231 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001232 le16_add_cpu(&el->l_next_free_rec, 1);
1233
1234 /* fe needs a new last extent block pointer, as does the
1235 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001236 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001237
Mark Fasheh328d5752007-06-18 10:48:04 -07001238 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001239 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1240
Mark Fasheh328d5752007-06-18 10:48:04 -07001241 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001242 if (status < 0)
1243 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001244 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001245 if (status < 0)
1246 mlog_errno(status);
1247 if (eb_bh) {
1248 status = ocfs2_journal_dirty(handle, eb_bh);
1249 if (status < 0)
1250 mlog_errno(status);
1251 }
1252
Mark Fasheh328d5752007-06-18 10:48:04 -07001253 /*
1254 * Some callers want to track the rightmost leaf so pass it
1255 * back here.
1256 */
1257 brelse(*last_eb_bh);
1258 get_bh(new_eb_bhs[0]);
1259 *last_eb_bh = new_eb_bhs[0];
1260
Mark Fashehccd979b2005-12-15 14:31:24 -08001261 status = 0;
1262bail:
1263 if (new_eb_bhs) {
1264 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001265 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001266 kfree(new_eb_bhs);
1267 }
1268
1269 mlog_exit(status);
1270 return status;
1271}
1272
1273/*
1274 * adds another level to the allocation tree.
1275 * returns back the new extent block so you can add a branch to it
1276 * after this call.
1277 */
1278static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001279 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001280 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001281 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001282 struct ocfs2_alloc_context *meta_ac,
1283 struct buffer_head **ret_new_eb_bh)
1284{
1285 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001286 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001287 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001288 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001289 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001290 struct ocfs2_extent_list *eb_el;
1291
1292 mlog_entry_void();
1293
1294 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
1295 &new_eb_bh);
1296 if (status < 0) {
1297 mlog_errno(status);
1298 goto bail;
1299 }
1300
1301 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001302 /* ocfs2_create_new_meta_bhs() should create it right! */
1303 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001304
1305 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001306 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001307
Joel Becker13723d02008-10-17 19:25:01 -07001308 status = ocfs2_journal_access_eb(handle, inode, new_eb_bh,
1309 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001310 if (status < 0) {
1311 mlog_errno(status);
1312 goto bail;
1313 }
1314
Tao Mae7d4cb62008-08-18 17:38:44 +08001315 /* copy the root extent list data into the new extent block */
1316 eb_el->l_tree_depth = root_el->l_tree_depth;
1317 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1318 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1319 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001320
1321 status = ocfs2_journal_dirty(handle, new_eb_bh);
1322 if (status < 0) {
1323 mlog_errno(status);
1324 goto bail;
1325 }
1326
Joel Becker13723d02008-10-17 19:25:01 -07001327 status = ocfs2_et_root_journal_access(handle, inode, et,
1328 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001329 if (status < 0) {
1330 mlog_errno(status);
1331 goto bail;
1332 }
1333
Mark Fashehdcd05382007-01-16 11:32:23 -08001334 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1335
Tao Mae7d4cb62008-08-18 17:38:44 +08001336 /* update root_bh now */
1337 le16_add_cpu(&root_el->l_tree_depth, 1);
1338 root_el->l_recs[0].e_cpos = 0;
1339 root_el->l_recs[0].e_blkno = eb->h_blkno;
1340 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1341 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1342 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1343 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001344
1345 /* If this is our 1st tree depth shift, then last_eb_blk
1346 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001347 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001348 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001349
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001350 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001351 if (status < 0) {
1352 mlog_errno(status);
1353 goto bail;
1354 }
1355
1356 *ret_new_eb_bh = new_eb_bh;
1357 new_eb_bh = NULL;
1358 status = 0;
1359bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001360 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001361
1362 mlog_exit(status);
1363 return status;
1364}
1365
1366/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001367 * Should only be called when there is no space left in any of the
1368 * leaf nodes. What we want to do is find the lowest tree depth
1369 * non-leaf extent block with room for new records. There are three
1370 * valid results of this search:
1371 *
1372 * 1) a lowest extent block is found, then we pass it back in
1373 * *lowest_eb_bh and return '0'
1374 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001375 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001376 * pass NULL back in *lowest_eb_bh, but still return '0'
1377 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001378 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001379 * which case we return > 0
1380 *
1381 * return status < 0 indicates an error.
1382 */
1383static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1384 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001385 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001386 struct buffer_head **target_bh)
1387{
1388 int status = 0, i;
1389 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001390 struct ocfs2_extent_block *eb;
1391 struct ocfs2_extent_list *el;
1392 struct buffer_head *bh = NULL;
1393 struct buffer_head *lowest_bh = NULL;
1394
1395 mlog_entry_void();
1396
1397 *target_bh = NULL;
1398
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001399 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001400
1401 while(le16_to_cpu(el->l_tree_depth) > 1) {
1402 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Mark Fashehb06970532006-03-03 10:24:33 -08001403 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001404 "extent list (next_free_rec == 0)",
Mark Fashehb06970532006-03-03 10:24:33 -08001405 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001406 status = -EIO;
1407 goto bail;
1408 }
1409 i = le16_to_cpu(el->l_next_free_rec) - 1;
1410 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1411 if (!blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -08001412 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001413 "list where extent # %d has no physical "
1414 "block start",
Mark Fashehb06970532006-03-03 10:24:33 -08001415 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001416 status = -EIO;
1417 goto bail;
1418 }
1419
Mark Fasheha81cb882008-10-07 14:25:16 -07001420 brelse(bh);
1421 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001422
Joel Becker5e965812008-11-13 14:49:16 -08001423 status = ocfs2_read_extent_block(inode, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001424 if (status < 0) {
1425 mlog_errno(status);
1426 goto bail;
1427 }
1428
1429 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001430 el = &eb->h_list;
1431
1432 if (le16_to_cpu(el->l_next_free_rec) <
1433 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001434 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001435 lowest_bh = bh;
1436 get_bh(lowest_bh);
1437 }
1438 }
1439
1440 /* If we didn't find one and the fe doesn't have any room,
1441 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001442 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001443 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001444 status = 1;
1445
1446 *target_bh = lowest_bh;
1447bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001448 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001449
1450 mlog_exit(status);
1451 return status;
1452}
1453
Mark Fashehe48edee2007-03-07 16:46:57 -08001454/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001455 * Grow a b-tree so that it has more records.
1456 *
1457 * We might shift the tree depth in which case existing paths should
1458 * be considered invalid.
1459 *
1460 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001461 *
1462 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001463 */
1464static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001465 struct ocfs2_extent_tree *et, int *final_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07001466 struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001467 struct ocfs2_alloc_context *meta_ac)
1468{
1469 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001470 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001471 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001472 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1473 struct buffer_head *bh = NULL;
1474
1475 BUG_ON(meta_ac == NULL);
1476
Tao Mae7d4cb62008-08-18 17:38:44 +08001477 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001478 if (shift < 0) {
1479 ret = shift;
1480 mlog_errno(ret);
1481 goto out;
1482 }
1483
1484 /* We traveled all the way to the bottom of the allocation tree
1485 * and didn't find room for any more extents - we need to add
1486 * another tree level */
1487 if (shift) {
1488 BUG_ON(bh);
1489 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1490
1491 /* ocfs2_shift_tree_depth will return us a buffer with
1492 * the new extent block (so we can pass that to
1493 * ocfs2_add_branch). */
Tao Mae7d4cb62008-08-18 17:38:44 +08001494 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001495 meta_ac, &bh);
1496 if (ret < 0) {
1497 mlog_errno(ret);
1498 goto out;
1499 }
1500 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001501 if (depth == 1) {
1502 /*
1503 * Special case: we have room now if we shifted from
1504 * tree_depth 0, so no more work needs to be done.
1505 *
1506 * We won't be calling add_branch, so pass
1507 * back *last_eb_bh as the new leaf. At depth
1508 * zero, it should always be null so there's
1509 * no reason to brelse.
1510 */
1511 BUG_ON(*last_eb_bh);
1512 get_bh(bh);
1513 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001514 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001515 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001516 }
1517
1518 /* call ocfs2_add_branch to add the final part of the tree with
1519 * the new data. */
1520 mlog(0, "add branch. bh = %p\n", bh);
Tao Mae7d4cb62008-08-18 17:38:44 +08001521 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001522 meta_ac);
1523 if (ret < 0) {
1524 mlog_errno(ret);
1525 goto out;
1526 }
1527
1528out:
1529 if (final_depth)
1530 *final_depth = depth;
1531 brelse(bh);
1532 return ret;
1533}
1534
1535/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001536 * This function will discard the rightmost extent record.
1537 */
1538static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1539{
1540 int next_free = le16_to_cpu(el->l_next_free_rec);
1541 int count = le16_to_cpu(el->l_count);
1542 unsigned int num_bytes;
1543
1544 BUG_ON(!next_free);
1545 /* This will cause us to go off the end of our extent list. */
1546 BUG_ON(next_free >= count);
1547
1548 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1549
1550 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1551}
1552
1553static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1554 struct ocfs2_extent_rec *insert_rec)
1555{
1556 int i, insert_index, next_free, has_empty, num_bytes;
1557 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1558 struct ocfs2_extent_rec *rec;
1559
1560 next_free = le16_to_cpu(el->l_next_free_rec);
1561 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1562
1563 BUG_ON(!next_free);
1564
1565 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001566 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001567
1568 /*
1569 * The easiest way to approach this is to just remove the
1570 * empty extent and temporarily decrement next_free.
1571 */
1572 if (has_empty) {
1573 /*
1574 * If next_free was 1 (only an empty extent), this
1575 * loop won't execute, which is fine. We still want
1576 * the decrement above to happen.
1577 */
1578 for(i = 0; i < (next_free - 1); i++)
1579 el->l_recs[i] = el->l_recs[i+1];
1580
1581 next_free--;
1582 }
1583
1584 /*
1585 * Figure out what the new record index should be.
1586 */
1587 for(i = 0; i < next_free; i++) {
1588 rec = &el->l_recs[i];
1589
1590 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1591 break;
1592 }
1593 insert_index = i;
1594
1595 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1596 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1597
1598 BUG_ON(insert_index < 0);
1599 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1600 BUG_ON(insert_index > next_free);
1601
1602 /*
1603 * No need to memmove if we're just adding to the tail.
1604 */
1605 if (insert_index != next_free) {
1606 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1607
1608 num_bytes = next_free - insert_index;
1609 num_bytes *= sizeof(struct ocfs2_extent_rec);
1610 memmove(&el->l_recs[insert_index + 1],
1611 &el->l_recs[insert_index],
1612 num_bytes);
1613 }
1614
1615 /*
1616 * Either we had an empty extent, and need to re-increment or
1617 * there was no empty extent on a non full rightmost leaf node,
1618 * in which case we still need to increment.
1619 */
1620 next_free++;
1621 el->l_next_free_rec = cpu_to_le16(next_free);
1622 /*
1623 * Make sure none of the math above just messed up our tree.
1624 */
1625 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1626
1627 el->l_recs[insert_index] = *insert_rec;
1628
1629}
1630
Mark Fasheh328d5752007-06-18 10:48:04 -07001631static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1632{
1633 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1634
1635 BUG_ON(num_recs == 0);
1636
1637 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1638 num_recs--;
1639 size = num_recs * sizeof(struct ocfs2_extent_rec);
1640 memmove(&el->l_recs[0], &el->l_recs[1], size);
1641 memset(&el->l_recs[num_recs], 0,
1642 sizeof(struct ocfs2_extent_rec));
1643 el->l_next_free_rec = cpu_to_le16(num_recs);
1644 }
1645}
1646
Mark Fashehdcd05382007-01-16 11:32:23 -08001647/*
1648 * Create an empty extent record .
1649 *
1650 * l_next_free_rec may be updated.
1651 *
1652 * If an empty extent already exists do nothing.
1653 */
1654static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1655{
1656 int next_free = le16_to_cpu(el->l_next_free_rec);
1657
Mark Fashehe48edee2007-03-07 16:46:57 -08001658 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1659
Mark Fashehdcd05382007-01-16 11:32:23 -08001660 if (next_free == 0)
1661 goto set_and_inc;
1662
1663 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1664 return;
1665
1666 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1667 "Asked to create an empty extent in a full list:\n"
1668 "count = %u, tree depth = %u",
1669 le16_to_cpu(el->l_count),
1670 le16_to_cpu(el->l_tree_depth));
1671
1672 ocfs2_shift_records_right(el);
1673
1674set_and_inc:
1675 le16_add_cpu(&el->l_next_free_rec, 1);
1676 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1677}
1678
1679/*
1680 * For a rotation which involves two leaf nodes, the "root node" is
1681 * the lowest level tree node which contains a path to both leafs. This
1682 * resulting set of information can be used to form a complete "subtree"
1683 *
1684 * This function is passed two full paths from the dinode down to a
1685 * pair of adjacent leaves. It's task is to figure out which path
1686 * index contains the subtree root - this can be the root index itself
1687 * in a worst-case rotation.
1688 *
1689 * The array index of the subtree root is passed back.
1690 */
1691static int ocfs2_find_subtree_root(struct inode *inode,
1692 struct ocfs2_path *left,
1693 struct ocfs2_path *right)
1694{
1695 int i = 0;
1696
1697 /*
1698 * Check that the caller passed in two paths from the same tree.
1699 */
1700 BUG_ON(path_root_bh(left) != path_root_bh(right));
1701
1702 do {
1703 i++;
1704
1705 /*
1706 * The caller didn't pass two adjacent paths.
1707 */
1708 mlog_bug_on_msg(i > left->p_tree_depth,
1709 "Inode %lu, left depth %u, right depth %u\n"
1710 "left leaf blk %llu, right leaf blk %llu\n",
1711 inode->i_ino, left->p_tree_depth,
1712 right->p_tree_depth,
1713 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1714 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1715 } while (left->p_node[i].bh->b_blocknr ==
1716 right->p_node[i].bh->b_blocknr);
1717
1718 return i - 1;
1719}
1720
1721typedef void (path_insert_t)(void *, struct buffer_head *);
1722
1723/*
1724 * Traverse a btree path in search of cpos, starting at root_el.
1725 *
1726 * This code can be called with a cpos larger than the tree, in which
1727 * case it will return the rightmost path.
1728 */
1729static int __ocfs2_find_path(struct inode *inode,
1730 struct ocfs2_extent_list *root_el, u32 cpos,
1731 path_insert_t *func, void *data)
1732{
1733 int i, ret = 0;
1734 u32 range;
1735 u64 blkno;
1736 struct buffer_head *bh = NULL;
1737 struct ocfs2_extent_block *eb;
1738 struct ocfs2_extent_list *el;
1739 struct ocfs2_extent_rec *rec;
1740 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1741
1742 el = root_el;
1743 while (el->l_tree_depth) {
1744 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1745 ocfs2_error(inode->i_sb,
1746 "Inode %llu has empty extent list at "
1747 "depth %u\n",
1748 (unsigned long long)oi->ip_blkno,
1749 le16_to_cpu(el->l_tree_depth));
1750 ret = -EROFS;
1751 goto out;
1752
1753 }
1754
1755 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1756 rec = &el->l_recs[i];
1757
1758 /*
1759 * In the case that cpos is off the allocation
1760 * tree, this should just wind up returning the
1761 * rightmost record.
1762 */
1763 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001764 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001765 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1766 break;
1767 }
1768
1769 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1770 if (blkno == 0) {
1771 ocfs2_error(inode->i_sb,
1772 "Inode %llu has bad blkno in extent list "
1773 "at depth %u (index %d)\n",
1774 (unsigned long long)oi->ip_blkno,
1775 le16_to_cpu(el->l_tree_depth), i);
1776 ret = -EROFS;
1777 goto out;
1778 }
1779
1780 brelse(bh);
1781 bh = NULL;
Joel Becker5e965812008-11-13 14:49:16 -08001782 ret = ocfs2_read_extent_block(inode, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001783 if (ret) {
1784 mlog_errno(ret);
1785 goto out;
1786 }
1787
1788 eb = (struct ocfs2_extent_block *) bh->b_data;
1789 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001790
1791 if (le16_to_cpu(el->l_next_free_rec) >
1792 le16_to_cpu(el->l_count)) {
1793 ocfs2_error(inode->i_sb,
1794 "Inode %llu has bad count in extent list "
1795 "at block %llu (next free=%u, count=%u)\n",
1796 (unsigned long long)oi->ip_blkno,
1797 (unsigned long long)bh->b_blocknr,
1798 le16_to_cpu(el->l_next_free_rec),
1799 le16_to_cpu(el->l_count));
1800 ret = -EROFS;
1801 goto out;
1802 }
1803
1804 if (func)
1805 func(data, bh);
1806 }
1807
1808out:
1809 /*
1810 * Catch any trailing bh that the loop didn't handle.
1811 */
1812 brelse(bh);
1813
1814 return ret;
1815}
1816
1817/*
1818 * Given an initialized path (that is, it has a valid root extent
1819 * list), this function will traverse the btree in search of the path
1820 * which would contain cpos.
1821 *
1822 * The path traveled is recorded in the path structure.
1823 *
1824 * Note that this will not do any comparisons on leaf node extent
1825 * records, so it will work fine in the case that we just added a tree
1826 * branch.
1827 */
1828struct find_path_data {
1829 int index;
1830 struct ocfs2_path *path;
1831};
1832static void find_path_ins(void *data, struct buffer_head *bh)
1833{
1834 struct find_path_data *fp = data;
1835
1836 get_bh(bh);
1837 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1838 fp->index++;
1839}
1840static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1841 u32 cpos)
1842{
1843 struct find_path_data data;
1844
1845 data.index = 1;
1846 data.path = path;
1847 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1848 find_path_ins, &data);
1849}
1850
1851static void find_leaf_ins(void *data, struct buffer_head *bh)
1852{
1853 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1854 struct ocfs2_extent_list *el = &eb->h_list;
1855 struct buffer_head **ret = data;
1856
1857 /* We want to retain only the leaf block. */
1858 if (le16_to_cpu(el->l_tree_depth) == 0) {
1859 get_bh(bh);
1860 *ret = bh;
1861 }
1862}
1863/*
1864 * Find the leaf block in the tree which would contain cpos. No
1865 * checking of the actual leaf is done.
1866 *
1867 * Some paths want to call this instead of allocating a path structure
1868 * and calling ocfs2_find_path().
1869 *
1870 * This function doesn't handle non btree extent lists.
1871 */
Mark Fasheh363041a2007-01-17 12:31:35 -08001872int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1873 u32 cpos, struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001874{
1875 int ret;
1876 struct buffer_head *bh = NULL;
1877
1878 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1879 if (ret) {
1880 mlog_errno(ret);
1881 goto out;
1882 }
1883
1884 *leaf_bh = bh;
1885out:
1886 return ret;
1887}
1888
1889/*
1890 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1891 *
1892 * Basically, we've moved stuff around at the bottom of the tree and
1893 * we need to fix up the extent records above the changes to reflect
1894 * the new changes.
1895 *
1896 * left_rec: the record on the left.
1897 * left_child_el: is the child list pointed to by left_rec
1898 * right_rec: the record to the right of left_rec
1899 * right_child_el: is the child list pointed to by right_rec
1900 *
1901 * By definition, this only works on interior nodes.
1902 */
1903static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1904 struct ocfs2_extent_list *left_child_el,
1905 struct ocfs2_extent_rec *right_rec,
1906 struct ocfs2_extent_list *right_child_el)
1907{
1908 u32 left_clusters, right_end;
1909
1910 /*
1911 * Interior nodes never have holes. Their cpos is the cpos of
1912 * the leftmost record in their child list. Their cluster
1913 * count covers the full theoretical range of their child list
1914 * - the range between their cpos and the cpos of the record
1915 * immediately to their right.
1916 */
1917 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08001918 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1919 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07001920 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1921 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1922 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001923 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001924 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001925
1926 /*
1927 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001928 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001929 * updating e_cpos to keep the same highest cluster count.
1930 */
1931 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001932 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001933
1934 right_rec->e_cpos = left_rec->e_cpos;
1935 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1936
1937 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001938 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001939}
1940
1941/*
1942 * Adjust the adjacent root node records involved in a
1943 * rotation. left_el_blkno is passed in as a key so that we can easily
1944 * find it's index in the root list.
1945 */
1946static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1947 struct ocfs2_extent_list *left_el,
1948 struct ocfs2_extent_list *right_el,
1949 u64 left_el_blkno)
1950{
1951 int i;
1952
1953 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1954 le16_to_cpu(left_el->l_tree_depth));
1955
1956 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1957 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1958 break;
1959 }
1960
1961 /*
1962 * The path walking code should have never returned a root and
1963 * two paths which are not adjacent.
1964 */
1965 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1966
1967 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1968 &root_el->l_recs[i + 1], right_el);
1969}
1970
1971/*
1972 * We've changed a leaf block (in right_path) and need to reflect that
1973 * change back up the subtree.
1974 *
1975 * This happens in multiple places:
1976 * - When we've moved an extent record from the left path leaf to the right
1977 * path leaf to make room for an empty extent in the left path leaf.
1978 * - When our insert into the right path leaf is at the leftmost edge
1979 * and requires an update of the path immediately to it's left. This
1980 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08001981 * - When we've adjusted the last extent record in the left path leaf and the
1982 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08001983 */
1984static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1985 struct ocfs2_path *left_path,
1986 struct ocfs2_path *right_path,
1987 int subtree_index)
1988{
1989 int ret, i, idx;
1990 struct ocfs2_extent_list *el, *left_el, *right_el;
1991 struct ocfs2_extent_rec *left_rec, *right_rec;
1992 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1993
1994 /*
1995 * Update the counts and position values within all the
1996 * interior nodes to reflect the leaf rotation we just did.
1997 *
1998 * The root node is handled below the loop.
1999 *
2000 * We begin the loop with right_el and left_el pointing to the
2001 * leaf lists and work our way up.
2002 *
2003 * NOTE: within this loop, left_el and right_el always refer
2004 * to the *child* lists.
2005 */
2006 left_el = path_leaf_el(left_path);
2007 right_el = path_leaf_el(right_path);
2008 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2009 mlog(0, "Adjust records at index %u\n", i);
2010
2011 /*
2012 * One nice property of knowing that all of these
2013 * nodes are below the root is that we only deal with
2014 * the leftmost right node record and the rightmost
2015 * left node record.
2016 */
2017 el = left_path->p_node[i].el;
2018 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2019 left_rec = &el->l_recs[idx];
2020
2021 el = right_path->p_node[i].el;
2022 right_rec = &el->l_recs[0];
2023
2024 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2025 right_el);
2026
2027 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2028 if (ret)
2029 mlog_errno(ret);
2030
2031 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2032 if (ret)
2033 mlog_errno(ret);
2034
2035 /*
2036 * Setup our list pointers now so that the current
2037 * parents become children in the next iteration.
2038 */
2039 left_el = left_path->p_node[i].el;
2040 right_el = right_path->p_node[i].el;
2041 }
2042
2043 /*
2044 * At the root node, adjust the two adjacent records which
2045 * begin our path to the leaves.
2046 */
2047
2048 el = left_path->p_node[subtree_index].el;
2049 left_el = left_path->p_node[subtree_index + 1].el;
2050 right_el = right_path->p_node[subtree_index + 1].el;
2051
2052 ocfs2_adjust_root_records(el, left_el, right_el,
2053 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2054
2055 root_bh = left_path->p_node[subtree_index].bh;
2056
2057 ret = ocfs2_journal_dirty(handle, root_bh);
2058 if (ret)
2059 mlog_errno(ret);
2060}
2061
2062static int ocfs2_rotate_subtree_right(struct inode *inode,
2063 handle_t *handle,
2064 struct ocfs2_path *left_path,
2065 struct ocfs2_path *right_path,
2066 int subtree_index)
2067{
2068 int ret, i;
2069 struct buffer_head *right_leaf_bh;
2070 struct buffer_head *left_leaf_bh = NULL;
2071 struct buffer_head *root_bh;
2072 struct ocfs2_extent_list *right_el, *left_el;
2073 struct ocfs2_extent_rec move_rec;
2074
2075 left_leaf_bh = path_leaf_bh(left_path);
2076 left_el = path_leaf_el(left_path);
2077
2078 if (left_el->l_next_free_rec != left_el->l_count) {
2079 ocfs2_error(inode->i_sb,
2080 "Inode %llu has non-full interior leaf node %llu"
2081 "(next free = %u)",
2082 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2083 (unsigned long long)left_leaf_bh->b_blocknr,
2084 le16_to_cpu(left_el->l_next_free_rec));
2085 return -EROFS;
2086 }
2087
2088 /*
2089 * This extent block may already have an empty record, so we
2090 * return early if so.
2091 */
2092 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2093 return 0;
2094
2095 root_bh = left_path->p_node[subtree_index].bh;
2096 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2097
Joel Becker13723d02008-10-17 19:25:01 -07002098 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
2099 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002100 if (ret) {
2101 mlog_errno(ret);
2102 goto out;
2103 }
2104
2105 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker13723d02008-10-17 19:25:01 -07002106 ret = ocfs2_path_bh_journal_access(handle, inode,
2107 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002108 if (ret) {
2109 mlog_errno(ret);
2110 goto out;
2111 }
2112
Joel Becker13723d02008-10-17 19:25:01 -07002113 ret = ocfs2_path_bh_journal_access(handle, inode,
2114 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002115 if (ret) {
2116 mlog_errno(ret);
2117 goto out;
2118 }
2119 }
2120
2121 right_leaf_bh = path_leaf_bh(right_path);
2122 right_el = path_leaf_el(right_path);
2123
2124 /* This is a code error, not a disk corruption. */
2125 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2126 "because rightmost leaf block %llu is empty\n",
2127 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2128 (unsigned long long)right_leaf_bh->b_blocknr);
2129
2130 ocfs2_create_empty_extent(right_el);
2131
2132 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2133 if (ret) {
2134 mlog_errno(ret);
2135 goto out;
2136 }
2137
2138 /* Do the copy now. */
2139 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2140 move_rec = left_el->l_recs[i];
2141 right_el->l_recs[0] = move_rec;
2142
2143 /*
2144 * Clear out the record we just copied and shift everything
2145 * over, leaving an empty extent in the left leaf.
2146 *
2147 * We temporarily subtract from next_free_rec so that the
2148 * shift will lose the tail record (which is now defunct).
2149 */
2150 le16_add_cpu(&left_el->l_next_free_rec, -1);
2151 ocfs2_shift_records_right(left_el);
2152 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2153 le16_add_cpu(&left_el->l_next_free_rec, 1);
2154
2155 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2156 if (ret) {
2157 mlog_errno(ret);
2158 goto out;
2159 }
2160
2161 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2162 subtree_index);
2163
2164out:
2165 return ret;
2166}
2167
2168/*
2169 * Given a full path, determine what cpos value would return us a path
2170 * containing the leaf immediately to the left of the current one.
2171 *
2172 * Will return zero if the path passed in is already the leftmost path.
2173 */
2174static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2175 struct ocfs2_path *path, u32 *cpos)
2176{
2177 int i, j, ret = 0;
2178 u64 blkno;
2179 struct ocfs2_extent_list *el;
2180
Mark Fashehe48edee2007-03-07 16:46:57 -08002181 BUG_ON(path->p_tree_depth == 0);
2182
Mark Fashehdcd05382007-01-16 11:32:23 -08002183 *cpos = 0;
2184
2185 blkno = path_leaf_bh(path)->b_blocknr;
2186
2187 /* Start at the tree node just above the leaf and work our way up. */
2188 i = path->p_tree_depth - 1;
2189 while (i >= 0) {
2190 el = path->p_node[i].el;
2191
2192 /*
2193 * Find the extent record just before the one in our
2194 * path.
2195 */
2196 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2197 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2198 if (j == 0) {
2199 if (i == 0) {
2200 /*
2201 * We've determined that the
2202 * path specified is already
2203 * the leftmost one - return a
2204 * cpos of zero.
2205 */
2206 goto out;
2207 }
2208 /*
2209 * The leftmost record points to our
2210 * leaf - we need to travel up the
2211 * tree one level.
2212 */
2213 goto next_node;
2214 }
2215
2216 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002217 *cpos = *cpos + ocfs2_rec_clusters(el,
2218 &el->l_recs[j - 1]);
2219 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002220 goto out;
2221 }
2222 }
2223
2224 /*
2225 * If we got here, we never found a valid node where
2226 * the tree indicated one should be.
2227 */
2228 ocfs2_error(sb,
2229 "Invalid extent tree at extent block %llu\n",
2230 (unsigned long long)blkno);
2231 ret = -EROFS;
2232 goto out;
2233
2234next_node:
2235 blkno = path->p_node[i].bh->b_blocknr;
2236 i--;
2237 }
2238
2239out:
2240 return ret;
2241}
2242
Mark Fasheh328d5752007-06-18 10:48:04 -07002243/*
2244 * Extend the transaction by enough credits to complete the rotation,
2245 * and still leave at least the original number of credits allocated
2246 * to this transaction.
2247 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002248static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002249 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002250 struct ocfs2_path *path)
2251{
Mark Fasheh328d5752007-06-18 10:48:04 -07002252 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002253
2254 if (handle->h_buffer_credits < credits)
2255 return ocfs2_extend_trans(handle, credits);
2256
2257 return 0;
2258}
2259
2260/*
2261 * Trap the case where we're inserting into the theoretical range past
2262 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2263 * whose cpos is less than ours into the right leaf.
2264 *
2265 * It's only necessary to look at the rightmost record of the left
2266 * leaf because the logic that calls us should ensure that the
2267 * theoretical ranges in the path components above the leaves are
2268 * correct.
2269 */
2270static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2271 u32 insert_cpos)
2272{
2273 struct ocfs2_extent_list *left_el;
2274 struct ocfs2_extent_rec *rec;
2275 int next_free;
2276
2277 left_el = path_leaf_el(left_path);
2278 next_free = le16_to_cpu(left_el->l_next_free_rec);
2279 rec = &left_el->l_recs[next_free - 1];
2280
2281 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2282 return 1;
2283 return 0;
2284}
2285
Mark Fasheh328d5752007-06-18 10:48:04 -07002286static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2287{
2288 int next_free = le16_to_cpu(el->l_next_free_rec);
2289 unsigned int range;
2290 struct ocfs2_extent_rec *rec;
2291
2292 if (next_free == 0)
2293 return 0;
2294
2295 rec = &el->l_recs[0];
2296 if (ocfs2_is_empty_extent(rec)) {
2297 /* Empty list. */
2298 if (next_free == 1)
2299 return 0;
2300 rec = &el->l_recs[1];
2301 }
2302
2303 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2304 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2305 return 1;
2306 return 0;
2307}
2308
Mark Fashehdcd05382007-01-16 11:32:23 -08002309/*
2310 * Rotate all the records in a btree right one record, starting at insert_cpos.
2311 *
2312 * The path to the rightmost leaf should be passed in.
2313 *
2314 * The array is assumed to be large enough to hold an entire path (tree depth).
2315 *
2316 * Upon succesful return from this function:
2317 *
2318 * - The 'right_path' array will contain a path to the leaf block
2319 * whose range contains e_cpos.
2320 * - That leaf block will have a single empty extent in list index 0.
2321 * - In the case that the rotation requires a post-insert update,
2322 * *ret_left_path will contain a valid path which can be passed to
2323 * ocfs2_insert_path().
2324 */
2325static int ocfs2_rotate_tree_right(struct inode *inode,
2326 handle_t *handle,
Mark Fasheh328d5752007-06-18 10:48:04 -07002327 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002328 u32 insert_cpos,
2329 struct ocfs2_path *right_path,
2330 struct ocfs2_path **ret_left_path)
2331{
Mark Fasheh328d5752007-06-18 10:48:04 -07002332 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002333 u32 cpos;
2334 struct ocfs2_path *left_path = NULL;
2335
2336 *ret_left_path = NULL;
2337
Joel Beckerffdd7a52008-10-17 22:32:01 -07002338 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002339 if (!left_path) {
2340 ret = -ENOMEM;
2341 mlog_errno(ret);
2342 goto out;
2343 }
2344
2345 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2346 if (ret) {
2347 mlog_errno(ret);
2348 goto out;
2349 }
2350
2351 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2352
2353 /*
2354 * What we want to do here is:
2355 *
2356 * 1) Start with the rightmost path.
2357 *
2358 * 2) Determine a path to the leaf block directly to the left
2359 * of that leaf.
2360 *
2361 * 3) Determine the 'subtree root' - the lowest level tree node
2362 * which contains a path to both leaves.
2363 *
2364 * 4) Rotate the subtree.
2365 *
2366 * 5) Find the next subtree by considering the left path to be
2367 * the new right path.
2368 *
2369 * The check at the top of this while loop also accepts
2370 * insert_cpos == cpos because cpos is only a _theoretical_
2371 * value to get us the left path - insert_cpos might very well
2372 * be filling that hole.
2373 *
2374 * Stop at a cpos of '0' because we either started at the
2375 * leftmost branch (i.e., a tree with one branch and a
2376 * rotation inside of it), or we've gone as far as we can in
2377 * rotating subtrees.
2378 */
2379 while (cpos && insert_cpos <= cpos) {
2380 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2381 insert_cpos, cpos);
2382
2383 ret = ocfs2_find_path(inode, left_path, cpos);
2384 if (ret) {
2385 mlog_errno(ret);
2386 goto out;
2387 }
2388
2389 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2390 path_leaf_bh(right_path),
2391 "Inode %lu: error during insert of %u "
2392 "(left path cpos %u) results in two identical "
2393 "paths ending at %llu\n",
2394 inode->i_ino, insert_cpos, cpos,
2395 (unsigned long long)
2396 path_leaf_bh(left_path)->b_blocknr);
2397
Mark Fasheh328d5752007-06-18 10:48:04 -07002398 if (split == SPLIT_NONE &&
2399 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002400 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002401
2402 /*
2403 * We've rotated the tree as much as we
2404 * should. The rest is up to
2405 * ocfs2_insert_path() to complete, after the
2406 * record insertion. We indicate this
2407 * situation by returning the left path.
2408 *
2409 * The reason we don't adjust the records here
2410 * before the record insert is that an error
2411 * later might break the rule where a parent
2412 * record e_cpos will reflect the actual
2413 * e_cpos of the 1st nonempty record of the
2414 * child list.
2415 */
2416 *ret_left_path = left_path;
2417 goto out_ret_path;
2418 }
2419
2420 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2421
2422 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2423 start,
2424 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2425 right_path->p_tree_depth);
2426
2427 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002428 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002429 if (ret) {
2430 mlog_errno(ret);
2431 goto out;
2432 }
2433
2434 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2435 right_path, start);
2436 if (ret) {
2437 mlog_errno(ret);
2438 goto out;
2439 }
2440
Mark Fasheh328d5752007-06-18 10:48:04 -07002441 if (split != SPLIT_NONE &&
2442 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2443 insert_cpos)) {
2444 /*
2445 * A rotate moves the rightmost left leaf
2446 * record over to the leftmost right leaf
2447 * slot. If we're doing an extent split
2448 * instead of a real insert, then we have to
2449 * check that the extent to be split wasn't
2450 * just moved over. If it was, then we can
2451 * exit here, passing left_path back -
2452 * ocfs2_split_extent() is smart enough to
2453 * search both leaves.
2454 */
2455 *ret_left_path = left_path;
2456 goto out_ret_path;
2457 }
2458
Mark Fashehdcd05382007-01-16 11:32:23 -08002459 /*
2460 * There is no need to re-read the next right path
2461 * as we know that it'll be our current left
2462 * path. Optimize by copying values instead.
2463 */
2464 ocfs2_mv_path(right_path, left_path);
2465
2466 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2467 &cpos);
2468 if (ret) {
2469 mlog_errno(ret);
2470 goto out;
2471 }
2472 }
2473
2474out:
2475 ocfs2_free_path(left_path);
2476
2477out_ret_path:
2478 return ret;
2479}
2480
Tao Ma3c5e1062009-07-21 15:42:05 +08002481static int ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2482 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002483{
Tao Ma3c5e1062009-07-21 15:42:05 +08002484 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002485 struct ocfs2_extent_rec *rec;
2486 struct ocfs2_extent_list *el;
2487 struct ocfs2_extent_block *eb;
2488 u32 range;
2489
Tao Ma3c5e1062009-07-21 15:42:05 +08002490 /*
2491 * In normal tree rotation process, we will never touch the
2492 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2493 * doesn't reserve the credits for them either.
2494 *
2495 * But we do have a special case here which will update the rightmost
2496 * records for all the bh in the path.
2497 * So we have to allocate extra credits and access them.
2498 */
2499 ret = ocfs2_extend_trans(handle,
2500 handle->h_buffer_credits + subtree_index);
2501 if (ret) {
2502 mlog_errno(ret);
2503 goto out;
2504 }
2505
2506 ret = ocfs2_journal_access_path(inode, handle, path);
2507 if (ret) {
2508 mlog_errno(ret);
2509 goto out;
2510 }
2511
Mark Fasheh328d5752007-06-18 10:48:04 -07002512 /* Path should always be rightmost. */
2513 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2514 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2515
2516 el = &eb->h_list;
2517 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2518 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2519 rec = &el->l_recs[idx];
2520 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2521
2522 for (i = 0; i < path->p_tree_depth; i++) {
2523 el = path->p_node[i].el;
2524 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2525 rec = &el->l_recs[idx];
2526
2527 rec->e_int_clusters = cpu_to_le32(range);
2528 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2529
2530 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2531 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002532out:
2533 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002534}
2535
2536static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2537 struct ocfs2_cached_dealloc_ctxt *dealloc,
2538 struct ocfs2_path *path, int unlink_start)
2539{
2540 int ret, i;
2541 struct ocfs2_extent_block *eb;
2542 struct ocfs2_extent_list *el;
2543 struct buffer_head *bh;
2544
2545 for(i = unlink_start; i < path_num_items(path); i++) {
2546 bh = path->p_node[i].bh;
2547
2548 eb = (struct ocfs2_extent_block *)bh->b_data;
2549 /*
2550 * Not all nodes might have had their final count
2551 * decremented by the caller - handle this here.
2552 */
2553 el = &eb->h_list;
2554 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2555 mlog(ML_ERROR,
2556 "Inode %llu, attempted to remove extent block "
2557 "%llu with %u records\n",
2558 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2559 (unsigned long long)le64_to_cpu(eb->h_blkno),
2560 le16_to_cpu(el->l_next_free_rec));
2561
2562 ocfs2_journal_dirty(handle, bh);
Joel Becker8cb471e2009-02-10 20:00:41 -08002563 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002564 continue;
2565 }
2566
2567 el->l_next_free_rec = 0;
2568 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2569
2570 ocfs2_journal_dirty(handle, bh);
2571
2572 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2573 if (ret)
2574 mlog_errno(ret);
2575
Joel Becker8cb471e2009-02-10 20:00:41 -08002576 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002577 }
2578}
2579
2580static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2581 struct ocfs2_path *left_path,
2582 struct ocfs2_path *right_path,
2583 int subtree_index,
2584 struct ocfs2_cached_dealloc_ctxt *dealloc)
2585{
2586 int i;
2587 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2588 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2589 struct ocfs2_extent_list *el;
2590 struct ocfs2_extent_block *eb;
2591
2592 el = path_leaf_el(left_path);
2593
2594 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2595
2596 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2597 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2598 break;
2599
2600 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2601
2602 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2603 le16_add_cpu(&root_el->l_next_free_rec, -1);
2604
2605 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2606 eb->h_next_leaf_blk = 0;
2607
2608 ocfs2_journal_dirty(handle, root_bh);
2609 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2610
2611 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2612 subtree_index + 1);
2613}
2614
2615static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2616 struct ocfs2_path *left_path,
2617 struct ocfs2_path *right_path,
2618 int subtree_index,
2619 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002620 int *deleted,
2621 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002622{
2623 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002624 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002625 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2626 struct ocfs2_extent_block *eb;
2627
2628 *deleted = 0;
2629
2630 right_leaf_el = path_leaf_el(right_path);
2631 left_leaf_el = path_leaf_el(left_path);
2632 root_bh = left_path->p_node[subtree_index].bh;
2633 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2634
2635 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2636 return 0;
2637
2638 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2639 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2640 /*
2641 * It's legal for us to proceed if the right leaf is
2642 * the rightmost one and it has an empty extent. There
2643 * are two cases to handle - whether the leaf will be
2644 * empty after removal or not. If the leaf isn't empty
2645 * then just remove the empty extent up front. The
2646 * next block will handle empty leaves by flagging
2647 * them for unlink.
2648 *
2649 * Non rightmost leaves will throw -EAGAIN and the
2650 * caller can manually move the subtree and retry.
2651 */
2652
2653 if (eb->h_next_leaf_blk != 0ULL)
2654 return -EAGAIN;
2655
2656 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker13723d02008-10-17 19:25:01 -07002657 ret = ocfs2_journal_access_eb(handle, inode,
2658 path_leaf_bh(right_path),
2659 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002660 if (ret) {
2661 mlog_errno(ret);
2662 goto out;
2663 }
2664
2665 ocfs2_remove_empty_extent(right_leaf_el);
2666 } else
2667 right_has_empty = 1;
2668 }
2669
2670 if (eb->h_next_leaf_blk == 0ULL &&
2671 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2672 /*
2673 * We have to update i_last_eb_blk during the meta
2674 * data delete.
2675 */
Joel Becker13723d02008-10-17 19:25:01 -07002676 ret = ocfs2_et_root_journal_access(handle, inode, et,
2677 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002678 if (ret) {
2679 mlog_errno(ret);
2680 goto out;
2681 }
2682
2683 del_right_subtree = 1;
2684 }
2685
2686 /*
2687 * Getting here with an empty extent in the right path implies
2688 * that it's the rightmost path and will be deleted.
2689 */
2690 BUG_ON(right_has_empty && !del_right_subtree);
2691
Joel Becker13723d02008-10-17 19:25:01 -07002692 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
2693 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002694 if (ret) {
2695 mlog_errno(ret);
2696 goto out;
2697 }
2698
2699 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker13723d02008-10-17 19:25:01 -07002700 ret = ocfs2_path_bh_journal_access(handle, inode,
2701 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002702 if (ret) {
2703 mlog_errno(ret);
2704 goto out;
2705 }
2706
Joel Becker13723d02008-10-17 19:25:01 -07002707 ret = ocfs2_path_bh_journal_access(handle, inode,
2708 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002709 if (ret) {
2710 mlog_errno(ret);
2711 goto out;
2712 }
2713 }
2714
2715 if (!right_has_empty) {
2716 /*
2717 * Only do this if we're moving a real
2718 * record. Otherwise, the action is delayed until
2719 * after removal of the right path in which case we
2720 * can do a simple shift to remove the empty extent.
2721 */
2722 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2723 memset(&right_leaf_el->l_recs[0], 0,
2724 sizeof(struct ocfs2_extent_rec));
2725 }
2726 if (eb->h_next_leaf_blk == 0ULL) {
2727 /*
2728 * Move recs over to get rid of empty extent, decrease
2729 * next_free. This is allowed to remove the last
2730 * extent in our leaf (setting l_next_free_rec to
2731 * zero) - the delete code below won't care.
2732 */
2733 ocfs2_remove_empty_extent(right_leaf_el);
2734 }
2735
2736 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2737 if (ret)
2738 mlog_errno(ret);
2739 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2740 if (ret)
2741 mlog_errno(ret);
2742
2743 if (del_right_subtree) {
2744 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2745 subtree_index, dealloc);
Tao Ma3c5e1062009-07-21 15:42:05 +08002746 ret = ocfs2_update_edge_lengths(inode, handle, subtree_index,
2747 left_path);
2748 if (ret) {
2749 mlog_errno(ret);
2750 goto out;
2751 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002752
2753 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002754 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002755
2756 /*
2757 * Removal of the extent in the left leaf was skipped
2758 * above so we could delete the right path
2759 * 1st.
2760 */
2761 if (right_has_empty)
2762 ocfs2_remove_empty_extent(left_leaf_el);
2763
Tao Mae7d4cb62008-08-18 17:38:44 +08002764 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002765 if (ret)
2766 mlog_errno(ret);
2767
2768 *deleted = 1;
2769 } else
2770 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2771 subtree_index);
2772
2773out:
2774 return ret;
2775}
2776
2777/*
2778 * Given a full path, determine what cpos value would return us a path
2779 * containing the leaf immediately to the right of the current one.
2780 *
2781 * Will return zero if the path passed in is already the rightmost path.
2782 *
2783 * This looks similar, but is subtly different to
2784 * ocfs2_find_cpos_for_left_leaf().
2785 */
2786static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2787 struct ocfs2_path *path, u32 *cpos)
2788{
2789 int i, j, ret = 0;
2790 u64 blkno;
2791 struct ocfs2_extent_list *el;
2792
2793 *cpos = 0;
2794
2795 if (path->p_tree_depth == 0)
2796 return 0;
2797
2798 blkno = path_leaf_bh(path)->b_blocknr;
2799
2800 /* Start at the tree node just above the leaf and work our way up. */
2801 i = path->p_tree_depth - 1;
2802 while (i >= 0) {
2803 int next_free;
2804
2805 el = path->p_node[i].el;
2806
2807 /*
2808 * Find the extent record just after the one in our
2809 * path.
2810 */
2811 next_free = le16_to_cpu(el->l_next_free_rec);
2812 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2813 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2814 if (j == (next_free - 1)) {
2815 if (i == 0) {
2816 /*
2817 * We've determined that the
2818 * path specified is already
2819 * the rightmost one - return a
2820 * cpos of zero.
2821 */
2822 goto out;
2823 }
2824 /*
2825 * The rightmost record points to our
2826 * leaf - we need to travel up the
2827 * tree one level.
2828 */
2829 goto next_node;
2830 }
2831
2832 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2833 goto out;
2834 }
2835 }
2836
2837 /*
2838 * If we got here, we never found a valid node where
2839 * the tree indicated one should be.
2840 */
2841 ocfs2_error(sb,
2842 "Invalid extent tree at extent block %llu\n",
2843 (unsigned long long)blkno);
2844 ret = -EROFS;
2845 goto out;
2846
2847next_node:
2848 blkno = path->p_node[i].bh->b_blocknr;
2849 i--;
2850 }
2851
2852out:
2853 return ret;
2854}
2855
2856static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2857 handle_t *handle,
Joel Becker13723d02008-10-17 19:25:01 -07002858 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002859{
2860 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002861 struct buffer_head *bh = path_leaf_bh(path);
2862 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002863
2864 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2865 return 0;
2866
Joel Becker13723d02008-10-17 19:25:01 -07002867 ret = ocfs2_path_bh_journal_access(handle, inode, path,
2868 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002869 if (ret) {
2870 mlog_errno(ret);
2871 goto out;
2872 }
2873
2874 ocfs2_remove_empty_extent(el);
2875
2876 ret = ocfs2_journal_dirty(handle, bh);
2877 if (ret)
2878 mlog_errno(ret);
2879
2880out:
2881 return ret;
2882}
2883
2884static int __ocfs2_rotate_tree_left(struct inode *inode,
2885 handle_t *handle, int orig_credits,
2886 struct ocfs2_path *path,
2887 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002888 struct ocfs2_path **empty_extent_path,
2889 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002890{
2891 int ret, subtree_root, deleted;
2892 u32 right_cpos;
2893 struct ocfs2_path *left_path = NULL;
2894 struct ocfs2_path *right_path = NULL;
2895
2896 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2897
2898 *empty_extent_path = NULL;
2899
2900 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2901 &right_cpos);
2902 if (ret) {
2903 mlog_errno(ret);
2904 goto out;
2905 }
2906
Joel Beckerffdd7a52008-10-17 22:32:01 -07002907 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002908 if (!left_path) {
2909 ret = -ENOMEM;
2910 mlog_errno(ret);
2911 goto out;
2912 }
2913
2914 ocfs2_cp_path(left_path, path);
2915
Joel Beckerffdd7a52008-10-17 22:32:01 -07002916 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002917 if (!right_path) {
2918 ret = -ENOMEM;
2919 mlog_errno(ret);
2920 goto out;
2921 }
2922
2923 while (right_cpos) {
2924 ret = ocfs2_find_path(inode, right_path, right_cpos);
2925 if (ret) {
2926 mlog_errno(ret);
2927 goto out;
2928 }
2929
2930 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2931 right_path);
2932
2933 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2934 subtree_root,
2935 (unsigned long long)
2936 right_path->p_node[subtree_root].bh->b_blocknr,
2937 right_path->p_tree_depth);
2938
2939 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2940 orig_credits, left_path);
2941 if (ret) {
2942 mlog_errno(ret);
2943 goto out;
2944 }
2945
Mark Fashehe8aed342007-12-03 16:43:01 -08002946 /*
2947 * Caller might still want to make changes to the
2948 * tree root, so re-add it to the journal here.
2949 */
Joel Becker13723d02008-10-17 19:25:01 -07002950 ret = ocfs2_path_bh_journal_access(handle, inode,
2951 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08002952 if (ret) {
2953 mlog_errno(ret);
2954 goto out;
2955 }
2956
Mark Fasheh328d5752007-06-18 10:48:04 -07002957 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2958 right_path, subtree_root,
Tao Mae7d4cb62008-08-18 17:38:44 +08002959 dealloc, &deleted, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002960 if (ret == -EAGAIN) {
2961 /*
2962 * The rotation has to temporarily stop due to
2963 * the right subtree having an empty
2964 * extent. Pass it back to the caller for a
2965 * fixup.
2966 */
2967 *empty_extent_path = right_path;
2968 right_path = NULL;
2969 goto out;
2970 }
2971 if (ret) {
2972 mlog_errno(ret);
2973 goto out;
2974 }
2975
2976 /*
2977 * The subtree rotate might have removed records on
2978 * the rightmost edge. If so, then rotation is
2979 * complete.
2980 */
2981 if (deleted)
2982 break;
2983
2984 ocfs2_mv_path(left_path, right_path);
2985
2986 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2987 &right_cpos);
2988 if (ret) {
2989 mlog_errno(ret);
2990 goto out;
2991 }
2992 }
2993
2994out:
2995 ocfs2_free_path(right_path);
2996 ocfs2_free_path(left_path);
2997
2998 return ret;
2999}
3000
3001static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003002 struct ocfs2_path *path,
3003 struct ocfs2_cached_dealloc_ctxt *dealloc,
3004 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003005{
3006 int ret, subtree_index;
3007 u32 cpos;
3008 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003009 struct ocfs2_extent_block *eb;
3010 struct ocfs2_extent_list *el;
3011
Mark Fasheh328d5752007-06-18 10:48:04 -07003012
Joel Becker35dc0aa2008-08-20 16:25:06 -07003013 ret = ocfs2_et_sanity_check(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003014 if (ret)
3015 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003016 /*
3017 * There's two ways we handle this depending on
3018 * whether path is the only existing one.
3019 */
3020 ret = ocfs2_extend_rotate_transaction(handle, 0,
3021 handle->h_buffer_credits,
3022 path);
3023 if (ret) {
3024 mlog_errno(ret);
3025 goto out;
3026 }
3027
3028 ret = ocfs2_journal_access_path(inode, handle, path);
3029 if (ret) {
3030 mlog_errno(ret);
3031 goto out;
3032 }
3033
3034 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
3035 if (ret) {
3036 mlog_errno(ret);
3037 goto out;
3038 }
3039
3040 if (cpos) {
3041 /*
3042 * We have a path to the left of this one - it needs
3043 * an update too.
3044 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003045 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003046 if (!left_path) {
3047 ret = -ENOMEM;
3048 mlog_errno(ret);
3049 goto out;
3050 }
3051
3052 ret = ocfs2_find_path(inode, left_path, cpos);
3053 if (ret) {
3054 mlog_errno(ret);
3055 goto out;
3056 }
3057
3058 ret = ocfs2_journal_access_path(inode, handle, left_path);
3059 if (ret) {
3060 mlog_errno(ret);
3061 goto out;
3062 }
3063
3064 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
3065
3066 ocfs2_unlink_subtree(inode, handle, left_path, path,
3067 subtree_index, dealloc);
Tao Ma3c5e1062009-07-21 15:42:05 +08003068 ret = ocfs2_update_edge_lengths(inode, handle, subtree_index,
3069 left_path);
3070 if (ret) {
3071 mlog_errno(ret);
3072 goto out;
3073 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003074
3075 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003076 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003077 } else {
3078 /*
3079 * 'path' is also the leftmost path which
3080 * means it must be the only one. This gets
3081 * handled differently because we want to
3082 * revert the inode back to having extents
3083 * in-line.
3084 */
3085 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
3086
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003087 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003088 el->l_tree_depth = 0;
3089 el->l_next_free_rec = 0;
3090 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3091
Joel Becker35dc0aa2008-08-20 16:25:06 -07003092 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003093 }
3094
3095 ocfs2_journal_dirty(handle, path_root_bh(path));
3096
3097out:
3098 ocfs2_free_path(left_path);
3099 return ret;
3100}
3101
3102/*
3103 * Left rotation of btree records.
3104 *
3105 * In many ways, this is (unsurprisingly) the opposite of right
3106 * rotation. We start at some non-rightmost path containing an empty
3107 * extent in the leaf block. The code works its way to the rightmost
3108 * path by rotating records to the left in every subtree.
3109 *
3110 * This is used by any code which reduces the number of extent records
3111 * in a leaf. After removal, an empty record should be placed in the
3112 * leftmost list position.
3113 *
3114 * This won't handle a length update of the rightmost path records if
3115 * the rightmost tree leaf record is removed so the caller is
3116 * responsible for detecting and correcting that.
3117 */
3118static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
3119 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003120 struct ocfs2_cached_dealloc_ctxt *dealloc,
3121 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003122{
3123 int ret, orig_credits = handle->h_buffer_credits;
3124 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3125 struct ocfs2_extent_block *eb;
3126 struct ocfs2_extent_list *el;
3127
3128 el = path_leaf_el(path);
3129 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3130 return 0;
3131
3132 if (path->p_tree_depth == 0) {
3133rightmost_no_delete:
3134 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003135 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003136 * it up front.
3137 */
3138 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
Joel Becker13723d02008-10-17 19:25:01 -07003139 path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003140 if (ret)
3141 mlog_errno(ret);
3142 goto out;
3143 }
3144
3145 /*
3146 * Handle rightmost branch now. There's several cases:
3147 * 1) simple rotation leaving records in there. That's trivial.
3148 * 2) rotation requiring a branch delete - there's no more
3149 * records left. Two cases of this:
3150 * a) There are branches to the left.
3151 * b) This is also the leftmost (the only) branch.
3152 *
3153 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3154 * 2a) we need the left branch so that we can update it with the unlink
3155 * 2b) we need to bring the inode back to inline extents.
3156 */
3157
3158 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3159 el = &eb->h_list;
3160 if (eb->h_next_leaf_blk == 0) {
3161 /*
3162 * This gets a bit tricky if we're going to delete the
3163 * rightmost path. Get the other cases out of the way
3164 * 1st.
3165 */
3166 if (le16_to_cpu(el->l_next_free_rec) > 1)
3167 goto rightmost_no_delete;
3168
3169 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3170 ret = -EIO;
3171 ocfs2_error(inode->i_sb,
3172 "Inode %llu has empty extent block at %llu",
3173 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3174 (unsigned long long)le64_to_cpu(eb->h_blkno));
3175 goto out;
3176 }
3177
3178 /*
3179 * XXX: The caller can not trust "path" any more after
3180 * this as it will have been deleted. What do we do?
3181 *
3182 * In theory the rotate-for-merge code will never get
3183 * here because it'll always ask for a rotate in a
3184 * nonempty list.
3185 */
3186
3187 ret = ocfs2_remove_rightmost_path(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003188 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003189 if (ret)
3190 mlog_errno(ret);
3191 goto out;
3192 }
3193
3194 /*
3195 * Now we can loop, remembering the path we get from -EAGAIN
3196 * and restarting from there.
3197 */
3198try_rotate:
3199 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003200 dealloc, &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003201 if (ret && ret != -EAGAIN) {
3202 mlog_errno(ret);
3203 goto out;
3204 }
3205
3206 while (ret == -EAGAIN) {
3207 tmp_path = restart_path;
3208 restart_path = NULL;
3209
3210 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
3211 tmp_path, dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003212 &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003213 if (ret && ret != -EAGAIN) {
3214 mlog_errno(ret);
3215 goto out;
3216 }
3217
3218 ocfs2_free_path(tmp_path);
3219 tmp_path = NULL;
3220
3221 if (ret == 0)
3222 goto try_rotate;
3223 }
3224
3225out:
3226 ocfs2_free_path(tmp_path);
3227 ocfs2_free_path(restart_path);
3228 return ret;
3229}
3230
3231static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3232 int index)
3233{
3234 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3235 unsigned int size;
3236
3237 if (rec->e_leaf_clusters == 0) {
3238 /*
3239 * We consumed all of the merged-from record. An empty
3240 * extent cannot exist anywhere but the 1st array
3241 * position, so move things over if the merged-from
3242 * record doesn't occupy that position.
3243 *
3244 * This creates a new empty extent so the caller
3245 * should be smart enough to have removed any existing
3246 * ones.
3247 */
3248 if (index > 0) {
3249 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3250 size = index * sizeof(struct ocfs2_extent_rec);
3251 memmove(&el->l_recs[1], &el->l_recs[0], size);
3252 }
3253
3254 /*
3255 * Always memset - the caller doesn't check whether it
3256 * created an empty extent, so there could be junk in
3257 * the other fields.
3258 */
3259 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3260 }
3261}
3262
Tao Ma677b9752008-01-30 14:21:05 +08003263static int ocfs2_get_right_path(struct inode *inode,
3264 struct ocfs2_path *left_path,
3265 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003266{
3267 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003268 u32 right_cpos;
3269 struct ocfs2_path *right_path = NULL;
3270 struct ocfs2_extent_list *left_el;
3271
3272 *ret_right_path = NULL;
3273
3274 /* This function shouldn't be called for non-trees. */
3275 BUG_ON(left_path->p_tree_depth == 0);
3276
3277 left_el = path_leaf_el(left_path);
3278 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3279
3280 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3281 &right_cpos);
3282 if (ret) {
3283 mlog_errno(ret);
3284 goto out;
3285 }
3286
3287 /* This function shouldn't be called for the rightmost leaf. */
3288 BUG_ON(right_cpos == 0);
3289
Joel Beckerffdd7a52008-10-17 22:32:01 -07003290 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003291 if (!right_path) {
3292 ret = -ENOMEM;
3293 mlog_errno(ret);
3294 goto out;
3295 }
3296
3297 ret = ocfs2_find_path(inode, right_path, right_cpos);
3298 if (ret) {
3299 mlog_errno(ret);
3300 goto out;
3301 }
3302
3303 *ret_right_path = right_path;
3304out:
3305 if (ret)
3306 ocfs2_free_path(right_path);
3307 return ret;
3308}
3309
3310/*
3311 * Remove split_rec clusters from the record at index and merge them
3312 * onto the beginning of the record "next" to it.
3313 * For index < l_count - 1, the next means the extent rec at index + 1.
3314 * For index == l_count - 1, the "next" means the 1st extent rec of the
3315 * next extent block.
3316 */
3317static int ocfs2_merge_rec_right(struct inode *inode,
3318 struct ocfs2_path *left_path,
3319 handle_t *handle,
3320 struct ocfs2_extent_rec *split_rec,
3321 int index)
3322{
3323 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003324 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3325 struct ocfs2_extent_rec *left_rec;
3326 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003327 struct ocfs2_extent_list *right_el;
3328 struct ocfs2_path *right_path = NULL;
3329 int subtree_index = 0;
3330 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3331 struct buffer_head *bh = path_leaf_bh(left_path);
3332 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003333
3334 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003335 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003336
Al Viro9d8df6a2008-05-21 06:32:11 +01003337 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003338 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3339 /* we meet with a cross extent block merge. */
3340 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3341 if (ret) {
3342 mlog_errno(ret);
3343 goto out;
3344 }
3345
3346 right_el = path_leaf_el(right_path);
3347 next_free = le16_to_cpu(right_el->l_next_free_rec);
3348 BUG_ON(next_free <= 0);
3349 right_rec = &right_el->l_recs[0];
3350 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003351 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003352 right_rec = &right_el->l_recs[1];
3353 }
3354
3355 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3356 le16_to_cpu(left_rec->e_leaf_clusters) !=
3357 le32_to_cpu(right_rec->e_cpos));
3358
3359 subtree_index = ocfs2_find_subtree_root(inode,
3360 left_path, right_path);
3361
3362 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3363 handle->h_buffer_credits,
3364 right_path);
3365 if (ret) {
3366 mlog_errno(ret);
3367 goto out;
3368 }
3369
3370 root_bh = left_path->p_node[subtree_index].bh;
3371 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3372
Joel Becker13723d02008-10-17 19:25:01 -07003373 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
3374 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003375 if (ret) {
3376 mlog_errno(ret);
3377 goto out;
3378 }
3379
3380 for (i = subtree_index + 1;
3381 i < path_num_items(right_path); i++) {
Joel Becker13723d02008-10-17 19:25:01 -07003382 ret = ocfs2_path_bh_journal_access(handle, inode,
3383 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003384 if (ret) {
3385 mlog_errno(ret);
3386 goto out;
3387 }
3388
Joel Becker13723d02008-10-17 19:25:01 -07003389 ret = ocfs2_path_bh_journal_access(handle, inode,
3390 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003391 if (ret) {
3392 mlog_errno(ret);
3393 goto out;
3394 }
3395 }
3396
3397 } else {
3398 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3399 right_rec = &el->l_recs[index + 1];
3400 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003401
Joel Becker13723d02008-10-17 19:25:01 -07003402 ret = ocfs2_path_bh_journal_access(handle, inode, left_path,
3403 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003404 if (ret) {
3405 mlog_errno(ret);
3406 goto out;
3407 }
3408
3409 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3410
3411 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3412 le64_add_cpu(&right_rec->e_blkno,
3413 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3414 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3415
3416 ocfs2_cleanup_merge(el, index);
3417
3418 ret = ocfs2_journal_dirty(handle, bh);
3419 if (ret)
3420 mlog_errno(ret);
3421
Tao Ma677b9752008-01-30 14:21:05 +08003422 if (right_path) {
3423 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3424 if (ret)
3425 mlog_errno(ret);
3426
3427 ocfs2_complete_edge_insert(inode, handle, left_path,
3428 right_path, subtree_index);
3429 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003430out:
Tao Ma677b9752008-01-30 14:21:05 +08003431 if (right_path)
3432 ocfs2_free_path(right_path);
3433 return ret;
3434}
3435
3436static int ocfs2_get_left_path(struct inode *inode,
3437 struct ocfs2_path *right_path,
3438 struct ocfs2_path **ret_left_path)
3439{
3440 int ret;
3441 u32 left_cpos;
3442 struct ocfs2_path *left_path = NULL;
3443
3444 *ret_left_path = NULL;
3445
3446 /* This function shouldn't be called for non-trees. */
3447 BUG_ON(right_path->p_tree_depth == 0);
3448
3449 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3450 right_path, &left_cpos);
3451 if (ret) {
3452 mlog_errno(ret);
3453 goto out;
3454 }
3455
3456 /* This function shouldn't be called for the leftmost leaf. */
3457 BUG_ON(left_cpos == 0);
3458
Joel Beckerffdd7a52008-10-17 22:32:01 -07003459 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003460 if (!left_path) {
3461 ret = -ENOMEM;
3462 mlog_errno(ret);
3463 goto out;
3464 }
3465
3466 ret = ocfs2_find_path(inode, left_path, left_cpos);
3467 if (ret) {
3468 mlog_errno(ret);
3469 goto out;
3470 }
3471
3472 *ret_left_path = left_path;
3473out:
3474 if (ret)
3475 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003476 return ret;
3477}
3478
3479/*
3480 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003481 * onto the tail of the record "before" it.
3482 * For index > 0, the "before" means the extent rec at index - 1.
3483 *
3484 * For index == 0, the "before" means the last record of the previous
3485 * extent block. And there is also a situation that we may need to
3486 * remove the rightmost leaf extent block in the right_path and change
3487 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003488 */
Tao Ma677b9752008-01-30 14:21:05 +08003489static int ocfs2_merge_rec_left(struct inode *inode,
3490 struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003491 handle_t *handle,
3492 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003493 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003494 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003495 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003496{
Tao Ma677b9752008-01-30 14:21:05 +08003497 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003498 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3499 struct ocfs2_extent_rec *left_rec;
3500 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003501 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3502 struct buffer_head *bh = path_leaf_bh(right_path);
3503 struct buffer_head *root_bh = NULL;
3504 struct ocfs2_path *left_path = NULL;
3505 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003506
Tao Ma677b9752008-01-30 14:21:05 +08003507 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003508
Mark Fasheh328d5752007-06-18 10:48:04 -07003509 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003510 if (index == 0) {
3511 /* we meet with a cross extent block merge. */
3512 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3513 if (ret) {
3514 mlog_errno(ret);
3515 goto out;
3516 }
3517
3518 left_el = path_leaf_el(left_path);
3519 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3520 le16_to_cpu(left_el->l_count));
3521
3522 left_rec = &left_el->l_recs[
3523 le16_to_cpu(left_el->l_next_free_rec) - 1];
3524 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3525 le16_to_cpu(left_rec->e_leaf_clusters) !=
3526 le32_to_cpu(split_rec->e_cpos));
3527
3528 subtree_index = ocfs2_find_subtree_root(inode,
3529 left_path, right_path);
3530
3531 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3532 handle->h_buffer_credits,
3533 left_path);
3534 if (ret) {
3535 mlog_errno(ret);
3536 goto out;
3537 }
3538
3539 root_bh = left_path->p_node[subtree_index].bh;
3540 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3541
Joel Becker13723d02008-10-17 19:25:01 -07003542 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
3543 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003544 if (ret) {
3545 mlog_errno(ret);
3546 goto out;
3547 }
3548
3549 for (i = subtree_index + 1;
3550 i < path_num_items(right_path); i++) {
Joel Becker13723d02008-10-17 19:25:01 -07003551 ret = ocfs2_path_bh_journal_access(handle, inode,
3552 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003553 if (ret) {
3554 mlog_errno(ret);
3555 goto out;
3556 }
3557
Joel Becker13723d02008-10-17 19:25:01 -07003558 ret = ocfs2_path_bh_journal_access(handle, inode,
3559 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003560 if (ret) {
3561 mlog_errno(ret);
3562 goto out;
3563 }
3564 }
3565 } else {
3566 left_rec = &el->l_recs[index - 1];
3567 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3568 has_empty_extent = 1;
3569 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003570
Tao Ma9047bea2009-01-05 14:45:24 +08003571 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
3572 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003573 if (ret) {
3574 mlog_errno(ret);
3575 goto out;
3576 }
3577
3578 if (has_empty_extent && index == 1) {
3579 /*
3580 * The easy case - we can just plop the record right in.
3581 */
3582 *left_rec = *split_rec;
3583
3584 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003585 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003586 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003587
3588 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3589 le64_add_cpu(&right_rec->e_blkno,
3590 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3591 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3592
3593 ocfs2_cleanup_merge(el, index);
3594
3595 ret = ocfs2_journal_dirty(handle, bh);
3596 if (ret)
3597 mlog_errno(ret);
3598
Tao Ma677b9752008-01-30 14:21:05 +08003599 if (left_path) {
3600 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3601 if (ret)
3602 mlog_errno(ret);
3603
3604 /*
3605 * In the situation that the right_rec is empty and the extent
3606 * block is empty also, ocfs2_complete_edge_insert can't handle
3607 * it and we need to delete the right extent block.
3608 */
3609 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3610 le16_to_cpu(el->l_next_free_rec) == 1) {
3611
3612 ret = ocfs2_remove_rightmost_path(inode, handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003613 right_path,
3614 dealloc, et);
Tao Ma677b9752008-01-30 14:21:05 +08003615 if (ret) {
3616 mlog_errno(ret);
3617 goto out;
3618 }
3619
3620 /* Now the rightmost extent block has been deleted.
3621 * So we use the new rightmost path.
3622 */
3623 ocfs2_mv_path(right_path, left_path);
3624 left_path = NULL;
3625 } else
3626 ocfs2_complete_edge_insert(inode, handle, left_path,
3627 right_path, subtree_index);
3628 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003629out:
Tao Ma677b9752008-01-30 14:21:05 +08003630 if (left_path)
3631 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003632 return ret;
3633}
3634
3635static int ocfs2_try_to_merge_extent(struct inode *inode,
3636 handle_t *handle,
Tao Ma677b9752008-01-30 14:21:05 +08003637 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003638 int split_index,
3639 struct ocfs2_extent_rec *split_rec,
3640 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003641 struct ocfs2_merge_ctxt *ctxt,
3642 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003643
3644{
Tao Mao518d7262007-08-28 17:25:35 -07003645 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003646 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003647 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3648
3649 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3650
Tao Mao518d7262007-08-28 17:25:35 -07003651 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3652 /*
3653 * The merge code will need to create an empty
3654 * extent to take the place of the newly
3655 * emptied slot. Remove any pre-existing empty
3656 * extents - having more than one in a leaf is
3657 * illegal.
3658 */
Tao Ma677b9752008-01-30 14:21:05 +08003659 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003660 dealloc, et);
Tao Mao518d7262007-08-28 17:25:35 -07003661 if (ret) {
3662 mlog_errno(ret);
3663 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003664 }
Tao Mao518d7262007-08-28 17:25:35 -07003665 split_index--;
3666 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003667 }
3668
3669 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3670 /*
3671 * Left-right contig implies this.
3672 */
3673 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003674
3675 /*
3676 * Since the leftright insert always covers the entire
3677 * extent, this call will delete the insert record
3678 * entirely, resulting in an empty extent record added to
3679 * the extent block.
3680 *
3681 * Since the adding of an empty extent shifts
3682 * everything back to the right, there's no need to
3683 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003684 *
3685 * When the split_index is zero, we need to merge it to the
3686 * prevoius extent block. It is more efficient and easier
3687 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003688 */
Tao Ma677b9752008-01-30 14:21:05 +08003689 ret = ocfs2_merge_rec_right(inode, path,
3690 handle, split_rec,
3691 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003692 if (ret) {
3693 mlog_errno(ret);
3694 goto out;
3695 }
3696
3697 /*
3698 * We can only get this from logic error above.
3699 */
3700 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3701
Tao Ma677b9752008-01-30 14:21:05 +08003702 /* The merge left us with an empty extent, remove it. */
Tao Mae7d4cb62008-08-18 17:38:44 +08003703 ret = ocfs2_rotate_tree_left(inode, handle, path,
3704 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003705 if (ret) {
3706 mlog_errno(ret);
3707 goto out;
3708 }
Tao Ma677b9752008-01-30 14:21:05 +08003709
Mark Fasheh328d5752007-06-18 10:48:04 -07003710 rec = &el->l_recs[split_index];
3711
3712 /*
3713 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003714 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003715 */
Tao Ma677b9752008-01-30 14:21:05 +08003716 ret = ocfs2_merge_rec_left(inode, path,
3717 handle, rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003718 dealloc, et,
Tao Ma677b9752008-01-30 14:21:05 +08003719 split_index);
3720
Mark Fasheh328d5752007-06-18 10:48:04 -07003721 if (ret) {
3722 mlog_errno(ret);
3723 goto out;
3724 }
3725
Tao Ma677b9752008-01-30 14:21:05 +08003726 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003727 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003728 /*
3729 * Error from this last rotate is not critical, so
3730 * print but don't bubble it up.
3731 */
3732 if (ret)
3733 mlog_errno(ret);
3734 ret = 0;
3735 } else {
3736 /*
3737 * Merge a record to the left or right.
3738 *
3739 * 'contig_type' is relative to the existing record,
3740 * so for example, if we're "right contig", it's to
3741 * the record on the left (hence the left merge).
3742 */
3743 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3744 ret = ocfs2_merge_rec_left(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003745 path,
3746 handle, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003747 dealloc, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003748 split_index);
3749 if (ret) {
3750 mlog_errno(ret);
3751 goto out;
3752 }
3753 } else {
3754 ret = ocfs2_merge_rec_right(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003755 path,
3756 handle, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003757 split_index);
3758 if (ret) {
3759 mlog_errno(ret);
3760 goto out;
3761 }
3762 }
3763
3764 if (ctxt->c_split_covers_rec) {
3765 /*
3766 * The merge may have left an empty extent in
3767 * our leaf. Try to rotate it away.
3768 */
Tao Ma677b9752008-01-30 14:21:05 +08003769 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003770 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003771 if (ret)
3772 mlog_errno(ret);
3773 ret = 0;
3774 }
3775 }
3776
3777out:
3778 return ret;
3779}
3780
3781static void ocfs2_subtract_from_rec(struct super_block *sb,
3782 enum ocfs2_split_type split,
3783 struct ocfs2_extent_rec *rec,
3784 struct ocfs2_extent_rec *split_rec)
3785{
3786 u64 len_blocks;
3787
3788 len_blocks = ocfs2_clusters_to_blocks(sb,
3789 le16_to_cpu(split_rec->e_leaf_clusters));
3790
3791 if (split == SPLIT_LEFT) {
3792 /*
3793 * Region is on the left edge of the existing
3794 * record.
3795 */
3796 le32_add_cpu(&rec->e_cpos,
3797 le16_to_cpu(split_rec->e_leaf_clusters));
3798 le64_add_cpu(&rec->e_blkno, len_blocks);
3799 le16_add_cpu(&rec->e_leaf_clusters,
3800 -le16_to_cpu(split_rec->e_leaf_clusters));
3801 } else {
3802 /*
3803 * Region is on the right edge of the existing
3804 * record.
3805 */
3806 le16_add_cpu(&rec->e_leaf_clusters,
3807 -le16_to_cpu(split_rec->e_leaf_clusters));
3808 }
3809}
3810
Mark Fashehdcd05382007-01-16 11:32:23 -08003811/*
3812 * Do the final bits of extent record insertion at the target leaf
3813 * list. If this leaf is part of an allocation tree, it is assumed
3814 * that the tree above has been prepared.
3815 */
3816static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3817 struct ocfs2_extent_list *el,
3818 struct ocfs2_insert_type *insert,
3819 struct inode *inode)
3820{
3821 int i = insert->ins_contig_index;
3822 unsigned int range;
3823 struct ocfs2_extent_rec *rec;
3824
Mark Fashehe48edee2007-03-07 16:46:57 -08003825 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003826
Mark Fasheh328d5752007-06-18 10:48:04 -07003827 if (insert->ins_split != SPLIT_NONE) {
3828 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3829 BUG_ON(i == -1);
3830 rec = &el->l_recs[i];
3831 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3832 insert_rec);
3833 goto rotate;
3834 }
3835
Mark Fashehdcd05382007-01-16 11:32:23 -08003836 /*
3837 * Contiguous insert - either left or right.
3838 */
3839 if (insert->ins_contig != CONTIG_NONE) {
3840 rec = &el->l_recs[i];
3841 if (insert->ins_contig == CONTIG_LEFT) {
3842 rec->e_blkno = insert_rec->e_blkno;
3843 rec->e_cpos = insert_rec->e_cpos;
3844 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003845 le16_add_cpu(&rec->e_leaf_clusters,
3846 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003847 return;
3848 }
3849
3850 /*
3851 * Handle insert into an empty leaf.
3852 */
3853 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3854 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3855 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3856 el->l_recs[0] = *insert_rec;
3857 el->l_next_free_rec = cpu_to_le16(1);
3858 return;
3859 }
3860
3861 /*
3862 * Appending insert.
3863 */
3864 if (insert->ins_appending == APPEND_TAIL) {
3865 i = le16_to_cpu(el->l_next_free_rec) - 1;
3866 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003867 range = le32_to_cpu(rec->e_cpos)
3868 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003869 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3870
3871 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3872 le16_to_cpu(el->l_count),
3873 "inode %lu, depth %u, count %u, next free %u, "
3874 "rec.cpos %u, rec.clusters %u, "
3875 "insert.cpos %u, insert.clusters %u\n",
3876 inode->i_ino,
3877 le16_to_cpu(el->l_tree_depth),
3878 le16_to_cpu(el->l_count),
3879 le16_to_cpu(el->l_next_free_rec),
3880 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003881 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003882 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003883 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003884 i++;
3885 el->l_recs[i] = *insert_rec;
3886 le16_add_cpu(&el->l_next_free_rec, 1);
3887 return;
3888 }
3889
Mark Fasheh328d5752007-06-18 10:48:04 -07003890rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003891 /*
3892 * Ok, we have to rotate.
3893 *
3894 * At this point, it is safe to assume that inserting into an
3895 * empty leaf and appending to a leaf have both been handled
3896 * above.
3897 *
3898 * This leaf needs to have space, either by the empty 1st
3899 * extent record, or by virtue of an l_next_rec < l_count.
3900 */
3901 ocfs2_rotate_leaf(el, insert_rec);
3902}
3903
Mark Fasheh328d5752007-06-18 10:48:04 -07003904static void ocfs2_adjust_rightmost_records(struct inode *inode,
3905 handle_t *handle,
3906 struct ocfs2_path *path,
3907 struct ocfs2_extent_rec *insert_rec)
3908{
3909 int ret, i, next_free;
3910 struct buffer_head *bh;
3911 struct ocfs2_extent_list *el;
3912 struct ocfs2_extent_rec *rec;
3913
3914 /*
3915 * Update everything except the leaf block.
3916 */
3917 for (i = 0; i < path->p_tree_depth; i++) {
3918 bh = path->p_node[i].bh;
3919 el = path->p_node[i].el;
3920
3921 next_free = le16_to_cpu(el->l_next_free_rec);
3922 if (next_free == 0) {
3923 ocfs2_error(inode->i_sb,
3924 "Dinode %llu has a bad extent list",
3925 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3926 ret = -EIO;
3927 return;
3928 }
3929
3930 rec = &el->l_recs[next_free - 1];
3931
3932 rec->e_int_clusters = insert_rec->e_cpos;
3933 le32_add_cpu(&rec->e_int_clusters,
3934 le16_to_cpu(insert_rec->e_leaf_clusters));
3935 le32_add_cpu(&rec->e_int_clusters,
3936 -le32_to_cpu(rec->e_cpos));
3937
3938 ret = ocfs2_journal_dirty(handle, bh);
3939 if (ret)
3940 mlog_errno(ret);
3941
3942 }
3943}
3944
Mark Fashehdcd05382007-01-16 11:32:23 -08003945static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3946 struct ocfs2_extent_rec *insert_rec,
3947 struct ocfs2_path *right_path,
3948 struct ocfs2_path **ret_left_path)
3949{
Mark Fasheh328d5752007-06-18 10:48:04 -07003950 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003951 struct ocfs2_extent_list *el;
3952 struct ocfs2_path *left_path = NULL;
3953
3954 *ret_left_path = NULL;
3955
3956 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003957 * This shouldn't happen for non-trees. The extent rec cluster
3958 * count manipulation below only works for interior nodes.
3959 */
3960 BUG_ON(right_path->p_tree_depth == 0);
3961
3962 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003963 * If our appending insert is at the leftmost edge of a leaf,
3964 * then we might need to update the rightmost records of the
3965 * neighboring path.
3966 */
3967 el = path_leaf_el(right_path);
3968 next_free = le16_to_cpu(el->l_next_free_rec);
3969 if (next_free == 0 ||
3970 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3971 u32 left_cpos;
3972
3973 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3974 &left_cpos);
3975 if (ret) {
3976 mlog_errno(ret);
3977 goto out;
3978 }
3979
3980 mlog(0, "Append may need a left path update. cpos: %u, "
3981 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3982 left_cpos);
3983
3984 /*
3985 * No need to worry if the append is already in the
3986 * leftmost leaf.
3987 */
3988 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07003989 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003990 if (!left_path) {
3991 ret = -ENOMEM;
3992 mlog_errno(ret);
3993 goto out;
3994 }
3995
3996 ret = ocfs2_find_path(inode, left_path, left_cpos);
3997 if (ret) {
3998 mlog_errno(ret);
3999 goto out;
4000 }
4001
4002 /*
4003 * ocfs2_insert_path() will pass the left_path to the
4004 * journal for us.
4005 */
4006 }
4007 }
4008
4009 ret = ocfs2_journal_access_path(inode, handle, right_path);
4010 if (ret) {
4011 mlog_errno(ret);
4012 goto out;
4013 }
4014
Mark Fasheh328d5752007-06-18 10:48:04 -07004015 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004016
4017 *ret_left_path = left_path;
4018 ret = 0;
4019out:
4020 if (ret != 0)
4021 ocfs2_free_path(left_path);
4022
4023 return ret;
4024}
4025
Mark Fasheh328d5752007-06-18 10:48:04 -07004026static void ocfs2_split_record(struct inode *inode,
4027 struct ocfs2_path *left_path,
4028 struct ocfs2_path *right_path,
4029 struct ocfs2_extent_rec *split_rec,
4030 enum ocfs2_split_type split)
4031{
4032 int index;
4033 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4034 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4035 struct ocfs2_extent_rec *rec, *tmprec;
4036
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004037 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004038 if (left_path)
4039 left_el = path_leaf_el(left_path);
4040
4041 el = right_el;
4042 insert_el = right_el;
4043 index = ocfs2_search_extent_list(el, cpos);
4044 if (index != -1) {
4045 if (index == 0 && left_path) {
4046 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4047
4048 /*
4049 * This typically means that the record
4050 * started in the left path but moved to the
4051 * right as a result of rotation. We either
4052 * move the existing record to the left, or we
4053 * do the later insert there.
4054 *
4055 * In this case, the left path should always
4056 * exist as the rotate code will have passed
4057 * it back for a post-insert update.
4058 */
4059
4060 if (split == SPLIT_LEFT) {
4061 /*
4062 * It's a left split. Since we know
4063 * that the rotate code gave us an
4064 * empty extent in the left path, we
4065 * can just do the insert there.
4066 */
4067 insert_el = left_el;
4068 } else {
4069 /*
4070 * Right split - we have to move the
4071 * existing record over to the left
4072 * leaf. The insert will be into the
4073 * newly created empty extent in the
4074 * right leaf.
4075 */
4076 tmprec = &right_el->l_recs[index];
4077 ocfs2_rotate_leaf(left_el, tmprec);
4078 el = left_el;
4079
4080 memset(tmprec, 0, sizeof(*tmprec));
4081 index = ocfs2_search_extent_list(left_el, cpos);
4082 BUG_ON(index == -1);
4083 }
4084 }
4085 } else {
4086 BUG_ON(!left_path);
4087 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4088 /*
4089 * Left path is easy - we can just allow the insert to
4090 * happen.
4091 */
4092 el = left_el;
4093 insert_el = left_el;
4094 index = ocfs2_search_extent_list(el, cpos);
4095 BUG_ON(index == -1);
4096 }
4097
4098 rec = &el->l_recs[index];
4099 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
4100 ocfs2_rotate_leaf(insert_el, split_rec);
4101}
4102
Mark Fashehdcd05382007-01-16 11:32:23 -08004103/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004104 * This function only does inserts on an allocation b-tree. For tree
4105 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004106 *
4107 * right_path is the path we want to do the actual insert
4108 * in. left_path should only be passed in if we need to update that
4109 * portion of the tree after an edge insert.
4110 */
4111static int ocfs2_insert_path(struct inode *inode,
4112 handle_t *handle,
4113 struct ocfs2_path *left_path,
4114 struct ocfs2_path *right_path,
4115 struct ocfs2_extent_rec *insert_rec,
4116 struct ocfs2_insert_type *insert)
4117{
4118 int ret, subtree_index;
4119 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004120
Mark Fashehdcd05382007-01-16 11:32:23 -08004121 if (left_path) {
4122 int credits = handle->h_buffer_credits;
4123
4124 /*
4125 * There's a chance that left_path got passed back to
4126 * us without being accounted for in the
4127 * journal. Extend our transaction here to be sure we
4128 * can change those blocks.
4129 */
4130 credits += left_path->p_tree_depth;
4131
4132 ret = ocfs2_extend_trans(handle, credits);
4133 if (ret < 0) {
4134 mlog_errno(ret);
4135 goto out;
4136 }
4137
4138 ret = ocfs2_journal_access_path(inode, handle, left_path);
4139 if (ret < 0) {
4140 mlog_errno(ret);
4141 goto out;
4142 }
4143 }
4144
Mark Fashehe8aed342007-12-03 16:43:01 -08004145 /*
4146 * Pass both paths to the journal. The majority of inserts
4147 * will be touching all components anyway.
4148 */
4149 ret = ocfs2_journal_access_path(inode, handle, right_path);
4150 if (ret < 0) {
4151 mlog_errno(ret);
4152 goto out;
4153 }
4154
Mark Fasheh328d5752007-06-18 10:48:04 -07004155 if (insert->ins_split != SPLIT_NONE) {
4156 /*
4157 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004158 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004159 * function sort it all out.
4160 */
4161 ocfs2_split_record(inode, left_path, right_path,
4162 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004163
4164 /*
4165 * Split might have modified either leaf and we don't
4166 * have a guarantee that the later edge insert will
4167 * dirty this for us.
4168 */
4169 if (left_path)
4170 ret = ocfs2_journal_dirty(handle,
4171 path_leaf_bh(left_path));
4172 if (ret)
4173 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07004174 } else
4175 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
4176 insert, inode);
Mark Fashehdcd05382007-01-16 11:32:23 -08004177
Mark Fashehdcd05382007-01-16 11:32:23 -08004178 ret = ocfs2_journal_dirty(handle, leaf_bh);
4179 if (ret)
4180 mlog_errno(ret);
4181
4182 if (left_path) {
4183 /*
4184 * The rotate code has indicated that we need to fix
4185 * up portions of the tree after the insert.
4186 *
4187 * XXX: Should we extend the transaction here?
4188 */
4189 subtree_index = ocfs2_find_subtree_root(inode, left_path,
4190 right_path);
4191 ocfs2_complete_edge_insert(inode, handle, left_path,
4192 right_path, subtree_index);
4193 }
4194
4195 ret = 0;
4196out:
4197 return ret;
4198}
4199
4200static int ocfs2_do_insert_extent(struct inode *inode,
4201 handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004202 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004203 struct ocfs2_extent_rec *insert_rec,
4204 struct ocfs2_insert_type *type)
4205{
4206 int ret, rotate = 0;
4207 u32 cpos;
4208 struct ocfs2_path *right_path = NULL;
4209 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004210 struct ocfs2_extent_list *el;
4211
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004212 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004213
Joel Becker13723d02008-10-17 19:25:01 -07004214 ret = ocfs2_et_root_journal_access(handle, inode, et,
4215 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004216 if (ret) {
4217 mlog_errno(ret);
4218 goto out;
4219 }
4220
4221 if (le16_to_cpu(el->l_tree_depth) == 0) {
4222 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4223 goto out_update_clusters;
4224 }
4225
Joel Beckerffdd7a52008-10-17 22:32:01 -07004226 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004227 if (!right_path) {
4228 ret = -ENOMEM;
4229 mlog_errno(ret);
4230 goto out;
4231 }
4232
4233 /*
4234 * Determine the path to start with. Rotations need the
4235 * rightmost path, everything else can go directly to the
4236 * target leaf.
4237 */
4238 cpos = le32_to_cpu(insert_rec->e_cpos);
4239 if (type->ins_appending == APPEND_NONE &&
4240 type->ins_contig == CONTIG_NONE) {
4241 rotate = 1;
4242 cpos = UINT_MAX;
4243 }
4244
4245 ret = ocfs2_find_path(inode, right_path, cpos);
4246 if (ret) {
4247 mlog_errno(ret);
4248 goto out;
4249 }
4250
4251 /*
4252 * Rotations and appends need special treatment - they modify
4253 * parts of the tree's above them.
4254 *
4255 * Both might pass back a path immediate to the left of the
4256 * one being inserted to. This will be cause
4257 * ocfs2_insert_path() to modify the rightmost records of
4258 * left_path to account for an edge insert.
4259 *
4260 * XXX: When modifying this code, keep in mind that an insert
4261 * can wind up skipping both of these two special cases...
4262 */
4263 if (rotate) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004264 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004265 le32_to_cpu(insert_rec->e_cpos),
4266 right_path, &left_path);
4267 if (ret) {
4268 mlog_errno(ret);
4269 goto out;
4270 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004271
4272 /*
4273 * ocfs2_rotate_tree_right() might have extended the
4274 * transaction without re-journaling our tree root.
4275 */
Joel Becker13723d02008-10-17 19:25:01 -07004276 ret = ocfs2_et_root_journal_access(handle, inode, et,
4277 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004278 if (ret) {
4279 mlog_errno(ret);
4280 goto out;
4281 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004282 } else if (type->ins_appending == APPEND_TAIL
4283 && type->ins_contig != CONTIG_LEFT) {
4284 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4285 right_path, &left_path);
4286 if (ret) {
4287 mlog_errno(ret);
4288 goto out;
4289 }
4290 }
4291
4292 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4293 insert_rec, type);
4294 if (ret) {
4295 mlog_errno(ret);
4296 goto out;
4297 }
4298
4299out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004300 if (type->ins_split == SPLIT_NONE)
Joel Becker35dc0aa2008-08-20 16:25:06 -07004301 ocfs2_et_update_clusters(inode, et,
4302 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004303
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004304 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004305 if (ret)
4306 mlog_errno(ret);
4307
4308out:
4309 ocfs2_free_path(left_path);
4310 ocfs2_free_path(right_path);
4311
4312 return ret;
4313}
4314
Mark Fasheh328d5752007-06-18 10:48:04 -07004315static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08004316ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004317 struct ocfs2_extent_list *el, int index,
4318 struct ocfs2_extent_rec *split_rec)
4319{
Tao Maad5a4d72008-01-30 14:21:32 +08004320 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004321 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004322 u32 left_cpos, right_cpos;
4323 struct ocfs2_extent_rec *rec = NULL;
4324 struct ocfs2_extent_list *new_el;
4325 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4326 struct buffer_head *bh;
4327 struct ocfs2_extent_block *eb;
4328
4329 if (index > 0) {
4330 rec = &el->l_recs[index - 1];
4331 } else if (path->p_tree_depth > 0) {
4332 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4333 path, &left_cpos);
4334 if (status)
4335 goto out;
4336
4337 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004338 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004339 if (!left_path)
4340 goto out;
4341
4342 status = ocfs2_find_path(inode, left_path, left_cpos);
4343 if (status)
4344 goto out;
4345
4346 new_el = path_leaf_el(left_path);
4347
4348 if (le16_to_cpu(new_el->l_next_free_rec) !=
4349 le16_to_cpu(new_el->l_count)) {
4350 bh = path_leaf_bh(left_path);
4351 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004352 ocfs2_error(inode->i_sb,
4353 "Extent block #%llu has an "
4354 "invalid l_next_free_rec of "
4355 "%d. It should have "
4356 "matched the l_count of %d",
4357 (unsigned long long)le64_to_cpu(eb->h_blkno),
4358 le16_to_cpu(new_el->l_next_free_rec),
4359 le16_to_cpu(new_el->l_count));
4360 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004361 goto out;
4362 }
4363 rec = &new_el->l_recs[
4364 le16_to_cpu(new_el->l_next_free_rec) - 1];
4365 }
4366 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004367
4368 /*
4369 * We're careful to check for an empty extent record here -
4370 * the merge code will know what to do if it sees one.
4371 */
Tao Maad5a4d72008-01-30 14:21:32 +08004372 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004373 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4374 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4375 ret = CONTIG_RIGHT;
4376 } else {
4377 ret = ocfs2_extent_contig(inode, rec, split_rec);
4378 }
4379 }
4380
Tao Maad5a4d72008-01-30 14:21:32 +08004381 rec = NULL;
4382 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4383 rec = &el->l_recs[index + 1];
4384 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4385 path->p_tree_depth > 0) {
4386 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4387 path, &right_cpos);
4388 if (status)
4389 goto out;
4390
4391 if (right_cpos == 0)
4392 goto out;
4393
Joel Beckerffdd7a52008-10-17 22:32:01 -07004394 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004395 if (!right_path)
4396 goto out;
4397
4398 status = ocfs2_find_path(inode, right_path, right_cpos);
4399 if (status)
4400 goto out;
4401
4402 new_el = path_leaf_el(right_path);
4403 rec = &new_el->l_recs[0];
4404 if (ocfs2_is_empty_extent(rec)) {
4405 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4406 bh = path_leaf_bh(right_path);
4407 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004408 ocfs2_error(inode->i_sb,
4409 "Extent block #%llu has an "
4410 "invalid l_next_free_rec of %d",
4411 (unsigned long long)le64_to_cpu(eb->h_blkno),
4412 le16_to_cpu(new_el->l_next_free_rec));
4413 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004414 goto out;
4415 }
4416 rec = &new_el->l_recs[1];
4417 }
4418 }
4419
4420 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004421 enum ocfs2_contig_type contig_type;
4422
Mark Fasheh328d5752007-06-18 10:48:04 -07004423 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4424
4425 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4426 ret = CONTIG_LEFTRIGHT;
4427 else if (ret == CONTIG_NONE)
4428 ret = contig_type;
4429 }
4430
Tao Maad5a4d72008-01-30 14:21:32 +08004431out:
4432 if (left_path)
4433 ocfs2_free_path(left_path);
4434 if (right_path)
4435 ocfs2_free_path(right_path);
4436
Mark Fasheh328d5752007-06-18 10:48:04 -07004437 return ret;
4438}
4439
Mark Fashehdcd05382007-01-16 11:32:23 -08004440static void ocfs2_figure_contig_type(struct inode *inode,
4441 struct ocfs2_insert_type *insert,
4442 struct ocfs2_extent_list *el,
Tao Maca12b7c2008-08-18 17:38:52 +08004443 struct ocfs2_extent_rec *insert_rec,
4444 struct ocfs2_extent_tree *et)
Mark Fashehdcd05382007-01-16 11:32:23 -08004445{
4446 int i;
4447 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4448
Mark Fashehe48edee2007-03-07 16:46:57 -08004449 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4450
Mark Fashehdcd05382007-01-16 11:32:23 -08004451 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4452 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4453 insert_rec);
4454 if (contig_type != CONTIG_NONE) {
4455 insert->ins_contig_index = i;
4456 break;
4457 }
4458 }
4459 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004460
4461 if (insert->ins_contig != CONTIG_NONE) {
4462 struct ocfs2_extent_rec *rec =
4463 &el->l_recs[insert->ins_contig_index];
4464 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4465 le16_to_cpu(insert_rec->e_leaf_clusters);
4466
4467 /*
4468 * Caller might want us to limit the size of extents, don't
4469 * calculate contiguousness if we might exceed that limit.
4470 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004471 if (et->et_max_leaf_clusters &&
4472 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004473 insert->ins_contig = CONTIG_NONE;
4474 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004475}
4476
4477/*
4478 * This should only be called against the righmost leaf extent list.
4479 *
4480 * ocfs2_figure_appending_type() will figure out whether we'll have to
4481 * insert at the tail of the rightmost leaf.
4482 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004483 * This should also work against the root extent list for tree's with 0
4484 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004485 * then the logic here makes sense.
4486 */
4487static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4488 struct ocfs2_extent_list *el,
4489 struct ocfs2_extent_rec *insert_rec)
4490{
4491 int i;
4492 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4493 struct ocfs2_extent_rec *rec;
4494
4495 insert->ins_appending = APPEND_NONE;
4496
Mark Fashehe48edee2007-03-07 16:46:57 -08004497 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004498
4499 if (!el->l_next_free_rec)
4500 goto set_tail_append;
4501
4502 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4503 /* Were all records empty? */
4504 if (le16_to_cpu(el->l_next_free_rec) == 1)
4505 goto set_tail_append;
4506 }
4507
4508 i = le16_to_cpu(el->l_next_free_rec) - 1;
4509 rec = &el->l_recs[i];
4510
Mark Fashehe48edee2007-03-07 16:46:57 -08004511 if (cpos >=
4512 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004513 goto set_tail_append;
4514
4515 return;
4516
4517set_tail_append:
4518 insert->ins_appending = APPEND_TAIL;
4519}
4520
4521/*
4522 * Helper function called at the begining of an insert.
4523 *
4524 * This computes a few things that are commonly used in the process of
4525 * inserting into the btree:
4526 * - Whether the new extent is contiguous with an existing one.
4527 * - The current tree depth.
4528 * - Whether the insert is an appending one.
4529 * - The total # of free records in the tree.
4530 *
4531 * All of the information is stored on the ocfs2_insert_type
4532 * structure.
4533 */
4534static int ocfs2_figure_insert_type(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004535 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004536 struct buffer_head **last_eb_bh,
4537 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004538 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004539 struct ocfs2_insert_type *insert)
4540{
4541 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004542 struct ocfs2_extent_block *eb;
4543 struct ocfs2_extent_list *el;
4544 struct ocfs2_path *path = NULL;
4545 struct buffer_head *bh = NULL;
4546
Mark Fasheh328d5752007-06-18 10:48:04 -07004547 insert->ins_split = SPLIT_NONE;
4548
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004549 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004550 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4551
4552 if (el->l_tree_depth) {
4553 /*
4554 * If we have tree depth, we read in the
4555 * rightmost extent block ahead of time as
4556 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4557 * may want it later.
4558 */
Joel Becker5e965812008-11-13 14:49:16 -08004559 ret = ocfs2_read_extent_block(inode,
4560 ocfs2_et_get_last_eb_blk(et),
4561 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004562 if (ret) {
4563 mlog_exit(ret);
4564 goto out;
4565 }
4566 eb = (struct ocfs2_extent_block *) bh->b_data;
4567 el = &eb->h_list;
4568 }
4569
4570 /*
4571 * Unless we have a contiguous insert, we'll need to know if
4572 * there is room left in our allocation tree for another
4573 * extent record.
4574 *
4575 * XXX: This test is simplistic, we can search for empty
4576 * extent records too.
4577 */
Tao Maoc77534f2007-08-28 17:22:33 -07004578 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004579 le16_to_cpu(el->l_next_free_rec);
4580
4581 if (!insert->ins_tree_depth) {
Tao Maca12b7c2008-08-18 17:38:52 +08004582 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004583 ocfs2_figure_appending_type(insert, el, insert_rec);
4584 return 0;
4585 }
4586
Joel Beckerffdd7a52008-10-17 22:32:01 -07004587 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004588 if (!path) {
4589 ret = -ENOMEM;
4590 mlog_errno(ret);
4591 goto out;
4592 }
4593
4594 /*
4595 * In the case that we're inserting past what the tree
4596 * currently accounts for, ocfs2_find_path() will return for
4597 * us the rightmost tree path. This is accounted for below in
4598 * the appending code.
4599 */
4600 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4601 if (ret) {
4602 mlog_errno(ret);
4603 goto out;
4604 }
4605
4606 el = path_leaf_el(path);
4607
4608 /*
4609 * Now that we have the path, there's two things we want to determine:
4610 * 1) Contiguousness (also set contig_index if this is so)
4611 *
4612 * 2) Are we doing an append? We can trivially break this up
4613 * into two types of appends: simple record append, or a
4614 * rotate inside the tail leaf.
4615 */
Tao Maca12b7c2008-08-18 17:38:52 +08004616 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004617
4618 /*
4619 * The insert code isn't quite ready to deal with all cases of
4620 * left contiguousness. Specifically, if it's an insert into
4621 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004622 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004623 * left. For now, just catch that case and fool the layers
4624 * above us. This works just fine for tree_depth == 0, which
4625 * is why we allow that above.
4626 */
4627 if (insert->ins_contig == CONTIG_LEFT &&
4628 insert->ins_contig_index == 0)
4629 insert->ins_contig = CONTIG_NONE;
4630
4631 /*
4632 * Ok, so we can simply compare against last_eb to figure out
4633 * whether the path doesn't exist. This will only happen in
4634 * the case that we're doing a tail append, so maybe we can
4635 * take advantage of that information somehow.
4636 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004637 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004638 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004639 /*
4640 * Ok, ocfs2_find_path() returned us the rightmost
4641 * tree path. This might be an appending insert. There are
4642 * two cases:
4643 * 1) We're doing a true append at the tail:
4644 * -This might even be off the end of the leaf
4645 * 2) We're "appending" by rotating in the tail
4646 */
4647 ocfs2_figure_appending_type(insert, el, insert_rec);
4648 }
4649
4650out:
4651 ocfs2_free_path(path);
4652
4653 if (ret == 0)
4654 *last_eb_bh = bh;
4655 else
4656 brelse(bh);
4657 return ret;
4658}
4659
4660/*
4661 * Insert an extent into an inode btree.
4662 *
4663 * The caller needs to update fe->i_clusters
4664 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004665int ocfs2_insert_extent(struct ocfs2_super *osb,
4666 handle_t *handle,
4667 struct inode *inode,
4668 struct ocfs2_extent_tree *et,
4669 u32 cpos,
4670 u64 start_blk,
4671 u32 new_clusters,
4672 u8 flags,
4673 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004674{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004675 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004676 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004677 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004678 struct ocfs2_insert_type insert = {0, };
4679 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004680
Mark Fashehdcd05382007-01-16 11:32:23 -08004681 mlog(0, "add %u clusters at position %u to inode %llu\n",
4682 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004683
Mark Fashehe48edee2007-03-07 16:46:57 -08004684 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004685 rec.e_cpos = cpu_to_le32(cpos);
4686 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004687 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004688 rec.e_flags = flags;
Joel Becker1e61ee72008-08-20 18:32:45 -07004689 status = ocfs2_et_insert_check(inode, et, &rec);
4690 if (status) {
4691 mlog_errno(status);
4692 goto bail;
4693 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004694
Tao Mae7d4cb62008-08-18 17:38:44 +08004695 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004696 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004697 if (status < 0) {
4698 mlog_errno(status);
4699 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004700 }
4701
Mark Fashehdcd05382007-01-16 11:32:23 -08004702 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4703 "Insert.contig_index: %d, Insert.free_records: %d, "
4704 "Insert.tree_depth: %d\n",
4705 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004706 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004707
Tao Maoc77534f2007-08-28 17:22:33 -07004708 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004709 status = ocfs2_grow_tree(inode, handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004710 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004711 meta_ac);
4712 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004713 mlog_errno(status);
4714 goto bail;
4715 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004716 }
4717
Mark Fashehdcd05382007-01-16 11:32:23 -08004718 /* Finally, we can add clusters. This might rotate the tree for us. */
Tao Mae7d4cb62008-08-18 17:38:44 +08004719 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004720 if (status < 0)
4721 mlog_errno(status);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004722 else if (et->et_ops == &ocfs2_dinode_et_ops)
Mark Fasheh83418972007-04-23 18:53:12 -07004723 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004724
4725bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004726 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004727
Tao Maf56654c2008-08-18 17:38:48 +08004728 mlog_exit(status);
4729 return status;
4730}
4731
Tao Ma0eb8d472008-08-18 17:38:45 +08004732/*
4733 * Allcate and add clusters into the extent b-tree.
4734 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004735 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004736 * it is not limited to the file storage. Any extent tree can use this
4737 * function if it implements the proper ocfs2_extent_tree.
4738 */
4739int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4740 struct inode *inode,
4741 u32 *logical_offset,
4742 u32 clusters_to_add,
4743 int mark_unwritten,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004744 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004745 handle_t *handle,
4746 struct ocfs2_alloc_context *data_ac,
4747 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004748 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004749{
4750 int status = 0;
4751 int free_extents;
4752 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4753 u32 bit_off, num_bits;
4754 u64 block;
4755 u8 flags = 0;
4756
4757 BUG_ON(!clusters_to_add);
4758
4759 if (mark_unwritten)
4760 flags = OCFS2_EXT_UNWRITTEN;
4761
Joel Beckerf99b9b72008-08-20 19:36:33 -07004762 free_extents = ocfs2_num_free_extents(osb, inode, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004763 if (free_extents < 0) {
4764 status = free_extents;
4765 mlog_errno(status);
4766 goto leave;
4767 }
4768
4769 /* there are two cases which could cause us to EAGAIN in the
4770 * we-need-more-metadata case:
4771 * 1) we haven't reserved *any*
4772 * 2) we are so fragmented, we've needed to add metadata too
4773 * many times. */
4774 if (!free_extents && !meta_ac) {
4775 mlog(0, "we haven't reserved any metadata!\n");
4776 status = -EAGAIN;
4777 reason = RESTART_META;
4778 goto leave;
4779 } else if ((!free_extents)
4780 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004781 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004782 mlog(0, "filesystem is really fragmented...\n");
4783 status = -EAGAIN;
4784 reason = RESTART_META;
4785 goto leave;
4786 }
4787
4788 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4789 clusters_to_add, &bit_off, &num_bits);
4790 if (status < 0) {
4791 if (status != -ENOSPC)
4792 mlog_errno(status);
4793 goto leave;
4794 }
4795
4796 BUG_ON(num_bits > clusters_to_add);
4797
Joel Becker13723d02008-10-17 19:25:01 -07004798 /* reserve our write early -- insert_extent may update the tree root */
4799 status = ocfs2_et_root_journal_access(handle, inode, et,
4800 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d472008-08-18 17:38:45 +08004801 if (status < 0) {
4802 mlog_errno(status);
4803 goto leave;
4804 }
4805
4806 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4807 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4808 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004809 status = ocfs2_insert_extent(osb, handle, inode, et,
4810 *logical_offset, block,
4811 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004812 if (status < 0) {
4813 mlog_errno(status);
4814 goto leave;
4815 }
4816
Joel Beckerf99b9b72008-08-20 19:36:33 -07004817 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004818 if (status < 0) {
4819 mlog_errno(status);
4820 goto leave;
4821 }
4822
4823 clusters_to_add -= num_bits;
4824 *logical_offset += num_bits;
4825
4826 if (clusters_to_add) {
4827 mlog(0, "need to alloc once more, wanted = %u\n",
4828 clusters_to_add);
4829 status = -EAGAIN;
4830 reason = RESTART_TRANS;
4831 }
4832
4833leave:
4834 mlog_exit(status);
4835 if (reason_ret)
4836 *reason_ret = reason;
4837 return status;
4838}
4839
Mark Fasheh328d5752007-06-18 10:48:04 -07004840static void ocfs2_make_right_split_rec(struct super_block *sb,
4841 struct ocfs2_extent_rec *split_rec,
4842 u32 cpos,
4843 struct ocfs2_extent_rec *rec)
4844{
4845 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4846 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4847
4848 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4849
4850 split_rec->e_cpos = cpu_to_le32(cpos);
4851 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4852
4853 split_rec->e_blkno = rec->e_blkno;
4854 le64_add_cpu(&split_rec->e_blkno,
4855 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4856
4857 split_rec->e_flags = rec->e_flags;
4858}
4859
4860static int ocfs2_split_and_insert(struct inode *inode,
4861 handle_t *handle,
4862 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004863 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004864 struct buffer_head **last_eb_bh,
4865 int split_index,
4866 struct ocfs2_extent_rec *orig_split_rec,
4867 struct ocfs2_alloc_context *meta_ac)
4868{
4869 int ret = 0, depth;
4870 unsigned int insert_range, rec_range, do_leftright = 0;
4871 struct ocfs2_extent_rec tmprec;
4872 struct ocfs2_extent_list *rightmost_el;
4873 struct ocfs2_extent_rec rec;
4874 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4875 struct ocfs2_insert_type insert;
4876 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004877
4878leftright:
4879 /*
4880 * Store a copy of the record on the stack - it might move
4881 * around as the tree is manipulated below.
4882 */
4883 rec = path_leaf_el(path)->l_recs[split_index];
4884
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004885 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004886
4887 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4888 if (depth) {
4889 BUG_ON(!(*last_eb_bh));
4890 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4891 rightmost_el = &eb->h_list;
4892 }
4893
4894 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4895 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004896 ret = ocfs2_grow_tree(inode, handle, et,
4897 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004898 if (ret) {
4899 mlog_errno(ret);
4900 goto out;
4901 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004902 }
4903
4904 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4905 insert.ins_appending = APPEND_NONE;
4906 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004907 insert.ins_tree_depth = depth;
4908
4909 insert_range = le32_to_cpu(split_rec.e_cpos) +
4910 le16_to_cpu(split_rec.e_leaf_clusters);
4911 rec_range = le32_to_cpu(rec.e_cpos) +
4912 le16_to_cpu(rec.e_leaf_clusters);
4913
4914 if (split_rec.e_cpos == rec.e_cpos) {
4915 insert.ins_split = SPLIT_LEFT;
4916 } else if (insert_range == rec_range) {
4917 insert.ins_split = SPLIT_RIGHT;
4918 } else {
4919 /*
4920 * Left/right split. We fake this as a right split
4921 * first and then make a second pass as a left split.
4922 */
4923 insert.ins_split = SPLIT_RIGHT;
4924
4925 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4926 &rec);
4927
4928 split_rec = tmprec;
4929
4930 BUG_ON(do_leftright);
4931 do_leftright = 1;
4932 }
4933
Tao Mae7d4cb62008-08-18 17:38:44 +08004934 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004935 if (ret) {
4936 mlog_errno(ret);
4937 goto out;
4938 }
4939
4940 if (do_leftright == 1) {
4941 u32 cpos;
4942 struct ocfs2_extent_list *el;
4943
4944 do_leftright++;
4945 split_rec = *orig_split_rec;
4946
4947 ocfs2_reinit_path(path, 1);
4948
4949 cpos = le32_to_cpu(split_rec.e_cpos);
4950 ret = ocfs2_find_path(inode, path, cpos);
4951 if (ret) {
4952 mlog_errno(ret);
4953 goto out;
4954 }
4955
4956 el = path_leaf_el(path);
4957 split_index = ocfs2_search_extent_list(el, cpos);
4958 goto leftright;
4959 }
4960out:
4961
4962 return ret;
4963}
4964
Tao Ma47be12e2009-01-09 07:32:48 +08004965static int ocfs2_replace_extent_rec(struct inode *inode,
4966 handle_t *handle,
4967 struct ocfs2_path *path,
4968 struct ocfs2_extent_list *el,
4969 int split_index,
4970 struct ocfs2_extent_rec *split_rec)
4971{
4972 int ret;
4973
4974 ret = ocfs2_path_bh_journal_access(handle, inode, path,
4975 path_num_items(path) - 1);
4976 if (ret) {
4977 mlog_errno(ret);
4978 goto out;
4979 }
4980
4981 el->l_recs[split_index] = *split_rec;
4982
4983 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4984out:
4985 return ret;
4986}
4987
Mark Fasheh328d5752007-06-18 10:48:04 -07004988/*
4989 * Mark part or all of the extent record at split_index in the leaf
4990 * pointed to by path as written. This removes the unwritten
4991 * extent flag.
4992 *
4993 * Care is taken to handle contiguousness so as to not grow the tree.
4994 *
4995 * meta_ac is not strictly necessary - we only truly need it if growth
4996 * of the tree is required. All other cases will degrade into a less
4997 * optimal tree layout.
4998 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004999 * last_eb_bh should be the rightmost leaf block for any extent
5000 * btree. Since a split may grow the tree or a merge might shrink it,
5001 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07005002 *
5003 * This code is optimized for readability - several passes might be
5004 * made over certain portions of the tree. All of those blocks will
5005 * have been brought into cache (and pinned via the journal), so the
5006 * extra overhead is not expressed in terms of disk reads.
5007 */
5008static int __ocfs2_mark_extent_written(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08005009 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005010 handle_t *handle,
5011 struct ocfs2_path *path,
5012 int split_index,
5013 struct ocfs2_extent_rec *split_rec,
5014 struct ocfs2_alloc_context *meta_ac,
5015 struct ocfs2_cached_dealloc_ctxt *dealloc)
5016{
5017 int ret = 0;
5018 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08005019 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07005020 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5021 struct ocfs2_merge_ctxt ctxt;
5022 struct ocfs2_extent_list *rightmost_el;
5023
Roel Kluin3cf0c502007-10-27 00:20:36 +02005024 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07005025 ret = -EIO;
5026 mlog_errno(ret);
5027 goto out;
5028 }
5029
5030 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5031 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5032 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5033 ret = -EIO;
5034 mlog_errno(ret);
5035 goto out;
5036 }
5037
Tao Maad5a4d72008-01-30 14:21:32 +08005038 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005039 split_index,
5040 split_rec);
5041
5042 /*
5043 * The core merge / split code wants to know how much room is
5044 * left in this inodes allocation tree, so we pass the
5045 * rightmost extent list.
5046 */
5047 if (path->p_tree_depth) {
5048 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005049
Joel Becker5e965812008-11-13 14:49:16 -08005050 ret = ocfs2_read_extent_block(inode,
5051 ocfs2_et_get_last_eb_blk(et),
5052 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005053 if (ret) {
5054 mlog_exit(ret);
5055 goto out;
5056 }
5057
5058 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005059 rightmost_el = &eb->h_list;
5060 } else
5061 rightmost_el = path_root_el(path);
5062
Mark Fasheh328d5752007-06-18 10:48:04 -07005063 if (rec->e_cpos == split_rec->e_cpos &&
5064 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5065 ctxt.c_split_covers_rec = 1;
5066 else
5067 ctxt.c_split_covers_rec = 0;
5068
5069 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5070
Mark Fasheh015452b2007-09-12 10:21:22 -07005071 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5072 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5073 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005074
5075 if (ctxt.c_contig_type == CONTIG_NONE) {
5076 if (ctxt.c_split_covers_rec)
Tao Ma47be12e2009-01-09 07:32:48 +08005077 ret = ocfs2_replace_extent_rec(inode, handle,
5078 path, el,
5079 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005080 else
Tao Mae7d4cb62008-08-18 17:38:44 +08005081 ret = ocfs2_split_and_insert(inode, handle, path, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005082 &last_eb_bh, split_index,
5083 split_rec, meta_ac);
5084 if (ret)
5085 mlog_errno(ret);
5086 } else {
5087 ret = ocfs2_try_to_merge_extent(inode, handle, path,
5088 split_index, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08005089 dealloc, &ctxt, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005090 if (ret)
5091 mlog_errno(ret);
5092 }
5093
Mark Fasheh328d5752007-06-18 10:48:04 -07005094out:
5095 brelse(last_eb_bh);
5096 return ret;
5097}
5098
5099/*
5100 * Mark the already-existing extent at cpos as written for len clusters.
5101 *
5102 * If the existing extent is larger than the request, initiate a
5103 * split. An attempt will be made at merging with adjacent extents.
5104 *
5105 * The caller is responsible for passing down meta_ac if we'll need it.
5106 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005107int ocfs2_mark_extent_written(struct inode *inode,
5108 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005109 handle_t *handle, u32 cpos, u32 len, u32 phys,
5110 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005111 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005112{
5113 int ret, index;
5114 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
5115 struct ocfs2_extent_rec split_rec;
5116 struct ocfs2_path *left_path = NULL;
5117 struct ocfs2_extent_list *el;
5118
5119 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5120 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
5121
5122 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5123 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5124 "that are being written to, but the feature bit "
5125 "is not set in the super block.",
5126 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5127 ret = -EROFS;
5128 goto out;
5129 }
5130
5131 /*
5132 * XXX: This should be fixed up so that we just re-insert the
5133 * next extent records.
Joel Beckerf99b9b72008-08-20 19:36:33 -07005134 *
5135 * XXX: This is a hack on the extent tree, maybe it should be
5136 * an op?
Mark Fasheh328d5752007-06-18 10:48:04 -07005137 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005138 if (et->et_ops == &ocfs2_dinode_et_ops)
Tao Mae7d4cb62008-08-18 17:38:44 +08005139 ocfs2_extent_map_trunc(inode, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07005140
Joel Beckerffdd7a52008-10-17 22:32:01 -07005141 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005142 if (!left_path) {
5143 ret = -ENOMEM;
5144 mlog_errno(ret);
5145 goto out;
5146 }
5147
5148 ret = ocfs2_find_path(inode, left_path, cpos);
5149 if (ret) {
5150 mlog_errno(ret);
5151 goto out;
5152 }
5153 el = path_leaf_el(left_path);
5154
5155 index = ocfs2_search_extent_list(el, cpos);
5156 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5157 ocfs2_error(inode->i_sb,
5158 "Inode %llu has an extent at cpos %u which can no "
5159 "longer be found.\n",
5160 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5161 ret = -EROFS;
5162 goto out;
5163 }
5164
5165 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5166 split_rec.e_cpos = cpu_to_le32(cpos);
5167 split_rec.e_leaf_clusters = cpu_to_le16(len);
5168 split_rec.e_blkno = cpu_to_le64(start_blkno);
5169 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
5170 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
5171
Joel Beckerf99b9b72008-08-20 19:36:33 -07005172 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
Tao Mae7d4cb62008-08-18 17:38:44 +08005173 index, &split_rec, meta_ac,
5174 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005175 if (ret)
5176 mlog_errno(ret);
5177
5178out:
5179 ocfs2_free_path(left_path);
5180 return ret;
5181}
5182
Tao Mae7d4cb62008-08-18 17:38:44 +08005183static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005184 handle_t *handle, struct ocfs2_path *path,
5185 int index, u32 new_range,
5186 struct ocfs2_alloc_context *meta_ac)
5187{
5188 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005189 struct buffer_head *last_eb_bh = NULL;
5190 struct ocfs2_extent_block *eb;
5191 struct ocfs2_extent_list *rightmost_el, *el;
5192 struct ocfs2_extent_rec split_rec;
5193 struct ocfs2_extent_rec *rec;
5194 struct ocfs2_insert_type insert;
5195
5196 /*
5197 * Setup the record to split before we grow the tree.
5198 */
5199 el = path_leaf_el(path);
5200 rec = &el->l_recs[index];
5201 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5202
5203 depth = path->p_tree_depth;
5204 if (depth > 0) {
Joel Becker5e965812008-11-13 14:49:16 -08005205 ret = ocfs2_read_extent_block(inode,
5206 ocfs2_et_get_last_eb_blk(et),
5207 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005208 if (ret < 0) {
5209 mlog_errno(ret);
5210 goto out;
5211 }
5212
5213 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5214 rightmost_el = &eb->h_list;
5215 } else
5216 rightmost_el = path_leaf_el(path);
5217
Tao Ma811f9332008-08-18 17:38:43 +08005218 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005219 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005220 ret = ocfs2_extend_trans(handle, credits);
5221 if (ret) {
5222 mlog_errno(ret);
5223 goto out;
5224 }
5225
5226 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5227 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005228 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005229 meta_ac);
5230 if (ret) {
5231 mlog_errno(ret);
5232 goto out;
5233 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005234 }
5235
5236 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5237 insert.ins_appending = APPEND_NONE;
5238 insert.ins_contig = CONTIG_NONE;
5239 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005240 insert.ins_tree_depth = depth;
5241
Tao Mae7d4cb62008-08-18 17:38:44 +08005242 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005243 if (ret)
5244 mlog_errno(ret);
5245
5246out:
5247 brelse(last_eb_bh);
5248 return ret;
5249}
5250
5251static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5252 struct ocfs2_path *path, int index,
5253 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08005254 u32 cpos, u32 len,
5255 struct ocfs2_extent_tree *et)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005256{
5257 int ret;
5258 u32 left_cpos, rec_range, trunc_range;
5259 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5260 struct super_block *sb = inode->i_sb;
5261 struct ocfs2_path *left_path = NULL;
5262 struct ocfs2_extent_list *el = path_leaf_el(path);
5263 struct ocfs2_extent_rec *rec;
5264 struct ocfs2_extent_block *eb;
5265
5266 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005267 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005268 if (ret) {
5269 mlog_errno(ret);
5270 goto out;
5271 }
5272
5273 index--;
5274 }
5275
5276 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5277 path->p_tree_depth) {
5278 /*
5279 * Check whether this is the rightmost tree record. If
5280 * we remove all of this record or part of its right
5281 * edge then an update of the record lengths above it
5282 * will be required.
5283 */
5284 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5285 if (eb->h_next_leaf_blk == 0)
5286 is_rightmost_tree_rec = 1;
5287 }
5288
5289 rec = &el->l_recs[index];
5290 if (index == 0 && path->p_tree_depth &&
5291 le32_to_cpu(rec->e_cpos) == cpos) {
5292 /*
5293 * Changing the leftmost offset (via partial or whole
5294 * record truncate) of an interior (or rightmost) path
5295 * means we have to update the subtree that is formed
5296 * by this leaf and the one to it's left.
5297 *
5298 * There are two cases we can skip:
5299 * 1) Path is the leftmost one in our inode tree.
5300 * 2) The leaf is rightmost and will be empty after
5301 * we remove the extent record - the rotate code
5302 * knows how to update the newly formed edge.
5303 */
5304
5305 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5306 &left_cpos);
5307 if (ret) {
5308 mlog_errno(ret);
5309 goto out;
5310 }
5311
5312 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005313 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005314 if (!left_path) {
5315 ret = -ENOMEM;
5316 mlog_errno(ret);
5317 goto out;
5318 }
5319
5320 ret = ocfs2_find_path(inode, left_path, left_cpos);
5321 if (ret) {
5322 mlog_errno(ret);
5323 goto out;
5324 }
5325 }
5326 }
5327
5328 ret = ocfs2_extend_rotate_transaction(handle, 0,
5329 handle->h_buffer_credits,
5330 path);
5331 if (ret) {
5332 mlog_errno(ret);
5333 goto out;
5334 }
5335
5336 ret = ocfs2_journal_access_path(inode, handle, path);
5337 if (ret) {
5338 mlog_errno(ret);
5339 goto out;
5340 }
5341
5342 ret = ocfs2_journal_access_path(inode, handle, left_path);
5343 if (ret) {
5344 mlog_errno(ret);
5345 goto out;
5346 }
5347
5348 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5349 trunc_range = cpos + len;
5350
5351 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5352 int next_free;
5353
5354 memset(rec, 0, sizeof(*rec));
5355 ocfs2_cleanup_merge(el, index);
5356 wants_rotate = 1;
5357
5358 next_free = le16_to_cpu(el->l_next_free_rec);
5359 if (is_rightmost_tree_rec && next_free > 1) {
5360 /*
5361 * We skip the edge update if this path will
5362 * be deleted by the rotate code.
5363 */
5364 rec = &el->l_recs[next_free - 1];
5365 ocfs2_adjust_rightmost_records(inode, handle, path,
5366 rec);
5367 }
5368 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5369 /* Remove leftmost portion of the record. */
5370 le32_add_cpu(&rec->e_cpos, len);
5371 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5372 le16_add_cpu(&rec->e_leaf_clusters, -len);
5373 } else if (rec_range == trunc_range) {
5374 /* Remove rightmost portion of the record */
5375 le16_add_cpu(&rec->e_leaf_clusters, -len);
5376 if (is_rightmost_tree_rec)
5377 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5378 } else {
5379 /* Caller should have trapped this. */
5380 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5381 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5382 le32_to_cpu(rec->e_cpos),
5383 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5384 BUG();
5385 }
5386
5387 if (left_path) {
5388 int subtree_index;
5389
5390 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5391 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5392 subtree_index);
5393 }
5394
5395 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5396
Tao Mae7d4cb62008-08-18 17:38:44 +08005397 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005398 if (ret) {
5399 mlog_errno(ret);
5400 goto out;
5401 }
5402
5403out:
5404 ocfs2_free_path(left_path);
5405 return ret;
5406}
5407
Joel Beckerf99b9b72008-08-20 19:36:33 -07005408int ocfs2_remove_extent(struct inode *inode,
5409 struct ocfs2_extent_tree *et,
Mark Fasheh063c4562007-07-03 13:34:11 -07005410 u32 cpos, u32 len, handle_t *handle,
5411 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005412 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005413{
5414 int ret, index;
5415 u32 rec_range, trunc_range;
5416 struct ocfs2_extent_rec *rec;
5417 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005418 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005419
5420 ocfs2_extent_map_trunc(inode, 0);
5421
Joel Beckerffdd7a52008-10-17 22:32:01 -07005422 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005423 if (!path) {
5424 ret = -ENOMEM;
5425 mlog_errno(ret);
5426 goto out;
5427 }
5428
5429 ret = ocfs2_find_path(inode, path, cpos);
5430 if (ret) {
5431 mlog_errno(ret);
5432 goto out;
5433 }
5434
5435 el = path_leaf_el(path);
5436 index = ocfs2_search_extent_list(el, cpos);
5437 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5438 ocfs2_error(inode->i_sb,
5439 "Inode %llu has an extent at cpos %u which can no "
5440 "longer be found.\n",
5441 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5442 ret = -EROFS;
5443 goto out;
5444 }
5445
5446 /*
5447 * We have 3 cases of extent removal:
5448 * 1) Range covers the entire extent rec
5449 * 2) Range begins or ends on one edge of the extent rec
5450 * 3) Range is in the middle of the extent rec (no shared edges)
5451 *
5452 * For case 1 we remove the extent rec and left rotate to
5453 * fill the hole.
5454 *
5455 * For case 2 we just shrink the existing extent rec, with a
5456 * tree update if the shrinking edge is also the edge of an
5457 * extent block.
5458 *
5459 * For case 3 we do a right split to turn the extent rec into
5460 * something case 2 can handle.
5461 */
5462 rec = &el->l_recs[index];
5463 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5464 trunc_range = cpos + len;
5465
5466 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5467
5468 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5469 "(cpos %u, len %u)\n",
5470 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5471 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5472
5473 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5474 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005475 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005476 if (ret) {
5477 mlog_errno(ret);
5478 goto out;
5479 }
5480 } else {
Joel Beckerf99b9b72008-08-20 19:36:33 -07005481 ret = ocfs2_split_tree(inode, et, handle, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005482 trunc_range, meta_ac);
5483 if (ret) {
5484 mlog_errno(ret);
5485 goto out;
5486 }
5487
5488 /*
5489 * The split could have manipulated the tree enough to
5490 * move the record location, so we have to look for it again.
5491 */
5492 ocfs2_reinit_path(path, 1);
5493
5494 ret = ocfs2_find_path(inode, path, cpos);
5495 if (ret) {
5496 mlog_errno(ret);
5497 goto out;
5498 }
5499
5500 el = path_leaf_el(path);
5501 index = ocfs2_search_extent_list(el, cpos);
5502 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5503 ocfs2_error(inode->i_sb,
5504 "Inode %llu: split at cpos %u lost record.",
5505 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5506 cpos);
5507 ret = -EROFS;
5508 goto out;
5509 }
5510
5511 /*
5512 * Double check our values here. If anything is fishy,
5513 * it's easier to catch it at the top level.
5514 */
5515 rec = &el->l_recs[index];
5516 rec_range = le32_to_cpu(rec->e_cpos) +
5517 ocfs2_rec_clusters(el, rec);
5518 if (rec_range != trunc_range) {
5519 ocfs2_error(inode->i_sb,
5520 "Inode %llu: error after split at cpos %u"
5521 "trunc len %u, existing record is (%u,%u)",
5522 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5523 cpos, len, le32_to_cpu(rec->e_cpos),
5524 ocfs2_rec_clusters(el, rec));
5525 ret = -EROFS;
5526 goto out;
5527 }
5528
5529 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005530 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005531 if (ret) {
5532 mlog_errno(ret);
5533 goto out;
5534 }
5535 }
5536
5537out:
5538 ocfs2_free_path(path);
5539 return ret;
5540}
5541
Mark Fashehfecc0112008-11-12 15:16:38 -08005542int ocfs2_remove_btree_range(struct inode *inode,
5543 struct ocfs2_extent_tree *et,
5544 u32 cpos, u32 phys_cpos, u32 len,
5545 struct ocfs2_cached_dealloc_ctxt *dealloc)
5546{
5547 int ret;
5548 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5549 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5550 struct inode *tl_inode = osb->osb_tl_inode;
5551 handle_t *handle;
5552 struct ocfs2_alloc_context *meta_ac = NULL;
5553
5554 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5555 if (ret) {
5556 mlog_errno(ret);
5557 return ret;
5558 }
5559
5560 mutex_lock(&tl_inode->i_mutex);
5561
5562 if (ocfs2_truncate_log_needs_flush(osb)) {
5563 ret = __ocfs2_flush_truncate_log(osb);
5564 if (ret < 0) {
5565 mlog_errno(ret);
5566 goto out;
5567 }
5568 }
5569
Jan Karaa90714c2008-10-09 19:38:40 +02005570 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005571 if (IS_ERR(handle)) {
5572 ret = PTR_ERR(handle);
5573 mlog_errno(ret);
5574 goto out;
5575 }
5576
Joel Becker13723d02008-10-17 19:25:01 -07005577 ret = ocfs2_et_root_journal_access(handle, inode, et,
5578 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005579 if (ret) {
5580 mlog_errno(ret);
5581 goto out;
5582 }
5583
Mark Fashehfd4ef232009-01-29 15:06:21 -08005584 vfs_dq_free_space_nodirty(inode,
5585 ocfs2_clusters_to_bytes(inode->i_sb, len));
5586
Mark Fashehfecc0112008-11-12 15:16:38 -08005587 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5588 dealloc);
5589 if (ret) {
5590 mlog_errno(ret);
5591 goto out_commit;
5592 }
5593
5594 ocfs2_et_update_clusters(inode, et, -len);
5595
5596 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5597 if (ret) {
5598 mlog_errno(ret);
5599 goto out_commit;
5600 }
5601
5602 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5603 if (ret)
5604 mlog_errno(ret);
5605
5606out_commit:
5607 ocfs2_commit_trans(osb, handle);
5608out:
5609 mutex_unlock(&tl_inode->i_mutex);
5610
5611 if (meta_ac)
5612 ocfs2_free_alloc_context(meta_ac);
5613
5614 return ret;
5615}
5616
Mark Fasheh063c4562007-07-03 13:34:11 -07005617int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005618{
5619 struct buffer_head *tl_bh = osb->osb_tl_bh;
5620 struct ocfs2_dinode *di;
5621 struct ocfs2_truncate_log *tl;
5622
5623 di = (struct ocfs2_dinode *) tl_bh->b_data;
5624 tl = &di->id2.i_dealloc;
5625
5626 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5627 "slot %d, invalid truncate log parameters: used = "
5628 "%u, count = %u\n", osb->slot_num,
5629 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5630 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5631}
5632
5633static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5634 unsigned int new_start)
5635{
5636 unsigned int tail_index;
5637 unsigned int current_tail;
5638
5639 /* No records, nothing to coalesce */
5640 if (!le16_to_cpu(tl->tl_used))
5641 return 0;
5642
5643 tail_index = le16_to_cpu(tl->tl_used) - 1;
5644 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5645 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5646
5647 return current_tail == new_start;
5648}
5649
Mark Fasheh063c4562007-07-03 13:34:11 -07005650int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5651 handle_t *handle,
5652 u64 start_blk,
5653 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005654{
5655 int status, index;
5656 unsigned int start_cluster, tl_count;
5657 struct inode *tl_inode = osb->osb_tl_inode;
5658 struct buffer_head *tl_bh = osb->osb_tl_bh;
5659 struct ocfs2_dinode *di;
5660 struct ocfs2_truncate_log *tl;
5661
Mark Fashehb06970532006-03-03 10:24:33 -08005662 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5663 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005664
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005665 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005666
5667 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5668
5669 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005670
Joel Becker10995aa2008-11-13 14:49:12 -08005671 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5672 * by the underlying call to ocfs2_read_inode_block(), so any
5673 * corruption is a code bug */
5674 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5675
5676 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005677 tl_count = le16_to_cpu(tl->tl_count);
5678 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5679 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005680 "Truncate record count on #%llu invalid "
5681 "wanted %u, actual %u\n",
5682 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005683 ocfs2_truncate_recs_per_inode(osb->sb),
5684 le16_to_cpu(tl->tl_count));
5685
5686 /* Caller should have known to flush before calling us. */
5687 index = le16_to_cpu(tl->tl_used);
5688 if (index >= tl_count) {
5689 status = -ENOSPC;
5690 mlog_errno(status);
5691 goto bail;
5692 }
5693
Joel Becker13723d02008-10-17 19:25:01 -07005694 status = ocfs2_journal_access_di(handle, tl_inode, tl_bh,
5695 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005696 if (status < 0) {
5697 mlog_errno(status);
5698 goto bail;
5699 }
5700
5701 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005702 "%llu (index = %d)\n", num_clusters, start_cluster,
5703 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005704
5705 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5706 /*
5707 * Move index back to the record we are coalescing with.
5708 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5709 */
5710 index--;
5711
5712 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5713 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5714 index, le32_to_cpu(tl->tl_recs[index].t_start),
5715 num_clusters);
5716 } else {
5717 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5718 tl->tl_used = cpu_to_le16(index + 1);
5719 }
5720 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5721
5722 status = ocfs2_journal_dirty(handle, tl_bh);
5723 if (status < 0) {
5724 mlog_errno(status);
5725 goto bail;
5726 }
5727
5728bail:
5729 mlog_exit(status);
5730 return status;
5731}
5732
5733static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005734 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005735 struct inode *data_alloc_inode,
5736 struct buffer_head *data_alloc_bh)
5737{
5738 int status = 0;
5739 int i;
5740 unsigned int num_clusters;
5741 u64 start_blk;
5742 struct ocfs2_truncate_rec rec;
5743 struct ocfs2_dinode *di;
5744 struct ocfs2_truncate_log *tl;
5745 struct inode *tl_inode = osb->osb_tl_inode;
5746 struct buffer_head *tl_bh = osb->osb_tl_bh;
5747
5748 mlog_entry_void();
5749
5750 di = (struct ocfs2_dinode *) tl_bh->b_data;
5751 tl = &di->id2.i_dealloc;
5752 i = le16_to_cpu(tl->tl_used) - 1;
5753 while (i >= 0) {
5754 /* Caller has given us at least enough credits to
5755 * update the truncate log dinode */
Joel Becker13723d02008-10-17 19:25:01 -07005756 status = ocfs2_journal_access_di(handle, tl_inode, tl_bh,
5757 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005758 if (status < 0) {
5759 mlog_errno(status);
5760 goto bail;
5761 }
5762
5763 tl->tl_used = cpu_to_le16(i);
5764
5765 status = ocfs2_journal_dirty(handle, tl_bh);
5766 if (status < 0) {
5767 mlog_errno(status);
5768 goto bail;
5769 }
5770
5771 /* TODO: Perhaps we can calculate the bulk of the
5772 * credits up front rather than extending like
5773 * this. */
5774 status = ocfs2_extend_trans(handle,
5775 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5776 if (status < 0) {
5777 mlog_errno(status);
5778 goto bail;
5779 }
5780
5781 rec = tl->tl_recs[i];
5782 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5783 le32_to_cpu(rec.t_start));
5784 num_clusters = le32_to_cpu(rec.t_clusters);
5785
5786 /* if start_blk is not set, we ignore the record as
5787 * invalid. */
5788 if (start_blk) {
5789 mlog(0, "free record %d, start = %u, clusters = %u\n",
5790 i, le32_to_cpu(rec.t_start), num_clusters);
5791
5792 status = ocfs2_free_clusters(handle, data_alloc_inode,
5793 data_alloc_bh, start_blk,
5794 num_clusters);
5795 if (status < 0) {
5796 mlog_errno(status);
5797 goto bail;
5798 }
5799 }
5800 i--;
5801 }
5802
5803bail:
5804 mlog_exit(status);
5805 return status;
5806}
5807
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005808/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005809int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005810{
5811 int status;
5812 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005813 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005814 struct inode *tl_inode = osb->osb_tl_inode;
5815 struct inode *data_alloc_inode = NULL;
5816 struct buffer_head *tl_bh = osb->osb_tl_bh;
5817 struct buffer_head *data_alloc_bh = NULL;
5818 struct ocfs2_dinode *di;
5819 struct ocfs2_truncate_log *tl;
5820
5821 mlog_entry_void();
5822
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005823 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005824
5825 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005826
Joel Becker10995aa2008-11-13 14:49:12 -08005827 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5828 * by the underlying call to ocfs2_read_inode_block(), so any
5829 * corruption is a code bug */
5830 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5831
5832 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005833 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005834 mlog(0, "Flush %u records from truncate log #%llu\n",
5835 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005836 if (!num_to_flush) {
5837 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005838 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005839 }
5840
5841 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5842 GLOBAL_BITMAP_SYSTEM_INODE,
5843 OCFS2_INVALID_SLOT);
5844 if (!data_alloc_inode) {
5845 status = -EINVAL;
5846 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005847 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005848 }
5849
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005850 mutex_lock(&data_alloc_inode->i_mutex);
5851
Mark Fashehe63aecb62007-10-18 15:30:42 -07005852 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005853 if (status < 0) {
5854 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005855 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005856 }
5857
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005858 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005859 if (IS_ERR(handle)) {
5860 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005861 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005862 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005863 }
5864
5865 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5866 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005867 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005868 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005869
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005870 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005871
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005872out_unlock:
5873 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005874 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005875
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005876out_mutex:
5877 mutex_unlock(&data_alloc_inode->i_mutex);
5878 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005879
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005880out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005881 mlog_exit(status);
5882 return status;
5883}
5884
5885int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5886{
5887 int status;
5888 struct inode *tl_inode = osb->osb_tl_inode;
5889
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005890 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005891 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005892 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005893
5894 return status;
5895}
5896
David Howellsc4028952006-11-22 14:57:56 +00005897static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005898{
5899 int status;
David Howellsc4028952006-11-22 14:57:56 +00005900 struct ocfs2_super *osb =
5901 container_of(work, struct ocfs2_super,
5902 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005903
5904 mlog_entry_void();
5905
5906 status = ocfs2_flush_truncate_log(osb);
5907 if (status < 0)
5908 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005909 else
5910 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005911
5912 mlog_exit(status);
5913}
5914
5915#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5916void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5917 int cancel)
5918{
5919 if (osb->osb_tl_inode) {
5920 /* We want to push off log flushes while truncates are
5921 * still running. */
5922 if (cancel)
5923 cancel_delayed_work(&osb->osb_truncate_log_wq);
5924
5925 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5926 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5927 }
5928}
5929
5930static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5931 int slot_num,
5932 struct inode **tl_inode,
5933 struct buffer_head **tl_bh)
5934{
5935 int status;
5936 struct inode *inode = NULL;
5937 struct buffer_head *bh = NULL;
5938
5939 inode = ocfs2_get_system_file_inode(osb,
5940 TRUNCATE_LOG_SYSTEM_INODE,
5941 slot_num);
5942 if (!inode) {
5943 status = -EINVAL;
5944 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5945 goto bail;
5946 }
5947
Joel Beckerb657c952008-11-13 14:49:11 -08005948 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005949 if (status < 0) {
5950 iput(inode);
5951 mlog_errno(status);
5952 goto bail;
5953 }
5954
5955 *tl_inode = inode;
5956 *tl_bh = bh;
5957bail:
5958 mlog_exit(status);
5959 return status;
5960}
5961
5962/* called during the 1st stage of node recovery. we stamp a clean
5963 * truncate log and pass back a copy for processing later. if the
5964 * truncate log does not require processing, a *tl_copy is set to
5965 * NULL. */
5966int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5967 int slot_num,
5968 struct ocfs2_dinode **tl_copy)
5969{
5970 int status;
5971 struct inode *tl_inode = NULL;
5972 struct buffer_head *tl_bh = NULL;
5973 struct ocfs2_dinode *di;
5974 struct ocfs2_truncate_log *tl;
5975
5976 *tl_copy = NULL;
5977
5978 mlog(0, "recover truncate log from slot %d\n", slot_num);
5979
5980 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5981 if (status < 0) {
5982 mlog_errno(status);
5983 goto bail;
5984 }
5985
5986 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005987
Joel Becker10995aa2008-11-13 14:49:12 -08005988 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5989 * validated by the underlying call to ocfs2_read_inode_block(),
5990 * so any corruption is a code bug */
5991 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5992
5993 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005994 if (le16_to_cpu(tl->tl_used)) {
5995 mlog(0, "We'll have %u logs to recover\n",
5996 le16_to_cpu(tl->tl_used));
5997
5998 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5999 if (!(*tl_copy)) {
6000 status = -ENOMEM;
6001 mlog_errno(status);
6002 goto bail;
6003 }
6004
6005 /* Assuming the write-out below goes well, this copy
6006 * will be passed back to recovery for processing. */
6007 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6008
6009 /* All we need to do to clear the truncate log is set
6010 * tl_used. */
6011 tl->tl_used = 0;
6012
Joel Becker13723d02008-10-17 19:25:01 -07006013 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08006014 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08006015 if (status < 0) {
6016 mlog_errno(status);
6017 goto bail;
6018 }
6019 }
6020
6021bail:
6022 if (tl_inode)
6023 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006024 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006025
6026 if (status < 0 && (*tl_copy)) {
6027 kfree(*tl_copy);
6028 *tl_copy = NULL;
6029 }
6030
6031 mlog_exit(status);
6032 return status;
6033}
6034
6035int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6036 struct ocfs2_dinode *tl_copy)
6037{
6038 int status = 0;
6039 int i;
6040 unsigned int clusters, num_recs, start_cluster;
6041 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006042 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006043 struct inode *tl_inode = osb->osb_tl_inode;
6044 struct ocfs2_truncate_log *tl;
6045
6046 mlog_entry_void();
6047
6048 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6049 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6050 return -EINVAL;
6051 }
6052
6053 tl = &tl_copy->id2.i_dealloc;
6054 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08006055 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006056 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006057
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006058 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006059 for(i = 0; i < num_recs; i++) {
6060 if (ocfs2_truncate_log_needs_flush(osb)) {
6061 status = __ocfs2_flush_truncate_log(osb);
6062 if (status < 0) {
6063 mlog_errno(status);
6064 goto bail_up;
6065 }
6066 }
6067
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006068 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006069 if (IS_ERR(handle)) {
6070 status = PTR_ERR(handle);
6071 mlog_errno(status);
6072 goto bail_up;
6073 }
6074
6075 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6076 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6077 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6078
6079 status = ocfs2_truncate_log_append(osb, handle,
6080 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006081 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006082 if (status < 0) {
6083 mlog_errno(status);
6084 goto bail_up;
6085 }
6086 }
6087
6088bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006089 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006090
6091 mlog_exit(status);
6092 return status;
6093}
6094
6095void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6096{
6097 int status;
6098 struct inode *tl_inode = osb->osb_tl_inode;
6099
6100 mlog_entry_void();
6101
6102 if (tl_inode) {
6103 cancel_delayed_work(&osb->osb_truncate_log_wq);
6104 flush_workqueue(ocfs2_wq);
6105
6106 status = ocfs2_flush_truncate_log(osb);
6107 if (status < 0)
6108 mlog_errno(status);
6109
6110 brelse(osb->osb_tl_bh);
6111 iput(osb->osb_tl_inode);
6112 }
6113
6114 mlog_exit_void();
6115}
6116
6117int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6118{
6119 int status;
6120 struct inode *tl_inode = NULL;
6121 struct buffer_head *tl_bh = NULL;
6122
6123 mlog_entry_void();
6124
6125 status = ocfs2_get_truncate_log_info(osb,
6126 osb->slot_num,
6127 &tl_inode,
6128 &tl_bh);
6129 if (status < 0)
6130 mlog_errno(status);
6131
6132 /* ocfs2_truncate_log_shutdown keys on the existence of
6133 * osb->osb_tl_inode so we don't set any of the osb variables
6134 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006135 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6136 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006137 osb->osb_tl_bh = tl_bh;
6138 osb->osb_tl_inode = tl_inode;
6139
6140 mlog_exit(status);
6141 return status;
6142}
6143
Mark Fasheh2b604352007-06-22 15:45:27 -07006144/*
6145 * Delayed de-allocation of suballocator blocks.
6146 *
6147 * Some sets of block de-allocations might involve multiple suballocator inodes.
6148 *
6149 * The locking for this can get extremely complicated, especially when
6150 * the suballocator inodes to delete from aren't known until deep
6151 * within an unrelated codepath.
6152 *
6153 * ocfs2_extent_block structures are a good example of this - an inode
6154 * btree could have been grown by any number of nodes each allocating
6155 * out of their own suballoc inode.
6156 *
6157 * These structures allow the delay of block de-allocation until a
6158 * later time, when locking of multiple cluster inodes won't cause
6159 * deadlock.
6160 */
6161
6162/*
Tao Ma2891d292008-11-12 08:26:58 +08006163 * Describe a single bit freed from a suballocator. For the block
6164 * suballocators, it represents one block. For the global cluster
6165 * allocator, it represents some clusters and free_bit indicates
6166 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006167 */
6168struct ocfs2_cached_block_free {
6169 struct ocfs2_cached_block_free *free_next;
6170 u64 free_blk;
6171 unsigned int free_bit;
6172};
6173
6174struct ocfs2_per_slot_free_list {
6175 struct ocfs2_per_slot_free_list *f_next_suballocator;
6176 int f_inode_type;
6177 int f_slot;
6178 struct ocfs2_cached_block_free *f_first;
6179};
6180
Tao Ma2891d292008-11-12 08:26:58 +08006181static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6182 int sysfile_type,
6183 int slot,
6184 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006185{
6186 int ret;
6187 u64 bg_blkno;
6188 handle_t *handle;
6189 struct inode *inode;
6190 struct buffer_head *di_bh = NULL;
6191 struct ocfs2_cached_block_free *tmp;
6192
6193 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6194 if (!inode) {
6195 ret = -EINVAL;
6196 mlog_errno(ret);
6197 goto out;
6198 }
6199
6200 mutex_lock(&inode->i_mutex);
6201
Mark Fashehe63aecb62007-10-18 15:30:42 -07006202 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006203 if (ret) {
6204 mlog_errno(ret);
6205 goto out_mutex;
6206 }
6207
6208 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6209 if (IS_ERR(handle)) {
6210 ret = PTR_ERR(handle);
6211 mlog_errno(ret);
6212 goto out_unlock;
6213 }
6214
6215 while (head) {
6216 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6217 head->free_bit);
6218 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6219 head->free_bit, (unsigned long long)head->free_blk);
6220
6221 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6222 head->free_bit, bg_blkno, 1);
6223 if (ret) {
6224 mlog_errno(ret);
6225 goto out_journal;
6226 }
6227
6228 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6229 if (ret) {
6230 mlog_errno(ret);
6231 goto out_journal;
6232 }
6233
6234 tmp = head;
6235 head = head->free_next;
6236 kfree(tmp);
6237 }
6238
6239out_journal:
6240 ocfs2_commit_trans(osb, handle);
6241
6242out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006243 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006244 brelse(di_bh);
6245out_mutex:
6246 mutex_unlock(&inode->i_mutex);
6247 iput(inode);
6248out:
6249 while(head) {
6250 /* Premature exit may have left some dangling items. */
6251 tmp = head;
6252 head = head->free_next;
6253 kfree(tmp);
6254 }
6255
6256 return ret;
6257}
6258
Tao Ma2891d292008-11-12 08:26:58 +08006259int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6260 u64 blkno, unsigned int bit)
6261{
6262 int ret = 0;
6263 struct ocfs2_cached_block_free *item;
6264
6265 item = kmalloc(sizeof(*item), GFP_NOFS);
6266 if (item == NULL) {
6267 ret = -ENOMEM;
6268 mlog_errno(ret);
6269 return ret;
6270 }
6271
6272 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6273 bit, (unsigned long long)blkno);
6274
6275 item->free_blk = blkno;
6276 item->free_bit = bit;
6277 item->free_next = ctxt->c_global_allocator;
6278
6279 ctxt->c_global_allocator = item;
6280 return ret;
6281}
6282
6283static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6284 struct ocfs2_cached_block_free *head)
6285{
6286 struct ocfs2_cached_block_free *tmp;
6287 struct inode *tl_inode = osb->osb_tl_inode;
6288 handle_t *handle;
6289 int ret = 0;
6290
6291 mutex_lock(&tl_inode->i_mutex);
6292
6293 while (head) {
6294 if (ocfs2_truncate_log_needs_flush(osb)) {
6295 ret = __ocfs2_flush_truncate_log(osb);
6296 if (ret < 0) {
6297 mlog_errno(ret);
6298 break;
6299 }
6300 }
6301
6302 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6303 if (IS_ERR(handle)) {
6304 ret = PTR_ERR(handle);
6305 mlog_errno(ret);
6306 break;
6307 }
6308
6309 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6310 head->free_bit);
6311
6312 ocfs2_commit_trans(osb, handle);
6313 tmp = head;
6314 head = head->free_next;
6315 kfree(tmp);
6316
6317 if (ret < 0) {
6318 mlog_errno(ret);
6319 break;
6320 }
6321 }
6322
6323 mutex_unlock(&tl_inode->i_mutex);
6324
6325 while (head) {
6326 /* Premature exit may have left some dangling items. */
6327 tmp = head;
6328 head = head->free_next;
6329 kfree(tmp);
6330 }
6331
6332 return ret;
6333}
6334
Mark Fasheh2b604352007-06-22 15:45:27 -07006335int ocfs2_run_deallocs(struct ocfs2_super *osb,
6336 struct ocfs2_cached_dealloc_ctxt *ctxt)
6337{
6338 int ret = 0, ret2;
6339 struct ocfs2_per_slot_free_list *fl;
6340
6341 if (!ctxt)
6342 return 0;
6343
6344 while (ctxt->c_first_suballocator) {
6345 fl = ctxt->c_first_suballocator;
6346
6347 if (fl->f_first) {
6348 mlog(0, "Free items: (type %u, slot %d)\n",
6349 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006350 ret2 = ocfs2_free_cached_blocks(osb,
6351 fl->f_inode_type,
6352 fl->f_slot,
6353 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006354 if (ret2)
6355 mlog_errno(ret2);
6356 if (!ret)
6357 ret = ret2;
6358 }
6359
6360 ctxt->c_first_suballocator = fl->f_next_suballocator;
6361 kfree(fl);
6362 }
6363
Tao Ma2891d292008-11-12 08:26:58 +08006364 if (ctxt->c_global_allocator) {
6365 ret2 = ocfs2_free_cached_clusters(osb,
6366 ctxt->c_global_allocator);
6367 if (ret2)
6368 mlog_errno(ret2);
6369 if (!ret)
6370 ret = ret2;
6371
6372 ctxt->c_global_allocator = NULL;
6373 }
6374
Mark Fasheh2b604352007-06-22 15:45:27 -07006375 return ret;
6376}
6377
6378static struct ocfs2_per_slot_free_list *
6379ocfs2_find_per_slot_free_list(int type,
6380 int slot,
6381 struct ocfs2_cached_dealloc_ctxt *ctxt)
6382{
6383 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6384
6385 while (fl) {
6386 if (fl->f_inode_type == type && fl->f_slot == slot)
6387 return fl;
6388
6389 fl = fl->f_next_suballocator;
6390 }
6391
6392 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6393 if (fl) {
6394 fl->f_inode_type = type;
6395 fl->f_slot = slot;
6396 fl->f_first = NULL;
6397 fl->f_next_suballocator = ctxt->c_first_suballocator;
6398
6399 ctxt->c_first_suballocator = fl;
6400 }
6401 return fl;
6402}
6403
6404static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6405 int type, int slot, u64 blkno,
6406 unsigned int bit)
6407{
6408 int ret;
6409 struct ocfs2_per_slot_free_list *fl;
6410 struct ocfs2_cached_block_free *item;
6411
6412 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6413 if (fl == NULL) {
6414 ret = -ENOMEM;
6415 mlog_errno(ret);
6416 goto out;
6417 }
6418
6419 item = kmalloc(sizeof(*item), GFP_NOFS);
6420 if (item == NULL) {
6421 ret = -ENOMEM;
6422 mlog_errno(ret);
6423 goto out;
6424 }
6425
6426 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6427 type, slot, bit, (unsigned long long)blkno);
6428
6429 item->free_blk = blkno;
6430 item->free_bit = bit;
6431 item->free_next = fl->f_first;
6432
6433 fl->f_first = item;
6434
6435 ret = 0;
6436out:
6437 return ret;
6438}
6439
Mark Fasheh59a5e412007-06-22 15:52:36 -07006440static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6441 struct ocfs2_extent_block *eb)
6442{
6443 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6444 le16_to_cpu(eb->h_suballoc_slot),
6445 le64_to_cpu(eb->h_blkno),
6446 le16_to_cpu(eb->h_suballoc_bit));
6447}
6448
Mark Fashehccd979b2005-12-15 14:31:24 -08006449/* This function will figure out whether the currently last extent
6450 * block will be deleted, and if it will, what the new last extent
6451 * block will be so we can update his h_next_leaf_blk field, as well
6452 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006453static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006454 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006455 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006456 struct buffer_head **new_last_eb)
6457{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006458 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006459 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006460 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006461 struct ocfs2_extent_block *eb;
6462 struct ocfs2_extent_list *el;
6463 struct buffer_head *bh = NULL;
6464
6465 *new_last_eb = NULL;
6466
Mark Fashehccd979b2005-12-15 14:31:24 -08006467 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006468 if (!path->p_tree_depth)
6469 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006470
6471 /* trunc to zero special case - this makes tree_depth = 0
6472 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006473 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006474 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006475
Mark Fashehdcd05382007-01-16 11:32:23 -08006476 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006477 BUG_ON(!el->l_next_free_rec);
6478
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006479 /*
6480 * Make sure that this extent list will actually be empty
6481 * after we clear away the data. We can shortcut out if
6482 * there's more than one non-empty extent in the
6483 * list. Otherwise, a check of the remaining extent is
6484 * necessary.
6485 */
6486 next_free = le16_to_cpu(el->l_next_free_rec);
6487 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006488 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006489 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006490 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006491
6492 /* We may have a valid extent in index 1, check it. */
6493 if (next_free == 2)
6494 rec = &el->l_recs[1];
6495
6496 /*
6497 * Fall through - no more nonempty extents, so we want
6498 * to delete this leaf.
6499 */
6500 } else {
6501 if (next_free > 1)
6502 goto out;
6503
6504 rec = &el->l_recs[0];
6505 }
6506
6507 if (rec) {
6508 /*
6509 * Check it we'll only be trimming off the end of this
6510 * cluster.
6511 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006512 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006513 goto out;
6514 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006515
Mark Fashehdcd05382007-01-16 11:32:23 -08006516 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6517 if (ret) {
6518 mlog_errno(ret);
6519 goto out;
6520 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006521
Mark Fashehdcd05382007-01-16 11:32:23 -08006522 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6523 if (ret) {
6524 mlog_errno(ret);
6525 goto out;
6526 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006527
Mark Fashehdcd05382007-01-16 11:32:23 -08006528 eb = (struct ocfs2_extent_block *) bh->b_data;
6529 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006530
6531 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6532 * Any corruption is a code bug. */
6533 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006534
6535 *new_last_eb = bh;
6536 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006537 mlog(0, "returning block %llu, (cpos: %u)\n",
6538 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6539out:
6540 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006541
Mark Fashehdcd05382007-01-16 11:32:23 -08006542 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006543}
6544
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006545/*
6546 * Trim some clusters off the rightmost edge of a tree. Only called
6547 * during truncate.
6548 *
6549 * The caller needs to:
6550 * - start journaling of each path component.
6551 * - compute and fully set up any new last ext block
6552 */
6553static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6554 handle_t *handle, struct ocfs2_truncate_context *tc,
6555 u32 clusters_to_del, u64 *delete_start)
6556{
6557 int ret, i, index = path->p_tree_depth;
6558 u32 new_edge = 0;
6559 u64 deleted_eb = 0;
6560 struct buffer_head *bh;
6561 struct ocfs2_extent_list *el;
6562 struct ocfs2_extent_rec *rec;
6563
6564 *delete_start = 0;
6565
6566 while (index >= 0) {
6567 bh = path->p_node[index].bh;
6568 el = path->p_node[index].el;
6569
6570 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6571 index, (unsigned long long)bh->b_blocknr);
6572
6573 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6574
6575 if (index !=
6576 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6577 ocfs2_error(inode->i_sb,
6578 "Inode %lu has invalid ext. block %llu",
6579 inode->i_ino,
6580 (unsigned long long)bh->b_blocknr);
6581 ret = -EROFS;
6582 goto out;
6583 }
6584
6585find_tail_record:
6586 i = le16_to_cpu(el->l_next_free_rec) - 1;
6587 rec = &el->l_recs[i];
6588
6589 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6590 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006591 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006592 (unsigned long long)le64_to_cpu(rec->e_blkno),
6593 le16_to_cpu(el->l_next_free_rec));
6594
Mark Fashehe48edee2007-03-07 16:46:57 -08006595 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006596
6597 if (le16_to_cpu(el->l_tree_depth) == 0) {
6598 /*
6599 * If the leaf block contains a single empty
6600 * extent and no records, we can just remove
6601 * the block.
6602 */
6603 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6604 memset(rec, 0,
6605 sizeof(struct ocfs2_extent_rec));
6606 el->l_next_free_rec = cpu_to_le16(0);
6607
6608 goto delete;
6609 }
6610
6611 /*
6612 * Remove any empty extents by shifting things
6613 * left. That should make life much easier on
6614 * the code below. This condition is rare
6615 * enough that we shouldn't see a performance
6616 * hit.
6617 */
6618 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6619 le16_add_cpu(&el->l_next_free_rec, -1);
6620
6621 for(i = 0;
6622 i < le16_to_cpu(el->l_next_free_rec); i++)
6623 el->l_recs[i] = el->l_recs[i + 1];
6624
6625 memset(&el->l_recs[i], 0,
6626 sizeof(struct ocfs2_extent_rec));
6627
6628 /*
6629 * We've modified our extent list. The
6630 * simplest way to handle this change
6631 * is to being the search from the
6632 * start again.
6633 */
6634 goto find_tail_record;
6635 }
6636
Mark Fashehe48edee2007-03-07 16:46:57 -08006637 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006638
6639 /*
6640 * We'll use "new_edge" on our way back up the
6641 * tree to know what our rightmost cpos is.
6642 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006643 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006644 new_edge += le32_to_cpu(rec->e_cpos);
6645
6646 /*
6647 * The caller will use this to delete data blocks.
6648 */
6649 *delete_start = le64_to_cpu(rec->e_blkno)
6650 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006651 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006652
6653 /*
6654 * If it's now empty, remove this record.
6655 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006656 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006657 memset(rec, 0,
6658 sizeof(struct ocfs2_extent_rec));
6659 le16_add_cpu(&el->l_next_free_rec, -1);
6660 }
6661 } else {
6662 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6663 memset(rec, 0,
6664 sizeof(struct ocfs2_extent_rec));
6665 le16_add_cpu(&el->l_next_free_rec, -1);
6666
6667 goto delete;
6668 }
6669
6670 /* Can this actually happen? */
6671 if (le16_to_cpu(el->l_next_free_rec) == 0)
6672 goto delete;
6673
6674 /*
6675 * We never actually deleted any clusters
6676 * because our leaf was empty. There's no
6677 * reason to adjust the rightmost edge then.
6678 */
6679 if (new_edge == 0)
6680 goto delete;
6681
Mark Fashehe48edee2007-03-07 16:46:57 -08006682 rec->e_int_clusters = cpu_to_le32(new_edge);
6683 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006684 -le32_to_cpu(rec->e_cpos));
6685
6686 /*
6687 * A deleted child record should have been
6688 * caught above.
6689 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006690 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006691 }
6692
6693delete:
6694 ret = ocfs2_journal_dirty(handle, bh);
6695 if (ret) {
6696 mlog_errno(ret);
6697 goto out;
6698 }
6699
6700 mlog(0, "extent list container %llu, after: record %d: "
6701 "(%u, %u, %llu), next = %u.\n",
6702 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006703 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006704 (unsigned long long)le64_to_cpu(rec->e_blkno),
6705 le16_to_cpu(el->l_next_free_rec));
6706
6707 /*
6708 * We must be careful to only attempt delete of an
6709 * extent block (and not the root inode block).
6710 */
6711 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6712 struct ocfs2_extent_block *eb =
6713 (struct ocfs2_extent_block *)bh->b_data;
6714
6715 /*
6716 * Save this for use when processing the
6717 * parent block.
6718 */
6719 deleted_eb = le64_to_cpu(eb->h_blkno);
6720
6721 mlog(0, "deleting this extent block.\n");
6722
Joel Becker8cb471e2009-02-10 20:00:41 -08006723 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006724
Mark Fashehe48edee2007-03-07 16:46:57 -08006725 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006726 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6727 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6728
Mark Fasheh59a5e412007-06-22 15:52:36 -07006729 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6730 /* An error here is not fatal. */
6731 if (ret < 0)
6732 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006733 } else {
6734 deleted_eb = 0;
6735 }
6736
6737 index--;
6738 }
6739
6740 ret = 0;
6741out:
6742 return ret;
6743}
6744
Mark Fashehccd979b2005-12-15 14:31:24 -08006745static int ocfs2_do_truncate(struct ocfs2_super *osb,
6746 unsigned int clusters_to_del,
6747 struct inode *inode,
6748 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006749 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006750 struct ocfs2_truncate_context *tc,
6751 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006752{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006753 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006754 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006755 struct ocfs2_extent_block *last_eb = NULL;
6756 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006757 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006758 u64 delete_blk = 0;
6759
6760 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6761
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006762 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006763 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006764 if (status < 0) {
6765 mlog_errno(status);
6766 goto bail;
6767 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006768
Mark Fashehdcd05382007-01-16 11:32:23 -08006769 /*
6770 * Each component will be touched, so we might as well journal
6771 * here to avoid having to handle errors later.
6772 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006773 status = ocfs2_journal_access_path(inode, handle, path);
6774 if (status < 0) {
6775 mlog_errno(status);
6776 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006777 }
6778
6779 if (last_eb_bh) {
Joel Becker13723d02008-10-17 19:25:01 -07006780 status = ocfs2_journal_access_eb(handle, inode, last_eb_bh,
6781 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08006782 if (status < 0) {
6783 mlog_errno(status);
6784 goto bail;
6785 }
6786
6787 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6788 }
6789
6790 el = &(fe->id2.i_list);
6791
6792 /*
6793 * Lower levels depend on this never happening, but it's best
6794 * to check it up here before changing the tree.
6795 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006796 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006797 ocfs2_error(inode->i_sb,
6798 "Inode %lu has an empty extent record, depth %u\n",
6799 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006800 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006801 goto bail;
6802 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006803
Jan Karaa90714c2008-10-09 19:38:40 +02006804 vfs_dq_free_space_nodirty(inode,
6805 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006806 spin_lock(&OCFS2_I(inode)->ip_lock);
6807 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6808 clusters_to_del;
6809 spin_unlock(&OCFS2_I(inode)->ip_lock);
6810 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006811 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006812
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006813 status = ocfs2_trim_tree(inode, path, handle, tc,
6814 clusters_to_del, &delete_blk);
6815 if (status) {
6816 mlog_errno(status);
6817 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006818 }
6819
Mark Fashehdcd05382007-01-16 11:32:23 -08006820 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006821 /* trunc to zero is a special case. */
6822 el->l_tree_depth = 0;
6823 fe->i_last_eb_blk = 0;
6824 } else if (last_eb)
6825 fe->i_last_eb_blk = last_eb->h_blkno;
6826
6827 status = ocfs2_journal_dirty(handle, fe_bh);
6828 if (status < 0) {
6829 mlog_errno(status);
6830 goto bail;
6831 }
6832
6833 if (last_eb) {
6834 /* If there will be a new last extent block, then by
6835 * definition, there cannot be any leaves to the right of
6836 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006837 last_eb->h_next_leaf_blk = 0;
6838 status = ocfs2_journal_dirty(handle, last_eb_bh);
6839 if (status < 0) {
6840 mlog_errno(status);
6841 goto bail;
6842 }
6843 }
6844
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006845 if (delete_blk) {
6846 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6847 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006848 if (status < 0) {
6849 mlog_errno(status);
6850 goto bail;
6851 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006852 }
6853 status = 0;
6854bail:
Tao Ma60e2ec42009-08-12 14:42:47 +08006855 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006856 mlog_exit(status);
6857 return status;
6858}
6859
Joel Becker2b4e30f2008-09-03 20:03:41 -07006860static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006861{
6862 set_buffer_uptodate(bh);
6863 mark_buffer_dirty(bh);
6864 return 0;
6865}
6866
Mark Fasheh1d410a62007-09-07 14:20:45 -07006867static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6868 unsigned int from, unsigned int to,
6869 struct page *page, int zero, u64 *phys)
6870{
6871 int ret, partial = 0;
6872
6873 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6874 if (ret)
6875 mlog_errno(ret);
6876
6877 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006878 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006879
6880 /*
6881 * Need to set the buffers we zero'd into uptodate
6882 * here if they aren't - ocfs2_map_page_blocks()
6883 * might've skipped some
6884 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006885 ret = walk_page_buffers(handle, page_buffers(page),
6886 from, to, &partial,
6887 ocfs2_zero_func);
6888 if (ret < 0)
6889 mlog_errno(ret);
6890 else if (ocfs2_should_order_data(inode)) {
6891 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006892 if (ret < 0)
6893 mlog_errno(ret);
6894 }
6895
6896 if (!partial)
6897 SetPageUptodate(page);
6898
6899 flush_dcache_page(page);
6900}
6901
Mark Fasheh35edec12007-07-06 14:41:18 -07006902static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6903 loff_t end, struct page **pages,
6904 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006905{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006906 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006907 struct page *page;
6908 unsigned int from, to = PAGE_CACHE_SIZE;
6909 struct super_block *sb = inode->i_sb;
6910
6911 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6912
6913 if (numpages == 0)
6914 goto out;
6915
Mark Fasheh35edec12007-07-06 14:41:18 -07006916 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006917 for(i = 0; i < numpages; i++) {
6918 page = pages[i];
6919
Mark Fasheh35edec12007-07-06 14:41:18 -07006920 from = start & (PAGE_CACHE_SIZE - 1);
6921 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6922 to = end & (PAGE_CACHE_SIZE - 1);
6923
Mark Fasheh60b11392007-02-16 11:46:50 -08006924 BUG_ON(from > PAGE_CACHE_SIZE);
6925 BUG_ON(to > PAGE_CACHE_SIZE);
6926
Mark Fasheh1d410a62007-09-07 14:20:45 -07006927 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6928 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006929
Mark Fasheh35edec12007-07-06 14:41:18 -07006930 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006931 }
6932out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006933 if (pages)
6934 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006935}
6936
Mark Fasheh35edec12007-07-06 14:41:18 -07006937static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006938 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006939{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006940 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006941 struct super_block *sb = inode->i_sb;
6942 struct address_space *mapping = inode->i_mapping;
6943 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006944 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006945
Mark Fasheh35edec12007-07-06 14:41:18 -07006946 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006947
Mark Fasheh35edec12007-07-06 14:41:18 -07006948 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6949 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6950
Mark Fasheh1d410a62007-09-07 14:20:45 -07006951 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006952 last_page_bytes = PAGE_ALIGN(end);
6953 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006954 do {
6955 pages[numpages] = grab_cache_page(mapping, index);
6956 if (!pages[numpages]) {
6957 ret = -ENOMEM;
6958 mlog_errno(ret);
6959 goto out;
6960 }
6961
6962 numpages++;
6963 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006964 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006965
6966out:
6967 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006968 if (pages)
6969 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006970 numpages = 0;
6971 }
6972
6973 *num = numpages;
6974
6975 return ret;
6976}
6977
6978/*
6979 * Zero the area past i_size but still within an allocated
6980 * cluster. This avoids exposing nonzero data on subsequent file
6981 * extends.
6982 *
6983 * We need to call this before i_size is updated on the inode because
6984 * otherwise block_write_full_page() will skip writeout of pages past
6985 * i_size. The new_i_size parameter is passed for this reason.
6986 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006987int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6988 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006989{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006990 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006991 struct page **pages = NULL;
6992 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006993 unsigned int ext_flags;
6994 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006995
6996 /*
6997 * File systems which don't support sparse files zero on every
6998 * extend.
6999 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07007000 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08007001 return 0;
7002
Mark Fasheh1d410a62007-09-07 14:20:45 -07007003 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08007004 sizeof(struct page *), GFP_NOFS);
7005 if (pages == NULL) {
7006 ret = -ENOMEM;
7007 mlog_errno(ret);
7008 goto out;
7009 }
7010
Mark Fasheh1d410a62007-09-07 14:20:45 -07007011 if (range_start == range_end)
7012 goto out;
7013
7014 ret = ocfs2_extent_map_get_blocks(inode,
7015 range_start >> sb->s_blocksize_bits,
7016 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08007017 if (ret) {
7018 mlog_errno(ret);
7019 goto out;
7020 }
7021
Mark Fasheh1d410a62007-09-07 14:20:45 -07007022 /*
7023 * Tail is a hole, or is marked unwritten. In either case, we
7024 * can count on read and write to return/push zero's.
7025 */
7026 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08007027 goto out;
7028
Mark Fasheh1d410a62007-09-07 14:20:45 -07007029 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7030 &numpages);
7031 if (ret) {
7032 mlog_errno(ret);
7033 goto out;
7034 }
7035
Mark Fasheh35edec12007-07-06 14:41:18 -07007036 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7037 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08007038
7039 /*
7040 * Initiate writeout of the pages we zero'd here. We don't
7041 * wait on them - the truncate_inode_pages() call later will
7042 * do that for us.
7043 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007044 ret = do_sync_mapping_range(inode->i_mapping, range_start,
7045 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08007046 if (ret)
7047 mlog_errno(ret);
7048
7049out:
7050 if (pages)
7051 kfree(pages);
7052
7053 return ret;
7054}
7055
Tiger Yangfdd77702008-08-18 17:08:55 +08007056static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7057 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007058{
7059 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08007060 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007061
Tiger Yangfdd77702008-08-18 17:08:55 +08007062 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7063 memset(&di->id2, 0, blocksize -
7064 offsetof(struct ocfs2_dinode, id2) -
7065 xattrsize);
7066 else
7067 memset(&di->id2, 0, blocksize -
7068 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007069}
7070
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007071void ocfs2_dinode_new_extent_list(struct inode *inode,
7072 struct ocfs2_dinode *di)
7073{
Tiger Yangfdd77702008-08-18 17:08:55 +08007074 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007075 di->id2.i_list.l_tree_depth = 0;
7076 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08007077 di->id2.i_list.l_count = cpu_to_le16(
7078 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007079}
7080
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007081void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7082{
7083 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7084 struct ocfs2_inline_data *idata = &di->id2.i_data;
7085
7086 spin_lock(&oi->ip_lock);
7087 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7088 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7089 spin_unlock(&oi->ip_lock);
7090
7091 /*
7092 * We clear the entire i_data structure here so that all
7093 * fields can be properly initialized.
7094 */
Tiger Yangfdd77702008-08-18 17:08:55 +08007095 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007096
Tiger Yangfdd77702008-08-18 17:08:55 +08007097 idata->id_count = cpu_to_le16(
7098 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007099}
7100
7101int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7102 struct buffer_head *di_bh)
7103{
7104 int ret, i, has_data, num_pages = 0;
7105 handle_t *handle;
7106 u64 uninitialized_var(block);
7107 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7108 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7109 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007110 struct ocfs2_alloc_context *data_ac = NULL;
7111 struct page **pages = NULL;
7112 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07007113 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02007114 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007115
7116 has_data = i_size_read(inode) ? 1 : 0;
7117
7118 if (has_data) {
7119 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7120 sizeof(struct page *), GFP_NOFS);
7121 if (pages == NULL) {
7122 ret = -ENOMEM;
7123 mlog_errno(ret);
7124 goto out;
7125 }
7126
7127 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7128 if (ret) {
7129 mlog_errno(ret);
7130 goto out;
7131 }
7132 }
7133
Jan Karaa90714c2008-10-09 19:38:40 +02007134 handle = ocfs2_start_trans(osb,
7135 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007136 if (IS_ERR(handle)) {
7137 ret = PTR_ERR(handle);
7138 mlog_errno(ret);
7139 goto out_unlock;
7140 }
7141
Joel Becker13723d02008-10-17 19:25:01 -07007142 ret = ocfs2_journal_access_di(handle, inode, di_bh,
7143 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007144 if (ret) {
7145 mlog_errno(ret);
7146 goto out_commit;
7147 }
7148
7149 if (has_data) {
7150 u32 bit_off, num;
7151 unsigned int page_end;
7152 u64 phys;
7153
Jan Karaa90714c2008-10-09 19:38:40 +02007154 if (vfs_dq_alloc_space_nodirty(inode,
7155 ocfs2_clusters_to_bytes(osb->sb, 1))) {
7156 ret = -EDQUOT;
7157 goto out_commit;
7158 }
7159 did_quota = 1;
7160
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007161 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7162 &num);
7163 if (ret) {
7164 mlog_errno(ret);
7165 goto out_commit;
7166 }
7167
7168 /*
7169 * Save two copies, one for insert, and one that can
7170 * be changed by ocfs2_map_and_dirty_page() below.
7171 */
7172 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7173
7174 /*
7175 * Non sparse file systems zero on extend, so no need
7176 * to do that now.
7177 */
7178 if (!ocfs2_sparse_alloc(osb) &&
7179 PAGE_CACHE_SIZE < osb->s_clustersize)
7180 end = PAGE_CACHE_SIZE;
7181
7182 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7183 if (ret) {
7184 mlog_errno(ret);
7185 goto out_commit;
7186 }
7187
7188 /*
7189 * This should populate the 1st page for us and mark
7190 * it up to date.
7191 */
7192 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7193 if (ret) {
7194 mlog_errno(ret);
7195 goto out_commit;
7196 }
7197
7198 page_end = PAGE_CACHE_SIZE;
7199 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7200 page_end = osb->s_clustersize;
7201
7202 for (i = 0; i < num_pages; i++)
7203 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7204 pages[i], i > 0, &phys);
7205 }
7206
7207 spin_lock(&oi->ip_lock);
7208 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7209 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7210 spin_unlock(&oi->ip_lock);
7211
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007212 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007213
7214 ocfs2_journal_dirty(handle, di_bh);
7215
7216 if (has_data) {
7217 /*
7218 * An error at this point should be extremely rare. If
7219 * this proves to be false, we could always re-build
7220 * the in-inode data from our pages.
7221 */
Joel Becker8d6220d2008-08-22 12:46:09 -07007222 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07007223 ret = ocfs2_insert_extent(osb, handle, inode, &et,
7224 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007225 if (ret) {
7226 mlog_errno(ret);
7227 goto out_commit;
7228 }
7229
7230 inode->i_blocks = ocfs2_inode_sector_count(inode);
7231 }
7232
7233out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007234 if (ret < 0 && did_quota)
7235 vfs_dq_free_space_nodirty(inode,
7236 ocfs2_clusters_to_bytes(osb->sb, 1));
7237
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007238 ocfs2_commit_trans(osb, handle);
7239
7240out_unlock:
7241 if (data_ac)
7242 ocfs2_free_alloc_context(data_ac);
7243
7244out:
7245 if (pages) {
7246 ocfs2_unlock_and_free_pages(pages, num_pages);
7247 kfree(pages);
7248 }
7249
7250 return ret;
7251}
7252
Mark Fashehccd979b2005-12-15 14:31:24 -08007253/*
7254 * It is expected, that by the time you call this function,
7255 * inode->i_size and fe->i_size have been adjusted.
7256 *
7257 * WARNING: This will kfree the truncate context
7258 */
7259int ocfs2_commit_truncate(struct ocfs2_super *osb,
7260 struct inode *inode,
7261 struct buffer_head *fe_bh,
7262 struct ocfs2_truncate_context *tc)
7263{
7264 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08007265 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08007266 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07007267 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007268 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08007269 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08007270 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007271
7272 mlog_entry_void();
7273
Mark Fashehdcd05382007-01-16 11:32:23 -08007274 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007275 i_size_read(inode));
7276
Joel Becker13723d02008-10-17 19:25:01 -07007277 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7278 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08007279 if (!path) {
7280 status = -ENOMEM;
7281 mlog_errno(status);
7282 goto bail;
7283 }
Mark Fasheh83418972007-04-23 18:53:12 -07007284
7285 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7286
Mark Fashehccd979b2005-12-15 14:31:24 -08007287start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007288 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007289 * Check that we still have allocation to delete.
7290 */
7291 if (OCFS2_I(inode)->ip_clusters == 0) {
7292 status = 0;
7293 goto bail;
7294 }
7295
7296 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007297 * Truncate always works against the rightmost tree branch.
7298 */
7299 status = ocfs2_find_path(inode, path, UINT_MAX);
7300 if (status) {
7301 mlog_errno(status);
7302 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007303 }
7304
Mark Fashehdcd05382007-01-16 11:32:23 -08007305 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7306 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7307
7308 /*
7309 * By now, el will point to the extent list on the bottom most
7310 * portion of this tree. Only the tail record is considered in
7311 * each pass.
7312 *
7313 * We handle the following cases, in order:
7314 * - empty extent: delete the remaining branch
7315 * - remove the entire record
7316 * - remove a partial record
7317 * - no record needs to be removed (truncate has completed)
7318 */
7319 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007320 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7321 ocfs2_error(inode->i_sb,
7322 "Inode %llu has empty extent block at %llu\n",
7323 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7324 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7325 status = -EROFS;
7326 goto bail;
7327 }
7328
Mark Fashehccd979b2005-12-15 14:31:24 -08007329 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08007330 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08007331 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007332 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7333 clusters_to_del = 0;
7334 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007335 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007336 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007337 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08007338 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08007339 new_highest_cpos;
7340 } else {
7341 status = 0;
7342 goto bail;
7343 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007344
Mark Fashehdcd05382007-01-16 11:32:23 -08007345 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7346 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7347
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007348 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007349 tl_sem = 1;
7350 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7351 * record is free for use. If there isn't any, we flush to get
7352 * an empty truncate log. */
7353 if (ocfs2_truncate_log_needs_flush(osb)) {
7354 status = __ocfs2_flush_truncate_log(osb);
7355 if (status < 0) {
7356 mlog_errno(status);
7357 goto bail;
7358 }
7359 }
7360
7361 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007362 (struct ocfs2_dinode *)fe_bh->b_data,
7363 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007364 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007365 if (IS_ERR(handle)) {
7366 status = PTR_ERR(handle);
7367 handle = NULL;
7368 mlog_errno(status);
7369 goto bail;
7370 }
7371
Mark Fashehdcd05382007-01-16 11:32:23 -08007372 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7373 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007374 if (status < 0) {
7375 mlog_errno(status);
7376 goto bail;
7377 }
7378
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007379 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007380 tl_sem = 0;
7381
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007382 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007383 handle = NULL;
7384
Mark Fashehdcd05382007-01-16 11:32:23 -08007385 ocfs2_reinit_path(path, 1);
7386
7387 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007388 * The check above will catch the case where we've truncated
7389 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007390 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007391 goto start;
7392
Mark Fashehccd979b2005-12-15 14:31:24 -08007393bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007394
7395 ocfs2_schedule_truncate_log_flush(osb, 1);
7396
7397 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007398 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007399
7400 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007401 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007402
Mark Fasheh59a5e412007-06-22 15:52:36 -07007403 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7404
Mark Fashehdcd05382007-01-16 11:32:23 -08007405 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007406
7407 /* This will drop the ext_alloc cluster lock for us */
7408 ocfs2_free_truncate_context(tc);
7409
7410 mlog_exit(status);
7411 return status;
7412}
7413
Mark Fashehccd979b2005-12-15 14:31:24 -08007414/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007415 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007416 */
7417int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7418 struct inode *inode,
7419 struct buffer_head *fe_bh,
7420 struct ocfs2_truncate_context **tc)
7421{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007422 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007423 unsigned int new_i_clusters;
7424 struct ocfs2_dinode *fe;
7425 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007426 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007427
7428 mlog_entry_void();
7429
7430 *tc = NULL;
7431
7432 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7433 i_size_read(inode));
7434 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7435
7436 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007437 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7438 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007439
Robert P. J. Daycd861282006-12-13 00:34:52 -08007440 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007441 if (!(*tc)) {
7442 status = -ENOMEM;
7443 mlog_errno(status);
7444 goto bail;
7445 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007446 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007447
Mark Fashehccd979b2005-12-15 14:31:24 -08007448 if (fe->id2.i_list.l_tree_depth) {
Joel Becker5e965812008-11-13 14:49:16 -08007449 status = ocfs2_read_extent_block(inode,
7450 le64_to_cpu(fe->i_last_eb_blk),
7451 &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007452 if (status < 0) {
7453 mlog_errno(status);
7454 goto bail;
7455 }
7456 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007457 }
7458
7459 (*tc)->tc_last_eb_bh = last_eb_bh;
7460
Mark Fashehccd979b2005-12-15 14:31:24 -08007461 status = 0;
7462bail:
7463 if (status < 0) {
7464 if (*tc)
7465 ocfs2_free_truncate_context(*tc);
7466 *tc = NULL;
7467 }
7468 mlog_exit_void();
7469 return status;
7470}
7471
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007472/*
7473 * 'start' is inclusive, 'end' is not.
7474 */
7475int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7476 unsigned int start, unsigned int end, int trunc)
7477{
7478 int ret;
7479 unsigned int numbytes;
7480 handle_t *handle;
7481 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7482 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7483 struct ocfs2_inline_data *idata = &di->id2.i_data;
7484
7485 if (end > i_size_read(inode))
7486 end = i_size_read(inode);
7487
7488 BUG_ON(start >= end);
7489
7490 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7491 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7492 !ocfs2_supports_inline_data(osb)) {
7493 ocfs2_error(inode->i_sb,
7494 "Inline data flags for inode %llu don't agree! "
7495 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7496 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7497 le16_to_cpu(di->i_dyn_features),
7498 OCFS2_I(inode)->ip_dyn_features,
7499 osb->s_feature_incompat);
7500 ret = -EROFS;
7501 goto out;
7502 }
7503
7504 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7505 if (IS_ERR(handle)) {
7506 ret = PTR_ERR(handle);
7507 mlog_errno(ret);
7508 goto out;
7509 }
7510
Joel Becker13723d02008-10-17 19:25:01 -07007511 ret = ocfs2_journal_access_di(handle, inode, di_bh,
7512 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007513 if (ret) {
7514 mlog_errno(ret);
7515 goto out_commit;
7516 }
7517
7518 numbytes = end - start;
7519 memset(idata->id_data + start, 0, numbytes);
7520
7521 /*
7522 * No need to worry about the data page here - it's been
7523 * truncated already and inline data doesn't need it for
7524 * pushing zero's to disk, so we'll let readpage pick it up
7525 * later.
7526 */
7527 if (trunc) {
7528 i_size_write(inode, start);
7529 di->i_size = cpu_to_le64(start);
7530 }
7531
7532 inode->i_blocks = ocfs2_inode_sector_count(inode);
7533 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7534
7535 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7536 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7537
7538 ocfs2_journal_dirty(handle, di_bh);
7539
7540out_commit:
7541 ocfs2_commit_trans(osb, handle);
7542
7543out:
7544 return ret;
7545}
7546
Mark Fashehccd979b2005-12-15 14:31:24 -08007547static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7548{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007549 /*
7550 * The caller is responsible for completing deallocation
7551 * before freeing the context.
7552 */
7553 if (tc->tc_dealloc.c_first_suballocator != NULL)
7554 mlog(ML_NOTICE,
7555 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007556
Mark Fasheha81cb882008-10-07 14:25:16 -07007557 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007558
7559 kfree(tc);
7560}