blob: af2d1bd00d0a229b49e937b078ba5f00bb79f048 [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;
Joel Becker2b6cb572010-03-26 10:09:15 +08001009 u64 suballoc_loc, 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) {
Joel Becker1ed9b772010-05-06 13:59:06 +08001018 status = ocfs2_claim_metadata(handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001019 meta_ac,
1020 wanted - count,
Joel Becker2b6cb572010-03-26 10:09:15 +08001021 &suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08001022 &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);
Joel Becker2b6cb572010-03-26 10:09:15 +08001055 eb->h_suballoc_loc = cpu_to_le64(suballoc_loc);
Mark Fashehccd979b2005-12-15 14:31:24 -08001056 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1057 eb->h_list.l_count =
1058 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1059
1060 suballoc_bit_start++;
1061 first_blkno++;
1062
1063 /* We'll also be dirtied by the caller, so
1064 * this isn't absolutely necessary. */
Joel Beckerec20cec2010-03-19 14:13:52 -07001065 ocfs2_journal_dirty(handle, bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001066 }
1067
1068 count += num_got;
1069 }
1070
1071 status = 0;
1072bail:
1073 if (status < 0) {
1074 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001075 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001076 bhs[i] = NULL;
1077 }
1078 }
1079 mlog_exit(status);
1080 return status;
1081}
1082
1083/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001084 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1085 *
1086 * Returns the sum of the rightmost extent rec logical offset and
1087 * cluster count.
1088 *
1089 * ocfs2_add_branch() uses this to determine what logical cluster
1090 * value should be populated into the leftmost new branch records.
1091 *
1092 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1093 * value for the new topmost tree record.
1094 */
1095static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1096{
1097 int i;
1098
1099 i = le16_to_cpu(el->l_next_free_rec) - 1;
1100
1101 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001102 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08001103}
1104
1105/*
Tao Ma6b791bc2009-06-12 14:18:36 +08001106 * Change range of the branches in the right most path according to the leaf
1107 * extent block's rightmost record.
1108 */
1109static int ocfs2_adjust_rightmost_branch(handle_t *handle,
Tao Ma6b791bc2009-06-12 14:18:36 +08001110 struct ocfs2_extent_tree *et)
1111{
1112 int status;
1113 struct ocfs2_path *path = NULL;
1114 struct ocfs2_extent_list *el;
1115 struct ocfs2_extent_rec *rec;
1116
1117 path = ocfs2_new_path_from_et(et);
1118 if (!path) {
1119 status = -ENOMEM;
1120 return status;
1121 }
1122
Joel Beckerfacdb772009-02-12 18:08:48 -08001123 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
Tao Ma6b791bc2009-06-12 14:18:36 +08001124 if (status < 0) {
1125 mlog_errno(status);
1126 goto out;
1127 }
1128
Tao Mac901fb02010-04-26 14:34:57 +08001129 status = ocfs2_extend_trans(handle, path_num_items(path));
Tao Ma6b791bc2009-06-12 14:18:36 +08001130 if (status < 0) {
1131 mlog_errno(status);
1132 goto out;
1133 }
1134
Joel Beckerd401dc12009-02-13 02:24:10 -08001135 status = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma6b791bc2009-06-12 14:18:36 +08001136 if (status < 0) {
1137 mlog_errno(status);
1138 goto out;
1139 }
1140
1141 el = path_leaf_el(path);
1142 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1143
Joel Beckerd401dc12009-02-13 02:24:10 -08001144 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Tao Ma6b791bc2009-06-12 14:18:36 +08001145
1146out:
1147 ocfs2_free_path(path);
1148 return status;
1149}
1150
1151/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001152 * Add an entire tree branch to our inode. eb_bh is the extent block
Joel Beckerd401dc12009-02-13 02:24:10 -08001153 * to start at, if we don't want to start the branch at the root
Mark Fashehccd979b2005-12-15 14:31:24 -08001154 * structure.
1155 *
1156 * last_eb_bh is required as we have to update it's next_leaf pointer
1157 * for the new last extent block.
1158 *
1159 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -08001160 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -08001161 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001162static int ocfs2_add_branch(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001163 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001164 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07001165 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001166 struct ocfs2_alloc_context *meta_ac)
1167{
1168 int status, new_blocks, i;
1169 u64 next_blkno, new_last_eb_blk;
1170 struct buffer_head *bh;
1171 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001172 struct ocfs2_extent_block *eb;
1173 struct ocfs2_extent_list *eb_el;
1174 struct ocfs2_extent_list *el;
Tao Ma6b791bc2009-06-12 14:18:36 +08001175 u32 new_cpos, root_end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001176
1177 mlog_entry_void();
1178
Mark Fasheh328d5752007-06-18 10:48:04 -07001179 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001180
Mark Fashehccd979b2005-12-15 14:31:24 -08001181 if (eb_bh) {
1182 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1183 el = &eb->h_list;
1184 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001185 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001186
1187 /* we never add a branch to a leaf. */
1188 BUG_ON(!el->l_tree_depth);
1189
1190 new_blocks = le16_to_cpu(el->l_tree_depth);
1191
Tao Ma6b791bc2009-06-12 14:18:36 +08001192 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1193 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1194 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1195
1196 /*
1197 * If there is a gap before the root end and the real end
1198 * of the righmost leaf block, we need to remove the gap
1199 * between new_cpos and root_end first so that the tree
1200 * is consistent after we add a new branch(it will start
1201 * from new_cpos).
1202 */
1203 if (root_end > new_cpos) {
1204 mlog(0, "adjust the cluster end from %u to %u\n",
1205 root_end, new_cpos);
Joel Beckerd401dc12009-02-13 02:24:10 -08001206 status = ocfs2_adjust_rightmost_branch(handle, et);
Tao Ma6b791bc2009-06-12 14:18:36 +08001207 if (status) {
1208 mlog_errno(status);
1209 goto bail;
1210 }
1211 }
1212
Mark Fashehccd979b2005-12-15 14:31:24 -08001213 /* allocate the number of new eb blocks we need */
1214 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1215 GFP_KERNEL);
1216 if (!new_eb_bhs) {
1217 status = -ENOMEM;
1218 mlog_errno(status);
1219 goto bail;
1220 }
1221
Joel Becker42a5a7a2009-02-12 18:49:19 -08001222 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
Mark Fashehccd979b2005-12-15 14:31:24 -08001223 meta_ac, new_eb_bhs);
1224 if (status < 0) {
1225 mlog_errno(status);
1226 goto bail;
1227 }
1228
1229 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1230 * linked with the rest of the tree.
1231 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1232 *
1233 * when we leave the loop, new_last_eb_blk will point to the
1234 * newest leaf, and next_blkno will point to the topmost extent
1235 * block. */
1236 next_blkno = new_last_eb_blk = 0;
1237 for(i = 0; i < new_blocks; i++) {
1238 bh = new_eb_bhs[i];
1239 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001240 /* ocfs2_create_new_meta_bhs() should create it right! */
1241 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001242 eb_el = &eb->h_list;
1243
Joel Beckerd401dc12009-02-13 02:24:10 -08001244 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
Joel Becker13723d02008-10-17 19:25:01 -07001245 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001246 if (status < 0) {
1247 mlog_errno(status);
1248 goto bail;
1249 }
1250
1251 eb->h_next_leaf_blk = 0;
1252 eb_el->l_tree_depth = cpu_to_le16(i);
1253 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001254 /*
1255 * This actually counts as an empty extent as
1256 * c_clusters == 0
1257 */
1258 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001259 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001260 /*
1261 * eb_el isn't always an interior node, but even leaf
1262 * nodes want a zero'd flags and reserved field so
1263 * this gets the whole 32 bits regardless of use.
1264 */
1265 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001266 if (!eb_el->l_tree_depth)
1267 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1268
Joel Beckerec20cec2010-03-19 14:13:52 -07001269 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001270 next_blkno = le64_to_cpu(eb->h_blkno);
1271 }
1272
1273 /* This is a bit hairy. We want to update up to three blocks
1274 * here without leaving any of them in an inconsistent state
1275 * in case of error. We don't have to worry about
1276 * journal_dirty erroring as it won't unless we've aborted the
1277 * handle (in which case we would never be here) so reserving
1278 * the write with journal_access is all we need to do. */
Joel Beckerd401dc12009-02-13 02:24:10 -08001279 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001280 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001281 if (status < 0) {
1282 mlog_errno(status);
1283 goto bail;
1284 }
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001285 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001286 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001287 if (status < 0) {
1288 mlog_errno(status);
1289 goto bail;
1290 }
1291 if (eb_bh) {
Joel Beckerd401dc12009-02-13 02:24:10 -08001292 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001293 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001294 if (status < 0) {
1295 mlog_errno(status);
1296 goto bail;
1297 }
1298 }
1299
1300 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001301 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001302 i = le16_to_cpu(el->l_next_free_rec);
1303 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001304 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001305 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001306 le16_add_cpu(&el->l_next_free_rec, 1);
1307
1308 /* fe needs a new last extent block pointer, as does the
1309 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001310 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001311
Mark Fasheh328d5752007-06-18 10:48:04 -07001312 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001313 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1314
Joel Beckerec20cec2010-03-19 14:13:52 -07001315 ocfs2_journal_dirty(handle, *last_eb_bh);
1316 ocfs2_journal_dirty(handle, et->et_root_bh);
1317 if (eb_bh)
1318 ocfs2_journal_dirty(handle, eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001319
Mark Fasheh328d5752007-06-18 10:48:04 -07001320 /*
1321 * Some callers want to track the rightmost leaf so pass it
1322 * back here.
1323 */
1324 brelse(*last_eb_bh);
1325 get_bh(new_eb_bhs[0]);
1326 *last_eb_bh = new_eb_bhs[0];
1327
Mark Fashehccd979b2005-12-15 14:31:24 -08001328 status = 0;
1329bail:
1330 if (new_eb_bhs) {
1331 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001332 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001333 kfree(new_eb_bhs);
1334 }
1335
1336 mlog_exit(status);
1337 return status;
1338}
1339
1340/*
1341 * adds another level to the allocation tree.
1342 * returns back the new extent block so you can add a branch to it
1343 * after this call.
1344 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001345static int ocfs2_shift_tree_depth(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001346 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001347 struct ocfs2_alloc_context *meta_ac,
1348 struct buffer_head **ret_new_eb_bh)
1349{
1350 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001351 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001352 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001353 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001354 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001355 struct ocfs2_extent_list *eb_el;
1356
1357 mlog_entry_void();
1358
Joel Becker42a5a7a2009-02-12 18:49:19 -08001359 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
Mark Fashehccd979b2005-12-15 14:31:24 -08001360 &new_eb_bh);
1361 if (status < 0) {
1362 mlog_errno(status);
1363 goto bail;
1364 }
1365
1366 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001367 /* ocfs2_create_new_meta_bhs() should create it right! */
1368 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001369
1370 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001371 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001372
Joel Beckerd401dc12009-02-13 02:24:10 -08001373 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001374 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001375 if (status < 0) {
1376 mlog_errno(status);
1377 goto bail;
1378 }
1379
Tao Mae7d4cb62008-08-18 17:38:44 +08001380 /* copy the root extent list data into the new extent block */
1381 eb_el->l_tree_depth = root_el->l_tree_depth;
1382 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1383 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1384 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001385
Joel Beckerec20cec2010-03-19 14:13:52 -07001386 ocfs2_journal_dirty(handle, new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001387
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001388 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001389 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001390 if (status < 0) {
1391 mlog_errno(status);
1392 goto bail;
1393 }
1394
Mark Fashehdcd05382007-01-16 11:32:23 -08001395 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1396
Tao Mae7d4cb62008-08-18 17:38:44 +08001397 /* update root_bh now */
1398 le16_add_cpu(&root_el->l_tree_depth, 1);
1399 root_el->l_recs[0].e_cpos = 0;
1400 root_el->l_recs[0].e_blkno = eb->h_blkno;
1401 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1402 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1403 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1404 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001405
1406 /* If this is our 1st tree depth shift, then last_eb_blk
1407 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001408 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001409 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001410
Joel Beckerec20cec2010-03-19 14:13:52 -07001411 ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001412
1413 *ret_new_eb_bh = new_eb_bh;
1414 new_eb_bh = NULL;
1415 status = 0;
1416bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001417 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001418
1419 mlog_exit(status);
1420 return status;
1421}
1422
1423/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001424 * Should only be called when there is no space left in any of the
1425 * leaf nodes. What we want to do is find the lowest tree depth
1426 * non-leaf extent block with room for new records. There are three
1427 * valid results of this search:
1428 *
1429 * 1) a lowest extent block is found, then we pass it back in
1430 * *lowest_eb_bh and return '0'
1431 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001432 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001433 * pass NULL back in *lowest_eb_bh, but still return '0'
1434 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001435 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001436 * which case we return > 0
1437 *
1438 * return status < 0 indicates an error.
1439 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001440static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001441 struct buffer_head **target_bh)
1442{
1443 int status = 0, i;
1444 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001445 struct ocfs2_extent_block *eb;
1446 struct ocfs2_extent_list *el;
1447 struct buffer_head *bh = NULL;
1448 struct buffer_head *lowest_bh = NULL;
1449
1450 mlog_entry_void();
1451
1452 *target_bh = NULL;
1453
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001454 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001455
1456 while(le16_to_cpu(el->l_tree_depth) > 1) {
1457 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08001458 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1459 "Owner %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001460 "extent list (next_free_rec == 0)",
Joel Becker3d03a302009-02-12 17:49:26 -08001461 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001462 status = -EIO;
1463 goto bail;
1464 }
1465 i = le16_to_cpu(el->l_next_free_rec) - 1;
1466 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1467 if (!blkno) {
Joel Becker3d03a302009-02-12 17:49:26 -08001468 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1469 "Owner %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001470 "list where extent # %d has no physical "
1471 "block start",
Joel Becker3d03a302009-02-12 17:49:26 -08001472 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001473 status = -EIO;
1474 goto bail;
1475 }
1476
Mark Fasheha81cb882008-10-07 14:25:16 -07001477 brelse(bh);
1478 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001479
Joel Becker3d03a302009-02-12 17:49:26 -08001480 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001481 if (status < 0) {
1482 mlog_errno(status);
1483 goto bail;
1484 }
1485
1486 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001487 el = &eb->h_list;
1488
1489 if (le16_to_cpu(el->l_next_free_rec) <
1490 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001491 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001492 lowest_bh = bh;
1493 get_bh(lowest_bh);
1494 }
1495 }
1496
1497 /* If we didn't find one and the fe doesn't have any room,
1498 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001499 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001500 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001501 status = 1;
1502
1503 *target_bh = lowest_bh;
1504bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001505 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001506
1507 mlog_exit(status);
1508 return status;
1509}
1510
Mark Fashehe48edee2007-03-07 16:46:57 -08001511/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001512 * Grow a b-tree so that it has more records.
1513 *
1514 * We might shift the tree depth in which case existing paths should
1515 * be considered invalid.
1516 *
1517 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001518 *
1519 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001520 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001521static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1522 int *final_depth, struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001523 struct ocfs2_alloc_context *meta_ac)
1524{
1525 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001526 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001527 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001528 struct buffer_head *bh = NULL;
1529
1530 BUG_ON(meta_ac == NULL);
1531
Joel Beckerd401dc12009-02-13 02:24:10 -08001532 shift = ocfs2_find_branch_target(et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001533 if (shift < 0) {
1534 ret = shift;
1535 mlog_errno(ret);
1536 goto out;
1537 }
1538
1539 /* We traveled all the way to the bottom of the allocation tree
1540 * and didn't find room for any more extents - we need to add
1541 * another tree level */
1542 if (shift) {
1543 BUG_ON(bh);
1544 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1545
1546 /* ocfs2_shift_tree_depth will return us a buffer with
1547 * the new extent block (so we can pass that to
1548 * ocfs2_add_branch). */
Joel Beckerd401dc12009-02-13 02:24:10 -08001549 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001550 if (ret < 0) {
1551 mlog_errno(ret);
1552 goto out;
1553 }
1554 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001555 if (depth == 1) {
1556 /*
1557 * Special case: we have room now if we shifted from
1558 * tree_depth 0, so no more work needs to be done.
1559 *
1560 * We won't be calling add_branch, so pass
1561 * back *last_eb_bh as the new leaf. At depth
1562 * zero, it should always be null so there's
1563 * no reason to brelse.
1564 */
1565 BUG_ON(*last_eb_bh);
1566 get_bh(bh);
1567 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001568 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001569 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001570 }
1571
1572 /* call ocfs2_add_branch to add the final part of the tree with
1573 * the new data. */
1574 mlog(0, "add branch. bh = %p\n", bh);
Joel Beckerd401dc12009-02-13 02:24:10 -08001575 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001576 meta_ac);
1577 if (ret < 0) {
1578 mlog_errno(ret);
1579 goto out;
1580 }
1581
1582out:
1583 if (final_depth)
1584 *final_depth = depth;
1585 brelse(bh);
1586 return ret;
1587}
1588
1589/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001590 * This function will discard the rightmost extent record.
1591 */
1592static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1593{
1594 int next_free = le16_to_cpu(el->l_next_free_rec);
1595 int count = le16_to_cpu(el->l_count);
1596 unsigned int num_bytes;
1597
1598 BUG_ON(!next_free);
1599 /* This will cause us to go off the end of our extent list. */
1600 BUG_ON(next_free >= count);
1601
1602 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1603
1604 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1605}
1606
1607static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1608 struct ocfs2_extent_rec *insert_rec)
1609{
1610 int i, insert_index, next_free, has_empty, num_bytes;
1611 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1612 struct ocfs2_extent_rec *rec;
1613
1614 next_free = le16_to_cpu(el->l_next_free_rec);
1615 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1616
1617 BUG_ON(!next_free);
1618
1619 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001620 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001621
1622 /*
1623 * The easiest way to approach this is to just remove the
1624 * empty extent and temporarily decrement next_free.
1625 */
1626 if (has_empty) {
1627 /*
1628 * If next_free was 1 (only an empty extent), this
1629 * loop won't execute, which is fine. We still want
1630 * the decrement above to happen.
1631 */
1632 for(i = 0; i < (next_free - 1); i++)
1633 el->l_recs[i] = el->l_recs[i+1];
1634
1635 next_free--;
1636 }
1637
1638 /*
1639 * Figure out what the new record index should be.
1640 */
1641 for(i = 0; i < next_free; i++) {
1642 rec = &el->l_recs[i];
1643
1644 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1645 break;
1646 }
1647 insert_index = i;
1648
1649 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1650 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1651
1652 BUG_ON(insert_index < 0);
1653 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1654 BUG_ON(insert_index > next_free);
1655
1656 /*
1657 * No need to memmove if we're just adding to the tail.
1658 */
1659 if (insert_index != next_free) {
1660 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1661
1662 num_bytes = next_free - insert_index;
1663 num_bytes *= sizeof(struct ocfs2_extent_rec);
1664 memmove(&el->l_recs[insert_index + 1],
1665 &el->l_recs[insert_index],
1666 num_bytes);
1667 }
1668
1669 /*
1670 * Either we had an empty extent, and need to re-increment or
1671 * there was no empty extent on a non full rightmost leaf node,
1672 * in which case we still need to increment.
1673 */
1674 next_free++;
1675 el->l_next_free_rec = cpu_to_le16(next_free);
1676 /*
1677 * Make sure none of the math above just messed up our tree.
1678 */
1679 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1680
1681 el->l_recs[insert_index] = *insert_rec;
1682
1683}
1684
Mark Fasheh328d5752007-06-18 10:48:04 -07001685static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1686{
1687 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1688
1689 BUG_ON(num_recs == 0);
1690
1691 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1692 num_recs--;
1693 size = num_recs * sizeof(struct ocfs2_extent_rec);
1694 memmove(&el->l_recs[0], &el->l_recs[1], size);
1695 memset(&el->l_recs[num_recs], 0,
1696 sizeof(struct ocfs2_extent_rec));
1697 el->l_next_free_rec = cpu_to_le16(num_recs);
1698 }
1699}
1700
Mark Fashehdcd05382007-01-16 11:32:23 -08001701/*
1702 * Create an empty extent record .
1703 *
1704 * l_next_free_rec may be updated.
1705 *
1706 * If an empty extent already exists do nothing.
1707 */
1708static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1709{
1710 int next_free = le16_to_cpu(el->l_next_free_rec);
1711
Mark Fashehe48edee2007-03-07 16:46:57 -08001712 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1713
Mark Fashehdcd05382007-01-16 11:32:23 -08001714 if (next_free == 0)
1715 goto set_and_inc;
1716
1717 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1718 return;
1719
1720 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1721 "Asked to create an empty extent in a full list:\n"
1722 "count = %u, tree depth = %u",
1723 le16_to_cpu(el->l_count),
1724 le16_to_cpu(el->l_tree_depth));
1725
1726 ocfs2_shift_records_right(el);
1727
1728set_and_inc:
1729 le16_add_cpu(&el->l_next_free_rec, 1);
1730 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1731}
1732
1733/*
1734 * For a rotation which involves two leaf nodes, the "root node" is
1735 * the lowest level tree node which contains a path to both leafs. This
1736 * resulting set of information can be used to form a complete "subtree"
1737 *
1738 * This function is passed two full paths from the dinode down to a
1739 * pair of adjacent leaves. It's task is to figure out which path
1740 * index contains the subtree root - this can be the root index itself
1741 * in a worst-case rotation.
1742 *
1743 * The array index of the subtree root is passed back.
1744 */
Tao Ma38a04e42009-11-30 14:32:19 +08001745int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1746 struct ocfs2_path *left,
1747 struct ocfs2_path *right)
Mark Fashehdcd05382007-01-16 11:32:23 -08001748{
1749 int i = 0;
1750
1751 /*
1752 * Check that the caller passed in two paths from the same tree.
1753 */
1754 BUG_ON(path_root_bh(left) != path_root_bh(right));
1755
1756 do {
1757 i++;
1758
1759 /*
1760 * The caller didn't pass two adjacent paths.
1761 */
1762 mlog_bug_on_msg(i > left->p_tree_depth,
Joel Becker7dc02802009-02-12 19:20:13 -08001763 "Owner %llu, left depth %u, right depth %u\n"
Mark Fashehdcd05382007-01-16 11:32:23 -08001764 "left leaf blk %llu, right leaf blk %llu\n",
Joel Becker7dc02802009-02-12 19:20:13 -08001765 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1766 left->p_tree_depth, right->p_tree_depth,
Mark Fashehdcd05382007-01-16 11:32:23 -08001767 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1768 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1769 } while (left->p_node[i].bh->b_blocknr ==
1770 right->p_node[i].bh->b_blocknr);
1771
1772 return i - 1;
1773}
1774
1775typedef void (path_insert_t)(void *, struct buffer_head *);
1776
1777/*
1778 * Traverse a btree path in search of cpos, starting at root_el.
1779 *
1780 * This code can be called with a cpos larger than the tree, in which
1781 * case it will return the rightmost path.
1782 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001783static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
Mark Fashehdcd05382007-01-16 11:32:23 -08001784 struct ocfs2_extent_list *root_el, u32 cpos,
1785 path_insert_t *func, void *data)
1786{
1787 int i, ret = 0;
1788 u32 range;
1789 u64 blkno;
1790 struct buffer_head *bh = NULL;
1791 struct ocfs2_extent_block *eb;
1792 struct ocfs2_extent_list *el;
1793 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08001794
1795 el = root_el;
1796 while (el->l_tree_depth) {
1797 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001798 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1799 "Owner %llu has empty extent list at "
Mark Fashehdcd05382007-01-16 11:32:23 -08001800 "depth %u\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001801 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001802 le16_to_cpu(el->l_tree_depth));
1803 ret = -EROFS;
1804 goto out;
1805
1806 }
1807
1808 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1809 rec = &el->l_recs[i];
1810
1811 /*
1812 * In the case that cpos is off the allocation
1813 * tree, this should just wind up returning the
1814 * rightmost record.
1815 */
1816 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001817 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001818 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1819 break;
1820 }
1821
1822 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1823 if (blkno == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001824 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1825 "Owner %llu has bad blkno in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001826 "at depth %u (index %d)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001827 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001828 le16_to_cpu(el->l_tree_depth), i);
1829 ret = -EROFS;
1830 goto out;
1831 }
1832
1833 brelse(bh);
1834 bh = NULL;
Joel Beckerfacdb772009-02-12 18:08:48 -08001835 ret = ocfs2_read_extent_block(ci, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001836 if (ret) {
1837 mlog_errno(ret);
1838 goto out;
1839 }
1840
1841 eb = (struct ocfs2_extent_block *) bh->b_data;
1842 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001843
1844 if (le16_to_cpu(el->l_next_free_rec) >
1845 le16_to_cpu(el->l_count)) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001846 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1847 "Owner %llu has bad count in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001848 "at block %llu (next free=%u, count=%u)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001849 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001850 (unsigned long long)bh->b_blocknr,
1851 le16_to_cpu(el->l_next_free_rec),
1852 le16_to_cpu(el->l_count));
1853 ret = -EROFS;
1854 goto out;
1855 }
1856
1857 if (func)
1858 func(data, bh);
1859 }
1860
1861out:
1862 /*
1863 * Catch any trailing bh that the loop didn't handle.
1864 */
1865 brelse(bh);
1866
1867 return ret;
1868}
1869
1870/*
1871 * Given an initialized path (that is, it has a valid root extent
1872 * list), this function will traverse the btree in search of the path
1873 * which would contain cpos.
1874 *
1875 * The path traveled is recorded in the path structure.
1876 *
1877 * Note that this will not do any comparisons on leaf node extent
1878 * records, so it will work fine in the case that we just added a tree
1879 * branch.
1880 */
1881struct find_path_data {
1882 int index;
1883 struct ocfs2_path *path;
1884};
1885static void find_path_ins(void *data, struct buffer_head *bh)
1886{
1887 struct find_path_data *fp = data;
1888
1889 get_bh(bh);
1890 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1891 fp->index++;
1892}
Tao Mae2e9f602009-08-18 11:22:34 +08001893int ocfs2_find_path(struct ocfs2_caching_info *ci,
1894 struct ocfs2_path *path, u32 cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08001895{
1896 struct find_path_data data;
1897
1898 data.index = 1;
1899 data.path = path;
Joel Beckerfacdb772009-02-12 18:08:48 -08001900 return __ocfs2_find_path(ci, path_root_el(path), cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08001901 find_path_ins, &data);
1902}
1903
1904static void find_leaf_ins(void *data, struct buffer_head *bh)
1905{
1906 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1907 struct ocfs2_extent_list *el = &eb->h_list;
1908 struct buffer_head **ret = data;
1909
1910 /* We want to retain only the leaf block. */
1911 if (le16_to_cpu(el->l_tree_depth) == 0) {
1912 get_bh(bh);
1913 *ret = bh;
1914 }
1915}
1916/*
1917 * Find the leaf block in the tree which would contain cpos. No
1918 * checking of the actual leaf is done.
1919 *
1920 * Some paths want to call this instead of allocating a path structure
1921 * and calling ocfs2_find_path().
1922 *
1923 * This function doesn't handle non btree extent lists.
1924 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001925int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1926 struct ocfs2_extent_list *root_el, u32 cpos,
1927 struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001928{
1929 int ret;
1930 struct buffer_head *bh = NULL;
1931
Joel Beckerfacdb772009-02-12 18:08:48 -08001932 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001933 if (ret) {
1934 mlog_errno(ret);
1935 goto out;
1936 }
1937
1938 *leaf_bh = bh;
1939out:
1940 return ret;
1941}
1942
1943/*
1944 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1945 *
1946 * Basically, we've moved stuff around at the bottom of the tree and
1947 * we need to fix up the extent records above the changes to reflect
1948 * the new changes.
1949 *
1950 * left_rec: the record on the left.
1951 * left_child_el: is the child list pointed to by left_rec
1952 * right_rec: the record to the right of left_rec
1953 * right_child_el: is the child list pointed to by right_rec
1954 *
1955 * By definition, this only works on interior nodes.
1956 */
1957static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1958 struct ocfs2_extent_list *left_child_el,
1959 struct ocfs2_extent_rec *right_rec,
1960 struct ocfs2_extent_list *right_child_el)
1961{
1962 u32 left_clusters, right_end;
1963
1964 /*
1965 * Interior nodes never have holes. Their cpos is the cpos of
1966 * the leftmost record in their child list. Their cluster
1967 * count covers the full theoretical range of their child list
1968 * - the range between their cpos and the cpos of the record
1969 * immediately to their right.
1970 */
1971 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08001972 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1973 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07001974 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1975 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1976 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001977 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001978 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001979
1980 /*
1981 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001982 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001983 * updating e_cpos to keep the same highest cluster count.
1984 */
1985 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001986 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001987
1988 right_rec->e_cpos = left_rec->e_cpos;
1989 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1990
1991 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001992 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001993}
1994
1995/*
1996 * Adjust the adjacent root node records involved in a
1997 * rotation. left_el_blkno is passed in as a key so that we can easily
1998 * find it's index in the root list.
1999 */
2000static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
2001 struct ocfs2_extent_list *left_el,
2002 struct ocfs2_extent_list *right_el,
2003 u64 left_el_blkno)
2004{
2005 int i;
2006
2007 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
2008 le16_to_cpu(left_el->l_tree_depth));
2009
2010 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
2011 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
2012 break;
2013 }
2014
2015 /*
2016 * The path walking code should have never returned a root and
2017 * two paths which are not adjacent.
2018 */
2019 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2020
2021 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
2022 &root_el->l_recs[i + 1], right_el);
2023}
2024
2025/*
2026 * We've changed a leaf block (in right_path) and need to reflect that
2027 * change back up the subtree.
2028 *
2029 * This happens in multiple places:
2030 * - When we've moved an extent record from the left path leaf to the right
2031 * path leaf to make room for an empty extent in the left path leaf.
2032 * - When our insert into the right path leaf is at the leftmost edge
2033 * and requires an update of the path immediately to it's left. This
2034 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08002035 * - When we've adjusted the last extent record in the left path leaf and the
2036 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08002037 */
Joel Becker4619c732009-02-12 19:02:36 -08002038static void ocfs2_complete_edge_insert(handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08002039 struct ocfs2_path *left_path,
2040 struct ocfs2_path *right_path,
2041 int subtree_index)
2042{
Joel Beckerec20cec2010-03-19 14:13:52 -07002043 int i, idx;
Mark Fashehdcd05382007-01-16 11:32:23 -08002044 struct ocfs2_extent_list *el, *left_el, *right_el;
2045 struct ocfs2_extent_rec *left_rec, *right_rec;
2046 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2047
2048 /*
2049 * Update the counts and position values within all the
2050 * interior nodes to reflect the leaf rotation we just did.
2051 *
2052 * The root node is handled below the loop.
2053 *
2054 * We begin the loop with right_el and left_el pointing to the
2055 * leaf lists and work our way up.
2056 *
2057 * NOTE: within this loop, left_el and right_el always refer
2058 * to the *child* lists.
2059 */
2060 left_el = path_leaf_el(left_path);
2061 right_el = path_leaf_el(right_path);
2062 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2063 mlog(0, "Adjust records at index %u\n", i);
2064
2065 /*
2066 * One nice property of knowing that all of these
2067 * nodes are below the root is that we only deal with
2068 * the leftmost right node record and the rightmost
2069 * left node record.
2070 */
2071 el = left_path->p_node[i].el;
2072 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2073 left_rec = &el->l_recs[idx];
2074
2075 el = right_path->p_node[i].el;
2076 right_rec = &el->l_recs[0];
2077
2078 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2079 right_el);
2080
Joel Beckerec20cec2010-03-19 14:13:52 -07002081 ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2082 ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002083
2084 /*
2085 * Setup our list pointers now so that the current
2086 * parents become children in the next iteration.
2087 */
2088 left_el = left_path->p_node[i].el;
2089 right_el = right_path->p_node[i].el;
2090 }
2091
2092 /*
2093 * At the root node, adjust the two adjacent records which
2094 * begin our path to the leaves.
2095 */
2096
2097 el = left_path->p_node[subtree_index].el;
2098 left_el = left_path->p_node[subtree_index + 1].el;
2099 right_el = right_path->p_node[subtree_index + 1].el;
2100
2101 ocfs2_adjust_root_records(el, left_el, right_el,
2102 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2103
2104 root_bh = left_path->p_node[subtree_index].bh;
2105
Joel Beckerec20cec2010-03-19 14:13:52 -07002106 ocfs2_journal_dirty(handle, root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002107}
2108
Joel Becker5c601ab2009-02-12 19:10:13 -08002109static int ocfs2_rotate_subtree_right(handle_t *handle,
2110 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08002111 struct ocfs2_path *left_path,
2112 struct ocfs2_path *right_path,
2113 int subtree_index)
2114{
2115 int ret, i;
2116 struct buffer_head *right_leaf_bh;
2117 struct buffer_head *left_leaf_bh = NULL;
2118 struct buffer_head *root_bh;
2119 struct ocfs2_extent_list *right_el, *left_el;
2120 struct ocfs2_extent_rec move_rec;
2121
2122 left_leaf_bh = path_leaf_bh(left_path);
2123 left_el = path_leaf_el(left_path);
2124
2125 if (left_el->l_next_free_rec != left_el->l_count) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002126 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002127 "Inode %llu has non-full interior leaf node %llu"
2128 "(next free = %u)",
Joel Becker5c601ab2009-02-12 19:10:13 -08002129 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002130 (unsigned long long)left_leaf_bh->b_blocknr,
2131 le16_to_cpu(left_el->l_next_free_rec));
2132 return -EROFS;
2133 }
2134
2135 /*
2136 * This extent block may already have an empty record, so we
2137 * return early if so.
2138 */
2139 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2140 return 0;
2141
2142 root_bh = left_path->p_node[subtree_index].bh;
2143 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2144
Joel Becker5c601ab2009-02-12 19:10:13 -08002145 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002146 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002147 if (ret) {
2148 mlog_errno(ret);
2149 goto out;
2150 }
2151
2152 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002153 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002154 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002155 if (ret) {
2156 mlog_errno(ret);
2157 goto out;
2158 }
2159
Joel Becker5c601ab2009-02-12 19:10:13 -08002160 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002161 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002162 if (ret) {
2163 mlog_errno(ret);
2164 goto out;
2165 }
2166 }
2167
2168 right_leaf_bh = path_leaf_bh(right_path);
2169 right_el = path_leaf_el(right_path);
2170
2171 /* This is a code error, not a disk corruption. */
2172 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2173 "because rightmost leaf block %llu is empty\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002174 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002175 (unsigned long long)right_leaf_bh->b_blocknr);
2176
2177 ocfs2_create_empty_extent(right_el);
2178
Joel Beckerec20cec2010-03-19 14:13:52 -07002179 ocfs2_journal_dirty(handle, right_leaf_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002180
2181 /* Do the copy now. */
2182 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2183 move_rec = left_el->l_recs[i];
2184 right_el->l_recs[0] = move_rec;
2185
2186 /*
2187 * Clear out the record we just copied and shift everything
2188 * over, leaving an empty extent in the left leaf.
2189 *
2190 * We temporarily subtract from next_free_rec so that the
2191 * shift will lose the tail record (which is now defunct).
2192 */
2193 le16_add_cpu(&left_el->l_next_free_rec, -1);
2194 ocfs2_shift_records_right(left_el);
2195 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2196 le16_add_cpu(&left_el->l_next_free_rec, 1);
2197
Joel Beckerec20cec2010-03-19 14:13:52 -07002198 ocfs2_journal_dirty(handle, left_leaf_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002199
Joel Becker4619c732009-02-12 19:02:36 -08002200 ocfs2_complete_edge_insert(handle, left_path, right_path,
2201 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002202
2203out:
2204 return ret;
2205}
2206
2207/*
2208 * Given a full path, determine what cpos value would return us a path
2209 * containing the leaf immediately to the left of the current one.
2210 *
2211 * Will return zero if the path passed in is already the leftmost path.
2212 */
2213static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2214 struct ocfs2_path *path, u32 *cpos)
2215{
2216 int i, j, ret = 0;
2217 u64 blkno;
2218 struct ocfs2_extent_list *el;
2219
Mark Fashehe48edee2007-03-07 16:46:57 -08002220 BUG_ON(path->p_tree_depth == 0);
2221
Mark Fashehdcd05382007-01-16 11:32:23 -08002222 *cpos = 0;
2223
2224 blkno = path_leaf_bh(path)->b_blocknr;
2225
2226 /* Start at the tree node just above the leaf and work our way up. */
2227 i = path->p_tree_depth - 1;
2228 while (i >= 0) {
2229 el = path->p_node[i].el;
2230
2231 /*
2232 * Find the extent record just before the one in our
2233 * path.
2234 */
2235 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2236 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2237 if (j == 0) {
2238 if (i == 0) {
2239 /*
2240 * We've determined that the
2241 * path specified is already
2242 * the leftmost one - return a
2243 * cpos of zero.
2244 */
2245 goto out;
2246 }
2247 /*
2248 * The leftmost record points to our
2249 * leaf - we need to travel up the
2250 * tree one level.
2251 */
2252 goto next_node;
2253 }
2254
2255 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002256 *cpos = *cpos + ocfs2_rec_clusters(el,
2257 &el->l_recs[j - 1]);
2258 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002259 goto out;
2260 }
2261 }
2262
2263 /*
2264 * If we got here, we never found a valid node where
2265 * the tree indicated one should be.
2266 */
2267 ocfs2_error(sb,
2268 "Invalid extent tree at extent block %llu\n",
2269 (unsigned long long)blkno);
2270 ret = -EROFS;
2271 goto out;
2272
2273next_node:
2274 blkno = path->p_node[i].bh->b_blocknr;
2275 i--;
2276 }
2277
2278out:
2279 return ret;
2280}
2281
Mark Fasheh328d5752007-06-18 10:48:04 -07002282/*
2283 * Extend the transaction by enough credits to complete the rotation,
2284 * and still leave at least the original number of credits allocated
2285 * to this transaction.
2286 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002287static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002288 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002289 struct ocfs2_path *path)
2290{
Tao Mac901fb02010-04-26 14:34:57 +08002291 int ret = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07002292 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002293
Tao Mac901fb02010-04-26 14:34:57 +08002294 if (handle->h_buffer_credits < credits)
Tao Mac18b8122009-08-18 11:44:07 +08002295 ret = ocfs2_extend_trans(handle,
2296 credits - handle->h_buffer_credits);
Tao Mac18b8122009-08-18 11:44:07 +08002297
Tao Mac901fb02010-04-26 14:34:57 +08002298 return ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08002299}
2300
2301/*
2302 * Trap the case where we're inserting into the theoretical range past
2303 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2304 * whose cpos is less than ours into the right leaf.
2305 *
2306 * It's only necessary to look at the rightmost record of the left
2307 * leaf because the logic that calls us should ensure that the
2308 * theoretical ranges in the path components above the leaves are
2309 * correct.
2310 */
2311static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2312 u32 insert_cpos)
2313{
2314 struct ocfs2_extent_list *left_el;
2315 struct ocfs2_extent_rec *rec;
2316 int next_free;
2317
2318 left_el = path_leaf_el(left_path);
2319 next_free = le16_to_cpu(left_el->l_next_free_rec);
2320 rec = &left_el->l_recs[next_free - 1];
2321
2322 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2323 return 1;
2324 return 0;
2325}
2326
Mark Fasheh328d5752007-06-18 10:48:04 -07002327static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2328{
2329 int next_free = le16_to_cpu(el->l_next_free_rec);
2330 unsigned int range;
2331 struct ocfs2_extent_rec *rec;
2332
2333 if (next_free == 0)
2334 return 0;
2335
2336 rec = &el->l_recs[0];
2337 if (ocfs2_is_empty_extent(rec)) {
2338 /* Empty list. */
2339 if (next_free == 1)
2340 return 0;
2341 rec = &el->l_recs[1];
2342 }
2343
2344 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2345 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2346 return 1;
2347 return 0;
2348}
2349
Mark Fashehdcd05382007-01-16 11:32:23 -08002350/*
2351 * Rotate all the records in a btree right one record, starting at insert_cpos.
2352 *
2353 * The path to the rightmost leaf should be passed in.
2354 *
2355 * The array is assumed to be large enough to hold an entire path (tree depth).
2356 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002357 * Upon successful return from this function:
Mark Fashehdcd05382007-01-16 11:32:23 -08002358 *
2359 * - The 'right_path' array will contain a path to the leaf block
2360 * whose range contains e_cpos.
2361 * - That leaf block will have a single empty extent in list index 0.
2362 * - In the case that the rotation requires a post-insert update,
2363 * *ret_left_path will contain a valid path which can be passed to
2364 * ocfs2_insert_path().
2365 */
Joel Becker1bbf0b82009-02-12 19:42:08 -08002366static int ocfs2_rotate_tree_right(handle_t *handle,
Joel Becker5c601ab2009-02-12 19:10:13 -08002367 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002368 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002369 u32 insert_cpos,
2370 struct ocfs2_path *right_path,
2371 struct ocfs2_path **ret_left_path)
2372{
Mark Fasheh328d5752007-06-18 10:48:04 -07002373 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002374 u32 cpos;
2375 struct ocfs2_path *left_path = NULL;
Joel Becker5c601ab2009-02-12 19:10:13 -08002376 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehdcd05382007-01-16 11:32:23 -08002377
2378 *ret_left_path = NULL;
2379
Joel Beckerffdd7a52008-10-17 22:32:01 -07002380 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002381 if (!left_path) {
2382 ret = -ENOMEM;
2383 mlog_errno(ret);
2384 goto out;
2385 }
2386
Joel Becker5c601ab2009-02-12 19:10:13 -08002387 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002388 if (ret) {
2389 mlog_errno(ret);
2390 goto out;
2391 }
2392
2393 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2394
2395 /*
2396 * What we want to do here is:
2397 *
2398 * 1) Start with the rightmost path.
2399 *
2400 * 2) Determine a path to the leaf block directly to the left
2401 * of that leaf.
2402 *
2403 * 3) Determine the 'subtree root' - the lowest level tree node
2404 * which contains a path to both leaves.
2405 *
2406 * 4) Rotate the subtree.
2407 *
2408 * 5) Find the next subtree by considering the left path to be
2409 * the new right path.
2410 *
2411 * The check at the top of this while loop also accepts
2412 * insert_cpos == cpos because cpos is only a _theoretical_
2413 * value to get us the left path - insert_cpos might very well
2414 * be filling that hole.
2415 *
2416 * Stop at a cpos of '0' because we either started at the
2417 * leftmost branch (i.e., a tree with one branch and a
2418 * rotation inside of it), or we've gone as far as we can in
2419 * rotating subtrees.
2420 */
2421 while (cpos && insert_cpos <= cpos) {
2422 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2423 insert_cpos, cpos);
2424
Joel Becker5c601ab2009-02-12 19:10:13 -08002425 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002426 if (ret) {
2427 mlog_errno(ret);
2428 goto out;
2429 }
2430
2431 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2432 path_leaf_bh(right_path),
Joel Becker5c601ab2009-02-12 19:10:13 -08002433 "Owner %llu: error during insert of %u "
Mark Fashehdcd05382007-01-16 11:32:23 -08002434 "(left path cpos %u) results in two identical "
2435 "paths ending at %llu\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002436 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2437 insert_cpos, cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08002438 (unsigned long long)
2439 path_leaf_bh(left_path)->b_blocknr);
2440
Mark Fasheh328d5752007-06-18 10:48:04 -07002441 if (split == SPLIT_NONE &&
2442 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002443 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002444
2445 /*
2446 * We've rotated the tree as much as we
2447 * should. The rest is up to
2448 * ocfs2_insert_path() to complete, after the
2449 * record insertion. We indicate this
2450 * situation by returning the left path.
2451 *
2452 * The reason we don't adjust the records here
2453 * before the record insert is that an error
2454 * later might break the rule where a parent
2455 * record e_cpos will reflect the actual
2456 * e_cpos of the 1st nonempty record of the
2457 * child list.
2458 */
2459 *ret_left_path = left_path;
2460 goto out_ret_path;
2461 }
2462
Joel Becker7dc02802009-02-12 19:20:13 -08002463 start = ocfs2_find_subtree_root(et, left_path, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002464
2465 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2466 start,
2467 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2468 right_path->p_tree_depth);
2469
2470 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002471 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002472 if (ret) {
2473 mlog_errno(ret);
2474 goto out;
2475 }
2476
Joel Becker5c601ab2009-02-12 19:10:13 -08002477 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002478 right_path, start);
2479 if (ret) {
2480 mlog_errno(ret);
2481 goto out;
2482 }
2483
Mark Fasheh328d5752007-06-18 10:48:04 -07002484 if (split != SPLIT_NONE &&
2485 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2486 insert_cpos)) {
2487 /*
2488 * A rotate moves the rightmost left leaf
2489 * record over to the leftmost right leaf
2490 * slot. If we're doing an extent split
2491 * instead of a real insert, then we have to
2492 * check that the extent to be split wasn't
2493 * just moved over. If it was, then we can
2494 * exit here, passing left_path back -
2495 * ocfs2_split_extent() is smart enough to
2496 * search both leaves.
2497 */
2498 *ret_left_path = left_path;
2499 goto out_ret_path;
2500 }
2501
Mark Fashehdcd05382007-01-16 11:32:23 -08002502 /*
2503 * There is no need to re-read the next right path
2504 * as we know that it'll be our current left
2505 * path. Optimize by copying values instead.
2506 */
2507 ocfs2_mv_path(right_path, left_path);
2508
Joel Becker5c601ab2009-02-12 19:10:13 -08002509 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002510 if (ret) {
2511 mlog_errno(ret);
2512 goto out;
2513 }
2514 }
2515
2516out:
2517 ocfs2_free_path(left_path);
2518
2519out_ret_path:
2520 return ret;
2521}
2522
Joel Becker09106ba2009-02-12 19:43:57 -08002523static int ocfs2_update_edge_lengths(handle_t *handle,
2524 struct ocfs2_extent_tree *et,
Tao Ma3c5e1062009-07-21 15:42:05 +08002525 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002526{
Tao Ma3c5e1062009-07-21 15:42:05 +08002527 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002528 struct ocfs2_extent_rec *rec;
2529 struct ocfs2_extent_list *el;
2530 struct ocfs2_extent_block *eb;
2531 u32 range;
2532
Tao Ma3c5e1062009-07-21 15:42:05 +08002533 /*
2534 * In normal tree rotation process, we will never touch the
2535 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2536 * doesn't reserve the credits for them either.
2537 *
2538 * But we do have a special case here which will update the rightmost
2539 * records for all the bh in the path.
2540 * So we have to allocate extra credits and access them.
2541 */
Tao Mac901fb02010-04-26 14:34:57 +08002542 ret = ocfs2_extend_trans(handle, subtree_index);
Tao Ma3c5e1062009-07-21 15:42:05 +08002543 if (ret) {
2544 mlog_errno(ret);
2545 goto out;
2546 }
2547
Joel Becker09106ba2009-02-12 19:43:57 -08002548 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002549 if (ret) {
2550 mlog_errno(ret);
2551 goto out;
2552 }
2553
Mark Fasheh328d5752007-06-18 10:48:04 -07002554 /* Path should always be rightmost. */
2555 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2556 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2557
2558 el = &eb->h_list;
2559 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2560 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2561 rec = &el->l_recs[idx];
2562 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2563
2564 for (i = 0; i < path->p_tree_depth; i++) {
2565 el = path->p_node[i].el;
2566 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2567 rec = &el->l_recs[idx];
2568
2569 rec->e_int_clusters = cpu_to_le32(range);
2570 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2571
2572 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2573 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002574out:
2575 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002576}
2577
Joel Becker6641b0c2009-02-12 18:57:52 -08002578static void ocfs2_unlink_path(handle_t *handle,
2579 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002580 struct ocfs2_cached_dealloc_ctxt *dealloc,
2581 struct ocfs2_path *path, int unlink_start)
2582{
2583 int ret, i;
2584 struct ocfs2_extent_block *eb;
2585 struct ocfs2_extent_list *el;
2586 struct buffer_head *bh;
2587
2588 for(i = unlink_start; i < path_num_items(path); i++) {
2589 bh = path->p_node[i].bh;
2590
2591 eb = (struct ocfs2_extent_block *)bh->b_data;
2592 /*
2593 * Not all nodes might have had their final count
2594 * decremented by the caller - handle this here.
2595 */
2596 el = &eb->h_list;
2597 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2598 mlog(ML_ERROR,
2599 "Inode %llu, attempted to remove extent block "
2600 "%llu with %u records\n",
Joel Becker6641b0c2009-02-12 18:57:52 -08002601 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07002602 (unsigned long long)le64_to_cpu(eb->h_blkno),
2603 le16_to_cpu(el->l_next_free_rec));
2604
2605 ocfs2_journal_dirty(handle, bh);
Joel Becker6641b0c2009-02-12 18:57:52 -08002606 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002607 continue;
2608 }
2609
2610 el->l_next_free_rec = 0;
2611 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2612
2613 ocfs2_journal_dirty(handle, bh);
2614
2615 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2616 if (ret)
2617 mlog_errno(ret);
2618
Joel Becker6641b0c2009-02-12 18:57:52 -08002619 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002620 }
2621}
2622
Joel Becker6641b0c2009-02-12 18:57:52 -08002623static void ocfs2_unlink_subtree(handle_t *handle,
2624 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002625 struct ocfs2_path *left_path,
2626 struct ocfs2_path *right_path,
2627 int subtree_index,
2628 struct ocfs2_cached_dealloc_ctxt *dealloc)
2629{
2630 int i;
2631 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2632 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2633 struct ocfs2_extent_list *el;
2634 struct ocfs2_extent_block *eb;
2635
2636 el = path_leaf_el(left_path);
2637
2638 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2639
2640 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2641 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2642 break;
2643
2644 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2645
2646 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2647 le16_add_cpu(&root_el->l_next_free_rec, -1);
2648
2649 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2650 eb->h_next_leaf_blk = 0;
2651
2652 ocfs2_journal_dirty(handle, root_bh);
2653 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2654
Joel Becker6641b0c2009-02-12 18:57:52 -08002655 ocfs2_unlink_path(handle, et, dealloc, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002656 subtree_index + 1);
2657}
2658
Joel Becker1e2dd632009-02-12 19:45:28 -08002659static int ocfs2_rotate_subtree_left(handle_t *handle,
2660 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002661 struct ocfs2_path *left_path,
2662 struct ocfs2_path *right_path,
2663 int subtree_index,
2664 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker1e2dd632009-02-12 19:45:28 -08002665 int *deleted)
Mark Fasheh328d5752007-06-18 10:48:04 -07002666{
2667 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002668 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002669 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2670 struct ocfs2_extent_block *eb;
2671
2672 *deleted = 0;
2673
2674 right_leaf_el = path_leaf_el(right_path);
2675 left_leaf_el = path_leaf_el(left_path);
2676 root_bh = left_path->p_node[subtree_index].bh;
2677 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2678
2679 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2680 return 0;
2681
2682 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2683 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2684 /*
2685 * It's legal for us to proceed if the right leaf is
2686 * the rightmost one and it has an empty extent. There
2687 * are two cases to handle - whether the leaf will be
2688 * empty after removal or not. If the leaf isn't empty
2689 * then just remove the empty extent up front. The
2690 * next block will handle empty leaves by flagging
2691 * them for unlink.
2692 *
2693 * Non rightmost leaves will throw -EAGAIN and the
2694 * caller can manually move the subtree and retry.
2695 */
2696
2697 if (eb->h_next_leaf_blk != 0ULL)
2698 return -EAGAIN;
2699
2700 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002701 ret = ocfs2_journal_access_eb(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002702 path_leaf_bh(right_path),
2703 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002704 if (ret) {
2705 mlog_errno(ret);
2706 goto out;
2707 }
2708
2709 ocfs2_remove_empty_extent(right_leaf_el);
2710 } else
2711 right_has_empty = 1;
2712 }
2713
2714 if (eb->h_next_leaf_blk == 0ULL &&
2715 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2716 /*
2717 * We have to update i_last_eb_blk during the meta
2718 * data delete.
2719 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08002720 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07002721 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002722 if (ret) {
2723 mlog_errno(ret);
2724 goto out;
2725 }
2726
2727 del_right_subtree = 1;
2728 }
2729
2730 /*
2731 * Getting here with an empty extent in the right path implies
2732 * that it's the rightmost path and will be deleted.
2733 */
2734 BUG_ON(right_has_empty && !del_right_subtree);
2735
Joel Becker1e2dd632009-02-12 19:45:28 -08002736 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002737 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002738 if (ret) {
2739 mlog_errno(ret);
2740 goto out;
2741 }
2742
2743 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002744 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002745 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002746 if (ret) {
2747 mlog_errno(ret);
2748 goto out;
2749 }
2750
Joel Becker1e2dd632009-02-12 19:45:28 -08002751 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002752 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002753 if (ret) {
2754 mlog_errno(ret);
2755 goto out;
2756 }
2757 }
2758
2759 if (!right_has_empty) {
2760 /*
2761 * Only do this if we're moving a real
2762 * record. Otherwise, the action is delayed until
2763 * after removal of the right path in which case we
2764 * can do a simple shift to remove the empty extent.
2765 */
2766 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2767 memset(&right_leaf_el->l_recs[0], 0,
2768 sizeof(struct ocfs2_extent_rec));
2769 }
2770 if (eb->h_next_leaf_blk == 0ULL) {
2771 /*
2772 * Move recs over to get rid of empty extent, decrease
2773 * next_free. This is allowed to remove the last
2774 * extent in our leaf (setting l_next_free_rec to
2775 * zero) - the delete code below won't care.
2776 */
2777 ocfs2_remove_empty_extent(right_leaf_el);
2778 }
2779
Joel Beckerec20cec2010-03-19 14:13:52 -07002780 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2781 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
Mark Fasheh328d5752007-06-18 10:48:04 -07002782
2783 if (del_right_subtree) {
Joel Becker6641b0c2009-02-12 18:57:52 -08002784 ocfs2_unlink_subtree(handle, et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002785 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08002786 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08002787 left_path);
2788 if (ret) {
2789 mlog_errno(ret);
2790 goto out;
2791 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002792
2793 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002794 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002795
2796 /*
2797 * Removal of the extent in the left leaf was skipped
2798 * above so we could delete the right path
2799 * 1st.
2800 */
2801 if (right_has_empty)
2802 ocfs2_remove_empty_extent(left_leaf_el);
2803
Joel Beckerec20cec2010-03-19 14:13:52 -07002804 ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002805
2806 *deleted = 1;
2807 } else
Joel Becker4619c732009-02-12 19:02:36 -08002808 ocfs2_complete_edge_insert(handle, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002809 subtree_index);
2810
2811out:
2812 return ret;
2813}
2814
2815/*
2816 * Given a full path, determine what cpos value would return us a path
2817 * containing the leaf immediately to the right of the current one.
2818 *
2819 * Will return zero if the path passed in is already the rightmost path.
2820 *
2821 * This looks similar, but is subtly different to
2822 * ocfs2_find_cpos_for_left_leaf().
2823 */
Tao Ma38a04e42009-11-30 14:32:19 +08002824int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2825 struct ocfs2_path *path, u32 *cpos)
Mark Fasheh328d5752007-06-18 10:48:04 -07002826{
2827 int i, j, ret = 0;
2828 u64 blkno;
2829 struct ocfs2_extent_list *el;
2830
2831 *cpos = 0;
2832
2833 if (path->p_tree_depth == 0)
2834 return 0;
2835
2836 blkno = path_leaf_bh(path)->b_blocknr;
2837
2838 /* Start at the tree node just above the leaf and work our way up. */
2839 i = path->p_tree_depth - 1;
2840 while (i >= 0) {
2841 int next_free;
2842
2843 el = path->p_node[i].el;
2844
2845 /*
2846 * Find the extent record just after the one in our
2847 * path.
2848 */
2849 next_free = le16_to_cpu(el->l_next_free_rec);
2850 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2851 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2852 if (j == (next_free - 1)) {
2853 if (i == 0) {
2854 /*
2855 * We've determined that the
2856 * path specified is already
2857 * the rightmost one - return a
2858 * cpos of zero.
2859 */
2860 goto out;
2861 }
2862 /*
2863 * The rightmost record points to our
2864 * leaf - we need to travel up the
2865 * tree one level.
2866 */
2867 goto next_node;
2868 }
2869
2870 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2871 goto out;
2872 }
2873 }
2874
2875 /*
2876 * If we got here, we never found a valid node where
2877 * the tree indicated one should be.
2878 */
2879 ocfs2_error(sb,
2880 "Invalid extent tree at extent block %llu\n",
2881 (unsigned long long)blkno);
2882 ret = -EROFS;
2883 goto out;
2884
2885next_node:
2886 blkno = path->p_node[i].bh->b_blocknr;
2887 i--;
2888 }
2889
2890out:
2891 return ret;
2892}
2893
Joel Becker70f18c02009-02-13 02:09:31 -08002894static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2895 struct ocfs2_extent_tree *et,
Joel Becker13723d02008-10-17 19:25:01 -07002896 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002897{
2898 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002899 struct buffer_head *bh = path_leaf_bh(path);
2900 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002901
2902 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2903 return 0;
2904
Joel Becker70f18c02009-02-13 02:09:31 -08002905 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Joel Becker13723d02008-10-17 19:25:01 -07002906 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002907 if (ret) {
2908 mlog_errno(ret);
2909 goto out;
2910 }
2911
2912 ocfs2_remove_empty_extent(el);
Joel Beckerec20cec2010-03-19 14:13:52 -07002913 ocfs2_journal_dirty(handle, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002914
2915out:
2916 return ret;
2917}
2918
Joel Beckere46f74d2009-02-12 19:47:43 -08002919static int __ocfs2_rotate_tree_left(handle_t *handle,
2920 struct ocfs2_extent_tree *et,
2921 int orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07002922 struct ocfs2_path *path,
2923 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08002924 struct ocfs2_path **empty_extent_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002925{
2926 int ret, subtree_root, deleted;
2927 u32 right_cpos;
2928 struct ocfs2_path *left_path = NULL;
2929 struct ocfs2_path *right_path = NULL;
Joel Beckere46f74d2009-02-12 19:47:43 -08002930 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fasheh328d5752007-06-18 10:48:04 -07002931
2932 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2933
2934 *empty_extent_path = NULL;
2935
Joel Beckere46f74d2009-02-12 19:47:43 -08002936 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002937 if (ret) {
2938 mlog_errno(ret);
2939 goto out;
2940 }
2941
Joel Beckerffdd7a52008-10-17 22:32:01 -07002942 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002943 if (!left_path) {
2944 ret = -ENOMEM;
2945 mlog_errno(ret);
2946 goto out;
2947 }
2948
2949 ocfs2_cp_path(left_path, path);
2950
Joel Beckerffdd7a52008-10-17 22:32:01 -07002951 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002952 if (!right_path) {
2953 ret = -ENOMEM;
2954 mlog_errno(ret);
2955 goto out;
2956 }
2957
2958 while (right_cpos) {
Joel Beckerfacdb772009-02-12 18:08:48 -08002959 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002960 if (ret) {
2961 mlog_errno(ret);
2962 goto out;
2963 }
2964
Joel Becker7dc02802009-02-12 19:20:13 -08002965 subtree_root = ocfs2_find_subtree_root(et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002966 right_path);
2967
2968 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2969 subtree_root,
2970 (unsigned long long)
2971 right_path->p_node[subtree_root].bh->b_blocknr,
2972 right_path->p_tree_depth);
2973
2974 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2975 orig_credits, left_path);
2976 if (ret) {
2977 mlog_errno(ret);
2978 goto out;
2979 }
2980
Mark Fashehe8aed342007-12-03 16:43:01 -08002981 /*
2982 * Caller might still want to make changes to the
2983 * tree root, so re-add it to the journal here.
2984 */
Joel Beckere46f74d2009-02-12 19:47:43 -08002985 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002986 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08002987 if (ret) {
2988 mlog_errno(ret);
2989 goto out;
2990 }
2991
Joel Becker1e2dd632009-02-12 19:45:28 -08002992 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002993 right_path, subtree_root,
Joel Becker1e2dd632009-02-12 19:45:28 -08002994 dealloc, &deleted);
Mark Fasheh328d5752007-06-18 10:48:04 -07002995 if (ret == -EAGAIN) {
2996 /*
2997 * The rotation has to temporarily stop due to
2998 * the right subtree having an empty
2999 * extent. Pass it back to the caller for a
3000 * fixup.
3001 */
3002 *empty_extent_path = right_path;
3003 right_path = NULL;
3004 goto out;
3005 }
3006 if (ret) {
3007 mlog_errno(ret);
3008 goto out;
3009 }
3010
3011 /*
3012 * The subtree rotate might have removed records on
3013 * the rightmost edge. If so, then rotation is
3014 * complete.
3015 */
3016 if (deleted)
3017 break;
3018
3019 ocfs2_mv_path(left_path, right_path);
3020
Joel Beckere46f74d2009-02-12 19:47:43 -08003021 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003022 &right_cpos);
3023 if (ret) {
3024 mlog_errno(ret);
3025 goto out;
3026 }
3027 }
3028
3029out:
3030 ocfs2_free_path(right_path);
3031 ocfs2_free_path(left_path);
3032
3033 return ret;
3034}
3035
Joel Becker70f18c02009-02-13 02:09:31 -08003036static int ocfs2_remove_rightmost_path(handle_t *handle,
3037 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003038 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003039 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003040{
3041 int ret, subtree_index;
3042 u32 cpos;
3043 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003044 struct ocfs2_extent_block *eb;
3045 struct ocfs2_extent_list *el;
3046
Mark Fasheh328d5752007-06-18 10:48:04 -07003047
Joel Becker6136ca52009-02-12 19:32:43 -08003048 ret = ocfs2_et_sanity_check(et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003049 if (ret)
3050 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003051 /*
3052 * There's two ways we handle this depending on
3053 * whether path is the only existing one.
3054 */
3055 ret = ocfs2_extend_rotate_transaction(handle, 0,
3056 handle->h_buffer_credits,
3057 path);
3058 if (ret) {
3059 mlog_errno(ret);
3060 goto out;
3061 }
3062
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003063 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003064 if (ret) {
3065 mlog_errno(ret);
3066 goto out;
3067 }
3068
Joel Becker3d03a302009-02-12 17:49:26 -08003069 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3070 path, &cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003071 if (ret) {
3072 mlog_errno(ret);
3073 goto out;
3074 }
3075
3076 if (cpos) {
3077 /*
3078 * We have a path to the left of this one - it needs
3079 * an update too.
3080 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003081 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003082 if (!left_path) {
3083 ret = -ENOMEM;
3084 mlog_errno(ret);
3085 goto out;
3086 }
3087
Joel Beckerfacdb772009-02-12 18:08:48 -08003088 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003089 if (ret) {
3090 mlog_errno(ret);
3091 goto out;
3092 }
3093
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003094 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003095 if (ret) {
3096 mlog_errno(ret);
3097 goto out;
3098 }
3099
Joel Becker7dc02802009-02-12 19:20:13 -08003100 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003101
Joel Becker6641b0c2009-02-12 18:57:52 -08003102 ocfs2_unlink_subtree(handle, et, left_path, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003103 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08003104 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08003105 left_path);
3106 if (ret) {
3107 mlog_errno(ret);
3108 goto out;
3109 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003110
3111 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003112 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003113 } else {
3114 /*
3115 * 'path' is also the leftmost path which
3116 * means it must be the only one. This gets
3117 * handled differently because we want to
Joel Becker70f18c02009-02-13 02:09:31 -08003118 * revert the root back to having extents
Mark Fasheh328d5752007-06-18 10:48:04 -07003119 * in-line.
3120 */
Joel Becker6641b0c2009-02-12 18:57:52 -08003121 ocfs2_unlink_path(handle, et, dealloc, path, 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003122
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003123 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003124 el->l_tree_depth = 0;
3125 el->l_next_free_rec = 0;
3126 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3127
Joel Becker35dc0aa2008-08-20 16:25:06 -07003128 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003129 }
3130
3131 ocfs2_journal_dirty(handle, path_root_bh(path));
3132
3133out:
3134 ocfs2_free_path(left_path);
3135 return ret;
3136}
3137
3138/*
3139 * Left rotation of btree records.
3140 *
3141 * In many ways, this is (unsurprisingly) the opposite of right
3142 * rotation. We start at some non-rightmost path containing an empty
3143 * extent in the leaf block. The code works its way to the rightmost
3144 * path by rotating records to the left in every subtree.
3145 *
3146 * This is used by any code which reduces the number of extent records
3147 * in a leaf. After removal, an empty record should be placed in the
3148 * leftmost list position.
3149 *
3150 * This won't handle a length update of the rightmost path records if
3151 * the rightmost tree leaf record is removed so the caller is
3152 * responsible for detecting and correcting that.
3153 */
Joel Becker70f18c02009-02-13 02:09:31 -08003154static int ocfs2_rotate_tree_left(handle_t *handle,
3155 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003156 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003157 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003158{
3159 int ret, orig_credits = handle->h_buffer_credits;
3160 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3161 struct ocfs2_extent_block *eb;
3162 struct ocfs2_extent_list *el;
3163
3164 el = path_leaf_el(path);
3165 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3166 return 0;
3167
3168 if (path->p_tree_depth == 0) {
3169rightmost_no_delete:
3170 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003171 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003172 * it up front.
3173 */
Joel Becker70f18c02009-02-13 02:09:31 -08003174 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003175 if (ret)
3176 mlog_errno(ret);
3177 goto out;
3178 }
3179
3180 /*
3181 * Handle rightmost branch now. There's several cases:
3182 * 1) simple rotation leaving records in there. That's trivial.
3183 * 2) rotation requiring a branch delete - there's no more
3184 * records left. Two cases of this:
3185 * a) There are branches to the left.
3186 * b) This is also the leftmost (the only) branch.
3187 *
3188 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3189 * 2a) we need the left branch so that we can update it with the unlink
Joel Becker70f18c02009-02-13 02:09:31 -08003190 * 2b) we need to bring the root back to inline extents.
Mark Fasheh328d5752007-06-18 10:48:04 -07003191 */
3192
3193 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3194 el = &eb->h_list;
3195 if (eb->h_next_leaf_blk == 0) {
3196 /*
3197 * This gets a bit tricky if we're going to delete the
3198 * rightmost path. Get the other cases out of the way
3199 * 1st.
3200 */
3201 if (le16_to_cpu(el->l_next_free_rec) > 1)
3202 goto rightmost_no_delete;
3203
3204 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3205 ret = -EIO;
Joel Becker70f18c02009-02-13 02:09:31 -08003206 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3207 "Owner %llu has empty extent block at %llu",
3208 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07003209 (unsigned long long)le64_to_cpu(eb->h_blkno));
3210 goto out;
3211 }
3212
3213 /*
3214 * XXX: The caller can not trust "path" any more after
3215 * this as it will have been deleted. What do we do?
3216 *
3217 * In theory the rotate-for-merge code will never get
3218 * here because it'll always ask for a rotate in a
3219 * nonempty list.
3220 */
3221
Joel Becker70f18c02009-02-13 02:09:31 -08003222 ret = ocfs2_remove_rightmost_path(handle, et, path,
3223 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003224 if (ret)
3225 mlog_errno(ret);
3226 goto out;
3227 }
3228
3229 /*
3230 * Now we can loop, remembering the path we get from -EAGAIN
3231 * and restarting from there.
3232 */
3233try_rotate:
Joel Beckere46f74d2009-02-12 19:47:43 -08003234 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3235 dealloc, &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003236 if (ret && ret != -EAGAIN) {
3237 mlog_errno(ret);
3238 goto out;
3239 }
3240
3241 while (ret == -EAGAIN) {
3242 tmp_path = restart_path;
3243 restart_path = NULL;
3244
Joel Beckere46f74d2009-02-12 19:47:43 -08003245 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07003246 tmp_path, dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08003247 &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003248 if (ret && ret != -EAGAIN) {
3249 mlog_errno(ret);
3250 goto out;
3251 }
3252
3253 ocfs2_free_path(tmp_path);
3254 tmp_path = NULL;
3255
3256 if (ret == 0)
3257 goto try_rotate;
3258 }
3259
3260out:
3261 ocfs2_free_path(tmp_path);
3262 ocfs2_free_path(restart_path);
3263 return ret;
3264}
3265
3266static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3267 int index)
3268{
3269 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3270 unsigned int size;
3271
3272 if (rec->e_leaf_clusters == 0) {
3273 /*
3274 * We consumed all of the merged-from record. An empty
3275 * extent cannot exist anywhere but the 1st array
3276 * position, so move things over if the merged-from
3277 * record doesn't occupy that position.
3278 *
3279 * This creates a new empty extent so the caller
3280 * should be smart enough to have removed any existing
3281 * ones.
3282 */
3283 if (index > 0) {
3284 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3285 size = index * sizeof(struct ocfs2_extent_rec);
3286 memmove(&el->l_recs[1], &el->l_recs[0], size);
3287 }
3288
3289 /*
3290 * Always memset - the caller doesn't check whether it
3291 * created an empty extent, so there could be junk in
3292 * the other fields.
3293 */
3294 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3295 }
3296}
3297
Joel Becker4fe82c32009-02-13 02:16:08 -08003298static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003299 struct ocfs2_path *left_path,
3300 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003301{
3302 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003303 u32 right_cpos;
3304 struct ocfs2_path *right_path = NULL;
3305 struct ocfs2_extent_list *left_el;
3306
3307 *ret_right_path = NULL;
3308
3309 /* This function shouldn't be called for non-trees. */
3310 BUG_ON(left_path->p_tree_depth == 0);
3311
3312 left_el = path_leaf_el(left_path);
3313 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3314
Joel Becker4fe82c32009-02-13 02:16:08 -08003315 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3316 left_path, &right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003317 if (ret) {
3318 mlog_errno(ret);
3319 goto out;
3320 }
3321
3322 /* This function shouldn't be called for the rightmost leaf. */
3323 BUG_ON(right_cpos == 0);
3324
Joel Beckerffdd7a52008-10-17 22:32:01 -07003325 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003326 if (!right_path) {
3327 ret = -ENOMEM;
3328 mlog_errno(ret);
3329 goto out;
3330 }
3331
Joel Becker4fe82c32009-02-13 02:16:08 -08003332 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003333 if (ret) {
3334 mlog_errno(ret);
3335 goto out;
3336 }
3337
3338 *ret_right_path = right_path;
3339out:
3340 if (ret)
3341 ocfs2_free_path(right_path);
3342 return ret;
3343}
3344
3345/*
3346 * Remove split_rec clusters from the record at index and merge them
3347 * onto the beginning of the record "next" to it.
3348 * For index < l_count - 1, the next means the extent rec at index + 1.
3349 * For index == l_count - 1, the "next" means the 1st extent rec of the
3350 * next extent block.
3351 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003352static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003353 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003354 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003355 struct ocfs2_extent_rec *split_rec,
3356 int index)
3357{
3358 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003359 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3360 struct ocfs2_extent_rec *left_rec;
3361 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003362 struct ocfs2_extent_list *right_el;
3363 struct ocfs2_path *right_path = NULL;
3364 int subtree_index = 0;
3365 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3366 struct buffer_head *bh = path_leaf_bh(left_path);
3367 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003368
3369 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003370 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003371
Al Viro9d8df6a2008-05-21 06:32:11 +01003372 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003373 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3374 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003375 ret = ocfs2_get_right_path(et, left_path, &right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003376 if (ret) {
3377 mlog_errno(ret);
3378 goto out;
3379 }
3380
3381 right_el = path_leaf_el(right_path);
3382 next_free = le16_to_cpu(right_el->l_next_free_rec);
3383 BUG_ON(next_free <= 0);
3384 right_rec = &right_el->l_recs[0];
3385 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003386 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003387 right_rec = &right_el->l_recs[1];
3388 }
3389
3390 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3391 le16_to_cpu(left_rec->e_leaf_clusters) !=
3392 le32_to_cpu(right_rec->e_cpos));
3393
Joel Becker7dc02802009-02-12 19:20:13 -08003394 subtree_index = ocfs2_find_subtree_root(et, left_path,
3395 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003396
3397 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3398 handle->h_buffer_credits,
3399 right_path);
3400 if (ret) {
3401 mlog_errno(ret);
3402 goto out;
3403 }
3404
3405 root_bh = left_path->p_node[subtree_index].bh;
3406 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3407
Joel Becker7dc02802009-02-12 19:20:13 -08003408 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003409 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003410 if (ret) {
3411 mlog_errno(ret);
3412 goto out;
3413 }
3414
3415 for (i = subtree_index + 1;
3416 i < path_num_items(right_path); i++) {
Joel Becker7dc02802009-02-12 19:20:13 -08003417 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003418 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003419 if (ret) {
3420 mlog_errno(ret);
3421 goto out;
3422 }
3423
Joel Becker7dc02802009-02-12 19:20:13 -08003424 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003425 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003426 if (ret) {
3427 mlog_errno(ret);
3428 goto out;
3429 }
3430 }
3431
3432 } else {
3433 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3434 right_rec = &el->l_recs[index + 1];
3435 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003436
Joel Becker7dc02802009-02-12 19:20:13 -08003437 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
Joel Becker13723d02008-10-17 19:25:01 -07003438 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003439 if (ret) {
3440 mlog_errno(ret);
3441 goto out;
3442 }
3443
3444 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3445
3446 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3447 le64_add_cpu(&right_rec->e_blkno,
Joel Becker7dc02802009-02-12 19:20:13 -08003448 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3449 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003450 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3451
3452 ocfs2_cleanup_merge(el, index);
3453
Joel Beckerec20cec2010-03-19 14:13:52 -07003454 ocfs2_journal_dirty(handle, bh);
Tao Ma677b9752008-01-30 14:21:05 +08003455 if (right_path) {
Joel Beckerec20cec2010-03-19 14:13:52 -07003456 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
Joel Becker4619c732009-02-12 19:02:36 -08003457 ocfs2_complete_edge_insert(handle, left_path, right_path,
3458 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003459 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003460out:
Tao Ma677b9752008-01-30 14:21:05 +08003461 if (right_path)
3462 ocfs2_free_path(right_path);
3463 return ret;
3464}
3465
Joel Becker4fe82c32009-02-13 02:16:08 -08003466static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003467 struct ocfs2_path *right_path,
3468 struct ocfs2_path **ret_left_path)
3469{
3470 int ret;
3471 u32 left_cpos;
3472 struct ocfs2_path *left_path = NULL;
3473
3474 *ret_left_path = NULL;
3475
3476 /* This function shouldn't be called for non-trees. */
3477 BUG_ON(right_path->p_tree_depth == 0);
3478
Joel Becker4fe82c32009-02-13 02:16:08 -08003479 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
Tao Ma677b9752008-01-30 14:21:05 +08003480 right_path, &left_cpos);
3481 if (ret) {
3482 mlog_errno(ret);
3483 goto out;
3484 }
3485
3486 /* This function shouldn't be called for the leftmost leaf. */
3487 BUG_ON(left_cpos == 0);
3488
Joel Beckerffdd7a52008-10-17 22:32:01 -07003489 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003490 if (!left_path) {
3491 ret = -ENOMEM;
3492 mlog_errno(ret);
3493 goto out;
3494 }
3495
Joel Becker4fe82c32009-02-13 02:16:08 -08003496 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003497 if (ret) {
3498 mlog_errno(ret);
3499 goto out;
3500 }
3501
3502 *ret_left_path = left_path;
3503out:
3504 if (ret)
3505 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003506 return ret;
3507}
3508
3509/*
3510 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003511 * onto the tail of the record "before" it.
3512 * For index > 0, the "before" means the extent rec at index - 1.
3513 *
3514 * For index == 0, the "before" means the last record of the previous
3515 * extent block. And there is also a situation that we may need to
3516 * remove the rightmost leaf extent block in the right_path and change
3517 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003518 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003519static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003520 handle_t *handle,
Joel Becker4fe82c32009-02-13 02:16:08 -08003521 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003522 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003523 struct ocfs2_cached_dealloc_ctxt *dealloc,
3524 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003525{
Tao Ma677b9752008-01-30 14:21:05 +08003526 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003527 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3528 struct ocfs2_extent_rec *left_rec;
3529 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003530 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3531 struct buffer_head *bh = path_leaf_bh(right_path);
3532 struct buffer_head *root_bh = NULL;
3533 struct ocfs2_path *left_path = NULL;
3534 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003535
Tao Ma677b9752008-01-30 14:21:05 +08003536 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003537
Mark Fasheh328d5752007-06-18 10:48:04 -07003538 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003539 if (index == 0) {
3540 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003541 ret = ocfs2_get_left_path(et, right_path, &left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003542 if (ret) {
3543 mlog_errno(ret);
3544 goto out;
3545 }
3546
3547 left_el = path_leaf_el(left_path);
3548 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3549 le16_to_cpu(left_el->l_count));
3550
3551 left_rec = &left_el->l_recs[
3552 le16_to_cpu(left_el->l_next_free_rec) - 1];
3553 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3554 le16_to_cpu(left_rec->e_leaf_clusters) !=
3555 le32_to_cpu(split_rec->e_cpos));
3556
Joel Becker7dc02802009-02-12 19:20:13 -08003557 subtree_index = ocfs2_find_subtree_root(et, left_path,
3558 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003559
3560 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3561 handle->h_buffer_credits,
3562 left_path);
3563 if (ret) {
3564 mlog_errno(ret);
3565 goto out;
3566 }
3567
3568 root_bh = left_path->p_node[subtree_index].bh;
3569 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3570
Joel Becker4fe82c32009-02-13 02:16:08 -08003571 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003572 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003573 if (ret) {
3574 mlog_errno(ret);
3575 goto out;
3576 }
3577
3578 for (i = subtree_index + 1;
3579 i < path_num_items(right_path); i++) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003580 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003581 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003582 if (ret) {
3583 mlog_errno(ret);
3584 goto out;
3585 }
3586
Joel Becker4fe82c32009-02-13 02:16:08 -08003587 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003588 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003589 if (ret) {
3590 mlog_errno(ret);
3591 goto out;
3592 }
3593 }
3594 } else {
3595 left_rec = &el->l_recs[index - 1];
3596 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3597 has_empty_extent = 1;
3598 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003599
Joel Becker4fe82c32009-02-13 02:16:08 -08003600 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Tao Ma9047bea2009-01-05 14:45:24 +08003601 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003602 if (ret) {
3603 mlog_errno(ret);
3604 goto out;
3605 }
3606
3607 if (has_empty_extent && index == 1) {
3608 /*
3609 * The easy case - we can just plop the record right in.
3610 */
3611 *left_rec = *split_rec;
3612
3613 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003614 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003615 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003616
3617 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3618 le64_add_cpu(&right_rec->e_blkno,
Joel Becker4fe82c32009-02-13 02:16:08 -08003619 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3620 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003621 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3622
3623 ocfs2_cleanup_merge(el, index);
3624
Joel Beckerec20cec2010-03-19 14:13:52 -07003625 ocfs2_journal_dirty(handle, bh);
Tao Ma677b9752008-01-30 14:21:05 +08003626 if (left_path) {
Joel Beckerec20cec2010-03-19 14:13:52 -07003627 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
Tao Ma677b9752008-01-30 14:21:05 +08003628
3629 /*
3630 * In the situation that the right_rec is empty and the extent
3631 * block is empty also, ocfs2_complete_edge_insert can't handle
3632 * it and we need to delete the right extent block.
3633 */
3634 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3635 le16_to_cpu(el->l_next_free_rec) == 1) {
3636
Joel Becker70f18c02009-02-13 02:09:31 -08003637 ret = ocfs2_remove_rightmost_path(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003638 right_path,
Joel Becker70f18c02009-02-13 02:09:31 -08003639 dealloc);
Tao Ma677b9752008-01-30 14:21:05 +08003640 if (ret) {
3641 mlog_errno(ret);
3642 goto out;
3643 }
3644
3645 /* Now the rightmost extent block has been deleted.
3646 * So we use the new rightmost path.
3647 */
3648 ocfs2_mv_path(right_path, left_path);
3649 left_path = NULL;
3650 } else
Joel Becker4619c732009-02-12 19:02:36 -08003651 ocfs2_complete_edge_insert(handle, left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003652 right_path, subtree_index);
3653 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003654out:
Tao Ma677b9752008-01-30 14:21:05 +08003655 if (left_path)
3656 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003657 return ret;
3658}
3659
Joel Beckerc495dd22009-02-13 02:19:11 -08003660static int ocfs2_try_to_merge_extent(handle_t *handle,
3661 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003662 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003663 int split_index,
3664 struct ocfs2_extent_rec *split_rec,
3665 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckerc495dd22009-02-13 02:19:11 -08003666 struct ocfs2_merge_ctxt *ctxt)
Mark Fasheh328d5752007-06-18 10:48:04 -07003667{
Tao Mao518d7262007-08-28 17:25:35 -07003668 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003669 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003670 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3671
3672 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3673
Tao Mao518d7262007-08-28 17:25:35 -07003674 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3675 /*
3676 * The merge code will need to create an empty
3677 * extent to take the place of the newly
3678 * emptied slot. Remove any pre-existing empty
3679 * extents - having more than one in a leaf is
3680 * illegal.
3681 */
Joel Becker70f18c02009-02-13 02:09:31 -08003682 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Tao Mao518d7262007-08-28 17:25:35 -07003683 if (ret) {
3684 mlog_errno(ret);
3685 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003686 }
Tao Mao518d7262007-08-28 17:25:35 -07003687 split_index--;
3688 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003689 }
3690
3691 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3692 /*
3693 * Left-right contig implies this.
3694 */
3695 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003696
3697 /*
3698 * Since the leftright insert always covers the entire
3699 * extent, this call will delete the insert record
3700 * entirely, resulting in an empty extent record added to
3701 * the extent block.
3702 *
3703 * Since the adding of an empty extent shifts
3704 * everything back to the right, there's no need to
3705 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003706 *
3707 * When the split_index is zero, we need to merge it to the
3708 * prevoius extent block. It is more efficient and easier
3709 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003710 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003711 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003712 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003713 if (ret) {
3714 mlog_errno(ret);
3715 goto out;
3716 }
3717
3718 /*
3719 * We can only get this from logic error above.
3720 */
3721 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3722
Tao Ma677b9752008-01-30 14:21:05 +08003723 /* The merge left us with an empty extent, remove it. */
Joel Becker70f18c02009-02-13 02:09:31 -08003724 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003725 if (ret) {
3726 mlog_errno(ret);
3727 goto out;
3728 }
Tao Ma677b9752008-01-30 14:21:05 +08003729
Mark Fasheh328d5752007-06-18 10:48:04 -07003730 rec = &el->l_recs[split_index];
3731
3732 /*
3733 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003734 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003735 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003736 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3737 dealloc, split_index);
Tao Ma677b9752008-01-30 14:21:05 +08003738
Mark Fasheh328d5752007-06-18 10:48:04 -07003739 if (ret) {
3740 mlog_errno(ret);
3741 goto out;
3742 }
3743
Joel Becker70f18c02009-02-13 02:09:31 -08003744 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003745 /*
3746 * Error from this last rotate is not critical, so
3747 * print but don't bubble it up.
3748 */
3749 if (ret)
3750 mlog_errno(ret);
3751 ret = 0;
3752 } else {
3753 /*
3754 * Merge a record to the left or right.
3755 *
3756 * 'contig_type' is relative to the existing record,
3757 * so for example, if we're "right contig", it's to
3758 * the record on the left (hence the left merge).
3759 */
3760 if (ctxt->c_contig_type == CONTIG_RIGHT) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003761 ret = ocfs2_merge_rec_left(path, handle, et,
3762 split_rec, dealloc,
Mark Fasheh328d5752007-06-18 10:48:04 -07003763 split_index);
3764 if (ret) {
3765 mlog_errno(ret);
3766 goto out;
3767 }
3768 } else {
Joel Becker4fe82c32009-02-13 02:16:08 -08003769 ret = ocfs2_merge_rec_right(path, handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003770 et, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003771 split_index);
3772 if (ret) {
3773 mlog_errno(ret);
3774 goto out;
3775 }
3776 }
3777
3778 if (ctxt->c_split_covers_rec) {
3779 /*
3780 * The merge may have left an empty extent in
3781 * our leaf. Try to rotate it away.
3782 */
Joel Becker70f18c02009-02-13 02:09:31 -08003783 ret = ocfs2_rotate_tree_left(handle, et, path,
3784 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003785 if (ret)
3786 mlog_errno(ret);
3787 ret = 0;
3788 }
3789 }
3790
3791out:
3792 return ret;
3793}
3794
3795static void ocfs2_subtract_from_rec(struct super_block *sb,
3796 enum ocfs2_split_type split,
3797 struct ocfs2_extent_rec *rec,
3798 struct ocfs2_extent_rec *split_rec)
3799{
3800 u64 len_blocks;
3801
3802 len_blocks = ocfs2_clusters_to_blocks(sb,
3803 le16_to_cpu(split_rec->e_leaf_clusters));
3804
3805 if (split == SPLIT_LEFT) {
3806 /*
3807 * Region is on the left edge of the existing
3808 * record.
3809 */
3810 le32_add_cpu(&rec->e_cpos,
3811 le16_to_cpu(split_rec->e_leaf_clusters));
3812 le64_add_cpu(&rec->e_blkno, len_blocks);
3813 le16_add_cpu(&rec->e_leaf_clusters,
3814 -le16_to_cpu(split_rec->e_leaf_clusters));
3815 } else {
3816 /*
3817 * Region is on the right edge of the existing
3818 * record.
3819 */
3820 le16_add_cpu(&rec->e_leaf_clusters,
3821 -le16_to_cpu(split_rec->e_leaf_clusters));
3822 }
3823}
3824
Mark Fashehdcd05382007-01-16 11:32:23 -08003825/*
3826 * Do the final bits of extent record insertion at the target leaf
3827 * list. If this leaf is part of an allocation tree, it is assumed
3828 * that the tree above has been prepared.
3829 */
Joel Beckerd5628622009-02-13 02:54:36 -08003830static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3831 struct ocfs2_extent_rec *insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08003832 struct ocfs2_extent_list *el,
Joel Beckerd5628622009-02-13 02:54:36 -08003833 struct ocfs2_insert_type *insert)
Mark Fashehdcd05382007-01-16 11:32:23 -08003834{
3835 int i = insert->ins_contig_index;
3836 unsigned int range;
3837 struct ocfs2_extent_rec *rec;
3838
Mark Fashehe48edee2007-03-07 16:46:57 -08003839 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003840
Mark Fasheh328d5752007-06-18 10:48:04 -07003841 if (insert->ins_split != SPLIT_NONE) {
3842 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3843 BUG_ON(i == -1);
3844 rec = &el->l_recs[i];
Joel Beckerd5628622009-02-13 02:54:36 -08003845 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3846 insert->ins_split, rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003847 insert_rec);
3848 goto rotate;
3849 }
3850
Mark Fashehdcd05382007-01-16 11:32:23 -08003851 /*
3852 * Contiguous insert - either left or right.
3853 */
3854 if (insert->ins_contig != CONTIG_NONE) {
3855 rec = &el->l_recs[i];
3856 if (insert->ins_contig == CONTIG_LEFT) {
3857 rec->e_blkno = insert_rec->e_blkno;
3858 rec->e_cpos = insert_rec->e_cpos;
3859 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003860 le16_add_cpu(&rec->e_leaf_clusters,
3861 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003862 return;
3863 }
3864
3865 /*
3866 * Handle insert into an empty leaf.
3867 */
3868 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3869 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3870 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3871 el->l_recs[0] = *insert_rec;
3872 el->l_next_free_rec = cpu_to_le16(1);
3873 return;
3874 }
3875
3876 /*
3877 * Appending insert.
3878 */
3879 if (insert->ins_appending == APPEND_TAIL) {
3880 i = le16_to_cpu(el->l_next_free_rec) - 1;
3881 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003882 range = le32_to_cpu(rec->e_cpos)
3883 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003884 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3885
3886 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3887 le16_to_cpu(el->l_count),
Joel Beckerd5628622009-02-13 02:54:36 -08003888 "owner %llu, depth %u, count %u, next free %u, "
Mark Fashehdcd05382007-01-16 11:32:23 -08003889 "rec.cpos %u, rec.clusters %u, "
3890 "insert.cpos %u, insert.clusters %u\n",
Joel Beckerd5628622009-02-13 02:54:36 -08003891 ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08003892 le16_to_cpu(el->l_tree_depth),
3893 le16_to_cpu(el->l_count),
3894 le16_to_cpu(el->l_next_free_rec),
3895 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003896 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003897 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003898 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003899 i++;
3900 el->l_recs[i] = *insert_rec;
3901 le16_add_cpu(&el->l_next_free_rec, 1);
3902 return;
3903 }
3904
Mark Fasheh328d5752007-06-18 10:48:04 -07003905rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003906 /*
3907 * Ok, we have to rotate.
3908 *
3909 * At this point, it is safe to assume that inserting into an
3910 * empty leaf and appending to a leaf have both been handled
3911 * above.
3912 *
3913 * This leaf needs to have space, either by the empty 1st
3914 * extent record, or by virtue of an l_next_rec < l_count.
3915 */
3916 ocfs2_rotate_leaf(el, insert_rec);
3917}
3918
Joel Beckerd401dc12009-02-13 02:24:10 -08003919static void ocfs2_adjust_rightmost_records(handle_t *handle,
3920 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003921 struct ocfs2_path *path,
3922 struct ocfs2_extent_rec *insert_rec)
3923{
3924 int ret, i, next_free;
3925 struct buffer_head *bh;
3926 struct ocfs2_extent_list *el;
3927 struct ocfs2_extent_rec *rec;
3928
3929 /*
3930 * Update everything except the leaf block.
3931 */
3932 for (i = 0; i < path->p_tree_depth; i++) {
3933 bh = path->p_node[i].bh;
3934 el = path->p_node[i].el;
3935
3936 next_free = le16_to_cpu(el->l_next_free_rec);
3937 if (next_free == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08003938 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3939 "Owner %llu has a bad extent list",
3940 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fasheh328d5752007-06-18 10:48:04 -07003941 ret = -EIO;
3942 return;
3943 }
3944
3945 rec = &el->l_recs[next_free - 1];
3946
3947 rec->e_int_clusters = insert_rec->e_cpos;
3948 le32_add_cpu(&rec->e_int_clusters,
3949 le16_to_cpu(insert_rec->e_leaf_clusters));
3950 le32_add_cpu(&rec->e_int_clusters,
3951 -le32_to_cpu(rec->e_cpos));
3952
Joel Beckerec20cec2010-03-19 14:13:52 -07003953 ocfs2_journal_dirty(handle, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07003954 }
3955}
3956
Joel Beckerd401dc12009-02-13 02:24:10 -08003957static int ocfs2_append_rec_to_path(handle_t *handle,
3958 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08003959 struct ocfs2_extent_rec *insert_rec,
3960 struct ocfs2_path *right_path,
3961 struct ocfs2_path **ret_left_path)
3962{
Mark Fasheh328d5752007-06-18 10:48:04 -07003963 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003964 struct ocfs2_extent_list *el;
3965 struct ocfs2_path *left_path = NULL;
3966
3967 *ret_left_path = NULL;
3968
3969 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003970 * This shouldn't happen for non-trees. The extent rec cluster
3971 * count manipulation below only works for interior nodes.
3972 */
3973 BUG_ON(right_path->p_tree_depth == 0);
3974
3975 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003976 * If our appending insert is at the leftmost edge of a leaf,
3977 * then we might need to update the rightmost records of the
3978 * neighboring path.
3979 */
3980 el = path_leaf_el(right_path);
3981 next_free = le16_to_cpu(el->l_next_free_rec);
3982 if (next_free == 0 ||
3983 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3984 u32 left_cpos;
3985
Joel Beckerd401dc12009-02-13 02:24:10 -08003986 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3987 right_path, &left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08003988 if (ret) {
3989 mlog_errno(ret);
3990 goto out;
3991 }
3992
3993 mlog(0, "Append may need a left path update. cpos: %u, "
3994 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3995 left_cpos);
3996
3997 /*
3998 * No need to worry if the append is already in the
3999 * leftmost leaf.
4000 */
4001 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004002 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004003 if (!left_path) {
4004 ret = -ENOMEM;
4005 mlog_errno(ret);
4006 goto out;
4007 }
4008
Joel Beckerd401dc12009-02-13 02:24:10 -08004009 ret = ocfs2_find_path(et->et_ci, left_path,
Joel Beckerfacdb772009-02-12 18:08:48 -08004010 left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004011 if (ret) {
4012 mlog_errno(ret);
4013 goto out;
4014 }
4015
4016 /*
4017 * ocfs2_insert_path() will pass the left_path to the
4018 * journal for us.
4019 */
4020 }
4021 }
4022
Joel Beckerd401dc12009-02-13 02:24:10 -08004023 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004024 if (ret) {
4025 mlog_errno(ret);
4026 goto out;
4027 }
4028
Joel Beckerd401dc12009-02-13 02:24:10 -08004029 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004030
4031 *ret_left_path = left_path;
4032 ret = 0;
4033out:
4034 if (ret != 0)
4035 ocfs2_free_path(left_path);
4036
4037 return ret;
4038}
4039
Joel Beckerc38e52b2009-02-13 02:56:23 -08004040static void ocfs2_split_record(struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004041 struct ocfs2_path *left_path,
4042 struct ocfs2_path *right_path,
4043 struct ocfs2_extent_rec *split_rec,
4044 enum ocfs2_split_type split)
4045{
4046 int index;
4047 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4048 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4049 struct ocfs2_extent_rec *rec, *tmprec;
4050
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004051 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004052 if (left_path)
4053 left_el = path_leaf_el(left_path);
4054
4055 el = right_el;
4056 insert_el = right_el;
4057 index = ocfs2_search_extent_list(el, cpos);
4058 if (index != -1) {
4059 if (index == 0 && left_path) {
4060 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4061
4062 /*
4063 * This typically means that the record
4064 * started in the left path but moved to the
4065 * right as a result of rotation. We either
4066 * move the existing record to the left, or we
4067 * do the later insert there.
4068 *
4069 * In this case, the left path should always
4070 * exist as the rotate code will have passed
4071 * it back for a post-insert update.
4072 */
4073
4074 if (split == SPLIT_LEFT) {
4075 /*
4076 * It's a left split. Since we know
4077 * that the rotate code gave us an
4078 * empty extent in the left path, we
4079 * can just do the insert there.
4080 */
4081 insert_el = left_el;
4082 } else {
4083 /*
4084 * Right split - we have to move the
4085 * existing record over to the left
4086 * leaf. The insert will be into the
4087 * newly created empty extent in the
4088 * right leaf.
4089 */
4090 tmprec = &right_el->l_recs[index];
4091 ocfs2_rotate_leaf(left_el, tmprec);
4092 el = left_el;
4093
4094 memset(tmprec, 0, sizeof(*tmprec));
4095 index = ocfs2_search_extent_list(left_el, cpos);
4096 BUG_ON(index == -1);
4097 }
4098 }
4099 } else {
4100 BUG_ON(!left_path);
4101 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4102 /*
4103 * Left path is easy - we can just allow the insert to
4104 * happen.
4105 */
4106 el = left_el;
4107 insert_el = left_el;
4108 index = ocfs2_search_extent_list(el, cpos);
4109 BUG_ON(index == -1);
4110 }
4111
4112 rec = &el->l_recs[index];
Joel Beckerc38e52b2009-02-13 02:56:23 -08004113 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4114 split, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004115 ocfs2_rotate_leaf(insert_el, split_rec);
4116}
4117
Mark Fashehdcd05382007-01-16 11:32:23 -08004118/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004119 * This function only does inserts on an allocation b-tree. For tree
4120 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004121 *
4122 * right_path is the path we want to do the actual insert
4123 * in. left_path should only be passed in if we need to update that
4124 * portion of the tree after an edge insert.
4125 */
Joel Becker3505bec2009-02-13 02:57:58 -08004126static int ocfs2_insert_path(handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08004127 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004128 struct ocfs2_path *left_path,
4129 struct ocfs2_path *right_path,
4130 struct ocfs2_extent_rec *insert_rec,
4131 struct ocfs2_insert_type *insert)
4132{
4133 int ret, subtree_index;
4134 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004135
Mark Fashehdcd05382007-01-16 11:32:23 -08004136 if (left_path) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004137 /*
4138 * There's a chance that left_path got passed back to
4139 * us without being accounted for in the
4140 * journal. Extend our transaction here to be sure we
4141 * can change those blocks.
4142 */
Tao Mac901fb02010-04-26 14:34:57 +08004143 ret = ocfs2_extend_trans(handle, left_path->p_tree_depth);
Mark Fashehdcd05382007-01-16 11:32:23 -08004144 if (ret < 0) {
4145 mlog_errno(ret);
4146 goto out;
4147 }
4148
Joel Becker7dc02802009-02-12 19:20:13 -08004149 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004150 if (ret < 0) {
4151 mlog_errno(ret);
4152 goto out;
4153 }
4154 }
4155
Mark Fashehe8aed342007-12-03 16:43:01 -08004156 /*
4157 * Pass both paths to the journal. The majority of inserts
4158 * will be touching all components anyway.
4159 */
Joel Becker7dc02802009-02-12 19:20:13 -08004160 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004161 if (ret < 0) {
4162 mlog_errno(ret);
4163 goto out;
4164 }
4165
Mark Fasheh328d5752007-06-18 10:48:04 -07004166 if (insert->ins_split != SPLIT_NONE) {
4167 /*
4168 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004169 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004170 * function sort it all out.
4171 */
Joel Beckerc38e52b2009-02-13 02:56:23 -08004172 ocfs2_split_record(et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004173 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004174
4175 /*
4176 * Split might have modified either leaf and we don't
4177 * have a guarantee that the later edge insert will
4178 * dirty this for us.
4179 */
4180 if (left_path)
Joel Beckerec20cec2010-03-19 14:13:52 -07004181 ocfs2_journal_dirty(handle,
4182 path_leaf_bh(left_path));
Mark Fasheh328d5752007-06-18 10:48:04 -07004183 } else
Joel Beckerd5628622009-02-13 02:54:36 -08004184 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4185 insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004186
Joel Beckerec20cec2010-03-19 14:13:52 -07004187 ocfs2_journal_dirty(handle, leaf_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004188
4189 if (left_path) {
4190 /*
4191 * The rotate code has indicated that we need to fix
4192 * up portions of the tree after the insert.
4193 *
4194 * XXX: Should we extend the transaction here?
4195 */
Joel Becker7dc02802009-02-12 19:20:13 -08004196 subtree_index = ocfs2_find_subtree_root(et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004197 right_path);
Joel Becker4619c732009-02-12 19:02:36 -08004198 ocfs2_complete_edge_insert(handle, left_path, right_path,
4199 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08004200 }
4201
4202 ret = 0;
4203out:
4204 return ret;
4205}
4206
Joel Becker3505bec2009-02-13 02:57:58 -08004207static int ocfs2_do_insert_extent(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004208 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004209 struct ocfs2_extent_rec *insert_rec,
4210 struct ocfs2_insert_type *type)
4211{
4212 int ret, rotate = 0;
4213 u32 cpos;
4214 struct ocfs2_path *right_path = NULL;
4215 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004216 struct ocfs2_extent_list *el;
4217
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004218 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004219
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004220 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004221 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004222 if (ret) {
4223 mlog_errno(ret);
4224 goto out;
4225 }
4226
4227 if (le16_to_cpu(el->l_tree_depth) == 0) {
Joel Beckerd5628622009-02-13 02:54:36 -08004228 ocfs2_insert_at_leaf(et, insert_rec, el, type);
Mark Fashehdcd05382007-01-16 11:32:23 -08004229 goto out_update_clusters;
4230 }
4231
Joel Beckerffdd7a52008-10-17 22:32:01 -07004232 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004233 if (!right_path) {
4234 ret = -ENOMEM;
4235 mlog_errno(ret);
4236 goto out;
4237 }
4238
4239 /*
4240 * Determine the path to start with. Rotations need the
4241 * rightmost path, everything else can go directly to the
4242 * target leaf.
4243 */
4244 cpos = le32_to_cpu(insert_rec->e_cpos);
4245 if (type->ins_appending == APPEND_NONE &&
4246 type->ins_contig == CONTIG_NONE) {
4247 rotate = 1;
4248 cpos = UINT_MAX;
4249 }
4250
Joel Beckerfacdb772009-02-12 18:08:48 -08004251 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004252 if (ret) {
4253 mlog_errno(ret);
4254 goto out;
4255 }
4256
4257 /*
4258 * Rotations and appends need special treatment - they modify
4259 * parts of the tree's above them.
4260 *
4261 * Both might pass back a path immediate to the left of the
4262 * one being inserted to. This will be cause
4263 * ocfs2_insert_path() to modify the rightmost records of
4264 * left_path to account for an edge insert.
4265 *
4266 * XXX: When modifying this code, keep in mind that an insert
4267 * can wind up skipping both of these two special cases...
4268 */
4269 if (rotate) {
Joel Becker1bbf0b82009-02-12 19:42:08 -08004270 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004271 le32_to_cpu(insert_rec->e_cpos),
4272 right_path, &left_path);
4273 if (ret) {
4274 mlog_errno(ret);
4275 goto out;
4276 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004277
4278 /*
4279 * ocfs2_rotate_tree_right() might have extended the
4280 * transaction without re-journaling our tree root.
4281 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004282 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004283 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004284 if (ret) {
4285 mlog_errno(ret);
4286 goto out;
4287 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004288 } else if (type->ins_appending == APPEND_TAIL
4289 && type->ins_contig != CONTIG_LEFT) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004290 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08004291 right_path, &left_path);
4292 if (ret) {
4293 mlog_errno(ret);
4294 goto out;
4295 }
4296 }
4297
Joel Becker3505bec2009-02-13 02:57:58 -08004298 ret = ocfs2_insert_path(handle, et, left_path, right_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004299 insert_rec, type);
4300 if (ret) {
4301 mlog_errno(ret);
4302 goto out;
4303 }
4304
4305out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004306 if (type->ins_split == SPLIT_NONE)
Joel Becker6136ca52009-02-12 19:32:43 -08004307 ocfs2_et_update_clusters(et,
Joel Becker35dc0aa2008-08-20 16:25:06 -07004308 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004309
Joel Beckerec20cec2010-03-19 14:13:52 -07004310 ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004311
4312out:
4313 ocfs2_free_path(left_path);
4314 ocfs2_free_path(right_path);
4315
4316 return ret;
4317}
4318
Mark Fasheh328d5752007-06-18 10:48:04 -07004319static enum ocfs2_contig_type
Joel Beckera2970292009-02-13 03:09:54 -08004320ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4321 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004322 struct ocfs2_extent_list *el, int index,
4323 struct ocfs2_extent_rec *split_rec)
4324{
Tao Maad5a4d72008-01-30 14:21:32 +08004325 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004326 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004327 u32 left_cpos, right_cpos;
4328 struct ocfs2_extent_rec *rec = NULL;
4329 struct ocfs2_extent_list *new_el;
4330 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4331 struct buffer_head *bh;
4332 struct ocfs2_extent_block *eb;
Joel Beckera2970292009-02-13 03:09:54 -08004333 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Tao Maad5a4d72008-01-30 14:21:32 +08004334
4335 if (index > 0) {
4336 rec = &el->l_recs[index - 1];
4337 } else if (path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004338 status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004339 if (status)
4340 goto out;
4341
4342 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004343 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004344 if (!left_path)
4345 goto out;
4346
Joel Beckera2970292009-02-13 03:09:54 -08004347 status = ocfs2_find_path(et->et_ci, left_path,
4348 left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004349 if (status)
4350 goto out;
4351
4352 new_el = path_leaf_el(left_path);
4353
4354 if (le16_to_cpu(new_el->l_next_free_rec) !=
4355 le16_to_cpu(new_el->l_count)) {
4356 bh = path_leaf_bh(left_path);
4357 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004358 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004359 "Extent block #%llu has an "
4360 "invalid l_next_free_rec of "
4361 "%d. It should have "
4362 "matched the l_count of %d",
4363 (unsigned long long)le64_to_cpu(eb->h_blkno),
4364 le16_to_cpu(new_el->l_next_free_rec),
4365 le16_to_cpu(new_el->l_count));
4366 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004367 goto out;
4368 }
4369 rec = &new_el->l_recs[
4370 le16_to_cpu(new_el->l_next_free_rec) - 1];
4371 }
4372 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004373
4374 /*
4375 * We're careful to check for an empty extent record here -
4376 * the merge code will know what to do if it sees one.
4377 */
Tao Maad5a4d72008-01-30 14:21:32 +08004378 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004379 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4380 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4381 ret = CONTIG_RIGHT;
4382 } else {
Tao Ma853a3a12009-08-18 11:22:18 +08004383 ret = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004384 }
4385 }
4386
Tao Maad5a4d72008-01-30 14:21:32 +08004387 rec = NULL;
4388 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4389 rec = &el->l_recs[index + 1];
4390 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4391 path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004392 status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004393 if (status)
4394 goto out;
4395
4396 if (right_cpos == 0)
4397 goto out;
4398
Joel Beckerffdd7a52008-10-17 22:32:01 -07004399 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004400 if (!right_path)
4401 goto out;
4402
Joel Beckera2970292009-02-13 03:09:54 -08004403 status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004404 if (status)
4405 goto out;
4406
4407 new_el = path_leaf_el(right_path);
4408 rec = &new_el->l_recs[0];
4409 if (ocfs2_is_empty_extent(rec)) {
4410 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4411 bh = path_leaf_bh(right_path);
4412 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004413 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004414 "Extent block #%llu has an "
4415 "invalid l_next_free_rec of %d",
4416 (unsigned long long)le64_to_cpu(eb->h_blkno),
4417 le16_to_cpu(new_el->l_next_free_rec));
4418 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004419 goto out;
4420 }
4421 rec = &new_el->l_recs[1];
4422 }
4423 }
4424
4425 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004426 enum ocfs2_contig_type contig_type;
4427
Tao Ma853a3a12009-08-18 11:22:18 +08004428 contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004429
4430 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4431 ret = CONTIG_LEFTRIGHT;
4432 else if (ret == CONTIG_NONE)
4433 ret = contig_type;
4434 }
4435
Tao Maad5a4d72008-01-30 14:21:32 +08004436out:
4437 if (left_path)
4438 ocfs2_free_path(left_path);
4439 if (right_path)
4440 ocfs2_free_path(right_path);
4441
Mark Fasheh328d5752007-06-18 10:48:04 -07004442 return ret;
4443}
4444
Joel Becker1ef61b32009-02-13 03:12:33 -08004445static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004446 struct ocfs2_insert_type *insert,
4447 struct ocfs2_extent_list *el,
Joel Becker1ef61b32009-02-13 03:12:33 -08004448 struct ocfs2_extent_rec *insert_rec)
Mark Fashehdcd05382007-01-16 11:32:23 -08004449{
4450 int i;
4451 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4452
Mark Fashehe48edee2007-03-07 16:46:57 -08004453 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4454
Mark Fashehdcd05382007-01-16 11:32:23 -08004455 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
Tao Ma853a3a12009-08-18 11:22:18 +08004456 contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4457 insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004458 if (contig_type != CONTIG_NONE) {
4459 insert->ins_contig_index = i;
4460 break;
4461 }
4462 }
4463 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004464
4465 if (insert->ins_contig != CONTIG_NONE) {
4466 struct ocfs2_extent_rec *rec =
4467 &el->l_recs[insert->ins_contig_index];
4468 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4469 le16_to_cpu(insert_rec->e_leaf_clusters);
4470
4471 /*
4472 * Caller might want us to limit the size of extents, don't
4473 * calculate contiguousness if we might exceed that limit.
4474 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004475 if (et->et_max_leaf_clusters &&
4476 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004477 insert->ins_contig = CONTIG_NONE;
4478 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004479}
4480
4481/*
4482 * This should only be called against the righmost leaf extent list.
4483 *
4484 * ocfs2_figure_appending_type() will figure out whether we'll have to
4485 * insert at the tail of the rightmost leaf.
4486 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004487 * This should also work against the root extent list for tree's with 0
4488 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004489 * then the logic here makes sense.
4490 */
4491static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4492 struct ocfs2_extent_list *el,
4493 struct ocfs2_extent_rec *insert_rec)
4494{
4495 int i;
4496 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4497 struct ocfs2_extent_rec *rec;
4498
4499 insert->ins_appending = APPEND_NONE;
4500
Mark Fashehe48edee2007-03-07 16:46:57 -08004501 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004502
4503 if (!el->l_next_free_rec)
4504 goto set_tail_append;
4505
4506 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4507 /* Were all records empty? */
4508 if (le16_to_cpu(el->l_next_free_rec) == 1)
4509 goto set_tail_append;
4510 }
4511
4512 i = le16_to_cpu(el->l_next_free_rec) - 1;
4513 rec = &el->l_recs[i];
4514
Mark Fashehe48edee2007-03-07 16:46:57 -08004515 if (cpos >=
4516 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004517 goto set_tail_append;
4518
4519 return;
4520
4521set_tail_append:
4522 insert->ins_appending = APPEND_TAIL;
4523}
4524
4525/*
4526 * Helper function called at the begining of an insert.
4527 *
4528 * This computes a few things that are commonly used in the process of
4529 * inserting into the btree:
4530 * - Whether the new extent is contiguous with an existing one.
4531 * - The current tree depth.
4532 * - Whether the insert is an appending one.
4533 * - The total # of free records in the tree.
4534 *
4535 * All of the information is stored on the ocfs2_insert_type
4536 * structure.
4537 */
Joel Becker627961b2009-02-13 03:14:38 -08004538static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004539 struct buffer_head **last_eb_bh,
4540 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004541 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004542 struct ocfs2_insert_type *insert)
4543{
4544 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004545 struct ocfs2_extent_block *eb;
4546 struct ocfs2_extent_list *el;
4547 struct ocfs2_path *path = NULL;
4548 struct buffer_head *bh = NULL;
4549
Mark Fasheh328d5752007-06-18 10:48:04 -07004550 insert->ins_split = SPLIT_NONE;
4551
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004552 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004553 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4554
4555 if (el->l_tree_depth) {
4556 /*
4557 * If we have tree depth, we read in the
4558 * rightmost extent block ahead of time as
4559 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4560 * may want it later.
4561 */
Joel Becker3d03a302009-02-12 17:49:26 -08004562 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08004563 ocfs2_et_get_last_eb_blk(et),
4564 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004565 if (ret) {
4566 mlog_exit(ret);
4567 goto out;
4568 }
4569 eb = (struct ocfs2_extent_block *) bh->b_data;
4570 el = &eb->h_list;
4571 }
4572
4573 /*
4574 * Unless we have a contiguous insert, we'll need to know if
4575 * there is room left in our allocation tree for another
4576 * extent record.
4577 *
4578 * XXX: This test is simplistic, we can search for empty
4579 * extent records too.
4580 */
Tao Maoc77534f2007-08-28 17:22:33 -07004581 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004582 le16_to_cpu(el->l_next_free_rec);
4583
4584 if (!insert->ins_tree_depth) {
Joel Becker1ef61b32009-02-13 03:12:33 -08004585 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004586 ocfs2_figure_appending_type(insert, el, insert_rec);
4587 return 0;
4588 }
4589
Joel Beckerffdd7a52008-10-17 22:32:01 -07004590 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004591 if (!path) {
4592 ret = -ENOMEM;
4593 mlog_errno(ret);
4594 goto out;
4595 }
4596
4597 /*
4598 * In the case that we're inserting past what the tree
4599 * currently accounts for, ocfs2_find_path() will return for
4600 * us the rightmost tree path. This is accounted for below in
4601 * the appending code.
4602 */
Joel Beckerfacdb772009-02-12 18:08:48 -08004603 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -08004604 if (ret) {
4605 mlog_errno(ret);
4606 goto out;
4607 }
4608
4609 el = path_leaf_el(path);
4610
4611 /*
4612 * Now that we have the path, there's two things we want to determine:
4613 * 1) Contiguousness (also set contig_index if this is so)
4614 *
4615 * 2) Are we doing an append? We can trivially break this up
4616 * into two types of appends: simple record append, or a
4617 * rotate inside the tail leaf.
4618 */
Joel Becker1ef61b32009-02-13 03:12:33 -08004619 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004620
4621 /*
4622 * The insert code isn't quite ready to deal with all cases of
4623 * left contiguousness. Specifically, if it's an insert into
4624 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004625 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004626 * left. For now, just catch that case and fool the layers
4627 * above us. This works just fine for tree_depth == 0, which
4628 * is why we allow that above.
4629 */
4630 if (insert->ins_contig == CONTIG_LEFT &&
4631 insert->ins_contig_index == 0)
4632 insert->ins_contig = CONTIG_NONE;
4633
4634 /*
4635 * Ok, so we can simply compare against last_eb to figure out
4636 * whether the path doesn't exist. This will only happen in
4637 * the case that we're doing a tail append, so maybe we can
4638 * take advantage of that information somehow.
4639 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004640 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004641 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004642 /*
4643 * Ok, ocfs2_find_path() returned us the rightmost
4644 * tree path. This might be an appending insert. There are
4645 * two cases:
4646 * 1) We're doing a true append at the tail:
4647 * -This might even be off the end of the leaf
4648 * 2) We're "appending" by rotating in the tail
4649 */
4650 ocfs2_figure_appending_type(insert, el, insert_rec);
4651 }
4652
4653out:
4654 ocfs2_free_path(path);
4655
4656 if (ret == 0)
4657 *last_eb_bh = bh;
4658 else
4659 brelse(bh);
4660 return ret;
4661}
4662
4663/*
Joel Beckercc79d8c2009-02-13 03:24:43 -08004664 * Insert an extent into a btree.
Mark Fashehdcd05382007-01-16 11:32:23 -08004665 *
Joel Beckercc79d8c2009-02-13 03:24:43 -08004666 * The caller needs to update the owning btree's cluster count.
Mark Fashehdcd05382007-01-16 11:32:23 -08004667 */
Joel Beckercc79d8c2009-02-13 03:24:43 -08004668int ocfs2_insert_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004669 struct ocfs2_extent_tree *et,
4670 u32 cpos,
4671 u64 start_blk,
4672 u32 new_clusters,
4673 u8 flags,
4674 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004675{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004676 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004677 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004678 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004679 struct ocfs2_insert_type insert = {0, };
4680 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004681
Joel Beckercc79d8c2009-02-13 03:24:43 -08004682 mlog(0, "add %u clusters at position %u to owner %llu\n",
4683 new_clusters, cpos,
4684 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08004685
Mark Fashehe48edee2007-03-07 16:46:57 -08004686 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004687 rec.e_cpos = cpu_to_le32(cpos);
4688 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004689 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004690 rec.e_flags = flags;
Joel Becker6136ca52009-02-12 19:32:43 -08004691 status = ocfs2_et_insert_check(et, &rec);
Joel Becker1e61ee72008-08-20 18:32:45 -07004692 if (status) {
4693 mlog_errno(status);
4694 goto bail;
4695 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004696
Joel Becker627961b2009-02-13 03:14:38 -08004697 status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004698 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004699 if (status < 0) {
4700 mlog_errno(status);
4701 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004702 }
4703
Mark Fashehdcd05382007-01-16 11:32:23 -08004704 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4705 "Insert.contig_index: %d, Insert.free_records: %d, "
4706 "Insert.tree_depth: %d\n",
4707 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004708 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004709
Tao Maoc77534f2007-08-28 17:22:33 -07004710 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004711 status = ocfs2_grow_tree(handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004712 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004713 meta_ac);
4714 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004715 mlog_errno(status);
4716 goto bail;
4717 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004718 }
4719
Mark Fashehdcd05382007-01-16 11:32:23 -08004720 /* Finally, we can add clusters. This might rotate the tree for us. */
Joel Becker3505bec2009-02-13 02:57:58 -08004721 status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004722 if (status < 0)
4723 mlog_errno(status);
Joel Becker92ba4702009-02-13 03:18:34 -08004724 else
4725 ocfs2_et_extent_map_insert(et, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004726
4727bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004728 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004729
Tao Maf56654c2008-08-18 17:38:48 +08004730 mlog_exit(status);
4731 return status;
4732}
4733
Tao Ma0eb8d472008-08-18 17:38:45 +08004734/*
4735 * Allcate and add clusters into the extent b-tree.
4736 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004737 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004738 * it is not limited to the file storage. Any extent tree can use this
4739 * function if it implements the proper ocfs2_extent_tree.
4740 */
Joel Beckercbee7e12009-02-13 03:34:15 -08004741int ocfs2_add_clusters_in_btree(handle_t *handle,
4742 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004743 u32 *logical_offset,
4744 u32 clusters_to_add,
4745 int mark_unwritten,
Tao Ma0eb8d472008-08-18 17:38:45 +08004746 struct ocfs2_alloc_context *data_ac,
4747 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004748 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004749{
4750 int status = 0;
4751 int free_extents;
4752 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4753 u32 bit_off, num_bits;
4754 u64 block;
4755 u8 flags = 0;
Joel Beckercbee7e12009-02-13 03:34:15 -08004756 struct ocfs2_super *osb =
4757 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Tao Ma0eb8d472008-08-18 17:38:45 +08004758
4759 BUG_ON(!clusters_to_add);
4760
4761 if (mark_unwritten)
4762 flags = OCFS2_EXT_UNWRITTEN;
4763
Joel Becker3d03a302009-02-12 17:49:26 -08004764 free_extents = ocfs2_num_free_extents(osb, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004765 if (free_extents < 0) {
4766 status = free_extents;
4767 mlog_errno(status);
4768 goto leave;
4769 }
4770
4771 /* there are two cases which could cause us to EAGAIN in the
4772 * we-need-more-metadata case:
4773 * 1) we haven't reserved *any*
4774 * 2) we are so fragmented, we've needed to add metadata too
4775 * many times. */
4776 if (!free_extents && !meta_ac) {
4777 mlog(0, "we haven't reserved any metadata!\n");
4778 status = -EAGAIN;
4779 reason = RESTART_META;
4780 goto leave;
4781 } else if ((!free_extents)
4782 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004783 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004784 mlog(0, "filesystem is really fragmented...\n");
4785 status = -EAGAIN;
4786 reason = RESTART_META;
4787 goto leave;
4788 }
4789
Joel Becker1ed9b772010-05-06 13:59:06 +08004790 status = __ocfs2_claim_clusters(handle, data_ac, 1,
Tao Ma0eb8d472008-08-18 17:38:45 +08004791 clusters_to_add, &bit_off, &num_bits);
4792 if (status < 0) {
4793 if (status != -ENOSPC)
4794 mlog_errno(status);
4795 goto leave;
4796 }
4797
4798 BUG_ON(num_bits > clusters_to_add);
4799
Joel Becker13723d02008-10-17 19:25:01 -07004800 /* reserve our write early -- insert_extent may update the tree root */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004801 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004802 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d472008-08-18 17:38:45 +08004803 if (status < 0) {
4804 mlog_errno(status);
4805 goto leave;
4806 }
4807
4808 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Joel Beckercbee7e12009-02-13 03:34:15 -08004809 mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
4810 num_bits, bit_off,
4811 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Joel Beckercc79d8c2009-02-13 03:24:43 -08004812 status = ocfs2_insert_extent(handle, et, *logical_offset, block,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004813 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004814 if (status < 0) {
4815 mlog_errno(status);
4816 goto leave;
4817 }
4818
Joel Beckerec20cec2010-03-19 14:13:52 -07004819 ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004820
4821 clusters_to_add -= num_bits;
4822 *logical_offset += num_bits;
4823
4824 if (clusters_to_add) {
4825 mlog(0, "need to alloc once more, wanted = %u\n",
4826 clusters_to_add);
4827 status = -EAGAIN;
4828 reason = RESTART_TRANS;
4829 }
4830
4831leave:
4832 mlog_exit(status);
4833 if (reason_ret)
4834 *reason_ret = reason;
4835 return status;
4836}
4837
Mark Fasheh328d5752007-06-18 10:48:04 -07004838static void ocfs2_make_right_split_rec(struct super_block *sb,
4839 struct ocfs2_extent_rec *split_rec,
4840 u32 cpos,
4841 struct ocfs2_extent_rec *rec)
4842{
4843 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4844 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4845
4846 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4847
4848 split_rec->e_cpos = cpu_to_le32(cpos);
4849 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4850
4851 split_rec->e_blkno = rec->e_blkno;
4852 le64_add_cpu(&split_rec->e_blkno,
4853 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4854
4855 split_rec->e_flags = rec->e_flags;
4856}
4857
Joel Beckerd2311292009-02-13 03:43:22 -08004858static int ocfs2_split_and_insert(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004859 struct ocfs2_extent_tree *et,
Joel Beckerd2311292009-02-13 03:43:22 -08004860 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004861 struct buffer_head **last_eb_bh,
4862 int split_index,
4863 struct ocfs2_extent_rec *orig_split_rec,
4864 struct ocfs2_alloc_context *meta_ac)
4865{
4866 int ret = 0, depth;
4867 unsigned int insert_range, rec_range, do_leftright = 0;
4868 struct ocfs2_extent_rec tmprec;
4869 struct ocfs2_extent_list *rightmost_el;
4870 struct ocfs2_extent_rec rec;
4871 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4872 struct ocfs2_insert_type insert;
4873 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004874
4875leftright:
4876 /*
4877 * Store a copy of the record on the stack - it might move
4878 * around as the tree is manipulated below.
4879 */
4880 rec = path_leaf_el(path)->l_recs[split_index];
4881
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004882 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004883
4884 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4885 if (depth) {
4886 BUG_ON(!(*last_eb_bh));
4887 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4888 rightmost_el = &eb->h_list;
4889 }
4890
4891 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4892 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004893 ret = ocfs2_grow_tree(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08004894 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004895 if (ret) {
4896 mlog_errno(ret);
4897 goto out;
4898 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004899 }
4900
4901 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4902 insert.ins_appending = APPEND_NONE;
4903 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004904 insert.ins_tree_depth = depth;
4905
4906 insert_range = le32_to_cpu(split_rec.e_cpos) +
4907 le16_to_cpu(split_rec.e_leaf_clusters);
4908 rec_range = le32_to_cpu(rec.e_cpos) +
4909 le16_to_cpu(rec.e_leaf_clusters);
4910
4911 if (split_rec.e_cpos == rec.e_cpos) {
4912 insert.ins_split = SPLIT_LEFT;
4913 } else if (insert_range == rec_range) {
4914 insert.ins_split = SPLIT_RIGHT;
4915 } else {
4916 /*
4917 * Left/right split. We fake this as a right split
4918 * first and then make a second pass as a left split.
4919 */
4920 insert.ins_split = SPLIT_RIGHT;
4921
Joel Beckerd2311292009-02-13 03:43:22 -08004922 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4923 &tmprec, insert_range, &rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004924
4925 split_rec = tmprec;
4926
4927 BUG_ON(do_leftright);
4928 do_leftright = 1;
4929 }
4930
Joel Becker3505bec2009-02-13 02:57:58 -08004931 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004932 if (ret) {
4933 mlog_errno(ret);
4934 goto out;
4935 }
4936
4937 if (do_leftright == 1) {
4938 u32 cpos;
4939 struct ocfs2_extent_list *el;
4940
4941 do_leftright++;
4942 split_rec = *orig_split_rec;
4943
4944 ocfs2_reinit_path(path, 1);
4945
4946 cpos = le32_to_cpu(split_rec.e_cpos);
Joel Beckerfacdb772009-02-12 18:08:48 -08004947 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07004948 if (ret) {
4949 mlog_errno(ret);
4950 goto out;
4951 }
4952
4953 el = path_leaf_el(path);
4954 split_index = ocfs2_search_extent_list(el, cpos);
4955 goto leftright;
4956 }
4957out:
4958
4959 return ret;
4960}
4961
Joel Beckerf3868d02009-02-17 19:46:04 -08004962static int ocfs2_replace_extent_rec(handle_t *handle,
4963 struct ocfs2_extent_tree *et,
Tao Ma47be12e2009-01-09 07:32:48 +08004964 struct ocfs2_path *path,
4965 struct ocfs2_extent_list *el,
4966 int split_index,
4967 struct ocfs2_extent_rec *split_rec)
4968{
4969 int ret;
4970
Joel Beckerf3868d02009-02-17 19:46:04 -08004971 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Tao Ma47be12e2009-01-09 07:32:48 +08004972 path_num_items(path) - 1);
4973 if (ret) {
4974 mlog_errno(ret);
4975 goto out;
4976 }
4977
4978 el->l_recs[split_index] = *split_rec;
4979
4980 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4981out:
4982 return ret;
4983}
4984
Mark Fasheh328d5752007-06-18 10:48:04 -07004985/*
Tao Ma555936b2009-08-18 11:22:21 +08004986 * Split part or all of the extent record at split_index in the leaf
4987 * pointed to by path. Merge with the contiguous extent record if needed.
Mark Fasheh328d5752007-06-18 10:48:04 -07004988 *
4989 * Care is taken to handle contiguousness so as to not grow the tree.
4990 *
4991 * meta_ac is not strictly necessary - we only truly need it if growth
4992 * of the tree is required. All other cases will degrade into a less
4993 * optimal tree layout.
4994 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004995 * last_eb_bh should be the rightmost leaf block for any extent
4996 * btree. Since a split may grow the tree or a merge might shrink it,
4997 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07004998 *
4999 * This code is optimized for readability - several passes might be
5000 * made over certain portions of the tree. All of those blocks will
5001 * have been brought into cache (and pinned via the journal), so the
5002 * extra overhead is not expressed in terms of disk reads.
5003 */
Tao Mae2e9f602009-08-18 11:22:34 +08005004int ocfs2_split_extent(handle_t *handle,
5005 struct ocfs2_extent_tree *et,
5006 struct ocfs2_path *path,
5007 int split_index,
5008 struct ocfs2_extent_rec *split_rec,
5009 struct ocfs2_alloc_context *meta_ac,
5010 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005011{
5012 int ret = 0;
5013 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08005014 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07005015 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5016 struct ocfs2_merge_ctxt ctxt;
5017 struct ocfs2_extent_list *rightmost_el;
5018
Mark Fasheh328d5752007-06-18 10:48:04 -07005019 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5020 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5021 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5022 ret = -EIO;
5023 mlog_errno(ret);
5024 goto out;
5025 }
5026
Joel Beckera2970292009-02-13 03:09:54 -08005027 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005028 split_index,
5029 split_rec);
5030
5031 /*
5032 * The core merge / split code wants to know how much room is
Joel Beckera1cf0762009-02-13 03:45:49 -08005033 * left in this allocation tree, so we pass the
Mark Fasheh328d5752007-06-18 10:48:04 -07005034 * rightmost extent list.
5035 */
5036 if (path->p_tree_depth) {
5037 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005038
Joel Becker3d03a302009-02-12 17:49:26 -08005039 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005040 ocfs2_et_get_last_eb_blk(et),
5041 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005042 if (ret) {
5043 mlog_exit(ret);
5044 goto out;
5045 }
5046
5047 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005048 rightmost_el = &eb->h_list;
5049 } else
5050 rightmost_el = path_root_el(path);
5051
Mark Fasheh328d5752007-06-18 10:48:04 -07005052 if (rec->e_cpos == split_rec->e_cpos &&
5053 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5054 ctxt.c_split_covers_rec = 1;
5055 else
5056 ctxt.c_split_covers_rec = 0;
5057
5058 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5059
Mark Fasheh015452b2007-09-12 10:21:22 -07005060 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5061 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5062 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005063
5064 if (ctxt.c_contig_type == CONTIG_NONE) {
5065 if (ctxt.c_split_covers_rec)
Joel Beckerf3868d02009-02-17 19:46:04 -08005066 ret = ocfs2_replace_extent_rec(handle, et, path, el,
Tao Ma47be12e2009-01-09 07:32:48 +08005067 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005068 else
Joel Beckerd2311292009-02-13 03:43:22 -08005069 ret = ocfs2_split_and_insert(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005070 &last_eb_bh, split_index,
5071 split_rec, meta_ac);
5072 if (ret)
5073 mlog_errno(ret);
5074 } else {
Joel Beckerc495dd22009-02-13 02:19:11 -08005075 ret = ocfs2_try_to_merge_extent(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005076 split_index, split_rec,
Joel Beckerc495dd22009-02-13 02:19:11 -08005077 dealloc, &ctxt);
Mark Fasheh328d5752007-06-18 10:48:04 -07005078 if (ret)
5079 mlog_errno(ret);
5080 }
5081
Mark Fasheh328d5752007-06-18 10:48:04 -07005082out:
5083 brelse(last_eb_bh);
5084 return ret;
5085}
5086
5087/*
Tao Ma555936b2009-08-18 11:22:21 +08005088 * Change the flags of the already-existing extent at cpos for len clusters.
5089 *
5090 * new_flags: the flags we want to set.
5091 * clear_flags: the flags we want to clear.
5092 * phys: the new physical offset we want this new extent starts from.
Mark Fasheh328d5752007-06-18 10:48:04 -07005093 *
5094 * If the existing extent is larger than the request, initiate a
5095 * split. An attempt will be made at merging with adjacent extents.
5096 *
5097 * The caller is responsible for passing down meta_ac if we'll need it.
5098 */
Tao Ma1aa75fe2009-08-18 11:28:39 +08005099int ocfs2_change_extent_flag(handle_t *handle,
5100 struct ocfs2_extent_tree *et,
5101 u32 cpos, u32 len, u32 phys,
5102 struct ocfs2_alloc_context *meta_ac,
5103 struct ocfs2_cached_dealloc_ctxt *dealloc,
5104 int new_flags, int clear_flags)
Mark Fasheh328d5752007-06-18 10:48:04 -07005105{
5106 int ret, index;
Tao Ma555936b2009-08-18 11:22:21 +08005107 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5108 u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
Mark Fasheh328d5752007-06-18 10:48:04 -07005109 struct ocfs2_extent_rec split_rec;
5110 struct ocfs2_path *left_path = NULL;
5111 struct ocfs2_extent_list *el;
Tao Ma555936b2009-08-18 11:22:21 +08005112 struct ocfs2_extent_rec *rec;
Mark Fasheh328d5752007-06-18 10:48:04 -07005113
Joel Beckerffdd7a52008-10-17 22:32:01 -07005114 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005115 if (!left_path) {
5116 ret = -ENOMEM;
5117 mlog_errno(ret);
5118 goto out;
5119 }
5120
Joel Beckerfacdb772009-02-12 18:08:48 -08005121 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005122 if (ret) {
5123 mlog_errno(ret);
5124 goto out;
5125 }
5126 el = path_leaf_el(left_path);
5127
5128 index = ocfs2_search_extent_list(el, cpos);
5129 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Tao Ma555936b2009-08-18 11:22:21 +08005130 ocfs2_error(sb,
5131 "Owner %llu has an extent at cpos %u which can no "
Mark Fasheh328d5752007-06-18 10:48:04 -07005132 "longer be found.\n",
Tao Ma555936b2009-08-18 11:22:21 +08005133 (unsigned long long)
5134 ocfs2_metadata_cache_owner(et->et_ci), cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005135 ret = -EROFS;
5136 goto out;
5137 }
5138
Tao Ma555936b2009-08-18 11:22:21 +08005139 ret = -EIO;
5140 rec = &el->l_recs[index];
5141 if (new_flags && (rec->e_flags & new_flags)) {
5142 mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
5143 "extent that already had them",
5144 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5145 new_flags);
5146 goto out;
5147 }
5148
5149 if (clear_flags && !(rec->e_flags & clear_flags)) {
5150 mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
5151 "extent that didn't have them",
5152 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5153 clear_flags);
5154 goto out;
5155 }
5156
Mark Fasheh328d5752007-06-18 10:48:04 -07005157 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5158 split_rec.e_cpos = cpu_to_le32(cpos);
5159 split_rec.e_leaf_clusters = cpu_to_le16(len);
5160 split_rec.e_blkno = cpu_to_le64(start_blkno);
Tao Ma555936b2009-08-18 11:22:21 +08005161 split_rec.e_flags = rec->e_flags;
5162 if (new_flags)
5163 split_rec.e_flags |= new_flags;
5164 if (clear_flags)
5165 split_rec.e_flags &= ~clear_flags;
Mark Fasheh328d5752007-06-18 10:48:04 -07005166
Tao Mae2e9f602009-08-18 11:22:34 +08005167 ret = ocfs2_split_extent(handle, et, left_path,
5168 index, &split_rec, meta_ac,
5169 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005170 if (ret)
5171 mlog_errno(ret);
5172
5173out:
5174 ocfs2_free_path(left_path);
5175 return ret;
Tao Ma555936b2009-08-18 11:22:21 +08005176
5177}
5178
5179/*
5180 * Mark the already-existing extent at cpos as written for len clusters.
5181 * This removes the unwritten extent flag.
5182 *
5183 * If the existing extent is larger than the request, initiate a
5184 * split. An attempt will be made at merging with adjacent extents.
5185 *
5186 * The caller is responsible for passing down meta_ac if we'll need it.
5187 */
5188int ocfs2_mark_extent_written(struct inode *inode,
5189 struct ocfs2_extent_tree *et,
5190 handle_t *handle, u32 cpos, u32 len, u32 phys,
5191 struct ocfs2_alloc_context *meta_ac,
5192 struct ocfs2_cached_dealloc_ctxt *dealloc)
5193{
5194 int ret;
5195
5196 mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
5197 inode->i_ino, cpos, len, phys);
5198
5199 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5200 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5201 "that are being written to, but the feature bit "
5202 "is not set in the super block.",
5203 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5204 ret = -EROFS;
5205 goto out;
5206 }
5207
5208 /*
5209 * XXX: This should be fixed up so that we just re-insert the
5210 * next extent records.
5211 */
5212 ocfs2_et_extent_map_truncate(et, 0);
5213
5214 ret = ocfs2_change_extent_flag(handle, et, cpos,
5215 len, phys, meta_ac, dealloc,
5216 0, OCFS2_EXT_UNWRITTEN);
5217 if (ret)
5218 mlog_errno(ret);
5219
5220out:
5221 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07005222}
5223
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005224static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5225 struct ocfs2_path *path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005226 int index, u32 new_range,
5227 struct ocfs2_alloc_context *meta_ac)
5228{
Tao Mac901fb02010-04-26 14:34:57 +08005229 int ret, depth, credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005230 struct buffer_head *last_eb_bh = NULL;
5231 struct ocfs2_extent_block *eb;
5232 struct ocfs2_extent_list *rightmost_el, *el;
5233 struct ocfs2_extent_rec split_rec;
5234 struct ocfs2_extent_rec *rec;
5235 struct ocfs2_insert_type insert;
5236
5237 /*
5238 * Setup the record to split before we grow the tree.
5239 */
5240 el = path_leaf_el(path);
5241 rec = &el->l_recs[index];
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005242 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5243 &split_rec, new_range, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005244
5245 depth = path->p_tree_depth;
5246 if (depth > 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08005247 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005248 ocfs2_et_get_last_eb_blk(et),
5249 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005250 if (ret < 0) {
5251 mlog_errno(ret);
5252 goto out;
5253 }
5254
5255 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5256 rightmost_el = &eb->h_list;
5257 } else
5258 rightmost_el = path_leaf_el(path);
5259
Tao Mac901fb02010-04-26 14:34:57 +08005260 credits = path->p_tree_depth +
5261 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005262 ret = ocfs2_extend_trans(handle, credits);
5263 if (ret) {
5264 mlog_errno(ret);
5265 goto out;
5266 }
5267
5268 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5269 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08005270 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005271 meta_ac);
5272 if (ret) {
5273 mlog_errno(ret);
5274 goto out;
5275 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005276 }
5277
5278 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5279 insert.ins_appending = APPEND_NONE;
5280 insert.ins_contig = CONTIG_NONE;
5281 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005282 insert.ins_tree_depth = depth;
5283
Joel Becker3505bec2009-02-13 02:57:58 -08005284 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005285 if (ret)
5286 mlog_errno(ret);
5287
5288out:
5289 brelse(last_eb_bh);
5290 return ret;
5291}
5292
Joel Becker043beeb2009-02-13 02:42:30 -08005293static int ocfs2_truncate_rec(handle_t *handle,
5294 struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005295 struct ocfs2_path *path, int index,
5296 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker043beeb2009-02-13 02:42:30 -08005297 u32 cpos, u32 len)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005298{
5299 int ret;
5300 u32 left_cpos, rec_range, trunc_range;
5301 int wants_rotate = 0, is_rightmost_tree_rec = 0;
Joel Becker043beeb2009-02-13 02:42:30 -08005302 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005303 struct ocfs2_path *left_path = NULL;
5304 struct ocfs2_extent_list *el = path_leaf_el(path);
5305 struct ocfs2_extent_rec *rec;
5306 struct ocfs2_extent_block *eb;
5307
5308 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Joel Becker70f18c02009-02-13 02:09:31 -08005309 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005310 if (ret) {
5311 mlog_errno(ret);
5312 goto out;
5313 }
5314
5315 index--;
5316 }
5317
5318 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5319 path->p_tree_depth) {
5320 /*
5321 * Check whether this is the rightmost tree record. If
5322 * we remove all of this record or part of its right
5323 * edge then an update of the record lengths above it
5324 * will be required.
5325 */
5326 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5327 if (eb->h_next_leaf_blk == 0)
5328 is_rightmost_tree_rec = 1;
5329 }
5330
5331 rec = &el->l_recs[index];
5332 if (index == 0 && path->p_tree_depth &&
5333 le32_to_cpu(rec->e_cpos) == cpos) {
5334 /*
5335 * Changing the leftmost offset (via partial or whole
5336 * record truncate) of an interior (or rightmost) path
5337 * means we have to update the subtree that is formed
5338 * by this leaf and the one to it's left.
5339 *
5340 * There are two cases we can skip:
Joel Becker043beeb2009-02-13 02:42:30 -08005341 * 1) Path is the leftmost one in our btree.
Mark Fashehd0c7d702007-07-03 13:27:22 -07005342 * 2) The leaf is rightmost and will be empty after
5343 * we remove the extent record - the rotate code
5344 * knows how to update the newly formed edge.
5345 */
5346
Joel Becker043beeb2009-02-13 02:42:30 -08005347 ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005348 if (ret) {
5349 mlog_errno(ret);
5350 goto out;
5351 }
5352
5353 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005354 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005355 if (!left_path) {
5356 ret = -ENOMEM;
5357 mlog_errno(ret);
5358 goto out;
5359 }
5360
Joel Beckerfacdb772009-02-12 18:08:48 -08005361 ret = ocfs2_find_path(et->et_ci, left_path,
5362 left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005363 if (ret) {
5364 mlog_errno(ret);
5365 goto out;
5366 }
5367 }
5368 }
5369
5370 ret = ocfs2_extend_rotate_transaction(handle, 0,
5371 handle->h_buffer_credits,
5372 path);
5373 if (ret) {
5374 mlog_errno(ret);
5375 goto out;
5376 }
5377
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005378 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005379 if (ret) {
5380 mlog_errno(ret);
5381 goto out;
5382 }
5383
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005384 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005385 if (ret) {
5386 mlog_errno(ret);
5387 goto out;
5388 }
5389
5390 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5391 trunc_range = cpos + len;
5392
5393 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5394 int next_free;
5395
5396 memset(rec, 0, sizeof(*rec));
5397 ocfs2_cleanup_merge(el, index);
5398 wants_rotate = 1;
5399
5400 next_free = le16_to_cpu(el->l_next_free_rec);
5401 if (is_rightmost_tree_rec && next_free > 1) {
5402 /*
5403 * We skip the edge update if this path will
5404 * be deleted by the rotate code.
5405 */
5406 rec = &el->l_recs[next_free - 1];
Joel Beckerd401dc12009-02-13 02:24:10 -08005407 ocfs2_adjust_rightmost_records(handle, et, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005408 rec);
5409 }
5410 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5411 /* Remove leftmost portion of the record. */
5412 le32_add_cpu(&rec->e_cpos, len);
5413 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5414 le16_add_cpu(&rec->e_leaf_clusters, -len);
5415 } else if (rec_range == trunc_range) {
5416 /* Remove rightmost portion of the record */
5417 le16_add_cpu(&rec->e_leaf_clusters, -len);
5418 if (is_rightmost_tree_rec)
Joel Beckerd401dc12009-02-13 02:24:10 -08005419 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005420 } else {
5421 /* Caller should have trapped this. */
Joel Becker043beeb2009-02-13 02:42:30 -08005422 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5423 "(%u, %u)\n",
5424 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005425 le32_to_cpu(rec->e_cpos),
5426 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5427 BUG();
5428 }
5429
5430 if (left_path) {
5431 int subtree_index;
5432
Joel Becker7dc02802009-02-12 19:20:13 -08005433 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Joel Becker4619c732009-02-12 19:02:36 -08005434 ocfs2_complete_edge_insert(handle, left_path, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005435 subtree_index);
5436 }
5437
5438 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5439
Joel Becker70f18c02009-02-13 02:09:31 -08005440 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005441 if (ret) {
5442 mlog_errno(ret);
5443 goto out;
5444 }
5445
5446out:
5447 ocfs2_free_path(left_path);
5448 return ret;
5449}
5450
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005451int ocfs2_remove_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005452 struct ocfs2_extent_tree *et,
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005453 u32 cpos, u32 len,
Mark Fasheh063c4562007-07-03 13:34:11 -07005454 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005455 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005456{
5457 int ret, index;
5458 u32 rec_range, trunc_range;
5459 struct ocfs2_extent_rec *rec;
5460 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005461 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005462
Joel Becker4c911ee2009-02-13 02:50:12 -08005463 /*
5464 * XXX: Why are we truncating to 0 instead of wherever this
5465 * affects us?
5466 */
5467 ocfs2_et_extent_map_truncate(et, 0);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005468
Joel Beckerffdd7a52008-10-17 22:32:01 -07005469 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005470 if (!path) {
5471 ret = -ENOMEM;
5472 mlog_errno(ret);
5473 goto out;
5474 }
5475
Joel Beckerfacdb772009-02-12 18:08:48 -08005476 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005477 if (ret) {
5478 mlog_errno(ret);
5479 goto out;
5480 }
5481
5482 el = path_leaf_el(path);
5483 index = ocfs2_search_extent_list(el, cpos);
5484 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005485 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5486 "Owner %llu has an extent at cpos %u which can no "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005487 "longer be found.\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005488 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5489 cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005490 ret = -EROFS;
5491 goto out;
5492 }
5493
5494 /*
5495 * We have 3 cases of extent removal:
5496 * 1) Range covers the entire extent rec
5497 * 2) Range begins or ends on one edge of the extent rec
5498 * 3) Range is in the middle of the extent rec (no shared edges)
5499 *
5500 * For case 1 we remove the extent rec and left rotate to
5501 * fill the hole.
5502 *
5503 * For case 2 we just shrink the existing extent rec, with a
5504 * tree update if the shrinking edge is also the edge of an
5505 * extent block.
5506 *
5507 * For case 3 we do a right split to turn the extent rec into
5508 * something case 2 can handle.
5509 */
5510 rec = &el->l_recs[index];
5511 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5512 trunc_range = cpos + len;
5513
5514 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5515
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005516 mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005517 "(cpos %u, len %u)\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005518 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5519 cpos, len, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005520 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5521
5522 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
Joel Becker043beeb2009-02-13 02:42:30 -08005523 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5524 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005525 if (ret) {
5526 mlog_errno(ret);
5527 goto out;
5528 }
5529 } else {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005530 ret = ocfs2_split_tree(handle, et, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005531 trunc_range, meta_ac);
5532 if (ret) {
5533 mlog_errno(ret);
5534 goto out;
5535 }
5536
5537 /*
5538 * The split could have manipulated the tree enough to
5539 * move the record location, so we have to look for it again.
5540 */
5541 ocfs2_reinit_path(path, 1);
5542
Joel Beckerfacdb772009-02-12 18:08:48 -08005543 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005544 if (ret) {
5545 mlog_errno(ret);
5546 goto out;
5547 }
5548
5549 el = path_leaf_el(path);
5550 index = ocfs2_search_extent_list(el, cpos);
5551 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005552 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5553 "Owner %llu: split at cpos %u lost record.",
5554 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005555 cpos);
5556 ret = -EROFS;
5557 goto out;
5558 }
5559
5560 /*
5561 * Double check our values here. If anything is fishy,
5562 * it's easier to catch it at the top level.
5563 */
5564 rec = &el->l_recs[index];
5565 rec_range = le32_to_cpu(rec->e_cpos) +
5566 ocfs2_rec_clusters(el, rec);
5567 if (rec_range != trunc_range) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005568 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5569 "Owner %llu: error after split at cpos %u"
Mark Fashehd0c7d702007-07-03 13:27:22 -07005570 "trunc len %u, existing record is (%u,%u)",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005571 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005572 cpos, len, le32_to_cpu(rec->e_cpos),
5573 ocfs2_rec_clusters(el, rec));
5574 ret = -EROFS;
5575 goto out;
5576 }
5577
Joel Becker043beeb2009-02-13 02:42:30 -08005578 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5579 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005580 if (ret) {
5581 mlog_errno(ret);
5582 goto out;
5583 }
5584 }
5585
5586out:
5587 ocfs2_free_path(path);
5588 return ret;
5589}
5590
Mark Fashehfecc0112008-11-12 15:16:38 -08005591int ocfs2_remove_btree_range(struct inode *inode,
5592 struct ocfs2_extent_tree *et,
5593 u32 cpos, u32 phys_cpos, u32 len,
5594 struct ocfs2_cached_dealloc_ctxt *dealloc)
5595{
5596 int ret;
5597 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5598 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5599 struct inode *tl_inode = osb->osb_tl_inode;
5600 handle_t *handle;
5601 struct ocfs2_alloc_context *meta_ac = NULL;
5602
5603 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5604 if (ret) {
5605 mlog_errno(ret);
5606 return ret;
5607 }
5608
5609 mutex_lock(&tl_inode->i_mutex);
5610
5611 if (ocfs2_truncate_log_needs_flush(osb)) {
5612 ret = __ocfs2_flush_truncate_log(osb);
5613 if (ret < 0) {
5614 mlog_errno(ret);
5615 goto out;
5616 }
5617 }
5618
Jan Karaa90714c2008-10-09 19:38:40 +02005619 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005620 if (IS_ERR(handle)) {
5621 ret = PTR_ERR(handle);
5622 mlog_errno(ret);
5623 goto out;
5624 }
5625
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005626 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07005627 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005628 if (ret) {
5629 mlog_errno(ret);
5630 goto out;
5631 }
5632
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005633 dquot_free_space_nodirty(inode,
Mark Fashehfd4ef232009-01-29 15:06:21 -08005634 ocfs2_clusters_to_bytes(inode->i_sb, len));
5635
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005636 ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
Mark Fashehfecc0112008-11-12 15:16:38 -08005637 if (ret) {
5638 mlog_errno(ret);
5639 goto out_commit;
5640 }
5641
Joel Becker6136ca52009-02-12 19:32:43 -08005642 ocfs2_et_update_clusters(et, -len);
Mark Fashehfecc0112008-11-12 15:16:38 -08005643
Joel Beckerec20cec2010-03-19 14:13:52 -07005644 ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehfecc0112008-11-12 15:16:38 -08005645
5646 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5647 if (ret)
5648 mlog_errno(ret);
5649
5650out_commit:
5651 ocfs2_commit_trans(osb, handle);
5652out:
5653 mutex_unlock(&tl_inode->i_mutex);
5654
5655 if (meta_ac)
5656 ocfs2_free_alloc_context(meta_ac);
5657
5658 return ret;
5659}
5660
Mark Fasheh063c4562007-07-03 13:34:11 -07005661int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005662{
5663 struct buffer_head *tl_bh = osb->osb_tl_bh;
5664 struct ocfs2_dinode *di;
5665 struct ocfs2_truncate_log *tl;
5666
5667 di = (struct ocfs2_dinode *) tl_bh->b_data;
5668 tl = &di->id2.i_dealloc;
5669
5670 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5671 "slot %d, invalid truncate log parameters: used = "
5672 "%u, count = %u\n", osb->slot_num,
5673 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5674 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5675}
5676
5677static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5678 unsigned int new_start)
5679{
5680 unsigned int tail_index;
5681 unsigned int current_tail;
5682
5683 /* No records, nothing to coalesce */
5684 if (!le16_to_cpu(tl->tl_used))
5685 return 0;
5686
5687 tail_index = le16_to_cpu(tl->tl_used) - 1;
5688 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5689 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5690
5691 return current_tail == new_start;
5692}
5693
Mark Fasheh063c4562007-07-03 13:34:11 -07005694int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5695 handle_t *handle,
5696 u64 start_blk,
5697 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005698{
5699 int status, index;
5700 unsigned int start_cluster, tl_count;
5701 struct inode *tl_inode = osb->osb_tl_inode;
5702 struct buffer_head *tl_bh = osb->osb_tl_bh;
5703 struct ocfs2_dinode *di;
5704 struct ocfs2_truncate_log *tl;
5705
Mark Fashehb06970532006-03-03 10:24:33 -08005706 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5707 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005708
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005709 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005710
5711 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5712
5713 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005714
Joel Becker10995aa2008-11-13 14:49:12 -08005715 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5716 * by the underlying call to ocfs2_read_inode_block(), so any
5717 * corruption is a code bug */
5718 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5719
5720 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005721 tl_count = le16_to_cpu(tl->tl_count);
5722 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5723 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005724 "Truncate record count on #%llu invalid "
5725 "wanted %u, actual %u\n",
5726 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005727 ocfs2_truncate_recs_per_inode(osb->sb),
5728 le16_to_cpu(tl->tl_count));
5729
5730 /* Caller should have known to flush before calling us. */
5731 index = le16_to_cpu(tl->tl_used);
5732 if (index >= tl_count) {
5733 status = -ENOSPC;
5734 mlog_errno(status);
5735 goto bail;
5736 }
5737
Joel Becker0cf2f762009-02-12 16:41:25 -08005738 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005739 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005740 if (status < 0) {
5741 mlog_errno(status);
5742 goto bail;
5743 }
5744
5745 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005746 "%llu (index = %d)\n", num_clusters, start_cluster,
5747 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005748
5749 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5750 /*
5751 * Move index back to the record we are coalescing with.
5752 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5753 */
5754 index--;
5755
5756 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5757 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5758 index, le32_to_cpu(tl->tl_recs[index].t_start),
5759 num_clusters);
5760 } else {
5761 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5762 tl->tl_used = cpu_to_le16(index + 1);
5763 }
5764 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5765
Joel Beckerec20cec2010-03-19 14:13:52 -07005766 ocfs2_journal_dirty(handle, tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005767
5768bail:
5769 mlog_exit(status);
5770 return status;
5771}
5772
5773static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005774 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005775 struct inode *data_alloc_inode,
5776 struct buffer_head *data_alloc_bh)
5777{
5778 int status = 0;
5779 int i;
5780 unsigned int num_clusters;
5781 u64 start_blk;
5782 struct ocfs2_truncate_rec rec;
5783 struct ocfs2_dinode *di;
5784 struct ocfs2_truncate_log *tl;
5785 struct inode *tl_inode = osb->osb_tl_inode;
5786 struct buffer_head *tl_bh = osb->osb_tl_bh;
5787
5788 mlog_entry_void();
5789
5790 di = (struct ocfs2_dinode *) tl_bh->b_data;
5791 tl = &di->id2.i_dealloc;
5792 i = le16_to_cpu(tl->tl_used) - 1;
5793 while (i >= 0) {
5794 /* Caller has given us at least enough credits to
5795 * update the truncate log dinode */
Joel Becker0cf2f762009-02-12 16:41:25 -08005796 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005797 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005798 if (status < 0) {
5799 mlog_errno(status);
5800 goto bail;
5801 }
5802
5803 tl->tl_used = cpu_to_le16(i);
5804
Joel Beckerec20cec2010-03-19 14:13:52 -07005805 ocfs2_journal_dirty(handle, tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005806
5807 /* TODO: Perhaps we can calculate the bulk of the
5808 * credits up front rather than extending like
5809 * this. */
5810 status = ocfs2_extend_trans(handle,
5811 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5812 if (status < 0) {
5813 mlog_errno(status);
5814 goto bail;
5815 }
5816
5817 rec = tl->tl_recs[i];
5818 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5819 le32_to_cpu(rec.t_start));
5820 num_clusters = le32_to_cpu(rec.t_clusters);
5821
5822 /* if start_blk is not set, we ignore the record as
5823 * invalid. */
5824 if (start_blk) {
5825 mlog(0, "free record %d, start = %u, clusters = %u\n",
5826 i, le32_to_cpu(rec.t_start), num_clusters);
5827
5828 status = ocfs2_free_clusters(handle, data_alloc_inode,
5829 data_alloc_bh, start_blk,
5830 num_clusters);
5831 if (status < 0) {
5832 mlog_errno(status);
5833 goto bail;
5834 }
5835 }
5836 i--;
5837 }
5838
5839bail:
5840 mlog_exit(status);
5841 return status;
5842}
5843
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005844/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005845int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005846{
5847 int status;
5848 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005849 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005850 struct inode *tl_inode = osb->osb_tl_inode;
5851 struct inode *data_alloc_inode = NULL;
5852 struct buffer_head *tl_bh = osb->osb_tl_bh;
5853 struct buffer_head *data_alloc_bh = NULL;
5854 struct ocfs2_dinode *di;
5855 struct ocfs2_truncate_log *tl;
5856
5857 mlog_entry_void();
5858
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005859 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005860
5861 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005862
Joel Becker10995aa2008-11-13 14:49:12 -08005863 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5864 * by the underlying call to ocfs2_read_inode_block(), so any
5865 * corruption is a code bug */
5866 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5867
5868 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005869 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005870 mlog(0, "Flush %u records from truncate log #%llu\n",
5871 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005872 if (!num_to_flush) {
5873 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005874 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005875 }
5876
5877 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5878 GLOBAL_BITMAP_SYSTEM_INODE,
5879 OCFS2_INVALID_SLOT);
5880 if (!data_alloc_inode) {
5881 status = -EINVAL;
5882 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005883 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005884 }
5885
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005886 mutex_lock(&data_alloc_inode->i_mutex);
5887
Mark Fashehe63aecb62007-10-18 15:30:42 -07005888 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005889 if (status < 0) {
5890 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005891 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005892 }
5893
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005894 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005895 if (IS_ERR(handle)) {
5896 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005897 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005898 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005899 }
5900
5901 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5902 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005903 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005904 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005905
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005906 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005907
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005908out_unlock:
5909 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005910 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005911
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005912out_mutex:
5913 mutex_unlock(&data_alloc_inode->i_mutex);
5914 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005915
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005916out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005917 mlog_exit(status);
5918 return status;
5919}
5920
5921int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5922{
5923 int status;
5924 struct inode *tl_inode = osb->osb_tl_inode;
5925
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005926 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005927 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005928 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005929
5930 return status;
5931}
5932
David Howellsc4028952006-11-22 14:57:56 +00005933static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005934{
5935 int status;
David Howellsc4028952006-11-22 14:57:56 +00005936 struct ocfs2_super *osb =
5937 container_of(work, struct ocfs2_super,
5938 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005939
5940 mlog_entry_void();
5941
5942 status = ocfs2_flush_truncate_log(osb);
5943 if (status < 0)
5944 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005945 else
Tiger Yangb89c5422010-01-25 14:11:06 +08005946 ocfs2_init_steal_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005947
5948 mlog_exit(status);
5949}
5950
5951#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5952void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5953 int cancel)
5954{
5955 if (osb->osb_tl_inode) {
5956 /* We want to push off log flushes while truncates are
5957 * still running. */
5958 if (cancel)
5959 cancel_delayed_work(&osb->osb_truncate_log_wq);
5960
5961 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5962 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5963 }
5964}
5965
5966static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5967 int slot_num,
5968 struct inode **tl_inode,
5969 struct buffer_head **tl_bh)
5970{
5971 int status;
5972 struct inode *inode = NULL;
5973 struct buffer_head *bh = NULL;
5974
5975 inode = ocfs2_get_system_file_inode(osb,
5976 TRUNCATE_LOG_SYSTEM_INODE,
5977 slot_num);
5978 if (!inode) {
5979 status = -EINVAL;
5980 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5981 goto bail;
5982 }
5983
Joel Beckerb657c952008-11-13 14:49:11 -08005984 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005985 if (status < 0) {
5986 iput(inode);
5987 mlog_errno(status);
5988 goto bail;
5989 }
5990
5991 *tl_inode = inode;
5992 *tl_bh = bh;
5993bail:
5994 mlog_exit(status);
5995 return status;
5996}
5997
5998/* called during the 1st stage of node recovery. we stamp a clean
5999 * truncate log and pass back a copy for processing later. if the
6000 * truncate log does not require processing, a *tl_copy is set to
6001 * NULL. */
6002int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6003 int slot_num,
6004 struct ocfs2_dinode **tl_copy)
6005{
6006 int status;
6007 struct inode *tl_inode = NULL;
6008 struct buffer_head *tl_bh = NULL;
6009 struct ocfs2_dinode *di;
6010 struct ocfs2_truncate_log *tl;
6011
6012 *tl_copy = NULL;
6013
6014 mlog(0, "recover truncate log from slot %d\n", slot_num);
6015
6016 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6017 if (status < 0) {
6018 mlog_errno(status);
6019 goto bail;
6020 }
6021
6022 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08006023
Joel Becker10995aa2008-11-13 14:49:12 -08006024 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6025 * validated by the underlying call to ocfs2_read_inode_block(),
6026 * so any corruption is a code bug */
6027 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6028
6029 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08006030 if (le16_to_cpu(tl->tl_used)) {
6031 mlog(0, "We'll have %u logs to recover\n",
6032 le16_to_cpu(tl->tl_used));
6033
6034 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6035 if (!(*tl_copy)) {
6036 status = -ENOMEM;
6037 mlog_errno(status);
6038 goto bail;
6039 }
6040
6041 /* Assuming the write-out below goes well, this copy
6042 * will be passed back to recovery for processing. */
6043 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6044
6045 /* All we need to do to clear the truncate log is set
6046 * tl_used. */
6047 tl->tl_used = 0;
6048
Joel Becker13723d02008-10-17 19:25:01 -07006049 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08006050 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08006051 if (status < 0) {
6052 mlog_errno(status);
6053 goto bail;
6054 }
6055 }
6056
6057bail:
6058 if (tl_inode)
6059 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006060 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006061
6062 if (status < 0 && (*tl_copy)) {
6063 kfree(*tl_copy);
6064 *tl_copy = NULL;
6065 }
6066
6067 mlog_exit(status);
6068 return status;
6069}
6070
6071int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6072 struct ocfs2_dinode *tl_copy)
6073{
6074 int status = 0;
6075 int i;
6076 unsigned int clusters, num_recs, start_cluster;
6077 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006078 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006079 struct inode *tl_inode = osb->osb_tl_inode;
6080 struct ocfs2_truncate_log *tl;
6081
6082 mlog_entry_void();
6083
6084 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6085 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6086 return -EINVAL;
6087 }
6088
6089 tl = &tl_copy->id2.i_dealloc;
6090 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08006091 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006092 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006093
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006094 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006095 for(i = 0; i < num_recs; i++) {
6096 if (ocfs2_truncate_log_needs_flush(osb)) {
6097 status = __ocfs2_flush_truncate_log(osb);
6098 if (status < 0) {
6099 mlog_errno(status);
6100 goto bail_up;
6101 }
6102 }
6103
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006104 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006105 if (IS_ERR(handle)) {
6106 status = PTR_ERR(handle);
6107 mlog_errno(status);
6108 goto bail_up;
6109 }
6110
6111 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6112 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6113 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6114
6115 status = ocfs2_truncate_log_append(osb, handle,
6116 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006117 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006118 if (status < 0) {
6119 mlog_errno(status);
6120 goto bail_up;
6121 }
6122 }
6123
6124bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006125 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006126
6127 mlog_exit(status);
6128 return status;
6129}
6130
6131void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6132{
6133 int status;
6134 struct inode *tl_inode = osb->osb_tl_inode;
6135
6136 mlog_entry_void();
6137
6138 if (tl_inode) {
6139 cancel_delayed_work(&osb->osb_truncate_log_wq);
6140 flush_workqueue(ocfs2_wq);
6141
6142 status = ocfs2_flush_truncate_log(osb);
6143 if (status < 0)
6144 mlog_errno(status);
6145
6146 brelse(osb->osb_tl_bh);
6147 iput(osb->osb_tl_inode);
6148 }
6149
6150 mlog_exit_void();
6151}
6152
6153int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6154{
6155 int status;
6156 struct inode *tl_inode = NULL;
6157 struct buffer_head *tl_bh = NULL;
6158
6159 mlog_entry_void();
6160
6161 status = ocfs2_get_truncate_log_info(osb,
6162 osb->slot_num,
6163 &tl_inode,
6164 &tl_bh);
6165 if (status < 0)
6166 mlog_errno(status);
6167
6168 /* ocfs2_truncate_log_shutdown keys on the existence of
6169 * osb->osb_tl_inode so we don't set any of the osb variables
6170 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006171 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6172 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006173 osb->osb_tl_bh = tl_bh;
6174 osb->osb_tl_inode = tl_inode;
6175
6176 mlog_exit(status);
6177 return status;
6178}
6179
Mark Fasheh2b604352007-06-22 15:45:27 -07006180/*
6181 * Delayed de-allocation of suballocator blocks.
6182 *
6183 * Some sets of block de-allocations might involve multiple suballocator inodes.
6184 *
6185 * The locking for this can get extremely complicated, especially when
6186 * the suballocator inodes to delete from aren't known until deep
6187 * within an unrelated codepath.
6188 *
6189 * ocfs2_extent_block structures are a good example of this - an inode
6190 * btree could have been grown by any number of nodes each allocating
6191 * out of their own suballoc inode.
6192 *
6193 * These structures allow the delay of block de-allocation until a
6194 * later time, when locking of multiple cluster inodes won't cause
6195 * deadlock.
6196 */
6197
6198/*
Tao Ma2891d292008-11-12 08:26:58 +08006199 * Describe a single bit freed from a suballocator. For the block
6200 * suballocators, it represents one block. For the global cluster
6201 * allocator, it represents some clusters and free_bit indicates
6202 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006203 */
6204struct ocfs2_cached_block_free {
6205 struct ocfs2_cached_block_free *free_next;
Tao Ma74380c42010-03-22 14:20:18 +08006206 u64 free_bg;
Mark Fasheh2b604352007-06-22 15:45:27 -07006207 u64 free_blk;
6208 unsigned int free_bit;
6209};
6210
6211struct ocfs2_per_slot_free_list {
6212 struct ocfs2_per_slot_free_list *f_next_suballocator;
6213 int f_inode_type;
6214 int f_slot;
6215 struct ocfs2_cached_block_free *f_first;
6216};
6217
Tao Ma2891d292008-11-12 08:26:58 +08006218static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6219 int sysfile_type,
6220 int slot,
6221 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006222{
6223 int ret;
6224 u64 bg_blkno;
6225 handle_t *handle;
6226 struct inode *inode;
6227 struct buffer_head *di_bh = NULL;
6228 struct ocfs2_cached_block_free *tmp;
6229
6230 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6231 if (!inode) {
6232 ret = -EINVAL;
6233 mlog_errno(ret);
6234 goto out;
6235 }
6236
6237 mutex_lock(&inode->i_mutex);
6238
Mark Fashehe63aecb62007-10-18 15:30:42 -07006239 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006240 if (ret) {
6241 mlog_errno(ret);
6242 goto out_mutex;
6243 }
6244
6245 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6246 if (IS_ERR(handle)) {
6247 ret = PTR_ERR(handle);
6248 mlog_errno(ret);
6249 goto out_unlock;
6250 }
6251
6252 while (head) {
Tao Ma74380c42010-03-22 14:20:18 +08006253 if (head->free_bg)
6254 bg_blkno = head->free_bg;
6255 else
6256 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6257 head->free_bit);
Mark Fasheh2b604352007-06-22 15:45:27 -07006258 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6259 head->free_bit, (unsigned long long)head->free_blk);
6260
6261 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6262 head->free_bit, bg_blkno, 1);
6263 if (ret) {
6264 mlog_errno(ret);
6265 goto out_journal;
6266 }
6267
6268 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6269 if (ret) {
6270 mlog_errno(ret);
6271 goto out_journal;
6272 }
6273
6274 tmp = head;
6275 head = head->free_next;
6276 kfree(tmp);
6277 }
6278
6279out_journal:
6280 ocfs2_commit_trans(osb, handle);
6281
6282out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006283 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006284 brelse(di_bh);
6285out_mutex:
6286 mutex_unlock(&inode->i_mutex);
6287 iput(inode);
6288out:
6289 while(head) {
6290 /* Premature exit may have left some dangling items. */
6291 tmp = head;
6292 head = head->free_next;
6293 kfree(tmp);
6294 }
6295
6296 return ret;
6297}
6298
Tao Ma2891d292008-11-12 08:26:58 +08006299int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6300 u64 blkno, unsigned int bit)
6301{
6302 int ret = 0;
6303 struct ocfs2_cached_block_free *item;
6304
Tao Ma74380c42010-03-22 14:20:18 +08006305 item = kzalloc(sizeof(*item), GFP_NOFS);
Tao Ma2891d292008-11-12 08:26:58 +08006306 if (item == NULL) {
6307 ret = -ENOMEM;
6308 mlog_errno(ret);
6309 return ret;
6310 }
6311
6312 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6313 bit, (unsigned long long)blkno);
6314
6315 item->free_blk = blkno;
6316 item->free_bit = bit;
6317 item->free_next = ctxt->c_global_allocator;
6318
6319 ctxt->c_global_allocator = item;
6320 return ret;
6321}
6322
6323static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6324 struct ocfs2_cached_block_free *head)
6325{
6326 struct ocfs2_cached_block_free *tmp;
6327 struct inode *tl_inode = osb->osb_tl_inode;
6328 handle_t *handle;
6329 int ret = 0;
6330
6331 mutex_lock(&tl_inode->i_mutex);
6332
6333 while (head) {
6334 if (ocfs2_truncate_log_needs_flush(osb)) {
6335 ret = __ocfs2_flush_truncate_log(osb);
6336 if (ret < 0) {
6337 mlog_errno(ret);
6338 break;
6339 }
6340 }
6341
6342 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6343 if (IS_ERR(handle)) {
6344 ret = PTR_ERR(handle);
6345 mlog_errno(ret);
6346 break;
6347 }
6348
6349 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6350 head->free_bit);
6351
6352 ocfs2_commit_trans(osb, handle);
6353 tmp = head;
6354 head = head->free_next;
6355 kfree(tmp);
6356
6357 if (ret < 0) {
6358 mlog_errno(ret);
6359 break;
6360 }
6361 }
6362
6363 mutex_unlock(&tl_inode->i_mutex);
6364
6365 while (head) {
6366 /* Premature exit may have left some dangling items. */
6367 tmp = head;
6368 head = head->free_next;
6369 kfree(tmp);
6370 }
6371
6372 return ret;
6373}
6374
Mark Fasheh2b604352007-06-22 15:45:27 -07006375int ocfs2_run_deallocs(struct ocfs2_super *osb,
6376 struct ocfs2_cached_dealloc_ctxt *ctxt)
6377{
6378 int ret = 0, ret2;
6379 struct ocfs2_per_slot_free_list *fl;
6380
6381 if (!ctxt)
6382 return 0;
6383
6384 while (ctxt->c_first_suballocator) {
6385 fl = ctxt->c_first_suballocator;
6386
6387 if (fl->f_first) {
6388 mlog(0, "Free items: (type %u, slot %d)\n",
6389 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006390 ret2 = ocfs2_free_cached_blocks(osb,
6391 fl->f_inode_type,
6392 fl->f_slot,
6393 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006394 if (ret2)
6395 mlog_errno(ret2);
6396 if (!ret)
6397 ret = ret2;
6398 }
6399
6400 ctxt->c_first_suballocator = fl->f_next_suballocator;
6401 kfree(fl);
6402 }
6403
Tao Ma2891d292008-11-12 08:26:58 +08006404 if (ctxt->c_global_allocator) {
6405 ret2 = ocfs2_free_cached_clusters(osb,
6406 ctxt->c_global_allocator);
6407 if (ret2)
6408 mlog_errno(ret2);
6409 if (!ret)
6410 ret = ret2;
6411
6412 ctxt->c_global_allocator = NULL;
6413 }
6414
Mark Fasheh2b604352007-06-22 15:45:27 -07006415 return ret;
6416}
6417
6418static struct ocfs2_per_slot_free_list *
6419ocfs2_find_per_slot_free_list(int type,
6420 int slot,
6421 struct ocfs2_cached_dealloc_ctxt *ctxt)
6422{
6423 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6424
6425 while (fl) {
6426 if (fl->f_inode_type == type && fl->f_slot == slot)
6427 return fl;
6428
6429 fl = fl->f_next_suballocator;
6430 }
6431
6432 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6433 if (fl) {
6434 fl->f_inode_type = type;
6435 fl->f_slot = slot;
6436 fl->f_first = NULL;
6437 fl->f_next_suballocator = ctxt->c_first_suballocator;
6438
6439 ctxt->c_first_suballocator = fl;
6440 }
6441 return fl;
6442}
6443
Tao Ma1823cb02009-08-18 11:24:49 +08006444int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
Tao Ma74380c42010-03-22 14:20:18 +08006445 int type, int slot, u64 suballoc,
6446 u64 blkno, unsigned int bit)
Mark Fasheh2b604352007-06-22 15:45:27 -07006447{
6448 int ret;
6449 struct ocfs2_per_slot_free_list *fl;
6450 struct ocfs2_cached_block_free *item;
6451
6452 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6453 if (fl == NULL) {
6454 ret = -ENOMEM;
6455 mlog_errno(ret);
6456 goto out;
6457 }
6458
Tao Ma74380c42010-03-22 14:20:18 +08006459 item = kzalloc(sizeof(*item), GFP_NOFS);
Mark Fasheh2b604352007-06-22 15:45:27 -07006460 if (item == NULL) {
6461 ret = -ENOMEM;
6462 mlog_errno(ret);
6463 goto out;
6464 }
6465
6466 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6467 type, slot, bit, (unsigned long long)blkno);
6468
Tao Ma74380c42010-03-22 14:20:18 +08006469 item->free_bg = suballoc;
Mark Fasheh2b604352007-06-22 15:45:27 -07006470 item->free_blk = blkno;
6471 item->free_bit = bit;
6472 item->free_next = fl->f_first;
6473
6474 fl->f_first = item;
6475
6476 ret = 0;
6477out:
6478 return ret;
6479}
6480
Mark Fasheh59a5e412007-06-22 15:52:36 -07006481static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6482 struct ocfs2_extent_block *eb)
6483{
6484 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6485 le16_to_cpu(eb->h_suballoc_slot),
Tao Ma74380c42010-03-22 14:20:18 +08006486 le64_to_cpu(eb->h_suballoc_loc),
Mark Fasheh59a5e412007-06-22 15:52:36 -07006487 le64_to_cpu(eb->h_blkno),
6488 le16_to_cpu(eb->h_suballoc_bit));
6489}
6490
Mark Fashehccd979b2005-12-15 14:31:24 -08006491/* This function will figure out whether the currently last extent
6492 * block will be deleted, and if it will, what the new last extent
6493 * block will be so we can update his h_next_leaf_blk field, as well
6494 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006495static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006496 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006497 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006498 struct buffer_head **new_last_eb)
6499{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006500 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006501 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006502 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006503 struct ocfs2_extent_block *eb;
6504 struct ocfs2_extent_list *el;
6505 struct buffer_head *bh = NULL;
6506
6507 *new_last_eb = NULL;
6508
Mark Fashehccd979b2005-12-15 14:31:24 -08006509 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006510 if (!path->p_tree_depth)
6511 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006512
6513 /* trunc to zero special case - this makes tree_depth = 0
6514 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006515 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006516 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006517
Mark Fashehdcd05382007-01-16 11:32:23 -08006518 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006519 BUG_ON(!el->l_next_free_rec);
6520
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006521 /*
6522 * Make sure that this extent list will actually be empty
6523 * after we clear away the data. We can shortcut out if
6524 * there's more than one non-empty extent in the
6525 * list. Otherwise, a check of the remaining extent is
6526 * necessary.
6527 */
6528 next_free = le16_to_cpu(el->l_next_free_rec);
6529 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006530 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006531 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006532 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006533
6534 /* We may have a valid extent in index 1, check it. */
6535 if (next_free == 2)
6536 rec = &el->l_recs[1];
6537
6538 /*
6539 * Fall through - no more nonempty extents, so we want
6540 * to delete this leaf.
6541 */
6542 } else {
6543 if (next_free > 1)
6544 goto out;
6545
6546 rec = &el->l_recs[0];
6547 }
6548
6549 if (rec) {
6550 /*
6551 * Check it we'll only be trimming off the end of this
6552 * cluster.
6553 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006554 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006555 goto out;
6556 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006557
Mark Fashehdcd05382007-01-16 11:32:23 -08006558 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6559 if (ret) {
6560 mlog_errno(ret);
6561 goto out;
6562 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006563
Joel Beckerfacdb772009-02-12 18:08:48 -08006564 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08006565 if (ret) {
6566 mlog_errno(ret);
6567 goto out;
6568 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006569
Mark Fashehdcd05382007-01-16 11:32:23 -08006570 eb = (struct ocfs2_extent_block *) bh->b_data;
6571 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006572
6573 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6574 * Any corruption is a code bug. */
6575 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006576
6577 *new_last_eb = bh;
6578 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006579 mlog(0, "returning block %llu, (cpos: %u)\n",
6580 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6581out:
6582 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006583
Mark Fashehdcd05382007-01-16 11:32:23 -08006584 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006585}
6586
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006587/*
6588 * Trim some clusters off the rightmost edge of a tree. Only called
6589 * during truncate.
6590 *
6591 * The caller needs to:
6592 * - start journaling of each path component.
6593 * - compute and fully set up any new last ext block
6594 */
6595static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6596 handle_t *handle, struct ocfs2_truncate_context *tc,
Tao Mabcbbb242009-08-18 11:29:12 +08006597 u32 clusters_to_del, u64 *delete_start, u8 *flags)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006598{
6599 int ret, i, index = path->p_tree_depth;
6600 u32 new_edge = 0;
6601 u64 deleted_eb = 0;
6602 struct buffer_head *bh;
6603 struct ocfs2_extent_list *el;
6604 struct ocfs2_extent_rec *rec;
6605
6606 *delete_start = 0;
Tao Mabcbbb242009-08-18 11:29:12 +08006607 *flags = 0;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006608
6609 while (index >= 0) {
6610 bh = path->p_node[index].bh;
6611 el = path->p_node[index].el;
6612
6613 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6614 index, (unsigned long long)bh->b_blocknr);
6615
6616 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6617
6618 if (index !=
6619 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6620 ocfs2_error(inode->i_sb,
6621 "Inode %lu has invalid ext. block %llu",
6622 inode->i_ino,
6623 (unsigned long long)bh->b_blocknr);
6624 ret = -EROFS;
6625 goto out;
6626 }
6627
6628find_tail_record:
6629 i = le16_to_cpu(el->l_next_free_rec) - 1;
6630 rec = &el->l_recs[i];
6631
6632 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6633 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006634 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006635 (unsigned long long)le64_to_cpu(rec->e_blkno),
6636 le16_to_cpu(el->l_next_free_rec));
6637
Mark Fashehe48edee2007-03-07 16:46:57 -08006638 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006639
6640 if (le16_to_cpu(el->l_tree_depth) == 0) {
6641 /*
6642 * If the leaf block contains a single empty
6643 * extent and no records, we can just remove
6644 * the block.
6645 */
6646 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6647 memset(rec, 0,
6648 sizeof(struct ocfs2_extent_rec));
6649 el->l_next_free_rec = cpu_to_le16(0);
6650
6651 goto delete;
6652 }
6653
6654 /*
6655 * Remove any empty extents by shifting things
6656 * left. That should make life much easier on
6657 * the code below. This condition is rare
6658 * enough that we shouldn't see a performance
6659 * hit.
6660 */
6661 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6662 le16_add_cpu(&el->l_next_free_rec, -1);
6663
6664 for(i = 0;
6665 i < le16_to_cpu(el->l_next_free_rec); i++)
6666 el->l_recs[i] = el->l_recs[i + 1];
6667
6668 memset(&el->l_recs[i], 0,
6669 sizeof(struct ocfs2_extent_rec));
6670
6671 /*
6672 * We've modified our extent list. The
6673 * simplest way to handle this change
6674 * is to being the search from the
6675 * start again.
6676 */
6677 goto find_tail_record;
6678 }
6679
Mark Fashehe48edee2007-03-07 16:46:57 -08006680 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006681
6682 /*
6683 * We'll use "new_edge" on our way back up the
6684 * tree to know what our rightmost cpos is.
6685 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006686 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006687 new_edge += le32_to_cpu(rec->e_cpos);
6688
6689 /*
6690 * The caller will use this to delete data blocks.
6691 */
6692 *delete_start = le64_to_cpu(rec->e_blkno)
6693 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006694 le16_to_cpu(rec->e_leaf_clusters));
Tao Mabcbbb242009-08-18 11:29:12 +08006695 *flags = rec->e_flags;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006696
6697 /*
6698 * If it's now empty, remove this record.
6699 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006700 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006701 memset(rec, 0,
6702 sizeof(struct ocfs2_extent_rec));
6703 le16_add_cpu(&el->l_next_free_rec, -1);
6704 }
6705 } else {
6706 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6707 memset(rec, 0,
6708 sizeof(struct ocfs2_extent_rec));
6709 le16_add_cpu(&el->l_next_free_rec, -1);
6710
6711 goto delete;
6712 }
6713
6714 /* Can this actually happen? */
6715 if (le16_to_cpu(el->l_next_free_rec) == 0)
6716 goto delete;
6717
6718 /*
6719 * We never actually deleted any clusters
6720 * because our leaf was empty. There's no
6721 * reason to adjust the rightmost edge then.
6722 */
6723 if (new_edge == 0)
6724 goto delete;
6725
Mark Fashehe48edee2007-03-07 16:46:57 -08006726 rec->e_int_clusters = cpu_to_le32(new_edge);
6727 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006728 -le32_to_cpu(rec->e_cpos));
6729
6730 /*
6731 * A deleted child record should have been
6732 * caught above.
6733 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006734 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006735 }
6736
6737delete:
Joel Beckerec20cec2010-03-19 14:13:52 -07006738 ocfs2_journal_dirty(handle, bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006739
6740 mlog(0, "extent list container %llu, after: record %d: "
6741 "(%u, %u, %llu), next = %u.\n",
6742 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006743 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006744 (unsigned long long)le64_to_cpu(rec->e_blkno),
6745 le16_to_cpu(el->l_next_free_rec));
6746
6747 /*
6748 * We must be careful to only attempt delete of an
6749 * extent block (and not the root inode block).
6750 */
6751 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6752 struct ocfs2_extent_block *eb =
6753 (struct ocfs2_extent_block *)bh->b_data;
6754
6755 /*
6756 * Save this for use when processing the
6757 * parent block.
6758 */
6759 deleted_eb = le64_to_cpu(eb->h_blkno);
6760
6761 mlog(0, "deleting this extent block.\n");
6762
Joel Becker8cb471e2009-02-10 20:00:41 -08006763 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006764
Mark Fashehe48edee2007-03-07 16:46:57 -08006765 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006766 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6767 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6768
Mark Fasheh59a5e412007-06-22 15:52:36 -07006769 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6770 /* An error here is not fatal. */
6771 if (ret < 0)
6772 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006773 } else {
6774 deleted_eb = 0;
6775 }
6776
6777 index--;
6778 }
6779
6780 ret = 0;
6781out:
6782 return ret;
6783}
6784
Mark Fashehccd979b2005-12-15 14:31:24 -08006785static int ocfs2_do_truncate(struct ocfs2_super *osb,
6786 unsigned int clusters_to_del,
6787 struct inode *inode,
6788 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006789 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006790 struct ocfs2_truncate_context *tc,
Tao Mabcbbb242009-08-18 11:29:12 +08006791 struct ocfs2_path *path,
6792 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08006793{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006794 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006795 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006796 struct ocfs2_extent_block *last_eb = NULL;
6797 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006798 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006799 u64 delete_blk = 0;
Tao Mabcbbb242009-08-18 11:29:12 +08006800 u8 rec_flags;
Mark Fashehccd979b2005-12-15 14:31:24 -08006801
6802 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6803
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006804 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006805 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006806 if (status < 0) {
6807 mlog_errno(status);
6808 goto bail;
6809 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006810
Mark Fashehdcd05382007-01-16 11:32:23 -08006811 /*
6812 * Each component will be touched, so we might as well journal
6813 * here to avoid having to handle errors later.
6814 */
Joel Becker0cf2f762009-02-12 16:41:25 -08006815 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006816 if (status < 0) {
6817 mlog_errno(status);
6818 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006819 }
6820
6821 if (last_eb_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08006822 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07006823 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08006824 if (status < 0) {
6825 mlog_errno(status);
6826 goto bail;
6827 }
6828
6829 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6830 }
6831
6832 el = &(fe->id2.i_list);
6833
6834 /*
6835 * Lower levels depend on this never happening, but it's best
6836 * to check it up here before changing the tree.
6837 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006838 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006839 ocfs2_error(inode->i_sb,
6840 "Inode %lu has an empty extent record, depth %u\n",
6841 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006842 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006843 goto bail;
6844 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006845
Christoph Hellwig5dd40562010-03-03 09:05:00 -05006846 dquot_free_space_nodirty(inode,
Jan Karaa90714c2008-10-09 19:38:40 +02006847 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006848 spin_lock(&OCFS2_I(inode)->ip_lock);
6849 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6850 clusters_to_del;
6851 spin_unlock(&OCFS2_I(inode)->ip_lock);
6852 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006853 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006854
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006855 status = ocfs2_trim_tree(inode, path, handle, tc,
Tao Mabcbbb242009-08-18 11:29:12 +08006856 clusters_to_del, &delete_blk, &rec_flags);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006857 if (status) {
6858 mlog_errno(status);
6859 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006860 }
6861
Mark Fashehdcd05382007-01-16 11:32:23 -08006862 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006863 /* trunc to zero is a special case. */
6864 el->l_tree_depth = 0;
6865 fe->i_last_eb_blk = 0;
6866 } else if (last_eb)
6867 fe->i_last_eb_blk = last_eb->h_blkno;
6868
Joel Beckerec20cec2010-03-19 14:13:52 -07006869 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006870
6871 if (last_eb) {
6872 /* If there will be a new last extent block, then by
6873 * definition, there cannot be any leaves to the right of
6874 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006875 last_eb->h_next_leaf_blk = 0;
Joel Beckerec20cec2010-03-19 14:13:52 -07006876 ocfs2_journal_dirty(handle, last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006877 }
6878
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006879 if (delete_blk) {
Tao Mabcbbb242009-08-18 11:29:12 +08006880 if (rec_flags & OCFS2_EXT_REFCOUNTED)
6881 status = ocfs2_decrease_refcount(inode, handle,
6882 ocfs2_blocks_to_clusters(osb->sb,
6883 delete_blk),
6884 clusters_to_del, meta_ac,
Tao Ma6ae23c52009-08-18 11:30:55 +08006885 &tc->tc_dealloc, 1);
Tao Mabcbbb242009-08-18 11:29:12 +08006886 else
6887 status = ocfs2_truncate_log_append(osb, handle,
6888 delete_blk,
6889 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006890 if (status < 0) {
6891 mlog_errno(status);
6892 goto bail;
6893 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006894 }
6895 status = 0;
6896bail:
Tao Ma60e2ec42009-08-12 14:42:47 +08006897 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006898 mlog_exit(status);
6899 return status;
6900}
6901
Joel Becker2b4e30f2008-09-03 20:03:41 -07006902static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006903{
6904 set_buffer_uptodate(bh);
6905 mark_buffer_dirty(bh);
6906 return 0;
6907}
6908
Tao Ma6f70fa52009-08-25 08:05:12 +08006909void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6910 unsigned int from, unsigned int to,
6911 struct page *page, int zero, u64 *phys)
Mark Fasheh1d410a62007-09-07 14:20:45 -07006912{
6913 int ret, partial = 0;
6914
6915 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6916 if (ret)
6917 mlog_errno(ret);
6918
6919 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006920 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006921
6922 /*
6923 * Need to set the buffers we zero'd into uptodate
6924 * here if they aren't - ocfs2_map_page_blocks()
6925 * might've skipped some
6926 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006927 ret = walk_page_buffers(handle, page_buffers(page),
6928 from, to, &partial,
6929 ocfs2_zero_func);
6930 if (ret < 0)
6931 mlog_errno(ret);
6932 else if (ocfs2_should_order_data(inode)) {
6933 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006934 if (ret < 0)
6935 mlog_errno(ret);
6936 }
6937
6938 if (!partial)
6939 SetPageUptodate(page);
6940
6941 flush_dcache_page(page);
6942}
6943
Mark Fasheh35edec12007-07-06 14:41:18 -07006944static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6945 loff_t end, struct page **pages,
6946 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006947{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006948 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006949 struct page *page;
6950 unsigned int from, to = PAGE_CACHE_SIZE;
6951 struct super_block *sb = inode->i_sb;
6952
6953 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6954
6955 if (numpages == 0)
6956 goto out;
6957
Mark Fasheh35edec12007-07-06 14:41:18 -07006958 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006959 for(i = 0; i < numpages; i++) {
6960 page = pages[i];
6961
Mark Fasheh35edec12007-07-06 14:41:18 -07006962 from = start & (PAGE_CACHE_SIZE - 1);
6963 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6964 to = end & (PAGE_CACHE_SIZE - 1);
6965
Mark Fasheh60b11392007-02-16 11:46:50 -08006966 BUG_ON(from > PAGE_CACHE_SIZE);
6967 BUG_ON(to > PAGE_CACHE_SIZE);
6968
Mark Fasheh1d410a62007-09-07 14:20:45 -07006969 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6970 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006971
Mark Fasheh35edec12007-07-06 14:41:18 -07006972 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006973 }
6974out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006975 if (pages)
6976 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006977}
6978
Tao Ma6f70fa52009-08-25 08:05:12 +08006979int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
6980 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006981{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006982 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006983 struct address_space *mapping = inode->i_mapping;
6984 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006985 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006986
Mark Fasheh35edec12007-07-06 14:41:18 -07006987 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006988
Mark Fasheh1d410a62007-09-07 14:20:45 -07006989 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006990 last_page_bytes = PAGE_ALIGN(end);
6991 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006992 do {
6993 pages[numpages] = grab_cache_page(mapping, index);
6994 if (!pages[numpages]) {
6995 ret = -ENOMEM;
6996 mlog_errno(ret);
6997 goto out;
6998 }
6999
7000 numpages++;
7001 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07007002 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08007003
7004out:
7005 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07007006 if (pages)
7007 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08007008 numpages = 0;
7009 }
7010
7011 *num = numpages;
7012
7013 return ret;
7014}
7015
Tao Ma6f70fa52009-08-25 08:05:12 +08007016static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
7017 struct page **pages, int *num)
7018{
7019 struct super_block *sb = inode->i_sb;
7020
7021 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
7022 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
7023
7024 return ocfs2_grab_pages(inode, start, end, pages, num);
7025}
7026
Mark Fasheh60b11392007-02-16 11:46:50 -08007027/*
7028 * Zero the area past i_size but still within an allocated
7029 * cluster. This avoids exposing nonzero data on subsequent file
7030 * extends.
7031 *
7032 * We need to call this before i_size is updated on the inode because
7033 * otherwise block_write_full_page() will skip writeout of pages past
7034 * i_size. The new_i_size parameter is passed for this reason.
7035 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007036int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
7037 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08007038{
Mark Fasheh1d410a62007-09-07 14:20:45 -07007039 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08007040 struct page **pages = NULL;
7041 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07007042 unsigned int ext_flags;
7043 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08007044
7045 /*
7046 * File systems which don't support sparse files zero on every
7047 * extend.
7048 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07007049 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08007050 return 0;
7051
Mark Fasheh1d410a62007-09-07 14:20:45 -07007052 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08007053 sizeof(struct page *), GFP_NOFS);
7054 if (pages == NULL) {
7055 ret = -ENOMEM;
7056 mlog_errno(ret);
7057 goto out;
7058 }
7059
Mark Fasheh1d410a62007-09-07 14:20:45 -07007060 if (range_start == range_end)
7061 goto out;
7062
7063 ret = ocfs2_extent_map_get_blocks(inode,
7064 range_start >> sb->s_blocksize_bits,
7065 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08007066 if (ret) {
7067 mlog_errno(ret);
7068 goto out;
7069 }
7070
Mark Fasheh1d410a62007-09-07 14:20:45 -07007071 /*
7072 * Tail is a hole, or is marked unwritten. In either case, we
7073 * can count on read and write to return/push zero's.
7074 */
7075 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08007076 goto out;
7077
Mark Fasheh1d410a62007-09-07 14:20:45 -07007078 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7079 &numpages);
7080 if (ret) {
7081 mlog_errno(ret);
7082 goto out;
7083 }
7084
Mark Fasheh35edec12007-07-06 14:41:18 -07007085 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7086 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08007087
7088 /*
7089 * Initiate writeout of the pages we zero'd here. We don't
7090 * wait on them - the truncate_inode_pages() call later will
7091 * do that for us.
7092 */
Christoph Hellwig2cfd30a2009-09-23 15:04:02 +02007093 ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
7094 range_end - 1);
Mark Fasheh60b11392007-02-16 11:46:50 -08007095 if (ret)
7096 mlog_errno(ret);
7097
7098out:
7099 if (pages)
7100 kfree(pages);
7101
7102 return ret;
7103}
7104
Tiger Yangfdd77702008-08-18 17:08:55 +08007105static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7106 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007107{
7108 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08007109 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007110
Tiger Yangfdd77702008-08-18 17:08:55 +08007111 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7112 memset(&di->id2, 0, blocksize -
7113 offsetof(struct ocfs2_dinode, id2) -
7114 xattrsize);
7115 else
7116 memset(&di->id2, 0, blocksize -
7117 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007118}
7119
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007120void ocfs2_dinode_new_extent_list(struct inode *inode,
7121 struct ocfs2_dinode *di)
7122{
Tiger Yangfdd77702008-08-18 17:08:55 +08007123 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007124 di->id2.i_list.l_tree_depth = 0;
7125 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08007126 di->id2.i_list.l_count = cpu_to_le16(
7127 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007128}
7129
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007130void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7131{
7132 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7133 struct ocfs2_inline_data *idata = &di->id2.i_data;
7134
7135 spin_lock(&oi->ip_lock);
7136 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7137 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7138 spin_unlock(&oi->ip_lock);
7139
7140 /*
7141 * We clear the entire i_data structure here so that all
7142 * fields can be properly initialized.
7143 */
Tiger Yangfdd77702008-08-18 17:08:55 +08007144 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007145
Tiger Yangfdd77702008-08-18 17:08:55 +08007146 idata->id_count = cpu_to_le16(
7147 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007148}
7149
7150int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7151 struct buffer_head *di_bh)
7152{
7153 int ret, i, has_data, num_pages = 0;
7154 handle_t *handle;
7155 u64 uninitialized_var(block);
7156 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7157 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7158 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007159 struct ocfs2_alloc_context *data_ac = NULL;
7160 struct page **pages = NULL;
7161 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07007162 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02007163 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007164
7165 has_data = i_size_read(inode) ? 1 : 0;
7166
7167 if (has_data) {
7168 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7169 sizeof(struct page *), GFP_NOFS);
7170 if (pages == NULL) {
7171 ret = -ENOMEM;
7172 mlog_errno(ret);
7173 goto out;
7174 }
7175
7176 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7177 if (ret) {
7178 mlog_errno(ret);
7179 goto out;
7180 }
7181 }
7182
Jan Karaa90714c2008-10-09 19:38:40 +02007183 handle = ocfs2_start_trans(osb,
7184 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007185 if (IS_ERR(handle)) {
7186 ret = PTR_ERR(handle);
7187 mlog_errno(ret);
7188 goto out_unlock;
7189 }
7190
Joel Becker0cf2f762009-02-12 16:41:25 -08007191 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007192 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007193 if (ret) {
7194 mlog_errno(ret);
7195 goto out_commit;
7196 }
7197
7198 if (has_data) {
7199 u32 bit_off, num;
7200 unsigned int page_end;
7201 u64 phys;
7202
Christoph Hellwig5dd40562010-03-03 09:05:00 -05007203 ret = dquot_alloc_space_nodirty(inode,
7204 ocfs2_clusters_to_bytes(osb->sb, 1));
7205 if (ret)
Jan Karaa90714c2008-10-09 19:38:40 +02007206 goto out_commit;
Jan Karaa90714c2008-10-09 19:38:40 +02007207 did_quota = 1;
7208
Mark Fasheh4fe370a2009-12-07 13:15:40 -08007209 data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
7210
Joel Becker1ed9b772010-05-06 13:59:06 +08007211 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007212 &num);
7213 if (ret) {
7214 mlog_errno(ret);
7215 goto out_commit;
7216 }
7217
7218 /*
7219 * Save two copies, one for insert, and one that can
7220 * be changed by ocfs2_map_and_dirty_page() below.
7221 */
7222 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7223
7224 /*
7225 * Non sparse file systems zero on extend, so no need
7226 * to do that now.
7227 */
7228 if (!ocfs2_sparse_alloc(osb) &&
7229 PAGE_CACHE_SIZE < osb->s_clustersize)
7230 end = PAGE_CACHE_SIZE;
7231
7232 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7233 if (ret) {
7234 mlog_errno(ret);
7235 goto out_commit;
7236 }
7237
7238 /*
7239 * This should populate the 1st page for us and mark
7240 * it up to date.
7241 */
7242 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7243 if (ret) {
7244 mlog_errno(ret);
7245 goto out_commit;
7246 }
7247
7248 page_end = PAGE_CACHE_SIZE;
7249 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7250 page_end = osb->s_clustersize;
7251
7252 for (i = 0; i < num_pages; i++)
7253 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7254 pages[i], i > 0, &phys);
7255 }
7256
7257 spin_lock(&oi->ip_lock);
7258 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7259 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7260 spin_unlock(&oi->ip_lock);
7261
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007262 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007263
7264 ocfs2_journal_dirty(handle, di_bh);
7265
7266 if (has_data) {
7267 /*
7268 * An error at this point should be extremely rare. If
7269 * this proves to be false, we could always re-build
7270 * the in-inode data from our pages.
7271 */
Joel Becker5e404e92009-02-13 03:54:22 -08007272 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
Joel Beckercc79d8c2009-02-13 03:24:43 -08007273 ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007274 if (ret) {
7275 mlog_errno(ret);
7276 goto out_commit;
7277 }
7278
7279 inode->i_blocks = ocfs2_inode_sector_count(inode);
7280 }
7281
7282out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007283 if (ret < 0 && did_quota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05007284 dquot_free_space_nodirty(inode,
Jan Karaa90714c2008-10-09 19:38:40 +02007285 ocfs2_clusters_to_bytes(osb->sb, 1));
7286
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007287 ocfs2_commit_trans(osb, handle);
7288
7289out_unlock:
7290 if (data_ac)
7291 ocfs2_free_alloc_context(data_ac);
7292
7293out:
7294 if (pages) {
7295 ocfs2_unlock_and_free_pages(pages, num_pages);
7296 kfree(pages);
7297 }
7298
7299 return ret;
7300}
7301
Mark Fashehccd979b2005-12-15 14:31:24 -08007302/*
7303 * It is expected, that by the time you call this function,
7304 * inode->i_size and fe->i_size have been adjusted.
7305 *
7306 * WARNING: This will kfree the truncate context
7307 */
7308int ocfs2_commit_truncate(struct ocfs2_super *osb,
7309 struct inode *inode,
7310 struct buffer_head *fe_bh,
7311 struct ocfs2_truncate_context *tc)
7312{
7313 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08007314 u32 clusters_to_del, new_highest_cpos, range;
Tao Mabcbbb242009-08-18 11:29:12 +08007315 u64 blkno = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08007316 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07007317 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007318 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08007319 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08007320 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Tao Mabcbbb242009-08-18 11:29:12 +08007321 struct ocfs2_alloc_context *meta_ac = NULL;
7322 struct ocfs2_refcount_tree *ref_tree = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007323
7324 mlog_entry_void();
7325
Mark Fashehdcd05382007-01-16 11:32:23 -08007326 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007327 i_size_read(inode));
7328
Joel Becker13723d02008-10-17 19:25:01 -07007329 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7330 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08007331 if (!path) {
7332 status = -ENOMEM;
7333 mlog_errno(status);
7334 goto bail;
7335 }
Mark Fasheh83418972007-04-23 18:53:12 -07007336
7337 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7338
Mark Fashehccd979b2005-12-15 14:31:24 -08007339start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007340 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007341 * Check that we still have allocation to delete.
7342 */
7343 if (OCFS2_I(inode)->ip_clusters == 0) {
7344 status = 0;
7345 goto bail;
7346 }
7347
Tao Mabcbbb242009-08-18 11:29:12 +08007348 credits = 0;
7349
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007350 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007351 * Truncate always works against the rightmost tree branch.
7352 */
Joel Beckerfacdb772009-02-12 18:08:48 -08007353 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
Mark Fashehdcd05382007-01-16 11:32:23 -08007354 if (status) {
7355 mlog_errno(status);
7356 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007357 }
7358
Mark Fashehdcd05382007-01-16 11:32:23 -08007359 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7360 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7361
7362 /*
7363 * By now, el will point to the extent list on the bottom most
7364 * portion of this tree. Only the tail record is considered in
7365 * each pass.
7366 *
7367 * We handle the following cases, in order:
7368 * - empty extent: delete the remaining branch
7369 * - remove the entire record
7370 * - remove a partial record
7371 * - no record needs to be removed (truncate has completed)
7372 */
7373 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007374 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7375 ocfs2_error(inode->i_sb,
7376 "Inode %llu has empty extent block at %llu\n",
7377 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7378 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7379 status = -EROFS;
7380 goto bail;
7381 }
7382
Mark Fashehccd979b2005-12-15 14:31:24 -08007383 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08007384 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08007385 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007386 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7387 clusters_to_del = 0;
7388 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007389 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Tao Mabcbbb242009-08-18 11:29:12 +08007390 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08007391 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007392 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08007393 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08007394 new_highest_cpos;
Tao Mabcbbb242009-08-18 11:29:12 +08007395 blkno = le64_to_cpu(el->l_recs[i].e_blkno) +
7396 ocfs2_clusters_to_blocks(inode->i_sb,
7397 ocfs2_rec_clusters(el, &el->l_recs[i]) -
7398 clusters_to_del);
Mark Fashehdcd05382007-01-16 11:32:23 -08007399 } else {
7400 status = 0;
7401 goto bail;
7402 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007403
Mark Fashehdcd05382007-01-16 11:32:23 -08007404 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7405 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7406
Tao Mabcbbb242009-08-18 11:29:12 +08007407 if (el->l_recs[i].e_flags & OCFS2_EXT_REFCOUNTED && clusters_to_del) {
7408 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
7409 OCFS2_HAS_REFCOUNT_FL));
7410
7411 status = ocfs2_lock_refcount_tree(osb,
7412 le64_to_cpu(di->i_refcount_loc),
7413 1, &ref_tree, NULL);
7414 if (status) {
7415 mlog_errno(status);
7416 goto bail;
7417 }
7418
7419 status = ocfs2_prepare_refcount_change_for_del(inode, fe_bh,
7420 blkno,
7421 clusters_to_del,
7422 &credits,
7423 &meta_ac);
7424 if (status < 0) {
7425 mlog_errno(status);
7426 goto bail;
7427 }
7428 }
7429
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007430 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007431 tl_sem = 1;
7432 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7433 * record is free for use. If there isn't any, we flush to get
7434 * an empty truncate log. */
7435 if (ocfs2_truncate_log_needs_flush(osb)) {
7436 status = __ocfs2_flush_truncate_log(osb);
7437 if (status < 0) {
7438 mlog_errno(status);
7439 goto bail;
7440 }
7441 }
7442
Tao Mabcbbb242009-08-18 11:29:12 +08007443 credits += ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007444 (struct ocfs2_dinode *)fe_bh->b_data,
7445 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007446 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007447 if (IS_ERR(handle)) {
7448 status = PTR_ERR(handle);
7449 handle = NULL;
7450 mlog_errno(status);
7451 goto bail;
7452 }
7453
Mark Fashehdcd05382007-01-16 11:32:23 -08007454 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
Tao Mabcbbb242009-08-18 11:29:12 +08007455 tc, path, meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08007456 if (status < 0) {
7457 mlog_errno(status);
7458 goto bail;
7459 }
7460
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007461 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007462 tl_sem = 0;
7463
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007464 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007465 handle = NULL;
7466
Mark Fashehdcd05382007-01-16 11:32:23 -08007467 ocfs2_reinit_path(path, 1);
7468
Tao Mabcbbb242009-08-18 11:29:12 +08007469 if (meta_ac) {
7470 ocfs2_free_alloc_context(meta_ac);
7471 meta_ac = NULL;
7472 }
7473
7474 if (ref_tree) {
7475 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
7476 ref_tree = NULL;
7477 }
7478
Mark Fashehdcd05382007-01-16 11:32:23 -08007479 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007480 * The check above will catch the case where we've truncated
7481 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007482 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007483 goto start;
7484
Mark Fashehccd979b2005-12-15 14:31:24 -08007485bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007486
7487 ocfs2_schedule_truncate_log_flush(osb, 1);
7488
7489 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007490 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007491
7492 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007493 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007494
Tao Mabcbbb242009-08-18 11:29:12 +08007495 if (meta_ac)
7496 ocfs2_free_alloc_context(meta_ac);
7497
7498 if (ref_tree)
7499 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
7500
Mark Fasheh59a5e412007-06-22 15:52:36 -07007501 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7502
Mark Fashehdcd05382007-01-16 11:32:23 -08007503 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007504
7505 /* This will drop the ext_alloc cluster lock for us */
7506 ocfs2_free_truncate_context(tc);
7507
7508 mlog_exit(status);
7509 return status;
7510}
7511
Mark Fashehccd979b2005-12-15 14:31:24 -08007512/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007513 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007514 */
7515int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7516 struct inode *inode,
7517 struct buffer_head *fe_bh,
7518 struct ocfs2_truncate_context **tc)
7519{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007520 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007521 unsigned int new_i_clusters;
7522 struct ocfs2_dinode *fe;
7523 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007524 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007525
7526 mlog_entry_void();
7527
7528 *tc = NULL;
7529
7530 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7531 i_size_read(inode));
7532 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7533
7534 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007535 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7536 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007537
Robert P. J. Daycd861282006-12-13 00:34:52 -08007538 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007539 if (!(*tc)) {
7540 status = -ENOMEM;
7541 mlog_errno(status);
7542 goto bail;
7543 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007544 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007545
Mark Fashehccd979b2005-12-15 14:31:24 -08007546 if (fe->id2.i_list.l_tree_depth) {
Joel Becker3d03a302009-02-12 17:49:26 -08007547 status = ocfs2_read_extent_block(INODE_CACHE(inode),
Joel Becker5e965812008-11-13 14:49:16 -08007548 le64_to_cpu(fe->i_last_eb_blk),
7549 &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007550 if (status < 0) {
7551 mlog_errno(status);
7552 goto bail;
7553 }
7554 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007555 }
7556
7557 (*tc)->tc_last_eb_bh = last_eb_bh;
7558
Mark Fashehccd979b2005-12-15 14:31:24 -08007559 status = 0;
7560bail:
7561 if (status < 0) {
7562 if (*tc)
7563 ocfs2_free_truncate_context(*tc);
7564 *tc = NULL;
7565 }
7566 mlog_exit_void();
7567 return status;
7568}
7569
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007570/*
7571 * 'start' is inclusive, 'end' is not.
7572 */
7573int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7574 unsigned int start, unsigned int end, int trunc)
7575{
7576 int ret;
7577 unsigned int numbytes;
7578 handle_t *handle;
7579 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7580 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7581 struct ocfs2_inline_data *idata = &di->id2.i_data;
7582
7583 if (end > i_size_read(inode))
7584 end = i_size_read(inode);
7585
7586 BUG_ON(start >= end);
7587
7588 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7589 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7590 !ocfs2_supports_inline_data(osb)) {
7591 ocfs2_error(inode->i_sb,
7592 "Inline data flags for inode %llu don't agree! "
7593 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7594 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7595 le16_to_cpu(di->i_dyn_features),
7596 OCFS2_I(inode)->ip_dyn_features,
7597 osb->s_feature_incompat);
7598 ret = -EROFS;
7599 goto out;
7600 }
7601
7602 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7603 if (IS_ERR(handle)) {
7604 ret = PTR_ERR(handle);
7605 mlog_errno(ret);
7606 goto out;
7607 }
7608
Joel Becker0cf2f762009-02-12 16:41:25 -08007609 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007610 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007611 if (ret) {
7612 mlog_errno(ret);
7613 goto out_commit;
7614 }
7615
7616 numbytes = end - start;
7617 memset(idata->id_data + start, 0, numbytes);
7618
7619 /*
7620 * No need to worry about the data page here - it's been
7621 * truncated already and inline data doesn't need it for
7622 * pushing zero's to disk, so we'll let readpage pick it up
7623 * later.
7624 */
7625 if (trunc) {
7626 i_size_write(inode, start);
7627 di->i_size = cpu_to_le64(start);
7628 }
7629
7630 inode->i_blocks = ocfs2_inode_sector_count(inode);
7631 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7632
7633 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7634 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7635
7636 ocfs2_journal_dirty(handle, di_bh);
7637
7638out_commit:
7639 ocfs2_commit_trans(osb, handle);
7640
7641out:
7642 return ret;
7643}
7644
Mark Fashehccd979b2005-12-15 14:31:24 -08007645static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7646{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007647 /*
7648 * The caller is responsible for completing deallocation
7649 * before freeing the context.
7650 */
7651 if (tc->tc_dealloc.c_first_suballocator != NULL)
7652 mlog(ML_NOTICE,
7653 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007654
Mark Fasheha81cb882008-10-07 14:25:16 -07007655 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007656
7657 kfree(tc);
7658}