blob: ec9c2ce7bb0cd26e7ababa446c418d13936c478f [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 /*
Joel Becker4c911ee2009-02-13 02:50:12 -080086 * If this extent tree is supported by an extent map, truncate the
87 * map to clusters,
88 */
89 void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et,
90 u32 clusters);
91
92 /*
Joel Becker1625f8a2008-08-21 17:11:10 -070093 * If ->eo_insert_check() exists, it is called before rec is
94 * inserted into the extent tree. It is optional.
95 */
Joel Becker6136ca52009-02-12 19:32:43 -080096 int (*eo_insert_check)(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -070097 struct ocfs2_extent_rec *rec);
Joel Becker6136ca52009-02-12 19:32:43 -080098 int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
Joel Becker0ce10102008-08-20 17:19:50 -070099
Joel Becker1625f8a2008-08-21 17:11:10 -0700100 /*
101 * --------------------------------------------------------------
102 * The remaining are internal to ocfs2_extent_tree and don't have
103 * accessor functions
104 */
105
106 /*
107 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
108 * It is required.
109 */
Joel Becker0ce10102008-08-20 17:19:50 -0700110 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -0700111
112 /*
113 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
114 * it exists. If it does not, et->et_max_leaf_clusters is set
115 * to 0 (unlimited). Optional.
116 */
Joel Becker6136ca52009-02-12 19:32:43 -0800117 void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800118};
119
Joel Beckerf99b9b72008-08-20 19:36:33 -0700120
121/*
122 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
123 * in the methods.
124 */
125static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
126static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
127 u64 blkno);
Joel Becker6136ca52009-02-12 19:32:43 -0800128static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700129 u32 clusters);
Joel Becker4c911ee2009-02-13 02:50:12 -0800130static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
131 u32 clusters);
Joel Becker6136ca52009-02-12 19:32:43 -0800132static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700133 struct ocfs2_extent_rec *rec);
Joel Becker6136ca52009-02-12 19:32:43 -0800134static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700135static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
136static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
137 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
138 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
139 .eo_update_clusters = ocfs2_dinode_update_clusters,
Joel Becker4c911ee2009-02-13 02:50:12 -0800140 .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700141 .eo_insert_check = ocfs2_dinode_insert_check,
142 .eo_sanity_check = ocfs2_dinode_sanity_check,
143 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
Tao Mae7d4cb62008-08-18 17:38:44 +0800144};
145
146static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
147 u64 blkno)
148{
Joel Beckerea5efa12008-08-20 16:57:27 -0700149 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800150
Joel Beckerf99b9b72008-08-20 19:36:33 -0700151 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800152 di->i_last_eb_blk = cpu_to_le64(blkno);
153}
154
155static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
156{
Joel Beckerea5efa12008-08-20 16:57:27 -0700157 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800158
Joel Beckerf99b9b72008-08-20 19:36:33 -0700159 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800160 return le64_to_cpu(di->i_last_eb_blk);
161}
162
Joel Becker6136ca52009-02-12 19:32:43 -0800163static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +0800164 u32 clusters)
165{
Joel Becker6136ca52009-02-12 19:32:43 -0800166 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
Joel Beckerea5efa12008-08-20 16:57:27 -0700167 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800168
169 le32_add_cpu(&di->i_clusters, clusters);
Joel Becker6136ca52009-02-12 19:32:43 -0800170 spin_lock(&oi->ip_lock);
171 oi->ip_clusters = le32_to_cpu(di->i_clusters);
172 spin_unlock(&oi->ip_lock);
Tao Mae7d4cb62008-08-18 17:38:44 +0800173}
174
Joel Becker4c911ee2009-02-13 02:50:12 -0800175static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
176 u32 clusters)
177{
178 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
179
180 ocfs2_extent_map_trunc(inode, clusters);
181}
182
Joel Becker6136ca52009-02-12 19:32:43 -0800183static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700184 struct ocfs2_extent_rec *rec)
185{
Joel Becker6136ca52009-02-12 19:32:43 -0800186 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
187 struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
Joel Becker1e61ee72008-08-20 18:32:45 -0700188
Joel Becker6136ca52009-02-12 19:32:43 -0800189 BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
Joel Becker1e61ee72008-08-20 18:32:45 -0700190 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
Joel Becker6136ca52009-02-12 19:32:43 -0800191 (oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
Joel Becker1e61ee72008-08-20 18:32:45 -0700192 "Device %s, asking for sparse allocation: inode %llu, "
193 "cpos %u, clusters %u\n",
194 osb->dev_str,
Joel Becker6136ca52009-02-12 19:32:43 -0800195 (unsigned long long)oi->ip_blkno,
196 rec->e_cpos, oi->ip_clusters);
Joel Becker1e61ee72008-08-20 18:32:45 -0700197
198 return 0;
199}
200
Joel Becker6136ca52009-02-12 19:32:43 -0800201static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800202{
Joel Becker10995aa2008-11-13 14:49:12 -0800203 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800204
Joel Beckerf99b9b72008-08-20 19:36:33 -0700205 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Joel Becker10995aa2008-11-13 14:49:12 -0800206 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
Tao Mae7d4cb62008-08-18 17:38:44 +0800207
Joel Becker10995aa2008-11-13 14:49:12 -0800208 return 0;
Tao Mae7d4cb62008-08-18 17:38:44 +0800209}
210
Joel Beckerf99b9b72008-08-20 19:36:33 -0700211static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
212{
213 struct ocfs2_dinode *di = et->et_object;
214
215 et->et_root_el = &di->id2.i_list;
216}
217
Tao Mae7d4cb62008-08-18 17:38:44 +0800218
Joel Becker0ce10102008-08-20 17:19:50 -0700219static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
220{
Joel Becker2a50a742008-12-09 14:24:33 -0800221 struct ocfs2_xattr_value_buf *vb = et->et_object;
Joel Becker0ce10102008-08-20 17:19:50 -0700222
Joel Becker2a50a742008-12-09 14:24:33 -0800223 et->et_root_el = &vb->vb_xv->xr_list;
Joel Becker0ce10102008-08-20 17:19:50 -0700224}
225
Tao Maf56654c2008-08-18 17:38:48 +0800226static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
227 u64 blkno)
228{
Joel Becker2a50a742008-12-09 14:24:33 -0800229 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800230
Joel Becker2a50a742008-12-09 14:24:33 -0800231 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
Tao Maf56654c2008-08-18 17:38:48 +0800232}
233
234static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
235{
Joel Becker2a50a742008-12-09 14:24:33 -0800236 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800237
Joel Becker2a50a742008-12-09 14:24:33 -0800238 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
Tao Maf56654c2008-08-18 17:38:48 +0800239}
240
Joel Becker6136ca52009-02-12 19:32:43 -0800241static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
Tao Maf56654c2008-08-18 17:38:48 +0800242 u32 clusters)
243{
Joel Becker2a50a742008-12-09 14:24:33 -0800244 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800245
Joel Becker2a50a742008-12-09 14:24:33 -0800246 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800247}
248
Joel Becker1a09f552008-08-20 17:44:24 -0700249static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700250 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
251 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
252 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700253 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800254};
255
Joel Becker0ce10102008-08-20 17:19:50 -0700256static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
257{
258 struct ocfs2_xattr_block *xb = et->et_object;
259
260 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
261}
262
Joel Becker6136ca52009-02-12 19:32:43 -0800263static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
Joel Becker943cced2008-08-20 17:31:10 -0700264{
Joel Becker6136ca52009-02-12 19:32:43 -0800265 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Joel Becker943cced2008-08-20 17:31:10 -0700266 et->et_max_leaf_clusters =
Joel Becker6136ca52009-02-12 19:32:43 -0800267 ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
Joel Becker943cced2008-08-20 17:31:10 -0700268}
269
Tao Maba492612008-08-18 17:38:49 +0800270static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
271 u64 blkno)
272{
Joel Beckerea5efa12008-08-20 16:57:27 -0700273 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800274 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
275
276 xt->xt_last_eb_blk = cpu_to_le64(blkno);
277}
278
279static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
280{
Joel Beckerea5efa12008-08-20 16:57:27 -0700281 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800282 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
283
284 return le64_to_cpu(xt->xt_last_eb_blk);
285}
286
Joel Becker6136ca52009-02-12 19:32:43 -0800287static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
Tao Maba492612008-08-18 17:38:49 +0800288 u32 clusters)
289{
Joel Beckerea5efa12008-08-20 16:57:27 -0700290 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800291
292 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
293}
294
Tao Maba492612008-08-18 17:38:49 +0800295static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700296 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
297 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
298 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700299 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
Joel Becker943cced2008-08-20 17:31:10 -0700300 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
Tao Maba492612008-08-18 17:38:49 +0800301};
302
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800303static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
304 u64 blkno)
305{
306 struct ocfs2_dx_root_block *dx_root = et->et_object;
307
308 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
309}
310
311static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
312{
313 struct ocfs2_dx_root_block *dx_root = et->et_object;
314
315 return le64_to_cpu(dx_root->dr_last_eb_blk);
316}
317
Joel Becker6136ca52009-02-12 19:32:43 -0800318static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800319 u32 clusters)
320{
321 struct ocfs2_dx_root_block *dx_root = et->et_object;
322
323 le32_add_cpu(&dx_root->dr_clusters, clusters);
324}
325
Joel Becker6136ca52009-02-12 19:32:43 -0800326static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800327{
328 struct ocfs2_dx_root_block *dx_root = et->et_object;
329
330 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
331
332 return 0;
333}
334
335static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
336{
337 struct ocfs2_dx_root_block *dx_root = et->et_object;
338
339 et->et_root_el = &dx_root->dr_list;
340}
341
342static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
343 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
344 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
345 .eo_update_clusters = ocfs2_dx_root_update_clusters,
346 .eo_sanity_check = ocfs2_dx_root_sanity_check,
347 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
348};
349
Joel Becker8d6220d2008-08-22 12:46:09 -0700350static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
351 struct inode *inode,
352 struct buffer_head *bh,
Joel Becker13723d02008-10-17 19:25:01 -0700353 ocfs2_journal_access_func access,
Joel Becker8d6220d2008-08-22 12:46:09 -0700354 void *obj,
355 struct ocfs2_extent_tree_operations *ops)
Tao Mae7d4cb62008-08-18 17:38:44 +0800356{
Joel Becker1a09f552008-08-20 17:44:24 -0700357 et->et_ops = ops;
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700358 et->et_root_bh = bh;
Joel Beckerd9a0a1f2009-02-12 17:32:34 -0800359 et->et_ci = INODE_CACHE(inode);
Joel Becker13723d02008-10-17 19:25:01 -0700360 et->et_root_journal_access = access;
Joel Beckerea5efa12008-08-20 16:57:27 -0700361 if (!obj)
362 obj = (void *)bh->b_data;
363 et->et_object = obj;
Tao Mae7d4cb62008-08-18 17:38:44 +0800364
Joel Becker0ce10102008-08-20 17:19:50 -0700365 et->et_ops->eo_fill_root_el(et);
Joel Becker943cced2008-08-20 17:31:10 -0700366 if (!et->et_ops->eo_fill_max_leaf_clusters)
367 et->et_max_leaf_clusters = 0;
368 else
Joel Becker6136ca52009-02-12 19:32:43 -0800369 et->et_ops->eo_fill_max_leaf_clusters(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800370}
371
Joel Becker8d6220d2008-08-22 12:46:09 -0700372void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
373 struct inode *inode,
374 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700375{
Joel Becker13723d02008-10-17 19:25:01 -0700376 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_di,
377 NULL, &ocfs2_dinode_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700378}
379
Joel Becker8d6220d2008-08-22 12:46:09 -0700380void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700381 struct inode *inode,
Joel Becker8d6220d2008-08-22 12:46:09 -0700382 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700383{
Joel Becker13723d02008-10-17 19:25:01 -0700384 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_xb,
385 NULL, &ocfs2_xattr_tree_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700386}
387
Joel Becker8d6220d2008-08-22 12:46:09 -0700388void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
389 struct inode *inode,
Joel Becker2a50a742008-12-09 14:24:33 -0800390 struct ocfs2_xattr_value_buf *vb)
Tao Mae7d4cb62008-08-18 17:38:44 +0800391{
Joel Becker2a50a742008-12-09 14:24:33 -0800392 __ocfs2_init_extent_tree(et, inode, vb->vb_bh, vb->vb_access, vb,
Joel Becker8d6220d2008-08-22 12:46:09 -0700393 &ocfs2_xattr_value_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800394}
395
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800396void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
397 struct inode *inode,
398 struct buffer_head *bh)
399{
400 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_dr,
401 NULL, &ocfs2_dx_root_et_ops);
402}
403
Joel Becker35dc0aa2008-08-20 16:25:06 -0700404static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
405 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800406{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700407 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800408}
409
Joel Becker35dc0aa2008-08-20 16:25:06 -0700410static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800411{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700412 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800413}
414
Joel Becker6136ca52009-02-12 19:32:43 -0800415static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
Joel Becker35dc0aa2008-08-20 16:25:06 -0700416 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800417{
Joel Becker6136ca52009-02-12 19:32:43 -0800418 et->et_ops->eo_update_clusters(et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700419}
420
Joel Becker4c911ee2009-02-13 02:50:12 -0800421static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et,
422 u32 clusters)
423{
424 if (et->et_ops->eo_extent_map_truncate)
425 et->et_ops->eo_extent_map_truncate(et, clusters);
426}
427
Joel Becker13723d02008-10-17 19:25:01 -0700428static inline int ocfs2_et_root_journal_access(handle_t *handle,
Joel Becker13723d02008-10-17 19:25:01 -0700429 struct ocfs2_extent_tree *et,
430 int type)
431{
Joel Beckerd9a0a1f2009-02-12 17:32:34 -0800432 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700433 type);
434}
435
Joel Becker6136ca52009-02-12 19:32:43 -0800436static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700437 struct ocfs2_extent_rec *rec)
438{
439 int ret = 0;
440
441 if (et->et_ops->eo_insert_check)
Joel Becker6136ca52009-02-12 19:32:43 -0800442 ret = et->et_ops->eo_insert_check(et, rec);
Joel Becker1e61ee72008-08-20 18:32:45 -0700443 return ret;
444}
445
Joel Becker6136ca52009-02-12 19:32:43 -0800446static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
Joel Becker35dc0aa2008-08-20 16:25:06 -0700447{
Joel Becker1e61ee72008-08-20 18:32:45 -0700448 int ret = 0;
449
450 if (et->et_ops->eo_sanity_check)
Joel Becker6136ca52009-02-12 19:32:43 -0800451 ret = et->et_ops->eo_sanity_check(et);
Joel Becker1e61ee72008-08-20 18:32:45 -0700452 return ret;
Tao Mae7d4cb62008-08-18 17:38:44 +0800453}
454
Mark Fashehccd979b2005-12-15 14:31:24 -0800455static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
Mark Fasheh59a5e412007-06-22 15:52:36 -0700456static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
457 struct ocfs2_extent_block *eb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800458
Mark Fashehdcd05382007-01-16 11:32:23 -0800459/*
460 * Structures which describe a path through a btree, and functions to
461 * manipulate them.
462 *
463 * The idea here is to be as generic as possible with the tree
464 * manipulation code.
465 */
466struct ocfs2_path_item {
467 struct buffer_head *bh;
468 struct ocfs2_extent_list *el;
469};
470
471#define OCFS2_MAX_PATH_DEPTH 5
472
473struct ocfs2_path {
Joel Becker13723d02008-10-17 19:25:01 -0700474 int p_tree_depth;
475 ocfs2_journal_access_func p_root_access;
476 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
Mark Fashehdcd05382007-01-16 11:32:23 -0800477};
478
479#define path_root_bh(_path) ((_path)->p_node[0].bh)
480#define path_root_el(_path) ((_path)->p_node[0].el)
Joel Becker13723d02008-10-17 19:25:01 -0700481#define path_root_access(_path)((_path)->p_root_access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800482#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
483#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
484#define path_num_items(_path) ((_path)->p_tree_depth + 1)
485
Joel Beckerfacdb772009-02-12 18:08:48 -0800486static int ocfs2_find_path(struct ocfs2_caching_info *ci,
487 struct ocfs2_path *path, u32 cpos);
Joel Beckerd401dc12009-02-13 02:24:10 -0800488static void ocfs2_adjust_rightmost_records(handle_t *handle,
489 struct ocfs2_extent_tree *et,
Tao Ma6b791bc2009-06-12 14:18:36 +0800490 struct ocfs2_path *path,
491 struct ocfs2_extent_rec *insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -0800492/*
493 * Reset the actual path elements so that we can re-use the structure
494 * to build another path. Generally, this involves freeing the buffer
495 * heads.
496 */
497static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
498{
499 int i, start = 0, depth = 0;
500 struct ocfs2_path_item *node;
501
502 if (keep_root)
503 start = 1;
504
505 for(i = start; i < path_num_items(path); i++) {
506 node = &path->p_node[i];
507
508 brelse(node->bh);
509 node->bh = NULL;
510 node->el = NULL;
511 }
512
513 /*
514 * Tree depth may change during truncate, or insert. If we're
515 * keeping the root extent list, then make sure that our path
516 * structure reflects the proper depth.
517 */
518 if (keep_root)
519 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
Joel Becker13723d02008-10-17 19:25:01 -0700520 else
521 path_root_access(path) = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -0800522
523 path->p_tree_depth = depth;
524}
525
526static void ocfs2_free_path(struct ocfs2_path *path)
527{
528 if (path) {
529 ocfs2_reinit_path(path, 0);
530 kfree(path);
531 }
532}
533
534/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700535 * All the elements of src into dest. After this call, src could be freed
536 * without affecting dest.
537 *
538 * Both paths should have the same root. Any non-root elements of dest
539 * will be freed.
540 */
541static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
542{
543 int i;
544
545 BUG_ON(path_root_bh(dest) != path_root_bh(src));
546 BUG_ON(path_root_el(dest) != path_root_el(src));
Joel Becker13723d02008-10-17 19:25:01 -0700547 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fasheh328d5752007-06-18 10:48:04 -0700548
549 ocfs2_reinit_path(dest, 1);
550
551 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
552 dest->p_node[i].bh = src->p_node[i].bh;
553 dest->p_node[i].el = src->p_node[i].el;
554
555 if (dest->p_node[i].bh)
556 get_bh(dest->p_node[i].bh);
557 }
558}
559
560/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800561 * Make the *dest path the same as src and re-initialize src path to
562 * have a root only.
563 */
564static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
565{
566 int i;
567
568 BUG_ON(path_root_bh(dest) != path_root_bh(src));
Joel Becker13723d02008-10-17 19:25:01 -0700569 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fashehdcd05382007-01-16 11:32:23 -0800570
571 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
572 brelse(dest->p_node[i].bh);
573
574 dest->p_node[i].bh = src->p_node[i].bh;
575 dest->p_node[i].el = src->p_node[i].el;
576
577 src->p_node[i].bh = NULL;
578 src->p_node[i].el = NULL;
579 }
580}
581
582/*
583 * Insert an extent block at given index.
584 *
585 * This will not take an additional reference on eb_bh.
586 */
587static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
588 struct buffer_head *eb_bh)
589{
590 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
591
592 /*
593 * Right now, no root bh is an extent block, so this helps
594 * catch code errors with dinode trees. The assertion can be
595 * safely removed if we ever need to insert extent block
596 * structures at the root.
597 */
598 BUG_ON(index == 0);
599
600 path->p_node[index].bh = eb_bh;
601 path->p_node[index].el = &eb->h_list;
602}
603
604static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700605 struct ocfs2_extent_list *root_el,
606 ocfs2_journal_access_func access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800607{
608 struct ocfs2_path *path;
609
610 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
611
612 path = kzalloc(sizeof(*path), GFP_NOFS);
613 if (path) {
614 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
615 get_bh(root_bh);
616 path_root_bh(path) = root_bh;
617 path_root_el(path) = root_el;
Joel Becker13723d02008-10-17 19:25:01 -0700618 path_root_access(path) = access;
Mark Fashehdcd05382007-01-16 11:32:23 -0800619 }
620
621 return path;
622}
623
Joel Beckerffdd7a52008-10-17 22:32:01 -0700624static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
625{
Joel Becker13723d02008-10-17 19:25:01 -0700626 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
627 path_root_access(path));
Joel Beckerffdd7a52008-10-17 22:32:01 -0700628}
629
630static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
631{
Joel Becker13723d02008-10-17 19:25:01 -0700632 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
633 et->et_root_journal_access);
634}
635
636/*
637 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
638 * otherwise it's the root_access function.
639 *
640 * I don't like the way this function's name looks next to
641 * ocfs2_journal_access_path(), but I don't have a better one.
642 */
643static int ocfs2_path_bh_journal_access(handle_t *handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800644 struct ocfs2_caching_info *ci,
Joel Becker13723d02008-10-17 19:25:01 -0700645 struct ocfs2_path *path,
646 int idx)
647{
648 ocfs2_journal_access_func access = path_root_access(path);
649
650 if (!access)
651 access = ocfs2_journal_access;
652
653 if (idx)
654 access = ocfs2_journal_access_eb;
655
Joel Becker0cf2f762009-02-12 16:41:25 -0800656 return access(handle, ci, path->p_node[idx].bh,
Joel Becker13723d02008-10-17 19:25:01 -0700657 OCFS2_JOURNAL_ACCESS_WRITE);
Joel Beckerffdd7a52008-10-17 22:32:01 -0700658}
659
Mark Fashehdcd05382007-01-16 11:32:23 -0800660/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800661 * Convenience function to journal all components in a path.
662 */
Joel Becker0cf2f762009-02-12 16:41:25 -0800663static int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
664 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -0800665 struct ocfs2_path *path)
666{
667 int i, ret = 0;
668
669 if (!path)
670 goto out;
671
672 for(i = 0; i < path_num_items(path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800673 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -0800674 if (ret < 0) {
675 mlog_errno(ret);
676 goto out;
677 }
678 }
679
680out:
681 return ret;
682}
683
Mark Fasheh328d5752007-06-18 10:48:04 -0700684/*
685 * Return the index of the extent record which contains cluster #v_cluster.
686 * -1 is returned if it was not found.
687 *
688 * Should work fine on interior and exterior nodes.
689 */
690int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
691{
692 int ret = -1;
693 int i;
694 struct ocfs2_extent_rec *rec;
695 u32 rec_end, rec_start, clusters;
696
697 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
698 rec = &el->l_recs[i];
699
700 rec_start = le32_to_cpu(rec->e_cpos);
701 clusters = ocfs2_rec_clusters(el, rec);
702
703 rec_end = rec_start + clusters;
704
705 if (v_cluster >= rec_start && v_cluster < rec_end) {
706 ret = i;
707 break;
708 }
709 }
710
711 return ret;
712}
713
Mark Fashehdcd05382007-01-16 11:32:23 -0800714enum ocfs2_contig_type {
715 CONTIG_NONE = 0,
716 CONTIG_LEFT,
Mark Fasheh328d5752007-06-18 10:48:04 -0700717 CONTIG_RIGHT,
718 CONTIG_LEFTRIGHT,
Mark Fashehdcd05382007-01-16 11:32:23 -0800719};
720
Mark Fashehe48edee2007-03-07 16:46:57 -0800721
722/*
723 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
724 * ocfs2_extent_contig only work properly against leaf nodes!
725 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800726static int ocfs2_block_extent_contig(struct super_block *sb,
727 struct ocfs2_extent_rec *ext,
728 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800729{
Mark Fashehe48edee2007-03-07 16:46:57 -0800730 u64 blk_end = le64_to_cpu(ext->e_blkno);
731
732 blk_end += ocfs2_clusters_to_blocks(sb,
733 le16_to_cpu(ext->e_leaf_clusters));
734
735 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800736}
737
Mark Fashehdcd05382007-01-16 11:32:23 -0800738static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
739 struct ocfs2_extent_rec *right)
740{
Mark Fashehe48edee2007-03-07 16:46:57 -0800741 u32 left_range;
742
743 left_range = le32_to_cpu(left->e_cpos) +
744 le16_to_cpu(left->e_leaf_clusters);
745
746 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800747}
748
749static enum ocfs2_contig_type
750 ocfs2_extent_contig(struct inode *inode,
751 struct ocfs2_extent_rec *ext,
752 struct ocfs2_extent_rec *insert_rec)
753{
754 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
755
Mark Fasheh328d5752007-06-18 10:48:04 -0700756 /*
757 * Refuse to coalesce extent records with different flag
758 * fields - we don't want to mix unwritten extents with user
759 * data.
760 */
761 if (ext->e_flags != insert_rec->e_flags)
762 return CONTIG_NONE;
763
Mark Fashehdcd05382007-01-16 11:32:23 -0800764 if (ocfs2_extents_adjacent(ext, insert_rec) &&
765 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
766 return CONTIG_RIGHT;
767
768 blkno = le64_to_cpu(ext->e_blkno);
769 if (ocfs2_extents_adjacent(insert_rec, ext) &&
770 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
771 return CONTIG_LEFT;
772
773 return CONTIG_NONE;
774}
775
776/*
777 * NOTE: We can have pretty much any combination of contiguousness and
778 * appending.
779 *
780 * The usefulness of APPEND_TAIL is more in that it lets us know that
781 * we'll have to update the path to that leaf.
782 */
783enum ocfs2_append_type {
784 APPEND_NONE = 0,
785 APPEND_TAIL,
786};
787
Mark Fasheh328d5752007-06-18 10:48:04 -0700788enum ocfs2_split_type {
789 SPLIT_NONE = 0,
790 SPLIT_LEFT,
791 SPLIT_RIGHT,
792};
793
Mark Fashehdcd05382007-01-16 11:32:23 -0800794struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700795 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800796 enum ocfs2_append_type ins_appending;
797 enum ocfs2_contig_type ins_contig;
798 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800799 int ins_tree_depth;
800};
801
Mark Fasheh328d5752007-06-18 10:48:04 -0700802struct ocfs2_merge_ctxt {
803 enum ocfs2_contig_type c_contig_type;
804 int c_has_empty_extent;
805 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700806};
807
Joel Becker5e965812008-11-13 14:49:16 -0800808static int ocfs2_validate_extent_block(struct super_block *sb,
809 struct buffer_head *bh)
810{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700811 int rc;
Joel Becker5e965812008-11-13 14:49:16 -0800812 struct ocfs2_extent_block *eb =
813 (struct ocfs2_extent_block *)bh->b_data;
814
Joel Becker970e4932008-11-13 14:49:19 -0800815 mlog(0, "Validating extent block %llu\n",
816 (unsigned long long)bh->b_blocknr);
817
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700818 BUG_ON(!buffer_uptodate(bh));
819
820 /*
821 * If the ecc fails, we return the error but otherwise
822 * leave the filesystem running. We know any error is
823 * local to this block.
824 */
825 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
Joel Becker13723d02008-10-17 19:25:01 -0700826 if (rc) {
827 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
828 (unsigned long long)bh->b_blocknr);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700829 return rc;
Joel Becker13723d02008-10-17 19:25:01 -0700830 }
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700831
832 /*
833 * Errors after here are fatal.
834 */
835
Joel Becker5e965812008-11-13 14:49:16 -0800836 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
837 ocfs2_error(sb,
838 "Extent block #%llu has bad signature %.*s",
839 (unsigned long long)bh->b_blocknr, 7,
840 eb->h_signature);
841 return -EINVAL;
842 }
843
844 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
845 ocfs2_error(sb,
846 "Extent block #%llu has an invalid h_blkno "
847 "of %llu",
848 (unsigned long long)bh->b_blocknr,
849 (unsigned long long)le64_to_cpu(eb->h_blkno));
850 return -EINVAL;
851 }
852
853 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
854 ocfs2_error(sb,
855 "Extent block #%llu has an invalid "
856 "h_fs_generation of #%u",
857 (unsigned long long)bh->b_blocknr,
858 le32_to_cpu(eb->h_fs_generation));
859 return -EINVAL;
860 }
861
862 return 0;
863}
864
Joel Becker3d03a302009-02-12 17:49:26 -0800865int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
Joel Becker5e965812008-11-13 14:49:16 -0800866 struct buffer_head **bh)
867{
868 int rc;
869 struct buffer_head *tmp = *bh;
870
Joel Becker3d03a302009-02-12 17:49:26 -0800871 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800872 ocfs2_validate_extent_block);
Joel Becker5e965812008-11-13 14:49:16 -0800873
874 /* If ocfs2_read_block() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -0800875 if (!rc && !*bh)
Joel Becker5e965812008-11-13 14:49:16 -0800876 *bh = tmp;
877
Joel Becker5e965812008-11-13 14:49:16 -0800878 return rc;
879}
880
881
Mark Fashehccd979b2005-12-15 14:31:24 -0800882/*
883 * How many free extents have we got before we need more meta data?
884 */
885int ocfs2_num_free_extents(struct ocfs2_super *osb,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700886 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -0800887{
888 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800889 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800890 struct ocfs2_extent_block *eb;
891 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800892 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800893
894 mlog_entry_void();
895
Joel Beckerf99b9b72008-08-20 19:36:33 -0700896 el = et->et_root_el;
897 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800898
Tao Mae7d4cb62008-08-18 17:38:44 +0800899 if (last_eb_blk) {
Joel Becker3d03a302009-02-12 17:49:26 -0800900 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
901 &eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800902 if (retval < 0) {
903 mlog_errno(retval);
904 goto bail;
905 }
906 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
907 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800908 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800909
910 BUG_ON(el->l_tree_depth != 0);
911
912 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
913bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700914 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800915
916 mlog_exit(retval);
917 return retval;
918}
919
920/* expects array to already be allocated
921 *
922 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
923 * l_count for you
924 */
Joel Becker42a5a7a2009-02-12 18:49:19 -0800925static int ocfs2_create_new_meta_bhs(handle_t *handle,
926 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -0800927 int wanted,
928 struct ocfs2_alloc_context *meta_ac,
929 struct buffer_head *bhs[])
930{
931 int count, status, i;
932 u16 suballoc_bit_start;
933 u32 num_got;
934 u64 first_blkno;
Joel Becker42a5a7a2009-02-12 18:49:19 -0800935 struct ocfs2_super *osb =
936 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -0800937 struct ocfs2_extent_block *eb;
938
939 mlog_entry_void();
940
941 count = 0;
942 while (count < wanted) {
943 status = ocfs2_claim_metadata(osb,
944 handle,
945 meta_ac,
946 wanted - count,
947 &suballoc_bit_start,
948 &num_got,
949 &first_blkno);
950 if (status < 0) {
951 mlog_errno(status);
952 goto bail;
953 }
954
955 for(i = count; i < (num_got + count); i++) {
956 bhs[i] = sb_getblk(osb->sb, first_blkno);
957 if (bhs[i] == NULL) {
958 status = -EIO;
959 mlog_errno(status);
960 goto bail;
961 }
Joel Becker42a5a7a2009-02-12 18:49:19 -0800962 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800963
Joel Becker42a5a7a2009-02-12 18:49:19 -0800964 status = ocfs2_journal_access_eb(handle, et->et_ci,
965 bhs[i],
Joel Becker13723d02008-10-17 19:25:01 -0700966 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800967 if (status < 0) {
968 mlog_errno(status);
969 goto bail;
970 }
971
972 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
973 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
974 /* Ok, setup the minimal stuff here. */
975 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
976 eb->h_blkno = cpu_to_le64(first_blkno);
977 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800978 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800979 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
980 eb->h_list.l_count =
981 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
982
983 suballoc_bit_start++;
984 first_blkno++;
985
986 /* We'll also be dirtied by the caller, so
987 * this isn't absolutely necessary. */
988 status = ocfs2_journal_dirty(handle, bhs[i]);
989 if (status < 0) {
990 mlog_errno(status);
991 goto bail;
992 }
993 }
994
995 count += num_got;
996 }
997
998 status = 0;
999bail:
1000 if (status < 0) {
1001 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001002 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001003 bhs[i] = NULL;
1004 }
1005 }
1006 mlog_exit(status);
1007 return status;
1008}
1009
1010/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001011 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1012 *
1013 * Returns the sum of the rightmost extent rec logical offset and
1014 * cluster count.
1015 *
1016 * ocfs2_add_branch() uses this to determine what logical cluster
1017 * value should be populated into the leftmost new branch records.
1018 *
1019 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1020 * value for the new topmost tree record.
1021 */
1022static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1023{
1024 int i;
1025
1026 i = le16_to_cpu(el->l_next_free_rec) - 1;
1027
1028 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001029 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08001030}
1031
1032/*
Tao Ma6b791bc2009-06-12 14:18:36 +08001033 * Change range of the branches in the right most path according to the leaf
1034 * extent block's rightmost record.
1035 */
1036static int ocfs2_adjust_rightmost_branch(handle_t *handle,
Tao Ma6b791bc2009-06-12 14:18:36 +08001037 struct ocfs2_extent_tree *et)
1038{
1039 int status;
1040 struct ocfs2_path *path = NULL;
1041 struct ocfs2_extent_list *el;
1042 struct ocfs2_extent_rec *rec;
1043
1044 path = ocfs2_new_path_from_et(et);
1045 if (!path) {
1046 status = -ENOMEM;
1047 return status;
1048 }
1049
Joel Beckerfacdb772009-02-12 18:08:48 -08001050 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
Tao Ma6b791bc2009-06-12 14:18:36 +08001051 if (status < 0) {
1052 mlog_errno(status);
1053 goto out;
1054 }
1055
1056 status = ocfs2_extend_trans(handle, path_num_items(path) +
1057 handle->h_buffer_credits);
1058 if (status < 0) {
1059 mlog_errno(status);
1060 goto out;
1061 }
1062
Joel Beckerd401dc12009-02-13 02:24:10 -08001063 status = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma6b791bc2009-06-12 14:18:36 +08001064 if (status < 0) {
1065 mlog_errno(status);
1066 goto out;
1067 }
1068
1069 el = path_leaf_el(path);
1070 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1071
Joel Beckerd401dc12009-02-13 02:24:10 -08001072 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Tao Ma6b791bc2009-06-12 14:18:36 +08001073
1074out:
1075 ocfs2_free_path(path);
1076 return status;
1077}
1078
1079/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001080 * Add an entire tree branch to our inode. eb_bh is the extent block
Joel Beckerd401dc12009-02-13 02:24:10 -08001081 * to start at, if we don't want to start the branch at the root
Mark Fashehccd979b2005-12-15 14:31:24 -08001082 * structure.
1083 *
1084 * last_eb_bh is required as we have to update it's next_leaf pointer
1085 * for the new last extent block.
1086 *
1087 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -08001088 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -08001089 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001090static int ocfs2_add_branch(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001091 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001092 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07001093 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001094 struct ocfs2_alloc_context *meta_ac)
1095{
1096 int status, new_blocks, i;
1097 u64 next_blkno, new_last_eb_blk;
1098 struct buffer_head *bh;
1099 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001100 struct ocfs2_extent_block *eb;
1101 struct ocfs2_extent_list *eb_el;
1102 struct ocfs2_extent_list *el;
Tao Ma6b791bc2009-06-12 14:18:36 +08001103 u32 new_cpos, root_end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001104
1105 mlog_entry_void();
1106
Mark Fasheh328d5752007-06-18 10:48:04 -07001107 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001108
Mark Fashehccd979b2005-12-15 14:31:24 -08001109 if (eb_bh) {
1110 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1111 el = &eb->h_list;
1112 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001113 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001114
1115 /* we never add a branch to a leaf. */
1116 BUG_ON(!el->l_tree_depth);
1117
1118 new_blocks = le16_to_cpu(el->l_tree_depth);
1119
Tao Ma6b791bc2009-06-12 14:18:36 +08001120 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1121 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1122 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1123
1124 /*
1125 * If there is a gap before the root end and the real end
1126 * of the righmost leaf block, we need to remove the gap
1127 * between new_cpos and root_end first so that the tree
1128 * is consistent after we add a new branch(it will start
1129 * from new_cpos).
1130 */
1131 if (root_end > new_cpos) {
1132 mlog(0, "adjust the cluster end from %u to %u\n",
1133 root_end, new_cpos);
Joel Beckerd401dc12009-02-13 02:24:10 -08001134 status = ocfs2_adjust_rightmost_branch(handle, et);
Tao Ma6b791bc2009-06-12 14:18:36 +08001135 if (status) {
1136 mlog_errno(status);
1137 goto bail;
1138 }
1139 }
1140
Mark Fashehccd979b2005-12-15 14:31:24 -08001141 /* allocate the number of new eb blocks we need */
1142 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1143 GFP_KERNEL);
1144 if (!new_eb_bhs) {
1145 status = -ENOMEM;
1146 mlog_errno(status);
1147 goto bail;
1148 }
1149
Joel Becker42a5a7a2009-02-12 18:49:19 -08001150 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
Mark Fashehccd979b2005-12-15 14:31:24 -08001151 meta_ac, new_eb_bhs);
1152 if (status < 0) {
1153 mlog_errno(status);
1154 goto bail;
1155 }
1156
1157 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1158 * linked with the rest of the tree.
1159 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1160 *
1161 * when we leave the loop, new_last_eb_blk will point to the
1162 * newest leaf, and next_blkno will point to the topmost extent
1163 * block. */
1164 next_blkno = new_last_eb_blk = 0;
1165 for(i = 0; i < new_blocks; i++) {
1166 bh = new_eb_bhs[i];
1167 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001168 /* ocfs2_create_new_meta_bhs() should create it right! */
1169 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001170 eb_el = &eb->h_list;
1171
Joel Beckerd401dc12009-02-13 02:24:10 -08001172 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
Joel Becker13723d02008-10-17 19:25:01 -07001173 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001174 if (status < 0) {
1175 mlog_errno(status);
1176 goto bail;
1177 }
1178
1179 eb->h_next_leaf_blk = 0;
1180 eb_el->l_tree_depth = cpu_to_le16(i);
1181 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001182 /*
1183 * This actually counts as an empty extent as
1184 * c_clusters == 0
1185 */
1186 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001187 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001188 /*
1189 * eb_el isn't always an interior node, but even leaf
1190 * nodes want a zero'd flags and reserved field so
1191 * this gets the whole 32 bits regardless of use.
1192 */
1193 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001194 if (!eb_el->l_tree_depth)
1195 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1196
1197 status = ocfs2_journal_dirty(handle, bh);
1198 if (status < 0) {
1199 mlog_errno(status);
1200 goto bail;
1201 }
1202
1203 next_blkno = le64_to_cpu(eb->h_blkno);
1204 }
1205
1206 /* This is a bit hairy. We want to update up to three blocks
1207 * here without leaving any of them in an inconsistent state
1208 * in case of error. We don't have to worry about
1209 * journal_dirty erroring as it won't unless we've aborted the
1210 * handle (in which case we would never be here) so reserving
1211 * the write with journal_access is all we need to do. */
Joel Beckerd401dc12009-02-13 02:24:10 -08001212 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001213 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001214 if (status < 0) {
1215 mlog_errno(status);
1216 goto bail;
1217 }
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001218 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001219 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001220 if (status < 0) {
1221 mlog_errno(status);
1222 goto bail;
1223 }
1224 if (eb_bh) {
Joel Beckerd401dc12009-02-13 02:24:10 -08001225 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001226 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001227 if (status < 0) {
1228 mlog_errno(status);
1229 goto bail;
1230 }
1231 }
1232
1233 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001234 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001235 i = le16_to_cpu(el->l_next_free_rec);
1236 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001237 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001238 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001239 le16_add_cpu(&el->l_next_free_rec, 1);
1240
1241 /* fe needs a new last extent block pointer, as does the
1242 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001243 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001244
Mark Fasheh328d5752007-06-18 10:48:04 -07001245 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001246 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1247
Mark Fasheh328d5752007-06-18 10:48:04 -07001248 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001249 if (status < 0)
1250 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001251 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001252 if (status < 0)
1253 mlog_errno(status);
1254 if (eb_bh) {
1255 status = ocfs2_journal_dirty(handle, eb_bh);
1256 if (status < 0)
1257 mlog_errno(status);
1258 }
1259
Mark Fasheh328d5752007-06-18 10:48:04 -07001260 /*
1261 * Some callers want to track the rightmost leaf so pass it
1262 * back here.
1263 */
1264 brelse(*last_eb_bh);
1265 get_bh(new_eb_bhs[0]);
1266 *last_eb_bh = new_eb_bhs[0];
1267
Mark Fashehccd979b2005-12-15 14:31:24 -08001268 status = 0;
1269bail:
1270 if (new_eb_bhs) {
1271 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001272 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001273 kfree(new_eb_bhs);
1274 }
1275
1276 mlog_exit(status);
1277 return status;
1278}
1279
1280/*
1281 * adds another level to the allocation tree.
1282 * returns back the new extent block so you can add a branch to it
1283 * after this call.
1284 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001285static int ocfs2_shift_tree_depth(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001286 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001287 struct ocfs2_alloc_context *meta_ac,
1288 struct buffer_head **ret_new_eb_bh)
1289{
1290 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001291 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001292 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001293 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001294 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001295 struct ocfs2_extent_list *eb_el;
1296
1297 mlog_entry_void();
1298
Joel Becker42a5a7a2009-02-12 18:49:19 -08001299 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
Mark Fashehccd979b2005-12-15 14:31:24 -08001300 &new_eb_bh);
1301 if (status < 0) {
1302 mlog_errno(status);
1303 goto bail;
1304 }
1305
1306 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001307 /* ocfs2_create_new_meta_bhs() should create it right! */
1308 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001309
1310 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001311 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001312
Joel Beckerd401dc12009-02-13 02:24:10 -08001313 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001314 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001315 if (status < 0) {
1316 mlog_errno(status);
1317 goto bail;
1318 }
1319
Tao Mae7d4cb62008-08-18 17:38:44 +08001320 /* copy the root extent list data into the new extent block */
1321 eb_el->l_tree_depth = root_el->l_tree_depth;
1322 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1323 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1324 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001325
1326 status = ocfs2_journal_dirty(handle, new_eb_bh);
1327 if (status < 0) {
1328 mlog_errno(status);
1329 goto bail;
1330 }
1331
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001332 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001333 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001334 if (status < 0) {
1335 mlog_errno(status);
1336 goto bail;
1337 }
1338
Mark Fashehdcd05382007-01-16 11:32:23 -08001339 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1340
Tao Mae7d4cb62008-08-18 17:38:44 +08001341 /* update root_bh now */
1342 le16_add_cpu(&root_el->l_tree_depth, 1);
1343 root_el->l_recs[0].e_cpos = 0;
1344 root_el->l_recs[0].e_blkno = eb->h_blkno;
1345 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1346 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1347 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1348 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001349
1350 /* If this is our 1st tree depth shift, then last_eb_blk
1351 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001352 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001353 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001354
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001355 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001356 if (status < 0) {
1357 mlog_errno(status);
1358 goto bail;
1359 }
1360
1361 *ret_new_eb_bh = new_eb_bh;
1362 new_eb_bh = NULL;
1363 status = 0;
1364bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001365 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001366
1367 mlog_exit(status);
1368 return status;
1369}
1370
1371/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001372 * Should only be called when there is no space left in any of the
1373 * leaf nodes. What we want to do is find the lowest tree depth
1374 * non-leaf extent block with room for new records. There are three
1375 * valid results of this search:
1376 *
1377 * 1) a lowest extent block is found, then we pass it back in
1378 * *lowest_eb_bh and return '0'
1379 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001380 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001381 * pass NULL back in *lowest_eb_bh, but still return '0'
1382 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001383 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001384 * which case we return > 0
1385 *
1386 * return status < 0 indicates an error.
1387 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001388static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001389 struct buffer_head **target_bh)
1390{
1391 int status = 0, i;
1392 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001393 struct ocfs2_extent_block *eb;
1394 struct ocfs2_extent_list *el;
1395 struct buffer_head *bh = NULL;
1396 struct buffer_head *lowest_bh = NULL;
1397
1398 mlog_entry_void();
1399
1400 *target_bh = NULL;
1401
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001402 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001403
1404 while(le16_to_cpu(el->l_tree_depth) > 1) {
1405 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08001406 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1407 "Owner %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001408 "extent list (next_free_rec == 0)",
Joel Becker3d03a302009-02-12 17:49:26 -08001409 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001410 status = -EIO;
1411 goto bail;
1412 }
1413 i = le16_to_cpu(el->l_next_free_rec) - 1;
1414 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1415 if (!blkno) {
Joel Becker3d03a302009-02-12 17:49:26 -08001416 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1417 "Owner %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001418 "list where extent # %d has no physical "
1419 "block start",
Joel Becker3d03a302009-02-12 17:49:26 -08001420 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001421 status = -EIO;
1422 goto bail;
1423 }
1424
Mark Fasheha81cb882008-10-07 14:25:16 -07001425 brelse(bh);
1426 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001427
Joel Becker3d03a302009-02-12 17:49:26 -08001428 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001429 if (status < 0) {
1430 mlog_errno(status);
1431 goto bail;
1432 }
1433
1434 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001435 el = &eb->h_list;
1436
1437 if (le16_to_cpu(el->l_next_free_rec) <
1438 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001439 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001440 lowest_bh = bh;
1441 get_bh(lowest_bh);
1442 }
1443 }
1444
1445 /* If we didn't find one and the fe doesn't have any room,
1446 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001447 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001448 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001449 status = 1;
1450
1451 *target_bh = lowest_bh;
1452bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001453 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001454
1455 mlog_exit(status);
1456 return status;
1457}
1458
Mark Fashehe48edee2007-03-07 16:46:57 -08001459/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001460 * Grow a b-tree so that it has more records.
1461 *
1462 * We might shift the tree depth in which case existing paths should
1463 * be considered invalid.
1464 *
1465 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001466 *
1467 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001468 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001469static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1470 int *final_depth, struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001471 struct ocfs2_alloc_context *meta_ac)
1472{
1473 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001474 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001475 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001476 struct buffer_head *bh = NULL;
1477
1478 BUG_ON(meta_ac == NULL);
1479
Joel Beckerd401dc12009-02-13 02:24:10 -08001480 shift = ocfs2_find_branch_target(et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001481 if (shift < 0) {
1482 ret = shift;
1483 mlog_errno(ret);
1484 goto out;
1485 }
1486
1487 /* We traveled all the way to the bottom of the allocation tree
1488 * and didn't find room for any more extents - we need to add
1489 * another tree level */
1490 if (shift) {
1491 BUG_ON(bh);
1492 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1493
1494 /* ocfs2_shift_tree_depth will return us a buffer with
1495 * the new extent block (so we can pass that to
1496 * ocfs2_add_branch). */
Joel Beckerd401dc12009-02-13 02:24:10 -08001497 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001498 if (ret < 0) {
1499 mlog_errno(ret);
1500 goto out;
1501 }
1502 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001503 if (depth == 1) {
1504 /*
1505 * Special case: we have room now if we shifted from
1506 * tree_depth 0, so no more work needs to be done.
1507 *
1508 * We won't be calling add_branch, so pass
1509 * back *last_eb_bh as the new leaf. At depth
1510 * zero, it should always be null so there's
1511 * no reason to brelse.
1512 */
1513 BUG_ON(*last_eb_bh);
1514 get_bh(bh);
1515 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001516 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001517 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001518 }
1519
1520 /* call ocfs2_add_branch to add the final part of the tree with
1521 * the new data. */
1522 mlog(0, "add branch. bh = %p\n", bh);
Joel Beckerd401dc12009-02-13 02:24:10 -08001523 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001524 meta_ac);
1525 if (ret < 0) {
1526 mlog_errno(ret);
1527 goto out;
1528 }
1529
1530out:
1531 if (final_depth)
1532 *final_depth = depth;
1533 brelse(bh);
1534 return ret;
1535}
1536
1537/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001538 * This function will discard the rightmost extent record.
1539 */
1540static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1541{
1542 int next_free = le16_to_cpu(el->l_next_free_rec);
1543 int count = le16_to_cpu(el->l_count);
1544 unsigned int num_bytes;
1545
1546 BUG_ON(!next_free);
1547 /* This will cause us to go off the end of our extent list. */
1548 BUG_ON(next_free >= count);
1549
1550 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1551
1552 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1553}
1554
1555static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1556 struct ocfs2_extent_rec *insert_rec)
1557{
1558 int i, insert_index, next_free, has_empty, num_bytes;
1559 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1560 struct ocfs2_extent_rec *rec;
1561
1562 next_free = le16_to_cpu(el->l_next_free_rec);
1563 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1564
1565 BUG_ON(!next_free);
1566
1567 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001568 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001569
1570 /*
1571 * The easiest way to approach this is to just remove the
1572 * empty extent and temporarily decrement next_free.
1573 */
1574 if (has_empty) {
1575 /*
1576 * If next_free was 1 (only an empty extent), this
1577 * loop won't execute, which is fine. We still want
1578 * the decrement above to happen.
1579 */
1580 for(i = 0; i < (next_free - 1); i++)
1581 el->l_recs[i] = el->l_recs[i+1];
1582
1583 next_free--;
1584 }
1585
1586 /*
1587 * Figure out what the new record index should be.
1588 */
1589 for(i = 0; i < next_free; i++) {
1590 rec = &el->l_recs[i];
1591
1592 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1593 break;
1594 }
1595 insert_index = i;
1596
1597 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1598 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1599
1600 BUG_ON(insert_index < 0);
1601 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1602 BUG_ON(insert_index > next_free);
1603
1604 /*
1605 * No need to memmove if we're just adding to the tail.
1606 */
1607 if (insert_index != next_free) {
1608 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1609
1610 num_bytes = next_free - insert_index;
1611 num_bytes *= sizeof(struct ocfs2_extent_rec);
1612 memmove(&el->l_recs[insert_index + 1],
1613 &el->l_recs[insert_index],
1614 num_bytes);
1615 }
1616
1617 /*
1618 * Either we had an empty extent, and need to re-increment or
1619 * there was no empty extent on a non full rightmost leaf node,
1620 * in which case we still need to increment.
1621 */
1622 next_free++;
1623 el->l_next_free_rec = cpu_to_le16(next_free);
1624 /*
1625 * Make sure none of the math above just messed up our tree.
1626 */
1627 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1628
1629 el->l_recs[insert_index] = *insert_rec;
1630
1631}
1632
Mark Fasheh328d5752007-06-18 10:48:04 -07001633static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1634{
1635 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1636
1637 BUG_ON(num_recs == 0);
1638
1639 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1640 num_recs--;
1641 size = num_recs * sizeof(struct ocfs2_extent_rec);
1642 memmove(&el->l_recs[0], &el->l_recs[1], size);
1643 memset(&el->l_recs[num_recs], 0,
1644 sizeof(struct ocfs2_extent_rec));
1645 el->l_next_free_rec = cpu_to_le16(num_recs);
1646 }
1647}
1648
Mark Fashehdcd05382007-01-16 11:32:23 -08001649/*
1650 * Create an empty extent record .
1651 *
1652 * l_next_free_rec may be updated.
1653 *
1654 * If an empty extent already exists do nothing.
1655 */
1656static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1657{
1658 int next_free = le16_to_cpu(el->l_next_free_rec);
1659
Mark Fashehe48edee2007-03-07 16:46:57 -08001660 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1661
Mark Fashehdcd05382007-01-16 11:32:23 -08001662 if (next_free == 0)
1663 goto set_and_inc;
1664
1665 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1666 return;
1667
1668 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1669 "Asked to create an empty extent in a full list:\n"
1670 "count = %u, tree depth = %u",
1671 le16_to_cpu(el->l_count),
1672 le16_to_cpu(el->l_tree_depth));
1673
1674 ocfs2_shift_records_right(el);
1675
1676set_and_inc:
1677 le16_add_cpu(&el->l_next_free_rec, 1);
1678 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1679}
1680
1681/*
1682 * For a rotation which involves two leaf nodes, the "root node" is
1683 * the lowest level tree node which contains a path to both leafs. This
1684 * resulting set of information can be used to form a complete "subtree"
1685 *
1686 * This function is passed two full paths from the dinode down to a
1687 * pair of adjacent leaves. It's task is to figure out which path
1688 * index contains the subtree root - this can be the root index itself
1689 * in a worst-case rotation.
1690 *
1691 * The array index of the subtree root is passed back.
1692 */
Joel Becker7dc02802009-02-12 19:20:13 -08001693static int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08001694 struct ocfs2_path *left,
1695 struct ocfs2_path *right)
1696{
1697 int i = 0;
1698
1699 /*
1700 * Check that the caller passed in two paths from the same tree.
1701 */
1702 BUG_ON(path_root_bh(left) != path_root_bh(right));
1703
1704 do {
1705 i++;
1706
1707 /*
1708 * The caller didn't pass two adjacent paths.
1709 */
1710 mlog_bug_on_msg(i > left->p_tree_depth,
Joel Becker7dc02802009-02-12 19:20:13 -08001711 "Owner %llu, left depth %u, right depth %u\n"
Mark Fashehdcd05382007-01-16 11:32:23 -08001712 "left leaf blk %llu, right leaf blk %llu\n",
Joel Becker7dc02802009-02-12 19:20:13 -08001713 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1714 left->p_tree_depth, right->p_tree_depth,
Mark Fashehdcd05382007-01-16 11:32:23 -08001715 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1716 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1717 } while (left->p_node[i].bh->b_blocknr ==
1718 right->p_node[i].bh->b_blocknr);
1719
1720 return i - 1;
1721}
1722
1723typedef void (path_insert_t)(void *, struct buffer_head *);
1724
1725/*
1726 * Traverse a btree path in search of cpos, starting at root_el.
1727 *
1728 * This code can be called with a cpos larger than the tree, in which
1729 * case it will return the rightmost path.
1730 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001731static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
Mark Fashehdcd05382007-01-16 11:32:23 -08001732 struct ocfs2_extent_list *root_el, u32 cpos,
1733 path_insert_t *func, void *data)
1734{
1735 int i, ret = 0;
1736 u32 range;
1737 u64 blkno;
1738 struct buffer_head *bh = NULL;
1739 struct ocfs2_extent_block *eb;
1740 struct ocfs2_extent_list *el;
1741 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08001742
1743 el = root_el;
1744 while (el->l_tree_depth) {
1745 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001746 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1747 "Owner %llu has empty extent list at "
Mark Fashehdcd05382007-01-16 11:32:23 -08001748 "depth %u\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001749 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001750 le16_to_cpu(el->l_tree_depth));
1751 ret = -EROFS;
1752 goto out;
1753
1754 }
1755
1756 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1757 rec = &el->l_recs[i];
1758
1759 /*
1760 * In the case that cpos is off the allocation
1761 * tree, this should just wind up returning the
1762 * rightmost record.
1763 */
1764 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001765 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001766 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1767 break;
1768 }
1769
1770 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1771 if (blkno == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001772 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1773 "Owner %llu has bad blkno in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001774 "at depth %u (index %d)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001775 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001776 le16_to_cpu(el->l_tree_depth), i);
1777 ret = -EROFS;
1778 goto out;
1779 }
1780
1781 brelse(bh);
1782 bh = NULL;
Joel Beckerfacdb772009-02-12 18:08:48 -08001783 ret = ocfs2_read_extent_block(ci, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001784 if (ret) {
1785 mlog_errno(ret);
1786 goto out;
1787 }
1788
1789 eb = (struct ocfs2_extent_block *) bh->b_data;
1790 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001791
1792 if (le16_to_cpu(el->l_next_free_rec) >
1793 le16_to_cpu(el->l_count)) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001794 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1795 "Owner %llu has bad count in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001796 "at block %llu (next free=%u, count=%u)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001797 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001798 (unsigned long long)bh->b_blocknr,
1799 le16_to_cpu(el->l_next_free_rec),
1800 le16_to_cpu(el->l_count));
1801 ret = -EROFS;
1802 goto out;
1803 }
1804
1805 if (func)
1806 func(data, bh);
1807 }
1808
1809out:
1810 /*
1811 * Catch any trailing bh that the loop didn't handle.
1812 */
1813 brelse(bh);
1814
1815 return ret;
1816}
1817
1818/*
1819 * Given an initialized path (that is, it has a valid root extent
1820 * list), this function will traverse the btree in search of the path
1821 * which would contain cpos.
1822 *
1823 * The path traveled is recorded in the path structure.
1824 *
1825 * Note that this will not do any comparisons on leaf node extent
1826 * records, so it will work fine in the case that we just added a tree
1827 * branch.
1828 */
1829struct find_path_data {
1830 int index;
1831 struct ocfs2_path *path;
1832};
1833static void find_path_ins(void *data, struct buffer_head *bh)
1834{
1835 struct find_path_data *fp = data;
1836
1837 get_bh(bh);
1838 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1839 fp->index++;
1840}
Joel Beckerfacdb772009-02-12 18:08:48 -08001841static int ocfs2_find_path(struct ocfs2_caching_info *ci,
1842 struct ocfs2_path *path, u32 cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08001843{
1844 struct find_path_data data;
1845
1846 data.index = 1;
1847 data.path = path;
Joel Beckerfacdb772009-02-12 18:08:48 -08001848 return __ocfs2_find_path(ci, path_root_el(path), cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08001849 find_path_ins, &data);
1850}
1851
1852static void find_leaf_ins(void *data, struct buffer_head *bh)
1853{
1854 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1855 struct ocfs2_extent_list *el = &eb->h_list;
1856 struct buffer_head **ret = data;
1857
1858 /* We want to retain only the leaf block. */
1859 if (le16_to_cpu(el->l_tree_depth) == 0) {
1860 get_bh(bh);
1861 *ret = bh;
1862 }
1863}
1864/*
1865 * Find the leaf block in the tree which would contain cpos. No
1866 * checking of the actual leaf is done.
1867 *
1868 * Some paths want to call this instead of allocating a path structure
1869 * and calling ocfs2_find_path().
1870 *
1871 * This function doesn't handle non btree extent lists.
1872 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001873int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1874 struct ocfs2_extent_list *root_el, u32 cpos,
1875 struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001876{
1877 int ret;
1878 struct buffer_head *bh = NULL;
1879
Joel Beckerfacdb772009-02-12 18:08:48 -08001880 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001881 if (ret) {
1882 mlog_errno(ret);
1883 goto out;
1884 }
1885
1886 *leaf_bh = bh;
1887out:
1888 return ret;
1889}
1890
1891/*
1892 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1893 *
1894 * Basically, we've moved stuff around at the bottom of the tree and
1895 * we need to fix up the extent records above the changes to reflect
1896 * the new changes.
1897 *
1898 * left_rec: the record on the left.
1899 * left_child_el: is the child list pointed to by left_rec
1900 * right_rec: the record to the right of left_rec
1901 * right_child_el: is the child list pointed to by right_rec
1902 *
1903 * By definition, this only works on interior nodes.
1904 */
1905static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1906 struct ocfs2_extent_list *left_child_el,
1907 struct ocfs2_extent_rec *right_rec,
1908 struct ocfs2_extent_list *right_child_el)
1909{
1910 u32 left_clusters, right_end;
1911
1912 /*
1913 * Interior nodes never have holes. Their cpos is the cpos of
1914 * the leftmost record in their child list. Their cluster
1915 * count covers the full theoretical range of their child list
1916 * - the range between their cpos and the cpos of the record
1917 * immediately to their right.
1918 */
1919 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08001920 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1921 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07001922 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1923 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1924 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001925 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001926 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001927
1928 /*
1929 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001930 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001931 * updating e_cpos to keep the same highest cluster count.
1932 */
1933 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001934 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001935
1936 right_rec->e_cpos = left_rec->e_cpos;
1937 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1938
1939 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001940 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001941}
1942
1943/*
1944 * Adjust the adjacent root node records involved in a
1945 * rotation. left_el_blkno is passed in as a key so that we can easily
1946 * find it's index in the root list.
1947 */
1948static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1949 struct ocfs2_extent_list *left_el,
1950 struct ocfs2_extent_list *right_el,
1951 u64 left_el_blkno)
1952{
1953 int i;
1954
1955 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1956 le16_to_cpu(left_el->l_tree_depth));
1957
1958 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1959 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1960 break;
1961 }
1962
1963 /*
1964 * The path walking code should have never returned a root and
1965 * two paths which are not adjacent.
1966 */
1967 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1968
1969 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1970 &root_el->l_recs[i + 1], right_el);
1971}
1972
1973/*
1974 * We've changed a leaf block (in right_path) and need to reflect that
1975 * change back up the subtree.
1976 *
1977 * This happens in multiple places:
1978 * - When we've moved an extent record from the left path leaf to the right
1979 * path leaf to make room for an empty extent in the left path leaf.
1980 * - When our insert into the right path leaf is at the leftmost edge
1981 * and requires an update of the path immediately to it's left. This
1982 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08001983 * - When we've adjusted the last extent record in the left path leaf and the
1984 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08001985 */
Joel Becker4619c732009-02-12 19:02:36 -08001986static void ocfs2_complete_edge_insert(handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08001987 struct ocfs2_path *left_path,
1988 struct ocfs2_path *right_path,
1989 int subtree_index)
1990{
1991 int ret, i, idx;
1992 struct ocfs2_extent_list *el, *left_el, *right_el;
1993 struct ocfs2_extent_rec *left_rec, *right_rec;
1994 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1995
1996 /*
1997 * Update the counts and position values within all the
1998 * interior nodes to reflect the leaf rotation we just did.
1999 *
2000 * The root node is handled below the loop.
2001 *
2002 * We begin the loop with right_el and left_el pointing to the
2003 * leaf lists and work our way up.
2004 *
2005 * NOTE: within this loop, left_el and right_el always refer
2006 * to the *child* lists.
2007 */
2008 left_el = path_leaf_el(left_path);
2009 right_el = path_leaf_el(right_path);
2010 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2011 mlog(0, "Adjust records at index %u\n", i);
2012
2013 /*
2014 * One nice property of knowing that all of these
2015 * nodes are below the root is that we only deal with
2016 * the leftmost right node record and the rightmost
2017 * left node record.
2018 */
2019 el = left_path->p_node[i].el;
2020 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2021 left_rec = &el->l_recs[idx];
2022
2023 el = right_path->p_node[i].el;
2024 right_rec = &el->l_recs[0];
2025
2026 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2027 right_el);
2028
2029 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2030 if (ret)
2031 mlog_errno(ret);
2032
2033 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2034 if (ret)
2035 mlog_errno(ret);
2036
2037 /*
2038 * Setup our list pointers now so that the current
2039 * parents become children in the next iteration.
2040 */
2041 left_el = left_path->p_node[i].el;
2042 right_el = right_path->p_node[i].el;
2043 }
2044
2045 /*
2046 * At the root node, adjust the two adjacent records which
2047 * begin our path to the leaves.
2048 */
2049
2050 el = left_path->p_node[subtree_index].el;
2051 left_el = left_path->p_node[subtree_index + 1].el;
2052 right_el = right_path->p_node[subtree_index + 1].el;
2053
2054 ocfs2_adjust_root_records(el, left_el, right_el,
2055 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2056
2057 root_bh = left_path->p_node[subtree_index].bh;
2058
2059 ret = ocfs2_journal_dirty(handle, root_bh);
2060 if (ret)
2061 mlog_errno(ret);
2062}
2063
Joel Becker5c601ab2009-02-12 19:10:13 -08002064static int ocfs2_rotate_subtree_right(handle_t *handle,
2065 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08002066 struct ocfs2_path *left_path,
2067 struct ocfs2_path *right_path,
2068 int subtree_index)
2069{
2070 int ret, i;
2071 struct buffer_head *right_leaf_bh;
2072 struct buffer_head *left_leaf_bh = NULL;
2073 struct buffer_head *root_bh;
2074 struct ocfs2_extent_list *right_el, *left_el;
2075 struct ocfs2_extent_rec move_rec;
2076
2077 left_leaf_bh = path_leaf_bh(left_path);
2078 left_el = path_leaf_el(left_path);
2079
2080 if (left_el->l_next_free_rec != left_el->l_count) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002081 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002082 "Inode %llu has non-full interior leaf node %llu"
2083 "(next free = %u)",
Joel Becker5c601ab2009-02-12 19:10:13 -08002084 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002085 (unsigned long long)left_leaf_bh->b_blocknr,
2086 le16_to_cpu(left_el->l_next_free_rec));
2087 return -EROFS;
2088 }
2089
2090 /*
2091 * This extent block may already have an empty record, so we
2092 * return early if so.
2093 */
2094 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2095 return 0;
2096
2097 root_bh = left_path->p_node[subtree_index].bh;
2098 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2099
Joel Becker5c601ab2009-02-12 19:10:13 -08002100 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002101 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002102 if (ret) {
2103 mlog_errno(ret);
2104 goto out;
2105 }
2106
2107 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002108 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002109 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002110 if (ret) {
2111 mlog_errno(ret);
2112 goto out;
2113 }
2114
Joel Becker5c601ab2009-02-12 19:10:13 -08002115 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002116 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002117 if (ret) {
2118 mlog_errno(ret);
2119 goto out;
2120 }
2121 }
2122
2123 right_leaf_bh = path_leaf_bh(right_path);
2124 right_el = path_leaf_el(right_path);
2125
2126 /* This is a code error, not a disk corruption. */
2127 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2128 "because rightmost leaf block %llu is empty\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002129 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002130 (unsigned long long)right_leaf_bh->b_blocknr);
2131
2132 ocfs2_create_empty_extent(right_el);
2133
2134 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2135 if (ret) {
2136 mlog_errno(ret);
2137 goto out;
2138 }
2139
2140 /* Do the copy now. */
2141 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2142 move_rec = left_el->l_recs[i];
2143 right_el->l_recs[0] = move_rec;
2144
2145 /*
2146 * Clear out the record we just copied and shift everything
2147 * over, leaving an empty extent in the left leaf.
2148 *
2149 * We temporarily subtract from next_free_rec so that the
2150 * shift will lose the tail record (which is now defunct).
2151 */
2152 le16_add_cpu(&left_el->l_next_free_rec, -1);
2153 ocfs2_shift_records_right(left_el);
2154 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2155 le16_add_cpu(&left_el->l_next_free_rec, 1);
2156
2157 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2158 if (ret) {
2159 mlog_errno(ret);
2160 goto out;
2161 }
2162
Joel Becker4619c732009-02-12 19:02:36 -08002163 ocfs2_complete_edge_insert(handle, left_path, right_path,
2164 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002165
2166out:
2167 return ret;
2168}
2169
2170/*
2171 * Given a full path, determine what cpos value would return us a path
2172 * containing the leaf immediately to the left of the current one.
2173 *
2174 * Will return zero if the path passed in is already the leftmost path.
2175 */
2176static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2177 struct ocfs2_path *path, u32 *cpos)
2178{
2179 int i, j, ret = 0;
2180 u64 blkno;
2181 struct ocfs2_extent_list *el;
2182
Mark Fashehe48edee2007-03-07 16:46:57 -08002183 BUG_ON(path->p_tree_depth == 0);
2184
Mark Fashehdcd05382007-01-16 11:32:23 -08002185 *cpos = 0;
2186
2187 blkno = path_leaf_bh(path)->b_blocknr;
2188
2189 /* Start at the tree node just above the leaf and work our way up. */
2190 i = path->p_tree_depth - 1;
2191 while (i >= 0) {
2192 el = path->p_node[i].el;
2193
2194 /*
2195 * Find the extent record just before the one in our
2196 * path.
2197 */
2198 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2199 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2200 if (j == 0) {
2201 if (i == 0) {
2202 /*
2203 * We've determined that the
2204 * path specified is already
2205 * the leftmost one - return a
2206 * cpos of zero.
2207 */
2208 goto out;
2209 }
2210 /*
2211 * The leftmost record points to our
2212 * leaf - we need to travel up the
2213 * tree one level.
2214 */
2215 goto next_node;
2216 }
2217
2218 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002219 *cpos = *cpos + ocfs2_rec_clusters(el,
2220 &el->l_recs[j - 1]);
2221 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002222 goto out;
2223 }
2224 }
2225
2226 /*
2227 * If we got here, we never found a valid node where
2228 * the tree indicated one should be.
2229 */
2230 ocfs2_error(sb,
2231 "Invalid extent tree at extent block %llu\n",
2232 (unsigned long long)blkno);
2233 ret = -EROFS;
2234 goto out;
2235
2236next_node:
2237 blkno = path->p_node[i].bh->b_blocknr;
2238 i--;
2239 }
2240
2241out:
2242 return ret;
2243}
2244
Mark Fasheh328d5752007-06-18 10:48:04 -07002245/*
2246 * Extend the transaction by enough credits to complete the rotation,
2247 * and still leave at least the original number of credits allocated
2248 * to this transaction.
2249 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002250static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002251 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002252 struct ocfs2_path *path)
2253{
Mark Fasheh328d5752007-06-18 10:48:04 -07002254 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002255
2256 if (handle->h_buffer_credits < credits)
2257 return ocfs2_extend_trans(handle, credits);
2258
2259 return 0;
2260}
2261
2262/*
2263 * Trap the case where we're inserting into the theoretical range past
2264 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2265 * whose cpos is less than ours into the right leaf.
2266 *
2267 * It's only necessary to look at the rightmost record of the left
2268 * leaf because the logic that calls us should ensure that the
2269 * theoretical ranges in the path components above the leaves are
2270 * correct.
2271 */
2272static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2273 u32 insert_cpos)
2274{
2275 struct ocfs2_extent_list *left_el;
2276 struct ocfs2_extent_rec *rec;
2277 int next_free;
2278
2279 left_el = path_leaf_el(left_path);
2280 next_free = le16_to_cpu(left_el->l_next_free_rec);
2281 rec = &left_el->l_recs[next_free - 1];
2282
2283 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2284 return 1;
2285 return 0;
2286}
2287
Mark Fasheh328d5752007-06-18 10:48:04 -07002288static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2289{
2290 int next_free = le16_to_cpu(el->l_next_free_rec);
2291 unsigned int range;
2292 struct ocfs2_extent_rec *rec;
2293
2294 if (next_free == 0)
2295 return 0;
2296
2297 rec = &el->l_recs[0];
2298 if (ocfs2_is_empty_extent(rec)) {
2299 /* Empty list. */
2300 if (next_free == 1)
2301 return 0;
2302 rec = &el->l_recs[1];
2303 }
2304
2305 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2306 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2307 return 1;
2308 return 0;
2309}
2310
Mark Fashehdcd05382007-01-16 11:32:23 -08002311/*
2312 * Rotate all the records in a btree right one record, starting at insert_cpos.
2313 *
2314 * The path to the rightmost leaf should be passed in.
2315 *
2316 * The array is assumed to be large enough to hold an entire path (tree depth).
2317 *
2318 * Upon succesful return from this function:
2319 *
2320 * - The 'right_path' array will contain a path to the leaf block
2321 * whose range contains e_cpos.
2322 * - That leaf block will have a single empty extent in list index 0.
2323 * - In the case that the rotation requires a post-insert update,
2324 * *ret_left_path will contain a valid path which can be passed to
2325 * ocfs2_insert_path().
2326 */
Joel Becker1bbf0b82009-02-12 19:42:08 -08002327static int ocfs2_rotate_tree_right(handle_t *handle,
Joel Becker5c601ab2009-02-12 19:10:13 -08002328 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002329 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002330 u32 insert_cpos,
2331 struct ocfs2_path *right_path,
2332 struct ocfs2_path **ret_left_path)
2333{
Mark Fasheh328d5752007-06-18 10:48:04 -07002334 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002335 u32 cpos;
2336 struct ocfs2_path *left_path = NULL;
Joel Becker5c601ab2009-02-12 19:10:13 -08002337 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehdcd05382007-01-16 11:32:23 -08002338
2339 *ret_left_path = NULL;
2340
Joel Beckerffdd7a52008-10-17 22:32:01 -07002341 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002342 if (!left_path) {
2343 ret = -ENOMEM;
2344 mlog_errno(ret);
2345 goto out;
2346 }
2347
Joel Becker5c601ab2009-02-12 19:10:13 -08002348 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002349 if (ret) {
2350 mlog_errno(ret);
2351 goto out;
2352 }
2353
2354 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2355
2356 /*
2357 * What we want to do here is:
2358 *
2359 * 1) Start with the rightmost path.
2360 *
2361 * 2) Determine a path to the leaf block directly to the left
2362 * of that leaf.
2363 *
2364 * 3) Determine the 'subtree root' - the lowest level tree node
2365 * which contains a path to both leaves.
2366 *
2367 * 4) Rotate the subtree.
2368 *
2369 * 5) Find the next subtree by considering the left path to be
2370 * the new right path.
2371 *
2372 * The check at the top of this while loop also accepts
2373 * insert_cpos == cpos because cpos is only a _theoretical_
2374 * value to get us the left path - insert_cpos might very well
2375 * be filling that hole.
2376 *
2377 * Stop at a cpos of '0' because we either started at the
2378 * leftmost branch (i.e., a tree with one branch and a
2379 * rotation inside of it), or we've gone as far as we can in
2380 * rotating subtrees.
2381 */
2382 while (cpos && insert_cpos <= cpos) {
2383 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2384 insert_cpos, cpos);
2385
Joel Becker5c601ab2009-02-12 19:10:13 -08002386 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002387 if (ret) {
2388 mlog_errno(ret);
2389 goto out;
2390 }
2391
2392 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2393 path_leaf_bh(right_path),
Joel Becker5c601ab2009-02-12 19:10:13 -08002394 "Owner %llu: error during insert of %u "
Mark Fashehdcd05382007-01-16 11:32:23 -08002395 "(left path cpos %u) results in two identical "
2396 "paths ending at %llu\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002397 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2398 insert_cpos, cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08002399 (unsigned long long)
2400 path_leaf_bh(left_path)->b_blocknr);
2401
Mark Fasheh328d5752007-06-18 10:48:04 -07002402 if (split == SPLIT_NONE &&
2403 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002404 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002405
2406 /*
2407 * We've rotated the tree as much as we
2408 * should. The rest is up to
2409 * ocfs2_insert_path() to complete, after the
2410 * record insertion. We indicate this
2411 * situation by returning the left path.
2412 *
2413 * The reason we don't adjust the records here
2414 * before the record insert is that an error
2415 * later might break the rule where a parent
2416 * record e_cpos will reflect the actual
2417 * e_cpos of the 1st nonempty record of the
2418 * child list.
2419 */
2420 *ret_left_path = left_path;
2421 goto out_ret_path;
2422 }
2423
Joel Becker7dc02802009-02-12 19:20:13 -08002424 start = ocfs2_find_subtree_root(et, left_path, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002425
2426 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2427 start,
2428 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2429 right_path->p_tree_depth);
2430
2431 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002432 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002433 if (ret) {
2434 mlog_errno(ret);
2435 goto out;
2436 }
2437
Joel Becker5c601ab2009-02-12 19:10:13 -08002438 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002439 right_path, start);
2440 if (ret) {
2441 mlog_errno(ret);
2442 goto out;
2443 }
2444
Mark Fasheh328d5752007-06-18 10:48:04 -07002445 if (split != SPLIT_NONE &&
2446 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2447 insert_cpos)) {
2448 /*
2449 * A rotate moves the rightmost left leaf
2450 * record over to the leftmost right leaf
2451 * slot. If we're doing an extent split
2452 * instead of a real insert, then we have to
2453 * check that the extent to be split wasn't
2454 * just moved over. If it was, then we can
2455 * exit here, passing left_path back -
2456 * ocfs2_split_extent() is smart enough to
2457 * search both leaves.
2458 */
2459 *ret_left_path = left_path;
2460 goto out_ret_path;
2461 }
2462
Mark Fashehdcd05382007-01-16 11:32:23 -08002463 /*
2464 * There is no need to re-read the next right path
2465 * as we know that it'll be our current left
2466 * path. Optimize by copying values instead.
2467 */
2468 ocfs2_mv_path(right_path, left_path);
2469
Joel Becker5c601ab2009-02-12 19:10:13 -08002470 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002471 if (ret) {
2472 mlog_errno(ret);
2473 goto out;
2474 }
2475 }
2476
2477out:
2478 ocfs2_free_path(left_path);
2479
2480out_ret_path:
2481 return ret;
2482}
2483
Joel Becker09106ba2009-02-12 19:43:57 -08002484static int ocfs2_update_edge_lengths(handle_t *handle,
2485 struct ocfs2_extent_tree *et,
Tao Ma3c5e1062009-07-21 15:42:05 +08002486 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002487{
Tao Ma3c5e1062009-07-21 15:42:05 +08002488 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002489 struct ocfs2_extent_rec *rec;
2490 struct ocfs2_extent_list *el;
2491 struct ocfs2_extent_block *eb;
2492 u32 range;
2493
Tao Ma3c5e1062009-07-21 15:42:05 +08002494 /*
2495 * In normal tree rotation process, we will never touch the
2496 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2497 * doesn't reserve the credits for them either.
2498 *
2499 * But we do have a special case here which will update the rightmost
2500 * records for all the bh in the path.
2501 * So we have to allocate extra credits and access them.
2502 */
2503 ret = ocfs2_extend_trans(handle,
2504 handle->h_buffer_credits + subtree_index);
2505 if (ret) {
2506 mlog_errno(ret);
2507 goto out;
2508 }
2509
Joel Becker09106ba2009-02-12 19:43:57 -08002510 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002511 if (ret) {
2512 mlog_errno(ret);
2513 goto out;
2514 }
2515
Mark Fasheh328d5752007-06-18 10:48:04 -07002516 /* Path should always be rightmost. */
2517 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2518 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2519
2520 el = &eb->h_list;
2521 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2522 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2523 rec = &el->l_recs[idx];
2524 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2525
2526 for (i = 0; i < path->p_tree_depth; i++) {
2527 el = path->p_node[i].el;
2528 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2529 rec = &el->l_recs[idx];
2530
2531 rec->e_int_clusters = cpu_to_le32(range);
2532 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2533
2534 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2535 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002536out:
2537 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002538}
2539
Joel Becker6641b0c2009-02-12 18:57:52 -08002540static void ocfs2_unlink_path(handle_t *handle,
2541 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002542 struct ocfs2_cached_dealloc_ctxt *dealloc,
2543 struct ocfs2_path *path, int unlink_start)
2544{
2545 int ret, i;
2546 struct ocfs2_extent_block *eb;
2547 struct ocfs2_extent_list *el;
2548 struct buffer_head *bh;
2549
2550 for(i = unlink_start; i < path_num_items(path); i++) {
2551 bh = path->p_node[i].bh;
2552
2553 eb = (struct ocfs2_extent_block *)bh->b_data;
2554 /*
2555 * Not all nodes might have had their final count
2556 * decremented by the caller - handle this here.
2557 */
2558 el = &eb->h_list;
2559 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2560 mlog(ML_ERROR,
2561 "Inode %llu, attempted to remove extent block "
2562 "%llu with %u records\n",
Joel Becker6641b0c2009-02-12 18:57:52 -08002563 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07002564 (unsigned long long)le64_to_cpu(eb->h_blkno),
2565 le16_to_cpu(el->l_next_free_rec));
2566
2567 ocfs2_journal_dirty(handle, bh);
Joel Becker6641b0c2009-02-12 18:57:52 -08002568 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002569 continue;
2570 }
2571
2572 el->l_next_free_rec = 0;
2573 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2574
2575 ocfs2_journal_dirty(handle, bh);
2576
2577 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2578 if (ret)
2579 mlog_errno(ret);
2580
Joel Becker6641b0c2009-02-12 18:57:52 -08002581 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002582 }
2583}
2584
Joel Becker6641b0c2009-02-12 18:57:52 -08002585static void ocfs2_unlink_subtree(handle_t *handle,
2586 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002587 struct ocfs2_path *left_path,
2588 struct ocfs2_path *right_path,
2589 int subtree_index,
2590 struct ocfs2_cached_dealloc_ctxt *dealloc)
2591{
2592 int i;
2593 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2594 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2595 struct ocfs2_extent_list *el;
2596 struct ocfs2_extent_block *eb;
2597
2598 el = path_leaf_el(left_path);
2599
2600 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2601
2602 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2603 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2604 break;
2605
2606 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2607
2608 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2609 le16_add_cpu(&root_el->l_next_free_rec, -1);
2610
2611 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2612 eb->h_next_leaf_blk = 0;
2613
2614 ocfs2_journal_dirty(handle, root_bh);
2615 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2616
Joel Becker6641b0c2009-02-12 18:57:52 -08002617 ocfs2_unlink_path(handle, et, dealloc, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002618 subtree_index + 1);
2619}
2620
Joel Becker1e2dd632009-02-12 19:45:28 -08002621static int ocfs2_rotate_subtree_left(handle_t *handle,
2622 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002623 struct ocfs2_path *left_path,
2624 struct ocfs2_path *right_path,
2625 int subtree_index,
2626 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker1e2dd632009-02-12 19:45:28 -08002627 int *deleted)
Mark Fasheh328d5752007-06-18 10:48:04 -07002628{
2629 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002630 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002631 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2632 struct ocfs2_extent_block *eb;
2633
2634 *deleted = 0;
2635
2636 right_leaf_el = path_leaf_el(right_path);
2637 left_leaf_el = path_leaf_el(left_path);
2638 root_bh = left_path->p_node[subtree_index].bh;
2639 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2640
2641 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2642 return 0;
2643
2644 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2645 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2646 /*
2647 * It's legal for us to proceed if the right leaf is
2648 * the rightmost one and it has an empty extent. There
2649 * are two cases to handle - whether the leaf will be
2650 * empty after removal or not. If the leaf isn't empty
2651 * then just remove the empty extent up front. The
2652 * next block will handle empty leaves by flagging
2653 * them for unlink.
2654 *
2655 * Non rightmost leaves will throw -EAGAIN and the
2656 * caller can manually move the subtree and retry.
2657 */
2658
2659 if (eb->h_next_leaf_blk != 0ULL)
2660 return -EAGAIN;
2661
2662 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002663 ret = ocfs2_journal_access_eb(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002664 path_leaf_bh(right_path),
2665 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002666 if (ret) {
2667 mlog_errno(ret);
2668 goto out;
2669 }
2670
2671 ocfs2_remove_empty_extent(right_leaf_el);
2672 } else
2673 right_has_empty = 1;
2674 }
2675
2676 if (eb->h_next_leaf_blk == 0ULL &&
2677 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2678 /*
2679 * We have to update i_last_eb_blk during the meta
2680 * data delete.
2681 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08002682 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07002683 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002684 if (ret) {
2685 mlog_errno(ret);
2686 goto out;
2687 }
2688
2689 del_right_subtree = 1;
2690 }
2691
2692 /*
2693 * Getting here with an empty extent in the right path implies
2694 * that it's the rightmost path and will be deleted.
2695 */
2696 BUG_ON(right_has_empty && !del_right_subtree);
2697
Joel Becker1e2dd632009-02-12 19:45:28 -08002698 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002699 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002700 if (ret) {
2701 mlog_errno(ret);
2702 goto out;
2703 }
2704
2705 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002706 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002707 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002708 if (ret) {
2709 mlog_errno(ret);
2710 goto out;
2711 }
2712
Joel Becker1e2dd632009-02-12 19:45:28 -08002713 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002714 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002715 if (ret) {
2716 mlog_errno(ret);
2717 goto out;
2718 }
2719 }
2720
2721 if (!right_has_empty) {
2722 /*
2723 * Only do this if we're moving a real
2724 * record. Otherwise, the action is delayed until
2725 * after removal of the right path in which case we
2726 * can do a simple shift to remove the empty extent.
2727 */
2728 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2729 memset(&right_leaf_el->l_recs[0], 0,
2730 sizeof(struct ocfs2_extent_rec));
2731 }
2732 if (eb->h_next_leaf_blk == 0ULL) {
2733 /*
2734 * Move recs over to get rid of empty extent, decrease
2735 * next_free. This is allowed to remove the last
2736 * extent in our leaf (setting l_next_free_rec to
2737 * zero) - the delete code below won't care.
2738 */
2739 ocfs2_remove_empty_extent(right_leaf_el);
2740 }
2741
2742 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2743 if (ret)
2744 mlog_errno(ret);
2745 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2746 if (ret)
2747 mlog_errno(ret);
2748
2749 if (del_right_subtree) {
Joel Becker6641b0c2009-02-12 18:57:52 -08002750 ocfs2_unlink_subtree(handle, et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002751 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08002752 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08002753 left_path);
2754 if (ret) {
2755 mlog_errno(ret);
2756 goto out;
2757 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002758
2759 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002760 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002761
2762 /*
2763 * Removal of the extent in the left leaf was skipped
2764 * above so we could delete the right path
2765 * 1st.
2766 */
2767 if (right_has_empty)
2768 ocfs2_remove_empty_extent(left_leaf_el);
2769
Tao Mae7d4cb62008-08-18 17:38:44 +08002770 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002771 if (ret)
2772 mlog_errno(ret);
2773
2774 *deleted = 1;
2775 } else
Joel Becker4619c732009-02-12 19:02:36 -08002776 ocfs2_complete_edge_insert(handle, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002777 subtree_index);
2778
2779out:
2780 return ret;
2781}
2782
2783/*
2784 * Given a full path, determine what cpos value would return us a path
2785 * containing the leaf immediately to the right of the current one.
2786 *
2787 * Will return zero if the path passed in is already the rightmost path.
2788 *
2789 * This looks similar, but is subtly different to
2790 * ocfs2_find_cpos_for_left_leaf().
2791 */
2792static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2793 struct ocfs2_path *path, u32 *cpos)
2794{
2795 int i, j, ret = 0;
2796 u64 blkno;
2797 struct ocfs2_extent_list *el;
2798
2799 *cpos = 0;
2800
2801 if (path->p_tree_depth == 0)
2802 return 0;
2803
2804 blkno = path_leaf_bh(path)->b_blocknr;
2805
2806 /* Start at the tree node just above the leaf and work our way up. */
2807 i = path->p_tree_depth - 1;
2808 while (i >= 0) {
2809 int next_free;
2810
2811 el = path->p_node[i].el;
2812
2813 /*
2814 * Find the extent record just after the one in our
2815 * path.
2816 */
2817 next_free = le16_to_cpu(el->l_next_free_rec);
2818 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2819 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2820 if (j == (next_free - 1)) {
2821 if (i == 0) {
2822 /*
2823 * We've determined that the
2824 * path specified is already
2825 * the rightmost one - return a
2826 * cpos of zero.
2827 */
2828 goto out;
2829 }
2830 /*
2831 * The rightmost record points to our
2832 * leaf - we need to travel up the
2833 * tree one level.
2834 */
2835 goto next_node;
2836 }
2837
2838 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2839 goto out;
2840 }
2841 }
2842
2843 /*
2844 * If we got here, we never found a valid node where
2845 * the tree indicated one should be.
2846 */
2847 ocfs2_error(sb,
2848 "Invalid extent tree at extent block %llu\n",
2849 (unsigned long long)blkno);
2850 ret = -EROFS;
2851 goto out;
2852
2853next_node:
2854 blkno = path->p_node[i].bh->b_blocknr;
2855 i--;
2856 }
2857
2858out:
2859 return ret;
2860}
2861
Joel Becker70f18c02009-02-13 02:09:31 -08002862static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2863 struct ocfs2_extent_tree *et,
Joel Becker13723d02008-10-17 19:25:01 -07002864 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002865{
2866 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002867 struct buffer_head *bh = path_leaf_bh(path);
2868 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002869
2870 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2871 return 0;
2872
Joel Becker70f18c02009-02-13 02:09:31 -08002873 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Joel Becker13723d02008-10-17 19:25:01 -07002874 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002875 if (ret) {
2876 mlog_errno(ret);
2877 goto out;
2878 }
2879
2880 ocfs2_remove_empty_extent(el);
2881
2882 ret = ocfs2_journal_dirty(handle, bh);
2883 if (ret)
2884 mlog_errno(ret);
2885
2886out:
2887 return ret;
2888}
2889
Joel Beckere46f74d2009-02-12 19:47:43 -08002890static int __ocfs2_rotate_tree_left(handle_t *handle,
2891 struct ocfs2_extent_tree *et,
2892 int orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07002893 struct ocfs2_path *path,
2894 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08002895 struct ocfs2_path **empty_extent_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002896{
2897 int ret, subtree_root, deleted;
2898 u32 right_cpos;
2899 struct ocfs2_path *left_path = NULL;
2900 struct ocfs2_path *right_path = NULL;
Joel Beckere46f74d2009-02-12 19:47:43 -08002901 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fasheh328d5752007-06-18 10:48:04 -07002902
2903 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2904
2905 *empty_extent_path = NULL;
2906
Joel Beckere46f74d2009-02-12 19:47:43 -08002907 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002908 if (ret) {
2909 mlog_errno(ret);
2910 goto out;
2911 }
2912
Joel Beckerffdd7a52008-10-17 22:32:01 -07002913 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002914 if (!left_path) {
2915 ret = -ENOMEM;
2916 mlog_errno(ret);
2917 goto out;
2918 }
2919
2920 ocfs2_cp_path(left_path, path);
2921
Joel Beckerffdd7a52008-10-17 22:32:01 -07002922 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002923 if (!right_path) {
2924 ret = -ENOMEM;
2925 mlog_errno(ret);
2926 goto out;
2927 }
2928
2929 while (right_cpos) {
Joel Beckerfacdb772009-02-12 18:08:48 -08002930 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002931 if (ret) {
2932 mlog_errno(ret);
2933 goto out;
2934 }
2935
Joel Becker7dc02802009-02-12 19:20:13 -08002936 subtree_root = ocfs2_find_subtree_root(et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002937 right_path);
2938
2939 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2940 subtree_root,
2941 (unsigned long long)
2942 right_path->p_node[subtree_root].bh->b_blocknr,
2943 right_path->p_tree_depth);
2944
2945 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2946 orig_credits, left_path);
2947 if (ret) {
2948 mlog_errno(ret);
2949 goto out;
2950 }
2951
Mark Fashehe8aed342007-12-03 16:43:01 -08002952 /*
2953 * Caller might still want to make changes to the
2954 * tree root, so re-add it to the journal here.
2955 */
Joel Beckere46f74d2009-02-12 19:47:43 -08002956 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002957 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08002958 if (ret) {
2959 mlog_errno(ret);
2960 goto out;
2961 }
2962
Joel Becker1e2dd632009-02-12 19:45:28 -08002963 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002964 right_path, subtree_root,
Joel Becker1e2dd632009-02-12 19:45:28 -08002965 dealloc, &deleted);
Mark Fasheh328d5752007-06-18 10:48:04 -07002966 if (ret == -EAGAIN) {
2967 /*
2968 * The rotation has to temporarily stop due to
2969 * the right subtree having an empty
2970 * extent. Pass it back to the caller for a
2971 * fixup.
2972 */
2973 *empty_extent_path = right_path;
2974 right_path = NULL;
2975 goto out;
2976 }
2977 if (ret) {
2978 mlog_errno(ret);
2979 goto out;
2980 }
2981
2982 /*
2983 * The subtree rotate might have removed records on
2984 * the rightmost edge. If so, then rotation is
2985 * complete.
2986 */
2987 if (deleted)
2988 break;
2989
2990 ocfs2_mv_path(left_path, right_path);
2991
Joel Beckere46f74d2009-02-12 19:47:43 -08002992 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002993 &right_cpos);
2994 if (ret) {
2995 mlog_errno(ret);
2996 goto out;
2997 }
2998 }
2999
3000out:
3001 ocfs2_free_path(right_path);
3002 ocfs2_free_path(left_path);
3003
3004 return ret;
3005}
3006
Joel Becker70f18c02009-02-13 02:09:31 -08003007static int ocfs2_remove_rightmost_path(handle_t *handle,
3008 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003009 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003010 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003011{
3012 int ret, subtree_index;
3013 u32 cpos;
3014 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003015 struct ocfs2_extent_block *eb;
3016 struct ocfs2_extent_list *el;
3017
Mark Fasheh328d5752007-06-18 10:48:04 -07003018
Joel Becker6136ca52009-02-12 19:32:43 -08003019 ret = ocfs2_et_sanity_check(et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003020 if (ret)
3021 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003022 /*
3023 * There's two ways we handle this depending on
3024 * whether path is the only existing one.
3025 */
3026 ret = ocfs2_extend_rotate_transaction(handle, 0,
3027 handle->h_buffer_credits,
3028 path);
3029 if (ret) {
3030 mlog_errno(ret);
3031 goto out;
3032 }
3033
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003034 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003035 if (ret) {
3036 mlog_errno(ret);
3037 goto out;
3038 }
3039
Joel Becker3d03a302009-02-12 17:49:26 -08003040 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3041 path, &cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003042 if (ret) {
3043 mlog_errno(ret);
3044 goto out;
3045 }
3046
3047 if (cpos) {
3048 /*
3049 * We have a path to the left of this one - it needs
3050 * an update too.
3051 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003052 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003053 if (!left_path) {
3054 ret = -ENOMEM;
3055 mlog_errno(ret);
3056 goto out;
3057 }
3058
Joel Beckerfacdb772009-02-12 18:08:48 -08003059 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003060 if (ret) {
3061 mlog_errno(ret);
3062 goto out;
3063 }
3064
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003065 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003066 if (ret) {
3067 mlog_errno(ret);
3068 goto out;
3069 }
3070
Joel Becker7dc02802009-02-12 19:20:13 -08003071 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003072
Joel Becker6641b0c2009-02-12 18:57:52 -08003073 ocfs2_unlink_subtree(handle, et, left_path, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003074 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08003075 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08003076 left_path);
3077 if (ret) {
3078 mlog_errno(ret);
3079 goto out;
3080 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003081
3082 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003083 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003084 } else {
3085 /*
3086 * 'path' is also the leftmost path which
3087 * means it must be the only one. This gets
3088 * handled differently because we want to
Joel Becker70f18c02009-02-13 02:09:31 -08003089 * revert the root back to having extents
Mark Fasheh328d5752007-06-18 10:48:04 -07003090 * in-line.
3091 */
Joel Becker6641b0c2009-02-12 18:57:52 -08003092 ocfs2_unlink_path(handle, et, dealloc, path, 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003093
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003094 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003095 el->l_tree_depth = 0;
3096 el->l_next_free_rec = 0;
3097 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3098
Joel Becker35dc0aa2008-08-20 16:25:06 -07003099 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003100 }
3101
3102 ocfs2_journal_dirty(handle, path_root_bh(path));
3103
3104out:
3105 ocfs2_free_path(left_path);
3106 return ret;
3107}
3108
3109/*
3110 * Left rotation of btree records.
3111 *
3112 * In many ways, this is (unsurprisingly) the opposite of right
3113 * rotation. We start at some non-rightmost path containing an empty
3114 * extent in the leaf block. The code works its way to the rightmost
3115 * path by rotating records to the left in every subtree.
3116 *
3117 * This is used by any code which reduces the number of extent records
3118 * in a leaf. After removal, an empty record should be placed in the
3119 * leftmost list position.
3120 *
3121 * This won't handle a length update of the rightmost path records if
3122 * the rightmost tree leaf record is removed so the caller is
3123 * responsible for detecting and correcting that.
3124 */
Joel Becker70f18c02009-02-13 02:09:31 -08003125static int ocfs2_rotate_tree_left(handle_t *handle,
3126 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003127 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003128 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003129{
3130 int ret, orig_credits = handle->h_buffer_credits;
3131 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3132 struct ocfs2_extent_block *eb;
3133 struct ocfs2_extent_list *el;
3134
3135 el = path_leaf_el(path);
3136 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3137 return 0;
3138
3139 if (path->p_tree_depth == 0) {
3140rightmost_no_delete:
3141 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003142 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003143 * it up front.
3144 */
Joel Becker70f18c02009-02-13 02:09:31 -08003145 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003146 if (ret)
3147 mlog_errno(ret);
3148 goto out;
3149 }
3150
3151 /*
3152 * Handle rightmost branch now. There's several cases:
3153 * 1) simple rotation leaving records in there. That's trivial.
3154 * 2) rotation requiring a branch delete - there's no more
3155 * records left. Two cases of this:
3156 * a) There are branches to the left.
3157 * b) This is also the leftmost (the only) branch.
3158 *
3159 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3160 * 2a) we need the left branch so that we can update it with the unlink
Joel Becker70f18c02009-02-13 02:09:31 -08003161 * 2b) we need to bring the root back to inline extents.
Mark Fasheh328d5752007-06-18 10:48:04 -07003162 */
3163
3164 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3165 el = &eb->h_list;
3166 if (eb->h_next_leaf_blk == 0) {
3167 /*
3168 * This gets a bit tricky if we're going to delete the
3169 * rightmost path. Get the other cases out of the way
3170 * 1st.
3171 */
3172 if (le16_to_cpu(el->l_next_free_rec) > 1)
3173 goto rightmost_no_delete;
3174
3175 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3176 ret = -EIO;
Joel Becker70f18c02009-02-13 02:09:31 -08003177 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3178 "Owner %llu has empty extent block at %llu",
3179 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07003180 (unsigned long long)le64_to_cpu(eb->h_blkno));
3181 goto out;
3182 }
3183
3184 /*
3185 * XXX: The caller can not trust "path" any more after
3186 * this as it will have been deleted. What do we do?
3187 *
3188 * In theory the rotate-for-merge code will never get
3189 * here because it'll always ask for a rotate in a
3190 * nonempty list.
3191 */
3192
Joel Becker70f18c02009-02-13 02:09:31 -08003193 ret = ocfs2_remove_rightmost_path(handle, et, path,
3194 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003195 if (ret)
3196 mlog_errno(ret);
3197 goto out;
3198 }
3199
3200 /*
3201 * Now we can loop, remembering the path we get from -EAGAIN
3202 * and restarting from there.
3203 */
3204try_rotate:
Joel Beckere46f74d2009-02-12 19:47:43 -08003205 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3206 dealloc, &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003207 if (ret && ret != -EAGAIN) {
3208 mlog_errno(ret);
3209 goto out;
3210 }
3211
3212 while (ret == -EAGAIN) {
3213 tmp_path = restart_path;
3214 restart_path = NULL;
3215
Joel Beckere46f74d2009-02-12 19:47:43 -08003216 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07003217 tmp_path, dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08003218 &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003219 if (ret && ret != -EAGAIN) {
3220 mlog_errno(ret);
3221 goto out;
3222 }
3223
3224 ocfs2_free_path(tmp_path);
3225 tmp_path = NULL;
3226
3227 if (ret == 0)
3228 goto try_rotate;
3229 }
3230
3231out:
3232 ocfs2_free_path(tmp_path);
3233 ocfs2_free_path(restart_path);
3234 return ret;
3235}
3236
3237static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3238 int index)
3239{
3240 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3241 unsigned int size;
3242
3243 if (rec->e_leaf_clusters == 0) {
3244 /*
3245 * We consumed all of the merged-from record. An empty
3246 * extent cannot exist anywhere but the 1st array
3247 * position, so move things over if the merged-from
3248 * record doesn't occupy that position.
3249 *
3250 * This creates a new empty extent so the caller
3251 * should be smart enough to have removed any existing
3252 * ones.
3253 */
3254 if (index > 0) {
3255 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3256 size = index * sizeof(struct ocfs2_extent_rec);
3257 memmove(&el->l_recs[1], &el->l_recs[0], size);
3258 }
3259
3260 /*
3261 * Always memset - the caller doesn't check whether it
3262 * created an empty extent, so there could be junk in
3263 * the other fields.
3264 */
3265 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3266 }
3267}
3268
Joel Becker4fe82c32009-02-13 02:16:08 -08003269static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003270 struct ocfs2_path *left_path,
3271 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003272{
3273 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003274 u32 right_cpos;
3275 struct ocfs2_path *right_path = NULL;
3276 struct ocfs2_extent_list *left_el;
3277
3278 *ret_right_path = NULL;
3279
3280 /* This function shouldn't be called for non-trees. */
3281 BUG_ON(left_path->p_tree_depth == 0);
3282
3283 left_el = path_leaf_el(left_path);
3284 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3285
Joel Becker4fe82c32009-02-13 02:16:08 -08003286 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3287 left_path, &right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003288 if (ret) {
3289 mlog_errno(ret);
3290 goto out;
3291 }
3292
3293 /* This function shouldn't be called for the rightmost leaf. */
3294 BUG_ON(right_cpos == 0);
3295
Joel Beckerffdd7a52008-10-17 22:32:01 -07003296 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003297 if (!right_path) {
3298 ret = -ENOMEM;
3299 mlog_errno(ret);
3300 goto out;
3301 }
3302
Joel Becker4fe82c32009-02-13 02:16:08 -08003303 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003304 if (ret) {
3305 mlog_errno(ret);
3306 goto out;
3307 }
3308
3309 *ret_right_path = right_path;
3310out:
3311 if (ret)
3312 ocfs2_free_path(right_path);
3313 return ret;
3314}
3315
3316/*
3317 * Remove split_rec clusters from the record at index and merge them
3318 * onto the beginning of the record "next" to it.
3319 * For index < l_count - 1, the next means the extent rec at index + 1.
3320 * For index == l_count - 1, the "next" means the 1st extent rec of the
3321 * next extent block.
3322 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003323static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003324 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003325 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003326 struct ocfs2_extent_rec *split_rec,
3327 int index)
3328{
3329 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003330 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3331 struct ocfs2_extent_rec *left_rec;
3332 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003333 struct ocfs2_extent_list *right_el;
3334 struct ocfs2_path *right_path = NULL;
3335 int subtree_index = 0;
3336 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3337 struct buffer_head *bh = path_leaf_bh(left_path);
3338 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003339
3340 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003341 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003342
Al Viro9d8df6a2008-05-21 06:32:11 +01003343 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003344 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3345 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003346 ret = ocfs2_get_right_path(et, left_path, &right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003347 if (ret) {
3348 mlog_errno(ret);
3349 goto out;
3350 }
3351
3352 right_el = path_leaf_el(right_path);
3353 next_free = le16_to_cpu(right_el->l_next_free_rec);
3354 BUG_ON(next_free <= 0);
3355 right_rec = &right_el->l_recs[0];
3356 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003357 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003358 right_rec = &right_el->l_recs[1];
3359 }
3360
3361 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3362 le16_to_cpu(left_rec->e_leaf_clusters) !=
3363 le32_to_cpu(right_rec->e_cpos));
3364
Joel Becker7dc02802009-02-12 19:20:13 -08003365 subtree_index = ocfs2_find_subtree_root(et, left_path,
3366 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003367
3368 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3369 handle->h_buffer_credits,
3370 right_path);
3371 if (ret) {
3372 mlog_errno(ret);
3373 goto out;
3374 }
3375
3376 root_bh = left_path->p_node[subtree_index].bh;
3377 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3378
Joel Becker7dc02802009-02-12 19:20:13 -08003379 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003380 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003381 if (ret) {
3382 mlog_errno(ret);
3383 goto out;
3384 }
3385
3386 for (i = subtree_index + 1;
3387 i < path_num_items(right_path); i++) {
Joel Becker7dc02802009-02-12 19:20:13 -08003388 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003389 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003390 if (ret) {
3391 mlog_errno(ret);
3392 goto out;
3393 }
3394
Joel Becker7dc02802009-02-12 19:20:13 -08003395 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003396 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003397 if (ret) {
3398 mlog_errno(ret);
3399 goto out;
3400 }
3401 }
3402
3403 } else {
3404 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3405 right_rec = &el->l_recs[index + 1];
3406 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003407
Joel Becker7dc02802009-02-12 19:20:13 -08003408 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
Joel Becker13723d02008-10-17 19:25:01 -07003409 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003410 if (ret) {
3411 mlog_errno(ret);
3412 goto out;
3413 }
3414
3415 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3416
3417 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3418 le64_add_cpu(&right_rec->e_blkno,
Joel Becker7dc02802009-02-12 19:20:13 -08003419 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3420 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003421 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3422
3423 ocfs2_cleanup_merge(el, index);
3424
3425 ret = ocfs2_journal_dirty(handle, bh);
3426 if (ret)
3427 mlog_errno(ret);
3428
Tao Ma677b9752008-01-30 14:21:05 +08003429 if (right_path) {
3430 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3431 if (ret)
3432 mlog_errno(ret);
3433
Joel Becker4619c732009-02-12 19:02:36 -08003434 ocfs2_complete_edge_insert(handle, left_path, right_path,
3435 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003436 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003437out:
Tao Ma677b9752008-01-30 14:21:05 +08003438 if (right_path)
3439 ocfs2_free_path(right_path);
3440 return ret;
3441}
3442
Joel Becker4fe82c32009-02-13 02:16:08 -08003443static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003444 struct ocfs2_path *right_path,
3445 struct ocfs2_path **ret_left_path)
3446{
3447 int ret;
3448 u32 left_cpos;
3449 struct ocfs2_path *left_path = NULL;
3450
3451 *ret_left_path = NULL;
3452
3453 /* This function shouldn't be called for non-trees. */
3454 BUG_ON(right_path->p_tree_depth == 0);
3455
Joel Becker4fe82c32009-02-13 02:16:08 -08003456 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
Tao Ma677b9752008-01-30 14:21:05 +08003457 right_path, &left_cpos);
3458 if (ret) {
3459 mlog_errno(ret);
3460 goto out;
3461 }
3462
3463 /* This function shouldn't be called for the leftmost leaf. */
3464 BUG_ON(left_cpos == 0);
3465
Joel Beckerffdd7a52008-10-17 22:32:01 -07003466 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003467 if (!left_path) {
3468 ret = -ENOMEM;
3469 mlog_errno(ret);
3470 goto out;
3471 }
3472
Joel Becker4fe82c32009-02-13 02:16:08 -08003473 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003474 if (ret) {
3475 mlog_errno(ret);
3476 goto out;
3477 }
3478
3479 *ret_left_path = left_path;
3480out:
3481 if (ret)
3482 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003483 return ret;
3484}
3485
3486/*
3487 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003488 * onto the tail of the record "before" it.
3489 * For index > 0, the "before" means the extent rec at index - 1.
3490 *
3491 * For index == 0, the "before" means the last record of the previous
3492 * extent block. And there is also a situation that we may need to
3493 * remove the rightmost leaf extent block in the right_path and change
3494 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003495 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003496static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003497 handle_t *handle,
Joel Becker4fe82c32009-02-13 02:16:08 -08003498 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003499 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003500 struct ocfs2_cached_dealloc_ctxt *dealloc,
3501 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003502{
Tao Ma677b9752008-01-30 14:21:05 +08003503 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003504 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3505 struct ocfs2_extent_rec *left_rec;
3506 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003507 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3508 struct buffer_head *bh = path_leaf_bh(right_path);
3509 struct buffer_head *root_bh = NULL;
3510 struct ocfs2_path *left_path = NULL;
3511 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003512
Tao Ma677b9752008-01-30 14:21:05 +08003513 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003514
Mark Fasheh328d5752007-06-18 10:48:04 -07003515 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003516 if (index == 0) {
3517 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003518 ret = ocfs2_get_left_path(et, right_path, &left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003519 if (ret) {
3520 mlog_errno(ret);
3521 goto out;
3522 }
3523
3524 left_el = path_leaf_el(left_path);
3525 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3526 le16_to_cpu(left_el->l_count));
3527
3528 left_rec = &left_el->l_recs[
3529 le16_to_cpu(left_el->l_next_free_rec) - 1];
3530 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3531 le16_to_cpu(left_rec->e_leaf_clusters) !=
3532 le32_to_cpu(split_rec->e_cpos));
3533
Joel Becker7dc02802009-02-12 19:20:13 -08003534 subtree_index = ocfs2_find_subtree_root(et, left_path,
3535 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003536
3537 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3538 handle->h_buffer_credits,
3539 left_path);
3540 if (ret) {
3541 mlog_errno(ret);
3542 goto out;
3543 }
3544
3545 root_bh = left_path->p_node[subtree_index].bh;
3546 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3547
Joel Becker4fe82c32009-02-13 02:16:08 -08003548 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003549 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003550 if (ret) {
3551 mlog_errno(ret);
3552 goto out;
3553 }
3554
3555 for (i = subtree_index + 1;
3556 i < path_num_items(right_path); i++) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003557 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003558 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003559 if (ret) {
3560 mlog_errno(ret);
3561 goto out;
3562 }
3563
Joel Becker4fe82c32009-02-13 02:16:08 -08003564 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003565 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003566 if (ret) {
3567 mlog_errno(ret);
3568 goto out;
3569 }
3570 }
3571 } else {
3572 left_rec = &el->l_recs[index - 1];
3573 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3574 has_empty_extent = 1;
3575 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003576
Joel Becker4fe82c32009-02-13 02:16:08 -08003577 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Tao Ma9047bea2009-01-05 14:45:24 +08003578 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003579 if (ret) {
3580 mlog_errno(ret);
3581 goto out;
3582 }
3583
3584 if (has_empty_extent && index == 1) {
3585 /*
3586 * The easy case - we can just plop the record right in.
3587 */
3588 *left_rec = *split_rec;
3589
3590 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003591 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003592 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003593
3594 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3595 le64_add_cpu(&right_rec->e_blkno,
Joel Becker4fe82c32009-02-13 02:16:08 -08003596 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3597 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003598 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3599
3600 ocfs2_cleanup_merge(el, index);
3601
3602 ret = ocfs2_journal_dirty(handle, bh);
3603 if (ret)
3604 mlog_errno(ret);
3605
Tao Ma677b9752008-01-30 14:21:05 +08003606 if (left_path) {
3607 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3608 if (ret)
3609 mlog_errno(ret);
3610
3611 /*
3612 * In the situation that the right_rec is empty and the extent
3613 * block is empty also, ocfs2_complete_edge_insert can't handle
3614 * it and we need to delete the right extent block.
3615 */
3616 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3617 le16_to_cpu(el->l_next_free_rec) == 1) {
3618
Joel Becker70f18c02009-02-13 02:09:31 -08003619 ret = ocfs2_remove_rightmost_path(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003620 right_path,
Joel Becker70f18c02009-02-13 02:09:31 -08003621 dealloc);
Tao Ma677b9752008-01-30 14:21:05 +08003622 if (ret) {
3623 mlog_errno(ret);
3624 goto out;
3625 }
3626
3627 /* Now the rightmost extent block has been deleted.
3628 * So we use the new rightmost path.
3629 */
3630 ocfs2_mv_path(right_path, left_path);
3631 left_path = NULL;
3632 } else
Joel Becker4619c732009-02-12 19:02:36 -08003633 ocfs2_complete_edge_insert(handle, left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003634 right_path, subtree_index);
3635 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003636out:
Tao Ma677b9752008-01-30 14:21:05 +08003637 if (left_path)
3638 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003639 return ret;
3640}
3641
Joel Beckerc495dd22009-02-13 02:19:11 -08003642static int ocfs2_try_to_merge_extent(handle_t *handle,
3643 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003644 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003645 int split_index,
3646 struct ocfs2_extent_rec *split_rec,
3647 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckerc495dd22009-02-13 02:19:11 -08003648 struct ocfs2_merge_ctxt *ctxt)
Mark Fasheh328d5752007-06-18 10:48:04 -07003649{
Tao Mao518d7262007-08-28 17:25:35 -07003650 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003651 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003652 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3653
3654 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3655
Tao Mao518d7262007-08-28 17:25:35 -07003656 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3657 /*
3658 * The merge code will need to create an empty
3659 * extent to take the place of the newly
3660 * emptied slot. Remove any pre-existing empty
3661 * extents - having more than one in a leaf is
3662 * illegal.
3663 */
Joel Becker70f18c02009-02-13 02:09:31 -08003664 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Tao Mao518d7262007-08-28 17:25:35 -07003665 if (ret) {
3666 mlog_errno(ret);
3667 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003668 }
Tao Mao518d7262007-08-28 17:25:35 -07003669 split_index--;
3670 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003671 }
3672
3673 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3674 /*
3675 * Left-right contig implies this.
3676 */
3677 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003678
3679 /*
3680 * Since the leftright insert always covers the entire
3681 * extent, this call will delete the insert record
3682 * entirely, resulting in an empty extent record added to
3683 * the extent block.
3684 *
3685 * Since the adding of an empty extent shifts
3686 * everything back to the right, there's no need to
3687 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003688 *
3689 * When the split_index is zero, we need to merge it to the
3690 * prevoius extent block. It is more efficient and easier
3691 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003692 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003693 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003694 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003695 if (ret) {
3696 mlog_errno(ret);
3697 goto out;
3698 }
3699
3700 /*
3701 * We can only get this from logic error above.
3702 */
3703 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3704
Tao Ma677b9752008-01-30 14:21:05 +08003705 /* The merge left us with an empty extent, remove it. */
Joel Becker70f18c02009-02-13 02:09:31 -08003706 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003707 if (ret) {
3708 mlog_errno(ret);
3709 goto out;
3710 }
Tao Ma677b9752008-01-30 14:21:05 +08003711
Mark Fasheh328d5752007-06-18 10:48:04 -07003712 rec = &el->l_recs[split_index];
3713
3714 /*
3715 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003716 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003717 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003718 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3719 dealloc, split_index);
Tao Ma677b9752008-01-30 14:21:05 +08003720
Mark Fasheh328d5752007-06-18 10:48:04 -07003721 if (ret) {
3722 mlog_errno(ret);
3723 goto out;
3724 }
3725
Joel Becker70f18c02009-02-13 02:09:31 -08003726 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003727 /*
3728 * Error from this last rotate is not critical, so
3729 * print but don't bubble it up.
3730 */
3731 if (ret)
3732 mlog_errno(ret);
3733 ret = 0;
3734 } else {
3735 /*
3736 * Merge a record to the left or right.
3737 *
3738 * 'contig_type' is relative to the existing record,
3739 * so for example, if we're "right contig", it's to
3740 * the record on the left (hence the left merge).
3741 */
3742 if (ctxt->c_contig_type == CONTIG_RIGHT) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003743 ret = ocfs2_merge_rec_left(path, handle, et,
3744 split_rec, dealloc,
Mark Fasheh328d5752007-06-18 10:48:04 -07003745 split_index);
3746 if (ret) {
3747 mlog_errno(ret);
3748 goto out;
3749 }
3750 } else {
Joel Becker4fe82c32009-02-13 02:16:08 -08003751 ret = ocfs2_merge_rec_right(path, handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003752 et, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003753 split_index);
3754 if (ret) {
3755 mlog_errno(ret);
3756 goto out;
3757 }
3758 }
3759
3760 if (ctxt->c_split_covers_rec) {
3761 /*
3762 * The merge may have left an empty extent in
3763 * our leaf. Try to rotate it away.
3764 */
Joel Becker70f18c02009-02-13 02:09:31 -08003765 ret = ocfs2_rotate_tree_left(handle, et, path,
3766 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003767 if (ret)
3768 mlog_errno(ret);
3769 ret = 0;
3770 }
3771 }
3772
3773out:
3774 return ret;
3775}
3776
3777static void ocfs2_subtract_from_rec(struct super_block *sb,
3778 enum ocfs2_split_type split,
3779 struct ocfs2_extent_rec *rec,
3780 struct ocfs2_extent_rec *split_rec)
3781{
3782 u64 len_blocks;
3783
3784 len_blocks = ocfs2_clusters_to_blocks(sb,
3785 le16_to_cpu(split_rec->e_leaf_clusters));
3786
3787 if (split == SPLIT_LEFT) {
3788 /*
3789 * Region is on the left edge of the existing
3790 * record.
3791 */
3792 le32_add_cpu(&rec->e_cpos,
3793 le16_to_cpu(split_rec->e_leaf_clusters));
3794 le64_add_cpu(&rec->e_blkno, len_blocks);
3795 le16_add_cpu(&rec->e_leaf_clusters,
3796 -le16_to_cpu(split_rec->e_leaf_clusters));
3797 } else {
3798 /*
3799 * Region is on the right edge of the existing
3800 * record.
3801 */
3802 le16_add_cpu(&rec->e_leaf_clusters,
3803 -le16_to_cpu(split_rec->e_leaf_clusters));
3804 }
3805}
3806
Mark Fashehdcd05382007-01-16 11:32:23 -08003807/*
3808 * Do the final bits of extent record insertion at the target leaf
3809 * list. If this leaf is part of an allocation tree, it is assumed
3810 * that the tree above has been prepared.
3811 */
Joel Beckerd5628622009-02-13 02:54:36 -08003812static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3813 struct ocfs2_extent_rec *insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08003814 struct ocfs2_extent_list *el,
Joel Beckerd5628622009-02-13 02:54:36 -08003815 struct ocfs2_insert_type *insert)
Mark Fashehdcd05382007-01-16 11:32:23 -08003816{
3817 int i = insert->ins_contig_index;
3818 unsigned int range;
3819 struct ocfs2_extent_rec *rec;
3820
Mark Fashehe48edee2007-03-07 16:46:57 -08003821 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003822
Mark Fasheh328d5752007-06-18 10:48:04 -07003823 if (insert->ins_split != SPLIT_NONE) {
3824 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3825 BUG_ON(i == -1);
3826 rec = &el->l_recs[i];
Joel Beckerd5628622009-02-13 02:54:36 -08003827 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3828 insert->ins_split, rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003829 insert_rec);
3830 goto rotate;
3831 }
3832
Mark Fashehdcd05382007-01-16 11:32:23 -08003833 /*
3834 * Contiguous insert - either left or right.
3835 */
3836 if (insert->ins_contig != CONTIG_NONE) {
3837 rec = &el->l_recs[i];
3838 if (insert->ins_contig == CONTIG_LEFT) {
3839 rec->e_blkno = insert_rec->e_blkno;
3840 rec->e_cpos = insert_rec->e_cpos;
3841 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003842 le16_add_cpu(&rec->e_leaf_clusters,
3843 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003844 return;
3845 }
3846
3847 /*
3848 * Handle insert into an empty leaf.
3849 */
3850 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3851 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3852 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3853 el->l_recs[0] = *insert_rec;
3854 el->l_next_free_rec = cpu_to_le16(1);
3855 return;
3856 }
3857
3858 /*
3859 * Appending insert.
3860 */
3861 if (insert->ins_appending == APPEND_TAIL) {
3862 i = le16_to_cpu(el->l_next_free_rec) - 1;
3863 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003864 range = le32_to_cpu(rec->e_cpos)
3865 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003866 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3867
3868 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3869 le16_to_cpu(el->l_count),
Joel Beckerd5628622009-02-13 02:54:36 -08003870 "owner %llu, depth %u, count %u, next free %u, "
Mark Fashehdcd05382007-01-16 11:32:23 -08003871 "rec.cpos %u, rec.clusters %u, "
3872 "insert.cpos %u, insert.clusters %u\n",
Joel Beckerd5628622009-02-13 02:54:36 -08003873 ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08003874 le16_to_cpu(el->l_tree_depth),
3875 le16_to_cpu(el->l_count),
3876 le16_to_cpu(el->l_next_free_rec),
3877 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003878 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003879 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003880 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003881 i++;
3882 el->l_recs[i] = *insert_rec;
3883 le16_add_cpu(&el->l_next_free_rec, 1);
3884 return;
3885 }
3886
Mark Fasheh328d5752007-06-18 10:48:04 -07003887rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003888 /*
3889 * Ok, we have to rotate.
3890 *
3891 * At this point, it is safe to assume that inserting into an
3892 * empty leaf and appending to a leaf have both been handled
3893 * above.
3894 *
3895 * This leaf needs to have space, either by the empty 1st
3896 * extent record, or by virtue of an l_next_rec < l_count.
3897 */
3898 ocfs2_rotate_leaf(el, insert_rec);
3899}
3900
Joel Beckerd401dc12009-02-13 02:24:10 -08003901static void ocfs2_adjust_rightmost_records(handle_t *handle,
3902 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003903 struct ocfs2_path *path,
3904 struct ocfs2_extent_rec *insert_rec)
3905{
3906 int ret, i, next_free;
3907 struct buffer_head *bh;
3908 struct ocfs2_extent_list *el;
3909 struct ocfs2_extent_rec *rec;
3910
3911 /*
3912 * Update everything except the leaf block.
3913 */
3914 for (i = 0; i < path->p_tree_depth; i++) {
3915 bh = path->p_node[i].bh;
3916 el = path->p_node[i].el;
3917
3918 next_free = le16_to_cpu(el->l_next_free_rec);
3919 if (next_free == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08003920 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3921 "Owner %llu has a bad extent list",
3922 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fasheh328d5752007-06-18 10:48:04 -07003923 ret = -EIO;
3924 return;
3925 }
3926
3927 rec = &el->l_recs[next_free - 1];
3928
3929 rec->e_int_clusters = insert_rec->e_cpos;
3930 le32_add_cpu(&rec->e_int_clusters,
3931 le16_to_cpu(insert_rec->e_leaf_clusters));
3932 le32_add_cpu(&rec->e_int_clusters,
3933 -le32_to_cpu(rec->e_cpos));
3934
3935 ret = ocfs2_journal_dirty(handle, bh);
3936 if (ret)
3937 mlog_errno(ret);
3938
3939 }
3940}
3941
Joel Beckerd401dc12009-02-13 02:24:10 -08003942static int ocfs2_append_rec_to_path(handle_t *handle,
3943 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08003944 struct ocfs2_extent_rec *insert_rec,
3945 struct ocfs2_path *right_path,
3946 struct ocfs2_path **ret_left_path)
3947{
Mark Fasheh328d5752007-06-18 10:48:04 -07003948 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003949 struct ocfs2_extent_list *el;
3950 struct ocfs2_path *left_path = NULL;
3951
3952 *ret_left_path = NULL;
3953
3954 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003955 * This shouldn't happen for non-trees. The extent rec cluster
3956 * count manipulation below only works for interior nodes.
3957 */
3958 BUG_ON(right_path->p_tree_depth == 0);
3959
3960 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003961 * If our appending insert is at the leftmost edge of a leaf,
3962 * then we might need to update the rightmost records of the
3963 * neighboring path.
3964 */
3965 el = path_leaf_el(right_path);
3966 next_free = le16_to_cpu(el->l_next_free_rec);
3967 if (next_free == 0 ||
3968 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3969 u32 left_cpos;
3970
Joel Beckerd401dc12009-02-13 02:24:10 -08003971 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3972 right_path, &left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08003973 if (ret) {
3974 mlog_errno(ret);
3975 goto out;
3976 }
3977
3978 mlog(0, "Append may need a left path update. cpos: %u, "
3979 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3980 left_cpos);
3981
3982 /*
3983 * No need to worry if the append is already in the
3984 * leftmost leaf.
3985 */
3986 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07003987 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003988 if (!left_path) {
3989 ret = -ENOMEM;
3990 mlog_errno(ret);
3991 goto out;
3992 }
3993
Joel Beckerd401dc12009-02-13 02:24:10 -08003994 ret = ocfs2_find_path(et->et_ci, left_path,
Joel Beckerfacdb772009-02-12 18:08:48 -08003995 left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08003996 if (ret) {
3997 mlog_errno(ret);
3998 goto out;
3999 }
4000
4001 /*
4002 * ocfs2_insert_path() will pass the left_path to the
4003 * journal for us.
4004 */
4005 }
4006 }
4007
Joel Beckerd401dc12009-02-13 02:24:10 -08004008 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004009 if (ret) {
4010 mlog_errno(ret);
4011 goto out;
4012 }
4013
Joel Beckerd401dc12009-02-13 02:24:10 -08004014 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004015
4016 *ret_left_path = left_path;
4017 ret = 0;
4018out:
4019 if (ret != 0)
4020 ocfs2_free_path(left_path);
4021
4022 return ret;
4023}
4024
Mark Fasheh328d5752007-06-18 10:48:04 -07004025static void ocfs2_split_record(struct inode *inode,
4026 struct ocfs2_path *left_path,
4027 struct ocfs2_path *right_path,
4028 struct ocfs2_extent_rec *split_rec,
4029 enum ocfs2_split_type split)
4030{
4031 int index;
4032 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4033 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4034 struct ocfs2_extent_rec *rec, *tmprec;
4035
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004036 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004037 if (left_path)
4038 left_el = path_leaf_el(left_path);
4039
4040 el = right_el;
4041 insert_el = right_el;
4042 index = ocfs2_search_extent_list(el, cpos);
4043 if (index != -1) {
4044 if (index == 0 && left_path) {
4045 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4046
4047 /*
4048 * This typically means that the record
4049 * started in the left path but moved to the
4050 * right as a result of rotation. We either
4051 * move the existing record to the left, or we
4052 * do the later insert there.
4053 *
4054 * In this case, the left path should always
4055 * exist as the rotate code will have passed
4056 * it back for a post-insert update.
4057 */
4058
4059 if (split == SPLIT_LEFT) {
4060 /*
4061 * It's a left split. Since we know
4062 * that the rotate code gave us an
4063 * empty extent in the left path, we
4064 * can just do the insert there.
4065 */
4066 insert_el = left_el;
4067 } else {
4068 /*
4069 * Right split - we have to move the
4070 * existing record over to the left
4071 * leaf. The insert will be into the
4072 * newly created empty extent in the
4073 * right leaf.
4074 */
4075 tmprec = &right_el->l_recs[index];
4076 ocfs2_rotate_leaf(left_el, tmprec);
4077 el = left_el;
4078
4079 memset(tmprec, 0, sizeof(*tmprec));
4080 index = ocfs2_search_extent_list(left_el, cpos);
4081 BUG_ON(index == -1);
4082 }
4083 }
4084 } else {
4085 BUG_ON(!left_path);
4086 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4087 /*
4088 * Left path is easy - we can just allow the insert to
4089 * happen.
4090 */
4091 el = left_el;
4092 insert_el = left_el;
4093 index = ocfs2_search_extent_list(el, cpos);
4094 BUG_ON(index == -1);
4095 }
4096
4097 rec = &el->l_recs[index];
4098 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
4099 ocfs2_rotate_leaf(insert_el, split_rec);
4100}
4101
Mark Fashehdcd05382007-01-16 11:32:23 -08004102/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004103 * This function only does inserts on an allocation b-tree. For tree
4104 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004105 *
4106 * right_path is the path we want to do the actual insert
4107 * in. left_path should only be passed in if we need to update that
4108 * portion of the tree after an edge insert.
4109 */
4110static int ocfs2_insert_path(struct inode *inode,
4111 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08004112 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004113 struct ocfs2_path *left_path,
4114 struct ocfs2_path *right_path,
4115 struct ocfs2_extent_rec *insert_rec,
4116 struct ocfs2_insert_type *insert)
4117{
4118 int ret, subtree_index;
4119 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004120
Mark Fashehdcd05382007-01-16 11:32:23 -08004121 if (left_path) {
4122 int credits = handle->h_buffer_credits;
4123
4124 /*
4125 * There's a chance that left_path got passed back to
4126 * us without being accounted for in the
4127 * journal. Extend our transaction here to be sure we
4128 * can change those blocks.
4129 */
4130 credits += left_path->p_tree_depth;
4131
4132 ret = ocfs2_extend_trans(handle, credits);
4133 if (ret < 0) {
4134 mlog_errno(ret);
4135 goto out;
4136 }
4137
Joel Becker7dc02802009-02-12 19:20:13 -08004138 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004139 if (ret < 0) {
4140 mlog_errno(ret);
4141 goto out;
4142 }
4143 }
4144
Mark Fashehe8aed342007-12-03 16:43:01 -08004145 /*
4146 * Pass both paths to the journal. The majority of inserts
4147 * will be touching all components anyway.
4148 */
Joel Becker7dc02802009-02-12 19:20:13 -08004149 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004150 if (ret < 0) {
4151 mlog_errno(ret);
4152 goto out;
4153 }
4154
Mark Fasheh328d5752007-06-18 10:48:04 -07004155 if (insert->ins_split != SPLIT_NONE) {
4156 /*
4157 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004158 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004159 * function sort it all out.
4160 */
4161 ocfs2_split_record(inode, left_path, right_path,
4162 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004163
4164 /*
4165 * Split might have modified either leaf and we don't
4166 * have a guarantee that the later edge insert will
4167 * dirty this for us.
4168 */
4169 if (left_path)
4170 ret = ocfs2_journal_dirty(handle,
4171 path_leaf_bh(left_path));
4172 if (ret)
4173 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07004174 } else
Joel Beckerd5628622009-02-13 02:54:36 -08004175 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4176 insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004177
Mark Fashehdcd05382007-01-16 11:32:23 -08004178 ret = ocfs2_journal_dirty(handle, leaf_bh);
4179 if (ret)
4180 mlog_errno(ret);
4181
4182 if (left_path) {
4183 /*
4184 * The rotate code has indicated that we need to fix
4185 * up portions of the tree after the insert.
4186 *
4187 * XXX: Should we extend the transaction here?
4188 */
Joel Becker7dc02802009-02-12 19:20:13 -08004189 subtree_index = ocfs2_find_subtree_root(et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004190 right_path);
Joel Becker4619c732009-02-12 19:02:36 -08004191 ocfs2_complete_edge_insert(handle, left_path, right_path,
4192 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08004193 }
4194
4195 ret = 0;
4196out:
4197 return ret;
4198}
4199
4200static int ocfs2_do_insert_extent(struct inode *inode,
4201 handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004202 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004203 struct ocfs2_extent_rec *insert_rec,
4204 struct ocfs2_insert_type *type)
4205{
4206 int ret, rotate = 0;
4207 u32 cpos;
4208 struct ocfs2_path *right_path = NULL;
4209 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004210 struct ocfs2_extent_list *el;
4211
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004212 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004213
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004214 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004215 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004216 if (ret) {
4217 mlog_errno(ret);
4218 goto out;
4219 }
4220
4221 if (le16_to_cpu(el->l_tree_depth) == 0) {
Joel Beckerd5628622009-02-13 02:54:36 -08004222 ocfs2_insert_at_leaf(et, insert_rec, el, type);
Mark Fashehdcd05382007-01-16 11:32:23 -08004223 goto out_update_clusters;
4224 }
4225
Joel Beckerffdd7a52008-10-17 22:32:01 -07004226 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004227 if (!right_path) {
4228 ret = -ENOMEM;
4229 mlog_errno(ret);
4230 goto out;
4231 }
4232
4233 /*
4234 * Determine the path to start with. Rotations need the
4235 * rightmost path, everything else can go directly to the
4236 * target leaf.
4237 */
4238 cpos = le32_to_cpu(insert_rec->e_cpos);
4239 if (type->ins_appending == APPEND_NONE &&
4240 type->ins_contig == CONTIG_NONE) {
4241 rotate = 1;
4242 cpos = UINT_MAX;
4243 }
4244
Joel Beckerfacdb772009-02-12 18:08:48 -08004245 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004246 if (ret) {
4247 mlog_errno(ret);
4248 goto out;
4249 }
4250
4251 /*
4252 * Rotations and appends need special treatment - they modify
4253 * parts of the tree's above them.
4254 *
4255 * Both might pass back a path immediate to the left of the
4256 * one being inserted to. This will be cause
4257 * ocfs2_insert_path() to modify the rightmost records of
4258 * left_path to account for an edge insert.
4259 *
4260 * XXX: When modifying this code, keep in mind that an insert
4261 * can wind up skipping both of these two special cases...
4262 */
4263 if (rotate) {
Joel Becker1bbf0b82009-02-12 19:42:08 -08004264 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004265 le32_to_cpu(insert_rec->e_cpos),
4266 right_path, &left_path);
4267 if (ret) {
4268 mlog_errno(ret);
4269 goto out;
4270 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004271
4272 /*
4273 * ocfs2_rotate_tree_right() might have extended the
4274 * transaction without re-journaling our tree root.
4275 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004276 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004277 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004278 if (ret) {
4279 mlog_errno(ret);
4280 goto out;
4281 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004282 } else if (type->ins_appending == APPEND_TAIL
4283 && type->ins_contig != CONTIG_LEFT) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004284 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08004285 right_path, &left_path);
4286 if (ret) {
4287 mlog_errno(ret);
4288 goto out;
4289 }
4290 }
4291
Joel Becker7dc02802009-02-12 19:20:13 -08004292 ret = ocfs2_insert_path(inode, handle, et, left_path, right_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004293 insert_rec, type);
4294 if (ret) {
4295 mlog_errno(ret);
4296 goto out;
4297 }
4298
4299out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004300 if (type->ins_split == SPLIT_NONE)
Joel Becker6136ca52009-02-12 19:32:43 -08004301 ocfs2_et_update_clusters(et,
Joel Becker35dc0aa2008-08-20 16:25:06 -07004302 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004303
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004304 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004305 if (ret)
4306 mlog_errno(ret);
4307
4308out:
4309 ocfs2_free_path(left_path);
4310 ocfs2_free_path(right_path);
4311
4312 return ret;
4313}
4314
Mark Fasheh328d5752007-06-18 10:48:04 -07004315static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08004316ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004317 struct ocfs2_extent_list *el, int index,
4318 struct ocfs2_extent_rec *split_rec)
4319{
Tao Maad5a4d72008-01-30 14:21:32 +08004320 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004321 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004322 u32 left_cpos, right_cpos;
4323 struct ocfs2_extent_rec *rec = NULL;
4324 struct ocfs2_extent_list *new_el;
4325 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4326 struct buffer_head *bh;
4327 struct ocfs2_extent_block *eb;
4328
4329 if (index > 0) {
4330 rec = &el->l_recs[index - 1];
4331 } else if (path->p_tree_depth > 0) {
4332 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4333 path, &left_cpos);
4334 if (status)
4335 goto out;
4336
4337 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004338 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004339 if (!left_path)
4340 goto out;
4341
Joel Beckerfacdb772009-02-12 18:08:48 -08004342 status = ocfs2_find_path(INODE_CACHE(inode),
4343 left_path, left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004344 if (status)
4345 goto out;
4346
4347 new_el = path_leaf_el(left_path);
4348
4349 if (le16_to_cpu(new_el->l_next_free_rec) !=
4350 le16_to_cpu(new_el->l_count)) {
4351 bh = path_leaf_bh(left_path);
4352 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004353 ocfs2_error(inode->i_sb,
4354 "Extent block #%llu has an "
4355 "invalid l_next_free_rec of "
4356 "%d. It should have "
4357 "matched the l_count of %d",
4358 (unsigned long long)le64_to_cpu(eb->h_blkno),
4359 le16_to_cpu(new_el->l_next_free_rec),
4360 le16_to_cpu(new_el->l_count));
4361 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004362 goto out;
4363 }
4364 rec = &new_el->l_recs[
4365 le16_to_cpu(new_el->l_next_free_rec) - 1];
4366 }
4367 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004368
4369 /*
4370 * We're careful to check for an empty extent record here -
4371 * the merge code will know what to do if it sees one.
4372 */
Tao Maad5a4d72008-01-30 14:21:32 +08004373 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004374 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4375 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4376 ret = CONTIG_RIGHT;
4377 } else {
4378 ret = ocfs2_extent_contig(inode, rec, split_rec);
4379 }
4380 }
4381
Tao Maad5a4d72008-01-30 14:21:32 +08004382 rec = NULL;
4383 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4384 rec = &el->l_recs[index + 1];
4385 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4386 path->p_tree_depth > 0) {
4387 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4388 path, &right_cpos);
4389 if (status)
4390 goto out;
4391
4392 if (right_cpos == 0)
4393 goto out;
4394
Joel Beckerffdd7a52008-10-17 22:32:01 -07004395 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004396 if (!right_path)
4397 goto out;
4398
Joel Beckerfacdb772009-02-12 18:08:48 -08004399 status = ocfs2_find_path(INODE_CACHE(inode), right_path, right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004400 if (status)
4401 goto out;
4402
4403 new_el = path_leaf_el(right_path);
4404 rec = &new_el->l_recs[0];
4405 if (ocfs2_is_empty_extent(rec)) {
4406 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4407 bh = path_leaf_bh(right_path);
4408 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004409 ocfs2_error(inode->i_sb,
4410 "Extent block #%llu has an "
4411 "invalid l_next_free_rec of %d",
4412 (unsigned long long)le64_to_cpu(eb->h_blkno),
4413 le16_to_cpu(new_el->l_next_free_rec));
4414 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004415 goto out;
4416 }
4417 rec = &new_el->l_recs[1];
4418 }
4419 }
4420
4421 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004422 enum ocfs2_contig_type contig_type;
4423
Mark Fasheh328d5752007-06-18 10:48:04 -07004424 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4425
4426 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4427 ret = CONTIG_LEFTRIGHT;
4428 else if (ret == CONTIG_NONE)
4429 ret = contig_type;
4430 }
4431
Tao Maad5a4d72008-01-30 14:21:32 +08004432out:
4433 if (left_path)
4434 ocfs2_free_path(left_path);
4435 if (right_path)
4436 ocfs2_free_path(right_path);
4437
Mark Fasheh328d5752007-06-18 10:48:04 -07004438 return ret;
4439}
4440
Mark Fashehdcd05382007-01-16 11:32:23 -08004441static void ocfs2_figure_contig_type(struct inode *inode,
4442 struct ocfs2_insert_type *insert,
4443 struct ocfs2_extent_list *el,
Tao Maca12b7c2008-08-18 17:38:52 +08004444 struct ocfs2_extent_rec *insert_rec,
4445 struct ocfs2_extent_tree *et)
Mark Fashehdcd05382007-01-16 11:32:23 -08004446{
4447 int i;
4448 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4449
Mark Fashehe48edee2007-03-07 16:46:57 -08004450 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4451
Mark Fashehdcd05382007-01-16 11:32:23 -08004452 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4453 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4454 insert_rec);
4455 if (contig_type != CONTIG_NONE) {
4456 insert->ins_contig_index = i;
4457 break;
4458 }
4459 }
4460 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004461
4462 if (insert->ins_contig != CONTIG_NONE) {
4463 struct ocfs2_extent_rec *rec =
4464 &el->l_recs[insert->ins_contig_index];
4465 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4466 le16_to_cpu(insert_rec->e_leaf_clusters);
4467
4468 /*
4469 * Caller might want us to limit the size of extents, don't
4470 * calculate contiguousness if we might exceed that limit.
4471 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004472 if (et->et_max_leaf_clusters &&
4473 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004474 insert->ins_contig = CONTIG_NONE;
4475 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004476}
4477
4478/*
4479 * This should only be called against the righmost leaf extent list.
4480 *
4481 * ocfs2_figure_appending_type() will figure out whether we'll have to
4482 * insert at the tail of the rightmost leaf.
4483 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004484 * This should also work against the root extent list for tree's with 0
4485 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004486 * then the logic here makes sense.
4487 */
4488static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4489 struct ocfs2_extent_list *el,
4490 struct ocfs2_extent_rec *insert_rec)
4491{
4492 int i;
4493 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4494 struct ocfs2_extent_rec *rec;
4495
4496 insert->ins_appending = APPEND_NONE;
4497
Mark Fashehe48edee2007-03-07 16:46:57 -08004498 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004499
4500 if (!el->l_next_free_rec)
4501 goto set_tail_append;
4502
4503 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4504 /* Were all records empty? */
4505 if (le16_to_cpu(el->l_next_free_rec) == 1)
4506 goto set_tail_append;
4507 }
4508
4509 i = le16_to_cpu(el->l_next_free_rec) - 1;
4510 rec = &el->l_recs[i];
4511
Mark Fashehe48edee2007-03-07 16:46:57 -08004512 if (cpos >=
4513 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004514 goto set_tail_append;
4515
4516 return;
4517
4518set_tail_append:
4519 insert->ins_appending = APPEND_TAIL;
4520}
4521
4522/*
4523 * Helper function called at the begining of an insert.
4524 *
4525 * This computes a few things that are commonly used in the process of
4526 * inserting into the btree:
4527 * - Whether the new extent is contiguous with an existing one.
4528 * - The current tree depth.
4529 * - Whether the insert is an appending one.
4530 * - The total # of free records in the tree.
4531 *
4532 * All of the information is stored on the ocfs2_insert_type
4533 * structure.
4534 */
4535static int ocfs2_figure_insert_type(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004536 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004537 struct buffer_head **last_eb_bh,
4538 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004539 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004540 struct ocfs2_insert_type *insert)
4541{
4542 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004543 struct ocfs2_extent_block *eb;
4544 struct ocfs2_extent_list *el;
4545 struct ocfs2_path *path = NULL;
4546 struct buffer_head *bh = NULL;
4547
Mark Fasheh328d5752007-06-18 10:48:04 -07004548 insert->ins_split = SPLIT_NONE;
4549
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004550 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004551 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4552
4553 if (el->l_tree_depth) {
4554 /*
4555 * If we have tree depth, we read in the
4556 * rightmost extent block ahead of time as
4557 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4558 * may want it later.
4559 */
Joel Becker3d03a302009-02-12 17:49:26 -08004560 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08004561 ocfs2_et_get_last_eb_blk(et),
4562 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004563 if (ret) {
4564 mlog_exit(ret);
4565 goto out;
4566 }
4567 eb = (struct ocfs2_extent_block *) bh->b_data;
4568 el = &eb->h_list;
4569 }
4570
4571 /*
4572 * Unless we have a contiguous insert, we'll need to know if
4573 * there is room left in our allocation tree for another
4574 * extent record.
4575 *
4576 * XXX: This test is simplistic, we can search for empty
4577 * extent records too.
4578 */
Tao Maoc77534f2007-08-28 17:22:33 -07004579 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004580 le16_to_cpu(el->l_next_free_rec);
4581
4582 if (!insert->ins_tree_depth) {
Tao Maca12b7c2008-08-18 17:38:52 +08004583 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004584 ocfs2_figure_appending_type(insert, el, insert_rec);
4585 return 0;
4586 }
4587
Joel Beckerffdd7a52008-10-17 22:32:01 -07004588 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004589 if (!path) {
4590 ret = -ENOMEM;
4591 mlog_errno(ret);
4592 goto out;
4593 }
4594
4595 /*
4596 * In the case that we're inserting past what the tree
4597 * currently accounts for, ocfs2_find_path() will return for
4598 * us the rightmost tree path. This is accounted for below in
4599 * the appending code.
4600 */
Joel Beckerfacdb772009-02-12 18:08:48 -08004601 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -08004602 if (ret) {
4603 mlog_errno(ret);
4604 goto out;
4605 }
4606
4607 el = path_leaf_el(path);
4608
4609 /*
4610 * Now that we have the path, there's two things we want to determine:
4611 * 1) Contiguousness (also set contig_index if this is so)
4612 *
4613 * 2) Are we doing an append? We can trivially break this up
4614 * into two types of appends: simple record append, or a
4615 * rotate inside the tail leaf.
4616 */
Tao Maca12b7c2008-08-18 17:38:52 +08004617 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004618
4619 /*
4620 * The insert code isn't quite ready to deal with all cases of
4621 * left contiguousness. Specifically, if it's an insert into
4622 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004623 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004624 * left. For now, just catch that case and fool the layers
4625 * above us. This works just fine for tree_depth == 0, which
4626 * is why we allow that above.
4627 */
4628 if (insert->ins_contig == CONTIG_LEFT &&
4629 insert->ins_contig_index == 0)
4630 insert->ins_contig = CONTIG_NONE;
4631
4632 /*
4633 * Ok, so we can simply compare against last_eb to figure out
4634 * whether the path doesn't exist. This will only happen in
4635 * the case that we're doing a tail append, so maybe we can
4636 * take advantage of that information somehow.
4637 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004638 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004639 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004640 /*
4641 * Ok, ocfs2_find_path() returned us the rightmost
4642 * tree path. This might be an appending insert. There are
4643 * two cases:
4644 * 1) We're doing a true append at the tail:
4645 * -This might even be off the end of the leaf
4646 * 2) We're "appending" by rotating in the tail
4647 */
4648 ocfs2_figure_appending_type(insert, el, insert_rec);
4649 }
4650
4651out:
4652 ocfs2_free_path(path);
4653
4654 if (ret == 0)
4655 *last_eb_bh = bh;
4656 else
4657 brelse(bh);
4658 return ret;
4659}
4660
4661/*
4662 * Insert an extent into an inode btree.
4663 *
4664 * The caller needs to update fe->i_clusters
4665 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004666int ocfs2_insert_extent(struct ocfs2_super *osb,
4667 handle_t *handle,
4668 struct inode *inode,
4669 struct ocfs2_extent_tree *et,
4670 u32 cpos,
4671 u64 start_blk,
4672 u32 new_clusters,
4673 u8 flags,
4674 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004675{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004676 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004677 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004678 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004679 struct ocfs2_insert_type insert = {0, };
4680 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004681
Mark Fashehdcd05382007-01-16 11:32:23 -08004682 mlog(0, "add %u clusters at position %u to inode %llu\n",
4683 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004684
Mark Fashehe48edee2007-03-07 16:46:57 -08004685 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004686 rec.e_cpos = cpu_to_le32(cpos);
4687 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004688 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004689 rec.e_flags = flags;
Joel Becker6136ca52009-02-12 19:32:43 -08004690 status = ocfs2_et_insert_check(et, &rec);
Joel Becker1e61ee72008-08-20 18:32:45 -07004691 if (status) {
4692 mlog_errno(status);
4693 goto bail;
4694 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004695
Tao Mae7d4cb62008-08-18 17:38:44 +08004696 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004697 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004698 if (status < 0) {
4699 mlog_errno(status);
4700 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004701 }
4702
Mark Fashehdcd05382007-01-16 11:32:23 -08004703 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4704 "Insert.contig_index: %d, Insert.free_records: %d, "
4705 "Insert.tree_depth: %d\n",
4706 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004707 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004708
Tao Maoc77534f2007-08-28 17:22:33 -07004709 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004710 status = ocfs2_grow_tree(handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004711 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004712 meta_ac);
4713 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004714 mlog_errno(status);
4715 goto bail;
4716 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004717 }
4718
Mark Fashehdcd05382007-01-16 11:32:23 -08004719 /* Finally, we can add clusters. This might rotate the tree for us. */
Tao Mae7d4cb62008-08-18 17:38:44 +08004720 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004721 if (status < 0)
4722 mlog_errno(status);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004723 else if (et->et_ops == &ocfs2_dinode_et_ops)
Mark Fasheh83418972007-04-23 18:53:12 -07004724 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004725
4726bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004727 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004728
Tao Maf56654c2008-08-18 17:38:48 +08004729 mlog_exit(status);
4730 return status;
4731}
4732
Tao Ma0eb8d472008-08-18 17:38:45 +08004733/*
4734 * Allcate and add clusters into the extent b-tree.
4735 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004736 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004737 * it is not limited to the file storage. Any extent tree can use this
4738 * function if it implements the proper ocfs2_extent_tree.
4739 */
4740int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4741 struct inode *inode,
4742 u32 *logical_offset,
4743 u32 clusters_to_add,
4744 int mark_unwritten,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004745 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004746 handle_t *handle,
4747 struct ocfs2_alloc_context *data_ac,
4748 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004749 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004750{
4751 int status = 0;
4752 int free_extents;
4753 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4754 u32 bit_off, num_bits;
4755 u64 block;
4756 u8 flags = 0;
4757
4758 BUG_ON(!clusters_to_add);
4759
4760 if (mark_unwritten)
4761 flags = OCFS2_EXT_UNWRITTEN;
4762
Joel Becker3d03a302009-02-12 17:49:26 -08004763 free_extents = ocfs2_num_free_extents(osb, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004764 if (free_extents < 0) {
4765 status = free_extents;
4766 mlog_errno(status);
4767 goto leave;
4768 }
4769
4770 /* there are two cases which could cause us to EAGAIN in the
4771 * we-need-more-metadata case:
4772 * 1) we haven't reserved *any*
4773 * 2) we are so fragmented, we've needed to add metadata too
4774 * many times. */
4775 if (!free_extents && !meta_ac) {
4776 mlog(0, "we haven't reserved any metadata!\n");
4777 status = -EAGAIN;
4778 reason = RESTART_META;
4779 goto leave;
4780 } else if ((!free_extents)
4781 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004782 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004783 mlog(0, "filesystem is really fragmented...\n");
4784 status = -EAGAIN;
4785 reason = RESTART_META;
4786 goto leave;
4787 }
4788
4789 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4790 clusters_to_add, &bit_off, &num_bits);
4791 if (status < 0) {
4792 if (status != -ENOSPC)
4793 mlog_errno(status);
4794 goto leave;
4795 }
4796
4797 BUG_ON(num_bits > clusters_to_add);
4798
Joel Becker13723d02008-10-17 19:25:01 -07004799 /* reserve our write early -- insert_extent may update the tree root */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004800 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004801 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d472008-08-18 17:38:45 +08004802 if (status < 0) {
4803 mlog_errno(status);
4804 goto leave;
4805 }
4806
4807 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4808 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4809 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004810 status = ocfs2_insert_extent(osb, handle, inode, et,
4811 *logical_offset, block,
4812 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004813 if (status < 0) {
4814 mlog_errno(status);
4815 goto leave;
4816 }
4817
Joel Beckerf99b9b72008-08-20 19:36:33 -07004818 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004819 if (status < 0) {
4820 mlog_errno(status);
4821 goto leave;
4822 }
4823
4824 clusters_to_add -= num_bits;
4825 *logical_offset += num_bits;
4826
4827 if (clusters_to_add) {
4828 mlog(0, "need to alloc once more, wanted = %u\n",
4829 clusters_to_add);
4830 status = -EAGAIN;
4831 reason = RESTART_TRANS;
4832 }
4833
4834leave:
4835 mlog_exit(status);
4836 if (reason_ret)
4837 *reason_ret = reason;
4838 return status;
4839}
4840
Mark Fasheh328d5752007-06-18 10:48:04 -07004841static void ocfs2_make_right_split_rec(struct super_block *sb,
4842 struct ocfs2_extent_rec *split_rec,
4843 u32 cpos,
4844 struct ocfs2_extent_rec *rec)
4845{
4846 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4847 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4848
4849 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4850
4851 split_rec->e_cpos = cpu_to_le32(cpos);
4852 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4853
4854 split_rec->e_blkno = rec->e_blkno;
4855 le64_add_cpu(&split_rec->e_blkno,
4856 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4857
4858 split_rec->e_flags = rec->e_flags;
4859}
4860
4861static int ocfs2_split_and_insert(struct inode *inode,
4862 handle_t *handle,
4863 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004864 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004865 struct buffer_head **last_eb_bh,
4866 int split_index,
4867 struct ocfs2_extent_rec *orig_split_rec,
4868 struct ocfs2_alloc_context *meta_ac)
4869{
4870 int ret = 0, depth;
4871 unsigned int insert_range, rec_range, do_leftright = 0;
4872 struct ocfs2_extent_rec tmprec;
4873 struct ocfs2_extent_list *rightmost_el;
4874 struct ocfs2_extent_rec rec;
4875 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4876 struct ocfs2_insert_type insert;
4877 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004878
4879leftright:
4880 /*
4881 * Store a copy of the record on the stack - it might move
4882 * around as the tree is manipulated below.
4883 */
4884 rec = path_leaf_el(path)->l_recs[split_index];
4885
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004886 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004887
4888 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4889 if (depth) {
4890 BUG_ON(!(*last_eb_bh));
4891 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4892 rightmost_el = &eb->h_list;
4893 }
4894
4895 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4896 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004897 ret = ocfs2_grow_tree(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08004898 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004899 if (ret) {
4900 mlog_errno(ret);
4901 goto out;
4902 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004903 }
4904
4905 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4906 insert.ins_appending = APPEND_NONE;
4907 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004908 insert.ins_tree_depth = depth;
4909
4910 insert_range = le32_to_cpu(split_rec.e_cpos) +
4911 le16_to_cpu(split_rec.e_leaf_clusters);
4912 rec_range = le32_to_cpu(rec.e_cpos) +
4913 le16_to_cpu(rec.e_leaf_clusters);
4914
4915 if (split_rec.e_cpos == rec.e_cpos) {
4916 insert.ins_split = SPLIT_LEFT;
4917 } else if (insert_range == rec_range) {
4918 insert.ins_split = SPLIT_RIGHT;
4919 } else {
4920 /*
4921 * Left/right split. We fake this as a right split
4922 * first and then make a second pass as a left split.
4923 */
4924 insert.ins_split = SPLIT_RIGHT;
4925
4926 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4927 &rec);
4928
4929 split_rec = tmprec;
4930
4931 BUG_ON(do_leftright);
4932 do_leftright = 1;
4933 }
4934
Tao Mae7d4cb62008-08-18 17:38:44 +08004935 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004936 if (ret) {
4937 mlog_errno(ret);
4938 goto out;
4939 }
4940
4941 if (do_leftright == 1) {
4942 u32 cpos;
4943 struct ocfs2_extent_list *el;
4944
4945 do_leftright++;
4946 split_rec = *orig_split_rec;
4947
4948 ocfs2_reinit_path(path, 1);
4949
4950 cpos = le32_to_cpu(split_rec.e_cpos);
Joel Beckerfacdb772009-02-12 18:08:48 -08004951 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07004952 if (ret) {
4953 mlog_errno(ret);
4954 goto out;
4955 }
4956
4957 el = path_leaf_el(path);
4958 split_index = ocfs2_search_extent_list(el, cpos);
4959 goto leftright;
4960 }
4961out:
4962
4963 return ret;
4964}
4965
Tao Ma47be12e2009-01-09 07:32:48 +08004966static int ocfs2_replace_extent_rec(struct inode *inode,
4967 handle_t *handle,
4968 struct ocfs2_path *path,
4969 struct ocfs2_extent_list *el,
4970 int split_index,
4971 struct ocfs2_extent_rec *split_rec)
4972{
4973 int ret;
4974
Joel Becker0cf2f762009-02-12 16:41:25 -08004975 ret = ocfs2_path_bh_journal_access(handle, INODE_CACHE(inode), path,
Tao Ma47be12e2009-01-09 07:32:48 +08004976 path_num_items(path) - 1);
4977 if (ret) {
4978 mlog_errno(ret);
4979 goto out;
4980 }
4981
4982 el->l_recs[split_index] = *split_rec;
4983
4984 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4985out:
4986 return ret;
4987}
4988
Mark Fasheh328d5752007-06-18 10:48:04 -07004989/*
4990 * Mark part or all of the extent record at split_index in the leaf
4991 * pointed to by path as written. This removes the unwritten
4992 * extent flag.
4993 *
4994 * Care is taken to handle contiguousness so as to not grow the tree.
4995 *
4996 * meta_ac is not strictly necessary - we only truly need it if growth
4997 * of the tree is required. All other cases will degrade into a less
4998 * optimal tree layout.
4999 *
Tao Mae7d4cb62008-08-18 17:38:44 +08005000 * last_eb_bh should be the rightmost leaf block for any extent
5001 * btree. Since a split may grow the tree or a merge might shrink it,
5002 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07005003 *
5004 * This code is optimized for readability - several passes might be
5005 * made over certain portions of the tree. All of those blocks will
5006 * have been brought into cache (and pinned via the journal), so the
5007 * extra overhead is not expressed in terms of disk reads.
5008 */
5009static int __ocfs2_mark_extent_written(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08005010 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005011 handle_t *handle,
5012 struct ocfs2_path *path,
5013 int split_index,
5014 struct ocfs2_extent_rec *split_rec,
5015 struct ocfs2_alloc_context *meta_ac,
5016 struct ocfs2_cached_dealloc_ctxt *dealloc)
5017{
5018 int ret = 0;
5019 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08005020 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07005021 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5022 struct ocfs2_merge_ctxt ctxt;
5023 struct ocfs2_extent_list *rightmost_el;
5024
Roel Kluin3cf0c502007-10-27 00:20:36 +02005025 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07005026 ret = -EIO;
5027 mlog_errno(ret);
5028 goto out;
5029 }
5030
5031 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5032 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5033 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5034 ret = -EIO;
5035 mlog_errno(ret);
5036 goto out;
5037 }
5038
Tao Maad5a4d72008-01-30 14:21:32 +08005039 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005040 split_index,
5041 split_rec);
5042
5043 /*
5044 * The core merge / split code wants to know how much room is
5045 * left in this inodes allocation tree, so we pass the
5046 * rightmost extent list.
5047 */
5048 if (path->p_tree_depth) {
5049 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005050
Joel Becker3d03a302009-02-12 17:49:26 -08005051 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005052 ocfs2_et_get_last_eb_blk(et),
5053 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005054 if (ret) {
5055 mlog_exit(ret);
5056 goto out;
5057 }
5058
5059 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005060 rightmost_el = &eb->h_list;
5061 } else
5062 rightmost_el = path_root_el(path);
5063
Mark Fasheh328d5752007-06-18 10:48:04 -07005064 if (rec->e_cpos == split_rec->e_cpos &&
5065 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5066 ctxt.c_split_covers_rec = 1;
5067 else
5068 ctxt.c_split_covers_rec = 0;
5069
5070 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5071
Mark Fasheh015452b2007-09-12 10:21:22 -07005072 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5073 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5074 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005075
5076 if (ctxt.c_contig_type == CONTIG_NONE) {
5077 if (ctxt.c_split_covers_rec)
Tao Ma47be12e2009-01-09 07:32:48 +08005078 ret = ocfs2_replace_extent_rec(inode, handle,
5079 path, el,
5080 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005081 else
Tao Mae7d4cb62008-08-18 17:38:44 +08005082 ret = ocfs2_split_and_insert(inode, handle, path, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005083 &last_eb_bh, split_index,
5084 split_rec, meta_ac);
5085 if (ret)
5086 mlog_errno(ret);
5087 } else {
Joel Beckerc495dd22009-02-13 02:19:11 -08005088 ret = ocfs2_try_to_merge_extent(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005089 split_index, split_rec,
Joel Beckerc495dd22009-02-13 02:19:11 -08005090 dealloc, &ctxt);
Mark Fasheh328d5752007-06-18 10:48:04 -07005091 if (ret)
5092 mlog_errno(ret);
5093 }
5094
Mark Fasheh328d5752007-06-18 10:48:04 -07005095out:
5096 brelse(last_eb_bh);
5097 return ret;
5098}
5099
5100/*
5101 * Mark the already-existing extent at cpos as written for len clusters.
5102 *
5103 * If the existing extent is larger than the request, initiate a
5104 * split. An attempt will be made at merging with adjacent extents.
5105 *
5106 * The caller is responsible for passing down meta_ac if we'll need it.
5107 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005108int ocfs2_mark_extent_written(struct inode *inode,
5109 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005110 handle_t *handle, u32 cpos, u32 len, u32 phys,
5111 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005112 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005113{
5114 int ret, index;
5115 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
5116 struct ocfs2_extent_rec split_rec;
5117 struct ocfs2_path *left_path = NULL;
5118 struct ocfs2_extent_list *el;
5119
5120 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5121 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
5122
5123 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5124 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5125 "that are being written to, but the feature bit "
5126 "is not set in the super block.",
5127 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5128 ret = -EROFS;
5129 goto out;
5130 }
5131
5132 /*
5133 * XXX: This should be fixed up so that we just re-insert the
5134 * next extent records.
5135 */
Joel Becker4c911ee2009-02-13 02:50:12 -08005136 ocfs2_et_extent_map_truncate(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07005137
Joel Beckerffdd7a52008-10-17 22:32:01 -07005138 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005139 if (!left_path) {
5140 ret = -ENOMEM;
5141 mlog_errno(ret);
5142 goto out;
5143 }
5144
Joel Beckerfacdb772009-02-12 18:08:48 -08005145 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005146 if (ret) {
5147 mlog_errno(ret);
5148 goto out;
5149 }
5150 el = path_leaf_el(left_path);
5151
5152 index = ocfs2_search_extent_list(el, cpos);
5153 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5154 ocfs2_error(inode->i_sb,
5155 "Inode %llu has an extent at cpos %u which can no "
5156 "longer be found.\n",
5157 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5158 ret = -EROFS;
5159 goto out;
5160 }
5161
5162 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5163 split_rec.e_cpos = cpu_to_le32(cpos);
5164 split_rec.e_leaf_clusters = cpu_to_le16(len);
5165 split_rec.e_blkno = cpu_to_le64(start_blkno);
5166 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
5167 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
5168
Joel Beckerf99b9b72008-08-20 19:36:33 -07005169 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
Tao Mae7d4cb62008-08-18 17:38:44 +08005170 index, &split_rec, meta_ac,
5171 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005172 if (ret)
5173 mlog_errno(ret);
5174
5175out:
5176 ocfs2_free_path(left_path);
5177 return ret;
5178}
5179
Tao Mae7d4cb62008-08-18 17:38:44 +08005180static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005181 handle_t *handle, struct ocfs2_path *path,
5182 int index, u32 new_range,
5183 struct ocfs2_alloc_context *meta_ac)
5184{
5185 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005186 struct buffer_head *last_eb_bh = NULL;
5187 struct ocfs2_extent_block *eb;
5188 struct ocfs2_extent_list *rightmost_el, *el;
5189 struct ocfs2_extent_rec split_rec;
5190 struct ocfs2_extent_rec *rec;
5191 struct ocfs2_insert_type insert;
5192
5193 /*
5194 * Setup the record to split before we grow the tree.
5195 */
5196 el = path_leaf_el(path);
5197 rec = &el->l_recs[index];
5198 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5199
5200 depth = path->p_tree_depth;
5201 if (depth > 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08005202 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005203 ocfs2_et_get_last_eb_blk(et),
5204 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005205 if (ret < 0) {
5206 mlog_errno(ret);
5207 goto out;
5208 }
5209
5210 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5211 rightmost_el = &eb->h_list;
5212 } else
5213 rightmost_el = path_leaf_el(path);
5214
Tao Ma811f9332008-08-18 17:38:43 +08005215 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005216 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005217 ret = ocfs2_extend_trans(handle, credits);
5218 if (ret) {
5219 mlog_errno(ret);
5220 goto out;
5221 }
5222
5223 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5224 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08005225 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005226 meta_ac);
5227 if (ret) {
5228 mlog_errno(ret);
5229 goto out;
5230 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005231 }
5232
5233 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5234 insert.ins_appending = APPEND_NONE;
5235 insert.ins_contig = CONTIG_NONE;
5236 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005237 insert.ins_tree_depth = depth;
5238
Tao Mae7d4cb62008-08-18 17:38:44 +08005239 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005240 if (ret)
5241 mlog_errno(ret);
5242
5243out:
5244 brelse(last_eb_bh);
5245 return ret;
5246}
5247
Joel Becker043beeb2009-02-13 02:42:30 -08005248static int ocfs2_truncate_rec(handle_t *handle,
5249 struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005250 struct ocfs2_path *path, int index,
5251 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker043beeb2009-02-13 02:42:30 -08005252 u32 cpos, u32 len)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005253{
5254 int ret;
5255 u32 left_cpos, rec_range, trunc_range;
5256 int wants_rotate = 0, is_rightmost_tree_rec = 0;
Joel Becker043beeb2009-02-13 02:42:30 -08005257 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005258 struct ocfs2_path *left_path = NULL;
5259 struct ocfs2_extent_list *el = path_leaf_el(path);
5260 struct ocfs2_extent_rec *rec;
5261 struct ocfs2_extent_block *eb;
5262
5263 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Joel Becker70f18c02009-02-13 02:09:31 -08005264 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005265 if (ret) {
5266 mlog_errno(ret);
5267 goto out;
5268 }
5269
5270 index--;
5271 }
5272
5273 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5274 path->p_tree_depth) {
5275 /*
5276 * Check whether this is the rightmost tree record. If
5277 * we remove all of this record or part of its right
5278 * edge then an update of the record lengths above it
5279 * will be required.
5280 */
5281 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5282 if (eb->h_next_leaf_blk == 0)
5283 is_rightmost_tree_rec = 1;
5284 }
5285
5286 rec = &el->l_recs[index];
5287 if (index == 0 && path->p_tree_depth &&
5288 le32_to_cpu(rec->e_cpos) == cpos) {
5289 /*
5290 * Changing the leftmost offset (via partial or whole
5291 * record truncate) of an interior (or rightmost) path
5292 * means we have to update the subtree that is formed
5293 * by this leaf and the one to it's left.
5294 *
5295 * There are two cases we can skip:
Joel Becker043beeb2009-02-13 02:42:30 -08005296 * 1) Path is the leftmost one in our btree.
Mark Fashehd0c7d702007-07-03 13:27:22 -07005297 * 2) The leaf is rightmost and will be empty after
5298 * we remove the extent record - the rotate code
5299 * knows how to update the newly formed edge.
5300 */
5301
Joel Becker043beeb2009-02-13 02:42:30 -08005302 ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005303 if (ret) {
5304 mlog_errno(ret);
5305 goto out;
5306 }
5307
5308 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005309 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005310 if (!left_path) {
5311 ret = -ENOMEM;
5312 mlog_errno(ret);
5313 goto out;
5314 }
5315
Joel Beckerfacdb772009-02-12 18:08:48 -08005316 ret = ocfs2_find_path(et->et_ci, left_path,
5317 left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005318 if (ret) {
5319 mlog_errno(ret);
5320 goto out;
5321 }
5322 }
5323 }
5324
5325 ret = ocfs2_extend_rotate_transaction(handle, 0,
5326 handle->h_buffer_credits,
5327 path);
5328 if (ret) {
5329 mlog_errno(ret);
5330 goto out;
5331 }
5332
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005333 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005334 if (ret) {
5335 mlog_errno(ret);
5336 goto out;
5337 }
5338
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005339 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005340 if (ret) {
5341 mlog_errno(ret);
5342 goto out;
5343 }
5344
5345 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5346 trunc_range = cpos + len;
5347
5348 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5349 int next_free;
5350
5351 memset(rec, 0, sizeof(*rec));
5352 ocfs2_cleanup_merge(el, index);
5353 wants_rotate = 1;
5354
5355 next_free = le16_to_cpu(el->l_next_free_rec);
5356 if (is_rightmost_tree_rec && next_free > 1) {
5357 /*
5358 * We skip the edge update if this path will
5359 * be deleted by the rotate code.
5360 */
5361 rec = &el->l_recs[next_free - 1];
Joel Beckerd401dc12009-02-13 02:24:10 -08005362 ocfs2_adjust_rightmost_records(handle, et, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005363 rec);
5364 }
5365 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5366 /* Remove leftmost portion of the record. */
5367 le32_add_cpu(&rec->e_cpos, len);
5368 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5369 le16_add_cpu(&rec->e_leaf_clusters, -len);
5370 } else if (rec_range == trunc_range) {
5371 /* Remove rightmost portion of the record */
5372 le16_add_cpu(&rec->e_leaf_clusters, -len);
5373 if (is_rightmost_tree_rec)
Joel Beckerd401dc12009-02-13 02:24:10 -08005374 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005375 } else {
5376 /* Caller should have trapped this. */
Joel Becker043beeb2009-02-13 02:42:30 -08005377 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5378 "(%u, %u)\n",
5379 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005380 le32_to_cpu(rec->e_cpos),
5381 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5382 BUG();
5383 }
5384
5385 if (left_path) {
5386 int subtree_index;
5387
Joel Becker7dc02802009-02-12 19:20:13 -08005388 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Joel Becker4619c732009-02-12 19:02:36 -08005389 ocfs2_complete_edge_insert(handle, left_path, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005390 subtree_index);
5391 }
5392
5393 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5394
Joel Becker70f18c02009-02-13 02:09:31 -08005395 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005396 if (ret) {
5397 mlog_errno(ret);
5398 goto out;
5399 }
5400
5401out:
5402 ocfs2_free_path(left_path);
5403 return ret;
5404}
5405
Joel Beckerf99b9b72008-08-20 19:36:33 -07005406int ocfs2_remove_extent(struct inode *inode,
5407 struct ocfs2_extent_tree *et,
Mark Fasheh063c4562007-07-03 13:34:11 -07005408 u32 cpos, u32 len, handle_t *handle,
5409 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005410 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005411{
5412 int ret, index;
5413 u32 rec_range, trunc_range;
5414 struct ocfs2_extent_rec *rec;
5415 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005416 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005417
Joel Becker4c911ee2009-02-13 02:50:12 -08005418 /*
5419 * XXX: Why are we truncating to 0 instead of wherever this
5420 * affects us?
5421 */
5422 ocfs2_et_extent_map_truncate(et, 0);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005423
Joel Beckerffdd7a52008-10-17 22:32:01 -07005424 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005425 if (!path) {
5426 ret = -ENOMEM;
5427 mlog_errno(ret);
5428 goto out;
5429 }
5430
Joel Beckerfacdb772009-02-12 18:08:48 -08005431 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005432 if (ret) {
5433 mlog_errno(ret);
5434 goto out;
5435 }
5436
5437 el = path_leaf_el(path);
5438 index = ocfs2_search_extent_list(el, cpos);
5439 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5440 ocfs2_error(inode->i_sb,
5441 "Inode %llu has an extent at cpos %u which can no "
5442 "longer be found.\n",
5443 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5444 ret = -EROFS;
5445 goto out;
5446 }
5447
5448 /*
5449 * We have 3 cases of extent removal:
5450 * 1) Range covers the entire extent rec
5451 * 2) Range begins or ends on one edge of the extent rec
5452 * 3) Range is in the middle of the extent rec (no shared edges)
5453 *
5454 * For case 1 we remove the extent rec and left rotate to
5455 * fill the hole.
5456 *
5457 * For case 2 we just shrink the existing extent rec, with a
5458 * tree update if the shrinking edge is also the edge of an
5459 * extent block.
5460 *
5461 * For case 3 we do a right split to turn the extent rec into
5462 * something case 2 can handle.
5463 */
5464 rec = &el->l_recs[index];
5465 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5466 trunc_range = cpos + len;
5467
5468 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5469
5470 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5471 "(cpos %u, len %u)\n",
5472 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5473 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5474
5475 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
Joel Becker043beeb2009-02-13 02:42:30 -08005476 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5477 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005478 if (ret) {
5479 mlog_errno(ret);
5480 goto out;
5481 }
5482 } else {
Joel Beckerf99b9b72008-08-20 19:36:33 -07005483 ret = ocfs2_split_tree(inode, et, handle, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005484 trunc_range, meta_ac);
5485 if (ret) {
5486 mlog_errno(ret);
5487 goto out;
5488 }
5489
5490 /*
5491 * The split could have manipulated the tree enough to
5492 * move the record location, so we have to look for it again.
5493 */
5494 ocfs2_reinit_path(path, 1);
5495
Joel Beckerfacdb772009-02-12 18:08:48 -08005496 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005497 if (ret) {
5498 mlog_errno(ret);
5499 goto out;
5500 }
5501
5502 el = path_leaf_el(path);
5503 index = ocfs2_search_extent_list(el, cpos);
5504 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5505 ocfs2_error(inode->i_sb,
5506 "Inode %llu: split at cpos %u lost record.",
5507 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5508 cpos);
5509 ret = -EROFS;
5510 goto out;
5511 }
5512
5513 /*
5514 * Double check our values here. If anything is fishy,
5515 * it's easier to catch it at the top level.
5516 */
5517 rec = &el->l_recs[index];
5518 rec_range = le32_to_cpu(rec->e_cpos) +
5519 ocfs2_rec_clusters(el, rec);
5520 if (rec_range != trunc_range) {
5521 ocfs2_error(inode->i_sb,
5522 "Inode %llu: error after split at cpos %u"
5523 "trunc len %u, existing record is (%u,%u)",
5524 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5525 cpos, len, le32_to_cpu(rec->e_cpos),
5526 ocfs2_rec_clusters(el, rec));
5527 ret = -EROFS;
5528 goto out;
5529 }
5530
Joel Becker043beeb2009-02-13 02:42:30 -08005531 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5532 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005533 if (ret) {
5534 mlog_errno(ret);
5535 goto out;
5536 }
5537 }
5538
5539out:
5540 ocfs2_free_path(path);
5541 return ret;
5542}
5543
Mark Fashehfecc0112008-11-12 15:16:38 -08005544int ocfs2_remove_btree_range(struct inode *inode,
5545 struct ocfs2_extent_tree *et,
5546 u32 cpos, u32 phys_cpos, u32 len,
5547 struct ocfs2_cached_dealloc_ctxt *dealloc)
5548{
5549 int ret;
5550 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5551 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5552 struct inode *tl_inode = osb->osb_tl_inode;
5553 handle_t *handle;
5554 struct ocfs2_alloc_context *meta_ac = NULL;
5555
5556 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5557 if (ret) {
5558 mlog_errno(ret);
5559 return ret;
5560 }
5561
5562 mutex_lock(&tl_inode->i_mutex);
5563
5564 if (ocfs2_truncate_log_needs_flush(osb)) {
5565 ret = __ocfs2_flush_truncate_log(osb);
5566 if (ret < 0) {
5567 mlog_errno(ret);
5568 goto out;
5569 }
5570 }
5571
Jan Karaa90714c2008-10-09 19:38:40 +02005572 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005573 if (IS_ERR(handle)) {
5574 ret = PTR_ERR(handle);
5575 mlog_errno(ret);
5576 goto out;
5577 }
5578
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005579 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07005580 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005581 if (ret) {
5582 mlog_errno(ret);
5583 goto out;
5584 }
5585
Mark Fashehfd4ef232009-01-29 15:06:21 -08005586 vfs_dq_free_space_nodirty(inode,
5587 ocfs2_clusters_to_bytes(inode->i_sb, len));
5588
Mark Fashehfecc0112008-11-12 15:16:38 -08005589 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5590 dealloc);
5591 if (ret) {
5592 mlog_errno(ret);
5593 goto out_commit;
5594 }
5595
Joel Becker6136ca52009-02-12 19:32:43 -08005596 ocfs2_et_update_clusters(et, -len);
Mark Fashehfecc0112008-11-12 15:16:38 -08005597
5598 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5599 if (ret) {
5600 mlog_errno(ret);
5601 goto out_commit;
5602 }
5603
5604 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5605 if (ret)
5606 mlog_errno(ret);
5607
5608out_commit:
5609 ocfs2_commit_trans(osb, handle);
5610out:
5611 mutex_unlock(&tl_inode->i_mutex);
5612
5613 if (meta_ac)
5614 ocfs2_free_alloc_context(meta_ac);
5615
5616 return ret;
5617}
5618
Mark Fasheh063c4562007-07-03 13:34:11 -07005619int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005620{
5621 struct buffer_head *tl_bh = osb->osb_tl_bh;
5622 struct ocfs2_dinode *di;
5623 struct ocfs2_truncate_log *tl;
5624
5625 di = (struct ocfs2_dinode *) tl_bh->b_data;
5626 tl = &di->id2.i_dealloc;
5627
5628 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5629 "slot %d, invalid truncate log parameters: used = "
5630 "%u, count = %u\n", osb->slot_num,
5631 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5632 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5633}
5634
5635static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5636 unsigned int new_start)
5637{
5638 unsigned int tail_index;
5639 unsigned int current_tail;
5640
5641 /* No records, nothing to coalesce */
5642 if (!le16_to_cpu(tl->tl_used))
5643 return 0;
5644
5645 tail_index = le16_to_cpu(tl->tl_used) - 1;
5646 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5647 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5648
5649 return current_tail == new_start;
5650}
5651
Mark Fasheh063c4562007-07-03 13:34:11 -07005652int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5653 handle_t *handle,
5654 u64 start_blk,
5655 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005656{
5657 int status, index;
5658 unsigned int start_cluster, tl_count;
5659 struct inode *tl_inode = osb->osb_tl_inode;
5660 struct buffer_head *tl_bh = osb->osb_tl_bh;
5661 struct ocfs2_dinode *di;
5662 struct ocfs2_truncate_log *tl;
5663
Mark Fashehb06970532006-03-03 10:24:33 -08005664 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5665 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005666
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005667 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005668
5669 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5670
5671 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005672
Joel Becker10995aa2008-11-13 14:49:12 -08005673 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5674 * by the underlying call to ocfs2_read_inode_block(), so any
5675 * corruption is a code bug */
5676 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5677
5678 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005679 tl_count = le16_to_cpu(tl->tl_count);
5680 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5681 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005682 "Truncate record count on #%llu invalid "
5683 "wanted %u, actual %u\n",
5684 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005685 ocfs2_truncate_recs_per_inode(osb->sb),
5686 le16_to_cpu(tl->tl_count));
5687
5688 /* Caller should have known to flush before calling us. */
5689 index = le16_to_cpu(tl->tl_used);
5690 if (index >= tl_count) {
5691 status = -ENOSPC;
5692 mlog_errno(status);
5693 goto bail;
5694 }
5695
Joel Becker0cf2f762009-02-12 16:41:25 -08005696 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005697 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005698 if (status < 0) {
5699 mlog_errno(status);
5700 goto bail;
5701 }
5702
5703 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005704 "%llu (index = %d)\n", num_clusters, start_cluster,
5705 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005706
5707 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5708 /*
5709 * Move index back to the record we are coalescing with.
5710 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5711 */
5712 index--;
5713
5714 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5715 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5716 index, le32_to_cpu(tl->tl_recs[index].t_start),
5717 num_clusters);
5718 } else {
5719 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5720 tl->tl_used = cpu_to_le16(index + 1);
5721 }
5722 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5723
5724 status = ocfs2_journal_dirty(handle, tl_bh);
5725 if (status < 0) {
5726 mlog_errno(status);
5727 goto bail;
5728 }
5729
5730bail:
5731 mlog_exit(status);
5732 return status;
5733}
5734
5735static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005736 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005737 struct inode *data_alloc_inode,
5738 struct buffer_head *data_alloc_bh)
5739{
5740 int status = 0;
5741 int i;
5742 unsigned int num_clusters;
5743 u64 start_blk;
5744 struct ocfs2_truncate_rec rec;
5745 struct ocfs2_dinode *di;
5746 struct ocfs2_truncate_log *tl;
5747 struct inode *tl_inode = osb->osb_tl_inode;
5748 struct buffer_head *tl_bh = osb->osb_tl_bh;
5749
5750 mlog_entry_void();
5751
5752 di = (struct ocfs2_dinode *) tl_bh->b_data;
5753 tl = &di->id2.i_dealloc;
5754 i = le16_to_cpu(tl->tl_used) - 1;
5755 while (i >= 0) {
5756 /* Caller has given us at least enough credits to
5757 * update the truncate log dinode */
Joel Becker0cf2f762009-02-12 16:41:25 -08005758 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005759 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005760 if (status < 0) {
5761 mlog_errno(status);
5762 goto bail;
5763 }
5764
5765 tl->tl_used = cpu_to_le16(i);
5766
5767 status = ocfs2_journal_dirty(handle, tl_bh);
5768 if (status < 0) {
5769 mlog_errno(status);
5770 goto bail;
5771 }
5772
5773 /* TODO: Perhaps we can calculate the bulk of the
5774 * credits up front rather than extending like
5775 * this. */
5776 status = ocfs2_extend_trans(handle,
5777 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5778 if (status < 0) {
5779 mlog_errno(status);
5780 goto bail;
5781 }
5782
5783 rec = tl->tl_recs[i];
5784 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5785 le32_to_cpu(rec.t_start));
5786 num_clusters = le32_to_cpu(rec.t_clusters);
5787
5788 /* if start_blk is not set, we ignore the record as
5789 * invalid. */
5790 if (start_blk) {
5791 mlog(0, "free record %d, start = %u, clusters = %u\n",
5792 i, le32_to_cpu(rec.t_start), num_clusters);
5793
5794 status = ocfs2_free_clusters(handle, data_alloc_inode,
5795 data_alloc_bh, start_blk,
5796 num_clusters);
5797 if (status < 0) {
5798 mlog_errno(status);
5799 goto bail;
5800 }
5801 }
5802 i--;
5803 }
5804
5805bail:
5806 mlog_exit(status);
5807 return status;
5808}
5809
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005810/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005811int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005812{
5813 int status;
5814 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005815 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005816 struct inode *tl_inode = osb->osb_tl_inode;
5817 struct inode *data_alloc_inode = NULL;
5818 struct buffer_head *tl_bh = osb->osb_tl_bh;
5819 struct buffer_head *data_alloc_bh = NULL;
5820 struct ocfs2_dinode *di;
5821 struct ocfs2_truncate_log *tl;
5822
5823 mlog_entry_void();
5824
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005825 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005826
5827 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005828
Joel Becker10995aa2008-11-13 14:49:12 -08005829 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5830 * by the underlying call to ocfs2_read_inode_block(), so any
5831 * corruption is a code bug */
5832 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5833
5834 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005835 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005836 mlog(0, "Flush %u records from truncate log #%llu\n",
5837 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005838 if (!num_to_flush) {
5839 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005840 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005841 }
5842
5843 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5844 GLOBAL_BITMAP_SYSTEM_INODE,
5845 OCFS2_INVALID_SLOT);
5846 if (!data_alloc_inode) {
5847 status = -EINVAL;
5848 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005849 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005850 }
5851
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005852 mutex_lock(&data_alloc_inode->i_mutex);
5853
Mark Fashehe63aecb62007-10-18 15:30:42 -07005854 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005855 if (status < 0) {
5856 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005857 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005858 }
5859
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005860 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005861 if (IS_ERR(handle)) {
5862 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005863 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005864 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005865 }
5866
5867 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5868 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005869 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005870 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005871
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005872 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005873
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005874out_unlock:
5875 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005876 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005877
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005878out_mutex:
5879 mutex_unlock(&data_alloc_inode->i_mutex);
5880 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005881
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005882out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005883 mlog_exit(status);
5884 return status;
5885}
5886
5887int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5888{
5889 int status;
5890 struct inode *tl_inode = osb->osb_tl_inode;
5891
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005892 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005893 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005894 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005895
5896 return status;
5897}
5898
David Howellsc4028952006-11-22 14:57:56 +00005899static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005900{
5901 int status;
David Howellsc4028952006-11-22 14:57:56 +00005902 struct ocfs2_super *osb =
5903 container_of(work, struct ocfs2_super,
5904 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005905
5906 mlog_entry_void();
5907
5908 status = ocfs2_flush_truncate_log(osb);
5909 if (status < 0)
5910 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005911 else
5912 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005913
5914 mlog_exit(status);
5915}
5916
5917#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5918void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5919 int cancel)
5920{
5921 if (osb->osb_tl_inode) {
5922 /* We want to push off log flushes while truncates are
5923 * still running. */
5924 if (cancel)
5925 cancel_delayed_work(&osb->osb_truncate_log_wq);
5926
5927 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5928 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5929 }
5930}
5931
5932static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5933 int slot_num,
5934 struct inode **tl_inode,
5935 struct buffer_head **tl_bh)
5936{
5937 int status;
5938 struct inode *inode = NULL;
5939 struct buffer_head *bh = NULL;
5940
5941 inode = ocfs2_get_system_file_inode(osb,
5942 TRUNCATE_LOG_SYSTEM_INODE,
5943 slot_num);
5944 if (!inode) {
5945 status = -EINVAL;
5946 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5947 goto bail;
5948 }
5949
Joel Beckerb657c952008-11-13 14:49:11 -08005950 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005951 if (status < 0) {
5952 iput(inode);
5953 mlog_errno(status);
5954 goto bail;
5955 }
5956
5957 *tl_inode = inode;
5958 *tl_bh = bh;
5959bail:
5960 mlog_exit(status);
5961 return status;
5962}
5963
5964/* called during the 1st stage of node recovery. we stamp a clean
5965 * truncate log and pass back a copy for processing later. if the
5966 * truncate log does not require processing, a *tl_copy is set to
5967 * NULL. */
5968int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5969 int slot_num,
5970 struct ocfs2_dinode **tl_copy)
5971{
5972 int status;
5973 struct inode *tl_inode = NULL;
5974 struct buffer_head *tl_bh = NULL;
5975 struct ocfs2_dinode *di;
5976 struct ocfs2_truncate_log *tl;
5977
5978 *tl_copy = NULL;
5979
5980 mlog(0, "recover truncate log from slot %d\n", slot_num);
5981
5982 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5983 if (status < 0) {
5984 mlog_errno(status);
5985 goto bail;
5986 }
5987
5988 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005989
Joel Becker10995aa2008-11-13 14:49:12 -08005990 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5991 * validated by the underlying call to ocfs2_read_inode_block(),
5992 * so any corruption is a code bug */
5993 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5994
5995 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005996 if (le16_to_cpu(tl->tl_used)) {
5997 mlog(0, "We'll have %u logs to recover\n",
5998 le16_to_cpu(tl->tl_used));
5999
6000 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6001 if (!(*tl_copy)) {
6002 status = -ENOMEM;
6003 mlog_errno(status);
6004 goto bail;
6005 }
6006
6007 /* Assuming the write-out below goes well, this copy
6008 * will be passed back to recovery for processing. */
6009 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6010
6011 /* All we need to do to clear the truncate log is set
6012 * tl_used. */
6013 tl->tl_used = 0;
6014
Joel Becker13723d02008-10-17 19:25:01 -07006015 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08006016 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08006017 if (status < 0) {
6018 mlog_errno(status);
6019 goto bail;
6020 }
6021 }
6022
6023bail:
6024 if (tl_inode)
6025 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006026 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006027
6028 if (status < 0 && (*tl_copy)) {
6029 kfree(*tl_copy);
6030 *tl_copy = NULL;
6031 }
6032
6033 mlog_exit(status);
6034 return status;
6035}
6036
6037int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6038 struct ocfs2_dinode *tl_copy)
6039{
6040 int status = 0;
6041 int i;
6042 unsigned int clusters, num_recs, start_cluster;
6043 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006044 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006045 struct inode *tl_inode = osb->osb_tl_inode;
6046 struct ocfs2_truncate_log *tl;
6047
6048 mlog_entry_void();
6049
6050 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6051 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6052 return -EINVAL;
6053 }
6054
6055 tl = &tl_copy->id2.i_dealloc;
6056 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08006057 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006058 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006059
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006060 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006061 for(i = 0; i < num_recs; i++) {
6062 if (ocfs2_truncate_log_needs_flush(osb)) {
6063 status = __ocfs2_flush_truncate_log(osb);
6064 if (status < 0) {
6065 mlog_errno(status);
6066 goto bail_up;
6067 }
6068 }
6069
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006070 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006071 if (IS_ERR(handle)) {
6072 status = PTR_ERR(handle);
6073 mlog_errno(status);
6074 goto bail_up;
6075 }
6076
6077 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6078 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6079 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6080
6081 status = ocfs2_truncate_log_append(osb, handle,
6082 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006083 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006084 if (status < 0) {
6085 mlog_errno(status);
6086 goto bail_up;
6087 }
6088 }
6089
6090bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006091 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006092
6093 mlog_exit(status);
6094 return status;
6095}
6096
6097void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6098{
6099 int status;
6100 struct inode *tl_inode = osb->osb_tl_inode;
6101
6102 mlog_entry_void();
6103
6104 if (tl_inode) {
6105 cancel_delayed_work(&osb->osb_truncate_log_wq);
6106 flush_workqueue(ocfs2_wq);
6107
6108 status = ocfs2_flush_truncate_log(osb);
6109 if (status < 0)
6110 mlog_errno(status);
6111
6112 brelse(osb->osb_tl_bh);
6113 iput(osb->osb_tl_inode);
6114 }
6115
6116 mlog_exit_void();
6117}
6118
6119int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6120{
6121 int status;
6122 struct inode *tl_inode = NULL;
6123 struct buffer_head *tl_bh = NULL;
6124
6125 mlog_entry_void();
6126
6127 status = ocfs2_get_truncate_log_info(osb,
6128 osb->slot_num,
6129 &tl_inode,
6130 &tl_bh);
6131 if (status < 0)
6132 mlog_errno(status);
6133
6134 /* ocfs2_truncate_log_shutdown keys on the existence of
6135 * osb->osb_tl_inode so we don't set any of the osb variables
6136 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006137 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6138 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006139 osb->osb_tl_bh = tl_bh;
6140 osb->osb_tl_inode = tl_inode;
6141
6142 mlog_exit(status);
6143 return status;
6144}
6145
Mark Fasheh2b604352007-06-22 15:45:27 -07006146/*
6147 * Delayed de-allocation of suballocator blocks.
6148 *
6149 * Some sets of block de-allocations might involve multiple suballocator inodes.
6150 *
6151 * The locking for this can get extremely complicated, especially when
6152 * the suballocator inodes to delete from aren't known until deep
6153 * within an unrelated codepath.
6154 *
6155 * ocfs2_extent_block structures are a good example of this - an inode
6156 * btree could have been grown by any number of nodes each allocating
6157 * out of their own suballoc inode.
6158 *
6159 * These structures allow the delay of block de-allocation until a
6160 * later time, when locking of multiple cluster inodes won't cause
6161 * deadlock.
6162 */
6163
6164/*
Tao Ma2891d292008-11-12 08:26:58 +08006165 * Describe a single bit freed from a suballocator. For the block
6166 * suballocators, it represents one block. For the global cluster
6167 * allocator, it represents some clusters and free_bit indicates
6168 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006169 */
6170struct ocfs2_cached_block_free {
6171 struct ocfs2_cached_block_free *free_next;
6172 u64 free_blk;
6173 unsigned int free_bit;
6174};
6175
6176struct ocfs2_per_slot_free_list {
6177 struct ocfs2_per_slot_free_list *f_next_suballocator;
6178 int f_inode_type;
6179 int f_slot;
6180 struct ocfs2_cached_block_free *f_first;
6181};
6182
Tao Ma2891d292008-11-12 08:26:58 +08006183static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6184 int sysfile_type,
6185 int slot,
6186 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006187{
6188 int ret;
6189 u64 bg_blkno;
6190 handle_t *handle;
6191 struct inode *inode;
6192 struct buffer_head *di_bh = NULL;
6193 struct ocfs2_cached_block_free *tmp;
6194
6195 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6196 if (!inode) {
6197 ret = -EINVAL;
6198 mlog_errno(ret);
6199 goto out;
6200 }
6201
6202 mutex_lock(&inode->i_mutex);
6203
Mark Fashehe63aecb62007-10-18 15:30:42 -07006204 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006205 if (ret) {
6206 mlog_errno(ret);
6207 goto out_mutex;
6208 }
6209
6210 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6211 if (IS_ERR(handle)) {
6212 ret = PTR_ERR(handle);
6213 mlog_errno(ret);
6214 goto out_unlock;
6215 }
6216
6217 while (head) {
6218 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6219 head->free_bit);
6220 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6221 head->free_bit, (unsigned long long)head->free_blk);
6222
6223 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6224 head->free_bit, bg_blkno, 1);
6225 if (ret) {
6226 mlog_errno(ret);
6227 goto out_journal;
6228 }
6229
6230 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6231 if (ret) {
6232 mlog_errno(ret);
6233 goto out_journal;
6234 }
6235
6236 tmp = head;
6237 head = head->free_next;
6238 kfree(tmp);
6239 }
6240
6241out_journal:
6242 ocfs2_commit_trans(osb, handle);
6243
6244out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006245 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006246 brelse(di_bh);
6247out_mutex:
6248 mutex_unlock(&inode->i_mutex);
6249 iput(inode);
6250out:
6251 while(head) {
6252 /* Premature exit may have left some dangling items. */
6253 tmp = head;
6254 head = head->free_next;
6255 kfree(tmp);
6256 }
6257
6258 return ret;
6259}
6260
Tao Ma2891d292008-11-12 08:26:58 +08006261int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6262 u64 blkno, unsigned int bit)
6263{
6264 int ret = 0;
6265 struct ocfs2_cached_block_free *item;
6266
6267 item = kmalloc(sizeof(*item), GFP_NOFS);
6268 if (item == NULL) {
6269 ret = -ENOMEM;
6270 mlog_errno(ret);
6271 return ret;
6272 }
6273
6274 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6275 bit, (unsigned long long)blkno);
6276
6277 item->free_blk = blkno;
6278 item->free_bit = bit;
6279 item->free_next = ctxt->c_global_allocator;
6280
6281 ctxt->c_global_allocator = item;
6282 return ret;
6283}
6284
6285static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6286 struct ocfs2_cached_block_free *head)
6287{
6288 struct ocfs2_cached_block_free *tmp;
6289 struct inode *tl_inode = osb->osb_tl_inode;
6290 handle_t *handle;
6291 int ret = 0;
6292
6293 mutex_lock(&tl_inode->i_mutex);
6294
6295 while (head) {
6296 if (ocfs2_truncate_log_needs_flush(osb)) {
6297 ret = __ocfs2_flush_truncate_log(osb);
6298 if (ret < 0) {
6299 mlog_errno(ret);
6300 break;
6301 }
6302 }
6303
6304 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6305 if (IS_ERR(handle)) {
6306 ret = PTR_ERR(handle);
6307 mlog_errno(ret);
6308 break;
6309 }
6310
6311 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6312 head->free_bit);
6313
6314 ocfs2_commit_trans(osb, handle);
6315 tmp = head;
6316 head = head->free_next;
6317 kfree(tmp);
6318
6319 if (ret < 0) {
6320 mlog_errno(ret);
6321 break;
6322 }
6323 }
6324
6325 mutex_unlock(&tl_inode->i_mutex);
6326
6327 while (head) {
6328 /* Premature exit may have left some dangling items. */
6329 tmp = head;
6330 head = head->free_next;
6331 kfree(tmp);
6332 }
6333
6334 return ret;
6335}
6336
Mark Fasheh2b604352007-06-22 15:45:27 -07006337int ocfs2_run_deallocs(struct ocfs2_super *osb,
6338 struct ocfs2_cached_dealloc_ctxt *ctxt)
6339{
6340 int ret = 0, ret2;
6341 struct ocfs2_per_slot_free_list *fl;
6342
6343 if (!ctxt)
6344 return 0;
6345
6346 while (ctxt->c_first_suballocator) {
6347 fl = ctxt->c_first_suballocator;
6348
6349 if (fl->f_first) {
6350 mlog(0, "Free items: (type %u, slot %d)\n",
6351 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006352 ret2 = ocfs2_free_cached_blocks(osb,
6353 fl->f_inode_type,
6354 fl->f_slot,
6355 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006356 if (ret2)
6357 mlog_errno(ret2);
6358 if (!ret)
6359 ret = ret2;
6360 }
6361
6362 ctxt->c_first_suballocator = fl->f_next_suballocator;
6363 kfree(fl);
6364 }
6365
Tao Ma2891d292008-11-12 08:26:58 +08006366 if (ctxt->c_global_allocator) {
6367 ret2 = ocfs2_free_cached_clusters(osb,
6368 ctxt->c_global_allocator);
6369 if (ret2)
6370 mlog_errno(ret2);
6371 if (!ret)
6372 ret = ret2;
6373
6374 ctxt->c_global_allocator = NULL;
6375 }
6376
Mark Fasheh2b604352007-06-22 15:45:27 -07006377 return ret;
6378}
6379
6380static struct ocfs2_per_slot_free_list *
6381ocfs2_find_per_slot_free_list(int type,
6382 int slot,
6383 struct ocfs2_cached_dealloc_ctxt *ctxt)
6384{
6385 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6386
6387 while (fl) {
6388 if (fl->f_inode_type == type && fl->f_slot == slot)
6389 return fl;
6390
6391 fl = fl->f_next_suballocator;
6392 }
6393
6394 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6395 if (fl) {
6396 fl->f_inode_type = type;
6397 fl->f_slot = slot;
6398 fl->f_first = NULL;
6399 fl->f_next_suballocator = ctxt->c_first_suballocator;
6400
6401 ctxt->c_first_suballocator = fl;
6402 }
6403 return fl;
6404}
6405
6406static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6407 int type, int slot, u64 blkno,
6408 unsigned int bit)
6409{
6410 int ret;
6411 struct ocfs2_per_slot_free_list *fl;
6412 struct ocfs2_cached_block_free *item;
6413
6414 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6415 if (fl == NULL) {
6416 ret = -ENOMEM;
6417 mlog_errno(ret);
6418 goto out;
6419 }
6420
6421 item = kmalloc(sizeof(*item), GFP_NOFS);
6422 if (item == NULL) {
6423 ret = -ENOMEM;
6424 mlog_errno(ret);
6425 goto out;
6426 }
6427
6428 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6429 type, slot, bit, (unsigned long long)blkno);
6430
6431 item->free_blk = blkno;
6432 item->free_bit = bit;
6433 item->free_next = fl->f_first;
6434
6435 fl->f_first = item;
6436
6437 ret = 0;
6438out:
6439 return ret;
6440}
6441
Mark Fasheh59a5e412007-06-22 15:52:36 -07006442static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6443 struct ocfs2_extent_block *eb)
6444{
6445 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6446 le16_to_cpu(eb->h_suballoc_slot),
6447 le64_to_cpu(eb->h_blkno),
6448 le16_to_cpu(eb->h_suballoc_bit));
6449}
6450
Mark Fashehccd979b2005-12-15 14:31:24 -08006451/* This function will figure out whether the currently last extent
6452 * block will be deleted, and if it will, what the new last extent
6453 * block will be so we can update his h_next_leaf_blk field, as well
6454 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006455static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006456 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006457 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006458 struct buffer_head **new_last_eb)
6459{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006460 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006461 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006462 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006463 struct ocfs2_extent_block *eb;
6464 struct ocfs2_extent_list *el;
6465 struct buffer_head *bh = NULL;
6466
6467 *new_last_eb = NULL;
6468
Mark Fashehccd979b2005-12-15 14:31:24 -08006469 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006470 if (!path->p_tree_depth)
6471 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006472
6473 /* trunc to zero special case - this makes tree_depth = 0
6474 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006475 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006476 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006477
Mark Fashehdcd05382007-01-16 11:32:23 -08006478 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006479 BUG_ON(!el->l_next_free_rec);
6480
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006481 /*
6482 * Make sure that this extent list will actually be empty
6483 * after we clear away the data. We can shortcut out if
6484 * there's more than one non-empty extent in the
6485 * list. Otherwise, a check of the remaining extent is
6486 * necessary.
6487 */
6488 next_free = le16_to_cpu(el->l_next_free_rec);
6489 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006490 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006491 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006492 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006493
6494 /* We may have a valid extent in index 1, check it. */
6495 if (next_free == 2)
6496 rec = &el->l_recs[1];
6497
6498 /*
6499 * Fall through - no more nonempty extents, so we want
6500 * to delete this leaf.
6501 */
6502 } else {
6503 if (next_free > 1)
6504 goto out;
6505
6506 rec = &el->l_recs[0];
6507 }
6508
6509 if (rec) {
6510 /*
6511 * Check it we'll only be trimming off the end of this
6512 * cluster.
6513 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006514 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006515 goto out;
6516 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006517
Mark Fashehdcd05382007-01-16 11:32:23 -08006518 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6519 if (ret) {
6520 mlog_errno(ret);
6521 goto out;
6522 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006523
Joel Beckerfacdb772009-02-12 18:08:48 -08006524 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08006525 if (ret) {
6526 mlog_errno(ret);
6527 goto out;
6528 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006529
Mark Fashehdcd05382007-01-16 11:32:23 -08006530 eb = (struct ocfs2_extent_block *) bh->b_data;
6531 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006532
6533 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6534 * Any corruption is a code bug. */
6535 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006536
6537 *new_last_eb = bh;
6538 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006539 mlog(0, "returning block %llu, (cpos: %u)\n",
6540 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6541out:
6542 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006543
Mark Fashehdcd05382007-01-16 11:32:23 -08006544 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006545}
6546
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006547/*
6548 * Trim some clusters off the rightmost edge of a tree. Only called
6549 * during truncate.
6550 *
6551 * The caller needs to:
6552 * - start journaling of each path component.
6553 * - compute and fully set up any new last ext block
6554 */
6555static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6556 handle_t *handle, struct ocfs2_truncate_context *tc,
6557 u32 clusters_to_del, u64 *delete_start)
6558{
6559 int ret, i, index = path->p_tree_depth;
6560 u32 new_edge = 0;
6561 u64 deleted_eb = 0;
6562 struct buffer_head *bh;
6563 struct ocfs2_extent_list *el;
6564 struct ocfs2_extent_rec *rec;
6565
6566 *delete_start = 0;
6567
6568 while (index >= 0) {
6569 bh = path->p_node[index].bh;
6570 el = path->p_node[index].el;
6571
6572 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6573 index, (unsigned long long)bh->b_blocknr);
6574
6575 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6576
6577 if (index !=
6578 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6579 ocfs2_error(inode->i_sb,
6580 "Inode %lu has invalid ext. block %llu",
6581 inode->i_ino,
6582 (unsigned long long)bh->b_blocknr);
6583 ret = -EROFS;
6584 goto out;
6585 }
6586
6587find_tail_record:
6588 i = le16_to_cpu(el->l_next_free_rec) - 1;
6589 rec = &el->l_recs[i];
6590
6591 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6592 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006593 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006594 (unsigned long long)le64_to_cpu(rec->e_blkno),
6595 le16_to_cpu(el->l_next_free_rec));
6596
Mark Fashehe48edee2007-03-07 16:46:57 -08006597 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006598
6599 if (le16_to_cpu(el->l_tree_depth) == 0) {
6600 /*
6601 * If the leaf block contains a single empty
6602 * extent and no records, we can just remove
6603 * the block.
6604 */
6605 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6606 memset(rec, 0,
6607 sizeof(struct ocfs2_extent_rec));
6608 el->l_next_free_rec = cpu_to_le16(0);
6609
6610 goto delete;
6611 }
6612
6613 /*
6614 * Remove any empty extents by shifting things
6615 * left. That should make life much easier on
6616 * the code below. This condition is rare
6617 * enough that we shouldn't see a performance
6618 * hit.
6619 */
6620 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6621 le16_add_cpu(&el->l_next_free_rec, -1);
6622
6623 for(i = 0;
6624 i < le16_to_cpu(el->l_next_free_rec); i++)
6625 el->l_recs[i] = el->l_recs[i + 1];
6626
6627 memset(&el->l_recs[i], 0,
6628 sizeof(struct ocfs2_extent_rec));
6629
6630 /*
6631 * We've modified our extent list. The
6632 * simplest way to handle this change
6633 * is to being the search from the
6634 * start again.
6635 */
6636 goto find_tail_record;
6637 }
6638
Mark Fashehe48edee2007-03-07 16:46:57 -08006639 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006640
6641 /*
6642 * We'll use "new_edge" on our way back up the
6643 * tree to know what our rightmost cpos is.
6644 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006645 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006646 new_edge += le32_to_cpu(rec->e_cpos);
6647
6648 /*
6649 * The caller will use this to delete data blocks.
6650 */
6651 *delete_start = le64_to_cpu(rec->e_blkno)
6652 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006653 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006654
6655 /*
6656 * If it's now empty, remove this record.
6657 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006658 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006659 memset(rec, 0,
6660 sizeof(struct ocfs2_extent_rec));
6661 le16_add_cpu(&el->l_next_free_rec, -1);
6662 }
6663 } else {
6664 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6665 memset(rec, 0,
6666 sizeof(struct ocfs2_extent_rec));
6667 le16_add_cpu(&el->l_next_free_rec, -1);
6668
6669 goto delete;
6670 }
6671
6672 /* Can this actually happen? */
6673 if (le16_to_cpu(el->l_next_free_rec) == 0)
6674 goto delete;
6675
6676 /*
6677 * We never actually deleted any clusters
6678 * because our leaf was empty. There's no
6679 * reason to adjust the rightmost edge then.
6680 */
6681 if (new_edge == 0)
6682 goto delete;
6683
Mark Fashehe48edee2007-03-07 16:46:57 -08006684 rec->e_int_clusters = cpu_to_le32(new_edge);
6685 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006686 -le32_to_cpu(rec->e_cpos));
6687
6688 /*
6689 * A deleted child record should have been
6690 * caught above.
6691 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006692 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006693 }
6694
6695delete:
6696 ret = ocfs2_journal_dirty(handle, bh);
6697 if (ret) {
6698 mlog_errno(ret);
6699 goto out;
6700 }
6701
6702 mlog(0, "extent list container %llu, after: record %d: "
6703 "(%u, %u, %llu), next = %u.\n",
6704 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006705 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006706 (unsigned long long)le64_to_cpu(rec->e_blkno),
6707 le16_to_cpu(el->l_next_free_rec));
6708
6709 /*
6710 * We must be careful to only attempt delete of an
6711 * extent block (and not the root inode block).
6712 */
6713 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6714 struct ocfs2_extent_block *eb =
6715 (struct ocfs2_extent_block *)bh->b_data;
6716
6717 /*
6718 * Save this for use when processing the
6719 * parent block.
6720 */
6721 deleted_eb = le64_to_cpu(eb->h_blkno);
6722
6723 mlog(0, "deleting this extent block.\n");
6724
Joel Becker8cb471e2009-02-10 20:00:41 -08006725 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006726
Mark Fashehe48edee2007-03-07 16:46:57 -08006727 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006728 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6729 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6730
Mark Fasheh59a5e412007-06-22 15:52:36 -07006731 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6732 /* An error here is not fatal. */
6733 if (ret < 0)
6734 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006735 } else {
6736 deleted_eb = 0;
6737 }
6738
6739 index--;
6740 }
6741
6742 ret = 0;
6743out:
6744 return ret;
6745}
6746
Mark Fashehccd979b2005-12-15 14:31:24 -08006747static int ocfs2_do_truncate(struct ocfs2_super *osb,
6748 unsigned int clusters_to_del,
6749 struct inode *inode,
6750 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006751 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006752 struct ocfs2_truncate_context *tc,
6753 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006754{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006755 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006756 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006757 struct ocfs2_extent_block *last_eb = NULL;
6758 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006759 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006760 u64 delete_blk = 0;
6761
6762 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6763
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006764 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006765 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006766 if (status < 0) {
6767 mlog_errno(status);
6768 goto bail;
6769 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006770
Mark Fashehdcd05382007-01-16 11:32:23 -08006771 /*
6772 * Each component will be touched, so we might as well journal
6773 * here to avoid having to handle errors later.
6774 */
Joel Becker0cf2f762009-02-12 16:41:25 -08006775 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006776 if (status < 0) {
6777 mlog_errno(status);
6778 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006779 }
6780
6781 if (last_eb_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08006782 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07006783 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08006784 if (status < 0) {
6785 mlog_errno(status);
6786 goto bail;
6787 }
6788
6789 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6790 }
6791
6792 el = &(fe->id2.i_list);
6793
6794 /*
6795 * Lower levels depend on this never happening, but it's best
6796 * to check it up here before changing the tree.
6797 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006798 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006799 ocfs2_error(inode->i_sb,
6800 "Inode %lu has an empty extent record, depth %u\n",
6801 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006802 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006803 goto bail;
6804 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006805
Jan Karaa90714c2008-10-09 19:38:40 +02006806 vfs_dq_free_space_nodirty(inode,
6807 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006808 spin_lock(&OCFS2_I(inode)->ip_lock);
6809 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6810 clusters_to_del;
6811 spin_unlock(&OCFS2_I(inode)->ip_lock);
6812 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006813 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006814
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006815 status = ocfs2_trim_tree(inode, path, handle, tc,
6816 clusters_to_del, &delete_blk);
6817 if (status) {
6818 mlog_errno(status);
6819 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006820 }
6821
Mark Fashehdcd05382007-01-16 11:32:23 -08006822 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006823 /* trunc to zero is a special case. */
6824 el->l_tree_depth = 0;
6825 fe->i_last_eb_blk = 0;
6826 } else if (last_eb)
6827 fe->i_last_eb_blk = last_eb->h_blkno;
6828
6829 status = ocfs2_journal_dirty(handle, fe_bh);
6830 if (status < 0) {
6831 mlog_errno(status);
6832 goto bail;
6833 }
6834
6835 if (last_eb) {
6836 /* If there will be a new last extent block, then by
6837 * definition, there cannot be any leaves to the right of
6838 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006839 last_eb->h_next_leaf_blk = 0;
6840 status = ocfs2_journal_dirty(handle, last_eb_bh);
6841 if (status < 0) {
6842 mlog_errno(status);
6843 goto bail;
6844 }
6845 }
6846
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006847 if (delete_blk) {
6848 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6849 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006850 if (status < 0) {
6851 mlog_errno(status);
6852 goto bail;
6853 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006854 }
6855 status = 0;
6856bail:
Tao Ma60e2ec42009-08-12 14:42:47 +08006857 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006858 mlog_exit(status);
6859 return status;
6860}
6861
Joel Becker2b4e30f2008-09-03 20:03:41 -07006862static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006863{
6864 set_buffer_uptodate(bh);
6865 mark_buffer_dirty(bh);
6866 return 0;
6867}
6868
Mark Fasheh1d410a62007-09-07 14:20:45 -07006869static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6870 unsigned int from, unsigned int to,
6871 struct page *page, int zero, u64 *phys)
6872{
6873 int ret, partial = 0;
6874
6875 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6876 if (ret)
6877 mlog_errno(ret);
6878
6879 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006880 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006881
6882 /*
6883 * Need to set the buffers we zero'd into uptodate
6884 * here if they aren't - ocfs2_map_page_blocks()
6885 * might've skipped some
6886 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006887 ret = walk_page_buffers(handle, page_buffers(page),
6888 from, to, &partial,
6889 ocfs2_zero_func);
6890 if (ret < 0)
6891 mlog_errno(ret);
6892 else if (ocfs2_should_order_data(inode)) {
6893 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006894 if (ret < 0)
6895 mlog_errno(ret);
6896 }
6897
6898 if (!partial)
6899 SetPageUptodate(page);
6900
6901 flush_dcache_page(page);
6902}
6903
Mark Fasheh35edec12007-07-06 14:41:18 -07006904static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6905 loff_t end, struct page **pages,
6906 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006907{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006908 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006909 struct page *page;
6910 unsigned int from, to = PAGE_CACHE_SIZE;
6911 struct super_block *sb = inode->i_sb;
6912
6913 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6914
6915 if (numpages == 0)
6916 goto out;
6917
Mark Fasheh35edec12007-07-06 14:41:18 -07006918 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006919 for(i = 0; i < numpages; i++) {
6920 page = pages[i];
6921
Mark Fasheh35edec12007-07-06 14:41:18 -07006922 from = start & (PAGE_CACHE_SIZE - 1);
6923 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6924 to = end & (PAGE_CACHE_SIZE - 1);
6925
Mark Fasheh60b11392007-02-16 11:46:50 -08006926 BUG_ON(from > PAGE_CACHE_SIZE);
6927 BUG_ON(to > PAGE_CACHE_SIZE);
6928
Mark Fasheh1d410a62007-09-07 14:20:45 -07006929 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6930 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006931
Mark Fasheh35edec12007-07-06 14:41:18 -07006932 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006933 }
6934out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006935 if (pages)
6936 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006937}
6938
Mark Fasheh35edec12007-07-06 14:41:18 -07006939static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006940 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006941{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006942 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006943 struct super_block *sb = inode->i_sb;
6944 struct address_space *mapping = inode->i_mapping;
6945 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006946 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006947
Mark Fasheh35edec12007-07-06 14:41:18 -07006948 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006949
Mark Fasheh35edec12007-07-06 14:41:18 -07006950 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6951 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6952
Mark Fasheh1d410a62007-09-07 14:20:45 -07006953 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006954 last_page_bytes = PAGE_ALIGN(end);
6955 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006956 do {
6957 pages[numpages] = grab_cache_page(mapping, index);
6958 if (!pages[numpages]) {
6959 ret = -ENOMEM;
6960 mlog_errno(ret);
6961 goto out;
6962 }
6963
6964 numpages++;
6965 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006966 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006967
6968out:
6969 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006970 if (pages)
6971 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006972 numpages = 0;
6973 }
6974
6975 *num = numpages;
6976
6977 return ret;
6978}
6979
6980/*
6981 * Zero the area past i_size but still within an allocated
6982 * cluster. This avoids exposing nonzero data on subsequent file
6983 * extends.
6984 *
6985 * We need to call this before i_size is updated on the inode because
6986 * otherwise block_write_full_page() will skip writeout of pages past
6987 * i_size. The new_i_size parameter is passed for this reason.
6988 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006989int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6990 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006991{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006992 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006993 struct page **pages = NULL;
6994 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006995 unsigned int ext_flags;
6996 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006997
6998 /*
6999 * File systems which don't support sparse files zero on every
7000 * extend.
7001 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07007002 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08007003 return 0;
7004
Mark Fasheh1d410a62007-09-07 14:20:45 -07007005 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08007006 sizeof(struct page *), GFP_NOFS);
7007 if (pages == NULL) {
7008 ret = -ENOMEM;
7009 mlog_errno(ret);
7010 goto out;
7011 }
7012
Mark Fasheh1d410a62007-09-07 14:20:45 -07007013 if (range_start == range_end)
7014 goto out;
7015
7016 ret = ocfs2_extent_map_get_blocks(inode,
7017 range_start >> sb->s_blocksize_bits,
7018 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08007019 if (ret) {
7020 mlog_errno(ret);
7021 goto out;
7022 }
7023
Mark Fasheh1d410a62007-09-07 14:20:45 -07007024 /*
7025 * Tail is a hole, or is marked unwritten. In either case, we
7026 * can count on read and write to return/push zero's.
7027 */
7028 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08007029 goto out;
7030
Mark Fasheh1d410a62007-09-07 14:20:45 -07007031 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7032 &numpages);
7033 if (ret) {
7034 mlog_errno(ret);
7035 goto out;
7036 }
7037
Mark Fasheh35edec12007-07-06 14:41:18 -07007038 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7039 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08007040
7041 /*
7042 * Initiate writeout of the pages we zero'd here. We don't
7043 * wait on them - the truncate_inode_pages() call later will
7044 * do that for us.
7045 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007046 ret = do_sync_mapping_range(inode->i_mapping, range_start,
7047 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08007048 if (ret)
7049 mlog_errno(ret);
7050
7051out:
7052 if (pages)
7053 kfree(pages);
7054
7055 return ret;
7056}
7057
Tiger Yangfdd77702008-08-18 17:08:55 +08007058static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7059 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007060{
7061 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08007062 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007063
Tiger Yangfdd77702008-08-18 17:08:55 +08007064 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7065 memset(&di->id2, 0, blocksize -
7066 offsetof(struct ocfs2_dinode, id2) -
7067 xattrsize);
7068 else
7069 memset(&di->id2, 0, blocksize -
7070 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007071}
7072
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007073void ocfs2_dinode_new_extent_list(struct inode *inode,
7074 struct ocfs2_dinode *di)
7075{
Tiger Yangfdd77702008-08-18 17:08:55 +08007076 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007077 di->id2.i_list.l_tree_depth = 0;
7078 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08007079 di->id2.i_list.l_count = cpu_to_le16(
7080 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007081}
7082
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007083void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7084{
7085 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7086 struct ocfs2_inline_data *idata = &di->id2.i_data;
7087
7088 spin_lock(&oi->ip_lock);
7089 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7090 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7091 spin_unlock(&oi->ip_lock);
7092
7093 /*
7094 * We clear the entire i_data structure here so that all
7095 * fields can be properly initialized.
7096 */
Tiger Yangfdd77702008-08-18 17:08:55 +08007097 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007098
Tiger Yangfdd77702008-08-18 17:08:55 +08007099 idata->id_count = cpu_to_le16(
7100 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007101}
7102
7103int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7104 struct buffer_head *di_bh)
7105{
7106 int ret, i, has_data, num_pages = 0;
7107 handle_t *handle;
7108 u64 uninitialized_var(block);
7109 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7110 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7111 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007112 struct ocfs2_alloc_context *data_ac = NULL;
7113 struct page **pages = NULL;
7114 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07007115 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02007116 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007117
7118 has_data = i_size_read(inode) ? 1 : 0;
7119
7120 if (has_data) {
7121 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7122 sizeof(struct page *), GFP_NOFS);
7123 if (pages == NULL) {
7124 ret = -ENOMEM;
7125 mlog_errno(ret);
7126 goto out;
7127 }
7128
7129 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7130 if (ret) {
7131 mlog_errno(ret);
7132 goto out;
7133 }
7134 }
7135
Jan Karaa90714c2008-10-09 19:38:40 +02007136 handle = ocfs2_start_trans(osb,
7137 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007138 if (IS_ERR(handle)) {
7139 ret = PTR_ERR(handle);
7140 mlog_errno(ret);
7141 goto out_unlock;
7142 }
7143
Joel Becker0cf2f762009-02-12 16:41:25 -08007144 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007145 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007146 if (ret) {
7147 mlog_errno(ret);
7148 goto out_commit;
7149 }
7150
7151 if (has_data) {
7152 u32 bit_off, num;
7153 unsigned int page_end;
7154 u64 phys;
7155
Jan Karaa90714c2008-10-09 19:38:40 +02007156 if (vfs_dq_alloc_space_nodirty(inode,
7157 ocfs2_clusters_to_bytes(osb->sb, 1))) {
7158 ret = -EDQUOT;
7159 goto out_commit;
7160 }
7161 did_quota = 1;
7162
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007163 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7164 &num);
7165 if (ret) {
7166 mlog_errno(ret);
7167 goto out_commit;
7168 }
7169
7170 /*
7171 * Save two copies, one for insert, and one that can
7172 * be changed by ocfs2_map_and_dirty_page() below.
7173 */
7174 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7175
7176 /*
7177 * Non sparse file systems zero on extend, so no need
7178 * to do that now.
7179 */
7180 if (!ocfs2_sparse_alloc(osb) &&
7181 PAGE_CACHE_SIZE < osb->s_clustersize)
7182 end = PAGE_CACHE_SIZE;
7183
7184 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7185 if (ret) {
7186 mlog_errno(ret);
7187 goto out_commit;
7188 }
7189
7190 /*
7191 * This should populate the 1st page for us and mark
7192 * it up to date.
7193 */
7194 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7195 if (ret) {
7196 mlog_errno(ret);
7197 goto out_commit;
7198 }
7199
7200 page_end = PAGE_CACHE_SIZE;
7201 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7202 page_end = osb->s_clustersize;
7203
7204 for (i = 0; i < num_pages; i++)
7205 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7206 pages[i], i > 0, &phys);
7207 }
7208
7209 spin_lock(&oi->ip_lock);
7210 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7211 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7212 spin_unlock(&oi->ip_lock);
7213
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007214 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007215
7216 ocfs2_journal_dirty(handle, di_bh);
7217
7218 if (has_data) {
7219 /*
7220 * An error at this point should be extremely rare. If
7221 * this proves to be false, we could always re-build
7222 * the in-inode data from our pages.
7223 */
Joel Becker8d6220d2008-08-22 12:46:09 -07007224 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07007225 ret = ocfs2_insert_extent(osb, handle, inode, &et,
7226 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007227 if (ret) {
7228 mlog_errno(ret);
7229 goto out_commit;
7230 }
7231
7232 inode->i_blocks = ocfs2_inode_sector_count(inode);
7233 }
7234
7235out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007236 if (ret < 0 && did_quota)
7237 vfs_dq_free_space_nodirty(inode,
7238 ocfs2_clusters_to_bytes(osb->sb, 1));
7239
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007240 ocfs2_commit_trans(osb, handle);
7241
7242out_unlock:
7243 if (data_ac)
7244 ocfs2_free_alloc_context(data_ac);
7245
7246out:
7247 if (pages) {
7248 ocfs2_unlock_and_free_pages(pages, num_pages);
7249 kfree(pages);
7250 }
7251
7252 return ret;
7253}
7254
Mark Fashehccd979b2005-12-15 14:31:24 -08007255/*
7256 * It is expected, that by the time you call this function,
7257 * inode->i_size and fe->i_size have been adjusted.
7258 *
7259 * WARNING: This will kfree the truncate context
7260 */
7261int ocfs2_commit_truncate(struct ocfs2_super *osb,
7262 struct inode *inode,
7263 struct buffer_head *fe_bh,
7264 struct ocfs2_truncate_context *tc)
7265{
7266 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08007267 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08007268 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07007269 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007270 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08007271 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08007272 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007273
7274 mlog_entry_void();
7275
Mark Fashehdcd05382007-01-16 11:32:23 -08007276 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007277 i_size_read(inode));
7278
Joel Becker13723d02008-10-17 19:25:01 -07007279 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7280 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08007281 if (!path) {
7282 status = -ENOMEM;
7283 mlog_errno(status);
7284 goto bail;
7285 }
Mark Fasheh83418972007-04-23 18:53:12 -07007286
7287 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7288
Mark Fashehccd979b2005-12-15 14:31:24 -08007289start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007290 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007291 * Check that we still have allocation to delete.
7292 */
7293 if (OCFS2_I(inode)->ip_clusters == 0) {
7294 status = 0;
7295 goto bail;
7296 }
7297
7298 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007299 * Truncate always works against the rightmost tree branch.
7300 */
Joel Beckerfacdb772009-02-12 18:08:48 -08007301 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
Mark Fashehdcd05382007-01-16 11:32:23 -08007302 if (status) {
7303 mlog_errno(status);
7304 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007305 }
7306
Mark Fashehdcd05382007-01-16 11:32:23 -08007307 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7308 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7309
7310 /*
7311 * By now, el will point to the extent list on the bottom most
7312 * portion of this tree. Only the tail record is considered in
7313 * each pass.
7314 *
7315 * We handle the following cases, in order:
7316 * - empty extent: delete the remaining branch
7317 * - remove the entire record
7318 * - remove a partial record
7319 * - no record needs to be removed (truncate has completed)
7320 */
7321 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007322 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7323 ocfs2_error(inode->i_sb,
7324 "Inode %llu has empty extent block at %llu\n",
7325 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7326 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7327 status = -EROFS;
7328 goto bail;
7329 }
7330
Mark Fashehccd979b2005-12-15 14:31:24 -08007331 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08007332 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08007333 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007334 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7335 clusters_to_del = 0;
7336 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007337 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007338 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007339 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08007340 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08007341 new_highest_cpos;
7342 } else {
7343 status = 0;
7344 goto bail;
7345 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007346
Mark Fashehdcd05382007-01-16 11:32:23 -08007347 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7348 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7349
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007350 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007351 tl_sem = 1;
7352 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7353 * record is free for use. If there isn't any, we flush to get
7354 * an empty truncate log. */
7355 if (ocfs2_truncate_log_needs_flush(osb)) {
7356 status = __ocfs2_flush_truncate_log(osb);
7357 if (status < 0) {
7358 mlog_errno(status);
7359 goto bail;
7360 }
7361 }
7362
7363 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007364 (struct ocfs2_dinode *)fe_bh->b_data,
7365 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007366 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007367 if (IS_ERR(handle)) {
7368 status = PTR_ERR(handle);
7369 handle = NULL;
7370 mlog_errno(status);
7371 goto bail;
7372 }
7373
Mark Fashehdcd05382007-01-16 11:32:23 -08007374 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7375 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007376 if (status < 0) {
7377 mlog_errno(status);
7378 goto bail;
7379 }
7380
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007381 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007382 tl_sem = 0;
7383
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007384 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007385 handle = NULL;
7386
Mark Fashehdcd05382007-01-16 11:32:23 -08007387 ocfs2_reinit_path(path, 1);
7388
7389 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007390 * The check above will catch the case where we've truncated
7391 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007392 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007393 goto start;
7394
Mark Fashehccd979b2005-12-15 14:31:24 -08007395bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007396
7397 ocfs2_schedule_truncate_log_flush(osb, 1);
7398
7399 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007400 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007401
7402 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007403 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007404
Mark Fasheh59a5e412007-06-22 15:52:36 -07007405 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7406
Mark Fashehdcd05382007-01-16 11:32:23 -08007407 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007408
7409 /* This will drop the ext_alloc cluster lock for us */
7410 ocfs2_free_truncate_context(tc);
7411
7412 mlog_exit(status);
7413 return status;
7414}
7415
Mark Fashehccd979b2005-12-15 14:31:24 -08007416/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007417 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007418 */
7419int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7420 struct inode *inode,
7421 struct buffer_head *fe_bh,
7422 struct ocfs2_truncate_context **tc)
7423{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007424 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007425 unsigned int new_i_clusters;
7426 struct ocfs2_dinode *fe;
7427 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007428 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007429
7430 mlog_entry_void();
7431
7432 *tc = NULL;
7433
7434 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7435 i_size_read(inode));
7436 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7437
7438 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007439 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7440 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007441
Robert P. J. Daycd861282006-12-13 00:34:52 -08007442 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007443 if (!(*tc)) {
7444 status = -ENOMEM;
7445 mlog_errno(status);
7446 goto bail;
7447 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007448 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007449
Mark Fashehccd979b2005-12-15 14:31:24 -08007450 if (fe->id2.i_list.l_tree_depth) {
Joel Becker3d03a302009-02-12 17:49:26 -08007451 status = ocfs2_read_extent_block(INODE_CACHE(inode),
Joel Becker5e965812008-11-13 14:49:16 -08007452 le64_to_cpu(fe->i_last_eb_blk),
7453 &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007454 if (status < 0) {
7455 mlog_errno(status);
7456 goto bail;
7457 }
7458 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007459 }
7460
7461 (*tc)->tc_last_eb_bh = last_eb_bh;
7462
Mark Fashehccd979b2005-12-15 14:31:24 -08007463 status = 0;
7464bail:
7465 if (status < 0) {
7466 if (*tc)
7467 ocfs2_free_truncate_context(*tc);
7468 *tc = NULL;
7469 }
7470 mlog_exit_void();
7471 return status;
7472}
7473
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007474/*
7475 * 'start' is inclusive, 'end' is not.
7476 */
7477int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7478 unsigned int start, unsigned int end, int trunc)
7479{
7480 int ret;
7481 unsigned int numbytes;
7482 handle_t *handle;
7483 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7484 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7485 struct ocfs2_inline_data *idata = &di->id2.i_data;
7486
7487 if (end > i_size_read(inode))
7488 end = i_size_read(inode);
7489
7490 BUG_ON(start >= end);
7491
7492 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7493 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7494 !ocfs2_supports_inline_data(osb)) {
7495 ocfs2_error(inode->i_sb,
7496 "Inline data flags for inode %llu don't agree! "
7497 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7498 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7499 le16_to_cpu(di->i_dyn_features),
7500 OCFS2_I(inode)->ip_dyn_features,
7501 osb->s_feature_incompat);
7502 ret = -EROFS;
7503 goto out;
7504 }
7505
7506 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7507 if (IS_ERR(handle)) {
7508 ret = PTR_ERR(handle);
7509 mlog_errno(ret);
7510 goto out;
7511 }
7512
Joel Becker0cf2f762009-02-12 16:41:25 -08007513 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007514 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007515 if (ret) {
7516 mlog_errno(ret);
7517 goto out_commit;
7518 }
7519
7520 numbytes = end - start;
7521 memset(idata->id_data + start, 0, numbytes);
7522
7523 /*
7524 * No need to worry about the data page here - it's been
7525 * truncated already and inline data doesn't need it for
7526 * pushing zero's to disk, so we'll let readpage pick it up
7527 * later.
7528 */
7529 if (trunc) {
7530 i_size_write(inode, start);
7531 di->i_size = cpu_to_le64(start);
7532 }
7533
7534 inode->i_blocks = ocfs2_inode_sector_count(inode);
7535 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7536
7537 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7538 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7539
7540 ocfs2_journal_dirty(handle, di_bh);
7541
7542out_commit:
7543 ocfs2_commit_trans(osb, handle);
7544
7545out:
7546 return ret;
7547}
7548
Mark Fashehccd979b2005-12-15 14:31:24 -08007549static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7550{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007551 /*
7552 * The caller is responsible for completing deallocation
7553 * before freeing the context.
7554 */
7555 if (tc->tc_dealloc.c_first_suballocator != NULL)
7556 mlog(ML_NOTICE,
7557 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007558
Mark Fasheha81cb882008-10-07 14:25:16 -07007559 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007560
7561 kfree(tc);
7562}