blob: 2c4967f7b66733631332555a8341f9974348184c [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);
Tao Ma6b791bc2009-06-12 14:18:36 +0800463static void ocfs2_adjust_rightmost_records(struct inode *inode,
464 handle_t *handle,
465 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,
1012 struct inode *inode,
1013 struct ocfs2_extent_tree *et)
1014{
1015 int status;
1016 struct ocfs2_path *path = NULL;
1017 struct ocfs2_extent_list *el;
1018 struct ocfs2_extent_rec *rec;
1019
1020 path = ocfs2_new_path_from_et(et);
1021 if (!path) {
1022 status = -ENOMEM;
1023 return status;
1024 }
1025
Joel Beckerfacdb772009-02-12 18:08:48 -08001026 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
Tao Ma6b791bc2009-06-12 14:18:36 +08001027 if (status < 0) {
1028 mlog_errno(status);
1029 goto out;
1030 }
1031
1032 status = ocfs2_extend_trans(handle, path_num_items(path) +
1033 handle->h_buffer_credits);
1034 if (status < 0) {
1035 mlog_errno(status);
1036 goto out;
1037 }
1038
Joel Becker0cf2f762009-02-12 16:41:25 -08001039 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Tao Ma6b791bc2009-06-12 14:18:36 +08001040 if (status < 0) {
1041 mlog_errno(status);
1042 goto out;
1043 }
1044
1045 el = path_leaf_el(path);
1046 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1047
1048 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
1049
1050out:
1051 ocfs2_free_path(path);
1052 return status;
1053}
1054
1055/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001056 * Add an entire tree branch to our inode. eb_bh is the extent block
1057 * to start at, if we don't want to start the branch at the dinode
1058 * structure.
1059 *
1060 * last_eb_bh is required as we have to update it's next_leaf pointer
1061 * for the new last extent block.
1062 *
1063 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -08001064 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -08001065 */
1066static int ocfs2_add_branch(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001067 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001068 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001069 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001070 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07001071 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001072 struct ocfs2_alloc_context *meta_ac)
1073{
1074 int status, new_blocks, i;
1075 u64 next_blkno, new_last_eb_blk;
1076 struct buffer_head *bh;
1077 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001078 struct ocfs2_extent_block *eb;
1079 struct ocfs2_extent_list *eb_el;
1080 struct ocfs2_extent_list *el;
Tao Ma6b791bc2009-06-12 14:18:36 +08001081 u32 new_cpos, root_end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001082
1083 mlog_entry_void();
1084
Mark Fasheh328d5752007-06-18 10:48:04 -07001085 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001086
Mark Fashehccd979b2005-12-15 14:31:24 -08001087 if (eb_bh) {
1088 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1089 el = &eb->h_list;
1090 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001091 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001092
1093 /* we never add a branch to a leaf. */
1094 BUG_ON(!el->l_tree_depth);
1095
1096 new_blocks = le16_to_cpu(el->l_tree_depth);
1097
Tao Ma6b791bc2009-06-12 14:18:36 +08001098 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1099 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1100 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1101
1102 /*
1103 * If there is a gap before the root end and the real end
1104 * of the righmost leaf block, we need to remove the gap
1105 * between new_cpos and root_end first so that the tree
1106 * is consistent after we add a new branch(it will start
1107 * from new_cpos).
1108 */
1109 if (root_end > new_cpos) {
1110 mlog(0, "adjust the cluster end from %u to %u\n",
1111 root_end, new_cpos);
1112 status = ocfs2_adjust_rightmost_branch(handle, inode, et);
1113 if (status) {
1114 mlog_errno(status);
1115 goto bail;
1116 }
1117 }
1118
Mark Fashehccd979b2005-12-15 14:31:24 -08001119 /* allocate the number of new eb blocks we need */
1120 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1121 GFP_KERNEL);
1122 if (!new_eb_bhs) {
1123 status = -ENOMEM;
1124 mlog_errno(status);
1125 goto bail;
1126 }
1127
Joel Becker42a5a7a2009-02-12 18:49:19 -08001128 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
Mark Fashehccd979b2005-12-15 14:31:24 -08001129 meta_ac, new_eb_bhs);
1130 if (status < 0) {
1131 mlog_errno(status);
1132 goto bail;
1133 }
1134
1135 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1136 * linked with the rest of the tree.
1137 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1138 *
1139 * when we leave the loop, new_last_eb_blk will point to the
1140 * newest leaf, and next_blkno will point to the topmost extent
1141 * block. */
1142 next_blkno = new_last_eb_blk = 0;
1143 for(i = 0; i < new_blocks; i++) {
1144 bh = new_eb_bhs[i];
1145 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001146 /* ocfs2_create_new_meta_bhs() should create it right! */
1147 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001148 eb_el = &eb->h_list;
1149
Joel Becker0cf2f762009-02-12 16:41:25 -08001150 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), bh,
Joel Becker13723d02008-10-17 19:25:01 -07001151 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001152 if (status < 0) {
1153 mlog_errno(status);
1154 goto bail;
1155 }
1156
1157 eb->h_next_leaf_blk = 0;
1158 eb_el->l_tree_depth = cpu_to_le16(i);
1159 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001160 /*
1161 * This actually counts as an empty extent as
1162 * c_clusters == 0
1163 */
1164 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001165 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001166 /*
1167 * eb_el isn't always an interior node, but even leaf
1168 * nodes want a zero'd flags and reserved field so
1169 * this gets the whole 32 bits regardless of use.
1170 */
1171 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001172 if (!eb_el->l_tree_depth)
1173 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1174
1175 status = ocfs2_journal_dirty(handle, bh);
1176 if (status < 0) {
1177 mlog_errno(status);
1178 goto bail;
1179 }
1180
1181 next_blkno = le64_to_cpu(eb->h_blkno);
1182 }
1183
1184 /* This is a bit hairy. We want to update up to three blocks
1185 * here without leaving any of them in an inconsistent state
1186 * in case of error. We don't have to worry about
1187 * journal_dirty erroring as it won't unless we've aborted the
1188 * handle (in which case we would never be here) so reserving
1189 * the write with journal_access is all we need to do. */
Joel Becker0cf2f762009-02-12 16:41:25 -08001190 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), *last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001191 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001192 if (status < 0) {
1193 mlog_errno(status);
1194 goto bail;
1195 }
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001196 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001197 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001198 if (status < 0) {
1199 mlog_errno(status);
1200 goto bail;
1201 }
1202 if (eb_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08001203 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001204 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001205 if (status < 0) {
1206 mlog_errno(status);
1207 goto bail;
1208 }
1209 }
1210
1211 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001212 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001213 i = le16_to_cpu(el->l_next_free_rec);
1214 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001215 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001216 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001217 le16_add_cpu(&el->l_next_free_rec, 1);
1218
1219 /* fe needs a new last extent block pointer, as does the
1220 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001221 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001222
Mark Fasheh328d5752007-06-18 10:48:04 -07001223 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001224 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1225
Mark Fasheh328d5752007-06-18 10:48:04 -07001226 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001227 if (status < 0)
1228 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001229 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001230 if (status < 0)
1231 mlog_errno(status);
1232 if (eb_bh) {
1233 status = ocfs2_journal_dirty(handle, eb_bh);
1234 if (status < 0)
1235 mlog_errno(status);
1236 }
1237
Mark Fasheh328d5752007-06-18 10:48:04 -07001238 /*
1239 * Some callers want to track the rightmost leaf so pass it
1240 * back here.
1241 */
1242 brelse(*last_eb_bh);
1243 get_bh(new_eb_bhs[0]);
1244 *last_eb_bh = new_eb_bhs[0];
1245
Mark Fashehccd979b2005-12-15 14:31:24 -08001246 status = 0;
1247bail:
1248 if (new_eb_bhs) {
1249 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001250 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001251 kfree(new_eb_bhs);
1252 }
1253
1254 mlog_exit(status);
1255 return status;
1256}
1257
1258/*
1259 * adds another level to the allocation tree.
1260 * returns back the new extent block so you can add a branch to it
1261 * after this call.
1262 */
1263static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001264 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001265 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001266 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001267 struct ocfs2_alloc_context *meta_ac,
1268 struct buffer_head **ret_new_eb_bh)
1269{
1270 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001271 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001272 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001273 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001274 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001275 struct ocfs2_extent_list *eb_el;
1276
1277 mlog_entry_void();
1278
Joel Becker42a5a7a2009-02-12 18:49:19 -08001279 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
Mark Fashehccd979b2005-12-15 14:31:24 -08001280 &new_eb_bh);
1281 if (status < 0) {
1282 mlog_errno(status);
1283 goto bail;
1284 }
1285
1286 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001287 /* ocfs2_create_new_meta_bhs() should create it right! */
1288 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001289
1290 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001291 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001292
Joel Becker0cf2f762009-02-12 16:41:25 -08001293 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), new_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001294 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001295 if (status < 0) {
1296 mlog_errno(status);
1297 goto bail;
1298 }
1299
Tao Mae7d4cb62008-08-18 17:38:44 +08001300 /* copy the root extent list data into the new extent block */
1301 eb_el->l_tree_depth = root_el->l_tree_depth;
1302 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1303 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1304 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001305
1306 status = ocfs2_journal_dirty(handle, new_eb_bh);
1307 if (status < 0) {
1308 mlog_errno(status);
1309 goto bail;
1310 }
1311
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001312 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001313 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001314 if (status < 0) {
1315 mlog_errno(status);
1316 goto bail;
1317 }
1318
Mark Fashehdcd05382007-01-16 11:32:23 -08001319 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1320
Tao Mae7d4cb62008-08-18 17:38:44 +08001321 /* update root_bh now */
1322 le16_add_cpu(&root_el->l_tree_depth, 1);
1323 root_el->l_recs[0].e_cpos = 0;
1324 root_el->l_recs[0].e_blkno = eb->h_blkno;
1325 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1326 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1327 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1328 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001329
1330 /* If this is our 1st tree depth shift, then last_eb_blk
1331 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001332 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001333 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001334
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001335 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001336 if (status < 0) {
1337 mlog_errno(status);
1338 goto bail;
1339 }
1340
1341 *ret_new_eb_bh = new_eb_bh;
1342 new_eb_bh = NULL;
1343 status = 0;
1344bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001345 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001346
1347 mlog_exit(status);
1348 return status;
1349}
1350
1351/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001352 * Should only be called when there is no space left in any of the
1353 * leaf nodes. What we want to do is find the lowest tree depth
1354 * non-leaf extent block with room for new records. There are three
1355 * valid results of this search:
1356 *
1357 * 1) a lowest extent block is found, then we pass it back in
1358 * *lowest_eb_bh and return '0'
1359 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001360 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001361 * pass NULL back in *lowest_eb_bh, but still return '0'
1362 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001363 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001364 * which case we return > 0
1365 *
1366 * return status < 0 indicates an error.
1367 */
1368static int ocfs2_find_branch_target(struct ocfs2_super *osb,
Tao Mae7d4cb62008-08-18 17:38:44 +08001369 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001370 struct buffer_head **target_bh)
1371{
1372 int status = 0, i;
1373 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001374 struct ocfs2_extent_block *eb;
1375 struct ocfs2_extent_list *el;
1376 struct buffer_head *bh = NULL;
1377 struct buffer_head *lowest_bh = NULL;
1378
1379 mlog_entry_void();
1380
1381 *target_bh = NULL;
1382
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001383 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001384
1385 while(le16_to_cpu(el->l_tree_depth) > 1) {
1386 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08001387 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1388 "Owner %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001389 "extent list (next_free_rec == 0)",
Joel Becker3d03a302009-02-12 17:49:26 -08001390 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001391 status = -EIO;
1392 goto bail;
1393 }
1394 i = le16_to_cpu(el->l_next_free_rec) - 1;
1395 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1396 if (!blkno) {
Joel Becker3d03a302009-02-12 17:49:26 -08001397 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1398 "Owner %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001399 "list where extent # %d has no physical "
1400 "block start",
Joel Becker3d03a302009-02-12 17:49:26 -08001401 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001402 status = -EIO;
1403 goto bail;
1404 }
1405
Mark Fasheha81cb882008-10-07 14:25:16 -07001406 brelse(bh);
1407 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001408
Joel Becker3d03a302009-02-12 17:49:26 -08001409 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001410 if (status < 0) {
1411 mlog_errno(status);
1412 goto bail;
1413 }
1414
1415 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001416 el = &eb->h_list;
1417
1418 if (le16_to_cpu(el->l_next_free_rec) <
1419 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001420 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001421 lowest_bh = bh;
1422 get_bh(lowest_bh);
1423 }
1424 }
1425
1426 /* If we didn't find one and the fe doesn't have any room,
1427 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001428 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001429 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001430 status = 1;
1431
1432 *target_bh = lowest_bh;
1433bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001434 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001435
1436 mlog_exit(status);
1437 return status;
1438}
1439
Mark Fashehe48edee2007-03-07 16:46:57 -08001440/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001441 * Grow a b-tree so that it has more records.
1442 *
1443 * We might shift the tree depth in which case existing paths should
1444 * be considered invalid.
1445 *
1446 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001447 *
1448 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001449 */
1450static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001451 struct ocfs2_extent_tree *et, int *final_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07001452 struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001453 struct ocfs2_alloc_context *meta_ac)
1454{
1455 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001456 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001457 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001458 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1459 struct buffer_head *bh = NULL;
1460
1461 BUG_ON(meta_ac == NULL);
1462
Joel Becker3d03a302009-02-12 17:49:26 -08001463 shift = ocfs2_find_branch_target(osb, et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001464 if (shift < 0) {
1465 ret = shift;
1466 mlog_errno(ret);
1467 goto out;
1468 }
1469
1470 /* We traveled all the way to the bottom of the allocation tree
1471 * and didn't find room for any more extents - we need to add
1472 * another tree level */
1473 if (shift) {
1474 BUG_ON(bh);
1475 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1476
1477 /* ocfs2_shift_tree_depth will return us a buffer with
1478 * the new extent block (so we can pass that to
1479 * ocfs2_add_branch). */
Tao Mae7d4cb62008-08-18 17:38:44 +08001480 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001481 meta_ac, &bh);
1482 if (ret < 0) {
1483 mlog_errno(ret);
1484 goto out;
1485 }
1486 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001487 if (depth == 1) {
1488 /*
1489 * Special case: we have room now if we shifted from
1490 * tree_depth 0, so no more work needs to be done.
1491 *
1492 * We won't be calling add_branch, so pass
1493 * back *last_eb_bh as the new leaf. At depth
1494 * zero, it should always be null so there's
1495 * no reason to brelse.
1496 */
1497 BUG_ON(*last_eb_bh);
1498 get_bh(bh);
1499 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001500 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001501 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001502 }
1503
1504 /* call ocfs2_add_branch to add the final part of the tree with
1505 * the new data. */
1506 mlog(0, "add branch. bh = %p\n", bh);
Tao Mae7d4cb62008-08-18 17:38:44 +08001507 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001508 meta_ac);
1509 if (ret < 0) {
1510 mlog_errno(ret);
1511 goto out;
1512 }
1513
1514out:
1515 if (final_depth)
1516 *final_depth = depth;
1517 brelse(bh);
1518 return ret;
1519}
1520
1521/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001522 * This function will discard the rightmost extent record.
1523 */
1524static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1525{
1526 int next_free = le16_to_cpu(el->l_next_free_rec);
1527 int count = le16_to_cpu(el->l_count);
1528 unsigned int num_bytes;
1529
1530 BUG_ON(!next_free);
1531 /* This will cause us to go off the end of our extent list. */
1532 BUG_ON(next_free >= count);
1533
1534 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1535
1536 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1537}
1538
1539static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1540 struct ocfs2_extent_rec *insert_rec)
1541{
1542 int i, insert_index, next_free, has_empty, num_bytes;
1543 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1544 struct ocfs2_extent_rec *rec;
1545
1546 next_free = le16_to_cpu(el->l_next_free_rec);
1547 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1548
1549 BUG_ON(!next_free);
1550
1551 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001552 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001553
1554 /*
1555 * The easiest way to approach this is to just remove the
1556 * empty extent and temporarily decrement next_free.
1557 */
1558 if (has_empty) {
1559 /*
1560 * If next_free was 1 (only an empty extent), this
1561 * loop won't execute, which is fine. We still want
1562 * the decrement above to happen.
1563 */
1564 for(i = 0; i < (next_free - 1); i++)
1565 el->l_recs[i] = el->l_recs[i+1];
1566
1567 next_free--;
1568 }
1569
1570 /*
1571 * Figure out what the new record index should be.
1572 */
1573 for(i = 0; i < next_free; i++) {
1574 rec = &el->l_recs[i];
1575
1576 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1577 break;
1578 }
1579 insert_index = i;
1580
1581 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1582 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1583
1584 BUG_ON(insert_index < 0);
1585 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1586 BUG_ON(insert_index > next_free);
1587
1588 /*
1589 * No need to memmove if we're just adding to the tail.
1590 */
1591 if (insert_index != next_free) {
1592 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1593
1594 num_bytes = next_free - insert_index;
1595 num_bytes *= sizeof(struct ocfs2_extent_rec);
1596 memmove(&el->l_recs[insert_index + 1],
1597 &el->l_recs[insert_index],
1598 num_bytes);
1599 }
1600
1601 /*
1602 * Either we had an empty extent, and need to re-increment or
1603 * there was no empty extent on a non full rightmost leaf node,
1604 * in which case we still need to increment.
1605 */
1606 next_free++;
1607 el->l_next_free_rec = cpu_to_le16(next_free);
1608 /*
1609 * Make sure none of the math above just messed up our tree.
1610 */
1611 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1612
1613 el->l_recs[insert_index] = *insert_rec;
1614
1615}
1616
Mark Fasheh328d5752007-06-18 10:48:04 -07001617static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1618{
1619 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1620
1621 BUG_ON(num_recs == 0);
1622
1623 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1624 num_recs--;
1625 size = num_recs * sizeof(struct ocfs2_extent_rec);
1626 memmove(&el->l_recs[0], &el->l_recs[1], size);
1627 memset(&el->l_recs[num_recs], 0,
1628 sizeof(struct ocfs2_extent_rec));
1629 el->l_next_free_rec = cpu_to_le16(num_recs);
1630 }
1631}
1632
Mark Fashehdcd05382007-01-16 11:32:23 -08001633/*
1634 * Create an empty extent record .
1635 *
1636 * l_next_free_rec may be updated.
1637 *
1638 * If an empty extent already exists do nothing.
1639 */
1640static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1641{
1642 int next_free = le16_to_cpu(el->l_next_free_rec);
1643
Mark Fashehe48edee2007-03-07 16:46:57 -08001644 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1645
Mark Fashehdcd05382007-01-16 11:32:23 -08001646 if (next_free == 0)
1647 goto set_and_inc;
1648
1649 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1650 return;
1651
1652 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1653 "Asked to create an empty extent in a full list:\n"
1654 "count = %u, tree depth = %u",
1655 le16_to_cpu(el->l_count),
1656 le16_to_cpu(el->l_tree_depth));
1657
1658 ocfs2_shift_records_right(el);
1659
1660set_and_inc:
1661 le16_add_cpu(&el->l_next_free_rec, 1);
1662 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1663}
1664
1665/*
1666 * For a rotation which involves two leaf nodes, the "root node" is
1667 * the lowest level tree node which contains a path to both leafs. This
1668 * resulting set of information can be used to form a complete "subtree"
1669 *
1670 * This function is passed two full paths from the dinode down to a
1671 * pair of adjacent leaves. It's task is to figure out which path
1672 * index contains the subtree root - this can be the root index itself
1673 * in a worst-case rotation.
1674 *
1675 * The array index of the subtree root is passed back.
1676 */
Joel Becker7dc02802009-02-12 19:20:13 -08001677static int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08001678 struct ocfs2_path *left,
1679 struct ocfs2_path *right)
1680{
1681 int i = 0;
1682
1683 /*
1684 * Check that the caller passed in two paths from the same tree.
1685 */
1686 BUG_ON(path_root_bh(left) != path_root_bh(right));
1687
1688 do {
1689 i++;
1690
1691 /*
1692 * The caller didn't pass two adjacent paths.
1693 */
1694 mlog_bug_on_msg(i > left->p_tree_depth,
Joel Becker7dc02802009-02-12 19:20:13 -08001695 "Owner %llu, left depth %u, right depth %u\n"
Mark Fashehdcd05382007-01-16 11:32:23 -08001696 "left leaf blk %llu, right leaf blk %llu\n",
Joel Becker7dc02802009-02-12 19:20:13 -08001697 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1698 left->p_tree_depth, right->p_tree_depth,
Mark Fashehdcd05382007-01-16 11:32:23 -08001699 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1700 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1701 } while (left->p_node[i].bh->b_blocknr ==
1702 right->p_node[i].bh->b_blocknr);
1703
1704 return i - 1;
1705}
1706
1707typedef void (path_insert_t)(void *, struct buffer_head *);
1708
1709/*
1710 * Traverse a btree path in search of cpos, starting at root_el.
1711 *
1712 * This code can be called with a cpos larger than the tree, in which
1713 * case it will return the rightmost path.
1714 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001715static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
Mark Fashehdcd05382007-01-16 11:32:23 -08001716 struct ocfs2_extent_list *root_el, u32 cpos,
1717 path_insert_t *func, void *data)
1718{
1719 int i, ret = 0;
1720 u32 range;
1721 u64 blkno;
1722 struct buffer_head *bh = NULL;
1723 struct ocfs2_extent_block *eb;
1724 struct ocfs2_extent_list *el;
1725 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08001726
1727 el = root_el;
1728 while (el->l_tree_depth) {
1729 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001730 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1731 "Owner %llu has empty extent list at "
Mark Fashehdcd05382007-01-16 11:32:23 -08001732 "depth %u\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001733 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001734 le16_to_cpu(el->l_tree_depth));
1735 ret = -EROFS;
1736 goto out;
1737
1738 }
1739
1740 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1741 rec = &el->l_recs[i];
1742
1743 /*
1744 * In the case that cpos is off the allocation
1745 * tree, this should just wind up returning the
1746 * rightmost record.
1747 */
1748 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001749 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001750 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1751 break;
1752 }
1753
1754 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1755 if (blkno == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001756 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1757 "Owner %llu has bad blkno in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001758 "at depth %u (index %d)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001759 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001760 le16_to_cpu(el->l_tree_depth), i);
1761 ret = -EROFS;
1762 goto out;
1763 }
1764
1765 brelse(bh);
1766 bh = NULL;
Joel Beckerfacdb772009-02-12 18:08:48 -08001767 ret = ocfs2_read_extent_block(ci, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001768 if (ret) {
1769 mlog_errno(ret);
1770 goto out;
1771 }
1772
1773 eb = (struct ocfs2_extent_block *) bh->b_data;
1774 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001775
1776 if (le16_to_cpu(el->l_next_free_rec) >
1777 le16_to_cpu(el->l_count)) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001778 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1779 "Owner %llu has bad count in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001780 "at block %llu (next free=%u, count=%u)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001781 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001782 (unsigned long long)bh->b_blocknr,
1783 le16_to_cpu(el->l_next_free_rec),
1784 le16_to_cpu(el->l_count));
1785 ret = -EROFS;
1786 goto out;
1787 }
1788
1789 if (func)
1790 func(data, bh);
1791 }
1792
1793out:
1794 /*
1795 * Catch any trailing bh that the loop didn't handle.
1796 */
1797 brelse(bh);
1798
1799 return ret;
1800}
1801
1802/*
1803 * Given an initialized path (that is, it has a valid root extent
1804 * list), this function will traverse the btree in search of the path
1805 * which would contain cpos.
1806 *
1807 * The path traveled is recorded in the path structure.
1808 *
1809 * Note that this will not do any comparisons on leaf node extent
1810 * records, so it will work fine in the case that we just added a tree
1811 * branch.
1812 */
1813struct find_path_data {
1814 int index;
1815 struct ocfs2_path *path;
1816};
1817static void find_path_ins(void *data, struct buffer_head *bh)
1818{
1819 struct find_path_data *fp = data;
1820
1821 get_bh(bh);
1822 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1823 fp->index++;
1824}
Joel Beckerfacdb772009-02-12 18:08:48 -08001825static int ocfs2_find_path(struct ocfs2_caching_info *ci,
1826 struct ocfs2_path *path, u32 cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08001827{
1828 struct find_path_data data;
1829
1830 data.index = 1;
1831 data.path = path;
Joel Beckerfacdb772009-02-12 18:08:48 -08001832 return __ocfs2_find_path(ci, path_root_el(path), cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08001833 find_path_ins, &data);
1834}
1835
1836static void find_leaf_ins(void *data, struct buffer_head *bh)
1837{
1838 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1839 struct ocfs2_extent_list *el = &eb->h_list;
1840 struct buffer_head **ret = data;
1841
1842 /* We want to retain only the leaf block. */
1843 if (le16_to_cpu(el->l_tree_depth) == 0) {
1844 get_bh(bh);
1845 *ret = bh;
1846 }
1847}
1848/*
1849 * Find the leaf block in the tree which would contain cpos. No
1850 * checking of the actual leaf is done.
1851 *
1852 * Some paths want to call this instead of allocating a path structure
1853 * and calling ocfs2_find_path().
1854 *
1855 * This function doesn't handle non btree extent lists.
1856 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001857int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1858 struct ocfs2_extent_list *root_el, u32 cpos,
1859 struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001860{
1861 int ret;
1862 struct buffer_head *bh = NULL;
1863
Joel Beckerfacdb772009-02-12 18:08:48 -08001864 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001865 if (ret) {
1866 mlog_errno(ret);
1867 goto out;
1868 }
1869
1870 *leaf_bh = bh;
1871out:
1872 return ret;
1873}
1874
1875/*
1876 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1877 *
1878 * Basically, we've moved stuff around at the bottom of the tree and
1879 * we need to fix up the extent records above the changes to reflect
1880 * the new changes.
1881 *
1882 * left_rec: the record on the left.
1883 * left_child_el: is the child list pointed to by left_rec
1884 * right_rec: the record to the right of left_rec
1885 * right_child_el: is the child list pointed to by right_rec
1886 *
1887 * By definition, this only works on interior nodes.
1888 */
1889static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1890 struct ocfs2_extent_list *left_child_el,
1891 struct ocfs2_extent_rec *right_rec,
1892 struct ocfs2_extent_list *right_child_el)
1893{
1894 u32 left_clusters, right_end;
1895
1896 /*
1897 * Interior nodes never have holes. Their cpos is the cpos of
1898 * the leftmost record in their child list. Their cluster
1899 * count covers the full theoretical range of their child list
1900 * - the range between their cpos and the cpos of the record
1901 * immediately to their right.
1902 */
1903 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08001904 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1905 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07001906 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1907 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1908 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001909 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001910 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001911
1912 /*
1913 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001914 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001915 * updating e_cpos to keep the same highest cluster count.
1916 */
1917 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001918 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001919
1920 right_rec->e_cpos = left_rec->e_cpos;
1921 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1922
1923 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001924 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001925}
1926
1927/*
1928 * Adjust the adjacent root node records involved in a
1929 * rotation. left_el_blkno is passed in as a key so that we can easily
1930 * find it's index in the root list.
1931 */
1932static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1933 struct ocfs2_extent_list *left_el,
1934 struct ocfs2_extent_list *right_el,
1935 u64 left_el_blkno)
1936{
1937 int i;
1938
1939 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1940 le16_to_cpu(left_el->l_tree_depth));
1941
1942 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1943 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1944 break;
1945 }
1946
1947 /*
1948 * The path walking code should have never returned a root and
1949 * two paths which are not adjacent.
1950 */
1951 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1952
1953 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1954 &root_el->l_recs[i + 1], right_el);
1955}
1956
1957/*
1958 * We've changed a leaf block (in right_path) and need to reflect that
1959 * change back up the subtree.
1960 *
1961 * This happens in multiple places:
1962 * - When we've moved an extent record from the left path leaf to the right
1963 * path leaf to make room for an empty extent in the left path leaf.
1964 * - When our insert into the right path leaf is at the leftmost edge
1965 * and requires an update of the path immediately to it's left. This
1966 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08001967 * - When we've adjusted the last extent record in the left path leaf and the
1968 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08001969 */
Joel Becker4619c732009-02-12 19:02:36 -08001970static void ocfs2_complete_edge_insert(handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08001971 struct ocfs2_path *left_path,
1972 struct ocfs2_path *right_path,
1973 int subtree_index)
1974{
1975 int ret, i, idx;
1976 struct ocfs2_extent_list *el, *left_el, *right_el;
1977 struct ocfs2_extent_rec *left_rec, *right_rec;
1978 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1979
1980 /*
1981 * Update the counts and position values within all the
1982 * interior nodes to reflect the leaf rotation we just did.
1983 *
1984 * The root node is handled below the loop.
1985 *
1986 * We begin the loop with right_el and left_el pointing to the
1987 * leaf lists and work our way up.
1988 *
1989 * NOTE: within this loop, left_el and right_el always refer
1990 * to the *child* lists.
1991 */
1992 left_el = path_leaf_el(left_path);
1993 right_el = path_leaf_el(right_path);
1994 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1995 mlog(0, "Adjust records at index %u\n", i);
1996
1997 /*
1998 * One nice property of knowing that all of these
1999 * nodes are below the root is that we only deal with
2000 * the leftmost right node record and the rightmost
2001 * left node record.
2002 */
2003 el = left_path->p_node[i].el;
2004 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2005 left_rec = &el->l_recs[idx];
2006
2007 el = right_path->p_node[i].el;
2008 right_rec = &el->l_recs[0];
2009
2010 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2011 right_el);
2012
2013 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2014 if (ret)
2015 mlog_errno(ret);
2016
2017 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2018 if (ret)
2019 mlog_errno(ret);
2020
2021 /*
2022 * Setup our list pointers now so that the current
2023 * parents become children in the next iteration.
2024 */
2025 left_el = left_path->p_node[i].el;
2026 right_el = right_path->p_node[i].el;
2027 }
2028
2029 /*
2030 * At the root node, adjust the two adjacent records which
2031 * begin our path to the leaves.
2032 */
2033
2034 el = left_path->p_node[subtree_index].el;
2035 left_el = left_path->p_node[subtree_index + 1].el;
2036 right_el = right_path->p_node[subtree_index + 1].el;
2037
2038 ocfs2_adjust_root_records(el, left_el, right_el,
2039 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2040
2041 root_bh = left_path->p_node[subtree_index].bh;
2042
2043 ret = ocfs2_journal_dirty(handle, root_bh);
2044 if (ret)
2045 mlog_errno(ret);
2046}
2047
Joel Becker5c601ab2009-02-12 19:10:13 -08002048static int ocfs2_rotate_subtree_right(handle_t *handle,
2049 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08002050 struct ocfs2_path *left_path,
2051 struct ocfs2_path *right_path,
2052 int subtree_index)
2053{
2054 int ret, i;
2055 struct buffer_head *right_leaf_bh;
2056 struct buffer_head *left_leaf_bh = NULL;
2057 struct buffer_head *root_bh;
2058 struct ocfs2_extent_list *right_el, *left_el;
2059 struct ocfs2_extent_rec move_rec;
2060
2061 left_leaf_bh = path_leaf_bh(left_path);
2062 left_el = path_leaf_el(left_path);
2063
2064 if (left_el->l_next_free_rec != left_el->l_count) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002065 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002066 "Inode %llu has non-full interior leaf node %llu"
2067 "(next free = %u)",
Joel Becker5c601ab2009-02-12 19:10:13 -08002068 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002069 (unsigned long long)left_leaf_bh->b_blocknr,
2070 le16_to_cpu(left_el->l_next_free_rec));
2071 return -EROFS;
2072 }
2073
2074 /*
2075 * This extent block may already have an empty record, so we
2076 * return early if so.
2077 */
2078 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2079 return 0;
2080
2081 root_bh = left_path->p_node[subtree_index].bh;
2082 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2083
Joel Becker5c601ab2009-02-12 19:10:13 -08002084 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002085 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002086 if (ret) {
2087 mlog_errno(ret);
2088 goto out;
2089 }
2090
2091 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002092 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002093 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002094 if (ret) {
2095 mlog_errno(ret);
2096 goto out;
2097 }
2098
Joel Becker5c601ab2009-02-12 19:10:13 -08002099 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002100 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002101 if (ret) {
2102 mlog_errno(ret);
2103 goto out;
2104 }
2105 }
2106
2107 right_leaf_bh = path_leaf_bh(right_path);
2108 right_el = path_leaf_el(right_path);
2109
2110 /* This is a code error, not a disk corruption. */
2111 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2112 "because rightmost leaf block %llu is empty\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002113 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002114 (unsigned long long)right_leaf_bh->b_blocknr);
2115
2116 ocfs2_create_empty_extent(right_el);
2117
2118 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2119 if (ret) {
2120 mlog_errno(ret);
2121 goto out;
2122 }
2123
2124 /* Do the copy now. */
2125 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2126 move_rec = left_el->l_recs[i];
2127 right_el->l_recs[0] = move_rec;
2128
2129 /*
2130 * Clear out the record we just copied and shift everything
2131 * over, leaving an empty extent in the left leaf.
2132 *
2133 * We temporarily subtract from next_free_rec so that the
2134 * shift will lose the tail record (which is now defunct).
2135 */
2136 le16_add_cpu(&left_el->l_next_free_rec, -1);
2137 ocfs2_shift_records_right(left_el);
2138 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2139 le16_add_cpu(&left_el->l_next_free_rec, 1);
2140
2141 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2142 if (ret) {
2143 mlog_errno(ret);
2144 goto out;
2145 }
2146
Joel Becker4619c732009-02-12 19:02:36 -08002147 ocfs2_complete_edge_insert(handle, left_path, right_path,
2148 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002149
2150out:
2151 return ret;
2152}
2153
2154/*
2155 * Given a full path, determine what cpos value would return us a path
2156 * containing the leaf immediately to the left of the current one.
2157 *
2158 * Will return zero if the path passed in is already the leftmost path.
2159 */
2160static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2161 struct ocfs2_path *path, u32 *cpos)
2162{
2163 int i, j, ret = 0;
2164 u64 blkno;
2165 struct ocfs2_extent_list *el;
2166
Mark Fashehe48edee2007-03-07 16:46:57 -08002167 BUG_ON(path->p_tree_depth == 0);
2168
Mark Fashehdcd05382007-01-16 11:32:23 -08002169 *cpos = 0;
2170
2171 blkno = path_leaf_bh(path)->b_blocknr;
2172
2173 /* Start at the tree node just above the leaf and work our way up. */
2174 i = path->p_tree_depth - 1;
2175 while (i >= 0) {
2176 el = path->p_node[i].el;
2177
2178 /*
2179 * Find the extent record just before the one in our
2180 * path.
2181 */
2182 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2183 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2184 if (j == 0) {
2185 if (i == 0) {
2186 /*
2187 * We've determined that the
2188 * path specified is already
2189 * the leftmost one - return a
2190 * cpos of zero.
2191 */
2192 goto out;
2193 }
2194 /*
2195 * The leftmost record points to our
2196 * leaf - we need to travel up the
2197 * tree one level.
2198 */
2199 goto next_node;
2200 }
2201
2202 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002203 *cpos = *cpos + ocfs2_rec_clusters(el,
2204 &el->l_recs[j - 1]);
2205 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002206 goto out;
2207 }
2208 }
2209
2210 /*
2211 * If we got here, we never found a valid node where
2212 * the tree indicated one should be.
2213 */
2214 ocfs2_error(sb,
2215 "Invalid extent tree at extent block %llu\n",
2216 (unsigned long long)blkno);
2217 ret = -EROFS;
2218 goto out;
2219
2220next_node:
2221 blkno = path->p_node[i].bh->b_blocknr;
2222 i--;
2223 }
2224
2225out:
2226 return ret;
2227}
2228
Mark Fasheh328d5752007-06-18 10:48:04 -07002229/*
2230 * Extend the transaction by enough credits to complete the rotation,
2231 * and still leave at least the original number of credits allocated
2232 * to this transaction.
2233 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002234static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002235 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002236 struct ocfs2_path *path)
2237{
Mark Fasheh328d5752007-06-18 10:48:04 -07002238 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002239
2240 if (handle->h_buffer_credits < credits)
2241 return ocfs2_extend_trans(handle, credits);
2242
2243 return 0;
2244}
2245
2246/*
2247 * Trap the case where we're inserting into the theoretical range past
2248 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2249 * whose cpos is less than ours into the right leaf.
2250 *
2251 * It's only necessary to look at the rightmost record of the left
2252 * leaf because the logic that calls us should ensure that the
2253 * theoretical ranges in the path components above the leaves are
2254 * correct.
2255 */
2256static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2257 u32 insert_cpos)
2258{
2259 struct ocfs2_extent_list *left_el;
2260 struct ocfs2_extent_rec *rec;
2261 int next_free;
2262
2263 left_el = path_leaf_el(left_path);
2264 next_free = le16_to_cpu(left_el->l_next_free_rec);
2265 rec = &left_el->l_recs[next_free - 1];
2266
2267 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2268 return 1;
2269 return 0;
2270}
2271
Mark Fasheh328d5752007-06-18 10:48:04 -07002272static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2273{
2274 int next_free = le16_to_cpu(el->l_next_free_rec);
2275 unsigned int range;
2276 struct ocfs2_extent_rec *rec;
2277
2278 if (next_free == 0)
2279 return 0;
2280
2281 rec = &el->l_recs[0];
2282 if (ocfs2_is_empty_extent(rec)) {
2283 /* Empty list. */
2284 if (next_free == 1)
2285 return 0;
2286 rec = &el->l_recs[1];
2287 }
2288
2289 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2290 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2291 return 1;
2292 return 0;
2293}
2294
Mark Fashehdcd05382007-01-16 11:32:23 -08002295/*
2296 * Rotate all the records in a btree right one record, starting at insert_cpos.
2297 *
2298 * The path to the rightmost leaf should be passed in.
2299 *
2300 * The array is assumed to be large enough to hold an entire path (tree depth).
2301 *
2302 * Upon succesful return from this function:
2303 *
2304 * - The 'right_path' array will contain a path to the leaf block
2305 * whose range contains e_cpos.
2306 * - That leaf block will have a single empty extent in list index 0.
2307 * - In the case that the rotation requires a post-insert update,
2308 * *ret_left_path will contain a valid path which can be passed to
2309 * ocfs2_insert_path().
2310 */
Joel Becker1bbf0b82009-02-12 19:42:08 -08002311static int ocfs2_rotate_tree_right(handle_t *handle,
Joel Becker5c601ab2009-02-12 19:10:13 -08002312 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002313 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002314 u32 insert_cpos,
2315 struct ocfs2_path *right_path,
2316 struct ocfs2_path **ret_left_path)
2317{
Mark Fasheh328d5752007-06-18 10:48:04 -07002318 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002319 u32 cpos;
2320 struct ocfs2_path *left_path = NULL;
Joel Becker5c601ab2009-02-12 19:10:13 -08002321 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehdcd05382007-01-16 11:32:23 -08002322
2323 *ret_left_path = NULL;
2324
Joel Beckerffdd7a52008-10-17 22:32:01 -07002325 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002326 if (!left_path) {
2327 ret = -ENOMEM;
2328 mlog_errno(ret);
2329 goto out;
2330 }
2331
Joel Becker5c601ab2009-02-12 19:10:13 -08002332 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002333 if (ret) {
2334 mlog_errno(ret);
2335 goto out;
2336 }
2337
2338 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2339
2340 /*
2341 * What we want to do here is:
2342 *
2343 * 1) Start with the rightmost path.
2344 *
2345 * 2) Determine a path to the leaf block directly to the left
2346 * of that leaf.
2347 *
2348 * 3) Determine the 'subtree root' - the lowest level tree node
2349 * which contains a path to both leaves.
2350 *
2351 * 4) Rotate the subtree.
2352 *
2353 * 5) Find the next subtree by considering the left path to be
2354 * the new right path.
2355 *
2356 * The check at the top of this while loop also accepts
2357 * insert_cpos == cpos because cpos is only a _theoretical_
2358 * value to get us the left path - insert_cpos might very well
2359 * be filling that hole.
2360 *
2361 * Stop at a cpos of '0' because we either started at the
2362 * leftmost branch (i.e., a tree with one branch and a
2363 * rotation inside of it), or we've gone as far as we can in
2364 * rotating subtrees.
2365 */
2366 while (cpos && insert_cpos <= cpos) {
2367 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2368 insert_cpos, cpos);
2369
Joel Becker5c601ab2009-02-12 19:10:13 -08002370 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002371 if (ret) {
2372 mlog_errno(ret);
2373 goto out;
2374 }
2375
2376 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2377 path_leaf_bh(right_path),
Joel Becker5c601ab2009-02-12 19:10:13 -08002378 "Owner %llu: error during insert of %u "
Mark Fashehdcd05382007-01-16 11:32:23 -08002379 "(left path cpos %u) results in two identical "
2380 "paths ending at %llu\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002381 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2382 insert_cpos, cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08002383 (unsigned long long)
2384 path_leaf_bh(left_path)->b_blocknr);
2385
Mark Fasheh328d5752007-06-18 10:48:04 -07002386 if (split == SPLIT_NONE &&
2387 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002388 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002389
2390 /*
2391 * We've rotated the tree as much as we
2392 * should. The rest is up to
2393 * ocfs2_insert_path() to complete, after the
2394 * record insertion. We indicate this
2395 * situation by returning the left path.
2396 *
2397 * The reason we don't adjust the records here
2398 * before the record insert is that an error
2399 * later might break the rule where a parent
2400 * record e_cpos will reflect the actual
2401 * e_cpos of the 1st nonempty record of the
2402 * child list.
2403 */
2404 *ret_left_path = left_path;
2405 goto out_ret_path;
2406 }
2407
Joel Becker7dc02802009-02-12 19:20:13 -08002408 start = ocfs2_find_subtree_root(et, left_path, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002409
2410 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2411 start,
2412 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2413 right_path->p_tree_depth);
2414
2415 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002416 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002417 if (ret) {
2418 mlog_errno(ret);
2419 goto out;
2420 }
2421
Joel Becker5c601ab2009-02-12 19:10:13 -08002422 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002423 right_path, start);
2424 if (ret) {
2425 mlog_errno(ret);
2426 goto out;
2427 }
2428
Mark Fasheh328d5752007-06-18 10:48:04 -07002429 if (split != SPLIT_NONE &&
2430 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2431 insert_cpos)) {
2432 /*
2433 * A rotate moves the rightmost left leaf
2434 * record over to the leftmost right leaf
2435 * slot. If we're doing an extent split
2436 * instead of a real insert, then we have to
2437 * check that the extent to be split wasn't
2438 * just moved over. If it was, then we can
2439 * exit here, passing left_path back -
2440 * ocfs2_split_extent() is smart enough to
2441 * search both leaves.
2442 */
2443 *ret_left_path = left_path;
2444 goto out_ret_path;
2445 }
2446
Mark Fashehdcd05382007-01-16 11:32:23 -08002447 /*
2448 * There is no need to re-read the next right path
2449 * as we know that it'll be our current left
2450 * path. Optimize by copying values instead.
2451 */
2452 ocfs2_mv_path(right_path, left_path);
2453
Joel Becker5c601ab2009-02-12 19:10:13 -08002454 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002455 if (ret) {
2456 mlog_errno(ret);
2457 goto out;
2458 }
2459 }
2460
2461out:
2462 ocfs2_free_path(left_path);
2463
2464out_ret_path:
2465 return ret;
2466}
2467
Joel Becker09106ba2009-02-12 19:43:57 -08002468static int ocfs2_update_edge_lengths(handle_t *handle,
2469 struct ocfs2_extent_tree *et,
Tao Ma3c5e1062009-07-21 15:42:05 +08002470 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002471{
Tao Ma3c5e1062009-07-21 15:42:05 +08002472 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002473 struct ocfs2_extent_rec *rec;
2474 struct ocfs2_extent_list *el;
2475 struct ocfs2_extent_block *eb;
2476 u32 range;
2477
Tao Ma3c5e1062009-07-21 15:42:05 +08002478 /*
2479 * In normal tree rotation process, we will never touch the
2480 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2481 * doesn't reserve the credits for them either.
2482 *
2483 * But we do have a special case here which will update the rightmost
2484 * records for all the bh in the path.
2485 * So we have to allocate extra credits and access them.
2486 */
2487 ret = ocfs2_extend_trans(handle,
2488 handle->h_buffer_credits + subtree_index);
2489 if (ret) {
2490 mlog_errno(ret);
2491 goto out;
2492 }
2493
Joel Becker09106ba2009-02-12 19:43:57 -08002494 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002495 if (ret) {
2496 mlog_errno(ret);
2497 goto out;
2498 }
2499
Mark Fasheh328d5752007-06-18 10:48:04 -07002500 /* Path should always be rightmost. */
2501 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2502 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2503
2504 el = &eb->h_list;
2505 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2506 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2507 rec = &el->l_recs[idx];
2508 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2509
2510 for (i = 0; i < path->p_tree_depth; i++) {
2511 el = path->p_node[i].el;
2512 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2513 rec = &el->l_recs[idx];
2514
2515 rec->e_int_clusters = cpu_to_le32(range);
2516 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2517
2518 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2519 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002520out:
2521 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002522}
2523
Joel Becker6641b0c2009-02-12 18:57:52 -08002524static void ocfs2_unlink_path(handle_t *handle,
2525 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002526 struct ocfs2_cached_dealloc_ctxt *dealloc,
2527 struct ocfs2_path *path, int unlink_start)
2528{
2529 int ret, i;
2530 struct ocfs2_extent_block *eb;
2531 struct ocfs2_extent_list *el;
2532 struct buffer_head *bh;
2533
2534 for(i = unlink_start; i < path_num_items(path); i++) {
2535 bh = path->p_node[i].bh;
2536
2537 eb = (struct ocfs2_extent_block *)bh->b_data;
2538 /*
2539 * Not all nodes might have had their final count
2540 * decremented by the caller - handle this here.
2541 */
2542 el = &eb->h_list;
2543 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2544 mlog(ML_ERROR,
2545 "Inode %llu, attempted to remove extent block "
2546 "%llu with %u records\n",
Joel Becker6641b0c2009-02-12 18:57:52 -08002547 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07002548 (unsigned long long)le64_to_cpu(eb->h_blkno),
2549 le16_to_cpu(el->l_next_free_rec));
2550
2551 ocfs2_journal_dirty(handle, bh);
Joel Becker6641b0c2009-02-12 18:57:52 -08002552 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002553 continue;
2554 }
2555
2556 el->l_next_free_rec = 0;
2557 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2558
2559 ocfs2_journal_dirty(handle, bh);
2560
2561 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2562 if (ret)
2563 mlog_errno(ret);
2564
Joel Becker6641b0c2009-02-12 18:57:52 -08002565 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002566 }
2567}
2568
Joel Becker6641b0c2009-02-12 18:57:52 -08002569static void ocfs2_unlink_subtree(handle_t *handle,
2570 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002571 struct ocfs2_path *left_path,
2572 struct ocfs2_path *right_path,
2573 int subtree_index,
2574 struct ocfs2_cached_dealloc_ctxt *dealloc)
2575{
2576 int i;
2577 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2578 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2579 struct ocfs2_extent_list *el;
2580 struct ocfs2_extent_block *eb;
2581
2582 el = path_leaf_el(left_path);
2583
2584 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2585
2586 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2587 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2588 break;
2589
2590 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2591
2592 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2593 le16_add_cpu(&root_el->l_next_free_rec, -1);
2594
2595 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2596 eb->h_next_leaf_blk = 0;
2597
2598 ocfs2_journal_dirty(handle, root_bh);
2599 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2600
Joel Becker6641b0c2009-02-12 18:57:52 -08002601 ocfs2_unlink_path(handle, et, dealloc, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002602 subtree_index + 1);
2603}
2604
Joel Becker1e2dd632009-02-12 19:45:28 -08002605static int ocfs2_rotate_subtree_left(handle_t *handle,
2606 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002607 struct ocfs2_path *left_path,
2608 struct ocfs2_path *right_path,
2609 int subtree_index,
2610 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker1e2dd632009-02-12 19:45:28 -08002611 int *deleted)
Mark Fasheh328d5752007-06-18 10:48:04 -07002612{
2613 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002614 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002615 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2616 struct ocfs2_extent_block *eb;
2617
2618 *deleted = 0;
2619
2620 right_leaf_el = path_leaf_el(right_path);
2621 left_leaf_el = path_leaf_el(left_path);
2622 root_bh = left_path->p_node[subtree_index].bh;
2623 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2624
2625 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2626 return 0;
2627
2628 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2629 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2630 /*
2631 * It's legal for us to proceed if the right leaf is
2632 * the rightmost one and it has an empty extent. There
2633 * are two cases to handle - whether the leaf will be
2634 * empty after removal or not. If the leaf isn't empty
2635 * then just remove the empty extent up front. The
2636 * next block will handle empty leaves by flagging
2637 * them for unlink.
2638 *
2639 * Non rightmost leaves will throw -EAGAIN and the
2640 * caller can manually move the subtree and retry.
2641 */
2642
2643 if (eb->h_next_leaf_blk != 0ULL)
2644 return -EAGAIN;
2645
2646 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002647 ret = ocfs2_journal_access_eb(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002648 path_leaf_bh(right_path),
2649 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002650 if (ret) {
2651 mlog_errno(ret);
2652 goto out;
2653 }
2654
2655 ocfs2_remove_empty_extent(right_leaf_el);
2656 } else
2657 right_has_empty = 1;
2658 }
2659
2660 if (eb->h_next_leaf_blk == 0ULL &&
2661 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2662 /*
2663 * We have to update i_last_eb_blk during the meta
2664 * data delete.
2665 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08002666 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07002667 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002668 if (ret) {
2669 mlog_errno(ret);
2670 goto out;
2671 }
2672
2673 del_right_subtree = 1;
2674 }
2675
2676 /*
2677 * Getting here with an empty extent in the right path implies
2678 * that it's the rightmost path and will be deleted.
2679 */
2680 BUG_ON(right_has_empty && !del_right_subtree);
2681
Joel Becker1e2dd632009-02-12 19:45:28 -08002682 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002683 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002684 if (ret) {
2685 mlog_errno(ret);
2686 goto out;
2687 }
2688
2689 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002690 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002691 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002692 if (ret) {
2693 mlog_errno(ret);
2694 goto out;
2695 }
2696
Joel Becker1e2dd632009-02-12 19:45:28 -08002697 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002698 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002699 if (ret) {
2700 mlog_errno(ret);
2701 goto out;
2702 }
2703 }
2704
2705 if (!right_has_empty) {
2706 /*
2707 * Only do this if we're moving a real
2708 * record. Otherwise, the action is delayed until
2709 * after removal of the right path in which case we
2710 * can do a simple shift to remove the empty extent.
2711 */
2712 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2713 memset(&right_leaf_el->l_recs[0], 0,
2714 sizeof(struct ocfs2_extent_rec));
2715 }
2716 if (eb->h_next_leaf_blk == 0ULL) {
2717 /*
2718 * Move recs over to get rid of empty extent, decrease
2719 * next_free. This is allowed to remove the last
2720 * extent in our leaf (setting l_next_free_rec to
2721 * zero) - the delete code below won't care.
2722 */
2723 ocfs2_remove_empty_extent(right_leaf_el);
2724 }
2725
2726 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2727 if (ret)
2728 mlog_errno(ret);
2729 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2730 if (ret)
2731 mlog_errno(ret);
2732
2733 if (del_right_subtree) {
Joel Becker6641b0c2009-02-12 18:57:52 -08002734 ocfs2_unlink_subtree(handle, et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002735 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08002736 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08002737 left_path);
2738 if (ret) {
2739 mlog_errno(ret);
2740 goto out;
2741 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002742
2743 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002744 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002745
2746 /*
2747 * Removal of the extent in the left leaf was skipped
2748 * above so we could delete the right path
2749 * 1st.
2750 */
2751 if (right_has_empty)
2752 ocfs2_remove_empty_extent(left_leaf_el);
2753
Tao Mae7d4cb62008-08-18 17:38:44 +08002754 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002755 if (ret)
2756 mlog_errno(ret);
2757
2758 *deleted = 1;
2759 } else
Joel Becker4619c732009-02-12 19:02:36 -08002760 ocfs2_complete_edge_insert(handle, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002761 subtree_index);
2762
2763out:
2764 return ret;
2765}
2766
2767/*
2768 * Given a full path, determine what cpos value would return us a path
2769 * containing the leaf immediately to the right of the current one.
2770 *
2771 * Will return zero if the path passed in is already the rightmost path.
2772 *
2773 * This looks similar, but is subtly different to
2774 * ocfs2_find_cpos_for_left_leaf().
2775 */
2776static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2777 struct ocfs2_path *path, u32 *cpos)
2778{
2779 int i, j, ret = 0;
2780 u64 blkno;
2781 struct ocfs2_extent_list *el;
2782
2783 *cpos = 0;
2784
2785 if (path->p_tree_depth == 0)
2786 return 0;
2787
2788 blkno = path_leaf_bh(path)->b_blocknr;
2789
2790 /* Start at the tree node just above the leaf and work our way up. */
2791 i = path->p_tree_depth - 1;
2792 while (i >= 0) {
2793 int next_free;
2794
2795 el = path->p_node[i].el;
2796
2797 /*
2798 * Find the extent record just after the one in our
2799 * path.
2800 */
2801 next_free = le16_to_cpu(el->l_next_free_rec);
2802 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2803 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2804 if (j == (next_free - 1)) {
2805 if (i == 0) {
2806 /*
2807 * We've determined that the
2808 * path specified is already
2809 * the rightmost one - return a
2810 * cpos of zero.
2811 */
2812 goto out;
2813 }
2814 /*
2815 * The rightmost record points to our
2816 * leaf - we need to travel up the
2817 * tree one level.
2818 */
2819 goto next_node;
2820 }
2821
2822 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2823 goto out;
2824 }
2825 }
2826
2827 /*
2828 * If we got here, we never found a valid node where
2829 * the tree indicated one should be.
2830 */
2831 ocfs2_error(sb,
2832 "Invalid extent tree at extent block %llu\n",
2833 (unsigned long long)blkno);
2834 ret = -EROFS;
2835 goto out;
2836
2837next_node:
2838 blkno = path->p_node[i].bh->b_blocknr;
2839 i--;
2840 }
2841
2842out:
2843 return ret;
2844}
2845
Joel Becker70f18c02009-02-13 02:09:31 -08002846static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2847 struct ocfs2_extent_tree *et,
Joel Becker13723d02008-10-17 19:25:01 -07002848 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002849{
2850 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002851 struct buffer_head *bh = path_leaf_bh(path);
2852 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002853
2854 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2855 return 0;
2856
Joel Becker70f18c02009-02-13 02:09:31 -08002857 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Joel Becker13723d02008-10-17 19:25:01 -07002858 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002859 if (ret) {
2860 mlog_errno(ret);
2861 goto out;
2862 }
2863
2864 ocfs2_remove_empty_extent(el);
2865
2866 ret = ocfs2_journal_dirty(handle, bh);
2867 if (ret)
2868 mlog_errno(ret);
2869
2870out:
2871 return ret;
2872}
2873
Joel Beckere46f74d2009-02-12 19:47:43 -08002874static int __ocfs2_rotate_tree_left(handle_t *handle,
2875 struct ocfs2_extent_tree *et,
2876 int orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07002877 struct ocfs2_path *path,
2878 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08002879 struct ocfs2_path **empty_extent_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002880{
2881 int ret, subtree_root, deleted;
2882 u32 right_cpos;
2883 struct ocfs2_path *left_path = NULL;
2884 struct ocfs2_path *right_path = NULL;
Joel Beckere46f74d2009-02-12 19:47:43 -08002885 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fasheh328d5752007-06-18 10:48:04 -07002886
2887 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2888
2889 *empty_extent_path = NULL;
2890
Joel Beckere46f74d2009-02-12 19:47:43 -08002891 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002892 if (ret) {
2893 mlog_errno(ret);
2894 goto out;
2895 }
2896
Joel Beckerffdd7a52008-10-17 22:32:01 -07002897 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002898 if (!left_path) {
2899 ret = -ENOMEM;
2900 mlog_errno(ret);
2901 goto out;
2902 }
2903
2904 ocfs2_cp_path(left_path, path);
2905
Joel Beckerffdd7a52008-10-17 22:32:01 -07002906 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002907 if (!right_path) {
2908 ret = -ENOMEM;
2909 mlog_errno(ret);
2910 goto out;
2911 }
2912
2913 while (right_cpos) {
Joel Beckerfacdb772009-02-12 18:08:48 -08002914 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002915 if (ret) {
2916 mlog_errno(ret);
2917 goto out;
2918 }
2919
Joel Becker7dc02802009-02-12 19:20:13 -08002920 subtree_root = ocfs2_find_subtree_root(et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002921 right_path);
2922
2923 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2924 subtree_root,
2925 (unsigned long long)
2926 right_path->p_node[subtree_root].bh->b_blocknr,
2927 right_path->p_tree_depth);
2928
2929 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2930 orig_credits, left_path);
2931 if (ret) {
2932 mlog_errno(ret);
2933 goto out;
2934 }
2935
Mark Fashehe8aed342007-12-03 16:43:01 -08002936 /*
2937 * Caller might still want to make changes to the
2938 * tree root, so re-add it to the journal here.
2939 */
Joel Beckere46f74d2009-02-12 19:47:43 -08002940 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002941 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08002942 if (ret) {
2943 mlog_errno(ret);
2944 goto out;
2945 }
2946
Joel Becker1e2dd632009-02-12 19:45:28 -08002947 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002948 right_path, subtree_root,
Joel Becker1e2dd632009-02-12 19:45:28 -08002949 dealloc, &deleted);
Mark Fasheh328d5752007-06-18 10:48:04 -07002950 if (ret == -EAGAIN) {
2951 /*
2952 * The rotation has to temporarily stop due to
2953 * the right subtree having an empty
2954 * extent. Pass it back to the caller for a
2955 * fixup.
2956 */
2957 *empty_extent_path = right_path;
2958 right_path = NULL;
2959 goto out;
2960 }
2961 if (ret) {
2962 mlog_errno(ret);
2963 goto out;
2964 }
2965
2966 /*
2967 * The subtree rotate might have removed records on
2968 * the rightmost edge. If so, then rotation is
2969 * complete.
2970 */
2971 if (deleted)
2972 break;
2973
2974 ocfs2_mv_path(left_path, right_path);
2975
Joel Beckere46f74d2009-02-12 19:47:43 -08002976 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002977 &right_cpos);
2978 if (ret) {
2979 mlog_errno(ret);
2980 goto out;
2981 }
2982 }
2983
2984out:
2985 ocfs2_free_path(right_path);
2986 ocfs2_free_path(left_path);
2987
2988 return ret;
2989}
2990
Joel Becker70f18c02009-02-13 02:09:31 -08002991static int ocfs2_remove_rightmost_path(handle_t *handle,
2992 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08002993 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08002994 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07002995{
2996 int ret, subtree_index;
2997 u32 cpos;
2998 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07002999 struct ocfs2_extent_block *eb;
3000 struct ocfs2_extent_list *el;
3001
Mark Fasheh328d5752007-06-18 10:48:04 -07003002
Joel Becker6136ca52009-02-12 19:32:43 -08003003 ret = ocfs2_et_sanity_check(et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003004 if (ret)
3005 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003006 /*
3007 * There's two ways we handle this depending on
3008 * whether path is the only existing one.
3009 */
3010 ret = ocfs2_extend_rotate_transaction(handle, 0,
3011 handle->h_buffer_credits,
3012 path);
3013 if (ret) {
3014 mlog_errno(ret);
3015 goto out;
3016 }
3017
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003018 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003019 if (ret) {
3020 mlog_errno(ret);
3021 goto out;
3022 }
3023
Joel Becker3d03a302009-02-12 17:49:26 -08003024 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3025 path, &cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003026 if (ret) {
3027 mlog_errno(ret);
3028 goto out;
3029 }
3030
3031 if (cpos) {
3032 /*
3033 * We have a path to the left of this one - it needs
3034 * an update too.
3035 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003036 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003037 if (!left_path) {
3038 ret = -ENOMEM;
3039 mlog_errno(ret);
3040 goto out;
3041 }
3042
Joel Beckerfacdb772009-02-12 18:08:48 -08003043 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003044 if (ret) {
3045 mlog_errno(ret);
3046 goto out;
3047 }
3048
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003049 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003050 if (ret) {
3051 mlog_errno(ret);
3052 goto out;
3053 }
3054
Joel Becker7dc02802009-02-12 19:20:13 -08003055 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003056
Joel Becker6641b0c2009-02-12 18:57:52 -08003057 ocfs2_unlink_subtree(handle, et, left_path, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003058 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08003059 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08003060 left_path);
3061 if (ret) {
3062 mlog_errno(ret);
3063 goto out;
3064 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003065
3066 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003067 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003068 } else {
3069 /*
3070 * 'path' is also the leftmost path which
3071 * means it must be the only one. This gets
3072 * handled differently because we want to
Joel Becker70f18c02009-02-13 02:09:31 -08003073 * revert the root back to having extents
Mark Fasheh328d5752007-06-18 10:48:04 -07003074 * in-line.
3075 */
Joel Becker6641b0c2009-02-12 18:57:52 -08003076 ocfs2_unlink_path(handle, et, dealloc, path, 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003077
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003078 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003079 el->l_tree_depth = 0;
3080 el->l_next_free_rec = 0;
3081 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3082
Joel Becker35dc0aa2008-08-20 16:25:06 -07003083 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003084 }
3085
3086 ocfs2_journal_dirty(handle, path_root_bh(path));
3087
3088out:
3089 ocfs2_free_path(left_path);
3090 return ret;
3091}
3092
3093/*
3094 * Left rotation of btree records.
3095 *
3096 * In many ways, this is (unsurprisingly) the opposite of right
3097 * rotation. We start at some non-rightmost path containing an empty
3098 * extent in the leaf block. The code works its way to the rightmost
3099 * path by rotating records to the left in every subtree.
3100 *
3101 * This is used by any code which reduces the number of extent records
3102 * in a leaf. After removal, an empty record should be placed in the
3103 * leftmost list position.
3104 *
3105 * This won't handle a length update of the rightmost path records if
3106 * the rightmost tree leaf record is removed so the caller is
3107 * responsible for detecting and correcting that.
3108 */
Joel Becker70f18c02009-02-13 02:09:31 -08003109static int ocfs2_rotate_tree_left(handle_t *handle,
3110 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003111 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003112 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003113{
3114 int ret, orig_credits = handle->h_buffer_credits;
3115 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3116 struct ocfs2_extent_block *eb;
3117 struct ocfs2_extent_list *el;
3118
3119 el = path_leaf_el(path);
3120 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3121 return 0;
3122
3123 if (path->p_tree_depth == 0) {
3124rightmost_no_delete:
3125 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003126 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003127 * it up front.
3128 */
Joel Becker70f18c02009-02-13 02:09:31 -08003129 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003130 if (ret)
3131 mlog_errno(ret);
3132 goto out;
3133 }
3134
3135 /*
3136 * Handle rightmost branch now. There's several cases:
3137 * 1) simple rotation leaving records in there. That's trivial.
3138 * 2) rotation requiring a branch delete - there's no more
3139 * records left. Two cases of this:
3140 * a) There are branches to the left.
3141 * b) This is also the leftmost (the only) branch.
3142 *
3143 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3144 * 2a) we need the left branch so that we can update it with the unlink
Joel Becker70f18c02009-02-13 02:09:31 -08003145 * 2b) we need to bring the root back to inline extents.
Mark Fasheh328d5752007-06-18 10:48:04 -07003146 */
3147
3148 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3149 el = &eb->h_list;
3150 if (eb->h_next_leaf_blk == 0) {
3151 /*
3152 * This gets a bit tricky if we're going to delete the
3153 * rightmost path. Get the other cases out of the way
3154 * 1st.
3155 */
3156 if (le16_to_cpu(el->l_next_free_rec) > 1)
3157 goto rightmost_no_delete;
3158
3159 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3160 ret = -EIO;
Joel Becker70f18c02009-02-13 02:09:31 -08003161 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3162 "Owner %llu has empty extent block at %llu",
3163 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07003164 (unsigned long long)le64_to_cpu(eb->h_blkno));
3165 goto out;
3166 }
3167
3168 /*
3169 * XXX: The caller can not trust "path" any more after
3170 * this as it will have been deleted. What do we do?
3171 *
3172 * In theory the rotate-for-merge code will never get
3173 * here because it'll always ask for a rotate in a
3174 * nonempty list.
3175 */
3176
Joel Becker70f18c02009-02-13 02:09:31 -08003177 ret = ocfs2_remove_rightmost_path(handle, et, path,
3178 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003179 if (ret)
3180 mlog_errno(ret);
3181 goto out;
3182 }
3183
3184 /*
3185 * Now we can loop, remembering the path we get from -EAGAIN
3186 * and restarting from there.
3187 */
3188try_rotate:
Joel Beckere46f74d2009-02-12 19:47:43 -08003189 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3190 dealloc, &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003191 if (ret && ret != -EAGAIN) {
3192 mlog_errno(ret);
3193 goto out;
3194 }
3195
3196 while (ret == -EAGAIN) {
3197 tmp_path = restart_path;
3198 restart_path = NULL;
3199
Joel Beckere46f74d2009-02-12 19:47:43 -08003200 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07003201 tmp_path, dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08003202 &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003203 if (ret && ret != -EAGAIN) {
3204 mlog_errno(ret);
3205 goto out;
3206 }
3207
3208 ocfs2_free_path(tmp_path);
3209 tmp_path = NULL;
3210
3211 if (ret == 0)
3212 goto try_rotate;
3213 }
3214
3215out:
3216 ocfs2_free_path(tmp_path);
3217 ocfs2_free_path(restart_path);
3218 return ret;
3219}
3220
3221static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3222 int index)
3223{
3224 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3225 unsigned int size;
3226
3227 if (rec->e_leaf_clusters == 0) {
3228 /*
3229 * We consumed all of the merged-from record. An empty
3230 * extent cannot exist anywhere but the 1st array
3231 * position, so move things over if the merged-from
3232 * record doesn't occupy that position.
3233 *
3234 * This creates a new empty extent so the caller
3235 * should be smart enough to have removed any existing
3236 * ones.
3237 */
3238 if (index > 0) {
3239 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3240 size = index * sizeof(struct ocfs2_extent_rec);
3241 memmove(&el->l_recs[1], &el->l_recs[0], size);
3242 }
3243
3244 /*
3245 * Always memset - the caller doesn't check whether it
3246 * created an empty extent, so there could be junk in
3247 * the other fields.
3248 */
3249 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3250 }
3251}
3252
Joel Becker4fe82c32009-02-13 02:16:08 -08003253static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003254 struct ocfs2_path *left_path,
3255 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003256{
3257 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003258 u32 right_cpos;
3259 struct ocfs2_path *right_path = NULL;
3260 struct ocfs2_extent_list *left_el;
3261
3262 *ret_right_path = NULL;
3263
3264 /* This function shouldn't be called for non-trees. */
3265 BUG_ON(left_path->p_tree_depth == 0);
3266
3267 left_el = path_leaf_el(left_path);
3268 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3269
Joel Becker4fe82c32009-02-13 02:16:08 -08003270 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3271 left_path, &right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003272 if (ret) {
3273 mlog_errno(ret);
3274 goto out;
3275 }
3276
3277 /* This function shouldn't be called for the rightmost leaf. */
3278 BUG_ON(right_cpos == 0);
3279
Joel Beckerffdd7a52008-10-17 22:32:01 -07003280 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003281 if (!right_path) {
3282 ret = -ENOMEM;
3283 mlog_errno(ret);
3284 goto out;
3285 }
3286
Joel Becker4fe82c32009-02-13 02:16:08 -08003287 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003288 if (ret) {
3289 mlog_errno(ret);
3290 goto out;
3291 }
3292
3293 *ret_right_path = right_path;
3294out:
3295 if (ret)
3296 ocfs2_free_path(right_path);
3297 return ret;
3298}
3299
3300/*
3301 * Remove split_rec clusters from the record at index and merge them
3302 * onto the beginning of the record "next" to it.
3303 * For index < l_count - 1, the next means the extent rec at index + 1.
3304 * For index == l_count - 1, the "next" means the 1st extent rec of the
3305 * next extent block.
3306 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003307static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003308 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003309 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003310 struct ocfs2_extent_rec *split_rec,
3311 int index)
3312{
3313 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003314 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3315 struct ocfs2_extent_rec *left_rec;
3316 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003317 struct ocfs2_extent_list *right_el;
3318 struct ocfs2_path *right_path = NULL;
3319 int subtree_index = 0;
3320 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3321 struct buffer_head *bh = path_leaf_bh(left_path);
3322 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003323
3324 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003325 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003326
Al Viro9d8df6a2008-05-21 06:32:11 +01003327 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003328 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3329 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003330 ret = ocfs2_get_right_path(et, left_path, &right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003331 if (ret) {
3332 mlog_errno(ret);
3333 goto out;
3334 }
3335
3336 right_el = path_leaf_el(right_path);
3337 next_free = le16_to_cpu(right_el->l_next_free_rec);
3338 BUG_ON(next_free <= 0);
3339 right_rec = &right_el->l_recs[0];
3340 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003341 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003342 right_rec = &right_el->l_recs[1];
3343 }
3344
3345 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3346 le16_to_cpu(left_rec->e_leaf_clusters) !=
3347 le32_to_cpu(right_rec->e_cpos));
3348
Joel Becker7dc02802009-02-12 19:20:13 -08003349 subtree_index = ocfs2_find_subtree_root(et, left_path,
3350 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003351
3352 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3353 handle->h_buffer_credits,
3354 right_path);
3355 if (ret) {
3356 mlog_errno(ret);
3357 goto out;
3358 }
3359
3360 root_bh = left_path->p_node[subtree_index].bh;
3361 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3362
Joel Becker7dc02802009-02-12 19:20:13 -08003363 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003364 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003365 if (ret) {
3366 mlog_errno(ret);
3367 goto out;
3368 }
3369
3370 for (i = subtree_index + 1;
3371 i < path_num_items(right_path); i++) {
Joel Becker7dc02802009-02-12 19:20:13 -08003372 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003373 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003374 if (ret) {
3375 mlog_errno(ret);
3376 goto out;
3377 }
3378
Joel Becker7dc02802009-02-12 19:20:13 -08003379 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003380 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003381 if (ret) {
3382 mlog_errno(ret);
3383 goto out;
3384 }
3385 }
3386
3387 } else {
3388 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3389 right_rec = &el->l_recs[index + 1];
3390 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003391
Joel Becker7dc02802009-02-12 19:20:13 -08003392 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
Joel Becker13723d02008-10-17 19:25:01 -07003393 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003394 if (ret) {
3395 mlog_errno(ret);
3396 goto out;
3397 }
3398
3399 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3400
3401 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3402 le64_add_cpu(&right_rec->e_blkno,
Joel Becker7dc02802009-02-12 19:20:13 -08003403 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3404 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003405 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3406
3407 ocfs2_cleanup_merge(el, index);
3408
3409 ret = ocfs2_journal_dirty(handle, bh);
3410 if (ret)
3411 mlog_errno(ret);
3412
Tao Ma677b9752008-01-30 14:21:05 +08003413 if (right_path) {
3414 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3415 if (ret)
3416 mlog_errno(ret);
3417
Joel Becker4619c732009-02-12 19:02:36 -08003418 ocfs2_complete_edge_insert(handle, left_path, right_path,
3419 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003420 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003421out:
Tao Ma677b9752008-01-30 14:21:05 +08003422 if (right_path)
3423 ocfs2_free_path(right_path);
3424 return ret;
3425}
3426
Joel Becker4fe82c32009-02-13 02:16:08 -08003427static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003428 struct ocfs2_path *right_path,
3429 struct ocfs2_path **ret_left_path)
3430{
3431 int ret;
3432 u32 left_cpos;
3433 struct ocfs2_path *left_path = NULL;
3434
3435 *ret_left_path = NULL;
3436
3437 /* This function shouldn't be called for non-trees. */
3438 BUG_ON(right_path->p_tree_depth == 0);
3439
Joel Becker4fe82c32009-02-13 02:16:08 -08003440 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
Tao Ma677b9752008-01-30 14:21:05 +08003441 right_path, &left_cpos);
3442 if (ret) {
3443 mlog_errno(ret);
3444 goto out;
3445 }
3446
3447 /* This function shouldn't be called for the leftmost leaf. */
3448 BUG_ON(left_cpos == 0);
3449
Joel Beckerffdd7a52008-10-17 22:32:01 -07003450 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003451 if (!left_path) {
3452 ret = -ENOMEM;
3453 mlog_errno(ret);
3454 goto out;
3455 }
3456
Joel Becker4fe82c32009-02-13 02:16:08 -08003457 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003458 if (ret) {
3459 mlog_errno(ret);
3460 goto out;
3461 }
3462
3463 *ret_left_path = left_path;
3464out:
3465 if (ret)
3466 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003467 return ret;
3468}
3469
3470/*
3471 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003472 * onto the tail of the record "before" it.
3473 * For index > 0, the "before" means the extent rec at index - 1.
3474 *
3475 * For index == 0, the "before" means the last record of the previous
3476 * extent block. And there is also a situation that we may need to
3477 * remove the rightmost leaf extent block in the right_path and change
3478 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003479 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003480static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003481 handle_t *handle,
Joel Becker4fe82c32009-02-13 02:16:08 -08003482 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003483 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003484 struct ocfs2_cached_dealloc_ctxt *dealloc,
3485 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003486{
Tao Ma677b9752008-01-30 14:21:05 +08003487 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003488 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3489 struct ocfs2_extent_rec *left_rec;
3490 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003491 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3492 struct buffer_head *bh = path_leaf_bh(right_path);
3493 struct buffer_head *root_bh = NULL;
3494 struct ocfs2_path *left_path = NULL;
3495 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003496
Tao Ma677b9752008-01-30 14:21:05 +08003497 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003498
Mark Fasheh328d5752007-06-18 10:48:04 -07003499 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003500 if (index == 0) {
3501 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003502 ret = ocfs2_get_left_path(et, right_path, &left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003503 if (ret) {
3504 mlog_errno(ret);
3505 goto out;
3506 }
3507
3508 left_el = path_leaf_el(left_path);
3509 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3510 le16_to_cpu(left_el->l_count));
3511
3512 left_rec = &left_el->l_recs[
3513 le16_to_cpu(left_el->l_next_free_rec) - 1];
3514 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3515 le16_to_cpu(left_rec->e_leaf_clusters) !=
3516 le32_to_cpu(split_rec->e_cpos));
3517
Joel Becker7dc02802009-02-12 19:20:13 -08003518 subtree_index = ocfs2_find_subtree_root(et, left_path,
3519 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003520
3521 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3522 handle->h_buffer_credits,
3523 left_path);
3524 if (ret) {
3525 mlog_errno(ret);
3526 goto out;
3527 }
3528
3529 root_bh = left_path->p_node[subtree_index].bh;
3530 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3531
Joel Becker4fe82c32009-02-13 02:16:08 -08003532 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003533 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003534 if (ret) {
3535 mlog_errno(ret);
3536 goto out;
3537 }
3538
3539 for (i = subtree_index + 1;
3540 i < path_num_items(right_path); i++) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003541 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003542 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003543 if (ret) {
3544 mlog_errno(ret);
3545 goto out;
3546 }
3547
Joel Becker4fe82c32009-02-13 02:16:08 -08003548 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003549 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003550 if (ret) {
3551 mlog_errno(ret);
3552 goto out;
3553 }
3554 }
3555 } else {
3556 left_rec = &el->l_recs[index - 1];
3557 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3558 has_empty_extent = 1;
3559 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003560
Joel Becker4fe82c32009-02-13 02:16:08 -08003561 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Tao Ma9047bea2009-01-05 14:45:24 +08003562 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003563 if (ret) {
3564 mlog_errno(ret);
3565 goto out;
3566 }
3567
3568 if (has_empty_extent && index == 1) {
3569 /*
3570 * The easy case - we can just plop the record right in.
3571 */
3572 *left_rec = *split_rec;
3573
3574 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003575 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003576 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003577
3578 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3579 le64_add_cpu(&right_rec->e_blkno,
Joel Becker4fe82c32009-02-13 02:16:08 -08003580 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3581 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003582 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3583
3584 ocfs2_cleanup_merge(el, index);
3585
3586 ret = ocfs2_journal_dirty(handle, bh);
3587 if (ret)
3588 mlog_errno(ret);
3589
Tao Ma677b9752008-01-30 14:21:05 +08003590 if (left_path) {
3591 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3592 if (ret)
3593 mlog_errno(ret);
3594
3595 /*
3596 * In the situation that the right_rec is empty and the extent
3597 * block is empty also, ocfs2_complete_edge_insert can't handle
3598 * it and we need to delete the right extent block.
3599 */
3600 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3601 le16_to_cpu(el->l_next_free_rec) == 1) {
3602
Joel Becker70f18c02009-02-13 02:09:31 -08003603 ret = ocfs2_remove_rightmost_path(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003604 right_path,
Joel Becker70f18c02009-02-13 02:09:31 -08003605 dealloc);
Tao Ma677b9752008-01-30 14:21:05 +08003606 if (ret) {
3607 mlog_errno(ret);
3608 goto out;
3609 }
3610
3611 /* Now the rightmost extent block has been deleted.
3612 * So we use the new rightmost path.
3613 */
3614 ocfs2_mv_path(right_path, left_path);
3615 left_path = NULL;
3616 } else
Joel Becker4619c732009-02-12 19:02:36 -08003617 ocfs2_complete_edge_insert(handle, left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003618 right_path, subtree_index);
3619 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003620out:
Tao Ma677b9752008-01-30 14:21:05 +08003621 if (left_path)
3622 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003623 return ret;
3624}
3625
Joel Beckerc495dd22009-02-13 02:19:11 -08003626static int ocfs2_try_to_merge_extent(handle_t *handle,
3627 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003628 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003629 int split_index,
3630 struct ocfs2_extent_rec *split_rec,
3631 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckerc495dd22009-02-13 02:19:11 -08003632 struct ocfs2_merge_ctxt *ctxt)
Mark Fasheh328d5752007-06-18 10:48:04 -07003633{
Tao Mao518d7262007-08-28 17:25:35 -07003634 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003635 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003636 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3637
3638 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3639
Tao Mao518d7262007-08-28 17:25:35 -07003640 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3641 /*
3642 * The merge code will need to create an empty
3643 * extent to take the place of the newly
3644 * emptied slot. Remove any pre-existing empty
3645 * extents - having more than one in a leaf is
3646 * illegal.
3647 */
Joel Becker70f18c02009-02-13 02:09:31 -08003648 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Tao Mao518d7262007-08-28 17:25:35 -07003649 if (ret) {
3650 mlog_errno(ret);
3651 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003652 }
Tao Mao518d7262007-08-28 17:25:35 -07003653 split_index--;
3654 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003655 }
3656
3657 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3658 /*
3659 * Left-right contig implies this.
3660 */
3661 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003662
3663 /*
3664 * Since the leftright insert always covers the entire
3665 * extent, this call will delete the insert record
3666 * entirely, resulting in an empty extent record added to
3667 * the extent block.
3668 *
3669 * Since the adding of an empty extent shifts
3670 * everything back to the right, there's no need to
3671 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003672 *
3673 * When the split_index is zero, we need to merge it to the
3674 * prevoius extent block. It is more efficient and easier
3675 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003676 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003677 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003678 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003679 if (ret) {
3680 mlog_errno(ret);
3681 goto out;
3682 }
3683
3684 /*
3685 * We can only get this from logic error above.
3686 */
3687 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3688
Tao Ma677b9752008-01-30 14:21:05 +08003689 /* The merge left us with an empty extent, remove it. */
Joel Becker70f18c02009-02-13 02:09:31 -08003690 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003691 if (ret) {
3692 mlog_errno(ret);
3693 goto out;
3694 }
Tao Ma677b9752008-01-30 14:21:05 +08003695
Mark Fasheh328d5752007-06-18 10:48:04 -07003696 rec = &el->l_recs[split_index];
3697
3698 /*
3699 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003700 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003701 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003702 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3703 dealloc, split_index);
Tao Ma677b9752008-01-30 14:21:05 +08003704
Mark Fasheh328d5752007-06-18 10:48:04 -07003705 if (ret) {
3706 mlog_errno(ret);
3707 goto out;
3708 }
3709
Joel Becker70f18c02009-02-13 02:09:31 -08003710 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003711 /*
3712 * Error from this last rotate is not critical, so
3713 * print but don't bubble it up.
3714 */
3715 if (ret)
3716 mlog_errno(ret);
3717 ret = 0;
3718 } else {
3719 /*
3720 * Merge a record to the left or right.
3721 *
3722 * 'contig_type' is relative to the existing record,
3723 * so for example, if we're "right contig", it's to
3724 * the record on the left (hence the left merge).
3725 */
3726 if (ctxt->c_contig_type == CONTIG_RIGHT) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003727 ret = ocfs2_merge_rec_left(path, handle, et,
3728 split_rec, dealloc,
Mark Fasheh328d5752007-06-18 10:48:04 -07003729 split_index);
3730 if (ret) {
3731 mlog_errno(ret);
3732 goto out;
3733 }
3734 } else {
Joel Becker4fe82c32009-02-13 02:16:08 -08003735 ret = ocfs2_merge_rec_right(path, handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003736 et, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003737 split_index);
3738 if (ret) {
3739 mlog_errno(ret);
3740 goto out;
3741 }
3742 }
3743
3744 if (ctxt->c_split_covers_rec) {
3745 /*
3746 * The merge may have left an empty extent in
3747 * our leaf. Try to rotate it away.
3748 */
Joel Becker70f18c02009-02-13 02:09:31 -08003749 ret = ocfs2_rotate_tree_left(handle, et, path,
3750 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003751 if (ret)
3752 mlog_errno(ret);
3753 ret = 0;
3754 }
3755 }
3756
3757out:
3758 return ret;
3759}
3760
3761static void ocfs2_subtract_from_rec(struct super_block *sb,
3762 enum ocfs2_split_type split,
3763 struct ocfs2_extent_rec *rec,
3764 struct ocfs2_extent_rec *split_rec)
3765{
3766 u64 len_blocks;
3767
3768 len_blocks = ocfs2_clusters_to_blocks(sb,
3769 le16_to_cpu(split_rec->e_leaf_clusters));
3770
3771 if (split == SPLIT_LEFT) {
3772 /*
3773 * Region is on the left edge of the existing
3774 * record.
3775 */
3776 le32_add_cpu(&rec->e_cpos,
3777 le16_to_cpu(split_rec->e_leaf_clusters));
3778 le64_add_cpu(&rec->e_blkno, len_blocks);
3779 le16_add_cpu(&rec->e_leaf_clusters,
3780 -le16_to_cpu(split_rec->e_leaf_clusters));
3781 } else {
3782 /*
3783 * Region is on the right edge of the existing
3784 * record.
3785 */
3786 le16_add_cpu(&rec->e_leaf_clusters,
3787 -le16_to_cpu(split_rec->e_leaf_clusters));
3788 }
3789}
3790
Mark Fashehdcd05382007-01-16 11:32:23 -08003791/*
3792 * Do the final bits of extent record insertion at the target leaf
3793 * list. If this leaf is part of an allocation tree, it is assumed
3794 * that the tree above has been prepared.
3795 */
3796static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3797 struct ocfs2_extent_list *el,
3798 struct ocfs2_insert_type *insert,
3799 struct inode *inode)
3800{
3801 int i = insert->ins_contig_index;
3802 unsigned int range;
3803 struct ocfs2_extent_rec *rec;
3804
Mark Fashehe48edee2007-03-07 16:46:57 -08003805 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003806
Mark Fasheh328d5752007-06-18 10:48:04 -07003807 if (insert->ins_split != SPLIT_NONE) {
3808 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3809 BUG_ON(i == -1);
3810 rec = &el->l_recs[i];
3811 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3812 insert_rec);
3813 goto rotate;
3814 }
3815
Mark Fashehdcd05382007-01-16 11:32:23 -08003816 /*
3817 * Contiguous insert - either left or right.
3818 */
3819 if (insert->ins_contig != CONTIG_NONE) {
3820 rec = &el->l_recs[i];
3821 if (insert->ins_contig == CONTIG_LEFT) {
3822 rec->e_blkno = insert_rec->e_blkno;
3823 rec->e_cpos = insert_rec->e_cpos;
3824 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003825 le16_add_cpu(&rec->e_leaf_clusters,
3826 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003827 return;
3828 }
3829
3830 /*
3831 * Handle insert into an empty leaf.
3832 */
3833 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3834 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3835 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3836 el->l_recs[0] = *insert_rec;
3837 el->l_next_free_rec = cpu_to_le16(1);
3838 return;
3839 }
3840
3841 /*
3842 * Appending insert.
3843 */
3844 if (insert->ins_appending == APPEND_TAIL) {
3845 i = le16_to_cpu(el->l_next_free_rec) - 1;
3846 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003847 range = le32_to_cpu(rec->e_cpos)
3848 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003849 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3850
3851 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3852 le16_to_cpu(el->l_count),
3853 "inode %lu, depth %u, count %u, next free %u, "
3854 "rec.cpos %u, rec.clusters %u, "
3855 "insert.cpos %u, insert.clusters %u\n",
3856 inode->i_ino,
3857 le16_to_cpu(el->l_tree_depth),
3858 le16_to_cpu(el->l_count),
3859 le16_to_cpu(el->l_next_free_rec),
3860 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003861 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003862 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003863 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003864 i++;
3865 el->l_recs[i] = *insert_rec;
3866 le16_add_cpu(&el->l_next_free_rec, 1);
3867 return;
3868 }
3869
Mark Fasheh328d5752007-06-18 10:48:04 -07003870rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003871 /*
3872 * Ok, we have to rotate.
3873 *
3874 * At this point, it is safe to assume that inserting into an
3875 * empty leaf and appending to a leaf have both been handled
3876 * above.
3877 *
3878 * This leaf needs to have space, either by the empty 1st
3879 * extent record, or by virtue of an l_next_rec < l_count.
3880 */
3881 ocfs2_rotate_leaf(el, insert_rec);
3882}
3883
Mark Fasheh328d5752007-06-18 10:48:04 -07003884static void ocfs2_adjust_rightmost_records(struct inode *inode,
3885 handle_t *handle,
3886 struct ocfs2_path *path,
3887 struct ocfs2_extent_rec *insert_rec)
3888{
3889 int ret, i, next_free;
3890 struct buffer_head *bh;
3891 struct ocfs2_extent_list *el;
3892 struct ocfs2_extent_rec *rec;
3893
3894 /*
3895 * Update everything except the leaf block.
3896 */
3897 for (i = 0; i < path->p_tree_depth; i++) {
3898 bh = path->p_node[i].bh;
3899 el = path->p_node[i].el;
3900
3901 next_free = le16_to_cpu(el->l_next_free_rec);
3902 if (next_free == 0) {
3903 ocfs2_error(inode->i_sb,
3904 "Dinode %llu has a bad extent list",
3905 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3906 ret = -EIO;
3907 return;
3908 }
3909
3910 rec = &el->l_recs[next_free - 1];
3911
3912 rec->e_int_clusters = insert_rec->e_cpos;
3913 le32_add_cpu(&rec->e_int_clusters,
3914 le16_to_cpu(insert_rec->e_leaf_clusters));
3915 le32_add_cpu(&rec->e_int_clusters,
3916 -le32_to_cpu(rec->e_cpos));
3917
3918 ret = ocfs2_journal_dirty(handle, bh);
3919 if (ret)
3920 mlog_errno(ret);
3921
3922 }
3923}
3924
Mark Fashehdcd05382007-01-16 11:32:23 -08003925static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3926 struct ocfs2_extent_rec *insert_rec,
3927 struct ocfs2_path *right_path,
3928 struct ocfs2_path **ret_left_path)
3929{
Mark Fasheh328d5752007-06-18 10:48:04 -07003930 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003931 struct ocfs2_extent_list *el;
3932 struct ocfs2_path *left_path = NULL;
3933
3934 *ret_left_path = NULL;
3935
3936 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003937 * This shouldn't happen for non-trees. The extent rec cluster
3938 * count manipulation below only works for interior nodes.
3939 */
3940 BUG_ON(right_path->p_tree_depth == 0);
3941
3942 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003943 * If our appending insert is at the leftmost edge of a leaf,
3944 * then we might need to update the rightmost records of the
3945 * neighboring path.
3946 */
3947 el = path_leaf_el(right_path);
3948 next_free = le16_to_cpu(el->l_next_free_rec);
3949 if (next_free == 0 ||
3950 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3951 u32 left_cpos;
3952
3953 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3954 &left_cpos);
3955 if (ret) {
3956 mlog_errno(ret);
3957 goto out;
3958 }
3959
3960 mlog(0, "Append may need a left path update. cpos: %u, "
3961 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3962 left_cpos);
3963
3964 /*
3965 * No need to worry if the append is already in the
3966 * leftmost leaf.
3967 */
3968 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07003969 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003970 if (!left_path) {
3971 ret = -ENOMEM;
3972 mlog_errno(ret);
3973 goto out;
3974 }
3975
Joel Beckerfacdb772009-02-12 18:08:48 -08003976 ret = ocfs2_find_path(INODE_CACHE(inode), left_path,
3977 left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08003978 if (ret) {
3979 mlog_errno(ret);
3980 goto out;
3981 }
3982
3983 /*
3984 * ocfs2_insert_path() will pass the left_path to the
3985 * journal for us.
3986 */
3987 }
3988 }
3989
Joel Becker0cf2f762009-02-12 16:41:25 -08003990 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003991 if (ret) {
3992 mlog_errno(ret);
3993 goto out;
3994 }
3995
Mark Fasheh328d5752007-06-18 10:48:04 -07003996 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08003997
3998 *ret_left_path = left_path;
3999 ret = 0;
4000out:
4001 if (ret != 0)
4002 ocfs2_free_path(left_path);
4003
4004 return ret;
4005}
4006
Mark Fasheh328d5752007-06-18 10:48:04 -07004007static void ocfs2_split_record(struct inode *inode,
4008 struct ocfs2_path *left_path,
4009 struct ocfs2_path *right_path,
4010 struct ocfs2_extent_rec *split_rec,
4011 enum ocfs2_split_type split)
4012{
4013 int index;
4014 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4015 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4016 struct ocfs2_extent_rec *rec, *tmprec;
4017
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004018 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004019 if (left_path)
4020 left_el = path_leaf_el(left_path);
4021
4022 el = right_el;
4023 insert_el = right_el;
4024 index = ocfs2_search_extent_list(el, cpos);
4025 if (index != -1) {
4026 if (index == 0 && left_path) {
4027 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4028
4029 /*
4030 * This typically means that the record
4031 * started in the left path but moved to the
4032 * right as a result of rotation. We either
4033 * move the existing record to the left, or we
4034 * do the later insert there.
4035 *
4036 * In this case, the left path should always
4037 * exist as the rotate code will have passed
4038 * it back for a post-insert update.
4039 */
4040
4041 if (split == SPLIT_LEFT) {
4042 /*
4043 * It's a left split. Since we know
4044 * that the rotate code gave us an
4045 * empty extent in the left path, we
4046 * can just do the insert there.
4047 */
4048 insert_el = left_el;
4049 } else {
4050 /*
4051 * Right split - we have to move the
4052 * existing record over to the left
4053 * leaf. The insert will be into the
4054 * newly created empty extent in the
4055 * right leaf.
4056 */
4057 tmprec = &right_el->l_recs[index];
4058 ocfs2_rotate_leaf(left_el, tmprec);
4059 el = left_el;
4060
4061 memset(tmprec, 0, sizeof(*tmprec));
4062 index = ocfs2_search_extent_list(left_el, cpos);
4063 BUG_ON(index == -1);
4064 }
4065 }
4066 } else {
4067 BUG_ON(!left_path);
4068 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4069 /*
4070 * Left path is easy - we can just allow the insert to
4071 * happen.
4072 */
4073 el = left_el;
4074 insert_el = left_el;
4075 index = ocfs2_search_extent_list(el, cpos);
4076 BUG_ON(index == -1);
4077 }
4078
4079 rec = &el->l_recs[index];
4080 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
4081 ocfs2_rotate_leaf(insert_el, split_rec);
4082}
4083
Mark Fashehdcd05382007-01-16 11:32:23 -08004084/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004085 * This function only does inserts on an allocation b-tree. For tree
4086 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004087 *
4088 * right_path is the path we want to do the actual insert
4089 * in. left_path should only be passed in if we need to update that
4090 * portion of the tree after an edge insert.
4091 */
4092static int ocfs2_insert_path(struct inode *inode,
4093 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08004094 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004095 struct ocfs2_path *left_path,
4096 struct ocfs2_path *right_path,
4097 struct ocfs2_extent_rec *insert_rec,
4098 struct ocfs2_insert_type *insert)
4099{
4100 int ret, subtree_index;
4101 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004102
Mark Fashehdcd05382007-01-16 11:32:23 -08004103 if (left_path) {
4104 int credits = handle->h_buffer_credits;
4105
4106 /*
4107 * There's a chance that left_path got passed back to
4108 * us without being accounted for in the
4109 * journal. Extend our transaction here to be sure we
4110 * can change those blocks.
4111 */
4112 credits += left_path->p_tree_depth;
4113
4114 ret = ocfs2_extend_trans(handle, credits);
4115 if (ret < 0) {
4116 mlog_errno(ret);
4117 goto out;
4118 }
4119
Joel Becker7dc02802009-02-12 19:20:13 -08004120 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004121 if (ret < 0) {
4122 mlog_errno(ret);
4123 goto out;
4124 }
4125 }
4126
Mark Fashehe8aed342007-12-03 16:43:01 -08004127 /*
4128 * Pass both paths to the journal. The majority of inserts
4129 * will be touching all components anyway.
4130 */
Joel Becker7dc02802009-02-12 19:20:13 -08004131 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004132 if (ret < 0) {
4133 mlog_errno(ret);
4134 goto out;
4135 }
4136
Mark Fasheh328d5752007-06-18 10:48:04 -07004137 if (insert->ins_split != SPLIT_NONE) {
4138 /*
4139 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004140 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004141 * function sort it all out.
4142 */
4143 ocfs2_split_record(inode, left_path, right_path,
4144 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004145
4146 /*
4147 * Split might have modified either leaf and we don't
4148 * have a guarantee that the later edge insert will
4149 * dirty this for us.
4150 */
4151 if (left_path)
4152 ret = ocfs2_journal_dirty(handle,
4153 path_leaf_bh(left_path));
4154 if (ret)
4155 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07004156 } else
4157 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
4158 insert, inode);
Mark Fashehdcd05382007-01-16 11:32:23 -08004159
Mark Fashehdcd05382007-01-16 11:32:23 -08004160 ret = ocfs2_journal_dirty(handle, leaf_bh);
4161 if (ret)
4162 mlog_errno(ret);
4163
4164 if (left_path) {
4165 /*
4166 * The rotate code has indicated that we need to fix
4167 * up portions of the tree after the insert.
4168 *
4169 * XXX: Should we extend the transaction here?
4170 */
Joel Becker7dc02802009-02-12 19:20:13 -08004171 subtree_index = ocfs2_find_subtree_root(et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004172 right_path);
Joel Becker4619c732009-02-12 19:02:36 -08004173 ocfs2_complete_edge_insert(handle, left_path, right_path,
4174 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08004175 }
4176
4177 ret = 0;
4178out:
4179 return ret;
4180}
4181
4182static int ocfs2_do_insert_extent(struct inode *inode,
4183 handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004184 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004185 struct ocfs2_extent_rec *insert_rec,
4186 struct ocfs2_insert_type *type)
4187{
4188 int ret, rotate = 0;
4189 u32 cpos;
4190 struct ocfs2_path *right_path = NULL;
4191 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004192 struct ocfs2_extent_list *el;
4193
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004194 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004195
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004196 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004197 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004198 if (ret) {
4199 mlog_errno(ret);
4200 goto out;
4201 }
4202
4203 if (le16_to_cpu(el->l_tree_depth) == 0) {
4204 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4205 goto out_update_clusters;
4206 }
4207
Joel Beckerffdd7a52008-10-17 22:32:01 -07004208 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004209 if (!right_path) {
4210 ret = -ENOMEM;
4211 mlog_errno(ret);
4212 goto out;
4213 }
4214
4215 /*
4216 * Determine the path to start with. Rotations need the
4217 * rightmost path, everything else can go directly to the
4218 * target leaf.
4219 */
4220 cpos = le32_to_cpu(insert_rec->e_cpos);
4221 if (type->ins_appending == APPEND_NONE &&
4222 type->ins_contig == CONTIG_NONE) {
4223 rotate = 1;
4224 cpos = UINT_MAX;
4225 }
4226
Joel Beckerfacdb772009-02-12 18:08:48 -08004227 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004228 if (ret) {
4229 mlog_errno(ret);
4230 goto out;
4231 }
4232
4233 /*
4234 * Rotations and appends need special treatment - they modify
4235 * parts of the tree's above them.
4236 *
4237 * Both might pass back a path immediate to the left of the
4238 * one being inserted to. This will be cause
4239 * ocfs2_insert_path() to modify the rightmost records of
4240 * left_path to account for an edge insert.
4241 *
4242 * XXX: When modifying this code, keep in mind that an insert
4243 * can wind up skipping both of these two special cases...
4244 */
4245 if (rotate) {
Joel Becker1bbf0b82009-02-12 19:42:08 -08004246 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004247 le32_to_cpu(insert_rec->e_cpos),
4248 right_path, &left_path);
4249 if (ret) {
4250 mlog_errno(ret);
4251 goto out;
4252 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004253
4254 /*
4255 * ocfs2_rotate_tree_right() might have extended the
4256 * transaction without re-journaling our tree root.
4257 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004258 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004259 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004260 if (ret) {
4261 mlog_errno(ret);
4262 goto out;
4263 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004264 } else if (type->ins_appending == APPEND_TAIL
4265 && type->ins_contig != CONTIG_LEFT) {
4266 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4267 right_path, &left_path);
4268 if (ret) {
4269 mlog_errno(ret);
4270 goto out;
4271 }
4272 }
4273
Joel Becker7dc02802009-02-12 19:20:13 -08004274 ret = ocfs2_insert_path(inode, handle, et, left_path, right_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004275 insert_rec, type);
4276 if (ret) {
4277 mlog_errno(ret);
4278 goto out;
4279 }
4280
4281out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004282 if (type->ins_split == SPLIT_NONE)
Joel Becker6136ca52009-02-12 19:32:43 -08004283 ocfs2_et_update_clusters(et,
Joel Becker35dc0aa2008-08-20 16:25:06 -07004284 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004285
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004286 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004287 if (ret)
4288 mlog_errno(ret);
4289
4290out:
4291 ocfs2_free_path(left_path);
4292 ocfs2_free_path(right_path);
4293
4294 return ret;
4295}
4296
Mark Fasheh328d5752007-06-18 10:48:04 -07004297static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08004298ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004299 struct ocfs2_extent_list *el, int index,
4300 struct ocfs2_extent_rec *split_rec)
4301{
Tao Maad5a4d72008-01-30 14:21:32 +08004302 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004303 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004304 u32 left_cpos, right_cpos;
4305 struct ocfs2_extent_rec *rec = NULL;
4306 struct ocfs2_extent_list *new_el;
4307 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4308 struct buffer_head *bh;
4309 struct ocfs2_extent_block *eb;
4310
4311 if (index > 0) {
4312 rec = &el->l_recs[index - 1];
4313 } else if (path->p_tree_depth > 0) {
4314 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4315 path, &left_cpos);
4316 if (status)
4317 goto out;
4318
4319 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004320 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004321 if (!left_path)
4322 goto out;
4323
Joel Beckerfacdb772009-02-12 18:08:48 -08004324 status = ocfs2_find_path(INODE_CACHE(inode),
4325 left_path, left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004326 if (status)
4327 goto out;
4328
4329 new_el = path_leaf_el(left_path);
4330
4331 if (le16_to_cpu(new_el->l_next_free_rec) !=
4332 le16_to_cpu(new_el->l_count)) {
4333 bh = path_leaf_bh(left_path);
4334 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004335 ocfs2_error(inode->i_sb,
4336 "Extent block #%llu has an "
4337 "invalid l_next_free_rec of "
4338 "%d. It should have "
4339 "matched the l_count of %d",
4340 (unsigned long long)le64_to_cpu(eb->h_blkno),
4341 le16_to_cpu(new_el->l_next_free_rec),
4342 le16_to_cpu(new_el->l_count));
4343 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004344 goto out;
4345 }
4346 rec = &new_el->l_recs[
4347 le16_to_cpu(new_el->l_next_free_rec) - 1];
4348 }
4349 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004350
4351 /*
4352 * We're careful to check for an empty extent record here -
4353 * the merge code will know what to do if it sees one.
4354 */
Tao Maad5a4d72008-01-30 14:21:32 +08004355 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004356 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4357 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4358 ret = CONTIG_RIGHT;
4359 } else {
4360 ret = ocfs2_extent_contig(inode, rec, split_rec);
4361 }
4362 }
4363
Tao Maad5a4d72008-01-30 14:21:32 +08004364 rec = NULL;
4365 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4366 rec = &el->l_recs[index + 1];
4367 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4368 path->p_tree_depth > 0) {
4369 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4370 path, &right_cpos);
4371 if (status)
4372 goto out;
4373
4374 if (right_cpos == 0)
4375 goto out;
4376
Joel Beckerffdd7a52008-10-17 22:32:01 -07004377 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004378 if (!right_path)
4379 goto out;
4380
Joel Beckerfacdb772009-02-12 18:08:48 -08004381 status = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004382 if (status)
4383 goto out;
4384
4385 new_el = path_leaf_el(right_path);
4386 rec = &new_el->l_recs[0];
4387 if (ocfs2_is_empty_extent(rec)) {
4388 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4389 bh = path_leaf_bh(right_path);
4390 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004391 ocfs2_error(inode->i_sb,
4392 "Extent block #%llu has an "
4393 "invalid l_next_free_rec of %d",
4394 (unsigned long long)le64_to_cpu(eb->h_blkno),
4395 le16_to_cpu(new_el->l_next_free_rec));
4396 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004397 goto out;
4398 }
4399 rec = &new_el->l_recs[1];
4400 }
4401 }
4402
4403 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004404 enum ocfs2_contig_type contig_type;
4405
Mark Fasheh328d5752007-06-18 10:48:04 -07004406 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4407
4408 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4409 ret = CONTIG_LEFTRIGHT;
4410 else if (ret == CONTIG_NONE)
4411 ret = contig_type;
4412 }
4413
Tao Maad5a4d72008-01-30 14:21:32 +08004414out:
4415 if (left_path)
4416 ocfs2_free_path(left_path);
4417 if (right_path)
4418 ocfs2_free_path(right_path);
4419
Mark Fasheh328d5752007-06-18 10:48:04 -07004420 return ret;
4421}
4422
Mark Fashehdcd05382007-01-16 11:32:23 -08004423static void ocfs2_figure_contig_type(struct inode *inode,
4424 struct ocfs2_insert_type *insert,
4425 struct ocfs2_extent_list *el,
Tao Maca12b7c2008-08-18 17:38:52 +08004426 struct ocfs2_extent_rec *insert_rec,
4427 struct ocfs2_extent_tree *et)
Mark Fashehdcd05382007-01-16 11:32:23 -08004428{
4429 int i;
4430 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4431
Mark Fashehe48edee2007-03-07 16:46:57 -08004432 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4433
Mark Fashehdcd05382007-01-16 11:32:23 -08004434 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4435 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4436 insert_rec);
4437 if (contig_type != CONTIG_NONE) {
4438 insert->ins_contig_index = i;
4439 break;
4440 }
4441 }
4442 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004443
4444 if (insert->ins_contig != CONTIG_NONE) {
4445 struct ocfs2_extent_rec *rec =
4446 &el->l_recs[insert->ins_contig_index];
4447 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4448 le16_to_cpu(insert_rec->e_leaf_clusters);
4449
4450 /*
4451 * Caller might want us to limit the size of extents, don't
4452 * calculate contiguousness if we might exceed that limit.
4453 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004454 if (et->et_max_leaf_clusters &&
4455 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004456 insert->ins_contig = CONTIG_NONE;
4457 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004458}
4459
4460/*
4461 * This should only be called against the righmost leaf extent list.
4462 *
4463 * ocfs2_figure_appending_type() will figure out whether we'll have to
4464 * insert at the tail of the rightmost leaf.
4465 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004466 * This should also work against the root extent list for tree's with 0
4467 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004468 * then the logic here makes sense.
4469 */
4470static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4471 struct ocfs2_extent_list *el,
4472 struct ocfs2_extent_rec *insert_rec)
4473{
4474 int i;
4475 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4476 struct ocfs2_extent_rec *rec;
4477
4478 insert->ins_appending = APPEND_NONE;
4479
Mark Fashehe48edee2007-03-07 16:46:57 -08004480 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004481
4482 if (!el->l_next_free_rec)
4483 goto set_tail_append;
4484
4485 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4486 /* Were all records empty? */
4487 if (le16_to_cpu(el->l_next_free_rec) == 1)
4488 goto set_tail_append;
4489 }
4490
4491 i = le16_to_cpu(el->l_next_free_rec) - 1;
4492 rec = &el->l_recs[i];
4493
Mark Fashehe48edee2007-03-07 16:46:57 -08004494 if (cpos >=
4495 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004496 goto set_tail_append;
4497
4498 return;
4499
4500set_tail_append:
4501 insert->ins_appending = APPEND_TAIL;
4502}
4503
4504/*
4505 * Helper function called at the begining of an insert.
4506 *
4507 * This computes a few things that are commonly used in the process of
4508 * inserting into the btree:
4509 * - Whether the new extent is contiguous with an existing one.
4510 * - The current tree depth.
4511 * - Whether the insert is an appending one.
4512 * - The total # of free records in the tree.
4513 *
4514 * All of the information is stored on the ocfs2_insert_type
4515 * structure.
4516 */
4517static int ocfs2_figure_insert_type(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004518 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004519 struct buffer_head **last_eb_bh,
4520 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004521 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004522 struct ocfs2_insert_type *insert)
4523{
4524 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004525 struct ocfs2_extent_block *eb;
4526 struct ocfs2_extent_list *el;
4527 struct ocfs2_path *path = NULL;
4528 struct buffer_head *bh = NULL;
4529
Mark Fasheh328d5752007-06-18 10:48:04 -07004530 insert->ins_split = SPLIT_NONE;
4531
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004532 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004533 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4534
4535 if (el->l_tree_depth) {
4536 /*
4537 * If we have tree depth, we read in the
4538 * rightmost extent block ahead of time as
4539 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4540 * may want it later.
4541 */
Joel Becker3d03a302009-02-12 17:49:26 -08004542 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08004543 ocfs2_et_get_last_eb_blk(et),
4544 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004545 if (ret) {
4546 mlog_exit(ret);
4547 goto out;
4548 }
4549 eb = (struct ocfs2_extent_block *) bh->b_data;
4550 el = &eb->h_list;
4551 }
4552
4553 /*
4554 * Unless we have a contiguous insert, we'll need to know if
4555 * there is room left in our allocation tree for another
4556 * extent record.
4557 *
4558 * XXX: This test is simplistic, we can search for empty
4559 * extent records too.
4560 */
Tao Maoc77534f2007-08-28 17:22:33 -07004561 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004562 le16_to_cpu(el->l_next_free_rec);
4563
4564 if (!insert->ins_tree_depth) {
Tao Maca12b7c2008-08-18 17:38:52 +08004565 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004566 ocfs2_figure_appending_type(insert, el, insert_rec);
4567 return 0;
4568 }
4569
Joel Beckerffdd7a52008-10-17 22:32:01 -07004570 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004571 if (!path) {
4572 ret = -ENOMEM;
4573 mlog_errno(ret);
4574 goto out;
4575 }
4576
4577 /*
4578 * In the case that we're inserting past what the tree
4579 * currently accounts for, ocfs2_find_path() will return for
4580 * us the rightmost tree path. This is accounted for below in
4581 * the appending code.
4582 */
Joel Beckerfacdb772009-02-12 18:08:48 -08004583 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -08004584 if (ret) {
4585 mlog_errno(ret);
4586 goto out;
4587 }
4588
4589 el = path_leaf_el(path);
4590
4591 /*
4592 * Now that we have the path, there's two things we want to determine:
4593 * 1) Contiguousness (also set contig_index if this is so)
4594 *
4595 * 2) Are we doing an append? We can trivially break this up
4596 * into two types of appends: simple record append, or a
4597 * rotate inside the tail leaf.
4598 */
Tao Maca12b7c2008-08-18 17:38:52 +08004599 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004600
4601 /*
4602 * The insert code isn't quite ready to deal with all cases of
4603 * left contiguousness. Specifically, if it's an insert into
4604 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004605 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004606 * left. For now, just catch that case and fool the layers
4607 * above us. This works just fine for tree_depth == 0, which
4608 * is why we allow that above.
4609 */
4610 if (insert->ins_contig == CONTIG_LEFT &&
4611 insert->ins_contig_index == 0)
4612 insert->ins_contig = CONTIG_NONE;
4613
4614 /*
4615 * Ok, so we can simply compare against last_eb to figure out
4616 * whether the path doesn't exist. This will only happen in
4617 * the case that we're doing a tail append, so maybe we can
4618 * take advantage of that information somehow.
4619 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004620 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004621 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004622 /*
4623 * Ok, ocfs2_find_path() returned us the rightmost
4624 * tree path. This might be an appending insert. There are
4625 * two cases:
4626 * 1) We're doing a true append at the tail:
4627 * -This might even be off the end of the leaf
4628 * 2) We're "appending" by rotating in the tail
4629 */
4630 ocfs2_figure_appending_type(insert, el, insert_rec);
4631 }
4632
4633out:
4634 ocfs2_free_path(path);
4635
4636 if (ret == 0)
4637 *last_eb_bh = bh;
4638 else
4639 brelse(bh);
4640 return ret;
4641}
4642
4643/*
4644 * Insert an extent into an inode btree.
4645 *
4646 * The caller needs to update fe->i_clusters
4647 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004648int ocfs2_insert_extent(struct ocfs2_super *osb,
4649 handle_t *handle,
4650 struct inode *inode,
4651 struct ocfs2_extent_tree *et,
4652 u32 cpos,
4653 u64 start_blk,
4654 u32 new_clusters,
4655 u8 flags,
4656 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004657{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004658 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004659 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004660 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004661 struct ocfs2_insert_type insert = {0, };
4662 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004663
Mark Fashehdcd05382007-01-16 11:32:23 -08004664 mlog(0, "add %u clusters at position %u to inode %llu\n",
4665 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004666
Mark Fashehe48edee2007-03-07 16:46:57 -08004667 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004668 rec.e_cpos = cpu_to_le32(cpos);
4669 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004670 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004671 rec.e_flags = flags;
Joel Becker6136ca52009-02-12 19:32:43 -08004672 status = ocfs2_et_insert_check(et, &rec);
Joel Becker1e61ee72008-08-20 18:32:45 -07004673 if (status) {
4674 mlog_errno(status);
4675 goto bail;
4676 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004677
Tao Mae7d4cb62008-08-18 17:38:44 +08004678 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004679 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004680 if (status < 0) {
4681 mlog_errno(status);
4682 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004683 }
4684
Mark Fashehdcd05382007-01-16 11:32:23 -08004685 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4686 "Insert.contig_index: %d, Insert.free_records: %d, "
4687 "Insert.tree_depth: %d\n",
4688 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004689 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004690
Tao Maoc77534f2007-08-28 17:22:33 -07004691 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004692 status = ocfs2_grow_tree(inode, handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004693 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004694 meta_ac);
4695 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004696 mlog_errno(status);
4697 goto bail;
4698 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004699 }
4700
Mark Fashehdcd05382007-01-16 11:32:23 -08004701 /* Finally, we can add clusters. This might rotate the tree for us. */
Tao Mae7d4cb62008-08-18 17:38:44 +08004702 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004703 if (status < 0)
4704 mlog_errno(status);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004705 else if (et->et_ops == &ocfs2_dinode_et_ops)
Mark Fasheh83418972007-04-23 18:53:12 -07004706 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004707
4708bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004709 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004710
Tao Maf56654c2008-08-18 17:38:48 +08004711 mlog_exit(status);
4712 return status;
4713}
4714
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004715/*
4716 * Allcate and add clusters into the extent b-tree.
4717 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004718 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004719 * it is not limited to the file storage. Any extent tree can use this
4720 * function if it implements the proper ocfs2_extent_tree.
4721 */
4722int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4723 struct inode *inode,
4724 u32 *logical_offset,
4725 u32 clusters_to_add,
4726 int mark_unwritten,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004727 struct ocfs2_extent_tree *et,
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004728 handle_t *handle,
4729 struct ocfs2_alloc_context *data_ac,
4730 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004731 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004732{
4733 int status = 0;
4734 int free_extents;
4735 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4736 u32 bit_off, num_bits;
4737 u64 block;
4738 u8 flags = 0;
4739
4740 BUG_ON(!clusters_to_add);
4741
4742 if (mark_unwritten)
4743 flags = OCFS2_EXT_UNWRITTEN;
4744
Joel Becker3d03a302009-02-12 17:49:26 -08004745 free_extents = ocfs2_num_free_extents(osb, et);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004746 if (free_extents < 0) {
4747 status = free_extents;
4748 mlog_errno(status);
4749 goto leave;
4750 }
4751
4752 /* there are two cases which could cause us to EAGAIN in the
4753 * we-need-more-metadata case:
4754 * 1) we haven't reserved *any*
4755 * 2) we are so fragmented, we've needed to add metadata too
4756 * many times. */
4757 if (!free_extents && !meta_ac) {
4758 mlog(0, "we haven't reserved any metadata!\n");
4759 status = -EAGAIN;
4760 reason = RESTART_META;
4761 goto leave;
4762 } else if ((!free_extents)
4763 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004764 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004765 mlog(0, "filesystem is really fragmented...\n");
4766 status = -EAGAIN;
4767 reason = RESTART_META;
4768 goto leave;
4769 }
4770
4771 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4772 clusters_to_add, &bit_off, &num_bits);
4773 if (status < 0) {
4774 if (status != -ENOSPC)
4775 mlog_errno(status);
4776 goto leave;
4777 }
4778
4779 BUG_ON(num_bits > clusters_to_add);
4780
Joel Becker13723d02008-10-17 19:25:01 -07004781 /* reserve our write early -- insert_extent may update the tree root */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004782 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004783 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004784 if (status < 0) {
4785 mlog_errno(status);
4786 goto leave;
4787 }
4788
4789 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4790 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4791 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004792 status = ocfs2_insert_extent(osb, handle, inode, et,
4793 *logical_offset, block,
4794 num_bits, flags, meta_ac);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004795 if (status < 0) {
4796 mlog_errno(status);
4797 goto leave;
4798 }
4799
Joel Beckerf99b9b72008-08-20 19:36:33 -07004800 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004801 if (status < 0) {
4802 mlog_errno(status);
4803 goto leave;
4804 }
4805
4806 clusters_to_add -= num_bits;
4807 *logical_offset += num_bits;
4808
4809 if (clusters_to_add) {
4810 mlog(0, "need to alloc once more, wanted = %u\n",
4811 clusters_to_add);
4812 status = -EAGAIN;
4813 reason = RESTART_TRANS;
4814 }
4815
4816leave:
4817 mlog_exit(status);
4818 if (reason_ret)
4819 *reason_ret = reason;
4820 return status;
4821}
4822
Mark Fasheh328d5752007-06-18 10:48:04 -07004823static void ocfs2_make_right_split_rec(struct super_block *sb,
4824 struct ocfs2_extent_rec *split_rec,
4825 u32 cpos,
4826 struct ocfs2_extent_rec *rec)
4827{
4828 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4829 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4830
4831 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4832
4833 split_rec->e_cpos = cpu_to_le32(cpos);
4834 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4835
4836 split_rec->e_blkno = rec->e_blkno;
4837 le64_add_cpu(&split_rec->e_blkno,
4838 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4839
4840 split_rec->e_flags = rec->e_flags;
4841}
4842
4843static int ocfs2_split_and_insert(struct inode *inode,
4844 handle_t *handle,
4845 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004846 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004847 struct buffer_head **last_eb_bh,
4848 int split_index,
4849 struct ocfs2_extent_rec *orig_split_rec,
4850 struct ocfs2_alloc_context *meta_ac)
4851{
4852 int ret = 0, depth;
4853 unsigned int insert_range, rec_range, do_leftright = 0;
4854 struct ocfs2_extent_rec tmprec;
4855 struct ocfs2_extent_list *rightmost_el;
4856 struct ocfs2_extent_rec rec;
4857 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4858 struct ocfs2_insert_type insert;
4859 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004860
4861leftright:
4862 /*
4863 * Store a copy of the record on the stack - it might move
4864 * around as the tree is manipulated below.
4865 */
4866 rec = path_leaf_el(path)->l_recs[split_index];
4867
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004868 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004869
4870 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4871 if (depth) {
4872 BUG_ON(!(*last_eb_bh));
4873 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4874 rightmost_el = &eb->h_list;
4875 }
4876
4877 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4878 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004879 ret = ocfs2_grow_tree(inode, handle, et,
4880 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004881 if (ret) {
4882 mlog_errno(ret);
4883 goto out;
4884 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004885 }
4886
4887 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4888 insert.ins_appending = APPEND_NONE;
4889 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004890 insert.ins_tree_depth = depth;
4891
4892 insert_range = le32_to_cpu(split_rec.e_cpos) +
4893 le16_to_cpu(split_rec.e_leaf_clusters);
4894 rec_range = le32_to_cpu(rec.e_cpos) +
4895 le16_to_cpu(rec.e_leaf_clusters);
4896
4897 if (split_rec.e_cpos == rec.e_cpos) {
4898 insert.ins_split = SPLIT_LEFT;
4899 } else if (insert_range == rec_range) {
4900 insert.ins_split = SPLIT_RIGHT;
4901 } else {
4902 /*
4903 * Left/right split. We fake this as a right split
4904 * first and then make a second pass as a left split.
4905 */
4906 insert.ins_split = SPLIT_RIGHT;
4907
4908 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4909 &rec);
4910
4911 split_rec = tmprec;
4912
4913 BUG_ON(do_leftright);
4914 do_leftright = 1;
4915 }
4916
Tao Mae7d4cb62008-08-18 17:38:44 +08004917 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004918 if (ret) {
4919 mlog_errno(ret);
4920 goto out;
4921 }
4922
4923 if (do_leftright == 1) {
4924 u32 cpos;
4925 struct ocfs2_extent_list *el;
4926
4927 do_leftright++;
4928 split_rec = *orig_split_rec;
4929
4930 ocfs2_reinit_path(path, 1);
4931
4932 cpos = le32_to_cpu(split_rec.e_cpos);
Joel Beckerfacdb772009-02-12 18:08:48 -08004933 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07004934 if (ret) {
4935 mlog_errno(ret);
4936 goto out;
4937 }
4938
4939 el = path_leaf_el(path);
4940 split_index = ocfs2_search_extent_list(el, cpos);
4941 goto leftright;
4942 }
4943out:
4944
4945 return ret;
4946}
4947
Tao Ma47be12e2009-01-09 07:32:48 +08004948static int ocfs2_replace_extent_rec(struct inode *inode,
4949 handle_t *handle,
4950 struct ocfs2_path *path,
4951 struct ocfs2_extent_list *el,
4952 int split_index,
4953 struct ocfs2_extent_rec *split_rec)
4954{
4955 int ret;
4956
Joel Becker0cf2f762009-02-12 16:41:25 -08004957 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path,
Tao Ma47be12e2009-01-09 07:32:48 +08004958 path_num_items(path) - 1);
4959 if (ret) {
4960 mlog_errno(ret);
4961 goto out;
4962 }
4963
4964 el->l_recs[split_index] = *split_rec;
4965
4966 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4967out:
4968 return ret;
4969}
4970
Mark Fasheh328d5752007-06-18 10:48:04 -07004971/*
4972 * Mark part or all of the extent record at split_index in the leaf
4973 * pointed to by path as written. This removes the unwritten
4974 * extent flag.
4975 *
4976 * Care is taken to handle contiguousness so as to not grow the tree.
4977 *
4978 * meta_ac is not strictly necessary - we only truly need it if growth
4979 * of the tree is required. All other cases will degrade into a less
4980 * optimal tree layout.
4981 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004982 * last_eb_bh should be the rightmost leaf block for any extent
4983 * btree. Since a split may grow the tree or a merge might shrink it,
4984 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07004985 *
4986 * This code is optimized for readability - several passes might be
4987 * made over certain portions of the tree. All of those blocks will
4988 * have been brought into cache (and pinned via the journal), so the
4989 * extra overhead is not expressed in terms of disk reads.
4990 */
4991static int __ocfs2_mark_extent_written(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004992 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004993 handle_t *handle,
4994 struct ocfs2_path *path,
4995 int split_index,
4996 struct ocfs2_extent_rec *split_rec,
4997 struct ocfs2_alloc_context *meta_ac,
4998 struct ocfs2_cached_dealloc_ctxt *dealloc)
4999{
5000 int ret = 0;
5001 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08005002 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07005003 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5004 struct ocfs2_merge_ctxt ctxt;
5005 struct ocfs2_extent_list *rightmost_el;
5006
Roel Kluin3cf0c502007-10-27 00:20:36 +02005007 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07005008 ret = -EIO;
5009 mlog_errno(ret);
5010 goto out;
5011 }
5012
5013 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5014 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5015 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5016 ret = -EIO;
5017 mlog_errno(ret);
5018 goto out;
5019 }
5020
Tao Maad5a4d72008-01-30 14:21:32 +08005021 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005022 split_index,
5023 split_rec);
5024
5025 /*
5026 * The core merge / split code wants to know how much room is
5027 * left in this inodes allocation tree, so we pass the
5028 * rightmost extent list.
5029 */
5030 if (path->p_tree_depth) {
5031 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005032
Joel Becker3d03a302009-02-12 17:49:26 -08005033 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005034 ocfs2_et_get_last_eb_blk(et),
5035 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005036 if (ret) {
5037 mlog_exit(ret);
5038 goto out;
5039 }
5040
5041 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005042 rightmost_el = &eb->h_list;
5043 } else
5044 rightmost_el = path_root_el(path);
5045
Mark Fasheh328d5752007-06-18 10:48:04 -07005046 if (rec->e_cpos == split_rec->e_cpos &&
5047 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5048 ctxt.c_split_covers_rec = 1;
5049 else
5050 ctxt.c_split_covers_rec = 0;
5051
5052 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5053
Mark Fasheh015452b2007-09-12 10:21:22 -07005054 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5055 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5056 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005057
5058 if (ctxt.c_contig_type == CONTIG_NONE) {
5059 if (ctxt.c_split_covers_rec)
Tao Ma47be12e2009-01-09 07:32:48 +08005060 ret = ocfs2_replace_extent_rec(inode, handle,
5061 path, el,
5062 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005063 else
Tao Mae7d4cb62008-08-18 17:38:44 +08005064 ret = ocfs2_split_and_insert(inode, handle, path, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005065 &last_eb_bh, split_index,
5066 split_rec, meta_ac);
5067 if (ret)
5068 mlog_errno(ret);
5069 } else {
Joel Beckerc495dd22009-02-13 02:19:11 -08005070 ret = ocfs2_try_to_merge_extent(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005071 split_index, split_rec,
Joel Beckerc495dd22009-02-13 02:19:11 -08005072 dealloc, &ctxt);
Mark Fasheh328d5752007-06-18 10:48:04 -07005073 if (ret)
5074 mlog_errno(ret);
5075 }
5076
Mark Fasheh328d5752007-06-18 10:48:04 -07005077out:
5078 brelse(last_eb_bh);
5079 return ret;
5080}
5081
5082/*
5083 * Mark the already-existing extent at cpos as written for len clusters.
5084 *
5085 * If the existing extent is larger than the request, initiate a
5086 * split. An attempt will be made at merging with adjacent extents.
5087 *
5088 * The caller is responsible for passing down meta_ac if we'll need it.
5089 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005090int ocfs2_mark_extent_written(struct inode *inode,
5091 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005092 handle_t *handle, u32 cpos, u32 len, u32 phys,
5093 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005094 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005095{
5096 int ret, index;
5097 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
5098 struct ocfs2_extent_rec split_rec;
5099 struct ocfs2_path *left_path = NULL;
5100 struct ocfs2_extent_list *el;
5101
5102 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5103 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
5104
5105 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5106 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5107 "that are being written to, but the feature bit "
5108 "is not set in the super block.",
5109 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5110 ret = -EROFS;
5111 goto out;
5112 }
5113
5114 /*
5115 * XXX: This should be fixed up so that we just re-insert the
5116 * next extent records.
Joel Beckerf99b9b72008-08-20 19:36:33 -07005117 *
5118 * XXX: This is a hack on the extent tree, maybe it should be
5119 * an op?
Mark Fasheh328d5752007-06-18 10:48:04 -07005120 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005121 if (et->et_ops == &ocfs2_dinode_et_ops)
Tao Mae7d4cb62008-08-18 17:38:44 +08005122 ocfs2_extent_map_trunc(inode, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07005123
Joel Beckerffdd7a52008-10-17 22:32:01 -07005124 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005125 if (!left_path) {
5126 ret = -ENOMEM;
5127 mlog_errno(ret);
5128 goto out;
5129 }
5130
Joel Beckerfacdb772009-02-12 18:08:48 -08005131 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005132 if (ret) {
5133 mlog_errno(ret);
5134 goto out;
5135 }
5136 el = path_leaf_el(left_path);
5137
5138 index = ocfs2_search_extent_list(el, cpos);
5139 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5140 ocfs2_error(inode->i_sb,
5141 "Inode %llu has an extent at cpos %u which can no "
5142 "longer be found.\n",
5143 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5144 ret = -EROFS;
5145 goto out;
5146 }
5147
5148 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5149 split_rec.e_cpos = cpu_to_le32(cpos);
5150 split_rec.e_leaf_clusters = cpu_to_le16(len);
5151 split_rec.e_blkno = cpu_to_le64(start_blkno);
5152 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
5153 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
5154
Joel Beckerf99b9b72008-08-20 19:36:33 -07005155 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
Tao Mae7d4cb62008-08-18 17:38:44 +08005156 index, &split_rec, meta_ac,
5157 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005158 if (ret)
5159 mlog_errno(ret);
5160
5161out:
5162 ocfs2_free_path(left_path);
5163 return ret;
5164}
5165
Tao Mae7d4cb62008-08-18 17:38:44 +08005166static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005167 handle_t *handle, struct ocfs2_path *path,
5168 int index, u32 new_range,
5169 struct ocfs2_alloc_context *meta_ac)
5170{
5171 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005172 struct buffer_head *last_eb_bh = NULL;
5173 struct ocfs2_extent_block *eb;
5174 struct ocfs2_extent_list *rightmost_el, *el;
5175 struct ocfs2_extent_rec split_rec;
5176 struct ocfs2_extent_rec *rec;
5177 struct ocfs2_insert_type insert;
5178
5179 /*
5180 * Setup the record to split before we grow the tree.
5181 */
5182 el = path_leaf_el(path);
5183 rec = &el->l_recs[index];
5184 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5185
5186 depth = path->p_tree_depth;
5187 if (depth > 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08005188 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005189 ocfs2_et_get_last_eb_blk(et),
5190 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005191 if (ret < 0) {
5192 mlog_errno(ret);
5193 goto out;
5194 }
5195
5196 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5197 rightmost_el = &eb->h_list;
5198 } else
5199 rightmost_el = path_leaf_el(path);
5200
Tao Ma811f9332008-08-18 17:38:43 +08005201 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005202 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005203 ret = ocfs2_extend_trans(handle, credits);
5204 if (ret) {
5205 mlog_errno(ret);
5206 goto out;
5207 }
5208
5209 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5210 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005211 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005212 meta_ac);
5213 if (ret) {
5214 mlog_errno(ret);
5215 goto out;
5216 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005217 }
5218
5219 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5220 insert.ins_appending = APPEND_NONE;
5221 insert.ins_contig = CONTIG_NONE;
5222 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005223 insert.ins_tree_depth = depth;
5224
Tao Mae7d4cb62008-08-18 17:38:44 +08005225 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005226 if (ret)
5227 mlog_errno(ret);
5228
5229out:
5230 brelse(last_eb_bh);
5231 return ret;
5232}
5233
5234static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5235 struct ocfs2_path *path, int index,
5236 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08005237 u32 cpos, u32 len,
5238 struct ocfs2_extent_tree *et)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005239{
5240 int ret;
5241 u32 left_cpos, rec_range, trunc_range;
5242 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5243 struct super_block *sb = inode->i_sb;
5244 struct ocfs2_path *left_path = NULL;
5245 struct ocfs2_extent_list *el = path_leaf_el(path);
5246 struct ocfs2_extent_rec *rec;
5247 struct ocfs2_extent_block *eb;
5248
5249 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Joel Becker70f18c02009-02-13 02:09:31 -08005250 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005251 if (ret) {
5252 mlog_errno(ret);
5253 goto out;
5254 }
5255
5256 index--;
5257 }
5258
5259 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5260 path->p_tree_depth) {
5261 /*
5262 * Check whether this is the rightmost tree record. If
5263 * we remove all of this record or part of its right
5264 * edge then an update of the record lengths above it
5265 * will be required.
5266 */
5267 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5268 if (eb->h_next_leaf_blk == 0)
5269 is_rightmost_tree_rec = 1;
5270 }
5271
5272 rec = &el->l_recs[index];
5273 if (index == 0 && path->p_tree_depth &&
5274 le32_to_cpu(rec->e_cpos) == cpos) {
5275 /*
5276 * Changing the leftmost offset (via partial or whole
5277 * record truncate) of an interior (or rightmost) path
5278 * means we have to update the subtree that is formed
5279 * by this leaf and the one to it's left.
5280 *
5281 * There are two cases we can skip:
5282 * 1) Path is the leftmost one in our inode tree.
5283 * 2) The leaf is rightmost and will be empty after
5284 * we remove the extent record - the rotate code
5285 * knows how to update the newly formed edge.
5286 */
5287
5288 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5289 &left_cpos);
5290 if (ret) {
5291 mlog_errno(ret);
5292 goto out;
5293 }
5294
5295 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005296 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005297 if (!left_path) {
5298 ret = -ENOMEM;
5299 mlog_errno(ret);
5300 goto out;
5301 }
5302
Joel Beckerfacdb772009-02-12 18:08:48 -08005303 ret = ocfs2_find_path(et->et_ci, left_path,
5304 left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005305 if (ret) {
5306 mlog_errno(ret);
5307 goto out;
5308 }
5309 }
5310 }
5311
5312 ret = ocfs2_extend_rotate_transaction(handle, 0,
5313 handle->h_buffer_credits,
5314 path);
5315 if (ret) {
5316 mlog_errno(ret);
5317 goto out;
5318 }
5319
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005320 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005321 if (ret) {
5322 mlog_errno(ret);
5323 goto out;
5324 }
5325
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005326 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005327 if (ret) {
5328 mlog_errno(ret);
5329 goto out;
5330 }
5331
5332 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5333 trunc_range = cpos + len;
5334
5335 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5336 int next_free;
5337
5338 memset(rec, 0, sizeof(*rec));
5339 ocfs2_cleanup_merge(el, index);
5340 wants_rotate = 1;
5341
5342 next_free = le16_to_cpu(el->l_next_free_rec);
5343 if (is_rightmost_tree_rec && next_free > 1) {
5344 /*
5345 * We skip the edge update if this path will
5346 * be deleted by the rotate code.
5347 */
5348 rec = &el->l_recs[next_free - 1];
5349 ocfs2_adjust_rightmost_records(inode, handle, path,
5350 rec);
5351 }
5352 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5353 /* Remove leftmost portion of the record. */
5354 le32_add_cpu(&rec->e_cpos, len);
5355 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5356 le16_add_cpu(&rec->e_leaf_clusters, -len);
5357 } else if (rec_range == trunc_range) {
5358 /* Remove rightmost portion of the record */
5359 le16_add_cpu(&rec->e_leaf_clusters, -len);
5360 if (is_rightmost_tree_rec)
5361 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5362 } else {
5363 /* Caller should have trapped this. */
5364 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5365 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5366 le32_to_cpu(rec->e_cpos),
5367 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5368 BUG();
5369 }
5370
5371 if (left_path) {
5372 int subtree_index;
5373
Joel Becker7dc02802009-02-12 19:20:13 -08005374 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Joel Becker4619c732009-02-12 19:02:36 -08005375 ocfs2_complete_edge_insert(handle, left_path, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005376 subtree_index);
5377 }
5378
5379 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5380
Joel Becker70f18c02009-02-13 02:09:31 -08005381 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005382 if (ret) {
5383 mlog_errno(ret);
5384 goto out;
5385 }
5386
5387out:
5388 ocfs2_free_path(left_path);
5389 return ret;
5390}
5391
Joel Beckerf99b9b72008-08-20 19:36:33 -07005392int ocfs2_remove_extent(struct inode *inode,
5393 struct ocfs2_extent_tree *et,
Mark Fasheh063c4562007-07-03 13:34:11 -07005394 u32 cpos, u32 len, handle_t *handle,
5395 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005396 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005397{
5398 int ret, index;
5399 u32 rec_range, trunc_range;
5400 struct ocfs2_extent_rec *rec;
5401 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005402 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005403
5404 ocfs2_extent_map_trunc(inode, 0);
5405
Joel Beckerffdd7a52008-10-17 22:32:01 -07005406 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005407 if (!path) {
5408 ret = -ENOMEM;
5409 mlog_errno(ret);
5410 goto out;
5411 }
5412
Joel Beckerfacdb772009-02-12 18:08:48 -08005413 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005414 if (ret) {
5415 mlog_errno(ret);
5416 goto out;
5417 }
5418
5419 el = path_leaf_el(path);
5420 index = ocfs2_search_extent_list(el, cpos);
5421 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5422 ocfs2_error(inode->i_sb,
5423 "Inode %llu has an extent at cpos %u which can no "
5424 "longer be found.\n",
5425 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5426 ret = -EROFS;
5427 goto out;
5428 }
5429
5430 /*
5431 * We have 3 cases of extent removal:
5432 * 1) Range covers the entire extent rec
5433 * 2) Range begins or ends on one edge of the extent rec
5434 * 3) Range is in the middle of the extent rec (no shared edges)
5435 *
5436 * For case 1 we remove the extent rec and left rotate to
5437 * fill the hole.
5438 *
5439 * For case 2 we just shrink the existing extent rec, with a
5440 * tree update if the shrinking edge is also the edge of an
5441 * extent block.
5442 *
5443 * For case 3 we do a right split to turn the extent rec into
5444 * something case 2 can handle.
5445 */
5446 rec = &el->l_recs[index];
5447 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5448 trunc_range = cpos + len;
5449
5450 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5451
5452 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5453 "(cpos %u, len %u)\n",
5454 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5455 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5456
5457 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5458 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005459 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005460 if (ret) {
5461 mlog_errno(ret);
5462 goto out;
5463 }
5464 } else {
Joel Beckerf99b9b72008-08-20 19:36:33 -07005465 ret = ocfs2_split_tree(inode, et, handle, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005466 trunc_range, meta_ac);
5467 if (ret) {
5468 mlog_errno(ret);
5469 goto out;
5470 }
5471
5472 /*
5473 * The split could have manipulated the tree enough to
5474 * move the record location, so we have to look for it again.
5475 */
5476 ocfs2_reinit_path(path, 1);
5477
Joel Beckerfacdb772009-02-12 18:08:48 -08005478 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005479 if (ret) {
5480 mlog_errno(ret);
5481 goto out;
5482 }
5483
5484 el = path_leaf_el(path);
5485 index = ocfs2_search_extent_list(el, cpos);
5486 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5487 ocfs2_error(inode->i_sb,
5488 "Inode %llu: split at cpos %u lost record.",
5489 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5490 cpos);
5491 ret = -EROFS;
5492 goto out;
5493 }
5494
5495 /*
5496 * Double check our values here. If anything is fishy,
5497 * it's easier to catch it at the top level.
5498 */
5499 rec = &el->l_recs[index];
5500 rec_range = le32_to_cpu(rec->e_cpos) +
5501 ocfs2_rec_clusters(el, rec);
5502 if (rec_range != trunc_range) {
5503 ocfs2_error(inode->i_sb,
5504 "Inode %llu: error after split at cpos %u"
5505 "trunc len %u, existing record is (%u,%u)",
5506 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5507 cpos, len, le32_to_cpu(rec->e_cpos),
5508 ocfs2_rec_clusters(el, rec));
5509 ret = -EROFS;
5510 goto out;
5511 }
5512
5513 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005514 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005515 if (ret) {
5516 mlog_errno(ret);
5517 goto out;
5518 }
5519 }
5520
5521out:
5522 ocfs2_free_path(path);
5523 return ret;
5524}
5525
Mark Fashehfecc0112008-11-12 15:16:38 -08005526int ocfs2_remove_btree_range(struct inode *inode,
5527 struct ocfs2_extent_tree *et,
5528 u32 cpos, u32 phys_cpos, u32 len,
5529 struct ocfs2_cached_dealloc_ctxt *dealloc)
5530{
5531 int ret;
5532 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5533 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5534 struct inode *tl_inode = osb->osb_tl_inode;
5535 handle_t *handle;
5536 struct ocfs2_alloc_context *meta_ac = NULL;
5537
5538 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5539 if (ret) {
5540 mlog_errno(ret);
5541 return ret;
5542 }
5543
5544 mutex_lock(&tl_inode->i_mutex);
5545
5546 if (ocfs2_truncate_log_needs_flush(osb)) {
5547 ret = __ocfs2_flush_truncate_log(osb);
5548 if (ret < 0) {
5549 mlog_errno(ret);
5550 goto out;
5551 }
5552 }
5553
Jan Karaa90714c2008-10-09 19:38:40 +02005554 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005555 if (IS_ERR(handle)) {
5556 ret = PTR_ERR(handle);
5557 mlog_errno(ret);
5558 goto out;
5559 }
5560
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005561 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07005562 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005563 if (ret) {
5564 mlog_errno(ret);
5565 goto out;
5566 }
5567
Mark Fashehfd4ef232009-01-29 15:06:21 -08005568 vfs_dq_free_space_nodirty(inode,
5569 ocfs2_clusters_to_bytes(inode->i_sb, len));
5570
Mark Fashehfecc0112008-11-12 15:16:38 -08005571 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5572 dealloc);
5573 if (ret) {
5574 mlog_errno(ret);
5575 goto out_commit;
5576 }
5577
Joel Becker6136ca52009-02-12 19:32:43 -08005578 ocfs2_et_update_clusters(et, -len);
Mark Fashehfecc0112008-11-12 15:16:38 -08005579
5580 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5581 if (ret) {
5582 mlog_errno(ret);
5583 goto out_commit;
5584 }
5585
5586 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5587 if (ret)
5588 mlog_errno(ret);
5589
5590out_commit:
5591 ocfs2_commit_trans(osb, handle);
5592out:
5593 mutex_unlock(&tl_inode->i_mutex);
5594
5595 if (meta_ac)
5596 ocfs2_free_alloc_context(meta_ac);
5597
5598 return ret;
5599}
5600
Mark Fasheh063c4562007-07-03 13:34:11 -07005601int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005602{
5603 struct buffer_head *tl_bh = osb->osb_tl_bh;
5604 struct ocfs2_dinode *di;
5605 struct ocfs2_truncate_log *tl;
5606
5607 di = (struct ocfs2_dinode *) tl_bh->b_data;
5608 tl = &di->id2.i_dealloc;
5609
5610 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5611 "slot %d, invalid truncate log parameters: used = "
5612 "%u, count = %u\n", osb->slot_num,
5613 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5614 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5615}
5616
5617static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5618 unsigned int new_start)
5619{
5620 unsigned int tail_index;
5621 unsigned int current_tail;
5622
5623 /* No records, nothing to coalesce */
5624 if (!le16_to_cpu(tl->tl_used))
5625 return 0;
5626
5627 tail_index = le16_to_cpu(tl->tl_used) - 1;
5628 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5629 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5630
5631 return current_tail == new_start;
5632}
5633
Mark Fasheh063c4562007-07-03 13:34:11 -07005634int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5635 handle_t *handle,
5636 u64 start_blk,
5637 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005638{
5639 int status, index;
5640 unsigned int start_cluster, tl_count;
5641 struct inode *tl_inode = osb->osb_tl_inode;
5642 struct buffer_head *tl_bh = osb->osb_tl_bh;
5643 struct ocfs2_dinode *di;
5644 struct ocfs2_truncate_log *tl;
5645
Mark Fashehb06970532006-03-03 10:24:33 -08005646 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5647 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005648
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005649 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005650
5651 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5652
5653 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005654
Joel Becker10995aa2008-11-13 14:49:12 -08005655 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5656 * by the underlying call to ocfs2_read_inode_block(), so any
5657 * corruption is a code bug */
5658 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5659
5660 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005661 tl_count = le16_to_cpu(tl->tl_count);
5662 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5663 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005664 "Truncate record count on #%llu invalid "
5665 "wanted %u, actual %u\n",
5666 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005667 ocfs2_truncate_recs_per_inode(osb->sb),
5668 le16_to_cpu(tl->tl_count));
5669
5670 /* Caller should have known to flush before calling us. */
5671 index = le16_to_cpu(tl->tl_used);
5672 if (index >= tl_count) {
5673 status = -ENOSPC;
5674 mlog_errno(status);
5675 goto bail;
5676 }
5677
Joel Becker0cf2f762009-02-12 16:41:25 -08005678 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005679 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005680 if (status < 0) {
5681 mlog_errno(status);
5682 goto bail;
5683 }
5684
5685 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005686 "%llu (index = %d)\n", num_clusters, start_cluster,
5687 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005688
5689 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5690 /*
5691 * Move index back to the record we are coalescing with.
5692 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5693 */
5694 index--;
5695
5696 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5697 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5698 index, le32_to_cpu(tl->tl_recs[index].t_start),
5699 num_clusters);
5700 } else {
5701 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5702 tl->tl_used = cpu_to_le16(index + 1);
5703 }
5704 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5705
5706 status = ocfs2_journal_dirty(handle, tl_bh);
5707 if (status < 0) {
5708 mlog_errno(status);
5709 goto bail;
5710 }
5711
5712bail:
5713 mlog_exit(status);
5714 return status;
5715}
5716
5717static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005718 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005719 struct inode *data_alloc_inode,
5720 struct buffer_head *data_alloc_bh)
5721{
5722 int status = 0;
5723 int i;
5724 unsigned int num_clusters;
5725 u64 start_blk;
5726 struct ocfs2_truncate_rec rec;
5727 struct ocfs2_dinode *di;
5728 struct ocfs2_truncate_log *tl;
5729 struct inode *tl_inode = osb->osb_tl_inode;
5730 struct buffer_head *tl_bh = osb->osb_tl_bh;
5731
5732 mlog_entry_void();
5733
5734 di = (struct ocfs2_dinode *) tl_bh->b_data;
5735 tl = &di->id2.i_dealloc;
5736 i = le16_to_cpu(tl->tl_used) - 1;
5737 while (i >= 0) {
5738 /* Caller has given us at least enough credits to
5739 * update the truncate log dinode */
Joel Becker0cf2f762009-02-12 16:41:25 -08005740 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005741 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005742 if (status < 0) {
5743 mlog_errno(status);
5744 goto bail;
5745 }
5746
5747 tl->tl_used = cpu_to_le16(i);
5748
5749 status = ocfs2_journal_dirty(handle, tl_bh);
5750 if (status < 0) {
5751 mlog_errno(status);
5752 goto bail;
5753 }
5754
5755 /* TODO: Perhaps we can calculate the bulk of the
5756 * credits up front rather than extending like
5757 * this. */
5758 status = ocfs2_extend_trans(handle,
5759 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5760 if (status < 0) {
5761 mlog_errno(status);
5762 goto bail;
5763 }
5764
5765 rec = tl->tl_recs[i];
5766 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5767 le32_to_cpu(rec.t_start));
5768 num_clusters = le32_to_cpu(rec.t_clusters);
5769
5770 /* if start_blk is not set, we ignore the record as
5771 * invalid. */
5772 if (start_blk) {
5773 mlog(0, "free record %d, start = %u, clusters = %u\n",
5774 i, le32_to_cpu(rec.t_start), num_clusters);
5775
5776 status = ocfs2_free_clusters(handle, data_alloc_inode,
5777 data_alloc_bh, start_blk,
5778 num_clusters);
5779 if (status < 0) {
5780 mlog_errno(status);
5781 goto bail;
5782 }
5783 }
5784 i--;
5785 }
5786
5787bail:
5788 mlog_exit(status);
5789 return status;
5790}
5791
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005792/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005793int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005794{
5795 int status;
5796 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005797 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005798 struct inode *tl_inode = osb->osb_tl_inode;
5799 struct inode *data_alloc_inode = NULL;
5800 struct buffer_head *tl_bh = osb->osb_tl_bh;
5801 struct buffer_head *data_alloc_bh = NULL;
5802 struct ocfs2_dinode *di;
5803 struct ocfs2_truncate_log *tl;
5804
5805 mlog_entry_void();
5806
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005807 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005808
5809 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005810
Joel Becker10995aa2008-11-13 14:49:12 -08005811 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5812 * by the underlying call to ocfs2_read_inode_block(), so any
5813 * corruption is a code bug */
5814 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5815
5816 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005817 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005818 mlog(0, "Flush %u records from truncate log #%llu\n",
5819 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005820 if (!num_to_flush) {
5821 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005822 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005823 }
5824
5825 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5826 GLOBAL_BITMAP_SYSTEM_INODE,
5827 OCFS2_INVALID_SLOT);
5828 if (!data_alloc_inode) {
5829 status = -EINVAL;
5830 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005831 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005832 }
5833
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005834 mutex_lock(&data_alloc_inode->i_mutex);
5835
Mark Fashehe63aecb62007-10-18 15:30:42 -07005836 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005837 if (status < 0) {
5838 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005839 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005840 }
5841
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005842 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005843 if (IS_ERR(handle)) {
5844 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005845 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005846 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005847 }
5848
5849 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5850 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005851 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005852 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005853
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005854 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005855
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005856out_unlock:
5857 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005858 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005859
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005860out_mutex:
5861 mutex_unlock(&data_alloc_inode->i_mutex);
5862 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005863
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005864out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005865 mlog_exit(status);
5866 return status;
5867}
5868
5869int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5870{
5871 int status;
5872 struct inode *tl_inode = osb->osb_tl_inode;
5873
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005874 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005875 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005876 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005877
5878 return status;
5879}
5880
David Howellsc4028952006-11-22 14:57:56 +00005881static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005882{
5883 int status;
David Howellsc4028952006-11-22 14:57:56 +00005884 struct ocfs2_super *osb =
5885 container_of(work, struct ocfs2_super,
5886 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005887
5888 mlog_entry_void();
5889
5890 status = ocfs2_flush_truncate_log(osb);
5891 if (status < 0)
5892 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005893 else
5894 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005895
5896 mlog_exit(status);
5897}
5898
5899#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5900void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5901 int cancel)
5902{
5903 if (osb->osb_tl_inode) {
5904 /* We want to push off log flushes while truncates are
5905 * still running. */
5906 if (cancel)
5907 cancel_delayed_work(&osb->osb_truncate_log_wq);
5908
5909 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5910 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5911 }
5912}
5913
5914static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5915 int slot_num,
5916 struct inode **tl_inode,
5917 struct buffer_head **tl_bh)
5918{
5919 int status;
5920 struct inode *inode = NULL;
5921 struct buffer_head *bh = NULL;
5922
5923 inode = ocfs2_get_system_file_inode(osb,
5924 TRUNCATE_LOG_SYSTEM_INODE,
5925 slot_num);
5926 if (!inode) {
5927 status = -EINVAL;
5928 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5929 goto bail;
5930 }
5931
Joel Beckerb657c952008-11-13 14:49:11 -08005932 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005933 if (status < 0) {
5934 iput(inode);
5935 mlog_errno(status);
5936 goto bail;
5937 }
5938
5939 *tl_inode = inode;
5940 *tl_bh = bh;
5941bail:
5942 mlog_exit(status);
5943 return status;
5944}
5945
5946/* called during the 1st stage of node recovery. we stamp a clean
5947 * truncate log and pass back a copy for processing later. if the
5948 * truncate log does not require processing, a *tl_copy is set to
5949 * NULL. */
5950int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5951 int slot_num,
5952 struct ocfs2_dinode **tl_copy)
5953{
5954 int status;
5955 struct inode *tl_inode = NULL;
5956 struct buffer_head *tl_bh = NULL;
5957 struct ocfs2_dinode *di;
5958 struct ocfs2_truncate_log *tl;
5959
5960 *tl_copy = NULL;
5961
5962 mlog(0, "recover truncate log from slot %d\n", slot_num);
5963
5964 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5965 if (status < 0) {
5966 mlog_errno(status);
5967 goto bail;
5968 }
5969
5970 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005971
Joel Becker10995aa2008-11-13 14:49:12 -08005972 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5973 * validated by the underlying call to ocfs2_read_inode_block(),
5974 * so any corruption is a code bug */
5975 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5976
5977 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005978 if (le16_to_cpu(tl->tl_used)) {
5979 mlog(0, "We'll have %u logs to recover\n",
5980 le16_to_cpu(tl->tl_used));
5981
5982 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5983 if (!(*tl_copy)) {
5984 status = -ENOMEM;
5985 mlog_errno(status);
5986 goto bail;
5987 }
5988
5989 /* Assuming the write-out below goes well, this copy
5990 * will be passed back to recovery for processing. */
5991 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5992
5993 /* All we need to do to clear the truncate log is set
5994 * tl_used. */
5995 tl->tl_used = 0;
5996
Joel Becker13723d02008-10-17 19:25:01 -07005997 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08005998 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08005999 if (status < 0) {
6000 mlog_errno(status);
6001 goto bail;
6002 }
6003 }
6004
6005bail:
6006 if (tl_inode)
6007 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006008 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006009
6010 if (status < 0 && (*tl_copy)) {
6011 kfree(*tl_copy);
6012 *tl_copy = NULL;
6013 }
6014
6015 mlog_exit(status);
6016 return status;
6017}
6018
6019int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6020 struct ocfs2_dinode *tl_copy)
6021{
6022 int status = 0;
6023 int i;
6024 unsigned int clusters, num_recs, start_cluster;
6025 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006026 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006027 struct inode *tl_inode = osb->osb_tl_inode;
6028 struct ocfs2_truncate_log *tl;
6029
6030 mlog_entry_void();
6031
6032 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6033 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6034 return -EINVAL;
6035 }
6036
6037 tl = &tl_copy->id2.i_dealloc;
6038 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08006039 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006040 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006041
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006042 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006043 for(i = 0; i < num_recs; i++) {
6044 if (ocfs2_truncate_log_needs_flush(osb)) {
6045 status = __ocfs2_flush_truncate_log(osb);
6046 if (status < 0) {
6047 mlog_errno(status);
6048 goto bail_up;
6049 }
6050 }
6051
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006052 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006053 if (IS_ERR(handle)) {
6054 status = PTR_ERR(handle);
6055 mlog_errno(status);
6056 goto bail_up;
6057 }
6058
6059 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6060 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6061 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6062
6063 status = ocfs2_truncate_log_append(osb, handle,
6064 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006065 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006066 if (status < 0) {
6067 mlog_errno(status);
6068 goto bail_up;
6069 }
6070 }
6071
6072bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006073 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006074
6075 mlog_exit(status);
6076 return status;
6077}
6078
6079void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6080{
6081 int status;
6082 struct inode *tl_inode = osb->osb_tl_inode;
6083
6084 mlog_entry_void();
6085
6086 if (tl_inode) {
6087 cancel_delayed_work(&osb->osb_truncate_log_wq);
6088 flush_workqueue(ocfs2_wq);
6089
6090 status = ocfs2_flush_truncate_log(osb);
6091 if (status < 0)
6092 mlog_errno(status);
6093
6094 brelse(osb->osb_tl_bh);
6095 iput(osb->osb_tl_inode);
6096 }
6097
6098 mlog_exit_void();
6099}
6100
6101int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6102{
6103 int status;
6104 struct inode *tl_inode = NULL;
6105 struct buffer_head *tl_bh = NULL;
6106
6107 mlog_entry_void();
6108
6109 status = ocfs2_get_truncate_log_info(osb,
6110 osb->slot_num,
6111 &tl_inode,
6112 &tl_bh);
6113 if (status < 0)
6114 mlog_errno(status);
6115
6116 /* ocfs2_truncate_log_shutdown keys on the existence of
6117 * osb->osb_tl_inode so we don't set any of the osb variables
6118 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006119 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6120 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006121 osb->osb_tl_bh = tl_bh;
6122 osb->osb_tl_inode = tl_inode;
6123
6124 mlog_exit(status);
6125 return status;
6126}
6127
Mark Fasheh2b604352007-06-22 15:45:27 -07006128/*
6129 * Delayed de-allocation of suballocator blocks.
6130 *
6131 * Some sets of block de-allocations might involve multiple suballocator inodes.
6132 *
6133 * The locking for this can get extremely complicated, especially when
6134 * the suballocator inodes to delete from aren't known until deep
6135 * within an unrelated codepath.
6136 *
6137 * ocfs2_extent_block structures are a good example of this - an inode
6138 * btree could have been grown by any number of nodes each allocating
6139 * out of their own suballoc inode.
6140 *
6141 * These structures allow the delay of block de-allocation until a
6142 * later time, when locking of multiple cluster inodes won't cause
6143 * deadlock.
6144 */
6145
6146/*
Tao Ma2891d292008-11-12 08:26:58 +08006147 * Describe a single bit freed from a suballocator. For the block
6148 * suballocators, it represents one block. For the global cluster
6149 * allocator, it represents some clusters and free_bit indicates
6150 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006151 */
6152struct ocfs2_cached_block_free {
6153 struct ocfs2_cached_block_free *free_next;
6154 u64 free_blk;
6155 unsigned int free_bit;
6156};
6157
6158struct ocfs2_per_slot_free_list {
6159 struct ocfs2_per_slot_free_list *f_next_suballocator;
6160 int f_inode_type;
6161 int f_slot;
6162 struct ocfs2_cached_block_free *f_first;
6163};
6164
Tao Ma2891d292008-11-12 08:26:58 +08006165static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6166 int sysfile_type,
6167 int slot,
6168 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006169{
6170 int ret;
6171 u64 bg_blkno;
6172 handle_t *handle;
6173 struct inode *inode;
6174 struct buffer_head *di_bh = NULL;
6175 struct ocfs2_cached_block_free *tmp;
6176
6177 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6178 if (!inode) {
6179 ret = -EINVAL;
6180 mlog_errno(ret);
6181 goto out;
6182 }
6183
6184 mutex_lock(&inode->i_mutex);
6185
Mark Fashehe63aecb62007-10-18 15:30:42 -07006186 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006187 if (ret) {
6188 mlog_errno(ret);
6189 goto out_mutex;
6190 }
6191
6192 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6193 if (IS_ERR(handle)) {
6194 ret = PTR_ERR(handle);
6195 mlog_errno(ret);
6196 goto out_unlock;
6197 }
6198
6199 while (head) {
6200 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6201 head->free_bit);
6202 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6203 head->free_bit, (unsigned long long)head->free_blk);
6204
6205 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6206 head->free_bit, bg_blkno, 1);
6207 if (ret) {
6208 mlog_errno(ret);
6209 goto out_journal;
6210 }
6211
6212 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6213 if (ret) {
6214 mlog_errno(ret);
6215 goto out_journal;
6216 }
6217
6218 tmp = head;
6219 head = head->free_next;
6220 kfree(tmp);
6221 }
6222
6223out_journal:
6224 ocfs2_commit_trans(osb, handle);
6225
6226out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006227 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006228 brelse(di_bh);
6229out_mutex:
6230 mutex_unlock(&inode->i_mutex);
6231 iput(inode);
6232out:
6233 while(head) {
6234 /* Premature exit may have left some dangling items. */
6235 tmp = head;
6236 head = head->free_next;
6237 kfree(tmp);
6238 }
6239
6240 return ret;
6241}
6242
Tao Ma2891d292008-11-12 08:26:58 +08006243int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6244 u64 blkno, unsigned int bit)
6245{
6246 int ret = 0;
6247 struct ocfs2_cached_block_free *item;
6248
6249 item = kmalloc(sizeof(*item), GFP_NOFS);
6250 if (item == NULL) {
6251 ret = -ENOMEM;
6252 mlog_errno(ret);
6253 return ret;
6254 }
6255
6256 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6257 bit, (unsigned long long)blkno);
6258
6259 item->free_blk = blkno;
6260 item->free_bit = bit;
6261 item->free_next = ctxt->c_global_allocator;
6262
6263 ctxt->c_global_allocator = item;
6264 return ret;
6265}
6266
6267static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6268 struct ocfs2_cached_block_free *head)
6269{
6270 struct ocfs2_cached_block_free *tmp;
6271 struct inode *tl_inode = osb->osb_tl_inode;
6272 handle_t *handle;
6273 int ret = 0;
6274
6275 mutex_lock(&tl_inode->i_mutex);
6276
6277 while (head) {
6278 if (ocfs2_truncate_log_needs_flush(osb)) {
6279 ret = __ocfs2_flush_truncate_log(osb);
6280 if (ret < 0) {
6281 mlog_errno(ret);
6282 break;
6283 }
6284 }
6285
6286 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6287 if (IS_ERR(handle)) {
6288 ret = PTR_ERR(handle);
6289 mlog_errno(ret);
6290 break;
6291 }
6292
6293 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6294 head->free_bit);
6295
6296 ocfs2_commit_trans(osb, handle);
6297 tmp = head;
6298 head = head->free_next;
6299 kfree(tmp);
6300
6301 if (ret < 0) {
6302 mlog_errno(ret);
6303 break;
6304 }
6305 }
6306
6307 mutex_unlock(&tl_inode->i_mutex);
6308
6309 while (head) {
6310 /* Premature exit may have left some dangling items. */
6311 tmp = head;
6312 head = head->free_next;
6313 kfree(tmp);
6314 }
6315
6316 return ret;
6317}
6318
Mark Fasheh2b604352007-06-22 15:45:27 -07006319int ocfs2_run_deallocs(struct ocfs2_super *osb,
6320 struct ocfs2_cached_dealloc_ctxt *ctxt)
6321{
6322 int ret = 0, ret2;
6323 struct ocfs2_per_slot_free_list *fl;
6324
6325 if (!ctxt)
6326 return 0;
6327
6328 while (ctxt->c_first_suballocator) {
6329 fl = ctxt->c_first_suballocator;
6330
6331 if (fl->f_first) {
6332 mlog(0, "Free items: (type %u, slot %d)\n",
6333 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006334 ret2 = ocfs2_free_cached_blocks(osb,
6335 fl->f_inode_type,
6336 fl->f_slot,
6337 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006338 if (ret2)
6339 mlog_errno(ret2);
6340 if (!ret)
6341 ret = ret2;
6342 }
6343
6344 ctxt->c_first_suballocator = fl->f_next_suballocator;
6345 kfree(fl);
6346 }
6347
Tao Ma2891d292008-11-12 08:26:58 +08006348 if (ctxt->c_global_allocator) {
6349 ret2 = ocfs2_free_cached_clusters(osb,
6350 ctxt->c_global_allocator);
6351 if (ret2)
6352 mlog_errno(ret2);
6353 if (!ret)
6354 ret = ret2;
6355
6356 ctxt->c_global_allocator = NULL;
6357 }
6358
Mark Fasheh2b604352007-06-22 15:45:27 -07006359 return ret;
6360}
6361
6362static struct ocfs2_per_slot_free_list *
6363ocfs2_find_per_slot_free_list(int type,
6364 int slot,
6365 struct ocfs2_cached_dealloc_ctxt *ctxt)
6366{
6367 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6368
6369 while (fl) {
6370 if (fl->f_inode_type == type && fl->f_slot == slot)
6371 return fl;
6372
6373 fl = fl->f_next_suballocator;
6374 }
6375
6376 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6377 if (fl) {
6378 fl->f_inode_type = type;
6379 fl->f_slot = slot;
6380 fl->f_first = NULL;
6381 fl->f_next_suballocator = ctxt->c_first_suballocator;
6382
6383 ctxt->c_first_suballocator = fl;
6384 }
6385 return fl;
6386}
6387
6388static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6389 int type, int slot, u64 blkno,
6390 unsigned int bit)
6391{
6392 int ret;
6393 struct ocfs2_per_slot_free_list *fl;
6394 struct ocfs2_cached_block_free *item;
6395
6396 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6397 if (fl == NULL) {
6398 ret = -ENOMEM;
6399 mlog_errno(ret);
6400 goto out;
6401 }
6402
6403 item = kmalloc(sizeof(*item), GFP_NOFS);
6404 if (item == NULL) {
6405 ret = -ENOMEM;
6406 mlog_errno(ret);
6407 goto out;
6408 }
6409
6410 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6411 type, slot, bit, (unsigned long long)blkno);
6412
6413 item->free_blk = blkno;
6414 item->free_bit = bit;
6415 item->free_next = fl->f_first;
6416
6417 fl->f_first = item;
6418
6419 ret = 0;
6420out:
6421 return ret;
6422}
6423
Mark Fasheh59a5e412007-06-22 15:52:36 -07006424static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6425 struct ocfs2_extent_block *eb)
6426{
6427 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6428 le16_to_cpu(eb->h_suballoc_slot),
6429 le64_to_cpu(eb->h_blkno),
6430 le16_to_cpu(eb->h_suballoc_bit));
6431}
6432
Mark Fashehccd979b2005-12-15 14:31:24 -08006433/* This function will figure out whether the currently last extent
6434 * block will be deleted, and if it will, what the new last extent
6435 * block will be so we can update his h_next_leaf_blk field, as well
6436 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006437static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006438 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006439 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006440 struct buffer_head **new_last_eb)
6441{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006442 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006443 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006444 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006445 struct ocfs2_extent_block *eb;
6446 struct ocfs2_extent_list *el;
6447 struct buffer_head *bh = NULL;
6448
6449 *new_last_eb = NULL;
6450
Mark Fashehccd979b2005-12-15 14:31:24 -08006451 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006452 if (!path->p_tree_depth)
6453 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006454
6455 /* trunc to zero special case - this makes tree_depth = 0
6456 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006457 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006458 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006459
Mark Fashehdcd05382007-01-16 11:32:23 -08006460 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006461 BUG_ON(!el->l_next_free_rec);
6462
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006463 /*
6464 * Make sure that this extent list will actually be empty
6465 * after we clear away the data. We can shortcut out if
6466 * there's more than one non-empty extent in the
6467 * list. Otherwise, a check of the remaining extent is
6468 * necessary.
6469 */
6470 next_free = le16_to_cpu(el->l_next_free_rec);
6471 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006472 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006473 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006474 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006475
6476 /* We may have a valid extent in index 1, check it. */
6477 if (next_free == 2)
6478 rec = &el->l_recs[1];
6479
6480 /*
6481 * Fall through - no more nonempty extents, so we want
6482 * to delete this leaf.
6483 */
6484 } else {
6485 if (next_free > 1)
6486 goto out;
6487
6488 rec = &el->l_recs[0];
6489 }
6490
6491 if (rec) {
6492 /*
6493 * Check it we'll only be trimming off the end of this
6494 * cluster.
6495 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006496 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006497 goto out;
6498 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006499
Mark Fashehdcd05382007-01-16 11:32:23 -08006500 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6501 if (ret) {
6502 mlog_errno(ret);
6503 goto out;
6504 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006505
Joel Beckerfacdb772009-02-12 18:08:48 -08006506 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08006507 if (ret) {
6508 mlog_errno(ret);
6509 goto out;
6510 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006511
Mark Fashehdcd05382007-01-16 11:32:23 -08006512 eb = (struct ocfs2_extent_block *) bh->b_data;
6513 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006514
6515 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6516 * Any corruption is a code bug. */
6517 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006518
6519 *new_last_eb = bh;
6520 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006521 mlog(0, "returning block %llu, (cpos: %u)\n",
6522 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6523out:
6524 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006525
Mark Fashehdcd05382007-01-16 11:32:23 -08006526 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006527}
6528
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006529/*
6530 * Trim some clusters off the rightmost edge of a tree. Only called
6531 * during truncate.
6532 *
6533 * The caller needs to:
6534 * - start journaling of each path component.
6535 * - compute and fully set up any new last ext block
6536 */
6537static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6538 handle_t *handle, struct ocfs2_truncate_context *tc,
6539 u32 clusters_to_del, u64 *delete_start)
6540{
6541 int ret, i, index = path->p_tree_depth;
6542 u32 new_edge = 0;
6543 u64 deleted_eb = 0;
6544 struct buffer_head *bh;
6545 struct ocfs2_extent_list *el;
6546 struct ocfs2_extent_rec *rec;
6547
6548 *delete_start = 0;
6549
6550 while (index >= 0) {
6551 bh = path->p_node[index].bh;
6552 el = path->p_node[index].el;
6553
6554 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6555 index, (unsigned long long)bh->b_blocknr);
6556
6557 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6558
6559 if (index !=
6560 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6561 ocfs2_error(inode->i_sb,
6562 "Inode %lu has invalid ext. block %llu",
6563 inode->i_ino,
6564 (unsigned long long)bh->b_blocknr);
6565 ret = -EROFS;
6566 goto out;
6567 }
6568
6569find_tail_record:
6570 i = le16_to_cpu(el->l_next_free_rec) - 1;
6571 rec = &el->l_recs[i];
6572
6573 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6574 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006575 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006576 (unsigned long long)le64_to_cpu(rec->e_blkno),
6577 le16_to_cpu(el->l_next_free_rec));
6578
Mark Fashehe48edee2007-03-07 16:46:57 -08006579 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006580
6581 if (le16_to_cpu(el->l_tree_depth) == 0) {
6582 /*
6583 * If the leaf block contains a single empty
6584 * extent and no records, we can just remove
6585 * the block.
6586 */
6587 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6588 memset(rec, 0,
6589 sizeof(struct ocfs2_extent_rec));
6590 el->l_next_free_rec = cpu_to_le16(0);
6591
6592 goto delete;
6593 }
6594
6595 /*
6596 * Remove any empty extents by shifting things
6597 * left. That should make life much easier on
6598 * the code below. This condition is rare
6599 * enough that we shouldn't see a performance
6600 * hit.
6601 */
6602 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6603 le16_add_cpu(&el->l_next_free_rec, -1);
6604
6605 for(i = 0;
6606 i < le16_to_cpu(el->l_next_free_rec); i++)
6607 el->l_recs[i] = el->l_recs[i + 1];
6608
6609 memset(&el->l_recs[i], 0,
6610 sizeof(struct ocfs2_extent_rec));
6611
6612 /*
6613 * We've modified our extent list. The
6614 * simplest way to handle this change
6615 * is to being the search from the
6616 * start again.
6617 */
6618 goto find_tail_record;
6619 }
6620
Mark Fashehe48edee2007-03-07 16:46:57 -08006621 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006622
6623 /*
6624 * We'll use "new_edge" on our way back up the
6625 * tree to know what our rightmost cpos is.
6626 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006627 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006628 new_edge += le32_to_cpu(rec->e_cpos);
6629
6630 /*
6631 * The caller will use this to delete data blocks.
6632 */
6633 *delete_start = le64_to_cpu(rec->e_blkno)
6634 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006635 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006636
6637 /*
6638 * If it's now empty, remove this record.
6639 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006640 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006641 memset(rec, 0,
6642 sizeof(struct ocfs2_extent_rec));
6643 le16_add_cpu(&el->l_next_free_rec, -1);
6644 }
6645 } else {
6646 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6647 memset(rec, 0,
6648 sizeof(struct ocfs2_extent_rec));
6649 le16_add_cpu(&el->l_next_free_rec, -1);
6650
6651 goto delete;
6652 }
6653
6654 /* Can this actually happen? */
6655 if (le16_to_cpu(el->l_next_free_rec) == 0)
6656 goto delete;
6657
6658 /*
6659 * We never actually deleted any clusters
6660 * because our leaf was empty. There's no
6661 * reason to adjust the rightmost edge then.
6662 */
6663 if (new_edge == 0)
6664 goto delete;
6665
Mark Fashehe48edee2007-03-07 16:46:57 -08006666 rec->e_int_clusters = cpu_to_le32(new_edge);
6667 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006668 -le32_to_cpu(rec->e_cpos));
6669
6670 /*
6671 * A deleted child record should have been
6672 * caught above.
6673 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006674 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006675 }
6676
6677delete:
6678 ret = ocfs2_journal_dirty(handle, bh);
6679 if (ret) {
6680 mlog_errno(ret);
6681 goto out;
6682 }
6683
6684 mlog(0, "extent list container %llu, after: record %d: "
6685 "(%u, %u, %llu), next = %u.\n",
6686 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006687 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006688 (unsigned long long)le64_to_cpu(rec->e_blkno),
6689 le16_to_cpu(el->l_next_free_rec));
6690
6691 /*
6692 * We must be careful to only attempt delete of an
6693 * extent block (and not the root inode block).
6694 */
6695 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6696 struct ocfs2_extent_block *eb =
6697 (struct ocfs2_extent_block *)bh->b_data;
6698
6699 /*
6700 * Save this for use when processing the
6701 * parent block.
6702 */
6703 deleted_eb = le64_to_cpu(eb->h_blkno);
6704
6705 mlog(0, "deleting this extent block.\n");
6706
Joel Becker8cb471e2009-02-10 20:00:41 -08006707 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006708
Mark Fashehe48edee2007-03-07 16:46:57 -08006709 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006710 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6711 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6712
Mark Fasheh59a5e412007-06-22 15:52:36 -07006713 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6714 /* An error here is not fatal. */
6715 if (ret < 0)
6716 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006717 } else {
6718 deleted_eb = 0;
6719 }
6720
6721 index--;
6722 }
6723
6724 ret = 0;
6725out:
6726 return ret;
6727}
6728
Mark Fashehccd979b2005-12-15 14:31:24 -08006729static int ocfs2_do_truncate(struct ocfs2_super *osb,
6730 unsigned int clusters_to_del,
6731 struct inode *inode,
6732 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006733 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006734 struct ocfs2_truncate_context *tc,
6735 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006736{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006737 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006738 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006739 struct ocfs2_extent_block *last_eb = NULL;
6740 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006741 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006742 u64 delete_blk = 0;
6743
6744 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6745
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006746 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006747 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006748 if (status < 0) {
6749 mlog_errno(status);
6750 goto bail;
6751 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006752
Mark Fashehdcd05382007-01-16 11:32:23 -08006753 /*
6754 * Each component will be touched, so we might as well journal
6755 * here to avoid having to handle errors later.
6756 */
Joel Becker0cf2f762009-02-12 16:41:25 -08006757 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006758 if (status < 0) {
6759 mlog_errno(status);
6760 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006761 }
6762
6763 if (last_eb_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08006764 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07006765 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08006766 if (status < 0) {
6767 mlog_errno(status);
6768 goto bail;
6769 }
6770
6771 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6772 }
6773
6774 el = &(fe->id2.i_list);
6775
6776 /*
6777 * Lower levels depend on this never happening, but it's best
6778 * to check it up here before changing the tree.
6779 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006780 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006781 ocfs2_error(inode->i_sb,
6782 "Inode %lu has an empty extent record, depth %u\n",
6783 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006784 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006785 goto bail;
6786 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006787
Jan Karaa90714c2008-10-09 19:38:40 +02006788 vfs_dq_free_space_nodirty(inode,
6789 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006790 spin_lock(&OCFS2_I(inode)->ip_lock);
6791 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6792 clusters_to_del;
6793 spin_unlock(&OCFS2_I(inode)->ip_lock);
6794 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006795 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006796
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006797 status = ocfs2_trim_tree(inode, path, handle, tc,
6798 clusters_to_del, &delete_blk);
6799 if (status) {
6800 mlog_errno(status);
6801 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006802 }
6803
Mark Fashehdcd05382007-01-16 11:32:23 -08006804 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006805 /* trunc to zero is a special case. */
6806 el->l_tree_depth = 0;
6807 fe->i_last_eb_blk = 0;
6808 } else if (last_eb)
6809 fe->i_last_eb_blk = last_eb->h_blkno;
6810
6811 status = ocfs2_journal_dirty(handle, fe_bh);
6812 if (status < 0) {
6813 mlog_errno(status);
6814 goto bail;
6815 }
6816
6817 if (last_eb) {
6818 /* If there will be a new last extent block, then by
6819 * definition, there cannot be any leaves to the right of
6820 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006821 last_eb->h_next_leaf_blk = 0;
6822 status = ocfs2_journal_dirty(handle, last_eb_bh);
6823 if (status < 0) {
6824 mlog_errno(status);
6825 goto bail;
6826 }
6827 }
6828
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006829 if (delete_blk) {
6830 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6831 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006832 if (status < 0) {
6833 mlog_errno(status);
6834 goto bail;
6835 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006836 }
6837 status = 0;
6838bail:
Tao Ma60e2ec42009-08-12 14:42:47 +08006839 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006840 mlog_exit(status);
6841 return status;
6842}
6843
Joel Becker2b4e30f2008-09-03 20:03:41 -07006844static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006845{
6846 set_buffer_uptodate(bh);
6847 mark_buffer_dirty(bh);
6848 return 0;
6849}
6850
Mark Fasheh1d410a62007-09-07 14:20:45 -07006851static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6852 unsigned int from, unsigned int to,
6853 struct page *page, int zero, u64 *phys)
6854{
6855 int ret, partial = 0;
6856
6857 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6858 if (ret)
6859 mlog_errno(ret);
6860
6861 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006862 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006863
6864 /*
6865 * Need to set the buffers we zero'd into uptodate
6866 * here if they aren't - ocfs2_map_page_blocks()
6867 * might've skipped some
6868 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006869 ret = walk_page_buffers(handle, page_buffers(page),
6870 from, to, &partial,
6871 ocfs2_zero_func);
6872 if (ret < 0)
6873 mlog_errno(ret);
6874 else if (ocfs2_should_order_data(inode)) {
6875 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006876 if (ret < 0)
6877 mlog_errno(ret);
6878 }
6879
6880 if (!partial)
6881 SetPageUptodate(page);
6882
6883 flush_dcache_page(page);
6884}
6885
Mark Fasheh35edec12007-07-06 14:41:18 -07006886static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6887 loff_t end, struct page **pages,
6888 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006889{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006890 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006891 struct page *page;
6892 unsigned int from, to = PAGE_CACHE_SIZE;
6893 struct super_block *sb = inode->i_sb;
6894
6895 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6896
6897 if (numpages == 0)
6898 goto out;
6899
Mark Fasheh35edec12007-07-06 14:41:18 -07006900 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006901 for(i = 0; i < numpages; i++) {
6902 page = pages[i];
6903
Mark Fasheh35edec12007-07-06 14:41:18 -07006904 from = start & (PAGE_CACHE_SIZE - 1);
6905 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6906 to = end & (PAGE_CACHE_SIZE - 1);
6907
Mark Fasheh60b11392007-02-16 11:46:50 -08006908 BUG_ON(from > PAGE_CACHE_SIZE);
6909 BUG_ON(to > PAGE_CACHE_SIZE);
6910
Mark Fasheh1d410a62007-09-07 14:20:45 -07006911 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6912 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006913
Mark Fasheh35edec12007-07-06 14:41:18 -07006914 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006915 }
6916out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006917 if (pages)
6918 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006919}
6920
Mark Fasheh35edec12007-07-06 14:41:18 -07006921static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006922 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006923{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006924 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006925 struct super_block *sb = inode->i_sb;
6926 struct address_space *mapping = inode->i_mapping;
6927 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006928 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006929
Mark Fasheh35edec12007-07-06 14:41:18 -07006930 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006931
Mark Fasheh35edec12007-07-06 14:41:18 -07006932 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6933 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6934
Mark Fasheh1d410a62007-09-07 14:20:45 -07006935 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006936 last_page_bytes = PAGE_ALIGN(end);
6937 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006938 do {
6939 pages[numpages] = grab_cache_page(mapping, index);
6940 if (!pages[numpages]) {
6941 ret = -ENOMEM;
6942 mlog_errno(ret);
6943 goto out;
6944 }
6945
6946 numpages++;
6947 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006948 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006949
6950out:
6951 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006952 if (pages)
6953 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006954 numpages = 0;
6955 }
6956
6957 *num = numpages;
6958
6959 return ret;
6960}
6961
6962/*
6963 * Zero the area past i_size but still within an allocated
6964 * cluster. This avoids exposing nonzero data on subsequent file
6965 * extends.
6966 *
6967 * We need to call this before i_size is updated on the inode because
6968 * otherwise block_write_full_page() will skip writeout of pages past
6969 * i_size. The new_i_size parameter is passed for this reason.
6970 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006971int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6972 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006973{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006974 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006975 struct page **pages = NULL;
6976 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006977 unsigned int ext_flags;
6978 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006979
6980 /*
6981 * File systems which don't support sparse files zero on every
6982 * extend.
6983 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07006984 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08006985 return 0;
6986
Mark Fasheh1d410a62007-09-07 14:20:45 -07006987 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08006988 sizeof(struct page *), GFP_NOFS);
6989 if (pages == NULL) {
6990 ret = -ENOMEM;
6991 mlog_errno(ret);
6992 goto out;
6993 }
6994
Mark Fasheh1d410a62007-09-07 14:20:45 -07006995 if (range_start == range_end)
6996 goto out;
6997
6998 ret = ocfs2_extent_map_get_blocks(inode,
6999 range_start >> sb->s_blocksize_bits,
7000 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08007001 if (ret) {
7002 mlog_errno(ret);
7003 goto out;
7004 }
7005
Mark Fasheh1d410a62007-09-07 14:20:45 -07007006 /*
7007 * Tail is a hole, or is marked unwritten. In either case, we
7008 * can count on read and write to return/push zero's.
7009 */
7010 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08007011 goto out;
7012
Mark Fasheh1d410a62007-09-07 14:20:45 -07007013 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7014 &numpages);
7015 if (ret) {
7016 mlog_errno(ret);
7017 goto out;
7018 }
7019
Mark Fasheh35edec12007-07-06 14:41:18 -07007020 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7021 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08007022
7023 /*
7024 * Initiate writeout of the pages we zero'd here. We don't
7025 * wait on them - the truncate_inode_pages() call later will
7026 * do that for us.
7027 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007028 ret = do_sync_mapping_range(inode->i_mapping, range_start,
7029 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08007030 if (ret)
7031 mlog_errno(ret);
7032
7033out:
7034 if (pages)
7035 kfree(pages);
7036
7037 return ret;
7038}
7039
Tiger Yangfdd77702008-08-18 17:08:55 +08007040static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7041 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007042{
7043 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08007044 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007045
Tiger Yangfdd77702008-08-18 17:08:55 +08007046 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7047 memset(&di->id2, 0, blocksize -
7048 offsetof(struct ocfs2_dinode, id2) -
7049 xattrsize);
7050 else
7051 memset(&di->id2, 0, blocksize -
7052 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007053}
7054
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007055void ocfs2_dinode_new_extent_list(struct inode *inode,
7056 struct ocfs2_dinode *di)
7057{
Tiger Yangfdd77702008-08-18 17:08:55 +08007058 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007059 di->id2.i_list.l_tree_depth = 0;
7060 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08007061 di->id2.i_list.l_count = cpu_to_le16(
7062 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007063}
7064
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007065void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7066{
7067 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7068 struct ocfs2_inline_data *idata = &di->id2.i_data;
7069
7070 spin_lock(&oi->ip_lock);
7071 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7072 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7073 spin_unlock(&oi->ip_lock);
7074
7075 /*
7076 * We clear the entire i_data structure here so that all
7077 * fields can be properly initialized.
7078 */
Tiger Yangfdd77702008-08-18 17:08:55 +08007079 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007080
Tiger Yangfdd77702008-08-18 17:08:55 +08007081 idata->id_count = cpu_to_le16(
7082 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007083}
7084
7085int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7086 struct buffer_head *di_bh)
7087{
7088 int ret, i, has_data, num_pages = 0;
7089 handle_t *handle;
7090 u64 uninitialized_var(block);
7091 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7092 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7093 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007094 struct ocfs2_alloc_context *data_ac = NULL;
7095 struct page **pages = NULL;
7096 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07007097 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02007098 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007099
7100 has_data = i_size_read(inode) ? 1 : 0;
7101
7102 if (has_data) {
7103 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7104 sizeof(struct page *), GFP_NOFS);
7105 if (pages == NULL) {
7106 ret = -ENOMEM;
7107 mlog_errno(ret);
7108 goto out;
7109 }
7110
7111 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7112 if (ret) {
7113 mlog_errno(ret);
7114 goto out;
7115 }
7116 }
7117
Jan Karaa90714c2008-10-09 19:38:40 +02007118 handle = ocfs2_start_trans(osb,
7119 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007120 if (IS_ERR(handle)) {
7121 ret = PTR_ERR(handle);
7122 mlog_errno(ret);
7123 goto out_unlock;
7124 }
7125
Joel Becker0cf2f762009-02-12 16:41:25 -08007126 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007127 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007128 if (ret) {
7129 mlog_errno(ret);
7130 goto out_commit;
7131 }
7132
7133 if (has_data) {
7134 u32 bit_off, num;
7135 unsigned int page_end;
7136 u64 phys;
7137
Jan Karaa90714c2008-10-09 19:38:40 +02007138 if (vfs_dq_alloc_space_nodirty(inode,
7139 ocfs2_clusters_to_bytes(osb->sb, 1))) {
7140 ret = -EDQUOT;
7141 goto out_commit;
7142 }
7143 did_quota = 1;
7144
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007145 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7146 &num);
7147 if (ret) {
7148 mlog_errno(ret);
7149 goto out_commit;
7150 }
7151
7152 /*
7153 * Save two copies, one for insert, and one that can
7154 * be changed by ocfs2_map_and_dirty_page() below.
7155 */
7156 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7157
7158 /*
7159 * Non sparse file systems zero on extend, so no need
7160 * to do that now.
7161 */
7162 if (!ocfs2_sparse_alloc(osb) &&
7163 PAGE_CACHE_SIZE < osb->s_clustersize)
7164 end = PAGE_CACHE_SIZE;
7165
7166 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7167 if (ret) {
7168 mlog_errno(ret);
7169 goto out_commit;
7170 }
7171
7172 /*
7173 * This should populate the 1st page for us and mark
7174 * it up to date.
7175 */
7176 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7177 if (ret) {
7178 mlog_errno(ret);
7179 goto out_commit;
7180 }
7181
7182 page_end = PAGE_CACHE_SIZE;
7183 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7184 page_end = osb->s_clustersize;
7185
7186 for (i = 0; i < num_pages; i++)
7187 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7188 pages[i], i > 0, &phys);
7189 }
7190
7191 spin_lock(&oi->ip_lock);
7192 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7193 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7194 spin_unlock(&oi->ip_lock);
7195
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007196 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007197
7198 ocfs2_journal_dirty(handle, di_bh);
7199
7200 if (has_data) {
7201 /*
7202 * An error at this point should be extremely rare. If
7203 * this proves to be false, we could always re-build
7204 * the in-inode data from our pages.
7205 */
Joel Becker8d6220d2008-08-22 12:46:09 -07007206 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07007207 ret = ocfs2_insert_extent(osb, handle, inode, &et,
7208 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007209 if (ret) {
7210 mlog_errno(ret);
7211 goto out_commit;
7212 }
7213
7214 inode->i_blocks = ocfs2_inode_sector_count(inode);
7215 }
7216
7217out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007218 if (ret < 0 && did_quota)
7219 vfs_dq_free_space_nodirty(inode,
7220 ocfs2_clusters_to_bytes(osb->sb, 1));
7221
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007222 ocfs2_commit_trans(osb, handle);
7223
7224out_unlock:
7225 if (data_ac)
7226 ocfs2_free_alloc_context(data_ac);
7227
7228out:
7229 if (pages) {
7230 ocfs2_unlock_and_free_pages(pages, num_pages);
7231 kfree(pages);
7232 }
7233
7234 return ret;
7235}
7236
Mark Fashehccd979b2005-12-15 14:31:24 -08007237/*
7238 * It is expected, that by the time you call this function,
7239 * inode->i_size and fe->i_size have been adjusted.
7240 *
7241 * WARNING: This will kfree the truncate context
7242 */
7243int ocfs2_commit_truncate(struct ocfs2_super *osb,
7244 struct inode *inode,
7245 struct buffer_head *fe_bh,
7246 struct ocfs2_truncate_context *tc)
7247{
7248 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08007249 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08007250 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07007251 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007252 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08007253 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08007254 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007255
7256 mlog_entry_void();
7257
Mark Fashehdcd05382007-01-16 11:32:23 -08007258 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007259 i_size_read(inode));
7260
Joel Becker13723d02008-10-17 19:25:01 -07007261 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7262 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08007263 if (!path) {
7264 status = -ENOMEM;
7265 mlog_errno(status);
7266 goto bail;
7267 }
Mark Fasheh83418972007-04-23 18:53:12 -07007268
7269 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7270
Mark Fashehccd979b2005-12-15 14:31:24 -08007271start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007272 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007273 * Check that we still have allocation to delete.
7274 */
7275 if (OCFS2_I(inode)->ip_clusters == 0) {
7276 status = 0;
7277 goto bail;
7278 }
7279
7280 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007281 * Truncate always works against the rightmost tree branch.
7282 */
Joel Beckerfacdb772009-02-12 18:08:48 -08007283 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
Mark Fashehdcd05382007-01-16 11:32:23 -08007284 if (status) {
7285 mlog_errno(status);
7286 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007287 }
7288
Mark Fashehdcd05382007-01-16 11:32:23 -08007289 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7290 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7291
7292 /*
7293 * By now, el will point to the extent list on the bottom most
7294 * portion of this tree. Only the tail record is considered in
7295 * each pass.
7296 *
7297 * We handle the following cases, in order:
7298 * - empty extent: delete the remaining branch
7299 * - remove the entire record
7300 * - remove a partial record
7301 * - no record needs to be removed (truncate has completed)
7302 */
7303 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007304 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7305 ocfs2_error(inode->i_sb,
7306 "Inode %llu has empty extent block at %llu\n",
7307 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7308 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7309 status = -EROFS;
7310 goto bail;
7311 }
7312
Mark Fashehccd979b2005-12-15 14:31:24 -08007313 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08007314 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08007315 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007316 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7317 clusters_to_del = 0;
7318 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007319 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007320 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007321 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08007322 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08007323 new_highest_cpos;
7324 } else {
7325 status = 0;
7326 goto bail;
7327 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007328
Mark Fashehdcd05382007-01-16 11:32:23 -08007329 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7330 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7331
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007332 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007333 tl_sem = 1;
7334 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7335 * record is free for use. If there isn't any, we flush to get
7336 * an empty truncate log. */
7337 if (ocfs2_truncate_log_needs_flush(osb)) {
7338 status = __ocfs2_flush_truncate_log(osb);
7339 if (status < 0) {
7340 mlog_errno(status);
7341 goto bail;
7342 }
7343 }
7344
7345 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007346 (struct ocfs2_dinode *)fe_bh->b_data,
7347 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007348 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007349 if (IS_ERR(handle)) {
7350 status = PTR_ERR(handle);
7351 handle = NULL;
7352 mlog_errno(status);
7353 goto bail;
7354 }
7355
Mark Fashehdcd05382007-01-16 11:32:23 -08007356 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7357 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007358 if (status < 0) {
7359 mlog_errno(status);
7360 goto bail;
7361 }
7362
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007363 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007364 tl_sem = 0;
7365
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007366 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007367 handle = NULL;
7368
Mark Fashehdcd05382007-01-16 11:32:23 -08007369 ocfs2_reinit_path(path, 1);
7370
7371 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007372 * The check above will catch the case where we've truncated
7373 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007374 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007375 goto start;
7376
Mark Fashehccd979b2005-12-15 14:31:24 -08007377bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007378
7379 ocfs2_schedule_truncate_log_flush(osb, 1);
7380
7381 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007382 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007383
7384 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007385 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007386
Mark Fasheh59a5e412007-06-22 15:52:36 -07007387 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7388
Mark Fashehdcd05382007-01-16 11:32:23 -08007389 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007390
7391 /* This will drop the ext_alloc cluster lock for us */
7392 ocfs2_free_truncate_context(tc);
7393
7394 mlog_exit(status);
7395 return status;
7396}
7397
Mark Fashehccd979b2005-12-15 14:31:24 -08007398/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007399 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007400 */
7401int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7402 struct inode *inode,
7403 struct buffer_head *fe_bh,
7404 struct ocfs2_truncate_context **tc)
7405{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007406 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007407 unsigned int new_i_clusters;
7408 struct ocfs2_dinode *fe;
7409 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007410 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007411
7412 mlog_entry_void();
7413
7414 *tc = NULL;
7415
7416 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7417 i_size_read(inode));
7418 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7419
7420 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007421 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7422 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007423
Robert P. J. Daycd861282006-12-13 00:34:52 -08007424 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007425 if (!(*tc)) {
7426 status = -ENOMEM;
7427 mlog_errno(status);
7428 goto bail;
7429 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007430 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007431
Mark Fashehccd979b2005-12-15 14:31:24 -08007432 if (fe->id2.i_list.l_tree_depth) {
Joel Becker3d03a302009-02-12 17:49:26 -08007433 status = ocfs2_read_extent_block(INODE_CACHE(inode),
Joel Becker5e965812008-11-13 14:49:16 -08007434 le64_to_cpu(fe->i_last_eb_blk),
7435 &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007436 if (status < 0) {
7437 mlog_errno(status);
7438 goto bail;
7439 }
7440 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007441 }
7442
7443 (*tc)->tc_last_eb_bh = last_eb_bh;
7444
Mark Fashehccd979b2005-12-15 14:31:24 -08007445 status = 0;
7446bail:
7447 if (status < 0) {
7448 if (*tc)
7449 ocfs2_free_truncate_context(*tc);
7450 *tc = NULL;
7451 }
7452 mlog_exit_void();
7453 return status;
7454}
7455
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007456/*
7457 * 'start' is inclusive, 'end' is not.
7458 */
7459int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7460 unsigned int start, unsigned int end, int trunc)
7461{
7462 int ret;
7463 unsigned int numbytes;
7464 handle_t *handle;
7465 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7466 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7467 struct ocfs2_inline_data *idata = &di->id2.i_data;
7468
7469 if (end > i_size_read(inode))
7470 end = i_size_read(inode);
7471
7472 BUG_ON(start >= end);
7473
7474 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7475 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7476 !ocfs2_supports_inline_data(osb)) {
7477 ocfs2_error(inode->i_sb,
7478 "Inline data flags for inode %llu don't agree! "
7479 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7480 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7481 le16_to_cpu(di->i_dyn_features),
7482 OCFS2_I(inode)->ip_dyn_features,
7483 osb->s_feature_incompat);
7484 ret = -EROFS;
7485 goto out;
7486 }
7487
7488 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7489 if (IS_ERR(handle)) {
7490 ret = PTR_ERR(handle);
7491 mlog_errno(ret);
7492 goto out;
7493 }
7494
Joel Becker0cf2f762009-02-12 16:41:25 -08007495 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007496 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007497 if (ret) {
7498 mlog_errno(ret);
7499 goto out_commit;
7500 }
7501
7502 numbytes = end - start;
7503 memset(idata->id_data + start, 0, numbytes);
7504
7505 /*
7506 * No need to worry about the data page here - it's been
7507 * truncated already and inline data doesn't need it for
7508 * pushing zero's to disk, so we'll let readpage pick it up
7509 * later.
7510 */
7511 if (trunc) {
7512 i_size_write(inode, start);
7513 di->i_size = cpu_to_le64(start);
7514 }
7515
7516 inode->i_blocks = ocfs2_inode_sector_count(inode);
7517 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7518
7519 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7520 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7521
7522 ocfs2_journal_dirty(handle, di_bh);
7523
7524out_commit:
7525 ocfs2_commit_trans(osb, handle);
7526
7527out:
7528 return ret;
7529}
7530
Mark Fashehccd979b2005-12-15 14:31:24 -08007531static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7532{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007533 /*
7534 * The caller is responsible for completing deallocation
7535 * before freeing the context.
7536 */
7537 if (tc->tc_dealloc.c_first_suballocator != NULL)
7538 mlog(ML_NOTICE,
7539 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007540
Mark Fasheha81cb882008-10-07 14:25:16 -07007541 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007542
7543 kfree(tc);
7544}