blob: 75e65df34b69a339ef129351c76e4104362160b4 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * alloc.c
5 *
6 * Extent allocs and frees
7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/fs.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/highmem.h>
Mark Fasheh60b11392007-02-16 11:46:50 -080030#include <linux/swap.h>
Jan Karaa90714c2008-10-09 19:38:40 +020031#include <linux/quotaops.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080032
33#define MLOG_MASK_PREFIX ML_DISK_ALLOC
34#include <cluster/masklog.h>
35
36#include "ocfs2.h"
37
38#include "alloc.h"
Mark Fasheh60b11392007-02-16 11:46:50 -080039#include "aops.h"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070040#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080041#include "dlmglue.h"
42#include "extent_map.h"
43#include "inode.h"
44#include "journal.h"
45#include "localalloc.h"
46#include "suballoc.h"
47#include "sysfile.h"
48#include "file.h"
49#include "super.h"
50#include "uptodate.h"
Joel Becker2a50a742008-12-09 14:24:33 -080051#include "xattr.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080052
53#include "buffer_head_io.h"
54
Tao Ma853a3a12009-08-18 11:22:18 +080055enum ocfs2_contig_type {
56 CONTIG_NONE = 0,
57 CONTIG_LEFT,
58 CONTIG_RIGHT,
59 CONTIG_LEFTRIGHT,
60};
Tao Mae7d4cb62008-08-18 17:38:44 +080061
Tao Ma853a3a12009-08-18 11:22:18 +080062static enum ocfs2_contig_type
63 ocfs2_extent_rec_contig(struct super_block *sb,
64 struct ocfs2_extent_rec *ext,
65 struct ocfs2_extent_rec *insert_rec);
Joel Becker1625f8a2008-08-21 17:11:10 -070066/*
67 * Operations for a specific extent tree type.
68 *
69 * To implement an on-disk btree (extent tree) type in ocfs2, add
70 * an ocfs2_extent_tree_operations structure and the matching
Joel Becker8d6220d2008-08-22 12:46:09 -070071 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
Joel Becker1625f8a2008-08-21 17:11:10 -070072 * for the allocation portion of the extent tree.
73 */
Tao Mae7d4cb62008-08-18 17:38:44 +080074struct ocfs2_extent_tree_operations {
Joel Becker1625f8a2008-08-21 17:11:10 -070075 /*
76 * last_eb_blk is the block number of the right most leaf extent
77 * block. Most on-disk structures containing an extent tree store
78 * this value for fast access. The ->eo_set_last_eb_blk() and
79 * ->eo_get_last_eb_blk() operations access this value. They are
80 * both required.
81 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070082 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
83 u64 blkno);
84 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -070085
86 /*
87 * The on-disk structure usually keeps track of how many total
88 * clusters are stored in this extent tree. This function updates
89 * that value. new_clusters is the delta, and must be
90 * added to the total. Required.
91 */
Joel Becker6136ca52009-02-12 19:32:43 -080092 void (*eo_update_clusters)(struct ocfs2_extent_tree *et,
Joel Becker35dc0aa2008-08-20 16:25:06 -070093 u32 new_clusters);
Joel Becker1625f8a2008-08-21 17:11:10 -070094
95 /*
Joel Becker92ba4702009-02-13 03:18:34 -080096 * If this extent tree is supported by an extent map, insert
97 * a record into the map.
98 */
99 void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et,
100 struct ocfs2_extent_rec *rec);
101
102 /*
Joel Becker4c911ee2009-02-13 02:50:12 -0800103 * If this extent tree is supported by an extent map, truncate the
104 * map to clusters,
105 */
106 void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et,
107 u32 clusters);
108
109 /*
Joel Becker1625f8a2008-08-21 17:11:10 -0700110 * If ->eo_insert_check() exists, it is called before rec is
111 * inserted into the extent tree. It is optional.
112 */
Joel Becker6136ca52009-02-12 19:32:43 -0800113 int (*eo_insert_check)(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700114 struct ocfs2_extent_rec *rec);
Joel Becker6136ca52009-02-12 19:32:43 -0800115 int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
Joel Becker0ce10102008-08-20 17:19:50 -0700116
Joel Becker1625f8a2008-08-21 17:11:10 -0700117 /*
118 * --------------------------------------------------------------
119 * The remaining are internal to ocfs2_extent_tree and don't have
120 * accessor functions
121 */
122
123 /*
124 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
125 * It is required.
126 */
Joel Becker0ce10102008-08-20 17:19:50 -0700127 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -0700128
129 /*
130 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
131 * it exists. If it does not, et->et_max_leaf_clusters is set
132 * to 0 (unlimited). Optional.
133 */
Joel Becker6136ca52009-02-12 19:32:43 -0800134 void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
Tao Ma853a3a12009-08-18 11:22:18 +0800135
136 /*
137 * ->eo_extent_contig test whether the 2 ocfs2_extent_rec
138 * are contiguous or not. Optional. Don't need to set it if use
139 * ocfs2_extent_rec as the tree leaf.
140 */
141 enum ocfs2_contig_type
142 (*eo_extent_contig)(struct ocfs2_extent_tree *et,
143 struct ocfs2_extent_rec *ext,
144 struct ocfs2_extent_rec *insert_rec);
Tao Mae7d4cb62008-08-18 17:38:44 +0800145};
146
Joel Beckerf99b9b72008-08-20 19:36:33 -0700147
148/*
149 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
150 * in the methods.
151 */
152static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
153static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
154 u64 blkno);
Joel Becker6136ca52009-02-12 19:32:43 -0800155static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700156 u32 clusters);
Joel Becker92ba4702009-02-13 03:18:34 -0800157static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
158 struct ocfs2_extent_rec *rec);
Joel Becker4c911ee2009-02-13 02:50:12 -0800159static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
160 u32 clusters);
Joel Becker6136ca52009-02-12 19:32:43 -0800161static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700162 struct ocfs2_extent_rec *rec);
Joel Becker6136ca52009-02-12 19:32:43 -0800163static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700164static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
165static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
166 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
167 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
168 .eo_update_clusters = ocfs2_dinode_update_clusters,
Joel Becker92ba4702009-02-13 03:18:34 -0800169 .eo_extent_map_insert = ocfs2_dinode_extent_map_insert,
Joel Becker4c911ee2009-02-13 02:50:12 -0800170 .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700171 .eo_insert_check = ocfs2_dinode_insert_check,
172 .eo_sanity_check = ocfs2_dinode_sanity_check,
173 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
Tao Mae7d4cb62008-08-18 17:38:44 +0800174};
175
176static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
177 u64 blkno)
178{
Joel Beckerea5efa12008-08-20 16:57:27 -0700179 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800180
Joel Beckerf99b9b72008-08-20 19:36:33 -0700181 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800182 di->i_last_eb_blk = cpu_to_le64(blkno);
183}
184
185static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
186{
Joel Beckerea5efa12008-08-20 16:57:27 -0700187 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800188
Joel Beckerf99b9b72008-08-20 19:36:33 -0700189 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800190 return le64_to_cpu(di->i_last_eb_blk);
191}
192
Joel Becker6136ca52009-02-12 19:32:43 -0800193static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +0800194 u32 clusters)
195{
Joel Becker6136ca52009-02-12 19:32:43 -0800196 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
Joel Beckerea5efa12008-08-20 16:57:27 -0700197 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800198
199 le32_add_cpu(&di->i_clusters, clusters);
Joel Becker6136ca52009-02-12 19:32:43 -0800200 spin_lock(&oi->ip_lock);
201 oi->ip_clusters = le32_to_cpu(di->i_clusters);
202 spin_unlock(&oi->ip_lock);
Tao Mae7d4cb62008-08-18 17:38:44 +0800203}
204
Joel Becker92ba4702009-02-13 03:18:34 -0800205static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
206 struct ocfs2_extent_rec *rec)
207{
208 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
209
210 ocfs2_extent_map_insert_rec(inode, rec);
211}
212
Joel Becker4c911ee2009-02-13 02:50:12 -0800213static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
214 u32 clusters)
215{
216 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
217
218 ocfs2_extent_map_trunc(inode, clusters);
219}
220
Joel Becker6136ca52009-02-12 19:32:43 -0800221static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700222 struct ocfs2_extent_rec *rec)
223{
Joel Becker6136ca52009-02-12 19:32:43 -0800224 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
225 struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
Joel Becker1e61ee72008-08-20 18:32:45 -0700226
Joel Becker6136ca52009-02-12 19:32:43 -0800227 BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
Joel Becker1e61ee72008-08-20 18:32:45 -0700228 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
Joel Becker6136ca52009-02-12 19:32:43 -0800229 (oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
Joel Becker1e61ee72008-08-20 18:32:45 -0700230 "Device %s, asking for sparse allocation: inode %llu, "
231 "cpos %u, clusters %u\n",
232 osb->dev_str,
Joel Becker6136ca52009-02-12 19:32:43 -0800233 (unsigned long long)oi->ip_blkno,
234 rec->e_cpos, oi->ip_clusters);
Joel Becker1e61ee72008-08-20 18:32:45 -0700235
236 return 0;
237}
238
Joel Becker6136ca52009-02-12 19:32:43 -0800239static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800240{
Joel Becker10995aa2008-11-13 14:49:12 -0800241 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800242
Joel Beckerf99b9b72008-08-20 19:36:33 -0700243 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Joel Becker10995aa2008-11-13 14:49:12 -0800244 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
Tao Mae7d4cb62008-08-18 17:38:44 +0800245
Joel Becker10995aa2008-11-13 14:49:12 -0800246 return 0;
Tao Mae7d4cb62008-08-18 17:38:44 +0800247}
248
Joel Beckerf99b9b72008-08-20 19:36:33 -0700249static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
250{
251 struct ocfs2_dinode *di = et->et_object;
252
253 et->et_root_el = &di->id2.i_list;
254}
255
Tao Mae7d4cb62008-08-18 17:38:44 +0800256
Joel Becker0ce10102008-08-20 17:19:50 -0700257static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
258{
Joel Becker2a50a742008-12-09 14:24:33 -0800259 struct ocfs2_xattr_value_buf *vb = et->et_object;
Joel Becker0ce10102008-08-20 17:19:50 -0700260
Joel Becker2a50a742008-12-09 14:24:33 -0800261 et->et_root_el = &vb->vb_xv->xr_list;
Joel Becker0ce10102008-08-20 17:19:50 -0700262}
263
Tao Maf56654c2008-08-18 17:38:48 +0800264static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
265 u64 blkno)
266{
Joel Becker2a50a742008-12-09 14:24:33 -0800267 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800268
Joel Becker2a50a742008-12-09 14:24:33 -0800269 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
Tao Maf56654c2008-08-18 17:38:48 +0800270}
271
272static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
273{
Joel Becker2a50a742008-12-09 14:24:33 -0800274 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800275
Joel Becker2a50a742008-12-09 14:24:33 -0800276 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
Tao Maf56654c2008-08-18 17:38:48 +0800277}
278
Joel Becker6136ca52009-02-12 19:32:43 -0800279static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
Tao Maf56654c2008-08-18 17:38:48 +0800280 u32 clusters)
281{
Joel Becker2a50a742008-12-09 14:24:33 -0800282 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800283
Joel Becker2a50a742008-12-09 14:24:33 -0800284 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800285}
286
Joel Becker1a09f552008-08-20 17:44:24 -0700287static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700288 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
289 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
290 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700291 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800292};
293
Joel Becker0ce10102008-08-20 17:19:50 -0700294static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
295{
296 struct ocfs2_xattr_block *xb = et->et_object;
297
298 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
299}
300
Joel Becker6136ca52009-02-12 19:32:43 -0800301static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
Joel Becker943cced2008-08-20 17:31:10 -0700302{
Joel Becker6136ca52009-02-12 19:32:43 -0800303 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Joel Becker943cced2008-08-20 17:31:10 -0700304 et->et_max_leaf_clusters =
Joel Becker6136ca52009-02-12 19:32:43 -0800305 ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
Joel Becker943cced2008-08-20 17:31:10 -0700306}
307
Tao Maba492612008-08-18 17:38:49 +0800308static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
309 u64 blkno)
310{
Joel Beckerea5efa12008-08-20 16:57:27 -0700311 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800312 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
313
314 xt->xt_last_eb_blk = cpu_to_le64(blkno);
315}
316
317static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
318{
Joel Beckerea5efa12008-08-20 16:57:27 -0700319 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800320 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
321
322 return le64_to_cpu(xt->xt_last_eb_blk);
323}
324
Joel Becker6136ca52009-02-12 19:32:43 -0800325static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
Tao Maba492612008-08-18 17:38:49 +0800326 u32 clusters)
327{
Joel Beckerea5efa12008-08-20 16:57:27 -0700328 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800329
330 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
331}
332
Tao Maba492612008-08-18 17:38:49 +0800333static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700334 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
335 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
336 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700337 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
Joel Becker943cced2008-08-20 17:31:10 -0700338 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
Tao Maba492612008-08-18 17:38:49 +0800339};
340
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800341static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
342 u64 blkno)
343{
344 struct ocfs2_dx_root_block *dx_root = et->et_object;
345
346 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
347}
348
349static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
350{
351 struct ocfs2_dx_root_block *dx_root = et->et_object;
352
353 return le64_to_cpu(dx_root->dr_last_eb_blk);
354}
355
Joel Becker6136ca52009-02-12 19:32:43 -0800356static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800357 u32 clusters)
358{
359 struct ocfs2_dx_root_block *dx_root = et->et_object;
360
361 le32_add_cpu(&dx_root->dr_clusters, clusters);
362}
363
Joel Becker6136ca52009-02-12 19:32:43 -0800364static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800365{
366 struct ocfs2_dx_root_block *dx_root = et->et_object;
367
368 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
369
370 return 0;
371}
372
373static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
374{
375 struct ocfs2_dx_root_block *dx_root = et->et_object;
376
377 et->et_root_el = &dx_root->dr_list;
378}
379
380static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
381 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
382 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
383 .eo_update_clusters = ocfs2_dx_root_update_clusters,
384 .eo_sanity_check = ocfs2_dx_root_sanity_check,
385 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
386};
387
Joel Becker8d6220d2008-08-22 12:46:09 -0700388static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800389 struct ocfs2_caching_info *ci,
Joel Becker8d6220d2008-08-22 12:46:09 -0700390 struct buffer_head *bh,
Joel Becker13723d02008-10-17 19:25:01 -0700391 ocfs2_journal_access_func access,
Joel Becker8d6220d2008-08-22 12:46:09 -0700392 void *obj,
393 struct ocfs2_extent_tree_operations *ops)
Tao Mae7d4cb62008-08-18 17:38:44 +0800394{
Joel Becker1a09f552008-08-20 17:44:24 -0700395 et->et_ops = ops;
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700396 et->et_root_bh = bh;
Joel Becker5e404e92009-02-13 03:54:22 -0800397 et->et_ci = ci;
Joel Becker13723d02008-10-17 19:25:01 -0700398 et->et_root_journal_access = access;
Joel Beckerea5efa12008-08-20 16:57:27 -0700399 if (!obj)
400 obj = (void *)bh->b_data;
401 et->et_object = obj;
Tao Mae7d4cb62008-08-18 17:38:44 +0800402
Joel Becker0ce10102008-08-20 17:19:50 -0700403 et->et_ops->eo_fill_root_el(et);
Joel Becker943cced2008-08-20 17:31:10 -0700404 if (!et->et_ops->eo_fill_max_leaf_clusters)
405 et->et_max_leaf_clusters = 0;
406 else
Joel Becker6136ca52009-02-12 19:32:43 -0800407 et->et_ops->eo_fill_max_leaf_clusters(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800408}
409
Joel Becker8d6220d2008-08-22 12:46:09 -0700410void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800411 struct ocfs2_caching_info *ci,
Joel Becker8d6220d2008-08-22 12:46:09 -0700412 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700413{
Joel Becker5e404e92009-02-13 03:54:22 -0800414 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di,
Joel Becker13723d02008-10-17 19:25:01 -0700415 NULL, &ocfs2_dinode_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700416}
417
Joel Becker8d6220d2008-08-22 12:46:09 -0700418void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800419 struct ocfs2_caching_info *ci,
Joel Becker8d6220d2008-08-22 12:46:09 -0700420 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700421{
Joel Becker5e404e92009-02-13 03:54:22 -0800422 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb,
Joel Becker13723d02008-10-17 19:25:01 -0700423 NULL, &ocfs2_xattr_tree_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700424}
425
Joel Becker8d6220d2008-08-22 12:46:09 -0700426void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800427 struct ocfs2_caching_info *ci,
Joel Becker2a50a742008-12-09 14:24:33 -0800428 struct ocfs2_xattr_value_buf *vb)
Tao Mae7d4cb62008-08-18 17:38:44 +0800429{
Joel Becker5e404e92009-02-13 03:54:22 -0800430 __ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb,
Joel Becker8d6220d2008-08-22 12:46:09 -0700431 &ocfs2_xattr_value_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800432}
433
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800434void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800435 struct ocfs2_caching_info *ci,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800436 struct buffer_head *bh)
437{
Joel Becker5e404e92009-02-13 03:54:22 -0800438 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800439 NULL, &ocfs2_dx_root_et_ops);
440}
441
Joel Becker35dc0aa2008-08-20 16:25:06 -0700442static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
443 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800444{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700445 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800446}
447
Joel Becker35dc0aa2008-08-20 16:25:06 -0700448static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800449{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700450 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800451}
452
Joel Becker6136ca52009-02-12 19:32:43 -0800453static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
Joel Becker35dc0aa2008-08-20 16:25:06 -0700454 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800455{
Joel Becker6136ca52009-02-12 19:32:43 -0800456 et->et_ops->eo_update_clusters(et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700457}
458
Joel Becker92ba4702009-02-13 03:18:34 -0800459static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et,
460 struct ocfs2_extent_rec *rec)
461{
462 if (et->et_ops->eo_extent_map_insert)
463 et->et_ops->eo_extent_map_insert(et, rec);
464}
465
Joel Becker4c911ee2009-02-13 02:50:12 -0800466static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et,
467 u32 clusters)
468{
469 if (et->et_ops->eo_extent_map_truncate)
470 et->et_ops->eo_extent_map_truncate(et, clusters);
471}
472
Joel Becker13723d02008-10-17 19:25:01 -0700473static inline int ocfs2_et_root_journal_access(handle_t *handle,
Joel Becker13723d02008-10-17 19:25:01 -0700474 struct ocfs2_extent_tree *et,
475 int type)
476{
Joel Beckerd9a0a1f2009-02-12 17:32:34 -0800477 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700478 type);
479}
480
Tao Ma853a3a12009-08-18 11:22:18 +0800481static inline enum ocfs2_contig_type
482 ocfs2_et_extent_contig(struct ocfs2_extent_tree *et,
483 struct ocfs2_extent_rec *rec,
484 struct ocfs2_extent_rec *insert_rec)
485{
486 if (et->et_ops->eo_extent_contig)
487 return et->et_ops->eo_extent_contig(et, rec, insert_rec);
488
489 return ocfs2_extent_rec_contig(
490 ocfs2_metadata_cache_get_super(et->et_ci),
491 rec, insert_rec);
492}
493
Joel Becker6136ca52009-02-12 19:32:43 -0800494static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700495 struct ocfs2_extent_rec *rec)
496{
497 int ret = 0;
498
499 if (et->et_ops->eo_insert_check)
Joel Becker6136ca52009-02-12 19:32:43 -0800500 ret = et->et_ops->eo_insert_check(et, rec);
Joel Becker1e61ee72008-08-20 18:32:45 -0700501 return ret;
502}
503
Joel Becker6136ca52009-02-12 19:32:43 -0800504static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
Joel Becker35dc0aa2008-08-20 16:25:06 -0700505{
Joel Becker1e61ee72008-08-20 18:32:45 -0700506 int ret = 0;
507
508 if (et->et_ops->eo_sanity_check)
Joel Becker6136ca52009-02-12 19:32:43 -0800509 ret = et->et_ops->eo_sanity_check(et);
Joel Becker1e61ee72008-08-20 18:32:45 -0700510 return ret;
Tao Mae7d4cb62008-08-18 17:38:44 +0800511}
512
Mark Fashehccd979b2005-12-15 14:31:24 -0800513static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
Mark Fasheh59a5e412007-06-22 15:52:36 -0700514static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
515 struct ocfs2_extent_block *eb);
Mark Fashehccd979b2005-12-15 14:31:24 -0800516
Mark Fashehdcd05382007-01-16 11:32:23 -0800517/*
518 * Structures which describe a path through a btree, and functions to
519 * manipulate them.
520 *
521 * The idea here is to be as generic as possible with the tree
522 * manipulation code.
523 */
524struct ocfs2_path_item {
525 struct buffer_head *bh;
526 struct ocfs2_extent_list *el;
527};
528
529#define OCFS2_MAX_PATH_DEPTH 5
530
531struct ocfs2_path {
Joel Becker13723d02008-10-17 19:25:01 -0700532 int p_tree_depth;
533 ocfs2_journal_access_func p_root_access;
534 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
Mark Fashehdcd05382007-01-16 11:32:23 -0800535};
536
537#define path_root_bh(_path) ((_path)->p_node[0].bh)
538#define path_root_el(_path) ((_path)->p_node[0].el)
Joel Becker13723d02008-10-17 19:25:01 -0700539#define path_root_access(_path)((_path)->p_root_access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800540#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
541#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
542#define path_num_items(_path) ((_path)->p_tree_depth + 1)
543
Joel Beckerfacdb772009-02-12 18:08:48 -0800544static int ocfs2_find_path(struct ocfs2_caching_info *ci,
545 struct ocfs2_path *path, u32 cpos);
Joel Beckerd401dc12009-02-13 02:24:10 -0800546static void ocfs2_adjust_rightmost_records(handle_t *handle,
547 struct ocfs2_extent_tree *et,
Tao Ma6b791bc2009-06-12 14:18:36 +0800548 struct ocfs2_path *path,
549 struct ocfs2_extent_rec *insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -0800550/*
551 * Reset the actual path elements so that we can re-use the structure
552 * to build another path. Generally, this involves freeing the buffer
553 * heads.
554 */
555static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
556{
557 int i, start = 0, depth = 0;
558 struct ocfs2_path_item *node;
559
560 if (keep_root)
561 start = 1;
562
563 for(i = start; i < path_num_items(path); i++) {
564 node = &path->p_node[i];
565
566 brelse(node->bh);
567 node->bh = NULL;
568 node->el = NULL;
569 }
570
571 /*
572 * Tree depth may change during truncate, or insert. If we're
573 * keeping the root extent list, then make sure that our path
574 * structure reflects the proper depth.
575 */
576 if (keep_root)
577 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
Joel Becker13723d02008-10-17 19:25:01 -0700578 else
579 path_root_access(path) = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -0800580
581 path->p_tree_depth = depth;
582}
583
584static void ocfs2_free_path(struct ocfs2_path *path)
585{
586 if (path) {
587 ocfs2_reinit_path(path, 0);
588 kfree(path);
589 }
590}
591
592/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700593 * All the elements of src into dest. After this call, src could be freed
594 * without affecting dest.
595 *
596 * Both paths should have the same root. Any non-root elements of dest
597 * will be freed.
598 */
599static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
600{
601 int i;
602
603 BUG_ON(path_root_bh(dest) != path_root_bh(src));
604 BUG_ON(path_root_el(dest) != path_root_el(src));
Joel Becker13723d02008-10-17 19:25:01 -0700605 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fasheh328d5752007-06-18 10:48:04 -0700606
607 ocfs2_reinit_path(dest, 1);
608
609 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
610 dest->p_node[i].bh = src->p_node[i].bh;
611 dest->p_node[i].el = src->p_node[i].el;
612
613 if (dest->p_node[i].bh)
614 get_bh(dest->p_node[i].bh);
615 }
616}
617
618/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800619 * Make the *dest path the same as src and re-initialize src path to
620 * have a root only.
621 */
622static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
623{
624 int i;
625
626 BUG_ON(path_root_bh(dest) != path_root_bh(src));
Joel Becker13723d02008-10-17 19:25:01 -0700627 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fashehdcd05382007-01-16 11:32:23 -0800628
629 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
630 brelse(dest->p_node[i].bh);
631
632 dest->p_node[i].bh = src->p_node[i].bh;
633 dest->p_node[i].el = src->p_node[i].el;
634
635 src->p_node[i].bh = NULL;
636 src->p_node[i].el = NULL;
637 }
638}
639
640/*
641 * Insert an extent block at given index.
642 *
643 * This will not take an additional reference on eb_bh.
644 */
645static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
646 struct buffer_head *eb_bh)
647{
648 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
649
650 /*
651 * Right now, no root bh is an extent block, so this helps
652 * catch code errors with dinode trees. The assertion can be
653 * safely removed if we ever need to insert extent block
654 * structures at the root.
655 */
656 BUG_ON(index == 0);
657
658 path->p_node[index].bh = eb_bh;
659 path->p_node[index].el = &eb->h_list;
660}
661
662static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700663 struct ocfs2_extent_list *root_el,
664 ocfs2_journal_access_func access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800665{
666 struct ocfs2_path *path;
667
668 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
669
670 path = kzalloc(sizeof(*path), GFP_NOFS);
671 if (path) {
672 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
673 get_bh(root_bh);
674 path_root_bh(path) = root_bh;
675 path_root_el(path) = root_el;
Joel Becker13723d02008-10-17 19:25:01 -0700676 path_root_access(path) = access;
Mark Fashehdcd05382007-01-16 11:32:23 -0800677 }
678
679 return path;
680}
681
Joel Beckerffdd7a52008-10-17 22:32:01 -0700682static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
683{
Joel Becker13723d02008-10-17 19:25:01 -0700684 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
685 path_root_access(path));
Joel Beckerffdd7a52008-10-17 22:32:01 -0700686}
687
688static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
689{
Joel Becker13723d02008-10-17 19:25:01 -0700690 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
691 et->et_root_journal_access);
692}
693
694/*
695 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
696 * otherwise it's the root_access function.
697 *
698 * I don't like the way this function's name looks next to
699 * ocfs2_journal_access_path(), but I don't have a better one.
700 */
701static int ocfs2_path_bh_journal_access(handle_t *handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800702 struct ocfs2_caching_info *ci,
Joel Becker13723d02008-10-17 19:25:01 -0700703 struct ocfs2_path *path,
704 int idx)
705{
706 ocfs2_journal_access_func access = path_root_access(path);
707
708 if (!access)
709 access = ocfs2_journal_access;
710
711 if (idx)
712 access = ocfs2_journal_access_eb;
713
Joel Becker0cf2f762009-02-12 16:41:25 -0800714 return access(handle, ci, path->p_node[idx].bh,
Joel Becker13723d02008-10-17 19:25:01 -0700715 OCFS2_JOURNAL_ACCESS_WRITE);
Joel Beckerffdd7a52008-10-17 22:32:01 -0700716}
717
Mark Fashehdcd05382007-01-16 11:32:23 -0800718/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800719 * Convenience function to journal all components in a path.
720 */
Joel Becker0cf2f762009-02-12 16:41:25 -0800721static int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
722 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -0800723 struct ocfs2_path *path)
724{
725 int i, ret = 0;
726
727 if (!path)
728 goto out;
729
730 for(i = 0; i < path_num_items(path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800731 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -0800732 if (ret < 0) {
733 mlog_errno(ret);
734 goto out;
735 }
736 }
737
738out:
739 return ret;
740}
741
Mark Fasheh328d5752007-06-18 10:48:04 -0700742/*
743 * Return the index of the extent record which contains cluster #v_cluster.
744 * -1 is returned if it was not found.
745 *
746 * Should work fine on interior and exterior nodes.
747 */
748int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
749{
750 int ret = -1;
751 int i;
752 struct ocfs2_extent_rec *rec;
753 u32 rec_end, rec_start, clusters;
754
755 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
756 rec = &el->l_recs[i];
757
758 rec_start = le32_to_cpu(rec->e_cpos);
759 clusters = ocfs2_rec_clusters(el, rec);
760
761 rec_end = rec_start + clusters;
762
763 if (v_cluster >= rec_start && v_cluster < rec_end) {
764 ret = i;
765 break;
766 }
767 }
768
769 return ret;
770}
771
Mark Fashehe48edee2007-03-07 16:46:57 -0800772/*
773 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
Tao Ma853a3a12009-08-18 11:22:18 +0800774 * ocfs2_extent_rec_contig only work properly against leaf nodes!
Mark Fashehe48edee2007-03-07 16:46:57 -0800775 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800776static int ocfs2_block_extent_contig(struct super_block *sb,
777 struct ocfs2_extent_rec *ext,
778 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800779{
Mark Fashehe48edee2007-03-07 16:46:57 -0800780 u64 blk_end = le64_to_cpu(ext->e_blkno);
781
782 blk_end += ocfs2_clusters_to_blocks(sb,
783 le16_to_cpu(ext->e_leaf_clusters));
784
785 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800786}
787
Mark Fashehdcd05382007-01-16 11:32:23 -0800788static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
789 struct ocfs2_extent_rec *right)
790{
Mark Fashehe48edee2007-03-07 16:46:57 -0800791 u32 left_range;
792
793 left_range = le32_to_cpu(left->e_cpos) +
794 le16_to_cpu(left->e_leaf_clusters);
795
796 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800797}
798
799static enum ocfs2_contig_type
Tao Ma853a3a12009-08-18 11:22:18 +0800800 ocfs2_extent_rec_contig(struct super_block *sb,
801 struct ocfs2_extent_rec *ext,
802 struct ocfs2_extent_rec *insert_rec)
Mark Fashehdcd05382007-01-16 11:32:23 -0800803{
804 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
805
Mark Fasheh328d5752007-06-18 10:48:04 -0700806 /*
807 * Refuse to coalesce extent records with different flag
808 * fields - we don't want to mix unwritten extents with user
809 * data.
810 */
811 if (ext->e_flags != insert_rec->e_flags)
812 return CONTIG_NONE;
813
Mark Fashehdcd05382007-01-16 11:32:23 -0800814 if (ocfs2_extents_adjacent(ext, insert_rec) &&
Joel Beckerb4a17652009-02-13 03:07:09 -0800815 ocfs2_block_extent_contig(sb, ext, blkno))
Mark Fashehdcd05382007-01-16 11:32:23 -0800816 return CONTIG_RIGHT;
817
818 blkno = le64_to_cpu(ext->e_blkno);
819 if (ocfs2_extents_adjacent(insert_rec, ext) &&
Joel Beckerb4a17652009-02-13 03:07:09 -0800820 ocfs2_block_extent_contig(sb, insert_rec, blkno))
Mark Fashehdcd05382007-01-16 11:32:23 -0800821 return CONTIG_LEFT;
822
823 return CONTIG_NONE;
824}
825
826/*
827 * NOTE: We can have pretty much any combination of contiguousness and
828 * appending.
829 *
830 * The usefulness of APPEND_TAIL is more in that it lets us know that
831 * we'll have to update the path to that leaf.
832 */
833enum ocfs2_append_type {
834 APPEND_NONE = 0,
835 APPEND_TAIL,
836};
837
Mark Fasheh328d5752007-06-18 10:48:04 -0700838enum ocfs2_split_type {
839 SPLIT_NONE = 0,
840 SPLIT_LEFT,
841 SPLIT_RIGHT,
842};
843
Mark Fashehdcd05382007-01-16 11:32:23 -0800844struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700845 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800846 enum ocfs2_append_type ins_appending;
847 enum ocfs2_contig_type ins_contig;
848 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800849 int ins_tree_depth;
850};
851
Mark Fasheh328d5752007-06-18 10:48:04 -0700852struct ocfs2_merge_ctxt {
853 enum ocfs2_contig_type c_contig_type;
854 int c_has_empty_extent;
855 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700856};
857
Joel Becker5e965812008-11-13 14:49:16 -0800858static int ocfs2_validate_extent_block(struct super_block *sb,
859 struct buffer_head *bh)
860{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700861 int rc;
Joel Becker5e965812008-11-13 14:49:16 -0800862 struct ocfs2_extent_block *eb =
863 (struct ocfs2_extent_block *)bh->b_data;
864
Joel Becker970e4932008-11-13 14:49:19 -0800865 mlog(0, "Validating extent block %llu\n",
866 (unsigned long long)bh->b_blocknr);
867
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700868 BUG_ON(!buffer_uptodate(bh));
869
870 /*
871 * If the ecc fails, we return the error but otherwise
872 * leave the filesystem running. We know any error is
873 * local to this block.
874 */
875 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
Joel Becker13723d02008-10-17 19:25:01 -0700876 if (rc) {
877 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
878 (unsigned long long)bh->b_blocknr);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700879 return rc;
Joel Becker13723d02008-10-17 19:25:01 -0700880 }
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700881
882 /*
883 * Errors after here are fatal.
884 */
885
Joel Becker5e965812008-11-13 14:49:16 -0800886 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
887 ocfs2_error(sb,
888 "Extent block #%llu has bad signature %.*s",
889 (unsigned long long)bh->b_blocknr, 7,
890 eb->h_signature);
891 return -EINVAL;
892 }
893
894 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
895 ocfs2_error(sb,
896 "Extent block #%llu has an invalid h_blkno "
897 "of %llu",
898 (unsigned long long)bh->b_blocknr,
899 (unsigned long long)le64_to_cpu(eb->h_blkno));
900 return -EINVAL;
901 }
902
903 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
904 ocfs2_error(sb,
905 "Extent block #%llu has an invalid "
906 "h_fs_generation of #%u",
907 (unsigned long long)bh->b_blocknr,
908 le32_to_cpu(eb->h_fs_generation));
909 return -EINVAL;
910 }
911
912 return 0;
913}
914
Joel Becker3d03a302009-02-12 17:49:26 -0800915int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
Joel Becker5e965812008-11-13 14:49:16 -0800916 struct buffer_head **bh)
917{
918 int rc;
919 struct buffer_head *tmp = *bh;
920
Joel Becker3d03a302009-02-12 17:49:26 -0800921 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800922 ocfs2_validate_extent_block);
Joel Becker5e965812008-11-13 14:49:16 -0800923
924 /* If ocfs2_read_block() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -0800925 if (!rc && !*bh)
Joel Becker5e965812008-11-13 14:49:16 -0800926 *bh = tmp;
927
Joel Becker5e965812008-11-13 14:49:16 -0800928 return rc;
929}
930
931
Mark Fashehccd979b2005-12-15 14:31:24 -0800932/*
933 * How many free extents have we got before we need more meta data?
934 */
935int ocfs2_num_free_extents(struct ocfs2_super *osb,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700936 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -0800937{
938 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800939 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800940 struct ocfs2_extent_block *eb;
941 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800942 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800943
944 mlog_entry_void();
945
Joel Beckerf99b9b72008-08-20 19:36:33 -0700946 el = et->et_root_el;
947 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800948
Tao Mae7d4cb62008-08-18 17:38:44 +0800949 if (last_eb_blk) {
Joel Becker3d03a302009-02-12 17:49:26 -0800950 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
951 &eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800952 if (retval < 0) {
953 mlog_errno(retval);
954 goto bail;
955 }
956 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
957 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800958 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800959
960 BUG_ON(el->l_tree_depth != 0);
961
962 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
963bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700964 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800965
966 mlog_exit(retval);
967 return retval;
968}
969
970/* expects array to already be allocated
971 *
972 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
973 * l_count for you
974 */
Joel Becker42a5a7a2009-02-12 18:49:19 -0800975static int ocfs2_create_new_meta_bhs(handle_t *handle,
976 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -0800977 int wanted,
978 struct ocfs2_alloc_context *meta_ac,
979 struct buffer_head *bhs[])
980{
981 int count, status, i;
982 u16 suballoc_bit_start;
983 u32 num_got;
984 u64 first_blkno;
Joel Becker42a5a7a2009-02-12 18:49:19 -0800985 struct ocfs2_super *osb =
986 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -0800987 struct ocfs2_extent_block *eb;
988
989 mlog_entry_void();
990
991 count = 0;
992 while (count < wanted) {
993 status = ocfs2_claim_metadata(osb,
994 handle,
995 meta_ac,
996 wanted - count,
997 &suballoc_bit_start,
998 &num_got,
999 &first_blkno);
1000 if (status < 0) {
1001 mlog_errno(status);
1002 goto bail;
1003 }
1004
1005 for(i = count; i < (num_got + count); i++) {
1006 bhs[i] = sb_getblk(osb->sb, first_blkno);
1007 if (bhs[i] == NULL) {
1008 status = -EIO;
1009 mlog_errno(status);
1010 goto bail;
1011 }
Joel Becker42a5a7a2009-02-12 18:49:19 -08001012 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001013
Joel Becker42a5a7a2009-02-12 18:49:19 -08001014 status = ocfs2_journal_access_eb(handle, et->et_ci,
1015 bhs[i],
Joel Becker13723d02008-10-17 19:25:01 -07001016 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001017 if (status < 0) {
1018 mlog_errno(status);
1019 goto bail;
1020 }
1021
1022 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
1023 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
1024 /* Ok, setup the minimal stuff here. */
1025 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
1026 eb->h_blkno = cpu_to_le64(first_blkno);
1027 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08001028 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001029 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1030 eb->h_list.l_count =
1031 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1032
1033 suballoc_bit_start++;
1034 first_blkno++;
1035
1036 /* We'll also be dirtied by the caller, so
1037 * this isn't absolutely necessary. */
1038 status = ocfs2_journal_dirty(handle, bhs[i]);
1039 if (status < 0) {
1040 mlog_errno(status);
1041 goto bail;
1042 }
1043 }
1044
1045 count += num_got;
1046 }
1047
1048 status = 0;
1049bail:
1050 if (status < 0) {
1051 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001052 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001053 bhs[i] = NULL;
1054 }
1055 }
1056 mlog_exit(status);
1057 return status;
1058}
1059
1060/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001061 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1062 *
1063 * Returns the sum of the rightmost extent rec logical offset and
1064 * cluster count.
1065 *
1066 * ocfs2_add_branch() uses this to determine what logical cluster
1067 * value should be populated into the leftmost new branch records.
1068 *
1069 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1070 * value for the new topmost tree record.
1071 */
1072static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1073{
1074 int i;
1075
1076 i = le16_to_cpu(el->l_next_free_rec) - 1;
1077
1078 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001079 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08001080}
1081
1082/*
Tao Ma6b791bc2009-06-12 14:18:36 +08001083 * Change range of the branches in the right most path according to the leaf
1084 * extent block's rightmost record.
1085 */
1086static int ocfs2_adjust_rightmost_branch(handle_t *handle,
Tao Ma6b791bc2009-06-12 14:18:36 +08001087 struct ocfs2_extent_tree *et)
1088{
1089 int status;
1090 struct ocfs2_path *path = NULL;
1091 struct ocfs2_extent_list *el;
1092 struct ocfs2_extent_rec *rec;
1093
1094 path = ocfs2_new_path_from_et(et);
1095 if (!path) {
1096 status = -ENOMEM;
1097 return status;
1098 }
1099
Joel Beckerfacdb772009-02-12 18:08:48 -08001100 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
Tao Ma6b791bc2009-06-12 14:18:36 +08001101 if (status < 0) {
1102 mlog_errno(status);
1103 goto out;
1104 }
1105
1106 status = ocfs2_extend_trans(handle, path_num_items(path) +
1107 handle->h_buffer_credits);
1108 if (status < 0) {
1109 mlog_errno(status);
1110 goto out;
1111 }
1112
Joel Beckerd401dc12009-02-13 02:24:10 -08001113 status = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma6b791bc2009-06-12 14:18:36 +08001114 if (status < 0) {
1115 mlog_errno(status);
1116 goto out;
1117 }
1118
1119 el = path_leaf_el(path);
1120 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1121
Joel Beckerd401dc12009-02-13 02:24:10 -08001122 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Tao Ma6b791bc2009-06-12 14:18:36 +08001123
1124out:
1125 ocfs2_free_path(path);
1126 return status;
1127}
1128
1129/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001130 * Add an entire tree branch to our inode. eb_bh is the extent block
Joel Beckerd401dc12009-02-13 02:24:10 -08001131 * to start at, if we don't want to start the branch at the root
Mark Fashehccd979b2005-12-15 14:31:24 -08001132 * structure.
1133 *
1134 * last_eb_bh is required as we have to update it's next_leaf pointer
1135 * for the new last extent block.
1136 *
1137 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -08001138 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -08001139 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001140static int ocfs2_add_branch(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001141 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001142 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07001143 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001144 struct ocfs2_alloc_context *meta_ac)
1145{
1146 int status, new_blocks, i;
1147 u64 next_blkno, new_last_eb_blk;
1148 struct buffer_head *bh;
1149 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001150 struct ocfs2_extent_block *eb;
1151 struct ocfs2_extent_list *eb_el;
1152 struct ocfs2_extent_list *el;
Tao Ma6b791bc2009-06-12 14:18:36 +08001153 u32 new_cpos, root_end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001154
1155 mlog_entry_void();
1156
Mark Fasheh328d5752007-06-18 10:48:04 -07001157 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001158
Mark Fashehccd979b2005-12-15 14:31:24 -08001159 if (eb_bh) {
1160 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1161 el = &eb->h_list;
1162 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001163 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001164
1165 /* we never add a branch to a leaf. */
1166 BUG_ON(!el->l_tree_depth);
1167
1168 new_blocks = le16_to_cpu(el->l_tree_depth);
1169
Tao Ma6b791bc2009-06-12 14:18:36 +08001170 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1171 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1172 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1173
1174 /*
1175 * If there is a gap before the root end and the real end
1176 * of the righmost leaf block, we need to remove the gap
1177 * between new_cpos and root_end first so that the tree
1178 * is consistent after we add a new branch(it will start
1179 * from new_cpos).
1180 */
1181 if (root_end > new_cpos) {
1182 mlog(0, "adjust the cluster end from %u to %u\n",
1183 root_end, new_cpos);
Joel Beckerd401dc12009-02-13 02:24:10 -08001184 status = ocfs2_adjust_rightmost_branch(handle, et);
Tao Ma6b791bc2009-06-12 14:18:36 +08001185 if (status) {
1186 mlog_errno(status);
1187 goto bail;
1188 }
1189 }
1190
Mark Fashehccd979b2005-12-15 14:31:24 -08001191 /* allocate the number of new eb blocks we need */
1192 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1193 GFP_KERNEL);
1194 if (!new_eb_bhs) {
1195 status = -ENOMEM;
1196 mlog_errno(status);
1197 goto bail;
1198 }
1199
Joel Becker42a5a7a2009-02-12 18:49:19 -08001200 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
Mark Fashehccd979b2005-12-15 14:31:24 -08001201 meta_ac, new_eb_bhs);
1202 if (status < 0) {
1203 mlog_errno(status);
1204 goto bail;
1205 }
1206
1207 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1208 * linked with the rest of the tree.
1209 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1210 *
1211 * when we leave the loop, new_last_eb_blk will point to the
1212 * newest leaf, and next_blkno will point to the topmost extent
1213 * block. */
1214 next_blkno = new_last_eb_blk = 0;
1215 for(i = 0; i < new_blocks; i++) {
1216 bh = new_eb_bhs[i];
1217 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001218 /* ocfs2_create_new_meta_bhs() should create it right! */
1219 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001220 eb_el = &eb->h_list;
1221
Joel Beckerd401dc12009-02-13 02:24:10 -08001222 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
Joel Becker13723d02008-10-17 19:25:01 -07001223 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001224 if (status < 0) {
1225 mlog_errno(status);
1226 goto bail;
1227 }
1228
1229 eb->h_next_leaf_blk = 0;
1230 eb_el->l_tree_depth = cpu_to_le16(i);
1231 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001232 /*
1233 * This actually counts as an empty extent as
1234 * c_clusters == 0
1235 */
1236 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001237 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001238 /*
1239 * eb_el isn't always an interior node, but even leaf
1240 * nodes want a zero'd flags and reserved field so
1241 * this gets the whole 32 bits regardless of use.
1242 */
1243 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001244 if (!eb_el->l_tree_depth)
1245 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1246
1247 status = ocfs2_journal_dirty(handle, bh);
1248 if (status < 0) {
1249 mlog_errno(status);
1250 goto bail;
1251 }
1252
1253 next_blkno = le64_to_cpu(eb->h_blkno);
1254 }
1255
1256 /* This is a bit hairy. We want to update up to three blocks
1257 * here without leaving any of them in an inconsistent state
1258 * in case of error. We don't have to worry about
1259 * journal_dirty erroring as it won't unless we've aborted the
1260 * handle (in which case we would never be here) so reserving
1261 * the write with journal_access is all we need to do. */
Joel Beckerd401dc12009-02-13 02:24:10 -08001262 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001263 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001264 if (status < 0) {
1265 mlog_errno(status);
1266 goto bail;
1267 }
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001268 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001269 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001270 if (status < 0) {
1271 mlog_errno(status);
1272 goto bail;
1273 }
1274 if (eb_bh) {
Joel Beckerd401dc12009-02-13 02:24:10 -08001275 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001276 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001277 if (status < 0) {
1278 mlog_errno(status);
1279 goto bail;
1280 }
1281 }
1282
1283 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001284 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001285 i = le16_to_cpu(el->l_next_free_rec);
1286 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001287 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001288 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001289 le16_add_cpu(&el->l_next_free_rec, 1);
1290
1291 /* fe needs a new last extent block pointer, as does the
1292 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001293 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001294
Mark Fasheh328d5752007-06-18 10:48:04 -07001295 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001296 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1297
Mark Fasheh328d5752007-06-18 10:48:04 -07001298 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001299 if (status < 0)
1300 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001301 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001302 if (status < 0)
1303 mlog_errno(status);
1304 if (eb_bh) {
1305 status = ocfs2_journal_dirty(handle, eb_bh);
1306 if (status < 0)
1307 mlog_errno(status);
1308 }
1309
Mark Fasheh328d5752007-06-18 10:48:04 -07001310 /*
1311 * Some callers want to track the rightmost leaf so pass it
1312 * back here.
1313 */
1314 brelse(*last_eb_bh);
1315 get_bh(new_eb_bhs[0]);
1316 *last_eb_bh = new_eb_bhs[0];
1317
Mark Fashehccd979b2005-12-15 14:31:24 -08001318 status = 0;
1319bail:
1320 if (new_eb_bhs) {
1321 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001322 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001323 kfree(new_eb_bhs);
1324 }
1325
1326 mlog_exit(status);
1327 return status;
1328}
1329
1330/*
1331 * adds another level to the allocation tree.
1332 * returns back the new extent block so you can add a branch to it
1333 * after this call.
1334 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001335static int ocfs2_shift_tree_depth(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001336 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001337 struct ocfs2_alloc_context *meta_ac,
1338 struct buffer_head **ret_new_eb_bh)
1339{
1340 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001341 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001342 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001343 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001344 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001345 struct ocfs2_extent_list *eb_el;
1346
1347 mlog_entry_void();
1348
Joel Becker42a5a7a2009-02-12 18:49:19 -08001349 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
Mark Fashehccd979b2005-12-15 14:31:24 -08001350 &new_eb_bh);
1351 if (status < 0) {
1352 mlog_errno(status);
1353 goto bail;
1354 }
1355
1356 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001357 /* ocfs2_create_new_meta_bhs() should create it right! */
1358 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001359
1360 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001361 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001362
Joel Beckerd401dc12009-02-13 02:24:10 -08001363 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001364 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001365 if (status < 0) {
1366 mlog_errno(status);
1367 goto bail;
1368 }
1369
Tao Mae7d4cb62008-08-18 17:38:44 +08001370 /* copy the root extent list data into the new extent block */
1371 eb_el->l_tree_depth = root_el->l_tree_depth;
1372 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1373 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1374 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001375
1376 status = ocfs2_journal_dirty(handle, new_eb_bh);
1377 if (status < 0) {
1378 mlog_errno(status);
1379 goto bail;
1380 }
1381
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001382 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001383 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001384 if (status < 0) {
1385 mlog_errno(status);
1386 goto bail;
1387 }
1388
Mark Fashehdcd05382007-01-16 11:32:23 -08001389 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1390
Tao Mae7d4cb62008-08-18 17:38:44 +08001391 /* update root_bh now */
1392 le16_add_cpu(&root_el->l_tree_depth, 1);
1393 root_el->l_recs[0].e_cpos = 0;
1394 root_el->l_recs[0].e_blkno = eb->h_blkno;
1395 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1396 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1397 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1398 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001399
1400 /* If this is our 1st tree depth shift, then last_eb_blk
1401 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001402 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001403 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001404
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001405 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001406 if (status < 0) {
1407 mlog_errno(status);
1408 goto bail;
1409 }
1410
1411 *ret_new_eb_bh = new_eb_bh;
1412 new_eb_bh = NULL;
1413 status = 0;
1414bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001415 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001416
1417 mlog_exit(status);
1418 return status;
1419}
1420
1421/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001422 * Should only be called when there is no space left in any of the
1423 * leaf nodes. What we want to do is find the lowest tree depth
1424 * non-leaf extent block with room for new records. There are three
1425 * valid results of this search:
1426 *
1427 * 1) a lowest extent block is found, then we pass it back in
1428 * *lowest_eb_bh and return '0'
1429 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001430 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001431 * pass NULL back in *lowest_eb_bh, but still return '0'
1432 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001433 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001434 * which case we return > 0
1435 *
1436 * return status < 0 indicates an error.
1437 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001438static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001439 struct buffer_head **target_bh)
1440{
1441 int status = 0, i;
1442 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001443 struct ocfs2_extent_block *eb;
1444 struct ocfs2_extent_list *el;
1445 struct buffer_head *bh = NULL;
1446 struct buffer_head *lowest_bh = NULL;
1447
1448 mlog_entry_void();
1449
1450 *target_bh = NULL;
1451
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001452 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001453
1454 while(le16_to_cpu(el->l_tree_depth) > 1) {
1455 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08001456 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1457 "Owner %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001458 "extent list (next_free_rec == 0)",
Joel Becker3d03a302009-02-12 17:49:26 -08001459 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001460 status = -EIO;
1461 goto bail;
1462 }
1463 i = le16_to_cpu(el->l_next_free_rec) - 1;
1464 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1465 if (!blkno) {
Joel Becker3d03a302009-02-12 17:49:26 -08001466 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1467 "Owner %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001468 "list where extent # %d has no physical "
1469 "block start",
Joel Becker3d03a302009-02-12 17:49:26 -08001470 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001471 status = -EIO;
1472 goto bail;
1473 }
1474
Mark Fasheha81cb882008-10-07 14:25:16 -07001475 brelse(bh);
1476 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001477
Joel Becker3d03a302009-02-12 17:49:26 -08001478 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001479 if (status < 0) {
1480 mlog_errno(status);
1481 goto bail;
1482 }
1483
1484 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001485 el = &eb->h_list;
1486
1487 if (le16_to_cpu(el->l_next_free_rec) <
1488 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001489 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001490 lowest_bh = bh;
1491 get_bh(lowest_bh);
1492 }
1493 }
1494
1495 /* If we didn't find one and the fe doesn't have any room,
1496 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001497 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001498 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001499 status = 1;
1500
1501 *target_bh = lowest_bh;
1502bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001503 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001504
1505 mlog_exit(status);
1506 return status;
1507}
1508
Mark Fashehe48edee2007-03-07 16:46:57 -08001509/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001510 * Grow a b-tree so that it has more records.
1511 *
1512 * We might shift the tree depth in which case existing paths should
1513 * be considered invalid.
1514 *
1515 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001516 *
1517 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001518 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001519static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1520 int *final_depth, struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001521 struct ocfs2_alloc_context *meta_ac)
1522{
1523 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001524 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001525 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001526 struct buffer_head *bh = NULL;
1527
1528 BUG_ON(meta_ac == NULL);
1529
Joel Beckerd401dc12009-02-13 02:24:10 -08001530 shift = ocfs2_find_branch_target(et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001531 if (shift < 0) {
1532 ret = shift;
1533 mlog_errno(ret);
1534 goto out;
1535 }
1536
1537 /* We traveled all the way to the bottom of the allocation tree
1538 * and didn't find room for any more extents - we need to add
1539 * another tree level */
1540 if (shift) {
1541 BUG_ON(bh);
1542 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1543
1544 /* ocfs2_shift_tree_depth will return us a buffer with
1545 * the new extent block (so we can pass that to
1546 * ocfs2_add_branch). */
Joel Beckerd401dc12009-02-13 02:24:10 -08001547 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001548 if (ret < 0) {
1549 mlog_errno(ret);
1550 goto out;
1551 }
1552 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001553 if (depth == 1) {
1554 /*
1555 * Special case: we have room now if we shifted from
1556 * tree_depth 0, so no more work needs to be done.
1557 *
1558 * We won't be calling add_branch, so pass
1559 * back *last_eb_bh as the new leaf. At depth
1560 * zero, it should always be null so there's
1561 * no reason to brelse.
1562 */
1563 BUG_ON(*last_eb_bh);
1564 get_bh(bh);
1565 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001566 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001567 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001568 }
1569
1570 /* call ocfs2_add_branch to add the final part of the tree with
1571 * the new data. */
1572 mlog(0, "add branch. bh = %p\n", bh);
Joel Beckerd401dc12009-02-13 02:24:10 -08001573 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001574 meta_ac);
1575 if (ret < 0) {
1576 mlog_errno(ret);
1577 goto out;
1578 }
1579
1580out:
1581 if (final_depth)
1582 *final_depth = depth;
1583 brelse(bh);
1584 return ret;
1585}
1586
1587/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001588 * This function will discard the rightmost extent record.
1589 */
1590static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1591{
1592 int next_free = le16_to_cpu(el->l_next_free_rec);
1593 int count = le16_to_cpu(el->l_count);
1594 unsigned int num_bytes;
1595
1596 BUG_ON(!next_free);
1597 /* This will cause us to go off the end of our extent list. */
1598 BUG_ON(next_free >= count);
1599
1600 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1601
1602 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1603}
1604
1605static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1606 struct ocfs2_extent_rec *insert_rec)
1607{
1608 int i, insert_index, next_free, has_empty, num_bytes;
1609 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1610 struct ocfs2_extent_rec *rec;
1611
1612 next_free = le16_to_cpu(el->l_next_free_rec);
1613 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1614
1615 BUG_ON(!next_free);
1616
1617 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001618 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001619
1620 /*
1621 * The easiest way to approach this is to just remove the
1622 * empty extent and temporarily decrement next_free.
1623 */
1624 if (has_empty) {
1625 /*
1626 * If next_free was 1 (only an empty extent), this
1627 * loop won't execute, which is fine. We still want
1628 * the decrement above to happen.
1629 */
1630 for(i = 0; i < (next_free - 1); i++)
1631 el->l_recs[i] = el->l_recs[i+1];
1632
1633 next_free--;
1634 }
1635
1636 /*
1637 * Figure out what the new record index should be.
1638 */
1639 for(i = 0; i < next_free; i++) {
1640 rec = &el->l_recs[i];
1641
1642 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1643 break;
1644 }
1645 insert_index = i;
1646
1647 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1648 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1649
1650 BUG_ON(insert_index < 0);
1651 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1652 BUG_ON(insert_index > next_free);
1653
1654 /*
1655 * No need to memmove if we're just adding to the tail.
1656 */
1657 if (insert_index != next_free) {
1658 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1659
1660 num_bytes = next_free - insert_index;
1661 num_bytes *= sizeof(struct ocfs2_extent_rec);
1662 memmove(&el->l_recs[insert_index + 1],
1663 &el->l_recs[insert_index],
1664 num_bytes);
1665 }
1666
1667 /*
1668 * Either we had an empty extent, and need to re-increment or
1669 * there was no empty extent on a non full rightmost leaf node,
1670 * in which case we still need to increment.
1671 */
1672 next_free++;
1673 el->l_next_free_rec = cpu_to_le16(next_free);
1674 /*
1675 * Make sure none of the math above just messed up our tree.
1676 */
1677 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1678
1679 el->l_recs[insert_index] = *insert_rec;
1680
1681}
1682
Mark Fasheh328d5752007-06-18 10:48:04 -07001683static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1684{
1685 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1686
1687 BUG_ON(num_recs == 0);
1688
1689 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1690 num_recs--;
1691 size = num_recs * sizeof(struct ocfs2_extent_rec);
1692 memmove(&el->l_recs[0], &el->l_recs[1], size);
1693 memset(&el->l_recs[num_recs], 0,
1694 sizeof(struct ocfs2_extent_rec));
1695 el->l_next_free_rec = cpu_to_le16(num_recs);
1696 }
1697}
1698
Mark Fashehdcd05382007-01-16 11:32:23 -08001699/*
1700 * Create an empty extent record .
1701 *
1702 * l_next_free_rec may be updated.
1703 *
1704 * If an empty extent already exists do nothing.
1705 */
1706static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1707{
1708 int next_free = le16_to_cpu(el->l_next_free_rec);
1709
Mark Fashehe48edee2007-03-07 16:46:57 -08001710 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1711
Mark Fashehdcd05382007-01-16 11:32:23 -08001712 if (next_free == 0)
1713 goto set_and_inc;
1714
1715 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1716 return;
1717
1718 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1719 "Asked to create an empty extent in a full list:\n"
1720 "count = %u, tree depth = %u",
1721 le16_to_cpu(el->l_count),
1722 le16_to_cpu(el->l_tree_depth));
1723
1724 ocfs2_shift_records_right(el);
1725
1726set_and_inc:
1727 le16_add_cpu(&el->l_next_free_rec, 1);
1728 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1729}
1730
1731/*
1732 * For a rotation which involves two leaf nodes, the "root node" is
1733 * the lowest level tree node which contains a path to both leafs. This
1734 * resulting set of information can be used to form a complete "subtree"
1735 *
1736 * This function is passed two full paths from the dinode down to a
1737 * pair of adjacent leaves. It's task is to figure out which path
1738 * index contains the subtree root - this can be the root index itself
1739 * in a worst-case rotation.
1740 *
1741 * The array index of the subtree root is passed back.
1742 */
Joel Becker7dc02802009-02-12 19:20:13 -08001743static int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08001744 struct ocfs2_path *left,
1745 struct ocfs2_path *right)
1746{
1747 int i = 0;
1748
1749 /*
1750 * Check that the caller passed in two paths from the same tree.
1751 */
1752 BUG_ON(path_root_bh(left) != path_root_bh(right));
1753
1754 do {
1755 i++;
1756
1757 /*
1758 * The caller didn't pass two adjacent paths.
1759 */
1760 mlog_bug_on_msg(i > left->p_tree_depth,
Joel Becker7dc02802009-02-12 19:20:13 -08001761 "Owner %llu, left depth %u, right depth %u\n"
Mark Fashehdcd05382007-01-16 11:32:23 -08001762 "left leaf blk %llu, right leaf blk %llu\n",
Joel Becker7dc02802009-02-12 19:20:13 -08001763 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1764 left->p_tree_depth, right->p_tree_depth,
Mark Fashehdcd05382007-01-16 11:32:23 -08001765 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1766 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1767 } while (left->p_node[i].bh->b_blocknr ==
1768 right->p_node[i].bh->b_blocknr);
1769
1770 return i - 1;
1771}
1772
1773typedef void (path_insert_t)(void *, struct buffer_head *);
1774
1775/*
1776 * Traverse a btree path in search of cpos, starting at root_el.
1777 *
1778 * This code can be called with a cpos larger than the tree, in which
1779 * case it will return the rightmost path.
1780 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001781static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
Mark Fashehdcd05382007-01-16 11:32:23 -08001782 struct ocfs2_extent_list *root_el, u32 cpos,
1783 path_insert_t *func, void *data)
1784{
1785 int i, ret = 0;
1786 u32 range;
1787 u64 blkno;
1788 struct buffer_head *bh = NULL;
1789 struct ocfs2_extent_block *eb;
1790 struct ocfs2_extent_list *el;
1791 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08001792
1793 el = root_el;
1794 while (el->l_tree_depth) {
1795 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001796 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1797 "Owner %llu has empty extent list at "
Mark Fashehdcd05382007-01-16 11:32:23 -08001798 "depth %u\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001799 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001800 le16_to_cpu(el->l_tree_depth));
1801 ret = -EROFS;
1802 goto out;
1803
1804 }
1805
1806 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1807 rec = &el->l_recs[i];
1808
1809 /*
1810 * In the case that cpos is off the allocation
1811 * tree, this should just wind up returning the
1812 * rightmost record.
1813 */
1814 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001815 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001816 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1817 break;
1818 }
1819
1820 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1821 if (blkno == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001822 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1823 "Owner %llu has bad blkno in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001824 "at depth %u (index %d)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001825 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001826 le16_to_cpu(el->l_tree_depth), i);
1827 ret = -EROFS;
1828 goto out;
1829 }
1830
1831 brelse(bh);
1832 bh = NULL;
Joel Beckerfacdb772009-02-12 18:08:48 -08001833 ret = ocfs2_read_extent_block(ci, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001834 if (ret) {
1835 mlog_errno(ret);
1836 goto out;
1837 }
1838
1839 eb = (struct ocfs2_extent_block *) bh->b_data;
1840 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001841
1842 if (le16_to_cpu(el->l_next_free_rec) >
1843 le16_to_cpu(el->l_count)) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001844 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1845 "Owner %llu has bad count in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001846 "at block %llu (next free=%u, count=%u)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001847 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001848 (unsigned long long)bh->b_blocknr,
1849 le16_to_cpu(el->l_next_free_rec),
1850 le16_to_cpu(el->l_count));
1851 ret = -EROFS;
1852 goto out;
1853 }
1854
1855 if (func)
1856 func(data, bh);
1857 }
1858
1859out:
1860 /*
1861 * Catch any trailing bh that the loop didn't handle.
1862 */
1863 brelse(bh);
1864
1865 return ret;
1866}
1867
1868/*
1869 * Given an initialized path (that is, it has a valid root extent
1870 * list), this function will traverse the btree in search of the path
1871 * which would contain cpos.
1872 *
1873 * The path traveled is recorded in the path structure.
1874 *
1875 * Note that this will not do any comparisons on leaf node extent
1876 * records, so it will work fine in the case that we just added a tree
1877 * branch.
1878 */
1879struct find_path_data {
1880 int index;
1881 struct ocfs2_path *path;
1882};
1883static void find_path_ins(void *data, struct buffer_head *bh)
1884{
1885 struct find_path_data *fp = data;
1886
1887 get_bh(bh);
1888 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1889 fp->index++;
1890}
Joel Beckerfacdb772009-02-12 18:08:48 -08001891static int ocfs2_find_path(struct ocfs2_caching_info *ci,
1892 struct ocfs2_path *path, u32 cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08001893{
1894 struct find_path_data data;
1895
1896 data.index = 1;
1897 data.path = path;
Joel Beckerfacdb772009-02-12 18:08:48 -08001898 return __ocfs2_find_path(ci, path_root_el(path), cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08001899 find_path_ins, &data);
1900}
1901
1902static void find_leaf_ins(void *data, struct buffer_head *bh)
1903{
1904 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1905 struct ocfs2_extent_list *el = &eb->h_list;
1906 struct buffer_head **ret = data;
1907
1908 /* We want to retain only the leaf block. */
1909 if (le16_to_cpu(el->l_tree_depth) == 0) {
1910 get_bh(bh);
1911 *ret = bh;
1912 }
1913}
1914/*
1915 * Find the leaf block in the tree which would contain cpos. No
1916 * checking of the actual leaf is done.
1917 *
1918 * Some paths want to call this instead of allocating a path structure
1919 * and calling ocfs2_find_path().
1920 *
1921 * This function doesn't handle non btree extent lists.
1922 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001923int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1924 struct ocfs2_extent_list *root_el, u32 cpos,
1925 struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001926{
1927 int ret;
1928 struct buffer_head *bh = NULL;
1929
Joel Beckerfacdb772009-02-12 18:08:48 -08001930 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001931 if (ret) {
1932 mlog_errno(ret);
1933 goto out;
1934 }
1935
1936 *leaf_bh = bh;
1937out:
1938 return ret;
1939}
1940
1941/*
1942 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1943 *
1944 * Basically, we've moved stuff around at the bottom of the tree and
1945 * we need to fix up the extent records above the changes to reflect
1946 * the new changes.
1947 *
1948 * left_rec: the record on the left.
1949 * left_child_el: is the child list pointed to by left_rec
1950 * right_rec: the record to the right of left_rec
1951 * right_child_el: is the child list pointed to by right_rec
1952 *
1953 * By definition, this only works on interior nodes.
1954 */
1955static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1956 struct ocfs2_extent_list *left_child_el,
1957 struct ocfs2_extent_rec *right_rec,
1958 struct ocfs2_extent_list *right_child_el)
1959{
1960 u32 left_clusters, right_end;
1961
1962 /*
1963 * Interior nodes never have holes. Their cpos is the cpos of
1964 * the leftmost record in their child list. Their cluster
1965 * count covers the full theoretical range of their child list
1966 * - the range between their cpos and the cpos of the record
1967 * immediately to their right.
1968 */
1969 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08001970 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1971 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07001972 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1973 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1974 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001975 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001976 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001977
1978 /*
1979 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001980 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001981 * updating e_cpos to keep the same highest cluster count.
1982 */
1983 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001984 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001985
1986 right_rec->e_cpos = left_rec->e_cpos;
1987 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1988
1989 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001990 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001991}
1992
1993/*
1994 * Adjust the adjacent root node records involved in a
1995 * rotation. left_el_blkno is passed in as a key so that we can easily
1996 * find it's index in the root list.
1997 */
1998static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1999 struct ocfs2_extent_list *left_el,
2000 struct ocfs2_extent_list *right_el,
2001 u64 left_el_blkno)
2002{
2003 int i;
2004
2005 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
2006 le16_to_cpu(left_el->l_tree_depth));
2007
2008 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
2009 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
2010 break;
2011 }
2012
2013 /*
2014 * The path walking code should have never returned a root and
2015 * two paths which are not adjacent.
2016 */
2017 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2018
2019 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
2020 &root_el->l_recs[i + 1], right_el);
2021}
2022
2023/*
2024 * We've changed a leaf block (in right_path) and need to reflect that
2025 * change back up the subtree.
2026 *
2027 * This happens in multiple places:
2028 * - When we've moved an extent record from the left path leaf to the right
2029 * path leaf to make room for an empty extent in the left path leaf.
2030 * - When our insert into the right path leaf is at the leftmost edge
2031 * and requires an update of the path immediately to it's left. This
2032 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08002033 * - When we've adjusted the last extent record in the left path leaf and the
2034 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08002035 */
Joel Becker4619c732009-02-12 19:02:36 -08002036static void ocfs2_complete_edge_insert(handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08002037 struct ocfs2_path *left_path,
2038 struct ocfs2_path *right_path,
2039 int subtree_index)
2040{
2041 int ret, i, idx;
2042 struct ocfs2_extent_list *el, *left_el, *right_el;
2043 struct ocfs2_extent_rec *left_rec, *right_rec;
2044 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2045
2046 /*
2047 * Update the counts and position values within all the
2048 * interior nodes to reflect the leaf rotation we just did.
2049 *
2050 * The root node is handled below the loop.
2051 *
2052 * We begin the loop with right_el and left_el pointing to the
2053 * leaf lists and work our way up.
2054 *
2055 * NOTE: within this loop, left_el and right_el always refer
2056 * to the *child* lists.
2057 */
2058 left_el = path_leaf_el(left_path);
2059 right_el = path_leaf_el(right_path);
2060 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2061 mlog(0, "Adjust records at index %u\n", i);
2062
2063 /*
2064 * One nice property of knowing that all of these
2065 * nodes are below the root is that we only deal with
2066 * the leftmost right node record and the rightmost
2067 * left node record.
2068 */
2069 el = left_path->p_node[i].el;
2070 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2071 left_rec = &el->l_recs[idx];
2072
2073 el = right_path->p_node[i].el;
2074 right_rec = &el->l_recs[0];
2075
2076 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2077 right_el);
2078
2079 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2080 if (ret)
2081 mlog_errno(ret);
2082
2083 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2084 if (ret)
2085 mlog_errno(ret);
2086
2087 /*
2088 * Setup our list pointers now so that the current
2089 * parents become children in the next iteration.
2090 */
2091 left_el = left_path->p_node[i].el;
2092 right_el = right_path->p_node[i].el;
2093 }
2094
2095 /*
2096 * At the root node, adjust the two adjacent records which
2097 * begin our path to the leaves.
2098 */
2099
2100 el = left_path->p_node[subtree_index].el;
2101 left_el = left_path->p_node[subtree_index + 1].el;
2102 right_el = right_path->p_node[subtree_index + 1].el;
2103
2104 ocfs2_adjust_root_records(el, left_el, right_el,
2105 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2106
2107 root_bh = left_path->p_node[subtree_index].bh;
2108
2109 ret = ocfs2_journal_dirty(handle, root_bh);
2110 if (ret)
2111 mlog_errno(ret);
2112}
2113
Joel Becker5c601ab2009-02-12 19:10:13 -08002114static int ocfs2_rotate_subtree_right(handle_t *handle,
2115 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08002116 struct ocfs2_path *left_path,
2117 struct ocfs2_path *right_path,
2118 int subtree_index)
2119{
2120 int ret, i;
2121 struct buffer_head *right_leaf_bh;
2122 struct buffer_head *left_leaf_bh = NULL;
2123 struct buffer_head *root_bh;
2124 struct ocfs2_extent_list *right_el, *left_el;
2125 struct ocfs2_extent_rec move_rec;
2126
2127 left_leaf_bh = path_leaf_bh(left_path);
2128 left_el = path_leaf_el(left_path);
2129
2130 if (left_el->l_next_free_rec != left_el->l_count) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002131 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002132 "Inode %llu has non-full interior leaf node %llu"
2133 "(next free = %u)",
Joel Becker5c601ab2009-02-12 19:10:13 -08002134 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002135 (unsigned long long)left_leaf_bh->b_blocknr,
2136 le16_to_cpu(left_el->l_next_free_rec));
2137 return -EROFS;
2138 }
2139
2140 /*
2141 * This extent block may already have an empty record, so we
2142 * return early if so.
2143 */
2144 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2145 return 0;
2146
2147 root_bh = left_path->p_node[subtree_index].bh;
2148 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2149
Joel Becker5c601ab2009-02-12 19:10:13 -08002150 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002151 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002152 if (ret) {
2153 mlog_errno(ret);
2154 goto out;
2155 }
2156
2157 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002158 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002159 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002160 if (ret) {
2161 mlog_errno(ret);
2162 goto out;
2163 }
2164
Joel Becker5c601ab2009-02-12 19:10:13 -08002165 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002166 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002167 if (ret) {
2168 mlog_errno(ret);
2169 goto out;
2170 }
2171 }
2172
2173 right_leaf_bh = path_leaf_bh(right_path);
2174 right_el = path_leaf_el(right_path);
2175
2176 /* This is a code error, not a disk corruption. */
2177 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2178 "because rightmost leaf block %llu is empty\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002179 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002180 (unsigned long long)right_leaf_bh->b_blocknr);
2181
2182 ocfs2_create_empty_extent(right_el);
2183
2184 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2185 if (ret) {
2186 mlog_errno(ret);
2187 goto out;
2188 }
2189
2190 /* Do the copy now. */
2191 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2192 move_rec = left_el->l_recs[i];
2193 right_el->l_recs[0] = move_rec;
2194
2195 /*
2196 * Clear out the record we just copied and shift everything
2197 * over, leaving an empty extent in the left leaf.
2198 *
2199 * We temporarily subtract from next_free_rec so that the
2200 * shift will lose the tail record (which is now defunct).
2201 */
2202 le16_add_cpu(&left_el->l_next_free_rec, -1);
2203 ocfs2_shift_records_right(left_el);
2204 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2205 le16_add_cpu(&left_el->l_next_free_rec, 1);
2206
2207 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2208 if (ret) {
2209 mlog_errno(ret);
2210 goto out;
2211 }
2212
Joel Becker4619c732009-02-12 19:02:36 -08002213 ocfs2_complete_edge_insert(handle, left_path, right_path,
2214 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002215
2216out:
2217 return ret;
2218}
2219
2220/*
2221 * Given a full path, determine what cpos value would return us a path
2222 * containing the leaf immediately to the left of the current one.
2223 *
2224 * Will return zero if the path passed in is already the leftmost path.
2225 */
2226static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2227 struct ocfs2_path *path, u32 *cpos)
2228{
2229 int i, j, ret = 0;
2230 u64 blkno;
2231 struct ocfs2_extent_list *el;
2232
Mark Fashehe48edee2007-03-07 16:46:57 -08002233 BUG_ON(path->p_tree_depth == 0);
2234
Mark Fashehdcd05382007-01-16 11:32:23 -08002235 *cpos = 0;
2236
2237 blkno = path_leaf_bh(path)->b_blocknr;
2238
2239 /* Start at the tree node just above the leaf and work our way up. */
2240 i = path->p_tree_depth - 1;
2241 while (i >= 0) {
2242 el = path->p_node[i].el;
2243
2244 /*
2245 * Find the extent record just before the one in our
2246 * path.
2247 */
2248 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2249 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2250 if (j == 0) {
2251 if (i == 0) {
2252 /*
2253 * We've determined that the
2254 * path specified is already
2255 * the leftmost one - return a
2256 * cpos of zero.
2257 */
2258 goto out;
2259 }
2260 /*
2261 * The leftmost record points to our
2262 * leaf - we need to travel up the
2263 * tree one level.
2264 */
2265 goto next_node;
2266 }
2267
2268 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002269 *cpos = *cpos + ocfs2_rec_clusters(el,
2270 &el->l_recs[j - 1]);
2271 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002272 goto out;
2273 }
2274 }
2275
2276 /*
2277 * If we got here, we never found a valid node where
2278 * the tree indicated one should be.
2279 */
2280 ocfs2_error(sb,
2281 "Invalid extent tree at extent block %llu\n",
2282 (unsigned long long)blkno);
2283 ret = -EROFS;
2284 goto out;
2285
2286next_node:
2287 blkno = path->p_node[i].bh->b_blocknr;
2288 i--;
2289 }
2290
2291out:
2292 return ret;
2293}
2294
Mark Fasheh328d5752007-06-18 10:48:04 -07002295/*
2296 * Extend the transaction by enough credits to complete the rotation,
2297 * and still leave at least the original number of credits allocated
2298 * to this transaction.
2299 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002300static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002301 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002302 struct ocfs2_path *path)
2303{
Mark Fasheh328d5752007-06-18 10:48:04 -07002304 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002305
2306 if (handle->h_buffer_credits < credits)
2307 return ocfs2_extend_trans(handle, credits);
2308
2309 return 0;
2310}
2311
2312/*
2313 * Trap the case where we're inserting into the theoretical range past
2314 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2315 * whose cpos is less than ours into the right leaf.
2316 *
2317 * It's only necessary to look at the rightmost record of the left
2318 * leaf because the logic that calls us should ensure that the
2319 * theoretical ranges in the path components above the leaves are
2320 * correct.
2321 */
2322static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2323 u32 insert_cpos)
2324{
2325 struct ocfs2_extent_list *left_el;
2326 struct ocfs2_extent_rec *rec;
2327 int next_free;
2328
2329 left_el = path_leaf_el(left_path);
2330 next_free = le16_to_cpu(left_el->l_next_free_rec);
2331 rec = &left_el->l_recs[next_free - 1];
2332
2333 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2334 return 1;
2335 return 0;
2336}
2337
Mark Fasheh328d5752007-06-18 10:48:04 -07002338static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2339{
2340 int next_free = le16_to_cpu(el->l_next_free_rec);
2341 unsigned int range;
2342 struct ocfs2_extent_rec *rec;
2343
2344 if (next_free == 0)
2345 return 0;
2346
2347 rec = &el->l_recs[0];
2348 if (ocfs2_is_empty_extent(rec)) {
2349 /* Empty list. */
2350 if (next_free == 1)
2351 return 0;
2352 rec = &el->l_recs[1];
2353 }
2354
2355 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2356 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2357 return 1;
2358 return 0;
2359}
2360
Mark Fashehdcd05382007-01-16 11:32:23 -08002361/*
2362 * Rotate all the records in a btree right one record, starting at insert_cpos.
2363 *
2364 * The path to the rightmost leaf should be passed in.
2365 *
2366 * The array is assumed to be large enough to hold an entire path (tree depth).
2367 *
2368 * Upon succesful return from this function:
2369 *
2370 * - The 'right_path' array will contain a path to the leaf block
2371 * whose range contains e_cpos.
2372 * - That leaf block will have a single empty extent in list index 0.
2373 * - In the case that the rotation requires a post-insert update,
2374 * *ret_left_path will contain a valid path which can be passed to
2375 * ocfs2_insert_path().
2376 */
Joel Becker1bbf0b82009-02-12 19:42:08 -08002377static int ocfs2_rotate_tree_right(handle_t *handle,
Joel Becker5c601ab2009-02-12 19:10:13 -08002378 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002379 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002380 u32 insert_cpos,
2381 struct ocfs2_path *right_path,
2382 struct ocfs2_path **ret_left_path)
2383{
Mark Fasheh328d5752007-06-18 10:48:04 -07002384 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002385 u32 cpos;
2386 struct ocfs2_path *left_path = NULL;
Joel Becker5c601ab2009-02-12 19:10:13 -08002387 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehdcd05382007-01-16 11:32:23 -08002388
2389 *ret_left_path = NULL;
2390
Joel Beckerffdd7a52008-10-17 22:32:01 -07002391 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002392 if (!left_path) {
2393 ret = -ENOMEM;
2394 mlog_errno(ret);
2395 goto out;
2396 }
2397
Joel Becker5c601ab2009-02-12 19:10:13 -08002398 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002399 if (ret) {
2400 mlog_errno(ret);
2401 goto out;
2402 }
2403
2404 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2405
2406 /*
2407 * What we want to do here is:
2408 *
2409 * 1) Start with the rightmost path.
2410 *
2411 * 2) Determine a path to the leaf block directly to the left
2412 * of that leaf.
2413 *
2414 * 3) Determine the 'subtree root' - the lowest level tree node
2415 * which contains a path to both leaves.
2416 *
2417 * 4) Rotate the subtree.
2418 *
2419 * 5) Find the next subtree by considering the left path to be
2420 * the new right path.
2421 *
2422 * The check at the top of this while loop also accepts
2423 * insert_cpos == cpos because cpos is only a _theoretical_
2424 * value to get us the left path - insert_cpos might very well
2425 * be filling that hole.
2426 *
2427 * Stop at a cpos of '0' because we either started at the
2428 * leftmost branch (i.e., a tree with one branch and a
2429 * rotation inside of it), or we've gone as far as we can in
2430 * rotating subtrees.
2431 */
2432 while (cpos && insert_cpos <= cpos) {
2433 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2434 insert_cpos, cpos);
2435
Joel Becker5c601ab2009-02-12 19:10:13 -08002436 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002437 if (ret) {
2438 mlog_errno(ret);
2439 goto out;
2440 }
2441
2442 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2443 path_leaf_bh(right_path),
Joel Becker5c601ab2009-02-12 19:10:13 -08002444 "Owner %llu: error during insert of %u "
Mark Fashehdcd05382007-01-16 11:32:23 -08002445 "(left path cpos %u) results in two identical "
2446 "paths ending at %llu\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002447 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2448 insert_cpos, cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08002449 (unsigned long long)
2450 path_leaf_bh(left_path)->b_blocknr);
2451
Mark Fasheh328d5752007-06-18 10:48:04 -07002452 if (split == SPLIT_NONE &&
2453 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002454 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002455
2456 /*
2457 * We've rotated the tree as much as we
2458 * should. The rest is up to
2459 * ocfs2_insert_path() to complete, after the
2460 * record insertion. We indicate this
2461 * situation by returning the left path.
2462 *
2463 * The reason we don't adjust the records here
2464 * before the record insert is that an error
2465 * later might break the rule where a parent
2466 * record e_cpos will reflect the actual
2467 * e_cpos of the 1st nonempty record of the
2468 * child list.
2469 */
2470 *ret_left_path = left_path;
2471 goto out_ret_path;
2472 }
2473
Joel Becker7dc02802009-02-12 19:20:13 -08002474 start = ocfs2_find_subtree_root(et, left_path, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002475
2476 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2477 start,
2478 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2479 right_path->p_tree_depth);
2480
2481 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002482 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002483 if (ret) {
2484 mlog_errno(ret);
2485 goto out;
2486 }
2487
Joel Becker5c601ab2009-02-12 19:10:13 -08002488 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002489 right_path, start);
2490 if (ret) {
2491 mlog_errno(ret);
2492 goto out;
2493 }
2494
Mark Fasheh328d5752007-06-18 10:48:04 -07002495 if (split != SPLIT_NONE &&
2496 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2497 insert_cpos)) {
2498 /*
2499 * A rotate moves the rightmost left leaf
2500 * record over to the leftmost right leaf
2501 * slot. If we're doing an extent split
2502 * instead of a real insert, then we have to
2503 * check that the extent to be split wasn't
2504 * just moved over. If it was, then we can
2505 * exit here, passing left_path back -
2506 * ocfs2_split_extent() is smart enough to
2507 * search both leaves.
2508 */
2509 *ret_left_path = left_path;
2510 goto out_ret_path;
2511 }
2512
Mark Fashehdcd05382007-01-16 11:32:23 -08002513 /*
2514 * There is no need to re-read the next right path
2515 * as we know that it'll be our current left
2516 * path. Optimize by copying values instead.
2517 */
2518 ocfs2_mv_path(right_path, left_path);
2519
Joel Becker5c601ab2009-02-12 19:10:13 -08002520 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002521 if (ret) {
2522 mlog_errno(ret);
2523 goto out;
2524 }
2525 }
2526
2527out:
2528 ocfs2_free_path(left_path);
2529
2530out_ret_path:
2531 return ret;
2532}
2533
Joel Becker09106ba2009-02-12 19:43:57 -08002534static int ocfs2_update_edge_lengths(handle_t *handle,
2535 struct ocfs2_extent_tree *et,
Tao Ma3c5e1062009-07-21 15:42:05 +08002536 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002537{
Tao Ma3c5e1062009-07-21 15:42:05 +08002538 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002539 struct ocfs2_extent_rec *rec;
2540 struct ocfs2_extent_list *el;
2541 struct ocfs2_extent_block *eb;
2542 u32 range;
2543
Tao Ma3c5e1062009-07-21 15:42:05 +08002544 /*
2545 * In normal tree rotation process, we will never touch the
2546 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2547 * doesn't reserve the credits for them either.
2548 *
2549 * But we do have a special case here which will update the rightmost
2550 * records for all the bh in the path.
2551 * So we have to allocate extra credits and access them.
2552 */
2553 ret = ocfs2_extend_trans(handle,
2554 handle->h_buffer_credits + subtree_index);
2555 if (ret) {
2556 mlog_errno(ret);
2557 goto out;
2558 }
2559
Joel Becker09106ba2009-02-12 19:43:57 -08002560 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002561 if (ret) {
2562 mlog_errno(ret);
2563 goto out;
2564 }
2565
Mark Fasheh328d5752007-06-18 10:48:04 -07002566 /* Path should always be rightmost. */
2567 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2568 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2569
2570 el = &eb->h_list;
2571 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2572 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2573 rec = &el->l_recs[idx];
2574 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2575
2576 for (i = 0; i < path->p_tree_depth; i++) {
2577 el = path->p_node[i].el;
2578 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2579 rec = &el->l_recs[idx];
2580
2581 rec->e_int_clusters = cpu_to_le32(range);
2582 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2583
2584 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2585 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002586out:
2587 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002588}
2589
Joel Becker6641b0c2009-02-12 18:57:52 -08002590static void ocfs2_unlink_path(handle_t *handle,
2591 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002592 struct ocfs2_cached_dealloc_ctxt *dealloc,
2593 struct ocfs2_path *path, int unlink_start)
2594{
2595 int ret, i;
2596 struct ocfs2_extent_block *eb;
2597 struct ocfs2_extent_list *el;
2598 struct buffer_head *bh;
2599
2600 for(i = unlink_start; i < path_num_items(path); i++) {
2601 bh = path->p_node[i].bh;
2602
2603 eb = (struct ocfs2_extent_block *)bh->b_data;
2604 /*
2605 * Not all nodes might have had their final count
2606 * decremented by the caller - handle this here.
2607 */
2608 el = &eb->h_list;
2609 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2610 mlog(ML_ERROR,
2611 "Inode %llu, attempted to remove extent block "
2612 "%llu with %u records\n",
Joel Becker6641b0c2009-02-12 18:57:52 -08002613 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07002614 (unsigned long long)le64_to_cpu(eb->h_blkno),
2615 le16_to_cpu(el->l_next_free_rec));
2616
2617 ocfs2_journal_dirty(handle, bh);
Joel Becker6641b0c2009-02-12 18:57:52 -08002618 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002619 continue;
2620 }
2621
2622 el->l_next_free_rec = 0;
2623 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2624
2625 ocfs2_journal_dirty(handle, bh);
2626
2627 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2628 if (ret)
2629 mlog_errno(ret);
2630
Joel Becker6641b0c2009-02-12 18:57:52 -08002631 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002632 }
2633}
2634
Joel Becker6641b0c2009-02-12 18:57:52 -08002635static void ocfs2_unlink_subtree(handle_t *handle,
2636 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002637 struct ocfs2_path *left_path,
2638 struct ocfs2_path *right_path,
2639 int subtree_index,
2640 struct ocfs2_cached_dealloc_ctxt *dealloc)
2641{
2642 int i;
2643 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2644 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2645 struct ocfs2_extent_list *el;
2646 struct ocfs2_extent_block *eb;
2647
2648 el = path_leaf_el(left_path);
2649
2650 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2651
2652 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2653 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2654 break;
2655
2656 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2657
2658 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2659 le16_add_cpu(&root_el->l_next_free_rec, -1);
2660
2661 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2662 eb->h_next_leaf_blk = 0;
2663
2664 ocfs2_journal_dirty(handle, root_bh);
2665 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2666
Joel Becker6641b0c2009-02-12 18:57:52 -08002667 ocfs2_unlink_path(handle, et, dealloc, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002668 subtree_index + 1);
2669}
2670
Joel Becker1e2dd632009-02-12 19:45:28 -08002671static int ocfs2_rotate_subtree_left(handle_t *handle,
2672 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002673 struct ocfs2_path *left_path,
2674 struct ocfs2_path *right_path,
2675 int subtree_index,
2676 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker1e2dd632009-02-12 19:45:28 -08002677 int *deleted)
Mark Fasheh328d5752007-06-18 10:48:04 -07002678{
2679 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002680 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002681 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2682 struct ocfs2_extent_block *eb;
2683
2684 *deleted = 0;
2685
2686 right_leaf_el = path_leaf_el(right_path);
2687 left_leaf_el = path_leaf_el(left_path);
2688 root_bh = left_path->p_node[subtree_index].bh;
2689 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2690
2691 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2692 return 0;
2693
2694 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2695 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2696 /*
2697 * It's legal for us to proceed if the right leaf is
2698 * the rightmost one and it has an empty extent. There
2699 * are two cases to handle - whether the leaf will be
2700 * empty after removal or not. If the leaf isn't empty
2701 * then just remove the empty extent up front. The
2702 * next block will handle empty leaves by flagging
2703 * them for unlink.
2704 *
2705 * Non rightmost leaves will throw -EAGAIN and the
2706 * caller can manually move the subtree and retry.
2707 */
2708
2709 if (eb->h_next_leaf_blk != 0ULL)
2710 return -EAGAIN;
2711
2712 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002713 ret = ocfs2_journal_access_eb(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002714 path_leaf_bh(right_path),
2715 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002716 if (ret) {
2717 mlog_errno(ret);
2718 goto out;
2719 }
2720
2721 ocfs2_remove_empty_extent(right_leaf_el);
2722 } else
2723 right_has_empty = 1;
2724 }
2725
2726 if (eb->h_next_leaf_blk == 0ULL &&
2727 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2728 /*
2729 * We have to update i_last_eb_blk during the meta
2730 * data delete.
2731 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08002732 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07002733 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002734 if (ret) {
2735 mlog_errno(ret);
2736 goto out;
2737 }
2738
2739 del_right_subtree = 1;
2740 }
2741
2742 /*
2743 * Getting here with an empty extent in the right path implies
2744 * that it's the rightmost path and will be deleted.
2745 */
2746 BUG_ON(right_has_empty && !del_right_subtree);
2747
Joel Becker1e2dd632009-02-12 19:45:28 -08002748 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002749 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002750 if (ret) {
2751 mlog_errno(ret);
2752 goto out;
2753 }
2754
2755 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002756 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002757 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002758 if (ret) {
2759 mlog_errno(ret);
2760 goto out;
2761 }
2762
Joel Becker1e2dd632009-02-12 19:45:28 -08002763 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002764 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002765 if (ret) {
2766 mlog_errno(ret);
2767 goto out;
2768 }
2769 }
2770
2771 if (!right_has_empty) {
2772 /*
2773 * Only do this if we're moving a real
2774 * record. Otherwise, the action is delayed until
2775 * after removal of the right path in which case we
2776 * can do a simple shift to remove the empty extent.
2777 */
2778 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2779 memset(&right_leaf_el->l_recs[0], 0,
2780 sizeof(struct ocfs2_extent_rec));
2781 }
2782 if (eb->h_next_leaf_blk == 0ULL) {
2783 /*
2784 * Move recs over to get rid of empty extent, decrease
2785 * next_free. This is allowed to remove the last
2786 * extent in our leaf (setting l_next_free_rec to
2787 * zero) - the delete code below won't care.
2788 */
2789 ocfs2_remove_empty_extent(right_leaf_el);
2790 }
2791
2792 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2793 if (ret)
2794 mlog_errno(ret);
2795 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2796 if (ret)
2797 mlog_errno(ret);
2798
2799 if (del_right_subtree) {
Joel Becker6641b0c2009-02-12 18:57:52 -08002800 ocfs2_unlink_subtree(handle, et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002801 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08002802 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08002803 left_path);
2804 if (ret) {
2805 mlog_errno(ret);
2806 goto out;
2807 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002808
2809 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002810 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002811
2812 /*
2813 * Removal of the extent in the left leaf was skipped
2814 * above so we could delete the right path
2815 * 1st.
2816 */
2817 if (right_has_empty)
2818 ocfs2_remove_empty_extent(left_leaf_el);
2819
Tao Mae7d4cb62008-08-18 17:38:44 +08002820 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002821 if (ret)
2822 mlog_errno(ret);
2823
2824 *deleted = 1;
2825 } else
Joel Becker4619c732009-02-12 19:02:36 -08002826 ocfs2_complete_edge_insert(handle, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002827 subtree_index);
2828
2829out:
2830 return ret;
2831}
2832
2833/*
2834 * Given a full path, determine what cpos value would return us a path
2835 * containing the leaf immediately to the right of the current one.
2836 *
2837 * Will return zero if the path passed in is already the rightmost path.
2838 *
2839 * This looks similar, but is subtly different to
2840 * ocfs2_find_cpos_for_left_leaf().
2841 */
2842static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2843 struct ocfs2_path *path, u32 *cpos)
2844{
2845 int i, j, ret = 0;
2846 u64 blkno;
2847 struct ocfs2_extent_list *el;
2848
2849 *cpos = 0;
2850
2851 if (path->p_tree_depth == 0)
2852 return 0;
2853
2854 blkno = path_leaf_bh(path)->b_blocknr;
2855
2856 /* Start at the tree node just above the leaf and work our way up. */
2857 i = path->p_tree_depth - 1;
2858 while (i >= 0) {
2859 int next_free;
2860
2861 el = path->p_node[i].el;
2862
2863 /*
2864 * Find the extent record just after the one in our
2865 * path.
2866 */
2867 next_free = le16_to_cpu(el->l_next_free_rec);
2868 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2869 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2870 if (j == (next_free - 1)) {
2871 if (i == 0) {
2872 /*
2873 * We've determined that the
2874 * path specified is already
2875 * the rightmost one - return a
2876 * cpos of zero.
2877 */
2878 goto out;
2879 }
2880 /*
2881 * The rightmost record points to our
2882 * leaf - we need to travel up the
2883 * tree one level.
2884 */
2885 goto next_node;
2886 }
2887
2888 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2889 goto out;
2890 }
2891 }
2892
2893 /*
2894 * If we got here, we never found a valid node where
2895 * the tree indicated one should be.
2896 */
2897 ocfs2_error(sb,
2898 "Invalid extent tree at extent block %llu\n",
2899 (unsigned long long)blkno);
2900 ret = -EROFS;
2901 goto out;
2902
2903next_node:
2904 blkno = path->p_node[i].bh->b_blocknr;
2905 i--;
2906 }
2907
2908out:
2909 return ret;
2910}
2911
Joel Becker70f18c02009-02-13 02:09:31 -08002912static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2913 struct ocfs2_extent_tree *et,
Joel Becker13723d02008-10-17 19:25:01 -07002914 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002915{
2916 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002917 struct buffer_head *bh = path_leaf_bh(path);
2918 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002919
2920 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2921 return 0;
2922
Joel Becker70f18c02009-02-13 02:09:31 -08002923 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Joel Becker13723d02008-10-17 19:25:01 -07002924 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002925 if (ret) {
2926 mlog_errno(ret);
2927 goto out;
2928 }
2929
2930 ocfs2_remove_empty_extent(el);
2931
2932 ret = ocfs2_journal_dirty(handle, bh);
2933 if (ret)
2934 mlog_errno(ret);
2935
2936out:
2937 return ret;
2938}
2939
Joel Beckere46f74d2009-02-12 19:47:43 -08002940static int __ocfs2_rotate_tree_left(handle_t *handle,
2941 struct ocfs2_extent_tree *et,
2942 int orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07002943 struct ocfs2_path *path,
2944 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08002945 struct ocfs2_path **empty_extent_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002946{
2947 int ret, subtree_root, deleted;
2948 u32 right_cpos;
2949 struct ocfs2_path *left_path = NULL;
2950 struct ocfs2_path *right_path = NULL;
Joel Beckere46f74d2009-02-12 19:47:43 -08002951 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fasheh328d5752007-06-18 10:48:04 -07002952
2953 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2954
2955 *empty_extent_path = NULL;
2956
Joel Beckere46f74d2009-02-12 19:47:43 -08002957 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002958 if (ret) {
2959 mlog_errno(ret);
2960 goto out;
2961 }
2962
Joel Beckerffdd7a52008-10-17 22:32:01 -07002963 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002964 if (!left_path) {
2965 ret = -ENOMEM;
2966 mlog_errno(ret);
2967 goto out;
2968 }
2969
2970 ocfs2_cp_path(left_path, path);
2971
Joel Beckerffdd7a52008-10-17 22:32:01 -07002972 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002973 if (!right_path) {
2974 ret = -ENOMEM;
2975 mlog_errno(ret);
2976 goto out;
2977 }
2978
2979 while (right_cpos) {
Joel Beckerfacdb772009-02-12 18:08:48 -08002980 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002981 if (ret) {
2982 mlog_errno(ret);
2983 goto out;
2984 }
2985
Joel Becker7dc02802009-02-12 19:20:13 -08002986 subtree_root = ocfs2_find_subtree_root(et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002987 right_path);
2988
2989 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2990 subtree_root,
2991 (unsigned long long)
2992 right_path->p_node[subtree_root].bh->b_blocknr,
2993 right_path->p_tree_depth);
2994
2995 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2996 orig_credits, left_path);
2997 if (ret) {
2998 mlog_errno(ret);
2999 goto out;
3000 }
3001
Mark Fashehe8aed342007-12-03 16:43:01 -08003002 /*
3003 * Caller might still want to make changes to the
3004 * tree root, so re-add it to the journal here.
3005 */
Joel Beckere46f74d2009-02-12 19:47:43 -08003006 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003007 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08003008 if (ret) {
3009 mlog_errno(ret);
3010 goto out;
3011 }
3012
Joel Becker1e2dd632009-02-12 19:45:28 -08003013 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003014 right_path, subtree_root,
Joel Becker1e2dd632009-02-12 19:45:28 -08003015 dealloc, &deleted);
Mark Fasheh328d5752007-06-18 10:48:04 -07003016 if (ret == -EAGAIN) {
3017 /*
3018 * The rotation has to temporarily stop due to
3019 * the right subtree having an empty
3020 * extent. Pass it back to the caller for a
3021 * fixup.
3022 */
3023 *empty_extent_path = right_path;
3024 right_path = NULL;
3025 goto out;
3026 }
3027 if (ret) {
3028 mlog_errno(ret);
3029 goto out;
3030 }
3031
3032 /*
3033 * The subtree rotate might have removed records on
3034 * the rightmost edge. If so, then rotation is
3035 * complete.
3036 */
3037 if (deleted)
3038 break;
3039
3040 ocfs2_mv_path(left_path, right_path);
3041
Joel Beckere46f74d2009-02-12 19:47:43 -08003042 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003043 &right_cpos);
3044 if (ret) {
3045 mlog_errno(ret);
3046 goto out;
3047 }
3048 }
3049
3050out:
3051 ocfs2_free_path(right_path);
3052 ocfs2_free_path(left_path);
3053
3054 return ret;
3055}
3056
Joel Becker70f18c02009-02-13 02:09:31 -08003057static int ocfs2_remove_rightmost_path(handle_t *handle,
3058 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003059 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003060 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003061{
3062 int ret, subtree_index;
3063 u32 cpos;
3064 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003065 struct ocfs2_extent_block *eb;
3066 struct ocfs2_extent_list *el;
3067
Mark Fasheh328d5752007-06-18 10:48:04 -07003068
Joel Becker6136ca52009-02-12 19:32:43 -08003069 ret = ocfs2_et_sanity_check(et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003070 if (ret)
3071 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003072 /*
3073 * There's two ways we handle this depending on
3074 * whether path is the only existing one.
3075 */
3076 ret = ocfs2_extend_rotate_transaction(handle, 0,
3077 handle->h_buffer_credits,
3078 path);
3079 if (ret) {
3080 mlog_errno(ret);
3081 goto out;
3082 }
3083
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003084 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003085 if (ret) {
3086 mlog_errno(ret);
3087 goto out;
3088 }
3089
Joel Becker3d03a302009-02-12 17:49:26 -08003090 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3091 path, &cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003092 if (ret) {
3093 mlog_errno(ret);
3094 goto out;
3095 }
3096
3097 if (cpos) {
3098 /*
3099 * We have a path to the left of this one - it needs
3100 * an update too.
3101 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003102 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003103 if (!left_path) {
3104 ret = -ENOMEM;
3105 mlog_errno(ret);
3106 goto out;
3107 }
3108
Joel Beckerfacdb772009-02-12 18:08:48 -08003109 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003110 if (ret) {
3111 mlog_errno(ret);
3112 goto out;
3113 }
3114
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003115 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003116 if (ret) {
3117 mlog_errno(ret);
3118 goto out;
3119 }
3120
Joel Becker7dc02802009-02-12 19:20:13 -08003121 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003122
Joel Becker6641b0c2009-02-12 18:57:52 -08003123 ocfs2_unlink_subtree(handle, et, left_path, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003124 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08003125 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08003126 left_path);
3127 if (ret) {
3128 mlog_errno(ret);
3129 goto out;
3130 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003131
3132 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003133 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003134 } else {
3135 /*
3136 * 'path' is also the leftmost path which
3137 * means it must be the only one. This gets
3138 * handled differently because we want to
Joel Becker70f18c02009-02-13 02:09:31 -08003139 * revert the root back to having extents
Mark Fasheh328d5752007-06-18 10:48:04 -07003140 * in-line.
3141 */
Joel Becker6641b0c2009-02-12 18:57:52 -08003142 ocfs2_unlink_path(handle, et, dealloc, path, 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003143
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003144 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003145 el->l_tree_depth = 0;
3146 el->l_next_free_rec = 0;
3147 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3148
Joel Becker35dc0aa2008-08-20 16:25:06 -07003149 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003150 }
3151
3152 ocfs2_journal_dirty(handle, path_root_bh(path));
3153
3154out:
3155 ocfs2_free_path(left_path);
3156 return ret;
3157}
3158
3159/*
3160 * Left rotation of btree records.
3161 *
3162 * In many ways, this is (unsurprisingly) the opposite of right
3163 * rotation. We start at some non-rightmost path containing an empty
3164 * extent in the leaf block. The code works its way to the rightmost
3165 * path by rotating records to the left in every subtree.
3166 *
3167 * This is used by any code which reduces the number of extent records
3168 * in a leaf. After removal, an empty record should be placed in the
3169 * leftmost list position.
3170 *
3171 * This won't handle a length update of the rightmost path records if
3172 * the rightmost tree leaf record is removed so the caller is
3173 * responsible for detecting and correcting that.
3174 */
Joel Becker70f18c02009-02-13 02:09:31 -08003175static int ocfs2_rotate_tree_left(handle_t *handle,
3176 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003177 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003178 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003179{
3180 int ret, orig_credits = handle->h_buffer_credits;
3181 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3182 struct ocfs2_extent_block *eb;
3183 struct ocfs2_extent_list *el;
3184
3185 el = path_leaf_el(path);
3186 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3187 return 0;
3188
3189 if (path->p_tree_depth == 0) {
3190rightmost_no_delete:
3191 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003192 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003193 * it up front.
3194 */
Joel Becker70f18c02009-02-13 02:09:31 -08003195 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003196 if (ret)
3197 mlog_errno(ret);
3198 goto out;
3199 }
3200
3201 /*
3202 * Handle rightmost branch now. There's several cases:
3203 * 1) simple rotation leaving records in there. That's trivial.
3204 * 2) rotation requiring a branch delete - there's no more
3205 * records left. Two cases of this:
3206 * a) There are branches to the left.
3207 * b) This is also the leftmost (the only) branch.
3208 *
3209 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3210 * 2a) we need the left branch so that we can update it with the unlink
Joel Becker70f18c02009-02-13 02:09:31 -08003211 * 2b) we need to bring the root back to inline extents.
Mark Fasheh328d5752007-06-18 10:48:04 -07003212 */
3213
3214 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3215 el = &eb->h_list;
3216 if (eb->h_next_leaf_blk == 0) {
3217 /*
3218 * This gets a bit tricky if we're going to delete the
3219 * rightmost path. Get the other cases out of the way
3220 * 1st.
3221 */
3222 if (le16_to_cpu(el->l_next_free_rec) > 1)
3223 goto rightmost_no_delete;
3224
3225 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3226 ret = -EIO;
Joel Becker70f18c02009-02-13 02:09:31 -08003227 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3228 "Owner %llu has empty extent block at %llu",
3229 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07003230 (unsigned long long)le64_to_cpu(eb->h_blkno));
3231 goto out;
3232 }
3233
3234 /*
3235 * XXX: The caller can not trust "path" any more after
3236 * this as it will have been deleted. What do we do?
3237 *
3238 * In theory the rotate-for-merge code will never get
3239 * here because it'll always ask for a rotate in a
3240 * nonempty list.
3241 */
3242
Joel Becker70f18c02009-02-13 02:09:31 -08003243 ret = ocfs2_remove_rightmost_path(handle, et, path,
3244 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003245 if (ret)
3246 mlog_errno(ret);
3247 goto out;
3248 }
3249
3250 /*
3251 * Now we can loop, remembering the path we get from -EAGAIN
3252 * and restarting from there.
3253 */
3254try_rotate:
Joel Beckere46f74d2009-02-12 19:47:43 -08003255 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3256 dealloc, &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003257 if (ret && ret != -EAGAIN) {
3258 mlog_errno(ret);
3259 goto out;
3260 }
3261
3262 while (ret == -EAGAIN) {
3263 tmp_path = restart_path;
3264 restart_path = NULL;
3265
Joel Beckere46f74d2009-02-12 19:47:43 -08003266 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07003267 tmp_path, dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08003268 &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003269 if (ret && ret != -EAGAIN) {
3270 mlog_errno(ret);
3271 goto out;
3272 }
3273
3274 ocfs2_free_path(tmp_path);
3275 tmp_path = NULL;
3276
3277 if (ret == 0)
3278 goto try_rotate;
3279 }
3280
3281out:
3282 ocfs2_free_path(tmp_path);
3283 ocfs2_free_path(restart_path);
3284 return ret;
3285}
3286
3287static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3288 int index)
3289{
3290 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3291 unsigned int size;
3292
3293 if (rec->e_leaf_clusters == 0) {
3294 /*
3295 * We consumed all of the merged-from record. An empty
3296 * extent cannot exist anywhere but the 1st array
3297 * position, so move things over if the merged-from
3298 * record doesn't occupy that position.
3299 *
3300 * This creates a new empty extent so the caller
3301 * should be smart enough to have removed any existing
3302 * ones.
3303 */
3304 if (index > 0) {
3305 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3306 size = index * sizeof(struct ocfs2_extent_rec);
3307 memmove(&el->l_recs[1], &el->l_recs[0], size);
3308 }
3309
3310 /*
3311 * Always memset - the caller doesn't check whether it
3312 * created an empty extent, so there could be junk in
3313 * the other fields.
3314 */
3315 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3316 }
3317}
3318
Joel Becker4fe82c32009-02-13 02:16:08 -08003319static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003320 struct ocfs2_path *left_path,
3321 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003322{
3323 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003324 u32 right_cpos;
3325 struct ocfs2_path *right_path = NULL;
3326 struct ocfs2_extent_list *left_el;
3327
3328 *ret_right_path = NULL;
3329
3330 /* This function shouldn't be called for non-trees. */
3331 BUG_ON(left_path->p_tree_depth == 0);
3332
3333 left_el = path_leaf_el(left_path);
3334 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3335
Joel Becker4fe82c32009-02-13 02:16:08 -08003336 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3337 left_path, &right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003338 if (ret) {
3339 mlog_errno(ret);
3340 goto out;
3341 }
3342
3343 /* This function shouldn't be called for the rightmost leaf. */
3344 BUG_ON(right_cpos == 0);
3345
Joel Beckerffdd7a52008-10-17 22:32:01 -07003346 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003347 if (!right_path) {
3348 ret = -ENOMEM;
3349 mlog_errno(ret);
3350 goto out;
3351 }
3352
Joel Becker4fe82c32009-02-13 02:16:08 -08003353 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003354 if (ret) {
3355 mlog_errno(ret);
3356 goto out;
3357 }
3358
3359 *ret_right_path = right_path;
3360out:
3361 if (ret)
3362 ocfs2_free_path(right_path);
3363 return ret;
3364}
3365
3366/*
3367 * Remove split_rec clusters from the record at index and merge them
3368 * onto the beginning of the record "next" to it.
3369 * For index < l_count - 1, the next means the extent rec at index + 1.
3370 * For index == l_count - 1, the "next" means the 1st extent rec of the
3371 * next extent block.
3372 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003373static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003374 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003375 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003376 struct ocfs2_extent_rec *split_rec,
3377 int index)
3378{
3379 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003380 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3381 struct ocfs2_extent_rec *left_rec;
3382 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003383 struct ocfs2_extent_list *right_el;
3384 struct ocfs2_path *right_path = NULL;
3385 int subtree_index = 0;
3386 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3387 struct buffer_head *bh = path_leaf_bh(left_path);
3388 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003389
3390 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003391 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003392
Al Viro9d8df6a2008-05-21 06:32:11 +01003393 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003394 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3395 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003396 ret = ocfs2_get_right_path(et, left_path, &right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003397 if (ret) {
3398 mlog_errno(ret);
3399 goto out;
3400 }
3401
3402 right_el = path_leaf_el(right_path);
3403 next_free = le16_to_cpu(right_el->l_next_free_rec);
3404 BUG_ON(next_free <= 0);
3405 right_rec = &right_el->l_recs[0];
3406 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003407 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003408 right_rec = &right_el->l_recs[1];
3409 }
3410
3411 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3412 le16_to_cpu(left_rec->e_leaf_clusters) !=
3413 le32_to_cpu(right_rec->e_cpos));
3414
Joel Becker7dc02802009-02-12 19:20:13 -08003415 subtree_index = ocfs2_find_subtree_root(et, left_path,
3416 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003417
3418 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3419 handle->h_buffer_credits,
3420 right_path);
3421 if (ret) {
3422 mlog_errno(ret);
3423 goto out;
3424 }
3425
3426 root_bh = left_path->p_node[subtree_index].bh;
3427 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3428
Joel Becker7dc02802009-02-12 19:20:13 -08003429 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003430 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003431 if (ret) {
3432 mlog_errno(ret);
3433 goto out;
3434 }
3435
3436 for (i = subtree_index + 1;
3437 i < path_num_items(right_path); i++) {
Joel Becker7dc02802009-02-12 19:20:13 -08003438 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003439 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003440 if (ret) {
3441 mlog_errno(ret);
3442 goto out;
3443 }
3444
Joel Becker7dc02802009-02-12 19:20:13 -08003445 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003446 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003447 if (ret) {
3448 mlog_errno(ret);
3449 goto out;
3450 }
3451 }
3452
3453 } else {
3454 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3455 right_rec = &el->l_recs[index + 1];
3456 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003457
Joel Becker7dc02802009-02-12 19:20:13 -08003458 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
Joel Becker13723d02008-10-17 19:25:01 -07003459 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003460 if (ret) {
3461 mlog_errno(ret);
3462 goto out;
3463 }
3464
3465 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3466
3467 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3468 le64_add_cpu(&right_rec->e_blkno,
Joel Becker7dc02802009-02-12 19:20:13 -08003469 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3470 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003471 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3472
3473 ocfs2_cleanup_merge(el, index);
3474
3475 ret = ocfs2_journal_dirty(handle, bh);
3476 if (ret)
3477 mlog_errno(ret);
3478
Tao Ma677b9752008-01-30 14:21:05 +08003479 if (right_path) {
3480 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3481 if (ret)
3482 mlog_errno(ret);
3483
Joel Becker4619c732009-02-12 19:02:36 -08003484 ocfs2_complete_edge_insert(handle, left_path, right_path,
3485 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003486 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003487out:
Tao Ma677b9752008-01-30 14:21:05 +08003488 if (right_path)
3489 ocfs2_free_path(right_path);
3490 return ret;
3491}
3492
Joel Becker4fe82c32009-02-13 02:16:08 -08003493static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003494 struct ocfs2_path *right_path,
3495 struct ocfs2_path **ret_left_path)
3496{
3497 int ret;
3498 u32 left_cpos;
3499 struct ocfs2_path *left_path = NULL;
3500
3501 *ret_left_path = NULL;
3502
3503 /* This function shouldn't be called for non-trees. */
3504 BUG_ON(right_path->p_tree_depth == 0);
3505
Joel Becker4fe82c32009-02-13 02:16:08 -08003506 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
Tao Ma677b9752008-01-30 14:21:05 +08003507 right_path, &left_cpos);
3508 if (ret) {
3509 mlog_errno(ret);
3510 goto out;
3511 }
3512
3513 /* This function shouldn't be called for the leftmost leaf. */
3514 BUG_ON(left_cpos == 0);
3515
Joel Beckerffdd7a52008-10-17 22:32:01 -07003516 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003517 if (!left_path) {
3518 ret = -ENOMEM;
3519 mlog_errno(ret);
3520 goto out;
3521 }
3522
Joel Becker4fe82c32009-02-13 02:16:08 -08003523 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003524 if (ret) {
3525 mlog_errno(ret);
3526 goto out;
3527 }
3528
3529 *ret_left_path = left_path;
3530out:
3531 if (ret)
3532 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003533 return ret;
3534}
3535
3536/*
3537 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003538 * onto the tail of the record "before" it.
3539 * For index > 0, the "before" means the extent rec at index - 1.
3540 *
3541 * For index == 0, the "before" means the last record of the previous
3542 * extent block. And there is also a situation that we may need to
3543 * remove the rightmost leaf extent block in the right_path and change
3544 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003545 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003546static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003547 handle_t *handle,
Joel Becker4fe82c32009-02-13 02:16:08 -08003548 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003549 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003550 struct ocfs2_cached_dealloc_ctxt *dealloc,
3551 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003552{
Tao Ma677b9752008-01-30 14:21:05 +08003553 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003554 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3555 struct ocfs2_extent_rec *left_rec;
3556 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003557 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3558 struct buffer_head *bh = path_leaf_bh(right_path);
3559 struct buffer_head *root_bh = NULL;
3560 struct ocfs2_path *left_path = NULL;
3561 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003562
Tao Ma677b9752008-01-30 14:21:05 +08003563 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003564
Mark Fasheh328d5752007-06-18 10:48:04 -07003565 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003566 if (index == 0) {
3567 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003568 ret = ocfs2_get_left_path(et, right_path, &left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003569 if (ret) {
3570 mlog_errno(ret);
3571 goto out;
3572 }
3573
3574 left_el = path_leaf_el(left_path);
3575 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3576 le16_to_cpu(left_el->l_count));
3577
3578 left_rec = &left_el->l_recs[
3579 le16_to_cpu(left_el->l_next_free_rec) - 1];
3580 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3581 le16_to_cpu(left_rec->e_leaf_clusters) !=
3582 le32_to_cpu(split_rec->e_cpos));
3583
Joel Becker7dc02802009-02-12 19:20:13 -08003584 subtree_index = ocfs2_find_subtree_root(et, left_path,
3585 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003586
3587 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3588 handle->h_buffer_credits,
3589 left_path);
3590 if (ret) {
3591 mlog_errno(ret);
3592 goto out;
3593 }
3594
3595 root_bh = left_path->p_node[subtree_index].bh;
3596 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3597
Joel Becker4fe82c32009-02-13 02:16:08 -08003598 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003599 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003600 if (ret) {
3601 mlog_errno(ret);
3602 goto out;
3603 }
3604
3605 for (i = subtree_index + 1;
3606 i < path_num_items(right_path); i++) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003607 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003608 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003609 if (ret) {
3610 mlog_errno(ret);
3611 goto out;
3612 }
3613
Joel Becker4fe82c32009-02-13 02:16:08 -08003614 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003615 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003616 if (ret) {
3617 mlog_errno(ret);
3618 goto out;
3619 }
3620 }
3621 } else {
3622 left_rec = &el->l_recs[index - 1];
3623 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3624 has_empty_extent = 1;
3625 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003626
Joel Becker4fe82c32009-02-13 02:16:08 -08003627 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Tao Ma9047bea2009-01-05 14:45:24 +08003628 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003629 if (ret) {
3630 mlog_errno(ret);
3631 goto out;
3632 }
3633
3634 if (has_empty_extent && index == 1) {
3635 /*
3636 * The easy case - we can just plop the record right in.
3637 */
3638 *left_rec = *split_rec;
3639
3640 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003641 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003642 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003643
3644 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3645 le64_add_cpu(&right_rec->e_blkno,
Joel Becker4fe82c32009-02-13 02:16:08 -08003646 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3647 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003648 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3649
3650 ocfs2_cleanup_merge(el, index);
3651
3652 ret = ocfs2_journal_dirty(handle, bh);
3653 if (ret)
3654 mlog_errno(ret);
3655
Tao Ma677b9752008-01-30 14:21:05 +08003656 if (left_path) {
3657 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3658 if (ret)
3659 mlog_errno(ret);
3660
3661 /*
3662 * In the situation that the right_rec is empty and the extent
3663 * block is empty also, ocfs2_complete_edge_insert can't handle
3664 * it and we need to delete the right extent block.
3665 */
3666 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3667 le16_to_cpu(el->l_next_free_rec) == 1) {
3668
Joel Becker70f18c02009-02-13 02:09:31 -08003669 ret = ocfs2_remove_rightmost_path(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003670 right_path,
Joel Becker70f18c02009-02-13 02:09:31 -08003671 dealloc);
Tao Ma677b9752008-01-30 14:21:05 +08003672 if (ret) {
3673 mlog_errno(ret);
3674 goto out;
3675 }
3676
3677 /* Now the rightmost extent block has been deleted.
3678 * So we use the new rightmost path.
3679 */
3680 ocfs2_mv_path(right_path, left_path);
3681 left_path = NULL;
3682 } else
Joel Becker4619c732009-02-12 19:02:36 -08003683 ocfs2_complete_edge_insert(handle, left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003684 right_path, subtree_index);
3685 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003686out:
Tao Ma677b9752008-01-30 14:21:05 +08003687 if (left_path)
3688 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003689 return ret;
3690}
3691
Joel Beckerc495dd22009-02-13 02:19:11 -08003692static int ocfs2_try_to_merge_extent(handle_t *handle,
3693 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003694 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003695 int split_index,
3696 struct ocfs2_extent_rec *split_rec,
3697 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckerc495dd22009-02-13 02:19:11 -08003698 struct ocfs2_merge_ctxt *ctxt)
Mark Fasheh328d5752007-06-18 10:48:04 -07003699{
Tao Mao518d7262007-08-28 17:25:35 -07003700 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003701 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003702 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3703
3704 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3705
Tao Mao518d7262007-08-28 17:25:35 -07003706 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3707 /*
3708 * The merge code will need to create an empty
3709 * extent to take the place of the newly
3710 * emptied slot. Remove any pre-existing empty
3711 * extents - having more than one in a leaf is
3712 * illegal.
3713 */
Joel Becker70f18c02009-02-13 02:09:31 -08003714 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Tao Mao518d7262007-08-28 17:25:35 -07003715 if (ret) {
3716 mlog_errno(ret);
3717 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003718 }
Tao Mao518d7262007-08-28 17:25:35 -07003719 split_index--;
3720 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003721 }
3722
3723 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3724 /*
3725 * Left-right contig implies this.
3726 */
3727 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003728
3729 /*
3730 * Since the leftright insert always covers the entire
3731 * extent, this call will delete the insert record
3732 * entirely, resulting in an empty extent record added to
3733 * the extent block.
3734 *
3735 * Since the adding of an empty extent shifts
3736 * everything back to the right, there's no need to
3737 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003738 *
3739 * When the split_index is zero, we need to merge it to the
3740 * prevoius extent block. It is more efficient and easier
3741 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003742 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003743 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003744 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003745 if (ret) {
3746 mlog_errno(ret);
3747 goto out;
3748 }
3749
3750 /*
3751 * We can only get this from logic error above.
3752 */
3753 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3754
Tao Ma677b9752008-01-30 14:21:05 +08003755 /* The merge left us with an empty extent, remove it. */
Joel Becker70f18c02009-02-13 02:09:31 -08003756 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003757 if (ret) {
3758 mlog_errno(ret);
3759 goto out;
3760 }
Tao Ma677b9752008-01-30 14:21:05 +08003761
Mark Fasheh328d5752007-06-18 10:48:04 -07003762 rec = &el->l_recs[split_index];
3763
3764 /*
3765 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003766 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003767 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003768 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3769 dealloc, split_index);
Tao Ma677b9752008-01-30 14:21:05 +08003770
Mark Fasheh328d5752007-06-18 10:48:04 -07003771 if (ret) {
3772 mlog_errno(ret);
3773 goto out;
3774 }
3775
Joel Becker70f18c02009-02-13 02:09:31 -08003776 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003777 /*
3778 * Error from this last rotate is not critical, so
3779 * print but don't bubble it up.
3780 */
3781 if (ret)
3782 mlog_errno(ret);
3783 ret = 0;
3784 } else {
3785 /*
3786 * Merge a record to the left or right.
3787 *
3788 * 'contig_type' is relative to the existing record,
3789 * so for example, if we're "right contig", it's to
3790 * the record on the left (hence the left merge).
3791 */
3792 if (ctxt->c_contig_type == CONTIG_RIGHT) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003793 ret = ocfs2_merge_rec_left(path, handle, et,
3794 split_rec, dealloc,
Mark Fasheh328d5752007-06-18 10:48:04 -07003795 split_index);
3796 if (ret) {
3797 mlog_errno(ret);
3798 goto out;
3799 }
3800 } else {
Joel Becker4fe82c32009-02-13 02:16:08 -08003801 ret = ocfs2_merge_rec_right(path, handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003802 et, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003803 split_index);
3804 if (ret) {
3805 mlog_errno(ret);
3806 goto out;
3807 }
3808 }
3809
3810 if (ctxt->c_split_covers_rec) {
3811 /*
3812 * The merge may have left an empty extent in
3813 * our leaf. Try to rotate it away.
3814 */
Joel Becker70f18c02009-02-13 02:09:31 -08003815 ret = ocfs2_rotate_tree_left(handle, et, path,
3816 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003817 if (ret)
3818 mlog_errno(ret);
3819 ret = 0;
3820 }
3821 }
3822
3823out:
3824 return ret;
3825}
3826
3827static void ocfs2_subtract_from_rec(struct super_block *sb,
3828 enum ocfs2_split_type split,
3829 struct ocfs2_extent_rec *rec,
3830 struct ocfs2_extent_rec *split_rec)
3831{
3832 u64 len_blocks;
3833
3834 len_blocks = ocfs2_clusters_to_blocks(sb,
3835 le16_to_cpu(split_rec->e_leaf_clusters));
3836
3837 if (split == SPLIT_LEFT) {
3838 /*
3839 * Region is on the left edge of the existing
3840 * record.
3841 */
3842 le32_add_cpu(&rec->e_cpos,
3843 le16_to_cpu(split_rec->e_leaf_clusters));
3844 le64_add_cpu(&rec->e_blkno, len_blocks);
3845 le16_add_cpu(&rec->e_leaf_clusters,
3846 -le16_to_cpu(split_rec->e_leaf_clusters));
3847 } else {
3848 /*
3849 * Region is on the right edge of the existing
3850 * record.
3851 */
3852 le16_add_cpu(&rec->e_leaf_clusters,
3853 -le16_to_cpu(split_rec->e_leaf_clusters));
3854 }
3855}
3856
Mark Fashehdcd05382007-01-16 11:32:23 -08003857/*
3858 * Do the final bits of extent record insertion at the target leaf
3859 * list. If this leaf is part of an allocation tree, it is assumed
3860 * that the tree above has been prepared.
3861 */
Joel Beckerd5628622009-02-13 02:54:36 -08003862static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3863 struct ocfs2_extent_rec *insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08003864 struct ocfs2_extent_list *el,
Joel Beckerd5628622009-02-13 02:54:36 -08003865 struct ocfs2_insert_type *insert)
Mark Fashehdcd05382007-01-16 11:32:23 -08003866{
3867 int i = insert->ins_contig_index;
3868 unsigned int range;
3869 struct ocfs2_extent_rec *rec;
3870
Mark Fashehe48edee2007-03-07 16:46:57 -08003871 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003872
Mark Fasheh328d5752007-06-18 10:48:04 -07003873 if (insert->ins_split != SPLIT_NONE) {
3874 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3875 BUG_ON(i == -1);
3876 rec = &el->l_recs[i];
Joel Beckerd5628622009-02-13 02:54:36 -08003877 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3878 insert->ins_split, rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003879 insert_rec);
3880 goto rotate;
3881 }
3882
Mark Fashehdcd05382007-01-16 11:32:23 -08003883 /*
3884 * Contiguous insert - either left or right.
3885 */
3886 if (insert->ins_contig != CONTIG_NONE) {
3887 rec = &el->l_recs[i];
3888 if (insert->ins_contig == CONTIG_LEFT) {
3889 rec->e_blkno = insert_rec->e_blkno;
3890 rec->e_cpos = insert_rec->e_cpos;
3891 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003892 le16_add_cpu(&rec->e_leaf_clusters,
3893 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003894 return;
3895 }
3896
3897 /*
3898 * Handle insert into an empty leaf.
3899 */
3900 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3901 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3902 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3903 el->l_recs[0] = *insert_rec;
3904 el->l_next_free_rec = cpu_to_le16(1);
3905 return;
3906 }
3907
3908 /*
3909 * Appending insert.
3910 */
3911 if (insert->ins_appending == APPEND_TAIL) {
3912 i = le16_to_cpu(el->l_next_free_rec) - 1;
3913 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003914 range = le32_to_cpu(rec->e_cpos)
3915 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003916 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3917
3918 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3919 le16_to_cpu(el->l_count),
Joel Beckerd5628622009-02-13 02:54:36 -08003920 "owner %llu, depth %u, count %u, next free %u, "
Mark Fashehdcd05382007-01-16 11:32:23 -08003921 "rec.cpos %u, rec.clusters %u, "
3922 "insert.cpos %u, insert.clusters %u\n",
Joel Beckerd5628622009-02-13 02:54:36 -08003923 ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08003924 le16_to_cpu(el->l_tree_depth),
3925 le16_to_cpu(el->l_count),
3926 le16_to_cpu(el->l_next_free_rec),
3927 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003928 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003929 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003930 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003931 i++;
3932 el->l_recs[i] = *insert_rec;
3933 le16_add_cpu(&el->l_next_free_rec, 1);
3934 return;
3935 }
3936
Mark Fasheh328d5752007-06-18 10:48:04 -07003937rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003938 /*
3939 * Ok, we have to rotate.
3940 *
3941 * At this point, it is safe to assume that inserting into an
3942 * empty leaf and appending to a leaf have both been handled
3943 * above.
3944 *
3945 * This leaf needs to have space, either by the empty 1st
3946 * extent record, or by virtue of an l_next_rec < l_count.
3947 */
3948 ocfs2_rotate_leaf(el, insert_rec);
3949}
3950
Joel Beckerd401dc12009-02-13 02:24:10 -08003951static void ocfs2_adjust_rightmost_records(handle_t *handle,
3952 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003953 struct ocfs2_path *path,
3954 struct ocfs2_extent_rec *insert_rec)
3955{
3956 int ret, i, next_free;
3957 struct buffer_head *bh;
3958 struct ocfs2_extent_list *el;
3959 struct ocfs2_extent_rec *rec;
3960
3961 /*
3962 * Update everything except the leaf block.
3963 */
3964 for (i = 0; i < path->p_tree_depth; i++) {
3965 bh = path->p_node[i].bh;
3966 el = path->p_node[i].el;
3967
3968 next_free = le16_to_cpu(el->l_next_free_rec);
3969 if (next_free == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08003970 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3971 "Owner %llu has a bad extent list",
3972 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fasheh328d5752007-06-18 10:48:04 -07003973 ret = -EIO;
3974 return;
3975 }
3976
3977 rec = &el->l_recs[next_free - 1];
3978
3979 rec->e_int_clusters = insert_rec->e_cpos;
3980 le32_add_cpu(&rec->e_int_clusters,
3981 le16_to_cpu(insert_rec->e_leaf_clusters));
3982 le32_add_cpu(&rec->e_int_clusters,
3983 -le32_to_cpu(rec->e_cpos));
3984
3985 ret = ocfs2_journal_dirty(handle, bh);
3986 if (ret)
3987 mlog_errno(ret);
3988
3989 }
3990}
3991
Joel Beckerd401dc12009-02-13 02:24:10 -08003992static int ocfs2_append_rec_to_path(handle_t *handle,
3993 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08003994 struct ocfs2_extent_rec *insert_rec,
3995 struct ocfs2_path *right_path,
3996 struct ocfs2_path **ret_left_path)
3997{
Mark Fasheh328d5752007-06-18 10:48:04 -07003998 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003999 struct ocfs2_extent_list *el;
4000 struct ocfs2_path *left_path = NULL;
4001
4002 *ret_left_path = NULL;
4003
4004 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08004005 * This shouldn't happen for non-trees. The extent rec cluster
4006 * count manipulation below only works for interior nodes.
4007 */
4008 BUG_ON(right_path->p_tree_depth == 0);
4009
4010 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08004011 * If our appending insert is at the leftmost edge of a leaf,
4012 * then we might need to update the rightmost records of the
4013 * neighboring path.
4014 */
4015 el = path_leaf_el(right_path);
4016 next_free = le16_to_cpu(el->l_next_free_rec);
4017 if (next_free == 0 ||
4018 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
4019 u32 left_cpos;
4020
Joel Beckerd401dc12009-02-13 02:24:10 -08004021 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
4022 right_path, &left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004023 if (ret) {
4024 mlog_errno(ret);
4025 goto out;
4026 }
4027
4028 mlog(0, "Append may need a left path update. cpos: %u, "
4029 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
4030 left_cpos);
4031
4032 /*
4033 * No need to worry if the append is already in the
4034 * leftmost leaf.
4035 */
4036 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004037 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004038 if (!left_path) {
4039 ret = -ENOMEM;
4040 mlog_errno(ret);
4041 goto out;
4042 }
4043
Joel Beckerd401dc12009-02-13 02:24:10 -08004044 ret = ocfs2_find_path(et->et_ci, left_path,
Joel Beckerfacdb772009-02-12 18:08:48 -08004045 left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004046 if (ret) {
4047 mlog_errno(ret);
4048 goto out;
4049 }
4050
4051 /*
4052 * ocfs2_insert_path() will pass the left_path to the
4053 * journal for us.
4054 */
4055 }
4056 }
4057
Joel Beckerd401dc12009-02-13 02:24:10 -08004058 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004059 if (ret) {
4060 mlog_errno(ret);
4061 goto out;
4062 }
4063
Joel Beckerd401dc12009-02-13 02:24:10 -08004064 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004065
4066 *ret_left_path = left_path;
4067 ret = 0;
4068out:
4069 if (ret != 0)
4070 ocfs2_free_path(left_path);
4071
4072 return ret;
4073}
4074
Joel Beckerc38e52b2009-02-13 02:56:23 -08004075static void ocfs2_split_record(struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004076 struct ocfs2_path *left_path,
4077 struct ocfs2_path *right_path,
4078 struct ocfs2_extent_rec *split_rec,
4079 enum ocfs2_split_type split)
4080{
4081 int index;
4082 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4083 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4084 struct ocfs2_extent_rec *rec, *tmprec;
4085
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004086 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004087 if (left_path)
4088 left_el = path_leaf_el(left_path);
4089
4090 el = right_el;
4091 insert_el = right_el;
4092 index = ocfs2_search_extent_list(el, cpos);
4093 if (index != -1) {
4094 if (index == 0 && left_path) {
4095 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4096
4097 /*
4098 * This typically means that the record
4099 * started in the left path but moved to the
4100 * right as a result of rotation. We either
4101 * move the existing record to the left, or we
4102 * do the later insert there.
4103 *
4104 * In this case, the left path should always
4105 * exist as the rotate code will have passed
4106 * it back for a post-insert update.
4107 */
4108
4109 if (split == SPLIT_LEFT) {
4110 /*
4111 * It's a left split. Since we know
4112 * that the rotate code gave us an
4113 * empty extent in the left path, we
4114 * can just do the insert there.
4115 */
4116 insert_el = left_el;
4117 } else {
4118 /*
4119 * Right split - we have to move the
4120 * existing record over to the left
4121 * leaf. The insert will be into the
4122 * newly created empty extent in the
4123 * right leaf.
4124 */
4125 tmprec = &right_el->l_recs[index];
4126 ocfs2_rotate_leaf(left_el, tmprec);
4127 el = left_el;
4128
4129 memset(tmprec, 0, sizeof(*tmprec));
4130 index = ocfs2_search_extent_list(left_el, cpos);
4131 BUG_ON(index == -1);
4132 }
4133 }
4134 } else {
4135 BUG_ON(!left_path);
4136 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4137 /*
4138 * Left path is easy - we can just allow the insert to
4139 * happen.
4140 */
4141 el = left_el;
4142 insert_el = left_el;
4143 index = ocfs2_search_extent_list(el, cpos);
4144 BUG_ON(index == -1);
4145 }
4146
4147 rec = &el->l_recs[index];
Joel Beckerc38e52b2009-02-13 02:56:23 -08004148 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4149 split, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004150 ocfs2_rotate_leaf(insert_el, split_rec);
4151}
4152
Mark Fashehdcd05382007-01-16 11:32:23 -08004153/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004154 * This function only does inserts on an allocation b-tree. For tree
4155 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004156 *
4157 * right_path is the path we want to do the actual insert
4158 * in. left_path should only be passed in if we need to update that
4159 * portion of the tree after an edge insert.
4160 */
Joel Becker3505bec2009-02-13 02:57:58 -08004161static int ocfs2_insert_path(handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08004162 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004163 struct ocfs2_path *left_path,
4164 struct ocfs2_path *right_path,
4165 struct ocfs2_extent_rec *insert_rec,
4166 struct ocfs2_insert_type *insert)
4167{
4168 int ret, subtree_index;
4169 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004170
Mark Fashehdcd05382007-01-16 11:32:23 -08004171 if (left_path) {
4172 int credits = handle->h_buffer_credits;
4173
4174 /*
4175 * There's a chance that left_path got passed back to
4176 * us without being accounted for in the
4177 * journal. Extend our transaction here to be sure we
4178 * can change those blocks.
4179 */
4180 credits += left_path->p_tree_depth;
4181
4182 ret = ocfs2_extend_trans(handle, credits);
4183 if (ret < 0) {
4184 mlog_errno(ret);
4185 goto out;
4186 }
4187
Joel Becker7dc02802009-02-12 19:20:13 -08004188 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004189 if (ret < 0) {
4190 mlog_errno(ret);
4191 goto out;
4192 }
4193 }
4194
Mark Fashehe8aed342007-12-03 16:43:01 -08004195 /*
4196 * Pass both paths to the journal. The majority of inserts
4197 * will be touching all components anyway.
4198 */
Joel Becker7dc02802009-02-12 19:20:13 -08004199 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004200 if (ret < 0) {
4201 mlog_errno(ret);
4202 goto out;
4203 }
4204
Mark Fasheh328d5752007-06-18 10:48:04 -07004205 if (insert->ins_split != SPLIT_NONE) {
4206 /*
4207 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004208 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004209 * function sort it all out.
4210 */
Joel Beckerc38e52b2009-02-13 02:56:23 -08004211 ocfs2_split_record(et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004212 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004213
4214 /*
4215 * Split might have modified either leaf and we don't
4216 * have a guarantee that the later edge insert will
4217 * dirty this for us.
4218 */
4219 if (left_path)
4220 ret = ocfs2_journal_dirty(handle,
4221 path_leaf_bh(left_path));
4222 if (ret)
4223 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07004224 } else
Joel Beckerd5628622009-02-13 02:54:36 -08004225 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4226 insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004227
Mark Fashehdcd05382007-01-16 11:32:23 -08004228 ret = ocfs2_journal_dirty(handle, leaf_bh);
4229 if (ret)
4230 mlog_errno(ret);
4231
4232 if (left_path) {
4233 /*
4234 * The rotate code has indicated that we need to fix
4235 * up portions of the tree after the insert.
4236 *
4237 * XXX: Should we extend the transaction here?
4238 */
Joel Becker7dc02802009-02-12 19:20:13 -08004239 subtree_index = ocfs2_find_subtree_root(et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004240 right_path);
Joel Becker4619c732009-02-12 19:02:36 -08004241 ocfs2_complete_edge_insert(handle, left_path, right_path,
4242 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08004243 }
4244
4245 ret = 0;
4246out:
4247 return ret;
4248}
4249
Joel Becker3505bec2009-02-13 02:57:58 -08004250static int ocfs2_do_insert_extent(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004251 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004252 struct ocfs2_extent_rec *insert_rec,
4253 struct ocfs2_insert_type *type)
4254{
4255 int ret, rotate = 0;
4256 u32 cpos;
4257 struct ocfs2_path *right_path = NULL;
4258 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004259 struct ocfs2_extent_list *el;
4260
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004261 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004262
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004263 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004264 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004265 if (ret) {
4266 mlog_errno(ret);
4267 goto out;
4268 }
4269
4270 if (le16_to_cpu(el->l_tree_depth) == 0) {
Joel Beckerd5628622009-02-13 02:54:36 -08004271 ocfs2_insert_at_leaf(et, insert_rec, el, type);
Mark Fashehdcd05382007-01-16 11:32:23 -08004272 goto out_update_clusters;
4273 }
4274
Joel Beckerffdd7a52008-10-17 22:32:01 -07004275 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004276 if (!right_path) {
4277 ret = -ENOMEM;
4278 mlog_errno(ret);
4279 goto out;
4280 }
4281
4282 /*
4283 * Determine the path to start with. Rotations need the
4284 * rightmost path, everything else can go directly to the
4285 * target leaf.
4286 */
4287 cpos = le32_to_cpu(insert_rec->e_cpos);
4288 if (type->ins_appending == APPEND_NONE &&
4289 type->ins_contig == CONTIG_NONE) {
4290 rotate = 1;
4291 cpos = UINT_MAX;
4292 }
4293
Joel Beckerfacdb772009-02-12 18:08:48 -08004294 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004295 if (ret) {
4296 mlog_errno(ret);
4297 goto out;
4298 }
4299
4300 /*
4301 * Rotations and appends need special treatment - they modify
4302 * parts of the tree's above them.
4303 *
4304 * Both might pass back a path immediate to the left of the
4305 * one being inserted to. This will be cause
4306 * ocfs2_insert_path() to modify the rightmost records of
4307 * left_path to account for an edge insert.
4308 *
4309 * XXX: When modifying this code, keep in mind that an insert
4310 * can wind up skipping both of these two special cases...
4311 */
4312 if (rotate) {
Joel Becker1bbf0b82009-02-12 19:42:08 -08004313 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004314 le32_to_cpu(insert_rec->e_cpos),
4315 right_path, &left_path);
4316 if (ret) {
4317 mlog_errno(ret);
4318 goto out;
4319 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004320
4321 /*
4322 * ocfs2_rotate_tree_right() might have extended the
4323 * transaction without re-journaling our tree root.
4324 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004325 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004326 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004327 if (ret) {
4328 mlog_errno(ret);
4329 goto out;
4330 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004331 } else if (type->ins_appending == APPEND_TAIL
4332 && type->ins_contig != CONTIG_LEFT) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004333 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08004334 right_path, &left_path);
4335 if (ret) {
4336 mlog_errno(ret);
4337 goto out;
4338 }
4339 }
4340
Joel Becker3505bec2009-02-13 02:57:58 -08004341 ret = ocfs2_insert_path(handle, et, left_path, right_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004342 insert_rec, type);
4343 if (ret) {
4344 mlog_errno(ret);
4345 goto out;
4346 }
4347
4348out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004349 if (type->ins_split == SPLIT_NONE)
Joel Becker6136ca52009-02-12 19:32:43 -08004350 ocfs2_et_update_clusters(et,
Joel Becker35dc0aa2008-08-20 16:25:06 -07004351 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004352
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004353 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004354 if (ret)
4355 mlog_errno(ret);
4356
4357out:
4358 ocfs2_free_path(left_path);
4359 ocfs2_free_path(right_path);
4360
4361 return ret;
4362}
4363
Mark Fasheh328d5752007-06-18 10:48:04 -07004364static enum ocfs2_contig_type
Joel Beckera2970292009-02-13 03:09:54 -08004365ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4366 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004367 struct ocfs2_extent_list *el, int index,
4368 struct ocfs2_extent_rec *split_rec)
4369{
Tao Maad5a4d72008-01-30 14:21:32 +08004370 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004371 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004372 u32 left_cpos, right_cpos;
4373 struct ocfs2_extent_rec *rec = NULL;
4374 struct ocfs2_extent_list *new_el;
4375 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4376 struct buffer_head *bh;
4377 struct ocfs2_extent_block *eb;
Joel Beckera2970292009-02-13 03:09:54 -08004378 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Tao Maad5a4d72008-01-30 14:21:32 +08004379
4380 if (index > 0) {
4381 rec = &el->l_recs[index - 1];
4382 } else if (path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004383 status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004384 if (status)
4385 goto out;
4386
4387 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004388 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004389 if (!left_path)
4390 goto out;
4391
Joel Beckera2970292009-02-13 03:09:54 -08004392 status = ocfs2_find_path(et->et_ci, left_path,
4393 left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004394 if (status)
4395 goto out;
4396
4397 new_el = path_leaf_el(left_path);
4398
4399 if (le16_to_cpu(new_el->l_next_free_rec) !=
4400 le16_to_cpu(new_el->l_count)) {
4401 bh = path_leaf_bh(left_path);
4402 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004403 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004404 "Extent block #%llu has an "
4405 "invalid l_next_free_rec of "
4406 "%d. It should have "
4407 "matched the l_count of %d",
4408 (unsigned long long)le64_to_cpu(eb->h_blkno),
4409 le16_to_cpu(new_el->l_next_free_rec),
4410 le16_to_cpu(new_el->l_count));
4411 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004412 goto out;
4413 }
4414 rec = &new_el->l_recs[
4415 le16_to_cpu(new_el->l_next_free_rec) - 1];
4416 }
4417 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004418
4419 /*
4420 * We're careful to check for an empty extent record here -
4421 * the merge code will know what to do if it sees one.
4422 */
Tao Maad5a4d72008-01-30 14:21:32 +08004423 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004424 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4425 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4426 ret = CONTIG_RIGHT;
4427 } else {
Tao Ma853a3a12009-08-18 11:22:18 +08004428 ret = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004429 }
4430 }
4431
Tao Maad5a4d72008-01-30 14:21:32 +08004432 rec = NULL;
4433 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4434 rec = &el->l_recs[index + 1];
4435 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4436 path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004437 status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004438 if (status)
4439 goto out;
4440
4441 if (right_cpos == 0)
4442 goto out;
4443
Joel Beckerffdd7a52008-10-17 22:32:01 -07004444 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004445 if (!right_path)
4446 goto out;
4447
Joel Beckera2970292009-02-13 03:09:54 -08004448 status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004449 if (status)
4450 goto out;
4451
4452 new_el = path_leaf_el(right_path);
4453 rec = &new_el->l_recs[0];
4454 if (ocfs2_is_empty_extent(rec)) {
4455 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4456 bh = path_leaf_bh(right_path);
4457 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004458 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004459 "Extent block #%llu has an "
4460 "invalid l_next_free_rec of %d",
4461 (unsigned long long)le64_to_cpu(eb->h_blkno),
4462 le16_to_cpu(new_el->l_next_free_rec));
4463 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004464 goto out;
4465 }
4466 rec = &new_el->l_recs[1];
4467 }
4468 }
4469
4470 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004471 enum ocfs2_contig_type contig_type;
4472
Tao Ma853a3a12009-08-18 11:22:18 +08004473 contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004474
4475 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4476 ret = CONTIG_LEFTRIGHT;
4477 else if (ret == CONTIG_NONE)
4478 ret = contig_type;
4479 }
4480
Tao Maad5a4d72008-01-30 14:21:32 +08004481out:
4482 if (left_path)
4483 ocfs2_free_path(left_path);
4484 if (right_path)
4485 ocfs2_free_path(right_path);
4486
Mark Fasheh328d5752007-06-18 10:48:04 -07004487 return ret;
4488}
4489
Joel Becker1ef61b32009-02-13 03:12:33 -08004490static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004491 struct ocfs2_insert_type *insert,
4492 struct ocfs2_extent_list *el,
Joel Becker1ef61b32009-02-13 03:12:33 -08004493 struct ocfs2_extent_rec *insert_rec)
Mark Fashehdcd05382007-01-16 11:32:23 -08004494{
4495 int i;
4496 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4497
Mark Fashehe48edee2007-03-07 16:46:57 -08004498 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4499
Mark Fashehdcd05382007-01-16 11:32:23 -08004500 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
Tao Ma853a3a12009-08-18 11:22:18 +08004501 contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4502 insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004503 if (contig_type != CONTIG_NONE) {
4504 insert->ins_contig_index = i;
4505 break;
4506 }
4507 }
4508 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004509
4510 if (insert->ins_contig != CONTIG_NONE) {
4511 struct ocfs2_extent_rec *rec =
4512 &el->l_recs[insert->ins_contig_index];
4513 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4514 le16_to_cpu(insert_rec->e_leaf_clusters);
4515
4516 /*
4517 * Caller might want us to limit the size of extents, don't
4518 * calculate contiguousness if we might exceed that limit.
4519 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004520 if (et->et_max_leaf_clusters &&
4521 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004522 insert->ins_contig = CONTIG_NONE;
4523 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004524}
4525
4526/*
4527 * This should only be called against the righmost leaf extent list.
4528 *
4529 * ocfs2_figure_appending_type() will figure out whether we'll have to
4530 * insert at the tail of the rightmost leaf.
4531 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004532 * This should also work against the root extent list for tree's with 0
4533 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004534 * then the logic here makes sense.
4535 */
4536static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4537 struct ocfs2_extent_list *el,
4538 struct ocfs2_extent_rec *insert_rec)
4539{
4540 int i;
4541 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4542 struct ocfs2_extent_rec *rec;
4543
4544 insert->ins_appending = APPEND_NONE;
4545
Mark Fashehe48edee2007-03-07 16:46:57 -08004546 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004547
4548 if (!el->l_next_free_rec)
4549 goto set_tail_append;
4550
4551 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4552 /* Were all records empty? */
4553 if (le16_to_cpu(el->l_next_free_rec) == 1)
4554 goto set_tail_append;
4555 }
4556
4557 i = le16_to_cpu(el->l_next_free_rec) - 1;
4558 rec = &el->l_recs[i];
4559
Mark Fashehe48edee2007-03-07 16:46:57 -08004560 if (cpos >=
4561 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004562 goto set_tail_append;
4563
4564 return;
4565
4566set_tail_append:
4567 insert->ins_appending = APPEND_TAIL;
4568}
4569
4570/*
4571 * Helper function called at the begining of an insert.
4572 *
4573 * This computes a few things that are commonly used in the process of
4574 * inserting into the btree:
4575 * - Whether the new extent is contiguous with an existing one.
4576 * - The current tree depth.
4577 * - Whether the insert is an appending one.
4578 * - The total # of free records in the tree.
4579 *
4580 * All of the information is stored on the ocfs2_insert_type
4581 * structure.
4582 */
Joel Becker627961b2009-02-13 03:14:38 -08004583static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004584 struct buffer_head **last_eb_bh,
4585 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004586 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004587 struct ocfs2_insert_type *insert)
4588{
4589 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004590 struct ocfs2_extent_block *eb;
4591 struct ocfs2_extent_list *el;
4592 struct ocfs2_path *path = NULL;
4593 struct buffer_head *bh = NULL;
4594
Mark Fasheh328d5752007-06-18 10:48:04 -07004595 insert->ins_split = SPLIT_NONE;
4596
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004597 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004598 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4599
4600 if (el->l_tree_depth) {
4601 /*
4602 * If we have tree depth, we read in the
4603 * rightmost extent block ahead of time as
4604 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4605 * may want it later.
4606 */
Joel Becker3d03a302009-02-12 17:49:26 -08004607 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08004608 ocfs2_et_get_last_eb_blk(et),
4609 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004610 if (ret) {
4611 mlog_exit(ret);
4612 goto out;
4613 }
4614 eb = (struct ocfs2_extent_block *) bh->b_data;
4615 el = &eb->h_list;
4616 }
4617
4618 /*
4619 * Unless we have a contiguous insert, we'll need to know if
4620 * there is room left in our allocation tree for another
4621 * extent record.
4622 *
4623 * XXX: This test is simplistic, we can search for empty
4624 * extent records too.
4625 */
Tao Maoc77534f2007-08-28 17:22:33 -07004626 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004627 le16_to_cpu(el->l_next_free_rec);
4628
4629 if (!insert->ins_tree_depth) {
Joel Becker1ef61b32009-02-13 03:12:33 -08004630 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004631 ocfs2_figure_appending_type(insert, el, insert_rec);
4632 return 0;
4633 }
4634
Joel Beckerffdd7a52008-10-17 22:32:01 -07004635 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004636 if (!path) {
4637 ret = -ENOMEM;
4638 mlog_errno(ret);
4639 goto out;
4640 }
4641
4642 /*
4643 * In the case that we're inserting past what the tree
4644 * currently accounts for, ocfs2_find_path() will return for
4645 * us the rightmost tree path. This is accounted for below in
4646 * the appending code.
4647 */
Joel Beckerfacdb772009-02-12 18:08:48 -08004648 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -08004649 if (ret) {
4650 mlog_errno(ret);
4651 goto out;
4652 }
4653
4654 el = path_leaf_el(path);
4655
4656 /*
4657 * Now that we have the path, there's two things we want to determine:
4658 * 1) Contiguousness (also set contig_index if this is so)
4659 *
4660 * 2) Are we doing an append? We can trivially break this up
4661 * into two types of appends: simple record append, or a
4662 * rotate inside the tail leaf.
4663 */
Joel Becker1ef61b32009-02-13 03:12:33 -08004664 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004665
4666 /*
4667 * The insert code isn't quite ready to deal with all cases of
4668 * left contiguousness. Specifically, if it's an insert into
4669 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004670 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004671 * left. For now, just catch that case and fool the layers
4672 * above us. This works just fine for tree_depth == 0, which
4673 * is why we allow that above.
4674 */
4675 if (insert->ins_contig == CONTIG_LEFT &&
4676 insert->ins_contig_index == 0)
4677 insert->ins_contig = CONTIG_NONE;
4678
4679 /*
4680 * Ok, so we can simply compare against last_eb to figure out
4681 * whether the path doesn't exist. This will only happen in
4682 * the case that we're doing a tail append, so maybe we can
4683 * take advantage of that information somehow.
4684 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004685 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004686 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004687 /*
4688 * Ok, ocfs2_find_path() returned us the rightmost
4689 * tree path. This might be an appending insert. There are
4690 * two cases:
4691 * 1) We're doing a true append at the tail:
4692 * -This might even be off the end of the leaf
4693 * 2) We're "appending" by rotating in the tail
4694 */
4695 ocfs2_figure_appending_type(insert, el, insert_rec);
4696 }
4697
4698out:
4699 ocfs2_free_path(path);
4700
4701 if (ret == 0)
4702 *last_eb_bh = bh;
4703 else
4704 brelse(bh);
4705 return ret;
4706}
4707
4708/*
Joel Beckercc79d8c2009-02-13 03:24:43 -08004709 * Insert an extent into a btree.
Mark Fashehdcd05382007-01-16 11:32:23 -08004710 *
Joel Beckercc79d8c2009-02-13 03:24:43 -08004711 * The caller needs to update the owning btree's cluster count.
Mark Fashehdcd05382007-01-16 11:32:23 -08004712 */
Joel Beckercc79d8c2009-02-13 03:24:43 -08004713int ocfs2_insert_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004714 struct ocfs2_extent_tree *et,
4715 u32 cpos,
4716 u64 start_blk,
4717 u32 new_clusters,
4718 u8 flags,
4719 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004720{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004721 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004722 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004723 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004724 struct ocfs2_insert_type insert = {0, };
4725 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004726
Joel Beckercc79d8c2009-02-13 03:24:43 -08004727 mlog(0, "add %u clusters at position %u to owner %llu\n",
4728 new_clusters, cpos,
4729 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08004730
Mark Fashehe48edee2007-03-07 16:46:57 -08004731 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004732 rec.e_cpos = cpu_to_le32(cpos);
4733 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004734 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004735 rec.e_flags = flags;
Joel Becker6136ca52009-02-12 19:32:43 -08004736 status = ocfs2_et_insert_check(et, &rec);
Joel Becker1e61ee72008-08-20 18:32:45 -07004737 if (status) {
4738 mlog_errno(status);
4739 goto bail;
4740 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004741
Joel Becker627961b2009-02-13 03:14:38 -08004742 status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004743 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004744 if (status < 0) {
4745 mlog_errno(status);
4746 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004747 }
4748
Mark Fashehdcd05382007-01-16 11:32:23 -08004749 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4750 "Insert.contig_index: %d, Insert.free_records: %d, "
4751 "Insert.tree_depth: %d\n",
4752 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004753 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004754
Tao Maoc77534f2007-08-28 17:22:33 -07004755 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004756 status = ocfs2_grow_tree(handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004757 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004758 meta_ac);
4759 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004760 mlog_errno(status);
4761 goto bail;
4762 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004763 }
4764
Mark Fashehdcd05382007-01-16 11:32:23 -08004765 /* Finally, we can add clusters. This might rotate the tree for us. */
Joel Becker3505bec2009-02-13 02:57:58 -08004766 status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004767 if (status < 0)
4768 mlog_errno(status);
Joel Becker92ba4702009-02-13 03:18:34 -08004769 else
4770 ocfs2_et_extent_map_insert(et, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004771
4772bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004773 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004774
Tao Maf56654c2008-08-18 17:38:48 +08004775 mlog_exit(status);
4776 return status;
4777}
4778
Tao Ma0eb8d472008-08-18 17:38:45 +08004779/*
4780 * Allcate and add clusters into the extent b-tree.
4781 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004782 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004783 * it is not limited to the file storage. Any extent tree can use this
4784 * function if it implements the proper ocfs2_extent_tree.
4785 */
Joel Beckercbee7e12009-02-13 03:34:15 -08004786int ocfs2_add_clusters_in_btree(handle_t *handle,
4787 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004788 u32 *logical_offset,
4789 u32 clusters_to_add,
4790 int mark_unwritten,
Tao Ma0eb8d472008-08-18 17:38:45 +08004791 struct ocfs2_alloc_context *data_ac,
4792 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004793 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004794{
4795 int status = 0;
4796 int free_extents;
4797 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4798 u32 bit_off, num_bits;
4799 u64 block;
4800 u8 flags = 0;
Joel Beckercbee7e12009-02-13 03:34:15 -08004801 struct ocfs2_super *osb =
4802 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Tao Ma0eb8d472008-08-18 17:38:45 +08004803
4804 BUG_ON(!clusters_to_add);
4805
4806 if (mark_unwritten)
4807 flags = OCFS2_EXT_UNWRITTEN;
4808
Joel Becker3d03a302009-02-12 17:49:26 -08004809 free_extents = ocfs2_num_free_extents(osb, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004810 if (free_extents < 0) {
4811 status = free_extents;
4812 mlog_errno(status);
4813 goto leave;
4814 }
4815
4816 /* there are two cases which could cause us to EAGAIN in the
4817 * we-need-more-metadata case:
4818 * 1) we haven't reserved *any*
4819 * 2) we are so fragmented, we've needed to add metadata too
4820 * many times. */
4821 if (!free_extents && !meta_ac) {
4822 mlog(0, "we haven't reserved any metadata!\n");
4823 status = -EAGAIN;
4824 reason = RESTART_META;
4825 goto leave;
4826 } else if ((!free_extents)
4827 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004828 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004829 mlog(0, "filesystem is really fragmented...\n");
4830 status = -EAGAIN;
4831 reason = RESTART_META;
4832 goto leave;
4833 }
4834
4835 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4836 clusters_to_add, &bit_off, &num_bits);
4837 if (status < 0) {
4838 if (status != -ENOSPC)
4839 mlog_errno(status);
4840 goto leave;
4841 }
4842
4843 BUG_ON(num_bits > clusters_to_add);
4844
Joel Becker13723d02008-10-17 19:25:01 -07004845 /* reserve our write early -- insert_extent may update the tree root */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004846 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004847 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d472008-08-18 17:38:45 +08004848 if (status < 0) {
4849 mlog_errno(status);
4850 goto leave;
4851 }
4852
4853 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Joel Beckercbee7e12009-02-13 03:34:15 -08004854 mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
4855 num_bits, bit_off,
4856 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Joel Beckercc79d8c2009-02-13 03:24:43 -08004857 status = ocfs2_insert_extent(handle, et, *logical_offset, block,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004858 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004859 if (status < 0) {
4860 mlog_errno(status);
4861 goto leave;
4862 }
4863
Joel Beckerf99b9b72008-08-20 19:36:33 -07004864 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004865 if (status < 0) {
4866 mlog_errno(status);
4867 goto leave;
4868 }
4869
4870 clusters_to_add -= num_bits;
4871 *logical_offset += num_bits;
4872
4873 if (clusters_to_add) {
4874 mlog(0, "need to alloc once more, wanted = %u\n",
4875 clusters_to_add);
4876 status = -EAGAIN;
4877 reason = RESTART_TRANS;
4878 }
4879
4880leave:
4881 mlog_exit(status);
4882 if (reason_ret)
4883 *reason_ret = reason;
4884 return status;
4885}
4886
Mark Fasheh328d5752007-06-18 10:48:04 -07004887static void ocfs2_make_right_split_rec(struct super_block *sb,
4888 struct ocfs2_extent_rec *split_rec,
4889 u32 cpos,
4890 struct ocfs2_extent_rec *rec)
4891{
4892 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4893 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4894
4895 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4896
4897 split_rec->e_cpos = cpu_to_le32(cpos);
4898 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4899
4900 split_rec->e_blkno = rec->e_blkno;
4901 le64_add_cpu(&split_rec->e_blkno,
4902 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4903
4904 split_rec->e_flags = rec->e_flags;
4905}
4906
Joel Beckerd2311292009-02-13 03:43:22 -08004907static int ocfs2_split_and_insert(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004908 struct ocfs2_extent_tree *et,
Joel Beckerd2311292009-02-13 03:43:22 -08004909 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004910 struct buffer_head **last_eb_bh,
4911 int split_index,
4912 struct ocfs2_extent_rec *orig_split_rec,
4913 struct ocfs2_alloc_context *meta_ac)
4914{
4915 int ret = 0, depth;
4916 unsigned int insert_range, rec_range, do_leftright = 0;
4917 struct ocfs2_extent_rec tmprec;
4918 struct ocfs2_extent_list *rightmost_el;
4919 struct ocfs2_extent_rec rec;
4920 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4921 struct ocfs2_insert_type insert;
4922 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004923
4924leftright:
4925 /*
4926 * Store a copy of the record on the stack - it might move
4927 * around as the tree is manipulated below.
4928 */
4929 rec = path_leaf_el(path)->l_recs[split_index];
4930
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004931 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004932
4933 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4934 if (depth) {
4935 BUG_ON(!(*last_eb_bh));
4936 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4937 rightmost_el = &eb->h_list;
4938 }
4939
4940 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4941 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004942 ret = ocfs2_grow_tree(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08004943 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004944 if (ret) {
4945 mlog_errno(ret);
4946 goto out;
4947 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004948 }
4949
4950 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4951 insert.ins_appending = APPEND_NONE;
4952 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004953 insert.ins_tree_depth = depth;
4954
4955 insert_range = le32_to_cpu(split_rec.e_cpos) +
4956 le16_to_cpu(split_rec.e_leaf_clusters);
4957 rec_range = le32_to_cpu(rec.e_cpos) +
4958 le16_to_cpu(rec.e_leaf_clusters);
4959
4960 if (split_rec.e_cpos == rec.e_cpos) {
4961 insert.ins_split = SPLIT_LEFT;
4962 } else if (insert_range == rec_range) {
4963 insert.ins_split = SPLIT_RIGHT;
4964 } else {
4965 /*
4966 * Left/right split. We fake this as a right split
4967 * first and then make a second pass as a left split.
4968 */
4969 insert.ins_split = SPLIT_RIGHT;
4970
Joel Beckerd2311292009-02-13 03:43:22 -08004971 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4972 &tmprec, insert_range, &rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004973
4974 split_rec = tmprec;
4975
4976 BUG_ON(do_leftright);
4977 do_leftright = 1;
4978 }
4979
Joel Becker3505bec2009-02-13 02:57:58 -08004980 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004981 if (ret) {
4982 mlog_errno(ret);
4983 goto out;
4984 }
4985
4986 if (do_leftright == 1) {
4987 u32 cpos;
4988 struct ocfs2_extent_list *el;
4989
4990 do_leftright++;
4991 split_rec = *orig_split_rec;
4992
4993 ocfs2_reinit_path(path, 1);
4994
4995 cpos = le32_to_cpu(split_rec.e_cpos);
Joel Beckerfacdb772009-02-12 18:08:48 -08004996 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07004997 if (ret) {
4998 mlog_errno(ret);
4999 goto out;
5000 }
5001
5002 el = path_leaf_el(path);
5003 split_index = ocfs2_search_extent_list(el, cpos);
5004 goto leftright;
5005 }
5006out:
5007
5008 return ret;
5009}
5010
Joel Beckerf3868d02009-02-17 19:46:04 -08005011static int ocfs2_replace_extent_rec(handle_t *handle,
5012 struct ocfs2_extent_tree *et,
Tao Ma47be12e2009-01-09 07:32:48 +08005013 struct ocfs2_path *path,
5014 struct ocfs2_extent_list *el,
5015 int split_index,
5016 struct ocfs2_extent_rec *split_rec)
5017{
5018 int ret;
5019
Joel Beckerf3868d02009-02-17 19:46:04 -08005020 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Tao Ma47be12e2009-01-09 07:32:48 +08005021 path_num_items(path) - 1);
5022 if (ret) {
5023 mlog_errno(ret);
5024 goto out;
5025 }
5026
5027 el->l_recs[split_index] = *split_rec;
5028
5029 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5030out:
5031 return ret;
5032}
5033
Mark Fasheh328d5752007-06-18 10:48:04 -07005034/*
5035 * Mark part or all of the extent record at split_index in the leaf
5036 * pointed to by path as written. This removes the unwritten
5037 * extent flag.
5038 *
5039 * Care is taken to handle contiguousness so as to not grow the tree.
5040 *
5041 * meta_ac is not strictly necessary - we only truly need it if growth
5042 * of the tree is required. All other cases will degrade into a less
5043 * optimal tree layout.
5044 *
Tao Mae7d4cb62008-08-18 17:38:44 +08005045 * last_eb_bh should be the rightmost leaf block for any extent
5046 * btree. Since a split may grow the tree or a merge might shrink it,
5047 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07005048 *
5049 * This code is optimized for readability - several passes might be
5050 * made over certain portions of the tree. All of those blocks will
5051 * have been brought into cache (and pinned via the journal), so the
5052 * extra overhead is not expressed in terms of disk reads.
5053 */
Joel Beckera1cf0762009-02-13 03:45:49 -08005054static int __ocfs2_mark_extent_written(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08005055 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005056 struct ocfs2_path *path,
5057 int split_index,
5058 struct ocfs2_extent_rec *split_rec,
5059 struct ocfs2_alloc_context *meta_ac,
5060 struct ocfs2_cached_dealloc_ctxt *dealloc)
5061{
5062 int ret = 0;
5063 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08005064 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07005065 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5066 struct ocfs2_merge_ctxt ctxt;
5067 struct ocfs2_extent_list *rightmost_el;
5068
Roel Kluin3cf0c502007-10-27 00:20:36 +02005069 if (!(rec->e_flags & OCFS2_EXT_UNWRITTEN)) {
Mark Fasheh328d5752007-06-18 10:48:04 -07005070 ret = -EIO;
5071 mlog_errno(ret);
5072 goto out;
5073 }
5074
5075 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5076 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5077 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5078 ret = -EIO;
5079 mlog_errno(ret);
5080 goto out;
5081 }
5082
Joel Beckera2970292009-02-13 03:09:54 -08005083 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005084 split_index,
5085 split_rec);
5086
5087 /*
5088 * The core merge / split code wants to know how much room is
Joel Beckera1cf0762009-02-13 03:45:49 -08005089 * left in this allocation tree, so we pass the
Mark Fasheh328d5752007-06-18 10:48:04 -07005090 * rightmost extent list.
5091 */
5092 if (path->p_tree_depth) {
5093 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005094
Joel Becker3d03a302009-02-12 17:49:26 -08005095 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005096 ocfs2_et_get_last_eb_blk(et),
5097 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005098 if (ret) {
5099 mlog_exit(ret);
5100 goto out;
5101 }
5102
5103 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005104 rightmost_el = &eb->h_list;
5105 } else
5106 rightmost_el = path_root_el(path);
5107
Mark Fasheh328d5752007-06-18 10:48:04 -07005108 if (rec->e_cpos == split_rec->e_cpos &&
5109 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5110 ctxt.c_split_covers_rec = 1;
5111 else
5112 ctxt.c_split_covers_rec = 0;
5113
5114 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5115
Mark Fasheh015452b2007-09-12 10:21:22 -07005116 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5117 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5118 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005119
5120 if (ctxt.c_contig_type == CONTIG_NONE) {
5121 if (ctxt.c_split_covers_rec)
Joel Beckerf3868d02009-02-17 19:46:04 -08005122 ret = ocfs2_replace_extent_rec(handle, et, path, el,
Tao Ma47be12e2009-01-09 07:32:48 +08005123 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005124 else
Joel Beckerd2311292009-02-13 03:43:22 -08005125 ret = ocfs2_split_and_insert(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005126 &last_eb_bh, split_index,
5127 split_rec, meta_ac);
5128 if (ret)
5129 mlog_errno(ret);
5130 } else {
Joel Beckerc495dd22009-02-13 02:19:11 -08005131 ret = ocfs2_try_to_merge_extent(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005132 split_index, split_rec,
Joel Beckerc495dd22009-02-13 02:19:11 -08005133 dealloc, &ctxt);
Mark Fasheh328d5752007-06-18 10:48:04 -07005134 if (ret)
5135 mlog_errno(ret);
5136 }
5137
Mark Fasheh328d5752007-06-18 10:48:04 -07005138out:
5139 brelse(last_eb_bh);
5140 return ret;
5141}
5142
5143/*
5144 * Mark the already-existing extent at cpos as written for len clusters.
5145 *
5146 * If the existing extent is larger than the request, initiate a
5147 * split. An attempt will be made at merging with adjacent extents.
5148 *
5149 * The caller is responsible for passing down meta_ac if we'll need it.
5150 */
Joel Beckerf99b9b72008-08-20 19:36:33 -07005151int ocfs2_mark_extent_written(struct inode *inode,
5152 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07005153 handle_t *handle, u32 cpos, u32 len, u32 phys,
5154 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005155 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005156{
5157 int ret, index;
5158 u64 start_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys);
5159 struct ocfs2_extent_rec split_rec;
5160 struct ocfs2_path *left_path = NULL;
5161 struct ocfs2_extent_list *el;
5162
5163 mlog(0, "Inode %lu cpos %u, len %u, phys %u (%llu)\n",
5164 inode->i_ino, cpos, len, phys, (unsigned long long)start_blkno);
5165
5166 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5167 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5168 "that are being written to, but the feature bit "
5169 "is not set in the super block.",
5170 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5171 ret = -EROFS;
5172 goto out;
5173 }
5174
5175 /*
5176 * XXX: This should be fixed up so that we just re-insert the
5177 * next extent records.
5178 */
Joel Becker4c911ee2009-02-13 02:50:12 -08005179 ocfs2_et_extent_map_truncate(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07005180
Joel Beckerffdd7a52008-10-17 22:32:01 -07005181 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005182 if (!left_path) {
5183 ret = -ENOMEM;
5184 mlog_errno(ret);
5185 goto out;
5186 }
5187
Joel Beckerfacdb772009-02-12 18:08:48 -08005188 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005189 if (ret) {
5190 mlog_errno(ret);
5191 goto out;
5192 }
5193 el = path_leaf_el(left_path);
5194
5195 index = ocfs2_search_extent_list(el, cpos);
5196 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
5197 ocfs2_error(inode->i_sb,
5198 "Inode %llu has an extent at cpos %u which can no "
5199 "longer be found.\n",
5200 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
5201 ret = -EROFS;
5202 goto out;
5203 }
5204
5205 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5206 split_rec.e_cpos = cpu_to_le32(cpos);
5207 split_rec.e_leaf_clusters = cpu_to_le16(len);
5208 split_rec.e_blkno = cpu_to_le64(start_blkno);
5209 split_rec.e_flags = path_leaf_el(left_path)->l_recs[index].e_flags;
5210 split_rec.e_flags &= ~OCFS2_EXT_UNWRITTEN;
5211
Joel Beckera1cf0762009-02-13 03:45:49 -08005212 ret = __ocfs2_mark_extent_written(handle, et, left_path,
Tao Mae7d4cb62008-08-18 17:38:44 +08005213 index, &split_rec, meta_ac,
5214 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005215 if (ret)
5216 mlog_errno(ret);
5217
5218out:
5219 ocfs2_free_path(left_path);
5220 return ret;
5221}
5222
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005223static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5224 struct ocfs2_path *path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005225 int index, u32 new_range,
5226 struct ocfs2_alloc_context *meta_ac)
5227{
5228 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005229 struct buffer_head *last_eb_bh = NULL;
5230 struct ocfs2_extent_block *eb;
5231 struct ocfs2_extent_list *rightmost_el, *el;
5232 struct ocfs2_extent_rec split_rec;
5233 struct ocfs2_extent_rec *rec;
5234 struct ocfs2_insert_type insert;
5235
5236 /*
5237 * Setup the record to split before we grow the tree.
5238 */
5239 el = path_leaf_el(path);
5240 rec = &el->l_recs[index];
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005241 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5242 &split_rec, new_range, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005243
5244 depth = path->p_tree_depth;
5245 if (depth > 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08005246 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005247 ocfs2_et_get_last_eb_blk(et),
5248 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005249 if (ret < 0) {
5250 mlog_errno(ret);
5251 goto out;
5252 }
5253
5254 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5255 rightmost_el = &eb->h_list;
5256 } else
5257 rightmost_el = path_leaf_el(path);
5258
Tao Ma811f9332008-08-18 17:38:43 +08005259 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005260 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005261 ret = ocfs2_extend_trans(handle, credits);
5262 if (ret) {
5263 mlog_errno(ret);
5264 goto out;
5265 }
5266
5267 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5268 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08005269 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005270 meta_ac);
5271 if (ret) {
5272 mlog_errno(ret);
5273 goto out;
5274 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005275 }
5276
5277 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5278 insert.ins_appending = APPEND_NONE;
5279 insert.ins_contig = CONTIG_NONE;
5280 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005281 insert.ins_tree_depth = depth;
5282
Joel Becker3505bec2009-02-13 02:57:58 -08005283 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005284 if (ret)
5285 mlog_errno(ret);
5286
5287out:
5288 brelse(last_eb_bh);
5289 return ret;
5290}
5291
Joel Becker043beeb2009-02-13 02:42:30 -08005292static int ocfs2_truncate_rec(handle_t *handle,
5293 struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005294 struct ocfs2_path *path, int index,
5295 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker043beeb2009-02-13 02:42:30 -08005296 u32 cpos, u32 len)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005297{
5298 int ret;
5299 u32 left_cpos, rec_range, trunc_range;
5300 int wants_rotate = 0, is_rightmost_tree_rec = 0;
Joel Becker043beeb2009-02-13 02:42:30 -08005301 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005302 struct ocfs2_path *left_path = NULL;
5303 struct ocfs2_extent_list *el = path_leaf_el(path);
5304 struct ocfs2_extent_rec *rec;
5305 struct ocfs2_extent_block *eb;
5306
5307 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Joel Becker70f18c02009-02-13 02:09:31 -08005308 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005309 if (ret) {
5310 mlog_errno(ret);
5311 goto out;
5312 }
5313
5314 index--;
5315 }
5316
5317 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5318 path->p_tree_depth) {
5319 /*
5320 * Check whether this is the rightmost tree record. If
5321 * we remove all of this record or part of its right
5322 * edge then an update of the record lengths above it
5323 * will be required.
5324 */
5325 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5326 if (eb->h_next_leaf_blk == 0)
5327 is_rightmost_tree_rec = 1;
5328 }
5329
5330 rec = &el->l_recs[index];
5331 if (index == 0 && path->p_tree_depth &&
5332 le32_to_cpu(rec->e_cpos) == cpos) {
5333 /*
5334 * Changing the leftmost offset (via partial or whole
5335 * record truncate) of an interior (or rightmost) path
5336 * means we have to update the subtree that is formed
5337 * by this leaf and the one to it's left.
5338 *
5339 * There are two cases we can skip:
Joel Becker043beeb2009-02-13 02:42:30 -08005340 * 1) Path is the leftmost one in our btree.
Mark Fashehd0c7d702007-07-03 13:27:22 -07005341 * 2) The leaf is rightmost and will be empty after
5342 * we remove the extent record - the rotate code
5343 * knows how to update the newly formed edge.
5344 */
5345
Joel Becker043beeb2009-02-13 02:42:30 -08005346 ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005347 if (ret) {
5348 mlog_errno(ret);
5349 goto out;
5350 }
5351
5352 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005353 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005354 if (!left_path) {
5355 ret = -ENOMEM;
5356 mlog_errno(ret);
5357 goto out;
5358 }
5359
Joel Beckerfacdb772009-02-12 18:08:48 -08005360 ret = ocfs2_find_path(et->et_ci, left_path,
5361 left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005362 if (ret) {
5363 mlog_errno(ret);
5364 goto out;
5365 }
5366 }
5367 }
5368
5369 ret = ocfs2_extend_rotate_transaction(handle, 0,
5370 handle->h_buffer_credits,
5371 path);
5372 if (ret) {
5373 mlog_errno(ret);
5374 goto out;
5375 }
5376
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005377 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005378 if (ret) {
5379 mlog_errno(ret);
5380 goto out;
5381 }
5382
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005383 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005384 if (ret) {
5385 mlog_errno(ret);
5386 goto out;
5387 }
5388
5389 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5390 trunc_range = cpos + len;
5391
5392 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5393 int next_free;
5394
5395 memset(rec, 0, sizeof(*rec));
5396 ocfs2_cleanup_merge(el, index);
5397 wants_rotate = 1;
5398
5399 next_free = le16_to_cpu(el->l_next_free_rec);
5400 if (is_rightmost_tree_rec && next_free > 1) {
5401 /*
5402 * We skip the edge update if this path will
5403 * be deleted by the rotate code.
5404 */
5405 rec = &el->l_recs[next_free - 1];
Joel Beckerd401dc12009-02-13 02:24:10 -08005406 ocfs2_adjust_rightmost_records(handle, et, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005407 rec);
5408 }
5409 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5410 /* Remove leftmost portion of the record. */
5411 le32_add_cpu(&rec->e_cpos, len);
5412 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5413 le16_add_cpu(&rec->e_leaf_clusters, -len);
5414 } else if (rec_range == trunc_range) {
5415 /* Remove rightmost portion of the record */
5416 le16_add_cpu(&rec->e_leaf_clusters, -len);
5417 if (is_rightmost_tree_rec)
Joel Beckerd401dc12009-02-13 02:24:10 -08005418 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005419 } else {
5420 /* Caller should have trapped this. */
Joel Becker043beeb2009-02-13 02:42:30 -08005421 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5422 "(%u, %u)\n",
5423 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005424 le32_to_cpu(rec->e_cpos),
5425 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5426 BUG();
5427 }
5428
5429 if (left_path) {
5430 int subtree_index;
5431
Joel Becker7dc02802009-02-12 19:20:13 -08005432 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Joel Becker4619c732009-02-12 19:02:36 -08005433 ocfs2_complete_edge_insert(handle, left_path, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005434 subtree_index);
5435 }
5436
5437 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5438
Joel Becker70f18c02009-02-13 02:09:31 -08005439 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005440 if (ret) {
5441 mlog_errno(ret);
5442 goto out;
5443 }
5444
5445out:
5446 ocfs2_free_path(left_path);
5447 return ret;
5448}
5449
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005450int ocfs2_remove_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005451 struct ocfs2_extent_tree *et,
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005452 u32 cpos, u32 len,
Mark Fasheh063c4562007-07-03 13:34:11 -07005453 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005454 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005455{
5456 int ret, index;
5457 u32 rec_range, trunc_range;
5458 struct ocfs2_extent_rec *rec;
5459 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005460 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005461
Joel Becker4c911ee2009-02-13 02:50:12 -08005462 /*
5463 * XXX: Why are we truncating to 0 instead of wherever this
5464 * affects us?
5465 */
5466 ocfs2_et_extent_map_truncate(et, 0);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005467
Joel Beckerffdd7a52008-10-17 22:32:01 -07005468 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005469 if (!path) {
5470 ret = -ENOMEM;
5471 mlog_errno(ret);
5472 goto out;
5473 }
5474
Joel Beckerfacdb772009-02-12 18:08:48 -08005475 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005476 if (ret) {
5477 mlog_errno(ret);
5478 goto out;
5479 }
5480
5481 el = path_leaf_el(path);
5482 index = ocfs2_search_extent_list(el, cpos);
5483 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005484 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5485 "Owner %llu has an extent at cpos %u which can no "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005486 "longer be found.\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005487 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5488 cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005489 ret = -EROFS;
5490 goto out;
5491 }
5492
5493 /*
5494 * We have 3 cases of extent removal:
5495 * 1) Range covers the entire extent rec
5496 * 2) Range begins or ends on one edge of the extent rec
5497 * 3) Range is in the middle of the extent rec (no shared edges)
5498 *
5499 * For case 1 we remove the extent rec and left rotate to
5500 * fill the hole.
5501 *
5502 * For case 2 we just shrink the existing extent rec, with a
5503 * tree update if the shrinking edge is also the edge of an
5504 * extent block.
5505 *
5506 * For case 3 we do a right split to turn the extent rec into
5507 * something case 2 can handle.
5508 */
5509 rec = &el->l_recs[index];
5510 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5511 trunc_range = cpos + len;
5512
5513 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5514
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005515 mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005516 "(cpos %u, len %u)\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005517 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5518 cpos, len, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005519 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5520
5521 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
Joel Becker043beeb2009-02-13 02:42:30 -08005522 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5523 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005524 if (ret) {
5525 mlog_errno(ret);
5526 goto out;
5527 }
5528 } else {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005529 ret = ocfs2_split_tree(handle, et, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005530 trunc_range, meta_ac);
5531 if (ret) {
5532 mlog_errno(ret);
5533 goto out;
5534 }
5535
5536 /*
5537 * The split could have manipulated the tree enough to
5538 * move the record location, so we have to look for it again.
5539 */
5540 ocfs2_reinit_path(path, 1);
5541
Joel Beckerfacdb772009-02-12 18:08:48 -08005542 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005543 if (ret) {
5544 mlog_errno(ret);
5545 goto out;
5546 }
5547
5548 el = path_leaf_el(path);
5549 index = ocfs2_search_extent_list(el, cpos);
5550 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005551 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5552 "Owner %llu: split at cpos %u lost record.",
5553 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005554 cpos);
5555 ret = -EROFS;
5556 goto out;
5557 }
5558
5559 /*
5560 * Double check our values here. If anything is fishy,
5561 * it's easier to catch it at the top level.
5562 */
5563 rec = &el->l_recs[index];
5564 rec_range = le32_to_cpu(rec->e_cpos) +
5565 ocfs2_rec_clusters(el, rec);
5566 if (rec_range != trunc_range) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005567 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5568 "Owner %llu: error after split at cpos %u"
Mark Fashehd0c7d702007-07-03 13:27:22 -07005569 "trunc len %u, existing record is (%u,%u)",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005570 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005571 cpos, len, le32_to_cpu(rec->e_cpos),
5572 ocfs2_rec_clusters(el, rec));
5573 ret = -EROFS;
5574 goto out;
5575 }
5576
Joel Becker043beeb2009-02-13 02:42:30 -08005577 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5578 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005579 if (ret) {
5580 mlog_errno(ret);
5581 goto out;
5582 }
5583 }
5584
5585out:
5586 ocfs2_free_path(path);
5587 return ret;
5588}
5589
Mark Fashehfecc0112008-11-12 15:16:38 -08005590int ocfs2_remove_btree_range(struct inode *inode,
5591 struct ocfs2_extent_tree *et,
5592 u32 cpos, u32 phys_cpos, u32 len,
5593 struct ocfs2_cached_dealloc_ctxt *dealloc)
5594{
5595 int ret;
5596 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5597 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5598 struct inode *tl_inode = osb->osb_tl_inode;
5599 handle_t *handle;
5600 struct ocfs2_alloc_context *meta_ac = NULL;
5601
5602 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5603 if (ret) {
5604 mlog_errno(ret);
5605 return ret;
5606 }
5607
5608 mutex_lock(&tl_inode->i_mutex);
5609
5610 if (ocfs2_truncate_log_needs_flush(osb)) {
5611 ret = __ocfs2_flush_truncate_log(osb);
5612 if (ret < 0) {
5613 mlog_errno(ret);
5614 goto out;
5615 }
5616 }
5617
Jan Karaa90714c2008-10-09 19:38:40 +02005618 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005619 if (IS_ERR(handle)) {
5620 ret = PTR_ERR(handle);
5621 mlog_errno(ret);
5622 goto out;
5623 }
5624
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005625 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07005626 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005627 if (ret) {
5628 mlog_errno(ret);
5629 goto out;
5630 }
5631
Mark Fashehfd4ef232009-01-29 15:06:21 -08005632 vfs_dq_free_space_nodirty(inode,
5633 ocfs2_clusters_to_bytes(inode->i_sb, len));
5634
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005635 ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
Mark Fashehfecc0112008-11-12 15:16:38 -08005636 if (ret) {
5637 mlog_errno(ret);
5638 goto out_commit;
5639 }
5640
Joel Becker6136ca52009-02-12 19:32:43 -08005641 ocfs2_et_update_clusters(et, -len);
Mark Fashehfecc0112008-11-12 15:16:38 -08005642
5643 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5644 if (ret) {
5645 mlog_errno(ret);
5646 goto out_commit;
5647 }
5648
5649 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5650 if (ret)
5651 mlog_errno(ret);
5652
5653out_commit:
5654 ocfs2_commit_trans(osb, handle);
5655out:
5656 mutex_unlock(&tl_inode->i_mutex);
5657
5658 if (meta_ac)
5659 ocfs2_free_alloc_context(meta_ac);
5660
5661 return ret;
5662}
5663
Mark Fasheh063c4562007-07-03 13:34:11 -07005664int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005665{
5666 struct buffer_head *tl_bh = osb->osb_tl_bh;
5667 struct ocfs2_dinode *di;
5668 struct ocfs2_truncate_log *tl;
5669
5670 di = (struct ocfs2_dinode *) tl_bh->b_data;
5671 tl = &di->id2.i_dealloc;
5672
5673 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5674 "slot %d, invalid truncate log parameters: used = "
5675 "%u, count = %u\n", osb->slot_num,
5676 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5677 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5678}
5679
5680static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5681 unsigned int new_start)
5682{
5683 unsigned int tail_index;
5684 unsigned int current_tail;
5685
5686 /* No records, nothing to coalesce */
5687 if (!le16_to_cpu(tl->tl_used))
5688 return 0;
5689
5690 tail_index = le16_to_cpu(tl->tl_used) - 1;
5691 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5692 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5693
5694 return current_tail == new_start;
5695}
5696
Mark Fasheh063c4562007-07-03 13:34:11 -07005697int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5698 handle_t *handle,
5699 u64 start_blk,
5700 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005701{
5702 int status, index;
5703 unsigned int start_cluster, tl_count;
5704 struct inode *tl_inode = osb->osb_tl_inode;
5705 struct buffer_head *tl_bh = osb->osb_tl_bh;
5706 struct ocfs2_dinode *di;
5707 struct ocfs2_truncate_log *tl;
5708
Mark Fashehb06970532006-03-03 10:24:33 -08005709 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5710 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005711
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005712 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005713
5714 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5715
5716 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005717
Joel Becker10995aa2008-11-13 14:49:12 -08005718 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5719 * by the underlying call to ocfs2_read_inode_block(), so any
5720 * corruption is a code bug */
5721 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5722
5723 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005724 tl_count = le16_to_cpu(tl->tl_count);
5725 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5726 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005727 "Truncate record count on #%llu invalid "
5728 "wanted %u, actual %u\n",
5729 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005730 ocfs2_truncate_recs_per_inode(osb->sb),
5731 le16_to_cpu(tl->tl_count));
5732
5733 /* Caller should have known to flush before calling us. */
5734 index = le16_to_cpu(tl->tl_used);
5735 if (index >= tl_count) {
5736 status = -ENOSPC;
5737 mlog_errno(status);
5738 goto bail;
5739 }
5740
Joel Becker0cf2f762009-02-12 16:41:25 -08005741 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005742 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005743 if (status < 0) {
5744 mlog_errno(status);
5745 goto bail;
5746 }
5747
5748 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005749 "%llu (index = %d)\n", num_clusters, start_cluster,
5750 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005751
5752 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5753 /*
5754 * Move index back to the record we are coalescing with.
5755 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5756 */
5757 index--;
5758
5759 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5760 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5761 index, le32_to_cpu(tl->tl_recs[index].t_start),
5762 num_clusters);
5763 } else {
5764 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5765 tl->tl_used = cpu_to_le16(index + 1);
5766 }
5767 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5768
5769 status = ocfs2_journal_dirty(handle, tl_bh);
5770 if (status < 0) {
5771 mlog_errno(status);
5772 goto bail;
5773 }
5774
5775bail:
5776 mlog_exit(status);
5777 return status;
5778}
5779
5780static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005781 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005782 struct inode *data_alloc_inode,
5783 struct buffer_head *data_alloc_bh)
5784{
5785 int status = 0;
5786 int i;
5787 unsigned int num_clusters;
5788 u64 start_blk;
5789 struct ocfs2_truncate_rec rec;
5790 struct ocfs2_dinode *di;
5791 struct ocfs2_truncate_log *tl;
5792 struct inode *tl_inode = osb->osb_tl_inode;
5793 struct buffer_head *tl_bh = osb->osb_tl_bh;
5794
5795 mlog_entry_void();
5796
5797 di = (struct ocfs2_dinode *) tl_bh->b_data;
5798 tl = &di->id2.i_dealloc;
5799 i = le16_to_cpu(tl->tl_used) - 1;
5800 while (i >= 0) {
5801 /* Caller has given us at least enough credits to
5802 * update the truncate log dinode */
Joel Becker0cf2f762009-02-12 16:41:25 -08005803 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005804 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005805 if (status < 0) {
5806 mlog_errno(status);
5807 goto bail;
5808 }
5809
5810 tl->tl_used = cpu_to_le16(i);
5811
5812 status = ocfs2_journal_dirty(handle, tl_bh);
5813 if (status < 0) {
5814 mlog_errno(status);
5815 goto bail;
5816 }
5817
5818 /* TODO: Perhaps we can calculate the bulk of the
5819 * credits up front rather than extending like
5820 * this. */
5821 status = ocfs2_extend_trans(handle,
5822 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5823 if (status < 0) {
5824 mlog_errno(status);
5825 goto bail;
5826 }
5827
5828 rec = tl->tl_recs[i];
5829 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5830 le32_to_cpu(rec.t_start));
5831 num_clusters = le32_to_cpu(rec.t_clusters);
5832
5833 /* if start_blk is not set, we ignore the record as
5834 * invalid. */
5835 if (start_blk) {
5836 mlog(0, "free record %d, start = %u, clusters = %u\n",
5837 i, le32_to_cpu(rec.t_start), num_clusters);
5838
5839 status = ocfs2_free_clusters(handle, data_alloc_inode,
5840 data_alloc_bh, start_blk,
5841 num_clusters);
5842 if (status < 0) {
5843 mlog_errno(status);
5844 goto bail;
5845 }
5846 }
5847 i--;
5848 }
5849
5850bail:
5851 mlog_exit(status);
5852 return status;
5853}
5854
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005855/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005856int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005857{
5858 int status;
5859 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005860 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005861 struct inode *tl_inode = osb->osb_tl_inode;
5862 struct inode *data_alloc_inode = NULL;
5863 struct buffer_head *tl_bh = osb->osb_tl_bh;
5864 struct buffer_head *data_alloc_bh = NULL;
5865 struct ocfs2_dinode *di;
5866 struct ocfs2_truncate_log *tl;
5867
5868 mlog_entry_void();
5869
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005870 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005871
5872 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005873
Joel Becker10995aa2008-11-13 14:49:12 -08005874 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5875 * by the underlying call to ocfs2_read_inode_block(), so any
5876 * corruption is a code bug */
5877 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5878
5879 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005880 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005881 mlog(0, "Flush %u records from truncate log #%llu\n",
5882 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005883 if (!num_to_flush) {
5884 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005885 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005886 }
5887
5888 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5889 GLOBAL_BITMAP_SYSTEM_INODE,
5890 OCFS2_INVALID_SLOT);
5891 if (!data_alloc_inode) {
5892 status = -EINVAL;
5893 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005894 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005895 }
5896
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005897 mutex_lock(&data_alloc_inode->i_mutex);
5898
Mark Fashehe63aecb62007-10-18 15:30:42 -07005899 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005900 if (status < 0) {
5901 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005902 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005903 }
5904
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005905 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005906 if (IS_ERR(handle)) {
5907 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005908 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005909 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005910 }
5911
5912 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5913 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005914 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005915 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005916
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005917 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005918
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005919out_unlock:
5920 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005921 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005922
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005923out_mutex:
5924 mutex_unlock(&data_alloc_inode->i_mutex);
5925 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005926
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005927out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005928 mlog_exit(status);
5929 return status;
5930}
5931
5932int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5933{
5934 int status;
5935 struct inode *tl_inode = osb->osb_tl_inode;
5936
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005937 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005938 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005939 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005940
5941 return status;
5942}
5943
David Howellsc4028952006-11-22 14:57:56 +00005944static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005945{
5946 int status;
David Howellsc4028952006-11-22 14:57:56 +00005947 struct ocfs2_super *osb =
5948 container_of(work, struct ocfs2_super,
5949 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005950
5951 mlog_entry_void();
5952
5953 status = ocfs2_flush_truncate_log(osb);
5954 if (status < 0)
5955 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005956 else
5957 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005958
5959 mlog_exit(status);
5960}
5961
5962#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5963void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5964 int cancel)
5965{
5966 if (osb->osb_tl_inode) {
5967 /* We want to push off log flushes while truncates are
5968 * still running. */
5969 if (cancel)
5970 cancel_delayed_work(&osb->osb_truncate_log_wq);
5971
5972 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5973 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5974 }
5975}
5976
5977static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5978 int slot_num,
5979 struct inode **tl_inode,
5980 struct buffer_head **tl_bh)
5981{
5982 int status;
5983 struct inode *inode = NULL;
5984 struct buffer_head *bh = NULL;
5985
5986 inode = ocfs2_get_system_file_inode(osb,
5987 TRUNCATE_LOG_SYSTEM_INODE,
5988 slot_num);
5989 if (!inode) {
5990 status = -EINVAL;
5991 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5992 goto bail;
5993 }
5994
Joel Beckerb657c952008-11-13 14:49:11 -08005995 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005996 if (status < 0) {
5997 iput(inode);
5998 mlog_errno(status);
5999 goto bail;
6000 }
6001
6002 *tl_inode = inode;
6003 *tl_bh = bh;
6004bail:
6005 mlog_exit(status);
6006 return status;
6007}
6008
6009/* called during the 1st stage of node recovery. we stamp a clean
6010 * truncate log and pass back a copy for processing later. if the
6011 * truncate log does not require processing, a *tl_copy is set to
6012 * NULL. */
6013int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6014 int slot_num,
6015 struct ocfs2_dinode **tl_copy)
6016{
6017 int status;
6018 struct inode *tl_inode = NULL;
6019 struct buffer_head *tl_bh = NULL;
6020 struct ocfs2_dinode *di;
6021 struct ocfs2_truncate_log *tl;
6022
6023 *tl_copy = NULL;
6024
6025 mlog(0, "recover truncate log from slot %d\n", slot_num);
6026
6027 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6028 if (status < 0) {
6029 mlog_errno(status);
6030 goto bail;
6031 }
6032
6033 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08006034
Joel Becker10995aa2008-11-13 14:49:12 -08006035 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6036 * validated by the underlying call to ocfs2_read_inode_block(),
6037 * so any corruption is a code bug */
6038 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6039
6040 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08006041 if (le16_to_cpu(tl->tl_used)) {
6042 mlog(0, "We'll have %u logs to recover\n",
6043 le16_to_cpu(tl->tl_used));
6044
6045 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6046 if (!(*tl_copy)) {
6047 status = -ENOMEM;
6048 mlog_errno(status);
6049 goto bail;
6050 }
6051
6052 /* Assuming the write-out below goes well, this copy
6053 * will be passed back to recovery for processing. */
6054 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6055
6056 /* All we need to do to clear the truncate log is set
6057 * tl_used. */
6058 tl->tl_used = 0;
6059
Joel Becker13723d02008-10-17 19:25:01 -07006060 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08006061 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08006062 if (status < 0) {
6063 mlog_errno(status);
6064 goto bail;
6065 }
6066 }
6067
6068bail:
6069 if (tl_inode)
6070 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006071 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006072
6073 if (status < 0 && (*tl_copy)) {
6074 kfree(*tl_copy);
6075 *tl_copy = NULL;
6076 }
6077
6078 mlog_exit(status);
6079 return status;
6080}
6081
6082int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6083 struct ocfs2_dinode *tl_copy)
6084{
6085 int status = 0;
6086 int i;
6087 unsigned int clusters, num_recs, start_cluster;
6088 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006089 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006090 struct inode *tl_inode = osb->osb_tl_inode;
6091 struct ocfs2_truncate_log *tl;
6092
6093 mlog_entry_void();
6094
6095 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6096 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6097 return -EINVAL;
6098 }
6099
6100 tl = &tl_copy->id2.i_dealloc;
6101 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08006102 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006103 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006104
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006105 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006106 for(i = 0; i < num_recs; i++) {
6107 if (ocfs2_truncate_log_needs_flush(osb)) {
6108 status = __ocfs2_flush_truncate_log(osb);
6109 if (status < 0) {
6110 mlog_errno(status);
6111 goto bail_up;
6112 }
6113 }
6114
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006115 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006116 if (IS_ERR(handle)) {
6117 status = PTR_ERR(handle);
6118 mlog_errno(status);
6119 goto bail_up;
6120 }
6121
6122 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6123 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6124 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6125
6126 status = ocfs2_truncate_log_append(osb, handle,
6127 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006128 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006129 if (status < 0) {
6130 mlog_errno(status);
6131 goto bail_up;
6132 }
6133 }
6134
6135bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006136 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006137
6138 mlog_exit(status);
6139 return status;
6140}
6141
6142void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6143{
6144 int status;
6145 struct inode *tl_inode = osb->osb_tl_inode;
6146
6147 mlog_entry_void();
6148
6149 if (tl_inode) {
6150 cancel_delayed_work(&osb->osb_truncate_log_wq);
6151 flush_workqueue(ocfs2_wq);
6152
6153 status = ocfs2_flush_truncate_log(osb);
6154 if (status < 0)
6155 mlog_errno(status);
6156
6157 brelse(osb->osb_tl_bh);
6158 iput(osb->osb_tl_inode);
6159 }
6160
6161 mlog_exit_void();
6162}
6163
6164int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6165{
6166 int status;
6167 struct inode *tl_inode = NULL;
6168 struct buffer_head *tl_bh = NULL;
6169
6170 mlog_entry_void();
6171
6172 status = ocfs2_get_truncate_log_info(osb,
6173 osb->slot_num,
6174 &tl_inode,
6175 &tl_bh);
6176 if (status < 0)
6177 mlog_errno(status);
6178
6179 /* ocfs2_truncate_log_shutdown keys on the existence of
6180 * osb->osb_tl_inode so we don't set any of the osb variables
6181 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006182 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6183 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006184 osb->osb_tl_bh = tl_bh;
6185 osb->osb_tl_inode = tl_inode;
6186
6187 mlog_exit(status);
6188 return status;
6189}
6190
Mark Fasheh2b604352007-06-22 15:45:27 -07006191/*
6192 * Delayed de-allocation of suballocator blocks.
6193 *
6194 * Some sets of block de-allocations might involve multiple suballocator inodes.
6195 *
6196 * The locking for this can get extremely complicated, especially when
6197 * the suballocator inodes to delete from aren't known until deep
6198 * within an unrelated codepath.
6199 *
6200 * ocfs2_extent_block structures are a good example of this - an inode
6201 * btree could have been grown by any number of nodes each allocating
6202 * out of their own suballoc inode.
6203 *
6204 * These structures allow the delay of block de-allocation until a
6205 * later time, when locking of multiple cluster inodes won't cause
6206 * deadlock.
6207 */
6208
6209/*
Tao Ma2891d292008-11-12 08:26:58 +08006210 * Describe a single bit freed from a suballocator. For the block
6211 * suballocators, it represents one block. For the global cluster
6212 * allocator, it represents some clusters and free_bit indicates
6213 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006214 */
6215struct ocfs2_cached_block_free {
6216 struct ocfs2_cached_block_free *free_next;
6217 u64 free_blk;
6218 unsigned int free_bit;
6219};
6220
6221struct ocfs2_per_slot_free_list {
6222 struct ocfs2_per_slot_free_list *f_next_suballocator;
6223 int f_inode_type;
6224 int f_slot;
6225 struct ocfs2_cached_block_free *f_first;
6226};
6227
Tao Ma2891d292008-11-12 08:26:58 +08006228static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6229 int sysfile_type,
6230 int slot,
6231 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006232{
6233 int ret;
6234 u64 bg_blkno;
6235 handle_t *handle;
6236 struct inode *inode;
6237 struct buffer_head *di_bh = NULL;
6238 struct ocfs2_cached_block_free *tmp;
6239
6240 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6241 if (!inode) {
6242 ret = -EINVAL;
6243 mlog_errno(ret);
6244 goto out;
6245 }
6246
6247 mutex_lock(&inode->i_mutex);
6248
Mark Fashehe63aecb62007-10-18 15:30:42 -07006249 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006250 if (ret) {
6251 mlog_errno(ret);
6252 goto out_mutex;
6253 }
6254
6255 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6256 if (IS_ERR(handle)) {
6257 ret = PTR_ERR(handle);
6258 mlog_errno(ret);
6259 goto out_unlock;
6260 }
6261
6262 while (head) {
6263 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6264 head->free_bit);
6265 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6266 head->free_bit, (unsigned long long)head->free_blk);
6267
6268 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6269 head->free_bit, bg_blkno, 1);
6270 if (ret) {
6271 mlog_errno(ret);
6272 goto out_journal;
6273 }
6274
6275 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6276 if (ret) {
6277 mlog_errno(ret);
6278 goto out_journal;
6279 }
6280
6281 tmp = head;
6282 head = head->free_next;
6283 kfree(tmp);
6284 }
6285
6286out_journal:
6287 ocfs2_commit_trans(osb, handle);
6288
6289out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006290 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006291 brelse(di_bh);
6292out_mutex:
6293 mutex_unlock(&inode->i_mutex);
6294 iput(inode);
6295out:
6296 while(head) {
6297 /* Premature exit may have left some dangling items. */
6298 tmp = head;
6299 head = head->free_next;
6300 kfree(tmp);
6301 }
6302
6303 return ret;
6304}
6305
Tao Ma2891d292008-11-12 08:26:58 +08006306int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6307 u64 blkno, unsigned int bit)
6308{
6309 int ret = 0;
6310 struct ocfs2_cached_block_free *item;
6311
6312 item = kmalloc(sizeof(*item), GFP_NOFS);
6313 if (item == NULL) {
6314 ret = -ENOMEM;
6315 mlog_errno(ret);
6316 return ret;
6317 }
6318
6319 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6320 bit, (unsigned long long)blkno);
6321
6322 item->free_blk = blkno;
6323 item->free_bit = bit;
6324 item->free_next = ctxt->c_global_allocator;
6325
6326 ctxt->c_global_allocator = item;
6327 return ret;
6328}
6329
6330static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6331 struct ocfs2_cached_block_free *head)
6332{
6333 struct ocfs2_cached_block_free *tmp;
6334 struct inode *tl_inode = osb->osb_tl_inode;
6335 handle_t *handle;
6336 int ret = 0;
6337
6338 mutex_lock(&tl_inode->i_mutex);
6339
6340 while (head) {
6341 if (ocfs2_truncate_log_needs_flush(osb)) {
6342 ret = __ocfs2_flush_truncate_log(osb);
6343 if (ret < 0) {
6344 mlog_errno(ret);
6345 break;
6346 }
6347 }
6348
6349 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6350 if (IS_ERR(handle)) {
6351 ret = PTR_ERR(handle);
6352 mlog_errno(ret);
6353 break;
6354 }
6355
6356 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6357 head->free_bit);
6358
6359 ocfs2_commit_trans(osb, handle);
6360 tmp = head;
6361 head = head->free_next;
6362 kfree(tmp);
6363
6364 if (ret < 0) {
6365 mlog_errno(ret);
6366 break;
6367 }
6368 }
6369
6370 mutex_unlock(&tl_inode->i_mutex);
6371
6372 while (head) {
6373 /* Premature exit may have left some dangling items. */
6374 tmp = head;
6375 head = head->free_next;
6376 kfree(tmp);
6377 }
6378
6379 return ret;
6380}
6381
Mark Fasheh2b604352007-06-22 15:45:27 -07006382int ocfs2_run_deallocs(struct ocfs2_super *osb,
6383 struct ocfs2_cached_dealloc_ctxt *ctxt)
6384{
6385 int ret = 0, ret2;
6386 struct ocfs2_per_slot_free_list *fl;
6387
6388 if (!ctxt)
6389 return 0;
6390
6391 while (ctxt->c_first_suballocator) {
6392 fl = ctxt->c_first_suballocator;
6393
6394 if (fl->f_first) {
6395 mlog(0, "Free items: (type %u, slot %d)\n",
6396 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006397 ret2 = ocfs2_free_cached_blocks(osb,
6398 fl->f_inode_type,
6399 fl->f_slot,
6400 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006401 if (ret2)
6402 mlog_errno(ret2);
6403 if (!ret)
6404 ret = ret2;
6405 }
6406
6407 ctxt->c_first_suballocator = fl->f_next_suballocator;
6408 kfree(fl);
6409 }
6410
Tao Ma2891d292008-11-12 08:26:58 +08006411 if (ctxt->c_global_allocator) {
6412 ret2 = ocfs2_free_cached_clusters(osb,
6413 ctxt->c_global_allocator);
6414 if (ret2)
6415 mlog_errno(ret2);
6416 if (!ret)
6417 ret = ret2;
6418
6419 ctxt->c_global_allocator = NULL;
6420 }
6421
Mark Fasheh2b604352007-06-22 15:45:27 -07006422 return ret;
6423}
6424
6425static struct ocfs2_per_slot_free_list *
6426ocfs2_find_per_slot_free_list(int type,
6427 int slot,
6428 struct ocfs2_cached_dealloc_ctxt *ctxt)
6429{
6430 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6431
6432 while (fl) {
6433 if (fl->f_inode_type == type && fl->f_slot == slot)
6434 return fl;
6435
6436 fl = fl->f_next_suballocator;
6437 }
6438
6439 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6440 if (fl) {
6441 fl->f_inode_type = type;
6442 fl->f_slot = slot;
6443 fl->f_first = NULL;
6444 fl->f_next_suballocator = ctxt->c_first_suballocator;
6445
6446 ctxt->c_first_suballocator = fl;
6447 }
6448 return fl;
6449}
6450
6451static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6452 int type, int slot, u64 blkno,
6453 unsigned int bit)
6454{
6455 int ret;
6456 struct ocfs2_per_slot_free_list *fl;
6457 struct ocfs2_cached_block_free *item;
6458
6459 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6460 if (fl == NULL) {
6461 ret = -ENOMEM;
6462 mlog_errno(ret);
6463 goto out;
6464 }
6465
6466 item = kmalloc(sizeof(*item), GFP_NOFS);
6467 if (item == NULL) {
6468 ret = -ENOMEM;
6469 mlog_errno(ret);
6470 goto out;
6471 }
6472
6473 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6474 type, slot, bit, (unsigned long long)blkno);
6475
6476 item->free_blk = blkno;
6477 item->free_bit = bit;
6478 item->free_next = fl->f_first;
6479
6480 fl->f_first = item;
6481
6482 ret = 0;
6483out:
6484 return ret;
6485}
6486
Mark Fasheh59a5e412007-06-22 15:52:36 -07006487static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6488 struct ocfs2_extent_block *eb)
6489{
6490 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6491 le16_to_cpu(eb->h_suballoc_slot),
6492 le64_to_cpu(eb->h_blkno),
6493 le16_to_cpu(eb->h_suballoc_bit));
6494}
6495
Mark Fashehccd979b2005-12-15 14:31:24 -08006496/* This function will figure out whether the currently last extent
6497 * block will be deleted, and if it will, what the new last extent
6498 * block will be so we can update his h_next_leaf_blk field, as well
6499 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006500static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006501 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006502 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006503 struct buffer_head **new_last_eb)
6504{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006505 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006506 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006507 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006508 struct ocfs2_extent_block *eb;
6509 struct ocfs2_extent_list *el;
6510 struct buffer_head *bh = NULL;
6511
6512 *new_last_eb = NULL;
6513
Mark Fashehccd979b2005-12-15 14:31:24 -08006514 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006515 if (!path->p_tree_depth)
6516 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006517
6518 /* trunc to zero special case - this makes tree_depth = 0
6519 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006520 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006521 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006522
Mark Fashehdcd05382007-01-16 11:32:23 -08006523 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006524 BUG_ON(!el->l_next_free_rec);
6525
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006526 /*
6527 * Make sure that this extent list will actually be empty
6528 * after we clear away the data. We can shortcut out if
6529 * there's more than one non-empty extent in the
6530 * list. Otherwise, a check of the remaining extent is
6531 * necessary.
6532 */
6533 next_free = le16_to_cpu(el->l_next_free_rec);
6534 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006535 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006536 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006537 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006538
6539 /* We may have a valid extent in index 1, check it. */
6540 if (next_free == 2)
6541 rec = &el->l_recs[1];
6542
6543 /*
6544 * Fall through - no more nonempty extents, so we want
6545 * to delete this leaf.
6546 */
6547 } else {
6548 if (next_free > 1)
6549 goto out;
6550
6551 rec = &el->l_recs[0];
6552 }
6553
6554 if (rec) {
6555 /*
6556 * Check it we'll only be trimming off the end of this
6557 * cluster.
6558 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006559 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006560 goto out;
6561 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006562
Mark Fashehdcd05382007-01-16 11:32:23 -08006563 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6564 if (ret) {
6565 mlog_errno(ret);
6566 goto out;
6567 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006568
Joel Beckerfacdb772009-02-12 18:08:48 -08006569 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08006570 if (ret) {
6571 mlog_errno(ret);
6572 goto out;
6573 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006574
Mark Fashehdcd05382007-01-16 11:32:23 -08006575 eb = (struct ocfs2_extent_block *) bh->b_data;
6576 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006577
6578 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6579 * Any corruption is a code bug. */
6580 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006581
6582 *new_last_eb = bh;
6583 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006584 mlog(0, "returning block %llu, (cpos: %u)\n",
6585 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6586out:
6587 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006588
Mark Fashehdcd05382007-01-16 11:32:23 -08006589 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006590}
6591
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006592/*
6593 * Trim some clusters off the rightmost edge of a tree. Only called
6594 * during truncate.
6595 *
6596 * The caller needs to:
6597 * - start journaling of each path component.
6598 * - compute and fully set up any new last ext block
6599 */
6600static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6601 handle_t *handle, struct ocfs2_truncate_context *tc,
6602 u32 clusters_to_del, u64 *delete_start)
6603{
6604 int ret, i, index = path->p_tree_depth;
6605 u32 new_edge = 0;
6606 u64 deleted_eb = 0;
6607 struct buffer_head *bh;
6608 struct ocfs2_extent_list *el;
6609 struct ocfs2_extent_rec *rec;
6610
6611 *delete_start = 0;
6612
6613 while (index >= 0) {
6614 bh = path->p_node[index].bh;
6615 el = path->p_node[index].el;
6616
6617 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6618 index, (unsigned long long)bh->b_blocknr);
6619
6620 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6621
6622 if (index !=
6623 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6624 ocfs2_error(inode->i_sb,
6625 "Inode %lu has invalid ext. block %llu",
6626 inode->i_ino,
6627 (unsigned long long)bh->b_blocknr);
6628 ret = -EROFS;
6629 goto out;
6630 }
6631
6632find_tail_record:
6633 i = le16_to_cpu(el->l_next_free_rec) - 1;
6634 rec = &el->l_recs[i];
6635
6636 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6637 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006638 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006639 (unsigned long long)le64_to_cpu(rec->e_blkno),
6640 le16_to_cpu(el->l_next_free_rec));
6641
Mark Fashehe48edee2007-03-07 16:46:57 -08006642 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006643
6644 if (le16_to_cpu(el->l_tree_depth) == 0) {
6645 /*
6646 * If the leaf block contains a single empty
6647 * extent and no records, we can just remove
6648 * the block.
6649 */
6650 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6651 memset(rec, 0,
6652 sizeof(struct ocfs2_extent_rec));
6653 el->l_next_free_rec = cpu_to_le16(0);
6654
6655 goto delete;
6656 }
6657
6658 /*
6659 * Remove any empty extents by shifting things
6660 * left. That should make life much easier on
6661 * the code below. This condition is rare
6662 * enough that we shouldn't see a performance
6663 * hit.
6664 */
6665 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6666 le16_add_cpu(&el->l_next_free_rec, -1);
6667
6668 for(i = 0;
6669 i < le16_to_cpu(el->l_next_free_rec); i++)
6670 el->l_recs[i] = el->l_recs[i + 1];
6671
6672 memset(&el->l_recs[i], 0,
6673 sizeof(struct ocfs2_extent_rec));
6674
6675 /*
6676 * We've modified our extent list. The
6677 * simplest way to handle this change
6678 * is to being the search from the
6679 * start again.
6680 */
6681 goto find_tail_record;
6682 }
6683
Mark Fashehe48edee2007-03-07 16:46:57 -08006684 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006685
6686 /*
6687 * We'll use "new_edge" on our way back up the
6688 * tree to know what our rightmost cpos is.
6689 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006690 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006691 new_edge += le32_to_cpu(rec->e_cpos);
6692
6693 /*
6694 * The caller will use this to delete data blocks.
6695 */
6696 *delete_start = le64_to_cpu(rec->e_blkno)
6697 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006698 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006699
6700 /*
6701 * If it's now empty, remove this record.
6702 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006703 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006704 memset(rec, 0,
6705 sizeof(struct ocfs2_extent_rec));
6706 le16_add_cpu(&el->l_next_free_rec, -1);
6707 }
6708 } else {
6709 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6710 memset(rec, 0,
6711 sizeof(struct ocfs2_extent_rec));
6712 le16_add_cpu(&el->l_next_free_rec, -1);
6713
6714 goto delete;
6715 }
6716
6717 /* Can this actually happen? */
6718 if (le16_to_cpu(el->l_next_free_rec) == 0)
6719 goto delete;
6720
6721 /*
6722 * We never actually deleted any clusters
6723 * because our leaf was empty. There's no
6724 * reason to adjust the rightmost edge then.
6725 */
6726 if (new_edge == 0)
6727 goto delete;
6728
Mark Fashehe48edee2007-03-07 16:46:57 -08006729 rec->e_int_clusters = cpu_to_le32(new_edge);
6730 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006731 -le32_to_cpu(rec->e_cpos));
6732
6733 /*
6734 * A deleted child record should have been
6735 * caught above.
6736 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006737 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006738 }
6739
6740delete:
6741 ret = ocfs2_journal_dirty(handle, bh);
6742 if (ret) {
6743 mlog_errno(ret);
6744 goto out;
6745 }
6746
6747 mlog(0, "extent list container %llu, after: record %d: "
6748 "(%u, %u, %llu), next = %u.\n",
6749 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006750 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006751 (unsigned long long)le64_to_cpu(rec->e_blkno),
6752 le16_to_cpu(el->l_next_free_rec));
6753
6754 /*
6755 * We must be careful to only attempt delete of an
6756 * extent block (and not the root inode block).
6757 */
6758 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6759 struct ocfs2_extent_block *eb =
6760 (struct ocfs2_extent_block *)bh->b_data;
6761
6762 /*
6763 * Save this for use when processing the
6764 * parent block.
6765 */
6766 deleted_eb = le64_to_cpu(eb->h_blkno);
6767
6768 mlog(0, "deleting this extent block.\n");
6769
Joel Becker8cb471e2009-02-10 20:00:41 -08006770 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006771
Mark Fashehe48edee2007-03-07 16:46:57 -08006772 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006773 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6774 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6775
Mark Fasheh59a5e412007-06-22 15:52:36 -07006776 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6777 /* An error here is not fatal. */
6778 if (ret < 0)
6779 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006780 } else {
6781 deleted_eb = 0;
6782 }
6783
6784 index--;
6785 }
6786
6787 ret = 0;
6788out:
6789 return ret;
6790}
6791
Mark Fashehccd979b2005-12-15 14:31:24 -08006792static int ocfs2_do_truncate(struct ocfs2_super *osb,
6793 unsigned int clusters_to_del,
6794 struct inode *inode,
6795 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006796 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006797 struct ocfs2_truncate_context *tc,
6798 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006799{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006800 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006801 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006802 struct ocfs2_extent_block *last_eb = NULL;
6803 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006804 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006805 u64 delete_blk = 0;
6806
6807 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6808
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006809 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006810 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006811 if (status < 0) {
6812 mlog_errno(status);
6813 goto bail;
6814 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006815
Mark Fashehdcd05382007-01-16 11:32:23 -08006816 /*
6817 * Each component will be touched, so we might as well journal
6818 * here to avoid having to handle errors later.
6819 */
Joel Becker0cf2f762009-02-12 16:41:25 -08006820 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006821 if (status < 0) {
6822 mlog_errno(status);
6823 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006824 }
6825
6826 if (last_eb_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08006827 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07006828 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08006829 if (status < 0) {
6830 mlog_errno(status);
6831 goto bail;
6832 }
6833
6834 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6835 }
6836
6837 el = &(fe->id2.i_list);
6838
6839 /*
6840 * Lower levels depend on this never happening, but it's best
6841 * to check it up here before changing the tree.
6842 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006843 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006844 ocfs2_error(inode->i_sb,
6845 "Inode %lu has an empty extent record, depth %u\n",
6846 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006847 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006848 goto bail;
6849 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006850
Jan Karaa90714c2008-10-09 19:38:40 +02006851 vfs_dq_free_space_nodirty(inode,
6852 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006853 spin_lock(&OCFS2_I(inode)->ip_lock);
6854 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6855 clusters_to_del;
6856 spin_unlock(&OCFS2_I(inode)->ip_lock);
6857 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006858 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006859
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006860 status = ocfs2_trim_tree(inode, path, handle, tc,
6861 clusters_to_del, &delete_blk);
6862 if (status) {
6863 mlog_errno(status);
6864 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006865 }
6866
Mark Fashehdcd05382007-01-16 11:32:23 -08006867 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006868 /* trunc to zero is a special case. */
6869 el->l_tree_depth = 0;
6870 fe->i_last_eb_blk = 0;
6871 } else if (last_eb)
6872 fe->i_last_eb_blk = last_eb->h_blkno;
6873
6874 status = ocfs2_journal_dirty(handle, fe_bh);
6875 if (status < 0) {
6876 mlog_errno(status);
6877 goto bail;
6878 }
6879
6880 if (last_eb) {
6881 /* If there will be a new last extent block, then by
6882 * definition, there cannot be any leaves to the right of
6883 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006884 last_eb->h_next_leaf_blk = 0;
6885 status = ocfs2_journal_dirty(handle, last_eb_bh);
6886 if (status < 0) {
6887 mlog_errno(status);
6888 goto bail;
6889 }
6890 }
6891
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006892 if (delete_blk) {
6893 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6894 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006895 if (status < 0) {
6896 mlog_errno(status);
6897 goto bail;
6898 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006899 }
6900 status = 0;
6901bail:
Tao Ma60e2ec42009-08-12 14:42:47 +08006902 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006903 mlog_exit(status);
6904 return status;
6905}
6906
Joel Becker2b4e30f2008-09-03 20:03:41 -07006907static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006908{
6909 set_buffer_uptodate(bh);
6910 mark_buffer_dirty(bh);
6911 return 0;
6912}
6913
Mark Fasheh1d410a62007-09-07 14:20:45 -07006914static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6915 unsigned int from, unsigned int to,
6916 struct page *page, int zero, u64 *phys)
6917{
6918 int ret, partial = 0;
6919
6920 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6921 if (ret)
6922 mlog_errno(ret);
6923
6924 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006925 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006926
6927 /*
6928 * Need to set the buffers we zero'd into uptodate
6929 * here if they aren't - ocfs2_map_page_blocks()
6930 * might've skipped some
6931 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006932 ret = walk_page_buffers(handle, page_buffers(page),
6933 from, to, &partial,
6934 ocfs2_zero_func);
6935 if (ret < 0)
6936 mlog_errno(ret);
6937 else if (ocfs2_should_order_data(inode)) {
6938 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006939 if (ret < 0)
6940 mlog_errno(ret);
6941 }
6942
6943 if (!partial)
6944 SetPageUptodate(page);
6945
6946 flush_dcache_page(page);
6947}
6948
Mark Fasheh35edec12007-07-06 14:41:18 -07006949static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6950 loff_t end, struct page **pages,
6951 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006952{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006953 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006954 struct page *page;
6955 unsigned int from, to = PAGE_CACHE_SIZE;
6956 struct super_block *sb = inode->i_sb;
6957
6958 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6959
6960 if (numpages == 0)
6961 goto out;
6962
Mark Fasheh35edec12007-07-06 14:41:18 -07006963 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006964 for(i = 0; i < numpages; i++) {
6965 page = pages[i];
6966
Mark Fasheh35edec12007-07-06 14:41:18 -07006967 from = start & (PAGE_CACHE_SIZE - 1);
6968 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6969 to = end & (PAGE_CACHE_SIZE - 1);
6970
Mark Fasheh60b11392007-02-16 11:46:50 -08006971 BUG_ON(from > PAGE_CACHE_SIZE);
6972 BUG_ON(to > PAGE_CACHE_SIZE);
6973
Mark Fasheh1d410a62007-09-07 14:20:45 -07006974 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6975 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006976
Mark Fasheh35edec12007-07-06 14:41:18 -07006977 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006978 }
6979out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006980 if (pages)
6981 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006982}
6983
Mark Fasheh35edec12007-07-06 14:41:18 -07006984static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07006985 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006986{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006987 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006988 struct super_block *sb = inode->i_sb;
6989 struct address_space *mapping = inode->i_mapping;
6990 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006991 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006992
Mark Fasheh35edec12007-07-06 14:41:18 -07006993 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006994
Mark Fasheh35edec12007-07-06 14:41:18 -07006995 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6996 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6997
Mark Fasheh1d410a62007-09-07 14:20:45 -07006998 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006999 last_page_bytes = PAGE_ALIGN(end);
7000 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08007001 do {
7002 pages[numpages] = grab_cache_page(mapping, index);
7003 if (!pages[numpages]) {
7004 ret = -ENOMEM;
7005 mlog_errno(ret);
7006 goto out;
7007 }
7008
7009 numpages++;
7010 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07007011 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08007012
7013out:
7014 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07007015 if (pages)
7016 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08007017 numpages = 0;
7018 }
7019
7020 *num = numpages;
7021
7022 return ret;
7023}
7024
7025/*
7026 * Zero the area past i_size but still within an allocated
7027 * cluster. This avoids exposing nonzero data on subsequent file
7028 * extends.
7029 *
7030 * We need to call this before i_size is updated on the inode because
7031 * otherwise block_write_full_page() will skip writeout of pages past
7032 * i_size. The new_i_size parameter is passed for this reason.
7033 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007034int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
7035 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08007036{
Mark Fasheh1d410a62007-09-07 14:20:45 -07007037 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08007038 struct page **pages = NULL;
7039 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07007040 unsigned int ext_flags;
7041 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08007042
7043 /*
7044 * File systems which don't support sparse files zero on every
7045 * extend.
7046 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07007047 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08007048 return 0;
7049
Mark Fasheh1d410a62007-09-07 14:20:45 -07007050 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08007051 sizeof(struct page *), GFP_NOFS);
7052 if (pages == NULL) {
7053 ret = -ENOMEM;
7054 mlog_errno(ret);
7055 goto out;
7056 }
7057
Mark Fasheh1d410a62007-09-07 14:20:45 -07007058 if (range_start == range_end)
7059 goto out;
7060
7061 ret = ocfs2_extent_map_get_blocks(inode,
7062 range_start >> sb->s_blocksize_bits,
7063 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08007064 if (ret) {
7065 mlog_errno(ret);
7066 goto out;
7067 }
7068
Mark Fasheh1d410a62007-09-07 14:20:45 -07007069 /*
7070 * Tail is a hole, or is marked unwritten. In either case, we
7071 * can count on read and write to return/push zero's.
7072 */
7073 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08007074 goto out;
7075
Mark Fasheh1d410a62007-09-07 14:20:45 -07007076 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7077 &numpages);
7078 if (ret) {
7079 mlog_errno(ret);
7080 goto out;
7081 }
7082
Mark Fasheh35edec12007-07-06 14:41:18 -07007083 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7084 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08007085
7086 /*
7087 * Initiate writeout of the pages we zero'd here. We don't
7088 * wait on them - the truncate_inode_pages() call later will
7089 * do that for us.
7090 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007091 ret = do_sync_mapping_range(inode->i_mapping, range_start,
7092 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08007093 if (ret)
7094 mlog_errno(ret);
7095
7096out:
7097 if (pages)
7098 kfree(pages);
7099
7100 return ret;
7101}
7102
Tiger Yangfdd77702008-08-18 17:08:55 +08007103static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7104 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007105{
7106 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08007107 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007108
Tiger Yangfdd77702008-08-18 17:08:55 +08007109 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7110 memset(&di->id2, 0, blocksize -
7111 offsetof(struct ocfs2_dinode, id2) -
7112 xattrsize);
7113 else
7114 memset(&di->id2, 0, blocksize -
7115 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007116}
7117
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007118void ocfs2_dinode_new_extent_list(struct inode *inode,
7119 struct ocfs2_dinode *di)
7120{
Tiger Yangfdd77702008-08-18 17:08:55 +08007121 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007122 di->id2.i_list.l_tree_depth = 0;
7123 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08007124 di->id2.i_list.l_count = cpu_to_le16(
7125 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007126}
7127
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007128void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7129{
7130 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7131 struct ocfs2_inline_data *idata = &di->id2.i_data;
7132
7133 spin_lock(&oi->ip_lock);
7134 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7135 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7136 spin_unlock(&oi->ip_lock);
7137
7138 /*
7139 * We clear the entire i_data structure here so that all
7140 * fields can be properly initialized.
7141 */
Tiger Yangfdd77702008-08-18 17:08:55 +08007142 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007143
Tiger Yangfdd77702008-08-18 17:08:55 +08007144 idata->id_count = cpu_to_le16(
7145 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007146}
7147
7148int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7149 struct buffer_head *di_bh)
7150{
7151 int ret, i, has_data, num_pages = 0;
7152 handle_t *handle;
7153 u64 uninitialized_var(block);
7154 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7155 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7156 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007157 struct ocfs2_alloc_context *data_ac = NULL;
7158 struct page **pages = NULL;
7159 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07007160 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02007161 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007162
7163 has_data = i_size_read(inode) ? 1 : 0;
7164
7165 if (has_data) {
7166 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7167 sizeof(struct page *), GFP_NOFS);
7168 if (pages == NULL) {
7169 ret = -ENOMEM;
7170 mlog_errno(ret);
7171 goto out;
7172 }
7173
7174 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7175 if (ret) {
7176 mlog_errno(ret);
7177 goto out;
7178 }
7179 }
7180
Jan Karaa90714c2008-10-09 19:38:40 +02007181 handle = ocfs2_start_trans(osb,
7182 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007183 if (IS_ERR(handle)) {
7184 ret = PTR_ERR(handle);
7185 mlog_errno(ret);
7186 goto out_unlock;
7187 }
7188
Joel Becker0cf2f762009-02-12 16:41:25 -08007189 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007190 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007191 if (ret) {
7192 mlog_errno(ret);
7193 goto out_commit;
7194 }
7195
7196 if (has_data) {
7197 u32 bit_off, num;
7198 unsigned int page_end;
7199 u64 phys;
7200
Jan Karaa90714c2008-10-09 19:38:40 +02007201 if (vfs_dq_alloc_space_nodirty(inode,
7202 ocfs2_clusters_to_bytes(osb->sb, 1))) {
7203 ret = -EDQUOT;
7204 goto out_commit;
7205 }
7206 did_quota = 1;
7207
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007208 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7209 &num);
7210 if (ret) {
7211 mlog_errno(ret);
7212 goto out_commit;
7213 }
7214
7215 /*
7216 * Save two copies, one for insert, and one that can
7217 * be changed by ocfs2_map_and_dirty_page() below.
7218 */
7219 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7220
7221 /*
7222 * Non sparse file systems zero on extend, so no need
7223 * to do that now.
7224 */
7225 if (!ocfs2_sparse_alloc(osb) &&
7226 PAGE_CACHE_SIZE < osb->s_clustersize)
7227 end = PAGE_CACHE_SIZE;
7228
7229 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7230 if (ret) {
7231 mlog_errno(ret);
7232 goto out_commit;
7233 }
7234
7235 /*
7236 * This should populate the 1st page for us and mark
7237 * it up to date.
7238 */
7239 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7240 if (ret) {
7241 mlog_errno(ret);
7242 goto out_commit;
7243 }
7244
7245 page_end = PAGE_CACHE_SIZE;
7246 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7247 page_end = osb->s_clustersize;
7248
7249 for (i = 0; i < num_pages; i++)
7250 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7251 pages[i], i > 0, &phys);
7252 }
7253
7254 spin_lock(&oi->ip_lock);
7255 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7256 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7257 spin_unlock(&oi->ip_lock);
7258
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007259 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007260
7261 ocfs2_journal_dirty(handle, di_bh);
7262
7263 if (has_data) {
7264 /*
7265 * An error at this point should be extremely rare. If
7266 * this proves to be false, we could always re-build
7267 * the in-inode data from our pages.
7268 */
Joel Becker5e404e92009-02-13 03:54:22 -08007269 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
Joel Beckercc79d8c2009-02-13 03:24:43 -08007270 ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007271 if (ret) {
7272 mlog_errno(ret);
7273 goto out_commit;
7274 }
7275
7276 inode->i_blocks = ocfs2_inode_sector_count(inode);
7277 }
7278
7279out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007280 if (ret < 0 && did_quota)
7281 vfs_dq_free_space_nodirty(inode,
7282 ocfs2_clusters_to_bytes(osb->sb, 1));
7283
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007284 ocfs2_commit_trans(osb, handle);
7285
7286out_unlock:
7287 if (data_ac)
7288 ocfs2_free_alloc_context(data_ac);
7289
7290out:
7291 if (pages) {
7292 ocfs2_unlock_and_free_pages(pages, num_pages);
7293 kfree(pages);
7294 }
7295
7296 return ret;
7297}
7298
Mark Fashehccd979b2005-12-15 14:31:24 -08007299/*
7300 * It is expected, that by the time you call this function,
7301 * inode->i_size and fe->i_size have been adjusted.
7302 *
7303 * WARNING: This will kfree the truncate context
7304 */
7305int ocfs2_commit_truncate(struct ocfs2_super *osb,
7306 struct inode *inode,
7307 struct buffer_head *fe_bh,
7308 struct ocfs2_truncate_context *tc)
7309{
7310 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08007311 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08007312 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07007313 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007314 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08007315 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08007316 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007317
7318 mlog_entry_void();
7319
Mark Fashehdcd05382007-01-16 11:32:23 -08007320 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007321 i_size_read(inode));
7322
Joel Becker13723d02008-10-17 19:25:01 -07007323 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7324 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08007325 if (!path) {
7326 status = -ENOMEM;
7327 mlog_errno(status);
7328 goto bail;
7329 }
Mark Fasheh83418972007-04-23 18:53:12 -07007330
7331 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7332
Mark Fashehccd979b2005-12-15 14:31:24 -08007333start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007334 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007335 * Check that we still have allocation to delete.
7336 */
7337 if (OCFS2_I(inode)->ip_clusters == 0) {
7338 status = 0;
7339 goto bail;
7340 }
7341
7342 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007343 * Truncate always works against the rightmost tree branch.
7344 */
Joel Beckerfacdb772009-02-12 18:08:48 -08007345 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
Mark Fashehdcd05382007-01-16 11:32:23 -08007346 if (status) {
7347 mlog_errno(status);
7348 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007349 }
7350
Mark Fashehdcd05382007-01-16 11:32:23 -08007351 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7352 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7353
7354 /*
7355 * By now, el will point to the extent list on the bottom most
7356 * portion of this tree. Only the tail record is considered in
7357 * each pass.
7358 *
7359 * We handle the following cases, in order:
7360 * - empty extent: delete the remaining branch
7361 * - remove the entire record
7362 * - remove a partial record
7363 * - no record needs to be removed (truncate has completed)
7364 */
7365 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007366 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7367 ocfs2_error(inode->i_sb,
7368 "Inode %llu has empty extent block at %llu\n",
7369 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7370 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7371 status = -EROFS;
7372 goto bail;
7373 }
7374
Mark Fashehccd979b2005-12-15 14:31:24 -08007375 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08007376 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08007377 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007378 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7379 clusters_to_del = 0;
7380 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007381 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007382 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007383 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08007384 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08007385 new_highest_cpos;
7386 } else {
7387 status = 0;
7388 goto bail;
7389 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007390
Mark Fashehdcd05382007-01-16 11:32:23 -08007391 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7392 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7393
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007394 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007395 tl_sem = 1;
7396 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7397 * record is free for use. If there isn't any, we flush to get
7398 * an empty truncate log. */
7399 if (ocfs2_truncate_log_needs_flush(osb)) {
7400 status = __ocfs2_flush_truncate_log(osb);
7401 if (status < 0) {
7402 mlog_errno(status);
7403 goto bail;
7404 }
7405 }
7406
7407 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007408 (struct ocfs2_dinode *)fe_bh->b_data,
7409 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007410 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007411 if (IS_ERR(handle)) {
7412 status = PTR_ERR(handle);
7413 handle = NULL;
7414 mlog_errno(status);
7415 goto bail;
7416 }
7417
Mark Fashehdcd05382007-01-16 11:32:23 -08007418 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7419 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007420 if (status < 0) {
7421 mlog_errno(status);
7422 goto bail;
7423 }
7424
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007425 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007426 tl_sem = 0;
7427
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007428 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007429 handle = NULL;
7430
Mark Fashehdcd05382007-01-16 11:32:23 -08007431 ocfs2_reinit_path(path, 1);
7432
7433 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007434 * The check above will catch the case where we've truncated
7435 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007436 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007437 goto start;
7438
Mark Fashehccd979b2005-12-15 14:31:24 -08007439bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007440
7441 ocfs2_schedule_truncate_log_flush(osb, 1);
7442
7443 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007444 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007445
7446 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007447 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007448
Mark Fasheh59a5e412007-06-22 15:52:36 -07007449 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7450
Mark Fashehdcd05382007-01-16 11:32:23 -08007451 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007452
7453 /* This will drop the ext_alloc cluster lock for us */
7454 ocfs2_free_truncate_context(tc);
7455
7456 mlog_exit(status);
7457 return status;
7458}
7459
Mark Fashehccd979b2005-12-15 14:31:24 -08007460/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007461 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007462 */
7463int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7464 struct inode *inode,
7465 struct buffer_head *fe_bh,
7466 struct ocfs2_truncate_context **tc)
7467{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007468 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007469 unsigned int new_i_clusters;
7470 struct ocfs2_dinode *fe;
7471 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007472 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007473
7474 mlog_entry_void();
7475
7476 *tc = NULL;
7477
7478 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7479 i_size_read(inode));
7480 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7481
7482 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007483 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7484 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007485
Robert P. J. Daycd861282006-12-13 00:34:52 -08007486 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007487 if (!(*tc)) {
7488 status = -ENOMEM;
7489 mlog_errno(status);
7490 goto bail;
7491 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007492 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007493
Mark Fashehccd979b2005-12-15 14:31:24 -08007494 if (fe->id2.i_list.l_tree_depth) {
Joel Becker3d03a302009-02-12 17:49:26 -08007495 status = ocfs2_read_extent_block(INODE_CACHE(inode),
Joel Becker5e965812008-11-13 14:49:16 -08007496 le64_to_cpu(fe->i_last_eb_blk),
7497 &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007498 if (status < 0) {
7499 mlog_errno(status);
7500 goto bail;
7501 }
7502 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007503 }
7504
7505 (*tc)->tc_last_eb_bh = last_eb_bh;
7506
Mark Fashehccd979b2005-12-15 14:31:24 -08007507 status = 0;
7508bail:
7509 if (status < 0) {
7510 if (*tc)
7511 ocfs2_free_truncate_context(*tc);
7512 *tc = NULL;
7513 }
7514 mlog_exit_void();
7515 return status;
7516}
7517
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007518/*
7519 * 'start' is inclusive, 'end' is not.
7520 */
7521int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7522 unsigned int start, unsigned int end, int trunc)
7523{
7524 int ret;
7525 unsigned int numbytes;
7526 handle_t *handle;
7527 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7528 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7529 struct ocfs2_inline_data *idata = &di->id2.i_data;
7530
7531 if (end > i_size_read(inode))
7532 end = i_size_read(inode);
7533
7534 BUG_ON(start >= end);
7535
7536 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7537 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7538 !ocfs2_supports_inline_data(osb)) {
7539 ocfs2_error(inode->i_sb,
7540 "Inline data flags for inode %llu don't agree! "
7541 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7542 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7543 le16_to_cpu(di->i_dyn_features),
7544 OCFS2_I(inode)->ip_dyn_features,
7545 osb->s_feature_incompat);
7546 ret = -EROFS;
7547 goto out;
7548 }
7549
7550 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7551 if (IS_ERR(handle)) {
7552 ret = PTR_ERR(handle);
7553 mlog_errno(ret);
7554 goto out;
7555 }
7556
Joel Becker0cf2f762009-02-12 16:41:25 -08007557 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007558 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007559 if (ret) {
7560 mlog_errno(ret);
7561 goto out_commit;
7562 }
7563
7564 numbytes = end - start;
7565 memset(idata->id_data + start, 0, numbytes);
7566
7567 /*
7568 * No need to worry about the data page here - it's been
7569 * truncated already and inline data doesn't need it for
7570 * pushing zero's to disk, so we'll let readpage pick it up
7571 * later.
7572 */
7573 if (trunc) {
7574 i_size_write(inode, start);
7575 di->i_size = cpu_to_le64(start);
7576 }
7577
7578 inode->i_blocks = ocfs2_inode_sector_count(inode);
7579 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7580
7581 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7582 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7583
7584 ocfs2_journal_dirty(handle, di_bh);
7585
7586out_commit:
7587 ocfs2_commit_trans(osb, handle);
7588
7589out:
7590 return ret;
7591}
7592
Mark Fashehccd979b2005-12-15 14:31:24 -08007593static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7594{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007595 /*
7596 * The caller is responsible for completing deallocation
7597 * before freeing the context.
7598 */
7599 if (tc->tc_dealloc.c_first_suballocator != NULL)
7600 mlog(ML_NOTICE,
7601 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007602
Mark Fasheha81cb882008-10-07 14:25:16 -07007603 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007604
7605 kfree(tc);
7606}