blob: d17bdc718f74b3820f0381ed770e85a86217c5e0 [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"
Tao Mabcbbb242009-08-18 11:29:12 +080052#include "refcounttree.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080053
54#include "buffer_head_io.h"
55
Tao Ma853a3a12009-08-18 11:22:18 +080056enum ocfs2_contig_type {
57 CONTIG_NONE = 0,
58 CONTIG_LEFT,
59 CONTIG_RIGHT,
60 CONTIG_LEFTRIGHT,
61};
Tao Mae7d4cb62008-08-18 17:38:44 +080062
Tao Ma853a3a12009-08-18 11:22:18 +080063static enum ocfs2_contig_type
64 ocfs2_extent_rec_contig(struct super_block *sb,
65 struct ocfs2_extent_rec *ext,
66 struct ocfs2_extent_rec *insert_rec);
Joel Becker1625f8a2008-08-21 17:11:10 -070067/*
68 * Operations for a specific extent tree type.
69 *
70 * To implement an on-disk btree (extent tree) type in ocfs2, add
71 * an ocfs2_extent_tree_operations structure and the matching
Joel Becker8d6220d2008-08-22 12:46:09 -070072 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
Joel Becker1625f8a2008-08-21 17:11:10 -070073 * for the allocation portion of the extent tree.
74 */
Tao Mae7d4cb62008-08-18 17:38:44 +080075struct ocfs2_extent_tree_operations {
Joel Becker1625f8a2008-08-21 17:11:10 -070076 /*
77 * last_eb_blk is the block number of the right most leaf extent
78 * block. Most on-disk structures containing an extent tree store
79 * this value for fast access. The ->eo_set_last_eb_blk() and
80 * ->eo_get_last_eb_blk() operations access this value. They are
81 * both required.
82 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070083 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
84 u64 blkno);
85 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -070086
87 /*
88 * The on-disk structure usually keeps track of how many total
89 * clusters are stored in this extent tree. This function updates
90 * that value. new_clusters is the delta, and must be
91 * added to the total. Required.
92 */
Joel Becker6136ca52009-02-12 19:32:43 -080093 void (*eo_update_clusters)(struct ocfs2_extent_tree *et,
Joel Becker35dc0aa2008-08-20 16:25:06 -070094 u32 new_clusters);
Joel Becker1625f8a2008-08-21 17:11:10 -070095
96 /*
Joel Becker92ba4702009-02-13 03:18:34 -080097 * If this extent tree is supported by an extent map, insert
98 * a record into the map.
99 */
100 void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et,
101 struct ocfs2_extent_rec *rec);
102
103 /*
Joel Becker4c911ee2009-02-13 02:50:12 -0800104 * If this extent tree is supported by an extent map, truncate the
105 * map to clusters,
106 */
107 void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et,
108 u32 clusters);
109
110 /*
Joel Becker1625f8a2008-08-21 17:11:10 -0700111 * If ->eo_insert_check() exists, it is called before rec is
112 * inserted into the extent tree. It is optional.
113 */
Joel Becker6136ca52009-02-12 19:32:43 -0800114 int (*eo_insert_check)(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700115 struct ocfs2_extent_rec *rec);
Joel Becker6136ca52009-02-12 19:32:43 -0800116 int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
Joel Becker0ce10102008-08-20 17:19:50 -0700117
Joel Becker1625f8a2008-08-21 17:11:10 -0700118 /*
119 * --------------------------------------------------------------
120 * The remaining are internal to ocfs2_extent_tree and don't have
121 * accessor functions
122 */
123
124 /*
125 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
126 * It is required.
127 */
Joel Becker0ce10102008-08-20 17:19:50 -0700128 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -0700129
130 /*
131 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
132 * it exists. If it does not, et->et_max_leaf_clusters is set
133 * to 0 (unlimited). Optional.
134 */
Joel Becker6136ca52009-02-12 19:32:43 -0800135 void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
Tao Ma853a3a12009-08-18 11:22:18 +0800136
137 /*
138 * ->eo_extent_contig test whether the 2 ocfs2_extent_rec
139 * are contiguous or not. Optional. Don't need to set it if use
140 * ocfs2_extent_rec as the tree leaf.
141 */
142 enum ocfs2_contig_type
143 (*eo_extent_contig)(struct ocfs2_extent_tree *et,
144 struct ocfs2_extent_rec *ext,
145 struct ocfs2_extent_rec *insert_rec);
Tao Mae7d4cb62008-08-18 17:38:44 +0800146};
147
Joel Beckerf99b9b72008-08-20 19:36:33 -0700148
149/*
150 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
151 * in the methods.
152 */
153static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
154static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
155 u64 blkno);
Joel Becker6136ca52009-02-12 19:32:43 -0800156static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700157 u32 clusters);
Joel Becker92ba4702009-02-13 03:18:34 -0800158static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
159 struct ocfs2_extent_rec *rec);
Joel Becker4c911ee2009-02-13 02:50:12 -0800160static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
161 u32 clusters);
Joel Becker6136ca52009-02-12 19:32:43 -0800162static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700163 struct ocfs2_extent_rec *rec);
Joel Becker6136ca52009-02-12 19:32:43 -0800164static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700165static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
166static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
167 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
168 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
169 .eo_update_clusters = ocfs2_dinode_update_clusters,
Joel Becker92ba4702009-02-13 03:18:34 -0800170 .eo_extent_map_insert = ocfs2_dinode_extent_map_insert,
Joel Becker4c911ee2009-02-13 02:50:12 -0800171 .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700172 .eo_insert_check = ocfs2_dinode_insert_check,
173 .eo_sanity_check = ocfs2_dinode_sanity_check,
174 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
Tao Mae7d4cb62008-08-18 17:38:44 +0800175};
176
177static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
178 u64 blkno)
179{
Joel Beckerea5efa12008-08-20 16:57:27 -0700180 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800181
Joel Beckerf99b9b72008-08-20 19:36:33 -0700182 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800183 di->i_last_eb_blk = cpu_to_le64(blkno);
184}
185
186static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
187{
Joel Beckerea5efa12008-08-20 16:57:27 -0700188 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800189
Joel Beckerf99b9b72008-08-20 19:36:33 -0700190 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800191 return le64_to_cpu(di->i_last_eb_blk);
192}
193
Joel Becker6136ca52009-02-12 19:32:43 -0800194static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +0800195 u32 clusters)
196{
Joel Becker6136ca52009-02-12 19:32:43 -0800197 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
Joel Beckerea5efa12008-08-20 16:57:27 -0700198 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800199
200 le32_add_cpu(&di->i_clusters, clusters);
Joel Becker6136ca52009-02-12 19:32:43 -0800201 spin_lock(&oi->ip_lock);
202 oi->ip_clusters = le32_to_cpu(di->i_clusters);
203 spin_unlock(&oi->ip_lock);
Tao Mae7d4cb62008-08-18 17:38:44 +0800204}
205
Joel Becker92ba4702009-02-13 03:18:34 -0800206static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
207 struct ocfs2_extent_rec *rec)
208{
209 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
210
211 ocfs2_extent_map_insert_rec(inode, rec);
212}
213
Joel Becker4c911ee2009-02-13 02:50:12 -0800214static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
215 u32 clusters)
216{
217 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
218
219 ocfs2_extent_map_trunc(inode, clusters);
220}
221
Joel Becker6136ca52009-02-12 19:32:43 -0800222static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700223 struct ocfs2_extent_rec *rec)
224{
Joel Becker6136ca52009-02-12 19:32:43 -0800225 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
226 struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
Joel Becker1e61ee72008-08-20 18:32:45 -0700227
Joel Becker6136ca52009-02-12 19:32:43 -0800228 BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
Joel Becker1e61ee72008-08-20 18:32:45 -0700229 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
Joel Becker6136ca52009-02-12 19:32:43 -0800230 (oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
Joel Becker1e61ee72008-08-20 18:32:45 -0700231 "Device %s, asking for sparse allocation: inode %llu, "
232 "cpos %u, clusters %u\n",
233 osb->dev_str,
Joel Becker6136ca52009-02-12 19:32:43 -0800234 (unsigned long long)oi->ip_blkno,
235 rec->e_cpos, oi->ip_clusters);
Joel Becker1e61ee72008-08-20 18:32:45 -0700236
237 return 0;
238}
239
Joel Becker6136ca52009-02-12 19:32:43 -0800240static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800241{
Joel Becker10995aa2008-11-13 14:49:12 -0800242 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800243
Joel Beckerf99b9b72008-08-20 19:36:33 -0700244 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Joel Becker10995aa2008-11-13 14:49:12 -0800245 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
Tao Mae7d4cb62008-08-18 17:38:44 +0800246
Joel Becker10995aa2008-11-13 14:49:12 -0800247 return 0;
Tao Mae7d4cb62008-08-18 17:38:44 +0800248}
249
Joel Beckerf99b9b72008-08-20 19:36:33 -0700250static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
251{
252 struct ocfs2_dinode *di = et->et_object;
253
254 et->et_root_el = &di->id2.i_list;
255}
256
Tao Mae7d4cb62008-08-18 17:38:44 +0800257
Joel Becker0ce10102008-08-20 17:19:50 -0700258static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
259{
Joel Becker2a50a742008-12-09 14:24:33 -0800260 struct ocfs2_xattr_value_buf *vb = et->et_object;
Joel Becker0ce10102008-08-20 17:19:50 -0700261
Joel Becker2a50a742008-12-09 14:24:33 -0800262 et->et_root_el = &vb->vb_xv->xr_list;
Joel Becker0ce10102008-08-20 17:19:50 -0700263}
264
Tao Maf56654c2008-08-18 17:38:48 +0800265static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
266 u64 blkno)
267{
Joel Becker2a50a742008-12-09 14:24:33 -0800268 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800269
Joel Becker2a50a742008-12-09 14:24:33 -0800270 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
Tao Maf56654c2008-08-18 17:38:48 +0800271}
272
273static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
274{
Joel Becker2a50a742008-12-09 14:24:33 -0800275 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800276
Joel Becker2a50a742008-12-09 14:24:33 -0800277 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
Tao Maf56654c2008-08-18 17:38:48 +0800278}
279
Joel Becker6136ca52009-02-12 19:32:43 -0800280static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
Tao Maf56654c2008-08-18 17:38:48 +0800281 u32 clusters)
282{
Joel Becker2a50a742008-12-09 14:24:33 -0800283 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800284
Joel Becker2a50a742008-12-09 14:24:33 -0800285 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800286}
287
Joel Becker1a09f552008-08-20 17:44:24 -0700288static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700289 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
290 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
291 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700292 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800293};
294
Joel Becker0ce10102008-08-20 17:19:50 -0700295static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
296{
297 struct ocfs2_xattr_block *xb = et->et_object;
298
299 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
300}
301
Joel Becker6136ca52009-02-12 19:32:43 -0800302static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
Joel Becker943cced2008-08-20 17:31:10 -0700303{
Joel Becker6136ca52009-02-12 19:32:43 -0800304 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Joel Becker943cced2008-08-20 17:31:10 -0700305 et->et_max_leaf_clusters =
Joel Becker6136ca52009-02-12 19:32:43 -0800306 ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
Joel Becker943cced2008-08-20 17:31:10 -0700307}
308
Tao Maba492612008-08-18 17:38:49 +0800309static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
310 u64 blkno)
311{
Joel Beckerea5efa12008-08-20 16:57:27 -0700312 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800313 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
314
315 xt->xt_last_eb_blk = cpu_to_le64(blkno);
316}
317
318static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
319{
Joel Beckerea5efa12008-08-20 16:57:27 -0700320 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800321 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
322
323 return le64_to_cpu(xt->xt_last_eb_blk);
324}
325
Joel Becker6136ca52009-02-12 19:32:43 -0800326static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
Tao Maba492612008-08-18 17:38:49 +0800327 u32 clusters)
328{
Joel Beckerea5efa12008-08-20 16:57:27 -0700329 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800330
331 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
332}
333
Tao Maba492612008-08-18 17:38:49 +0800334static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700335 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
336 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
337 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700338 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
Joel Becker943cced2008-08-20 17:31:10 -0700339 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
Tao Maba492612008-08-18 17:38:49 +0800340};
341
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800342static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
343 u64 blkno)
344{
345 struct ocfs2_dx_root_block *dx_root = et->et_object;
346
347 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
348}
349
350static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
351{
352 struct ocfs2_dx_root_block *dx_root = et->et_object;
353
354 return le64_to_cpu(dx_root->dr_last_eb_blk);
355}
356
Joel Becker6136ca52009-02-12 19:32:43 -0800357static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800358 u32 clusters)
359{
360 struct ocfs2_dx_root_block *dx_root = et->et_object;
361
362 le32_add_cpu(&dx_root->dr_clusters, clusters);
363}
364
Joel Becker6136ca52009-02-12 19:32:43 -0800365static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800366{
367 struct ocfs2_dx_root_block *dx_root = et->et_object;
368
369 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
370
371 return 0;
372}
373
374static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
375{
376 struct ocfs2_dx_root_block *dx_root = et->et_object;
377
378 et->et_root_el = &dx_root->dr_list;
379}
380
381static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
382 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
383 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
384 .eo_update_clusters = ocfs2_dx_root_update_clusters,
385 .eo_sanity_check = ocfs2_dx_root_sanity_check,
386 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
387};
388
Tao Mafe924412009-08-18 11:22:25 +0800389static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et)
390{
391 struct ocfs2_refcount_block *rb = et->et_object;
392
393 et->et_root_el = &rb->rf_list;
394}
395
396static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
397 u64 blkno)
398{
399 struct ocfs2_refcount_block *rb = et->et_object;
400
401 rb->rf_last_eb_blk = cpu_to_le64(blkno);
402}
403
404static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
405{
406 struct ocfs2_refcount_block *rb = et->et_object;
407
408 return le64_to_cpu(rb->rf_last_eb_blk);
409}
410
411static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et,
412 u32 clusters)
413{
414 struct ocfs2_refcount_block *rb = et->et_object;
415
416 le32_add_cpu(&rb->rf_clusters, clusters);
417}
418
419static enum ocfs2_contig_type
420ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et,
421 struct ocfs2_extent_rec *ext,
422 struct ocfs2_extent_rec *insert_rec)
423{
424 return CONTIG_NONE;
425}
426
427static struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = {
428 .eo_set_last_eb_blk = ocfs2_refcount_tree_set_last_eb_blk,
429 .eo_get_last_eb_blk = ocfs2_refcount_tree_get_last_eb_blk,
430 .eo_update_clusters = ocfs2_refcount_tree_update_clusters,
431 .eo_fill_root_el = ocfs2_refcount_tree_fill_root_el,
432 .eo_extent_contig = ocfs2_refcount_tree_extent_contig,
433};
434
Joel Becker8d6220d2008-08-22 12:46:09 -0700435static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800436 struct ocfs2_caching_info *ci,
Joel Becker8d6220d2008-08-22 12:46:09 -0700437 struct buffer_head *bh,
Joel Becker13723d02008-10-17 19:25:01 -0700438 ocfs2_journal_access_func access,
Joel Becker8d6220d2008-08-22 12:46:09 -0700439 void *obj,
440 struct ocfs2_extent_tree_operations *ops)
Tao Mae7d4cb62008-08-18 17:38:44 +0800441{
Joel Becker1a09f552008-08-20 17:44:24 -0700442 et->et_ops = ops;
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700443 et->et_root_bh = bh;
Joel Becker5e404e92009-02-13 03:54:22 -0800444 et->et_ci = ci;
Joel Becker13723d02008-10-17 19:25:01 -0700445 et->et_root_journal_access = access;
Joel Beckerea5efa12008-08-20 16:57:27 -0700446 if (!obj)
447 obj = (void *)bh->b_data;
448 et->et_object = obj;
Tao Mae7d4cb62008-08-18 17:38:44 +0800449
Joel Becker0ce10102008-08-20 17:19:50 -0700450 et->et_ops->eo_fill_root_el(et);
Joel Becker943cced2008-08-20 17:31:10 -0700451 if (!et->et_ops->eo_fill_max_leaf_clusters)
452 et->et_max_leaf_clusters = 0;
453 else
Joel Becker6136ca52009-02-12 19:32:43 -0800454 et->et_ops->eo_fill_max_leaf_clusters(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800455}
456
Joel Becker8d6220d2008-08-22 12:46:09 -0700457void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800458 struct ocfs2_caching_info *ci,
Joel Becker8d6220d2008-08-22 12:46:09 -0700459 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700460{
Joel Becker5e404e92009-02-13 03:54:22 -0800461 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di,
Joel Becker13723d02008-10-17 19:25:01 -0700462 NULL, &ocfs2_dinode_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700463}
464
Joel Becker8d6220d2008-08-22 12:46:09 -0700465void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800466 struct ocfs2_caching_info *ci,
Joel Becker8d6220d2008-08-22 12:46:09 -0700467 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700468{
Joel Becker5e404e92009-02-13 03:54:22 -0800469 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb,
Joel Becker13723d02008-10-17 19:25:01 -0700470 NULL, &ocfs2_xattr_tree_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700471}
472
Joel Becker8d6220d2008-08-22 12:46:09 -0700473void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800474 struct ocfs2_caching_info *ci,
Joel Becker2a50a742008-12-09 14:24:33 -0800475 struct ocfs2_xattr_value_buf *vb)
Tao Mae7d4cb62008-08-18 17:38:44 +0800476{
Joel Becker5e404e92009-02-13 03:54:22 -0800477 __ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb,
Joel Becker8d6220d2008-08-22 12:46:09 -0700478 &ocfs2_xattr_value_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800479}
480
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800481void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800482 struct ocfs2_caching_info *ci,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800483 struct buffer_head *bh)
484{
Joel Becker5e404e92009-02-13 03:54:22 -0800485 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800486 NULL, &ocfs2_dx_root_et_ops);
487}
488
Tao Mafe924412009-08-18 11:22:25 +0800489void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
490 struct ocfs2_caching_info *ci,
491 struct buffer_head *bh)
492{
493 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb,
494 NULL, &ocfs2_refcount_tree_et_ops);
495}
496
Joel Becker35dc0aa2008-08-20 16:25:06 -0700497static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
498 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800499{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700500 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800501}
502
Joel Becker35dc0aa2008-08-20 16:25:06 -0700503static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800504{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700505 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800506}
507
Joel Becker6136ca52009-02-12 19:32:43 -0800508static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
Joel Becker35dc0aa2008-08-20 16:25:06 -0700509 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800510{
Joel Becker6136ca52009-02-12 19:32:43 -0800511 et->et_ops->eo_update_clusters(et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700512}
513
Joel Becker92ba4702009-02-13 03:18:34 -0800514static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et,
515 struct ocfs2_extent_rec *rec)
516{
517 if (et->et_ops->eo_extent_map_insert)
518 et->et_ops->eo_extent_map_insert(et, rec);
519}
520
Joel Becker4c911ee2009-02-13 02:50:12 -0800521static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et,
522 u32 clusters)
523{
524 if (et->et_ops->eo_extent_map_truncate)
525 et->et_ops->eo_extent_map_truncate(et, clusters);
526}
527
Joel Becker13723d02008-10-17 19:25:01 -0700528static inline int ocfs2_et_root_journal_access(handle_t *handle,
Joel Becker13723d02008-10-17 19:25:01 -0700529 struct ocfs2_extent_tree *et,
530 int type)
531{
Joel Beckerd9a0a1f2009-02-12 17:32:34 -0800532 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700533 type);
534}
535
Tao Ma853a3a12009-08-18 11:22:18 +0800536static inline enum ocfs2_contig_type
537 ocfs2_et_extent_contig(struct ocfs2_extent_tree *et,
538 struct ocfs2_extent_rec *rec,
539 struct ocfs2_extent_rec *insert_rec)
540{
541 if (et->et_ops->eo_extent_contig)
542 return et->et_ops->eo_extent_contig(et, rec, insert_rec);
543
544 return ocfs2_extent_rec_contig(
545 ocfs2_metadata_cache_get_super(et->et_ci),
546 rec, insert_rec);
547}
548
Joel Becker6136ca52009-02-12 19:32:43 -0800549static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700550 struct ocfs2_extent_rec *rec)
551{
552 int ret = 0;
553
554 if (et->et_ops->eo_insert_check)
Joel Becker6136ca52009-02-12 19:32:43 -0800555 ret = et->et_ops->eo_insert_check(et, rec);
Joel Becker1e61ee72008-08-20 18:32:45 -0700556 return ret;
557}
558
Joel Becker6136ca52009-02-12 19:32:43 -0800559static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
Joel Becker35dc0aa2008-08-20 16:25:06 -0700560{
Joel Becker1e61ee72008-08-20 18:32:45 -0700561 int ret = 0;
562
563 if (et->et_ops->eo_sanity_check)
Joel Becker6136ca52009-02-12 19:32:43 -0800564 ret = et->et_ops->eo_sanity_check(et);
Joel Becker1e61ee72008-08-20 18:32:45 -0700565 return ret;
Tao Mae7d4cb62008-08-18 17:38:44 +0800566}
567
Mark Fashehccd979b2005-12-15 14:31:24 -0800568static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
Mark Fasheh59a5e412007-06-22 15:52:36 -0700569static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
570 struct ocfs2_extent_block *eb);
Joel Beckerd401dc12009-02-13 02:24:10 -0800571static void ocfs2_adjust_rightmost_records(handle_t *handle,
572 struct ocfs2_extent_tree *et,
Tao Ma6b791bc2009-06-12 14:18:36 +0800573 struct ocfs2_path *path,
574 struct ocfs2_extent_rec *insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -0800575/*
576 * Reset the actual path elements so that we can re-use the structure
577 * to build another path. Generally, this involves freeing the buffer
578 * heads.
579 */
Tao Mae2e9f602009-08-18 11:22:34 +0800580void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
Mark Fashehdcd05382007-01-16 11:32:23 -0800581{
582 int i, start = 0, depth = 0;
583 struct ocfs2_path_item *node;
584
585 if (keep_root)
586 start = 1;
587
588 for(i = start; i < path_num_items(path); i++) {
589 node = &path->p_node[i];
590
591 brelse(node->bh);
592 node->bh = NULL;
593 node->el = NULL;
594 }
595
596 /*
597 * Tree depth may change during truncate, or insert. If we're
598 * keeping the root extent list, then make sure that our path
599 * structure reflects the proper depth.
600 */
601 if (keep_root)
602 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
Joel Becker13723d02008-10-17 19:25:01 -0700603 else
604 path_root_access(path) = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -0800605
606 path->p_tree_depth = depth;
607}
608
Tao Mae2e9f602009-08-18 11:22:34 +0800609void ocfs2_free_path(struct ocfs2_path *path)
Mark Fashehdcd05382007-01-16 11:32:23 -0800610{
611 if (path) {
612 ocfs2_reinit_path(path, 0);
613 kfree(path);
614 }
615}
616
617/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700618 * All the elements of src into dest. After this call, src could be freed
619 * without affecting dest.
620 *
621 * Both paths should have the same root. Any non-root elements of dest
622 * will be freed.
623 */
624static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
625{
626 int i;
627
628 BUG_ON(path_root_bh(dest) != path_root_bh(src));
629 BUG_ON(path_root_el(dest) != path_root_el(src));
Joel Becker13723d02008-10-17 19:25:01 -0700630 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fasheh328d5752007-06-18 10:48:04 -0700631
632 ocfs2_reinit_path(dest, 1);
633
634 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
635 dest->p_node[i].bh = src->p_node[i].bh;
636 dest->p_node[i].el = src->p_node[i].el;
637
638 if (dest->p_node[i].bh)
639 get_bh(dest->p_node[i].bh);
640 }
641}
642
643/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800644 * Make the *dest path the same as src and re-initialize src path to
645 * have a root only.
646 */
647static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
648{
649 int i;
650
651 BUG_ON(path_root_bh(dest) != path_root_bh(src));
Joel Becker13723d02008-10-17 19:25:01 -0700652 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fashehdcd05382007-01-16 11:32:23 -0800653
654 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
655 brelse(dest->p_node[i].bh);
656
657 dest->p_node[i].bh = src->p_node[i].bh;
658 dest->p_node[i].el = src->p_node[i].el;
659
660 src->p_node[i].bh = NULL;
661 src->p_node[i].el = NULL;
662 }
663}
664
665/*
666 * Insert an extent block at given index.
667 *
668 * This will not take an additional reference on eb_bh.
669 */
670static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
671 struct buffer_head *eb_bh)
672{
673 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
674
675 /*
676 * Right now, no root bh is an extent block, so this helps
677 * catch code errors with dinode trees. The assertion can be
678 * safely removed if we ever need to insert extent block
679 * structures at the root.
680 */
681 BUG_ON(index == 0);
682
683 path->p_node[index].bh = eb_bh;
684 path->p_node[index].el = &eb->h_list;
685}
686
687static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700688 struct ocfs2_extent_list *root_el,
689 ocfs2_journal_access_func access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800690{
691 struct ocfs2_path *path;
692
693 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
694
695 path = kzalloc(sizeof(*path), GFP_NOFS);
696 if (path) {
697 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
698 get_bh(root_bh);
699 path_root_bh(path) = root_bh;
700 path_root_el(path) = root_el;
Joel Becker13723d02008-10-17 19:25:01 -0700701 path_root_access(path) = access;
Mark Fashehdcd05382007-01-16 11:32:23 -0800702 }
703
704 return path;
705}
706
Tao Mae2e9f602009-08-18 11:22:34 +0800707struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
Joel Beckerffdd7a52008-10-17 22:32:01 -0700708{
Joel Becker13723d02008-10-17 19:25:01 -0700709 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
710 path_root_access(path));
Joel Beckerffdd7a52008-10-17 22:32:01 -0700711}
712
Tao Mae2e9f602009-08-18 11:22:34 +0800713struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
Joel Beckerffdd7a52008-10-17 22:32:01 -0700714{
Joel Becker13723d02008-10-17 19:25:01 -0700715 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
716 et->et_root_journal_access);
717}
718
719/*
720 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
721 * otherwise it's the root_access function.
722 *
723 * I don't like the way this function's name looks next to
724 * ocfs2_journal_access_path(), but I don't have a better one.
725 */
Tao Mae2e9f602009-08-18 11:22:34 +0800726int ocfs2_path_bh_journal_access(handle_t *handle,
727 struct ocfs2_caching_info *ci,
728 struct ocfs2_path *path,
729 int idx)
Joel Becker13723d02008-10-17 19:25:01 -0700730{
731 ocfs2_journal_access_func access = path_root_access(path);
732
733 if (!access)
734 access = ocfs2_journal_access;
735
736 if (idx)
737 access = ocfs2_journal_access_eb;
738
Joel Becker0cf2f762009-02-12 16:41:25 -0800739 return access(handle, ci, path->p_node[idx].bh,
Joel Becker13723d02008-10-17 19:25:01 -0700740 OCFS2_JOURNAL_ACCESS_WRITE);
Joel Beckerffdd7a52008-10-17 22:32:01 -0700741}
742
Mark Fashehdcd05382007-01-16 11:32:23 -0800743/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800744 * Convenience function to journal all components in a path.
745 */
Tao Mae2e9f602009-08-18 11:22:34 +0800746int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
747 handle_t *handle,
748 struct ocfs2_path *path)
Mark Fashehdcd05382007-01-16 11:32:23 -0800749{
750 int i, ret = 0;
751
752 if (!path)
753 goto out;
754
755 for(i = 0; i < path_num_items(path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800756 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -0800757 if (ret < 0) {
758 mlog_errno(ret);
759 goto out;
760 }
761 }
762
763out:
764 return ret;
765}
766
Mark Fasheh328d5752007-06-18 10:48:04 -0700767/*
768 * Return the index of the extent record which contains cluster #v_cluster.
769 * -1 is returned if it was not found.
770 *
771 * Should work fine on interior and exterior nodes.
772 */
773int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
774{
775 int ret = -1;
776 int i;
777 struct ocfs2_extent_rec *rec;
778 u32 rec_end, rec_start, clusters;
779
780 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
781 rec = &el->l_recs[i];
782
783 rec_start = le32_to_cpu(rec->e_cpos);
784 clusters = ocfs2_rec_clusters(el, rec);
785
786 rec_end = rec_start + clusters;
787
788 if (v_cluster >= rec_start && v_cluster < rec_end) {
789 ret = i;
790 break;
791 }
792 }
793
794 return ret;
795}
796
Mark Fashehe48edee2007-03-07 16:46:57 -0800797/*
798 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
Tao Ma853a3a12009-08-18 11:22:18 +0800799 * ocfs2_extent_rec_contig only work properly against leaf nodes!
Mark Fashehe48edee2007-03-07 16:46:57 -0800800 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800801static int ocfs2_block_extent_contig(struct super_block *sb,
802 struct ocfs2_extent_rec *ext,
803 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800804{
Mark Fashehe48edee2007-03-07 16:46:57 -0800805 u64 blk_end = le64_to_cpu(ext->e_blkno);
806
807 blk_end += ocfs2_clusters_to_blocks(sb,
808 le16_to_cpu(ext->e_leaf_clusters));
809
810 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800811}
812
Mark Fashehdcd05382007-01-16 11:32:23 -0800813static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
814 struct ocfs2_extent_rec *right)
815{
Mark Fashehe48edee2007-03-07 16:46:57 -0800816 u32 left_range;
817
818 left_range = le32_to_cpu(left->e_cpos) +
819 le16_to_cpu(left->e_leaf_clusters);
820
821 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800822}
823
824static enum ocfs2_contig_type
Tao Ma853a3a12009-08-18 11:22:18 +0800825 ocfs2_extent_rec_contig(struct super_block *sb,
826 struct ocfs2_extent_rec *ext,
827 struct ocfs2_extent_rec *insert_rec)
Mark Fashehdcd05382007-01-16 11:32:23 -0800828{
829 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
830
Mark Fasheh328d5752007-06-18 10:48:04 -0700831 /*
832 * Refuse to coalesce extent records with different flag
833 * fields - we don't want to mix unwritten extents with user
834 * data.
835 */
836 if (ext->e_flags != insert_rec->e_flags)
837 return CONTIG_NONE;
838
Mark Fashehdcd05382007-01-16 11:32:23 -0800839 if (ocfs2_extents_adjacent(ext, insert_rec) &&
Joel Beckerb4a17652009-02-13 03:07:09 -0800840 ocfs2_block_extent_contig(sb, ext, blkno))
Mark Fashehdcd05382007-01-16 11:32:23 -0800841 return CONTIG_RIGHT;
842
843 blkno = le64_to_cpu(ext->e_blkno);
844 if (ocfs2_extents_adjacent(insert_rec, ext) &&
Joel Beckerb4a17652009-02-13 03:07:09 -0800845 ocfs2_block_extent_contig(sb, insert_rec, blkno))
Mark Fashehdcd05382007-01-16 11:32:23 -0800846 return CONTIG_LEFT;
847
848 return CONTIG_NONE;
849}
850
851/*
852 * NOTE: We can have pretty much any combination of contiguousness and
853 * appending.
854 *
855 * The usefulness of APPEND_TAIL is more in that it lets us know that
856 * we'll have to update the path to that leaf.
857 */
858enum ocfs2_append_type {
859 APPEND_NONE = 0,
860 APPEND_TAIL,
861};
862
Mark Fasheh328d5752007-06-18 10:48:04 -0700863enum ocfs2_split_type {
864 SPLIT_NONE = 0,
865 SPLIT_LEFT,
866 SPLIT_RIGHT,
867};
868
Mark Fashehdcd05382007-01-16 11:32:23 -0800869struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700870 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800871 enum ocfs2_append_type ins_appending;
872 enum ocfs2_contig_type ins_contig;
873 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800874 int ins_tree_depth;
875};
876
Mark Fasheh328d5752007-06-18 10:48:04 -0700877struct ocfs2_merge_ctxt {
878 enum ocfs2_contig_type c_contig_type;
879 int c_has_empty_extent;
880 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700881};
882
Joel Becker5e965812008-11-13 14:49:16 -0800883static int ocfs2_validate_extent_block(struct super_block *sb,
884 struct buffer_head *bh)
885{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700886 int rc;
Joel Becker5e965812008-11-13 14:49:16 -0800887 struct ocfs2_extent_block *eb =
888 (struct ocfs2_extent_block *)bh->b_data;
889
Joel Becker970e4932008-11-13 14:49:19 -0800890 mlog(0, "Validating extent block %llu\n",
891 (unsigned long long)bh->b_blocknr);
892
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700893 BUG_ON(!buffer_uptodate(bh));
894
895 /*
896 * If the ecc fails, we return the error but otherwise
897 * leave the filesystem running. We know any error is
898 * local to this block.
899 */
900 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
Joel Becker13723d02008-10-17 19:25:01 -0700901 if (rc) {
902 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
903 (unsigned long long)bh->b_blocknr);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700904 return rc;
Joel Becker13723d02008-10-17 19:25:01 -0700905 }
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700906
907 /*
908 * Errors after here are fatal.
909 */
910
Joel Becker5e965812008-11-13 14:49:16 -0800911 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
912 ocfs2_error(sb,
913 "Extent block #%llu has bad signature %.*s",
914 (unsigned long long)bh->b_blocknr, 7,
915 eb->h_signature);
916 return -EINVAL;
917 }
918
919 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
920 ocfs2_error(sb,
921 "Extent block #%llu has an invalid h_blkno "
922 "of %llu",
923 (unsigned long long)bh->b_blocknr,
924 (unsigned long long)le64_to_cpu(eb->h_blkno));
925 return -EINVAL;
926 }
927
928 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
929 ocfs2_error(sb,
930 "Extent block #%llu has an invalid "
931 "h_fs_generation of #%u",
932 (unsigned long long)bh->b_blocknr,
933 le32_to_cpu(eb->h_fs_generation));
934 return -EINVAL;
935 }
936
937 return 0;
938}
939
Joel Becker3d03a302009-02-12 17:49:26 -0800940int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
Joel Becker5e965812008-11-13 14:49:16 -0800941 struct buffer_head **bh)
942{
943 int rc;
944 struct buffer_head *tmp = *bh;
945
Joel Becker3d03a302009-02-12 17:49:26 -0800946 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800947 ocfs2_validate_extent_block);
Joel Becker5e965812008-11-13 14:49:16 -0800948
949 /* If ocfs2_read_block() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -0800950 if (!rc && !*bh)
Joel Becker5e965812008-11-13 14:49:16 -0800951 *bh = tmp;
952
Joel Becker5e965812008-11-13 14:49:16 -0800953 return rc;
954}
955
956
Mark Fashehccd979b2005-12-15 14:31:24 -0800957/*
958 * How many free extents have we got before we need more meta data?
959 */
960int ocfs2_num_free_extents(struct ocfs2_super *osb,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700961 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -0800962{
963 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800964 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800965 struct ocfs2_extent_block *eb;
966 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800967 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800968
969 mlog_entry_void();
970
Joel Beckerf99b9b72008-08-20 19:36:33 -0700971 el = et->et_root_el;
972 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800973
Tao Mae7d4cb62008-08-18 17:38:44 +0800974 if (last_eb_blk) {
Joel Becker3d03a302009-02-12 17:49:26 -0800975 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
976 &eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800977 if (retval < 0) {
978 mlog_errno(retval);
979 goto bail;
980 }
981 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
982 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800983 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800984
985 BUG_ON(el->l_tree_depth != 0);
986
987 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
988bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700989 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800990
991 mlog_exit(retval);
992 return retval;
993}
994
995/* expects array to already be allocated
996 *
997 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
998 * l_count for you
999 */
Joel Becker42a5a7a2009-02-12 18:49:19 -08001000static int ocfs2_create_new_meta_bhs(handle_t *handle,
1001 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001002 int wanted,
1003 struct ocfs2_alloc_context *meta_ac,
1004 struct buffer_head *bhs[])
1005{
1006 int count, status, i;
1007 u16 suballoc_bit_start;
1008 u32 num_got;
1009 u64 first_blkno;
Joel Becker42a5a7a2009-02-12 18:49:19 -08001010 struct ocfs2_super *osb =
1011 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001012 struct ocfs2_extent_block *eb;
1013
1014 mlog_entry_void();
1015
1016 count = 0;
1017 while (count < wanted) {
1018 status = ocfs2_claim_metadata(osb,
1019 handle,
1020 meta_ac,
1021 wanted - count,
1022 &suballoc_bit_start,
1023 &num_got,
1024 &first_blkno);
1025 if (status < 0) {
1026 mlog_errno(status);
1027 goto bail;
1028 }
1029
1030 for(i = count; i < (num_got + count); i++) {
1031 bhs[i] = sb_getblk(osb->sb, first_blkno);
1032 if (bhs[i] == NULL) {
1033 status = -EIO;
1034 mlog_errno(status);
1035 goto bail;
1036 }
Joel Becker42a5a7a2009-02-12 18:49:19 -08001037 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001038
Joel Becker42a5a7a2009-02-12 18:49:19 -08001039 status = ocfs2_journal_access_eb(handle, et->et_ci,
1040 bhs[i],
Joel Becker13723d02008-10-17 19:25:01 -07001041 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001042 if (status < 0) {
1043 mlog_errno(status);
1044 goto bail;
1045 }
1046
1047 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
1048 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
1049 /* Ok, setup the minimal stuff here. */
1050 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
1051 eb->h_blkno = cpu_to_le64(first_blkno);
1052 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08001053 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001054 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1055 eb->h_list.l_count =
1056 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1057
1058 suballoc_bit_start++;
1059 first_blkno++;
1060
1061 /* We'll also be dirtied by the caller, so
1062 * this isn't absolutely necessary. */
1063 status = ocfs2_journal_dirty(handle, bhs[i]);
1064 if (status < 0) {
1065 mlog_errno(status);
1066 goto bail;
1067 }
1068 }
1069
1070 count += num_got;
1071 }
1072
1073 status = 0;
1074bail:
1075 if (status < 0) {
1076 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001077 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001078 bhs[i] = NULL;
1079 }
1080 }
1081 mlog_exit(status);
1082 return status;
1083}
1084
1085/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001086 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1087 *
1088 * Returns the sum of the rightmost extent rec logical offset and
1089 * cluster count.
1090 *
1091 * ocfs2_add_branch() uses this to determine what logical cluster
1092 * value should be populated into the leftmost new branch records.
1093 *
1094 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1095 * value for the new topmost tree record.
1096 */
1097static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1098{
1099 int i;
1100
1101 i = le16_to_cpu(el->l_next_free_rec) - 1;
1102
1103 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001104 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08001105}
1106
1107/*
Tao Ma6b791bc2009-06-12 14:18:36 +08001108 * Change range of the branches in the right most path according to the leaf
1109 * extent block's rightmost record.
1110 */
1111static int ocfs2_adjust_rightmost_branch(handle_t *handle,
Tao Ma6b791bc2009-06-12 14:18:36 +08001112 struct ocfs2_extent_tree *et)
1113{
1114 int status;
1115 struct ocfs2_path *path = NULL;
1116 struct ocfs2_extent_list *el;
1117 struct ocfs2_extent_rec *rec;
1118
1119 path = ocfs2_new_path_from_et(et);
1120 if (!path) {
1121 status = -ENOMEM;
1122 return status;
1123 }
1124
Joel Beckerfacdb772009-02-12 18:08:48 -08001125 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
Tao Ma6b791bc2009-06-12 14:18:36 +08001126 if (status < 0) {
1127 mlog_errno(status);
1128 goto out;
1129 }
1130
1131 status = ocfs2_extend_trans(handle, path_num_items(path) +
1132 handle->h_buffer_credits);
1133 if (status < 0) {
1134 mlog_errno(status);
1135 goto out;
1136 }
1137
Joel Beckerd401dc12009-02-13 02:24:10 -08001138 status = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma6b791bc2009-06-12 14:18:36 +08001139 if (status < 0) {
1140 mlog_errno(status);
1141 goto out;
1142 }
1143
1144 el = path_leaf_el(path);
1145 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1146
Joel Beckerd401dc12009-02-13 02:24:10 -08001147 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Tao Ma6b791bc2009-06-12 14:18:36 +08001148
1149out:
1150 ocfs2_free_path(path);
1151 return status;
1152}
1153
1154/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001155 * Add an entire tree branch to our inode. eb_bh is the extent block
Joel Beckerd401dc12009-02-13 02:24:10 -08001156 * to start at, if we don't want to start the branch at the root
Mark Fashehccd979b2005-12-15 14:31:24 -08001157 * structure.
1158 *
1159 * last_eb_bh is required as we have to update it's next_leaf pointer
1160 * for the new last extent block.
1161 *
1162 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -08001163 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -08001164 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001165static int ocfs2_add_branch(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001166 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001167 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07001168 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001169 struct ocfs2_alloc_context *meta_ac)
1170{
1171 int status, new_blocks, i;
1172 u64 next_blkno, new_last_eb_blk;
1173 struct buffer_head *bh;
1174 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001175 struct ocfs2_extent_block *eb;
1176 struct ocfs2_extent_list *eb_el;
1177 struct ocfs2_extent_list *el;
Tao Ma6b791bc2009-06-12 14:18:36 +08001178 u32 new_cpos, root_end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001179
1180 mlog_entry_void();
1181
Mark Fasheh328d5752007-06-18 10:48:04 -07001182 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001183
Mark Fashehccd979b2005-12-15 14:31:24 -08001184 if (eb_bh) {
1185 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1186 el = &eb->h_list;
1187 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001188 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001189
1190 /* we never add a branch to a leaf. */
1191 BUG_ON(!el->l_tree_depth);
1192
1193 new_blocks = le16_to_cpu(el->l_tree_depth);
1194
Tao Ma6b791bc2009-06-12 14:18:36 +08001195 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1196 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1197 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1198
1199 /*
1200 * If there is a gap before the root end and the real end
1201 * of the righmost leaf block, we need to remove the gap
1202 * between new_cpos and root_end first so that the tree
1203 * is consistent after we add a new branch(it will start
1204 * from new_cpos).
1205 */
1206 if (root_end > new_cpos) {
1207 mlog(0, "adjust the cluster end from %u to %u\n",
1208 root_end, new_cpos);
Joel Beckerd401dc12009-02-13 02:24:10 -08001209 status = ocfs2_adjust_rightmost_branch(handle, et);
Tao Ma6b791bc2009-06-12 14:18:36 +08001210 if (status) {
1211 mlog_errno(status);
1212 goto bail;
1213 }
1214 }
1215
Mark Fashehccd979b2005-12-15 14:31:24 -08001216 /* allocate the number of new eb blocks we need */
1217 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1218 GFP_KERNEL);
1219 if (!new_eb_bhs) {
1220 status = -ENOMEM;
1221 mlog_errno(status);
1222 goto bail;
1223 }
1224
Joel Becker42a5a7a2009-02-12 18:49:19 -08001225 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
Mark Fashehccd979b2005-12-15 14:31:24 -08001226 meta_ac, new_eb_bhs);
1227 if (status < 0) {
1228 mlog_errno(status);
1229 goto bail;
1230 }
1231
1232 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1233 * linked with the rest of the tree.
1234 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1235 *
1236 * when we leave the loop, new_last_eb_blk will point to the
1237 * newest leaf, and next_blkno will point to the topmost extent
1238 * block. */
1239 next_blkno = new_last_eb_blk = 0;
1240 for(i = 0; i < new_blocks; i++) {
1241 bh = new_eb_bhs[i];
1242 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001243 /* ocfs2_create_new_meta_bhs() should create it right! */
1244 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001245 eb_el = &eb->h_list;
1246
Joel Beckerd401dc12009-02-13 02:24:10 -08001247 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
Joel Becker13723d02008-10-17 19:25:01 -07001248 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001249 if (status < 0) {
1250 mlog_errno(status);
1251 goto bail;
1252 }
1253
1254 eb->h_next_leaf_blk = 0;
1255 eb_el->l_tree_depth = cpu_to_le16(i);
1256 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001257 /*
1258 * This actually counts as an empty extent as
1259 * c_clusters == 0
1260 */
1261 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001262 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001263 /*
1264 * eb_el isn't always an interior node, but even leaf
1265 * nodes want a zero'd flags and reserved field so
1266 * this gets the whole 32 bits regardless of use.
1267 */
1268 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001269 if (!eb_el->l_tree_depth)
1270 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1271
1272 status = ocfs2_journal_dirty(handle, bh);
1273 if (status < 0) {
1274 mlog_errno(status);
1275 goto bail;
1276 }
1277
1278 next_blkno = le64_to_cpu(eb->h_blkno);
1279 }
1280
1281 /* This is a bit hairy. We want to update up to three blocks
1282 * here without leaving any of them in an inconsistent state
1283 * in case of error. We don't have to worry about
1284 * journal_dirty erroring as it won't unless we've aborted the
1285 * handle (in which case we would never be here) so reserving
1286 * the write with journal_access is all we need to do. */
Joel Beckerd401dc12009-02-13 02:24:10 -08001287 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001288 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001289 if (status < 0) {
1290 mlog_errno(status);
1291 goto bail;
1292 }
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001293 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001294 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001295 if (status < 0) {
1296 mlog_errno(status);
1297 goto bail;
1298 }
1299 if (eb_bh) {
Joel Beckerd401dc12009-02-13 02:24:10 -08001300 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001301 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001302 if (status < 0) {
1303 mlog_errno(status);
1304 goto bail;
1305 }
1306 }
1307
1308 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001309 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001310 i = le16_to_cpu(el->l_next_free_rec);
1311 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001312 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001313 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001314 le16_add_cpu(&el->l_next_free_rec, 1);
1315
1316 /* fe needs a new last extent block pointer, as does the
1317 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001318 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001319
Mark Fasheh328d5752007-06-18 10:48:04 -07001320 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001321 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1322
Mark Fasheh328d5752007-06-18 10:48:04 -07001323 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001324 if (status < 0)
1325 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001326 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001327 if (status < 0)
1328 mlog_errno(status);
1329 if (eb_bh) {
1330 status = ocfs2_journal_dirty(handle, eb_bh);
1331 if (status < 0)
1332 mlog_errno(status);
1333 }
1334
Mark Fasheh328d5752007-06-18 10:48:04 -07001335 /*
1336 * Some callers want to track the rightmost leaf so pass it
1337 * back here.
1338 */
1339 brelse(*last_eb_bh);
1340 get_bh(new_eb_bhs[0]);
1341 *last_eb_bh = new_eb_bhs[0];
1342
Mark Fashehccd979b2005-12-15 14:31:24 -08001343 status = 0;
1344bail:
1345 if (new_eb_bhs) {
1346 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001347 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001348 kfree(new_eb_bhs);
1349 }
1350
1351 mlog_exit(status);
1352 return status;
1353}
1354
1355/*
1356 * adds another level to the allocation tree.
1357 * returns back the new extent block so you can add a branch to it
1358 * after this call.
1359 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001360static int ocfs2_shift_tree_depth(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001361 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001362 struct ocfs2_alloc_context *meta_ac,
1363 struct buffer_head **ret_new_eb_bh)
1364{
1365 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001366 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001367 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001368 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001369 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001370 struct ocfs2_extent_list *eb_el;
1371
1372 mlog_entry_void();
1373
Joel Becker42a5a7a2009-02-12 18:49:19 -08001374 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
Mark Fashehccd979b2005-12-15 14:31:24 -08001375 &new_eb_bh);
1376 if (status < 0) {
1377 mlog_errno(status);
1378 goto bail;
1379 }
1380
1381 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001382 /* ocfs2_create_new_meta_bhs() should create it right! */
1383 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001384
1385 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001386 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001387
Joel Beckerd401dc12009-02-13 02:24:10 -08001388 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001389 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001390 if (status < 0) {
1391 mlog_errno(status);
1392 goto bail;
1393 }
1394
Tao Mae7d4cb62008-08-18 17:38:44 +08001395 /* copy the root extent list data into the new extent block */
1396 eb_el->l_tree_depth = root_el->l_tree_depth;
1397 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1398 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1399 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001400
1401 status = ocfs2_journal_dirty(handle, new_eb_bh);
1402 if (status < 0) {
1403 mlog_errno(status);
1404 goto bail;
1405 }
1406
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001407 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001408 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001409 if (status < 0) {
1410 mlog_errno(status);
1411 goto bail;
1412 }
1413
Mark Fashehdcd05382007-01-16 11:32:23 -08001414 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1415
Tao Mae7d4cb62008-08-18 17:38:44 +08001416 /* update root_bh now */
1417 le16_add_cpu(&root_el->l_tree_depth, 1);
1418 root_el->l_recs[0].e_cpos = 0;
1419 root_el->l_recs[0].e_blkno = eb->h_blkno;
1420 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1421 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1422 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1423 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001424
1425 /* If this is our 1st tree depth shift, then last_eb_blk
1426 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001427 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001428 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001429
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001430 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001431 if (status < 0) {
1432 mlog_errno(status);
1433 goto bail;
1434 }
1435
1436 *ret_new_eb_bh = new_eb_bh;
1437 new_eb_bh = NULL;
1438 status = 0;
1439bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001440 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001441
1442 mlog_exit(status);
1443 return status;
1444}
1445
1446/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001447 * Should only be called when there is no space left in any of the
1448 * leaf nodes. What we want to do is find the lowest tree depth
1449 * non-leaf extent block with room for new records. There are three
1450 * valid results of this search:
1451 *
1452 * 1) a lowest extent block is found, then we pass it back in
1453 * *lowest_eb_bh and return '0'
1454 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001455 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001456 * pass NULL back in *lowest_eb_bh, but still return '0'
1457 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001458 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001459 * which case we return > 0
1460 *
1461 * return status < 0 indicates an error.
1462 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001463static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001464 struct buffer_head **target_bh)
1465{
1466 int status = 0, i;
1467 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001468 struct ocfs2_extent_block *eb;
1469 struct ocfs2_extent_list *el;
1470 struct buffer_head *bh = NULL;
1471 struct buffer_head *lowest_bh = NULL;
1472
1473 mlog_entry_void();
1474
1475 *target_bh = NULL;
1476
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001477 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001478
1479 while(le16_to_cpu(el->l_tree_depth) > 1) {
1480 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08001481 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1482 "Owner %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001483 "extent list (next_free_rec == 0)",
Joel Becker3d03a302009-02-12 17:49:26 -08001484 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001485 status = -EIO;
1486 goto bail;
1487 }
1488 i = le16_to_cpu(el->l_next_free_rec) - 1;
1489 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1490 if (!blkno) {
Joel Becker3d03a302009-02-12 17:49:26 -08001491 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1492 "Owner %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001493 "list where extent # %d has no physical "
1494 "block start",
Joel Becker3d03a302009-02-12 17:49:26 -08001495 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001496 status = -EIO;
1497 goto bail;
1498 }
1499
Mark Fasheha81cb882008-10-07 14:25:16 -07001500 brelse(bh);
1501 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001502
Joel Becker3d03a302009-02-12 17:49:26 -08001503 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001504 if (status < 0) {
1505 mlog_errno(status);
1506 goto bail;
1507 }
1508
1509 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001510 el = &eb->h_list;
1511
1512 if (le16_to_cpu(el->l_next_free_rec) <
1513 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001514 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001515 lowest_bh = bh;
1516 get_bh(lowest_bh);
1517 }
1518 }
1519
1520 /* If we didn't find one and the fe doesn't have any room,
1521 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001522 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001523 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001524 status = 1;
1525
1526 *target_bh = lowest_bh;
1527bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001528 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001529
1530 mlog_exit(status);
1531 return status;
1532}
1533
Mark Fashehe48edee2007-03-07 16:46:57 -08001534/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001535 * Grow a b-tree so that it has more records.
1536 *
1537 * We might shift the tree depth in which case existing paths should
1538 * be considered invalid.
1539 *
1540 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001541 *
1542 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001543 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001544static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1545 int *final_depth, struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001546 struct ocfs2_alloc_context *meta_ac)
1547{
1548 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001549 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001550 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001551 struct buffer_head *bh = NULL;
1552
1553 BUG_ON(meta_ac == NULL);
1554
Joel Beckerd401dc12009-02-13 02:24:10 -08001555 shift = ocfs2_find_branch_target(et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001556 if (shift < 0) {
1557 ret = shift;
1558 mlog_errno(ret);
1559 goto out;
1560 }
1561
1562 /* We traveled all the way to the bottom of the allocation tree
1563 * and didn't find room for any more extents - we need to add
1564 * another tree level */
1565 if (shift) {
1566 BUG_ON(bh);
1567 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1568
1569 /* ocfs2_shift_tree_depth will return us a buffer with
1570 * the new extent block (so we can pass that to
1571 * ocfs2_add_branch). */
Joel Beckerd401dc12009-02-13 02:24:10 -08001572 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001573 if (ret < 0) {
1574 mlog_errno(ret);
1575 goto out;
1576 }
1577 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001578 if (depth == 1) {
1579 /*
1580 * Special case: we have room now if we shifted from
1581 * tree_depth 0, so no more work needs to be done.
1582 *
1583 * We won't be calling add_branch, so pass
1584 * back *last_eb_bh as the new leaf. At depth
1585 * zero, it should always be null so there's
1586 * no reason to brelse.
1587 */
1588 BUG_ON(*last_eb_bh);
1589 get_bh(bh);
1590 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001591 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001592 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001593 }
1594
1595 /* call ocfs2_add_branch to add the final part of the tree with
1596 * the new data. */
1597 mlog(0, "add branch. bh = %p\n", bh);
Joel Beckerd401dc12009-02-13 02:24:10 -08001598 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001599 meta_ac);
1600 if (ret < 0) {
1601 mlog_errno(ret);
1602 goto out;
1603 }
1604
1605out:
1606 if (final_depth)
1607 *final_depth = depth;
1608 brelse(bh);
1609 return ret;
1610}
1611
1612/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001613 * This function will discard the rightmost extent record.
1614 */
1615static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1616{
1617 int next_free = le16_to_cpu(el->l_next_free_rec);
1618 int count = le16_to_cpu(el->l_count);
1619 unsigned int num_bytes;
1620
1621 BUG_ON(!next_free);
1622 /* This will cause us to go off the end of our extent list. */
1623 BUG_ON(next_free >= count);
1624
1625 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1626
1627 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1628}
1629
1630static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1631 struct ocfs2_extent_rec *insert_rec)
1632{
1633 int i, insert_index, next_free, has_empty, num_bytes;
1634 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1635 struct ocfs2_extent_rec *rec;
1636
1637 next_free = le16_to_cpu(el->l_next_free_rec);
1638 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1639
1640 BUG_ON(!next_free);
1641
1642 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001643 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001644
1645 /*
1646 * The easiest way to approach this is to just remove the
1647 * empty extent and temporarily decrement next_free.
1648 */
1649 if (has_empty) {
1650 /*
1651 * If next_free was 1 (only an empty extent), this
1652 * loop won't execute, which is fine. We still want
1653 * the decrement above to happen.
1654 */
1655 for(i = 0; i < (next_free - 1); i++)
1656 el->l_recs[i] = el->l_recs[i+1];
1657
1658 next_free--;
1659 }
1660
1661 /*
1662 * Figure out what the new record index should be.
1663 */
1664 for(i = 0; i < next_free; i++) {
1665 rec = &el->l_recs[i];
1666
1667 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1668 break;
1669 }
1670 insert_index = i;
1671
1672 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1673 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1674
1675 BUG_ON(insert_index < 0);
1676 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1677 BUG_ON(insert_index > next_free);
1678
1679 /*
1680 * No need to memmove if we're just adding to the tail.
1681 */
1682 if (insert_index != next_free) {
1683 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1684
1685 num_bytes = next_free - insert_index;
1686 num_bytes *= sizeof(struct ocfs2_extent_rec);
1687 memmove(&el->l_recs[insert_index + 1],
1688 &el->l_recs[insert_index],
1689 num_bytes);
1690 }
1691
1692 /*
1693 * Either we had an empty extent, and need to re-increment or
1694 * there was no empty extent on a non full rightmost leaf node,
1695 * in which case we still need to increment.
1696 */
1697 next_free++;
1698 el->l_next_free_rec = cpu_to_le16(next_free);
1699 /*
1700 * Make sure none of the math above just messed up our tree.
1701 */
1702 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1703
1704 el->l_recs[insert_index] = *insert_rec;
1705
1706}
1707
Mark Fasheh328d5752007-06-18 10:48:04 -07001708static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1709{
1710 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1711
1712 BUG_ON(num_recs == 0);
1713
1714 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1715 num_recs--;
1716 size = num_recs * sizeof(struct ocfs2_extent_rec);
1717 memmove(&el->l_recs[0], &el->l_recs[1], size);
1718 memset(&el->l_recs[num_recs], 0,
1719 sizeof(struct ocfs2_extent_rec));
1720 el->l_next_free_rec = cpu_to_le16(num_recs);
1721 }
1722}
1723
Mark Fashehdcd05382007-01-16 11:32:23 -08001724/*
1725 * Create an empty extent record .
1726 *
1727 * l_next_free_rec may be updated.
1728 *
1729 * If an empty extent already exists do nothing.
1730 */
1731static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1732{
1733 int next_free = le16_to_cpu(el->l_next_free_rec);
1734
Mark Fashehe48edee2007-03-07 16:46:57 -08001735 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1736
Mark Fashehdcd05382007-01-16 11:32:23 -08001737 if (next_free == 0)
1738 goto set_and_inc;
1739
1740 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1741 return;
1742
1743 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1744 "Asked to create an empty extent in a full list:\n"
1745 "count = %u, tree depth = %u",
1746 le16_to_cpu(el->l_count),
1747 le16_to_cpu(el->l_tree_depth));
1748
1749 ocfs2_shift_records_right(el);
1750
1751set_and_inc:
1752 le16_add_cpu(&el->l_next_free_rec, 1);
1753 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1754}
1755
1756/*
1757 * For a rotation which involves two leaf nodes, the "root node" is
1758 * the lowest level tree node which contains a path to both leafs. This
1759 * resulting set of information can be used to form a complete "subtree"
1760 *
1761 * This function is passed two full paths from the dinode down to a
1762 * pair of adjacent leaves. It's task is to figure out which path
1763 * index contains the subtree root - this can be the root index itself
1764 * in a worst-case rotation.
1765 *
1766 * The array index of the subtree root is passed back.
1767 */
Tao Ma38a04e42009-11-30 14:32:19 +08001768int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1769 struct ocfs2_path *left,
1770 struct ocfs2_path *right)
Mark Fashehdcd05382007-01-16 11:32:23 -08001771{
1772 int i = 0;
1773
1774 /*
1775 * Check that the caller passed in two paths from the same tree.
1776 */
1777 BUG_ON(path_root_bh(left) != path_root_bh(right));
1778
1779 do {
1780 i++;
1781
1782 /*
1783 * The caller didn't pass two adjacent paths.
1784 */
1785 mlog_bug_on_msg(i > left->p_tree_depth,
Joel Becker7dc02802009-02-12 19:20:13 -08001786 "Owner %llu, left depth %u, right depth %u\n"
Mark Fashehdcd05382007-01-16 11:32:23 -08001787 "left leaf blk %llu, right leaf blk %llu\n",
Joel Becker7dc02802009-02-12 19:20:13 -08001788 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1789 left->p_tree_depth, right->p_tree_depth,
Mark Fashehdcd05382007-01-16 11:32:23 -08001790 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1791 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1792 } while (left->p_node[i].bh->b_blocknr ==
1793 right->p_node[i].bh->b_blocknr);
1794
1795 return i - 1;
1796}
1797
1798typedef void (path_insert_t)(void *, struct buffer_head *);
1799
1800/*
1801 * Traverse a btree path in search of cpos, starting at root_el.
1802 *
1803 * This code can be called with a cpos larger than the tree, in which
1804 * case it will return the rightmost path.
1805 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001806static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
Mark Fashehdcd05382007-01-16 11:32:23 -08001807 struct ocfs2_extent_list *root_el, u32 cpos,
1808 path_insert_t *func, void *data)
1809{
1810 int i, ret = 0;
1811 u32 range;
1812 u64 blkno;
1813 struct buffer_head *bh = NULL;
1814 struct ocfs2_extent_block *eb;
1815 struct ocfs2_extent_list *el;
1816 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08001817
1818 el = root_el;
1819 while (el->l_tree_depth) {
1820 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001821 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1822 "Owner %llu has empty extent list at "
Mark Fashehdcd05382007-01-16 11:32:23 -08001823 "depth %u\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001824 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001825 le16_to_cpu(el->l_tree_depth));
1826 ret = -EROFS;
1827 goto out;
1828
1829 }
1830
1831 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1832 rec = &el->l_recs[i];
1833
1834 /*
1835 * In the case that cpos is off the allocation
1836 * tree, this should just wind up returning the
1837 * rightmost record.
1838 */
1839 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001840 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001841 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1842 break;
1843 }
1844
1845 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1846 if (blkno == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001847 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1848 "Owner %llu has bad blkno in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001849 "at depth %u (index %d)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001850 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001851 le16_to_cpu(el->l_tree_depth), i);
1852 ret = -EROFS;
1853 goto out;
1854 }
1855
1856 brelse(bh);
1857 bh = NULL;
Joel Beckerfacdb772009-02-12 18:08:48 -08001858 ret = ocfs2_read_extent_block(ci, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001859 if (ret) {
1860 mlog_errno(ret);
1861 goto out;
1862 }
1863
1864 eb = (struct ocfs2_extent_block *) bh->b_data;
1865 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001866
1867 if (le16_to_cpu(el->l_next_free_rec) >
1868 le16_to_cpu(el->l_count)) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001869 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1870 "Owner %llu has bad count in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001871 "at block %llu (next free=%u, count=%u)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001872 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001873 (unsigned long long)bh->b_blocknr,
1874 le16_to_cpu(el->l_next_free_rec),
1875 le16_to_cpu(el->l_count));
1876 ret = -EROFS;
1877 goto out;
1878 }
1879
1880 if (func)
1881 func(data, bh);
1882 }
1883
1884out:
1885 /*
1886 * Catch any trailing bh that the loop didn't handle.
1887 */
1888 brelse(bh);
1889
1890 return ret;
1891}
1892
1893/*
1894 * Given an initialized path (that is, it has a valid root extent
1895 * list), this function will traverse the btree in search of the path
1896 * which would contain cpos.
1897 *
1898 * The path traveled is recorded in the path structure.
1899 *
1900 * Note that this will not do any comparisons on leaf node extent
1901 * records, so it will work fine in the case that we just added a tree
1902 * branch.
1903 */
1904struct find_path_data {
1905 int index;
1906 struct ocfs2_path *path;
1907};
1908static void find_path_ins(void *data, struct buffer_head *bh)
1909{
1910 struct find_path_data *fp = data;
1911
1912 get_bh(bh);
1913 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1914 fp->index++;
1915}
Tao Mae2e9f602009-08-18 11:22:34 +08001916int ocfs2_find_path(struct ocfs2_caching_info *ci,
1917 struct ocfs2_path *path, u32 cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08001918{
1919 struct find_path_data data;
1920
1921 data.index = 1;
1922 data.path = path;
Joel Beckerfacdb772009-02-12 18:08:48 -08001923 return __ocfs2_find_path(ci, path_root_el(path), cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08001924 find_path_ins, &data);
1925}
1926
1927static void find_leaf_ins(void *data, struct buffer_head *bh)
1928{
1929 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1930 struct ocfs2_extent_list *el = &eb->h_list;
1931 struct buffer_head **ret = data;
1932
1933 /* We want to retain only the leaf block. */
1934 if (le16_to_cpu(el->l_tree_depth) == 0) {
1935 get_bh(bh);
1936 *ret = bh;
1937 }
1938}
1939/*
1940 * Find the leaf block in the tree which would contain cpos. No
1941 * checking of the actual leaf is done.
1942 *
1943 * Some paths want to call this instead of allocating a path structure
1944 * and calling ocfs2_find_path().
1945 *
1946 * This function doesn't handle non btree extent lists.
1947 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001948int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1949 struct ocfs2_extent_list *root_el, u32 cpos,
1950 struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001951{
1952 int ret;
1953 struct buffer_head *bh = NULL;
1954
Joel Beckerfacdb772009-02-12 18:08:48 -08001955 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001956 if (ret) {
1957 mlog_errno(ret);
1958 goto out;
1959 }
1960
1961 *leaf_bh = bh;
1962out:
1963 return ret;
1964}
1965
1966/*
1967 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1968 *
1969 * Basically, we've moved stuff around at the bottom of the tree and
1970 * we need to fix up the extent records above the changes to reflect
1971 * the new changes.
1972 *
1973 * left_rec: the record on the left.
1974 * left_child_el: is the child list pointed to by left_rec
1975 * right_rec: the record to the right of left_rec
1976 * right_child_el: is the child list pointed to by right_rec
1977 *
1978 * By definition, this only works on interior nodes.
1979 */
1980static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1981 struct ocfs2_extent_list *left_child_el,
1982 struct ocfs2_extent_rec *right_rec,
1983 struct ocfs2_extent_list *right_child_el)
1984{
1985 u32 left_clusters, right_end;
1986
1987 /*
1988 * Interior nodes never have holes. Their cpos is the cpos of
1989 * the leftmost record in their child list. Their cluster
1990 * count covers the full theoretical range of their child list
1991 * - the range between their cpos and the cpos of the record
1992 * immediately to their right.
1993 */
1994 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08001995 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1996 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07001997 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1998 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1999 }
Mark Fashehdcd05382007-01-16 11:32:23 -08002000 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002001 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08002002
2003 /*
2004 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08002005 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08002006 * updating e_cpos to keep the same highest cluster count.
2007 */
2008 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002009 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08002010
2011 right_rec->e_cpos = left_rec->e_cpos;
2012 le32_add_cpu(&right_rec->e_cpos, left_clusters);
2013
2014 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002015 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08002016}
2017
2018/*
2019 * Adjust the adjacent root node records involved in a
2020 * rotation. left_el_blkno is passed in as a key so that we can easily
2021 * find it's index in the root list.
2022 */
2023static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
2024 struct ocfs2_extent_list *left_el,
2025 struct ocfs2_extent_list *right_el,
2026 u64 left_el_blkno)
2027{
2028 int i;
2029
2030 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
2031 le16_to_cpu(left_el->l_tree_depth));
2032
2033 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
2034 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
2035 break;
2036 }
2037
2038 /*
2039 * The path walking code should have never returned a root and
2040 * two paths which are not adjacent.
2041 */
2042 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2043
2044 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
2045 &root_el->l_recs[i + 1], right_el);
2046}
2047
2048/*
2049 * We've changed a leaf block (in right_path) and need to reflect that
2050 * change back up the subtree.
2051 *
2052 * This happens in multiple places:
2053 * - When we've moved an extent record from the left path leaf to the right
2054 * path leaf to make room for an empty extent in the left path leaf.
2055 * - When our insert into the right path leaf is at the leftmost edge
2056 * and requires an update of the path immediately to it's left. This
2057 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08002058 * - When we've adjusted the last extent record in the left path leaf and the
2059 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08002060 */
Joel Becker4619c732009-02-12 19:02:36 -08002061static void ocfs2_complete_edge_insert(handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08002062 struct ocfs2_path *left_path,
2063 struct ocfs2_path *right_path,
2064 int subtree_index)
2065{
2066 int ret, i, idx;
2067 struct ocfs2_extent_list *el, *left_el, *right_el;
2068 struct ocfs2_extent_rec *left_rec, *right_rec;
2069 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2070
2071 /*
2072 * Update the counts and position values within all the
2073 * interior nodes to reflect the leaf rotation we just did.
2074 *
2075 * The root node is handled below the loop.
2076 *
2077 * We begin the loop with right_el and left_el pointing to the
2078 * leaf lists and work our way up.
2079 *
2080 * NOTE: within this loop, left_el and right_el always refer
2081 * to the *child* lists.
2082 */
2083 left_el = path_leaf_el(left_path);
2084 right_el = path_leaf_el(right_path);
2085 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2086 mlog(0, "Adjust records at index %u\n", i);
2087
2088 /*
2089 * One nice property of knowing that all of these
2090 * nodes are below the root is that we only deal with
2091 * the leftmost right node record and the rightmost
2092 * left node record.
2093 */
2094 el = left_path->p_node[i].el;
2095 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2096 left_rec = &el->l_recs[idx];
2097
2098 el = right_path->p_node[i].el;
2099 right_rec = &el->l_recs[0];
2100
2101 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2102 right_el);
2103
2104 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2105 if (ret)
2106 mlog_errno(ret);
2107
2108 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2109 if (ret)
2110 mlog_errno(ret);
2111
2112 /*
2113 * Setup our list pointers now so that the current
2114 * parents become children in the next iteration.
2115 */
2116 left_el = left_path->p_node[i].el;
2117 right_el = right_path->p_node[i].el;
2118 }
2119
2120 /*
2121 * At the root node, adjust the two adjacent records which
2122 * begin our path to the leaves.
2123 */
2124
2125 el = left_path->p_node[subtree_index].el;
2126 left_el = left_path->p_node[subtree_index + 1].el;
2127 right_el = right_path->p_node[subtree_index + 1].el;
2128
2129 ocfs2_adjust_root_records(el, left_el, right_el,
2130 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2131
2132 root_bh = left_path->p_node[subtree_index].bh;
2133
2134 ret = ocfs2_journal_dirty(handle, root_bh);
2135 if (ret)
2136 mlog_errno(ret);
2137}
2138
Joel Becker5c601ab2009-02-12 19:10:13 -08002139static int ocfs2_rotate_subtree_right(handle_t *handle,
2140 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08002141 struct ocfs2_path *left_path,
2142 struct ocfs2_path *right_path,
2143 int subtree_index)
2144{
2145 int ret, i;
2146 struct buffer_head *right_leaf_bh;
2147 struct buffer_head *left_leaf_bh = NULL;
2148 struct buffer_head *root_bh;
2149 struct ocfs2_extent_list *right_el, *left_el;
2150 struct ocfs2_extent_rec move_rec;
2151
2152 left_leaf_bh = path_leaf_bh(left_path);
2153 left_el = path_leaf_el(left_path);
2154
2155 if (left_el->l_next_free_rec != left_el->l_count) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002156 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002157 "Inode %llu has non-full interior leaf node %llu"
2158 "(next free = %u)",
Joel Becker5c601ab2009-02-12 19:10:13 -08002159 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002160 (unsigned long long)left_leaf_bh->b_blocknr,
2161 le16_to_cpu(left_el->l_next_free_rec));
2162 return -EROFS;
2163 }
2164
2165 /*
2166 * This extent block may already have an empty record, so we
2167 * return early if so.
2168 */
2169 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2170 return 0;
2171
2172 root_bh = left_path->p_node[subtree_index].bh;
2173 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2174
Joel Becker5c601ab2009-02-12 19:10:13 -08002175 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002176 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002177 if (ret) {
2178 mlog_errno(ret);
2179 goto out;
2180 }
2181
2182 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002183 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002184 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002185 if (ret) {
2186 mlog_errno(ret);
2187 goto out;
2188 }
2189
Joel Becker5c601ab2009-02-12 19:10:13 -08002190 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002191 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002192 if (ret) {
2193 mlog_errno(ret);
2194 goto out;
2195 }
2196 }
2197
2198 right_leaf_bh = path_leaf_bh(right_path);
2199 right_el = path_leaf_el(right_path);
2200
2201 /* This is a code error, not a disk corruption. */
2202 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2203 "because rightmost leaf block %llu is empty\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002204 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002205 (unsigned long long)right_leaf_bh->b_blocknr);
2206
2207 ocfs2_create_empty_extent(right_el);
2208
2209 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2210 if (ret) {
2211 mlog_errno(ret);
2212 goto out;
2213 }
2214
2215 /* Do the copy now. */
2216 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2217 move_rec = left_el->l_recs[i];
2218 right_el->l_recs[0] = move_rec;
2219
2220 /*
2221 * Clear out the record we just copied and shift everything
2222 * over, leaving an empty extent in the left leaf.
2223 *
2224 * We temporarily subtract from next_free_rec so that the
2225 * shift will lose the tail record (which is now defunct).
2226 */
2227 le16_add_cpu(&left_el->l_next_free_rec, -1);
2228 ocfs2_shift_records_right(left_el);
2229 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2230 le16_add_cpu(&left_el->l_next_free_rec, 1);
2231
2232 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2233 if (ret) {
2234 mlog_errno(ret);
2235 goto out;
2236 }
2237
Joel Becker4619c732009-02-12 19:02:36 -08002238 ocfs2_complete_edge_insert(handle, left_path, right_path,
2239 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002240
2241out:
2242 return ret;
2243}
2244
2245/*
2246 * Given a full path, determine what cpos value would return us a path
2247 * containing the leaf immediately to the left of the current one.
2248 *
2249 * Will return zero if the path passed in is already the leftmost path.
2250 */
2251static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2252 struct ocfs2_path *path, u32 *cpos)
2253{
2254 int i, j, ret = 0;
2255 u64 blkno;
2256 struct ocfs2_extent_list *el;
2257
Mark Fashehe48edee2007-03-07 16:46:57 -08002258 BUG_ON(path->p_tree_depth == 0);
2259
Mark Fashehdcd05382007-01-16 11:32:23 -08002260 *cpos = 0;
2261
2262 blkno = path_leaf_bh(path)->b_blocknr;
2263
2264 /* Start at the tree node just above the leaf and work our way up. */
2265 i = path->p_tree_depth - 1;
2266 while (i >= 0) {
2267 el = path->p_node[i].el;
2268
2269 /*
2270 * Find the extent record just before the one in our
2271 * path.
2272 */
2273 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2274 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2275 if (j == 0) {
2276 if (i == 0) {
2277 /*
2278 * We've determined that the
2279 * path specified is already
2280 * the leftmost one - return a
2281 * cpos of zero.
2282 */
2283 goto out;
2284 }
2285 /*
2286 * The leftmost record points to our
2287 * leaf - we need to travel up the
2288 * tree one level.
2289 */
2290 goto next_node;
2291 }
2292
2293 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002294 *cpos = *cpos + ocfs2_rec_clusters(el,
2295 &el->l_recs[j - 1]);
2296 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002297 goto out;
2298 }
2299 }
2300
2301 /*
2302 * If we got here, we never found a valid node where
2303 * the tree indicated one should be.
2304 */
2305 ocfs2_error(sb,
2306 "Invalid extent tree at extent block %llu\n",
2307 (unsigned long long)blkno);
2308 ret = -EROFS;
2309 goto out;
2310
2311next_node:
2312 blkno = path->p_node[i].bh->b_blocknr;
2313 i--;
2314 }
2315
2316out:
2317 return ret;
2318}
2319
Mark Fasheh328d5752007-06-18 10:48:04 -07002320/*
2321 * Extend the transaction by enough credits to complete the rotation,
2322 * and still leave at least the original number of credits allocated
2323 * to this transaction.
2324 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002325static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002326 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002327 struct ocfs2_path *path)
2328{
Tao Mac18b8122009-08-18 11:44:07 +08002329 int ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002330 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002331
Tao Mac18b8122009-08-18 11:44:07 +08002332 if (handle->h_buffer_credits < credits) {
2333 ret = ocfs2_extend_trans(handle,
2334 credits - handle->h_buffer_credits);
2335 if (ret)
2336 return ret;
2337
2338 if (unlikely(handle->h_buffer_credits < credits))
2339 return ocfs2_extend_trans(handle, credits);
2340 }
Mark Fashehdcd05382007-01-16 11:32:23 -08002341
2342 return 0;
2343}
2344
2345/*
2346 * Trap the case where we're inserting into the theoretical range past
2347 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2348 * whose cpos is less than ours into the right leaf.
2349 *
2350 * It's only necessary to look at the rightmost record of the left
2351 * leaf because the logic that calls us should ensure that the
2352 * theoretical ranges in the path components above the leaves are
2353 * correct.
2354 */
2355static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2356 u32 insert_cpos)
2357{
2358 struct ocfs2_extent_list *left_el;
2359 struct ocfs2_extent_rec *rec;
2360 int next_free;
2361
2362 left_el = path_leaf_el(left_path);
2363 next_free = le16_to_cpu(left_el->l_next_free_rec);
2364 rec = &left_el->l_recs[next_free - 1];
2365
2366 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2367 return 1;
2368 return 0;
2369}
2370
Mark Fasheh328d5752007-06-18 10:48:04 -07002371static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2372{
2373 int next_free = le16_to_cpu(el->l_next_free_rec);
2374 unsigned int range;
2375 struct ocfs2_extent_rec *rec;
2376
2377 if (next_free == 0)
2378 return 0;
2379
2380 rec = &el->l_recs[0];
2381 if (ocfs2_is_empty_extent(rec)) {
2382 /* Empty list. */
2383 if (next_free == 1)
2384 return 0;
2385 rec = &el->l_recs[1];
2386 }
2387
2388 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2389 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2390 return 1;
2391 return 0;
2392}
2393
Mark Fashehdcd05382007-01-16 11:32:23 -08002394/*
2395 * Rotate all the records in a btree right one record, starting at insert_cpos.
2396 *
2397 * The path to the rightmost leaf should be passed in.
2398 *
2399 * The array is assumed to be large enough to hold an entire path (tree depth).
2400 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002401 * Upon successful return from this function:
Mark Fashehdcd05382007-01-16 11:32:23 -08002402 *
2403 * - The 'right_path' array will contain a path to the leaf block
2404 * whose range contains e_cpos.
2405 * - That leaf block will have a single empty extent in list index 0.
2406 * - In the case that the rotation requires a post-insert update,
2407 * *ret_left_path will contain a valid path which can be passed to
2408 * ocfs2_insert_path().
2409 */
Joel Becker1bbf0b82009-02-12 19:42:08 -08002410static int ocfs2_rotate_tree_right(handle_t *handle,
Joel Becker5c601ab2009-02-12 19:10:13 -08002411 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002412 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002413 u32 insert_cpos,
2414 struct ocfs2_path *right_path,
2415 struct ocfs2_path **ret_left_path)
2416{
Mark Fasheh328d5752007-06-18 10:48:04 -07002417 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002418 u32 cpos;
2419 struct ocfs2_path *left_path = NULL;
Joel Becker5c601ab2009-02-12 19:10:13 -08002420 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehdcd05382007-01-16 11:32:23 -08002421
2422 *ret_left_path = NULL;
2423
Joel Beckerffdd7a52008-10-17 22:32:01 -07002424 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002425 if (!left_path) {
2426 ret = -ENOMEM;
2427 mlog_errno(ret);
2428 goto out;
2429 }
2430
Joel Becker5c601ab2009-02-12 19:10:13 -08002431 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002432 if (ret) {
2433 mlog_errno(ret);
2434 goto out;
2435 }
2436
2437 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2438
2439 /*
2440 * What we want to do here is:
2441 *
2442 * 1) Start with the rightmost path.
2443 *
2444 * 2) Determine a path to the leaf block directly to the left
2445 * of that leaf.
2446 *
2447 * 3) Determine the 'subtree root' - the lowest level tree node
2448 * which contains a path to both leaves.
2449 *
2450 * 4) Rotate the subtree.
2451 *
2452 * 5) Find the next subtree by considering the left path to be
2453 * the new right path.
2454 *
2455 * The check at the top of this while loop also accepts
2456 * insert_cpos == cpos because cpos is only a _theoretical_
2457 * value to get us the left path - insert_cpos might very well
2458 * be filling that hole.
2459 *
2460 * Stop at a cpos of '0' because we either started at the
2461 * leftmost branch (i.e., a tree with one branch and a
2462 * rotation inside of it), or we've gone as far as we can in
2463 * rotating subtrees.
2464 */
2465 while (cpos && insert_cpos <= cpos) {
2466 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2467 insert_cpos, cpos);
2468
Joel Becker5c601ab2009-02-12 19:10:13 -08002469 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002470 if (ret) {
2471 mlog_errno(ret);
2472 goto out;
2473 }
2474
2475 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2476 path_leaf_bh(right_path),
Joel Becker5c601ab2009-02-12 19:10:13 -08002477 "Owner %llu: error during insert of %u "
Mark Fashehdcd05382007-01-16 11:32:23 -08002478 "(left path cpos %u) results in two identical "
2479 "paths ending at %llu\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002480 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2481 insert_cpos, cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08002482 (unsigned long long)
2483 path_leaf_bh(left_path)->b_blocknr);
2484
Mark Fasheh328d5752007-06-18 10:48:04 -07002485 if (split == SPLIT_NONE &&
2486 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002487 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002488
2489 /*
2490 * We've rotated the tree as much as we
2491 * should. The rest is up to
2492 * ocfs2_insert_path() to complete, after the
2493 * record insertion. We indicate this
2494 * situation by returning the left path.
2495 *
2496 * The reason we don't adjust the records here
2497 * before the record insert is that an error
2498 * later might break the rule where a parent
2499 * record e_cpos will reflect the actual
2500 * e_cpos of the 1st nonempty record of the
2501 * child list.
2502 */
2503 *ret_left_path = left_path;
2504 goto out_ret_path;
2505 }
2506
Joel Becker7dc02802009-02-12 19:20:13 -08002507 start = ocfs2_find_subtree_root(et, left_path, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002508
2509 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2510 start,
2511 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2512 right_path->p_tree_depth);
2513
2514 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002515 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002516 if (ret) {
2517 mlog_errno(ret);
2518 goto out;
2519 }
2520
Joel Becker5c601ab2009-02-12 19:10:13 -08002521 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002522 right_path, start);
2523 if (ret) {
2524 mlog_errno(ret);
2525 goto out;
2526 }
2527
Mark Fasheh328d5752007-06-18 10:48:04 -07002528 if (split != SPLIT_NONE &&
2529 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2530 insert_cpos)) {
2531 /*
2532 * A rotate moves the rightmost left leaf
2533 * record over to the leftmost right leaf
2534 * slot. If we're doing an extent split
2535 * instead of a real insert, then we have to
2536 * check that the extent to be split wasn't
2537 * just moved over. If it was, then we can
2538 * exit here, passing left_path back -
2539 * ocfs2_split_extent() is smart enough to
2540 * search both leaves.
2541 */
2542 *ret_left_path = left_path;
2543 goto out_ret_path;
2544 }
2545
Mark Fashehdcd05382007-01-16 11:32:23 -08002546 /*
2547 * There is no need to re-read the next right path
2548 * as we know that it'll be our current left
2549 * path. Optimize by copying values instead.
2550 */
2551 ocfs2_mv_path(right_path, left_path);
2552
Joel Becker5c601ab2009-02-12 19:10:13 -08002553 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002554 if (ret) {
2555 mlog_errno(ret);
2556 goto out;
2557 }
2558 }
2559
2560out:
2561 ocfs2_free_path(left_path);
2562
2563out_ret_path:
2564 return ret;
2565}
2566
Joel Becker09106ba2009-02-12 19:43:57 -08002567static int ocfs2_update_edge_lengths(handle_t *handle,
2568 struct ocfs2_extent_tree *et,
Tao Ma3c5e1062009-07-21 15:42:05 +08002569 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002570{
Tao Ma3c5e1062009-07-21 15:42:05 +08002571 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002572 struct ocfs2_extent_rec *rec;
2573 struct ocfs2_extent_list *el;
2574 struct ocfs2_extent_block *eb;
2575 u32 range;
2576
Tao Ma3c5e1062009-07-21 15:42:05 +08002577 /*
2578 * In normal tree rotation process, we will never touch the
2579 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2580 * doesn't reserve the credits for them either.
2581 *
2582 * But we do have a special case here which will update the rightmost
2583 * records for all the bh in the path.
2584 * So we have to allocate extra credits and access them.
2585 */
2586 ret = ocfs2_extend_trans(handle,
2587 handle->h_buffer_credits + subtree_index);
2588 if (ret) {
2589 mlog_errno(ret);
2590 goto out;
2591 }
2592
Joel Becker09106ba2009-02-12 19:43:57 -08002593 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002594 if (ret) {
2595 mlog_errno(ret);
2596 goto out;
2597 }
2598
Mark Fasheh328d5752007-06-18 10:48:04 -07002599 /* Path should always be rightmost. */
2600 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2601 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2602
2603 el = &eb->h_list;
2604 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2605 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2606 rec = &el->l_recs[idx];
2607 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2608
2609 for (i = 0; i < path->p_tree_depth; i++) {
2610 el = path->p_node[i].el;
2611 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2612 rec = &el->l_recs[idx];
2613
2614 rec->e_int_clusters = cpu_to_le32(range);
2615 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2616
2617 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2618 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002619out:
2620 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002621}
2622
Joel Becker6641b0c2009-02-12 18:57:52 -08002623static void ocfs2_unlink_path(handle_t *handle,
2624 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002625 struct ocfs2_cached_dealloc_ctxt *dealloc,
2626 struct ocfs2_path *path, int unlink_start)
2627{
2628 int ret, i;
2629 struct ocfs2_extent_block *eb;
2630 struct ocfs2_extent_list *el;
2631 struct buffer_head *bh;
2632
2633 for(i = unlink_start; i < path_num_items(path); i++) {
2634 bh = path->p_node[i].bh;
2635
2636 eb = (struct ocfs2_extent_block *)bh->b_data;
2637 /*
2638 * Not all nodes might have had their final count
2639 * decremented by the caller - handle this here.
2640 */
2641 el = &eb->h_list;
2642 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2643 mlog(ML_ERROR,
2644 "Inode %llu, attempted to remove extent block "
2645 "%llu with %u records\n",
Joel Becker6641b0c2009-02-12 18:57:52 -08002646 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07002647 (unsigned long long)le64_to_cpu(eb->h_blkno),
2648 le16_to_cpu(el->l_next_free_rec));
2649
2650 ocfs2_journal_dirty(handle, bh);
Joel Becker6641b0c2009-02-12 18:57:52 -08002651 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002652 continue;
2653 }
2654
2655 el->l_next_free_rec = 0;
2656 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2657
2658 ocfs2_journal_dirty(handle, bh);
2659
2660 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2661 if (ret)
2662 mlog_errno(ret);
2663
Joel Becker6641b0c2009-02-12 18:57:52 -08002664 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002665 }
2666}
2667
Joel Becker6641b0c2009-02-12 18:57:52 -08002668static void ocfs2_unlink_subtree(handle_t *handle,
2669 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002670 struct ocfs2_path *left_path,
2671 struct ocfs2_path *right_path,
2672 int subtree_index,
2673 struct ocfs2_cached_dealloc_ctxt *dealloc)
2674{
2675 int i;
2676 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2677 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2678 struct ocfs2_extent_list *el;
2679 struct ocfs2_extent_block *eb;
2680
2681 el = path_leaf_el(left_path);
2682
2683 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2684
2685 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2686 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2687 break;
2688
2689 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2690
2691 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2692 le16_add_cpu(&root_el->l_next_free_rec, -1);
2693
2694 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2695 eb->h_next_leaf_blk = 0;
2696
2697 ocfs2_journal_dirty(handle, root_bh);
2698 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2699
Joel Becker6641b0c2009-02-12 18:57:52 -08002700 ocfs2_unlink_path(handle, et, dealloc, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002701 subtree_index + 1);
2702}
2703
Joel Becker1e2dd632009-02-12 19:45:28 -08002704static int ocfs2_rotate_subtree_left(handle_t *handle,
2705 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002706 struct ocfs2_path *left_path,
2707 struct ocfs2_path *right_path,
2708 int subtree_index,
2709 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker1e2dd632009-02-12 19:45:28 -08002710 int *deleted)
Mark Fasheh328d5752007-06-18 10:48:04 -07002711{
2712 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002713 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002714 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2715 struct ocfs2_extent_block *eb;
2716
2717 *deleted = 0;
2718
2719 right_leaf_el = path_leaf_el(right_path);
2720 left_leaf_el = path_leaf_el(left_path);
2721 root_bh = left_path->p_node[subtree_index].bh;
2722 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2723
2724 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2725 return 0;
2726
2727 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2728 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2729 /*
2730 * It's legal for us to proceed if the right leaf is
2731 * the rightmost one and it has an empty extent. There
2732 * are two cases to handle - whether the leaf will be
2733 * empty after removal or not. If the leaf isn't empty
2734 * then just remove the empty extent up front. The
2735 * next block will handle empty leaves by flagging
2736 * them for unlink.
2737 *
2738 * Non rightmost leaves will throw -EAGAIN and the
2739 * caller can manually move the subtree and retry.
2740 */
2741
2742 if (eb->h_next_leaf_blk != 0ULL)
2743 return -EAGAIN;
2744
2745 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002746 ret = ocfs2_journal_access_eb(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002747 path_leaf_bh(right_path),
2748 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002749 if (ret) {
2750 mlog_errno(ret);
2751 goto out;
2752 }
2753
2754 ocfs2_remove_empty_extent(right_leaf_el);
2755 } else
2756 right_has_empty = 1;
2757 }
2758
2759 if (eb->h_next_leaf_blk == 0ULL &&
2760 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2761 /*
2762 * We have to update i_last_eb_blk during the meta
2763 * data delete.
2764 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08002765 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07002766 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002767 if (ret) {
2768 mlog_errno(ret);
2769 goto out;
2770 }
2771
2772 del_right_subtree = 1;
2773 }
2774
2775 /*
2776 * Getting here with an empty extent in the right path implies
2777 * that it's the rightmost path and will be deleted.
2778 */
2779 BUG_ON(right_has_empty && !del_right_subtree);
2780
Joel Becker1e2dd632009-02-12 19:45:28 -08002781 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002782 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002783 if (ret) {
2784 mlog_errno(ret);
2785 goto out;
2786 }
2787
2788 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002789 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002790 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002791 if (ret) {
2792 mlog_errno(ret);
2793 goto out;
2794 }
2795
Joel Becker1e2dd632009-02-12 19:45:28 -08002796 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002797 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002798 if (ret) {
2799 mlog_errno(ret);
2800 goto out;
2801 }
2802 }
2803
2804 if (!right_has_empty) {
2805 /*
2806 * Only do this if we're moving a real
2807 * record. Otherwise, the action is delayed until
2808 * after removal of the right path in which case we
2809 * can do a simple shift to remove the empty extent.
2810 */
2811 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2812 memset(&right_leaf_el->l_recs[0], 0,
2813 sizeof(struct ocfs2_extent_rec));
2814 }
2815 if (eb->h_next_leaf_blk == 0ULL) {
2816 /*
2817 * Move recs over to get rid of empty extent, decrease
2818 * next_free. This is allowed to remove the last
2819 * extent in our leaf (setting l_next_free_rec to
2820 * zero) - the delete code below won't care.
2821 */
2822 ocfs2_remove_empty_extent(right_leaf_el);
2823 }
2824
2825 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2826 if (ret)
2827 mlog_errno(ret);
2828 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2829 if (ret)
2830 mlog_errno(ret);
2831
2832 if (del_right_subtree) {
Joel Becker6641b0c2009-02-12 18:57:52 -08002833 ocfs2_unlink_subtree(handle, et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002834 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08002835 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08002836 left_path);
2837 if (ret) {
2838 mlog_errno(ret);
2839 goto out;
2840 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002841
2842 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002843 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002844
2845 /*
2846 * Removal of the extent in the left leaf was skipped
2847 * above so we could delete the right path
2848 * 1st.
2849 */
2850 if (right_has_empty)
2851 ocfs2_remove_empty_extent(left_leaf_el);
2852
Tao Mae7d4cb62008-08-18 17:38:44 +08002853 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002854 if (ret)
2855 mlog_errno(ret);
2856
2857 *deleted = 1;
2858 } else
Joel Becker4619c732009-02-12 19:02:36 -08002859 ocfs2_complete_edge_insert(handle, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002860 subtree_index);
2861
2862out:
2863 return ret;
2864}
2865
2866/*
2867 * Given a full path, determine what cpos value would return us a path
2868 * containing the leaf immediately to the right of the current one.
2869 *
2870 * Will return zero if the path passed in is already the rightmost path.
2871 *
2872 * This looks similar, but is subtly different to
2873 * ocfs2_find_cpos_for_left_leaf().
2874 */
Tao Ma38a04e42009-11-30 14:32:19 +08002875int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2876 struct ocfs2_path *path, u32 *cpos)
Mark Fasheh328d5752007-06-18 10:48:04 -07002877{
2878 int i, j, ret = 0;
2879 u64 blkno;
2880 struct ocfs2_extent_list *el;
2881
2882 *cpos = 0;
2883
2884 if (path->p_tree_depth == 0)
2885 return 0;
2886
2887 blkno = path_leaf_bh(path)->b_blocknr;
2888
2889 /* Start at the tree node just above the leaf and work our way up. */
2890 i = path->p_tree_depth - 1;
2891 while (i >= 0) {
2892 int next_free;
2893
2894 el = path->p_node[i].el;
2895
2896 /*
2897 * Find the extent record just after the one in our
2898 * path.
2899 */
2900 next_free = le16_to_cpu(el->l_next_free_rec);
2901 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2902 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2903 if (j == (next_free - 1)) {
2904 if (i == 0) {
2905 /*
2906 * We've determined that the
2907 * path specified is already
2908 * the rightmost one - return a
2909 * cpos of zero.
2910 */
2911 goto out;
2912 }
2913 /*
2914 * The rightmost record points to our
2915 * leaf - we need to travel up the
2916 * tree one level.
2917 */
2918 goto next_node;
2919 }
2920
2921 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2922 goto out;
2923 }
2924 }
2925
2926 /*
2927 * If we got here, we never found a valid node where
2928 * the tree indicated one should be.
2929 */
2930 ocfs2_error(sb,
2931 "Invalid extent tree at extent block %llu\n",
2932 (unsigned long long)blkno);
2933 ret = -EROFS;
2934 goto out;
2935
2936next_node:
2937 blkno = path->p_node[i].bh->b_blocknr;
2938 i--;
2939 }
2940
2941out:
2942 return ret;
2943}
2944
Joel Becker70f18c02009-02-13 02:09:31 -08002945static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2946 struct ocfs2_extent_tree *et,
Joel Becker13723d02008-10-17 19:25:01 -07002947 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002948{
2949 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002950 struct buffer_head *bh = path_leaf_bh(path);
2951 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002952
2953 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2954 return 0;
2955
Joel Becker70f18c02009-02-13 02:09:31 -08002956 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Joel Becker13723d02008-10-17 19:25:01 -07002957 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002958 if (ret) {
2959 mlog_errno(ret);
2960 goto out;
2961 }
2962
2963 ocfs2_remove_empty_extent(el);
2964
2965 ret = ocfs2_journal_dirty(handle, bh);
2966 if (ret)
2967 mlog_errno(ret);
2968
2969out:
2970 return ret;
2971}
2972
Joel Beckere46f74d2009-02-12 19:47:43 -08002973static int __ocfs2_rotate_tree_left(handle_t *handle,
2974 struct ocfs2_extent_tree *et,
2975 int orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07002976 struct ocfs2_path *path,
2977 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08002978 struct ocfs2_path **empty_extent_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002979{
2980 int ret, subtree_root, deleted;
2981 u32 right_cpos;
2982 struct ocfs2_path *left_path = NULL;
2983 struct ocfs2_path *right_path = NULL;
Joel Beckere46f74d2009-02-12 19:47:43 -08002984 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fasheh328d5752007-06-18 10:48:04 -07002985
2986 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2987
2988 *empty_extent_path = NULL;
2989
Joel Beckere46f74d2009-02-12 19:47:43 -08002990 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002991 if (ret) {
2992 mlog_errno(ret);
2993 goto out;
2994 }
2995
Joel Beckerffdd7a52008-10-17 22:32:01 -07002996 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002997 if (!left_path) {
2998 ret = -ENOMEM;
2999 mlog_errno(ret);
3000 goto out;
3001 }
3002
3003 ocfs2_cp_path(left_path, path);
3004
Joel Beckerffdd7a52008-10-17 22:32:01 -07003005 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003006 if (!right_path) {
3007 ret = -ENOMEM;
3008 mlog_errno(ret);
3009 goto out;
3010 }
3011
3012 while (right_cpos) {
Joel Beckerfacdb772009-02-12 18:08:48 -08003013 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003014 if (ret) {
3015 mlog_errno(ret);
3016 goto out;
3017 }
3018
Joel Becker7dc02802009-02-12 19:20:13 -08003019 subtree_root = ocfs2_find_subtree_root(et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003020 right_path);
3021
3022 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
3023 subtree_root,
3024 (unsigned long long)
3025 right_path->p_node[subtree_root].bh->b_blocknr,
3026 right_path->p_tree_depth);
3027
3028 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
3029 orig_credits, left_path);
3030 if (ret) {
3031 mlog_errno(ret);
3032 goto out;
3033 }
3034
Mark Fashehe8aed342007-12-03 16:43:01 -08003035 /*
3036 * Caller might still want to make changes to the
3037 * tree root, so re-add it to the journal here.
3038 */
Joel Beckere46f74d2009-02-12 19:47:43 -08003039 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003040 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08003041 if (ret) {
3042 mlog_errno(ret);
3043 goto out;
3044 }
3045
Joel Becker1e2dd632009-02-12 19:45:28 -08003046 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003047 right_path, subtree_root,
Joel Becker1e2dd632009-02-12 19:45:28 -08003048 dealloc, &deleted);
Mark Fasheh328d5752007-06-18 10:48:04 -07003049 if (ret == -EAGAIN) {
3050 /*
3051 * The rotation has to temporarily stop due to
3052 * the right subtree having an empty
3053 * extent. Pass it back to the caller for a
3054 * fixup.
3055 */
3056 *empty_extent_path = right_path;
3057 right_path = NULL;
3058 goto out;
3059 }
3060 if (ret) {
3061 mlog_errno(ret);
3062 goto out;
3063 }
3064
3065 /*
3066 * The subtree rotate might have removed records on
3067 * the rightmost edge. If so, then rotation is
3068 * complete.
3069 */
3070 if (deleted)
3071 break;
3072
3073 ocfs2_mv_path(left_path, right_path);
3074
Joel Beckere46f74d2009-02-12 19:47:43 -08003075 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003076 &right_cpos);
3077 if (ret) {
3078 mlog_errno(ret);
3079 goto out;
3080 }
3081 }
3082
3083out:
3084 ocfs2_free_path(right_path);
3085 ocfs2_free_path(left_path);
3086
3087 return ret;
3088}
3089
Joel Becker70f18c02009-02-13 02:09:31 -08003090static int ocfs2_remove_rightmost_path(handle_t *handle,
3091 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003092 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003093 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003094{
3095 int ret, subtree_index;
3096 u32 cpos;
3097 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003098 struct ocfs2_extent_block *eb;
3099 struct ocfs2_extent_list *el;
3100
Mark Fasheh328d5752007-06-18 10:48:04 -07003101
Joel Becker6136ca52009-02-12 19:32:43 -08003102 ret = ocfs2_et_sanity_check(et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003103 if (ret)
3104 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003105 /*
3106 * There's two ways we handle this depending on
3107 * whether path is the only existing one.
3108 */
3109 ret = ocfs2_extend_rotate_transaction(handle, 0,
3110 handle->h_buffer_credits,
3111 path);
3112 if (ret) {
3113 mlog_errno(ret);
3114 goto out;
3115 }
3116
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003117 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003118 if (ret) {
3119 mlog_errno(ret);
3120 goto out;
3121 }
3122
Joel Becker3d03a302009-02-12 17:49:26 -08003123 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3124 path, &cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003125 if (ret) {
3126 mlog_errno(ret);
3127 goto out;
3128 }
3129
3130 if (cpos) {
3131 /*
3132 * We have a path to the left of this one - it needs
3133 * an update too.
3134 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003135 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003136 if (!left_path) {
3137 ret = -ENOMEM;
3138 mlog_errno(ret);
3139 goto out;
3140 }
3141
Joel Beckerfacdb772009-02-12 18:08:48 -08003142 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003143 if (ret) {
3144 mlog_errno(ret);
3145 goto out;
3146 }
3147
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003148 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003149 if (ret) {
3150 mlog_errno(ret);
3151 goto out;
3152 }
3153
Joel Becker7dc02802009-02-12 19:20:13 -08003154 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003155
Joel Becker6641b0c2009-02-12 18:57:52 -08003156 ocfs2_unlink_subtree(handle, et, left_path, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003157 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08003158 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08003159 left_path);
3160 if (ret) {
3161 mlog_errno(ret);
3162 goto out;
3163 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003164
3165 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003166 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003167 } else {
3168 /*
3169 * 'path' is also the leftmost path which
3170 * means it must be the only one. This gets
3171 * handled differently because we want to
Joel Becker70f18c02009-02-13 02:09:31 -08003172 * revert the root back to having extents
Mark Fasheh328d5752007-06-18 10:48:04 -07003173 * in-line.
3174 */
Joel Becker6641b0c2009-02-12 18:57:52 -08003175 ocfs2_unlink_path(handle, et, dealloc, path, 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003176
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003177 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003178 el->l_tree_depth = 0;
3179 el->l_next_free_rec = 0;
3180 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3181
Joel Becker35dc0aa2008-08-20 16:25:06 -07003182 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003183 }
3184
3185 ocfs2_journal_dirty(handle, path_root_bh(path));
3186
3187out:
3188 ocfs2_free_path(left_path);
3189 return ret;
3190}
3191
3192/*
3193 * Left rotation of btree records.
3194 *
3195 * In many ways, this is (unsurprisingly) the opposite of right
3196 * rotation. We start at some non-rightmost path containing an empty
3197 * extent in the leaf block. The code works its way to the rightmost
3198 * path by rotating records to the left in every subtree.
3199 *
3200 * This is used by any code which reduces the number of extent records
3201 * in a leaf. After removal, an empty record should be placed in the
3202 * leftmost list position.
3203 *
3204 * This won't handle a length update of the rightmost path records if
3205 * the rightmost tree leaf record is removed so the caller is
3206 * responsible for detecting and correcting that.
3207 */
Joel Becker70f18c02009-02-13 02:09:31 -08003208static int ocfs2_rotate_tree_left(handle_t *handle,
3209 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003210 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003211 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003212{
3213 int ret, orig_credits = handle->h_buffer_credits;
3214 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3215 struct ocfs2_extent_block *eb;
3216 struct ocfs2_extent_list *el;
3217
3218 el = path_leaf_el(path);
3219 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3220 return 0;
3221
3222 if (path->p_tree_depth == 0) {
3223rightmost_no_delete:
3224 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003225 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003226 * it up front.
3227 */
Joel Becker70f18c02009-02-13 02:09:31 -08003228 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003229 if (ret)
3230 mlog_errno(ret);
3231 goto out;
3232 }
3233
3234 /*
3235 * Handle rightmost branch now. There's several cases:
3236 * 1) simple rotation leaving records in there. That's trivial.
3237 * 2) rotation requiring a branch delete - there's no more
3238 * records left. Two cases of this:
3239 * a) There are branches to the left.
3240 * b) This is also the leftmost (the only) branch.
3241 *
3242 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3243 * 2a) we need the left branch so that we can update it with the unlink
Joel Becker70f18c02009-02-13 02:09:31 -08003244 * 2b) we need to bring the root back to inline extents.
Mark Fasheh328d5752007-06-18 10:48:04 -07003245 */
3246
3247 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3248 el = &eb->h_list;
3249 if (eb->h_next_leaf_blk == 0) {
3250 /*
3251 * This gets a bit tricky if we're going to delete the
3252 * rightmost path. Get the other cases out of the way
3253 * 1st.
3254 */
3255 if (le16_to_cpu(el->l_next_free_rec) > 1)
3256 goto rightmost_no_delete;
3257
3258 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3259 ret = -EIO;
Joel Becker70f18c02009-02-13 02:09:31 -08003260 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3261 "Owner %llu has empty extent block at %llu",
3262 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07003263 (unsigned long long)le64_to_cpu(eb->h_blkno));
3264 goto out;
3265 }
3266
3267 /*
3268 * XXX: The caller can not trust "path" any more after
3269 * this as it will have been deleted. What do we do?
3270 *
3271 * In theory the rotate-for-merge code will never get
3272 * here because it'll always ask for a rotate in a
3273 * nonempty list.
3274 */
3275
Joel Becker70f18c02009-02-13 02:09:31 -08003276 ret = ocfs2_remove_rightmost_path(handle, et, path,
3277 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003278 if (ret)
3279 mlog_errno(ret);
3280 goto out;
3281 }
3282
3283 /*
3284 * Now we can loop, remembering the path we get from -EAGAIN
3285 * and restarting from there.
3286 */
3287try_rotate:
Joel Beckere46f74d2009-02-12 19:47:43 -08003288 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3289 dealloc, &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003290 if (ret && ret != -EAGAIN) {
3291 mlog_errno(ret);
3292 goto out;
3293 }
3294
3295 while (ret == -EAGAIN) {
3296 tmp_path = restart_path;
3297 restart_path = NULL;
3298
Joel Beckere46f74d2009-02-12 19:47:43 -08003299 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07003300 tmp_path, dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08003301 &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003302 if (ret && ret != -EAGAIN) {
3303 mlog_errno(ret);
3304 goto out;
3305 }
3306
3307 ocfs2_free_path(tmp_path);
3308 tmp_path = NULL;
3309
3310 if (ret == 0)
3311 goto try_rotate;
3312 }
3313
3314out:
3315 ocfs2_free_path(tmp_path);
3316 ocfs2_free_path(restart_path);
3317 return ret;
3318}
3319
3320static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3321 int index)
3322{
3323 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3324 unsigned int size;
3325
3326 if (rec->e_leaf_clusters == 0) {
3327 /*
3328 * We consumed all of the merged-from record. An empty
3329 * extent cannot exist anywhere but the 1st array
3330 * position, so move things over if the merged-from
3331 * record doesn't occupy that position.
3332 *
3333 * This creates a new empty extent so the caller
3334 * should be smart enough to have removed any existing
3335 * ones.
3336 */
3337 if (index > 0) {
3338 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3339 size = index * sizeof(struct ocfs2_extent_rec);
3340 memmove(&el->l_recs[1], &el->l_recs[0], size);
3341 }
3342
3343 /*
3344 * Always memset - the caller doesn't check whether it
3345 * created an empty extent, so there could be junk in
3346 * the other fields.
3347 */
3348 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3349 }
3350}
3351
Joel Becker4fe82c32009-02-13 02:16:08 -08003352static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003353 struct ocfs2_path *left_path,
3354 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003355{
3356 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003357 u32 right_cpos;
3358 struct ocfs2_path *right_path = NULL;
3359 struct ocfs2_extent_list *left_el;
3360
3361 *ret_right_path = NULL;
3362
3363 /* This function shouldn't be called for non-trees. */
3364 BUG_ON(left_path->p_tree_depth == 0);
3365
3366 left_el = path_leaf_el(left_path);
3367 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3368
Joel Becker4fe82c32009-02-13 02:16:08 -08003369 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3370 left_path, &right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003371 if (ret) {
3372 mlog_errno(ret);
3373 goto out;
3374 }
3375
3376 /* This function shouldn't be called for the rightmost leaf. */
3377 BUG_ON(right_cpos == 0);
3378
Joel Beckerffdd7a52008-10-17 22:32:01 -07003379 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003380 if (!right_path) {
3381 ret = -ENOMEM;
3382 mlog_errno(ret);
3383 goto out;
3384 }
3385
Joel Becker4fe82c32009-02-13 02:16:08 -08003386 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003387 if (ret) {
3388 mlog_errno(ret);
3389 goto out;
3390 }
3391
3392 *ret_right_path = right_path;
3393out:
3394 if (ret)
3395 ocfs2_free_path(right_path);
3396 return ret;
3397}
3398
3399/*
3400 * Remove split_rec clusters from the record at index and merge them
3401 * onto the beginning of the record "next" to it.
3402 * For index < l_count - 1, the next means the extent rec at index + 1.
3403 * For index == l_count - 1, the "next" means the 1st extent rec of the
3404 * next extent block.
3405 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003406static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003407 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003408 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003409 struct ocfs2_extent_rec *split_rec,
3410 int index)
3411{
3412 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003413 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3414 struct ocfs2_extent_rec *left_rec;
3415 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003416 struct ocfs2_extent_list *right_el;
3417 struct ocfs2_path *right_path = NULL;
3418 int subtree_index = 0;
3419 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3420 struct buffer_head *bh = path_leaf_bh(left_path);
3421 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003422
3423 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003424 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003425
Al Viro9d8df6a2008-05-21 06:32:11 +01003426 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003427 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3428 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003429 ret = ocfs2_get_right_path(et, left_path, &right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003430 if (ret) {
3431 mlog_errno(ret);
3432 goto out;
3433 }
3434
3435 right_el = path_leaf_el(right_path);
3436 next_free = le16_to_cpu(right_el->l_next_free_rec);
3437 BUG_ON(next_free <= 0);
3438 right_rec = &right_el->l_recs[0];
3439 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003440 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003441 right_rec = &right_el->l_recs[1];
3442 }
3443
3444 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3445 le16_to_cpu(left_rec->e_leaf_clusters) !=
3446 le32_to_cpu(right_rec->e_cpos));
3447
Joel Becker7dc02802009-02-12 19:20:13 -08003448 subtree_index = ocfs2_find_subtree_root(et, left_path,
3449 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003450
3451 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3452 handle->h_buffer_credits,
3453 right_path);
3454 if (ret) {
3455 mlog_errno(ret);
3456 goto out;
3457 }
3458
3459 root_bh = left_path->p_node[subtree_index].bh;
3460 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3461
Joel Becker7dc02802009-02-12 19:20:13 -08003462 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003463 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003464 if (ret) {
3465 mlog_errno(ret);
3466 goto out;
3467 }
3468
3469 for (i = subtree_index + 1;
3470 i < path_num_items(right_path); i++) {
Joel Becker7dc02802009-02-12 19:20:13 -08003471 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003472 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003473 if (ret) {
3474 mlog_errno(ret);
3475 goto out;
3476 }
3477
Joel Becker7dc02802009-02-12 19:20:13 -08003478 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003479 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003480 if (ret) {
3481 mlog_errno(ret);
3482 goto out;
3483 }
3484 }
3485
3486 } else {
3487 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3488 right_rec = &el->l_recs[index + 1];
3489 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003490
Joel Becker7dc02802009-02-12 19:20:13 -08003491 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
Joel Becker13723d02008-10-17 19:25:01 -07003492 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003493 if (ret) {
3494 mlog_errno(ret);
3495 goto out;
3496 }
3497
3498 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3499
3500 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3501 le64_add_cpu(&right_rec->e_blkno,
Joel Becker7dc02802009-02-12 19:20:13 -08003502 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3503 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003504 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3505
3506 ocfs2_cleanup_merge(el, index);
3507
3508 ret = ocfs2_journal_dirty(handle, bh);
3509 if (ret)
3510 mlog_errno(ret);
3511
Tao Ma677b9752008-01-30 14:21:05 +08003512 if (right_path) {
3513 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3514 if (ret)
3515 mlog_errno(ret);
3516
Joel Becker4619c732009-02-12 19:02:36 -08003517 ocfs2_complete_edge_insert(handle, left_path, right_path,
3518 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003519 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003520out:
Tao Ma677b9752008-01-30 14:21:05 +08003521 if (right_path)
3522 ocfs2_free_path(right_path);
3523 return ret;
3524}
3525
Joel Becker4fe82c32009-02-13 02:16:08 -08003526static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003527 struct ocfs2_path *right_path,
3528 struct ocfs2_path **ret_left_path)
3529{
3530 int ret;
3531 u32 left_cpos;
3532 struct ocfs2_path *left_path = NULL;
3533
3534 *ret_left_path = NULL;
3535
3536 /* This function shouldn't be called for non-trees. */
3537 BUG_ON(right_path->p_tree_depth == 0);
3538
Joel Becker4fe82c32009-02-13 02:16:08 -08003539 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
Tao Ma677b9752008-01-30 14:21:05 +08003540 right_path, &left_cpos);
3541 if (ret) {
3542 mlog_errno(ret);
3543 goto out;
3544 }
3545
3546 /* This function shouldn't be called for the leftmost leaf. */
3547 BUG_ON(left_cpos == 0);
3548
Joel Beckerffdd7a52008-10-17 22:32:01 -07003549 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003550 if (!left_path) {
3551 ret = -ENOMEM;
3552 mlog_errno(ret);
3553 goto out;
3554 }
3555
Joel Becker4fe82c32009-02-13 02:16:08 -08003556 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003557 if (ret) {
3558 mlog_errno(ret);
3559 goto out;
3560 }
3561
3562 *ret_left_path = left_path;
3563out:
3564 if (ret)
3565 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003566 return ret;
3567}
3568
3569/*
3570 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003571 * onto the tail of the record "before" it.
3572 * For index > 0, the "before" means the extent rec at index - 1.
3573 *
3574 * For index == 0, the "before" means the last record of the previous
3575 * extent block. And there is also a situation that we may need to
3576 * remove the rightmost leaf extent block in the right_path and change
3577 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003578 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003579static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003580 handle_t *handle,
Joel Becker4fe82c32009-02-13 02:16:08 -08003581 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003582 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003583 struct ocfs2_cached_dealloc_ctxt *dealloc,
3584 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003585{
Tao Ma677b9752008-01-30 14:21:05 +08003586 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003587 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3588 struct ocfs2_extent_rec *left_rec;
3589 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003590 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3591 struct buffer_head *bh = path_leaf_bh(right_path);
3592 struct buffer_head *root_bh = NULL;
3593 struct ocfs2_path *left_path = NULL;
3594 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003595
Tao Ma677b9752008-01-30 14:21:05 +08003596 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003597
Mark Fasheh328d5752007-06-18 10:48:04 -07003598 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003599 if (index == 0) {
3600 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003601 ret = ocfs2_get_left_path(et, right_path, &left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003602 if (ret) {
3603 mlog_errno(ret);
3604 goto out;
3605 }
3606
3607 left_el = path_leaf_el(left_path);
3608 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3609 le16_to_cpu(left_el->l_count));
3610
3611 left_rec = &left_el->l_recs[
3612 le16_to_cpu(left_el->l_next_free_rec) - 1];
3613 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3614 le16_to_cpu(left_rec->e_leaf_clusters) !=
3615 le32_to_cpu(split_rec->e_cpos));
3616
Joel Becker7dc02802009-02-12 19:20:13 -08003617 subtree_index = ocfs2_find_subtree_root(et, left_path,
3618 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003619
3620 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3621 handle->h_buffer_credits,
3622 left_path);
3623 if (ret) {
3624 mlog_errno(ret);
3625 goto out;
3626 }
3627
3628 root_bh = left_path->p_node[subtree_index].bh;
3629 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3630
Joel Becker4fe82c32009-02-13 02:16:08 -08003631 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003632 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003633 if (ret) {
3634 mlog_errno(ret);
3635 goto out;
3636 }
3637
3638 for (i = subtree_index + 1;
3639 i < path_num_items(right_path); i++) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003640 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003641 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003642 if (ret) {
3643 mlog_errno(ret);
3644 goto out;
3645 }
3646
Joel Becker4fe82c32009-02-13 02:16:08 -08003647 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003648 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003649 if (ret) {
3650 mlog_errno(ret);
3651 goto out;
3652 }
3653 }
3654 } else {
3655 left_rec = &el->l_recs[index - 1];
3656 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3657 has_empty_extent = 1;
3658 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003659
Joel Becker4fe82c32009-02-13 02:16:08 -08003660 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Tao Ma9047bea2009-01-05 14:45:24 +08003661 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003662 if (ret) {
3663 mlog_errno(ret);
3664 goto out;
3665 }
3666
3667 if (has_empty_extent && index == 1) {
3668 /*
3669 * The easy case - we can just plop the record right in.
3670 */
3671 *left_rec = *split_rec;
3672
3673 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003674 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003675 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003676
3677 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3678 le64_add_cpu(&right_rec->e_blkno,
Joel Becker4fe82c32009-02-13 02:16:08 -08003679 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3680 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003681 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3682
3683 ocfs2_cleanup_merge(el, index);
3684
3685 ret = ocfs2_journal_dirty(handle, bh);
3686 if (ret)
3687 mlog_errno(ret);
3688
Tao Ma677b9752008-01-30 14:21:05 +08003689 if (left_path) {
3690 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3691 if (ret)
3692 mlog_errno(ret);
3693
3694 /*
3695 * In the situation that the right_rec is empty and the extent
3696 * block is empty also, ocfs2_complete_edge_insert can't handle
3697 * it and we need to delete the right extent block.
3698 */
3699 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3700 le16_to_cpu(el->l_next_free_rec) == 1) {
3701
Joel Becker70f18c02009-02-13 02:09:31 -08003702 ret = ocfs2_remove_rightmost_path(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003703 right_path,
Joel Becker70f18c02009-02-13 02:09:31 -08003704 dealloc);
Tao Ma677b9752008-01-30 14:21:05 +08003705 if (ret) {
3706 mlog_errno(ret);
3707 goto out;
3708 }
3709
3710 /* Now the rightmost extent block has been deleted.
3711 * So we use the new rightmost path.
3712 */
3713 ocfs2_mv_path(right_path, left_path);
3714 left_path = NULL;
3715 } else
Joel Becker4619c732009-02-12 19:02:36 -08003716 ocfs2_complete_edge_insert(handle, left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003717 right_path, subtree_index);
3718 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003719out:
Tao Ma677b9752008-01-30 14:21:05 +08003720 if (left_path)
3721 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003722 return ret;
3723}
3724
Joel Beckerc495dd22009-02-13 02:19:11 -08003725static int ocfs2_try_to_merge_extent(handle_t *handle,
3726 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003727 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003728 int split_index,
3729 struct ocfs2_extent_rec *split_rec,
3730 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckerc495dd22009-02-13 02:19:11 -08003731 struct ocfs2_merge_ctxt *ctxt)
Mark Fasheh328d5752007-06-18 10:48:04 -07003732{
Tao Mao518d7262007-08-28 17:25:35 -07003733 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003734 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003735 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3736
3737 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3738
Tao Mao518d7262007-08-28 17:25:35 -07003739 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3740 /*
3741 * The merge code will need to create an empty
3742 * extent to take the place of the newly
3743 * emptied slot. Remove any pre-existing empty
3744 * extents - having more than one in a leaf is
3745 * illegal.
3746 */
Joel Becker70f18c02009-02-13 02:09:31 -08003747 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Tao Mao518d7262007-08-28 17:25:35 -07003748 if (ret) {
3749 mlog_errno(ret);
3750 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003751 }
Tao Mao518d7262007-08-28 17:25:35 -07003752 split_index--;
3753 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003754 }
3755
3756 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3757 /*
3758 * Left-right contig implies this.
3759 */
3760 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003761
3762 /*
3763 * Since the leftright insert always covers the entire
3764 * extent, this call will delete the insert record
3765 * entirely, resulting in an empty extent record added to
3766 * the extent block.
3767 *
3768 * Since the adding of an empty extent shifts
3769 * everything back to the right, there's no need to
3770 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003771 *
3772 * When the split_index is zero, we need to merge it to the
3773 * prevoius extent block. It is more efficient and easier
3774 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003775 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003776 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003777 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003778 if (ret) {
3779 mlog_errno(ret);
3780 goto out;
3781 }
3782
3783 /*
3784 * We can only get this from logic error above.
3785 */
3786 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3787
Tao Ma677b9752008-01-30 14:21:05 +08003788 /* The merge left us with an empty extent, remove it. */
Joel Becker70f18c02009-02-13 02:09:31 -08003789 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003790 if (ret) {
3791 mlog_errno(ret);
3792 goto out;
3793 }
Tao Ma677b9752008-01-30 14:21:05 +08003794
Mark Fasheh328d5752007-06-18 10:48:04 -07003795 rec = &el->l_recs[split_index];
3796
3797 /*
3798 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003799 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003800 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003801 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3802 dealloc, split_index);
Tao Ma677b9752008-01-30 14:21:05 +08003803
Mark Fasheh328d5752007-06-18 10:48:04 -07003804 if (ret) {
3805 mlog_errno(ret);
3806 goto out;
3807 }
3808
Joel Becker70f18c02009-02-13 02:09:31 -08003809 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003810 /*
3811 * Error from this last rotate is not critical, so
3812 * print but don't bubble it up.
3813 */
3814 if (ret)
3815 mlog_errno(ret);
3816 ret = 0;
3817 } else {
3818 /*
3819 * Merge a record to the left or right.
3820 *
3821 * 'contig_type' is relative to the existing record,
3822 * so for example, if we're "right contig", it's to
3823 * the record on the left (hence the left merge).
3824 */
3825 if (ctxt->c_contig_type == CONTIG_RIGHT) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003826 ret = ocfs2_merge_rec_left(path, handle, et,
3827 split_rec, dealloc,
Mark Fasheh328d5752007-06-18 10:48:04 -07003828 split_index);
3829 if (ret) {
3830 mlog_errno(ret);
3831 goto out;
3832 }
3833 } else {
Joel Becker4fe82c32009-02-13 02:16:08 -08003834 ret = ocfs2_merge_rec_right(path, handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003835 et, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003836 split_index);
3837 if (ret) {
3838 mlog_errno(ret);
3839 goto out;
3840 }
3841 }
3842
3843 if (ctxt->c_split_covers_rec) {
3844 /*
3845 * The merge may have left an empty extent in
3846 * our leaf. Try to rotate it away.
3847 */
Joel Becker70f18c02009-02-13 02:09:31 -08003848 ret = ocfs2_rotate_tree_left(handle, et, path,
3849 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003850 if (ret)
3851 mlog_errno(ret);
3852 ret = 0;
3853 }
3854 }
3855
3856out:
3857 return ret;
3858}
3859
3860static void ocfs2_subtract_from_rec(struct super_block *sb,
3861 enum ocfs2_split_type split,
3862 struct ocfs2_extent_rec *rec,
3863 struct ocfs2_extent_rec *split_rec)
3864{
3865 u64 len_blocks;
3866
3867 len_blocks = ocfs2_clusters_to_blocks(sb,
3868 le16_to_cpu(split_rec->e_leaf_clusters));
3869
3870 if (split == SPLIT_LEFT) {
3871 /*
3872 * Region is on the left edge of the existing
3873 * record.
3874 */
3875 le32_add_cpu(&rec->e_cpos,
3876 le16_to_cpu(split_rec->e_leaf_clusters));
3877 le64_add_cpu(&rec->e_blkno, len_blocks);
3878 le16_add_cpu(&rec->e_leaf_clusters,
3879 -le16_to_cpu(split_rec->e_leaf_clusters));
3880 } else {
3881 /*
3882 * Region is on the right edge of the existing
3883 * record.
3884 */
3885 le16_add_cpu(&rec->e_leaf_clusters,
3886 -le16_to_cpu(split_rec->e_leaf_clusters));
3887 }
3888}
3889
Mark Fashehdcd05382007-01-16 11:32:23 -08003890/*
3891 * Do the final bits of extent record insertion at the target leaf
3892 * list. If this leaf is part of an allocation tree, it is assumed
3893 * that the tree above has been prepared.
3894 */
Joel Beckerd5628622009-02-13 02:54:36 -08003895static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3896 struct ocfs2_extent_rec *insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08003897 struct ocfs2_extent_list *el,
Joel Beckerd5628622009-02-13 02:54:36 -08003898 struct ocfs2_insert_type *insert)
Mark Fashehdcd05382007-01-16 11:32:23 -08003899{
3900 int i = insert->ins_contig_index;
3901 unsigned int range;
3902 struct ocfs2_extent_rec *rec;
3903
Mark Fashehe48edee2007-03-07 16:46:57 -08003904 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003905
Mark Fasheh328d5752007-06-18 10:48:04 -07003906 if (insert->ins_split != SPLIT_NONE) {
3907 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3908 BUG_ON(i == -1);
3909 rec = &el->l_recs[i];
Joel Beckerd5628622009-02-13 02:54:36 -08003910 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3911 insert->ins_split, rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003912 insert_rec);
3913 goto rotate;
3914 }
3915
Mark Fashehdcd05382007-01-16 11:32:23 -08003916 /*
3917 * Contiguous insert - either left or right.
3918 */
3919 if (insert->ins_contig != CONTIG_NONE) {
3920 rec = &el->l_recs[i];
3921 if (insert->ins_contig == CONTIG_LEFT) {
3922 rec->e_blkno = insert_rec->e_blkno;
3923 rec->e_cpos = insert_rec->e_cpos;
3924 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003925 le16_add_cpu(&rec->e_leaf_clusters,
3926 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003927 return;
3928 }
3929
3930 /*
3931 * Handle insert into an empty leaf.
3932 */
3933 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3934 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3935 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3936 el->l_recs[0] = *insert_rec;
3937 el->l_next_free_rec = cpu_to_le16(1);
3938 return;
3939 }
3940
3941 /*
3942 * Appending insert.
3943 */
3944 if (insert->ins_appending == APPEND_TAIL) {
3945 i = le16_to_cpu(el->l_next_free_rec) - 1;
3946 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003947 range = le32_to_cpu(rec->e_cpos)
3948 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003949 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3950
3951 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3952 le16_to_cpu(el->l_count),
Joel Beckerd5628622009-02-13 02:54:36 -08003953 "owner %llu, depth %u, count %u, next free %u, "
Mark Fashehdcd05382007-01-16 11:32:23 -08003954 "rec.cpos %u, rec.clusters %u, "
3955 "insert.cpos %u, insert.clusters %u\n",
Joel Beckerd5628622009-02-13 02:54:36 -08003956 ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08003957 le16_to_cpu(el->l_tree_depth),
3958 le16_to_cpu(el->l_count),
3959 le16_to_cpu(el->l_next_free_rec),
3960 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003961 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003962 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003963 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003964 i++;
3965 el->l_recs[i] = *insert_rec;
3966 le16_add_cpu(&el->l_next_free_rec, 1);
3967 return;
3968 }
3969
Mark Fasheh328d5752007-06-18 10:48:04 -07003970rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003971 /*
3972 * Ok, we have to rotate.
3973 *
3974 * At this point, it is safe to assume that inserting into an
3975 * empty leaf and appending to a leaf have both been handled
3976 * above.
3977 *
3978 * This leaf needs to have space, either by the empty 1st
3979 * extent record, or by virtue of an l_next_rec < l_count.
3980 */
3981 ocfs2_rotate_leaf(el, insert_rec);
3982}
3983
Joel Beckerd401dc12009-02-13 02:24:10 -08003984static void ocfs2_adjust_rightmost_records(handle_t *handle,
3985 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003986 struct ocfs2_path *path,
3987 struct ocfs2_extent_rec *insert_rec)
3988{
3989 int ret, i, next_free;
3990 struct buffer_head *bh;
3991 struct ocfs2_extent_list *el;
3992 struct ocfs2_extent_rec *rec;
3993
3994 /*
3995 * Update everything except the leaf block.
3996 */
3997 for (i = 0; i < path->p_tree_depth; i++) {
3998 bh = path->p_node[i].bh;
3999 el = path->p_node[i].el;
4000
4001 next_free = le16_to_cpu(el->l_next_free_rec);
4002 if (next_free == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004003 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
4004 "Owner %llu has a bad extent list",
4005 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fasheh328d5752007-06-18 10:48:04 -07004006 ret = -EIO;
4007 return;
4008 }
4009
4010 rec = &el->l_recs[next_free - 1];
4011
4012 rec->e_int_clusters = insert_rec->e_cpos;
4013 le32_add_cpu(&rec->e_int_clusters,
4014 le16_to_cpu(insert_rec->e_leaf_clusters));
4015 le32_add_cpu(&rec->e_int_clusters,
4016 -le32_to_cpu(rec->e_cpos));
4017
4018 ret = ocfs2_journal_dirty(handle, bh);
4019 if (ret)
4020 mlog_errno(ret);
4021
4022 }
4023}
4024
Joel Beckerd401dc12009-02-13 02:24:10 -08004025static int ocfs2_append_rec_to_path(handle_t *handle,
4026 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004027 struct ocfs2_extent_rec *insert_rec,
4028 struct ocfs2_path *right_path,
4029 struct ocfs2_path **ret_left_path)
4030{
Mark Fasheh328d5752007-06-18 10:48:04 -07004031 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08004032 struct ocfs2_extent_list *el;
4033 struct ocfs2_path *left_path = NULL;
4034
4035 *ret_left_path = NULL;
4036
4037 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08004038 * This shouldn't happen for non-trees. The extent rec cluster
4039 * count manipulation below only works for interior nodes.
4040 */
4041 BUG_ON(right_path->p_tree_depth == 0);
4042
4043 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08004044 * If our appending insert is at the leftmost edge of a leaf,
4045 * then we might need to update the rightmost records of the
4046 * neighboring path.
4047 */
4048 el = path_leaf_el(right_path);
4049 next_free = le16_to_cpu(el->l_next_free_rec);
4050 if (next_free == 0 ||
4051 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
4052 u32 left_cpos;
4053
Joel Beckerd401dc12009-02-13 02:24:10 -08004054 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
4055 right_path, &left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004056 if (ret) {
4057 mlog_errno(ret);
4058 goto out;
4059 }
4060
4061 mlog(0, "Append may need a left path update. cpos: %u, "
4062 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
4063 left_cpos);
4064
4065 /*
4066 * No need to worry if the append is already in the
4067 * leftmost leaf.
4068 */
4069 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004070 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004071 if (!left_path) {
4072 ret = -ENOMEM;
4073 mlog_errno(ret);
4074 goto out;
4075 }
4076
Joel Beckerd401dc12009-02-13 02:24:10 -08004077 ret = ocfs2_find_path(et->et_ci, left_path,
Joel Beckerfacdb772009-02-12 18:08:48 -08004078 left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004079 if (ret) {
4080 mlog_errno(ret);
4081 goto out;
4082 }
4083
4084 /*
4085 * ocfs2_insert_path() will pass the left_path to the
4086 * journal for us.
4087 */
4088 }
4089 }
4090
Joel Beckerd401dc12009-02-13 02:24:10 -08004091 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004092 if (ret) {
4093 mlog_errno(ret);
4094 goto out;
4095 }
4096
Joel Beckerd401dc12009-02-13 02:24:10 -08004097 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004098
4099 *ret_left_path = left_path;
4100 ret = 0;
4101out:
4102 if (ret != 0)
4103 ocfs2_free_path(left_path);
4104
4105 return ret;
4106}
4107
Joel Beckerc38e52b2009-02-13 02:56:23 -08004108static void ocfs2_split_record(struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004109 struct ocfs2_path *left_path,
4110 struct ocfs2_path *right_path,
4111 struct ocfs2_extent_rec *split_rec,
4112 enum ocfs2_split_type split)
4113{
4114 int index;
4115 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4116 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4117 struct ocfs2_extent_rec *rec, *tmprec;
4118
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004119 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004120 if (left_path)
4121 left_el = path_leaf_el(left_path);
4122
4123 el = right_el;
4124 insert_el = right_el;
4125 index = ocfs2_search_extent_list(el, cpos);
4126 if (index != -1) {
4127 if (index == 0 && left_path) {
4128 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4129
4130 /*
4131 * This typically means that the record
4132 * started in the left path but moved to the
4133 * right as a result of rotation. We either
4134 * move the existing record to the left, or we
4135 * do the later insert there.
4136 *
4137 * In this case, the left path should always
4138 * exist as the rotate code will have passed
4139 * it back for a post-insert update.
4140 */
4141
4142 if (split == SPLIT_LEFT) {
4143 /*
4144 * It's a left split. Since we know
4145 * that the rotate code gave us an
4146 * empty extent in the left path, we
4147 * can just do the insert there.
4148 */
4149 insert_el = left_el;
4150 } else {
4151 /*
4152 * Right split - we have to move the
4153 * existing record over to the left
4154 * leaf. The insert will be into the
4155 * newly created empty extent in the
4156 * right leaf.
4157 */
4158 tmprec = &right_el->l_recs[index];
4159 ocfs2_rotate_leaf(left_el, tmprec);
4160 el = left_el;
4161
4162 memset(tmprec, 0, sizeof(*tmprec));
4163 index = ocfs2_search_extent_list(left_el, cpos);
4164 BUG_ON(index == -1);
4165 }
4166 }
4167 } else {
4168 BUG_ON(!left_path);
4169 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4170 /*
4171 * Left path is easy - we can just allow the insert to
4172 * happen.
4173 */
4174 el = left_el;
4175 insert_el = left_el;
4176 index = ocfs2_search_extent_list(el, cpos);
4177 BUG_ON(index == -1);
4178 }
4179
4180 rec = &el->l_recs[index];
Joel Beckerc38e52b2009-02-13 02:56:23 -08004181 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4182 split, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004183 ocfs2_rotate_leaf(insert_el, split_rec);
4184}
4185
Mark Fashehdcd05382007-01-16 11:32:23 -08004186/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004187 * This function only does inserts on an allocation b-tree. For tree
4188 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004189 *
4190 * right_path is the path we want to do the actual insert
4191 * in. left_path should only be passed in if we need to update that
4192 * portion of the tree after an edge insert.
4193 */
Joel Becker3505bec2009-02-13 02:57:58 -08004194static int ocfs2_insert_path(handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08004195 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004196 struct ocfs2_path *left_path,
4197 struct ocfs2_path *right_path,
4198 struct ocfs2_extent_rec *insert_rec,
4199 struct ocfs2_insert_type *insert)
4200{
4201 int ret, subtree_index;
4202 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004203
Mark Fashehdcd05382007-01-16 11:32:23 -08004204 if (left_path) {
4205 int credits = handle->h_buffer_credits;
4206
4207 /*
4208 * There's a chance that left_path got passed back to
4209 * us without being accounted for in the
4210 * journal. Extend our transaction here to be sure we
4211 * can change those blocks.
4212 */
4213 credits += left_path->p_tree_depth;
4214
4215 ret = ocfs2_extend_trans(handle, credits);
4216 if (ret < 0) {
4217 mlog_errno(ret);
4218 goto out;
4219 }
4220
Joel Becker7dc02802009-02-12 19:20:13 -08004221 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004222 if (ret < 0) {
4223 mlog_errno(ret);
4224 goto out;
4225 }
4226 }
4227
Mark Fashehe8aed342007-12-03 16:43:01 -08004228 /*
4229 * Pass both paths to the journal. The majority of inserts
4230 * will be touching all components anyway.
4231 */
Joel Becker7dc02802009-02-12 19:20:13 -08004232 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004233 if (ret < 0) {
4234 mlog_errno(ret);
4235 goto out;
4236 }
4237
Mark Fasheh328d5752007-06-18 10:48:04 -07004238 if (insert->ins_split != SPLIT_NONE) {
4239 /*
4240 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004241 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004242 * function sort it all out.
4243 */
Joel Beckerc38e52b2009-02-13 02:56:23 -08004244 ocfs2_split_record(et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004245 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004246
4247 /*
4248 * Split might have modified either leaf and we don't
4249 * have a guarantee that the later edge insert will
4250 * dirty this for us.
4251 */
4252 if (left_path)
4253 ret = ocfs2_journal_dirty(handle,
4254 path_leaf_bh(left_path));
4255 if (ret)
4256 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07004257 } else
Joel Beckerd5628622009-02-13 02:54:36 -08004258 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4259 insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004260
Mark Fashehdcd05382007-01-16 11:32:23 -08004261 ret = ocfs2_journal_dirty(handle, leaf_bh);
4262 if (ret)
4263 mlog_errno(ret);
4264
4265 if (left_path) {
4266 /*
4267 * The rotate code has indicated that we need to fix
4268 * up portions of the tree after the insert.
4269 *
4270 * XXX: Should we extend the transaction here?
4271 */
Joel Becker7dc02802009-02-12 19:20:13 -08004272 subtree_index = ocfs2_find_subtree_root(et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004273 right_path);
Joel Becker4619c732009-02-12 19:02:36 -08004274 ocfs2_complete_edge_insert(handle, left_path, right_path,
4275 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08004276 }
4277
4278 ret = 0;
4279out:
4280 return ret;
4281}
4282
Joel Becker3505bec2009-02-13 02:57:58 -08004283static int ocfs2_do_insert_extent(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004284 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004285 struct ocfs2_extent_rec *insert_rec,
4286 struct ocfs2_insert_type *type)
4287{
4288 int ret, rotate = 0;
4289 u32 cpos;
4290 struct ocfs2_path *right_path = NULL;
4291 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004292 struct ocfs2_extent_list *el;
4293
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004294 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004295
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004296 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004297 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004298 if (ret) {
4299 mlog_errno(ret);
4300 goto out;
4301 }
4302
4303 if (le16_to_cpu(el->l_tree_depth) == 0) {
Joel Beckerd5628622009-02-13 02:54:36 -08004304 ocfs2_insert_at_leaf(et, insert_rec, el, type);
Mark Fashehdcd05382007-01-16 11:32:23 -08004305 goto out_update_clusters;
4306 }
4307
Joel Beckerffdd7a52008-10-17 22:32:01 -07004308 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004309 if (!right_path) {
4310 ret = -ENOMEM;
4311 mlog_errno(ret);
4312 goto out;
4313 }
4314
4315 /*
4316 * Determine the path to start with. Rotations need the
4317 * rightmost path, everything else can go directly to the
4318 * target leaf.
4319 */
4320 cpos = le32_to_cpu(insert_rec->e_cpos);
4321 if (type->ins_appending == APPEND_NONE &&
4322 type->ins_contig == CONTIG_NONE) {
4323 rotate = 1;
4324 cpos = UINT_MAX;
4325 }
4326
Joel Beckerfacdb772009-02-12 18:08:48 -08004327 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004328 if (ret) {
4329 mlog_errno(ret);
4330 goto out;
4331 }
4332
4333 /*
4334 * Rotations and appends need special treatment - they modify
4335 * parts of the tree's above them.
4336 *
4337 * Both might pass back a path immediate to the left of the
4338 * one being inserted to. This will be cause
4339 * ocfs2_insert_path() to modify the rightmost records of
4340 * left_path to account for an edge insert.
4341 *
4342 * XXX: When modifying this code, keep in mind that an insert
4343 * can wind up skipping both of these two special cases...
4344 */
4345 if (rotate) {
Joel Becker1bbf0b82009-02-12 19:42:08 -08004346 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004347 le32_to_cpu(insert_rec->e_cpos),
4348 right_path, &left_path);
4349 if (ret) {
4350 mlog_errno(ret);
4351 goto out;
4352 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004353
4354 /*
4355 * ocfs2_rotate_tree_right() might have extended the
4356 * transaction without re-journaling our tree root.
4357 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004358 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004359 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004360 if (ret) {
4361 mlog_errno(ret);
4362 goto out;
4363 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004364 } else if (type->ins_appending == APPEND_TAIL
4365 && type->ins_contig != CONTIG_LEFT) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004366 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08004367 right_path, &left_path);
4368 if (ret) {
4369 mlog_errno(ret);
4370 goto out;
4371 }
4372 }
4373
Joel Becker3505bec2009-02-13 02:57:58 -08004374 ret = ocfs2_insert_path(handle, et, left_path, right_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004375 insert_rec, type);
4376 if (ret) {
4377 mlog_errno(ret);
4378 goto out;
4379 }
4380
4381out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004382 if (type->ins_split == SPLIT_NONE)
Joel Becker6136ca52009-02-12 19:32:43 -08004383 ocfs2_et_update_clusters(et,
Joel Becker35dc0aa2008-08-20 16:25:06 -07004384 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004385
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004386 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004387 if (ret)
4388 mlog_errno(ret);
4389
4390out:
4391 ocfs2_free_path(left_path);
4392 ocfs2_free_path(right_path);
4393
4394 return ret;
4395}
4396
Mark Fasheh328d5752007-06-18 10:48:04 -07004397static enum ocfs2_contig_type
Joel Beckera2970292009-02-13 03:09:54 -08004398ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4399 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004400 struct ocfs2_extent_list *el, int index,
4401 struct ocfs2_extent_rec *split_rec)
4402{
Tao Maad5a4d72008-01-30 14:21:32 +08004403 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004404 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004405 u32 left_cpos, right_cpos;
4406 struct ocfs2_extent_rec *rec = NULL;
4407 struct ocfs2_extent_list *new_el;
4408 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4409 struct buffer_head *bh;
4410 struct ocfs2_extent_block *eb;
Joel Beckera2970292009-02-13 03:09:54 -08004411 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Tao Maad5a4d72008-01-30 14:21:32 +08004412
4413 if (index > 0) {
4414 rec = &el->l_recs[index - 1];
4415 } else if (path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004416 status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004417 if (status)
4418 goto out;
4419
4420 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004421 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004422 if (!left_path)
4423 goto out;
4424
Joel Beckera2970292009-02-13 03:09:54 -08004425 status = ocfs2_find_path(et->et_ci, left_path,
4426 left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004427 if (status)
4428 goto out;
4429
4430 new_el = path_leaf_el(left_path);
4431
4432 if (le16_to_cpu(new_el->l_next_free_rec) !=
4433 le16_to_cpu(new_el->l_count)) {
4434 bh = path_leaf_bh(left_path);
4435 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004436 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004437 "Extent block #%llu has an "
4438 "invalid l_next_free_rec of "
4439 "%d. It should have "
4440 "matched the l_count of %d",
4441 (unsigned long long)le64_to_cpu(eb->h_blkno),
4442 le16_to_cpu(new_el->l_next_free_rec),
4443 le16_to_cpu(new_el->l_count));
4444 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004445 goto out;
4446 }
4447 rec = &new_el->l_recs[
4448 le16_to_cpu(new_el->l_next_free_rec) - 1];
4449 }
4450 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004451
4452 /*
4453 * We're careful to check for an empty extent record here -
4454 * the merge code will know what to do if it sees one.
4455 */
Tao Maad5a4d72008-01-30 14:21:32 +08004456 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004457 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4458 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4459 ret = CONTIG_RIGHT;
4460 } else {
Tao Ma853a3a12009-08-18 11:22:18 +08004461 ret = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004462 }
4463 }
4464
Tao Maad5a4d72008-01-30 14:21:32 +08004465 rec = NULL;
4466 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4467 rec = &el->l_recs[index + 1];
4468 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4469 path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004470 status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004471 if (status)
4472 goto out;
4473
4474 if (right_cpos == 0)
4475 goto out;
4476
Joel Beckerffdd7a52008-10-17 22:32:01 -07004477 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004478 if (!right_path)
4479 goto out;
4480
Joel Beckera2970292009-02-13 03:09:54 -08004481 status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004482 if (status)
4483 goto out;
4484
4485 new_el = path_leaf_el(right_path);
4486 rec = &new_el->l_recs[0];
4487 if (ocfs2_is_empty_extent(rec)) {
4488 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4489 bh = path_leaf_bh(right_path);
4490 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004491 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004492 "Extent block #%llu has an "
4493 "invalid l_next_free_rec of %d",
4494 (unsigned long long)le64_to_cpu(eb->h_blkno),
4495 le16_to_cpu(new_el->l_next_free_rec));
4496 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004497 goto out;
4498 }
4499 rec = &new_el->l_recs[1];
4500 }
4501 }
4502
4503 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004504 enum ocfs2_contig_type contig_type;
4505
Tao Ma853a3a12009-08-18 11:22:18 +08004506 contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004507
4508 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4509 ret = CONTIG_LEFTRIGHT;
4510 else if (ret == CONTIG_NONE)
4511 ret = contig_type;
4512 }
4513
Tao Maad5a4d72008-01-30 14:21:32 +08004514out:
4515 if (left_path)
4516 ocfs2_free_path(left_path);
4517 if (right_path)
4518 ocfs2_free_path(right_path);
4519
Mark Fasheh328d5752007-06-18 10:48:04 -07004520 return ret;
4521}
4522
Joel Becker1ef61b32009-02-13 03:12:33 -08004523static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004524 struct ocfs2_insert_type *insert,
4525 struct ocfs2_extent_list *el,
Joel Becker1ef61b32009-02-13 03:12:33 -08004526 struct ocfs2_extent_rec *insert_rec)
Mark Fashehdcd05382007-01-16 11:32:23 -08004527{
4528 int i;
4529 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4530
Mark Fashehe48edee2007-03-07 16:46:57 -08004531 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4532
Mark Fashehdcd05382007-01-16 11:32:23 -08004533 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
Tao Ma853a3a12009-08-18 11:22:18 +08004534 contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4535 insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004536 if (contig_type != CONTIG_NONE) {
4537 insert->ins_contig_index = i;
4538 break;
4539 }
4540 }
4541 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004542
4543 if (insert->ins_contig != CONTIG_NONE) {
4544 struct ocfs2_extent_rec *rec =
4545 &el->l_recs[insert->ins_contig_index];
4546 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4547 le16_to_cpu(insert_rec->e_leaf_clusters);
4548
4549 /*
4550 * Caller might want us to limit the size of extents, don't
4551 * calculate contiguousness if we might exceed that limit.
4552 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004553 if (et->et_max_leaf_clusters &&
4554 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004555 insert->ins_contig = CONTIG_NONE;
4556 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004557}
4558
4559/*
4560 * This should only be called against the righmost leaf extent list.
4561 *
4562 * ocfs2_figure_appending_type() will figure out whether we'll have to
4563 * insert at the tail of the rightmost leaf.
4564 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004565 * This should also work against the root extent list for tree's with 0
4566 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004567 * then the logic here makes sense.
4568 */
4569static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4570 struct ocfs2_extent_list *el,
4571 struct ocfs2_extent_rec *insert_rec)
4572{
4573 int i;
4574 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4575 struct ocfs2_extent_rec *rec;
4576
4577 insert->ins_appending = APPEND_NONE;
4578
Mark Fashehe48edee2007-03-07 16:46:57 -08004579 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004580
4581 if (!el->l_next_free_rec)
4582 goto set_tail_append;
4583
4584 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4585 /* Were all records empty? */
4586 if (le16_to_cpu(el->l_next_free_rec) == 1)
4587 goto set_tail_append;
4588 }
4589
4590 i = le16_to_cpu(el->l_next_free_rec) - 1;
4591 rec = &el->l_recs[i];
4592
Mark Fashehe48edee2007-03-07 16:46:57 -08004593 if (cpos >=
4594 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004595 goto set_tail_append;
4596
4597 return;
4598
4599set_tail_append:
4600 insert->ins_appending = APPEND_TAIL;
4601}
4602
4603/*
4604 * Helper function called at the begining of an insert.
4605 *
4606 * This computes a few things that are commonly used in the process of
4607 * inserting into the btree:
4608 * - Whether the new extent is contiguous with an existing one.
4609 * - The current tree depth.
4610 * - Whether the insert is an appending one.
4611 * - The total # of free records in the tree.
4612 *
4613 * All of the information is stored on the ocfs2_insert_type
4614 * structure.
4615 */
Joel Becker627961b2009-02-13 03:14:38 -08004616static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004617 struct buffer_head **last_eb_bh,
4618 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004619 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004620 struct ocfs2_insert_type *insert)
4621{
4622 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004623 struct ocfs2_extent_block *eb;
4624 struct ocfs2_extent_list *el;
4625 struct ocfs2_path *path = NULL;
4626 struct buffer_head *bh = NULL;
4627
Mark Fasheh328d5752007-06-18 10:48:04 -07004628 insert->ins_split = SPLIT_NONE;
4629
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004630 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004631 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4632
4633 if (el->l_tree_depth) {
4634 /*
4635 * If we have tree depth, we read in the
4636 * rightmost extent block ahead of time as
4637 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4638 * may want it later.
4639 */
Joel Becker3d03a302009-02-12 17:49:26 -08004640 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08004641 ocfs2_et_get_last_eb_blk(et),
4642 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004643 if (ret) {
4644 mlog_exit(ret);
4645 goto out;
4646 }
4647 eb = (struct ocfs2_extent_block *) bh->b_data;
4648 el = &eb->h_list;
4649 }
4650
4651 /*
4652 * Unless we have a contiguous insert, we'll need to know if
4653 * there is room left in our allocation tree for another
4654 * extent record.
4655 *
4656 * XXX: This test is simplistic, we can search for empty
4657 * extent records too.
4658 */
Tao Maoc77534f2007-08-28 17:22:33 -07004659 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004660 le16_to_cpu(el->l_next_free_rec);
4661
4662 if (!insert->ins_tree_depth) {
Joel Becker1ef61b32009-02-13 03:12:33 -08004663 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004664 ocfs2_figure_appending_type(insert, el, insert_rec);
4665 return 0;
4666 }
4667
Joel Beckerffdd7a52008-10-17 22:32:01 -07004668 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004669 if (!path) {
4670 ret = -ENOMEM;
4671 mlog_errno(ret);
4672 goto out;
4673 }
4674
4675 /*
4676 * In the case that we're inserting past what the tree
4677 * currently accounts for, ocfs2_find_path() will return for
4678 * us the rightmost tree path. This is accounted for below in
4679 * the appending code.
4680 */
Joel Beckerfacdb772009-02-12 18:08:48 -08004681 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -08004682 if (ret) {
4683 mlog_errno(ret);
4684 goto out;
4685 }
4686
4687 el = path_leaf_el(path);
4688
4689 /*
4690 * Now that we have the path, there's two things we want to determine:
4691 * 1) Contiguousness (also set contig_index if this is so)
4692 *
4693 * 2) Are we doing an append? We can trivially break this up
4694 * into two types of appends: simple record append, or a
4695 * rotate inside the tail leaf.
4696 */
Joel Becker1ef61b32009-02-13 03:12:33 -08004697 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004698
4699 /*
4700 * The insert code isn't quite ready to deal with all cases of
4701 * left contiguousness. Specifically, if it's an insert into
4702 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004703 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004704 * left. For now, just catch that case and fool the layers
4705 * above us. This works just fine for tree_depth == 0, which
4706 * is why we allow that above.
4707 */
4708 if (insert->ins_contig == CONTIG_LEFT &&
4709 insert->ins_contig_index == 0)
4710 insert->ins_contig = CONTIG_NONE;
4711
4712 /*
4713 * Ok, so we can simply compare against last_eb to figure out
4714 * whether the path doesn't exist. This will only happen in
4715 * the case that we're doing a tail append, so maybe we can
4716 * take advantage of that information somehow.
4717 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004718 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004719 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004720 /*
4721 * Ok, ocfs2_find_path() returned us the rightmost
4722 * tree path. This might be an appending insert. There are
4723 * two cases:
4724 * 1) We're doing a true append at the tail:
4725 * -This might even be off the end of the leaf
4726 * 2) We're "appending" by rotating in the tail
4727 */
4728 ocfs2_figure_appending_type(insert, el, insert_rec);
4729 }
4730
4731out:
4732 ocfs2_free_path(path);
4733
4734 if (ret == 0)
4735 *last_eb_bh = bh;
4736 else
4737 brelse(bh);
4738 return ret;
4739}
4740
4741/*
Joel Beckercc79d8c2009-02-13 03:24:43 -08004742 * Insert an extent into a btree.
Mark Fashehdcd05382007-01-16 11:32:23 -08004743 *
Joel Beckercc79d8c2009-02-13 03:24:43 -08004744 * The caller needs to update the owning btree's cluster count.
Mark Fashehdcd05382007-01-16 11:32:23 -08004745 */
Joel Beckercc79d8c2009-02-13 03:24:43 -08004746int ocfs2_insert_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004747 struct ocfs2_extent_tree *et,
4748 u32 cpos,
4749 u64 start_blk,
4750 u32 new_clusters,
4751 u8 flags,
4752 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004753{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004754 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004755 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004756 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004757 struct ocfs2_insert_type insert = {0, };
4758 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004759
Joel Beckercc79d8c2009-02-13 03:24:43 -08004760 mlog(0, "add %u clusters at position %u to owner %llu\n",
4761 new_clusters, cpos,
4762 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08004763
Mark Fashehe48edee2007-03-07 16:46:57 -08004764 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004765 rec.e_cpos = cpu_to_le32(cpos);
4766 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004767 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004768 rec.e_flags = flags;
Joel Becker6136ca52009-02-12 19:32:43 -08004769 status = ocfs2_et_insert_check(et, &rec);
Joel Becker1e61ee72008-08-20 18:32:45 -07004770 if (status) {
4771 mlog_errno(status);
4772 goto bail;
4773 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004774
Joel Becker627961b2009-02-13 03:14:38 -08004775 status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004776 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004777 if (status < 0) {
4778 mlog_errno(status);
4779 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004780 }
4781
Mark Fashehdcd05382007-01-16 11:32:23 -08004782 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4783 "Insert.contig_index: %d, Insert.free_records: %d, "
4784 "Insert.tree_depth: %d\n",
4785 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004786 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004787
Tao Maoc77534f2007-08-28 17:22:33 -07004788 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004789 status = ocfs2_grow_tree(handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004790 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004791 meta_ac);
4792 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004793 mlog_errno(status);
4794 goto bail;
4795 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004796 }
4797
Mark Fashehdcd05382007-01-16 11:32:23 -08004798 /* Finally, we can add clusters. This might rotate the tree for us. */
Joel Becker3505bec2009-02-13 02:57:58 -08004799 status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004800 if (status < 0)
4801 mlog_errno(status);
Joel Becker92ba4702009-02-13 03:18:34 -08004802 else
4803 ocfs2_et_extent_map_insert(et, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004804
4805bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004806 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004807
Tao Maf56654c2008-08-18 17:38:48 +08004808 mlog_exit(status);
4809 return status;
4810}
4811
Tao Ma0eb8d472008-08-18 17:38:45 +08004812/*
4813 * Allcate and add clusters into the extent b-tree.
4814 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004815 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004816 * it is not limited to the file storage. Any extent tree can use this
4817 * function if it implements the proper ocfs2_extent_tree.
4818 */
Joel Beckercbee7e12009-02-13 03:34:15 -08004819int ocfs2_add_clusters_in_btree(handle_t *handle,
4820 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004821 u32 *logical_offset,
4822 u32 clusters_to_add,
4823 int mark_unwritten,
Tao Ma0eb8d472008-08-18 17:38:45 +08004824 struct ocfs2_alloc_context *data_ac,
4825 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004826 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004827{
4828 int status = 0;
4829 int free_extents;
4830 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4831 u32 bit_off, num_bits;
4832 u64 block;
4833 u8 flags = 0;
Joel Beckercbee7e12009-02-13 03:34:15 -08004834 struct ocfs2_super *osb =
4835 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Tao Ma0eb8d472008-08-18 17:38:45 +08004836
4837 BUG_ON(!clusters_to_add);
4838
4839 if (mark_unwritten)
4840 flags = OCFS2_EXT_UNWRITTEN;
4841
Joel Becker3d03a302009-02-12 17:49:26 -08004842 free_extents = ocfs2_num_free_extents(osb, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004843 if (free_extents < 0) {
4844 status = free_extents;
4845 mlog_errno(status);
4846 goto leave;
4847 }
4848
4849 /* there are two cases which could cause us to EAGAIN in the
4850 * we-need-more-metadata case:
4851 * 1) we haven't reserved *any*
4852 * 2) we are so fragmented, we've needed to add metadata too
4853 * many times. */
4854 if (!free_extents && !meta_ac) {
4855 mlog(0, "we haven't reserved any metadata!\n");
4856 status = -EAGAIN;
4857 reason = RESTART_META;
4858 goto leave;
4859 } else if ((!free_extents)
4860 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004861 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004862 mlog(0, "filesystem is really fragmented...\n");
4863 status = -EAGAIN;
4864 reason = RESTART_META;
4865 goto leave;
4866 }
4867
4868 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4869 clusters_to_add, &bit_off, &num_bits);
4870 if (status < 0) {
4871 if (status != -ENOSPC)
4872 mlog_errno(status);
4873 goto leave;
4874 }
4875
4876 BUG_ON(num_bits > clusters_to_add);
4877
Joel Becker13723d02008-10-17 19:25:01 -07004878 /* reserve our write early -- insert_extent may update the tree root */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004879 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004880 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d472008-08-18 17:38:45 +08004881 if (status < 0) {
4882 mlog_errno(status);
4883 goto leave;
4884 }
4885
4886 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Joel Beckercbee7e12009-02-13 03:34:15 -08004887 mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
4888 num_bits, bit_off,
4889 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Joel Beckercc79d8c2009-02-13 03:24:43 -08004890 status = ocfs2_insert_extent(handle, et, *logical_offset, block,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004891 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004892 if (status < 0) {
4893 mlog_errno(status);
4894 goto leave;
4895 }
4896
Joel Beckerf99b9b72008-08-20 19:36:33 -07004897 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004898 if (status < 0) {
4899 mlog_errno(status);
4900 goto leave;
4901 }
4902
4903 clusters_to_add -= num_bits;
4904 *logical_offset += num_bits;
4905
4906 if (clusters_to_add) {
4907 mlog(0, "need to alloc once more, wanted = %u\n",
4908 clusters_to_add);
4909 status = -EAGAIN;
4910 reason = RESTART_TRANS;
4911 }
4912
4913leave:
4914 mlog_exit(status);
4915 if (reason_ret)
4916 *reason_ret = reason;
4917 return status;
4918}
4919
Mark Fasheh328d5752007-06-18 10:48:04 -07004920static void ocfs2_make_right_split_rec(struct super_block *sb,
4921 struct ocfs2_extent_rec *split_rec,
4922 u32 cpos,
4923 struct ocfs2_extent_rec *rec)
4924{
4925 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4926 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4927
4928 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4929
4930 split_rec->e_cpos = cpu_to_le32(cpos);
4931 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4932
4933 split_rec->e_blkno = rec->e_blkno;
4934 le64_add_cpu(&split_rec->e_blkno,
4935 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4936
4937 split_rec->e_flags = rec->e_flags;
4938}
4939
Joel Beckerd2311292009-02-13 03:43:22 -08004940static int ocfs2_split_and_insert(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004941 struct ocfs2_extent_tree *et,
Joel Beckerd2311292009-02-13 03:43:22 -08004942 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004943 struct buffer_head **last_eb_bh,
4944 int split_index,
4945 struct ocfs2_extent_rec *orig_split_rec,
4946 struct ocfs2_alloc_context *meta_ac)
4947{
4948 int ret = 0, depth;
4949 unsigned int insert_range, rec_range, do_leftright = 0;
4950 struct ocfs2_extent_rec tmprec;
4951 struct ocfs2_extent_list *rightmost_el;
4952 struct ocfs2_extent_rec rec;
4953 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4954 struct ocfs2_insert_type insert;
4955 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004956
4957leftright:
4958 /*
4959 * Store a copy of the record on the stack - it might move
4960 * around as the tree is manipulated below.
4961 */
4962 rec = path_leaf_el(path)->l_recs[split_index];
4963
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004964 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004965
4966 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4967 if (depth) {
4968 BUG_ON(!(*last_eb_bh));
4969 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4970 rightmost_el = &eb->h_list;
4971 }
4972
4973 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4974 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004975 ret = ocfs2_grow_tree(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08004976 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004977 if (ret) {
4978 mlog_errno(ret);
4979 goto out;
4980 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004981 }
4982
4983 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4984 insert.ins_appending = APPEND_NONE;
4985 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004986 insert.ins_tree_depth = depth;
4987
4988 insert_range = le32_to_cpu(split_rec.e_cpos) +
4989 le16_to_cpu(split_rec.e_leaf_clusters);
4990 rec_range = le32_to_cpu(rec.e_cpos) +
4991 le16_to_cpu(rec.e_leaf_clusters);
4992
4993 if (split_rec.e_cpos == rec.e_cpos) {
4994 insert.ins_split = SPLIT_LEFT;
4995 } else if (insert_range == rec_range) {
4996 insert.ins_split = SPLIT_RIGHT;
4997 } else {
4998 /*
4999 * Left/right split. We fake this as a right split
5000 * first and then make a second pass as a left split.
5001 */
5002 insert.ins_split = SPLIT_RIGHT;
5003
Joel Beckerd2311292009-02-13 03:43:22 -08005004 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5005 &tmprec, insert_range, &rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005006
5007 split_rec = tmprec;
5008
5009 BUG_ON(do_leftright);
5010 do_leftright = 1;
5011 }
5012
Joel Becker3505bec2009-02-13 02:57:58 -08005013 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07005014 if (ret) {
5015 mlog_errno(ret);
5016 goto out;
5017 }
5018
5019 if (do_leftright == 1) {
5020 u32 cpos;
5021 struct ocfs2_extent_list *el;
5022
5023 do_leftright++;
5024 split_rec = *orig_split_rec;
5025
5026 ocfs2_reinit_path(path, 1);
5027
5028 cpos = le32_to_cpu(split_rec.e_cpos);
Joel Beckerfacdb772009-02-12 18:08:48 -08005029 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005030 if (ret) {
5031 mlog_errno(ret);
5032 goto out;
5033 }
5034
5035 el = path_leaf_el(path);
5036 split_index = ocfs2_search_extent_list(el, cpos);
5037 goto leftright;
5038 }
5039out:
5040
5041 return ret;
5042}
5043
Joel Beckerf3868d02009-02-17 19:46:04 -08005044static int ocfs2_replace_extent_rec(handle_t *handle,
5045 struct ocfs2_extent_tree *et,
Tao Ma47be12e2009-01-09 07:32:48 +08005046 struct ocfs2_path *path,
5047 struct ocfs2_extent_list *el,
5048 int split_index,
5049 struct ocfs2_extent_rec *split_rec)
5050{
5051 int ret;
5052
Joel Beckerf3868d02009-02-17 19:46:04 -08005053 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Tao Ma47be12e2009-01-09 07:32:48 +08005054 path_num_items(path) - 1);
5055 if (ret) {
5056 mlog_errno(ret);
5057 goto out;
5058 }
5059
5060 el->l_recs[split_index] = *split_rec;
5061
5062 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5063out:
5064 return ret;
5065}
5066
Mark Fasheh328d5752007-06-18 10:48:04 -07005067/*
Tao Ma555936b2009-08-18 11:22:21 +08005068 * Split part or all of the extent record at split_index in the leaf
5069 * pointed to by path. Merge with the contiguous extent record if needed.
Mark Fasheh328d5752007-06-18 10:48:04 -07005070 *
5071 * Care is taken to handle contiguousness so as to not grow the tree.
5072 *
5073 * meta_ac is not strictly necessary - we only truly need it if growth
5074 * of the tree is required. All other cases will degrade into a less
5075 * optimal tree layout.
5076 *
Tao Mae7d4cb62008-08-18 17:38:44 +08005077 * last_eb_bh should be the rightmost leaf block for any extent
5078 * btree. Since a split may grow the tree or a merge might shrink it,
5079 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07005080 *
5081 * This code is optimized for readability - several passes might be
5082 * made over certain portions of the tree. All of those blocks will
5083 * have been brought into cache (and pinned via the journal), so the
5084 * extra overhead is not expressed in terms of disk reads.
5085 */
Tao Mae2e9f602009-08-18 11:22:34 +08005086int ocfs2_split_extent(handle_t *handle,
5087 struct ocfs2_extent_tree *et,
5088 struct ocfs2_path *path,
5089 int split_index,
5090 struct ocfs2_extent_rec *split_rec,
5091 struct ocfs2_alloc_context *meta_ac,
5092 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005093{
5094 int ret = 0;
5095 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08005096 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07005097 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5098 struct ocfs2_merge_ctxt ctxt;
5099 struct ocfs2_extent_list *rightmost_el;
5100
Mark Fasheh328d5752007-06-18 10:48:04 -07005101 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5102 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5103 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5104 ret = -EIO;
5105 mlog_errno(ret);
5106 goto out;
5107 }
5108
Joel Beckera2970292009-02-13 03:09:54 -08005109 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005110 split_index,
5111 split_rec);
5112
5113 /*
5114 * The core merge / split code wants to know how much room is
Joel Beckera1cf0762009-02-13 03:45:49 -08005115 * left in this allocation tree, so we pass the
Mark Fasheh328d5752007-06-18 10:48:04 -07005116 * rightmost extent list.
5117 */
5118 if (path->p_tree_depth) {
5119 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005120
Joel Becker3d03a302009-02-12 17:49:26 -08005121 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005122 ocfs2_et_get_last_eb_blk(et),
5123 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005124 if (ret) {
5125 mlog_exit(ret);
5126 goto out;
5127 }
5128
5129 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005130 rightmost_el = &eb->h_list;
5131 } else
5132 rightmost_el = path_root_el(path);
5133
Mark Fasheh328d5752007-06-18 10:48:04 -07005134 if (rec->e_cpos == split_rec->e_cpos &&
5135 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5136 ctxt.c_split_covers_rec = 1;
5137 else
5138 ctxt.c_split_covers_rec = 0;
5139
5140 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5141
Mark Fasheh015452b2007-09-12 10:21:22 -07005142 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5143 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5144 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005145
5146 if (ctxt.c_contig_type == CONTIG_NONE) {
5147 if (ctxt.c_split_covers_rec)
Joel Beckerf3868d02009-02-17 19:46:04 -08005148 ret = ocfs2_replace_extent_rec(handle, et, path, el,
Tao Ma47be12e2009-01-09 07:32:48 +08005149 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005150 else
Joel Beckerd2311292009-02-13 03:43:22 -08005151 ret = ocfs2_split_and_insert(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005152 &last_eb_bh, split_index,
5153 split_rec, meta_ac);
5154 if (ret)
5155 mlog_errno(ret);
5156 } else {
Joel Beckerc495dd22009-02-13 02:19:11 -08005157 ret = ocfs2_try_to_merge_extent(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005158 split_index, split_rec,
Joel Beckerc495dd22009-02-13 02:19:11 -08005159 dealloc, &ctxt);
Mark Fasheh328d5752007-06-18 10:48:04 -07005160 if (ret)
5161 mlog_errno(ret);
5162 }
5163
Mark Fasheh328d5752007-06-18 10:48:04 -07005164out:
5165 brelse(last_eb_bh);
5166 return ret;
5167}
5168
5169/*
Tao Ma555936b2009-08-18 11:22:21 +08005170 * Change the flags of the already-existing extent at cpos for len clusters.
5171 *
5172 * new_flags: the flags we want to set.
5173 * clear_flags: the flags we want to clear.
5174 * phys: the new physical offset we want this new extent starts from.
Mark Fasheh328d5752007-06-18 10:48:04 -07005175 *
5176 * If the existing extent is larger than the request, initiate a
5177 * split. An attempt will be made at merging with adjacent extents.
5178 *
5179 * The caller is responsible for passing down meta_ac if we'll need it.
5180 */
Tao Ma1aa75fe2009-08-18 11:28:39 +08005181int ocfs2_change_extent_flag(handle_t *handle,
5182 struct ocfs2_extent_tree *et,
5183 u32 cpos, u32 len, u32 phys,
5184 struct ocfs2_alloc_context *meta_ac,
5185 struct ocfs2_cached_dealloc_ctxt *dealloc,
5186 int new_flags, int clear_flags)
Mark Fasheh328d5752007-06-18 10:48:04 -07005187{
5188 int ret, index;
Tao Ma555936b2009-08-18 11:22:21 +08005189 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5190 u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
Mark Fasheh328d5752007-06-18 10:48:04 -07005191 struct ocfs2_extent_rec split_rec;
5192 struct ocfs2_path *left_path = NULL;
5193 struct ocfs2_extent_list *el;
Tao Ma555936b2009-08-18 11:22:21 +08005194 struct ocfs2_extent_rec *rec;
Mark Fasheh328d5752007-06-18 10:48:04 -07005195
Joel Beckerffdd7a52008-10-17 22:32:01 -07005196 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005197 if (!left_path) {
5198 ret = -ENOMEM;
5199 mlog_errno(ret);
5200 goto out;
5201 }
5202
Joel Beckerfacdb772009-02-12 18:08:48 -08005203 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005204 if (ret) {
5205 mlog_errno(ret);
5206 goto out;
5207 }
5208 el = path_leaf_el(left_path);
5209
5210 index = ocfs2_search_extent_list(el, cpos);
5211 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Tao Ma555936b2009-08-18 11:22:21 +08005212 ocfs2_error(sb,
5213 "Owner %llu has an extent at cpos %u which can no "
Mark Fasheh328d5752007-06-18 10:48:04 -07005214 "longer be found.\n",
Tao Ma555936b2009-08-18 11:22:21 +08005215 (unsigned long long)
5216 ocfs2_metadata_cache_owner(et->et_ci), cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005217 ret = -EROFS;
5218 goto out;
5219 }
5220
Tao Ma555936b2009-08-18 11:22:21 +08005221 ret = -EIO;
5222 rec = &el->l_recs[index];
5223 if (new_flags && (rec->e_flags & new_flags)) {
5224 mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
5225 "extent that already had them",
5226 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5227 new_flags);
5228 goto out;
5229 }
5230
5231 if (clear_flags && !(rec->e_flags & clear_flags)) {
5232 mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
5233 "extent that didn't have them",
5234 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5235 clear_flags);
5236 goto out;
5237 }
5238
Mark Fasheh328d5752007-06-18 10:48:04 -07005239 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5240 split_rec.e_cpos = cpu_to_le32(cpos);
5241 split_rec.e_leaf_clusters = cpu_to_le16(len);
5242 split_rec.e_blkno = cpu_to_le64(start_blkno);
Tao Ma555936b2009-08-18 11:22:21 +08005243 split_rec.e_flags = rec->e_flags;
5244 if (new_flags)
5245 split_rec.e_flags |= new_flags;
5246 if (clear_flags)
5247 split_rec.e_flags &= ~clear_flags;
Mark Fasheh328d5752007-06-18 10:48:04 -07005248
Tao Mae2e9f602009-08-18 11:22:34 +08005249 ret = ocfs2_split_extent(handle, et, left_path,
5250 index, &split_rec, meta_ac,
5251 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005252 if (ret)
5253 mlog_errno(ret);
5254
5255out:
5256 ocfs2_free_path(left_path);
5257 return ret;
Tao Ma555936b2009-08-18 11:22:21 +08005258
5259}
5260
5261/*
5262 * Mark the already-existing extent at cpos as written for len clusters.
5263 * This removes the unwritten extent flag.
5264 *
5265 * If the existing extent is larger than the request, initiate a
5266 * split. An attempt will be made at merging with adjacent extents.
5267 *
5268 * The caller is responsible for passing down meta_ac if we'll need it.
5269 */
5270int ocfs2_mark_extent_written(struct inode *inode,
5271 struct ocfs2_extent_tree *et,
5272 handle_t *handle, u32 cpos, u32 len, u32 phys,
5273 struct ocfs2_alloc_context *meta_ac,
5274 struct ocfs2_cached_dealloc_ctxt *dealloc)
5275{
5276 int ret;
5277
5278 mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
5279 inode->i_ino, cpos, len, phys);
5280
5281 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5282 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5283 "that are being written to, but the feature bit "
5284 "is not set in the super block.",
5285 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5286 ret = -EROFS;
5287 goto out;
5288 }
5289
5290 /*
5291 * XXX: This should be fixed up so that we just re-insert the
5292 * next extent records.
5293 */
5294 ocfs2_et_extent_map_truncate(et, 0);
5295
5296 ret = ocfs2_change_extent_flag(handle, et, cpos,
5297 len, phys, meta_ac, dealloc,
5298 0, OCFS2_EXT_UNWRITTEN);
5299 if (ret)
5300 mlog_errno(ret);
5301
5302out:
5303 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07005304}
5305
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005306static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5307 struct ocfs2_path *path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005308 int index, u32 new_range,
5309 struct ocfs2_alloc_context *meta_ac)
5310{
5311 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005312 struct buffer_head *last_eb_bh = NULL;
5313 struct ocfs2_extent_block *eb;
5314 struct ocfs2_extent_list *rightmost_el, *el;
5315 struct ocfs2_extent_rec split_rec;
5316 struct ocfs2_extent_rec *rec;
5317 struct ocfs2_insert_type insert;
5318
5319 /*
5320 * Setup the record to split before we grow the tree.
5321 */
5322 el = path_leaf_el(path);
5323 rec = &el->l_recs[index];
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005324 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5325 &split_rec, new_range, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005326
5327 depth = path->p_tree_depth;
5328 if (depth > 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08005329 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005330 ocfs2_et_get_last_eb_blk(et),
5331 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005332 if (ret < 0) {
5333 mlog_errno(ret);
5334 goto out;
5335 }
5336
5337 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5338 rightmost_el = &eb->h_list;
5339 } else
5340 rightmost_el = path_leaf_el(path);
5341
Tao Ma811f9332008-08-18 17:38:43 +08005342 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005343 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005344 ret = ocfs2_extend_trans(handle, credits);
5345 if (ret) {
5346 mlog_errno(ret);
5347 goto out;
5348 }
5349
5350 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5351 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08005352 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005353 meta_ac);
5354 if (ret) {
5355 mlog_errno(ret);
5356 goto out;
5357 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005358 }
5359
5360 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5361 insert.ins_appending = APPEND_NONE;
5362 insert.ins_contig = CONTIG_NONE;
5363 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005364 insert.ins_tree_depth = depth;
5365
Joel Becker3505bec2009-02-13 02:57:58 -08005366 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005367 if (ret)
5368 mlog_errno(ret);
5369
5370out:
5371 brelse(last_eb_bh);
5372 return ret;
5373}
5374
Joel Becker043beeb2009-02-13 02:42:30 -08005375static int ocfs2_truncate_rec(handle_t *handle,
5376 struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005377 struct ocfs2_path *path, int index,
5378 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker043beeb2009-02-13 02:42:30 -08005379 u32 cpos, u32 len)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005380{
5381 int ret;
5382 u32 left_cpos, rec_range, trunc_range;
5383 int wants_rotate = 0, is_rightmost_tree_rec = 0;
Joel Becker043beeb2009-02-13 02:42:30 -08005384 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005385 struct ocfs2_path *left_path = NULL;
5386 struct ocfs2_extent_list *el = path_leaf_el(path);
5387 struct ocfs2_extent_rec *rec;
5388 struct ocfs2_extent_block *eb;
5389
5390 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Joel Becker70f18c02009-02-13 02:09:31 -08005391 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005392 if (ret) {
5393 mlog_errno(ret);
5394 goto out;
5395 }
5396
5397 index--;
5398 }
5399
5400 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5401 path->p_tree_depth) {
5402 /*
5403 * Check whether this is the rightmost tree record. If
5404 * we remove all of this record or part of its right
5405 * edge then an update of the record lengths above it
5406 * will be required.
5407 */
5408 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5409 if (eb->h_next_leaf_blk == 0)
5410 is_rightmost_tree_rec = 1;
5411 }
5412
5413 rec = &el->l_recs[index];
5414 if (index == 0 && path->p_tree_depth &&
5415 le32_to_cpu(rec->e_cpos) == cpos) {
5416 /*
5417 * Changing the leftmost offset (via partial or whole
5418 * record truncate) of an interior (or rightmost) path
5419 * means we have to update the subtree that is formed
5420 * by this leaf and the one to it's left.
5421 *
5422 * There are two cases we can skip:
Joel Becker043beeb2009-02-13 02:42:30 -08005423 * 1) Path is the leftmost one in our btree.
Mark Fashehd0c7d702007-07-03 13:27:22 -07005424 * 2) The leaf is rightmost and will be empty after
5425 * we remove the extent record - the rotate code
5426 * knows how to update the newly formed edge.
5427 */
5428
Joel Becker043beeb2009-02-13 02:42:30 -08005429 ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005430 if (ret) {
5431 mlog_errno(ret);
5432 goto out;
5433 }
5434
5435 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005436 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005437 if (!left_path) {
5438 ret = -ENOMEM;
5439 mlog_errno(ret);
5440 goto out;
5441 }
5442
Joel Beckerfacdb772009-02-12 18:08:48 -08005443 ret = ocfs2_find_path(et->et_ci, left_path,
5444 left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005445 if (ret) {
5446 mlog_errno(ret);
5447 goto out;
5448 }
5449 }
5450 }
5451
5452 ret = ocfs2_extend_rotate_transaction(handle, 0,
5453 handle->h_buffer_credits,
5454 path);
5455 if (ret) {
5456 mlog_errno(ret);
5457 goto out;
5458 }
5459
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005460 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005461 if (ret) {
5462 mlog_errno(ret);
5463 goto out;
5464 }
5465
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005466 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005467 if (ret) {
5468 mlog_errno(ret);
5469 goto out;
5470 }
5471
5472 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5473 trunc_range = cpos + len;
5474
5475 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5476 int next_free;
5477
5478 memset(rec, 0, sizeof(*rec));
5479 ocfs2_cleanup_merge(el, index);
5480 wants_rotate = 1;
5481
5482 next_free = le16_to_cpu(el->l_next_free_rec);
5483 if (is_rightmost_tree_rec && next_free > 1) {
5484 /*
5485 * We skip the edge update if this path will
5486 * be deleted by the rotate code.
5487 */
5488 rec = &el->l_recs[next_free - 1];
Joel Beckerd401dc12009-02-13 02:24:10 -08005489 ocfs2_adjust_rightmost_records(handle, et, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005490 rec);
5491 }
5492 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5493 /* Remove leftmost portion of the record. */
5494 le32_add_cpu(&rec->e_cpos, len);
5495 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5496 le16_add_cpu(&rec->e_leaf_clusters, -len);
5497 } else if (rec_range == trunc_range) {
5498 /* Remove rightmost portion of the record */
5499 le16_add_cpu(&rec->e_leaf_clusters, -len);
5500 if (is_rightmost_tree_rec)
Joel Beckerd401dc12009-02-13 02:24:10 -08005501 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005502 } else {
5503 /* Caller should have trapped this. */
Joel Becker043beeb2009-02-13 02:42:30 -08005504 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5505 "(%u, %u)\n",
5506 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005507 le32_to_cpu(rec->e_cpos),
5508 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5509 BUG();
5510 }
5511
5512 if (left_path) {
5513 int subtree_index;
5514
Joel Becker7dc02802009-02-12 19:20:13 -08005515 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Joel Becker4619c732009-02-12 19:02:36 -08005516 ocfs2_complete_edge_insert(handle, left_path, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005517 subtree_index);
5518 }
5519
5520 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5521
Joel Becker70f18c02009-02-13 02:09:31 -08005522 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005523 if (ret) {
5524 mlog_errno(ret);
5525 goto out;
5526 }
5527
5528out:
5529 ocfs2_free_path(left_path);
5530 return ret;
5531}
5532
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005533int ocfs2_remove_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005534 struct ocfs2_extent_tree *et,
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005535 u32 cpos, u32 len,
Mark Fasheh063c4562007-07-03 13:34:11 -07005536 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005537 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005538{
5539 int ret, index;
5540 u32 rec_range, trunc_range;
5541 struct ocfs2_extent_rec *rec;
5542 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005543 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005544
Joel Becker4c911ee2009-02-13 02:50:12 -08005545 /*
5546 * XXX: Why are we truncating to 0 instead of wherever this
5547 * affects us?
5548 */
5549 ocfs2_et_extent_map_truncate(et, 0);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005550
Joel Beckerffdd7a52008-10-17 22:32:01 -07005551 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005552 if (!path) {
5553 ret = -ENOMEM;
5554 mlog_errno(ret);
5555 goto out;
5556 }
5557
Joel Beckerfacdb772009-02-12 18:08:48 -08005558 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005559 if (ret) {
5560 mlog_errno(ret);
5561 goto out;
5562 }
5563
5564 el = path_leaf_el(path);
5565 index = ocfs2_search_extent_list(el, cpos);
5566 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005567 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5568 "Owner %llu has an extent at cpos %u which can no "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005569 "longer be found.\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005570 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5571 cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005572 ret = -EROFS;
5573 goto out;
5574 }
5575
5576 /*
5577 * We have 3 cases of extent removal:
5578 * 1) Range covers the entire extent rec
5579 * 2) Range begins or ends on one edge of the extent rec
5580 * 3) Range is in the middle of the extent rec (no shared edges)
5581 *
5582 * For case 1 we remove the extent rec and left rotate to
5583 * fill the hole.
5584 *
5585 * For case 2 we just shrink the existing extent rec, with a
5586 * tree update if the shrinking edge is also the edge of an
5587 * extent block.
5588 *
5589 * For case 3 we do a right split to turn the extent rec into
5590 * something case 2 can handle.
5591 */
5592 rec = &el->l_recs[index];
5593 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5594 trunc_range = cpos + len;
5595
5596 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5597
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005598 mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005599 "(cpos %u, len %u)\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005600 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5601 cpos, len, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005602 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5603
5604 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
Joel Becker043beeb2009-02-13 02:42:30 -08005605 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5606 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005607 if (ret) {
5608 mlog_errno(ret);
5609 goto out;
5610 }
5611 } else {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005612 ret = ocfs2_split_tree(handle, et, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005613 trunc_range, meta_ac);
5614 if (ret) {
5615 mlog_errno(ret);
5616 goto out;
5617 }
5618
5619 /*
5620 * The split could have manipulated the tree enough to
5621 * move the record location, so we have to look for it again.
5622 */
5623 ocfs2_reinit_path(path, 1);
5624
Joel Beckerfacdb772009-02-12 18:08:48 -08005625 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005626 if (ret) {
5627 mlog_errno(ret);
5628 goto out;
5629 }
5630
5631 el = path_leaf_el(path);
5632 index = ocfs2_search_extent_list(el, cpos);
5633 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005634 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5635 "Owner %llu: split at cpos %u lost record.",
5636 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005637 cpos);
5638 ret = -EROFS;
5639 goto out;
5640 }
5641
5642 /*
5643 * Double check our values here. If anything is fishy,
5644 * it's easier to catch it at the top level.
5645 */
5646 rec = &el->l_recs[index];
5647 rec_range = le32_to_cpu(rec->e_cpos) +
5648 ocfs2_rec_clusters(el, rec);
5649 if (rec_range != trunc_range) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005650 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5651 "Owner %llu: error after split at cpos %u"
Mark Fashehd0c7d702007-07-03 13:27:22 -07005652 "trunc len %u, existing record is (%u,%u)",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005653 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005654 cpos, len, le32_to_cpu(rec->e_cpos),
5655 ocfs2_rec_clusters(el, rec));
5656 ret = -EROFS;
5657 goto out;
5658 }
5659
Joel Becker043beeb2009-02-13 02:42:30 -08005660 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5661 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005662 if (ret) {
5663 mlog_errno(ret);
5664 goto out;
5665 }
5666 }
5667
5668out:
5669 ocfs2_free_path(path);
5670 return ret;
5671}
5672
Mark Fashehfecc0112008-11-12 15:16:38 -08005673int ocfs2_remove_btree_range(struct inode *inode,
5674 struct ocfs2_extent_tree *et,
5675 u32 cpos, u32 phys_cpos, u32 len,
5676 struct ocfs2_cached_dealloc_ctxt *dealloc)
5677{
5678 int ret;
5679 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5680 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5681 struct inode *tl_inode = osb->osb_tl_inode;
5682 handle_t *handle;
5683 struct ocfs2_alloc_context *meta_ac = NULL;
5684
5685 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5686 if (ret) {
5687 mlog_errno(ret);
5688 return ret;
5689 }
5690
5691 mutex_lock(&tl_inode->i_mutex);
5692
5693 if (ocfs2_truncate_log_needs_flush(osb)) {
5694 ret = __ocfs2_flush_truncate_log(osb);
5695 if (ret < 0) {
5696 mlog_errno(ret);
5697 goto out;
5698 }
5699 }
5700
Jan Karaa90714c2008-10-09 19:38:40 +02005701 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005702 if (IS_ERR(handle)) {
5703 ret = PTR_ERR(handle);
5704 mlog_errno(ret);
5705 goto out;
5706 }
5707
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005708 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07005709 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005710 if (ret) {
5711 mlog_errno(ret);
5712 goto out;
5713 }
5714
Mark Fashehfd4ef232009-01-29 15:06:21 -08005715 vfs_dq_free_space_nodirty(inode,
5716 ocfs2_clusters_to_bytes(inode->i_sb, len));
5717
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005718 ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
Mark Fashehfecc0112008-11-12 15:16:38 -08005719 if (ret) {
5720 mlog_errno(ret);
5721 goto out_commit;
5722 }
5723
Joel Becker6136ca52009-02-12 19:32:43 -08005724 ocfs2_et_update_clusters(et, -len);
Mark Fashehfecc0112008-11-12 15:16:38 -08005725
5726 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5727 if (ret) {
5728 mlog_errno(ret);
5729 goto out_commit;
5730 }
5731
5732 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5733 if (ret)
5734 mlog_errno(ret);
5735
5736out_commit:
5737 ocfs2_commit_trans(osb, handle);
5738out:
5739 mutex_unlock(&tl_inode->i_mutex);
5740
5741 if (meta_ac)
5742 ocfs2_free_alloc_context(meta_ac);
5743
5744 return ret;
5745}
5746
Mark Fasheh063c4562007-07-03 13:34:11 -07005747int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005748{
5749 struct buffer_head *tl_bh = osb->osb_tl_bh;
5750 struct ocfs2_dinode *di;
5751 struct ocfs2_truncate_log *tl;
5752
5753 di = (struct ocfs2_dinode *) tl_bh->b_data;
5754 tl = &di->id2.i_dealloc;
5755
5756 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5757 "slot %d, invalid truncate log parameters: used = "
5758 "%u, count = %u\n", osb->slot_num,
5759 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5760 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5761}
5762
5763static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5764 unsigned int new_start)
5765{
5766 unsigned int tail_index;
5767 unsigned int current_tail;
5768
5769 /* No records, nothing to coalesce */
5770 if (!le16_to_cpu(tl->tl_used))
5771 return 0;
5772
5773 tail_index = le16_to_cpu(tl->tl_used) - 1;
5774 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5775 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5776
5777 return current_tail == new_start;
5778}
5779
Mark Fasheh063c4562007-07-03 13:34:11 -07005780int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5781 handle_t *handle,
5782 u64 start_blk,
5783 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005784{
5785 int status, index;
5786 unsigned int start_cluster, tl_count;
5787 struct inode *tl_inode = osb->osb_tl_inode;
5788 struct buffer_head *tl_bh = osb->osb_tl_bh;
5789 struct ocfs2_dinode *di;
5790 struct ocfs2_truncate_log *tl;
5791
Mark Fashehb06970532006-03-03 10:24:33 -08005792 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5793 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005794
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005795 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005796
5797 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5798
5799 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005800
Joel Becker10995aa2008-11-13 14:49:12 -08005801 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5802 * by the underlying call to ocfs2_read_inode_block(), so any
5803 * corruption is a code bug */
5804 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5805
5806 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005807 tl_count = le16_to_cpu(tl->tl_count);
5808 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5809 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005810 "Truncate record count on #%llu invalid "
5811 "wanted %u, actual %u\n",
5812 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005813 ocfs2_truncate_recs_per_inode(osb->sb),
5814 le16_to_cpu(tl->tl_count));
5815
5816 /* Caller should have known to flush before calling us. */
5817 index = le16_to_cpu(tl->tl_used);
5818 if (index >= tl_count) {
5819 status = -ENOSPC;
5820 mlog_errno(status);
5821 goto bail;
5822 }
5823
Joel Becker0cf2f762009-02-12 16:41:25 -08005824 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005825 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005826 if (status < 0) {
5827 mlog_errno(status);
5828 goto bail;
5829 }
5830
5831 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005832 "%llu (index = %d)\n", num_clusters, start_cluster,
5833 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005834
5835 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5836 /*
5837 * Move index back to the record we are coalescing with.
5838 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5839 */
5840 index--;
5841
5842 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5843 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5844 index, le32_to_cpu(tl->tl_recs[index].t_start),
5845 num_clusters);
5846 } else {
5847 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5848 tl->tl_used = cpu_to_le16(index + 1);
5849 }
5850 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5851
5852 status = ocfs2_journal_dirty(handle, tl_bh);
5853 if (status < 0) {
5854 mlog_errno(status);
5855 goto bail;
5856 }
5857
5858bail:
5859 mlog_exit(status);
5860 return status;
5861}
5862
5863static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005864 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005865 struct inode *data_alloc_inode,
5866 struct buffer_head *data_alloc_bh)
5867{
5868 int status = 0;
5869 int i;
5870 unsigned int num_clusters;
5871 u64 start_blk;
5872 struct ocfs2_truncate_rec rec;
5873 struct ocfs2_dinode *di;
5874 struct ocfs2_truncate_log *tl;
5875 struct inode *tl_inode = osb->osb_tl_inode;
5876 struct buffer_head *tl_bh = osb->osb_tl_bh;
5877
5878 mlog_entry_void();
5879
5880 di = (struct ocfs2_dinode *) tl_bh->b_data;
5881 tl = &di->id2.i_dealloc;
5882 i = le16_to_cpu(tl->tl_used) - 1;
5883 while (i >= 0) {
5884 /* Caller has given us at least enough credits to
5885 * update the truncate log dinode */
Joel Becker0cf2f762009-02-12 16:41:25 -08005886 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005887 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005888 if (status < 0) {
5889 mlog_errno(status);
5890 goto bail;
5891 }
5892
5893 tl->tl_used = cpu_to_le16(i);
5894
5895 status = ocfs2_journal_dirty(handle, tl_bh);
5896 if (status < 0) {
5897 mlog_errno(status);
5898 goto bail;
5899 }
5900
5901 /* TODO: Perhaps we can calculate the bulk of the
5902 * credits up front rather than extending like
5903 * this. */
5904 status = ocfs2_extend_trans(handle,
5905 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5906 if (status < 0) {
5907 mlog_errno(status);
5908 goto bail;
5909 }
5910
5911 rec = tl->tl_recs[i];
5912 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5913 le32_to_cpu(rec.t_start));
5914 num_clusters = le32_to_cpu(rec.t_clusters);
5915
5916 /* if start_blk is not set, we ignore the record as
5917 * invalid. */
5918 if (start_blk) {
5919 mlog(0, "free record %d, start = %u, clusters = %u\n",
5920 i, le32_to_cpu(rec.t_start), num_clusters);
5921
5922 status = ocfs2_free_clusters(handle, data_alloc_inode,
5923 data_alloc_bh, start_blk,
5924 num_clusters);
5925 if (status < 0) {
5926 mlog_errno(status);
5927 goto bail;
5928 }
5929 }
5930 i--;
5931 }
5932
5933bail:
5934 mlog_exit(status);
5935 return status;
5936}
5937
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005938/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005939int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005940{
5941 int status;
5942 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005943 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005944 struct inode *tl_inode = osb->osb_tl_inode;
5945 struct inode *data_alloc_inode = NULL;
5946 struct buffer_head *tl_bh = osb->osb_tl_bh;
5947 struct buffer_head *data_alloc_bh = NULL;
5948 struct ocfs2_dinode *di;
5949 struct ocfs2_truncate_log *tl;
5950
5951 mlog_entry_void();
5952
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005953 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005954
5955 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005956
Joel Becker10995aa2008-11-13 14:49:12 -08005957 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5958 * by the underlying call to ocfs2_read_inode_block(), so any
5959 * corruption is a code bug */
5960 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5961
5962 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005963 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005964 mlog(0, "Flush %u records from truncate log #%llu\n",
5965 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005966 if (!num_to_flush) {
5967 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005968 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005969 }
5970
5971 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5972 GLOBAL_BITMAP_SYSTEM_INODE,
5973 OCFS2_INVALID_SLOT);
5974 if (!data_alloc_inode) {
5975 status = -EINVAL;
5976 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005977 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005978 }
5979
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005980 mutex_lock(&data_alloc_inode->i_mutex);
5981
Mark Fashehe63aecb62007-10-18 15:30:42 -07005982 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005983 if (status < 0) {
5984 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005985 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005986 }
5987
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005988 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005989 if (IS_ERR(handle)) {
5990 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005991 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005992 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005993 }
5994
5995 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5996 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005997 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005998 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005999
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006000 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006001
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006002out_unlock:
6003 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07006004 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08006005
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006006out_mutex:
6007 mutex_unlock(&data_alloc_inode->i_mutex);
6008 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006009
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006010out:
Mark Fashehccd979b2005-12-15 14:31:24 -08006011 mlog_exit(status);
6012 return status;
6013}
6014
6015int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
6016{
6017 int status;
6018 struct inode *tl_inode = osb->osb_tl_inode;
6019
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006020 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006021 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006022 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006023
6024 return status;
6025}
6026
David Howellsc4028952006-11-22 14:57:56 +00006027static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08006028{
6029 int status;
David Howellsc4028952006-11-22 14:57:56 +00006030 struct ocfs2_super *osb =
6031 container_of(work, struct ocfs2_super,
6032 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08006033
6034 mlog_entry_void();
6035
6036 status = ocfs2_flush_truncate_log(osb);
6037 if (status < 0)
6038 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08006039 else
6040 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08006041
6042 mlog_exit(status);
6043}
6044
6045#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
6046void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
6047 int cancel)
6048{
6049 if (osb->osb_tl_inode) {
6050 /* We want to push off log flushes while truncates are
6051 * still running. */
6052 if (cancel)
6053 cancel_delayed_work(&osb->osb_truncate_log_wq);
6054
6055 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
6056 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
6057 }
6058}
6059
6060static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
6061 int slot_num,
6062 struct inode **tl_inode,
6063 struct buffer_head **tl_bh)
6064{
6065 int status;
6066 struct inode *inode = NULL;
6067 struct buffer_head *bh = NULL;
6068
6069 inode = ocfs2_get_system_file_inode(osb,
6070 TRUNCATE_LOG_SYSTEM_INODE,
6071 slot_num);
6072 if (!inode) {
6073 status = -EINVAL;
6074 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
6075 goto bail;
6076 }
6077
Joel Beckerb657c952008-11-13 14:49:11 -08006078 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006079 if (status < 0) {
6080 iput(inode);
6081 mlog_errno(status);
6082 goto bail;
6083 }
6084
6085 *tl_inode = inode;
6086 *tl_bh = bh;
6087bail:
6088 mlog_exit(status);
6089 return status;
6090}
6091
6092/* called during the 1st stage of node recovery. we stamp a clean
6093 * truncate log and pass back a copy for processing later. if the
6094 * truncate log does not require processing, a *tl_copy is set to
6095 * NULL. */
6096int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6097 int slot_num,
6098 struct ocfs2_dinode **tl_copy)
6099{
6100 int status;
6101 struct inode *tl_inode = NULL;
6102 struct buffer_head *tl_bh = NULL;
6103 struct ocfs2_dinode *di;
6104 struct ocfs2_truncate_log *tl;
6105
6106 *tl_copy = NULL;
6107
6108 mlog(0, "recover truncate log from slot %d\n", slot_num);
6109
6110 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6111 if (status < 0) {
6112 mlog_errno(status);
6113 goto bail;
6114 }
6115
6116 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08006117
Joel Becker10995aa2008-11-13 14:49:12 -08006118 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6119 * validated by the underlying call to ocfs2_read_inode_block(),
6120 * so any corruption is a code bug */
6121 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6122
6123 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08006124 if (le16_to_cpu(tl->tl_used)) {
6125 mlog(0, "We'll have %u logs to recover\n",
6126 le16_to_cpu(tl->tl_used));
6127
6128 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6129 if (!(*tl_copy)) {
6130 status = -ENOMEM;
6131 mlog_errno(status);
6132 goto bail;
6133 }
6134
6135 /* Assuming the write-out below goes well, this copy
6136 * will be passed back to recovery for processing. */
6137 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6138
6139 /* All we need to do to clear the truncate log is set
6140 * tl_used. */
6141 tl->tl_used = 0;
6142
Joel Becker13723d02008-10-17 19:25:01 -07006143 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08006144 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08006145 if (status < 0) {
6146 mlog_errno(status);
6147 goto bail;
6148 }
6149 }
6150
6151bail:
6152 if (tl_inode)
6153 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006154 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006155
6156 if (status < 0 && (*tl_copy)) {
6157 kfree(*tl_copy);
6158 *tl_copy = NULL;
6159 }
6160
6161 mlog_exit(status);
6162 return status;
6163}
6164
6165int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6166 struct ocfs2_dinode *tl_copy)
6167{
6168 int status = 0;
6169 int i;
6170 unsigned int clusters, num_recs, start_cluster;
6171 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006172 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006173 struct inode *tl_inode = osb->osb_tl_inode;
6174 struct ocfs2_truncate_log *tl;
6175
6176 mlog_entry_void();
6177
6178 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6179 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6180 return -EINVAL;
6181 }
6182
6183 tl = &tl_copy->id2.i_dealloc;
6184 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08006185 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006186 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006187
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006188 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006189 for(i = 0; i < num_recs; i++) {
6190 if (ocfs2_truncate_log_needs_flush(osb)) {
6191 status = __ocfs2_flush_truncate_log(osb);
6192 if (status < 0) {
6193 mlog_errno(status);
6194 goto bail_up;
6195 }
6196 }
6197
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006198 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006199 if (IS_ERR(handle)) {
6200 status = PTR_ERR(handle);
6201 mlog_errno(status);
6202 goto bail_up;
6203 }
6204
6205 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6206 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6207 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6208
6209 status = ocfs2_truncate_log_append(osb, handle,
6210 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006211 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006212 if (status < 0) {
6213 mlog_errno(status);
6214 goto bail_up;
6215 }
6216 }
6217
6218bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006219 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006220
6221 mlog_exit(status);
6222 return status;
6223}
6224
6225void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6226{
6227 int status;
6228 struct inode *tl_inode = osb->osb_tl_inode;
6229
6230 mlog_entry_void();
6231
6232 if (tl_inode) {
6233 cancel_delayed_work(&osb->osb_truncate_log_wq);
6234 flush_workqueue(ocfs2_wq);
6235
6236 status = ocfs2_flush_truncate_log(osb);
6237 if (status < 0)
6238 mlog_errno(status);
6239
6240 brelse(osb->osb_tl_bh);
6241 iput(osb->osb_tl_inode);
6242 }
6243
6244 mlog_exit_void();
6245}
6246
6247int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6248{
6249 int status;
6250 struct inode *tl_inode = NULL;
6251 struct buffer_head *tl_bh = NULL;
6252
6253 mlog_entry_void();
6254
6255 status = ocfs2_get_truncate_log_info(osb,
6256 osb->slot_num,
6257 &tl_inode,
6258 &tl_bh);
6259 if (status < 0)
6260 mlog_errno(status);
6261
6262 /* ocfs2_truncate_log_shutdown keys on the existence of
6263 * osb->osb_tl_inode so we don't set any of the osb variables
6264 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006265 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6266 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006267 osb->osb_tl_bh = tl_bh;
6268 osb->osb_tl_inode = tl_inode;
6269
6270 mlog_exit(status);
6271 return status;
6272}
6273
Mark Fasheh2b604352007-06-22 15:45:27 -07006274/*
6275 * Delayed de-allocation of suballocator blocks.
6276 *
6277 * Some sets of block de-allocations might involve multiple suballocator inodes.
6278 *
6279 * The locking for this can get extremely complicated, especially when
6280 * the suballocator inodes to delete from aren't known until deep
6281 * within an unrelated codepath.
6282 *
6283 * ocfs2_extent_block structures are a good example of this - an inode
6284 * btree could have been grown by any number of nodes each allocating
6285 * out of their own suballoc inode.
6286 *
6287 * These structures allow the delay of block de-allocation until a
6288 * later time, when locking of multiple cluster inodes won't cause
6289 * deadlock.
6290 */
6291
6292/*
Tao Ma2891d292008-11-12 08:26:58 +08006293 * Describe a single bit freed from a suballocator. For the block
6294 * suballocators, it represents one block. For the global cluster
6295 * allocator, it represents some clusters and free_bit indicates
6296 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006297 */
6298struct ocfs2_cached_block_free {
6299 struct ocfs2_cached_block_free *free_next;
6300 u64 free_blk;
6301 unsigned int free_bit;
6302};
6303
6304struct ocfs2_per_slot_free_list {
6305 struct ocfs2_per_slot_free_list *f_next_suballocator;
6306 int f_inode_type;
6307 int f_slot;
6308 struct ocfs2_cached_block_free *f_first;
6309};
6310
Tao Ma2891d292008-11-12 08:26:58 +08006311static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6312 int sysfile_type,
6313 int slot,
6314 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006315{
6316 int ret;
6317 u64 bg_blkno;
6318 handle_t *handle;
6319 struct inode *inode;
6320 struct buffer_head *di_bh = NULL;
6321 struct ocfs2_cached_block_free *tmp;
6322
6323 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6324 if (!inode) {
6325 ret = -EINVAL;
6326 mlog_errno(ret);
6327 goto out;
6328 }
6329
6330 mutex_lock(&inode->i_mutex);
6331
Mark Fashehe63aecb62007-10-18 15:30:42 -07006332 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006333 if (ret) {
6334 mlog_errno(ret);
6335 goto out_mutex;
6336 }
6337
6338 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6339 if (IS_ERR(handle)) {
6340 ret = PTR_ERR(handle);
6341 mlog_errno(ret);
6342 goto out_unlock;
6343 }
6344
6345 while (head) {
6346 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6347 head->free_bit);
6348 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6349 head->free_bit, (unsigned long long)head->free_blk);
6350
6351 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6352 head->free_bit, bg_blkno, 1);
6353 if (ret) {
6354 mlog_errno(ret);
6355 goto out_journal;
6356 }
6357
6358 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6359 if (ret) {
6360 mlog_errno(ret);
6361 goto out_journal;
6362 }
6363
6364 tmp = head;
6365 head = head->free_next;
6366 kfree(tmp);
6367 }
6368
6369out_journal:
6370 ocfs2_commit_trans(osb, handle);
6371
6372out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006373 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006374 brelse(di_bh);
6375out_mutex:
6376 mutex_unlock(&inode->i_mutex);
6377 iput(inode);
6378out:
6379 while(head) {
6380 /* Premature exit may have left some dangling items. */
6381 tmp = head;
6382 head = head->free_next;
6383 kfree(tmp);
6384 }
6385
6386 return ret;
6387}
6388
Tao Ma2891d292008-11-12 08:26:58 +08006389int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6390 u64 blkno, unsigned int bit)
6391{
6392 int ret = 0;
6393 struct ocfs2_cached_block_free *item;
6394
6395 item = kmalloc(sizeof(*item), GFP_NOFS);
6396 if (item == NULL) {
6397 ret = -ENOMEM;
6398 mlog_errno(ret);
6399 return ret;
6400 }
6401
6402 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6403 bit, (unsigned long long)blkno);
6404
6405 item->free_blk = blkno;
6406 item->free_bit = bit;
6407 item->free_next = ctxt->c_global_allocator;
6408
6409 ctxt->c_global_allocator = item;
6410 return ret;
6411}
6412
6413static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6414 struct ocfs2_cached_block_free *head)
6415{
6416 struct ocfs2_cached_block_free *tmp;
6417 struct inode *tl_inode = osb->osb_tl_inode;
6418 handle_t *handle;
6419 int ret = 0;
6420
6421 mutex_lock(&tl_inode->i_mutex);
6422
6423 while (head) {
6424 if (ocfs2_truncate_log_needs_flush(osb)) {
6425 ret = __ocfs2_flush_truncate_log(osb);
6426 if (ret < 0) {
6427 mlog_errno(ret);
6428 break;
6429 }
6430 }
6431
6432 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6433 if (IS_ERR(handle)) {
6434 ret = PTR_ERR(handle);
6435 mlog_errno(ret);
6436 break;
6437 }
6438
6439 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6440 head->free_bit);
6441
6442 ocfs2_commit_trans(osb, handle);
6443 tmp = head;
6444 head = head->free_next;
6445 kfree(tmp);
6446
6447 if (ret < 0) {
6448 mlog_errno(ret);
6449 break;
6450 }
6451 }
6452
6453 mutex_unlock(&tl_inode->i_mutex);
6454
6455 while (head) {
6456 /* Premature exit may have left some dangling items. */
6457 tmp = head;
6458 head = head->free_next;
6459 kfree(tmp);
6460 }
6461
6462 return ret;
6463}
6464
Mark Fasheh2b604352007-06-22 15:45:27 -07006465int ocfs2_run_deallocs(struct ocfs2_super *osb,
6466 struct ocfs2_cached_dealloc_ctxt *ctxt)
6467{
6468 int ret = 0, ret2;
6469 struct ocfs2_per_slot_free_list *fl;
6470
6471 if (!ctxt)
6472 return 0;
6473
6474 while (ctxt->c_first_suballocator) {
6475 fl = ctxt->c_first_suballocator;
6476
6477 if (fl->f_first) {
6478 mlog(0, "Free items: (type %u, slot %d)\n",
6479 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006480 ret2 = ocfs2_free_cached_blocks(osb,
6481 fl->f_inode_type,
6482 fl->f_slot,
6483 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006484 if (ret2)
6485 mlog_errno(ret2);
6486 if (!ret)
6487 ret = ret2;
6488 }
6489
6490 ctxt->c_first_suballocator = fl->f_next_suballocator;
6491 kfree(fl);
6492 }
6493
Tao Ma2891d292008-11-12 08:26:58 +08006494 if (ctxt->c_global_allocator) {
6495 ret2 = ocfs2_free_cached_clusters(osb,
6496 ctxt->c_global_allocator);
6497 if (ret2)
6498 mlog_errno(ret2);
6499 if (!ret)
6500 ret = ret2;
6501
6502 ctxt->c_global_allocator = NULL;
6503 }
6504
Mark Fasheh2b604352007-06-22 15:45:27 -07006505 return ret;
6506}
6507
6508static struct ocfs2_per_slot_free_list *
6509ocfs2_find_per_slot_free_list(int type,
6510 int slot,
6511 struct ocfs2_cached_dealloc_ctxt *ctxt)
6512{
6513 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6514
6515 while (fl) {
6516 if (fl->f_inode_type == type && fl->f_slot == slot)
6517 return fl;
6518
6519 fl = fl->f_next_suballocator;
6520 }
6521
6522 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6523 if (fl) {
6524 fl->f_inode_type = type;
6525 fl->f_slot = slot;
6526 fl->f_first = NULL;
6527 fl->f_next_suballocator = ctxt->c_first_suballocator;
6528
6529 ctxt->c_first_suballocator = fl;
6530 }
6531 return fl;
6532}
6533
Tao Ma1823cb02009-08-18 11:24:49 +08006534int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6535 int type, int slot, u64 blkno,
6536 unsigned int bit)
Mark Fasheh2b604352007-06-22 15:45:27 -07006537{
6538 int ret;
6539 struct ocfs2_per_slot_free_list *fl;
6540 struct ocfs2_cached_block_free *item;
6541
6542 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6543 if (fl == NULL) {
6544 ret = -ENOMEM;
6545 mlog_errno(ret);
6546 goto out;
6547 }
6548
6549 item = kmalloc(sizeof(*item), GFP_NOFS);
6550 if (item == NULL) {
6551 ret = -ENOMEM;
6552 mlog_errno(ret);
6553 goto out;
6554 }
6555
6556 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6557 type, slot, bit, (unsigned long long)blkno);
6558
6559 item->free_blk = blkno;
6560 item->free_bit = bit;
6561 item->free_next = fl->f_first;
6562
6563 fl->f_first = item;
6564
6565 ret = 0;
6566out:
6567 return ret;
6568}
6569
Mark Fasheh59a5e412007-06-22 15:52:36 -07006570static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6571 struct ocfs2_extent_block *eb)
6572{
6573 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6574 le16_to_cpu(eb->h_suballoc_slot),
6575 le64_to_cpu(eb->h_blkno),
6576 le16_to_cpu(eb->h_suballoc_bit));
6577}
6578
Mark Fashehccd979b2005-12-15 14:31:24 -08006579/* This function will figure out whether the currently last extent
6580 * block will be deleted, and if it will, what the new last extent
6581 * block will be so we can update his h_next_leaf_blk field, as well
6582 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006583static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006584 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006585 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006586 struct buffer_head **new_last_eb)
6587{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006588 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006589 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006590 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006591 struct ocfs2_extent_block *eb;
6592 struct ocfs2_extent_list *el;
6593 struct buffer_head *bh = NULL;
6594
6595 *new_last_eb = NULL;
6596
Mark Fashehccd979b2005-12-15 14:31:24 -08006597 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006598 if (!path->p_tree_depth)
6599 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006600
6601 /* trunc to zero special case - this makes tree_depth = 0
6602 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006603 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006604 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006605
Mark Fashehdcd05382007-01-16 11:32:23 -08006606 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006607 BUG_ON(!el->l_next_free_rec);
6608
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006609 /*
6610 * Make sure that this extent list will actually be empty
6611 * after we clear away the data. We can shortcut out if
6612 * there's more than one non-empty extent in the
6613 * list. Otherwise, a check of the remaining extent is
6614 * necessary.
6615 */
6616 next_free = le16_to_cpu(el->l_next_free_rec);
6617 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006618 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006619 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006620 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006621
6622 /* We may have a valid extent in index 1, check it. */
6623 if (next_free == 2)
6624 rec = &el->l_recs[1];
6625
6626 /*
6627 * Fall through - no more nonempty extents, so we want
6628 * to delete this leaf.
6629 */
6630 } else {
6631 if (next_free > 1)
6632 goto out;
6633
6634 rec = &el->l_recs[0];
6635 }
6636
6637 if (rec) {
6638 /*
6639 * Check it we'll only be trimming off the end of this
6640 * cluster.
6641 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006642 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006643 goto out;
6644 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006645
Mark Fashehdcd05382007-01-16 11:32:23 -08006646 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6647 if (ret) {
6648 mlog_errno(ret);
6649 goto out;
6650 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006651
Joel Beckerfacdb772009-02-12 18:08:48 -08006652 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08006653 if (ret) {
6654 mlog_errno(ret);
6655 goto out;
6656 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006657
Mark Fashehdcd05382007-01-16 11:32:23 -08006658 eb = (struct ocfs2_extent_block *) bh->b_data;
6659 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006660
6661 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6662 * Any corruption is a code bug. */
6663 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006664
6665 *new_last_eb = bh;
6666 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006667 mlog(0, "returning block %llu, (cpos: %u)\n",
6668 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6669out:
6670 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006671
Mark Fashehdcd05382007-01-16 11:32:23 -08006672 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006673}
6674
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006675/*
6676 * Trim some clusters off the rightmost edge of a tree. Only called
6677 * during truncate.
6678 *
6679 * The caller needs to:
6680 * - start journaling of each path component.
6681 * - compute and fully set up any new last ext block
6682 */
6683static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6684 handle_t *handle, struct ocfs2_truncate_context *tc,
Tao Mabcbbb242009-08-18 11:29:12 +08006685 u32 clusters_to_del, u64 *delete_start, u8 *flags)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006686{
6687 int ret, i, index = path->p_tree_depth;
6688 u32 new_edge = 0;
6689 u64 deleted_eb = 0;
6690 struct buffer_head *bh;
6691 struct ocfs2_extent_list *el;
6692 struct ocfs2_extent_rec *rec;
6693
6694 *delete_start = 0;
Tao Mabcbbb242009-08-18 11:29:12 +08006695 *flags = 0;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006696
6697 while (index >= 0) {
6698 bh = path->p_node[index].bh;
6699 el = path->p_node[index].el;
6700
6701 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6702 index, (unsigned long long)bh->b_blocknr);
6703
6704 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6705
6706 if (index !=
6707 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6708 ocfs2_error(inode->i_sb,
6709 "Inode %lu has invalid ext. block %llu",
6710 inode->i_ino,
6711 (unsigned long long)bh->b_blocknr);
6712 ret = -EROFS;
6713 goto out;
6714 }
6715
6716find_tail_record:
6717 i = le16_to_cpu(el->l_next_free_rec) - 1;
6718 rec = &el->l_recs[i];
6719
6720 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6721 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006722 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006723 (unsigned long long)le64_to_cpu(rec->e_blkno),
6724 le16_to_cpu(el->l_next_free_rec));
6725
Mark Fashehe48edee2007-03-07 16:46:57 -08006726 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006727
6728 if (le16_to_cpu(el->l_tree_depth) == 0) {
6729 /*
6730 * If the leaf block contains a single empty
6731 * extent and no records, we can just remove
6732 * the block.
6733 */
6734 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6735 memset(rec, 0,
6736 sizeof(struct ocfs2_extent_rec));
6737 el->l_next_free_rec = cpu_to_le16(0);
6738
6739 goto delete;
6740 }
6741
6742 /*
6743 * Remove any empty extents by shifting things
6744 * left. That should make life much easier on
6745 * the code below. This condition is rare
6746 * enough that we shouldn't see a performance
6747 * hit.
6748 */
6749 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6750 le16_add_cpu(&el->l_next_free_rec, -1);
6751
6752 for(i = 0;
6753 i < le16_to_cpu(el->l_next_free_rec); i++)
6754 el->l_recs[i] = el->l_recs[i + 1];
6755
6756 memset(&el->l_recs[i], 0,
6757 sizeof(struct ocfs2_extent_rec));
6758
6759 /*
6760 * We've modified our extent list. The
6761 * simplest way to handle this change
6762 * is to being the search from the
6763 * start again.
6764 */
6765 goto find_tail_record;
6766 }
6767
Mark Fashehe48edee2007-03-07 16:46:57 -08006768 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006769
6770 /*
6771 * We'll use "new_edge" on our way back up the
6772 * tree to know what our rightmost cpos is.
6773 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006774 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006775 new_edge += le32_to_cpu(rec->e_cpos);
6776
6777 /*
6778 * The caller will use this to delete data blocks.
6779 */
6780 *delete_start = le64_to_cpu(rec->e_blkno)
6781 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006782 le16_to_cpu(rec->e_leaf_clusters));
Tao Mabcbbb242009-08-18 11:29:12 +08006783 *flags = rec->e_flags;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006784
6785 /*
6786 * If it's now empty, remove this record.
6787 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006788 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006789 memset(rec, 0,
6790 sizeof(struct ocfs2_extent_rec));
6791 le16_add_cpu(&el->l_next_free_rec, -1);
6792 }
6793 } else {
6794 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6795 memset(rec, 0,
6796 sizeof(struct ocfs2_extent_rec));
6797 le16_add_cpu(&el->l_next_free_rec, -1);
6798
6799 goto delete;
6800 }
6801
6802 /* Can this actually happen? */
6803 if (le16_to_cpu(el->l_next_free_rec) == 0)
6804 goto delete;
6805
6806 /*
6807 * We never actually deleted any clusters
6808 * because our leaf was empty. There's no
6809 * reason to adjust the rightmost edge then.
6810 */
6811 if (new_edge == 0)
6812 goto delete;
6813
Mark Fashehe48edee2007-03-07 16:46:57 -08006814 rec->e_int_clusters = cpu_to_le32(new_edge);
6815 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006816 -le32_to_cpu(rec->e_cpos));
6817
6818 /*
6819 * A deleted child record should have been
6820 * caught above.
6821 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006822 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006823 }
6824
6825delete:
6826 ret = ocfs2_journal_dirty(handle, bh);
6827 if (ret) {
6828 mlog_errno(ret);
6829 goto out;
6830 }
6831
6832 mlog(0, "extent list container %llu, after: record %d: "
6833 "(%u, %u, %llu), next = %u.\n",
6834 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006835 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006836 (unsigned long long)le64_to_cpu(rec->e_blkno),
6837 le16_to_cpu(el->l_next_free_rec));
6838
6839 /*
6840 * We must be careful to only attempt delete of an
6841 * extent block (and not the root inode block).
6842 */
6843 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6844 struct ocfs2_extent_block *eb =
6845 (struct ocfs2_extent_block *)bh->b_data;
6846
6847 /*
6848 * Save this for use when processing the
6849 * parent block.
6850 */
6851 deleted_eb = le64_to_cpu(eb->h_blkno);
6852
6853 mlog(0, "deleting this extent block.\n");
6854
Joel Becker8cb471e2009-02-10 20:00:41 -08006855 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006856
Mark Fashehe48edee2007-03-07 16:46:57 -08006857 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006858 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6859 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6860
Mark Fasheh59a5e412007-06-22 15:52:36 -07006861 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6862 /* An error here is not fatal. */
6863 if (ret < 0)
6864 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006865 } else {
6866 deleted_eb = 0;
6867 }
6868
6869 index--;
6870 }
6871
6872 ret = 0;
6873out:
6874 return ret;
6875}
6876
Mark Fashehccd979b2005-12-15 14:31:24 -08006877static int ocfs2_do_truncate(struct ocfs2_super *osb,
6878 unsigned int clusters_to_del,
6879 struct inode *inode,
6880 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006881 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006882 struct ocfs2_truncate_context *tc,
Tao Mabcbbb242009-08-18 11:29:12 +08006883 struct ocfs2_path *path,
6884 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08006885{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006886 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006887 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006888 struct ocfs2_extent_block *last_eb = NULL;
6889 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006890 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006891 u64 delete_blk = 0;
Tao Mabcbbb242009-08-18 11:29:12 +08006892 u8 rec_flags;
Mark Fashehccd979b2005-12-15 14:31:24 -08006893
6894 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6895
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006896 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006897 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006898 if (status < 0) {
6899 mlog_errno(status);
6900 goto bail;
6901 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006902
Mark Fashehdcd05382007-01-16 11:32:23 -08006903 /*
6904 * Each component will be touched, so we might as well journal
6905 * here to avoid having to handle errors later.
6906 */
Joel Becker0cf2f762009-02-12 16:41:25 -08006907 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006908 if (status < 0) {
6909 mlog_errno(status);
6910 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006911 }
6912
6913 if (last_eb_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08006914 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07006915 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08006916 if (status < 0) {
6917 mlog_errno(status);
6918 goto bail;
6919 }
6920
6921 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6922 }
6923
6924 el = &(fe->id2.i_list);
6925
6926 /*
6927 * Lower levels depend on this never happening, but it's best
6928 * to check it up here before changing the tree.
6929 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006930 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006931 ocfs2_error(inode->i_sb,
6932 "Inode %lu has an empty extent record, depth %u\n",
6933 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006934 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006935 goto bail;
6936 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006937
Jan Karaa90714c2008-10-09 19:38:40 +02006938 vfs_dq_free_space_nodirty(inode,
6939 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006940 spin_lock(&OCFS2_I(inode)->ip_lock);
6941 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6942 clusters_to_del;
6943 spin_unlock(&OCFS2_I(inode)->ip_lock);
6944 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006945 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006946
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006947 status = ocfs2_trim_tree(inode, path, handle, tc,
Tao Mabcbbb242009-08-18 11:29:12 +08006948 clusters_to_del, &delete_blk, &rec_flags);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006949 if (status) {
6950 mlog_errno(status);
6951 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006952 }
6953
Mark Fashehdcd05382007-01-16 11:32:23 -08006954 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006955 /* trunc to zero is a special case. */
6956 el->l_tree_depth = 0;
6957 fe->i_last_eb_blk = 0;
6958 } else if (last_eb)
6959 fe->i_last_eb_blk = last_eb->h_blkno;
6960
6961 status = ocfs2_journal_dirty(handle, fe_bh);
6962 if (status < 0) {
6963 mlog_errno(status);
6964 goto bail;
6965 }
6966
6967 if (last_eb) {
6968 /* If there will be a new last extent block, then by
6969 * definition, there cannot be any leaves to the right of
6970 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006971 last_eb->h_next_leaf_blk = 0;
6972 status = ocfs2_journal_dirty(handle, last_eb_bh);
6973 if (status < 0) {
6974 mlog_errno(status);
6975 goto bail;
6976 }
6977 }
6978
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006979 if (delete_blk) {
Tao Mabcbbb242009-08-18 11:29:12 +08006980 if (rec_flags & OCFS2_EXT_REFCOUNTED)
6981 status = ocfs2_decrease_refcount(inode, handle,
6982 ocfs2_blocks_to_clusters(osb->sb,
6983 delete_blk),
6984 clusters_to_del, meta_ac,
Tao Ma6ae23c52009-08-18 11:30:55 +08006985 &tc->tc_dealloc, 1);
Tao Mabcbbb242009-08-18 11:29:12 +08006986 else
6987 status = ocfs2_truncate_log_append(osb, handle,
6988 delete_blk,
6989 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006990 if (status < 0) {
6991 mlog_errno(status);
6992 goto bail;
6993 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006994 }
6995 status = 0;
6996bail:
Tao Ma60e2ec42009-08-12 14:42:47 +08006997 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006998 mlog_exit(status);
6999 return status;
7000}
7001
Joel Becker2b4e30f2008-09-03 20:03:41 -07007002static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08007003{
7004 set_buffer_uptodate(bh);
7005 mark_buffer_dirty(bh);
7006 return 0;
7007}
7008
Tao Ma6f70fa52009-08-25 08:05:12 +08007009void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
7010 unsigned int from, unsigned int to,
7011 struct page *page, int zero, u64 *phys)
Mark Fasheh1d410a62007-09-07 14:20:45 -07007012{
7013 int ret, partial = 0;
7014
7015 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
7016 if (ret)
7017 mlog_errno(ret);
7018
7019 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08007020 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07007021
7022 /*
7023 * Need to set the buffers we zero'd into uptodate
7024 * here if they aren't - ocfs2_map_page_blocks()
7025 * might've skipped some
7026 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07007027 ret = walk_page_buffers(handle, page_buffers(page),
7028 from, to, &partial,
7029 ocfs2_zero_func);
7030 if (ret < 0)
7031 mlog_errno(ret);
7032 else if (ocfs2_should_order_data(inode)) {
7033 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07007034 if (ret < 0)
7035 mlog_errno(ret);
7036 }
7037
7038 if (!partial)
7039 SetPageUptodate(page);
7040
7041 flush_dcache_page(page);
7042}
7043
Mark Fasheh35edec12007-07-06 14:41:18 -07007044static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
7045 loff_t end, struct page **pages,
7046 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08007047{
Mark Fasheh1d410a62007-09-07 14:20:45 -07007048 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08007049 struct page *page;
7050 unsigned int from, to = PAGE_CACHE_SIZE;
7051 struct super_block *sb = inode->i_sb;
7052
7053 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
7054
7055 if (numpages == 0)
7056 goto out;
7057
Mark Fasheh35edec12007-07-06 14:41:18 -07007058 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08007059 for(i = 0; i < numpages; i++) {
7060 page = pages[i];
7061
Mark Fasheh35edec12007-07-06 14:41:18 -07007062 from = start & (PAGE_CACHE_SIZE - 1);
7063 if ((end >> PAGE_CACHE_SHIFT) == page->index)
7064 to = end & (PAGE_CACHE_SIZE - 1);
7065
Mark Fasheh60b11392007-02-16 11:46:50 -08007066 BUG_ON(from > PAGE_CACHE_SIZE);
7067 BUG_ON(to > PAGE_CACHE_SIZE);
7068
Mark Fasheh1d410a62007-09-07 14:20:45 -07007069 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
7070 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08007071
Mark Fasheh35edec12007-07-06 14:41:18 -07007072 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08007073 }
7074out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07007075 if (pages)
7076 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08007077}
7078
Tao Ma6f70fa52009-08-25 08:05:12 +08007079int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
7080 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08007081{
Mark Fasheh1d410a62007-09-07 14:20:45 -07007082 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08007083 struct address_space *mapping = inode->i_mapping;
7084 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07007085 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08007086
Mark Fasheh35edec12007-07-06 14:41:18 -07007087 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08007088
Mark Fasheh1d410a62007-09-07 14:20:45 -07007089 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07007090 last_page_bytes = PAGE_ALIGN(end);
7091 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08007092 do {
7093 pages[numpages] = grab_cache_page(mapping, index);
7094 if (!pages[numpages]) {
7095 ret = -ENOMEM;
7096 mlog_errno(ret);
7097 goto out;
7098 }
7099
7100 numpages++;
7101 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07007102 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08007103
7104out:
7105 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07007106 if (pages)
7107 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08007108 numpages = 0;
7109 }
7110
7111 *num = numpages;
7112
7113 return ret;
7114}
7115
Tao Ma6f70fa52009-08-25 08:05:12 +08007116static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
7117 struct page **pages, int *num)
7118{
7119 struct super_block *sb = inode->i_sb;
7120
7121 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
7122 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
7123
7124 return ocfs2_grab_pages(inode, start, end, pages, num);
7125}
7126
Mark Fasheh60b11392007-02-16 11:46:50 -08007127/*
7128 * Zero the area past i_size but still within an allocated
7129 * cluster. This avoids exposing nonzero data on subsequent file
7130 * extends.
7131 *
7132 * We need to call this before i_size is updated on the inode because
7133 * otherwise block_write_full_page() will skip writeout of pages past
7134 * i_size. The new_i_size parameter is passed for this reason.
7135 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007136int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
7137 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08007138{
Mark Fasheh1d410a62007-09-07 14:20:45 -07007139 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08007140 struct page **pages = NULL;
7141 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07007142 unsigned int ext_flags;
7143 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08007144
7145 /*
7146 * File systems which don't support sparse files zero on every
7147 * extend.
7148 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07007149 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08007150 return 0;
7151
Mark Fasheh1d410a62007-09-07 14:20:45 -07007152 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08007153 sizeof(struct page *), GFP_NOFS);
7154 if (pages == NULL) {
7155 ret = -ENOMEM;
7156 mlog_errno(ret);
7157 goto out;
7158 }
7159
Mark Fasheh1d410a62007-09-07 14:20:45 -07007160 if (range_start == range_end)
7161 goto out;
7162
7163 ret = ocfs2_extent_map_get_blocks(inode,
7164 range_start >> sb->s_blocksize_bits,
7165 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08007166 if (ret) {
7167 mlog_errno(ret);
7168 goto out;
7169 }
7170
Mark Fasheh1d410a62007-09-07 14:20:45 -07007171 /*
7172 * Tail is a hole, or is marked unwritten. In either case, we
7173 * can count on read and write to return/push zero's.
7174 */
7175 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08007176 goto out;
7177
Mark Fasheh1d410a62007-09-07 14:20:45 -07007178 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7179 &numpages);
7180 if (ret) {
7181 mlog_errno(ret);
7182 goto out;
7183 }
7184
Mark Fasheh35edec12007-07-06 14:41:18 -07007185 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7186 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08007187
7188 /*
7189 * Initiate writeout of the pages we zero'd here. We don't
7190 * wait on them - the truncate_inode_pages() call later will
7191 * do that for us.
7192 */
Christoph Hellwig2cfd30a2009-09-23 15:04:02 +02007193 ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
7194 range_end - 1);
Mark Fasheh60b11392007-02-16 11:46:50 -08007195 if (ret)
7196 mlog_errno(ret);
7197
7198out:
7199 if (pages)
7200 kfree(pages);
7201
7202 return ret;
7203}
7204
Tiger Yangfdd77702008-08-18 17:08:55 +08007205static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7206 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007207{
7208 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08007209 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007210
Tiger Yangfdd77702008-08-18 17:08:55 +08007211 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7212 memset(&di->id2, 0, blocksize -
7213 offsetof(struct ocfs2_dinode, id2) -
7214 xattrsize);
7215 else
7216 memset(&di->id2, 0, blocksize -
7217 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007218}
7219
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007220void ocfs2_dinode_new_extent_list(struct inode *inode,
7221 struct ocfs2_dinode *di)
7222{
Tiger Yangfdd77702008-08-18 17:08:55 +08007223 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007224 di->id2.i_list.l_tree_depth = 0;
7225 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08007226 di->id2.i_list.l_count = cpu_to_le16(
7227 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007228}
7229
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007230void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7231{
7232 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7233 struct ocfs2_inline_data *idata = &di->id2.i_data;
7234
7235 spin_lock(&oi->ip_lock);
7236 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7237 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7238 spin_unlock(&oi->ip_lock);
7239
7240 /*
7241 * We clear the entire i_data structure here so that all
7242 * fields can be properly initialized.
7243 */
Tiger Yangfdd77702008-08-18 17:08:55 +08007244 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007245
Tiger Yangfdd77702008-08-18 17:08:55 +08007246 idata->id_count = cpu_to_le16(
7247 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007248}
7249
7250int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7251 struct buffer_head *di_bh)
7252{
7253 int ret, i, has_data, num_pages = 0;
7254 handle_t *handle;
7255 u64 uninitialized_var(block);
7256 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7257 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7258 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007259 struct ocfs2_alloc_context *data_ac = NULL;
7260 struct page **pages = NULL;
7261 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07007262 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02007263 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007264
7265 has_data = i_size_read(inode) ? 1 : 0;
7266
7267 if (has_data) {
7268 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7269 sizeof(struct page *), GFP_NOFS);
7270 if (pages == NULL) {
7271 ret = -ENOMEM;
7272 mlog_errno(ret);
7273 goto out;
7274 }
7275
7276 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7277 if (ret) {
7278 mlog_errno(ret);
7279 goto out;
7280 }
7281 }
7282
Jan Karaa90714c2008-10-09 19:38:40 +02007283 handle = ocfs2_start_trans(osb,
7284 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007285 if (IS_ERR(handle)) {
7286 ret = PTR_ERR(handle);
7287 mlog_errno(ret);
7288 goto out_unlock;
7289 }
7290
Joel Becker0cf2f762009-02-12 16:41:25 -08007291 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007292 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007293 if (ret) {
7294 mlog_errno(ret);
7295 goto out_commit;
7296 }
7297
7298 if (has_data) {
7299 u32 bit_off, num;
7300 unsigned int page_end;
7301 u64 phys;
7302
Jan Karaa90714c2008-10-09 19:38:40 +02007303 if (vfs_dq_alloc_space_nodirty(inode,
7304 ocfs2_clusters_to_bytes(osb->sb, 1))) {
7305 ret = -EDQUOT;
7306 goto out_commit;
7307 }
7308 did_quota = 1;
7309
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007310 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7311 &num);
7312 if (ret) {
7313 mlog_errno(ret);
7314 goto out_commit;
7315 }
7316
7317 /*
7318 * Save two copies, one for insert, and one that can
7319 * be changed by ocfs2_map_and_dirty_page() below.
7320 */
7321 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7322
7323 /*
7324 * Non sparse file systems zero on extend, so no need
7325 * to do that now.
7326 */
7327 if (!ocfs2_sparse_alloc(osb) &&
7328 PAGE_CACHE_SIZE < osb->s_clustersize)
7329 end = PAGE_CACHE_SIZE;
7330
7331 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7332 if (ret) {
7333 mlog_errno(ret);
7334 goto out_commit;
7335 }
7336
7337 /*
7338 * This should populate the 1st page for us and mark
7339 * it up to date.
7340 */
7341 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7342 if (ret) {
7343 mlog_errno(ret);
7344 goto out_commit;
7345 }
7346
7347 page_end = PAGE_CACHE_SIZE;
7348 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7349 page_end = osb->s_clustersize;
7350
7351 for (i = 0; i < num_pages; i++)
7352 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7353 pages[i], i > 0, &phys);
7354 }
7355
7356 spin_lock(&oi->ip_lock);
7357 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7358 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7359 spin_unlock(&oi->ip_lock);
7360
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007361 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007362
7363 ocfs2_journal_dirty(handle, di_bh);
7364
7365 if (has_data) {
7366 /*
7367 * An error at this point should be extremely rare. If
7368 * this proves to be false, we could always re-build
7369 * the in-inode data from our pages.
7370 */
Joel Becker5e404e92009-02-13 03:54:22 -08007371 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
Joel Beckercc79d8c2009-02-13 03:24:43 -08007372 ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007373 if (ret) {
7374 mlog_errno(ret);
7375 goto out_commit;
7376 }
7377
7378 inode->i_blocks = ocfs2_inode_sector_count(inode);
7379 }
7380
7381out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007382 if (ret < 0 && did_quota)
7383 vfs_dq_free_space_nodirty(inode,
7384 ocfs2_clusters_to_bytes(osb->sb, 1));
7385
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007386 ocfs2_commit_trans(osb, handle);
7387
7388out_unlock:
7389 if (data_ac)
7390 ocfs2_free_alloc_context(data_ac);
7391
7392out:
7393 if (pages) {
7394 ocfs2_unlock_and_free_pages(pages, num_pages);
7395 kfree(pages);
7396 }
7397
7398 return ret;
7399}
7400
Mark Fashehccd979b2005-12-15 14:31:24 -08007401/*
7402 * It is expected, that by the time you call this function,
7403 * inode->i_size and fe->i_size have been adjusted.
7404 *
7405 * WARNING: This will kfree the truncate context
7406 */
7407int ocfs2_commit_truncate(struct ocfs2_super *osb,
7408 struct inode *inode,
7409 struct buffer_head *fe_bh,
7410 struct ocfs2_truncate_context *tc)
7411{
7412 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08007413 u32 clusters_to_del, new_highest_cpos, range;
Tao Mabcbbb242009-08-18 11:29:12 +08007414 u64 blkno = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08007415 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07007416 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007417 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08007418 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08007419 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Tao Mabcbbb242009-08-18 11:29:12 +08007420 struct ocfs2_alloc_context *meta_ac = NULL;
7421 struct ocfs2_refcount_tree *ref_tree = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007422
7423 mlog_entry_void();
7424
Mark Fashehdcd05382007-01-16 11:32:23 -08007425 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007426 i_size_read(inode));
7427
Joel Becker13723d02008-10-17 19:25:01 -07007428 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7429 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08007430 if (!path) {
7431 status = -ENOMEM;
7432 mlog_errno(status);
7433 goto bail;
7434 }
Mark Fasheh83418972007-04-23 18:53:12 -07007435
7436 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7437
Mark Fashehccd979b2005-12-15 14:31:24 -08007438start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007439 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007440 * Check that we still have allocation to delete.
7441 */
7442 if (OCFS2_I(inode)->ip_clusters == 0) {
7443 status = 0;
7444 goto bail;
7445 }
7446
Tao Mabcbbb242009-08-18 11:29:12 +08007447 credits = 0;
7448
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007449 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007450 * Truncate always works against the rightmost tree branch.
7451 */
Joel Beckerfacdb772009-02-12 18:08:48 -08007452 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
Mark Fashehdcd05382007-01-16 11:32:23 -08007453 if (status) {
7454 mlog_errno(status);
7455 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007456 }
7457
Mark Fashehdcd05382007-01-16 11:32:23 -08007458 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7459 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7460
7461 /*
7462 * By now, el will point to the extent list on the bottom most
7463 * portion of this tree. Only the tail record is considered in
7464 * each pass.
7465 *
7466 * We handle the following cases, in order:
7467 * - empty extent: delete the remaining branch
7468 * - remove the entire record
7469 * - remove a partial record
7470 * - no record needs to be removed (truncate has completed)
7471 */
7472 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007473 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7474 ocfs2_error(inode->i_sb,
7475 "Inode %llu has empty extent block at %llu\n",
7476 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7477 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7478 status = -EROFS;
7479 goto bail;
7480 }
7481
Mark Fashehccd979b2005-12-15 14:31:24 -08007482 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08007483 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08007484 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007485 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7486 clusters_to_del = 0;
7487 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007488 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Tao Mabcbbb242009-08-18 11:29:12 +08007489 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08007490 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007491 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08007492 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08007493 new_highest_cpos;
Tao Mabcbbb242009-08-18 11:29:12 +08007494 blkno = le64_to_cpu(el->l_recs[i].e_blkno) +
7495 ocfs2_clusters_to_blocks(inode->i_sb,
7496 ocfs2_rec_clusters(el, &el->l_recs[i]) -
7497 clusters_to_del);
Mark Fashehdcd05382007-01-16 11:32:23 -08007498 } else {
7499 status = 0;
7500 goto bail;
7501 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007502
Mark Fashehdcd05382007-01-16 11:32:23 -08007503 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7504 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7505
Tao Mabcbbb242009-08-18 11:29:12 +08007506 if (el->l_recs[i].e_flags & OCFS2_EXT_REFCOUNTED && clusters_to_del) {
7507 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
7508 OCFS2_HAS_REFCOUNT_FL));
7509
7510 status = ocfs2_lock_refcount_tree(osb,
7511 le64_to_cpu(di->i_refcount_loc),
7512 1, &ref_tree, NULL);
7513 if (status) {
7514 mlog_errno(status);
7515 goto bail;
7516 }
7517
7518 status = ocfs2_prepare_refcount_change_for_del(inode, fe_bh,
7519 blkno,
7520 clusters_to_del,
7521 &credits,
7522 &meta_ac);
7523 if (status < 0) {
7524 mlog_errno(status);
7525 goto bail;
7526 }
7527 }
7528
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007529 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007530 tl_sem = 1;
7531 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7532 * record is free for use. If there isn't any, we flush to get
7533 * an empty truncate log. */
7534 if (ocfs2_truncate_log_needs_flush(osb)) {
7535 status = __ocfs2_flush_truncate_log(osb);
7536 if (status < 0) {
7537 mlog_errno(status);
7538 goto bail;
7539 }
7540 }
7541
Tao Mabcbbb242009-08-18 11:29:12 +08007542 credits += ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007543 (struct ocfs2_dinode *)fe_bh->b_data,
7544 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007545 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007546 if (IS_ERR(handle)) {
7547 status = PTR_ERR(handle);
7548 handle = NULL;
7549 mlog_errno(status);
7550 goto bail;
7551 }
7552
Mark Fashehdcd05382007-01-16 11:32:23 -08007553 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
Tao Mabcbbb242009-08-18 11:29:12 +08007554 tc, path, meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08007555 if (status < 0) {
7556 mlog_errno(status);
7557 goto bail;
7558 }
7559
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007560 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007561 tl_sem = 0;
7562
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007563 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007564 handle = NULL;
7565
Mark Fashehdcd05382007-01-16 11:32:23 -08007566 ocfs2_reinit_path(path, 1);
7567
Tao Mabcbbb242009-08-18 11:29:12 +08007568 if (meta_ac) {
7569 ocfs2_free_alloc_context(meta_ac);
7570 meta_ac = NULL;
7571 }
7572
7573 if (ref_tree) {
7574 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
7575 ref_tree = NULL;
7576 }
7577
Mark Fashehdcd05382007-01-16 11:32:23 -08007578 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007579 * The check above will catch the case where we've truncated
7580 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007581 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007582 goto start;
7583
Mark Fashehccd979b2005-12-15 14:31:24 -08007584bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007585
7586 ocfs2_schedule_truncate_log_flush(osb, 1);
7587
7588 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007589 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007590
7591 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007592 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007593
Tao Mabcbbb242009-08-18 11:29:12 +08007594 if (meta_ac)
7595 ocfs2_free_alloc_context(meta_ac);
7596
7597 if (ref_tree)
7598 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
7599
Mark Fasheh59a5e412007-06-22 15:52:36 -07007600 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7601
Mark Fashehdcd05382007-01-16 11:32:23 -08007602 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007603
7604 /* This will drop the ext_alloc cluster lock for us */
7605 ocfs2_free_truncate_context(tc);
7606
7607 mlog_exit(status);
7608 return status;
7609}
7610
Mark Fashehccd979b2005-12-15 14:31:24 -08007611/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007612 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007613 */
7614int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7615 struct inode *inode,
7616 struct buffer_head *fe_bh,
7617 struct ocfs2_truncate_context **tc)
7618{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007619 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007620 unsigned int new_i_clusters;
7621 struct ocfs2_dinode *fe;
7622 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007623 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007624
7625 mlog_entry_void();
7626
7627 *tc = NULL;
7628
7629 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7630 i_size_read(inode));
7631 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7632
7633 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007634 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7635 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007636
Robert P. J. Daycd861282006-12-13 00:34:52 -08007637 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007638 if (!(*tc)) {
7639 status = -ENOMEM;
7640 mlog_errno(status);
7641 goto bail;
7642 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007643 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007644
Mark Fashehccd979b2005-12-15 14:31:24 -08007645 if (fe->id2.i_list.l_tree_depth) {
Joel Becker3d03a302009-02-12 17:49:26 -08007646 status = ocfs2_read_extent_block(INODE_CACHE(inode),
Joel Becker5e965812008-11-13 14:49:16 -08007647 le64_to_cpu(fe->i_last_eb_blk),
7648 &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007649 if (status < 0) {
7650 mlog_errno(status);
7651 goto bail;
7652 }
7653 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007654 }
7655
7656 (*tc)->tc_last_eb_bh = last_eb_bh;
7657
Mark Fashehccd979b2005-12-15 14:31:24 -08007658 status = 0;
7659bail:
7660 if (status < 0) {
7661 if (*tc)
7662 ocfs2_free_truncate_context(*tc);
7663 *tc = NULL;
7664 }
7665 mlog_exit_void();
7666 return status;
7667}
7668
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007669/*
7670 * 'start' is inclusive, 'end' is not.
7671 */
7672int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7673 unsigned int start, unsigned int end, int trunc)
7674{
7675 int ret;
7676 unsigned int numbytes;
7677 handle_t *handle;
7678 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7679 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7680 struct ocfs2_inline_data *idata = &di->id2.i_data;
7681
7682 if (end > i_size_read(inode))
7683 end = i_size_read(inode);
7684
7685 BUG_ON(start >= end);
7686
7687 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7688 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7689 !ocfs2_supports_inline_data(osb)) {
7690 ocfs2_error(inode->i_sb,
7691 "Inline data flags for inode %llu don't agree! "
7692 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7693 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7694 le16_to_cpu(di->i_dyn_features),
7695 OCFS2_I(inode)->ip_dyn_features,
7696 osb->s_feature_incompat);
7697 ret = -EROFS;
7698 goto out;
7699 }
7700
7701 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7702 if (IS_ERR(handle)) {
7703 ret = PTR_ERR(handle);
7704 mlog_errno(ret);
7705 goto out;
7706 }
7707
Joel Becker0cf2f762009-02-12 16:41:25 -08007708 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007709 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007710 if (ret) {
7711 mlog_errno(ret);
7712 goto out_commit;
7713 }
7714
7715 numbytes = end - start;
7716 memset(idata->id_data + start, 0, numbytes);
7717
7718 /*
7719 * No need to worry about the data page here - it's been
7720 * truncated already and inline data doesn't need it for
7721 * pushing zero's to disk, so we'll let readpage pick it up
7722 * later.
7723 */
7724 if (trunc) {
7725 i_size_write(inode, start);
7726 di->i_size = cpu_to_le64(start);
7727 }
7728
7729 inode->i_blocks = ocfs2_inode_sector_count(inode);
7730 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7731
7732 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7733 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7734
7735 ocfs2_journal_dirty(handle, di_bh);
7736
7737out_commit:
7738 ocfs2_commit_trans(osb, handle);
7739
7740out:
7741 return ret;
7742}
7743
Mark Fashehccd979b2005-12-15 14:31:24 -08007744static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7745{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007746 /*
7747 * The caller is responsible for completing deallocation
7748 * before freeing the context.
7749 */
7750 if (tc->tc_dealloc.c_first_suballocator != NULL)
7751 mlog(ML_NOTICE,
7752 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007753
Mark Fasheha81cb882008-10-07 14:25:16 -07007754 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007755
7756 kfree(tc);
7757}