blob: 9f8bd913c51ebfb1bd6463615d5dde04ee940708 [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);
Tiger Yangb89c5422010-01-25 14:11:06 +08001053 eb->h_suballoc_slot =
1054 cpu_to_le16(meta_ac->ac_alloc_slot);
Mark Fashehccd979b2005-12-15 14:31:24 -08001055 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1056 eb->h_list.l_count =
1057 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1058
1059 suballoc_bit_start++;
1060 first_blkno++;
1061
1062 /* We'll also be dirtied by the caller, so
1063 * this isn't absolutely necessary. */
1064 status = ocfs2_journal_dirty(handle, bhs[i]);
1065 if (status < 0) {
1066 mlog_errno(status);
1067 goto bail;
1068 }
1069 }
1070
1071 count += num_got;
1072 }
1073
1074 status = 0;
1075bail:
1076 if (status < 0) {
1077 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001078 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001079 bhs[i] = NULL;
1080 }
1081 }
1082 mlog_exit(status);
1083 return status;
1084}
1085
1086/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001087 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1088 *
1089 * Returns the sum of the rightmost extent rec logical offset and
1090 * cluster count.
1091 *
1092 * ocfs2_add_branch() uses this to determine what logical cluster
1093 * value should be populated into the leftmost new branch records.
1094 *
1095 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1096 * value for the new topmost tree record.
1097 */
1098static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1099{
1100 int i;
1101
1102 i = le16_to_cpu(el->l_next_free_rec) - 1;
1103
1104 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001105 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08001106}
1107
1108/*
Tao Ma6b791bc2009-06-12 14:18:36 +08001109 * Change range of the branches in the right most path according to the leaf
1110 * extent block's rightmost record.
1111 */
1112static int ocfs2_adjust_rightmost_branch(handle_t *handle,
Tao Ma6b791bc2009-06-12 14:18:36 +08001113 struct ocfs2_extent_tree *et)
1114{
1115 int status;
1116 struct ocfs2_path *path = NULL;
1117 struct ocfs2_extent_list *el;
1118 struct ocfs2_extent_rec *rec;
1119
1120 path = ocfs2_new_path_from_et(et);
1121 if (!path) {
1122 status = -ENOMEM;
1123 return status;
1124 }
1125
Joel Beckerfacdb772009-02-12 18:08:48 -08001126 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
Tao Ma6b791bc2009-06-12 14:18:36 +08001127 if (status < 0) {
1128 mlog_errno(status);
1129 goto out;
1130 }
1131
1132 status = ocfs2_extend_trans(handle, path_num_items(path) +
1133 handle->h_buffer_credits);
1134 if (status < 0) {
1135 mlog_errno(status);
1136 goto out;
1137 }
1138
Joel Beckerd401dc12009-02-13 02:24:10 -08001139 status = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma6b791bc2009-06-12 14:18:36 +08001140 if (status < 0) {
1141 mlog_errno(status);
1142 goto out;
1143 }
1144
1145 el = path_leaf_el(path);
1146 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1147
Joel Beckerd401dc12009-02-13 02:24:10 -08001148 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Tao Ma6b791bc2009-06-12 14:18:36 +08001149
1150out:
1151 ocfs2_free_path(path);
1152 return status;
1153}
1154
1155/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001156 * Add an entire tree branch to our inode. eb_bh is the extent block
Joel Beckerd401dc12009-02-13 02:24:10 -08001157 * to start at, if we don't want to start the branch at the root
Mark Fashehccd979b2005-12-15 14:31:24 -08001158 * structure.
1159 *
1160 * last_eb_bh is required as we have to update it's next_leaf pointer
1161 * for the new last extent block.
1162 *
1163 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -08001164 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -08001165 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001166static int ocfs2_add_branch(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001167 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001168 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07001169 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001170 struct ocfs2_alloc_context *meta_ac)
1171{
1172 int status, new_blocks, i;
1173 u64 next_blkno, new_last_eb_blk;
1174 struct buffer_head *bh;
1175 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001176 struct ocfs2_extent_block *eb;
1177 struct ocfs2_extent_list *eb_el;
1178 struct ocfs2_extent_list *el;
Tao Ma6b791bc2009-06-12 14:18:36 +08001179 u32 new_cpos, root_end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001180
1181 mlog_entry_void();
1182
Mark Fasheh328d5752007-06-18 10:48:04 -07001183 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001184
Mark Fashehccd979b2005-12-15 14:31:24 -08001185 if (eb_bh) {
1186 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1187 el = &eb->h_list;
1188 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001189 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001190
1191 /* we never add a branch to a leaf. */
1192 BUG_ON(!el->l_tree_depth);
1193
1194 new_blocks = le16_to_cpu(el->l_tree_depth);
1195
Tao Ma6b791bc2009-06-12 14:18:36 +08001196 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1197 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1198 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1199
1200 /*
1201 * If there is a gap before the root end and the real end
1202 * of the righmost leaf block, we need to remove the gap
1203 * between new_cpos and root_end first so that the tree
1204 * is consistent after we add a new branch(it will start
1205 * from new_cpos).
1206 */
1207 if (root_end > new_cpos) {
1208 mlog(0, "adjust the cluster end from %u to %u\n",
1209 root_end, new_cpos);
Joel Beckerd401dc12009-02-13 02:24:10 -08001210 status = ocfs2_adjust_rightmost_branch(handle, et);
Tao Ma6b791bc2009-06-12 14:18:36 +08001211 if (status) {
1212 mlog_errno(status);
1213 goto bail;
1214 }
1215 }
1216
Mark Fashehccd979b2005-12-15 14:31:24 -08001217 /* allocate the number of new eb blocks we need */
1218 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1219 GFP_KERNEL);
1220 if (!new_eb_bhs) {
1221 status = -ENOMEM;
1222 mlog_errno(status);
1223 goto bail;
1224 }
1225
Joel Becker42a5a7a2009-02-12 18:49:19 -08001226 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
Mark Fashehccd979b2005-12-15 14:31:24 -08001227 meta_ac, new_eb_bhs);
1228 if (status < 0) {
1229 mlog_errno(status);
1230 goto bail;
1231 }
1232
1233 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1234 * linked with the rest of the tree.
1235 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1236 *
1237 * when we leave the loop, new_last_eb_blk will point to the
1238 * newest leaf, and next_blkno will point to the topmost extent
1239 * block. */
1240 next_blkno = new_last_eb_blk = 0;
1241 for(i = 0; i < new_blocks; i++) {
1242 bh = new_eb_bhs[i];
1243 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001244 /* ocfs2_create_new_meta_bhs() should create it right! */
1245 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001246 eb_el = &eb->h_list;
1247
Joel Beckerd401dc12009-02-13 02:24:10 -08001248 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
Joel Becker13723d02008-10-17 19:25:01 -07001249 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001250 if (status < 0) {
1251 mlog_errno(status);
1252 goto bail;
1253 }
1254
1255 eb->h_next_leaf_blk = 0;
1256 eb_el->l_tree_depth = cpu_to_le16(i);
1257 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001258 /*
1259 * This actually counts as an empty extent as
1260 * c_clusters == 0
1261 */
1262 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001263 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001264 /*
1265 * eb_el isn't always an interior node, but even leaf
1266 * nodes want a zero'd flags and reserved field so
1267 * this gets the whole 32 bits regardless of use.
1268 */
1269 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001270 if (!eb_el->l_tree_depth)
1271 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1272
1273 status = ocfs2_journal_dirty(handle, bh);
1274 if (status < 0) {
1275 mlog_errno(status);
1276 goto bail;
1277 }
1278
1279 next_blkno = le64_to_cpu(eb->h_blkno);
1280 }
1281
1282 /* This is a bit hairy. We want to update up to three blocks
1283 * here without leaving any of them in an inconsistent state
1284 * in case of error. We don't have to worry about
1285 * journal_dirty erroring as it won't unless we've aborted the
1286 * handle (in which case we would never be here) so reserving
1287 * the write with journal_access is all we need to do. */
Joel Beckerd401dc12009-02-13 02:24:10 -08001288 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001289 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001290 if (status < 0) {
1291 mlog_errno(status);
1292 goto bail;
1293 }
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001294 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001295 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001296 if (status < 0) {
1297 mlog_errno(status);
1298 goto bail;
1299 }
1300 if (eb_bh) {
Joel Beckerd401dc12009-02-13 02:24:10 -08001301 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001302 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001303 if (status < 0) {
1304 mlog_errno(status);
1305 goto bail;
1306 }
1307 }
1308
1309 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001310 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001311 i = le16_to_cpu(el->l_next_free_rec);
1312 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001313 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001314 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001315 le16_add_cpu(&el->l_next_free_rec, 1);
1316
1317 /* fe needs a new last extent block pointer, as does the
1318 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001319 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001320
Mark Fasheh328d5752007-06-18 10:48:04 -07001321 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001322 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1323
Mark Fasheh328d5752007-06-18 10:48:04 -07001324 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001325 if (status < 0)
1326 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001327 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001328 if (status < 0)
1329 mlog_errno(status);
1330 if (eb_bh) {
1331 status = ocfs2_journal_dirty(handle, eb_bh);
1332 if (status < 0)
1333 mlog_errno(status);
1334 }
1335
Mark Fasheh328d5752007-06-18 10:48:04 -07001336 /*
1337 * Some callers want to track the rightmost leaf so pass it
1338 * back here.
1339 */
1340 brelse(*last_eb_bh);
1341 get_bh(new_eb_bhs[0]);
1342 *last_eb_bh = new_eb_bhs[0];
1343
Mark Fashehccd979b2005-12-15 14:31:24 -08001344 status = 0;
1345bail:
1346 if (new_eb_bhs) {
1347 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001348 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001349 kfree(new_eb_bhs);
1350 }
1351
1352 mlog_exit(status);
1353 return status;
1354}
1355
1356/*
1357 * adds another level to the allocation tree.
1358 * returns back the new extent block so you can add a branch to it
1359 * after this call.
1360 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001361static int ocfs2_shift_tree_depth(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001362 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001363 struct ocfs2_alloc_context *meta_ac,
1364 struct buffer_head **ret_new_eb_bh)
1365{
1366 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001367 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001368 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001369 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001370 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001371 struct ocfs2_extent_list *eb_el;
1372
1373 mlog_entry_void();
1374
Joel Becker42a5a7a2009-02-12 18:49:19 -08001375 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
Mark Fashehccd979b2005-12-15 14:31:24 -08001376 &new_eb_bh);
1377 if (status < 0) {
1378 mlog_errno(status);
1379 goto bail;
1380 }
1381
1382 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001383 /* ocfs2_create_new_meta_bhs() should create it right! */
1384 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001385
1386 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001387 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001388
Joel Beckerd401dc12009-02-13 02:24:10 -08001389 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001390 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001391 if (status < 0) {
1392 mlog_errno(status);
1393 goto bail;
1394 }
1395
Tao Mae7d4cb62008-08-18 17:38:44 +08001396 /* copy the root extent list data into the new extent block */
1397 eb_el->l_tree_depth = root_el->l_tree_depth;
1398 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1399 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1400 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001401
1402 status = ocfs2_journal_dirty(handle, new_eb_bh);
1403 if (status < 0) {
1404 mlog_errno(status);
1405 goto bail;
1406 }
1407
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001408 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001409 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001410 if (status < 0) {
1411 mlog_errno(status);
1412 goto bail;
1413 }
1414
Mark Fashehdcd05382007-01-16 11:32:23 -08001415 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1416
Tao Mae7d4cb62008-08-18 17:38:44 +08001417 /* update root_bh now */
1418 le16_add_cpu(&root_el->l_tree_depth, 1);
1419 root_el->l_recs[0].e_cpos = 0;
1420 root_el->l_recs[0].e_blkno = eb->h_blkno;
1421 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1422 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1423 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1424 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001425
1426 /* If this is our 1st tree depth shift, then last_eb_blk
1427 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001428 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001429 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001430
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001431 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001432 if (status < 0) {
1433 mlog_errno(status);
1434 goto bail;
1435 }
1436
1437 *ret_new_eb_bh = new_eb_bh;
1438 new_eb_bh = NULL;
1439 status = 0;
1440bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001441 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001442
1443 mlog_exit(status);
1444 return status;
1445}
1446
1447/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001448 * Should only be called when there is no space left in any of the
1449 * leaf nodes. What we want to do is find the lowest tree depth
1450 * non-leaf extent block with room for new records. There are three
1451 * valid results of this search:
1452 *
1453 * 1) a lowest extent block is found, then we pass it back in
1454 * *lowest_eb_bh and return '0'
1455 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001456 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001457 * pass NULL back in *lowest_eb_bh, but still return '0'
1458 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001459 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001460 * which case we return > 0
1461 *
1462 * return status < 0 indicates an error.
1463 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001464static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001465 struct buffer_head **target_bh)
1466{
1467 int status = 0, i;
1468 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001469 struct ocfs2_extent_block *eb;
1470 struct ocfs2_extent_list *el;
1471 struct buffer_head *bh = NULL;
1472 struct buffer_head *lowest_bh = NULL;
1473
1474 mlog_entry_void();
1475
1476 *target_bh = NULL;
1477
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001478 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001479
1480 while(le16_to_cpu(el->l_tree_depth) > 1) {
1481 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08001482 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1483 "Owner %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001484 "extent list (next_free_rec == 0)",
Joel Becker3d03a302009-02-12 17:49:26 -08001485 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001486 status = -EIO;
1487 goto bail;
1488 }
1489 i = le16_to_cpu(el->l_next_free_rec) - 1;
1490 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1491 if (!blkno) {
Joel Becker3d03a302009-02-12 17:49:26 -08001492 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1493 "Owner %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001494 "list where extent # %d has no physical "
1495 "block start",
Joel Becker3d03a302009-02-12 17:49:26 -08001496 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001497 status = -EIO;
1498 goto bail;
1499 }
1500
Mark Fasheha81cb882008-10-07 14:25:16 -07001501 brelse(bh);
1502 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001503
Joel Becker3d03a302009-02-12 17:49:26 -08001504 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001505 if (status < 0) {
1506 mlog_errno(status);
1507 goto bail;
1508 }
1509
1510 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001511 el = &eb->h_list;
1512
1513 if (le16_to_cpu(el->l_next_free_rec) <
1514 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001515 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001516 lowest_bh = bh;
1517 get_bh(lowest_bh);
1518 }
1519 }
1520
1521 /* If we didn't find one and the fe doesn't have any room,
1522 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001523 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001524 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001525 status = 1;
1526
1527 *target_bh = lowest_bh;
1528bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001529 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001530
1531 mlog_exit(status);
1532 return status;
1533}
1534
Mark Fashehe48edee2007-03-07 16:46:57 -08001535/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001536 * Grow a b-tree so that it has more records.
1537 *
1538 * We might shift the tree depth in which case existing paths should
1539 * be considered invalid.
1540 *
1541 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001542 *
1543 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001544 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001545static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1546 int *final_depth, struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001547 struct ocfs2_alloc_context *meta_ac)
1548{
1549 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001550 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001551 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001552 struct buffer_head *bh = NULL;
1553
1554 BUG_ON(meta_ac == NULL);
1555
Joel Beckerd401dc12009-02-13 02:24:10 -08001556 shift = ocfs2_find_branch_target(et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001557 if (shift < 0) {
1558 ret = shift;
1559 mlog_errno(ret);
1560 goto out;
1561 }
1562
1563 /* We traveled all the way to the bottom of the allocation tree
1564 * and didn't find room for any more extents - we need to add
1565 * another tree level */
1566 if (shift) {
1567 BUG_ON(bh);
1568 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1569
1570 /* ocfs2_shift_tree_depth will return us a buffer with
1571 * the new extent block (so we can pass that to
1572 * ocfs2_add_branch). */
Joel Beckerd401dc12009-02-13 02:24:10 -08001573 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001574 if (ret < 0) {
1575 mlog_errno(ret);
1576 goto out;
1577 }
1578 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001579 if (depth == 1) {
1580 /*
1581 * Special case: we have room now if we shifted from
1582 * tree_depth 0, so no more work needs to be done.
1583 *
1584 * We won't be calling add_branch, so pass
1585 * back *last_eb_bh as the new leaf. At depth
1586 * zero, it should always be null so there's
1587 * no reason to brelse.
1588 */
1589 BUG_ON(*last_eb_bh);
1590 get_bh(bh);
1591 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001592 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001593 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001594 }
1595
1596 /* call ocfs2_add_branch to add the final part of the tree with
1597 * the new data. */
1598 mlog(0, "add branch. bh = %p\n", bh);
Joel Beckerd401dc12009-02-13 02:24:10 -08001599 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001600 meta_ac);
1601 if (ret < 0) {
1602 mlog_errno(ret);
1603 goto out;
1604 }
1605
1606out:
1607 if (final_depth)
1608 *final_depth = depth;
1609 brelse(bh);
1610 return ret;
1611}
1612
1613/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001614 * This function will discard the rightmost extent record.
1615 */
1616static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1617{
1618 int next_free = le16_to_cpu(el->l_next_free_rec);
1619 int count = le16_to_cpu(el->l_count);
1620 unsigned int num_bytes;
1621
1622 BUG_ON(!next_free);
1623 /* This will cause us to go off the end of our extent list. */
1624 BUG_ON(next_free >= count);
1625
1626 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1627
1628 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1629}
1630
1631static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1632 struct ocfs2_extent_rec *insert_rec)
1633{
1634 int i, insert_index, next_free, has_empty, num_bytes;
1635 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1636 struct ocfs2_extent_rec *rec;
1637
1638 next_free = le16_to_cpu(el->l_next_free_rec);
1639 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1640
1641 BUG_ON(!next_free);
1642
1643 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001644 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001645
1646 /*
1647 * The easiest way to approach this is to just remove the
1648 * empty extent and temporarily decrement next_free.
1649 */
1650 if (has_empty) {
1651 /*
1652 * If next_free was 1 (only an empty extent), this
1653 * loop won't execute, which is fine. We still want
1654 * the decrement above to happen.
1655 */
1656 for(i = 0; i < (next_free - 1); i++)
1657 el->l_recs[i] = el->l_recs[i+1];
1658
1659 next_free--;
1660 }
1661
1662 /*
1663 * Figure out what the new record index should be.
1664 */
1665 for(i = 0; i < next_free; i++) {
1666 rec = &el->l_recs[i];
1667
1668 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1669 break;
1670 }
1671 insert_index = i;
1672
1673 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1674 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1675
1676 BUG_ON(insert_index < 0);
1677 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1678 BUG_ON(insert_index > next_free);
1679
1680 /*
1681 * No need to memmove if we're just adding to the tail.
1682 */
1683 if (insert_index != next_free) {
1684 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1685
1686 num_bytes = next_free - insert_index;
1687 num_bytes *= sizeof(struct ocfs2_extent_rec);
1688 memmove(&el->l_recs[insert_index + 1],
1689 &el->l_recs[insert_index],
1690 num_bytes);
1691 }
1692
1693 /*
1694 * Either we had an empty extent, and need to re-increment or
1695 * there was no empty extent on a non full rightmost leaf node,
1696 * in which case we still need to increment.
1697 */
1698 next_free++;
1699 el->l_next_free_rec = cpu_to_le16(next_free);
1700 /*
1701 * Make sure none of the math above just messed up our tree.
1702 */
1703 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1704
1705 el->l_recs[insert_index] = *insert_rec;
1706
1707}
1708
Mark Fasheh328d5752007-06-18 10:48:04 -07001709static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1710{
1711 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1712
1713 BUG_ON(num_recs == 0);
1714
1715 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1716 num_recs--;
1717 size = num_recs * sizeof(struct ocfs2_extent_rec);
1718 memmove(&el->l_recs[0], &el->l_recs[1], size);
1719 memset(&el->l_recs[num_recs], 0,
1720 sizeof(struct ocfs2_extent_rec));
1721 el->l_next_free_rec = cpu_to_le16(num_recs);
1722 }
1723}
1724
Mark Fashehdcd05382007-01-16 11:32:23 -08001725/*
1726 * Create an empty extent record .
1727 *
1728 * l_next_free_rec may be updated.
1729 *
1730 * If an empty extent already exists do nothing.
1731 */
1732static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1733{
1734 int next_free = le16_to_cpu(el->l_next_free_rec);
1735
Mark Fashehe48edee2007-03-07 16:46:57 -08001736 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1737
Mark Fashehdcd05382007-01-16 11:32:23 -08001738 if (next_free == 0)
1739 goto set_and_inc;
1740
1741 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1742 return;
1743
1744 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1745 "Asked to create an empty extent in a full list:\n"
1746 "count = %u, tree depth = %u",
1747 le16_to_cpu(el->l_count),
1748 le16_to_cpu(el->l_tree_depth));
1749
1750 ocfs2_shift_records_right(el);
1751
1752set_and_inc:
1753 le16_add_cpu(&el->l_next_free_rec, 1);
1754 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1755}
1756
1757/*
1758 * For a rotation which involves two leaf nodes, the "root node" is
1759 * the lowest level tree node which contains a path to both leafs. This
1760 * resulting set of information can be used to form a complete "subtree"
1761 *
1762 * This function is passed two full paths from the dinode down to a
1763 * pair of adjacent leaves. It's task is to figure out which path
1764 * index contains the subtree root - this can be the root index itself
1765 * in a worst-case rotation.
1766 *
1767 * The array index of the subtree root is passed back.
1768 */
Tao Ma38a04e42009-11-30 14:32:19 +08001769int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1770 struct ocfs2_path *left,
1771 struct ocfs2_path *right)
Mark Fashehdcd05382007-01-16 11:32:23 -08001772{
1773 int i = 0;
1774
1775 /*
1776 * Check that the caller passed in two paths from the same tree.
1777 */
1778 BUG_ON(path_root_bh(left) != path_root_bh(right));
1779
1780 do {
1781 i++;
1782
1783 /*
1784 * The caller didn't pass two adjacent paths.
1785 */
1786 mlog_bug_on_msg(i > left->p_tree_depth,
Joel Becker7dc02802009-02-12 19:20:13 -08001787 "Owner %llu, left depth %u, right depth %u\n"
Mark Fashehdcd05382007-01-16 11:32:23 -08001788 "left leaf blk %llu, right leaf blk %llu\n",
Joel Becker7dc02802009-02-12 19:20:13 -08001789 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1790 left->p_tree_depth, right->p_tree_depth,
Mark Fashehdcd05382007-01-16 11:32:23 -08001791 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1792 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1793 } while (left->p_node[i].bh->b_blocknr ==
1794 right->p_node[i].bh->b_blocknr);
1795
1796 return i - 1;
1797}
1798
1799typedef void (path_insert_t)(void *, struct buffer_head *);
1800
1801/*
1802 * Traverse a btree path in search of cpos, starting at root_el.
1803 *
1804 * This code can be called with a cpos larger than the tree, in which
1805 * case it will return the rightmost path.
1806 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001807static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
Mark Fashehdcd05382007-01-16 11:32:23 -08001808 struct ocfs2_extent_list *root_el, u32 cpos,
1809 path_insert_t *func, void *data)
1810{
1811 int i, ret = 0;
1812 u32 range;
1813 u64 blkno;
1814 struct buffer_head *bh = NULL;
1815 struct ocfs2_extent_block *eb;
1816 struct ocfs2_extent_list *el;
1817 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08001818
1819 el = root_el;
1820 while (el->l_tree_depth) {
1821 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001822 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1823 "Owner %llu has empty extent list at "
Mark Fashehdcd05382007-01-16 11:32:23 -08001824 "depth %u\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001825 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001826 le16_to_cpu(el->l_tree_depth));
1827 ret = -EROFS;
1828 goto out;
1829
1830 }
1831
1832 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1833 rec = &el->l_recs[i];
1834
1835 /*
1836 * In the case that cpos is off the allocation
1837 * tree, this should just wind up returning the
1838 * rightmost record.
1839 */
1840 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001841 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001842 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1843 break;
1844 }
1845
1846 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1847 if (blkno == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001848 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1849 "Owner %llu has bad blkno in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001850 "at depth %u (index %d)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001851 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001852 le16_to_cpu(el->l_tree_depth), i);
1853 ret = -EROFS;
1854 goto out;
1855 }
1856
1857 brelse(bh);
1858 bh = NULL;
Joel Beckerfacdb772009-02-12 18:08:48 -08001859 ret = ocfs2_read_extent_block(ci, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001860 if (ret) {
1861 mlog_errno(ret);
1862 goto out;
1863 }
1864
1865 eb = (struct ocfs2_extent_block *) bh->b_data;
1866 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001867
1868 if (le16_to_cpu(el->l_next_free_rec) >
1869 le16_to_cpu(el->l_count)) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001870 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1871 "Owner %llu has bad count in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001872 "at block %llu (next free=%u, count=%u)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001873 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001874 (unsigned long long)bh->b_blocknr,
1875 le16_to_cpu(el->l_next_free_rec),
1876 le16_to_cpu(el->l_count));
1877 ret = -EROFS;
1878 goto out;
1879 }
1880
1881 if (func)
1882 func(data, bh);
1883 }
1884
1885out:
1886 /*
1887 * Catch any trailing bh that the loop didn't handle.
1888 */
1889 brelse(bh);
1890
1891 return ret;
1892}
1893
1894/*
1895 * Given an initialized path (that is, it has a valid root extent
1896 * list), this function will traverse the btree in search of the path
1897 * which would contain cpos.
1898 *
1899 * The path traveled is recorded in the path structure.
1900 *
1901 * Note that this will not do any comparisons on leaf node extent
1902 * records, so it will work fine in the case that we just added a tree
1903 * branch.
1904 */
1905struct find_path_data {
1906 int index;
1907 struct ocfs2_path *path;
1908};
1909static void find_path_ins(void *data, struct buffer_head *bh)
1910{
1911 struct find_path_data *fp = data;
1912
1913 get_bh(bh);
1914 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1915 fp->index++;
1916}
Tao Mae2e9f602009-08-18 11:22:34 +08001917int ocfs2_find_path(struct ocfs2_caching_info *ci,
1918 struct ocfs2_path *path, u32 cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08001919{
1920 struct find_path_data data;
1921
1922 data.index = 1;
1923 data.path = path;
Joel Beckerfacdb772009-02-12 18:08:48 -08001924 return __ocfs2_find_path(ci, path_root_el(path), cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08001925 find_path_ins, &data);
1926}
1927
1928static void find_leaf_ins(void *data, struct buffer_head *bh)
1929{
1930 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1931 struct ocfs2_extent_list *el = &eb->h_list;
1932 struct buffer_head **ret = data;
1933
1934 /* We want to retain only the leaf block. */
1935 if (le16_to_cpu(el->l_tree_depth) == 0) {
1936 get_bh(bh);
1937 *ret = bh;
1938 }
1939}
1940/*
1941 * Find the leaf block in the tree which would contain cpos. No
1942 * checking of the actual leaf is done.
1943 *
1944 * Some paths want to call this instead of allocating a path structure
1945 * and calling ocfs2_find_path().
1946 *
1947 * This function doesn't handle non btree extent lists.
1948 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001949int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1950 struct ocfs2_extent_list *root_el, u32 cpos,
1951 struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001952{
1953 int ret;
1954 struct buffer_head *bh = NULL;
1955
Joel Beckerfacdb772009-02-12 18:08:48 -08001956 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001957 if (ret) {
1958 mlog_errno(ret);
1959 goto out;
1960 }
1961
1962 *leaf_bh = bh;
1963out:
1964 return ret;
1965}
1966
1967/*
1968 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1969 *
1970 * Basically, we've moved stuff around at the bottom of the tree and
1971 * we need to fix up the extent records above the changes to reflect
1972 * the new changes.
1973 *
1974 * left_rec: the record on the left.
1975 * left_child_el: is the child list pointed to by left_rec
1976 * right_rec: the record to the right of left_rec
1977 * right_child_el: is the child list pointed to by right_rec
1978 *
1979 * By definition, this only works on interior nodes.
1980 */
1981static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1982 struct ocfs2_extent_list *left_child_el,
1983 struct ocfs2_extent_rec *right_rec,
1984 struct ocfs2_extent_list *right_child_el)
1985{
1986 u32 left_clusters, right_end;
1987
1988 /*
1989 * Interior nodes never have holes. Their cpos is the cpos of
1990 * the leftmost record in their child list. Their cluster
1991 * count covers the full theoretical range of their child list
1992 * - the range between their cpos and the cpos of the record
1993 * immediately to their right.
1994 */
1995 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08001996 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1997 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07001998 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1999 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
2000 }
Mark Fashehdcd05382007-01-16 11:32:23 -08002001 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002002 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08002003
2004 /*
2005 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08002006 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08002007 * updating e_cpos to keep the same highest cluster count.
2008 */
2009 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002010 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08002011
2012 right_rec->e_cpos = left_rec->e_cpos;
2013 le32_add_cpu(&right_rec->e_cpos, left_clusters);
2014
2015 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002016 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08002017}
2018
2019/*
2020 * Adjust the adjacent root node records involved in a
2021 * rotation. left_el_blkno is passed in as a key so that we can easily
2022 * find it's index in the root list.
2023 */
2024static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
2025 struct ocfs2_extent_list *left_el,
2026 struct ocfs2_extent_list *right_el,
2027 u64 left_el_blkno)
2028{
2029 int i;
2030
2031 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
2032 le16_to_cpu(left_el->l_tree_depth));
2033
2034 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
2035 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
2036 break;
2037 }
2038
2039 /*
2040 * The path walking code should have never returned a root and
2041 * two paths which are not adjacent.
2042 */
2043 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2044
2045 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
2046 &root_el->l_recs[i + 1], right_el);
2047}
2048
2049/*
2050 * We've changed a leaf block (in right_path) and need to reflect that
2051 * change back up the subtree.
2052 *
2053 * This happens in multiple places:
2054 * - When we've moved an extent record from the left path leaf to the right
2055 * path leaf to make room for an empty extent in the left path leaf.
2056 * - When our insert into the right path leaf is at the leftmost edge
2057 * and requires an update of the path immediately to it's left. This
2058 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08002059 * - When we've adjusted the last extent record in the left path leaf and the
2060 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08002061 */
Joel Becker4619c732009-02-12 19:02:36 -08002062static void ocfs2_complete_edge_insert(handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08002063 struct ocfs2_path *left_path,
2064 struct ocfs2_path *right_path,
2065 int subtree_index)
2066{
2067 int ret, i, idx;
2068 struct ocfs2_extent_list *el, *left_el, *right_el;
2069 struct ocfs2_extent_rec *left_rec, *right_rec;
2070 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2071
2072 /*
2073 * Update the counts and position values within all the
2074 * interior nodes to reflect the leaf rotation we just did.
2075 *
2076 * The root node is handled below the loop.
2077 *
2078 * We begin the loop with right_el and left_el pointing to the
2079 * leaf lists and work our way up.
2080 *
2081 * NOTE: within this loop, left_el and right_el always refer
2082 * to the *child* lists.
2083 */
2084 left_el = path_leaf_el(left_path);
2085 right_el = path_leaf_el(right_path);
2086 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2087 mlog(0, "Adjust records at index %u\n", i);
2088
2089 /*
2090 * One nice property of knowing that all of these
2091 * nodes are below the root is that we only deal with
2092 * the leftmost right node record and the rightmost
2093 * left node record.
2094 */
2095 el = left_path->p_node[i].el;
2096 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2097 left_rec = &el->l_recs[idx];
2098
2099 el = right_path->p_node[i].el;
2100 right_rec = &el->l_recs[0];
2101
2102 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2103 right_el);
2104
2105 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2106 if (ret)
2107 mlog_errno(ret);
2108
2109 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2110 if (ret)
2111 mlog_errno(ret);
2112
2113 /*
2114 * Setup our list pointers now so that the current
2115 * parents become children in the next iteration.
2116 */
2117 left_el = left_path->p_node[i].el;
2118 right_el = right_path->p_node[i].el;
2119 }
2120
2121 /*
2122 * At the root node, adjust the two adjacent records which
2123 * begin our path to the leaves.
2124 */
2125
2126 el = left_path->p_node[subtree_index].el;
2127 left_el = left_path->p_node[subtree_index + 1].el;
2128 right_el = right_path->p_node[subtree_index + 1].el;
2129
2130 ocfs2_adjust_root_records(el, left_el, right_el,
2131 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2132
2133 root_bh = left_path->p_node[subtree_index].bh;
2134
2135 ret = ocfs2_journal_dirty(handle, root_bh);
2136 if (ret)
2137 mlog_errno(ret);
2138}
2139
Joel Becker5c601ab2009-02-12 19:10:13 -08002140static int ocfs2_rotate_subtree_right(handle_t *handle,
2141 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08002142 struct ocfs2_path *left_path,
2143 struct ocfs2_path *right_path,
2144 int subtree_index)
2145{
2146 int ret, i;
2147 struct buffer_head *right_leaf_bh;
2148 struct buffer_head *left_leaf_bh = NULL;
2149 struct buffer_head *root_bh;
2150 struct ocfs2_extent_list *right_el, *left_el;
2151 struct ocfs2_extent_rec move_rec;
2152
2153 left_leaf_bh = path_leaf_bh(left_path);
2154 left_el = path_leaf_el(left_path);
2155
2156 if (left_el->l_next_free_rec != left_el->l_count) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002157 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002158 "Inode %llu has non-full interior leaf node %llu"
2159 "(next free = %u)",
Joel Becker5c601ab2009-02-12 19:10:13 -08002160 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002161 (unsigned long long)left_leaf_bh->b_blocknr,
2162 le16_to_cpu(left_el->l_next_free_rec));
2163 return -EROFS;
2164 }
2165
2166 /*
2167 * This extent block may already have an empty record, so we
2168 * return early if so.
2169 */
2170 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2171 return 0;
2172
2173 root_bh = left_path->p_node[subtree_index].bh;
2174 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2175
Joel Becker5c601ab2009-02-12 19:10:13 -08002176 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002177 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002178 if (ret) {
2179 mlog_errno(ret);
2180 goto out;
2181 }
2182
2183 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002184 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002185 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002186 if (ret) {
2187 mlog_errno(ret);
2188 goto out;
2189 }
2190
Joel Becker5c601ab2009-02-12 19:10:13 -08002191 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002192 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002193 if (ret) {
2194 mlog_errno(ret);
2195 goto out;
2196 }
2197 }
2198
2199 right_leaf_bh = path_leaf_bh(right_path);
2200 right_el = path_leaf_el(right_path);
2201
2202 /* This is a code error, not a disk corruption. */
2203 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2204 "because rightmost leaf block %llu is empty\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002205 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002206 (unsigned long long)right_leaf_bh->b_blocknr);
2207
2208 ocfs2_create_empty_extent(right_el);
2209
2210 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2211 if (ret) {
2212 mlog_errno(ret);
2213 goto out;
2214 }
2215
2216 /* Do the copy now. */
2217 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2218 move_rec = left_el->l_recs[i];
2219 right_el->l_recs[0] = move_rec;
2220
2221 /*
2222 * Clear out the record we just copied and shift everything
2223 * over, leaving an empty extent in the left leaf.
2224 *
2225 * We temporarily subtract from next_free_rec so that the
2226 * shift will lose the tail record (which is now defunct).
2227 */
2228 le16_add_cpu(&left_el->l_next_free_rec, -1);
2229 ocfs2_shift_records_right(left_el);
2230 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2231 le16_add_cpu(&left_el->l_next_free_rec, 1);
2232
2233 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2234 if (ret) {
2235 mlog_errno(ret);
2236 goto out;
2237 }
2238
Joel Becker4619c732009-02-12 19:02:36 -08002239 ocfs2_complete_edge_insert(handle, left_path, right_path,
2240 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002241
2242out:
2243 return ret;
2244}
2245
2246/*
2247 * Given a full path, determine what cpos value would return us a path
2248 * containing the leaf immediately to the left of the current one.
2249 *
2250 * Will return zero if the path passed in is already the leftmost path.
2251 */
2252static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2253 struct ocfs2_path *path, u32 *cpos)
2254{
2255 int i, j, ret = 0;
2256 u64 blkno;
2257 struct ocfs2_extent_list *el;
2258
Mark Fashehe48edee2007-03-07 16:46:57 -08002259 BUG_ON(path->p_tree_depth == 0);
2260
Mark Fashehdcd05382007-01-16 11:32:23 -08002261 *cpos = 0;
2262
2263 blkno = path_leaf_bh(path)->b_blocknr;
2264
2265 /* Start at the tree node just above the leaf and work our way up. */
2266 i = path->p_tree_depth - 1;
2267 while (i >= 0) {
2268 el = path->p_node[i].el;
2269
2270 /*
2271 * Find the extent record just before the one in our
2272 * path.
2273 */
2274 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2275 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2276 if (j == 0) {
2277 if (i == 0) {
2278 /*
2279 * We've determined that the
2280 * path specified is already
2281 * the leftmost one - return a
2282 * cpos of zero.
2283 */
2284 goto out;
2285 }
2286 /*
2287 * The leftmost record points to our
2288 * leaf - we need to travel up the
2289 * tree one level.
2290 */
2291 goto next_node;
2292 }
2293
2294 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002295 *cpos = *cpos + ocfs2_rec_clusters(el,
2296 &el->l_recs[j - 1]);
2297 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002298 goto out;
2299 }
2300 }
2301
2302 /*
2303 * If we got here, we never found a valid node where
2304 * the tree indicated one should be.
2305 */
2306 ocfs2_error(sb,
2307 "Invalid extent tree at extent block %llu\n",
2308 (unsigned long long)blkno);
2309 ret = -EROFS;
2310 goto out;
2311
2312next_node:
2313 blkno = path->p_node[i].bh->b_blocknr;
2314 i--;
2315 }
2316
2317out:
2318 return ret;
2319}
2320
Mark Fasheh328d5752007-06-18 10:48:04 -07002321/*
2322 * Extend the transaction by enough credits to complete the rotation,
2323 * and still leave at least the original number of credits allocated
2324 * to this transaction.
2325 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002326static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002327 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002328 struct ocfs2_path *path)
2329{
Tao Mac18b8122009-08-18 11:44:07 +08002330 int ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002331 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002332
Tao Mac18b8122009-08-18 11:44:07 +08002333 if (handle->h_buffer_credits < credits) {
2334 ret = ocfs2_extend_trans(handle,
2335 credits - handle->h_buffer_credits);
2336 if (ret)
2337 return ret;
2338
2339 if (unlikely(handle->h_buffer_credits < credits))
2340 return ocfs2_extend_trans(handle, credits);
2341 }
Mark Fashehdcd05382007-01-16 11:32:23 -08002342
2343 return 0;
2344}
2345
2346/*
2347 * Trap the case where we're inserting into the theoretical range past
2348 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2349 * whose cpos is less than ours into the right leaf.
2350 *
2351 * It's only necessary to look at the rightmost record of the left
2352 * leaf because the logic that calls us should ensure that the
2353 * theoretical ranges in the path components above the leaves are
2354 * correct.
2355 */
2356static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2357 u32 insert_cpos)
2358{
2359 struct ocfs2_extent_list *left_el;
2360 struct ocfs2_extent_rec *rec;
2361 int next_free;
2362
2363 left_el = path_leaf_el(left_path);
2364 next_free = le16_to_cpu(left_el->l_next_free_rec);
2365 rec = &left_el->l_recs[next_free - 1];
2366
2367 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2368 return 1;
2369 return 0;
2370}
2371
Mark Fasheh328d5752007-06-18 10:48:04 -07002372static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2373{
2374 int next_free = le16_to_cpu(el->l_next_free_rec);
2375 unsigned int range;
2376 struct ocfs2_extent_rec *rec;
2377
2378 if (next_free == 0)
2379 return 0;
2380
2381 rec = &el->l_recs[0];
2382 if (ocfs2_is_empty_extent(rec)) {
2383 /* Empty list. */
2384 if (next_free == 1)
2385 return 0;
2386 rec = &el->l_recs[1];
2387 }
2388
2389 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2390 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2391 return 1;
2392 return 0;
2393}
2394
Mark Fashehdcd05382007-01-16 11:32:23 -08002395/*
2396 * Rotate all the records in a btree right one record, starting at insert_cpos.
2397 *
2398 * The path to the rightmost leaf should be passed in.
2399 *
2400 * The array is assumed to be large enough to hold an entire path (tree depth).
2401 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002402 * Upon successful return from this function:
Mark Fashehdcd05382007-01-16 11:32:23 -08002403 *
2404 * - The 'right_path' array will contain a path to the leaf block
2405 * whose range contains e_cpos.
2406 * - That leaf block will have a single empty extent in list index 0.
2407 * - In the case that the rotation requires a post-insert update,
2408 * *ret_left_path will contain a valid path which can be passed to
2409 * ocfs2_insert_path().
2410 */
Joel Becker1bbf0b82009-02-12 19:42:08 -08002411static int ocfs2_rotate_tree_right(handle_t *handle,
Joel Becker5c601ab2009-02-12 19:10:13 -08002412 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002413 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002414 u32 insert_cpos,
2415 struct ocfs2_path *right_path,
2416 struct ocfs2_path **ret_left_path)
2417{
Mark Fasheh328d5752007-06-18 10:48:04 -07002418 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002419 u32 cpos;
2420 struct ocfs2_path *left_path = NULL;
Joel Becker5c601ab2009-02-12 19:10:13 -08002421 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehdcd05382007-01-16 11:32:23 -08002422
2423 *ret_left_path = NULL;
2424
Joel Beckerffdd7a52008-10-17 22:32:01 -07002425 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002426 if (!left_path) {
2427 ret = -ENOMEM;
2428 mlog_errno(ret);
2429 goto out;
2430 }
2431
Joel Becker5c601ab2009-02-12 19:10:13 -08002432 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002433 if (ret) {
2434 mlog_errno(ret);
2435 goto out;
2436 }
2437
2438 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2439
2440 /*
2441 * What we want to do here is:
2442 *
2443 * 1) Start with the rightmost path.
2444 *
2445 * 2) Determine a path to the leaf block directly to the left
2446 * of that leaf.
2447 *
2448 * 3) Determine the 'subtree root' - the lowest level tree node
2449 * which contains a path to both leaves.
2450 *
2451 * 4) Rotate the subtree.
2452 *
2453 * 5) Find the next subtree by considering the left path to be
2454 * the new right path.
2455 *
2456 * The check at the top of this while loop also accepts
2457 * insert_cpos == cpos because cpos is only a _theoretical_
2458 * value to get us the left path - insert_cpos might very well
2459 * be filling that hole.
2460 *
2461 * Stop at a cpos of '0' because we either started at the
2462 * leftmost branch (i.e., a tree with one branch and a
2463 * rotation inside of it), or we've gone as far as we can in
2464 * rotating subtrees.
2465 */
2466 while (cpos && insert_cpos <= cpos) {
2467 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2468 insert_cpos, cpos);
2469
Joel Becker5c601ab2009-02-12 19:10:13 -08002470 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002471 if (ret) {
2472 mlog_errno(ret);
2473 goto out;
2474 }
2475
2476 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2477 path_leaf_bh(right_path),
Joel Becker5c601ab2009-02-12 19:10:13 -08002478 "Owner %llu: error during insert of %u "
Mark Fashehdcd05382007-01-16 11:32:23 -08002479 "(left path cpos %u) results in two identical "
2480 "paths ending at %llu\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002481 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2482 insert_cpos, cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08002483 (unsigned long long)
2484 path_leaf_bh(left_path)->b_blocknr);
2485
Mark Fasheh328d5752007-06-18 10:48:04 -07002486 if (split == SPLIT_NONE &&
2487 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002488 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002489
2490 /*
2491 * We've rotated the tree as much as we
2492 * should. The rest is up to
2493 * ocfs2_insert_path() to complete, after the
2494 * record insertion. We indicate this
2495 * situation by returning the left path.
2496 *
2497 * The reason we don't adjust the records here
2498 * before the record insert is that an error
2499 * later might break the rule where a parent
2500 * record e_cpos will reflect the actual
2501 * e_cpos of the 1st nonempty record of the
2502 * child list.
2503 */
2504 *ret_left_path = left_path;
2505 goto out_ret_path;
2506 }
2507
Joel Becker7dc02802009-02-12 19:20:13 -08002508 start = ocfs2_find_subtree_root(et, left_path, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002509
2510 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2511 start,
2512 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2513 right_path->p_tree_depth);
2514
2515 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002516 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002517 if (ret) {
2518 mlog_errno(ret);
2519 goto out;
2520 }
2521
Joel Becker5c601ab2009-02-12 19:10:13 -08002522 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002523 right_path, start);
2524 if (ret) {
2525 mlog_errno(ret);
2526 goto out;
2527 }
2528
Mark Fasheh328d5752007-06-18 10:48:04 -07002529 if (split != SPLIT_NONE &&
2530 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2531 insert_cpos)) {
2532 /*
2533 * A rotate moves the rightmost left leaf
2534 * record over to the leftmost right leaf
2535 * slot. If we're doing an extent split
2536 * instead of a real insert, then we have to
2537 * check that the extent to be split wasn't
2538 * just moved over. If it was, then we can
2539 * exit here, passing left_path back -
2540 * ocfs2_split_extent() is smart enough to
2541 * search both leaves.
2542 */
2543 *ret_left_path = left_path;
2544 goto out_ret_path;
2545 }
2546
Mark Fashehdcd05382007-01-16 11:32:23 -08002547 /*
2548 * There is no need to re-read the next right path
2549 * as we know that it'll be our current left
2550 * path. Optimize by copying values instead.
2551 */
2552 ocfs2_mv_path(right_path, left_path);
2553
Joel Becker5c601ab2009-02-12 19:10:13 -08002554 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002555 if (ret) {
2556 mlog_errno(ret);
2557 goto out;
2558 }
2559 }
2560
2561out:
2562 ocfs2_free_path(left_path);
2563
2564out_ret_path:
2565 return ret;
2566}
2567
Joel Becker09106ba2009-02-12 19:43:57 -08002568static int ocfs2_update_edge_lengths(handle_t *handle,
2569 struct ocfs2_extent_tree *et,
Tao Ma3c5e1062009-07-21 15:42:05 +08002570 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002571{
Tao Ma3c5e1062009-07-21 15:42:05 +08002572 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002573 struct ocfs2_extent_rec *rec;
2574 struct ocfs2_extent_list *el;
2575 struct ocfs2_extent_block *eb;
2576 u32 range;
2577
Tao Ma3c5e1062009-07-21 15:42:05 +08002578 /*
2579 * In normal tree rotation process, we will never touch the
2580 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2581 * doesn't reserve the credits for them either.
2582 *
2583 * But we do have a special case here which will update the rightmost
2584 * records for all the bh in the path.
2585 * So we have to allocate extra credits and access them.
2586 */
2587 ret = ocfs2_extend_trans(handle,
2588 handle->h_buffer_credits + subtree_index);
2589 if (ret) {
2590 mlog_errno(ret);
2591 goto out;
2592 }
2593
Joel Becker09106ba2009-02-12 19:43:57 -08002594 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002595 if (ret) {
2596 mlog_errno(ret);
2597 goto out;
2598 }
2599
Mark Fasheh328d5752007-06-18 10:48:04 -07002600 /* Path should always be rightmost. */
2601 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2602 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2603
2604 el = &eb->h_list;
2605 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2606 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2607 rec = &el->l_recs[idx];
2608 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2609
2610 for (i = 0; i < path->p_tree_depth; i++) {
2611 el = path->p_node[i].el;
2612 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2613 rec = &el->l_recs[idx];
2614
2615 rec->e_int_clusters = cpu_to_le32(range);
2616 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2617
2618 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2619 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002620out:
2621 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002622}
2623
Joel Becker6641b0c2009-02-12 18:57:52 -08002624static void ocfs2_unlink_path(handle_t *handle,
2625 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002626 struct ocfs2_cached_dealloc_ctxt *dealloc,
2627 struct ocfs2_path *path, int unlink_start)
2628{
2629 int ret, i;
2630 struct ocfs2_extent_block *eb;
2631 struct ocfs2_extent_list *el;
2632 struct buffer_head *bh;
2633
2634 for(i = unlink_start; i < path_num_items(path); i++) {
2635 bh = path->p_node[i].bh;
2636
2637 eb = (struct ocfs2_extent_block *)bh->b_data;
2638 /*
2639 * Not all nodes might have had their final count
2640 * decremented by the caller - handle this here.
2641 */
2642 el = &eb->h_list;
2643 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2644 mlog(ML_ERROR,
2645 "Inode %llu, attempted to remove extent block "
2646 "%llu with %u records\n",
Joel Becker6641b0c2009-02-12 18:57:52 -08002647 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07002648 (unsigned long long)le64_to_cpu(eb->h_blkno),
2649 le16_to_cpu(el->l_next_free_rec));
2650
2651 ocfs2_journal_dirty(handle, bh);
Joel Becker6641b0c2009-02-12 18:57:52 -08002652 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002653 continue;
2654 }
2655
2656 el->l_next_free_rec = 0;
2657 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2658
2659 ocfs2_journal_dirty(handle, bh);
2660
2661 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2662 if (ret)
2663 mlog_errno(ret);
2664
Joel Becker6641b0c2009-02-12 18:57:52 -08002665 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002666 }
2667}
2668
Joel Becker6641b0c2009-02-12 18:57:52 -08002669static void ocfs2_unlink_subtree(handle_t *handle,
2670 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002671 struct ocfs2_path *left_path,
2672 struct ocfs2_path *right_path,
2673 int subtree_index,
2674 struct ocfs2_cached_dealloc_ctxt *dealloc)
2675{
2676 int i;
2677 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2678 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2679 struct ocfs2_extent_list *el;
2680 struct ocfs2_extent_block *eb;
2681
2682 el = path_leaf_el(left_path);
2683
2684 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2685
2686 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2687 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2688 break;
2689
2690 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2691
2692 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2693 le16_add_cpu(&root_el->l_next_free_rec, -1);
2694
2695 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2696 eb->h_next_leaf_blk = 0;
2697
2698 ocfs2_journal_dirty(handle, root_bh);
2699 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2700
Joel Becker6641b0c2009-02-12 18:57:52 -08002701 ocfs2_unlink_path(handle, et, dealloc, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002702 subtree_index + 1);
2703}
2704
Joel Becker1e2dd632009-02-12 19:45:28 -08002705static int ocfs2_rotate_subtree_left(handle_t *handle,
2706 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002707 struct ocfs2_path *left_path,
2708 struct ocfs2_path *right_path,
2709 int subtree_index,
2710 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker1e2dd632009-02-12 19:45:28 -08002711 int *deleted)
Mark Fasheh328d5752007-06-18 10:48:04 -07002712{
2713 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002714 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002715 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2716 struct ocfs2_extent_block *eb;
2717
2718 *deleted = 0;
2719
2720 right_leaf_el = path_leaf_el(right_path);
2721 left_leaf_el = path_leaf_el(left_path);
2722 root_bh = left_path->p_node[subtree_index].bh;
2723 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2724
2725 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2726 return 0;
2727
2728 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2729 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2730 /*
2731 * It's legal for us to proceed if the right leaf is
2732 * the rightmost one and it has an empty extent. There
2733 * are two cases to handle - whether the leaf will be
2734 * empty after removal or not. If the leaf isn't empty
2735 * then just remove the empty extent up front. The
2736 * next block will handle empty leaves by flagging
2737 * them for unlink.
2738 *
2739 * Non rightmost leaves will throw -EAGAIN and the
2740 * caller can manually move the subtree and retry.
2741 */
2742
2743 if (eb->h_next_leaf_blk != 0ULL)
2744 return -EAGAIN;
2745
2746 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002747 ret = ocfs2_journal_access_eb(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002748 path_leaf_bh(right_path),
2749 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002750 if (ret) {
2751 mlog_errno(ret);
2752 goto out;
2753 }
2754
2755 ocfs2_remove_empty_extent(right_leaf_el);
2756 } else
2757 right_has_empty = 1;
2758 }
2759
2760 if (eb->h_next_leaf_blk == 0ULL &&
2761 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2762 /*
2763 * We have to update i_last_eb_blk during the meta
2764 * data delete.
2765 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08002766 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07002767 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002768 if (ret) {
2769 mlog_errno(ret);
2770 goto out;
2771 }
2772
2773 del_right_subtree = 1;
2774 }
2775
2776 /*
2777 * Getting here with an empty extent in the right path implies
2778 * that it's the rightmost path and will be deleted.
2779 */
2780 BUG_ON(right_has_empty && !del_right_subtree);
2781
Joel Becker1e2dd632009-02-12 19:45:28 -08002782 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002783 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002784 if (ret) {
2785 mlog_errno(ret);
2786 goto out;
2787 }
2788
2789 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002790 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002791 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002792 if (ret) {
2793 mlog_errno(ret);
2794 goto out;
2795 }
2796
Joel Becker1e2dd632009-02-12 19:45:28 -08002797 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002798 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002799 if (ret) {
2800 mlog_errno(ret);
2801 goto out;
2802 }
2803 }
2804
2805 if (!right_has_empty) {
2806 /*
2807 * Only do this if we're moving a real
2808 * record. Otherwise, the action is delayed until
2809 * after removal of the right path in which case we
2810 * can do a simple shift to remove the empty extent.
2811 */
2812 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2813 memset(&right_leaf_el->l_recs[0], 0,
2814 sizeof(struct ocfs2_extent_rec));
2815 }
2816 if (eb->h_next_leaf_blk == 0ULL) {
2817 /*
2818 * Move recs over to get rid of empty extent, decrease
2819 * next_free. This is allowed to remove the last
2820 * extent in our leaf (setting l_next_free_rec to
2821 * zero) - the delete code below won't care.
2822 */
2823 ocfs2_remove_empty_extent(right_leaf_el);
2824 }
2825
2826 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2827 if (ret)
2828 mlog_errno(ret);
2829 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2830 if (ret)
2831 mlog_errno(ret);
2832
2833 if (del_right_subtree) {
Joel Becker6641b0c2009-02-12 18:57:52 -08002834 ocfs2_unlink_subtree(handle, et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002835 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08002836 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08002837 left_path);
2838 if (ret) {
2839 mlog_errno(ret);
2840 goto out;
2841 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002842
2843 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002844 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002845
2846 /*
2847 * Removal of the extent in the left leaf was skipped
2848 * above so we could delete the right path
2849 * 1st.
2850 */
2851 if (right_has_empty)
2852 ocfs2_remove_empty_extent(left_leaf_el);
2853
Tao Mae7d4cb62008-08-18 17:38:44 +08002854 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002855 if (ret)
2856 mlog_errno(ret);
2857
2858 *deleted = 1;
2859 } else
Joel Becker4619c732009-02-12 19:02:36 -08002860 ocfs2_complete_edge_insert(handle, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002861 subtree_index);
2862
2863out:
2864 return ret;
2865}
2866
2867/*
2868 * Given a full path, determine what cpos value would return us a path
2869 * containing the leaf immediately to the right of the current one.
2870 *
2871 * Will return zero if the path passed in is already the rightmost path.
2872 *
2873 * This looks similar, but is subtly different to
2874 * ocfs2_find_cpos_for_left_leaf().
2875 */
Tao Ma38a04e42009-11-30 14:32:19 +08002876int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2877 struct ocfs2_path *path, u32 *cpos)
Mark Fasheh328d5752007-06-18 10:48:04 -07002878{
2879 int i, j, ret = 0;
2880 u64 blkno;
2881 struct ocfs2_extent_list *el;
2882
2883 *cpos = 0;
2884
2885 if (path->p_tree_depth == 0)
2886 return 0;
2887
2888 blkno = path_leaf_bh(path)->b_blocknr;
2889
2890 /* Start at the tree node just above the leaf and work our way up. */
2891 i = path->p_tree_depth - 1;
2892 while (i >= 0) {
2893 int next_free;
2894
2895 el = path->p_node[i].el;
2896
2897 /*
2898 * Find the extent record just after the one in our
2899 * path.
2900 */
2901 next_free = le16_to_cpu(el->l_next_free_rec);
2902 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2903 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2904 if (j == (next_free - 1)) {
2905 if (i == 0) {
2906 /*
2907 * We've determined that the
2908 * path specified is already
2909 * the rightmost one - return a
2910 * cpos of zero.
2911 */
2912 goto out;
2913 }
2914 /*
2915 * The rightmost record points to our
2916 * leaf - we need to travel up the
2917 * tree one level.
2918 */
2919 goto next_node;
2920 }
2921
2922 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2923 goto out;
2924 }
2925 }
2926
2927 /*
2928 * If we got here, we never found a valid node where
2929 * the tree indicated one should be.
2930 */
2931 ocfs2_error(sb,
2932 "Invalid extent tree at extent block %llu\n",
2933 (unsigned long long)blkno);
2934 ret = -EROFS;
2935 goto out;
2936
2937next_node:
2938 blkno = path->p_node[i].bh->b_blocknr;
2939 i--;
2940 }
2941
2942out:
2943 return ret;
2944}
2945
Joel Becker70f18c02009-02-13 02:09:31 -08002946static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2947 struct ocfs2_extent_tree *et,
Joel Becker13723d02008-10-17 19:25:01 -07002948 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002949{
2950 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002951 struct buffer_head *bh = path_leaf_bh(path);
2952 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002953
2954 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2955 return 0;
2956
Joel Becker70f18c02009-02-13 02:09:31 -08002957 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Joel Becker13723d02008-10-17 19:25:01 -07002958 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002959 if (ret) {
2960 mlog_errno(ret);
2961 goto out;
2962 }
2963
2964 ocfs2_remove_empty_extent(el);
2965
2966 ret = ocfs2_journal_dirty(handle, bh);
2967 if (ret)
2968 mlog_errno(ret);
2969
2970out:
2971 return ret;
2972}
2973
Joel Beckere46f74d2009-02-12 19:47:43 -08002974static int __ocfs2_rotate_tree_left(handle_t *handle,
2975 struct ocfs2_extent_tree *et,
2976 int orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07002977 struct ocfs2_path *path,
2978 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08002979 struct ocfs2_path **empty_extent_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002980{
2981 int ret, subtree_root, deleted;
2982 u32 right_cpos;
2983 struct ocfs2_path *left_path = NULL;
2984 struct ocfs2_path *right_path = NULL;
Joel Beckere46f74d2009-02-12 19:47:43 -08002985 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fasheh328d5752007-06-18 10:48:04 -07002986
2987 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2988
2989 *empty_extent_path = NULL;
2990
Joel Beckere46f74d2009-02-12 19:47:43 -08002991 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002992 if (ret) {
2993 mlog_errno(ret);
2994 goto out;
2995 }
2996
Joel Beckerffdd7a52008-10-17 22:32:01 -07002997 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002998 if (!left_path) {
2999 ret = -ENOMEM;
3000 mlog_errno(ret);
3001 goto out;
3002 }
3003
3004 ocfs2_cp_path(left_path, path);
3005
Joel Beckerffdd7a52008-10-17 22:32:01 -07003006 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003007 if (!right_path) {
3008 ret = -ENOMEM;
3009 mlog_errno(ret);
3010 goto out;
3011 }
3012
3013 while (right_cpos) {
Joel Beckerfacdb772009-02-12 18:08:48 -08003014 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003015 if (ret) {
3016 mlog_errno(ret);
3017 goto out;
3018 }
3019
Joel Becker7dc02802009-02-12 19:20:13 -08003020 subtree_root = ocfs2_find_subtree_root(et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003021 right_path);
3022
3023 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
3024 subtree_root,
3025 (unsigned long long)
3026 right_path->p_node[subtree_root].bh->b_blocknr,
3027 right_path->p_tree_depth);
3028
3029 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
3030 orig_credits, left_path);
3031 if (ret) {
3032 mlog_errno(ret);
3033 goto out;
3034 }
3035
Mark Fashehe8aed342007-12-03 16:43:01 -08003036 /*
3037 * Caller might still want to make changes to the
3038 * tree root, so re-add it to the journal here.
3039 */
Joel Beckere46f74d2009-02-12 19:47:43 -08003040 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003041 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08003042 if (ret) {
3043 mlog_errno(ret);
3044 goto out;
3045 }
3046
Joel Becker1e2dd632009-02-12 19:45:28 -08003047 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003048 right_path, subtree_root,
Joel Becker1e2dd632009-02-12 19:45:28 -08003049 dealloc, &deleted);
Mark Fasheh328d5752007-06-18 10:48:04 -07003050 if (ret == -EAGAIN) {
3051 /*
3052 * The rotation has to temporarily stop due to
3053 * the right subtree having an empty
3054 * extent. Pass it back to the caller for a
3055 * fixup.
3056 */
3057 *empty_extent_path = right_path;
3058 right_path = NULL;
3059 goto out;
3060 }
3061 if (ret) {
3062 mlog_errno(ret);
3063 goto out;
3064 }
3065
3066 /*
3067 * The subtree rotate might have removed records on
3068 * the rightmost edge. If so, then rotation is
3069 * complete.
3070 */
3071 if (deleted)
3072 break;
3073
3074 ocfs2_mv_path(left_path, right_path);
3075
Joel Beckere46f74d2009-02-12 19:47:43 -08003076 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003077 &right_cpos);
3078 if (ret) {
3079 mlog_errno(ret);
3080 goto out;
3081 }
3082 }
3083
3084out:
3085 ocfs2_free_path(right_path);
3086 ocfs2_free_path(left_path);
3087
3088 return ret;
3089}
3090
Joel Becker70f18c02009-02-13 02:09:31 -08003091static int ocfs2_remove_rightmost_path(handle_t *handle,
3092 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003093 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003094 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003095{
3096 int ret, subtree_index;
3097 u32 cpos;
3098 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003099 struct ocfs2_extent_block *eb;
3100 struct ocfs2_extent_list *el;
3101
Mark Fasheh328d5752007-06-18 10:48:04 -07003102
Joel Becker6136ca52009-02-12 19:32:43 -08003103 ret = ocfs2_et_sanity_check(et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003104 if (ret)
3105 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003106 /*
3107 * There's two ways we handle this depending on
3108 * whether path is the only existing one.
3109 */
3110 ret = ocfs2_extend_rotate_transaction(handle, 0,
3111 handle->h_buffer_credits,
3112 path);
3113 if (ret) {
3114 mlog_errno(ret);
3115 goto out;
3116 }
3117
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003118 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003119 if (ret) {
3120 mlog_errno(ret);
3121 goto out;
3122 }
3123
Joel Becker3d03a302009-02-12 17:49:26 -08003124 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3125 path, &cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003126 if (ret) {
3127 mlog_errno(ret);
3128 goto out;
3129 }
3130
3131 if (cpos) {
3132 /*
3133 * We have a path to the left of this one - it needs
3134 * an update too.
3135 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003136 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003137 if (!left_path) {
3138 ret = -ENOMEM;
3139 mlog_errno(ret);
3140 goto out;
3141 }
3142
Joel Beckerfacdb772009-02-12 18:08:48 -08003143 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003144 if (ret) {
3145 mlog_errno(ret);
3146 goto out;
3147 }
3148
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003149 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003150 if (ret) {
3151 mlog_errno(ret);
3152 goto out;
3153 }
3154
Joel Becker7dc02802009-02-12 19:20:13 -08003155 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003156
Joel Becker6641b0c2009-02-12 18:57:52 -08003157 ocfs2_unlink_subtree(handle, et, left_path, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003158 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08003159 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08003160 left_path);
3161 if (ret) {
3162 mlog_errno(ret);
3163 goto out;
3164 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003165
3166 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003167 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003168 } else {
3169 /*
3170 * 'path' is also the leftmost path which
3171 * means it must be the only one. This gets
3172 * handled differently because we want to
Joel Becker70f18c02009-02-13 02:09:31 -08003173 * revert the root back to having extents
Mark Fasheh328d5752007-06-18 10:48:04 -07003174 * in-line.
3175 */
Joel Becker6641b0c2009-02-12 18:57:52 -08003176 ocfs2_unlink_path(handle, et, dealloc, path, 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003177
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003178 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003179 el->l_tree_depth = 0;
3180 el->l_next_free_rec = 0;
3181 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3182
Joel Becker35dc0aa2008-08-20 16:25:06 -07003183 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003184 }
3185
3186 ocfs2_journal_dirty(handle, path_root_bh(path));
3187
3188out:
3189 ocfs2_free_path(left_path);
3190 return ret;
3191}
3192
3193/*
3194 * Left rotation of btree records.
3195 *
3196 * In many ways, this is (unsurprisingly) the opposite of right
3197 * rotation. We start at some non-rightmost path containing an empty
3198 * extent in the leaf block. The code works its way to the rightmost
3199 * path by rotating records to the left in every subtree.
3200 *
3201 * This is used by any code which reduces the number of extent records
3202 * in a leaf. After removal, an empty record should be placed in the
3203 * leftmost list position.
3204 *
3205 * This won't handle a length update of the rightmost path records if
3206 * the rightmost tree leaf record is removed so the caller is
3207 * responsible for detecting and correcting that.
3208 */
Joel Becker70f18c02009-02-13 02:09:31 -08003209static int ocfs2_rotate_tree_left(handle_t *handle,
3210 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003211 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003212 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003213{
3214 int ret, orig_credits = handle->h_buffer_credits;
3215 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3216 struct ocfs2_extent_block *eb;
3217 struct ocfs2_extent_list *el;
3218
3219 el = path_leaf_el(path);
3220 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3221 return 0;
3222
3223 if (path->p_tree_depth == 0) {
3224rightmost_no_delete:
3225 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003226 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003227 * it up front.
3228 */
Joel Becker70f18c02009-02-13 02:09:31 -08003229 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003230 if (ret)
3231 mlog_errno(ret);
3232 goto out;
3233 }
3234
3235 /*
3236 * Handle rightmost branch now. There's several cases:
3237 * 1) simple rotation leaving records in there. That's trivial.
3238 * 2) rotation requiring a branch delete - there's no more
3239 * records left. Two cases of this:
3240 * a) There are branches to the left.
3241 * b) This is also the leftmost (the only) branch.
3242 *
3243 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3244 * 2a) we need the left branch so that we can update it with the unlink
Joel Becker70f18c02009-02-13 02:09:31 -08003245 * 2b) we need to bring the root back to inline extents.
Mark Fasheh328d5752007-06-18 10:48:04 -07003246 */
3247
3248 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3249 el = &eb->h_list;
3250 if (eb->h_next_leaf_blk == 0) {
3251 /*
3252 * This gets a bit tricky if we're going to delete the
3253 * rightmost path. Get the other cases out of the way
3254 * 1st.
3255 */
3256 if (le16_to_cpu(el->l_next_free_rec) > 1)
3257 goto rightmost_no_delete;
3258
3259 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3260 ret = -EIO;
Joel Becker70f18c02009-02-13 02:09:31 -08003261 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3262 "Owner %llu has empty extent block at %llu",
3263 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07003264 (unsigned long long)le64_to_cpu(eb->h_blkno));
3265 goto out;
3266 }
3267
3268 /*
3269 * XXX: The caller can not trust "path" any more after
3270 * this as it will have been deleted. What do we do?
3271 *
3272 * In theory the rotate-for-merge code will never get
3273 * here because it'll always ask for a rotate in a
3274 * nonempty list.
3275 */
3276
Joel Becker70f18c02009-02-13 02:09:31 -08003277 ret = ocfs2_remove_rightmost_path(handle, et, path,
3278 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003279 if (ret)
3280 mlog_errno(ret);
3281 goto out;
3282 }
3283
3284 /*
3285 * Now we can loop, remembering the path we get from -EAGAIN
3286 * and restarting from there.
3287 */
3288try_rotate:
Joel Beckere46f74d2009-02-12 19:47:43 -08003289 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3290 dealloc, &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003291 if (ret && ret != -EAGAIN) {
3292 mlog_errno(ret);
3293 goto out;
3294 }
3295
3296 while (ret == -EAGAIN) {
3297 tmp_path = restart_path;
3298 restart_path = NULL;
3299
Joel Beckere46f74d2009-02-12 19:47:43 -08003300 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07003301 tmp_path, dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08003302 &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003303 if (ret && ret != -EAGAIN) {
3304 mlog_errno(ret);
3305 goto out;
3306 }
3307
3308 ocfs2_free_path(tmp_path);
3309 tmp_path = NULL;
3310
3311 if (ret == 0)
3312 goto try_rotate;
3313 }
3314
3315out:
3316 ocfs2_free_path(tmp_path);
3317 ocfs2_free_path(restart_path);
3318 return ret;
3319}
3320
3321static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3322 int index)
3323{
3324 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3325 unsigned int size;
3326
3327 if (rec->e_leaf_clusters == 0) {
3328 /*
3329 * We consumed all of the merged-from record. An empty
3330 * extent cannot exist anywhere but the 1st array
3331 * position, so move things over if the merged-from
3332 * record doesn't occupy that position.
3333 *
3334 * This creates a new empty extent so the caller
3335 * should be smart enough to have removed any existing
3336 * ones.
3337 */
3338 if (index > 0) {
3339 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3340 size = index * sizeof(struct ocfs2_extent_rec);
3341 memmove(&el->l_recs[1], &el->l_recs[0], size);
3342 }
3343
3344 /*
3345 * Always memset - the caller doesn't check whether it
3346 * created an empty extent, so there could be junk in
3347 * the other fields.
3348 */
3349 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3350 }
3351}
3352
Joel Becker4fe82c32009-02-13 02:16:08 -08003353static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003354 struct ocfs2_path *left_path,
3355 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003356{
3357 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003358 u32 right_cpos;
3359 struct ocfs2_path *right_path = NULL;
3360 struct ocfs2_extent_list *left_el;
3361
3362 *ret_right_path = NULL;
3363
3364 /* This function shouldn't be called for non-trees. */
3365 BUG_ON(left_path->p_tree_depth == 0);
3366
3367 left_el = path_leaf_el(left_path);
3368 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3369
Joel Becker4fe82c32009-02-13 02:16:08 -08003370 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3371 left_path, &right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003372 if (ret) {
3373 mlog_errno(ret);
3374 goto out;
3375 }
3376
3377 /* This function shouldn't be called for the rightmost leaf. */
3378 BUG_ON(right_cpos == 0);
3379
Joel Beckerffdd7a52008-10-17 22:32:01 -07003380 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003381 if (!right_path) {
3382 ret = -ENOMEM;
3383 mlog_errno(ret);
3384 goto out;
3385 }
3386
Joel Becker4fe82c32009-02-13 02:16:08 -08003387 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003388 if (ret) {
3389 mlog_errno(ret);
3390 goto out;
3391 }
3392
3393 *ret_right_path = right_path;
3394out:
3395 if (ret)
3396 ocfs2_free_path(right_path);
3397 return ret;
3398}
3399
3400/*
3401 * Remove split_rec clusters from the record at index and merge them
3402 * onto the beginning of the record "next" to it.
3403 * For index < l_count - 1, the next means the extent rec at index + 1.
3404 * For index == l_count - 1, the "next" means the 1st extent rec of the
3405 * next extent block.
3406 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003407static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003408 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003409 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003410 struct ocfs2_extent_rec *split_rec,
3411 int index)
3412{
3413 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003414 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3415 struct ocfs2_extent_rec *left_rec;
3416 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003417 struct ocfs2_extent_list *right_el;
3418 struct ocfs2_path *right_path = NULL;
3419 int subtree_index = 0;
3420 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3421 struct buffer_head *bh = path_leaf_bh(left_path);
3422 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003423
3424 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003425 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003426
Al Viro9d8df6a2008-05-21 06:32:11 +01003427 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003428 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3429 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003430 ret = ocfs2_get_right_path(et, left_path, &right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003431 if (ret) {
3432 mlog_errno(ret);
3433 goto out;
3434 }
3435
3436 right_el = path_leaf_el(right_path);
3437 next_free = le16_to_cpu(right_el->l_next_free_rec);
3438 BUG_ON(next_free <= 0);
3439 right_rec = &right_el->l_recs[0];
3440 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003441 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003442 right_rec = &right_el->l_recs[1];
3443 }
3444
3445 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3446 le16_to_cpu(left_rec->e_leaf_clusters) !=
3447 le32_to_cpu(right_rec->e_cpos));
3448
Joel Becker7dc02802009-02-12 19:20:13 -08003449 subtree_index = ocfs2_find_subtree_root(et, left_path,
3450 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003451
3452 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3453 handle->h_buffer_credits,
3454 right_path);
3455 if (ret) {
3456 mlog_errno(ret);
3457 goto out;
3458 }
3459
3460 root_bh = left_path->p_node[subtree_index].bh;
3461 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3462
Joel Becker7dc02802009-02-12 19:20:13 -08003463 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003464 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003465 if (ret) {
3466 mlog_errno(ret);
3467 goto out;
3468 }
3469
3470 for (i = subtree_index + 1;
3471 i < path_num_items(right_path); i++) {
Joel Becker7dc02802009-02-12 19:20:13 -08003472 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003473 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003474 if (ret) {
3475 mlog_errno(ret);
3476 goto out;
3477 }
3478
Joel Becker7dc02802009-02-12 19:20:13 -08003479 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003480 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003481 if (ret) {
3482 mlog_errno(ret);
3483 goto out;
3484 }
3485 }
3486
3487 } else {
3488 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3489 right_rec = &el->l_recs[index + 1];
3490 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003491
Joel Becker7dc02802009-02-12 19:20:13 -08003492 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
Joel Becker13723d02008-10-17 19:25:01 -07003493 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003494 if (ret) {
3495 mlog_errno(ret);
3496 goto out;
3497 }
3498
3499 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3500
3501 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3502 le64_add_cpu(&right_rec->e_blkno,
Joel Becker7dc02802009-02-12 19:20:13 -08003503 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3504 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003505 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3506
3507 ocfs2_cleanup_merge(el, index);
3508
3509 ret = ocfs2_journal_dirty(handle, bh);
3510 if (ret)
3511 mlog_errno(ret);
3512
Tao Ma677b9752008-01-30 14:21:05 +08003513 if (right_path) {
3514 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3515 if (ret)
3516 mlog_errno(ret);
3517
Joel Becker4619c732009-02-12 19:02:36 -08003518 ocfs2_complete_edge_insert(handle, left_path, right_path,
3519 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003520 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003521out:
Tao Ma677b9752008-01-30 14:21:05 +08003522 if (right_path)
3523 ocfs2_free_path(right_path);
3524 return ret;
3525}
3526
Joel Becker4fe82c32009-02-13 02:16:08 -08003527static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003528 struct ocfs2_path *right_path,
3529 struct ocfs2_path **ret_left_path)
3530{
3531 int ret;
3532 u32 left_cpos;
3533 struct ocfs2_path *left_path = NULL;
3534
3535 *ret_left_path = NULL;
3536
3537 /* This function shouldn't be called for non-trees. */
3538 BUG_ON(right_path->p_tree_depth == 0);
3539
Joel Becker4fe82c32009-02-13 02:16:08 -08003540 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
Tao Ma677b9752008-01-30 14:21:05 +08003541 right_path, &left_cpos);
3542 if (ret) {
3543 mlog_errno(ret);
3544 goto out;
3545 }
3546
3547 /* This function shouldn't be called for the leftmost leaf. */
3548 BUG_ON(left_cpos == 0);
3549
Joel Beckerffdd7a52008-10-17 22:32:01 -07003550 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003551 if (!left_path) {
3552 ret = -ENOMEM;
3553 mlog_errno(ret);
3554 goto out;
3555 }
3556
Joel Becker4fe82c32009-02-13 02:16:08 -08003557 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003558 if (ret) {
3559 mlog_errno(ret);
3560 goto out;
3561 }
3562
3563 *ret_left_path = left_path;
3564out:
3565 if (ret)
3566 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003567 return ret;
3568}
3569
3570/*
3571 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003572 * onto the tail of the record "before" it.
3573 * For index > 0, the "before" means the extent rec at index - 1.
3574 *
3575 * For index == 0, the "before" means the last record of the previous
3576 * extent block. And there is also a situation that we may need to
3577 * remove the rightmost leaf extent block in the right_path and change
3578 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003579 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003580static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003581 handle_t *handle,
Joel Becker4fe82c32009-02-13 02:16:08 -08003582 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003583 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003584 struct ocfs2_cached_dealloc_ctxt *dealloc,
3585 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003586{
Tao Ma677b9752008-01-30 14:21:05 +08003587 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003588 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3589 struct ocfs2_extent_rec *left_rec;
3590 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003591 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3592 struct buffer_head *bh = path_leaf_bh(right_path);
3593 struct buffer_head *root_bh = NULL;
3594 struct ocfs2_path *left_path = NULL;
3595 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003596
Tao Ma677b9752008-01-30 14:21:05 +08003597 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003598
Mark Fasheh328d5752007-06-18 10:48:04 -07003599 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003600 if (index == 0) {
3601 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003602 ret = ocfs2_get_left_path(et, right_path, &left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003603 if (ret) {
3604 mlog_errno(ret);
3605 goto out;
3606 }
3607
3608 left_el = path_leaf_el(left_path);
3609 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3610 le16_to_cpu(left_el->l_count));
3611
3612 left_rec = &left_el->l_recs[
3613 le16_to_cpu(left_el->l_next_free_rec) - 1];
3614 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3615 le16_to_cpu(left_rec->e_leaf_clusters) !=
3616 le32_to_cpu(split_rec->e_cpos));
3617
Joel Becker7dc02802009-02-12 19:20:13 -08003618 subtree_index = ocfs2_find_subtree_root(et, left_path,
3619 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003620
3621 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3622 handle->h_buffer_credits,
3623 left_path);
3624 if (ret) {
3625 mlog_errno(ret);
3626 goto out;
3627 }
3628
3629 root_bh = left_path->p_node[subtree_index].bh;
3630 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3631
Joel Becker4fe82c32009-02-13 02:16:08 -08003632 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003633 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003634 if (ret) {
3635 mlog_errno(ret);
3636 goto out;
3637 }
3638
3639 for (i = subtree_index + 1;
3640 i < path_num_items(right_path); i++) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003641 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003642 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003643 if (ret) {
3644 mlog_errno(ret);
3645 goto out;
3646 }
3647
Joel Becker4fe82c32009-02-13 02:16:08 -08003648 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003649 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003650 if (ret) {
3651 mlog_errno(ret);
3652 goto out;
3653 }
3654 }
3655 } else {
3656 left_rec = &el->l_recs[index - 1];
3657 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3658 has_empty_extent = 1;
3659 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003660
Joel Becker4fe82c32009-02-13 02:16:08 -08003661 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Tao Ma9047bea2009-01-05 14:45:24 +08003662 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003663 if (ret) {
3664 mlog_errno(ret);
3665 goto out;
3666 }
3667
3668 if (has_empty_extent && index == 1) {
3669 /*
3670 * The easy case - we can just plop the record right in.
3671 */
3672 *left_rec = *split_rec;
3673
3674 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003675 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003676 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003677
3678 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3679 le64_add_cpu(&right_rec->e_blkno,
Joel Becker4fe82c32009-02-13 02:16:08 -08003680 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3681 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003682 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3683
3684 ocfs2_cleanup_merge(el, index);
3685
3686 ret = ocfs2_journal_dirty(handle, bh);
3687 if (ret)
3688 mlog_errno(ret);
3689
Tao Ma677b9752008-01-30 14:21:05 +08003690 if (left_path) {
3691 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3692 if (ret)
3693 mlog_errno(ret);
3694
3695 /*
3696 * In the situation that the right_rec is empty and the extent
3697 * block is empty also, ocfs2_complete_edge_insert can't handle
3698 * it and we need to delete the right extent block.
3699 */
3700 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3701 le16_to_cpu(el->l_next_free_rec) == 1) {
3702
Joel Becker70f18c02009-02-13 02:09:31 -08003703 ret = ocfs2_remove_rightmost_path(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003704 right_path,
Joel Becker70f18c02009-02-13 02:09:31 -08003705 dealloc);
Tao Ma677b9752008-01-30 14:21:05 +08003706 if (ret) {
3707 mlog_errno(ret);
3708 goto out;
3709 }
3710
3711 /* Now the rightmost extent block has been deleted.
3712 * So we use the new rightmost path.
3713 */
3714 ocfs2_mv_path(right_path, left_path);
3715 left_path = NULL;
3716 } else
Joel Becker4619c732009-02-12 19:02:36 -08003717 ocfs2_complete_edge_insert(handle, left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003718 right_path, subtree_index);
3719 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003720out:
Tao Ma677b9752008-01-30 14:21:05 +08003721 if (left_path)
3722 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003723 return ret;
3724}
3725
Joel Beckerc495dd22009-02-13 02:19:11 -08003726static int ocfs2_try_to_merge_extent(handle_t *handle,
3727 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003728 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003729 int split_index,
3730 struct ocfs2_extent_rec *split_rec,
3731 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckerc495dd22009-02-13 02:19:11 -08003732 struct ocfs2_merge_ctxt *ctxt)
Mark Fasheh328d5752007-06-18 10:48:04 -07003733{
Tao Mao518d7262007-08-28 17:25:35 -07003734 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003735 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003736 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3737
3738 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3739
Tao Mao518d7262007-08-28 17:25:35 -07003740 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3741 /*
3742 * The merge code will need to create an empty
3743 * extent to take the place of the newly
3744 * emptied slot. Remove any pre-existing empty
3745 * extents - having more than one in a leaf is
3746 * illegal.
3747 */
Joel Becker70f18c02009-02-13 02:09:31 -08003748 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Tao Mao518d7262007-08-28 17:25:35 -07003749 if (ret) {
3750 mlog_errno(ret);
3751 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003752 }
Tao Mao518d7262007-08-28 17:25:35 -07003753 split_index--;
3754 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003755 }
3756
3757 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3758 /*
3759 * Left-right contig implies this.
3760 */
3761 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003762
3763 /*
3764 * Since the leftright insert always covers the entire
3765 * extent, this call will delete the insert record
3766 * entirely, resulting in an empty extent record added to
3767 * the extent block.
3768 *
3769 * Since the adding of an empty extent shifts
3770 * everything back to the right, there's no need to
3771 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003772 *
3773 * When the split_index is zero, we need to merge it to the
3774 * prevoius extent block. It is more efficient and easier
3775 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003776 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003777 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003778 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003779 if (ret) {
3780 mlog_errno(ret);
3781 goto out;
3782 }
3783
3784 /*
3785 * We can only get this from logic error above.
3786 */
3787 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3788
Tao Ma677b9752008-01-30 14:21:05 +08003789 /* The merge left us with an empty extent, remove it. */
Joel Becker70f18c02009-02-13 02:09:31 -08003790 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003791 if (ret) {
3792 mlog_errno(ret);
3793 goto out;
3794 }
Tao Ma677b9752008-01-30 14:21:05 +08003795
Mark Fasheh328d5752007-06-18 10:48:04 -07003796 rec = &el->l_recs[split_index];
3797
3798 /*
3799 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003800 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003801 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003802 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3803 dealloc, split_index);
Tao Ma677b9752008-01-30 14:21:05 +08003804
Mark Fasheh328d5752007-06-18 10:48:04 -07003805 if (ret) {
3806 mlog_errno(ret);
3807 goto out;
3808 }
3809
Joel Becker70f18c02009-02-13 02:09:31 -08003810 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003811 /*
3812 * Error from this last rotate is not critical, so
3813 * print but don't bubble it up.
3814 */
3815 if (ret)
3816 mlog_errno(ret);
3817 ret = 0;
3818 } else {
3819 /*
3820 * Merge a record to the left or right.
3821 *
3822 * 'contig_type' is relative to the existing record,
3823 * so for example, if we're "right contig", it's to
3824 * the record on the left (hence the left merge).
3825 */
3826 if (ctxt->c_contig_type == CONTIG_RIGHT) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003827 ret = ocfs2_merge_rec_left(path, handle, et,
3828 split_rec, dealloc,
Mark Fasheh328d5752007-06-18 10:48:04 -07003829 split_index);
3830 if (ret) {
3831 mlog_errno(ret);
3832 goto out;
3833 }
3834 } else {
Joel Becker4fe82c32009-02-13 02:16:08 -08003835 ret = ocfs2_merge_rec_right(path, handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003836 et, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003837 split_index);
3838 if (ret) {
3839 mlog_errno(ret);
3840 goto out;
3841 }
3842 }
3843
3844 if (ctxt->c_split_covers_rec) {
3845 /*
3846 * The merge may have left an empty extent in
3847 * our leaf. Try to rotate it away.
3848 */
Joel Becker70f18c02009-02-13 02:09:31 -08003849 ret = ocfs2_rotate_tree_left(handle, et, path,
3850 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003851 if (ret)
3852 mlog_errno(ret);
3853 ret = 0;
3854 }
3855 }
3856
3857out:
3858 return ret;
3859}
3860
3861static void ocfs2_subtract_from_rec(struct super_block *sb,
3862 enum ocfs2_split_type split,
3863 struct ocfs2_extent_rec *rec,
3864 struct ocfs2_extent_rec *split_rec)
3865{
3866 u64 len_blocks;
3867
3868 len_blocks = ocfs2_clusters_to_blocks(sb,
3869 le16_to_cpu(split_rec->e_leaf_clusters));
3870
3871 if (split == SPLIT_LEFT) {
3872 /*
3873 * Region is on the left edge of the existing
3874 * record.
3875 */
3876 le32_add_cpu(&rec->e_cpos,
3877 le16_to_cpu(split_rec->e_leaf_clusters));
3878 le64_add_cpu(&rec->e_blkno, len_blocks);
3879 le16_add_cpu(&rec->e_leaf_clusters,
3880 -le16_to_cpu(split_rec->e_leaf_clusters));
3881 } else {
3882 /*
3883 * Region is on the right edge of the existing
3884 * record.
3885 */
3886 le16_add_cpu(&rec->e_leaf_clusters,
3887 -le16_to_cpu(split_rec->e_leaf_clusters));
3888 }
3889}
3890
Mark Fashehdcd05382007-01-16 11:32:23 -08003891/*
3892 * Do the final bits of extent record insertion at the target leaf
3893 * list. If this leaf is part of an allocation tree, it is assumed
3894 * that the tree above has been prepared.
3895 */
Joel Beckerd5628622009-02-13 02:54:36 -08003896static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3897 struct ocfs2_extent_rec *insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08003898 struct ocfs2_extent_list *el,
Joel Beckerd5628622009-02-13 02:54:36 -08003899 struct ocfs2_insert_type *insert)
Mark Fashehdcd05382007-01-16 11:32:23 -08003900{
3901 int i = insert->ins_contig_index;
3902 unsigned int range;
3903 struct ocfs2_extent_rec *rec;
3904
Mark Fashehe48edee2007-03-07 16:46:57 -08003905 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003906
Mark Fasheh328d5752007-06-18 10:48:04 -07003907 if (insert->ins_split != SPLIT_NONE) {
3908 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3909 BUG_ON(i == -1);
3910 rec = &el->l_recs[i];
Joel Beckerd5628622009-02-13 02:54:36 -08003911 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3912 insert->ins_split, rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003913 insert_rec);
3914 goto rotate;
3915 }
3916
Mark Fashehdcd05382007-01-16 11:32:23 -08003917 /*
3918 * Contiguous insert - either left or right.
3919 */
3920 if (insert->ins_contig != CONTIG_NONE) {
3921 rec = &el->l_recs[i];
3922 if (insert->ins_contig == CONTIG_LEFT) {
3923 rec->e_blkno = insert_rec->e_blkno;
3924 rec->e_cpos = insert_rec->e_cpos;
3925 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003926 le16_add_cpu(&rec->e_leaf_clusters,
3927 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003928 return;
3929 }
3930
3931 /*
3932 * Handle insert into an empty leaf.
3933 */
3934 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3935 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3936 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3937 el->l_recs[0] = *insert_rec;
3938 el->l_next_free_rec = cpu_to_le16(1);
3939 return;
3940 }
3941
3942 /*
3943 * Appending insert.
3944 */
3945 if (insert->ins_appending == APPEND_TAIL) {
3946 i = le16_to_cpu(el->l_next_free_rec) - 1;
3947 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003948 range = le32_to_cpu(rec->e_cpos)
3949 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003950 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3951
3952 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3953 le16_to_cpu(el->l_count),
Joel Beckerd5628622009-02-13 02:54:36 -08003954 "owner %llu, depth %u, count %u, next free %u, "
Mark Fashehdcd05382007-01-16 11:32:23 -08003955 "rec.cpos %u, rec.clusters %u, "
3956 "insert.cpos %u, insert.clusters %u\n",
Joel Beckerd5628622009-02-13 02:54:36 -08003957 ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08003958 le16_to_cpu(el->l_tree_depth),
3959 le16_to_cpu(el->l_count),
3960 le16_to_cpu(el->l_next_free_rec),
3961 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003962 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003963 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003964 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003965 i++;
3966 el->l_recs[i] = *insert_rec;
3967 le16_add_cpu(&el->l_next_free_rec, 1);
3968 return;
3969 }
3970
Mark Fasheh328d5752007-06-18 10:48:04 -07003971rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003972 /*
3973 * Ok, we have to rotate.
3974 *
3975 * At this point, it is safe to assume that inserting into an
3976 * empty leaf and appending to a leaf have both been handled
3977 * above.
3978 *
3979 * This leaf needs to have space, either by the empty 1st
3980 * extent record, or by virtue of an l_next_rec < l_count.
3981 */
3982 ocfs2_rotate_leaf(el, insert_rec);
3983}
3984
Joel Beckerd401dc12009-02-13 02:24:10 -08003985static void ocfs2_adjust_rightmost_records(handle_t *handle,
3986 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003987 struct ocfs2_path *path,
3988 struct ocfs2_extent_rec *insert_rec)
3989{
3990 int ret, i, next_free;
3991 struct buffer_head *bh;
3992 struct ocfs2_extent_list *el;
3993 struct ocfs2_extent_rec *rec;
3994
3995 /*
3996 * Update everything except the leaf block.
3997 */
3998 for (i = 0; i < path->p_tree_depth; i++) {
3999 bh = path->p_node[i].bh;
4000 el = path->p_node[i].el;
4001
4002 next_free = le16_to_cpu(el->l_next_free_rec);
4003 if (next_free == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004004 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
4005 "Owner %llu has a bad extent list",
4006 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fasheh328d5752007-06-18 10:48:04 -07004007 ret = -EIO;
4008 return;
4009 }
4010
4011 rec = &el->l_recs[next_free - 1];
4012
4013 rec->e_int_clusters = insert_rec->e_cpos;
4014 le32_add_cpu(&rec->e_int_clusters,
4015 le16_to_cpu(insert_rec->e_leaf_clusters));
4016 le32_add_cpu(&rec->e_int_clusters,
4017 -le32_to_cpu(rec->e_cpos));
4018
4019 ret = ocfs2_journal_dirty(handle, bh);
4020 if (ret)
4021 mlog_errno(ret);
4022
4023 }
4024}
4025
Joel Beckerd401dc12009-02-13 02:24:10 -08004026static int ocfs2_append_rec_to_path(handle_t *handle,
4027 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004028 struct ocfs2_extent_rec *insert_rec,
4029 struct ocfs2_path *right_path,
4030 struct ocfs2_path **ret_left_path)
4031{
Mark Fasheh328d5752007-06-18 10:48:04 -07004032 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08004033 struct ocfs2_extent_list *el;
4034 struct ocfs2_path *left_path = NULL;
4035
4036 *ret_left_path = NULL;
4037
4038 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08004039 * This shouldn't happen for non-trees. The extent rec cluster
4040 * count manipulation below only works for interior nodes.
4041 */
4042 BUG_ON(right_path->p_tree_depth == 0);
4043
4044 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08004045 * If our appending insert is at the leftmost edge of a leaf,
4046 * then we might need to update the rightmost records of the
4047 * neighboring path.
4048 */
4049 el = path_leaf_el(right_path);
4050 next_free = le16_to_cpu(el->l_next_free_rec);
4051 if (next_free == 0 ||
4052 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
4053 u32 left_cpos;
4054
Joel Beckerd401dc12009-02-13 02:24:10 -08004055 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
4056 right_path, &left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004057 if (ret) {
4058 mlog_errno(ret);
4059 goto out;
4060 }
4061
4062 mlog(0, "Append may need a left path update. cpos: %u, "
4063 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
4064 left_cpos);
4065
4066 /*
4067 * No need to worry if the append is already in the
4068 * leftmost leaf.
4069 */
4070 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004071 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004072 if (!left_path) {
4073 ret = -ENOMEM;
4074 mlog_errno(ret);
4075 goto out;
4076 }
4077
Joel Beckerd401dc12009-02-13 02:24:10 -08004078 ret = ocfs2_find_path(et->et_ci, left_path,
Joel Beckerfacdb772009-02-12 18:08:48 -08004079 left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004080 if (ret) {
4081 mlog_errno(ret);
4082 goto out;
4083 }
4084
4085 /*
4086 * ocfs2_insert_path() will pass the left_path to the
4087 * journal for us.
4088 */
4089 }
4090 }
4091
Joel Beckerd401dc12009-02-13 02:24:10 -08004092 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004093 if (ret) {
4094 mlog_errno(ret);
4095 goto out;
4096 }
4097
Joel Beckerd401dc12009-02-13 02:24:10 -08004098 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004099
4100 *ret_left_path = left_path;
4101 ret = 0;
4102out:
4103 if (ret != 0)
4104 ocfs2_free_path(left_path);
4105
4106 return ret;
4107}
4108
Joel Beckerc38e52b2009-02-13 02:56:23 -08004109static void ocfs2_split_record(struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004110 struct ocfs2_path *left_path,
4111 struct ocfs2_path *right_path,
4112 struct ocfs2_extent_rec *split_rec,
4113 enum ocfs2_split_type split)
4114{
4115 int index;
4116 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4117 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4118 struct ocfs2_extent_rec *rec, *tmprec;
4119
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004120 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004121 if (left_path)
4122 left_el = path_leaf_el(left_path);
4123
4124 el = right_el;
4125 insert_el = right_el;
4126 index = ocfs2_search_extent_list(el, cpos);
4127 if (index != -1) {
4128 if (index == 0 && left_path) {
4129 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4130
4131 /*
4132 * This typically means that the record
4133 * started in the left path but moved to the
4134 * right as a result of rotation. We either
4135 * move the existing record to the left, or we
4136 * do the later insert there.
4137 *
4138 * In this case, the left path should always
4139 * exist as the rotate code will have passed
4140 * it back for a post-insert update.
4141 */
4142
4143 if (split == SPLIT_LEFT) {
4144 /*
4145 * It's a left split. Since we know
4146 * that the rotate code gave us an
4147 * empty extent in the left path, we
4148 * can just do the insert there.
4149 */
4150 insert_el = left_el;
4151 } else {
4152 /*
4153 * Right split - we have to move the
4154 * existing record over to the left
4155 * leaf. The insert will be into the
4156 * newly created empty extent in the
4157 * right leaf.
4158 */
4159 tmprec = &right_el->l_recs[index];
4160 ocfs2_rotate_leaf(left_el, tmprec);
4161 el = left_el;
4162
4163 memset(tmprec, 0, sizeof(*tmprec));
4164 index = ocfs2_search_extent_list(left_el, cpos);
4165 BUG_ON(index == -1);
4166 }
4167 }
4168 } else {
4169 BUG_ON(!left_path);
4170 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4171 /*
4172 * Left path is easy - we can just allow the insert to
4173 * happen.
4174 */
4175 el = left_el;
4176 insert_el = left_el;
4177 index = ocfs2_search_extent_list(el, cpos);
4178 BUG_ON(index == -1);
4179 }
4180
4181 rec = &el->l_recs[index];
Joel Beckerc38e52b2009-02-13 02:56:23 -08004182 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4183 split, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004184 ocfs2_rotate_leaf(insert_el, split_rec);
4185}
4186
Mark Fashehdcd05382007-01-16 11:32:23 -08004187/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004188 * This function only does inserts on an allocation b-tree. For tree
4189 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004190 *
4191 * right_path is the path we want to do the actual insert
4192 * in. left_path should only be passed in if we need to update that
4193 * portion of the tree after an edge insert.
4194 */
Joel Becker3505bec2009-02-13 02:57:58 -08004195static int ocfs2_insert_path(handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08004196 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004197 struct ocfs2_path *left_path,
4198 struct ocfs2_path *right_path,
4199 struct ocfs2_extent_rec *insert_rec,
4200 struct ocfs2_insert_type *insert)
4201{
4202 int ret, subtree_index;
4203 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004204
Mark Fashehdcd05382007-01-16 11:32:23 -08004205 if (left_path) {
4206 int credits = handle->h_buffer_credits;
4207
4208 /*
4209 * There's a chance that left_path got passed back to
4210 * us without being accounted for in the
4211 * journal. Extend our transaction here to be sure we
4212 * can change those blocks.
4213 */
4214 credits += left_path->p_tree_depth;
4215
4216 ret = ocfs2_extend_trans(handle, credits);
4217 if (ret < 0) {
4218 mlog_errno(ret);
4219 goto out;
4220 }
4221
Joel Becker7dc02802009-02-12 19:20:13 -08004222 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004223 if (ret < 0) {
4224 mlog_errno(ret);
4225 goto out;
4226 }
4227 }
4228
Mark Fashehe8aed342007-12-03 16:43:01 -08004229 /*
4230 * Pass both paths to the journal. The majority of inserts
4231 * will be touching all components anyway.
4232 */
Joel Becker7dc02802009-02-12 19:20:13 -08004233 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004234 if (ret < 0) {
4235 mlog_errno(ret);
4236 goto out;
4237 }
4238
Mark Fasheh328d5752007-06-18 10:48:04 -07004239 if (insert->ins_split != SPLIT_NONE) {
4240 /*
4241 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004242 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004243 * function sort it all out.
4244 */
Joel Beckerc38e52b2009-02-13 02:56:23 -08004245 ocfs2_split_record(et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004246 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004247
4248 /*
4249 * Split might have modified either leaf and we don't
4250 * have a guarantee that the later edge insert will
4251 * dirty this for us.
4252 */
4253 if (left_path)
4254 ret = ocfs2_journal_dirty(handle,
4255 path_leaf_bh(left_path));
4256 if (ret)
4257 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07004258 } else
Joel Beckerd5628622009-02-13 02:54:36 -08004259 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4260 insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004261
Mark Fashehdcd05382007-01-16 11:32:23 -08004262 ret = ocfs2_journal_dirty(handle, leaf_bh);
4263 if (ret)
4264 mlog_errno(ret);
4265
4266 if (left_path) {
4267 /*
4268 * The rotate code has indicated that we need to fix
4269 * up portions of the tree after the insert.
4270 *
4271 * XXX: Should we extend the transaction here?
4272 */
Joel Becker7dc02802009-02-12 19:20:13 -08004273 subtree_index = ocfs2_find_subtree_root(et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004274 right_path);
Joel Becker4619c732009-02-12 19:02:36 -08004275 ocfs2_complete_edge_insert(handle, left_path, right_path,
4276 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08004277 }
4278
4279 ret = 0;
4280out:
4281 return ret;
4282}
4283
Joel Becker3505bec2009-02-13 02:57:58 -08004284static int ocfs2_do_insert_extent(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004285 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004286 struct ocfs2_extent_rec *insert_rec,
4287 struct ocfs2_insert_type *type)
4288{
4289 int ret, rotate = 0;
4290 u32 cpos;
4291 struct ocfs2_path *right_path = NULL;
4292 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004293 struct ocfs2_extent_list *el;
4294
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004295 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004296
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004297 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004298 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004299 if (ret) {
4300 mlog_errno(ret);
4301 goto out;
4302 }
4303
4304 if (le16_to_cpu(el->l_tree_depth) == 0) {
Joel Beckerd5628622009-02-13 02:54:36 -08004305 ocfs2_insert_at_leaf(et, insert_rec, el, type);
Mark Fashehdcd05382007-01-16 11:32:23 -08004306 goto out_update_clusters;
4307 }
4308
Joel Beckerffdd7a52008-10-17 22:32:01 -07004309 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004310 if (!right_path) {
4311 ret = -ENOMEM;
4312 mlog_errno(ret);
4313 goto out;
4314 }
4315
4316 /*
4317 * Determine the path to start with. Rotations need the
4318 * rightmost path, everything else can go directly to the
4319 * target leaf.
4320 */
4321 cpos = le32_to_cpu(insert_rec->e_cpos);
4322 if (type->ins_appending == APPEND_NONE &&
4323 type->ins_contig == CONTIG_NONE) {
4324 rotate = 1;
4325 cpos = UINT_MAX;
4326 }
4327
Joel Beckerfacdb772009-02-12 18:08:48 -08004328 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004329 if (ret) {
4330 mlog_errno(ret);
4331 goto out;
4332 }
4333
4334 /*
4335 * Rotations and appends need special treatment - they modify
4336 * parts of the tree's above them.
4337 *
4338 * Both might pass back a path immediate to the left of the
4339 * one being inserted to. This will be cause
4340 * ocfs2_insert_path() to modify the rightmost records of
4341 * left_path to account for an edge insert.
4342 *
4343 * XXX: When modifying this code, keep in mind that an insert
4344 * can wind up skipping both of these two special cases...
4345 */
4346 if (rotate) {
Joel Becker1bbf0b82009-02-12 19:42:08 -08004347 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004348 le32_to_cpu(insert_rec->e_cpos),
4349 right_path, &left_path);
4350 if (ret) {
4351 mlog_errno(ret);
4352 goto out;
4353 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004354
4355 /*
4356 * ocfs2_rotate_tree_right() might have extended the
4357 * transaction without re-journaling our tree root.
4358 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004359 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004360 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004361 if (ret) {
4362 mlog_errno(ret);
4363 goto out;
4364 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004365 } else if (type->ins_appending == APPEND_TAIL
4366 && type->ins_contig != CONTIG_LEFT) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004367 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08004368 right_path, &left_path);
4369 if (ret) {
4370 mlog_errno(ret);
4371 goto out;
4372 }
4373 }
4374
Joel Becker3505bec2009-02-13 02:57:58 -08004375 ret = ocfs2_insert_path(handle, et, left_path, right_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004376 insert_rec, type);
4377 if (ret) {
4378 mlog_errno(ret);
4379 goto out;
4380 }
4381
4382out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004383 if (type->ins_split == SPLIT_NONE)
Joel Becker6136ca52009-02-12 19:32:43 -08004384 ocfs2_et_update_clusters(et,
Joel Becker35dc0aa2008-08-20 16:25:06 -07004385 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004386
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004387 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004388 if (ret)
4389 mlog_errno(ret);
4390
4391out:
4392 ocfs2_free_path(left_path);
4393 ocfs2_free_path(right_path);
4394
4395 return ret;
4396}
4397
Mark Fasheh328d5752007-06-18 10:48:04 -07004398static enum ocfs2_contig_type
Joel Beckera2970292009-02-13 03:09:54 -08004399ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4400 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004401 struct ocfs2_extent_list *el, int index,
4402 struct ocfs2_extent_rec *split_rec)
4403{
Tao Maad5a4d72008-01-30 14:21:32 +08004404 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004405 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004406 u32 left_cpos, right_cpos;
4407 struct ocfs2_extent_rec *rec = NULL;
4408 struct ocfs2_extent_list *new_el;
4409 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4410 struct buffer_head *bh;
4411 struct ocfs2_extent_block *eb;
Joel Beckera2970292009-02-13 03:09:54 -08004412 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Tao Maad5a4d72008-01-30 14:21:32 +08004413
4414 if (index > 0) {
4415 rec = &el->l_recs[index - 1];
4416 } else if (path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004417 status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004418 if (status)
4419 goto out;
4420
4421 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004422 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004423 if (!left_path)
4424 goto out;
4425
Joel Beckera2970292009-02-13 03:09:54 -08004426 status = ocfs2_find_path(et->et_ci, left_path,
4427 left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004428 if (status)
4429 goto out;
4430
4431 new_el = path_leaf_el(left_path);
4432
4433 if (le16_to_cpu(new_el->l_next_free_rec) !=
4434 le16_to_cpu(new_el->l_count)) {
4435 bh = path_leaf_bh(left_path);
4436 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004437 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004438 "Extent block #%llu has an "
4439 "invalid l_next_free_rec of "
4440 "%d. It should have "
4441 "matched the l_count of %d",
4442 (unsigned long long)le64_to_cpu(eb->h_blkno),
4443 le16_to_cpu(new_el->l_next_free_rec),
4444 le16_to_cpu(new_el->l_count));
4445 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004446 goto out;
4447 }
4448 rec = &new_el->l_recs[
4449 le16_to_cpu(new_el->l_next_free_rec) - 1];
4450 }
4451 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004452
4453 /*
4454 * We're careful to check for an empty extent record here -
4455 * the merge code will know what to do if it sees one.
4456 */
Tao Maad5a4d72008-01-30 14:21:32 +08004457 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004458 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4459 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4460 ret = CONTIG_RIGHT;
4461 } else {
Tao Ma853a3a12009-08-18 11:22:18 +08004462 ret = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004463 }
4464 }
4465
Tao Maad5a4d72008-01-30 14:21:32 +08004466 rec = NULL;
4467 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4468 rec = &el->l_recs[index + 1];
4469 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4470 path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004471 status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004472 if (status)
4473 goto out;
4474
4475 if (right_cpos == 0)
4476 goto out;
4477
Joel Beckerffdd7a52008-10-17 22:32:01 -07004478 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004479 if (!right_path)
4480 goto out;
4481
Joel Beckera2970292009-02-13 03:09:54 -08004482 status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004483 if (status)
4484 goto out;
4485
4486 new_el = path_leaf_el(right_path);
4487 rec = &new_el->l_recs[0];
4488 if (ocfs2_is_empty_extent(rec)) {
4489 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4490 bh = path_leaf_bh(right_path);
4491 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004492 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004493 "Extent block #%llu has an "
4494 "invalid l_next_free_rec of %d",
4495 (unsigned long long)le64_to_cpu(eb->h_blkno),
4496 le16_to_cpu(new_el->l_next_free_rec));
4497 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004498 goto out;
4499 }
4500 rec = &new_el->l_recs[1];
4501 }
4502 }
4503
4504 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004505 enum ocfs2_contig_type contig_type;
4506
Tao Ma853a3a12009-08-18 11:22:18 +08004507 contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004508
4509 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4510 ret = CONTIG_LEFTRIGHT;
4511 else if (ret == CONTIG_NONE)
4512 ret = contig_type;
4513 }
4514
Tao Maad5a4d72008-01-30 14:21:32 +08004515out:
4516 if (left_path)
4517 ocfs2_free_path(left_path);
4518 if (right_path)
4519 ocfs2_free_path(right_path);
4520
Mark Fasheh328d5752007-06-18 10:48:04 -07004521 return ret;
4522}
4523
Joel Becker1ef61b32009-02-13 03:12:33 -08004524static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004525 struct ocfs2_insert_type *insert,
4526 struct ocfs2_extent_list *el,
Joel Becker1ef61b32009-02-13 03:12:33 -08004527 struct ocfs2_extent_rec *insert_rec)
Mark Fashehdcd05382007-01-16 11:32:23 -08004528{
4529 int i;
4530 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4531
Mark Fashehe48edee2007-03-07 16:46:57 -08004532 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4533
Mark Fashehdcd05382007-01-16 11:32:23 -08004534 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
Tao Ma853a3a12009-08-18 11:22:18 +08004535 contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4536 insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004537 if (contig_type != CONTIG_NONE) {
4538 insert->ins_contig_index = i;
4539 break;
4540 }
4541 }
4542 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004543
4544 if (insert->ins_contig != CONTIG_NONE) {
4545 struct ocfs2_extent_rec *rec =
4546 &el->l_recs[insert->ins_contig_index];
4547 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4548 le16_to_cpu(insert_rec->e_leaf_clusters);
4549
4550 /*
4551 * Caller might want us to limit the size of extents, don't
4552 * calculate contiguousness if we might exceed that limit.
4553 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004554 if (et->et_max_leaf_clusters &&
4555 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004556 insert->ins_contig = CONTIG_NONE;
4557 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004558}
4559
4560/*
4561 * This should only be called against the righmost leaf extent list.
4562 *
4563 * ocfs2_figure_appending_type() will figure out whether we'll have to
4564 * insert at the tail of the rightmost leaf.
4565 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004566 * This should also work against the root extent list for tree's with 0
4567 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004568 * then the logic here makes sense.
4569 */
4570static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4571 struct ocfs2_extent_list *el,
4572 struct ocfs2_extent_rec *insert_rec)
4573{
4574 int i;
4575 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4576 struct ocfs2_extent_rec *rec;
4577
4578 insert->ins_appending = APPEND_NONE;
4579
Mark Fashehe48edee2007-03-07 16:46:57 -08004580 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004581
4582 if (!el->l_next_free_rec)
4583 goto set_tail_append;
4584
4585 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4586 /* Were all records empty? */
4587 if (le16_to_cpu(el->l_next_free_rec) == 1)
4588 goto set_tail_append;
4589 }
4590
4591 i = le16_to_cpu(el->l_next_free_rec) - 1;
4592 rec = &el->l_recs[i];
4593
Mark Fashehe48edee2007-03-07 16:46:57 -08004594 if (cpos >=
4595 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004596 goto set_tail_append;
4597
4598 return;
4599
4600set_tail_append:
4601 insert->ins_appending = APPEND_TAIL;
4602}
4603
4604/*
4605 * Helper function called at the begining of an insert.
4606 *
4607 * This computes a few things that are commonly used in the process of
4608 * inserting into the btree:
4609 * - Whether the new extent is contiguous with an existing one.
4610 * - The current tree depth.
4611 * - Whether the insert is an appending one.
4612 * - The total # of free records in the tree.
4613 *
4614 * All of the information is stored on the ocfs2_insert_type
4615 * structure.
4616 */
Joel Becker627961b2009-02-13 03:14:38 -08004617static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004618 struct buffer_head **last_eb_bh,
4619 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004620 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004621 struct ocfs2_insert_type *insert)
4622{
4623 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004624 struct ocfs2_extent_block *eb;
4625 struct ocfs2_extent_list *el;
4626 struct ocfs2_path *path = NULL;
4627 struct buffer_head *bh = NULL;
4628
Mark Fasheh328d5752007-06-18 10:48:04 -07004629 insert->ins_split = SPLIT_NONE;
4630
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004631 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004632 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4633
4634 if (el->l_tree_depth) {
4635 /*
4636 * If we have tree depth, we read in the
4637 * rightmost extent block ahead of time as
4638 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4639 * may want it later.
4640 */
Joel Becker3d03a302009-02-12 17:49:26 -08004641 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08004642 ocfs2_et_get_last_eb_blk(et),
4643 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004644 if (ret) {
4645 mlog_exit(ret);
4646 goto out;
4647 }
4648 eb = (struct ocfs2_extent_block *) bh->b_data;
4649 el = &eb->h_list;
4650 }
4651
4652 /*
4653 * Unless we have a contiguous insert, we'll need to know if
4654 * there is room left in our allocation tree for another
4655 * extent record.
4656 *
4657 * XXX: This test is simplistic, we can search for empty
4658 * extent records too.
4659 */
Tao Maoc77534f2007-08-28 17:22:33 -07004660 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004661 le16_to_cpu(el->l_next_free_rec);
4662
4663 if (!insert->ins_tree_depth) {
Joel Becker1ef61b32009-02-13 03:12:33 -08004664 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004665 ocfs2_figure_appending_type(insert, el, insert_rec);
4666 return 0;
4667 }
4668
Joel Beckerffdd7a52008-10-17 22:32:01 -07004669 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004670 if (!path) {
4671 ret = -ENOMEM;
4672 mlog_errno(ret);
4673 goto out;
4674 }
4675
4676 /*
4677 * In the case that we're inserting past what the tree
4678 * currently accounts for, ocfs2_find_path() will return for
4679 * us the rightmost tree path. This is accounted for below in
4680 * the appending code.
4681 */
Joel Beckerfacdb772009-02-12 18:08:48 -08004682 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -08004683 if (ret) {
4684 mlog_errno(ret);
4685 goto out;
4686 }
4687
4688 el = path_leaf_el(path);
4689
4690 /*
4691 * Now that we have the path, there's two things we want to determine:
4692 * 1) Contiguousness (also set contig_index if this is so)
4693 *
4694 * 2) Are we doing an append? We can trivially break this up
4695 * into two types of appends: simple record append, or a
4696 * rotate inside the tail leaf.
4697 */
Joel Becker1ef61b32009-02-13 03:12:33 -08004698 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004699
4700 /*
4701 * The insert code isn't quite ready to deal with all cases of
4702 * left contiguousness. Specifically, if it's an insert into
4703 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004704 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004705 * left. For now, just catch that case and fool the layers
4706 * above us. This works just fine for tree_depth == 0, which
4707 * is why we allow that above.
4708 */
4709 if (insert->ins_contig == CONTIG_LEFT &&
4710 insert->ins_contig_index == 0)
4711 insert->ins_contig = CONTIG_NONE;
4712
4713 /*
4714 * Ok, so we can simply compare against last_eb to figure out
4715 * whether the path doesn't exist. This will only happen in
4716 * the case that we're doing a tail append, so maybe we can
4717 * take advantage of that information somehow.
4718 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004719 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004720 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004721 /*
4722 * Ok, ocfs2_find_path() returned us the rightmost
4723 * tree path. This might be an appending insert. There are
4724 * two cases:
4725 * 1) We're doing a true append at the tail:
4726 * -This might even be off the end of the leaf
4727 * 2) We're "appending" by rotating in the tail
4728 */
4729 ocfs2_figure_appending_type(insert, el, insert_rec);
4730 }
4731
4732out:
4733 ocfs2_free_path(path);
4734
4735 if (ret == 0)
4736 *last_eb_bh = bh;
4737 else
4738 brelse(bh);
4739 return ret;
4740}
4741
4742/*
Joel Beckercc79d8c2009-02-13 03:24:43 -08004743 * Insert an extent into a btree.
Mark Fashehdcd05382007-01-16 11:32:23 -08004744 *
Joel Beckercc79d8c2009-02-13 03:24:43 -08004745 * The caller needs to update the owning btree's cluster count.
Mark Fashehdcd05382007-01-16 11:32:23 -08004746 */
Joel Beckercc79d8c2009-02-13 03:24:43 -08004747int ocfs2_insert_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004748 struct ocfs2_extent_tree *et,
4749 u32 cpos,
4750 u64 start_blk,
4751 u32 new_clusters,
4752 u8 flags,
4753 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004754{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004755 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004756 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004757 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004758 struct ocfs2_insert_type insert = {0, };
4759 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004760
Joel Beckercc79d8c2009-02-13 03:24:43 -08004761 mlog(0, "add %u clusters at position %u to owner %llu\n",
4762 new_clusters, cpos,
4763 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08004764
Mark Fashehe48edee2007-03-07 16:46:57 -08004765 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004766 rec.e_cpos = cpu_to_le32(cpos);
4767 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004768 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004769 rec.e_flags = flags;
Joel Becker6136ca52009-02-12 19:32:43 -08004770 status = ocfs2_et_insert_check(et, &rec);
Joel Becker1e61ee72008-08-20 18:32:45 -07004771 if (status) {
4772 mlog_errno(status);
4773 goto bail;
4774 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004775
Joel Becker627961b2009-02-13 03:14:38 -08004776 status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004777 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004778 if (status < 0) {
4779 mlog_errno(status);
4780 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004781 }
4782
Mark Fashehdcd05382007-01-16 11:32:23 -08004783 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4784 "Insert.contig_index: %d, Insert.free_records: %d, "
4785 "Insert.tree_depth: %d\n",
4786 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004787 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004788
Tao Maoc77534f2007-08-28 17:22:33 -07004789 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004790 status = ocfs2_grow_tree(handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004791 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004792 meta_ac);
4793 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004794 mlog_errno(status);
4795 goto bail;
4796 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004797 }
4798
Mark Fashehdcd05382007-01-16 11:32:23 -08004799 /* Finally, we can add clusters. This might rotate the tree for us. */
Joel Becker3505bec2009-02-13 02:57:58 -08004800 status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004801 if (status < 0)
4802 mlog_errno(status);
Joel Becker92ba4702009-02-13 03:18:34 -08004803 else
4804 ocfs2_et_extent_map_insert(et, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004805
4806bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004807 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004808
Tao Maf56654c2008-08-18 17:38:48 +08004809 mlog_exit(status);
4810 return status;
4811}
4812
Tao Ma0eb8d472008-08-18 17:38:45 +08004813/*
4814 * Allcate and add clusters into the extent b-tree.
4815 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004816 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004817 * it is not limited to the file storage. Any extent tree can use this
4818 * function if it implements the proper ocfs2_extent_tree.
4819 */
Joel Beckercbee7e12009-02-13 03:34:15 -08004820int ocfs2_add_clusters_in_btree(handle_t *handle,
4821 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004822 u32 *logical_offset,
4823 u32 clusters_to_add,
4824 int mark_unwritten,
Tao Ma0eb8d472008-08-18 17:38:45 +08004825 struct ocfs2_alloc_context *data_ac,
4826 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004827 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004828{
4829 int status = 0;
4830 int free_extents;
4831 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4832 u32 bit_off, num_bits;
4833 u64 block;
4834 u8 flags = 0;
Joel Beckercbee7e12009-02-13 03:34:15 -08004835 struct ocfs2_super *osb =
4836 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Tao Ma0eb8d472008-08-18 17:38:45 +08004837
4838 BUG_ON(!clusters_to_add);
4839
4840 if (mark_unwritten)
4841 flags = OCFS2_EXT_UNWRITTEN;
4842
Joel Becker3d03a302009-02-12 17:49:26 -08004843 free_extents = ocfs2_num_free_extents(osb, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004844 if (free_extents < 0) {
4845 status = free_extents;
4846 mlog_errno(status);
4847 goto leave;
4848 }
4849
4850 /* there are two cases which could cause us to EAGAIN in the
4851 * we-need-more-metadata case:
4852 * 1) we haven't reserved *any*
4853 * 2) we are so fragmented, we've needed to add metadata too
4854 * many times. */
4855 if (!free_extents && !meta_ac) {
4856 mlog(0, "we haven't reserved any metadata!\n");
4857 status = -EAGAIN;
4858 reason = RESTART_META;
4859 goto leave;
4860 } else if ((!free_extents)
4861 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004862 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004863 mlog(0, "filesystem is really fragmented...\n");
4864 status = -EAGAIN;
4865 reason = RESTART_META;
4866 goto leave;
4867 }
4868
4869 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4870 clusters_to_add, &bit_off, &num_bits);
4871 if (status < 0) {
4872 if (status != -ENOSPC)
4873 mlog_errno(status);
4874 goto leave;
4875 }
4876
4877 BUG_ON(num_bits > clusters_to_add);
4878
Joel Becker13723d02008-10-17 19:25:01 -07004879 /* reserve our write early -- insert_extent may update the tree root */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004880 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004881 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d472008-08-18 17:38:45 +08004882 if (status < 0) {
4883 mlog_errno(status);
4884 goto leave;
4885 }
4886
4887 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Joel Beckercbee7e12009-02-13 03:34:15 -08004888 mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
4889 num_bits, bit_off,
4890 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Joel Beckercc79d8c2009-02-13 03:24:43 -08004891 status = ocfs2_insert_extent(handle, et, *logical_offset, block,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004892 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004893 if (status < 0) {
4894 mlog_errno(status);
4895 goto leave;
4896 }
4897
Joel Beckerf99b9b72008-08-20 19:36:33 -07004898 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004899 if (status < 0) {
4900 mlog_errno(status);
4901 goto leave;
4902 }
4903
4904 clusters_to_add -= num_bits;
4905 *logical_offset += num_bits;
4906
4907 if (clusters_to_add) {
4908 mlog(0, "need to alloc once more, wanted = %u\n",
4909 clusters_to_add);
4910 status = -EAGAIN;
4911 reason = RESTART_TRANS;
4912 }
4913
4914leave:
4915 mlog_exit(status);
4916 if (reason_ret)
4917 *reason_ret = reason;
4918 return status;
4919}
4920
Mark Fasheh328d5752007-06-18 10:48:04 -07004921static void ocfs2_make_right_split_rec(struct super_block *sb,
4922 struct ocfs2_extent_rec *split_rec,
4923 u32 cpos,
4924 struct ocfs2_extent_rec *rec)
4925{
4926 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4927 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4928
4929 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4930
4931 split_rec->e_cpos = cpu_to_le32(cpos);
4932 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4933
4934 split_rec->e_blkno = rec->e_blkno;
4935 le64_add_cpu(&split_rec->e_blkno,
4936 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4937
4938 split_rec->e_flags = rec->e_flags;
4939}
4940
Joel Beckerd2311292009-02-13 03:43:22 -08004941static int ocfs2_split_and_insert(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004942 struct ocfs2_extent_tree *et,
Joel Beckerd2311292009-02-13 03:43:22 -08004943 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004944 struct buffer_head **last_eb_bh,
4945 int split_index,
4946 struct ocfs2_extent_rec *orig_split_rec,
4947 struct ocfs2_alloc_context *meta_ac)
4948{
4949 int ret = 0, depth;
4950 unsigned int insert_range, rec_range, do_leftright = 0;
4951 struct ocfs2_extent_rec tmprec;
4952 struct ocfs2_extent_list *rightmost_el;
4953 struct ocfs2_extent_rec rec;
4954 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4955 struct ocfs2_insert_type insert;
4956 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004957
4958leftright:
4959 /*
4960 * Store a copy of the record on the stack - it might move
4961 * around as the tree is manipulated below.
4962 */
4963 rec = path_leaf_el(path)->l_recs[split_index];
4964
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004965 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004966
4967 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4968 if (depth) {
4969 BUG_ON(!(*last_eb_bh));
4970 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4971 rightmost_el = &eb->h_list;
4972 }
4973
4974 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4975 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004976 ret = ocfs2_grow_tree(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08004977 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004978 if (ret) {
4979 mlog_errno(ret);
4980 goto out;
4981 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004982 }
4983
4984 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4985 insert.ins_appending = APPEND_NONE;
4986 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004987 insert.ins_tree_depth = depth;
4988
4989 insert_range = le32_to_cpu(split_rec.e_cpos) +
4990 le16_to_cpu(split_rec.e_leaf_clusters);
4991 rec_range = le32_to_cpu(rec.e_cpos) +
4992 le16_to_cpu(rec.e_leaf_clusters);
4993
4994 if (split_rec.e_cpos == rec.e_cpos) {
4995 insert.ins_split = SPLIT_LEFT;
4996 } else if (insert_range == rec_range) {
4997 insert.ins_split = SPLIT_RIGHT;
4998 } else {
4999 /*
5000 * Left/right split. We fake this as a right split
5001 * first and then make a second pass as a left split.
5002 */
5003 insert.ins_split = SPLIT_RIGHT;
5004
Joel Beckerd2311292009-02-13 03:43:22 -08005005 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5006 &tmprec, insert_range, &rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005007
5008 split_rec = tmprec;
5009
5010 BUG_ON(do_leftright);
5011 do_leftright = 1;
5012 }
5013
Joel Becker3505bec2009-02-13 02:57:58 -08005014 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07005015 if (ret) {
5016 mlog_errno(ret);
5017 goto out;
5018 }
5019
5020 if (do_leftright == 1) {
5021 u32 cpos;
5022 struct ocfs2_extent_list *el;
5023
5024 do_leftright++;
5025 split_rec = *orig_split_rec;
5026
5027 ocfs2_reinit_path(path, 1);
5028
5029 cpos = le32_to_cpu(split_rec.e_cpos);
Joel Beckerfacdb772009-02-12 18:08:48 -08005030 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005031 if (ret) {
5032 mlog_errno(ret);
5033 goto out;
5034 }
5035
5036 el = path_leaf_el(path);
5037 split_index = ocfs2_search_extent_list(el, cpos);
5038 goto leftright;
5039 }
5040out:
5041
5042 return ret;
5043}
5044
Joel Beckerf3868d02009-02-17 19:46:04 -08005045static int ocfs2_replace_extent_rec(handle_t *handle,
5046 struct ocfs2_extent_tree *et,
Tao Ma47be12e2009-01-09 07:32:48 +08005047 struct ocfs2_path *path,
5048 struct ocfs2_extent_list *el,
5049 int split_index,
5050 struct ocfs2_extent_rec *split_rec)
5051{
5052 int ret;
5053
Joel Beckerf3868d02009-02-17 19:46:04 -08005054 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Tao Ma47be12e2009-01-09 07:32:48 +08005055 path_num_items(path) - 1);
5056 if (ret) {
5057 mlog_errno(ret);
5058 goto out;
5059 }
5060
5061 el->l_recs[split_index] = *split_rec;
5062
5063 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5064out:
5065 return ret;
5066}
5067
Mark Fasheh328d5752007-06-18 10:48:04 -07005068/*
Tao Ma555936b2009-08-18 11:22:21 +08005069 * Split part or all of the extent record at split_index in the leaf
5070 * pointed to by path. Merge with the contiguous extent record if needed.
Mark Fasheh328d5752007-06-18 10:48:04 -07005071 *
5072 * Care is taken to handle contiguousness so as to not grow the tree.
5073 *
5074 * meta_ac is not strictly necessary - we only truly need it if growth
5075 * of the tree is required. All other cases will degrade into a less
5076 * optimal tree layout.
5077 *
Tao Mae7d4cb62008-08-18 17:38:44 +08005078 * last_eb_bh should be the rightmost leaf block for any extent
5079 * btree. Since a split may grow the tree or a merge might shrink it,
5080 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07005081 *
5082 * This code is optimized for readability - several passes might be
5083 * made over certain portions of the tree. All of those blocks will
5084 * have been brought into cache (and pinned via the journal), so the
5085 * extra overhead is not expressed in terms of disk reads.
5086 */
Tao Mae2e9f602009-08-18 11:22:34 +08005087int ocfs2_split_extent(handle_t *handle,
5088 struct ocfs2_extent_tree *et,
5089 struct ocfs2_path *path,
5090 int split_index,
5091 struct ocfs2_extent_rec *split_rec,
5092 struct ocfs2_alloc_context *meta_ac,
5093 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005094{
5095 int ret = 0;
5096 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08005097 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07005098 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5099 struct ocfs2_merge_ctxt ctxt;
5100 struct ocfs2_extent_list *rightmost_el;
5101
Mark Fasheh328d5752007-06-18 10:48:04 -07005102 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5103 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5104 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5105 ret = -EIO;
5106 mlog_errno(ret);
5107 goto out;
5108 }
5109
Joel Beckera2970292009-02-13 03:09:54 -08005110 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005111 split_index,
5112 split_rec);
5113
5114 /*
5115 * The core merge / split code wants to know how much room is
Joel Beckera1cf0762009-02-13 03:45:49 -08005116 * left in this allocation tree, so we pass the
Mark Fasheh328d5752007-06-18 10:48:04 -07005117 * rightmost extent list.
5118 */
5119 if (path->p_tree_depth) {
5120 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005121
Joel Becker3d03a302009-02-12 17:49:26 -08005122 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005123 ocfs2_et_get_last_eb_blk(et),
5124 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005125 if (ret) {
5126 mlog_exit(ret);
5127 goto out;
5128 }
5129
5130 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005131 rightmost_el = &eb->h_list;
5132 } else
5133 rightmost_el = path_root_el(path);
5134
Mark Fasheh328d5752007-06-18 10:48:04 -07005135 if (rec->e_cpos == split_rec->e_cpos &&
5136 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5137 ctxt.c_split_covers_rec = 1;
5138 else
5139 ctxt.c_split_covers_rec = 0;
5140
5141 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5142
Mark Fasheh015452b2007-09-12 10:21:22 -07005143 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5144 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5145 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005146
5147 if (ctxt.c_contig_type == CONTIG_NONE) {
5148 if (ctxt.c_split_covers_rec)
Joel Beckerf3868d02009-02-17 19:46:04 -08005149 ret = ocfs2_replace_extent_rec(handle, et, path, el,
Tao Ma47be12e2009-01-09 07:32:48 +08005150 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005151 else
Joel Beckerd2311292009-02-13 03:43:22 -08005152 ret = ocfs2_split_and_insert(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005153 &last_eb_bh, split_index,
5154 split_rec, meta_ac);
5155 if (ret)
5156 mlog_errno(ret);
5157 } else {
Joel Beckerc495dd22009-02-13 02:19:11 -08005158 ret = ocfs2_try_to_merge_extent(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005159 split_index, split_rec,
Joel Beckerc495dd22009-02-13 02:19:11 -08005160 dealloc, &ctxt);
Mark Fasheh328d5752007-06-18 10:48:04 -07005161 if (ret)
5162 mlog_errno(ret);
5163 }
5164
Mark Fasheh328d5752007-06-18 10:48:04 -07005165out:
5166 brelse(last_eb_bh);
5167 return ret;
5168}
5169
5170/*
Tao Ma555936b2009-08-18 11:22:21 +08005171 * Change the flags of the already-existing extent at cpos for len clusters.
5172 *
5173 * new_flags: the flags we want to set.
5174 * clear_flags: the flags we want to clear.
5175 * phys: the new physical offset we want this new extent starts from.
Mark Fasheh328d5752007-06-18 10:48:04 -07005176 *
5177 * If the existing extent is larger than the request, initiate a
5178 * split. An attempt will be made at merging with adjacent extents.
5179 *
5180 * The caller is responsible for passing down meta_ac if we'll need it.
5181 */
Tao Ma1aa75fe2009-08-18 11:28:39 +08005182int ocfs2_change_extent_flag(handle_t *handle,
5183 struct ocfs2_extent_tree *et,
5184 u32 cpos, u32 len, u32 phys,
5185 struct ocfs2_alloc_context *meta_ac,
5186 struct ocfs2_cached_dealloc_ctxt *dealloc,
5187 int new_flags, int clear_flags)
Mark Fasheh328d5752007-06-18 10:48:04 -07005188{
5189 int ret, index;
Tao Ma555936b2009-08-18 11:22:21 +08005190 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5191 u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
Mark Fasheh328d5752007-06-18 10:48:04 -07005192 struct ocfs2_extent_rec split_rec;
5193 struct ocfs2_path *left_path = NULL;
5194 struct ocfs2_extent_list *el;
Tao Ma555936b2009-08-18 11:22:21 +08005195 struct ocfs2_extent_rec *rec;
Mark Fasheh328d5752007-06-18 10:48:04 -07005196
Joel Beckerffdd7a52008-10-17 22:32:01 -07005197 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005198 if (!left_path) {
5199 ret = -ENOMEM;
5200 mlog_errno(ret);
5201 goto out;
5202 }
5203
Joel Beckerfacdb772009-02-12 18:08:48 -08005204 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005205 if (ret) {
5206 mlog_errno(ret);
5207 goto out;
5208 }
5209 el = path_leaf_el(left_path);
5210
5211 index = ocfs2_search_extent_list(el, cpos);
5212 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Tao Ma555936b2009-08-18 11:22:21 +08005213 ocfs2_error(sb,
5214 "Owner %llu has an extent at cpos %u which can no "
Mark Fasheh328d5752007-06-18 10:48:04 -07005215 "longer be found.\n",
Tao Ma555936b2009-08-18 11:22:21 +08005216 (unsigned long long)
5217 ocfs2_metadata_cache_owner(et->et_ci), cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005218 ret = -EROFS;
5219 goto out;
5220 }
5221
Tao Ma555936b2009-08-18 11:22:21 +08005222 ret = -EIO;
5223 rec = &el->l_recs[index];
5224 if (new_flags && (rec->e_flags & new_flags)) {
5225 mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
5226 "extent that already had them",
5227 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5228 new_flags);
5229 goto out;
5230 }
5231
5232 if (clear_flags && !(rec->e_flags & clear_flags)) {
5233 mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
5234 "extent that didn't have them",
5235 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5236 clear_flags);
5237 goto out;
5238 }
5239
Mark Fasheh328d5752007-06-18 10:48:04 -07005240 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5241 split_rec.e_cpos = cpu_to_le32(cpos);
5242 split_rec.e_leaf_clusters = cpu_to_le16(len);
5243 split_rec.e_blkno = cpu_to_le64(start_blkno);
Tao Ma555936b2009-08-18 11:22:21 +08005244 split_rec.e_flags = rec->e_flags;
5245 if (new_flags)
5246 split_rec.e_flags |= new_flags;
5247 if (clear_flags)
5248 split_rec.e_flags &= ~clear_flags;
Mark Fasheh328d5752007-06-18 10:48:04 -07005249
Tao Mae2e9f602009-08-18 11:22:34 +08005250 ret = ocfs2_split_extent(handle, et, left_path,
5251 index, &split_rec, meta_ac,
5252 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005253 if (ret)
5254 mlog_errno(ret);
5255
5256out:
5257 ocfs2_free_path(left_path);
5258 return ret;
Tao Ma555936b2009-08-18 11:22:21 +08005259
5260}
5261
5262/*
5263 * Mark the already-existing extent at cpos as written for len clusters.
5264 * This removes the unwritten extent flag.
5265 *
5266 * If the existing extent is larger than the request, initiate a
5267 * split. An attempt will be made at merging with adjacent extents.
5268 *
5269 * The caller is responsible for passing down meta_ac if we'll need it.
5270 */
5271int ocfs2_mark_extent_written(struct inode *inode,
5272 struct ocfs2_extent_tree *et,
5273 handle_t *handle, u32 cpos, u32 len, u32 phys,
5274 struct ocfs2_alloc_context *meta_ac,
5275 struct ocfs2_cached_dealloc_ctxt *dealloc)
5276{
5277 int ret;
5278
5279 mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
5280 inode->i_ino, cpos, len, phys);
5281
5282 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5283 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5284 "that are being written to, but the feature bit "
5285 "is not set in the super block.",
5286 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5287 ret = -EROFS;
5288 goto out;
5289 }
5290
5291 /*
5292 * XXX: This should be fixed up so that we just re-insert the
5293 * next extent records.
5294 */
5295 ocfs2_et_extent_map_truncate(et, 0);
5296
5297 ret = ocfs2_change_extent_flag(handle, et, cpos,
5298 len, phys, meta_ac, dealloc,
5299 0, OCFS2_EXT_UNWRITTEN);
5300 if (ret)
5301 mlog_errno(ret);
5302
5303out:
5304 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07005305}
5306
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005307static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5308 struct ocfs2_path *path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005309 int index, u32 new_range,
5310 struct ocfs2_alloc_context *meta_ac)
5311{
5312 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005313 struct buffer_head *last_eb_bh = NULL;
5314 struct ocfs2_extent_block *eb;
5315 struct ocfs2_extent_list *rightmost_el, *el;
5316 struct ocfs2_extent_rec split_rec;
5317 struct ocfs2_extent_rec *rec;
5318 struct ocfs2_insert_type insert;
5319
5320 /*
5321 * Setup the record to split before we grow the tree.
5322 */
5323 el = path_leaf_el(path);
5324 rec = &el->l_recs[index];
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005325 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5326 &split_rec, new_range, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005327
5328 depth = path->p_tree_depth;
5329 if (depth > 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08005330 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005331 ocfs2_et_get_last_eb_blk(et),
5332 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005333 if (ret < 0) {
5334 mlog_errno(ret);
5335 goto out;
5336 }
5337
5338 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5339 rightmost_el = &eb->h_list;
5340 } else
5341 rightmost_el = path_leaf_el(path);
5342
Tao Ma811f9332008-08-18 17:38:43 +08005343 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005344 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005345 ret = ocfs2_extend_trans(handle, credits);
5346 if (ret) {
5347 mlog_errno(ret);
5348 goto out;
5349 }
5350
5351 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5352 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08005353 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005354 meta_ac);
5355 if (ret) {
5356 mlog_errno(ret);
5357 goto out;
5358 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005359 }
5360
5361 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5362 insert.ins_appending = APPEND_NONE;
5363 insert.ins_contig = CONTIG_NONE;
5364 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005365 insert.ins_tree_depth = depth;
5366
Joel Becker3505bec2009-02-13 02:57:58 -08005367 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005368 if (ret)
5369 mlog_errno(ret);
5370
5371out:
5372 brelse(last_eb_bh);
5373 return ret;
5374}
5375
Joel Becker043beeb2009-02-13 02:42:30 -08005376static int ocfs2_truncate_rec(handle_t *handle,
5377 struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005378 struct ocfs2_path *path, int index,
5379 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker043beeb2009-02-13 02:42:30 -08005380 u32 cpos, u32 len)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005381{
5382 int ret;
5383 u32 left_cpos, rec_range, trunc_range;
5384 int wants_rotate = 0, is_rightmost_tree_rec = 0;
Joel Becker043beeb2009-02-13 02:42:30 -08005385 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005386 struct ocfs2_path *left_path = NULL;
5387 struct ocfs2_extent_list *el = path_leaf_el(path);
5388 struct ocfs2_extent_rec *rec;
5389 struct ocfs2_extent_block *eb;
5390
5391 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Joel Becker70f18c02009-02-13 02:09:31 -08005392 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005393 if (ret) {
5394 mlog_errno(ret);
5395 goto out;
5396 }
5397
5398 index--;
5399 }
5400
5401 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5402 path->p_tree_depth) {
5403 /*
5404 * Check whether this is the rightmost tree record. If
5405 * we remove all of this record or part of its right
5406 * edge then an update of the record lengths above it
5407 * will be required.
5408 */
5409 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5410 if (eb->h_next_leaf_blk == 0)
5411 is_rightmost_tree_rec = 1;
5412 }
5413
5414 rec = &el->l_recs[index];
5415 if (index == 0 && path->p_tree_depth &&
5416 le32_to_cpu(rec->e_cpos) == cpos) {
5417 /*
5418 * Changing the leftmost offset (via partial or whole
5419 * record truncate) of an interior (or rightmost) path
5420 * means we have to update the subtree that is formed
5421 * by this leaf and the one to it's left.
5422 *
5423 * There are two cases we can skip:
Joel Becker043beeb2009-02-13 02:42:30 -08005424 * 1) Path is the leftmost one in our btree.
Mark Fashehd0c7d702007-07-03 13:27:22 -07005425 * 2) The leaf is rightmost and will be empty after
5426 * we remove the extent record - the rotate code
5427 * knows how to update the newly formed edge.
5428 */
5429
Joel Becker043beeb2009-02-13 02:42:30 -08005430 ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005431 if (ret) {
5432 mlog_errno(ret);
5433 goto out;
5434 }
5435
5436 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005437 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005438 if (!left_path) {
5439 ret = -ENOMEM;
5440 mlog_errno(ret);
5441 goto out;
5442 }
5443
Joel Beckerfacdb772009-02-12 18:08:48 -08005444 ret = ocfs2_find_path(et->et_ci, left_path,
5445 left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005446 if (ret) {
5447 mlog_errno(ret);
5448 goto out;
5449 }
5450 }
5451 }
5452
5453 ret = ocfs2_extend_rotate_transaction(handle, 0,
5454 handle->h_buffer_credits,
5455 path);
5456 if (ret) {
5457 mlog_errno(ret);
5458 goto out;
5459 }
5460
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005461 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005462 if (ret) {
5463 mlog_errno(ret);
5464 goto out;
5465 }
5466
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005467 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005468 if (ret) {
5469 mlog_errno(ret);
5470 goto out;
5471 }
5472
5473 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5474 trunc_range = cpos + len;
5475
5476 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5477 int next_free;
5478
5479 memset(rec, 0, sizeof(*rec));
5480 ocfs2_cleanup_merge(el, index);
5481 wants_rotate = 1;
5482
5483 next_free = le16_to_cpu(el->l_next_free_rec);
5484 if (is_rightmost_tree_rec && next_free > 1) {
5485 /*
5486 * We skip the edge update if this path will
5487 * be deleted by the rotate code.
5488 */
5489 rec = &el->l_recs[next_free - 1];
Joel Beckerd401dc12009-02-13 02:24:10 -08005490 ocfs2_adjust_rightmost_records(handle, et, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005491 rec);
5492 }
5493 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5494 /* Remove leftmost portion of the record. */
5495 le32_add_cpu(&rec->e_cpos, len);
5496 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5497 le16_add_cpu(&rec->e_leaf_clusters, -len);
5498 } else if (rec_range == trunc_range) {
5499 /* Remove rightmost portion of the record */
5500 le16_add_cpu(&rec->e_leaf_clusters, -len);
5501 if (is_rightmost_tree_rec)
Joel Beckerd401dc12009-02-13 02:24:10 -08005502 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005503 } else {
5504 /* Caller should have trapped this. */
Joel Becker043beeb2009-02-13 02:42:30 -08005505 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5506 "(%u, %u)\n",
5507 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005508 le32_to_cpu(rec->e_cpos),
5509 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5510 BUG();
5511 }
5512
5513 if (left_path) {
5514 int subtree_index;
5515
Joel Becker7dc02802009-02-12 19:20:13 -08005516 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Joel Becker4619c732009-02-12 19:02:36 -08005517 ocfs2_complete_edge_insert(handle, left_path, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005518 subtree_index);
5519 }
5520
5521 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5522
Joel Becker70f18c02009-02-13 02:09:31 -08005523 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005524 if (ret) {
5525 mlog_errno(ret);
5526 goto out;
5527 }
5528
5529out:
5530 ocfs2_free_path(left_path);
5531 return ret;
5532}
5533
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005534int ocfs2_remove_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005535 struct ocfs2_extent_tree *et,
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005536 u32 cpos, u32 len,
Mark Fasheh063c4562007-07-03 13:34:11 -07005537 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005538 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005539{
5540 int ret, index;
5541 u32 rec_range, trunc_range;
5542 struct ocfs2_extent_rec *rec;
5543 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005544 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005545
Joel Becker4c911ee2009-02-13 02:50:12 -08005546 /*
5547 * XXX: Why are we truncating to 0 instead of wherever this
5548 * affects us?
5549 */
5550 ocfs2_et_extent_map_truncate(et, 0);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005551
Joel Beckerffdd7a52008-10-17 22:32:01 -07005552 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005553 if (!path) {
5554 ret = -ENOMEM;
5555 mlog_errno(ret);
5556 goto out;
5557 }
5558
Joel Beckerfacdb772009-02-12 18:08:48 -08005559 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005560 if (ret) {
5561 mlog_errno(ret);
5562 goto out;
5563 }
5564
5565 el = path_leaf_el(path);
5566 index = ocfs2_search_extent_list(el, cpos);
5567 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005568 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5569 "Owner %llu has an extent at cpos %u which can no "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005570 "longer be found.\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005571 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5572 cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005573 ret = -EROFS;
5574 goto out;
5575 }
5576
5577 /*
5578 * We have 3 cases of extent removal:
5579 * 1) Range covers the entire extent rec
5580 * 2) Range begins or ends on one edge of the extent rec
5581 * 3) Range is in the middle of the extent rec (no shared edges)
5582 *
5583 * For case 1 we remove the extent rec and left rotate to
5584 * fill the hole.
5585 *
5586 * For case 2 we just shrink the existing extent rec, with a
5587 * tree update if the shrinking edge is also the edge of an
5588 * extent block.
5589 *
5590 * For case 3 we do a right split to turn the extent rec into
5591 * something case 2 can handle.
5592 */
5593 rec = &el->l_recs[index];
5594 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5595 trunc_range = cpos + len;
5596
5597 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5598
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005599 mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005600 "(cpos %u, len %u)\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005601 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5602 cpos, len, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005603 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5604
5605 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
Joel Becker043beeb2009-02-13 02:42:30 -08005606 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5607 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005608 if (ret) {
5609 mlog_errno(ret);
5610 goto out;
5611 }
5612 } else {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005613 ret = ocfs2_split_tree(handle, et, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005614 trunc_range, meta_ac);
5615 if (ret) {
5616 mlog_errno(ret);
5617 goto out;
5618 }
5619
5620 /*
5621 * The split could have manipulated the tree enough to
5622 * move the record location, so we have to look for it again.
5623 */
5624 ocfs2_reinit_path(path, 1);
5625
Joel Beckerfacdb772009-02-12 18:08:48 -08005626 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005627 if (ret) {
5628 mlog_errno(ret);
5629 goto out;
5630 }
5631
5632 el = path_leaf_el(path);
5633 index = ocfs2_search_extent_list(el, cpos);
5634 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005635 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5636 "Owner %llu: split at cpos %u lost record.",
5637 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005638 cpos);
5639 ret = -EROFS;
5640 goto out;
5641 }
5642
5643 /*
5644 * Double check our values here. If anything is fishy,
5645 * it's easier to catch it at the top level.
5646 */
5647 rec = &el->l_recs[index];
5648 rec_range = le32_to_cpu(rec->e_cpos) +
5649 ocfs2_rec_clusters(el, rec);
5650 if (rec_range != trunc_range) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005651 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5652 "Owner %llu: error after split at cpos %u"
Mark Fashehd0c7d702007-07-03 13:27:22 -07005653 "trunc len %u, existing record is (%u,%u)",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005654 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005655 cpos, len, le32_to_cpu(rec->e_cpos),
5656 ocfs2_rec_clusters(el, rec));
5657 ret = -EROFS;
5658 goto out;
5659 }
5660
Joel Becker043beeb2009-02-13 02:42:30 -08005661 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5662 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005663 if (ret) {
5664 mlog_errno(ret);
5665 goto out;
5666 }
5667 }
5668
5669out:
5670 ocfs2_free_path(path);
5671 return ret;
5672}
5673
Mark Fashehfecc0112008-11-12 15:16:38 -08005674int ocfs2_remove_btree_range(struct inode *inode,
5675 struct ocfs2_extent_tree *et,
5676 u32 cpos, u32 phys_cpos, u32 len,
5677 struct ocfs2_cached_dealloc_ctxt *dealloc)
5678{
5679 int ret;
5680 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5681 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5682 struct inode *tl_inode = osb->osb_tl_inode;
5683 handle_t *handle;
5684 struct ocfs2_alloc_context *meta_ac = NULL;
5685
5686 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5687 if (ret) {
5688 mlog_errno(ret);
5689 return ret;
5690 }
5691
5692 mutex_lock(&tl_inode->i_mutex);
5693
5694 if (ocfs2_truncate_log_needs_flush(osb)) {
5695 ret = __ocfs2_flush_truncate_log(osb);
5696 if (ret < 0) {
5697 mlog_errno(ret);
5698 goto out;
5699 }
5700 }
5701
Jan Karaa90714c2008-10-09 19:38:40 +02005702 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005703 if (IS_ERR(handle)) {
5704 ret = PTR_ERR(handle);
5705 mlog_errno(ret);
5706 goto out;
5707 }
5708
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005709 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07005710 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005711 if (ret) {
5712 mlog_errno(ret);
5713 goto out;
5714 }
5715
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005716 dquot_free_space_nodirty(inode,
Mark Fashehfd4ef232009-01-29 15:06:21 -08005717 ocfs2_clusters_to_bytes(inode->i_sb, len));
5718
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005719 ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
Mark Fashehfecc0112008-11-12 15:16:38 -08005720 if (ret) {
5721 mlog_errno(ret);
5722 goto out_commit;
5723 }
5724
Joel Becker6136ca52009-02-12 19:32:43 -08005725 ocfs2_et_update_clusters(et, -len);
Mark Fashehfecc0112008-11-12 15:16:38 -08005726
5727 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5728 if (ret) {
5729 mlog_errno(ret);
5730 goto out_commit;
5731 }
5732
5733 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5734 if (ret)
5735 mlog_errno(ret);
5736
5737out_commit:
5738 ocfs2_commit_trans(osb, handle);
5739out:
5740 mutex_unlock(&tl_inode->i_mutex);
5741
5742 if (meta_ac)
5743 ocfs2_free_alloc_context(meta_ac);
5744
5745 return ret;
5746}
5747
Mark Fasheh063c4562007-07-03 13:34:11 -07005748int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005749{
5750 struct buffer_head *tl_bh = osb->osb_tl_bh;
5751 struct ocfs2_dinode *di;
5752 struct ocfs2_truncate_log *tl;
5753
5754 di = (struct ocfs2_dinode *) tl_bh->b_data;
5755 tl = &di->id2.i_dealloc;
5756
5757 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5758 "slot %d, invalid truncate log parameters: used = "
5759 "%u, count = %u\n", osb->slot_num,
5760 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5761 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5762}
5763
5764static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5765 unsigned int new_start)
5766{
5767 unsigned int tail_index;
5768 unsigned int current_tail;
5769
5770 /* No records, nothing to coalesce */
5771 if (!le16_to_cpu(tl->tl_used))
5772 return 0;
5773
5774 tail_index = le16_to_cpu(tl->tl_used) - 1;
5775 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5776 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5777
5778 return current_tail == new_start;
5779}
5780
Mark Fasheh063c4562007-07-03 13:34:11 -07005781int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5782 handle_t *handle,
5783 u64 start_blk,
5784 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005785{
5786 int status, index;
5787 unsigned int start_cluster, tl_count;
5788 struct inode *tl_inode = osb->osb_tl_inode;
5789 struct buffer_head *tl_bh = osb->osb_tl_bh;
5790 struct ocfs2_dinode *di;
5791 struct ocfs2_truncate_log *tl;
5792
Mark Fashehb06970532006-03-03 10:24:33 -08005793 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5794 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005795
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005796 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005797
5798 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5799
5800 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005801
Joel Becker10995aa2008-11-13 14:49:12 -08005802 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5803 * by the underlying call to ocfs2_read_inode_block(), so any
5804 * corruption is a code bug */
5805 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5806
5807 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005808 tl_count = le16_to_cpu(tl->tl_count);
5809 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5810 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005811 "Truncate record count on #%llu invalid "
5812 "wanted %u, actual %u\n",
5813 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005814 ocfs2_truncate_recs_per_inode(osb->sb),
5815 le16_to_cpu(tl->tl_count));
5816
5817 /* Caller should have known to flush before calling us. */
5818 index = le16_to_cpu(tl->tl_used);
5819 if (index >= tl_count) {
5820 status = -ENOSPC;
5821 mlog_errno(status);
5822 goto bail;
5823 }
5824
Joel Becker0cf2f762009-02-12 16:41:25 -08005825 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005826 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005827 if (status < 0) {
5828 mlog_errno(status);
5829 goto bail;
5830 }
5831
5832 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005833 "%llu (index = %d)\n", num_clusters, start_cluster,
5834 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005835
5836 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5837 /*
5838 * Move index back to the record we are coalescing with.
5839 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5840 */
5841 index--;
5842
5843 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5844 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5845 index, le32_to_cpu(tl->tl_recs[index].t_start),
5846 num_clusters);
5847 } else {
5848 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5849 tl->tl_used = cpu_to_le16(index + 1);
5850 }
5851 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5852
5853 status = ocfs2_journal_dirty(handle, tl_bh);
5854 if (status < 0) {
5855 mlog_errno(status);
5856 goto bail;
5857 }
5858
5859bail:
5860 mlog_exit(status);
5861 return status;
5862}
5863
5864static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005865 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005866 struct inode *data_alloc_inode,
5867 struct buffer_head *data_alloc_bh)
5868{
5869 int status = 0;
5870 int i;
5871 unsigned int num_clusters;
5872 u64 start_blk;
5873 struct ocfs2_truncate_rec rec;
5874 struct ocfs2_dinode *di;
5875 struct ocfs2_truncate_log *tl;
5876 struct inode *tl_inode = osb->osb_tl_inode;
5877 struct buffer_head *tl_bh = osb->osb_tl_bh;
5878
5879 mlog_entry_void();
5880
5881 di = (struct ocfs2_dinode *) tl_bh->b_data;
5882 tl = &di->id2.i_dealloc;
5883 i = le16_to_cpu(tl->tl_used) - 1;
5884 while (i >= 0) {
5885 /* Caller has given us at least enough credits to
5886 * update the truncate log dinode */
Joel Becker0cf2f762009-02-12 16:41:25 -08005887 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005888 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005889 if (status < 0) {
5890 mlog_errno(status);
5891 goto bail;
5892 }
5893
5894 tl->tl_used = cpu_to_le16(i);
5895
5896 status = ocfs2_journal_dirty(handle, tl_bh);
5897 if (status < 0) {
5898 mlog_errno(status);
5899 goto bail;
5900 }
5901
5902 /* TODO: Perhaps we can calculate the bulk of the
5903 * credits up front rather than extending like
5904 * this. */
5905 status = ocfs2_extend_trans(handle,
5906 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5907 if (status < 0) {
5908 mlog_errno(status);
5909 goto bail;
5910 }
5911
5912 rec = tl->tl_recs[i];
5913 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5914 le32_to_cpu(rec.t_start));
5915 num_clusters = le32_to_cpu(rec.t_clusters);
5916
5917 /* if start_blk is not set, we ignore the record as
5918 * invalid. */
5919 if (start_blk) {
5920 mlog(0, "free record %d, start = %u, clusters = %u\n",
5921 i, le32_to_cpu(rec.t_start), num_clusters);
5922
5923 status = ocfs2_free_clusters(handle, data_alloc_inode,
5924 data_alloc_bh, start_blk,
5925 num_clusters);
5926 if (status < 0) {
5927 mlog_errno(status);
5928 goto bail;
5929 }
5930 }
5931 i--;
5932 }
5933
5934bail:
5935 mlog_exit(status);
5936 return status;
5937}
5938
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005939/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005940int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005941{
5942 int status;
5943 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005944 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005945 struct inode *tl_inode = osb->osb_tl_inode;
5946 struct inode *data_alloc_inode = NULL;
5947 struct buffer_head *tl_bh = osb->osb_tl_bh;
5948 struct buffer_head *data_alloc_bh = NULL;
5949 struct ocfs2_dinode *di;
5950 struct ocfs2_truncate_log *tl;
5951
5952 mlog_entry_void();
5953
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005954 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005955
5956 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005957
Joel Becker10995aa2008-11-13 14:49:12 -08005958 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5959 * by the underlying call to ocfs2_read_inode_block(), so any
5960 * corruption is a code bug */
5961 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5962
5963 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005964 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005965 mlog(0, "Flush %u records from truncate log #%llu\n",
5966 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005967 if (!num_to_flush) {
5968 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005969 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005970 }
5971
5972 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5973 GLOBAL_BITMAP_SYSTEM_INODE,
5974 OCFS2_INVALID_SLOT);
5975 if (!data_alloc_inode) {
5976 status = -EINVAL;
5977 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005978 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005979 }
5980
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005981 mutex_lock(&data_alloc_inode->i_mutex);
5982
Mark Fashehe63aecb62007-10-18 15:30:42 -07005983 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005984 if (status < 0) {
5985 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005986 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005987 }
5988
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005989 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005990 if (IS_ERR(handle)) {
5991 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005992 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005993 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005994 }
5995
5996 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5997 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005998 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005999 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08006000
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006001 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006002
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006003out_unlock:
6004 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07006005 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08006006
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006007out_mutex:
6008 mutex_unlock(&data_alloc_inode->i_mutex);
6009 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006010
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006011out:
Mark Fashehccd979b2005-12-15 14:31:24 -08006012 mlog_exit(status);
6013 return status;
6014}
6015
6016int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
6017{
6018 int status;
6019 struct inode *tl_inode = osb->osb_tl_inode;
6020
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006021 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006022 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006023 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006024
6025 return status;
6026}
6027
David Howellsc4028952006-11-22 14:57:56 +00006028static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08006029{
6030 int status;
David Howellsc4028952006-11-22 14:57:56 +00006031 struct ocfs2_super *osb =
6032 container_of(work, struct ocfs2_super,
6033 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08006034
6035 mlog_entry_void();
6036
6037 status = ocfs2_flush_truncate_log(osb);
6038 if (status < 0)
6039 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08006040 else
Tiger Yangb89c5422010-01-25 14:11:06 +08006041 ocfs2_init_steal_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08006042
6043 mlog_exit(status);
6044}
6045
6046#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
6047void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
6048 int cancel)
6049{
6050 if (osb->osb_tl_inode) {
6051 /* We want to push off log flushes while truncates are
6052 * still running. */
6053 if (cancel)
6054 cancel_delayed_work(&osb->osb_truncate_log_wq);
6055
6056 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
6057 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
6058 }
6059}
6060
6061static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
6062 int slot_num,
6063 struct inode **tl_inode,
6064 struct buffer_head **tl_bh)
6065{
6066 int status;
6067 struct inode *inode = NULL;
6068 struct buffer_head *bh = NULL;
6069
6070 inode = ocfs2_get_system_file_inode(osb,
6071 TRUNCATE_LOG_SYSTEM_INODE,
6072 slot_num);
6073 if (!inode) {
6074 status = -EINVAL;
6075 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
6076 goto bail;
6077 }
6078
Joel Beckerb657c952008-11-13 14:49:11 -08006079 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006080 if (status < 0) {
6081 iput(inode);
6082 mlog_errno(status);
6083 goto bail;
6084 }
6085
6086 *tl_inode = inode;
6087 *tl_bh = bh;
6088bail:
6089 mlog_exit(status);
6090 return status;
6091}
6092
6093/* called during the 1st stage of node recovery. we stamp a clean
6094 * truncate log and pass back a copy for processing later. if the
6095 * truncate log does not require processing, a *tl_copy is set to
6096 * NULL. */
6097int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6098 int slot_num,
6099 struct ocfs2_dinode **tl_copy)
6100{
6101 int status;
6102 struct inode *tl_inode = NULL;
6103 struct buffer_head *tl_bh = NULL;
6104 struct ocfs2_dinode *di;
6105 struct ocfs2_truncate_log *tl;
6106
6107 *tl_copy = NULL;
6108
6109 mlog(0, "recover truncate log from slot %d\n", slot_num);
6110
6111 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6112 if (status < 0) {
6113 mlog_errno(status);
6114 goto bail;
6115 }
6116
6117 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08006118
Joel Becker10995aa2008-11-13 14:49:12 -08006119 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6120 * validated by the underlying call to ocfs2_read_inode_block(),
6121 * so any corruption is a code bug */
6122 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6123
6124 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08006125 if (le16_to_cpu(tl->tl_used)) {
6126 mlog(0, "We'll have %u logs to recover\n",
6127 le16_to_cpu(tl->tl_used));
6128
6129 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6130 if (!(*tl_copy)) {
6131 status = -ENOMEM;
6132 mlog_errno(status);
6133 goto bail;
6134 }
6135
6136 /* Assuming the write-out below goes well, this copy
6137 * will be passed back to recovery for processing. */
6138 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6139
6140 /* All we need to do to clear the truncate log is set
6141 * tl_used. */
6142 tl->tl_used = 0;
6143
Joel Becker13723d02008-10-17 19:25:01 -07006144 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08006145 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08006146 if (status < 0) {
6147 mlog_errno(status);
6148 goto bail;
6149 }
6150 }
6151
6152bail:
6153 if (tl_inode)
6154 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006155 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006156
6157 if (status < 0 && (*tl_copy)) {
6158 kfree(*tl_copy);
6159 *tl_copy = NULL;
6160 }
6161
6162 mlog_exit(status);
6163 return status;
6164}
6165
6166int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6167 struct ocfs2_dinode *tl_copy)
6168{
6169 int status = 0;
6170 int i;
6171 unsigned int clusters, num_recs, start_cluster;
6172 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006173 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006174 struct inode *tl_inode = osb->osb_tl_inode;
6175 struct ocfs2_truncate_log *tl;
6176
6177 mlog_entry_void();
6178
6179 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6180 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6181 return -EINVAL;
6182 }
6183
6184 tl = &tl_copy->id2.i_dealloc;
6185 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08006186 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006187 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006188
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006189 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006190 for(i = 0; i < num_recs; i++) {
6191 if (ocfs2_truncate_log_needs_flush(osb)) {
6192 status = __ocfs2_flush_truncate_log(osb);
6193 if (status < 0) {
6194 mlog_errno(status);
6195 goto bail_up;
6196 }
6197 }
6198
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006199 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006200 if (IS_ERR(handle)) {
6201 status = PTR_ERR(handle);
6202 mlog_errno(status);
6203 goto bail_up;
6204 }
6205
6206 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6207 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6208 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6209
6210 status = ocfs2_truncate_log_append(osb, handle,
6211 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006212 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006213 if (status < 0) {
6214 mlog_errno(status);
6215 goto bail_up;
6216 }
6217 }
6218
6219bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006220 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006221
6222 mlog_exit(status);
6223 return status;
6224}
6225
6226void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6227{
6228 int status;
6229 struct inode *tl_inode = osb->osb_tl_inode;
6230
6231 mlog_entry_void();
6232
6233 if (tl_inode) {
6234 cancel_delayed_work(&osb->osb_truncate_log_wq);
6235 flush_workqueue(ocfs2_wq);
6236
6237 status = ocfs2_flush_truncate_log(osb);
6238 if (status < 0)
6239 mlog_errno(status);
6240
6241 brelse(osb->osb_tl_bh);
6242 iput(osb->osb_tl_inode);
6243 }
6244
6245 mlog_exit_void();
6246}
6247
6248int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6249{
6250 int status;
6251 struct inode *tl_inode = NULL;
6252 struct buffer_head *tl_bh = NULL;
6253
6254 mlog_entry_void();
6255
6256 status = ocfs2_get_truncate_log_info(osb,
6257 osb->slot_num,
6258 &tl_inode,
6259 &tl_bh);
6260 if (status < 0)
6261 mlog_errno(status);
6262
6263 /* ocfs2_truncate_log_shutdown keys on the existence of
6264 * osb->osb_tl_inode so we don't set any of the osb variables
6265 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006266 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6267 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006268 osb->osb_tl_bh = tl_bh;
6269 osb->osb_tl_inode = tl_inode;
6270
6271 mlog_exit(status);
6272 return status;
6273}
6274
Mark Fasheh2b604352007-06-22 15:45:27 -07006275/*
6276 * Delayed de-allocation of suballocator blocks.
6277 *
6278 * Some sets of block de-allocations might involve multiple suballocator inodes.
6279 *
6280 * The locking for this can get extremely complicated, especially when
6281 * the suballocator inodes to delete from aren't known until deep
6282 * within an unrelated codepath.
6283 *
6284 * ocfs2_extent_block structures are a good example of this - an inode
6285 * btree could have been grown by any number of nodes each allocating
6286 * out of their own suballoc inode.
6287 *
6288 * These structures allow the delay of block de-allocation until a
6289 * later time, when locking of multiple cluster inodes won't cause
6290 * deadlock.
6291 */
6292
6293/*
Tao Ma2891d292008-11-12 08:26:58 +08006294 * Describe a single bit freed from a suballocator. For the block
6295 * suballocators, it represents one block. For the global cluster
6296 * allocator, it represents some clusters and free_bit indicates
6297 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006298 */
6299struct ocfs2_cached_block_free {
6300 struct ocfs2_cached_block_free *free_next;
6301 u64 free_blk;
6302 unsigned int free_bit;
6303};
6304
6305struct ocfs2_per_slot_free_list {
6306 struct ocfs2_per_slot_free_list *f_next_suballocator;
6307 int f_inode_type;
6308 int f_slot;
6309 struct ocfs2_cached_block_free *f_first;
6310};
6311
Tao Ma2891d292008-11-12 08:26:58 +08006312static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6313 int sysfile_type,
6314 int slot,
6315 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006316{
6317 int ret;
6318 u64 bg_blkno;
6319 handle_t *handle;
6320 struct inode *inode;
6321 struct buffer_head *di_bh = NULL;
6322 struct ocfs2_cached_block_free *tmp;
6323
6324 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6325 if (!inode) {
6326 ret = -EINVAL;
6327 mlog_errno(ret);
6328 goto out;
6329 }
6330
6331 mutex_lock(&inode->i_mutex);
6332
Mark Fashehe63aecb62007-10-18 15:30:42 -07006333 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006334 if (ret) {
6335 mlog_errno(ret);
6336 goto out_mutex;
6337 }
6338
6339 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6340 if (IS_ERR(handle)) {
6341 ret = PTR_ERR(handle);
6342 mlog_errno(ret);
6343 goto out_unlock;
6344 }
6345
6346 while (head) {
6347 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6348 head->free_bit);
6349 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6350 head->free_bit, (unsigned long long)head->free_blk);
6351
6352 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6353 head->free_bit, bg_blkno, 1);
6354 if (ret) {
6355 mlog_errno(ret);
6356 goto out_journal;
6357 }
6358
6359 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6360 if (ret) {
6361 mlog_errno(ret);
6362 goto out_journal;
6363 }
6364
6365 tmp = head;
6366 head = head->free_next;
6367 kfree(tmp);
6368 }
6369
6370out_journal:
6371 ocfs2_commit_trans(osb, handle);
6372
6373out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006374 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006375 brelse(di_bh);
6376out_mutex:
6377 mutex_unlock(&inode->i_mutex);
6378 iput(inode);
6379out:
6380 while(head) {
6381 /* Premature exit may have left some dangling items. */
6382 tmp = head;
6383 head = head->free_next;
6384 kfree(tmp);
6385 }
6386
6387 return ret;
6388}
6389
Tao Ma2891d292008-11-12 08:26:58 +08006390int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6391 u64 blkno, unsigned int bit)
6392{
6393 int ret = 0;
6394 struct ocfs2_cached_block_free *item;
6395
6396 item = kmalloc(sizeof(*item), GFP_NOFS);
6397 if (item == NULL) {
6398 ret = -ENOMEM;
6399 mlog_errno(ret);
6400 return ret;
6401 }
6402
6403 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6404 bit, (unsigned long long)blkno);
6405
6406 item->free_blk = blkno;
6407 item->free_bit = bit;
6408 item->free_next = ctxt->c_global_allocator;
6409
6410 ctxt->c_global_allocator = item;
6411 return ret;
6412}
6413
6414static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6415 struct ocfs2_cached_block_free *head)
6416{
6417 struct ocfs2_cached_block_free *tmp;
6418 struct inode *tl_inode = osb->osb_tl_inode;
6419 handle_t *handle;
6420 int ret = 0;
6421
6422 mutex_lock(&tl_inode->i_mutex);
6423
6424 while (head) {
6425 if (ocfs2_truncate_log_needs_flush(osb)) {
6426 ret = __ocfs2_flush_truncate_log(osb);
6427 if (ret < 0) {
6428 mlog_errno(ret);
6429 break;
6430 }
6431 }
6432
6433 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6434 if (IS_ERR(handle)) {
6435 ret = PTR_ERR(handle);
6436 mlog_errno(ret);
6437 break;
6438 }
6439
6440 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6441 head->free_bit);
6442
6443 ocfs2_commit_trans(osb, handle);
6444 tmp = head;
6445 head = head->free_next;
6446 kfree(tmp);
6447
6448 if (ret < 0) {
6449 mlog_errno(ret);
6450 break;
6451 }
6452 }
6453
6454 mutex_unlock(&tl_inode->i_mutex);
6455
6456 while (head) {
6457 /* Premature exit may have left some dangling items. */
6458 tmp = head;
6459 head = head->free_next;
6460 kfree(tmp);
6461 }
6462
6463 return ret;
6464}
6465
Mark Fasheh2b604352007-06-22 15:45:27 -07006466int ocfs2_run_deallocs(struct ocfs2_super *osb,
6467 struct ocfs2_cached_dealloc_ctxt *ctxt)
6468{
6469 int ret = 0, ret2;
6470 struct ocfs2_per_slot_free_list *fl;
6471
6472 if (!ctxt)
6473 return 0;
6474
6475 while (ctxt->c_first_suballocator) {
6476 fl = ctxt->c_first_suballocator;
6477
6478 if (fl->f_first) {
6479 mlog(0, "Free items: (type %u, slot %d)\n",
6480 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006481 ret2 = ocfs2_free_cached_blocks(osb,
6482 fl->f_inode_type,
6483 fl->f_slot,
6484 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006485 if (ret2)
6486 mlog_errno(ret2);
6487 if (!ret)
6488 ret = ret2;
6489 }
6490
6491 ctxt->c_first_suballocator = fl->f_next_suballocator;
6492 kfree(fl);
6493 }
6494
Tao Ma2891d292008-11-12 08:26:58 +08006495 if (ctxt->c_global_allocator) {
6496 ret2 = ocfs2_free_cached_clusters(osb,
6497 ctxt->c_global_allocator);
6498 if (ret2)
6499 mlog_errno(ret2);
6500 if (!ret)
6501 ret = ret2;
6502
6503 ctxt->c_global_allocator = NULL;
6504 }
6505
Mark Fasheh2b604352007-06-22 15:45:27 -07006506 return ret;
6507}
6508
6509static struct ocfs2_per_slot_free_list *
6510ocfs2_find_per_slot_free_list(int type,
6511 int slot,
6512 struct ocfs2_cached_dealloc_ctxt *ctxt)
6513{
6514 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6515
6516 while (fl) {
6517 if (fl->f_inode_type == type && fl->f_slot == slot)
6518 return fl;
6519
6520 fl = fl->f_next_suballocator;
6521 }
6522
6523 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6524 if (fl) {
6525 fl->f_inode_type = type;
6526 fl->f_slot = slot;
6527 fl->f_first = NULL;
6528 fl->f_next_suballocator = ctxt->c_first_suballocator;
6529
6530 ctxt->c_first_suballocator = fl;
6531 }
6532 return fl;
6533}
6534
Tao Ma1823cb02009-08-18 11:24:49 +08006535int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6536 int type, int slot, u64 blkno,
6537 unsigned int bit)
Mark Fasheh2b604352007-06-22 15:45:27 -07006538{
6539 int ret;
6540 struct ocfs2_per_slot_free_list *fl;
6541 struct ocfs2_cached_block_free *item;
6542
6543 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6544 if (fl == NULL) {
6545 ret = -ENOMEM;
6546 mlog_errno(ret);
6547 goto out;
6548 }
6549
6550 item = kmalloc(sizeof(*item), GFP_NOFS);
6551 if (item == NULL) {
6552 ret = -ENOMEM;
6553 mlog_errno(ret);
6554 goto out;
6555 }
6556
6557 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6558 type, slot, bit, (unsigned long long)blkno);
6559
6560 item->free_blk = blkno;
6561 item->free_bit = bit;
6562 item->free_next = fl->f_first;
6563
6564 fl->f_first = item;
6565
6566 ret = 0;
6567out:
6568 return ret;
6569}
6570
Mark Fasheh59a5e412007-06-22 15:52:36 -07006571static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6572 struct ocfs2_extent_block *eb)
6573{
6574 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6575 le16_to_cpu(eb->h_suballoc_slot),
6576 le64_to_cpu(eb->h_blkno),
6577 le16_to_cpu(eb->h_suballoc_bit));
6578}
6579
Mark Fashehccd979b2005-12-15 14:31:24 -08006580/* This function will figure out whether the currently last extent
6581 * block will be deleted, and if it will, what the new last extent
6582 * block will be so we can update his h_next_leaf_blk field, as well
6583 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006584static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006585 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006586 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006587 struct buffer_head **new_last_eb)
6588{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006589 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006590 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006591 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006592 struct ocfs2_extent_block *eb;
6593 struct ocfs2_extent_list *el;
6594 struct buffer_head *bh = NULL;
6595
6596 *new_last_eb = NULL;
6597
Mark Fashehccd979b2005-12-15 14:31:24 -08006598 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006599 if (!path->p_tree_depth)
6600 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006601
6602 /* trunc to zero special case - this makes tree_depth = 0
6603 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006604 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006605 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006606
Mark Fashehdcd05382007-01-16 11:32:23 -08006607 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006608 BUG_ON(!el->l_next_free_rec);
6609
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006610 /*
6611 * Make sure that this extent list will actually be empty
6612 * after we clear away the data. We can shortcut out if
6613 * there's more than one non-empty extent in the
6614 * list. Otherwise, a check of the remaining extent is
6615 * necessary.
6616 */
6617 next_free = le16_to_cpu(el->l_next_free_rec);
6618 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006619 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006620 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006621 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006622
6623 /* We may have a valid extent in index 1, check it. */
6624 if (next_free == 2)
6625 rec = &el->l_recs[1];
6626
6627 /*
6628 * Fall through - no more nonempty extents, so we want
6629 * to delete this leaf.
6630 */
6631 } else {
6632 if (next_free > 1)
6633 goto out;
6634
6635 rec = &el->l_recs[0];
6636 }
6637
6638 if (rec) {
6639 /*
6640 * Check it we'll only be trimming off the end of this
6641 * cluster.
6642 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006643 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006644 goto out;
6645 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006646
Mark Fashehdcd05382007-01-16 11:32:23 -08006647 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6648 if (ret) {
6649 mlog_errno(ret);
6650 goto out;
6651 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006652
Joel Beckerfacdb772009-02-12 18:08:48 -08006653 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08006654 if (ret) {
6655 mlog_errno(ret);
6656 goto out;
6657 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006658
Mark Fashehdcd05382007-01-16 11:32:23 -08006659 eb = (struct ocfs2_extent_block *) bh->b_data;
6660 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006661
6662 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6663 * Any corruption is a code bug. */
6664 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006665
6666 *new_last_eb = bh;
6667 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006668 mlog(0, "returning block %llu, (cpos: %u)\n",
6669 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6670out:
6671 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006672
Mark Fashehdcd05382007-01-16 11:32:23 -08006673 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006674}
6675
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006676/*
6677 * Trim some clusters off the rightmost edge of a tree. Only called
6678 * during truncate.
6679 *
6680 * The caller needs to:
6681 * - start journaling of each path component.
6682 * - compute and fully set up any new last ext block
6683 */
6684static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6685 handle_t *handle, struct ocfs2_truncate_context *tc,
Tao Mabcbbb242009-08-18 11:29:12 +08006686 u32 clusters_to_del, u64 *delete_start, u8 *flags)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006687{
6688 int ret, i, index = path->p_tree_depth;
6689 u32 new_edge = 0;
6690 u64 deleted_eb = 0;
6691 struct buffer_head *bh;
6692 struct ocfs2_extent_list *el;
6693 struct ocfs2_extent_rec *rec;
6694
6695 *delete_start = 0;
Tao Mabcbbb242009-08-18 11:29:12 +08006696 *flags = 0;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006697
6698 while (index >= 0) {
6699 bh = path->p_node[index].bh;
6700 el = path->p_node[index].el;
6701
6702 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6703 index, (unsigned long long)bh->b_blocknr);
6704
6705 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6706
6707 if (index !=
6708 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6709 ocfs2_error(inode->i_sb,
6710 "Inode %lu has invalid ext. block %llu",
6711 inode->i_ino,
6712 (unsigned long long)bh->b_blocknr);
6713 ret = -EROFS;
6714 goto out;
6715 }
6716
6717find_tail_record:
6718 i = le16_to_cpu(el->l_next_free_rec) - 1;
6719 rec = &el->l_recs[i];
6720
6721 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6722 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006723 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006724 (unsigned long long)le64_to_cpu(rec->e_blkno),
6725 le16_to_cpu(el->l_next_free_rec));
6726
Mark Fashehe48edee2007-03-07 16:46:57 -08006727 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006728
6729 if (le16_to_cpu(el->l_tree_depth) == 0) {
6730 /*
6731 * If the leaf block contains a single empty
6732 * extent and no records, we can just remove
6733 * the block.
6734 */
6735 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6736 memset(rec, 0,
6737 sizeof(struct ocfs2_extent_rec));
6738 el->l_next_free_rec = cpu_to_le16(0);
6739
6740 goto delete;
6741 }
6742
6743 /*
6744 * Remove any empty extents by shifting things
6745 * left. That should make life much easier on
6746 * the code below. This condition is rare
6747 * enough that we shouldn't see a performance
6748 * hit.
6749 */
6750 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6751 le16_add_cpu(&el->l_next_free_rec, -1);
6752
6753 for(i = 0;
6754 i < le16_to_cpu(el->l_next_free_rec); i++)
6755 el->l_recs[i] = el->l_recs[i + 1];
6756
6757 memset(&el->l_recs[i], 0,
6758 sizeof(struct ocfs2_extent_rec));
6759
6760 /*
6761 * We've modified our extent list. The
6762 * simplest way to handle this change
6763 * is to being the search from the
6764 * start again.
6765 */
6766 goto find_tail_record;
6767 }
6768
Mark Fashehe48edee2007-03-07 16:46:57 -08006769 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006770
6771 /*
6772 * We'll use "new_edge" on our way back up the
6773 * tree to know what our rightmost cpos is.
6774 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006775 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006776 new_edge += le32_to_cpu(rec->e_cpos);
6777
6778 /*
6779 * The caller will use this to delete data blocks.
6780 */
6781 *delete_start = le64_to_cpu(rec->e_blkno)
6782 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006783 le16_to_cpu(rec->e_leaf_clusters));
Tao Mabcbbb242009-08-18 11:29:12 +08006784 *flags = rec->e_flags;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006785
6786 /*
6787 * If it's now empty, remove this record.
6788 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006789 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006790 memset(rec, 0,
6791 sizeof(struct ocfs2_extent_rec));
6792 le16_add_cpu(&el->l_next_free_rec, -1);
6793 }
6794 } else {
6795 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6796 memset(rec, 0,
6797 sizeof(struct ocfs2_extent_rec));
6798 le16_add_cpu(&el->l_next_free_rec, -1);
6799
6800 goto delete;
6801 }
6802
6803 /* Can this actually happen? */
6804 if (le16_to_cpu(el->l_next_free_rec) == 0)
6805 goto delete;
6806
6807 /*
6808 * We never actually deleted any clusters
6809 * because our leaf was empty. There's no
6810 * reason to adjust the rightmost edge then.
6811 */
6812 if (new_edge == 0)
6813 goto delete;
6814
Mark Fashehe48edee2007-03-07 16:46:57 -08006815 rec->e_int_clusters = cpu_to_le32(new_edge);
6816 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006817 -le32_to_cpu(rec->e_cpos));
6818
6819 /*
6820 * A deleted child record should have been
6821 * caught above.
6822 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006823 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006824 }
6825
6826delete:
6827 ret = ocfs2_journal_dirty(handle, bh);
6828 if (ret) {
6829 mlog_errno(ret);
6830 goto out;
6831 }
6832
6833 mlog(0, "extent list container %llu, after: record %d: "
6834 "(%u, %u, %llu), next = %u.\n",
6835 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006836 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006837 (unsigned long long)le64_to_cpu(rec->e_blkno),
6838 le16_to_cpu(el->l_next_free_rec));
6839
6840 /*
6841 * We must be careful to only attempt delete of an
6842 * extent block (and not the root inode block).
6843 */
6844 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6845 struct ocfs2_extent_block *eb =
6846 (struct ocfs2_extent_block *)bh->b_data;
6847
6848 /*
6849 * Save this for use when processing the
6850 * parent block.
6851 */
6852 deleted_eb = le64_to_cpu(eb->h_blkno);
6853
6854 mlog(0, "deleting this extent block.\n");
6855
Joel Becker8cb471e2009-02-10 20:00:41 -08006856 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006857
Mark Fashehe48edee2007-03-07 16:46:57 -08006858 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006859 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6860 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6861
Mark Fasheh59a5e412007-06-22 15:52:36 -07006862 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6863 /* An error here is not fatal. */
6864 if (ret < 0)
6865 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006866 } else {
6867 deleted_eb = 0;
6868 }
6869
6870 index--;
6871 }
6872
6873 ret = 0;
6874out:
6875 return ret;
6876}
6877
Mark Fashehccd979b2005-12-15 14:31:24 -08006878static int ocfs2_do_truncate(struct ocfs2_super *osb,
6879 unsigned int clusters_to_del,
6880 struct inode *inode,
6881 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006882 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006883 struct ocfs2_truncate_context *tc,
Tao Mabcbbb242009-08-18 11:29:12 +08006884 struct ocfs2_path *path,
6885 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08006886{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006887 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006888 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006889 struct ocfs2_extent_block *last_eb = NULL;
6890 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006891 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006892 u64 delete_blk = 0;
Tao Mabcbbb242009-08-18 11:29:12 +08006893 u8 rec_flags;
Mark Fashehccd979b2005-12-15 14:31:24 -08006894
6895 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6896
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006897 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006898 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006899 if (status < 0) {
6900 mlog_errno(status);
6901 goto bail;
6902 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006903
Mark Fashehdcd05382007-01-16 11:32:23 -08006904 /*
6905 * Each component will be touched, so we might as well journal
6906 * here to avoid having to handle errors later.
6907 */
Joel Becker0cf2f762009-02-12 16:41:25 -08006908 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006909 if (status < 0) {
6910 mlog_errno(status);
6911 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006912 }
6913
6914 if (last_eb_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08006915 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07006916 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08006917 if (status < 0) {
6918 mlog_errno(status);
6919 goto bail;
6920 }
6921
6922 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6923 }
6924
6925 el = &(fe->id2.i_list);
6926
6927 /*
6928 * Lower levels depend on this never happening, but it's best
6929 * to check it up here before changing the tree.
6930 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006931 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006932 ocfs2_error(inode->i_sb,
6933 "Inode %lu has an empty extent record, depth %u\n",
6934 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006935 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006936 goto bail;
6937 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006938
Christoph Hellwig5dd40562010-03-03 09:05:00 -05006939 dquot_free_space_nodirty(inode,
Jan Karaa90714c2008-10-09 19:38:40 +02006940 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006941 spin_lock(&OCFS2_I(inode)->ip_lock);
6942 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6943 clusters_to_del;
6944 spin_unlock(&OCFS2_I(inode)->ip_lock);
6945 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006946 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006947
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006948 status = ocfs2_trim_tree(inode, path, handle, tc,
Tao Mabcbbb242009-08-18 11:29:12 +08006949 clusters_to_del, &delete_blk, &rec_flags);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006950 if (status) {
6951 mlog_errno(status);
6952 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006953 }
6954
Mark Fashehdcd05382007-01-16 11:32:23 -08006955 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006956 /* trunc to zero is a special case. */
6957 el->l_tree_depth = 0;
6958 fe->i_last_eb_blk = 0;
6959 } else if (last_eb)
6960 fe->i_last_eb_blk = last_eb->h_blkno;
6961
6962 status = ocfs2_journal_dirty(handle, fe_bh);
6963 if (status < 0) {
6964 mlog_errno(status);
6965 goto bail;
6966 }
6967
6968 if (last_eb) {
6969 /* If there will be a new last extent block, then by
6970 * definition, there cannot be any leaves to the right of
6971 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006972 last_eb->h_next_leaf_blk = 0;
6973 status = ocfs2_journal_dirty(handle, last_eb_bh);
6974 if (status < 0) {
6975 mlog_errno(status);
6976 goto bail;
6977 }
6978 }
6979
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006980 if (delete_blk) {
Tao Mabcbbb242009-08-18 11:29:12 +08006981 if (rec_flags & OCFS2_EXT_REFCOUNTED)
6982 status = ocfs2_decrease_refcount(inode, handle,
6983 ocfs2_blocks_to_clusters(osb->sb,
6984 delete_blk),
6985 clusters_to_del, meta_ac,
Tao Ma6ae23c52009-08-18 11:30:55 +08006986 &tc->tc_dealloc, 1);
Tao Mabcbbb242009-08-18 11:29:12 +08006987 else
6988 status = ocfs2_truncate_log_append(osb, handle,
6989 delete_blk,
6990 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006991 if (status < 0) {
6992 mlog_errno(status);
6993 goto bail;
6994 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006995 }
6996 status = 0;
6997bail:
Tao Ma60e2ec42009-08-12 14:42:47 +08006998 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006999 mlog_exit(status);
7000 return status;
7001}
7002
Joel Becker2b4e30f2008-09-03 20:03:41 -07007003static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08007004{
7005 set_buffer_uptodate(bh);
7006 mark_buffer_dirty(bh);
7007 return 0;
7008}
7009
Tao Ma6f70fa52009-08-25 08:05:12 +08007010void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
7011 unsigned int from, unsigned int to,
7012 struct page *page, int zero, u64 *phys)
Mark Fasheh1d410a62007-09-07 14:20:45 -07007013{
7014 int ret, partial = 0;
7015
7016 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
7017 if (ret)
7018 mlog_errno(ret);
7019
7020 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08007021 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07007022
7023 /*
7024 * Need to set the buffers we zero'd into uptodate
7025 * here if they aren't - ocfs2_map_page_blocks()
7026 * might've skipped some
7027 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07007028 ret = walk_page_buffers(handle, page_buffers(page),
7029 from, to, &partial,
7030 ocfs2_zero_func);
7031 if (ret < 0)
7032 mlog_errno(ret);
7033 else if (ocfs2_should_order_data(inode)) {
7034 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07007035 if (ret < 0)
7036 mlog_errno(ret);
7037 }
7038
7039 if (!partial)
7040 SetPageUptodate(page);
7041
7042 flush_dcache_page(page);
7043}
7044
Mark Fasheh35edec12007-07-06 14:41:18 -07007045static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
7046 loff_t end, struct page **pages,
7047 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08007048{
Mark Fasheh1d410a62007-09-07 14:20:45 -07007049 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08007050 struct page *page;
7051 unsigned int from, to = PAGE_CACHE_SIZE;
7052 struct super_block *sb = inode->i_sb;
7053
7054 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
7055
7056 if (numpages == 0)
7057 goto out;
7058
Mark Fasheh35edec12007-07-06 14:41:18 -07007059 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08007060 for(i = 0; i < numpages; i++) {
7061 page = pages[i];
7062
Mark Fasheh35edec12007-07-06 14:41:18 -07007063 from = start & (PAGE_CACHE_SIZE - 1);
7064 if ((end >> PAGE_CACHE_SHIFT) == page->index)
7065 to = end & (PAGE_CACHE_SIZE - 1);
7066
Mark Fasheh60b11392007-02-16 11:46:50 -08007067 BUG_ON(from > PAGE_CACHE_SIZE);
7068 BUG_ON(to > PAGE_CACHE_SIZE);
7069
Mark Fasheh1d410a62007-09-07 14:20:45 -07007070 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
7071 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08007072
Mark Fasheh35edec12007-07-06 14:41:18 -07007073 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08007074 }
7075out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07007076 if (pages)
7077 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08007078}
7079
Tao Ma6f70fa52009-08-25 08:05:12 +08007080int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
7081 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08007082{
Mark Fasheh1d410a62007-09-07 14:20:45 -07007083 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08007084 struct address_space *mapping = inode->i_mapping;
7085 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07007086 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08007087
Mark Fasheh35edec12007-07-06 14:41:18 -07007088 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08007089
Mark Fasheh1d410a62007-09-07 14:20:45 -07007090 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07007091 last_page_bytes = PAGE_ALIGN(end);
7092 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08007093 do {
7094 pages[numpages] = grab_cache_page(mapping, index);
7095 if (!pages[numpages]) {
7096 ret = -ENOMEM;
7097 mlog_errno(ret);
7098 goto out;
7099 }
7100
7101 numpages++;
7102 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07007103 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08007104
7105out:
7106 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07007107 if (pages)
7108 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08007109 numpages = 0;
7110 }
7111
7112 *num = numpages;
7113
7114 return ret;
7115}
7116
Tao Ma6f70fa52009-08-25 08:05:12 +08007117static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
7118 struct page **pages, int *num)
7119{
7120 struct super_block *sb = inode->i_sb;
7121
7122 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
7123 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
7124
7125 return ocfs2_grab_pages(inode, start, end, pages, num);
7126}
7127
Mark Fasheh60b11392007-02-16 11:46:50 -08007128/*
7129 * Zero the area past i_size but still within an allocated
7130 * cluster. This avoids exposing nonzero data on subsequent file
7131 * extends.
7132 *
7133 * We need to call this before i_size is updated on the inode because
7134 * otherwise block_write_full_page() will skip writeout of pages past
7135 * i_size. The new_i_size parameter is passed for this reason.
7136 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007137int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
7138 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08007139{
Mark Fasheh1d410a62007-09-07 14:20:45 -07007140 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08007141 struct page **pages = NULL;
7142 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07007143 unsigned int ext_flags;
7144 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08007145
7146 /*
7147 * File systems which don't support sparse files zero on every
7148 * extend.
7149 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07007150 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08007151 return 0;
7152
Mark Fasheh1d410a62007-09-07 14:20:45 -07007153 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08007154 sizeof(struct page *), GFP_NOFS);
7155 if (pages == NULL) {
7156 ret = -ENOMEM;
7157 mlog_errno(ret);
7158 goto out;
7159 }
7160
Mark Fasheh1d410a62007-09-07 14:20:45 -07007161 if (range_start == range_end)
7162 goto out;
7163
7164 ret = ocfs2_extent_map_get_blocks(inode,
7165 range_start >> sb->s_blocksize_bits,
7166 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08007167 if (ret) {
7168 mlog_errno(ret);
7169 goto out;
7170 }
7171
Mark Fasheh1d410a62007-09-07 14:20:45 -07007172 /*
7173 * Tail is a hole, or is marked unwritten. In either case, we
7174 * can count on read and write to return/push zero's.
7175 */
7176 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08007177 goto out;
7178
Mark Fasheh1d410a62007-09-07 14:20:45 -07007179 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7180 &numpages);
7181 if (ret) {
7182 mlog_errno(ret);
7183 goto out;
7184 }
7185
Mark Fasheh35edec12007-07-06 14:41:18 -07007186 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7187 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08007188
7189 /*
7190 * Initiate writeout of the pages we zero'd here. We don't
7191 * wait on them - the truncate_inode_pages() call later will
7192 * do that for us.
7193 */
Christoph Hellwig2cfd30a2009-09-23 15:04:02 +02007194 ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
7195 range_end - 1);
Mark Fasheh60b11392007-02-16 11:46:50 -08007196 if (ret)
7197 mlog_errno(ret);
7198
7199out:
7200 if (pages)
7201 kfree(pages);
7202
7203 return ret;
7204}
7205
Tiger Yangfdd77702008-08-18 17:08:55 +08007206static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7207 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007208{
7209 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08007210 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007211
Tiger Yangfdd77702008-08-18 17:08:55 +08007212 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7213 memset(&di->id2, 0, blocksize -
7214 offsetof(struct ocfs2_dinode, id2) -
7215 xattrsize);
7216 else
7217 memset(&di->id2, 0, blocksize -
7218 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007219}
7220
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007221void ocfs2_dinode_new_extent_list(struct inode *inode,
7222 struct ocfs2_dinode *di)
7223{
Tiger Yangfdd77702008-08-18 17:08:55 +08007224 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007225 di->id2.i_list.l_tree_depth = 0;
7226 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08007227 di->id2.i_list.l_count = cpu_to_le16(
7228 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007229}
7230
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007231void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7232{
7233 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7234 struct ocfs2_inline_data *idata = &di->id2.i_data;
7235
7236 spin_lock(&oi->ip_lock);
7237 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7238 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7239 spin_unlock(&oi->ip_lock);
7240
7241 /*
7242 * We clear the entire i_data structure here so that all
7243 * fields can be properly initialized.
7244 */
Tiger Yangfdd77702008-08-18 17:08:55 +08007245 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007246
Tiger Yangfdd77702008-08-18 17:08:55 +08007247 idata->id_count = cpu_to_le16(
7248 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007249}
7250
7251int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7252 struct buffer_head *di_bh)
7253{
7254 int ret, i, has_data, num_pages = 0;
7255 handle_t *handle;
7256 u64 uninitialized_var(block);
7257 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7258 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7259 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007260 struct ocfs2_alloc_context *data_ac = NULL;
7261 struct page **pages = NULL;
7262 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07007263 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02007264 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007265
7266 has_data = i_size_read(inode) ? 1 : 0;
7267
7268 if (has_data) {
7269 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7270 sizeof(struct page *), GFP_NOFS);
7271 if (pages == NULL) {
7272 ret = -ENOMEM;
7273 mlog_errno(ret);
7274 goto out;
7275 }
7276
7277 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7278 if (ret) {
7279 mlog_errno(ret);
7280 goto out;
7281 }
7282 }
7283
Jan Karaa90714c2008-10-09 19:38:40 +02007284 handle = ocfs2_start_trans(osb,
7285 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007286 if (IS_ERR(handle)) {
7287 ret = PTR_ERR(handle);
7288 mlog_errno(ret);
7289 goto out_unlock;
7290 }
7291
Joel Becker0cf2f762009-02-12 16:41:25 -08007292 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007293 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007294 if (ret) {
7295 mlog_errno(ret);
7296 goto out_commit;
7297 }
7298
7299 if (has_data) {
7300 u32 bit_off, num;
7301 unsigned int page_end;
7302 u64 phys;
7303
Christoph Hellwig5dd40562010-03-03 09:05:00 -05007304 ret = dquot_alloc_space_nodirty(inode,
7305 ocfs2_clusters_to_bytes(osb->sb, 1));
7306 if (ret)
Jan Karaa90714c2008-10-09 19:38:40 +02007307 goto out_commit;
Jan Karaa90714c2008-10-09 19:38:40 +02007308 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)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05007383 dquot_free_space_nodirty(inode,
Jan Karaa90714c2008-10-09 19:38:40 +02007384 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}