blob: b6e2ba1f6a7b79db6d21c747c4700c2b51048521 [file] [log] [blame]
Mark Fashehccd979b2005-12-15 14:31:24 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * alloc.c
5 *
6 * Extent allocs and frees
7 *
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 */
25
26#include <linux/fs.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/highmem.h>
Mark Fasheh60b11392007-02-16 11:46:50 -080030#include <linux/swap.h>
Jan Karaa90714c2008-10-09 19:38:40 +020031#include <linux/quotaops.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080032
33#define MLOG_MASK_PREFIX ML_DISK_ALLOC
34#include <cluster/masklog.h>
35
36#include "ocfs2.h"
37
38#include "alloc.h"
Mark Fasheh60b11392007-02-16 11:46:50 -080039#include "aops.h"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070040#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080041#include "dlmglue.h"
42#include "extent_map.h"
43#include "inode.h"
44#include "journal.h"
45#include "localalloc.h"
46#include "suballoc.h"
47#include "sysfile.h"
48#include "file.h"
49#include "super.h"
50#include "uptodate.h"
Joel Becker2a50a742008-12-09 14:24:33 -080051#include "xattr.h"
Tao Mabcbbb242009-08-18 11:29:12 +080052#include "refcounttree.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080053
54#include "buffer_head_io.h"
55
Tao Ma853a3a12009-08-18 11:22:18 +080056enum ocfs2_contig_type {
57 CONTIG_NONE = 0,
58 CONTIG_LEFT,
59 CONTIG_RIGHT,
60 CONTIG_LEFTRIGHT,
61};
Tao Mae7d4cb62008-08-18 17:38:44 +080062
Tao Ma853a3a12009-08-18 11:22:18 +080063static enum ocfs2_contig_type
64 ocfs2_extent_rec_contig(struct super_block *sb,
65 struct ocfs2_extent_rec *ext,
66 struct ocfs2_extent_rec *insert_rec);
Joel Becker1625f8a2008-08-21 17:11:10 -070067/*
68 * Operations for a specific extent tree type.
69 *
70 * To implement an on-disk btree (extent tree) type in ocfs2, add
71 * an ocfs2_extent_tree_operations structure and the matching
Joel Becker8d6220d2008-08-22 12:46:09 -070072 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
Joel Becker1625f8a2008-08-21 17:11:10 -070073 * for the allocation portion of the extent tree.
74 */
Tao Mae7d4cb62008-08-18 17:38:44 +080075struct ocfs2_extent_tree_operations {
Joel Becker1625f8a2008-08-21 17:11:10 -070076 /*
77 * last_eb_blk is the block number of the right most leaf extent
78 * block. Most on-disk structures containing an extent tree store
79 * this value for fast access. The ->eo_set_last_eb_blk() and
80 * ->eo_get_last_eb_blk() operations access this value. They are
81 * both required.
82 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070083 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
84 u64 blkno);
85 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -070086
87 /*
88 * The on-disk structure usually keeps track of how many total
89 * clusters are stored in this extent tree. This function updates
90 * that value. new_clusters is the delta, and must be
91 * added to the total. Required.
92 */
Joel Becker6136ca52009-02-12 19:32:43 -080093 void (*eo_update_clusters)(struct ocfs2_extent_tree *et,
Joel Becker35dc0aa2008-08-20 16:25:06 -070094 u32 new_clusters);
Joel Becker1625f8a2008-08-21 17:11:10 -070095
96 /*
Joel Becker92ba4702009-02-13 03:18:34 -080097 * If this extent tree is supported by an extent map, insert
98 * a record into the map.
99 */
100 void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et,
101 struct ocfs2_extent_rec *rec);
102
103 /*
Joel Becker4c911ee2009-02-13 02:50:12 -0800104 * If this extent tree is supported by an extent map, truncate the
105 * map to clusters,
106 */
107 void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et,
108 u32 clusters);
109
110 /*
Joel Becker1625f8a2008-08-21 17:11:10 -0700111 * If ->eo_insert_check() exists, it is called before rec is
112 * inserted into the extent tree. It is optional.
113 */
Joel Becker6136ca52009-02-12 19:32:43 -0800114 int (*eo_insert_check)(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700115 struct ocfs2_extent_rec *rec);
Joel Becker6136ca52009-02-12 19:32:43 -0800116 int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
Joel Becker0ce10102008-08-20 17:19:50 -0700117
Joel Becker1625f8a2008-08-21 17:11:10 -0700118 /*
119 * --------------------------------------------------------------
120 * The remaining are internal to ocfs2_extent_tree and don't have
121 * accessor functions
122 */
123
124 /*
125 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
126 * It is required.
127 */
Joel Becker0ce10102008-08-20 17:19:50 -0700128 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -0700129
130 /*
131 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
132 * it exists. If it does not, et->et_max_leaf_clusters is set
133 * to 0 (unlimited). Optional.
134 */
Joel Becker6136ca52009-02-12 19:32:43 -0800135 void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
Tao Ma853a3a12009-08-18 11:22:18 +0800136
137 /*
138 * ->eo_extent_contig test whether the 2 ocfs2_extent_rec
139 * are contiguous or not. Optional. Don't need to set it if use
140 * ocfs2_extent_rec as the tree leaf.
141 */
142 enum ocfs2_contig_type
143 (*eo_extent_contig)(struct ocfs2_extent_tree *et,
144 struct ocfs2_extent_rec *ext,
145 struct ocfs2_extent_rec *insert_rec);
Tao Mae7d4cb62008-08-18 17:38:44 +0800146};
147
Joel Beckerf99b9b72008-08-20 19:36:33 -0700148
149/*
150 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
151 * in the methods.
152 */
153static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
154static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
155 u64 blkno);
Joel Becker6136ca52009-02-12 19:32:43 -0800156static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700157 u32 clusters);
Joel Becker92ba4702009-02-13 03:18:34 -0800158static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
159 struct ocfs2_extent_rec *rec);
Joel Becker4c911ee2009-02-13 02:50:12 -0800160static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
161 u32 clusters);
Joel Becker6136ca52009-02-12 19:32:43 -0800162static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700163 struct ocfs2_extent_rec *rec);
Joel Becker6136ca52009-02-12 19:32:43 -0800164static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700165static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
166static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
167 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
168 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
169 .eo_update_clusters = ocfs2_dinode_update_clusters,
Joel Becker92ba4702009-02-13 03:18:34 -0800170 .eo_extent_map_insert = ocfs2_dinode_extent_map_insert,
Joel Becker4c911ee2009-02-13 02:50:12 -0800171 .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700172 .eo_insert_check = ocfs2_dinode_insert_check,
173 .eo_sanity_check = ocfs2_dinode_sanity_check,
174 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
Tao Mae7d4cb62008-08-18 17:38:44 +0800175};
176
177static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
178 u64 blkno)
179{
Joel Beckerea5efa12008-08-20 16:57:27 -0700180 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800181
Joel Beckerf99b9b72008-08-20 19:36:33 -0700182 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800183 di->i_last_eb_blk = cpu_to_le64(blkno);
184}
185
186static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
187{
Joel Beckerea5efa12008-08-20 16:57:27 -0700188 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800189
Joel Beckerf99b9b72008-08-20 19:36:33 -0700190 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800191 return le64_to_cpu(di->i_last_eb_blk);
192}
193
Joel Becker6136ca52009-02-12 19:32:43 -0800194static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +0800195 u32 clusters)
196{
Joel Becker6136ca52009-02-12 19:32:43 -0800197 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
Joel Beckerea5efa12008-08-20 16:57:27 -0700198 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800199
200 le32_add_cpu(&di->i_clusters, clusters);
Joel Becker6136ca52009-02-12 19:32:43 -0800201 spin_lock(&oi->ip_lock);
202 oi->ip_clusters = le32_to_cpu(di->i_clusters);
203 spin_unlock(&oi->ip_lock);
Tao Mae7d4cb62008-08-18 17:38:44 +0800204}
205
Joel Becker92ba4702009-02-13 03:18:34 -0800206static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
207 struct ocfs2_extent_rec *rec)
208{
209 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
210
211 ocfs2_extent_map_insert_rec(inode, rec);
212}
213
Joel Becker4c911ee2009-02-13 02:50:12 -0800214static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
215 u32 clusters)
216{
217 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
218
219 ocfs2_extent_map_trunc(inode, clusters);
220}
221
Joel Becker6136ca52009-02-12 19:32:43 -0800222static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700223 struct ocfs2_extent_rec *rec)
224{
Joel Becker6136ca52009-02-12 19:32:43 -0800225 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
226 struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
Joel Becker1e61ee72008-08-20 18:32:45 -0700227
Joel Becker6136ca52009-02-12 19:32:43 -0800228 BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
Joel Becker1e61ee72008-08-20 18:32:45 -0700229 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
Joel Becker6136ca52009-02-12 19:32:43 -0800230 (oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
Joel Becker1e61ee72008-08-20 18:32:45 -0700231 "Device %s, asking for sparse allocation: inode %llu, "
232 "cpos %u, clusters %u\n",
233 osb->dev_str,
Joel Becker6136ca52009-02-12 19:32:43 -0800234 (unsigned long long)oi->ip_blkno,
235 rec->e_cpos, oi->ip_clusters);
Joel Becker1e61ee72008-08-20 18:32:45 -0700236
237 return 0;
238}
239
Joel Becker6136ca52009-02-12 19:32:43 -0800240static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800241{
Joel Becker10995aa2008-11-13 14:49:12 -0800242 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800243
Joel Beckerf99b9b72008-08-20 19:36:33 -0700244 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Joel Becker10995aa2008-11-13 14:49:12 -0800245 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
Tao Mae7d4cb62008-08-18 17:38:44 +0800246
Joel Becker10995aa2008-11-13 14:49:12 -0800247 return 0;
Tao Mae7d4cb62008-08-18 17:38:44 +0800248}
249
Joel Beckerf99b9b72008-08-20 19:36:33 -0700250static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
251{
252 struct ocfs2_dinode *di = et->et_object;
253
254 et->et_root_el = &di->id2.i_list;
255}
256
Tao Mae7d4cb62008-08-18 17:38:44 +0800257
Joel Becker0ce10102008-08-20 17:19:50 -0700258static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
259{
Joel Becker2a50a742008-12-09 14:24:33 -0800260 struct ocfs2_xattr_value_buf *vb = et->et_object;
Joel Becker0ce10102008-08-20 17:19:50 -0700261
Joel Becker2a50a742008-12-09 14:24:33 -0800262 et->et_root_el = &vb->vb_xv->xr_list;
Joel Becker0ce10102008-08-20 17:19:50 -0700263}
264
Tao Maf56654c2008-08-18 17:38:48 +0800265static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
266 u64 blkno)
267{
Joel Becker2a50a742008-12-09 14:24:33 -0800268 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800269
Joel Becker2a50a742008-12-09 14:24:33 -0800270 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
Tao Maf56654c2008-08-18 17:38:48 +0800271}
272
273static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
274{
Joel Becker2a50a742008-12-09 14:24:33 -0800275 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800276
Joel Becker2a50a742008-12-09 14:24:33 -0800277 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
Tao Maf56654c2008-08-18 17:38:48 +0800278}
279
Joel Becker6136ca52009-02-12 19:32:43 -0800280static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
Tao Maf56654c2008-08-18 17:38:48 +0800281 u32 clusters)
282{
Joel Becker2a50a742008-12-09 14:24:33 -0800283 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800284
Joel Becker2a50a742008-12-09 14:24:33 -0800285 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800286}
287
Joel Becker1a09f552008-08-20 17:44:24 -0700288static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700289 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
290 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
291 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700292 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800293};
294
Joel Becker0ce10102008-08-20 17:19:50 -0700295static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
296{
297 struct ocfs2_xattr_block *xb = et->et_object;
298
299 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
300}
301
Joel Becker6136ca52009-02-12 19:32:43 -0800302static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
Joel Becker943cced2008-08-20 17:31:10 -0700303{
Joel Becker6136ca52009-02-12 19:32:43 -0800304 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Joel Becker943cced2008-08-20 17:31:10 -0700305 et->et_max_leaf_clusters =
Joel Becker6136ca52009-02-12 19:32:43 -0800306 ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
Joel Becker943cced2008-08-20 17:31:10 -0700307}
308
Tao Maba492612008-08-18 17:38:49 +0800309static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
310 u64 blkno)
311{
Joel Beckerea5efa12008-08-20 16:57:27 -0700312 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800313 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
314
315 xt->xt_last_eb_blk = cpu_to_le64(blkno);
316}
317
318static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
319{
Joel Beckerea5efa12008-08-20 16:57:27 -0700320 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800321 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
322
323 return le64_to_cpu(xt->xt_last_eb_blk);
324}
325
Joel Becker6136ca52009-02-12 19:32:43 -0800326static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
Tao Maba492612008-08-18 17:38:49 +0800327 u32 clusters)
328{
Joel Beckerea5efa12008-08-20 16:57:27 -0700329 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800330
331 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
332}
333
Tao Maba492612008-08-18 17:38:49 +0800334static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700335 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
336 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
337 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700338 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
Joel Becker943cced2008-08-20 17:31:10 -0700339 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
Tao Maba492612008-08-18 17:38:49 +0800340};
341
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800342static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
343 u64 blkno)
344{
345 struct ocfs2_dx_root_block *dx_root = et->et_object;
346
347 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
348}
349
350static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
351{
352 struct ocfs2_dx_root_block *dx_root = et->et_object;
353
354 return le64_to_cpu(dx_root->dr_last_eb_blk);
355}
356
Joel Becker6136ca52009-02-12 19:32:43 -0800357static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800358 u32 clusters)
359{
360 struct ocfs2_dx_root_block *dx_root = et->et_object;
361
362 le32_add_cpu(&dx_root->dr_clusters, clusters);
363}
364
Joel Becker6136ca52009-02-12 19:32:43 -0800365static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800366{
367 struct ocfs2_dx_root_block *dx_root = et->et_object;
368
369 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
370
371 return 0;
372}
373
374static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
375{
376 struct ocfs2_dx_root_block *dx_root = et->et_object;
377
378 et->et_root_el = &dx_root->dr_list;
379}
380
381static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
382 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
383 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
384 .eo_update_clusters = ocfs2_dx_root_update_clusters,
385 .eo_sanity_check = ocfs2_dx_root_sanity_check,
386 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
387};
388
Tao Mafe924412009-08-18 11:22:25 +0800389static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et)
390{
391 struct ocfs2_refcount_block *rb = et->et_object;
392
393 et->et_root_el = &rb->rf_list;
394}
395
396static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
397 u64 blkno)
398{
399 struct ocfs2_refcount_block *rb = et->et_object;
400
401 rb->rf_last_eb_blk = cpu_to_le64(blkno);
402}
403
404static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
405{
406 struct ocfs2_refcount_block *rb = et->et_object;
407
408 return le64_to_cpu(rb->rf_last_eb_blk);
409}
410
411static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et,
412 u32 clusters)
413{
414 struct ocfs2_refcount_block *rb = et->et_object;
415
416 le32_add_cpu(&rb->rf_clusters, clusters);
417}
418
419static enum ocfs2_contig_type
420ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et,
421 struct ocfs2_extent_rec *ext,
422 struct ocfs2_extent_rec *insert_rec)
423{
424 return CONTIG_NONE;
425}
426
427static struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = {
428 .eo_set_last_eb_blk = ocfs2_refcount_tree_set_last_eb_blk,
429 .eo_get_last_eb_blk = ocfs2_refcount_tree_get_last_eb_blk,
430 .eo_update_clusters = ocfs2_refcount_tree_update_clusters,
431 .eo_fill_root_el = ocfs2_refcount_tree_fill_root_el,
432 .eo_extent_contig = ocfs2_refcount_tree_extent_contig,
433};
434
Joel Becker8d6220d2008-08-22 12:46:09 -0700435static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800436 struct ocfs2_caching_info *ci,
Joel Becker8d6220d2008-08-22 12:46:09 -0700437 struct buffer_head *bh,
Joel Becker13723d02008-10-17 19:25:01 -0700438 ocfs2_journal_access_func access,
Joel Becker8d6220d2008-08-22 12:46:09 -0700439 void *obj,
440 struct ocfs2_extent_tree_operations *ops)
Tao Mae7d4cb62008-08-18 17:38:44 +0800441{
Joel Becker1a09f552008-08-20 17:44:24 -0700442 et->et_ops = ops;
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700443 et->et_root_bh = bh;
Joel Becker5e404e92009-02-13 03:54:22 -0800444 et->et_ci = ci;
Joel Becker13723d02008-10-17 19:25:01 -0700445 et->et_root_journal_access = access;
Joel Beckerea5efa12008-08-20 16:57:27 -0700446 if (!obj)
447 obj = (void *)bh->b_data;
448 et->et_object = obj;
Tao Mae7d4cb62008-08-18 17:38:44 +0800449
Joel Becker0ce10102008-08-20 17:19:50 -0700450 et->et_ops->eo_fill_root_el(et);
Joel Becker943cced2008-08-20 17:31:10 -0700451 if (!et->et_ops->eo_fill_max_leaf_clusters)
452 et->et_max_leaf_clusters = 0;
453 else
Joel Becker6136ca52009-02-12 19:32:43 -0800454 et->et_ops->eo_fill_max_leaf_clusters(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800455}
456
Joel Becker8d6220d2008-08-22 12:46:09 -0700457void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800458 struct ocfs2_caching_info *ci,
Joel Becker8d6220d2008-08-22 12:46:09 -0700459 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700460{
Joel Becker5e404e92009-02-13 03:54:22 -0800461 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di,
Joel Becker13723d02008-10-17 19:25:01 -0700462 NULL, &ocfs2_dinode_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700463}
464
Joel Becker8d6220d2008-08-22 12:46:09 -0700465void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800466 struct ocfs2_caching_info *ci,
Joel Becker8d6220d2008-08-22 12:46:09 -0700467 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700468{
Joel Becker5e404e92009-02-13 03:54:22 -0800469 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb,
Joel Becker13723d02008-10-17 19:25:01 -0700470 NULL, &ocfs2_xattr_tree_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700471}
472
Joel Becker8d6220d2008-08-22 12:46:09 -0700473void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800474 struct ocfs2_caching_info *ci,
Joel Becker2a50a742008-12-09 14:24:33 -0800475 struct ocfs2_xattr_value_buf *vb)
Tao Mae7d4cb62008-08-18 17:38:44 +0800476{
Joel Becker5e404e92009-02-13 03:54:22 -0800477 __ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb,
Joel Becker8d6220d2008-08-22 12:46:09 -0700478 &ocfs2_xattr_value_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800479}
480
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800481void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800482 struct ocfs2_caching_info *ci,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800483 struct buffer_head *bh)
484{
Joel Becker5e404e92009-02-13 03:54:22 -0800485 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800486 NULL, &ocfs2_dx_root_et_ops);
487}
488
Tao Mafe924412009-08-18 11:22:25 +0800489void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
490 struct ocfs2_caching_info *ci,
491 struct buffer_head *bh)
492{
493 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb,
494 NULL, &ocfs2_refcount_tree_et_ops);
495}
496
Joel Becker35dc0aa2008-08-20 16:25:06 -0700497static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
498 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800499{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700500 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800501}
502
Joel Becker35dc0aa2008-08-20 16:25:06 -0700503static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800504{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700505 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800506}
507
Joel Becker6136ca52009-02-12 19:32:43 -0800508static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
Joel Becker35dc0aa2008-08-20 16:25:06 -0700509 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800510{
Joel Becker6136ca52009-02-12 19:32:43 -0800511 et->et_ops->eo_update_clusters(et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700512}
513
Joel Becker92ba4702009-02-13 03:18:34 -0800514static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et,
515 struct ocfs2_extent_rec *rec)
516{
517 if (et->et_ops->eo_extent_map_insert)
518 et->et_ops->eo_extent_map_insert(et, rec);
519}
520
Joel Becker4c911ee2009-02-13 02:50:12 -0800521static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et,
522 u32 clusters)
523{
524 if (et->et_ops->eo_extent_map_truncate)
525 et->et_ops->eo_extent_map_truncate(et, clusters);
526}
527
Joel Becker13723d02008-10-17 19:25:01 -0700528static inline int ocfs2_et_root_journal_access(handle_t *handle,
Joel Becker13723d02008-10-17 19:25:01 -0700529 struct ocfs2_extent_tree *et,
530 int type)
531{
Joel Beckerd9a0a1f2009-02-12 17:32:34 -0800532 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700533 type);
534}
535
Tao Ma853a3a12009-08-18 11:22:18 +0800536static inline enum ocfs2_contig_type
537 ocfs2_et_extent_contig(struct ocfs2_extent_tree *et,
538 struct ocfs2_extent_rec *rec,
539 struct ocfs2_extent_rec *insert_rec)
540{
541 if (et->et_ops->eo_extent_contig)
542 return et->et_ops->eo_extent_contig(et, rec, insert_rec);
543
544 return ocfs2_extent_rec_contig(
545 ocfs2_metadata_cache_get_super(et->et_ci),
546 rec, insert_rec);
547}
548
Joel Becker6136ca52009-02-12 19:32:43 -0800549static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700550 struct ocfs2_extent_rec *rec)
551{
552 int ret = 0;
553
554 if (et->et_ops->eo_insert_check)
Joel Becker6136ca52009-02-12 19:32:43 -0800555 ret = et->et_ops->eo_insert_check(et, rec);
Joel Becker1e61ee72008-08-20 18:32:45 -0700556 return ret;
557}
558
Joel Becker6136ca52009-02-12 19:32:43 -0800559static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
Joel Becker35dc0aa2008-08-20 16:25:06 -0700560{
Joel Becker1e61ee72008-08-20 18:32:45 -0700561 int ret = 0;
562
563 if (et->et_ops->eo_sanity_check)
Joel Becker6136ca52009-02-12 19:32:43 -0800564 ret = et->et_ops->eo_sanity_check(et);
Joel Becker1e61ee72008-08-20 18:32:45 -0700565 return ret;
Tao Mae7d4cb62008-08-18 17:38:44 +0800566}
567
Mark Fashehccd979b2005-12-15 14:31:24 -0800568static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
Mark Fasheh59a5e412007-06-22 15:52:36 -0700569static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
570 struct ocfs2_extent_block *eb);
Joel Beckerd401dc12009-02-13 02:24:10 -0800571static void ocfs2_adjust_rightmost_records(handle_t *handle,
572 struct ocfs2_extent_tree *et,
Tao Ma6b791bc2009-06-12 14:18:36 +0800573 struct ocfs2_path *path,
574 struct ocfs2_extent_rec *insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -0800575/*
576 * Reset the actual path elements so that we can re-use the structure
577 * to build another path. Generally, this involves freeing the buffer
578 * heads.
579 */
Tao Mae2e9f602009-08-18 11:22:34 +0800580void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
Mark Fashehdcd05382007-01-16 11:32:23 -0800581{
582 int i, start = 0, depth = 0;
583 struct ocfs2_path_item *node;
584
585 if (keep_root)
586 start = 1;
587
588 for(i = start; i < path_num_items(path); i++) {
589 node = &path->p_node[i];
590
591 brelse(node->bh);
592 node->bh = NULL;
593 node->el = NULL;
594 }
595
596 /*
597 * Tree depth may change during truncate, or insert. If we're
598 * keeping the root extent list, then make sure that our path
599 * structure reflects the proper depth.
600 */
601 if (keep_root)
602 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
Joel Becker13723d02008-10-17 19:25:01 -0700603 else
604 path_root_access(path) = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -0800605
606 path->p_tree_depth = depth;
607}
608
Tao Mae2e9f602009-08-18 11:22:34 +0800609void ocfs2_free_path(struct ocfs2_path *path)
Mark Fashehdcd05382007-01-16 11:32:23 -0800610{
611 if (path) {
612 ocfs2_reinit_path(path, 0);
613 kfree(path);
614 }
615}
616
617/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700618 * All the elements of src into dest. After this call, src could be freed
619 * without affecting dest.
620 *
621 * Both paths should have the same root. Any non-root elements of dest
622 * will be freed.
623 */
624static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
625{
626 int i;
627
628 BUG_ON(path_root_bh(dest) != path_root_bh(src));
629 BUG_ON(path_root_el(dest) != path_root_el(src));
Joel Becker13723d02008-10-17 19:25:01 -0700630 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fasheh328d5752007-06-18 10:48:04 -0700631
632 ocfs2_reinit_path(dest, 1);
633
634 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
635 dest->p_node[i].bh = src->p_node[i].bh;
636 dest->p_node[i].el = src->p_node[i].el;
637
638 if (dest->p_node[i].bh)
639 get_bh(dest->p_node[i].bh);
640 }
641}
642
643/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800644 * Make the *dest path the same as src and re-initialize src path to
645 * have a root only.
646 */
647static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
648{
649 int i;
650
651 BUG_ON(path_root_bh(dest) != path_root_bh(src));
Joel Becker13723d02008-10-17 19:25:01 -0700652 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fashehdcd05382007-01-16 11:32:23 -0800653
654 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
655 brelse(dest->p_node[i].bh);
656
657 dest->p_node[i].bh = src->p_node[i].bh;
658 dest->p_node[i].el = src->p_node[i].el;
659
660 src->p_node[i].bh = NULL;
661 src->p_node[i].el = NULL;
662 }
663}
664
665/*
666 * Insert an extent block at given index.
667 *
668 * This will not take an additional reference on eb_bh.
669 */
670static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
671 struct buffer_head *eb_bh)
672{
673 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
674
675 /*
676 * Right now, no root bh is an extent block, so this helps
677 * catch code errors with dinode trees. The assertion can be
678 * safely removed if we ever need to insert extent block
679 * structures at the root.
680 */
681 BUG_ON(index == 0);
682
683 path->p_node[index].bh = eb_bh;
684 path->p_node[index].el = &eb->h_list;
685}
686
687static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700688 struct ocfs2_extent_list *root_el,
689 ocfs2_journal_access_func access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800690{
691 struct ocfs2_path *path;
692
693 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
694
695 path = kzalloc(sizeof(*path), GFP_NOFS);
696 if (path) {
697 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
698 get_bh(root_bh);
699 path_root_bh(path) = root_bh;
700 path_root_el(path) = root_el;
Joel Becker13723d02008-10-17 19:25:01 -0700701 path_root_access(path) = access;
Mark Fashehdcd05382007-01-16 11:32:23 -0800702 }
703
704 return path;
705}
706
Tao Mae2e9f602009-08-18 11:22:34 +0800707struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
Joel Beckerffdd7a52008-10-17 22:32:01 -0700708{
Joel Becker13723d02008-10-17 19:25:01 -0700709 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
710 path_root_access(path));
Joel Beckerffdd7a52008-10-17 22:32:01 -0700711}
712
Tao Mae2e9f602009-08-18 11:22:34 +0800713struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
Joel Beckerffdd7a52008-10-17 22:32:01 -0700714{
Joel Becker13723d02008-10-17 19:25:01 -0700715 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
716 et->et_root_journal_access);
717}
718
719/*
720 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
721 * otherwise it's the root_access function.
722 *
723 * I don't like the way this function's name looks next to
724 * ocfs2_journal_access_path(), but I don't have a better one.
725 */
Tao Mae2e9f602009-08-18 11:22:34 +0800726int ocfs2_path_bh_journal_access(handle_t *handle,
727 struct ocfs2_caching_info *ci,
728 struct ocfs2_path *path,
729 int idx)
Joel Becker13723d02008-10-17 19:25:01 -0700730{
731 ocfs2_journal_access_func access = path_root_access(path);
732
733 if (!access)
734 access = ocfs2_journal_access;
735
736 if (idx)
737 access = ocfs2_journal_access_eb;
738
Joel Becker0cf2f762009-02-12 16:41:25 -0800739 return access(handle, ci, path->p_node[idx].bh,
Joel Becker13723d02008-10-17 19:25:01 -0700740 OCFS2_JOURNAL_ACCESS_WRITE);
Joel Beckerffdd7a52008-10-17 22:32:01 -0700741}
742
Mark Fashehdcd05382007-01-16 11:32:23 -0800743/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800744 * Convenience function to journal all components in a path.
745 */
Tao Mae2e9f602009-08-18 11:22:34 +0800746int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
747 handle_t *handle,
748 struct ocfs2_path *path)
Mark Fashehdcd05382007-01-16 11:32:23 -0800749{
750 int i, ret = 0;
751
752 if (!path)
753 goto out;
754
755 for(i = 0; i < path_num_items(path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800756 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -0800757 if (ret < 0) {
758 mlog_errno(ret);
759 goto out;
760 }
761 }
762
763out:
764 return ret;
765}
766
Mark Fasheh328d5752007-06-18 10:48:04 -0700767/*
768 * Return the index of the extent record which contains cluster #v_cluster.
769 * -1 is returned if it was not found.
770 *
771 * Should work fine on interior and exterior nodes.
772 */
773int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
774{
775 int ret = -1;
776 int i;
777 struct ocfs2_extent_rec *rec;
778 u32 rec_end, rec_start, clusters;
779
780 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
781 rec = &el->l_recs[i];
782
783 rec_start = le32_to_cpu(rec->e_cpos);
784 clusters = ocfs2_rec_clusters(el, rec);
785
786 rec_end = rec_start + clusters;
787
788 if (v_cluster >= rec_start && v_cluster < rec_end) {
789 ret = i;
790 break;
791 }
792 }
793
794 return ret;
795}
796
Mark Fashehe48edee2007-03-07 16:46:57 -0800797/*
798 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
Tao Ma853a3a12009-08-18 11:22:18 +0800799 * ocfs2_extent_rec_contig only work properly against leaf nodes!
Mark Fashehe48edee2007-03-07 16:46:57 -0800800 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800801static int ocfs2_block_extent_contig(struct super_block *sb,
802 struct ocfs2_extent_rec *ext,
803 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800804{
Mark Fashehe48edee2007-03-07 16:46:57 -0800805 u64 blk_end = le64_to_cpu(ext->e_blkno);
806
807 blk_end += ocfs2_clusters_to_blocks(sb,
808 le16_to_cpu(ext->e_leaf_clusters));
809
810 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800811}
812
Mark Fashehdcd05382007-01-16 11:32:23 -0800813static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
814 struct ocfs2_extent_rec *right)
815{
Mark Fashehe48edee2007-03-07 16:46:57 -0800816 u32 left_range;
817
818 left_range = le32_to_cpu(left->e_cpos) +
819 le16_to_cpu(left->e_leaf_clusters);
820
821 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800822}
823
824static enum ocfs2_contig_type
Tao Ma853a3a12009-08-18 11:22:18 +0800825 ocfs2_extent_rec_contig(struct super_block *sb,
826 struct ocfs2_extent_rec *ext,
827 struct ocfs2_extent_rec *insert_rec)
Mark Fashehdcd05382007-01-16 11:32:23 -0800828{
829 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
830
Mark Fasheh328d5752007-06-18 10:48:04 -0700831 /*
832 * Refuse to coalesce extent records with different flag
833 * fields - we don't want to mix unwritten extents with user
834 * data.
835 */
836 if (ext->e_flags != insert_rec->e_flags)
837 return CONTIG_NONE;
838
Mark Fashehdcd05382007-01-16 11:32:23 -0800839 if (ocfs2_extents_adjacent(ext, insert_rec) &&
Joel Beckerb4a17652009-02-13 03:07:09 -0800840 ocfs2_block_extent_contig(sb, ext, blkno))
Mark Fashehdcd05382007-01-16 11:32:23 -0800841 return CONTIG_RIGHT;
842
843 blkno = le64_to_cpu(ext->e_blkno);
844 if (ocfs2_extents_adjacent(insert_rec, ext) &&
Joel Beckerb4a17652009-02-13 03:07:09 -0800845 ocfs2_block_extent_contig(sb, insert_rec, blkno))
Mark Fashehdcd05382007-01-16 11:32:23 -0800846 return CONTIG_LEFT;
847
848 return CONTIG_NONE;
849}
850
851/*
852 * NOTE: We can have pretty much any combination of contiguousness and
853 * appending.
854 *
855 * The usefulness of APPEND_TAIL is more in that it lets us know that
856 * we'll have to update the path to that leaf.
857 */
858enum ocfs2_append_type {
859 APPEND_NONE = 0,
860 APPEND_TAIL,
861};
862
Mark Fasheh328d5752007-06-18 10:48:04 -0700863enum ocfs2_split_type {
864 SPLIT_NONE = 0,
865 SPLIT_LEFT,
866 SPLIT_RIGHT,
867};
868
Mark Fashehdcd05382007-01-16 11:32:23 -0800869struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700870 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800871 enum ocfs2_append_type ins_appending;
872 enum ocfs2_contig_type ins_contig;
873 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800874 int ins_tree_depth;
875};
876
Mark Fasheh328d5752007-06-18 10:48:04 -0700877struct ocfs2_merge_ctxt {
878 enum ocfs2_contig_type c_contig_type;
879 int c_has_empty_extent;
880 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700881};
882
Joel Becker5e965812008-11-13 14:49:16 -0800883static int ocfs2_validate_extent_block(struct super_block *sb,
884 struct buffer_head *bh)
885{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700886 int rc;
Joel Becker5e965812008-11-13 14:49:16 -0800887 struct ocfs2_extent_block *eb =
888 (struct ocfs2_extent_block *)bh->b_data;
889
Joel Becker970e4932008-11-13 14:49:19 -0800890 mlog(0, "Validating extent block %llu\n",
891 (unsigned long long)bh->b_blocknr);
892
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700893 BUG_ON(!buffer_uptodate(bh));
894
895 /*
896 * If the ecc fails, we return the error but otherwise
897 * leave the filesystem running. We know any error is
898 * local to this block.
899 */
900 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
Joel Becker13723d02008-10-17 19:25:01 -0700901 if (rc) {
902 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
903 (unsigned long long)bh->b_blocknr);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700904 return rc;
Joel Becker13723d02008-10-17 19:25:01 -0700905 }
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700906
907 /*
908 * Errors after here are fatal.
909 */
910
Joel Becker5e965812008-11-13 14:49:16 -0800911 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
912 ocfs2_error(sb,
913 "Extent block #%llu has bad signature %.*s",
914 (unsigned long long)bh->b_blocknr, 7,
915 eb->h_signature);
916 return -EINVAL;
917 }
918
919 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
920 ocfs2_error(sb,
921 "Extent block #%llu has an invalid h_blkno "
922 "of %llu",
923 (unsigned long long)bh->b_blocknr,
924 (unsigned long long)le64_to_cpu(eb->h_blkno));
925 return -EINVAL;
926 }
927
928 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
929 ocfs2_error(sb,
930 "Extent block #%llu has an invalid "
931 "h_fs_generation of #%u",
932 (unsigned long long)bh->b_blocknr,
933 le32_to_cpu(eb->h_fs_generation));
934 return -EINVAL;
935 }
936
937 return 0;
938}
939
Joel Becker3d03a302009-02-12 17:49:26 -0800940int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
Joel Becker5e965812008-11-13 14:49:16 -0800941 struct buffer_head **bh)
942{
943 int rc;
944 struct buffer_head *tmp = *bh;
945
Joel Becker3d03a302009-02-12 17:49:26 -0800946 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800947 ocfs2_validate_extent_block);
Joel Becker5e965812008-11-13 14:49:16 -0800948
949 /* If ocfs2_read_block() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -0800950 if (!rc && !*bh)
Joel Becker5e965812008-11-13 14:49:16 -0800951 *bh = tmp;
952
Joel Becker5e965812008-11-13 14:49:16 -0800953 return rc;
954}
955
956
Mark Fashehccd979b2005-12-15 14:31:24 -0800957/*
958 * How many free extents have we got before we need more meta data?
959 */
960int ocfs2_num_free_extents(struct ocfs2_super *osb,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700961 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -0800962{
963 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800964 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800965 struct ocfs2_extent_block *eb;
966 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800967 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800968
969 mlog_entry_void();
970
Joel Beckerf99b9b72008-08-20 19:36:33 -0700971 el = et->et_root_el;
972 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800973
Tao Mae7d4cb62008-08-18 17:38:44 +0800974 if (last_eb_blk) {
Joel Becker3d03a302009-02-12 17:49:26 -0800975 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
976 &eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800977 if (retval < 0) {
978 mlog_errno(retval);
979 goto bail;
980 }
981 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
982 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800983 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800984
985 BUG_ON(el->l_tree_depth != 0);
986
987 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
988bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700989 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800990
991 mlog_exit(retval);
992 return retval;
993}
994
995/* expects array to already be allocated
996 *
997 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
998 * l_count for you
999 */
Joel Becker42a5a7a2009-02-12 18:49:19 -08001000static int ocfs2_create_new_meta_bhs(handle_t *handle,
1001 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001002 int wanted,
1003 struct ocfs2_alloc_context *meta_ac,
1004 struct buffer_head *bhs[])
1005{
1006 int count, status, i;
1007 u16 suballoc_bit_start;
1008 u32 num_got;
1009 u64 first_blkno;
Joel Becker42a5a7a2009-02-12 18:49:19 -08001010 struct ocfs2_super *osb =
1011 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001012 struct ocfs2_extent_block *eb;
1013
1014 mlog_entry_void();
1015
1016 count = 0;
1017 while (count < wanted) {
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,
1021 &suballoc_bit_start,
1022 &num_got,
1023 &first_blkno);
1024 if (status < 0) {
1025 mlog_errno(status);
1026 goto bail;
1027 }
1028
1029 for(i = count; i < (num_got + count); i++) {
1030 bhs[i] = sb_getblk(osb->sb, first_blkno);
1031 if (bhs[i] == NULL) {
1032 status = -EIO;
1033 mlog_errno(status);
1034 goto bail;
1035 }
Joel Becker42a5a7a2009-02-12 18:49:19 -08001036 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001037
Joel Becker42a5a7a2009-02-12 18:49:19 -08001038 status = ocfs2_journal_access_eb(handle, et->et_ci,
1039 bhs[i],
Joel Becker13723d02008-10-17 19:25:01 -07001040 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001041 if (status < 0) {
1042 mlog_errno(status);
1043 goto bail;
1044 }
1045
1046 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
1047 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
1048 /* Ok, setup the minimal stuff here. */
1049 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
1050 eb->h_blkno = cpu_to_le64(first_blkno);
1051 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Tiger Yangb89c5422010-01-25 14:11:06 +08001052 eb->h_suballoc_slot =
1053 cpu_to_le16(meta_ac->ac_alloc_slot);
Mark Fashehccd979b2005-12-15 14:31:24 -08001054 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1055 eb->h_list.l_count =
1056 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1057
1058 suballoc_bit_start++;
1059 first_blkno++;
1060
1061 /* We'll also be dirtied by the caller, so
1062 * this isn't absolutely necessary. */
Joel Beckerec20cec2010-03-19 14:13:52 -07001063 ocfs2_journal_dirty(handle, bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001064 }
1065
1066 count += num_got;
1067 }
1068
1069 status = 0;
1070bail:
1071 if (status < 0) {
1072 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001073 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001074 bhs[i] = NULL;
1075 }
1076 }
1077 mlog_exit(status);
1078 return status;
1079}
1080
1081/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001082 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1083 *
1084 * Returns the sum of the rightmost extent rec logical offset and
1085 * cluster count.
1086 *
1087 * ocfs2_add_branch() uses this to determine what logical cluster
1088 * value should be populated into the leftmost new branch records.
1089 *
1090 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1091 * value for the new topmost tree record.
1092 */
1093static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1094{
1095 int i;
1096
1097 i = le16_to_cpu(el->l_next_free_rec) - 1;
1098
1099 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001100 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08001101}
1102
1103/*
Tao Ma6b791bc2009-06-12 14:18:36 +08001104 * Change range of the branches in the right most path according to the leaf
1105 * extent block's rightmost record.
1106 */
1107static int ocfs2_adjust_rightmost_branch(handle_t *handle,
Tao Ma6b791bc2009-06-12 14:18:36 +08001108 struct ocfs2_extent_tree *et)
1109{
1110 int status;
1111 struct ocfs2_path *path = NULL;
1112 struct ocfs2_extent_list *el;
1113 struct ocfs2_extent_rec *rec;
1114
1115 path = ocfs2_new_path_from_et(et);
1116 if (!path) {
1117 status = -ENOMEM;
1118 return status;
1119 }
1120
Joel Beckerfacdb772009-02-12 18:08:48 -08001121 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
Tao Ma6b791bc2009-06-12 14:18:36 +08001122 if (status < 0) {
1123 mlog_errno(status);
1124 goto out;
1125 }
1126
Tao Mac901fb02010-04-26 14:34:57 +08001127 status = ocfs2_extend_trans(handle, path_num_items(path));
Tao Ma6b791bc2009-06-12 14:18:36 +08001128 if (status < 0) {
1129 mlog_errno(status);
1130 goto out;
1131 }
1132
Joel Beckerd401dc12009-02-13 02:24:10 -08001133 status = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma6b791bc2009-06-12 14:18:36 +08001134 if (status < 0) {
1135 mlog_errno(status);
1136 goto out;
1137 }
1138
1139 el = path_leaf_el(path);
1140 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1141
Joel Beckerd401dc12009-02-13 02:24:10 -08001142 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Tao Ma6b791bc2009-06-12 14:18:36 +08001143
1144out:
1145 ocfs2_free_path(path);
1146 return status;
1147}
1148
1149/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001150 * Add an entire tree branch to our inode. eb_bh is the extent block
Joel Beckerd401dc12009-02-13 02:24:10 -08001151 * to start at, if we don't want to start the branch at the root
Mark Fashehccd979b2005-12-15 14:31:24 -08001152 * structure.
1153 *
1154 * last_eb_bh is required as we have to update it's next_leaf pointer
1155 * for the new last extent block.
1156 *
1157 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -08001158 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -08001159 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001160static int ocfs2_add_branch(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001161 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001162 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07001163 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001164 struct ocfs2_alloc_context *meta_ac)
1165{
1166 int status, new_blocks, i;
1167 u64 next_blkno, new_last_eb_blk;
1168 struct buffer_head *bh;
1169 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001170 struct ocfs2_extent_block *eb;
1171 struct ocfs2_extent_list *eb_el;
1172 struct ocfs2_extent_list *el;
Tao Ma6b791bc2009-06-12 14:18:36 +08001173 u32 new_cpos, root_end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001174
1175 mlog_entry_void();
1176
Mark Fasheh328d5752007-06-18 10:48:04 -07001177 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001178
Mark Fashehccd979b2005-12-15 14:31:24 -08001179 if (eb_bh) {
1180 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1181 el = &eb->h_list;
1182 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001183 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001184
1185 /* we never add a branch to a leaf. */
1186 BUG_ON(!el->l_tree_depth);
1187
1188 new_blocks = le16_to_cpu(el->l_tree_depth);
1189
Tao Ma6b791bc2009-06-12 14:18:36 +08001190 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1191 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1192 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1193
1194 /*
1195 * If there is a gap before the root end and the real end
1196 * of the righmost leaf block, we need to remove the gap
1197 * between new_cpos and root_end first so that the tree
1198 * is consistent after we add a new branch(it will start
1199 * from new_cpos).
1200 */
1201 if (root_end > new_cpos) {
1202 mlog(0, "adjust the cluster end from %u to %u\n",
1203 root_end, new_cpos);
Joel Beckerd401dc12009-02-13 02:24:10 -08001204 status = ocfs2_adjust_rightmost_branch(handle, et);
Tao Ma6b791bc2009-06-12 14:18:36 +08001205 if (status) {
1206 mlog_errno(status);
1207 goto bail;
1208 }
1209 }
1210
Mark Fashehccd979b2005-12-15 14:31:24 -08001211 /* allocate the number of new eb blocks we need */
1212 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1213 GFP_KERNEL);
1214 if (!new_eb_bhs) {
1215 status = -ENOMEM;
1216 mlog_errno(status);
1217 goto bail;
1218 }
1219
Joel Becker42a5a7a2009-02-12 18:49:19 -08001220 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
Mark Fashehccd979b2005-12-15 14:31:24 -08001221 meta_ac, new_eb_bhs);
1222 if (status < 0) {
1223 mlog_errno(status);
1224 goto bail;
1225 }
1226
1227 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1228 * linked with the rest of the tree.
1229 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1230 *
1231 * when we leave the loop, new_last_eb_blk will point to the
1232 * newest leaf, and next_blkno will point to the topmost extent
1233 * block. */
1234 next_blkno = new_last_eb_blk = 0;
1235 for(i = 0; i < new_blocks; i++) {
1236 bh = new_eb_bhs[i];
1237 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001238 /* ocfs2_create_new_meta_bhs() should create it right! */
1239 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001240 eb_el = &eb->h_list;
1241
Joel Beckerd401dc12009-02-13 02:24:10 -08001242 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
Joel Becker13723d02008-10-17 19:25:01 -07001243 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001244 if (status < 0) {
1245 mlog_errno(status);
1246 goto bail;
1247 }
1248
1249 eb->h_next_leaf_blk = 0;
1250 eb_el->l_tree_depth = cpu_to_le16(i);
1251 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001252 /*
1253 * This actually counts as an empty extent as
1254 * c_clusters == 0
1255 */
1256 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001257 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001258 /*
1259 * eb_el isn't always an interior node, but even leaf
1260 * nodes want a zero'd flags and reserved field so
1261 * this gets the whole 32 bits regardless of use.
1262 */
1263 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001264 if (!eb_el->l_tree_depth)
1265 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1266
Joel Beckerec20cec2010-03-19 14:13:52 -07001267 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001268 next_blkno = le64_to_cpu(eb->h_blkno);
1269 }
1270
1271 /* This is a bit hairy. We want to update up to three blocks
1272 * here without leaving any of them in an inconsistent state
1273 * in case of error. We don't have to worry about
1274 * journal_dirty erroring as it won't unless we've aborted the
1275 * handle (in which case we would never be here) so reserving
1276 * the write with journal_access is all we need to do. */
Joel Beckerd401dc12009-02-13 02:24:10 -08001277 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001278 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001279 if (status < 0) {
1280 mlog_errno(status);
1281 goto bail;
1282 }
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001283 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001284 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001285 if (status < 0) {
1286 mlog_errno(status);
1287 goto bail;
1288 }
1289 if (eb_bh) {
Joel Beckerd401dc12009-02-13 02:24:10 -08001290 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001291 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001292 if (status < 0) {
1293 mlog_errno(status);
1294 goto bail;
1295 }
1296 }
1297
1298 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001299 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001300 i = le16_to_cpu(el->l_next_free_rec);
1301 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001302 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001303 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001304 le16_add_cpu(&el->l_next_free_rec, 1);
1305
1306 /* fe needs a new last extent block pointer, as does the
1307 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001308 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001309
Mark Fasheh328d5752007-06-18 10:48:04 -07001310 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001311 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1312
Joel Beckerec20cec2010-03-19 14:13:52 -07001313 ocfs2_journal_dirty(handle, *last_eb_bh);
1314 ocfs2_journal_dirty(handle, et->et_root_bh);
1315 if (eb_bh)
1316 ocfs2_journal_dirty(handle, eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001317
Mark Fasheh328d5752007-06-18 10:48:04 -07001318 /*
1319 * Some callers want to track the rightmost leaf so pass it
1320 * back here.
1321 */
1322 brelse(*last_eb_bh);
1323 get_bh(new_eb_bhs[0]);
1324 *last_eb_bh = new_eb_bhs[0];
1325
Mark Fashehccd979b2005-12-15 14:31:24 -08001326 status = 0;
1327bail:
1328 if (new_eb_bhs) {
1329 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001330 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001331 kfree(new_eb_bhs);
1332 }
1333
1334 mlog_exit(status);
1335 return status;
1336}
1337
1338/*
1339 * adds another level to the allocation tree.
1340 * returns back the new extent block so you can add a branch to it
1341 * after this call.
1342 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001343static int ocfs2_shift_tree_depth(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001344 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001345 struct ocfs2_alloc_context *meta_ac,
1346 struct buffer_head **ret_new_eb_bh)
1347{
1348 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001349 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001350 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001351 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001352 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001353 struct ocfs2_extent_list *eb_el;
1354
1355 mlog_entry_void();
1356
Joel Becker42a5a7a2009-02-12 18:49:19 -08001357 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
Mark Fashehccd979b2005-12-15 14:31:24 -08001358 &new_eb_bh);
1359 if (status < 0) {
1360 mlog_errno(status);
1361 goto bail;
1362 }
1363
1364 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001365 /* ocfs2_create_new_meta_bhs() should create it right! */
1366 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001367
1368 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001369 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001370
Joel Beckerd401dc12009-02-13 02:24:10 -08001371 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001372 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001373 if (status < 0) {
1374 mlog_errno(status);
1375 goto bail;
1376 }
1377
Tao Mae7d4cb62008-08-18 17:38:44 +08001378 /* copy the root extent list data into the new extent block */
1379 eb_el->l_tree_depth = root_el->l_tree_depth;
1380 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1381 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1382 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001383
Joel Beckerec20cec2010-03-19 14:13:52 -07001384 ocfs2_journal_dirty(handle, new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001385
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001386 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001387 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001388 if (status < 0) {
1389 mlog_errno(status);
1390 goto bail;
1391 }
1392
Mark Fashehdcd05382007-01-16 11:32:23 -08001393 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1394
Tao Mae7d4cb62008-08-18 17:38:44 +08001395 /* update root_bh now */
1396 le16_add_cpu(&root_el->l_tree_depth, 1);
1397 root_el->l_recs[0].e_cpos = 0;
1398 root_el->l_recs[0].e_blkno = eb->h_blkno;
1399 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1400 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1401 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1402 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001403
1404 /* If this is our 1st tree depth shift, then last_eb_blk
1405 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001406 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001407 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001408
Joel Beckerec20cec2010-03-19 14:13:52 -07001409 ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001410
1411 *ret_new_eb_bh = new_eb_bh;
1412 new_eb_bh = NULL;
1413 status = 0;
1414bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001415 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001416
1417 mlog_exit(status);
1418 return status;
1419}
1420
1421/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001422 * Should only be called when there is no space left in any of the
1423 * leaf nodes. What we want to do is find the lowest tree depth
1424 * non-leaf extent block with room for new records. There are three
1425 * valid results of this search:
1426 *
1427 * 1) a lowest extent block is found, then we pass it back in
1428 * *lowest_eb_bh and return '0'
1429 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001430 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001431 * pass NULL back in *lowest_eb_bh, but still return '0'
1432 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001433 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001434 * which case we return > 0
1435 *
1436 * return status < 0 indicates an error.
1437 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001438static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001439 struct buffer_head **target_bh)
1440{
1441 int status = 0, i;
1442 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001443 struct ocfs2_extent_block *eb;
1444 struct ocfs2_extent_list *el;
1445 struct buffer_head *bh = NULL;
1446 struct buffer_head *lowest_bh = NULL;
1447
1448 mlog_entry_void();
1449
1450 *target_bh = NULL;
1451
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001452 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001453
1454 while(le16_to_cpu(el->l_tree_depth) > 1) {
1455 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08001456 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1457 "Owner %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001458 "extent list (next_free_rec == 0)",
Joel Becker3d03a302009-02-12 17:49:26 -08001459 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001460 status = -EIO;
1461 goto bail;
1462 }
1463 i = le16_to_cpu(el->l_next_free_rec) - 1;
1464 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1465 if (!blkno) {
Joel Becker3d03a302009-02-12 17:49:26 -08001466 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1467 "Owner %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001468 "list where extent # %d has no physical "
1469 "block start",
Joel Becker3d03a302009-02-12 17:49:26 -08001470 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001471 status = -EIO;
1472 goto bail;
1473 }
1474
Mark Fasheha81cb882008-10-07 14:25:16 -07001475 brelse(bh);
1476 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001477
Joel Becker3d03a302009-02-12 17:49:26 -08001478 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001479 if (status < 0) {
1480 mlog_errno(status);
1481 goto bail;
1482 }
1483
1484 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001485 el = &eb->h_list;
1486
1487 if (le16_to_cpu(el->l_next_free_rec) <
1488 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001489 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001490 lowest_bh = bh;
1491 get_bh(lowest_bh);
1492 }
1493 }
1494
1495 /* If we didn't find one and the fe doesn't have any room,
1496 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001497 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001498 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001499 status = 1;
1500
1501 *target_bh = lowest_bh;
1502bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001503 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001504
1505 mlog_exit(status);
1506 return status;
1507}
1508
Mark Fashehe48edee2007-03-07 16:46:57 -08001509/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001510 * Grow a b-tree so that it has more records.
1511 *
1512 * We might shift the tree depth in which case existing paths should
1513 * be considered invalid.
1514 *
1515 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001516 *
1517 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001518 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001519static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1520 int *final_depth, struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001521 struct ocfs2_alloc_context *meta_ac)
1522{
1523 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001524 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001525 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001526 struct buffer_head *bh = NULL;
1527
1528 BUG_ON(meta_ac == NULL);
1529
Joel Beckerd401dc12009-02-13 02:24:10 -08001530 shift = ocfs2_find_branch_target(et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001531 if (shift < 0) {
1532 ret = shift;
1533 mlog_errno(ret);
1534 goto out;
1535 }
1536
1537 /* We traveled all the way to the bottom of the allocation tree
1538 * and didn't find room for any more extents - we need to add
1539 * another tree level */
1540 if (shift) {
1541 BUG_ON(bh);
1542 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1543
1544 /* ocfs2_shift_tree_depth will return us a buffer with
1545 * the new extent block (so we can pass that to
1546 * ocfs2_add_branch). */
Joel Beckerd401dc12009-02-13 02:24:10 -08001547 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001548 if (ret < 0) {
1549 mlog_errno(ret);
1550 goto out;
1551 }
1552 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001553 if (depth == 1) {
1554 /*
1555 * Special case: we have room now if we shifted from
1556 * tree_depth 0, so no more work needs to be done.
1557 *
1558 * We won't be calling add_branch, so pass
1559 * back *last_eb_bh as the new leaf. At depth
1560 * zero, it should always be null so there's
1561 * no reason to brelse.
1562 */
1563 BUG_ON(*last_eb_bh);
1564 get_bh(bh);
1565 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001566 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001567 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001568 }
1569
1570 /* call ocfs2_add_branch to add the final part of the tree with
1571 * the new data. */
1572 mlog(0, "add branch. bh = %p\n", bh);
Joel Beckerd401dc12009-02-13 02:24:10 -08001573 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001574 meta_ac);
1575 if (ret < 0) {
1576 mlog_errno(ret);
1577 goto out;
1578 }
1579
1580out:
1581 if (final_depth)
1582 *final_depth = depth;
1583 brelse(bh);
1584 return ret;
1585}
1586
1587/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001588 * This function will discard the rightmost extent record.
1589 */
1590static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1591{
1592 int next_free = le16_to_cpu(el->l_next_free_rec);
1593 int count = le16_to_cpu(el->l_count);
1594 unsigned int num_bytes;
1595
1596 BUG_ON(!next_free);
1597 /* This will cause us to go off the end of our extent list. */
1598 BUG_ON(next_free >= count);
1599
1600 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1601
1602 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1603}
1604
1605static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1606 struct ocfs2_extent_rec *insert_rec)
1607{
1608 int i, insert_index, next_free, has_empty, num_bytes;
1609 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1610 struct ocfs2_extent_rec *rec;
1611
1612 next_free = le16_to_cpu(el->l_next_free_rec);
1613 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1614
1615 BUG_ON(!next_free);
1616
1617 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001618 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001619
1620 /*
1621 * The easiest way to approach this is to just remove the
1622 * empty extent and temporarily decrement next_free.
1623 */
1624 if (has_empty) {
1625 /*
1626 * If next_free was 1 (only an empty extent), this
1627 * loop won't execute, which is fine. We still want
1628 * the decrement above to happen.
1629 */
1630 for(i = 0; i < (next_free - 1); i++)
1631 el->l_recs[i] = el->l_recs[i+1];
1632
1633 next_free--;
1634 }
1635
1636 /*
1637 * Figure out what the new record index should be.
1638 */
1639 for(i = 0; i < next_free; i++) {
1640 rec = &el->l_recs[i];
1641
1642 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1643 break;
1644 }
1645 insert_index = i;
1646
1647 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1648 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1649
1650 BUG_ON(insert_index < 0);
1651 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1652 BUG_ON(insert_index > next_free);
1653
1654 /*
1655 * No need to memmove if we're just adding to the tail.
1656 */
1657 if (insert_index != next_free) {
1658 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1659
1660 num_bytes = next_free - insert_index;
1661 num_bytes *= sizeof(struct ocfs2_extent_rec);
1662 memmove(&el->l_recs[insert_index + 1],
1663 &el->l_recs[insert_index],
1664 num_bytes);
1665 }
1666
1667 /*
1668 * Either we had an empty extent, and need to re-increment or
1669 * there was no empty extent on a non full rightmost leaf node,
1670 * in which case we still need to increment.
1671 */
1672 next_free++;
1673 el->l_next_free_rec = cpu_to_le16(next_free);
1674 /*
1675 * Make sure none of the math above just messed up our tree.
1676 */
1677 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1678
1679 el->l_recs[insert_index] = *insert_rec;
1680
1681}
1682
Mark Fasheh328d5752007-06-18 10:48:04 -07001683static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1684{
1685 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1686
1687 BUG_ON(num_recs == 0);
1688
1689 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1690 num_recs--;
1691 size = num_recs * sizeof(struct ocfs2_extent_rec);
1692 memmove(&el->l_recs[0], &el->l_recs[1], size);
1693 memset(&el->l_recs[num_recs], 0,
1694 sizeof(struct ocfs2_extent_rec));
1695 el->l_next_free_rec = cpu_to_le16(num_recs);
1696 }
1697}
1698
Mark Fashehdcd05382007-01-16 11:32:23 -08001699/*
1700 * Create an empty extent record .
1701 *
1702 * l_next_free_rec may be updated.
1703 *
1704 * If an empty extent already exists do nothing.
1705 */
1706static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1707{
1708 int next_free = le16_to_cpu(el->l_next_free_rec);
1709
Mark Fashehe48edee2007-03-07 16:46:57 -08001710 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1711
Mark Fashehdcd05382007-01-16 11:32:23 -08001712 if (next_free == 0)
1713 goto set_and_inc;
1714
1715 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1716 return;
1717
1718 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1719 "Asked to create an empty extent in a full list:\n"
1720 "count = %u, tree depth = %u",
1721 le16_to_cpu(el->l_count),
1722 le16_to_cpu(el->l_tree_depth));
1723
1724 ocfs2_shift_records_right(el);
1725
1726set_and_inc:
1727 le16_add_cpu(&el->l_next_free_rec, 1);
1728 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1729}
1730
1731/*
1732 * For a rotation which involves two leaf nodes, the "root node" is
1733 * the lowest level tree node which contains a path to both leafs. This
1734 * resulting set of information can be used to form a complete "subtree"
1735 *
1736 * This function is passed two full paths from the dinode down to a
1737 * pair of adjacent leaves. It's task is to figure out which path
1738 * index contains the subtree root - this can be the root index itself
1739 * in a worst-case rotation.
1740 *
1741 * The array index of the subtree root is passed back.
1742 */
Tao Ma38a04e42009-11-30 14:32:19 +08001743int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1744 struct ocfs2_path *left,
1745 struct ocfs2_path *right)
Mark Fashehdcd05382007-01-16 11:32:23 -08001746{
1747 int i = 0;
1748
1749 /*
1750 * Check that the caller passed in two paths from the same tree.
1751 */
1752 BUG_ON(path_root_bh(left) != path_root_bh(right));
1753
1754 do {
1755 i++;
1756
1757 /*
1758 * The caller didn't pass two adjacent paths.
1759 */
1760 mlog_bug_on_msg(i > left->p_tree_depth,
Joel Becker7dc02802009-02-12 19:20:13 -08001761 "Owner %llu, left depth %u, right depth %u\n"
Mark Fashehdcd05382007-01-16 11:32:23 -08001762 "left leaf blk %llu, right leaf blk %llu\n",
Joel Becker7dc02802009-02-12 19:20:13 -08001763 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1764 left->p_tree_depth, right->p_tree_depth,
Mark Fashehdcd05382007-01-16 11:32:23 -08001765 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1766 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1767 } while (left->p_node[i].bh->b_blocknr ==
1768 right->p_node[i].bh->b_blocknr);
1769
1770 return i - 1;
1771}
1772
1773typedef void (path_insert_t)(void *, struct buffer_head *);
1774
1775/*
1776 * Traverse a btree path in search of cpos, starting at root_el.
1777 *
1778 * This code can be called with a cpos larger than the tree, in which
1779 * case it will return the rightmost path.
1780 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001781static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
Mark Fashehdcd05382007-01-16 11:32:23 -08001782 struct ocfs2_extent_list *root_el, u32 cpos,
1783 path_insert_t *func, void *data)
1784{
1785 int i, ret = 0;
1786 u32 range;
1787 u64 blkno;
1788 struct buffer_head *bh = NULL;
1789 struct ocfs2_extent_block *eb;
1790 struct ocfs2_extent_list *el;
1791 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08001792
1793 el = root_el;
1794 while (el->l_tree_depth) {
1795 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001796 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1797 "Owner %llu has empty extent list at "
Mark Fashehdcd05382007-01-16 11:32:23 -08001798 "depth %u\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001799 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001800 le16_to_cpu(el->l_tree_depth));
1801 ret = -EROFS;
1802 goto out;
1803
1804 }
1805
1806 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1807 rec = &el->l_recs[i];
1808
1809 /*
1810 * In the case that cpos is off the allocation
1811 * tree, this should just wind up returning the
1812 * rightmost record.
1813 */
1814 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001815 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001816 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1817 break;
1818 }
1819
1820 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1821 if (blkno == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001822 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1823 "Owner %llu has bad blkno in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001824 "at depth %u (index %d)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001825 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001826 le16_to_cpu(el->l_tree_depth), i);
1827 ret = -EROFS;
1828 goto out;
1829 }
1830
1831 brelse(bh);
1832 bh = NULL;
Joel Beckerfacdb772009-02-12 18:08:48 -08001833 ret = ocfs2_read_extent_block(ci, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001834 if (ret) {
1835 mlog_errno(ret);
1836 goto out;
1837 }
1838
1839 eb = (struct ocfs2_extent_block *) bh->b_data;
1840 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001841
1842 if (le16_to_cpu(el->l_next_free_rec) >
1843 le16_to_cpu(el->l_count)) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001844 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1845 "Owner %llu has bad count in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001846 "at block %llu (next free=%u, count=%u)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001847 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001848 (unsigned long long)bh->b_blocknr,
1849 le16_to_cpu(el->l_next_free_rec),
1850 le16_to_cpu(el->l_count));
1851 ret = -EROFS;
1852 goto out;
1853 }
1854
1855 if (func)
1856 func(data, bh);
1857 }
1858
1859out:
1860 /*
1861 * Catch any trailing bh that the loop didn't handle.
1862 */
1863 brelse(bh);
1864
1865 return ret;
1866}
1867
1868/*
1869 * Given an initialized path (that is, it has a valid root extent
1870 * list), this function will traverse the btree in search of the path
1871 * which would contain cpos.
1872 *
1873 * The path traveled is recorded in the path structure.
1874 *
1875 * Note that this will not do any comparisons on leaf node extent
1876 * records, so it will work fine in the case that we just added a tree
1877 * branch.
1878 */
1879struct find_path_data {
1880 int index;
1881 struct ocfs2_path *path;
1882};
1883static void find_path_ins(void *data, struct buffer_head *bh)
1884{
1885 struct find_path_data *fp = data;
1886
1887 get_bh(bh);
1888 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1889 fp->index++;
1890}
Tao Mae2e9f602009-08-18 11:22:34 +08001891int ocfs2_find_path(struct ocfs2_caching_info *ci,
1892 struct ocfs2_path *path, u32 cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08001893{
1894 struct find_path_data data;
1895
1896 data.index = 1;
1897 data.path = path;
Joel Beckerfacdb772009-02-12 18:08:48 -08001898 return __ocfs2_find_path(ci, path_root_el(path), cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08001899 find_path_ins, &data);
1900}
1901
1902static void find_leaf_ins(void *data, struct buffer_head *bh)
1903{
1904 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1905 struct ocfs2_extent_list *el = &eb->h_list;
1906 struct buffer_head **ret = data;
1907
1908 /* We want to retain only the leaf block. */
1909 if (le16_to_cpu(el->l_tree_depth) == 0) {
1910 get_bh(bh);
1911 *ret = bh;
1912 }
1913}
1914/*
1915 * Find the leaf block in the tree which would contain cpos. No
1916 * checking of the actual leaf is done.
1917 *
1918 * Some paths want to call this instead of allocating a path structure
1919 * and calling ocfs2_find_path().
1920 *
1921 * This function doesn't handle non btree extent lists.
1922 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001923int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1924 struct ocfs2_extent_list *root_el, u32 cpos,
1925 struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001926{
1927 int ret;
1928 struct buffer_head *bh = NULL;
1929
Joel Beckerfacdb772009-02-12 18:08:48 -08001930 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001931 if (ret) {
1932 mlog_errno(ret);
1933 goto out;
1934 }
1935
1936 *leaf_bh = bh;
1937out:
1938 return ret;
1939}
1940
1941/*
1942 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1943 *
1944 * Basically, we've moved stuff around at the bottom of the tree and
1945 * we need to fix up the extent records above the changes to reflect
1946 * the new changes.
1947 *
1948 * left_rec: the record on the left.
1949 * left_child_el: is the child list pointed to by left_rec
1950 * right_rec: the record to the right of left_rec
1951 * right_child_el: is the child list pointed to by right_rec
1952 *
1953 * By definition, this only works on interior nodes.
1954 */
1955static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1956 struct ocfs2_extent_list *left_child_el,
1957 struct ocfs2_extent_rec *right_rec,
1958 struct ocfs2_extent_list *right_child_el)
1959{
1960 u32 left_clusters, right_end;
1961
1962 /*
1963 * Interior nodes never have holes. Their cpos is the cpos of
1964 * the leftmost record in their child list. Their cluster
1965 * count covers the full theoretical range of their child list
1966 * - the range between their cpos and the cpos of the record
1967 * immediately to their right.
1968 */
1969 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08001970 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1971 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07001972 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1973 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1974 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001975 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001976 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001977
1978 /*
1979 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001980 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001981 * updating e_cpos to keep the same highest cluster count.
1982 */
1983 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001984 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001985
1986 right_rec->e_cpos = left_rec->e_cpos;
1987 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1988
1989 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001990 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001991}
1992
1993/*
1994 * Adjust the adjacent root node records involved in a
1995 * rotation. left_el_blkno is passed in as a key so that we can easily
1996 * find it's index in the root list.
1997 */
1998static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1999 struct ocfs2_extent_list *left_el,
2000 struct ocfs2_extent_list *right_el,
2001 u64 left_el_blkno)
2002{
2003 int i;
2004
2005 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
2006 le16_to_cpu(left_el->l_tree_depth));
2007
2008 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
2009 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
2010 break;
2011 }
2012
2013 /*
2014 * The path walking code should have never returned a root and
2015 * two paths which are not adjacent.
2016 */
2017 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2018
2019 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
2020 &root_el->l_recs[i + 1], right_el);
2021}
2022
2023/*
2024 * We've changed a leaf block (in right_path) and need to reflect that
2025 * change back up the subtree.
2026 *
2027 * This happens in multiple places:
2028 * - When we've moved an extent record from the left path leaf to the right
2029 * path leaf to make room for an empty extent in the left path leaf.
2030 * - When our insert into the right path leaf is at the leftmost edge
2031 * and requires an update of the path immediately to it's left. This
2032 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08002033 * - When we've adjusted the last extent record in the left path leaf and the
2034 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08002035 */
Joel Becker4619c732009-02-12 19:02:36 -08002036static void ocfs2_complete_edge_insert(handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08002037 struct ocfs2_path *left_path,
2038 struct ocfs2_path *right_path,
2039 int subtree_index)
2040{
Joel Beckerec20cec2010-03-19 14:13:52 -07002041 int i, idx;
Mark Fashehdcd05382007-01-16 11:32:23 -08002042 struct ocfs2_extent_list *el, *left_el, *right_el;
2043 struct ocfs2_extent_rec *left_rec, *right_rec;
2044 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2045
2046 /*
2047 * Update the counts and position values within all the
2048 * interior nodes to reflect the leaf rotation we just did.
2049 *
2050 * The root node is handled below the loop.
2051 *
2052 * We begin the loop with right_el and left_el pointing to the
2053 * leaf lists and work our way up.
2054 *
2055 * NOTE: within this loop, left_el and right_el always refer
2056 * to the *child* lists.
2057 */
2058 left_el = path_leaf_el(left_path);
2059 right_el = path_leaf_el(right_path);
2060 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2061 mlog(0, "Adjust records at index %u\n", i);
2062
2063 /*
2064 * One nice property of knowing that all of these
2065 * nodes are below the root is that we only deal with
2066 * the leftmost right node record and the rightmost
2067 * left node record.
2068 */
2069 el = left_path->p_node[i].el;
2070 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2071 left_rec = &el->l_recs[idx];
2072
2073 el = right_path->p_node[i].el;
2074 right_rec = &el->l_recs[0];
2075
2076 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2077 right_el);
2078
Joel Beckerec20cec2010-03-19 14:13:52 -07002079 ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2080 ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002081
2082 /*
2083 * Setup our list pointers now so that the current
2084 * parents become children in the next iteration.
2085 */
2086 left_el = left_path->p_node[i].el;
2087 right_el = right_path->p_node[i].el;
2088 }
2089
2090 /*
2091 * At the root node, adjust the two adjacent records which
2092 * begin our path to the leaves.
2093 */
2094
2095 el = left_path->p_node[subtree_index].el;
2096 left_el = left_path->p_node[subtree_index + 1].el;
2097 right_el = right_path->p_node[subtree_index + 1].el;
2098
2099 ocfs2_adjust_root_records(el, left_el, right_el,
2100 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2101
2102 root_bh = left_path->p_node[subtree_index].bh;
2103
Joel Beckerec20cec2010-03-19 14:13:52 -07002104 ocfs2_journal_dirty(handle, root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002105}
2106
Joel Becker5c601ab2009-02-12 19:10:13 -08002107static int ocfs2_rotate_subtree_right(handle_t *handle,
2108 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08002109 struct ocfs2_path *left_path,
2110 struct ocfs2_path *right_path,
2111 int subtree_index)
2112{
2113 int ret, i;
2114 struct buffer_head *right_leaf_bh;
2115 struct buffer_head *left_leaf_bh = NULL;
2116 struct buffer_head *root_bh;
2117 struct ocfs2_extent_list *right_el, *left_el;
2118 struct ocfs2_extent_rec move_rec;
2119
2120 left_leaf_bh = path_leaf_bh(left_path);
2121 left_el = path_leaf_el(left_path);
2122
2123 if (left_el->l_next_free_rec != left_el->l_count) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002124 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002125 "Inode %llu has non-full interior leaf node %llu"
2126 "(next free = %u)",
Joel Becker5c601ab2009-02-12 19:10:13 -08002127 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002128 (unsigned long long)left_leaf_bh->b_blocknr,
2129 le16_to_cpu(left_el->l_next_free_rec));
2130 return -EROFS;
2131 }
2132
2133 /*
2134 * This extent block may already have an empty record, so we
2135 * return early if so.
2136 */
2137 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2138 return 0;
2139
2140 root_bh = left_path->p_node[subtree_index].bh;
2141 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2142
Joel Becker5c601ab2009-02-12 19:10:13 -08002143 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002144 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002145 if (ret) {
2146 mlog_errno(ret);
2147 goto out;
2148 }
2149
2150 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002151 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002152 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002153 if (ret) {
2154 mlog_errno(ret);
2155 goto out;
2156 }
2157
Joel Becker5c601ab2009-02-12 19:10:13 -08002158 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002159 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002160 if (ret) {
2161 mlog_errno(ret);
2162 goto out;
2163 }
2164 }
2165
2166 right_leaf_bh = path_leaf_bh(right_path);
2167 right_el = path_leaf_el(right_path);
2168
2169 /* This is a code error, not a disk corruption. */
2170 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2171 "because rightmost leaf block %llu is empty\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002172 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002173 (unsigned long long)right_leaf_bh->b_blocknr);
2174
2175 ocfs2_create_empty_extent(right_el);
2176
Joel Beckerec20cec2010-03-19 14:13:52 -07002177 ocfs2_journal_dirty(handle, right_leaf_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002178
2179 /* Do the copy now. */
2180 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2181 move_rec = left_el->l_recs[i];
2182 right_el->l_recs[0] = move_rec;
2183
2184 /*
2185 * Clear out the record we just copied and shift everything
2186 * over, leaving an empty extent in the left leaf.
2187 *
2188 * We temporarily subtract from next_free_rec so that the
2189 * shift will lose the tail record (which is now defunct).
2190 */
2191 le16_add_cpu(&left_el->l_next_free_rec, -1);
2192 ocfs2_shift_records_right(left_el);
2193 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2194 le16_add_cpu(&left_el->l_next_free_rec, 1);
2195
Joel Beckerec20cec2010-03-19 14:13:52 -07002196 ocfs2_journal_dirty(handle, left_leaf_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002197
Joel Becker4619c732009-02-12 19:02:36 -08002198 ocfs2_complete_edge_insert(handle, left_path, right_path,
2199 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002200
2201out:
2202 return ret;
2203}
2204
2205/*
2206 * Given a full path, determine what cpos value would return us a path
2207 * containing the leaf immediately to the left of the current one.
2208 *
2209 * Will return zero if the path passed in is already the leftmost path.
2210 */
2211static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2212 struct ocfs2_path *path, u32 *cpos)
2213{
2214 int i, j, ret = 0;
2215 u64 blkno;
2216 struct ocfs2_extent_list *el;
2217
Mark Fashehe48edee2007-03-07 16:46:57 -08002218 BUG_ON(path->p_tree_depth == 0);
2219
Mark Fashehdcd05382007-01-16 11:32:23 -08002220 *cpos = 0;
2221
2222 blkno = path_leaf_bh(path)->b_blocknr;
2223
2224 /* Start at the tree node just above the leaf and work our way up. */
2225 i = path->p_tree_depth - 1;
2226 while (i >= 0) {
2227 el = path->p_node[i].el;
2228
2229 /*
2230 * Find the extent record just before the one in our
2231 * path.
2232 */
2233 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2234 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2235 if (j == 0) {
2236 if (i == 0) {
2237 /*
2238 * We've determined that the
2239 * path specified is already
2240 * the leftmost one - return a
2241 * cpos of zero.
2242 */
2243 goto out;
2244 }
2245 /*
2246 * The leftmost record points to our
2247 * leaf - we need to travel up the
2248 * tree one level.
2249 */
2250 goto next_node;
2251 }
2252
2253 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002254 *cpos = *cpos + ocfs2_rec_clusters(el,
2255 &el->l_recs[j - 1]);
2256 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002257 goto out;
2258 }
2259 }
2260
2261 /*
2262 * If we got here, we never found a valid node where
2263 * the tree indicated one should be.
2264 */
2265 ocfs2_error(sb,
2266 "Invalid extent tree at extent block %llu\n",
2267 (unsigned long long)blkno);
2268 ret = -EROFS;
2269 goto out;
2270
2271next_node:
2272 blkno = path->p_node[i].bh->b_blocknr;
2273 i--;
2274 }
2275
2276out:
2277 return ret;
2278}
2279
Mark Fasheh328d5752007-06-18 10:48:04 -07002280/*
2281 * Extend the transaction by enough credits to complete the rotation,
2282 * and still leave at least the original number of credits allocated
2283 * to this transaction.
2284 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002285static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002286 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002287 struct ocfs2_path *path)
2288{
Tao Mac901fb02010-04-26 14:34:57 +08002289 int ret = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07002290 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002291
Tao Mac901fb02010-04-26 14:34:57 +08002292 if (handle->h_buffer_credits < credits)
Tao Mac18b8122009-08-18 11:44:07 +08002293 ret = ocfs2_extend_trans(handle,
2294 credits - handle->h_buffer_credits);
Tao Mac18b8122009-08-18 11:44:07 +08002295
Tao Mac901fb02010-04-26 14:34:57 +08002296 return ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08002297}
2298
2299/*
2300 * Trap the case where we're inserting into the theoretical range past
2301 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2302 * whose cpos is less than ours into the right leaf.
2303 *
2304 * It's only necessary to look at the rightmost record of the left
2305 * leaf because the logic that calls us should ensure that the
2306 * theoretical ranges in the path components above the leaves are
2307 * correct.
2308 */
2309static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2310 u32 insert_cpos)
2311{
2312 struct ocfs2_extent_list *left_el;
2313 struct ocfs2_extent_rec *rec;
2314 int next_free;
2315
2316 left_el = path_leaf_el(left_path);
2317 next_free = le16_to_cpu(left_el->l_next_free_rec);
2318 rec = &left_el->l_recs[next_free - 1];
2319
2320 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2321 return 1;
2322 return 0;
2323}
2324
Mark Fasheh328d5752007-06-18 10:48:04 -07002325static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2326{
2327 int next_free = le16_to_cpu(el->l_next_free_rec);
2328 unsigned int range;
2329 struct ocfs2_extent_rec *rec;
2330
2331 if (next_free == 0)
2332 return 0;
2333
2334 rec = &el->l_recs[0];
2335 if (ocfs2_is_empty_extent(rec)) {
2336 /* Empty list. */
2337 if (next_free == 1)
2338 return 0;
2339 rec = &el->l_recs[1];
2340 }
2341
2342 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2343 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2344 return 1;
2345 return 0;
2346}
2347
Mark Fashehdcd05382007-01-16 11:32:23 -08002348/*
2349 * Rotate all the records in a btree right one record, starting at insert_cpos.
2350 *
2351 * The path to the rightmost leaf should be passed in.
2352 *
2353 * The array is assumed to be large enough to hold an entire path (tree depth).
2354 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002355 * Upon successful return from this function:
Mark Fashehdcd05382007-01-16 11:32:23 -08002356 *
2357 * - The 'right_path' array will contain a path to the leaf block
2358 * whose range contains e_cpos.
2359 * - That leaf block will have a single empty extent in list index 0.
2360 * - In the case that the rotation requires a post-insert update,
2361 * *ret_left_path will contain a valid path which can be passed to
2362 * ocfs2_insert_path().
2363 */
Joel Becker1bbf0b82009-02-12 19:42:08 -08002364static int ocfs2_rotate_tree_right(handle_t *handle,
Joel Becker5c601ab2009-02-12 19:10:13 -08002365 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002366 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002367 u32 insert_cpos,
2368 struct ocfs2_path *right_path,
2369 struct ocfs2_path **ret_left_path)
2370{
Mark Fasheh328d5752007-06-18 10:48:04 -07002371 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002372 u32 cpos;
2373 struct ocfs2_path *left_path = NULL;
Joel Becker5c601ab2009-02-12 19:10:13 -08002374 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehdcd05382007-01-16 11:32:23 -08002375
2376 *ret_left_path = NULL;
2377
Joel Beckerffdd7a52008-10-17 22:32:01 -07002378 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002379 if (!left_path) {
2380 ret = -ENOMEM;
2381 mlog_errno(ret);
2382 goto out;
2383 }
2384
Joel Becker5c601ab2009-02-12 19:10:13 -08002385 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002386 if (ret) {
2387 mlog_errno(ret);
2388 goto out;
2389 }
2390
2391 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2392
2393 /*
2394 * What we want to do here is:
2395 *
2396 * 1) Start with the rightmost path.
2397 *
2398 * 2) Determine a path to the leaf block directly to the left
2399 * of that leaf.
2400 *
2401 * 3) Determine the 'subtree root' - the lowest level tree node
2402 * which contains a path to both leaves.
2403 *
2404 * 4) Rotate the subtree.
2405 *
2406 * 5) Find the next subtree by considering the left path to be
2407 * the new right path.
2408 *
2409 * The check at the top of this while loop also accepts
2410 * insert_cpos == cpos because cpos is only a _theoretical_
2411 * value to get us the left path - insert_cpos might very well
2412 * be filling that hole.
2413 *
2414 * Stop at a cpos of '0' because we either started at the
2415 * leftmost branch (i.e., a tree with one branch and a
2416 * rotation inside of it), or we've gone as far as we can in
2417 * rotating subtrees.
2418 */
2419 while (cpos && insert_cpos <= cpos) {
2420 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2421 insert_cpos, cpos);
2422
Joel Becker5c601ab2009-02-12 19:10:13 -08002423 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002424 if (ret) {
2425 mlog_errno(ret);
2426 goto out;
2427 }
2428
2429 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2430 path_leaf_bh(right_path),
Joel Becker5c601ab2009-02-12 19:10:13 -08002431 "Owner %llu: error during insert of %u "
Mark Fashehdcd05382007-01-16 11:32:23 -08002432 "(left path cpos %u) results in two identical "
2433 "paths ending at %llu\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002434 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2435 insert_cpos, cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08002436 (unsigned long long)
2437 path_leaf_bh(left_path)->b_blocknr);
2438
Mark Fasheh328d5752007-06-18 10:48:04 -07002439 if (split == SPLIT_NONE &&
2440 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002441 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002442
2443 /*
2444 * We've rotated the tree as much as we
2445 * should. The rest is up to
2446 * ocfs2_insert_path() to complete, after the
2447 * record insertion. We indicate this
2448 * situation by returning the left path.
2449 *
2450 * The reason we don't adjust the records here
2451 * before the record insert is that an error
2452 * later might break the rule where a parent
2453 * record e_cpos will reflect the actual
2454 * e_cpos of the 1st nonempty record of the
2455 * child list.
2456 */
2457 *ret_left_path = left_path;
2458 goto out_ret_path;
2459 }
2460
Joel Becker7dc02802009-02-12 19:20:13 -08002461 start = ocfs2_find_subtree_root(et, left_path, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002462
2463 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2464 start,
2465 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2466 right_path->p_tree_depth);
2467
2468 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002469 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002470 if (ret) {
2471 mlog_errno(ret);
2472 goto out;
2473 }
2474
Joel Becker5c601ab2009-02-12 19:10:13 -08002475 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002476 right_path, start);
2477 if (ret) {
2478 mlog_errno(ret);
2479 goto out;
2480 }
2481
Mark Fasheh328d5752007-06-18 10:48:04 -07002482 if (split != SPLIT_NONE &&
2483 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2484 insert_cpos)) {
2485 /*
2486 * A rotate moves the rightmost left leaf
2487 * record over to the leftmost right leaf
2488 * slot. If we're doing an extent split
2489 * instead of a real insert, then we have to
2490 * check that the extent to be split wasn't
2491 * just moved over. If it was, then we can
2492 * exit here, passing left_path back -
2493 * ocfs2_split_extent() is smart enough to
2494 * search both leaves.
2495 */
2496 *ret_left_path = left_path;
2497 goto out_ret_path;
2498 }
2499
Mark Fashehdcd05382007-01-16 11:32:23 -08002500 /*
2501 * There is no need to re-read the next right path
2502 * as we know that it'll be our current left
2503 * path. Optimize by copying values instead.
2504 */
2505 ocfs2_mv_path(right_path, left_path);
2506
Joel Becker5c601ab2009-02-12 19:10:13 -08002507 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002508 if (ret) {
2509 mlog_errno(ret);
2510 goto out;
2511 }
2512 }
2513
2514out:
2515 ocfs2_free_path(left_path);
2516
2517out_ret_path:
2518 return ret;
2519}
2520
Joel Becker09106ba2009-02-12 19:43:57 -08002521static int ocfs2_update_edge_lengths(handle_t *handle,
2522 struct ocfs2_extent_tree *et,
Tao Ma3c5e1062009-07-21 15:42:05 +08002523 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002524{
Tao Ma3c5e1062009-07-21 15:42:05 +08002525 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002526 struct ocfs2_extent_rec *rec;
2527 struct ocfs2_extent_list *el;
2528 struct ocfs2_extent_block *eb;
2529 u32 range;
2530
Tao Ma3c5e1062009-07-21 15:42:05 +08002531 /*
2532 * In normal tree rotation process, we will never touch the
2533 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2534 * doesn't reserve the credits for them either.
2535 *
2536 * But we do have a special case here which will update the rightmost
2537 * records for all the bh in the path.
2538 * So we have to allocate extra credits and access them.
2539 */
Tao Mac901fb02010-04-26 14:34:57 +08002540 ret = ocfs2_extend_trans(handle, subtree_index);
Tao Ma3c5e1062009-07-21 15:42:05 +08002541 if (ret) {
2542 mlog_errno(ret);
2543 goto out;
2544 }
2545
Joel Becker09106ba2009-02-12 19:43:57 -08002546 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002547 if (ret) {
2548 mlog_errno(ret);
2549 goto out;
2550 }
2551
Mark Fasheh328d5752007-06-18 10:48:04 -07002552 /* Path should always be rightmost. */
2553 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2554 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2555
2556 el = &eb->h_list;
2557 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2558 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2559 rec = &el->l_recs[idx];
2560 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2561
2562 for (i = 0; i < path->p_tree_depth; i++) {
2563 el = path->p_node[i].el;
2564 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2565 rec = &el->l_recs[idx];
2566
2567 rec->e_int_clusters = cpu_to_le32(range);
2568 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2569
2570 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2571 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002572out:
2573 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002574}
2575
Joel Becker6641b0c2009-02-12 18:57:52 -08002576static void ocfs2_unlink_path(handle_t *handle,
2577 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002578 struct ocfs2_cached_dealloc_ctxt *dealloc,
2579 struct ocfs2_path *path, int unlink_start)
2580{
2581 int ret, i;
2582 struct ocfs2_extent_block *eb;
2583 struct ocfs2_extent_list *el;
2584 struct buffer_head *bh;
2585
2586 for(i = unlink_start; i < path_num_items(path); i++) {
2587 bh = path->p_node[i].bh;
2588
2589 eb = (struct ocfs2_extent_block *)bh->b_data;
2590 /*
2591 * Not all nodes might have had their final count
2592 * decremented by the caller - handle this here.
2593 */
2594 el = &eb->h_list;
2595 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2596 mlog(ML_ERROR,
2597 "Inode %llu, attempted to remove extent block "
2598 "%llu with %u records\n",
Joel Becker6641b0c2009-02-12 18:57:52 -08002599 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07002600 (unsigned long long)le64_to_cpu(eb->h_blkno),
2601 le16_to_cpu(el->l_next_free_rec));
2602
2603 ocfs2_journal_dirty(handle, bh);
Joel Becker6641b0c2009-02-12 18:57:52 -08002604 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002605 continue;
2606 }
2607
2608 el->l_next_free_rec = 0;
2609 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2610
2611 ocfs2_journal_dirty(handle, bh);
2612
2613 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2614 if (ret)
2615 mlog_errno(ret);
2616
Joel Becker6641b0c2009-02-12 18:57:52 -08002617 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002618 }
2619}
2620
Joel Becker6641b0c2009-02-12 18:57:52 -08002621static void ocfs2_unlink_subtree(handle_t *handle,
2622 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002623 struct ocfs2_path *left_path,
2624 struct ocfs2_path *right_path,
2625 int subtree_index,
2626 struct ocfs2_cached_dealloc_ctxt *dealloc)
2627{
2628 int i;
2629 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2630 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2631 struct ocfs2_extent_list *el;
2632 struct ocfs2_extent_block *eb;
2633
2634 el = path_leaf_el(left_path);
2635
2636 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2637
2638 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2639 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2640 break;
2641
2642 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2643
2644 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2645 le16_add_cpu(&root_el->l_next_free_rec, -1);
2646
2647 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2648 eb->h_next_leaf_blk = 0;
2649
2650 ocfs2_journal_dirty(handle, root_bh);
2651 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2652
Joel Becker6641b0c2009-02-12 18:57:52 -08002653 ocfs2_unlink_path(handle, et, dealloc, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002654 subtree_index + 1);
2655}
2656
Joel Becker1e2dd632009-02-12 19:45:28 -08002657static int ocfs2_rotate_subtree_left(handle_t *handle,
2658 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002659 struct ocfs2_path *left_path,
2660 struct ocfs2_path *right_path,
2661 int subtree_index,
2662 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker1e2dd632009-02-12 19:45:28 -08002663 int *deleted)
Mark Fasheh328d5752007-06-18 10:48:04 -07002664{
2665 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002666 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002667 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2668 struct ocfs2_extent_block *eb;
2669
2670 *deleted = 0;
2671
2672 right_leaf_el = path_leaf_el(right_path);
2673 left_leaf_el = path_leaf_el(left_path);
2674 root_bh = left_path->p_node[subtree_index].bh;
2675 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2676
2677 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2678 return 0;
2679
2680 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2681 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2682 /*
2683 * It's legal for us to proceed if the right leaf is
2684 * the rightmost one and it has an empty extent. There
2685 * are two cases to handle - whether the leaf will be
2686 * empty after removal or not. If the leaf isn't empty
2687 * then just remove the empty extent up front. The
2688 * next block will handle empty leaves by flagging
2689 * them for unlink.
2690 *
2691 * Non rightmost leaves will throw -EAGAIN and the
2692 * caller can manually move the subtree and retry.
2693 */
2694
2695 if (eb->h_next_leaf_blk != 0ULL)
2696 return -EAGAIN;
2697
2698 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002699 ret = ocfs2_journal_access_eb(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002700 path_leaf_bh(right_path),
2701 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002702 if (ret) {
2703 mlog_errno(ret);
2704 goto out;
2705 }
2706
2707 ocfs2_remove_empty_extent(right_leaf_el);
2708 } else
2709 right_has_empty = 1;
2710 }
2711
2712 if (eb->h_next_leaf_blk == 0ULL &&
2713 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2714 /*
2715 * We have to update i_last_eb_blk during the meta
2716 * data delete.
2717 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08002718 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07002719 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002720 if (ret) {
2721 mlog_errno(ret);
2722 goto out;
2723 }
2724
2725 del_right_subtree = 1;
2726 }
2727
2728 /*
2729 * Getting here with an empty extent in the right path implies
2730 * that it's the rightmost path and will be deleted.
2731 */
2732 BUG_ON(right_has_empty && !del_right_subtree);
2733
Joel Becker1e2dd632009-02-12 19:45:28 -08002734 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002735 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002736 if (ret) {
2737 mlog_errno(ret);
2738 goto out;
2739 }
2740
2741 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002742 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002743 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002744 if (ret) {
2745 mlog_errno(ret);
2746 goto out;
2747 }
2748
Joel Becker1e2dd632009-02-12 19:45:28 -08002749 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002750 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002751 if (ret) {
2752 mlog_errno(ret);
2753 goto out;
2754 }
2755 }
2756
2757 if (!right_has_empty) {
2758 /*
2759 * Only do this if we're moving a real
2760 * record. Otherwise, the action is delayed until
2761 * after removal of the right path in which case we
2762 * can do a simple shift to remove the empty extent.
2763 */
2764 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2765 memset(&right_leaf_el->l_recs[0], 0,
2766 sizeof(struct ocfs2_extent_rec));
2767 }
2768 if (eb->h_next_leaf_blk == 0ULL) {
2769 /*
2770 * Move recs over to get rid of empty extent, decrease
2771 * next_free. This is allowed to remove the last
2772 * extent in our leaf (setting l_next_free_rec to
2773 * zero) - the delete code below won't care.
2774 */
2775 ocfs2_remove_empty_extent(right_leaf_el);
2776 }
2777
Joel Beckerec20cec2010-03-19 14:13:52 -07002778 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2779 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
Mark Fasheh328d5752007-06-18 10:48:04 -07002780
2781 if (del_right_subtree) {
Joel Becker6641b0c2009-02-12 18:57:52 -08002782 ocfs2_unlink_subtree(handle, et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002783 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08002784 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08002785 left_path);
2786 if (ret) {
2787 mlog_errno(ret);
2788 goto out;
2789 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002790
2791 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002792 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002793
2794 /*
2795 * Removal of the extent in the left leaf was skipped
2796 * above so we could delete the right path
2797 * 1st.
2798 */
2799 if (right_has_empty)
2800 ocfs2_remove_empty_extent(left_leaf_el);
2801
Joel Beckerec20cec2010-03-19 14:13:52 -07002802 ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002803
2804 *deleted = 1;
2805 } else
Joel Becker4619c732009-02-12 19:02:36 -08002806 ocfs2_complete_edge_insert(handle, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002807 subtree_index);
2808
2809out:
2810 return ret;
2811}
2812
2813/*
2814 * Given a full path, determine what cpos value would return us a path
2815 * containing the leaf immediately to the right of the current one.
2816 *
2817 * Will return zero if the path passed in is already the rightmost path.
2818 *
2819 * This looks similar, but is subtly different to
2820 * ocfs2_find_cpos_for_left_leaf().
2821 */
Tao Ma38a04e42009-11-30 14:32:19 +08002822int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2823 struct ocfs2_path *path, u32 *cpos)
Mark Fasheh328d5752007-06-18 10:48:04 -07002824{
2825 int i, j, ret = 0;
2826 u64 blkno;
2827 struct ocfs2_extent_list *el;
2828
2829 *cpos = 0;
2830
2831 if (path->p_tree_depth == 0)
2832 return 0;
2833
2834 blkno = path_leaf_bh(path)->b_blocknr;
2835
2836 /* Start at the tree node just above the leaf and work our way up. */
2837 i = path->p_tree_depth - 1;
2838 while (i >= 0) {
2839 int next_free;
2840
2841 el = path->p_node[i].el;
2842
2843 /*
2844 * Find the extent record just after the one in our
2845 * path.
2846 */
2847 next_free = le16_to_cpu(el->l_next_free_rec);
2848 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2849 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2850 if (j == (next_free - 1)) {
2851 if (i == 0) {
2852 /*
2853 * We've determined that the
2854 * path specified is already
2855 * the rightmost one - return a
2856 * cpos of zero.
2857 */
2858 goto out;
2859 }
2860 /*
2861 * The rightmost record points to our
2862 * leaf - we need to travel up the
2863 * tree one level.
2864 */
2865 goto next_node;
2866 }
2867
2868 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2869 goto out;
2870 }
2871 }
2872
2873 /*
2874 * If we got here, we never found a valid node where
2875 * the tree indicated one should be.
2876 */
2877 ocfs2_error(sb,
2878 "Invalid extent tree at extent block %llu\n",
2879 (unsigned long long)blkno);
2880 ret = -EROFS;
2881 goto out;
2882
2883next_node:
2884 blkno = path->p_node[i].bh->b_blocknr;
2885 i--;
2886 }
2887
2888out:
2889 return ret;
2890}
2891
Joel Becker70f18c02009-02-13 02:09:31 -08002892static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2893 struct ocfs2_extent_tree *et,
Joel Becker13723d02008-10-17 19:25:01 -07002894 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002895{
2896 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002897 struct buffer_head *bh = path_leaf_bh(path);
2898 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002899
2900 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2901 return 0;
2902
Joel Becker70f18c02009-02-13 02:09:31 -08002903 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Joel Becker13723d02008-10-17 19:25:01 -07002904 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002905 if (ret) {
2906 mlog_errno(ret);
2907 goto out;
2908 }
2909
2910 ocfs2_remove_empty_extent(el);
Joel Beckerec20cec2010-03-19 14:13:52 -07002911 ocfs2_journal_dirty(handle, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002912
2913out:
2914 return ret;
2915}
2916
Joel Beckere46f74d2009-02-12 19:47:43 -08002917static int __ocfs2_rotate_tree_left(handle_t *handle,
2918 struct ocfs2_extent_tree *et,
2919 int orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07002920 struct ocfs2_path *path,
2921 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08002922 struct ocfs2_path **empty_extent_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002923{
2924 int ret, subtree_root, deleted;
2925 u32 right_cpos;
2926 struct ocfs2_path *left_path = NULL;
2927 struct ocfs2_path *right_path = NULL;
Joel Beckere46f74d2009-02-12 19:47:43 -08002928 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fasheh328d5752007-06-18 10:48:04 -07002929
2930 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2931
2932 *empty_extent_path = NULL;
2933
Joel Beckere46f74d2009-02-12 19:47:43 -08002934 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002935 if (ret) {
2936 mlog_errno(ret);
2937 goto out;
2938 }
2939
Joel Beckerffdd7a52008-10-17 22:32:01 -07002940 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002941 if (!left_path) {
2942 ret = -ENOMEM;
2943 mlog_errno(ret);
2944 goto out;
2945 }
2946
2947 ocfs2_cp_path(left_path, path);
2948
Joel Beckerffdd7a52008-10-17 22:32:01 -07002949 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002950 if (!right_path) {
2951 ret = -ENOMEM;
2952 mlog_errno(ret);
2953 goto out;
2954 }
2955
2956 while (right_cpos) {
Joel Beckerfacdb772009-02-12 18:08:48 -08002957 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002958 if (ret) {
2959 mlog_errno(ret);
2960 goto out;
2961 }
2962
Joel Becker7dc02802009-02-12 19:20:13 -08002963 subtree_root = ocfs2_find_subtree_root(et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002964 right_path);
2965
2966 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2967 subtree_root,
2968 (unsigned long long)
2969 right_path->p_node[subtree_root].bh->b_blocknr,
2970 right_path->p_tree_depth);
2971
2972 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2973 orig_credits, left_path);
2974 if (ret) {
2975 mlog_errno(ret);
2976 goto out;
2977 }
2978
Mark Fashehe8aed342007-12-03 16:43:01 -08002979 /*
2980 * Caller might still want to make changes to the
2981 * tree root, so re-add it to the journal here.
2982 */
Joel Beckere46f74d2009-02-12 19:47:43 -08002983 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002984 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08002985 if (ret) {
2986 mlog_errno(ret);
2987 goto out;
2988 }
2989
Joel Becker1e2dd632009-02-12 19:45:28 -08002990 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002991 right_path, subtree_root,
Joel Becker1e2dd632009-02-12 19:45:28 -08002992 dealloc, &deleted);
Mark Fasheh328d5752007-06-18 10:48:04 -07002993 if (ret == -EAGAIN) {
2994 /*
2995 * The rotation has to temporarily stop due to
2996 * the right subtree having an empty
2997 * extent. Pass it back to the caller for a
2998 * fixup.
2999 */
3000 *empty_extent_path = right_path;
3001 right_path = NULL;
3002 goto out;
3003 }
3004 if (ret) {
3005 mlog_errno(ret);
3006 goto out;
3007 }
3008
3009 /*
3010 * The subtree rotate might have removed records on
3011 * the rightmost edge. If so, then rotation is
3012 * complete.
3013 */
3014 if (deleted)
3015 break;
3016
3017 ocfs2_mv_path(left_path, right_path);
3018
Joel Beckere46f74d2009-02-12 19:47:43 -08003019 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003020 &right_cpos);
3021 if (ret) {
3022 mlog_errno(ret);
3023 goto out;
3024 }
3025 }
3026
3027out:
3028 ocfs2_free_path(right_path);
3029 ocfs2_free_path(left_path);
3030
3031 return ret;
3032}
3033
Joel Becker70f18c02009-02-13 02:09:31 -08003034static int ocfs2_remove_rightmost_path(handle_t *handle,
3035 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003036 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003037 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003038{
3039 int ret, subtree_index;
3040 u32 cpos;
3041 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003042 struct ocfs2_extent_block *eb;
3043 struct ocfs2_extent_list *el;
3044
Mark Fasheh328d5752007-06-18 10:48:04 -07003045
Joel Becker6136ca52009-02-12 19:32:43 -08003046 ret = ocfs2_et_sanity_check(et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003047 if (ret)
3048 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003049 /*
3050 * There's two ways we handle this depending on
3051 * whether path is the only existing one.
3052 */
3053 ret = ocfs2_extend_rotate_transaction(handle, 0,
3054 handle->h_buffer_credits,
3055 path);
3056 if (ret) {
3057 mlog_errno(ret);
3058 goto out;
3059 }
3060
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003061 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003062 if (ret) {
3063 mlog_errno(ret);
3064 goto out;
3065 }
3066
Joel Becker3d03a302009-02-12 17:49:26 -08003067 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3068 path, &cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003069 if (ret) {
3070 mlog_errno(ret);
3071 goto out;
3072 }
3073
3074 if (cpos) {
3075 /*
3076 * We have a path to the left of this one - it needs
3077 * an update too.
3078 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003079 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003080 if (!left_path) {
3081 ret = -ENOMEM;
3082 mlog_errno(ret);
3083 goto out;
3084 }
3085
Joel Beckerfacdb772009-02-12 18:08:48 -08003086 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003087 if (ret) {
3088 mlog_errno(ret);
3089 goto out;
3090 }
3091
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003092 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003093 if (ret) {
3094 mlog_errno(ret);
3095 goto out;
3096 }
3097
Joel Becker7dc02802009-02-12 19:20:13 -08003098 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003099
Joel Becker6641b0c2009-02-12 18:57:52 -08003100 ocfs2_unlink_subtree(handle, et, left_path, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003101 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08003102 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08003103 left_path);
3104 if (ret) {
3105 mlog_errno(ret);
3106 goto out;
3107 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003108
3109 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003110 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003111 } else {
3112 /*
3113 * 'path' is also the leftmost path which
3114 * means it must be the only one. This gets
3115 * handled differently because we want to
Joel Becker70f18c02009-02-13 02:09:31 -08003116 * revert the root back to having extents
Mark Fasheh328d5752007-06-18 10:48:04 -07003117 * in-line.
3118 */
Joel Becker6641b0c2009-02-12 18:57:52 -08003119 ocfs2_unlink_path(handle, et, dealloc, path, 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003120
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003121 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003122 el->l_tree_depth = 0;
3123 el->l_next_free_rec = 0;
3124 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3125
Joel Becker35dc0aa2008-08-20 16:25:06 -07003126 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003127 }
3128
3129 ocfs2_journal_dirty(handle, path_root_bh(path));
3130
3131out:
3132 ocfs2_free_path(left_path);
3133 return ret;
3134}
3135
3136/*
3137 * Left rotation of btree records.
3138 *
3139 * In many ways, this is (unsurprisingly) the opposite of right
3140 * rotation. We start at some non-rightmost path containing an empty
3141 * extent in the leaf block. The code works its way to the rightmost
3142 * path by rotating records to the left in every subtree.
3143 *
3144 * This is used by any code which reduces the number of extent records
3145 * in a leaf. After removal, an empty record should be placed in the
3146 * leftmost list position.
3147 *
3148 * This won't handle a length update of the rightmost path records if
3149 * the rightmost tree leaf record is removed so the caller is
3150 * responsible for detecting and correcting that.
3151 */
Joel Becker70f18c02009-02-13 02:09:31 -08003152static int ocfs2_rotate_tree_left(handle_t *handle,
3153 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003154 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003155 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003156{
3157 int ret, orig_credits = handle->h_buffer_credits;
3158 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3159 struct ocfs2_extent_block *eb;
3160 struct ocfs2_extent_list *el;
3161
3162 el = path_leaf_el(path);
3163 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3164 return 0;
3165
3166 if (path->p_tree_depth == 0) {
3167rightmost_no_delete:
3168 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003169 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003170 * it up front.
3171 */
Joel Becker70f18c02009-02-13 02:09:31 -08003172 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003173 if (ret)
3174 mlog_errno(ret);
3175 goto out;
3176 }
3177
3178 /*
3179 * Handle rightmost branch now. There's several cases:
3180 * 1) simple rotation leaving records in there. That's trivial.
3181 * 2) rotation requiring a branch delete - there's no more
3182 * records left. Two cases of this:
3183 * a) There are branches to the left.
3184 * b) This is also the leftmost (the only) branch.
3185 *
3186 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3187 * 2a) we need the left branch so that we can update it with the unlink
Joel Becker70f18c02009-02-13 02:09:31 -08003188 * 2b) we need to bring the root back to inline extents.
Mark Fasheh328d5752007-06-18 10:48:04 -07003189 */
3190
3191 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3192 el = &eb->h_list;
3193 if (eb->h_next_leaf_blk == 0) {
3194 /*
3195 * This gets a bit tricky if we're going to delete the
3196 * rightmost path. Get the other cases out of the way
3197 * 1st.
3198 */
3199 if (le16_to_cpu(el->l_next_free_rec) > 1)
3200 goto rightmost_no_delete;
3201
3202 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3203 ret = -EIO;
Joel Becker70f18c02009-02-13 02:09:31 -08003204 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3205 "Owner %llu has empty extent block at %llu",
3206 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07003207 (unsigned long long)le64_to_cpu(eb->h_blkno));
3208 goto out;
3209 }
3210
3211 /*
3212 * XXX: The caller can not trust "path" any more after
3213 * this as it will have been deleted. What do we do?
3214 *
3215 * In theory the rotate-for-merge code will never get
3216 * here because it'll always ask for a rotate in a
3217 * nonempty list.
3218 */
3219
Joel Becker70f18c02009-02-13 02:09:31 -08003220 ret = ocfs2_remove_rightmost_path(handle, et, path,
3221 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003222 if (ret)
3223 mlog_errno(ret);
3224 goto out;
3225 }
3226
3227 /*
3228 * Now we can loop, remembering the path we get from -EAGAIN
3229 * and restarting from there.
3230 */
3231try_rotate:
Joel Beckere46f74d2009-02-12 19:47:43 -08003232 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3233 dealloc, &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003234 if (ret && ret != -EAGAIN) {
3235 mlog_errno(ret);
3236 goto out;
3237 }
3238
3239 while (ret == -EAGAIN) {
3240 tmp_path = restart_path;
3241 restart_path = NULL;
3242
Joel Beckere46f74d2009-02-12 19:47:43 -08003243 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07003244 tmp_path, dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08003245 &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003246 if (ret && ret != -EAGAIN) {
3247 mlog_errno(ret);
3248 goto out;
3249 }
3250
3251 ocfs2_free_path(tmp_path);
3252 tmp_path = NULL;
3253
3254 if (ret == 0)
3255 goto try_rotate;
3256 }
3257
3258out:
3259 ocfs2_free_path(tmp_path);
3260 ocfs2_free_path(restart_path);
3261 return ret;
3262}
3263
3264static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3265 int index)
3266{
3267 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3268 unsigned int size;
3269
3270 if (rec->e_leaf_clusters == 0) {
3271 /*
3272 * We consumed all of the merged-from record. An empty
3273 * extent cannot exist anywhere but the 1st array
3274 * position, so move things over if the merged-from
3275 * record doesn't occupy that position.
3276 *
3277 * This creates a new empty extent so the caller
3278 * should be smart enough to have removed any existing
3279 * ones.
3280 */
3281 if (index > 0) {
3282 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3283 size = index * sizeof(struct ocfs2_extent_rec);
3284 memmove(&el->l_recs[1], &el->l_recs[0], size);
3285 }
3286
3287 /*
3288 * Always memset - the caller doesn't check whether it
3289 * created an empty extent, so there could be junk in
3290 * the other fields.
3291 */
3292 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3293 }
3294}
3295
Joel Becker4fe82c32009-02-13 02:16:08 -08003296static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003297 struct ocfs2_path *left_path,
3298 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003299{
3300 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003301 u32 right_cpos;
3302 struct ocfs2_path *right_path = NULL;
3303 struct ocfs2_extent_list *left_el;
3304
3305 *ret_right_path = NULL;
3306
3307 /* This function shouldn't be called for non-trees. */
3308 BUG_ON(left_path->p_tree_depth == 0);
3309
3310 left_el = path_leaf_el(left_path);
3311 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3312
Joel Becker4fe82c32009-02-13 02:16:08 -08003313 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3314 left_path, &right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003315 if (ret) {
3316 mlog_errno(ret);
3317 goto out;
3318 }
3319
3320 /* This function shouldn't be called for the rightmost leaf. */
3321 BUG_ON(right_cpos == 0);
3322
Joel Beckerffdd7a52008-10-17 22:32:01 -07003323 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003324 if (!right_path) {
3325 ret = -ENOMEM;
3326 mlog_errno(ret);
3327 goto out;
3328 }
3329
Joel Becker4fe82c32009-02-13 02:16:08 -08003330 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003331 if (ret) {
3332 mlog_errno(ret);
3333 goto out;
3334 }
3335
3336 *ret_right_path = right_path;
3337out:
3338 if (ret)
3339 ocfs2_free_path(right_path);
3340 return ret;
3341}
3342
3343/*
3344 * Remove split_rec clusters from the record at index and merge them
3345 * onto the beginning of the record "next" to it.
3346 * For index < l_count - 1, the next means the extent rec at index + 1.
3347 * For index == l_count - 1, the "next" means the 1st extent rec of the
3348 * next extent block.
3349 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003350static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003351 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003352 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003353 struct ocfs2_extent_rec *split_rec,
3354 int index)
3355{
3356 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003357 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3358 struct ocfs2_extent_rec *left_rec;
3359 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003360 struct ocfs2_extent_list *right_el;
3361 struct ocfs2_path *right_path = NULL;
3362 int subtree_index = 0;
3363 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3364 struct buffer_head *bh = path_leaf_bh(left_path);
3365 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003366
3367 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003368 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003369
Al Viro9d8df6a2008-05-21 06:32:11 +01003370 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003371 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3372 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003373 ret = ocfs2_get_right_path(et, left_path, &right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003374 if (ret) {
3375 mlog_errno(ret);
3376 goto out;
3377 }
3378
3379 right_el = path_leaf_el(right_path);
3380 next_free = le16_to_cpu(right_el->l_next_free_rec);
3381 BUG_ON(next_free <= 0);
3382 right_rec = &right_el->l_recs[0];
3383 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003384 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003385 right_rec = &right_el->l_recs[1];
3386 }
3387
3388 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3389 le16_to_cpu(left_rec->e_leaf_clusters) !=
3390 le32_to_cpu(right_rec->e_cpos));
3391
Joel Becker7dc02802009-02-12 19:20:13 -08003392 subtree_index = ocfs2_find_subtree_root(et, left_path,
3393 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003394
3395 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3396 handle->h_buffer_credits,
3397 right_path);
3398 if (ret) {
3399 mlog_errno(ret);
3400 goto out;
3401 }
3402
3403 root_bh = left_path->p_node[subtree_index].bh;
3404 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3405
Joel Becker7dc02802009-02-12 19:20:13 -08003406 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003407 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003408 if (ret) {
3409 mlog_errno(ret);
3410 goto out;
3411 }
3412
3413 for (i = subtree_index + 1;
3414 i < path_num_items(right_path); i++) {
Joel Becker7dc02802009-02-12 19:20:13 -08003415 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003416 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003417 if (ret) {
3418 mlog_errno(ret);
3419 goto out;
3420 }
3421
Joel Becker7dc02802009-02-12 19:20:13 -08003422 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003423 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003424 if (ret) {
3425 mlog_errno(ret);
3426 goto out;
3427 }
3428 }
3429
3430 } else {
3431 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3432 right_rec = &el->l_recs[index + 1];
3433 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003434
Joel Becker7dc02802009-02-12 19:20:13 -08003435 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
Joel Becker13723d02008-10-17 19:25:01 -07003436 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003437 if (ret) {
3438 mlog_errno(ret);
3439 goto out;
3440 }
3441
3442 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3443
3444 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3445 le64_add_cpu(&right_rec->e_blkno,
Joel Becker7dc02802009-02-12 19:20:13 -08003446 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3447 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003448 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3449
3450 ocfs2_cleanup_merge(el, index);
3451
Joel Beckerec20cec2010-03-19 14:13:52 -07003452 ocfs2_journal_dirty(handle, bh);
Tao Ma677b9752008-01-30 14:21:05 +08003453 if (right_path) {
Joel Beckerec20cec2010-03-19 14:13:52 -07003454 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
Joel Becker4619c732009-02-12 19:02:36 -08003455 ocfs2_complete_edge_insert(handle, left_path, right_path,
3456 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003457 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003458out:
Tao Ma677b9752008-01-30 14:21:05 +08003459 if (right_path)
3460 ocfs2_free_path(right_path);
3461 return ret;
3462}
3463
Joel Becker4fe82c32009-02-13 02:16:08 -08003464static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003465 struct ocfs2_path *right_path,
3466 struct ocfs2_path **ret_left_path)
3467{
3468 int ret;
3469 u32 left_cpos;
3470 struct ocfs2_path *left_path = NULL;
3471
3472 *ret_left_path = NULL;
3473
3474 /* This function shouldn't be called for non-trees. */
3475 BUG_ON(right_path->p_tree_depth == 0);
3476
Joel Becker4fe82c32009-02-13 02:16:08 -08003477 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
Tao Ma677b9752008-01-30 14:21:05 +08003478 right_path, &left_cpos);
3479 if (ret) {
3480 mlog_errno(ret);
3481 goto out;
3482 }
3483
3484 /* This function shouldn't be called for the leftmost leaf. */
3485 BUG_ON(left_cpos == 0);
3486
Joel Beckerffdd7a52008-10-17 22:32:01 -07003487 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003488 if (!left_path) {
3489 ret = -ENOMEM;
3490 mlog_errno(ret);
3491 goto out;
3492 }
3493
Joel Becker4fe82c32009-02-13 02:16:08 -08003494 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003495 if (ret) {
3496 mlog_errno(ret);
3497 goto out;
3498 }
3499
3500 *ret_left_path = left_path;
3501out:
3502 if (ret)
3503 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003504 return ret;
3505}
3506
3507/*
3508 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003509 * onto the tail of the record "before" it.
3510 * For index > 0, the "before" means the extent rec at index - 1.
3511 *
3512 * For index == 0, the "before" means the last record of the previous
3513 * extent block. And there is also a situation that we may need to
3514 * remove the rightmost leaf extent block in the right_path and change
3515 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003516 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003517static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003518 handle_t *handle,
Joel Becker4fe82c32009-02-13 02:16:08 -08003519 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003520 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003521 struct ocfs2_cached_dealloc_ctxt *dealloc,
3522 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003523{
Tao Ma677b9752008-01-30 14:21:05 +08003524 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003525 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3526 struct ocfs2_extent_rec *left_rec;
3527 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003528 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3529 struct buffer_head *bh = path_leaf_bh(right_path);
3530 struct buffer_head *root_bh = NULL;
3531 struct ocfs2_path *left_path = NULL;
3532 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003533
Tao Ma677b9752008-01-30 14:21:05 +08003534 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003535
Mark Fasheh328d5752007-06-18 10:48:04 -07003536 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003537 if (index == 0) {
3538 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003539 ret = ocfs2_get_left_path(et, right_path, &left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003540 if (ret) {
3541 mlog_errno(ret);
3542 goto out;
3543 }
3544
3545 left_el = path_leaf_el(left_path);
3546 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3547 le16_to_cpu(left_el->l_count));
3548
3549 left_rec = &left_el->l_recs[
3550 le16_to_cpu(left_el->l_next_free_rec) - 1];
3551 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3552 le16_to_cpu(left_rec->e_leaf_clusters) !=
3553 le32_to_cpu(split_rec->e_cpos));
3554
Joel Becker7dc02802009-02-12 19:20:13 -08003555 subtree_index = ocfs2_find_subtree_root(et, left_path,
3556 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003557
3558 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3559 handle->h_buffer_credits,
3560 left_path);
3561 if (ret) {
3562 mlog_errno(ret);
3563 goto out;
3564 }
3565
3566 root_bh = left_path->p_node[subtree_index].bh;
3567 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3568
Joel Becker4fe82c32009-02-13 02:16:08 -08003569 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003570 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003571 if (ret) {
3572 mlog_errno(ret);
3573 goto out;
3574 }
3575
3576 for (i = subtree_index + 1;
3577 i < path_num_items(right_path); i++) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003578 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003579 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003580 if (ret) {
3581 mlog_errno(ret);
3582 goto out;
3583 }
3584
Joel Becker4fe82c32009-02-13 02:16:08 -08003585 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003586 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003587 if (ret) {
3588 mlog_errno(ret);
3589 goto out;
3590 }
3591 }
3592 } else {
3593 left_rec = &el->l_recs[index - 1];
3594 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3595 has_empty_extent = 1;
3596 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003597
Joel Becker4fe82c32009-02-13 02:16:08 -08003598 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Tao Ma9047bea2009-01-05 14:45:24 +08003599 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003600 if (ret) {
3601 mlog_errno(ret);
3602 goto out;
3603 }
3604
3605 if (has_empty_extent && index == 1) {
3606 /*
3607 * The easy case - we can just plop the record right in.
3608 */
3609 *left_rec = *split_rec;
3610
3611 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003612 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003613 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003614
3615 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3616 le64_add_cpu(&right_rec->e_blkno,
Joel Becker4fe82c32009-02-13 02:16:08 -08003617 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3618 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003619 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3620
3621 ocfs2_cleanup_merge(el, index);
3622
Joel Beckerec20cec2010-03-19 14:13:52 -07003623 ocfs2_journal_dirty(handle, bh);
Tao Ma677b9752008-01-30 14:21:05 +08003624 if (left_path) {
Joel Beckerec20cec2010-03-19 14:13:52 -07003625 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
Tao Ma677b9752008-01-30 14:21:05 +08003626
3627 /*
3628 * In the situation that the right_rec is empty and the extent
3629 * block is empty also, ocfs2_complete_edge_insert can't handle
3630 * it and we need to delete the right extent block.
3631 */
3632 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3633 le16_to_cpu(el->l_next_free_rec) == 1) {
3634
Joel Becker70f18c02009-02-13 02:09:31 -08003635 ret = ocfs2_remove_rightmost_path(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003636 right_path,
Joel Becker70f18c02009-02-13 02:09:31 -08003637 dealloc);
Tao Ma677b9752008-01-30 14:21:05 +08003638 if (ret) {
3639 mlog_errno(ret);
3640 goto out;
3641 }
3642
3643 /* Now the rightmost extent block has been deleted.
3644 * So we use the new rightmost path.
3645 */
3646 ocfs2_mv_path(right_path, left_path);
3647 left_path = NULL;
3648 } else
Joel Becker4619c732009-02-12 19:02:36 -08003649 ocfs2_complete_edge_insert(handle, left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003650 right_path, subtree_index);
3651 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003652out:
Tao Ma677b9752008-01-30 14:21:05 +08003653 if (left_path)
3654 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003655 return ret;
3656}
3657
Joel Beckerc495dd22009-02-13 02:19:11 -08003658static int ocfs2_try_to_merge_extent(handle_t *handle,
3659 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003660 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003661 int split_index,
3662 struct ocfs2_extent_rec *split_rec,
3663 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckerc495dd22009-02-13 02:19:11 -08003664 struct ocfs2_merge_ctxt *ctxt)
Mark Fasheh328d5752007-06-18 10:48:04 -07003665{
Tao Mao518d7262007-08-28 17:25:35 -07003666 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003667 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003668 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3669
3670 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3671
Tao Mao518d7262007-08-28 17:25:35 -07003672 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3673 /*
3674 * The merge code will need to create an empty
3675 * extent to take the place of the newly
3676 * emptied slot. Remove any pre-existing empty
3677 * extents - having more than one in a leaf is
3678 * illegal.
3679 */
Joel Becker70f18c02009-02-13 02:09:31 -08003680 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Tao Mao518d7262007-08-28 17:25:35 -07003681 if (ret) {
3682 mlog_errno(ret);
3683 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003684 }
Tao Mao518d7262007-08-28 17:25:35 -07003685 split_index--;
3686 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003687 }
3688
3689 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3690 /*
3691 * Left-right contig implies this.
3692 */
3693 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003694
3695 /*
3696 * Since the leftright insert always covers the entire
3697 * extent, this call will delete the insert record
3698 * entirely, resulting in an empty extent record added to
3699 * the extent block.
3700 *
3701 * Since the adding of an empty extent shifts
3702 * everything back to the right, there's no need to
3703 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003704 *
3705 * When the split_index is zero, we need to merge it to the
3706 * prevoius extent block. It is more efficient and easier
3707 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003708 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003709 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003710 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003711 if (ret) {
3712 mlog_errno(ret);
3713 goto out;
3714 }
3715
3716 /*
3717 * We can only get this from logic error above.
3718 */
3719 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3720
Tao Ma677b9752008-01-30 14:21:05 +08003721 /* The merge left us with an empty extent, remove it. */
Joel Becker70f18c02009-02-13 02:09:31 -08003722 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003723 if (ret) {
3724 mlog_errno(ret);
3725 goto out;
3726 }
Tao Ma677b9752008-01-30 14:21:05 +08003727
Mark Fasheh328d5752007-06-18 10:48:04 -07003728 rec = &el->l_recs[split_index];
3729
3730 /*
3731 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003732 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003733 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003734 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3735 dealloc, split_index);
Tao Ma677b9752008-01-30 14:21:05 +08003736
Mark Fasheh328d5752007-06-18 10:48:04 -07003737 if (ret) {
3738 mlog_errno(ret);
3739 goto out;
3740 }
3741
Joel Becker70f18c02009-02-13 02:09:31 -08003742 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003743 /*
3744 * Error from this last rotate is not critical, so
3745 * print but don't bubble it up.
3746 */
3747 if (ret)
3748 mlog_errno(ret);
3749 ret = 0;
3750 } else {
3751 /*
3752 * Merge a record to the left or right.
3753 *
3754 * 'contig_type' is relative to the existing record,
3755 * so for example, if we're "right contig", it's to
3756 * the record on the left (hence the left merge).
3757 */
3758 if (ctxt->c_contig_type == CONTIG_RIGHT) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003759 ret = ocfs2_merge_rec_left(path, handle, et,
3760 split_rec, dealloc,
Mark Fasheh328d5752007-06-18 10:48:04 -07003761 split_index);
3762 if (ret) {
3763 mlog_errno(ret);
3764 goto out;
3765 }
3766 } else {
Joel Becker4fe82c32009-02-13 02:16:08 -08003767 ret = ocfs2_merge_rec_right(path, handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003768 et, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003769 split_index);
3770 if (ret) {
3771 mlog_errno(ret);
3772 goto out;
3773 }
3774 }
3775
3776 if (ctxt->c_split_covers_rec) {
3777 /*
3778 * The merge may have left an empty extent in
3779 * our leaf. Try to rotate it away.
3780 */
Joel Becker70f18c02009-02-13 02:09:31 -08003781 ret = ocfs2_rotate_tree_left(handle, et, path,
3782 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003783 if (ret)
3784 mlog_errno(ret);
3785 ret = 0;
3786 }
3787 }
3788
3789out:
3790 return ret;
3791}
3792
3793static void ocfs2_subtract_from_rec(struct super_block *sb,
3794 enum ocfs2_split_type split,
3795 struct ocfs2_extent_rec *rec,
3796 struct ocfs2_extent_rec *split_rec)
3797{
3798 u64 len_blocks;
3799
3800 len_blocks = ocfs2_clusters_to_blocks(sb,
3801 le16_to_cpu(split_rec->e_leaf_clusters));
3802
3803 if (split == SPLIT_LEFT) {
3804 /*
3805 * Region is on the left edge of the existing
3806 * record.
3807 */
3808 le32_add_cpu(&rec->e_cpos,
3809 le16_to_cpu(split_rec->e_leaf_clusters));
3810 le64_add_cpu(&rec->e_blkno, len_blocks);
3811 le16_add_cpu(&rec->e_leaf_clusters,
3812 -le16_to_cpu(split_rec->e_leaf_clusters));
3813 } else {
3814 /*
3815 * Region is on the right edge of the existing
3816 * record.
3817 */
3818 le16_add_cpu(&rec->e_leaf_clusters,
3819 -le16_to_cpu(split_rec->e_leaf_clusters));
3820 }
3821}
3822
Mark Fashehdcd05382007-01-16 11:32:23 -08003823/*
3824 * Do the final bits of extent record insertion at the target leaf
3825 * list. If this leaf is part of an allocation tree, it is assumed
3826 * that the tree above has been prepared.
3827 */
Joel Beckerd5628622009-02-13 02:54:36 -08003828static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3829 struct ocfs2_extent_rec *insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08003830 struct ocfs2_extent_list *el,
Joel Beckerd5628622009-02-13 02:54:36 -08003831 struct ocfs2_insert_type *insert)
Mark Fashehdcd05382007-01-16 11:32:23 -08003832{
3833 int i = insert->ins_contig_index;
3834 unsigned int range;
3835 struct ocfs2_extent_rec *rec;
3836
Mark Fashehe48edee2007-03-07 16:46:57 -08003837 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003838
Mark Fasheh328d5752007-06-18 10:48:04 -07003839 if (insert->ins_split != SPLIT_NONE) {
3840 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3841 BUG_ON(i == -1);
3842 rec = &el->l_recs[i];
Joel Beckerd5628622009-02-13 02:54:36 -08003843 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3844 insert->ins_split, rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003845 insert_rec);
3846 goto rotate;
3847 }
3848
Mark Fashehdcd05382007-01-16 11:32:23 -08003849 /*
3850 * Contiguous insert - either left or right.
3851 */
3852 if (insert->ins_contig != CONTIG_NONE) {
3853 rec = &el->l_recs[i];
3854 if (insert->ins_contig == CONTIG_LEFT) {
3855 rec->e_blkno = insert_rec->e_blkno;
3856 rec->e_cpos = insert_rec->e_cpos;
3857 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003858 le16_add_cpu(&rec->e_leaf_clusters,
3859 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003860 return;
3861 }
3862
3863 /*
3864 * Handle insert into an empty leaf.
3865 */
3866 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3867 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3868 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3869 el->l_recs[0] = *insert_rec;
3870 el->l_next_free_rec = cpu_to_le16(1);
3871 return;
3872 }
3873
3874 /*
3875 * Appending insert.
3876 */
3877 if (insert->ins_appending == APPEND_TAIL) {
3878 i = le16_to_cpu(el->l_next_free_rec) - 1;
3879 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003880 range = le32_to_cpu(rec->e_cpos)
3881 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003882 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3883
3884 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3885 le16_to_cpu(el->l_count),
Joel Beckerd5628622009-02-13 02:54:36 -08003886 "owner %llu, depth %u, count %u, next free %u, "
Mark Fashehdcd05382007-01-16 11:32:23 -08003887 "rec.cpos %u, rec.clusters %u, "
3888 "insert.cpos %u, insert.clusters %u\n",
Joel Beckerd5628622009-02-13 02:54:36 -08003889 ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08003890 le16_to_cpu(el->l_tree_depth),
3891 le16_to_cpu(el->l_count),
3892 le16_to_cpu(el->l_next_free_rec),
3893 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003894 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003895 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003896 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003897 i++;
3898 el->l_recs[i] = *insert_rec;
3899 le16_add_cpu(&el->l_next_free_rec, 1);
3900 return;
3901 }
3902
Mark Fasheh328d5752007-06-18 10:48:04 -07003903rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003904 /*
3905 * Ok, we have to rotate.
3906 *
3907 * At this point, it is safe to assume that inserting into an
3908 * empty leaf and appending to a leaf have both been handled
3909 * above.
3910 *
3911 * This leaf needs to have space, either by the empty 1st
3912 * extent record, or by virtue of an l_next_rec < l_count.
3913 */
3914 ocfs2_rotate_leaf(el, insert_rec);
3915}
3916
Joel Beckerd401dc12009-02-13 02:24:10 -08003917static void ocfs2_adjust_rightmost_records(handle_t *handle,
3918 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003919 struct ocfs2_path *path,
3920 struct ocfs2_extent_rec *insert_rec)
3921{
3922 int ret, i, next_free;
3923 struct buffer_head *bh;
3924 struct ocfs2_extent_list *el;
3925 struct ocfs2_extent_rec *rec;
3926
3927 /*
3928 * Update everything except the leaf block.
3929 */
3930 for (i = 0; i < path->p_tree_depth; i++) {
3931 bh = path->p_node[i].bh;
3932 el = path->p_node[i].el;
3933
3934 next_free = le16_to_cpu(el->l_next_free_rec);
3935 if (next_free == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08003936 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3937 "Owner %llu has a bad extent list",
3938 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fasheh328d5752007-06-18 10:48:04 -07003939 ret = -EIO;
3940 return;
3941 }
3942
3943 rec = &el->l_recs[next_free - 1];
3944
3945 rec->e_int_clusters = insert_rec->e_cpos;
3946 le32_add_cpu(&rec->e_int_clusters,
3947 le16_to_cpu(insert_rec->e_leaf_clusters));
3948 le32_add_cpu(&rec->e_int_clusters,
3949 -le32_to_cpu(rec->e_cpos));
3950
Joel Beckerec20cec2010-03-19 14:13:52 -07003951 ocfs2_journal_dirty(handle, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07003952 }
3953}
3954
Joel Beckerd401dc12009-02-13 02:24:10 -08003955static int ocfs2_append_rec_to_path(handle_t *handle,
3956 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08003957 struct ocfs2_extent_rec *insert_rec,
3958 struct ocfs2_path *right_path,
3959 struct ocfs2_path **ret_left_path)
3960{
Mark Fasheh328d5752007-06-18 10:48:04 -07003961 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003962 struct ocfs2_extent_list *el;
3963 struct ocfs2_path *left_path = NULL;
3964
3965 *ret_left_path = NULL;
3966
3967 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003968 * This shouldn't happen for non-trees. The extent rec cluster
3969 * count manipulation below only works for interior nodes.
3970 */
3971 BUG_ON(right_path->p_tree_depth == 0);
3972
3973 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003974 * If our appending insert is at the leftmost edge of a leaf,
3975 * then we might need to update the rightmost records of the
3976 * neighboring path.
3977 */
3978 el = path_leaf_el(right_path);
3979 next_free = le16_to_cpu(el->l_next_free_rec);
3980 if (next_free == 0 ||
3981 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3982 u32 left_cpos;
3983
Joel Beckerd401dc12009-02-13 02:24:10 -08003984 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3985 right_path, &left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08003986 if (ret) {
3987 mlog_errno(ret);
3988 goto out;
3989 }
3990
3991 mlog(0, "Append may need a left path update. cpos: %u, "
3992 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3993 left_cpos);
3994
3995 /*
3996 * No need to worry if the append is already in the
3997 * leftmost leaf.
3998 */
3999 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004000 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004001 if (!left_path) {
4002 ret = -ENOMEM;
4003 mlog_errno(ret);
4004 goto out;
4005 }
4006
Joel Beckerd401dc12009-02-13 02:24:10 -08004007 ret = ocfs2_find_path(et->et_ci, left_path,
Joel Beckerfacdb772009-02-12 18:08:48 -08004008 left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004009 if (ret) {
4010 mlog_errno(ret);
4011 goto out;
4012 }
4013
4014 /*
4015 * ocfs2_insert_path() will pass the left_path to the
4016 * journal for us.
4017 */
4018 }
4019 }
4020
Joel Beckerd401dc12009-02-13 02:24:10 -08004021 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004022 if (ret) {
4023 mlog_errno(ret);
4024 goto out;
4025 }
4026
Joel Beckerd401dc12009-02-13 02:24:10 -08004027 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004028
4029 *ret_left_path = left_path;
4030 ret = 0;
4031out:
4032 if (ret != 0)
4033 ocfs2_free_path(left_path);
4034
4035 return ret;
4036}
4037
Joel Beckerc38e52b2009-02-13 02:56:23 -08004038static void ocfs2_split_record(struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004039 struct ocfs2_path *left_path,
4040 struct ocfs2_path *right_path,
4041 struct ocfs2_extent_rec *split_rec,
4042 enum ocfs2_split_type split)
4043{
4044 int index;
4045 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4046 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4047 struct ocfs2_extent_rec *rec, *tmprec;
4048
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004049 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004050 if (left_path)
4051 left_el = path_leaf_el(left_path);
4052
4053 el = right_el;
4054 insert_el = right_el;
4055 index = ocfs2_search_extent_list(el, cpos);
4056 if (index != -1) {
4057 if (index == 0 && left_path) {
4058 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4059
4060 /*
4061 * This typically means that the record
4062 * started in the left path but moved to the
4063 * right as a result of rotation. We either
4064 * move the existing record to the left, or we
4065 * do the later insert there.
4066 *
4067 * In this case, the left path should always
4068 * exist as the rotate code will have passed
4069 * it back for a post-insert update.
4070 */
4071
4072 if (split == SPLIT_LEFT) {
4073 /*
4074 * It's a left split. Since we know
4075 * that the rotate code gave us an
4076 * empty extent in the left path, we
4077 * can just do the insert there.
4078 */
4079 insert_el = left_el;
4080 } else {
4081 /*
4082 * Right split - we have to move the
4083 * existing record over to the left
4084 * leaf. The insert will be into the
4085 * newly created empty extent in the
4086 * right leaf.
4087 */
4088 tmprec = &right_el->l_recs[index];
4089 ocfs2_rotate_leaf(left_el, tmprec);
4090 el = left_el;
4091
4092 memset(tmprec, 0, sizeof(*tmprec));
4093 index = ocfs2_search_extent_list(left_el, cpos);
4094 BUG_ON(index == -1);
4095 }
4096 }
4097 } else {
4098 BUG_ON(!left_path);
4099 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4100 /*
4101 * Left path is easy - we can just allow the insert to
4102 * happen.
4103 */
4104 el = left_el;
4105 insert_el = left_el;
4106 index = ocfs2_search_extent_list(el, cpos);
4107 BUG_ON(index == -1);
4108 }
4109
4110 rec = &el->l_recs[index];
Joel Beckerc38e52b2009-02-13 02:56:23 -08004111 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4112 split, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004113 ocfs2_rotate_leaf(insert_el, split_rec);
4114}
4115
Mark Fashehdcd05382007-01-16 11:32:23 -08004116/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004117 * This function only does inserts on an allocation b-tree. For tree
4118 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004119 *
4120 * right_path is the path we want to do the actual insert
4121 * in. left_path should only be passed in if we need to update that
4122 * portion of the tree after an edge insert.
4123 */
Joel Becker3505bec2009-02-13 02:57:58 -08004124static int ocfs2_insert_path(handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08004125 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004126 struct ocfs2_path *left_path,
4127 struct ocfs2_path *right_path,
4128 struct ocfs2_extent_rec *insert_rec,
4129 struct ocfs2_insert_type *insert)
4130{
4131 int ret, subtree_index;
4132 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004133
Mark Fashehdcd05382007-01-16 11:32:23 -08004134 if (left_path) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004135 /*
4136 * There's a chance that left_path got passed back to
4137 * us without being accounted for in the
4138 * journal. Extend our transaction here to be sure we
4139 * can change those blocks.
4140 */
Tao Mac901fb02010-04-26 14:34:57 +08004141 ret = ocfs2_extend_trans(handle, left_path->p_tree_depth);
Mark Fashehdcd05382007-01-16 11:32:23 -08004142 if (ret < 0) {
4143 mlog_errno(ret);
4144 goto out;
4145 }
4146
Joel Becker7dc02802009-02-12 19:20:13 -08004147 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004148 if (ret < 0) {
4149 mlog_errno(ret);
4150 goto out;
4151 }
4152 }
4153
Mark Fashehe8aed342007-12-03 16:43:01 -08004154 /*
4155 * Pass both paths to the journal. The majority of inserts
4156 * will be touching all components anyway.
4157 */
Joel Becker7dc02802009-02-12 19:20:13 -08004158 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004159 if (ret < 0) {
4160 mlog_errno(ret);
4161 goto out;
4162 }
4163
Mark Fasheh328d5752007-06-18 10:48:04 -07004164 if (insert->ins_split != SPLIT_NONE) {
4165 /*
4166 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004167 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004168 * function sort it all out.
4169 */
Joel Beckerc38e52b2009-02-13 02:56:23 -08004170 ocfs2_split_record(et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004171 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004172
4173 /*
4174 * Split might have modified either leaf and we don't
4175 * have a guarantee that the later edge insert will
4176 * dirty this for us.
4177 */
4178 if (left_path)
Joel Beckerec20cec2010-03-19 14:13:52 -07004179 ocfs2_journal_dirty(handle,
4180 path_leaf_bh(left_path));
Mark Fasheh328d5752007-06-18 10:48:04 -07004181 } else
Joel Beckerd5628622009-02-13 02:54:36 -08004182 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4183 insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004184
Joel Beckerec20cec2010-03-19 14:13:52 -07004185 ocfs2_journal_dirty(handle, leaf_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004186
4187 if (left_path) {
4188 /*
4189 * The rotate code has indicated that we need to fix
4190 * up portions of the tree after the insert.
4191 *
4192 * XXX: Should we extend the transaction here?
4193 */
Joel Becker7dc02802009-02-12 19:20:13 -08004194 subtree_index = ocfs2_find_subtree_root(et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004195 right_path);
Joel Becker4619c732009-02-12 19:02:36 -08004196 ocfs2_complete_edge_insert(handle, left_path, right_path,
4197 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08004198 }
4199
4200 ret = 0;
4201out:
4202 return ret;
4203}
4204
Joel Becker3505bec2009-02-13 02:57:58 -08004205static int ocfs2_do_insert_extent(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004206 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004207 struct ocfs2_extent_rec *insert_rec,
4208 struct ocfs2_insert_type *type)
4209{
4210 int ret, rotate = 0;
4211 u32 cpos;
4212 struct ocfs2_path *right_path = NULL;
4213 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004214 struct ocfs2_extent_list *el;
4215
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004216 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004217
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004218 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004219 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004220 if (ret) {
4221 mlog_errno(ret);
4222 goto out;
4223 }
4224
4225 if (le16_to_cpu(el->l_tree_depth) == 0) {
Joel Beckerd5628622009-02-13 02:54:36 -08004226 ocfs2_insert_at_leaf(et, insert_rec, el, type);
Mark Fashehdcd05382007-01-16 11:32:23 -08004227 goto out_update_clusters;
4228 }
4229
Joel Beckerffdd7a52008-10-17 22:32:01 -07004230 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004231 if (!right_path) {
4232 ret = -ENOMEM;
4233 mlog_errno(ret);
4234 goto out;
4235 }
4236
4237 /*
4238 * Determine the path to start with. Rotations need the
4239 * rightmost path, everything else can go directly to the
4240 * target leaf.
4241 */
4242 cpos = le32_to_cpu(insert_rec->e_cpos);
4243 if (type->ins_appending == APPEND_NONE &&
4244 type->ins_contig == CONTIG_NONE) {
4245 rotate = 1;
4246 cpos = UINT_MAX;
4247 }
4248
Joel Beckerfacdb772009-02-12 18:08:48 -08004249 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004250 if (ret) {
4251 mlog_errno(ret);
4252 goto out;
4253 }
4254
4255 /*
4256 * Rotations and appends need special treatment - they modify
4257 * parts of the tree's above them.
4258 *
4259 * Both might pass back a path immediate to the left of the
4260 * one being inserted to. This will be cause
4261 * ocfs2_insert_path() to modify the rightmost records of
4262 * left_path to account for an edge insert.
4263 *
4264 * XXX: When modifying this code, keep in mind that an insert
4265 * can wind up skipping both of these two special cases...
4266 */
4267 if (rotate) {
Joel Becker1bbf0b82009-02-12 19:42:08 -08004268 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004269 le32_to_cpu(insert_rec->e_cpos),
4270 right_path, &left_path);
4271 if (ret) {
4272 mlog_errno(ret);
4273 goto out;
4274 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004275
4276 /*
4277 * ocfs2_rotate_tree_right() might have extended the
4278 * transaction without re-journaling our tree root.
4279 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004280 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004281 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004282 if (ret) {
4283 mlog_errno(ret);
4284 goto out;
4285 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004286 } else if (type->ins_appending == APPEND_TAIL
4287 && type->ins_contig != CONTIG_LEFT) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004288 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08004289 right_path, &left_path);
4290 if (ret) {
4291 mlog_errno(ret);
4292 goto out;
4293 }
4294 }
4295
Joel Becker3505bec2009-02-13 02:57:58 -08004296 ret = ocfs2_insert_path(handle, et, left_path, right_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004297 insert_rec, type);
4298 if (ret) {
4299 mlog_errno(ret);
4300 goto out;
4301 }
4302
4303out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004304 if (type->ins_split == SPLIT_NONE)
Joel Becker6136ca52009-02-12 19:32:43 -08004305 ocfs2_et_update_clusters(et,
Joel Becker35dc0aa2008-08-20 16:25:06 -07004306 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004307
Joel Beckerec20cec2010-03-19 14:13:52 -07004308 ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004309
4310out:
4311 ocfs2_free_path(left_path);
4312 ocfs2_free_path(right_path);
4313
4314 return ret;
4315}
4316
Mark Fasheh328d5752007-06-18 10:48:04 -07004317static enum ocfs2_contig_type
Joel Beckera2970292009-02-13 03:09:54 -08004318ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4319 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004320 struct ocfs2_extent_list *el, int index,
4321 struct ocfs2_extent_rec *split_rec)
4322{
Tao Maad5a4d72008-01-30 14:21:32 +08004323 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004324 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004325 u32 left_cpos, right_cpos;
4326 struct ocfs2_extent_rec *rec = NULL;
4327 struct ocfs2_extent_list *new_el;
4328 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4329 struct buffer_head *bh;
4330 struct ocfs2_extent_block *eb;
Joel Beckera2970292009-02-13 03:09:54 -08004331 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Tao Maad5a4d72008-01-30 14:21:32 +08004332
4333 if (index > 0) {
4334 rec = &el->l_recs[index - 1];
4335 } else if (path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004336 status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004337 if (status)
4338 goto out;
4339
4340 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004341 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004342 if (!left_path)
4343 goto out;
4344
Joel Beckera2970292009-02-13 03:09:54 -08004345 status = ocfs2_find_path(et->et_ci, left_path,
4346 left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004347 if (status)
4348 goto out;
4349
4350 new_el = path_leaf_el(left_path);
4351
4352 if (le16_to_cpu(new_el->l_next_free_rec) !=
4353 le16_to_cpu(new_el->l_count)) {
4354 bh = path_leaf_bh(left_path);
4355 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004356 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004357 "Extent block #%llu has an "
4358 "invalid l_next_free_rec of "
4359 "%d. It should have "
4360 "matched the l_count of %d",
4361 (unsigned long long)le64_to_cpu(eb->h_blkno),
4362 le16_to_cpu(new_el->l_next_free_rec),
4363 le16_to_cpu(new_el->l_count));
4364 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004365 goto out;
4366 }
4367 rec = &new_el->l_recs[
4368 le16_to_cpu(new_el->l_next_free_rec) - 1];
4369 }
4370 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004371
4372 /*
4373 * We're careful to check for an empty extent record here -
4374 * the merge code will know what to do if it sees one.
4375 */
Tao Maad5a4d72008-01-30 14:21:32 +08004376 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004377 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4378 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4379 ret = CONTIG_RIGHT;
4380 } else {
Tao Ma853a3a12009-08-18 11:22:18 +08004381 ret = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004382 }
4383 }
4384
Tao Maad5a4d72008-01-30 14:21:32 +08004385 rec = NULL;
4386 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4387 rec = &el->l_recs[index + 1];
4388 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4389 path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004390 status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004391 if (status)
4392 goto out;
4393
4394 if (right_cpos == 0)
4395 goto out;
4396
Joel Beckerffdd7a52008-10-17 22:32:01 -07004397 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004398 if (!right_path)
4399 goto out;
4400
Joel Beckera2970292009-02-13 03:09:54 -08004401 status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004402 if (status)
4403 goto out;
4404
4405 new_el = path_leaf_el(right_path);
4406 rec = &new_el->l_recs[0];
4407 if (ocfs2_is_empty_extent(rec)) {
4408 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4409 bh = path_leaf_bh(right_path);
4410 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004411 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004412 "Extent block #%llu has an "
4413 "invalid l_next_free_rec of %d",
4414 (unsigned long long)le64_to_cpu(eb->h_blkno),
4415 le16_to_cpu(new_el->l_next_free_rec));
4416 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004417 goto out;
4418 }
4419 rec = &new_el->l_recs[1];
4420 }
4421 }
4422
4423 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004424 enum ocfs2_contig_type contig_type;
4425
Tao Ma853a3a12009-08-18 11:22:18 +08004426 contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004427
4428 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4429 ret = CONTIG_LEFTRIGHT;
4430 else if (ret == CONTIG_NONE)
4431 ret = contig_type;
4432 }
4433
Tao Maad5a4d72008-01-30 14:21:32 +08004434out:
4435 if (left_path)
4436 ocfs2_free_path(left_path);
4437 if (right_path)
4438 ocfs2_free_path(right_path);
4439
Mark Fasheh328d5752007-06-18 10:48:04 -07004440 return ret;
4441}
4442
Joel Becker1ef61b32009-02-13 03:12:33 -08004443static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004444 struct ocfs2_insert_type *insert,
4445 struct ocfs2_extent_list *el,
Joel Becker1ef61b32009-02-13 03:12:33 -08004446 struct ocfs2_extent_rec *insert_rec)
Mark Fashehdcd05382007-01-16 11:32:23 -08004447{
4448 int i;
4449 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4450
Mark Fashehe48edee2007-03-07 16:46:57 -08004451 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4452
Mark Fashehdcd05382007-01-16 11:32:23 -08004453 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
Tao Ma853a3a12009-08-18 11:22:18 +08004454 contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4455 insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004456 if (contig_type != CONTIG_NONE) {
4457 insert->ins_contig_index = i;
4458 break;
4459 }
4460 }
4461 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004462
4463 if (insert->ins_contig != CONTIG_NONE) {
4464 struct ocfs2_extent_rec *rec =
4465 &el->l_recs[insert->ins_contig_index];
4466 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4467 le16_to_cpu(insert_rec->e_leaf_clusters);
4468
4469 /*
4470 * Caller might want us to limit the size of extents, don't
4471 * calculate contiguousness if we might exceed that limit.
4472 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004473 if (et->et_max_leaf_clusters &&
4474 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004475 insert->ins_contig = CONTIG_NONE;
4476 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004477}
4478
4479/*
4480 * This should only be called against the righmost leaf extent list.
4481 *
4482 * ocfs2_figure_appending_type() will figure out whether we'll have to
4483 * insert at the tail of the rightmost leaf.
4484 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004485 * This should also work against the root extent list for tree's with 0
4486 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004487 * then the logic here makes sense.
4488 */
4489static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4490 struct ocfs2_extent_list *el,
4491 struct ocfs2_extent_rec *insert_rec)
4492{
4493 int i;
4494 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4495 struct ocfs2_extent_rec *rec;
4496
4497 insert->ins_appending = APPEND_NONE;
4498
Mark Fashehe48edee2007-03-07 16:46:57 -08004499 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004500
4501 if (!el->l_next_free_rec)
4502 goto set_tail_append;
4503
4504 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4505 /* Were all records empty? */
4506 if (le16_to_cpu(el->l_next_free_rec) == 1)
4507 goto set_tail_append;
4508 }
4509
4510 i = le16_to_cpu(el->l_next_free_rec) - 1;
4511 rec = &el->l_recs[i];
4512
Mark Fashehe48edee2007-03-07 16:46:57 -08004513 if (cpos >=
4514 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004515 goto set_tail_append;
4516
4517 return;
4518
4519set_tail_append:
4520 insert->ins_appending = APPEND_TAIL;
4521}
4522
4523/*
4524 * Helper function called at the begining of an insert.
4525 *
4526 * This computes a few things that are commonly used in the process of
4527 * inserting into the btree:
4528 * - Whether the new extent is contiguous with an existing one.
4529 * - The current tree depth.
4530 * - Whether the insert is an appending one.
4531 * - The total # of free records in the tree.
4532 *
4533 * All of the information is stored on the ocfs2_insert_type
4534 * structure.
4535 */
Joel Becker627961b2009-02-13 03:14:38 -08004536static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004537 struct buffer_head **last_eb_bh,
4538 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004539 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004540 struct ocfs2_insert_type *insert)
4541{
4542 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004543 struct ocfs2_extent_block *eb;
4544 struct ocfs2_extent_list *el;
4545 struct ocfs2_path *path = NULL;
4546 struct buffer_head *bh = NULL;
4547
Mark Fasheh328d5752007-06-18 10:48:04 -07004548 insert->ins_split = SPLIT_NONE;
4549
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004550 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004551 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4552
4553 if (el->l_tree_depth) {
4554 /*
4555 * If we have tree depth, we read in the
4556 * rightmost extent block ahead of time as
4557 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4558 * may want it later.
4559 */
Joel Becker3d03a302009-02-12 17:49:26 -08004560 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08004561 ocfs2_et_get_last_eb_blk(et),
4562 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004563 if (ret) {
4564 mlog_exit(ret);
4565 goto out;
4566 }
4567 eb = (struct ocfs2_extent_block *) bh->b_data;
4568 el = &eb->h_list;
4569 }
4570
4571 /*
4572 * Unless we have a contiguous insert, we'll need to know if
4573 * there is room left in our allocation tree for another
4574 * extent record.
4575 *
4576 * XXX: This test is simplistic, we can search for empty
4577 * extent records too.
4578 */
Tao Maoc77534f2007-08-28 17:22:33 -07004579 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004580 le16_to_cpu(el->l_next_free_rec);
4581
4582 if (!insert->ins_tree_depth) {
Joel Becker1ef61b32009-02-13 03:12:33 -08004583 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004584 ocfs2_figure_appending_type(insert, el, insert_rec);
4585 return 0;
4586 }
4587
Joel Beckerffdd7a52008-10-17 22:32:01 -07004588 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004589 if (!path) {
4590 ret = -ENOMEM;
4591 mlog_errno(ret);
4592 goto out;
4593 }
4594
4595 /*
4596 * In the case that we're inserting past what the tree
4597 * currently accounts for, ocfs2_find_path() will return for
4598 * us the rightmost tree path. This is accounted for below in
4599 * the appending code.
4600 */
Joel Beckerfacdb772009-02-12 18:08:48 -08004601 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -08004602 if (ret) {
4603 mlog_errno(ret);
4604 goto out;
4605 }
4606
4607 el = path_leaf_el(path);
4608
4609 /*
4610 * Now that we have the path, there's two things we want to determine:
4611 * 1) Contiguousness (also set contig_index if this is so)
4612 *
4613 * 2) Are we doing an append? We can trivially break this up
4614 * into two types of appends: simple record append, or a
4615 * rotate inside the tail leaf.
4616 */
Joel Becker1ef61b32009-02-13 03:12:33 -08004617 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004618
4619 /*
4620 * The insert code isn't quite ready to deal with all cases of
4621 * left contiguousness. Specifically, if it's an insert into
4622 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004623 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004624 * left. For now, just catch that case and fool the layers
4625 * above us. This works just fine for tree_depth == 0, which
4626 * is why we allow that above.
4627 */
4628 if (insert->ins_contig == CONTIG_LEFT &&
4629 insert->ins_contig_index == 0)
4630 insert->ins_contig = CONTIG_NONE;
4631
4632 /*
4633 * Ok, so we can simply compare against last_eb to figure out
4634 * whether the path doesn't exist. This will only happen in
4635 * the case that we're doing a tail append, so maybe we can
4636 * take advantage of that information somehow.
4637 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004638 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004639 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004640 /*
4641 * Ok, ocfs2_find_path() returned us the rightmost
4642 * tree path. This might be an appending insert. There are
4643 * two cases:
4644 * 1) We're doing a true append at the tail:
4645 * -This might even be off the end of the leaf
4646 * 2) We're "appending" by rotating in the tail
4647 */
4648 ocfs2_figure_appending_type(insert, el, insert_rec);
4649 }
4650
4651out:
4652 ocfs2_free_path(path);
4653
4654 if (ret == 0)
4655 *last_eb_bh = bh;
4656 else
4657 brelse(bh);
4658 return ret;
4659}
4660
4661/*
Joel Beckercc79d8c2009-02-13 03:24:43 -08004662 * Insert an extent into a btree.
Mark Fashehdcd05382007-01-16 11:32:23 -08004663 *
Joel Beckercc79d8c2009-02-13 03:24:43 -08004664 * The caller needs to update the owning btree's cluster count.
Mark Fashehdcd05382007-01-16 11:32:23 -08004665 */
Joel Beckercc79d8c2009-02-13 03:24:43 -08004666int ocfs2_insert_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004667 struct ocfs2_extent_tree *et,
4668 u32 cpos,
4669 u64 start_blk,
4670 u32 new_clusters,
4671 u8 flags,
4672 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004673{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004674 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004675 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004676 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004677 struct ocfs2_insert_type insert = {0, };
4678 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004679
Joel Beckercc79d8c2009-02-13 03:24:43 -08004680 mlog(0, "add %u clusters at position %u to owner %llu\n",
4681 new_clusters, cpos,
4682 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08004683
Mark Fashehe48edee2007-03-07 16:46:57 -08004684 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004685 rec.e_cpos = cpu_to_le32(cpos);
4686 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004687 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004688 rec.e_flags = flags;
Joel Becker6136ca52009-02-12 19:32:43 -08004689 status = ocfs2_et_insert_check(et, &rec);
Joel Becker1e61ee72008-08-20 18:32:45 -07004690 if (status) {
4691 mlog_errno(status);
4692 goto bail;
4693 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004694
Joel Becker627961b2009-02-13 03:14:38 -08004695 status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004696 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004697 if (status < 0) {
4698 mlog_errno(status);
4699 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004700 }
4701
Mark Fashehdcd05382007-01-16 11:32:23 -08004702 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4703 "Insert.contig_index: %d, Insert.free_records: %d, "
4704 "Insert.tree_depth: %d\n",
4705 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004706 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004707
Tao Maoc77534f2007-08-28 17:22:33 -07004708 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004709 status = ocfs2_grow_tree(handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004710 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004711 meta_ac);
4712 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004713 mlog_errno(status);
4714 goto bail;
4715 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004716 }
4717
Mark Fashehdcd05382007-01-16 11:32:23 -08004718 /* Finally, we can add clusters. This might rotate the tree for us. */
Joel Becker3505bec2009-02-13 02:57:58 -08004719 status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004720 if (status < 0)
4721 mlog_errno(status);
Joel Becker92ba4702009-02-13 03:18:34 -08004722 else
4723 ocfs2_et_extent_map_insert(et, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004724
4725bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004726 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004727
Tao Maf56654c2008-08-18 17:38:48 +08004728 mlog_exit(status);
4729 return status;
4730}
4731
Tao Ma0eb8d472008-08-18 17:38:45 +08004732/*
4733 * Allcate and add clusters into the extent b-tree.
4734 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004735 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004736 * it is not limited to the file storage. Any extent tree can use this
4737 * function if it implements the proper ocfs2_extent_tree.
4738 */
Joel Beckercbee7e12009-02-13 03:34:15 -08004739int ocfs2_add_clusters_in_btree(handle_t *handle,
4740 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004741 u32 *logical_offset,
4742 u32 clusters_to_add,
4743 int mark_unwritten,
Tao Ma0eb8d472008-08-18 17:38:45 +08004744 struct ocfs2_alloc_context *data_ac,
4745 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004746 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004747{
4748 int status = 0;
4749 int free_extents;
4750 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4751 u32 bit_off, num_bits;
4752 u64 block;
4753 u8 flags = 0;
Joel Beckercbee7e12009-02-13 03:34:15 -08004754 struct ocfs2_super *osb =
4755 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Tao Ma0eb8d472008-08-18 17:38:45 +08004756
4757 BUG_ON(!clusters_to_add);
4758
4759 if (mark_unwritten)
4760 flags = OCFS2_EXT_UNWRITTEN;
4761
Joel Becker3d03a302009-02-12 17:49:26 -08004762 free_extents = ocfs2_num_free_extents(osb, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004763 if (free_extents < 0) {
4764 status = free_extents;
4765 mlog_errno(status);
4766 goto leave;
4767 }
4768
4769 /* there are two cases which could cause us to EAGAIN in the
4770 * we-need-more-metadata case:
4771 * 1) we haven't reserved *any*
4772 * 2) we are so fragmented, we've needed to add metadata too
4773 * many times. */
4774 if (!free_extents && !meta_ac) {
4775 mlog(0, "we haven't reserved any metadata!\n");
4776 status = -EAGAIN;
4777 reason = RESTART_META;
4778 goto leave;
4779 } else if ((!free_extents)
4780 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004781 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004782 mlog(0, "filesystem is really fragmented...\n");
4783 status = -EAGAIN;
4784 reason = RESTART_META;
4785 goto leave;
4786 }
4787
Joel Becker1ed9b772010-05-06 13:59:06 +08004788 status = __ocfs2_claim_clusters(handle, data_ac, 1,
Tao Ma0eb8d472008-08-18 17:38:45 +08004789 clusters_to_add, &bit_off, &num_bits);
4790 if (status < 0) {
4791 if (status != -ENOSPC)
4792 mlog_errno(status);
4793 goto leave;
4794 }
4795
4796 BUG_ON(num_bits > clusters_to_add);
4797
Joel Becker13723d02008-10-17 19:25:01 -07004798 /* reserve our write early -- insert_extent may update the tree root */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004799 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004800 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d472008-08-18 17:38:45 +08004801 if (status < 0) {
4802 mlog_errno(status);
4803 goto leave;
4804 }
4805
4806 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Joel Beckercbee7e12009-02-13 03:34:15 -08004807 mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
4808 num_bits, bit_off,
4809 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Joel Beckercc79d8c2009-02-13 03:24:43 -08004810 status = ocfs2_insert_extent(handle, et, *logical_offset, block,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004811 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004812 if (status < 0) {
4813 mlog_errno(status);
4814 goto leave;
4815 }
4816
Joel Beckerec20cec2010-03-19 14:13:52 -07004817 ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004818
4819 clusters_to_add -= num_bits;
4820 *logical_offset += num_bits;
4821
4822 if (clusters_to_add) {
4823 mlog(0, "need to alloc once more, wanted = %u\n",
4824 clusters_to_add);
4825 status = -EAGAIN;
4826 reason = RESTART_TRANS;
4827 }
4828
4829leave:
4830 mlog_exit(status);
4831 if (reason_ret)
4832 *reason_ret = reason;
4833 return status;
4834}
4835
Mark Fasheh328d5752007-06-18 10:48:04 -07004836static void ocfs2_make_right_split_rec(struct super_block *sb,
4837 struct ocfs2_extent_rec *split_rec,
4838 u32 cpos,
4839 struct ocfs2_extent_rec *rec)
4840{
4841 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4842 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4843
4844 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4845
4846 split_rec->e_cpos = cpu_to_le32(cpos);
4847 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4848
4849 split_rec->e_blkno = rec->e_blkno;
4850 le64_add_cpu(&split_rec->e_blkno,
4851 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4852
4853 split_rec->e_flags = rec->e_flags;
4854}
4855
Joel Beckerd2311292009-02-13 03:43:22 -08004856static int ocfs2_split_and_insert(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004857 struct ocfs2_extent_tree *et,
Joel Beckerd2311292009-02-13 03:43:22 -08004858 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004859 struct buffer_head **last_eb_bh,
4860 int split_index,
4861 struct ocfs2_extent_rec *orig_split_rec,
4862 struct ocfs2_alloc_context *meta_ac)
4863{
4864 int ret = 0, depth;
4865 unsigned int insert_range, rec_range, do_leftright = 0;
4866 struct ocfs2_extent_rec tmprec;
4867 struct ocfs2_extent_list *rightmost_el;
4868 struct ocfs2_extent_rec rec;
4869 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4870 struct ocfs2_insert_type insert;
4871 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004872
4873leftright:
4874 /*
4875 * Store a copy of the record on the stack - it might move
4876 * around as the tree is manipulated below.
4877 */
4878 rec = path_leaf_el(path)->l_recs[split_index];
4879
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004880 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004881
4882 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4883 if (depth) {
4884 BUG_ON(!(*last_eb_bh));
4885 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4886 rightmost_el = &eb->h_list;
4887 }
4888
4889 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4890 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004891 ret = ocfs2_grow_tree(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08004892 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004893 if (ret) {
4894 mlog_errno(ret);
4895 goto out;
4896 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004897 }
4898
4899 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4900 insert.ins_appending = APPEND_NONE;
4901 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004902 insert.ins_tree_depth = depth;
4903
4904 insert_range = le32_to_cpu(split_rec.e_cpos) +
4905 le16_to_cpu(split_rec.e_leaf_clusters);
4906 rec_range = le32_to_cpu(rec.e_cpos) +
4907 le16_to_cpu(rec.e_leaf_clusters);
4908
4909 if (split_rec.e_cpos == rec.e_cpos) {
4910 insert.ins_split = SPLIT_LEFT;
4911 } else if (insert_range == rec_range) {
4912 insert.ins_split = SPLIT_RIGHT;
4913 } else {
4914 /*
4915 * Left/right split. We fake this as a right split
4916 * first and then make a second pass as a left split.
4917 */
4918 insert.ins_split = SPLIT_RIGHT;
4919
Joel Beckerd2311292009-02-13 03:43:22 -08004920 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4921 &tmprec, insert_range, &rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004922
4923 split_rec = tmprec;
4924
4925 BUG_ON(do_leftright);
4926 do_leftright = 1;
4927 }
4928
Joel Becker3505bec2009-02-13 02:57:58 -08004929 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004930 if (ret) {
4931 mlog_errno(ret);
4932 goto out;
4933 }
4934
4935 if (do_leftright == 1) {
4936 u32 cpos;
4937 struct ocfs2_extent_list *el;
4938
4939 do_leftright++;
4940 split_rec = *orig_split_rec;
4941
4942 ocfs2_reinit_path(path, 1);
4943
4944 cpos = le32_to_cpu(split_rec.e_cpos);
Joel Beckerfacdb772009-02-12 18:08:48 -08004945 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07004946 if (ret) {
4947 mlog_errno(ret);
4948 goto out;
4949 }
4950
4951 el = path_leaf_el(path);
4952 split_index = ocfs2_search_extent_list(el, cpos);
4953 goto leftright;
4954 }
4955out:
4956
4957 return ret;
4958}
4959
Joel Beckerf3868d02009-02-17 19:46:04 -08004960static int ocfs2_replace_extent_rec(handle_t *handle,
4961 struct ocfs2_extent_tree *et,
Tao Ma47be12e2009-01-09 07:32:48 +08004962 struct ocfs2_path *path,
4963 struct ocfs2_extent_list *el,
4964 int split_index,
4965 struct ocfs2_extent_rec *split_rec)
4966{
4967 int ret;
4968
Joel Beckerf3868d02009-02-17 19:46:04 -08004969 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Tao Ma47be12e2009-01-09 07:32:48 +08004970 path_num_items(path) - 1);
4971 if (ret) {
4972 mlog_errno(ret);
4973 goto out;
4974 }
4975
4976 el->l_recs[split_index] = *split_rec;
4977
4978 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4979out:
4980 return ret;
4981}
4982
Mark Fasheh328d5752007-06-18 10:48:04 -07004983/*
Tao Ma555936b2009-08-18 11:22:21 +08004984 * Split part or all of the extent record at split_index in the leaf
4985 * pointed to by path. Merge with the contiguous extent record if needed.
Mark Fasheh328d5752007-06-18 10:48:04 -07004986 *
4987 * Care is taken to handle contiguousness so as to not grow the tree.
4988 *
4989 * meta_ac is not strictly necessary - we only truly need it if growth
4990 * of the tree is required. All other cases will degrade into a less
4991 * optimal tree layout.
4992 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004993 * last_eb_bh should be the rightmost leaf block for any extent
4994 * btree. Since a split may grow the tree or a merge might shrink it,
4995 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07004996 *
4997 * This code is optimized for readability - several passes might be
4998 * made over certain portions of the tree. All of those blocks will
4999 * have been brought into cache (and pinned via the journal), so the
5000 * extra overhead is not expressed in terms of disk reads.
5001 */
Tao Mae2e9f602009-08-18 11:22:34 +08005002int ocfs2_split_extent(handle_t *handle,
5003 struct ocfs2_extent_tree *et,
5004 struct ocfs2_path *path,
5005 int split_index,
5006 struct ocfs2_extent_rec *split_rec,
5007 struct ocfs2_alloc_context *meta_ac,
5008 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005009{
5010 int ret = 0;
5011 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08005012 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07005013 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5014 struct ocfs2_merge_ctxt ctxt;
5015 struct ocfs2_extent_list *rightmost_el;
5016
Mark Fasheh328d5752007-06-18 10:48:04 -07005017 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5018 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5019 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5020 ret = -EIO;
5021 mlog_errno(ret);
5022 goto out;
5023 }
5024
Joel Beckera2970292009-02-13 03:09:54 -08005025 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005026 split_index,
5027 split_rec);
5028
5029 /*
5030 * The core merge / split code wants to know how much room is
Joel Beckera1cf0762009-02-13 03:45:49 -08005031 * left in this allocation tree, so we pass the
Mark Fasheh328d5752007-06-18 10:48:04 -07005032 * rightmost extent list.
5033 */
5034 if (path->p_tree_depth) {
5035 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005036
Joel Becker3d03a302009-02-12 17:49:26 -08005037 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005038 ocfs2_et_get_last_eb_blk(et),
5039 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005040 if (ret) {
5041 mlog_exit(ret);
5042 goto out;
5043 }
5044
5045 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005046 rightmost_el = &eb->h_list;
5047 } else
5048 rightmost_el = path_root_el(path);
5049
Mark Fasheh328d5752007-06-18 10:48:04 -07005050 if (rec->e_cpos == split_rec->e_cpos &&
5051 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5052 ctxt.c_split_covers_rec = 1;
5053 else
5054 ctxt.c_split_covers_rec = 0;
5055
5056 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5057
Mark Fasheh015452b2007-09-12 10:21:22 -07005058 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5059 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5060 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005061
5062 if (ctxt.c_contig_type == CONTIG_NONE) {
5063 if (ctxt.c_split_covers_rec)
Joel Beckerf3868d02009-02-17 19:46:04 -08005064 ret = ocfs2_replace_extent_rec(handle, et, path, el,
Tao Ma47be12e2009-01-09 07:32:48 +08005065 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005066 else
Joel Beckerd2311292009-02-13 03:43:22 -08005067 ret = ocfs2_split_and_insert(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005068 &last_eb_bh, split_index,
5069 split_rec, meta_ac);
5070 if (ret)
5071 mlog_errno(ret);
5072 } else {
Joel Beckerc495dd22009-02-13 02:19:11 -08005073 ret = ocfs2_try_to_merge_extent(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005074 split_index, split_rec,
Joel Beckerc495dd22009-02-13 02:19:11 -08005075 dealloc, &ctxt);
Mark Fasheh328d5752007-06-18 10:48:04 -07005076 if (ret)
5077 mlog_errno(ret);
5078 }
5079
Mark Fasheh328d5752007-06-18 10:48:04 -07005080out:
5081 brelse(last_eb_bh);
5082 return ret;
5083}
5084
5085/*
Tao Ma555936b2009-08-18 11:22:21 +08005086 * Change the flags of the already-existing extent at cpos for len clusters.
5087 *
5088 * new_flags: the flags we want to set.
5089 * clear_flags: the flags we want to clear.
5090 * phys: the new physical offset we want this new extent starts from.
Mark Fasheh328d5752007-06-18 10:48:04 -07005091 *
5092 * If the existing extent is larger than the request, initiate a
5093 * split. An attempt will be made at merging with adjacent extents.
5094 *
5095 * The caller is responsible for passing down meta_ac if we'll need it.
5096 */
Tao Ma1aa75fe2009-08-18 11:28:39 +08005097int ocfs2_change_extent_flag(handle_t *handle,
5098 struct ocfs2_extent_tree *et,
5099 u32 cpos, u32 len, u32 phys,
5100 struct ocfs2_alloc_context *meta_ac,
5101 struct ocfs2_cached_dealloc_ctxt *dealloc,
5102 int new_flags, int clear_flags)
Mark Fasheh328d5752007-06-18 10:48:04 -07005103{
5104 int ret, index;
Tao Ma555936b2009-08-18 11:22:21 +08005105 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5106 u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
Mark Fasheh328d5752007-06-18 10:48:04 -07005107 struct ocfs2_extent_rec split_rec;
5108 struct ocfs2_path *left_path = NULL;
5109 struct ocfs2_extent_list *el;
Tao Ma555936b2009-08-18 11:22:21 +08005110 struct ocfs2_extent_rec *rec;
Mark Fasheh328d5752007-06-18 10:48:04 -07005111
Joel Beckerffdd7a52008-10-17 22:32:01 -07005112 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005113 if (!left_path) {
5114 ret = -ENOMEM;
5115 mlog_errno(ret);
5116 goto out;
5117 }
5118
Joel Beckerfacdb772009-02-12 18:08:48 -08005119 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005120 if (ret) {
5121 mlog_errno(ret);
5122 goto out;
5123 }
5124 el = path_leaf_el(left_path);
5125
5126 index = ocfs2_search_extent_list(el, cpos);
5127 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Tao Ma555936b2009-08-18 11:22:21 +08005128 ocfs2_error(sb,
5129 "Owner %llu has an extent at cpos %u which can no "
Mark Fasheh328d5752007-06-18 10:48:04 -07005130 "longer be found.\n",
Tao Ma555936b2009-08-18 11:22:21 +08005131 (unsigned long long)
5132 ocfs2_metadata_cache_owner(et->et_ci), cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005133 ret = -EROFS;
5134 goto out;
5135 }
5136
Tao Ma555936b2009-08-18 11:22:21 +08005137 ret = -EIO;
5138 rec = &el->l_recs[index];
5139 if (new_flags && (rec->e_flags & new_flags)) {
5140 mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
5141 "extent that already had them",
5142 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5143 new_flags);
5144 goto out;
5145 }
5146
5147 if (clear_flags && !(rec->e_flags & clear_flags)) {
5148 mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
5149 "extent that didn't have them",
5150 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5151 clear_flags);
5152 goto out;
5153 }
5154
Mark Fasheh328d5752007-06-18 10:48:04 -07005155 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5156 split_rec.e_cpos = cpu_to_le32(cpos);
5157 split_rec.e_leaf_clusters = cpu_to_le16(len);
5158 split_rec.e_blkno = cpu_to_le64(start_blkno);
Tao Ma555936b2009-08-18 11:22:21 +08005159 split_rec.e_flags = rec->e_flags;
5160 if (new_flags)
5161 split_rec.e_flags |= new_flags;
5162 if (clear_flags)
5163 split_rec.e_flags &= ~clear_flags;
Mark Fasheh328d5752007-06-18 10:48:04 -07005164
Tao Mae2e9f602009-08-18 11:22:34 +08005165 ret = ocfs2_split_extent(handle, et, left_path,
5166 index, &split_rec, meta_ac,
5167 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005168 if (ret)
5169 mlog_errno(ret);
5170
5171out:
5172 ocfs2_free_path(left_path);
5173 return ret;
Tao Ma555936b2009-08-18 11:22:21 +08005174
5175}
5176
5177/*
5178 * Mark the already-existing extent at cpos as written for len clusters.
5179 * This removes the unwritten extent flag.
5180 *
5181 * If the existing extent is larger than the request, initiate a
5182 * split. An attempt will be made at merging with adjacent extents.
5183 *
5184 * The caller is responsible for passing down meta_ac if we'll need it.
5185 */
5186int ocfs2_mark_extent_written(struct inode *inode,
5187 struct ocfs2_extent_tree *et,
5188 handle_t *handle, u32 cpos, u32 len, u32 phys,
5189 struct ocfs2_alloc_context *meta_ac,
5190 struct ocfs2_cached_dealloc_ctxt *dealloc)
5191{
5192 int ret;
5193
5194 mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
5195 inode->i_ino, cpos, len, phys);
5196
5197 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5198 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5199 "that are being written to, but the feature bit "
5200 "is not set in the super block.",
5201 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5202 ret = -EROFS;
5203 goto out;
5204 }
5205
5206 /*
5207 * XXX: This should be fixed up so that we just re-insert the
5208 * next extent records.
5209 */
5210 ocfs2_et_extent_map_truncate(et, 0);
5211
5212 ret = ocfs2_change_extent_flag(handle, et, cpos,
5213 len, phys, meta_ac, dealloc,
5214 0, OCFS2_EXT_UNWRITTEN);
5215 if (ret)
5216 mlog_errno(ret);
5217
5218out:
5219 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07005220}
5221
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005222static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5223 struct ocfs2_path *path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005224 int index, u32 new_range,
5225 struct ocfs2_alloc_context *meta_ac)
5226{
Tao Mac901fb02010-04-26 14:34:57 +08005227 int ret, depth, credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005228 struct buffer_head *last_eb_bh = NULL;
5229 struct ocfs2_extent_block *eb;
5230 struct ocfs2_extent_list *rightmost_el, *el;
5231 struct ocfs2_extent_rec split_rec;
5232 struct ocfs2_extent_rec *rec;
5233 struct ocfs2_insert_type insert;
5234
5235 /*
5236 * Setup the record to split before we grow the tree.
5237 */
5238 el = path_leaf_el(path);
5239 rec = &el->l_recs[index];
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005240 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5241 &split_rec, new_range, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005242
5243 depth = path->p_tree_depth;
5244 if (depth > 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08005245 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005246 ocfs2_et_get_last_eb_blk(et),
5247 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005248 if (ret < 0) {
5249 mlog_errno(ret);
5250 goto out;
5251 }
5252
5253 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5254 rightmost_el = &eb->h_list;
5255 } else
5256 rightmost_el = path_leaf_el(path);
5257
Tao Mac901fb02010-04-26 14:34:57 +08005258 credits = path->p_tree_depth +
5259 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005260 ret = ocfs2_extend_trans(handle, credits);
5261 if (ret) {
5262 mlog_errno(ret);
5263 goto out;
5264 }
5265
5266 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5267 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08005268 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005269 meta_ac);
5270 if (ret) {
5271 mlog_errno(ret);
5272 goto out;
5273 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005274 }
5275
5276 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5277 insert.ins_appending = APPEND_NONE;
5278 insert.ins_contig = CONTIG_NONE;
5279 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005280 insert.ins_tree_depth = depth;
5281
Joel Becker3505bec2009-02-13 02:57:58 -08005282 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005283 if (ret)
5284 mlog_errno(ret);
5285
5286out:
5287 brelse(last_eb_bh);
5288 return ret;
5289}
5290
Joel Becker043beeb2009-02-13 02:42:30 -08005291static int ocfs2_truncate_rec(handle_t *handle,
5292 struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005293 struct ocfs2_path *path, int index,
5294 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker043beeb2009-02-13 02:42:30 -08005295 u32 cpos, u32 len)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005296{
5297 int ret;
5298 u32 left_cpos, rec_range, trunc_range;
5299 int wants_rotate = 0, is_rightmost_tree_rec = 0;
Joel Becker043beeb2009-02-13 02:42:30 -08005300 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005301 struct ocfs2_path *left_path = NULL;
5302 struct ocfs2_extent_list *el = path_leaf_el(path);
5303 struct ocfs2_extent_rec *rec;
5304 struct ocfs2_extent_block *eb;
5305
5306 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Joel Becker70f18c02009-02-13 02:09:31 -08005307 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005308 if (ret) {
5309 mlog_errno(ret);
5310 goto out;
5311 }
5312
5313 index--;
5314 }
5315
5316 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5317 path->p_tree_depth) {
5318 /*
5319 * Check whether this is the rightmost tree record. If
5320 * we remove all of this record or part of its right
5321 * edge then an update of the record lengths above it
5322 * will be required.
5323 */
5324 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5325 if (eb->h_next_leaf_blk == 0)
5326 is_rightmost_tree_rec = 1;
5327 }
5328
5329 rec = &el->l_recs[index];
5330 if (index == 0 && path->p_tree_depth &&
5331 le32_to_cpu(rec->e_cpos) == cpos) {
5332 /*
5333 * Changing the leftmost offset (via partial or whole
5334 * record truncate) of an interior (or rightmost) path
5335 * means we have to update the subtree that is formed
5336 * by this leaf and the one to it's left.
5337 *
5338 * There are two cases we can skip:
Joel Becker043beeb2009-02-13 02:42:30 -08005339 * 1) Path is the leftmost one in our btree.
Mark Fashehd0c7d702007-07-03 13:27:22 -07005340 * 2) The leaf is rightmost and will be empty after
5341 * we remove the extent record - the rotate code
5342 * knows how to update the newly formed edge.
5343 */
5344
Joel Becker043beeb2009-02-13 02:42:30 -08005345 ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005346 if (ret) {
5347 mlog_errno(ret);
5348 goto out;
5349 }
5350
5351 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005352 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005353 if (!left_path) {
5354 ret = -ENOMEM;
5355 mlog_errno(ret);
5356 goto out;
5357 }
5358
Joel Beckerfacdb772009-02-12 18:08:48 -08005359 ret = ocfs2_find_path(et->et_ci, left_path,
5360 left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005361 if (ret) {
5362 mlog_errno(ret);
5363 goto out;
5364 }
5365 }
5366 }
5367
5368 ret = ocfs2_extend_rotate_transaction(handle, 0,
5369 handle->h_buffer_credits,
5370 path);
5371 if (ret) {
5372 mlog_errno(ret);
5373 goto out;
5374 }
5375
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005376 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005377 if (ret) {
5378 mlog_errno(ret);
5379 goto out;
5380 }
5381
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005382 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005383 if (ret) {
5384 mlog_errno(ret);
5385 goto out;
5386 }
5387
5388 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5389 trunc_range = cpos + len;
5390
5391 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5392 int next_free;
5393
5394 memset(rec, 0, sizeof(*rec));
5395 ocfs2_cleanup_merge(el, index);
5396 wants_rotate = 1;
5397
5398 next_free = le16_to_cpu(el->l_next_free_rec);
5399 if (is_rightmost_tree_rec && next_free > 1) {
5400 /*
5401 * We skip the edge update if this path will
5402 * be deleted by the rotate code.
5403 */
5404 rec = &el->l_recs[next_free - 1];
Joel Beckerd401dc12009-02-13 02:24:10 -08005405 ocfs2_adjust_rightmost_records(handle, et, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005406 rec);
5407 }
5408 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5409 /* Remove leftmost portion of the record. */
5410 le32_add_cpu(&rec->e_cpos, len);
5411 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5412 le16_add_cpu(&rec->e_leaf_clusters, -len);
5413 } else if (rec_range == trunc_range) {
5414 /* Remove rightmost portion of the record */
5415 le16_add_cpu(&rec->e_leaf_clusters, -len);
5416 if (is_rightmost_tree_rec)
Joel Beckerd401dc12009-02-13 02:24:10 -08005417 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005418 } else {
5419 /* Caller should have trapped this. */
Joel Becker043beeb2009-02-13 02:42:30 -08005420 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5421 "(%u, %u)\n",
5422 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005423 le32_to_cpu(rec->e_cpos),
5424 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5425 BUG();
5426 }
5427
5428 if (left_path) {
5429 int subtree_index;
5430
Joel Becker7dc02802009-02-12 19:20:13 -08005431 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Joel Becker4619c732009-02-12 19:02:36 -08005432 ocfs2_complete_edge_insert(handle, left_path, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005433 subtree_index);
5434 }
5435
5436 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5437
Joel Becker70f18c02009-02-13 02:09:31 -08005438 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005439 if (ret) {
5440 mlog_errno(ret);
5441 goto out;
5442 }
5443
5444out:
5445 ocfs2_free_path(left_path);
5446 return ret;
5447}
5448
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005449int ocfs2_remove_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005450 struct ocfs2_extent_tree *et,
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005451 u32 cpos, u32 len,
Mark Fasheh063c4562007-07-03 13:34:11 -07005452 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005453 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005454{
5455 int ret, index;
5456 u32 rec_range, trunc_range;
5457 struct ocfs2_extent_rec *rec;
5458 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005459 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005460
Joel Becker4c911ee2009-02-13 02:50:12 -08005461 /*
5462 * XXX: Why are we truncating to 0 instead of wherever this
5463 * affects us?
5464 */
5465 ocfs2_et_extent_map_truncate(et, 0);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005466
Joel Beckerffdd7a52008-10-17 22:32:01 -07005467 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005468 if (!path) {
5469 ret = -ENOMEM;
5470 mlog_errno(ret);
5471 goto out;
5472 }
5473
Joel Beckerfacdb772009-02-12 18:08:48 -08005474 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005475 if (ret) {
5476 mlog_errno(ret);
5477 goto out;
5478 }
5479
5480 el = path_leaf_el(path);
5481 index = ocfs2_search_extent_list(el, cpos);
5482 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005483 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5484 "Owner %llu has an extent at cpos %u which can no "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005485 "longer be found.\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005486 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5487 cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005488 ret = -EROFS;
5489 goto out;
5490 }
5491
5492 /*
5493 * We have 3 cases of extent removal:
5494 * 1) Range covers the entire extent rec
5495 * 2) Range begins or ends on one edge of the extent rec
5496 * 3) Range is in the middle of the extent rec (no shared edges)
5497 *
5498 * For case 1 we remove the extent rec and left rotate to
5499 * fill the hole.
5500 *
5501 * For case 2 we just shrink the existing extent rec, with a
5502 * tree update if the shrinking edge is also the edge of an
5503 * extent block.
5504 *
5505 * For case 3 we do a right split to turn the extent rec into
5506 * something case 2 can handle.
5507 */
5508 rec = &el->l_recs[index];
5509 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5510 trunc_range = cpos + len;
5511
5512 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5513
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005514 mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005515 "(cpos %u, len %u)\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005516 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5517 cpos, len, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005518 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5519
5520 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
Joel Becker043beeb2009-02-13 02:42:30 -08005521 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5522 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005523 if (ret) {
5524 mlog_errno(ret);
5525 goto out;
5526 }
5527 } else {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005528 ret = ocfs2_split_tree(handle, et, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005529 trunc_range, meta_ac);
5530 if (ret) {
5531 mlog_errno(ret);
5532 goto out;
5533 }
5534
5535 /*
5536 * The split could have manipulated the tree enough to
5537 * move the record location, so we have to look for it again.
5538 */
5539 ocfs2_reinit_path(path, 1);
5540
Joel Beckerfacdb772009-02-12 18:08:48 -08005541 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005542 if (ret) {
5543 mlog_errno(ret);
5544 goto out;
5545 }
5546
5547 el = path_leaf_el(path);
5548 index = ocfs2_search_extent_list(el, cpos);
5549 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005550 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5551 "Owner %llu: split at cpos %u lost record.",
5552 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005553 cpos);
5554 ret = -EROFS;
5555 goto out;
5556 }
5557
5558 /*
5559 * Double check our values here. If anything is fishy,
5560 * it's easier to catch it at the top level.
5561 */
5562 rec = &el->l_recs[index];
5563 rec_range = le32_to_cpu(rec->e_cpos) +
5564 ocfs2_rec_clusters(el, rec);
5565 if (rec_range != trunc_range) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005566 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5567 "Owner %llu: error after split at cpos %u"
Mark Fashehd0c7d702007-07-03 13:27:22 -07005568 "trunc len %u, existing record is (%u,%u)",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005569 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005570 cpos, len, le32_to_cpu(rec->e_cpos),
5571 ocfs2_rec_clusters(el, rec));
5572 ret = -EROFS;
5573 goto out;
5574 }
5575
Joel Becker043beeb2009-02-13 02:42:30 -08005576 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5577 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005578 if (ret) {
5579 mlog_errno(ret);
5580 goto out;
5581 }
5582 }
5583
5584out:
5585 ocfs2_free_path(path);
5586 return ret;
5587}
5588
Mark Fashehfecc0112008-11-12 15:16:38 -08005589int ocfs2_remove_btree_range(struct inode *inode,
5590 struct ocfs2_extent_tree *et,
5591 u32 cpos, u32 phys_cpos, u32 len,
5592 struct ocfs2_cached_dealloc_ctxt *dealloc)
5593{
5594 int ret;
5595 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5596 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5597 struct inode *tl_inode = osb->osb_tl_inode;
5598 handle_t *handle;
5599 struct ocfs2_alloc_context *meta_ac = NULL;
5600
5601 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5602 if (ret) {
5603 mlog_errno(ret);
5604 return ret;
5605 }
5606
5607 mutex_lock(&tl_inode->i_mutex);
5608
5609 if (ocfs2_truncate_log_needs_flush(osb)) {
5610 ret = __ocfs2_flush_truncate_log(osb);
5611 if (ret < 0) {
5612 mlog_errno(ret);
5613 goto out;
5614 }
5615 }
5616
Jan Karaa90714c2008-10-09 19:38:40 +02005617 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005618 if (IS_ERR(handle)) {
5619 ret = PTR_ERR(handle);
5620 mlog_errno(ret);
5621 goto out;
5622 }
5623
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005624 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07005625 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005626 if (ret) {
5627 mlog_errno(ret);
5628 goto out;
5629 }
5630
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005631 dquot_free_space_nodirty(inode,
Mark Fashehfd4ef232009-01-29 15:06:21 -08005632 ocfs2_clusters_to_bytes(inode->i_sb, len));
5633
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005634 ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
Mark Fashehfecc0112008-11-12 15:16:38 -08005635 if (ret) {
5636 mlog_errno(ret);
5637 goto out_commit;
5638 }
5639
Joel Becker6136ca52009-02-12 19:32:43 -08005640 ocfs2_et_update_clusters(et, -len);
Mark Fashehfecc0112008-11-12 15:16:38 -08005641
Joel Beckerec20cec2010-03-19 14:13:52 -07005642 ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehfecc0112008-11-12 15:16:38 -08005643
5644 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5645 if (ret)
5646 mlog_errno(ret);
5647
5648out_commit:
5649 ocfs2_commit_trans(osb, handle);
5650out:
5651 mutex_unlock(&tl_inode->i_mutex);
5652
5653 if (meta_ac)
5654 ocfs2_free_alloc_context(meta_ac);
5655
5656 return ret;
5657}
5658
Mark Fasheh063c4562007-07-03 13:34:11 -07005659int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005660{
5661 struct buffer_head *tl_bh = osb->osb_tl_bh;
5662 struct ocfs2_dinode *di;
5663 struct ocfs2_truncate_log *tl;
5664
5665 di = (struct ocfs2_dinode *) tl_bh->b_data;
5666 tl = &di->id2.i_dealloc;
5667
5668 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5669 "slot %d, invalid truncate log parameters: used = "
5670 "%u, count = %u\n", osb->slot_num,
5671 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5672 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5673}
5674
5675static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5676 unsigned int new_start)
5677{
5678 unsigned int tail_index;
5679 unsigned int current_tail;
5680
5681 /* No records, nothing to coalesce */
5682 if (!le16_to_cpu(tl->tl_used))
5683 return 0;
5684
5685 tail_index = le16_to_cpu(tl->tl_used) - 1;
5686 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5687 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5688
5689 return current_tail == new_start;
5690}
5691
Mark Fasheh063c4562007-07-03 13:34:11 -07005692int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5693 handle_t *handle,
5694 u64 start_blk,
5695 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005696{
5697 int status, index;
5698 unsigned int start_cluster, tl_count;
5699 struct inode *tl_inode = osb->osb_tl_inode;
5700 struct buffer_head *tl_bh = osb->osb_tl_bh;
5701 struct ocfs2_dinode *di;
5702 struct ocfs2_truncate_log *tl;
5703
Mark Fashehb06970532006-03-03 10:24:33 -08005704 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5705 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005706
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005707 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005708
5709 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5710
5711 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005712
Joel Becker10995aa2008-11-13 14:49:12 -08005713 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5714 * by the underlying call to ocfs2_read_inode_block(), so any
5715 * corruption is a code bug */
5716 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5717
5718 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005719 tl_count = le16_to_cpu(tl->tl_count);
5720 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5721 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005722 "Truncate record count on #%llu invalid "
5723 "wanted %u, actual %u\n",
5724 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005725 ocfs2_truncate_recs_per_inode(osb->sb),
5726 le16_to_cpu(tl->tl_count));
5727
5728 /* Caller should have known to flush before calling us. */
5729 index = le16_to_cpu(tl->tl_used);
5730 if (index >= tl_count) {
5731 status = -ENOSPC;
5732 mlog_errno(status);
5733 goto bail;
5734 }
5735
Joel Becker0cf2f762009-02-12 16:41:25 -08005736 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005737 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005738 if (status < 0) {
5739 mlog_errno(status);
5740 goto bail;
5741 }
5742
5743 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005744 "%llu (index = %d)\n", num_clusters, start_cluster,
5745 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005746
5747 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5748 /*
5749 * Move index back to the record we are coalescing with.
5750 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5751 */
5752 index--;
5753
5754 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5755 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5756 index, le32_to_cpu(tl->tl_recs[index].t_start),
5757 num_clusters);
5758 } else {
5759 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5760 tl->tl_used = cpu_to_le16(index + 1);
5761 }
5762 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5763
Joel Beckerec20cec2010-03-19 14:13:52 -07005764 ocfs2_journal_dirty(handle, tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005765
5766bail:
5767 mlog_exit(status);
5768 return status;
5769}
5770
5771static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005772 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005773 struct inode *data_alloc_inode,
5774 struct buffer_head *data_alloc_bh)
5775{
5776 int status = 0;
5777 int i;
5778 unsigned int num_clusters;
5779 u64 start_blk;
5780 struct ocfs2_truncate_rec rec;
5781 struct ocfs2_dinode *di;
5782 struct ocfs2_truncate_log *tl;
5783 struct inode *tl_inode = osb->osb_tl_inode;
5784 struct buffer_head *tl_bh = osb->osb_tl_bh;
5785
5786 mlog_entry_void();
5787
5788 di = (struct ocfs2_dinode *) tl_bh->b_data;
5789 tl = &di->id2.i_dealloc;
5790 i = le16_to_cpu(tl->tl_used) - 1;
5791 while (i >= 0) {
5792 /* Caller has given us at least enough credits to
5793 * update the truncate log dinode */
Joel Becker0cf2f762009-02-12 16:41:25 -08005794 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005795 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005796 if (status < 0) {
5797 mlog_errno(status);
5798 goto bail;
5799 }
5800
5801 tl->tl_used = cpu_to_le16(i);
5802
Joel Beckerec20cec2010-03-19 14:13:52 -07005803 ocfs2_journal_dirty(handle, tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005804
5805 /* TODO: Perhaps we can calculate the bulk of the
5806 * credits up front rather than extending like
5807 * this. */
5808 status = ocfs2_extend_trans(handle,
5809 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5810 if (status < 0) {
5811 mlog_errno(status);
5812 goto bail;
5813 }
5814
5815 rec = tl->tl_recs[i];
5816 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5817 le32_to_cpu(rec.t_start));
5818 num_clusters = le32_to_cpu(rec.t_clusters);
5819
5820 /* if start_blk is not set, we ignore the record as
5821 * invalid. */
5822 if (start_blk) {
5823 mlog(0, "free record %d, start = %u, clusters = %u\n",
5824 i, le32_to_cpu(rec.t_start), num_clusters);
5825
5826 status = ocfs2_free_clusters(handle, data_alloc_inode,
5827 data_alloc_bh, start_blk,
5828 num_clusters);
5829 if (status < 0) {
5830 mlog_errno(status);
5831 goto bail;
5832 }
5833 }
5834 i--;
5835 }
5836
5837bail:
5838 mlog_exit(status);
5839 return status;
5840}
5841
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005842/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005843int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005844{
5845 int status;
5846 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005847 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005848 struct inode *tl_inode = osb->osb_tl_inode;
5849 struct inode *data_alloc_inode = NULL;
5850 struct buffer_head *tl_bh = osb->osb_tl_bh;
5851 struct buffer_head *data_alloc_bh = NULL;
5852 struct ocfs2_dinode *di;
5853 struct ocfs2_truncate_log *tl;
5854
5855 mlog_entry_void();
5856
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005857 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005858
5859 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005860
Joel Becker10995aa2008-11-13 14:49:12 -08005861 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5862 * by the underlying call to ocfs2_read_inode_block(), so any
5863 * corruption is a code bug */
5864 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5865
5866 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005867 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005868 mlog(0, "Flush %u records from truncate log #%llu\n",
5869 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005870 if (!num_to_flush) {
5871 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005872 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005873 }
5874
5875 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5876 GLOBAL_BITMAP_SYSTEM_INODE,
5877 OCFS2_INVALID_SLOT);
5878 if (!data_alloc_inode) {
5879 status = -EINVAL;
5880 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005881 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005882 }
5883
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005884 mutex_lock(&data_alloc_inode->i_mutex);
5885
Mark Fashehe63aecb62007-10-18 15:30:42 -07005886 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005887 if (status < 0) {
5888 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005889 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005890 }
5891
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005892 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005893 if (IS_ERR(handle)) {
5894 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005895 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005896 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005897 }
5898
5899 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5900 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005901 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005902 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005903
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005904 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005905
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005906out_unlock:
5907 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005908 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005909
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005910out_mutex:
5911 mutex_unlock(&data_alloc_inode->i_mutex);
5912 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005913
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005914out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005915 mlog_exit(status);
5916 return status;
5917}
5918
5919int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5920{
5921 int status;
5922 struct inode *tl_inode = osb->osb_tl_inode;
5923
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005924 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005925 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005926 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08005927
5928 return status;
5929}
5930
David Howellsc4028952006-11-22 14:57:56 +00005931static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08005932{
5933 int status;
David Howellsc4028952006-11-22 14:57:56 +00005934 struct ocfs2_super *osb =
5935 container_of(work, struct ocfs2_super,
5936 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08005937
5938 mlog_entry_void();
5939
5940 status = ocfs2_flush_truncate_log(osb);
5941 if (status < 0)
5942 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08005943 else
Tiger Yangb89c5422010-01-25 14:11:06 +08005944 ocfs2_init_steal_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08005945
5946 mlog_exit(status);
5947}
5948
5949#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
5950void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
5951 int cancel)
5952{
5953 if (osb->osb_tl_inode) {
5954 /* We want to push off log flushes while truncates are
5955 * still running. */
5956 if (cancel)
5957 cancel_delayed_work(&osb->osb_truncate_log_wq);
5958
5959 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
5960 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
5961 }
5962}
5963
5964static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
5965 int slot_num,
5966 struct inode **tl_inode,
5967 struct buffer_head **tl_bh)
5968{
5969 int status;
5970 struct inode *inode = NULL;
5971 struct buffer_head *bh = NULL;
5972
5973 inode = ocfs2_get_system_file_inode(osb,
5974 TRUNCATE_LOG_SYSTEM_INODE,
5975 slot_num);
5976 if (!inode) {
5977 status = -EINVAL;
5978 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
5979 goto bail;
5980 }
5981
Joel Beckerb657c952008-11-13 14:49:11 -08005982 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005983 if (status < 0) {
5984 iput(inode);
5985 mlog_errno(status);
5986 goto bail;
5987 }
5988
5989 *tl_inode = inode;
5990 *tl_bh = bh;
5991bail:
5992 mlog_exit(status);
5993 return status;
5994}
5995
5996/* called during the 1st stage of node recovery. we stamp a clean
5997 * truncate log and pass back a copy for processing later. if the
5998 * truncate log does not require processing, a *tl_copy is set to
5999 * NULL. */
6000int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6001 int slot_num,
6002 struct ocfs2_dinode **tl_copy)
6003{
6004 int status;
6005 struct inode *tl_inode = NULL;
6006 struct buffer_head *tl_bh = NULL;
6007 struct ocfs2_dinode *di;
6008 struct ocfs2_truncate_log *tl;
6009
6010 *tl_copy = NULL;
6011
6012 mlog(0, "recover truncate log from slot %d\n", slot_num);
6013
6014 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6015 if (status < 0) {
6016 mlog_errno(status);
6017 goto bail;
6018 }
6019
6020 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08006021
Joel Becker10995aa2008-11-13 14:49:12 -08006022 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6023 * validated by the underlying call to ocfs2_read_inode_block(),
6024 * so any corruption is a code bug */
6025 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6026
6027 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08006028 if (le16_to_cpu(tl->tl_used)) {
6029 mlog(0, "We'll have %u logs to recover\n",
6030 le16_to_cpu(tl->tl_used));
6031
6032 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6033 if (!(*tl_copy)) {
6034 status = -ENOMEM;
6035 mlog_errno(status);
6036 goto bail;
6037 }
6038
6039 /* Assuming the write-out below goes well, this copy
6040 * will be passed back to recovery for processing. */
6041 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6042
6043 /* All we need to do to clear the truncate log is set
6044 * tl_used. */
6045 tl->tl_used = 0;
6046
Joel Becker13723d02008-10-17 19:25:01 -07006047 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08006048 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08006049 if (status < 0) {
6050 mlog_errno(status);
6051 goto bail;
6052 }
6053 }
6054
6055bail:
6056 if (tl_inode)
6057 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006058 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006059
6060 if (status < 0 && (*tl_copy)) {
6061 kfree(*tl_copy);
6062 *tl_copy = NULL;
6063 }
6064
6065 mlog_exit(status);
6066 return status;
6067}
6068
6069int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6070 struct ocfs2_dinode *tl_copy)
6071{
6072 int status = 0;
6073 int i;
6074 unsigned int clusters, num_recs, start_cluster;
6075 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006076 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006077 struct inode *tl_inode = osb->osb_tl_inode;
6078 struct ocfs2_truncate_log *tl;
6079
6080 mlog_entry_void();
6081
6082 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6083 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6084 return -EINVAL;
6085 }
6086
6087 tl = &tl_copy->id2.i_dealloc;
6088 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08006089 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006090 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006091
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006092 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006093 for(i = 0; i < num_recs; i++) {
6094 if (ocfs2_truncate_log_needs_flush(osb)) {
6095 status = __ocfs2_flush_truncate_log(osb);
6096 if (status < 0) {
6097 mlog_errno(status);
6098 goto bail_up;
6099 }
6100 }
6101
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006102 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006103 if (IS_ERR(handle)) {
6104 status = PTR_ERR(handle);
6105 mlog_errno(status);
6106 goto bail_up;
6107 }
6108
6109 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6110 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6111 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6112
6113 status = ocfs2_truncate_log_append(osb, handle,
6114 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006115 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006116 if (status < 0) {
6117 mlog_errno(status);
6118 goto bail_up;
6119 }
6120 }
6121
6122bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006123 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006124
6125 mlog_exit(status);
6126 return status;
6127}
6128
6129void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6130{
6131 int status;
6132 struct inode *tl_inode = osb->osb_tl_inode;
6133
6134 mlog_entry_void();
6135
6136 if (tl_inode) {
6137 cancel_delayed_work(&osb->osb_truncate_log_wq);
6138 flush_workqueue(ocfs2_wq);
6139
6140 status = ocfs2_flush_truncate_log(osb);
6141 if (status < 0)
6142 mlog_errno(status);
6143
6144 brelse(osb->osb_tl_bh);
6145 iput(osb->osb_tl_inode);
6146 }
6147
6148 mlog_exit_void();
6149}
6150
6151int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6152{
6153 int status;
6154 struct inode *tl_inode = NULL;
6155 struct buffer_head *tl_bh = NULL;
6156
6157 mlog_entry_void();
6158
6159 status = ocfs2_get_truncate_log_info(osb,
6160 osb->slot_num,
6161 &tl_inode,
6162 &tl_bh);
6163 if (status < 0)
6164 mlog_errno(status);
6165
6166 /* ocfs2_truncate_log_shutdown keys on the existence of
6167 * osb->osb_tl_inode so we don't set any of the osb variables
6168 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006169 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6170 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006171 osb->osb_tl_bh = tl_bh;
6172 osb->osb_tl_inode = tl_inode;
6173
6174 mlog_exit(status);
6175 return status;
6176}
6177
Mark Fasheh2b604352007-06-22 15:45:27 -07006178/*
6179 * Delayed de-allocation of suballocator blocks.
6180 *
6181 * Some sets of block de-allocations might involve multiple suballocator inodes.
6182 *
6183 * The locking for this can get extremely complicated, especially when
6184 * the suballocator inodes to delete from aren't known until deep
6185 * within an unrelated codepath.
6186 *
6187 * ocfs2_extent_block structures are a good example of this - an inode
6188 * btree could have been grown by any number of nodes each allocating
6189 * out of their own suballoc inode.
6190 *
6191 * These structures allow the delay of block de-allocation until a
6192 * later time, when locking of multiple cluster inodes won't cause
6193 * deadlock.
6194 */
6195
6196/*
Tao Ma2891d292008-11-12 08:26:58 +08006197 * Describe a single bit freed from a suballocator. For the block
6198 * suballocators, it represents one block. For the global cluster
6199 * allocator, it represents some clusters and free_bit indicates
6200 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006201 */
6202struct ocfs2_cached_block_free {
6203 struct ocfs2_cached_block_free *free_next;
6204 u64 free_blk;
6205 unsigned int free_bit;
6206};
6207
6208struct ocfs2_per_slot_free_list {
6209 struct ocfs2_per_slot_free_list *f_next_suballocator;
6210 int f_inode_type;
6211 int f_slot;
6212 struct ocfs2_cached_block_free *f_first;
6213};
6214
Tao Ma2891d292008-11-12 08:26:58 +08006215static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6216 int sysfile_type,
6217 int slot,
6218 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006219{
6220 int ret;
6221 u64 bg_blkno;
6222 handle_t *handle;
6223 struct inode *inode;
6224 struct buffer_head *di_bh = NULL;
6225 struct ocfs2_cached_block_free *tmp;
6226
6227 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6228 if (!inode) {
6229 ret = -EINVAL;
6230 mlog_errno(ret);
6231 goto out;
6232 }
6233
6234 mutex_lock(&inode->i_mutex);
6235
Mark Fashehe63aecb62007-10-18 15:30:42 -07006236 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006237 if (ret) {
6238 mlog_errno(ret);
6239 goto out_mutex;
6240 }
6241
6242 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6243 if (IS_ERR(handle)) {
6244 ret = PTR_ERR(handle);
6245 mlog_errno(ret);
6246 goto out_unlock;
6247 }
6248
6249 while (head) {
6250 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6251 head->free_bit);
6252 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6253 head->free_bit, (unsigned long long)head->free_blk);
6254
6255 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6256 head->free_bit, bg_blkno, 1);
6257 if (ret) {
6258 mlog_errno(ret);
6259 goto out_journal;
6260 }
6261
6262 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6263 if (ret) {
6264 mlog_errno(ret);
6265 goto out_journal;
6266 }
6267
6268 tmp = head;
6269 head = head->free_next;
6270 kfree(tmp);
6271 }
6272
6273out_journal:
6274 ocfs2_commit_trans(osb, handle);
6275
6276out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006277 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006278 brelse(di_bh);
6279out_mutex:
6280 mutex_unlock(&inode->i_mutex);
6281 iput(inode);
6282out:
6283 while(head) {
6284 /* Premature exit may have left some dangling items. */
6285 tmp = head;
6286 head = head->free_next;
6287 kfree(tmp);
6288 }
6289
6290 return ret;
6291}
6292
Tao Ma2891d292008-11-12 08:26:58 +08006293int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6294 u64 blkno, unsigned int bit)
6295{
6296 int ret = 0;
6297 struct ocfs2_cached_block_free *item;
6298
6299 item = kmalloc(sizeof(*item), GFP_NOFS);
6300 if (item == NULL) {
6301 ret = -ENOMEM;
6302 mlog_errno(ret);
6303 return ret;
6304 }
6305
6306 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6307 bit, (unsigned long long)blkno);
6308
6309 item->free_blk = blkno;
6310 item->free_bit = bit;
6311 item->free_next = ctxt->c_global_allocator;
6312
6313 ctxt->c_global_allocator = item;
6314 return ret;
6315}
6316
6317static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6318 struct ocfs2_cached_block_free *head)
6319{
6320 struct ocfs2_cached_block_free *tmp;
6321 struct inode *tl_inode = osb->osb_tl_inode;
6322 handle_t *handle;
6323 int ret = 0;
6324
6325 mutex_lock(&tl_inode->i_mutex);
6326
6327 while (head) {
6328 if (ocfs2_truncate_log_needs_flush(osb)) {
6329 ret = __ocfs2_flush_truncate_log(osb);
6330 if (ret < 0) {
6331 mlog_errno(ret);
6332 break;
6333 }
6334 }
6335
6336 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6337 if (IS_ERR(handle)) {
6338 ret = PTR_ERR(handle);
6339 mlog_errno(ret);
6340 break;
6341 }
6342
6343 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6344 head->free_bit);
6345
6346 ocfs2_commit_trans(osb, handle);
6347 tmp = head;
6348 head = head->free_next;
6349 kfree(tmp);
6350
6351 if (ret < 0) {
6352 mlog_errno(ret);
6353 break;
6354 }
6355 }
6356
6357 mutex_unlock(&tl_inode->i_mutex);
6358
6359 while (head) {
6360 /* Premature exit may have left some dangling items. */
6361 tmp = head;
6362 head = head->free_next;
6363 kfree(tmp);
6364 }
6365
6366 return ret;
6367}
6368
Mark Fasheh2b604352007-06-22 15:45:27 -07006369int ocfs2_run_deallocs(struct ocfs2_super *osb,
6370 struct ocfs2_cached_dealloc_ctxt *ctxt)
6371{
6372 int ret = 0, ret2;
6373 struct ocfs2_per_slot_free_list *fl;
6374
6375 if (!ctxt)
6376 return 0;
6377
6378 while (ctxt->c_first_suballocator) {
6379 fl = ctxt->c_first_suballocator;
6380
6381 if (fl->f_first) {
6382 mlog(0, "Free items: (type %u, slot %d)\n",
6383 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006384 ret2 = ocfs2_free_cached_blocks(osb,
6385 fl->f_inode_type,
6386 fl->f_slot,
6387 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006388 if (ret2)
6389 mlog_errno(ret2);
6390 if (!ret)
6391 ret = ret2;
6392 }
6393
6394 ctxt->c_first_suballocator = fl->f_next_suballocator;
6395 kfree(fl);
6396 }
6397
Tao Ma2891d292008-11-12 08:26:58 +08006398 if (ctxt->c_global_allocator) {
6399 ret2 = ocfs2_free_cached_clusters(osb,
6400 ctxt->c_global_allocator);
6401 if (ret2)
6402 mlog_errno(ret2);
6403 if (!ret)
6404 ret = ret2;
6405
6406 ctxt->c_global_allocator = NULL;
6407 }
6408
Mark Fasheh2b604352007-06-22 15:45:27 -07006409 return ret;
6410}
6411
6412static struct ocfs2_per_slot_free_list *
6413ocfs2_find_per_slot_free_list(int type,
6414 int slot,
6415 struct ocfs2_cached_dealloc_ctxt *ctxt)
6416{
6417 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6418
6419 while (fl) {
6420 if (fl->f_inode_type == type && fl->f_slot == slot)
6421 return fl;
6422
6423 fl = fl->f_next_suballocator;
6424 }
6425
6426 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6427 if (fl) {
6428 fl->f_inode_type = type;
6429 fl->f_slot = slot;
6430 fl->f_first = NULL;
6431 fl->f_next_suballocator = ctxt->c_first_suballocator;
6432
6433 ctxt->c_first_suballocator = fl;
6434 }
6435 return fl;
6436}
6437
Tao Ma1823cb02009-08-18 11:24:49 +08006438int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6439 int type, int slot, u64 blkno,
6440 unsigned int bit)
Mark Fasheh2b604352007-06-22 15:45:27 -07006441{
6442 int ret;
6443 struct ocfs2_per_slot_free_list *fl;
6444 struct ocfs2_cached_block_free *item;
6445
6446 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6447 if (fl == NULL) {
6448 ret = -ENOMEM;
6449 mlog_errno(ret);
6450 goto out;
6451 }
6452
6453 item = kmalloc(sizeof(*item), GFP_NOFS);
6454 if (item == NULL) {
6455 ret = -ENOMEM;
6456 mlog_errno(ret);
6457 goto out;
6458 }
6459
6460 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6461 type, slot, bit, (unsigned long long)blkno);
6462
6463 item->free_blk = blkno;
6464 item->free_bit = bit;
6465 item->free_next = fl->f_first;
6466
6467 fl->f_first = item;
6468
6469 ret = 0;
6470out:
6471 return ret;
6472}
6473
Mark Fasheh59a5e412007-06-22 15:52:36 -07006474static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6475 struct ocfs2_extent_block *eb)
6476{
6477 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6478 le16_to_cpu(eb->h_suballoc_slot),
6479 le64_to_cpu(eb->h_blkno),
6480 le16_to_cpu(eb->h_suballoc_bit));
6481}
6482
Mark Fashehccd979b2005-12-15 14:31:24 -08006483/* This function will figure out whether the currently last extent
6484 * block will be deleted, and if it will, what the new last extent
6485 * block will be so we can update his h_next_leaf_blk field, as well
6486 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006487static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006488 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006489 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006490 struct buffer_head **new_last_eb)
6491{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006492 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006493 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006494 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006495 struct ocfs2_extent_block *eb;
6496 struct ocfs2_extent_list *el;
6497 struct buffer_head *bh = NULL;
6498
6499 *new_last_eb = NULL;
6500
Mark Fashehccd979b2005-12-15 14:31:24 -08006501 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006502 if (!path->p_tree_depth)
6503 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006504
6505 /* trunc to zero special case - this makes tree_depth = 0
6506 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006507 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006508 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006509
Mark Fashehdcd05382007-01-16 11:32:23 -08006510 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006511 BUG_ON(!el->l_next_free_rec);
6512
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006513 /*
6514 * Make sure that this extent list will actually be empty
6515 * after we clear away the data. We can shortcut out if
6516 * there's more than one non-empty extent in the
6517 * list. Otherwise, a check of the remaining extent is
6518 * necessary.
6519 */
6520 next_free = le16_to_cpu(el->l_next_free_rec);
6521 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006522 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006523 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006524 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006525
6526 /* We may have a valid extent in index 1, check it. */
6527 if (next_free == 2)
6528 rec = &el->l_recs[1];
6529
6530 /*
6531 * Fall through - no more nonempty extents, so we want
6532 * to delete this leaf.
6533 */
6534 } else {
6535 if (next_free > 1)
6536 goto out;
6537
6538 rec = &el->l_recs[0];
6539 }
6540
6541 if (rec) {
6542 /*
6543 * Check it we'll only be trimming off the end of this
6544 * cluster.
6545 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006546 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006547 goto out;
6548 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006549
Mark Fashehdcd05382007-01-16 11:32:23 -08006550 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6551 if (ret) {
6552 mlog_errno(ret);
6553 goto out;
6554 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006555
Joel Beckerfacdb772009-02-12 18:08:48 -08006556 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08006557 if (ret) {
6558 mlog_errno(ret);
6559 goto out;
6560 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006561
Mark Fashehdcd05382007-01-16 11:32:23 -08006562 eb = (struct ocfs2_extent_block *) bh->b_data;
6563 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006564
6565 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6566 * Any corruption is a code bug. */
6567 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006568
6569 *new_last_eb = bh;
6570 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006571 mlog(0, "returning block %llu, (cpos: %u)\n",
6572 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6573out:
6574 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006575
Mark Fashehdcd05382007-01-16 11:32:23 -08006576 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006577}
6578
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006579/*
6580 * Trim some clusters off the rightmost edge of a tree. Only called
6581 * during truncate.
6582 *
6583 * The caller needs to:
6584 * - start journaling of each path component.
6585 * - compute and fully set up any new last ext block
6586 */
6587static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6588 handle_t *handle, struct ocfs2_truncate_context *tc,
Tao Mabcbbb242009-08-18 11:29:12 +08006589 u32 clusters_to_del, u64 *delete_start, u8 *flags)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006590{
6591 int ret, i, index = path->p_tree_depth;
6592 u32 new_edge = 0;
6593 u64 deleted_eb = 0;
6594 struct buffer_head *bh;
6595 struct ocfs2_extent_list *el;
6596 struct ocfs2_extent_rec *rec;
6597
6598 *delete_start = 0;
Tao Mabcbbb242009-08-18 11:29:12 +08006599 *flags = 0;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006600
6601 while (index >= 0) {
6602 bh = path->p_node[index].bh;
6603 el = path->p_node[index].el;
6604
6605 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6606 index, (unsigned long long)bh->b_blocknr);
6607
6608 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6609
6610 if (index !=
6611 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6612 ocfs2_error(inode->i_sb,
6613 "Inode %lu has invalid ext. block %llu",
6614 inode->i_ino,
6615 (unsigned long long)bh->b_blocknr);
6616 ret = -EROFS;
6617 goto out;
6618 }
6619
6620find_tail_record:
6621 i = le16_to_cpu(el->l_next_free_rec) - 1;
6622 rec = &el->l_recs[i];
6623
6624 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6625 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006626 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006627 (unsigned long long)le64_to_cpu(rec->e_blkno),
6628 le16_to_cpu(el->l_next_free_rec));
6629
Mark Fashehe48edee2007-03-07 16:46:57 -08006630 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006631
6632 if (le16_to_cpu(el->l_tree_depth) == 0) {
6633 /*
6634 * If the leaf block contains a single empty
6635 * extent and no records, we can just remove
6636 * the block.
6637 */
6638 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6639 memset(rec, 0,
6640 sizeof(struct ocfs2_extent_rec));
6641 el->l_next_free_rec = cpu_to_le16(0);
6642
6643 goto delete;
6644 }
6645
6646 /*
6647 * Remove any empty extents by shifting things
6648 * left. That should make life much easier on
6649 * the code below. This condition is rare
6650 * enough that we shouldn't see a performance
6651 * hit.
6652 */
6653 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6654 le16_add_cpu(&el->l_next_free_rec, -1);
6655
6656 for(i = 0;
6657 i < le16_to_cpu(el->l_next_free_rec); i++)
6658 el->l_recs[i] = el->l_recs[i + 1];
6659
6660 memset(&el->l_recs[i], 0,
6661 sizeof(struct ocfs2_extent_rec));
6662
6663 /*
6664 * We've modified our extent list. The
6665 * simplest way to handle this change
6666 * is to being the search from the
6667 * start again.
6668 */
6669 goto find_tail_record;
6670 }
6671
Mark Fashehe48edee2007-03-07 16:46:57 -08006672 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006673
6674 /*
6675 * We'll use "new_edge" on our way back up the
6676 * tree to know what our rightmost cpos is.
6677 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006678 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006679 new_edge += le32_to_cpu(rec->e_cpos);
6680
6681 /*
6682 * The caller will use this to delete data blocks.
6683 */
6684 *delete_start = le64_to_cpu(rec->e_blkno)
6685 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006686 le16_to_cpu(rec->e_leaf_clusters));
Tao Mabcbbb242009-08-18 11:29:12 +08006687 *flags = rec->e_flags;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006688
6689 /*
6690 * If it's now empty, remove this record.
6691 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006692 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006693 memset(rec, 0,
6694 sizeof(struct ocfs2_extent_rec));
6695 le16_add_cpu(&el->l_next_free_rec, -1);
6696 }
6697 } else {
6698 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6699 memset(rec, 0,
6700 sizeof(struct ocfs2_extent_rec));
6701 le16_add_cpu(&el->l_next_free_rec, -1);
6702
6703 goto delete;
6704 }
6705
6706 /* Can this actually happen? */
6707 if (le16_to_cpu(el->l_next_free_rec) == 0)
6708 goto delete;
6709
6710 /*
6711 * We never actually deleted any clusters
6712 * because our leaf was empty. There's no
6713 * reason to adjust the rightmost edge then.
6714 */
6715 if (new_edge == 0)
6716 goto delete;
6717
Mark Fashehe48edee2007-03-07 16:46:57 -08006718 rec->e_int_clusters = cpu_to_le32(new_edge);
6719 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006720 -le32_to_cpu(rec->e_cpos));
6721
6722 /*
6723 * A deleted child record should have been
6724 * caught above.
6725 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006726 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006727 }
6728
6729delete:
Joel Beckerec20cec2010-03-19 14:13:52 -07006730 ocfs2_journal_dirty(handle, bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006731
6732 mlog(0, "extent list container %llu, after: record %d: "
6733 "(%u, %u, %llu), next = %u.\n",
6734 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006735 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006736 (unsigned long long)le64_to_cpu(rec->e_blkno),
6737 le16_to_cpu(el->l_next_free_rec));
6738
6739 /*
6740 * We must be careful to only attempt delete of an
6741 * extent block (and not the root inode block).
6742 */
6743 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6744 struct ocfs2_extent_block *eb =
6745 (struct ocfs2_extent_block *)bh->b_data;
6746
6747 /*
6748 * Save this for use when processing the
6749 * parent block.
6750 */
6751 deleted_eb = le64_to_cpu(eb->h_blkno);
6752
6753 mlog(0, "deleting this extent block.\n");
6754
Joel Becker8cb471e2009-02-10 20:00:41 -08006755 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006756
Mark Fashehe48edee2007-03-07 16:46:57 -08006757 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006758 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6759 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6760
Mark Fasheh59a5e412007-06-22 15:52:36 -07006761 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6762 /* An error here is not fatal. */
6763 if (ret < 0)
6764 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006765 } else {
6766 deleted_eb = 0;
6767 }
6768
6769 index--;
6770 }
6771
6772 ret = 0;
6773out:
6774 return ret;
6775}
6776
Mark Fashehccd979b2005-12-15 14:31:24 -08006777static int ocfs2_do_truncate(struct ocfs2_super *osb,
6778 unsigned int clusters_to_del,
6779 struct inode *inode,
6780 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006781 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006782 struct ocfs2_truncate_context *tc,
Tao Mabcbbb242009-08-18 11:29:12 +08006783 struct ocfs2_path *path,
6784 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08006785{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006786 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006787 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006788 struct ocfs2_extent_block *last_eb = NULL;
6789 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006790 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006791 u64 delete_blk = 0;
Tao Mabcbbb242009-08-18 11:29:12 +08006792 u8 rec_flags;
Mark Fashehccd979b2005-12-15 14:31:24 -08006793
6794 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6795
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006796 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006797 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006798 if (status < 0) {
6799 mlog_errno(status);
6800 goto bail;
6801 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006802
Mark Fashehdcd05382007-01-16 11:32:23 -08006803 /*
6804 * Each component will be touched, so we might as well journal
6805 * here to avoid having to handle errors later.
6806 */
Joel Becker0cf2f762009-02-12 16:41:25 -08006807 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006808 if (status < 0) {
6809 mlog_errno(status);
6810 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006811 }
6812
6813 if (last_eb_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08006814 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07006815 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08006816 if (status < 0) {
6817 mlog_errno(status);
6818 goto bail;
6819 }
6820
6821 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6822 }
6823
6824 el = &(fe->id2.i_list);
6825
6826 /*
6827 * Lower levels depend on this never happening, but it's best
6828 * to check it up here before changing the tree.
6829 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006830 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006831 ocfs2_error(inode->i_sb,
6832 "Inode %lu has an empty extent record, depth %u\n",
6833 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006834 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006835 goto bail;
6836 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006837
Christoph Hellwig5dd40562010-03-03 09:05:00 -05006838 dquot_free_space_nodirty(inode,
Jan Karaa90714c2008-10-09 19:38:40 +02006839 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006840 spin_lock(&OCFS2_I(inode)->ip_lock);
6841 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6842 clusters_to_del;
6843 spin_unlock(&OCFS2_I(inode)->ip_lock);
6844 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006845 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006846
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006847 status = ocfs2_trim_tree(inode, path, handle, tc,
Tao Mabcbbb242009-08-18 11:29:12 +08006848 clusters_to_del, &delete_blk, &rec_flags);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006849 if (status) {
6850 mlog_errno(status);
6851 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006852 }
6853
Mark Fashehdcd05382007-01-16 11:32:23 -08006854 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006855 /* trunc to zero is a special case. */
6856 el->l_tree_depth = 0;
6857 fe->i_last_eb_blk = 0;
6858 } else if (last_eb)
6859 fe->i_last_eb_blk = last_eb->h_blkno;
6860
Joel Beckerec20cec2010-03-19 14:13:52 -07006861 ocfs2_journal_dirty(handle, fe_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006862
6863 if (last_eb) {
6864 /* If there will be a new last extent block, then by
6865 * definition, there cannot be any leaves to the right of
6866 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006867 last_eb->h_next_leaf_blk = 0;
Joel Beckerec20cec2010-03-19 14:13:52 -07006868 ocfs2_journal_dirty(handle, last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006869 }
6870
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006871 if (delete_blk) {
Tao Mabcbbb242009-08-18 11:29:12 +08006872 if (rec_flags & OCFS2_EXT_REFCOUNTED)
6873 status = ocfs2_decrease_refcount(inode, handle,
6874 ocfs2_blocks_to_clusters(osb->sb,
6875 delete_blk),
6876 clusters_to_del, meta_ac,
Tao Ma6ae23c52009-08-18 11:30:55 +08006877 &tc->tc_dealloc, 1);
Tao Mabcbbb242009-08-18 11:29:12 +08006878 else
6879 status = ocfs2_truncate_log_append(osb, handle,
6880 delete_blk,
6881 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006882 if (status < 0) {
6883 mlog_errno(status);
6884 goto bail;
6885 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006886 }
6887 status = 0;
6888bail:
Tao Ma60e2ec42009-08-12 14:42:47 +08006889 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006890 mlog_exit(status);
6891 return status;
6892}
6893
Joel Becker2b4e30f2008-09-03 20:03:41 -07006894static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006895{
6896 set_buffer_uptodate(bh);
6897 mark_buffer_dirty(bh);
6898 return 0;
6899}
6900
Tao Ma6f70fa52009-08-25 08:05:12 +08006901void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6902 unsigned int from, unsigned int to,
6903 struct page *page, int zero, u64 *phys)
Mark Fasheh1d410a62007-09-07 14:20:45 -07006904{
6905 int ret, partial = 0;
6906
6907 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6908 if (ret)
6909 mlog_errno(ret);
6910
6911 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006912 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006913
6914 /*
6915 * Need to set the buffers we zero'd into uptodate
6916 * here if they aren't - ocfs2_map_page_blocks()
6917 * might've skipped some
6918 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006919 ret = walk_page_buffers(handle, page_buffers(page),
6920 from, to, &partial,
6921 ocfs2_zero_func);
6922 if (ret < 0)
6923 mlog_errno(ret);
6924 else if (ocfs2_should_order_data(inode)) {
6925 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006926 if (ret < 0)
6927 mlog_errno(ret);
6928 }
6929
6930 if (!partial)
6931 SetPageUptodate(page);
6932
6933 flush_dcache_page(page);
6934}
6935
Mark Fasheh35edec12007-07-06 14:41:18 -07006936static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6937 loff_t end, struct page **pages,
6938 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006939{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006940 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006941 struct page *page;
6942 unsigned int from, to = PAGE_CACHE_SIZE;
6943 struct super_block *sb = inode->i_sb;
6944
6945 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6946
6947 if (numpages == 0)
6948 goto out;
6949
Mark Fasheh35edec12007-07-06 14:41:18 -07006950 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006951 for(i = 0; i < numpages; i++) {
6952 page = pages[i];
6953
Mark Fasheh35edec12007-07-06 14:41:18 -07006954 from = start & (PAGE_CACHE_SIZE - 1);
6955 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6956 to = end & (PAGE_CACHE_SIZE - 1);
6957
Mark Fasheh60b11392007-02-16 11:46:50 -08006958 BUG_ON(from > PAGE_CACHE_SIZE);
6959 BUG_ON(to > PAGE_CACHE_SIZE);
6960
Mark Fasheh1d410a62007-09-07 14:20:45 -07006961 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6962 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006963
Mark Fasheh35edec12007-07-06 14:41:18 -07006964 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006965 }
6966out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006967 if (pages)
6968 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006969}
6970
Tao Ma6f70fa52009-08-25 08:05:12 +08006971int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
6972 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006973{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006974 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006975 struct address_space *mapping = inode->i_mapping;
6976 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006977 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006978
Mark Fasheh35edec12007-07-06 14:41:18 -07006979 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006980
Mark Fasheh1d410a62007-09-07 14:20:45 -07006981 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006982 last_page_bytes = PAGE_ALIGN(end);
6983 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006984 do {
6985 pages[numpages] = grab_cache_page(mapping, index);
6986 if (!pages[numpages]) {
6987 ret = -ENOMEM;
6988 mlog_errno(ret);
6989 goto out;
6990 }
6991
6992 numpages++;
6993 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006994 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006995
6996out:
6997 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006998 if (pages)
6999 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08007000 numpages = 0;
7001 }
7002
7003 *num = numpages;
7004
7005 return ret;
7006}
7007
Tao Ma6f70fa52009-08-25 08:05:12 +08007008static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
7009 struct page **pages, int *num)
7010{
7011 struct super_block *sb = inode->i_sb;
7012
7013 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
7014 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
7015
7016 return ocfs2_grab_pages(inode, start, end, pages, num);
7017}
7018
Mark Fasheh60b11392007-02-16 11:46:50 -08007019/*
7020 * Zero the area past i_size but still within an allocated
7021 * cluster. This avoids exposing nonzero data on subsequent file
7022 * extends.
7023 *
7024 * We need to call this before i_size is updated on the inode because
7025 * otherwise block_write_full_page() will skip writeout of pages past
7026 * i_size. The new_i_size parameter is passed for this reason.
7027 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007028int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
7029 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08007030{
Mark Fasheh1d410a62007-09-07 14:20:45 -07007031 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08007032 struct page **pages = NULL;
7033 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07007034 unsigned int ext_flags;
7035 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08007036
7037 /*
7038 * File systems which don't support sparse files zero on every
7039 * extend.
7040 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07007041 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08007042 return 0;
7043
Mark Fasheh1d410a62007-09-07 14:20:45 -07007044 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08007045 sizeof(struct page *), GFP_NOFS);
7046 if (pages == NULL) {
7047 ret = -ENOMEM;
7048 mlog_errno(ret);
7049 goto out;
7050 }
7051
Mark Fasheh1d410a62007-09-07 14:20:45 -07007052 if (range_start == range_end)
7053 goto out;
7054
7055 ret = ocfs2_extent_map_get_blocks(inode,
7056 range_start >> sb->s_blocksize_bits,
7057 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08007058 if (ret) {
7059 mlog_errno(ret);
7060 goto out;
7061 }
7062
Mark Fasheh1d410a62007-09-07 14:20:45 -07007063 /*
7064 * Tail is a hole, or is marked unwritten. In either case, we
7065 * can count on read and write to return/push zero's.
7066 */
7067 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08007068 goto out;
7069
Mark Fasheh1d410a62007-09-07 14:20:45 -07007070 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7071 &numpages);
7072 if (ret) {
7073 mlog_errno(ret);
7074 goto out;
7075 }
7076
Mark Fasheh35edec12007-07-06 14:41:18 -07007077 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7078 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08007079
7080 /*
7081 * Initiate writeout of the pages we zero'd here. We don't
7082 * wait on them - the truncate_inode_pages() call later will
7083 * do that for us.
7084 */
Christoph Hellwig2cfd30a2009-09-23 15:04:02 +02007085 ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
7086 range_end - 1);
Mark Fasheh60b11392007-02-16 11:46:50 -08007087 if (ret)
7088 mlog_errno(ret);
7089
7090out:
7091 if (pages)
7092 kfree(pages);
7093
7094 return ret;
7095}
7096
Tiger Yangfdd77702008-08-18 17:08:55 +08007097static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7098 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007099{
7100 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08007101 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007102
Tiger Yangfdd77702008-08-18 17:08:55 +08007103 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7104 memset(&di->id2, 0, blocksize -
7105 offsetof(struct ocfs2_dinode, id2) -
7106 xattrsize);
7107 else
7108 memset(&di->id2, 0, blocksize -
7109 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007110}
7111
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007112void ocfs2_dinode_new_extent_list(struct inode *inode,
7113 struct ocfs2_dinode *di)
7114{
Tiger Yangfdd77702008-08-18 17:08:55 +08007115 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007116 di->id2.i_list.l_tree_depth = 0;
7117 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08007118 di->id2.i_list.l_count = cpu_to_le16(
7119 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007120}
7121
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007122void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7123{
7124 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7125 struct ocfs2_inline_data *idata = &di->id2.i_data;
7126
7127 spin_lock(&oi->ip_lock);
7128 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7129 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7130 spin_unlock(&oi->ip_lock);
7131
7132 /*
7133 * We clear the entire i_data structure here so that all
7134 * fields can be properly initialized.
7135 */
Tiger Yangfdd77702008-08-18 17:08:55 +08007136 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007137
Tiger Yangfdd77702008-08-18 17:08:55 +08007138 idata->id_count = cpu_to_le16(
7139 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007140}
7141
7142int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7143 struct buffer_head *di_bh)
7144{
7145 int ret, i, has_data, num_pages = 0;
7146 handle_t *handle;
7147 u64 uninitialized_var(block);
7148 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7149 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7150 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007151 struct ocfs2_alloc_context *data_ac = NULL;
7152 struct page **pages = NULL;
7153 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07007154 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02007155 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007156
7157 has_data = i_size_read(inode) ? 1 : 0;
7158
7159 if (has_data) {
7160 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7161 sizeof(struct page *), GFP_NOFS);
7162 if (pages == NULL) {
7163 ret = -ENOMEM;
7164 mlog_errno(ret);
7165 goto out;
7166 }
7167
7168 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7169 if (ret) {
7170 mlog_errno(ret);
7171 goto out;
7172 }
7173 }
7174
Jan Karaa90714c2008-10-09 19:38:40 +02007175 handle = ocfs2_start_trans(osb,
7176 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007177 if (IS_ERR(handle)) {
7178 ret = PTR_ERR(handle);
7179 mlog_errno(ret);
7180 goto out_unlock;
7181 }
7182
Joel Becker0cf2f762009-02-12 16:41:25 -08007183 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007184 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007185 if (ret) {
7186 mlog_errno(ret);
7187 goto out_commit;
7188 }
7189
7190 if (has_data) {
7191 u32 bit_off, num;
7192 unsigned int page_end;
7193 u64 phys;
7194
Christoph Hellwig5dd40562010-03-03 09:05:00 -05007195 ret = dquot_alloc_space_nodirty(inode,
7196 ocfs2_clusters_to_bytes(osb->sb, 1));
7197 if (ret)
Jan Karaa90714c2008-10-09 19:38:40 +02007198 goto out_commit;
Jan Karaa90714c2008-10-09 19:38:40 +02007199 did_quota = 1;
7200
Mark Fasheh4fe370a2009-12-07 13:15:40 -08007201 data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
7202
Joel Becker1ed9b772010-05-06 13:59:06 +08007203 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007204 &num);
7205 if (ret) {
7206 mlog_errno(ret);
7207 goto out_commit;
7208 }
7209
7210 /*
7211 * Save two copies, one for insert, and one that can
7212 * be changed by ocfs2_map_and_dirty_page() below.
7213 */
7214 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7215
7216 /*
7217 * Non sparse file systems zero on extend, so no need
7218 * to do that now.
7219 */
7220 if (!ocfs2_sparse_alloc(osb) &&
7221 PAGE_CACHE_SIZE < osb->s_clustersize)
7222 end = PAGE_CACHE_SIZE;
7223
7224 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7225 if (ret) {
7226 mlog_errno(ret);
7227 goto out_commit;
7228 }
7229
7230 /*
7231 * This should populate the 1st page for us and mark
7232 * it up to date.
7233 */
7234 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7235 if (ret) {
7236 mlog_errno(ret);
7237 goto out_commit;
7238 }
7239
7240 page_end = PAGE_CACHE_SIZE;
7241 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7242 page_end = osb->s_clustersize;
7243
7244 for (i = 0; i < num_pages; i++)
7245 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7246 pages[i], i > 0, &phys);
7247 }
7248
7249 spin_lock(&oi->ip_lock);
7250 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7251 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7252 spin_unlock(&oi->ip_lock);
7253
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007254 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007255
7256 ocfs2_journal_dirty(handle, di_bh);
7257
7258 if (has_data) {
7259 /*
7260 * An error at this point should be extremely rare. If
7261 * this proves to be false, we could always re-build
7262 * the in-inode data from our pages.
7263 */
Joel Becker5e404e92009-02-13 03:54:22 -08007264 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
Joel Beckercc79d8c2009-02-13 03:24:43 -08007265 ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007266 if (ret) {
7267 mlog_errno(ret);
7268 goto out_commit;
7269 }
7270
7271 inode->i_blocks = ocfs2_inode_sector_count(inode);
7272 }
7273
7274out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007275 if (ret < 0 && did_quota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05007276 dquot_free_space_nodirty(inode,
Jan Karaa90714c2008-10-09 19:38:40 +02007277 ocfs2_clusters_to_bytes(osb->sb, 1));
7278
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007279 ocfs2_commit_trans(osb, handle);
7280
7281out_unlock:
7282 if (data_ac)
7283 ocfs2_free_alloc_context(data_ac);
7284
7285out:
7286 if (pages) {
7287 ocfs2_unlock_and_free_pages(pages, num_pages);
7288 kfree(pages);
7289 }
7290
7291 return ret;
7292}
7293
Mark Fashehccd979b2005-12-15 14:31:24 -08007294/*
7295 * It is expected, that by the time you call this function,
7296 * inode->i_size and fe->i_size have been adjusted.
7297 *
7298 * WARNING: This will kfree the truncate context
7299 */
7300int ocfs2_commit_truncate(struct ocfs2_super *osb,
7301 struct inode *inode,
7302 struct buffer_head *fe_bh,
7303 struct ocfs2_truncate_context *tc)
7304{
7305 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08007306 u32 clusters_to_del, new_highest_cpos, range;
Tao Mabcbbb242009-08-18 11:29:12 +08007307 u64 blkno = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08007308 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07007309 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007310 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08007311 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08007312 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Tao Mabcbbb242009-08-18 11:29:12 +08007313 struct ocfs2_alloc_context *meta_ac = NULL;
7314 struct ocfs2_refcount_tree *ref_tree = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007315
7316 mlog_entry_void();
7317
Mark Fashehdcd05382007-01-16 11:32:23 -08007318 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007319 i_size_read(inode));
7320
Joel Becker13723d02008-10-17 19:25:01 -07007321 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7322 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08007323 if (!path) {
7324 status = -ENOMEM;
7325 mlog_errno(status);
7326 goto bail;
7327 }
Mark Fasheh83418972007-04-23 18:53:12 -07007328
7329 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7330
Mark Fashehccd979b2005-12-15 14:31:24 -08007331start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007332 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007333 * Check that we still have allocation to delete.
7334 */
7335 if (OCFS2_I(inode)->ip_clusters == 0) {
7336 status = 0;
7337 goto bail;
7338 }
7339
Tao Mabcbbb242009-08-18 11:29:12 +08007340 credits = 0;
7341
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007342 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007343 * Truncate always works against the rightmost tree branch.
7344 */
Joel Beckerfacdb772009-02-12 18:08:48 -08007345 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
Mark Fashehdcd05382007-01-16 11:32:23 -08007346 if (status) {
7347 mlog_errno(status);
7348 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007349 }
7350
Mark Fashehdcd05382007-01-16 11:32:23 -08007351 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7352 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7353
7354 /*
7355 * By now, el will point to the extent list on the bottom most
7356 * portion of this tree. Only the tail record is considered in
7357 * each pass.
7358 *
7359 * We handle the following cases, in order:
7360 * - empty extent: delete the remaining branch
7361 * - remove the entire record
7362 * - remove a partial record
7363 * - no record needs to be removed (truncate has completed)
7364 */
7365 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007366 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7367 ocfs2_error(inode->i_sb,
7368 "Inode %llu has empty extent block at %llu\n",
7369 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7370 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7371 status = -EROFS;
7372 goto bail;
7373 }
7374
Mark Fashehccd979b2005-12-15 14:31:24 -08007375 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08007376 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08007377 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007378 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7379 clusters_to_del = 0;
7380 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007381 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Tao Mabcbbb242009-08-18 11:29:12 +08007382 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08007383 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007384 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08007385 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08007386 new_highest_cpos;
Tao Mabcbbb242009-08-18 11:29:12 +08007387 blkno = le64_to_cpu(el->l_recs[i].e_blkno) +
7388 ocfs2_clusters_to_blocks(inode->i_sb,
7389 ocfs2_rec_clusters(el, &el->l_recs[i]) -
7390 clusters_to_del);
Mark Fashehdcd05382007-01-16 11:32:23 -08007391 } else {
7392 status = 0;
7393 goto bail;
7394 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007395
Mark Fashehdcd05382007-01-16 11:32:23 -08007396 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7397 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7398
Tao Mabcbbb242009-08-18 11:29:12 +08007399 if (el->l_recs[i].e_flags & OCFS2_EXT_REFCOUNTED && clusters_to_del) {
7400 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
7401 OCFS2_HAS_REFCOUNT_FL));
7402
7403 status = ocfs2_lock_refcount_tree(osb,
7404 le64_to_cpu(di->i_refcount_loc),
7405 1, &ref_tree, NULL);
7406 if (status) {
7407 mlog_errno(status);
7408 goto bail;
7409 }
7410
7411 status = ocfs2_prepare_refcount_change_for_del(inode, fe_bh,
7412 blkno,
7413 clusters_to_del,
7414 &credits,
7415 &meta_ac);
7416 if (status < 0) {
7417 mlog_errno(status);
7418 goto bail;
7419 }
7420 }
7421
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007422 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007423 tl_sem = 1;
7424 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7425 * record is free for use. If there isn't any, we flush to get
7426 * an empty truncate log. */
7427 if (ocfs2_truncate_log_needs_flush(osb)) {
7428 status = __ocfs2_flush_truncate_log(osb);
7429 if (status < 0) {
7430 mlog_errno(status);
7431 goto bail;
7432 }
7433 }
7434
Tao Mabcbbb242009-08-18 11:29:12 +08007435 credits += ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007436 (struct ocfs2_dinode *)fe_bh->b_data,
7437 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007438 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007439 if (IS_ERR(handle)) {
7440 status = PTR_ERR(handle);
7441 handle = NULL;
7442 mlog_errno(status);
7443 goto bail;
7444 }
7445
Mark Fashehdcd05382007-01-16 11:32:23 -08007446 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
Tao Mabcbbb242009-08-18 11:29:12 +08007447 tc, path, meta_ac);
Mark Fashehccd979b2005-12-15 14:31:24 -08007448 if (status < 0) {
7449 mlog_errno(status);
7450 goto bail;
7451 }
7452
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007453 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007454 tl_sem = 0;
7455
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007456 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007457 handle = NULL;
7458
Mark Fashehdcd05382007-01-16 11:32:23 -08007459 ocfs2_reinit_path(path, 1);
7460
Tao Mabcbbb242009-08-18 11:29:12 +08007461 if (meta_ac) {
7462 ocfs2_free_alloc_context(meta_ac);
7463 meta_ac = NULL;
7464 }
7465
7466 if (ref_tree) {
7467 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
7468 ref_tree = NULL;
7469 }
7470
Mark Fashehdcd05382007-01-16 11:32:23 -08007471 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007472 * The check above will catch the case where we've truncated
7473 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007474 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007475 goto start;
7476
Mark Fashehccd979b2005-12-15 14:31:24 -08007477bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007478
7479 ocfs2_schedule_truncate_log_flush(osb, 1);
7480
7481 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007482 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007483
7484 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007485 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007486
Tao Mabcbbb242009-08-18 11:29:12 +08007487 if (meta_ac)
7488 ocfs2_free_alloc_context(meta_ac);
7489
7490 if (ref_tree)
7491 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
7492
Mark Fasheh59a5e412007-06-22 15:52:36 -07007493 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7494
Mark Fashehdcd05382007-01-16 11:32:23 -08007495 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007496
7497 /* This will drop the ext_alloc cluster lock for us */
7498 ocfs2_free_truncate_context(tc);
7499
7500 mlog_exit(status);
7501 return status;
7502}
7503
Mark Fashehccd979b2005-12-15 14:31:24 -08007504/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007505 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007506 */
7507int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7508 struct inode *inode,
7509 struct buffer_head *fe_bh,
7510 struct ocfs2_truncate_context **tc)
7511{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007512 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007513 unsigned int new_i_clusters;
7514 struct ocfs2_dinode *fe;
7515 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007516 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007517
7518 mlog_entry_void();
7519
7520 *tc = NULL;
7521
7522 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7523 i_size_read(inode));
7524 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7525
7526 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007527 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7528 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007529
Robert P. J. Daycd861282006-12-13 00:34:52 -08007530 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007531 if (!(*tc)) {
7532 status = -ENOMEM;
7533 mlog_errno(status);
7534 goto bail;
7535 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007536 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007537
Mark Fashehccd979b2005-12-15 14:31:24 -08007538 if (fe->id2.i_list.l_tree_depth) {
Joel Becker3d03a302009-02-12 17:49:26 -08007539 status = ocfs2_read_extent_block(INODE_CACHE(inode),
Joel Becker5e965812008-11-13 14:49:16 -08007540 le64_to_cpu(fe->i_last_eb_blk),
7541 &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007542 if (status < 0) {
7543 mlog_errno(status);
7544 goto bail;
7545 }
7546 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007547 }
7548
7549 (*tc)->tc_last_eb_bh = last_eb_bh;
7550
Mark Fashehccd979b2005-12-15 14:31:24 -08007551 status = 0;
7552bail:
7553 if (status < 0) {
7554 if (*tc)
7555 ocfs2_free_truncate_context(*tc);
7556 *tc = NULL;
7557 }
7558 mlog_exit_void();
7559 return status;
7560}
7561
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007562/*
7563 * 'start' is inclusive, 'end' is not.
7564 */
7565int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7566 unsigned int start, unsigned int end, int trunc)
7567{
7568 int ret;
7569 unsigned int numbytes;
7570 handle_t *handle;
7571 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7572 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7573 struct ocfs2_inline_data *idata = &di->id2.i_data;
7574
7575 if (end > i_size_read(inode))
7576 end = i_size_read(inode);
7577
7578 BUG_ON(start >= end);
7579
7580 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7581 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7582 !ocfs2_supports_inline_data(osb)) {
7583 ocfs2_error(inode->i_sb,
7584 "Inline data flags for inode %llu don't agree! "
7585 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7586 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7587 le16_to_cpu(di->i_dyn_features),
7588 OCFS2_I(inode)->ip_dyn_features,
7589 osb->s_feature_incompat);
7590 ret = -EROFS;
7591 goto out;
7592 }
7593
7594 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7595 if (IS_ERR(handle)) {
7596 ret = PTR_ERR(handle);
7597 mlog_errno(ret);
7598 goto out;
7599 }
7600
Joel Becker0cf2f762009-02-12 16:41:25 -08007601 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007602 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007603 if (ret) {
7604 mlog_errno(ret);
7605 goto out_commit;
7606 }
7607
7608 numbytes = end - start;
7609 memset(idata->id_data + start, 0, numbytes);
7610
7611 /*
7612 * No need to worry about the data page here - it's been
7613 * truncated already and inline data doesn't need it for
7614 * pushing zero's to disk, so we'll let readpage pick it up
7615 * later.
7616 */
7617 if (trunc) {
7618 i_size_write(inode, start);
7619 di->i_size = cpu_to_le64(start);
7620 }
7621
7622 inode->i_blocks = ocfs2_inode_sector_count(inode);
7623 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7624
7625 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7626 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7627
7628 ocfs2_journal_dirty(handle, di_bh);
7629
7630out_commit:
7631 ocfs2_commit_trans(osb, handle);
7632
7633out:
7634 return ret;
7635}
7636
Mark Fashehccd979b2005-12-15 14:31:24 -08007637static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7638{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007639 /*
7640 * The caller is responsible for completing deallocation
7641 * before freeing the context.
7642 */
7643 if (tc->tc_dealloc.c_first_suballocator != NULL)
7644 mlog(ML_NOTICE,
7645 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007646
Mark Fasheha81cb882008-10-07 14:25:16 -07007647 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007648
7649 kfree(tc);
7650}