blob: e1479faeb01c041de4b0fb00f899d6580952f045 [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 Becker6136ca52009-02-12 19:32:43 -080082 void (*eo_update_clusters)(struct ocfs2_extent_tree *et,
Joel Becker35dc0aa2008-08-20 16:25:06 -070083 u32 new_clusters);
Joel Becker1625f8a2008-08-21 17:11:10 -070084
85 /*
86 * If ->eo_insert_check() exists, it is called before rec is
87 * inserted into the extent tree. It is optional.
88 */
Joel Becker6136ca52009-02-12 19:32:43 -080089 int (*eo_insert_check)(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -070090 struct ocfs2_extent_rec *rec);
Joel Becker6136ca52009-02-12 19:32:43 -080091 int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
Joel Becker0ce10102008-08-20 17:19:50 -070092
Joel Becker1625f8a2008-08-21 17:11:10 -070093 /*
94 * --------------------------------------------------------------
95 * The remaining are internal to ocfs2_extent_tree and don't have
96 * accessor functions
97 */
98
99 /*
100 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
101 * It is required.
102 */
Joel Becker0ce10102008-08-20 17:19:50 -0700103 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -0700104
105 /*
106 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
107 * it exists. If it does not, et->et_max_leaf_clusters is set
108 * to 0 (unlimited). Optional.
109 */
Joel Becker6136ca52009-02-12 19:32:43 -0800110 void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800111};
112
Joel Beckerf99b9b72008-08-20 19:36:33 -0700113
114/*
115 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
116 * in the methods.
117 */
118static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
119static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
120 u64 blkno);
Joel Becker6136ca52009-02-12 19:32:43 -0800121static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700122 u32 clusters);
Joel Becker6136ca52009-02-12 19:32:43 -0800123static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700124 struct ocfs2_extent_rec *rec);
Joel Becker6136ca52009-02-12 19:32:43 -0800125static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700126static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
127static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
128 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
129 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
130 .eo_update_clusters = ocfs2_dinode_update_clusters,
131 .eo_insert_check = ocfs2_dinode_insert_check,
132 .eo_sanity_check = ocfs2_dinode_sanity_check,
133 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
Tao Mae7d4cb62008-08-18 17:38:44 +0800134};
135
136static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
137 u64 blkno)
138{
Joel Beckerea5efa12008-08-20 16:57:27 -0700139 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800140
Joel Beckerf99b9b72008-08-20 19:36:33 -0700141 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800142 di->i_last_eb_blk = cpu_to_le64(blkno);
143}
144
145static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
146{
Joel Beckerea5efa12008-08-20 16:57:27 -0700147 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800148
Joel Beckerf99b9b72008-08-20 19:36:33 -0700149 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800150 return le64_to_cpu(di->i_last_eb_blk);
151}
152
Joel Becker6136ca52009-02-12 19:32:43 -0800153static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +0800154 u32 clusters)
155{
Joel Becker6136ca52009-02-12 19:32:43 -0800156 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
Joel Beckerea5efa12008-08-20 16:57:27 -0700157 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800158
159 le32_add_cpu(&di->i_clusters, clusters);
Joel Becker6136ca52009-02-12 19:32:43 -0800160 spin_lock(&oi->ip_lock);
161 oi->ip_clusters = le32_to_cpu(di->i_clusters);
162 spin_unlock(&oi->ip_lock);
Tao Mae7d4cb62008-08-18 17:38:44 +0800163}
164
Joel Becker6136ca52009-02-12 19:32:43 -0800165static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700166 struct ocfs2_extent_rec *rec)
167{
Joel Becker6136ca52009-02-12 19:32:43 -0800168 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
169 struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
Joel Becker1e61ee72008-08-20 18:32:45 -0700170
Joel Becker6136ca52009-02-12 19:32:43 -0800171 BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
Joel Becker1e61ee72008-08-20 18:32:45 -0700172 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
Joel Becker6136ca52009-02-12 19:32:43 -0800173 (oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
Joel Becker1e61ee72008-08-20 18:32:45 -0700174 "Device %s, asking for sparse allocation: inode %llu, "
175 "cpos %u, clusters %u\n",
176 osb->dev_str,
Joel Becker6136ca52009-02-12 19:32:43 -0800177 (unsigned long long)oi->ip_blkno,
178 rec->e_cpos, oi->ip_clusters);
Joel Becker1e61ee72008-08-20 18:32:45 -0700179
180 return 0;
181}
182
Joel Becker6136ca52009-02-12 19:32:43 -0800183static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800184{
Joel Becker10995aa2008-11-13 14:49:12 -0800185 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800186
Joel Beckerf99b9b72008-08-20 19:36:33 -0700187 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Joel Becker10995aa2008-11-13 14:49:12 -0800188 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
Tao Mae7d4cb62008-08-18 17:38:44 +0800189
Joel Becker10995aa2008-11-13 14:49:12 -0800190 return 0;
Tao Mae7d4cb62008-08-18 17:38:44 +0800191}
192
Joel Beckerf99b9b72008-08-20 19:36:33 -0700193static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
194{
195 struct ocfs2_dinode *di = et->et_object;
196
197 et->et_root_el = &di->id2.i_list;
198}
199
Tao Mae7d4cb62008-08-18 17:38:44 +0800200
Joel Becker0ce10102008-08-20 17:19:50 -0700201static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
202{
Joel Becker2a50a742008-12-09 14:24:33 -0800203 struct ocfs2_xattr_value_buf *vb = et->et_object;
Joel Becker0ce10102008-08-20 17:19:50 -0700204
Joel Becker2a50a742008-12-09 14:24:33 -0800205 et->et_root_el = &vb->vb_xv->xr_list;
Joel Becker0ce10102008-08-20 17:19:50 -0700206}
207
Tao Maf56654c2008-08-18 17:38:48 +0800208static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
209 u64 blkno)
210{
Joel Becker2a50a742008-12-09 14:24:33 -0800211 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800212
Joel Becker2a50a742008-12-09 14:24:33 -0800213 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
Tao Maf56654c2008-08-18 17:38:48 +0800214}
215
216static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
217{
Joel Becker2a50a742008-12-09 14:24:33 -0800218 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800219
Joel Becker2a50a742008-12-09 14:24:33 -0800220 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
Tao Maf56654c2008-08-18 17:38:48 +0800221}
222
Joel Becker6136ca52009-02-12 19:32:43 -0800223static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
Tao Maf56654c2008-08-18 17:38:48 +0800224 u32 clusters)
225{
Joel Becker2a50a742008-12-09 14:24:33 -0800226 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800227
Joel Becker2a50a742008-12-09 14:24:33 -0800228 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800229}
230
Joel Becker1a09f552008-08-20 17:44:24 -0700231static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700232 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
233 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
234 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700235 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800236};
237
Joel Becker0ce10102008-08-20 17:19:50 -0700238static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
239{
240 struct ocfs2_xattr_block *xb = et->et_object;
241
242 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
243}
244
Joel Becker6136ca52009-02-12 19:32:43 -0800245static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
Joel Becker943cced2008-08-20 17:31:10 -0700246{
Joel Becker6136ca52009-02-12 19:32:43 -0800247 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Joel Becker943cced2008-08-20 17:31:10 -0700248 et->et_max_leaf_clusters =
Joel Becker6136ca52009-02-12 19:32:43 -0800249 ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
Joel Becker943cced2008-08-20 17:31:10 -0700250}
251
Tao Maba492612008-08-18 17:38:49 +0800252static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
253 u64 blkno)
254{
Joel Beckerea5efa12008-08-20 16:57:27 -0700255 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800256 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
257
258 xt->xt_last_eb_blk = cpu_to_le64(blkno);
259}
260
261static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
262{
Joel Beckerea5efa12008-08-20 16:57:27 -0700263 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800264 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
265
266 return le64_to_cpu(xt->xt_last_eb_blk);
267}
268
Joel Becker6136ca52009-02-12 19:32:43 -0800269static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
Tao Maba492612008-08-18 17:38:49 +0800270 u32 clusters)
271{
Joel Beckerea5efa12008-08-20 16:57:27 -0700272 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800273
274 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
275}
276
Tao Maba492612008-08-18 17:38:49 +0800277static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700278 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
279 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
280 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700281 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
Joel Becker943cced2008-08-20 17:31:10 -0700282 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
Tao Maba492612008-08-18 17:38:49 +0800283};
284
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800285static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
286 u64 blkno)
287{
288 struct ocfs2_dx_root_block *dx_root = et->et_object;
289
290 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
291}
292
293static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
294{
295 struct ocfs2_dx_root_block *dx_root = et->et_object;
296
297 return le64_to_cpu(dx_root->dr_last_eb_blk);
298}
299
Joel Becker6136ca52009-02-12 19:32:43 -0800300static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800301 u32 clusters)
302{
303 struct ocfs2_dx_root_block *dx_root = et->et_object;
304
305 le32_add_cpu(&dx_root->dr_clusters, clusters);
306}
307
Joel Becker6136ca52009-02-12 19:32:43 -0800308static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800309{
310 struct ocfs2_dx_root_block *dx_root = et->et_object;
311
312 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
313
314 return 0;
315}
316
317static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
318{
319 struct ocfs2_dx_root_block *dx_root = et->et_object;
320
321 et->et_root_el = &dx_root->dr_list;
322}
323
324static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
325 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
326 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
327 .eo_update_clusters = ocfs2_dx_root_update_clusters,
328 .eo_sanity_check = ocfs2_dx_root_sanity_check,
329 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
330};
331
Joel Becker8d6220d2008-08-22 12:46:09 -0700332static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
333 struct inode *inode,
334 struct buffer_head *bh,
Joel Becker13723d02008-10-17 19:25:01 -0700335 ocfs2_journal_access_func access,
Joel Becker8d6220d2008-08-22 12:46:09 -0700336 void *obj,
337 struct ocfs2_extent_tree_operations *ops)
Tao Mae7d4cb62008-08-18 17:38:44 +0800338{
Joel Becker1a09f552008-08-20 17:44:24 -0700339 et->et_ops = ops;
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700340 et->et_root_bh = bh;
Joel Beckerd9a0a1f2009-02-12 17:32:34 -0800341 et->et_ci = INODE_CACHE(inode);
Joel Becker13723d02008-10-17 19:25:01 -0700342 et->et_root_journal_access = access;
Joel Beckerea5efa12008-08-20 16:57:27 -0700343 if (!obj)
344 obj = (void *)bh->b_data;
345 et->et_object = obj;
Tao Mae7d4cb62008-08-18 17:38:44 +0800346
Joel Becker0ce10102008-08-20 17:19:50 -0700347 et->et_ops->eo_fill_root_el(et);
Joel Becker943cced2008-08-20 17:31:10 -0700348 if (!et->et_ops->eo_fill_max_leaf_clusters)
349 et->et_max_leaf_clusters = 0;
350 else
Joel Becker6136ca52009-02-12 19:32:43 -0800351 et->et_ops->eo_fill_max_leaf_clusters(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800352}
353
Joel Becker8d6220d2008-08-22 12:46:09 -0700354void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
355 struct inode *inode,
356 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700357{
Joel Becker13723d02008-10-17 19:25:01 -0700358 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_di,
359 NULL, &ocfs2_dinode_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700360}
361
Joel Becker8d6220d2008-08-22 12:46:09 -0700362void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700363 struct inode *inode,
Joel Becker8d6220d2008-08-22 12:46:09 -0700364 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700365{
Joel Becker13723d02008-10-17 19:25:01 -0700366 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_xb,
367 NULL, &ocfs2_xattr_tree_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700368}
369
Joel Becker8d6220d2008-08-22 12:46:09 -0700370void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
371 struct inode *inode,
Joel Becker2a50a742008-12-09 14:24:33 -0800372 struct ocfs2_xattr_value_buf *vb)
Tao Mae7d4cb62008-08-18 17:38:44 +0800373{
Joel Becker2a50a742008-12-09 14:24:33 -0800374 __ocfs2_init_extent_tree(et, inode, vb->vb_bh, vb->vb_access, vb,
Joel Becker8d6220d2008-08-22 12:46:09 -0700375 &ocfs2_xattr_value_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800376}
377
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800378void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
379 struct inode *inode,
380 struct buffer_head *bh)
381{
382 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_dr,
383 NULL, &ocfs2_dx_root_et_ops);
384}
385
Joel Becker35dc0aa2008-08-20 16:25:06 -0700386static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
387 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800388{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700389 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800390}
391
Joel Becker35dc0aa2008-08-20 16:25:06 -0700392static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800393{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700394 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800395}
396
Joel Becker6136ca52009-02-12 19:32:43 -0800397static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
Joel Becker35dc0aa2008-08-20 16:25:06 -0700398 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800399{
Joel Becker6136ca52009-02-12 19:32:43 -0800400 et->et_ops->eo_update_clusters(et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700401}
402
Joel Becker13723d02008-10-17 19:25:01 -0700403static inline int ocfs2_et_root_journal_access(handle_t *handle,
Joel Becker13723d02008-10-17 19:25:01 -0700404 struct ocfs2_extent_tree *et,
405 int type)
406{
Joel Beckerd9a0a1f2009-02-12 17:32:34 -0800407 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700408 type);
409}
410
Joel Becker6136ca52009-02-12 19:32:43 -0800411static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700412 struct ocfs2_extent_rec *rec)
413{
414 int ret = 0;
415
416 if (et->et_ops->eo_insert_check)
Joel Becker6136ca52009-02-12 19:32:43 -0800417 ret = et->et_ops->eo_insert_check(et, rec);
Joel Becker1e61ee72008-08-20 18:32:45 -0700418 return ret;
419}
420
Joel Becker6136ca52009-02-12 19:32:43 -0800421static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
Joel Becker35dc0aa2008-08-20 16:25:06 -0700422{
Joel Becker1e61ee72008-08-20 18:32:45 -0700423 int ret = 0;
424
425 if (et->et_ops->eo_sanity_check)
Joel Becker6136ca52009-02-12 19:32:43 -0800426 ret = et->et_ops->eo_sanity_check(et);
Joel Becker1e61ee72008-08-20 18:32:45 -0700427 return ret;
Tao Mae7d4cb62008-08-18 17:38:44 +0800428}
429
Mark Fashehccd979b2005-12-15 14:31:24 -0800430static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
Mark Fasheh59a5e412007-06-22 15:52:36 -0700431static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
432 struct ocfs2_extent_block *eb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800433
Mark Fashehdcd05382007-01-16 11:32:23 -0800434/*
435 * Structures which describe a path through a btree, and functions to
436 * manipulate them.
437 *
438 * The idea here is to be as generic as possible with the tree
439 * manipulation code.
440 */
441struct ocfs2_path_item {
442 struct buffer_head *bh;
443 struct ocfs2_extent_list *el;
444};
445
446#define OCFS2_MAX_PATH_DEPTH 5
447
448struct ocfs2_path {
Joel Becker13723d02008-10-17 19:25:01 -0700449 int p_tree_depth;
450 ocfs2_journal_access_func p_root_access;
451 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
Mark Fashehdcd05382007-01-16 11:32:23 -0800452};
453
454#define path_root_bh(_path) ((_path)->p_node[0].bh)
455#define path_root_el(_path) ((_path)->p_node[0].el)
Joel Becker13723d02008-10-17 19:25:01 -0700456#define path_root_access(_path)((_path)->p_root_access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800457#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
458#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
459#define path_num_items(_path) ((_path)->p_tree_depth + 1)
460
Joel Beckerfacdb772009-02-12 18:08:48 -0800461static int ocfs2_find_path(struct ocfs2_caching_info *ci,
462 struct ocfs2_path *path, u32 cpos);
Joel Beckerd401dc12009-02-13 02:24:10 -0800463static void ocfs2_adjust_rightmost_records(handle_t *handle,
464 struct ocfs2_extent_tree *et,
Tao Ma6b791bc2009-06-12 14:18:36 +0800465 struct ocfs2_path *path,
466 struct ocfs2_extent_rec *insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -0800467/*
468 * Reset the actual path elements so that we can re-use the structure
469 * to build another path. Generally, this involves freeing the buffer
470 * heads.
471 */
472static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
473{
474 int i, start = 0, depth = 0;
475 struct ocfs2_path_item *node;
476
477 if (keep_root)
478 start = 1;
479
480 for(i = start; i < path_num_items(path); i++) {
481 node = &path->p_node[i];
482
483 brelse(node->bh);
484 node->bh = NULL;
485 node->el = NULL;
486 }
487
488 /*
489 * Tree depth may change during truncate, or insert. If we're
490 * keeping the root extent list, then make sure that our path
491 * structure reflects the proper depth.
492 */
493 if (keep_root)
494 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
Joel Becker13723d02008-10-17 19:25:01 -0700495 else
496 path_root_access(path) = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -0800497
498 path->p_tree_depth = depth;
499}
500
501static void ocfs2_free_path(struct ocfs2_path *path)
502{
503 if (path) {
504 ocfs2_reinit_path(path, 0);
505 kfree(path);
506 }
507}
508
509/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700510 * All the elements of src into dest. After this call, src could be freed
511 * without affecting dest.
512 *
513 * Both paths should have the same root. Any non-root elements of dest
514 * will be freed.
515 */
516static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
517{
518 int i;
519
520 BUG_ON(path_root_bh(dest) != path_root_bh(src));
521 BUG_ON(path_root_el(dest) != path_root_el(src));
Joel Becker13723d02008-10-17 19:25:01 -0700522 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fasheh328d5752007-06-18 10:48:04 -0700523
524 ocfs2_reinit_path(dest, 1);
525
526 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
527 dest->p_node[i].bh = src->p_node[i].bh;
528 dest->p_node[i].el = src->p_node[i].el;
529
530 if (dest->p_node[i].bh)
531 get_bh(dest->p_node[i].bh);
532 }
533}
534
535/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800536 * Make the *dest path the same as src and re-initialize src path to
537 * have a root only.
538 */
539static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
540{
541 int i;
542
543 BUG_ON(path_root_bh(dest) != path_root_bh(src));
Joel Becker13723d02008-10-17 19:25:01 -0700544 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fashehdcd05382007-01-16 11:32:23 -0800545
546 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
547 brelse(dest->p_node[i].bh);
548
549 dest->p_node[i].bh = src->p_node[i].bh;
550 dest->p_node[i].el = src->p_node[i].el;
551
552 src->p_node[i].bh = NULL;
553 src->p_node[i].el = NULL;
554 }
555}
556
557/*
558 * Insert an extent block at given index.
559 *
560 * This will not take an additional reference on eb_bh.
561 */
562static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
563 struct buffer_head *eb_bh)
564{
565 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
566
567 /*
568 * Right now, no root bh is an extent block, so this helps
569 * catch code errors with dinode trees. The assertion can be
570 * safely removed if we ever need to insert extent block
571 * structures at the root.
572 */
573 BUG_ON(index == 0);
574
575 path->p_node[index].bh = eb_bh;
576 path->p_node[index].el = &eb->h_list;
577}
578
579static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700580 struct ocfs2_extent_list *root_el,
581 ocfs2_journal_access_func access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800582{
583 struct ocfs2_path *path;
584
585 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
586
587 path = kzalloc(sizeof(*path), GFP_NOFS);
588 if (path) {
589 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
590 get_bh(root_bh);
591 path_root_bh(path) = root_bh;
592 path_root_el(path) = root_el;
Joel Becker13723d02008-10-17 19:25:01 -0700593 path_root_access(path) = access;
Mark Fashehdcd05382007-01-16 11:32:23 -0800594 }
595
596 return path;
597}
598
Joel Beckerffdd7a52008-10-17 22:32:01 -0700599static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
600{
Joel Becker13723d02008-10-17 19:25:01 -0700601 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
602 path_root_access(path));
Joel Beckerffdd7a52008-10-17 22:32:01 -0700603}
604
605static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
606{
Joel Becker13723d02008-10-17 19:25:01 -0700607 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
608 et->et_root_journal_access);
609}
610
611/*
612 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
613 * otherwise it's the root_access function.
614 *
615 * I don't like the way this function's name looks next to
616 * ocfs2_journal_access_path(), but I don't have a better one.
617 */
618static int ocfs2_path_bh_journal_access(handle_t *handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800619 struct ocfs2_caching_info *ci,
Joel Becker13723d02008-10-17 19:25:01 -0700620 struct ocfs2_path *path,
621 int idx)
622{
623 ocfs2_journal_access_func access = path_root_access(path);
624
625 if (!access)
626 access = ocfs2_journal_access;
627
628 if (idx)
629 access = ocfs2_journal_access_eb;
630
Joel Becker0cf2f762009-02-12 16:41:25 -0800631 return access(handle, ci, path->p_node[idx].bh,
Joel Becker13723d02008-10-17 19:25:01 -0700632 OCFS2_JOURNAL_ACCESS_WRITE);
Joel Beckerffdd7a52008-10-17 22:32:01 -0700633}
634
Mark Fashehdcd05382007-01-16 11:32:23 -0800635/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800636 * Convenience function to journal all components in a path.
637 */
Joel Becker0cf2f762009-02-12 16:41:25 -0800638static int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
639 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -0800640 struct ocfs2_path *path)
641{
642 int i, ret = 0;
643
644 if (!path)
645 goto out;
646
647 for(i = 0; i < path_num_items(path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800648 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -0800649 if (ret < 0) {
650 mlog_errno(ret);
651 goto out;
652 }
653 }
654
655out:
656 return ret;
657}
658
Mark Fasheh328d5752007-06-18 10:48:04 -0700659/*
660 * Return the index of the extent record which contains cluster #v_cluster.
661 * -1 is returned if it was not found.
662 *
663 * Should work fine on interior and exterior nodes.
664 */
665int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
666{
667 int ret = -1;
668 int i;
669 struct ocfs2_extent_rec *rec;
670 u32 rec_end, rec_start, clusters;
671
672 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
673 rec = &el->l_recs[i];
674
675 rec_start = le32_to_cpu(rec->e_cpos);
676 clusters = ocfs2_rec_clusters(el, rec);
677
678 rec_end = rec_start + clusters;
679
680 if (v_cluster >= rec_start && v_cluster < rec_end) {
681 ret = i;
682 break;
683 }
684 }
685
686 return ret;
687}
688
Mark Fashehdcd05382007-01-16 11:32:23 -0800689enum ocfs2_contig_type {
690 CONTIG_NONE = 0,
691 CONTIG_LEFT,
Mark Fasheh328d5752007-06-18 10:48:04 -0700692 CONTIG_RIGHT,
693 CONTIG_LEFTRIGHT,
Mark Fashehdcd05382007-01-16 11:32:23 -0800694};
695
Mark Fashehe48edee2007-03-07 16:46:57 -0800696
697/*
698 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
699 * ocfs2_extent_contig only work properly against leaf nodes!
700 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800701static int ocfs2_block_extent_contig(struct super_block *sb,
702 struct ocfs2_extent_rec *ext,
703 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800704{
Mark Fashehe48edee2007-03-07 16:46:57 -0800705 u64 blk_end = le64_to_cpu(ext->e_blkno);
706
707 blk_end += ocfs2_clusters_to_blocks(sb,
708 le16_to_cpu(ext->e_leaf_clusters));
709
710 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800711}
712
Mark Fashehdcd05382007-01-16 11:32:23 -0800713static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
714 struct ocfs2_extent_rec *right)
715{
Mark Fashehe48edee2007-03-07 16:46:57 -0800716 u32 left_range;
717
718 left_range = le32_to_cpu(left->e_cpos) +
719 le16_to_cpu(left->e_leaf_clusters);
720
721 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800722}
723
724static enum ocfs2_contig_type
725 ocfs2_extent_contig(struct inode *inode,
726 struct ocfs2_extent_rec *ext,
727 struct ocfs2_extent_rec *insert_rec)
728{
729 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
730
Mark Fasheh328d5752007-06-18 10:48:04 -0700731 /*
732 * Refuse to coalesce extent records with different flag
733 * fields - we don't want to mix unwritten extents with user
734 * data.
735 */
736 if (ext->e_flags != insert_rec->e_flags)
737 return CONTIG_NONE;
738
Mark Fashehdcd05382007-01-16 11:32:23 -0800739 if (ocfs2_extents_adjacent(ext, insert_rec) &&
740 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
741 return CONTIG_RIGHT;
742
743 blkno = le64_to_cpu(ext->e_blkno);
744 if (ocfs2_extents_adjacent(insert_rec, ext) &&
745 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
746 return CONTIG_LEFT;
747
748 return CONTIG_NONE;
749}
750
751/*
752 * NOTE: We can have pretty much any combination of contiguousness and
753 * appending.
754 *
755 * The usefulness of APPEND_TAIL is more in that it lets us know that
756 * we'll have to update the path to that leaf.
757 */
758enum ocfs2_append_type {
759 APPEND_NONE = 0,
760 APPEND_TAIL,
761};
762
Mark Fasheh328d5752007-06-18 10:48:04 -0700763enum ocfs2_split_type {
764 SPLIT_NONE = 0,
765 SPLIT_LEFT,
766 SPLIT_RIGHT,
767};
768
Mark Fashehdcd05382007-01-16 11:32:23 -0800769struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700770 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800771 enum ocfs2_append_type ins_appending;
772 enum ocfs2_contig_type ins_contig;
773 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800774 int ins_tree_depth;
775};
776
Mark Fasheh328d5752007-06-18 10:48:04 -0700777struct ocfs2_merge_ctxt {
778 enum ocfs2_contig_type c_contig_type;
779 int c_has_empty_extent;
780 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700781};
782
Joel Becker5e965812008-11-13 14:49:16 -0800783static int ocfs2_validate_extent_block(struct super_block *sb,
784 struct buffer_head *bh)
785{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700786 int rc;
Joel Becker5e965812008-11-13 14:49:16 -0800787 struct ocfs2_extent_block *eb =
788 (struct ocfs2_extent_block *)bh->b_data;
789
Joel Becker970e4932008-11-13 14:49:19 -0800790 mlog(0, "Validating extent block %llu\n",
791 (unsigned long long)bh->b_blocknr);
792
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700793 BUG_ON(!buffer_uptodate(bh));
794
795 /*
796 * If the ecc fails, we return the error but otherwise
797 * leave the filesystem running. We know any error is
798 * local to this block.
799 */
800 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
Joel Becker13723d02008-10-17 19:25:01 -0700801 if (rc) {
802 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
803 (unsigned long long)bh->b_blocknr);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700804 return rc;
Joel Becker13723d02008-10-17 19:25:01 -0700805 }
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700806
807 /*
808 * Errors after here are fatal.
809 */
810
Joel Becker5e965812008-11-13 14:49:16 -0800811 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
812 ocfs2_error(sb,
813 "Extent block #%llu has bad signature %.*s",
814 (unsigned long long)bh->b_blocknr, 7,
815 eb->h_signature);
816 return -EINVAL;
817 }
818
819 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
820 ocfs2_error(sb,
821 "Extent block #%llu has an invalid h_blkno "
822 "of %llu",
823 (unsigned long long)bh->b_blocknr,
824 (unsigned long long)le64_to_cpu(eb->h_blkno));
825 return -EINVAL;
826 }
827
828 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
829 ocfs2_error(sb,
830 "Extent block #%llu has an invalid "
831 "h_fs_generation of #%u",
832 (unsigned long long)bh->b_blocknr,
833 le32_to_cpu(eb->h_fs_generation));
834 return -EINVAL;
835 }
836
837 return 0;
838}
839
Joel Becker3d03a302009-02-12 17:49:26 -0800840int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
Joel Becker5e965812008-11-13 14:49:16 -0800841 struct buffer_head **bh)
842{
843 int rc;
844 struct buffer_head *tmp = *bh;
845
Joel Becker3d03a302009-02-12 17:49:26 -0800846 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800847 ocfs2_validate_extent_block);
Joel Becker5e965812008-11-13 14:49:16 -0800848
849 /* If ocfs2_read_block() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -0800850 if (!rc && !*bh)
Joel Becker5e965812008-11-13 14:49:16 -0800851 *bh = tmp;
852
Joel Becker5e965812008-11-13 14:49:16 -0800853 return rc;
854}
855
856
Mark Fashehccd979b2005-12-15 14:31:24 -0800857/*
858 * How many free extents have we got before we need more meta data?
859 */
860int ocfs2_num_free_extents(struct ocfs2_super *osb,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700861 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -0800862{
863 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800864 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800865 struct ocfs2_extent_block *eb;
866 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800867 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800868
869 mlog_entry_void();
870
Joel Beckerf99b9b72008-08-20 19:36:33 -0700871 el = et->et_root_el;
872 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800873
Tao Mae7d4cb62008-08-18 17:38:44 +0800874 if (last_eb_blk) {
Joel Becker3d03a302009-02-12 17:49:26 -0800875 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
876 &eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800877 if (retval < 0) {
878 mlog_errno(retval);
879 goto bail;
880 }
881 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
882 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800883 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800884
885 BUG_ON(el->l_tree_depth != 0);
886
887 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
888bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700889 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800890
891 mlog_exit(retval);
892 return retval;
893}
894
895/* expects array to already be allocated
896 *
897 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
898 * l_count for you
899 */
Joel Becker42a5a7a2009-02-12 18:49:19 -0800900static int ocfs2_create_new_meta_bhs(handle_t *handle,
901 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -0800902 int wanted,
903 struct ocfs2_alloc_context *meta_ac,
904 struct buffer_head *bhs[])
905{
906 int count, status, i;
907 u16 suballoc_bit_start;
908 u32 num_got;
909 u64 first_blkno;
Joel Becker42a5a7a2009-02-12 18:49:19 -0800910 struct ocfs2_super *osb =
911 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -0800912 struct ocfs2_extent_block *eb;
913
914 mlog_entry_void();
915
916 count = 0;
917 while (count < wanted) {
918 status = ocfs2_claim_metadata(osb,
919 handle,
920 meta_ac,
921 wanted - count,
922 &suballoc_bit_start,
923 &num_got,
924 &first_blkno);
925 if (status < 0) {
926 mlog_errno(status);
927 goto bail;
928 }
929
930 for(i = count; i < (num_got + count); i++) {
931 bhs[i] = sb_getblk(osb->sb, first_blkno);
932 if (bhs[i] == NULL) {
933 status = -EIO;
934 mlog_errno(status);
935 goto bail;
936 }
Joel Becker42a5a7a2009-02-12 18:49:19 -0800937 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800938
Joel Becker42a5a7a2009-02-12 18:49:19 -0800939 status = ocfs2_journal_access_eb(handle, et->et_ci,
940 bhs[i],
Joel Becker13723d02008-10-17 19:25:01 -0700941 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800942 if (status < 0) {
943 mlog_errno(status);
944 goto bail;
945 }
946
947 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
948 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
949 /* Ok, setup the minimal stuff here. */
950 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
951 eb->h_blkno = cpu_to_le64(first_blkno);
952 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800953 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800954 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
955 eb->h_list.l_count =
956 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
957
958 suballoc_bit_start++;
959 first_blkno++;
960
961 /* We'll also be dirtied by the caller, so
962 * this isn't absolutely necessary. */
963 status = ocfs2_journal_dirty(handle, bhs[i]);
964 if (status < 0) {
965 mlog_errno(status);
966 goto bail;
967 }
968 }
969
970 count += num_got;
971 }
972
973 status = 0;
974bail:
975 if (status < 0) {
976 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -0700977 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800978 bhs[i] = NULL;
979 }
980 }
981 mlog_exit(status);
982 return status;
983}
984
985/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800986 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
987 *
988 * Returns the sum of the rightmost extent rec logical offset and
989 * cluster count.
990 *
991 * ocfs2_add_branch() uses this to determine what logical cluster
992 * value should be populated into the leftmost new branch records.
993 *
994 * ocfs2_shift_tree_depth() uses this to determine the # clusters
995 * value for the new topmost tree record.
996 */
997static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
998{
999 int i;
1000
1001 i = le16_to_cpu(el->l_next_free_rec) - 1;
1002
1003 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001004 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08001005}
1006
1007/*
Tao Ma6b791bc2009-06-12 14:18:36 +08001008 * Change range of the branches in the right most path according to the leaf
1009 * extent block's rightmost record.
1010 */
1011static int ocfs2_adjust_rightmost_branch(handle_t *handle,
Tao Ma6b791bc2009-06-12 14:18:36 +08001012 struct ocfs2_extent_tree *et)
1013{
1014 int status;
1015 struct ocfs2_path *path = NULL;
1016 struct ocfs2_extent_list *el;
1017 struct ocfs2_extent_rec *rec;
1018
1019 path = ocfs2_new_path_from_et(et);
1020 if (!path) {
1021 status = -ENOMEM;
1022 return status;
1023 }
1024
Joel Beckerfacdb772009-02-12 18:08:48 -08001025 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
Tao Ma6b791bc2009-06-12 14:18:36 +08001026 if (status < 0) {
1027 mlog_errno(status);
1028 goto out;
1029 }
1030
1031 status = ocfs2_extend_trans(handle, path_num_items(path) +
1032 handle->h_buffer_credits);
1033 if (status < 0) {
1034 mlog_errno(status);
1035 goto out;
1036 }
1037
Joel Beckerd401dc12009-02-13 02:24:10 -08001038 status = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma6b791bc2009-06-12 14:18:36 +08001039 if (status < 0) {
1040 mlog_errno(status);
1041 goto out;
1042 }
1043
1044 el = path_leaf_el(path);
1045 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1046
Joel Beckerd401dc12009-02-13 02:24:10 -08001047 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Tao Ma6b791bc2009-06-12 14:18:36 +08001048
1049out:
1050 ocfs2_free_path(path);
1051 return status;
1052}
1053
1054/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001055 * Add an entire tree branch to our inode. eb_bh is the extent block
Joel Beckerd401dc12009-02-13 02:24:10 -08001056 * to start at, if we don't want to start the branch at the root
Mark Fashehccd979b2005-12-15 14:31:24 -08001057 * structure.
1058 *
1059 * last_eb_bh is required as we have to update it's next_leaf pointer
1060 * for the new last extent block.
1061 *
1062 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -08001063 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -08001064 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001065static int ocfs2_add_branch(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001066 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001067 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07001068 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001069 struct ocfs2_alloc_context *meta_ac)
1070{
1071 int status, new_blocks, i;
1072 u64 next_blkno, new_last_eb_blk;
1073 struct buffer_head *bh;
1074 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001075 struct ocfs2_extent_block *eb;
1076 struct ocfs2_extent_list *eb_el;
1077 struct ocfs2_extent_list *el;
Tao Ma6b791bc2009-06-12 14:18:36 +08001078 u32 new_cpos, root_end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001079
1080 mlog_entry_void();
1081
Mark Fasheh328d5752007-06-18 10:48:04 -07001082 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001083
Mark Fashehccd979b2005-12-15 14:31:24 -08001084 if (eb_bh) {
1085 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1086 el = &eb->h_list;
1087 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001088 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001089
1090 /* we never add a branch to a leaf. */
1091 BUG_ON(!el->l_tree_depth);
1092
1093 new_blocks = le16_to_cpu(el->l_tree_depth);
1094
Tao Ma6b791bc2009-06-12 14:18:36 +08001095 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1096 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1097 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1098
1099 /*
1100 * If there is a gap before the root end and the real end
1101 * of the righmost leaf block, we need to remove the gap
1102 * between new_cpos and root_end first so that the tree
1103 * is consistent after we add a new branch(it will start
1104 * from new_cpos).
1105 */
1106 if (root_end > new_cpos) {
1107 mlog(0, "adjust the cluster end from %u to %u\n",
1108 root_end, new_cpos);
Joel Beckerd401dc12009-02-13 02:24:10 -08001109 status = ocfs2_adjust_rightmost_branch(handle, et);
Tao Ma6b791bc2009-06-12 14:18:36 +08001110 if (status) {
1111 mlog_errno(status);
1112 goto bail;
1113 }
1114 }
1115
Mark Fashehccd979b2005-12-15 14:31:24 -08001116 /* allocate the number of new eb blocks we need */
1117 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1118 GFP_KERNEL);
1119 if (!new_eb_bhs) {
1120 status = -ENOMEM;
1121 mlog_errno(status);
1122 goto bail;
1123 }
1124
Joel Becker42a5a7a2009-02-12 18:49:19 -08001125 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
Mark Fashehccd979b2005-12-15 14:31:24 -08001126 meta_ac, new_eb_bhs);
1127 if (status < 0) {
1128 mlog_errno(status);
1129 goto bail;
1130 }
1131
1132 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1133 * linked with the rest of the tree.
1134 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1135 *
1136 * when we leave the loop, new_last_eb_blk will point to the
1137 * newest leaf, and next_blkno will point to the topmost extent
1138 * block. */
1139 next_blkno = new_last_eb_blk = 0;
1140 for(i = 0; i < new_blocks; i++) {
1141 bh = new_eb_bhs[i];
1142 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001143 /* ocfs2_create_new_meta_bhs() should create it right! */
1144 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001145 eb_el = &eb->h_list;
1146
Joel Beckerd401dc12009-02-13 02:24:10 -08001147 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
Joel Becker13723d02008-10-17 19:25:01 -07001148 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001149 if (status < 0) {
1150 mlog_errno(status);
1151 goto bail;
1152 }
1153
1154 eb->h_next_leaf_blk = 0;
1155 eb_el->l_tree_depth = cpu_to_le16(i);
1156 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001157 /*
1158 * This actually counts as an empty extent as
1159 * c_clusters == 0
1160 */
1161 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001162 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001163 /*
1164 * eb_el isn't always an interior node, but even leaf
1165 * nodes want a zero'd flags and reserved field so
1166 * this gets the whole 32 bits regardless of use.
1167 */
1168 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001169 if (!eb_el->l_tree_depth)
1170 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1171
1172 status = ocfs2_journal_dirty(handle, bh);
1173 if (status < 0) {
1174 mlog_errno(status);
1175 goto bail;
1176 }
1177
1178 next_blkno = le64_to_cpu(eb->h_blkno);
1179 }
1180
1181 /* This is a bit hairy. We want to update up to three blocks
1182 * here without leaving any of them in an inconsistent state
1183 * in case of error. We don't have to worry about
1184 * journal_dirty erroring as it won't unless we've aborted the
1185 * handle (in which case we would never be here) so reserving
1186 * the write with journal_access is all we need to do. */
Joel Beckerd401dc12009-02-13 02:24:10 -08001187 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001188 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001189 if (status < 0) {
1190 mlog_errno(status);
1191 goto bail;
1192 }
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001193 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001194 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001195 if (status < 0) {
1196 mlog_errno(status);
1197 goto bail;
1198 }
1199 if (eb_bh) {
Joel Beckerd401dc12009-02-13 02:24:10 -08001200 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001201 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001202 if (status < 0) {
1203 mlog_errno(status);
1204 goto bail;
1205 }
1206 }
1207
1208 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001209 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001210 i = le16_to_cpu(el->l_next_free_rec);
1211 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001212 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001213 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001214 le16_add_cpu(&el->l_next_free_rec, 1);
1215
1216 /* fe needs a new last extent block pointer, as does the
1217 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001218 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001219
Mark Fasheh328d5752007-06-18 10:48:04 -07001220 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001221 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1222
Mark Fasheh328d5752007-06-18 10:48:04 -07001223 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001224 if (status < 0)
1225 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001226 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001227 if (status < 0)
1228 mlog_errno(status);
1229 if (eb_bh) {
1230 status = ocfs2_journal_dirty(handle, eb_bh);
1231 if (status < 0)
1232 mlog_errno(status);
1233 }
1234
Mark Fasheh328d5752007-06-18 10:48:04 -07001235 /*
1236 * Some callers want to track the rightmost leaf so pass it
1237 * back here.
1238 */
1239 brelse(*last_eb_bh);
1240 get_bh(new_eb_bhs[0]);
1241 *last_eb_bh = new_eb_bhs[0];
1242
Mark Fashehccd979b2005-12-15 14:31:24 -08001243 status = 0;
1244bail:
1245 if (new_eb_bhs) {
1246 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001247 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001248 kfree(new_eb_bhs);
1249 }
1250
1251 mlog_exit(status);
1252 return status;
1253}
1254
1255/*
1256 * adds another level to the allocation tree.
1257 * returns back the new extent block so you can add a branch to it
1258 * after this call.
1259 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001260static int ocfs2_shift_tree_depth(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001261 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001262 struct ocfs2_alloc_context *meta_ac,
1263 struct buffer_head **ret_new_eb_bh)
1264{
1265 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001266 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001267 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001268 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001269 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001270 struct ocfs2_extent_list *eb_el;
1271
1272 mlog_entry_void();
1273
Joel Becker42a5a7a2009-02-12 18:49:19 -08001274 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
Mark Fashehccd979b2005-12-15 14:31:24 -08001275 &new_eb_bh);
1276 if (status < 0) {
1277 mlog_errno(status);
1278 goto bail;
1279 }
1280
1281 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001282 /* ocfs2_create_new_meta_bhs() should create it right! */
1283 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001284
1285 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001286 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001287
Joel Beckerd401dc12009-02-13 02:24:10 -08001288 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001289 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001290 if (status < 0) {
1291 mlog_errno(status);
1292 goto bail;
1293 }
1294
Tao Mae7d4cb62008-08-18 17:38:44 +08001295 /* copy the root extent list data into the new extent block */
1296 eb_el->l_tree_depth = root_el->l_tree_depth;
1297 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1298 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1299 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001300
1301 status = ocfs2_journal_dirty(handle, new_eb_bh);
1302 if (status < 0) {
1303 mlog_errno(status);
1304 goto bail;
1305 }
1306
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001307 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001308 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001309 if (status < 0) {
1310 mlog_errno(status);
1311 goto bail;
1312 }
1313
Mark Fashehdcd05382007-01-16 11:32:23 -08001314 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1315
Tao Mae7d4cb62008-08-18 17:38:44 +08001316 /* update root_bh now */
1317 le16_add_cpu(&root_el->l_tree_depth, 1);
1318 root_el->l_recs[0].e_cpos = 0;
1319 root_el->l_recs[0].e_blkno = eb->h_blkno;
1320 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1321 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1322 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1323 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001324
1325 /* If this is our 1st tree depth shift, then last_eb_blk
1326 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001327 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001328 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001329
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001330 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001331 if (status < 0) {
1332 mlog_errno(status);
1333 goto bail;
1334 }
1335
1336 *ret_new_eb_bh = new_eb_bh;
1337 new_eb_bh = NULL;
1338 status = 0;
1339bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001340 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001341
1342 mlog_exit(status);
1343 return status;
1344}
1345
1346/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001347 * Should only be called when there is no space left in any of the
1348 * leaf nodes. What we want to do is find the lowest tree depth
1349 * non-leaf extent block with room for new records. There are three
1350 * valid results of this search:
1351 *
1352 * 1) a lowest extent block is found, then we pass it back in
1353 * *lowest_eb_bh and return '0'
1354 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001355 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001356 * pass NULL back in *lowest_eb_bh, but still return '0'
1357 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001358 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001359 * which case we return > 0
1360 *
1361 * return status < 0 indicates an error.
1362 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001363static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001364 struct buffer_head **target_bh)
1365{
1366 int status = 0, i;
1367 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001368 struct ocfs2_extent_block *eb;
1369 struct ocfs2_extent_list *el;
1370 struct buffer_head *bh = NULL;
1371 struct buffer_head *lowest_bh = NULL;
1372
1373 mlog_entry_void();
1374
1375 *target_bh = NULL;
1376
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001377 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001378
1379 while(le16_to_cpu(el->l_tree_depth) > 1) {
1380 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08001381 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1382 "Owner %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001383 "extent list (next_free_rec == 0)",
Joel Becker3d03a302009-02-12 17:49:26 -08001384 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001385 status = -EIO;
1386 goto bail;
1387 }
1388 i = le16_to_cpu(el->l_next_free_rec) - 1;
1389 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1390 if (!blkno) {
Joel Becker3d03a302009-02-12 17:49:26 -08001391 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1392 "Owner %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001393 "list where extent # %d has no physical "
1394 "block start",
Joel Becker3d03a302009-02-12 17:49:26 -08001395 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001396 status = -EIO;
1397 goto bail;
1398 }
1399
Mark Fasheha81cb882008-10-07 14:25:16 -07001400 brelse(bh);
1401 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001402
Joel Becker3d03a302009-02-12 17:49:26 -08001403 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001404 if (status < 0) {
1405 mlog_errno(status);
1406 goto bail;
1407 }
1408
1409 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001410 el = &eb->h_list;
1411
1412 if (le16_to_cpu(el->l_next_free_rec) <
1413 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001414 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001415 lowest_bh = bh;
1416 get_bh(lowest_bh);
1417 }
1418 }
1419
1420 /* If we didn't find one and the fe doesn't have any room,
1421 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001422 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001423 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001424 status = 1;
1425
1426 *target_bh = lowest_bh;
1427bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001428 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001429
1430 mlog_exit(status);
1431 return status;
1432}
1433
Mark Fashehe48edee2007-03-07 16:46:57 -08001434/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001435 * Grow a b-tree so that it has more records.
1436 *
1437 * We might shift the tree depth in which case existing paths should
1438 * be considered invalid.
1439 *
1440 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001441 *
1442 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001443 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001444static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1445 int *final_depth, struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001446 struct ocfs2_alloc_context *meta_ac)
1447{
1448 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001449 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001450 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001451 struct buffer_head *bh = NULL;
1452
1453 BUG_ON(meta_ac == NULL);
1454
Joel Beckerd401dc12009-02-13 02:24:10 -08001455 shift = ocfs2_find_branch_target(et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001456 if (shift < 0) {
1457 ret = shift;
1458 mlog_errno(ret);
1459 goto out;
1460 }
1461
1462 /* We traveled all the way to the bottom of the allocation tree
1463 * and didn't find room for any more extents - we need to add
1464 * another tree level */
1465 if (shift) {
1466 BUG_ON(bh);
1467 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1468
1469 /* ocfs2_shift_tree_depth will return us a buffer with
1470 * the new extent block (so we can pass that to
1471 * ocfs2_add_branch). */
Joel Beckerd401dc12009-02-13 02:24:10 -08001472 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001473 if (ret < 0) {
1474 mlog_errno(ret);
1475 goto out;
1476 }
1477 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001478 if (depth == 1) {
1479 /*
1480 * Special case: we have room now if we shifted from
1481 * tree_depth 0, so no more work needs to be done.
1482 *
1483 * We won't be calling add_branch, so pass
1484 * back *last_eb_bh as the new leaf. At depth
1485 * zero, it should always be null so there's
1486 * no reason to brelse.
1487 */
1488 BUG_ON(*last_eb_bh);
1489 get_bh(bh);
1490 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001491 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001492 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001493 }
1494
1495 /* call ocfs2_add_branch to add the final part of the tree with
1496 * the new data. */
1497 mlog(0, "add branch. bh = %p\n", bh);
Joel Beckerd401dc12009-02-13 02:24:10 -08001498 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001499 meta_ac);
1500 if (ret < 0) {
1501 mlog_errno(ret);
1502 goto out;
1503 }
1504
1505out:
1506 if (final_depth)
1507 *final_depth = depth;
1508 brelse(bh);
1509 return ret;
1510}
1511
1512/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001513 * This function will discard the rightmost extent record.
1514 */
1515static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1516{
1517 int next_free = le16_to_cpu(el->l_next_free_rec);
1518 int count = le16_to_cpu(el->l_count);
1519 unsigned int num_bytes;
1520
1521 BUG_ON(!next_free);
1522 /* This will cause us to go off the end of our extent list. */
1523 BUG_ON(next_free >= count);
1524
1525 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1526
1527 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1528}
1529
1530static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1531 struct ocfs2_extent_rec *insert_rec)
1532{
1533 int i, insert_index, next_free, has_empty, num_bytes;
1534 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1535 struct ocfs2_extent_rec *rec;
1536
1537 next_free = le16_to_cpu(el->l_next_free_rec);
1538 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1539
1540 BUG_ON(!next_free);
1541
1542 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001543 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001544
1545 /*
1546 * The easiest way to approach this is to just remove the
1547 * empty extent and temporarily decrement next_free.
1548 */
1549 if (has_empty) {
1550 /*
1551 * If next_free was 1 (only an empty extent), this
1552 * loop won't execute, which is fine. We still want
1553 * the decrement above to happen.
1554 */
1555 for(i = 0; i < (next_free - 1); i++)
1556 el->l_recs[i] = el->l_recs[i+1];
1557
1558 next_free--;
1559 }
1560
1561 /*
1562 * Figure out what the new record index should be.
1563 */
1564 for(i = 0; i < next_free; i++) {
1565 rec = &el->l_recs[i];
1566
1567 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1568 break;
1569 }
1570 insert_index = i;
1571
1572 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1573 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1574
1575 BUG_ON(insert_index < 0);
1576 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1577 BUG_ON(insert_index > next_free);
1578
1579 /*
1580 * No need to memmove if we're just adding to the tail.
1581 */
1582 if (insert_index != next_free) {
1583 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1584
1585 num_bytes = next_free - insert_index;
1586 num_bytes *= sizeof(struct ocfs2_extent_rec);
1587 memmove(&el->l_recs[insert_index + 1],
1588 &el->l_recs[insert_index],
1589 num_bytes);
1590 }
1591
1592 /*
1593 * Either we had an empty extent, and need to re-increment or
1594 * there was no empty extent on a non full rightmost leaf node,
1595 * in which case we still need to increment.
1596 */
1597 next_free++;
1598 el->l_next_free_rec = cpu_to_le16(next_free);
1599 /*
1600 * Make sure none of the math above just messed up our tree.
1601 */
1602 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1603
1604 el->l_recs[insert_index] = *insert_rec;
1605
1606}
1607
Mark Fasheh328d5752007-06-18 10:48:04 -07001608static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1609{
1610 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1611
1612 BUG_ON(num_recs == 0);
1613
1614 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1615 num_recs--;
1616 size = num_recs * sizeof(struct ocfs2_extent_rec);
1617 memmove(&el->l_recs[0], &el->l_recs[1], size);
1618 memset(&el->l_recs[num_recs], 0,
1619 sizeof(struct ocfs2_extent_rec));
1620 el->l_next_free_rec = cpu_to_le16(num_recs);
1621 }
1622}
1623
Mark Fashehdcd05382007-01-16 11:32:23 -08001624/*
1625 * Create an empty extent record .
1626 *
1627 * l_next_free_rec may be updated.
1628 *
1629 * If an empty extent already exists do nothing.
1630 */
1631static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1632{
1633 int next_free = le16_to_cpu(el->l_next_free_rec);
1634
Mark Fashehe48edee2007-03-07 16:46:57 -08001635 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1636
Mark Fashehdcd05382007-01-16 11:32:23 -08001637 if (next_free == 0)
1638 goto set_and_inc;
1639
1640 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1641 return;
1642
1643 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1644 "Asked to create an empty extent in a full list:\n"
1645 "count = %u, tree depth = %u",
1646 le16_to_cpu(el->l_count),
1647 le16_to_cpu(el->l_tree_depth));
1648
1649 ocfs2_shift_records_right(el);
1650
1651set_and_inc:
1652 le16_add_cpu(&el->l_next_free_rec, 1);
1653 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1654}
1655
1656/*
1657 * For a rotation which involves two leaf nodes, the "root node" is
1658 * the lowest level tree node which contains a path to both leafs. This
1659 * resulting set of information can be used to form a complete "subtree"
1660 *
1661 * This function is passed two full paths from the dinode down to a
1662 * pair of adjacent leaves. It's task is to figure out which path
1663 * index contains the subtree root - this can be the root index itself
1664 * in a worst-case rotation.
1665 *
1666 * The array index of the subtree root is passed back.
1667 */
Joel Becker7dc02802009-02-12 19:20:13 -08001668static int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08001669 struct ocfs2_path *left,
1670 struct ocfs2_path *right)
1671{
1672 int i = 0;
1673
1674 /*
1675 * Check that the caller passed in two paths from the same tree.
1676 */
1677 BUG_ON(path_root_bh(left) != path_root_bh(right));
1678
1679 do {
1680 i++;
1681
1682 /*
1683 * The caller didn't pass two adjacent paths.
1684 */
1685 mlog_bug_on_msg(i > left->p_tree_depth,
Joel Becker7dc02802009-02-12 19:20:13 -08001686 "Owner %llu, left depth %u, right depth %u\n"
Mark Fashehdcd05382007-01-16 11:32:23 -08001687 "left leaf blk %llu, right leaf blk %llu\n",
Joel Becker7dc02802009-02-12 19:20:13 -08001688 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1689 left->p_tree_depth, right->p_tree_depth,
Mark Fashehdcd05382007-01-16 11:32:23 -08001690 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1691 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1692 } while (left->p_node[i].bh->b_blocknr ==
1693 right->p_node[i].bh->b_blocknr);
1694
1695 return i - 1;
1696}
1697
1698typedef void (path_insert_t)(void *, struct buffer_head *);
1699
1700/*
1701 * Traverse a btree path in search of cpos, starting at root_el.
1702 *
1703 * This code can be called with a cpos larger than the tree, in which
1704 * case it will return the rightmost path.
1705 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001706static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
Mark Fashehdcd05382007-01-16 11:32:23 -08001707 struct ocfs2_extent_list *root_el, u32 cpos,
1708 path_insert_t *func, void *data)
1709{
1710 int i, ret = 0;
1711 u32 range;
1712 u64 blkno;
1713 struct buffer_head *bh = NULL;
1714 struct ocfs2_extent_block *eb;
1715 struct ocfs2_extent_list *el;
1716 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08001717
1718 el = root_el;
1719 while (el->l_tree_depth) {
1720 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001721 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1722 "Owner %llu has empty extent list at "
Mark Fashehdcd05382007-01-16 11:32:23 -08001723 "depth %u\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001724 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001725 le16_to_cpu(el->l_tree_depth));
1726 ret = -EROFS;
1727 goto out;
1728
1729 }
1730
1731 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1732 rec = &el->l_recs[i];
1733
1734 /*
1735 * In the case that cpos is off the allocation
1736 * tree, this should just wind up returning the
1737 * rightmost record.
1738 */
1739 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001740 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001741 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1742 break;
1743 }
1744
1745 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1746 if (blkno == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001747 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1748 "Owner %llu has bad blkno in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001749 "at depth %u (index %d)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001750 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001751 le16_to_cpu(el->l_tree_depth), i);
1752 ret = -EROFS;
1753 goto out;
1754 }
1755
1756 brelse(bh);
1757 bh = NULL;
Joel Beckerfacdb772009-02-12 18:08:48 -08001758 ret = ocfs2_read_extent_block(ci, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001759 if (ret) {
1760 mlog_errno(ret);
1761 goto out;
1762 }
1763
1764 eb = (struct ocfs2_extent_block *) bh->b_data;
1765 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001766
1767 if (le16_to_cpu(el->l_next_free_rec) >
1768 le16_to_cpu(el->l_count)) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001769 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1770 "Owner %llu has bad count in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001771 "at block %llu (next free=%u, count=%u)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001772 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001773 (unsigned long long)bh->b_blocknr,
1774 le16_to_cpu(el->l_next_free_rec),
1775 le16_to_cpu(el->l_count));
1776 ret = -EROFS;
1777 goto out;
1778 }
1779
1780 if (func)
1781 func(data, bh);
1782 }
1783
1784out:
1785 /*
1786 * Catch any trailing bh that the loop didn't handle.
1787 */
1788 brelse(bh);
1789
1790 return ret;
1791}
1792
1793/*
1794 * Given an initialized path (that is, it has a valid root extent
1795 * list), this function will traverse the btree in search of the path
1796 * which would contain cpos.
1797 *
1798 * The path traveled is recorded in the path structure.
1799 *
1800 * Note that this will not do any comparisons on leaf node extent
1801 * records, so it will work fine in the case that we just added a tree
1802 * branch.
1803 */
1804struct find_path_data {
1805 int index;
1806 struct ocfs2_path *path;
1807};
1808static void find_path_ins(void *data, struct buffer_head *bh)
1809{
1810 struct find_path_data *fp = data;
1811
1812 get_bh(bh);
1813 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1814 fp->index++;
1815}
Joel Beckerfacdb772009-02-12 18:08:48 -08001816static int ocfs2_find_path(struct ocfs2_caching_info *ci,
1817 struct ocfs2_path *path, u32 cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08001818{
1819 struct find_path_data data;
1820
1821 data.index = 1;
1822 data.path = path;
Joel Beckerfacdb772009-02-12 18:08:48 -08001823 return __ocfs2_find_path(ci, path_root_el(path), cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08001824 find_path_ins, &data);
1825}
1826
1827static void find_leaf_ins(void *data, struct buffer_head *bh)
1828{
1829 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1830 struct ocfs2_extent_list *el = &eb->h_list;
1831 struct buffer_head **ret = data;
1832
1833 /* We want to retain only the leaf block. */
1834 if (le16_to_cpu(el->l_tree_depth) == 0) {
1835 get_bh(bh);
1836 *ret = bh;
1837 }
1838}
1839/*
1840 * Find the leaf block in the tree which would contain cpos. No
1841 * checking of the actual leaf is done.
1842 *
1843 * Some paths want to call this instead of allocating a path structure
1844 * and calling ocfs2_find_path().
1845 *
1846 * This function doesn't handle non btree extent lists.
1847 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001848int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1849 struct ocfs2_extent_list *root_el, u32 cpos,
1850 struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001851{
1852 int ret;
1853 struct buffer_head *bh = NULL;
1854
Joel Beckerfacdb772009-02-12 18:08:48 -08001855 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001856 if (ret) {
1857 mlog_errno(ret);
1858 goto out;
1859 }
1860
1861 *leaf_bh = bh;
1862out:
1863 return ret;
1864}
1865
1866/*
1867 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1868 *
1869 * Basically, we've moved stuff around at the bottom of the tree and
1870 * we need to fix up the extent records above the changes to reflect
1871 * the new changes.
1872 *
1873 * left_rec: the record on the left.
1874 * left_child_el: is the child list pointed to by left_rec
1875 * right_rec: the record to the right of left_rec
1876 * right_child_el: is the child list pointed to by right_rec
1877 *
1878 * By definition, this only works on interior nodes.
1879 */
1880static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1881 struct ocfs2_extent_list *left_child_el,
1882 struct ocfs2_extent_rec *right_rec,
1883 struct ocfs2_extent_list *right_child_el)
1884{
1885 u32 left_clusters, right_end;
1886
1887 /*
1888 * Interior nodes never have holes. Their cpos is the cpos of
1889 * the leftmost record in their child list. Their cluster
1890 * count covers the full theoretical range of their child list
1891 * - the range between their cpos and the cpos of the record
1892 * immediately to their right.
1893 */
1894 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08001895 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1896 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07001897 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1898 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1899 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001900 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001901 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001902
1903 /*
1904 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001905 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001906 * updating e_cpos to keep the same highest cluster count.
1907 */
1908 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001909 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001910
1911 right_rec->e_cpos = left_rec->e_cpos;
1912 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1913
1914 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001915 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001916}
1917
1918/*
1919 * Adjust the adjacent root node records involved in a
1920 * rotation. left_el_blkno is passed in as a key so that we can easily
1921 * find it's index in the root list.
1922 */
1923static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1924 struct ocfs2_extent_list *left_el,
1925 struct ocfs2_extent_list *right_el,
1926 u64 left_el_blkno)
1927{
1928 int i;
1929
1930 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1931 le16_to_cpu(left_el->l_tree_depth));
1932
1933 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1934 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1935 break;
1936 }
1937
1938 /*
1939 * The path walking code should have never returned a root and
1940 * two paths which are not adjacent.
1941 */
1942 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1943
1944 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1945 &root_el->l_recs[i + 1], right_el);
1946}
1947
1948/*
1949 * We've changed a leaf block (in right_path) and need to reflect that
1950 * change back up the subtree.
1951 *
1952 * This happens in multiple places:
1953 * - When we've moved an extent record from the left path leaf to the right
1954 * path leaf to make room for an empty extent in the left path leaf.
1955 * - When our insert into the right path leaf is at the leftmost edge
1956 * and requires an update of the path immediately to it's left. This
1957 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08001958 * - When we've adjusted the last extent record in the left path leaf and the
1959 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08001960 */
Joel Becker4619c732009-02-12 19:02:36 -08001961static void ocfs2_complete_edge_insert(handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08001962 struct ocfs2_path *left_path,
1963 struct ocfs2_path *right_path,
1964 int subtree_index)
1965{
1966 int ret, i, idx;
1967 struct ocfs2_extent_list *el, *left_el, *right_el;
1968 struct ocfs2_extent_rec *left_rec, *right_rec;
1969 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1970
1971 /*
1972 * Update the counts and position values within all the
1973 * interior nodes to reflect the leaf rotation we just did.
1974 *
1975 * The root node is handled below the loop.
1976 *
1977 * We begin the loop with right_el and left_el pointing to the
1978 * leaf lists and work our way up.
1979 *
1980 * NOTE: within this loop, left_el and right_el always refer
1981 * to the *child* lists.
1982 */
1983 left_el = path_leaf_el(left_path);
1984 right_el = path_leaf_el(right_path);
1985 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1986 mlog(0, "Adjust records at index %u\n", i);
1987
1988 /*
1989 * One nice property of knowing that all of these
1990 * nodes are below the root is that we only deal with
1991 * the leftmost right node record and the rightmost
1992 * left node record.
1993 */
1994 el = left_path->p_node[i].el;
1995 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1996 left_rec = &el->l_recs[idx];
1997
1998 el = right_path->p_node[i].el;
1999 right_rec = &el->l_recs[0];
2000
2001 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2002 right_el);
2003
2004 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2005 if (ret)
2006 mlog_errno(ret);
2007
2008 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2009 if (ret)
2010 mlog_errno(ret);
2011
2012 /*
2013 * Setup our list pointers now so that the current
2014 * parents become children in the next iteration.
2015 */
2016 left_el = left_path->p_node[i].el;
2017 right_el = right_path->p_node[i].el;
2018 }
2019
2020 /*
2021 * At the root node, adjust the two adjacent records which
2022 * begin our path to the leaves.
2023 */
2024
2025 el = left_path->p_node[subtree_index].el;
2026 left_el = left_path->p_node[subtree_index + 1].el;
2027 right_el = right_path->p_node[subtree_index + 1].el;
2028
2029 ocfs2_adjust_root_records(el, left_el, right_el,
2030 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2031
2032 root_bh = left_path->p_node[subtree_index].bh;
2033
2034 ret = ocfs2_journal_dirty(handle, root_bh);
2035 if (ret)
2036 mlog_errno(ret);
2037}
2038
Joel Becker5c601ab2009-02-12 19:10:13 -08002039static int ocfs2_rotate_subtree_right(handle_t *handle,
2040 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08002041 struct ocfs2_path *left_path,
2042 struct ocfs2_path *right_path,
2043 int subtree_index)
2044{
2045 int ret, i;
2046 struct buffer_head *right_leaf_bh;
2047 struct buffer_head *left_leaf_bh = NULL;
2048 struct buffer_head *root_bh;
2049 struct ocfs2_extent_list *right_el, *left_el;
2050 struct ocfs2_extent_rec move_rec;
2051
2052 left_leaf_bh = path_leaf_bh(left_path);
2053 left_el = path_leaf_el(left_path);
2054
2055 if (left_el->l_next_free_rec != left_el->l_count) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002056 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002057 "Inode %llu has non-full interior leaf node %llu"
2058 "(next free = %u)",
Joel Becker5c601ab2009-02-12 19:10:13 -08002059 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002060 (unsigned long long)left_leaf_bh->b_blocknr,
2061 le16_to_cpu(left_el->l_next_free_rec));
2062 return -EROFS;
2063 }
2064
2065 /*
2066 * This extent block may already have an empty record, so we
2067 * return early if so.
2068 */
2069 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2070 return 0;
2071
2072 root_bh = left_path->p_node[subtree_index].bh;
2073 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2074
Joel Becker5c601ab2009-02-12 19:10:13 -08002075 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002076 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002077 if (ret) {
2078 mlog_errno(ret);
2079 goto out;
2080 }
2081
2082 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002083 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002084 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002085 if (ret) {
2086 mlog_errno(ret);
2087 goto out;
2088 }
2089
Joel Becker5c601ab2009-02-12 19:10:13 -08002090 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002091 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002092 if (ret) {
2093 mlog_errno(ret);
2094 goto out;
2095 }
2096 }
2097
2098 right_leaf_bh = path_leaf_bh(right_path);
2099 right_el = path_leaf_el(right_path);
2100
2101 /* This is a code error, not a disk corruption. */
2102 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2103 "because rightmost leaf block %llu is empty\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002104 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002105 (unsigned long long)right_leaf_bh->b_blocknr);
2106
2107 ocfs2_create_empty_extent(right_el);
2108
2109 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2110 if (ret) {
2111 mlog_errno(ret);
2112 goto out;
2113 }
2114
2115 /* Do the copy now. */
2116 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2117 move_rec = left_el->l_recs[i];
2118 right_el->l_recs[0] = move_rec;
2119
2120 /*
2121 * Clear out the record we just copied and shift everything
2122 * over, leaving an empty extent in the left leaf.
2123 *
2124 * We temporarily subtract from next_free_rec so that the
2125 * shift will lose the tail record (which is now defunct).
2126 */
2127 le16_add_cpu(&left_el->l_next_free_rec, -1);
2128 ocfs2_shift_records_right(left_el);
2129 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2130 le16_add_cpu(&left_el->l_next_free_rec, 1);
2131
2132 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2133 if (ret) {
2134 mlog_errno(ret);
2135 goto out;
2136 }
2137
Joel Becker4619c732009-02-12 19:02:36 -08002138 ocfs2_complete_edge_insert(handle, left_path, right_path,
2139 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002140
2141out:
2142 return ret;
2143}
2144
2145/*
2146 * Given a full path, determine what cpos value would return us a path
2147 * containing the leaf immediately to the left of the current one.
2148 *
2149 * Will return zero if the path passed in is already the leftmost path.
2150 */
2151static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2152 struct ocfs2_path *path, u32 *cpos)
2153{
2154 int i, j, ret = 0;
2155 u64 blkno;
2156 struct ocfs2_extent_list *el;
2157
Mark Fashehe48edee2007-03-07 16:46:57 -08002158 BUG_ON(path->p_tree_depth == 0);
2159
Mark Fashehdcd05382007-01-16 11:32:23 -08002160 *cpos = 0;
2161
2162 blkno = path_leaf_bh(path)->b_blocknr;
2163
2164 /* Start at the tree node just above the leaf and work our way up. */
2165 i = path->p_tree_depth - 1;
2166 while (i >= 0) {
2167 el = path->p_node[i].el;
2168
2169 /*
2170 * Find the extent record just before the one in our
2171 * path.
2172 */
2173 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2174 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2175 if (j == 0) {
2176 if (i == 0) {
2177 /*
2178 * We've determined that the
2179 * path specified is already
2180 * the leftmost one - return a
2181 * cpos of zero.
2182 */
2183 goto out;
2184 }
2185 /*
2186 * The leftmost record points to our
2187 * leaf - we need to travel up the
2188 * tree one level.
2189 */
2190 goto next_node;
2191 }
2192
2193 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002194 *cpos = *cpos + ocfs2_rec_clusters(el,
2195 &el->l_recs[j - 1]);
2196 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002197 goto out;
2198 }
2199 }
2200
2201 /*
2202 * If we got here, we never found a valid node where
2203 * the tree indicated one should be.
2204 */
2205 ocfs2_error(sb,
2206 "Invalid extent tree at extent block %llu\n",
2207 (unsigned long long)blkno);
2208 ret = -EROFS;
2209 goto out;
2210
2211next_node:
2212 blkno = path->p_node[i].bh->b_blocknr;
2213 i--;
2214 }
2215
2216out:
2217 return ret;
2218}
2219
Mark Fasheh328d5752007-06-18 10:48:04 -07002220/*
2221 * Extend the transaction by enough credits to complete the rotation,
2222 * and still leave at least the original number of credits allocated
2223 * to this transaction.
2224 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002225static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002226 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002227 struct ocfs2_path *path)
2228{
Mark Fasheh328d5752007-06-18 10:48:04 -07002229 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002230
2231 if (handle->h_buffer_credits < credits)
2232 return ocfs2_extend_trans(handle, credits);
2233
2234 return 0;
2235}
2236
2237/*
2238 * Trap the case where we're inserting into the theoretical range past
2239 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2240 * whose cpos is less than ours into the right leaf.
2241 *
2242 * It's only necessary to look at the rightmost record of the left
2243 * leaf because the logic that calls us should ensure that the
2244 * theoretical ranges in the path components above the leaves are
2245 * correct.
2246 */
2247static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2248 u32 insert_cpos)
2249{
2250 struct ocfs2_extent_list *left_el;
2251 struct ocfs2_extent_rec *rec;
2252 int next_free;
2253
2254 left_el = path_leaf_el(left_path);
2255 next_free = le16_to_cpu(left_el->l_next_free_rec);
2256 rec = &left_el->l_recs[next_free - 1];
2257
2258 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2259 return 1;
2260 return 0;
2261}
2262
Mark Fasheh328d5752007-06-18 10:48:04 -07002263static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2264{
2265 int next_free = le16_to_cpu(el->l_next_free_rec);
2266 unsigned int range;
2267 struct ocfs2_extent_rec *rec;
2268
2269 if (next_free == 0)
2270 return 0;
2271
2272 rec = &el->l_recs[0];
2273 if (ocfs2_is_empty_extent(rec)) {
2274 /* Empty list. */
2275 if (next_free == 1)
2276 return 0;
2277 rec = &el->l_recs[1];
2278 }
2279
2280 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2281 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2282 return 1;
2283 return 0;
2284}
2285
Mark Fashehdcd05382007-01-16 11:32:23 -08002286/*
2287 * Rotate all the records in a btree right one record, starting at insert_cpos.
2288 *
2289 * The path to the rightmost leaf should be passed in.
2290 *
2291 * The array is assumed to be large enough to hold an entire path (tree depth).
2292 *
2293 * Upon succesful return from this function:
2294 *
2295 * - The 'right_path' array will contain a path to the leaf block
2296 * whose range contains e_cpos.
2297 * - That leaf block will have a single empty extent in list index 0.
2298 * - In the case that the rotation requires a post-insert update,
2299 * *ret_left_path will contain a valid path which can be passed to
2300 * ocfs2_insert_path().
2301 */
Joel Becker1bbf0b82009-02-12 19:42:08 -08002302static int ocfs2_rotate_tree_right(handle_t *handle,
Joel Becker5c601ab2009-02-12 19:10:13 -08002303 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002304 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002305 u32 insert_cpos,
2306 struct ocfs2_path *right_path,
2307 struct ocfs2_path **ret_left_path)
2308{
Mark Fasheh328d5752007-06-18 10:48:04 -07002309 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002310 u32 cpos;
2311 struct ocfs2_path *left_path = NULL;
Joel Becker5c601ab2009-02-12 19:10:13 -08002312 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehdcd05382007-01-16 11:32:23 -08002313
2314 *ret_left_path = NULL;
2315
Joel Beckerffdd7a52008-10-17 22:32:01 -07002316 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002317 if (!left_path) {
2318 ret = -ENOMEM;
2319 mlog_errno(ret);
2320 goto out;
2321 }
2322
Joel Becker5c601ab2009-02-12 19:10:13 -08002323 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002324 if (ret) {
2325 mlog_errno(ret);
2326 goto out;
2327 }
2328
2329 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2330
2331 /*
2332 * What we want to do here is:
2333 *
2334 * 1) Start with the rightmost path.
2335 *
2336 * 2) Determine a path to the leaf block directly to the left
2337 * of that leaf.
2338 *
2339 * 3) Determine the 'subtree root' - the lowest level tree node
2340 * which contains a path to both leaves.
2341 *
2342 * 4) Rotate the subtree.
2343 *
2344 * 5) Find the next subtree by considering the left path to be
2345 * the new right path.
2346 *
2347 * The check at the top of this while loop also accepts
2348 * insert_cpos == cpos because cpos is only a _theoretical_
2349 * value to get us the left path - insert_cpos might very well
2350 * be filling that hole.
2351 *
2352 * Stop at a cpos of '0' because we either started at the
2353 * leftmost branch (i.e., a tree with one branch and a
2354 * rotation inside of it), or we've gone as far as we can in
2355 * rotating subtrees.
2356 */
2357 while (cpos && insert_cpos <= cpos) {
2358 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2359 insert_cpos, cpos);
2360
Joel Becker5c601ab2009-02-12 19:10:13 -08002361 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002362 if (ret) {
2363 mlog_errno(ret);
2364 goto out;
2365 }
2366
2367 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2368 path_leaf_bh(right_path),
Joel Becker5c601ab2009-02-12 19:10:13 -08002369 "Owner %llu: error during insert of %u "
Mark Fashehdcd05382007-01-16 11:32:23 -08002370 "(left path cpos %u) results in two identical "
2371 "paths ending at %llu\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002372 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2373 insert_cpos, cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08002374 (unsigned long long)
2375 path_leaf_bh(left_path)->b_blocknr);
2376
Mark Fasheh328d5752007-06-18 10:48:04 -07002377 if (split == SPLIT_NONE &&
2378 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002379 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002380
2381 /*
2382 * We've rotated the tree as much as we
2383 * should. The rest is up to
2384 * ocfs2_insert_path() to complete, after the
2385 * record insertion. We indicate this
2386 * situation by returning the left path.
2387 *
2388 * The reason we don't adjust the records here
2389 * before the record insert is that an error
2390 * later might break the rule where a parent
2391 * record e_cpos will reflect the actual
2392 * e_cpos of the 1st nonempty record of the
2393 * child list.
2394 */
2395 *ret_left_path = left_path;
2396 goto out_ret_path;
2397 }
2398
Joel Becker7dc02802009-02-12 19:20:13 -08002399 start = ocfs2_find_subtree_root(et, left_path, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002400
2401 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2402 start,
2403 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2404 right_path->p_tree_depth);
2405
2406 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002407 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002408 if (ret) {
2409 mlog_errno(ret);
2410 goto out;
2411 }
2412
Joel Becker5c601ab2009-02-12 19:10:13 -08002413 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002414 right_path, start);
2415 if (ret) {
2416 mlog_errno(ret);
2417 goto out;
2418 }
2419
Mark Fasheh328d5752007-06-18 10:48:04 -07002420 if (split != SPLIT_NONE &&
2421 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2422 insert_cpos)) {
2423 /*
2424 * A rotate moves the rightmost left leaf
2425 * record over to the leftmost right leaf
2426 * slot. If we're doing an extent split
2427 * instead of a real insert, then we have to
2428 * check that the extent to be split wasn't
2429 * just moved over. If it was, then we can
2430 * exit here, passing left_path back -
2431 * ocfs2_split_extent() is smart enough to
2432 * search both leaves.
2433 */
2434 *ret_left_path = left_path;
2435 goto out_ret_path;
2436 }
2437
Mark Fashehdcd05382007-01-16 11:32:23 -08002438 /*
2439 * There is no need to re-read the next right path
2440 * as we know that it'll be our current left
2441 * path. Optimize by copying values instead.
2442 */
2443 ocfs2_mv_path(right_path, left_path);
2444
Joel Becker5c601ab2009-02-12 19:10:13 -08002445 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002446 if (ret) {
2447 mlog_errno(ret);
2448 goto out;
2449 }
2450 }
2451
2452out:
2453 ocfs2_free_path(left_path);
2454
2455out_ret_path:
2456 return ret;
2457}
2458
Joel Becker09106ba2009-02-12 19:43:57 -08002459static int ocfs2_update_edge_lengths(handle_t *handle,
2460 struct ocfs2_extent_tree *et,
Tao Ma3c5e1062009-07-21 15:42:05 +08002461 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002462{
Tao Ma3c5e1062009-07-21 15:42:05 +08002463 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002464 struct ocfs2_extent_rec *rec;
2465 struct ocfs2_extent_list *el;
2466 struct ocfs2_extent_block *eb;
2467 u32 range;
2468
Tao Ma3c5e1062009-07-21 15:42:05 +08002469 /*
2470 * In normal tree rotation process, we will never touch the
2471 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2472 * doesn't reserve the credits for them either.
2473 *
2474 * But we do have a special case here which will update the rightmost
2475 * records for all the bh in the path.
2476 * So we have to allocate extra credits and access them.
2477 */
2478 ret = ocfs2_extend_trans(handle,
2479 handle->h_buffer_credits + subtree_index);
2480 if (ret) {
2481 mlog_errno(ret);
2482 goto out;
2483 }
2484
Joel Becker09106ba2009-02-12 19:43:57 -08002485 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002486 if (ret) {
2487 mlog_errno(ret);
2488 goto out;
2489 }
2490
Mark Fasheh328d5752007-06-18 10:48:04 -07002491 /* Path should always be rightmost. */
2492 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2493 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2494
2495 el = &eb->h_list;
2496 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2497 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2498 rec = &el->l_recs[idx];
2499 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2500
2501 for (i = 0; i < path->p_tree_depth; i++) {
2502 el = path->p_node[i].el;
2503 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2504 rec = &el->l_recs[idx];
2505
2506 rec->e_int_clusters = cpu_to_le32(range);
2507 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2508
2509 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2510 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002511out:
2512 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002513}
2514
Joel Becker6641b0c2009-02-12 18:57:52 -08002515static void ocfs2_unlink_path(handle_t *handle,
2516 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002517 struct ocfs2_cached_dealloc_ctxt *dealloc,
2518 struct ocfs2_path *path, int unlink_start)
2519{
2520 int ret, i;
2521 struct ocfs2_extent_block *eb;
2522 struct ocfs2_extent_list *el;
2523 struct buffer_head *bh;
2524
2525 for(i = unlink_start; i < path_num_items(path); i++) {
2526 bh = path->p_node[i].bh;
2527
2528 eb = (struct ocfs2_extent_block *)bh->b_data;
2529 /*
2530 * Not all nodes might have had their final count
2531 * decremented by the caller - handle this here.
2532 */
2533 el = &eb->h_list;
2534 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2535 mlog(ML_ERROR,
2536 "Inode %llu, attempted to remove extent block "
2537 "%llu with %u records\n",
Joel Becker6641b0c2009-02-12 18:57:52 -08002538 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07002539 (unsigned long long)le64_to_cpu(eb->h_blkno),
2540 le16_to_cpu(el->l_next_free_rec));
2541
2542 ocfs2_journal_dirty(handle, bh);
Joel Becker6641b0c2009-02-12 18:57:52 -08002543 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002544 continue;
2545 }
2546
2547 el->l_next_free_rec = 0;
2548 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2549
2550 ocfs2_journal_dirty(handle, bh);
2551
2552 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2553 if (ret)
2554 mlog_errno(ret);
2555
Joel Becker6641b0c2009-02-12 18:57:52 -08002556 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002557 }
2558}
2559
Joel Becker6641b0c2009-02-12 18:57:52 -08002560static void ocfs2_unlink_subtree(handle_t *handle,
2561 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002562 struct ocfs2_path *left_path,
2563 struct ocfs2_path *right_path,
2564 int subtree_index,
2565 struct ocfs2_cached_dealloc_ctxt *dealloc)
2566{
2567 int i;
2568 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2569 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2570 struct ocfs2_extent_list *el;
2571 struct ocfs2_extent_block *eb;
2572
2573 el = path_leaf_el(left_path);
2574
2575 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2576
2577 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2578 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2579 break;
2580
2581 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2582
2583 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2584 le16_add_cpu(&root_el->l_next_free_rec, -1);
2585
2586 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2587 eb->h_next_leaf_blk = 0;
2588
2589 ocfs2_journal_dirty(handle, root_bh);
2590 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2591
Joel Becker6641b0c2009-02-12 18:57:52 -08002592 ocfs2_unlink_path(handle, et, dealloc, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002593 subtree_index + 1);
2594}
2595
Joel Becker1e2dd632009-02-12 19:45:28 -08002596static int ocfs2_rotate_subtree_left(handle_t *handle,
2597 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002598 struct ocfs2_path *left_path,
2599 struct ocfs2_path *right_path,
2600 int subtree_index,
2601 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker1e2dd632009-02-12 19:45:28 -08002602 int *deleted)
Mark Fasheh328d5752007-06-18 10:48:04 -07002603{
2604 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002605 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002606 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2607 struct ocfs2_extent_block *eb;
2608
2609 *deleted = 0;
2610
2611 right_leaf_el = path_leaf_el(right_path);
2612 left_leaf_el = path_leaf_el(left_path);
2613 root_bh = left_path->p_node[subtree_index].bh;
2614 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2615
2616 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2617 return 0;
2618
2619 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2620 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2621 /*
2622 * It's legal for us to proceed if the right leaf is
2623 * the rightmost one and it has an empty extent. There
2624 * are two cases to handle - whether the leaf will be
2625 * empty after removal or not. If the leaf isn't empty
2626 * then just remove the empty extent up front. The
2627 * next block will handle empty leaves by flagging
2628 * them for unlink.
2629 *
2630 * Non rightmost leaves will throw -EAGAIN and the
2631 * caller can manually move the subtree and retry.
2632 */
2633
2634 if (eb->h_next_leaf_blk != 0ULL)
2635 return -EAGAIN;
2636
2637 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002638 ret = ocfs2_journal_access_eb(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002639 path_leaf_bh(right_path),
2640 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002641 if (ret) {
2642 mlog_errno(ret);
2643 goto out;
2644 }
2645
2646 ocfs2_remove_empty_extent(right_leaf_el);
2647 } else
2648 right_has_empty = 1;
2649 }
2650
2651 if (eb->h_next_leaf_blk == 0ULL &&
2652 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2653 /*
2654 * We have to update i_last_eb_blk during the meta
2655 * data delete.
2656 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08002657 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07002658 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002659 if (ret) {
2660 mlog_errno(ret);
2661 goto out;
2662 }
2663
2664 del_right_subtree = 1;
2665 }
2666
2667 /*
2668 * Getting here with an empty extent in the right path implies
2669 * that it's the rightmost path and will be deleted.
2670 */
2671 BUG_ON(right_has_empty && !del_right_subtree);
2672
Joel Becker1e2dd632009-02-12 19:45:28 -08002673 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002674 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002675 if (ret) {
2676 mlog_errno(ret);
2677 goto out;
2678 }
2679
2680 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002681 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002682 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002683 if (ret) {
2684 mlog_errno(ret);
2685 goto out;
2686 }
2687
Joel Becker1e2dd632009-02-12 19:45:28 -08002688 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002689 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002690 if (ret) {
2691 mlog_errno(ret);
2692 goto out;
2693 }
2694 }
2695
2696 if (!right_has_empty) {
2697 /*
2698 * Only do this if we're moving a real
2699 * record. Otherwise, the action is delayed until
2700 * after removal of the right path in which case we
2701 * can do a simple shift to remove the empty extent.
2702 */
2703 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2704 memset(&right_leaf_el->l_recs[0], 0,
2705 sizeof(struct ocfs2_extent_rec));
2706 }
2707 if (eb->h_next_leaf_blk == 0ULL) {
2708 /*
2709 * Move recs over to get rid of empty extent, decrease
2710 * next_free. This is allowed to remove the last
2711 * extent in our leaf (setting l_next_free_rec to
2712 * zero) - the delete code below won't care.
2713 */
2714 ocfs2_remove_empty_extent(right_leaf_el);
2715 }
2716
2717 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2718 if (ret)
2719 mlog_errno(ret);
2720 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2721 if (ret)
2722 mlog_errno(ret);
2723
2724 if (del_right_subtree) {
Joel Becker6641b0c2009-02-12 18:57:52 -08002725 ocfs2_unlink_subtree(handle, et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002726 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08002727 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08002728 left_path);
2729 if (ret) {
2730 mlog_errno(ret);
2731 goto out;
2732 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002733
2734 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002735 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002736
2737 /*
2738 * Removal of the extent in the left leaf was skipped
2739 * above so we could delete the right path
2740 * 1st.
2741 */
2742 if (right_has_empty)
2743 ocfs2_remove_empty_extent(left_leaf_el);
2744
Tao Mae7d4cb62008-08-18 17:38:44 +08002745 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002746 if (ret)
2747 mlog_errno(ret);
2748
2749 *deleted = 1;
2750 } else
Joel Becker4619c732009-02-12 19:02:36 -08002751 ocfs2_complete_edge_insert(handle, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002752 subtree_index);
2753
2754out:
2755 return ret;
2756}
2757
2758/*
2759 * Given a full path, determine what cpos value would return us a path
2760 * containing the leaf immediately to the right of the current one.
2761 *
2762 * Will return zero if the path passed in is already the rightmost path.
2763 *
2764 * This looks similar, but is subtly different to
2765 * ocfs2_find_cpos_for_left_leaf().
2766 */
2767static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2768 struct ocfs2_path *path, u32 *cpos)
2769{
2770 int i, j, ret = 0;
2771 u64 blkno;
2772 struct ocfs2_extent_list *el;
2773
2774 *cpos = 0;
2775
2776 if (path->p_tree_depth == 0)
2777 return 0;
2778
2779 blkno = path_leaf_bh(path)->b_blocknr;
2780
2781 /* Start at the tree node just above the leaf and work our way up. */
2782 i = path->p_tree_depth - 1;
2783 while (i >= 0) {
2784 int next_free;
2785
2786 el = path->p_node[i].el;
2787
2788 /*
2789 * Find the extent record just after the one in our
2790 * path.
2791 */
2792 next_free = le16_to_cpu(el->l_next_free_rec);
2793 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2794 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2795 if (j == (next_free - 1)) {
2796 if (i == 0) {
2797 /*
2798 * We've determined that the
2799 * path specified is already
2800 * the rightmost one - return a
2801 * cpos of zero.
2802 */
2803 goto out;
2804 }
2805 /*
2806 * The rightmost record points to our
2807 * leaf - we need to travel up the
2808 * tree one level.
2809 */
2810 goto next_node;
2811 }
2812
2813 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2814 goto out;
2815 }
2816 }
2817
2818 /*
2819 * If we got here, we never found a valid node where
2820 * the tree indicated one should be.
2821 */
2822 ocfs2_error(sb,
2823 "Invalid extent tree at extent block %llu\n",
2824 (unsigned long long)blkno);
2825 ret = -EROFS;
2826 goto out;
2827
2828next_node:
2829 blkno = path->p_node[i].bh->b_blocknr;
2830 i--;
2831 }
2832
2833out:
2834 return ret;
2835}
2836
Joel Becker70f18c02009-02-13 02:09:31 -08002837static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2838 struct ocfs2_extent_tree *et,
Joel Becker13723d02008-10-17 19:25:01 -07002839 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002840{
2841 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002842 struct buffer_head *bh = path_leaf_bh(path);
2843 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002844
2845 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2846 return 0;
2847
Joel Becker70f18c02009-02-13 02:09:31 -08002848 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Joel Becker13723d02008-10-17 19:25:01 -07002849 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002850 if (ret) {
2851 mlog_errno(ret);
2852 goto out;
2853 }
2854
2855 ocfs2_remove_empty_extent(el);
2856
2857 ret = ocfs2_journal_dirty(handle, bh);
2858 if (ret)
2859 mlog_errno(ret);
2860
2861out:
2862 return ret;
2863}
2864
Joel Beckere46f74d2009-02-12 19:47:43 -08002865static int __ocfs2_rotate_tree_left(handle_t *handle,
2866 struct ocfs2_extent_tree *et,
2867 int orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07002868 struct ocfs2_path *path,
2869 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08002870 struct ocfs2_path **empty_extent_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002871{
2872 int ret, subtree_root, deleted;
2873 u32 right_cpos;
2874 struct ocfs2_path *left_path = NULL;
2875 struct ocfs2_path *right_path = NULL;
Joel Beckere46f74d2009-02-12 19:47:43 -08002876 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fasheh328d5752007-06-18 10:48:04 -07002877
2878 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2879
2880 *empty_extent_path = NULL;
2881
Joel Beckere46f74d2009-02-12 19:47:43 -08002882 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002883 if (ret) {
2884 mlog_errno(ret);
2885 goto out;
2886 }
2887
Joel Beckerffdd7a52008-10-17 22:32:01 -07002888 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002889 if (!left_path) {
2890 ret = -ENOMEM;
2891 mlog_errno(ret);
2892 goto out;
2893 }
2894
2895 ocfs2_cp_path(left_path, path);
2896
Joel Beckerffdd7a52008-10-17 22:32:01 -07002897 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002898 if (!right_path) {
2899 ret = -ENOMEM;
2900 mlog_errno(ret);
2901 goto out;
2902 }
2903
2904 while (right_cpos) {
Joel Beckerfacdb772009-02-12 18:08:48 -08002905 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002906 if (ret) {
2907 mlog_errno(ret);
2908 goto out;
2909 }
2910
Joel Becker7dc02802009-02-12 19:20:13 -08002911 subtree_root = ocfs2_find_subtree_root(et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002912 right_path);
2913
2914 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2915 subtree_root,
2916 (unsigned long long)
2917 right_path->p_node[subtree_root].bh->b_blocknr,
2918 right_path->p_tree_depth);
2919
2920 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2921 orig_credits, left_path);
2922 if (ret) {
2923 mlog_errno(ret);
2924 goto out;
2925 }
2926
Mark Fashehe8aed342007-12-03 16:43:01 -08002927 /*
2928 * Caller might still want to make changes to the
2929 * tree root, so re-add it to the journal here.
2930 */
Joel Beckere46f74d2009-02-12 19:47:43 -08002931 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002932 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08002933 if (ret) {
2934 mlog_errno(ret);
2935 goto out;
2936 }
2937
Joel Becker1e2dd632009-02-12 19:45:28 -08002938 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002939 right_path, subtree_root,
Joel Becker1e2dd632009-02-12 19:45:28 -08002940 dealloc, &deleted);
Mark Fasheh328d5752007-06-18 10:48:04 -07002941 if (ret == -EAGAIN) {
2942 /*
2943 * The rotation has to temporarily stop due to
2944 * the right subtree having an empty
2945 * extent. Pass it back to the caller for a
2946 * fixup.
2947 */
2948 *empty_extent_path = right_path;
2949 right_path = NULL;
2950 goto out;
2951 }
2952 if (ret) {
2953 mlog_errno(ret);
2954 goto out;
2955 }
2956
2957 /*
2958 * The subtree rotate might have removed records on
2959 * the rightmost edge. If so, then rotation is
2960 * complete.
2961 */
2962 if (deleted)
2963 break;
2964
2965 ocfs2_mv_path(left_path, right_path);
2966
Joel Beckere46f74d2009-02-12 19:47:43 -08002967 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002968 &right_cpos);
2969 if (ret) {
2970 mlog_errno(ret);
2971 goto out;
2972 }
2973 }
2974
2975out:
2976 ocfs2_free_path(right_path);
2977 ocfs2_free_path(left_path);
2978
2979 return ret;
2980}
2981
Joel Becker70f18c02009-02-13 02:09:31 -08002982static int ocfs2_remove_rightmost_path(handle_t *handle,
2983 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002984 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08002985 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07002986{
2987 int ret, subtree_index;
2988 u32 cpos;
2989 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07002990 struct ocfs2_extent_block *eb;
2991 struct ocfs2_extent_list *el;
2992
Mark Fasheh328d5752007-06-18 10:48:04 -07002993
Joel Becker6136ca52009-02-12 19:32:43 -08002994 ret = ocfs2_et_sanity_check(et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002995 if (ret)
2996 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07002997 /*
2998 * There's two ways we handle this depending on
2999 * whether path is the only existing one.
3000 */
3001 ret = ocfs2_extend_rotate_transaction(handle, 0,
3002 handle->h_buffer_credits,
3003 path);
3004 if (ret) {
3005 mlog_errno(ret);
3006 goto out;
3007 }
3008
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003009 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003010 if (ret) {
3011 mlog_errno(ret);
3012 goto out;
3013 }
3014
Joel Becker3d03a302009-02-12 17:49:26 -08003015 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3016 path, &cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003017 if (ret) {
3018 mlog_errno(ret);
3019 goto out;
3020 }
3021
3022 if (cpos) {
3023 /*
3024 * We have a path to the left of this one - it needs
3025 * an update too.
3026 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003027 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003028 if (!left_path) {
3029 ret = -ENOMEM;
3030 mlog_errno(ret);
3031 goto out;
3032 }
3033
Joel Beckerfacdb772009-02-12 18:08:48 -08003034 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003035 if (ret) {
3036 mlog_errno(ret);
3037 goto out;
3038 }
3039
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003040 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003041 if (ret) {
3042 mlog_errno(ret);
3043 goto out;
3044 }
3045
Joel Becker7dc02802009-02-12 19:20:13 -08003046 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003047
Joel Becker6641b0c2009-02-12 18:57:52 -08003048 ocfs2_unlink_subtree(handle, et, left_path, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003049 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08003050 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08003051 left_path);
3052 if (ret) {
3053 mlog_errno(ret);
3054 goto out;
3055 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003056
3057 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003058 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003059 } else {
3060 /*
3061 * 'path' is also the leftmost path which
3062 * means it must be the only one. This gets
3063 * handled differently because we want to
Joel Becker70f18c02009-02-13 02:09:31 -08003064 * revert the root back to having extents
Mark Fasheh328d5752007-06-18 10:48:04 -07003065 * in-line.
3066 */
Joel Becker6641b0c2009-02-12 18:57:52 -08003067 ocfs2_unlink_path(handle, et, dealloc, path, 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003068
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003069 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003070 el->l_tree_depth = 0;
3071 el->l_next_free_rec = 0;
3072 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3073
Joel Becker35dc0aa2008-08-20 16:25:06 -07003074 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003075 }
3076
3077 ocfs2_journal_dirty(handle, path_root_bh(path));
3078
3079out:
3080 ocfs2_free_path(left_path);
3081 return ret;
3082}
3083
3084/*
3085 * Left rotation of btree records.
3086 *
3087 * In many ways, this is (unsurprisingly) the opposite of right
3088 * rotation. We start at some non-rightmost path containing an empty
3089 * extent in the leaf block. The code works its way to the rightmost
3090 * path by rotating records to the left in every subtree.
3091 *
3092 * This is used by any code which reduces the number of extent records
3093 * in a leaf. After removal, an empty record should be placed in the
3094 * leftmost list position.
3095 *
3096 * This won't handle a length update of the rightmost path records if
3097 * the rightmost tree leaf record is removed so the caller is
3098 * responsible for detecting and correcting that.
3099 */
Joel Becker70f18c02009-02-13 02:09:31 -08003100static int ocfs2_rotate_tree_left(handle_t *handle,
3101 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003102 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003103 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003104{
3105 int ret, orig_credits = handle->h_buffer_credits;
3106 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3107 struct ocfs2_extent_block *eb;
3108 struct ocfs2_extent_list *el;
3109
3110 el = path_leaf_el(path);
3111 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3112 return 0;
3113
3114 if (path->p_tree_depth == 0) {
3115rightmost_no_delete:
3116 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003117 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003118 * it up front.
3119 */
Joel Becker70f18c02009-02-13 02:09:31 -08003120 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003121 if (ret)
3122 mlog_errno(ret);
3123 goto out;
3124 }
3125
3126 /*
3127 * Handle rightmost branch now. There's several cases:
3128 * 1) simple rotation leaving records in there. That's trivial.
3129 * 2) rotation requiring a branch delete - there's no more
3130 * records left. Two cases of this:
3131 * a) There are branches to the left.
3132 * b) This is also the leftmost (the only) branch.
3133 *
3134 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3135 * 2a) we need the left branch so that we can update it with the unlink
Joel Becker70f18c02009-02-13 02:09:31 -08003136 * 2b) we need to bring the root back to inline extents.
Mark Fasheh328d5752007-06-18 10:48:04 -07003137 */
3138
3139 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3140 el = &eb->h_list;
3141 if (eb->h_next_leaf_blk == 0) {
3142 /*
3143 * This gets a bit tricky if we're going to delete the
3144 * rightmost path. Get the other cases out of the way
3145 * 1st.
3146 */
3147 if (le16_to_cpu(el->l_next_free_rec) > 1)
3148 goto rightmost_no_delete;
3149
3150 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3151 ret = -EIO;
Joel Becker70f18c02009-02-13 02:09:31 -08003152 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3153 "Owner %llu has empty extent block at %llu",
3154 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07003155 (unsigned long long)le64_to_cpu(eb->h_blkno));
3156 goto out;
3157 }
3158
3159 /*
3160 * XXX: The caller can not trust "path" any more after
3161 * this as it will have been deleted. What do we do?
3162 *
3163 * In theory the rotate-for-merge code will never get
3164 * here because it'll always ask for a rotate in a
3165 * nonempty list.
3166 */
3167
Joel Becker70f18c02009-02-13 02:09:31 -08003168 ret = ocfs2_remove_rightmost_path(handle, et, path,
3169 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003170 if (ret)
3171 mlog_errno(ret);
3172 goto out;
3173 }
3174
3175 /*
3176 * Now we can loop, remembering the path we get from -EAGAIN
3177 * and restarting from there.
3178 */
3179try_rotate:
Joel Beckere46f74d2009-02-12 19:47:43 -08003180 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3181 dealloc, &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003182 if (ret && ret != -EAGAIN) {
3183 mlog_errno(ret);
3184 goto out;
3185 }
3186
3187 while (ret == -EAGAIN) {
3188 tmp_path = restart_path;
3189 restart_path = NULL;
3190
Joel Beckere46f74d2009-02-12 19:47:43 -08003191 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07003192 tmp_path, dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08003193 &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003194 if (ret && ret != -EAGAIN) {
3195 mlog_errno(ret);
3196 goto out;
3197 }
3198
3199 ocfs2_free_path(tmp_path);
3200 tmp_path = NULL;
3201
3202 if (ret == 0)
3203 goto try_rotate;
3204 }
3205
3206out:
3207 ocfs2_free_path(tmp_path);
3208 ocfs2_free_path(restart_path);
3209 return ret;
3210}
3211
3212static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3213 int index)
3214{
3215 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3216 unsigned int size;
3217
3218 if (rec->e_leaf_clusters == 0) {
3219 /*
3220 * We consumed all of the merged-from record. An empty
3221 * extent cannot exist anywhere but the 1st array
3222 * position, so move things over if the merged-from
3223 * record doesn't occupy that position.
3224 *
3225 * This creates a new empty extent so the caller
3226 * should be smart enough to have removed any existing
3227 * ones.
3228 */
3229 if (index > 0) {
3230 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3231 size = index * sizeof(struct ocfs2_extent_rec);
3232 memmove(&el->l_recs[1], &el->l_recs[0], size);
3233 }
3234
3235 /*
3236 * Always memset - the caller doesn't check whether it
3237 * created an empty extent, so there could be junk in
3238 * the other fields.
3239 */
3240 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3241 }
3242}
3243
Joel Becker4fe82c32009-02-13 02:16:08 -08003244static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003245 struct ocfs2_path *left_path,
3246 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003247{
3248 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003249 u32 right_cpos;
3250 struct ocfs2_path *right_path = NULL;
3251 struct ocfs2_extent_list *left_el;
3252
3253 *ret_right_path = NULL;
3254
3255 /* This function shouldn't be called for non-trees. */
3256 BUG_ON(left_path->p_tree_depth == 0);
3257
3258 left_el = path_leaf_el(left_path);
3259 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3260
Joel Becker4fe82c32009-02-13 02:16:08 -08003261 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3262 left_path, &right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003263 if (ret) {
3264 mlog_errno(ret);
3265 goto out;
3266 }
3267
3268 /* This function shouldn't be called for the rightmost leaf. */
3269 BUG_ON(right_cpos == 0);
3270
Joel Beckerffdd7a52008-10-17 22:32:01 -07003271 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003272 if (!right_path) {
3273 ret = -ENOMEM;
3274 mlog_errno(ret);
3275 goto out;
3276 }
3277
Joel Becker4fe82c32009-02-13 02:16:08 -08003278 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003279 if (ret) {
3280 mlog_errno(ret);
3281 goto out;
3282 }
3283
3284 *ret_right_path = right_path;
3285out:
3286 if (ret)
3287 ocfs2_free_path(right_path);
3288 return ret;
3289}
3290
3291/*
3292 * Remove split_rec clusters from the record at index and merge them
3293 * onto the beginning of the record "next" to it.
3294 * For index < l_count - 1, the next means the extent rec at index + 1.
3295 * For index == l_count - 1, the "next" means the 1st extent rec of the
3296 * next extent block.
3297 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003298static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003299 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003300 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003301 struct ocfs2_extent_rec *split_rec,
3302 int index)
3303{
3304 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003305 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3306 struct ocfs2_extent_rec *left_rec;
3307 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003308 struct ocfs2_extent_list *right_el;
3309 struct ocfs2_path *right_path = NULL;
3310 int subtree_index = 0;
3311 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3312 struct buffer_head *bh = path_leaf_bh(left_path);
3313 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003314
3315 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003316 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003317
Al Viro9d8df6a2008-05-21 06:32:11 +01003318 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003319 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3320 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003321 ret = ocfs2_get_right_path(et, left_path, &right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003322 if (ret) {
3323 mlog_errno(ret);
3324 goto out;
3325 }
3326
3327 right_el = path_leaf_el(right_path);
3328 next_free = le16_to_cpu(right_el->l_next_free_rec);
3329 BUG_ON(next_free <= 0);
3330 right_rec = &right_el->l_recs[0];
3331 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003332 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003333 right_rec = &right_el->l_recs[1];
3334 }
3335
3336 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3337 le16_to_cpu(left_rec->e_leaf_clusters) !=
3338 le32_to_cpu(right_rec->e_cpos));
3339
Joel Becker7dc02802009-02-12 19:20:13 -08003340 subtree_index = ocfs2_find_subtree_root(et, left_path,
3341 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003342
3343 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3344 handle->h_buffer_credits,
3345 right_path);
3346 if (ret) {
3347 mlog_errno(ret);
3348 goto out;
3349 }
3350
3351 root_bh = left_path->p_node[subtree_index].bh;
3352 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3353
Joel Becker7dc02802009-02-12 19:20:13 -08003354 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003355 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003356 if (ret) {
3357 mlog_errno(ret);
3358 goto out;
3359 }
3360
3361 for (i = subtree_index + 1;
3362 i < path_num_items(right_path); i++) {
Joel Becker7dc02802009-02-12 19:20:13 -08003363 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003364 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003365 if (ret) {
3366 mlog_errno(ret);
3367 goto out;
3368 }
3369
Joel Becker7dc02802009-02-12 19:20:13 -08003370 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003371 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003372 if (ret) {
3373 mlog_errno(ret);
3374 goto out;
3375 }
3376 }
3377
3378 } else {
3379 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3380 right_rec = &el->l_recs[index + 1];
3381 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003382
Joel Becker7dc02802009-02-12 19:20:13 -08003383 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
Joel Becker13723d02008-10-17 19:25:01 -07003384 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003385 if (ret) {
3386 mlog_errno(ret);
3387 goto out;
3388 }
3389
3390 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3391
3392 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3393 le64_add_cpu(&right_rec->e_blkno,
Joel Becker7dc02802009-02-12 19:20:13 -08003394 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3395 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003396 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3397
3398 ocfs2_cleanup_merge(el, index);
3399
3400 ret = ocfs2_journal_dirty(handle, bh);
3401 if (ret)
3402 mlog_errno(ret);
3403
Tao Ma677b9752008-01-30 14:21:05 +08003404 if (right_path) {
3405 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3406 if (ret)
3407 mlog_errno(ret);
3408
Joel Becker4619c732009-02-12 19:02:36 -08003409 ocfs2_complete_edge_insert(handle, left_path, right_path,
3410 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003411 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003412out:
Tao Ma677b9752008-01-30 14:21:05 +08003413 if (right_path)
3414 ocfs2_free_path(right_path);
3415 return ret;
3416}
3417
Joel Becker4fe82c32009-02-13 02:16:08 -08003418static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003419 struct ocfs2_path *right_path,
3420 struct ocfs2_path **ret_left_path)
3421{
3422 int ret;
3423 u32 left_cpos;
3424 struct ocfs2_path *left_path = NULL;
3425
3426 *ret_left_path = NULL;
3427
3428 /* This function shouldn't be called for non-trees. */
3429 BUG_ON(right_path->p_tree_depth == 0);
3430
Joel Becker4fe82c32009-02-13 02:16:08 -08003431 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
Tao Ma677b9752008-01-30 14:21:05 +08003432 right_path, &left_cpos);
3433 if (ret) {
3434 mlog_errno(ret);
3435 goto out;
3436 }
3437
3438 /* This function shouldn't be called for the leftmost leaf. */
3439 BUG_ON(left_cpos == 0);
3440
Joel Beckerffdd7a52008-10-17 22:32:01 -07003441 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003442 if (!left_path) {
3443 ret = -ENOMEM;
3444 mlog_errno(ret);
3445 goto out;
3446 }
3447
Joel Becker4fe82c32009-02-13 02:16:08 -08003448 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003449 if (ret) {
3450 mlog_errno(ret);
3451 goto out;
3452 }
3453
3454 *ret_left_path = left_path;
3455out:
3456 if (ret)
3457 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003458 return ret;
3459}
3460
3461/*
3462 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003463 * onto the tail of the record "before" it.
3464 * For index > 0, the "before" means the extent rec at index - 1.
3465 *
3466 * For index == 0, the "before" means the last record of the previous
3467 * extent block. And there is also a situation that we may need to
3468 * remove the rightmost leaf extent block in the right_path and change
3469 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003470 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003471static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003472 handle_t *handle,
Joel Becker4fe82c32009-02-13 02:16:08 -08003473 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003474 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003475 struct ocfs2_cached_dealloc_ctxt *dealloc,
3476 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003477{
Tao Ma677b9752008-01-30 14:21:05 +08003478 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003479 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3480 struct ocfs2_extent_rec *left_rec;
3481 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003482 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3483 struct buffer_head *bh = path_leaf_bh(right_path);
3484 struct buffer_head *root_bh = NULL;
3485 struct ocfs2_path *left_path = NULL;
3486 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003487
Tao Ma677b9752008-01-30 14:21:05 +08003488 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003489
Mark Fasheh328d5752007-06-18 10:48:04 -07003490 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003491 if (index == 0) {
3492 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003493 ret = ocfs2_get_left_path(et, right_path, &left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003494 if (ret) {
3495 mlog_errno(ret);
3496 goto out;
3497 }
3498
3499 left_el = path_leaf_el(left_path);
3500 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3501 le16_to_cpu(left_el->l_count));
3502
3503 left_rec = &left_el->l_recs[
3504 le16_to_cpu(left_el->l_next_free_rec) - 1];
3505 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3506 le16_to_cpu(left_rec->e_leaf_clusters) !=
3507 le32_to_cpu(split_rec->e_cpos));
3508
Joel Becker7dc02802009-02-12 19:20:13 -08003509 subtree_index = ocfs2_find_subtree_root(et, left_path,
3510 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003511
3512 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3513 handle->h_buffer_credits,
3514 left_path);
3515 if (ret) {
3516 mlog_errno(ret);
3517 goto out;
3518 }
3519
3520 root_bh = left_path->p_node[subtree_index].bh;
3521 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3522
Joel Becker4fe82c32009-02-13 02:16:08 -08003523 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003524 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003525 if (ret) {
3526 mlog_errno(ret);
3527 goto out;
3528 }
3529
3530 for (i = subtree_index + 1;
3531 i < path_num_items(right_path); i++) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003532 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003533 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003534 if (ret) {
3535 mlog_errno(ret);
3536 goto out;
3537 }
3538
Joel Becker4fe82c32009-02-13 02:16:08 -08003539 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003540 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003541 if (ret) {
3542 mlog_errno(ret);
3543 goto out;
3544 }
3545 }
3546 } else {
3547 left_rec = &el->l_recs[index - 1];
3548 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3549 has_empty_extent = 1;
3550 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003551
Joel Becker4fe82c32009-02-13 02:16:08 -08003552 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Tao Ma9047bea2009-01-05 14:45:24 +08003553 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003554 if (ret) {
3555 mlog_errno(ret);
3556 goto out;
3557 }
3558
3559 if (has_empty_extent && index == 1) {
3560 /*
3561 * The easy case - we can just plop the record right in.
3562 */
3563 *left_rec = *split_rec;
3564
3565 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003566 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003567 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003568
3569 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3570 le64_add_cpu(&right_rec->e_blkno,
Joel Becker4fe82c32009-02-13 02:16:08 -08003571 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3572 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003573 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3574
3575 ocfs2_cleanup_merge(el, index);
3576
3577 ret = ocfs2_journal_dirty(handle, bh);
3578 if (ret)
3579 mlog_errno(ret);
3580
Tao Ma677b9752008-01-30 14:21:05 +08003581 if (left_path) {
3582 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3583 if (ret)
3584 mlog_errno(ret);
3585
3586 /*
3587 * In the situation that the right_rec is empty and the extent
3588 * block is empty also, ocfs2_complete_edge_insert can't handle
3589 * it and we need to delete the right extent block.
3590 */
3591 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3592 le16_to_cpu(el->l_next_free_rec) == 1) {
3593
Joel Becker70f18c02009-02-13 02:09:31 -08003594 ret = ocfs2_remove_rightmost_path(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003595 right_path,
Joel Becker70f18c02009-02-13 02:09:31 -08003596 dealloc);
Tao Ma677b9752008-01-30 14:21:05 +08003597 if (ret) {
3598 mlog_errno(ret);
3599 goto out;
3600 }
3601
3602 /* Now the rightmost extent block has been deleted.
3603 * So we use the new rightmost path.
3604 */
3605 ocfs2_mv_path(right_path, left_path);
3606 left_path = NULL;
3607 } else
Joel Becker4619c732009-02-12 19:02:36 -08003608 ocfs2_complete_edge_insert(handle, left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003609 right_path, subtree_index);
3610 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003611out:
Tao Ma677b9752008-01-30 14:21:05 +08003612 if (left_path)
3613 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003614 return ret;
3615}
3616
Joel Beckerc495dd22009-02-13 02:19:11 -08003617static int ocfs2_try_to_merge_extent(handle_t *handle,
3618 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003619 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003620 int split_index,
3621 struct ocfs2_extent_rec *split_rec,
3622 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckerc495dd22009-02-13 02:19:11 -08003623 struct ocfs2_merge_ctxt *ctxt)
Mark Fasheh328d5752007-06-18 10:48:04 -07003624{
Tao Mao518d7262007-08-28 17:25:35 -07003625 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003626 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003627 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3628
3629 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3630
Tao Mao518d7262007-08-28 17:25:35 -07003631 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3632 /*
3633 * The merge code will need to create an empty
3634 * extent to take the place of the newly
3635 * emptied slot. Remove any pre-existing empty
3636 * extents - having more than one in a leaf is
3637 * illegal.
3638 */
Joel Becker70f18c02009-02-13 02:09:31 -08003639 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Tao Mao518d7262007-08-28 17:25:35 -07003640 if (ret) {
3641 mlog_errno(ret);
3642 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003643 }
Tao Mao518d7262007-08-28 17:25:35 -07003644 split_index--;
3645 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003646 }
3647
3648 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3649 /*
3650 * Left-right contig implies this.
3651 */
3652 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003653
3654 /*
3655 * Since the leftright insert always covers the entire
3656 * extent, this call will delete the insert record
3657 * entirely, resulting in an empty extent record added to
3658 * the extent block.
3659 *
3660 * Since the adding of an empty extent shifts
3661 * everything back to the right, there's no need to
3662 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003663 *
3664 * When the split_index is zero, we need to merge it to the
3665 * prevoius extent block. It is more efficient and easier
3666 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003667 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003668 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003669 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003670 if (ret) {
3671 mlog_errno(ret);
3672 goto out;
3673 }
3674
3675 /*
3676 * We can only get this from logic error above.
3677 */
3678 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3679
Tao Ma677b9752008-01-30 14:21:05 +08003680 /* The merge left us with an empty extent, remove it. */
Joel Becker70f18c02009-02-13 02:09:31 -08003681 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003682 if (ret) {
3683 mlog_errno(ret);
3684 goto out;
3685 }
Tao Ma677b9752008-01-30 14:21:05 +08003686
Mark Fasheh328d5752007-06-18 10:48:04 -07003687 rec = &el->l_recs[split_index];
3688
3689 /*
3690 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003691 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003692 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003693 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3694 dealloc, split_index);
Tao Ma677b9752008-01-30 14:21:05 +08003695
Mark Fasheh328d5752007-06-18 10:48:04 -07003696 if (ret) {
3697 mlog_errno(ret);
3698 goto out;
3699 }
3700
Joel Becker70f18c02009-02-13 02:09:31 -08003701 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003702 /*
3703 * Error from this last rotate is not critical, so
3704 * print but don't bubble it up.
3705 */
3706 if (ret)
3707 mlog_errno(ret);
3708 ret = 0;
3709 } else {
3710 /*
3711 * Merge a record to the left or right.
3712 *
3713 * 'contig_type' is relative to the existing record,
3714 * so for example, if we're "right contig", it's to
3715 * the record on the left (hence the left merge).
3716 */
3717 if (ctxt->c_contig_type == CONTIG_RIGHT) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003718 ret = ocfs2_merge_rec_left(path, handle, et,
3719 split_rec, dealloc,
Mark Fasheh328d5752007-06-18 10:48:04 -07003720 split_index);
3721 if (ret) {
3722 mlog_errno(ret);
3723 goto out;
3724 }
3725 } else {
Joel Becker4fe82c32009-02-13 02:16:08 -08003726 ret = ocfs2_merge_rec_right(path, handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003727 et, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003728 split_index);
3729 if (ret) {
3730 mlog_errno(ret);
3731 goto out;
3732 }
3733 }
3734
3735 if (ctxt->c_split_covers_rec) {
3736 /*
3737 * The merge may have left an empty extent in
3738 * our leaf. Try to rotate it away.
3739 */
Joel Becker70f18c02009-02-13 02:09:31 -08003740 ret = ocfs2_rotate_tree_left(handle, et, path,
3741 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003742 if (ret)
3743 mlog_errno(ret);
3744 ret = 0;
3745 }
3746 }
3747
3748out:
3749 return ret;
3750}
3751
3752static void ocfs2_subtract_from_rec(struct super_block *sb,
3753 enum ocfs2_split_type split,
3754 struct ocfs2_extent_rec *rec,
3755 struct ocfs2_extent_rec *split_rec)
3756{
3757 u64 len_blocks;
3758
3759 len_blocks = ocfs2_clusters_to_blocks(sb,
3760 le16_to_cpu(split_rec->e_leaf_clusters));
3761
3762 if (split == SPLIT_LEFT) {
3763 /*
3764 * Region is on the left edge of the existing
3765 * record.
3766 */
3767 le32_add_cpu(&rec->e_cpos,
3768 le16_to_cpu(split_rec->e_leaf_clusters));
3769 le64_add_cpu(&rec->e_blkno, len_blocks);
3770 le16_add_cpu(&rec->e_leaf_clusters,
3771 -le16_to_cpu(split_rec->e_leaf_clusters));
3772 } else {
3773 /*
3774 * Region is on the right edge of the existing
3775 * record.
3776 */
3777 le16_add_cpu(&rec->e_leaf_clusters,
3778 -le16_to_cpu(split_rec->e_leaf_clusters));
3779 }
3780}
3781
Mark Fashehdcd05382007-01-16 11:32:23 -08003782/*
3783 * Do the final bits of extent record insertion at the target leaf
3784 * list. If this leaf is part of an allocation tree, it is assumed
3785 * that the tree above has been prepared.
3786 */
3787static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3788 struct ocfs2_extent_list *el,
3789 struct ocfs2_insert_type *insert,
3790 struct inode *inode)
3791{
3792 int i = insert->ins_contig_index;
3793 unsigned int range;
3794 struct ocfs2_extent_rec *rec;
3795
Mark Fashehe48edee2007-03-07 16:46:57 -08003796 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003797
Mark Fasheh328d5752007-06-18 10:48:04 -07003798 if (insert->ins_split != SPLIT_NONE) {
3799 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3800 BUG_ON(i == -1);
3801 rec = &el->l_recs[i];
3802 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3803 insert_rec);
3804 goto rotate;
3805 }
3806
Mark Fashehdcd05382007-01-16 11:32:23 -08003807 /*
3808 * Contiguous insert - either left or right.
3809 */
3810 if (insert->ins_contig != CONTIG_NONE) {
3811 rec = &el->l_recs[i];
3812 if (insert->ins_contig == CONTIG_LEFT) {
3813 rec->e_blkno = insert_rec->e_blkno;
3814 rec->e_cpos = insert_rec->e_cpos;
3815 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003816 le16_add_cpu(&rec->e_leaf_clusters,
3817 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003818 return;
3819 }
3820
3821 /*
3822 * Handle insert into an empty leaf.
3823 */
3824 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3825 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3826 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3827 el->l_recs[0] = *insert_rec;
3828 el->l_next_free_rec = cpu_to_le16(1);
3829 return;
3830 }
3831
3832 /*
3833 * Appending insert.
3834 */
3835 if (insert->ins_appending == APPEND_TAIL) {
3836 i = le16_to_cpu(el->l_next_free_rec) - 1;
3837 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003838 range = le32_to_cpu(rec->e_cpos)
3839 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003840 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3841
3842 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3843 le16_to_cpu(el->l_count),
3844 "inode %lu, depth %u, count %u, next free %u, "
3845 "rec.cpos %u, rec.clusters %u, "
3846 "insert.cpos %u, insert.clusters %u\n",
3847 inode->i_ino,
3848 le16_to_cpu(el->l_tree_depth),
3849 le16_to_cpu(el->l_count),
3850 le16_to_cpu(el->l_next_free_rec),
3851 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003852 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003853 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003854 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003855 i++;
3856 el->l_recs[i] = *insert_rec;
3857 le16_add_cpu(&el->l_next_free_rec, 1);
3858 return;
3859 }
3860
Mark Fasheh328d5752007-06-18 10:48:04 -07003861rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003862 /*
3863 * Ok, we have to rotate.
3864 *
3865 * At this point, it is safe to assume that inserting into an
3866 * empty leaf and appending to a leaf have both been handled
3867 * above.
3868 *
3869 * This leaf needs to have space, either by the empty 1st
3870 * extent record, or by virtue of an l_next_rec < l_count.
3871 */
3872 ocfs2_rotate_leaf(el, insert_rec);
3873}
3874
Joel Beckerd401dc12009-02-13 02:24:10 -08003875static void ocfs2_adjust_rightmost_records(handle_t *handle,
3876 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003877 struct ocfs2_path *path,
3878 struct ocfs2_extent_rec *insert_rec)
3879{
3880 int ret, i, next_free;
3881 struct buffer_head *bh;
3882 struct ocfs2_extent_list *el;
3883 struct ocfs2_extent_rec *rec;
3884
3885 /*
3886 * Update everything except the leaf block.
3887 */
3888 for (i = 0; i < path->p_tree_depth; i++) {
3889 bh = path->p_node[i].bh;
3890 el = path->p_node[i].el;
3891
3892 next_free = le16_to_cpu(el->l_next_free_rec);
3893 if (next_free == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08003894 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3895 "Owner %llu has a bad extent list",
3896 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fasheh328d5752007-06-18 10:48:04 -07003897 ret = -EIO;
3898 return;
3899 }
3900
3901 rec = &el->l_recs[next_free - 1];
3902
3903 rec->e_int_clusters = insert_rec->e_cpos;
3904 le32_add_cpu(&rec->e_int_clusters,
3905 le16_to_cpu(insert_rec->e_leaf_clusters));
3906 le32_add_cpu(&rec->e_int_clusters,
3907 -le32_to_cpu(rec->e_cpos));
3908
3909 ret = ocfs2_journal_dirty(handle, bh);
3910 if (ret)
3911 mlog_errno(ret);
3912
3913 }
3914}
3915
Joel Beckerd401dc12009-02-13 02:24:10 -08003916static int ocfs2_append_rec_to_path(handle_t *handle,
3917 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08003918 struct ocfs2_extent_rec *insert_rec,
3919 struct ocfs2_path *right_path,
3920 struct ocfs2_path **ret_left_path)
3921{
Mark Fasheh328d5752007-06-18 10:48:04 -07003922 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003923 struct ocfs2_extent_list *el;
3924 struct ocfs2_path *left_path = NULL;
3925
3926 *ret_left_path = NULL;
3927
3928 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003929 * This shouldn't happen for non-trees. The extent rec cluster
3930 * count manipulation below only works for interior nodes.
3931 */
3932 BUG_ON(right_path->p_tree_depth == 0);
3933
3934 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003935 * If our appending insert is at the leftmost edge of a leaf,
3936 * then we might need to update the rightmost records of the
3937 * neighboring path.
3938 */
3939 el = path_leaf_el(right_path);
3940 next_free = le16_to_cpu(el->l_next_free_rec);
3941 if (next_free == 0 ||
3942 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3943 u32 left_cpos;
3944
Joel Beckerd401dc12009-02-13 02:24:10 -08003945 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3946 right_path, &left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08003947 if (ret) {
3948 mlog_errno(ret);
3949 goto out;
3950 }
3951
3952 mlog(0, "Append may need a left path update. cpos: %u, "
3953 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3954 left_cpos);
3955
3956 /*
3957 * No need to worry if the append is already in the
3958 * leftmost leaf.
3959 */
3960 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07003961 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003962 if (!left_path) {
3963 ret = -ENOMEM;
3964 mlog_errno(ret);
3965 goto out;
3966 }
3967
Joel Beckerd401dc12009-02-13 02:24:10 -08003968 ret = ocfs2_find_path(et->et_ci, left_path,
Joel Beckerfacdb772009-02-12 18:08:48 -08003969 left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08003970 if (ret) {
3971 mlog_errno(ret);
3972 goto out;
3973 }
3974
3975 /*
3976 * ocfs2_insert_path() will pass the left_path to the
3977 * journal for us.
3978 */
3979 }
3980 }
3981
Joel Beckerd401dc12009-02-13 02:24:10 -08003982 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003983 if (ret) {
3984 mlog_errno(ret);
3985 goto out;
3986 }
3987
Joel Beckerd401dc12009-02-13 02:24:10 -08003988 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08003989
3990 *ret_left_path = left_path;
3991 ret = 0;
3992out:
3993 if (ret != 0)
3994 ocfs2_free_path(left_path);
3995
3996 return ret;
3997}
3998
Mark Fasheh328d5752007-06-18 10:48:04 -07003999static void ocfs2_split_record(struct inode *inode,
4000 struct ocfs2_path *left_path,
4001 struct ocfs2_path *right_path,
4002 struct ocfs2_extent_rec *split_rec,
4003 enum ocfs2_split_type split)
4004{
4005 int index;
4006 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4007 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4008 struct ocfs2_extent_rec *rec, *tmprec;
4009
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004010 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004011 if (left_path)
4012 left_el = path_leaf_el(left_path);
4013
4014 el = right_el;
4015 insert_el = right_el;
4016 index = ocfs2_search_extent_list(el, cpos);
4017 if (index != -1) {
4018 if (index == 0 && left_path) {
4019 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4020
4021 /*
4022 * This typically means that the record
4023 * started in the left path but moved to the
4024 * right as a result of rotation. We either
4025 * move the existing record to the left, or we
4026 * do the later insert there.
4027 *
4028 * In this case, the left path should always
4029 * exist as the rotate code will have passed
4030 * it back for a post-insert update.
4031 */
4032
4033 if (split == SPLIT_LEFT) {
4034 /*
4035 * It's a left split. Since we know
4036 * that the rotate code gave us an
4037 * empty extent in the left path, we
4038 * can just do the insert there.
4039 */
4040 insert_el = left_el;
4041 } else {
4042 /*
4043 * Right split - we have to move the
4044 * existing record over to the left
4045 * leaf. The insert will be into the
4046 * newly created empty extent in the
4047 * right leaf.
4048 */
4049 tmprec = &right_el->l_recs[index];
4050 ocfs2_rotate_leaf(left_el, tmprec);
4051 el = left_el;
4052
4053 memset(tmprec, 0, sizeof(*tmprec));
4054 index = ocfs2_search_extent_list(left_el, cpos);
4055 BUG_ON(index == -1);
4056 }
4057 }
4058 } else {
4059 BUG_ON(!left_path);
4060 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4061 /*
4062 * Left path is easy - we can just allow the insert to
4063 * happen.
4064 */
4065 el = left_el;
4066 insert_el = left_el;
4067 index = ocfs2_search_extent_list(el, cpos);
4068 BUG_ON(index == -1);
4069 }
4070
4071 rec = &el->l_recs[index];
4072 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
4073 ocfs2_rotate_leaf(insert_el, split_rec);
4074}
4075
Mark Fashehdcd05382007-01-16 11:32:23 -08004076/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004077 * This function only does inserts on an allocation b-tree. For tree
4078 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004079 *
4080 * right_path is the path we want to do the actual insert
4081 * in. left_path should only be passed in if we need to update that
4082 * portion of the tree after an edge insert.
4083 */
4084static int ocfs2_insert_path(struct inode *inode,
4085 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08004086 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004087 struct ocfs2_path *left_path,
4088 struct ocfs2_path *right_path,
4089 struct ocfs2_extent_rec *insert_rec,
4090 struct ocfs2_insert_type *insert)
4091{
4092 int ret, subtree_index;
4093 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004094
Mark Fashehdcd05382007-01-16 11:32:23 -08004095 if (left_path) {
4096 int credits = handle->h_buffer_credits;
4097
4098 /*
4099 * There's a chance that left_path got passed back to
4100 * us without being accounted for in the
4101 * journal. Extend our transaction here to be sure we
4102 * can change those blocks.
4103 */
4104 credits += left_path->p_tree_depth;
4105
4106 ret = ocfs2_extend_trans(handle, credits);
4107 if (ret < 0) {
4108 mlog_errno(ret);
4109 goto out;
4110 }
4111
Joel Becker7dc02802009-02-12 19:20:13 -08004112 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004113 if (ret < 0) {
4114 mlog_errno(ret);
4115 goto out;
4116 }
4117 }
4118
Mark Fashehe8aed342007-12-03 16:43:01 -08004119 /*
4120 * Pass both paths to the journal. The majority of inserts
4121 * will be touching all components anyway.
4122 */
Joel Becker7dc02802009-02-12 19:20:13 -08004123 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004124 if (ret < 0) {
4125 mlog_errno(ret);
4126 goto out;
4127 }
4128
Mark Fasheh328d5752007-06-18 10:48:04 -07004129 if (insert->ins_split != SPLIT_NONE) {
4130 /*
4131 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004132 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004133 * function sort it all out.
4134 */
4135 ocfs2_split_record(inode, left_path, right_path,
4136 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004137
4138 /*
4139 * Split might have modified either leaf and we don't
4140 * have a guarantee that the later edge insert will
4141 * dirty this for us.
4142 */
4143 if (left_path)
4144 ret = ocfs2_journal_dirty(handle,
4145 path_leaf_bh(left_path));
4146 if (ret)
4147 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07004148 } else
4149 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
4150 insert, inode);
Mark Fashehdcd05382007-01-16 11:32:23 -08004151
Mark Fashehdcd05382007-01-16 11:32:23 -08004152 ret = ocfs2_journal_dirty(handle, leaf_bh);
4153 if (ret)
4154 mlog_errno(ret);
4155
4156 if (left_path) {
4157 /*
4158 * The rotate code has indicated that we need to fix
4159 * up portions of the tree after the insert.
4160 *
4161 * XXX: Should we extend the transaction here?
4162 */
Joel Becker7dc02802009-02-12 19:20:13 -08004163 subtree_index = ocfs2_find_subtree_root(et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004164 right_path);
Joel Becker4619c732009-02-12 19:02:36 -08004165 ocfs2_complete_edge_insert(handle, left_path, right_path,
4166 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08004167 }
4168
4169 ret = 0;
4170out:
4171 return ret;
4172}
4173
4174static int ocfs2_do_insert_extent(struct inode *inode,
4175 handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004176 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004177 struct ocfs2_extent_rec *insert_rec,
4178 struct ocfs2_insert_type *type)
4179{
4180 int ret, rotate = 0;
4181 u32 cpos;
4182 struct ocfs2_path *right_path = NULL;
4183 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004184 struct ocfs2_extent_list *el;
4185
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004186 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004187
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004188 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004189 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004190 if (ret) {
4191 mlog_errno(ret);
4192 goto out;
4193 }
4194
4195 if (le16_to_cpu(el->l_tree_depth) == 0) {
4196 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4197 goto out_update_clusters;
4198 }
4199
Joel Beckerffdd7a52008-10-17 22:32:01 -07004200 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004201 if (!right_path) {
4202 ret = -ENOMEM;
4203 mlog_errno(ret);
4204 goto out;
4205 }
4206
4207 /*
4208 * Determine the path to start with. Rotations need the
4209 * rightmost path, everything else can go directly to the
4210 * target leaf.
4211 */
4212 cpos = le32_to_cpu(insert_rec->e_cpos);
4213 if (type->ins_appending == APPEND_NONE &&
4214 type->ins_contig == CONTIG_NONE) {
4215 rotate = 1;
4216 cpos = UINT_MAX;
4217 }
4218
Joel Beckerfacdb772009-02-12 18:08:48 -08004219 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004220 if (ret) {
4221 mlog_errno(ret);
4222 goto out;
4223 }
4224
4225 /*
4226 * Rotations and appends need special treatment - they modify
4227 * parts of the tree's above them.
4228 *
4229 * Both might pass back a path immediate to the left of the
4230 * one being inserted to. This will be cause
4231 * ocfs2_insert_path() to modify the rightmost records of
4232 * left_path to account for an edge insert.
4233 *
4234 * XXX: When modifying this code, keep in mind that an insert
4235 * can wind up skipping both of these two special cases...
4236 */
4237 if (rotate) {
Joel Becker1bbf0b82009-02-12 19:42:08 -08004238 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004239 le32_to_cpu(insert_rec->e_cpos),
4240 right_path, &left_path);
4241 if (ret) {
4242 mlog_errno(ret);
4243 goto out;
4244 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004245
4246 /*
4247 * ocfs2_rotate_tree_right() might have extended the
4248 * transaction without re-journaling our tree root.
4249 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004250 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004251 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004252 if (ret) {
4253 mlog_errno(ret);
4254 goto out;
4255 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004256 } else if (type->ins_appending == APPEND_TAIL
4257 && type->ins_contig != CONTIG_LEFT) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004258 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08004259 right_path, &left_path);
4260 if (ret) {
4261 mlog_errno(ret);
4262 goto out;
4263 }
4264 }
4265
Joel Becker7dc02802009-02-12 19:20:13 -08004266 ret = ocfs2_insert_path(inode, handle, et, left_path, right_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004267 insert_rec, type);
4268 if (ret) {
4269 mlog_errno(ret);
4270 goto out;
4271 }
4272
4273out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004274 if (type->ins_split == SPLIT_NONE)
Joel Becker6136ca52009-02-12 19:32:43 -08004275 ocfs2_et_update_clusters(et,
Joel Becker35dc0aa2008-08-20 16:25:06 -07004276 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004277
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004278 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004279 if (ret)
4280 mlog_errno(ret);
4281
4282out:
4283 ocfs2_free_path(left_path);
4284 ocfs2_free_path(right_path);
4285
4286 return ret;
4287}
4288
Mark Fasheh328d5752007-06-18 10:48:04 -07004289static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08004290ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004291 struct ocfs2_extent_list *el, int index,
4292 struct ocfs2_extent_rec *split_rec)
4293{
Tao Maad5a4d72008-01-30 14:21:32 +08004294 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004295 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004296 u32 left_cpos, right_cpos;
4297 struct ocfs2_extent_rec *rec = NULL;
4298 struct ocfs2_extent_list *new_el;
4299 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4300 struct buffer_head *bh;
4301 struct ocfs2_extent_block *eb;
4302
4303 if (index > 0) {
4304 rec = &el->l_recs[index - 1];
4305 } else if (path->p_tree_depth > 0) {
4306 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4307 path, &left_cpos);
4308 if (status)
4309 goto out;
4310
4311 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004312 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004313 if (!left_path)
4314 goto out;
4315
Joel Beckerfacdb772009-02-12 18:08:48 -08004316 status = ocfs2_find_path(INODE_CACHE(inode),
4317 left_path, left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004318 if (status)
4319 goto out;
4320
4321 new_el = path_leaf_el(left_path);
4322
4323 if (le16_to_cpu(new_el->l_next_free_rec) !=
4324 le16_to_cpu(new_el->l_count)) {
4325 bh = path_leaf_bh(left_path);
4326 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004327 ocfs2_error(inode->i_sb,
4328 "Extent block #%llu has an "
4329 "invalid l_next_free_rec of "
4330 "%d. It should have "
4331 "matched the l_count of %d",
4332 (unsigned long long)le64_to_cpu(eb->h_blkno),
4333 le16_to_cpu(new_el->l_next_free_rec),
4334 le16_to_cpu(new_el->l_count));
4335 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004336 goto out;
4337 }
4338 rec = &new_el->l_recs[
4339 le16_to_cpu(new_el->l_next_free_rec) - 1];
4340 }
4341 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004342
4343 /*
4344 * We're careful to check for an empty extent record here -
4345 * the merge code will know what to do if it sees one.
4346 */
Tao Maad5a4d72008-01-30 14:21:32 +08004347 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004348 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4349 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4350 ret = CONTIG_RIGHT;
4351 } else {
4352 ret = ocfs2_extent_contig(inode, rec, split_rec);
4353 }
4354 }
4355
Tao Maad5a4d72008-01-30 14:21:32 +08004356 rec = NULL;
4357 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4358 rec = &el->l_recs[index + 1];
4359 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4360 path->p_tree_depth > 0) {
4361 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4362 path, &right_cpos);
4363 if (status)
4364 goto out;
4365
4366 if (right_cpos == 0)
4367 goto out;
4368
Joel Beckerffdd7a52008-10-17 22:32:01 -07004369 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004370 if (!right_path)
4371 goto out;
4372
Joel Beckerfacdb772009-02-12 18:08:48 -08004373 status = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004374 if (status)
4375 goto out;
4376
4377 new_el = path_leaf_el(right_path);
4378 rec = &new_el->l_recs[0];
4379 if (ocfs2_is_empty_extent(rec)) {
4380 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4381 bh = path_leaf_bh(right_path);
4382 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004383 ocfs2_error(inode->i_sb,
4384 "Extent block #%llu has an "
4385 "invalid l_next_free_rec of %d",
4386 (unsigned long long)le64_to_cpu(eb->h_blkno),
4387 le16_to_cpu(new_el->l_next_free_rec));
4388 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004389 goto out;
4390 }
4391 rec = &new_el->l_recs[1];
4392 }
4393 }
4394
4395 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004396 enum ocfs2_contig_type contig_type;
4397
Mark Fasheh328d5752007-06-18 10:48:04 -07004398 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4399
4400 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4401 ret = CONTIG_LEFTRIGHT;
4402 else if (ret == CONTIG_NONE)
4403 ret = contig_type;
4404 }
4405
Tao Maad5a4d72008-01-30 14:21:32 +08004406out:
4407 if (left_path)
4408 ocfs2_free_path(left_path);
4409 if (right_path)
4410 ocfs2_free_path(right_path);
4411
Mark Fasheh328d5752007-06-18 10:48:04 -07004412 return ret;
4413}
4414
Mark Fashehdcd05382007-01-16 11:32:23 -08004415static void ocfs2_figure_contig_type(struct inode *inode,
4416 struct ocfs2_insert_type *insert,
4417 struct ocfs2_extent_list *el,
Tao Maca12b7c2008-08-18 17:38:52 +08004418 struct ocfs2_extent_rec *insert_rec,
4419 struct ocfs2_extent_tree *et)
Mark Fashehdcd05382007-01-16 11:32:23 -08004420{
4421 int i;
4422 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4423
Mark Fashehe48edee2007-03-07 16:46:57 -08004424 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4425
Mark Fashehdcd05382007-01-16 11:32:23 -08004426 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4427 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4428 insert_rec);
4429 if (contig_type != CONTIG_NONE) {
4430 insert->ins_contig_index = i;
4431 break;
4432 }
4433 }
4434 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004435
4436 if (insert->ins_contig != CONTIG_NONE) {
4437 struct ocfs2_extent_rec *rec =
4438 &el->l_recs[insert->ins_contig_index];
4439 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4440 le16_to_cpu(insert_rec->e_leaf_clusters);
4441
4442 /*
4443 * Caller might want us to limit the size of extents, don't
4444 * calculate contiguousness if we might exceed that limit.
4445 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004446 if (et->et_max_leaf_clusters &&
4447 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004448 insert->ins_contig = CONTIG_NONE;
4449 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004450}
4451
4452/*
4453 * This should only be called against the righmost leaf extent list.
4454 *
4455 * ocfs2_figure_appending_type() will figure out whether we'll have to
4456 * insert at the tail of the rightmost leaf.
4457 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004458 * This should also work against the root extent list for tree's with 0
4459 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004460 * then the logic here makes sense.
4461 */
4462static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4463 struct ocfs2_extent_list *el,
4464 struct ocfs2_extent_rec *insert_rec)
4465{
4466 int i;
4467 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4468 struct ocfs2_extent_rec *rec;
4469
4470 insert->ins_appending = APPEND_NONE;
4471
Mark Fashehe48edee2007-03-07 16:46:57 -08004472 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004473
4474 if (!el->l_next_free_rec)
4475 goto set_tail_append;
4476
4477 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4478 /* Were all records empty? */
4479 if (le16_to_cpu(el->l_next_free_rec) == 1)
4480 goto set_tail_append;
4481 }
4482
4483 i = le16_to_cpu(el->l_next_free_rec) - 1;
4484 rec = &el->l_recs[i];
4485
Mark Fashehe48edee2007-03-07 16:46:57 -08004486 if (cpos >=
4487 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004488 goto set_tail_append;
4489
4490 return;
4491
4492set_tail_append:
4493 insert->ins_appending = APPEND_TAIL;
4494}
4495
4496/*
4497 * Helper function called at the begining of an insert.
4498 *
4499 * This computes a few things that are commonly used in the process of
4500 * inserting into the btree:
4501 * - Whether the new extent is contiguous with an existing one.
4502 * - The current tree depth.
4503 * - Whether the insert is an appending one.
4504 * - The total # of free records in the tree.
4505 *
4506 * All of the information is stored on the ocfs2_insert_type
4507 * structure.
4508 */
4509static int ocfs2_figure_insert_type(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004510 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004511 struct buffer_head **last_eb_bh,
4512 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004513 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004514 struct ocfs2_insert_type *insert)
4515{
4516 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004517 struct ocfs2_extent_block *eb;
4518 struct ocfs2_extent_list *el;
4519 struct ocfs2_path *path = NULL;
4520 struct buffer_head *bh = NULL;
4521
Mark Fasheh328d5752007-06-18 10:48:04 -07004522 insert->ins_split = SPLIT_NONE;
4523
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004524 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004525 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4526
4527 if (el->l_tree_depth) {
4528 /*
4529 * If we have tree depth, we read in the
4530 * rightmost extent block ahead of time as
4531 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4532 * may want it later.
4533 */
Joel Becker3d03a302009-02-12 17:49:26 -08004534 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08004535 ocfs2_et_get_last_eb_blk(et),
4536 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004537 if (ret) {
4538 mlog_exit(ret);
4539 goto out;
4540 }
4541 eb = (struct ocfs2_extent_block *) bh->b_data;
4542 el = &eb->h_list;
4543 }
4544
4545 /*
4546 * Unless we have a contiguous insert, we'll need to know if
4547 * there is room left in our allocation tree for another
4548 * extent record.
4549 *
4550 * XXX: This test is simplistic, we can search for empty
4551 * extent records too.
4552 */
Tao Maoc77534f2007-08-28 17:22:33 -07004553 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004554 le16_to_cpu(el->l_next_free_rec);
4555
4556 if (!insert->ins_tree_depth) {
Tao Maca12b7c2008-08-18 17:38:52 +08004557 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004558 ocfs2_figure_appending_type(insert, el, insert_rec);
4559 return 0;
4560 }
4561
Joel Beckerffdd7a52008-10-17 22:32:01 -07004562 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004563 if (!path) {
4564 ret = -ENOMEM;
4565 mlog_errno(ret);
4566 goto out;
4567 }
4568
4569 /*
4570 * In the case that we're inserting past what the tree
4571 * currently accounts for, ocfs2_find_path() will return for
4572 * us the rightmost tree path. This is accounted for below in
4573 * the appending code.
4574 */
Joel Beckerfacdb772009-02-12 18:08:48 -08004575 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -08004576 if (ret) {
4577 mlog_errno(ret);
4578 goto out;
4579 }
4580
4581 el = path_leaf_el(path);
4582
4583 /*
4584 * Now that we have the path, there's two things we want to determine:
4585 * 1) Contiguousness (also set contig_index if this is so)
4586 *
4587 * 2) Are we doing an append? We can trivially break this up
4588 * into two types of appends: simple record append, or a
4589 * rotate inside the tail leaf.
4590 */
Tao Maca12b7c2008-08-18 17:38:52 +08004591 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004592
4593 /*
4594 * The insert code isn't quite ready to deal with all cases of
4595 * left contiguousness. Specifically, if it's an insert into
4596 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004597 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004598 * left. For now, just catch that case and fool the layers
4599 * above us. This works just fine for tree_depth == 0, which
4600 * is why we allow that above.
4601 */
4602 if (insert->ins_contig == CONTIG_LEFT &&
4603 insert->ins_contig_index == 0)
4604 insert->ins_contig = CONTIG_NONE;
4605
4606 /*
4607 * Ok, so we can simply compare against last_eb to figure out
4608 * whether the path doesn't exist. This will only happen in
4609 * the case that we're doing a tail append, so maybe we can
4610 * take advantage of that information somehow.
4611 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004612 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004613 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004614 /*
4615 * Ok, ocfs2_find_path() returned us the rightmost
4616 * tree path. This might be an appending insert. There are
4617 * two cases:
4618 * 1) We're doing a true append at the tail:
4619 * -This might even be off the end of the leaf
4620 * 2) We're "appending" by rotating in the tail
4621 */
4622 ocfs2_figure_appending_type(insert, el, insert_rec);
4623 }
4624
4625out:
4626 ocfs2_free_path(path);
4627
4628 if (ret == 0)
4629 *last_eb_bh = bh;
4630 else
4631 brelse(bh);
4632 return ret;
4633}
4634
4635/*
4636 * Insert an extent into an inode btree.
4637 *
4638 * The caller needs to update fe->i_clusters
4639 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004640int ocfs2_insert_extent(struct ocfs2_super *osb,
4641 handle_t *handle,
4642 struct inode *inode,
4643 struct ocfs2_extent_tree *et,
4644 u32 cpos,
4645 u64 start_blk,
4646 u32 new_clusters,
4647 u8 flags,
4648 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004649{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004650 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004651 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004652 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004653 struct ocfs2_insert_type insert = {0, };
4654 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004655
Mark Fashehdcd05382007-01-16 11:32:23 -08004656 mlog(0, "add %u clusters at position %u to inode %llu\n",
4657 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004658
Mark Fashehe48edee2007-03-07 16:46:57 -08004659 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004660 rec.e_cpos = cpu_to_le32(cpos);
4661 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004662 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004663 rec.e_flags = flags;
Joel Becker6136ca52009-02-12 19:32:43 -08004664 status = ocfs2_et_insert_check(et, &rec);
Joel Becker1e61ee72008-08-20 18:32:45 -07004665 if (status) {
4666 mlog_errno(status);
4667 goto bail;
4668 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004669
Tao Mae7d4cb62008-08-18 17:38:44 +08004670 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004671 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004672 if (status < 0) {
4673 mlog_errno(status);
4674 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004675 }
4676
Mark Fashehdcd05382007-01-16 11:32:23 -08004677 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4678 "Insert.contig_index: %d, Insert.free_records: %d, "
4679 "Insert.tree_depth: %d\n",
4680 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004681 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004682
Tao Maoc77534f2007-08-28 17:22:33 -07004683 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004684 status = ocfs2_grow_tree(handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004685 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004686 meta_ac);
4687 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004688 mlog_errno(status);
4689 goto bail;
4690 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004691 }
4692
Mark Fashehdcd05382007-01-16 11:32:23 -08004693 /* Finally, we can add clusters. This might rotate the tree for us. */
Tao Mae7d4cb62008-08-18 17:38:44 +08004694 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004695 if (status < 0)
4696 mlog_errno(status);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004697 else if (et->et_ops == &ocfs2_dinode_et_ops)
Mark Fasheh83418972007-04-23 18:53:12 -07004698 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004699
4700bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004701 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004702
Tao Maf56654c2008-08-18 17:38:48 +08004703 mlog_exit(status);
4704 return status;
4705}
4706
Tao Ma0eb8d472008-08-18 17:38:45 +08004707/*
4708 * Allcate and add clusters into the extent b-tree.
4709 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004710 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004711 * it is not limited to the file storage. Any extent tree can use this
4712 * function if it implements the proper ocfs2_extent_tree.
4713 */
4714int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4715 struct inode *inode,
4716 u32 *logical_offset,
4717 u32 clusters_to_add,
4718 int mark_unwritten,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004719 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004720 handle_t *handle,
4721 struct ocfs2_alloc_context *data_ac,
4722 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004723 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004724{
4725 int status = 0;
4726 int free_extents;
4727 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4728 u32 bit_off, num_bits;
4729 u64 block;
4730 u8 flags = 0;
4731
4732 BUG_ON(!clusters_to_add);
4733
4734 if (mark_unwritten)
4735 flags = OCFS2_EXT_UNWRITTEN;
4736
Joel Becker3d03a302009-02-12 17:49:26 -08004737 free_extents = ocfs2_num_free_extents(osb, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004738 if (free_extents < 0) {
4739 status = free_extents;
4740 mlog_errno(status);
4741 goto leave;
4742 }
4743
4744 /* there are two cases which could cause us to EAGAIN in the
4745 * we-need-more-metadata case:
4746 * 1) we haven't reserved *any*
4747 * 2) we are so fragmented, we've needed to add metadata too
4748 * many times. */
4749 if (!free_extents && !meta_ac) {
4750 mlog(0, "we haven't reserved any metadata!\n");
4751 status = -EAGAIN;
4752 reason = RESTART_META;
4753 goto leave;
4754 } else if ((!free_extents)
4755 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004756 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004757 mlog(0, "filesystem is really fragmented...\n");
4758 status = -EAGAIN;
4759 reason = RESTART_META;
4760 goto leave;
4761 }
4762
4763 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4764 clusters_to_add, &bit_off, &num_bits);
4765 if (status < 0) {
4766 if (status != -ENOSPC)
4767 mlog_errno(status);
4768 goto leave;
4769 }
4770
4771 BUG_ON(num_bits > clusters_to_add);
4772
Joel Becker13723d02008-10-17 19:25:01 -07004773 /* reserve our write early -- insert_extent may update the tree root */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004774 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004775 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d472008-08-18 17:38:45 +08004776 if (status < 0) {
4777 mlog_errno(status);
4778 goto leave;
4779 }
4780
4781 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4782 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4783 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004784 status = ocfs2_insert_extent(osb, handle, inode, et,
4785 *logical_offset, block,
4786 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004787 if (status < 0) {
4788 mlog_errno(status);
4789 goto leave;
4790 }
4791
Joel Beckerf99b9b72008-08-20 19:36:33 -07004792 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004793 if (status < 0) {
4794 mlog_errno(status);
4795 goto leave;
4796 }
4797
4798 clusters_to_add -= num_bits;
4799 *logical_offset += num_bits;
4800
4801 if (clusters_to_add) {
4802 mlog(0, "need to alloc once more, wanted = %u\n",
4803 clusters_to_add);
4804 status = -EAGAIN;
4805 reason = RESTART_TRANS;
4806 }
4807
4808leave:
4809 mlog_exit(status);
4810 if (reason_ret)
4811 *reason_ret = reason;
4812 return status;
4813}
4814
Mark Fasheh328d5752007-06-18 10:48:04 -07004815static void ocfs2_make_right_split_rec(struct super_block *sb,
4816 struct ocfs2_extent_rec *split_rec,
4817 u32 cpos,
4818 struct ocfs2_extent_rec *rec)
4819{
4820 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4821 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4822
4823 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4824
4825 split_rec->e_cpos = cpu_to_le32(cpos);
4826 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4827
4828 split_rec->e_blkno = rec->e_blkno;
4829 le64_add_cpu(&split_rec->e_blkno,
4830 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4831
4832 split_rec->e_flags = rec->e_flags;
4833}
4834
4835static int ocfs2_split_and_insert(struct inode *inode,
4836 handle_t *handle,
4837 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004838 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004839 struct buffer_head **last_eb_bh,
4840 int split_index,
4841 struct ocfs2_extent_rec *orig_split_rec,
4842 struct ocfs2_alloc_context *meta_ac)
4843{
4844 int ret = 0, depth;
4845 unsigned int insert_range, rec_range, do_leftright = 0;
4846 struct ocfs2_extent_rec tmprec;
4847 struct ocfs2_extent_list *rightmost_el;
4848 struct ocfs2_extent_rec rec;
4849 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4850 struct ocfs2_insert_type insert;
4851 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004852
4853leftright:
4854 /*
4855 * Store a copy of the record on the stack - it might move
4856 * around as the tree is manipulated below.
4857 */
4858 rec = path_leaf_el(path)->l_recs[split_index];
4859
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004860 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004861
4862 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4863 if (depth) {
4864 BUG_ON(!(*last_eb_bh));
4865 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4866 rightmost_el = &eb->h_list;
4867 }
4868
4869 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4870 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004871 ret = ocfs2_grow_tree(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08004872 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004873 if (ret) {
4874 mlog_errno(ret);
4875 goto out;
4876 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004877 }
4878
4879 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4880 insert.ins_appending = APPEND_NONE;
4881 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004882 insert.ins_tree_depth = depth;
4883
4884 insert_range = le32_to_cpu(split_rec.e_cpos) +
4885 le16_to_cpu(split_rec.e_leaf_clusters);
4886 rec_range = le32_to_cpu(rec.e_cpos) +
4887 le16_to_cpu(rec.e_leaf_clusters);
4888
4889 if (split_rec.e_cpos == rec.e_cpos) {
4890 insert.ins_split = SPLIT_LEFT;
4891 } else if (insert_range == rec_range) {
4892 insert.ins_split = SPLIT_RIGHT;
4893 } else {
4894 /*
4895 * Left/right split. We fake this as a right split
4896 * first and then make a second pass as a left split.
4897 */
4898 insert.ins_split = SPLIT_RIGHT;
4899
4900 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4901 &rec);
4902
4903 split_rec = tmprec;
4904
4905 BUG_ON(do_leftright);
4906 do_leftright = 1;
4907 }
4908
Tao Mae7d4cb62008-08-18 17:38:44 +08004909 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004910 if (ret) {
4911 mlog_errno(ret);
4912 goto out;
4913 }
4914
4915 if (do_leftright == 1) {
4916 u32 cpos;
4917 struct ocfs2_extent_list *el;
4918
4919 do_leftright++;
4920 split_rec = *orig_split_rec;
4921
4922 ocfs2_reinit_path(path, 1);
4923
4924 cpos = le32_to_cpu(split_rec.e_cpos);
Joel Beckerfacdb772009-02-12 18:08:48 -08004925 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07004926 if (ret) {
4927 mlog_errno(ret);
4928 goto out;
4929 }
4930
4931 el = path_leaf_el(path);
4932 split_index = ocfs2_search_extent_list(el, cpos);
4933 goto leftright;
4934 }
4935out:
4936
4937 return ret;
4938}
4939
Tao Ma47be12e2009-01-09 07:32:48 +08004940static int ocfs2_replace_extent_rec(struct inode *inode,
4941 handle_t *handle,
4942 struct ocfs2_path *path,
4943 struct ocfs2_extent_list *el,
4944 int split_index,
4945 struct ocfs2_extent_rec *split_rec)
4946{
4947 int ret;
4948
Joel Becker0cf2f762009-02-12 16:41:25 -08004949 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path,
Tao Ma47be12e2009-01-09 07:32:48 +08004950 path_num_items(path) - 1);
4951 if (ret) {
4952 mlog_errno(ret);
4953 goto out;
4954 }
4955
4956 el->l_recs[split_index] = *split_rec;
4957
4958 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4959out:
4960 return ret;
4961}
4962
Mark Fasheh328d5752007-06-18 10:48:04 -07004963/*
4964 * Mark part or all of the extent record at split_index in the leaf
4965 * pointed to by path as written. This removes the unwritten
4966 * extent flag.
4967 *
4968 * Care is taken to handle contiguousness so as to not grow the tree.
4969 *
4970 * meta_ac is not strictly necessary - we only truly need it if growth
4971 * of the tree is required. All other cases will degrade into a less
4972 * optimal tree layout.
4973 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004974 * last_eb_bh should be the rightmost leaf block for any extent
4975 * btree. Since a split may grow the tree or a merge might shrink it,
4976 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07004977 *
4978 * This code is optimized for readability - several passes might be
4979 * made over certain portions of the tree. All of those blocks will
4980 * have been brought into cache (and pinned via the journal), so the
4981 * extra overhead is not expressed in terms of disk reads.
4982 */
4983static int __ocfs2_mark_extent_written(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004984 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004985 handle_t *handle,
4986 struct ocfs2_path *path,
4987 int split_index,
4988 struct ocfs2_extent_rec *split_rec,
4989 struct ocfs2_alloc_context *meta_ac,
4990 struct ocfs2_cached_dealloc_ctxt *dealloc)
4991{
4992 int ret = 0;
4993 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004994 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07004995 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4996 struct ocfs2_merge_ctxt ctxt;
4997 struct ocfs2_extent_list *rightmost_el;
4998
Roel Kluin3cf0c502007-10-27 00:20:36 +02004999 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07005000 ret = -EIO;
5001 mlog_errno(ret);
5002 goto out;
5003 }
5004
5005 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5006 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5007 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5008 ret = -EIO;
5009 mlog_errno(ret);
5010 goto out;
5011 }
5012
Tao Maad5a4d72008-01-30 14:21:32 +08005013 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005014 split_index,
5015 split_rec);
5016
5017 /*
5018 * The core merge / split code wants to know how much room is
5019 * left in this inodes allocation tree, so we pass the
5020 * rightmost extent list.
5021 */
5022 if (path->p_tree_depth) {
5023 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005024
Joel Becker3d03a302009-02-12 17:49:26 -08005025 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005026 ocfs2_et_get_last_eb_blk(et),
5027 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005028 if (ret) {
5029 mlog_exit(ret);
5030 goto out;
5031 }
5032
5033 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005034 rightmost_el = &eb->h_list;
5035 } else
5036 rightmost_el = path_root_el(path);
5037
Mark Fasheh328d5752007-06-18 10:48:04 -07005038 if (rec->e_cpos == split_rec->e_cpos &&
5039 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5040 ctxt.c_split_covers_rec = 1;
5041 else
5042 ctxt.c_split_covers_rec = 0;
5043
5044 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5045
Mark Fasheh015452b2007-09-12 10:21:22 -07005046 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5047 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5048 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005049
5050 if (ctxt.c_contig_type == CONTIG_NONE) {
5051 if (ctxt.c_split_covers_rec)
Tao Ma47be12e2009-01-09 07:32:48 +08005052 ret = ocfs2_replace_extent_rec(inode, handle,
5053 path, el,
5054 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005055 else
Tao Mae7d4cb62008-08-18 17:38:44 +08005056 ret = ocfs2_split_and_insert(inode, handle, path, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005057 &last_eb_bh, split_index,
5058 split_rec, meta_ac);
5059 if (ret)
5060 mlog_errno(ret);
5061 } else {
Joel Beckerc495dd22009-02-13 02:19:11 -08005062 ret = ocfs2_try_to_merge_extent(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005063 split_index, split_rec,
Joel Beckerc495dd22009-02-13 02:19:11 -08005064 dealloc, &ctxt);
Mark Fasheh328d5752007-06-18 10:48:04 -07005065 if (ret)
5066 mlog_errno(ret);
5067 }
5068
Mark Fasheh328d5752007-06-18 10:48:04 -07005069out:
5070 brelse(last_eb_bh);
5071 return ret;
5072}
5073
5074/*
5075 * Mark the already-existing extent at cpos as written for len clusters.
5076 *
5077 * If the existing extent is larger than the request, initiate a
5078 * split. An attempt will be made at merging with adjacent extents.
5079 *
5080 * The caller is responsible for passing down meta_ac if we'll need it.
5081 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005082int ocfs2_mark_extent_written(struct inode *inode,
5083 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005084 handle_t *handle, u32 cpos, u32 len, u32 phys,
5085 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005086 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005087{
5088 int ret, index;
5089 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
5090 struct ocfs2_extent_rec split_rec;
5091 struct ocfs2_path *left_path = NULL;
5092 struct ocfs2_extent_list *el;
5093
5094 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5095 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
5096
5097 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5098 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5099 "that are being written to, but the feature bit "
5100 "is not set in the super block.",
5101 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5102 ret = -EROFS;
5103 goto out;
5104 }
5105
5106 /*
5107 * XXX: This should be fixed up so that we just re-insert the
5108 * next extent records.
Joel Beckerf99b9b72008-08-20 19:36:33 -07005109 *
5110 * XXX: This is a hack on the extent tree, maybe it should be
5111 * an op?
Mark Fasheh328d5752007-06-18 10:48:04 -07005112 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005113 if (et->et_ops == &ocfs2_dinode_et_ops)
Tao Mae7d4cb62008-08-18 17:38:44 +08005114 ocfs2_extent_map_trunc(inode, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07005115
Joel Beckerffdd7a52008-10-17 22:32:01 -07005116 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005117 if (!left_path) {
5118 ret = -ENOMEM;
5119 mlog_errno(ret);
5120 goto out;
5121 }
5122
Joel Beckerfacdb772009-02-12 18:08:48 -08005123 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005124 if (ret) {
5125 mlog_errno(ret);
5126 goto out;
5127 }
5128 el = path_leaf_el(left_path);
5129
5130 index = ocfs2_search_extent_list(el, cpos);
5131 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5132 ocfs2_error(inode->i_sb,
5133 "Inode %llu has an extent at cpos %u which can no "
5134 "longer be found.\n",
5135 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5136 ret = -EROFS;
5137 goto out;
5138 }
5139
5140 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5141 split_rec.e_cpos = cpu_to_le32(cpos);
5142 split_rec.e_leaf_clusters = cpu_to_le16(len);
5143 split_rec.e_blkno = cpu_to_le64(start_blkno);
5144 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
5145 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
5146
Joel Beckerf99b9b72008-08-20 19:36:33 -07005147 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
Tao Mae7d4cb62008-08-18 17:38:44 +08005148 index, &split_rec, meta_ac,
5149 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005150 if (ret)
5151 mlog_errno(ret);
5152
5153out:
5154 ocfs2_free_path(left_path);
5155 return ret;
5156}
5157
Tao Mae7d4cb62008-08-18 17:38:44 +08005158static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005159 handle_t *handle, struct ocfs2_path *path,
5160 int index, u32 new_range,
5161 struct ocfs2_alloc_context *meta_ac)
5162{
5163 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005164 struct buffer_head *last_eb_bh = NULL;
5165 struct ocfs2_extent_block *eb;
5166 struct ocfs2_extent_list *rightmost_el, *el;
5167 struct ocfs2_extent_rec split_rec;
5168 struct ocfs2_extent_rec *rec;
5169 struct ocfs2_insert_type insert;
5170
5171 /*
5172 * Setup the record to split before we grow the tree.
5173 */
5174 el = path_leaf_el(path);
5175 rec = &el->l_recs[index];
5176 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5177
5178 depth = path->p_tree_depth;
5179 if (depth > 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08005180 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005181 ocfs2_et_get_last_eb_blk(et),
5182 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005183 if (ret < 0) {
5184 mlog_errno(ret);
5185 goto out;
5186 }
5187
5188 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5189 rightmost_el = &eb->h_list;
5190 } else
5191 rightmost_el = path_leaf_el(path);
5192
Tao Ma811f9332008-08-18 17:38:43 +08005193 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005194 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005195 ret = ocfs2_extend_trans(handle, credits);
5196 if (ret) {
5197 mlog_errno(ret);
5198 goto out;
5199 }
5200
5201 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5202 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08005203 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005204 meta_ac);
5205 if (ret) {
5206 mlog_errno(ret);
5207 goto out;
5208 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005209 }
5210
5211 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5212 insert.ins_appending = APPEND_NONE;
5213 insert.ins_contig = CONTIG_NONE;
5214 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005215 insert.ins_tree_depth = depth;
5216
Tao Mae7d4cb62008-08-18 17:38:44 +08005217 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005218 if (ret)
5219 mlog_errno(ret);
5220
5221out:
5222 brelse(last_eb_bh);
5223 return ret;
5224}
5225
5226static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5227 struct ocfs2_path *path, int index,
5228 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08005229 u32 cpos, u32 len,
5230 struct ocfs2_extent_tree *et)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005231{
5232 int ret;
5233 u32 left_cpos, rec_range, trunc_range;
5234 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5235 struct super_block *sb = inode->i_sb;
5236 struct ocfs2_path *left_path = NULL;
5237 struct ocfs2_extent_list *el = path_leaf_el(path);
5238 struct ocfs2_extent_rec *rec;
5239 struct ocfs2_extent_block *eb;
5240
5241 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Joel Becker70f18c02009-02-13 02:09:31 -08005242 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005243 if (ret) {
5244 mlog_errno(ret);
5245 goto out;
5246 }
5247
5248 index--;
5249 }
5250
5251 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5252 path->p_tree_depth) {
5253 /*
5254 * Check whether this is the rightmost tree record. If
5255 * we remove all of this record or part of its right
5256 * edge then an update of the record lengths above it
5257 * will be required.
5258 */
5259 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5260 if (eb->h_next_leaf_blk == 0)
5261 is_rightmost_tree_rec = 1;
5262 }
5263
5264 rec = &el->l_recs[index];
5265 if (index == 0 && path->p_tree_depth &&
5266 le32_to_cpu(rec->e_cpos) == cpos) {
5267 /*
5268 * Changing the leftmost offset (via partial or whole
5269 * record truncate) of an interior (or rightmost) path
5270 * means we have to update the subtree that is formed
5271 * by this leaf and the one to it's left.
5272 *
5273 * There are two cases we can skip:
5274 * 1) Path is the leftmost one in our inode tree.
5275 * 2) The leaf is rightmost and will be empty after
5276 * we remove the extent record - the rotate code
5277 * knows how to update the newly formed edge.
5278 */
5279
5280 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5281 &left_cpos);
5282 if (ret) {
5283 mlog_errno(ret);
5284 goto out;
5285 }
5286
5287 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005288 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005289 if (!left_path) {
5290 ret = -ENOMEM;
5291 mlog_errno(ret);
5292 goto out;
5293 }
5294
Joel Beckerfacdb772009-02-12 18:08:48 -08005295 ret = ocfs2_find_path(et->et_ci, left_path,
5296 left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005297 if (ret) {
5298 mlog_errno(ret);
5299 goto out;
5300 }
5301 }
5302 }
5303
5304 ret = ocfs2_extend_rotate_transaction(handle, 0,
5305 handle->h_buffer_credits,
5306 path);
5307 if (ret) {
5308 mlog_errno(ret);
5309 goto out;
5310 }
5311
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005312 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005313 if (ret) {
5314 mlog_errno(ret);
5315 goto out;
5316 }
5317
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005318 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005319 if (ret) {
5320 mlog_errno(ret);
5321 goto out;
5322 }
5323
5324 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5325 trunc_range = cpos + len;
5326
5327 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5328 int next_free;
5329
5330 memset(rec, 0, sizeof(*rec));
5331 ocfs2_cleanup_merge(el, index);
5332 wants_rotate = 1;
5333
5334 next_free = le16_to_cpu(el->l_next_free_rec);
5335 if (is_rightmost_tree_rec && next_free > 1) {
5336 /*
5337 * We skip the edge update if this path will
5338 * be deleted by the rotate code.
5339 */
5340 rec = &el->l_recs[next_free - 1];
Joel Beckerd401dc12009-02-13 02:24:10 -08005341 ocfs2_adjust_rightmost_records(handle, et, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005342 rec);
5343 }
5344 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5345 /* Remove leftmost portion of the record. */
5346 le32_add_cpu(&rec->e_cpos, len);
5347 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5348 le16_add_cpu(&rec->e_leaf_clusters, -len);
5349 } else if (rec_range == trunc_range) {
5350 /* Remove rightmost portion of the record */
5351 le16_add_cpu(&rec->e_leaf_clusters, -len);
5352 if (is_rightmost_tree_rec)
Joel Beckerd401dc12009-02-13 02:24:10 -08005353 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005354 } else {
5355 /* Caller should have trapped this. */
5356 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5357 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5358 le32_to_cpu(rec->e_cpos),
5359 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5360 BUG();
5361 }
5362
5363 if (left_path) {
5364 int subtree_index;
5365
Joel Becker7dc02802009-02-12 19:20:13 -08005366 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Joel Becker4619c732009-02-12 19:02:36 -08005367 ocfs2_complete_edge_insert(handle, left_path, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005368 subtree_index);
5369 }
5370
5371 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5372
Joel Becker70f18c02009-02-13 02:09:31 -08005373 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005374 if (ret) {
5375 mlog_errno(ret);
5376 goto out;
5377 }
5378
5379out:
5380 ocfs2_free_path(left_path);
5381 return ret;
5382}
5383
Joel Beckerf99b9b72008-08-20 19:36:33 -07005384int ocfs2_remove_extent(struct inode *inode,
5385 struct ocfs2_extent_tree *et,
Mark Fasheh063c4562007-07-03 13:34:11 -07005386 u32 cpos, u32 len, handle_t *handle,
5387 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005388 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005389{
5390 int ret, index;
5391 u32 rec_range, trunc_range;
5392 struct ocfs2_extent_rec *rec;
5393 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005394 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005395
5396 ocfs2_extent_map_trunc(inode, 0);
5397
Joel Beckerffdd7a52008-10-17 22:32:01 -07005398 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005399 if (!path) {
5400 ret = -ENOMEM;
5401 mlog_errno(ret);
5402 goto out;
5403 }
5404
Joel Beckerfacdb772009-02-12 18:08:48 -08005405 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005406 if (ret) {
5407 mlog_errno(ret);
5408 goto out;
5409 }
5410
5411 el = path_leaf_el(path);
5412 index = ocfs2_search_extent_list(el, cpos);
5413 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5414 ocfs2_error(inode->i_sb,
5415 "Inode %llu has an extent at cpos %u which can no "
5416 "longer be found.\n",
5417 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5418 ret = -EROFS;
5419 goto out;
5420 }
5421
5422 /*
5423 * We have 3 cases of extent removal:
5424 * 1) Range covers the entire extent rec
5425 * 2) Range begins or ends on one edge of the extent rec
5426 * 3) Range is in the middle of the extent rec (no shared edges)
5427 *
5428 * For case 1 we remove the extent rec and left rotate to
5429 * fill the hole.
5430 *
5431 * For case 2 we just shrink the existing extent rec, with a
5432 * tree update if the shrinking edge is also the edge of an
5433 * extent block.
5434 *
5435 * For case 3 we do a right split to turn the extent rec into
5436 * something case 2 can handle.
5437 */
5438 rec = &el->l_recs[index];
5439 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5440 trunc_range = cpos + len;
5441
5442 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5443
5444 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5445 "(cpos %u, len %u)\n",
5446 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5447 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5448
5449 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5450 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005451 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005452 if (ret) {
5453 mlog_errno(ret);
5454 goto out;
5455 }
5456 } else {
Joel Beckerf99b9b72008-08-20 19:36:33 -07005457 ret = ocfs2_split_tree(inode, et, handle, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005458 trunc_range, meta_ac);
5459 if (ret) {
5460 mlog_errno(ret);
5461 goto out;
5462 }
5463
5464 /*
5465 * The split could have manipulated the tree enough to
5466 * move the record location, so we have to look for it again.
5467 */
5468 ocfs2_reinit_path(path, 1);
5469
Joel Beckerfacdb772009-02-12 18:08:48 -08005470 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005471 if (ret) {
5472 mlog_errno(ret);
5473 goto out;
5474 }
5475
5476 el = path_leaf_el(path);
5477 index = ocfs2_search_extent_list(el, cpos);
5478 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5479 ocfs2_error(inode->i_sb,
5480 "Inode %llu: split at cpos %u lost record.",
5481 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5482 cpos);
5483 ret = -EROFS;
5484 goto out;
5485 }
5486
5487 /*
5488 * Double check our values here. If anything is fishy,
5489 * it's easier to catch it at the top level.
5490 */
5491 rec = &el->l_recs[index];
5492 rec_range = le32_to_cpu(rec->e_cpos) +
5493 ocfs2_rec_clusters(el, rec);
5494 if (rec_range != trunc_range) {
5495 ocfs2_error(inode->i_sb,
5496 "Inode %llu: error after split at cpos %u"
5497 "trunc len %u, existing record is (%u,%u)",
5498 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5499 cpos, len, le32_to_cpu(rec->e_cpos),
5500 ocfs2_rec_clusters(el, rec));
5501 ret = -EROFS;
5502 goto out;
5503 }
5504
5505 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005506 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005507 if (ret) {
5508 mlog_errno(ret);
5509 goto out;
5510 }
5511 }
5512
5513out:
5514 ocfs2_free_path(path);
5515 return ret;
5516}
5517
Mark Fashehfecc0112008-11-12 15:16:38 -08005518int ocfs2_remove_btree_range(struct inode *inode,
5519 struct ocfs2_extent_tree *et,
5520 u32 cpos, u32 phys_cpos, u32 len,
5521 struct ocfs2_cached_dealloc_ctxt *dealloc)
5522{
5523 int ret;
5524 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5525 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5526 struct inode *tl_inode = osb->osb_tl_inode;
5527 handle_t *handle;
5528 struct ocfs2_alloc_context *meta_ac = NULL;
5529
5530 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5531 if (ret) {
5532 mlog_errno(ret);
5533 return ret;
5534 }
5535
5536 mutex_lock(&tl_inode->i_mutex);
5537
5538 if (ocfs2_truncate_log_needs_flush(osb)) {
5539 ret = __ocfs2_flush_truncate_log(osb);
5540 if (ret < 0) {
5541 mlog_errno(ret);
5542 goto out;
5543 }
5544 }
5545
Jan Karaa90714c2008-10-09 19:38:40 +02005546 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005547 if (IS_ERR(handle)) {
5548 ret = PTR_ERR(handle);
5549 mlog_errno(ret);
5550 goto out;
5551 }
5552
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005553 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07005554 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005555 if (ret) {
5556 mlog_errno(ret);
5557 goto out;
5558 }
5559
Mark Fashehfd4ef232009-01-29 15:06:21 -08005560 vfs_dq_free_space_nodirty(inode,
5561 ocfs2_clusters_to_bytes(inode->i_sb, len));
5562
Mark Fashehfecc0112008-11-12 15:16:38 -08005563 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5564 dealloc);
5565 if (ret) {
5566 mlog_errno(ret);
5567 goto out_commit;
5568 }
5569
Joel Becker6136ca52009-02-12 19:32:43 -08005570 ocfs2_et_update_clusters(et, -len);
Mark Fashehfecc0112008-11-12 15:16:38 -08005571
5572 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5573 if (ret) {
5574 mlog_errno(ret);
5575 goto out_commit;
5576 }
5577
5578 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5579 if (ret)
5580 mlog_errno(ret);
5581
5582out_commit:
5583 ocfs2_commit_trans(osb, handle);
5584out:
5585 mutex_unlock(&tl_inode->i_mutex);
5586
5587 if (meta_ac)
5588 ocfs2_free_alloc_context(meta_ac);
5589
5590 return ret;
5591}
5592
Mark Fasheh063c4562007-07-03 13:34:11 -07005593int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005594{
5595 struct buffer_head *tl_bh = osb->osb_tl_bh;
5596 struct ocfs2_dinode *di;
5597 struct ocfs2_truncate_log *tl;
5598
5599 di = (struct ocfs2_dinode *) tl_bh->b_data;
5600 tl = &di->id2.i_dealloc;
5601
5602 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5603 "slot %d, invalid truncate log parameters: used = "
5604 "%u, count = %u\n", osb->slot_num,
5605 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5606 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5607}
5608
5609static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5610 unsigned int new_start)
5611{
5612 unsigned int tail_index;
5613 unsigned int current_tail;
5614
5615 /* No records, nothing to coalesce */
5616 if (!le16_to_cpu(tl->tl_used))
5617 return 0;
5618
5619 tail_index = le16_to_cpu(tl->tl_used) - 1;
5620 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5621 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5622
5623 return current_tail == new_start;
5624}
5625
Mark Fasheh063c4562007-07-03 13:34:11 -07005626int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5627 handle_t *handle,
5628 u64 start_blk,
5629 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005630{
5631 int status, index;
5632 unsigned int start_cluster, tl_count;
5633 struct inode *tl_inode = osb->osb_tl_inode;
5634 struct buffer_head *tl_bh = osb->osb_tl_bh;
5635 struct ocfs2_dinode *di;
5636 struct ocfs2_truncate_log *tl;
5637
Mark Fashehb06970532006-03-03 10:24:33 -08005638 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5639 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005640
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005641 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005642
5643 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5644
5645 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005646
Joel Becker10995aa2008-11-13 14:49:12 -08005647 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5648 * by the underlying call to ocfs2_read_inode_block(), so any
5649 * corruption is a code bug */
5650 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5651
5652 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005653 tl_count = le16_to_cpu(tl->tl_count);
5654 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5655 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005656 "Truncate record count on #%llu invalid "
5657 "wanted %u, actual %u\n",
5658 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005659 ocfs2_truncate_recs_per_inode(osb->sb),
5660 le16_to_cpu(tl->tl_count));
5661
5662 /* Caller should have known to flush before calling us. */
5663 index = le16_to_cpu(tl->tl_used);
5664 if (index >= tl_count) {
5665 status = -ENOSPC;
5666 mlog_errno(status);
5667 goto bail;
5668 }
5669
Joel Becker0cf2f762009-02-12 16:41:25 -08005670 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005671 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005672 if (status < 0) {
5673 mlog_errno(status);
5674 goto bail;
5675 }
5676
5677 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005678 "%llu (index = %d)\n", num_clusters, start_cluster,
5679 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005680
5681 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5682 /*
5683 * Move index back to the record we are coalescing with.
5684 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5685 */
5686 index--;
5687
5688 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5689 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5690 index, le32_to_cpu(tl->tl_recs[index].t_start),
5691 num_clusters);
5692 } else {
5693 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5694 tl->tl_used = cpu_to_le16(index + 1);
5695 }
5696 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5697
5698 status = ocfs2_journal_dirty(handle, tl_bh);
5699 if (status < 0) {
5700 mlog_errno(status);
5701 goto bail;
5702 }
5703
5704bail:
5705 mlog_exit(status);
5706 return status;
5707}
5708
5709static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005710 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005711 struct inode *data_alloc_inode,
5712 struct buffer_head *data_alloc_bh)
5713{
5714 int status = 0;
5715 int i;
5716 unsigned int num_clusters;
5717 u64 start_blk;
5718 struct ocfs2_truncate_rec rec;
5719 struct ocfs2_dinode *di;
5720 struct ocfs2_truncate_log *tl;
5721 struct inode *tl_inode = osb->osb_tl_inode;
5722 struct buffer_head *tl_bh = osb->osb_tl_bh;
5723
5724 mlog_entry_void();
5725
5726 di = (struct ocfs2_dinode *) tl_bh->b_data;
5727 tl = &di->id2.i_dealloc;
5728 i = le16_to_cpu(tl->tl_used) - 1;
5729 while (i >= 0) {
5730 /* Caller has given us at least enough credits to
5731 * update the truncate log dinode */
Joel Becker0cf2f762009-02-12 16:41:25 -08005732 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005733 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005734 if (status < 0) {
5735 mlog_errno(status);
5736 goto bail;
5737 }
5738
5739 tl->tl_used = cpu_to_le16(i);
5740
5741 status = ocfs2_journal_dirty(handle, tl_bh);
5742 if (status < 0) {
5743 mlog_errno(status);
5744 goto bail;
5745 }
5746
5747 /* TODO: Perhaps we can calculate the bulk of the
5748 * credits up front rather than extending like
5749 * this. */
5750 status = ocfs2_extend_trans(handle,
5751 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5752 if (status < 0) {
5753 mlog_errno(status);
5754 goto bail;
5755 }
5756
5757 rec = tl->tl_recs[i];
5758 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5759 le32_to_cpu(rec.t_start));
5760 num_clusters = le32_to_cpu(rec.t_clusters);
5761
5762 /* if start_blk is not set, we ignore the record as
5763 * invalid. */
5764 if (start_blk) {
5765 mlog(0, "free record %d, start = %u, clusters = %u\n",
5766 i, le32_to_cpu(rec.t_start), num_clusters);
5767
5768 status = ocfs2_free_clusters(handle, data_alloc_inode,
5769 data_alloc_bh, start_blk,
5770 num_clusters);
5771 if (status < 0) {
5772 mlog_errno(status);
5773 goto bail;
5774 }
5775 }
5776 i--;
5777 }
5778
5779bail:
5780 mlog_exit(status);
5781 return status;
5782}
5783
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005784/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005785int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005786{
5787 int status;
5788 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005789 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005790 struct inode *tl_inode = osb->osb_tl_inode;
5791 struct inode *data_alloc_inode = NULL;
5792 struct buffer_head *tl_bh = osb->osb_tl_bh;
5793 struct buffer_head *data_alloc_bh = NULL;
5794 struct ocfs2_dinode *di;
5795 struct ocfs2_truncate_log *tl;
5796
5797 mlog_entry_void();
5798
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005799 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005800
5801 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005802
Joel Becker10995aa2008-11-13 14:49:12 -08005803 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5804 * by the underlying call to ocfs2_read_inode_block(), so any
5805 * corruption is a code bug */
5806 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5807
5808 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005809 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005810 mlog(0, "Flush %u records from truncate log #%llu\n",
5811 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005812 if (!num_to_flush) {
5813 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005814 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005815 }
5816
5817 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5818 GLOBAL_BITMAP_SYSTEM_INODE,
5819 OCFS2_INVALID_SLOT);
5820 if (!data_alloc_inode) {
5821 status = -EINVAL;
5822 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005823 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005824 }
5825
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005826 mutex_lock(&data_alloc_inode->i_mutex);
5827
Mark Fashehe63aecb62007-10-18 15:30:42 -07005828 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005829 if (status < 0) {
5830 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005831 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005832 }
5833
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005834 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005835 if (IS_ERR(handle)) {
5836 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005837 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005838 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005839 }
5840
5841 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5842 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005843 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005844 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005845
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005846 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005847
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005848out_unlock:
5849 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005850 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005851
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005852out_mutex:
5853 mutex_unlock(&data_alloc_inode->i_mutex);
5854 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005855
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005856out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005857 mlog_exit(status);
5858 return status;
5859}
5860
5861int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5862{
5863 int status;
5864 struct inode *tl_inode = osb->osb_tl_inode;
5865
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005866 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005867 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005868 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005869
5870 return status;
5871}
5872
David Howellsc4028952006-11-22 14:57:56 +00005873static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005874{
5875 int status;
David Howellsc4028952006-11-22 14:57:56 +00005876 struct ocfs2_super *osb =
5877 container_of(work, struct ocfs2_super,
5878 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005879
5880 mlog_entry_void();
5881
5882 status = ocfs2_flush_truncate_log(osb);
5883 if (status < 0)
5884 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005885 else
5886 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005887
5888 mlog_exit(status);
5889}
5890
5891#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5892void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5893 int cancel)
5894{
5895 if (osb->osb_tl_inode) {
5896 /* We want to push off log flushes while truncates are
5897 * still running. */
5898 if (cancel)
5899 cancel_delayed_work(&osb->osb_truncate_log_wq);
5900
5901 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5902 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5903 }
5904}
5905
5906static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5907 int slot_num,
5908 struct inode **tl_inode,
5909 struct buffer_head **tl_bh)
5910{
5911 int status;
5912 struct inode *inode = NULL;
5913 struct buffer_head *bh = NULL;
5914
5915 inode = ocfs2_get_system_file_inode(osb,
5916 TRUNCATE_LOG_SYSTEM_INODE,
5917 slot_num);
5918 if (!inode) {
5919 status = -EINVAL;
5920 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5921 goto bail;
5922 }
5923
Joel Beckerb657c952008-11-13 14:49:11 -08005924 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005925 if (status < 0) {
5926 iput(inode);
5927 mlog_errno(status);
5928 goto bail;
5929 }
5930
5931 *tl_inode = inode;
5932 *tl_bh = bh;
5933bail:
5934 mlog_exit(status);
5935 return status;
5936}
5937
5938/* called during the 1st stage of node recovery. we stamp a clean
5939 * truncate log and pass back a copy for processing later. if the
5940 * truncate log does not require processing, a *tl_copy is set to
5941 * NULL. */
5942int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5943 int slot_num,
5944 struct ocfs2_dinode **tl_copy)
5945{
5946 int status;
5947 struct inode *tl_inode = NULL;
5948 struct buffer_head *tl_bh = NULL;
5949 struct ocfs2_dinode *di;
5950 struct ocfs2_truncate_log *tl;
5951
5952 *tl_copy = NULL;
5953
5954 mlog(0, "recover truncate log from slot %d\n", slot_num);
5955
5956 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5957 if (status < 0) {
5958 mlog_errno(status);
5959 goto bail;
5960 }
5961
5962 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005963
Joel Becker10995aa2008-11-13 14:49:12 -08005964 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5965 * validated by the underlying call to ocfs2_read_inode_block(),
5966 * so any corruption is a code bug */
5967 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5968
5969 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005970 if (le16_to_cpu(tl->tl_used)) {
5971 mlog(0, "We'll have %u logs to recover\n",
5972 le16_to_cpu(tl->tl_used));
5973
5974 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5975 if (!(*tl_copy)) {
5976 status = -ENOMEM;
5977 mlog_errno(status);
5978 goto bail;
5979 }
5980
5981 /* Assuming the write-out below goes well, this copy
5982 * will be passed back to recovery for processing. */
5983 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5984
5985 /* All we need to do to clear the truncate log is set
5986 * tl_used. */
5987 tl->tl_used = 0;
5988
Joel Becker13723d02008-10-17 19:25:01 -07005989 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08005990 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08005991 if (status < 0) {
5992 mlog_errno(status);
5993 goto bail;
5994 }
5995 }
5996
5997bail:
5998 if (tl_inode)
5999 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006000 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006001
6002 if (status < 0 && (*tl_copy)) {
6003 kfree(*tl_copy);
6004 *tl_copy = NULL;
6005 }
6006
6007 mlog_exit(status);
6008 return status;
6009}
6010
6011int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6012 struct ocfs2_dinode *tl_copy)
6013{
6014 int status = 0;
6015 int i;
6016 unsigned int clusters, num_recs, start_cluster;
6017 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006018 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006019 struct inode *tl_inode = osb->osb_tl_inode;
6020 struct ocfs2_truncate_log *tl;
6021
6022 mlog_entry_void();
6023
6024 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6025 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6026 return -EINVAL;
6027 }
6028
6029 tl = &tl_copy->id2.i_dealloc;
6030 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08006031 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006032 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006033
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006034 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006035 for(i = 0; i < num_recs; i++) {
6036 if (ocfs2_truncate_log_needs_flush(osb)) {
6037 status = __ocfs2_flush_truncate_log(osb);
6038 if (status < 0) {
6039 mlog_errno(status);
6040 goto bail_up;
6041 }
6042 }
6043
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006044 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006045 if (IS_ERR(handle)) {
6046 status = PTR_ERR(handle);
6047 mlog_errno(status);
6048 goto bail_up;
6049 }
6050
6051 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6052 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6053 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6054
6055 status = ocfs2_truncate_log_append(osb, handle,
6056 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006057 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006058 if (status < 0) {
6059 mlog_errno(status);
6060 goto bail_up;
6061 }
6062 }
6063
6064bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006065 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006066
6067 mlog_exit(status);
6068 return status;
6069}
6070
6071void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6072{
6073 int status;
6074 struct inode *tl_inode = osb->osb_tl_inode;
6075
6076 mlog_entry_void();
6077
6078 if (tl_inode) {
6079 cancel_delayed_work(&osb->osb_truncate_log_wq);
6080 flush_workqueue(ocfs2_wq);
6081
6082 status = ocfs2_flush_truncate_log(osb);
6083 if (status < 0)
6084 mlog_errno(status);
6085
6086 brelse(osb->osb_tl_bh);
6087 iput(osb->osb_tl_inode);
6088 }
6089
6090 mlog_exit_void();
6091}
6092
6093int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6094{
6095 int status;
6096 struct inode *tl_inode = NULL;
6097 struct buffer_head *tl_bh = NULL;
6098
6099 mlog_entry_void();
6100
6101 status = ocfs2_get_truncate_log_info(osb,
6102 osb->slot_num,
6103 &tl_inode,
6104 &tl_bh);
6105 if (status < 0)
6106 mlog_errno(status);
6107
6108 /* ocfs2_truncate_log_shutdown keys on the existence of
6109 * osb->osb_tl_inode so we don't set any of the osb variables
6110 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006111 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6112 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006113 osb->osb_tl_bh = tl_bh;
6114 osb->osb_tl_inode = tl_inode;
6115
6116 mlog_exit(status);
6117 return status;
6118}
6119
Mark Fasheh2b604352007-06-22 15:45:27 -07006120/*
6121 * Delayed de-allocation of suballocator blocks.
6122 *
6123 * Some sets of block de-allocations might involve multiple suballocator inodes.
6124 *
6125 * The locking for this can get extremely complicated, especially when
6126 * the suballocator inodes to delete from aren't known until deep
6127 * within an unrelated codepath.
6128 *
6129 * ocfs2_extent_block structures are a good example of this - an inode
6130 * btree could have been grown by any number of nodes each allocating
6131 * out of their own suballoc inode.
6132 *
6133 * These structures allow the delay of block de-allocation until a
6134 * later time, when locking of multiple cluster inodes won't cause
6135 * deadlock.
6136 */
6137
6138/*
Tao Ma2891d292008-11-12 08:26:58 +08006139 * Describe a single bit freed from a suballocator. For the block
6140 * suballocators, it represents one block. For the global cluster
6141 * allocator, it represents some clusters and free_bit indicates
6142 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006143 */
6144struct ocfs2_cached_block_free {
6145 struct ocfs2_cached_block_free *free_next;
6146 u64 free_blk;
6147 unsigned int free_bit;
6148};
6149
6150struct ocfs2_per_slot_free_list {
6151 struct ocfs2_per_slot_free_list *f_next_suballocator;
6152 int f_inode_type;
6153 int f_slot;
6154 struct ocfs2_cached_block_free *f_first;
6155};
6156
Tao Ma2891d292008-11-12 08:26:58 +08006157static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6158 int sysfile_type,
6159 int slot,
6160 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006161{
6162 int ret;
6163 u64 bg_blkno;
6164 handle_t *handle;
6165 struct inode *inode;
6166 struct buffer_head *di_bh = NULL;
6167 struct ocfs2_cached_block_free *tmp;
6168
6169 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6170 if (!inode) {
6171 ret = -EINVAL;
6172 mlog_errno(ret);
6173 goto out;
6174 }
6175
6176 mutex_lock(&inode->i_mutex);
6177
Mark Fashehe63aecb62007-10-18 15:30:42 -07006178 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006179 if (ret) {
6180 mlog_errno(ret);
6181 goto out_mutex;
6182 }
6183
6184 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6185 if (IS_ERR(handle)) {
6186 ret = PTR_ERR(handle);
6187 mlog_errno(ret);
6188 goto out_unlock;
6189 }
6190
6191 while (head) {
6192 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6193 head->free_bit);
6194 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6195 head->free_bit, (unsigned long long)head->free_blk);
6196
6197 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6198 head->free_bit, bg_blkno, 1);
6199 if (ret) {
6200 mlog_errno(ret);
6201 goto out_journal;
6202 }
6203
6204 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6205 if (ret) {
6206 mlog_errno(ret);
6207 goto out_journal;
6208 }
6209
6210 tmp = head;
6211 head = head->free_next;
6212 kfree(tmp);
6213 }
6214
6215out_journal:
6216 ocfs2_commit_trans(osb, handle);
6217
6218out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006219 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006220 brelse(di_bh);
6221out_mutex:
6222 mutex_unlock(&inode->i_mutex);
6223 iput(inode);
6224out:
6225 while(head) {
6226 /* Premature exit may have left some dangling items. */
6227 tmp = head;
6228 head = head->free_next;
6229 kfree(tmp);
6230 }
6231
6232 return ret;
6233}
6234
Tao Ma2891d292008-11-12 08:26:58 +08006235int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6236 u64 blkno, unsigned int bit)
6237{
6238 int ret = 0;
6239 struct ocfs2_cached_block_free *item;
6240
6241 item = kmalloc(sizeof(*item), GFP_NOFS);
6242 if (item == NULL) {
6243 ret = -ENOMEM;
6244 mlog_errno(ret);
6245 return ret;
6246 }
6247
6248 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6249 bit, (unsigned long long)blkno);
6250
6251 item->free_blk = blkno;
6252 item->free_bit = bit;
6253 item->free_next = ctxt->c_global_allocator;
6254
6255 ctxt->c_global_allocator = item;
6256 return ret;
6257}
6258
6259static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6260 struct ocfs2_cached_block_free *head)
6261{
6262 struct ocfs2_cached_block_free *tmp;
6263 struct inode *tl_inode = osb->osb_tl_inode;
6264 handle_t *handle;
6265 int ret = 0;
6266
6267 mutex_lock(&tl_inode->i_mutex);
6268
6269 while (head) {
6270 if (ocfs2_truncate_log_needs_flush(osb)) {
6271 ret = __ocfs2_flush_truncate_log(osb);
6272 if (ret < 0) {
6273 mlog_errno(ret);
6274 break;
6275 }
6276 }
6277
6278 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6279 if (IS_ERR(handle)) {
6280 ret = PTR_ERR(handle);
6281 mlog_errno(ret);
6282 break;
6283 }
6284
6285 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6286 head->free_bit);
6287
6288 ocfs2_commit_trans(osb, handle);
6289 tmp = head;
6290 head = head->free_next;
6291 kfree(tmp);
6292
6293 if (ret < 0) {
6294 mlog_errno(ret);
6295 break;
6296 }
6297 }
6298
6299 mutex_unlock(&tl_inode->i_mutex);
6300
6301 while (head) {
6302 /* Premature exit may have left some dangling items. */
6303 tmp = head;
6304 head = head->free_next;
6305 kfree(tmp);
6306 }
6307
6308 return ret;
6309}
6310
Mark Fasheh2b604352007-06-22 15:45:27 -07006311int ocfs2_run_deallocs(struct ocfs2_super *osb,
6312 struct ocfs2_cached_dealloc_ctxt *ctxt)
6313{
6314 int ret = 0, ret2;
6315 struct ocfs2_per_slot_free_list *fl;
6316
6317 if (!ctxt)
6318 return 0;
6319
6320 while (ctxt->c_first_suballocator) {
6321 fl = ctxt->c_first_suballocator;
6322
6323 if (fl->f_first) {
6324 mlog(0, "Free items: (type %u, slot %d)\n",
6325 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006326 ret2 = ocfs2_free_cached_blocks(osb,
6327 fl->f_inode_type,
6328 fl->f_slot,
6329 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006330 if (ret2)
6331 mlog_errno(ret2);
6332 if (!ret)
6333 ret = ret2;
6334 }
6335
6336 ctxt->c_first_suballocator = fl->f_next_suballocator;
6337 kfree(fl);
6338 }
6339
Tao Ma2891d292008-11-12 08:26:58 +08006340 if (ctxt->c_global_allocator) {
6341 ret2 = ocfs2_free_cached_clusters(osb,
6342 ctxt->c_global_allocator);
6343 if (ret2)
6344 mlog_errno(ret2);
6345 if (!ret)
6346 ret = ret2;
6347
6348 ctxt->c_global_allocator = NULL;
6349 }
6350
Mark Fasheh2b604352007-06-22 15:45:27 -07006351 return ret;
6352}
6353
6354static struct ocfs2_per_slot_free_list *
6355ocfs2_find_per_slot_free_list(int type,
6356 int slot,
6357 struct ocfs2_cached_dealloc_ctxt *ctxt)
6358{
6359 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6360
6361 while (fl) {
6362 if (fl->f_inode_type == type && fl->f_slot == slot)
6363 return fl;
6364
6365 fl = fl->f_next_suballocator;
6366 }
6367
6368 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6369 if (fl) {
6370 fl->f_inode_type = type;
6371 fl->f_slot = slot;
6372 fl->f_first = NULL;
6373 fl->f_next_suballocator = ctxt->c_first_suballocator;
6374
6375 ctxt->c_first_suballocator = fl;
6376 }
6377 return fl;
6378}
6379
6380static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6381 int type, int slot, u64 blkno,
6382 unsigned int bit)
6383{
6384 int ret;
6385 struct ocfs2_per_slot_free_list *fl;
6386 struct ocfs2_cached_block_free *item;
6387
6388 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6389 if (fl == NULL) {
6390 ret = -ENOMEM;
6391 mlog_errno(ret);
6392 goto out;
6393 }
6394
6395 item = kmalloc(sizeof(*item), GFP_NOFS);
6396 if (item == NULL) {
6397 ret = -ENOMEM;
6398 mlog_errno(ret);
6399 goto out;
6400 }
6401
6402 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6403 type, slot, bit, (unsigned long long)blkno);
6404
6405 item->free_blk = blkno;
6406 item->free_bit = bit;
6407 item->free_next = fl->f_first;
6408
6409 fl->f_first = item;
6410
6411 ret = 0;
6412out:
6413 return ret;
6414}
6415
Mark Fasheh59a5e412007-06-22 15:52:36 -07006416static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6417 struct ocfs2_extent_block *eb)
6418{
6419 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6420 le16_to_cpu(eb->h_suballoc_slot),
6421 le64_to_cpu(eb->h_blkno),
6422 le16_to_cpu(eb->h_suballoc_bit));
6423}
6424
Mark Fashehccd979b2005-12-15 14:31:24 -08006425/* This function will figure out whether the currently last extent
6426 * block will be deleted, and if it will, what the new last extent
6427 * block will be so we can update his h_next_leaf_blk field, as well
6428 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006429static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006430 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006431 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006432 struct buffer_head **new_last_eb)
6433{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006434 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006435 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006436 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006437 struct ocfs2_extent_block *eb;
6438 struct ocfs2_extent_list *el;
6439 struct buffer_head *bh = NULL;
6440
6441 *new_last_eb = NULL;
6442
Mark Fashehccd979b2005-12-15 14:31:24 -08006443 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006444 if (!path->p_tree_depth)
6445 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006446
6447 /* trunc to zero special case - this makes tree_depth = 0
6448 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006449 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006450 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006451
Mark Fashehdcd05382007-01-16 11:32:23 -08006452 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006453 BUG_ON(!el->l_next_free_rec);
6454
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006455 /*
6456 * Make sure that this extent list will actually be empty
6457 * after we clear away the data. We can shortcut out if
6458 * there's more than one non-empty extent in the
6459 * list. Otherwise, a check of the remaining extent is
6460 * necessary.
6461 */
6462 next_free = le16_to_cpu(el->l_next_free_rec);
6463 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006464 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006465 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006466 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006467
6468 /* We may have a valid extent in index 1, check it. */
6469 if (next_free == 2)
6470 rec = &el->l_recs[1];
6471
6472 /*
6473 * Fall through - no more nonempty extents, so we want
6474 * to delete this leaf.
6475 */
6476 } else {
6477 if (next_free > 1)
6478 goto out;
6479
6480 rec = &el->l_recs[0];
6481 }
6482
6483 if (rec) {
6484 /*
6485 * Check it we'll only be trimming off the end of this
6486 * cluster.
6487 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006488 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006489 goto out;
6490 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006491
Mark Fashehdcd05382007-01-16 11:32:23 -08006492 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6493 if (ret) {
6494 mlog_errno(ret);
6495 goto out;
6496 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006497
Joel Beckerfacdb772009-02-12 18:08:48 -08006498 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08006499 if (ret) {
6500 mlog_errno(ret);
6501 goto out;
6502 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006503
Mark Fashehdcd05382007-01-16 11:32:23 -08006504 eb = (struct ocfs2_extent_block *) bh->b_data;
6505 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006506
6507 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6508 * Any corruption is a code bug. */
6509 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006510
6511 *new_last_eb = bh;
6512 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006513 mlog(0, "returning block %llu, (cpos: %u)\n",
6514 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6515out:
6516 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006517
Mark Fashehdcd05382007-01-16 11:32:23 -08006518 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006519}
6520
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006521/*
6522 * Trim some clusters off the rightmost edge of a tree. Only called
6523 * during truncate.
6524 *
6525 * The caller needs to:
6526 * - start journaling of each path component.
6527 * - compute and fully set up any new last ext block
6528 */
6529static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6530 handle_t *handle, struct ocfs2_truncate_context *tc,
6531 u32 clusters_to_del, u64 *delete_start)
6532{
6533 int ret, i, index = path->p_tree_depth;
6534 u32 new_edge = 0;
6535 u64 deleted_eb = 0;
6536 struct buffer_head *bh;
6537 struct ocfs2_extent_list *el;
6538 struct ocfs2_extent_rec *rec;
6539
6540 *delete_start = 0;
6541
6542 while (index >= 0) {
6543 bh = path->p_node[index].bh;
6544 el = path->p_node[index].el;
6545
6546 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6547 index, (unsigned long long)bh->b_blocknr);
6548
6549 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6550
6551 if (index !=
6552 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6553 ocfs2_error(inode->i_sb,
6554 "Inode %lu has invalid ext. block %llu",
6555 inode->i_ino,
6556 (unsigned long long)bh->b_blocknr);
6557 ret = -EROFS;
6558 goto out;
6559 }
6560
6561find_tail_record:
6562 i = le16_to_cpu(el->l_next_free_rec) - 1;
6563 rec = &el->l_recs[i];
6564
6565 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6566 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006567 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006568 (unsigned long long)le64_to_cpu(rec->e_blkno),
6569 le16_to_cpu(el->l_next_free_rec));
6570
Mark Fashehe48edee2007-03-07 16:46:57 -08006571 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006572
6573 if (le16_to_cpu(el->l_tree_depth) == 0) {
6574 /*
6575 * If the leaf block contains a single empty
6576 * extent and no records, we can just remove
6577 * the block.
6578 */
6579 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6580 memset(rec, 0,
6581 sizeof(struct ocfs2_extent_rec));
6582 el->l_next_free_rec = cpu_to_le16(0);
6583
6584 goto delete;
6585 }
6586
6587 /*
6588 * Remove any empty extents by shifting things
6589 * left. That should make life much easier on
6590 * the code below. This condition is rare
6591 * enough that we shouldn't see a performance
6592 * hit.
6593 */
6594 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6595 le16_add_cpu(&el->l_next_free_rec, -1);
6596
6597 for(i = 0;
6598 i < le16_to_cpu(el->l_next_free_rec); i++)
6599 el->l_recs[i] = el->l_recs[i + 1];
6600
6601 memset(&el->l_recs[i], 0,
6602 sizeof(struct ocfs2_extent_rec));
6603
6604 /*
6605 * We've modified our extent list. The
6606 * simplest way to handle this change
6607 * is to being the search from the
6608 * start again.
6609 */
6610 goto find_tail_record;
6611 }
6612
Mark Fashehe48edee2007-03-07 16:46:57 -08006613 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006614
6615 /*
6616 * We'll use "new_edge" on our way back up the
6617 * tree to know what our rightmost cpos is.
6618 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006619 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006620 new_edge += le32_to_cpu(rec->e_cpos);
6621
6622 /*
6623 * The caller will use this to delete data blocks.
6624 */
6625 *delete_start = le64_to_cpu(rec->e_blkno)
6626 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006627 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006628
6629 /*
6630 * If it's now empty, remove this record.
6631 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006632 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006633 memset(rec, 0,
6634 sizeof(struct ocfs2_extent_rec));
6635 le16_add_cpu(&el->l_next_free_rec, -1);
6636 }
6637 } else {
6638 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6639 memset(rec, 0,
6640 sizeof(struct ocfs2_extent_rec));
6641 le16_add_cpu(&el->l_next_free_rec, -1);
6642
6643 goto delete;
6644 }
6645
6646 /* Can this actually happen? */
6647 if (le16_to_cpu(el->l_next_free_rec) == 0)
6648 goto delete;
6649
6650 /*
6651 * We never actually deleted any clusters
6652 * because our leaf was empty. There's no
6653 * reason to adjust the rightmost edge then.
6654 */
6655 if (new_edge == 0)
6656 goto delete;
6657
Mark Fashehe48edee2007-03-07 16:46:57 -08006658 rec->e_int_clusters = cpu_to_le32(new_edge);
6659 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006660 -le32_to_cpu(rec->e_cpos));
6661
6662 /*
6663 * A deleted child record should have been
6664 * caught above.
6665 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006666 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006667 }
6668
6669delete:
6670 ret = ocfs2_journal_dirty(handle, bh);
6671 if (ret) {
6672 mlog_errno(ret);
6673 goto out;
6674 }
6675
6676 mlog(0, "extent list container %llu, after: record %d: "
6677 "(%u, %u, %llu), next = %u.\n",
6678 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006679 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006680 (unsigned long long)le64_to_cpu(rec->e_blkno),
6681 le16_to_cpu(el->l_next_free_rec));
6682
6683 /*
6684 * We must be careful to only attempt delete of an
6685 * extent block (and not the root inode block).
6686 */
6687 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6688 struct ocfs2_extent_block *eb =
6689 (struct ocfs2_extent_block *)bh->b_data;
6690
6691 /*
6692 * Save this for use when processing the
6693 * parent block.
6694 */
6695 deleted_eb = le64_to_cpu(eb->h_blkno);
6696
6697 mlog(0, "deleting this extent block.\n");
6698
Joel Becker8cb471e2009-02-10 20:00:41 -08006699 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006700
Mark Fashehe48edee2007-03-07 16:46:57 -08006701 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006702 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6703 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6704
Mark Fasheh59a5e412007-06-22 15:52:36 -07006705 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6706 /* An error here is not fatal. */
6707 if (ret < 0)
6708 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006709 } else {
6710 deleted_eb = 0;
6711 }
6712
6713 index--;
6714 }
6715
6716 ret = 0;
6717out:
6718 return ret;
6719}
6720
Mark Fashehccd979b2005-12-15 14:31:24 -08006721static int ocfs2_do_truncate(struct ocfs2_super *osb,
6722 unsigned int clusters_to_del,
6723 struct inode *inode,
6724 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006725 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006726 struct ocfs2_truncate_context *tc,
6727 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006728{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006729 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006730 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006731 struct ocfs2_extent_block *last_eb = NULL;
6732 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006733 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006734 u64 delete_blk = 0;
6735
6736 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6737
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006738 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006739 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006740 if (status < 0) {
6741 mlog_errno(status);
6742 goto bail;
6743 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006744
Mark Fashehdcd05382007-01-16 11:32:23 -08006745 /*
6746 * Each component will be touched, so we might as well journal
6747 * here to avoid having to handle errors later.
6748 */
Joel Becker0cf2f762009-02-12 16:41:25 -08006749 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006750 if (status < 0) {
6751 mlog_errno(status);
6752 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006753 }
6754
6755 if (last_eb_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08006756 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07006757 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08006758 if (status < 0) {
6759 mlog_errno(status);
6760 goto bail;
6761 }
6762
6763 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6764 }
6765
6766 el = &(fe->id2.i_list);
6767
6768 /*
6769 * Lower levels depend on this never happening, but it's best
6770 * to check it up here before changing the tree.
6771 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006772 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006773 ocfs2_error(inode->i_sb,
6774 "Inode %lu has an empty extent record, depth %u\n",
6775 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006776 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006777 goto bail;
6778 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006779
Jan Karaa90714c2008-10-09 19:38:40 +02006780 vfs_dq_free_space_nodirty(inode,
6781 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006782 spin_lock(&OCFS2_I(inode)->ip_lock);
6783 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6784 clusters_to_del;
6785 spin_unlock(&OCFS2_I(inode)->ip_lock);
6786 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006787 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006788
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006789 status = ocfs2_trim_tree(inode, path, handle, tc,
6790 clusters_to_del, &delete_blk);
6791 if (status) {
6792 mlog_errno(status);
6793 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006794 }
6795
Mark Fashehdcd05382007-01-16 11:32:23 -08006796 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006797 /* trunc to zero is a special case. */
6798 el->l_tree_depth = 0;
6799 fe->i_last_eb_blk = 0;
6800 } else if (last_eb)
6801 fe->i_last_eb_blk = last_eb->h_blkno;
6802
6803 status = ocfs2_journal_dirty(handle, fe_bh);
6804 if (status < 0) {
6805 mlog_errno(status);
6806 goto bail;
6807 }
6808
6809 if (last_eb) {
6810 /* If there will be a new last extent block, then by
6811 * definition, there cannot be any leaves to the right of
6812 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006813 last_eb->h_next_leaf_blk = 0;
6814 status = ocfs2_journal_dirty(handle, last_eb_bh);
6815 if (status < 0) {
6816 mlog_errno(status);
6817 goto bail;
6818 }
6819 }
6820
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006821 if (delete_blk) {
6822 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6823 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006824 if (status < 0) {
6825 mlog_errno(status);
6826 goto bail;
6827 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006828 }
6829 status = 0;
6830bail:
Tao Ma60e2ec42009-08-12 14:42:47 +08006831 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006832 mlog_exit(status);
6833 return status;
6834}
6835
Joel Becker2b4e30f2008-09-03 20:03:41 -07006836static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006837{
6838 set_buffer_uptodate(bh);
6839 mark_buffer_dirty(bh);
6840 return 0;
6841}
6842
Mark Fasheh1d410a62007-09-07 14:20:45 -07006843static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6844 unsigned int from, unsigned int to,
6845 struct page *page, int zero, u64 *phys)
6846{
6847 int ret, partial = 0;
6848
6849 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6850 if (ret)
6851 mlog_errno(ret);
6852
6853 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006854 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006855
6856 /*
6857 * Need to set the buffers we zero'd into uptodate
6858 * here if they aren't - ocfs2_map_page_blocks()
6859 * might've skipped some
6860 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006861 ret = walk_page_buffers(handle, page_buffers(page),
6862 from, to, &partial,
6863 ocfs2_zero_func);
6864 if (ret < 0)
6865 mlog_errno(ret);
6866 else if (ocfs2_should_order_data(inode)) {
6867 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006868 if (ret < 0)
6869 mlog_errno(ret);
6870 }
6871
6872 if (!partial)
6873 SetPageUptodate(page);
6874
6875 flush_dcache_page(page);
6876}
6877
Mark Fasheh35edec12007-07-06 14:41:18 -07006878static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6879 loff_t end, struct page **pages,
6880 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006881{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006882 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006883 struct page *page;
6884 unsigned int from, to = PAGE_CACHE_SIZE;
6885 struct super_block *sb = inode->i_sb;
6886
6887 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6888
6889 if (numpages == 0)
6890 goto out;
6891
Mark Fasheh35edec12007-07-06 14:41:18 -07006892 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006893 for(i = 0; i < numpages; i++) {
6894 page = pages[i];
6895
Mark Fasheh35edec12007-07-06 14:41:18 -07006896 from = start & (PAGE_CACHE_SIZE - 1);
6897 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6898 to = end & (PAGE_CACHE_SIZE - 1);
6899
Mark Fasheh60b11392007-02-16 11:46:50 -08006900 BUG_ON(from > PAGE_CACHE_SIZE);
6901 BUG_ON(to > PAGE_CACHE_SIZE);
6902
Mark Fasheh1d410a62007-09-07 14:20:45 -07006903 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6904 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006905
Mark Fasheh35edec12007-07-06 14:41:18 -07006906 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006907 }
6908out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006909 if (pages)
6910 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006911}
6912
Mark Fasheh35edec12007-07-06 14:41:18 -07006913static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006914 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006915{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006916 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006917 struct super_block *sb = inode->i_sb;
6918 struct address_space *mapping = inode->i_mapping;
6919 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006920 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006921
Mark Fasheh35edec12007-07-06 14:41:18 -07006922 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006923
Mark Fasheh35edec12007-07-06 14:41:18 -07006924 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6925 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6926
Mark Fasheh1d410a62007-09-07 14:20:45 -07006927 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006928 last_page_bytes = PAGE_ALIGN(end);
6929 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006930 do {
6931 pages[numpages] = grab_cache_page(mapping, index);
6932 if (!pages[numpages]) {
6933 ret = -ENOMEM;
6934 mlog_errno(ret);
6935 goto out;
6936 }
6937
6938 numpages++;
6939 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006940 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006941
6942out:
6943 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006944 if (pages)
6945 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006946 numpages = 0;
6947 }
6948
6949 *num = numpages;
6950
6951 return ret;
6952}
6953
6954/*
6955 * Zero the area past i_size but still within an allocated
6956 * cluster. This avoids exposing nonzero data on subsequent file
6957 * extends.
6958 *
6959 * We need to call this before i_size is updated on the inode because
6960 * otherwise block_write_full_page() will skip writeout of pages past
6961 * i_size. The new_i_size parameter is passed for this reason.
6962 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006963int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6964 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006965{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006966 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006967 struct page **pages = NULL;
6968 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006969 unsigned int ext_flags;
6970 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006971
6972 /*
6973 * File systems which don't support sparse files zero on every
6974 * extend.
6975 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07006976 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08006977 return 0;
6978
Mark Fasheh1d410a62007-09-07 14:20:45 -07006979 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08006980 sizeof(struct page *), GFP_NOFS);
6981 if (pages == NULL) {
6982 ret = -ENOMEM;
6983 mlog_errno(ret);
6984 goto out;
6985 }
6986
Mark Fasheh1d410a62007-09-07 14:20:45 -07006987 if (range_start == range_end)
6988 goto out;
6989
6990 ret = ocfs2_extent_map_get_blocks(inode,
6991 range_start >> sb->s_blocksize_bits,
6992 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08006993 if (ret) {
6994 mlog_errno(ret);
6995 goto out;
6996 }
6997
Mark Fasheh1d410a62007-09-07 14:20:45 -07006998 /*
6999 * Tail is a hole, or is marked unwritten. In either case, we
7000 * can count on read and write to return/push zero's.
7001 */
7002 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08007003 goto out;
7004
Mark Fasheh1d410a62007-09-07 14:20:45 -07007005 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7006 &numpages);
7007 if (ret) {
7008 mlog_errno(ret);
7009 goto out;
7010 }
7011
Mark Fasheh35edec12007-07-06 14:41:18 -07007012 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7013 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08007014
7015 /*
7016 * Initiate writeout of the pages we zero'd here. We don't
7017 * wait on them - the truncate_inode_pages() call later will
7018 * do that for us.
7019 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007020 ret = do_sync_mapping_range(inode->i_mapping, range_start,
7021 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08007022 if (ret)
7023 mlog_errno(ret);
7024
7025out:
7026 if (pages)
7027 kfree(pages);
7028
7029 return ret;
7030}
7031
Tiger Yangfdd77702008-08-18 17:08:55 +08007032static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7033 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007034{
7035 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08007036 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007037
Tiger Yangfdd77702008-08-18 17:08:55 +08007038 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7039 memset(&di->id2, 0, blocksize -
7040 offsetof(struct ocfs2_dinode, id2) -
7041 xattrsize);
7042 else
7043 memset(&di->id2, 0, blocksize -
7044 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007045}
7046
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007047void ocfs2_dinode_new_extent_list(struct inode *inode,
7048 struct ocfs2_dinode *di)
7049{
Tiger Yangfdd77702008-08-18 17:08:55 +08007050 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007051 di->id2.i_list.l_tree_depth = 0;
7052 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08007053 di->id2.i_list.l_count = cpu_to_le16(
7054 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007055}
7056
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007057void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7058{
7059 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7060 struct ocfs2_inline_data *idata = &di->id2.i_data;
7061
7062 spin_lock(&oi->ip_lock);
7063 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7064 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7065 spin_unlock(&oi->ip_lock);
7066
7067 /*
7068 * We clear the entire i_data structure here so that all
7069 * fields can be properly initialized.
7070 */
Tiger Yangfdd77702008-08-18 17:08:55 +08007071 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007072
Tiger Yangfdd77702008-08-18 17:08:55 +08007073 idata->id_count = cpu_to_le16(
7074 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007075}
7076
7077int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7078 struct buffer_head *di_bh)
7079{
7080 int ret, i, has_data, num_pages = 0;
7081 handle_t *handle;
7082 u64 uninitialized_var(block);
7083 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7084 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7085 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007086 struct ocfs2_alloc_context *data_ac = NULL;
7087 struct page **pages = NULL;
7088 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07007089 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02007090 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007091
7092 has_data = i_size_read(inode) ? 1 : 0;
7093
7094 if (has_data) {
7095 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7096 sizeof(struct page *), GFP_NOFS);
7097 if (pages == NULL) {
7098 ret = -ENOMEM;
7099 mlog_errno(ret);
7100 goto out;
7101 }
7102
7103 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7104 if (ret) {
7105 mlog_errno(ret);
7106 goto out;
7107 }
7108 }
7109
Jan Karaa90714c2008-10-09 19:38:40 +02007110 handle = ocfs2_start_trans(osb,
7111 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007112 if (IS_ERR(handle)) {
7113 ret = PTR_ERR(handle);
7114 mlog_errno(ret);
7115 goto out_unlock;
7116 }
7117
Joel Becker0cf2f762009-02-12 16:41:25 -08007118 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007119 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007120 if (ret) {
7121 mlog_errno(ret);
7122 goto out_commit;
7123 }
7124
7125 if (has_data) {
7126 u32 bit_off, num;
7127 unsigned int page_end;
7128 u64 phys;
7129
Jan Karaa90714c2008-10-09 19:38:40 +02007130 if (vfs_dq_alloc_space_nodirty(inode,
7131 ocfs2_clusters_to_bytes(osb->sb, 1))) {
7132 ret = -EDQUOT;
7133 goto out_commit;
7134 }
7135 did_quota = 1;
7136
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007137 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7138 &num);
7139 if (ret) {
7140 mlog_errno(ret);
7141 goto out_commit;
7142 }
7143
7144 /*
7145 * Save two copies, one for insert, and one that can
7146 * be changed by ocfs2_map_and_dirty_page() below.
7147 */
7148 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7149
7150 /*
7151 * Non sparse file systems zero on extend, so no need
7152 * to do that now.
7153 */
7154 if (!ocfs2_sparse_alloc(osb) &&
7155 PAGE_CACHE_SIZE < osb->s_clustersize)
7156 end = PAGE_CACHE_SIZE;
7157
7158 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7159 if (ret) {
7160 mlog_errno(ret);
7161 goto out_commit;
7162 }
7163
7164 /*
7165 * This should populate the 1st page for us and mark
7166 * it up to date.
7167 */
7168 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7169 if (ret) {
7170 mlog_errno(ret);
7171 goto out_commit;
7172 }
7173
7174 page_end = PAGE_CACHE_SIZE;
7175 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7176 page_end = osb->s_clustersize;
7177
7178 for (i = 0; i < num_pages; i++)
7179 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7180 pages[i], i > 0, &phys);
7181 }
7182
7183 spin_lock(&oi->ip_lock);
7184 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7185 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7186 spin_unlock(&oi->ip_lock);
7187
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007188 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007189
7190 ocfs2_journal_dirty(handle, di_bh);
7191
7192 if (has_data) {
7193 /*
7194 * An error at this point should be extremely rare. If
7195 * this proves to be false, we could always re-build
7196 * the in-inode data from our pages.
7197 */
Joel Becker8d6220d2008-08-22 12:46:09 -07007198 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07007199 ret = ocfs2_insert_extent(osb, handle, inode, &et,
7200 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007201 if (ret) {
7202 mlog_errno(ret);
7203 goto out_commit;
7204 }
7205
7206 inode->i_blocks = ocfs2_inode_sector_count(inode);
7207 }
7208
7209out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007210 if (ret < 0 && did_quota)
7211 vfs_dq_free_space_nodirty(inode,
7212 ocfs2_clusters_to_bytes(osb->sb, 1));
7213
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007214 ocfs2_commit_trans(osb, handle);
7215
7216out_unlock:
7217 if (data_ac)
7218 ocfs2_free_alloc_context(data_ac);
7219
7220out:
7221 if (pages) {
7222 ocfs2_unlock_and_free_pages(pages, num_pages);
7223 kfree(pages);
7224 }
7225
7226 return ret;
7227}
7228
Mark Fashehccd979b2005-12-15 14:31:24 -08007229/*
7230 * It is expected, that by the time you call this function,
7231 * inode->i_size and fe->i_size have been adjusted.
7232 *
7233 * WARNING: This will kfree the truncate context
7234 */
7235int ocfs2_commit_truncate(struct ocfs2_super *osb,
7236 struct inode *inode,
7237 struct buffer_head *fe_bh,
7238 struct ocfs2_truncate_context *tc)
7239{
7240 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08007241 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08007242 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07007243 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007244 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08007245 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08007246 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007247
7248 mlog_entry_void();
7249
Mark Fashehdcd05382007-01-16 11:32:23 -08007250 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007251 i_size_read(inode));
7252
Joel Becker13723d02008-10-17 19:25:01 -07007253 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7254 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08007255 if (!path) {
7256 status = -ENOMEM;
7257 mlog_errno(status);
7258 goto bail;
7259 }
Mark Fasheh83418972007-04-23 18:53:12 -07007260
7261 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7262
Mark Fashehccd979b2005-12-15 14:31:24 -08007263start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007264 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007265 * Check that we still have allocation to delete.
7266 */
7267 if (OCFS2_I(inode)->ip_clusters == 0) {
7268 status = 0;
7269 goto bail;
7270 }
7271
7272 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007273 * Truncate always works against the rightmost tree branch.
7274 */
Joel Beckerfacdb772009-02-12 18:08:48 -08007275 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
Mark Fashehdcd05382007-01-16 11:32:23 -08007276 if (status) {
7277 mlog_errno(status);
7278 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007279 }
7280
Mark Fashehdcd05382007-01-16 11:32:23 -08007281 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7282 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7283
7284 /*
7285 * By now, el will point to the extent list on the bottom most
7286 * portion of this tree. Only the tail record is considered in
7287 * each pass.
7288 *
7289 * We handle the following cases, in order:
7290 * - empty extent: delete the remaining branch
7291 * - remove the entire record
7292 * - remove a partial record
7293 * - no record needs to be removed (truncate has completed)
7294 */
7295 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007296 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7297 ocfs2_error(inode->i_sb,
7298 "Inode %llu has empty extent block at %llu\n",
7299 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7300 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7301 status = -EROFS;
7302 goto bail;
7303 }
7304
Mark Fashehccd979b2005-12-15 14:31:24 -08007305 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08007306 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08007307 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007308 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7309 clusters_to_del = 0;
7310 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007311 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007312 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007313 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08007314 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08007315 new_highest_cpos;
7316 } else {
7317 status = 0;
7318 goto bail;
7319 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007320
Mark Fashehdcd05382007-01-16 11:32:23 -08007321 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7322 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7323
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007324 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007325 tl_sem = 1;
7326 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7327 * record is free for use. If there isn't any, we flush to get
7328 * an empty truncate log. */
7329 if (ocfs2_truncate_log_needs_flush(osb)) {
7330 status = __ocfs2_flush_truncate_log(osb);
7331 if (status < 0) {
7332 mlog_errno(status);
7333 goto bail;
7334 }
7335 }
7336
7337 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007338 (struct ocfs2_dinode *)fe_bh->b_data,
7339 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007340 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007341 if (IS_ERR(handle)) {
7342 status = PTR_ERR(handle);
7343 handle = NULL;
7344 mlog_errno(status);
7345 goto bail;
7346 }
7347
Mark Fashehdcd05382007-01-16 11:32:23 -08007348 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7349 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007350 if (status < 0) {
7351 mlog_errno(status);
7352 goto bail;
7353 }
7354
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007355 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007356 tl_sem = 0;
7357
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007358 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007359 handle = NULL;
7360
Mark Fashehdcd05382007-01-16 11:32:23 -08007361 ocfs2_reinit_path(path, 1);
7362
7363 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007364 * The check above will catch the case where we've truncated
7365 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007366 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007367 goto start;
7368
Mark Fashehccd979b2005-12-15 14:31:24 -08007369bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007370
7371 ocfs2_schedule_truncate_log_flush(osb, 1);
7372
7373 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007374 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007375
7376 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007377 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007378
Mark Fasheh59a5e412007-06-22 15:52:36 -07007379 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7380
Mark Fashehdcd05382007-01-16 11:32:23 -08007381 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007382
7383 /* This will drop the ext_alloc cluster lock for us */
7384 ocfs2_free_truncate_context(tc);
7385
7386 mlog_exit(status);
7387 return status;
7388}
7389
Mark Fashehccd979b2005-12-15 14:31:24 -08007390/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007391 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007392 */
7393int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7394 struct inode *inode,
7395 struct buffer_head *fe_bh,
7396 struct ocfs2_truncate_context **tc)
7397{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007398 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007399 unsigned int new_i_clusters;
7400 struct ocfs2_dinode *fe;
7401 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007402 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007403
7404 mlog_entry_void();
7405
7406 *tc = NULL;
7407
7408 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7409 i_size_read(inode));
7410 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7411
7412 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007413 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7414 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007415
Robert P. J. Daycd861282006-12-13 00:34:52 -08007416 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007417 if (!(*tc)) {
7418 status = -ENOMEM;
7419 mlog_errno(status);
7420 goto bail;
7421 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007422 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007423
Mark Fashehccd979b2005-12-15 14:31:24 -08007424 if (fe->id2.i_list.l_tree_depth) {
Joel Becker3d03a302009-02-12 17:49:26 -08007425 status = ocfs2_read_extent_block(INODE_CACHE(inode),
Joel Becker5e965812008-11-13 14:49:16 -08007426 le64_to_cpu(fe->i_last_eb_blk),
7427 &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007428 if (status < 0) {
7429 mlog_errno(status);
7430 goto bail;
7431 }
7432 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007433 }
7434
7435 (*tc)->tc_last_eb_bh = last_eb_bh;
7436
Mark Fashehccd979b2005-12-15 14:31:24 -08007437 status = 0;
7438bail:
7439 if (status < 0) {
7440 if (*tc)
7441 ocfs2_free_truncate_context(*tc);
7442 *tc = NULL;
7443 }
7444 mlog_exit_void();
7445 return status;
7446}
7447
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007448/*
7449 * 'start' is inclusive, 'end' is not.
7450 */
7451int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7452 unsigned int start, unsigned int end, int trunc)
7453{
7454 int ret;
7455 unsigned int numbytes;
7456 handle_t *handle;
7457 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7458 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7459 struct ocfs2_inline_data *idata = &di->id2.i_data;
7460
7461 if (end > i_size_read(inode))
7462 end = i_size_read(inode);
7463
7464 BUG_ON(start >= end);
7465
7466 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7467 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7468 !ocfs2_supports_inline_data(osb)) {
7469 ocfs2_error(inode->i_sb,
7470 "Inline data flags for inode %llu don't agree! "
7471 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7472 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7473 le16_to_cpu(di->i_dyn_features),
7474 OCFS2_I(inode)->ip_dyn_features,
7475 osb->s_feature_incompat);
7476 ret = -EROFS;
7477 goto out;
7478 }
7479
7480 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7481 if (IS_ERR(handle)) {
7482 ret = PTR_ERR(handle);
7483 mlog_errno(ret);
7484 goto out;
7485 }
7486
Joel Becker0cf2f762009-02-12 16:41:25 -08007487 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007488 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007489 if (ret) {
7490 mlog_errno(ret);
7491 goto out_commit;
7492 }
7493
7494 numbytes = end - start;
7495 memset(idata->id_data + start, 0, numbytes);
7496
7497 /*
7498 * No need to worry about the data page here - it's been
7499 * truncated already and inline data doesn't need it for
7500 * pushing zero's to disk, so we'll let readpage pick it up
7501 * later.
7502 */
7503 if (trunc) {
7504 i_size_write(inode, start);
7505 di->i_size = cpu_to_le64(start);
7506 }
7507
7508 inode->i_blocks = ocfs2_inode_sector_count(inode);
7509 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7510
7511 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7512 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7513
7514 ocfs2_journal_dirty(handle, di_bh);
7515
7516out_commit:
7517 ocfs2_commit_trans(osb, handle);
7518
7519out:
7520 return ret;
7521}
7522
Mark Fashehccd979b2005-12-15 14:31:24 -08007523static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7524{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007525 /*
7526 * The caller is responsible for completing deallocation
7527 * before freeing the context.
7528 */
7529 if (tc->tc_dealloc.c_first_suballocator != NULL)
7530 mlog(ML_NOTICE,
7531 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007532
Mark Fasheha81cb882008-10-07 14:25:16 -07007533 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007534
7535 kfree(tc);
7536}