blob: 6e58fd557e5bd9e942118627e68c730653a400ce [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"
51
52#include "buffer_head_io.h"
53
Tao Mae7d4cb62008-08-18 17:38:44 +080054
Joel Becker1625f8a2008-08-21 17:11:10 -070055/*
56 * Operations for a specific extent tree type.
57 *
58 * To implement an on-disk btree (extent tree) type in ocfs2, add
59 * an ocfs2_extent_tree_operations structure and the matching
Joel Becker8d6220d2008-08-22 12:46:09 -070060 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
Joel Becker1625f8a2008-08-21 17:11:10 -070061 * for the allocation portion of the extent tree.
62 */
Tao Mae7d4cb62008-08-18 17:38:44 +080063struct ocfs2_extent_tree_operations {
Joel Becker1625f8a2008-08-21 17:11:10 -070064 /*
65 * last_eb_blk is the block number of the right most leaf extent
66 * block. Most on-disk structures containing an extent tree store
67 * this value for fast access. The ->eo_set_last_eb_blk() and
68 * ->eo_get_last_eb_blk() operations access this value. They are
69 * both required.
70 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070071 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
72 u64 blkno);
73 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -070074
75 /*
76 * The on-disk structure usually keeps track of how many total
77 * clusters are stored in this extent tree. This function updates
78 * that value. new_clusters is the delta, and must be
79 * added to the total. Required.
80 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070081 void (*eo_update_clusters)(struct inode *inode,
82 struct ocfs2_extent_tree *et,
83 u32 new_clusters);
Joel Becker1625f8a2008-08-21 17:11:10 -070084
85 /*
86 * If ->eo_insert_check() exists, it is called before rec is
87 * inserted into the extent tree. It is optional.
88 */
Joel Becker1e61ee72008-08-20 18:32:45 -070089 int (*eo_insert_check)(struct inode *inode,
90 struct ocfs2_extent_tree *et,
91 struct ocfs2_extent_rec *rec);
Joel Becker35dc0aa2008-08-20 16:25:06 -070092 int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
Joel Becker0ce10102008-08-20 17:19:50 -070093
Joel Becker1625f8a2008-08-21 17:11:10 -070094 /*
95 * --------------------------------------------------------------
96 * The remaining are internal to ocfs2_extent_tree and don't have
97 * accessor functions
98 */
99
100 /*
101 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
102 * It is required.
103 */
Joel Becker0ce10102008-08-20 17:19:50 -0700104 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -0700105
106 /*
107 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
108 * it exists. If it does not, et->et_max_leaf_clusters is set
109 * to 0 (unlimited). Optional.
110 */
Joel Becker943cced2008-08-20 17:31:10 -0700111 void (*eo_fill_max_leaf_clusters)(struct inode *inode,
112 struct ocfs2_extent_tree *et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800113};
114
Joel Beckerf99b9b72008-08-20 19:36:33 -0700115
116/*
117 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
118 * in the methods.
119 */
120static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
121static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
122 u64 blkno);
123static void ocfs2_dinode_update_clusters(struct inode *inode,
124 struct ocfs2_extent_tree *et,
125 u32 clusters);
126static int ocfs2_dinode_insert_check(struct inode *inode,
127 struct ocfs2_extent_tree *et,
128 struct ocfs2_extent_rec *rec);
129static int ocfs2_dinode_sanity_check(struct inode *inode,
130 struct ocfs2_extent_tree *et);
131static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
132static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
133 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
134 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
135 .eo_update_clusters = ocfs2_dinode_update_clusters,
136 .eo_insert_check = ocfs2_dinode_insert_check,
137 .eo_sanity_check = ocfs2_dinode_sanity_check,
138 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
Tao Mae7d4cb62008-08-18 17:38:44 +0800139};
140
141static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
142 u64 blkno)
143{
Joel Beckerea5efa12008-08-20 16:57:27 -0700144 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800145
Joel Beckerf99b9b72008-08-20 19:36:33 -0700146 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800147 di->i_last_eb_blk = cpu_to_le64(blkno);
148}
149
150static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
151{
Joel Beckerea5efa12008-08-20 16:57:27 -0700152 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800153
Joel Beckerf99b9b72008-08-20 19:36:33 -0700154 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800155 return le64_to_cpu(di->i_last_eb_blk);
156}
157
158static void ocfs2_dinode_update_clusters(struct inode *inode,
159 struct ocfs2_extent_tree *et,
160 u32 clusters)
161{
Joel Beckerea5efa12008-08-20 16:57:27 -0700162 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800163
164 le32_add_cpu(&di->i_clusters, clusters);
165 spin_lock(&OCFS2_I(inode)->ip_lock);
166 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
167 spin_unlock(&OCFS2_I(inode)->ip_lock);
168}
169
Joel Becker1e61ee72008-08-20 18:32:45 -0700170static int ocfs2_dinode_insert_check(struct inode *inode,
171 struct ocfs2_extent_tree *et,
172 struct ocfs2_extent_rec *rec)
173{
174 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
175
176 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
177 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
178 (OCFS2_I(inode)->ip_clusters != rec->e_cpos),
179 "Device %s, asking for sparse allocation: inode %llu, "
180 "cpos %u, clusters %u\n",
181 osb->dev_str,
182 (unsigned long long)OCFS2_I(inode)->ip_blkno,
183 rec->e_cpos,
184 OCFS2_I(inode)->ip_clusters);
185
186 return 0;
187}
188
Tao Mae7d4cb62008-08-18 17:38:44 +0800189static int ocfs2_dinode_sanity_check(struct inode *inode,
190 struct ocfs2_extent_tree *et)
191{
Joel Becker10995aa2008-11-13 14:49:12 -0800192 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800193
Joel Beckerf99b9b72008-08-20 19:36:33 -0700194 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Joel Becker10995aa2008-11-13 14:49:12 -0800195 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
Tao Mae7d4cb62008-08-18 17:38:44 +0800196
Joel Becker10995aa2008-11-13 14:49:12 -0800197 return 0;
Tao Mae7d4cb62008-08-18 17:38:44 +0800198}
199
Joel Beckerf99b9b72008-08-20 19:36:33 -0700200static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
201{
202 struct ocfs2_dinode *di = et->et_object;
203
204 et->et_root_el = &di->id2.i_list;
205}
206
Tao Mae7d4cb62008-08-18 17:38:44 +0800207
Joel Becker0ce10102008-08-20 17:19:50 -0700208static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
209{
210 struct ocfs2_xattr_value_root *xv = et->et_object;
211
212 et->et_root_el = &xv->xr_list;
213}
214
Tao Maf56654c2008-08-18 17:38:48 +0800215static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
216 u64 blkno)
217{
218 struct ocfs2_xattr_value_root *xv =
Joel Beckerea5efa12008-08-20 16:57:27 -0700219 (struct ocfs2_xattr_value_root *)et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800220
221 xv->xr_last_eb_blk = cpu_to_le64(blkno);
222}
223
224static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
225{
226 struct ocfs2_xattr_value_root *xv =
Joel Beckerea5efa12008-08-20 16:57:27 -0700227 (struct ocfs2_xattr_value_root *) et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800228
229 return le64_to_cpu(xv->xr_last_eb_blk);
230}
231
232static void ocfs2_xattr_value_update_clusters(struct inode *inode,
233 struct ocfs2_extent_tree *et,
234 u32 clusters)
235{
236 struct ocfs2_xattr_value_root *xv =
Joel Beckerea5efa12008-08-20 16:57:27 -0700237 (struct ocfs2_xattr_value_root *)et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800238
239 le32_add_cpu(&xv->xr_clusters, clusters);
240}
241
Joel Becker1a09f552008-08-20 17:44:24 -0700242static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700243 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
244 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
245 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700246 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800247};
248
Joel Becker0ce10102008-08-20 17:19:50 -0700249static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
250{
251 struct ocfs2_xattr_block *xb = et->et_object;
252
253 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
254}
255
Joel Becker943cced2008-08-20 17:31:10 -0700256static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct inode *inode,
257 struct ocfs2_extent_tree *et)
258{
259 et->et_max_leaf_clusters =
260 ocfs2_clusters_for_bytes(inode->i_sb,
261 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
262}
263
Tao Maba492612008-08-18 17:38:49 +0800264static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
265 u64 blkno)
266{
Joel Beckerea5efa12008-08-20 16:57:27 -0700267 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800268 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
269
270 xt->xt_last_eb_blk = cpu_to_le64(blkno);
271}
272
273static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
274{
Joel Beckerea5efa12008-08-20 16:57:27 -0700275 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800276 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
277
278 return le64_to_cpu(xt->xt_last_eb_blk);
279}
280
281static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
282 struct ocfs2_extent_tree *et,
283 u32 clusters)
284{
Joel Beckerea5efa12008-08-20 16:57:27 -0700285 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800286
287 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
288}
289
Tao Maba492612008-08-18 17:38:49 +0800290static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700291 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
292 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
293 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700294 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
Joel Becker943cced2008-08-20 17:31:10 -0700295 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
Tao Maba492612008-08-18 17:38:49 +0800296};
297
Joel Becker8d6220d2008-08-22 12:46:09 -0700298static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
299 struct inode *inode,
300 struct buffer_head *bh,
Joel Becker13723d02008-10-17 19:25:01 -0700301 ocfs2_journal_access_func access,
Joel Becker8d6220d2008-08-22 12:46:09 -0700302 void *obj,
303 struct ocfs2_extent_tree_operations *ops)
Tao Mae7d4cb62008-08-18 17:38:44 +0800304{
Joel Becker1a09f552008-08-20 17:44:24 -0700305 et->et_ops = ops;
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700306 et->et_root_bh = bh;
Joel Becker13723d02008-10-17 19:25:01 -0700307 et->et_root_journal_access = access;
Joel Beckerea5efa12008-08-20 16:57:27 -0700308 if (!obj)
309 obj = (void *)bh->b_data;
310 et->et_object = obj;
Tao Mae7d4cb62008-08-18 17:38:44 +0800311
Joel Becker0ce10102008-08-20 17:19:50 -0700312 et->et_ops->eo_fill_root_el(et);
Joel Becker943cced2008-08-20 17:31:10 -0700313 if (!et->et_ops->eo_fill_max_leaf_clusters)
314 et->et_max_leaf_clusters = 0;
315 else
316 et->et_ops->eo_fill_max_leaf_clusters(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800317}
318
Joel Becker8d6220d2008-08-22 12:46:09 -0700319void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
320 struct inode *inode,
321 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700322{
Joel Becker13723d02008-10-17 19:25:01 -0700323 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_di,
324 NULL, &ocfs2_dinode_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700325}
326
Joel Becker8d6220d2008-08-22 12:46:09 -0700327void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700328 struct inode *inode,
Joel Becker8d6220d2008-08-22 12:46:09 -0700329 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700330{
Joel Becker13723d02008-10-17 19:25:01 -0700331 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access_xb,
332 NULL, &ocfs2_xattr_tree_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700333}
334
Joel Becker8d6220d2008-08-22 12:46:09 -0700335void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
336 struct inode *inode,
337 struct buffer_head *bh,
338 struct ocfs2_xattr_value_root *xv)
Tao Mae7d4cb62008-08-18 17:38:44 +0800339{
Joel Becker13723d02008-10-17 19:25:01 -0700340 __ocfs2_init_extent_tree(et, inode, bh, ocfs2_journal_access, xv,
Joel Becker8d6220d2008-08-22 12:46:09 -0700341 &ocfs2_xattr_value_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800342}
343
Joel Becker35dc0aa2008-08-20 16:25:06 -0700344static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
345 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800346{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700347 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800348}
349
Joel Becker35dc0aa2008-08-20 16:25:06 -0700350static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800351{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700352 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800353}
354
Joel Becker35dc0aa2008-08-20 16:25:06 -0700355static inline void ocfs2_et_update_clusters(struct inode *inode,
356 struct ocfs2_extent_tree *et,
357 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800358{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700359 et->et_ops->eo_update_clusters(inode, et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700360}
361
Joel Becker13723d02008-10-17 19:25:01 -0700362static inline int ocfs2_et_root_journal_access(handle_t *handle,
363 struct inode *inode,
364 struct ocfs2_extent_tree *et,
365 int type)
366{
367 return et->et_root_journal_access(handle, inode, et->et_root_bh,
368 type);
369}
370
Joel Becker1e61ee72008-08-20 18:32:45 -0700371static inline int ocfs2_et_insert_check(struct inode *inode,
372 struct ocfs2_extent_tree *et,
373 struct ocfs2_extent_rec *rec)
374{
375 int ret = 0;
376
377 if (et->et_ops->eo_insert_check)
378 ret = et->et_ops->eo_insert_check(inode, et, rec);
379 return ret;
380}
381
Joel Becker35dc0aa2008-08-20 16:25:06 -0700382static inline int ocfs2_et_sanity_check(struct inode *inode,
383 struct ocfs2_extent_tree *et)
384{
Joel Becker1e61ee72008-08-20 18:32:45 -0700385 int ret = 0;
386
387 if (et->et_ops->eo_sanity_check)
388 ret = et->et_ops->eo_sanity_check(inode, et);
389 return ret;
Tao Mae7d4cb62008-08-18 17:38:44 +0800390}
391
Mark Fashehccd979b2005-12-15 14:31:24 -0800392static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
Mark Fasheh59a5e412007-06-22 15:52:36 -0700393static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
394 struct ocfs2_extent_block *eb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800395
Mark Fashehdcd05382007-01-16 11:32:23 -0800396/*
397 * Structures which describe a path through a btree, and functions to
398 * manipulate them.
399 *
400 * The idea here is to be as generic as possible with the tree
401 * manipulation code.
402 */
403struct ocfs2_path_item {
404 struct buffer_head *bh;
405 struct ocfs2_extent_list *el;
406};
407
408#define OCFS2_MAX_PATH_DEPTH 5
409
410struct ocfs2_path {
Joel Becker13723d02008-10-17 19:25:01 -0700411 int p_tree_depth;
412 ocfs2_journal_access_func p_root_access;
413 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
Mark Fashehdcd05382007-01-16 11:32:23 -0800414};
415
416#define path_root_bh(_path) ((_path)->p_node[0].bh)
417#define path_root_el(_path) ((_path)->p_node[0].el)
Joel Becker13723d02008-10-17 19:25:01 -0700418#define path_root_access(_path)((_path)->p_root_access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800419#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
420#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
421#define path_num_items(_path) ((_path)->p_tree_depth + 1)
422
423/*
424 * Reset the actual path elements so that we can re-use the structure
425 * to build another path. Generally, this involves freeing the buffer
426 * heads.
427 */
428static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
429{
430 int i, start = 0, depth = 0;
431 struct ocfs2_path_item *node;
432
433 if (keep_root)
434 start = 1;
435
436 for(i = start; i < path_num_items(path); i++) {
437 node = &path->p_node[i];
438
439 brelse(node->bh);
440 node->bh = NULL;
441 node->el = NULL;
442 }
443
444 /*
445 * Tree depth may change during truncate, or insert. If we're
446 * keeping the root extent list, then make sure that our path
447 * structure reflects the proper depth.
448 */
449 if (keep_root)
450 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
Joel Becker13723d02008-10-17 19:25:01 -0700451 else
452 path_root_access(path) = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -0800453
454 path->p_tree_depth = depth;
455}
456
457static void ocfs2_free_path(struct ocfs2_path *path)
458{
459 if (path) {
460 ocfs2_reinit_path(path, 0);
461 kfree(path);
462 }
463}
464
465/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700466 * All the elements of src into dest. After this call, src could be freed
467 * without affecting dest.
468 *
469 * Both paths should have the same root. Any non-root elements of dest
470 * will be freed.
471 */
472static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
473{
474 int i;
475
476 BUG_ON(path_root_bh(dest) != path_root_bh(src));
477 BUG_ON(path_root_el(dest) != path_root_el(src));
Joel Becker13723d02008-10-17 19:25:01 -0700478 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fasheh328d5752007-06-18 10:48:04 -0700479
480 ocfs2_reinit_path(dest, 1);
481
482 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
483 dest->p_node[i].bh = src->p_node[i].bh;
484 dest->p_node[i].el = src->p_node[i].el;
485
486 if (dest->p_node[i].bh)
487 get_bh(dest->p_node[i].bh);
488 }
489}
490
491/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800492 * Make the *dest path the same as src and re-initialize src path to
493 * have a root only.
494 */
495static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
496{
497 int i;
498
499 BUG_ON(path_root_bh(dest) != path_root_bh(src));
Joel Becker13723d02008-10-17 19:25:01 -0700500 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fashehdcd05382007-01-16 11:32:23 -0800501
502 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
503 brelse(dest->p_node[i].bh);
504
505 dest->p_node[i].bh = src->p_node[i].bh;
506 dest->p_node[i].el = src->p_node[i].el;
507
508 src->p_node[i].bh = NULL;
509 src->p_node[i].el = NULL;
510 }
511}
512
513/*
514 * Insert an extent block at given index.
515 *
516 * This will not take an additional reference on eb_bh.
517 */
518static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
519 struct buffer_head *eb_bh)
520{
521 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
522
523 /*
524 * Right now, no root bh is an extent block, so this helps
525 * catch code errors with dinode trees. The assertion can be
526 * safely removed if we ever need to insert extent block
527 * structures at the root.
528 */
529 BUG_ON(index == 0);
530
531 path->p_node[index].bh = eb_bh;
532 path->p_node[index].el = &eb->h_list;
533}
534
535static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700536 struct ocfs2_extent_list *root_el,
537 ocfs2_journal_access_func access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800538{
539 struct ocfs2_path *path;
540
541 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
542
543 path = kzalloc(sizeof(*path), GFP_NOFS);
544 if (path) {
545 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
546 get_bh(root_bh);
547 path_root_bh(path) = root_bh;
548 path_root_el(path) = root_el;
Joel Becker13723d02008-10-17 19:25:01 -0700549 path_root_access(path) = access;
Mark Fashehdcd05382007-01-16 11:32:23 -0800550 }
551
552 return path;
553}
554
Joel Beckerffdd7a52008-10-17 22:32:01 -0700555static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
556{
Joel Becker13723d02008-10-17 19:25:01 -0700557 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
558 path_root_access(path));
Joel Beckerffdd7a52008-10-17 22:32:01 -0700559}
560
561static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
562{
Joel Becker13723d02008-10-17 19:25:01 -0700563 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
564 et->et_root_journal_access);
565}
566
567/*
568 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
569 * otherwise it's the root_access function.
570 *
571 * I don't like the way this function's name looks next to
572 * ocfs2_journal_access_path(), but I don't have a better one.
573 */
574static int ocfs2_path_bh_journal_access(handle_t *handle,
575 struct inode *inode,
576 struct ocfs2_path *path,
577 int idx)
578{
579 ocfs2_journal_access_func access = path_root_access(path);
580
581 if (!access)
582 access = ocfs2_journal_access;
583
584 if (idx)
585 access = ocfs2_journal_access_eb;
586
587 return access(handle, inode, path->p_node[idx].bh,
588 OCFS2_JOURNAL_ACCESS_WRITE);
Joel Beckerffdd7a52008-10-17 22:32:01 -0700589}
590
Mark Fashehdcd05382007-01-16 11:32:23 -0800591/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800592 * Convenience function to journal all components in a path.
593 */
594static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
595 struct ocfs2_path *path)
596{
597 int i, ret = 0;
598
599 if (!path)
600 goto out;
601
602 for(i = 0; i < path_num_items(path); i++) {
Joel Becker13723d02008-10-17 19:25:01 -0700603 ret = ocfs2_path_bh_journal_access(handle, inode, path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -0800604 if (ret < 0) {
605 mlog_errno(ret);
606 goto out;
607 }
608 }
609
610out:
611 return ret;
612}
613
Mark Fasheh328d5752007-06-18 10:48:04 -0700614/*
615 * Return the index of the extent record which contains cluster #v_cluster.
616 * -1 is returned if it was not found.
617 *
618 * Should work fine on interior and exterior nodes.
619 */
620int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
621{
622 int ret = -1;
623 int i;
624 struct ocfs2_extent_rec *rec;
625 u32 rec_end, rec_start, clusters;
626
627 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
628 rec = &el->l_recs[i];
629
630 rec_start = le32_to_cpu(rec->e_cpos);
631 clusters = ocfs2_rec_clusters(el, rec);
632
633 rec_end = rec_start + clusters;
634
635 if (v_cluster >= rec_start && v_cluster < rec_end) {
636 ret = i;
637 break;
638 }
639 }
640
641 return ret;
642}
643
Mark Fashehdcd05382007-01-16 11:32:23 -0800644enum ocfs2_contig_type {
645 CONTIG_NONE = 0,
646 CONTIG_LEFT,
Mark Fasheh328d5752007-06-18 10:48:04 -0700647 CONTIG_RIGHT,
648 CONTIG_LEFTRIGHT,
Mark Fashehdcd05382007-01-16 11:32:23 -0800649};
650
Mark Fashehe48edee2007-03-07 16:46:57 -0800651
652/*
653 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
654 * ocfs2_extent_contig only work properly against leaf nodes!
655 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800656static int ocfs2_block_extent_contig(struct super_block *sb,
657 struct ocfs2_extent_rec *ext,
658 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800659{
Mark Fashehe48edee2007-03-07 16:46:57 -0800660 u64 blk_end = le64_to_cpu(ext->e_blkno);
661
662 blk_end += ocfs2_clusters_to_blocks(sb,
663 le16_to_cpu(ext->e_leaf_clusters));
664
665 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800666}
667
Mark Fashehdcd05382007-01-16 11:32:23 -0800668static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
669 struct ocfs2_extent_rec *right)
670{
Mark Fashehe48edee2007-03-07 16:46:57 -0800671 u32 left_range;
672
673 left_range = le32_to_cpu(left->e_cpos) +
674 le16_to_cpu(left->e_leaf_clusters);
675
676 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800677}
678
679static enum ocfs2_contig_type
680 ocfs2_extent_contig(struct inode *inode,
681 struct ocfs2_extent_rec *ext,
682 struct ocfs2_extent_rec *insert_rec)
683{
684 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
685
Mark Fasheh328d5752007-06-18 10:48:04 -0700686 /*
687 * Refuse to coalesce extent records with different flag
688 * fields - we don't want to mix unwritten extents with user
689 * data.
690 */
691 if (ext->e_flags != insert_rec->e_flags)
692 return CONTIG_NONE;
693
Mark Fashehdcd05382007-01-16 11:32:23 -0800694 if (ocfs2_extents_adjacent(ext, insert_rec) &&
695 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
696 return CONTIG_RIGHT;
697
698 blkno = le64_to_cpu(ext->e_blkno);
699 if (ocfs2_extents_adjacent(insert_rec, ext) &&
700 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
701 return CONTIG_LEFT;
702
703 return CONTIG_NONE;
704}
705
706/*
707 * NOTE: We can have pretty much any combination of contiguousness and
708 * appending.
709 *
710 * The usefulness of APPEND_TAIL is more in that it lets us know that
711 * we'll have to update the path to that leaf.
712 */
713enum ocfs2_append_type {
714 APPEND_NONE = 0,
715 APPEND_TAIL,
716};
717
Mark Fasheh328d5752007-06-18 10:48:04 -0700718enum ocfs2_split_type {
719 SPLIT_NONE = 0,
720 SPLIT_LEFT,
721 SPLIT_RIGHT,
722};
723
Mark Fashehdcd05382007-01-16 11:32:23 -0800724struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700725 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800726 enum ocfs2_append_type ins_appending;
727 enum ocfs2_contig_type ins_contig;
728 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800729 int ins_tree_depth;
730};
731
Mark Fasheh328d5752007-06-18 10:48:04 -0700732struct ocfs2_merge_ctxt {
733 enum ocfs2_contig_type c_contig_type;
734 int c_has_empty_extent;
735 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700736};
737
Joel Becker5e965812008-11-13 14:49:16 -0800738static int ocfs2_validate_extent_block(struct super_block *sb,
739 struct buffer_head *bh)
740{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700741 int rc;
Joel Becker5e965812008-11-13 14:49:16 -0800742 struct ocfs2_extent_block *eb =
743 (struct ocfs2_extent_block *)bh->b_data;
744
Joel Becker970e4932008-11-13 14:49:19 -0800745 mlog(0, "Validating extent block %llu\n",
746 (unsigned long long)bh->b_blocknr);
747
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700748 BUG_ON(!buffer_uptodate(bh));
749
750 /*
751 * If the ecc fails, we return the error but otherwise
752 * leave the filesystem running. We know any error is
753 * local to this block.
754 */
755 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
Joel Becker13723d02008-10-17 19:25:01 -0700756 if (rc) {
757 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
758 (unsigned long long)bh->b_blocknr);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700759 return rc;
Joel Becker13723d02008-10-17 19:25:01 -0700760 }
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700761
762 /*
763 * Errors after here are fatal.
764 */
765
Joel Becker5e965812008-11-13 14:49:16 -0800766 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
767 ocfs2_error(sb,
768 "Extent block #%llu has bad signature %.*s",
769 (unsigned long long)bh->b_blocknr, 7,
770 eb->h_signature);
771 return -EINVAL;
772 }
773
774 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
775 ocfs2_error(sb,
776 "Extent block #%llu has an invalid h_blkno "
777 "of %llu",
778 (unsigned long long)bh->b_blocknr,
779 (unsigned long long)le64_to_cpu(eb->h_blkno));
780 return -EINVAL;
781 }
782
783 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
784 ocfs2_error(sb,
785 "Extent block #%llu has an invalid "
786 "h_fs_generation of #%u",
787 (unsigned long long)bh->b_blocknr,
788 le32_to_cpu(eb->h_fs_generation));
789 return -EINVAL;
790 }
791
792 return 0;
793}
794
795int ocfs2_read_extent_block(struct inode *inode, u64 eb_blkno,
796 struct buffer_head **bh)
797{
798 int rc;
799 struct buffer_head *tmp = *bh;
800
Joel Becker970e4932008-11-13 14:49:19 -0800801 rc = ocfs2_read_block(inode, eb_blkno, &tmp,
802 ocfs2_validate_extent_block);
Joel Becker5e965812008-11-13 14:49:16 -0800803
804 /* If ocfs2_read_block() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -0800805 if (!rc && !*bh)
Joel Becker5e965812008-11-13 14:49:16 -0800806 *bh = tmp;
807
Joel Becker5e965812008-11-13 14:49:16 -0800808 return rc;
809}
810
811
Mark Fashehccd979b2005-12-15 14:31:24 -0800812/*
813 * How many free extents have we got before we need more meta data?
814 */
815int ocfs2_num_free_extents(struct ocfs2_super *osb,
816 struct inode *inode,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700817 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -0800818{
819 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800820 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800821 struct ocfs2_extent_block *eb;
822 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800823 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800824
825 mlog_entry_void();
826
Joel Beckerf99b9b72008-08-20 19:36:33 -0700827 el = et->et_root_el;
828 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800829
Tao Mae7d4cb62008-08-18 17:38:44 +0800830 if (last_eb_blk) {
Joel Becker5e965812008-11-13 14:49:16 -0800831 retval = ocfs2_read_extent_block(inode, last_eb_blk, &eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800832 if (retval < 0) {
833 mlog_errno(retval);
834 goto bail;
835 }
836 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
837 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800838 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800839
840 BUG_ON(el->l_tree_depth != 0);
841
842 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
843bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700844 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800845
846 mlog_exit(retval);
847 return retval;
848}
849
850/* expects array to already be allocated
851 *
852 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
853 * l_count for you
854 */
855static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700856 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800857 struct inode *inode,
858 int wanted,
859 struct ocfs2_alloc_context *meta_ac,
860 struct buffer_head *bhs[])
861{
862 int count, status, i;
863 u16 suballoc_bit_start;
864 u32 num_got;
865 u64 first_blkno;
866 struct ocfs2_extent_block *eb;
867
868 mlog_entry_void();
869
870 count = 0;
871 while (count < wanted) {
872 status = ocfs2_claim_metadata(osb,
873 handle,
874 meta_ac,
875 wanted - count,
876 &suballoc_bit_start,
877 &num_got,
878 &first_blkno);
879 if (status < 0) {
880 mlog_errno(status);
881 goto bail;
882 }
883
884 for(i = count; i < (num_got + count); i++) {
885 bhs[i] = sb_getblk(osb->sb, first_blkno);
886 if (bhs[i] == NULL) {
887 status = -EIO;
888 mlog_errno(status);
889 goto bail;
890 }
891 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
892
Joel Becker13723d02008-10-17 19:25:01 -0700893 status = ocfs2_journal_access_eb(handle, inode, bhs[i],
894 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -0800895 if (status < 0) {
896 mlog_errno(status);
897 goto bail;
898 }
899
900 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
901 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
902 /* Ok, setup the minimal stuff here. */
903 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
904 eb->h_blkno = cpu_to_le64(first_blkno);
905 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800906 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800907 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
908 eb->h_list.l_count =
909 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
910
911 suballoc_bit_start++;
912 first_blkno++;
913
914 /* We'll also be dirtied by the caller, so
915 * this isn't absolutely necessary. */
916 status = ocfs2_journal_dirty(handle, bhs[i]);
917 if (status < 0) {
918 mlog_errno(status);
919 goto bail;
920 }
921 }
922
923 count += num_got;
924 }
925
926 status = 0;
927bail:
928 if (status < 0) {
929 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -0700930 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -0800931 bhs[i] = NULL;
932 }
933 }
934 mlog_exit(status);
935 return status;
936}
937
938/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800939 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
940 *
941 * Returns the sum of the rightmost extent rec logical offset and
942 * cluster count.
943 *
944 * ocfs2_add_branch() uses this to determine what logical cluster
945 * value should be populated into the leftmost new branch records.
946 *
947 * ocfs2_shift_tree_depth() uses this to determine the # clusters
948 * value for the new topmost tree record.
949 */
950static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
951{
952 int i;
953
954 i = le16_to_cpu(el->l_next_free_rec) - 1;
955
956 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -0800957 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -0800958}
959
960/*
Mark Fashehccd979b2005-12-15 14:31:24 -0800961 * Add an entire tree branch to our inode. eb_bh is the extent block
962 * to start at, if we don't want to start the branch at the dinode
963 * structure.
964 *
965 * last_eb_bh is required as we have to update it's next_leaf pointer
966 * for the new last extent block.
967 *
968 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -0800969 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -0800970 */
971static int ocfs2_add_branch(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700972 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800973 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +0800974 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -0800975 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -0700976 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800977 struct ocfs2_alloc_context *meta_ac)
978{
979 int status, new_blocks, i;
980 u64 next_blkno, new_last_eb_blk;
981 struct buffer_head *bh;
982 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800983 struct ocfs2_extent_block *eb;
984 struct ocfs2_extent_list *eb_el;
985 struct ocfs2_extent_list *el;
Mark Fashehdcd05382007-01-16 11:32:23 -0800986 u32 new_cpos;
Mark Fashehccd979b2005-12-15 14:31:24 -0800987
988 mlog_entry_void();
989
Mark Fasheh328d5752007-06-18 10:48:04 -0700990 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800991
Mark Fashehccd979b2005-12-15 14:31:24 -0800992 if (eb_bh) {
993 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
994 el = &eb->h_list;
995 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700996 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -0800997
998 /* we never add a branch to a leaf. */
999 BUG_ON(!el->l_tree_depth);
1000
1001 new_blocks = le16_to_cpu(el->l_tree_depth);
1002
1003 /* allocate the number of new eb blocks we need */
1004 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1005 GFP_KERNEL);
1006 if (!new_eb_bhs) {
1007 status = -ENOMEM;
1008 mlog_errno(status);
1009 goto bail;
1010 }
1011
1012 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
1013 meta_ac, new_eb_bhs);
1014 if (status < 0) {
1015 mlog_errno(status);
1016 goto bail;
1017 }
1018
Mark Fasheh328d5752007-06-18 10:48:04 -07001019 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
Mark Fashehdcd05382007-01-16 11:32:23 -08001020 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1021
Mark Fashehccd979b2005-12-15 14:31:24 -08001022 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1023 * linked with the rest of the tree.
1024 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1025 *
1026 * when we leave the loop, new_last_eb_blk will point to the
1027 * newest leaf, and next_blkno will point to the topmost extent
1028 * block. */
1029 next_blkno = new_last_eb_blk = 0;
1030 for(i = 0; i < new_blocks; i++) {
1031 bh = new_eb_bhs[i];
1032 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001033 /* ocfs2_create_new_meta_bhs() should create it right! */
1034 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001035 eb_el = &eb->h_list;
1036
Joel Becker13723d02008-10-17 19:25:01 -07001037 status = ocfs2_journal_access_eb(handle, inode, bh,
1038 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001039 if (status < 0) {
1040 mlog_errno(status);
1041 goto bail;
1042 }
1043
1044 eb->h_next_leaf_blk = 0;
1045 eb_el->l_tree_depth = cpu_to_le16(i);
1046 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001047 /*
1048 * This actually counts as an empty extent as
1049 * c_clusters == 0
1050 */
1051 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001052 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001053 /*
1054 * eb_el isn't always an interior node, but even leaf
1055 * nodes want a zero'd flags and reserved field so
1056 * this gets the whole 32 bits regardless of use.
1057 */
1058 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001059 if (!eb_el->l_tree_depth)
1060 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1061
1062 status = ocfs2_journal_dirty(handle, bh);
1063 if (status < 0) {
1064 mlog_errno(status);
1065 goto bail;
1066 }
1067
1068 next_blkno = le64_to_cpu(eb->h_blkno);
1069 }
1070
1071 /* This is a bit hairy. We want to update up to three blocks
1072 * here without leaving any of them in an inconsistent state
1073 * in case of error. We don't have to worry about
1074 * journal_dirty erroring as it won't unless we've aborted the
1075 * handle (in which case we would never be here) so reserving
1076 * the write with journal_access is all we need to do. */
Joel Becker13723d02008-10-17 19:25:01 -07001077 status = ocfs2_journal_access_eb(handle, inode, *last_eb_bh,
1078 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001079 if (status < 0) {
1080 mlog_errno(status);
1081 goto bail;
1082 }
Joel Becker13723d02008-10-17 19:25:01 -07001083 status = ocfs2_et_root_journal_access(handle, inode, et,
1084 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001085 if (status < 0) {
1086 mlog_errno(status);
1087 goto bail;
1088 }
1089 if (eb_bh) {
Joel Becker13723d02008-10-17 19:25:01 -07001090 status = ocfs2_journal_access_eb(handle, inode, eb_bh,
1091 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001092 if (status < 0) {
1093 mlog_errno(status);
1094 goto bail;
1095 }
1096 }
1097
1098 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001099 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001100 i = le16_to_cpu(el->l_next_free_rec);
1101 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001102 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001103 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001104 le16_add_cpu(&el->l_next_free_rec, 1);
1105
1106 /* fe needs a new last extent block pointer, as does the
1107 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001108 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001109
Mark Fasheh328d5752007-06-18 10:48:04 -07001110 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001111 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1112
Mark Fasheh328d5752007-06-18 10:48:04 -07001113 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001114 if (status < 0)
1115 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001116 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001117 if (status < 0)
1118 mlog_errno(status);
1119 if (eb_bh) {
1120 status = ocfs2_journal_dirty(handle, eb_bh);
1121 if (status < 0)
1122 mlog_errno(status);
1123 }
1124
Mark Fasheh328d5752007-06-18 10:48:04 -07001125 /*
1126 * Some callers want to track the rightmost leaf so pass it
1127 * back here.
1128 */
1129 brelse(*last_eb_bh);
1130 get_bh(new_eb_bhs[0]);
1131 *last_eb_bh = new_eb_bhs[0];
1132
Mark Fashehccd979b2005-12-15 14:31:24 -08001133 status = 0;
1134bail:
1135 if (new_eb_bhs) {
1136 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001137 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001138 kfree(new_eb_bhs);
1139 }
1140
1141 mlog_exit(status);
1142 return status;
1143}
1144
1145/*
1146 * adds another level to the allocation tree.
1147 * returns back the new extent block so you can add a branch to it
1148 * after this call.
1149 */
1150static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07001151 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001152 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001153 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001154 struct ocfs2_alloc_context *meta_ac,
1155 struct buffer_head **ret_new_eb_bh)
1156{
1157 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001158 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001159 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001160 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001161 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001162 struct ocfs2_extent_list *eb_el;
1163
1164 mlog_entry_void();
1165
1166 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
1167 &new_eb_bh);
1168 if (status < 0) {
1169 mlog_errno(status);
1170 goto bail;
1171 }
1172
1173 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001174 /* ocfs2_create_new_meta_bhs() should create it right! */
1175 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001176
1177 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001178 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001179
Joel Becker13723d02008-10-17 19:25:01 -07001180 status = ocfs2_journal_access_eb(handle, inode, new_eb_bh,
1181 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001182 if (status < 0) {
1183 mlog_errno(status);
1184 goto bail;
1185 }
1186
Tao Mae7d4cb62008-08-18 17:38:44 +08001187 /* copy the root extent list data into the new extent block */
1188 eb_el->l_tree_depth = root_el->l_tree_depth;
1189 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1190 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1191 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001192
1193 status = ocfs2_journal_dirty(handle, new_eb_bh);
1194 if (status < 0) {
1195 mlog_errno(status);
1196 goto bail;
1197 }
1198
Joel Becker13723d02008-10-17 19:25:01 -07001199 status = ocfs2_et_root_journal_access(handle, inode, et,
1200 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001201 if (status < 0) {
1202 mlog_errno(status);
1203 goto bail;
1204 }
1205
Mark Fashehdcd05382007-01-16 11:32:23 -08001206 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1207
Tao Mae7d4cb62008-08-18 17:38:44 +08001208 /* update root_bh now */
1209 le16_add_cpu(&root_el->l_tree_depth, 1);
1210 root_el->l_recs[0].e_cpos = 0;
1211 root_el->l_recs[0].e_blkno = eb->h_blkno;
1212 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1213 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1214 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1215 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001216
1217 /* If this is our 1st tree depth shift, then last_eb_blk
1218 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001219 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001220 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001221
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001222 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001223 if (status < 0) {
1224 mlog_errno(status);
1225 goto bail;
1226 }
1227
1228 *ret_new_eb_bh = new_eb_bh;
1229 new_eb_bh = NULL;
1230 status = 0;
1231bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001232 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001233
1234 mlog_exit(status);
1235 return status;
1236}
1237
1238/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001239 * Should only be called when there is no space left in any of the
1240 * leaf nodes. What we want to do is find the lowest tree depth
1241 * non-leaf extent block with room for new records. There are three
1242 * valid results of this search:
1243 *
1244 * 1) a lowest extent block is found, then we pass it back in
1245 * *lowest_eb_bh and return '0'
1246 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001247 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001248 * pass NULL back in *lowest_eb_bh, but still return '0'
1249 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001250 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001251 * which case we return > 0
1252 *
1253 * return status < 0 indicates an error.
1254 */
1255static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1256 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001257 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001258 struct buffer_head **target_bh)
1259{
1260 int status = 0, i;
1261 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001262 struct ocfs2_extent_block *eb;
1263 struct ocfs2_extent_list *el;
1264 struct buffer_head *bh = NULL;
1265 struct buffer_head *lowest_bh = NULL;
1266
1267 mlog_entry_void();
1268
1269 *target_bh = NULL;
1270
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001271 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001272
1273 while(le16_to_cpu(el->l_tree_depth) > 1) {
1274 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Mark Fashehb06970532006-03-03 10:24:33 -08001275 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001276 "extent list (next_free_rec == 0)",
Mark Fashehb06970532006-03-03 10:24:33 -08001277 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001278 status = -EIO;
1279 goto bail;
1280 }
1281 i = le16_to_cpu(el->l_next_free_rec) - 1;
1282 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1283 if (!blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -08001284 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001285 "list where extent # %d has no physical "
1286 "block start",
Mark Fashehb06970532006-03-03 10:24:33 -08001287 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001288 status = -EIO;
1289 goto bail;
1290 }
1291
Mark Fasheha81cb882008-10-07 14:25:16 -07001292 brelse(bh);
1293 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001294
Joel Becker5e965812008-11-13 14:49:16 -08001295 status = ocfs2_read_extent_block(inode, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001296 if (status < 0) {
1297 mlog_errno(status);
1298 goto bail;
1299 }
1300
1301 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001302 el = &eb->h_list;
1303
1304 if (le16_to_cpu(el->l_next_free_rec) <
1305 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001306 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001307 lowest_bh = bh;
1308 get_bh(lowest_bh);
1309 }
1310 }
1311
1312 /* If we didn't find one and the fe doesn't have any room,
1313 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001314 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001315 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001316 status = 1;
1317
1318 *target_bh = lowest_bh;
1319bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001320 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001321
1322 mlog_exit(status);
1323 return status;
1324}
1325
Mark Fashehe48edee2007-03-07 16:46:57 -08001326/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001327 * Grow a b-tree so that it has more records.
1328 *
1329 * We might shift the tree depth in which case existing paths should
1330 * be considered invalid.
1331 *
1332 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001333 *
1334 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001335 */
1336static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001337 struct ocfs2_extent_tree *et, int *final_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07001338 struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001339 struct ocfs2_alloc_context *meta_ac)
1340{
1341 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001342 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001343 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001344 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1345 struct buffer_head *bh = NULL;
1346
1347 BUG_ON(meta_ac == NULL);
1348
Tao Mae7d4cb62008-08-18 17:38:44 +08001349 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001350 if (shift < 0) {
1351 ret = shift;
1352 mlog_errno(ret);
1353 goto out;
1354 }
1355
1356 /* We traveled all the way to the bottom of the allocation tree
1357 * and didn't find room for any more extents - we need to add
1358 * another tree level */
1359 if (shift) {
1360 BUG_ON(bh);
1361 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1362
1363 /* ocfs2_shift_tree_depth will return us a buffer with
1364 * the new extent block (so we can pass that to
1365 * ocfs2_add_branch). */
Tao Mae7d4cb62008-08-18 17:38:44 +08001366 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001367 meta_ac, &bh);
1368 if (ret < 0) {
1369 mlog_errno(ret);
1370 goto out;
1371 }
1372 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001373 if (depth == 1) {
1374 /*
1375 * Special case: we have room now if we shifted from
1376 * tree_depth 0, so no more work needs to be done.
1377 *
1378 * We won't be calling add_branch, so pass
1379 * back *last_eb_bh as the new leaf. At depth
1380 * zero, it should always be null so there's
1381 * no reason to brelse.
1382 */
1383 BUG_ON(*last_eb_bh);
1384 get_bh(bh);
1385 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001386 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001387 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001388 }
1389
1390 /* call ocfs2_add_branch to add the final part of the tree with
1391 * the new data. */
1392 mlog(0, "add branch. bh = %p\n", bh);
Tao Mae7d4cb62008-08-18 17:38:44 +08001393 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001394 meta_ac);
1395 if (ret < 0) {
1396 mlog_errno(ret);
1397 goto out;
1398 }
1399
1400out:
1401 if (final_depth)
1402 *final_depth = depth;
1403 brelse(bh);
1404 return ret;
1405}
1406
1407/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001408 * This function will discard the rightmost extent record.
1409 */
1410static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1411{
1412 int next_free = le16_to_cpu(el->l_next_free_rec);
1413 int count = le16_to_cpu(el->l_count);
1414 unsigned int num_bytes;
1415
1416 BUG_ON(!next_free);
1417 /* This will cause us to go off the end of our extent list. */
1418 BUG_ON(next_free >= count);
1419
1420 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1421
1422 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1423}
1424
1425static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1426 struct ocfs2_extent_rec *insert_rec)
1427{
1428 int i, insert_index, next_free, has_empty, num_bytes;
1429 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1430 struct ocfs2_extent_rec *rec;
1431
1432 next_free = le16_to_cpu(el->l_next_free_rec);
1433 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1434
1435 BUG_ON(!next_free);
1436
1437 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001438 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001439
1440 /*
1441 * The easiest way to approach this is to just remove the
1442 * empty extent and temporarily decrement next_free.
1443 */
1444 if (has_empty) {
1445 /*
1446 * If next_free was 1 (only an empty extent), this
1447 * loop won't execute, which is fine. We still want
1448 * the decrement above to happen.
1449 */
1450 for(i = 0; i < (next_free - 1); i++)
1451 el->l_recs[i] = el->l_recs[i+1];
1452
1453 next_free--;
1454 }
1455
1456 /*
1457 * Figure out what the new record index should be.
1458 */
1459 for(i = 0; i < next_free; i++) {
1460 rec = &el->l_recs[i];
1461
1462 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1463 break;
1464 }
1465 insert_index = i;
1466
1467 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1468 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1469
1470 BUG_ON(insert_index < 0);
1471 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1472 BUG_ON(insert_index > next_free);
1473
1474 /*
1475 * No need to memmove if we're just adding to the tail.
1476 */
1477 if (insert_index != next_free) {
1478 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1479
1480 num_bytes = next_free - insert_index;
1481 num_bytes *= sizeof(struct ocfs2_extent_rec);
1482 memmove(&el->l_recs[insert_index + 1],
1483 &el->l_recs[insert_index],
1484 num_bytes);
1485 }
1486
1487 /*
1488 * Either we had an empty extent, and need to re-increment or
1489 * there was no empty extent on a non full rightmost leaf node,
1490 * in which case we still need to increment.
1491 */
1492 next_free++;
1493 el->l_next_free_rec = cpu_to_le16(next_free);
1494 /*
1495 * Make sure none of the math above just messed up our tree.
1496 */
1497 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1498
1499 el->l_recs[insert_index] = *insert_rec;
1500
1501}
1502
Mark Fasheh328d5752007-06-18 10:48:04 -07001503static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1504{
1505 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1506
1507 BUG_ON(num_recs == 0);
1508
1509 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1510 num_recs--;
1511 size = num_recs * sizeof(struct ocfs2_extent_rec);
1512 memmove(&el->l_recs[0], &el->l_recs[1], size);
1513 memset(&el->l_recs[num_recs], 0,
1514 sizeof(struct ocfs2_extent_rec));
1515 el->l_next_free_rec = cpu_to_le16(num_recs);
1516 }
1517}
1518
Mark Fashehdcd05382007-01-16 11:32:23 -08001519/*
1520 * Create an empty extent record .
1521 *
1522 * l_next_free_rec may be updated.
1523 *
1524 * If an empty extent already exists do nothing.
1525 */
1526static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1527{
1528 int next_free = le16_to_cpu(el->l_next_free_rec);
1529
Mark Fashehe48edee2007-03-07 16:46:57 -08001530 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1531
Mark Fashehdcd05382007-01-16 11:32:23 -08001532 if (next_free == 0)
1533 goto set_and_inc;
1534
1535 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1536 return;
1537
1538 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1539 "Asked to create an empty extent in a full list:\n"
1540 "count = %u, tree depth = %u",
1541 le16_to_cpu(el->l_count),
1542 le16_to_cpu(el->l_tree_depth));
1543
1544 ocfs2_shift_records_right(el);
1545
1546set_and_inc:
1547 le16_add_cpu(&el->l_next_free_rec, 1);
1548 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1549}
1550
1551/*
1552 * For a rotation which involves two leaf nodes, the "root node" is
1553 * the lowest level tree node which contains a path to both leafs. This
1554 * resulting set of information can be used to form a complete "subtree"
1555 *
1556 * This function is passed two full paths from the dinode down to a
1557 * pair of adjacent leaves. It's task is to figure out which path
1558 * index contains the subtree root - this can be the root index itself
1559 * in a worst-case rotation.
1560 *
1561 * The array index of the subtree root is passed back.
1562 */
1563static int ocfs2_find_subtree_root(struct inode *inode,
1564 struct ocfs2_path *left,
1565 struct ocfs2_path *right)
1566{
1567 int i = 0;
1568
1569 /*
1570 * Check that the caller passed in two paths from the same tree.
1571 */
1572 BUG_ON(path_root_bh(left) != path_root_bh(right));
1573
1574 do {
1575 i++;
1576
1577 /*
1578 * The caller didn't pass two adjacent paths.
1579 */
1580 mlog_bug_on_msg(i > left->p_tree_depth,
1581 "Inode %lu, left depth %u, right depth %u\n"
1582 "left leaf blk %llu, right leaf blk %llu\n",
1583 inode->i_ino, left->p_tree_depth,
1584 right->p_tree_depth,
1585 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1586 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1587 } while (left->p_node[i].bh->b_blocknr ==
1588 right->p_node[i].bh->b_blocknr);
1589
1590 return i - 1;
1591}
1592
1593typedef void (path_insert_t)(void *, struct buffer_head *);
1594
1595/*
1596 * Traverse a btree path in search of cpos, starting at root_el.
1597 *
1598 * This code can be called with a cpos larger than the tree, in which
1599 * case it will return the rightmost path.
1600 */
1601static int __ocfs2_find_path(struct inode *inode,
1602 struct ocfs2_extent_list *root_el, u32 cpos,
1603 path_insert_t *func, void *data)
1604{
1605 int i, ret = 0;
1606 u32 range;
1607 u64 blkno;
1608 struct buffer_head *bh = NULL;
1609 struct ocfs2_extent_block *eb;
1610 struct ocfs2_extent_list *el;
1611 struct ocfs2_extent_rec *rec;
1612 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1613
1614 el = root_el;
1615 while (el->l_tree_depth) {
1616 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1617 ocfs2_error(inode->i_sb,
1618 "Inode %llu has empty extent list at "
1619 "depth %u\n",
1620 (unsigned long long)oi->ip_blkno,
1621 le16_to_cpu(el->l_tree_depth));
1622 ret = -EROFS;
1623 goto out;
1624
1625 }
1626
1627 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1628 rec = &el->l_recs[i];
1629
1630 /*
1631 * In the case that cpos is off the allocation
1632 * tree, this should just wind up returning the
1633 * rightmost record.
1634 */
1635 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001636 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001637 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1638 break;
1639 }
1640
1641 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1642 if (blkno == 0) {
1643 ocfs2_error(inode->i_sb,
1644 "Inode %llu has bad blkno in extent list "
1645 "at depth %u (index %d)\n",
1646 (unsigned long long)oi->ip_blkno,
1647 le16_to_cpu(el->l_tree_depth), i);
1648 ret = -EROFS;
1649 goto out;
1650 }
1651
1652 brelse(bh);
1653 bh = NULL;
Joel Becker5e965812008-11-13 14:49:16 -08001654 ret = ocfs2_read_extent_block(inode, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001655 if (ret) {
1656 mlog_errno(ret);
1657 goto out;
1658 }
1659
1660 eb = (struct ocfs2_extent_block *) bh->b_data;
1661 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001662
1663 if (le16_to_cpu(el->l_next_free_rec) >
1664 le16_to_cpu(el->l_count)) {
1665 ocfs2_error(inode->i_sb,
1666 "Inode %llu has bad count in extent list "
1667 "at block %llu (next free=%u, count=%u)\n",
1668 (unsigned long long)oi->ip_blkno,
1669 (unsigned long long)bh->b_blocknr,
1670 le16_to_cpu(el->l_next_free_rec),
1671 le16_to_cpu(el->l_count));
1672 ret = -EROFS;
1673 goto out;
1674 }
1675
1676 if (func)
1677 func(data, bh);
1678 }
1679
1680out:
1681 /*
1682 * Catch any trailing bh that the loop didn't handle.
1683 */
1684 brelse(bh);
1685
1686 return ret;
1687}
1688
1689/*
1690 * Given an initialized path (that is, it has a valid root extent
1691 * list), this function will traverse the btree in search of the path
1692 * which would contain cpos.
1693 *
1694 * The path traveled is recorded in the path structure.
1695 *
1696 * Note that this will not do any comparisons on leaf node extent
1697 * records, so it will work fine in the case that we just added a tree
1698 * branch.
1699 */
1700struct find_path_data {
1701 int index;
1702 struct ocfs2_path *path;
1703};
1704static void find_path_ins(void *data, struct buffer_head *bh)
1705{
1706 struct find_path_data *fp = data;
1707
1708 get_bh(bh);
1709 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1710 fp->index++;
1711}
1712static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1713 u32 cpos)
1714{
1715 struct find_path_data data;
1716
1717 data.index = 1;
1718 data.path = path;
1719 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1720 find_path_ins, &data);
1721}
1722
1723static void find_leaf_ins(void *data, struct buffer_head *bh)
1724{
1725 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1726 struct ocfs2_extent_list *el = &eb->h_list;
1727 struct buffer_head **ret = data;
1728
1729 /* We want to retain only the leaf block. */
1730 if (le16_to_cpu(el->l_tree_depth) == 0) {
1731 get_bh(bh);
1732 *ret = bh;
1733 }
1734}
1735/*
1736 * Find the leaf block in the tree which would contain cpos. No
1737 * checking of the actual leaf is done.
1738 *
1739 * Some paths want to call this instead of allocating a path structure
1740 * and calling ocfs2_find_path().
1741 *
1742 * This function doesn't handle non btree extent lists.
1743 */
Mark Fasheh363041a2007-01-17 12:31:35 -08001744int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1745 u32 cpos, struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001746{
1747 int ret;
1748 struct buffer_head *bh = NULL;
1749
1750 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1751 if (ret) {
1752 mlog_errno(ret);
1753 goto out;
1754 }
1755
1756 *leaf_bh = bh;
1757out:
1758 return ret;
1759}
1760
1761/*
1762 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1763 *
1764 * Basically, we've moved stuff around at the bottom of the tree and
1765 * we need to fix up the extent records above the changes to reflect
1766 * the new changes.
1767 *
1768 * left_rec: the record on the left.
1769 * left_child_el: is the child list pointed to by left_rec
1770 * right_rec: the record to the right of left_rec
1771 * right_child_el: is the child list pointed to by right_rec
1772 *
1773 * By definition, this only works on interior nodes.
1774 */
1775static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1776 struct ocfs2_extent_list *left_child_el,
1777 struct ocfs2_extent_rec *right_rec,
1778 struct ocfs2_extent_list *right_child_el)
1779{
1780 u32 left_clusters, right_end;
1781
1782 /*
1783 * Interior nodes never have holes. Their cpos is the cpos of
1784 * the leftmost record in their child list. Their cluster
1785 * count covers the full theoretical range of their child list
1786 * - the range between their cpos and the cpos of the record
1787 * immediately to their right.
1788 */
1789 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07001790 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1791 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1792 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1793 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001794 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001795 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001796
1797 /*
1798 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001799 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001800 * updating e_cpos to keep the same highest cluster count.
1801 */
1802 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001803 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001804
1805 right_rec->e_cpos = left_rec->e_cpos;
1806 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1807
1808 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001809 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001810}
1811
1812/*
1813 * Adjust the adjacent root node records involved in a
1814 * rotation. left_el_blkno is passed in as a key so that we can easily
1815 * find it's index in the root list.
1816 */
1817static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1818 struct ocfs2_extent_list *left_el,
1819 struct ocfs2_extent_list *right_el,
1820 u64 left_el_blkno)
1821{
1822 int i;
1823
1824 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1825 le16_to_cpu(left_el->l_tree_depth));
1826
1827 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1828 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1829 break;
1830 }
1831
1832 /*
1833 * The path walking code should have never returned a root and
1834 * two paths which are not adjacent.
1835 */
1836 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1837
1838 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1839 &root_el->l_recs[i + 1], right_el);
1840}
1841
1842/*
1843 * We've changed a leaf block (in right_path) and need to reflect that
1844 * change back up the subtree.
1845 *
1846 * This happens in multiple places:
1847 * - When we've moved an extent record from the left path leaf to the right
1848 * path leaf to make room for an empty extent in the left path leaf.
1849 * - When our insert into the right path leaf is at the leftmost edge
1850 * and requires an update of the path immediately to it's left. This
1851 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08001852 * - When we've adjusted the last extent record in the left path leaf and the
1853 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08001854 */
1855static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1856 struct ocfs2_path *left_path,
1857 struct ocfs2_path *right_path,
1858 int subtree_index)
1859{
1860 int ret, i, idx;
1861 struct ocfs2_extent_list *el, *left_el, *right_el;
1862 struct ocfs2_extent_rec *left_rec, *right_rec;
1863 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1864
1865 /*
1866 * Update the counts and position values within all the
1867 * interior nodes to reflect the leaf rotation we just did.
1868 *
1869 * The root node is handled below the loop.
1870 *
1871 * We begin the loop with right_el and left_el pointing to the
1872 * leaf lists and work our way up.
1873 *
1874 * NOTE: within this loop, left_el and right_el always refer
1875 * to the *child* lists.
1876 */
1877 left_el = path_leaf_el(left_path);
1878 right_el = path_leaf_el(right_path);
1879 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1880 mlog(0, "Adjust records at index %u\n", i);
1881
1882 /*
1883 * One nice property of knowing that all of these
1884 * nodes are below the root is that we only deal with
1885 * the leftmost right node record and the rightmost
1886 * left node record.
1887 */
1888 el = left_path->p_node[i].el;
1889 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1890 left_rec = &el->l_recs[idx];
1891
1892 el = right_path->p_node[i].el;
1893 right_rec = &el->l_recs[0];
1894
1895 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1896 right_el);
1897
1898 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1899 if (ret)
1900 mlog_errno(ret);
1901
1902 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1903 if (ret)
1904 mlog_errno(ret);
1905
1906 /*
1907 * Setup our list pointers now so that the current
1908 * parents become children in the next iteration.
1909 */
1910 left_el = left_path->p_node[i].el;
1911 right_el = right_path->p_node[i].el;
1912 }
1913
1914 /*
1915 * At the root node, adjust the two adjacent records which
1916 * begin our path to the leaves.
1917 */
1918
1919 el = left_path->p_node[subtree_index].el;
1920 left_el = left_path->p_node[subtree_index + 1].el;
1921 right_el = right_path->p_node[subtree_index + 1].el;
1922
1923 ocfs2_adjust_root_records(el, left_el, right_el,
1924 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1925
1926 root_bh = left_path->p_node[subtree_index].bh;
1927
1928 ret = ocfs2_journal_dirty(handle, root_bh);
1929 if (ret)
1930 mlog_errno(ret);
1931}
1932
1933static int ocfs2_rotate_subtree_right(struct inode *inode,
1934 handle_t *handle,
1935 struct ocfs2_path *left_path,
1936 struct ocfs2_path *right_path,
1937 int subtree_index)
1938{
1939 int ret, i;
1940 struct buffer_head *right_leaf_bh;
1941 struct buffer_head *left_leaf_bh = NULL;
1942 struct buffer_head *root_bh;
1943 struct ocfs2_extent_list *right_el, *left_el;
1944 struct ocfs2_extent_rec move_rec;
1945
1946 left_leaf_bh = path_leaf_bh(left_path);
1947 left_el = path_leaf_el(left_path);
1948
1949 if (left_el->l_next_free_rec != left_el->l_count) {
1950 ocfs2_error(inode->i_sb,
1951 "Inode %llu has non-full interior leaf node %llu"
1952 "(next free = %u)",
1953 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1954 (unsigned long long)left_leaf_bh->b_blocknr,
1955 le16_to_cpu(left_el->l_next_free_rec));
1956 return -EROFS;
1957 }
1958
1959 /*
1960 * This extent block may already have an empty record, so we
1961 * return early if so.
1962 */
1963 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1964 return 0;
1965
1966 root_bh = left_path->p_node[subtree_index].bh;
1967 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1968
Joel Becker13723d02008-10-17 19:25:01 -07001969 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
1970 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08001971 if (ret) {
1972 mlog_errno(ret);
1973 goto out;
1974 }
1975
1976 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker13723d02008-10-17 19:25:01 -07001977 ret = ocfs2_path_bh_journal_access(handle, inode,
1978 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08001979 if (ret) {
1980 mlog_errno(ret);
1981 goto out;
1982 }
1983
Joel Becker13723d02008-10-17 19:25:01 -07001984 ret = ocfs2_path_bh_journal_access(handle, inode,
1985 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08001986 if (ret) {
1987 mlog_errno(ret);
1988 goto out;
1989 }
1990 }
1991
1992 right_leaf_bh = path_leaf_bh(right_path);
1993 right_el = path_leaf_el(right_path);
1994
1995 /* This is a code error, not a disk corruption. */
1996 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1997 "because rightmost leaf block %llu is empty\n",
1998 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1999 (unsigned long long)right_leaf_bh->b_blocknr);
2000
2001 ocfs2_create_empty_extent(right_el);
2002
2003 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2004 if (ret) {
2005 mlog_errno(ret);
2006 goto out;
2007 }
2008
2009 /* Do the copy now. */
2010 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2011 move_rec = left_el->l_recs[i];
2012 right_el->l_recs[0] = move_rec;
2013
2014 /*
2015 * Clear out the record we just copied and shift everything
2016 * over, leaving an empty extent in the left leaf.
2017 *
2018 * We temporarily subtract from next_free_rec so that the
2019 * shift will lose the tail record (which is now defunct).
2020 */
2021 le16_add_cpu(&left_el->l_next_free_rec, -1);
2022 ocfs2_shift_records_right(left_el);
2023 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2024 le16_add_cpu(&left_el->l_next_free_rec, 1);
2025
2026 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2027 if (ret) {
2028 mlog_errno(ret);
2029 goto out;
2030 }
2031
2032 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2033 subtree_index);
2034
2035out:
2036 return ret;
2037}
2038
2039/*
2040 * Given a full path, determine what cpos value would return us a path
2041 * containing the leaf immediately to the left of the current one.
2042 *
2043 * Will return zero if the path passed in is already the leftmost path.
2044 */
2045static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2046 struct ocfs2_path *path, u32 *cpos)
2047{
2048 int i, j, ret = 0;
2049 u64 blkno;
2050 struct ocfs2_extent_list *el;
2051
Mark Fashehe48edee2007-03-07 16:46:57 -08002052 BUG_ON(path->p_tree_depth == 0);
2053
Mark Fashehdcd05382007-01-16 11:32:23 -08002054 *cpos = 0;
2055
2056 blkno = path_leaf_bh(path)->b_blocknr;
2057
2058 /* Start at the tree node just above the leaf and work our way up. */
2059 i = path->p_tree_depth - 1;
2060 while (i >= 0) {
2061 el = path->p_node[i].el;
2062
2063 /*
2064 * Find the extent record just before the one in our
2065 * path.
2066 */
2067 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2068 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2069 if (j == 0) {
2070 if (i == 0) {
2071 /*
2072 * We've determined that the
2073 * path specified is already
2074 * the leftmost one - return a
2075 * cpos of zero.
2076 */
2077 goto out;
2078 }
2079 /*
2080 * The leftmost record points to our
2081 * leaf - we need to travel up the
2082 * tree one level.
2083 */
2084 goto next_node;
2085 }
2086
2087 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002088 *cpos = *cpos + ocfs2_rec_clusters(el,
2089 &el->l_recs[j - 1]);
2090 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002091 goto out;
2092 }
2093 }
2094
2095 /*
2096 * If we got here, we never found a valid node where
2097 * the tree indicated one should be.
2098 */
2099 ocfs2_error(sb,
2100 "Invalid extent tree at extent block %llu\n",
2101 (unsigned long long)blkno);
2102 ret = -EROFS;
2103 goto out;
2104
2105next_node:
2106 blkno = path->p_node[i].bh->b_blocknr;
2107 i--;
2108 }
2109
2110out:
2111 return ret;
2112}
2113
Mark Fasheh328d5752007-06-18 10:48:04 -07002114/*
2115 * Extend the transaction by enough credits to complete the rotation,
2116 * and still leave at least the original number of credits allocated
2117 * to this transaction.
2118 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002119static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002120 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002121 struct ocfs2_path *path)
2122{
Mark Fasheh328d5752007-06-18 10:48:04 -07002123 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002124
2125 if (handle->h_buffer_credits < credits)
2126 return ocfs2_extend_trans(handle, credits);
2127
2128 return 0;
2129}
2130
2131/*
2132 * Trap the case where we're inserting into the theoretical range past
2133 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2134 * whose cpos is less than ours into the right leaf.
2135 *
2136 * It's only necessary to look at the rightmost record of the left
2137 * leaf because the logic that calls us should ensure that the
2138 * theoretical ranges in the path components above the leaves are
2139 * correct.
2140 */
2141static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2142 u32 insert_cpos)
2143{
2144 struct ocfs2_extent_list *left_el;
2145 struct ocfs2_extent_rec *rec;
2146 int next_free;
2147
2148 left_el = path_leaf_el(left_path);
2149 next_free = le16_to_cpu(left_el->l_next_free_rec);
2150 rec = &left_el->l_recs[next_free - 1];
2151
2152 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2153 return 1;
2154 return 0;
2155}
2156
Mark Fasheh328d5752007-06-18 10:48:04 -07002157static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2158{
2159 int next_free = le16_to_cpu(el->l_next_free_rec);
2160 unsigned int range;
2161 struct ocfs2_extent_rec *rec;
2162
2163 if (next_free == 0)
2164 return 0;
2165
2166 rec = &el->l_recs[0];
2167 if (ocfs2_is_empty_extent(rec)) {
2168 /* Empty list. */
2169 if (next_free == 1)
2170 return 0;
2171 rec = &el->l_recs[1];
2172 }
2173
2174 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2175 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2176 return 1;
2177 return 0;
2178}
2179
Mark Fashehdcd05382007-01-16 11:32:23 -08002180/*
2181 * Rotate all the records in a btree right one record, starting at insert_cpos.
2182 *
2183 * The path to the rightmost leaf should be passed in.
2184 *
2185 * The array is assumed to be large enough to hold an entire path (tree depth).
2186 *
2187 * Upon succesful return from this function:
2188 *
2189 * - The 'right_path' array will contain a path to the leaf block
2190 * whose range contains e_cpos.
2191 * - That leaf block will have a single empty extent in list index 0.
2192 * - In the case that the rotation requires a post-insert update,
2193 * *ret_left_path will contain a valid path which can be passed to
2194 * ocfs2_insert_path().
2195 */
2196static int ocfs2_rotate_tree_right(struct inode *inode,
2197 handle_t *handle,
Mark Fasheh328d5752007-06-18 10:48:04 -07002198 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002199 u32 insert_cpos,
2200 struct ocfs2_path *right_path,
2201 struct ocfs2_path **ret_left_path)
2202{
Mark Fasheh328d5752007-06-18 10:48:04 -07002203 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002204 u32 cpos;
2205 struct ocfs2_path *left_path = NULL;
2206
2207 *ret_left_path = NULL;
2208
Joel Beckerffdd7a52008-10-17 22:32:01 -07002209 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002210 if (!left_path) {
2211 ret = -ENOMEM;
2212 mlog_errno(ret);
2213 goto out;
2214 }
2215
2216 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2217 if (ret) {
2218 mlog_errno(ret);
2219 goto out;
2220 }
2221
2222 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2223
2224 /*
2225 * What we want to do here is:
2226 *
2227 * 1) Start with the rightmost path.
2228 *
2229 * 2) Determine a path to the leaf block directly to the left
2230 * of that leaf.
2231 *
2232 * 3) Determine the 'subtree root' - the lowest level tree node
2233 * which contains a path to both leaves.
2234 *
2235 * 4) Rotate the subtree.
2236 *
2237 * 5) Find the next subtree by considering the left path to be
2238 * the new right path.
2239 *
2240 * The check at the top of this while loop also accepts
2241 * insert_cpos == cpos because cpos is only a _theoretical_
2242 * value to get us the left path - insert_cpos might very well
2243 * be filling that hole.
2244 *
2245 * Stop at a cpos of '0' because we either started at the
2246 * leftmost branch (i.e., a tree with one branch and a
2247 * rotation inside of it), or we've gone as far as we can in
2248 * rotating subtrees.
2249 */
2250 while (cpos && insert_cpos <= cpos) {
2251 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2252 insert_cpos, cpos);
2253
2254 ret = ocfs2_find_path(inode, left_path, cpos);
2255 if (ret) {
2256 mlog_errno(ret);
2257 goto out;
2258 }
2259
2260 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2261 path_leaf_bh(right_path),
2262 "Inode %lu: error during insert of %u "
2263 "(left path cpos %u) results in two identical "
2264 "paths ending at %llu\n",
2265 inode->i_ino, insert_cpos, cpos,
2266 (unsigned long long)
2267 path_leaf_bh(left_path)->b_blocknr);
2268
Mark Fasheh328d5752007-06-18 10:48:04 -07002269 if (split == SPLIT_NONE &&
2270 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002271 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002272
2273 /*
2274 * We've rotated the tree as much as we
2275 * should. The rest is up to
2276 * ocfs2_insert_path() to complete, after the
2277 * record insertion. We indicate this
2278 * situation by returning the left path.
2279 *
2280 * The reason we don't adjust the records here
2281 * before the record insert is that an error
2282 * later might break the rule where a parent
2283 * record e_cpos will reflect the actual
2284 * e_cpos of the 1st nonempty record of the
2285 * child list.
2286 */
2287 *ret_left_path = left_path;
2288 goto out_ret_path;
2289 }
2290
2291 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2292
2293 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2294 start,
2295 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2296 right_path->p_tree_depth);
2297
2298 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002299 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002300 if (ret) {
2301 mlog_errno(ret);
2302 goto out;
2303 }
2304
2305 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2306 right_path, start);
2307 if (ret) {
2308 mlog_errno(ret);
2309 goto out;
2310 }
2311
Mark Fasheh328d5752007-06-18 10:48:04 -07002312 if (split != SPLIT_NONE &&
2313 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2314 insert_cpos)) {
2315 /*
2316 * A rotate moves the rightmost left leaf
2317 * record over to the leftmost right leaf
2318 * slot. If we're doing an extent split
2319 * instead of a real insert, then we have to
2320 * check that the extent to be split wasn't
2321 * just moved over. If it was, then we can
2322 * exit here, passing left_path back -
2323 * ocfs2_split_extent() is smart enough to
2324 * search both leaves.
2325 */
2326 *ret_left_path = left_path;
2327 goto out_ret_path;
2328 }
2329
Mark Fashehdcd05382007-01-16 11:32:23 -08002330 /*
2331 * There is no need to re-read the next right path
2332 * as we know that it'll be our current left
2333 * path. Optimize by copying values instead.
2334 */
2335 ocfs2_mv_path(right_path, left_path);
2336
2337 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2338 &cpos);
2339 if (ret) {
2340 mlog_errno(ret);
2341 goto out;
2342 }
2343 }
2344
2345out:
2346 ocfs2_free_path(left_path);
2347
2348out_ret_path:
2349 return ret;
2350}
2351
Mark Fasheh328d5752007-06-18 10:48:04 -07002352static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2353 struct ocfs2_path *path)
2354{
2355 int i, idx;
2356 struct ocfs2_extent_rec *rec;
2357 struct ocfs2_extent_list *el;
2358 struct ocfs2_extent_block *eb;
2359 u32 range;
2360
2361 /* Path should always be rightmost. */
2362 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2363 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2364
2365 el = &eb->h_list;
2366 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2367 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2368 rec = &el->l_recs[idx];
2369 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2370
2371 for (i = 0; i < path->p_tree_depth; i++) {
2372 el = path->p_node[i].el;
2373 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2374 rec = &el->l_recs[idx];
2375
2376 rec->e_int_clusters = cpu_to_le32(range);
2377 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2378
2379 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2380 }
2381}
2382
2383static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2384 struct ocfs2_cached_dealloc_ctxt *dealloc,
2385 struct ocfs2_path *path, int unlink_start)
2386{
2387 int ret, i;
2388 struct ocfs2_extent_block *eb;
2389 struct ocfs2_extent_list *el;
2390 struct buffer_head *bh;
2391
2392 for(i = unlink_start; i < path_num_items(path); i++) {
2393 bh = path->p_node[i].bh;
2394
2395 eb = (struct ocfs2_extent_block *)bh->b_data;
2396 /*
2397 * Not all nodes might have had their final count
2398 * decremented by the caller - handle this here.
2399 */
2400 el = &eb->h_list;
2401 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2402 mlog(ML_ERROR,
2403 "Inode %llu, attempted to remove extent block "
2404 "%llu with %u records\n",
2405 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2406 (unsigned long long)le64_to_cpu(eb->h_blkno),
2407 le16_to_cpu(el->l_next_free_rec));
2408
2409 ocfs2_journal_dirty(handle, bh);
2410 ocfs2_remove_from_cache(inode, bh);
2411 continue;
2412 }
2413
2414 el->l_next_free_rec = 0;
2415 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2416
2417 ocfs2_journal_dirty(handle, bh);
2418
2419 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2420 if (ret)
2421 mlog_errno(ret);
2422
2423 ocfs2_remove_from_cache(inode, bh);
2424 }
2425}
2426
2427static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2428 struct ocfs2_path *left_path,
2429 struct ocfs2_path *right_path,
2430 int subtree_index,
2431 struct ocfs2_cached_dealloc_ctxt *dealloc)
2432{
2433 int i;
2434 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2435 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2436 struct ocfs2_extent_list *el;
2437 struct ocfs2_extent_block *eb;
2438
2439 el = path_leaf_el(left_path);
2440
2441 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2442
2443 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2444 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2445 break;
2446
2447 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2448
2449 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2450 le16_add_cpu(&root_el->l_next_free_rec, -1);
2451
2452 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2453 eb->h_next_leaf_blk = 0;
2454
2455 ocfs2_journal_dirty(handle, root_bh);
2456 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2457
2458 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2459 subtree_index + 1);
2460}
2461
2462static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2463 struct ocfs2_path *left_path,
2464 struct ocfs2_path *right_path,
2465 int subtree_index,
2466 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002467 int *deleted,
2468 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002469{
2470 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002471 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002472 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2473 struct ocfs2_extent_block *eb;
2474
2475 *deleted = 0;
2476
2477 right_leaf_el = path_leaf_el(right_path);
2478 left_leaf_el = path_leaf_el(left_path);
2479 root_bh = left_path->p_node[subtree_index].bh;
2480 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2481
2482 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2483 return 0;
2484
2485 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2486 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2487 /*
2488 * It's legal for us to proceed if the right leaf is
2489 * the rightmost one and it has an empty extent. There
2490 * are two cases to handle - whether the leaf will be
2491 * empty after removal or not. If the leaf isn't empty
2492 * then just remove the empty extent up front. The
2493 * next block will handle empty leaves by flagging
2494 * them for unlink.
2495 *
2496 * Non rightmost leaves will throw -EAGAIN and the
2497 * caller can manually move the subtree and retry.
2498 */
2499
2500 if (eb->h_next_leaf_blk != 0ULL)
2501 return -EAGAIN;
2502
2503 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker13723d02008-10-17 19:25:01 -07002504 ret = ocfs2_journal_access_eb(handle, inode,
2505 path_leaf_bh(right_path),
2506 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002507 if (ret) {
2508 mlog_errno(ret);
2509 goto out;
2510 }
2511
2512 ocfs2_remove_empty_extent(right_leaf_el);
2513 } else
2514 right_has_empty = 1;
2515 }
2516
2517 if (eb->h_next_leaf_blk == 0ULL &&
2518 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2519 /*
2520 * We have to update i_last_eb_blk during the meta
2521 * data delete.
2522 */
Joel Becker13723d02008-10-17 19:25:01 -07002523 ret = ocfs2_et_root_journal_access(handle, inode, et,
2524 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002525 if (ret) {
2526 mlog_errno(ret);
2527 goto out;
2528 }
2529
2530 del_right_subtree = 1;
2531 }
2532
2533 /*
2534 * Getting here with an empty extent in the right path implies
2535 * that it's the rightmost path and will be deleted.
2536 */
2537 BUG_ON(right_has_empty && !del_right_subtree);
2538
Joel Becker13723d02008-10-17 19:25:01 -07002539 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
2540 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002541 if (ret) {
2542 mlog_errno(ret);
2543 goto out;
2544 }
2545
2546 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker13723d02008-10-17 19:25:01 -07002547 ret = ocfs2_path_bh_journal_access(handle, inode,
2548 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002549 if (ret) {
2550 mlog_errno(ret);
2551 goto out;
2552 }
2553
Joel Becker13723d02008-10-17 19:25:01 -07002554 ret = ocfs2_path_bh_journal_access(handle, inode,
2555 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002556 if (ret) {
2557 mlog_errno(ret);
2558 goto out;
2559 }
2560 }
2561
2562 if (!right_has_empty) {
2563 /*
2564 * Only do this if we're moving a real
2565 * record. Otherwise, the action is delayed until
2566 * after removal of the right path in which case we
2567 * can do a simple shift to remove the empty extent.
2568 */
2569 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2570 memset(&right_leaf_el->l_recs[0], 0,
2571 sizeof(struct ocfs2_extent_rec));
2572 }
2573 if (eb->h_next_leaf_blk == 0ULL) {
2574 /*
2575 * Move recs over to get rid of empty extent, decrease
2576 * next_free. This is allowed to remove the last
2577 * extent in our leaf (setting l_next_free_rec to
2578 * zero) - the delete code below won't care.
2579 */
2580 ocfs2_remove_empty_extent(right_leaf_el);
2581 }
2582
2583 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2584 if (ret)
2585 mlog_errno(ret);
2586 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2587 if (ret)
2588 mlog_errno(ret);
2589
2590 if (del_right_subtree) {
2591 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2592 subtree_index, dealloc);
2593 ocfs2_update_edge_lengths(inode, handle, left_path);
2594
2595 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002596 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002597
2598 /*
2599 * Removal of the extent in the left leaf was skipped
2600 * above so we could delete the right path
2601 * 1st.
2602 */
2603 if (right_has_empty)
2604 ocfs2_remove_empty_extent(left_leaf_el);
2605
Tao Mae7d4cb62008-08-18 17:38:44 +08002606 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002607 if (ret)
2608 mlog_errno(ret);
2609
2610 *deleted = 1;
2611 } else
2612 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2613 subtree_index);
2614
2615out:
2616 return ret;
2617}
2618
2619/*
2620 * Given a full path, determine what cpos value would return us a path
2621 * containing the leaf immediately to the right of the current one.
2622 *
2623 * Will return zero if the path passed in is already the rightmost path.
2624 *
2625 * This looks similar, but is subtly different to
2626 * ocfs2_find_cpos_for_left_leaf().
2627 */
2628static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2629 struct ocfs2_path *path, u32 *cpos)
2630{
2631 int i, j, ret = 0;
2632 u64 blkno;
2633 struct ocfs2_extent_list *el;
2634
2635 *cpos = 0;
2636
2637 if (path->p_tree_depth == 0)
2638 return 0;
2639
2640 blkno = path_leaf_bh(path)->b_blocknr;
2641
2642 /* Start at the tree node just above the leaf and work our way up. */
2643 i = path->p_tree_depth - 1;
2644 while (i >= 0) {
2645 int next_free;
2646
2647 el = path->p_node[i].el;
2648
2649 /*
2650 * Find the extent record just after the one in our
2651 * path.
2652 */
2653 next_free = le16_to_cpu(el->l_next_free_rec);
2654 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2655 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2656 if (j == (next_free - 1)) {
2657 if (i == 0) {
2658 /*
2659 * We've determined that the
2660 * path specified is already
2661 * the rightmost one - return a
2662 * cpos of zero.
2663 */
2664 goto out;
2665 }
2666 /*
2667 * The rightmost record points to our
2668 * leaf - we need to travel up the
2669 * tree one level.
2670 */
2671 goto next_node;
2672 }
2673
2674 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2675 goto out;
2676 }
2677 }
2678
2679 /*
2680 * If we got here, we never found a valid node where
2681 * the tree indicated one should be.
2682 */
2683 ocfs2_error(sb,
2684 "Invalid extent tree at extent block %llu\n",
2685 (unsigned long long)blkno);
2686 ret = -EROFS;
2687 goto out;
2688
2689next_node:
2690 blkno = path->p_node[i].bh->b_blocknr;
2691 i--;
2692 }
2693
2694out:
2695 return ret;
2696}
2697
2698static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2699 handle_t *handle,
Joel Becker13723d02008-10-17 19:25:01 -07002700 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002701{
2702 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002703 struct buffer_head *bh = path_leaf_bh(path);
2704 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002705
2706 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2707 return 0;
2708
Joel Becker13723d02008-10-17 19:25:01 -07002709 ret = ocfs2_path_bh_journal_access(handle, inode, path,
2710 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002711 if (ret) {
2712 mlog_errno(ret);
2713 goto out;
2714 }
2715
2716 ocfs2_remove_empty_extent(el);
2717
2718 ret = ocfs2_journal_dirty(handle, bh);
2719 if (ret)
2720 mlog_errno(ret);
2721
2722out:
2723 return ret;
2724}
2725
2726static int __ocfs2_rotate_tree_left(struct inode *inode,
2727 handle_t *handle, int orig_credits,
2728 struct ocfs2_path *path,
2729 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002730 struct ocfs2_path **empty_extent_path,
2731 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002732{
2733 int ret, subtree_root, deleted;
2734 u32 right_cpos;
2735 struct ocfs2_path *left_path = NULL;
2736 struct ocfs2_path *right_path = NULL;
2737
2738 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2739
2740 *empty_extent_path = NULL;
2741
2742 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2743 &right_cpos);
2744 if (ret) {
2745 mlog_errno(ret);
2746 goto out;
2747 }
2748
Joel Beckerffdd7a52008-10-17 22:32:01 -07002749 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002750 if (!left_path) {
2751 ret = -ENOMEM;
2752 mlog_errno(ret);
2753 goto out;
2754 }
2755
2756 ocfs2_cp_path(left_path, path);
2757
Joel Beckerffdd7a52008-10-17 22:32:01 -07002758 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002759 if (!right_path) {
2760 ret = -ENOMEM;
2761 mlog_errno(ret);
2762 goto out;
2763 }
2764
2765 while (right_cpos) {
2766 ret = ocfs2_find_path(inode, right_path, right_cpos);
2767 if (ret) {
2768 mlog_errno(ret);
2769 goto out;
2770 }
2771
2772 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2773 right_path);
2774
2775 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2776 subtree_root,
2777 (unsigned long long)
2778 right_path->p_node[subtree_root].bh->b_blocknr,
2779 right_path->p_tree_depth);
2780
2781 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2782 orig_credits, left_path);
2783 if (ret) {
2784 mlog_errno(ret);
2785 goto out;
2786 }
2787
Mark Fashehe8aed342007-12-03 16:43:01 -08002788 /*
2789 * Caller might still want to make changes to the
2790 * tree root, so re-add it to the journal here.
2791 */
Joel Becker13723d02008-10-17 19:25:01 -07002792 ret = ocfs2_path_bh_journal_access(handle, inode,
2793 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08002794 if (ret) {
2795 mlog_errno(ret);
2796 goto out;
2797 }
2798
Mark Fasheh328d5752007-06-18 10:48:04 -07002799 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2800 right_path, subtree_root,
Tao Mae7d4cb62008-08-18 17:38:44 +08002801 dealloc, &deleted, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002802 if (ret == -EAGAIN) {
2803 /*
2804 * The rotation has to temporarily stop due to
2805 * the right subtree having an empty
2806 * extent. Pass it back to the caller for a
2807 * fixup.
2808 */
2809 *empty_extent_path = right_path;
2810 right_path = NULL;
2811 goto out;
2812 }
2813 if (ret) {
2814 mlog_errno(ret);
2815 goto out;
2816 }
2817
2818 /*
2819 * The subtree rotate might have removed records on
2820 * the rightmost edge. If so, then rotation is
2821 * complete.
2822 */
2823 if (deleted)
2824 break;
2825
2826 ocfs2_mv_path(left_path, right_path);
2827
2828 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2829 &right_cpos);
2830 if (ret) {
2831 mlog_errno(ret);
2832 goto out;
2833 }
2834 }
2835
2836out:
2837 ocfs2_free_path(right_path);
2838 ocfs2_free_path(left_path);
2839
2840 return ret;
2841}
2842
2843static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08002844 struct ocfs2_path *path,
2845 struct ocfs2_cached_dealloc_ctxt *dealloc,
2846 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002847{
2848 int ret, subtree_index;
2849 u32 cpos;
2850 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07002851 struct ocfs2_extent_block *eb;
2852 struct ocfs2_extent_list *el;
2853
Mark Fasheh328d5752007-06-18 10:48:04 -07002854
Joel Becker35dc0aa2008-08-20 16:25:06 -07002855 ret = ocfs2_et_sanity_check(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002856 if (ret)
2857 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07002858 /*
2859 * There's two ways we handle this depending on
2860 * whether path is the only existing one.
2861 */
2862 ret = ocfs2_extend_rotate_transaction(handle, 0,
2863 handle->h_buffer_credits,
2864 path);
2865 if (ret) {
2866 mlog_errno(ret);
2867 goto out;
2868 }
2869
2870 ret = ocfs2_journal_access_path(inode, handle, path);
2871 if (ret) {
2872 mlog_errno(ret);
2873 goto out;
2874 }
2875
2876 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2877 if (ret) {
2878 mlog_errno(ret);
2879 goto out;
2880 }
2881
2882 if (cpos) {
2883 /*
2884 * We have a path to the left of this one - it needs
2885 * an update too.
2886 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07002887 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002888 if (!left_path) {
2889 ret = -ENOMEM;
2890 mlog_errno(ret);
2891 goto out;
2892 }
2893
2894 ret = ocfs2_find_path(inode, left_path, cpos);
2895 if (ret) {
2896 mlog_errno(ret);
2897 goto out;
2898 }
2899
2900 ret = ocfs2_journal_access_path(inode, handle, left_path);
2901 if (ret) {
2902 mlog_errno(ret);
2903 goto out;
2904 }
2905
2906 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2907
2908 ocfs2_unlink_subtree(inode, handle, left_path, path,
2909 subtree_index, dealloc);
2910 ocfs2_update_edge_lengths(inode, handle, left_path);
2911
2912 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002913 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002914 } else {
2915 /*
2916 * 'path' is also the leftmost path which
2917 * means it must be the only one. This gets
2918 * handled differently because we want to
2919 * revert the inode back to having extents
2920 * in-line.
2921 */
2922 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2923
Joel Beckerce1d9ea2008-08-20 16:30:07 -07002924 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07002925 el->l_tree_depth = 0;
2926 el->l_next_free_rec = 0;
2927 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2928
Joel Becker35dc0aa2008-08-20 16:25:06 -07002929 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07002930 }
2931
2932 ocfs2_journal_dirty(handle, path_root_bh(path));
2933
2934out:
2935 ocfs2_free_path(left_path);
2936 return ret;
2937}
2938
2939/*
2940 * Left rotation of btree records.
2941 *
2942 * In many ways, this is (unsurprisingly) the opposite of right
2943 * rotation. We start at some non-rightmost path containing an empty
2944 * extent in the leaf block. The code works its way to the rightmost
2945 * path by rotating records to the left in every subtree.
2946 *
2947 * This is used by any code which reduces the number of extent records
2948 * in a leaf. After removal, an empty record should be placed in the
2949 * leftmost list position.
2950 *
2951 * This won't handle a length update of the rightmost path records if
2952 * the rightmost tree leaf record is removed so the caller is
2953 * responsible for detecting and correcting that.
2954 */
2955static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2956 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002957 struct ocfs2_cached_dealloc_ctxt *dealloc,
2958 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002959{
2960 int ret, orig_credits = handle->h_buffer_credits;
2961 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2962 struct ocfs2_extent_block *eb;
2963 struct ocfs2_extent_list *el;
2964
2965 el = path_leaf_el(path);
2966 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2967 return 0;
2968
2969 if (path->p_tree_depth == 0) {
2970rightmost_no_delete:
2971 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08002972 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07002973 * it up front.
2974 */
2975 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
Joel Becker13723d02008-10-17 19:25:01 -07002976 path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002977 if (ret)
2978 mlog_errno(ret);
2979 goto out;
2980 }
2981
2982 /*
2983 * Handle rightmost branch now. There's several cases:
2984 * 1) simple rotation leaving records in there. That's trivial.
2985 * 2) rotation requiring a branch delete - there's no more
2986 * records left. Two cases of this:
2987 * a) There are branches to the left.
2988 * b) This is also the leftmost (the only) branch.
2989 *
2990 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2991 * 2a) we need the left branch so that we can update it with the unlink
2992 * 2b) we need to bring the inode back to inline extents.
2993 */
2994
2995 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2996 el = &eb->h_list;
2997 if (eb->h_next_leaf_blk == 0) {
2998 /*
2999 * This gets a bit tricky if we're going to delete the
3000 * rightmost path. Get the other cases out of the way
3001 * 1st.
3002 */
3003 if (le16_to_cpu(el->l_next_free_rec) > 1)
3004 goto rightmost_no_delete;
3005
3006 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3007 ret = -EIO;
3008 ocfs2_error(inode->i_sb,
3009 "Inode %llu has empty extent block at %llu",
3010 (unsigned long long)OCFS2_I(inode)->ip_blkno,
3011 (unsigned long long)le64_to_cpu(eb->h_blkno));
3012 goto out;
3013 }
3014
3015 /*
3016 * XXX: The caller can not trust "path" any more after
3017 * this as it will have been deleted. What do we do?
3018 *
3019 * In theory the rotate-for-merge code will never get
3020 * here because it'll always ask for a rotate in a
3021 * nonempty list.
3022 */
3023
3024 ret = ocfs2_remove_rightmost_path(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003025 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003026 if (ret)
3027 mlog_errno(ret);
3028 goto out;
3029 }
3030
3031 /*
3032 * Now we can loop, remembering the path we get from -EAGAIN
3033 * and restarting from there.
3034 */
3035try_rotate:
3036 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003037 dealloc, &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003038 if (ret && ret != -EAGAIN) {
3039 mlog_errno(ret);
3040 goto out;
3041 }
3042
3043 while (ret == -EAGAIN) {
3044 tmp_path = restart_path;
3045 restart_path = NULL;
3046
3047 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
3048 tmp_path, dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003049 &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003050 if (ret && ret != -EAGAIN) {
3051 mlog_errno(ret);
3052 goto out;
3053 }
3054
3055 ocfs2_free_path(tmp_path);
3056 tmp_path = NULL;
3057
3058 if (ret == 0)
3059 goto try_rotate;
3060 }
3061
3062out:
3063 ocfs2_free_path(tmp_path);
3064 ocfs2_free_path(restart_path);
3065 return ret;
3066}
3067
3068static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3069 int index)
3070{
3071 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3072 unsigned int size;
3073
3074 if (rec->e_leaf_clusters == 0) {
3075 /*
3076 * We consumed all of the merged-from record. An empty
3077 * extent cannot exist anywhere but the 1st array
3078 * position, so move things over if the merged-from
3079 * record doesn't occupy that position.
3080 *
3081 * This creates a new empty extent so the caller
3082 * should be smart enough to have removed any existing
3083 * ones.
3084 */
3085 if (index > 0) {
3086 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3087 size = index * sizeof(struct ocfs2_extent_rec);
3088 memmove(&el->l_recs[1], &el->l_recs[0], size);
3089 }
3090
3091 /*
3092 * Always memset - the caller doesn't check whether it
3093 * created an empty extent, so there could be junk in
3094 * the other fields.
3095 */
3096 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3097 }
3098}
3099
Tao Ma677b9752008-01-30 14:21:05 +08003100static int ocfs2_get_right_path(struct inode *inode,
3101 struct ocfs2_path *left_path,
3102 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003103{
3104 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003105 u32 right_cpos;
3106 struct ocfs2_path *right_path = NULL;
3107 struct ocfs2_extent_list *left_el;
3108
3109 *ret_right_path = NULL;
3110
3111 /* This function shouldn't be called for non-trees. */
3112 BUG_ON(left_path->p_tree_depth == 0);
3113
3114 left_el = path_leaf_el(left_path);
3115 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3116
3117 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
3118 &right_cpos);
3119 if (ret) {
3120 mlog_errno(ret);
3121 goto out;
3122 }
3123
3124 /* This function shouldn't be called for the rightmost leaf. */
3125 BUG_ON(right_cpos == 0);
3126
Joel Beckerffdd7a52008-10-17 22:32:01 -07003127 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003128 if (!right_path) {
3129 ret = -ENOMEM;
3130 mlog_errno(ret);
3131 goto out;
3132 }
3133
3134 ret = ocfs2_find_path(inode, right_path, right_cpos);
3135 if (ret) {
3136 mlog_errno(ret);
3137 goto out;
3138 }
3139
3140 *ret_right_path = right_path;
3141out:
3142 if (ret)
3143 ocfs2_free_path(right_path);
3144 return ret;
3145}
3146
3147/*
3148 * Remove split_rec clusters from the record at index and merge them
3149 * onto the beginning of the record "next" to it.
3150 * For index < l_count - 1, the next means the extent rec at index + 1.
3151 * For index == l_count - 1, the "next" means the 1st extent rec of the
3152 * next extent block.
3153 */
3154static int ocfs2_merge_rec_right(struct inode *inode,
3155 struct ocfs2_path *left_path,
3156 handle_t *handle,
3157 struct ocfs2_extent_rec *split_rec,
3158 int index)
3159{
3160 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003161 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3162 struct ocfs2_extent_rec *left_rec;
3163 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003164 struct ocfs2_extent_list *right_el;
3165 struct ocfs2_path *right_path = NULL;
3166 int subtree_index = 0;
3167 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3168 struct buffer_head *bh = path_leaf_bh(left_path);
3169 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003170
3171 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003172 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003173
Al Viro9d8df6a2008-05-21 06:32:11 +01003174 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003175 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3176 /* we meet with a cross extent block merge. */
3177 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3178 if (ret) {
3179 mlog_errno(ret);
3180 goto out;
3181 }
3182
3183 right_el = path_leaf_el(right_path);
3184 next_free = le16_to_cpu(right_el->l_next_free_rec);
3185 BUG_ON(next_free <= 0);
3186 right_rec = &right_el->l_recs[0];
3187 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003188 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003189 right_rec = &right_el->l_recs[1];
3190 }
3191
3192 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3193 le16_to_cpu(left_rec->e_leaf_clusters) !=
3194 le32_to_cpu(right_rec->e_cpos));
3195
3196 subtree_index = ocfs2_find_subtree_root(inode,
3197 left_path, right_path);
3198
3199 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3200 handle->h_buffer_credits,
3201 right_path);
3202 if (ret) {
3203 mlog_errno(ret);
3204 goto out;
3205 }
3206
3207 root_bh = left_path->p_node[subtree_index].bh;
3208 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3209
Joel Becker13723d02008-10-17 19:25:01 -07003210 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
3211 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003212 if (ret) {
3213 mlog_errno(ret);
3214 goto out;
3215 }
3216
3217 for (i = subtree_index + 1;
3218 i < path_num_items(right_path); i++) {
Joel Becker13723d02008-10-17 19:25:01 -07003219 ret = ocfs2_path_bh_journal_access(handle, inode,
3220 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003221 if (ret) {
3222 mlog_errno(ret);
3223 goto out;
3224 }
3225
Joel Becker13723d02008-10-17 19:25:01 -07003226 ret = ocfs2_path_bh_journal_access(handle, inode,
3227 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003228 if (ret) {
3229 mlog_errno(ret);
3230 goto out;
3231 }
3232 }
3233
3234 } else {
3235 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3236 right_rec = &el->l_recs[index + 1];
3237 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003238
Joel Becker13723d02008-10-17 19:25:01 -07003239 ret = ocfs2_path_bh_journal_access(handle, inode, left_path,
3240 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003241 if (ret) {
3242 mlog_errno(ret);
3243 goto out;
3244 }
3245
3246 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3247
3248 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3249 le64_add_cpu(&right_rec->e_blkno,
3250 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3251 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3252
3253 ocfs2_cleanup_merge(el, index);
3254
3255 ret = ocfs2_journal_dirty(handle, bh);
3256 if (ret)
3257 mlog_errno(ret);
3258
Tao Ma677b9752008-01-30 14:21:05 +08003259 if (right_path) {
3260 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3261 if (ret)
3262 mlog_errno(ret);
3263
3264 ocfs2_complete_edge_insert(inode, handle, left_path,
3265 right_path, subtree_index);
3266 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003267out:
Tao Ma677b9752008-01-30 14:21:05 +08003268 if (right_path)
3269 ocfs2_free_path(right_path);
3270 return ret;
3271}
3272
3273static int ocfs2_get_left_path(struct inode *inode,
3274 struct ocfs2_path *right_path,
3275 struct ocfs2_path **ret_left_path)
3276{
3277 int ret;
3278 u32 left_cpos;
3279 struct ocfs2_path *left_path = NULL;
3280
3281 *ret_left_path = NULL;
3282
3283 /* This function shouldn't be called for non-trees. */
3284 BUG_ON(right_path->p_tree_depth == 0);
3285
3286 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3287 right_path, &left_cpos);
3288 if (ret) {
3289 mlog_errno(ret);
3290 goto out;
3291 }
3292
3293 /* This function shouldn't be called for the leftmost leaf. */
3294 BUG_ON(left_cpos == 0);
3295
Joel Beckerffdd7a52008-10-17 22:32:01 -07003296 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003297 if (!left_path) {
3298 ret = -ENOMEM;
3299 mlog_errno(ret);
3300 goto out;
3301 }
3302
3303 ret = ocfs2_find_path(inode, left_path, left_cpos);
3304 if (ret) {
3305 mlog_errno(ret);
3306 goto out;
3307 }
3308
3309 *ret_left_path = left_path;
3310out:
3311 if (ret)
3312 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003313 return ret;
3314}
3315
3316/*
3317 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003318 * onto the tail of the record "before" it.
3319 * For index > 0, the "before" means the extent rec at index - 1.
3320 *
3321 * For index == 0, the "before" means the last record of the previous
3322 * extent block. And there is also a situation that we may need to
3323 * remove the rightmost leaf extent block in the right_path and change
3324 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003325 */
Tao Ma677b9752008-01-30 14:21:05 +08003326static int ocfs2_merge_rec_left(struct inode *inode,
3327 struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003328 handle_t *handle,
3329 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003330 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003331 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003332 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003333{
Tao Ma677b9752008-01-30 14:21:05 +08003334 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003335 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3336 struct ocfs2_extent_rec *left_rec;
3337 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003338 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3339 struct buffer_head *bh = path_leaf_bh(right_path);
3340 struct buffer_head *root_bh = NULL;
3341 struct ocfs2_path *left_path = NULL;
3342 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003343
Tao Ma677b9752008-01-30 14:21:05 +08003344 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003345
Mark Fasheh328d5752007-06-18 10:48:04 -07003346 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003347 if (index == 0) {
3348 /* we meet with a cross extent block merge. */
3349 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3350 if (ret) {
3351 mlog_errno(ret);
3352 goto out;
3353 }
3354
3355 left_el = path_leaf_el(left_path);
3356 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3357 le16_to_cpu(left_el->l_count));
3358
3359 left_rec = &left_el->l_recs[
3360 le16_to_cpu(left_el->l_next_free_rec) - 1];
3361 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3362 le16_to_cpu(left_rec->e_leaf_clusters) !=
3363 le32_to_cpu(split_rec->e_cpos));
3364
3365 subtree_index = ocfs2_find_subtree_root(inode,
3366 left_path, right_path);
3367
3368 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3369 handle->h_buffer_credits,
3370 left_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 Becker13723d02008-10-17 19:25:01 -07003379 ret = ocfs2_path_bh_journal_access(handle, inode, right_path,
3380 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 Becker13723d02008-10-17 19:25:01 -07003388 ret = ocfs2_path_bh_journal_access(handle, inode,
3389 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003390 if (ret) {
3391 mlog_errno(ret);
3392 goto out;
3393 }
3394
Joel Becker13723d02008-10-17 19:25:01 -07003395 ret = ocfs2_path_bh_journal_access(handle, inode,
3396 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003397 if (ret) {
3398 mlog_errno(ret);
3399 goto out;
3400 }
3401 }
3402 } else {
3403 left_rec = &el->l_recs[index - 1];
3404 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3405 has_empty_extent = 1;
3406 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003407
Joel Becker13723d02008-10-17 19:25:01 -07003408 ret = ocfs2_path_bh_journal_access(handle, inode, left_path,
3409 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 if (has_empty_extent && index == 1) {
3416 /*
3417 * The easy case - we can just plop the record right in.
3418 */
3419 *left_rec = *split_rec;
3420
3421 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003422 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003423 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003424
3425 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3426 le64_add_cpu(&right_rec->e_blkno,
3427 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3428 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3429
3430 ocfs2_cleanup_merge(el, index);
3431
3432 ret = ocfs2_journal_dirty(handle, bh);
3433 if (ret)
3434 mlog_errno(ret);
3435
Tao Ma677b9752008-01-30 14:21:05 +08003436 if (left_path) {
3437 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3438 if (ret)
3439 mlog_errno(ret);
3440
3441 /*
3442 * In the situation that the right_rec is empty and the extent
3443 * block is empty also, ocfs2_complete_edge_insert can't handle
3444 * it and we need to delete the right extent block.
3445 */
3446 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3447 le16_to_cpu(el->l_next_free_rec) == 1) {
3448
3449 ret = ocfs2_remove_rightmost_path(inode, handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003450 right_path,
3451 dealloc, et);
Tao Ma677b9752008-01-30 14:21:05 +08003452 if (ret) {
3453 mlog_errno(ret);
3454 goto out;
3455 }
3456
3457 /* Now the rightmost extent block has been deleted.
3458 * So we use the new rightmost path.
3459 */
3460 ocfs2_mv_path(right_path, left_path);
3461 left_path = NULL;
3462 } else
3463 ocfs2_complete_edge_insert(inode, handle, left_path,
3464 right_path, subtree_index);
3465 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003466out:
Tao Ma677b9752008-01-30 14:21:05 +08003467 if (left_path)
3468 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003469 return ret;
3470}
3471
3472static int ocfs2_try_to_merge_extent(struct inode *inode,
3473 handle_t *handle,
Tao Ma677b9752008-01-30 14:21:05 +08003474 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003475 int split_index,
3476 struct ocfs2_extent_rec *split_rec,
3477 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003478 struct ocfs2_merge_ctxt *ctxt,
3479 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003480
3481{
Tao Mao518d7262007-08-28 17:25:35 -07003482 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003483 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003484 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3485
3486 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3487
Tao Mao518d7262007-08-28 17:25:35 -07003488 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3489 /*
3490 * The merge code will need to create an empty
3491 * extent to take the place of the newly
3492 * emptied slot. Remove any pre-existing empty
3493 * extents - having more than one in a leaf is
3494 * illegal.
3495 */
Tao Ma677b9752008-01-30 14:21:05 +08003496 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003497 dealloc, et);
Tao Mao518d7262007-08-28 17:25:35 -07003498 if (ret) {
3499 mlog_errno(ret);
3500 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003501 }
Tao Mao518d7262007-08-28 17:25:35 -07003502 split_index--;
3503 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003504 }
3505
3506 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3507 /*
3508 * Left-right contig implies this.
3509 */
3510 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003511
3512 /*
3513 * Since the leftright insert always covers the entire
3514 * extent, this call will delete the insert record
3515 * entirely, resulting in an empty extent record added to
3516 * the extent block.
3517 *
3518 * Since the adding of an empty extent shifts
3519 * everything back to the right, there's no need to
3520 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003521 *
3522 * When the split_index is zero, we need to merge it to the
3523 * prevoius extent block. It is more efficient and easier
3524 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003525 */
Tao Ma677b9752008-01-30 14:21:05 +08003526 ret = ocfs2_merge_rec_right(inode, path,
3527 handle, split_rec,
3528 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003529 if (ret) {
3530 mlog_errno(ret);
3531 goto out;
3532 }
3533
3534 /*
3535 * We can only get this from logic error above.
3536 */
3537 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3538
Tao Ma677b9752008-01-30 14:21:05 +08003539 /* The merge left us with an empty extent, remove it. */
Tao Mae7d4cb62008-08-18 17:38:44 +08003540 ret = ocfs2_rotate_tree_left(inode, handle, path,
3541 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003542 if (ret) {
3543 mlog_errno(ret);
3544 goto out;
3545 }
Tao Ma677b9752008-01-30 14:21:05 +08003546
Mark Fasheh328d5752007-06-18 10:48:04 -07003547 rec = &el->l_recs[split_index];
3548
3549 /*
3550 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003551 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003552 */
Tao Ma677b9752008-01-30 14:21:05 +08003553 ret = ocfs2_merge_rec_left(inode, path,
3554 handle, rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003555 dealloc, et,
Tao Ma677b9752008-01-30 14:21:05 +08003556 split_index);
3557
Mark Fasheh328d5752007-06-18 10:48:04 -07003558 if (ret) {
3559 mlog_errno(ret);
3560 goto out;
3561 }
3562
Tao Ma677b9752008-01-30 14:21:05 +08003563 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003564 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003565 /*
3566 * Error from this last rotate is not critical, so
3567 * print but don't bubble it up.
3568 */
3569 if (ret)
3570 mlog_errno(ret);
3571 ret = 0;
3572 } else {
3573 /*
3574 * Merge a record to the left or right.
3575 *
3576 * 'contig_type' is relative to the existing record,
3577 * so for example, if we're "right contig", it's to
3578 * the record on the left (hence the left merge).
3579 */
3580 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3581 ret = ocfs2_merge_rec_left(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003582 path,
3583 handle, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003584 dealloc, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003585 split_index);
3586 if (ret) {
3587 mlog_errno(ret);
3588 goto out;
3589 }
3590 } else {
3591 ret = ocfs2_merge_rec_right(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003592 path,
3593 handle, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003594 split_index);
3595 if (ret) {
3596 mlog_errno(ret);
3597 goto out;
3598 }
3599 }
3600
3601 if (ctxt->c_split_covers_rec) {
3602 /*
3603 * The merge may have left an empty extent in
3604 * our leaf. Try to rotate it away.
3605 */
Tao Ma677b9752008-01-30 14:21:05 +08003606 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003607 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003608 if (ret)
3609 mlog_errno(ret);
3610 ret = 0;
3611 }
3612 }
3613
3614out:
3615 return ret;
3616}
3617
3618static void ocfs2_subtract_from_rec(struct super_block *sb,
3619 enum ocfs2_split_type split,
3620 struct ocfs2_extent_rec *rec,
3621 struct ocfs2_extent_rec *split_rec)
3622{
3623 u64 len_blocks;
3624
3625 len_blocks = ocfs2_clusters_to_blocks(sb,
3626 le16_to_cpu(split_rec->e_leaf_clusters));
3627
3628 if (split == SPLIT_LEFT) {
3629 /*
3630 * Region is on the left edge of the existing
3631 * record.
3632 */
3633 le32_add_cpu(&rec->e_cpos,
3634 le16_to_cpu(split_rec->e_leaf_clusters));
3635 le64_add_cpu(&rec->e_blkno, len_blocks);
3636 le16_add_cpu(&rec->e_leaf_clusters,
3637 -le16_to_cpu(split_rec->e_leaf_clusters));
3638 } else {
3639 /*
3640 * Region is on the right edge of the existing
3641 * record.
3642 */
3643 le16_add_cpu(&rec->e_leaf_clusters,
3644 -le16_to_cpu(split_rec->e_leaf_clusters));
3645 }
3646}
3647
Mark Fashehdcd05382007-01-16 11:32:23 -08003648/*
3649 * Do the final bits of extent record insertion at the target leaf
3650 * list. If this leaf is part of an allocation tree, it is assumed
3651 * that the tree above has been prepared.
3652 */
3653static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3654 struct ocfs2_extent_list *el,
3655 struct ocfs2_insert_type *insert,
3656 struct inode *inode)
3657{
3658 int i = insert->ins_contig_index;
3659 unsigned int range;
3660 struct ocfs2_extent_rec *rec;
3661
Mark Fashehe48edee2007-03-07 16:46:57 -08003662 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003663
Mark Fasheh328d5752007-06-18 10:48:04 -07003664 if (insert->ins_split != SPLIT_NONE) {
3665 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3666 BUG_ON(i == -1);
3667 rec = &el->l_recs[i];
3668 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3669 insert_rec);
3670 goto rotate;
3671 }
3672
Mark Fashehdcd05382007-01-16 11:32:23 -08003673 /*
3674 * Contiguous insert - either left or right.
3675 */
3676 if (insert->ins_contig != CONTIG_NONE) {
3677 rec = &el->l_recs[i];
3678 if (insert->ins_contig == CONTIG_LEFT) {
3679 rec->e_blkno = insert_rec->e_blkno;
3680 rec->e_cpos = insert_rec->e_cpos;
3681 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003682 le16_add_cpu(&rec->e_leaf_clusters,
3683 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003684 return;
3685 }
3686
3687 /*
3688 * Handle insert into an empty leaf.
3689 */
3690 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3691 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3692 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3693 el->l_recs[0] = *insert_rec;
3694 el->l_next_free_rec = cpu_to_le16(1);
3695 return;
3696 }
3697
3698 /*
3699 * Appending insert.
3700 */
3701 if (insert->ins_appending == APPEND_TAIL) {
3702 i = le16_to_cpu(el->l_next_free_rec) - 1;
3703 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003704 range = le32_to_cpu(rec->e_cpos)
3705 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003706 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3707
3708 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3709 le16_to_cpu(el->l_count),
3710 "inode %lu, depth %u, count %u, next free %u, "
3711 "rec.cpos %u, rec.clusters %u, "
3712 "insert.cpos %u, insert.clusters %u\n",
3713 inode->i_ino,
3714 le16_to_cpu(el->l_tree_depth),
3715 le16_to_cpu(el->l_count),
3716 le16_to_cpu(el->l_next_free_rec),
3717 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003718 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003719 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003720 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003721 i++;
3722 el->l_recs[i] = *insert_rec;
3723 le16_add_cpu(&el->l_next_free_rec, 1);
3724 return;
3725 }
3726
Mark Fasheh328d5752007-06-18 10:48:04 -07003727rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003728 /*
3729 * Ok, we have to rotate.
3730 *
3731 * At this point, it is safe to assume that inserting into an
3732 * empty leaf and appending to a leaf have both been handled
3733 * above.
3734 *
3735 * This leaf needs to have space, either by the empty 1st
3736 * extent record, or by virtue of an l_next_rec < l_count.
3737 */
3738 ocfs2_rotate_leaf(el, insert_rec);
3739}
3740
Mark Fasheh328d5752007-06-18 10:48:04 -07003741static void ocfs2_adjust_rightmost_records(struct inode *inode,
3742 handle_t *handle,
3743 struct ocfs2_path *path,
3744 struct ocfs2_extent_rec *insert_rec)
3745{
3746 int ret, i, next_free;
3747 struct buffer_head *bh;
3748 struct ocfs2_extent_list *el;
3749 struct ocfs2_extent_rec *rec;
3750
3751 /*
3752 * Update everything except the leaf block.
3753 */
3754 for (i = 0; i < path->p_tree_depth; i++) {
3755 bh = path->p_node[i].bh;
3756 el = path->p_node[i].el;
3757
3758 next_free = le16_to_cpu(el->l_next_free_rec);
3759 if (next_free == 0) {
3760 ocfs2_error(inode->i_sb,
3761 "Dinode %llu has a bad extent list",
3762 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3763 ret = -EIO;
3764 return;
3765 }
3766
3767 rec = &el->l_recs[next_free - 1];
3768
3769 rec->e_int_clusters = insert_rec->e_cpos;
3770 le32_add_cpu(&rec->e_int_clusters,
3771 le16_to_cpu(insert_rec->e_leaf_clusters));
3772 le32_add_cpu(&rec->e_int_clusters,
3773 -le32_to_cpu(rec->e_cpos));
3774
3775 ret = ocfs2_journal_dirty(handle, bh);
3776 if (ret)
3777 mlog_errno(ret);
3778
3779 }
3780}
3781
Mark Fashehdcd05382007-01-16 11:32:23 -08003782static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3783 struct ocfs2_extent_rec *insert_rec,
3784 struct ocfs2_path *right_path,
3785 struct ocfs2_path **ret_left_path)
3786{
Mark Fasheh328d5752007-06-18 10:48:04 -07003787 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003788 struct ocfs2_extent_list *el;
3789 struct ocfs2_path *left_path = NULL;
3790
3791 *ret_left_path = NULL;
3792
3793 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003794 * This shouldn't happen for non-trees. The extent rec cluster
3795 * count manipulation below only works for interior nodes.
3796 */
3797 BUG_ON(right_path->p_tree_depth == 0);
3798
3799 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003800 * If our appending insert is at the leftmost edge of a leaf,
3801 * then we might need to update the rightmost records of the
3802 * neighboring path.
3803 */
3804 el = path_leaf_el(right_path);
3805 next_free = le16_to_cpu(el->l_next_free_rec);
3806 if (next_free == 0 ||
3807 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3808 u32 left_cpos;
3809
3810 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3811 &left_cpos);
3812 if (ret) {
3813 mlog_errno(ret);
3814 goto out;
3815 }
3816
3817 mlog(0, "Append may need a left path update. cpos: %u, "
3818 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3819 left_cpos);
3820
3821 /*
3822 * No need to worry if the append is already in the
3823 * leftmost leaf.
3824 */
3825 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07003826 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003827 if (!left_path) {
3828 ret = -ENOMEM;
3829 mlog_errno(ret);
3830 goto out;
3831 }
3832
3833 ret = ocfs2_find_path(inode, left_path, left_cpos);
3834 if (ret) {
3835 mlog_errno(ret);
3836 goto out;
3837 }
3838
3839 /*
3840 * ocfs2_insert_path() will pass the left_path to the
3841 * journal for us.
3842 */
3843 }
3844 }
3845
3846 ret = ocfs2_journal_access_path(inode, handle, right_path);
3847 if (ret) {
3848 mlog_errno(ret);
3849 goto out;
3850 }
3851
Mark Fasheh328d5752007-06-18 10:48:04 -07003852 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08003853
3854 *ret_left_path = left_path;
3855 ret = 0;
3856out:
3857 if (ret != 0)
3858 ocfs2_free_path(left_path);
3859
3860 return ret;
3861}
3862
Mark Fasheh328d5752007-06-18 10:48:04 -07003863static void ocfs2_split_record(struct inode *inode,
3864 struct ocfs2_path *left_path,
3865 struct ocfs2_path *right_path,
3866 struct ocfs2_extent_rec *split_rec,
3867 enum ocfs2_split_type split)
3868{
3869 int index;
3870 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3871 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3872 struct ocfs2_extent_rec *rec, *tmprec;
3873
3874 right_el = path_leaf_el(right_path);;
3875 if (left_path)
3876 left_el = path_leaf_el(left_path);
3877
3878 el = right_el;
3879 insert_el = right_el;
3880 index = ocfs2_search_extent_list(el, cpos);
3881 if (index != -1) {
3882 if (index == 0 && left_path) {
3883 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3884
3885 /*
3886 * This typically means that the record
3887 * started in the left path but moved to the
3888 * right as a result of rotation. We either
3889 * move the existing record to the left, or we
3890 * do the later insert there.
3891 *
3892 * In this case, the left path should always
3893 * exist as the rotate code will have passed
3894 * it back for a post-insert update.
3895 */
3896
3897 if (split == SPLIT_LEFT) {
3898 /*
3899 * It's a left split. Since we know
3900 * that the rotate code gave us an
3901 * empty extent in the left path, we
3902 * can just do the insert there.
3903 */
3904 insert_el = left_el;
3905 } else {
3906 /*
3907 * Right split - we have to move the
3908 * existing record over to the left
3909 * leaf. The insert will be into the
3910 * newly created empty extent in the
3911 * right leaf.
3912 */
3913 tmprec = &right_el->l_recs[index];
3914 ocfs2_rotate_leaf(left_el, tmprec);
3915 el = left_el;
3916
3917 memset(tmprec, 0, sizeof(*tmprec));
3918 index = ocfs2_search_extent_list(left_el, cpos);
3919 BUG_ON(index == -1);
3920 }
3921 }
3922 } else {
3923 BUG_ON(!left_path);
3924 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3925 /*
3926 * Left path is easy - we can just allow the insert to
3927 * happen.
3928 */
3929 el = left_el;
3930 insert_el = left_el;
3931 index = ocfs2_search_extent_list(el, cpos);
3932 BUG_ON(index == -1);
3933 }
3934
3935 rec = &el->l_recs[index];
3936 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3937 ocfs2_rotate_leaf(insert_el, split_rec);
3938}
3939
Mark Fashehdcd05382007-01-16 11:32:23 -08003940/*
Tao Mae7d4cb62008-08-18 17:38:44 +08003941 * This function only does inserts on an allocation b-tree. For tree
3942 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08003943 *
3944 * right_path is the path we want to do the actual insert
3945 * in. left_path should only be passed in if we need to update that
3946 * portion of the tree after an edge insert.
3947 */
3948static int ocfs2_insert_path(struct inode *inode,
3949 handle_t *handle,
3950 struct ocfs2_path *left_path,
3951 struct ocfs2_path *right_path,
3952 struct ocfs2_extent_rec *insert_rec,
3953 struct ocfs2_insert_type *insert)
3954{
3955 int ret, subtree_index;
3956 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003957
Mark Fashehdcd05382007-01-16 11:32:23 -08003958 if (left_path) {
3959 int credits = handle->h_buffer_credits;
3960
3961 /*
3962 * There's a chance that left_path got passed back to
3963 * us without being accounted for in the
3964 * journal. Extend our transaction here to be sure we
3965 * can change those blocks.
3966 */
3967 credits += left_path->p_tree_depth;
3968
3969 ret = ocfs2_extend_trans(handle, credits);
3970 if (ret < 0) {
3971 mlog_errno(ret);
3972 goto out;
3973 }
3974
3975 ret = ocfs2_journal_access_path(inode, handle, left_path);
3976 if (ret < 0) {
3977 mlog_errno(ret);
3978 goto out;
3979 }
3980 }
3981
Mark Fashehe8aed342007-12-03 16:43:01 -08003982 /*
3983 * Pass both paths to the journal. The majority of inserts
3984 * will be touching all components anyway.
3985 */
3986 ret = ocfs2_journal_access_path(inode, handle, right_path);
3987 if (ret < 0) {
3988 mlog_errno(ret);
3989 goto out;
3990 }
3991
Mark Fasheh328d5752007-06-18 10:48:04 -07003992 if (insert->ins_split != SPLIT_NONE) {
3993 /*
3994 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02003995 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07003996 * function sort it all out.
3997 */
3998 ocfs2_split_record(inode, left_path, right_path,
3999 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004000
4001 /*
4002 * Split might have modified either leaf and we don't
4003 * have a guarantee that the later edge insert will
4004 * dirty this for us.
4005 */
4006 if (left_path)
4007 ret = ocfs2_journal_dirty(handle,
4008 path_leaf_bh(left_path));
4009 if (ret)
4010 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07004011 } else
4012 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
4013 insert, inode);
Mark Fashehdcd05382007-01-16 11:32:23 -08004014
Mark Fashehdcd05382007-01-16 11:32:23 -08004015 ret = ocfs2_journal_dirty(handle, leaf_bh);
4016 if (ret)
4017 mlog_errno(ret);
4018
4019 if (left_path) {
4020 /*
4021 * The rotate code has indicated that we need to fix
4022 * up portions of the tree after the insert.
4023 *
4024 * XXX: Should we extend the transaction here?
4025 */
4026 subtree_index = ocfs2_find_subtree_root(inode, left_path,
4027 right_path);
4028 ocfs2_complete_edge_insert(inode, handle, left_path,
4029 right_path, subtree_index);
4030 }
4031
4032 ret = 0;
4033out:
4034 return ret;
4035}
4036
4037static int ocfs2_do_insert_extent(struct inode *inode,
4038 handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004039 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004040 struct ocfs2_extent_rec *insert_rec,
4041 struct ocfs2_insert_type *type)
4042{
4043 int ret, rotate = 0;
4044 u32 cpos;
4045 struct ocfs2_path *right_path = NULL;
4046 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004047 struct ocfs2_extent_list *el;
4048
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004049 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004050
Joel Becker13723d02008-10-17 19:25:01 -07004051 ret = ocfs2_et_root_journal_access(handle, inode, et,
4052 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004053 if (ret) {
4054 mlog_errno(ret);
4055 goto out;
4056 }
4057
4058 if (le16_to_cpu(el->l_tree_depth) == 0) {
4059 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
4060 goto out_update_clusters;
4061 }
4062
Joel Beckerffdd7a52008-10-17 22:32:01 -07004063 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004064 if (!right_path) {
4065 ret = -ENOMEM;
4066 mlog_errno(ret);
4067 goto out;
4068 }
4069
4070 /*
4071 * Determine the path to start with. Rotations need the
4072 * rightmost path, everything else can go directly to the
4073 * target leaf.
4074 */
4075 cpos = le32_to_cpu(insert_rec->e_cpos);
4076 if (type->ins_appending == APPEND_NONE &&
4077 type->ins_contig == CONTIG_NONE) {
4078 rotate = 1;
4079 cpos = UINT_MAX;
4080 }
4081
4082 ret = ocfs2_find_path(inode, right_path, cpos);
4083 if (ret) {
4084 mlog_errno(ret);
4085 goto out;
4086 }
4087
4088 /*
4089 * Rotations and appends need special treatment - they modify
4090 * parts of the tree's above them.
4091 *
4092 * Both might pass back a path immediate to the left of the
4093 * one being inserted to. This will be cause
4094 * ocfs2_insert_path() to modify the rightmost records of
4095 * left_path to account for an edge insert.
4096 *
4097 * XXX: When modifying this code, keep in mind that an insert
4098 * can wind up skipping both of these two special cases...
4099 */
4100 if (rotate) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004101 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004102 le32_to_cpu(insert_rec->e_cpos),
4103 right_path, &left_path);
4104 if (ret) {
4105 mlog_errno(ret);
4106 goto out;
4107 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004108
4109 /*
4110 * ocfs2_rotate_tree_right() might have extended the
4111 * transaction without re-journaling our tree root.
4112 */
Joel Becker13723d02008-10-17 19:25:01 -07004113 ret = ocfs2_et_root_journal_access(handle, inode, et,
4114 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004115 if (ret) {
4116 mlog_errno(ret);
4117 goto out;
4118 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004119 } else if (type->ins_appending == APPEND_TAIL
4120 && type->ins_contig != CONTIG_LEFT) {
4121 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
4122 right_path, &left_path);
4123 if (ret) {
4124 mlog_errno(ret);
4125 goto out;
4126 }
4127 }
4128
4129 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
4130 insert_rec, type);
4131 if (ret) {
4132 mlog_errno(ret);
4133 goto out;
4134 }
4135
4136out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004137 if (type->ins_split == SPLIT_NONE)
Joel Becker35dc0aa2008-08-20 16:25:06 -07004138 ocfs2_et_update_clusters(inode, et,
4139 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004140
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004141 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004142 if (ret)
4143 mlog_errno(ret);
4144
4145out:
4146 ocfs2_free_path(left_path);
4147 ocfs2_free_path(right_path);
4148
4149 return ret;
4150}
4151
Mark Fasheh328d5752007-06-18 10:48:04 -07004152static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08004153ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004154 struct ocfs2_extent_list *el, int index,
4155 struct ocfs2_extent_rec *split_rec)
4156{
Tao Maad5a4d72008-01-30 14:21:32 +08004157 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004158 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004159 u32 left_cpos, right_cpos;
4160 struct ocfs2_extent_rec *rec = NULL;
4161 struct ocfs2_extent_list *new_el;
4162 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4163 struct buffer_head *bh;
4164 struct ocfs2_extent_block *eb;
4165
4166 if (index > 0) {
4167 rec = &el->l_recs[index - 1];
4168 } else if (path->p_tree_depth > 0) {
4169 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4170 path, &left_cpos);
4171 if (status)
4172 goto out;
4173
4174 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004175 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004176 if (!left_path)
4177 goto out;
4178
4179 status = ocfs2_find_path(inode, left_path, left_cpos);
4180 if (status)
4181 goto out;
4182
4183 new_el = path_leaf_el(left_path);
4184
4185 if (le16_to_cpu(new_el->l_next_free_rec) !=
4186 le16_to_cpu(new_el->l_count)) {
4187 bh = path_leaf_bh(left_path);
4188 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004189 ocfs2_error(inode->i_sb,
4190 "Extent block #%llu has an "
4191 "invalid l_next_free_rec of "
4192 "%d. It should have "
4193 "matched the l_count of %d",
4194 (unsigned long long)le64_to_cpu(eb->h_blkno),
4195 le16_to_cpu(new_el->l_next_free_rec),
4196 le16_to_cpu(new_el->l_count));
4197 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004198 goto out;
4199 }
4200 rec = &new_el->l_recs[
4201 le16_to_cpu(new_el->l_next_free_rec) - 1];
4202 }
4203 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004204
4205 /*
4206 * We're careful to check for an empty extent record here -
4207 * the merge code will know what to do if it sees one.
4208 */
Tao Maad5a4d72008-01-30 14:21:32 +08004209 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004210 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4211 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4212 ret = CONTIG_RIGHT;
4213 } else {
4214 ret = ocfs2_extent_contig(inode, rec, split_rec);
4215 }
4216 }
4217
Tao Maad5a4d72008-01-30 14:21:32 +08004218 rec = NULL;
4219 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4220 rec = &el->l_recs[index + 1];
4221 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4222 path->p_tree_depth > 0) {
4223 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4224 path, &right_cpos);
4225 if (status)
4226 goto out;
4227
4228 if (right_cpos == 0)
4229 goto out;
4230
Joel Beckerffdd7a52008-10-17 22:32:01 -07004231 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004232 if (!right_path)
4233 goto out;
4234
4235 status = ocfs2_find_path(inode, right_path, right_cpos);
4236 if (status)
4237 goto out;
4238
4239 new_el = path_leaf_el(right_path);
4240 rec = &new_el->l_recs[0];
4241 if (ocfs2_is_empty_extent(rec)) {
4242 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4243 bh = path_leaf_bh(right_path);
4244 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08004245 ocfs2_error(inode->i_sb,
4246 "Extent block #%llu has an "
4247 "invalid l_next_free_rec of %d",
4248 (unsigned long long)le64_to_cpu(eb->h_blkno),
4249 le16_to_cpu(new_el->l_next_free_rec));
4250 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004251 goto out;
4252 }
4253 rec = &new_el->l_recs[1];
4254 }
4255 }
4256
4257 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004258 enum ocfs2_contig_type contig_type;
4259
Mark Fasheh328d5752007-06-18 10:48:04 -07004260 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4261
4262 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4263 ret = CONTIG_LEFTRIGHT;
4264 else if (ret == CONTIG_NONE)
4265 ret = contig_type;
4266 }
4267
Tao Maad5a4d72008-01-30 14:21:32 +08004268out:
4269 if (left_path)
4270 ocfs2_free_path(left_path);
4271 if (right_path)
4272 ocfs2_free_path(right_path);
4273
Mark Fasheh328d5752007-06-18 10:48:04 -07004274 return ret;
4275}
4276
Mark Fashehdcd05382007-01-16 11:32:23 -08004277static void ocfs2_figure_contig_type(struct inode *inode,
4278 struct ocfs2_insert_type *insert,
4279 struct ocfs2_extent_list *el,
Tao Maca12b7c2008-08-18 17:38:52 +08004280 struct ocfs2_extent_rec *insert_rec,
4281 struct ocfs2_extent_tree *et)
Mark Fashehdcd05382007-01-16 11:32:23 -08004282{
4283 int i;
4284 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4285
Mark Fashehe48edee2007-03-07 16:46:57 -08004286 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4287
Mark Fashehdcd05382007-01-16 11:32:23 -08004288 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4289 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4290 insert_rec);
4291 if (contig_type != CONTIG_NONE) {
4292 insert->ins_contig_index = i;
4293 break;
4294 }
4295 }
4296 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004297
4298 if (insert->ins_contig != CONTIG_NONE) {
4299 struct ocfs2_extent_rec *rec =
4300 &el->l_recs[insert->ins_contig_index];
4301 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4302 le16_to_cpu(insert_rec->e_leaf_clusters);
4303
4304 /*
4305 * Caller might want us to limit the size of extents, don't
4306 * calculate contiguousness if we might exceed that limit.
4307 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004308 if (et->et_max_leaf_clusters &&
4309 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004310 insert->ins_contig = CONTIG_NONE;
4311 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004312}
4313
4314/*
4315 * This should only be called against the righmost leaf extent list.
4316 *
4317 * ocfs2_figure_appending_type() will figure out whether we'll have to
4318 * insert at the tail of the rightmost leaf.
4319 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004320 * This should also work against the root extent list for tree's with 0
4321 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004322 * then the logic here makes sense.
4323 */
4324static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4325 struct ocfs2_extent_list *el,
4326 struct ocfs2_extent_rec *insert_rec)
4327{
4328 int i;
4329 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4330 struct ocfs2_extent_rec *rec;
4331
4332 insert->ins_appending = APPEND_NONE;
4333
Mark Fashehe48edee2007-03-07 16:46:57 -08004334 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004335
4336 if (!el->l_next_free_rec)
4337 goto set_tail_append;
4338
4339 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4340 /* Were all records empty? */
4341 if (le16_to_cpu(el->l_next_free_rec) == 1)
4342 goto set_tail_append;
4343 }
4344
4345 i = le16_to_cpu(el->l_next_free_rec) - 1;
4346 rec = &el->l_recs[i];
4347
Mark Fashehe48edee2007-03-07 16:46:57 -08004348 if (cpos >=
4349 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004350 goto set_tail_append;
4351
4352 return;
4353
4354set_tail_append:
4355 insert->ins_appending = APPEND_TAIL;
4356}
4357
4358/*
4359 * Helper function called at the begining of an insert.
4360 *
4361 * This computes a few things that are commonly used in the process of
4362 * inserting into the btree:
4363 * - Whether the new extent is contiguous with an existing one.
4364 * - The current tree depth.
4365 * - Whether the insert is an appending one.
4366 * - The total # of free records in the tree.
4367 *
4368 * All of the information is stored on the ocfs2_insert_type
4369 * structure.
4370 */
4371static int ocfs2_figure_insert_type(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004372 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004373 struct buffer_head **last_eb_bh,
4374 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004375 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004376 struct ocfs2_insert_type *insert)
4377{
4378 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004379 struct ocfs2_extent_block *eb;
4380 struct ocfs2_extent_list *el;
4381 struct ocfs2_path *path = NULL;
4382 struct buffer_head *bh = NULL;
4383
Mark Fasheh328d5752007-06-18 10:48:04 -07004384 insert->ins_split = SPLIT_NONE;
4385
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004386 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004387 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4388
4389 if (el->l_tree_depth) {
4390 /*
4391 * If we have tree depth, we read in the
4392 * rightmost extent block ahead of time as
4393 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4394 * may want it later.
4395 */
Joel Becker5e965812008-11-13 14:49:16 -08004396 ret = ocfs2_read_extent_block(inode,
4397 ocfs2_et_get_last_eb_blk(et),
4398 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004399 if (ret) {
4400 mlog_exit(ret);
4401 goto out;
4402 }
4403 eb = (struct ocfs2_extent_block *) bh->b_data;
4404 el = &eb->h_list;
4405 }
4406
4407 /*
4408 * Unless we have a contiguous insert, we'll need to know if
4409 * there is room left in our allocation tree for another
4410 * extent record.
4411 *
4412 * XXX: This test is simplistic, we can search for empty
4413 * extent records too.
4414 */
Tao Maoc77534f2007-08-28 17:22:33 -07004415 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004416 le16_to_cpu(el->l_next_free_rec);
4417
4418 if (!insert->ins_tree_depth) {
Tao Maca12b7c2008-08-18 17:38:52 +08004419 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004420 ocfs2_figure_appending_type(insert, el, insert_rec);
4421 return 0;
4422 }
4423
Joel Beckerffdd7a52008-10-17 22:32:01 -07004424 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004425 if (!path) {
4426 ret = -ENOMEM;
4427 mlog_errno(ret);
4428 goto out;
4429 }
4430
4431 /*
4432 * In the case that we're inserting past what the tree
4433 * currently accounts for, ocfs2_find_path() will return for
4434 * us the rightmost tree path. This is accounted for below in
4435 * the appending code.
4436 */
4437 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4438 if (ret) {
4439 mlog_errno(ret);
4440 goto out;
4441 }
4442
4443 el = path_leaf_el(path);
4444
4445 /*
4446 * Now that we have the path, there's two things we want to determine:
4447 * 1) Contiguousness (also set contig_index if this is so)
4448 *
4449 * 2) Are we doing an append? We can trivially break this up
4450 * into two types of appends: simple record append, or a
4451 * rotate inside the tail leaf.
4452 */
Tao Maca12b7c2008-08-18 17:38:52 +08004453 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004454
4455 /*
4456 * The insert code isn't quite ready to deal with all cases of
4457 * left contiguousness. Specifically, if it's an insert into
4458 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004459 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004460 * left. For now, just catch that case and fool the layers
4461 * above us. This works just fine for tree_depth == 0, which
4462 * is why we allow that above.
4463 */
4464 if (insert->ins_contig == CONTIG_LEFT &&
4465 insert->ins_contig_index == 0)
4466 insert->ins_contig = CONTIG_NONE;
4467
4468 /*
4469 * Ok, so we can simply compare against last_eb to figure out
4470 * whether the path doesn't exist. This will only happen in
4471 * the case that we're doing a tail append, so maybe we can
4472 * take advantage of that information somehow.
4473 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004474 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004475 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004476 /*
4477 * Ok, ocfs2_find_path() returned us the rightmost
4478 * tree path. This might be an appending insert. There are
4479 * two cases:
4480 * 1) We're doing a true append at the tail:
4481 * -This might even be off the end of the leaf
4482 * 2) We're "appending" by rotating in the tail
4483 */
4484 ocfs2_figure_appending_type(insert, el, insert_rec);
4485 }
4486
4487out:
4488 ocfs2_free_path(path);
4489
4490 if (ret == 0)
4491 *last_eb_bh = bh;
4492 else
4493 brelse(bh);
4494 return ret;
4495}
4496
4497/*
4498 * Insert an extent into an inode btree.
4499 *
4500 * The caller needs to update fe->i_clusters
4501 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004502int ocfs2_insert_extent(struct ocfs2_super *osb,
4503 handle_t *handle,
4504 struct inode *inode,
4505 struct ocfs2_extent_tree *et,
4506 u32 cpos,
4507 u64 start_blk,
4508 u32 new_clusters,
4509 u8 flags,
4510 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004511{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004512 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004513 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004514 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004515 struct ocfs2_insert_type insert = {0, };
4516 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004517
Mark Fashehdcd05382007-01-16 11:32:23 -08004518 mlog(0, "add %u clusters at position %u to inode %llu\n",
4519 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004520
Mark Fashehe48edee2007-03-07 16:46:57 -08004521 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004522 rec.e_cpos = cpu_to_le32(cpos);
4523 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004524 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004525 rec.e_flags = flags;
Joel Becker1e61ee72008-08-20 18:32:45 -07004526 status = ocfs2_et_insert_check(inode, et, &rec);
4527 if (status) {
4528 mlog_errno(status);
4529 goto bail;
4530 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004531
Tao Mae7d4cb62008-08-18 17:38:44 +08004532 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004533 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004534 if (status < 0) {
4535 mlog_errno(status);
4536 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004537 }
4538
Mark Fashehdcd05382007-01-16 11:32:23 -08004539 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4540 "Insert.contig_index: %d, Insert.free_records: %d, "
4541 "Insert.tree_depth: %d\n",
4542 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004543 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004544
Tao Maoc77534f2007-08-28 17:22:33 -07004545 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004546 status = ocfs2_grow_tree(inode, handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004547 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004548 meta_ac);
4549 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004550 mlog_errno(status);
4551 goto bail;
4552 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004553 }
4554
Mark Fashehdcd05382007-01-16 11:32:23 -08004555 /* Finally, we can add clusters. This might rotate the tree for us. */
Tao Mae7d4cb62008-08-18 17:38:44 +08004556 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004557 if (status < 0)
4558 mlog_errno(status);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004559 else if (et->et_ops == &ocfs2_dinode_et_ops)
Mark Fasheh83418972007-04-23 18:53:12 -07004560 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004561
4562bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004563 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004564
Tao Maf56654c2008-08-18 17:38:48 +08004565 mlog_exit(status);
4566 return status;
4567}
4568
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004569/*
4570 * Allcate and add clusters into the extent b-tree.
4571 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004572 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004573 * it is not limited to the file storage. Any extent tree can use this
4574 * function if it implements the proper ocfs2_extent_tree.
4575 */
4576int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4577 struct inode *inode,
4578 u32 *logical_offset,
4579 u32 clusters_to_add,
4580 int mark_unwritten,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004581 struct ocfs2_extent_tree *et,
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004582 handle_t *handle,
4583 struct ocfs2_alloc_context *data_ac,
4584 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004585 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004586{
4587 int status = 0;
4588 int free_extents;
4589 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4590 u32 bit_off, num_bits;
4591 u64 block;
4592 u8 flags = 0;
4593
4594 BUG_ON(!clusters_to_add);
4595
4596 if (mark_unwritten)
4597 flags = OCFS2_EXT_UNWRITTEN;
4598
Joel Beckerf99b9b72008-08-20 19:36:33 -07004599 free_extents = ocfs2_num_free_extents(osb, inode, et);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004600 if (free_extents < 0) {
4601 status = free_extents;
4602 mlog_errno(status);
4603 goto leave;
4604 }
4605
4606 /* there are two cases which could cause us to EAGAIN in the
4607 * we-need-more-metadata case:
4608 * 1) we haven't reserved *any*
4609 * 2) we are so fragmented, we've needed to add metadata too
4610 * many times. */
4611 if (!free_extents && !meta_ac) {
4612 mlog(0, "we haven't reserved any metadata!\n");
4613 status = -EAGAIN;
4614 reason = RESTART_META;
4615 goto leave;
4616 } else if ((!free_extents)
4617 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004618 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004619 mlog(0, "filesystem is really fragmented...\n");
4620 status = -EAGAIN;
4621 reason = RESTART_META;
4622 goto leave;
4623 }
4624
4625 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4626 clusters_to_add, &bit_off, &num_bits);
4627 if (status < 0) {
4628 if (status != -ENOSPC)
4629 mlog_errno(status);
4630 goto leave;
4631 }
4632
4633 BUG_ON(num_bits > clusters_to_add);
4634
Joel Becker13723d02008-10-17 19:25:01 -07004635 /* reserve our write early -- insert_extent may update the tree root */
4636 status = ocfs2_et_root_journal_access(handle, inode, et,
4637 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004638 if (status < 0) {
4639 mlog_errno(status);
4640 goto leave;
4641 }
4642
4643 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4644 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4645 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Joel Beckerf99b9b72008-08-20 19:36:33 -07004646 status = ocfs2_insert_extent(osb, handle, inode, et,
4647 *logical_offset, block,
4648 num_bits, flags, meta_ac);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004649 if (status < 0) {
4650 mlog_errno(status);
4651 goto leave;
4652 }
4653
Joel Beckerf99b9b72008-08-20 19:36:33 -07004654 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004655 if (status < 0) {
4656 mlog_errno(status);
4657 goto leave;
4658 }
4659
4660 clusters_to_add -= num_bits;
4661 *logical_offset += num_bits;
4662
4663 if (clusters_to_add) {
4664 mlog(0, "need to alloc once more, wanted = %u\n",
4665 clusters_to_add);
4666 status = -EAGAIN;
4667 reason = RESTART_TRANS;
4668 }
4669
4670leave:
4671 mlog_exit(status);
4672 if (reason_ret)
4673 *reason_ret = reason;
4674 return status;
4675}
4676
Mark Fasheh328d5752007-06-18 10:48:04 -07004677static void ocfs2_make_right_split_rec(struct super_block *sb,
4678 struct ocfs2_extent_rec *split_rec,
4679 u32 cpos,
4680 struct ocfs2_extent_rec *rec)
4681{
4682 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4683 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4684
4685 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4686
4687 split_rec->e_cpos = cpu_to_le32(cpos);
4688 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4689
4690 split_rec->e_blkno = rec->e_blkno;
4691 le64_add_cpu(&split_rec->e_blkno,
4692 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4693
4694 split_rec->e_flags = rec->e_flags;
4695}
4696
4697static int ocfs2_split_and_insert(struct inode *inode,
4698 handle_t *handle,
4699 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004700 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004701 struct buffer_head **last_eb_bh,
4702 int split_index,
4703 struct ocfs2_extent_rec *orig_split_rec,
4704 struct ocfs2_alloc_context *meta_ac)
4705{
4706 int ret = 0, depth;
4707 unsigned int insert_range, rec_range, do_leftright = 0;
4708 struct ocfs2_extent_rec tmprec;
4709 struct ocfs2_extent_list *rightmost_el;
4710 struct ocfs2_extent_rec rec;
4711 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4712 struct ocfs2_insert_type insert;
4713 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004714
4715leftright:
4716 /*
4717 * Store a copy of the record on the stack - it might move
4718 * around as the tree is manipulated below.
4719 */
4720 rec = path_leaf_el(path)->l_recs[split_index];
4721
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004722 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004723
4724 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4725 if (depth) {
4726 BUG_ON(!(*last_eb_bh));
4727 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4728 rightmost_el = &eb->h_list;
4729 }
4730
4731 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4732 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004733 ret = ocfs2_grow_tree(inode, handle, et,
4734 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004735 if (ret) {
4736 mlog_errno(ret);
4737 goto out;
4738 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004739 }
4740
4741 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4742 insert.ins_appending = APPEND_NONE;
4743 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004744 insert.ins_tree_depth = depth;
4745
4746 insert_range = le32_to_cpu(split_rec.e_cpos) +
4747 le16_to_cpu(split_rec.e_leaf_clusters);
4748 rec_range = le32_to_cpu(rec.e_cpos) +
4749 le16_to_cpu(rec.e_leaf_clusters);
4750
4751 if (split_rec.e_cpos == rec.e_cpos) {
4752 insert.ins_split = SPLIT_LEFT;
4753 } else if (insert_range == rec_range) {
4754 insert.ins_split = SPLIT_RIGHT;
4755 } else {
4756 /*
4757 * Left/right split. We fake this as a right split
4758 * first and then make a second pass as a left split.
4759 */
4760 insert.ins_split = SPLIT_RIGHT;
4761
4762 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4763 &rec);
4764
4765 split_rec = tmprec;
4766
4767 BUG_ON(do_leftright);
4768 do_leftright = 1;
4769 }
4770
Tao Mae7d4cb62008-08-18 17:38:44 +08004771 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004772 if (ret) {
4773 mlog_errno(ret);
4774 goto out;
4775 }
4776
4777 if (do_leftright == 1) {
4778 u32 cpos;
4779 struct ocfs2_extent_list *el;
4780
4781 do_leftright++;
4782 split_rec = *orig_split_rec;
4783
4784 ocfs2_reinit_path(path, 1);
4785
4786 cpos = le32_to_cpu(split_rec.e_cpos);
4787 ret = ocfs2_find_path(inode, path, cpos);
4788 if (ret) {
4789 mlog_errno(ret);
4790 goto out;
4791 }
4792
4793 el = path_leaf_el(path);
4794 split_index = ocfs2_search_extent_list(el, cpos);
4795 goto leftright;
4796 }
4797out:
4798
4799 return ret;
4800}
4801
4802/*
4803 * Mark part or all of the extent record at split_index in the leaf
4804 * pointed to by path as written. This removes the unwritten
4805 * extent flag.
4806 *
4807 * Care is taken to handle contiguousness so as to not grow the tree.
4808 *
4809 * meta_ac is not strictly necessary - we only truly need it if growth
4810 * of the tree is required. All other cases will degrade into a less
4811 * optimal tree layout.
4812 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004813 * last_eb_bh should be the rightmost leaf block for any extent
4814 * btree. Since a split may grow the tree or a merge might shrink it,
4815 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07004816 *
4817 * This code is optimized for readability - several passes might be
4818 * made over certain portions of the tree. All of those blocks will
4819 * have been brought into cache (and pinned via the journal), so the
4820 * extra overhead is not expressed in terms of disk reads.
4821 */
4822static int __ocfs2_mark_extent_written(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004823 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004824 handle_t *handle,
4825 struct ocfs2_path *path,
4826 int split_index,
4827 struct ocfs2_extent_rec *split_rec,
4828 struct ocfs2_alloc_context *meta_ac,
4829 struct ocfs2_cached_dealloc_ctxt *dealloc)
4830{
4831 int ret = 0;
4832 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004833 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07004834 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4835 struct ocfs2_merge_ctxt ctxt;
4836 struct ocfs2_extent_list *rightmost_el;
4837
Roel Kluin3cf0c502007-10-27 00:20:36 +02004838 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004839 ret = -EIO;
4840 mlog_errno(ret);
4841 goto out;
4842 }
4843
4844 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4845 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4846 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4847 ret = -EIO;
4848 mlog_errno(ret);
4849 goto out;
4850 }
4851
Tao Maad5a4d72008-01-30 14:21:32 +08004852 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07004853 split_index,
4854 split_rec);
4855
4856 /*
4857 * The core merge / split code wants to know how much room is
4858 * left in this inodes allocation tree, so we pass the
4859 * rightmost extent list.
4860 */
4861 if (path->p_tree_depth) {
4862 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004863
Joel Becker5e965812008-11-13 14:49:16 -08004864 ret = ocfs2_read_extent_block(inode,
4865 ocfs2_et_get_last_eb_blk(et),
4866 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07004867 if (ret) {
4868 mlog_exit(ret);
4869 goto out;
4870 }
4871
4872 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07004873 rightmost_el = &eb->h_list;
4874 } else
4875 rightmost_el = path_root_el(path);
4876
Mark Fasheh328d5752007-06-18 10:48:04 -07004877 if (rec->e_cpos == split_rec->e_cpos &&
4878 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4879 ctxt.c_split_covers_rec = 1;
4880 else
4881 ctxt.c_split_covers_rec = 0;
4882
4883 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4884
Mark Fasheh015452b2007-09-12 10:21:22 -07004885 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4886 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4887 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004888
4889 if (ctxt.c_contig_type == CONTIG_NONE) {
4890 if (ctxt.c_split_covers_rec)
4891 el->l_recs[split_index] = *split_rec;
4892 else
Tao Mae7d4cb62008-08-18 17:38:44 +08004893 ret = ocfs2_split_and_insert(inode, handle, path, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004894 &last_eb_bh, split_index,
4895 split_rec, meta_ac);
4896 if (ret)
4897 mlog_errno(ret);
4898 } else {
4899 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4900 split_index, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08004901 dealloc, &ctxt, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07004902 if (ret)
4903 mlog_errno(ret);
4904 }
4905
Mark Fasheh328d5752007-06-18 10:48:04 -07004906out:
4907 brelse(last_eb_bh);
4908 return ret;
4909}
4910
4911/*
4912 * Mark the already-existing extent at cpos as written for len clusters.
4913 *
4914 * If the existing extent is larger than the request, initiate a
4915 * split. An attempt will be made at merging with adjacent extents.
4916 *
4917 * The caller is responsible for passing down meta_ac if we'll need it.
4918 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004919int ocfs2_mark_extent_written(struct inode *inode,
4920 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004921 handle_t *handle, u32 cpos, u32 len, u32 phys,
4922 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004923 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07004924{
4925 int ret, index;
4926 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4927 struct ocfs2_extent_rec split_rec;
4928 struct ocfs2_path *left_path = NULL;
4929 struct ocfs2_extent_list *el;
4930
4931 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4932 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4933
4934 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4935 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4936 "that are being written to, but the feature bit "
4937 "is not set in the super block.",
4938 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4939 ret = -EROFS;
4940 goto out;
4941 }
4942
4943 /*
4944 * XXX: This should be fixed up so that we just re-insert the
4945 * next extent records.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004946 *
4947 * XXX: This is a hack on the extent tree, maybe it should be
4948 * an op?
Mark Fasheh328d5752007-06-18 10:48:04 -07004949 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07004950 if (et->et_ops == &ocfs2_dinode_et_ops)
Tao Mae7d4cb62008-08-18 17:38:44 +08004951 ocfs2_extent_map_trunc(inode, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07004952
Joel Beckerffdd7a52008-10-17 22:32:01 -07004953 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07004954 if (!left_path) {
4955 ret = -ENOMEM;
4956 mlog_errno(ret);
4957 goto out;
4958 }
4959
4960 ret = ocfs2_find_path(inode, left_path, cpos);
4961 if (ret) {
4962 mlog_errno(ret);
4963 goto out;
4964 }
4965 el = path_leaf_el(left_path);
4966
4967 index = ocfs2_search_extent_list(el, cpos);
4968 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4969 ocfs2_error(inode->i_sb,
4970 "Inode %llu has an extent at cpos %u which can no "
4971 "longer be found.\n",
4972 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4973 ret = -EROFS;
4974 goto out;
4975 }
4976
4977 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4978 split_rec.e_cpos = cpu_to_le32(cpos);
4979 split_rec.e_leaf_clusters = cpu_to_le16(len);
4980 split_rec.e_blkno = cpu_to_le64(start_blkno);
4981 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4982 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4983
Joel Beckerf99b9b72008-08-20 19:36:33 -07004984 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004985 index, &split_rec, meta_ac,
4986 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07004987 if (ret)
4988 mlog_errno(ret);
4989
4990out:
4991 ocfs2_free_path(left_path);
4992 return ret;
4993}
4994
Tao Mae7d4cb62008-08-18 17:38:44 +08004995static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07004996 handle_t *handle, struct ocfs2_path *path,
4997 int index, u32 new_range,
4998 struct ocfs2_alloc_context *meta_ac)
4999{
5000 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005001 struct buffer_head *last_eb_bh = NULL;
5002 struct ocfs2_extent_block *eb;
5003 struct ocfs2_extent_list *rightmost_el, *el;
5004 struct ocfs2_extent_rec split_rec;
5005 struct ocfs2_extent_rec *rec;
5006 struct ocfs2_insert_type insert;
5007
5008 /*
5009 * Setup the record to split before we grow the tree.
5010 */
5011 el = path_leaf_el(path);
5012 rec = &el->l_recs[index];
5013 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
5014
5015 depth = path->p_tree_depth;
5016 if (depth > 0) {
Joel Becker5e965812008-11-13 14:49:16 -08005017 ret = ocfs2_read_extent_block(inode,
5018 ocfs2_et_get_last_eb_blk(et),
5019 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005020 if (ret < 0) {
5021 mlog_errno(ret);
5022 goto out;
5023 }
5024
5025 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5026 rightmost_el = &eb->h_list;
5027 } else
5028 rightmost_el = path_leaf_el(path);
5029
Tao Ma811f9332008-08-18 17:38:43 +08005030 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005031 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005032 ret = ocfs2_extend_trans(handle, credits);
5033 if (ret) {
5034 mlog_errno(ret);
5035 goto out;
5036 }
5037
5038 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5039 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005040 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005041 meta_ac);
5042 if (ret) {
5043 mlog_errno(ret);
5044 goto out;
5045 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005046 }
5047
5048 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5049 insert.ins_appending = APPEND_NONE;
5050 insert.ins_contig = CONTIG_NONE;
5051 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005052 insert.ins_tree_depth = depth;
5053
Tao Mae7d4cb62008-08-18 17:38:44 +08005054 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005055 if (ret)
5056 mlog_errno(ret);
5057
5058out:
5059 brelse(last_eb_bh);
5060 return ret;
5061}
5062
5063static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5064 struct ocfs2_path *path, int index,
5065 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08005066 u32 cpos, u32 len,
5067 struct ocfs2_extent_tree *et)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005068{
5069 int ret;
5070 u32 left_cpos, rec_range, trunc_range;
5071 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5072 struct super_block *sb = inode->i_sb;
5073 struct ocfs2_path *left_path = NULL;
5074 struct ocfs2_extent_list *el = path_leaf_el(path);
5075 struct ocfs2_extent_rec *rec;
5076 struct ocfs2_extent_block *eb;
5077
5078 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005079 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005080 if (ret) {
5081 mlog_errno(ret);
5082 goto out;
5083 }
5084
5085 index--;
5086 }
5087
5088 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5089 path->p_tree_depth) {
5090 /*
5091 * Check whether this is the rightmost tree record. If
5092 * we remove all of this record or part of its right
5093 * edge then an update of the record lengths above it
5094 * will be required.
5095 */
5096 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5097 if (eb->h_next_leaf_blk == 0)
5098 is_rightmost_tree_rec = 1;
5099 }
5100
5101 rec = &el->l_recs[index];
5102 if (index == 0 && path->p_tree_depth &&
5103 le32_to_cpu(rec->e_cpos) == cpos) {
5104 /*
5105 * Changing the leftmost offset (via partial or whole
5106 * record truncate) of an interior (or rightmost) path
5107 * means we have to update the subtree that is formed
5108 * by this leaf and the one to it's left.
5109 *
5110 * There are two cases we can skip:
5111 * 1) Path is the leftmost one in our inode tree.
5112 * 2) The leaf is rightmost and will be empty after
5113 * we remove the extent record - the rotate code
5114 * knows how to update the newly formed edge.
5115 */
5116
5117 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5118 &left_cpos);
5119 if (ret) {
5120 mlog_errno(ret);
5121 goto out;
5122 }
5123
5124 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005125 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005126 if (!left_path) {
5127 ret = -ENOMEM;
5128 mlog_errno(ret);
5129 goto out;
5130 }
5131
5132 ret = ocfs2_find_path(inode, left_path, left_cpos);
5133 if (ret) {
5134 mlog_errno(ret);
5135 goto out;
5136 }
5137 }
5138 }
5139
5140 ret = ocfs2_extend_rotate_transaction(handle, 0,
5141 handle->h_buffer_credits,
5142 path);
5143 if (ret) {
5144 mlog_errno(ret);
5145 goto out;
5146 }
5147
5148 ret = ocfs2_journal_access_path(inode, handle, path);
5149 if (ret) {
5150 mlog_errno(ret);
5151 goto out;
5152 }
5153
5154 ret = ocfs2_journal_access_path(inode, handle, left_path);
5155 if (ret) {
5156 mlog_errno(ret);
5157 goto out;
5158 }
5159
5160 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5161 trunc_range = cpos + len;
5162
5163 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5164 int next_free;
5165
5166 memset(rec, 0, sizeof(*rec));
5167 ocfs2_cleanup_merge(el, index);
5168 wants_rotate = 1;
5169
5170 next_free = le16_to_cpu(el->l_next_free_rec);
5171 if (is_rightmost_tree_rec && next_free > 1) {
5172 /*
5173 * We skip the edge update if this path will
5174 * be deleted by the rotate code.
5175 */
5176 rec = &el->l_recs[next_free - 1];
5177 ocfs2_adjust_rightmost_records(inode, handle, path,
5178 rec);
5179 }
5180 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5181 /* Remove leftmost portion of the record. */
5182 le32_add_cpu(&rec->e_cpos, len);
5183 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5184 le16_add_cpu(&rec->e_leaf_clusters, -len);
5185 } else if (rec_range == trunc_range) {
5186 /* Remove rightmost portion of the record */
5187 le16_add_cpu(&rec->e_leaf_clusters, -len);
5188 if (is_rightmost_tree_rec)
5189 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5190 } else {
5191 /* Caller should have trapped this. */
5192 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5193 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5194 le32_to_cpu(rec->e_cpos),
5195 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5196 BUG();
5197 }
5198
5199 if (left_path) {
5200 int subtree_index;
5201
5202 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5203 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5204 subtree_index);
5205 }
5206
5207 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5208
Tao Mae7d4cb62008-08-18 17:38:44 +08005209 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005210 if (ret) {
5211 mlog_errno(ret);
5212 goto out;
5213 }
5214
5215out:
5216 ocfs2_free_path(left_path);
5217 return ret;
5218}
5219
Joel Beckerf99b9b72008-08-20 19:36:33 -07005220int ocfs2_remove_extent(struct inode *inode,
5221 struct ocfs2_extent_tree *et,
Mark Fasheh063c4562007-07-03 13:34:11 -07005222 u32 cpos, u32 len, handle_t *handle,
5223 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005224 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005225{
5226 int ret, index;
5227 u32 rec_range, trunc_range;
5228 struct ocfs2_extent_rec *rec;
5229 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005230 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005231
5232 ocfs2_extent_map_trunc(inode, 0);
5233
Joel Beckerffdd7a52008-10-17 22:32:01 -07005234 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005235 if (!path) {
5236 ret = -ENOMEM;
5237 mlog_errno(ret);
5238 goto out;
5239 }
5240
5241 ret = ocfs2_find_path(inode, path, cpos);
5242 if (ret) {
5243 mlog_errno(ret);
5244 goto out;
5245 }
5246
5247 el = path_leaf_el(path);
5248 index = ocfs2_search_extent_list(el, cpos);
5249 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5250 ocfs2_error(inode->i_sb,
5251 "Inode %llu has an extent at cpos %u which can no "
5252 "longer be found.\n",
5253 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5254 ret = -EROFS;
5255 goto out;
5256 }
5257
5258 /*
5259 * We have 3 cases of extent removal:
5260 * 1) Range covers the entire extent rec
5261 * 2) Range begins or ends on one edge of the extent rec
5262 * 3) Range is in the middle of the extent rec (no shared edges)
5263 *
5264 * For case 1 we remove the extent rec and left rotate to
5265 * fill the hole.
5266 *
5267 * For case 2 we just shrink the existing extent rec, with a
5268 * tree update if the shrinking edge is also the edge of an
5269 * extent block.
5270 *
5271 * For case 3 we do a right split to turn the extent rec into
5272 * something case 2 can handle.
5273 */
5274 rec = &el->l_recs[index];
5275 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5276 trunc_range = cpos + len;
5277
5278 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5279
5280 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5281 "(cpos %u, len %u)\n",
5282 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5283 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5284
5285 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5286 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005287 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005288 if (ret) {
5289 mlog_errno(ret);
5290 goto out;
5291 }
5292 } else {
Joel Beckerf99b9b72008-08-20 19:36:33 -07005293 ret = ocfs2_split_tree(inode, et, handle, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005294 trunc_range, meta_ac);
5295 if (ret) {
5296 mlog_errno(ret);
5297 goto out;
5298 }
5299
5300 /*
5301 * The split could have manipulated the tree enough to
5302 * move the record location, so we have to look for it again.
5303 */
5304 ocfs2_reinit_path(path, 1);
5305
5306 ret = ocfs2_find_path(inode, path, cpos);
5307 if (ret) {
5308 mlog_errno(ret);
5309 goto out;
5310 }
5311
5312 el = path_leaf_el(path);
5313 index = ocfs2_search_extent_list(el, cpos);
5314 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5315 ocfs2_error(inode->i_sb,
5316 "Inode %llu: split at cpos %u lost record.",
5317 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5318 cpos);
5319 ret = -EROFS;
5320 goto out;
5321 }
5322
5323 /*
5324 * Double check our values here. If anything is fishy,
5325 * it's easier to catch it at the top level.
5326 */
5327 rec = &el->l_recs[index];
5328 rec_range = le32_to_cpu(rec->e_cpos) +
5329 ocfs2_rec_clusters(el, rec);
5330 if (rec_range != trunc_range) {
5331 ocfs2_error(inode->i_sb,
5332 "Inode %llu: error after split at cpos %u"
5333 "trunc len %u, existing record is (%u,%u)",
5334 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5335 cpos, len, le32_to_cpu(rec->e_cpos),
5336 ocfs2_rec_clusters(el, rec));
5337 ret = -EROFS;
5338 goto out;
5339 }
5340
5341 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005342 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005343 if (ret) {
5344 mlog_errno(ret);
5345 goto out;
5346 }
5347 }
5348
5349out:
5350 ocfs2_free_path(path);
5351 return ret;
5352}
5353
Mark Fashehfecc0112008-11-12 15:16:38 -08005354int ocfs2_remove_btree_range(struct inode *inode,
5355 struct ocfs2_extent_tree *et,
5356 u32 cpos, u32 phys_cpos, u32 len,
5357 struct ocfs2_cached_dealloc_ctxt *dealloc)
5358{
5359 int ret;
5360 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5361 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5362 struct inode *tl_inode = osb->osb_tl_inode;
5363 handle_t *handle;
5364 struct ocfs2_alloc_context *meta_ac = NULL;
5365
5366 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5367 if (ret) {
5368 mlog_errno(ret);
5369 return ret;
5370 }
5371
5372 mutex_lock(&tl_inode->i_mutex);
5373
5374 if (ocfs2_truncate_log_needs_flush(osb)) {
5375 ret = __ocfs2_flush_truncate_log(osb);
5376 if (ret < 0) {
5377 mlog_errno(ret);
5378 goto out;
5379 }
5380 }
5381
Jan Karaa90714c2008-10-09 19:38:40 +02005382 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005383 if (IS_ERR(handle)) {
5384 ret = PTR_ERR(handle);
5385 mlog_errno(ret);
5386 goto out;
5387 }
5388
Joel Becker13723d02008-10-17 19:25:01 -07005389 ret = ocfs2_et_root_journal_access(handle, inode, et,
5390 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005391 if (ret) {
5392 mlog_errno(ret);
5393 goto out;
5394 }
5395
5396 ret = ocfs2_remove_extent(inode, et, cpos, len, handle, meta_ac,
5397 dealloc);
5398 if (ret) {
5399 mlog_errno(ret);
5400 goto out_commit;
5401 }
5402
5403 ocfs2_et_update_clusters(inode, et, -len);
5404
5405 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5406 if (ret) {
5407 mlog_errno(ret);
5408 goto out_commit;
5409 }
5410
5411 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5412 if (ret)
5413 mlog_errno(ret);
5414
5415out_commit:
5416 ocfs2_commit_trans(osb, handle);
5417out:
5418 mutex_unlock(&tl_inode->i_mutex);
5419
5420 if (meta_ac)
5421 ocfs2_free_alloc_context(meta_ac);
5422
5423 return ret;
5424}
5425
Mark Fasheh063c4562007-07-03 13:34:11 -07005426int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005427{
5428 struct buffer_head *tl_bh = osb->osb_tl_bh;
5429 struct ocfs2_dinode *di;
5430 struct ocfs2_truncate_log *tl;
5431
5432 di = (struct ocfs2_dinode *) tl_bh->b_data;
5433 tl = &di->id2.i_dealloc;
5434
5435 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5436 "slot %d, invalid truncate log parameters: used = "
5437 "%u, count = %u\n", osb->slot_num,
5438 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5439 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5440}
5441
5442static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5443 unsigned int new_start)
5444{
5445 unsigned int tail_index;
5446 unsigned int current_tail;
5447
5448 /* No records, nothing to coalesce */
5449 if (!le16_to_cpu(tl->tl_used))
5450 return 0;
5451
5452 tail_index = le16_to_cpu(tl->tl_used) - 1;
5453 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5454 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5455
5456 return current_tail == new_start;
5457}
5458
Mark Fasheh063c4562007-07-03 13:34:11 -07005459int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5460 handle_t *handle,
5461 u64 start_blk,
5462 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005463{
5464 int status, index;
5465 unsigned int start_cluster, tl_count;
5466 struct inode *tl_inode = osb->osb_tl_inode;
5467 struct buffer_head *tl_bh = osb->osb_tl_bh;
5468 struct ocfs2_dinode *di;
5469 struct ocfs2_truncate_log *tl;
5470
Mark Fashehb06970532006-03-03 10:24:33 -08005471 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5472 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005473
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005474 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005475
5476 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5477
5478 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005479
Joel Becker10995aa2008-11-13 14:49:12 -08005480 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5481 * by the underlying call to ocfs2_read_inode_block(), so any
5482 * corruption is a code bug */
5483 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5484
5485 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005486 tl_count = le16_to_cpu(tl->tl_count);
5487 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5488 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005489 "Truncate record count on #%llu invalid "
5490 "wanted %u, actual %u\n",
5491 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005492 ocfs2_truncate_recs_per_inode(osb->sb),
5493 le16_to_cpu(tl->tl_count));
5494
5495 /* Caller should have known to flush before calling us. */
5496 index = le16_to_cpu(tl->tl_used);
5497 if (index >= tl_count) {
5498 status = -ENOSPC;
5499 mlog_errno(status);
5500 goto bail;
5501 }
5502
Joel Becker13723d02008-10-17 19:25:01 -07005503 status = ocfs2_journal_access_di(handle, tl_inode, tl_bh,
5504 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005505 if (status < 0) {
5506 mlog_errno(status);
5507 goto bail;
5508 }
5509
5510 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005511 "%llu (index = %d)\n", num_clusters, start_cluster,
5512 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005513
5514 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5515 /*
5516 * Move index back to the record we are coalescing with.
5517 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5518 */
5519 index--;
5520
5521 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5522 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5523 index, le32_to_cpu(tl->tl_recs[index].t_start),
5524 num_clusters);
5525 } else {
5526 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5527 tl->tl_used = cpu_to_le16(index + 1);
5528 }
5529 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5530
5531 status = ocfs2_journal_dirty(handle, tl_bh);
5532 if (status < 0) {
5533 mlog_errno(status);
5534 goto bail;
5535 }
5536
5537bail:
5538 mlog_exit(status);
5539 return status;
5540}
5541
5542static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005543 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005544 struct inode *data_alloc_inode,
5545 struct buffer_head *data_alloc_bh)
5546{
5547 int status = 0;
5548 int i;
5549 unsigned int num_clusters;
5550 u64 start_blk;
5551 struct ocfs2_truncate_rec rec;
5552 struct ocfs2_dinode *di;
5553 struct ocfs2_truncate_log *tl;
5554 struct inode *tl_inode = osb->osb_tl_inode;
5555 struct buffer_head *tl_bh = osb->osb_tl_bh;
5556
5557 mlog_entry_void();
5558
5559 di = (struct ocfs2_dinode *) tl_bh->b_data;
5560 tl = &di->id2.i_dealloc;
5561 i = le16_to_cpu(tl->tl_used) - 1;
5562 while (i >= 0) {
5563 /* Caller has given us at least enough credits to
5564 * update the truncate log dinode */
Joel Becker13723d02008-10-17 19:25:01 -07005565 status = ocfs2_journal_access_di(handle, tl_inode, tl_bh,
5566 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005567 if (status < 0) {
5568 mlog_errno(status);
5569 goto bail;
5570 }
5571
5572 tl->tl_used = cpu_to_le16(i);
5573
5574 status = ocfs2_journal_dirty(handle, tl_bh);
5575 if (status < 0) {
5576 mlog_errno(status);
5577 goto bail;
5578 }
5579
5580 /* TODO: Perhaps we can calculate the bulk of the
5581 * credits up front rather than extending like
5582 * this. */
5583 status = ocfs2_extend_trans(handle,
5584 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5585 if (status < 0) {
5586 mlog_errno(status);
5587 goto bail;
5588 }
5589
5590 rec = tl->tl_recs[i];
5591 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5592 le32_to_cpu(rec.t_start));
5593 num_clusters = le32_to_cpu(rec.t_clusters);
5594
5595 /* if start_blk is not set, we ignore the record as
5596 * invalid. */
5597 if (start_blk) {
5598 mlog(0, "free record %d, start = %u, clusters = %u\n",
5599 i, le32_to_cpu(rec.t_start), num_clusters);
5600
5601 status = ocfs2_free_clusters(handle, data_alloc_inode,
5602 data_alloc_bh, start_blk,
5603 num_clusters);
5604 if (status < 0) {
5605 mlog_errno(status);
5606 goto bail;
5607 }
5608 }
5609 i--;
5610 }
5611
5612bail:
5613 mlog_exit(status);
5614 return status;
5615}
5616
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005617/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005618int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005619{
5620 int status;
5621 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005622 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005623 struct inode *tl_inode = osb->osb_tl_inode;
5624 struct inode *data_alloc_inode = NULL;
5625 struct buffer_head *tl_bh = osb->osb_tl_bh;
5626 struct buffer_head *data_alloc_bh = NULL;
5627 struct ocfs2_dinode *di;
5628 struct ocfs2_truncate_log *tl;
5629
5630 mlog_entry_void();
5631
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005632 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005633
5634 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005635
Joel Becker10995aa2008-11-13 14:49:12 -08005636 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5637 * by the underlying call to ocfs2_read_inode_block(), so any
5638 * corruption is a code bug */
5639 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5640
5641 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005642 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005643 mlog(0, "Flush %u records from truncate log #%llu\n",
5644 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005645 if (!num_to_flush) {
5646 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005647 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005648 }
5649
5650 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5651 GLOBAL_BITMAP_SYSTEM_INODE,
5652 OCFS2_INVALID_SLOT);
5653 if (!data_alloc_inode) {
5654 status = -EINVAL;
5655 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005656 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005657 }
5658
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005659 mutex_lock(&data_alloc_inode->i_mutex);
5660
Mark Fashehe63aecb62007-10-18 15:30:42 -07005661 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005662 if (status < 0) {
5663 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005664 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005665 }
5666
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005667 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005668 if (IS_ERR(handle)) {
5669 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005670 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005671 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005672 }
5673
5674 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5675 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005676 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005677 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005678
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005679 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005680
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005681out_unlock:
5682 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005683 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005684
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005685out_mutex:
5686 mutex_unlock(&data_alloc_inode->i_mutex);
5687 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005688
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005689out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005690 mlog_exit(status);
5691 return status;
5692}
5693
5694int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5695{
5696 int status;
5697 struct inode *tl_inode = osb->osb_tl_inode;
5698
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005699 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005700 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005701 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005702
5703 return status;
5704}
5705
David Howellsc4028952006-11-22 14:57:56 +00005706static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005707{
5708 int status;
David Howellsc4028952006-11-22 14:57:56 +00005709 struct ocfs2_super *osb =
5710 container_of(work, struct ocfs2_super,
5711 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005712
5713 mlog_entry_void();
5714
5715 status = ocfs2_flush_truncate_log(osb);
5716 if (status < 0)
5717 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005718 else
5719 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005720
5721 mlog_exit(status);
5722}
5723
5724#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5725void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5726 int cancel)
5727{
5728 if (osb->osb_tl_inode) {
5729 /* We want to push off log flushes while truncates are
5730 * still running. */
5731 if (cancel)
5732 cancel_delayed_work(&osb->osb_truncate_log_wq);
5733
5734 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5735 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5736 }
5737}
5738
5739static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5740 int slot_num,
5741 struct inode **tl_inode,
5742 struct buffer_head **tl_bh)
5743{
5744 int status;
5745 struct inode *inode = NULL;
5746 struct buffer_head *bh = NULL;
5747
5748 inode = ocfs2_get_system_file_inode(osb,
5749 TRUNCATE_LOG_SYSTEM_INODE,
5750 slot_num);
5751 if (!inode) {
5752 status = -EINVAL;
5753 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5754 goto bail;
5755 }
5756
Joel Beckerb657c952008-11-13 14:49:11 -08005757 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005758 if (status < 0) {
5759 iput(inode);
5760 mlog_errno(status);
5761 goto bail;
5762 }
5763
5764 *tl_inode = inode;
5765 *tl_bh = bh;
5766bail:
5767 mlog_exit(status);
5768 return status;
5769}
5770
5771/* called during the 1st stage of node recovery. we stamp a clean
5772 * truncate log and pass back a copy for processing later. if the
5773 * truncate log does not require processing, a *tl_copy is set to
5774 * NULL. */
5775int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5776 int slot_num,
5777 struct ocfs2_dinode **tl_copy)
5778{
5779 int status;
5780 struct inode *tl_inode = NULL;
5781 struct buffer_head *tl_bh = NULL;
5782 struct ocfs2_dinode *di;
5783 struct ocfs2_truncate_log *tl;
5784
5785 *tl_copy = NULL;
5786
5787 mlog(0, "recover truncate log from slot %d\n", slot_num);
5788
5789 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5790 if (status < 0) {
5791 mlog_errno(status);
5792 goto bail;
5793 }
5794
5795 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005796
Joel Becker10995aa2008-11-13 14:49:12 -08005797 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
5798 * validated by the underlying call to ocfs2_read_inode_block(),
5799 * so any corruption is a code bug */
5800 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5801
5802 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005803 if (le16_to_cpu(tl->tl_used)) {
5804 mlog(0, "We'll have %u logs to recover\n",
5805 le16_to_cpu(tl->tl_used));
5806
5807 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5808 if (!(*tl_copy)) {
5809 status = -ENOMEM;
5810 mlog_errno(status);
5811 goto bail;
5812 }
5813
5814 /* Assuming the write-out below goes well, this copy
5815 * will be passed back to recovery for processing. */
5816 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5817
5818 /* All we need to do to clear the truncate log is set
5819 * tl_used. */
5820 tl->tl_used = 0;
5821
Joel Becker13723d02008-10-17 19:25:01 -07005822 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Mark Fashehccd979b2005-12-15 14:31:24 -08005823 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5824 if (status < 0) {
5825 mlog_errno(status);
5826 goto bail;
5827 }
5828 }
5829
5830bail:
5831 if (tl_inode)
5832 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07005833 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005834
5835 if (status < 0 && (*tl_copy)) {
5836 kfree(*tl_copy);
5837 *tl_copy = NULL;
5838 }
5839
5840 mlog_exit(status);
5841 return status;
5842}
5843
5844int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5845 struct ocfs2_dinode *tl_copy)
5846{
5847 int status = 0;
5848 int i;
5849 unsigned int clusters, num_recs, start_cluster;
5850 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005851 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005852 struct inode *tl_inode = osb->osb_tl_inode;
5853 struct ocfs2_truncate_log *tl;
5854
5855 mlog_entry_void();
5856
5857 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5858 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5859 return -EINVAL;
5860 }
5861
5862 tl = &tl_copy->id2.i_dealloc;
5863 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005864 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07005865 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08005866
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005867 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005868 for(i = 0; i < num_recs; i++) {
5869 if (ocfs2_truncate_log_needs_flush(osb)) {
5870 status = __ocfs2_flush_truncate_log(osb);
5871 if (status < 0) {
5872 mlog_errno(status);
5873 goto bail_up;
5874 }
5875 }
5876
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005877 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005878 if (IS_ERR(handle)) {
5879 status = PTR_ERR(handle);
5880 mlog_errno(status);
5881 goto bail_up;
5882 }
5883
5884 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5885 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5886 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5887
5888 status = ocfs2_truncate_log_append(osb, handle,
5889 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005890 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005891 if (status < 0) {
5892 mlog_errno(status);
5893 goto bail_up;
5894 }
5895 }
5896
5897bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005898 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005899
5900 mlog_exit(status);
5901 return status;
5902}
5903
5904void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5905{
5906 int status;
5907 struct inode *tl_inode = osb->osb_tl_inode;
5908
5909 mlog_entry_void();
5910
5911 if (tl_inode) {
5912 cancel_delayed_work(&osb->osb_truncate_log_wq);
5913 flush_workqueue(ocfs2_wq);
5914
5915 status = ocfs2_flush_truncate_log(osb);
5916 if (status < 0)
5917 mlog_errno(status);
5918
5919 brelse(osb->osb_tl_bh);
5920 iput(osb->osb_tl_inode);
5921 }
5922
5923 mlog_exit_void();
5924}
5925
5926int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5927{
5928 int status;
5929 struct inode *tl_inode = NULL;
5930 struct buffer_head *tl_bh = NULL;
5931
5932 mlog_entry_void();
5933
5934 status = ocfs2_get_truncate_log_info(osb,
5935 osb->slot_num,
5936 &tl_inode,
5937 &tl_bh);
5938 if (status < 0)
5939 mlog_errno(status);
5940
5941 /* ocfs2_truncate_log_shutdown keys on the existence of
5942 * osb->osb_tl_inode so we don't set any of the osb variables
5943 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00005944 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5945 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08005946 osb->osb_tl_bh = tl_bh;
5947 osb->osb_tl_inode = tl_inode;
5948
5949 mlog_exit(status);
5950 return status;
5951}
5952
Mark Fasheh2b604352007-06-22 15:45:27 -07005953/*
5954 * Delayed de-allocation of suballocator blocks.
5955 *
5956 * Some sets of block de-allocations might involve multiple suballocator inodes.
5957 *
5958 * The locking for this can get extremely complicated, especially when
5959 * the suballocator inodes to delete from aren't known until deep
5960 * within an unrelated codepath.
5961 *
5962 * ocfs2_extent_block structures are a good example of this - an inode
5963 * btree could have been grown by any number of nodes each allocating
5964 * out of their own suballoc inode.
5965 *
5966 * These structures allow the delay of block de-allocation until a
5967 * later time, when locking of multiple cluster inodes won't cause
5968 * deadlock.
5969 */
5970
5971/*
Tao Ma2891d292008-11-12 08:26:58 +08005972 * Describe a single bit freed from a suballocator. For the block
5973 * suballocators, it represents one block. For the global cluster
5974 * allocator, it represents some clusters and free_bit indicates
5975 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07005976 */
5977struct ocfs2_cached_block_free {
5978 struct ocfs2_cached_block_free *free_next;
5979 u64 free_blk;
5980 unsigned int free_bit;
5981};
5982
5983struct ocfs2_per_slot_free_list {
5984 struct ocfs2_per_slot_free_list *f_next_suballocator;
5985 int f_inode_type;
5986 int f_slot;
5987 struct ocfs2_cached_block_free *f_first;
5988};
5989
Tao Ma2891d292008-11-12 08:26:58 +08005990static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
5991 int sysfile_type,
5992 int slot,
5993 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07005994{
5995 int ret;
5996 u64 bg_blkno;
5997 handle_t *handle;
5998 struct inode *inode;
5999 struct buffer_head *di_bh = NULL;
6000 struct ocfs2_cached_block_free *tmp;
6001
6002 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6003 if (!inode) {
6004 ret = -EINVAL;
6005 mlog_errno(ret);
6006 goto out;
6007 }
6008
6009 mutex_lock(&inode->i_mutex);
6010
Mark Fashehe63aecb62007-10-18 15:30:42 -07006011 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006012 if (ret) {
6013 mlog_errno(ret);
6014 goto out_mutex;
6015 }
6016
6017 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6018 if (IS_ERR(handle)) {
6019 ret = PTR_ERR(handle);
6020 mlog_errno(ret);
6021 goto out_unlock;
6022 }
6023
6024 while (head) {
6025 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6026 head->free_bit);
6027 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6028 head->free_bit, (unsigned long long)head->free_blk);
6029
6030 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6031 head->free_bit, bg_blkno, 1);
6032 if (ret) {
6033 mlog_errno(ret);
6034 goto out_journal;
6035 }
6036
6037 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6038 if (ret) {
6039 mlog_errno(ret);
6040 goto out_journal;
6041 }
6042
6043 tmp = head;
6044 head = head->free_next;
6045 kfree(tmp);
6046 }
6047
6048out_journal:
6049 ocfs2_commit_trans(osb, handle);
6050
6051out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006052 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006053 brelse(di_bh);
6054out_mutex:
6055 mutex_unlock(&inode->i_mutex);
6056 iput(inode);
6057out:
6058 while(head) {
6059 /* Premature exit may have left some dangling items. */
6060 tmp = head;
6061 head = head->free_next;
6062 kfree(tmp);
6063 }
6064
6065 return ret;
6066}
6067
Tao Ma2891d292008-11-12 08:26:58 +08006068int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6069 u64 blkno, unsigned int bit)
6070{
6071 int ret = 0;
6072 struct ocfs2_cached_block_free *item;
6073
6074 item = kmalloc(sizeof(*item), GFP_NOFS);
6075 if (item == NULL) {
6076 ret = -ENOMEM;
6077 mlog_errno(ret);
6078 return ret;
6079 }
6080
6081 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6082 bit, (unsigned long long)blkno);
6083
6084 item->free_blk = blkno;
6085 item->free_bit = bit;
6086 item->free_next = ctxt->c_global_allocator;
6087
6088 ctxt->c_global_allocator = item;
6089 return ret;
6090}
6091
6092static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6093 struct ocfs2_cached_block_free *head)
6094{
6095 struct ocfs2_cached_block_free *tmp;
6096 struct inode *tl_inode = osb->osb_tl_inode;
6097 handle_t *handle;
6098 int ret = 0;
6099
6100 mutex_lock(&tl_inode->i_mutex);
6101
6102 while (head) {
6103 if (ocfs2_truncate_log_needs_flush(osb)) {
6104 ret = __ocfs2_flush_truncate_log(osb);
6105 if (ret < 0) {
6106 mlog_errno(ret);
6107 break;
6108 }
6109 }
6110
6111 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6112 if (IS_ERR(handle)) {
6113 ret = PTR_ERR(handle);
6114 mlog_errno(ret);
6115 break;
6116 }
6117
6118 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6119 head->free_bit);
6120
6121 ocfs2_commit_trans(osb, handle);
6122 tmp = head;
6123 head = head->free_next;
6124 kfree(tmp);
6125
6126 if (ret < 0) {
6127 mlog_errno(ret);
6128 break;
6129 }
6130 }
6131
6132 mutex_unlock(&tl_inode->i_mutex);
6133
6134 while (head) {
6135 /* Premature exit may have left some dangling items. */
6136 tmp = head;
6137 head = head->free_next;
6138 kfree(tmp);
6139 }
6140
6141 return ret;
6142}
6143
Mark Fasheh2b604352007-06-22 15:45:27 -07006144int ocfs2_run_deallocs(struct ocfs2_super *osb,
6145 struct ocfs2_cached_dealloc_ctxt *ctxt)
6146{
6147 int ret = 0, ret2;
6148 struct ocfs2_per_slot_free_list *fl;
6149
6150 if (!ctxt)
6151 return 0;
6152
6153 while (ctxt->c_first_suballocator) {
6154 fl = ctxt->c_first_suballocator;
6155
6156 if (fl->f_first) {
6157 mlog(0, "Free items: (type %u, slot %d)\n",
6158 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006159 ret2 = ocfs2_free_cached_blocks(osb,
6160 fl->f_inode_type,
6161 fl->f_slot,
6162 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006163 if (ret2)
6164 mlog_errno(ret2);
6165 if (!ret)
6166 ret = ret2;
6167 }
6168
6169 ctxt->c_first_suballocator = fl->f_next_suballocator;
6170 kfree(fl);
6171 }
6172
Tao Ma2891d292008-11-12 08:26:58 +08006173 if (ctxt->c_global_allocator) {
6174 ret2 = ocfs2_free_cached_clusters(osb,
6175 ctxt->c_global_allocator);
6176 if (ret2)
6177 mlog_errno(ret2);
6178 if (!ret)
6179 ret = ret2;
6180
6181 ctxt->c_global_allocator = NULL;
6182 }
6183
Mark Fasheh2b604352007-06-22 15:45:27 -07006184 return ret;
6185}
6186
6187static struct ocfs2_per_slot_free_list *
6188ocfs2_find_per_slot_free_list(int type,
6189 int slot,
6190 struct ocfs2_cached_dealloc_ctxt *ctxt)
6191{
6192 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6193
6194 while (fl) {
6195 if (fl->f_inode_type == type && fl->f_slot == slot)
6196 return fl;
6197
6198 fl = fl->f_next_suballocator;
6199 }
6200
6201 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6202 if (fl) {
6203 fl->f_inode_type = type;
6204 fl->f_slot = slot;
6205 fl->f_first = NULL;
6206 fl->f_next_suballocator = ctxt->c_first_suballocator;
6207
6208 ctxt->c_first_suballocator = fl;
6209 }
6210 return fl;
6211}
6212
6213static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6214 int type, int slot, u64 blkno,
6215 unsigned int bit)
6216{
6217 int ret;
6218 struct ocfs2_per_slot_free_list *fl;
6219 struct ocfs2_cached_block_free *item;
6220
6221 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6222 if (fl == NULL) {
6223 ret = -ENOMEM;
6224 mlog_errno(ret);
6225 goto out;
6226 }
6227
6228 item = kmalloc(sizeof(*item), GFP_NOFS);
6229 if (item == NULL) {
6230 ret = -ENOMEM;
6231 mlog_errno(ret);
6232 goto out;
6233 }
6234
6235 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6236 type, slot, bit, (unsigned long long)blkno);
6237
6238 item->free_blk = blkno;
6239 item->free_bit = bit;
6240 item->free_next = fl->f_first;
6241
6242 fl->f_first = item;
6243
6244 ret = 0;
6245out:
6246 return ret;
6247}
6248
Mark Fasheh59a5e412007-06-22 15:52:36 -07006249static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6250 struct ocfs2_extent_block *eb)
6251{
6252 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6253 le16_to_cpu(eb->h_suballoc_slot),
6254 le64_to_cpu(eb->h_blkno),
6255 le16_to_cpu(eb->h_suballoc_bit));
6256}
6257
Mark Fashehccd979b2005-12-15 14:31:24 -08006258/* This function will figure out whether the currently last extent
6259 * block will be deleted, and if it will, what the new last extent
6260 * block will be so we can update his h_next_leaf_blk field, as well
6261 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006262static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006263 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006264 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006265 struct buffer_head **new_last_eb)
6266{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006267 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006268 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006269 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006270 struct ocfs2_extent_block *eb;
6271 struct ocfs2_extent_list *el;
6272 struct buffer_head *bh = NULL;
6273
6274 *new_last_eb = NULL;
6275
Mark Fashehccd979b2005-12-15 14:31:24 -08006276 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006277 if (!path->p_tree_depth)
6278 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006279
6280 /* trunc to zero special case - this makes tree_depth = 0
6281 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006282 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006283 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006284
Mark Fashehdcd05382007-01-16 11:32:23 -08006285 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006286 BUG_ON(!el->l_next_free_rec);
6287
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006288 /*
6289 * Make sure that this extent list will actually be empty
6290 * after we clear away the data. We can shortcut out if
6291 * there's more than one non-empty extent in the
6292 * list. Otherwise, a check of the remaining extent is
6293 * necessary.
6294 */
6295 next_free = le16_to_cpu(el->l_next_free_rec);
6296 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006297 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006298 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006299 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006300
6301 /* We may have a valid extent in index 1, check it. */
6302 if (next_free == 2)
6303 rec = &el->l_recs[1];
6304
6305 /*
6306 * Fall through - no more nonempty extents, so we want
6307 * to delete this leaf.
6308 */
6309 } else {
6310 if (next_free > 1)
6311 goto out;
6312
6313 rec = &el->l_recs[0];
6314 }
6315
6316 if (rec) {
6317 /*
6318 * Check it we'll only be trimming off the end of this
6319 * cluster.
6320 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006321 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006322 goto out;
6323 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006324
Mark Fashehdcd05382007-01-16 11:32:23 -08006325 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6326 if (ret) {
6327 mlog_errno(ret);
6328 goto out;
6329 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006330
Mark Fashehdcd05382007-01-16 11:32:23 -08006331 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6332 if (ret) {
6333 mlog_errno(ret);
6334 goto out;
6335 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006336
Mark Fashehdcd05382007-01-16 11:32:23 -08006337 eb = (struct ocfs2_extent_block *) bh->b_data;
6338 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006339
6340 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6341 * Any corruption is a code bug. */
6342 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006343
6344 *new_last_eb = bh;
6345 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006346 mlog(0, "returning block %llu, (cpos: %u)\n",
6347 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6348out:
6349 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006350
Mark Fashehdcd05382007-01-16 11:32:23 -08006351 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006352}
6353
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006354/*
6355 * Trim some clusters off the rightmost edge of a tree. Only called
6356 * during truncate.
6357 *
6358 * The caller needs to:
6359 * - start journaling of each path component.
6360 * - compute and fully set up any new last ext block
6361 */
6362static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6363 handle_t *handle, struct ocfs2_truncate_context *tc,
6364 u32 clusters_to_del, u64 *delete_start)
6365{
6366 int ret, i, index = path->p_tree_depth;
6367 u32 new_edge = 0;
6368 u64 deleted_eb = 0;
6369 struct buffer_head *bh;
6370 struct ocfs2_extent_list *el;
6371 struct ocfs2_extent_rec *rec;
6372
6373 *delete_start = 0;
6374
6375 while (index >= 0) {
6376 bh = path->p_node[index].bh;
6377 el = path->p_node[index].el;
6378
6379 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6380 index, (unsigned long long)bh->b_blocknr);
6381
6382 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6383
6384 if (index !=
6385 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6386 ocfs2_error(inode->i_sb,
6387 "Inode %lu has invalid ext. block %llu",
6388 inode->i_ino,
6389 (unsigned long long)bh->b_blocknr);
6390 ret = -EROFS;
6391 goto out;
6392 }
6393
6394find_tail_record:
6395 i = le16_to_cpu(el->l_next_free_rec) - 1;
6396 rec = &el->l_recs[i];
6397
6398 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6399 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006400 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006401 (unsigned long long)le64_to_cpu(rec->e_blkno),
6402 le16_to_cpu(el->l_next_free_rec));
6403
Mark Fashehe48edee2007-03-07 16:46:57 -08006404 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006405
6406 if (le16_to_cpu(el->l_tree_depth) == 0) {
6407 /*
6408 * If the leaf block contains a single empty
6409 * extent and no records, we can just remove
6410 * the block.
6411 */
6412 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6413 memset(rec, 0,
6414 sizeof(struct ocfs2_extent_rec));
6415 el->l_next_free_rec = cpu_to_le16(0);
6416
6417 goto delete;
6418 }
6419
6420 /*
6421 * Remove any empty extents by shifting things
6422 * left. That should make life much easier on
6423 * the code below. This condition is rare
6424 * enough that we shouldn't see a performance
6425 * hit.
6426 */
6427 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6428 le16_add_cpu(&el->l_next_free_rec, -1);
6429
6430 for(i = 0;
6431 i < le16_to_cpu(el->l_next_free_rec); i++)
6432 el->l_recs[i] = el->l_recs[i + 1];
6433
6434 memset(&el->l_recs[i], 0,
6435 sizeof(struct ocfs2_extent_rec));
6436
6437 /*
6438 * We've modified our extent list. The
6439 * simplest way to handle this change
6440 * is to being the search from the
6441 * start again.
6442 */
6443 goto find_tail_record;
6444 }
6445
Mark Fashehe48edee2007-03-07 16:46:57 -08006446 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006447
6448 /*
6449 * We'll use "new_edge" on our way back up the
6450 * tree to know what our rightmost cpos is.
6451 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006452 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006453 new_edge += le32_to_cpu(rec->e_cpos);
6454
6455 /*
6456 * The caller will use this to delete data blocks.
6457 */
6458 *delete_start = le64_to_cpu(rec->e_blkno)
6459 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006460 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006461
6462 /*
6463 * If it's now empty, remove this record.
6464 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006465 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006466 memset(rec, 0,
6467 sizeof(struct ocfs2_extent_rec));
6468 le16_add_cpu(&el->l_next_free_rec, -1);
6469 }
6470 } else {
6471 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6472 memset(rec, 0,
6473 sizeof(struct ocfs2_extent_rec));
6474 le16_add_cpu(&el->l_next_free_rec, -1);
6475
6476 goto delete;
6477 }
6478
6479 /* Can this actually happen? */
6480 if (le16_to_cpu(el->l_next_free_rec) == 0)
6481 goto delete;
6482
6483 /*
6484 * We never actually deleted any clusters
6485 * because our leaf was empty. There's no
6486 * reason to adjust the rightmost edge then.
6487 */
6488 if (new_edge == 0)
6489 goto delete;
6490
Mark Fashehe48edee2007-03-07 16:46:57 -08006491 rec->e_int_clusters = cpu_to_le32(new_edge);
6492 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006493 -le32_to_cpu(rec->e_cpos));
6494
6495 /*
6496 * A deleted child record should have been
6497 * caught above.
6498 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006499 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006500 }
6501
6502delete:
6503 ret = ocfs2_journal_dirty(handle, bh);
6504 if (ret) {
6505 mlog_errno(ret);
6506 goto out;
6507 }
6508
6509 mlog(0, "extent list container %llu, after: record %d: "
6510 "(%u, %u, %llu), next = %u.\n",
6511 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006512 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006513 (unsigned long long)le64_to_cpu(rec->e_blkno),
6514 le16_to_cpu(el->l_next_free_rec));
6515
6516 /*
6517 * We must be careful to only attempt delete of an
6518 * extent block (and not the root inode block).
6519 */
6520 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6521 struct ocfs2_extent_block *eb =
6522 (struct ocfs2_extent_block *)bh->b_data;
6523
6524 /*
6525 * Save this for use when processing the
6526 * parent block.
6527 */
6528 deleted_eb = le64_to_cpu(eb->h_blkno);
6529
6530 mlog(0, "deleting this extent block.\n");
6531
6532 ocfs2_remove_from_cache(inode, bh);
6533
Mark Fashehe48edee2007-03-07 16:46:57 -08006534 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006535 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6536 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6537
Mark Fasheh59a5e412007-06-22 15:52:36 -07006538 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6539 /* An error here is not fatal. */
6540 if (ret < 0)
6541 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006542 } else {
6543 deleted_eb = 0;
6544 }
6545
6546 index--;
6547 }
6548
6549 ret = 0;
6550out:
6551 return ret;
6552}
6553
Mark Fashehccd979b2005-12-15 14:31:24 -08006554static int ocfs2_do_truncate(struct ocfs2_super *osb,
6555 unsigned int clusters_to_del,
6556 struct inode *inode,
6557 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006558 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006559 struct ocfs2_truncate_context *tc,
6560 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006561{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006562 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006563 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006564 struct ocfs2_extent_block *last_eb = NULL;
6565 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006566 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006567 u64 delete_blk = 0;
6568
6569 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6570
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006571 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006572 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006573 if (status < 0) {
6574 mlog_errno(status);
6575 goto bail;
6576 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006577
Mark Fashehdcd05382007-01-16 11:32:23 -08006578 /*
6579 * Each component will be touched, so we might as well journal
6580 * here to avoid having to handle errors later.
6581 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006582 status = ocfs2_journal_access_path(inode, handle, path);
6583 if (status < 0) {
6584 mlog_errno(status);
6585 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006586 }
6587
6588 if (last_eb_bh) {
Joel Becker13723d02008-10-17 19:25:01 -07006589 status = ocfs2_journal_access_eb(handle, inode, last_eb_bh,
6590 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08006591 if (status < 0) {
6592 mlog_errno(status);
6593 goto bail;
6594 }
6595
6596 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6597 }
6598
6599 el = &(fe->id2.i_list);
6600
6601 /*
6602 * Lower levels depend on this never happening, but it's best
6603 * to check it up here before changing the tree.
6604 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006605 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006606 ocfs2_error(inode->i_sb,
6607 "Inode %lu has an empty extent record, depth %u\n",
6608 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006609 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006610 goto bail;
6611 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006612
Jan Karaa90714c2008-10-09 19:38:40 +02006613 vfs_dq_free_space_nodirty(inode,
6614 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006615 spin_lock(&OCFS2_I(inode)->ip_lock);
6616 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6617 clusters_to_del;
6618 spin_unlock(&OCFS2_I(inode)->ip_lock);
6619 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006620 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006621
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006622 status = ocfs2_trim_tree(inode, path, handle, tc,
6623 clusters_to_del, &delete_blk);
6624 if (status) {
6625 mlog_errno(status);
6626 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006627 }
6628
Mark Fashehdcd05382007-01-16 11:32:23 -08006629 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006630 /* trunc to zero is a special case. */
6631 el->l_tree_depth = 0;
6632 fe->i_last_eb_blk = 0;
6633 } else if (last_eb)
6634 fe->i_last_eb_blk = last_eb->h_blkno;
6635
6636 status = ocfs2_journal_dirty(handle, fe_bh);
6637 if (status < 0) {
6638 mlog_errno(status);
6639 goto bail;
6640 }
6641
6642 if (last_eb) {
6643 /* If there will be a new last extent block, then by
6644 * definition, there cannot be any leaves to the right of
6645 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006646 last_eb->h_next_leaf_blk = 0;
6647 status = ocfs2_journal_dirty(handle, last_eb_bh);
6648 if (status < 0) {
6649 mlog_errno(status);
6650 goto bail;
6651 }
6652 }
6653
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006654 if (delete_blk) {
6655 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6656 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006657 if (status < 0) {
6658 mlog_errno(status);
6659 goto bail;
6660 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006661 }
6662 status = 0;
6663bail:
Mark Fashehdcd05382007-01-16 11:32:23 -08006664
Mark Fashehccd979b2005-12-15 14:31:24 -08006665 mlog_exit(status);
6666 return status;
6667}
6668
Joel Becker2b4e30f2008-09-03 20:03:41 -07006669static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006670{
6671 set_buffer_uptodate(bh);
6672 mark_buffer_dirty(bh);
6673 return 0;
6674}
6675
Mark Fasheh1d410a62007-09-07 14:20:45 -07006676static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6677 unsigned int from, unsigned int to,
6678 struct page *page, int zero, u64 *phys)
6679{
6680 int ret, partial = 0;
6681
6682 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6683 if (ret)
6684 mlog_errno(ret);
6685
6686 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006687 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006688
6689 /*
6690 * Need to set the buffers we zero'd into uptodate
6691 * here if they aren't - ocfs2_map_page_blocks()
6692 * might've skipped some
6693 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006694 ret = walk_page_buffers(handle, page_buffers(page),
6695 from, to, &partial,
6696 ocfs2_zero_func);
6697 if (ret < 0)
6698 mlog_errno(ret);
6699 else if (ocfs2_should_order_data(inode)) {
6700 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006701 if (ret < 0)
6702 mlog_errno(ret);
6703 }
6704
6705 if (!partial)
6706 SetPageUptodate(page);
6707
6708 flush_dcache_page(page);
6709}
6710
Mark Fasheh35edec12007-07-06 14:41:18 -07006711static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6712 loff_t end, struct page **pages,
6713 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006714{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006715 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006716 struct page *page;
6717 unsigned int from, to = PAGE_CACHE_SIZE;
6718 struct super_block *sb = inode->i_sb;
6719
6720 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6721
6722 if (numpages == 0)
6723 goto out;
6724
Mark Fasheh35edec12007-07-06 14:41:18 -07006725 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006726 for(i = 0; i < numpages; i++) {
6727 page = pages[i];
6728
Mark Fasheh35edec12007-07-06 14:41:18 -07006729 from = start & (PAGE_CACHE_SIZE - 1);
6730 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6731 to = end & (PAGE_CACHE_SIZE - 1);
6732
Mark Fasheh60b11392007-02-16 11:46:50 -08006733 BUG_ON(from > PAGE_CACHE_SIZE);
6734 BUG_ON(to > PAGE_CACHE_SIZE);
6735
Mark Fasheh1d410a62007-09-07 14:20:45 -07006736 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6737 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006738
Mark Fasheh35edec12007-07-06 14:41:18 -07006739 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006740 }
6741out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006742 if (pages)
6743 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006744}
6745
Mark Fasheh35edec12007-07-06 14:41:18 -07006746static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006747 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006748{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006749 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006750 struct super_block *sb = inode->i_sb;
6751 struct address_space *mapping = inode->i_mapping;
6752 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006753 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006754
Mark Fasheh35edec12007-07-06 14:41:18 -07006755 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006756
Mark Fasheh35edec12007-07-06 14:41:18 -07006757 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6758 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6759
Mark Fasheh1d410a62007-09-07 14:20:45 -07006760 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006761 last_page_bytes = PAGE_ALIGN(end);
6762 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006763 do {
6764 pages[numpages] = grab_cache_page(mapping, index);
6765 if (!pages[numpages]) {
6766 ret = -ENOMEM;
6767 mlog_errno(ret);
6768 goto out;
6769 }
6770
6771 numpages++;
6772 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006773 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006774
6775out:
6776 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006777 if (pages)
6778 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006779 numpages = 0;
6780 }
6781
6782 *num = numpages;
6783
6784 return ret;
6785}
6786
6787/*
6788 * Zero the area past i_size but still within an allocated
6789 * cluster. This avoids exposing nonzero data on subsequent file
6790 * extends.
6791 *
6792 * We need to call this before i_size is updated on the inode because
6793 * otherwise block_write_full_page() will skip writeout of pages past
6794 * i_size. The new_i_size parameter is passed for this reason.
6795 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006796int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6797 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006798{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006799 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006800 struct page **pages = NULL;
6801 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006802 unsigned int ext_flags;
6803 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006804
6805 /*
6806 * File systems which don't support sparse files zero on every
6807 * extend.
6808 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07006809 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08006810 return 0;
6811
Mark Fasheh1d410a62007-09-07 14:20:45 -07006812 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08006813 sizeof(struct page *), GFP_NOFS);
6814 if (pages == NULL) {
6815 ret = -ENOMEM;
6816 mlog_errno(ret);
6817 goto out;
6818 }
6819
Mark Fasheh1d410a62007-09-07 14:20:45 -07006820 if (range_start == range_end)
6821 goto out;
6822
6823 ret = ocfs2_extent_map_get_blocks(inode,
6824 range_start >> sb->s_blocksize_bits,
6825 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08006826 if (ret) {
6827 mlog_errno(ret);
6828 goto out;
6829 }
6830
Mark Fasheh1d410a62007-09-07 14:20:45 -07006831 /*
6832 * Tail is a hole, or is marked unwritten. In either case, we
6833 * can count on read and write to return/push zero's.
6834 */
6835 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08006836 goto out;
6837
Mark Fasheh1d410a62007-09-07 14:20:45 -07006838 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6839 &numpages);
6840 if (ret) {
6841 mlog_errno(ret);
6842 goto out;
6843 }
6844
Mark Fasheh35edec12007-07-06 14:41:18 -07006845 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6846 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08006847
6848 /*
6849 * Initiate writeout of the pages we zero'd here. We don't
6850 * wait on them - the truncate_inode_pages() call later will
6851 * do that for us.
6852 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006853 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6854 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08006855 if (ret)
6856 mlog_errno(ret);
6857
6858out:
6859 if (pages)
6860 kfree(pages);
6861
6862 return ret;
6863}
6864
Tiger Yangfdd77702008-08-18 17:08:55 +08006865static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6866 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006867{
6868 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08006869 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006870
Tiger Yangfdd77702008-08-18 17:08:55 +08006871 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6872 memset(&di->id2, 0, blocksize -
6873 offsetof(struct ocfs2_dinode, id2) -
6874 xattrsize);
6875 else
6876 memset(&di->id2, 0, blocksize -
6877 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006878}
6879
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006880void ocfs2_dinode_new_extent_list(struct inode *inode,
6881 struct ocfs2_dinode *di)
6882{
Tiger Yangfdd77702008-08-18 17:08:55 +08006883 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006884 di->id2.i_list.l_tree_depth = 0;
6885 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08006886 di->id2.i_list.l_count = cpu_to_le16(
6887 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006888}
6889
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006890void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6891{
6892 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6893 struct ocfs2_inline_data *idata = &di->id2.i_data;
6894
6895 spin_lock(&oi->ip_lock);
6896 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6897 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6898 spin_unlock(&oi->ip_lock);
6899
6900 /*
6901 * We clear the entire i_data structure here so that all
6902 * fields can be properly initialized.
6903 */
Tiger Yangfdd77702008-08-18 17:08:55 +08006904 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006905
Tiger Yangfdd77702008-08-18 17:08:55 +08006906 idata->id_count = cpu_to_le16(
6907 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006908}
6909
6910int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6911 struct buffer_head *di_bh)
6912{
6913 int ret, i, has_data, num_pages = 0;
6914 handle_t *handle;
6915 u64 uninitialized_var(block);
6916 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6917 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6918 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006919 struct ocfs2_alloc_context *data_ac = NULL;
6920 struct page **pages = NULL;
6921 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07006922 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02006923 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006924
6925 has_data = i_size_read(inode) ? 1 : 0;
6926
6927 if (has_data) {
6928 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6929 sizeof(struct page *), GFP_NOFS);
6930 if (pages == NULL) {
6931 ret = -ENOMEM;
6932 mlog_errno(ret);
6933 goto out;
6934 }
6935
6936 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6937 if (ret) {
6938 mlog_errno(ret);
6939 goto out;
6940 }
6941 }
6942
Jan Karaa90714c2008-10-09 19:38:40 +02006943 handle = ocfs2_start_trans(osb,
6944 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006945 if (IS_ERR(handle)) {
6946 ret = PTR_ERR(handle);
6947 mlog_errno(ret);
6948 goto out_unlock;
6949 }
6950
Joel Becker13723d02008-10-17 19:25:01 -07006951 ret = ocfs2_journal_access_di(handle, inode, di_bh,
6952 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006953 if (ret) {
6954 mlog_errno(ret);
6955 goto out_commit;
6956 }
6957
6958 if (has_data) {
6959 u32 bit_off, num;
6960 unsigned int page_end;
6961 u64 phys;
6962
Jan Karaa90714c2008-10-09 19:38:40 +02006963 if (vfs_dq_alloc_space_nodirty(inode,
6964 ocfs2_clusters_to_bytes(osb->sb, 1))) {
6965 ret = -EDQUOT;
6966 goto out_commit;
6967 }
6968 did_quota = 1;
6969
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006970 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
6971 &num);
6972 if (ret) {
6973 mlog_errno(ret);
6974 goto out_commit;
6975 }
6976
6977 /*
6978 * Save two copies, one for insert, and one that can
6979 * be changed by ocfs2_map_and_dirty_page() below.
6980 */
6981 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6982
6983 /*
6984 * Non sparse file systems zero on extend, so no need
6985 * to do that now.
6986 */
6987 if (!ocfs2_sparse_alloc(osb) &&
6988 PAGE_CACHE_SIZE < osb->s_clustersize)
6989 end = PAGE_CACHE_SIZE;
6990
6991 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6992 if (ret) {
6993 mlog_errno(ret);
6994 goto out_commit;
6995 }
6996
6997 /*
6998 * This should populate the 1st page for us and mark
6999 * it up to date.
7000 */
7001 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7002 if (ret) {
7003 mlog_errno(ret);
7004 goto out_commit;
7005 }
7006
7007 page_end = PAGE_CACHE_SIZE;
7008 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7009 page_end = osb->s_clustersize;
7010
7011 for (i = 0; i < num_pages; i++)
7012 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7013 pages[i], i > 0, &phys);
7014 }
7015
7016 spin_lock(&oi->ip_lock);
7017 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7018 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7019 spin_unlock(&oi->ip_lock);
7020
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007021 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007022
7023 ocfs2_journal_dirty(handle, di_bh);
7024
7025 if (has_data) {
7026 /*
7027 * An error at this point should be extremely rare. If
7028 * this proves to be false, we could always re-build
7029 * the in-inode data from our pages.
7030 */
Joel Becker8d6220d2008-08-22 12:46:09 -07007031 ocfs2_init_dinode_extent_tree(&et, inode, di_bh);
Joel Beckerf99b9b72008-08-20 19:36:33 -07007032 ret = ocfs2_insert_extent(osb, handle, inode, &et,
7033 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007034 if (ret) {
7035 mlog_errno(ret);
7036 goto out_commit;
7037 }
7038
7039 inode->i_blocks = ocfs2_inode_sector_count(inode);
7040 }
7041
7042out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007043 if (ret < 0 && did_quota)
7044 vfs_dq_free_space_nodirty(inode,
7045 ocfs2_clusters_to_bytes(osb->sb, 1));
7046
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007047 ocfs2_commit_trans(osb, handle);
7048
7049out_unlock:
7050 if (data_ac)
7051 ocfs2_free_alloc_context(data_ac);
7052
7053out:
7054 if (pages) {
7055 ocfs2_unlock_and_free_pages(pages, num_pages);
7056 kfree(pages);
7057 }
7058
7059 return ret;
7060}
7061
Mark Fashehccd979b2005-12-15 14:31:24 -08007062/*
7063 * It is expected, that by the time you call this function,
7064 * inode->i_size and fe->i_size have been adjusted.
7065 *
7066 * WARNING: This will kfree the truncate context
7067 */
7068int ocfs2_commit_truncate(struct ocfs2_super *osb,
7069 struct inode *inode,
7070 struct buffer_head *fe_bh,
7071 struct ocfs2_truncate_context *tc)
7072{
7073 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08007074 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08007075 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07007076 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007077 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08007078 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08007079 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007080
7081 mlog_entry_void();
7082
Mark Fashehdcd05382007-01-16 11:32:23 -08007083 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007084 i_size_read(inode));
7085
Joel Becker13723d02008-10-17 19:25:01 -07007086 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7087 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08007088 if (!path) {
7089 status = -ENOMEM;
7090 mlog_errno(status);
7091 goto bail;
7092 }
Mark Fasheh83418972007-04-23 18:53:12 -07007093
7094 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7095
Mark Fashehccd979b2005-12-15 14:31:24 -08007096start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007097 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007098 * Check that we still have allocation to delete.
7099 */
7100 if (OCFS2_I(inode)->ip_clusters == 0) {
7101 status = 0;
7102 goto bail;
7103 }
7104
7105 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007106 * Truncate always works against the rightmost tree branch.
7107 */
7108 status = ocfs2_find_path(inode, path, UINT_MAX);
7109 if (status) {
7110 mlog_errno(status);
7111 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007112 }
7113
Mark Fashehdcd05382007-01-16 11:32:23 -08007114 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7115 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7116
7117 /*
7118 * By now, el will point to the extent list on the bottom most
7119 * portion of this tree. Only the tail record is considered in
7120 * each pass.
7121 *
7122 * We handle the following cases, in order:
7123 * - empty extent: delete the remaining branch
7124 * - remove the entire record
7125 * - remove a partial record
7126 * - no record needs to be removed (truncate has completed)
7127 */
7128 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007129 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7130 ocfs2_error(inode->i_sb,
7131 "Inode %llu has empty extent block at %llu\n",
7132 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7133 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7134 status = -EROFS;
7135 goto bail;
7136 }
7137
Mark Fashehccd979b2005-12-15 14:31:24 -08007138 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08007139 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08007140 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007141 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7142 clusters_to_del = 0;
7143 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007144 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007145 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007146 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08007147 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08007148 new_highest_cpos;
7149 } else {
7150 status = 0;
7151 goto bail;
7152 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007153
Mark Fashehdcd05382007-01-16 11:32:23 -08007154 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7155 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7156
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007157 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007158 tl_sem = 1;
7159 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7160 * record is free for use. If there isn't any, we flush to get
7161 * an empty truncate log. */
7162 if (ocfs2_truncate_log_needs_flush(osb)) {
7163 status = __ocfs2_flush_truncate_log(osb);
7164 if (status < 0) {
7165 mlog_errno(status);
7166 goto bail;
7167 }
7168 }
7169
7170 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007171 (struct ocfs2_dinode *)fe_bh->b_data,
7172 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007173 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007174 if (IS_ERR(handle)) {
7175 status = PTR_ERR(handle);
7176 handle = NULL;
7177 mlog_errno(status);
7178 goto bail;
7179 }
7180
Mark Fashehdcd05382007-01-16 11:32:23 -08007181 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7182 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007183 if (status < 0) {
7184 mlog_errno(status);
7185 goto bail;
7186 }
7187
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007188 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007189 tl_sem = 0;
7190
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007191 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007192 handle = NULL;
7193
Mark Fashehdcd05382007-01-16 11:32:23 -08007194 ocfs2_reinit_path(path, 1);
7195
7196 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007197 * The check above will catch the case where we've truncated
7198 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007199 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007200 goto start;
7201
Mark Fashehccd979b2005-12-15 14:31:24 -08007202bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007203
7204 ocfs2_schedule_truncate_log_flush(osb, 1);
7205
7206 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007207 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007208
7209 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007210 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007211
Mark Fasheh59a5e412007-06-22 15:52:36 -07007212 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7213
Mark Fashehdcd05382007-01-16 11:32:23 -08007214 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007215
7216 /* This will drop the ext_alloc cluster lock for us */
7217 ocfs2_free_truncate_context(tc);
7218
7219 mlog_exit(status);
7220 return status;
7221}
7222
Mark Fashehccd979b2005-12-15 14:31:24 -08007223/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007224 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007225 */
7226int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7227 struct inode *inode,
7228 struct buffer_head *fe_bh,
7229 struct ocfs2_truncate_context **tc)
7230{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007231 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007232 unsigned int new_i_clusters;
7233 struct ocfs2_dinode *fe;
7234 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007235 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007236
7237 mlog_entry_void();
7238
7239 *tc = NULL;
7240
7241 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7242 i_size_read(inode));
7243 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7244
7245 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007246 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7247 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007248
Robert P. J. Daycd861282006-12-13 00:34:52 -08007249 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007250 if (!(*tc)) {
7251 status = -ENOMEM;
7252 mlog_errno(status);
7253 goto bail;
7254 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007255 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007256
Mark Fashehccd979b2005-12-15 14:31:24 -08007257 if (fe->id2.i_list.l_tree_depth) {
Joel Becker5e965812008-11-13 14:49:16 -08007258 status = ocfs2_read_extent_block(inode,
7259 le64_to_cpu(fe->i_last_eb_blk),
7260 &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007261 if (status < 0) {
7262 mlog_errno(status);
7263 goto bail;
7264 }
7265 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007266 }
7267
7268 (*tc)->tc_last_eb_bh = last_eb_bh;
7269
Mark Fashehccd979b2005-12-15 14:31:24 -08007270 status = 0;
7271bail:
7272 if (status < 0) {
7273 if (*tc)
7274 ocfs2_free_truncate_context(*tc);
7275 *tc = NULL;
7276 }
7277 mlog_exit_void();
7278 return status;
7279}
7280
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007281/*
7282 * 'start' is inclusive, 'end' is not.
7283 */
7284int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7285 unsigned int start, unsigned int end, int trunc)
7286{
7287 int ret;
7288 unsigned int numbytes;
7289 handle_t *handle;
7290 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7291 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7292 struct ocfs2_inline_data *idata = &di->id2.i_data;
7293
7294 if (end > i_size_read(inode))
7295 end = i_size_read(inode);
7296
7297 BUG_ON(start >= end);
7298
7299 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7300 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7301 !ocfs2_supports_inline_data(osb)) {
7302 ocfs2_error(inode->i_sb,
7303 "Inline data flags for inode %llu don't agree! "
7304 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7305 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7306 le16_to_cpu(di->i_dyn_features),
7307 OCFS2_I(inode)->ip_dyn_features,
7308 osb->s_feature_incompat);
7309 ret = -EROFS;
7310 goto out;
7311 }
7312
7313 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7314 if (IS_ERR(handle)) {
7315 ret = PTR_ERR(handle);
7316 mlog_errno(ret);
7317 goto out;
7318 }
7319
Joel Becker13723d02008-10-17 19:25:01 -07007320 ret = ocfs2_journal_access_di(handle, inode, di_bh,
7321 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007322 if (ret) {
7323 mlog_errno(ret);
7324 goto out_commit;
7325 }
7326
7327 numbytes = end - start;
7328 memset(idata->id_data + start, 0, numbytes);
7329
7330 /*
7331 * No need to worry about the data page here - it's been
7332 * truncated already and inline data doesn't need it for
7333 * pushing zero's to disk, so we'll let readpage pick it up
7334 * later.
7335 */
7336 if (trunc) {
7337 i_size_write(inode, start);
7338 di->i_size = cpu_to_le64(start);
7339 }
7340
7341 inode->i_blocks = ocfs2_inode_sector_count(inode);
7342 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7343
7344 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7345 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7346
7347 ocfs2_journal_dirty(handle, di_bh);
7348
7349out_commit:
7350 ocfs2_commit_trans(osb, handle);
7351
7352out:
7353 return ret;
7354}
7355
Mark Fashehccd979b2005-12-15 14:31:24 -08007356static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7357{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007358 /*
7359 * The caller is responsible for completing deallocation
7360 * before freeing the context.
7361 */
7362 if (tc->tc_dealloc.c_first_suballocator != NULL)
7363 mlog(ML_NOTICE,
7364 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007365
Mark Fasheha81cb882008-10-07 14:25:16 -07007366 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007367
7368 kfree(tc);
7369}