blob: 4ade2b259e6dcebc503aa28c3aebc3f894bf523d [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>
Mark Fashehccd979b2005-12-15 14:31:24 -080031
32#define MLOG_MASK_PREFIX ML_DISK_ALLOC
33#include <cluster/masklog.h>
34
35#include "ocfs2.h"
36
37#include "alloc.h"
Mark Fasheh60b11392007-02-16 11:46:50 -080038#include "aops.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080039#include "dlmglue.h"
40#include "extent_map.h"
41#include "inode.h"
42#include "journal.h"
43#include "localalloc.h"
44#include "suballoc.h"
45#include "sysfile.h"
46#include "file.h"
47#include "super.h"
48#include "uptodate.h"
49
50#include "buffer_head_io.h"
51
Tao Mae7d4cb62008-08-18 17:38:44 +080052/*
53 * ocfs2_extent_tree and ocfs2_extent_tree_operations are used to abstract
54 * the b-tree operations in ocfs2. Now all the b-tree operations are not
55 * limited to ocfs2_dinode only. Any data which need to allocate clusters
56 * to store can use b-tree. And it only needs to implement its ocfs2_extent_tree
57 * and operation.
58 *
59 * ocfs2_extent_tree contains info for the root of the b-tree, it must have a
60 * root ocfs2_extent_list and a root_bh so that they can be used in the b-tree
61 * functions.
62 * ocfs2_extent_tree_operations abstract the normal operations we do for
63 * the root of extent b-tree.
64 */
65struct ocfs2_extent_tree;
66
67struct ocfs2_extent_tree_operations {
Joel Becker35dc0aa2008-08-20 16:25:06 -070068 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
69 u64 blkno);
70 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
71 void (*eo_update_clusters)(struct inode *inode,
72 struct ocfs2_extent_tree *et,
73 u32 new_clusters);
74 int (*eo_sanity_check)(struct inode *inode, struct ocfs2_extent_tree *et);
Tao Mae7d4cb62008-08-18 17:38:44 +080075};
76
77struct ocfs2_extent_tree {
Joel Beckerce1d9ea2008-08-20 16:30:07 -070078 enum ocfs2_extent_tree_type et_type;
79 struct ocfs2_extent_tree_operations *et_ops;
80 struct buffer_head *et_root_bh;
81 struct ocfs2_extent_list *et_root_el;
82 void *et_private;
83 unsigned int et_max_leaf_clusters;
Tao Mae7d4cb62008-08-18 17:38:44 +080084};
85
86static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
87 u64 blkno)
88{
Joel Beckerce1d9ea2008-08-20 16:30:07 -070089 struct ocfs2_dinode *di =
90 (struct ocfs2_dinode *)et->et_root_bh->b_data;
Tao Mae7d4cb62008-08-18 17:38:44 +080091
Joel Beckerce1d9ea2008-08-20 16:30:07 -070092 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
Tao Mae7d4cb62008-08-18 17:38:44 +080093 di->i_last_eb_blk = cpu_to_le64(blkno);
94}
95
96static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
97{
Joel Beckerce1d9ea2008-08-20 16:30:07 -070098 struct ocfs2_dinode *di =
99 (struct ocfs2_dinode *)et->et_root_bh->b_data;
Tao Mae7d4cb62008-08-18 17:38:44 +0800100
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700101 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
Tao Mae7d4cb62008-08-18 17:38:44 +0800102 return le64_to_cpu(di->i_last_eb_blk);
103}
104
105static void ocfs2_dinode_update_clusters(struct inode *inode,
106 struct ocfs2_extent_tree *et,
107 u32 clusters)
108{
109 struct ocfs2_dinode *di =
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700110 (struct ocfs2_dinode *)et->et_root_bh->b_data;
Tao Mae7d4cb62008-08-18 17:38:44 +0800111
112 le32_add_cpu(&di->i_clusters, clusters);
113 spin_lock(&OCFS2_I(inode)->ip_lock);
114 OCFS2_I(inode)->ip_clusters = le32_to_cpu(di->i_clusters);
115 spin_unlock(&OCFS2_I(inode)->ip_lock);
116}
117
118static int ocfs2_dinode_sanity_check(struct inode *inode,
119 struct ocfs2_extent_tree *et)
120{
121 int ret = 0;
122 struct ocfs2_dinode *di;
123
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700124 BUG_ON(et->et_type != OCFS2_DINODE_EXTENT);
Tao Mae7d4cb62008-08-18 17:38:44 +0800125
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700126 di = (struct ocfs2_dinode *)et->et_root_bh->b_data;
Tao Mae7d4cb62008-08-18 17:38:44 +0800127 if (!OCFS2_IS_VALID_DINODE(di)) {
128 ret = -EIO;
129 ocfs2_error(inode->i_sb,
130 "Inode %llu has invalid path root",
131 (unsigned long long)OCFS2_I(inode)->ip_blkno);
132 }
133
134 return ret;
135}
136
137static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700138 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
139 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
140 .eo_update_clusters = ocfs2_dinode_update_clusters,
141 .eo_sanity_check = ocfs2_dinode_sanity_check,
Tao Mae7d4cb62008-08-18 17:38:44 +0800142};
143
Tao Maf56654c2008-08-18 17:38:48 +0800144static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
145 u64 blkno)
146{
147 struct ocfs2_xattr_value_root *xv =
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700148 (struct ocfs2_xattr_value_root *)et->et_private;
Tao Maf56654c2008-08-18 17:38:48 +0800149
150 xv->xr_last_eb_blk = cpu_to_le64(blkno);
151}
152
153static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
154{
155 struct ocfs2_xattr_value_root *xv =
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700156 (struct ocfs2_xattr_value_root *) et->et_private;
Tao Maf56654c2008-08-18 17:38:48 +0800157
158 return le64_to_cpu(xv->xr_last_eb_blk);
159}
160
161static void ocfs2_xattr_value_update_clusters(struct inode *inode,
162 struct ocfs2_extent_tree *et,
163 u32 clusters)
164{
165 struct ocfs2_xattr_value_root *xv =
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700166 (struct ocfs2_xattr_value_root *)et->et_private;
Tao Maf56654c2008-08-18 17:38:48 +0800167
168 le32_add_cpu(&xv->xr_clusters, clusters);
169}
170
171static int ocfs2_xattr_value_sanity_check(struct inode *inode,
172 struct ocfs2_extent_tree *et)
173{
174 return 0;
175}
176
177static struct ocfs2_extent_tree_operations ocfs2_xattr_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700178 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
179 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
180 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
181 .eo_sanity_check = ocfs2_xattr_value_sanity_check,
Tao Maf56654c2008-08-18 17:38:48 +0800182};
183
Tao Maba492612008-08-18 17:38:49 +0800184static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
185 u64 blkno)
186{
187 struct ocfs2_xattr_block *xb =
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700188 (struct ocfs2_xattr_block *) et->et_root_bh->b_data;
Tao Maba492612008-08-18 17:38:49 +0800189 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
190
191 xt->xt_last_eb_blk = cpu_to_le64(blkno);
192}
193
194static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
195{
196 struct ocfs2_xattr_block *xb =
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700197 (struct ocfs2_xattr_block *) et->et_root_bh->b_data;
Tao Maba492612008-08-18 17:38:49 +0800198 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
199
200 return le64_to_cpu(xt->xt_last_eb_blk);
201}
202
203static void ocfs2_xattr_tree_update_clusters(struct inode *inode,
204 struct ocfs2_extent_tree *et,
205 u32 clusters)
206{
207 struct ocfs2_xattr_block *xb =
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700208 (struct ocfs2_xattr_block *)et->et_root_bh->b_data;
Tao Maba492612008-08-18 17:38:49 +0800209
210 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
211}
212
213static int ocfs2_xattr_tree_sanity_check(struct inode *inode,
214 struct ocfs2_extent_tree *et)
215{
216 return 0;
217}
218
219static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700220 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
221 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
222 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
223 .eo_sanity_check = ocfs2_xattr_tree_sanity_check,
Tao Maba492612008-08-18 17:38:49 +0800224};
225
Tao Mae7d4cb62008-08-18 17:38:44 +0800226static struct ocfs2_extent_tree*
Tao Maca12b7c2008-08-18 17:38:52 +0800227 ocfs2_new_extent_tree(struct inode *inode,
228 struct buffer_head *bh,
Tao Maf56654c2008-08-18 17:38:48 +0800229 enum ocfs2_extent_tree_type et_type,
230 void *private)
Tao Mae7d4cb62008-08-18 17:38:44 +0800231{
232 struct ocfs2_extent_tree *et;
233
234 et = kzalloc(sizeof(*et), GFP_NOFS);
235 if (!et)
236 return NULL;
237
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700238 et->et_type = et_type;
Tao Mae7d4cb62008-08-18 17:38:44 +0800239 get_bh(bh);
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700240 et->et_root_bh = bh;
241 et->et_private = private;
Tao Mae7d4cb62008-08-18 17:38:44 +0800242
Tao Mae7d4cb62008-08-18 17:38:44 +0800243 if (et_type == OCFS2_DINODE_EXTENT) {
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700244 et->et_root_el =
245 &((struct ocfs2_dinode *)bh->b_data)->id2.i_list;
246 et->et_ops = &ocfs2_dinode_et_ops;
Tao Maf56654c2008-08-18 17:38:48 +0800247 } else if (et_type == OCFS2_XATTR_VALUE_EXTENT) {
248 struct ocfs2_xattr_value_root *xv =
249 (struct ocfs2_xattr_value_root *) private;
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700250 et->et_root_el = &xv->xr_list;
251 et->et_ops = &ocfs2_xattr_et_ops;
Tao Maba492612008-08-18 17:38:49 +0800252 } else if (et_type == OCFS2_XATTR_TREE_EXTENT) {
253 struct ocfs2_xattr_block *xb =
254 (struct ocfs2_xattr_block *)bh->b_data;
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700255 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
256 et->et_ops = &ocfs2_xattr_tree_et_ops;
257 et->et_max_leaf_clusters = ocfs2_clusters_for_bytes(inode->i_sb,
Tao Maca12b7c2008-08-18 17:38:52 +0800258 OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
Tao Mae7d4cb62008-08-18 17:38:44 +0800259 }
260
261 return et;
262}
263
264static void ocfs2_free_extent_tree(struct ocfs2_extent_tree *et)
265{
266 if (et) {
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700267 brelse(et->et_root_bh);
Tao Mae7d4cb62008-08-18 17:38:44 +0800268 kfree(et);
269 }
270}
271
Joel Becker35dc0aa2008-08-20 16:25:06 -0700272static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
273 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800274{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700275 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800276}
277
Joel Becker35dc0aa2008-08-20 16:25:06 -0700278static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800279{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700280 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800281}
282
Joel Becker35dc0aa2008-08-20 16:25:06 -0700283static inline void ocfs2_et_update_clusters(struct inode *inode,
284 struct ocfs2_extent_tree *et,
285 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800286{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700287 et->et_ops->eo_update_clusters(inode, et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700288}
289
290static inline int ocfs2_et_sanity_check(struct inode *inode,
291 struct ocfs2_extent_tree *et)
292{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700293 return et->et_ops->eo_sanity_check(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800294}
295
Mark Fashehccd979b2005-12-15 14:31:24 -0800296static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
Mark Fasheh59a5e412007-06-22 15:52:36 -0700297static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
298 struct ocfs2_extent_block *eb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800299
Mark Fashehdcd05382007-01-16 11:32:23 -0800300/*
301 * Structures which describe a path through a btree, and functions to
302 * manipulate them.
303 *
304 * The idea here is to be as generic as possible with the tree
305 * manipulation code.
306 */
307struct ocfs2_path_item {
308 struct buffer_head *bh;
309 struct ocfs2_extent_list *el;
310};
311
312#define OCFS2_MAX_PATH_DEPTH 5
313
314struct ocfs2_path {
315 int p_tree_depth;
316 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
317};
318
319#define path_root_bh(_path) ((_path)->p_node[0].bh)
320#define path_root_el(_path) ((_path)->p_node[0].el)
321#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
322#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
323#define path_num_items(_path) ((_path)->p_tree_depth + 1)
324
325/*
326 * Reset the actual path elements so that we can re-use the structure
327 * to build another path. Generally, this involves freeing the buffer
328 * heads.
329 */
330static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
331{
332 int i, start = 0, depth = 0;
333 struct ocfs2_path_item *node;
334
335 if (keep_root)
336 start = 1;
337
338 for(i = start; i < path_num_items(path); i++) {
339 node = &path->p_node[i];
340
341 brelse(node->bh);
342 node->bh = NULL;
343 node->el = NULL;
344 }
345
346 /*
347 * Tree depth may change during truncate, or insert. If we're
348 * keeping the root extent list, then make sure that our path
349 * structure reflects the proper depth.
350 */
351 if (keep_root)
352 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
353
354 path->p_tree_depth = depth;
355}
356
357static void ocfs2_free_path(struct ocfs2_path *path)
358{
359 if (path) {
360 ocfs2_reinit_path(path, 0);
361 kfree(path);
362 }
363}
364
365/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700366 * All the elements of src into dest. After this call, src could be freed
367 * without affecting dest.
368 *
369 * Both paths should have the same root. Any non-root elements of dest
370 * will be freed.
371 */
372static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
373{
374 int i;
375
376 BUG_ON(path_root_bh(dest) != path_root_bh(src));
377 BUG_ON(path_root_el(dest) != path_root_el(src));
378
379 ocfs2_reinit_path(dest, 1);
380
381 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
382 dest->p_node[i].bh = src->p_node[i].bh;
383 dest->p_node[i].el = src->p_node[i].el;
384
385 if (dest->p_node[i].bh)
386 get_bh(dest->p_node[i].bh);
387 }
388}
389
390/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800391 * Make the *dest path the same as src and re-initialize src path to
392 * have a root only.
393 */
394static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
395{
396 int i;
397
398 BUG_ON(path_root_bh(dest) != path_root_bh(src));
399
400 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
401 brelse(dest->p_node[i].bh);
402
403 dest->p_node[i].bh = src->p_node[i].bh;
404 dest->p_node[i].el = src->p_node[i].el;
405
406 src->p_node[i].bh = NULL;
407 src->p_node[i].el = NULL;
408 }
409}
410
411/*
412 * Insert an extent block at given index.
413 *
414 * This will not take an additional reference on eb_bh.
415 */
416static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
417 struct buffer_head *eb_bh)
418{
419 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
420
421 /*
422 * Right now, no root bh is an extent block, so this helps
423 * catch code errors with dinode trees. The assertion can be
424 * safely removed if we ever need to insert extent block
425 * structures at the root.
426 */
427 BUG_ON(index == 0);
428
429 path->p_node[index].bh = eb_bh;
430 path->p_node[index].el = &eb->h_list;
431}
432
433static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
434 struct ocfs2_extent_list *root_el)
435{
436 struct ocfs2_path *path;
437
438 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
439
440 path = kzalloc(sizeof(*path), GFP_NOFS);
441 if (path) {
442 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
443 get_bh(root_bh);
444 path_root_bh(path) = root_bh;
445 path_root_el(path) = root_el;
446 }
447
448 return path;
449}
450
451/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800452 * Convenience function to journal all components in a path.
453 */
454static int ocfs2_journal_access_path(struct inode *inode, handle_t *handle,
455 struct ocfs2_path *path)
456{
457 int i, ret = 0;
458
459 if (!path)
460 goto out;
461
462 for(i = 0; i < path_num_items(path); i++) {
463 ret = ocfs2_journal_access(handle, inode, path->p_node[i].bh,
464 OCFS2_JOURNAL_ACCESS_WRITE);
465 if (ret < 0) {
466 mlog_errno(ret);
467 goto out;
468 }
469 }
470
471out:
472 return ret;
473}
474
Mark Fasheh328d5752007-06-18 10:48:04 -0700475/*
476 * Return the index of the extent record which contains cluster #v_cluster.
477 * -1 is returned if it was not found.
478 *
479 * Should work fine on interior and exterior nodes.
480 */
481int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
482{
483 int ret = -1;
484 int i;
485 struct ocfs2_extent_rec *rec;
486 u32 rec_end, rec_start, clusters;
487
488 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
489 rec = &el->l_recs[i];
490
491 rec_start = le32_to_cpu(rec->e_cpos);
492 clusters = ocfs2_rec_clusters(el, rec);
493
494 rec_end = rec_start + clusters;
495
496 if (v_cluster >= rec_start && v_cluster < rec_end) {
497 ret = i;
498 break;
499 }
500 }
501
502 return ret;
503}
504
Mark Fashehdcd05382007-01-16 11:32:23 -0800505enum ocfs2_contig_type {
506 CONTIG_NONE = 0,
507 CONTIG_LEFT,
Mark Fasheh328d5752007-06-18 10:48:04 -0700508 CONTIG_RIGHT,
509 CONTIG_LEFTRIGHT,
Mark Fashehdcd05382007-01-16 11:32:23 -0800510};
511
Mark Fashehe48edee2007-03-07 16:46:57 -0800512
513/*
514 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
515 * ocfs2_extent_contig only work properly against leaf nodes!
516 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800517static int ocfs2_block_extent_contig(struct super_block *sb,
518 struct ocfs2_extent_rec *ext,
519 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800520{
Mark Fashehe48edee2007-03-07 16:46:57 -0800521 u64 blk_end = le64_to_cpu(ext->e_blkno);
522
523 blk_end += ocfs2_clusters_to_blocks(sb,
524 le16_to_cpu(ext->e_leaf_clusters));
525
526 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800527}
528
Mark Fashehdcd05382007-01-16 11:32:23 -0800529static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
530 struct ocfs2_extent_rec *right)
531{
Mark Fashehe48edee2007-03-07 16:46:57 -0800532 u32 left_range;
533
534 left_range = le32_to_cpu(left->e_cpos) +
535 le16_to_cpu(left->e_leaf_clusters);
536
537 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800538}
539
540static enum ocfs2_contig_type
541 ocfs2_extent_contig(struct inode *inode,
542 struct ocfs2_extent_rec *ext,
543 struct ocfs2_extent_rec *insert_rec)
544{
545 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
546
Mark Fasheh328d5752007-06-18 10:48:04 -0700547 /*
548 * Refuse to coalesce extent records with different flag
549 * fields - we don't want to mix unwritten extents with user
550 * data.
551 */
552 if (ext->e_flags != insert_rec->e_flags)
553 return CONTIG_NONE;
554
Mark Fashehdcd05382007-01-16 11:32:23 -0800555 if (ocfs2_extents_adjacent(ext, insert_rec) &&
556 ocfs2_block_extent_contig(inode->i_sb, ext, blkno))
557 return CONTIG_RIGHT;
558
559 blkno = le64_to_cpu(ext->e_blkno);
560 if (ocfs2_extents_adjacent(insert_rec, ext) &&
561 ocfs2_block_extent_contig(inode->i_sb, insert_rec, blkno))
562 return CONTIG_LEFT;
563
564 return CONTIG_NONE;
565}
566
567/*
568 * NOTE: We can have pretty much any combination of contiguousness and
569 * appending.
570 *
571 * The usefulness of APPEND_TAIL is more in that it lets us know that
572 * we'll have to update the path to that leaf.
573 */
574enum ocfs2_append_type {
575 APPEND_NONE = 0,
576 APPEND_TAIL,
577};
578
Mark Fasheh328d5752007-06-18 10:48:04 -0700579enum ocfs2_split_type {
580 SPLIT_NONE = 0,
581 SPLIT_LEFT,
582 SPLIT_RIGHT,
583};
584
Mark Fashehdcd05382007-01-16 11:32:23 -0800585struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700586 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800587 enum ocfs2_append_type ins_appending;
588 enum ocfs2_contig_type ins_contig;
589 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800590 int ins_tree_depth;
591};
592
Mark Fasheh328d5752007-06-18 10:48:04 -0700593struct ocfs2_merge_ctxt {
594 enum ocfs2_contig_type c_contig_type;
595 int c_has_empty_extent;
596 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700597};
598
Mark Fashehccd979b2005-12-15 14:31:24 -0800599/*
600 * How many free extents have we got before we need more meta data?
601 */
602int ocfs2_num_free_extents(struct ocfs2_super *osb,
603 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +0800604 struct buffer_head *root_bh,
Tao Maf56654c2008-08-18 17:38:48 +0800605 enum ocfs2_extent_tree_type type,
606 void *private)
Mark Fashehccd979b2005-12-15 14:31:24 -0800607{
608 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800609 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800610 struct ocfs2_extent_block *eb;
611 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800612 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800613
614 mlog_entry_void();
615
Tao Mae7d4cb62008-08-18 17:38:44 +0800616 if (type == OCFS2_DINODE_EXTENT) {
617 struct ocfs2_dinode *fe =
618 (struct ocfs2_dinode *)root_bh->b_data;
619 if (!OCFS2_IS_VALID_DINODE(fe)) {
620 OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe);
621 retval = -EIO;
622 goto bail;
623 }
624
625 if (fe->i_last_eb_blk)
626 last_eb_blk = le64_to_cpu(fe->i_last_eb_blk);
627 el = &fe->id2.i_list;
Tao Maf56654c2008-08-18 17:38:48 +0800628 } else if (type == OCFS2_XATTR_VALUE_EXTENT) {
629 struct ocfs2_xattr_value_root *xv =
630 (struct ocfs2_xattr_value_root *) private;
631
632 last_eb_blk = le64_to_cpu(xv->xr_last_eb_blk);
633 el = &xv->xr_list;
Tao Maba492612008-08-18 17:38:49 +0800634 } else if (type == OCFS2_XATTR_TREE_EXTENT) {
635 struct ocfs2_xattr_block *xb =
636 (struct ocfs2_xattr_block *)root_bh->b_data;
637
638 last_eb_blk = le64_to_cpu(xb->xb_attrs.xb_root.xt_last_eb_blk);
639 el = &xb->xb_attrs.xb_root.xt_list;
Mark Fashehccd979b2005-12-15 14:31:24 -0800640 }
641
Tao Mae7d4cb62008-08-18 17:38:44 +0800642 if (last_eb_blk) {
643 retval = ocfs2_read_block(osb, last_eb_blk,
Mark Fashehccd979b2005-12-15 14:31:24 -0800644 &eb_bh, OCFS2_BH_CACHED, inode);
645 if (retval < 0) {
646 mlog_errno(retval);
647 goto bail;
648 }
649 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
650 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800651 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800652
653 BUG_ON(el->l_tree_depth != 0);
654
655 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
656bail:
657 if (eb_bh)
658 brelse(eb_bh);
659
660 mlog_exit(retval);
661 return retval;
662}
663
664/* expects array to already be allocated
665 *
666 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
667 * l_count for you
668 */
669static int ocfs2_create_new_meta_bhs(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700670 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800671 struct inode *inode,
672 int wanted,
673 struct ocfs2_alloc_context *meta_ac,
674 struct buffer_head *bhs[])
675{
676 int count, status, i;
677 u16 suballoc_bit_start;
678 u32 num_got;
679 u64 first_blkno;
680 struct ocfs2_extent_block *eb;
681
682 mlog_entry_void();
683
684 count = 0;
685 while (count < wanted) {
686 status = ocfs2_claim_metadata(osb,
687 handle,
688 meta_ac,
689 wanted - count,
690 &suballoc_bit_start,
691 &num_got,
692 &first_blkno);
693 if (status < 0) {
694 mlog_errno(status);
695 goto bail;
696 }
697
698 for(i = count; i < (num_got + count); i++) {
699 bhs[i] = sb_getblk(osb->sb, first_blkno);
700 if (bhs[i] == NULL) {
701 status = -EIO;
702 mlog_errno(status);
703 goto bail;
704 }
705 ocfs2_set_new_buffer_uptodate(inode, bhs[i]);
706
707 status = ocfs2_journal_access(handle, inode, bhs[i],
708 OCFS2_JOURNAL_ACCESS_CREATE);
709 if (status < 0) {
710 mlog_errno(status);
711 goto bail;
712 }
713
714 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
715 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
716 /* Ok, setup the minimal stuff here. */
717 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
718 eb->h_blkno = cpu_to_le64(first_blkno);
719 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -0800720 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -0800721 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
722 eb->h_list.l_count =
723 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
724
725 suballoc_bit_start++;
726 first_blkno++;
727
728 /* We'll also be dirtied by the caller, so
729 * this isn't absolutely necessary. */
730 status = ocfs2_journal_dirty(handle, bhs[i]);
731 if (status < 0) {
732 mlog_errno(status);
733 goto bail;
734 }
735 }
736
737 count += num_got;
738 }
739
740 status = 0;
741bail:
742 if (status < 0) {
743 for(i = 0; i < wanted; i++) {
744 if (bhs[i])
745 brelse(bhs[i]);
746 bhs[i] = NULL;
747 }
748 }
749 mlog_exit(status);
750 return status;
751}
752
753/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800754 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
755 *
756 * Returns the sum of the rightmost extent rec logical offset and
757 * cluster count.
758 *
759 * ocfs2_add_branch() uses this to determine what logical cluster
760 * value should be populated into the leftmost new branch records.
761 *
762 * ocfs2_shift_tree_depth() uses this to determine the # clusters
763 * value for the new topmost tree record.
764 */
765static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
766{
767 int i;
768
769 i = le16_to_cpu(el->l_next_free_rec) - 1;
770
771 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -0800772 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -0800773}
774
775/*
Mark Fashehccd979b2005-12-15 14:31:24 -0800776 * Add an entire tree branch to our inode. eb_bh is the extent block
777 * to start at, if we don't want to start the branch at the dinode
778 * structure.
779 *
780 * last_eb_bh is required as we have to update it's next_leaf pointer
781 * for the new last extent block.
782 *
783 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -0800784 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -0800785 */
786static int ocfs2_add_branch(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700787 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800788 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +0800789 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -0800790 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -0700791 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800792 struct ocfs2_alloc_context *meta_ac)
793{
794 int status, new_blocks, i;
795 u64 next_blkno, new_last_eb_blk;
796 struct buffer_head *bh;
797 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800798 struct ocfs2_extent_block *eb;
799 struct ocfs2_extent_list *eb_el;
800 struct ocfs2_extent_list *el;
Mark Fashehdcd05382007-01-16 11:32:23 -0800801 u32 new_cpos;
Mark Fashehccd979b2005-12-15 14:31:24 -0800802
803 mlog_entry_void();
804
Mark Fasheh328d5752007-06-18 10:48:04 -0700805 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800806
Mark Fashehccd979b2005-12-15 14:31:24 -0800807 if (eb_bh) {
808 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
809 el = &eb->h_list;
810 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700811 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -0800812
813 /* we never add a branch to a leaf. */
814 BUG_ON(!el->l_tree_depth);
815
816 new_blocks = le16_to_cpu(el->l_tree_depth);
817
818 /* allocate the number of new eb blocks we need */
819 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
820 GFP_KERNEL);
821 if (!new_eb_bhs) {
822 status = -ENOMEM;
823 mlog_errno(status);
824 goto bail;
825 }
826
827 status = ocfs2_create_new_meta_bhs(osb, handle, inode, new_blocks,
828 meta_ac, new_eb_bhs);
829 if (status < 0) {
830 mlog_errno(status);
831 goto bail;
832 }
833
Mark Fasheh328d5752007-06-18 10:48:04 -0700834 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
Mark Fashehdcd05382007-01-16 11:32:23 -0800835 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
836
Mark Fashehccd979b2005-12-15 14:31:24 -0800837 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
838 * linked with the rest of the tree.
839 * conversly, new_eb_bhs[0] is the new bottommost leaf.
840 *
841 * when we leave the loop, new_last_eb_blk will point to the
842 * newest leaf, and next_blkno will point to the topmost extent
843 * block. */
844 next_blkno = new_last_eb_blk = 0;
845 for(i = 0; i < new_blocks; i++) {
846 bh = new_eb_bhs[i];
847 eb = (struct ocfs2_extent_block *) bh->b_data;
848 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
849 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
850 status = -EIO;
851 goto bail;
852 }
853 eb_el = &eb->h_list;
854
855 status = ocfs2_journal_access(handle, inode, bh,
856 OCFS2_JOURNAL_ACCESS_CREATE);
857 if (status < 0) {
858 mlog_errno(status);
859 goto bail;
860 }
861
862 eb->h_next_leaf_blk = 0;
863 eb_el->l_tree_depth = cpu_to_le16(i);
864 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -0800865 /*
866 * This actually counts as an empty extent as
867 * c_clusters == 0
868 */
869 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -0800870 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -0800871 /*
872 * eb_el isn't always an interior node, but even leaf
873 * nodes want a zero'd flags and reserved field so
874 * this gets the whole 32 bits regardless of use.
875 */
876 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -0800877 if (!eb_el->l_tree_depth)
878 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
879
880 status = ocfs2_journal_dirty(handle, bh);
881 if (status < 0) {
882 mlog_errno(status);
883 goto bail;
884 }
885
886 next_blkno = le64_to_cpu(eb->h_blkno);
887 }
888
889 /* This is a bit hairy. We want to update up to three blocks
890 * here without leaving any of them in an inconsistent state
891 * in case of error. We don't have to worry about
892 * journal_dirty erroring as it won't unless we've aborted the
893 * handle (in which case we would never be here) so reserving
894 * the write with journal_access is all we need to do. */
Mark Fasheh328d5752007-06-18 10:48:04 -0700895 status = ocfs2_journal_access(handle, inode, *last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800896 OCFS2_JOURNAL_ACCESS_WRITE);
897 if (status < 0) {
898 mlog_errno(status);
899 goto bail;
900 }
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700901 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -0800902 OCFS2_JOURNAL_ACCESS_WRITE);
903 if (status < 0) {
904 mlog_errno(status);
905 goto bail;
906 }
907 if (eb_bh) {
908 status = ocfs2_journal_access(handle, inode, eb_bh,
909 OCFS2_JOURNAL_ACCESS_WRITE);
910 if (status < 0) {
911 mlog_errno(status);
912 goto bail;
913 }
914 }
915
916 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +0800917 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -0800918 i = le16_to_cpu(el->l_next_free_rec);
919 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -0800920 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -0800921 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800922 le16_add_cpu(&el->l_next_free_rec, 1);
923
924 /* fe needs a new last extent block pointer, as does the
925 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -0700926 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -0800927
Mark Fasheh328d5752007-06-18 10:48:04 -0700928 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -0800929 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
930
Mark Fasheh328d5752007-06-18 10:48:04 -0700931 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800932 if (status < 0)
933 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700934 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800935 if (status < 0)
936 mlog_errno(status);
937 if (eb_bh) {
938 status = ocfs2_journal_dirty(handle, eb_bh);
939 if (status < 0)
940 mlog_errno(status);
941 }
942
Mark Fasheh328d5752007-06-18 10:48:04 -0700943 /*
944 * Some callers want to track the rightmost leaf so pass it
945 * back here.
946 */
947 brelse(*last_eb_bh);
948 get_bh(new_eb_bhs[0]);
949 *last_eb_bh = new_eb_bhs[0];
950
Mark Fashehccd979b2005-12-15 14:31:24 -0800951 status = 0;
952bail:
953 if (new_eb_bhs) {
954 for (i = 0; i < new_blocks; i++)
955 if (new_eb_bhs[i])
956 brelse(new_eb_bhs[i]);
957 kfree(new_eb_bhs);
958 }
959
960 mlog_exit(status);
961 return status;
962}
963
964/*
965 * adds another level to the allocation tree.
966 * returns back the new extent block so you can add a branch to it
967 * after this call.
968 */
969static int ocfs2_shift_tree_depth(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -0700970 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -0800971 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +0800972 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -0800973 struct ocfs2_alloc_context *meta_ac,
974 struct buffer_head **ret_new_eb_bh)
975{
976 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -0800977 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -0800978 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800979 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +0800980 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -0800981 struct ocfs2_extent_list *eb_el;
982
983 mlog_entry_void();
984
985 status = ocfs2_create_new_meta_bhs(osb, handle, inode, 1, meta_ac,
986 &new_eb_bh);
987 if (status < 0) {
988 mlog_errno(status);
989 goto bail;
990 }
991
992 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
993 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
994 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
995 status = -EIO;
996 goto bail;
997 }
998
999 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001000 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001001
1002 status = ocfs2_journal_access(handle, inode, new_eb_bh,
1003 OCFS2_JOURNAL_ACCESS_CREATE);
1004 if (status < 0) {
1005 mlog_errno(status);
1006 goto bail;
1007 }
1008
Tao Mae7d4cb62008-08-18 17:38:44 +08001009 /* copy the root extent list data into the new extent block */
1010 eb_el->l_tree_depth = root_el->l_tree_depth;
1011 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1012 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1013 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001014
1015 status = ocfs2_journal_dirty(handle, new_eb_bh);
1016 if (status < 0) {
1017 mlog_errno(status);
1018 goto bail;
1019 }
1020
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001021 status = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001022 OCFS2_JOURNAL_ACCESS_WRITE);
1023 if (status < 0) {
1024 mlog_errno(status);
1025 goto bail;
1026 }
1027
Mark Fashehdcd05382007-01-16 11:32:23 -08001028 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1029
Tao Mae7d4cb62008-08-18 17:38:44 +08001030 /* update root_bh now */
1031 le16_add_cpu(&root_el->l_tree_depth, 1);
1032 root_el->l_recs[0].e_cpos = 0;
1033 root_el->l_recs[0].e_blkno = eb->h_blkno;
1034 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1035 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1036 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1037 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001038
1039 /* If this is our 1st tree depth shift, then last_eb_blk
1040 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001041 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001042 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001043
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001044 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001045 if (status < 0) {
1046 mlog_errno(status);
1047 goto bail;
1048 }
1049
1050 *ret_new_eb_bh = new_eb_bh;
1051 new_eb_bh = NULL;
1052 status = 0;
1053bail:
1054 if (new_eb_bh)
1055 brelse(new_eb_bh);
1056
1057 mlog_exit(status);
1058 return status;
1059}
1060
1061/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001062 * Should only be called when there is no space left in any of the
1063 * leaf nodes. What we want to do is find the lowest tree depth
1064 * non-leaf extent block with room for new records. There are three
1065 * valid results of this search:
1066 *
1067 * 1) a lowest extent block is found, then we pass it back in
1068 * *lowest_eb_bh and return '0'
1069 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001070 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001071 * pass NULL back in *lowest_eb_bh, but still return '0'
1072 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001073 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001074 * which case we return > 0
1075 *
1076 * return status < 0 indicates an error.
1077 */
1078static int ocfs2_find_branch_target(struct ocfs2_super *osb,
1079 struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08001080 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001081 struct buffer_head **target_bh)
1082{
1083 int status = 0, i;
1084 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001085 struct ocfs2_extent_block *eb;
1086 struct ocfs2_extent_list *el;
1087 struct buffer_head *bh = NULL;
1088 struct buffer_head *lowest_bh = NULL;
1089
1090 mlog_entry_void();
1091
1092 *target_bh = NULL;
1093
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001094 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001095
1096 while(le16_to_cpu(el->l_tree_depth) > 1) {
1097 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Mark Fashehb06970532006-03-03 10:24:33 -08001098 ocfs2_error(inode->i_sb, "Dinode %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001099 "extent list (next_free_rec == 0)",
Mark Fashehb06970532006-03-03 10:24:33 -08001100 (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08001101 status = -EIO;
1102 goto bail;
1103 }
1104 i = le16_to_cpu(el->l_next_free_rec) - 1;
1105 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1106 if (!blkno) {
Mark Fashehb06970532006-03-03 10:24:33 -08001107 ocfs2_error(inode->i_sb, "Dinode %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001108 "list where extent # %d has no physical "
1109 "block start",
Mark Fashehb06970532006-03-03 10:24:33 -08001110 (unsigned long long)OCFS2_I(inode)->ip_blkno, i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001111 status = -EIO;
1112 goto bail;
1113 }
1114
1115 if (bh) {
1116 brelse(bh);
1117 bh = NULL;
1118 }
1119
1120 status = ocfs2_read_block(osb, blkno, &bh, OCFS2_BH_CACHED,
1121 inode);
1122 if (status < 0) {
1123 mlog_errno(status);
1124 goto bail;
1125 }
1126
1127 eb = (struct ocfs2_extent_block *) bh->b_data;
1128 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1129 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1130 status = -EIO;
1131 goto bail;
1132 }
1133 el = &eb->h_list;
1134
1135 if (le16_to_cpu(el->l_next_free_rec) <
1136 le16_to_cpu(el->l_count)) {
1137 if (lowest_bh)
1138 brelse(lowest_bh);
1139 lowest_bh = bh;
1140 get_bh(lowest_bh);
1141 }
1142 }
1143
1144 /* If we didn't find one and the fe doesn't have any room,
1145 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001146 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001147 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001148 status = 1;
1149
1150 *target_bh = lowest_bh;
1151bail:
1152 if (bh)
1153 brelse(bh);
1154
1155 mlog_exit(status);
1156 return status;
1157}
1158
Mark Fashehe48edee2007-03-07 16:46:57 -08001159/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001160 * Grow a b-tree so that it has more records.
1161 *
1162 * We might shift the tree depth in which case existing paths should
1163 * be considered invalid.
1164 *
1165 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001166 *
1167 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001168 */
1169static int ocfs2_grow_tree(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001170 struct ocfs2_extent_tree *et, int *final_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07001171 struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001172 struct ocfs2_alloc_context *meta_ac)
1173{
1174 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001175 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001176 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001177 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1178 struct buffer_head *bh = NULL;
1179
1180 BUG_ON(meta_ac == NULL);
1181
Tao Mae7d4cb62008-08-18 17:38:44 +08001182 shift = ocfs2_find_branch_target(osb, inode, et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001183 if (shift < 0) {
1184 ret = shift;
1185 mlog_errno(ret);
1186 goto out;
1187 }
1188
1189 /* We traveled all the way to the bottom of the allocation tree
1190 * and didn't find room for any more extents - we need to add
1191 * another tree level */
1192 if (shift) {
1193 BUG_ON(bh);
1194 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1195
1196 /* ocfs2_shift_tree_depth will return us a buffer with
1197 * the new extent block (so we can pass that to
1198 * ocfs2_add_branch). */
Tao Mae7d4cb62008-08-18 17:38:44 +08001199 ret = ocfs2_shift_tree_depth(osb, handle, inode, et,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001200 meta_ac, &bh);
1201 if (ret < 0) {
1202 mlog_errno(ret);
1203 goto out;
1204 }
1205 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001206 if (depth == 1) {
1207 /*
1208 * Special case: we have room now if we shifted from
1209 * tree_depth 0, so no more work needs to be done.
1210 *
1211 * We won't be calling add_branch, so pass
1212 * back *last_eb_bh as the new leaf. At depth
1213 * zero, it should always be null so there's
1214 * no reason to brelse.
1215 */
1216 BUG_ON(*last_eb_bh);
1217 get_bh(bh);
1218 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001219 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001220 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001221 }
1222
1223 /* call ocfs2_add_branch to add the final part of the tree with
1224 * the new data. */
1225 mlog(0, "add branch. bh = %p\n", bh);
Tao Mae7d4cb62008-08-18 17:38:44 +08001226 ret = ocfs2_add_branch(osb, handle, inode, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001227 meta_ac);
1228 if (ret < 0) {
1229 mlog_errno(ret);
1230 goto out;
1231 }
1232
1233out:
1234 if (final_depth)
1235 *final_depth = depth;
1236 brelse(bh);
1237 return ret;
1238}
1239
1240/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001241 * This function will discard the rightmost extent record.
1242 */
1243static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1244{
1245 int next_free = le16_to_cpu(el->l_next_free_rec);
1246 int count = le16_to_cpu(el->l_count);
1247 unsigned int num_bytes;
1248
1249 BUG_ON(!next_free);
1250 /* This will cause us to go off the end of our extent list. */
1251 BUG_ON(next_free >= count);
1252
1253 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1254
1255 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1256}
1257
1258static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1259 struct ocfs2_extent_rec *insert_rec)
1260{
1261 int i, insert_index, next_free, has_empty, num_bytes;
1262 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1263 struct ocfs2_extent_rec *rec;
1264
1265 next_free = le16_to_cpu(el->l_next_free_rec);
1266 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1267
1268 BUG_ON(!next_free);
1269
1270 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001271 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001272
1273 /*
1274 * The easiest way to approach this is to just remove the
1275 * empty extent and temporarily decrement next_free.
1276 */
1277 if (has_empty) {
1278 /*
1279 * If next_free was 1 (only an empty extent), this
1280 * loop won't execute, which is fine. We still want
1281 * the decrement above to happen.
1282 */
1283 for(i = 0; i < (next_free - 1); i++)
1284 el->l_recs[i] = el->l_recs[i+1];
1285
1286 next_free--;
1287 }
1288
1289 /*
1290 * Figure out what the new record index should be.
1291 */
1292 for(i = 0; i < next_free; i++) {
1293 rec = &el->l_recs[i];
1294
1295 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1296 break;
1297 }
1298 insert_index = i;
1299
1300 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1301 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1302
1303 BUG_ON(insert_index < 0);
1304 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1305 BUG_ON(insert_index > next_free);
1306
1307 /*
1308 * No need to memmove if we're just adding to the tail.
1309 */
1310 if (insert_index != next_free) {
1311 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1312
1313 num_bytes = next_free - insert_index;
1314 num_bytes *= sizeof(struct ocfs2_extent_rec);
1315 memmove(&el->l_recs[insert_index + 1],
1316 &el->l_recs[insert_index],
1317 num_bytes);
1318 }
1319
1320 /*
1321 * Either we had an empty extent, and need to re-increment or
1322 * there was no empty extent on a non full rightmost leaf node,
1323 * in which case we still need to increment.
1324 */
1325 next_free++;
1326 el->l_next_free_rec = cpu_to_le16(next_free);
1327 /*
1328 * Make sure none of the math above just messed up our tree.
1329 */
1330 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1331
1332 el->l_recs[insert_index] = *insert_rec;
1333
1334}
1335
Mark Fasheh328d5752007-06-18 10:48:04 -07001336static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1337{
1338 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1339
1340 BUG_ON(num_recs == 0);
1341
1342 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1343 num_recs--;
1344 size = num_recs * sizeof(struct ocfs2_extent_rec);
1345 memmove(&el->l_recs[0], &el->l_recs[1], size);
1346 memset(&el->l_recs[num_recs], 0,
1347 sizeof(struct ocfs2_extent_rec));
1348 el->l_next_free_rec = cpu_to_le16(num_recs);
1349 }
1350}
1351
Mark Fashehdcd05382007-01-16 11:32:23 -08001352/*
1353 * Create an empty extent record .
1354 *
1355 * l_next_free_rec may be updated.
1356 *
1357 * If an empty extent already exists do nothing.
1358 */
1359static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1360{
1361 int next_free = le16_to_cpu(el->l_next_free_rec);
1362
Mark Fashehe48edee2007-03-07 16:46:57 -08001363 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1364
Mark Fashehdcd05382007-01-16 11:32:23 -08001365 if (next_free == 0)
1366 goto set_and_inc;
1367
1368 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1369 return;
1370
1371 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1372 "Asked to create an empty extent in a full list:\n"
1373 "count = %u, tree depth = %u",
1374 le16_to_cpu(el->l_count),
1375 le16_to_cpu(el->l_tree_depth));
1376
1377 ocfs2_shift_records_right(el);
1378
1379set_and_inc:
1380 le16_add_cpu(&el->l_next_free_rec, 1);
1381 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1382}
1383
1384/*
1385 * For a rotation which involves two leaf nodes, the "root node" is
1386 * the lowest level tree node which contains a path to both leafs. This
1387 * resulting set of information can be used to form a complete "subtree"
1388 *
1389 * This function is passed two full paths from the dinode down to a
1390 * pair of adjacent leaves. It's task is to figure out which path
1391 * index contains the subtree root - this can be the root index itself
1392 * in a worst-case rotation.
1393 *
1394 * The array index of the subtree root is passed back.
1395 */
1396static int ocfs2_find_subtree_root(struct inode *inode,
1397 struct ocfs2_path *left,
1398 struct ocfs2_path *right)
1399{
1400 int i = 0;
1401
1402 /*
1403 * Check that the caller passed in two paths from the same tree.
1404 */
1405 BUG_ON(path_root_bh(left) != path_root_bh(right));
1406
1407 do {
1408 i++;
1409
1410 /*
1411 * The caller didn't pass two adjacent paths.
1412 */
1413 mlog_bug_on_msg(i > left->p_tree_depth,
1414 "Inode %lu, left depth %u, right depth %u\n"
1415 "left leaf blk %llu, right leaf blk %llu\n",
1416 inode->i_ino, left->p_tree_depth,
1417 right->p_tree_depth,
1418 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1419 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1420 } while (left->p_node[i].bh->b_blocknr ==
1421 right->p_node[i].bh->b_blocknr);
1422
1423 return i - 1;
1424}
1425
1426typedef void (path_insert_t)(void *, struct buffer_head *);
1427
1428/*
1429 * Traverse a btree path in search of cpos, starting at root_el.
1430 *
1431 * This code can be called with a cpos larger than the tree, in which
1432 * case it will return the rightmost path.
1433 */
1434static int __ocfs2_find_path(struct inode *inode,
1435 struct ocfs2_extent_list *root_el, u32 cpos,
1436 path_insert_t *func, void *data)
1437{
1438 int i, ret = 0;
1439 u32 range;
1440 u64 blkno;
1441 struct buffer_head *bh = NULL;
1442 struct ocfs2_extent_block *eb;
1443 struct ocfs2_extent_list *el;
1444 struct ocfs2_extent_rec *rec;
1445 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1446
1447 el = root_el;
1448 while (el->l_tree_depth) {
1449 if (le16_to_cpu(el->l_next_free_rec) == 0) {
1450 ocfs2_error(inode->i_sb,
1451 "Inode %llu has empty extent list at "
1452 "depth %u\n",
1453 (unsigned long long)oi->ip_blkno,
1454 le16_to_cpu(el->l_tree_depth));
1455 ret = -EROFS;
1456 goto out;
1457
1458 }
1459
1460 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1461 rec = &el->l_recs[i];
1462
1463 /*
1464 * In the case that cpos is off the allocation
1465 * tree, this should just wind up returning the
1466 * rightmost record.
1467 */
1468 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001469 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001470 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1471 break;
1472 }
1473
1474 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1475 if (blkno == 0) {
1476 ocfs2_error(inode->i_sb,
1477 "Inode %llu has bad blkno in extent list "
1478 "at depth %u (index %d)\n",
1479 (unsigned long long)oi->ip_blkno,
1480 le16_to_cpu(el->l_tree_depth), i);
1481 ret = -EROFS;
1482 goto out;
1483 }
1484
1485 brelse(bh);
1486 bh = NULL;
1487 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb), blkno,
1488 &bh, OCFS2_BH_CACHED, inode);
1489 if (ret) {
1490 mlog_errno(ret);
1491 goto out;
1492 }
1493
1494 eb = (struct ocfs2_extent_block *) bh->b_data;
1495 el = &eb->h_list;
1496 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
1497 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
1498 ret = -EIO;
1499 goto out;
1500 }
1501
1502 if (le16_to_cpu(el->l_next_free_rec) >
1503 le16_to_cpu(el->l_count)) {
1504 ocfs2_error(inode->i_sb,
1505 "Inode %llu has bad count in extent list "
1506 "at block %llu (next free=%u, count=%u)\n",
1507 (unsigned long long)oi->ip_blkno,
1508 (unsigned long long)bh->b_blocknr,
1509 le16_to_cpu(el->l_next_free_rec),
1510 le16_to_cpu(el->l_count));
1511 ret = -EROFS;
1512 goto out;
1513 }
1514
1515 if (func)
1516 func(data, bh);
1517 }
1518
1519out:
1520 /*
1521 * Catch any trailing bh that the loop didn't handle.
1522 */
1523 brelse(bh);
1524
1525 return ret;
1526}
1527
1528/*
1529 * Given an initialized path (that is, it has a valid root extent
1530 * list), this function will traverse the btree in search of the path
1531 * which would contain cpos.
1532 *
1533 * The path traveled is recorded in the path structure.
1534 *
1535 * Note that this will not do any comparisons on leaf node extent
1536 * records, so it will work fine in the case that we just added a tree
1537 * branch.
1538 */
1539struct find_path_data {
1540 int index;
1541 struct ocfs2_path *path;
1542};
1543static void find_path_ins(void *data, struct buffer_head *bh)
1544{
1545 struct find_path_data *fp = data;
1546
1547 get_bh(bh);
1548 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1549 fp->index++;
1550}
1551static int ocfs2_find_path(struct inode *inode, struct ocfs2_path *path,
1552 u32 cpos)
1553{
1554 struct find_path_data data;
1555
1556 data.index = 1;
1557 data.path = path;
1558 return __ocfs2_find_path(inode, path_root_el(path), cpos,
1559 find_path_ins, &data);
1560}
1561
1562static void find_leaf_ins(void *data, struct buffer_head *bh)
1563{
1564 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1565 struct ocfs2_extent_list *el = &eb->h_list;
1566 struct buffer_head **ret = data;
1567
1568 /* We want to retain only the leaf block. */
1569 if (le16_to_cpu(el->l_tree_depth) == 0) {
1570 get_bh(bh);
1571 *ret = bh;
1572 }
1573}
1574/*
1575 * Find the leaf block in the tree which would contain cpos. No
1576 * checking of the actual leaf is done.
1577 *
1578 * Some paths want to call this instead of allocating a path structure
1579 * and calling ocfs2_find_path().
1580 *
1581 * This function doesn't handle non btree extent lists.
1582 */
Mark Fasheh363041a2007-01-17 12:31:35 -08001583int ocfs2_find_leaf(struct inode *inode, struct ocfs2_extent_list *root_el,
1584 u32 cpos, struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001585{
1586 int ret;
1587 struct buffer_head *bh = NULL;
1588
1589 ret = __ocfs2_find_path(inode, root_el, cpos, find_leaf_ins, &bh);
1590 if (ret) {
1591 mlog_errno(ret);
1592 goto out;
1593 }
1594
1595 *leaf_bh = bh;
1596out:
1597 return ret;
1598}
1599
1600/*
1601 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1602 *
1603 * Basically, we've moved stuff around at the bottom of the tree and
1604 * we need to fix up the extent records above the changes to reflect
1605 * the new changes.
1606 *
1607 * left_rec: the record on the left.
1608 * left_child_el: is the child list pointed to by left_rec
1609 * right_rec: the record to the right of left_rec
1610 * right_child_el: is the child list pointed to by right_rec
1611 *
1612 * By definition, this only works on interior nodes.
1613 */
1614static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1615 struct ocfs2_extent_list *left_child_el,
1616 struct ocfs2_extent_rec *right_rec,
1617 struct ocfs2_extent_list *right_child_el)
1618{
1619 u32 left_clusters, right_end;
1620
1621 /*
1622 * Interior nodes never have holes. Their cpos is the cpos of
1623 * the leftmost record in their child list. Their cluster
1624 * count covers the full theoretical range of their child list
1625 * - the range between their cpos and the cpos of the record
1626 * immediately to their right.
1627 */
1628 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07001629 if (ocfs2_is_empty_extent(&right_child_el->l_recs[0])) {
1630 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1631 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1632 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001633 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001634 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001635
1636 /*
1637 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001638 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001639 * updating e_cpos to keep the same highest cluster count.
1640 */
1641 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001642 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001643
1644 right_rec->e_cpos = left_rec->e_cpos;
1645 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1646
1647 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001648 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001649}
1650
1651/*
1652 * Adjust the adjacent root node records involved in a
1653 * rotation. left_el_blkno is passed in as a key so that we can easily
1654 * find it's index in the root list.
1655 */
1656static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1657 struct ocfs2_extent_list *left_el,
1658 struct ocfs2_extent_list *right_el,
1659 u64 left_el_blkno)
1660{
1661 int i;
1662
1663 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1664 le16_to_cpu(left_el->l_tree_depth));
1665
1666 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1667 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1668 break;
1669 }
1670
1671 /*
1672 * The path walking code should have never returned a root and
1673 * two paths which are not adjacent.
1674 */
1675 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
1676
1677 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
1678 &root_el->l_recs[i + 1], right_el);
1679}
1680
1681/*
1682 * We've changed a leaf block (in right_path) and need to reflect that
1683 * change back up the subtree.
1684 *
1685 * This happens in multiple places:
1686 * - When we've moved an extent record from the left path leaf to the right
1687 * path leaf to make room for an empty extent in the left path leaf.
1688 * - When our insert into the right path leaf is at the leftmost edge
1689 * and requires an update of the path immediately to it's left. This
1690 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08001691 * - When we've adjusted the last extent record in the left path leaf and the
1692 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08001693 */
1694static void ocfs2_complete_edge_insert(struct inode *inode, handle_t *handle,
1695 struct ocfs2_path *left_path,
1696 struct ocfs2_path *right_path,
1697 int subtree_index)
1698{
1699 int ret, i, idx;
1700 struct ocfs2_extent_list *el, *left_el, *right_el;
1701 struct ocfs2_extent_rec *left_rec, *right_rec;
1702 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
1703
1704 /*
1705 * Update the counts and position values within all the
1706 * interior nodes to reflect the leaf rotation we just did.
1707 *
1708 * The root node is handled below the loop.
1709 *
1710 * We begin the loop with right_el and left_el pointing to the
1711 * leaf lists and work our way up.
1712 *
1713 * NOTE: within this loop, left_el and right_el always refer
1714 * to the *child* lists.
1715 */
1716 left_el = path_leaf_el(left_path);
1717 right_el = path_leaf_el(right_path);
1718 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
1719 mlog(0, "Adjust records at index %u\n", i);
1720
1721 /*
1722 * One nice property of knowing that all of these
1723 * nodes are below the root is that we only deal with
1724 * the leftmost right node record and the rightmost
1725 * left node record.
1726 */
1727 el = left_path->p_node[i].el;
1728 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
1729 left_rec = &el->l_recs[idx];
1730
1731 el = right_path->p_node[i].el;
1732 right_rec = &el->l_recs[0];
1733
1734 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
1735 right_el);
1736
1737 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
1738 if (ret)
1739 mlog_errno(ret);
1740
1741 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
1742 if (ret)
1743 mlog_errno(ret);
1744
1745 /*
1746 * Setup our list pointers now so that the current
1747 * parents become children in the next iteration.
1748 */
1749 left_el = left_path->p_node[i].el;
1750 right_el = right_path->p_node[i].el;
1751 }
1752
1753 /*
1754 * At the root node, adjust the two adjacent records which
1755 * begin our path to the leaves.
1756 */
1757
1758 el = left_path->p_node[subtree_index].el;
1759 left_el = left_path->p_node[subtree_index + 1].el;
1760 right_el = right_path->p_node[subtree_index + 1].el;
1761
1762 ocfs2_adjust_root_records(el, left_el, right_el,
1763 left_path->p_node[subtree_index + 1].bh->b_blocknr);
1764
1765 root_bh = left_path->p_node[subtree_index].bh;
1766
1767 ret = ocfs2_journal_dirty(handle, root_bh);
1768 if (ret)
1769 mlog_errno(ret);
1770}
1771
1772static int ocfs2_rotate_subtree_right(struct inode *inode,
1773 handle_t *handle,
1774 struct ocfs2_path *left_path,
1775 struct ocfs2_path *right_path,
1776 int subtree_index)
1777{
1778 int ret, i;
1779 struct buffer_head *right_leaf_bh;
1780 struct buffer_head *left_leaf_bh = NULL;
1781 struct buffer_head *root_bh;
1782 struct ocfs2_extent_list *right_el, *left_el;
1783 struct ocfs2_extent_rec move_rec;
1784
1785 left_leaf_bh = path_leaf_bh(left_path);
1786 left_el = path_leaf_el(left_path);
1787
1788 if (left_el->l_next_free_rec != left_el->l_count) {
1789 ocfs2_error(inode->i_sb,
1790 "Inode %llu has non-full interior leaf node %llu"
1791 "(next free = %u)",
1792 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1793 (unsigned long long)left_leaf_bh->b_blocknr,
1794 le16_to_cpu(left_el->l_next_free_rec));
1795 return -EROFS;
1796 }
1797
1798 /*
1799 * This extent block may already have an empty record, so we
1800 * return early if so.
1801 */
1802 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
1803 return 0;
1804
1805 root_bh = left_path->p_node[subtree_index].bh;
1806 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
1807
1808 ret = ocfs2_journal_access(handle, inode, root_bh,
1809 OCFS2_JOURNAL_ACCESS_WRITE);
1810 if (ret) {
1811 mlog_errno(ret);
1812 goto out;
1813 }
1814
1815 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
1816 ret = ocfs2_journal_access(handle, inode,
1817 right_path->p_node[i].bh,
1818 OCFS2_JOURNAL_ACCESS_WRITE);
1819 if (ret) {
1820 mlog_errno(ret);
1821 goto out;
1822 }
1823
1824 ret = ocfs2_journal_access(handle, inode,
1825 left_path->p_node[i].bh,
1826 OCFS2_JOURNAL_ACCESS_WRITE);
1827 if (ret) {
1828 mlog_errno(ret);
1829 goto out;
1830 }
1831 }
1832
1833 right_leaf_bh = path_leaf_bh(right_path);
1834 right_el = path_leaf_el(right_path);
1835
1836 /* This is a code error, not a disk corruption. */
1837 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
1838 "because rightmost leaf block %llu is empty\n",
1839 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1840 (unsigned long long)right_leaf_bh->b_blocknr);
1841
1842 ocfs2_create_empty_extent(right_el);
1843
1844 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
1845 if (ret) {
1846 mlog_errno(ret);
1847 goto out;
1848 }
1849
1850 /* Do the copy now. */
1851 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
1852 move_rec = left_el->l_recs[i];
1853 right_el->l_recs[0] = move_rec;
1854
1855 /*
1856 * Clear out the record we just copied and shift everything
1857 * over, leaving an empty extent in the left leaf.
1858 *
1859 * We temporarily subtract from next_free_rec so that the
1860 * shift will lose the tail record (which is now defunct).
1861 */
1862 le16_add_cpu(&left_el->l_next_free_rec, -1);
1863 ocfs2_shift_records_right(left_el);
1864 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1865 le16_add_cpu(&left_el->l_next_free_rec, 1);
1866
1867 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
1868 if (ret) {
1869 mlog_errno(ret);
1870 goto out;
1871 }
1872
1873 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
1874 subtree_index);
1875
1876out:
1877 return ret;
1878}
1879
1880/*
1881 * Given a full path, determine what cpos value would return us a path
1882 * containing the leaf immediately to the left of the current one.
1883 *
1884 * Will return zero if the path passed in is already the leftmost path.
1885 */
1886static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
1887 struct ocfs2_path *path, u32 *cpos)
1888{
1889 int i, j, ret = 0;
1890 u64 blkno;
1891 struct ocfs2_extent_list *el;
1892
Mark Fashehe48edee2007-03-07 16:46:57 -08001893 BUG_ON(path->p_tree_depth == 0);
1894
Mark Fashehdcd05382007-01-16 11:32:23 -08001895 *cpos = 0;
1896
1897 blkno = path_leaf_bh(path)->b_blocknr;
1898
1899 /* Start at the tree node just above the leaf and work our way up. */
1900 i = path->p_tree_depth - 1;
1901 while (i >= 0) {
1902 el = path->p_node[i].el;
1903
1904 /*
1905 * Find the extent record just before the one in our
1906 * path.
1907 */
1908 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
1909 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
1910 if (j == 0) {
1911 if (i == 0) {
1912 /*
1913 * We've determined that the
1914 * path specified is already
1915 * the leftmost one - return a
1916 * cpos of zero.
1917 */
1918 goto out;
1919 }
1920 /*
1921 * The leftmost record points to our
1922 * leaf - we need to travel up the
1923 * tree one level.
1924 */
1925 goto next_node;
1926 }
1927
1928 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001929 *cpos = *cpos + ocfs2_rec_clusters(el,
1930 &el->l_recs[j - 1]);
1931 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08001932 goto out;
1933 }
1934 }
1935
1936 /*
1937 * If we got here, we never found a valid node where
1938 * the tree indicated one should be.
1939 */
1940 ocfs2_error(sb,
1941 "Invalid extent tree at extent block %llu\n",
1942 (unsigned long long)blkno);
1943 ret = -EROFS;
1944 goto out;
1945
1946next_node:
1947 blkno = path->p_node[i].bh->b_blocknr;
1948 i--;
1949 }
1950
1951out:
1952 return ret;
1953}
1954
Mark Fasheh328d5752007-06-18 10:48:04 -07001955/*
1956 * Extend the transaction by enough credits to complete the rotation,
1957 * and still leave at least the original number of credits allocated
1958 * to this transaction.
1959 */
Mark Fashehdcd05382007-01-16 11:32:23 -08001960static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07001961 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08001962 struct ocfs2_path *path)
1963{
Mark Fasheh328d5752007-06-18 10:48:04 -07001964 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08001965
1966 if (handle->h_buffer_credits < credits)
1967 return ocfs2_extend_trans(handle, credits);
1968
1969 return 0;
1970}
1971
1972/*
1973 * Trap the case where we're inserting into the theoretical range past
1974 * the _actual_ left leaf range. Otherwise, we'll rotate a record
1975 * whose cpos is less than ours into the right leaf.
1976 *
1977 * It's only necessary to look at the rightmost record of the left
1978 * leaf because the logic that calls us should ensure that the
1979 * theoretical ranges in the path components above the leaves are
1980 * correct.
1981 */
1982static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
1983 u32 insert_cpos)
1984{
1985 struct ocfs2_extent_list *left_el;
1986 struct ocfs2_extent_rec *rec;
1987 int next_free;
1988
1989 left_el = path_leaf_el(left_path);
1990 next_free = le16_to_cpu(left_el->l_next_free_rec);
1991 rec = &left_el->l_recs[next_free - 1];
1992
1993 if (insert_cpos > le32_to_cpu(rec->e_cpos))
1994 return 1;
1995 return 0;
1996}
1997
Mark Fasheh328d5752007-06-18 10:48:04 -07001998static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
1999{
2000 int next_free = le16_to_cpu(el->l_next_free_rec);
2001 unsigned int range;
2002 struct ocfs2_extent_rec *rec;
2003
2004 if (next_free == 0)
2005 return 0;
2006
2007 rec = &el->l_recs[0];
2008 if (ocfs2_is_empty_extent(rec)) {
2009 /* Empty list. */
2010 if (next_free == 1)
2011 return 0;
2012 rec = &el->l_recs[1];
2013 }
2014
2015 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2016 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2017 return 1;
2018 return 0;
2019}
2020
Mark Fashehdcd05382007-01-16 11:32:23 -08002021/*
2022 * Rotate all the records in a btree right one record, starting at insert_cpos.
2023 *
2024 * The path to the rightmost leaf should be passed in.
2025 *
2026 * The array is assumed to be large enough to hold an entire path (tree depth).
2027 *
2028 * Upon succesful return from this function:
2029 *
2030 * - The 'right_path' array will contain a path to the leaf block
2031 * whose range contains e_cpos.
2032 * - That leaf block will have a single empty extent in list index 0.
2033 * - In the case that the rotation requires a post-insert update,
2034 * *ret_left_path will contain a valid path which can be passed to
2035 * ocfs2_insert_path().
2036 */
2037static int ocfs2_rotate_tree_right(struct inode *inode,
2038 handle_t *handle,
Mark Fasheh328d5752007-06-18 10:48:04 -07002039 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002040 u32 insert_cpos,
2041 struct ocfs2_path *right_path,
2042 struct ocfs2_path **ret_left_path)
2043{
Mark Fasheh328d5752007-06-18 10:48:04 -07002044 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002045 u32 cpos;
2046 struct ocfs2_path *left_path = NULL;
2047
2048 *ret_left_path = NULL;
2049
2050 left_path = ocfs2_new_path(path_root_bh(right_path),
2051 path_root_el(right_path));
2052 if (!left_path) {
2053 ret = -ENOMEM;
2054 mlog_errno(ret);
2055 goto out;
2056 }
2057
2058 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path, &cpos);
2059 if (ret) {
2060 mlog_errno(ret);
2061 goto out;
2062 }
2063
2064 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2065
2066 /*
2067 * What we want to do here is:
2068 *
2069 * 1) Start with the rightmost path.
2070 *
2071 * 2) Determine a path to the leaf block directly to the left
2072 * of that leaf.
2073 *
2074 * 3) Determine the 'subtree root' - the lowest level tree node
2075 * which contains a path to both leaves.
2076 *
2077 * 4) Rotate the subtree.
2078 *
2079 * 5) Find the next subtree by considering the left path to be
2080 * the new right path.
2081 *
2082 * The check at the top of this while loop also accepts
2083 * insert_cpos == cpos because cpos is only a _theoretical_
2084 * value to get us the left path - insert_cpos might very well
2085 * be filling that hole.
2086 *
2087 * Stop at a cpos of '0' because we either started at the
2088 * leftmost branch (i.e., a tree with one branch and a
2089 * rotation inside of it), or we've gone as far as we can in
2090 * rotating subtrees.
2091 */
2092 while (cpos && insert_cpos <= cpos) {
2093 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2094 insert_cpos, cpos);
2095
2096 ret = ocfs2_find_path(inode, left_path, cpos);
2097 if (ret) {
2098 mlog_errno(ret);
2099 goto out;
2100 }
2101
2102 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2103 path_leaf_bh(right_path),
2104 "Inode %lu: error during insert of %u "
2105 "(left path cpos %u) results in two identical "
2106 "paths ending at %llu\n",
2107 inode->i_ino, insert_cpos, cpos,
2108 (unsigned long long)
2109 path_leaf_bh(left_path)->b_blocknr);
2110
Mark Fasheh328d5752007-06-18 10:48:04 -07002111 if (split == SPLIT_NONE &&
2112 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002113 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002114
2115 /*
2116 * We've rotated the tree as much as we
2117 * should. The rest is up to
2118 * ocfs2_insert_path() to complete, after the
2119 * record insertion. We indicate this
2120 * situation by returning the left path.
2121 *
2122 * The reason we don't adjust the records here
2123 * before the record insert is that an error
2124 * later might break the rule where a parent
2125 * record e_cpos will reflect the actual
2126 * e_cpos of the 1st nonempty record of the
2127 * child list.
2128 */
2129 *ret_left_path = left_path;
2130 goto out_ret_path;
2131 }
2132
2133 start = ocfs2_find_subtree_root(inode, left_path, right_path);
2134
2135 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2136 start,
2137 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2138 right_path->p_tree_depth);
2139
2140 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002141 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002142 if (ret) {
2143 mlog_errno(ret);
2144 goto out;
2145 }
2146
2147 ret = ocfs2_rotate_subtree_right(inode, handle, left_path,
2148 right_path, start);
2149 if (ret) {
2150 mlog_errno(ret);
2151 goto out;
2152 }
2153
Mark Fasheh328d5752007-06-18 10:48:04 -07002154 if (split != SPLIT_NONE &&
2155 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2156 insert_cpos)) {
2157 /*
2158 * A rotate moves the rightmost left leaf
2159 * record over to the leftmost right leaf
2160 * slot. If we're doing an extent split
2161 * instead of a real insert, then we have to
2162 * check that the extent to be split wasn't
2163 * just moved over. If it was, then we can
2164 * exit here, passing left_path back -
2165 * ocfs2_split_extent() is smart enough to
2166 * search both leaves.
2167 */
2168 *ret_left_path = left_path;
2169 goto out_ret_path;
2170 }
2171
Mark Fashehdcd05382007-01-16 11:32:23 -08002172 /*
2173 * There is no need to re-read the next right path
2174 * as we know that it'll be our current left
2175 * path. Optimize by copying values instead.
2176 */
2177 ocfs2_mv_path(right_path, left_path);
2178
2179 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
2180 &cpos);
2181 if (ret) {
2182 mlog_errno(ret);
2183 goto out;
2184 }
2185 }
2186
2187out:
2188 ocfs2_free_path(left_path);
2189
2190out_ret_path:
2191 return ret;
2192}
2193
Mark Fasheh328d5752007-06-18 10:48:04 -07002194static void ocfs2_update_edge_lengths(struct inode *inode, handle_t *handle,
2195 struct ocfs2_path *path)
2196{
2197 int i, idx;
2198 struct ocfs2_extent_rec *rec;
2199 struct ocfs2_extent_list *el;
2200 struct ocfs2_extent_block *eb;
2201 u32 range;
2202
2203 /* Path should always be rightmost. */
2204 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2205 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2206
2207 el = &eb->h_list;
2208 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2209 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2210 rec = &el->l_recs[idx];
2211 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2212
2213 for (i = 0; i < path->p_tree_depth; i++) {
2214 el = path->p_node[i].el;
2215 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2216 rec = &el->l_recs[idx];
2217
2218 rec->e_int_clusters = cpu_to_le32(range);
2219 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2220
2221 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2222 }
2223}
2224
2225static void ocfs2_unlink_path(struct inode *inode, handle_t *handle,
2226 struct ocfs2_cached_dealloc_ctxt *dealloc,
2227 struct ocfs2_path *path, int unlink_start)
2228{
2229 int ret, i;
2230 struct ocfs2_extent_block *eb;
2231 struct ocfs2_extent_list *el;
2232 struct buffer_head *bh;
2233
2234 for(i = unlink_start; i < path_num_items(path); i++) {
2235 bh = path->p_node[i].bh;
2236
2237 eb = (struct ocfs2_extent_block *)bh->b_data;
2238 /*
2239 * Not all nodes might have had their final count
2240 * decremented by the caller - handle this here.
2241 */
2242 el = &eb->h_list;
2243 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2244 mlog(ML_ERROR,
2245 "Inode %llu, attempted to remove extent block "
2246 "%llu with %u records\n",
2247 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2248 (unsigned long long)le64_to_cpu(eb->h_blkno),
2249 le16_to_cpu(el->l_next_free_rec));
2250
2251 ocfs2_journal_dirty(handle, bh);
2252 ocfs2_remove_from_cache(inode, bh);
2253 continue;
2254 }
2255
2256 el->l_next_free_rec = 0;
2257 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2258
2259 ocfs2_journal_dirty(handle, bh);
2260
2261 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2262 if (ret)
2263 mlog_errno(ret);
2264
2265 ocfs2_remove_from_cache(inode, bh);
2266 }
2267}
2268
2269static void ocfs2_unlink_subtree(struct inode *inode, handle_t *handle,
2270 struct ocfs2_path *left_path,
2271 struct ocfs2_path *right_path,
2272 int subtree_index,
2273 struct ocfs2_cached_dealloc_ctxt *dealloc)
2274{
2275 int i;
2276 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2277 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2278 struct ocfs2_extent_list *el;
2279 struct ocfs2_extent_block *eb;
2280
2281 el = path_leaf_el(left_path);
2282
2283 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2284
2285 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2286 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2287 break;
2288
2289 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2290
2291 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2292 le16_add_cpu(&root_el->l_next_free_rec, -1);
2293
2294 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2295 eb->h_next_leaf_blk = 0;
2296
2297 ocfs2_journal_dirty(handle, root_bh);
2298 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2299
2300 ocfs2_unlink_path(inode, handle, dealloc, right_path,
2301 subtree_index + 1);
2302}
2303
2304static int ocfs2_rotate_subtree_left(struct inode *inode, handle_t *handle,
2305 struct ocfs2_path *left_path,
2306 struct ocfs2_path *right_path,
2307 int subtree_index,
2308 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002309 int *deleted,
2310 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002311{
2312 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002313 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002314 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2315 struct ocfs2_extent_block *eb;
2316
2317 *deleted = 0;
2318
2319 right_leaf_el = path_leaf_el(right_path);
2320 left_leaf_el = path_leaf_el(left_path);
2321 root_bh = left_path->p_node[subtree_index].bh;
2322 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2323
2324 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2325 return 0;
2326
2327 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2328 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2329 /*
2330 * It's legal for us to proceed if the right leaf is
2331 * the rightmost one and it has an empty extent. There
2332 * are two cases to handle - whether the leaf will be
2333 * empty after removal or not. If the leaf isn't empty
2334 * then just remove the empty extent up front. The
2335 * next block will handle empty leaves by flagging
2336 * them for unlink.
2337 *
2338 * Non rightmost leaves will throw -EAGAIN and the
2339 * caller can manually move the subtree and retry.
2340 */
2341
2342 if (eb->h_next_leaf_blk != 0ULL)
2343 return -EAGAIN;
2344
2345 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
2346 ret = ocfs2_journal_access(handle, inode,
2347 path_leaf_bh(right_path),
2348 OCFS2_JOURNAL_ACCESS_WRITE);
2349 if (ret) {
2350 mlog_errno(ret);
2351 goto out;
2352 }
2353
2354 ocfs2_remove_empty_extent(right_leaf_el);
2355 } else
2356 right_has_empty = 1;
2357 }
2358
2359 if (eb->h_next_leaf_blk == 0ULL &&
2360 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2361 /*
2362 * We have to update i_last_eb_blk during the meta
2363 * data delete.
2364 */
Tao Mae7d4cb62008-08-18 17:38:44 +08002365 ret = ocfs2_journal_access(handle, inode, et_root_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07002366 OCFS2_JOURNAL_ACCESS_WRITE);
2367 if (ret) {
2368 mlog_errno(ret);
2369 goto out;
2370 }
2371
2372 del_right_subtree = 1;
2373 }
2374
2375 /*
2376 * Getting here with an empty extent in the right path implies
2377 * that it's the rightmost path and will be deleted.
2378 */
2379 BUG_ON(right_has_empty && !del_right_subtree);
2380
2381 ret = ocfs2_journal_access(handle, inode, root_bh,
2382 OCFS2_JOURNAL_ACCESS_WRITE);
2383 if (ret) {
2384 mlog_errno(ret);
2385 goto out;
2386 }
2387
2388 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
2389 ret = ocfs2_journal_access(handle, inode,
2390 right_path->p_node[i].bh,
2391 OCFS2_JOURNAL_ACCESS_WRITE);
2392 if (ret) {
2393 mlog_errno(ret);
2394 goto out;
2395 }
2396
2397 ret = ocfs2_journal_access(handle, inode,
2398 left_path->p_node[i].bh,
2399 OCFS2_JOURNAL_ACCESS_WRITE);
2400 if (ret) {
2401 mlog_errno(ret);
2402 goto out;
2403 }
2404 }
2405
2406 if (!right_has_empty) {
2407 /*
2408 * Only do this if we're moving a real
2409 * record. Otherwise, the action is delayed until
2410 * after removal of the right path in which case we
2411 * can do a simple shift to remove the empty extent.
2412 */
2413 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2414 memset(&right_leaf_el->l_recs[0], 0,
2415 sizeof(struct ocfs2_extent_rec));
2416 }
2417 if (eb->h_next_leaf_blk == 0ULL) {
2418 /*
2419 * Move recs over to get rid of empty extent, decrease
2420 * next_free. This is allowed to remove the last
2421 * extent in our leaf (setting l_next_free_rec to
2422 * zero) - the delete code below won't care.
2423 */
2424 ocfs2_remove_empty_extent(right_leaf_el);
2425 }
2426
2427 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2428 if (ret)
2429 mlog_errno(ret);
2430 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2431 if (ret)
2432 mlog_errno(ret);
2433
2434 if (del_right_subtree) {
2435 ocfs2_unlink_subtree(inode, handle, left_path, right_path,
2436 subtree_index, dealloc);
2437 ocfs2_update_edge_lengths(inode, handle, left_path);
2438
2439 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002440 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002441
2442 /*
2443 * Removal of the extent in the left leaf was skipped
2444 * above so we could delete the right path
2445 * 1st.
2446 */
2447 if (right_has_empty)
2448 ocfs2_remove_empty_extent(left_leaf_el);
2449
Tao Mae7d4cb62008-08-18 17:38:44 +08002450 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002451 if (ret)
2452 mlog_errno(ret);
2453
2454 *deleted = 1;
2455 } else
2456 ocfs2_complete_edge_insert(inode, handle, left_path, right_path,
2457 subtree_index);
2458
2459out:
2460 return ret;
2461}
2462
2463/*
2464 * Given a full path, determine what cpos value would return us a path
2465 * containing the leaf immediately to the right of the current one.
2466 *
2467 * Will return zero if the path passed in is already the rightmost path.
2468 *
2469 * This looks similar, but is subtly different to
2470 * ocfs2_find_cpos_for_left_leaf().
2471 */
2472static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2473 struct ocfs2_path *path, u32 *cpos)
2474{
2475 int i, j, ret = 0;
2476 u64 blkno;
2477 struct ocfs2_extent_list *el;
2478
2479 *cpos = 0;
2480
2481 if (path->p_tree_depth == 0)
2482 return 0;
2483
2484 blkno = path_leaf_bh(path)->b_blocknr;
2485
2486 /* Start at the tree node just above the leaf and work our way up. */
2487 i = path->p_tree_depth - 1;
2488 while (i >= 0) {
2489 int next_free;
2490
2491 el = path->p_node[i].el;
2492
2493 /*
2494 * Find the extent record just after the one in our
2495 * path.
2496 */
2497 next_free = le16_to_cpu(el->l_next_free_rec);
2498 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2499 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2500 if (j == (next_free - 1)) {
2501 if (i == 0) {
2502 /*
2503 * We've determined that the
2504 * path specified is already
2505 * the rightmost one - return a
2506 * cpos of zero.
2507 */
2508 goto out;
2509 }
2510 /*
2511 * The rightmost record points to our
2512 * leaf - we need to travel up the
2513 * tree one level.
2514 */
2515 goto next_node;
2516 }
2517
2518 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2519 goto out;
2520 }
2521 }
2522
2523 /*
2524 * If we got here, we never found a valid node where
2525 * the tree indicated one should be.
2526 */
2527 ocfs2_error(sb,
2528 "Invalid extent tree at extent block %llu\n",
2529 (unsigned long long)blkno);
2530 ret = -EROFS;
2531 goto out;
2532
2533next_node:
2534 blkno = path->p_node[i].bh->b_blocknr;
2535 i--;
2536 }
2537
2538out:
2539 return ret;
2540}
2541
2542static int ocfs2_rotate_rightmost_leaf_left(struct inode *inode,
2543 handle_t *handle,
2544 struct buffer_head *bh,
2545 struct ocfs2_extent_list *el)
2546{
2547 int ret;
2548
2549 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2550 return 0;
2551
2552 ret = ocfs2_journal_access(handle, inode, bh,
2553 OCFS2_JOURNAL_ACCESS_WRITE);
2554 if (ret) {
2555 mlog_errno(ret);
2556 goto out;
2557 }
2558
2559 ocfs2_remove_empty_extent(el);
2560
2561 ret = ocfs2_journal_dirty(handle, bh);
2562 if (ret)
2563 mlog_errno(ret);
2564
2565out:
2566 return ret;
2567}
2568
2569static int __ocfs2_rotate_tree_left(struct inode *inode,
2570 handle_t *handle, int orig_credits,
2571 struct ocfs2_path *path,
2572 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002573 struct ocfs2_path **empty_extent_path,
2574 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002575{
2576 int ret, subtree_root, deleted;
2577 u32 right_cpos;
2578 struct ocfs2_path *left_path = NULL;
2579 struct ocfs2_path *right_path = NULL;
2580
2581 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2582
2583 *empty_extent_path = NULL;
2584
2585 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, path,
2586 &right_cpos);
2587 if (ret) {
2588 mlog_errno(ret);
2589 goto out;
2590 }
2591
2592 left_path = ocfs2_new_path(path_root_bh(path),
2593 path_root_el(path));
2594 if (!left_path) {
2595 ret = -ENOMEM;
2596 mlog_errno(ret);
2597 goto out;
2598 }
2599
2600 ocfs2_cp_path(left_path, path);
2601
2602 right_path = ocfs2_new_path(path_root_bh(path),
2603 path_root_el(path));
2604 if (!right_path) {
2605 ret = -ENOMEM;
2606 mlog_errno(ret);
2607 goto out;
2608 }
2609
2610 while (right_cpos) {
2611 ret = ocfs2_find_path(inode, right_path, right_cpos);
2612 if (ret) {
2613 mlog_errno(ret);
2614 goto out;
2615 }
2616
2617 subtree_root = ocfs2_find_subtree_root(inode, left_path,
2618 right_path);
2619
2620 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2621 subtree_root,
2622 (unsigned long long)
2623 right_path->p_node[subtree_root].bh->b_blocknr,
2624 right_path->p_tree_depth);
2625
2626 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2627 orig_credits, left_path);
2628 if (ret) {
2629 mlog_errno(ret);
2630 goto out;
2631 }
2632
Mark Fashehe8aed342007-12-03 16:43:01 -08002633 /*
2634 * Caller might still want to make changes to the
2635 * tree root, so re-add it to the journal here.
2636 */
2637 ret = ocfs2_journal_access(handle, inode,
2638 path_root_bh(left_path),
2639 OCFS2_JOURNAL_ACCESS_WRITE);
2640 if (ret) {
2641 mlog_errno(ret);
2642 goto out;
2643 }
2644
Mark Fasheh328d5752007-06-18 10:48:04 -07002645 ret = ocfs2_rotate_subtree_left(inode, handle, left_path,
2646 right_path, subtree_root,
Tao Mae7d4cb62008-08-18 17:38:44 +08002647 dealloc, &deleted, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002648 if (ret == -EAGAIN) {
2649 /*
2650 * The rotation has to temporarily stop due to
2651 * the right subtree having an empty
2652 * extent. Pass it back to the caller for a
2653 * fixup.
2654 */
2655 *empty_extent_path = right_path;
2656 right_path = NULL;
2657 goto out;
2658 }
2659 if (ret) {
2660 mlog_errno(ret);
2661 goto out;
2662 }
2663
2664 /*
2665 * The subtree rotate might have removed records on
2666 * the rightmost edge. If so, then rotation is
2667 * complete.
2668 */
2669 if (deleted)
2670 break;
2671
2672 ocfs2_mv_path(left_path, right_path);
2673
2674 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2675 &right_cpos);
2676 if (ret) {
2677 mlog_errno(ret);
2678 goto out;
2679 }
2680 }
2681
2682out:
2683 ocfs2_free_path(right_path);
2684 ocfs2_free_path(left_path);
2685
2686 return ret;
2687}
2688
2689static int ocfs2_remove_rightmost_path(struct inode *inode, handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08002690 struct ocfs2_path *path,
2691 struct ocfs2_cached_dealloc_ctxt *dealloc,
2692 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002693{
2694 int ret, subtree_index;
2695 u32 cpos;
2696 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07002697 struct ocfs2_extent_block *eb;
2698 struct ocfs2_extent_list *el;
2699
Mark Fasheh328d5752007-06-18 10:48:04 -07002700
Joel Becker35dc0aa2008-08-20 16:25:06 -07002701 ret = ocfs2_et_sanity_check(inode, et);
Tao Mae7d4cb62008-08-18 17:38:44 +08002702 if (ret)
2703 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07002704 /*
2705 * There's two ways we handle this depending on
2706 * whether path is the only existing one.
2707 */
2708 ret = ocfs2_extend_rotate_transaction(handle, 0,
2709 handle->h_buffer_credits,
2710 path);
2711 if (ret) {
2712 mlog_errno(ret);
2713 goto out;
2714 }
2715
2716 ret = ocfs2_journal_access_path(inode, handle, path);
2717 if (ret) {
2718 mlog_errno(ret);
2719 goto out;
2720 }
2721
2722 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
2723 if (ret) {
2724 mlog_errno(ret);
2725 goto out;
2726 }
2727
2728 if (cpos) {
2729 /*
2730 * We have a path to the left of this one - it needs
2731 * an update too.
2732 */
2733 left_path = ocfs2_new_path(path_root_bh(path),
2734 path_root_el(path));
2735 if (!left_path) {
2736 ret = -ENOMEM;
2737 mlog_errno(ret);
2738 goto out;
2739 }
2740
2741 ret = ocfs2_find_path(inode, left_path, cpos);
2742 if (ret) {
2743 mlog_errno(ret);
2744 goto out;
2745 }
2746
2747 ret = ocfs2_journal_access_path(inode, handle, left_path);
2748 if (ret) {
2749 mlog_errno(ret);
2750 goto out;
2751 }
2752
2753 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
2754
2755 ocfs2_unlink_subtree(inode, handle, left_path, path,
2756 subtree_index, dealloc);
2757 ocfs2_update_edge_lengths(inode, handle, left_path);
2758
2759 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002760 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002761 } else {
2762 /*
2763 * 'path' is also the leftmost path which
2764 * means it must be the only one. This gets
2765 * handled differently because we want to
2766 * revert the inode back to having extents
2767 * in-line.
2768 */
2769 ocfs2_unlink_path(inode, handle, dealloc, path, 1);
2770
Joel Beckerce1d9ea2008-08-20 16:30:07 -07002771 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07002772 el->l_tree_depth = 0;
2773 el->l_next_free_rec = 0;
2774 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2775
Joel Becker35dc0aa2008-08-20 16:25:06 -07002776 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07002777 }
2778
2779 ocfs2_journal_dirty(handle, path_root_bh(path));
2780
2781out:
2782 ocfs2_free_path(left_path);
2783 return ret;
2784}
2785
2786/*
2787 * Left rotation of btree records.
2788 *
2789 * In many ways, this is (unsurprisingly) the opposite of right
2790 * rotation. We start at some non-rightmost path containing an empty
2791 * extent in the leaf block. The code works its way to the rightmost
2792 * path by rotating records to the left in every subtree.
2793 *
2794 * This is used by any code which reduces the number of extent records
2795 * in a leaf. After removal, an empty record should be placed in the
2796 * leftmost list position.
2797 *
2798 * This won't handle a length update of the rightmost path records if
2799 * the rightmost tree leaf record is removed so the caller is
2800 * responsible for detecting and correcting that.
2801 */
2802static int ocfs2_rotate_tree_left(struct inode *inode, handle_t *handle,
2803 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002804 struct ocfs2_cached_dealloc_ctxt *dealloc,
2805 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07002806{
2807 int ret, orig_credits = handle->h_buffer_credits;
2808 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
2809 struct ocfs2_extent_block *eb;
2810 struct ocfs2_extent_list *el;
2811
2812 el = path_leaf_el(path);
2813 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2814 return 0;
2815
2816 if (path->p_tree_depth == 0) {
2817rightmost_no_delete:
2818 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08002819 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07002820 * it up front.
2821 */
2822 ret = ocfs2_rotate_rightmost_leaf_left(inode, handle,
2823 path_leaf_bh(path),
2824 path_leaf_el(path));
2825 if (ret)
2826 mlog_errno(ret);
2827 goto out;
2828 }
2829
2830 /*
2831 * Handle rightmost branch now. There's several cases:
2832 * 1) simple rotation leaving records in there. That's trivial.
2833 * 2) rotation requiring a branch delete - there's no more
2834 * records left. Two cases of this:
2835 * a) There are branches to the left.
2836 * b) This is also the leftmost (the only) branch.
2837 *
2838 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
2839 * 2a) we need the left branch so that we can update it with the unlink
2840 * 2b) we need to bring the inode back to inline extents.
2841 */
2842
2843 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2844 el = &eb->h_list;
2845 if (eb->h_next_leaf_blk == 0) {
2846 /*
2847 * This gets a bit tricky if we're going to delete the
2848 * rightmost path. Get the other cases out of the way
2849 * 1st.
2850 */
2851 if (le16_to_cpu(el->l_next_free_rec) > 1)
2852 goto rightmost_no_delete;
2853
2854 if (le16_to_cpu(el->l_next_free_rec) == 0) {
2855 ret = -EIO;
2856 ocfs2_error(inode->i_sb,
2857 "Inode %llu has empty extent block at %llu",
2858 (unsigned long long)OCFS2_I(inode)->ip_blkno,
2859 (unsigned long long)le64_to_cpu(eb->h_blkno));
2860 goto out;
2861 }
2862
2863 /*
2864 * XXX: The caller can not trust "path" any more after
2865 * this as it will have been deleted. What do we do?
2866 *
2867 * In theory the rotate-for-merge code will never get
2868 * here because it'll always ask for a rotate in a
2869 * nonempty list.
2870 */
2871
2872 ret = ocfs2_remove_rightmost_path(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002873 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002874 if (ret)
2875 mlog_errno(ret);
2876 goto out;
2877 }
2878
2879 /*
2880 * Now we can loop, remembering the path we get from -EAGAIN
2881 * and restarting from there.
2882 */
2883try_rotate:
2884 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08002885 dealloc, &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002886 if (ret && ret != -EAGAIN) {
2887 mlog_errno(ret);
2888 goto out;
2889 }
2890
2891 while (ret == -EAGAIN) {
2892 tmp_path = restart_path;
2893 restart_path = NULL;
2894
2895 ret = __ocfs2_rotate_tree_left(inode, handle, orig_credits,
2896 tmp_path, dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08002897 &restart_path, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07002898 if (ret && ret != -EAGAIN) {
2899 mlog_errno(ret);
2900 goto out;
2901 }
2902
2903 ocfs2_free_path(tmp_path);
2904 tmp_path = NULL;
2905
2906 if (ret == 0)
2907 goto try_rotate;
2908 }
2909
2910out:
2911 ocfs2_free_path(tmp_path);
2912 ocfs2_free_path(restart_path);
2913 return ret;
2914}
2915
2916static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
2917 int index)
2918{
2919 struct ocfs2_extent_rec *rec = &el->l_recs[index];
2920 unsigned int size;
2921
2922 if (rec->e_leaf_clusters == 0) {
2923 /*
2924 * We consumed all of the merged-from record. An empty
2925 * extent cannot exist anywhere but the 1st array
2926 * position, so move things over if the merged-from
2927 * record doesn't occupy that position.
2928 *
2929 * This creates a new empty extent so the caller
2930 * should be smart enough to have removed any existing
2931 * ones.
2932 */
2933 if (index > 0) {
2934 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
2935 size = index * sizeof(struct ocfs2_extent_rec);
2936 memmove(&el->l_recs[1], &el->l_recs[0], size);
2937 }
2938
2939 /*
2940 * Always memset - the caller doesn't check whether it
2941 * created an empty extent, so there could be junk in
2942 * the other fields.
2943 */
2944 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2945 }
2946}
2947
Tao Ma677b9752008-01-30 14:21:05 +08002948static int ocfs2_get_right_path(struct inode *inode,
2949 struct ocfs2_path *left_path,
2950 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002951{
2952 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08002953 u32 right_cpos;
2954 struct ocfs2_path *right_path = NULL;
2955 struct ocfs2_extent_list *left_el;
2956
2957 *ret_right_path = NULL;
2958
2959 /* This function shouldn't be called for non-trees. */
2960 BUG_ON(left_path->p_tree_depth == 0);
2961
2962 left_el = path_leaf_el(left_path);
2963 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
2964
2965 ret = ocfs2_find_cpos_for_right_leaf(inode->i_sb, left_path,
2966 &right_cpos);
2967 if (ret) {
2968 mlog_errno(ret);
2969 goto out;
2970 }
2971
2972 /* This function shouldn't be called for the rightmost leaf. */
2973 BUG_ON(right_cpos == 0);
2974
2975 right_path = ocfs2_new_path(path_root_bh(left_path),
2976 path_root_el(left_path));
2977 if (!right_path) {
2978 ret = -ENOMEM;
2979 mlog_errno(ret);
2980 goto out;
2981 }
2982
2983 ret = ocfs2_find_path(inode, right_path, right_cpos);
2984 if (ret) {
2985 mlog_errno(ret);
2986 goto out;
2987 }
2988
2989 *ret_right_path = right_path;
2990out:
2991 if (ret)
2992 ocfs2_free_path(right_path);
2993 return ret;
2994}
2995
2996/*
2997 * Remove split_rec clusters from the record at index and merge them
2998 * onto the beginning of the record "next" to it.
2999 * For index < l_count - 1, the next means the extent rec at index + 1.
3000 * For index == l_count - 1, the "next" means the 1st extent rec of the
3001 * next extent block.
3002 */
3003static int ocfs2_merge_rec_right(struct inode *inode,
3004 struct ocfs2_path *left_path,
3005 handle_t *handle,
3006 struct ocfs2_extent_rec *split_rec,
3007 int index)
3008{
3009 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003010 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3011 struct ocfs2_extent_rec *left_rec;
3012 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003013 struct ocfs2_extent_list *right_el;
3014 struct ocfs2_path *right_path = NULL;
3015 int subtree_index = 0;
3016 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3017 struct buffer_head *bh = path_leaf_bh(left_path);
3018 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003019
3020 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003021 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003022
Al Viro9d8df6a2008-05-21 06:32:11 +01003023 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003024 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3025 /* we meet with a cross extent block merge. */
3026 ret = ocfs2_get_right_path(inode, left_path, &right_path);
3027 if (ret) {
3028 mlog_errno(ret);
3029 goto out;
3030 }
3031
3032 right_el = path_leaf_el(right_path);
3033 next_free = le16_to_cpu(right_el->l_next_free_rec);
3034 BUG_ON(next_free <= 0);
3035 right_rec = &right_el->l_recs[0];
3036 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003037 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003038 right_rec = &right_el->l_recs[1];
3039 }
3040
3041 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3042 le16_to_cpu(left_rec->e_leaf_clusters) !=
3043 le32_to_cpu(right_rec->e_cpos));
3044
3045 subtree_index = ocfs2_find_subtree_root(inode,
3046 left_path, right_path);
3047
3048 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3049 handle->h_buffer_credits,
3050 right_path);
3051 if (ret) {
3052 mlog_errno(ret);
3053 goto out;
3054 }
3055
3056 root_bh = left_path->p_node[subtree_index].bh;
3057 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3058
3059 ret = ocfs2_journal_access(handle, inode, root_bh,
3060 OCFS2_JOURNAL_ACCESS_WRITE);
3061 if (ret) {
3062 mlog_errno(ret);
3063 goto out;
3064 }
3065
3066 for (i = subtree_index + 1;
3067 i < path_num_items(right_path); i++) {
3068 ret = ocfs2_journal_access(handle, inode,
3069 right_path->p_node[i].bh,
3070 OCFS2_JOURNAL_ACCESS_WRITE);
3071 if (ret) {
3072 mlog_errno(ret);
3073 goto out;
3074 }
3075
3076 ret = ocfs2_journal_access(handle, inode,
3077 left_path->p_node[i].bh,
3078 OCFS2_JOURNAL_ACCESS_WRITE);
3079 if (ret) {
3080 mlog_errno(ret);
3081 goto out;
3082 }
3083 }
3084
3085 } else {
3086 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3087 right_rec = &el->l_recs[index + 1];
3088 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003089
3090 ret = ocfs2_journal_access(handle, inode, bh,
3091 OCFS2_JOURNAL_ACCESS_WRITE);
3092 if (ret) {
3093 mlog_errno(ret);
3094 goto out;
3095 }
3096
3097 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3098
3099 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3100 le64_add_cpu(&right_rec->e_blkno,
3101 -ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3102 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3103
3104 ocfs2_cleanup_merge(el, index);
3105
3106 ret = ocfs2_journal_dirty(handle, bh);
3107 if (ret)
3108 mlog_errno(ret);
3109
Tao Ma677b9752008-01-30 14:21:05 +08003110 if (right_path) {
3111 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3112 if (ret)
3113 mlog_errno(ret);
3114
3115 ocfs2_complete_edge_insert(inode, handle, left_path,
3116 right_path, subtree_index);
3117 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003118out:
Tao Ma677b9752008-01-30 14:21:05 +08003119 if (right_path)
3120 ocfs2_free_path(right_path);
3121 return ret;
3122}
3123
3124static int ocfs2_get_left_path(struct inode *inode,
3125 struct ocfs2_path *right_path,
3126 struct ocfs2_path **ret_left_path)
3127{
3128 int ret;
3129 u32 left_cpos;
3130 struct ocfs2_path *left_path = NULL;
3131
3132 *ret_left_path = NULL;
3133
3134 /* This function shouldn't be called for non-trees. */
3135 BUG_ON(right_path->p_tree_depth == 0);
3136
3137 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
3138 right_path, &left_cpos);
3139 if (ret) {
3140 mlog_errno(ret);
3141 goto out;
3142 }
3143
3144 /* This function shouldn't be called for the leftmost leaf. */
3145 BUG_ON(left_cpos == 0);
3146
3147 left_path = ocfs2_new_path(path_root_bh(right_path),
3148 path_root_el(right_path));
3149 if (!left_path) {
3150 ret = -ENOMEM;
3151 mlog_errno(ret);
3152 goto out;
3153 }
3154
3155 ret = ocfs2_find_path(inode, left_path, left_cpos);
3156 if (ret) {
3157 mlog_errno(ret);
3158 goto out;
3159 }
3160
3161 *ret_left_path = left_path;
3162out:
3163 if (ret)
3164 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003165 return ret;
3166}
3167
3168/*
3169 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003170 * onto the tail of the record "before" it.
3171 * For index > 0, the "before" means the extent rec at index - 1.
3172 *
3173 * For index == 0, the "before" means the last record of the previous
3174 * extent block. And there is also a situation that we may need to
3175 * remove the rightmost leaf extent block in the right_path and change
3176 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003177 */
Tao Ma677b9752008-01-30 14:21:05 +08003178static int ocfs2_merge_rec_left(struct inode *inode,
3179 struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003180 handle_t *handle,
3181 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003182 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003183 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003184 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003185{
Tao Ma677b9752008-01-30 14:21:05 +08003186 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003187 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3188 struct ocfs2_extent_rec *left_rec;
3189 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003190 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3191 struct buffer_head *bh = path_leaf_bh(right_path);
3192 struct buffer_head *root_bh = NULL;
3193 struct ocfs2_path *left_path = NULL;
3194 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003195
Tao Ma677b9752008-01-30 14:21:05 +08003196 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003197
Mark Fasheh328d5752007-06-18 10:48:04 -07003198 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003199 if (index == 0) {
3200 /* we meet with a cross extent block merge. */
3201 ret = ocfs2_get_left_path(inode, right_path, &left_path);
3202 if (ret) {
3203 mlog_errno(ret);
3204 goto out;
3205 }
3206
3207 left_el = path_leaf_el(left_path);
3208 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3209 le16_to_cpu(left_el->l_count));
3210
3211 left_rec = &left_el->l_recs[
3212 le16_to_cpu(left_el->l_next_free_rec) - 1];
3213 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3214 le16_to_cpu(left_rec->e_leaf_clusters) !=
3215 le32_to_cpu(split_rec->e_cpos));
3216
3217 subtree_index = ocfs2_find_subtree_root(inode,
3218 left_path, right_path);
3219
3220 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3221 handle->h_buffer_credits,
3222 left_path);
3223 if (ret) {
3224 mlog_errno(ret);
3225 goto out;
3226 }
3227
3228 root_bh = left_path->p_node[subtree_index].bh;
3229 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3230
3231 ret = ocfs2_journal_access(handle, inode, root_bh,
3232 OCFS2_JOURNAL_ACCESS_WRITE);
3233 if (ret) {
3234 mlog_errno(ret);
3235 goto out;
3236 }
3237
3238 for (i = subtree_index + 1;
3239 i < path_num_items(right_path); i++) {
3240 ret = ocfs2_journal_access(handle, inode,
3241 right_path->p_node[i].bh,
3242 OCFS2_JOURNAL_ACCESS_WRITE);
3243 if (ret) {
3244 mlog_errno(ret);
3245 goto out;
3246 }
3247
3248 ret = ocfs2_journal_access(handle, inode,
3249 left_path->p_node[i].bh,
3250 OCFS2_JOURNAL_ACCESS_WRITE);
3251 if (ret) {
3252 mlog_errno(ret);
3253 goto out;
3254 }
3255 }
3256 } else {
3257 left_rec = &el->l_recs[index - 1];
3258 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3259 has_empty_extent = 1;
3260 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003261
3262 ret = ocfs2_journal_access(handle, inode, bh,
3263 OCFS2_JOURNAL_ACCESS_WRITE);
3264 if (ret) {
3265 mlog_errno(ret);
3266 goto out;
3267 }
3268
3269 if (has_empty_extent && index == 1) {
3270 /*
3271 * The easy case - we can just plop the record right in.
3272 */
3273 *left_rec = *split_rec;
3274
3275 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003276 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003277 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003278
3279 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3280 le64_add_cpu(&right_rec->e_blkno,
3281 ocfs2_clusters_to_blocks(inode->i_sb, split_clusters));
3282 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3283
3284 ocfs2_cleanup_merge(el, index);
3285
3286 ret = ocfs2_journal_dirty(handle, bh);
3287 if (ret)
3288 mlog_errno(ret);
3289
Tao Ma677b9752008-01-30 14:21:05 +08003290 if (left_path) {
3291 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3292 if (ret)
3293 mlog_errno(ret);
3294
3295 /*
3296 * In the situation that the right_rec is empty and the extent
3297 * block is empty also, ocfs2_complete_edge_insert can't handle
3298 * it and we need to delete the right extent block.
3299 */
3300 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3301 le16_to_cpu(el->l_next_free_rec) == 1) {
3302
3303 ret = ocfs2_remove_rightmost_path(inode, handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003304 right_path,
3305 dealloc, et);
Tao Ma677b9752008-01-30 14:21:05 +08003306 if (ret) {
3307 mlog_errno(ret);
3308 goto out;
3309 }
3310
3311 /* Now the rightmost extent block has been deleted.
3312 * So we use the new rightmost path.
3313 */
3314 ocfs2_mv_path(right_path, left_path);
3315 left_path = NULL;
3316 } else
3317 ocfs2_complete_edge_insert(inode, handle, left_path,
3318 right_path, subtree_index);
3319 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003320out:
Tao Ma677b9752008-01-30 14:21:05 +08003321 if (left_path)
3322 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003323 return ret;
3324}
3325
3326static int ocfs2_try_to_merge_extent(struct inode *inode,
3327 handle_t *handle,
Tao Ma677b9752008-01-30 14:21:05 +08003328 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003329 int split_index,
3330 struct ocfs2_extent_rec *split_rec,
3331 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08003332 struct ocfs2_merge_ctxt *ctxt,
3333 struct ocfs2_extent_tree *et)
Mark Fasheh328d5752007-06-18 10:48:04 -07003334
3335{
Tao Mao518d7262007-08-28 17:25:35 -07003336 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003337 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003338 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3339
3340 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3341
Tao Mao518d7262007-08-28 17:25:35 -07003342 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3343 /*
3344 * The merge code will need to create an empty
3345 * extent to take the place of the newly
3346 * emptied slot. Remove any pre-existing empty
3347 * extents - having more than one in a leaf is
3348 * illegal.
3349 */
Tao Ma677b9752008-01-30 14:21:05 +08003350 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003351 dealloc, et);
Tao Mao518d7262007-08-28 17:25:35 -07003352 if (ret) {
3353 mlog_errno(ret);
3354 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003355 }
Tao Mao518d7262007-08-28 17:25:35 -07003356 split_index--;
3357 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003358 }
3359
3360 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3361 /*
3362 * Left-right contig implies this.
3363 */
3364 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003365
3366 /*
3367 * Since the leftright insert always covers the entire
3368 * extent, this call will delete the insert record
3369 * entirely, resulting in an empty extent record added to
3370 * the extent block.
3371 *
3372 * Since the adding of an empty extent shifts
3373 * everything back to the right, there's no need to
3374 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003375 *
3376 * When the split_index is zero, we need to merge it to the
3377 * prevoius extent block. It is more efficient and easier
3378 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003379 */
Tao Ma677b9752008-01-30 14:21:05 +08003380 ret = ocfs2_merge_rec_right(inode, path,
3381 handle, split_rec,
3382 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003383 if (ret) {
3384 mlog_errno(ret);
3385 goto out;
3386 }
3387
3388 /*
3389 * We can only get this from logic error above.
3390 */
3391 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3392
Tao Ma677b9752008-01-30 14:21:05 +08003393 /* The merge left us with an empty extent, remove it. */
Tao Mae7d4cb62008-08-18 17:38:44 +08003394 ret = ocfs2_rotate_tree_left(inode, handle, path,
3395 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003396 if (ret) {
3397 mlog_errno(ret);
3398 goto out;
3399 }
Tao Ma677b9752008-01-30 14:21:05 +08003400
Mark Fasheh328d5752007-06-18 10:48:04 -07003401 rec = &el->l_recs[split_index];
3402
3403 /*
3404 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003405 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003406 */
Tao Ma677b9752008-01-30 14:21:05 +08003407 ret = ocfs2_merge_rec_left(inode, path,
3408 handle, rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003409 dealloc, et,
Tao Ma677b9752008-01-30 14:21:05 +08003410 split_index);
3411
Mark Fasheh328d5752007-06-18 10:48:04 -07003412 if (ret) {
3413 mlog_errno(ret);
3414 goto out;
3415 }
3416
Tao Ma677b9752008-01-30 14:21:05 +08003417 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003418 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003419 /*
3420 * Error from this last rotate is not critical, so
3421 * print but don't bubble it up.
3422 */
3423 if (ret)
3424 mlog_errno(ret);
3425 ret = 0;
3426 } else {
3427 /*
3428 * Merge a record to the left or right.
3429 *
3430 * 'contig_type' is relative to the existing record,
3431 * so for example, if we're "right contig", it's to
3432 * the record on the left (hence the left merge).
3433 */
3434 if (ctxt->c_contig_type == CONTIG_RIGHT) {
3435 ret = ocfs2_merge_rec_left(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003436 path,
3437 handle, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08003438 dealloc, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003439 split_index);
3440 if (ret) {
3441 mlog_errno(ret);
3442 goto out;
3443 }
3444 } else {
3445 ret = ocfs2_merge_rec_right(inode,
Tao Ma677b9752008-01-30 14:21:05 +08003446 path,
3447 handle, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003448 split_index);
3449 if (ret) {
3450 mlog_errno(ret);
3451 goto out;
3452 }
3453 }
3454
3455 if (ctxt->c_split_covers_rec) {
3456 /*
3457 * The merge may have left an empty extent in
3458 * our leaf. Try to rotate it away.
3459 */
Tao Ma677b9752008-01-30 14:21:05 +08003460 ret = ocfs2_rotate_tree_left(inode, handle, path,
Tao Mae7d4cb62008-08-18 17:38:44 +08003461 dealloc, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07003462 if (ret)
3463 mlog_errno(ret);
3464 ret = 0;
3465 }
3466 }
3467
3468out:
3469 return ret;
3470}
3471
3472static void ocfs2_subtract_from_rec(struct super_block *sb,
3473 enum ocfs2_split_type split,
3474 struct ocfs2_extent_rec *rec,
3475 struct ocfs2_extent_rec *split_rec)
3476{
3477 u64 len_blocks;
3478
3479 len_blocks = ocfs2_clusters_to_blocks(sb,
3480 le16_to_cpu(split_rec->e_leaf_clusters));
3481
3482 if (split == SPLIT_LEFT) {
3483 /*
3484 * Region is on the left edge of the existing
3485 * record.
3486 */
3487 le32_add_cpu(&rec->e_cpos,
3488 le16_to_cpu(split_rec->e_leaf_clusters));
3489 le64_add_cpu(&rec->e_blkno, len_blocks);
3490 le16_add_cpu(&rec->e_leaf_clusters,
3491 -le16_to_cpu(split_rec->e_leaf_clusters));
3492 } else {
3493 /*
3494 * Region is on the right edge of the existing
3495 * record.
3496 */
3497 le16_add_cpu(&rec->e_leaf_clusters,
3498 -le16_to_cpu(split_rec->e_leaf_clusters));
3499 }
3500}
3501
Mark Fashehdcd05382007-01-16 11:32:23 -08003502/*
3503 * Do the final bits of extent record insertion at the target leaf
3504 * list. If this leaf is part of an allocation tree, it is assumed
3505 * that the tree above has been prepared.
3506 */
3507static void ocfs2_insert_at_leaf(struct ocfs2_extent_rec *insert_rec,
3508 struct ocfs2_extent_list *el,
3509 struct ocfs2_insert_type *insert,
3510 struct inode *inode)
3511{
3512 int i = insert->ins_contig_index;
3513 unsigned int range;
3514 struct ocfs2_extent_rec *rec;
3515
Mark Fashehe48edee2007-03-07 16:46:57 -08003516 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003517
Mark Fasheh328d5752007-06-18 10:48:04 -07003518 if (insert->ins_split != SPLIT_NONE) {
3519 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3520 BUG_ON(i == -1);
3521 rec = &el->l_recs[i];
3522 ocfs2_subtract_from_rec(inode->i_sb, insert->ins_split, rec,
3523 insert_rec);
3524 goto rotate;
3525 }
3526
Mark Fashehdcd05382007-01-16 11:32:23 -08003527 /*
3528 * Contiguous insert - either left or right.
3529 */
3530 if (insert->ins_contig != CONTIG_NONE) {
3531 rec = &el->l_recs[i];
3532 if (insert->ins_contig == CONTIG_LEFT) {
3533 rec->e_blkno = insert_rec->e_blkno;
3534 rec->e_cpos = insert_rec->e_cpos;
3535 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003536 le16_add_cpu(&rec->e_leaf_clusters,
3537 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003538 return;
3539 }
3540
3541 /*
3542 * Handle insert into an empty leaf.
3543 */
3544 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3545 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3546 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3547 el->l_recs[0] = *insert_rec;
3548 el->l_next_free_rec = cpu_to_le16(1);
3549 return;
3550 }
3551
3552 /*
3553 * Appending insert.
3554 */
3555 if (insert->ins_appending == APPEND_TAIL) {
3556 i = le16_to_cpu(el->l_next_free_rec) - 1;
3557 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003558 range = le32_to_cpu(rec->e_cpos)
3559 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003560 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3561
3562 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3563 le16_to_cpu(el->l_count),
3564 "inode %lu, depth %u, count %u, next free %u, "
3565 "rec.cpos %u, rec.clusters %u, "
3566 "insert.cpos %u, insert.clusters %u\n",
3567 inode->i_ino,
3568 le16_to_cpu(el->l_tree_depth),
3569 le16_to_cpu(el->l_count),
3570 le16_to_cpu(el->l_next_free_rec),
3571 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003572 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003573 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003574 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003575 i++;
3576 el->l_recs[i] = *insert_rec;
3577 le16_add_cpu(&el->l_next_free_rec, 1);
3578 return;
3579 }
3580
Mark Fasheh328d5752007-06-18 10:48:04 -07003581rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003582 /*
3583 * Ok, we have to rotate.
3584 *
3585 * At this point, it is safe to assume that inserting into an
3586 * empty leaf and appending to a leaf have both been handled
3587 * above.
3588 *
3589 * This leaf needs to have space, either by the empty 1st
3590 * extent record, or by virtue of an l_next_rec < l_count.
3591 */
3592 ocfs2_rotate_leaf(el, insert_rec);
3593}
3594
Mark Fasheh328d5752007-06-18 10:48:04 -07003595static void ocfs2_adjust_rightmost_records(struct inode *inode,
3596 handle_t *handle,
3597 struct ocfs2_path *path,
3598 struct ocfs2_extent_rec *insert_rec)
3599{
3600 int ret, i, next_free;
3601 struct buffer_head *bh;
3602 struct ocfs2_extent_list *el;
3603 struct ocfs2_extent_rec *rec;
3604
3605 /*
3606 * Update everything except the leaf block.
3607 */
3608 for (i = 0; i < path->p_tree_depth; i++) {
3609 bh = path->p_node[i].bh;
3610 el = path->p_node[i].el;
3611
3612 next_free = le16_to_cpu(el->l_next_free_rec);
3613 if (next_free == 0) {
3614 ocfs2_error(inode->i_sb,
3615 "Dinode %llu has a bad extent list",
3616 (unsigned long long)OCFS2_I(inode)->ip_blkno);
3617 ret = -EIO;
3618 return;
3619 }
3620
3621 rec = &el->l_recs[next_free - 1];
3622
3623 rec->e_int_clusters = insert_rec->e_cpos;
3624 le32_add_cpu(&rec->e_int_clusters,
3625 le16_to_cpu(insert_rec->e_leaf_clusters));
3626 le32_add_cpu(&rec->e_int_clusters,
3627 -le32_to_cpu(rec->e_cpos));
3628
3629 ret = ocfs2_journal_dirty(handle, bh);
3630 if (ret)
3631 mlog_errno(ret);
3632
3633 }
3634}
3635
Mark Fashehdcd05382007-01-16 11:32:23 -08003636static int ocfs2_append_rec_to_path(struct inode *inode, handle_t *handle,
3637 struct ocfs2_extent_rec *insert_rec,
3638 struct ocfs2_path *right_path,
3639 struct ocfs2_path **ret_left_path)
3640{
Mark Fasheh328d5752007-06-18 10:48:04 -07003641 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003642 struct ocfs2_extent_list *el;
3643 struct ocfs2_path *left_path = NULL;
3644
3645 *ret_left_path = NULL;
3646
3647 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003648 * This shouldn't happen for non-trees. The extent rec cluster
3649 * count manipulation below only works for interior nodes.
3650 */
3651 BUG_ON(right_path->p_tree_depth == 0);
3652
3653 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003654 * If our appending insert is at the leftmost edge of a leaf,
3655 * then we might need to update the rightmost records of the
3656 * neighboring path.
3657 */
3658 el = path_leaf_el(right_path);
3659 next_free = le16_to_cpu(el->l_next_free_rec);
3660 if (next_free == 0 ||
3661 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3662 u32 left_cpos;
3663
3664 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, right_path,
3665 &left_cpos);
3666 if (ret) {
3667 mlog_errno(ret);
3668 goto out;
3669 }
3670
3671 mlog(0, "Append may need a left path update. cpos: %u, "
3672 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3673 left_cpos);
3674
3675 /*
3676 * No need to worry if the append is already in the
3677 * leftmost leaf.
3678 */
3679 if (left_cpos) {
3680 left_path = ocfs2_new_path(path_root_bh(right_path),
3681 path_root_el(right_path));
3682 if (!left_path) {
3683 ret = -ENOMEM;
3684 mlog_errno(ret);
3685 goto out;
3686 }
3687
3688 ret = ocfs2_find_path(inode, left_path, left_cpos);
3689 if (ret) {
3690 mlog_errno(ret);
3691 goto out;
3692 }
3693
3694 /*
3695 * ocfs2_insert_path() will pass the left_path to the
3696 * journal for us.
3697 */
3698 }
3699 }
3700
3701 ret = ocfs2_journal_access_path(inode, handle, right_path);
3702 if (ret) {
3703 mlog_errno(ret);
3704 goto out;
3705 }
3706
Mark Fasheh328d5752007-06-18 10:48:04 -07003707 ocfs2_adjust_rightmost_records(inode, handle, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08003708
3709 *ret_left_path = left_path;
3710 ret = 0;
3711out:
3712 if (ret != 0)
3713 ocfs2_free_path(left_path);
3714
3715 return ret;
3716}
3717
Mark Fasheh328d5752007-06-18 10:48:04 -07003718static void ocfs2_split_record(struct inode *inode,
3719 struct ocfs2_path *left_path,
3720 struct ocfs2_path *right_path,
3721 struct ocfs2_extent_rec *split_rec,
3722 enum ocfs2_split_type split)
3723{
3724 int index;
3725 u32 cpos = le32_to_cpu(split_rec->e_cpos);
3726 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
3727 struct ocfs2_extent_rec *rec, *tmprec;
3728
3729 right_el = path_leaf_el(right_path);;
3730 if (left_path)
3731 left_el = path_leaf_el(left_path);
3732
3733 el = right_el;
3734 insert_el = right_el;
3735 index = ocfs2_search_extent_list(el, cpos);
3736 if (index != -1) {
3737 if (index == 0 && left_path) {
3738 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3739
3740 /*
3741 * This typically means that the record
3742 * started in the left path but moved to the
3743 * right as a result of rotation. We either
3744 * move the existing record to the left, or we
3745 * do the later insert there.
3746 *
3747 * In this case, the left path should always
3748 * exist as the rotate code will have passed
3749 * it back for a post-insert update.
3750 */
3751
3752 if (split == SPLIT_LEFT) {
3753 /*
3754 * It's a left split. Since we know
3755 * that the rotate code gave us an
3756 * empty extent in the left path, we
3757 * can just do the insert there.
3758 */
3759 insert_el = left_el;
3760 } else {
3761 /*
3762 * Right split - we have to move the
3763 * existing record over to the left
3764 * leaf. The insert will be into the
3765 * newly created empty extent in the
3766 * right leaf.
3767 */
3768 tmprec = &right_el->l_recs[index];
3769 ocfs2_rotate_leaf(left_el, tmprec);
3770 el = left_el;
3771
3772 memset(tmprec, 0, sizeof(*tmprec));
3773 index = ocfs2_search_extent_list(left_el, cpos);
3774 BUG_ON(index == -1);
3775 }
3776 }
3777 } else {
3778 BUG_ON(!left_path);
3779 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
3780 /*
3781 * Left path is easy - we can just allow the insert to
3782 * happen.
3783 */
3784 el = left_el;
3785 insert_el = left_el;
3786 index = ocfs2_search_extent_list(el, cpos);
3787 BUG_ON(index == -1);
3788 }
3789
3790 rec = &el->l_recs[index];
3791 ocfs2_subtract_from_rec(inode->i_sb, split, rec, split_rec);
3792 ocfs2_rotate_leaf(insert_el, split_rec);
3793}
3794
Mark Fashehdcd05382007-01-16 11:32:23 -08003795/*
Tao Mae7d4cb62008-08-18 17:38:44 +08003796 * This function only does inserts on an allocation b-tree. For tree
3797 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08003798 *
3799 * right_path is the path we want to do the actual insert
3800 * in. left_path should only be passed in if we need to update that
3801 * portion of the tree after an edge insert.
3802 */
3803static int ocfs2_insert_path(struct inode *inode,
3804 handle_t *handle,
3805 struct ocfs2_path *left_path,
3806 struct ocfs2_path *right_path,
3807 struct ocfs2_extent_rec *insert_rec,
3808 struct ocfs2_insert_type *insert)
3809{
3810 int ret, subtree_index;
3811 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003812
Mark Fashehdcd05382007-01-16 11:32:23 -08003813 if (left_path) {
3814 int credits = handle->h_buffer_credits;
3815
3816 /*
3817 * There's a chance that left_path got passed back to
3818 * us without being accounted for in the
3819 * journal. Extend our transaction here to be sure we
3820 * can change those blocks.
3821 */
3822 credits += left_path->p_tree_depth;
3823
3824 ret = ocfs2_extend_trans(handle, credits);
3825 if (ret < 0) {
3826 mlog_errno(ret);
3827 goto out;
3828 }
3829
3830 ret = ocfs2_journal_access_path(inode, handle, left_path);
3831 if (ret < 0) {
3832 mlog_errno(ret);
3833 goto out;
3834 }
3835 }
3836
Mark Fashehe8aed342007-12-03 16:43:01 -08003837 /*
3838 * Pass both paths to the journal. The majority of inserts
3839 * will be touching all components anyway.
3840 */
3841 ret = ocfs2_journal_access_path(inode, handle, right_path);
3842 if (ret < 0) {
3843 mlog_errno(ret);
3844 goto out;
3845 }
3846
Mark Fasheh328d5752007-06-18 10:48:04 -07003847 if (insert->ins_split != SPLIT_NONE) {
3848 /*
3849 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02003850 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07003851 * function sort it all out.
3852 */
3853 ocfs2_split_record(inode, left_path, right_path,
3854 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08003855
3856 /*
3857 * Split might have modified either leaf and we don't
3858 * have a guarantee that the later edge insert will
3859 * dirty this for us.
3860 */
3861 if (left_path)
3862 ret = ocfs2_journal_dirty(handle,
3863 path_leaf_bh(left_path));
3864 if (ret)
3865 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07003866 } else
3867 ocfs2_insert_at_leaf(insert_rec, path_leaf_el(right_path),
3868 insert, inode);
Mark Fashehdcd05382007-01-16 11:32:23 -08003869
Mark Fashehdcd05382007-01-16 11:32:23 -08003870 ret = ocfs2_journal_dirty(handle, leaf_bh);
3871 if (ret)
3872 mlog_errno(ret);
3873
3874 if (left_path) {
3875 /*
3876 * The rotate code has indicated that we need to fix
3877 * up portions of the tree after the insert.
3878 *
3879 * XXX: Should we extend the transaction here?
3880 */
3881 subtree_index = ocfs2_find_subtree_root(inode, left_path,
3882 right_path);
3883 ocfs2_complete_edge_insert(inode, handle, left_path,
3884 right_path, subtree_index);
3885 }
3886
3887 ret = 0;
3888out:
3889 return ret;
3890}
3891
3892static int ocfs2_do_insert_extent(struct inode *inode,
3893 handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08003894 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08003895 struct ocfs2_extent_rec *insert_rec,
3896 struct ocfs2_insert_type *type)
3897{
3898 int ret, rotate = 0;
3899 u32 cpos;
3900 struct ocfs2_path *right_path = NULL;
3901 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08003902 struct ocfs2_extent_list *el;
3903
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003904 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08003905
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003906 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehdcd05382007-01-16 11:32:23 -08003907 OCFS2_JOURNAL_ACCESS_WRITE);
3908 if (ret) {
3909 mlog_errno(ret);
3910 goto out;
3911 }
3912
3913 if (le16_to_cpu(el->l_tree_depth) == 0) {
3914 ocfs2_insert_at_leaf(insert_rec, el, type, inode);
3915 goto out_update_clusters;
3916 }
3917
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003918 right_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fashehdcd05382007-01-16 11:32:23 -08003919 if (!right_path) {
3920 ret = -ENOMEM;
3921 mlog_errno(ret);
3922 goto out;
3923 }
3924
3925 /*
3926 * Determine the path to start with. Rotations need the
3927 * rightmost path, everything else can go directly to the
3928 * target leaf.
3929 */
3930 cpos = le32_to_cpu(insert_rec->e_cpos);
3931 if (type->ins_appending == APPEND_NONE &&
3932 type->ins_contig == CONTIG_NONE) {
3933 rotate = 1;
3934 cpos = UINT_MAX;
3935 }
3936
3937 ret = ocfs2_find_path(inode, right_path, cpos);
3938 if (ret) {
3939 mlog_errno(ret);
3940 goto out;
3941 }
3942
3943 /*
3944 * Rotations and appends need special treatment - they modify
3945 * parts of the tree's above them.
3946 *
3947 * Both might pass back a path immediate to the left of the
3948 * one being inserted to. This will be cause
3949 * ocfs2_insert_path() to modify the rightmost records of
3950 * left_path to account for an edge insert.
3951 *
3952 * XXX: When modifying this code, keep in mind that an insert
3953 * can wind up skipping both of these two special cases...
3954 */
3955 if (rotate) {
Mark Fasheh328d5752007-06-18 10:48:04 -07003956 ret = ocfs2_rotate_tree_right(inode, handle, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08003957 le32_to_cpu(insert_rec->e_cpos),
3958 right_path, &left_path);
3959 if (ret) {
3960 mlog_errno(ret);
3961 goto out;
3962 }
Mark Fashehe8aed342007-12-03 16:43:01 -08003963
3964 /*
3965 * ocfs2_rotate_tree_right() might have extended the
3966 * transaction without re-journaling our tree root.
3967 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003968 ret = ocfs2_journal_access(handle, inode, et->et_root_bh,
Mark Fashehe8aed342007-12-03 16:43:01 -08003969 OCFS2_JOURNAL_ACCESS_WRITE);
3970 if (ret) {
3971 mlog_errno(ret);
3972 goto out;
3973 }
Mark Fashehdcd05382007-01-16 11:32:23 -08003974 } else if (type->ins_appending == APPEND_TAIL
3975 && type->ins_contig != CONTIG_LEFT) {
3976 ret = ocfs2_append_rec_to_path(inode, handle, insert_rec,
3977 right_path, &left_path);
3978 if (ret) {
3979 mlog_errno(ret);
3980 goto out;
3981 }
3982 }
3983
3984 ret = ocfs2_insert_path(inode, handle, left_path, right_path,
3985 insert_rec, type);
3986 if (ret) {
3987 mlog_errno(ret);
3988 goto out;
3989 }
3990
3991out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07003992 if (type->ins_split == SPLIT_NONE)
Joel Becker35dc0aa2008-08-20 16:25:06 -07003993 ocfs2_et_update_clusters(inode, et,
3994 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003995
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003996 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08003997 if (ret)
3998 mlog_errno(ret);
3999
4000out:
4001 ocfs2_free_path(left_path);
4002 ocfs2_free_path(right_path);
4003
4004 return ret;
4005}
4006
Mark Fasheh328d5752007-06-18 10:48:04 -07004007static enum ocfs2_contig_type
Tao Maad5a4d72008-01-30 14:21:32 +08004008ocfs2_figure_merge_contig_type(struct inode *inode, struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004009 struct ocfs2_extent_list *el, int index,
4010 struct ocfs2_extent_rec *split_rec)
4011{
Tao Maad5a4d72008-01-30 14:21:32 +08004012 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004013 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004014 u32 left_cpos, right_cpos;
4015 struct ocfs2_extent_rec *rec = NULL;
4016 struct ocfs2_extent_list *new_el;
4017 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4018 struct buffer_head *bh;
4019 struct ocfs2_extent_block *eb;
4020
4021 if (index > 0) {
4022 rec = &el->l_recs[index - 1];
4023 } else if (path->p_tree_depth > 0) {
4024 status = ocfs2_find_cpos_for_left_leaf(inode->i_sb,
4025 path, &left_cpos);
4026 if (status)
4027 goto out;
4028
4029 if (left_cpos != 0) {
4030 left_path = ocfs2_new_path(path_root_bh(path),
4031 path_root_el(path));
4032 if (!left_path)
4033 goto out;
4034
4035 status = ocfs2_find_path(inode, left_path, left_cpos);
4036 if (status)
4037 goto out;
4038
4039 new_el = path_leaf_el(left_path);
4040
4041 if (le16_to_cpu(new_el->l_next_free_rec) !=
4042 le16_to_cpu(new_el->l_count)) {
4043 bh = path_leaf_bh(left_path);
4044 eb = (struct ocfs2_extent_block *)bh->b_data;
4045 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4046 eb);
4047 goto out;
4048 }
4049 rec = &new_el->l_recs[
4050 le16_to_cpu(new_el->l_next_free_rec) - 1];
4051 }
4052 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004053
4054 /*
4055 * We're careful to check for an empty extent record here -
4056 * the merge code will know what to do if it sees one.
4057 */
Tao Maad5a4d72008-01-30 14:21:32 +08004058 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004059 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4060 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4061 ret = CONTIG_RIGHT;
4062 } else {
4063 ret = ocfs2_extent_contig(inode, rec, split_rec);
4064 }
4065 }
4066
Tao Maad5a4d72008-01-30 14:21:32 +08004067 rec = NULL;
4068 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4069 rec = &el->l_recs[index + 1];
4070 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4071 path->p_tree_depth > 0) {
4072 status = ocfs2_find_cpos_for_right_leaf(inode->i_sb,
4073 path, &right_cpos);
4074 if (status)
4075 goto out;
4076
4077 if (right_cpos == 0)
4078 goto out;
4079
4080 right_path = ocfs2_new_path(path_root_bh(path),
4081 path_root_el(path));
4082 if (!right_path)
4083 goto out;
4084
4085 status = ocfs2_find_path(inode, right_path, right_cpos);
4086 if (status)
4087 goto out;
4088
4089 new_el = path_leaf_el(right_path);
4090 rec = &new_el->l_recs[0];
4091 if (ocfs2_is_empty_extent(rec)) {
4092 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4093 bh = path_leaf_bh(right_path);
4094 eb = (struct ocfs2_extent_block *)bh->b_data;
4095 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb,
4096 eb);
4097 goto out;
4098 }
4099 rec = &new_el->l_recs[1];
4100 }
4101 }
4102
4103 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004104 enum ocfs2_contig_type contig_type;
4105
Mark Fasheh328d5752007-06-18 10:48:04 -07004106 contig_type = ocfs2_extent_contig(inode, rec, split_rec);
4107
4108 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4109 ret = CONTIG_LEFTRIGHT;
4110 else if (ret == CONTIG_NONE)
4111 ret = contig_type;
4112 }
4113
Tao Maad5a4d72008-01-30 14:21:32 +08004114out:
4115 if (left_path)
4116 ocfs2_free_path(left_path);
4117 if (right_path)
4118 ocfs2_free_path(right_path);
4119
Mark Fasheh328d5752007-06-18 10:48:04 -07004120 return ret;
4121}
4122
Mark Fashehdcd05382007-01-16 11:32:23 -08004123static void ocfs2_figure_contig_type(struct inode *inode,
4124 struct ocfs2_insert_type *insert,
4125 struct ocfs2_extent_list *el,
Tao Maca12b7c2008-08-18 17:38:52 +08004126 struct ocfs2_extent_rec *insert_rec,
4127 struct ocfs2_extent_tree *et)
Mark Fashehdcd05382007-01-16 11:32:23 -08004128{
4129 int i;
4130 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4131
Mark Fashehe48edee2007-03-07 16:46:57 -08004132 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4133
Mark Fashehdcd05382007-01-16 11:32:23 -08004134 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
4135 contig_type = ocfs2_extent_contig(inode, &el->l_recs[i],
4136 insert_rec);
4137 if (contig_type != CONTIG_NONE) {
4138 insert->ins_contig_index = i;
4139 break;
4140 }
4141 }
4142 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004143
4144 if (insert->ins_contig != CONTIG_NONE) {
4145 struct ocfs2_extent_rec *rec =
4146 &el->l_recs[insert->ins_contig_index];
4147 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4148 le16_to_cpu(insert_rec->e_leaf_clusters);
4149
4150 /*
4151 * Caller might want us to limit the size of extents, don't
4152 * calculate contiguousness if we might exceed that limit.
4153 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004154 if (et->et_max_leaf_clusters &&
4155 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004156 insert->ins_contig = CONTIG_NONE;
4157 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004158}
4159
4160/*
4161 * This should only be called against the righmost leaf extent list.
4162 *
4163 * ocfs2_figure_appending_type() will figure out whether we'll have to
4164 * insert at the tail of the rightmost leaf.
4165 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004166 * This should also work against the root extent list for tree's with 0
4167 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004168 * then the logic here makes sense.
4169 */
4170static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4171 struct ocfs2_extent_list *el,
4172 struct ocfs2_extent_rec *insert_rec)
4173{
4174 int i;
4175 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4176 struct ocfs2_extent_rec *rec;
4177
4178 insert->ins_appending = APPEND_NONE;
4179
Mark Fashehe48edee2007-03-07 16:46:57 -08004180 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004181
4182 if (!el->l_next_free_rec)
4183 goto set_tail_append;
4184
4185 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4186 /* Were all records empty? */
4187 if (le16_to_cpu(el->l_next_free_rec) == 1)
4188 goto set_tail_append;
4189 }
4190
4191 i = le16_to_cpu(el->l_next_free_rec) - 1;
4192 rec = &el->l_recs[i];
4193
Mark Fashehe48edee2007-03-07 16:46:57 -08004194 if (cpos >=
4195 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004196 goto set_tail_append;
4197
4198 return;
4199
4200set_tail_append:
4201 insert->ins_appending = APPEND_TAIL;
4202}
4203
4204/*
4205 * Helper function called at the begining of an insert.
4206 *
4207 * This computes a few things that are commonly used in the process of
4208 * inserting into the btree:
4209 * - Whether the new extent is contiguous with an existing one.
4210 * - The current tree depth.
4211 * - Whether the insert is an appending one.
4212 * - The total # of free records in the tree.
4213 *
4214 * All of the information is stored on the ocfs2_insert_type
4215 * structure.
4216 */
4217static int ocfs2_figure_insert_type(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004218 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004219 struct buffer_head **last_eb_bh,
4220 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004221 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004222 struct ocfs2_insert_type *insert)
4223{
4224 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004225 struct ocfs2_extent_block *eb;
4226 struct ocfs2_extent_list *el;
4227 struct ocfs2_path *path = NULL;
4228 struct buffer_head *bh = NULL;
4229
Mark Fasheh328d5752007-06-18 10:48:04 -07004230 insert->ins_split = SPLIT_NONE;
4231
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004232 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004233 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4234
4235 if (el->l_tree_depth) {
4236 /*
4237 * If we have tree depth, we read in the
4238 * rightmost extent block ahead of time as
4239 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4240 * may want it later.
4241 */
4242 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
Joel Becker35dc0aa2008-08-20 16:25:06 -07004243 ocfs2_et_get_last_eb_blk(et), &bh,
Mark Fashehdcd05382007-01-16 11:32:23 -08004244 OCFS2_BH_CACHED, inode);
4245 if (ret) {
4246 mlog_exit(ret);
4247 goto out;
4248 }
4249 eb = (struct ocfs2_extent_block *) bh->b_data;
4250 el = &eb->h_list;
4251 }
4252
4253 /*
4254 * Unless we have a contiguous insert, we'll need to know if
4255 * there is room left in our allocation tree for another
4256 * extent record.
4257 *
4258 * XXX: This test is simplistic, we can search for empty
4259 * extent records too.
4260 */
Tao Maoc77534f2007-08-28 17:22:33 -07004261 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004262 le16_to_cpu(el->l_next_free_rec);
4263
4264 if (!insert->ins_tree_depth) {
Tao Maca12b7c2008-08-18 17:38:52 +08004265 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004266 ocfs2_figure_appending_type(insert, el, insert_rec);
4267 return 0;
4268 }
4269
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004270 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fashehdcd05382007-01-16 11:32:23 -08004271 if (!path) {
4272 ret = -ENOMEM;
4273 mlog_errno(ret);
4274 goto out;
4275 }
4276
4277 /*
4278 * In the case that we're inserting past what the tree
4279 * currently accounts for, ocfs2_find_path() will return for
4280 * us the rightmost tree path. This is accounted for below in
4281 * the appending code.
4282 */
4283 ret = ocfs2_find_path(inode, path, le32_to_cpu(insert_rec->e_cpos));
4284 if (ret) {
4285 mlog_errno(ret);
4286 goto out;
4287 }
4288
4289 el = path_leaf_el(path);
4290
4291 /*
4292 * Now that we have the path, there's two things we want to determine:
4293 * 1) Contiguousness (also set contig_index if this is so)
4294 *
4295 * 2) Are we doing an append? We can trivially break this up
4296 * into two types of appends: simple record append, or a
4297 * rotate inside the tail leaf.
4298 */
Tao Maca12b7c2008-08-18 17:38:52 +08004299 ocfs2_figure_contig_type(inode, insert, el, insert_rec, et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004300
4301 /*
4302 * The insert code isn't quite ready to deal with all cases of
4303 * left contiguousness. Specifically, if it's an insert into
4304 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004305 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004306 * left. For now, just catch that case and fool the layers
4307 * above us. This works just fine for tree_depth == 0, which
4308 * is why we allow that above.
4309 */
4310 if (insert->ins_contig == CONTIG_LEFT &&
4311 insert->ins_contig_index == 0)
4312 insert->ins_contig = CONTIG_NONE;
4313
4314 /*
4315 * Ok, so we can simply compare against last_eb to figure out
4316 * whether the path doesn't exist. This will only happen in
4317 * the case that we're doing a tail append, so maybe we can
4318 * take advantage of that information somehow.
4319 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004320 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004321 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004322 /*
4323 * Ok, ocfs2_find_path() returned us the rightmost
4324 * tree path. This might be an appending insert. There are
4325 * two cases:
4326 * 1) We're doing a true append at the tail:
4327 * -This might even be off the end of the leaf
4328 * 2) We're "appending" by rotating in the tail
4329 */
4330 ocfs2_figure_appending_type(insert, el, insert_rec);
4331 }
4332
4333out:
4334 ocfs2_free_path(path);
4335
4336 if (ret == 0)
4337 *last_eb_bh = bh;
4338 else
4339 brelse(bh);
4340 return ret;
4341}
4342
4343/*
4344 * Insert an extent into an inode btree.
4345 *
4346 * The caller needs to update fe->i_clusters
4347 */
Tao Maf56654c2008-08-18 17:38:48 +08004348static int ocfs2_insert_extent(struct ocfs2_super *osb,
4349 handle_t *handle,
4350 struct inode *inode,
4351 struct buffer_head *root_bh,
4352 u32 cpos,
4353 u64 start_blk,
4354 u32 new_clusters,
4355 u8 flags,
4356 struct ocfs2_alloc_context *meta_ac,
4357 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -08004358{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004359 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004360 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004361 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004362 struct ocfs2_insert_type insert = {0, };
4363 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004364
Mark Fasheh1afc32b2007-09-07 14:46:51 -07004365 BUG_ON(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL);
4366
Mark Fashehdcd05382007-01-16 11:32:23 -08004367 mlog(0, "add %u clusters at position %u to inode %llu\n",
4368 new_clusters, cpos, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08004369
Mark Fashehdcd05382007-01-16 11:32:23 -08004370 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
4371 (OCFS2_I(inode)->ip_clusters != cpos),
4372 "Device %s, asking for sparse allocation: inode %llu, "
4373 "cpos %u, clusters %u\n",
4374 osb->dev_str,
4375 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos,
4376 OCFS2_I(inode)->ip_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08004377
Mark Fashehe48edee2007-03-07 16:46:57 -08004378 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004379 rec.e_cpos = cpu_to_le32(cpos);
4380 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004381 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004382 rec.e_flags = flags;
Mark Fashehccd979b2005-12-15 14:31:24 -08004383
Tao Mae7d4cb62008-08-18 17:38:44 +08004384 status = ocfs2_figure_insert_type(inode, et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004385 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004386 if (status < 0) {
4387 mlog_errno(status);
4388 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004389 }
4390
Mark Fashehdcd05382007-01-16 11:32:23 -08004391 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4392 "Insert.contig_index: %d, Insert.free_records: %d, "
4393 "Insert.tree_depth: %d\n",
4394 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004395 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004396
Tao Maoc77534f2007-08-28 17:22:33 -07004397 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004398 status = ocfs2_grow_tree(inode, handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004399 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004400 meta_ac);
4401 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004402 mlog_errno(status);
4403 goto bail;
4404 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004405 }
4406
Mark Fashehdcd05382007-01-16 11:32:23 -08004407 /* Finally, we can add clusters. This might rotate the tree for us. */
Tao Mae7d4cb62008-08-18 17:38:44 +08004408 status = ocfs2_do_insert_extent(inode, handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004409 if (status < 0)
4410 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004411 else if (et->et_type == OCFS2_DINODE_EXTENT)
Mark Fasheh83418972007-04-23 18:53:12 -07004412 ocfs2_extent_map_insert_rec(inode, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004413
4414bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08004415 if (last_eb_bh)
4416 brelse(last_eb_bh);
4417
Tao Maf56654c2008-08-18 17:38:48 +08004418 mlog_exit(status);
4419 return status;
4420}
4421
4422int ocfs2_dinode_insert_extent(struct ocfs2_super *osb,
4423 handle_t *handle,
4424 struct inode *inode,
4425 struct buffer_head *root_bh,
4426 u32 cpos,
4427 u64 start_blk,
4428 u32 new_clusters,
4429 u8 flags,
4430 struct ocfs2_alloc_context *meta_ac)
4431{
4432 int status;
4433 struct ocfs2_extent_tree *et = NULL;
4434
Tao Maca12b7c2008-08-18 17:38:52 +08004435 et = ocfs2_new_extent_tree(inode, root_bh, OCFS2_DINODE_EXTENT, NULL);
Tao Maf56654c2008-08-18 17:38:48 +08004436 if (!et) {
4437 status = -ENOMEM;
4438 mlog_errno(status);
4439 goto bail;
4440 }
4441
4442 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4443 cpos, start_blk, new_clusters,
4444 flags, meta_ac, et);
4445
Tao Mae7d4cb62008-08-18 17:38:44 +08004446 if (et)
4447 ocfs2_free_extent_tree(et);
Tao Maf56654c2008-08-18 17:38:48 +08004448bail:
4449 return status;
4450}
4451
4452int ocfs2_xattr_value_insert_extent(struct ocfs2_super *osb,
4453 handle_t *handle,
4454 struct inode *inode,
4455 struct buffer_head *root_bh,
4456 u32 cpos,
4457 u64 start_blk,
4458 u32 new_clusters,
4459 u8 flags,
4460 struct ocfs2_alloc_context *meta_ac,
4461 void *private)
4462{
4463 int status;
4464 struct ocfs2_extent_tree *et = NULL;
4465
Tao Maca12b7c2008-08-18 17:38:52 +08004466 et = ocfs2_new_extent_tree(inode, root_bh,
4467 OCFS2_XATTR_VALUE_EXTENT, private);
Tao Maf56654c2008-08-18 17:38:48 +08004468 if (!et) {
4469 status = -ENOMEM;
4470 mlog_errno(status);
4471 goto bail;
4472 }
4473
4474 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4475 cpos, start_blk, new_clusters,
4476 flags, meta_ac, et);
4477
4478 if (et)
4479 ocfs2_free_extent_tree(et);
4480bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08004481 return status;
4482}
4483
Tao Maba492612008-08-18 17:38:49 +08004484int ocfs2_xattr_tree_insert_extent(struct ocfs2_super *osb,
4485 handle_t *handle,
4486 struct inode *inode,
4487 struct buffer_head *root_bh,
4488 u32 cpos,
4489 u64 start_blk,
4490 u32 new_clusters,
4491 u8 flags,
4492 struct ocfs2_alloc_context *meta_ac)
4493{
4494 int status;
4495 struct ocfs2_extent_tree *et = NULL;
4496
Tao Maca12b7c2008-08-18 17:38:52 +08004497 et = ocfs2_new_extent_tree(inode, root_bh, OCFS2_XATTR_TREE_EXTENT,
4498 NULL);
Tao Maba492612008-08-18 17:38:49 +08004499 if (!et) {
4500 status = -ENOMEM;
4501 mlog_errno(status);
4502 goto bail;
4503 }
4504
4505 status = ocfs2_insert_extent(osb, handle, inode, root_bh,
4506 cpos, start_blk, new_clusters,
4507 flags, meta_ac, et);
4508
4509 if (et)
4510 ocfs2_free_extent_tree(et);
4511bail:
4512 return status;
4513}
4514
Tao Ma0eb8d472008-08-18 17:38:45 +08004515/*
4516 * Allcate and add clusters into the extent b-tree.
4517 * The new clusters(clusters_to_add) will be inserted at logical_offset.
4518 * The extent b-tree's root is root_el and it should be in root_bh, and
4519 * it is not limited to the file storage. Any extent tree can use this
4520 * function if it implements the proper ocfs2_extent_tree.
4521 */
4522int ocfs2_add_clusters_in_btree(struct ocfs2_super *osb,
4523 struct inode *inode,
4524 u32 *logical_offset,
4525 u32 clusters_to_add,
4526 int mark_unwritten,
4527 struct buffer_head *root_bh,
4528 struct ocfs2_extent_list *root_el,
4529 handle_t *handle,
4530 struct ocfs2_alloc_context *data_ac,
4531 struct ocfs2_alloc_context *meta_ac,
4532 enum ocfs2_alloc_restarted *reason_ret,
Tao Maf56654c2008-08-18 17:38:48 +08004533 enum ocfs2_extent_tree_type type,
4534 void *private)
Tao Ma0eb8d472008-08-18 17:38:45 +08004535{
4536 int status = 0;
4537 int free_extents;
4538 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4539 u32 bit_off, num_bits;
4540 u64 block;
4541 u8 flags = 0;
4542
4543 BUG_ON(!clusters_to_add);
4544
4545 if (mark_unwritten)
4546 flags = OCFS2_EXT_UNWRITTEN;
4547
Tao Maf56654c2008-08-18 17:38:48 +08004548 free_extents = ocfs2_num_free_extents(osb, inode, root_bh, type,
4549 private);
Tao Ma0eb8d472008-08-18 17:38:45 +08004550 if (free_extents < 0) {
4551 status = free_extents;
4552 mlog_errno(status);
4553 goto leave;
4554 }
4555
4556 /* there are two cases which could cause us to EAGAIN in the
4557 * we-need-more-metadata case:
4558 * 1) we haven't reserved *any*
4559 * 2) we are so fragmented, we've needed to add metadata too
4560 * many times. */
4561 if (!free_extents && !meta_ac) {
4562 mlog(0, "we haven't reserved any metadata!\n");
4563 status = -EAGAIN;
4564 reason = RESTART_META;
4565 goto leave;
4566 } else if ((!free_extents)
4567 && (ocfs2_alloc_context_bits_left(meta_ac)
4568 < ocfs2_extend_meta_needed(root_el))) {
4569 mlog(0, "filesystem is really fragmented...\n");
4570 status = -EAGAIN;
4571 reason = RESTART_META;
4572 goto leave;
4573 }
4574
4575 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4576 clusters_to_add, &bit_off, &num_bits);
4577 if (status < 0) {
4578 if (status != -ENOSPC)
4579 mlog_errno(status);
4580 goto leave;
4581 }
4582
4583 BUG_ON(num_bits > clusters_to_add);
4584
4585 /* reserve our write early -- insert_extent may update the inode */
4586 status = ocfs2_journal_access(handle, inode, root_bh,
4587 OCFS2_JOURNAL_ACCESS_WRITE);
4588 if (status < 0) {
4589 mlog_errno(status);
4590 goto leave;
4591 }
4592
4593 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
4594 mlog(0, "Allocating %u clusters at block %u for inode %llu\n",
4595 num_bits, bit_off, (unsigned long long)OCFS2_I(inode)->ip_blkno);
Tao Maf56654c2008-08-18 17:38:48 +08004596 if (type == OCFS2_DINODE_EXTENT)
4597 status = ocfs2_dinode_insert_extent(osb, handle, inode, root_bh,
4598 *logical_offset, block,
4599 num_bits, flags, meta_ac);
Tao Maba492612008-08-18 17:38:49 +08004600 else if (type == OCFS2_XATTR_TREE_EXTENT)
4601 status = ocfs2_xattr_tree_insert_extent(osb, handle,
4602 inode, root_bh,
4603 *logical_offset,
4604 block, num_bits, flags,
4605 meta_ac);
Tao Maf56654c2008-08-18 17:38:48 +08004606 else
4607 status = ocfs2_xattr_value_insert_extent(osb, handle,
4608 inode, root_bh,
4609 *logical_offset,
4610 block, num_bits, flags,
4611 meta_ac, private);
Tao Ma0eb8d472008-08-18 17:38:45 +08004612 if (status < 0) {
4613 mlog_errno(status);
4614 goto leave;
4615 }
4616
4617 status = ocfs2_journal_dirty(handle, root_bh);
4618 if (status < 0) {
4619 mlog_errno(status);
4620 goto leave;
4621 }
4622
4623 clusters_to_add -= num_bits;
4624 *logical_offset += num_bits;
4625
4626 if (clusters_to_add) {
4627 mlog(0, "need to alloc once more, wanted = %u\n",
4628 clusters_to_add);
4629 status = -EAGAIN;
4630 reason = RESTART_TRANS;
4631 }
4632
4633leave:
4634 mlog_exit(status);
4635 if (reason_ret)
4636 *reason_ret = reason;
4637 return status;
4638}
4639
Mark Fasheh328d5752007-06-18 10:48:04 -07004640static void ocfs2_make_right_split_rec(struct super_block *sb,
4641 struct ocfs2_extent_rec *split_rec,
4642 u32 cpos,
4643 struct ocfs2_extent_rec *rec)
4644{
4645 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4646 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4647
4648 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4649
4650 split_rec->e_cpos = cpu_to_le32(cpos);
4651 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4652
4653 split_rec->e_blkno = rec->e_blkno;
4654 le64_add_cpu(&split_rec->e_blkno,
4655 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4656
4657 split_rec->e_flags = rec->e_flags;
4658}
4659
4660static int ocfs2_split_and_insert(struct inode *inode,
4661 handle_t *handle,
4662 struct ocfs2_path *path,
Tao Mae7d4cb62008-08-18 17:38:44 +08004663 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004664 struct buffer_head **last_eb_bh,
4665 int split_index,
4666 struct ocfs2_extent_rec *orig_split_rec,
4667 struct ocfs2_alloc_context *meta_ac)
4668{
4669 int ret = 0, depth;
4670 unsigned int insert_range, rec_range, do_leftright = 0;
4671 struct ocfs2_extent_rec tmprec;
4672 struct ocfs2_extent_list *rightmost_el;
4673 struct ocfs2_extent_rec rec;
4674 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4675 struct ocfs2_insert_type insert;
4676 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004677
4678leftright:
4679 /*
4680 * Store a copy of the record on the stack - it might move
4681 * around as the tree is manipulated below.
4682 */
4683 rec = path_leaf_el(path)->l_recs[split_index];
4684
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004685 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004686
4687 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4688 if (depth) {
4689 BUG_ON(!(*last_eb_bh));
4690 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4691 rightmost_el = &eb->h_list;
4692 }
4693
4694 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4695 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08004696 ret = ocfs2_grow_tree(inode, handle, et,
4697 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004698 if (ret) {
4699 mlog_errno(ret);
4700 goto out;
4701 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004702 }
4703
4704 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4705 insert.ins_appending = APPEND_NONE;
4706 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004707 insert.ins_tree_depth = depth;
4708
4709 insert_range = le32_to_cpu(split_rec.e_cpos) +
4710 le16_to_cpu(split_rec.e_leaf_clusters);
4711 rec_range = le32_to_cpu(rec.e_cpos) +
4712 le16_to_cpu(rec.e_leaf_clusters);
4713
4714 if (split_rec.e_cpos == rec.e_cpos) {
4715 insert.ins_split = SPLIT_LEFT;
4716 } else if (insert_range == rec_range) {
4717 insert.ins_split = SPLIT_RIGHT;
4718 } else {
4719 /*
4720 * Left/right split. We fake this as a right split
4721 * first and then make a second pass as a left split.
4722 */
4723 insert.ins_split = SPLIT_RIGHT;
4724
4725 ocfs2_make_right_split_rec(inode->i_sb, &tmprec, insert_range,
4726 &rec);
4727
4728 split_rec = tmprec;
4729
4730 BUG_ON(do_leftright);
4731 do_leftright = 1;
4732 }
4733
Tao Mae7d4cb62008-08-18 17:38:44 +08004734 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004735 if (ret) {
4736 mlog_errno(ret);
4737 goto out;
4738 }
4739
4740 if (do_leftright == 1) {
4741 u32 cpos;
4742 struct ocfs2_extent_list *el;
4743
4744 do_leftright++;
4745 split_rec = *orig_split_rec;
4746
4747 ocfs2_reinit_path(path, 1);
4748
4749 cpos = le32_to_cpu(split_rec.e_cpos);
4750 ret = ocfs2_find_path(inode, path, cpos);
4751 if (ret) {
4752 mlog_errno(ret);
4753 goto out;
4754 }
4755
4756 el = path_leaf_el(path);
4757 split_index = ocfs2_search_extent_list(el, cpos);
4758 goto leftright;
4759 }
4760out:
4761
4762 return ret;
4763}
4764
4765/*
4766 * Mark part or all of the extent record at split_index in the leaf
4767 * pointed to by path as written. This removes the unwritten
4768 * extent flag.
4769 *
4770 * Care is taken to handle contiguousness so as to not grow the tree.
4771 *
4772 * meta_ac is not strictly necessary - we only truly need it if growth
4773 * of the tree is required. All other cases will degrade into a less
4774 * optimal tree layout.
4775 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004776 * last_eb_bh should be the rightmost leaf block for any extent
4777 * btree. Since a split may grow the tree or a merge might shrink it,
4778 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07004779 *
4780 * This code is optimized for readability - several passes might be
4781 * made over certain portions of the tree. All of those blocks will
4782 * have been brought into cache (and pinned via the journal), so the
4783 * extra overhead is not expressed in terms of disk reads.
4784 */
4785static int __ocfs2_mark_extent_written(struct inode *inode,
Tao Mae7d4cb62008-08-18 17:38:44 +08004786 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004787 handle_t *handle,
4788 struct ocfs2_path *path,
4789 int split_index,
4790 struct ocfs2_extent_rec *split_rec,
4791 struct ocfs2_alloc_context *meta_ac,
4792 struct ocfs2_cached_dealloc_ctxt *dealloc)
4793{
4794 int ret = 0;
4795 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004796 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07004797 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
4798 struct ocfs2_merge_ctxt ctxt;
4799 struct ocfs2_extent_list *rightmost_el;
4800
Roel Kluin3cf0c502007-10-27 00:20:36 +02004801 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004802 ret = -EIO;
4803 mlog_errno(ret);
4804 goto out;
4805 }
4806
4807 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
4808 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
4809 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
4810 ret = -EIO;
4811 mlog_errno(ret);
4812 goto out;
4813 }
4814
Tao Maad5a4d72008-01-30 14:21:32 +08004815 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(inode, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07004816 split_index,
4817 split_rec);
4818
4819 /*
4820 * The core merge / split code wants to know how much room is
4821 * left in this inodes allocation tree, so we pass the
4822 * rightmost extent list.
4823 */
4824 if (path->p_tree_depth) {
4825 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004826
4827 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
Joel Becker35dc0aa2008-08-20 16:25:06 -07004828 ocfs2_et_get_last_eb_blk(et),
Mark Fasheh328d5752007-06-18 10:48:04 -07004829 &last_eb_bh, OCFS2_BH_CACHED, inode);
4830 if (ret) {
4831 mlog_exit(ret);
4832 goto out;
4833 }
4834
4835 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
4836 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
4837 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
4838 ret = -EROFS;
4839 goto out;
4840 }
4841
4842 rightmost_el = &eb->h_list;
4843 } else
4844 rightmost_el = path_root_el(path);
4845
Mark Fasheh328d5752007-06-18 10:48:04 -07004846 if (rec->e_cpos == split_rec->e_cpos &&
4847 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
4848 ctxt.c_split_covers_rec = 1;
4849 else
4850 ctxt.c_split_covers_rec = 0;
4851
4852 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
4853
Mark Fasheh015452b2007-09-12 10:21:22 -07004854 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
4855 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
4856 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004857
4858 if (ctxt.c_contig_type == CONTIG_NONE) {
4859 if (ctxt.c_split_covers_rec)
4860 el->l_recs[split_index] = *split_rec;
4861 else
Tao Mae7d4cb62008-08-18 17:38:44 +08004862 ret = ocfs2_split_and_insert(inode, handle, path, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004863 &last_eb_bh, split_index,
4864 split_rec, meta_ac);
4865 if (ret)
4866 mlog_errno(ret);
4867 } else {
4868 ret = ocfs2_try_to_merge_extent(inode, handle, path,
4869 split_index, split_rec,
Tao Mae7d4cb62008-08-18 17:38:44 +08004870 dealloc, &ctxt, et);
Mark Fasheh328d5752007-06-18 10:48:04 -07004871 if (ret)
4872 mlog_errno(ret);
4873 }
4874
Mark Fasheh328d5752007-06-18 10:48:04 -07004875out:
4876 brelse(last_eb_bh);
4877 return ret;
4878}
4879
4880/*
4881 * Mark the already-existing extent at cpos as written for len clusters.
4882 *
4883 * If the existing extent is larger than the request, initiate a
4884 * split. An attempt will be made at merging with adjacent extents.
4885 *
4886 * The caller is responsible for passing down meta_ac if we'll need it.
4887 */
Tao Mae7d4cb62008-08-18 17:38:44 +08004888int ocfs2_mark_extent_written(struct inode *inode, struct buffer_head *root_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07004889 handle_t *handle, u32 cpos, u32 len, u32 phys,
4890 struct ocfs2_alloc_context *meta_ac,
Tao Mae7d4cb62008-08-18 17:38:44 +08004891 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Maf56654c2008-08-18 17:38:48 +08004892 enum ocfs2_extent_tree_type et_type,
4893 void *private)
Mark Fasheh328d5752007-06-18 10:48:04 -07004894{
4895 int ret, index;
4896 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
4897 struct ocfs2_extent_rec split_rec;
4898 struct ocfs2_path *left_path = NULL;
4899 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08004900 struct ocfs2_extent_tree *et = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07004901
4902 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
4903 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
4904
4905 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
4906 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
4907 "that are being written to, but the feature bit "
4908 "is not set in the super block.",
4909 (unsigned long long)OCFS2_I(inode)->ip_blkno);
4910 ret = -EROFS;
4911 goto out;
4912 }
4913
Tao Maca12b7c2008-08-18 17:38:52 +08004914 et = ocfs2_new_extent_tree(inode, root_bh, et_type, private);
Tao Mae7d4cb62008-08-18 17:38:44 +08004915 if (!et) {
4916 ret = -ENOMEM;
4917 mlog_errno(ret);
4918 goto out;
4919 }
4920
Mark Fasheh328d5752007-06-18 10:48:04 -07004921 /*
4922 * XXX: This should be fixed up so that we just re-insert the
4923 * next extent records.
4924 */
Tao Mae7d4cb62008-08-18 17:38:44 +08004925 if (et_type == OCFS2_DINODE_EXTENT)
4926 ocfs2_extent_map_trunc(inode, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07004927
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004928 left_path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fasheh328d5752007-06-18 10:48:04 -07004929 if (!left_path) {
4930 ret = -ENOMEM;
4931 mlog_errno(ret);
4932 goto out;
4933 }
4934
4935 ret = ocfs2_find_path(inode, left_path, cpos);
4936 if (ret) {
4937 mlog_errno(ret);
4938 goto out;
4939 }
4940 el = path_leaf_el(left_path);
4941
4942 index = ocfs2_search_extent_list(el, cpos);
4943 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
4944 ocfs2_error(inode->i_sb,
4945 "Inode %llu has an extent at cpos %u which can no "
4946 "longer be found.\n",
4947 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
4948 ret = -EROFS;
4949 goto out;
4950 }
4951
4952 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
4953 split_rec.e_cpos = cpu_to_le32(cpos);
4954 split_rec.e_leaf_clusters = cpu_to_le16(len);
4955 split_rec.e_blkno = cpu_to_le64(start_blkno);
4956 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
4957 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
4958
Tao Mae7d4cb62008-08-18 17:38:44 +08004959 ret = __ocfs2_mark_extent_written(inode, et, handle, left_path,
4960 index, &split_rec, meta_ac,
4961 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07004962 if (ret)
4963 mlog_errno(ret);
4964
4965out:
4966 ocfs2_free_path(left_path);
Tao Mae7d4cb62008-08-18 17:38:44 +08004967 if (et)
4968 ocfs2_free_extent_tree(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07004969 return ret;
4970}
4971
Tao Mae7d4cb62008-08-18 17:38:44 +08004972static int ocfs2_split_tree(struct inode *inode, struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07004973 handle_t *handle, struct ocfs2_path *path,
4974 int index, u32 new_range,
4975 struct ocfs2_alloc_context *meta_ac)
4976{
4977 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07004978 struct buffer_head *last_eb_bh = NULL;
4979 struct ocfs2_extent_block *eb;
4980 struct ocfs2_extent_list *rightmost_el, *el;
4981 struct ocfs2_extent_rec split_rec;
4982 struct ocfs2_extent_rec *rec;
4983 struct ocfs2_insert_type insert;
4984
4985 /*
4986 * Setup the record to split before we grow the tree.
4987 */
4988 el = path_leaf_el(path);
4989 rec = &el->l_recs[index];
4990 ocfs2_make_right_split_rec(inode->i_sb, &split_rec, new_range, rec);
4991
4992 depth = path->p_tree_depth;
4993 if (depth > 0) {
4994 ret = ocfs2_read_block(OCFS2_SB(inode->i_sb),
Joel Becker35dc0aa2008-08-20 16:25:06 -07004995 ocfs2_et_get_last_eb_blk(et),
Mark Fashehd0c7d702007-07-03 13:27:22 -07004996 &last_eb_bh, OCFS2_BH_CACHED, inode);
4997 if (ret < 0) {
4998 mlog_errno(ret);
4999 goto out;
5000 }
5001
5002 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5003 rightmost_el = &eb->h_list;
5004 } else
5005 rightmost_el = path_leaf_el(path);
5006
Tao Ma811f9332008-08-18 17:38:43 +08005007 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005008 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005009 ret = ocfs2_extend_trans(handle, credits);
5010 if (ret) {
5011 mlog_errno(ret);
5012 goto out;
5013 }
5014
5015 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5016 le16_to_cpu(rightmost_el->l_count)) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005017 ret = ocfs2_grow_tree(inode, handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005018 meta_ac);
5019 if (ret) {
5020 mlog_errno(ret);
5021 goto out;
5022 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005023 }
5024
5025 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5026 insert.ins_appending = APPEND_NONE;
5027 insert.ins_contig = CONTIG_NONE;
5028 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005029 insert.ins_tree_depth = depth;
5030
Tao Mae7d4cb62008-08-18 17:38:44 +08005031 ret = ocfs2_do_insert_extent(inode, handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005032 if (ret)
5033 mlog_errno(ret);
5034
5035out:
5036 brelse(last_eb_bh);
5037 return ret;
5038}
5039
5040static int ocfs2_truncate_rec(struct inode *inode, handle_t *handle,
5041 struct ocfs2_path *path, int index,
5042 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08005043 u32 cpos, u32 len,
5044 struct ocfs2_extent_tree *et)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005045{
5046 int ret;
5047 u32 left_cpos, rec_range, trunc_range;
5048 int wants_rotate = 0, is_rightmost_tree_rec = 0;
5049 struct super_block *sb = inode->i_sb;
5050 struct ocfs2_path *left_path = NULL;
5051 struct ocfs2_extent_list *el = path_leaf_el(path);
5052 struct ocfs2_extent_rec *rec;
5053 struct ocfs2_extent_block *eb;
5054
5055 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Tao Mae7d4cb62008-08-18 17:38:44 +08005056 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005057 if (ret) {
5058 mlog_errno(ret);
5059 goto out;
5060 }
5061
5062 index--;
5063 }
5064
5065 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5066 path->p_tree_depth) {
5067 /*
5068 * Check whether this is the rightmost tree record. If
5069 * we remove all of this record or part of its right
5070 * edge then an update of the record lengths above it
5071 * will be required.
5072 */
5073 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5074 if (eb->h_next_leaf_blk == 0)
5075 is_rightmost_tree_rec = 1;
5076 }
5077
5078 rec = &el->l_recs[index];
5079 if (index == 0 && path->p_tree_depth &&
5080 le32_to_cpu(rec->e_cpos) == cpos) {
5081 /*
5082 * Changing the leftmost offset (via partial or whole
5083 * record truncate) of an interior (or rightmost) path
5084 * means we have to update the subtree that is formed
5085 * by this leaf and the one to it's left.
5086 *
5087 * There are two cases we can skip:
5088 * 1) Path is the leftmost one in our inode tree.
5089 * 2) The leaf is rightmost and will be empty after
5090 * we remove the extent record - the rotate code
5091 * knows how to update the newly formed edge.
5092 */
5093
5094 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path,
5095 &left_cpos);
5096 if (ret) {
5097 mlog_errno(ret);
5098 goto out;
5099 }
5100
5101 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
5102 left_path = ocfs2_new_path(path_root_bh(path),
5103 path_root_el(path));
5104 if (!left_path) {
5105 ret = -ENOMEM;
5106 mlog_errno(ret);
5107 goto out;
5108 }
5109
5110 ret = ocfs2_find_path(inode, left_path, left_cpos);
5111 if (ret) {
5112 mlog_errno(ret);
5113 goto out;
5114 }
5115 }
5116 }
5117
5118 ret = ocfs2_extend_rotate_transaction(handle, 0,
5119 handle->h_buffer_credits,
5120 path);
5121 if (ret) {
5122 mlog_errno(ret);
5123 goto out;
5124 }
5125
5126 ret = ocfs2_journal_access_path(inode, handle, path);
5127 if (ret) {
5128 mlog_errno(ret);
5129 goto out;
5130 }
5131
5132 ret = ocfs2_journal_access_path(inode, handle, left_path);
5133 if (ret) {
5134 mlog_errno(ret);
5135 goto out;
5136 }
5137
5138 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5139 trunc_range = cpos + len;
5140
5141 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5142 int next_free;
5143
5144 memset(rec, 0, sizeof(*rec));
5145 ocfs2_cleanup_merge(el, index);
5146 wants_rotate = 1;
5147
5148 next_free = le16_to_cpu(el->l_next_free_rec);
5149 if (is_rightmost_tree_rec && next_free > 1) {
5150 /*
5151 * We skip the edge update if this path will
5152 * be deleted by the rotate code.
5153 */
5154 rec = &el->l_recs[next_free - 1];
5155 ocfs2_adjust_rightmost_records(inode, handle, path,
5156 rec);
5157 }
5158 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5159 /* Remove leftmost portion of the record. */
5160 le32_add_cpu(&rec->e_cpos, len);
5161 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5162 le16_add_cpu(&rec->e_leaf_clusters, -len);
5163 } else if (rec_range == trunc_range) {
5164 /* Remove rightmost portion of the record */
5165 le16_add_cpu(&rec->e_leaf_clusters, -len);
5166 if (is_rightmost_tree_rec)
5167 ocfs2_adjust_rightmost_records(inode, handle, path, rec);
5168 } else {
5169 /* Caller should have trapped this. */
5170 mlog(ML_ERROR, "Inode %llu: Invalid record truncate: (%u, %u) "
5171 "(%u, %u)\n", (unsigned long long)OCFS2_I(inode)->ip_blkno,
5172 le32_to_cpu(rec->e_cpos),
5173 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5174 BUG();
5175 }
5176
5177 if (left_path) {
5178 int subtree_index;
5179
5180 subtree_index = ocfs2_find_subtree_root(inode, left_path, path);
5181 ocfs2_complete_edge_insert(inode, handle, left_path, path,
5182 subtree_index);
5183 }
5184
5185 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5186
Tao Mae7d4cb62008-08-18 17:38:44 +08005187 ret = ocfs2_rotate_tree_left(inode, handle, path, dealloc, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005188 if (ret) {
5189 mlog_errno(ret);
5190 goto out;
5191 }
5192
5193out:
5194 ocfs2_free_path(left_path);
5195 return ret;
5196}
5197
Tao Mae7d4cb62008-08-18 17:38:44 +08005198int ocfs2_remove_extent(struct inode *inode, struct buffer_head *root_bh,
Mark Fasheh063c4562007-07-03 13:34:11 -07005199 u32 cpos, u32 len, handle_t *handle,
5200 struct ocfs2_alloc_context *meta_ac,
Tao Mae7d4cb62008-08-18 17:38:44 +08005201 struct ocfs2_cached_dealloc_ctxt *dealloc,
Tao Maf56654c2008-08-18 17:38:48 +08005202 enum ocfs2_extent_tree_type et_type,
5203 void *private)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005204{
5205 int ret, index;
5206 u32 rec_range, trunc_range;
5207 struct ocfs2_extent_rec *rec;
5208 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005209 struct ocfs2_path *path = NULL;
5210 struct ocfs2_extent_tree *et = NULL;
5211
Tao Maca12b7c2008-08-18 17:38:52 +08005212 et = ocfs2_new_extent_tree(inode, root_bh, et_type, private);
Tao Mae7d4cb62008-08-18 17:38:44 +08005213 if (!et) {
5214 ret = -ENOMEM;
5215 mlog_errno(ret);
5216 goto out;
5217 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005218
5219 ocfs2_extent_map_trunc(inode, 0);
5220
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005221 path = ocfs2_new_path(et->et_root_bh, et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005222 if (!path) {
5223 ret = -ENOMEM;
5224 mlog_errno(ret);
5225 goto out;
5226 }
5227
5228 ret = ocfs2_find_path(inode, path, cpos);
5229 if (ret) {
5230 mlog_errno(ret);
5231 goto out;
5232 }
5233
5234 el = path_leaf_el(path);
5235 index = ocfs2_search_extent_list(el, cpos);
5236 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5237 ocfs2_error(inode->i_sb,
5238 "Inode %llu has an extent at cpos %u which can no "
5239 "longer be found.\n",
5240 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5241 ret = -EROFS;
5242 goto out;
5243 }
5244
5245 /*
5246 * We have 3 cases of extent removal:
5247 * 1) Range covers the entire extent rec
5248 * 2) Range begins or ends on one edge of the extent rec
5249 * 3) Range is in the middle of the extent rec (no shared edges)
5250 *
5251 * For case 1 we remove the extent rec and left rotate to
5252 * fill the hole.
5253 *
5254 * For case 2 we just shrink the existing extent rec, with a
5255 * tree update if the shrinking edge is also the edge of an
5256 * extent block.
5257 *
5258 * For case 3 we do a right split to turn the extent rec into
5259 * something case 2 can handle.
5260 */
5261 rec = &el->l_recs[index];
5262 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5263 trunc_range = cpos + len;
5264
5265 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5266
5267 mlog(0, "Inode %llu, remove (cpos %u, len %u). Existing index %d "
5268 "(cpos %u, len %u)\n",
5269 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos, len, index,
5270 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5271
5272 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
5273 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08005274 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005275 if (ret) {
5276 mlog_errno(ret);
5277 goto out;
5278 }
5279 } else {
Tao Mae7d4cb62008-08-18 17:38:44 +08005280 ret = ocfs2_split_tree(inode, et, handle, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005281 trunc_range, meta_ac);
5282 if (ret) {
5283 mlog_errno(ret);
5284 goto out;
5285 }
5286
5287 /*
5288 * The split could have manipulated the tree enough to
5289 * move the record location, so we have to look for it again.
5290 */
5291 ocfs2_reinit_path(path, 1);
5292
5293 ret = ocfs2_find_path(inode, path, cpos);
5294 if (ret) {
5295 mlog_errno(ret);
5296 goto out;
5297 }
5298
5299 el = path_leaf_el(path);
5300 index = ocfs2_search_extent_list(el, cpos);
5301 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5302 ocfs2_error(inode->i_sb,
5303 "Inode %llu: split at cpos %u lost record.",
5304 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5305 cpos);
5306 ret = -EROFS;
5307 goto out;
5308 }
5309
5310 /*
5311 * Double check our values here. If anything is fishy,
5312 * it's easier to catch it at the top level.
5313 */
5314 rec = &el->l_recs[index];
5315 rec_range = le32_to_cpu(rec->e_cpos) +
5316 ocfs2_rec_clusters(el, rec);
5317 if (rec_range != trunc_range) {
5318 ocfs2_error(inode->i_sb,
5319 "Inode %llu: error after split at cpos %u"
5320 "trunc len %u, existing record is (%u,%u)",
5321 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5322 cpos, len, le32_to_cpu(rec->e_cpos),
5323 ocfs2_rec_clusters(el, rec));
5324 ret = -EROFS;
5325 goto out;
5326 }
5327
5328 ret = ocfs2_truncate_rec(inode, handle, path, index, dealloc,
Tao Mae7d4cb62008-08-18 17:38:44 +08005329 cpos, len, et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005330 if (ret) {
5331 mlog_errno(ret);
5332 goto out;
5333 }
5334 }
5335
5336out:
5337 ocfs2_free_path(path);
Tao Mae7d4cb62008-08-18 17:38:44 +08005338 if (et)
5339 ocfs2_free_extent_tree(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005340 return ret;
5341}
5342
Mark Fasheh063c4562007-07-03 13:34:11 -07005343int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005344{
5345 struct buffer_head *tl_bh = osb->osb_tl_bh;
5346 struct ocfs2_dinode *di;
5347 struct ocfs2_truncate_log *tl;
5348
5349 di = (struct ocfs2_dinode *) tl_bh->b_data;
5350 tl = &di->id2.i_dealloc;
5351
5352 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5353 "slot %d, invalid truncate log parameters: used = "
5354 "%u, count = %u\n", osb->slot_num,
5355 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5356 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5357}
5358
5359static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5360 unsigned int new_start)
5361{
5362 unsigned int tail_index;
5363 unsigned int current_tail;
5364
5365 /* No records, nothing to coalesce */
5366 if (!le16_to_cpu(tl->tl_used))
5367 return 0;
5368
5369 tail_index = le16_to_cpu(tl->tl_used) - 1;
5370 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5371 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5372
5373 return current_tail == new_start;
5374}
5375
Mark Fasheh063c4562007-07-03 13:34:11 -07005376int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5377 handle_t *handle,
5378 u64 start_blk,
5379 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005380{
5381 int status, index;
5382 unsigned int start_cluster, tl_count;
5383 struct inode *tl_inode = osb->osb_tl_inode;
5384 struct buffer_head *tl_bh = osb->osb_tl_bh;
5385 struct ocfs2_dinode *di;
5386 struct ocfs2_truncate_log *tl;
5387
Mark Fashehb06970532006-03-03 10:24:33 -08005388 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5389 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005390
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005391 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005392
5393 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5394
5395 di = (struct ocfs2_dinode *) tl_bh->b_data;
5396 tl = &di->id2.i_dealloc;
5397 if (!OCFS2_IS_VALID_DINODE(di)) {
5398 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5399 status = -EIO;
5400 goto bail;
5401 }
5402
5403 tl_count = le16_to_cpu(tl->tl_count);
5404 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5405 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005406 "Truncate record count on #%llu invalid "
5407 "wanted %u, actual %u\n",
5408 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005409 ocfs2_truncate_recs_per_inode(osb->sb),
5410 le16_to_cpu(tl->tl_count));
5411
5412 /* Caller should have known to flush before calling us. */
5413 index = le16_to_cpu(tl->tl_used);
5414 if (index >= tl_count) {
5415 status = -ENOSPC;
5416 mlog_errno(status);
5417 goto bail;
5418 }
5419
5420 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5421 OCFS2_JOURNAL_ACCESS_WRITE);
5422 if (status < 0) {
5423 mlog_errno(status);
5424 goto bail;
5425 }
5426
5427 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005428 "%llu (index = %d)\n", num_clusters, start_cluster,
5429 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005430
5431 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5432 /*
5433 * Move index back to the record we are coalescing with.
5434 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5435 */
5436 index--;
5437
5438 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5439 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5440 index, le32_to_cpu(tl->tl_recs[index].t_start),
5441 num_clusters);
5442 } else {
5443 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5444 tl->tl_used = cpu_to_le16(index + 1);
5445 }
5446 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5447
5448 status = ocfs2_journal_dirty(handle, tl_bh);
5449 if (status < 0) {
5450 mlog_errno(status);
5451 goto bail;
5452 }
5453
5454bail:
5455 mlog_exit(status);
5456 return status;
5457}
5458
5459static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005460 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005461 struct inode *data_alloc_inode,
5462 struct buffer_head *data_alloc_bh)
5463{
5464 int status = 0;
5465 int i;
5466 unsigned int num_clusters;
5467 u64 start_blk;
5468 struct ocfs2_truncate_rec rec;
5469 struct ocfs2_dinode *di;
5470 struct ocfs2_truncate_log *tl;
5471 struct inode *tl_inode = osb->osb_tl_inode;
5472 struct buffer_head *tl_bh = osb->osb_tl_bh;
5473
5474 mlog_entry_void();
5475
5476 di = (struct ocfs2_dinode *) tl_bh->b_data;
5477 tl = &di->id2.i_dealloc;
5478 i = le16_to_cpu(tl->tl_used) - 1;
5479 while (i >= 0) {
5480 /* Caller has given us at least enough credits to
5481 * update the truncate log dinode */
5482 status = ocfs2_journal_access(handle, tl_inode, tl_bh,
5483 OCFS2_JOURNAL_ACCESS_WRITE);
5484 if (status < 0) {
5485 mlog_errno(status);
5486 goto bail;
5487 }
5488
5489 tl->tl_used = cpu_to_le16(i);
5490
5491 status = ocfs2_journal_dirty(handle, tl_bh);
5492 if (status < 0) {
5493 mlog_errno(status);
5494 goto bail;
5495 }
5496
5497 /* TODO: Perhaps we can calculate the bulk of the
5498 * credits up front rather than extending like
5499 * this. */
5500 status = ocfs2_extend_trans(handle,
5501 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5502 if (status < 0) {
5503 mlog_errno(status);
5504 goto bail;
5505 }
5506
5507 rec = tl->tl_recs[i];
5508 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5509 le32_to_cpu(rec.t_start));
5510 num_clusters = le32_to_cpu(rec.t_clusters);
5511
5512 /* if start_blk is not set, we ignore the record as
5513 * invalid. */
5514 if (start_blk) {
5515 mlog(0, "free record %d, start = %u, clusters = %u\n",
5516 i, le32_to_cpu(rec.t_start), num_clusters);
5517
5518 status = ocfs2_free_clusters(handle, data_alloc_inode,
5519 data_alloc_bh, start_blk,
5520 num_clusters);
5521 if (status < 0) {
5522 mlog_errno(status);
5523 goto bail;
5524 }
5525 }
5526 i--;
5527 }
5528
5529bail:
5530 mlog_exit(status);
5531 return status;
5532}
5533
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005534/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005535int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005536{
5537 int status;
5538 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005539 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005540 struct inode *tl_inode = osb->osb_tl_inode;
5541 struct inode *data_alloc_inode = NULL;
5542 struct buffer_head *tl_bh = osb->osb_tl_bh;
5543 struct buffer_head *data_alloc_bh = NULL;
5544 struct ocfs2_dinode *di;
5545 struct ocfs2_truncate_log *tl;
5546
5547 mlog_entry_void();
5548
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005549 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005550
5551 di = (struct ocfs2_dinode *) tl_bh->b_data;
5552 tl = &di->id2.i_dealloc;
5553 if (!OCFS2_IS_VALID_DINODE(di)) {
5554 OCFS2_RO_ON_INVALID_DINODE(osb->sb, di);
5555 status = -EIO;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005556 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005557 }
5558
5559 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005560 mlog(0, "Flush %u records from truncate log #%llu\n",
5561 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005562 if (!num_to_flush) {
5563 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005564 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005565 }
5566
5567 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5568 GLOBAL_BITMAP_SYSTEM_INODE,
5569 OCFS2_INVALID_SLOT);
5570 if (!data_alloc_inode) {
5571 status = -EINVAL;
5572 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005573 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005574 }
5575
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005576 mutex_lock(&data_alloc_inode->i_mutex);
5577
Mark Fashehe63aecb62007-10-18 15:30:42 -07005578 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005579 if (status < 0) {
5580 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005581 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005582 }
5583
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005584 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005585 if (IS_ERR(handle)) {
5586 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005587 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005588 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005589 }
5590
5591 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5592 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005593 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005594 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005595
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005596 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005597
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005598out_unlock:
5599 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005600 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005601
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005602out_mutex:
5603 mutex_unlock(&data_alloc_inode->i_mutex);
5604 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005605
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005606out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005607 mlog_exit(status);
5608 return status;
5609}
5610
5611int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5612{
5613 int status;
5614 struct inode *tl_inode = osb->osb_tl_inode;
5615
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005616 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005617 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005618 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005619
5620 return status;
5621}
5622
David Howellsc4028952006-11-22 14:57:56 +00005623static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005624{
5625 int status;
David Howellsc4028952006-11-22 14:57:56 +00005626 struct ocfs2_super *osb =
5627 container_of(work, struct ocfs2_super,
5628 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005629
5630 mlog_entry_void();
5631
5632 status = ocfs2_flush_truncate_log(osb);
5633 if (status < 0)
5634 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005635 else
5636 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005637
5638 mlog_exit(status);
5639}
5640
5641#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5642void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5643 int cancel)
5644{
5645 if (osb->osb_tl_inode) {
5646 /* We want to push off log flushes while truncates are
5647 * still running. */
5648 if (cancel)
5649 cancel_delayed_work(&osb->osb_truncate_log_wq);
5650
5651 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5652 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5653 }
5654}
5655
5656static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5657 int slot_num,
5658 struct inode **tl_inode,
5659 struct buffer_head **tl_bh)
5660{
5661 int status;
5662 struct inode *inode = NULL;
5663 struct buffer_head *bh = NULL;
5664
5665 inode = ocfs2_get_system_file_inode(osb,
5666 TRUNCATE_LOG_SYSTEM_INODE,
5667 slot_num);
5668 if (!inode) {
5669 status = -EINVAL;
5670 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5671 goto bail;
5672 }
5673
5674 status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh,
5675 OCFS2_BH_CACHED, inode);
5676 if (status < 0) {
5677 iput(inode);
5678 mlog_errno(status);
5679 goto bail;
5680 }
5681
5682 *tl_inode = inode;
5683 *tl_bh = bh;
5684bail:
5685 mlog_exit(status);
5686 return status;
5687}
5688
5689/* called during the 1st stage of node recovery. we stamp a clean
5690 * truncate log and pass back a copy for processing later. if the
5691 * truncate log does not require processing, a *tl_copy is set to
5692 * NULL. */
5693int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
5694 int slot_num,
5695 struct ocfs2_dinode **tl_copy)
5696{
5697 int status;
5698 struct inode *tl_inode = NULL;
5699 struct buffer_head *tl_bh = NULL;
5700 struct ocfs2_dinode *di;
5701 struct ocfs2_truncate_log *tl;
5702
5703 *tl_copy = NULL;
5704
5705 mlog(0, "recover truncate log from slot %d\n", slot_num);
5706
5707 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
5708 if (status < 0) {
5709 mlog_errno(status);
5710 goto bail;
5711 }
5712
5713 di = (struct ocfs2_dinode *) tl_bh->b_data;
5714 tl = &di->id2.i_dealloc;
5715 if (!OCFS2_IS_VALID_DINODE(di)) {
5716 OCFS2_RO_ON_INVALID_DINODE(tl_inode->i_sb, di);
5717 status = -EIO;
5718 goto bail;
5719 }
5720
5721 if (le16_to_cpu(tl->tl_used)) {
5722 mlog(0, "We'll have %u logs to recover\n",
5723 le16_to_cpu(tl->tl_used));
5724
5725 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
5726 if (!(*tl_copy)) {
5727 status = -ENOMEM;
5728 mlog_errno(status);
5729 goto bail;
5730 }
5731
5732 /* Assuming the write-out below goes well, this copy
5733 * will be passed back to recovery for processing. */
5734 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
5735
5736 /* All we need to do to clear the truncate log is set
5737 * tl_used. */
5738 tl->tl_used = 0;
5739
5740 status = ocfs2_write_block(osb, tl_bh, tl_inode);
5741 if (status < 0) {
5742 mlog_errno(status);
5743 goto bail;
5744 }
5745 }
5746
5747bail:
5748 if (tl_inode)
5749 iput(tl_inode);
5750 if (tl_bh)
5751 brelse(tl_bh);
5752
5753 if (status < 0 && (*tl_copy)) {
5754 kfree(*tl_copy);
5755 *tl_copy = NULL;
5756 }
5757
5758 mlog_exit(status);
5759 return status;
5760}
5761
5762int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
5763 struct ocfs2_dinode *tl_copy)
5764{
5765 int status = 0;
5766 int i;
5767 unsigned int clusters, num_recs, start_cluster;
5768 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005769 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005770 struct inode *tl_inode = osb->osb_tl_inode;
5771 struct ocfs2_truncate_log *tl;
5772
5773 mlog_entry_void();
5774
5775 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
5776 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
5777 return -EINVAL;
5778 }
5779
5780 tl = &tl_copy->id2.i_dealloc;
5781 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005782 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07005783 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08005784
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005785 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005786 for(i = 0; i < num_recs; i++) {
5787 if (ocfs2_truncate_log_needs_flush(osb)) {
5788 status = __ocfs2_flush_truncate_log(osb);
5789 if (status < 0) {
5790 mlog_errno(status);
5791 goto bail_up;
5792 }
5793 }
5794
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005795 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005796 if (IS_ERR(handle)) {
5797 status = PTR_ERR(handle);
5798 mlog_errno(status);
5799 goto bail_up;
5800 }
5801
5802 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
5803 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
5804 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
5805
5806 status = ocfs2_truncate_log_append(osb, handle,
5807 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005808 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005809 if (status < 0) {
5810 mlog_errno(status);
5811 goto bail_up;
5812 }
5813 }
5814
5815bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005816 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005817
5818 mlog_exit(status);
5819 return status;
5820}
5821
5822void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
5823{
5824 int status;
5825 struct inode *tl_inode = osb->osb_tl_inode;
5826
5827 mlog_entry_void();
5828
5829 if (tl_inode) {
5830 cancel_delayed_work(&osb->osb_truncate_log_wq);
5831 flush_workqueue(ocfs2_wq);
5832
5833 status = ocfs2_flush_truncate_log(osb);
5834 if (status < 0)
5835 mlog_errno(status);
5836
5837 brelse(osb->osb_tl_bh);
5838 iput(osb->osb_tl_inode);
5839 }
5840
5841 mlog_exit_void();
5842}
5843
5844int ocfs2_truncate_log_init(struct ocfs2_super *osb)
5845{
5846 int status;
5847 struct inode *tl_inode = NULL;
5848 struct buffer_head *tl_bh = NULL;
5849
5850 mlog_entry_void();
5851
5852 status = ocfs2_get_truncate_log_info(osb,
5853 osb->slot_num,
5854 &tl_inode,
5855 &tl_bh);
5856 if (status < 0)
5857 mlog_errno(status);
5858
5859 /* ocfs2_truncate_log_shutdown keys on the existence of
5860 * osb->osb_tl_inode so we don't set any of the osb variables
5861 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00005862 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
5863 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08005864 osb->osb_tl_bh = tl_bh;
5865 osb->osb_tl_inode = tl_inode;
5866
5867 mlog_exit(status);
5868 return status;
5869}
5870
Mark Fasheh2b604352007-06-22 15:45:27 -07005871/*
5872 * Delayed de-allocation of suballocator blocks.
5873 *
5874 * Some sets of block de-allocations might involve multiple suballocator inodes.
5875 *
5876 * The locking for this can get extremely complicated, especially when
5877 * the suballocator inodes to delete from aren't known until deep
5878 * within an unrelated codepath.
5879 *
5880 * ocfs2_extent_block structures are a good example of this - an inode
5881 * btree could have been grown by any number of nodes each allocating
5882 * out of their own suballoc inode.
5883 *
5884 * These structures allow the delay of block de-allocation until a
5885 * later time, when locking of multiple cluster inodes won't cause
5886 * deadlock.
5887 */
5888
5889/*
5890 * Describes a single block free from a suballocator
5891 */
5892struct ocfs2_cached_block_free {
5893 struct ocfs2_cached_block_free *free_next;
5894 u64 free_blk;
5895 unsigned int free_bit;
5896};
5897
5898struct ocfs2_per_slot_free_list {
5899 struct ocfs2_per_slot_free_list *f_next_suballocator;
5900 int f_inode_type;
5901 int f_slot;
5902 struct ocfs2_cached_block_free *f_first;
5903};
5904
5905static int ocfs2_free_cached_items(struct ocfs2_super *osb,
5906 int sysfile_type,
5907 int slot,
5908 struct ocfs2_cached_block_free *head)
5909{
5910 int ret;
5911 u64 bg_blkno;
5912 handle_t *handle;
5913 struct inode *inode;
5914 struct buffer_head *di_bh = NULL;
5915 struct ocfs2_cached_block_free *tmp;
5916
5917 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
5918 if (!inode) {
5919 ret = -EINVAL;
5920 mlog_errno(ret);
5921 goto out;
5922 }
5923
5924 mutex_lock(&inode->i_mutex);
5925
Mark Fashehe63aecb62007-10-18 15:30:42 -07005926 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07005927 if (ret) {
5928 mlog_errno(ret);
5929 goto out_mutex;
5930 }
5931
5932 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
5933 if (IS_ERR(handle)) {
5934 ret = PTR_ERR(handle);
5935 mlog_errno(ret);
5936 goto out_unlock;
5937 }
5938
5939 while (head) {
5940 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
5941 head->free_bit);
5942 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
5943 head->free_bit, (unsigned long long)head->free_blk);
5944
5945 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
5946 head->free_bit, bg_blkno, 1);
5947 if (ret) {
5948 mlog_errno(ret);
5949 goto out_journal;
5950 }
5951
5952 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
5953 if (ret) {
5954 mlog_errno(ret);
5955 goto out_journal;
5956 }
5957
5958 tmp = head;
5959 head = head->free_next;
5960 kfree(tmp);
5961 }
5962
5963out_journal:
5964 ocfs2_commit_trans(osb, handle);
5965
5966out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07005967 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07005968 brelse(di_bh);
5969out_mutex:
5970 mutex_unlock(&inode->i_mutex);
5971 iput(inode);
5972out:
5973 while(head) {
5974 /* Premature exit may have left some dangling items. */
5975 tmp = head;
5976 head = head->free_next;
5977 kfree(tmp);
5978 }
5979
5980 return ret;
5981}
5982
5983int ocfs2_run_deallocs(struct ocfs2_super *osb,
5984 struct ocfs2_cached_dealloc_ctxt *ctxt)
5985{
5986 int ret = 0, ret2;
5987 struct ocfs2_per_slot_free_list *fl;
5988
5989 if (!ctxt)
5990 return 0;
5991
5992 while (ctxt->c_first_suballocator) {
5993 fl = ctxt->c_first_suballocator;
5994
5995 if (fl->f_first) {
5996 mlog(0, "Free items: (type %u, slot %d)\n",
5997 fl->f_inode_type, fl->f_slot);
5998 ret2 = ocfs2_free_cached_items(osb, fl->f_inode_type,
5999 fl->f_slot, fl->f_first);
6000 if (ret2)
6001 mlog_errno(ret2);
6002 if (!ret)
6003 ret = ret2;
6004 }
6005
6006 ctxt->c_first_suballocator = fl->f_next_suballocator;
6007 kfree(fl);
6008 }
6009
6010 return ret;
6011}
6012
6013static struct ocfs2_per_slot_free_list *
6014ocfs2_find_per_slot_free_list(int type,
6015 int slot,
6016 struct ocfs2_cached_dealloc_ctxt *ctxt)
6017{
6018 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6019
6020 while (fl) {
6021 if (fl->f_inode_type == type && fl->f_slot == slot)
6022 return fl;
6023
6024 fl = fl->f_next_suballocator;
6025 }
6026
6027 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6028 if (fl) {
6029 fl->f_inode_type = type;
6030 fl->f_slot = slot;
6031 fl->f_first = NULL;
6032 fl->f_next_suballocator = ctxt->c_first_suballocator;
6033
6034 ctxt->c_first_suballocator = fl;
6035 }
6036 return fl;
6037}
6038
6039static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6040 int type, int slot, u64 blkno,
6041 unsigned int bit)
6042{
6043 int ret;
6044 struct ocfs2_per_slot_free_list *fl;
6045 struct ocfs2_cached_block_free *item;
6046
6047 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6048 if (fl == NULL) {
6049 ret = -ENOMEM;
6050 mlog_errno(ret);
6051 goto out;
6052 }
6053
6054 item = kmalloc(sizeof(*item), GFP_NOFS);
6055 if (item == NULL) {
6056 ret = -ENOMEM;
6057 mlog_errno(ret);
6058 goto out;
6059 }
6060
6061 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6062 type, slot, bit, (unsigned long long)blkno);
6063
6064 item->free_blk = blkno;
6065 item->free_bit = bit;
6066 item->free_next = fl->f_first;
6067
6068 fl->f_first = item;
6069
6070 ret = 0;
6071out:
6072 return ret;
6073}
6074
Mark Fasheh59a5e412007-06-22 15:52:36 -07006075static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6076 struct ocfs2_extent_block *eb)
6077{
6078 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6079 le16_to_cpu(eb->h_suballoc_slot),
6080 le64_to_cpu(eb->h_blkno),
6081 le16_to_cpu(eb->h_suballoc_bit));
6082}
6083
Mark Fashehccd979b2005-12-15 14:31:24 -08006084/* This function will figure out whether the currently last extent
6085 * block will be deleted, and if it will, what the new last extent
6086 * block will be so we can update his h_next_leaf_blk field, as well
6087 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006088static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006089 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006090 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006091 struct buffer_head **new_last_eb)
6092{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006093 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006094 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006095 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006096 struct ocfs2_extent_block *eb;
6097 struct ocfs2_extent_list *el;
6098 struct buffer_head *bh = NULL;
6099
6100 *new_last_eb = NULL;
6101
Mark Fashehccd979b2005-12-15 14:31:24 -08006102 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006103 if (!path->p_tree_depth)
6104 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006105
6106 /* trunc to zero special case - this makes tree_depth = 0
6107 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006108 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006109 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006110
Mark Fashehdcd05382007-01-16 11:32:23 -08006111 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006112 BUG_ON(!el->l_next_free_rec);
6113
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006114 /*
6115 * Make sure that this extent list will actually be empty
6116 * after we clear away the data. We can shortcut out if
6117 * there's more than one non-empty extent in the
6118 * list. Otherwise, a check of the remaining extent is
6119 * necessary.
6120 */
6121 next_free = le16_to_cpu(el->l_next_free_rec);
6122 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006123 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006124 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006125 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006126
6127 /* We may have a valid extent in index 1, check it. */
6128 if (next_free == 2)
6129 rec = &el->l_recs[1];
6130
6131 /*
6132 * Fall through - no more nonempty extents, so we want
6133 * to delete this leaf.
6134 */
6135 } else {
6136 if (next_free > 1)
6137 goto out;
6138
6139 rec = &el->l_recs[0];
6140 }
6141
6142 if (rec) {
6143 /*
6144 * Check it we'll only be trimming off the end of this
6145 * cluster.
6146 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006147 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006148 goto out;
6149 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006150
Mark Fashehdcd05382007-01-16 11:32:23 -08006151 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6152 if (ret) {
6153 mlog_errno(ret);
6154 goto out;
6155 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006156
Mark Fashehdcd05382007-01-16 11:32:23 -08006157 ret = ocfs2_find_leaf(inode, path_root_el(path), cpos, &bh);
6158 if (ret) {
6159 mlog_errno(ret);
6160 goto out;
6161 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006162
Mark Fashehdcd05382007-01-16 11:32:23 -08006163 eb = (struct ocfs2_extent_block *) bh->b_data;
6164 el = &eb->h_list;
6165 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
6166 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
6167 ret = -EROFS;
6168 goto out;
6169 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006170
6171 *new_last_eb = bh;
6172 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006173 mlog(0, "returning block %llu, (cpos: %u)\n",
6174 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6175out:
6176 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006177
Mark Fashehdcd05382007-01-16 11:32:23 -08006178 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006179}
6180
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006181/*
6182 * Trim some clusters off the rightmost edge of a tree. Only called
6183 * during truncate.
6184 *
6185 * The caller needs to:
6186 * - start journaling of each path component.
6187 * - compute and fully set up any new last ext block
6188 */
6189static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6190 handle_t *handle, struct ocfs2_truncate_context *tc,
6191 u32 clusters_to_del, u64 *delete_start)
6192{
6193 int ret, i, index = path->p_tree_depth;
6194 u32 new_edge = 0;
6195 u64 deleted_eb = 0;
6196 struct buffer_head *bh;
6197 struct ocfs2_extent_list *el;
6198 struct ocfs2_extent_rec *rec;
6199
6200 *delete_start = 0;
6201
6202 while (index >= 0) {
6203 bh = path->p_node[index].bh;
6204 el = path->p_node[index].el;
6205
6206 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6207 index, (unsigned long long)bh->b_blocknr);
6208
6209 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6210
6211 if (index !=
6212 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6213 ocfs2_error(inode->i_sb,
6214 "Inode %lu has invalid ext. block %llu",
6215 inode->i_ino,
6216 (unsigned long long)bh->b_blocknr);
6217 ret = -EROFS;
6218 goto out;
6219 }
6220
6221find_tail_record:
6222 i = le16_to_cpu(el->l_next_free_rec) - 1;
6223 rec = &el->l_recs[i];
6224
6225 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6226 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006227 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006228 (unsigned long long)le64_to_cpu(rec->e_blkno),
6229 le16_to_cpu(el->l_next_free_rec));
6230
Mark Fashehe48edee2007-03-07 16:46:57 -08006231 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006232
6233 if (le16_to_cpu(el->l_tree_depth) == 0) {
6234 /*
6235 * If the leaf block contains a single empty
6236 * extent and no records, we can just remove
6237 * the block.
6238 */
6239 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6240 memset(rec, 0,
6241 sizeof(struct ocfs2_extent_rec));
6242 el->l_next_free_rec = cpu_to_le16(0);
6243
6244 goto delete;
6245 }
6246
6247 /*
6248 * Remove any empty extents by shifting things
6249 * left. That should make life much easier on
6250 * the code below. This condition is rare
6251 * enough that we shouldn't see a performance
6252 * hit.
6253 */
6254 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6255 le16_add_cpu(&el->l_next_free_rec, -1);
6256
6257 for(i = 0;
6258 i < le16_to_cpu(el->l_next_free_rec); i++)
6259 el->l_recs[i] = el->l_recs[i + 1];
6260
6261 memset(&el->l_recs[i], 0,
6262 sizeof(struct ocfs2_extent_rec));
6263
6264 /*
6265 * We've modified our extent list. The
6266 * simplest way to handle this change
6267 * is to being the search from the
6268 * start again.
6269 */
6270 goto find_tail_record;
6271 }
6272
Mark Fashehe48edee2007-03-07 16:46:57 -08006273 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006274
6275 /*
6276 * We'll use "new_edge" on our way back up the
6277 * tree to know what our rightmost cpos is.
6278 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006279 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006280 new_edge += le32_to_cpu(rec->e_cpos);
6281
6282 /*
6283 * The caller will use this to delete data blocks.
6284 */
6285 *delete_start = le64_to_cpu(rec->e_blkno)
6286 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006287 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006288
6289 /*
6290 * If it's now empty, remove this record.
6291 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006292 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006293 memset(rec, 0,
6294 sizeof(struct ocfs2_extent_rec));
6295 le16_add_cpu(&el->l_next_free_rec, -1);
6296 }
6297 } else {
6298 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6299 memset(rec, 0,
6300 sizeof(struct ocfs2_extent_rec));
6301 le16_add_cpu(&el->l_next_free_rec, -1);
6302
6303 goto delete;
6304 }
6305
6306 /* Can this actually happen? */
6307 if (le16_to_cpu(el->l_next_free_rec) == 0)
6308 goto delete;
6309
6310 /*
6311 * We never actually deleted any clusters
6312 * because our leaf was empty. There's no
6313 * reason to adjust the rightmost edge then.
6314 */
6315 if (new_edge == 0)
6316 goto delete;
6317
Mark Fashehe48edee2007-03-07 16:46:57 -08006318 rec->e_int_clusters = cpu_to_le32(new_edge);
6319 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006320 -le32_to_cpu(rec->e_cpos));
6321
6322 /*
6323 * A deleted child record should have been
6324 * caught above.
6325 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006326 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006327 }
6328
6329delete:
6330 ret = ocfs2_journal_dirty(handle, bh);
6331 if (ret) {
6332 mlog_errno(ret);
6333 goto out;
6334 }
6335
6336 mlog(0, "extent list container %llu, after: record %d: "
6337 "(%u, %u, %llu), next = %u.\n",
6338 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006339 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006340 (unsigned long long)le64_to_cpu(rec->e_blkno),
6341 le16_to_cpu(el->l_next_free_rec));
6342
6343 /*
6344 * We must be careful to only attempt delete of an
6345 * extent block (and not the root inode block).
6346 */
6347 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6348 struct ocfs2_extent_block *eb =
6349 (struct ocfs2_extent_block *)bh->b_data;
6350
6351 /*
6352 * Save this for use when processing the
6353 * parent block.
6354 */
6355 deleted_eb = le64_to_cpu(eb->h_blkno);
6356
6357 mlog(0, "deleting this extent block.\n");
6358
6359 ocfs2_remove_from_cache(inode, bh);
6360
Mark Fashehe48edee2007-03-07 16:46:57 -08006361 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006362 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6363 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6364
Mark Fasheh59a5e412007-06-22 15:52:36 -07006365 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6366 /* An error here is not fatal. */
6367 if (ret < 0)
6368 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006369 } else {
6370 deleted_eb = 0;
6371 }
6372
6373 index--;
6374 }
6375
6376 ret = 0;
6377out:
6378 return ret;
6379}
6380
Mark Fashehccd979b2005-12-15 14:31:24 -08006381static int ocfs2_do_truncate(struct ocfs2_super *osb,
6382 unsigned int clusters_to_del,
6383 struct inode *inode,
6384 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006385 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006386 struct ocfs2_truncate_context *tc,
6387 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006388{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006389 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006390 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006391 struct ocfs2_extent_block *last_eb = NULL;
6392 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006393 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006394 u64 delete_blk = 0;
6395
6396 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6397
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006398 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006399 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006400 if (status < 0) {
6401 mlog_errno(status);
6402 goto bail;
6403 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006404
Mark Fashehdcd05382007-01-16 11:32:23 -08006405 /*
6406 * Each component will be touched, so we might as well journal
6407 * here to avoid having to handle errors later.
6408 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006409 status = ocfs2_journal_access_path(inode, handle, path);
6410 if (status < 0) {
6411 mlog_errno(status);
6412 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006413 }
6414
6415 if (last_eb_bh) {
6416 status = ocfs2_journal_access(handle, inode, last_eb_bh,
6417 OCFS2_JOURNAL_ACCESS_WRITE);
6418 if (status < 0) {
6419 mlog_errno(status);
6420 goto bail;
6421 }
6422
6423 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6424 }
6425
6426 el = &(fe->id2.i_list);
6427
6428 /*
6429 * Lower levels depend on this never happening, but it's best
6430 * to check it up here before changing the tree.
6431 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006432 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006433 ocfs2_error(inode->i_sb,
6434 "Inode %lu has an empty extent record, depth %u\n",
6435 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006436 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006437 goto bail;
6438 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006439
6440 spin_lock(&OCFS2_I(inode)->ip_lock);
6441 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6442 clusters_to_del;
6443 spin_unlock(&OCFS2_I(inode)->ip_lock);
6444 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006445 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006446
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006447 status = ocfs2_trim_tree(inode, path, handle, tc,
6448 clusters_to_del, &delete_blk);
6449 if (status) {
6450 mlog_errno(status);
6451 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006452 }
6453
Mark Fashehdcd05382007-01-16 11:32:23 -08006454 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006455 /* trunc to zero is a special case. */
6456 el->l_tree_depth = 0;
6457 fe->i_last_eb_blk = 0;
6458 } else if (last_eb)
6459 fe->i_last_eb_blk = last_eb->h_blkno;
6460
6461 status = ocfs2_journal_dirty(handle, fe_bh);
6462 if (status < 0) {
6463 mlog_errno(status);
6464 goto bail;
6465 }
6466
6467 if (last_eb) {
6468 /* If there will be a new last extent block, then by
6469 * definition, there cannot be any leaves to the right of
6470 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006471 last_eb->h_next_leaf_blk = 0;
6472 status = ocfs2_journal_dirty(handle, last_eb_bh);
6473 if (status < 0) {
6474 mlog_errno(status);
6475 goto bail;
6476 }
6477 }
6478
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006479 if (delete_blk) {
6480 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6481 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006482 if (status < 0) {
6483 mlog_errno(status);
6484 goto bail;
6485 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006486 }
6487 status = 0;
6488bail:
Mark Fashehdcd05382007-01-16 11:32:23 -08006489
Mark Fashehccd979b2005-12-15 14:31:24 -08006490 mlog_exit(status);
6491 return status;
6492}
6493
Mark Fasheh60b11392007-02-16 11:46:50 -08006494static int ocfs2_writeback_zero_func(handle_t *handle, struct buffer_head *bh)
6495{
6496 set_buffer_uptodate(bh);
6497 mark_buffer_dirty(bh);
6498 return 0;
6499}
6500
6501static int ocfs2_ordered_zero_func(handle_t *handle, struct buffer_head *bh)
6502{
6503 set_buffer_uptodate(bh);
6504 mark_buffer_dirty(bh);
6505 return ocfs2_journal_dirty_data(handle, bh);
6506}
6507
Mark Fasheh1d410a62007-09-07 14:20:45 -07006508static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6509 unsigned int from, unsigned int to,
6510 struct page *page, int zero, u64 *phys)
6511{
6512 int ret, partial = 0;
6513
6514 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6515 if (ret)
6516 mlog_errno(ret);
6517
6518 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006519 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006520
6521 /*
6522 * Need to set the buffers we zero'd into uptodate
6523 * here if they aren't - ocfs2_map_page_blocks()
6524 * might've skipped some
6525 */
6526 if (ocfs2_should_order_data(inode)) {
6527 ret = walk_page_buffers(handle,
6528 page_buffers(page),
6529 from, to, &partial,
6530 ocfs2_ordered_zero_func);
6531 if (ret < 0)
6532 mlog_errno(ret);
6533 } else {
6534 ret = walk_page_buffers(handle, page_buffers(page),
6535 from, to, &partial,
6536 ocfs2_writeback_zero_func);
6537 if (ret < 0)
6538 mlog_errno(ret);
6539 }
6540
6541 if (!partial)
6542 SetPageUptodate(page);
6543
6544 flush_dcache_page(page);
6545}
6546
Mark Fasheh35edec12007-07-06 14:41:18 -07006547static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6548 loff_t end, struct page **pages,
6549 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006550{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006551 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006552 struct page *page;
6553 unsigned int from, to = PAGE_CACHE_SIZE;
6554 struct super_block *sb = inode->i_sb;
6555
6556 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6557
6558 if (numpages == 0)
6559 goto out;
6560
Mark Fasheh35edec12007-07-06 14:41:18 -07006561 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006562 for(i = 0; i < numpages; i++) {
6563 page = pages[i];
6564
Mark Fasheh35edec12007-07-06 14:41:18 -07006565 from = start & (PAGE_CACHE_SIZE - 1);
6566 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6567 to = end & (PAGE_CACHE_SIZE - 1);
6568
Mark Fasheh60b11392007-02-16 11:46:50 -08006569 BUG_ON(from > PAGE_CACHE_SIZE);
6570 BUG_ON(to > PAGE_CACHE_SIZE);
6571
Mark Fasheh1d410a62007-09-07 14:20:45 -07006572 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6573 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006574
Mark Fasheh35edec12007-07-06 14:41:18 -07006575 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006576 }
6577out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006578 if (pages)
6579 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006580}
6581
Mark Fasheh35edec12007-07-06 14:41:18 -07006582static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006583 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006584{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006585 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006586 struct super_block *sb = inode->i_sb;
6587 struct address_space *mapping = inode->i_mapping;
6588 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006589 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006590
Mark Fasheh35edec12007-07-06 14:41:18 -07006591 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006592
Mark Fasheh35edec12007-07-06 14:41:18 -07006593 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6594 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6595
Mark Fasheh1d410a62007-09-07 14:20:45 -07006596 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006597 last_page_bytes = PAGE_ALIGN(end);
6598 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006599 do {
6600 pages[numpages] = grab_cache_page(mapping, index);
6601 if (!pages[numpages]) {
6602 ret = -ENOMEM;
6603 mlog_errno(ret);
6604 goto out;
6605 }
6606
6607 numpages++;
6608 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006609 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006610
6611out:
6612 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006613 if (pages)
6614 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006615 numpages = 0;
6616 }
6617
6618 *num = numpages;
6619
6620 return ret;
6621}
6622
6623/*
6624 * Zero the area past i_size but still within an allocated
6625 * cluster. This avoids exposing nonzero data on subsequent file
6626 * extends.
6627 *
6628 * We need to call this before i_size is updated on the inode because
6629 * otherwise block_write_full_page() will skip writeout of pages past
6630 * i_size. The new_i_size parameter is passed for this reason.
6631 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006632int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6633 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006634{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006635 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006636 struct page **pages = NULL;
6637 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006638 unsigned int ext_flags;
6639 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006640
6641 /*
6642 * File systems which don't support sparse files zero on every
6643 * extend.
6644 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07006645 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08006646 return 0;
6647
Mark Fasheh1d410a62007-09-07 14:20:45 -07006648 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08006649 sizeof(struct page *), GFP_NOFS);
6650 if (pages == NULL) {
6651 ret = -ENOMEM;
6652 mlog_errno(ret);
6653 goto out;
6654 }
6655
Mark Fasheh1d410a62007-09-07 14:20:45 -07006656 if (range_start == range_end)
6657 goto out;
6658
6659 ret = ocfs2_extent_map_get_blocks(inode,
6660 range_start >> sb->s_blocksize_bits,
6661 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08006662 if (ret) {
6663 mlog_errno(ret);
6664 goto out;
6665 }
6666
Mark Fasheh1d410a62007-09-07 14:20:45 -07006667 /*
6668 * Tail is a hole, or is marked unwritten. In either case, we
6669 * can count on read and write to return/push zero's.
6670 */
6671 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08006672 goto out;
6673
Mark Fasheh1d410a62007-09-07 14:20:45 -07006674 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6675 &numpages);
6676 if (ret) {
6677 mlog_errno(ret);
6678 goto out;
6679 }
6680
Mark Fasheh35edec12007-07-06 14:41:18 -07006681 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6682 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08006683
6684 /*
6685 * Initiate writeout of the pages we zero'd here. We don't
6686 * wait on them - the truncate_inode_pages() call later will
6687 * do that for us.
6688 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006689 ret = do_sync_mapping_range(inode->i_mapping, range_start,
6690 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08006691 if (ret)
6692 mlog_errno(ret);
6693
6694out:
6695 if (pages)
6696 kfree(pages);
6697
6698 return ret;
6699}
6700
Tiger Yangfdd77702008-08-18 17:08:55 +08006701static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6702 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006703{
6704 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08006705 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006706
Tiger Yangfdd77702008-08-18 17:08:55 +08006707 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6708 memset(&di->id2, 0, blocksize -
6709 offsetof(struct ocfs2_dinode, id2) -
6710 xattrsize);
6711 else
6712 memset(&di->id2, 0, blocksize -
6713 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006714}
6715
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006716void ocfs2_dinode_new_extent_list(struct inode *inode,
6717 struct ocfs2_dinode *di)
6718{
Tiger Yangfdd77702008-08-18 17:08:55 +08006719 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006720 di->id2.i_list.l_tree_depth = 0;
6721 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08006722 di->id2.i_list.l_count = cpu_to_le16(
6723 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006724}
6725
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006726void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6727{
6728 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6729 struct ocfs2_inline_data *idata = &di->id2.i_data;
6730
6731 spin_lock(&oi->ip_lock);
6732 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6733 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6734 spin_unlock(&oi->ip_lock);
6735
6736 /*
6737 * We clear the entire i_data structure here so that all
6738 * fields can be properly initialized.
6739 */
Tiger Yangfdd77702008-08-18 17:08:55 +08006740 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006741
Tiger Yangfdd77702008-08-18 17:08:55 +08006742 idata->id_count = cpu_to_le16(
6743 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006744}
6745
6746int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6747 struct buffer_head *di_bh)
6748{
6749 int ret, i, has_data, num_pages = 0;
6750 handle_t *handle;
6751 u64 uninitialized_var(block);
6752 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6753 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6754 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006755 struct ocfs2_alloc_context *data_ac = NULL;
6756 struct page **pages = NULL;
6757 loff_t end = osb->s_clustersize;
6758
6759 has_data = i_size_read(inode) ? 1 : 0;
6760
6761 if (has_data) {
6762 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6763 sizeof(struct page *), GFP_NOFS);
6764 if (pages == NULL) {
6765 ret = -ENOMEM;
6766 mlog_errno(ret);
6767 goto out;
6768 }
6769
6770 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6771 if (ret) {
6772 mlog_errno(ret);
6773 goto out;
6774 }
6775 }
6776
6777 handle = ocfs2_start_trans(osb, OCFS2_INLINE_TO_EXTENTS_CREDITS);
6778 if (IS_ERR(handle)) {
6779 ret = PTR_ERR(handle);
6780 mlog_errno(ret);
6781 goto out_unlock;
6782 }
6783
6784 ret = ocfs2_journal_access(handle, inode, di_bh,
6785 OCFS2_JOURNAL_ACCESS_WRITE);
6786 if (ret) {
6787 mlog_errno(ret);
6788 goto out_commit;
6789 }
6790
6791 if (has_data) {
6792 u32 bit_off, num;
6793 unsigned int page_end;
6794 u64 phys;
6795
6796 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
6797 &num);
6798 if (ret) {
6799 mlog_errno(ret);
6800 goto out_commit;
6801 }
6802
6803 /*
6804 * Save two copies, one for insert, and one that can
6805 * be changed by ocfs2_map_and_dirty_page() below.
6806 */
6807 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6808
6809 /*
6810 * Non sparse file systems zero on extend, so no need
6811 * to do that now.
6812 */
6813 if (!ocfs2_sparse_alloc(osb) &&
6814 PAGE_CACHE_SIZE < osb->s_clustersize)
6815 end = PAGE_CACHE_SIZE;
6816
6817 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6818 if (ret) {
6819 mlog_errno(ret);
6820 goto out_commit;
6821 }
6822
6823 /*
6824 * This should populate the 1st page for us and mark
6825 * it up to date.
6826 */
6827 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6828 if (ret) {
6829 mlog_errno(ret);
6830 goto out_commit;
6831 }
6832
6833 page_end = PAGE_CACHE_SIZE;
6834 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6835 page_end = osb->s_clustersize;
6836
6837 for (i = 0; i < num_pages; i++)
6838 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6839 pages[i], i > 0, &phys);
6840 }
6841
6842 spin_lock(&oi->ip_lock);
6843 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6844 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6845 spin_unlock(&oi->ip_lock);
6846
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006847 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006848
6849 ocfs2_journal_dirty(handle, di_bh);
6850
6851 if (has_data) {
6852 /*
6853 * An error at this point should be extremely rare. If
6854 * this proves to be false, we could always re-build
6855 * the in-inode data from our pages.
6856 */
Tao Maf56654c2008-08-18 17:38:48 +08006857 ret = ocfs2_dinode_insert_extent(osb, handle, inode, di_bh,
6858 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006859 if (ret) {
6860 mlog_errno(ret);
6861 goto out_commit;
6862 }
6863
6864 inode->i_blocks = ocfs2_inode_sector_count(inode);
6865 }
6866
6867out_commit:
6868 ocfs2_commit_trans(osb, handle);
6869
6870out_unlock:
6871 if (data_ac)
6872 ocfs2_free_alloc_context(data_ac);
6873
6874out:
6875 if (pages) {
6876 ocfs2_unlock_and_free_pages(pages, num_pages);
6877 kfree(pages);
6878 }
6879
6880 return ret;
6881}
6882
Mark Fashehccd979b2005-12-15 14:31:24 -08006883/*
6884 * It is expected, that by the time you call this function,
6885 * inode->i_size and fe->i_size have been adjusted.
6886 *
6887 * WARNING: This will kfree the truncate context
6888 */
6889int ocfs2_commit_truncate(struct ocfs2_super *osb,
6890 struct inode *inode,
6891 struct buffer_head *fe_bh,
6892 struct ocfs2_truncate_context *tc)
6893{
6894 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006895 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08006896 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006897 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006898 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08006899 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08006900 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08006901
6902 mlog_entry_void();
6903
Mark Fashehdcd05382007-01-16 11:32:23 -08006904 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08006905 i_size_read(inode));
6906
Tao Mae7d4cb62008-08-18 17:38:44 +08006907 path = ocfs2_new_path(fe_bh, &di->id2.i_list);
Mark Fashehdcd05382007-01-16 11:32:23 -08006908 if (!path) {
6909 status = -ENOMEM;
6910 mlog_errno(status);
6911 goto bail;
6912 }
Mark Fasheh83418972007-04-23 18:53:12 -07006913
6914 ocfs2_extent_map_trunc(inode, new_highest_cpos);
6915
Mark Fashehccd979b2005-12-15 14:31:24 -08006916start:
Mark Fashehdcd05382007-01-16 11:32:23 -08006917 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006918 * Check that we still have allocation to delete.
6919 */
6920 if (OCFS2_I(inode)->ip_clusters == 0) {
6921 status = 0;
6922 goto bail;
6923 }
6924
6925 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08006926 * Truncate always works against the rightmost tree branch.
6927 */
6928 status = ocfs2_find_path(inode, path, UINT_MAX);
6929 if (status) {
6930 mlog_errno(status);
6931 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006932 }
6933
Mark Fashehdcd05382007-01-16 11:32:23 -08006934 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
6935 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
6936
6937 /*
6938 * By now, el will point to the extent list on the bottom most
6939 * portion of this tree. Only the tail record is considered in
6940 * each pass.
6941 *
6942 * We handle the following cases, in order:
6943 * - empty extent: delete the remaining branch
6944 * - remove the entire record
6945 * - remove a partial record
6946 * - no record needs to be removed (truncate has completed)
6947 */
6948 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006949 if (le16_to_cpu(el->l_next_free_rec) == 0) {
6950 ocfs2_error(inode->i_sb,
6951 "Inode %llu has empty extent block at %llu\n",
6952 (unsigned long long)OCFS2_I(inode)->ip_blkno,
6953 (unsigned long long)path_leaf_bh(path)->b_blocknr);
6954 status = -EROFS;
6955 goto bail;
6956 }
6957
Mark Fashehccd979b2005-12-15 14:31:24 -08006958 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08006959 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08006960 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08006961 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
6962 clusters_to_del = 0;
6963 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08006964 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08006965 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08006966 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08006967 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08006968 new_highest_cpos;
6969 } else {
6970 status = 0;
6971 goto bail;
6972 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006973
Mark Fashehdcd05382007-01-16 11:32:23 -08006974 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
6975 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
6976
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006977 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006978 tl_sem = 1;
6979 /* ocfs2_truncate_log_needs_flush guarantees us at least one
6980 * record is free for use. If there isn't any, we flush to get
6981 * an empty truncate log. */
6982 if (ocfs2_truncate_log_needs_flush(osb)) {
6983 status = __ocfs2_flush_truncate_log(osb);
6984 if (status < 0) {
6985 mlog_errno(status);
6986 goto bail;
6987 }
6988 }
6989
6990 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006991 (struct ocfs2_dinode *)fe_bh->b_data,
6992 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006993 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08006994 if (IS_ERR(handle)) {
6995 status = PTR_ERR(handle);
6996 handle = NULL;
6997 mlog_errno(status);
6998 goto bail;
6999 }
7000
Mark Fashehdcd05382007-01-16 11:32:23 -08007001 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7002 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007003 if (status < 0) {
7004 mlog_errno(status);
7005 goto bail;
7006 }
7007
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007008 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007009 tl_sem = 0;
7010
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007011 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007012 handle = NULL;
7013
Mark Fashehdcd05382007-01-16 11:32:23 -08007014 ocfs2_reinit_path(path, 1);
7015
7016 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007017 * The check above will catch the case where we've truncated
7018 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007019 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007020 goto start;
7021
Mark Fashehccd979b2005-12-15 14:31:24 -08007022bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007023
7024 ocfs2_schedule_truncate_log_flush(osb, 1);
7025
7026 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007027 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007028
7029 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007030 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007031
Mark Fasheh59a5e412007-06-22 15:52:36 -07007032 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7033
Mark Fashehdcd05382007-01-16 11:32:23 -08007034 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007035
7036 /* This will drop the ext_alloc cluster lock for us */
7037 ocfs2_free_truncate_context(tc);
7038
7039 mlog_exit(status);
7040 return status;
7041}
7042
Mark Fashehccd979b2005-12-15 14:31:24 -08007043/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007044 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007045 */
7046int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7047 struct inode *inode,
7048 struct buffer_head *fe_bh,
7049 struct ocfs2_truncate_context **tc)
7050{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007051 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007052 unsigned int new_i_clusters;
7053 struct ocfs2_dinode *fe;
7054 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007055 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007056
7057 mlog_entry_void();
7058
7059 *tc = NULL;
7060
7061 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7062 i_size_read(inode));
7063 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7064
7065 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007066 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7067 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007068
Robert P. J. Daycd861282006-12-13 00:34:52 -08007069 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007070 if (!(*tc)) {
7071 status = -ENOMEM;
7072 mlog_errno(status);
7073 goto bail;
7074 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007075 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007076
Mark Fashehccd979b2005-12-15 14:31:24 -08007077 if (fe->id2.i_list.l_tree_depth) {
Mark Fashehccd979b2005-12-15 14:31:24 -08007078 status = ocfs2_read_block(osb, le64_to_cpu(fe->i_last_eb_blk),
7079 &last_eb_bh, OCFS2_BH_CACHED, inode);
7080 if (status < 0) {
7081 mlog_errno(status);
7082 goto bail;
7083 }
7084 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
7085 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
7086 OCFS2_RO_ON_INVALID_EXTENT_BLOCK(inode->i_sb, eb);
7087
7088 brelse(last_eb_bh);
7089 status = -EIO;
7090 goto bail;
7091 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007092 }
7093
7094 (*tc)->tc_last_eb_bh = last_eb_bh;
7095
Mark Fashehccd979b2005-12-15 14:31:24 -08007096 status = 0;
7097bail:
7098 if (status < 0) {
7099 if (*tc)
7100 ocfs2_free_truncate_context(*tc);
7101 *tc = NULL;
7102 }
7103 mlog_exit_void();
7104 return status;
7105}
7106
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007107/*
7108 * 'start' is inclusive, 'end' is not.
7109 */
7110int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7111 unsigned int start, unsigned int end, int trunc)
7112{
7113 int ret;
7114 unsigned int numbytes;
7115 handle_t *handle;
7116 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7117 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7118 struct ocfs2_inline_data *idata = &di->id2.i_data;
7119
7120 if (end > i_size_read(inode))
7121 end = i_size_read(inode);
7122
7123 BUG_ON(start >= end);
7124
7125 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7126 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7127 !ocfs2_supports_inline_data(osb)) {
7128 ocfs2_error(inode->i_sb,
7129 "Inline data flags for inode %llu don't agree! "
7130 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7131 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7132 le16_to_cpu(di->i_dyn_features),
7133 OCFS2_I(inode)->ip_dyn_features,
7134 osb->s_feature_incompat);
7135 ret = -EROFS;
7136 goto out;
7137 }
7138
7139 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7140 if (IS_ERR(handle)) {
7141 ret = PTR_ERR(handle);
7142 mlog_errno(ret);
7143 goto out;
7144 }
7145
7146 ret = ocfs2_journal_access(handle, inode, di_bh,
7147 OCFS2_JOURNAL_ACCESS_WRITE);
7148 if (ret) {
7149 mlog_errno(ret);
7150 goto out_commit;
7151 }
7152
7153 numbytes = end - start;
7154 memset(idata->id_data + start, 0, numbytes);
7155
7156 /*
7157 * No need to worry about the data page here - it's been
7158 * truncated already and inline data doesn't need it for
7159 * pushing zero's to disk, so we'll let readpage pick it up
7160 * later.
7161 */
7162 if (trunc) {
7163 i_size_write(inode, start);
7164 di->i_size = cpu_to_le64(start);
7165 }
7166
7167 inode->i_blocks = ocfs2_inode_sector_count(inode);
7168 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7169
7170 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7171 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7172
7173 ocfs2_journal_dirty(handle, di_bh);
7174
7175out_commit:
7176 ocfs2_commit_trans(osb, handle);
7177
7178out:
7179 return ret;
7180}
7181
Mark Fashehccd979b2005-12-15 14:31:24 -08007182static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7183{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007184 /*
7185 * The caller is responsible for completing deallocation
7186 * before freeing the context.
7187 */
7188 if (tc->tc_dealloc.c_first_suballocator != NULL)
7189 mlog(ML_NOTICE,
7190 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007191
7192 if (tc->tc_last_eb_bh)
7193 brelse(tc->tc_last_eb_bh);
7194
7195 kfree(tc);
7196}