blob: 072f7fe54073128321edc8eccf8cd8f6c447370e [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 Becker5c601ab2009-02-12 19:10:13 -08002311static int ocfs2_rotate_tree_right(struct inode *inode, handle_t *handle,
2312 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
Tao Ma3c5e1062009-07-21 15:42:05 +08002468static int ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2469 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002470{
Tao Ma3c5e1062009-07-21 15:42:05 +08002471 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002472 struct ocfs2_extent_rec *rec;
2473 struct ocfs2_extent_list *el;
2474 struct ocfs2_extent_block *eb;
2475 u32 range;
2476
Tao Ma3c5e1062009-07-21 15:42:05 +08002477 /*
2478 * In normal tree rotation process, we will never touch the
2479 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2480 * doesn't reserve the credits for them either.
2481 *
2482 * But we do have a special case here which will update the rightmost
2483 * records for all the bh in the path.
2484 * So we have to allocate extra credits and access them.
2485 */
2486 ret = ocfs2_extend_trans(handle,
2487 handle->h_buffer_credits + subtree_index);
2488 if (ret) {
2489 mlog_errno(ret);
2490 goto out;
2491 }
2492
Joel Becker0cf2f762009-02-12 16:41:25 -08002493 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002494 if (ret) {
2495 mlog_errno(ret);
2496 goto out;
2497 }
2498
Mark Fasheh328d5752007-06-18 10:48:04 -07002499 /* Path should always be rightmost. */
2500 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2501 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2502
2503 el = &eb->h_list;
2504 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2505 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2506 rec = &el->l_recs[idx];
2507 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2508
2509 for (i = 0; i < path->p_tree_depth; i++) {
2510 el = path->p_node[i].el;
2511 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2512 rec = &el->l_recs[idx];
2513
2514 rec->e_int_clusters = cpu_to_le32(range);
2515 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2516
2517 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2518 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002519out:
2520 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002521}
2522
Joel Becker6641b0c2009-02-12 18:57:52 -08002523static void ocfs2_unlink_path(handle_t *handle,
2524 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002525 struct ocfs2_cached_dealloc_ctxt *dealloc,
2526 struct ocfs2_path *path, int unlink_start)
2527{
2528 int ret, i;
2529 struct ocfs2_extent_block *eb;
2530 struct ocfs2_extent_list *el;
2531 struct buffer_head *bh;
2532
2533 for(i = unlink_start; i < path_num_items(path); i++) {
2534 bh = path->p_node[i].bh;
2535
2536 eb = (struct ocfs2_extent_block *)bh->b_data;
2537 /*
2538 * Not all nodes might have had their final count
2539 * decremented by the caller - handle this here.
2540 */
2541 el = &eb->h_list;
2542 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2543 mlog(ML_ERROR,
2544 "Inode %llu, attempted to remove extent block "
2545 "%llu with %u records\n",
Joel Becker6641b0c2009-02-12 18:57:52 -08002546 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07002547 (unsigned long long)le64_to_cpu(eb->h_blkno),
2548 le16_to_cpu(el->l_next_free_rec));
2549
2550 ocfs2_journal_dirty(handle, bh);
Joel Becker6641b0c2009-02-12 18:57:52 -08002551 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002552 continue;
2553 }
2554
2555 el->l_next_free_rec = 0;
2556 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2557
2558 ocfs2_journal_dirty(handle, bh);
2559
2560 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2561 if (ret)
2562 mlog_errno(ret);
2563
Joel Becker6641b0c2009-02-12 18:57:52 -08002564 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002565 }
2566}
2567
Joel Becker6641b0c2009-02-12 18:57:52 -08002568static void ocfs2_unlink_subtree(handle_t *handle,
2569 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002570 struct ocfs2_path *left_path,
2571 struct ocfs2_path *right_path,
2572 int subtree_index,
2573 struct ocfs2_cached_dealloc_ctxt *dealloc)
2574{
2575 int i;
2576 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2577 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2578 struct ocfs2_extent_list *el;
2579 struct ocfs2_extent_block *eb;
2580
2581 el = path_leaf_el(left_path);
2582
2583 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2584
2585 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2586 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2587 break;
2588
2589 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2590
2591 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2592 le16_add_cpu(&root_el->l_next_free_rec, -1);
2593
2594 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2595 eb->h_next_leaf_blk = 0;
2596
2597 ocfs2_journal_dirty(handle, root_bh);
2598 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2599
Joel Becker6641b0c2009-02-12 18:57:52 -08002600 ocfs2_unlink_path(handle, et, dealloc, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002601 subtree_index + 1);
2602}
2603
2604static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2605 struct ocfs2_path *left_path,
2606 struct ocfs2_path *right_path,
2607 int subtree_index,
2608 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002609 int *deleted,
2610 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002611{
2612 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002613 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002614 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2615 struct ocfs2_extent_block *eb;
2616
2617 *deleted = 0;
2618
2619 right_leaf_el = path_leaf_el(right_path);
2620 left_leaf_el = path_leaf_el(left_path);
2621 root_bh = left_path->p_node[subtree_index].bh;
2622 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2623
2624 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2625 return 0;
2626
2627 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2628 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2629 /*
2630 * It's legal for us to proceed if the right leaf is
2631 * the rightmost one and it has an empty extent. There
2632 * are two cases to handle - whether the leaf will be
2633 * empty after removal or not. If the leaf isn't empty
2634 * then just remove the empty extent up front. The
2635 * next block will handle empty leaves by flagging
2636 * them for unlink.
2637 *
2638 * Non rightmost leaves will throw -EAGAIN and the
2639 * caller can manually move the subtree and retry.
2640 */
2641
2642 if (eb->h_next_leaf_blk != 0ULL)
2643 return -EAGAIN;
2644
2645 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker0cf2f762009-02-12 16:41:25 -08002646 ret = ocfs2_journal_access_eb(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002647 path_leaf_bh(right_path),
2648 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002649 if (ret) {
2650 mlog_errno(ret);
2651 goto out;
2652 }
2653
2654 ocfs2_remove_empty_extent(right_leaf_el);
2655 } else
2656 right_has_empty = 1;
2657 }
2658
2659 if (eb->h_next_leaf_blk == 0ULL &&
2660 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2661 /*
2662 * We have to update i_last_eb_blk during the meta
2663 * data delete.
2664 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08002665 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07002666 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002667 if (ret) {
2668 mlog_errno(ret);
2669 goto out;
2670 }
2671
2672 del_right_subtree = 1;
2673 }
2674
2675 /*
2676 * Getting here with an empty extent in the right path implies
2677 * that it's the rightmost path and will be deleted.
2678 */
2679 BUG_ON(right_has_empty && !del_right_subtree);
2680
Joel Becker0cf2f762009-02-12 16:41:25 -08002681 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002682 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002683 if (ret) {
2684 mlog_errno(ret);
2685 goto out;
2686 }
2687
2688 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -08002689 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002690 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002691 if (ret) {
2692 mlog_errno(ret);
2693 goto out;
2694 }
2695
Joel Becker0cf2f762009-02-12 16:41:25 -08002696 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002697 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002698 if (ret) {
2699 mlog_errno(ret);
2700 goto out;
2701 }
2702 }
2703
2704 if (!right_has_empty) {
2705 /*
2706 * Only do this if we're moving a real
2707 * record. Otherwise, the action is delayed until
2708 * after removal of the right path in which case we
2709 * can do a simple shift to remove the empty extent.
2710 */
2711 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2712 memset(&right_leaf_el->l_recs[0], 0,
2713 sizeof(struct ocfs2_extent_rec));
2714 }
2715 if (eb->h_next_leaf_blk == 0ULL) {
2716 /*
2717 * Move recs over to get rid of empty extent, decrease
2718 * next_free. This is allowed to remove the last
2719 * extent in our leaf (setting l_next_free_rec to
2720 * zero) - the delete code below won't care.
2721 */
2722 ocfs2_remove_empty_extent(right_leaf_el);
2723 }
2724
2725 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2726 if (ret)
2727 mlog_errno(ret);
2728 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2729 if (ret)
2730 mlog_errno(ret);
2731
2732 if (del_right_subtree) {
Joel Becker6641b0c2009-02-12 18:57:52 -08002733 ocfs2_unlink_subtree(handle, et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002734 subtree_index, dealloc);
Tao Ma3c5e1062009-07-21 15:42:05 +08002735 ret = ocfs2_update_edge_lengths(inode, handle, subtree_index,
2736 left_path);
2737 if (ret) {
2738 mlog_errno(ret);
2739 goto out;
2740 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002741
2742 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002743 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002744
2745 /*
2746 * Removal of the extent in the left leaf was skipped
2747 * above so we could delete the right path
2748 * 1st.
2749 */
2750 if (right_has_empty)
2751 ocfs2_remove_empty_extent(left_leaf_el);
2752
Tao Mae7d4cb62008-08-18 17:38:44 +08002753 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002754 if (ret)
2755 mlog_errno(ret);
2756
2757 *deleted = 1;
2758 } else
Joel Becker4619c732009-02-12 19:02:36 -08002759 ocfs2_complete_edge_insert(handle, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002760 subtree_index);
2761
2762out:
2763 return ret;
2764}
2765
2766/*
2767 * Given a full path, determine what cpos value would return us a path
2768 * containing the leaf immediately to the right of the current one.
2769 *
2770 * Will return zero if the path passed in is already the rightmost path.
2771 *
2772 * This looks similar, but is subtly different to
2773 * ocfs2_find_cpos_for_left_leaf().
2774 */
2775static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2776 struct ocfs2_path *path, u32 *cpos)
2777{
2778 int i, j, ret = 0;
2779 u64 blkno;
2780 struct ocfs2_extent_list *el;
2781
2782 *cpos = 0;
2783
2784 if (path->p_tree_depth == 0)
2785 return 0;
2786
2787 blkno = path_leaf_bh(path)->b_blocknr;
2788
2789 /* Start at the tree node just above the leaf and work our way up. */
2790 i = path->p_tree_depth - 1;
2791 while (i >= 0) {
2792 int next_free;
2793
2794 el = path->p_node[i].el;
2795
2796 /*
2797 * Find the extent record just after the one in our
2798 * path.
2799 */
2800 next_free = le16_to_cpu(el->l_next_free_rec);
2801 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2802 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2803 if (j == (next_free - 1)) {
2804 if (i == 0) {
2805 /*
2806 * We've determined that the
2807 * path specified is already
2808 * the rightmost one - return a
2809 * cpos of zero.
2810 */
2811 goto out;
2812 }
2813 /*
2814 * The rightmost record points to our
2815 * leaf - we need to travel up the
2816 * tree one level.
2817 */
2818 goto next_node;
2819 }
2820
2821 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2822 goto out;
2823 }
2824 }
2825
2826 /*
2827 * If we got here, we never found a valid node where
2828 * the tree indicated one should be.
2829 */
2830 ocfs2_error(sb,
2831 "Invalid extent tree at extent block %llu\n",
2832 (unsigned long long)blkno);
2833 ret = -EROFS;
2834 goto out;
2835
2836next_node:
2837 blkno = path->p_node[i].bh->b_blocknr;
2838 i--;
2839 }
2840
2841out:
2842 return ret;
2843}
2844
2845static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2846 handle_t *handle,
Joel Becker13723d02008-10-17 19:25:01 -07002847 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002848{
2849 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002850 struct buffer_head *bh = path_leaf_bh(path);
2851 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002852
2853 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2854 return 0;
2855
Joel Becker0cf2f762009-02-12 16:41:25 -08002856 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path,
Joel Becker13723d02008-10-17 19:25:01 -07002857 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002858 if (ret) {
2859 mlog_errno(ret);
2860 goto out;
2861 }
2862
2863 ocfs2_remove_empty_extent(el);
2864
2865 ret = ocfs2_journal_dirty(handle, bh);
2866 if (ret)
2867 mlog_errno(ret);
2868
2869out:
2870 return ret;
2871}
2872
2873static int __ocfs2_rotate_tree_left(struct inode *inode,
2874 handle_t *handle, int orig_credits,
2875 struct ocfs2_path *path,
2876 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002877 struct ocfs2_path **empty_extent_path,
2878 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002879{
2880 int ret, subtree_root, deleted;
2881 u32 right_cpos;
2882 struct ocfs2_path *left_path = NULL;
2883 struct ocfs2_path *right_path = NULL;
2884
2885 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2886
2887 *empty_extent_path = NULL;
2888
2889 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2890 &right_cpos);
2891 if (ret) {
2892 mlog_errno(ret);
2893 goto out;
2894 }
2895
Joel Beckerffdd7a52008-10-17 22:32:01 -07002896 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002897 if (!left_path) {
2898 ret = -ENOMEM;
2899 mlog_errno(ret);
2900 goto out;
2901 }
2902
2903 ocfs2_cp_path(left_path, path);
2904
Joel Beckerffdd7a52008-10-17 22:32:01 -07002905 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002906 if (!right_path) {
2907 ret = -ENOMEM;
2908 mlog_errno(ret);
2909 goto out;
2910 }
2911
2912 while (right_cpos) {
Joel Beckerfacdb772009-02-12 18:08:48 -08002913 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002914 if (ret) {
2915 mlog_errno(ret);
2916 goto out;
2917 }
2918
Joel Becker7dc02802009-02-12 19:20:13 -08002919 subtree_root = ocfs2_find_subtree_root(et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002920 right_path);
2921
2922 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2923 subtree_root,
2924 (unsigned long long)
2925 right_path->p_node[subtree_root].bh->b_blocknr,
2926 right_path->p_tree_depth);
2927
2928 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2929 orig_credits, left_path);
2930 if (ret) {
2931 mlog_errno(ret);
2932 goto out;
2933 }
2934
Mark Fashehe8aed342007-12-03 16:43:01 -08002935 /*
2936 * Caller might still want to make changes to the
2937 * tree root, so re-add it to the journal here.
2938 */
Joel Becker0cf2f762009-02-12 16:41:25 -08002939 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07002940 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08002941 if (ret) {
2942 mlog_errno(ret);
2943 goto out;
2944 }
2945
Mark Fasheh328d5752007-06-18 10:48:04 -07002946 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2947 right_path, subtree_root,
Tao Mae7d4cb62008-08-18 17:38:44 +08002948 dealloc, &deleted, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002949 if (ret == -EAGAIN) {
2950 /*
2951 * The rotation has to temporarily stop due to
2952 * the right subtree having an empty
2953 * extent. Pass it back to the caller for a
2954 * fixup.
2955 */
2956 *empty_extent_path = right_path;
2957 right_path = NULL;
2958 goto out;
2959 }
2960 if (ret) {
2961 mlog_errno(ret);
2962 goto out;
2963 }
2964
2965 /*
2966 * The subtree rotate might have removed records on
2967 * the rightmost edge. If so, then rotation is
2968 * complete.
2969 */
2970 if (deleted)
2971 break;
2972
2973 ocfs2_mv_path(left_path, right_path);
2974
2975 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2976 &right_cpos);
2977 if (ret) {
2978 mlog_errno(ret);
2979 goto out;
2980 }
2981 }
2982
2983out:
2984 ocfs2_free_path(right_path);
2985 ocfs2_free_path(left_path);
2986
2987 return ret;
2988}
2989
2990static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08002991 struct ocfs2_path *path,
2992 struct ocfs2_cached_dealloc_ctxt *dealloc,
2993 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002994{
2995 int ret, subtree_index;
2996 u32 cpos;
2997 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07002998 struct ocfs2_extent_block *eb;
2999 struct ocfs2_extent_list *el;
3000
Mark Fasheh328d5752007-06-18 10:48:04 -07003001
Joel Becker6136ca52009-02-12 19:32:43 -08003002 ret = ocfs2_et_sanity_check(et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003003 if (ret)
3004 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003005 /*
3006 * There's two ways we handle this depending on
3007 * whether path is the only existing one.
3008 */
3009 ret = ocfs2_extend_rotate_transaction(handle, 0,
3010 handle->h_buffer_credits,
3011 path);
3012 if (ret) {
3013 mlog_errno(ret);
3014 goto out;
3015 }
3016
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003017 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003018 if (ret) {
3019 mlog_errno(ret);
3020 goto out;
3021 }
3022
Joel Becker3d03a302009-02-12 17:49:26 -08003023 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3024 path, &cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003025 if (ret) {
3026 mlog_errno(ret);
3027 goto out;
3028 }
3029
3030 if (cpos) {
3031 /*
3032 * We have a path to the left of this one - it needs
3033 * an update too.
3034 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003035 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003036 if (!left_path) {
3037 ret = -ENOMEM;
3038 mlog_errno(ret);
3039 goto out;
3040 }
3041
Joel Beckerfacdb772009-02-12 18:08:48 -08003042 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003043 if (ret) {
3044 mlog_errno(ret);
3045 goto out;
3046 }
3047
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003048 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003049 if (ret) {
3050 mlog_errno(ret);
3051 goto out;
3052 }
3053
Joel Becker7dc02802009-02-12 19:20:13 -08003054 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003055
Joel Becker6641b0c2009-02-12 18:57:52 -08003056 ocfs2_unlink_subtree(handle, et, left_path, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003057 subtree_index, dealloc);
Tao Ma3c5e1062009-07-21 15:42:05 +08003058 ret = ocfs2_update_edge_lengths(inode, handle, subtree_index,
3059 left_path);
3060 if (ret) {
3061 mlog_errno(ret);
3062 goto out;
3063 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003064
3065 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003066 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003067 } else {
3068 /*
3069 * 'path' is also the leftmost path which
3070 * means it must be the only one. This gets
3071 * handled differently because we want to
3072 * revert the inode back to having extents
3073 * in-line.
3074 */
Joel Becker6641b0c2009-02-12 18:57:52 -08003075 ocfs2_unlink_path(handle, et, dealloc, path, 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003076
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003077 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003078 el->l_tree_depth = 0;
3079 el->l_next_free_rec = 0;
3080 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3081
Joel Becker35dc0aa2008-08-20 16:25:06 -07003082 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003083 }
3084
3085 ocfs2_journal_dirty(handle, path_root_bh(path));
3086
3087out:
3088 ocfs2_free_path(left_path);
3089 return ret;
3090}
3091
3092/*
3093 * Left rotation of btree records.
3094 *
3095 * In many ways, this is (unsurprisingly) the opposite of right
3096 * rotation. We start at some non-rightmost path containing an empty
3097 * extent in the leaf block. The code works its way to the rightmost
3098 * path by rotating records to the left in every subtree.
3099 *
3100 * This is used by any code which reduces the number of extent records
3101 * in a leaf. After removal, an empty record should be placed in the
3102 * leftmost list position.
3103 *
3104 * This won't handle a length update of the rightmost path records if
3105 * the rightmost tree leaf record is removed so the caller is
3106 * responsible for detecting and correcting that.
3107 */
3108static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
3109 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003110 struct ocfs2_cached_dealloc_ctxt *dealloc,
3111 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003112{
3113 int ret, orig_credits = handle->h_buffer_credits;
3114 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3115 struct ocfs2_extent_block *eb;
3116 struct ocfs2_extent_list *el;
3117
3118 el = path_leaf_el(path);
3119 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3120 return 0;
3121
3122 if (path->p_tree_depth == 0) {
3123rightmost_no_delete:
3124 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003125 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003126 * it up front.
3127 */
3128 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
Joel Becker13723d02008-10-17 19:25:01 -07003129 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
3145 * 2b) we need to bring the inode back to inline extents.
3146 */
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;
3161 ocfs2_error(inode->i_sb,
3162 "Inode %llu has empty extent block at %llu",
3163 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3164 (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
3177 ret = ocfs2_remove_rightmost_path(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003178 dealloc, et);
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:
3189 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003190 dealloc, &restart_path, et);
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
3200 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
3201 tmp_path, dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003202 &restart_path, et);
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
Tao Ma677b9752008-01-30 14:21:05 +08003253static int ocfs2_get_right_path(struct inode *inode,
3254 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
3270 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3271 &right_cpos);
3272 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 Beckerfacdb772009-02-12 18:08:48 -08003287 ret = ocfs2_find_path(INODE_CACHE(inode), 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 */
3307static int ocfs2_merge_rec_right(struct inode *inode,
3308 struct ocfs2_path *left_path,
3309 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003310 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003311 struct ocfs2_extent_rec *split_rec,
3312 int index)
3313{
3314 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003315 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3316 struct ocfs2_extent_rec *left_rec;
3317 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003318 struct ocfs2_extent_list *right_el;
3319 struct ocfs2_path *right_path = NULL;
3320 int subtree_index = 0;
3321 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3322 struct buffer_head *bh = path_leaf_bh(left_path);
3323 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003324
3325 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003326 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003327
Al Viro9d8df6a2008-05-21 06:32:11 +01003328 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003329 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3330 /* we meet with a cross extent block merge. */
3331 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3332 if (ret) {
3333 mlog_errno(ret);
3334 goto out;
3335 }
3336
3337 right_el = path_leaf_el(right_path);
3338 next_free = le16_to_cpu(right_el->l_next_free_rec);
3339 BUG_ON(next_free <= 0);
3340 right_rec = &right_el->l_recs[0];
3341 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003342 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003343 right_rec = &right_el->l_recs[1];
3344 }
3345
3346 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3347 le16_to_cpu(left_rec->e_leaf_clusters) !=
3348 le32_to_cpu(right_rec->e_cpos));
3349
Joel Becker7dc02802009-02-12 19:20:13 -08003350 subtree_index = ocfs2_find_subtree_root(et, left_path,
3351 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003352
3353 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3354 handle->h_buffer_credits,
3355 right_path);
3356 if (ret) {
3357 mlog_errno(ret);
3358 goto out;
3359 }
3360
3361 root_bh = left_path->p_node[subtree_index].bh;
3362 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3363
Joel Becker7dc02802009-02-12 19:20:13 -08003364 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003365 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003366 if (ret) {
3367 mlog_errno(ret);
3368 goto out;
3369 }
3370
3371 for (i = subtree_index + 1;
3372 i < path_num_items(right_path); i++) {
Joel Becker7dc02802009-02-12 19:20:13 -08003373 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003374 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003375 if (ret) {
3376 mlog_errno(ret);
3377 goto out;
3378 }
3379
Joel Becker7dc02802009-02-12 19:20:13 -08003380 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003381 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003382 if (ret) {
3383 mlog_errno(ret);
3384 goto out;
3385 }
3386 }
3387
3388 } else {
3389 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3390 right_rec = &el->l_recs[index + 1];
3391 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003392
Joel Becker7dc02802009-02-12 19:20:13 -08003393 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
Joel Becker13723d02008-10-17 19:25:01 -07003394 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003395 if (ret) {
3396 mlog_errno(ret);
3397 goto out;
3398 }
3399
3400 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3401
3402 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3403 le64_add_cpu(&right_rec->e_blkno,
Joel Becker7dc02802009-02-12 19:20:13 -08003404 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3405 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003406 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3407
3408 ocfs2_cleanup_merge(el, index);
3409
3410 ret = ocfs2_journal_dirty(handle, bh);
3411 if (ret)
3412 mlog_errno(ret);
3413
Tao Ma677b9752008-01-30 14:21:05 +08003414 if (right_path) {
3415 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3416 if (ret)
3417 mlog_errno(ret);
3418
Joel Becker4619c732009-02-12 19:02:36 -08003419 ocfs2_complete_edge_insert(handle, left_path, right_path,
3420 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003421 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003422out:
Tao Ma677b9752008-01-30 14:21:05 +08003423 if (right_path)
3424 ocfs2_free_path(right_path);
3425 return ret;
3426}
3427
3428static int ocfs2_get_left_path(struct inode *inode,
3429 struct ocfs2_path *right_path,
3430 struct ocfs2_path **ret_left_path)
3431{
3432 int ret;
3433 u32 left_cpos;
3434 struct ocfs2_path *left_path = NULL;
3435
3436 *ret_left_path = NULL;
3437
3438 /* This function shouldn't be called for non-trees. */
3439 BUG_ON(right_path->p_tree_depth == 0);
3440
3441 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3442 right_path, &left_cpos);
3443 if (ret) {
3444 mlog_errno(ret);
3445 goto out;
3446 }
3447
3448 /* This function shouldn't be called for the leftmost leaf. */
3449 BUG_ON(left_cpos == 0);
3450
Joel Beckerffdd7a52008-10-17 22:32:01 -07003451 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003452 if (!left_path) {
3453 ret = -ENOMEM;
3454 mlog_errno(ret);
3455 goto out;
3456 }
3457
Joel Beckerfacdb772009-02-12 18:08:48 -08003458 ret = ocfs2_find_path(INODE_CACHE(inode), left_path, left_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003459 if (ret) {
3460 mlog_errno(ret);
3461 goto out;
3462 }
3463
3464 *ret_left_path = left_path;
3465out:
3466 if (ret)
3467 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003468 return ret;
3469}
3470
3471/*
3472 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003473 * onto the tail of the record "before" it.
3474 * For index > 0, the "before" means the extent rec at index - 1.
3475 *
3476 * For index == 0, the "before" means the last record of the previous
3477 * extent block. And there is also a situation that we may need to
3478 * remove the rightmost leaf extent block in the right_path and change
3479 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003480 */
Tao Ma677b9752008-01-30 14:21:05 +08003481static int ocfs2_merge_rec_left(struct inode *inode,
3482 struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003483 handle_t *handle,
3484 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003485 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003486 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003487 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003488{
Tao Ma677b9752008-01-30 14:21:05 +08003489 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003490 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3491 struct ocfs2_extent_rec *left_rec;
3492 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003493 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3494 struct buffer_head *bh = path_leaf_bh(right_path);
3495 struct buffer_head *root_bh = NULL;
3496 struct ocfs2_path *left_path = NULL;
3497 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003498
Tao Ma677b9752008-01-30 14:21:05 +08003499 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003500
Mark Fasheh328d5752007-06-18 10:48:04 -07003501 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003502 if (index == 0) {
3503 /* we meet with a cross extent block merge. */
3504 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3505 if (ret) {
3506 mlog_errno(ret);
3507 goto out;
3508 }
3509
3510 left_el = path_leaf_el(left_path);
3511 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3512 le16_to_cpu(left_el->l_count));
3513
3514 left_rec = &left_el->l_recs[
3515 le16_to_cpu(left_el->l_next_free_rec) - 1];
3516 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3517 le16_to_cpu(left_rec->e_leaf_clusters) !=
3518 le32_to_cpu(split_rec->e_cpos));
3519
Joel Becker7dc02802009-02-12 19:20:13 -08003520 subtree_index = ocfs2_find_subtree_root(et, left_path,
3521 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003522
3523 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3524 handle->h_buffer_credits,
3525 left_path);
3526 if (ret) {
3527 mlog_errno(ret);
3528 goto out;
3529 }
3530
3531 root_bh = left_path->p_node[subtree_index].bh;
3532 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3533
Joel Becker0cf2f762009-02-12 16:41:25 -08003534 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003535 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003536 if (ret) {
3537 mlog_errno(ret);
3538 goto out;
3539 }
3540
3541 for (i = subtree_index + 1;
3542 i < path_num_items(right_path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -08003543 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07003544 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003545 if (ret) {
3546 mlog_errno(ret);
3547 goto out;
3548 }
3549
Joel Becker0cf2f762009-02-12 16:41:25 -08003550 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode),
Joel Becker13723d02008-10-17 19:25:01 -07003551 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003552 if (ret) {
3553 mlog_errno(ret);
3554 goto out;
3555 }
3556 }
3557 } else {
3558 left_rec = &el->l_recs[index - 1];
3559 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3560 has_empty_extent = 1;
3561 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003562
Joel Becker0cf2f762009-02-12 16:41:25 -08003563 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), right_path,
Tao Ma9047bea2009-01-05 14:45:24 +08003564 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003565 if (ret) {
3566 mlog_errno(ret);
3567 goto out;
3568 }
3569
3570 if (has_empty_extent && index == 1) {
3571 /*
3572 * The easy case - we can just plop the record right in.
3573 */
3574 *left_rec = *split_rec;
3575
3576 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003577 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003578 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003579
3580 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3581 le64_add_cpu(&right_rec->e_blkno,
3582 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3583 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3584
3585 ocfs2_cleanup_merge(el, index);
3586
3587 ret = ocfs2_journal_dirty(handle, bh);
3588 if (ret)
3589 mlog_errno(ret);
3590
Tao Ma677b9752008-01-30 14:21:05 +08003591 if (left_path) {
3592 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3593 if (ret)
3594 mlog_errno(ret);
3595
3596 /*
3597 * In the situation that the right_rec is empty and the extent
3598 * block is empty also, ocfs2_complete_edge_insert can't handle
3599 * it and we need to delete the right extent block.
3600 */
3601 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3602 le16_to_cpu(el->l_next_free_rec) == 1) {
3603
3604 ret = ocfs2_remove_rightmost_path(inode, handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003605 right_path,
3606 dealloc, et);
Tao Ma677b9752008-01-30 14:21:05 +08003607 if (ret) {
3608 mlog_errno(ret);
3609 goto out;
3610 }
3611
3612 /* Now the rightmost extent block has been deleted.
3613 * So we use the new rightmost path.
3614 */
3615 ocfs2_mv_path(right_path, left_path);
3616 left_path = NULL;
3617 } else
Joel Becker4619c732009-02-12 19:02:36 -08003618 ocfs2_complete_edge_insert(handle, left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003619 right_path, subtree_index);
3620 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003621out:
Tao Ma677b9752008-01-30 14:21:05 +08003622 if (left_path)
3623 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003624 return ret;
3625}
3626
3627static int ocfs2_try_to_merge_extent(struct inode *inode,
3628 handle_t *handle,
Tao Ma677b9752008-01-30 14:21:05 +08003629 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003630 int split_index,
3631 struct ocfs2_extent_rec *split_rec,
3632 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003633 struct ocfs2_merge_ctxt *ctxt,
3634 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003635
3636{
Tao Mao518d7262007-08-28 17:25:35 -07003637 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003638 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003639 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3640
3641 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3642
Tao Mao518d7262007-08-28 17:25:35 -07003643 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3644 /*
3645 * The merge code will need to create an empty
3646 * extent to take the place of the newly
3647 * emptied slot. Remove any pre-existing empty
3648 * extents - having more than one in a leaf is
3649 * illegal.
3650 */
Tao Ma677b9752008-01-30 14:21:05 +08003651 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003652 dealloc, et);
Tao Mao518d7262007-08-28 17:25:35 -07003653 if (ret) {
3654 mlog_errno(ret);
3655 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003656 }
Tao Mao518d7262007-08-28 17:25:35 -07003657 split_index--;
3658 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003659 }
3660
3661 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3662 /*
3663 * Left-right contig implies this.
3664 */
3665 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003666
3667 /*
3668 * Since the leftright insert always covers the entire
3669 * extent, this call will delete the insert record
3670 * entirely, resulting in an empty extent record added to
3671 * the extent block.
3672 *
3673 * Since the adding of an empty extent shifts
3674 * everything back to the right, there's no need to
3675 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003676 *
3677 * When the split_index is zero, we need to merge it to the
3678 * prevoius extent block. It is more efficient and easier
3679 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003680 */
Tao Ma677b9752008-01-30 14:21:05 +08003681 ret = ocfs2_merge_rec_right(inode, path,
Joel Becker7dc02802009-02-12 19:20:13 -08003682 handle, et, split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003683 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003684 if (ret) {
3685 mlog_errno(ret);
3686 goto out;
3687 }
3688
3689 /*
3690 * We can only get this from logic error above.
3691 */
3692 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3693
Tao Ma677b9752008-01-30 14:21:05 +08003694 /* The merge left us with an empty extent, remove it. */
Tao Mae7d4cb62008-08-18 17:38:44 +08003695 ret = ocfs2_rotate_tree_left(inode, handle, path,
3696 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003697 if (ret) {
3698 mlog_errno(ret);
3699 goto out;
3700 }
Tao Ma677b9752008-01-30 14:21:05 +08003701
Mark Fasheh328d5752007-06-18 10:48:04 -07003702 rec = &el->l_recs[split_index];
3703
3704 /*
3705 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003706 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003707 */
Tao Ma677b9752008-01-30 14:21:05 +08003708 ret = ocfs2_merge_rec_left(inode, path,
3709 handle, rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003710 dealloc, et,
Tao Ma677b9752008-01-30 14:21:05 +08003711 split_index);
3712
Mark Fasheh328d5752007-06-18 10:48:04 -07003713 if (ret) {
3714 mlog_errno(ret);
3715 goto out;
3716 }
3717
Tao Ma677b9752008-01-30 14:21:05 +08003718 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003719 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003720 /*
3721 * Error from this last rotate is not critical, so
3722 * print but don't bubble it up.
3723 */
3724 if (ret)
3725 mlog_errno(ret);
3726 ret = 0;
3727 } else {
3728 /*
3729 * Merge a record to the left or right.
3730 *
3731 * 'contig_type' is relative to the existing record,
3732 * so for example, if we're "right contig", it's to
3733 * the record on the left (hence the left merge).
3734 */
3735 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3736 ret = ocfs2_merge_rec_left(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003737 path,
3738 handle, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003739 dealloc, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003740 split_index);
3741 if (ret) {
3742 mlog_errno(ret);
3743 goto out;
3744 }
3745 } else {
Joel Becker7dc02802009-02-12 19:20:13 -08003746 ret = ocfs2_merge_rec_right(inode, path, handle,
3747 et, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003748 split_index);
3749 if (ret) {
3750 mlog_errno(ret);
3751 goto out;
3752 }
3753 }
3754
3755 if (ctxt->c_split_covers_rec) {
3756 /*
3757 * The merge may have left an empty extent in
3758 * our leaf. Try to rotate it away.
3759 */
Tao Ma677b9752008-01-30 14:21:05 +08003760 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003761 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003762 if (ret)
3763 mlog_errno(ret);
3764 ret = 0;
3765 }
3766 }
3767
3768out:
3769 return ret;
3770}
3771
3772static void ocfs2_subtract_from_rec(struct super_block *sb,
3773 enum ocfs2_split_type split,
3774 struct ocfs2_extent_rec *rec,
3775 struct ocfs2_extent_rec *split_rec)
3776{
3777 u64 len_blocks;
3778
3779 len_blocks = ocfs2_clusters_to_blocks(sb,
3780 le16_to_cpu(split_rec->e_leaf_clusters));
3781
3782 if (split == SPLIT_LEFT) {
3783 /*
3784 * Region is on the left edge of the existing
3785 * record.
3786 */
3787 le32_add_cpu(&rec->e_cpos,
3788 le16_to_cpu(split_rec->e_leaf_clusters));
3789 le64_add_cpu(&rec->e_blkno, len_blocks);
3790 le16_add_cpu(&rec->e_leaf_clusters,
3791 -le16_to_cpu(split_rec->e_leaf_clusters));
3792 } else {
3793 /*
3794 * Region is on the right edge of the existing
3795 * record.
3796 */
3797 le16_add_cpu(&rec->e_leaf_clusters,
3798 -le16_to_cpu(split_rec->e_leaf_clusters));
3799 }
3800}
3801
Mark Fashehdcd05382007-01-16 11:32:23 -08003802/*
3803 * Do the final bits of extent record insertion at the target leaf
3804 * list. If this leaf is part of an allocation tree, it is assumed
3805 * that the tree above has been prepared.
3806 */
3807static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3808 struct ocfs2_extent_list *el,
3809 struct ocfs2_insert_type *insert,
3810 struct inode *inode)
3811{
3812 int i = insert->ins_contig_index;
3813 unsigned int range;
3814 struct ocfs2_extent_rec *rec;
3815
Mark Fashehe48edee2007-03-07 16:46:57 -08003816 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003817
Mark Fasheh328d5752007-06-18 10:48:04 -07003818 if (insert->ins_split != SPLIT_NONE) {
3819 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3820 BUG_ON(i == -1);
3821 rec = &el->l_recs[i];
3822 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3823 insert_rec);
3824 goto rotate;
3825 }
3826
Mark Fashehdcd05382007-01-16 11:32:23 -08003827 /*
3828 * Contiguous insert - either left or right.
3829 */
3830 if (insert->ins_contig != CONTIG_NONE) {
3831 rec = &el->l_recs[i];
3832 if (insert->ins_contig == CONTIG_LEFT) {
3833 rec->e_blkno = insert_rec->e_blkno;
3834 rec->e_cpos = insert_rec->e_cpos;
3835 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003836 le16_add_cpu(&rec->e_leaf_clusters,
3837 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003838 return;
3839 }
3840
3841 /*
3842 * Handle insert into an empty leaf.
3843 */
3844 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3845 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3846 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3847 el->l_recs[0] = *insert_rec;
3848 el->l_next_free_rec = cpu_to_le16(1);
3849 return;
3850 }
3851
3852 /*
3853 * Appending insert.
3854 */
3855 if (insert->ins_appending == APPEND_TAIL) {
3856 i = le16_to_cpu(el->l_next_free_rec) - 1;
3857 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003858 range = le32_to_cpu(rec->e_cpos)
3859 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003860 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3861
3862 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3863 le16_to_cpu(el->l_count),
3864 "inode %lu, depth %u, count %u, next free %u, "
3865 "rec.cpos %u, rec.clusters %u, "
3866 "insert.cpos %u, insert.clusters %u\n",
3867 inode->i_ino,
3868 le16_to_cpu(el->l_tree_depth),
3869 le16_to_cpu(el->l_count),
3870 le16_to_cpu(el->l_next_free_rec),
3871 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003872 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003873 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003874 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003875 i++;
3876 el->l_recs[i] = *insert_rec;
3877 le16_add_cpu(&el->l_next_free_rec, 1);
3878 return;
3879 }
3880
Mark Fasheh328d5752007-06-18 10:48:04 -07003881rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003882 /*
3883 * Ok, we have to rotate.
3884 *
3885 * At this point, it is safe to assume that inserting into an
3886 * empty leaf and appending to a leaf have both been handled
3887 * above.
3888 *
3889 * This leaf needs to have space, either by the empty 1st
3890 * extent record, or by virtue of an l_next_rec < l_count.
3891 */
3892 ocfs2_rotate_leaf(el, insert_rec);
3893}
3894
Mark Fasheh328d5752007-06-18 10:48:04 -07003895static void ocfs2_adjust_rightmost_records(struct inode *inode,
3896 handle_t *handle,
3897 struct ocfs2_path *path,
3898 struct ocfs2_extent_rec *insert_rec)
3899{
3900 int ret, i, next_free;
3901 struct buffer_head *bh;
3902 struct ocfs2_extent_list *el;
3903 struct ocfs2_extent_rec *rec;
3904
3905 /*
3906 * Update everything except the leaf block.
3907 */
3908 for (i = 0; i < path->p_tree_depth; i++) {
3909 bh = path->p_node[i].bh;
3910 el = path->p_node[i].el;
3911
3912 next_free = le16_to_cpu(el->l_next_free_rec);
3913 if (next_free == 0) {
3914 ocfs2_error(inode->i_sb,
3915 "Dinode %llu has a bad extent list",
3916 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3917 ret = -EIO;
3918 return;
3919 }
3920
3921 rec = &el->l_recs[next_free - 1];
3922
3923 rec->e_int_clusters = insert_rec->e_cpos;
3924 le32_add_cpu(&rec->e_int_clusters,
3925 le16_to_cpu(insert_rec->e_leaf_clusters));
3926 le32_add_cpu(&rec->e_int_clusters,
3927 -le32_to_cpu(rec->e_cpos));
3928
3929 ret = ocfs2_journal_dirty(handle, bh);
3930 if (ret)
3931 mlog_errno(ret);
3932
3933 }
3934}
3935
Mark Fashehdcd05382007-01-16 11:32:23 -08003936static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3937 struct ocfs2_extent_rec *insert_rec,
3938 struct ocfs2_path *right_path,
3939 struct ocfs2_path **ret_left_path)
3940{
Mark Fasheh328d5752007-06-18 10:48:04 -07003941 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003942 struct ocfs2_extent_list *el;
3943 struct ocfs2_path *left_path = NULL;
3944
3945 *ret_left_path = NULL;
3946
3947 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003948 * This shouldn't happen for non-trees. The extent rec cluster
3949 * count manipulation below only works for interior nodes.
3950 */
3951 BUG_ON(right_path->p_tree_depth == 0);
3952
3953 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003954 * If our appending insert is at the leftmost edge of a leaf,
3955 * then we might need to update the rightmost records of the
3956 * neighboring path.
3957 */
3958 el = path_leaf_el(right_path);
3959 next_free = le16_to_cpu(el->l_next_free_rec);
3960 if (next_free == 0 ||
3961 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3962 u32 left_cpos;
3963
3964 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3965 &left_cpos);
3966 if (ret) {
3967 mlog_errno(ret);
3968 goto out;
3969 }
3970
3971 mlog(0, "Append may need a left path update. cpos: %u, "
3972 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3973 left_cpos);
3974
3975 /*
3976 * No need to worry if the append is already in the
3977 * leftmost leaf.
3978 */
3979 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07003980 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003981 if (!left_path) {
3982 ret = -ENOMEM;
3983 mlog_errno(ret);
3984 goto out;
3985 }
3986
Joel Beckerfacdb772009-02-12 18:08:48 -08003987 ret = ocfs2_find_path(INODE_CACHE(inode), left_path,
3988 left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08003989 if (ret) {
3990 mlog_errno(ret);
3991 goto out;
3992 }
3993
3994 /*
3995 * ocfs2_insert_path() will pass the left_path to the
3996 * journal for us.
3997 */
3998 }
3999 }
4000
Joel Becker0cf2f762009-02-12 16:41:25 -08004001 ret = ocfs2_journal_access_path(INODE_CACHE(inode), handle, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004002 if (ret) {
4003 mlog_errno(ret);
4004 goto out;
4005 }
4006
Mark Fasheh328d5752007-06-18 10:48:04 -07004007 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004008
4009 *ret_left_path = left_path;
4010 ret = 0;
4011out:
4012 if (ret != 0)
4013 ocfs2_free_path(left_path);
4014
4015 return ret;
4016}
4017
Mark Fasheh328d5752007-06-18 10:48:04 -07004018static void ocfs2_split_record(struct inode *inode,
4019 struct ocfs2_path *left_path,
4020 struct ocfs2_path *right_path,
4021 struct ocfs2_extent_rec *split_rec,
4022 enum ocfs2_split_type split)
4023{
4024 int index;
4025 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4026 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4027 struct ocfs2_extent_rec *rec, *tmprec;
4028
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004029 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004030 if (left_path)
4031 left_el = path_leaf_el(left_path);
4032
4033 el = right_el;
4034 insert_el = right_el;
4035 index = ocfs2_search_extent_list(el, cpos);
4036 if (index != -1) {
4037 if (index == 0 && left_path) {
4038 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4039
4040 /*
4041 * This typically means that the record
4042 * started in the left path but moved to the
4043 * right as a result of rotation. We either
4044 * move the existing record to the left, or we
4045 * do the later insert there.
4046 *
4047 * In this case, the left path should always
4048 * exist as the rotate code will have passed
4049 * it back for a post-insert update.
4050 */
4051
4052 if (split == SPLIT_LEFT) {
4053 /*
4054 * It's a left split. Since we know
4055 * that the rotate code gave us an
4056 * empty extent in the left path, we
4057 * can just do the insert there.
4058 */
4059 insert_el = left_el;
4060 } else {
4061 /*
4062 * Right split - we have to move the
4063 * existing record over to the left
4064 * leaf. The insert will be into the
4065 * newly created empty extent in the
4066 * right leaf.
4067 */
4068 tmprec = &right_el->l_recs[index];
4069 ocfs2_rotate_leaf(left_el, tmprec);
4070 el = left_el;
4071
4072 memset(tmprec, 0, sizeof(*tmprec));
4073 index = ocfs2_search_extent_list(left_el, cpos);
4074 BUG_ON(index == -1);
4075 }
4076 }
4077 } else {
4078 BUG_ON(!left_path);
4079 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4080 /*
4081 * Left path is easy - we can just allow the insert to
4082 * happen.
4083 */
4084 el = left_el;
4085 insert_el = left_el;
4086 index = ocfs2_search_extent_list(el, cpos);
4087 BUG_ON(index == -1);
4088 }
4089
4090 rec = &el->l_recs[index];
4091 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
4092 ocfs2_rotate_leaf(insert_el, split_rec);
4093}
4094
Mark Fashehdcd05382007-01-16 11:32:23 -08004095/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004096 * This function only does inserts on an allocation b-tree. For tree
4097 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004098 *
4099 * right_path is the path we want to do the actual insert
4100 * in. left_path should only be passed in if we need to update that
4101 * portion of the tree after an edge insert.
4102 */
4103static int ocfs2_insert_path(struct inode *inode,
4104 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08004105 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004106 struct ocfs2_path *left_path,
4107 struct ocfs2_path *right_path,
4108 struct ocfs2_extent_rec *insert_rec,
4109 struct ocfs2_insert_type *insert)
4110{
4111 int ret, subtree_index;
4112 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004113
Mark Fashehdcd05382007-01-16 11:32:23 -08004114 if (left_path) {
4115 int credits = handle->h_buffer_credits;
4116
4117 /*
4118 * There's a chance that left_path got passed back to
4119 * us without being accounted for in the
4120 * journal. Extend our transaction here to be sure we
4121 * can change those blocks.
4122 */
4123 credits += left_path->p_tree_depth;
4124
4125 ret = ocfs2_extend_trans(handle, credits);
4126 if (ret < 0) {
4127 mlog_errno(ret);
4128 goto out;
4129 }
4130
Joel Becker7dc02802009-02-12 19:20:13 -08004131 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004132 if (ret < 0) {
4133 mlog_errno(ret);
4134 goto out;
4135 }
4136 }
4137
Mark Fashehe8aed342007-12-03 16:43:01 -08004138 /*
4139 * Pass both paths to the journal. The majority of inserts
4140 * will be touching all components anyway.
4141 */
Joel Becker7dc02802009-02-12 19:20:13 -08004142 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004143 if (ret < 0) {
4144 mlog_errno(ret);
4145 goto out;
4146 }
4147
Mark Fasheh328d5752007-06-18 10:48:04 -07004148 if (insert->ins_split != SPLIT_NONE) {
4149 /*
4150 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004151 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004152 * function sort it all out.
4153 */
4154 ocfs2_split_record(inode, left_path, right_path,
4155 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004156
4157 /*
4158 * Split might have modified either leaf and we don't
4159 * have a guarantee that the later edge insert will
4160 * dirty this for us.
4161 */
4162 if (left_path)
4163 ret = ocfs2_journal_dirty(handle,
4164 path_leaf_bh(left_path));
4165 if (ret)
4166 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07004167 } else
4168 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
4169 insert, inode);
Mark Fashehdcd05382007-01-16 11:32:23 -08004170
Mark Fashehdcd05382007-01-16 11:32:23 -08004171 ret = ocfs2_journal_dirty(handle, leaf_bh);
4172 if (ret)
4173 mlog_errno(ret);
4174
4175 if (left_path) {
4176 /*
4177 * The rotate code has indicated that we need to fix
4178 * up portions of the tree after the insert.
4179 *
4180 * XXX: Should we extend the transaction here?
4181 */
Joel Becker7dc02802009-02-12 19:20:13 -08004182 subtree_index = ocfs2_find_subtree_root(et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004183 right_path);
Joel Becker4619c732009-02-12 19:02:36 -08004184 ocfs2_complete_edge_insert(handle, left_path, right_path,
4185 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08004186 }
4187
4188 ret = 0;
4189out:
4190 return ret;
4191}
4192
4193static int ocfs2_do_insert_extent(struct inode *inode,
4194 handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004195 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004196 struct ocfs2_extent_rec *insert_rec,
4197 struct ocfs2_insert_type *type)
4198{
4199 int ret, rotate = 0;
4200 u32 cpos;
4201 struct ocfs2_path *right_path = NULL;
4202 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004203 struct ocfs2_extent_list *el;
4204
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004205 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004206
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004207 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004208 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004209 if (ret) {
4210 mlog_errno(ret);
4211 goto out;
4212 }
4213
4214 if (le16_to_cpu(el->l_tree_depth) == 0) {
4215 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4216 goto out_update_clusters;
4217 }
4218
Joel Beckerffdd7a52008-10-17 22:32:01 -07004219 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004220 if (!right_path) {
4221 ret = -ENOMEM;
4222 mlog_errno(ret);
4223 goto out;
4224 }
4225
4226 /*
4227 * Determine the path to start with. Rotations need the
4228 * rightmost path, everything else can go directly to the
4229 * target leaf.
4230 */
4231 cpos = le32_to_cpu(insert_rec->e_cpos);
4232 if (type->ins_appending == APPEND_NONE &&
4233 type->ins_contig == CONTIG_NONE) {
4234 rotate = 1;
4235 cpos = UINT_MAX;
4236 }
4237
Joel Beckerfacdb772009-02-12 18:08:48 -08004238 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004239 if (ret) {
4240 mlog_errno(ret);
4241 goto out;
4242 }
4243
4244 /*
4245 * Rotations and appends need special treatment - they modify
4246 * parts of the tree's above them.
4247 *
4248 * Both might pass back a path immediate to the left of the
4249 * one being inserted to. This will be cause
4250 * ocfs2_insert_path() to modify the rightmost records of
4251 * left_path to account for an edge insert.
4252 *
4253 * XXX: When modifying this code, keep in mind that an insert
4254 * can wind up skipping both of these two special cases...
4255 */
4256 if (rotate) {
Joel Becker5c601ab2009-02-12 19:10:13 -08004257 ret = ocfs2_rotate_tree_right(inode, handle, et, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004258 le32_to_cpu(insert_rec->e_cpos),
4259 right_path, &left_path);
4260 if (ret) {
4261 mlog_errno(ret);
4262 goto out;
4263 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004264
4265 /*
4266 * ocfs2_rotate_tree_right() might have extended the
4267 * transaction without re-journaling our tree root.
4268 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004269 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004270 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004271 if (ret) {
4272 mlog_errno(ret);
4273 goto out;
4274 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004275 } else if (type->ins_appending == APPEND_TAIL
4276 && type->ins_contig != CONTIG_LEFT) {
4277 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4278 right_path, &left_path);
4279 if (ret) {
4280 mlog_errno(ret);
4281 goto out;
4282 }
4283 }
4284
Joel Becker7dc02802009-02-12 19:20:13 -08004285 ret = ocfs2_insert_path(inode, handle, et, left_path, right_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004286 insert_rec, type);
4287 if (ret) {
4288 mlog_errno(ret);
4289 goto out;
4290 }
4291
4292out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004293 if (type->ins_split == SPLIT_NONE)
Joel Becker6136ca52009-02-12 19:32:43 -08004294 ocfs2_et_update_clusters(et,
Joel Becker35dc0aa2008-08-20 16:25:06 -07004295 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004296
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004297 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004298 if (ret)
4299 mlog_errno(ret);
4300
4301out:
4302 ocfs2_free_path(left_path);
4303 ocfs2_free_path(right_path);
4304
4305 return ret;
4306}
4307
Mark Fasheh328d5752007-06-18 10:48:04 -07004308static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08004309ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004310 struct ocfs2_extent_list *el, int index,
4311 struct ocfs2_extent_rec *split_rec)
4312{
Tao Maad5a4d72008-01-30 14:21:32 +08004313 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004314 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004315 u32 left_cpos, right_cpos;
4316 struct ocfs2_extent_rec *rec = NULL;
4317 struct ocfs2_extent_list *new_el;
4318 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4319 struct buffer_head *bh;
4320 struct ocfs2_extent_block *eb;
4321
4322 if (index > 0) {
4323 rec = &el->l_recs[index - 1];
4324 } else if (path->p_tree_depth > 0) {
4325 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4326 path, &left_cpos);
4327 if (status)
4328 goto out;
4329
4330 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004331 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004332 if (!left_path)
4333 goto out;
4334
Joel Beckerfacdb772009-02-12 18:08:48 -08004335 status = ocfs2_find_path(INODE_CACHE(inode),
4336 left_path, left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004337 if (status)
4338 goto out;
4339
4340 new_el = path_leaf_el(left_path);
4341
4342 if (le16_to_cpu(new_el->l_next_free_rec) !=
4343 le16_to_cpu(new_el->l_count)) {
4344 bh = path_leaf_bh(left_path);
4345 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004346 ocfs2_error(inode->i_sb,
4347 "Extent block #%llu has an "
4348 "invalid l_next_free_rec of "
4349 "%d. It should have "
4350 "matched the l_count of %d",
4351 (unsigned long long)le64_to_cpu(eb->h_blkno),
4352 le16_to_cpu(new_el->l_next_free_rec),
4353 le16_to_cpu(new_el->l_count));
4354 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004355 goto out;
4356 }
4357 rec = &new_el->l_recs[
4358 le16_to_cpu(new_el->l_next_free_rec) - 1];
4359 }
4360 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004361
4362 /*
4363 * We're careful to check for an empty extent record here -
4364 * the merge code will know what to do if it sees one.
4365 */
Tao Maad5a4d72008-01-30 14:21:32 +08004366 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004367 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4368 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4369 ret = CONTIG_RIGHT;
4370 } else {
4371 ret = ocfs2_extent_contig(inode, rec, split_rec);
4372 }
4373 }
4374
Tao Maad5a4d72008-01-30 14:21:32 +08004375 rec = NULL;
4376 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4377 rec = &el->l_recs[index + 1];
4378 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4379 path->p_tree_depth > 0) {
4380 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4381 path, &right_cpos);
4382 if (status)
4383 goto out;
4384
4385 if (right_cpos == 0)
4386 goto out;
4387
Joel Beckerffdd7a52008-10-17 22:32:01 -07004388 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004389 if (!right_path)
4390 goto out;
4391
Joel Beckerfacdb772009-02-12 18:08:48 -08004392 status = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004393 if (status)
4394 goto out;
4395
4396 new_el = path_leaf_el(right_path);
4397 rec = &new_el->l_recs[0];
4398 if (ocfs2_is_empty_extent(rec)) {
4399 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4400 bh = path_leaf_bh(right_path);
4401 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004402 ocfs2_error(inode->i_sb,
4403 "Extent block #%llu has an "
4404 "invalid l_next_free_rec of %d",
4405 (unsigned long long)le64_to_cpu(eb->h_blkno),
4406 le16_to_cpu(new_el->l_next_free_rec));
4407 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004408 goto out;
4409 }
4410 rec = &new_el->l_recs[1];
4411 }
4412 }
4413
4414 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004415 enum ocfs2_contig_type contig_type;
4416
Mark Fasheh328d5752007-06-18 10:48:04 -07004417 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4418
4419 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4420 ret = CONTIG_LEFTRIGHT;
4421 else if (ret == CONTIG_NONE)
4422 ret = contig_type;
4423 }
4424
Tao Maad5a4d72008-01-30 14:21:32 +08004425out:
4426 if (left_path)
4427 ocfs2_free_path(left_path);
4428 if (right_path)
4429 ocfs2_free_path(right_path);
4430
Mark Fasheh328d5752007-06-18 10:48:04 -07004431 return ret;
4432}
4433
Mark Fashehdcd05382007-01-16 11:32:23 -08004434static void ocfs2_figure_contig_type(struct inode *inode,
4435 struct ocfs2_insert_type *insert,
4436 struct ocfs2_extent_list *el,
Tao Maca12b7c2008-08-18 17:38:52 +08004437 struct ocfs2_extent_rec *insert_rec,
4438 struct ocfs2_extent_tree *et)
Mark Fashehdcd05382007-01-16 11:32:23 -08004439{
4440 int i;
4441 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4442
Mark Fashehe48edee2007-03-07 16:46:57 -08004443 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4444
Mark Fashehdcd05382007-01-16 11:32:23 -08004445 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4446 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4447 insert_rec);
4448 if (contig_type != CONTIG_NONE) {
4449 insert->ins_contig_index = i;
4450 break;
4451 }
4452 }
4453 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004454
4455 if (insert->ins_contig != CONTIG_NONE) {
4456 struct ocfs2_extent_rec *rec =
4457 &el->l_recs[insert->ins_contig_index];
4458 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4459 le16_to_cpu(insert_rec->e_leaf_clusters);
4460
4461 /*
4462 * Caller might want us to limit the size of extents, don't
4463 * calculate contiguousness if we might exceed that limit.
4464 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004465 if (et->et_max_leaf_clusters &&
4466 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004467 insert->ins_contig = CONTIG_NONE;
4468 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004469}
4470
4471/*
4472 * This should only be called against the righmost leaf extent list.
4473 *
4474 * ocfs2_figure_appending_type() will figure out whether we'll have to
4475 * insert at the tail of the rightmost leaf.
4476 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004477 * This should also work against the root extent list for tree's with 0
4478 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004479 * then the logic here makes sense.
4480 */
4481static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4482 struct ocfs2_extent_list *el,
4483 struct ocfs2_extent_rec *insert_rec)
4484{
4485 int i;
4486 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4487 struct ocfs2_extent_rec *rec;
4488
4489 insert->ins_appending = APPEND_NONE;
4490
Mark Fashehe48edee2007-03-07 16:46:57 -08004491 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004492
4493 if (!el->l_next_free_rec)
4494 goto set_tail_append;
4495
4496 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4497 /* Were all records empty? */
4498 if (le16_to_cpu(el->l_next_free_rec) == 1)
4499 goto set_tail_append;
4500 }
4501
4502 i = le16_to_cpu(el->l_next_free_rec) - 1;
4503 rec = &el->l_recs[i];
4504
Mark Fashehe48edee2007-03-07 16:46:57 -08004505 if (cpos >=
4506 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004507 goto set_tail_append;
4508
4509 return;
4510
4511set_tail_append:
4512 insert->ins_appending = APPEND_TAIL;
4513}
4514
4515/*
4516 * Helper function called at the begining of an insert.
4517 *
4518 * This computes a few things that are commonly used in the process of
4519 * inserting into the btree:
4520 * - Whether the new extent is contiguous with an existing one.
4521 * - The current tree depth.
4522 * - Whether the insert is an appending one.
4523 * - The total # of free records in the tree.
4524 *
4525 * All of the information is stored on the ocfs2_insert_type
4526 * structure.
4527 */
4528static int ocfs2_figure_insert_type(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004529 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004530 struct buffer_head **last_eb_bh,
4531 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004532 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004533 struct ocfs2_insert_type *insert)
4534{
4535 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004536 struct ocfs2_extent_block *eb;
4537 struct ocfs2_extent_list *el;
4538 struct ocfs2_path *path = NULL;
4539 struct buffer_head *bh = NULL;
4540
Mark Fasheh328d5752007-06-18 10:48:04 -07004541 insert->ins_split = SPLIT_NONE;
4542
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004543 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004544 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4545
4546 if (el->l_tree_depth) {
4547 /*
4548 * If we have tree depth, we read in the
4549 * rightmost extent block ahead of time as
4550 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4551 * may want it later.
4552 */
Joel Becker3d03a302009-02-12 17:49:26 -08004553 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08004554 ocfs2_et_get_last_eb_blk(et),
4555 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004556 if (ret) {
4557 mlog_exit(ret);
4558 goto out;
4559 }
4560 eb = (struct ocfs2_extent_block *) bh->b_data;
4561 el = &eb->h_list;
4562 }
4563
4564 /*
4565 * Unless we have a contiguous insert, we'll need to know if
4566 * there is room left in our allocation tree for another
4567 * extent record.
4568 *
4569 * XXX: This test is simplistic, we can search for empty
4570 * extent records too.
4571 */
Tao Maoc77534f2007-08-28 17:22:33 -07004572 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004573 le16_to_cpu(el->l_next_free_rec);
4574
4575 if (!insert->ins_tree_depth) {
Tao Maca12b7c2008-08-18 17:38:52 +08004576 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004577 ocfs2_figure_appending_type(insert, el, insert_rec);
4578 return 0;
4579 }
4580
Joel Beckerffdd7a52008-10-17 22:32:01 -07004581 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004582 if (!path) {
4583 ret = -ENOMEM;
4584 mlog_errno(ret);
4585 goto out;
4586 }
4587
4588 /*
4589 * In the case that we're inserting past what the tree
4590 * currently accounts for, ocfs2_find_path() will return for
4591 * us the rightmost tree path. This is accounted for below in
4592 * the appending code.
4593 */
Joel Beckerfacdb772009-02-12 18:08:48 -08004594 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -08004595 if (ret) {
4596 mlog_errno(ret);
4597 goto out;
4598 }
4599
4600 el = path_leaf_el(path);
4601
4602 /*
4603 * Now that we have the path, there's two things we want to determine:
4604 * 1) Contiguousness (also set contig_index if this is so)
4605 *
4606 * 2) Are we doing an append? We can trivially break this up
4607 * into two types of appends: simple record append, or a
4608 * rotate inside the tail leaf.
4609 */
Tao Maca12b7c2008-08-18 17:38:52 +08004610 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004611
4612 /*
4613 * The insert code isn't quite ready to deal with all cases of
4614 * left contiguousness. Specifically, if it's an insert into
4615 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004616 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004617 * left. For now, just catch that case and fool the layers
4618 * above us. This works just fine for tree_depth == 0, which
4619 * is why we allow that above.
4620 */
4621 if (insert->ins_contig == CONTIG_LEFT &&
4622 insert->ins_contig_index == 0)
4623 insert->ins_contig = CONTIG_NONE;
4624
4625 /*
4626 * Ok, so we can simply compare against last_eb to figure out
4627 * whether the path doesn't exist. This will only happen in
4628 * the case that we're doing a tail append, so maybe we can
4629 * take advantage of that information somehow.
4630 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004631 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004632 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004633 /*
4634 * Ok, ocfs2_find_path() returned us the rightmost
4635 * tree path. This might be an appending insert. There are
4636 * two cases:
4637 * 1) We're doing a true append at the tail:
4638 * -This might even be off the end of the leaf
4639 * 2) We're "appending" by rotating in the tail
4640 */
4641 ocfs2_figure_appending_type(insert, el, insert_rec);
4642 }
4643
4644out:
4645 ocfs2_free_path(path);
4646
4647 if (ret == 0)
4648 *last_eb_bh = bh;
4649 else
4650 brelse(bh);
4651 return ret;
4652}
4653
4654/*
4655 * Insert an extent into an inode btree.
4656 *
4657 * The caller needs to update fe->i_clusters
4658 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004659int ocfs2_insert_extent(struct ocfs2_super *osb,
4660 handle_t *handle,
4661 struct inode *inode,
4662 struct ocfs2_extent_tree *et,
4663 u32 cpos,
4664 u64 start_blk,
4665 u32 new_clusters,
4666 u8 flags,
4667 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004668{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004669 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004670 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004671 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004672 struct ocfs2_insert_type insert = {0, };
4673 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004674
Mark Fashehdcd05382007-01-16 11:32:23 -08004675 mlog(0, "add %u clusters at position %u to inode %llu\n",
4676 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004677
Mark Fashehe48edee2007-03-07 16:46:57 -08004678 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004679 rec.e_cpos = cpu_to_le32(cpos);
4680 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004681 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004682 rec.e_flags = flags;
Joel Becker6136ca52009-02-12 19:32:43 -08004683 status = ocfs2_et_insert_check(et, &rec);
Joel Becker1e61ee72008-08-20 18:32:45 -07004684 if (status) {
4685 mlog_errno(status);
4686 goto bail;
4687 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004688
Tao Mae7d4cb62008-08-18 17:38:44 +08004689 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004690 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004691 if (status < 0) {
4692 mlog_errno(status);
4693 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004694 }
4695
Mark Fashehdcd05382007-01-16 11:32:23 -08004696 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4697 "Insert.contig_index: %d, Insert.free_records: %d, "
4698 "Insert.tree_depth: %d\n",
4699 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004700 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004701
Tao Maoc77534f2007-08-28 17:22:33 -07004702 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004703 status = ocfs2_grow_tree(inode, handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004704 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004705 meta_ac);
4706 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004707 mlog_errno(status);
4708 goto bail;
4709 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004710 }
4711
Mark Fashehdcd05382007-01-16 11:32:23 -08004712 /* Finally, we can add clusters. This might rotate the tree for us. */
Tao Mae7d4cb62008-08-18 17:38:44 +08004713 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004714 if (status < 0)
4715 mlog_errno(status);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004716 else if (et->et_ops == &ocfs2_dinode_et_ops)
Mark Fasheh83418972007-04-23 18:53:12 -07004717 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004718
4719bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004720 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004721
Tao Maf56654c2008-08-18 17:38:48 +08004722 mlog_exit(status);
4723 return status;
4724}
4725
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004726/*
4727 * Allcate and add clusters into the extent b-tree.
4728 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004729 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004730 * it is not limited to the file storage. Any extent tree can use this
4731 * function if it implements the proper ocfs2_extent_tree.
4732 */
4733int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4734 struct inode *inode,
4735 u32 *logical_offset,
4736 u32 clusters_to_add,
4737 int mark_unwritten,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004738 struct ocfs2_extent_tree *et,
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004739 handle_t *handle,
4740 struct ocfs2_alloc_context *data_ac,
4741 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004742 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004743{
4744 int status = 0;
4745 int free_extents;
4746 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4747 u32 bit_off, num_bits;
4748 u64 block;
4749 u8 flags = 0;
4750
4751 BUG_ON(!clusters_to_add);
4752
4753 if (mark_unwritten)
4754 flags = OCFS2_EXT_UNWRITTEN;
4755
Joel Becker3d03a302009-02-12 17:49:26 -08004756 free_extents = ocfs2_num_free_extents(osb, et);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004757 if (free_extents < 0) {
4758 status = free_extents;
4759 mlog_errno(status);
4760 goto leave;
4761 }
4762
4763 /* there are two cases which could cause us to EAGAIN in the
4764 * we-need-more-metadata case:
4765 * 1) we haven't reserved *any*
4766 * 2) we are so fragmented, we've needed to add metadata too
4767 * many times. */
4768 if (!free_extents && !meta_ac) {
4769 mlog(0, "we haven't reserved any metadata!\n");
4770 status = -EAGAIN;
4771 reason = RESTART_META;
4772 goto leave;
4773 } else if ((!free_extents)
4774 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004775 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004776 mlog(0, "filesystem is really fragmented...\n");
4777 status = -EAGAIN;
4778 reason = RESTART_META;
4779 goto leave;
4780 }
4781
4782 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4783 clusters_to_add, &bit_off, &num_bits);
4784 if (status < 0) {
4785 if (status != -ENOSPC)
4786 mlog_errno(status);
4787 goto leave;
4788 }
4789
4790 BUG_ON(num_bits > clusters_to_add);
4791
Joel Becker13723d02008-10-17 19:25:01 -07004792 /* reserve our write early -- insert_extent may update the tree root */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004793 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004794 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004795 if (status < 0) {
4796 mlog_errno(status);
4797 goto leave;
4798 }
4799
4800 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4801 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4802 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004803 status = ocfs2_insert_extent(osb, handle, inode, et,
4804 *logical_offset, block,
4805 num_bits, flags, meta_ac);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004806 if (status < 0) {
4807 mlog_errno(status);
4808 goto leave;
4809 }
4810
Joel Beckerf99b9b72008-08-20 19:36:33 -07004811 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004812 if (status < 0) {
4813 mlog_errno(status);
4814 goto leave;
4815 }
4816
4817 clusters_to_add -= num_bits;
4818 *logical_offset += num_bits;
4819
4820 if (clusters_to_add) {
4821 mlog(0, "need to alloc once more, wanted = %u\n",
4822 clusters_to_add);
4823 status = -EAGAIN;
4824 reason = RESTART_TRANS;
4825 }
4826
4827leave:
4828 mlog_exit(status);
4829 if (reason_ret)
4830 *reason_ret = reason;
4831 return status;
4832}
4833
Mark Fasheh328d5752007-06-18 10:48:04 -07004834static void ocfs2_make_right_split_rec(struct super_block *sb,
4835 struct ocfs2_extent_rec *split_rec,
4836 u32 cpos,
4837 struct ocfs2_extent_rec *rec)
4838{
4839 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4840 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4841
4842 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4843
4844 split_rec->e_cpos = cpu_to_le32(cpos);
4845 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4846
4847 split_rec->e_blkno = rec->e_blkno;
4848 le64_add_cpu(&split_rec->e_blkno,
4849 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4850
4851 split_rec->e_flags = rec->e_flags;
4852}
4853
4854static int ocfs2_split_and_insert(struct inode *inode,
4855 handle_t *handle,
4856 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004857 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004858 struct buffer_head **last_eb_bh,
4859 int split_index,
4860 struct ocfs2_extent_rec *orig_split_rec,
4861 struct ocfs2_alloc_context *meta_ac)
4862{
4863 int ret = 0, depth;
4864 unsigned int insert_range, rec_range, do_leftright = 0;
4865 struct ocfs2_extent_rec tmprec;
4866 struct ocfs2_extent_list *rightmost_el;
4867 struct ocfs2_extent_rec rec;
4868 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4869 struct ocfs2_insert_type insert;
4870 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004871
4872leftright:
4873 /*
4874 * Store a copy of the record on the stack - it might move
4875 * around as the tree is manipulated below.
4876 */
4877 rec = path_leaf_el(path)->l_recs[split_index];
4878
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004879 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004880
4881 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4882 if (depth) {
4883 BUG_ON(!(*last_eb_bh));
4884 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4885 rightmost_el = &eb->h_list;
4886 }
4887
4888 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4889 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004890 ret = ocfs2_grow_tree(inode, handle, et,
4891 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004892 if (ret) {
4893 mlog_errno(ret);
4894 goto out;
4895 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004896 }
4897
4898 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4899 insert.ins_appending = APPEND_NONE;
4900 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004901 insert.ins_tree_depth = depth;
4902
4903 insert_range = le32_to_cpu(split_rec.e_cpos) +
4904 le16_to_cpu(split_rec.e_leaf_clusters);
4905 rec_range = le32_to_cpu(rec.e_cpos) +
4906 le16_to_cpu(rec.e_leaf_clusters);
4907
4908 if (split_rec.e_cpos == rec.e_cpos) {
4909 insert.ins_split = SPLIT_LEFT;
4910 } else if (insert_range == rec_range) {
4911 insert.ins_split = SPLIT_RIGHT;
4912 } else {
4913 /*
4914 * Left/right split. We fake this as a right split
4915 * first and then make a second pass as a left split.
4916 */
4917 insert.ins_split = SPLIT_RIGHT;
4918
4919 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4920 &rec);
4921
4922 split_rec = tmprec;
4923
4924 BUG_ON(do_leftright);
4925 do_leftright = 1;
4926 }
4927
Tao Mae7d4cb62008-08-18 17:38:44 +08004928 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004929 if (ret) {
4930 mlog_errno(ret);
4931 goto out;
4932 }
4933
4934 if (do_leftright == 1) {
4935 u32 cpos;
4936 struct ocfs2_extent_list *el;
4937
4938 do_leftright++;
4939 split_rec = *orig_split_rec;
4940
4941 ocfs2_reinit_path(path, 1);
4942
4943 cpos = le32_to_cpu(split_rec.e_cpos);
Joel Beckerfacdb772009-02-12 18:08:48 -08004944 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07004945 if (ret) {
4946 mlog_errno(ret);
4947 goto out;
4948 }
4949
4950 el = path_leaf_el(path);
4951 split_index = ocfs2_search_extent_list(el, cpos);
4952 goto leftright;
4953 }
4954out:
4955
4956 return ret;
4957}
4958
Tao Ma47be12e2009-01-09 07:32:48 +08004959static int ocfs2_replace_extent_rec(struct inode *inode,
4960 handle_t *handle,
4961 struct ocfs2_path *path,
4962 struct ocfs2_extent_list *el,
4963 int split_index,
4964 struct ocfs2_extent_rec *split_rec)
4965{
4966 int ret;
4967
Joel Becker0cf2f762009-02-12 16:41:25 -08004968 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path,
Tao Ma47be12e2009-01-09 07:32:48 +08004969 path_num_items(path) - 1);
4970 if (ret) {
4971 mlog_errno(ret);
4972 goto out;
4973 }
4974
4975 el->l_recs[split_index] = *split_rec;
4976
4977 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4978out:
4979 return ret;
4980}
4981
Mark Fasheh328d5752007-06-18 10:48:04 -07004982/*
4983 * Mark part or all of the extent record at split_index in the leaf
4984 * pointed to by path as written. This removes the unwritten
4985 * extent flag.
4986 *
4987 * Care is taken to handle contiguousness so as to not grow the tree.
4988 *
4989 * meta_ac is not strictly necessary - we only truly need it if growth
4990 * of the tree is required. All other cases will degrade into a less
4991 * optimal tree layout.
4992 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004993 * last_eb_bh should be the rightmost leaf block for any extent
4994 * btree. Since a split may grow the tree or a merge might shrink it,
4995 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07004996 *
4997 * This code is optimized for readability - several passes might be
4998 * made over certain portions of the tree. All of those blocks will
4999 * have been brought into cache (and pinned via the journal), so the
5000 * extra overhead is not expressed in terms of disk reads.
5001 */
5002static int __ocfs2_mark_extent_written(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08005003 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005004 handle_t *handle,
5005 struct ocfs2_path *path,
5006 int split_index,
5007 struct ocfs2_extent_rec *split_rec,
5008 struct ocfs2_alloc_context *meta_ac,
5009 struct ocfs2_cached_dealloc_ctxt *dealloc)
5010{
5011 int ret = 0;
5012 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08005013 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07005014 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5015 struct ocfs2_merge_ctxt ctxt;
5016 struct ocfs2_extent_list *rightmost_el;
5017
Roel Kluin3cf0c502007-10-27 00:20:36 +02005018 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07005019 ret = -EIO;
5020 mlog_errno(ret);
5021 goto out;
5022 }
5023
5024 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5025 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5026 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5027 ret = -EIO;
5028 mlog_errno(ret);
5029 goto out;
5030 }
5031
Tao Maad5a4d72008-01-30 14:21:32 +08005032 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005033 split_index,
5034 split_rec);
5035
5036 /*
5037 * The core merge / split code wants to know how much room is
5038 * left in this inodes allocation tree, so we pass the
5039 * rightmost extent list.
5040 */
5041 if (path->p_tree_depth) {
5042 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005043
Joel Becker3d03a302009-02-12 17:49:26 -08005044 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005045 ocfs2_et_get_last_eb_blk(et),
5046 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005047 if (ret) {
5048 mlog_exit(ret);
5049 goto out;
5050 }
5051
5052 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005053 rightmost_el = &eb->h_list;
5054 } else
5055 rightmost_el = path_root_el(path);
5056
Mark Fasheh328d5752007-06-18 10:48:04 -07005057 if (rec->e_cpos == split_rec->e_cpos &&
5058 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5059 ctxt.c_split_covers_rec = 1;
5060 else
5061 ctxt.c_split_covers_rec = 0;
5062
5063 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5064
Mark Fasheh015452b2007-09-12 10:21:22 -07005065 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5066 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5067 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005068
5069 if (ctxt.c_contig_type == CONTIG_NONE) {
5070 if (ctxt.c_split_covers_rec)
Tao Ma47be12e2009-01-09 07:32:48 +08005071 ret = ocfs2_replace_extent_rec(inode, handle,
5072 path, el,
5073 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005074 else
Tao Mae7d4cb62008-08-18 17:38:44 +08005075 ret = ocfs2_split_and_insert(inode, handle, path, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005076 &last_eb_bh, split_index,
5077 split_rec, meta_ac);
5078 if (ret)
5079 mlog_errno(ret);
5080 } else {
5081 ret = ocfs2_try_to_merge_extent(inode, handle, path,
5082 split_index, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08005083 dealloc, &ctxt, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005084 if (ret)
5085 mlog_errno(ret);
5086 }
5087
Mark Fasheh328d5752007-06-18 10:48:04 -07005088out:
5089 brelse(last_eb_bh);
5090 return ret;
5091}
5092
5093/*
5094 * Mark the already-existing extent at cpos as written for len clusters.
5095 *
5096 * If the existing extent is larger than the request, initiate a
5097 * split. An attempt will be made at merging with adjacent extents.
5098 *
5099 * The caller is responsible for passing down meta_ac if we'll need it.
5100 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005101int ocfs2_mark_extent_written(struct inode *inode,
5102 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005103 handle_t *handle, u32 cpos, u32 len, u32 phys,
5104 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005105 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005106{
5107 int ret, index;
5108 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
5109 struct ocfs2_extent_rec split_rec;
5110 struct ocfs2_path *left_path = NULL;
5111 struct ocfs2_extent_list *el;
5112
5113 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5114 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
5115
5116 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5117 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5118 "that are being written to, but the feature bit "
5119 "is not set in the super block.",
5120 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5121 ret = -EROFS;
5122 goto out;
5123 }
5124
5125 /*
5126 * XXX: This should be fixed up so that we just re-insert the
5127 * next extent records.
Joel Beckerf99b9b72008-08-20 19:36:33 -07005128 *
5129 * XXX: This is a hack on the extent tree, maybe it should be
5130 * an op?
Mark Fasheh328d5752007-06-18 10:48:04 -07005131 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005132 if (et->et_ops == &ocfs2_dinode_et_ops)
Tao Mae7d4cb62008-08-18 17:38:44 +08005133 ocfs2_extent_map_trunc(inode, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07005134
Joel Beckerffdd7a52008-10-17 22:32:01 -07005135 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005136 if (!left_path) {
5137 ret = -ENOMEM;
5138 mlog_errno(ret);
5139 goto out;
5140 }
5141
Joel Beckerfacdb772009-02-12 18:08:48 -08005142 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005143 if (ret) {
5144 mlog_errno(ret);
5145 goto out;
5146 }
5147 el = path_leaf_el(left_path);
5148
5149 index = ocfs2_search_extent_list(el, cpos);
5150 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5151 ocfs2_error(inode->i_sb,
5152 "Inode %llu has an extent at cpos %u which can no "
5153 "longer be found.\n",
5154 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5155 ret = -EROFS;
5156 goto out;
5157 }
5158
5159 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5160 split_rec.e_cpos = cpu_to_le32(cpos);
5161 split_rec.e_leaf_clusters = cpu_to_le16(len);
5162 split_rec.e_blkno = cpu_to_le64(start_blkno);
5163 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
5164 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
5165
Joel Beckerf99b9b72008-08-20 19:36:33 -07005166 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
Tao Mae7d4cb62008-08-18 17:38:44 +08005167 index, &split_rec, meta_ac,
5168 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005169 if (ret)
5170 mlog_errno(ret);
5171
5172out:
5173 ocfs2_free_path(left_path);
5174 return ret;
5175}
5176
Tao Mae7d4cb62008-08-18 17:38:44 +08005177static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005178 handle_t *handle, struct ocfs2_path *path,
5179 int index, u32 new_range,
5180 struct ocfs2_alloc_context *meta_ac)
5181{
5182 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005183 struct buffer_head *last_eb_bh = NULL;
5184 struct ocfs2_extent_block *eb;
5185 struct ocfs2_extent_list *rightmost_el, *el;
5186 struct ocfs2_extent_rec split_rec;
5187 struct ocfs2_extent_rec *rec;
5188 struct ocfs2_insert_type insert;
5189
5190 /*
5191 * Setup the record to split before we grow the tree.
5192 */
5193 el = path_leaf_el(path);
5194 rec = &el->l_recs[index];
5195 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5196
5197 depth = path->p_tree_depth;
5198 if (depth > 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08005199 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005200 ocfs2_et_get_last_eb_blk(et),
5201 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005202 if (ret < 0) {
5203 mlog_errno(ret);
5204 goto out;
5205 }
5206
5207 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5208 rightmost_el = &eb->h_list;
5209 } else
5210 rightmost_el = path_leaf_el(path);
5211
Tao Ma811f9332008-08-18 17:38:43 +08005212 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005213 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005214 ret = ocfs2_extend_trans(handle, credits);
5215 if (ret) {
5216 mlog_errno(ret);
5217 goto out;
5218 }
5219
5220 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5221 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005222 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005223 meta_ac);
5224 if (ret) {
5225 mlog_errno(ret);
5226 goto out;
5227 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005228 }
5229
5230 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5231 insert.ins_appending = APPEND_NONE;
5232 insert.ins_contig = CONTIG_NONE;
5233 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005234 insert.ins_tree_depth = depth;
5235
Tao Mae7d4cb62008-08-18 17:38:44 +08005236 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005237 if (ret)
5238 mlog_errno(ret);
5239
5240out:
5241 brelse(last_eb_bh);
5242 return ret;
5243}
5244
5245static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5246 struct ocfs2_path *path, int index,
5247 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08005248 u32 cpos, u32 len,
5249 struct ocfs2_extent_tree *et)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005250{
5251 int ret;
5252 u32 left_cpos, rec_range, trunc_range;
5253 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5254 struct super_block *sb = inode->i_sb;
5255 struct ocfs2_path *left_path = NULL;
5256 struct ocfs2_extent_list *el = path_leaf_el(path);
5257 struct ocfs2_extent_rec *rec;
5258 struct ocfs2_extent_block *eb;
5259
5260 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005261 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005262 if (ret) {
5263 mlog_errno(ret);
5264 goto out;
5265 }
5266
5267 index--;
5268 }
5269
5270 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5271 path->p_tree_depth) {
5272 /*
5273 * Check whether this is the rightmost tree record. If
5274 * we remove all of this record or part of its right
5275 * edge then an update of the record lengths above it
5276 * will be required.
5277 */
5278 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5279 if (eb->h_next_leaf_blk == 0)
5280 is_rightmost_tree_rec = 1;
5281 }
5282
5283 rec = &el->l_recs[index];
5284 if (index == 0 && path->p_tree_depth &&
5285 le32_to_cpu(rec->e_cpos) == cpos) {
5286 /*
5287 * Changing the leftmost offset (via partial or whole
5288 * record truncate) of an interior (or rightmost) path
5289 * means we have to update the subtree that is formed
5290 * by this leaf and the one to it's left.
5291 *
5292 * There are two cases we can skip:
5293 * 1) Path is the leftmost one in our inode tree.
5294 * 2) The leaf is rightmost and will be empty after
5295 * we remove the extent record - the rotate code
5296 * knows how to update the newly formed edge.
5297 */
5298
5299 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5300 &left_cpos);
5301 if (ret) {
5302 mlog_errno(ret);
5303 goto out;
5304 }
5305
5306 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005307 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005308 if (!left_path) {
5309 ret = -ENOMEM;
5310 mlog_errno(ret);
5311 goto out;
5312 }
5313
Joel Beckerfacdb772009-02-12 18:08:48 -08005314 ret = ocfs2_find_path(et->et_ci, left_path,
5315 left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005316 if (ret) {
5317 mlog_errno(ret);
5318 goto out;
5319 }
5320 }
5321 }
5322
5323 ret = ocfs2_extend_rotate_transaction(handle, 0,
5324 handle->h_buffer_credits,
5325 path);
5326 if (ret) {
5327 mlog_errno(ret);
5328 goto out;
5329 }
5330
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005331 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005332 if (ret) {
5333 mlog_errno(ret);
5334 goto out;
5335 }
5336
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005337 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005338 if (ret) {
5339 mlog_errno(ret);
5340 goto out;
5341 }
5342
5343 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5344 trunc_range = cpos + len;
5345
5346 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5347 int next_free;
5348
5349 memset(rec, 0, sizeof(*rec));
5350 ocfs2_cleanup_merge(el, index);
5351 wants_rotate = 1;
5352
5353 next_free = le16_to_cpu(el->l_next_free_rec);
5354 if (is_rightmost_tree_rec && next_free > 1) {
5355 /*
5356 * We skip the edge update if this path will
5357 * be deleted by the rotate code.
5358 */
5359 rec = &el->l_recs[next_free - 1];
5360 ocfs2_adjust_rightmost_records(inode, handle, path,
5361 rec);
5362 }
5363 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5364 /* Remove leftmost portion of the record. */
5365 le32_add_cpu(&rec->e_cpos, len);
5366 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5367 le16_add_cpu(&rec->e_leaf_clusters, -len);
5368 } else if (rec_range == trunc_range) {
5369 /* Remove rightmost portion of the record */
5370 le16_add_cpu(&rec->e_leaf_clusters, -len);
5371 if (is_rightmost_tree_rec)
5372 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5373 } else {
5374 /* Caller should have trapped this. */
5375 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5376 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5377 le32_to_cpu(rec->e_cpos),
5378 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5379 BUG();
5380 }
5381
5382 if (left_path) {
5383 int subtree_index;
5384
Joel Becker7dc02802009-02-12 19:20:13 -08005385 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Joel Becker4619c732009-02-12 19:02:36 -08005386 ocfs2_complete_edge_insert(handle, left_path, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005387 subtree_index);
5388 }
5389
5390 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5391
Tao Mae7d4cb62008-08-18 17:38:44 +08005392 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005393 if (ret) {
5394 mlog_errno(ret);
5395 goto out;
5396 }
5397
5398out:
5399 ocfs2_free_path(left_path);
5400 return ret;
5401}
5402
Joel Beckerf99b9b72008-08-20 19:36:33 -07005403int ocfs2_remove_extent(struct inode *inode,
5404 struct ocfs2_extent_tree *et,
Mark Fasheh063c4562007-07-03 13:34:11 -07005405 u32 cpos, u32 len, handle_t *handle,
5406 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005407 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005408{
5409 int ret, index;
5410 u32 rec_range, trunc_range;
5411 struct ocfs2_extent_rec *rec;
5412 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005413 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005414
5415 ocfs2_extent_map_trunc(inode, 0);
5416
Joel Beckerffdd7a52008-10-17 22:32:01 -07005417 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005418 if (!path) {
5419 ret = -ENOMEM;
5420 mlog_errno(ret);
5421 goto out;
5422 }
5423
Joel Beckerfacdb772009-02-12 18:08:48 -08005424 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005425 if (ret) {
5426 mlog_errno(ret);
5427 goto out;
5428 }
5429
5430 el = path_leaf_el(path);
5431 index = ocfs2_search_extent_list(el, cpos);
5432 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5433 ocfs2_error(inode->i_sb,
5434 "Inode %llu has an extent at cpos %u which can no "
5435 "longer be found.\n",
5436 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5437 ret = -EROFS;
5438 goto out;
5439 }
5440
5441 /*
5442 * We have 3 cases of extent removal:
5443 * 1) Range covers the entire extent rec
5444 * 2) Range begins or ends on one edge of the extent rec
5445 * 3) Range is in the middle of the extent rec (no shared edges)
5446 *
5447 * For case 1 we remove the extent rec and left rotate to
5448 * fill the hole.
5449 *
5450 * For case 2 we just shrink the existing extent rec, with a
5451 * tree update if the shrinking edge is also the edge of an
5452 * extent block.
5453 *
5454 * For case 3 we do a right split to turn the extent rec into
5455 * something case 2 can handle.
5456 */
5457 rec = &el->l_recs[index];
5458 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5459 trunc_range = cpos + len;
5460
5461 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5462
5463 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5464 "(cpos %u, len %u)\n",
5465 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5466 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5467
5468 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5469 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005470 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005471 if (ret) {
5472 mlog_errno(ret);
5473 goto out;
5474 }
5475 } else {
Joel Beckerf99b9b72008-08-20 19:36:33 -07005476 ret = ocfs2_split_tree(inode, et, handle, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005477 trunc_range, meta_ac);
5478 if (ret) {
5479 mlog_errno(ret);
5480 goto out;
5481 }
5482
5483 /*
5484 * The split could have manipulated the tree enough to
5485 * move the record location, so we have to look for it again.
5486 */
5487 ocfs2_reinit_path(path, 1);
5488
Joel Beckerfacdb772009-02-12 18:08:48 -08005489 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005490 if (ret) {
5491 mlog_errno(ret);
5492 goto out;
5493 }
5494
5495 el = path_leaf_el(path);
5496 index = ocfs2_search_extent_list(el, cpos);
5497 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5498 ocfs2_error(inode->i_sb,
5499 "Inode %llu: split at cpos %u lost record.",
5500 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5501 cpos);
5502 ret = -EROFS;
5503 goto out;
5504 }
5505
5506 /*
5507 * Double check our values here. If anything is fishy,
5508 * it's easier to catch it at the top level.
5509 */
5510 rec = &el->l_recs[index];
5511 rec_range = le32_to_cpu(rec->e_cpos) +
5512 ocfs2_rec_clusters(el, rec);
5513 if (rec_range != trunc_range) {
5514 ocfs2_error(inode->i_sb,
5515 "Inode %llu: error after split at cpos %u"
5516 "trunc len %u, existing record is (%u,%u)",
5517 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5518 cpos, len, le32_to_cpu(rec->e_cpos),
5519 ocfs2_rec_clusters(el, rec));
5520 ret = -EROFS;
5521 goto out;
5522 }
5523
5524 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005525 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005526 if (ret) {
5527 mlog_errno(ret);
5528 goto out;
5529 }
5530 }
5531
5532out:
5533 ocfs2_free_path(path);
5534 return ret;
5535}
5536
Mark Fashehfecc0112008-11-12 15:16:38 -08005537int ocfs2_remove_btree_range(struct inode *inode,
5538 struct ocfs2_extent_tree *et,
5539 u32 cpos, u32 phys_cpos, u32 len,
5540 struct ocfs2_cached_dealloc_ctxt *dealloc)
5541{
5542 int ret;
5543 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5544 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5545 struct inode *tl_inode = osb->osb_tl_inode;
5546 handle_t *handle;
5547 struct ocfs2_alloc_context *meta_ac = NULL;
5548
5549 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5550 if (ret) {
5551 mlog_errno(ret);
5552 return ret;
5553 }
5554
5555 mutex_lock(&tl_inode->i_mutex);
5556
5557 if (ocfs2_truncate_log_needs_flush(osb)) {
5558 ret = __ocfs2_flush_truncate_log(osb);
5559 if (ret < 0) {
5560 mlog_errno(ret);
5561 goto out;
5562 }
5563 }
5564
Jan Karaa90714c2008-10-09 19:38:40 +02005565 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005566 if (IS_ERR(handle)) {
5567 ret = PTR_ERR(handle);
5568 mlog_errno(ret);
5569 goto out;
5570 }
5571
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005572 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07005573 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005574 if (ret) {
5575 mlog_errno(ret);
5576 goto out;
5577 }
5578
Mark Fashehfd4ef232009-01-29 15:06:21 -08005579 vfs_dq_free_space_nodirty(inode,
5580 ocfs2_clusters_to_bytes(inode->i_sb, len));
5581
Mark Fashehfecc0112008-11-12 15:16:38 -08005582 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5583 dealloc);
5584 if (ret) {
5585 mlog_errno(ret);
5586 goto out_commit;
5587 }
5588
Joel Becker6136ca52009-02-12 19:32:43 -08005589 ocfs2_et_update_clusters(et, -len);
Mark Fashehfecc0112008-11-12 15:16:38 -08005590
5591 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5592 if (ret) {
5593 mlog_errno(ret);
5594 goto out_commit;
5595 }
5596
5597 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5598 if (ret)
5599 mlog_errno(ret);
5600
5601out_commit:
5602 ocfs2_commit_trans(osb, handle);
5603out:
5604 mutex_unlock(&tl_inode->i_mutex);
5605
5606 if (meta_ac)
5607 ocfs2_free_alloc_context(meta_ac);
5608
5609 return ret;
5610}
5611
Mark Fasheh063c4562007-07-03 13:34:11 -07005612int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005613{
5614 struct buffer_head *tl_bh = osb->osb_tl_bh;
5615 struct ocfs2_dinode *di;
5616 struct ocfs2_truncate_log *tl;
5617
5618 di = (struct ocfs2_dinode *) tl_bh->b_data;
5619 tl = &di->id2.i_dealloc;
5620
5621 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5622 "slot %d, invalid truncate log parameters: used = "
5623 "%u, count = %u\n", osb->slot_num,
5624 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5625 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5626}
5627
5628static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5629 unsigned int new_start)
5630{
5631 unsigned int tail_index;
5632 unsigned int current_tail;
5633
5634 /* No records, nothing to coalesce */
5635 if (!le16_to_cpu(tl->tl_used))
5636 return 0;
5637
5638 tail_index = le16_to_cpu(tl->tl_used) - 1;
5639 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5640 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5641
5642 return current_tail == new_start;
5643}
5644
Mark Fasheh063c4562007-07-03 13:34:11 -07005645int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5646 handle_t *handle,
5647 u64 start_blk,
5648 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005649{
5650 int status, index;
5651 unsigned int start_cluster, tl_count;
5652 struct inode *tl_inode = osb->osb_tl_inode;
5653 struct buffer_head *tl_bh = osb->osb_tl_bh;
5654 struct ocfs2_dinode *di;
5655 struct ocfs2_truncate_log *tl;
5656
Mark Fashehb06970532006-03-03 10:24:33 -08005657 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5658 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005659
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005660 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005661
5662 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5663
5664 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005665
Joel Becker10995aa2008-11-13 14:49:12 -08005666 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5667 * by the underlying call to ocfs2_read_inode_block(), so any
5668 * corruption is a code bug */
5669 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5670
5671 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005672 tl_count = le16_to_cpu(tl->tl_count);
5673 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5674 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005675 "Truncate record count on #%llu invalid "
5676 "wanted %u, actual %u\n",
5677 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005678 ocfs2_truncate_recs_per_inode(osb->sb),
5679 le16_to_cpu(tl->tl_count));
5680
5681 /* Caller should have known to flush before calling us. */
5682 index = le16_to_cpu(tl->tl_used);
5683 if (index >= tl_count) {
5684 status = -ENOSPC;
5685 mlog_errno(status);
5686 goto bail;
5687 }
5688
Joel Becker0cf2f762009-02-12 16:41:25 -08005689 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005690 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005691 if (status < 0) {
5692 mlog_errno(status);
5693 goto bail;
5694 }
5695
5696 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005697 "%llu (index = %d)\n", num_clusters, start_cluster,
5698 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005699
5700 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5701 /*
5702 * Move index back to the record we are coalescing with.
5703 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5704 */
5705 index--;
5706
5707 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5708 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5709 index, le32_to_cpu(tl->tl_recs[index].t_start),
5710 num_clusters);
5711 } else {
5712 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5713 tl->tl_used = cpu_to_le16(index + 1);
5714 }
5715 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5716
5717 status = ocfs2_journal_dirty(handle, tl_bh);
5718 if (status < 0) {
5719 mlog_errno(status);
5720 goto bail;
5721 }
5722
5723bail:
5724 mlog_exit(status);
5725 return status;
5726}
5727
5728static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005729 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005730 struct inode *data_alloc_inode,
5731 struct buffer_head *data_alloc_bh)
5732{
5733 int status = 0;
5734 int i;
5735 unsigned int num_clusters;
5736 u64 start_blk;
5737 struct ocfs2_truncate_rec rec;
5738 struct ocfs2_dinode *di;
5739 struct ocfs2_truncate_log *tl;
5740 struct inode *tl_inode = osb->osb_tl_inode;
5741 struct buffer_head *tl_bh = osb->osb_tl_bh;
5742
5743 mlog_entry_void();
5744
5745 di = (struct ocfs2_dinode *) tl_bh->b_data;
5746 tl = &di->id2.i_dealloc;
5747 i = le16_to_cpu(tl->tl_used) - 1;
5748 while (i >= 0) {
5749 /* Caller has given us at least enough credits to
5750 * update the truncate log dinode */
Joel Becker0cf2f762009-02-12 16:41:25 -08005751 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005752 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005753 if (status < 0) {
5754 mlog_errno(status);
5755 goto bail;
5756 }
5757
5758 tl->tl_used = cpu_to_le16(i);
5759
5760 status = ocfs2_journal_dirty(handle, tl_bh);
5761 if (status < 0) {
5762 mlog_errno(status);
5763 goto bail;
5764 }
5765
5766 /* TODO: Perhaps we can calculate the bulk of the
5767 * credits up front rather than extending like
5768 * this. */
5769 status = ocfs2_extend_trans(handle,
5770 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5771 if (status < 0) {
5772 mlog_errno(status);
5773 goto bail;
5774 }
5775
5776 rec = tl->tl_recs[i];
5777 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5778 le32_to_cpu(rec.t_start));
5779 num_clusters = le32_to_cpu(rec.t_clusters);
5780
5781 /* if start_blk is not set, we ignore the record as
5782 * invalid. */
5783 if (start_blk) {
5784 mlog(0, "free record %d, start = %u, clusters = %u\n",
5785 i, le32_to_cpu(rec.t_start), num_clusters);
5786
5787 status = ocfs2_free_clusters(handle, data_alloc_inode,
5788 data_alloc_bh, start_blk,
5789 num_clusters);
5790 if (status < 0) {
5791 mlog_errno(status);
5792 goto bail;
5793 }
5794 }
5795 i--;
5796 }
5797
5798bail:
5799 mlog_exit(status);
5800 return status;
5801}
5802
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005803/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005804int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005805{
5806 int status;
5807 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005808 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005809 struct inode *tl_inode = osb->osb_tl_inode;
5810 struct inode *data_alloc_inode = NULL;
5811 struct buffer_head *tl_bh = osb->osb_tl_bh;
5812 struct buffer_head *data_alloc_bh = NULL;
5813 struct ocfs2_dinode *di;
5814 struct ocfs2_truncate_log *tl;
5815
5816 mlog_entry_void();
5817
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005818 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005819
5820 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005821
Joel Becker10995aa2008-11-13 14:49:12 -08005822 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5823 * by the underlying call to ocfs2_read_inode_block(), so any
5824 * corruption is a code bug */
5825 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5826
5827 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005828 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005829 mlog(0, "Flush %u records from truncate log #%llu\n",
5830 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005831 if (!num_to_flush) {
5832 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005833 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005834 }
5835
5836 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5837 GLOBAL_BITMAP_SYSTEM_INODE,
5838 OCFS2_INVALID_SLOT);
5839 if (!data_alloc_inode) {
5840 status = -EINVAL;
5841 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005842 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005843 }
5844
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005845 mutex_lock(&data_alloc_inode->i_mutex);
5846
Mark Fashehe63aecb62007-10-18 15:30:42 -07005847 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005848 if (status < 0) {
5849 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005850 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005851 }
5852
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005853 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005854 if (IS_ERR(handle)) {
5855 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005856 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005857 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005858 }
5859
5860 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5861 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005862 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005863 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005864
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005865 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005866
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005867out_unlock:
5868 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005869 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005870
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005871out_mutex:
5872 mutex_unlock(&data_alloc_inode->i_mutex);
5873 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005874
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005875out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005876 mlog_exit(status);
5877 return status;
5878}
5879
5880int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5881{
5882 int status;
5883 struct inode *tl_inode = osb->osb_tl_inode;
5884
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005885 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005886 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005887 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005888
5889 return status;
5890}
5891
David Howellsc4028952006-11-22 14:57:56 +00005892static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005893{
5894 int status;
David Howellsc4028952006-11-22 14:57:56 +00005895 struct ocfs2_super *osb =
5896 container_of(work, struct ocfs2_super,
5897 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005898
5899 mlog_entry_void();
5900
5901 status = ocfs2_flush_truncate_log(osb);
5902 if (status < 0)
5903 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005904 else
5905 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005906
5907 mlog_exit(status);
5908}
5909
5910#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5911void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5912 int cancel)
5913{
5914 if (osb->osb_tl_inode) {
5915 /* We want to push off log flushes while truncates are
5916 * still running. */
5917 if (cancel)
5918 cancel_delayed_work(&osb->osb_truncate_log_wq);
5919
5920 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5921 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5922 }
5923}
5924
5925static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5926 int slot_num,
5927 struct inode **tl_inode,
5928 struct buffer_head **tl_bh)
5929{
5930 int status;
5931 struct inode *inode = NULL;
5932 struct buffer_head *bh = NULL;
5933
5934 inode = ocfs2_get_system_file_inode(osb,
5935 TRUNCATE_LOG_SYSTEM_INODE,
5936 slot_num);
5937 if (!inode) {
5938 status = -EINVAL;
5939 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5940 goto bail;
5941 }
5942
Joel Beckerb657c952008-11-13 14:49:11 -08005943 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005944 if (status < 0) {
5945 iput(inode);
5946 mlog_errno(status);
5947 goto bail;
5948 }
5949
5950 *tl_inode = inode;
5951 *tl_bh = bh;
5952bail:
5953 mlog_exit(status);
5954 return status;
5955}
5956
5957/* called during the 1st stage of node recovery. we stamp a clean
5958 * truncate log and pass back a copy for processing later. if the
5959 * truncate log does not require processing, a *tl_copy is set to
5960 * NULL. */
5961int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5962 int slot_num,
5963 struct ocfs2_dinode **tl_copy)
5964{
5965 int status;
5966 struct inode *tl_inode = NULL;
5967 struct buffer_head *tl_bh = NULL;
5968 struct ocfs2_dinode *di;
5969 struct ocfs2_truncate_log *tl;
5970
5971 *tl_copy = NULL;
5972
5973 mlog(0, "recover truncate log from slot %d\n", slot_num);
5974
5975 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5976 if (status < 0) {
5977 mlog_errno(status);
5978 goto bail;
5979 }
5980
5981 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005982
Joel Becker10995aa2008-11-13 14:49:12 -08005983 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5984 * validated by the underlying call to ocfs2_read_inode_block(),
5985 * so any corruption is a code bug */
5986 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5987
5988 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005989 if (le16_to_cpu(tl->tl_used)) {
5990 mlog(0, "We'll have %u logs to recover\n",
5991 le16_to_cpu(tl->tl_used));
5992
5993 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5994 if (!(*tl_copy)) {
5995 status = -ENOMEM;
5996 mlog_errno(status);
5997 goto bail;
5998 }
5999
6000 /* Assuming the write-out below goes well, this copy
6001 * will be passed back to recovery for processing. */
6002 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6003
6004 /* All we need to do to clear the truncate log is set
6005 * tl_used. */
6006 tl->tl_used = 0;
6007
Joel Becker13723d02008-10-17 19:25:01 -07006008 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08006009 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08006010 if (status < 0) {
6011 mlog_errno(status);
6012 goto bail;
6013 }
6014 }
6015
6016bail:
6017 if (tl_inode)
6018 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006019 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006020
6021 if (status < 0 && (*tl_copy)) {
6022 kfree(*tl_copy);
6023 *tl_copy = NULL;
6024 }
6025
6026 mlog_exit(status);
6027 return status;
6028}
6029
6030int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6031 struct ocfs2_dinode *tl_copy)
6032{
6033 int status = 0;
6034 int i;
6035 unsigned int clusters, num_recs, start_cluster;
6036 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006037 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006038 struct inode *tl_inode = osb->osb_tl_inode;
6039 struct ocfs2_truncate_log *tl;
6040
6041 mlog_entry_void();
6042
6043 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6044 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6045 return -EINVAL;
6046 }
6047
6048 tl = &tl_copy->id2.i_dealloc;
6049 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08006050 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006051 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006052
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006053 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006054 for(i = 0; i < num_recs; i++) {
6055 if (ocfs2_truncate_log_needs_flush(osb)) {
6056 status = __ocfs2_flush_truncate_log(osb);
6057 if (status < 0) {
6058 mlog_errno(status);
6059 goto bail_up;
6060 }
6061 }
6062
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006063 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006064 if (IS_ERR(handle)) {
6065 status = PTR_ERR(handle);
6066 mlog_errno(status);
6067 goto bail_up;
6068 }
6069
6070 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6071 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6072 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6073
6074 status = ocfs2_truncate_log_append(osb, handle,
6075 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006076 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006077 if (status < 0) {
6078 mlog_errno(status);
6079 goto bail_up;
6080 }
6081 }
6082
6083bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006084 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006085
6086 mlog_exit(status);
6087 return status;
6088}
6089
6090void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6091{
6092 int status;
6093 struct inode *tl_inode = osb->osb_tl_inode;
6094
6095 mlog_entry_void();
6096
6097 if (tl_inode) {
6098 cancel_delayed_work(&osb->osb_truncate_log_wq);
6099 flush_workqueue(ocfs2_wq);
6100
6101 status = ocfs2_flush_truncate_log(osb);
6102 if (status < 0)
6103 mlog_errno(status);
6104
6105 brelse(osb->osb_tl_bh);
6106 iput(osb->osb_tl_inode);
6107 }
6108
6109 mlog_exit_void();
6110}
6111
6112int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6113{
6114 int status;
6115 struct inode *tl_inode = NULL;
6116 struct buffer_head *tl_bh = NULL;
6117
6118 mlog_entry_void();
6119
6120 status = ocfs2_get_truncate_log_info(osb,
6121 osb->slot_num,
6122 &tl_inode,
6123 &tl_bh);
6124 if (status < 0)
6125 mlog_errno(status);
6126
6127 /* ocfs2_truncate_log_shutdown keys on the existence of
6128 * osb->osb_tl_inode so we don't set any of the osb variables
6129 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006130 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6131 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006132 osb->osb_tl_bh = tl_bh;
6133 osb->osb_tl_inode = tl_inode;
6134
6135 mlog_exit(status);
6136 return status;
6137}
6138
Mark Fasheh2b604352007-06-22 15:45:27 -07006139/*
6140 * Delayed de-allocation of suballocator blocks.
6141 *
6142 * Some sets of block de-allocations might involve multiple suballocator inodes.
6143 *
6144 * The locking for this can get extremely complicated, especially when
6145 * the suballocator inodes to delete from aren't known until deep
6146 * within an unrelated codepath.
6147 *
6148 * ocfs2_extent_block structures are a good example of this - an inode
6149 * btree could have been grown by any number of nodes each allocating
6150 * out of their own suballoc inode.
6151 *
6152 * These structures allow the delay of block de-allocation until a
6153 * later time, when locking of multiple cluster inodes won't cause
6154 * deadlock.
6155 */
6156
6157/*
Tao Ma2891d292008-11-12 08:26:58 +08006158 * Describe a single bit freed from a suballocator. For the block
6159 * suballocators, it represents one block. For the global cluster
6160 * allocator, it represents some clusters and free_bit indicates
6161 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006162 */
6163struct ocfs2_cached_block_free {
6164 struct ocfs2_cached_block_free *free_next;
6165 u64 free_blk;
6166 unsigned int free_bit;
6167};
6168
6169struct ocfs2_per_slot_free_list {
6170 struct ocfs2_per_slot_free_list *f_next_suballocator;
6171 int f_inode_type;
6172 int f_slot;
6173 struct ocfs2_cached_block_free *f_first;
6174};
6175
Tao Ma2891d292008-11-12 08:26:58 +08006176static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6177 int sysfile_type,
6178 int slot,
6179 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006180{
6181 int ret;
6182 u64 bg_blkno;
6183 handle_t *handle;
6184 struct inode *inode;
6185 struct buffer_head *di_bh = NULL;
6186 struct ocfs2_cached_block_free *tmp;
6187
6188 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6189 if (!inode) {
6190 ret = -EINVAL;
6191 mlog_errno(ret);
6192 goto out;
6193 }
6194
6195 mutex_lock(&inode->i_mutex);
6196
Mark Fashehe63aecb62007-10-18 15:30:42 -07006197 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006198 if (ret) {
6199 mlog_errno(ret);
6200 goto out_mutex;
6201 }
6202
6203 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6204 if (IS_ERR(handle)) {
6205 ret = PTR_ERR(handle);
6206 mlog_errno(ret);
6207 goto out_unlock;
6208 }
6209
6210 while (head) {
6211 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6212 head->free_bit);
6213 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6214 head->free_bit, (unsigned long long)head->free_blk);
6215
6216 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6217 head->free_bit, bg_blkno, 1);
6218 if (ret) {
6219 mlog_errno(ret);
6220 goto out_journal;
6221 }
6222
6223 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6224 if (ret) {
6225 mlog_errno(ret);
6226 goto out_journal;
6227 }
6228
6229 tmp = head;
6230 head = head->free_next;
6231 kfree(tmp);
6232 }
6233
6234out_journal:
6235 ocfs2_commit_trans(osb, handle);
6236
6237out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006238 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006239 brelse(di_bh);
6240out_mutex:
6241 mutex_unlock(&inode->i_mutex);
6242 iput(inode);
6243out:
6244 while(head) {
6245 /* Premature exit may have left some dangling items. */
6246 tmp = head;
6247 head = head->free_next;
6248 kfree(tmp);
6249 }
6250
6251 return ret;
6252}
6253
Tao Ma2891d292008-11-12 08:26:58 +08006254int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6255 u64 blkno, unsigned int bit)
6256{
6257 int ret = 0;
6258 struct ocfs2_cached_block_free *item;
6259
6260 item = kmalloc(sizeof(*item), GFP_NOFS);
6261 if (item == NULL) {
6262 ret = -ENOMEM;
6263 mlog_errno(ret);
6264 return ret;
6265 }
6266
6267 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6268 bit, (unsigned long long)blkno);
6269
6270 item->free_blk = blkno;
6271 item->free_bit = bit;
6272 item->free_next = ctxt->c_global_allocator;
6273
6274 ctxt->c_global_allocator = item;
6275 return ret;
6276}
6277
6278static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6279 struct ocfs2_cached_block_free *head)
6280{
6281 struct ocfs2_cached_block_free *tmp;
6282 struct inode *tl_inode = osb->osb_tl_inode;
6283 handle_t *handle;
6284 int ret = 0;
6285
6286 mutex_lock(&tl_inode->i_mutex);
6287
6288 while (head) {
6289 if (ocfs2_truncate_log_needs_flush(osb)) {
6290 ret = __ocfs2_flush_truncate_log(osb);
6291 if (ret < 0) {
6292 mlog_errno(ret);
6293 break;
6294 }
6295 }
6296
6297 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6298 if (IS_ERR(handle)) {
6299 ret = PTR_ERR(handle);
6300 mlog_errno(ret);
6301 break;
6302 }
6303
6304 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6305 head->free_bit);
6306
6307 ocfs2_commit_trans(osb, handle);
6308 tmp = head;
6309 head = head->free_next;
6310 kfree(tmp);
6311
6312 if (ret < 0) {
6313 mlog_errno(ret);
6314 break;
6315 }
6316 }
6317
6318 mutex_unlock(&tl_inode->i_mutex);
6319
6320 while (head) {
6321 /* Premature exit may have left some dangling items. */
6322 tmp = head;
6323 head = head->free_next;
6324 kfree(tmp);
6325 }
6326
6327 return ret;
6328}
6329
Mark Fasheh2b604352007-06-22 15:45:27 -07006330int ocfs2_run_deallocs(struct ocfs2_super *osb,
6331 struct ocfs2_cached_dealloc_ctxt *ctxt)
6332{
6333 int ret = 0, ret2;
6334 struct ocfs2_per_slot_free_list *fl;
6335
6336 if (!ctxt)
6337 return 0;
6338
6339 while (ctxt->c_first_suballocator) {
6340 fl = ctxt->c_first_suballocator;
6341
6342 if (fl->f_first) {
6343 mlog(0, "Free items: (type %u, slot %d)\n",
6344 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006345 ret2 = ocfs2_free_cached_blocks(osb,
6346 fl->f_inode_type,
6347 fl->f_slot,
6348 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006349 if (ret2)
6350 mlog_errno(ret2);
6351 if (!ret)
6352 ret = ret2;
6353 }
6354
6355 ctxt->c_first_suballocator = fl->f_next_suballocator;
6356 kfree(fl);
6357 }
6358
Tao Ma2891d292008-11-12 08:26:58 +08006359 if (ctxt->c_global_allocator) {
6360 ret2 = ocfs2_free_cached_clusters(osb,
6361 ctxt->c_global_allocator);
6362 if (ret2)
6363 mlog_errno(ret2);
6364 if (!ret)
6365 ret = ret2;
6366
6367 ctxt->c_global_allocator = NULL;
6368 }
6369
Mark Fasheh2b604352007-06-22 15:45:27 -07006370 return ret;
6371}
6372
6373static struct ocfs2_per_slot_free_list *
6374ocfs2_find_per_slot_free_list(int type,
6375 int slot,
6376 struct ocfs2_cached_dealloc_ctxt *ctxt)
6377{
6378 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6379
6380 while (fl) {
6381 if (fl->f_inode_type == type && fl->f_slot == slot)
6382 return fl;
6383
6384 fl = fl->f_next_suballocator;
6385 }
6386
6387 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6388 if (fl) {
6389 fl->f_inode_type = type;
6390 fl->f_slot = slot;
6391 fl->f_first = NULL;
6392 fl->f_next_suballocator = ctxt->c_first_suballocator;
6393
6394 ctxt->c_first_suballocator = fl;
6395 }
6396 return fl;
6397}
6398
6399static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6400 int type, int slot, u64 blkno,
6401 unsigned int bit)
6402{
6403 int ret;
6404 struct ocfs2_per_slot_free_list *fl;
6405 struct ocfs2_cached_block_free *item;
6406
6407 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6408 if (fl == NULL) {
6409 ret = -ENOMEM;
6410 mlog_errno(ret);
6411 goto out;
6412 }
6413
6414 item = kmalloc(sizeof(*item), GFP_NOFS);
6415 if (item == NULL) {
6416 ret = -ENOMEM;
6417 mlog_errno(ret);
6418 goto out;
6419 }
6420
6421 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6422 type, slot, bit, (unsigned long long)blkno);
6423
6424 item->free_blk = blkno;
6425 item->free_bit = bit;
6426 item->free_next = fl->f_first;
6427
6428 fl->f_first = item;
6429
6430 ret = 0;
6431out:
6432 return ret;
6433}
6434
Mark Fasheh59a5e412007-06-22 15:52:36 -07006435static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6436 struct ocfs2_extent_block *eb)
6437{
6438 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6439 le16_to_cpu(eb->h_suballoc_slot),
6440 le64_to_cpu(eb->h_blkno),
6441 le16_to_cpu(eb->h_suballoc_bit));
6442}
6443
Mark Fashehccd979b2005-12-15 14:31:24 -08006444/* This function will figure out whether the currently last extent
6445 * block will be deleted, and if it will, what the new last extent
6446 * block will be so we can update his h_next_leaf_blk field, as well
6447 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006448static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006449 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006450 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006451 struct buffer_head **new_last_eb)
6452{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006453 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006454 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006455 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006456 struct ocfs2_extent_block *eb;
6457 struct ocfs2_extent_list *el;
6458 struct buffer_head *bh = NULL;
6459
6460 *new_last_eb = NULL;
6461
Mark Fashehccd979b2005-12-15 14:31:24 -08006462 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006463 if (!path->p_tree_depth)
6464 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006465
6466 /* trunc to zero special case - this makes tree_depth = 0
6467 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006468 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006469 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006470
Mark Fashehdcd05382007-01-16 11:32:23 -08006471 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006472 BUG_ON(!el->l_next_free_rec);
6473
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006474 /*
6475 * Make sure that this extent list will actually be empty
6476 * after we clear away the data. We can shortcut out if
6477 * there's more than one non-empty extent in the
6478 * list. Otherwise, a check of the remaining extent is
6479 * necessary.
6480 */
6481 next_free = le16_to_cpu(el->l_next_free_rec);
6482 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006483 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006484 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006485 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006486
6487 /* We may have a valid extent in index 1, check it. */
6488 if (next_free == 2)
6489 rec = &el->l_recs[1];
6490
6491 /*
6492 * Fall through - no more nonempty extents, so we want
6493 * to delete this leaf.
6494 */
6495 } else {
6496 if (next_free > 1)
6497 goto out;
6498
6499 rec = &el->l_recs[0];
6500 }
6501
6502 if (rec) {
6503 /*
6504 * Check it we'll only be trimming off the end of this
6505 * cluster.
6506 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006507 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006508 goto out;
6509 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006510
Mark Fashehdcd05382007-01-16 11:32:23 -08006511 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6512 if (ret) {
6513 mlog_errno(ret);
6514 goto out;
6515 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006516
Joel Beckerfacdb772009-02-12 18:08:48 -08006517 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08006518 if (ret) {
6519 mlog_errno(ret);
6520 goto out;
6521 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006522
Mark Fashehdcd05382007-01-16 11:32:23 -08006523 eb = (struct ocfs2_extent_block *) bh->b_data;
6524 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006525
6526 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6527 * Any corruption is a code bug. */
6528 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006529
6530 *new_last_eb = bh;
6531 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006532 mlog(0, "returning block %llu, (cpos: %u)\n",
6533 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6534out:
6535 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006536
Mark Fashehdcd05382007-01-16 11:32:23 -08006537 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006538}
6539
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006540/*
6541 * Trim some clusters off the rightmost edge of a tree. Only called
6542 * during truncate.
6543 *
6544 * The caller needs to:
6545 * - start journaling of each path component.
6546 * - compute and fully set up any new last ext block
6547 */
6548static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6549 handle_t *handle, struct ocfs2_truncate_context *tc,
6550 u32 clusters_to_del, u64 *delete_start)
6551{
6552 int ret, i, index = path->p_tree_depth;
6553 u32 new_edge = 0;
6554 u64 deleted_eb = 0;
6555 struct buffer_head *bh;
6556 struct ocfs2_extent_list *el;
6557 struct ocfs2_extent_rec *rec;
6558
6559 *delete_start = 0;
6560
6561 while (index >= 0) {
6562 bh = path->p_node[index].bh;
6563 el = path->p_node[index].el;
6564
6565 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6566 index, (unsigned long long)bh->b_blocknr);
6567
6568 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6569
6570 if (index !=
6571 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6572 ocfs2_error(inode->i_sb,
6573 "Inode %lu has invalid ext. block %llu",
6574 inode->i_ino,
6575 (unsigned long long)bh->b_blocknr);
6576 ret = -EROFS;
6577 goto out;
6578 }
6579
6580find_tail_record:
6581 i = le16_to_cpu(el->l_next_free_rec) - 1;
6582 rec = &el->l_recs[i];
6583
6584 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6585 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006586 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006587 (unsigned long long)le64_to_cpu(rec->e_blkno),
6588 le16_to_cpu(el->l_next_free_rec));
6589
Mark Fashehe48edee2007-03-07 16:46:57 -08006590 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006591
6592 if (le16_to_cpu(el->l_tree_depth) == 0) {
6593 /*
6594 * If the leaf block contains a single empty
6595 * extent and no records, we can just remove
6596 * the block.
6597 */
6598 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6599 memset(rec, 0,
6600 sizeof(struct ocfs2_extent_rec));
6601 el->l_next_free_rec = cpu_to_le16(0);
6602
6603 goto delete;
6604 }
6605
6606 /*
6607 * Remove any empty extents by shifting things
6608 * left. That should make life much easier on
6609 * the code below. This condition is rare
6610 * enough that we shouldn't see a performance
6611 * hit.
6612 */
6613 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6614 le16_add_cpu(&el->l_next_free_rec, -1);
6615
6616 for(i = 0;
6617 i < le16_to_cpu(el->l_next_free_rec); i++)
6618 el->l_recs[i] = el->l_recs[i + 1];
6619
6620 memset(&el->l_recs[i], 0,
6621 sizeof(struct ocfs2_extent_rec));
6622
6623 /*
6624 * We've modified our extent list. The
6625 * simplest way to handle this change
6626 * is to being the search from the
6627 * start again.
6628 */
6629 goto find_tail_record;
6630 }
6631
Mark Fashehe48edee2007-03-07 16:46:57 -08006632 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006633
6634 /*
6635 * We'll use "new_edge" on our way back up the
6636 * tree to know what our rightmost cpos is.
6637 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006638 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006639 new_edge += le32_to_cpu(rec->e_cpos);
6640
6641 /*
6642 * The caller will use this to delete data blocks.
6643 */
6644 *delete_start = le64_to_cpu(rec->e_blkno)
6645 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006646 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006647
6648 /*
6649 * If it's now empty, remove this record.
6650 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006651 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006652 memset(rec, 0,
6653 sizeof(struct ocfs2_extent_rec));
6654 le16_add_cpu(&el->l_next_free_rec, -1);
6655 }
6656 } else {
6657 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6658 memset(rec, 0,
6659 sizeof(struct ocfs2_extent_rec));
6660 le16_add_cpu(&el->l_next_free_rec, -1);
6661
6662 goto delete;
6663 }
6664
6665 /* Can this actually happen? */
6666 if (le16_to_cpu(el->l_next_free_rec) == 0)
6667 goto delete;
6668
6669 /*
6670 * We never actually deleted any clusters
6671 * because our leaf was empty. There's no
6672 * reason to adjust the rightmost edge then.
6673 */
6674 if (new_edge == 0)
6675 goto delete;
6676
Mark Fashehe48edee2007-03-07 16:46:57 -08006677 rec->e_int_clusters = cpu_to_le32(new_edge);
6678 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006679 -le32_to_cpu(rec->e_cpos));
6680
6681 /*
6682 * A deleted child record should have been
6683 * caught above.
6684 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006685 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006686 }
6687
6688delete:
6689 ret = ocfs2_journal_dirty(handle, bh);
6690 if (ret) {
6691 mlog_errno(ret);
6692 goto out;
6693 }
6694
6695 mlog(0, "extent list container %llu, after: record %d: "
6696 "(%u, %u, %llu), next = %u.\n",
6697 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006698 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006699 (unsigned long long)le64_to_cpu(rec->e_blkno),
6700 le16_to_cpu(el->l_next_free_rec));
6701
6702 /*
6703 * We must be careful to only attempt delete of an
6704 * extent block (and not the root inode block).
6705 */
6706 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6707 struct ocfs2_extent_block *eb =
6708 (struct ocfs2_extent_block *)bh->b_data;
6709
6710 /*
6711 * Save this for use when processing the
6712 * parent block.
6713 */
6714 deleted_eb = le64_to_cpu(eb->h_blkno);
6715
6716 mlog(0, "deleting this extent block.\n");
6717
Joel Becker8cb471e2009-02-10 20:00:41 -08006718 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006719
Mark Fashehe48edee2007-03-07 16:46:57 -08006720 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006721 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6722 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6723
Mark Fasheh59a5e412007-06-22 15:52:36 -07006724 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6725 /* An error here is not fatal. */
6726 if (ret < 0)
6727 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006728 } else {
6729 deleted_eb = 0;
6730 }
6731
6732 index--;
6733 }
6734
6735 ret = 0;
6736out:
6737 return ret;
6738}
6739
Mark Fashehccd979b2005-12-15 14:31:24 -08006740static int ocfs2_do_truncate(struct ocfs2_super *osb,
6741 unsigned int clusters_to_del,
6742 struct inode *inode,
6743 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006744 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006745 struct ocfs2_truncate_context *tc,
6746 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006747{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006748 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006749 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006750 struct ocfs2_extent_block *last_eb = NULL;
6751 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006752 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006753 u64 delete_blk = 0;
6754
6755 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6756
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006757 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006758 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006759 if (status < 0) {
6760 mlog_errno(status);
6761 goto bail;
6762 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006763
Mark Fashehdcd05382007-01-16 11:32:23 -08006764 /*
6765 * Each component will be touched, so we might as well journal
6766 * here to avoid having to handle errors later.
6767 */
Joel Becker0cf2f762009-02-12 16:41:25 -08006768 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006769 if (status < 0) {
6770 mlog_errno(status);
6771 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006772 }
6773
6774 if (last_eb_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08006775 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07006776 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08006777 if (status < 0) {
6778 mlog_errno(status);
6779 goto bail;
6780 }
6781
6782 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6783 }
6784
6785 el = &(fe->id2.i_list);
6786
6787 /*
6788 * Lower levels depend on this never happening, but it's best
6789 * to check it up here before changing the tree.
6790 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006791 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006792 ocfs2_error(inode->i_sb,
6793 "Inode %lu has an empty extent record, depth %u\n",
6794 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006795 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006796 goto bail;
6797 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006798
Jan Karaa90714c2008-10-09 19:38:40 +02006799 vfs_dq_free_space_nodirty(inode,
6800 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006801 spin_lock(&OCFS2_I(inode)->ip_lock);
6802 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6803 clusters_to_del;
6804 spin_unlock(&OCFS2_I(inode)->ip_lock);
6805 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006806 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006807
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006808 status = ocfs2_trim_tree(inode, path, handle, tc,
6809 clusters_to_del, &delete_blk);
6810 if (status) {
6811 mlog_errno(status);
6812 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006813 }
6814
Mark Fashehdcd05382007-01-16 11:32:23 -08006815 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006816 /* trunc to zero is a special case. */
6817 el->l_tree_depth = 0;
6818 fe->i_last_eb_blk = 0;
6819 } else if (last_eb)
6820 fe->i_last_eb_blk = last_eb->h_blkno;
6821
6822 status = ocfs2_journal_dirty(handle, fe_bh);
6823 if (status < 0) {
6824 mlog_errno(status);
6825 goto bail;
6826 }
6827
6828 if (last_eb) {
6829 /* If there will be a new last extent block, then by
6830 * definition, there cannot be any leaves to the right of
6831 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006832 last_eb->h_next_leaf_blk = 0;
6833 status = ocfs2_journal_dirty(handle, last_eb_bh);
6834 if (status < 0) {
6835 mlog_errno(status);
6836 goto bail;
6837 }
6838 }
6839
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006840 if (delete_blk) {
6841 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6842 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006843 if (status < 0) {
6844 mlog_errno(status);
6845 goto bail;
6846 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006847 }
6848 status = 0;
6849bail:
Tao Ma60e2ec42009-08-12 14:42:47 +08006850 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006851 mlog_exit(status);
6852 return status;
6853}
6854
Joel Becker2b4e30f2008-09-03 20:03:41 -07006855static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006856{
6857 set_buffer_uptodate(bh);
6858 mark_buffer_dirty(bh);
6859 return 0;
6860}
6861
Mark Fasheh1d410a62007-09-07 14:20:45 -07006862static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6863 unsigned int from, unsigned int to,
6864 struct page *page, int zero, u64 *phys)
6865{
6866 int ret, partial = 0;
6867
6868 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6869 if (ret)
6870 mlog_errno(ret);
6871
6872 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006873 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006874
6875 /*
6876 * Need to set the buffers we zero'd into uptodate
6877 * here if they aren't - ocfs2_map_page_blocks()
6878 * might've skipped some
6879 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006880 ret = walk_page_buffers(handle, page_buffers(page),
6881 from, to, &partial,
6882 ocfs2_zero_func);
6883 if (ret < 0)
6884 mlog_errno(ret);
6885 else if (ocfs2_should_order_data(inode)) {
6886 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006887 if (ret < 0)
6888 mlog_errno(ret);
6889 }
6890
6891 if (!partial)
6892 SetPageUptodate(page);
6893
6894 flush_dcache_page(page);
6895}
6896
Mark Fasheh35edec12007-07-06 14:41:18 -07006897static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6898 loff_t end, struct page **pages,
6899 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006900{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006901 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006902 struct page *page;
6903 unsigned int from, to = PAGE_CACHE_SIZE;
6904 struct super_block *sb = inode->i_sb;
6905
6906 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6907
6908 if (numpages == 0)
6909 goto out;
6910
Mark Fasheh35edec12007-07-06 14:41:18 -07006911 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006912 for(i = 0; i < numpages; i++) {
6913 page = pages[i];
6914
Mark Fasheh35edec12007-07-06 14:41:18 -07006915 from = start & (PAGE_CACHE_SIZE - 1);
6916 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6917 to = end & (PAGE_CACHE_SIZE - 1);
6918
Mark Fasheh60b11392007-02-16 11:46:50 -08006919 BUG_ON(from > PAGE_CACHE_SIZE);
6920 BUG_ON(to > PAGE_CACHE_SIZE);
6921
Mark Fasheh1d410a62007-09-07 14:20:45 -07006922 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6923 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006924
Mark Fasheh35edec12007-07-06 14:41:18 -07006925 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006926 }
6927out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006928 if (pages)
6929 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006930}
6931
Mark Fasheh35edec12007-07-06 14:41:18 -07006932static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006933 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006934{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006935 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006936 struct super_block *sb = inode->i_sb;
6937 struct address_space *mapping = inode->i_mapping;
6938 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006939 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006940
Mark Fasheh35edec12007-07-06 14:41:18 -07006941 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006942
Mark Fasheh35edec12007-07-06 14:41:18 -07006943 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6944 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6945
Mark Fasheh1d410a62007-09-07 14:20:45 -07006946 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006947 last_page_bytes = PAGE_ALIGN(end);
6948 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006949 do {
6950 pages[numpages] = grab_cache_page(mapping, index);
6951 if (!pages[numpages]) {
6952 ret = -ENOMEM;
6953 mlog_errno(ret);
6954 goto out;
6955 }
6956
6957 numpages++;
6958 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006959 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006960
6961out:
6962 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006963 if (pages)
6964 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006965 numpages = 0;
6966 }
6967
6968 *num = numpages;
6969
6970 return ret;
6971}
6972
6973/*
6974 * Zero the area past i_size but still within an allocated
6975 * cluster. This avoids exposing nonzero data on subsequent file
6976 * extends.
6977 *
6978 * We need to call this before i_size is updated on the inode because
6979 * otherwise block_write_full_page() will skip writeout of pages past
6980 * i_size. The new_i_size parameter is passed for this reason.
6981 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006982int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6983 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006984{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006985 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006986 struct page **pages = NULL;
6987 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006988 unsigned int ext_flags;
6989 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006990
6991 /*
6992 * File systems which don't support sparse files zero on every
6993 * extend.
6994 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07006995 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08006996 return 0;
6997
Mark Fasheh1d410a62007-09-07 14:20:45 -07006998 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08006999 sizeof(struct page *), GFP_NOFS);
7000 if (pages == NULL) {
7001 ret = -ENOMEM;
7002 mlog_errno(ret);
7003 goto out;
7004 }
7005
Mark Fasheh1d410a62007-09-07 14:20:45 -07007006 if (range_start == range_end)
7007 goto out;
7008
7009 ret = ocfs2_extent_map_get_blocks(inode,
7010 range_start >> sb->s_blocksize_bits,
7011 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08007012 if (ret) {
7013 mlog_errno(ret);
7014 goto out;
7015 }
7016
Mark Fasheh1d410a62007-09-07 14:20:45 -07007017 /*
7018 * Tail is a hole, or is marked unwritten. In either case, we
7019 * can count on read and write to return/push zero's.
7020 */
7021 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08007022 goto out;
7023
Mark Fasheh1d410a62007-09-07 14:20:45 -07007024 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7025 &numpages);
7026 if (ret) {
7027 mlog_errno(ret);
7028 goto out;
7029 }
7030
Mark Fasheh35edec12007-07-06 14:41:18 -07007031 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7032 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08007033
7034 /*
7035 * Initiate writeout of the pages we zero'd here. We don't
7036 * wait on them - the truncate_inode_pages() call later will
7037 * do that for us.
7038 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007039 ret = do_sync_mapping_range(inode->i_mapping, range_start,
7040 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08007041 if (ret)
7042 mlog_errno(ret);
7043
7044out:
7045 if (pages)
7046 kfree(pages);
7047
7048 return ret;
7049}
7050
Tiger Yangfdd77702008-08-18 17:08:55 +08007051static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7052 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007053{
7054 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08007055 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007056
Tiger Yangfdd77702008-08-18 17:08:55 +08007057 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7058 memset(&di->id2, 0, blocksize -
7059 offsetof(struct ocfs2_dinode, id2) -
7060 xattrsize);
7061 else
7062 memset(&di->id2, 0, blocksize -
7063 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007064}
7065
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007066void ocfs2_dinode_new_extent_list(struct inode *inode,
7067 struct ocfs2_dinode *di)
7068{
Tiger Yangfdd77702008-08-18 17:08:55 +08007069 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007070 di->id2.i_list.l_tree_depth = 0;
7071 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08007072 di->id2.i_list.l_count = cpu_to_le16(
7073 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007074}
7075
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007076void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7077{
7078 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7079 struct ocfs2_inline_data *idata = &di->id2.i_data;
7080
7081 spin_lock(&oi->ip_lock);
7082 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7083 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7084 spin_unlock(&oi->ip_lock);
7085
7086 /*
7087 * We clear the entire i_data structure here so that all
7088 * fields can be properly initialized.
7089 */
Tiger Yangfdd77702008-08-18 17:08:55 +08007090 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007091
Tiger Yangfdd77702008-08-18 17:08:55 +08007092 idata->id_count = cpu_to_le16(
7093 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007094}
7095
7096int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7097 struct buffer_head *di_bh)
7098{
7099 int ret, i, has_data, num_pages = 0;
7100 handle_t *handle;
7101 u64 uninitialized_var(block);
7102 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7103 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7104 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007105 struct ocfs2_alloc_context *data_ac = NULL;
7106 struct page **pages = NULL;
7107 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07007108 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02007109 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007110
7111 has_data = i_size_read(inode) ? 1 : 0;
7112
7113 if (has_data) {
7114 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7115 sizeof(struct page *), GFP_NOFS);
7116 if (pages == NULL) {
7117 ret = -ENOMEM;
7118 mlog_errno(ret);
7119 goto out;
7120 }
7121
7122 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7123 if (ret) {
7124 mlog_errno(ret);
7125 goto out;
7126 }
7127 }
7128
Jan Karaa90714c2008-10-09 19:38:40 +02007129 handle = ocfs2_start_trans(osb,
7130 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007131 if (IS_ERR(handle)) {
7132 ret = PTR_ERR(handle);
7133 mlog_errno(ret);
7134 goto out_unlock;
7135 }
7136
Joel Becker0cf2f762009-02-12 16:41:25 -08007137 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007138 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007139 if (ret) {
7140 mlog_errno(ret);
7141 goto out_commit;
7142 }
7143
7144 if (has_data) {
7145 u32 bit_off, num;
7146 unsigned int page_end;
7147 u64 phys;
7148
Jan Karaa90714c2008-10-09 19:38:40 +02007149 if (vfs_dq_alloc_space_nodirty(inode,
7150 ocfs2_clusters_to_bytes(osb->sb, 1))) {
7151 ret = -EDQUOT;
7152 goto out_commit;
7153 }
7154 did_quota = 1;
7155
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007156 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7157 &num);
7158 if (ret) {
7159 mlog_errno(ret);
7160 goto out_commit;
7161 }
7162
7163 /*
7164 * Save two copies, one for insert, and one that can
7165 * be changed by ocfs2_map_and_dirty_page() below.
7166 */
7167 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7168
7169 /*
7170 * Non sparse file systems zero on extend, so no need
7171 * to do that now.
7172 */
7173 if (!ocfs2_sparse_alloc(osb) &&
7174 PAGE_CACHE_SIZE < osb->s_clustersize)
7175 end = PAGE_CACHE_SIZE;
7176
7177 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7178 if (ret) {
7179 mlog_errno(ret);
7180 goto out_commit;
7181 }
7182
7183 /*
7184 * This should populate the 1st page for us and mark
7185 * it up to date.
7186 */
7187 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7188 if (ret) {
7189 mlog_errno(ret);
7190 goto out_commit;
7191 }
7192
7193 page_end = PAGE_CACHE_SIZE;
7194 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7195 page_end = osb->s_clustersize;
7196
7197 for (i = 0; i < num_pages; i++)
7198 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7199 pages[i], i > 0, &phys);
7200 }
7201
7202 spin_lock(&oi->ip_lock);
7203 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7204 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7205 spin_unlock(&oi->ip_lock);
7206
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007207 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007208
7209 ocfs2_journal_dirty(handle, di_bh);
7210
7211 if (has_data) {
7212 /*
7213 * An error at this point should be extremely rare. If
7214 * this proves to be false, we could always re-build
7215 * the in-inode data from our pages.
7216 */
Joel Becker8d6220d2008-08-22 12:46:09 -07007217 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07007218 ret = ocfs2_insert_extent(osb, handle, inode, &et,
7219 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007220 if (ret) {
7221 mlog_errno(ret);
7222 goto out_commit;
7223 }
7224
7225 inode->i_blocks = ocfs2_inode_sector_count(inode);
7226 }
7227
7228out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007229 if (ret < 0 && did_quota)
7230 vfs_dq_free_space_nodirty(inode,
7231 ocfs2_clusters_to_bytes(osb->sb, 1));
7232
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007233 ocfs2_commit_trans(osb, handle);
7234
7235out_unlock:
7236 if (data_ac)
7237 ocfs2_free_alloc_context(data_ac);
7238
7239out:
7240 if (pages) {
7241 ocfs2_unlock_and_free_pages(pages, num_pages);
7242 kfree(pages);
7243 }
7244
7245 return ret;
7246}
7247
Mark Fashehccd979b2005-12-15 14:31:24 -08007248/*
7249 * It is expected, that by the time you call this function,
7250 * inode->i_size and fe->i_size have been adjusted.
7251 *
7252 * WARNING: This will kfree the truncate context
7253 */
7254int ocfs2_commit_truncate(struct ocfs2_super *osb,
7255 struct inode *inode,
7256 struct buffer_head *fe_bh,
7257 struct ocfs2_truncate_context *tc)
7258{
7259 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08007260 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08007261 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07007262 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007263 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08007264 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08007265 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007266
7267 mlog_entry_void();
7268
Mark Fashehdcd05382007-01-16 11:32:23 -08007269 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007270 i_size_read(inode));
7271
Joel Becker13723d02008-10-17 19:25:01 -07007272 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7273 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08007274 if (!path) {
7275 status = -ENOMEM;
7276 mlog_errno(status);
7277 goto bail;
7278 }
Mark Fasheh83418972007-04-23 18:53:12 -07007279
7280 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7281
Mark Fashehccd979b2005-12-15 14:31:24 -08007282start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007283 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007284 * Check that we still have allocation to delete.
7285 */
7286 if (OCFS2_I(inode)->ip_clusters == 0) {
7287 status = 0;
7288 goto bail;
7289 }
7290
7291 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007292 * Truncate always works against the rightmost tree branch.
7293 */
Joel Beckerfacdb772009-02-12 18:08:48 -08007294 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
Mark Fashehdcd05382007-01-16 11:32:23 -08007295 if (status) {
7296 mlog_errno(status);
7297 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007298 }
7299
Mark Fashehdcd05382007-01-16 11:32:23 -08007300 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7301 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7302
7303 /*
7304 * By now, el will point to the extent list on the bottom most
7305 * portion of this tree. Only the tail record is considered in
7306 * each pass.
7307 *
7308 * We handle the following cases, in order:
7309 * - empty extent: delete the remaining branch
7310 * - remove the entire record
7311 * - remove a partial record
7312 * - no record needs to be removed (truncate has completed)
7313 */
7314 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007315 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7316 ocfs2_error(inode->i_sb,
7317 "Inode %llu has empty extent block at %llu\n",
7318 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7319 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7320 status = -EROFS;
7321 goto bail;
7322 }
7323
Mark Fashehccd979b2005-12-15 14:31:24 -08007324 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08007325 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08007326 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007327 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7328 clusters_to_del = 0;
7329 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007330 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007331 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007332 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08007333 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08007334 new_highest_cpos;
7335 } else {
7336 status = 0;
7337 goto bail;
7338 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007339
Mark Fashehdcd05382007-01-16 11:32:23 -08007340 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7341 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7342
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007343 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007344 tl_sem = 1;
7345 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7346 * record is free for use. If there isn't any, we flush to get
7347 * an empty truncate log. */
7348 if (ocfs2_truncate_log_needs_flush(osb)) {
7349 status = __ocfs2_flush_truncate_log(osb);
7350 if (status < 0) {
7351 mlog_errno(status);
7352 goto bail;
7353 }
7354 }
7355
7356 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007357 (struct ocfs2_dinode *)fe_bh->b_data,
7358 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007359 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007360 if (IS_ERR(handle)) {
7361 status = PTR_ERR(handle);
7362 handle = NULL;
7363 mlog_errno(status);
7364 goto bail;
7365 }
7366
Mark Fashehdcd05382007-01-16 11:32:23 -08007367 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7368 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007369 if (status < 0) {
7370 mlog_errno(status);
7371 goto bail;
7372 }
7373
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007374 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007375 tl_sem = 0;
7376
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007377 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007378 handle = NULL;
7379
Mark Fashehdcd05382007-01-16 11:32:23 -08007380 ocfs2_reinit_path(path, 1);
7381
7382 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007383 * The check above will catch the case where we've truncated
7384 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007385 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007386 goto start;
7387
Mark Fashehccd979b2005-12-15 14:31:24 -08007388bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007389
7390 ocfs2_schedule_truncate_log_flush(osb, 1);
7391
7392 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007393 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007394
7395 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007396 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007397
Mark Fasheh59a5e412007-06-22 15:52:36 -07007398 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7399
Mark Fashehdcd05382007-01-16 11:32:23 -08007400 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007401
7402 /* This will drop the ext_alloc cluster lock for us */
7403 ocfs2_free_truncate_context(tc);
7404
7405 mlog_exit(status);
7406 return status;
7407}
7408
Mark Fashehccd979b2005-12-15 14:31:24 -08007409/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007410 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007411 */
7412int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7413 struct inode *inode,
7414 struct buffer_head *fe_bh,
7415 struct ocfs2_truncate_context **tc)
7416{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007417 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007418 unsigned int new_i_clusters;
7419 struct ocfs2_dinode *fe;
7420 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007421 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007422
7423 mlog_entry_void();
7424
7425 *tc = NULL;
7426
7427 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7428 i_size_read(inode));
7429 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7430
7431 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007432 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7433 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007434
Robert P. J. Daycd861282006-12-13 00:34:52 -08007435 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007436 if (!(*tc)) {
7437 status = -ENOMEM;
7438 mlog_errno(status);
7439 goto bail;
7440 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007441 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007442
Mark Fashehccd979b2005-12-15 14:31:24 -08007443 if (fe->id2.i_list.l_tree_depth) {
Joel Becker3d03a302009-02-12 17:49:26 -08007444 status = ocfs2_read_extent_block(INODE_CACHE(inode),
Joel Becker5e965812008-11-13 14:49:16 -08007445 le64_to_cpu(fe->i_last_eb_blk),
7446 &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007447 if (status < 0) {
7448 mlog_errno(status);
7449 goto bail;
7450 }
7451 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007452 }
7453
7454 (*tc)->tc_last_eb_bh = last_eb_bh;
7455
Mark Fashehccd979b2005-12-15 14:31:24 -08007456 status = 0;
7457bail:
7458 if (status < 0) {
7459 if (*tc)
7460 ocfs2_free_truncate_context(*tc);
7461 *tc = NULL;
7462 }
7463 mlog_exit_void();
7464 return status;
7465}
7466
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007467/*
7468 * 'start' is inclusive, 'end' is not.
7469 */
7470int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7471 unsigned int start, unsigned int end, int trunc)
7472{
7473 int ret;
7474 unsigned int numbytes;
7475 handle_t *handle;
7476 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7477 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7478 struct ocfs2_inline_data *idata = &di->id2.i_data;
7479
7480 if (end > i_size_read(inode))
7481 end = i_size_read(inode);
7482
7483 BUG_ON(start >= end);
7484
7485 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7486 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7487 !ocfs2_supports_inline_data(osb)) {
7488 ocfs2_error(inode->i_sb,
7489 "Inline data flags for inode %llu don't agree! "
7490 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7491 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7492 le16_to_cpu(di->i_dyn_features),
7493 OCFS2_I(inode)->ip_dyn_features,
7494 osb->s_feature_incompat);
7495 ret = -EROFS;
7496 goto out;
7497 }
7498
7499 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7500 if (IS_ERR(handle)) {
7501 ret = PTR_ERR(handle);
7502 mlog_errno(ret);
7503 goto out;
7504 }
7505
Joel Becker0cf2f762009-02-12 16:41:25 -08007506 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007507 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007508 if (ret) {
7509 mlog_errno(ret);
7510 goto out_commit;
7511 }
7512
7513 numbytes = end - start;
7514 memset(idata->id_data + start, 0, numbytes);
7515
7516 /*
7517 * No need to worry about the data page here - it's been
7518 * truncated already and inline data doesn't need it for
7519 * pushing zero's to disk, so we'll let readpage pick it up
7520 * later.
7521 */
7522 if (trunc) {
7523 i_size_write(inode, start);
7524 di->i_size = cpu_to_le64(start);
7525 }
7526
7527 inode->i_blocks = ocfs2_inode_sector_count(inode);
7528 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7529
7530 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7531 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7532
7533 ocfs2_journal_dirty(handle, di_bh);
7534
7535out_commit:
7536 ocfs2_commit_trans(osb, handle);
7537
7538out:
7539 return ret;
7540}
7541
Mark Fashehccd979b2005-12-15 14:31:24 -08007542static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7543{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007544 /*
7545 * The caller is responsible for completing deallocation
7546 * before freeing the context.
7547 */
7548 if (tc->tc_dealloc.c_first_suballocator != NULL)
7549 mlog(ML_NOTICE,
7550 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007551
Mark Fasheha81cb882008-10-07 14:25:16 -07007552 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007553
7554 kfree(tc);
7555}