blob: 47a6ce84e67f31b552cc71572d45b5a457fa2c18 [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 Fasheh59a5e412007-06-22 15:52:36 -0700568static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
569 struct ocfs2_extent_block *eb);
Joel Beckerd401dc12009-02-13 02:24:10 -0800570static void ocfs2_adjust_rightmost_records(handle_t *handle,
571 struct ocfs2_extent_tree *et,
Tao Ma6b791bc2009-06-12 14:18:36 +0800572 struct ocfs2_path *path,
573 struct ocfs2_extent_rec *insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -0800574/*
575 * Reset the actual path elements so that we can re-use the structure
576 * to build another path. Generally, this involves freeing the buffer
577 * heads.
578 */
Tao Mae2e9f602009-08-18 11:22:34 +0800579void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
Mark Fashehdcd05382007-01-16 11:32:23 -0800580{
581 int i, start = 0, depth = 0;
582 struct ocfs2_path_item *node;
583
584 if (keep_root)
585 start = 1;
586
587 for(i = start; i < path_num_items(path); i++) {
588 node = &path->p_node[i];
589
590 brelse(node->bh);
591 node->bh = NULL;
592 node->el = NULL;
593 }
594
595 /*
596 * Tree depth may change during truncate, or insert. If we're
597 * keeping the root extent list, then make sure that our path
598 * structure reflects the proper depth.
599 */
600 if (keep_root)
601 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
Joel Becker13723d02008-10-17 19:25:01 -0700602 else
603 path_root_access(path) = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -0800604
605 path->p_tree_depth = depth;
606}
607
Tao Mae2e9f602009-08-18 11:22:34 +0800608void ocfs2_free_path(struct ocfs2_path *path)
Mark Fashehdcd05382007-01-16 11:32:23 -0800609{
610 if (path) {
611 ocfs2_reinit_path(path, 0);
612 kfree(path);
613 }
614}
615
616/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700617 * All the elements of src into dest. After this call, src could be freed
618 * without affecting dest.
619 *
620 * Both paths should have the same root. Any non-root elements of dest
621 * will be freed.
622 */
623static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
624{
625 int i;
626
627 BUG_ON(path_root_bh(dest) != path_root_bh(src));
628 BUG_ON(path_root_el(dest) != path_root_el(src));
Joel Becker13723d02008-10-17 19:25:01 -0700629 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fasheh328d5752007-06-18 10:48:04 -0700630
631 ocfs2_reinit_path(dest, 1);
632
633 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
634 dest->p_node[i].bh = src->p_node[i].bh;
635 dest->p_node[i].el = src->p_node[i].el;
636
637 if (dest->p_node[i].bh)
638 get_bh(dest->p_node[i].bh);
639 }
640}
641
642/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800643 * Make the *dest path the same as src and re-initialize src path to
644 * have a root only.
645 */
646static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
647{
648 int i;
649
650 BUG_ON(path_root_bh(dest) != path_root_bh(src));
Joel Becker13723d02008-10-17 19:25:01 -0700651 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fashehdcd05382007-01-16 11:32:23 -0800652
653 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
654 brelse(dest->p_node[i].bh);
655
656 dest->p_node[i].bh = src->p_node[i].bh;
657 dest->p_node[i].el = src->p_node[i].el;
658
659 src->p_node[i].bh = NULL;
660 src->p_node[i].el = NULL;
661 }
662}
663
664/*
665 * Insert an extent block at given index.
666 *
667 * This will not take an additional reference on eb_bh.
668 */
669static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
670 struct buffer_head *eb_bh)
671{
672 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
673
674 /*
675 * Right now, no root bh is an extent block, so this helps
676 * catch code errors with dinode trees. The assertion can be
677 * safely removed if we ever need to insert extent block
678 * structures at the root.
679 */
680 BUG_ON(index == 0);
681
682 path->p_node[index].bh = eb_bh;
683 path->p_node[index].el = &eb->h_list;
684}
685
686static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700687 struct ocfs2_extent_list *root_el,
688 ocfs2_journal_access_func access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800689{
690 struct ocfs2_path *path;
691
692 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
693
694 path = kzalloc(sizeof(*path), GFP_NOFS);
695 if (path) {
696 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
697 get_bh(root_bh);
698 path_root_bh(path) = root_bh;
699 path_root_el(path) = root_el;
Joel Becker13723d02008-10-17 19:25:01 -0700700 path_root_access(path) = access;
Mark Fashehdcd05382007-01-16 11:32:23 -0800701 }
702
703 return path;
704}
705
Tao Mae2e9f602009-08-18 11:22:34 +0800706struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
Joel Beckerffdd7a52008-10-17 22:32:01 -0700707{
Joel Becker13723d02008-10-17 19:25:01 -0700708 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
709 path_root_access(path));
Joel Beckerffdd7a52008-10-17 22:32:01 -0700710}
711
Tao Mae2e9f602009-08-18 11:22:34 +0800712struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
Joel Beckerffdd7a52008-10-17 22:32:01 -0700713{
Joel Becker13723d02008-10-17 19:25:01 -0700714 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
715 et->et_root_journal_access);
716}
717
718/*
719 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
720 * otherwise it's the root_access function.
721 *
722 * I don't like the way this function's name looks next to
723 * ocfs2_journal_access_path(), but I don't have a better one.
724 */
Tao Mae2e9f602009-08-18 11:22:34 +0800725int ocfs2_path_bh_journal_access(handle_t *handle,
726 struct ocfs2_caching_info *ci,
727 struct ocfs2_path *path,
728 int idx)
Joel Becker13723d02008-10-17 19:25:01 -0700729{
730 ocfs2_journal_access_func access = path_root_access(path);
731
732 if (!access)
733 access = ocfs2_journal_access;
734
735 if (idx)
736 access = ocfs2_journal_access_eb;
737
Joel Becker0cf2f762009-02-12 16:41:25 -0800738 return access(handle, ci, path->p_node[idx].bh,
Joel Becker13723d02008-10-17 19:25:01 -0700739 OCFS2_JOURNAL_ACCESS_WRITE);
Joel Beckerffdd7a52008-10-17 22:32:01 -0700740}
741
Mark Fashehdcd05382007-01-16 11:32:23 -0800742/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800743 * Convenience function to journal all components in a path.
744 */
Tao Mae2e9f602009-08-18 11:22:34 +0800745int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
746 handle_t *handle,
747 struct ocfs2_path *path)
Mark Fashehdcd05382007-01-16 11:32:23 -0800748{
749 int i, ret = 0;
750
751 if (!path)
752 goto out;
753
754 for(i = 0; i < path_num_items(path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800755 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -0800756 if (ret < 0) {
757 mlog_errno(ret);
758 goto out;
759 }
760 }
761
762out:
763 return ret;
764}
765
Mark Fasheh328d5752007-06-18 10:48:04 -0700766/*
767 * Return the index of the extent record which contains cluster #v_cluster.
768 * -1 is returned if it was not found.
769 *
770 * Should work fine on interior and exterior nodes.
771 */
772int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
773{
774 int ret = -1;
775 int i;
776 struct ocfs2_extent_rec *rec;
777 u32 rec_end, rec_start, clusters;
778
779 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
780 rec = &el->l_recs[i];
781
782 rec_start = le32_to_cpu(rec->e_cpos);
783 clusters = ocfs2_rec_clusters(el, rec);
784
785 rec_end = rec_start + clusters;
786
787 if (v_cluster >= rec_start && v_cluster < rec_end) {
788 ret = i;
789 break;
790 }
791 }
792
793 return ret;
794}
795
Mark Fashehe48edee2007-03-07 16:46:57 -0800796/*
797 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
Tao Ma853a3a12009-08-18 11:22:18 +0800798 * ocfs2_extent_rec_contig only work properly against leaf nodes!
Mark Fashehe48edee2007-03-07 16:46:57 -0800799 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800800static int ocfs2_block_extent_contig(struct super_block *sb,
801 struct ocfs2_extent_rec *ext,
802 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800803{
Mark Fashehe48edee2007-03-07 16:46:57 -0800804 u64 blk_end = le64_to_cpu(ext->e_blkno);
805
806 blk_end += ocfs2_clusters_to_blocks(sb,
807 le16_to_cpu(ext->e_leaf_clusters));
808
809 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800810}
811
Mark Fashehdcd05382007-01-16 11:32:23 -0800812static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
813 struct ocfs2_extent_rec *right)
814{
Mark Fashehe48edee2007-03-07 16:46:57 -0800815 u32 left_range;
816
817 left_range = le32_to_cpu(left->e_cpos) +
818 le16_to_cpu(left->e_leaf_clusters);
819
820 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800821}
822
823static enum ocfs2_contig_type
Tao Ma853a3a12009-08-18 11:22:18 +0800824 ocfs2_extent_rec_contig(struct super_block *sb,
825 struct ocfs2_extent_rec *ext,
826 struct ocfs2_extent_rec *insert_rec)
Mark Fashehdcd05382007-01-16 11:32:23 -0800827{
828 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
829
Mark Fasheh328d5752007-06-18 10:48:04 -0700830 /*
831 * Refuse to coalesce extent records with different flag
832 * fields - we don't want to mix unwritten extents with user
833 * data.
834 */
835 if (ext->e_flags != insert_rec->e_flags)
836 return CONTIG_NONE;
837
Mark Fashehdcd05382007-01-16 11:32:23 -0800838 if (ocfs2_extents_adjacent(ext, insert_rec) &&
Joel Beckerb4a17652009-02-13 03:07:09 -0800839 ocfs2_block_extent_contig(sb, ext, blkno))
Mark Fashehdcd05382007-01-16 11:32:23 -0800840 return CONTIG_RIGHT;
841
842 blkno = le64_to_cpu(ext->e_blkno);
843 if (ocfs2_extents_adjacent(insert_rec, ext) &&
Joel Beckerb4a17652009-02-13 03:07:09 -0800844 ocfs2_block_extent_contig(sb, insert_rec, blkno))
Mark Fashehdcd05382007-01-16 11:32:23 -0800845 return CONTIG_LEFT;
846
847 return CONTIG_NONE;
848}
849
850/*
851 * NOTE: We can have pretty much any combination of contiguousness and
852 * appending.
853 *
854 * The usefulness of APPEND_TAIL is more in that it lets us know that
855 * we'll have to update the path to that leaf.
856 */
857enum ocfs2_append_type {
858 APPEND_NONE = 0,
859 APPEND_TAIL,
860};
861
Mark Fasheh328d5752007-06-18 10:48:04 -0700862enum ocfs2_split_type {
863 SPLIT_NONE = 0,
864 SPLIT_LEFT,
865 SPLIT_RIGHT,
866};
867
Mark Fashehdcd05382007-01-16 11:32:23 -0800868struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700869 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800870 enum ocfs2_append_type ins_appending;
871 enum ocfs2_contig_type ins_contig;
872 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800873 int ins_tree_depth;
874};
875
Mark Fasheh328d5752007-06-18 10:48:04 -0700876struct ocfs2_merge_ctxt {
877 enum ocfs2_contig_type c_contig_type;
878 int c_has_empty_extent;
879 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700880};
881
Joel Becker5e965812008-11-13 14:49:16 -0800882static int ocfs2_validate_extent_block(struct super_block *sb,
883 struct buffer_head *bh)
884{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700885 int rc;
Joel Becker5e965812008-11-13 14:49:16 -0800886 struct ocfs2_extent_block *eb =
887 (struct ocfs2_extent_block *)bh->b_data;
888
Joel Becker970e4932008-11-13 14:49:19 -0800889 mlog(0, "Validating extent block %llu\n",
890 (unsigned long long)bh->b_blocknr);
891
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700892 BUG_ON(!buffer_uptodate(bh));
893
894 /*
895 * If the ecc fails, we return the error but otherwise
896 * leave the filesystem running. We know any error is
897 * local to this block.
898 */
899 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
Joel Becker13723d02008-10-17 19:25:01 -0700900 if (rc) {
901 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
902 (unsigned long long)bh->b_blocknr);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700903 return rc;
Joel Becker13723d02008-10-17 19:25:01 -0700904 }
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700905
906 /*
907 * Errors after here are fatal.
908 */
909
Joel Becker5e965812008-11-13 14:49:16 -0800910 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
911 ocfs2_error(sb,
912 "Extent block #%llu has bad signature %.*s",
913 (unsigned long long)bh->b_blocknr, 7,
914 eb->h_signature);
915 return -EINVAL;
916 }
917
918 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
919 ocfs2_error(sb,
920 "Extent block #%llu has an invalid h_blkno "
921 "of %llu",
922 (unsigned long long)bh->b_blocknr,
923 (unsigned long long)le64_to_cpu(eb->h_blkno));
924 return -EINVAL;
925 }
926
927 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
928 ocfs2_error(sb,
929 "Extent block #%llu has an invalid "
930 "h_fs_generation of #%u",
931 (unsigned long long)bh->b_blocknr,
932 le32_to_cpu(eb->h_fs_generation));
933 return -EINVAL;
934 }
935
936 return 0;
937}
938
Joel Becker3d03a302009-02-12 17:49:26 -0800939int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
Joel Becker5e965812008-11-13 14:49:16 -0800940 struct buffer_head **bh)
941{
942 int rc;
943 struct buffer_head *tmp = *bh;
944
Joel Becker3d03a302009-02-12 17:49:26 -0800945 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800946 ocfs2_validate_extent_block);
Joel Becker5e965812008-11-13 14:49:16 -0800947
948 /* If ocfs2_read_block() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -0800949 if (!rc && !*bh)
Joel Becker5e965812008-11-13 14:49:16 -0800950 *bh = tmp;
951
Joel Becker5e965812008-11-13 14:49:16 -0800952 return rc;
953}
954
955
Mark Fashehccd979b2005-12-15 14:31:24 -0800956/*
957 * How many free extents have we got before we need more meta data?
958 */
959int ocfs2_num_free_extents(struct ocfs2_super *osb,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700960 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -0800961{
962 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800963 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800964 struct ocfs2_extent_block *eb;
965 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800966 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800967
Joel Beckerf99b9b72008-08-20 19:36:33 -0700968 el = et->et_root_el;
969 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800970
Tao Mae7d4cb62008-08-18 17:38:44 +0800971 if (last_eb_blk) {
Joel Becker3d03a302009-02-12 17:49:26 -0800972 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
973 &eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800974 if (retval < 0) {
975 mlog_errno(retval);
976 goto bail;
977 }
978 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
979 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800980 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800981
982 BUG_ON(el->l_tree_depth != 0);
983
984 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
985bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700986 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800987
988 mlog_exit(retval);
989 return retval;
990}
991
992/* expects array to already be allocated
993 *
994 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
995 * l_count for you
996 */
Joel Becker42a5a7a2009-02-12 18:49:19 -0800997static int ocfs2_create_new_meta_bhs(handle_t *handle,
998 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -0800999 int wanted,
1000 struct ocfs2_alloc_context *meta_ac,
1001 struct buffer_head *bhs[])
1002{
1003 int count, status, i;
1004 u16 suballoc_bit_start;
1005 u32 num_got;
Joel Becker2b6cb572010-03-26 10:09:15 +08001006 u64 suballoc_loc, first_blkno;
Joel Becker42a5a7a2009-02-12 18:49:19 -08001007 struct ocfs2_super *osb =
1008 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001009 struct ocfs2_extent_block *eb;
1010
Mark Fashehccd979b2005-12-15 14:31:24 -08001011 count = 0;
1012 while (count < wanted) {
Joel Becker1ed9b772010-05-06 13:59:06 +08001013 status = ocfs2_claim_metadata(handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001014 meta_ac,
1015 wanted - count,
Joel Becker2b6cb572010-03-26 10:09:15 +08001016 &suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08001017 &suballoc_bit_start,
1018 &num_got,
1019 &first_blkno);
1020 if (status < 0) {
1021 mlog_errno(status);
1022 goto bail;
1023 }
1024
1025 for(i = count; i < (num_got + count); i++) {
1026 bhs[i] = sb_getblk(osb->sb, first_blkno);
1027 if (bhs[i] == NULL) {
1028 status = -EIO;
1029 mlog_errno(status);
1030 goto bail;
1031 }
Joel Becker42a5a7a2009-02-12 18:49:19 -08001032 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001033
Joel Becker42a5a7a2009-02-12 18:49:19 -08001034 status = ocfs2_journal_access_eb(handle, et->et_ci,
1035 bhs[i],
Joel Becker13723d02008-10-17 19:25:01 -07001036 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001037 if (status < 0) {
1038 mlog_errno(status);
1039 goto bail;
1040 }
1041
1042 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
1043 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
1044 /* Ok, setup the minimal stuff here. */
1045 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
1046 eb->h_blkno = cpu_to_le64(first_blkno);
1047 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Tiger Yangb89c5422010-01-25 14:11:06 +08001048 eb->h_suballoc_slot =
1049 cpu_to_le16(meta_ac->ac_alloc_slot);
Joel Becker2b6cb572010-03-26 10:09:15 +08001050 eb->h_suballoc_loc = cpu_to_le64(suballoc_loc);
Mark Fashehccd979b2005-12-15 14:31:24 -08001051 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1052 eb->h_list.l_count =
1053 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1054
1055 suballoc_bit_start++;
1056 first_blkno++;
1057
1058 /* We'll also be dirtied by the caller, so
1059 * this isn't absolutely necessary. */
Joel Beckerec20cec2010-03-19 14:13:52 -07001060 ocfs2_journal_dirty(handle, bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001061 }
1062
1063 count += num_got;
1064 }
1065
1066 status = 0;
1067bail:
1068 if (status < 0) {
1069 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001070 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001071 bhs[i] = NULL;
1072 }
1073 }
1074 mlog_exit(status);
1075 return status;
1076}
1077
1078/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001079 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1080 *
1081 * Returns the sum of the rightmost extent rec logical offset and
1082 * cluster count.
1083 *
1084 * ocfs2_add_branch() uses this to determine what logical cluster
1085 * value should be populated into the leftmost new branch records.
1086 *
1087 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1088 * value for the new topmost tree record.
1089 */
1090static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1091{
1092 int i;
1093
1094 i = le16_to_cpu(el->l_next_free_rec) - 1;
1095
1096 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001097 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08001098}
1099
1100/*
Tao Ma6b791bc2009-06-12 14:18:36 +08001101 * Change range of the branches in the right most path according to the leaf
1102 * extent block's rightmost record.
1103 */
1104static int ocfs2_adjust_rightmost_branch(handle_t *handle,
Tao Ma6b791bc2009-06-12 14:18:36 +08001105 struct ocfs2_extent_tree *et)
1106{
1107 int status;
1108 struct ocfs2_path *path = NULL;
1109 struct ocfs2_extent_list *el;
1110 struct ocfs2_extent_rec *rec;
1111
1112 path = ocfs2_new_path_from_et(et);
1113 if (!path) {
1114 status = -ENOMEM;
1115 return status;
1116 }
1117
Joel Beckerfacdb772009-02-12 18:08:48 -08001118 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
Tao Ma6b791bc2009-06-12 14:18:36 +08001119 if (status < 0) {
1120 mlog_errno(status);
1121 goto out;
1122 }
1123
Tao Mac901fb02010-04-26 14:34:57 +08001124 status = ocfs2_extend_trans(handle, path_num_items(path));
Tao Ma6b791bc2009-06-12 14:18:36 +08001125 if (status < 0) {
1126 mlog_errno(status);
1127 goto out;
1128 }
1129
Joel Beckerd401dc12009-02-13 02:24:10 -08001130 status = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma6b791bc2009-06-12 14:18:36 +08001131 if (status < 0) {
1132 mlog_errno(status);
1133 goto out;
1134 }
1135
1136 el = path_leaf_el(path);
1137 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1138
Joel Beckerd401dc12009-02-13 02:24:10 -08001139 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Tao Ma6b791bc2009-06-12 14:18:36 +08001140
1141out:
1142 ocfs2_free_path(path);
1143 return status;
1144}
1145
1146/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001147 * Add an entire tree branch to our inode. eb_bh is the extent block
Joel Beckerd401dc12009-02-13 02:24:10 -08001148 * to start at, if we don't want to start the branch at the root
Mark Fashehccd979b2005-12-15 14:31:24 -08001149 * structure.
1150 *
1151 * last_eb_bh is required as we have to update it's next_leaf pointer
1152 * for the new last extent block.
1153 *
1154 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -08001155 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -08001156 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001157static int ocfs2_add_branch(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001158 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001159 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07001160 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001161 struct ocfs2_alloc_context *meta_ac)
1162{
1163 int status, new_blocks, i;
1164 u64 next_blkno, new_last_eb_blk;
1165 struct buffer_head *bh;
1166 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001167 struct ocfs2_extent_block *eb;
1168 struct ocfs2_extent_list *eb_el;
1169 struct ocfs2_extent_list *el;
Tao Ma6b791bc2009-06-12 14:18:36 +08001170 u32 new_cpos, root_end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001171
Mark Fasheh328d5752007-06-18 10:48:04 -07001172 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001173
Mark Fashehccd979b2005-12-15 14:31:24 -08001174 if (eb_bh) {
1175 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1176 el = &eb->h_list;
1177 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001178 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001179
1180 /* we never add a branch to a leaf. */
1181 BUG_ON(!el->l_tree_depth);
1182
1183 new_blocks = le16_to_cpu(el->l_tree_depth);
1184
Tao Ma6b791bc2009-06-12 14:18:36 +08001185 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1186 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1187 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1188
1189 /*
1190 * If there is a gap before the root end and the real end
1191 * of the righmost leaf block, we need to remove the gap
1192 * between new_cpos and root_end first so that the tree
1193 * is consistent after we add a new branch(it will start
1194 * from new_cpos).
1195 */
1196 if (root_end > new_cpos) {
1197 mlog(0, "adjust the cluster end from %u to %u\n",
1198 root_end, new_cpos);
Joel Beckerd401dc12009-02-13 02:24:10 -08001199 status = ocfs2_adjust_rightmost_branch(handle, et);
Tao Ma6b791bc2009-06-12 14:18:36 +08001200 if (status) {
1201 mlog_errno(status);
1202 goto bail;
1203 }
1204 }
1205
Mark Fashehccd979b2005-12-15 14:31:24 -08001206 /* allocate the number of new eb blocks we need */
1207 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1208 GFP_KERNEL);
1209 if (!new_eb_bhs) {
1210 status = -ENOMEM;
1211 mlog_errno(status);
1212 goto bail;
1213 }
1214
Joel Becker42a5a7a2009-02-12 18:49:19 -08001215 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
Mark Fashehccd979b2005-12-15 14:31:24 -08001216 meta_ac, new_eb_bhs);
1217 if (status < 0) {
1218 mlog_errno(status);
1219 goto bail;
1220 }
1221
1222 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1223 * linked with the rest of the tree.
1224 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1225 *
1226 * when we leave the loop, new_last_eb_blk will point to the
1227 * newest leaf, and next_blkno will point to the topmost extent
1228 * block. */
1229 next_blkno = new_last_eb_blk = 0;
1230 for(i = 0; i < new_blocks; i++) {
1231 bh = new_eb_bhs[i];
1232 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001233 /* ocfs2_create_new_meta_bhs() should create it right! */
1234 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001235 eb_el = &eb->h_list;
1236
Joel Beckerd401dc12009-02-13 02:24:10 -08001237 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
Joel Becker13723d02008-10-17 19:25:01 -07001238 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001239 if (status < 0) {
1240 mlog_errno(status);
1241 goto bail;
1242 }
1243
1244 eb->h_next_leaf_blk = 0;
1245 eb_el->l_tree_depth = cpu_to_le16(i);
1246 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001247 /*
1248 * This actually counts as an empty extent as
1249 * c_clusters == 0
1250 */
1251 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001252 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001253 /*
1254 * eb_el isn't always an interior node, but even leaf
1255 * nodes want a zero'd flags and reserved field so
1256 * this gets the whole 32 bits regardless of use.
1257 */
1258 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001259 if (!eb_el->l_tree_depth)
1260 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1261
Joel Beckerec20cec2010-03-19 14:13:52 -07001262 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001263 next_blkno = le64_to_cpu(eb->h_blkno);
1264 }
1265
1266 /* This is a bit hairy. We want to update up to three blocks
1267 * here without leaving any of them in an inconsistent state
1268 * in case of error. We don't have to worry about
1269 * journal_dirty erroring as it won't unless we've aborted the
1270 * handle (in which case we would never be here) so reserving
1271 * the write with journal_access is all we need to do. */
Joel Beckerd401dc12009-02-13 02:24:10 -08001272 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001273 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001274 if (status < 0) {
1275 mlog_errno(status);
1276 goto bail;
1277 }
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001278 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001279 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001280 if (status < 0) {
1281 mlog_errno(status);
1282 goto bail;
1283 }
1284 if (eb_bh) {
Joel Beckerd401dc12009-02-13 02:24:10 -08001285 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001286 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001287 if (status < 0) {
1288 mlog_errno(status);
1289 goto bail;
1290 }
1291 }
1292
1293 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001294 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001295 i = le16_to_cpu(el->l_next_free_rec);
1296 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001297 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001298 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001299 le16_add_cpu(&el->l_next_free_rec, 1);
1300
1301 /* fe needs a new last extent block pointer, as does the
1302 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001303 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001304
Mark Fasheh328d5752007-06-18 10:48:04 -07001305 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001306 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1307
Joel Beckerec20cec2010-03-19 14:13:52 -07001308 ocfs2_journal_dirty(handle, *last_eb_bh);
1309 ocfs2_journal_dirty(handle, et->et_root_bh);
1310 if (eb_bh)
1311 ocfs2_journal_dirty(handle, eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001312
Mark Fasheh328d5752007-06-18 10:48:04 -07001313 /*
1314 * Some callers want to track the rightmost leaf so pass it
1315 * back here.
1316 */
1317 brelse(*last_eb_bh);
1318 get_bh(new_eb_bhs[0]);
1319 *last_eb_bh = new_eb_bhs[0];
1320
Mark Fashehccd979b2005-12-15 14:31:24 -08001321 status = 0;
1322bail:
1323 if (new_eb_bhs) {
1324 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001325 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001326 kfree(new_eb_bhs);
1327 }
1328
1329 mlog_exit(status);
1330 return status;
1331}
1332
1333/*
1334 * adds another level to the allocation tree.
1335 * returns back the new extent block so you can add a branch to it
1336 * after this call.
1337 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001338static int ocfs2_shift_tree_depth(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001339 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001340 struct ocfs2_alloc_context *meta_ac,
1341 struct buffer_head **ret_new_eb_bh)
1342{
1343 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001344 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001345 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001346 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001347 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001348 struct ocfs2_extent_list *eb_el;
1349
Joel Becker42a5a7a2009-02-12 18:49:19 -08001350 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
Mark Fashehccd979b2005-12-15 14:31:24 -08001351 &new_eb_bh);
1352 if (status < 0) {
1353 mlog_errno(status);
1354 goto bail;
1355 }
1356
1357 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001358 /* ocfs2_create_new_meta_bhs() should create it right! */
1359 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001360
1361 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001362 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001363
Joel Beckerd401dc12009-02-13 02:24:10 -08001364 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001365 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001366 if (status < 0) {
1367 mlog_errno(status);
1368 goto bail;
1369 }
1370
Tao Mae7d4cb62008-08-18 17:38:44 +08001371 /* copy the root extent list data into the new extent block */
1372 eb_el->l_tree_depth = root_el->l_tree_depth;
1373 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1374 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1375 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001376
Joel Beckerec20cec2010-03-19 14:13:52 -07001377 ocfs2_journal_dirty(handle, new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001378
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001379 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001380 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001381 if (status < 0) {
1382 mlog_errno(status);
1383 goto bail;
1384 }
1385
Mark Fashehdcd05382007-01-16 11:32:23 -08001386 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1387
Tao Mae7d4cb62008-08-18 17:38:44 +08001388 /* update root_bh now */
1389 le16_add_cpu(&root_el->l_tree_depth, 1);
1390 root_el->l_recs[0].e_cpos = 0;
1391 root_el->l_recs[0].e_blkno = eb->h_blkno;
1392 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1393 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1394 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1395 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001396
1397 /* If this is our 1st tree depth shift, then last_eb_blk
1398 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001399 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001400 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001401
Joel Beckerec20cec2010-03-19 14:13:52 -07001402 ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001403
1404 *ret_new_eb_bh = new_eb_bh;
1405 new_eb_bh = NULL;
1406 status = 0;
1407bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001408 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001409
1410 mlog_exit(status);
1411 return status;
1412}
1413
1414/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001415 * Should only be called when there is no space left in any of the
1416 * leaf nodes. What we want to do is find the lowest tree depth
1417 * non-leaf extent block with room for new records. There are three
1418 * valid results of this search:
1419 *
1420 * 1) a lowest extent block is found, then we pass it back in
1421 * *lowest_eb_bh and return '0'
1422 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001423 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001424 * pass NULL back in *lowest_eb_bh, but still return '0'
1425 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001426 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001427 * which case we return > 0
1428 *
1429 * return status < 0 indicates an error.
1430 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001431static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001432 struct buffer_head **target_bh)
1433{
1434 int status = 0, i;
1435 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001436 struct ocfs2_extent_block *eb;
1437 struct ocfs2_extent_list *el;
1438 struct buffer_head *bh = NULL;
1439 struct buffer_head *lowest_bh = NULL;
1440
Mark Fashehccd979b2005-12-15 14:31:24 -08001441 *target_bh = NULL;
1442
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001443 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001444
1445 while(le16_to_cpu(el->l_tree_depth) > 1) {
1446 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08001447 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1448 "Owner %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001449 "extent list (next_free_rec == 0)",
Joel Becker3d03a302009-02-12 17:49:26 -08001450 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001451 status = -EIO;
1452 goto bail;
1453 }
1454 i = le16_to_cpu(el->l_next_free_rec) - 1;
1455 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1456 if (!blkno) {
Joel Becker3d03a302009-02-12 17:49:26 -08001457 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1458 "Owner %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001459 "list where extent # %d has no physical "
1460 "block start",
Joel Becker3d03a302009-02-12 17:49:26 -08001461 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001462 status = -EIO;
1463 goto bail;
1464 }
1465
Mark Fasheha81cb882008-10-07 14:25:16 -07001466 brelse(bh);
1467 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001468
Joel Becker3d03a302009-02-12 17:49:26 -08001469 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001470 if (status < 0) {
1471 mlog_errno(status);
1472 goto bail;
1473 }
1474
1475 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001476 el = &eb->h_list;
1477
1478 if (le16_to_cpu(el->l_next_free_rec) <
1479 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001480 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001481 lowest_bh = bh;
1482 get_bh(lowest_bh);
1483 }
1484 }
1485
1486 /* If we didn't find one and the fe doesn't have any room,
1487 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001488 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001489 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001490 status = 1;
1491
1492 *target_bh = lowest_bh;
1493bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001494 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001495
1496 mlog_exit(status);
1497 return status;
1498}
1499
Mark Fashehe48edee2007-03-07 16:46:57 -08001500/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001501 * Grow a b-tree so that it has more records.
1502 *
1503 * We might shift the tree depth in which case existing paths should
1504 * be considered invalid.
1505 *
1506 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001507 *
1508 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001509 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001510static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1511 int *final_depth, struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001512 struct ocfs2_alloc_context *meta_ac)
1513{
1514 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001515 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001516 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001517 struct buffer_head *bh = NULL;
1518
1519 BUG_ON(meta_ac == NULL);
1520
Joel Beckerd401dc12009-02-13 02:24:10 -08001521 shift = ocfs2_find_branch_target(et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001522 if (shift < 0) {
1523 ret = shift;
1524 mlog_errno(ret);
1525 goto out;
1526 }
1527
1528 /* We traveled all the way to the bottom of the allocation tree
1529 * and didn't find room for any more extents - we need to add
1530 * another tree level */
1531 if (shift) {
1532 BUG_ON(bh);
1533 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1534
1535 /* ocfs2_shift_tree_depth will return us a buffer with
1536 * the new extent block (so we can pass that to
1537 * ocfs2_add_branch). */
Joel Beckerd401dc12009-02-13 02:24:10 -08001538 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001539 if (ret < 0) {
1540 mlog_errno(ret);
1541 goto out;
1542 }
1543 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001544 if (depth == 1) {
1545 /*
1546 * Special case: we have room now if we shifted from
1547 * tree_depth 0, so no more work needs to be done.
1548 *
1549 * We won't be calling add_branch, so pass
1550 * back *last_eb_bh as the new leaf. At depth
1551 * zero, it should always be null so there's
1552 * no reason to brelse.
1553 */
1554 BUG_ON(*last_eb_bh);
1555 get_bh(bh);
1556 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001557 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001558 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001559 }
1560
1561 /* call ocfs2_add_branch to add the final part of the tree with
1562 * the new data. */
1563 mlog(0, "add branch. bh = %p\n", bh);
Joel Beckerd401dc12009-02-13 02:24:10 -08001564 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001565 meta_ac);
1566 if (ret < 0) {
1567 mlog_errno(ret);
1568 goto out;
1569 }
1570
1571out:
1572 if (final_depth)
1573 *final_depth = depth;
1574 brelse(bh);
1575 return ret;
1576}
1577
1578/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001579 * This function will discard the rightmost extent record.
1580 */
1581static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1582{
1583 int next_free = le16_to_cpu(el->l_next_free_rec);
1584 int count = le16_to_cpu(el->l_count);
1585 unsigned int num_bytes;
1586
1587 BUG_ON(!next_free);
1588 /* This will cause us to go off the end of our extent list. */
1589 BUG_ON(next_free >= count);
1590
1591 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1592
1593 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1594}
1595
1596static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1597 struct ocfs2_extent_rec *insert_rec)
1598{
1599 int i, insert_index, next_free, has_empty, num_bytes;
1600 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1601 struct ocfs2_extent_rec *rec;
1602
1603 next_free = le16_to_cpu(el->l_next_free_rec);
1604 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1605
1606 BUG_ON(!next_free);
1607
1608 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001609 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001610
1611 /*
1612 * The easiest way to approach this is to just remove the
1613 * empty extent and temporarily decrement next_free.
1614 */
1615 if (has_empty) {
1616 /*
1617 * If next_free was 1 (only an empty extent), this
1618 * loop won't execute, which is fine. We still want
1619 * the decrement above to happen.
1620 */
1621 for(i = 0; i < (next_free - 1); i++)
1622 el->l_recs[i] = el->l_recs[i+1];
1623
1624 next_free--;
1625 }
1626
1627 /*
1628 * Figure out what the new record index should be.
1629 */
1630 for(i = 0; i < next_free; i++) {
1631 rec = &el->l_recs[i];
1632
1633 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1634 break;
1635 }
1636 insert_index = i;
1637
1638 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1639 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1640
1641 BUG_ON(insert_index < 0);
1642 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1643 BUG_ON(insert_index > next_free);
1644
1645 /*
1646 * No need to memmove if we're just adding to the tail.
1647 */
1648 if (insert_index != next_free) {
1649 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1650
1651 num_bytes = next_free - insert_index;
1652 num_bytes *= sizeof(struct ocfs2_extent_rec);
1653 memmove(&el->l_recs[insert_index + 1],
1654 &el->l_recs[insert_index],
1655 num_bytes);
1656 }
1657
1658 /*
1659 * Either we had an empty extent, and need to re-increment or
1660 * there was no empty extent on a non full rightmost leaf node,
1661 * in which case we still need to increment.
1662 */
1663 next_free++;
1664 el->l_next_free_rec = cpu_to_le16(next_free);
1665 /*
1666 * Make sure none of the math above just messed up our tree.
1667 */
1668 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1669
1670 el->l_recs[insert_index] = *insert_rec;
1671
1672}
1673
Mark Fasheh328d5752007-06-18 10:48:04 -07001674static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1675{
1676 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1677
1678 BUG_ON(num_recs == 0);
1679
1680 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1681 num_recs--;
1682 size = num_recs * sizeof(struct ocfs2_extent_rec);
1683 memmove(&el->l_recs[0], &el->l_recs[1], size);
1684 memset(&el->l_recs[num_recs], 0,
1685 sizeof(struct ocfs2_extent_rec));
1686 el->l_next_free_rec = cpu_to_le16(num_recs);
1687 }
1688}
1689
Mark Fashehdcd05382007-01-16 11:32:23 -08001690/*
1691 * Create an empty extent record .
1692 *
1693 * l_next_free_rec may be updated.
1694 *
1695 * If an empty extent already exists do nothing.
1696 */
1697static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1698{
1699 int next_free = le16_to_cpu(el->l_next_free_rec);
1700
Mark Fashehe48edee2007-03-07 16:46:57 -08001701 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1702
Mark Fashehdcd05382007-01-16 11:32:23 -08001703 if (next_free == 0)
1704 goto set_and_inc;
1705
1706 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1707 return;
1708
1709 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1710 "Asked to create an empty extent in a full list:\n"
1711 "count = %u, tree depth = %u",
1712 le16_to_cpu(el->l_count),
1713 le16_to_cpu(el->l_tree_depth));
1714
1715 ocfs2_shift_records_right(el);
1716
1717set_and_inc:
1718 le16_add_cpu(&el->l_next_free_rec, 1);
1719 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1720}
1721
1722/*
1723 * For a rotation which involves two leaf nodes, the "root node" is
1724 * the lowest level tree node which contains a path to both leafs. This
1725 * resulting set of information can be used to form a complete "subtree"
1726 *
1727 * This function is passed two full paths from the dinode down to a
1728 * pair of adjacent leaves. It's task is to figure out which path
1729 * index contains the subtree root - this can be the root index itself
1730 * in a worst-case rotation.
1731 *
1732 * The array index of the subtree root is passed back.
1733 */
Tao Ma38a04e42009-11-30 14:32:19 +08001734int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1735 struct ocfs2_path *left,
1736 struct ocfs2_path *right)
Mark Fashehdcd05382007-01-16 11:32:23 -08001737{
1738 int i = 0;
1739
1740 /*
1741 * Check that the caller passed in two paths from the same tree.
1742 */
1743 BUG_ON(path_root_bh(left) != path_root_bh(right));
1744
1745 do {
1746 i++;
1747
1748 /*
1749 * The caller didn't pass two adjacent paths.
1750 */
1751 mlog_bug_on_msg(i > left->p_tree_depth,
Joel Becker7dc02802009-02-12 19:20:13 -08001752 "Owner %llu, left depth %u, right depth %u\n"
Mark Fashehdcd05382007-01-16 11:32:23 -08001753 "left leaf blk %llu, right leaf blk %llu\n",
Joel Becker7dc02802009-02-12 19:20:13 -08001754 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1755 left->p_tree_depth, right->p_tree_depth,
Mark Fashehdcd05382007-01-16 11:32:23 -08001756 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1757 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1758 } while (left->p_node[i].bh->b_blocknr ==
1759 right->p_node[i].bh->b_blocknr);
1760
1761 return i - 1;
1762}
1763
1764typedef void (path_insert_t)(void *, struct buffer_head *);
1765
1766/*
1767 * Traverse a btree path in search of cpos, starting at root_el.
1768 *
1769 * This code can be called with a cpos larger than the tree, in which
1770 * case it will return the rightmost path.
1771 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001772static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
Mark Fashehdcd05382007-01-16 11:32:23 -08001773 struct ocfs2_extent_list *root_el, u32 cpos,
1774 path_insert_t *func, void *data)
1775{
1776 int i, ret = 0;
1777 u32 range;
1778 u64 blkno;
1779 struct buffer_head *bh = NULL;
1780 struct ocfs2_extent_block *eb;
1781 struct ocfs2_extent_list *el;
1782 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08001783
1784 el = root_el;
1785 while (el->l_tree_depth) {
1786 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001787 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1788 "Owner %llu has empty extent list at "
Mark Fashehdcd05382007-01-16 11:32:23 -08001789 "depth %u\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001790 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001791 le16_to_cpu(el->l_tree_depth));
1792 ret = -EROFS;
1793 goto out;
1794
1795 }
1796
1797 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1798 rec = &el->l_recs[i];
1799
1800 /*
1801 * In the case that cpos is off the allocation
1802 * tree, this should just wind up returning the
1803 * rightmost record.
1804 */
1805 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001806 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001807 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1808 break;
1809 }
1810
1811 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1812 if (blkno == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001813 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1814 "Owner %llu has bad blkno in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001815 "at depth %u (index %d)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001816 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001817 le16_to_cpu(el->l_tree_depth), i);
1818 ret = -EROFS;
1819 goto out;
1820 }
1821
1822 brelse(bh);
1823 bh = NULL;
Joel Beckerfacdb772009-02-12 18:08:48 -08001824 ret = ocfs2_read_extent_block(ci, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001825 if (ret) {
1826 mlog_errno(ret);
1827 goto out;
1828 }
1829
1830 eb = (struct ocfs2_extent_block *) bh->b_data;
1831 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001832
1833 if (le16_to_cpu(el->l_next_free_rec) >
1834 le16_to_cpu(el->l_count)) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001835 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1836 "Owner %llu has bad count in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001837 "at block %llu (next free=%u, count=%u)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001838 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001839 (unsigned long long)bh->b_blocknr,
1840 le16_to_cpu(el->l_next_free_rec),
1841 le16_to_cpu(el->l_count));
1842 ret = -EROFS;
1843 goto out;
1844 }
1845
1846 if (func)
1847 func(data, bh);
1848 }
1849
1850out:
1851 /*
1852 * Catch any trailing bh that the loop didn't handle.
1853 */
1854 brelse(bh);
1855
1856 return ret;
1857}
1858
1859/*
1860 * Given an initialized path (that is, it has a valid root extent
1861 * list), this function will traverse the btree in search of the path
1862 * which would contain cpos.
1863 *
1864 * The path traveled is recorded in the path structure.
1865 *
1866 * Note that this will not do any comparisons on leaf node extent
1867 * records, so it will work fine in the case that we just added a tree
1868 * branch.
1869 */
1870struct find_path_data {
1871 int index;
1872 struct ocfs2_path *path;
1873};
1874static void find_path_ins(void *data, struct buffer_head *bh)
1875{
1876 struct find_path_data *fp = data;
1877
1878 get_bh(bh);
1879 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1880 fp->index++;
1881}
Tao Mae2e9f602009-08-18 11:22:34 +08001882int ocfs2_find_path(struct ocfs2_caching_info *ci,
1883 struct ocfs2_path *path, u32 cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08001884{
1885 struct find_path_data data;
1886
1887 data.index = 1;
1888 data.path = path;
Joel Beckerfacdb772009-02-12 18:08:48 -08001889 return __ocfs2_find_path(ci, path_root_el(path), cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08001890 find_path_ins, &data);
1891}
1892
1893static void find_leaf_ins(void *data, struct buffer_head *bh)
1894{
1895 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1896 struct ocfs2_extent_list *el = &eb->h_list;
1897 struct buffer_head **ret = data;
1898
1899 /* We want to retain only the leaf block. */
1900 if (le16_to_cpu(el->l_tree_depth) == 0) {
1901 get_bh(bh);
1902 *ret = bh;
1903 }
1904}
1905/*
1906 * Find the leaf block in the tree which would contain cpos. No
1907 * checking of the actual leaf is done.
1908 *
1909 * Some paths want to call this instead of allocating a path structure
1910 * and calling ocfs2_find_path().
1911 *
1912 * This function doesn't handle non btree extent lists.
1913 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001914int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1915 struct ocfs2_extent_list *root_el, u32 cpos,
1916 struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001917{
1918 int ret;
1919 struct buffer_head *bh = NULL;
1920
Joel Beckerfacdb772009-02-12 18:08:48 -08001921 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001922 if (ret) {
1923 mlog_errno(ret);
1924 goto out;
1925 }
1926
1927 *leaf_bh = bh;
1928out:
1929 return ret;
1930}
1931
1932/*
1933 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1934 *
1935 * Basically, we've moved stuff around at the bottom of the tree and
1936 * we need to fix up the extent records above the changes to reflect
1937 * the new changes.
1938 *
1939 * left_rec: the record on the left.
1940 * left_child_el: is the child list pointed to by left_rec
1941 * right_rec: the record to the right of left_rec
1942 * right_child_el: is the child list pointed to by right_rec
1943 *
1944 * By definition, this only works on interior nodes.
1945 */
1946static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1947 struct ocfs2_extent_list *left_child_el,
1948 struct ocfs2_extent_rec *right_rec,
1949 struct ocfs2_extent_list *right_child_el)
1950{
1951 u32 left_clusters, right_end;
1952
1953 /*
1954 * Interior nodes never have holes. Their cpos is the cpos of
1955 * the leftmost record in their child list. Their cluster
1956 * count covers the full theoretical range of their child list
1957 * - the range between their cpos and the cpos of the record
1958 * immediately to their right.
1959 */
1960 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08001961 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1962 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07001963 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1964 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1965 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001966 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001967 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001968
1969 /*
1970 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001971 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001972 * updating e_cpos to keep the same highest cluster count.
1973 */
1974 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001975 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001976
1977 right_rec->e_cpos = left_rec->e_cpos;
1978 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1979
1980 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001981 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001982}
1983
1984/*
1985 * Adjust the adjacent root node records involved in a
1986 * rotation. left_el_blkno is passed in as a key so that we can easily
1987 * find it's index in the root list.
1988 */
1989static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1990 struct ocfs2_extent_list *left_el,
1991 struct ocfs2_extent_list *right_el,
1992 u64 left_el_blkno)
1993{
1994 int i;
1995
1996 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1997 le16_to_cpu(left_el->l_tree_depth));
1998
1999 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
2000 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
2001 break;
2002 }
2003
2004 /*
2005 * The path walking code should have never returned a root and
2006 * two paths which are not adjacent.
2007 */
2008 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2009
2010 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
2011 &root_el->l_recs[i + 1], right_el);
2012}
2013
2014/*
2015 * We've changed a leaf block (in right_path) and need to reflect that
2016 * change back up the subtree.
2017 *
2018 * This happens in multiple places:
2019 * - When we've moved an extent record from the left path leaf to the right
2020 * path leaf to make room for an empty extent in the left path leaf.
2021 * - When our insert into the right path leaf is at the leftmost edge
2022 * and requires an update of the path immediately to it's left. This
2023 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08002024 * - When we've adjusted the last extent record in the left path leaf and the
2025 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08002026 */
Joel Becker4619c732009-02-12 19:02:36 -08002027static void ocfs2_complete_edge_insert(handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08002028 struct ocfs2_path *left_path,
2029 struct ocfs2_path *right_path,
2030 int subtree_index)
2031{
Joel Beckerec20cec2010-03-19 14:13:52 -07002032 int i, idx;
Mark Fashehdcd05382007-01-16 11:32:23 -08002033 struct ocfs2_extent_list *el, *left_el, *right_el;
2034 struct ocfs2_extent_rec *left_rec, *right_rec;
2035 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2036
2037 /*
2038 * Update the counts and position values within all the
2039 * interior nodes to reflect the leaf rotation we just did.
2040 *
2041 * The root node is handled below the loop.
2042 *
2043 * We begin the loop with right_el and left_el pointing to the
2044 * leaf lists and work our way up.
2045 *
2046 * NOTE: within this loop, left_el and right_el always refer
2047 * to the *child* lists.
2048 */
2049 left_el = path_leaf_el(left_path);
2050 right_el = path_leaf_el(right_path);
2051 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2052 mlog(0, "Adjust records at index %u\n", i);
2053
2054 /*
2055 * One nice property of knowing that all of these
2056 * nodes are below the root is that we only deal with
2057 * the leftmost right node record and the rightmost
2058 * left node record.
2059 */
2060 el = left_path->p_node[i].el;
2061 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2062 left_rec = &el->l_recs[idx];
2063
2064 el = right_path->p_node[i].el;
2065 right_rec = &el->l_recs[0];
2066
2067 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2068 right_el);
2069
Joel Beckerec20cec2010-03-19 14:13:52 -07002070 ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2071 ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002072
2073 /*
2074 * Setup our list pointers now so that the current
2075 * parents become children in the next iteration.
2076 */
2077 left_el = left_path->p_node[i].el;
2078 right_el = right_path->p_node[i].el;
2079 }
2080
2081 /*
2082 * At the root node, adjust the two adjacent records which
2083 * begin our path to the leaves.
2084 */
2085
2086 el = left_path->p_node[subtree_index].el;
2087 left_el = left_path->p_node[subtree_index + 1].el;
2088 right_el = right_path->p_node[subtree_index + 1].el;
2089
2090 ocfs2_adjust_root_records(el, left_el, right_el,
2091 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2092
2093 root_bh = left_path->p_node[subtree_index].bh;
2094
Joel Beckerec20cec2010-03-19 14:13:52 -07002095 ocfs2_journal_dirty(handle, root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002096}
2097
Joel Becker5c601ab2009-02-12 19:10:13 -08002098static int ocfs2_rotate_subtree_right(handle_t *handle,
2099 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08002100 struct ocfs2_path *left_path,
2101 struct ocfs2_path *right_path,
2102 int subtree_index)
2103{
2104 int ret, i;
2105 struct buffer_head *right_leaf_bh;
2106 struct buffer_head *left_leaf_bh = NULL;
2107 struct buffer_head *root_bh;
2108 struct ocfs2_extent_list *right_el, *left_el;
2109 struct ocfs2_extent_rec move_rec;
2110
2111 left_leaf_bh = path_leaf_bh(left_path);
2112 left_el = path_leaf_el(left_path);
2113
2114 if (left_el->l_next_free_rec != left_el->l_count) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002115 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002116 "Inode %llu has non-full interior leaf node %llu"
2117 "(next free = %u)",
Joel Becker5c601ab2009-02-12 19:10:13 -08002118 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002119 (unsigned long long)left_leaf_bh->b_blocknr,
2120 le16_to_cpu(left_el->l_next_free_rec));
2121 return -EROFS;
2122 }
2123
2124 /*
2125 * This extent block may already have an empty record, so we
2126 * return early if so.
2127 */
2128 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2129 return 0;
2130
2131 root_bh = left_path->p_node[subtree_index].bh;
2132 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2133
Joel Becker5c601ab2009-02-12 19:10:13 -08002134 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002135 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002136 if (ret) {
2137 mlog_errno(ret);
2138 goto out;
2139 }
2140
2141 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002142 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002143 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002144 if (ret) {
2145 mlog_errno(ret);
2146 goto out;
2147 }
2148
Joel Becker5c601ab2009-02-12 19:10:13 -08002149 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002150 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002151 if (ret) {
2152 mlog_errno(ret);
2153 goto out;
2154 }
2155 }
2156
2157 right_leaf_bh = path_leaf_bh(right_path);
2158 right_el = path_leaf_el(right_path);
2159
2160 /* This is a code error, not a disk corruption. */
2161 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2162 "because rightmost leaf block %llu is empty\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002163 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002164 (unsigned long long)right_leaf_bh->b_blocknr);
2165
2166 ocfs2_create_empty_extent(right_el);
2167
Joel Beckerec20cec2010-03-19 14:13:52 -07002168 ocfs2_journal_dirty(handle, right_leaf_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002169
2170 /* Do the copy now. */
2171 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2172 move_rec = left_el->l_recs[i];
2173 right_el->l_recs[0] = move_rec;
2174
2175 /*
2176 * Clear out the record we just copied and shift everything
2177 * over, leaving an empty extent in the left leaf.
2178 *
2179 * We temporarily subtract from next_free_rec so that the
2180 * shift will lose the tail record (which is now defunct).
2181 */
2182 le16_add_cpu(&left_el->l_next_free_rec, -1);
2183 ocfs2_shift_records_right(left_el);
2184 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2185 le16_add_cpu(&left_el->l_next_free_rec, 1);
2186
Joel Beckerec20cec2010-03-19 14:13:52 -07002187 ocfs2_journal_dirty(handle, left_leaf_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002188
Joel Becker4619c732009-02-12 19:02:36 -08002189 ocfs2_complete_edge_insert(handle, left_path, right_path,
2190 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002191
2192out:
2193 return ret;
2194}
2195
2196/*
2197 * Given a full path, determine what cpos value would return us a path
2198 * containing the leaf immediately to the left of the current one.
2199 *
2200 * Will return zero if the path passed in is already the leftmost path.
2201 */
Tristan Yeee149a72010-05-11 17:54:44 +08002202int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2203 struct ocfs2_path *path, u32 *cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08002204{
2205 int i, j, ret = 0;
2206 u64 blkno;
2207 struct ocfs2_extent_list *el;
2208
Mark Fashehe48edee2007-03-07 16:46:57 -08002209 BUG_ON(path->p_tree_depth == 0);
2210
Mark Fashehdcd05382007-01-16 11:32:23 -08002211 *cpos = 0;
2212
2213 blkno = path_leaf_bh(path)->b_blocknr;
2214
2215 /* Start at the tree node just above the leaf and work our way up. */
2216 i = path->p_tree_depth - 1;
2217 while (i >= 0) {
2218 el = path->p_node[i].el;
2219
2220 /*
2221 * Find the extent record just before the one in our
2222 * path.
2223 */
2224 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2225 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2226 if (j == 0) {
2227 if (i == 0) {
2228 /*
2229 * We've determined that the
2230 * path specified is already
2231 * the leftmost one - return a
2232 * cpos of zero.
2233 */
2234 goto out;
2235 }
2236 /*
2237 * The leftmost record points to our
2238 * leaf - we need to travel up the
2239 * tree one level.
2240 */
2241 goto next_node;
2242 }
2243
2244 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002245 *cpos = *cpos + ocfs2_rec_clusters(el,
2246 &el->l_recs[j - 1]);
2247 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002248 goto out;
2249 }
2250 }
2251
2252 /*
2253 * If we got here, we never found a valid node where
2254 * the tree indicated one should be.
2255 */
2256 ocfs2_error(sb,
2257 "Invalid extent tree at extent block %llu\n",
2258 (unsigned long long)blkno);
2259 ret = -EROFS;
2260 goto out;
2261
2262next_node:
2263 blkno = path->p_node[i].bh->b_blocknr;
2264 i--;
2265 }
2266
2267out:
2268 return ret;
2269}
2270
Mark Fasheh328d5752007-06-18 10:48:04 -07002271/*
2272 * Extend the transaction by enough credits to complete the rotation,
2273 * and still leave at least the original number of credits allocated
2274 * to this transaction.
2275 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002276static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002277 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002278 struct ocfs2_path *path)
2279{
Tao Mac901fb02010-04-26 14:34:57 +08002280 int ret = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07002281 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002282
Tao Mac901fb02010-04-26 14:34:57 +08002283 if (handle->h_buffer_credits < credits)
Tao Mac18b8122009-08-18 11:44:07 +08002284 ret = ocfs2_extend_trans(handle,
2285 credits - handle->h_buffer_credits);
Tao Mac18b8122009-08-18 11:44:07 +08002286
Tao Mac901fb02010-04-26 14:34:57 +08002287 return ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08002288}
2289
2290/*
2291 * Trap the case where we're inserting into the theoretical range past
2292 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2293 * whose cpos is less than ours into the right leaf.
2294 *
2295 * It's only necessary to look at the rightmost record of the left
2296 * leaf because the logic that calls us should ensure that the
2297 * theoretical ranges in the path components above the leaves are
2298 * correct.
2299 */
2300static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2301 u32 insert_cpos)
2302{
2303 struct ocfs2_extent_list *left_el;
2304 struct ocfs2_extent_rec *rec;
2305 int next_free;
2306
2307 left_el = path_leaf_el(left_path);
2308 next_free = le16_to_cpu(left_el->l_next_free_rec);
2309 rec = &left_el->l_recs[next_free - 1];
2310
2311 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2312 return 1;
2313 return 0;
2314}
2315
Mark Fasheh328d5752007-06-18 10:48:04 -07002316static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2317{
2318 int next_free = le16_to_cpu(el->l_next_free_rec);
2319 unsigned int range;
2320 struct ocfs2_extent_rec *rec;
2321
2322 if (next_free == 0)
2323 return 0;
2324
2325 rec = &el->l_recs[0];
2326 if (ocfs2_is_empty_extent(rec)) {
2327 /* Empty list. */
2328 if (next_free == 1)
2329 return 0;
2330 rec = &el->l_recs[1];
2331 }
2332
2333 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2334 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2335 return 1;
2336 return 0;
2337}
2338
Mark Fashehdcd05382007-01-16 11:32:23 -08002339/*
2340 * Rotate all the records in a btree right one record, starting at insert_cpos.
2341 *
2342 * The path to the rightmost leaf should be passed in.
2343 *
2344 * The array is assumed to be large enough to hold an entire path (tree depth).
2345 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002346 * Upon successful return from this function:
Mark Fashehdcd05382007-01-16 11:32:23 -08002347 *
2348 * - The 'right_path' array will contain a path to the leaf block
2349 * whose range contains e_cpos.
2350 * - That leaf block will have a single empty extent in list index 0.
2351 * - In the case that the rotation requires a post-insert update,
2352 * *ret_left_path will contain a valid path which can be passed to
2353 * ocfs2_insert_path().
2354 */
Joel Becker1bbf0b82009-02-12 19:42:08 -08002355static int ocfs2_rotate_tree_right(handle_t *handle,
Joel Becker5c601ab2009-02-12 19:10:13 -08002356 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002357 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002358 u32 insert_cpos,
2359 struct ocfs2_path *right_path,
2360 struct ocfs2_path **ret_left_path)
2361{
Mark Fasheh328d5752007-06-18 10:48:04 -07002362 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002363 u32 cpos;
2364 struct ocfs2_path *left_path = NULL;
Joel Becker5c601ab2009-02-12 19:10:13 -08002365 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehdcd05382007-01-16 11:32:23 -08002366
2367 *ret_left_path = NULL;
2368
Joel Beckerffdd7a52008-10-17 22:32:01 -07002369 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002370 if (!left_path) {
2371 ret = -ENOMEM;
2372 mlog_errno(ret);
2373 goto out;
2374 }
2375
Joel Becker5c601ab2009-02-12 19:10:13 -08002376 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002377 if (ret) {
2378 mlog_errno(ret);
2379 goto out;
2380 }
2381
2382 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2383
2384 /*
2385 * What we want to do here is:
2386 *
2387 * 1) Start with the rightmost path.
2388 *
2389 * 2) Determine a path to the leaf block directly to the left
2390 * of that leaf.
2391 *
2392 * 3) Determine the 'subtree root' - the lowest level tree node
2393 * which contains a path to both leaves.
2394 *
2395 * 4) Rotate the subtree.
2396 *
2397 * 5) Find the next subtree by considering the left path to be
2398 * the new right path.
2399 *
2400 * The check at the top of this while loop also accepts
2401 * insert_cpos == cpos because cpos is only a _theoretical_
2402 * value to get us the left path - insert_cpos might very well
2403 * be filling that hole.
2404 *
2405 * Stop at a cpos of '0' because we either started at the
2406 * leftmost branch (i.e., a tree with one branch and a
2407 * rotation inside of it), or we've gone as far as we can in
2408 * rotating subtrees.
2409 */
2410 while (cpos && insert_cpos <= cpos) {
2411 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2412 insert_cpos, cpos);
2413
Joel Becker5c601ab2009-02-12 19:10:13 -08002414 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002415 if (ret) {
2416 mlog_errno(ret);
2417 goto out;
2418 }
2419
2420 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2421 path_leaf_bh(right_path),
Joel Becker5c601ab2009-02-12 19:10:13 -08002422 "Owner %llu: error during insert of %u "
Mark Fashehdcd05382007-01-16 11:32:23 -08002423 "(left path cpos %u) results in two identical "
2424 "paths ending at %llu\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002425 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2426 insert_cpos, cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08002427 (unsigned long long)
2428 path_leaf_bh(left_path)->b_blocknr);
2429
Mark Fasheh328d5752007-06-18 10:48:04 -07002430 if (split == SPLIT_NONE &&
2431 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002432 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002433
2434 /*
2435 * We've rotated the tree as much as we
2436 * should. The rest is up to
2437 * ocfs2_insert_path() to complete, after the
2438 * record insertion. We indicate this
2439 * situation by returning the left path.
2440 *
2441 * The reason we don't adjust the records here
2442 * before the record insert is that an error
2443 * later might break the rule where a parent
2444 * record e_cpos will reflect the actual
2445 * e_cpos of the 1st nonempty record of the
2446 * child list.
2447 */
2448 *ret_left_path = left_path;
2449 goto out_ret_path;
2450 }
2451
Joel Becker7dc02802009-02-12 19:20:13 -08002452 start = ocfs2_find_subtree_root(et, left_path, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002453
2454 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2455 start,
2456 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2457 right_path->p_tree_depth);
2458
2459 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002460 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002461 if (ret) {
2462 mlog_errno(ret);
2463 goto out;
2464 }
2465
Joel Becker5c601ab2009-02-12 19:10:13 -08002466 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002467 right_path, start);
2468 if (ret) {
2469 mlog_errno(ret);
2470 goto out;
2471 }
2472
Mark Fasheh328d5752007-06-18 10:48:04 -07002473 if (split != SPLIT_NONE &&
2474 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2475 insert_cpos)) {
2476 /*
2477 * A rotate moves the rightmost left leaf
2478 * record over to the leftmost right leaf
2479 * slot. If we're doing an extent split
2480 * instead of a real insert, then we have to
2481 * check that the extent to be split wasn't
2482 * just moved over. If it was, then we can
2483 * exit here, passing left_path back -
2484 * ocfs2_split_extent() is smart enough to
2485 * search both leaves.
2486 */
2487 *ret_left_path = left_path;
2488 goto out_ret_path;
2489 }
2490
Mark Fashehdcd05382007-01-16 11:32:23 -08002491 /*
2492 * There is no need to re-read the next right path
2493 * as we know that it'll be our current left
2494 * path. Optimize by copying values instead.
2495 */
2496 ocfs2_mv_path(right_path, left_path);
2497
Joel Becker5c601ab2009-02-12 19:10:13 -08002498 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002499 if (ret) {
2500 mlog_errno(ret);
2501 goto out;
2502 }
2503 }
2504
2505out:
2506 ocfs2_free_path(left_path);
2507
2508out_ret_path:
2509 return ret;
2510}
2511
Joel Becker09106ba2009-02-12 19:43:57 -08002512static int ocfs2_update_edge_lengths(handle_t *handle,
2513 struct ocfs2_extent_tree *et,
Tao Ma3c5e1062009-07-21 15:42:05 +08002514 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002515{
Tao Ma3c5e1062009-07-21 15:42:05 +08002516 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002517 struct ocfs2_extent_rec *rec;
2518 struct ocfs2_extent_list *el;
2519 struct ocfs2_extent_block *eb;
2520 u32 range;
2521
Tao Ma3c5e1062009-07-21 15:42:05 +08002522 /*
2523 * In normal tree rotation process, we will never touch the
2524 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2525 * doesn't reserve the credits for them either.
2526 *
2527 * But we do have a special case here which will update the rightmost
2528 * records for all the bh in the path.
2529 * So we have to allocate extra credits and access them.
2530 */
Tao Mac901fb02010-04-26 14:34:57 +08002531 ret = ocfs2_extend_trans(handle, subtree_index);
Tao Ma3c5e1062009-07-21 15:42:05 +08002532 if (ret) {
2533 mlog_errno(ret);
2534 goto out;
2535 }
2536
Joel Becker09106ba2009-02-12 19:43:57 -08002537 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002538 if (ret) {
2539 mlog_errno(ret);
2540 goto out;
2541 }
2542
Mark Fasheh328d5752007-06-18 10:48:04 -07002543 /* Path should always be rightmost. */
2544 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2545 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2546
2547 el = &eb->h_list;
2548 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2549 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2550 rec = &el->l_recs[idx];
2551 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2552
2553 for (i = 0; i < path->p_tree_depth; i++) {
2554 el = path->p_node[i].el;
2555 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2556 rec = &el->l_recs[idx];
2557
2558 rec->e_int_clusters = cpu_to_le32(range);
2559 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2560
2561 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2562 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002563out:
2564 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002565}
2566
Joel Becker6641b0c2009-02-12 18:57:52 -08002567static void ocfs2_unlink_path(handle_t *handle,
2568 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002569 struct ocfs2_cached_dealloc_ctxt *dealloc,
2570 struct ocfs2_path *path, int unlink_start)
2571{
2572 int ret, i;
2573 struct ocfs2_extent_block *eb;
2574 struct ocfs2_extent_list *el;
2575 struct buffer_head *bh;
2576
2577 for(i = unlink_start; i < path_num_items(path); i++) {
2578 bh = path->p_node[i].bh;
2579
2580 eb = (struct ocfs2_extent_block *)bh->b_data;
2581 /*
2582 * Not all nodes might have had their final count
2583 * decremented by the caller - handle this here.
2584 */
2585 el = &eb->h_list;
2586 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2587 mlog(ML_ERROR,
2588 "Inode %llu, attempted to remove extent block "
2589 "%llu with %u records\n",
Joel Becker6641b0c2009-02-12 18:57:52 -08002590 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07002591 (unsigned long long)le64_to_cpu(eb->h_blkno),
2592 le16_to_cpu(el->l_next_free_rec));
2593
2594 ocfs2_journal_dirty(handle, bh);
Joel Becker6641b0c2009-02-12 18:57:52 -08002595 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002596 continue;
2597 }
2598
2599 el->l_next_free_rec = 0;
2600 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2601
2602 ocfs2_journal_dirty(handle, bh);
2603
2604 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2605 if (ret)
2606 mlog_errno(ret);
2607
Joel Becker6641b0c2009-02-12 18:57:52 -08002608 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002609 }
2610}
2611
Joel Becker6641b0c2009-02-12 18:57:52 -08002612static void ocfs2_unlink_subtree(handle_t *handle,
2613 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002614 struct ocfs2_path *left_path,
2615 struct ocfs2_path *right_path,
2616 int subtree_index,
2617 struct ocfs2_cached_dealloc_ctxt *dealloc)
2618{
2619 int i;
2620 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2621 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2622 struct ocfs2_extent_list *el;
2623 struct ocfs2_extent_block *eb;
2624
2625 el = path_leaf_el(left_path);
2626
2627 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2628
2629 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2630 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2631 break;
2632
2633 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2634
2635 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2636 le16_add_cpu(&root_el->l_next_free_rec, -1);
2637
2638 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2639 eb->h_next_leaf_blk = 0;
2640
2641 ocfs2_journal_dirty(handle, root_bh);
2642 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2643
Joel Becker6641b0c2009-02-12 18:57:52 -08002644 ocfs2_unlink_path(handle, et, dealloc, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002645 subtree_index + 1);
2646}
2647
Joel Becker1e2dd632009-02-12 19:45:28 -08002648static int ocfs2_rotate_subtree_left(handle_t *handle,
2649 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002650 struct ocfs2_path *left_path,
2651 struct ocfs2_path *right_path,
2652 int subtree_index,
2653 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker1e2dd632009-02-12 19:45:28 -08002654 int *deleted)
Mark Fasheh328d5752007-06-18 10:48:04 -07002655{
2656 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002657 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002658 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2659 struct ocfs2_extent_block *eb;
2660
2661 *deleted = 0;
2662
2663 right_leaf_el = path_leaf_el(right_path);
2664 left_leaf_el = path_leaf_el(left_path);
2665 root_bh = left_path->p_node[subtree_index].bh;
2666 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2667
2668 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2669 return 0;
2670
2671 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2672 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2673 /*
2674 * It's legal for us to proceed if the right leaf is
2675 * the rightmost one and it has an empty extent. There
2676 * are two cases to handle - whether the leaf will be
2677 * empty after removal or not. If the leaf isn't empty
2678 * then just remove the empty extent up front. The
2679 * next block will handle empty leaves by flagging
2680 * them for unlink.
2681 *
2682 * Non rightmost leaves will throw -EAGAIN and the
2683 * caller can manually move the subtree and retry.
2684 */
2685
2686 if (eb->h_next_leaf_blk != 0ULL)
2687 return -EAGAIN;
2688
2689 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002690 ret = ocfs2_journal_access_eb(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002691 path_leaf_bh(right_path),
2692 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002693 if (ret) {
2694 mlog_errno(ret);
2695 goto out;
2696 }
2697
2698 ocfs2_remove_empty_extent(right_leaf_el);
2699 } else
2700 right_has_empty = 1;
2701 }
2702
2703 if (eb->h_next_leaf_blk == 0ULL &&
2704 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2705 /*
2706 * We have to update i_last_eb_blk during the meta
2707 * data delete.
2708 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08002709 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07002710 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002711 if (ret) {
2712 mlog_errno(ret);
2713 goto out;
2714 }
2715
2716 del_right_subtree = 1;
2717 }
2718
2719 /*
2720 * Getting here with an empty extent in the right path implies
2721 * that it's the rightmost path and will be deleted.
2722 */
2723 BUG_ON(right_has_empty && !del_right_subtree);
2724
Joel Becker1e2dd632009-02-12 19:45:28 -08002725 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002726 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002727 if (ret) {
2728 mlog_errno(ret);
2729 goto out;
2730 }
2731
2732 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002733 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002734 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002735 if (ret) {
2736 mlog_errno(ret);
2737 goto out;
2738 }
2739
Joel Becker1e2dd632009-02-12 19:45:28 -08002740 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002741 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002742 if (ret) {
2743 mlog_errno(ret);
2744 goto out;
2745 }
2746 }
2747
2748 if (!right_has_empty) {
2749 /*
2750 * Only do this if we're moving a real
2751 * record. Otherwise, the action is delayed until
2752 * after removal of the right path in which case we
2753 * can do a simple shift to remove the empty extent.
2754 */
2755 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2756 memset(&right_leaf_el->l_recs[0], 0,
2757 sizeof(struct ocfs2_extent_rec));
2758 }
2759 if (eb->h_next_leaf_blk == 0ULL) {
2760 /*
2761 * Move recs over to get rid of empty extent, decrease
2762 * next_free. This is allowed to remove the last
2763 * extent in our leaf (setting l_next_free_rec to
2764 * zero) - the delete code below won't care.
2765 */
2766 ocfs2_remove_empty_extent(right_leaf_el);
2767 }
2768
Joel Beckerec20cec2010-03-19 14:13:52 -07002769 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2770 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
Mark Fasheh328d5752007-06-18 10:48:04 -07002771
2772 if (del_right_subtree) {
Joel Becker6641b0c2009-02-12 18:57:52 -08002773 ocfs2_unlink_subtree(handle, et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002774 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08002775 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08002776 left_path);
2777 if (ret) {
2778 mlog_errno(ret);
2779 goto out;
2780 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002781
2782 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002783 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002784
2785 /*
2786 * Removal of the extent in the left leaf was skipped
2787 * above so we could delete the right path
2788 * 1st.
2789 */
2790 if (right_has_empty)
2791 ocfs2_remove_empty_extent(left_leaf_el);
2792
Joel Beckerec20cec2010-03-19 14:13:52 -07002793 ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002794
2795 *deleted = 1;
2796 } else
Joel Becker4619c732009-02-12 19:02:36 -08002797 ocfs2_complete_edge_insert(handle, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002798 subtree_index);
2799
2800out:
2801 return ret;
2802}
2803
2804/*
2805 * Given a full path, determine what cpos value would return us a path
2806 * containing the leaf immediately to the right of the current one.
2807 *
2808 * Will return zero if the path passed in is already the rightmost path.
2809 *
2810 * This looks similar, but is subtly different to
2811 * ocfs2_find_cpos_for_left_leaf().
2812 */
Tao Ma38a04e42009-11-30 14:32:19 +08002813int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2814 struct ocfs2_path *path, u32 *cpos)
Mark Fasheh328d5752007-06-18 10:48:04 -07002815{
2816 int i, j, ret = 0;
2817 u64 blkno;
2818 struct ocfs2_extent_list *el;
2819
2820 *cpos = 0;
2821
2822 if (path->p_tree_depth == 0)
2823 return 0;
2824
2825 blkno = path_leaf_bh(path)->b_blocknr;
2826
2827 /* Start at the tree node just above the leaf and work our way up. */
2828 i = path->p_tree_depth - 1;
2829 while (i >= 0) {
2830 int next_free;
2831
2832 el = path->p_node[i].el;
2833
2834 /*
2835 * Find the extent record just after the one in our
2836 * path.
2837 */
2838 next_free = le16_to_cpu(el->l_next_free_rec);
2839 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2840 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2841 if (j == (next_free - 1)) {
2842 if (i == 0) {
2843 /*
2844 * We've determined that the
2845 * path specified is already
2846 * the rightmost one - return a
2847 * cpos of zero.
2848 */
2849 goto out;
2850 }
2851 /*
2852 * The rightmost record points to our
2853 * leaf - we need to travel up the
2854 * tree one level.
2855 */
2856 goto next_node;
2857 }
2858
2859 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2860 goto out;
2861 }
2862 }
2863
2864 /*
2865 * If we got here, we never found a valid node where
2866 * the tree indicated one should be.
2867 */
2868 ocfs2_error(sb,
2869 "Invalid extent tree at extent block %llu\n",
2870 (unsigned long long)blkno);
2871 ret = -EROFS;
2872 goto out;
2873
2874next_node:
2875 blkno = path->p_node[i].bh->b_blocknr;
2876 i--;
2877 }
2878
2879out:
2880 return ret;
2881}
2882
Joel Becker70f18c02009-02-13 02:09:31 -08002883static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2884 struct ocfs2_extent_tree *et,
Joel Becker13723d02008-10-17 19:25:01 -07002885 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002886{
2887 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002888 struct buffer_head *bh = path_leaf_bh(path);
2889 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002890
2891 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2892 return 0;
2893
Joel Becker70f18c02009-02-13 02:09:31 -08002894 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Joel Becker13723d02008-10-17 19:25:01 -07002895 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002896 if (ret) {
2897 mlog_errno(ret);
2898 goto out;
2899 }
2900
2901 ocfs2_remove_empty_extent(el);
Joel Beckerec20cec2010-03-19 14:13:52 -07002902 ocfs2_journal_dirty(handle, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002903
2904out:
2905 return ret;
2906}
2907
Joel Beckere46f74d2009-02-12 19:47:43 -08002908static int __ocfs2_rotate_tree_left(handle_t *handle,
2909 struct ocfs2_extent_tree *et,
2910 int orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07002911 struct ocfs2_path *path,
2912 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08002913 struct ocfs2_path **empty_extent_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002914{
2915 int ret, subtree_root, deleted;
2916 u32 right_cpos;
2917 struct ocfs2_path *left_path = NULL;
2918 struct ocfs2_path *right_path = NULL;
Joel Beckere46f74d2009-02-12 19:47:43 -08002919 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fasheh328d5752007-06-18 10:48:04 -07002920
2921 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2922
2923 *empty_extent_path = NULL;
2924
Joel Beckere46f74d2009-02-12 19:47:43 -08002925 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002926 if (ret) {
2927 mlog_errno(ret);
2928 goto out;
2929 }
2930
Joel Beckerffdd7a52008-10-17 22:32:01 -07002931 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002932 if (!left_path) {
2933 ret = -ENOMEM;
2934 mlog_errno(ret);
2935 goto out;
2936 }
2937
2938 ocfs2_cp_path(left_path, path);
2939
Joel Beckerffdd7a52008-10-17 22:32:01 -07002940 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002941 if (!right_path) {
2942 ret = -ENOMEM;
2943 mlog_errno(ret);
2944 goto out;
2945 }
2946
2947 while (right_cpos) {
Joel Beckerfacdb772009-02-12 18:08:48 -08002948 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002949 if (ret) {
2950 mlog_errno(ret);
2951 goto out;
2952 }
2953
Joel Becker7dc02802009-02-12 19:20:13 -08002954 subtree_root = ocfs2_find_subtree_root(et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002955 right_path);
2956
2957 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2958 subtree_root,
2959 (unsigned long long)
2960 right_path->p_node[subtree_root].bh->b_blocknr,
2961 right_path->p_tree_depth);
2962
2963 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2964 orig_credits, left_path);
2965 if (ret) {
2966 mlog_errno(ret);
2967 goto out;
2968 }
2969
Mark Fashehe8aed342007-12-03 16:43:01 -08002970 /*
2971 * Caller might still want to make changes to the
2972 * tree root, so re-add it to the journal here.
2973 */
Joel Beckere46f74d2009-02-12 19:47:43 -08002974 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002975 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08002976 if (ret) {
2977 mlog_errno(ret);
2978 goto out;
2979 }
2980
Joel Becker1e2dd632009-02-12 19:45:28 -08002981 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002982 right_path, subtree_root,
Joel Becker1e2dd632009-02-12 19:45:28 -08002983 dealloc, &deleted);
Mark Fasheh328d5752007-06-18 10:48:04 -07002984 if (ret == -EAGAIN) {
2985 /*
2986 * The rotation has to temporarily stop due to
2987 * the right subtree having an empty
2988 * extent. Pass it back to the caller for a
2989 * fixup.
2990 */
2991 *empty_extent_path = right_path;
2992 right_path = NULL;
2993 goto out;
2994 }
2995 if (ret) {
2996 mlog_errno(ret);
2997 goto out;
2998 }
2999
3000 /*
3001 * The subtree rotate might have removed records on
3002 * the rightmost edge. If so, then rotation is
3003 * complete.
3004 */
3005 if (deleted)
3006 break;
3007
3008 ocfs2_mv_path(left_path, right_path);
3009
Joel Beckere46f74d2009-02-12 19:47:43 -08003010 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003011 &right_cpos);
3012 if (ret) {
3013 mlog_errno(ret);
3014 goto out;
3015 }
3016 }
3017
3018out:
3019 ocfs2_free_path(right_path);
3020 ocfs2_free_path(left_path);
3021
3022 return ret;
3023}
3024
Joel Becker70f18c02009-02-13 02:09:31 -08003025static int ocfs2_remove_rightmost_path(handle_t *handle,
3026 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003027 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003028 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003029{
3030 int ret, subtree_index;
3031 u32 cpos;
3032 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003033 struct ocfs2_extent_block *eb;
3034 struct ocfs2_extent_list *el;
3035
Mark Fasheh328d5752007-06-18 10:48:04 -07003036
Joel Becker6136ca52009-02-12 19:32:43 -08003037 ret = ocfs2_et_sanity_check(et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003038 if (ret)
3039 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003040 /*
3041 * There's two ways we handle this depending on
3042 * whether path is the only existing one.
3043 */
3044 ret = ocfs2_extend_rotate_transaction(handle, 0,
3045 handle->h_buffer_credits,
3046 path);
3047 if (ret) {
3048 mlog_errno(ret);
3049 goto out;
3050 }
3051
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003052 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003053 if (ret) {
3054 mlog_errno(ret);
3055 goto out;
3056 }
3057
Joel Becker3d03a302009-02-12 17:49:26 -08003058 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3059 path, &cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003060 if (ret) {
3061 mlog_errno(ret);
3062 goto out;
3063 }
3064
3065 if (cpos) {
3066 /*
3067 * We have a path to the left of this one - it needs
3068 * an update too.
3069 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003070 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003071 if (!left_path) {
3072 ret = -ENOMEM;
3073 mlog_errno(ret);
3074 goto out;
3075 }
3076
Joel Beckerfacdb772009-02-12 18:08:48 -08003077 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003078 if (ret) {
3079 mlog_errno(ret);
3080 goto out;
3081 }
3082
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003083 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003084 if (ret) {
3085 mlog_errno(ret);
3086 goto out;
3087 }
3088
Joel Becker7dc02802009-02-12 19:20:13 -08003089 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003090
Joel Becker6641b0c2009-02-12 18:57:52 -08003091 ocfs2_unlink_subtree(handle, et, left_path, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003092 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08003093 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08003094 left_path);
3095 if (ret) {
3096 mlog_errno(ret);
3097 goto out;
3098 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003099
3100 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003101 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003102 } else {
3103 /*
3104 * 'path' is also the leftmost path which
3105 * means it must be the only one. This gets
3106 * handled differently because we want to
Joel Becker70f18c02009-02-13 02:09:31 -08003107 * revert the root back to having extents
Mark Fasheh328d5752007-06-18 10:48:04 -07003108 * in-line.
3109 */
Joel Becker6641b0c2009-02-12 18:57:52 -08003110 ocfs2_unlink_path(handle, et, dealloc, path, 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003111
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003112 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003113 el->l_tree_depth = 0;
3114 el->l_next_free_rec = 0;
3115 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3116
Joel Becker35dc0aa2008-08-20 16:25:06 -07003117 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003118 }
3119
3120 ocfs2_journal_dirty(handle, path_root_bh(path));
3121
3122out:
3123 ocfs2_free_path(left_path);
3124 return ret;
3125}
3126
3127/*
3128 * Left rotation of btree records.
3129 *
3130 * In many ways, this is (unsurprisingly) the opposite of right
3131 * rotation. We start at some non-rightmost path containing an empty
3132 * extent in the leaf block. The code works its way to the rightmost
3133 * path by rotating records to the left in every subtree.
3134 *
3135 * This is used by any code which reduces the number of extent records
3136 * in a leaf. After removal, an empty record should be placed in the
3137 * leftmost list position.
3138 *
3139 * This won't handle a length update of the rightmost path records if
3140 * the rightmost tree leaf record is removed so the caller is
3141 * responsible for detecting and correcting that.
3142 */
Joel Becker70f18c02009-02-13 02:09:31 -08003143static int ocfs2_rotate_tree_left(handle_t *handle,
3144 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003145 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003146 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003147{
3148 int ret, orig_credits = handle->h_buffer_credits;
3149 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3150 struct ocfs2_extent_block *eb;
3151 struct ocfs2_extent_list *el;
3152
3153 el = path_leaf_el(path);
3154 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3155 return 0;
3156
3157 if (path->p_tree_depth == 0) {
3158rightmost_no_delete:
3159 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003160 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003161 * it up front.
3162 */
Joel Becker70f18c02009-02-13 02:09:31 -08003163 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003164 if (ret)
3165 mlog_errno(ret);
3166 goto out;
3167 }
3168
3169 /*
3170 * Handle rightmost branch now. There's several cases:
3171 * 1) simple rotation leaving records in there. That's trivial.
3172 * 2) rotation requiring a branch delete - there's no more
3173 * records left. Two cases of this:
3174 * a) There are branches to the left.
3175 * b) This is also the leftmost (the only) branch.
3176 *
3177 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3178 * 2a) we need the left branch so that we can update it with the unlink
Joel Becker70f18c02009-02-13 02:09:31 -08003179 * 2b) we need to bring the root back to inline extents.
Mark Fasheh328d5752007-06-18 10:48:04 -07003180 */
3181
3182 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3183 el = &eb->h_list;
3184 if (eb->h_next_leaf_blk == 0) {
3185 /*
3186 * This gets a bit tricky if we're going to delete the
3187 * rightmost path. Get the other cases out of the way
3188 * 1st.
3189 */
3190 if (le16_to_cpu(el->l_next_free_rec) > 1)
3191 goto rightmost_no_delete;
3192
3193 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3194 ret = -EIO;
Joel Becker70f18c02009-02-13 02:09:31 -08003195 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3196 "Owner %llu has empty extent block at %llu",
3197 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07003198 (unsigned long long)le64_to_cpu(eb->h_blkno));
3199 goto out;
3200 }
3201
3202 /*
3203 * XXX: The caller can not trust "path" any more after
3204 * this as it will have been deleted. What do we do?
3205 *
3206 * In theory the rotate-for-merge code will never get
3207 * here because it'll always ask for a rotate in a
3208 * nonempty list.
3209 */
3210
Joel Becker70f18c02009-02-13 02:09:31 -08003211 ret = ocfs2_remove_rightmost_path(handle, et, path,
3212 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003213 if (ret)
3214 mlog_errno(ret);
3215 goto out;
3216 }
3217
3218 /*
3219 * Now we can loop, remembering the path we get from -EAGAIN
3220 * and restarting from there.
3221 */
3222try_rotate:
Joel Beckere46f74d2009-02-12 19:47:43 -08003223 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3224 dealloc, &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003225 if (ret && ret != -EAGAIN) {
3226 mlog_errno(ret);
3227 goto out;
3228 }
3229
3230 while (ret == -EAGAIN) {
3231 tmp_path = restart_path;
3232 restart_path = NULL;
3233
Joel Beckere46f74d2009-02-12 19:47:43 -08003234 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07003235 tmp_path, dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08003236 &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003237 if (ret && ret != -EAGAIN) {
3238 mlog_errno(ret);
3239 goto out;
3240 }
3241
3242 ocfs2_free_path(tmp_path);
3243 tmp_path = NULL;
3244
3245 if (ret == 0)
3246 goto try_rotate;
3247 }
3248
3249out:
3250 ocfs2_free_path(tmp_path);
3251 ocfs2_free_path(restart_path);
3252 return ret;
3253}
3254
3255static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3256 int index)
3257{
3258 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3259 unsigned int size;
3260
3261 if (rec->e_leaf_clusters == 0) {
3262 /*
3263 * We consumed all of the merged-from record. An empty
3264 * extent cannot exist anywhere but the 1st array
3265 * position, so move things over if the merged-from
3266 * record doesn't occupy that position.
3267 *
3268 * This creates a new empty extent so the caller
3269 * should be smart enough to have removed any existing
3270 * ones.
3271 */
3272 if (index > 0) {
3273 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3274 size = index * sizeof(struct ocfs2_extent_rec);
3275 memmove(&el->l_recs[1], &el->l_recs[0], size);
3276 }
3277
3278 /*
3279 * Always memset - the caller doesn't check whether it
3280 * created an empty extent, so there could be junk in
3281 * the other fields.
3282 */
3283 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3284 }
3285}
3286
Joel Becker4fe82c32009-02-13 02:16:08 -08003287static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003288 struct ocfs2_path *left_path,
3289 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003290{
3291 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003292 u32 right_cpos;
3293 struct ocfs2_path *right_path = NULL;
3294 struct ocfs2_extent_list *left_el;
3295
3296 *ret_right_path = NULL;
3297
3298 /* This function shouldn't be called for non-trees. */
3299 BUG_ON(left_path->p_tree_depth == 0);
3300
3301 left_el = path_leaf_el(left_path);
3302 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3303
Joel Becker4fe82c32009-02-13 02:16:08 -08003304 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3305 left_path, &right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003306 if (ret) {
3307 mlog_errno(ret);
3308 goto out;
3309 }
3310
3311 /* This function shouldn't be called for the rightmost leaf. */
3312 BUG_ON(right_cpos == 0);
3313
Joel Beckerffdd7a52008-10-17 22:32:01 -07003314 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003315 if (!right_path) {
3316 ret = -ENOMEM;
3317 mlog_errno(ret);
3318 goto out;
3319 }
3320
Joel Becker4fe82c32009-02-13 02:16:08 -08003321 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003322 if (ret) {
3323 mlog_errno(ret);
3324 goto out;
3325 }
3326
3327 *ret_right_path = right_path;
3328out:
3329 if (ret)
3330 ocfs2_free_path(right_path);
3331 return ret;
3332}
3333
3334/*
3335 * Remove split_rec clusters from the record at index and merge them
3336 * onto the beginning of the record "next" to it.
3337 * For index < l_count - 1, the next means the extent rec at index + 1.
3338 * For index == l_count - 1, the "next" means the 1st extent rec of the
3339 * next extent block.
3340 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003341static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003342 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003343 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003344 struct ocfs2_extent_rec *split_rec,
3345 int index)
3346{
3347 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003348 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3349 struct ocfs2_extent_rec *left_rec;
3350 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003351 struct ocfs2_extent_list *right_el;
3352 struct ocfs2_path *right_path = NULL;
3353 int subtree_index = 0;
3354 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3355 struct buffer_head *bh = path_leaf_bh(left_path);
3356 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003357
3358 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003359 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003360
Al Viro9d8df6a2008-05-21 06:32:11 +01003361 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003362 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3363 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003364 ret = ocfs2_get_right_path(et, left_path, &right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003365 if (ret) {
3366 mlog_errno(ret);
3367 goto out;
3368 }
3369
3370 right_el = path_leaf_el(right_path);
3371 next_free = le16_to_cpu(right_el->l_next_free_rec);
3372 BUG_ON(next_free <= 0);
3373 right_rec = &right_el->l_recs[0];
3374 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003375 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003376 right_rec = &right_el->l_recs[1];
3377 }
3378
3379 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3380 le16_to_cpu(left_rec->e_leaf_clusters) !=
3381 le32_to_cpu(right_rec->e_cpos));
3382
Joel Becker7dc02802009-02-12 19:20:13 -08003383 subtree_index = ocfs2_find_subtree_root(et, left_path,
3384 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003385
3386 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3387 handle->h_buffer_credits,
3388 right_path);
3389 if (ret) {
3390 mlog_errno(ret);
3391 goto out;
3392 }
3393
3394 root_bh = left_path->p_node[subtree_index].bh;
3395 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3396
Joel Becker7dc02802009-02-12 19:20:13 -08003397 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003398 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003399 if (ret) {
3400 mlog_errno(ret);
3401 goto out;
3402 }
3403
3404 for (i = subtree_index + 1;
3405 i < path_num_items(right_path); i++) {
Joel Becker7dc02802009-02-12 19:20:13 -08003406 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003407 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003408 if (ret) {
3409 mlog_errno(ret);
3410 goto out;
3411 }
3412
Joel Becker7dc02802009-02-12 19:20:13 -08003413 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003414 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003415 if (ret) {
3416 mlog_errno(ret);
3417 goto out;
3418 }
3419 }
3420
3421 } else {
3422 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3423 right_rec = &el->l_recs[index + 1];
3424 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003425
Joel Becker7dc02802009-02-12 19:20:13 -08003426 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
Joel Becker13723d02008-10-17 19:25:01 -07003427 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003428 if (ret) {
3429 mlog_errno(ret);
3430 goto out;
3431 }
3432
3433 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3434
3435 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3436 le64_add_cpu(&right_rec->e_blkno,
Joel Becker7dc02802009-02-12 19:20:13 -08003437 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3438 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003439 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3440
3441 ocfs2_cleanup_merge(el, index);
3442
Joel Beckerec20cec2010-03-19 14:13:52 -07003443 ocfs2_journal_dirty(handle, bh);
Tao Ma677b9752008-01-30 14:21:05 +08003444 if (right_path) {
Joel Beckerec20cec2010-03-19 14:13:52 -07003445 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
Joel Becker4619c732009-02-12 19:02:36 -08003446 ocfs2_complete_edge_insert(handle, left_path, right_path,
3447 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003448 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003449out:
Tao Ma677b9752008-01-30 14:21:05 +08003450 if (right_path)
3451 ocfs2_free_path(right_path);
3452 return ret;
3453}
3454
Joel Becker4fe82c32009-02-13 02:16:08 -08003455static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003456 struct ocfs2_path *right_path,
3457 struct ocfs2_path **ret_left_path)
3458{
3459 int ret;
3460 u32 left_cpos;
3461 struct ocfs2_path *left_path = NULL;
3462
3463 *ret_left_path = NULL;
3464
3465 /* This function shouldn't be called for non-trees. */
3466 BUG_ON(right_path->p_tree_depth == 0);
3467
Joel Becker4fe82c32009-02-13 02:16:08 -08003468 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
Tao Ma677b9752008-01-30 14:21:05 +08003469 right_path, &left_cpos);
3470 if (ret) {
3471 mlog_errno(ret);
3472 goto out;
3473 }
3474
3475 /* This function shouldn't be called for the leftmost leaf. */
3476 BUG_ON(left_cpos == 0);
3477
Joel Beckerffdd7a52008-10-17 22:32:01 -07003478 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003479 if (!left_path) {
3480 ret = -ENOMEM;
3481 mlog_errno(ret);
3482 goto out;
3483 }
3484
Joel Becker4fe82c32009-02-13 02:16:08 -08003485 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003486 if (ret) {
3487 mlog_errno(ret);
3488 goto out;
3489 }
3490
3491 *ret_left_path = left_path;
3492out:
3493 if (ret)
3494 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003495 return ret;
3496}
3497
3498/*
3499 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003500 * onto the tail of the record "before" it.
3501 * For index > 0, the "before" means the extent rec at index - 1.
3502 *
3503 * For index == 0, the "before" means the last record of the previous
3504 * extent block. And there is also a situation that we may need to
3505 * remove the rightmost leaf extent block in the right_path and change
3506 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003507 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003508static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003509 handle_t *handle,
Joel Becker4fe82c32009-02-13 02:16:08 -08003510 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003511 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003512 struct ocfs2_cached_dealloc_ctxt *dealloc,
3513 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003514{
Tao Ma677b9752008-01-30 14:21:05 +08003515 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003516 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3517 struct ocfs2_extent_rec *left_rec;
3518 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003519 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3520 struct buffer_head *bh = path_leaf_bh(right_path);
3521 struct buffer_head *root_bh = NULL;
3522 struct ocfs2_path *left_path = NULL;
3523 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003524
Tao Ma677b9752008-01-30 14:21:05 +08003525 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003526
Mark Fasheh328d5752007-06-18 10:48:04 -07003527 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003528 if (index == 0) {
3529 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003530 ret = ocfs2_get_left_path(et, right_path, &left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003531 if (ret) {
3532 mlog_errno(ret);
3533 goto out;
3534 }
3535
3536 left_el = path_leaf_el(left_path);
3537 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3538 le16_to_cpu(left_el->l_count));
3539
3540 left_rec = &left_el->l_recs[
3541 le16_to_cpu(left_el->l_next_free_rec) - 1];
3542 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3543 le16_to_cpu(left_rec->e_leaf_clusters) !=
3544 le32_to_cpu(split_rec->e_cpos));
3545
Joel Becker7dc02802009-02-12 19:20:13 -08003546 subtree_index = ocfs2_find_subtree_root(et, left_path,
3547 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003548
3549 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3550 handle->h_buffer_credits,
3551 left_path);
3552 if (ret) {
3553 mlog_errno(ret);
3554 goto out;
3555 }
3556
3557 root_bh = left_path->p_node[subtree_index].bh;
3558 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3559
Joel Becker4fe82c32009-02-13 02:16:08 -08003560 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003561 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003562 if (ret) {
3563 mlog_errno(ret);
3564 goto out;
3565 }
3566
3567 for (i = subtree_index + 1;
3568 i < path_num_items(right_path); i++) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003569 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003570 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003571 if (ret) {
3572 mlog_errno(ret);
3573 goto out;
3574 }
3575
Joel Becker4fe82c32009-02-13 02:16:08 -08003576 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003577 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003578 if (ret) {
3579 mlog_errno(ret);
3580 goto out;
3581 }
3582 }
3583 } else {
3584 left_rec = &el->l_recs[index - 1];
3585 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3586 has_empty_extent = 1;
3587 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003588
Joel Becker4fe82c32009-02-13 02:16:08 -08003589 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Tao Ma9047bea2009-01-05 14:45:24 +08003590 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003591 if (ret) {
3592 mlog_errno(ret);
3593 goto out;
3594 }
3595
3596 if (has_empty_extent && index == 1) {
3597 /*
3598 * The easy case - we can just plop the record right in.
3599 */
3600 *left_rec = *split_rec;
3601
3602 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003603 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003604 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003605
3606 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3607 le64_add_cpu(&right_rec->e_blkno,
Joel Becker4fe82c32009-02-13 02:16:08 -08003608 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3609 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003610 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3611
3612 ocfs2_cleanup_merge(el, index);
3613
Joel Beckerec20cec2010-03-19 14:13:52 -07003614 ocfs2_journal_dirty(handle, bh);
Tao Ma677b9752008-01-30 14:21:05 +08003615 if (left_path) {
Joel Beckerec20cec2010-03-19 14:13:52 -07003616 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
Tao Ma677b9752008-01-30 14:21:05 +08003617
3618 /*
3619 * In the situation that the right_rec is empty and the extent
3620 * block is empty also, ocfs2_complete_edge_insert can't handle
3621 * it and we need to delete the right extent block.
3622 */
3623 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3624 le16_to_cpu(el->l_next_free_rec) == 1) {
3625
Joel Becker70f18c02009-02-13 02:09:31 -08003626 ret = ocfs2_remove_rightmost_path(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003627 right_path,
Joel Becker70f18c02009-02-13 02:09:31 -08003628 dealloc);
Tao Ma677b9752008-01-30 14:21:05 +08003629 if (ret) {
3630 mlog_errno(ret);
3631 goto out;
3632 }
3633
3634 /* Now the rightmost extent block has been deleted.
3635 * So we use the new rightmost path.
3636 */
3637 ocfs2_mv_path(right_path, left_path);
3638 left_path = NULL;
3639 } else
Joel Becker4619c732009-02-12 19:02:36 -08003640 ocfs2_complete_edge_insert(handle, left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003641 right_path, subtree_index);
3642 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003643out:
Tao Ma677b9752008-01-30 14:21:05 +08003644 if (left_path)
3645 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003646 return ret;
3647}
3648
Joel Beckerc495dd22009-02-13 02:19:11 -08003649static int ocfs2_try_to_merge_extent(handle_t *handle,
3650 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003651 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003652 int split_index,
3653 struct ocfs2_extent_rec *split_rec,
3654 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckerc495dd22009-02-13 02:19:11 -08003655 struct ocfs2_merge_ctxt *ctxt)
Mark Fasheh328d5752007-06-18 10:48:04 -07003656{
Tao Mao518d7262007-08-28 17:25:35 -07003657 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003658 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003659 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3660
3661 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3662
Tao Mao518d7262007-08-28 17:25:35 -07003663 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3664 /*
3665 * The merge code will need to create an empty
3666 * extent to take the place of the newly
3667 * emptied slot. Remove any pre-existing empty
3668 * extents - having more than one in a leaf is
3669 * illegal.
3670 */
Joel Becker70f18c02009-02-13 02:09:31 -08003671 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Tao Mao518d7262007-08-28 17:25:35 -07003672 if (ret) {
3673 mlog_errno(ret);
3674 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003675 }
Tao Mao518d7262007-08-28 17:25:35 -07003676 split_index--;
3677 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003678 }
3679
3680 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3681 /*
3682 * Left-right contig implies this.
3683 */
3684 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003685
3686 /*
3687 * Since the leftright insert always covers the entire
3688 * extent, this call will delete the insert record
3689 * entirely, resulting in an empty extent record added to
3690 * the extent block.
3691 *
3692 * Since the adding of an empty extent shifts
3693 * everything back to the right, there's no need to
3694 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003695 *
3696 * When the split_index is zero, we need to merge it to the
3697 * prevoius extent block. It is more efficient and easier
3698 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003699 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003700 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003701 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003702 if (ret) {
3703 mlog_errno(ret);
3704 goto out;
3705 }
3706
3707 /*
3708 * We can only get this from logic error above.
3709 */
3710 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3711
Tao Ma677b9752008-01-30 14:21:05 +08003712 /* The merge left us with an empty extent, remove it. */
Joel Becker70f18c02009-02-13 02:09:31 -08003713 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003714 if (ret) {
3715 mlog_errno(ret);
3716 goto out;
3717 }
Tao Ma677b9752008-01-30 14:21:05 +08003718
Mark Fasheh328d5752007-06-18 10:48:04 -07003719 rec = &el->l_recs[split_index];
3720
3721 /*
3722 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003723 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003724 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003725 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3726 dealloc, split_index);
Tao Ma677b9752008-01-30 14:21:05 +08003727
Mark Fasheh328d5752007-06-18 10:48:04 -07003728 if (ret) {
3729 mlog_errno(ret);
3730 goto out;
3731 }
3732
Joel Becker70f18c02009-02-13 02:09:31 -08003733 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003734 /*
3735 * Error from this last rotate is not critical, so
3736 * print but don't bubble it up.
3737 */
3738 if (ret)
3739 mlog_errno(ret);
3740 ret = 0;
3741 } else {
3742 /*
3743 * Merge a record to the left or right.
3744 *
3745 * 'contig_type' is relative to the existing record,
3746 * so for example, if we're "right contig", it's to
3747 * the record on the left (hence the left merge).
3748 */
3749 if (ctxt->c_contig_type == CONTIG_RIGHT) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003750 ret = ocfs2_merge_rec_left(path, handle, et,
3751 split_rec, dealloc,
Mark Fasheh328d5752007-06-18 10:48:04 -07003752 split_index);
3753 if (ret) {
3754 mlog_errno(ret);
3755 goto out;
3756 }
3757 } else {
Joel Becker4fe82c32009-02-13 02:16:08 -08003758 ret = ocfs2_merge_rec_right(path, handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003759 et, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003760 split_index);
3761 if (ret) {
3762 mlog_errno(ret);
3763 goto out;
3764 }
3765 }
3766
3767 if (ctxt->c_split_covers_rec) {
3768 /*
3769 * The merge may have left an empty extent in
3770 * our leaf. Try to rotate it away.
3771 */
Joel Becker70f18c02009-02-13 02:09:31 -08003772 ret = ocfs2_rotate_tree_left(handle, et, path,
3773 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003774 if (ret)
3775 mlog_errno(ret);
3776 ret = 0;
3777 }
3778 }
3779
3780out:
3781 return ret;
3782}
3783
3784static void ocfs2_subtract_from_rec(struct super_block *sb,
3785 enum ocfs2_split_type split,
3786 struct ocfs2_extent_rec *rec,
3787 struct ocfs2_extent_rec *split_rec)
3788{
3789 u64 len_blocks;
3790
3791 len_blocks = ocfs2_clusters_to_blocks(sb,
3792 le16_to_cpu(split_rec->e_leaf_clusters));
3793
3794 if (split == SPLIT_LEFT) {
3795 /*
3796 * Region is on the left edge of the existing
3797 * record.
3798 */
3799 le32_add_cpu(&rec->e_cpos,
3800 le16_to_cpu(split_rec->e_leaf_clusters));
3801 le64_add_cpu(&rec->e_blkno, len_blocks);
3802 le16_add_cpu(&rec->e_leaf_clusters,
3803 -le16_to_cpu(split_rec->e_leaf_clusters));
3804 } else {
3805 /*
3806 * Region is on the right edge of the existing
3807 * record.
3808 */
3809 le16_add_cpu(&rec->e_leaf_clusters,
3810 -le16_to_cpu(split_rec->e_leaf_clusters));
3811 }
3812}
3813
Mark Fashehdcd05382007-01-16 11:32:23 -08003814/*
3815 * Do the final bits of extent record insertion at the target leaf
3816 * list. If this leaf is part of an allocation tree, it is assumed
3817 * that the tree above has been prepared.
3818 */
Joel Beckerd5628622009-02-13 02:54:36 -08003819static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3820 struct ocfs2_extent_rec *insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08003821 struct ocfs2_extent_list *el,
Joel Beckerd5628622009-02-13 02:54:36 -08003822 struct ocfs2_insert_type *insert)
Mark Fashehdcd05382007-01-16 11:32:23 -08003823{
3824 int i = insert->ins_contig_index;
3825 unsigned int range;
3826 struct ocfs2_extent_rec *rec;
3827
Mark Fashehe48edee2007-03-07 16:46:57 -08003828 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003829
Mark Fasheh328d5752007-06-18 10:48:04 -07003830 if (insert->ins_split != SPLIT_NONE) {
3831 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3832 BUG_ON(i == -1);
3833 rec = &el->l_recs[i];
Joel Beckerd5628622009-02-13 02:54:36 -08003834 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3835 insert->ins_split, rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003836 insert_rec);
3837 goto rotate;
3838 }
3839
Mark Fashehdcd05382007-01-16 11:32:23 -08003840 /*
3841 * Contiguous insert - either left or right.
3842 */
3843 if (insert->ins_contig != CONTIG_NONE) {
3844 rec = &el->l_recs[i];
3845 if (insert->ins_contig == CONTIG_LEFT) {
3846 rec->e_blkno = insert_rec->e_blkno;
3847 rec->e_cpos = insert_rec->e_cpos;
3848 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003849 le16_add_cpu(&rec->e_leaf_clusters,
3850 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003851 return;
3852 }
3853
3854 /*
3855 * Handle insert into an empty leaf.
3856 */
3857 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3858 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3859 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3860 el->l_recs[0] = *insert_rec;
3861 el->l_next_free_rec = cpu_to_le16(1);
3862 return;
3863 }
3864
3865 /*
3866 * Appending insert.
3867 */
3868 if (insert->ins_appending == APPEND_TAIL) {
3869 i = le16_to_cpu(el->l_next_free_rec) - 1;
3870 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003871 range = le32_to_cpu(rec->e_cpos)
3872 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003873 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3874
3875 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3876 le16_to_cpu(el->l_count),
Joel Beckerd5628622009-02-13 02:54:36 -08003877 "owner %llu, depth %u, count %u, next free %u, "
Mark Fashehdcd05382007-01-16 11:32:23 -08003878 "rec.cpos %u, rec.clusters %u, "
3879 "insert.cpos %u, insert.clusters %u\n",
Joel Beckerd5628622009-02-13 02:54:36 -08003880 ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08003881 le16_to_cpu(el->l_tree_depth),
3882 le16_to_cpu(el->l_count),
3883 le16_to_cpu(el->l_next_free_rec),
3884 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003885 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003886 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003887 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003888 i++;
3889 el->l_recs[i] = *insert_rec;
3890 le16_add_cpu(&el->l_next_free_rec, 1);
3891 return;
3892 }
3893
Mark Fasheh328d5752007-06-18 10:48:04 -07003894rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003895 /*
3896 * Ok, we have to rotate.
3897 *
3898 * At this point, it is safe to assume that inserting into an
3899 * empty leaf and appending to a leaf have both been handled
3900 * above.
3901 *
3902 * This leaf needs to have space, either by the empty 1st
3903 * extent record, or by virtue of an l_next_rec < l_count.
3904 */
3905 ocfs2_rotate_leaf(el, insert_rec);
3906}
3907
Joel Beckerd401dc12009-02-13 02:24:10 -08003908static void ocfs2_adjust_rightmost_records(handle_t *handle,
3909 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003910 struct ocfs2_path *path,
3911 struct ocfs2_extent_rec *insert_rec)
3912{
3913 int ret, i, next_free;
3914 struct buffer_head *bh;
3915 struct ocfs2_extent_list *el;
3916 struct ocfs2_extent_rec *rec;
3917
3918 /*
3919 * Update everything except the leaf block.
3920 */
3921 for (i = 0; i < path->p_tree_depth; i++) {
3922 bh = path->p_node[i].bh;
3923 el = path->p_node[i].el;
3924
3925 next_free = le16_to_cpu(el->l_next_free_rec);
3926 if (next_free == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08003927 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3928 "Owner %llu has a bad extent list",
3929 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fasheh328d5752007-06-18 10:48:04 -07003930 ret = -EIO;
3931 return;
3932 }
3933
3934 rec = &el->l_recs[next_free - 1];
3935
3936 rec->e_int_clusters = insert_rec->e_cpos;
3937 le32_add_cpu(&rec->e_int_clusters,
3938 le16_to_cpu(insert_rec->e_leaf_clusters));
3939 le32_add_cpu(&rec->e_int_clusters,
3940 -le32_to_cpu(rec->e_cpos));
3941
Joel Beckerec20cec2010-03-19 14:13:52 -07003942 ocfs2_journal_dirty(handle, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07003943 }
3944}
3945
Joel Beckerd401dc12009-02-13 02:24:10 -08003946static int ocfs2_append_rec_to_path(handle_t *handle,
3947 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08003948 struct ocfs2_extent_rec *insert_rec,
3949 struct ocfs2_path *right_path,
3950 struct ocfs2_path **ret_left_path)
3951{
Mark Fasheh328d5752007-06-18 10:48:04 -07003952 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003953 struct ocfs2_extent_list *el;
3954 struct ocfs2_path *left_path = NULL;
3955
3956 *ret_left_path = NULL;
3957
3958 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003959 * This shouldn't happen for non-trees. The extent rec cluster
3960 * count manipulation below only works for interior nodes.
3961 */
3962 BUG_ON(right_path->p_tree_depth == 0);
3963
3964 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003965 * If our appending insert is at the leftmost edge of a leaf,
3966 * then we might need to update the rightmost records of the
3967 * neighboring path.
3968 */
3969 el = path_leaf_el(right_path);
3970 next_free = le16_to_cpu(el->l_next_free_rec);
3971 if (next_free == 0 ||
3972 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3973 u32 left_cpos;
3974
Joel Beckerd401dc12009-02-13 02:24:10 -08003975 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3976 right_path, &left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08003977 if (ret) {
3978 mlog_errno(ret);
3979 goto out;
3980 }
3981
3982 mlog(0, "Append may need a left path update. cpos: %u, "
3983 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3984 left_cpos);
3985
3986 /*
3987 * No need to worry if the append is already in the
3988 * leftmost leaf.
3989 */
3990 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07003991 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003992 if (!left_path) {
3993 ret = -ENOMEM;
3994 mlog_errno(ret);
3995 goto out;
3996 }
3997
Joel Beckerd401dc12009-02-13 02:24:10 -08003998 ret = ocfs2_find_path(et->et_ci, left_path,
Joel Beckerfacdb772009-02-12 18:08:48 -08003999 left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004000 if (ret) {
4001 mlog_errno(ret);
4002 goto out;
4003 }
4004
4005 /*
4006 * ocfs2_insert_path() will pass the left_path to the
4007 * journal for us.
4008 */
4009 }
4010 }
4011
Joel Beckerd401dc12009-02-13 02:24:10 -08004012 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004013 if (ret) {
4014 mlog_errno(ret);
4015 goto out;
4016 }
4017
Joel Beckerd401dc12009-02-13 02:24:10 -08004018 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004019
4020 *ret_left_path = left_path;
4021 ret = 0;
4022out:
4023 if (ret != 0)
4024 ocfs2_free_path(left_path);
4025
4026 return ret;
4027}
4028
Joel Beckerc38e52b2009-02-13 02:56:23 -08004029static void ocfs2_split_record(struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004030 struct ocfs2_path *left_path,
4031 struct ocfs2_path *right_path,
4032 struct ocfs2_extent_rec *split_rec,
4033 enum ocfs2_split_type split)
4034{
4035 int index;
4036 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4037 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4038 struct ocfs2_extent_rec *rec, *tmprec;
4039
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004040 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004041 if (left_path)
4042 left_el = path_leaf_el(left_path);
4043
4044 el = right_el;
4045 insert_el = right_el;
4046 index = ocfs2_search_extent_list(el, cpos);
4047 if (index != -1) {
4048 if (index == 0 && left_path) {
4049 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4050
4051 /*
4052 * This typically means that the record
4053 * started in the left path but moved to the
4054 * right as a result of rotation. We either
4055 * move the existing record to the left, or we
4056 * do the later insert there.
4057 *
4058 * In this case, the left path should always
4059 * exist as the rotate code will have passed
4060 * it back for a post-insert update.
4061 */
4062
4063 if (split == SPLIT_LEFT) {
4064 /*
4065 * It's a left split. Since we know
4066 * that the rotate code gave us an
4067 * empty extent in the left path, we
4068 * can just do the insert there.
4069 */
4070 insert_el = left_el;
4071 } else {
4072 /*
4073 * Right split - we have to move the
4074 * existing record over to the left
4075 * leaf. The insert will be into the
4076 * newly created empty extent in the
4077 * right leaf.
4078 */
4079 tmprec = &right_el->l_recs[index];
4080 ocfs2_rotate_leaf(left_el, tmprec);
4081 el = left_el;
4082
4083 memset(tmprec, 0, sizeof(*tmprec));
4084 index = ocfs2_search_extent_list(left_el, cpos);
4085 BUG_ON(index == -1);
4086 }
4087 }
4088 } else {
4089 BUG_ON(!left_path);
4090 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4091 /*
4092 * Left path is easy - we can just allow the insert to
4093 * happen.
4094 */
4095 el = left_el;
4096 insert_el = left_el;
4097 index = ocfs2_search_extent_list(el, cpos);
4098 BUG_ON(index == -1);
4099 }
4100
4101 rec = &el->l_recs[index];
Joel Beckerc38e52b2009-02-13 02:56:23 -08004102 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4103 split, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004104 ocfs2_rotate_leaf(insert_el, split_rec);
4105}
4106
Mark Fashehdcd05382007-01-16 11:32:23 -08004107/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004108 * This function only does inserts on an allocation b-tree. For tree
4109 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004110 *
4111 * right_path is the path we want to do the actual insert
4112 * in. left_path should only be passed in if we need to update that
4113 * portion of the tree after an edge insert.
4114 */
Joel Becker3505bec2009-02-13 02:57:58 -08004115static int ocfs2_insert_path(handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08004116 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004117 struct ocfs2_path *left_path,
4118 struct ocfs2_path *right_path,
4119 struct ocfs2_extent_rec *insert_rec,
4120 struct ocfs2_insert_type *insert)
4121{
4122 int ret, subtree_index;
4123 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004124
Mark Fashehdcd05382007-01-16 11:32:23 -08004125 if (left_path) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004126 /*
4127 * There's a chance that left_path got passed back to
4128 * us without being accounted for in the
4129 * journal. Extend our transaction here to be sure we
4130 * can change those blocks.
4131 */
Tao Mac901fb02010-04-26 14:34:57 +08004132 ret = ocfs2_extend_trans(handle, left_path->p_tree_depth);
Mark Fashehdcd05382007-01-16 11:32:23 -08004133 if (ret < 0) {
4134 mlog_errno(ret);
4135 goto out;
4136 }
4137
Joel Becker7dc02802009-02-12 19:20:13 -08004138 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004139 if (ret < 0) {
4140 mlog_errno(ret);
4141 goto out;
4142 }
4143 }
4144
Mark Fashehe8aed342007-12-03 16:43:01 -08004145 /*
4146 * Pass both paths to the journal. The majority of inserts
4147 * will be touching all components anyway.
4148 */
Joel Becker7dc02802009-02-12 19:20:13 -08004149 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004150 if (ret < 0) {
4151 mlog_errno(ret);
4152 goto out;
4153 }
4154
Mark Fasheh328d5752007-06-18 10:48:04 -07004155 if (insert->ins_split != SPLIT_NONE) {
4156 /*
4157 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004158 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004159 * function sort it all out.
4160 */
Joel Beckerc38e52b2009-02-13 02:56:23 -08004161 ocfs2_split_record(et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004162 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004163
4164 /*
4165 * Split might have modified either leaf and we don't
4166 * have a guarantee that the later edge insert will
4167 * dirty this for us.
4168 */
4169 if (left_path)
Joel Beckerec20cec2010-03-19 14:13:52 -07004170 ocfs2_journal_dirty(handle,
4171 path_leaf_bh(left_path));
Mark Fasheh328d5752007-06-18 10:48:04 -07004172 } else
Joel Beckerd5628622009-02-13 02:54:36 -08004173 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4174 insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004175
Joel Beckerec20cec2010-03-19 14:13:52 -07004176 ocfs2_journal_dirty(handle, leaf_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004177
4178 if (left_path) {
4179 /*
4180 * The rotate code has indicated that we need to fix
4181 * up portions of the tree after the insert.
4182 *
4183 * XXX: Should we extend the transaction here?
4184 */
Joel Becker7dc02802009-02-12 19:20:13 -08004185 subtree_index = ocfs2_find_subtree_root(et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004186 right_path);
Joel Becker4619c732009-02-12 19:02:36 -08004187 ocfs2_complete_edge_insert(handle, left_path, right_path,
4188 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08004189 }
4190
4191 ret = 0;
4192out:
4193 return ret;
4194}
4195
Joel Becker3505bec2009-02-13 02:57:58 -08004196static int ocfs2_do_insert_extent(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004197 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004198 struct ocfs2_extent_rec *insert_rec,
4199 struct ocfs2_insert_type *type)
4200{
4201 int ret, rotate = 0;
4202 u32 cpos;
4203 struct ocfs2_path *right_path = NULL;
4204 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004205 struct ocfs2_extent_list *el;
4206
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004207 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004208
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004209 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004210 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004211 if (ret) {
4212 mlog_errno(ret);
4213 goto out;
4214 }
4215
4216 if (le16_to_cpu(el->l_tree_depth) == 0) {
Joel Beckerd5628622009-02-13 02:54:36 -08004217 ocfs2_insert_at_leaf(et, insert_rec, el, type);
Mark Fashehdcd05382007-01-16 11:32:23 -08004218 goto out_update_clusters;
4219 }
4220
Joel Beckerffdd7a52008-10-17 22:32:01 -07004221 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004222 if (!right_path) {
4223 ret = -ENOMEM;
4224 mlog_errno(ret);
4225 goto out;
4226 }
4227
4228 /*
4229 * Determine the path to start with. Rotations need the
4230 * rightmost path, everything else can go directly to the
4231 * target leaf.
4232 */
4233 cpos = le32_to_cpu(insert_rec->e_cpos);
4234 if (type->ins_appending == APPEND_NONE &&
4235 type->ins_contig == CONTIG_NONE) {
4236 rotate = 1;
4237 cpos = UINT_MAX;
4238 }
4239
Joel Beckerfacdb772009-02-12 18:08:48 -08004240 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004241 if (ret) {
4242 mlog_errno(ret);
4243 goto out;
4244 }
4245
4246 /*
4247 * Rotations and appends need special treatment - they modify
4248 * parts of the tree's above them.
4249 *
4250 * Both might pass back a path immediate to the left of the
4251 * one being inserted to. This will be cause
4252 * ocfs2_insert_path() to modify the rightmost records of
4253 * left_path to account for an edge insert.
4254 *
4255 * XXX: When modifying this code, keep in mind that an insert
4256 * can wind up skipping both of these two special cases...
4257 */
4258 if (rotate) {
Joel Becker1bbf0b82009-02-12 19:42:08 -08004259 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004260 le32_to_cpu(insert_rec->e_cpos),
4261 right_path, &left_path);
4262 if (ret) {
4263 mlog_errno(ret);
4264 goto out;
4265 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004266
4267 /*
4268 * ocfs2_rotate_tree_right() might have extended the
4269 * transaction without re-journaling our tree root.
4270 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004271 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004272 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004273 if (ret) {
4274 mlog_errno(ret);
4275 goto out;
4276 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004277 } else if (type->ins_appending == APPEND_TAIL
4278 && type->ins_contig != CONTIG_LEFT) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004279 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08004280 right_path, &left_path);
4281 if (ret) {
4282 mlog_errno(ret);
4283 goto out;
4284 }
4285 }
4286
Joel Becker3505bec2009-02-13 02:57:58 -08004287 ret = ocfs2_insert_path(handle, et, left_path, right_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004288 insert_rec, type);
4289 if (ret) {
4290 mlog_errno(ret);
4291 goto out;
4292 }
4293
4294out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004295 if (type->ins_split == SPLIT_NONE)
Joel Becker6136ca52009-02-12 19:32:43 -08004296 ocfs2_et_update_clusters(et,
Joel Becker35dc0aa2008-08-20 16:25:06 -07004297 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004298
Joel Beckerec20cec2010-03-19 14:13:52 -07004299 ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004300
4301out:
4302 ocfs2_free_path(left_path);
4303 ocfs2_free_path(right_path);
4304
4305 return ret;
4306}
4307
Mark Fasheh328d5752007-06-18 10:48:04 -07004308static enum ocfs2_contig_type
Joel Beckera2970292009-02-13 03:09:54 -08004309ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4310 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004311 struct ocfs2_extent_list *el, int index,
4312 struct ocfs2_extent_rec *split_rec)
4313{
Tao Maad5a4d72008-01-30 14:21:32 +08004314 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004315 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004316 u32 left_cpos, right_cpos;
4317 struct ocfs2_extent_rec *rec = NULL;
4318 struct ocfs2_extent_list *new_el;
4319 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4320 struct buffer_head *bh;
4321 struct ocfs2_extent_block *eb;
Joel Beckera2970292009-02-13 03:09:54 -08004322 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Tao Maad5a4d72008-01-30 14:21:32 +08004323
4324 if (index > 0) {
4325 rec = &el->l_recs[index - 1];
4326 } else if (path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004327 status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004328 if (status)
4329 goto out;
4330
4331 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004332 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004333 if (!left_path)
4334 goto out;
4335
Joel Beckera2970292009-02-13 03:09:54 -08004336 status = ocfs2_find_path(et->et_ci, left_path,
4337 left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004338 if (status)
4339 goto out;
4340
4341 new_el = path_leaf_el(left_path);
4342
4343 if (le16_to_cpu(new_el->l_next_free_rec) !=
4344 le16_to_cpu(new_el->l_count)) {
4345 bh = path_leaf_bh(left_path);
4346 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004347 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004348 "Extent block #%llu has an "
4349 "invalid l_next_free_rec of "
4350 "%d. It should have "
4351 "matched the l_count of %d",
4352 (unsigned long long)le64_to_cpu(eb->h_blkno),
4353 le16_to_cpu(new_el->l_next_free_rec),
4354 le16_to_cpu(new_el->l_count));
4355 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004356 goto out;
4357 }
4358 rec = &new_el->l_recs[
4359 le16_to_cpu(new_el->l_next_free_rec) - 1];
4360 }
4361 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004362
4363 /*
4364 * We're careful to check for an empty extent record here -
4365 * the merge code will know what to do if it sees one.
4366 */
Tao Maad5a4d72008-01-30 14:21:32 +08004367 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004368 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4369 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4370 ret = CONTIG_RIGHT;
4371 } else {
Tao Ma853a3a12009-08-18 11:22:18 +08004372 ret = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004373 }
4374 }
4375
Tao Maad5a4d72008-01-30 14:21:32 +08004376 rec = NULL;
4377 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4378 rec = &el->l_recs[index + 1];
4379 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4380 path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004381 status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004382 if (status)
4383 goto out;
4384
4385 if (right_cpos == 0)
4386 goto out;
4387
Joel Beckerffdd7a52008-10-17 22:32:01 -07004388 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004389 if (!right_path)
4390 goto out;
4391
Joel Beckera2970292009-02-13 03:09:54 -08004392 status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004393 if (status)
4394 goto out;
4395
4396 new_el = path_leaf_el(right_path);
4397 rec = &new_el->l_recs[0];
4398 if (ocfs2_is_empty_extent(rec)) {
4399 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4400 bh = path_leaf_bh(right_path);
4401 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004402 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004403 "Extent block #%llu has an "
4404 "invalid l_next_free_rec of %d",
4405 (unsigned long long)le64_to_cpu(eb->h_blkno),
4406 le16_to_cpu(new_el->l_next_free_rec));
4407 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004408 goto out;
4409 }
4410 rec = &new_el->l_recs[1];
4411 }
4412 }
4413
4414 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004415 enum ocfs2_contig_type contig_type;
4416
Tao Ma853a3a12009-08-18 11:22:18 +08004417 contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004418
4419 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4420 ret = CONTIG_LEFTRIGHT;
4421 else if (ret == CONTIG_NONE)
4422 ret = contig_type;
4423 }
4424
Tao Maad5a4d72008-01-30 14:21:32 +08004425out:
4426 if (left_path)
4427 ocfs2_free_path(left_path);
4428 if (right_path)
4429 ocfs2_free_path(right_path);
4430
Mark Fasheh328d5752007-06-18 10:48:04 -07004431 return ret;
4432}
4433
Joel Becker1ef61b32009-02-13 03:12:33 -08004434static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004435 struct ocfs2_insert_type *insert,
4436 struct ocfs2_extent_list *el,
Joel Becker1ef61b32009-02-13 03:12:33 -08004437 struct ocfs2_extent_rec *insert_rec)
Mark Fashehdcd05382007-01-16 11:32:23 -08004438{
4439 int i;
4440 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4441
Mark Fashehe48edee2007-03-07 16:46:57 -08004442 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4443
Mark Fashehdcd05382007-01-16 11:32:23 -08004444 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
Tao Ma853a3a12009-08-18 11:22:18 +08004445 contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4446 insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004447 if (contig_type != CONTIG_NONE) {
4448 insert->ins_contig_index = i;
4449 break;
4450 }
4451 }
4452 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004453
4454 if (insert->ins_contig != CONTIG_NONE) {
4455 struct ocfs2_extent_rec *rec =
4456 &el->l_recs[insert->ins_contig_index];
4457 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4458 le16_to_cpu(insert_rec->e_leaf_clusters);
4459
4460 /*
4461 * Caller might want us to limit the size of extents, don't
4462 * calculate contiguousness if we might exceed that limit.
4463 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004464 if (et->et_max_leaf_clusters &&
4465 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004466 insert->ins_contig = CONTIG_NONE;
4467 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004468}
4469
4470/*
4471 * This should only be called against the righmost leaf extent list.
4472 *
4473 * ocfs2_figure_appending_type() will figure out whether we'll have to
4474 * insert at the tail of the rightmost leaf.
4475 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004476 * This should also work against the root extent list for tree's with 0
4477 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004478 * then the logic here makes sense.
4479 */
4480static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4481 struct ocfs2_extent_list *el,
4482 struct ocfs2_extent_rec *insert_rec)
4483{
4484 int i;
4485 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4486 struct ocfs2_extent_rec *rec;
4487
4488 insert->ins_appending = APPEND_NONE;
4489
Mark Fashehe48edee2007-03-07 16:46:57 -08004490 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004491
4492 if (!el->l_next_free_rec)
4493 goto set_tail_append;
4494
4495 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4496 /* Were all records empty? */
4497 if (le16_to_cpu(el->l_next_free_rec) == 1)
4498 goto set_tail_append;
4499 }
4500
4501 i = le16_to_cpu(el->l_next_free_rec) - 1;
4502 rec = &el->l_recs[i];
4503
Mark Fashehe48edee2007-03-07 16:46:57 -08004504 if (cpos >=
4505 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004506 goto set_tail_append;
4507
4508 return;
4509
4510set_tail_append:
4511 insert->ins_appending = APPEND_TAIL;
4512}
4513
4514/*
4515 * Helper function called at the begining of an insert.
4516 *
4517 * This computes a few things that are commonly used in the process of
4518 * inserting into the btree:
4519 * - Whether the new extent is contiguous with an existing one.
4520 * - The current tree depth.
4521 * - Whether the insert is an appending one.
4522 * - The total # of free records in the tree.
4523 *
4524 * All of the information is stored on the ocfs2_insert_type
4525 * structure.
4526 */
Joel Becker627961b2009-02-13 03:14:38 -08004527static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004528 struct buffer_head **last_eb_bh,
4529 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004530 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004531 struct ocfs2_insert_type *insert)
4532{
4533 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004534 struct ocfs2_extent_block *eb;
4535 struct ocfs2_extent_list *el;
4536 struct ocfs2_path *path = NULL;
4537 struct buffer_head *bh = NULL;
4538
Mark Fasheh328d5752007-06-18 10:48:04 -07004539 insert->ins_split = SPLIT_NONE;
4540
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004541 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004542 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4543
4544 if (el->l_tree_depth) {
4545 /*
4546 * If we have tree depth, we read in the
4547 * rightmost extent block ahead of time as
4548 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4549 * may want it later.
4550 */
Joel Becker3d03a302009-02-12 17:49:26 -08004551 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08004552 ocfs2_et_get_last_eb_blk(et),
4553 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004554 if (ret) {
4555 mlog_exit(ret);
4556 goto out;
4557 }
4558 eb = (struct ocfs2_extent_block *) bh->b_data;
4559 el = &eb->h_list;
4560 }
4561
4562 /*
4563 * Unless we have a contiguous insert, we'll need to know if
4564 * there is room left in our allocation tree for another
4565 * extent record.
4566 *
4567 * XXX: This test is simplistic, we can search for empty
4568 * extent records too.
4569 */
Tao Maoc77534f2007-08-28 17:22:33 -07004570 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004571 le16_to_cpu(el->l_next_free_rec);
4572
4573 if (!insert->ins_tree_depth) {
Joel Becker1ef61b32009-02-13 03:12:33 -08004574 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004575 ocfs2_figure_appending_type(insert, el, insert_rec);
4576 return 0;
4577 }
4578
Joel Beckerffdd7a52008-10-17 22:32:01 -07004579 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004580 if (!path) {
4581 ret = -ENOMEM;
4582 mlog_errno(ret);
4583 goto out;
4584 }
4585
4586 /*
4587 * In the case that we're inserting past what the tree
4588 * currently accounts for, ocfs2_find_path() will return for
4589 * us the rightmost tree path. This is accounted for below in
4590 * the appending code.
4591 */
Joel Beckerfacdb772009-02-12 18:08:48 -08004592 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -08004593 if (ret) {
4594 mlog_errno(ret);
4595 goto out;
4596 }
4597
4598 el = path_leaf_el(path);
4599
4600 /*
4601 * Now that we have the path, there's two things we want to determine:
4602 * 1) Contiguousness (also set contig_index if this is so)
4603 *
4604 * 2) Are we doing an append? We can trivially break this up
4605 * into two types of appends: simple record append, or a
4606 * rotate inside the tail leaf.
4607 */
Joel Becker1ef61b32009-02-13 03:12:33 -08004608 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004609
4610 /*
4611 * The insert code isn't quite ready to deal with all cases of
4612 * left contiguousness. Specifically, if it's an insert into
4613 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004614 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004615 * left. For now, just catch that case and fool the layers
4616 * above us. This works just fine for tree_depth == 0, which
4617 * is why we allow that above.
4618 */
4619 if (insert->ins_contig == CONTIG_LEFT &&
4620 insert->ins_contig_index == 0)
4621 insert->ins_contig = CONTIG_NONE;
4622
4623 /*
4624 * Ok, so we can simply compare against last_eb to figure out
4625 * whether the path doesn't exist. This will only happen in
4626 * the case that we're doing a tail append, so maybe we can
4627 * take advantage of that information somehow.
4628 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004629 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004630 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004631 /*
4632 * Ok, ocfs2_find_path() returned us the rightmost
4633 * tree path. This might be an appending insert. There are
4634 * two cases:
4635 * 1) We're doing a true append at the tail:
4636 * -This might even be off the end of the leaf
4637 * 2) We're "appending" by rotating in the tail
4638 */
4639 ocfs2_figure_appending_type(insert, el, insert_rec);
4640 }
4641
4642out:
4643 ocfs2_free_path(path);
4644
4645 if (ret == 0)
4646 *last_eb_bh = bh;
4647 else
4648 brelse(bh);
4649 return ret;
4650}
4651
4652/*
Joel Beckercc79d8c2009-02-13 03:24:43 -08004653 * Insert an extent into a btree.
Mark Fashehdcd05382007-01-16 11:32:23 -08004654 *
Joel Beckercc79d8c2009-02-13 03:24:43 -08004655 * The caller needs to update the owning btree's cluster count.
Mark Fashehdcd05382007-01-16 11:32:23 -08004656 */
Joel Beckercc79d8c2009-02-13 03:24:43 -08004657int ocfs2_insert_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004658 struct ocfs2_extent_tree *et,
4659 u32 cpos,
4660 u64 start_blk,
4661 u32 new_clusters,
4662 u8 flags,
4663 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004664{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004665 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004666 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004667 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004668 struct ocfs2_insert_type insert = {0, };
4669 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004670
Joel Beckercc79d8c2009-02-13 03:24:43 -08004671 mlog(0, "add %u clusters at position %u to owner %llu\n",
4672 new_clusters, cpos,
4673 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08004674
Mark Fashehe48edee2007-03-07 16:46:57 -08004675 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004676 rec.e_cpos = cpu_to_le32(cpos);
4677 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004678 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004679 rec.e_flags = flags;
Joel Becker6136ca52009-02-12 19:32:43 -08004680 status = ocfs2_et_insert_check(et, &rec);
Joel Becker1e61ee72008-08-20 18:32:45 -07004681 if (status) {
4682 mlog_errno(status);
4683 goto bail;
4684 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004685
Joel Becker627961b2009-02-13 03:14:38 -08004686 status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004687 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004688 if (status < 0) {
4689 mlog_errno(status);
4690 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004691 }
4692
Mark Fashehdcd05382007-01-16 11:32:23 -08004693 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4694 "Insert.contig_index: %d, Insert.free_records: %d, "
4695 "Insert.tree_depth: %d\n",
4696 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004697 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004698
Tao Maoc77534f2007-08-28 17:22:33 -07004699 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004700 status = ocfs2_grow_tree(handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004701 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004702 meta_ac);
4703 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004704 mlog_errno(status);
4705 goto bail;
4706 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004707 }
4708
Mark Fashehdcd05382007-01-16 11:32:23 -08004709 /* Finally, we can add clusters. This might rotate the tree for us. */
Joel Becker3505bec2009-02-13 02:57:58 -08004710 status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004711 if (status < 0)
4712 mlog_errno(status);
Joel Becker92ba4702009-02-13 03:18:34 -08004713 else
4714 ocfs2_et_extent_map_insert(et, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004715
4716bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004717 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004718
Tao Maf56654c2008-08-18 17:38:48 +08004719 mlog_exit(status);
4720 return status;
4721}
4722
Tao Ma0eb8d472008-08-18 17:38:45 +08004723/*
4724 * Allcate and add clusters into the extent b-tree.
4725 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004726 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004727 * it is not limited to the file storage. Any extent tree can use this
4728 * function if it implements the proper ocfs2_extent_tree.
4729 */
Joel Beckercbee7e12009-02-13 03:34:15 -08004730int ocfs2_add_clusters_in_btree(handle_t *handle,
4731 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004732 u32 *logical_offset,
4733 u32 clusters_to_add,
4734 int mark_unwritten,
Tao Ma0eb8d472008-08-18 17:38:45 +08004735 struct ocfs2_alloc_context *data_ac,
4736 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004737 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004738{
4739 int status = 0;
4740 int free_extents;
4741 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4742 u32 bit_off, num_bits;
4743 u64 block;
4744 u8 flags = 0;
Joel Beckercbee7e12009-02-13 03:34:15 -08004745 struct ocfs2_super *osb =
4746 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Tao Ma0eb8d472008-08-18 17:38:45 +08004747
4748 BUG_ON(!clusters_to_add);
4749
4750 if (mark_unwritten)
4751 flags = OCFS2_EXT_UNWRITTEN;
4752
Joel Becker3d03a302009-02-12 17:49:26 -08004753 free_extents = ocfs2_num_free_extents(osb, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004754 if (free_extents < 0) {
4755 status = free_extents;
4756 mlog_errno(status);
4757 goto leave;
4758 }
4759
4760 /* there are two cases which could cause us to EAGAIN in the
4761 * we-need-more-metadata case:
4762 * 1) we haven't reserved *any*
4763 * 2) we are so fragmented, we've needed to add metadata too
4764 * many times. */
4765 if (!free_extents && !meta_ac) {
4766 mlog(0, "we haven't reserved any metadata!\n");
4767 status = -EAGAIN;
4768 reason = RESTART_META;
4769 goto leave;
4770 } else if ((!free_extents)
4771 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004772 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004773 mlog(0, "filesystem is really fragmented...\n");
4774 status = -EAGAIN;
4775 reason = RESTART_META;
4776 goto leave;
4777 }
4778
Joel Becker1ed9b772010-05-06 13:59:06 +08004779 status = __ocfs2_claim_clusters(handle, data_ac, 1,
Tao Ma0eb8d472008-08-18 17:38:45 +08004780 clusters_to_add, &bit_off, &num_bits);
4781 if (status < 0) {
4782 if (status != -ENOSPC)
4783 mlog_errno(status);
4784 goto leave;
4785 }
4786
4787 BUG_ON(num_bits > clusters_to_add);
4788
Joel Becker13723d02008-10-17 19:25:01 -07004789 /* reserve our write early -- insert_extent may update the tree root */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004790 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004791 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d472008-08-18 17:38:45 +08004792 if (status < 0) {
4793 mlog_errno(status);
4794 goto leave;
4795 }
4796
4797 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Joel Beckercbee7e12009-02-13 03:34:15 -08004798 mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
4799 num_bits, bit_off,
4800 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Joel Beckercc79d8c2009-02-13 03:24:43 -08004801 status = ocfs2_insert_extent(handle, et, *logical_offset, block,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004802 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004803 if (status < 0) {
4804 mlog_errno(status);
4805 goto leave;
4806 }
4807
Joel Beckerec20cec2010-03-19 14:13:52 -07004808 ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004809
4810 clusters_to_add -= num_bits;
4811 *logical_offset += num_bits;
4812
4813 if (clusters_to_add) {
4814 mlog(0, "need to alloc once more, wanted = %u\n",
4815 clusters_to_add);
4816 status = -EAGAIN;
4817 reason = RESTART_TRANS;
4818 }
4819
4820leave:
4821 mlog_exit(status);
4822 if (reason_ret)
4823 *reason_ret = reason;
4824 return status;
4825}
4826
Mark Fasheh328d5752007-06-18 10:48:04 -07004827static void ocfs2_make_right_split_rec(struct super_block *sb,
4828 struct ocfs2_extent_rec *split_rec,
4829 u32 cpos,
4830 struct ocfs2_extent_rec *rec)
4831{
4832 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4833 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4834
4835 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4836
4837 split_rec->e_cpos = cpu_to_le32(cpos);
4838 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4839
4840 split_rec->e_blkno = rec->e_blkno;
4841 le64_add_cpu(&split_rec->e_blkno,
4842 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4843
4844 split_rec->e_flags = rec->e_flags;
4845}
4846
Joel Beckerd2311292009-02-13 03:43:22 -08004847static int ocfs2_split_and_insert(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004848 struct ocfs2_extent_tree *et,
Joel Beckerd2311292009-02-13 03:43:22 -08004849 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004850 struct buffer_head **last_eb_bh,
4851 int split_index,
4852 struct ocfs2_extent_rec *orig_split_rec,
4853 struct ocfs2_alloc_context *meta_ac)
4854{
4855 int ret = 0, depth;
4856 unsigned int insert_range, rec_range, do_leftright = 0;
4857 struct ocfs2_extent_rec tmprec;
4858 struct ocfs2_extent_list *rightmost_el;
4859 struct ocfs2_extent_rec rec;
4860 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4861 struct ocfs2_insert_type insert;
4862 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004863
4864leftright:
4865 /*
4866 * Store a copy of the record on the stack - it might move
4867 * around as the tree is manipulated below.
4868 */
4869 rec = path_leaf_el(path)->l_recs[split_index];
4870
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004871 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004872
4873 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4874 if (depth) {
4875 BUG_ON(!(*last_eb_bh));
4876 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4877 rightmost_el = &eb->h_list;
4878 }
4879
4880 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4881 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004882 ret = ocfs2_grow_tree(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08004883 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004884 if (ret) {
4885 mlog_errno(ret);
4886 goto out;
4887 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004888 }
4889
4890 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4891 insert.ins_appending = APPEND_NONE;
4892 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004893 insert.ins_tree_depth = depth;
4894
4895 insert_range = le32_to_cpu(split_rec.e_cpos) +
4896 le16_to_cpu(split_rec.e_leaf_clusters);
4897 rec_range = le32_to_cpu(rec.e_cpos) +
4898 le16_to_cpu(rec.e_leaf_clusters);
4899
4900 if (split_rec.e_cpos == rec.e_cpos) {
4901 insert.ins_split = SPLIT_LEFT;
4902 } else if (insert_range == rec_range) {
4903 insert.ins_split = SPLIT_RIGHT;
4904 } else {
4905 /*
4906 * Left/right split. We fake this as a right split
4907 * first and then make a second pass as a left split.
4908 */
4909 insert.ins_split = SPLIT_RIGHT;
4910
Joel Beckerd2311292009-02-13 03:43:22 -08004911 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4912 &tmprec, insert_range, &rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004913
4914 split_rec = tmprec;
4915
4916 BUG_ON(do_leftright);
4917 do_leftright = 1;
4918 }
4919
Joel Becker3505bec2009-02-13 02:57:58 -08004920 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004921 if (ret) {
4922 mlog_errno(ret);
4923 goto out;
4924 }
4925
4926 if (do_leftright == 1) {
4927 u32 cpos;
4928 struct ocfs2_extent_list *el;
4929
4930 do_leftright++;
4931 split_rec = *orig_split_rec;
4932
4933 ocfs2_reinit_path(path, 1);
4934
4935 cpos = le32_to_cpu(split_rec.e_cpos);
Joel Beckerfacdb772009-02-12 18:08:48 -08004936 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07004937 if (ret) {
4938 mlog_errno(ret);
4939 goto out;
4940 }
4941
4942 el = path_leaf_el(path);
4943 split_index = ocfs2_search_extent_list(el, cpos);
4944 goto leftright;
4945 }
4946out:
4947
4948 return ret;
4949}
4950
Joel Beckerf3868d02009-02-17 19:46:04 -08004951static int ocfs2_replace_extent_rec(handle_t *handle,
4952 struct ocfs2_extent_tree *et,
Tao Ma47be12e2009-01-09 07:32:48 +08004953 struct ocfs2_path *path,
4954 struct ocfs2_extent_list *el,
4955 int split_index,
4956 struct ocfs2_extent_rec *split_rec)
4957{
4958 int ret;
4959
Joel Beckerf3868d02009-02-17 19:46:04 -08004960 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Tao Ma47be12e2009-01-09 07:32:48 +08004961 path_num_items(path) - 1);
4962 if (ret) {
4963 mlog_errno(ret);
4964 goto out;
4965 }
4966
4967 el->l_recs[split_index] = *split_rec;
4968
4969 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4970out:
4971 return ret;
4972}
4973
Mark Fasheh328d5752007-06-18 10:48:04 -07004974/*
Tao Ma555936b2009-08-18 11:22:21 +08004975 * Split part or all of the extent record at split_index in the leaf
4976 * pointed to by path. Merge with the contiguous extent record if needed.
Mark Fasheh328d5752007-06-18 10:48:04 -07004977 *
4978 * Care is taken to handle contiguousness so as to not grow the tree.
4979 *
4980 * meta_ac is not strictly necessary - we only truly need it if growth
4981 * of the tree is required. All other cases will degrade into a less
4982 * optimal tree layout.
4983 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004984 * last_eb_bh should be the rightmost leaf block for any extent
4985 * btree. Since a split may grow the tree or a merge might shrink it,
4986 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07004987 *
4988 * This code is optimized for readability - several passes might be
4989 * made over certain portions of the tree. All of those blocks will
4990 * have been brought into cache (and pinned via the journal), so the
4991 * extra overhead is not expressed in terms of disk reads.
4992 */
Tao Mae2e9f602009-08-18 11:22:34 +08004993int ocfs2_split_extent(handle_t *handle,
4994 struct ocfs2_extent_tree *et,
4995 struct ocfs2_path *path,
4996 int split_index,
4997 struct ocfs2_extent_rec *split_rec,
4998 struct ocfs2_alloc_context *meta_ac,
4999 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005000{
5001 int ret = 0;
5002 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08005003 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07005004 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5005 struct ocfs2_merge_ctxt ctxt;
5006 struct ocfs2_extent_list *rightmost_el;
5007
Mark Fasheh328d5752007-06-18 10:48:04 -07005008 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5009 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5010 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5011 ret = -EIO;
5012 mlog_errno(ret);
5013 goto out;
5014 }
5015
Joel Beckera2970292009-02-13 03:09:54 -08005016 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005017 split_index,
5018 split_rec);
5019
5020 /*
5021 * The core merge / split code wants to know how much room is
Joel Beckera1cf0762009-02-13 03:45:49 -08005022 * left in this allocation tree, so we pass the
Mark Fasheh328d5752007-06-18 10:48:04 -07005023 * rightmost extent list.
5024 */
5025 if (path->p_tree_depth) {
5026 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005027
Joel Becker3d03a302009-02-12 17:49:26 -08005028 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005029 ocfs2_et_get_last_eb_blk(et),
5030 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005031 if (ret) {
5032 mlog_exit(ret);
5033 goto out;
5034 }
5035
5036 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005037 rightmost_el = &eb->h_list;
5038 } else
5039 rightmost_el = path_root_el(path);
5040
Mark Fasheh328d5752007-06-18 10:48:04 -07005041 if (rec->e_cpos == split_rec->e_cpos &&
5042 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5043 ctxt.c_split_covers_rec = 1;
5044 else
5045 ctxt.c_split_covers_rec = 0;
5046
5047 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5048
Mark Fasheh015452b2007-09-12 10:21:22 -07005049 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5050 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5051 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005052
5053 if (ctxt.c_contig_type == CONTIG_NONE) {
5054 if (ctxt.c_split_covers_rec)
Joel Beckerf3868d02009-02-17 19:46:04 -08005055 ret = ocfs2_replace_extent_rec(handle, et, path, el,
Tao Ma47be12e2009-01-09 07:32:48 +08005056 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005057 else
Joel Beckerd2311292009-02-13 03:43:22 -08005058 ret = ocfs2_split_and_insert(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005059 &last_eb_bh, split_index,
5060 split_rec, meta_ac);
5061 if (ret)
5062 mlog_errno(ret);
5063 } else {
Joel Beckerc495dd22009-02-13 02:19:11 -08005064 ret = ocfs2_try_to_merge_extent(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005065 split_index, split_rec,
Joel Beckerc495dd22009-02-13 02:19:11 -08005066 dealloc, &ctxt);
Mark Fasheh328d5752007-06-18 10:48:04 -07005067 if (ret)
5068 mlog_errno(ret);
5069 }
5070
Mark Fasheh328d5752007-06-18 10:48:04 -07005071out:
5072 brelse(last_eb_bh);
5073 return ret;
5074}
5075
5076/*
Tao Ma555936b2009-08-18 11:22:21 +08005077 * Change the flags of the already-existing extent at cpos for len clusters.
5078 *
5079 * new_flags: the flags we want to set.
5080 * clear_flags: the flags we want to clear.
5081 * phys: the new physical offset we want this new extent starts from.
Mark Fasheh328d5752007-06-18 10:48:04 -07005082 *
5083 * If the existing extent is larger than the request, initiate a
5084 * split. An attempt will be made at merging with adjacent extents.
5085 *
5086 * The caller is responsible for passing down meta_ac if we'll need it.
5087 */
Tao Ma1aa75fe2009-08-18 11:28:39 +08005088int ocfs2_change_extent_flag(handle_t *handle,
5089 struct ocfs2_extent_tree *et,
5090 u32 cpos, u32 len, u32 phys,
5091 struct ocfs2_alloc_context *meta_ac,
5092 struct ocfs2_cached_dealloc_ctxt *dealloc,
5093 int new_flags, int clear_flags)
Mark Fasheh328d5752007-06-18 10:48:04 -07005094{
5095 int ret, index;
Tao Ma555936b2009-08-18 11:22:21 +08005096 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5097 u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
Mark Fasheh328d5752007-06-18 10:48:04 -07005098 struct ocfs2_extent_rec split_rec;
5099 struct ocfs2_path *left_path = NULL;
5100 struct ocfs2_extent_list *el;
Tao Ma555936b2009-08-18 11:22:21 +08005101 struct ocfs2_extent_rec *rec;
Mark Fasheh328d5752007-06-18 10:48:04 -07005102
Joel Beckerffdd7a52008-10-17 22:32:01 -07005103 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005104 if (!left_path) {
5105 ret = -ENOMEM;
5106 mlog_errno(ret);
5107 goto out;
5108 }
5109
Joel Beckerfacdb772009-02-12 18:08:48 -08005110 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005111 if (ret) {
5112 mlog_errno(ret);
5113 goto out;
5114 }
5115 el = path_leaf_el(left_path);
5116
5117 index = ocfs2_search_extent_list(el, cpos);
5118 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Tao Ma555936b2009-08-18 11:22:21 +08005119 ocfs2_error(sb,
5120 "Owner %llu has an extent at cpos %u which can no "
Mark Fasheh328d5752007-06-18 10:48:04 -07005121 "longer be found.\n",
Tao Ma555936b2009-08-18 11:22:21 +08005122 (unsigned long long)
5123 ocfs2_metadata_cache_owner(et->et_ci), cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005124 ret = -EROFS;
5125 goto out;
5126 }
5127
Tao Ma555936b2009-08-18 11:22:21 +08005128 ret = -EIO;
5129 rec = &el->l_recs[index];
5130 if (new_flags && (rec->e_flags & new_flags)) {
5131 mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
5132 "extent that already had them",
5133 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5134 new_flags);
5135 goto out;
5136 }
5137
5138 if (clear_flags && !(rec->e_flags & clear_flags)) {
5139 mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
5140 "extent that didn't have them",
5141 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5142 clear_flags);
5143 goto out;
5144 }
5145
Mark Fasheh328d5752007-06-18 10:48:04 -07005146 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5147 split_rec.e_cpos = cpu_to_le32(cpos);
5148 split_rec.e_leaf_clusters = cpu_to_le16(len);
5149 split_rec.e_blkno = cpu_to_le64(start_blkno);
Tao Ma555936b2009-08-18 11:22:21 +08005150 split_rec.e_flags = rec->e_flags;
5151 if (new_flags)
5152 split_rec.e_flags |= new_flags;
5153 if (clear_flags)
5154 split_rec.e_flags &= ~clear_flags;
Mark Fasheh328d5752007-06-18 10:48:04 -07005155
Tao Mae2e9f602009-08-18 11:22:34 +08005156 ret = ocfs2_split_extent(handle, et, left_path,
5157 index, &split_rec, meta_ac,
5158 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005159 if (ret)
5160 mlog_errno(ret);
5161
5162out:
5163 ocfs2_free_path(left_path);
5164 return ret;
Tao Ma555936b2009-08-18 11:22:21 +08005165
5166}
5167
5168/*
5169 * Mark the already-existing extent at cpos as written for len clusters.
5170 * This removes the unwritten extent flag.
5171 *
5172 * If the existing extent is larger than the request, initiate a
5173 * split. An attempt will be made at merging with adjacent extents.
5174 *
5175 * The caller is responsible for passing down meta_ac if we'll need it.
5176 */
5177int ocfs2_mark_extent_written(struct inode *inode,
5178 struct ocfs2_extent_tree *et,
5179 handle_t *handle, u32 cpos, u32 len, u32 phys,
5180 struct ocfs2_alloc_context *meta_ac,
5181 struct ocfs2_cached_dealloc_ctxt *dealloc)
5182{
5183 int ret;
5184
5185 mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
5186 inode->i_ino, cpos, len, phys);
5187
5188 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5189 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5190 "that are being written to, but the feature bit "
5191 "is not set in the super block.",
5192 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5193 ret = -EROFS;
5194 goto out;
5195 }
5196
5197 /*
5198 * XXX: This should be fixed up so that we just re-insert the
5199 * next extent records.
5200 */
5201 ocfs2_et_extent_map_truncate(et, 0);
5202
5203 ret = ocfs2_change_extent_flag(handle, et, cpos,
5204 len, phys, meta_ac, dealloc,
5205 0, OCFS2_EXT_UNWRITTEN);
5206 if (ret)
5207 mlog_errno(ret);
5208
5209out:
5210 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07005211}
5212
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005213static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5214 struct ocfs2_path *path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005215 int index, u32 new_range,
5216 struct ocfs2_alloc_context *meta_ac)
5217{
Tao Mac901fb02010-04-26 14:34:57 +08005218 int ret, depth, credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005219 struct buffer_head *last_eb_bh = NULL;
5220 struct ocfs2_extent_block *eb;
5221 struct ocfs2_extent_list *rightmost_el, *el;
5222 struct ocfs2_extent_rec split_rec;
5223 struct ocfs2_extent_rec *rec;
5224 struct ocfs2_insert_type insert;
5225
5226 /*
5227 * Setup the record to split before we grow the tree.
5228 */
5229 el = path_leaf_el(path);
5230 rec = &el->l_recs[index];
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005231 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5232 &split_rec, new_range, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005233
5234 depth = path->p_tree_depth;
5235 if (depth > 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08005236 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005237 ocfs2_et_get_last_eb_blk(et),
5238 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005239 if (ret < 0) {
5240 mlog_errno(ret);
5241 goto out;
5242 }
5243
5244 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5245 rightmost_el = &eb->h_list;
5246 } else
5247 rightmost_el = path_leaf_el(path);
5248
Tao Mac901fb02010-04-26 14:34:57 +08005249 credits = path->p_tree_depth +
5250 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005251 ret = ocfs2_extend_trans(handle, credits);
5252 if (ret) {
5253 mlog_errno(ret);
5254 goto out;
5255 }
5256
5257 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5258 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08005259 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005260 meta_ac);
5261 if (ret) {
5262 mlog_errno(ret);
5263 goto out;
5264 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005265 }
5266
5267 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5268 insert.ins_appending = APPEND_NONE;
5269 insert.ins_contig = CONTIG_NONE;
5270 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005271 insert.ins_tree_depth = depth;
5272
Joel Becker3505bec2009-02-13 02:57:58 -08005273 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005274 if (ret)
5275 mlog_errno(ret);
5276
5277out:
5278 brelse(last_eb_bh);
5279 return ret;
5280}
5281
Joel Becker043beeb2009-02-13 02:42:30 -08005282static int ocfs2_truncate_rec(handle_t *handle,
5283 struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005284 struct ocfs2_path *path, int index,
5285 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker043beeb2009-02-13 02:42:30 -08005286 u32 cpos, u32 len)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005287{
5288 int ret;
5289 u32 left_cpos, rec_range, trunc_range;
5290 int wants_rotate = 0, is_rightmost_tree_rec = 0;
Joel Becker043beeb2009-02-13 02:42:30 -08005291 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005292 struct ocfs2_path *left_path = NULL;
5293 struct ocfs2_extent_list *el = path_leaf_el(path);
5294 struct ocfs2_extent_rec *rec;
5295 struct ocfs2_extent_block *eb;
5296
5297 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Joel Becker70f18c02009-02-13 02:09:31 -08005298 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005299 if (ret) {
5300 mlog_errno(ret);
5301 goto out;
5302 }
5303
5304 index--;
5305 }
5306
5307 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5308 path->p_tree_depth) {
5309 /*
5310 * Check whether this is the rightmost tree record. If
5311 * we remove all of this record or part of its right
5312 * edge then an update of the record lengths above it
5313 * will be required.
5314 */
5315 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5316 if (eb->h_next_leaf_blk == 0)
5317 is_rightmost_tree_rec = 1;
5318 }
5319
5320 rec = &el->l_recs[index];
5321 if (index == 0 && path->p_tree_depth &&
5322 le32_to_cpu(rec->e_cpos) == cpos) {
5323 /*
5324 * Changing the leftmost offset (via partial or whole
5325 * record truncate) of an interior (or rightmost) path
5326 * means we have to update the subtree that is formed
5327 * by this leaf and the one to it's left.
5328 *
5329 * There are two cases we can skip:
Joel Becker043beeb2009-02-13 02:42:30 -08005330 * 1) Path is the leftmost one in our btree.
Mark Fashehd0c7d702007-07-03 13:27:22 -07005331 * 2) The leaf is rightmost and will be empty after
5332 * we remove the extent record - the rotate code
5333 * knows how to update the newly formed edge.
5334 */
5335
Joel Becker043beeb2009-02-13 02:42:30 -08005336 ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005337 if (ret) {
5338 mlog_errno(ret);
5339 goto out;
5340 }
5341
5342 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005343 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005344 if (!left_path) {
5345 ret = -ENOMEM;
5346 mlog_errno(ret);
5347 goto out;
5348 }
5349
Joel Beckerfacdb772009-02-12 18:08:48 -08005350 ret = ocfs2_find_path(et->et_ci, left_path,
5351 left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005352 if (ret) {
5353 mlog_errno(ret);
5354 goto out;
5355 }
5356 }
5357 }
5358
5359 ret = ocfs2_extend_rotate_transaction(handle, 0,
5360 handle->h_buffer_credits,
5361 path);
5362 if (ret) {
5363 mlog_errno(ret);
5364 goto out;
5365 }
5366
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005367 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005368 if (ret) {
5369 mlog_errno(ret);
5370 goto out;
5371 }
5372
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005373 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005374 if (ret) {
5375 mlog_errno(ret);
5376 goto out;
5377 }
5378
5379 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5380 trunc_range = cpos + len;
5381
5382 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5383 int next_free;
5384
5385 memset(rec, 0, sizeof(*rec));
5386 ocfs2_cleanup_merge(el, index);
5387 wants_rotate = 1;
5388
5389 next_free = le16_to_cpu(el->l_next_free_rec);
5390 if (is_rightmost_tree_rec && next_free > 1) {
5391 /*
5392 * We skip the edge update if this path will
5393 * be deleted by the rotate code.
5394 */
5395 rec = &el->l_recs[next_free - 1];
Joel Beckerd401dc12009-02-13 02:24:10 -08005396 ocfs2_adjust_rightmost_records(handle, et, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005397 rec);
5398 }
5399 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5400 /* Remove leftmost portion of the record. */
5401 le32_add_cpu(&rec->e_cpos, len);
5402 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5403 le16_add_cpu(&rec->e_leaf_clusters, -len);
5404 } else if (rec_range == trunc_range) {
5405 /* Remove rightmost portion of the record */
5406 le16_add_cpu(&rec->e_leaf_clusters, -len);
5407 if (is_rightmost_tree_rec)
Joel Beckerd401dc12009-02-13 02:24:10 -08005408 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005409 } else {
5410 /* Caller should have trapped this. */
Joel Becker043beeb2009-02-13 02:42:30 -08005411 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5412 "(%u, %u)\n",
5413 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005414 le32_to_cpu(rec->e_cpos),
5415 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5416 BUG();
5417 }
5418
5419 if (left_path) {
5420 int subtree_index;
5421
Joel Becker7dc02802009-02-12 19:20:13 -08005422 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Joel Becker4619c732009-02-12 19:02:36 -08005423 ocfs2_complete_edge_insert(handle, left_path, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005424 subtree_index);
5425 }
5426
5427 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5428
Joel Becker70f18c02009-02-13 02:09:31 -08005429 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005430 if (ret) {
5431 mlog_errno(ret);
5432 goto out;
5433 }
5434
5435out:
5436 ocfs2_free_path(left_path);
5437 return ret;
5438}
5439
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005440int ocfs2_remove_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005441 struct ocfs2_extent_tree *et,
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005442 u32 cpos, u32 len,
Mark Fasheh063c4562007-07-03 13:34:11 -07005443 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005444 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005445{
5446 int ret, index;
5447 u32 rec_range, trunc_range;
5448 struct ocfs2_extent_rec *rec;
5449 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005450 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005451
Joel Becker4c911ee2009-02-13 02:50:12 -08005452 /*
5453 * XXX: Why are we truncating to 0 instead of wherever this
5454 * affects us?
5455 */
5456 ocfs2_et_extent_map_truncate(et, 0);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005457
Joel Beckerffdd7a52008-10-17 22:32:01 -07005458 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005459 if (!path) {
5460 ret = -ENOMEM;
5461 mlog_errno(ret);
5462 goto out;
5463 }
5464
Joel Beckerfacdb772009-02-12 18:08:48 -08005465 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005466 if (ret) {
5467 mlog_errno(ret);
5468 goto out;
5469 }
5470
5471 el = path_leaf_el(path);
5472 index = ocfs2_search_extent_list(el, cpos);
5473 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005474 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5475 "Owner %llu has an extent at cpos %u which can no "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005476 "longer be found.\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005477 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5478 cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005479 ret = -EROFS;
5480 goto out;
5481 }
5482
5483 /*
5484 * We have 3 cases of extent removal:
5485 * 1) Range covers the entire extent rec
5486 * 2) Range begins or ends on one edge of the extent rec
5487 * 3) Range is in the middle of the extent rec (no shared edges)
5488 *
5489 * For case 1 we remove the extent rec and left rotate to
5490 * fill the hole.
5491 *
5492 * For case 2 we just shrink the existing extent rec, with a
5493 * tree update if the shrinking edge is also the edge of an
5494 * extent block.
5495 *
5496 * For case 3 we do a right split to turn the extent rec into
5497 * something case 2 can handle.
5498 */
5499 rec = &el->l_recs[index];
5500 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5501 trunc_range = cpos + len;
5502
5503 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5504
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005505 mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005506 "(cpos %u, len %u)\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005507 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5508 cpos, len, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005509 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5510
5511 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
Joel Becker043beeb2009-02-13 02:42:30 -08005512 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5513 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005514 if (ret) {
5515 mlog_errno(ret);
5516 goto out;
5517 }
5518 } else {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005519 ret = ocfs2_split_tree(handle, et, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005520 trunc_range, meta_ac);
5521 if (ret) {
5522 mlog_errno(ret);
5523 goto out;
5524 }
5525
5526 /*
5527 * The split could have manipulated the tree enough to
5528 * move the record location, so we have to look for it again.
5529 */
5530 ocfs2_reinit_path(path, 1);
5531
Joel Beckerfacdb772009-02-12 18:08:48 -08005532 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005533 if (ret) {
5534 mlog_errno(ret);
5535 goto out;
5536 }
5537
5538 el = path_leaf_el(path);
5539 index = ocfs2_search_extent_list(el, cpos);
5540 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005541 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5542 "Owner %llu: split at cpos %u lost record.",
5543 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005544 cpos);
5545 ret = -EROFS;
5546 goto out;
5547 }
5548
5549 /*
5550 * Double check our values here. If anything is fishy,
5551 * it's easier to catch it at the top level.
5552 */
5553 rec = &el->l_recs[index];
5554 rec_range = le32_to_cpu(rec->e_cpos) +
5555 ocfs2_rec_clusters(el, rec);
5556 if (rec_range != trunc_range) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005557 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5558 "Owner %llu: error after split at cpos %u"
Mark Fashehd0c7d702007-07-03 13:27:22 -07005559 "trunc len %u, existing record is (%u,%u)",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005560 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005561 cpos, len, le32_to_cpu(rec->e_cpos),
5562 ocfs2_rec_clusters(el, rec));
5563 ret = -EROFS;
5564 goto out;
5565 }
5566
Joel Becker043beeb2009-02-13 02:42:30 -08005567 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5568 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005569 if (ret) {
5570 mlog_errno(ret);
5571 goto out;
5572 }
5573 }
5574
5575out:
5576 ocfs2_free_path(path);
5577 return ret;
5578}
5579
Tristan Ye78f94672010-05-11 17:54:42 +08005580/*
5581 * ocfs2_reserve_blocks_for_rec_trunc() would look basically the
5582 * same as ocfs2_lock_alloctors(), except for it accepts a blocks
5583 * number to reserve some extra blocks, and it only handles meta
5584 * data allocations.
5585 *
5586 * Currently, only ocfs2_remove_btree_range() uses it for truncating
5587 * and punching holes.
5588 */
5589static int ocfs2_reserve_blocks_for_rec_trunc(struct inode *inode,
5590 struct ocfs2_extent_tree *et,
5591 u32 extents_to_split,
5592 struct ocfs2_alloc_context **ac,
5593 int extra_blocks)
5594{
5595 int ret = 0, num_free_extents;
5596 unsigned int max_recs_needed = 2 * extents_to_split;
5597 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5598
5599 *ac = NULL;
5600
5601 num_free_extents = ocfs2_num_free_extents(osb, et);
5602 if (num_free_extents < 0) {
5603 ret = num_free_extents;
5604 mlog_errno(ret);
5605 goto out;
5606 }
5607
5608 if (!num_free_extents ||
5609 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed))
5610 extra_blocks += ocfs2_extend_meta_needed(et->et_root_el);
5611
5612 if (extra_blocks) {
5613 ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, ac);
5614 if (ret < 0) {
5615 if (ret != -ENOSPC)
5616 mlog_errno(ret);
5617 goto out;
5618 }
5619 }
5620
5621out:
5622 if (ret) {
5623 if (*ac) {
5624 ocfs2_free_alloc_context(*ac);
5625 *ac = NULL;
5626 }
5627 }
5628
5629 return ret;
5630}
5631
Mark Fashehfecc0112008-11-12 15:16:38 -08005632int ocfs2_remove_btree_range(struct inode *inode,
5633 struct ocfs2_extent_tree *et,
Tristan Ye78f94672010-05-11 17:54:42 +08005634 u32 cpos, u32 phys_cpos, u32 len, int flags,
5635 struct ocfs2_cached_dealloc_ctxt *dealloc,
5636 u64 refcount_loc)
Mark Fashehfecc0112008-11-12 15:16:38 -08005637{
Tristan Ye78f94672010-05-11 17:54:42 +08005638 int ret, credits = 0, extra_blocks = 0;
Mark Fashehfecc0112008-11-12 15:16:38 -08005639 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5640 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5641 struct inode *tl_inode = osb->osb_tl_inode;
5642 handle_t *handle;
5643 struct ocfs2_alloc_context *meta_ac = NULL;
Tristan Ye78f94672010-05-11 17:54:42 +08005644 struct ocfs2_refcount_tree *ref_tree = NULL;
Mark Fashehfecc0112008-11-12 15:16:38 -08005645
Tristan Ye78f94672010-05-11 17:54:42 +08005646 if ((flags & OCFS2_EXT_REFCOUNTED) && len) {
5647 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
5648 OCFS2_HAS_REFCOUNT_FL));
5649
5650 ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
5651 &ref_tree, NULL);
5652 if (ret) {
5653 mlog_errno(ret);
5654 goto out;
5655 }
5656
5657 ret = ocfs2_prepare_refcount_change_for_del(inode,
5658 refcount_loc,
5659 phys_blkno,
5660 len,
5661 &credits,
5662 &extra_blocks);
5663 if (ret < 0) {
5664 mlog_errno(ret);
5665 goto out;
5666 }
5667 }
5668
5669 ret = ocfs2_reserve_blocks_for_rec_trunc(inode, et, 1, &meta_ac,
5670 extra_blocks);
Mark Fashehfecc0112008-11-12 15:16:38 -08005671 if (ret) {
5672 mlog_errno(ret);
5673 return ret;
5674 }
5675
5676 mutex_lock(&tl_inode->i_mutex);
5677
5678 if (ocfs2_truncate_log_needs_flush(osb)) {
5679 ret = __ocfs2_flush_truncate_log(osb);
5680 if (ret < 0) {
5681 mlog_errno(ret);
5682 goto out;
5683 }
5684 }
5685
Tristan Ye78f94672010-05-11 17:54:42 +08005686 handle = ocfs2_start_trans(osb,
5687 ocfs2_remove_extent_credits(osb->sb) + credits);
Mark Fashehfecc0112008-11-12 15:16:38 -08005688 if (IS_ERR(handle)) {
5689 ret = PTR_ERR(handle);
5690 mlog_errno(ret);
5691 goto out;
5692 }
5693
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005694 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07005695 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005696 if (ret) {
5697 mlog_errno(ret);
5698 goto out;
5699 }
5700
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005701 dquot_free_space_nodirty(inode,
Mark Fashehfd4ef232009-01-29 15:06:21 -08005702 ocfs2_clusters_to_bytes(inode->i_sb, len));
5703
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005704 ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
Mark Fashehfecc0112008-11-12 15:16:38 -08005705 if (ret) {
5706 mlog_errno(ret);
5707 goto out_commit;
5708 }
5709
Joel Becker6136ca52009-02-12 19:32:43 -08005710 ocfs2_et_update_clusters(et, -len);
Mark Fashehfecc0112008-11-12 15:16:38 -08005711
Joel Beckerec20cec2010-03-19 14:13:52 -07005712 ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehfecc0112008-11-12 15:16:38 -08005713
Tristan Ye78f94672010-05-11 17:54:42 +08005714 if (phys_blkno) {
5715 if (flags & OCFS2_EXT_REFCOUNTED)
5716 ret = ocfs2_decrease_refcount(inode, handle,
5717 ocfs2_blocks_to_clusters(osb->sb,
5718 phys_blkno),
5719 len, meta_ac,
5720 dealloc, 1);
5721 else
5722 ret = ocfs2_truncate_log_append(osb, handle,
5723 phys_blkno, len);
5724 if (ret)
5725 mlog_errno(ret);
5726
5727 }
Mark Fashehfecc0112008-11-12 15:16:38 -08005728
5729out_commit:
5730 ocfs2_commit_trans(osb, handle);
5731out:
5732 mutex_unlock(&tl_inode->i_mutex);
5733
5734 if (meta_ac)
5735 ocfs2_free_alloc_context(meta_ac);
5736
Tristan Ye78f94672010-05-11 17:54:42 +08005737 if (ref_tree)
5738 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
5739
Mark Fashehfecc0112008-11-12 15:16:38 -08005740 return ret;
5741}
5742
Mark Fasheh063c4562007-07-03 13:34:11 -07005743int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005744{
5745 struct buffer_head *tl_bh = osb->osb_tl_bh;
5746 struct ocfs2_dinode *di;
5747 struct ocfs2_truncate_log *tl;
5748
5749 di = (struct ocfs2_dinode *) tl_bh->b_data;
5750 tl = &di->id2.i_dealloc;
5751
5752 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5753 "slot %d, invalid truncate log parameters: used = "
5754 "%u, count = %u\n", osb->slot_num,
5755 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5756 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5757}
5758
5759static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5760 unsigned int new_start)
5761{
5762 unsigned int tail_index;
5763 unsigned int current_tail;
5764
5765 /* No records, nothing to coalesce */
5766 if (!le16_to_cpu(tl->tl_used))
5767 return 0;
5768
5769 tail_index = le16_to_cpu(tl->tl_used) - 1;
5770 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5771 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5772
5773 return current_tail == new_start;
5774}
5775
Mark Fasheh063c4562007-07-03 13:34:11 -07005776int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5777 handle_t *handle,
5778 u64 start_blk,
5779 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005780{
5781 int status, index;
5782 unsigned int start_cluster, tl_count;
5783 struct inode *tl_inode = osb->osb_tl_inode;
5784 struct buffer_head *tl_bh = osb->osb_tl_bh;
5785 struct ocfs2_dinode *di;
5786 struct ocfs2_truncate_log *tl;
5787
Tao Maef6b6892011-02-21 11:10:44 +08005788 mlog(0, "start_blk = %llu, num_clusters = %u\n",
5789 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005790
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005791 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005792
5793 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5794
5795 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005796
Joel Becker10995aa2008-11-13 14:49:12 -08005797 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5798 * by the underlying call to ocfs2_read_inode_block(), so any
5799 * corruption is a code bug */
5800 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5801
5802 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005803 tl_count = le16_to_cpu(tl->tl_count);
5804 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5805 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005806 "Truncate record count on #%llu invalid "
5807 "wanted %u, actual %u\n",
5808 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005809 ocfs2_truncate_recs_per_inode(osb->sb),
5810 le16_to_cpu(tl->tl_count));
5811
5812 /* Caller should have known to flush before calling us. */
5813 index = le16_to_cpu(tl->tl_used);
5814 if (index >= tl_count) {
5815 status = -ENOSPC;
5816 mlog_errno(status);
5817 goto bail;
5818 }
5819
Joel Becker0cf2f762009-02-12 16:41:25 -08005820 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005821 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005822 if (status < 0) {
5823 mlog_errno(status);
5824 goto bail;
5825 }
5826
5827 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005828 "%llu (index = %d)\n", num_clusters, start_cluster,
5829 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005830
5831 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5832 /*
5833 * Move index back to the record we are coalescing with.
5834 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5835 */
5836 index--;
5837
5838 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5839 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5840 index, le32_to_cpu(tl->tl_recs[index].t_start),
5841 num_clusters);
5842 } else {
5843 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5844 tl->tl_used = cpu_to_le16(index + 1);
5845 }
5846 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5847
Joel Beckerec20cec2010-03-19 14:13:52 -07005848 ocfs2_journal_dirty(handle, tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005849
Tao Ma50308d82010-11-04 15:14:11 +08005850 osb->truncated_clusters += num_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08005851bail:
5852 mlog_exit(status);
5853 return status;
5854}
5855
5856static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005857 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005858 struct inode *data_alloc_inode,
5859 struct buffer_head *data_alloc_bh)
5860{
5861 int status = 0;
5862 int i;
5863 unsigned int num_clusters;
5864 u64 start_blk;
5865 struct ocfs2_truncate_rec rec;
5866 struct ocfs2_dinode *di;
5867 struct ocfs2_truncate_log *tl;
5868 struct inode *tl_inode = osb->osb_tl_inode;
5869 struct buffer_head *tl_bh = osb->osb_tl_bh;
5870
Mark Fashehccd979b2005-12-15 14:31:24 -08005871 di = (struct ocfs2_dinode *) tl_bh->b_data;
5872 tl = &di->id2.i_dealloc;
5873 i = le16_to_cpu(tl->tl_used) - 1;
5874 while (i >= 0) {
5875 /* Caller has given us at least enough credits to
5876 * update the truncate log dinode */
Joel Becker0cf2f762009-02-12 16:41:25 -08005877 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005878 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005879 if (status < 0) {
5880 mlog_errno(status);
5881 goto bail;
5882 }
5883
5884 tl->tl_used = cpu_to_le16(i);
5885
Joel Beckerec20cec2010-03-19 14:13:52 -07005886 ocfs2_journal_dirty(handle, tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005887
5888 /* TODO: Perhaps we can calculate the bulk of the
5889 * credits up front rather than extending like
5890 * this. */
5891 status = ocfs2_extend_trans(handle,
5892 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5893 if (status < 0) {
5894 mlog_errno(status);
5895 goto bail;
5896 }
5897
5898 rec = tl->tl_recs[i];
5899 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5900 le32_to_cpu(rec.t_start));
5901 num_clusters = le32_to_cpu(rec.t_clusters);
5902
5903 /* if start_blk is not set, we ignore the record as
5904 * invalid. */
5905 if (start_blk) {
5906 mlog(0, "free record %d, start = %u, clusters = %u\n",
5907 i, le32_to_cpu(rec.t_start), num_clusters);
5908
5909 status = ocfs2_free_clusters(handle, data_alloc_inode,
5910 data_alloc_bh, start_blk,
5911 num_clusters);
5912 if (status < 0) {
5913 mlog_errno(status);
5914 goto bail;
5915 }
5916 }
5917 i--;
5918 }
5919
Tao Ma50308d82010-11-04 15:14:11 +08005920 osb->truncated_clusters = 0;
5921
Mark Fashehccd979b2005-12-15 14:31:24 -08005922bail:
5923 mlog_exit(status);
5924 return status;
5925}
5926
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005927/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005928int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005929{
5930 int status;
5931 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005932 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005933 struct inode *tl_inode = osb->osb_tl_inode;
5934 struct inode *data_alloc_inode = NULL;
5935 struct buffer_head *tl_bh = osb->osb_tl_bh;
5936 struct buffer_head *data_alloc_bh = NULL;
5937 struct ocfs2_dinode *di;
5938 struct ocfs2_truncate_log *tl;
5939
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005940 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005941
5942 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005943
Joel Becker10995aa2008-11-13 14:49:12 -08005944 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5945 * by the underlying call to ocfs2_read_inode_block(), so any
5946 * corruption is a code bug */
5947 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5948
5949 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005950 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005951 mlog(0, "Flush %u records from truncate log #%llu\n",
5952 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005953 if (!num_to_flush) {
5954 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005955 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005956 }
5957
5958 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5959 GLOBAL_BITMAP_SYSTEM_INODE,
5960 OCFS2_INVALID_SLOT);
5961 if (!data_alloc_inode) {
5962 status = -EINVAL;
5963 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005964 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005965 }
5966
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005967 mutex_lock(&data_alloc_inode->i_mutex);
5968
Mark Fashehe63aecb62007-10-18 15:30:42 -07005969 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005970 if (status < 0) {
5971 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005972 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005973 }
5974
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005975 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005976 if (IS_ERR(handle)) {
5977 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005978 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005979 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005980 }
5981
5982 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5983 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005984 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005985 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005986
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005987 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005988
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005989out_unlock:
5990 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005991 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005992
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005993out_mutex:
5994 mutex_unlock(&data_alloc_inode->i_mutex);
5995 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005996
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005997out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005998 mlog_exit(status);
5999 return status;
6000}
6001
6002int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
6003{
6004 int status;
6005 struct inode *tl_inode = osb->osb_tl_inode;
6006
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006007 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006008 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006009 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006010
6011 return status;
6012}
6013
David Howellsc4028952006-11-22 14:57:56 +00006014static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08006015{
6016 int status;
David Howellsc4028952006-11-22 14:57:56 +00006017 struct ocfs2_super *osb =
6018 container_of(work, struct ocfs2_super,
6019 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08006020
Mark Fashehccd979b2005-12-15 14:31:24 -08006021 status = ocfs2_flush_truncate_log(osb);
6022 if (status < 0)
6023 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08006024 else
Tiger Yangb89c5422010-01-25 14:11:06 +08006025 ocfs2_init_steal_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08006026
6027 mlog_exit(status);
6028}
6029
6030#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
6031void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
6032 int cancel)
6033{
6034 if (osb->osb_tl_inode) {
6035 /* We want to push off log flushes while truncates are
6036 * still running. */
6037 if (cancel)
6038 cancel_delayed_work(&osb->osb_truncate_log_wq);
6039
6040 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
6041 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
6042 }
6043}
6044
6045static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
6046 int slot_num,
6047 struct inode **tl_inode,
6048 struct buffer_head **tl_bh)
6049{
6050 int status;
6051 struct inode *inode = NULL;
6052 struct buffer_head *bh = NULL;
6053
6054 inode = ocfs2_get_system_file_inode(osb,
6055 TRUNCATE_LOG_SYSTEM_INODE,
6056 slot_num);
6057 if (!inode) {
6058 status = -EINVAL;
6059 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
6060 goto bail;
6061 }
6062
Joel Beckerb657c952008-11-13 14:49:11 -08006063 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006064 if (status < 0) {
6065 iput(inode);
6066 mlog_errno(status);
6067 goto bail;
6068 }
6069
6070 *tl_inode = inode;
6071 *tl_bh = bh;
6072bail:
6073 mlog_exit(status);
6074 return status;
6075}
6076
6077/* called during the 1st stage of node recovery. we stamp a clean
6078 * truncate log and pass back a copy for processing later. if the
6079 * truncate log does not require processing, a *tl_copy is set to
6080 * NULL. */
6081int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6082 int slot_num,
6083 struct ocfs2_dinode **tl_copy)
6084{
6085 int status;
6086 struct inode *tl_inode = NULL;
6087 struct buffer_head *tl_bh = NULL;
6088 struct ocfs2_dinode *di;
6089 struct ocfs2_truncate_log *tl;
6090
6091 *tl_copy = NULL;
6092
6093 mlog(0, "recover truncate log from slot %d\n", slot_num);
6094
6095 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6096 if (status < 0) {
6097 mlog_errno(status);
6098 goto bail;
6099 }
6100
6101 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08006102
Joel Becker10995aa2008-11-13 14:49:12 -08006103 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6104 * validated by the underlying call to ocfs2_read_inode_block(),
6105 * so any corruption is a code bug */
6106 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6107
6108 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08006109 if (le16_to_cpu(tl->tl_used)) {
6110 mlog(0, "We'll have %u logs to recover\n",
6111 le16_to_cpu(tl->tl_used));
6112
6113 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6114 if (!(*tl_copy)) {
6115 status = -ENOMEM;
6116 mlog_errno(status);
6117 goto bail;
6118 }
6119
6120 /* Assuming the write-out below goes well, this copy
6121 * will be passed back to recovery for processing. */
6122 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6123
6124 /* All we need to do to clear the truncate log is set
6125 * tl_used. */
6126 tl->tl_used = 0;
6127
Joel Becker13723d02008-10-17 19:25:01 -07006128 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08006129 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08006130 if (status < 0) {
6131 mlog_errno(status);
6132 goto bail;
6133 }
6134 }
6135
6136bail:
6137 if (tl_inode)
6138 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006139 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006140
6141 if (status < 0 && (*tl_copy)) {
6142 kfree(*tl_copy);
6143 *tl_copy = NULL;
6144 }
6145
6146 mlog_exit(status);
6147 return status;
6148}
6149
6150int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6151 struct ocfs2_dinode *tl_copy)
6152{
6153 int status = 0;
6154 int i;
6155 unsigned int clusters, num_recs, start_cluster;
6156 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006157 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006158 struct inode *tl_inode = osb->osb_tl_inode;
6159 struct ocfs2_truncate_log *tl;
6160
Mark Fashehccd979b2005-12-15 14:31:24 -08006161 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6162 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6163 return -EINVAL;
6164 }
6165
6166 tl = &tl_copy->id2.i_dealloc;
6167 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08006168 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006169 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006170
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006171 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006172 for(i = 0; i < num_recs; i++) {
6173 if (ocfs2_truncate_log_needs_flush(osb)) {
6174 status = __ocfs2_flush_truncate_log(osb);
6175 if (status < 0) {
6176 mlog_errno(status);
6177 goto bail_up;
6178 }
6179 }
6180
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006181 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006182 if (IS_ERR(handle)) {
6183 status = PTR_ERR(handle);
6184 mlog_errno(status);
6185 goto bail_up;
6186 }
6187
6188 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6189 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6190 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6191
6192 status = ocfs2_truncate_log_append(osb, handle,
6193 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006194 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006195 if (status < 0) {
6196 mlog_errno(status);
6197 goto bail_up;
6198 }
6199 }
6200
6201bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006202 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006203
6204 mlog_exit(status);
6205 return status;
6206}
6207
6208void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6209{
6210 int status;
6211 struct inode *tl_inode = osb->osb_tl_inode;
6212
Mark Fashehccd979b2005-12-15 14:31:24 -08006213 if (tl_inode) {
6214 cancel_delayed_work(&osb->osb_truncate_log_wq);
6215 flush_workqueue(ocfs2_wq);
6216
6217 status = ocfs2_flush_truncate_log(osb);
6218 if (status < 0)
6219 mlog_errno(status);
6220
6221 brelse(osb->osb_tl_bh);
6222 iput(osb->osb_tl_inode);
6223 }
6224
6225 mlog_exit_void();
6226}
6227
6228int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6229{
6230 int status;
6231 struct inode *tl_inode = NULL;
6232 struct buffer_head *tl_bh = NULL;
6233
Mark Fashehccd979b2005-12-15 14:31:24 -08006234 status = ocfs2_get_truncate_log_info(osb,
6235 osb->slot_num,
6236 &tl_inode,
6237 &tl_bh);
6238 if (status < 0)
6239 mlog_errno(status);
6240
6241 /* ocfs2_truncate_log_shutdown keys on the existence of
6242 * osb->osb_tl_inode so we don't set any of the osb variables
6243 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006244 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6245 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006246 osb->osb_tl_bh = tl_bh;
6247 osb->osb_tl_inode = tl_inode;
6248
6249 mlog_exit(status);
6250 return status;
6251}
6252
Mark Fasheh2b604352007-06-22 15:45:27 -07006253/*
6254 * Delayed de-allocation of suballocator blocks.
6255 *
6256 * Some sets of block de-allocations might involve multiple suballocator inodes.
6257 *
6258 * The locking for this can get extremely complicated, especially when
6259 * the suballocator inodes to delete from aren't known until deep
6260 * within an unrelated codepath.
6261 *
6262 * ocfs2_extent_block structures are a good example of this - an inode
6263 * btree could have been grown by any number of nodes each allocating
6264 * out of their own suballoc inode.
6265 *
6266 * These structures allow the delay of block de-allocation until a
6267 * later time, when locking of multiple cluster inodes won't cause
6268 * deadlock.
6269 */
6270
6271/*
Tao Ma2891d292008-11-12 08:26:58 +08006272 * Describe a single bit freed from a suballocator. For the block
6273 * suballocators, it represents one block. For the global cluster
6274 * allocator, it represents some clusters and free_bit indicates
6275 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006276 */
6277struct ocfs2_cached_block_free {
6278 struct ocfs2_cached_block_free *free_next;
Tao Ma74380c42010-03-22 14:20:18 +08006279 u64 free_bg;
Mark Fasheh2b604352007-06-22 15:45:27 -07006280 u64 free_blk;
6281 unsigned int free_bit;
6282};
6283
6284struct ocfs2_per_slot_free_list {
6285 struct ocfs2_per_slot_free_list *f_next_suballocator;
6286 int f_inode_type;
6287 int f_slot;
6288 struct ocfs2_cached_block_free *f_first;
6289};
6290
Tao Ma2891d292008-11-12 08:26:58 +08006291static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6292 int sysfile_type,
6293 int slot,
6294 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006295{
6296 int ret;
6297 u64 bg_blkno;
6298 handle_t *handle;
6299 struct inode *inode;
6300 struct buffer_head *di_bh = NULL;
6301 struct ocfs2_cached_block_free *tmp;
6302
6303 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6304 if (!inode) {
6305 ret = -EINVAL;
6306 mlog_errno(ret);
6307 goto out;
6308 }
6309
6310 mutex_lock(&inode->i_mutex);
6311
Mark Fashehe63aecb62007-10-18 15:30:42 -07006312 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006313 if (ret) {
6314 mlog_errno(ret);
6315 goto out_mutex;
6316 }
6317
6318 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6319 if (IS_ERR(handle)) {
6320 ret = PTR_ERR(handle);
6321 mlog_errno(ret);
6322 goto out_unlock;
6323 }
6324
6325 while (head) {
Tao Ma74380c42010-03-22 14:20:18 +08006326 if (head->free_bg)
6327 bg_blkno = head->free_bg;
6328 else
6329 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6330 head->free_bit);
Mark Fasheh2b604352007-06-22 15:45:27 -07006331 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6332 head->free_bit, (unsigned long long)head->free_blk);
6333
6334 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6335 head->free_bit, bg_blkno, 1);
6336 if (ret) {
6337 mlog_errno(ret);
6338 goto out_journal;
6339 }
6340
6341 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6342 if (ret) {
6343 mlog_errno(ret);
6344 goto out_journal;
6345 }
6346
6347 tmp = head;
6348 head = head->free_next;
6349 kfree(tmp);
6350 }
6351
6352out_journal:
6353 ocfs2_commit_trans(osb, handle);
6354
6355out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006356 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006357 brelse(di_bh);
6358out_mutex:
6359 mutex_unlock(&inode->i_mutex);
6360 iput(inode);
6361out:
6362 while(head) {
6363 /* Premature exit may have left some dangling items. */
6364 tmp = head;
6365 head = head->free_next;
6366 kfree(tmp);
6367 }
6368
6369 return ret;
6370}
6371
Tao Ma2891d292008-11-12 08:26:58 +08006372int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6373 u64 blkno, unsigned int bit)
6374{
6375 int ret = 0;
6376 struct ocfs2_cached_block_free *item;
6377
Tao Ma74380c42010-03-22 14:20:18 +08006378 item = kzalloc(sizeof(*item), GFP_NOFS);
Tao Ma2891d292008-11-12 08:26:58 +08006379 if (item == NULL) {
6380 ret = -ENOMEM;
6381 mlog_errno(ret);
6382 return ret;
6383 }
6384
6385 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6386 bit, (unsigned long long)blkno);
6387
6388 item->free_blk = blkno;
6389 item->free_bit = bit;
6390 item->free_next = ctxt->c_global_allocator;
6391
6392 ctxt->c_global_allocator = item;
6393 return ret;
6394}
6395
6396static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6397 struct ocfs2_cached_block_free *head)
6398{
6399 struct ocfs2_cached_block_free *tmp;
6400 struct inode *tl_inode = osb->osb_tl_inode;
6401 handle_t *handle;
6402 int ret = 0;
6403
6404 mutex_lock(&tl_inode->i_mutex);
6405
6406 while (head) {
6407 if (ocfs2_truncate_log_needs_flush(osb)) {
6408 ret = __ocfs2_flush_truncate_log(osb);
6409 if (ret < 0) {
6410 mlog_errno(ret);
6411 break;
6412 }
6413 }
6414
6415 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6416 if (IS_ERR(handle)) {
6417 ret = PTR_ERR(handle);
6418 mlog_errno(ret);
6419 break;
6420 }
6421
6422 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6423 head->free_bit);
6424
6425 ocfs2_commit_trans(osb, handle);
6426 tmp = head;
6427 head = head->free_next;
6428 kfree(tmp);
6429
6430 if (ret < 0) {
6431 mlog_errno(ret);
6432 break;
6433 }
6434 }
6435
6436 mutex_unlock(&tl_inode->i_mutex);
6437
6438 while (head) {
6439 /* Premature exit may have left some dangling items. */
6440 tmp = head;
6441 head = head->free_next;
6442 kfree(tmp);
6443 }
6444
6445 return ret;
6446}
6447
Mark Fasheh2b604352007-06-22 15:45:27 -07006448int ocfs2_run_deallocs(struct ocfs2_super *osb,
6449 struct ocfs2_cached_dealloc_ctxt *ctxt)
6450{
6451 int ret = 0, ret2;
6452 struct ocfs2_per_slot_free_list *fl;
6453
6454 if (!ctxt)
6455 return 0;
6456
6457 while (ctxt->c_first_suballocator) {
6458 fl = ctxt->c_first_suballocator;
6459
6460 if (fl->f_first) {
6461 mlog(0, "Free items: (type %u, slot %d)\n",
6462 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006463 ret2 = ocfs2_free_cached_blocks(osb,
6464 fl->f_inode_type,
6465 fl->f_slot,
6466 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006467 if (ret2)
6468 mlog_errno(ret2);
6469 if (!ret)
6470 ret = ret2;
6471 }
6472
6473 ctxt->c_first_suballocator = fl->f_next_suballocator;
6474 kfree(fl);
6475 }
6476
Tao Ma2891d292008-11-12 08:26:58 +08006477 if (ctxt->c_global_allocator) {
6478 ret2 = ocfs2_free_cached_clusters(osb,
6479 ctxt->c_global_allocator);
6480 if (ret2)
6481 mlog_errno(ret2);
6482 if (!ret)
6483 ret = ret2;
6484
6485 ctxt->c_global_allocator = NULL;
6486 }
6487
Mark Fasheh2b604352007-06-22 15:45:27 -07006488 return ret;
6489}
6490
6491static struct ocfs2_per_slot_free_list *
6492ocfs2_find_per_slot_free_list(int type,
6493 int slot,
6494 struct ocfs2_cached_dealloc_ctxt *ctxt)
6495{
6496 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6497
6498 while (fl) {
6499 if (fl->f_inode_type == type && fl->f_slot == slot)
6500 return fl;
6501
6502 fl = fl->f_next_suballocator;
6503 }
6504
6505 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6506 if (fl) {
6507 fl->f_inode_type = type;
6508 fl->f_slot = slot;
6509 fl->f_first = NULL;
6510 fl->f_next_suballocator = ctxt->c_first_suballocator;
6511
6512 ctxt->c_first_suballocator = fl;
6513 }
6514 return fl;
6515}
6516
Tao Ma1823cb02009-08-18 11:24:49 +08006517int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
Tao Ma74380c42010-03-22 14:20:18 +08006518 int type, int slot, u64 suballoc,
6519 u64 blkno, unsigned int bit)
Mark Fasheh2b604352007-06-22 15:45:27 -07006520{
6521 int ret;
6522 struct ocfs2_per_slot_free_list *fl;
6523 struct ocfs2_cached_block_free *item;
6524
6525 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6526 if (fl == NULL) {
6527 ret = -ENOMEM;
6528 mlog_errno(ret);
6529 goto out;
6530 }
6531
Tao Ma74380c42010-03-22 14:20:18 +08006532 item = kzalloc(sizeof(*item), GFP_NOFS);
Mark Fasheh2b604352007-06-22 15:45:27 -07006533 if (item == NULL) {
6534 ret = -ENOMEM;
6535 mlog_errno(ret);
6536 goto out;
6537 }
6538
6539 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6540 type, slot, bit, (unsigned long long)blkno);
6541
Tao Ma74380c42010-03-22 14:20:18 +08006542 item->free_bg = suballoc;
Mark Fasheh2b604352007-06-22 15:45:27 -07006543 item->free_blk = blkno;
6544 item->free_bit = bit;
6545 item->free_next = fl->f_first;
6546
6547 fl->f_first = item;
6548
6549 ret = 0;
6550out:
6551 return ret;
6552}
6553
Mark Fasheh59a5e412007-06-22 15:52:36 -07006554static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6555 struct ocfs2_extent_block *eb)
6556{
6557 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6558 le16_to_cpu(eb->h_suballoc_slot),
Tao Ma74380c42010-03-22 14:20:18 +08006559 le64_to_cpu(eb->h_suballoc_loc),
Mark Fasheh59a5e412007-06-22 15:52:36 -07006560 le64_to_cpu(eb->h_blkno),
6561 le16_to_cpu(eb->h_suballoc_bit));
6562}
6563
Joel Becker2b4e30f2008-09-03 20:03:41 -07006564static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006565{
6566 set_buffer_uptodate(bh);
6567 mark_buffer_dirty(bh);
6568 return 0;
6569}
6570
Tao Ma6f70fa52009-08-25 08:05:12 +08006571void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6572 unsigned int from, unsigned int to,
6573 struct page *page, int zero, u64 *phys)
Mark Fasheh1d410a62007-09-07 14:20:45 -07006574{
6575 int ret, partial = 0;
6576
6577 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6578 if (ret)
6579 mlog_errno(ret);
6580
6581 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006582 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006583
6584 /*
6585 * Need to set the buffers we zero'd into uptodate
6586 * here if they aren't - ocfs2_map_page_blocks()
6587 * might've skipped some
6588 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006589 ret = walk_page_buffers(handle, page_buffers(page),
6590 from, to, &partial,
6591 ocfs2_zero_func);
6592 if (ret < 0)
6593 mlog_errno(ret);
6594 else if (ocfs2_should_order_data(inode)) {
6595 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006596 if (ret < 0)
6597 mlog_errno(ret);
6598 }
6599
6600 if (!partial)
6601 SetPageUptodate(page);
6602
6603 flush_dcache_page(page);
6604}
6605
Mark Fasheh35edec12007-07-06 14:41:18 -07006606static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6607 loff_t end, struct page **pages,
6608 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006609{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006610 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006611 struct page *page;
6612 unsigned int from, to = PAGE_CACHE_SIZE;
6613 struct super_block *sb = inode->i_sb;
6614
6615 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6616
6617 if (numpages == 0)
6618 goto out;
6619
Mark Fasheh35edec12007-07-06 14:41:18 -07006620 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006621 for(i = 0; i < numpages; i++) {
6622 page = pages[i];
6623
Mark Fasheh35edec12007-07-06 14:41:18 -07006624 from = start & (PAGE_CACHE_SIZE - 1);
6625 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6626 to = end & (PAGE_CACHE_SIZE - 1);
6627
Mark Fasheh60b11392007-02-16 11:46:50 -08006628 BUG_ON(from > PAGE_CACHE_SIZE);
6629 BUG_ON(to > PAGE_CACHE_SIZE);
6630
Mark Fasheh1d410a62007-09-07 14:20:45 -07006631 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6632 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006633
Mark Fasheh35edec12007-07-06 14:41:18 -07006634 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006635 }
6636out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006637 if (pages)
6638 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006639}
6640
Tao Ma6f70fa52009-08-25 08:05:12 +08006641int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
6642 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006643{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006644 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006645 struct address_space *mapping = inode->i_mapping;
6646 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006647 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006648
Mark Fasheh35edec12007-07-06 14:41:18 -07006649 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006650
Mark Fasheh1d410a62007-09-07 14:20:45 -07006651 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006652 last_page_bytes = PAGE_ALIGN(end);
6653 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006654 do {
Jan Kara9b4c0ff2010-08-24 14:28:03 +02006655 pages[numpages] = find_or_create_page(mapping, index, GFP_NOFS);
Mark Fasheh60b11392007-02-16 11:46:50 -08006656 if (!pages[numpages]) {
6657 ret = -ENOMEM;
6658 mlog_errno(ret);
6659 goto out;
6660 }
6661
6662 numpages++;
6663 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006664 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006665
6666out:
6667 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006668 if (pages)
6669 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006670 numpages = 0;
6671 }
6672
6673 *num = numpages;
6674
6675 return ret;
6676}
6677
Tao Ma6f70fa52009-08-25 08:05:12 +08006678static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6679 struct page **pages, int *num)
6680{
6681 struct super_block *sb = inode->i_sb;
6682
6683 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6684 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6685
6686 return ocfs2_grab_pages(inode, start, end, pages, num);
6687}
6688
Mark Fasheh60b11392007-02-16 11:46:50 -08006689/*
6690 * Zero the area past i_size but still within an allocated
6691 * cluster. This avoids exposing nonzero data on subsequent file
6692 * extends.
6693 *
6694 * We need to call this before i_size is updated on the inode because
6695 * otherwise block_write_full_page() will skip writeout of pages past
6696 * i_size. The new_i_size parameter is passed for this reason.
6697 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006698int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6699 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006700{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006701 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006702 struct page **pages = NULL;
6703 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006704 unsigned int ext_flags;
6705 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006706
6707 /*
6708 * File systems which don't support sparse files zero on every
6709 * extend.
6710 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07006711 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08006712 return 0;
6713
Mark Fasheh1d410a62007-09-07 14:20:45 -07006714 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08006715 sizeof(struct page *), GFP_NOFS);
6716 if (pages == NULL) {
6717 ret = -ENOMEM;
6718 mlog_errno(ret);
6719 goto out;
6720 }
6721
Mark Fasheh1d410a62007-09-07 14:20:45 -07006722 if (range_start == range_end)
6723 goto out;
6724
6725 ret = ocfs2_extent_map_get_blocks(inode,
6726 range_start >> sb->s_blocksize_bits,
6727 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08006728 if (ret) {
6729 mlog_errno(ret);
6730 goto out;
6731 }
6732
Mark Fasheh1d410a62007-09-07 14:20:45 -07006733 /*
6734 * Tail is a hole, or is marked unwritten. In either case, we
6735 * can count on read and write to return/push zero's.
6736 */
6737 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08006738 goto out;
6739
Mark Fasheh1d410a62007-09-07 14:20:45 -07006740 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6741 &numpages);
6742 if (ret) {
6743 mlog_errno(ret);
6744 goto out;
6745 }
6746
Mark Fasheh35edec12007-07-06 14:41:18 -07006747 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6748 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08006749
6750 /*
6751 * Initiate writeout of the pages we zero'd here. We don't
6752 * wait on them - the truncate_inode_pages() call later will
6753 * do that for us.
6754 */
Christoph Hellwig2cfd30a2009-09-23 15:04:02 +02006755 ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
6756 range_end - 1);
Mark Fasheh60b11392007-02-16 11:46:50 -08006757 if (ret)
6758 mlog_errno(ret);
6759
6760out:
6761 if (pages)
6762 kfree(pages);
6763
6764 return ret;
6765}
6766
Tiger Yangfdd77702008-08-18 17:08:55 +08006767static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6768 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006769{
6770 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08006771 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006772
Tiger Yangfdd77702008-08-18 17:08:55 +08006773 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6774 memset(&di->id2, 0, blocksize -
6775 offsetof(struct ocfs2_dinode, id2) -
6776 xattrsize);
6777 else
6778 memset(&di->id2, 0, blocksize -
6779 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006780}
6781
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006782void ocfs2_dinode_new_extent_list(struct inode *inode,
6783 struct ocfs2_dinode *di)
6784{
Tiger Yangfdd77702008-08-18 17:08:55 +08006785 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006786 di->id2.i_list.l_tree_depth = 0;
6787 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08006788 di->id2.i_list.l_count = cpu_to_le16(
6789 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006790}
6791
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006792void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6793{
6794 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6795 struct ocfs2_inline_data *idata = &di->id2.i_data;
6796
6797 spin_lock(&oi->ip_lock);
6798 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6799 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6800 spin_unlock(&oi->ip_lock);
6801
6802 /*
6803 * We clear the entire i_data structure here so that all
6804 * fields can be properly initialized.
6805 */
Tiger Yangfdd77702008-08-18 17:08:55 +08006806 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006807
Tiger Yangfdd77702008-08-18 17:08:55 +08006808 idata->id_count = cpu_to_le16(
6809 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006810}
6811
6812int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6813 struct buffer_head *di_bh)
6814{
6815 int ret, i, has_data, num_pages = 0;
6816 handle_t *handle;
6817 u64 uninitialized_var(block);
6818 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6819 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6820 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006821 struct ocfs2_alloc_context *data_ac = NULL;
6822 struct page **pages = NULL;
6823 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07006824 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02006825 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006826
6827 has_data = i_size_read(inode) ? 1 : 0;
6828
6829 if (has_data) {
6830 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6831 sizeof(struct page *), GFP_NOFS);
6832 if (pages == NULL) {
6833 ret = -ENOMEM;
6834 mlog_errno(ret);
6835 goto out;
6836 }
6837
6838 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6839 if (ret) {
6840 mlog_errno(ret);
6841 goto out;
6842 }
6843 }
6844
Jan Karaa90714c2008-10-09 19:38:40 +02006845 handle = ocfs2_start_trans(osb,
6846 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006847 if (IS_ERR(handle)) {
6848 ret = PTR_ERR(handle);
6849 mlog_errno(ret);
6850 goto out_unlock;
6851 }
6852
Joel Becker0cf2f762009-02-12 16:41:25 -08006853 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07006854 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006855 if (ret) {
6856 mlog_errno(ret);
6857 goto out_commit;
6858 }
6859
6860 if (has_data) {
6861 u32 bit_off, num;
6862 unsigned int page_end;
6863 u64 phys;
6864
Christoph Hellwig5dd40562010-03-03 09:05:00 -05006865 ret = dquot_alloc_space_nodirty(inode,
6866 ocfs2_clusters_to_bytes(osb->sb, 1));
6867 if (ret)
Jan Karaa90714c2008-10-09 19:38:40 +02006868 goto out_commit;
Jan Karaa90714c2008-10-09 19:38:40 +02006869 did_quota = 1;
6870
Mark Fasheh4fe370a2009-12-07 13:15:40 -08006871 data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
6872
Joel Becker1ed9b772010-05-06 13:59:06 +08006873 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006874 &num);
6875 if (ret) {
6876 mlog_errno(ret);
6877 goto out_commit;
6878 }
6879
6880 /*
6881 * Save two copies, one for insert, and one that can
6882 * be changed by ocfs2_map_and_dirty_page() below.
6883 */
6884 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6885
6886 /*
6887 * Non sparse file systems zero on extend, so no need
6888 * to do that now.
6889 */
6890 if (!ocfs2_sparse_alloc(osb) &&
6891 PAGE_CACHE_SIZE < osb->s_clustersize)
6892 end = PAGE_CACHE_SIZE;
6893
6894 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6895 if (ret) {
6896 mlog_errno(ret);
6897 goto out_commit;
6898 }
6899
6900 /*
6901 * This should populate the 1st page for us and mark
6902 * it up to date.
6903 */
6904 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6905 if (ret) {
6906 mlog_errno(ret);
6907 goto out_commit;
6908 }
6909
6910 page_end = PAGE_CACHE_SIZE;
6911 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6912 page_end = osb->s_clustersize;
6913
6914 for (i = 0; i < num_pages; i++)
6915 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6916 pages[i], i > 0, &phys);
6917 }
6918
6919 spin_lock(&oi->ip_lock);
6920 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6921 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6922 spin_unlock(&oi->ip_lock);
6923
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006924 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006925
6926 ocfs2_journal_dirty(handle, di_bh);
6927
6928 if (has_data) {
6929 /*
6930 * An error at this point should be extremely rare. If
6931 * this proves to be false, we could always re-build
6932 * the in-inode data from our pages.
6933 */
Joel Becker5e404e92009-02-13 03:54:22 -08006934 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
Joel Beckercc79d8c2009-02-13 03:24:43 -08006935 ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006936 if (ret) {
6937 mlog_errno(ret);
6938 goto out_commit;
6939 }
6940
6941 inode->i_blocks = ocfs2_inode_sector_count(inode);
6942 }
6943
6944out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02006945 if (ret < 0 && did_quota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05006946 dquot_free_space_nodirty(inode,
Jan Karaa90714c2008-10-09 19:38:40 +02006947 ocfs2_clusters_to_bytes(osb->sb, 1));
6948
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006949 ocfs2_commit_trans(osb, handle);
6950
6951out_unlock:
6952 if (data_ac)
6953 ocfs2_free_alloc_context(data_ac);
6954
6955out:
6956 if (pages) {
6957 ocfs2_unlock_and_free_pages(pages, num_pages);
6958 kfree(pages);
6959 }
6960
6961 return ret;
6962}
6963
Mark Fashehccd979b2005-12-15 14:31:24 -08006964/*
6965 * It is expected, that by the time you call this function,
6966 * inode->i_size and fe->i_size have been adjusted.
6967 *
6968 * WARNING: This will kfree the truncate context
6969 */
6970int ocfs2_commit_truncate(struct ocfs2_super *osb,
6971 struct inode *inode,
Tristan Ye78f94672010-05-11 17:54:42 +08006972 struct buffer_head *di_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08006973{
Tristan Ye78f94672010-05-11 17:54:42 +08006974 int status = 0, i, flags = 0;
6975 u32 new_highest_cpos, range, trunc_cpos, trunc_len, phys_cpos, coff;
Tao Mabcbbb242009-08-18 11:29:12 +08006976 u64 blkno = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08006977 struct ocfs2_extent_list *el;
Tristan Ye78f94672010-05-11 17:54:42 +08006978 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08006979 struct ocfs2_path *path = NULL;
Tristan Ye78f94672010-05-11 17:54:42 +08006980 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6981 struct ocfs2_extent_list *root_el = &(di->id2.i_list);
6982 u64 refcount_loc = le64_to_cpu(di->i_refcount_loc);
6983 struct ocfs2_extent_tree et;
6984 struct ocfs2_cached_dealloc_ctxt dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08006985
Tristan Ye78f94672010-05-11 17:54:42 +08006986 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
6987 ocfs2_init_dealloc_ctxt(&dealloc);
6988
Mark Fashehdcd05382007-01-16 11:32:23 -08006989 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08006990 i_size_read(inode));
6991
Tristan Ye78f94672010-05-11 17:54:42 +08006992 path = ocfs2_new_path(di_bh, &di->id2.i_list,
Joel Becker13723d02008-10-17 19:25:01 -07006993 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08006994 if (!path) {
6995 status = -ENOMEM;
6996 mlog_errno(status);
6997 goto bail;
6998 }
Mark Fasheh83418972007-04-23 18:53:12 -07006999
7000 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7001
Mark Fashehccd979b2005-12-15 14:31:24 -08007002start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007003 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007004 * Check that we still have allocation to delete.
7005 */
7006 if (OCFS2_I(inode)->ip_clusters == 0) {
7007 status = 0;
7008 goto bail;
7009 }
7010
7011 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007012 * Truncate always works against the rightmost tree branch.
7013 */
Joel Beckerfacdb772009-02-12 18:08:48 -08007014 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
Mark Fashehdcd05382007-01-16 11:32:23 -08007015 if (status) {
7016 mlog_errno(status);
7017 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007018 }
7019
Mark Fashehdcd05382007-01-16 11:32:23 -08007020 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7021 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7022
7023 /*
7024 * By now, el will point to the extent list on the bottom most
7025 * portion of this tree. Only the tail record is considered in
7026 * each pass.
7027 *
7028 * We handle the following cases, in order:
7029 * - empty extent: delete the remaining branch
7030 * - remove the entire record
7031 * - remove a partial record
7032 * - no record needs to be removed (truncate has completed)
7033 */
7034 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007035 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7036 ocfs2_error(inode->i_sb,
7037 "Inode %llu has empty extent block at %llu\n",
7038 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7039 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7040 status = -EROFS;
7041 goto bail;
7042 }
7043
Mark Fashehccd979b2005-12-15 14:31:24 -08007044 i = le16_to_cpu(el->l_next_free_rec) - 1;
Tristan Ye78f94672010-05-11 17:54:42 +08007045 rec = &el->l_recs[i];
7046 flags = rec->e_flags;
7047 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
7048
7049 if (i == 0 && ocfs2_is_empty_extent(rec)) {
7050 /*
7051 * Lower levels depend on this never happening, but it's best
7052 * to check it up here before changing the tree.
7053 */
7054 if (root_el->l_tree_depth && rec->e_int_clusters == 0) {
7055 ocfs2_error(inode->i_sb, "Inode %lu has an empty "
7056 "extent record, depth %u\n", inode->i_ino,
7057 le16_to_cpu(root_el->l_tree_depth));
7058 status = -EROFS;
7059 goto bail;
7060 }
7061 trunc_cpos = le32_to_cpu(rec->e_cpos);
7062 trunc_len = 0;
7063 blkno = 0;
7064 } else if (le32_to_cpu(rec->e_cpos) >= new_highest_cpos) {
7065 /*
7066 * Truncate entire record.
7067 */
7068 trunc_cpos = le32_to_cpu(rec->e_cpos);
7069 trunc_len = ocfs2_rec_clusters(el, rec);
7070 blkno = le64_to_cpu(rec->e_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08007071 } else if (range > new_highest_cpos) {
Tristan Ye78f94672010-05-11 17:54:42 +08007072 /*
7073 * Partial truncate. it also should be
7074 * the last truncate we're doing.
7075 */
7076 trunc_cpos = new_highest_cpos;
7077 trunc_len = range - new_highest_cpos;
7078 coff = new_highest_cpos - le32_to_cpu(rec->e_cpos);
7079 blkno = le64_to_cpu(rec->e_blkno) +
7080 ocfs2_clusters_to_blocks(inode->i_sb, coff);
Mark Fashehdcd05382007-01-16 11:32:23 -08007081 } else {
Tristan Ye78f94672010-05-11 17:54:42 +08007082 /*
7083 * Truncate completed, leave happily.
7084 */
Mark Fashehdcd05382007-01-16 11:32:23 -08007085 status = 0;
7086 goto bail;
7087 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007088
Tristan Ye78f94672010-05-11 17:54:42 +08007089 phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08007090
Tristan Ye78f94672010-05-11 17:54:42 +08007091 status = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
7092 phys_cpos, trunc_len, flags, &dealloc,
7093 refcount_loc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007094 if (status < 0) {
7095 mlog_errno(status);
7096 goto bail;
7097 }
7098
Mark Fashehdcd05382007-01-16 11:32:23 -08007099 ocfs2_reinit_path(path, 1);
7100
7101 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007102 * The check above will catch the case where we've truncated
7103 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007104 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007105 goto start;
7106
Mark Fashehccd979b2005-12-15 14:31:24 -08007107bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007108
7109 ocfs2_schedule_truncate_log_flush(osb, 1);
7110
Tristan Ye78f94672010-05-11 17:54:42 +08007111 ocfs2_run_deallocs(osb, &dealloc);
Mark Fasheh59a5e412007-06-22 15:52:36 -07007112
Mark Fashehdcd05382007-01-16 11:32:23 -08007113 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007114
Mark Fashehccd979b2005-12-15 14:31:24 -08007115 mlog_exit(status);
7116 return status;
7117}
7118
Mark Fashehccd979b2005-12-15 14:31:24 -08007119/*
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007120 * 'start' is inclusive, 'end' is not.
7121 */
7122int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7123 unsigned int start, unsigned int end, int trunc)
7124{
7125 int ret;
7126 unsigned int numbytes;
7127 handle_t *handle;
7128 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7129 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7130 struct ocfs2_inline_data *idata = &di->id2.i_data;
7131
7132 if (end > i_size_read(inode))
7133 end = i_size_read(inode);
7134
7135 BUG_ON(start >= end);
7136
7137 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7138 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7139 !ocfs2_supports_inline_data(osb)) {
7140 ocfs2_error(inode->i_sb,
7141 "Inline data flags for inode %llu don't agree! "
7142 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7143 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7144 le16_to_cpu(di->i_dyn_features),
7145 OCFS2_I(inode)->ip_dyn_features,
7146 osb->s_feature_incompat);
7147 ret = -EROFS;
7148 goto out;
7149 }
7150
7151 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7152 if (IS_ERR(handle)) {
7153 ret = PTR_ERR(handle);
7154 mlog_errno(ret);
7155 goto out;
7156 }
7157
Joel Becker0cf2f762009-02-12 16:41:25 -08007158 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007159 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007160 if (ret) {
7161 mlog_errno(ret);
7162 goto out_commit;
7163 }
7164
7165 numbytes = end - start;
7166 memset(idata->id_data + start, 0, numbytes);
7167
7168 /*
7169 * No need to worry about the data page here - it's been
7170 * truncated already and inline data doesn't need it for
7171 * pushing zero's to disk, so we'll let readpage pick it up
7172 * later.
7173 */
7174 if (trunc) {
7175 i_size_write(inode, start);
7176 di->i_size = cpu_to_le64(start);
7177 }
7178
7179 inode->i_blocks = ocfs2_inode_sector_count(inode);
7180 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7181
7182 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7183 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7184
7185 ocfs2_journal_dirty(handle, di_bh);
7186
7187out_commit:
7188 ocfs2_commit_trans(osb, handle);
7189
7190out:
7191 return ret;
7192}