blob: 80ffbe3dd32f48adef47d9c62715eed727b10e01 [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
Tao Mac1e8d352011-03-07 16:43:21 +0800988 mlog(0, "retval = %d\n", retval);
Mark Fashehccd979b2005-12-15 14:31:24 -0800989 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 }
Tao Mac1e8d352011-03-07 16:43:21 +08001073 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001074 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001075 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
Mark Fashehccd979b2005-12-15 14:31:24 -08001329 return status;
1330}
1331
1332/*
1333 * adds another level to the allocation tree.
1334 * returns back the new extent block so you can add a branch to it
1335 * after this call.
1336 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001337static int ocfs2_shift_tree_depth(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001338 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001339 struct ocfs2_alloc_context *meta_ac,
1340 struct buffer_head **ret_new_eb_bh)
1341{
1342 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001343 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001344 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001345 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001346 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001347 struct ocfs2_extent_list *eb_el;
1348
Joel Becker42a5a7a2009-02-12 18:49:19 -08001349 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
Mark Fashehccd979b2005-12-15 14:31:24 -08001350 &new_eb_bh);
1351 if (status < 0) {
1352 mlog_errno(status);
1353 goto bail;
1354 }
1355
1356 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001357 /* ocfs2_create_new_meta_bhs() should create it right! */
1358 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001359
1360 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001361 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001362
Joel Beckerd401dc12009-02-13 02:24:10 -08001363 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001364 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001365 if (status < 0) {
1366 mlog_errno(status);
1367 goto bail;
1368 }
1369
Tao Mae7d4cb62008-08-18 17:38:44 +08001370 /* copy the root extent list data into the new extent block */
1371 eb_el->l_tree_depth = root_el->l_tree_depth;
1372 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1373 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1374 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001375
Joel Beckerec20cec2010-03-19 14:13:52 -07001376 ocfs2_journal_dirty(handle, new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001377
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001378 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001379 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001380 if (status < 0) {
1381 mlog_errno(status);
1382 goto bail;
1383 }
1384
Mark Fashehdcd05382007-01-16 11:32:23 -08001385 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1386
Tao Mae7d4cb62008-08-18 17:38:44 +08001387 /* update root_bh now */
1388 le16_add_cpu(&root_el->l_tree_depth, 1);
1389 root_el->l_recs[0].e_cpos = 0;
1390 root_el->l_recs[0].e_blkno = eb->h_blkno;
1391 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1392 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1393 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1394 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001395
1396 /* If this is our 1st tree depth shift, then last_eb_blk
1397 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001398 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001399 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001400
Joel Beckerec20cec2010-03-19 14:13:52 -07001401 ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001402
1403 *ret_new_eb_bh = new_eb_bh;
1404 new_eb_bh = NULL;
1405 status = 0;
1406bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001407 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001408
Mark Fashehccd979b2005-12-15 14:31:24 -08001409 return status;
1410}
1411
1412/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001413 * Should only be called when there is no space left in any of the
1414 * leaf nodes. What we want to do is find the lowest tree depth
1415 * non-leaf extent block with room for new records. There are three
1416 * valid results of this search:
1417 *
1418 * 1) a lowest extent block is found, then we pass it back in
1419 * *lowest_eb_bh and return '0'
1420 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001421 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001422 * pass NULL back in *lowest_eb_bh, but still return '0'
1423 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001424 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001425 * which case we return > 0
1426 *
1427 * return status < 0 indicates an error.
1428 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001429static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001430 struct buffer_head **target_bh)
1431{
1432 int status = 0, i;
1433 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001434 struct ocfs2_extent_block *eb;
1435 struct ocfs2_extent_list *el;
1436 struct buffer_head *bh = NULL;
1437 struct buffer_head *lowest_bh = NULL;
1438
Mark Fashehccd979b2005-12-15 14:31:24 -08001439 *target_bh = NULL;
1440
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001441 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001442
1443 while(le16_to_cpu(el->l_tree_depth) > 1) {
1444 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08001445 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1446 "Owner %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001447 "extent list (next_free_rec == 0)",
Joel Becker3d03a302009-02-12 17:49:26 -08001448 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001449 status = -EIO;
1450 goto bail;
1451 }
1452 i = le16_to_cpu(el->l_next_free_rec) - 1;
1453 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1454 if (!blkno) {
Joel Becker3d03a302009-02-12 17:49:26 -08001455 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1456 "Owner %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001457 "list where extent # %d has no physical "
1458 "block start",
Joel Becker3d03a302009-02-12 17:49:26 -08001459 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001460 status = -EIO;
1461 goto bail;
1462 }
1463
Mark Fasheha81cb882008-10-07 14:25:16 -07001464 brelse(bh);
1465 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001466
Joel Becker3d03a302009-02-12 17:49:26 -08001467 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001468 if (status < 0) {
1469 mlog_errno(status);
1470 goto bail;
1471 }
1472
1473 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001474 el = &eb->h_list;
1475
1476 if (le16_to_cpu(el->l_next_free_rec) <
1477 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001478 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001479 lowest_bh = bh;
1480 get_bh(lowest_bh);
1481 }
1482 }
1483
1484 /* If we didn't find one and the fe doesn't have any room,
1485 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001486 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001487 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001488 status = 1;
1489
1490 *target_bh = lowest_bh;
1491bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001492 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001493
Mark Fashehccd979b2005-12-15 14:31:24 -08001494 return status;
1495}
1496
Mark Fashehe48edee2007-03-07 16:46:57 -08001497/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001498 * Grow a b-tree so that it has more records.
1499 *
1500 * We might shift the tree depth in which case existing paths should
1501 * be considered invalid.
1502 *
1503 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001504 *
1505 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001506 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001507static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1508 int *final_depth, struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001509 struct ocfs2_alloc_context *meta_ac)
1510{
1511 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001512 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001513 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001514 struct buffer_head *bh = NULL;
1515
1516 BUG_ON(meta_ac == NULL);
1517
Joel Beckerd401dc12009-02-13 02:24:10 -08001518 shift = ocfs2_find_branch_target(et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001519 if (shift < 0) {
1520 ret = shift;
1521 mlog_errno(ret);
1522 goto out;
1523 }
1524
1525 /* We traveled all the way to the bottom of the allocation tree
1526 * and didn't find room for any more extents - we need to add
1527 * another tree level */
1528 if (shift) {
1529 BUG_ON(bh);
1530 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1531
1532 /* ocfs2_shift_tree_depth will return us a buffer with
1533 * the new extent block (so we can pass that to
1534 * ocfs2_add_branch). */
Joel Beckerd401dc12009-02-13 02:24:10 -08001535 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001536 if (ret < 0) {
1537 mlog_errno(ret);
1538 goto out;
1539 }
1540 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001541 if (depth == 1) {
1542 /*
1543 * Special case: we have room now if we shifted from
1544 * tree_depth 0, so no more work needs to be done.
1545 *
1546 * We won't be calling add_branch, so pass
1547 * back *last_eb_bh as the new leaf. At depth
1548 * zero, it should always be null so there's
1549 * no reason to brelse.
1550 */
1551 BUG_ON(*last_eb_bh);
1552 get_bh(bh);
1553 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001554 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001555 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001556 }
1557
1558 /* call ocfs2_add_branch to add the final part of the tree with
1559 * the new data. */
1560 mlog(0, "add branch. bh = %p\n", bh);
Joel Beckerd401dc12009-02-13 02:24:10 -08001561 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001562 meta_ac);
1563 if (ret < 0) {
1564 mlog_errno(ret);
1565 goto out;
1566 }
1567
1568out:
1569 if (final_depth)
1570 *final_depth = depth;
1571 brelse(bh);
1572 return ret;
1573}
1574
1575/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001576 * This function will discard the rightmost extent record.
1577 */
1578static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1579{
1580 int next_free = le16_to_cpu(el->l_next_free_rec);
1581 int count = le16_to_cpu(el->l_count);
1582 unsigned int num_bytes;
1583
1584 BUG_ON(!next_free);
1585 /* This will cause us to go off the end of our extent list. */
1586 BUG_ON(next_free >= count);
1587
1588 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1589
1590 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1591}
1592
1593static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1594 struct ocfs2_extent_rec *insert_rec)
1595{
1596 int i, insert_index, next_free, has_empty, num_bytes;
1597 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1598 struct ocfs2_extent_rec *rec;
1599
1600 next_free = le16_to_cpu(el->l_next_free_rec);
1601 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1602
1603 BUG_ON(!next_free);
1604
1605 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001606 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001607
1608 /*
1609 * The easiest way to approach this is to just remove the
1610 * empty extent and temporarily decrement next_free.
1611 */
1612 if (has_empty) {
1613 /*
1614 * If next_free was 1 (only an empty extent), this
1615 * loop won't execute, which is fine. We still want
1616 * the decrement above to happen.
1617 */
1618 for(i = 0; i < (next_free - 1); i++)
1619 el->l_recs[i] = el->l_recs[i+1];
1620
1621 next_free--;
1622 }
1623
1624 /*
1625 * Figure out what the new record index should be.
1626 */
1627 for(i = 0; i < next_free; i++) {
1628 rec = &el->l_recs[i];
1629
1630 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1631 break;
1632 }
1633 insert_index = i;
1634
1635 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1636 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1637
1638 BUG_ON(insert_index < 0);
1639 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1640 BUG_ON(insert_index > next_free);
1641
1642 /*
1643 * No need to memmove if we're just adding to the tail.
1644 */
1645 if (insert_index != next_free) {
1646 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1647
1648 num_bytes = next_free - insert_index;
1649 num_bytes *= sizeof(struct ocfs2_extent_rec);
1650 memmove(&el->l_recs[insert_index + 1],
1651 &el->l_recs[insert_index],
1652 num_bytes);
1653 }
1654
1655 /*
1656 * Either we had an empty extent, and need to re-increment or
1657 * there was no empty extent on a non full rightmost leaf node,
1658 * in which case we still need to increment.
1659 */
1660 next_free++;
1661 el->l_next_free_rec = cpu_to_le16(next_free);
1662 /*
1663 * Make sure none of the math above just messed up our tree.
1664 */
1665 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1666
1667 el->l_recs[insert_index] = *insert_rec;
1668
1669}
1670
Mark Fasheh328d5752007-06-18 10:48:04 -07001671static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1672{
1673 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1674
1675 BUG_ON(num_recs == 0);
1676
1677 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1678 num_recs--;
1679 size = num_recs * sizeof(struct ocfs2_extent_rec);
1680 memmove(&el->l_recs[0], &el->l_recs[1], size);
1681 memset(&el->l_recs[num_recs], 0,
1682 sizeof(struct ocfs2_extent_rec));
1683 el->l_next_free_rec = cpu_to_le16(num_recs);
1684 }
1685}
1686
Mark Fashehdcd05382007-01-16 11:32:23 -08001687/*
1688 * Create an empty extent record .
1689 *
1690 * l_next_free_rec may be updated.
1691 *
1692 * If an empty extent already exists do nothing.
1693 */
1694static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1695{
1696 int next_free = le16_to_cpu(el->l_next_free_rec);
1697
Mark Fashehe48edee2007-03-07 16:46:57 -08001698 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1699
Mark Fashehdcd05382007-01-16 11:32:23 -08001700 if (next_free == 0)
1701 goto set_and_inc;
1702
1703 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1704 return;
1705
1706 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1707 "Asked to create an empty extent in a full list:\n"
1708 "count = %u, tree depth = %u",
1709 le16_to_cpu(el->l_count),
1710 le16_to_cpu(el->l_tree_depth));
1711
1712 ocfs2_shift_records_right(el);
1713
1714set_and_inc:
1715 le16_add_cpu(&el->l_next_free_rec, 1);
1716 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1717}
1718
1719/*
1720 * For a rotation which involves two leaf nodes, the "root node" is
1721 * the lowest level tree node which contains a path to both leafs. This
1722 * resulting set of information can be used to form a complete "subtree"
1723 *
1724 * This function is passed two full paths from the dinode down to a
1725 * pair of adjacent leaves. It's task is to figure out which path
1726 * index contains the subtree root - this can be the root index itself
1727 * in a worst-case rotation.
1728 *
1729 * The array index of the subtree root is passed back.
1730 */
Tao Ma38a04e42009-11-30 14:32:19 +08001731int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1732 struct ocfs2_path *left,
1733 struct ocfs2_path *right)
Mark Fashehdcd05382007-01-16 11:32:23 -08001734{
1735 int i = 0;
1736
1737 /*
1738 * Check that the caller passed in two paths from the same tree.
1739 */
1740 BUG_ON(path_root_bh(left) != path_root_bh(right));
1741
1742 do {
1743 i++;
1744
1745 /*
1746 * The caller didn't pass two adjacent paths.
1747 */
1748 mlog_bug_on_msg(i > left->p_tree_depth,
Joel Becker7dc02802009-02-12 19:20:13 -08001749 "Owner %llu, left depth %u, right depth %u\n"
Mark Fashehdcd05382007-01-16 11:32:23 -08001750 "left leaf blk %llu, right leaf blk %llu\n",
Joel Becker7dc02802009-02-12 19:20:13 -08001751 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1752 left->p_tree_depth, right->p_tree_depth,
Mark Fashehdcd05382007-01-16 11:32:23 -08001753 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1754 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1755 } while (left->p_node[i].bh->b_blocknr ==
1756 right->p_node[i].bh->b_blocknr);
1757
1758 return i - 1;
1759}
1760
1761typedef void (path_insert_t)(void *, struct buffer_head *);
1762
1763/*
1764 * Traverse a btree path in search of cpos, starting at root_el.
1765 *
1766 * This code can be called with a cpos larger than the tree, in which
1767 * case it will return the rightmost path.
1768 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001769static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
Mark Fashehdcd05382007-01-16 11:32:23 -08001770 struct ocfs2_extent_list *root_el, u32 cpos,
1771 path_insert_t *func, void *data)
1772{
1773 int i, ret = 0;
1774 u32 range;
1775 u64 blkno;
1776 struct buffer_head *bh = NULL;
1777 struct ocfs2_extent_block *eb;
1778 struct ocfs2_extent_list *el;
1779 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08001780
1781 el = root_el;
1782 while (el->l_tree_depth) {
1783 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001784 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1785 "Owner %llu has empty extent list at "
Mark Fashehdcd05382007-01-16 11:32:23 -08001786 "depth %u\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001787 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001788 le16_to_cpu(el->l_tree_depth));
1789 ret = -EROFS;
1790 goto out;
1791
1792 }
1793
1794 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1795 rec = &el->l_recs[i];
1796
1797 /*
1798 * In the case that cpos is off the allocation
1799 * tree, this should just wind up returning the
1800 * rightmost record.
1801 */
1802 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001803 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001804 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1805 break;
1806 }
1807
1808 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1809 if (blkno == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001810 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1811 "Owner %llu has bad blkno in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001812 "at depth %u (index %d)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001813 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001814 le16_to_cpu(el->l_tree_depth), i);
1815 ret = -EROFS;
1816 goto out;
1817 }
1818
1819 brelse(bh);
1820 bh = NULL;
Joel Beckerfacdb772009-02-12 18:08:48 -08001821 ret = ocfs2_read_extent_block(ci, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001822 if (ret) {
1823 mlog_errno(ret);
1824 goto out;
1825 }
1826
1827 eb = (struct ocfs2_extent_block *) bh->b_data;
1828 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001829
1830 if (le16_to_cpu(el->l_next_free_rec) >
1831 le16_to_cpu(el->l_count)) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001832 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1833 "Owner %llu has bad count in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001834 "at block %llu (next free=%u, count=%u)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001835 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001836 (unsigned long long)bh->b_blocknr,
1837 le16_to_cpu(el->l_next_free_rec),
1838 le16_to_cpu(el->l_count));
1839 ret = -EROFS;
1840 goto out;
1841 }
1842
1843 if (func)
1844 func(data, bh);
1845 }
1846
1847out:
1848 /*
1849 * Catch any trailing bh that the loop didn't handle.
1850 */
1851 brelse(bh);
1852
1853 return ret;
1854}
1855
1856/*
1857 * Given an initialized path (that is, it has a valid root extent
1858 * list), this function will traverse the btree in search of the path
1859 * which would contain cpos.
1860 *
1861 * The path traveled is recorded in the path structure.
1862 *
1863 * Note that this will not do any comparisons on leaf node extent
1864 * records, so it will work fine in the case that we just added a tree
1865 * branch.
1866 */
1867struct find_path_data {
1868 int index;
1869 struct ocfs2_path *path;
1870};
1871static void find_path_ins(void *data, struct buffer_head *bh)
1872{
1873 struct find_path_data *fp = data;
1874
1875 get_bh(bh);
1876 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1877 fp->index++;
1878}
Tao Mae2e9f602009-08-18 11:22:34 +08001879int ocfs2_find_path(struct ocfs2_caching_info *ci,
1880 struct ocfs2_path *path, u32 cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08001881{
1882 struct find_path_data data;
1883
1884 data.index = 1;
1885 data.path = path;
Joel Beckerfacdb772009-02-12 18:08:48 -08001886 return __ocfs2_find_path(ci, path_root_el(path), cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08001887 find_path_ins, &data);
1888}
1889
1890static void find_leaf_ins(void *data, struct buffer_head *bh)
1891{
1892 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1893 struct ocfs2_extent_list *el = &eb->h_list;
1894 struct buffer_head **ret = data;
1895
1896 /* We want to retain only the leaf block. */
1897 if (le16_to_cpu(el->l_tree_depth) == 0) {
1898 get_bh(bh);
1899 *ret = bh;
1900 }
1901}
1902/*
1903 * Find the leaf block in the tree which would contain cpos. No
1904 * checking of the actual leaf is done.
1905 *
1906 * Some paths want to call this instead of allocating a path structure
1907 * and calling ocfs2_find_path().
1908 *
1909 * This function doesn't handle non btree extent lists.
1910 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001911int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1912 struct ocfs2_extent_list *root_el, u32 cpos,
1913 struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001914{
1915 int ret;
1916 struct buffer_head *bh = NULL;
1917
Joel Beckerfacdb772009-02-12 18:08:48 -08001918 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001919 if (ret) {
1920 mlog_errno(ret);
1921 goto out;
1922 }
1923
1924 *leaf_bh = bh;
1925out:
1926 return ret;
1927}
1928
1929/*
1930 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1931 *
1932 * Basically, we've moved stuff around at the bottom of the tree and
1933 * we need to fix up the extent records above the changes to reflect
1934 * the new changes.
1935 *
1936 * left_rec: the record on the left.
1937 * left_child_el: is the child list pointed to by left_rec
1938 * right_rec: the record to the right of left_rec
1939 * right_child_el: is the child list pointed to by right_rec
1940 *
1941 * By definition, this only works on interior nodes.
1942 */
1943static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
1944 struct ocfs2_extent_list *left_child_el,
1945 struct ocfs2_extent_rec *right_rec,
1946 struct ocfs2_extent_list *right_child_el)
1947{
1948 u32 left_clusters, right_end;
1949
1950 /*
1951 * Interior nodes never have holes. Their cpos is the cpos of
1952 * the leftmost record in their child list. Their cluster
1953 * count covers the full theoretical range of their child list
1954 * - the range between their cpos and the cpos of the record
1955 * immediately to their right.
1956 */
1957 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08001958 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1959 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07001960 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1961 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1962 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001963 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001964 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001965
1966 /*
1967 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001968 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001969 * updating e_cpos to keep the same highest cluster count.
1970 */
1971 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001972 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001973
1974 right_rec->e_cpos = left_rec->e_cpos;
1975 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1976
1977 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001978 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001979}
1980
1981/*
1982 * Adjust the adjacent root node records involved in a
1983 * rotation. left_el_blkno is passed in as a key so that we can easily
1984 * find it's index in the root list.
1985 */
1986static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1987 struct ocfs2_extent_list *left_el,
1988 struct ocfs2_extent_list *right_el,
1989 u64 left_el_blkno)
1990{
1991 int i;
1992
1993 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1994 le16_to_cpu(left_el->l_tree_depth));
1995
1996 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1997 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1998 break;
1999 }
2000
2001 /*
2002 * The path walking code should have never returned a root and
2003 * two paths which are not adjacent.
2004 */
2005 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2006
2007 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
2008 &root_el->l_recs[i + 1], right_el);
2009}
2010
2011/*
2012 * We've changed a leaf block (in right_path) and need to reflect that
2013 * change back up the subtree.
2014 *
2015 * This happens in multiple places:
2016 * - When we've moved an extent record from the left path leaf to the right
2017 * path leaf to make room for an empty extent in the left path leaf.
2018 * - When our insert into the right path leaf is at the leftmost edge
2019 * and requires an update of the path immediately to it's left. This
2020 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08002021 * - When we've adjusted the last extent record in the left path leaf and the
2022 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08002023 */
Joel Becker4619c732009-02-12 19:02:36 -08002024static void ocfs2_complete_edge_insert(handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08002025 struct ocfs2_path *left_path,
2026 struct ocfs2_path *right_path,
2027 int subtree_index)
2028{
Joel Beckerec20cec2010-03-19 14:13:52 -07002029 int i, idx;
Mark Fashehdcd05382007-01-16 11:32:23 -08002030 struct ocfs2_extent_list *el, *left_el, *right_el;
2031 struct ocfs2_extent_rec *left_rec, *right_rec;
2032 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2033
2034 /*
2035 * Update the counts and position values within all the
2036 * interior nodes to reflect the leaf rotation we just did.
2037 *
2038 * The root node is handled below the loop.
2039 *
2040 * We begin the loop with right_el and left_el pointing to the
2041 * leaf lists and work our way up.
2042 *
2043 * NOTE: within this loop, left_el and right_el always refer
2044 * to the *child* lists.
2045 */
2046 left_el = path_leaf_el(left_path);
2047 right_el = path_leaf_el(right_path);
2048 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2049 mlog(0, "Adjust records at index %u\n", i);
2050
2051 /*
2052 * One nice property of knowing that all of these
2053 * nodes are below the root is that we only deal with
2054 * the leftmost right node record and the rightmost
2055 * left node record.
2056 */
2057 el = left_path->p_node[i].el;
2058 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2059 left_rec = &el->l_recs[idx];
2060
2061 el = right_path->p_node[i].el;
2062 right_rec = &el->l_recs[0];
2063
2064 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2065 right_el);
2066
Joel Beckerec20cec2010-03-19 14:13:52 -07002067 ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2068 ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002069
2070 /*
2071 * Setup our list pointers now so that the current
2072 * parents become children in the next iteration.
2073 */
2074 left_el = left_path->p_node[i].el;
2075 right_el = right_path->p_node[i].el;
2076 }
2077
2078 /*
2079 * At the root node, adjust the two adjacent records which
2080 * begin our path to the leaves.
2081 */
2082
2083 el = left_path->p_node[subtree_index].el;
2084 left_el = left_path->p_node[subtree_index + 1].el;
2085 right_el = right_path->p_node[subtree_index + 1].el;
2086
2087 ocfs2_adjust_root_records(el, left_el, right_el,
2088 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2089
2090 root_bh = left_path->p_node[subtree_index].bh;
2091
Joel Beckerec20cec2010-03-19 14:13:52 -07002092 ocfs2_journal_dirty(handle, root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002093}
2094
Joel Becker5c601ab2009-02-12 19:10:13 -08002095static int ocfs2_rotate_subtree_right(handle_t *handle,
2096 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08002097 struct ocfs2_path *left_path,
2098 struct ocfs2_path *right_path,
2099 int subtree_index)
2100{
2101 int ret, i;
2102 struct buffer_head *right_leaf_bh;
2103 struct buffer_head *left_leaf_bh = NULL;
2104 struct buffer_head *root_bh;
2105 struct ocfs2_extent_list *right_el, *left_el;
2106 struct ocfs2_extent_rec move_rec;
2107
2108 left_leaf_bh = path_leaf_bh(left_path);
2109 left_el = path_leaf_el(left_path);
2110
2111 if (left_el->l_next_free_rec != left_el->l_count) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002112 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002113 "Inode %llu has non-full interior leaf node %llu"
2114 "(next free = %u)",
Joel Becker5c601ab2009-02-12 19:10:13 -08002115 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002116 (unsigned long long)left_leaf_bh->b_blocknr,
2117 le16_to_cpu(left_el->l_next_free_rec));
2118 return -EROFS;
2119 }
2120
2121 /*
2122 * This extent block may already have an empty record, so we
2123 * return early if so.
2124 */
2125 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2126 return 0;
2127
2128 root_bh = left_path->p_node[subtree_index].bh;
2129 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2130
Joel Becker5c601ab2009-02-12 19:10:13 -08002131 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002132 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002133 if (ret) {
2134 mlog_errno(ret);
2135 goto out;
2136 }
2137
2138 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002139 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002140 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002141 if (ret) {
2142 mlog_errno(ret);
2143 goto out;
2144 }
2145
Joel Becker5c601ab2009-02-12 19:10:13 -08002146 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002147 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002148 if (ret) {
2149 mlog_errno(ret);
2150 goto out;
2151 }
2152 }
2153
2154 right_leaf_bh = path_leaf_bh(right_path);
2155 right_el = path_leaf_el(right_path);
2156
2157 /* This is a code error, not a disk corruption. */
2158 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2159 "because rightmost leaf block %llu is empty\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002160 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002161 (unsigned long long)right_leaf_bh->b_blocknr);
2162
2163 ocfs2_create_empty_extent(right_el);
2164
Joel Beckerec20cec2010-03-19 14:13:52 -07002165 ocfs2_journal_dirty(handle, right_leaf_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002166
2167 /* Do the copy now. */
2168 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2169 move_rec = left_el->l_recs[i];
2170 right_el->l_recs[0] = move_rec;
2171
2172 /*
2173 * Clear out the record we just copied and shift everything
2174 * over, leaving an empty extent in the left leaf.
2175 *
2176 * We temporarily subtract from next_free_rec so that the
2177 * shift will lose the tail record (which is now defunct).
2178 */
2179 le16_add_cpu(&left_el->l_next_free_rec, -1);
2180 ocfs2_shift_records_right(left_el);
2181 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2182 le16_add_cpu(&left_el->l_next_free_rec, 1);
2183
Joel Beckerec20cec2010-03-19 14:13:52 -07002184 ocfs2_journal_dirty(handle, left_leaf_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002185
Joel Becker4619c732009-02-12 19:02:36 -08002186 ocfs2_complete_edge_insert(handle, left_path, right_path,
2187 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002188
2189out:
2190 return ret;
2191}
2192
2193/*
2194 * Given a full path, determine what cpos value would return us a path
2195 * containing the leaf immediately to the left of the current one.
2196 *
2197 * Will return zero if the path passed in is already the leftmost path.
2198 */
Tristan Yeee149a72010-05-11 17:54:44 +08002199int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2200 struct ocfs2_path *path, u32 *cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08002201{
2202 int i, j, ret = 0;
2203 u64 blkno;
2204 struct ocfs2_extent_list *el;
2205
Mark Fashehe48edee2007-03-07 16:46:57 -08002206 BUG_ON(path->p_tree_depth == 0);
2207
Mark Fashehdcd05382007-01-16 11:32:23 -08002208 *cpos = 0;
2209
2210 blkno = path_leaf_bh(path)->b_blocknr;
2211
2212 /* Start at the tree node just above the leaf and work our way up. */
2213 i = path->p_tree_depth - 1;
2214 while (i >= 0) {
2215 el = path->p_node[i].el;
2216
2217 /*
2218 * Find the extent record just before the one in our
2219 * path.
2220 */
2221 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2222 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2223 if (j == 0) {
2224 if (i == 0) {
2225 /*
2226 * We've determined that the
2227 * path specified is already
2228 * the leftmost one - return a
2229 * cpos of zero.
2230 */
2231 goto out;
2232 }
2233 /*
2234 * The leftmost record points to our
2235 * leaf - we need to travel up the
2236 * tree one level.
2237 */
2238 goto next_node;
2239 }
2240
2241 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002242 *cpos = *cpos + ocfs2_rec_clusters(el,
2243 &el->l_recs[j - 1]);
2244 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002245 goto out;
2246 }
2247 }
2248
2249 /*
2250 * If we got here, we never found a valid node where
2251 * the tree indicated one should be.
2252 */
2253 ocfs2_error(sb,
2254 "Invalid extent tree at extent block %llu\n",
2255 (unsigned long long)blkno);
2256 ret = -EROFS;
2257 goto out;
2258
2259next_node:
2260 blkno = path->p_node[i].bh->b_blocknr;
2261 i--;
2262 }
2263
2264out:
2265 return ret;
2266}
2267
Mark Fasheh328d5752007-06-18 10:48:04 -07002268/*
2269 * Extend the transaction by enough credits to complete the rotation,
2270 * and still leave at least the original number of credits allocated
2271 * to this transaction.
2272 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002273static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002274 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002275 struct ocfs2_path *path)
2276{
Tao Mac901fb02010-04-26 14:34:57 +08002277 int ret = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07002278 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002279
Tao Mac901fb02010-04-26 14:34:57 +08002280 if (handle->h_buffer_credits < credits)
Tao Mac18b8122009-08-18 11:44:07 +08002281 ret = ocfs2_extend_trans(handle,
2282 credits - handle->h_buffer_credits);
Tao Mac18b8122009-08-18 11:44:07 +08002283
Tao Mac901fb02010-04-26 14:34:57 +08002284 return ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08002285}
2286
2287/*
2288 * Trap the case where we're inserting into the theoretical range past
2289 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2290 * whose cpos is less than ours into the right leaf.
2291 *
2292 * It's only necessary to look at the rightmost record of the left
2293 * leaf because the logic that calls us should ensure that the
2294 * theoretical ranges in the path components above the leaves are
2295 * correct.
2296 */
2297static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2298 u32 insert_cpos)
2299{
2300 struct ocfs2_extent_list *left_el;
2301 struct ocfs2_extent_rec *rec;
2302 int next_free;
2303
2304 left_el = path_leaf_el(left_path);
2305 next_free = le16_to_cpu(left_el->l_next_free_rec);
2306 rec = &left_el->l_recs[next_free - 1];
2307
2308 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2309 return 1;
2310 return 0;
2311}
2312
Mark Fasheh328d5752007-06-18 10:48:04 -07002313static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2314{
2315 int next_free = le16_to_cpu(el->l_next_free_rec);
2316 unsigned int range;
2317 struct ocfs2_extent_rec *rec;
2318
2319 if (next_free == 0)
2320 return 0;
2321
2322 rec = &el->l_recs[0];
2323 if (ocfs2_is_empty_extent(rec)) {
2324 /* Empty list. */
2325 if (next_free == 1)
2326 return 0;
2327 rec = &el->l_recs[1];
2328 }
2329
2330 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2331 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2332 return 1;
2333 return 0;
2334}
2335
Mark Fashehdcd05382007-01-16 11:32:23 -08002336/*
2337 * Rotate all the records in a btree right one record, starting at insert_cpos.
2338 *
2339 * The path to the rightmost leaf should be passed in.
2340 *
2341 * The array is assumed to be large enough to hold an entire path (tree depth).
2342 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002343 * Upon successful return from this function:
Mark Fashehdcd05382007-01-16 11:32:23 -08002344 *
2345 * - The 'right_path' array will contain a path to the leaf block
2346 * whose range contains e_cpos.
2347 * - That leaf block will have a single empty extent in list index 0.
2348 * - In the case that the rotation requires a post-insert update,
2349 * *ret_left_path will contain a valid path which can be passed to
2350 * ocfs2_insert_path().
2351 */
Joel Becker1bbf0b82009-02-12 19:42:08 -08002352static int ocfs2_rotate_tree_right(handle_t *handle,
Joel Becker5c601ab2009-02-12 19:10:13 -08002353 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002354 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002355 u32 insert_cpos,
2356 struct ocfs2_path *right_path,
2357 struct ocfs2_path **ret_left_path)
2358{
Mark Fasheh328d5752007-06-18 10:48:04 -07002359 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002360 u32 cpos;
2361 struct ocfs2_path *left_path = NULL;
Joel Becker5c601ab2009-02-12 19:10:13 -08002362 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehdcd05382007-01-16 11:32:23 -08002363
2364 *ret_left_path = NULL;
2365
Joel Beckerffdd7a52008-10-17 22:32:01 -07002366 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002367 if (!left_path) {
2368 ret = -ENOMEM;
2369 mlog_errno(ret);
2370 goto out;
2371 }
2372
Joel Becker5c601ab2009-02-12 19:10:13 -08002373 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002374 if (ret) {
2375 mlog_errno(ret);
2376 goto out;
2377 }
2378
2379 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2380
2381 /*
2382 * What we want to do here is:
2383 *
2384 * 1) Start with the rightmost path.
2385 *
2386 * 2) Determine a path to the leaf block directly to the left
2387 * of that leaf.
2388 *
2389 * 3) Determine the 'subtree root' - the lowest level tree node
2390 * which contains a path to both leaves.
2391 *
2392 * 4) Rotate the subtree.
2393 *
2394 * 5) Find the next subtree by considering the left path to be
2395 * the new right path.
2396 *
2397 * The check at the top of this while loop also accepts
2398 * insert_cpos == cpos because cpos is only a _theoretical_
2399 * value to get us the left path - insert_cpos might very well
2400 * be filling that hole.
2401 *
2402 * Stop at a cpos of '0' because we either started at the
2403 * leftmost branch (i.e., a tree with one branch and a
2404 * rotation inside of it), or we've gone as far as we can in
2405 * rotating subtrees.
2406 */
2407 while (cpos && insert_cpos <= cpos) {
2408 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2409 insert_cpos, cpos);
2410
Joel Becker5c601ab2009-02-12 19:10:13 -08002411 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002412 if (ret) {
2413 mlog_errno(ret);
2414 goto out;
2415 }
2416
2417 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2418 path_leaf_bh(right_path),
Joel Becker5c601ab2009-02-12 19:10:13 -08002419 "Owner %llu: error during insert of %u "
Mark Fashehdcd05382007-01-16 11:32:23 -08002420 "(left path cpos %u) results in two identical "
2421 "paths ending at %llu\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002422 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2423 insert_cpos, cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08002424 (unsigned long long)
2425 path_leaf_bh(left_path)->b_blocknr);
2426
Mark Fasheh328d5752007-06-18 10:48:04 -07002427 if (split == SPLIT_NONE &&
2428 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002429 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002430
2431 /*
2432 * We've rotated the tree as much as we
2433 * should. The rest is up to
2434 * ocfs2_insert_path() to complete, after the
2435 * record insertion. We indicate this
2436 * situation by returning the left path.
2437 *
2438 * The reason we don't adjust the records here
2439 * before the record insert is that an error
2440 * later might break the rule where a parent
2441 * record e_cpos will reflect the actual
2442 * e_cpos of the 1st nonempty record of the
2443 * child list.
2444 */
2445 *ret_left_path = left_path;
2446 goto out_ret_path;
2447 }
2448
Joel Becker7dc02802009-02-12 19:20:13 -08002449 start = ocfs2_find_subtree_root(et, left_path, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002450
2451 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2452 start,
2453 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2454 right_path->p_tree_depth);
2455
2456 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002457 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002458 if (ret) {
2459 mlog_errno(ret);
2460 goto out;
2461 }
2462
Joel Becker5c601ab2009-02-12 19:10:13 -08002463 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002464 right_path, start);
2465 if (ret) {
2466 mlog_errno(ret);
2467 goto out;
2468 }
2469
Mark Fasheh328d5752007-06-18 10:48:04 -07002470 if (split != SPLIT_NONE &&
2471 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2472 insert_cpos)) {
2473 /*
2474 * A rotate moves the rightmost left leaf
2475 * record over to the leftmost right leaf
2476 * slot. If we're doing an extent split
2477 * instead of a real insert, then we have to
2478 * check that the extent to be split wasn't
2479 * just moved over. If it was, then we can
2480 * exit here, passing left_path back -
2481 * ocfs2_split_extent() is smart enough to
2482 * search both leaves.
2483 */
2484 *ret_left_path = left_path;
2485 goto out_ret_path;
2486 }
2487
Mark Fashehdcd05382007-01-16 11:32:23 -08002488 /*
2489 * There is no need to re-read the next right path
2490 * as we know that it'll be our current left
2491 * path. Optimize by copying values instead.
2492 */
2493 ocfs2_mv_path(right_path, left_path);
2494
Joel Becker5c601ab2009-02-12 19:10:13 -08002495 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002496 if (ret) {
2497 mlog_errno(ret);
2498 goto out;
2499 }
2500 }
2501
2502out:
2503 ocfs2_free_path(left_path);
2504
2505out_ret_path:
2506 return ret;
2507}
2508
Joel Becker09106ba2009-02-12 19:43:57 -08002509static int ocfs2_update_edge_lengths(handle_t *handle,
2510 struct ocfs2_extent_tree *et,
Tao Ma3c5e1062009-07-21 15:42:05 +08002511 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002512{
Tao Ma3c5e1062009-07-21 15:42:05 +08002513 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002514 struct ocfs2_extent_rec *rec;
2515 struct ocfs2_extent_list *el;
2516 struct ocfs2_extent_block *eb;
2517 u32 range;
2518
Tao Ma3c5e1062009-07-21 15:42:05 +08002519 /*
2520 * In normal tree rotation process, we will never touch the
2521 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2522 * doesn't reserve the credits for them either.
2523 *
2524 * But we do have a special case here which will update the rightmost
2525 * records for all the bh in the path.
2526 * So we have to allocate extra credits and access them.
2527 */
Tao Mac901fb02010-04-26 14:34:57 +08002528 ret = ocfs2_extend_trans(handle, subtree_index);
Tao Ma3c5e1062009-07-21 15:42:05 +08002529 if (ret) {
2530 mlog_errno(ret);
2531 goto out;
2532 }
2533
Joel Becker09106ba2009-02-12 19:43:57 -08002534 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002535 if (ret) {
2536 mlog_errno(ret);
2537 goto out;
2538 }
2539
Mark Fasheh328d5752007-06-18 10:48:04 -07002540 /* Path should always be rightmost. */
2541 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2542 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2543
2544 el = &eb->h_list;
2545 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2546 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2547 rec = &el->l_recs[idx];
2548 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2549
2550 for (i = 0; i < path->p_tree_depth; i++) {
2551 el = path->p_node[i].el;
2552 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2553 rec = &el->l_recs[idx];
2554
2555 rec->e_int_clusters = cpu_to_le32(range);
2556 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2557
2558 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2559 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002560out:
2561 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002562}
2563
Joel Becker6641b0c2009-02-12 18:57:52 -08002564static void ocfs2_unlink_path(handle_t *handle,
2565 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002566 struct ocfs2_cached_dealloc_ctxt *dealloc,
2567 struct ocfs2_path *path, int unlink_start)
2568{
2569 int ret, i;
2570 struct ocfs2_extent_block *eb;
2571 struct ocfs2_extent_list *el;
2572 struct buffer_head *bh;
2573
2574 for(i = unlink_start; i < path_num_items(path); i++) {
2575 bh = path->p_node[i].bh;
2576
2577 eb = (struct ocfs2_extent_block *)bh->b_data;
2578 /*
2579 * Not all nodes might have had their final count
2580 * decremented by the caller - handle this here.
2581 */
2582 el = &eb->h_list;
2583 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2584 mlog(ML_ERROR,
2585 "Inode %llu, attempted to remove extent block "
2586 "%llu with %u records\n",
Joel Becker6641b0c2009-02-12 18:57:52 -08002587 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07002588 (unsigned long long)le64_to_cpu(eb->h_blkno),
2589 le16_to_cpu(el->l_next_free_rec));
2590
2591 ocfs2_journal_dirty(handle, bh);
Joel Becker6641b0c2009-02-12 18:57:52 -08002592 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002593 continue;
2594 }
2595
2596 el->l_next_free_rec = 0;
2597 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2598
2599 ocfs2_journal_dirty(handle, bh);
2600
2601 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2602 if (ret)
2603 mlog_errno(ret);
2604
Joel Becker6641b0c2009-02-12 18:57:52 -08002605 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002606 }
2607}
2608
Joel Becker6641b0c2009-02-12 18:57:52 -08002609static void ocfs2_unlink_subtree(handle_t *handle,
2610 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002611 struct ocfs2_path *left_path,
2612 struct ocfs2_path *right_path,
2613 int subtree_index,
2614 struct ocfs2_cached_dealloc_ctxt *dealloc)
2615{
2616 int i;
2617 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2618 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2619 struct ocfs2_extent_list *el;
2620 struct ocfs2_extent_block *eb;
2621
2622 el = path_leaf_el(left_path);
2623
2624 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2625
2626 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2627 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2628 break;
2629
2630 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2631
2632 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2633 le16_add_cpu(&root_el->l_next_free_rec, -1);
2634
2635 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2636 eb->h_next_leaf_blk = 0;
2637
2638 ocfs2_journal_dirty(handle, root_bh);
2639 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2640
Joel Becker6641b0c2009-02-12 18:57:52 -08002641 ocfs2_unlink_path(handle, et, dealloc, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002642 subtree_index + 1);
2643}
2644
Joel Becker1e2dd632009-02-12 19:45:28 -08002645static int ocfs2_rotate_subtree_left(handle_t *handle,
2646 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002647 struct ocfs2_path *left_path,
2648 struct ocfs2_path *right_path,
2649 int subtree_index,
2650 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker1e2dd632009-02-12 19:45:28 -08002651 int *deleted)
Mark Fasheh328d5752007-06-18 10:48:04 -07002652{
2653 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002654 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002655 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2656 struct ocfs2_extent_block *eb;
2657
2658 *deleted = 0;
2659
2660 right_leaf_el = path_leaf_el(right_path);
2661 left_leaf_el = path_leaf_el(left_path);
2662 root_bh = left_path->p_node[subtree_index].bh;
2663 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2664
2665 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2666 return 0;
2667
2668 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2669 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2670 /*
2671 * It's legal for us to proceed if the right leaf is
2672 * the rightmost one and it has an empty extent. There
2673 * are two cases to handle - whether the leaf will be
2674 * empty after removal or not. If the leaf isn't empty
2675 * then just remove the empty extent up front. The
2676 * next block will handle empty leaves by flagging
2677 * them for unlink.
2678 *
2679 * Non rightmost leaves will throw -EAGAIN and the
2680 * caller can manually move the subtree and retry.
2681 */
2682
2683 if (eb->h_next_leaf_blk != 0ULL)
2684 return -EAGAIN;
2685
2686 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002687 ret = ocfs2_journal_access_eb(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002688 path_leaf_bh(right_path),
2689 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002690 if (ret) {
2691 mlog_errno(ret);
2692 goto out;
2693 }
2694
2695 ocfs2_remove_empty_extent(right_leaf_el);
2696 } else
2697 right_has_empty = 1;
2698 }
2699
2700 if (eb->h_next_leaf_blk == 0ULL &&
2701 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2702 /*
2703 * We have to update i_last_eb_blk during the meta
2704 * data delete.
2705 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08002706 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07002707 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002708 if (ret) {
2709 mlog_errno(ret);
2710 goto out;
2711 }
2712
2713 del_right_subtree = 1;
2714 }
2715
2716 /*
2717 * Getting here with an empty extent in the right path implies
2718 * that it's the rightmost path and will be deleted.
2719 */
2720 BUG_ON(right_has_empty && !del_right_subtree);
2721
Joel Becker1e2dd632009-02-12 19:45:28 -08002722 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002723 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002724 if (ret) {
2725 mlog_errno(ret);
2726 goto out;
2727 }
2728
2729 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002730 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002731 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002732 if (ret) {
2733 mlog_errno(ret);
2734 goto out;
2735 }
2736
Joel Becker1e2dd632009-02-12 19:45:28 -08002737 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002738 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002739 if (ret) {
2740 mlog_errno(ret);
2741 goto out;
2742 }
2743 }
2744
2745 if (!right_has_empty) {
2746 /*
2747 * Only do this if we're moving a real
2748 * record. Otherwise, the action is delayed until
2749 * after removal of the right path in which case we
2750 * can do a simple shift to remove the empty extent.
2751 */
2752 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2753 memset(&right_leaf_el->l_recs[0], 0,
2754 sizeof(struct ocfs2_extent_rec));
2755 }
2756 if (eb->h_next_leaf_blk == 0ULL) {
2757 /*
2758 * Move recs over to get rid of empty extent, decrease
2759 * next_free. This is allowed to remove the last
2760 * extent in our leaf (setting l_next_free_rec to
2761 * zero) - the delete code below won't care.
2762 */
2763 ocfs2_remove_empty_extent(right_leaf_el);
2764 }
2765
Joel Beckerec20cec2010-03-19 14:13:52 -07002766 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2767 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
Mark Fasheh328d5752007-06-18 10:48:04 -07002768
2769 if (del_right_subtree) {
Joel Becker6641b0c2009-02-12 18:57:52 -08002770 ocfs2_unlink_subtree(handle, et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002771 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08002772 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08002773 left_path);
2774 if (ret) {
2775 mlog_errno(ret);
2776 goto out;
2777 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002778
2779 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002780 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002781
2782 /*
2783 * Removal of the extent in the left leaf was skipped
2784 * above so we could delete the right path
2785 * 1st.
2786 */
2787 if (right_has_empty)
2788 ocfs2_remove_empty_extent(left_leaf_el);
2789
Joel Beckerec20cec2010-03-19 14:13:52 -07002790 ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002791
2792 *deleted = 1;
2793 } else
Joel Becker4619c732009-02-12 19:02:36 -08002794 ocfs2_complete_edge_insert(handle, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002795 subtree_index);
2796
2797out:
2798 return ret;
2799}
2800
2801/*
2802 * Given a full path, determine what cpos value would return us a path
2803 * containing the leaf immediately to the right of the current one.
2804 *
2805 * Will return zero if the path passed in is already the rightmost path.
2806 *
2807 * This looks similar, but is subtly different to
2808 * ocfs2_find_cpos_for_left_leaf().
2809 */
Tao Ma38a04e42009-11-30 14:32:19 +08002810int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2811 struct ocfs2_path *path, u32 *cpos)
Mark Fasheh328d5752007-06-18 10:48:04 -07002812{
2813 int i, j, ret = 0;
2814 u64 blkno;
2815 struct ocfs2_extent_list *el;
2816
2817 *cpos = 0;
2818
2819 if (path->p_tree_depth == 0)
2820 return 0;
2821
2822 blkno = path_leaf_bh(path)->b_blocknr;
2823
2824 /* Start at the tree node just above the leaf and work our way up. */
2825 i = path->p_tree_depth - 1;
2826 while (i >= 0) {
2827 int next_free;
2828
2829 el = path->p_node[i].el;
2830
2831 /*
2832 * Find the extent record just after the one in our
2833 * path.
2834 */
2835 next_free = le16_to_cpu(el->l_next_free_rec);
2836 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2837 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2838 if (j == (next_free - 1)) {
2839 if (i == 0) {
2840 /*
2841 * We've determined that the
2842 * path specified is already
2843 * the rightmost one - return a
2844 * cpos of zero.
2845 */
2846 goto out;
2847 }
2848 /*
2849 * The rightmost record points to our
2850 * leaf - we need to travel up the
2851 * tree one level.
2852 */
2853 goto next_node;
2854 }
2855
2856 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2857 goto out;
2858 }
2859 }
2860
2861 /*
2862 * If we got here, we never found a valid node where
2863 * the tree indicated one should be.
2864 */
2865 ocfs2_error(sb,
2866 "Invalid extent tree at extent block %llu\n",
2867 (unsigned long long)blkno);
2868 ret = -EROFS;
2869 goto out;
2870
2871next_node:
2872 blkno = path->p_node[i].bh->b_blocknr;
2873 i--;
2874 }
2875
2876out:
2877 return ret;
2878}
2879
Joel Becker70f18c02009-02-13 02:09:31 -08002880static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2881 struct ocfs2_extent_tree *et,
Joel Becker13723d02008-10-17 19:25:01 -07002882 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002883{
2884 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002885 struct buffer_head *bh = path_leaf_bh(path);
2886 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002887
2888 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2889 return 0;
2890
Joel Becker70f18c02009-02-13 02:09:31 -08002891 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Joel Becker13723d02008-10-17 19:25:01 -07002892 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002893 if (ret) {
2894 mlog_errno(ret);
2895 goto out;
2896 }
2897
2898 ocfs2_remove_empty_extent(el);
Joel Beckerec20cec2010-03-19 14:13:52 -07002899 ocfs2_journal_dirty(handle, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002900
2901out:
2902 return ret;
2903}
2904
Joel Beckere46f74d2009-02-12 19:47:43 -08002905static int __ocfs2_rotate_tree_left(handle_t *handle,
2906 struct ocfs2_extent_tree *et,
2907 int orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07002908 struct ocfs2_path *path,
2909 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08002910 struct ocfs2_path **empty_extent_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002911{
2912 int ret, subtree_root, deleted;
2913 u32 right_cpos;
2914 struct ocfs2_path *left_path = NULL;
2915 struct ocfs2_path *right_path = NULL;
Joel Beckere46f74d2009-02-12 19:47:43 -08002916 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fasheh328d5752007-06-18 10:48:04 -07002917
2918 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
2919
2920 *empty_extent_path = NULL;
2921
Joel Beckere46f74d2009-02-12 19:47:43 -08002922 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002923 if (ret) {
2924 mlog_errno(ret);
2925 goto out;
2926 }
2927
Joel Beckerffdd7a52008-10-17 22:32:01 -07002928 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002929 if (!left_path) {
2930 ret = -ENOMEM;
2931 mlog_errno(ret);
2932 goto out;
2933 }
2934
2935 ocfs2_cp_path(left_path, path);
2936
Joel Beckerffdd7a52008-10-17 22:32:01 -07002937 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002938 if (!right_path) {
2939 ret = -ENOMEM;
2940 mlog_errno(ret);
2941 goto out;
2942 }
2943
2944 while (right_cpos) {
Joel Beckerfacdb772009-02-12 18:08:48 -08002945 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002946 if (ret) {
2947 mlog_errno(ret);
2948 goto out;
2949 }
2950
Joel Becker7dc02802009-02-12 19:20:13 -08002951 subtree_root = ocfs2_find_subtree_root(et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002952 right_path);
2953
2954 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2955 subtree_root,
2956 (unsigned long long)
2957 right_path->p_node[subtree_root].bh->b_blocknr,
2958 right_path->p_tree_depth);
2959
2960 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
2961 orig_credits, left_path);
2962 if (ret) {
2963 mlog_errno(ret);
2964 goto out;
2965 }
2966
Mark Fashehe8aed342007-12-03 16:43:01 -08002967 /*
2968 * Caller might still want to make changes to the
2969 * tree root, so re-add it to the journal here.
2970 */
Joel Beckere46f74d2009-02-12 19:47:43 -08002971 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002972 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08002973 if (ret) {
2974 mlog_errno(ret);
2975 goto out;
2976 }
2977
Joel Becker1e2dd632009-02-12 19:45:28 -08002978 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002979 right_path, subtree_root,
Joel Becker1e2dd632009-02-12 19:45:28 -08002980 dealloc, &deleted);
Mark Fasheh328d5752007-06-18 10:48:04 -07002981 if (ret == -EAGAIN) {
2982 /*
2983 * The rotation has to temporarily stop due to
2984 * the right subtree having an empty
2985 * extent. Pass it back to the caller for a
2986 * fixup.
2987 */
2988 *empty_extent_path = right_path;
2989 right_path = NULL;
2990 goto out;
2991 }
2992 if (ret) {
2993 mlog_errno(ret);
2994 goto out;
2995 }
2996
2997 /*
2998 * The subtree rotate might have removed records on
2999 * the rightmost edge. If so, then rotation is
3000 * complete.
3001 */
3002 if (deleted)
3003 break;
3004
3005 ocfs2_mv_path(left_path, right_path);
3006
Joel Beckere46f74d2009-02-12 19:47:43 -08003007 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003008 &right_cpos);
3009 if (ret) {
3010 mlog_errno(ret);
3011 goto out;
3012 }
3013 }
3014
3015out:
3016 ocfs2_free_path(right_path);
3017 ocfs2_free_path(left_path);
3018
3019 return ret;
3020}
3021
Joel Becker70f18c02009-02-13 02:09:31 -08003022static int ocfs2_remove_rightmost_path(handle_t *handle,
3023 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003024 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003025 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003026{
3027 int ret, subtree_index;
3028 u32 cpos;
3029 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003030 struct ocfs2_extent_block *eb;
3031 struct ocfs2_extent_list *el;
3032
Mark Fasheh328d5752007-06-18 10:48:04 -07003033
Joel Becker6136ca52009-02-12 19:32:43 -08003034 ret = ocfs2_et_sanity_check(et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003035 if (ret)
3036 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003037 /*
3038 * There's two ways we handle this depending on
3039 * whether path is the only existing one.
3040 */
3041 ret = ocfs2_extend_rotate_transaction(handle, 0,
3042 handle->h_buffer_credits,
3043 path);
3044 if (ret) {
3045 mlog_errno(ret);
3046 goto out;
3047 }
3048
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003049 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003050 if (ret) {
3051 mlog_errno(ret);
3052 goto out;
3053 }
3054
Joel Becker3d03a302009-02-12 17:49:26 -08003055 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3056 path, &cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003057 if (ret) {
3058 mlog_errno(ret);
3059 goto out;
3060 }
3061
3062 if (cpos) {
3063 /*
3064 * We have a path to the left of this one - it needs
3065 * an update too.
3066 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003067 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003068 if (!left_path) {
3069 ret = -ENOMEM;
3070 mlog_errno(ret);
3071 goto out;
3072 }
3073
Joel Beckerfacdb772009-02-12 18:08:48 -08003074 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003075 if (ret) {
3076 mlog_errno(ret);
3077 goto out;
3078 }
3079
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003080 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003081 if (ret) {
3082 mlog_errno(ret);
3083 goto out;
3084 }
3085
Joel Becker7dc02802009-02-12 19:20:13 -08003086 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003087
Joel Becker6641b0c2009-02-12 18:57:52 -08003088 ocfs2_unlink_subtree(handle, et, left_path, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003089 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08003090 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08003091 left_path);
3092 if (ret) {
3093 mlog_errno(ret);
3094 goto out;
3095 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003096
3097 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003098 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003099 } else {
3100 /*
3101 * 'path' is also the leftmost path which
3102 * means it must be the only one. This gets
3103 * handled differently because we want to
Joel Becker70f18c02009-02-13 02:09:31 -08003104 * revert the root back to having extents
Mark Fasheh328d5752007-06-18 10:48:04 -07003105 * in-line.
3106 */
Joel Becker6641b0c2009-02-12 18:57:52 -08003107 ocfs2_unlink_path(handle, et, dealloc, path, 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003108
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003109 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003110 el->l_tree_depth = 0;
3111 el->l_next_free_rec = 0;
3112 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3113
Joel Becker35dc0aa2008-08-20 16:25:06 -07003114 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003115 }
3116
3117 ocfs2_journal_dirty(handle, path_root_bh(path));
3118
3119out:
3120 ocfs2_free_path(left_path);
3121 return ret;
3122}
3123
3124/*
3125 * Left rotation of btree records.
3126 *
3127 * In many ways, this is (unsurprisingly) the opposite of right
3128 * rotation. We start at some non-rightmost path containing an empty
3129 * extent in the leaf block. The code works its way to the rightmost
3130 * path by rotating records to the left in every subtree.
3131 *
3132 * This is used by any code which reduces the number of extent records
3133 * in a leaf. After removal, an empty record should be placed in the
3134 * leftmost list position.
3135 *
3136 * This won't handle a length update of the rightmost path records if
3137 * the rightmost tree leaf record is removed so the caller is
3138 * responsible for detecting and correcting that.
3139 */
Joel Becker70f18c02009-02-13 02:09:31 -08003140static int ocfs2_rotate_tree_left(handle_t *handle,
3141 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003142 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003143 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003144{
3145 int ret, orig_credits = handle->h_buffer_credits;
3146 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3147 struct ocfs2_extent_block *eb;
3148 struct ocfs2_extent_list *el;
3149
3150 el = path_leaf_el(path);
3151 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3152 return 0;
3153
3154 if (path->p_tree_depth == 0) {
3155rightmost_no_delete:
3156 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003157 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003158 * it up front.
3159 */
Joel Becker70f18c02009-02-13 02:09:31 -08003160 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003161 if (ret)
3162 mlog_errno(ret);
3163 goto out;
3164 }
3165
3166 /*
3167 * Handle rightmost branch now. There's several cases:
3168 * 1) simple rotation leaving records in there. That's trivial.
3169 * 2) rotation requiring a branch delete - there's no more
3170 * records left. Two cases of this:
3171 * a) There are branches to the left.
3172 * b) This is also the leftmost (the only) branch.
3173 *
3174 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3175 * 2a) we need the left branch so that we can update it with the unlink
Joel Becker70f18c02009-02-13 02:09:31 -08003176 * 2b) we need to bring the root back to inline extents.
Mark Fasheh328d5752007-06-18 10:48:04 -07003177 */
3178
3179 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3180 el = &eb->h_list;
3181 if (eb->h_next_leaf_blk == 0) {
3182 /*
3183 * This gets a bit tricky if we're going to delete the
3184 * rightmost path. Get the other cases out of the way
3185 * 1st.
3186 */
3187 if (le16_to_cpu(el->l_next_free_rec) > 1)
3188 goto rightmost_no_delete;
3189
3190 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3191 ret = -EIO;
Joel Becker70f18c02009-02-13 02:09:31 -08003192 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3193 "Owner %llu has empty extent block at %llu",
3194 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07003195 (unsigned long long)le64_to_cpu(eb->h_blkno));
3196 goto out;
3197 }
3198
3199 /*
3200 * XXX: The caller can not trust "path" any more after
3201 * this as it will have been deleted. What do we do?
3202 *
3203 * In theory the rotate-for-merge code will never get
3204 * here because it'll always ask for a rotate in a
3205 * nonempty list.
3206 */
3207
Joel Becker70f18c02009-02-13 02:09:31 -08003208 ret = ocfs2_remove_rightmost_path(handle, et, path,
3209 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003210 if (ret)
3211 mlog_errno(ret);
3212 goto out;
3213 }
3214
3215 /*
3216 * Now we can loop, remembering the path we get from -EAGAIN
3217 * and restarting from there.
3218 */
3219try_rotate:
Joel Beckere46f74d2009-02-12 19:47:43 -08003220 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3221 dealloc, &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003222 if (ret && ret != -EAGAIN) {
3223 mlog_errno(ret);
3224 goto out;
3225 }
3226
3227 while (ret == -EAGAIN) {
3228 tmp_path = restart_path;
3229 restart_path = NULL;
3230
Joel Beckere46f74d2009-02-12 19:47:43 -08003231 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07003232 tmp_path, dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08003233 &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003234 if (ret && ret != -EAGAIN) {
3235 mlog_errno(ret);
3236 goto out;
3237 }
3238
3239 ocfs2_free_path(tmp_path);
3240 tmp_path = NULL;
3241
3242 if (ret == 0)
3243 goto try_rotate;
3244 }
3245
3246out:
3247 ocfs2_free_path(tmp_path);
3248 ocfs2_free_path(restart_path);
3249 return ret;
3250}
3251
3252static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3253 int index)
3254{
3255 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3256 unsigned int size;
3257
3258 if (rec->e_leaf_clusters == 0) {
3259 /*
3260 * We consumed all of the merged-from record. An empty
3261 * extent cannot exist anywhere but the 1st array
3262 * position, so move things over if the merged-from
3263 * record doesn't occupy that position.
3264 *
3265 * This creates a new empty extent so the caller
3266 * should be smart enough to have removed any existing
3267 * ones.
3268 */
3269 if (index > 0) {
3270 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3271 size = index * sizeof(struct ocfs2_extent_rec);
3272 memmove(&el->l_recs[1], &el->l_recs[0], size);
3273 }
3274
3275 /*
3276 * Always memset - the caller doesn't check whether it
3277 * created an empty extent, so there could be junk in
3278 * the other fields.
3279 */
3280 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3281 }
3282}
3283
Joel Becker4fe82c32009-02-13 02:16:08 -08003284static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003285 struct ocfs2_path *left_path,
3286 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003287{
3288 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003289 u32 right_cpos;
3290 struct ocfs2_path *right_path = NULL;
3291 struct ocfs2_extent_list *left_el;
3292
3293 *ret_right_path = NULL;
3294
3295 /* This function shouldn't be called for non-trees. */
3296 BUG_ON(left_path->p_tree_depth == 0);
3297
3298 left_el = path_leaf_el(left_path);
3299 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3300
Joel Becker4fe82c32009-02-13 02:16:08 -08003301 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3302 left_path, &right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003303 if (ret) {
3304 mlog_errno(ret);
3305 goto out;
3306 }
3307
3308 /* This function shouldn't be called for the rightmost leaf. */
3309 BUG_ON(right_cpos == 0);
3310
Joel Beckerffdd7a52008-10-17 22:32:01 -07003311 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003312 if (!right_path) {
3313 ret = -ENOMEM;
3314 mlog_errno(ret);
3315 goto out;
3316 }
3317
Joel Becker4fe82c32009-02-13 02:16:08 -08003318 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003319 if (ret) {
3320 mlog_errno(ret);
3321 goto out;
3322 }
3323
3324 *ret_right_path = right_path;
3325out:
3326 if (ret)
3327 ocfs2_free_path(right_path);
3328 return ret;
3329}
3330
3331/*
3332 * Remove split_rec clusters from the record at index and merge them
3333 * onto the beginning of the record "next" to it.
3334 * For index < l_count - 1, the next means the extent rec at index + 1.
3335 * For index == l_count - 1, the "next" means the 1st extent rec of the
3336 * next extent block.
3337 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003338static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003339 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003340 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003341 struct ocfs2_extent_rec *split_rec,
3342 int index)
3343{
3344 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003345 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3346 struct ocfs2_extent_rec *left_rec;
3347 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003348 struct ocfs2_extent_list *right_el;
3349 struct ocfs2_path *right_path = NULL;
3350 int subtree_index = 0;
3351 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3352 struct buffer_head *bh = path_leaf_bh(left_path);
3353 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003354
3355 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003356 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003357
Al Viro9d8df6a2008-05-21 06:32:11 +01003358 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003359 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3360 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003361 ret = ocfs2_get_right_path(et, left_path, &right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003362 if (ret) {
3363 mlog_errno(ret);
3364 goto out;
3365 }
3366
3367 right_el = path_leaf_el(right_path);
3368 next_free = le16_to_cpu(right_el->l_next_free_rec);
3369 BUG_ON(next_free <= 0);
3370 right_rec = &right_el->l_recs[0];
3371 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003372 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003373 right_rec = &right_el->l_recs[1];
3374 }
3375
3376 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3377 le16_to_cpu(left_rec->e_leaf_clusters) !=
3378 le32_to_cpu(right_rec->e_cpos));
3379
Joel Becker7dc02802009-02-12 19:20:13 -08003380 subtree_index = ocfs2_find_subtree_root(et, left_path,
3381 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003382
3383 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3384 handle->h_buffer_credits,
3385 right_path);
3386 if (ret) {
3387 mlog_errno(ret);
3388 goto out;
3389 }
3390
3391 root_bh = left_path->p_node[subtree_index].bh;
3392 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3393
Joel Becker7dc02802009-02-12 19:20:13 -08003394 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003395 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003396 if (ret) {
3397 mlog_errno(ret);
3398 goto out;
3399 }
3400
3401 for (i = subtree_index + 1;
3402 i < path_num_items(right_path); i++) {
Joel Becker7dc02802009-02-12 19:20:13 -08003403 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003404 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003405 if (ret) {
3406 mlog_errno(ret);
3407 goto out;
3408 }
3409
Joel Becker7dc02802009-02-12 19:20:13 -08003410 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003411 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003412 if (ret) {
3413 mlog_errno(ret);
3414 goto out;
3415 }
3416 }
3417
3418 } else {
3419 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3420 right_rec = &el->l_recs[index + 1];
3421 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003422
Joel Becker7dc02802009-02-12 19:20:13 -08003423 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
Joel Becker13723d02008-10-17 19:25:01 -07003424 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003425 if (ret) {
3426 mlog_errno(ret);
3427 goto out;
3428 }
3429
3430 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3431
3432 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3433 le64_add_cpu(&right_rec->e_blkno,
Joel Becker7dc02802009-02-12 19:20:13 -08003434 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3435 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003436 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3437
3438 ocfs2_cleanup_merge(el, index);
3439
Joel Beckerec20cec2010-03-19 14:13:52 -07003440 ocfs2_journal_dirty(handle, bh);
Tao Ma677b9752008-01-30 14:21:05 +08003441 if (right_path) {
Joel Beckerec20cec2010-03-19 14:13:52 -07003442 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
Joel Becker4619c732009-02-12 19:02:36 -08003443 ocfs2_complete_edge_insert(handle, left_path, right_path,
3444 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003445 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003446out:
Tao Ma677b9752008-01-30 14:21:05 +08003447 if (right_path)
3448 ocfs2_free_path(right_path);
3449 return ret;
3450}
3451
Joel Becker4fe82c32009-02-13 02:16:08 -08003452static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003453 struct ocfs2_path *right_path,
3454 struct ocfs2_path **ret_left_path)
3455{
3456 int ret;
3457 u32 left_cpos;
3458 struct ocfs2_path *left_path = NULL;
3459
3460 *ret_left_path = NULL;
3461
3462 /* This function shouldn't be called for non-trees. */
3463 BUG_ON(right_path->p_tree_depth == 0);
3464
Joel Becker4fe82c32009-02-13 02:16:08 -08003465 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
Tao Ma677b9752008-01-30 14:21:05 +08003466 right_path, &left_cpos);
3467 if (ret) {
3468 mlog_errno(ret);
3469 goto out;
3470 }
3471
3472 /* This function shouldn't be called for the leftmost leaf. */
3473 BUG_ON(left_cpos == 0);
3474
Joel Beckerffdd7a52008-10-17 22:32:01 -07003475 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003476 if (!left_path) {
3477 ret = -ENOMEM;
3478 mlog_errno(ret);
3479 goto out;
3480 }
3481
Joel Becker4fe82c32009-02-13 02:16:08 -08003482 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003483 if (ret) {
3484 mlog_errno(ret);
3485 goto out;
3486 }
3487
3488 *ret_left_path = left_path;
3489out:
3490 if (ret)
3491 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003492 return ret;
3493}
3494
3495/*
3496 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003497 * onto the tail of the record "before" it.
3498 * For index > 0, the "before" means the extent rec at index - 1.
3499 *
3500 * For index == 0, the "before" means the last record of the previous
3501 * extent block. And there is also a situation that we may need to
3502 * remove the rightmost leaf extent block in the right_path and change
3503 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003504 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003505static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003506 handle_t *handle,
Joel Becker4fe82c32009-02-13 02:16:08 -08003507 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003508 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003509 struct ocfs2_cached_dealloc_ctxt *dealloc,
3510 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003511{
Tao Ma677b9752008-01-30 14:21:05 +08003512 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003513 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3514 struct ocfs2_extent_rec *left_rec;
3515 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003516 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3517 struct buffer_head *bh = path_leaf_bh(right_path);
3518 struct buffer_head *root_bh = NULL;
3519 struct ocfs2_path *left_path = NULL;
3520 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003521
Tao Ma677b9752008-01-30 14:21:05 +08003522 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003523
Mark Fasheh328d5752007-06-18 10:48:04 -07003524 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003525 if (index == 0) {
3526 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003527 ret = ocfs2_get_left_path(et, right_path, &left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003528 if (ret) {
3529 mlog_errno(ret);
3530 goto out;
3531 }
3532
3533 left_el = path_leaf_el(left_path);
3534 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3535 le16_to_cpu(left_el->l_count));
3536
3537 left_rec = &left_el->l_recs[
3538 le16_to_cpu(left_el->l_next_free_rec) - 1];
3539 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3540 le16_to_cpu(left_rec->e_leaf_clusters) !=
3541 le32_to_cpu(split_rec->e_cpos));
3542
Joel Becker7dc02802009-02-12 19:20:13 -08003543 subtree_index = ocfs2_find_subtree_root(et, left_path,
3544 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003545
3546 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3547 handle->h_buffer_credits,
3548 left_path);
3549 if (ret) {
3550 mlog_errno(ret);
3551 goto out;
3552 }
3553
3554 root_bh = left_path->p_node[subtree_index].bh;
3555 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3556
Joel Becker4fe82c32009-02-13 02:16:08 -08003557 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003558 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003559 if (ret) {
3560 mlog_errno(ret);
3561 goto out;
3562 }
3563
3564 for (i = subtree_index + 1;
3565 i < path_num_items(right_path); i++) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003566 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003567 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003568 if (ret) {
3569 mlog_errno(ret);
3570 goto out;
3571 }
3572
Joel Becker4fe82c32009-02-13 02:16:08 -08003573 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003574 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003575 if (ret) {
3576 mlog_errno(ret);
3577 goto out;
3578 }
3579 }
3580 } else {
3581 left_rec = &el->l_recs[index - 1];
3582 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3583 has_empty_extent = 1;
3584 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003585
Joel Becker4fe82c32009-02-13 02:16:08 -08003586 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Tao Ma9047bea2009-01-05 14:45:24 +08003587 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003588 if (ret) {
3589 mlog_errno(ret);
3590 goto out;
3591 }
3592
3593 if (has_empty_extent && index == 1) {
3594 /*
3595 * The easy case - we can just plop the record right in.
3596 */
3597 *left_rec = *split_rec;
3598
3599 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003600 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003601 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003602
3603 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3604 le64_add_cpu(&right_rec->e_blkno,
Joel Becker4fe82c32009-02-13 02:16:08 -08003605 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3606 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003607 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3608
3609 ocfs2_cleanup_merge(el, index);
3610
Joel Beckerec20cec2010-03-19 14:13:52 -07003611 ocfs2_journal_dirty(handle, bh);
Tao Ma677b9752008-01-30 14:21:05 +08003612 if (left_path) {
Joel Beckerec20cec2010-03-19 14:13:52 -07003613 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
Tao Ma677b9752008-01-30 14:21:05 +08003614
3615 /*
3616 * In the situation that the right_rec is empty and the extent
3617 * block is empty also, ocfs2_complete_edge_insert can't handle
3618 * it and we need to delete the right extent block.
3619 */
3620 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3621 le16_to_cpu(el->l_next_free_rec) == 1) {
3622
Joel Becker70f18c02009-02-13 02:09:31 -08003623 ret = ocfs2_remove_rightmost_path(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003624 right_path,
Joel Becker70f18c02009-02-13 02:09:31 -08003625 dealloc);
Tao Ma677b9752008-01-30 14:21:05 +08003626 if (ret) {
3627 mlog_errno(ret);
3628 goto out;
3629 }
3630
3631 /* Now the rightmost extent block has been deleted.
3632 * So we use the new rightmost path.
3633 */
3634 ocfs2_mv_path(right_path, left_path);
3635 left_path = NULL;
3636 } else
Joel Becker4619c732009-02-12 19:02:36 -08003637 ocfs2_complete_edge_insert(handle, left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003638 right_path, subtree_index);
3639 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003640out:
Tao Ma677b9752008-01-30 14:21:05 +08003641 if (left_path)
3642 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003643 return ret;
3644}
3645
Joel Beckerc495dd22009-02-13 02:19:11 -08003646static int ocfs2_try_to_merge_extent(handle_t *handle,
3647 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003648 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003649 int split_index,
3650 struct ocfs2_extent_rec *split_rec,
3651 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckerc495dd22009-02-13 02:19:11 -08003652 struct ocfs2_merge_ctxt *ctxt)
Mark Fasheh328d5752007-06-18 10:48:04 -07003653{
Tao Mao518d7262007-08-28 17:25:35 -07003654 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003655 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003656 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3657
3658 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3659
Tao Mao518d7262007-08-28 17:25:35 -07003660 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3661 /*
3662 * The merge code will need to create an empty
3663 * extent to take the place of the newly
3664 * emptied slot. Remove any pre-existing empty
3665 * extents - having more than one in a leaf is
3666 * illegal.
3667 */
Joel Becker70f18c02009-02-13 02:09:31 -08003668 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Tao Mao518d7262007-08-28 17:25:35 -07003669 if (ret) {
3670 mlog_errno(ret);
3671 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003672 }
Tao Mao518d7262007-08-28 17:25:35 -07003673 split_index--;
3674 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003675 }
3676
3677 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3678 /*
3679 * Left-right contig implies this.
3680 */
3681 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003682
3683 /*
3684 * Since the leftright insert always covers the entire
3685 * extent, this call will delete the insert record
3686 * entirely, resulting in an empty extent record added to
3687 * the extent block.
3688 *
3689 * Since the adding of an empty extent shifts
3690 * everything back to the right, there's no need to
3691 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003692 *
3693 * When the split_index is zero, we need to merge it to the
3694 * prevoius extent block. It is more efficient and easier
3695 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003696 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003697 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003698 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003699 if (ret) {
3700 mlog_errno(ret);
3701 goto out;
3702 }
3703
3704 /*
3705 * We can only get this from logic error above.
3706 */
3707 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3708
Tao Ma677b9752008-01-30 14:21:05 +08003709 /* The merge left us with an empty extent, remove it. */
Joel Becker70f18c02009-02-13 02:09:31 -08003710 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003711 if (ret) {
3712 mlog_errno(ret);
3713 goto out;
3714 }
Tao Ma677b9752008-01-30 14:21:05 +08003715
Mark Fasheh328d5752007-06-18 10:48:04 -07003716 rec = &el->l_recs[split_index];
3717
3718 /*
3719 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003720 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003721 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003722 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3723 dealloc, split_index);
Tao Ma677b9752008-01-30 14:21:05 +08003724
Mark Fasheh328d5752007-06-18 10:48:04 -07003725 if (ret) {
3726 mlog_errno(ret);
3727 goto out;
3728 }
3729
Joel Becker70f18c02009-02-13 02:09:31 -08003730 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003731 /*
3732 * Error from this last rotate is not critical, so
3733 * print but don't bubble it up.
3734 */
3735 if (ret)
3736 mlog_errno(ret);
3737 ret = 0;
3738 } else {
3739 /*
3740 * Merge a record to the left or right.
3741 *
3742 * 'contig_type' is relative to the existing record,
3743 * so for example, if we're "right contig", it's to
3744 * the record on the left (hence the left merge).
3745 */
3746 if (ctxt->c_contig_type == CONTIG_RIGHT) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003747 ret = ocfs2_merge_rec_left(path, handle, et,
3748 split_rec, dealloc,
Mark Fasheh328d5752007-06-18 10:48:04 -07003749 split_index);
3750 if (ret) {
3751 mlog_errno(ret);
3752 goto out;
3753 }
3754 } else {
Joel Becker4fe82c32009-02-13 02:16:08 -08003755 ret = ocfs2_merge_rec_right(path, handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003756 et, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003757 split_index);
3758 if (ret) {
3759 mlog_errno(ret);
3760 goto out;
3761 }
3762 }
3763
3764 if (ctxt->c_split_covers_rec) {
3765 /*
3766 * The merge may have left an empty extent in
3767 * our leaf. Try to rotate it away.
3768 */
Joel Becker70f18c02009-02-13 02:09:31 -08003769 ret = ocfs2_rotate_tree_left(handle, et, path,
3770 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003771 if (ret)
3772 mlog_errno(ret);
3773 ret = 0;
3774 }
3775 }
3776
3777out:
3778 return ret;
3779}
3780
3781static void ocfs2_subtract_from_rec(struct super_block *sb,
3782 enum ocfs2_split_type split,
3783 struct ocfs2_extent_rec *rec,
3784 struct ocfs2_extent_rec *split_rec)
3785{
3786 u64 len_blocks;
3787
3788 len_blocks = ocfs2_clusters_to_blocks(sb,
3789 le16_to_cpu(split_rec->e_leaf_clusters));
3790
3791 if (split == SPLIT_LEFT) {
3792 /*
3793 * Region is on the left edge of the existing
3794 * record.
3795 */
3796 le32_add_cpu(&rec->e_cpos,
3797 le16_to_cpu(split_rec->e_leaf_clusters));
3798 le64_add_cpu(&rec->e_blkno, len_blocks);
3799 le16_add_cpu(&rec->e_leaf_clusters,
3800 -le16_to_cpu(split_rec->e_leaf_clusters));
3801 } else {
3802 /*
3803 * Region is on the right edge of the existing
3804 * record.
3805 */
3806 le16_add_cpu(&rec->e_leaf_clusters,
3807 -le16_to_cpu(split_rec->e_leaf_clusters));
3808 }
3809}
3810
Mark Fashehdcd05382007-01-16 11:32:23 -08003811/*
3812 * Do the final bits of extent record insertion at the target leaf
3813 * list. If this leaf is part of an allocation tree, it is assumed
3814 * that the tree above has been prepared.
3815 */
Joel Beckerd5628622009-02-13 02:54:36 -08003816static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3817 struct ocfs2_extent_rec *insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08003818 struct ocfs2_extent_list *el,
Joel Beckerd5628622009-02-13 02:54:36 -08003819 struct ocfs2_insert_type *insert)
Mark Fashehdcd05382007-01-16 11:32:23 -08003820{
3821 int i = insert->ins_contig_index;
3822 unsigned int range;
3823 struct ocfs2_extent_rec *rec;
3824
Mark Fashehe48edee2007-03-07 16:46:57 -08003825 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003826
Mark Fasheh328d5752007-06-18 10:48:04 -07003827 if (insert->ins_split != SPLIT_NONE) {
3828 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3829 BUG_ON(i == -1);
3830 rec = &el->l_recs[i];
Joel Beckerd5628622009-02-13 02:54:36 -08003831 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3832 insert->ins_split, rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003833 insert_rec);
3834 goto rotate;
3835 }
3836
Mark Fashehdcd05382007-01-16 11:32:23 -08003837 /*
3838 * Contiguous insert - either left or right.
3839 */
3840 if (insert->ins_contig != CONTIG_NONE) {
3841 rec = &el->l_recs[i];
3842 if (insert->ins_contig == CONTIG_LEFT) {
3843 rec->e_blkno = insert_rec->e_blkno;
3844 rec->e_cpos = insert_rec->e_cpos;
3845 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003846 le16_add_cpu(&rec->e_leaf_clusters,
3847 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003848 return;
3849 }
3850
3851 /*
3852 * Handle insert into an empty leaf.
3853 */
3854 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3855 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3856 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3857 el->l_recs[0] = *insert_rec;
3858 el->l_next_free_rec = cpu_to_le16(1);
3859 return;
3860 }
3861
3862 /*
3863 * Appending insert.
3864 */
3865 if (insert->ins_appending == APPEND_TAIL) {
3866 i = le16_to_cpu(el->l_next_free_rec) - 1;
3867 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003868 range = le32_to_cpu(rec->e_cpos)
3869 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003870 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3871
3872 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3873 le16_to_cpu(el->l_count),
Joel Beckerd5628622009-02-13 02:54:36 -08003874 "owner %llu, depth %u, count %u, next free %u, "
Mark Fashehdcd05382007-01-16 11:32:23 -08003875 "rec.cpos %u, rec.clusters %u, "
3876 "insert.cpos %u, insert.clusters %u\n",
Joel Beckerd5628622009-02-13 02:54:36 -08003877 ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08003878 le16_to_cpu(el->l_tree_depth),
3879 le16_to_cpu(el->l_count),
3880 le16_to_cpu(el->l_next_free_rec),
3881 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003882 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003883 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003884 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003885 i++;
3886 el->l_recs[i] = *insert_rec;
3887 le16_add_cpu(&el->l_next_free_rec, 1);
3888 return;
3889 }
3890
Mark Fasheh328d5752007-06-18 10:48:04 -07003891rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003892 /*
3893 * Ok, we have to rotate.
3894 *
3895 * At this point, it is safe to assume that inserting into an
3896 * empty leaf and appending to a leaf have both been handled
3897 * above.
3898 *
3899 * This leaf needs to have space, either by the empty 1st
3900 * extent record, or by virtue of an l_next_rec < l_count.
3901 */
3902 ocfs2_rotate_leaf(el, insert_rec);
3903}
3904
Joel Beckerd401dc12009-02-13 02:24:10 -08003905static void ocfs2_adjust_rightmost_records(handle_t *handle,
3906 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003907 struct ocfs2_path *path,
3908 struct ocfs2_extent_rec *insert_rec)
3909{
3910 int ret, i, next_free;
3911 struct buffer_head *bh;
3912 struct ocfs2_extent_list *el;
3913 struct ocfs2_extent_rec *rec;
3914
3915 /*
3916 * Update everything except the leaf block.
3917 */
3918 for (i = 0; i < path->p_tree_depth; i++) {
3919 bh = path->p_node[i].bh;
3920 el = path->p_node[i].el;
3921
3922 next_free = le16_to_cpu(el->l_next_free_rec);
3923 if (next_free == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08003924 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3925 "Owner %llu has a bad extent list",
3926 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fasheh328d5752007-06-18 10:48:04 -07003927 ret = -EIO;
3928 return;
3929 }
3930
3931 rec = &el->l_recs[next_free - 1];
3932
3933 rec->e_int_clusters = insert_rec->e_cpos;
3934 le32_add_cpu(&rec->e_int_clusters,
3935 le16_to_cpu(insert_rec->e_leaf_clusters));
3936 le32_add_cpu(&rec->e_int_clusters,
3937 -le32_to_cpu(rec->e_cpos));
3938
Joel Beckerec20cec2010-03-19 14:13:52 -07003939 ocfs2_journal_dirty(handle, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07003940 }
3941}
3942
Joel Beckerd401dc12009-02-13 02:24:10 -08003943static int ocfs2_append_rec_to_path(handle_t *handle,
3944 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08003945 struct ocfs2_extent_rec *insert_rec,
3946 struct ocfs2_path *right_path,
3947 struct ocfs2_path **ret_left_path)
3948{
Mark Fasheh328d5752007-06-18 10:48:04 -07003949 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003950 struct ocfs2_extent_list *el;
3951 struct ocfs2_path *left_path = NULL;
3952
3953 *ret_left_path = NULL;
3954
3955 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003956 * This shouldn't happen for non-trees. The extent rec cluster
3957 * count manipulation below only works for interior nodes.
3958 */
3959 BUG_ON(right_path->p_tree_depth == 0);
3960
3961 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003962 * If our appending insert is at the leftmost edge of a leaf,
3963 * then we might need to update the rightmost records of the
3964 * neighboring path.
3965 */
3966 el = path_leaf_el(right_path);
3967 next_free = le16_to_cpu(el->l_next_free_rec);
3968 if (next_free == 0 ||
3969 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
3970 u32 left_cpos;
3971
Joel Beckerd401dc12009-02-13 02:24:10 -08003972 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3973 right_path, &left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08003974 if (ret) {
3975 mlog_errno(ret);
3976 goto out;
3977 }
3978
3979 mlog(0, "Append may need a left path update. cpos: %u, "
3980 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
3981 left_cpos);
3982
3983 /*
3984 * No need to worry if the append is already in the
3985 * leftmost leaf.
3986 */
3987 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07003988 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08003989 if (!left_path) {
3990 ret = -ENOMEM;
3991 mlog_errno(ret);
3992 goto out;
3993 }
3994
Joel Beckerd401dc12009-02-13 02:24:10 -08003995 ret = ocfs2_find_path(et->et_ci, left_path,
Joel Beckerfacdb772009-02-12 18:08:48 -08003996 left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08003997 if (ret) {
3998 mlog_errno(ret);
3999 goto out;
4000 }
4001
4002 /*
4003 * ocfs2_insert_path() will pass the left_path to the
4004 * journal for us.
4005 */
4006 }
4007 }
4008
Joel Beckerd401dc12009-02-13 02:24:10 -08004009 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004010 if (ret) {
4011 mlog_errno(ret);
4012 goto out;
4013 }
4014
Joel Beckerd401dc12009-02-13 02:24:10 -08004015 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004016
4017 *ret_left_path = left_path;
4018 ret = 0;
4019out:
4020 if (ret != 0)
4021 ocfs2_free_path(left_path);
4022
4023 return ret;
4024}
4025
Joel Beckerc38e52b2009-02-13 02:56:23 -08004026static void ocfs2_split_record(struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004027 struct ocfs2_path *left_path,
4028 struct ocfs2_path *right_path,
4029 struct ocfs2_extent_rec *split_rec,
4030 enum ocfs2_split_type split)
4031{
4032 int index;
4033 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4034 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4035 struct ocfs2_extent_rec *rec, *tmprec;
4036
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004037 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004038 if (left_path)
4039 left_el = path_leaf_el(left_path);
4040
4041 el = right_el;
4042 insert_el = right_el;
4043 index = ocfs2_search_extent_list(el, cpos);
4044 if (index != -1) {
4045 if (index == 0 && left_path) {
4046 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4047
4048 /*
4049 * This typically means that the record
4050 * started in the left path but moved to the
4051 * right as a result of rotation. We either
4052 * move the existing record to the left, or we
4053 * do the later insert there.
4054 *
4055 * In this case, the left path should always
4056 * exist as the rotate code will have passed
4057 * it back for a post-insert update.
4058 */
4059
4060 if (split == SPLIT_LEFT) {
4061 /*
4062 * It's a left split. Since we know
4063 * that the rotate code gave us an
4064 * empty extent in the left path, we
4065 * can just do the insert there.
4066 */
4067 insert_el = left_el;
4068 } else {
4069 /*
4070 * Right split - we have to move the
4071 * existing record over to the left
4072 * leaf. The insert will be into the
4073 * newly created empty extent in the
4074 * right leaf.
4075 */
4076 tmprec = &right_el->l_recs[index];
4077 ocfs2_rotate_leaf(left_el, tmprec);
4078 el = left_el;
4079
4080 memset(tmprec, 0, sizeof(*tmprec));
4081 index = ocfs2_search_extent_list(left_el, cpos);
4082 BUG_ON(index == -1);
4083 }
4084 }
4085 } else {
4086 BUG_ON(!left_path);
4087 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4088 /*
4089 * Left path is easy - we can just allow the insert to
4090 * happen.
4091 */
4092 el = left_el;
4093 insert_el = left_el;
4094 index = ocfs2_search_extent_list(el, cpos);
4095 BUG_ON(index == -1);
4096 }
4097
4098 rec = &el->l_recs[index];
Joel Beckerc38e52b2009-02-13 02:56:23 -08004099 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4100 split, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004101 ocfs2_rotate_leaf(insert_el, split_rec);
4102}
4103
Mark Fashehdcd05382007-01-16 11:32:23 -08004104/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004105 * This function only does inserts on an allocation b-tree. For tree
4106 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004107 *
4108 * right_path is the path we want to do the actual insert
4109 * in. left_path should only be passed in if we need to update that
4110 * portion of the tree after an edge insert.
4111 */
Joel Becker3505bec2009-02-13 02:57:58 -08004112static int ocfs2_insert_path(handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08004113 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004114 struct ocfs2_path *left_path,
4115 struct ocfs2_path *right_path,
4116 struct ocfs2_extent_rec *insert_rec,
4117 struct ocfs2_insert_type *insert)
4118{
4119 int ret, subtree_index;
4120 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004121
Mark Fashehdcd05382007-01-16 11:32:23 -08004122 if (left_path) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004123 /*
4124 * There's a chance that left_path got passed back to
4125 * us without being accounted for in the
4126 * journal. Extend our transaction here to be sure we
4127 * can change those blocks.
4128 */
Tao Mac901fb02010-04-26 14:34:57 +08004129 ret = ocfs2_extend_trans(handle, left_path->p_tree_depth);
Mark Fashehdcd05382007-01-16 11:32:23 -08004130 if (ret < 0) {
4131 mlog_errno(ret);
4132 goto out;
4133 }
4134
Joel Becker7dc02802009-02-12 19:20:13 -08004135 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004136 if (ret < 0) {
4137 mlog_errno(ret);
4138 goto out;
4139 }
4140 }
4141
Mark Fashehe8aed342007-12-03 16:43:01 -08004142 /*
4143 * Pass both paths to the journal. The majority of inserts
4144 * will be touching all components anyway.
4145 */
Joel Becker7dc02802009-02-12 19:20:13 -08004146 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004147 if (ret < 0) {
4148 mlog_errno(ret);
4149 goto out;
4150 }
4151
Mark Fasheh328d5752007-06-18 10:48:04 -07004152 if (insert->ins_split != SPLIT_NONE) {
4153 /*
4154 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004155 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004156 * function sort it all out.
4157 */
Joel Beckerc38e52b2009-02-13 02:56:23 -08004158 ocfs2_split_record(et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004159 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004160
4161 /*
4162 * Split might have modified either leaf and we don't
4163 * have a guarantee that the later edge insert will
4164 * dirty this for us.
4165 */
4166 if (left_path)
Joel Beckerec20cec2010-03-19 14:13:52 -07004167 ocfs2_journal_dirty(handle,
4168 path_leaf_bh(left_path));
Mark Fasheh328d5752007-06-18 10:48:04 -07004169 } else
Joel Beckerd5628622009-02-13 02:54:36 -08004170 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4171 insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004172
Joel Beckerec20cec2010-03-19 14:13:52 -07004173 ocfs2_journal_dirty(handle, leaf_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004174
4175 if (left_path) {
4176 /*
4177 * The rotate code has indicated that we need to fix
4178 * up portions of the tree after the insert.
4179 *
4180 * XXX: Should we extend the transaction here?
4181 */
Joel Becker7dc02802009-02-12 19:20:13 -08004182 subtree_index = ocfs2_find_subtree_root(et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004183 right_path);
Joel Becker4619c732009-02-12 19:02:36 -08004184 ocfs2_complete_edge_insert(handle, left_path, right_path,
4185 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08004186 }
4187
4188 ret = 0;
4189out:
4190 return ret;
4191}
4192
Joel Becker3505bec2009-02-13 02:57:58 -08004193static int ocfs2_do_insert_extent(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004194 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004195 struct ocfs2_extent_rec *insert_rec,
4196 struct ocfs2_insert_type *type)
4197{
4198 int ret, rotate = 0;
4199 u32 cpos;
4200 struct ocfs2_path *right_path = NULL;
4201 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004202 struct ocfs2_extent_list *el;
4203
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004204 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004205
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004206 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004207 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004208 if (ret) {
4209 mlog_errno(ret);
4210 goto out;
4211 }
4212
4213 if (le16_to_cpu(el->l_tree_depth) == 0) {
Joel Beckerd5628622009-02-13 02:54:36 -08004214 ocfs2_insert_at_leaf(et, insert_rec, el, type);
Mark Fashehdcd05382007-01-16 11:32:23 -08004215 goto out_update_clusters;
4216 }
4217
Joel Beckerffdd7a52008-10-17 22:32:01 -07004218 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004219 if (!right_path) {
4220 ret = -ENOMEM;
4221 mlog_errno(ret);
4222 goto out;
4223 }
4224
4225 /*
4226 * Determine the path to start with. Rotations need the
4227 * rightmost path, everything else can go directly to the
4228 * target leaf.
4229 */
4230 cpos = le32_to_cpu(insert_rec->e_cpos);
4231 if (type->ins_appending == APPEND_NONE &&
4232 type->ins_contig == CONTIG_NONE) {
4233 rotate = 1;
4234 cpos = UINT_MAX;
4235 }
4236
Joel Beckerfacdb772009-02-12 18:08:48 -08004237 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004238 if (ret) {
4239 mlog_errno(ret);
4240 goto out;
4241 }
4242
4243 /*
4244 * Rotations and appends need special treatment - they modify
4245 * parts of the tree's above them.
4246 *
4247 * Both might pass back a path immediate to the left of the
4248 * one being inserted to. This will be cause
4249 * ocfs2_insert_path() to modify the rightmost records of
4250 * left_path to account for an edge insert.
4251 *
4252 * XXX: When modifying this code, keep in mind that an insert
4253 * can wind up skipping both of these two special cases...
4254 */
4255 if (rotate) {
Joel Becker1bbf0b82009-02-12 19:42:08 -08004256 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004257 le32_to_cpu(insert_rec->e_cpos),
4258 right_path, &left_path);
4259 if (ret) {
4260 mlog_errno(ret);
4261 goto out;
4262 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004263
4264 /*
4265 * ocfs2_rotate_tree_right() might have extended the
4266 * transaction without re-journaling our tree root.
4267 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004268 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004269 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004270 if (ret) {
4271 mlog_errno(ret);
4272 goto out;
4273 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004274 } else if (type->ins_appending == APPEND_TAIL
4275 && type->ins_contig != CONTIG_LEFT) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004276 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08004277 right_path, &left_path);
4278 if (ret) {
4279 mlog_errno(ret);
4280 goto out;
4281 }
4282 }
4283
Joel Becker3505bec2009-02-13 02:57:58 -08004284 ret = ocfs2_insert_path(handle, et, left_path, right_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004285 insert_rec, type);
4286 if (ret) {
4287 mlog_errno(ret);
4288 goto out;
4289 }
4290
4291out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004292 if (type->ins_split == SPLIT_NONE)
Joel Becker6136ca52009-02-12 19:32:43 -08004293 ocfs2_et_update_clusters(et,
Joel Becker35dc0aa2008-08-20 16:25:06 -07004294 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004295
Joel Beckerec20cec2010-03-19 14:13:52 -07004296 ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004297
4298out:
4299 ocfs2_free_path(left_path);
4300 ocfs2_free_path(right_path);
4301
4302 return ret;
4303}
4304
Mark Fasheh328d5752007-06-18 10:48:04 -07004305static enum ocfs2_contig_type
Joel Beckera2970292009-02-13 03:09:54 -08004306ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4307 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004308 struct ocfs2_extent_list *el, int index,
4309 struct ocfs2_extent_rec *split_rec)
4310{
Tao Maad5a4d72008-01-30 14:21:32 +08004311 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004312 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004313 u32 left_cpos, right_cpos;
4314 struct ocfs2_extent_rec *rec = NULL;
4315 struct ocfs2_extent_list *new_el;
4316 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4317 struct buffer_head *bh;
4318 struct ocfs2_extent_block *eb;
Joel Beckera2970292009-02-13 03:09:54 -08004319 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Tao Maad5a4d72008-01-30 14:21:32 +08004320
4321 if (index > 0) {
4322 rec = &el->l_recs[index - 1];
4323 } else if (path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004324 status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004325 if (status)
4326 goto out;
4327
4328 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004329 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004330 if (!left_path)
4331 goto out;
4332
Joel Beckera2970292009-02-13 03:09:54 -08004333 status = ocfs2_find_path(et->et_ci, left_path,
4334 left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004335 if (status)
4336 goto out;
4337
4338 new_el = path_leaf_el(left_path);
4339
4340 if (le16_to_cpu(new_el->l_next_free_rec) !=
4341 le16_to_cpu(new_el->l_count)) {
4342 bh = path_leaf_bh(left_path);
4343 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004344 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004345 "Extent block #%llu has an "
4346 "invalid l_next_free_rec of "
4347 "%d. It should have "
4348 "matched the l_count of %d",
4349 (unsigned long long)le64_to_cpu(eb->h_blkno),
4350 le16_to_cpu(new_el->l_next_free_rec),
4351 le16_to_cpu(new_el->l_count));
4352 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004353 goto out;
4354 }
4355 rec = &new_el->l_recs[
4356 le16_to_cpu(new_el->l_next_free_rec) - 1];
4357 }
4358 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004359
4360 /*
4361 * We're careful to check for an empty extent record here -
4362 * the merge code will know what to do if it sees one.
4363 */
Tao Maad5a4d72008-01-30 14:21:32 +08004364 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004365 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4366 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4367 ret = CONTIG_RIGHT;
4368 } else {
Tao Ma853a3a12009-08-18 11:22:18 +08004369 ret = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004370 }
4371 }
4372
Tao Maad5a4d72008-01-30 14:21:32 +08004373 rec = NULL;
4374 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4375 rec = &el->l_recs[index + 1];
4376 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4377 path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004378 status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004379 if (status)
4380 goto out;
4381
4382 if (right_cpos == 0)
4383 goto out;
4384
Joel Beckerffdd7a52008-10-17 22:32:01 -07004385 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004386 if (!right_path)
4387 goto out;
4388
Joel Beckera2970292009-02-13 03:09:54 -08004389 status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004390 if (status)
4391 goto out;
4392
4393 new_el = path_leaf_el(right_path);
4394 rec = &new_el->l_recs[0];
4395 if (ocfs2_is_empty_extent(rec)) {
4396 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4397 bh = path_leaf_bh(right_path);
4398 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004399 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004400 "Extent block #%llu has an "
4401 "invalid l_next_free_rec of %d",
4402 (unsigned long long)le64_to_cpu(eb->h_blkno),
4403 le16_to_cpu(new_el->l_next_free_rec));
4404 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004405 goto out;
4406 }
4407 rec = &new_el->l_recs[1];
4408 }
4409 }
4410
4411 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004412 enum ocfs2_contig_type contig_type;
4413
Tao Ma853a3a12009-08-18 11:22:18 +08004414 contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004415
4416 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4417 ret = CONTIG_LEFTRIGHT;
4418 else if (ret == CONTIG_NONE)
4419 ret = contig_type;
4420 }
4421
Tao Maad5a4d72008-01-30 14:21:32 +08004422out:
4423 if (left_path)
4424 ocfs2_free_path(left_path);
4425 if (right_path)
4426 ocfs2_free_path(right_path);
4427
Mark Fasheh328d5752007-06-18 10:48:04 -07004428 return ret;
4429}
4430
Joel Becker1ef61b32009-02-13 03:12:33 -08004431static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004432 struct ocfs2_insert_type *insert,
4433 struct ocfs2_extent_list *el,
Joel Becker1ef61b32009-02-13 03:12:33 -08004434 struct ocfs2_extent_rec *insert_rec)
Mark Fashehdcd05382007-01-16 11:32:23 -08004435{
4436 int i;
4437 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4438
Mark Fashehe48edee2007-03-07 16:46:57 -08004439 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4440
Mark Fashehdcd05382007-01-16 11:32:23 -08004441 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
Tao Ma853a3a12009-08-18 11:22:18 +08004442 contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4443 insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004444 if (contig_type != CONTIG_NONE) {
4445 insert->ins_contig_index = i;
4446 break;
4447 }
4448 }
4449 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004450
4451 if (insert->ins_contig != CONTIG_NONE) {
4452 struct ocfs2_extent_rec *rec =
4453 &el->l_recs[insert->ins_contig_index];
4454 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4455 le16_to_cpu(insert_rec->e_leaf_clusters);
4456
4457 /*
4458 * Caller might want us to limit the size of extents, don't
4459 * calculate contiguousness if we might exceed that limit.
4460 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004461 if (et->et_max_leaf_clusters &&
4462 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004463 insert->ins_contig = CONTIG_NONE;
4464 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004465}
4466
4467/*
4468 * This should only be called against the righmost leaf extent list.
4469 *
4470 * ocfs2_figure_appending_type() will figure out whether we'll have to
4471 * insert at the tail of the rightmost leaf.
4472 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004473 * This should also work against the root extent list for tree's with 0
4474 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004475 * then the logic here makes sense.
4476 */
4477static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4478 struct ocfs2_extent_list *el,
4479 struct ocfs2_extent_rec *insert_rec)
4480{
4481 int i;
4482 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4483 struct ocfs2_extent_rec *rec;
4484
4485 insert->ins_appending = APPEND_NONE;
4486
Mark Fashehe48edee2007-03-07 16:46:57 -08004487 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004488
4489 if (!el->l_next_free_rec)
4490 goto set_tail_append;
4491
4492 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4493 /* Were all records empty? */
4494 if (le16_to_cpu(el->l_next_free_rec) == 1)
4495 goto set_tail_append;
4496 }
4497
4498 i = le16_to_cpu(el->l_next_free_rec) - 1;
4499 rec = &el->l_recs[i];
4500
Mark Fashehe48edee2007-03-07 16:46:57 -08004501 if (cpos >=
4502 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004503 goto set_tail_append;
4504
4505 return;
4506
4507set_tail_append:
4508 insert->ins_appending = APPEND_TAIL;
4509}
4510
4511/*
4512 * Helper function called at the begining of an insert.
4513 *
4514 * This computes a few things that are commonly used in the process of
4515 * inserting into the btree:
4516 * - Whether the new extent is contiguous with an existing one.
4517 * - The current tree depth.
4518 * - Whether the insert is an appending one.
4519 * - The total # of free records in the tree.
4520 *
4521 * All of the information is stored on the ocfs2_insert_type
4522 * structure.
4523 */
Joel Becker627961b2009-02-13 03:14:38 -08004524static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004525 struct buffer_head **last_eb_bh,
4526 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004527 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004528 struct ocfs2_insert_type *insert)
4529{
4530 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004531 struct ocfs2_extent_block *eb;
4532 struct ocfs2_extent_list *el;
4533 struct ocfs2_path *path = NULL;
4534 struct buffer_head *bh = NULL;
4535
Mark Fasheh328d5752007-06-18 10:48:04 -07004536 insert->ins_split = SPLIT_NONE;
4537
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004538 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004539 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4540
4541 if (el->l_tree_depth) {
4542 /*
4543 * If we have tree depth, we read in the
4544 * rightmost extent block ahead of time as
4545 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4546 * may want it later.
4547 */
Joel Becker3d03a302009-02-12 17:49:26 -08004548 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08004549 ocfs2_et_get_last_eb_blk(et),
4550 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004551 if (ret) {
Tao Mac1e8d352011-03-07 16:43:21 +08004552 mlog_errno(ret);
Mark Fashehdcd05382007-01-16 11:32:23 -08004553 goto out;
4554 }
4555 eb = (struct ocfs2_extent_block *) bh->b_data;
4556 el = &eb->h_list;
4557 }
4558
4559 /*
4560 * Unless we have a contiguous insert, we'll need to know if
4561 * there is room left in our allocation tree for another
4562 * extent record.
4563 *
4564 * XXX: This test is simplistic, we can search for empty
4565 * extent records too.
4566 */
Tao Maoc77534f2007-08-28 17:22:33 -07004567 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004568 le16_to_cpu(el->l_next_free_rec);
4569
4570 if (!insert->ins_tree_depth) {
Joel Becker1ef61b32009-02-13 03:12:33 -08004571 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004572 ocfs2_figure_appending_type(insert, el, insert_rec);
4573 return 0;
4574 }
4575
Joel Beckerffdd7a52008-10-17 22:32:01 -07004576 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004577 if (!path) {
4578 ret = -ENOMEM;
4579 mlog_errno(ret);
4580 goto out;
4581 }
4582
4583 /*
4584 * In the case that we're inserting past what the tree
4585 * currently accounts for, ocfs2_find_path() will return for
4586 * us the rightmost tree path. This is accounted for below in
4587 * the appending code.
4588 */
Joel Beckerfacdb772009-02-12 18:08:48 -08004589 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -08004590 if (ret) {
4591 mlog_errno(ret);
4592 goto out;
4593 }
4594
4595 el = path_leaf_el(path);
4596
4597 /*
4598 * Now that we have the path, there's two things we want to determine:
4599 * 1) Contiguousness (also set contig_index if this is so)
4600 *
4601 * 2) Are we doing an append? We can trivially break this up
4602 * into two types of appends: simple record append, or a
4603 * rotate inside the tail leaf.
4604 */
Joel Becker1ef61b32009-02-13 03:12:33 -08004605 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004606
4607 /*
4608 * The insert code isn't quite ready to deal with all cases of
4609 * left contiguousness. Specifically, if it's an insert into
4610 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004611 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004612 * left. For now, just catch that case and fool the layers
4613 * above us. This works just fine for tree_depth == 0, which
4614 * is why we allow that above.
4615 */
4616 if (insert->ins_contig == CONTIG_LEFT &&
4617 insert->ins_contig_index == 0)
4618 insert->ins_contig = CONTIG_NONE;
4619
4620 /*
4621 * Ok, so we can simply compare against last_eb to figure out
4622 * whether the path doesn't exist. This will only happen in
4623 * the case that we're doing a tail append, so maybe we can
4624 * take advantage of that information somehow.
4625 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004626 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004627 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004628 /*
4629 * Ok, ocfs2_find_path() returned us the rightmost
4630 * tree path. This might be an appending insert. There are
4631 * two cases:
4632 * 1) We're doing a true append at the tail:
4633 * -This might even be off the end of the leaf
4634 * 2) We're "appending" by rotating in the tail
4635 */
4636 ocfs2_figure_appending_type(insert, el, insert_rec);
4637 }
4638
4639out:
4640 ocfs2_free_path(path);
4641
4642 if (ret == 0)
4643 *last_eb_bh = bh;
4644 else
4645 brelse(bh);
4646 return ret;
4647}
4648
4649/*
Joel Beckercc79d8c2009-02-13 03:24:43 -08004650 * Insert an extent into a btree.
Mark Fashehdcd05382007-01-16 11:32:23 -08004651 *
Joel Beckercc79d8c2009-02-13 03:24:43 -08004652 * The caller needs to update the owning btree's cluster count.
Mark Fashehdcd05382007-01-16 11:32:23 -08004653 */
Joel Beckercc79d8c2009-02-13 03:24:43 -08004654int ocfs2_insert_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004655 struct ocfs2_extent_tree *et,
4656 u32 cpos,
4657 u64 start_blk,
4658 u32 new_clusters,
4659 u8 flags,
4660 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004661{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004662 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004663 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004664 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004665 struct ocfs2_insert_type insert = {0, };
4666 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004667
Joel Beckercc79d8c2009-02-13 03:24:43 -08004668 mlog(0, "add %u clusters at position %u to owner %llu\n",
4669 new_clusters, cpos,
4670 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08004671
Mark Fashehe48edee2007-03-07 16:46:57 -08004672 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004673 rec.e_cpos = cpu_to_le32(cpos);
4674 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004675 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004676 rec.e_flags = flags;
Joel Becker6136ca52009-02-12 19:32:43 -08004677 status = ocfs2_et_insert_check(et, &rec);
Joel Becker1e61ee72008-08-20 18:32:45 -07004678 if (status) {
4679 mlog_errno(status);
4680 goto bail;
4681 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004682
Joel Becker627961b2009-02-13 03:14:38 -08004683 status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004684 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004685 if (status < 0) {
4686 mlog_errno(status);
4687 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004688 }
4689
Mark Fashehdcd05382007-01-16 11:32:23 -08004690 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4691 "Insert.contig_index: %d, Insert.free_records: %d, "
4692 "Insert.tree_depth: %d\n",
4693 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004694 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004695
Tao Maoc77534f2007-08-28 17:22:33 -07004696 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004697 status = ocfs2_grow_tree(handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004698 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004699 meta_ac);
4700 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004701 mlog_errno(status);
4702 goto bail;
4703 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004704 }
4705
Mark Fashehdcd05382007-01-16 11:32:23 -08004706 /* Finally, we can add clusters. This might rotate the tree for us. */
Joel Becker3505bec2009-02-13 02:57:58 -08004707 status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004708 if (status < 0)
4709 mlog_errno(status);
Joel Becker92ba4702009-02-13 03:18:34 -08004710 else
4711 ocfs2_et_extent_map_insert(et, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004712
4713bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004714 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004715
Tao Maf56654c2008-08-18 17:38:48 +08004716 return status;
4717}
4718
Tao Ma0eb8d472008-08-18 17:38:45 +08004719/*
4720 * Allcate and add clusters into the extent b-tree.
4721 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004722 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004723 * it is not limited to the file storage. Any extent tree can use this
4724 * function if it implements the proper ocfs2_extent_tree.
4725 */
Joel Beckercbee7e12009-02-13 03:34:15 -08004726int ocfs2_add_clusters_in_btree(handle_t *handle,
4727 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004728 u32 *logical_offset,
4729 u32 clusters_to_add,
4730 int mark_unwritten,
Tao Ma0eb8d472008-08-18 17:38:45 +08004731 struct ocfs2_alloc_context *data_ac,
4732 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004733 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004734{
4735 int status = 0;
4736 int free_extents;
4737 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4738 u32 bit_off, num_bits;
4739 u64 block;
4740 u8 flags = 0;
Joel Beckercbee7e12009-02-13 03:34:15 -08004741 struct ocfs2_super *osb =
4742 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Tao Ma0eb8d472008-08-18 17:38:45 +08004743
4744 BUG_ON(!clusters_to_add);
4745
4746 if (mark_unwritten)
4747 flags = OCFS2_EXT_UNWRITTEN;
4748
Joel Becker3d03a302009-02-12 17:49:26 -08004749 free_extents = ocfs2_num_free_extents(osb, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004750 if (free_extents < 0) {
4751 status = free_extents;
4752 mlog_errno(status);
4753 goto leave;
4754 }
4755
4756 /* there are two cases which could cause us to EAGAIN in the
4757 * we-need-more-metadata case:
4758 * 1) we haven't reserved *any*
4759 * 2) we are so fragmented, we've needed to add metadata too
4760 * many times. */
4761 if (!free_extents && !meta_ac) {
4762 mlog(0, "we haven't reserved any metadata!\n");
4763 status = -EAGAIN;
4764 reason = RESTART_META;
4765 goto leave;
4766 } else if ((!free_extents)
4767 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004768 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004769 mlog(0, "filesystem is really fragmented...\n");
4770 status = -EAGAIN;
4771 reason = RESTART_META;
4772 goto leave;
4773 }
4774
Joel Becker1ed9b772010-05-06 13:59:06 +08004775 status = __ocfs2_claim_clusters(handle, data_ac, 1,
Tao Ma0eb8d472008-08-18 17:38:45 +08004776 clusters_to_add, &bit_off, &num_bits);
4777 if (status < 0) {
4778 if (status != -ENOSPC)
4779 mlog_errno(status);
4780 goto leave;
4781 }
4782
4783 BUG_ON(num_bits > clusters_to_add);
4784
Joel Becker13723d02008-10-17 19:25:01 -07004785 /* reserve our write early -- insert_extent may update the tree root */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004786 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004787 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d472008-08-18 17:38:45 +08004788 if (status < 0) {
4789 mlog_errno(status);
4790 goto leave;
4791 }
4792
4793 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Joel Beckercbee7e12009-02-13 03:34:15 -08004794 mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
4795 num_bits, bit_off,
4796 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Joel Beckercc79d8c2009-02-13 03:24:43 -08004797 status = ocfs2_insert_extent(handle, et, *logical_offset, block,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004798 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004799 if (status < 0) {
4800 mlog_errno(status);
4801 goto leave;
4802 }
4803
Joel Beckerec20cec2010-03-19 14:13:52 -07004804 ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004805
4806 clusters_to_add -= num_bits;
4807 *logical_offset += num_bits;
4808
4809 if (clusters_to_add) {
4810 mlog(0, "need to alloc once more, wanted = %u\n",
4811 clusters_to_add);
4812 status = -EAGAIN;
4813 reason = RESTART_TRANS;
4814 }
4815
4816leave:
Tao Ma0eb8d472008-08-18 17:38:45 +08004817 if (reason_ret)
4818 *reason_ret = reason;
4819 return status;
4820}
4821
Mark Fasheh328d5752007-06-18 10:48:04 -07004822static void ocfs2_make_right_split_rec(struct super_block *sb,
4823 struct ocfs2_extent_rec *split_rec,
4824 u32 cpos,
4825 struct ocfs2_extent_rec *rec)
4826{
4827 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4828 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4829
4830 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4831
4832 split_rec->e_cpos = cpu_to_le32(cpos);
4833 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4834
4835 split_rec->e_blkno = rec->e_blkno;
4836 le64_add_cpu(&split_rec->e_blkno,
4837 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4838
4839 split_rec->e_flags = rec->e_flags;
4840}
4841
Joel Beckerd2311292009-02-13 03:43:22 -08004842static int ocfs2_split_and_insert(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004843 struct ocfs2_extent_tree *et,
Joel Beckerd2311292009-02-13 03:43:22 -08004844 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004845 struct buffer_head **last_eb_bh,
4846 int split_index,
4847 struct ocfs2_extent_rec *orig_split_rec,
4848 struct ocfs2_alloc_context *meta_ac)
4849{
4850 int ret = 0, depth;
4851 unsigned int insert_range, rec_range, do_leftright = 0;
4852 struct ocfs2_extent_rec tmprec;
4853 struct ocfs2_extent_list *rightmost_el;
4854 struct ocfs2_extent_rec rec;
4855 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4856 struct ocfs2_insert_type insert;
4857 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004858
4859leftright:
4860 /*
4861 * Store a copy of the record on the stack - it might move
4862 * around as the tree is manipulated below.
4863 */
4864 rec = path_leaf_el(path)->l_recs[split_index];
4865
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004866 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004867
4868 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4869 if (depth) {
4870 BUG_ON(!(*last_eb_bh));
4871 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4872 rightmost_el = &eb->h_list;
4873 }
4874
4875 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4876 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004877 ret = ocfs2_grow_tree(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08004878 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004879 if (ret) {
4880 mlog_errno(ret);
4881 goto out;
4882 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004883 }
4884
4885 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4886 insert.ins_appending = APPEND_NONE;
4887 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004888 insert.ins_tree_depth = depth;
4889
4890 insert_range = le32_to_cpu(split_rec.e_cpos) +
4891 le16_to_cpu(split_rec.e_leaf_clusters);
4892 rec_range = le32_to_cpu(rec.e_cpos) +
4893 le16_to_cpu(rec.e_leaf_clusters);
4894
4895 if (split_rec.e_cpos == rec.e_cpos) {
4896 insert.ins_split = SPLIT_LEFT;
4897 } else if (insert_range == rec_range) {
4898 insert.ins_split = SPLIT_RIGHT;
4899 } else {
4900 /*
4901 * Left/right split. We fake this as a right split
4902 * first and then make a second pass as a left split.
4903 */
4904 insert.ins_split = SPLIT_RIGHT;
4905
Joel Beckerd2311292009-02-13 03:43:22 -08004906 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4907 &tmprec, insert_range, &rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004908
4909 split_rec = tmprec;
4910
4911 BUG_ON(do_leftright);
4912 do_leftright = 1;
4913 }
4914
Joel Becker3505bec2009-02-13 02:57:58 -08004915 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004916 if (ret) {
4917 mlog_errno(ret);
4918 goto out;
4919 }
4920
4921 if (do_leftright == 1) {
4922 u32 cpos;
4923 struct ocfs2_extent_list *el;
4924
4925 do_leftright++;
4926 split_rec = *orig_split_rec;
4927
4928 ocfs2_reinit_path(path, 1);
4929
4930 cpos = le32_to_cpu(split_rec.e_cpos);
Joel Beckerfacdb772009-02-12 18:08:48 -08004931 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07004932 if (ret) {
4933 mlog_errno(ret);
4934 goto out;
4935 }
4936
4937 el = path_leaf_el(path);
4938 split_index = ocfs2_search_extent_list(el, cpos);
4939 goto leftright;
4940 }
4941out:
4942
4943 return ret;
4944}
4945
Joel Beckerf3868d02009-02-17 19:46:04 -08004946static int ocfs2_replace_extent_rec(handle_t *handle,
4947 struct ocfs2_extent_tree *et,
Tao Ma47be12e2009-01-09 07:32:48 +08004948 struct ocfs2_path *path,
4949 struct ocfs2_extent_list *el,
4950 int split_index,
4951 struct ocfs2_extent_rec *split_rec)
4952{
4953 int ret;
4954
Joel Beckerf3868d02009-02-17 19:46:04 -08004955 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Tao Ma47be12e2009-01-09 07:32:48 +08004956 path_num_items(path) - 1);
4957 if (ret) {
4958 mlog_errno(ret);
4959 goto out;
4960 }
4961
4962 el->l_recs[split_index] = *split_rec;
4963
4964 ocfs2_journal_dirty(handle, path_leaf_bh(path));
4965out:
4966 return ret;
4967}
4968
Mark Fasheh328d5752007-06-18 10:48:04 -07004969/*
Tao Ma555936b2009-08-18 11:22:21 +08004970 * Split part or all of the extent record at split_index in the leaf
4971 * pointed to by path. Merge with the contiguous extent record if needed.
Mark Fasheh328d5752007-06-18 10:48:04 -07004972 *
4973 * Care is taken to handle contiguousness so as to not grow the tree.
4974 *
4975 * meta_ac is not strictly necessary - we only truly need it if growth
4976 * of the tree is required. All other cases will degrade into a less
4977 * optimal tree layout.
4978 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004979 * last_eb_bh should be the rightmost leaf block for any extent
4980 * btree. Since a split may grow the tree or a merge might shrink it,
4981 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07004982 *
4983 * This code is optimized for readability - several passes might be
4984 * made over certain portions of the tree. All of those blocks will
4985 * have been brought into cache (and pinned via the journal), so the
4986 * extra overhead is not expressed in terms of disk reads.
4987 */
Tao Mae2e9f602009-08-18 11:22:34 +08004988int ocfs2_split_extent(handle_t *handle,
4989 struct ocfs2_extent_tree *et,
4990 struct ocfs2_path *path,
4991 int split_index,
4992 struct ocfs2_extent_rec *split_rec,
4993 struct ocfs2_alloc_context *meta_ac,
4994 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07004995{
4996 int ret = 0;
4997 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004998 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07004999 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5000 struct ocfs2_merge_ctxt ctxt;
5001 struct ocfs2_extent_list *rightmost_el;
5002
Mark Fasheh328d5752007-06-18 10:48:04 -07005003 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5004 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5005 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5006 ret = -EIO;
5007 mlog_errno(ret);
5008 goto out;
5009 }
5010
Joel Beckera2970292009-02-13 03:09:54 -08005011 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005012 split_index,
5013 split_rec);
5014
5015 /*
5016 * The core merge / split code wants to know how much room is
Joel Beckera1cf0762009-02-13 03:45:49 -08005017 * left in this allocation tree, so we pass the
Mark Fasheh328d5752007-06-18 10:48:04 -07005018 * rightmost extent list.
5019 */
5020 if (path->p_tree_depth) {
5021 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005022
Joel Becker3d03a302009-02-12 17:49:26 -08005023 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005024 ocfs2_et_get_last_eb_blk(et),
5025 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005026 if (ret) {
Tao Mac1e8d352011-03-07 16:43:21 +08005027 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07005028 goto out;
5029 }
5030
5031 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005032 rightmost_el = &eb->h_list;
5033 } else
5034 rightmost_el = path_root_el(path);
5035
Mark Fasheh328d5752007-06-18 10:48:04 -07005036 if (rec->e_cpos == split_rec->e_cpos &&
5037 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5038 ctxt.c_split_covers_rec = 1;
5039 else
5040 ctxt.c_split_covers_rec = 0;
5041
5042 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5043
Mark Fasheh015452b2007-09-12 10:21:22 -07005044 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5045 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5046 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005047
5048 if (ctxt.c_contig_type == CONTIG_NONE) {
5049 if (ctxt.c_split_covers_rec)
Joel Beckerf3868d02009-02-17 19:46:04 -08005050 ret = ocfs2_replace_extent_rec(handle, et, path, el,
Tao Ma47be12e2009-01-09 07:32:48 +08005051 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005052 else
Joel Beckerd2311292009-02-13 03:43:22 -08005053 ret = ocfs2_split_and_insert(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005054 &last_eb_bh, split_index,
5055 split_rec, meta_ac);
5056 if (ret)
5057 mlog_errno(ret);
5058 } else {
Joel Beckerc495dd22009-02-13 02:19:11 -08005059 ret = ocfs2_try_to_merge_extent(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005060 split_index, split_rec,
Joel Beckerc495dd22009-02-13 02:19:11 -08005061 dealloc, &ctxt);
Mark Fasheh328d5752007-06-18 10:48:04 -07005062 if (ret)
5063 mlog_errno(ret);
5064 }
5065
Mark Fasheh328d5752007-06-18 10:48:04 -07005066out:
5067 brelse(last_eb_bh);
5068 return ret;
5069}
5070
5071/*
Tao Ma555936b2009-08-18 11:22:21 +08005072 * Change the flags of the already-existing extent at cpos for len clusters.
5073 *
5074 * new_flags: the flags we want to set.
5075 * clear_flags: the flags we want to clear.
5076 * phys: the new physical offset we want this new extent starts from.
Mark Fasheh328d5752007-06-18 10:48:04 -07005077 *
5078 * If the existing extent is larger than the request, initiate a
5079 * split. An attempt will be made at merging with adjacent extents.
5080 *
5081 * The caller is responsible for passing down meta_ac if we'll need it.
5082 */
Tao Ma1aa75fe2009-08-18 11:28:39 +08005083int ocfs2_change_extent_flag(handle_t *handle,
5084 struct ocfs2_extent_tree *et,
5085 u32 cpos, u32 len, u32 phys,
5086 struct ocfs2_alloc_context *meta_ac,
5087 struct ocfs2_cached_dealloc_ctxt *dealloc,
5088 int new_flags, int clear_flags)
Mark Fasheh328d5752007-06-18 10:48:04 -07005089{
5090 int ret, index;
Tao Ma555936b2009-08-18 11:22:21 +08005091 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5092 u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
Mark Fasheh328d5752007-06-18 10:48:04 -07005093 struct ocfs2_extent_rec split_rec;
5094 struct ocfs2_path *left_path = NULL;
5095 struct ocfs2_extent_list *el;
Tao Ma555936b2009-08-18 11:22:21 +08005096 struct ocfs2_extent_rec *rec;
Mark Fasheh328d5752007-06-18 10:48:04 -07005097
Joel Beckerffdd7a52008-10-17 22:32:01 -07005098 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005099 if (!left_path) {
5100 ret = -ENOMEM;
5101 mlog_errno(ret);
5102 goto out;
5103 }
5104
Joel Beckerfacdb772009-02-12 18:08:48 -08005105 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005106 if (ret) {
5107 mlog_errno(ret);
5108 goto out;
5109 }
5110 el = path_leaf_el(left_path);
5111
5112 index = ocfs2_search_extent_list(el, cpos);
5113 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Tao Ma555936b2009-08-18 11:22:21 +08005114 ocfs2_error(sb,
5115 "Owner %llu has an extent at cpos %u which can no "
Mark Fasheh328d5752007-06-18 10:48:04 -07005116 "longer be found.\n",
Tao Ma555936b2009-08-18 11:22:21 +08005117 (unsigned long long)
5118 ocfs2_metadata_cache_owner(et->et_ci), cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005119 ret = -EROFS;
5120 goto out;
5121 }
5122
Tao Ma555936b2009-08-18 11:22:21 +08005123 ret = -EIO;
5124 rec = &el->l_recs[index];
5125 if (new_flags && (rec->e_flags & new_flags)) {
5126 mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
5127 "extent that already had them",
5128 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5129 new_flags);
5130 goto out;
5131 }
5132
5133 if (clear_flags && !(rec->e_flags & clear_flags)) {
5134 mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
5135 "extent that didn't have them",
5136 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5137 clear_flags);
5138 goto out;
5139 }
5140
Mark Fasheh328d5752007-06-18 10:48:04 -07005141 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5142 split_rec.e_cpos = cpu_to_le32(cpos);
5143 split_rec.e_leaf_clusters = cpu_to_le16(len);
5144 split_rec.e_blkno = cpu_to_le64(start_blkno);
Tao Ma555936b2009-08-18 11:22:21 +08005145 split_rec.e_flags = rec->e_flags;
5146 if (new_flags)
5147 split_rec.e_flags |= new_flags;
5148 if (clear_flags)
5149 split_rec.e_flags &= ~clear_flags;
Mark Fasheh328d5752007-06-18 10:48:04 -07005150
Tao Mae2e9f602009-08-18 11:22:34 +08005151 ret = ocfs2_split_extent(handle, et, left_path,
5152 index, &split_rec, meta_ac,
5153 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005154 if (ret)
5155 mlog_errno(ret);
5156
5157out:
5158 ocfs2_free_path(left_path);
5159 return ret;
Tao Ma555936b2009-08-18 11:22:21 +08005160
5161}
5162
5163/*
5164 * Mark the already-existing extent at cpos as written for len clusters.
5165 * This removes the unwritten extent flag.
5166 *
5167 * If the existing extent is larger than the request, initiate a
5168 * split. An attempt will be made at merging with adjacent extents.
5169 *
5170 * The caller is responsible for passing down meta_ac if we'll need it.
5171 */
5172int ocfs2_mark_extent_written(struct inode *inode,
5173 struct ocfs2_extent_tree *et,
5174 handle_t *handle, u32 cpos, u32 len, u32 phys,
5175 struct ocfs2_alloc_context *meta_ac,
5176 struct ocfs2_cached_dealloc_ctxt *dealloc)
5177{
5178 int ret;
5179
5180 mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
5181 inode->i_ino, cpos, len, phys);
5182
5183 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5184 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5185 "that are being written to, but the feature bit "
5186 "is not set in the super block.",
5187 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5188 ret = -EROFS;
5189 goto out;
5190 }
5191
5192 /*
5193 * XXX: This should be fixed up so that we just re-insert the
5194 * next extent records.
5195 */
5196 ocfs2_et_extent_map_truncate(et, 0);
5197
5198 ret = ocfs2_change_extent_flag(handle, et, cpos,
5199 len, phys, meta_ac, dealloc,
5200 0, OCFS2_EXT_UNWRITTEN);
5201 if (ret)
5202 mlog_errno(ret);
5203
5204out:
5205 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07005206}
5207
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005208static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5209 struct ocfs2_path *path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005210 int index, u32 new_range,
5211 struct ocfs2_alloc_context *meta_ac)
5212{
Tao Mac901fb02010-04-26 14:34:57 +08005213 int ret, depth, credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005214 struct buffer_head *last_eb_bh = NULL;
5215 struct ocfs2_extent_block *eb;
5216 struct ocfs2_extent_list *rightmost_el, *el;
5217 struct ocfs2_extent_rec split_rec;
5218 struct ocfs2_extent_rec *rec;
5219 struct ocfs2_insert_type insert;
5220
5221 /*
5222 * Setup the record to split before we grow the tree.
5223 */
5224 el = path_leaf_el(path);
5225 rec = &el->l_recs[index];
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005226 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5227 &split_rec, new_range, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005228
5229 depth = path->p_tree_depth;
5230 if (depth > 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08005231 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005232 ocfs2_et_get_last_eb_blk(et),
5233 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005234 if (ret < 0) {
5235 mlog_errno(ret);
5236 goto out;
5237 }
5238
5239 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5240 rightmost_el = &eb->h_list;
5241 } else
5242 rightmost_el = path_leaf_el(path);
5243
Tao Mac901fb02010-04-26 14:34:57 +08005244 credits = path->p_tree_depth +
5245 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005246 ret = ocfs2_extend_trans(handle, credits);
5247 if (ret) {
5248 mlog_errno(ret);
5249 goto out;
5250 }
5251
5252 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5253 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08005254 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005255 meta_ac);
5256 if (ret) {
5257 mlog_errno(ret);
5258 goto out;
5259 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005260 }
5261
5262 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5263 insert.ins_appending = APPEND_NONE;
5264 insert.ins_contig = CONTIG_NONE;
5265 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005266 insert.ins_tree_depth = depth;
5267
Joel Becker3505bec2009-02-13 02:57:58 -08005268 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005269 if (ret)
5270 mlog_errno(ret);
5271
5272out:
5273 brelse(last_eb_bh);
5274 return ret;
5275}
5276
Joel Becker043beeb2009-02-13 02:42:30 -08005277static int ocfs2_truncate_rec(handle_t *handle,
5278 struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005279 struct ocfs2_path *path, int index,
5280 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker043beeb2009-02-13 02:42:30 -08005281 u32 cpos, u32 len)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005282{
5283 int ret;
5284 u32 left_cpos, rec_range, trunc_range;
5285 int wants_rotate = 0, is_rightmost_tree_rec = 0;
Joel Becker043beeb2009-02-13 02:42:30 -08005286 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005287 struct ocfs2_path *left_path = NULL;
5288 struct ocfs2_extent_list *el = path_leaf_el(path);
5289 struct ocfs2_extent_rec *rec;
5290 struct ocfs2_extent_block *eb;
5291
5292 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Joel Becker70f18c02009-02-13 02:09:31 -08005293 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005294 if (ret) {
5295 mlog_errno(ret);
5296 goto out;
5297 }
5298
5299 index--;
5300 }
5301
5302 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5303 path->p_tree_depth) {
5304 /*
5305 * Check whether this is the rightmost tree record. If
5306 * we remove all of this record or part of its right
5307 * edge then an update of the record lengths above it
5308 * will be required.
5309 */
5310 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5311 if (eb->h_next_leaf_blk == 0)
5312 is_rightmost_tree_rec = 1;
5313 }
5314
5315 rec = &el->l_recs[index];
5316 if (index == 0 && path->p_tree_depth &&
5317 le32_to_cpu(rec->e_cpos) == cpos) {
5318 /*
5319 * Changing the leftmost offset (via partial or whole
5320 * record truncate) of an interior (or rightmost) path
5321 * means we have to update the subtree that is formed
5322 * by this leaf and the one to it's left.
5323 *
5324 * There are two cases we can skip:
Joel Becker043beeb2009-02-13 02:42:30 -08005325 * 1) Path is the leftmost one in our btree.
Mark Fashehd0c7d702007-07-03 13:27:22 -07005326 * 2) The leaf is rightmost and will be empty after
5327 * we remove the extent record - the rotate code
5328 * knows how to update the newly formed edge.
5329 */
5330
Joel Becker043beeb2009-02-13 02:42:30 -08005331 ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005332 if (ret) {
5333 mlog_errno(ret);
5334 goto out;
5335 }
5336
5337 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005338 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005339 if (!left_path) {
5340 ret = -ENOMEM;
5341 mlog_errno(ret);
5342 goto out;
5343 }
5344
Joel Beckerfacdb772009-02-12 18:08:48 -08005345 ret = ocfs2_find_path(et->et_ci, left_path,
5346 left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005347 if (ret) {
5348 mlog_errno(ret);
5349 goto out;
5350 }
5351 }
5352 }
5353
5354 ret = ocfs2_extend_rotate_transaction(handle, 0,
5355 handle->h_buffer_credits,
5356 path);
5357 if (ret) {
5358 mlog_errno(ret);
5359 goto out;
5360 }
5361
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005362 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005363 if (ret) {
5364 mlog_errno(ret);
5365 goto out;
5366 }
5367
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005368 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005369 if (ret) {
5370 mlog_errno(ret);
5371 goto out;
5372 }
5373
5374 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5375 trunc_range = cpos + len;
5376
5377 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5378 int next_free;
5379
5380 memset(rec, 0, sizeof(*rec));
5381 ocfs2_cleanup_merge(el, index);
5382 wants_rotate = 1;
5383
5384 next_free = le16_to_cpu(el->l_next_free_rec);
5385 if (is_rightmost_tree_rec && next_free > 1) {
5386 /*
5387 * We skip the edge update if this path will
5388 * be deleted by the rotate code.
5389 */
5390 rec = &el->l_recs[next_free - 1];
Joel Beckerd401dc12009-02-13 02:24:10 -08005391 ocfs2_adjust_rightmost_records(handle, et, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005392 rec);
5393 }
5394 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5395 /* Remove leftmost portion of the record. */
5396 le32_add_cpu(&rec->e_cpos, len);
5397 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5398 le16_add_cpu(&rec->e_leaf_clusters, -len);
5399 } else if (rec_range == trunc_range) {
5400 /* Remove rightmost portion of the record */
5401 le16_add_cpu(&rec->e_leaf_clusters, -len);
5402 if (is_rightmost_tree_rec)
Joel Beckerd401dc12009-02-13 02:24:10 -08005403 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005404 } else {
5405 /* Caller should have trapped this. */
Joel Becker043beeb2009-02-13 02:42:30 -08005406 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5407 "(%u, %u)\n",
5408 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005409 le32_to_cpu(rec->e_cpos),
5410 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5411 BUG();
5412 }
5413
5414 if (left_path) {
5415 int subtree_index;
5416
Joel Becker7dc02802009-02-12 19:20:13 -08005417 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Joel Becker4619c732009-02-12 19:02:36 -08005418 ocfs2_complete_edge_insert(handle, left_path, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005419 subtree_index);
5420 }
5421
5422 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5423
Joel Becker70f18c02009-02-13 02:09:31 -08005424 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005425 if (ret) {
5426 mlog_errno(ret);
5427 goto out;
5428 }
5429
5430out:
5431 ocfs2_free_path(left_path);
5432 return ret;
5433}
5434
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005435int ocfs2_remove_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005436 struct ocfs2_extent_tree *et,
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005437 u32 cpos, u32 len,
Mark Fasheh063c4562007-07-03 13:34:11 -07005438 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005439 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005440{
5441 int ret, index;
5442 u32 rec_range, trunc_range;
5443 struct ocfs2_extent_rec *rec;
5444 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005445 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005446
Joel Becker4c911ee2009-02-13 02:50:12 -08005447 /*
5448 * XXX: Why are we truncating to 0 instead of wherever this
5449 * affects us?
5450 */
5451 ocfs2_et_extent_map_truncate(et, 0);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005452
Joel Beckerffdd7a52008-10-17 22:32:01 -07005453 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005454 if (!path) {
5455 ret = -ENOMEM;
5456 mlog_errno(ret);
5457 goto out;
5458 }
5459
Joel Beckerfacdb772009-02-12 18:08:48 -08005460 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005461 if (ret) {
5462 mlog_errno(ret);
5463 goto out;
5464 }
5465
5466 el = path_leaf_el(path);
5467 index = ocfs2_search_extent_list(el, cpos);
5468 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005469 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5470 "Owner %llu has an extent at cpos %u which can no "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005471 "longer be found.\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005472 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5473 cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005474 ret = -EROFS;
5475 goto out;
5476 }
5477
5478 /*
5479 * We have 3 cases of extent removal:
5480 * 1) Range covers the entire extent rec
5481 * 2) Range begins or ends on one edge of the extent rec
5482 * 3) Range is in the middle of the extent rec (no shared edges)
5483 *
5484 * For case 1 we remove the extent rec and left rotate to
5485 * fill the hole.
5486 *
5487 * For case 2 we just shrink the existing extent rec, with a
5488 * tree update if the shrinking edge is also the edge of an
5489 * extent block.
5490 *
5491 * For case 3 we do a right split to turn the extent rec into
5492 * something case 2 can handle.
5493 */
5494 rec = &el->l_recs[index];
5495 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5496 trunc_range = cpos + len;
5497
5498 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5499
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005500 mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005501 "(cpos %u, len %u)\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005502 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5503 cpos, len, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005504 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5505
5506 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
Joel Becker043beeb2009-02-13 02:42:30 -08005507 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5508 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005509 if (ret) {
5510 mlog_errno(ret);
5511 goto out;
5512 }
5513 } else {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005514 ret = ocfs2_split_tree(handle, et, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005515 trunc_range, meta_ac);
5516 if (ret) {
5517 mlog_errno(ret);
5518 goto out;
5519 }
5520
5521 /*
5522 * The split could have manipulated the tree enough to
5523 * move the record location, so we have to look for it again.
5524 */
5525 ocfs2_reinit_path(path, 1);
5526
Joel Beckerfacdb772009-02-12 18:08:48 -08005527 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005528 if (ret) {
5529 mlog_errno(ret);
5530 goto out;
5531 }
5532
5533 el = path_leaf_el(path);
5534 index = ocfs2_search_extent_list(el, cpos);
5535 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005536 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5537 "Owner %llu: split at cpos %u lost record.",
5538 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005539 cpos);
5540 ret = -EROFS;
5541 goto out;
5542 }
5543
5544 /*
5545 * Double check our values here. If anything is fishy,
5546 * it's easier to catch it at the top level.
5547 */
5548 rec = &el->l_recs[index];
5549 rec_range = le32_to_cpu(rec->e_cpos) +
5550 ocfs2_rec_clusters(el, rec);
5551 if (rec_range != trunc_range) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005552 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5553 "Owner %llu: error after split at cpos %u"
Mark Fashehd0c7d702007-07-03 13:27:22 -07005554 "trunc len %u, existing record is (%u,%u)",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005555 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005556 cpos, len, le32_to_cpu(rec->e_cpos),
5557 ocfs2_rec_clusters(el, rec));
5558 ret = -EROFS;
5559 goto out;
5560 }
5561
Joel Becker043beeb2009-02-13 02:42:30 -08005562 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5563 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005564 if (ret) {
5565 mlog_errno(ret);
5566 goto out;
5567 }
5568 }
5569
5570out:
5571 ocfs2_free_path(path);
5572 return ret;
5573}
5574
Tristan Ye78f94672010-05-11 17:54:42 +08005575/*
5576 * ocfs2_reserve_blocks_for_rec_trunc() would look basically the
5577 * same as ocfs2_lock_alloctors(), except for it accepts a blocks
5578 * number to reserve some extra blocks, and it only handles meta
5579 * data allocations.
5580 *
5581 * Currently, only ocfs2_remove_btree_range() uses it for truncating
5582 * and punching holes.
5583 */
5584static int ocfs2_reserve_blocks_for_rec_trunc(struct inode *inode,
5585 struct ocfs2_extent_tree *et,
5586 u32 extents_to_split,
5587 struct ocfs2_alloc_context **ac,
5588 int extra_blocks)
5589{
5590 int ret = 0, num_free_extents;
5591 unsigned int max_recs_needed = 2 * extents_to_split;
5592 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5593
5594 *ac = NULL;
5595
5596 num_free_extents = ocfs2_num_free_extents(osb, et);
5597 if (num_free_extents < 0) {
5598 ret = num_free_extents;
5599 mlog_errno(ret);
5600 goto out;
5601 }
5602
5603 if (!num_free_extents ||
5604 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed))
5605 extra_blocks += ocfs2_extend_meta_needed(et->et_root_el);
5606
5607 if (extra_blocks) {
5608 ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, ac);
5609 if (ret < 0) {
5610 if (ret != -ENOSPC)
5611 mlog_errno(ret);
5612 goto out;
5613 }
5614 }
5615
5616out:
5617 if (ret) {
5618 if (*ac) {
5619 ocfs2_free_alloc_context(*ac);
5620 *ac = NULL;
5621 }
5622 }
5623
5624 return ret;
5625}
5626
Mark Fashehfecc0112008-11-12 15:16:38 -08005627int ocfs2_remove_btree_range(struct inode *inode,
5628 struct ocfs2_extent_tree *et,
Tristan Ye78f94672010-05-11 17:54:42 +08005629 u32 cpos, u32 phys_cpos, u32 len, int flags,
5630 struct ocfs2_cached_dealloc_ctxt *dealloc,
5631 u64 refcount_loc)
Mark Fashehfecc0112008-11-12 15:16:38 -08005632{
Tristan Ye78f94672010-05-11 17:54:42 +08005633 int ret, credits = 0, extra_blocks = 0;
Mark Fashehfecc0112008-11-12 15:16:38 -08005634 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5635 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5636 struct inode *tl_inode = osb->osb_tl_inode;
5637 handle_t *handle;
5638 struct ocfs2_alloc_context *meta_ac = NULL;
Tristan Ye78f94672010-05-11 17:54:42 +08005639 struct ocfs2_refcount_tree *ref_tree = NULL;
Mark Fashehfecc0112008-11-12 15:16:38 -08005640
Tristan Ye78f94672010-05-11 17:54:42 +08005641 if ((flags & OCFS2_EXT_REFCOUNTED) && len) {
5642 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features &
5643 OCFS2_HAS_REFCOUNT_FL));
5644
5645 ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
5646 &ref_tree, NULL);
5647 if (ret) {
5648 mlog_errno(ret);
5649 goto out;
5650 }
5651
5652 ret = ocfs2_prepare_refcount_change_for_del(inode,
5653 refcount_loc,
5654 phys_blkno,
5655 len,
5656 &credits,
5657 &extra_blocks);
5658 if (ret < 0) {
5659 mlog_errno(ret);
5660 goto out;
5661 }
5662 }
5663
5664 ret = ocfs2_reserve_blocks_for_rec_trunc(inode, et, 1, &meta_ac,
5665 extra_blocks);
Mark Fashehfecc0112008-11-12 15:16:38 -08005666 if (ret) {
5667 mlog_errno(ret);
5668 return ret;
5669 }
5670
5671 mutex_lock(&tl_inode->i_mutex);
5672
5673 if (ocfs2_truncate_log_needs_flush(osb)) {
5674 ret = __ocfs2_flush_truncate_log(osb);
5675 if (ret < 0) {
5676 mlog_errno(ret);
5677 goto out;
5678 }
5679 }
5680
Tristan Ye78f94672010-05-11 17:54:42 +08005681 handle = ocfs2_start_trans(osb,
5682 ocfs2_remove_extent_credits(osb->sb) + credits);
Mark Fashehfecc0112008-11-12 15:16:38 -08005683 if (IS_ERR(handle)) {
5684 ret = PTR_ERR(handle);
5685 mlog_errno(ret);
5686 goto out;
5687 }
5688
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005689 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07005690 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005691 if (ret) {
5692 mlog_errno(ret);
5693 goto out;
5694 }
5695
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005696 dquot_free_space_nodirty(inode,
Mark Fashehfd4ef232009-01-29 15:06:21 -08005697 ocfs2_clusters_to_bytes(inode->i_sb, len));
5698
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005699 ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
Mark Fashehfecc0112008-11-12 15:16:38 -08005700 if (ret) {
5701 mlog_errno(ret);
5702 goto out_commit;
5703 }
5704
Joel Becker6136ca52009-02-12 19:32:43 -08005705 ocfs2_et_update_clusters(et, -len);
Mark Fashehfecc0112008-11-12 15:16:38 -08005706
Joel Beckerec20cec2010-03-19 14:13:52 -07005707 ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehfecc0112008-11-12 15:16:38 -08005708
Tristan Ye78f94672010-05-11 17:54:42 +08005709 if (phys_blkno) {
5710 if (flags & OCFS2_EXT_REFCOUNTED)
5711 ret = ocfs2_decrease_refcount(inode, handle,
5712 ocfs2_blocks_to_clusters(osb->sb,
5713 phys_blkno),
5714 len, meta_ac,
5715 dealloc, 1);
5716 else
5717 ret = ocfs2_truncate_log_append(osb, handle,
5718 phys_blkno, len);
5719 if (ret)
5720 mlog_errno(ret);
5721
5722 }
Mark Fashehfecc0112008-11-12 15:16:38 -08005723
5724out_commit:
5725 ocfs2_commit_trans(osb, handle);
5726out:
5727 mutex_unlock(&tl_inode->i_mutex);
5728
5729 if (meta_ac)
5730 ocfs2_free_alloc_context(meta_ac);
5731
Tristan Ye78f94672010-05-11 17:54:42 +08005732 if (ref_tree)
5733 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
5734
Mark Fashehfecc0112008-11-12 15:16:38 -08005735 return ret;
5736}
5737
Mark Fasheh063c4562007-07-03 13:34:11 -07005738int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005739{
5740 struct buffer_head *tl_bh = osb->osb_tl_bh;
5741 struct ocfs2_dinode *di;
5742 struct ocfs2_truncate_log *tl;
5743
5744 di = (struct ocfs2_dinode *) tl_bh->b_data;
5745 tl = &di->id2.i_dealloc;
5746
5747 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5748 "slot %d, invalid truncate log parameters: used = "
5749 "%u, count = %u\n", osb->slot_num,
5750 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5751 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5752}
5753
5754static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5755 unsigned int new_start)
5756{
5757 unsigned int tail_index;
5758 unsigned int current_tail;
5759
5760 /* No records, nothing to coalesce */
5761 if (!le16_to_cpu(tl->tl_used))
5762 return 0;
5763
5764 tail_index = le16_to_cpu(tl->tl_used) - 1;
5765 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5766 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5767
5768 return current_tail == new_start;
5769}
5770
Mark Fasheh063c4562007-07-03 13:34:11 -07005771int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5772 handle_t *handle,
5773 u64 start_blk,
5774 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005775{
5776 int status, index;
5777 unsigned int start_cluster, tl_count;
5778 struct inode *tl_inode = osb->osb_tl_inode;
5779 struct buffer_head *tl_bh = osb->osb_tl_bh;
5780 struct ocfs2_dinode *di;
5781 struct ocfs2_truncate_log *tl;
5782
Tao Maef6b6892011-02-21 11:10:44 +08005783 mlog(0, "start_blk = %llu, num_clusters = %u\n",
5784 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005785
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005786 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005787
5788 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5789
5790 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005791
Joel Becker10995aa2008-11-13 14:49:12 -08005792 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5793 * by the underlying call to ocfs2_read_inode_block(), so any
5794 * corruption is a code bug */
5795 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5796
5797 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005798 tl_count = le16_to_cpu(tl->tl_count);
5799 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5800 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005801 "Truncate record count on #%llu invalid "
5802 "wanted %u, actual %u\n",
5803 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005804 ocfs2_truncate_recs_per_inode(osb->sb),
5805 le16_to_cpu(tl->tl_count));
5806
5807 /* Caller should have known to flush before calling us. */
5808 index = le16_to_cpu(tl->tl_used);
5809 if (index >= tl_count) {
5810 status = -ENOSPC;
5811 mlog_errno(status);
5812 goto bail;
5813 }
5814
Joel Becker0cf2f762009-02-12 16:41:25 -08005815 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005816 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005817 if (status < 0) {
5818 mlog_errno(status);
5819 goto bail;
5820 }
5821
5822 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005823 "%llu (index = %d)\n", num_clusters, start_cluster,
5824 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005825
5826 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5827 /*
5828 * Move index back to the record we are coalescing with.
5829 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5830 */
5831 index--;
5832
5833 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5834 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5835 index, le32_to_cpu(tl->tl_recs[index].t_start),
5836 num_clusters);
5837 } else {
5838 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5839 tl->tl_used = cpu_to_le16(index + 1);
5840 }
5841 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5842
Joel Beckerec20cec2010-03-19 14:13:52 -07005843 ocfs2_journal_dirty(handle, tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005844
Tao Ma50308d82010-11-04 15:14:11 +08005845 osb->truncated_clusters += num_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08005846bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08005847 return status;
5848}
5849
5850static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005851 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005852 struct inode *data_alloc_inode,
5853 struct buffer_head *data_alloc_bh)
5854{
5855 int status = 0;
5856 int i;
5857 unsigned int num_clusters;
5858 u64 start_blk;
5859 struct ocfs2_truncate_rec rec;
5860 struct ocfs2_dinode *di;
5861 struct ocfs2_truncate_log *tl;
5862 struct inode *tl_inode = osb->osb_tl_inode;
5863 struct buffer_head *tl_bh = osb->osb_tl_bh;
5864
Mark Fashehccd979b2005-12-15 14:31:24 -08005865 di = (struct ocfs2_dinode *) tl_bh->b_data;
5866 tl = &di->id2.i_dealloc;
5867 i = le16_to_cpu(tl->tl_used) - 1;
5868 while (i >= 0) {
5869 /* Caller has given us at least enough credits to
5870 * update the truncate log dinode */
Joel Becker0cf2f762009-02-12 16:41:25 -08005871 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005872 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005873 if (status < 0) {
5874 mlog_errno(status);
5875 goto bail;
5876 }
5877
5878 tl->tl_used = cpu_to_le16(i);
5879
Joel Beckerec20cec2010-03-19 14:13:52 -07005880 ocfs2_journal_dirty(handle, tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005881
5882 /* TODO: Perhaps we can calculate the bulk of the
5883 * credits up front rather than extending like
5884 * this. */
5885 status = ocfs2_extend_trans(handle,
5886 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5887 if (status < 0) {
5888 mlog_errno(status);
5889 goto bail;
5890 }
5891
5892 rec = tl->tl_recs[i];
5893 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5894 le32_to_cpu(rec.t_start));
5895 num_clusters = le32_to_cpu(rec.t_clusters);
5896
5897 /* if start_blk is not set, we ignore the record as
5898 * invalid. */
5899 if (start_blk) {
5900 mlog(0, "free record %d, start = %u, clusters = %u\n",
5901 i, le32_to_cpu(rec.t_start), num_clusters);
5902
5903 status = ocfs2_free_clusters(handle, data_alloc_inode,
5904 data_alloc_bh, start_blk,
5905 num_clusters);
5906 if (status < 0) {
5907 mlog_errno(status);
5908 goto bail;
5909 }
5910 }
5911 i--;
5912 }
5913
Tao Ma50308d82010-11-04 15:14:11 +08005914 osb->truncated_clusters = 0;
5915
Mark Fashehccd979b2005-12-15 14:31:24 -08005916bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08005917 return status;
5918}
5919
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005920/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005921int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005922{
5923 int status;
5924 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005925 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005926 struct inode *tl_inode = osb->osb_tl_inode;
5927 struct inode *data_alloc_inode = NULL;
5928 struct buffer_head *tl_bh = osb->osb_tl_bh;
5929 struct buffer_head *data_alloc_bh = NULL;
5930 struct ocfs2_dinode *di;
5931 struct ocfs2_truncate_log *tl;
5932
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005933 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005934
5935 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005936
Joel Becker10995aa2008-11-13 14:49:12 -08005937 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5938 * by the underlying call to ocfs2_read_inode_block(), so any
5939 * corruption is a code bug */
5940 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5941
5942 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005943 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005944 mlog(0, "Flush %u records from truncate log #%llu\n",
5945 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005946 if (!num_to_flush) {
5947 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005948 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005949 }
5950
5951 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5952 GLOBAL_BITMAP_SYSTEM_INODE,
5953 OCFS2_INVALID_SLOT);
5954 if (!data_alloc_inode) {
5955 status = -EINVAL;
5956 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005957 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005958 }
5959
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005960 mutex_lock(&data_alloc_inode->i_mutex);
5961
Mark Fashehe63aecb62007-10-18 15:30:42 -07005962 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005963 if (status < 0) {
5964 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005965 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08005966 }
5967
Mark Fasheh65eff9c2006-10-09 17:26:22 -07005968 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005969 if (IS_ERR(handle)) {
5970 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005971 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005972 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08005973 }
5974
5975 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
5976 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005977 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08005978 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08005979
Mark Fasheh02dc1af2006-10-09 16:48:10 -07005980 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005981
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005982out_unlock:
5983 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07005984 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08005985
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005986out_mutex:
5987 mutex_unlock(&data_alloc_inode->i_mutex);
5988 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08005989
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005990out:
Mark Fashehccd979b2005-12-15 14:31:24 -08005991 return status;
5992}
5993
5994int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
5995{
5996 int status;
5997 struct inode *tl_inode = osb->osb_tl_inode;
5998
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005999 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006000 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006001 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006002
6003 return status;
6004}
6005
David Howellsc4028952006-11-22 14:57:56 +00006006static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08006007{
6008 int status;
David Howellsc4028952006-11-22 14:57:56 +00006009 struct ocfs2_super *osb =
6010 container_of(work, struct ocfs2_super,
6011 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08006012
Mark Fashehccd979b2005-12-15 14:31:24 -08006013 status = ocfs2_flush_truncate_log(osb);
6014 if (status < 0)
6015 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08006016 else
Tiger Yangb89c5422010-01-25 14:11:06 +08006017 ocfs2_init_steal_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08006018}
6019
6020#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
6021void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
6022 int cancel)
6023{
6024 if (osb->osb_tl_inode) {
6025 /* We want to push off log flushes while truncates are
6026 * still running. */
6027 if (cancel)
6028 cancel_delayed_work(&osb->osb_truncate_log_wq);
6029
6030 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
6031 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
6032 }
6033}
6034
6035static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
6036 int slot_num,
6037 struct inode **tl_inode,
6038 struct buffer_head **tl_bh)
6039{
6040 int status;
6041 struct inode *inode = NULL;
6042 struct buffer_head *bh = NULL;
6043
6044 inode = ocfs2_get_system_file_inode(osb,
6045 TRUNCATE_LOG_SYSTEM_INODE,
6046 slot_num);
6047 if (!inode) {
6048 status = -EINVAL;
6049 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
6050 goto bail;
6051 }
6052
Joel Beckerb657c952008-11-13 14:49:11 -08006053 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006054 if (status < 0) {
6055 iput(inode);
6056 mlog_errno(status);
6057 goto bail;
6058 }
6059
6060 *tl_inode = inode;
6061 *tl_bh = bh;
6062bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08006063 return status;
6064}
6065
6066/* called during the 1st stage of node recovery. we stamp a clean
6067 * truncate log and pass back a copy for processing later. if the
6068 * truncate log does not require processing, a *tl_copy is set to
6069 * NULL. */
6070int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6071 int slot_num,
6072 struct ocfs2_dinode **tl_copy)
6073{
6074 int status;
6075 struct inode *tl_inode = NULL;
6076 struct buffer_head *tl_bh = NULL;
6077 struct ocfs2_dinode *di;
6078 struct ocfs2_truncate_log *tl;
6079
6080 *tl_copy = NULL;
6081
6082 mlog(0, "recover truncate log from slot %d\n", slot_num);
6083
6084 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6085 if (status < 0) {
6086 mlog_errno(status);
6087 goto bail;
6088 }
6089
6090 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08006091
Joel Becker10995aa2008-11-13 14:49:12 -08006092 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6093 * validated by the underlying call to ocfs2_read_inode_block(),
6094 * so any corruption is a code bug */
6095 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6096
6097 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08006098 if (le16_to_cpu(tl->tl_used)) {
6099 mlog(0, "We'll have %u logs to recover\n",
6100 le16_to_cpu(tl->tl_used));
6101
6102 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6103 if (!(*tl_copy)) {
6104 status = -ENOMEM;
6105 mlog_errno(status);
6106 goto bail;
6107 }
6108
6109 /* Assuming the write-out below goes well, this copy
6110 * will be passed back to recovery for processing. */
6111 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6112
6113 /* All we need to do to clear the truncate log is set
6114 * tl_used. */
6115 tl->tl_used = 0;
6116
Joel Becker13723d02008-10-17 19:25:01 -07006117 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08006118 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08006119 if (status < 0) {
6120 mlog_errno(status);
6121 goto bail;
6122 }
6123 }
6124
6125bail:
6126 if (tl_inode)
6127 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006128 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006129
6130 if (status < 0 && (*tl_copy)) {
6131 kfree(*tl_copy);
6132 *tl_copy = NULL;
Tao Mac1e8d352011-03-07 16:43:21 +08006133 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08006134 }
6135
Mark Fashehccd979b2005-12-15 14:31:24 -08006136 return status;
6137}
6138
6139int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6140 struct ocfs2_dinode *tl_copy)
6141{
6142 int status = 0;
6143 int i;
6144 unsigned int clusters, num_recs, start_cluster;
6145 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006146 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006147 struct inode *tl_inode = osb->osb_tl_inode;
6148 struct ocfs2_truncate_log *tl;
6149
Mark Fashehccd979b2005-12-15 14:31:24 -08006150 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6151 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6152 return -EINVAL;
6153 }
6154
6155 tl = &tl_copy->id2.i_dealloc;
6156 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08006157 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006158 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006159
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006160 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006161 for(i = 0; i < num_recs; i++) {
6162 if (ocfs2_truncate_log_needs_flush(osb)) {
6163 status = __ocfs2_flush_truncate_log(osb);
6164 if (status < 0) {
6165 mlog_errno(status);
6166 goto bail_up;
6167 }
6168 }
6169
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006170 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006171 if (IS_ERR(handle)) {
6172 status = PTR_ERR(handle);
6173 mlog_errno(status);
6174 goto bail_up;
6175 }
6176
6177 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6178 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6179 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6180
6181 status = ocfs2_truncate_log_append(osb, handle,
6182 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006183 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006184 if (status < 0) {
6185 mlog_errno(status);
6186 goto bail_up;
6187 }
6188 }
6189
6190bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006191 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006192
Mark Fashehccd979b2005-12-15 14:31:24 -08006193 return status;
6194}
6195
6196void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6197{
6198 int status;
6199 struct inode *tl_inode = osb->osb_tl_inode;
6200
Mark Fashehccd979b2005-12-15 14:31:24 -08006201 if (tl_inode) {
6202 cancel_delayed_work(&osb->osb_truncate_log_wq);
6203 flush_workqueue(ocfs2_wq);
6204
6205 status = ocfs2_flush_truncate_log(osb);
6206 if (status < 0)
6207 mlog_errno(status);
6208
6209 brelse(osb->osb_tl_bh);
6210 iput(osb->osb_tl_inode);
6211 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006212}
6213
6214int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6215{
6216 int status;
6217 struct inode *tl_inode = NULL;
6218 struct buffer_head *tl_bh = NULL;
6219
Mark Fashehccd979b2005-12-15 14:31:24 -08006220 status = ocfs2_get_truncate_log_info(osb,
6221 osb->slot_num,
6222 &tl_inode,
6223 &tl_bh);
6224 if (status < 0)
6225 mlog_errno(status);
6226
6227 /* ocfs2_truncate_log_shutdown keys on the existence of
6228 * osb->osb_tl_inode so we don't set any of the osb variables
6229 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006230 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6231 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006232 osb->osb_tl_bh = tl_bh;
6233 osb->osb_tl_inode = tl_inode;
6234
Mark Fashehccd979b2005-12-15 14:31:24 -08006235 return status;
6236}
6237
Mark Fasheh2b604352007-06-22 15:45:27 -07006238/*
6239 * Delayed de-allocation of suballocator blocks.
6240 *
6241 * Some sets of block de-allocations might involve multiple suballocator inodes.
6242 *
6243 * The locking for this can get extremely complicated, especially when
6244 * the suballocator inodes to delete from aren't known until deep
6245 * within an unrelated codepath.
6246 *
6247 * ocfs2_extent_block structures are a good example of this - an inode
6248 * btree could have been grown by any number of nodes each allocating
6249 * out of their own suballoc inode.
6250 *
6251 * These structures allow the delay of block de-allocation until a
6252 * later time, when locking of multiple cluster inodes won't cause
6253 * deadlock.
6254 */
6255
6256/*
Tao Ma2891d292008-11-12 08:26:58 +08006257 * Describe a single bit freed from a suballocator. For the block
6258 * suballocators, it represents one block. For the global cluster
6259 * allocator, it represents some clusters and free_bit indicates
6260 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006261 */
6262struct ocfs2_cached_block_free {
6263 struct ocfs2_cached_block_free *free_next;
Tao Ma74380c42010-03-22 14:20:18 +08006264 u64 free_bg;
Mark Fasheh2b604352007-06-22 15:45:27 -07006265 u64 free_blk;
6266 unsigned int free_bit;
6267};
6268
6269struct ocfs2_per_slot_free_list {
6270 struct ocfs2_per_slot_free_list *f_next_suballocator;
6271 int f_inode_type;
6272 int f_slot;
6273 struct ocfs2_cached_block_free *f_first;
6274};
6275
Tao Ma2891d292008-11-12 08:26:58 +08006276static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6277 int sysfile_type,
6278 int slot,
6279 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006280{
6281 int ret;
6282 u64 bg_blkno;
6283 handle_t *handle;
6284 struct inode *inode;
6285 struct buffer_head *di_bh = NULL;
6286 struct ocfs2_cached_block_free *tmp;
6287
6288 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6289 if (!inode) {
6290 ret = -EINVAL;
6291 mlog_errno(ret);
6292 goto out;
6293 }
6294
6295 mutex_lock(&inode->i_mutex);
6296
Mark Fashehe63aecb62007-10-18 15:30:42 -07006297 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006298 if (ret) {
6299 mlog_errno(ret);
6300 goto out_mutex;
6301 }
6302
6303 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6304 if (IS_ERR(handle)) {
6305 ret = PTR_ERR(handle);
6306 mlog_errno(ret);
6307 goto out_unlock;
6308 }
6309
6310 while (head) {
Tao Ma74380c42010-03-22 14:20:18 +08006311 if (head->free_bg)
6312 bg_blkno = head->free_bg;
6313 else
6314 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6315 head->free_bit);
Mark Fasheh2b604352007-06-22 15:45:27 -07006316 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6317 head->free_bit, (unsigned long long)head->free_blk);
6318
6319 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6320 head->free_bit, bg_blkno, 1);
6321 if (ret) {
6322 mlog_errno(ret);
6323 goto out_journal;
6324 }
6325
6326 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6327 if (ret) {
6328 mlog_errno(ret);
6329 goto out_journal;
6330 }
6331
6332 tmp = head;
6333 head = head->free_next;
6334 kfree(tmp);
6335 }
6336
6337out_journal:
6338 ocfs2_commit_trans(osb, handle);
6339
6340out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006341 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006342 brelse(di_bh);
6343out_mutex:
6344 mutex_unlock(&inode->i_mutex);
6345 iput(inode);
6346out:
6347 while(head) {
6348 /* Premature exit may have left some dangling items. */
6349 tmp = head;
6350 head = head->free_next;
6351 kfree(tmp);
6352 }
6353
6354 return ret;
6355}
6356
Tao Ma2891d292008-11-12 08:26:58 +08006357int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6358 u64 blkno, unsigned int bit)
6359{
6360 int ret = 0;
6361 struct ocfs2_cached_block_free *item;
6362
Tao Ma74380c42010-03-22 14:20:18 +08006363 item = kzalloc(sizeof(*item), GFP_NOFS);
Tao Ma2891d292008-11-12 08:26:58 +08006364 if (item == NULL) {
6365 ret = -ENOMEM;
6366 mlog_errno(ret);
6367 return ret;
6368 }
6369
6370 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6371 bit, (unsigned long long)blkno);
6372
6373 item->free_blk = blkno;
6374 item->free_bit = bit;
6375 item->free_next = ctxt->c_global_allocator;
6376
6377 ctxt->c_global_allocator = item;
6378 return ret;
6379}
6380
6381static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6382 struct ocfs2_cached_block_free *head)
6383{
6384 struct ocfs2_cached_block_free *tmp;
6385 struct inode *tl_inode = osb->osb_tl_inode;
6386 handle_t *handle;
6387 int ret = 0;
6388
6389 mutex_lock(&tl_inode->i_mutex);
6390
6391 while (head) {
6392 if (ocfs2_truncate_log_needs_flush(osb)) {
6393 ret = __ocfs2_flush_truncate_log(osb);
6394 if (ret < 0) {
6395 mlog_errno(ret);
6396 break;
6397 }
6398 }
6399
6400 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6401 if (IS_ERR(handle)) {
6402 ret = PTR_ERR(handle);
6403 mlog_errno(ret);
6404 break;
6405 }
6406
6407 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6408 head->free_bit);
6409
6410 ocfs2_commit_trans(osb, handle);
6411 tmp = head;
6412 head = head->free_next;
6413 kfree(tmp);
6414
6415 if (ret < 0) {
6416 mlog_errno(ret);
6417 break;
6418 }
6419 }
6420
6421 mutex_unlock(&tl_inode->i_mutex);
6422
6423 while (head) {
6424 /* Premature exit may have left some dangling items. */
6425 tmp = head;
6426 head = head->free_next;
6427 kfree(tmp);
6428 }
6429
6430 return ret;
6431}
6432
Mark Fasheh2b604352007-06-22 15:45:27 -07006433int ocfs2_run_deallocs(struct ocfs2_super *osb,
6434 struct ocfs2_cached_dealloc_ctxt *ctxt)
6435{
6436 int ret = 0, ret2;
6437 struct ocfs2_per_slot_free_list *fl;
6438
6439 if (!ctxt)
6440 return 0;
6441
6442 while (ctxt->c_first_suballocator) {
6443 fl = ctxt->c_first_suballocator;
6444
6445 if (fl->f_first) {
6446 mlog(0, "Free items: (type %u, slot %d)\n",
6447 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006448 ret2 = ocfs2_free_cached_blocks(osb,
6449 fl->f_inode_type,
6450 fl->f_slot,
6451 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006452 if (ret2)
6453 mlog_errno(ret2);
6454 if (!ret)
6455 ret = ret2;
6456 }
6457
6458 ctxt->c_first_suballocator = fl->f_next_suballocator;
6459 kfree(fl);
6460 }
6461
Tao Ma2891d292008-11-12 08:26:58 +08006462 if (ctxt->c_global_allocator) {
6463 ret2 = ocfs2_free_cached_clusters(osb,
6464 ctxt->c_global_allocator);
6465 if (ret2)
6466 mlog_errno(ret2);
6467 if (!ret)
6468 ret = ret2;
6469
6470 ctxt->c_global_allocator = NULL;
6471 }
6472
Mark Fasheh2b604352007-06-22 15:45:27 -07006473 return ret;
6474}
6475
6476static struct ocfs2_per_slot_free_list *
6477ocfs2_find_per_slot_free_list(int type,
6478 int slot,
6479 struct ocfs2_cached_dealloc_ctxt *ctxt)
6480{
6481 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6482
6483 while (fl) {
6484 if (fl->f_inode_type == type && fl->f_slot == slot)
6485 return fl;
6486
6487 fl = fl->f_next_suballocator;
6488 }
6489
6490 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6491 if (fl) {
6492 fl->f_inode_type = type;
6493 fl->f_slot = slot;
6494 fl->f_first = NULL;
6495 fl->f_next_suballocator = ctxt->c_first_suballocator;
6496
6497 ctxt->c_first_suballocator = fl;
6498 }
6499 return fl;
6500}
6501
Tao Ma1823cb02009-08-18 11:24:49 +08006502int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
Tao Ma74380c42010-03-22 14:20:18 +08006503 int type, int slot, u64 suballoc,
6504 u64 blkno, unsigned int bit)
Mark Fasheh2b604352007-06-22 15:45:27 -07006505{
6506 int ret;
6507 struct ocfs2_per_slot_free_list *fl;
6508 struct ocfs2_cached_block_free *item;
6509
6510 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6511 if (fl == NULL) {
6512 ret = -ENOMEM;
6513 mlog_errno(ret);
6514 goto out;
6515 }
6516
Tao Ma74380c42010-03-22 14:20:18 +08006517 item = kzalloc(sizeof(*item), GFP_NOFS);
Mark Fasheh2b604352007-06-22 15:45:27 -07006518 if (item == NULL) {
6519 ret = -ENOMEM;
6520 mlog_errno(ret);
6521 goto out;
6522 }
6523
6524 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6525 type, slot, bit, (unsigned long long)blkno);
6526
Tao Ma74380c42010-03-22 14:20:18 +08006527 item->free_bg = suballoc;
Mark Fasheh2b604352007-06-22 15:45:27 -07006528 item->free_blk = blkno;
6529 item->free_bit = bit;
6530 item->free_next = fl->f_first;
6531
6532 fl->f_first = item;
6533
6534 ret = 0;
6535out:
6536 return ret;
6537}
6538
Mark Fasheh59a5e412007-06-22 15:52:36 -07006539static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6540 struct ocfs2_extent_block *eb)
6541{
6542 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6543 le16_to_cpu(eb->h_suballoc_slot),
Tao Ma74380c42010-03-22 14:20:18 +08006544 le64_to_cpu(eb->h_suballoc_loc),
Mark Fasheh59a5e412007-06-22 15:52:36 -07006545 le64_to_cpu(eb->h_blkno),
6546 le16_to_cpu(eb->h_suballoc_bit));
6547}
6548
Joel Becker2b4e30f2008-09-03 20:03:41 -07006549static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006550{
6551 set_buffer_uptodate(bh);
6552 mark_buffer_dirty(bh);
6553 return 0;
6554}
6555
Tao Ma6f70fa52009-08-25 08:05:12 +08006556void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6557 unsigned int from, unsigned int to,
6558 struct page *page, int zero, u64 *phys)
Mark Fasheh1d410a62007-09-07 14:20:45 -07006559{
6560 int ret, partial = 0;
6561
6562 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6563 if (ret)
6564 mlog_errno(ret);
6565
6566 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006567 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006568
6569 /*
6570 * Need to set the buffers we zero'd into uptodate
6571 * here if they aren't - ocfs2_map_page_blocks()
6572 * might've skipped some
6573 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006574 ret = walk_page_buffers(handle, page_buffers(page),
6575 from, to, &partial,
6576 ocfs2_zero_func);
6577 if (ret < 0)
6578 mlog_errno(ret);
6579 else if (ocfs2_should_order_data(inode)) {
6580 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006581 if (ret < 0)
6582 mlog_errno(ret);
6583 }
6584
6585 if (!partial)
6586 SetPageUptodate(page);
6587
6588 flush_dcache_page(page);
6589}
6590
Mark Fasheh35edec12007-07-06 14:41:18 -07006591static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6592 loff_t end, struct page **pages,
6593 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006594{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006595 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006596 struct page *page;
6597 unsigned int from, to = PAGE_CACHE_SIZE;
6598 struct super_block *sb = inode->i_sb;
6599
6600 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6601
6602 if (numpages == 0)
6603 goto out;
6604
Mark Fasheh35edec12007-07-06 14:41:18 -07006605 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006606 for(i = 0; i < numpages; i++) {
6607 page = pages[i];
6608
Mark Fasheh35edec12007-07-06 14:41:18 -07006609 from = start & (PAGE_CACHE_SIZE - 1);
6610 if ((end >> PAGE_CACHE_SHIFT) == page->index)
6611 to = end & (PAGE_CACHE_SIZE - 1);
6612
Mark Fasheh60b11392007-02-16 11:46:50 -08006613 BUG_ON(from > PAGE_CACHE_SIZE);
6614 BUG_ON(to > PAGE_CACHE_SIZE);
6615
Mark Fasheh1d410a62007-09-07 14:20:45 -07006616 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6617 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006618
Mark Fasheh35edec12007-07-06 14:41:18 -07006619 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006620 }
6621out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006622 if (pages)
6623 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006624}
6625
Tao Ma6f70fa52009-08-25 08:05:12 +08006626int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
6627 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006628{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006629 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006630 struct address_space *mapping = inode->i_mapping;
6631 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006632 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006633
Mark Fasheh35edec12007-07-06 14:41:18 -07006634 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006635
Mark Fasheh1d410a62007-09-07 14:20:45 -07006636 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006637 last_page_bytes = PAGE_ALIGN(end);
6638 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006639 do {
Jan Kara9b4c0ff2010-08-24 14:28:03 +02006640 pages[numpages] = find_or_create_page(mapping, index, GFP_NOFS);
Mark Fasheh60b11392007-02-16 11:46:50 -08006641 if (!pages[numpages]) {
6642 ret = -ENOMEM;
6643 mlog_errno(ret);
6644 goto out;
6645 }
6646
6647 numpages++;
6648 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07006649 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006650
6651out:
6652 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006653 if (pages)
6654 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006655 numpages = 0;
6656 }
6657
6658 *num = numpages;
6659
6660 return ret;
6661}
6662
Tao Ma6f70fa52009-08-25 08:05:12 +08006663static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6664 struct page **pages, int *num)
6665{
6666 struct super_block *sb = inode->i_sb;
6667
6668 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6669 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6670
6671 return ocfs2_grab_pages(inode, start, end, pages, num);
6672}
6673
Mark Fasheh60b11392007-02-16 11:46:50 -08006674/*
6675 * Zero the area past i_size but still within an allocated
6676 * cluster. This avoids exposing nonzero data on subsequent file
6677 * extends.
6678 *
6679 * We need to call this before i_size is updated on the inode because
6680 * otherwise block_write_full_page() will skip writeout of pages past
6681 * i_size. The new_i_size parameter is passed for this reason.
6682 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006683int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6684 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006685{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006686 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006687 struct page **pages = NULL;
6688 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006689 unsigned int ext_flags;
6690 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006691
6692 /*
6693 * File systems which don't support sparse files zero on every
6694 * extend.
6695 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07006696 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08006697 return 0;
6698
Mark Fasheh1d410a62007-09-07 14:20:45 -07006699 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08006700 sizeof(struct page *), GFP_NOFS);
6701 if (pages == NULL) {
6702 ret = -ENOMEM;
6703 mlog_errno(ret);
6704 goto out;
6705 }
6706
Mark Fasheh1d410a62007-09-07 14:20:45 -07006707 if (range_start == range_end)
6708 goto out;
6709
6710 ret = ocfs2_extent_map_get_blocks(inode,
6711 range_start >> sb->s_blocksize_bits,
6712 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08006713 if (ret) {
6714 mlog_errno(ret);
6715 goto out;
6716 }
6717
Mark Fasheh1d410a62007-09-07 14:20:45 -07006718 /*
6719 * Tail is a hole, or is marked unwritten. In either case, we
6720 * can count on read and write to return/push zero's.
6721 */
6722 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08006723 goto out;
6724
Mark Fasheh1d410a62007-09-07 14:20:45 -07006725 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6726 &numpages);
6727 if (ret) {
6728 mlog_errno(ret);
6729 goto out;
6730 }
6731
Mark Fasheh35edec12007-07-06 14:41:18 -07006732 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6733 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08006734
6735 /*
6736 * Initiate writeout of the pages we zero'd here. We don't
6737 * wait on them - the truncate_inode_pages() call later will
6738 * do that for us.
6739 */
Christoph Hellwig2cfd30a2009-09-23 15:04:02 +02006740 ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
6741 range_end - 1);
Mark Fasheh60b11392007-02-16 11:46:50 -08006742 if (ret)
6743 mlog_errno(ret);
6744
6745out:
6746 if (pages)
6747 kfree(pages);
6748
6749 return ret;
6750}
6751
Tiger Yangfdd77702008-08-18 17:08:55 +08006752static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6753 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006754{
6755 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08006756 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006757
Tiger Yangfdd77702008-08-18 17:08:55 +08006758 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6759 memset(&di->id2, 0, blocksize -
6760 offsetof(struct ocfs2_dinode, id2) -
6761 xattrsize);
6762 else
6763 memset(&di->id2, 0, blocksize -
6764 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006765}
6766
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006767void ocfs2_dinode_new_extent_list(struct inode *inode,
6768 struct ocfs2_dinode *di)
6769{
Tiger Yangfdd77702008-08-18 17:08:55 +08006770 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006771 di->id2.i_list.l_tree_depth = 0;
6772 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08006773 di->id2.i_list.l_count = cpu_to_le16(
6774 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006775}
6776
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006777void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6778{
6779 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6780 struct ocfs2_inline_data *idata = &di->id2.i_data;
6781
6782 spin_lock(&oi->ip_lock);
6783 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6784 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6785 spin_unlock(&oi->ip_lock);
6786
6787 /*
6788 * We clear the entire i_data structure here so that all
6789 * fields can be properly initialized.
6790 */
Tiger Yangfdd77702008-08-18 17:08:55 +08006791 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006792
Tiger Yangfdd77702008-08-18 17:08:55 +08006793 idata->id_count = cpu_to_le16(
6794 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006795}
6796
6797int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6798 struct buffer_head *di_bh)
6799{
6800 int ret, i, has_data, num_pages = 0;
6801 handle_t *handle;
6802 u64 uninitialized_var(block);
6803 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6804 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6805 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006806 struct ocfs2_alloc_context *data_ac = NULL;
6807 struct page **pages = NULL;
6808 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07006809 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02006810 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006811
6812 has_data = i_size_read(inode) ? 1 : 0;
6813
6814 if (has_data) {
6815 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6816 sizeof(struct page *), GFP_NOFS);
6817 if (pages == NULL) {
6818 ret = -ENOMEM;
6819 mlog_errno(ret);
6820 goto out;
6821 }
6822
6823 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6824 if (ret) {
6825 mlog_errno(ret);
6826 goto out;
6827 }
6828 }
6829
Jan Karaa90714c2008-10-09 19:38:40 +02006830 handle = ocfs2_start_trans(osb,
6831 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006832 if (IS_ERR(handle)) {
6833 ret = PTR_ERR(handle);
6834 mlog_errno(ret);
6835 goto out_unlock;
6836 }
6837
Joel Becker0cf2f762009-02-12 16:41:25 -08006838 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07006839 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006840 if (ret) {
6841 mlog_errno(ret);
6842 goto out_commit;
6843 }
6844
6845 if (has_data) {
6846 u32 bit_off, num;
6847 unsigned int page_end;
6848 u64 phys;
6849
Christoph Hellwig5dd40562010-03-03 09:05:00 -05006850 ret = dquot_alloc_space_nodirty(inode,
6851 ocfs2_clusters_to_bytes(osb->sb, 1));
6852 if (ret)
Jan Karaa90714c2008-10-09 19:38:40 +02006853 goto out_commit;
Jan Karaa90714c2008-10-09 19:38:40 +02006854 did_quota = 1;
6855
Mark Fasheh4fe370a2009-12-07 13:15:40 -08006856 data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
6857
Joel Becker1ed9b772010-05-06 13:59:06 +08006858 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006859 &num);
6860 if (ret) {
6861 mlog_errno(ret);
6862 goto out_commit;
6863 }
6864
6865 /*
6866 * Save two copies, one for insert, and one that can
6867 * be changed by ocfs2_map_and_dirty_page() below.
6868 */
6869 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6870
6871 /*
6872 * Non sparse file systems zero on extend, so no need
6873 * to do that now.
6874 */
6875 if (!ocfs2_sparse_alloc(osb) &&
6876 PAGE_CACHE_SIZE < osb->s_clustersize)
6877 end = PAGE_CACHE_SIZE;
6878
6879 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6880 if (ret) {
6881 mlog_errno(ret);
6882 goto out_commit;
6883 }
6884
6885 /*
6886 * This should populate the 1st page for us and mark
6887 * it up to date.
6888 */
6889 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6890 if (ret) {
6891 mlog_errno(ret);
6892 goto out_commit;
6893 }
6894
6895 page_end = PAGE_CACHE_SIZE;
6896 if (PAGE_CACHE_SIZE > osb->s_clustersize)
6897 page_end = osb->s_clustersize;
6898
6899 for (i = 0; i < num_pages; i++)
6900 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6901 pages[i], i > 0, &phys);
6902 }
6903
6904 spin_lock(&oi->ip_lock);
6905 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6906 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6907 spin_unlock(&oi->ip_lock);
6908
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006909 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006910
6911 ocfs2_journal_dirty(handle, di_bh);
6912
6913 if (has_data) {
6914 /*
6915 * An error at this point should be extremely rare. If
6916 * this proves to be false, we could always re-build
6917 * the in-inode data from our pages.
6918 */
Joel Becker5e404e92009-02-13 03:54:22 -08006919 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
Joel Beckercc79d8c2009-02-13 03:24:43 -08006920 ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006921 if (ret) {
6922 mlog_errno(ret);
6923 goto out_commit;
6924 }
6925
6926 inode->i_blocks = ocfs2_inode_sector_count(inode);
6927 }
6928
6929out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02006930 if (ret < 0 && did_quota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05006931 dquot_free_space_nodirty(inode,
Jan Karaa90714c2008-10-09 19:38:40 +02006932 ocfs2_clusters_to_bytes(osb->sb, 1));
6933
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006934 ocfs2_commit_trans(osb, handle);
6935
6936out_unlock:
6937 if (data_ac)
6938 ocfs2_free_alloc_context(data_ac);
6939
6940out:
6941 if (pages) {
6942 ocfs2_unlock_and_free_pages(pages, num_pages);
6943 kfree(pages);
6944 }
6945
6946 return ret;
6947}
6948
Mark Fashehccd979b2005-12-15 14:31:24 -08006949/*
6950 * It is expected, that by the time you call this function,
6951 * inode->i_size and fe->i_size have been adjusted.
6952 *
6953 * WARNING: This will kfree the truncate context
6954 */
6955int ocfs2_commit_truncate(struct ocfs2_super *osb,
6956 struct inode *inode,
Tristan Ye78f94672010-05-11 17:54:42 +08006957 struct buffer_head *di_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08006958{
Tristan Ye78f94672010-05-11 17:54:42 +08006959 int status = 0, i, flags = 0;
6960 u32 new_highest_cpos, range, trunc_cpos, trunc_len, phys_cpos, coff;
Tao Mabcbbb242009-08-18 11:29:12 +08006961 u64 blkno = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08006962 struct ocfs2_extent_list *el;
Tristan Ye78f94672010-05-11 17:54:42 +08006963 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08006964 struct ocfs2_path *path = NULL;
Tristan Ye78f94672010-05-11 17:54:42 +08006965 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6966 struct ocfs2_extent_list *root_el = &(di->id2.i_list);
6967 u64 refcount_loc = le64_to_cpu(di->i_refcount_loc);
6968 struct ocfs2_extent_tree et;
6969 struct ocfs2_cached_dealloc_ctxt dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08006970
Tristan Ye78f94672010-05-11 17:54:42 +08006971 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
6972 ocfs2_init_dealloc_ctxt(&dealloc);
6973
Mark Fashehdcd05382007-01-16 11:32:23 -08006974 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08006975 i_size_read(inode));
6976
Tristan Ye78f94672010-05-11 17:54:42 +08006977 path = ocfs2_new_path(di_bh, &di->id2.i_list,
Joel Becker13723d02008-10-17 19:25:01 -07006978 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08006979 if (!path) {
6980 status = -ENOMEM;
6981 mlog_errno(status);
6982 goto bail;
6983 }
Mark Fasheh83418972007-04-23 18:53:12 -07006984
6985 ocfs2_extent_map_trunc(inode, new_highest_cpos);
6986
Mark Fashehccd979b2005-12-15 14:31:24 -08006987start:
Mark Fashehdcd05382007-01-16 11:32:23 -08006988 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006989 * Check that we still have allocation to delete.
6990 */
6991 if (OCFS2_I(inode)->ip_clusters == 0) {
6992 status = 0;
6993 goto bail;
6994 }
6995
6996 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08006997 * Truncate always works against the rightmost tree branch.
6998 */
Joel Beckerfacdb772009-02-12 18:08:48 -08006999 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
Mark Fashehdcd05382007-01-16 11:32:23 -08007000 if (status) {
7001 mlog_errno(status);
7002 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007003 }
7004
Mark Fashehdcd05382007-01-16 11:32:23 -08007005 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7006 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7007
7008 /*
7009 * By now, el will point to the extent list on the bottom most
7010 * portion of this tree. Only the tail record is considered in
7011 * each pass.
7012 *
7013 * We handle the following cases, in order:
7014 * - empty extent: delete the remaining branch
7015 * - remove the entire record
7016 * - remove a partial record
7017 * - no record needs to be removed (truncate has completed)
7018 */
7019 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007020 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7021 ocfs2_error(inode->i_sb,
7022 "Inode %llu has empty extent block at %llu\n",
7023 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7024 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7025 status = -EROFS;
7026 goto bail;
7027 }
7028
Mark Fashehccd979b2005-12-15 14:31:24 -08007029 i = le16_to_cpu(el->l_next_free_rec) - 1;
Tristan Ye78f94672010-05-11 17:54:42 +08007030 rec = &el->l_recs[i];
7031 flags = rec->e_flags;
7032 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
7033
7034 if (i == 0 && ocfs2_is_empty_extent(rec)) {
7035 /*
7036 * Lower levels depend on this never happening, but it's best
7037 * to check it up here before changing the tree.
7038 */
7039 if (root_el->l_tree_depth && rec->e_int_clusters == 0) {
7040 ocfs2_error(inode->i_sb, "Inode %lu has an empty "
7041 "extent record, depth %u\n", inode->i_ino,
7042 le16_to_cpu(root_el->l_tree_depth));
7043 status = -EROFS;
7044 goto bail;
7045 }
7046 trunc_cpos = le32_to_cpu(rec->e_cpos);
7047 trunc_len = 0;
7048 blkno = 0;
7049 } else if (le32_to_cpu(rec->e_cpos) >= new_highest_cpos) {
7050 /*
7051 * Truncate entire record.
7052 */
7053 trunc_cpos = le32_to_cpu(rec->e_cpos);
7054 trunc_len = ocfs2_rec_clusters(el, rec);
7055 blkno = le64_to_cpu(rec->e_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08007056 } else if (range > new_highest_cpos) {
Tristan Ye78f94672010-05-11 17:54:42 +08007057 /*
7058 * Partial truncate. it also should be
7059 * the last truncate we're doing.
7060 */
7061 trunc_cpos = new_highest_cpos;
7062 trunc_len = range - new_highest_cpos;
7063 coff = new_highest_cpos - le32_to_cpu(rec->e_cpos);
7064 blkno = le64_to_cpu(rec->e_blkno) +
7065 ocfs2_clusters_to_blocks(inode->i_sb, coff);
Mark Fashehdcd05382007-01-16 11:32:23 -08007066 } else {
Tristan Ye78f94672010-05-11 17:54:42 +08007067 /*
7068 * Truncate completed, leave happily.
7069 */
Mark Fashehdcd05382007-01-16 11:32:23 -08007070 status = 0;
7071 goto bail;
7072 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007073
Tristan Ye78f94672010-05-11 17:54:42 +08007074 phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08007075
Tristan Ye78f94672010-05-11 17:54:42 +08007076 status = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
7077 phys_cpos, trunc_len, flags, &dealloc,
7078 refcount_loc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007079 if (status < 0) {
7080 mlog_errno(status);
7081 goto bail;
7082 }
7083
Mark Fashehdcd05382007-01-16 11:32:23 -08007084 ocfs2_reinit_path(path, 1);
7085
7086 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007087 * The check above will catch the case where we've truncated
7088 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007089 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007090 goto start;
7091
Mark Fashehccd979b2005-12-15 14:31:24 -08007092bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007093
7094 ocfs2_schedule_truncate_log_flush(osb, 1);
7095
Tristan Ye78f94672010-05-11 17:54:42 +08007096 ocfs2_run_deallocs(osb, &dealloc);
Mark Fasheh59a5e412007-06-22 15:52:36 -07007097
Mark Fashehdcd05382007-01-16 11:32:23 -08007098 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007099
Mark Fashehccd979b2005-12-15 14:31:24 -08007100 return status;
7101}
7102
Mark Fashehccd979b2005-12-15 14:31:24 -08007103/*
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007104 * 'start' is inclusive, 'end' is not.
7105 */
7106int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7107 unsigned int start, unsigned int end, int trunc)
7108{
7109 int ret;
7110 unsigned int numbytes;
7111 handle_t *handle;
7112 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7113 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7114 struct ocfs2_inline_data *idata = &di->id2.i_data;
7115
7116 if (end > i_size_read(inode))
7117 end = i_size_read(inode);
7118
7119 BUG_ON(start >= end);
7120
7121 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7122 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7123 !ocfs2_supports_inline_data(osb)) {
7124 ocfs2_error(inode->i_sb,
7125 "Inline data flags for inode %llu don't agree! "
7126 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7127 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7128 le16_to_cpu(di->i_dyn_features),
7129 OCFS2_I(inode)->ip_dyn_features,
7130 osb->s_feature_incompat);
7131 ret = -EROFS;
7132 goto out;
7133 }
7134
7135 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7136 if (IS_ERR(handle)) {
7137 ret = PTR_ERR(handle);
7138 mlog_errno(ret);
7139 goto out;
7140 }
7141
Joel Becker0cf2f762009-02-12 16:41:25 -08007142 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007143 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007144 if (ret) {
7145 mlog_errno(ret);
7146 goto out_commit;
7147 }
7148
7149 numbytes = end - start;
7150 memset(idata->id_data + start, 0, numbytes);
7151
7152 /*
7153 * No need to worry about the data page here - it's been
7154 * truncated already and inline data doesn't need it for
7155 * pushing zero's to disk, so we'll let readpage pick it up
7156 * later.
7157 */
7158 if (trunc) {
7159 i_size_write(inode, start);
7160 di->i_size = cpu_to_le64(start);
7161 }
7162
7163 inode->i_blocks = ocfs2_inode_sector_count(inode);
7164 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7165
7166 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7167 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7168
7169 ocfs2_journal_dirty(handle, di_bh);
7170
7171out_commit:
7172 ocfs2_commit_trans(osb, handle);
7173
7174out:
7175 return ret;
7176}