blob: a6296560a5cb9c21db367a1bad7948406c1fbc1b [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"
Mark Fashehccd979b2005-12-15 14:31:24 -080052
53#include "buffer_head_io.h"
54
Tao Ma853a3a12009-08-18 11:22:18 +080055enum ocfs2_contig_type {
56 CONTIG_NONE = 0,
57 CONTIG_LEFT,
58 CONTIG_RIGHT,
59 CONTIG_LEFTRIGHT,
60};
Tao Mae7d4cb62008-08-18 17:38:44 +080061
Tao Ma853a3a12009-08-18 11:22:18 +080062static enum ocfs2_contig_type
63 ocfs2_extent_rec_contig(struct super_block *sb,
64 struct ocfs2_extent_rec *ext,
65 struct ocfs2_extent_rec *insert_rec);
Joel Becker1625f8a2008-08-21 17:11:10 -070066/*
67 * Operations for a specific extent tree type.
68 *
69 * To implement an on-disk btree (extent tree) type in ocfs2, add
70 * an ocfs2_extent_tree_operations structure and the matching
Joel Becker8d6220d2008-08-22 12:46:09 -070071 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
Joel Becker1625f8a2008-08-21 17:11:10 -070072 * for the allocation portion of the extent tree.
73 */
Tao Mae7d4cb62008-08-18 17:38:44 +080074struct ocfs2_extent_tree_operations {
Joel Becker1625f8a2008-08-21 17:11:10 -070075 /*
76 * last_eb_blk is the block number of the right most leaf extent
77 * block. Most on-disk structures containing an extent tree store
78 * this value for fast access. The ->eo_set_last_eb_blk() and
79 * ->eo_get_last_eb_blk() operations access this value. They are
80 * both required.
81 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070082 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
83 u64 blkno);
84 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -070085
86 /*
87 * The on-disk structure usually keeps track of how many total
88 * clusters are stored in this extent tree. This function updates
89 * that value. new_clusters is the delta, and must be
90 * added to the total. Required.
91 */
Joel Becker6136ca52009-02-12 19:32:43 -080092 void (*eo_update_clusters)(struct ocfs2_extent_tree *et,
Joel Becker35dc0aa2008-08-20 16:25:06 -070093 u32 new_clusters);
Joel Becker1625f8a2008-08-21 17:11:10 -070094
95 /*
Joel Becker92ba4702009-02-13 03:18:34 -080096 * If this extent tree is supported by an extent map, insert
97 * a record into the map.
98 */
99 void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et,
100 struct ocfs2_extent_rec *rec);
101
102 /*
Joel Becker4c911ee2009-02-13 02:50:12 -0800103 * If this extent tree is supported by an extent map, truncate the
104 * map to clusters,
105 */
106 void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et,
107 u32 clusters);
108
109 /*
Joel Becker1625f8a2008-08-21 17:11:10 -0700110 * If ->eo_insert_check() exists, it is called before rec is
111 * inserted into the extent tree. It is optional.
112 */
Joel Becker6136ca52009-02-12 19:32:43 -0800113 int (*eo_insert_check)(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700114 struct ocfs2_extent_rec *rec);
Joel Becker6136ca52009-02-12 19:32:43 -0800115 int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
Joel Becker0ce10102008-08-20 17:19:50 -0700116
Joel Becker1625f8a2008-08-21 17:11:10 -0700117 /*
118 * --------------------------------------------------------------
119 * The remaining are internal to ocfs2_extent_tree and don't have
120 * accessor functions
121 */
122
123 /*
124 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
125 * It is required.
126 */
Joel Becker0ce10102008-08-20 17:19:50 -0700127 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -0700128
129 /*
130 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
131 * it exists. If it does not, et->et_max_leaf_clusters is set
132 * to 0 (unlimited). Optional.
133 */
Joel Becker6136ca52009-02-12 19:32:43 -0800134 void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
Tao Ma853a3a12009-08-18 11:22:18 +0800135
136 /*
137 * ->eo_extent_contig test whether the 2 ocfs2_extent_rec
138 * are contiguous or not. Optional. Don't need to set it if use
139 * ocfs2_extent_rec as the tree leaf.
140 */
141 enum ocfs2_contig_type
142 (*eo_extent_contig)(struct ocfs2_extent_tree *et,
143 struct ocfs2_extent_rec *ext,
144 struct ocfs2_extent_rec *insert_rec);
Tao Mae7d4cb62008-08-18 17:38:44 +0800145};
146
Joel Beckerf99b9b72008-08-20 19:36:33 -0700147
148/*
149 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
150 * in the methods.
151 */
152static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
153static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
154 u64 blkno);
Joel Becker6136ca52009-02-12 19:32:43 -0800155static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700156 u32 clusters);
Joel Becker92ba4702009-02-13 03:18:34 -0800157static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
158 struct ocfs2_extent_rec *rec);
Joel Becker4c911ee2009-02-13 02:50:12 -0800159static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
160 u32 clusters);
Joel Becker6136ca52009-02-12 19:32:43 -0800161static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700162 struct ocfs2_extent_rec *rec);
Joel Becker6136ca52009-02-12 19:32:43 -0800163static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700164static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
165static struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
166 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
167 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
168 .eo_update_clusters = ocfs2_dinode_update_clusters,
Joel Becker92ba4702009-02-13 03:18:34 -0800169 .eo_extent_map_insert = ocfs2_dinode_extent_map_insert,
Joel Becker4c911ee2009-02-13 02:50:12 -0800170 .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700171 .eo_insert_check = ocfs2_dinode_insert_check,
172 .eo_sanity_check = ocfs2_dinode_sanity_check,
173 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
Tao Mae7d4cb62008-08-18 17:38:44 +0800174};
175
176static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
177 u64 blkno)
178{
Joel Beckerea5efa12008-08-20 16:57:27 -0700179 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800180
Joel Beckerf99b9b72008-08-20 19:36:33 -0700181 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800182 di->i_last_eb_blk = cpu_to_le64(blkno);
183}
184
185static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
186{
Joel Beckerea5efa12008-08-20 16:57:27 -0700187 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800188
Joel Beckerf99b9b72008-08-20 19:36:33 -0700189 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800190 return le64_to_cpu(di->i_last_eb_blk);
191}
192
Joel Becker6136ca52009-02-12 19:32:43 -0800193static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +0800194 u32 clusters)
195{
Joel Becker6136ca52009-02-12 19:32:43 -0800196 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
Joel Beckerea5efa12008-08-20 16:57:27 -0700197 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800198
199 le32_add_cpu(&di->i_clusters, clusters);
Joel Becker6136ca52009-02-12 19:32:43 -0800200 spin_lock(&oi->ip_lock);
201 oi->ip_clusters = le32_to_cpu(di->i_clusters);
202 spin_unlock(&oi->ip_lock);
Tao Mae7d4cb62008-08-18 17:38:44 +0800203}
204
Joel Becker92ba4702009-02-13 03:18:34 -0800205static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
206 struct ocfs2_extent_rec *rec)
207{
208 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
209
210 ocfs2_extent_map_insert_rec(inode, rec);
211}
212
Joel Becker4c911ee2009-02-13 02:50:12 -0800213static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
214 u32 clusters)
215{
216 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
217
218 ocfs2_extent_map_trunc(inode, clusters);
219}
220
Joel Becker6136ca52009-02-12 19:32:43 -0800221static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700222 struct ocfs2_extent_rec *rec)
223{
Joel Becker6136ca52009-02-12 19:32:43 -0800224 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
225 struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
Joel Becker1e61ee72008-08-20 18:32:45 -0700226
Joel Becker6136ca52009-02-12 19:32:43 -0800227 BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
Joel Becker1e61ee72008-08-20 18:32:45 -0700228 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
Joel Becker6136ca52009-02-12 19:32:43 -0800229 (oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
Joel Becker1e61ee72008-08-20 18:32:45 -0700230 "Device %s, asking for sparse allocation: inode %llu, "
231 "cpos %u, clusters %u\n",
232 osb->dev_str,
Joel Becker6136ca52009-02-12 19:32:43 -0800233 (unsigned long long)oi->ip_blkno,
234 rec->e_cpos, oi->ip_clusters);
Joel Becker1e61ee72008-08-20 18:32:45 -0700235
236 return 0;
237}
238
Joel Becker6136ca52009-02-12 19:32:43 -0800239static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800240{
Joel Becker10995aa2008-11-13 14:49:12 -0800241 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800242
Joel Beckerf99b9b72008-08-20 19:36:33 -0700243 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Joel Becker10995aa2008-11-13 14:49:12 -0800244 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
Tao Mae7d4cb62008-08-18 17:38:44 +0800245
Joel Becker10995aa2008-11-13 14:49:12 -0800246 return 0;
Tao Mae7d4cb62008-08-18 17:38:44 +0800247}
248
Joel Beckerf99b9b72008-08-20 19:36:33 -0700249static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
250{
251 struct ocfs2_dinode *di = et->et_object;
252
253 et->et_root_el = &di->id2.i_list;
254}
255
Tao Mae7d4cb62008-08-18 17:38:44 +0800256
Joel Becker0ce10102008-08-20 17:19:50 -0700257static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
258{
Joel Becker2a50a742008-12-09 14:24:33 -0800259 struct ocfs2_xattr_value_buf *vb = et->et_object;
Joel Becker0ce10102008-08-20 17:19:50 -0700260
Joel Becker2a50a742008-12-09 14:24:33 -0800261 et->et_root_el = &vb->vb_xv->xr_list;
Joel Becker0ce10102008-08-20 17:19:50 -0700262}
263
Tao Maf56654c2008-08-18 17:38:48 +0800264static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
265 u64 blkno)
266{
Joel Becker2a50a742008-12-09 14:24:33 -0800267 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800268
Joel Becker2a50a742008-12-09 14:24:33 -0800269 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
Tao Maf56654c2008-08-18 17:38:48 +0800270}
271
272static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
273{
Joel Becker2a50a742008-12-09 14:24:33 -0800274 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800275
Joel Becker2a50a742008-12-09 14:24:33 -0800276 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
Tao Maf56654c2008-08-18 17:38:48 +0800277}
278
Joel Becker6136ca52009-02-12 19:32:43 -0800279static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
Tao Maf56654c2008-08-18 17:38:48 +0800280 u32 clusters)
281{
Joel Becker2a50a742008-12-09 14:24:33 -0800282 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800283
Joel Becker2a50a742008-12-09 14:24:33 -0800284 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800285}
286
Joel Becker1a09f552008-08-20 17:44:24 -0700287static struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700288 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
289 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
290 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700291 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800292};
293
Joel Becker0ce10102008-08-20 17:19:50 -0700294static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
295{
296 struct ocfs2_xattr_block *xb = et->et_object;
297
298 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
299}
300
Joel Becker6136ca52009-02-12 19:32:43 -0800301static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
Joel Becker943cced2008-08-20 17:31:10 -0700302{
Joel Becker6136ca52009-02-12 19:32:43 -0800303 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Joel Becker943cced2008-08-20 17:31:10 -0700304 et->et_max_leaf_clusters =
Joel Becker6136ca52009-02-12 19:32:43 -0800305 ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
Joel Becker943cced2008-08-20 17:31:10 -0700306}
307
Tao Maba492612008-08-18 17:38:49 +0800308static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
309 u64 blkno)
310{
Joel Beckerea5efa12008-08-20 16:57:27 -0700311 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800312 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
313
314 xt->xt_last_eb_blk = cpu_to_le64(blkno);
315}
316
317static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
318{
Joel Beckerea5efa12008-08-20 16:57:27 -0700319 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800320 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
321
322 return le64_to_cpu(xt->xt_last_eb_blk);
323}
324
Joel Becker6136ca52009-02-12 19:32:43 -0800325static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
Tao Maba492612008-08-18 17:38:49 +0800326 u32 clusters)
327{
Joel Beckerea5efa12008-08-20 16:57:27 -0700328 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800329
330 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
331}
332
Tao Maba492612008-08-18 17:38:49 +0800333static struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700334 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
335 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
336 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700337 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
Joel Becker943cced2008-08-20 17:31:10 -0700338 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
Tao Maba492612008-08-18 17:38:49 +0800339};
340
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800341static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
342 u64 blkno)
343{
344 struct ocfs2_dx_root_block *dx_root = et->et_object;
345
346 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
347}
348
349static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
350{
351 struct ocfs2_dx_root_block *dx_root = et->et_object;
352
353 return le64_to_cpu(dx_root->dr_last_eb_blk);
354}
355
Joel Becker6136ca52009-02-12 19:32:43 -0800356static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800357 u32 clusters)
358{
359 struct ocfs2_dx_root_block *dx_root = et->et_object;
360
361 le32_add_cpu(&dx_root->dr_clusters, clusters);
362}
363
Joel Becker6136ca52009-02-12 19:32:43 -0800364static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800365{
366 struct ocfs2_dx_root_block *dx_root = et->et_object;
367
368 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
369
370 return 0;
371}
372
373static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
374{
375 struct ocfs2_dx_root_block *dx_root = et->et_object;
376
377 et->et_root_el = &dx_root->dr_list;
378}
379
380static struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
381 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
382 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
383 .eo_update_clusters = ocfs2_dx_root_update_clusters,
384 .eo_sanity_check = ocfs2_dx_root_sanity_check,
385 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
386};
387
Tao Mafe924412009-08-18 11:22:25 +0800388static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et)
389{
390 struct ocfs2_refcount_block *rb = et->et_object;
391
392 et->et_root_el = &rb->rf_list;
393}
394
395static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
396 u64 blkno)
397{
398 struct ocfs2_refcount_block *rb = et->et_object;
399
400 rb->rf_last_eb_blk = cpu_to_le64(blkno);
401}
402
403static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
404{
405 struct ocfs2_refcount_block *rb = et->et_object;
406
407 return le64_to_cpu(rb->rf_last_eb_blk);
408}
409
410static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et,
411 u32 clusters)
412{
413 struct ocfs2_refcount_block *rb = et->et_object;
414
415 le32_add_cpu(&rb->rf_clusters, clusters);
416}
417
418static enum ocfs2_contig_type
419ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et,
420 struct ocfs2_extent_rec *ext,
421 struct ocfs2_extent_rec *insert_rec)
422{
423 return CONTIG_NONE;
424}
425
426static struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = {
427 .eo_set_last_eb_blk = ocfs2_refcount_tree_set_last_eb_blk,
428 .eo_get_last_eb_blk = ocfs2_refcount_tree_get_last_eb_blk,
429 .eo_update_clusters = ocfs2_refcount_tree_update_clusters,
430 .eo_fill_root_el = ocfs2_refcount_tree_fill_root_el,
431 .eo_extent_contig = ocfs2_refcount_tree_extent_contig,
432};
433
Joel Becker8d6220d2008-08-22 12:46:09 -0700434static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800435 struct ocfs2_caching_info *ci,
Joel Becker8d6220d2008-08-22 12:46:09 -0700436 struct buffer_head *bh,
Joel Becker13723d02008-10-17 19:25:01 -0700437 ocfs2_journal_access_func access,
Joel Becker8d6220d2008-08-22 12:46:09 -0700438 void *obj,
439 struct ocfs2_extent_tree_operations *ops)
Tao Mae7d4cb62008-08-18 17:38:44 +0800440{
Joel Becker1a09f552008-08-20 17:44:24 -0700441 et->et_ops = ops;
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700442 et->et_root_bh = bh;
Joel Becker5e404e92009-02-13 03:54:22 -0800443 et->et_ci = ci;
Joel Becker13723d02008-10-17 19:25:01 -0700444 et->et_root_journal_access = access;
Joel Beckerea5efa12008-08-20 16:57:27 -0700445 if (!obj)
446 obj = (void *)bh->b_data;
447 et->et_object = obj;
Tao Mae7d4cb62008-08-18 17:38:44 +0800448
Joel Becker0ce10102008-08-20 17:19:50 -0700449 et->et_ops->eo_fill_root_el(et);
Joel Becker943cced2008-08-20 17:31:10 -0700450 if (!et->et_ops->eo_fill_max_leaf_clusters)
451 et->et_max_leaf_clusters = 0;
452 else
Joel Becker6136ca52009-02-12 19:32:43 -0800453 et->et_ops->eo_fill_max_leaf_clusters(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800454}
455
Joel Becker8d6220d2008-08-22 12:46:09 -0700456void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800457 struct ocfs2_caching_info *ci,
Joel Becker8d6220d2008-08-22 12:46:09 -0700458 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700459{
Joel Becker5e404e92009-02-13 03:54:22 -0800460 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di,
Joel Becker13723d02008-10-17 19:25:01 -0700461 NULL, &ocfs2_dinode_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700462}
463
Joel Becker8d6220d2008-08-22 12:46:09 -0700464void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800465 struct ocfs2_caching_info *ci,
Joel Becker8d6220d2008-08-22 12:46:09 -0700466 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700467{
Joel Becker5e404e92009-02-13 03:54:22 -0800468 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb,
Joel Becker13723d02008-10-17 19:25:01 -0700469 NULL, &ocfs2_xattr_tree_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700470}
471
Joel Becker8d6220d2008-08-22 12:46:09 -0700472void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800473 struct ocfs2_caching_info *ci,
Joel Becker2a50a742008-12-09 14:24:33 -0800474 struct ocfs2_xattr_value_buf *vb)
Tao Mae7d4cb62008-08-18 17:38:44 +0800475{
Joel Becker5e404e92009-02-13 03:54:22 -0800476 __ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb,
Joel Becker8d6220d2008-08-22 12:46:09 -0700477 &ocfs2_xattr_value_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800478}
479
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800480void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800481 struct ocfs2_caching_info *ci,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800482 struct buffer_head *bh)
483{
Joel Becker5e404e92009-02-13 03:54:22 -0800484 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800485 NULL, &ocfs2_dx_root_et_ops);
486}
487
Tao Mafe924412009-08-18 11:22:25 +0800488void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
489 struct ocfs2_caching_info *ci,
490 struct buffer_head *bh)
491{
492 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb,
493 NULL, &ocfs2_refcount_tree_et_ops);
494}
495
Joel Becker35dc0aa2008-08-20 16:25:06 -0700496static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
497 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800498{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700499 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800500}
501
Joel Becker35dc0aa2008-08-20 16:25:06 -0700502static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800503{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700504 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800505}
506
Joel Becker6136ca52009-02-12 19:32:43 -0800507static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
Joel Becker35dc0aa2008-08-20 16:25:06 -0700508 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800509{
Joel Becker6136ca52009-02-12 19:32:43 -0800510 et->et_ops->eo_update_clusters(et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700511}
512
Joel Becker92ba4702009-02-13 03:18:34 -0800513static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et,
514 struct ocfs2_extent_rec *rec)
515{
516 if (et->et_ops->eo_extent_map_insert)
517 et->et_ops->eo_extent_map_insert(et, rec);
518}
519
Joel Becker4c911ee2009-02-13 02:50:12 -0800520static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et,
521 u32 clusters)
522{
523 if (et->et_ops->eo_extent_map_truncate)
524 et->et_ops->eo_extent_map_truncate(et, clusters);
525}
526
Joel Becker13723d02008-10-17 19:25:01 -0700527static inline int ocfs2_et_root_journal_access(handle_t *handle,
Joel Becker13723d02008-10-17 19:25:01 -0700528 struct ocfs2_extent_tree *et,
529 int type)
530{
Joel Beckerd9a0a1f2009-02-12 17:32:34 -0800531 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700532 type);
533}
534
Tao Ma853a3a12009-08-18 11:22:18 +0800535static inline enum ocfs2_contig_type
536 ocfs2_et_extent_contig(struct ocfs2_extent_tree *et,
537 struct ocfs2_extent_rec *rec,
538 struct ocfs2_extent_rec *insert_rec)
539{
540 if (et->et_ops->eo_extent_contig)
541 return et->et_ops->eo_extent_contig(et, rec, insert_rec);
542
543 return ocfs2_extent_rec_contig(
544 ocfs2_metadata_cache_get_super(et->et_ci),
545 rec, insert_rec);
546}
547
Joel Becker6136ca52009-02-12 19:32:43 -0800548static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700549 struct ocfs2_extent_rec *rec)
550{
551 int ret = 0;
552
553 if (et->et_ops->eo_insert_check)
Joel Becker6136ca52009-02-12 19:32:43 -0800554 ret = et->et_ops->eo_insert_check(et, rec);
Joel Becker1e61ee72008-08-20 18:32:45 -0700555 return ret;
556}
557
Joel Becker6136ca52009-02-12 19:32:43 -0800558static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
Joel Becker35dc0aa2008-08-20 16:25:06 -0700559{
Joel Becker1e61ee72008-08-20 18:32:45 -0700560 int ret = 0;
561
562 if (et->et_ops->eo_sanity_check)
Joel Becker6136ca52009-02-12 19:32:43 -0800563 ret = et->et_ops->eo_sanity_check(et);
Joel Becker1e61ee72008-08-20 18:32:45 -0700564 return ret;
Tao Mae7d4cb62008-08-18 17:38:44 +0800565}
566
Mark Fashehccd979b2005-12-15 14:31:24 -0800567static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc);
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);
Mark Fashehccd979b2005-12-15 14:31:24 -0800570
Mark Fashehdcd05382007-01-16 11:32:23 -0800571/*
572 * Structures which describe a path through a btree, and functions to
573 * manipulate them.
574 *
575 * The idea here is to be as generic as possible with the tree
576 * manipulation code.
577 */
578struct ocfs2_path_item {
579 struct buffer_head *bh;
580 struct ocfs2_extent_list *el;
581};
582
583#define OCFS2_MAX_PATH_DEPTH 5
584
585struct ocfs2_path {
Joel Becker13723d02008-10-17 19:25:01 -0700586 int p_tree_depth;
587 ocfs2_journal_access_func p_root_access;
588 struct ocfs2_path_item p_node[OCFS2_MAX_PATH_DEPTH];
Mark Fashehdcd05382007-01-16 11:32:23 -0800589};
590
591#define path_root_bh(_path) ((_path)->p_node[0].bh)
592#define path_root_el(_path) ((_path)->p_node[0].el)
Joel Becker13723d02008-10-17 19:25:01 -0700593#define path_root_access(_path)((_path)->p_root_access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800594#define path_leaf_bh(_path) ((_path)->p_node[(_path)->p_tree_depth].bh)
595#define path_leaf_el(_path) ((_path)->p_node[(_path)->p_tree_depth].el)
596#define path_num_items(_path) ((_path)->p_tree_depth + 1)
597
Joel Beckerfacdb772009-02-12 18:08:48 -0800598static int ocfs2_find_path(struct ocfs2_caching_info *ci,
599 struct ocfs2_path *path, u32 cpos);
Joel Beckerd401dc12009-02-13 02:24:10 -0800600static void ocfs2_adjust_rightmost_records(handle_t *handle,
601 struct ocfs2_extent_tree *et,
Tao Ma6b791bc2009-06-12 14:18:36 +0800602 struct ocfs2_path *path,
603 struct ocfs2_extent_rec *insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -0800604/*
605 * Reset the actual path elements so that we can re-use the structure
606 * to build another path. Generally, this involves freeing the buffer
607 * heads.
608 */
609static void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
610{
611 int i, start = 0, depth = 0;
612 struct ocfs2_path_item *node;
613
614 if (keep_root)
615 start = 1;
616
617 for(i = start; i < path_num_items(path); i++) {
618 node = &path->p_node[i];
619
620 brelse(node->bh);
621 node->bh = NULL;
622 node->el = NULL;
623 }
624
625 /*
626 * Tree depth may change during truncate, or insert. If we're
627 * keeping the root extent list, then make sure that our path
628 * structure reflects the proper depth.
629 */
630 if (keep_root)
631 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
Joel Becker13723d02008-10-17 19:25:01 -0700632 else
633 path_root_access(path) = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -0800634
635 path->p_tree_depth = depth;
636}
637
638static void ocfs2_free_path(struct ocfs2_path *path)
639{
640 if (path) {
641 ocfs2_reinit_path(path, 0);
642 kfree(path);
643 }
644}
645
646/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700647 * All the elements of src into dest. After this call, src could be freed
648 * without affecting dest.
649 *
650 * Both paths should have the same root. Any non-root elements of dest
651 * will be freed.
652 */
653static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
654{
655 int i;
656
657 BUG_ON(path_root_bh(dest) != path_root_bh(src));
658 BUG_ON(path_root_el(dest) != path_root_el(src));
Joel Becker13723d02008-10-17 19:25:01 -0700659 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fasheh328d5752007-06-18 10:48:04 -0700660
661 ocfs2_reinit_path(dest, 1);
662
663 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
664 dest->p_node[i].bh = src->p_node[i].bh;
665 dest->p_node[i].el = src->p_node[i].el;
666
667 if (dest->p_node[i].bh)
668 get_bh(dest->p_node[i].bh);
669 }
670}
671
672/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800673 * Make the *dest path the same as src and re-initialize src path to
674 * have a root only.
675 */
676static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
677{
678 int i;
679
680 BUG_ON(path_root_bh(dest) != path_root_bh(src));
Joel Becker13723d02008-10-17 19:25:01 -0700681 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fashehdcd05382007-01-16 11:32:23 -0800682
683 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
684 brelse(dest->p_node[i].bh);
685
686 dest->p_node[i].bh = src->p_node[i].bh;
687 dest->p_node[i].el = src->p_node[i].el;
688
689 src->p_node[i].bh = NULL;
690 src->p_node[i].el = NULL;
691 }
692}
693
694/*
695 * Insert an extent block at given index.
696 *
697 * This will not take an additional reference on eb_bh.
698 */
699static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
700 struct buffer_head *eb_bh)
701{
702 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
703
704 /*
705 * Right now, no root bh is an extent block, so this helps
706 * catch code errors with dinode trees. The assertion can be
707 * safely removed if we ever need to insert extent block
708 * structures at the root.
709 */
710 BUG_ON(index == 0);
711
712 path->p_node[index].bh = eb_bh;
713 path->p_node[index].el = &eb->h_list;
714}
715
716static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700717 struct ocfs2_extent_list *root_el,
718 ocfs2_journal_access_func access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800719{
720 struct ocfs2_path *path;
721
722 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
723
724 path = kzalloc(sizeof(*path), GFP_NOFS);
725 if (path) {
726 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
727 get_bh(root_bh);
728 path_root_bh(path) = root_bh;
729 path_root_el(path) = root_el;
Joel Becker13723d02008-10-17 19:25:01 -0700730 path_root_access(path) = access;
Mark Fashehdcd05382007-01-16 11:32:23 -0800731 }
732
733 return path;
734}
735
Joel Beckerffdd7a52008-10-17 22:32:01 -0700736static struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
737{
Joel Becker13723d02008-10-17 19:25:01 -0700738 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
739 path_root_access(path));
Joel Beckerffdd7a52008-10-17 22:32:01 -0700740}
741
742static struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
743{
Joel Becker13723d02008-10-17 19:25:01 -0700744 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
745 et->et_root_journal_access);
746}
747
748/*
749 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
750 * otherwise it's the root_access function.
751 *
752 * I don't like the way this function's name looks next to
753 * ocfs2_journal_access_path(), but I don't have a better one.
754 */
755static int ocfs2_path_bh_journal_access(handle_t *handle,
Joel Becker0cf2f762009-02-12 16:41:25 -0800756 struct ocfs2_caching_info *ci,
Joel Becker13723d02008-10-17 19:25:01 -0700757 struct ocfs2_path *path,
758 int idx)
759{
760 ocfs2_journal_access_func access = path_root_access(path);
761
762 if (!access)
763 access = ocfs2_journal_access;
764
765 if (idx)
766 access = ocfs2_journal_access_eb;
767
Joel Becker0cf2f762009-02-12 16:41:25 -0800768 return access(handle, ci, path->p_node[idx].bh,
Joel Becker13723d02008-10-17 19:25:01 -0700769 OCFS2_JOURNAL_ACCESS_WRITE);
Joel Beckerffdd7a52008-10-17 22:32:01 -0700770}
771
Mark Fashehdcd05382007-01-16 11:32:23 -0800772/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800773 * Convenience function to journal all components in a path.
774 */
Joel Becker0cf2f762009-02-12 16:41:25 -0800775static int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
776 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -0800777 struct ocfs2_path *path)
778{
779 int i, ret = 0;
780
781 if (!path)
782 goto out;
783
784 for(i = 0; i < path_num_items(path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800785 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -0800786 if (ret < 0) {
787 mlog_errno(ret);
788 goto out;
789 }
790 }
791
792out:
793 return ret;
794}
795
Mark Fasheh328d5752007-06-18 10:48:04 -0700796/*
797 * Return the index of the extent record which contains cluster #v_cluster.
798 * -1 is returned if it was not found.
799 *
800 * Should work fine on interior and exterior nodes.
801 */
802int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
803{
804 int ret = -1;
805 int i;
806 struct ocfs2_extent_rec *rec;
807 u32 rec_end, rec_start, clusters;
808
809 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
810 rec = &el->l_recs[i];
811
812 rec_start = le32_to_cpu(rec->e_cpos);
813 clusters = ocfs2_rec_clusters(el, rec);
814
815 rec_end = rec_start + clusters;
816
817 if (v_cluster >= rec_start && v_cluster < rec_end) {
818 ret = i;
819 break;
820 }
821 }
822
823 return ret;
824}
825
Mark Fashehe48edee2007-03-07 16:46:57 -0800826/*
827 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
Tao Ma853a3a12009-08-18 11:22:18 +0800828 * ocfs2_extent_rec_contig only work properly against leaf nodes!
Mark Fashehe48edee2007-03-07 16:46:57 -0800829 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800830static int ocfs2_block_extent_contig(struct super_block *sb,
831 struct ocfs2_extent_rec *ext,
832 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800833{
Mark Fashehe48edee2007-03-07 16:46:57 -0800834 u64 blk_end = le64_to_cpu(ext->e_blkno);
835
836 blk_end += ocfs2_clusters_to_blocks(sb,
837 le16_to_cpu(ext->e_leaf_clusters));
838
839 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800840}
841
Mark Fashehdcd05382007-01-16 11:32:23 -0800842static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
843 struct ocfs2_extent_rec *right)
844{
Mark Fashehe48edee2007-03-07 16:46:57 -0800845 u32 left_range;
846
847 left_range = le32_to_cpu(left->e_cpos) +
848 le16_to_cpu(left->e_leaf_clusters);
849
850 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800851}
852
853static enum ocfs2_contig_type
Tao Ma853a3a12009-08-18 11:22:18 +0800854 ocfs2_extent_rec_contig(struct super_block *sb,
855 struct ocfs2_extent_rec *ext,
856 struct ocfs2_extent_rec *insert_rec)
Mark Fashehdcd05382007-01-16 11:32:23 -0800857{
858 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
859
Mark Fasheh328d5752007-06-18 10:48:04 -0700860 /*
861 * Refuse to coalesce extent records with different flag
862 * fields - we don't want to mix unwritten extents with user
863 * data.
864 */
865 if (ext->e_flags != insert_rec->e_flags)
866 return CONTIG_NONE;
867
Mark Fashehdcd05382007-01-16 11:32:23 -0800868 if (ocfs2_extents_adjacent(ext, insert_rec) &&
Joel Beckerb4a17652009-02-13 03:07:09 -0800869 ocfs2_block_extent_contig(sb, ext, blkno))
Mark Fashehdcd05382007-01-16 11:32:23 -0800870 return CONTIG_RIGHT;
871
872 blkno = le64_to_cpu(ext->e_blkno);
873 if (ocfs2_extents_adjacent(insert_rec, ext) &&
Joel Beckerb4a17652009-02-13 03:07:09 -0800874 ocfs2_block_extent_contig(sb, insert_rec, blkno))
Mark Fashehdcd05382007-01-16 11:32:23 -0800875 return CONTIG_LEFT;
876
877 return CONTIG_NONE;
878}
879
880/*
881 * NOTE: We can have pretty much any combination of contiguousness and
882 * appending.
883 *
884 * The usefulness of APPEND_TAIL is more in that it lets us know that
885 * we'll have to update the path to that leaf.
886 */
887enum ocfs2_append_type {
888 APPEND_NONE = 0,
889 APPEND_TAIL,
890};
891
Mark Fasheh328d5752007-06-18 10:48:04 -0700892enum ocfs2_split_type {
893 SPLIT_NONE = 0,
894 SPLIT_LEFT,
895 SPLIT_RIGHT,
896};
897
Mark Fashehdcd05382007-01-16 11:32:23 -0800898struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700899 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800900 enum ocfs2_append_type ins_appending;
901 enum ocfs2_contig_type ins_contig;
902 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800903 int ins_tree_depth;
904};
905
Mark Fasheh328d5752007-06-18 10:48:04 -0700906struct ocfs2_merge_ctxt {
907 enum ocfs2_contig_type c_contig_type;
908 int c_has_empty_extent;
909 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700910};
911
Joel Becker5e965812008-11-13 14:49:16 -0800912static int ocfs2_validate_extent_block(struct super_block *sb,
913 struct buffer_head *bh)
914{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700915 int rc;
Joel Becker5e965812008-11-13 14:49:16 -0800916 struct ocfs2_extent_block *eb =
917 (struct ocfs2_extent_block *)bh->b_data;
918
Joel Becker970e4932008-11-13 14:49:19 -0800919 mlog(0, "Validating extent block %llu\n",
920 (unsigned long long)bh->b_blocknr);
921
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700922 BUG_ON(!buffer_uptodate(bh));
923
924 /*
925 * If the ecc fails, we return the error but otherwise
926 * leave the filesystem running. We know any error is
927 * local to this block.
928 */
929 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
Joel Becker13723d02008-10-17 19:25:01 -0700930 if (rc) {
931 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
932 (unsigned long long)bh->b_blocknr);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700933 return rc;
Joel Becker13723d02008-10-17 19:25:01 -0700934 }
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700935
936 /*
937 * Errors after here are fatal.
938 */
939
Joel Becker5e965812008-11-13 14:49:16 -0800940 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
941 ocfs2_error(sb,
942 "Extent block #%llu has bad signature %.*s",
943 (unsigned long long)bh->b_blocknr, 7,
944 eb->h_signature);
945 return -EINVAL;
946 }
947
948 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
949 ocfs2_error(sb,
950 "Extent block #%llu has an invalid h_blkno "
951 "of %llu",
952 (unsigned long long)bh->b_blocknr,
953 (unsigned long long)le64_to_cpu(eb->h_blkno));
954 return -EINVAL;
955 }
956
957 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
958 ocfs2_error(sb,
959 "Extent block #%llu has an invalid "
960 "h_fs_generation of #%u",
961 (unsigned long long)bh->b_blocknr,
962 le32_to_cpu(eb->h_fs_generation));
963 return -EINVAL;
964 }
965
966 return 0;
967}
968
Joel Becker3d03a302009-02-12 17:49:26 -0800969int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
Joel Becker5e965812008-11-13 14:49:16 -0800970 struct buffer_head **bh)
971{
972 int rc;
973 struct buffer_head *tmp = *bh;
974
Joel Becker3d03a302009-02-12 17:49:26 -0800975 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800976 ocfs2_validate_extent_block);
Joel Becker5e965812008-11-13 14:49:16 -0800977
978 /* If ocfs2_read_block() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -0800979 if (!rc && !*bh)
Joel Becker5e965812008-11-13 14:49:16 -0800980 *bh = tmp;
981
Joel Becker5e965812008-11-13 14:49:16 -0800982 return rc;
983}
984
985
Mark Fashehccd979b2005-12-15 14:31:24 -0800986/*
987 * How many free extents have we got before we need more meta data?
988 */
989int ocfs2_num_free_extents(struct ocfs2_super *osb,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700990 struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -0800991{
992 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800993 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800994 struct ocfs2_extent_block *eb;
995 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800996 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800997
998 mlog_entry_void();
999
Joel Beckerf99b9b72008-08-20 19:36:33 -07001000 el = et->et_root_el;
1001 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
Mark Fashehccd979b2005-12-15 14:31:24 -08001002
Tao Mae7d4cb62008-08-18 17:38:44 +08001003 if (last_eb_blk) {
Joel Becker3d03a302009-02-12 17:49:26 -08001004 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
1005 &eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001006 if (retval < 0) {
1007 mlog_errno(retval);
1008 goto bail;
1009 }
1010 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1011 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +08001012 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001013
1014 BUG_ON(el->l_tree_depth != 0);
1015
1016 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
1017bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001018 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001019
1020 mlog_exit(retval);
1021 return retval;
1022}
1023
1024/* expects array to already be allocated
1025 *
1026 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
1027 * l_count for you
1028 */
Joel Becker42a5a7a2009-02-12 18:49:19 -08001029static int ocfs2_create_new_meta_bhs(handle_t *handle,
1030 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001031 int wanted,
1032 struct ocfs2_alloc_context *meta_ac,
1033 struct buffer_head *bhs[])
1034{
1035 int count, status, i;
1036 u16 suballoc_bit_start;
1037 u32 num_got;
1038 u64 first_blkno;
Joel Becker42a5a7a2009-02-12 18:49:19 -08001039 struct ocfs2_super *osb =
1040 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001041 struct ocfs2_extent_block *eb;
1042
1043 mlog_entry_void();
1044
1045 count = 0;
1046 while (count < wanted) {
1047 status = ocfs2_claim_metadata(osb,
1048 handle,
1049 meta_ac,
1050 wanted - count,
1051 &suballoc_bit_start,
1052 &num_got,
1053 &first_blkno);
1054 if (status < 0) {
1055 mlog_errno(status);
1056 goto bail;
1057 }
1058
1059 for(i = count; i < (num_got + count); i++) {
1060 bhs[i] = sb_getblk(osb->sb, first_blkno);
1061 if (bhs[i] == NULL) {
1062 status = -EIO;
1063 mlog_errno(status);
1064 goto bail;
1065 }
Joel Becker42a5a7a2009-02-12 18:49:19 -08001066 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001067
Joel Becker42a5a7a2009-02-12 18:49:19 -08001068 status = ocfs2_journal_access_eb(handle, et->et_ci,
1069 bhs[i],
Joel Becker13723d02008-10-17 19:25:01 -07001070 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001071 if (status < 0) {
1072 mlog_errno(status);
1073 goto bail;
1074 }
1075
1076 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
1077 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
1078 /* Ok, setup the minimal stuff here. */
1079 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
1080 eb->h_blkno = cpu_to_le64(first_blkno);
1081 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Mark Fashehccd979b2005-12-15 14:31:24 -08001082 eb->h_suballoc_slot = cpu_to_le16(osb->slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08001083 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1084 eb->h_list.l_count =
1085 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1086
1087 suballoc_bit_start++;
1088 first_blkno++;
1089
1090 /* We'll also be dirtied by the caller, so
1091 * this isn't absolutely necessary. */
1092 status = ocfs2_journal_dirty(handle, bhs[i]);
1093 if (status < 0) {
1094 mlog_errno(status);
1095 goto bail;
1096 }
1097 }
1098
1099 count += num_got;
1100 }
1101
1102 status = 0;
1103bail:
1104 if (status < 0) {
1105 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001106 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001107 bhs[i] = NULL;
1108 }
1109 }
1110 mlog_exit(status);
1111 return status;
1112}
1113
1114/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001115 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1116 *
1117 * Returns the sum of the rightmost extent rec logical offset and
1118 * cluster count.
1119 *
1120 * ocfs2_add_branch() uses this to determine what logical cluster
1121 * value should be populated into the leftmost new branch records.
1122 *
1123 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1124 * value for the new topmost tree record.
1125 */
1126static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1127{
1128 int i;
1129
1130 i = le16_to_cpu(el->l_next_free_rec) - 1;
1131
1132 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001133 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08001134}
1135
1136/*
Tao Ma6b791bc2009-06-12 14:18:36 +08001137 * Change range of the branches in the right most path according to the leaf
1138 * extent block's rightmost record.
1139 */
1140static int ocfs2_adjust_rightmost_branch(handle_t *handle,
Tao Ma6b791bc2009-06-12 14:18:36 +08001141 struct ocfs2_extent_tree *et)
1142{
1143 int status;
1144 struct ocfs2_path *path = NULL;
1145 struct ocfs2_extent_list *el;
1146 struct ocfs2_extent_rec *rec;
1147
1148 path = ocfs2_new_path_from_et(et);
1149 if (!path) {
1150 status = -ENOMEM;
1151 return status;
1152 }
1153
Joel Beckerfacdb772009-02-12 18:08:48 -08001154 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
Tao Ma6b791bc2009-06-12 14:18:36 +08001155 if (status < 0) {
1156 mlog_errno(status);
1157 goto out;
1158 }
1159
1160 status = ocfs2_extend_trans(handle, path_num_items(path) +
1161 handle->h_buffer_credits);
1162 if (status < 0) {
1163 mlog_errno(status);
1164 goto out;
1165 }
1166
Joel Beckerd401dc12009-02-13 02:24:10 -08001167 status = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma6b791bc2009-06-12 14:18:36 +08001168 if (status < 0) {
1169 mlog_errno(status);
1170 goto out;
1171 }
1172
1173 el = path_leaf_el(path);
1174 rec = &el->l_recs[le32_to_cpu(el->l_next_free_rec) - 1];
1175
Joel Beckerd401dc12009-02-13 02:24:10 -08001176 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Tao Ma6b791bc2009-06-12 14:18:36 +08001177
1178out:
1179 ocfs2_free_path(path);
1180 return status;
1181}
1182
1183/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001184 * Add an entire tree branch to our inode. eb_bh is the extent block
Joel Beckerd401dc12009-02-13 02:24:10 -08001185 * to start at, if we don't want to start the branch at the root
Mark Fashehccd979b2005-12-15 14:31:24 -08001186 * structure.
1187 *
1188 * last_eb_bh is required as we have to update it's next_leaf pointer
1189 * for the new last extent block.
1190 *
1191 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -08001192 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -08001193 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001194static int ocfs2_add_branch(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001195 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001196 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07001197 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001198 struct ocfs2_alloc_context *meta_ac)
1199{
1200 int status, new_blocks, i;
1201 u64 next_blkno, new_last_eb_blk;
1202 struct buffer_head *bh;
1203 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001204 struct ocfs2_extent_block *eb;
1205 struct ocfs2_extent_list *eb_el;
1206 struct ocfs2_extent_list *el;
Tao Ma6b791bc2009-06-12 14:18:36 +08001207 u32 new_cpos, root_end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001208
1209 mlog_entry_void();
1210
Mark Fasheh328d5752007-06-18 10:48:04 -07001211 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001212
Mark Fashehccd979b2005-12-15 14:31:24 -08001213 if (eb_bh) {
1214 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1215 el = &eb->h_list;
1216 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001217 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001218
1219 /* we never add a branch to a leaf. */
1220 BUG_ON(!el->l_tree_depth);
1221
1222 new_blocks = le16_to_cpu(el->l_tree_depth);
1223
Tao Ma6b791bc2009-06-12 14:18:36 +08001224 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1225 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1226 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1227
1228 /*
1229 * If there is a gap before the root end and the real end
1230 * of the righmost leaf block, we need to remove the gap
1231 * between new_cpos and root_end first so that the tree
1232 * is consistent after we add a new branch(it will start
1233 * from new_cpos).
1234 */
1235 if (root_end > new_cpos) {
1236 mlog(0, "adjust the cluster end from %u to %u\n",
1237 root_end, new_cpos);
Joel Beckerd401dc12009-02-13 02:24:10 -08001238 status = ocfs2_adjust_rightmost_branch(handle, et);
Tao Ma6b791bc2009-06-12 14:18:36 +08001239 if (status) {
1240 mlog_errno(status);
1241 goto bail;
1242 }
1243 }
1244
Mark Fashehccd979b2005-12-15 14:31:24 -08001245 /* allocate the number of new eb blocks we need */
1246 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1247 GFP_KERNEL);
1248 if (!new_eb_bhs) {
1249 status = -ENOMEM;
1250 mlog_errno(status);
1251 goto bail;
1252 }
1253
Joel Becker42a5a7a2009-02-12 18:49:19 -08001254 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
Mark Fashehccd979b2005-12-15 14:31:24 -08001255 meta_ac, new_eb_bhs);
1256 if (status < 0) {
1257 mlog_errno(status);
1258 goto bail;
1259 }
1260
1261 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1262 * linked with the rest of the tree.
1263 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1264 *
1265 * when we leave the loop, new_last_eb_blk will point to the
1266 * newest leaf, and next_blkno will point to the topmost extent
1267 * block. */
1268 next_blkno = new_last_eb_blk = 0;
1269 for(i = 0; i < new_blocks; i++) {
1270 bh = new_eb_bhs[i];
1271 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001272 /* ocfs2_create_new_meta_bhs() should create it right! */
1273 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001274 eb_el = &eb->h_list;
1275
Joel Beckerd401dc12009-02-13 02:24:10 -08001276 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
Joel Becker13723d02008-10-17 19:25:01 -07001277 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001278 if (status < 0) {
1279 mlog_errno(status);
1280 goto bail;
1281 }
1282
1283 eb->h_next_leaf_blk = 0;
1284 eb_el->l_tree_depth = cpu_to_le16(i);
1285 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001286 /*
1287 * This actually counts as an empty extent as
1288 * c_clusters == 0
1289 */
1290 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001291 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001292 /*
1293 * eb_el isn't always an interior node, but even leaf
1294 * nodes want a zero'd flags and reserved field so
1295 * this gets the whole 32 bits regardless of use.
1296 */
1297 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001298 if (!eb_el->l_tree_depth)
1299 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1300
1301 status = ocfs2_journal_dirty(handle, bh);
1302 if (status < 0) {
1303 mlog_errno(status);
1304 goto bail;
1305 }
1306
1307 next_blkno = le64_to_cpu(eb->h_blkno);
1308 }
1309
1310 /* This is a bit hairy. We want to update up to three blocks
1311 * here without leaving any of them in an inconsistent state
1312 * in case of error. We don't have to worry about
1313 * journal_dirty erroring as it won't unless we've aborted the
1314 * handle (in which case we would never be here) so reserving
1315 * the write with journal_access is all we need to do. */
Joel Beckerd401dc12009-02-13 02:24:10 -08001316 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001317 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001318 if (status < 0) {
1319 mlog_errno(status);
1320 goto bail;
1321 }
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001322 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001323 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001324 if (status < 0) {
1325 mlog_errno(status);
1326 goto bail;
1327 }
1328 if (eb_bh) {
Joel Beckerd401dc12009-02-13 02:24:10 -08001329 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001330 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001331 if (status < 0) {
1332 mlog_errno(status);
1333 goto bail;
1334 }
1335 }
1336
1337 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001338 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001339 i = le16_to_cpu(el->l_next_free_rec);
1340 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001341 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001342 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001343 le16_add_cpu(&el->l_next_free_rec, 1);
1344
1345 /* fe needs a new last extent block pointer, as does the
1346 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001347 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001348
Mark Fasheh328d5752007-06-18 10:48:04 -07001349 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001350 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1351
Mark Fasheh328d5752007-06-18 10:48:04 -07001352 status = ocfs2_journal_dirty(handle, *last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001353 if (status < 0)
1354 mlog_errno(status);
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001355 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001356 if (status < 0)
1357 mlog_errno(status);
1358 if (eb_bh) {
1359 status = ocfs2_journal_dirty(handle, eb_bh);
1360 if (status < 0)
1361 mlog_errno(status);
1362 }
1363
Mark Fasheh328d5752007-06-18 10:48:04 -07001364 /*
1365 * Some callers want to track the rightmost leaf so pass it
1366 * back here.
1367 */
1368 brelse(*last_eb_bh);
1369 get_bh(new_eb_bhs[0]);
1370 *last_eb_bh = new_eb_bhs[0];
1371
Mark Fashehccd979b2005-12-15 14:31:24 -08001372 status = 0;
1373bail:
1374 if (new_eb_bhs) {
1375 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001376 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001377 kfree(new_eb_bhs);
1378 }
1379
1380 mlog_exit(status);
1381 return status;
1382}
1383
1384/*
1385 * adds another level to the allocation tree.
1386 * returns back the new extent block so you can add a branch to it
1387 * after this call.
1388 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001389static int ocfs2_shift_tree_depth(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001390 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001391 struct ocfs2_alloc_context *meta_ac,
1392 struct buffer_head **ret_new_eb_bh)
1393{
1394 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001395 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001396 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001397 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001398 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001399 struct ocfs2_extent_list *eb_el;
1400
1401 mlog_entry_void();
1402
Joel Becker42a5a7a2009-02-12 18:49:19 -08001403 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
Mark Fashehccd979b2005-12-15 14:31:24 -08001404 &new_eb_bh);
1405 if (status < 0) {
1406 mlog_errno(status);
1407 goto bail;
1408 }
1409
1410 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001411 /* ocfs2_create_new_meta_bhs() should create it right! */
1412 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001413
1414 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001415 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001416
Joel Beckerd401dc12009-02-13 02:24:10 -08001417 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001418 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001419 if (status < 0) {
1420 mlog_errno(status);
1421 goto bail;
1422 }
1423
Tao Mae7d4cb62008-08-18 17:38:44 +08001424 /* copy the root extent list data into the new extent block */
1425 eb_el->l_tree_depth = root_el->l_tree_depth;
1426 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1427 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1428 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001429
1430 status = ocfs2_journal_dirty(handle, new_eb_bh);
1431 if (status < 0) {
1432 mlog_errno(status);
1433 goto bail;
1434 }
1435
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001436 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001437 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001438 if (status < 0) {
1439 mlog_errno(status);
1440 goto bail;
1441 }
1442
Mark Fashehdcd05382007-01-16 11:32:23 -08001443 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1444
Tao Mae7d4cb62008-08-18 17:38:44 +08001445 /* update root_bh now */
1446 le16_add_cpu(&root_el->l_tree_depth, 1);
1447 root_el->l_recs[0].e_cpos = 0;
1448 root_el->l_recs[0].e_blkno = eb->h_blkno;
1449 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1450 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1451 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1452 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001453
1454 /* If this is our 1st tree depth shift, then last_eb_blk
1455 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001456 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001457 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001458
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001459 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001460 if (status < 0) {
1461 mlog_errno(status);
1462 goto bail;
1463 }
1464
1465 *ret_new_eb_bh = new_eb_bh;
1466 new_eb_bh = NULL;
1467 status = 0;
1468bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001469 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001470
1471 mlog_exit(status);
1472 return status;
1473}
1474
1475/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001476 * Should only be called when there is no space left in any of the
1477 * leaf nodes. What we want to do is find the lowest tree depth
1478 * non-leaf extent block with room for new records. There are three
1479 * valid results of this search:
1480 *
1481 * 1) a lowest extent block is found, then we pass it back in
1482 * *lowest_eb_bh and return '0'
1483 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001484 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001485 * pass NULL back in *lowest_eb_bh, but still return '0'
1486 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001487 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001488 * which case we return > 0
1489 *
1490 * return status < 0 indicates an error.
1491 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001492static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001493 struct buffer_head **target_bh)
1494{
1495 int status = 0, i;
1496 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001497 struct ocfs2_extent_block *eb;
1498 struct ocfs2_extent_list *el;
1499 struct buffer_head *bh = NULL;
1500 struct buffer_head *lowest_bh = NULL;
1501
1502 mlog_entry_void();
1503
1504 *target_bh = NULL;
1505
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001506 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001507
1508 while(le16_to_cpu(el->l_tree_depth) > 1) {
1509 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08001510 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1511 "Owner %llu has empty "
Mark Fashehccd979b2005-12-15 14:31:24 -08001512 "extent list (next_free_rec == 0)",
Joel Becker3d03a302009-02-12 17:49:26 -08001513 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001514 status = -EIO;
1515 goto bail;
1516 }
1517 i = le16_to_cpu(el->l_next_free_rec) - 1;
1518 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1519 if (!blkno) {
Joel Becker3d03a302009-02-12 17:49:26 -08001520 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
1521 "Owner %llu has extent "
Mark Fashehccd979b2005-12-15 14:31:24 -08001522 "list where extent # %d has no physical "
1523 "block start",
Joel Becker3d03a302009-02-12 17:49:26 -08001524 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001525 status = -EIO;
1526 goto bail;
1527 }
1528
Mark Fasheha81cb882008-10-07 14:25:16 -07001529 brelse(bh);
1530 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001531
Joel Becker3d03a302009-02-12 17:49:26 -08001532 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001533 if (status < 0) {
1534 mlog_errno(status);
1535 goto bail;
1536 }
1537
1538 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001539 el = &eb->h_list;
1540
1541 if (le16_to_cpu(el->l_next_free_rec) <
1542 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001543 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001544 lowest_bh = bh;
1545 get_bh(lowest_bh);
1546 }
1547 }
1548
1549 /* If we didn't find one and the fe doesn't have any room,
1550 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001551 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001552 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001553 status = 1;
1554
1555 *target_bh = lowest_bh;
1556bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001557 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001558
1559 mlog_exit(status);
1560 return status;
1561}
1562
Mark Fashehe48edee2007-03-07 16:46:57 -08001563/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001564 * Grow a b-tree so that it has more records.
1565 *
1566 * We might shift the tree depth in which case existing paths should
1567 * be considered invalid.
1568 *
1569 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001570 *
1571 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001572 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001573static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1574 int *final_depth, struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001575 struct ocfs2_alloc_context *meta_ac)
1576{
1577 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001578 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001579 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001580 struct buffer_head *bh = NULL;
1581
1582 BUG_ON(meta_ac == NULL);
1583
Joel Beckerd401dc12009-02-13 02:24:10 -08001584 shift = ocfs2_find_branch_target(et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001585 if (shift < 0) {
1586 ret = shift;
1587 mlog_errno(ret);
1588 goto out;
1589 }
1590
1591 /* We traveled all the way to the bottom of the allocation tree
1592 * and didn't find room for any more extents - we need to add
1593 * another tree level */
1594 if (shift) {
1595 BUG_ON(bh);
1596 mlog(0, "need to shift tree depth (current = %d)\n", depth);
1597
1598 /* ocfs2_shift_tree_depth will return us a buffer with
1599 * the new extent block (so we can pass that to
1600 * ocfs2_add_branch). */
Joel Beckerd401dc12009-02-13 02:24:10 -08001601 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001602 if (ret < 0) {
1603 mlog_errno(ret);
1604 goto out;
1605 }
1606 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001607 if (depth == 1) {
1608 /*
1609 * Special case: we have room now if we shifted from
1610 * tree_depth 0, so no more work needs to be done.
1611 *
1612 * We won't be calling add_branch, so pass
1613 * back *last_eb_bh as the new leaf. At depth
1614 * zero, it should always be null so there's
1615 * no reason to brelse.
1616 */
1617 BUG_ON(*last_eb_bh);
1618 get_bh(bh);
1619 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001620 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001621 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001622 }
1623
1624 /* call ocfs2_add_branch to add the final part of the tree with
1625 * the new data. */
1626 mlog(0, "add branch. bh = %p\n", bh);
Joel Beckerd401dc12009-02-13 02:24:10 -08001627 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001628 meta_ac);
1629 if (ret < 0) {
1630 mlog_errno(ret);
1631 goto out;
1632 }
1633
1634out:
1635 if (final_depth)
1636 *final_depth = depth;
1637 brelse(bh);
1638 return ret;
1639}
1640
1641/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001642 * This function will discard the rightmost extent record.
1643 */
1644static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1645{
1646 int next_free = le16_to_cpu(el->l_next_free_rec);
1647 int count = le16_to_cpu(el->l_count);
1648 unsigned int num_bytes;
1649
1650 BUG_ON(!next_free);
1651 /* This will cause us to go off the end of our extent list. */
1652 BUG_ON(next_free >= count);
1653
1654 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1655
1656 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1657}
1658
1659static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1660 struct ocfs2_extent_rec *insert_rec)
1661{
1662 int i, insert_index, next_free, has_empty, num_bytes;
1663 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1664 struct ocfs2_extent_rec *rec;
1665
1666 next_free = le16_to_cpu(el->l_next_free_rec);
1667 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1668
1669 BUG_ON(!next_free);
1670
1671 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001672 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001673
1674 /*
1675 * The easiest way to approach this is to just remove the
1676 * empty extent and temporarily decrement next_free.
1677 */
1678 if (has_empty) {
1679 /*
1680 * If next_free was 1 (only an empty extent), this
1681 * loop won't execute, which is fine. We still want
1682 * the decrement above to happen.
1683 */
1684 for(i = 0; i < (next_free - 1); i++)
1685 el->l_recs[i] = el->l_recs[i+1];
1686
1687 next_free--;
1688 }
1689
1690 /*
1691 * Figure out what the new record index should be.
1692 */
1693 for(i = 0; i < next_free; i++) {
1694 rec = &el->l_recs[i];
1695
1696 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1697 break;
1698 }
1699 insert_index = i;
1700
1701 mlog(0, "ins %u: index %d, has_empty %d, next_free %d, count %d\n",
1702 insert_cpos, insert_index, has_empty, next_free, le16_to_cpu(el->l_count));
1703
1704 BUG_ON(insert_index < 0);
1705 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1706 BUG_ON(insert_index > next_free);
1707
1708 /*
1709 * No need to memmove if we're just adding to the tail.
1710 */
1711 if (insert_index != next_free) {
1712 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1713
1714 num_bytes = next_free - insert_index;
1715 num_bytes *= sizeof(struct ocfs2_extent_rec);
1716 memmove(&el->l_recs[insert_index + 1],
1717 &el->l_recs[insert_index],
1718 num_bytes);
1719 }
1720
1721 /*
1722 * Either we had an empty extent, and need to re-increment or
1723 * there was no empty extent on a non full rightmost leaf node,
1724 * in which case we still need to increment.
1725 */
1726 next_free++;
1727 el->l_next_free_rec = cpu_to_le16(next_free);
1728 /*
1729 * Make sure none of the math above just messed up our tree.
1730 */
1731 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1732
1733 el->l_recs[insert_index] = *insert_rec;
1734
1735}
1736
Mark Fasheh328d5752007-06-18 10:48:04 -07001737static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1738{
1739 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1740
1741 BUG_ON(num_recs == 0);
1742
1743 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1744 num_recs--;
1745 size = num_recs * sizeof(struct ocfs2_extent_rec);
1746 memmove(&el->l_recs[0], &el->l_recs[1], size);
1747 memset(&el->l_recs[num_recs], 0,
1748 sizeof(struct ocfs2_extent_rec));
1749 el->l_next_free_rec = cpu_to_le16(num_recs);
1750 }
1751}
1752
Mark Fashehdcd05382007-01-16 11:32:23 -08001753/*
1754 * Create an empty extent record .
1755 *
1756 * l_next_free_rec may be updated.
1757 *
1758 * If an empty extent already exists do nothing.
1759 */
1760static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1761{
1762 int next_free = le16_to_cpu(el->l_next_free_rec);
1763
Mark Fashehe48edee2007-03-07 16:46:57 -08001764 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1765
Mark Fashehdcd05382007-01-16 11:32:23 -08001766 if (next_free == 0)
1767 goto set_and_inc;
1768
1769 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1770 return;
1771
1772 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1773 "Asked to create an empty extent in a full list:\n"
1774 "count = %u, tree depth = %u",
1775 le16_to_cpu(el->l_count),
1776 le16_to_cpu(el->l_tree_depth));
1777
1778 ocfs2_shift_records_right(el);
1779
1780set_and_inc:
1781 le16_add_cpu(&el->l_next_free_rec, 1);
1782 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1783}
1784
1785/*
1786 * For a rotation which involves two leaf nodes, the "root node" is
1787 * the lowest level tree node which contains a path to both leafs. This
1788 * resulting set of information can be used to form a complete "subtree"
1789 *
1790 * This function is passed two full paths from the dinode down to a
1791 * pair of adjacent leaves. It's task is to figure out which path
1792 * index contains the subtree root - this can be the root index itself
1793 * in a worst-case rotation.
1794 *
1795 * The array index of the subtree root is passed back.
1796 */
Joel Becker7dc02802009-02-12 19:20:13 -08001797static int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08001798 struct ocfs2_path *left,
1799 struct ocfs2_path *right)
1800{
1801 int i = 0;
1802
1803 /*
1804 * Check that the caller passed in two paths from the same tree.
1805 */
1806 BUG_ON(path_root_bh(left) != path_root_bh(right));
1807
1808 do {
1809 i++;
1810
1811 /*
1812 * The caller didn't pass two adjacent paths.
1813 */
1814 mlog_bug_on_msg(i > left->p_tree_depth,
Joel Becker7dc02802009-02-12 19:20:13 -08001815 "Owner %llu, left depth %u, right depth %u\n"
Mark Fashehdcd05382007-01-16 11:32:23 -08001816 "left leaf blk %llu, right leaf blk %llu\n",
Joel Becker7dc02802009-02-12 19:20:13 -08001817 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1818 left->p_tree_depth, right->p_tree_depth,
Mark Fashehdcd05382007-01-16 11:32:23 -08001819 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1820 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1821 } while (left->p_node[i].bh->b_blocknr ==
1822 right->p_node[i].bh->b_blocknr);
1823
1824 return i - 1;
1825}
1826
1827typedef void (path_insert_t)(void *, struct buffer_head *);
1828
1829/*
1830 * Traverse a btree path in search of cpos, starting at root_el.
1831 *
1832 * This code can be called with a cpos larger than the tree, in which
1833 * case it will return the rightmost path.
1834 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001835static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
Mark Fashehdcd05382007-01-16 11:32:23 -08001836 struct ocfs2_extent_list *root_el, u32 cpos,
1837 path_insert_t *func, void *data)
1838{
1839 int i, ret = 0;
1840 u32 range;
1841 u64 blkno;
1842 struct buffer_head *bh = NULL;
1843 struct ocfs2_extent_block *eb;
1844 struct ocfs2_extent_list *el;
1845 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08001846
1847 el = root_el;
1848 while (el->l_tree_depth) {
1849 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001850 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1851 "Owner %llu has empty extent list at "
Mark Fashehdcd05382007-01-16 11:32:23 -08001852 "depth %u\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001853 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001854 le16_to_cpu(el->l_tree_depth));
1855 ret = -EROFS;
1856 goto out;
1857
1858 }
1859
1860 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1861 rec = &el->l_recs[i];
1862
1863 /*
1864 * In the case that cpos is off the allocation
1865 * tree, this should just wind up returning the
1866 * rightmost record.
1867 */
1868 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001869 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001870 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1871 break;
1872 }
1873
1874 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1875 if (blkno == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001876 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1877 "Owner %llu has bad blkno in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001878 "at depth %u (index %d)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001879 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001880 le16_to_cpu(el->l_tree_depth), i);
1881 ret = -EROFS;
1882 goto out;
1883 }
1884
1885 brelse(bh);
1886 bh = NULL;
Joel Beckerfacdb772009-02-12 18:08:48 -08001887 ret = ocfs2_read_extent_block(ci, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001888 if (ret) {
1889 mlog_errno(ret);
1890 goto out;
1891 }
1892
1893 eb = (struct ocfs2_extent_block *) bh->b_data;
1894 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001895
1896 if (le16_to_cpu(el->l_next_free_rec) >
1897 le16_to_cpu(el->l_count)) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001898 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
1899 "Owner %llu has bad count in extent list "
Mark Fashehdcd05382007-01-16 11:32:23 -08001900 "at block %llu (next free=%u, count=%u)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001901 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001902 (unsigned long long)bh->b_blocknr,
1903 le16_to_cpu(el->l_next_free_rec),
1904 le16_to_cpu(el->l_count));
1905 ret = -EROFS;
1906 goto out;
1907 }
1908
1909 if (func)
1910 func(data, bh);
1911 }
1912
1913out:
1914 /*
1915 * Catch any trailing bh that the loop didn't handle.
1916 */
1917 brelse(bh);
1918
1919 return ret;
1920}
1921
1922/*
1923 * Given an initialized path (that is, it has a valid root extent
1924 * list), this function will traverse the btree in search of the path
1925 * which would contain cpos.
1926 *
1927 * The path traveled is recorded in the path structure.
1928 *
1929 * Note that this will not do any comparisons on leaf node extent
1930 * records, so it will work fine in the case that we just added a tree
1931 * branch.
1932 */
1933struct find_path_data {
1934 int index;
1935 struct ocfs2_path *path;
1936};
1937static void find_path_ins(void *data, struct buffer_head *bh)
1938{
1939 struct find_path_data *fp = data;
1940
1941 get_bh(bh);
1942 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1943 fp->index++;
1944}
Joel Beckerfacdb772009-02-12 18:08:48 -08001945static int ocfs2_find_path(struct ocfs2_caching_info *ci,
1946 struct ocfs2_path *path, u32 cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08001947{
1948 struct find_path_data data;
1949
1950 data.index = 1;
1951 data.path = path;
Joel Beckerfacdb772009-02-12 18:08:48 -08001952 return __ocfs2_find_path(ci, path_root_el(path), cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08001953 find_path_ins, &data);
1954}
1955
1956static void find_leaf_ins(void *data, struct buffer_head *bh)
1957{
1958 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1959 struct ocfs2_extent_list *el = &eb->h_list;
1960 struct buffer_head **ret = data;
1961
1962 /* We want to retain only the leaf block. */
1963 if (le16_to_cpu(el->l_tree_depth) == 0) {
1964 get_bh(bh);
1965 *ret = bh;
1966 }
1967}
1968/*
1969 * Find the leaf block in the tree which would contain cpos. No
1970 * checking of the actual leaf is done.
1971 *
1972 * Some paths want to call this instead of allocating a path structure
1973 * and calling ocfs2_find_path().
1974 *
1975 * This function doesn't handle non btree extent lists.
1976 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001977int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1978 struct ocfs2_extent_list *root_el, u32 cpos,
1979 struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001980{
1981 int ret;
1982 struct buffer_head *bh = NULL;
1983
Joel Beckerfacdb772009-02-12 18:08:48 -08001984 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001985 if (ret) {
1986 mlog_errno(ret);
1987 goto out;
1988 }
1989
1990 *leaf_bh = bh;
1991out:
1992 return ret;
1993}
1994
1995/*
1996 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1997 *
1998 * Basically, we've moved stuff around at the bottom of the tree and
1999 * we need to fix up the extent records above the changes to reflect
2000 * the new changes.
2001 *
2002 * left_rec: the record on the left.
2003 * left_child_el: is the child list pointed to by left_rec
2004 * right_rec: the record to the right of left_rec
2005 * right_child_el: is the child list pointed to by right_rec
2006 *
2007 * By definition, this only works on interior nodes.
2008 */
2009static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
2010 struct ocfs2_extent_list *left_child_el,
2011 struct ocfs2_extent_rec *right_rec,
2012 struct ocfs2_extent_list *right_child_el)
2013{
2014 u32 left_clusters, right_end;
2015
2016 /*
2017 * Interior nodes never have holes. Their cpos is the cpos of
2018 * the leftmost record in their child list. Their cluster
2019 * count covers the full theoretical range of their child list
2020 * - the range between their cpos and the cpos of the record
2021 * immediately to their right.
2022 */
2023 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08002024 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
2025 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07002026 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
2027 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
2028 }
Mark Fashehdcd05382007-01-16 11:32:23 -08002029 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002030 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08002031
2032 /*
2033 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08002034 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08002035 * updating e_cpos to keep the same highest cluster count.
2036 */
2037 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002038 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08002039
2040 right_rec->e_cpos = left_rec->e_cpos;
2041 le32_add_cpu(&right_rec->e_cpos, left_clusters);
2042
2043 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002044 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08002045}
2046
2047/*
2048 * Adjust the adjacent root node records involved in a
2049 * rotation. left_el_blkno is passed in as a key so that we can easily
2050 * find it's index in the root list.
2051 */
2052static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
2053 struct ocfs2_extent_list *left_el,
2054 struct ocfs2_extent_list *right_el,
2055 u64 left_el_blkno)
2056{
2057 int i;
2058
2059 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
2060 le16_to_cpu(left_el->l_tree_depth));
2061
2062 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
2063 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
2064 break;
2065 }
2066
2067 /*
2068 * The path walking code should have never returned a root and
2069 * two paths which are not adjacent.
2070 */
2071 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2072
2073 ocfs2_adjust_adjacent_records(&root_el->l_recs[i], left_el,
2074 &root_el->l_recs[i + 1], right_el);
2075}
2076
2077/*
2078 * We've changed a leaf block (in right_path) and need to reflect that
2079 * change back up the subtree.
2080 *
2081 * This happens in multiple places:
2082 * - When we've moved an extent record from the left path leaf to the right
2083 * path leaf to make room for an empty extent in the left path leaf.
2084 * - When our insert into the right path leaf is at the leftmost edge
2085 * and requires an update of the path immediately to it's left. This
2086 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08002087 * - When we've adjusted the last extent record in the left path leaf and the
2088 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08002089 */
Joel Becker4619c732009-02-12 19:02:36 -08002090static void ocfs2_complete_edge_insert(handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08002091 struct ocfs2_path *left_path,
2092 struct ocfs2_path *right_path,
2093 int subtree_index)
2094{
2095 int ret, i, idx;
2096 struct ocfs2_extent_list *el, *left_el, *right_el;
2097 struct ocfs2_extent_rec *left_rec, *right_rec;
2098 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2099
2100 /*
2101 * Update the counts and position values within all the
2102 * interior nodes to reflect the leaf rotation we just did.
2103 *
2104 * The root node is handled below the loop.
2105 *
2106 * We begin the loop with right_el and left_el pointing to the
2107 * leaf lists and work our way up.
2108 *
2109 * NOTE: within this loop, left_el and right_el always refer
2110 * to the *child* lists.
2111 */
2112 left_el = path_leaf_el(left_path);
2113 right_el = path_leaf_el(right_path);
2114 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
2115 mlog(0, "Adjust records at index %u\n", i);
2116
2117 /*
2118 * One nice property of knowing that all of these
2119 * nodes are below the root is that we only deal with
2120 * the leftmost right node record and the rightmost
2121 * left node record.
2122 */
2123 el = left_path->p_node[i].el;
2124 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2125 left_rec = &el->l_recs[idx];
2126
2127 el = right_path->p_node[i].el;
2128 right_rec = &el->l_recs[0];
2129
2130 ocfs2_adjust_adjacent_records(left_rec, left_el, right_rec,
2131 right_el);
2132
2133 ret = ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2134 if (ret)
2135 mlog_errno(ret);
2136
2137 ret = ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
2138 if (ret)
2139 mlog_errno(ret);
2140
2141 /*
2142 * Setup our list pointers now so that the current
2143 * parents become children in the next iteration.
2144 */
2145 left_el = left_path->p_node[i].el;
2146 right_el = right_path->p_node[i].el;
2147 }
2148
2149 /*
2150 * At the root node, adjust the two adjacent records which
2151 * begin our path to the leaves.
2152 */
2153
2154 el = left_path->p_node[subtree_index].el;
2155 left_el = left_path->p_node[subtree_index + 1].el;
2156 right_el = right_path->p_node[subtree_index + 1].el;
2157
2158 ocfs2_adjust_root_records(el, left_el, right_el,
2159 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2160
2161 root_bh = left_path->p_node[subtree_index].bh;
2162
2163 ret = ocfs2_journal_dirty(handle, root_bh);
2164 if (ret)
2165 mlog_errno(ret);
2166}
2167
Joel Becker5c601ab2009-02-12 19:10:13 -08002168static int ocfs2_rotate_subtree_right(handle_t *handle,
2169 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08002170 struct ocfs2_path *left_path,
2171 struct ocfs2_path *right_path,
2172 int subtree_index)
2173{
2174 int ret, i;
2175 struct buffer_head *right_leaf_bh;
2176 struct buffer_head *left_leaf_bh = NULL;
2177 struct buffer_head *root_bh;
2178 struct ocfs2_extent_list *right_el, *left_el;
2179 struct ocfs2_extent_rec move_rec;
2180
2181 left_leaf_bh = path_leaf_bh(left_path);
2182 left_el = path_leaf_el(left_path);
2183
2184 if (left_el->l_next_free_rec != left_el->l_count) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002185 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002186 "Inode %llu has non-full interior leaf node %llu"
2187 "(next free = %u)",
Joel Becker5c601ab2009-02-12 19:10:13 -08002188 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002189 (unsigned long long)left_leaf_bh->b_blocknr,
2190 le16_to_cpu(left_el->l_next_free_rec));
2191 return -EROFS;
2192 }
2193
2194 /*
2195 * This extent block may already have an empty record, so we
2196 * return early if so.
2197 */
2198 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2199 return 0;
2200
2201 root_bh = left_path->p_node[subtree_index].bh;
2202 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2203
Joel Becker5c601ab2009-02-12 19:10:13 -08002204 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002205 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002206 if (ret) {
2207 mlog_errno(ret);
2208 goto out;
2209 }
2210
2211 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002212 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002213 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002214 if (ret) {
2215 mlog_errno(ret);
2216 goto out;
2217 }
2218
Joel Becker5c601ab2009-02-12 19:10:13 -08002219 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002220 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002221 if (ret) {
2222 mlog_errno(ret);
2223 goto out;
2224 }
2225 }
2226
2227 right_leaf_bh = path_leaf_bh(right_path);
2228 right_el = path_leaf_el(right_path);
2229
2230 /* This is a code error, not a disk corruption. */
2231 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2232 "because rightmost leaf block %llu is empty\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002233 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002234 (unsigned long long)right_leaf_bh->b_blocknr);
2235
2236 ocfs2_create_empty_extent(right_el);
2237
2238 ret = ocfs2_journal_dirty(handle, right_leaf_bh);
2239 if (ret) {
2240 mlog_errno(ret);
2241 goto out;
2242 }
2243
2244 /* Do the copy now. */
2245 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2246 move_rec = left_el->l_recs[i];
2247 right_el->l_recs[0] = move_rec;
2248
2249 /*
2250 * Clear out the record we just copied and shift everything
2251 * over, leaving an empty extent in the left leaf.
2252 *
2253 * We temporarily subtract from next_free_rec so that the
2254 * shift will lose the tail record (which is now defunct).
2255 */
2256 le16_add_cpu(&left_el->l_next_free_rec, -1);
2257 ocfs2_shift_records_right(left_el);
2258 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2259 le16_add_cpu(&left_el->l_next_free_rec, 1);
2260
2261 ret = ocfs2_journal_dirty(handle, left_leaf_bh);
2262 if (ret) {
2263 mlog_errno(ret);
2264 goto out;
2265 }
2266
Joel Becker4619c732009-02-12 19:02:36 -08002267 ocfs2_complete_edge_insert(handle, left_path, right_path,
2268 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002269
2270out:
2271 return ret;
2272}
2273
2274/*
2275 * Given a full path, determine what cpos value would return us a path
2276 * containing the leaf immediately to the left of the current one.
2277 *
2278 * Will return zero if the path passed in is already the leftmost path.
2279 */
2280static int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2281 struct ocfs2_path *path, u32 *cpos)
2282{
2283 int i, j, ret = 0;
2284 u64 blkno;
2285 struct ocfs2_extent_list *el;
2286
Mark Fashehe48edee2007-03-07 16:46:57 -08002287 BUG_ON(path->p_tree_depth == 0);
2288
Mark Fashehdcd05382007-01-16 11:32:23 -08002289 *cpos = 0;
2290
2291 blkno = path_leaf_bh(path)->b_blocknr;
2292
2293 /* Start at the tree node just above the leaf and work our way up. */
2294 i = path->p_tree_depth - 1;
2295 while (i >= 0) {
2296 el = path->p_node[i].el;
2297
2298 /*
2299 * Find the extent record just before the one in our
2300 * path.
2301 */
2302 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2303 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2304 if (j == 0) {
2305 if (i == 0) {
2306 /*
2307 * We've determined that the
2308 * path specified is already
2309 * the leftmost one - return a
2310 * cpos of zero.
2311 */
2312 goto out;
2313 }
2314 /*
2315 * The leftmost record points to our
2316 * leaf - we need to travel up the
2317 * tree one level.
2318 */
2319 goto next_node;
2320 }
2321
2322 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002323 *cpos = *cpos + ocfs2_rec_clusters(el,
2324 &el->l_recs[j - 1]);
2325 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002326 goto out;
2327 }
2328 }
2329
2330 /*
2331 * If we got here, we never found a valid node where
2332 * the tree indicated one should be.
2333 */
2334 ocfs2_error(sb,
2335 "Invalid extent tree at extent block %llu\n",
2336 (unsigned long long)blkno);
2337 ret = -EROFS;
2338 goto out;
2339
2340next_node:
2341 blkno = path->p_node[i].bh->b_blocknr;
2342 i--;
2343 }
2344
2345out:
2346 return ret;
2347}
2348
Mark Fasheh328d5752007-06-18 10:48:04 -07002349/*
2350 * Extend the transaction by enough credits to complete the rotation,
2351 * and still leave at least the original number of credits allocated
2352 * to this transaction.
2353 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002354static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002355 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002356 struct ocfs2_path *path)
2357{
Mark Fasheh328d5752007-06-18 10:48:04 -07002358 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002359
2360 if (handle->h_buffer_credits < credits)
2361 return ocfs2_extend_trans(handle, credits);
2362
2363 return 0;
2364}
2365
2366/*
2367 * Trap the case where we're inserting into the theoretical range past
2368 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2369 * whose cpos is less than ours into the right leaf.
2370 *
2371 * It's only necessary to look at the rightmost record of the left
2372 * leaf because the logic that calls us should ensure that the
2373 * theoretical ranges in the path components above the leaves are
2374 * correct.
2375 */
2376static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2377 u32 insert_cpos)
2378{
2379 struct ocfs2_extent_list *left_el;
2380 struct ocfs2_extent_rec *rec;
2381 int next_free;
2382
2383 left_el = path_leaf_el(left_path);
2384 next_free = le16_to_cpu(left_el->l_next_free_rec);
2385 rec = &left_el->l_recs[next_free - 1];
2386
2387 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2388 return 1;
2389 return 0;
2390}
2391
Mark Fasheh328d5752007-06-18 10:48:04 -07002392static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2393{
2394 int next_free = le16_to_cpu(el->l_next_free_rec);
2395 unsigned int range;
2396 struct ocfs2_extent_rec *rec;
2397
2398 if (next_free == 0)
2399 return 0;
2400
2401 rec = &el->l_recs[0];
2402 if (ocfs2_is_empty_extent(rec)) {
2403 /* Empty list. */
2404 if (next_free == 1)
2405 return 0;
2406 rec = &el->l_recs[1];
2407 }
2408
2409 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2410 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2411 return 1;
2412 return 0;
2413}
2414
Mark Fashehdcd05382007-01-16 11:32:23 -08002415/*
2416 * Rotate all the records in a btree right one record, starting at insert_cpos.
2417 *
2418 * The path to the rightmost leaf should be passed in.
2419 *
2420 * The array is assumed to be large enough to hold an entire path (tree depth).
2421 *
2422 * Upon succesful return from this function:
2423 *
2424 * - The 'right_path' array will contain a path to the leaf block
2425 * whose range contains e_cpos.
2426 * - That leaf block will have a single empty extent in list index 0.
2427 * - In the case that the rotation requires a post-insert update,
2428 * *ret_left_path will contain a valid path which can be passed to
2429 * ocfs2_insert_path().
2430 */
Joel Becker1bbf0b82009-02-12 19:42:08 -08002431static int ocfs2_rotate_tree_right(handle_t *handle,
Joel Becker5c601ab2009-02-12 19:10:13 -08002432 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002433 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002434 u32 insert_cpos,
2435 struct ocfs2_path *right_path,
2436 struct ocfs2_path **ret_left_path)
2437{
Mark Fasheh328d5752007-06-18 10:48:04 -07002438 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002439 u32 cpos;
2440 struct ocfs2_path *left_path = NULL;
Joel Becker5c601ab2009-02-12 19:10:13 -08002441 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehdcd05382007-01-16 11:32:23 -08002442
2443 *ret_left_path = NULL;
2444
Joel Beckerffdd7a52008-10-17 22:32:01 -07002445 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002446 if (!left_path) {
2447 ret = -ENOMEM;
2448 mlog_errno(ret);
2449 goto out;
2450 }
2451
Joel Becker5c601ab2009-02-12 19:10:13 -08002452 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002453 if (ret) {
2454 mlog_errno(ret);
2455 goto out;
2456 }
2457
2458 mlog(0, "Insert: %u, first left path cpos: %u\n", insert_cpos, cpos);
2459
2460 /*
2461 * What we want to do here is:
2462 *
2463 * 1) Start with the rightmost path.
2464 *
2465 * 2) Determine a path to the leaf block directly to the left
2466 * of that leaf.
2467 *
2468 * 3) Determine the 'subtree root' - the lowest level tree node
2469 * which contains a path to both leaves.
2470 *
2471 * 4) Rotate the subtree.
2472 *
2473 * 5) Find the next subtree by considering the left path to be
2474 * the new right path.
2475 *
2476 * The check at the top of this while loop also accepts
2477 * insert_cpos == cpos because cpos is only a _theoretical_
2478 * value to get us the left path - insert_cpos might very well
2479 * be filling that hole.
2480 *
2481 * Stop at a cpos of '0' because we either started at the
2482 * leftmost branch (i.e., a tree with one branch and a
2483 * rotation inside of it), or we've gone as far as we can in
2484 * rotating subtrees.
2485 */
2486 while (cpos && insert_cpos <= cpos) {
2487 mlog(0, "Rotating a tree: ins. cpos: %u, left path cpos: %u\n",
2488 insert_cpos, cpos);
2489
Joel Becker5c601ab2009-02-12 19:10:13 -08002490 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002491 if (ret) {
2492 mlog_errno(ret);
2493 goto out;
2494 }
2495
2496 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2497 path_leaf_bh(right_path),
Joel Becker5c601ab2009-02-12 19:10:13 -08002498 "Owner %llu: error during insert of %u "
Mark Fashehdcd05382007-01-16 11:32:23 -08002499 "(left path cpos %u) results in two identical "
2500 "paths ending at %llu\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002501 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2502 insert_cpos, cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08002503 (unsigned long long)
2504 path_leaf_bh(left_path)->b_blocknr);
2505
Mark Fasheh328d5752007-06-18 10:48:04 -07002506 if (split == SPLIT_NONE &&
2507 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002508 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002509
2510 /*
2511 * We've rotated the tree as much as we
2512 * should. The rest is up to
2513 * ocfs2_insert_path() to complete, after the
2514 * record insertion. We indicate this
2515 * situation by returning the left path.
2516 *
2517 * The reason we don't adjust the records here
2518 * before the record insert is that an error
2519 * later might break the rule where a parent
2520 * record e_cpos will reflect the actual
2521 * e_cpos of the 1st nonempty record of the
2522 * child list.
2523 */
2524 *ret_left_path = left_path;
2525 goto out_ret_path;
2526 }
2527
Joel Becker7dc02802009-02-12 19:20:13 -08002528 start = ocfs2_find_subtree_root(et, left_path, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002529
2530 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
2531 start,
2532 (unsigned long long) right_path->p_node[start].bh->b_blocknr,
2533 right_path->p_tree_depth);
2534
2535 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002536 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002537 if (ret) {
2538 mlog_errno(ret);
2539 goto out;
2540 }
2541
Joel Becker5c601ab2009-02-12 19:10:13 -08002542 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002543 right_path, start);
2544 if (ret) {
2545 mlog_errno(ret);
2546 goto out;
2547 }
2548
Mark Fasheh328d5752007-06-18 10:48:04 -07002549 if (split != SPLIT_NONE &&
2550 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2551 insert_cpos)) {
2552 /*
2553 * A rotate moves the rightmost left leaf
2554 * record over to the leftmost right leaf
2555 * slot. If we're doing an extent split
2556 * instead of a real insert, then we have to
2557 * check that the extent to be split wasn't
2558 * just moved over. If it was, then we can
2559 * exit here, passing left_path back -
2560 * ocfs2_split_extent() is smart enough to
2561 * search both leaves.
2562 */
2563 *ret_left_path = left_path;
2564 goto out_ret_path;
2565 }
2566
Mark Fashehdcd05382007-01-16 11:32:23 -08002567 /*
2568 * There is no need to re-read the next right path
2569 * as we know that it'll be our current left
2570 * path. Optimize by copying values instead.
2571 */
2572 ocfs2_mv_path(right_path, left_path);
2573
Joel Becker5c601ab2009-02-12 19:10:13 -08002574 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002575 if (ret) {
2576 mlog_errno(ret);
2577 goto out;
2578 }
2579 }
2580
2581out:
2582 ocfs2_free_path(left_path);
2583
2584out_ret_path:
2585 return ret;
2586}
2587
Joel Becker09106ba2009-02-12 19:43:57 -08002588static int ocfs2_update_edge_lengths(handle_t *handle,
2589 struct ocfs2_extent_tree *et,
Tao Ma3c5e1062009-07-21 15:42:05 +08002590 int subtree_index, struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002591{
Tao Ma3c5e1062009-07-21 15:42:05 +08002592 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002593 struct ocfs2_extent_rec *rec;
2594 struct ocfs2_extent_list *el;
2595 struct ocfs2_extent_block *eb;
2596 u32 range;
2597
Tao Ma3c5e1062009-07-21 15:42:05 +08002598 /*
2599 * In normal tree rotation process, we will never touch the
2600 * tree branch above subtree_index and ocfs2_extend_rotate_transaction
2601 * doesn't reserve the credits for them either.
2602 *
2603 * But we do have a special case here which will update the rightmost
2604 * records for all the bh in the path.
2605 * So we have to allocate extra credits and access them.
2606 */
2607 ret = ocfs2_extend_trans(handle,
2608 handle->h_buffer_credits + subtree_index);
2609 if (ret) {
2610 mlog_errno(ret);
2611 goto out;
2612 }
2613
Joel Becker09106ba2009-02-12 19:43:57 -08002614 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002615 if (ret) {
2616 mlog_errno(ret);
2617 goto out;
2618 }
2619
Mark Fasheh328d5752007-06-18 10:48:04 -07002620 /* Path should always be rightmost. */
2621 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2622 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2623
2624 el = &eb->h_list;
2625 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2626 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2627 rec = &el->l_recs[idx];
2628 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2629
2630 for (i = 0; i < path->p_tree_depth; i++) {
2631 el = path->p_node[i].el;
2632 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2633 rec = &el->l_recs[idx];
2634
2635 rec->e_int_clusters = cpu_to_le32(range);
2636 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2637
2638 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2639 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002640out:
2641 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002642}
2643
Joel Becker6641b0c2009-02-12 18:57:52 -08002644static void ocfs2_unlink_path(handle_t *handle,
2645 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002646 struct ocfs2_cached_dealloc_ctxt *dealloc,
2647 struct ocfs2_path *path, int unlink_start)
2648{
2649 int ret, i;
2650 struct ocfs2_extent_block *eb;
2651 struct ocfs2_extent_list *el;
2652 struct buffer_head *bh;
2653
2654 for(i = unlink_start; i < path_num_items(path); i++) {
2655 bh = path->p_node[i].bh;
2656
2657 eb = (struct ocfs2_extent_block *)bh->b_data;
2658 /*
2659 * Not all nodes might have had their final count
2660 * decremented by the caller - handle this here.
2661 */
2662 el = &eb->h_list;
2663 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2664 mlog(ML_ERROR,
2665 "Inode %llu, attempted to remove extent block "
2666 "%llu with %u records\n",
Joel Becker6641b0c2009-02-12 18:57:52 -08002667 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07002668 (unsigned long long)le64_to_cpu(eb->h_blkno),
2669 le16_to_cpu(el->l_next_free_rec));
2670
2671 ocfs2_journal_dirty(handle, bh);
Joel Becker6641b0c2009-02-12 18:57:52 -08002672 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002673 continue;
2674 }
2675
2676 el->l_next_free_rec = 0;
2677 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2678
2679 ocfs2_journal_dirty(handle, bh);
2680
2681 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2682 if (ret)
2683 mlog_errno(ret);
2684
Joel Becker6641b0c2009-02-12 18:57:52 -08002685 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002686 }
2687}
2688
Joel Becker6641b0c2009-02-12 18:57:52 -08002689static void ocfs2_unlink_subtree(handle_t *handle,
2690 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002691 struct ocfs2_path *left_path,
2692 struct ocfs2_path *right_path,
2693 int subtree_index,
2694 struct ocfs2_cached_dealloc_ctxt *dealloc)
2695{
2696 int i;
2697 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2698 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2699 struct ocfs2_extent_list *el;
2700 struct ocfs2_extent_block *eb;
2701
2702 el = path_leaf_el(left_path);
2703
2704 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2705
2706 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2707 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2708 break;
2709
2710 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2711
2712 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2713 le16_add_cpu(&root_el->l_next_free_rec, -1);
2714
2715 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2716 eb->h_next_leaf_blk = 0;
2717
2718 ocfs2_journal_dirty(handle, root_bh);
2719 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2720
Joel Becker6641b0c2009-02-12 18:57:52 -08002721 ocfs2_unlink_path(handle, et, dealloc, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002722 subtree_index + 1);
2723}
2724
Joel Becker1e2dd632009-02-12 19:45:28 -08002725static int ocfs2_rotate_subtree_left(handle_t *handle,
2726 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002727 struct ocfs2_path *left_path,
2728 struct ocfs2_path *right_path,
2729 int subtree_index,
2730 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker1e2dd632009-02-12 19:45:28 -08002731 int *deleted)
Mark Fasheh328d5752007-06-18 10:48:04 -07002732{
2733 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002734 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002735 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2736 struct ocfs2_extent_block *eb;
2737
2738 *deleted = 0;
2739
2740 right_leaf_el = path_leaf_el(right_path);
2741 left_leaf_el = path_leaf_el(left_path);
2742 root_bh = left_path->p_node[subtree_index].bh;
2743 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2744
2745 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2746 return 0;
2747
2748 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2749 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2750 /*
2751 * It's legal for us to proceed if the right leaf is
2752 * the rightmost one and it has an empty extent. There
2753 * are two cases to handle - whether the leaf will be
2754 * empty after removal or not. If the leaf isn't empty
2755 * then just remove the empty extent up front. The
2756 * next block will handle empty leaves by flagging
2757 * them for unlink.
2758 *
2759 * Non rightmost leaves will throw -EAGAIN and the
2760 * caller can manually move the subtree and retry.
2761 */
2762
2763 if (eb->h_next_leaf_blk != 0ULL)
2764 return -EAGAIN;
2765
2766 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002767 ret = ocfs2_journal_access_eb(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002768 path_leaf_bh(right_path),
2769 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002770 if (ret) {
2771 mlog_errno(ret);
2772 goto out;
2773 }
2774
2775 ocfs2_remove_empty_extent(right_leaf_el);
2776 } else
2777 right_has_empty = 1;
2778 }
2779
2780 if (eb->h_next_leaf_blk == 0ULL &&
2781 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2782 /*
2783 * We have to update i_last_eb_blk during the meta
2784 * data delete.
2785 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08002786 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07002787 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002788 if (ret) {
2789 mlog_errno(ret);
2790 goto out;
2791 }
2792
2793 del_right_subtree = 1;
2794 }
2795
2796 /*
2797 * Getting here with an empty extent in the right path implies
2798 * that it's the rightmost path and will be deleted.
2799 */
2800 BUG_ON(right_has_empty && !del_right_subtree);
2801
Joel Becker1e2dd632009-02-12 19:45:28 -08002802 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002803 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002804 if (ret) {
2805 mlog_errno(ret);
2806 goto out;
2807 }
2808
2809 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002810 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002811 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002812 if (ret) {
2813 mlog_errno(ret);
2814 goto out;
2815 }
2816
Joel Becker1e2dd632009-02-12 19:45:28 -08002817 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002818 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002819 if (ret) {
2820 mlog_errno(ret);
2821 goto out;
2822 }
2823 }
2824
2825 if (!right_has_empty) {
2826 /*
2827 * Only do this if we're moving a real
2828 * record. Otherwise, the action is delayed until
2829 * after removal of the right path in which case we
2830 * can do a simple shift to remove the empty extent.
2831 */
2832 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2833 memset(&right_leaf_el->l_recs[0], 0,
2834 sizeof(struct ocfs2_extent_rec));
2835 }
2836 if (eb->h_next_leaf_blk == 0ULL) {
2837 /*
2838 * Move recs over to get rid of empty extent, decrease
2839 * next_free. This is allowed to remove the last
2840 * extent in our leaf (setting l_next_free_rec to
2841 * zero) - the delete code below won't care.
2842 */
2843 ocfs2_remove_empty_extent(right_leaf_el);
2844 }
2845
2846 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2847 if (ret)
2848 mlog_errno(ret);
2849 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
2850 if (ret)
2851 mlog_errno(ret);
2852
2853 if (del_right_subtree) {
Joel Becker6641b0c2009-02-12 18:57:52 -08002854 ocfs2_unlink_subtree(handle, et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002855 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08002856 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08002857 left_path);
2858 if (ret) {
2859 mlog_errno(ret);
2860 goto out;
2861 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002862
2863 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002864 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002865
2866 /*
2867 * Removal of the extent in the left leaf was skipped
2868 * above so we could delete the right path
2869 * 1st.
2870 */
2871 if (right_has_empty)
2872 ocfs2_remove_empty_extent(left_leaf_el);
2873
Tao Mae7d4cb62008-08-18 17:38:44 +08002874 ret = ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002875 if (ret)
2876 mlog_errno(ret);
2877
2878 *deleted = 1;
2879 } else
Joel Becker4619c732009-02-12 19:02:36 -08002880 ocfs2_complete_edge_insert(handle, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002881 subtree_index);
2882
2883out:
2884 return ret;
2885}
2886
2887/*
2888 * Given a full path, determine what cpos value would return us a path
2889 * containing the leaf immediately to the right of the current one.
2890 *
2891 * Will return zero if the path passed in is already the rightmost path.
2892 *
2893 * This looks similar, but is subtly different to
2894 * ocfs2_find_cpos_for_left_leaf().
2895 */
2896static int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2897 struct ocfs2_path *path, u32 *cpos)
2898{
2899 int i, j, ret = 0;
2900 u64 blkno;
2901 struct ocfs2_extent_list *el;
2902
2903 *cpos = 0;
2904
2905 if (path->p_tree_depth == 0)
2906 return 0;
2907
2908 blkno = path_leaf_bh(path)->b_blocknr;
2909
2910 /* Start at the tree node just above the leaf and work our way up. */
2911 i = path->p_tree_depth - 1;
2912 while (i >= 0) {
2913 int next_free;
2914
2915 el = path->p_node[i].el;
2916
2917 /*
2918 * Find the extent record just after the one in our
2919 * path.
2920 */
2921 next_free = le16_to_cpu(el->l_next_free_rec);
2922 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2923 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2924 if (j == (next_free - 1)) {
2925 if (i == 0) {
2926 /*
2927 * We've determined that the
2928 * path specified is already
2929 * the rightmost one - return a
2930 * cpos of zero.
2931 */
2932 goto out;
2933 }
2934 /*
2935 * The rightmost record points to our
2936 * leaf - we need to travel up the
2937 * tree one level.
2938 */
2939 goto next_node;
2940 }
2941
2942 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2943 goto out;
2944 }
2945 }
2946
2947 /*
2948 * If we got here, we never found a valid node where
2949 * the tree indicated one should be.
2950 */
2951 ocfs2_error(sb,
2952 "Invalid extent tree at extent block %llu\n",
2953 (unsigned long long)blkno);
2954 ret = -EROFS;
2955 goto out;
2956
2957next_node:
2958 blkno = path->p_node[i].bh->b_blocknr;
2959 i--;
2960 }
2961
2962out:
2963 return ret;
2964}
2965
Joel Becker70f18c02009-02-13 02:09:31 -08002966static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2967 struct ocfs2_extent_tree *et,
Joel Becker13723d02008-10-17 19:25:01 -07002968 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002969{
2970 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002971 struct buffer_head *bh = path_leaf_bh(path);
2972 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002973
2974 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2975 return 0;
2976
Joel Becker70f18c02009-02-13 02:09:31 -08002977 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Joel Becker13723d02008-10-17 19:25:01 -07002978 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002979 if (ret) {
2980 mlog_errno(ret);
2981 goto out;
2982 }
2983
2984 ocfs2_remove_empty_extent(el);
2985
2986 ret = ocfs2_journal_dirty(handle, bh);
2987 if (ret)
2988 mlog_errno(ret);
2989
2990out:
2991 return ret;
2992}
2993
Joel Beckere46f74d2009-02-12 19:47:43 -08002994static int __ocfs2_rotate_tree_left(handle_t *handle,
2995 struct ocfs2_extent_tree *et,
2996 int orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07002997 struct ocfs2_path *path,
2998 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08002999 struct ocfs2_path **empty_extent_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003000{
3001 int ret, subtree_root, deleted;
3002 u32 right_cpos;
3003 struct ocfs2_path *left_path = NULL;
3004 struct ocfs2_path *right_path = NULL;
Joel Beckere46f74d2009-02-12 19:47:43 -08003005 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fasheh328d5752007-06-18 10:48:04 -07003006
3007 BUG_ON(!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])));
3008
3009 *empty_extent_path = NULL;
3010
Joel Beckere46f74d2009-02-12 19:47:43 -08003011 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003012 if (ret) {
3013 mlog_errno(ret);
3014 goto out;
3015 }
3016
Joel Beckerffdd7a52008-10-17 22:32:01 -07003017 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003018 if (!left_path) {
3019 ret = -ENOMEM;
3020 mlog_errno(ret);
3021 goto out;
3022 }
3023
3024 ocfs2_cp_path(left_path, path);
3025
Joel Beckerffdd7a52008-10-17 22:32:01 -07003026 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003027 if (!right_path) {
3028 ret = -ENOMEM;
3029 mlog_errno(ret);
3030 goto out;
3031 }
3032
3033 while (right_cpos) {
Joel Beckerfacdb772009-02-12 18:08:48 -08003034 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003035 if (ret) {
3036 mlog_errno(ret);
3037 goto out;
3038 }
3039
Joel Becker7dc02802009-02-12 19:20:13 -08003040 subtree_root = ocfs2_find_subtree_root(et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003041 right_path);
3042
3043 mlog(0, "Subtree root at index %d (blk %llu, depth %d)\n",
3044 subtree_root,
3045 (unsigned long long)
3046 right_path->p_node[subtree_root].bh->b_blocknr,
3047 right_path->p_tree_depth);
3048
3049 ret = ocfs2_extend_rotate_transaction(handle, subtree_root,
3050 orig_credits, left_path);
3051 if (ret) {
3052 mlog_errno(ret);
3053 goto out;
3054 }
3055
Mark Fashehe8aed342007-12-03 16:43:01 -08003056 /*
3057 * Caller might still want to make changes to the
3058 * tree root, so re-add it to the journal here.
3059 */
Joel Beckere46f74d2009-02-12 19:47:43 -08003060 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003061 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08003062 if (ret) {
3063 mlog_errno(ret);
3064 goto out;
3065 }
3066
Joel Becker1e2dd632009-02-12 19:45:28 -08003067 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003068 right_path, subtree_root,
Joel Becker1e2dd632009-02-12 19:45:28 -08003069 dealloc, &deleted);
Mark Fasheh328d5752007-06-18 10:48:04 -07003070 if (ret == -EAGAIN) {
3071 /*
3072 * The rotation has to temporarily stop due to
3073 * the right subtree having an empty
3074 * extent. Pass it back to the caller for a
3075 * fixup.
3076 */
3077 *empty_extent_path = right_path;
3078 right_path = NULL;
3079 goto out;
3080 }
3081 if (ret) {
3082 mlog_errno(ret);
3083 goto out;
3084 }
3085
3086 /*
3087 * The subtree rotate might have removed records on
3088 * the rightmost edge. If so, then rotation is
3089 * complete.
3090 */
3091 if (deleted)
3092 break;
3093
3094 ocfs2_mv_path(left_path, right_path);
3095
Joel Beckere46f74d2009-02-12 19:47:43 -08003096 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003097 &right_cpos);
3098 if (ret) {
3099 mlog_errno(ret);
3100 goto out;
3101 }
3102 }
3103
3104out:
3105 ocfs2_free_path(right_path);
3106 ocfs2_free_path(left_path);
3107
3108 return ret;
3109}
3110
Joel Becker70f18c02009-02-13 02:09:31 -08003111static int ocfs2_remove_rightmost_path(handle_t *handle,
3112 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003113 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003114 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003115{
3116 int ret, subtree_index;
3117 u32 cpos;
3118 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003119 struct ocfs2_extent_block *eb;
3120 struct ocfs2_extent_list *el;
3121
Mark Fasheh328d5752007-06-18 10:48:04 -07003122
Joel Becker6136ca52009-02-12 19:32:43 -08003123 ret = ocfs2_et_sanity_check(et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003124 if (ret)
3125 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003126 /*
3127 * There's two ways we handle this depending on
3128 * whether path is the only existing one.
3129 */
3130 ret = ocfs2_extend_rotate_transaction(handle, 0,
3131 handle->h_buffer_credits,
3132 path);
3133 if (ret) {
3134 mlog_errno(ret);
3135 goto out;
3136 }
3137
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003138 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003139 if (ret) {
3140 mlog_errno(ret);
3141 goto out;
3142 }
3143
Joel Becker3d03a302009-02-12 17:49:26 -08003144 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3145 path, &cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003146 if (ret) {
3147 mlog_errno(ret);
3148 goto out;
3149 }
3150
3151 if (cpos) {
3152 /*
3153 * We have a path to the left of this one - it needs
3154 * an update too.
3155 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003156 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003157 if (!left_path) {
3158 ret = -ENOMEM;
3159 mlog_errno(ret);
3160 goto out;
3161 }
3162
Joel Beckerfacdb772009-02-12 18:08:48 -08003163 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003164 if (ret) {
3165 mlog_errno(ret);
3166 goto out;
3167 }
3168
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003169 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003170 if (ret) {
3171 mlog_errno(ret);
3172 goto out;
3173 }
3174
Joel Becker7dc02802009-02-12 19:20:13 -08003175 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003176
Joel Becker6641b0c2009-02-12 18:57:52 -08003177 ocfs2_unlink_subtree(handle, et, left_path, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003178 subtree_index, dealloc);
Joel Becker09106ba2009-02-12 19:43:57 -08003179 ret = ocfs2_update_edge_lengths(handle, et, subtree_index,
Tao Ma3c5e1062009-07-21 15:42:05 +08003180 left_path);
3181 if (ret) {
3182 mlog_errno(ret);
3183 goto out;
3184 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003185
3186 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003187 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003188 } else {
3189 /*
3190 * 'path' is also the leftmost path which
3191 * means it must be the only one. This gets
3192 * handled differently because we want to
Joel Becker70f18c02009-02-13 02:09:31 -08003193 * revert the root back to having extents
Mark Fasheh328d5752007-06-18 10:48:04 -07003194 * in-line.
3195 */
Joel Becker6641b0c2009-02-12 18:57:52 -08003196 ocfs2_unlink_path(handle, et, dealloc, path, 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003197
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003198 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003199 el->l_tree_depth = 0;
3200 el->l_next_free_rec = 0;
3201 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3202
Joel Becker35dc0aa2008-08-20 16:25:06 -07003203 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003204 }
3205
3206 ocfs2_journal_dirty(handle, path_root_bh(path));
3207
3208out:
3209 ocfs2_free_path(left_path);
3210 return ret;
3211}
3212
3213/*
3214 * Left rotation of btree records.
3215 *
3216 * In many ways, this is (unsurprisingly) the opposite of right
3217 * rotation. We start at some non-rightmost path containing an empty
3218 * extent in the leaf block. The code works its way to the rightmost
3219 * path by rotating records to the left in every subtree.
3220 *
3221 * This is used by any code which reduces the number of extent records
3222 * in a leaf. After removal, an empty record should be placed in the
3223 * leftmost list position.
3224 *
3225 * This won't handle a length update of the rightmost path records if
3226 * the rightmost tree leaf record is removed so the caller is
3227 * responsible for detecting and correcting that.
3228 */
Joel Becker70f18c02009-02-13 02:09:31 -08003229static int ocfs2_rotate_tree_left(handle_t *handle,
3230 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003231 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003232 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003233{
3234 int ret, orig_credits = handle->h_buffer_credits;
3235 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3236 struct ocfs2_extent_block *eb;
3237 struct ocfs2_extent_list *el;
3238
3239 el = path_leaf_el(path);
3240 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3241 return 0;
3242
3243 if (path->p_tree_depth == 0) {
3244rightmost_no_delete:
3245 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003246 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003247 * it up front.
3248 */
Joel Becker70f18c02009-02-13 02:09:31 -08003249 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003250 if (ret)
3251 mlog_errno(ret);
3252 goto out;
3253 }
3254
3255 /*
3256 * Handle rightmost branch now. There's several cases:
3257 * 1) simple rotation leaving records in there. That's trivial.
3258 * 2) rotation requiring a branch delete - there's no more
3259 * records left. Two cases of this:
3260 * a) There are branches to the left.
3261 * b) This is also the leftmost (the only) branch.
3262 *
3263 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3264 * 2a) we need the left branch so that we can update it with the unlink
Joel Becker70f18c02009-02-13 02:09:31 -08003265 * 2b) we need to bring the root back to inline extents.
Mark Fasheh328d5752007-06-18 10:48:04 -07003266 */
3267
3268 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3269 el = &eb->h_list;
3270 if (eb->h_next_leaf_blk == 0) {
3271 /*
3272 * This gets a bit tricky if we're going to delete the
3273 * rightmost path. Get the other cases out of the way
3274 * 1st.
3275 */
3276 if (le16_to_cpu(el->l_next_free_rec) > 1)
3277 goto rightmost_no_delete;
3278
3279 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3280 ret = -EIO;
Joel Becker70f18c02009-02-13 02:09:31 -08003281 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
3282 "Owner %llu has empty extent block at %llu",
3283 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07003284 (unsigned long long)le64_to_cpu(eb->h_blkno));
3285 goto out;
3286 }
3287
3288 /*
3289 * XXX: The caller can not trust "path" any more after
3290 * this as it will have been deleted. What do we do?
3291 *
3292 * In theory the rotate-for-merge code will never get
3293 * here because it'll always ask for a rotate in a
3294 * nonempty list.
3295 */
3296
Joel Becker70f18c02009-02-13 02:09:31 -08003297 ret = ocfs2_remove_rightmost_path(handle, et, path,
3298 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003299 if (ret)
3300 mlog_errno(ret);
3301 goto out;
3302 }
3303
3304 /*
3305 * Now we can loop, remembering the path we get from -EAGAIN
3306 * and restarting from there.
3307 */
3308try_rotate:
Joel Beckere46f74d2009-02-12 19:47:43 -08003309 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3310 dealloc, &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003311 if (ret && ret != -EAGAIN) {
3312 mlog_errno(ret);
3313 goto out;
3314 }
3315
3316 while (ret == -EAGAIN) {
3317 tmp_path = restart_path;
3318 restart_path = NULL;
3319
Joel Beckere46f74d2009-02-12 19:47:43 -08003320 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07003321 tmp_path, dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08003322 &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003323 if (ret && ret != -EAGAIN) {
3324 mlog_errno(ret);
3325 goto out;
3326 }
3327
3328 ocfs2_free_path(tmp_path);
3329 tmp_path = NULL;
3330
3331 if (ret == 0)
3332 goto try_rotate;
3333 }
3334
3335out:
3336 ocfs2_free_path(tmp_path);
3337 ocfs2_free_path(restart_path);
3338 return ret;
3339}
3340
3341static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3342 int index)
3343{
3344 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3345 unsigned int size;
3346
3347 if (rec->e_leaf_clusters == 0) {
3348 /*
3349 * We consumed all of the merged-from record. An empty
3350 * extent cannot exist anywhere but the 1st array
3351 * position, so move things over if the merged-from
3352 * record doesn't occupy that position.
3353 *
3354 * This creates a new empty extent so the caller
3355 * should be smart enough to have removed any existing
3356 * ones.
3357 */
3358 if (index > 0) {
3359 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3360 size = index * sizeof(struct ocfs2_extent_rec);
3361 memmove(&el->l_recs[1], &el->l_recs[0], size);
3362 }
3363
3364 /*
3365 * Always memset - the caller doesn't check whether it
3366 * created an empty extent, so there could be junk in
3367 * the other fields.
3368 */
3369 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3370 }
3371}
3372
Joel Becker4fe82c32009-02-13 02:16:08 -08003373static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003374 struct ocfs2_path *left_path,
3375 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003376{
3377 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003378 u32 right_cpos;
3379 struct ocfs2_path *right_path = NULL;
3380 struct ocfs2_extent_list *left_el;
3381
3382 *ret_right_path = NULL;
3383
3384 /* This function shouldn't be called for non-trees. */
3385 BUG_ON(left_path->p_tree_depth == 0);
3386
3387 left_el = path_leaf_el(left_path);
3388 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3389
Joel Becker4fe82c32009-02-13 02:16:08 -08003390 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3391 left_path, &right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003392 if (ret) {
3393 mlog_errno(ret);
3394 goto out;
3395 }
3396
3397 /* This function shouldn't be called for the rightmost leaf. */
3398 BUG_ON(right_cpos == 0);
3399
Joel Beckerffdd7a52008-10-17 22:32:01 -07003400 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003401 if (!right_path) {
3402 ret = -ENOMEM;
3403 mlog_errno(ret);
3404 goto out;
3405 }
3406
Joel Becker4fe82c32009-02-13 02:16:08 -08003407 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003408 if (ret) {
3409 mlog_errno(ret);
3410 goto out;
3411 }
3412
3413 *ret_right_path = right_path;
3414out:
3415 if (ret)
3416 ocfs2_free_path(right_path);
3417 return ret;
3418}
3419
3420/*
3421 * Remove split_rec clusters from the record at index and merge them
3422 * onto the beginning of the record "next" to it.
3423 * For index < l_count - 1, the next means the extent rec at index + 1.
3424 * For index == l_count - 1, the "next" means the 1st extent rec of the
3425 * next extent block.
3426 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003427static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003428 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003429 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003430 struct ocfs2_extent_rec *split_rec,
3431 int index)
3432{
3433 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003434 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3435 struct ocfs2_extent_rec *left_rec;
3436 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003437 struct ocfs2_extent_list *right_el;
3438 struct ocfs2_path *right_path = NULL;
3439 int subtree_index = 0;
3440 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3441 struct buffer_head *bh = path_leaf_bh(left_path);
3442 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003443
3444 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003445 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003446
Al Viro9d8df6a2008-05-21 06:32:11 +01003447 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003448 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3449 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003450 ret = ocfs2_get_right_path(et, left_path, &right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003451 if (ret) {
3452 mlog_errno(ret);
3453 goto out;
3454 }
3455
3456 right_el = path_leaf_el(right_path);
3457 next_free = le16_to_cpu(right_el->l_next_free_rec);
3458 BUG_ON(next_free <= 0);
3459 right_rec = &right_el->l_recs[0];
3460 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003461 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003462 right_rec = &right_el->l_recs[1];
3463 }
3464
3465 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3466 le16_to_cpu(left_rec->e_leaf_clusters) !=
3467 le32_to_cpu(right_rec->e_cpos));
3468
Joel Becker7dc02802009-02-12 19:20:13 -08003469 subtree_index = ocfs2_find_subtree_root(et, left_path,
3470 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003471
3472 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3473 handle->h_buffer_credits,
3474 right_path);
3475 if (ret) {
3476 mlog_errno(ret);
3477 goto out;
3478 }
3479
3480 root_bh = left_path->p_node[subtree_index].bh;
3481 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3482
Joel Becker7dc02802009-02-12 19:20:13 -08003483 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003484 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003485 if (ret) {
3486 mlog_errno(ret);
3487 goto out;
3488 }
3489
3490 for (i = subtree_index + 1;
3491 i < path_num_items(right_path); i++) {
Joel Becker7dc02802009-02-12 19:20:13 -08003492 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003493 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003494 if (ret) {
3495 mlog_errno(ret);
3496 goto out;
3497 }
3498
Joel Becker7dc02802009-02-12 19:20:13 -08003499 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003500 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003501 if (ret) {
3502 mlog_errno(ret);
3503 goto out;
3504 }
3505 }
3506
3507 } else {
3508 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3509 right_rec = &el->l_recs[index + 1];
3510 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003511
Joel Becker7dc02802009-02-12 19:20:13 -08003512 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
Joel Becker13723d02008-10-17 19:25:01 -07003513 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003514 if (ret) {
3515 mlog_errno(ret);
3516 goto out;
3517 }
3518
3519 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3520
3521 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3522 le64_add_cpu(&right_rec->e_blkno,
Joel Becker7dc02802009-02-12 19:20:13 -08003523 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3524 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003525 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3526
3527 ocfs2_cleanup_merge(el, index);
3528
3529 ret = ocfs2_journal_dirty(handle, bh);
3530 if (ret)
3531 mlog_errno(ret);
3532
Tao Ma677b9752008-01-30 14:21:05 +08003533 if (right_path) {
3534 ret = ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
3535 if (ret)
3536 mlog_errno(ret);
3537
Joel Becker4619c732009-02-12 19:02:36 -08003538 ocfs2_complete_edge_insert(handle, left_path, right_path,
3539 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003540 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003541out:
Tao Ma677b9752008-01-30 14:21:05 +08003542 if (right_path)
3543 ocfs2_free_path(right_path);
3544 return ret;
3545}
3546
Joel Becker4fe82c32009-02-13 02:16:08 -08003547static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003548 struct ocfs2_path *right_path,
3549 struct ocfs2_path **ret_left_path)
3550{
3551 int ret;
3552 u32 left_cpos;
3553 struct ocfs2_path *left_path = NULL;
3554
3555 *ret_left_path = NULL;
3556
3557 /* This function shouldn't be called for non-trees. */
3558 BUG_ON(right_path->p_tree_depth == 0);
3559
Joel Becker4fe82c32009-02-13 02:16:08 -08003560 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
Tao Ma677b9752008-01-30 14:21:05 +08003561 right_path, &left_cpos);
3562 if (ret) {
3563 mlog_errno(ret);
3564 goto out;
3565 }
3566
3567 /* This function shouldn't be called for the leftmost leaf. */
3568 BUG_ON(left_cpos == 0);
3569
Joel Beckerffdd7a52008-10-17 22:32:01 -07003570 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003571 if (!left_path) {
3572 ret = -ENOMEM;
3573 mlog_errno(ret);
3574 goto out;
3575 }
3576
Joel Becker4fe82c32009-02-13 02:16:08 -08003577 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003578 if (ret) {
3579 mlog_errno(ret);
3580 goto out;
3581 }
3582
3583 *ret_left_path = left_path;
3584out:
3585 if (ret)
3586 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003587 return ret;
3588}
3589
3590/*
3591 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003592 * onto the tail of the record "before" it.
3593 * For index > 0, the "before" means the extent rec at index - 1.
3594 *
3595 * For index == 0, the "before" means the last record of the previous
3596 * extent block. And there is also a situation that we may need to
3597 * remove the rightmost leaf extent block in the right_path and change
3598 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003599 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003600static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003601 handle_t *handle,
Joel Becker4fe82c32009-02-13 02:16:08 -08003602 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003603 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003604 struct ocfs2_cached_dealloc_ctxt *dealloc,
3605 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003606{
Tao Ma677b9752008-01-30 14:21:05 +08003607 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003608 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3609 struct ocfs2_extent_rec *left_rec;
3610 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003611 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3612 struct buffer_head *bh = path_leaf_bh(right_path);
3613 struct buffer_head *root_bh = NULL;
3614 struct ocfs2_path *left_path = NULL;
3615 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003616
Tao Ma677b9752008-01-30 14:21:05 +08003617 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003618
Mark Fasheh328d5752007-06-18 10:48:04 -07003619 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003620 if (index == 0) {
3621 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003622 ret = ocfs2_get_left_path(et, right_path, &left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003623 if (ret) {
3624 mlog_errno(ret);
3625 goto out;
3626 }
3627
3628 left_el = path_leaf_el(left_path);
3629 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3630 le16_to_cpu(left_el->l_count));
3631
3632 left_rec = &left_el->l_recs[
3633 le16_to_cpu(left_el->l_next_free_rec) - 1];
3634 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3635 le16_to_cpu(left_rec->e_leaf_clusters) !=
3636 le32_to_cpu(split_rec->e_cpos));
3637
Joel Becker7dc02802009-02-12 19:20:13 -08003638 subtree_index = ocfs2_find_subtree_root(et, left_path,
3639 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003640
3641 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3642 handle->h_buffer_credits,
3643 left_path);
3644 if (ret) {
3645 mlog_errno(ret);
3646 goto out;
3647 }
3648
3649 root_bh = left_path->p_node[subtree_index].bh;
3650 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3651
Joel Becker4fe82c32009-02-13 02:16:08 -08003652 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003653 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003654 if (ret) {
3655 mlog_errno(ret);
3656 goto out;
3657 }
3658
3659 for (i = subtree_index + 1;
3660 i < path_num_items(right_path); i++) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003661 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003662 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003663 if (ret) {
3664 mlog_errno(ret);
3665 goto out;
3666 }
3667
Joel Becker4fe82c32009-02-13 02:16:08 -08003668 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003669 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003670 if (ret) {
3671 mlog_errno(ret);
3672 goto out;
3673 }
3674 }
3675 } else {
3676 left_rec = &el->l_recs[index - 1];
3677 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3678 has_empty_extent = 1;
3679 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003680
Joel Becker4fe82c32009-02-13 02:16:08 -08003681 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Tao Ma9047bea2009-01-05 14:45:24 +08003682 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003683 if (ret) {
3684 mlog_errno(ret);
3685 goto out;
3686 }
3687
3688 if (has_empty_extent && index == 1) {
3689 /*
3690 * The easy case - we can just plop the record right in.
3691 */
3692 *left_rec = *split_rec;
3693
3694 has_empty_extent = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003695 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003696 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003697
3698 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3699 le64_add_cpu(&right_rec->e_blkno,
Joel Becker4fe82c32009-02-13 02:16:08 -08003700 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3701 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003702 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3703
3704 ocfs2_cleanup_merge(el, index);
3705
3706 ret = ocfs2_journal_dirty(handle, bh);
3707 if (ret)
3708 mlog_errno(ret);
3709
Tao Ma677b9752008-01-30 14:21:05 +08003710 if (left_path) {
3711 ret = ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
3712 if (ret)
3713 mlog_errno(ret);
3714
3715 /*
3716 * In the situation that the right_rec is empty and the extent
3717 * block is empty also, ocfs2_complete_edge_insert can't handle
3718 * it and we need to delete the right extent block.
3719 */
3720 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3721 le16_to_cpu(el->l_next_free_rec) == 1) {
3722
Joel Becker70f18c02009-02-13 02:09:31 -08003723 ret = ocfs2_remove_rightmost_path(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003724 right_path,
Joel Becker70f18c02009-02-13 02:09:31 -08003725 dealloc);
Tao Ma677b9752008-01-30 14:21:05 +08003726 if (ret) {
3727 mlog_errno(ret);
3728 goto out;
3729 }
3730
3731 /* Now the rightmost extent block has been deleted.
3732 * So we use the new rightmost path.
3733 */
3734 ocfs2_mv_path(right_path, left_path);
3735 left_path = NULL;
3736 } else
Joel Becker4619c732009-02-12 19:02:36 -08003737 ocfs2_complete_edge_insert(handle, left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003738 right_path, subtree_index);
3739 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003740out:
Tao Ma677b9752008-01-30 14:21:05 +08003741 if (left_path)
3742 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003743 return ret;
3744}
3745
Joel Beckerc495dd22009-02-13 02:19:11 -08003746static int ocfs2_try_to_merge_extent(handle_t *handle,
3747 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003748 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003749 int split_index,
3750 struct ocfs2_extent_rec *split_rec,
3751 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckerc495dd22009-02-13 02:19:11 -08003752 struct ocfs2_merge_ctxt *ctxt)
Mark Fasheh328d5752007-06-18 10:48:04 -07003753{
Tao Mao518d7262007-08-28 17:25:35 -07003754 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003755 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003756 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3757
3758 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3759
Tao Mao518d7262007-08-28 17:25:35 -07003760 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
3761 /*
3762 * The merge code will need to create an empty
3763 * extent to take the place of the newly
3764 * emptied slot. Remove any pre-existing empty
3765 * extents - having more than one in a leaf is
3766 * illegal.
3767 */
Joel Becker70f18c02009-02-13 02:09:31 -08003768 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Tao Mao518d7262007-08-28 17:25:35 -07003769 if (ret) {
3770 mlog_errno(ret);
3771 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003772 }
Tao Mao518d7262007-08-28 17:25:35 -07003773 split_index--;
3774 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003775 }
3776
3777 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3778 /*
3779 * Left-right contig implies this.
3780 */
3781 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003782
3783 /*
3784 * Since the leftright insert always covers the entire
3785 * extent, this call will delete the insert record
3786 * entirely, resulting in an empty extent record added to
3787 * the extent block.
3788 *
3789 * Since the adding of an empty extent shifts
3790 * everything back to the right, there's no need to
3791 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003792 *
3793 * When the split_index is zero, we need to merge it to the
3794 * prevoius extent block. It is more efficient and easier
3795 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003796 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003797 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003798 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003799 if (ret) {
3800 mlog_errno(ret);
3801 goto out;
3802 }
3803
3804 /*
3805 * We can only get this from logic error above.
3806 */
3807 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3808
Tao Ma677b9752008-01-30 14:21:05 +08003809 /* The merge left us with an empty extent, remove it. */
Joel Becker70f18c02009-02-13 02:09:31 -08003810 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003811 if (ret) {
3812 mlog_errno(ret);
3813 goto out;
3814 }
Tao Ma677b9752008-01-30 14:21:05 +08003815
Mark Fasheh328d5752007-06-18 10:48:04 -07003816 rec = &el->l_recs[split_index];
3817
3818 /*
3819 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003820 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003821 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003822 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3823 dealloc, split_index);
Tao Ma677b9752008-01-30 14:21:05 +08003824
Mark Fasheh328d5752007-06-18 10:48:04 -07003825 if (ret) {
3826 mlog_errno(ret);
3827 goto out;
3828 }
3829
Joel Becker70f18c02009-02-13 02:09:31 -08003830 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003831 /*
3832 * Error from this last rotate is not critical, so
3833 * print but don't bubble it up.
3834 */
3835 if (ret)
3836 mlog_errno(ret);
3837 ret = 0;
3838 } else {
3839 /*
3840 * Merge a record to the left or right.
3841 *
3842 * 'contig_type' is relative to the existing record,
3843 * so for example, if we're "right contig", it's to
3844 * the record on the left (hence the left merge).
3845 */
3846 if (ctxt->c_contig_type == CONTIG_RIGHT) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003847 ret = ocfs2_merge_rec_left(path, handle, et,
3848 split_rec, dealloc,
Mark Fasheh328d5752007-06-18 10:48:04 -07003849 split_index);
3850 if (ret) {
3851 mlog_errno(ret);
3852 goto out;
3853 }
3854 } else {
Joel Becker4fe82c32009-02-13 02:16:08 -08003855 ret = ocfs2_merge_rec_right(path, handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003856 et, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003857 split_index);
3858 if (ret) {
3859 mlog_errno(ret);
3860 goto out;
3861 }
3862 }
3863
3864 if (ctxt->c_split_covers_rec) {
3865 /*
3866 * The merge may have left an empty extent in
3867 * our leaf. Try to rotate it away.
3868 */
Joel Becker70f18c02009-02-13 02:09:31 -08003869 ret = ocfs2_rotate_tree_left(handle, et, path,
3870 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003871 if (ret)
3872 mlog_errno(ret);
3873 ret = 0;
3874 }
3875 }
3876
3877out:
3878 return ret;
3879}
3880
3881static void ocfs2_subtract_from_rec(struct super_block *sb,
3882 enum ocfs2_split_type split,
3883 struct ocfs2_extent_rec *rec,
3884 struct ocfs2_extent_rec *split_rec)
3885{
3886 u64 len_blocks;
3887
3888 len_blocks = ocfs2_clusters_to_blocks(sb,
3889 le16_to_cpu(split_rec->e_leaf_clusters));
3890
3891 if (split == SPLIT_LEFT) {
3892 /*
3893 * Region is on the left edge of the existing
3894 * record.
3895 */
3896 le32_add_cpu(&rec->e_cpos,
3897 le16_to_cpu(split_rec->e_leaf_clusters));
3898 le64_add_cpu(&rec->e_blkno, len_blocks);
3899 le16_add_cpu(&rec->e_leaf_clusters,
3900 -le16_to_cpu(split_rec->e_leaf_clusters));
3901 } else {
3902 /*
3903 * Region is on the right edge of the existing
3904 * record.
3905 */
3906 le16_add_cpu(&rec->e_leaf_clusters,
3907 -le16_to_cpu(split_rec->e_leaf_clusters));
3908 }
3909}
3910
Mark Fashehdcd05382007-01-16 11:32:23 -08003911/*
3912 * Do the final bits of extent record insertion at the target leaf
3913 * list. If this leaf is part of an allocation tree, it is assumed
3914 * that the tree above has been prepared.
3915 */
Joel Beckerd5628622009-02-13 02:54:36 -08003916static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3917 struct ocfs2_extent_rec *insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08003918 struct ocfs2_extent_list *el,
Joel Beckerd5628622009-02-13 02:54:36 -08003919 struct ocfs2_insert_type *insert)
Mark Fashehdcd05382007-01-16 11:32:23 -08003920{
3921 int i = insert->ins_contig_index;
3922 unsigned int range;
3923 struct ocfs2_extent_rec *rec;
3924
Mark Fashehe48edee2007-03-07 16:46:57 -08003925 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003926
Mark Fasheh328d5752007-06-18 10:48:04 -07003927 if (insert->ins_split != SPLIT_NONE) {
3928 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3929 BUG_ON(i == -1);
3930 rec = &el->l_recs[i];
Joel Beckerd5628622009-02-13 02:54:36 -08003931 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3932 insert->ins_split, rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003933 insert_rec);
3934 goto rotate;
3935 }
3936
Mark Fashehdcd05382007-01-16 11:32:23 -08003937 /*
3938 * Contiguous insert - either left or right.
3939 */
3940 if (insert->ins_contig != CONTIG_NONE) {
3941 rec = &el->l_recs[i];
3942 if (insert->ins_contig == CONTIG_LEFT) {
3943 rec->e_blkno = insert_rec->e_blkno;
3944 rec->e_cpos = insert_rec->e_cpos;
3945 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003946 le16_add_cpu(&rec->e_leaf_clusters,
3947 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003948 return;
3949 }
3950
3951 /*
3952 * Handle insert into an empty leaf.
3953 */
3954 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3955 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3956 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3957 el->l_recs[0] = *insert_rec;
3958 el->l_next_free_rec = cpu_to_le16(1);
3959 return;
3960 }
3961
3962 /*
3963 * Appending insert.
3964 */
3965 if (insert->ins_appending == APPEND_TAIL) {
3966 i = le16_to_cpu(el->l_next_free_rec) - 1;
3967 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003968 range = le32_to_cpu(rec->e_cpos)
3969 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003970 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3971
3972 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3973 le16_to_cpu(el->l_count),
Joel Beckerd5628622009-02-13 02:54:36 -08003974 "owner %llu, depth %u, count %u, next free %u, "
Mark Fashehdcd05382007-01-16 11:32:23 -08003975 "rec.cpos %u, rec.clusters %u, "
3976 "insert.cpos %u, insert.clusters %u\n",
Joel Beckerd5628622009-02-13 02:54:36 -08003977 ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08003978 le16_to_cpu(el->l_tree_depth),
3979 le16_to_cpu(el->l_count),
3980 le16_to_cpu(el->l_next_free_rec),
3981 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003982 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003983 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003984 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003985 i++;
3986 el->l_recs[i] = *insert_rec;
3987 le16_add_cpu(&el->l_next_free_rec, 1);
3988 return;
3989 }
3990
Mark Fasheh328d5752007-06-18 10:48:04 -07003991rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003992 /*
3993 * Ok, we have to rotate.
3994 *
3995 * At this point, it is safe to assume that inserting into an
3996 * empty leaf and appending to a leaf have both been handled
3997 * above.
3998 *
3999 * This leaf needs to have space, either by the empty 1st
4000 * extent record, or by virtue of an l_next_rec < l_count.
4001 */
4002 ocfs2_rotate_leaf(el, insert_rec);
4003}
4004
Joel Beckerd401dc12009-02-13 02:24:10 -08004005static void ocfs2_adjust_rightmost_records(handle_t *handle,
4006 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004007 struct ocfs2_path *path,
4008 struct ocfs2_extent_rec *insert_rec)
4009{
4010 int ret, i, next_free;
4011 struct buffer_head *bh;
4012 struct ocfs2_extent_list *el;
4013 struct ocfs2_extent_rec *rec;
4014
4015 /*
4016 * Update everything except the leaf block.
4017 */
4018 for (i = 0; i < path->p_tree_depth; i++) {
4019 bh = path->p_node[i].bh;
4020 el = path->p_node[i].el;
4021
4022 next_free = le16_to_cpu(el->l_next_free_rec);
4023 if (next_free == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004024 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
4025 "Owner %llu has a bad extent list",
4026 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fasheh328d5752007-06-18 10:48:04 -07004027 ret = -EIO;
4028 return;
4029 }
4030
4031 rec = &el->l_recs[next_free - 1];
4032
4033 rec->e_int_clusters = insert_rec->e_cpos;
4034 le32_add_cpu(&rec->e_int_clusters,
4035 le16_to_cpu(insert_rec->e_leaf_clusters));
4036 le32_add_cpu(&rec->e_int_clusters,
4037 -le32_to_cpu(rec->e_cpos));
4038
4039 ret = ocfs2_journal_dirty(handle, bh);
4040 if (ret)
4041 mlog_errno(ret);
4042
4043 }
4044}
4045
Joel Beckerd401dc12009-02-13 02:24:10 -08004046static int ocfs2_append_rec_to_path(handle_t *handle,
4047 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004048 struct ocfs2_extent_rec *insert_rec,
4049 struct ocfs2_path *right_path,
4050 struct ocfs2_path **ret_left_path)
4051{
Mark Fasheh328d5752007-06-18 10:48:04 -07004052 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08004053 struct ocfs2_extent_list *el;
4054 struct ocfs2_path *left_path = NULL;
4055
4056 *ret_left_path = NULL;
4057
4058 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08004059 * This shouldn't happen for non-trees. The extent rec cluster
4060 * count manipulation below only works for interior nodes.
4061 */
4062 BUG_ON(right_path->p_tree_depth == 0);
4063
4064 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08004065 * If our appending insert is at the leftmost edge of a leaf,
4066 * then we might need to update the rightmost records of the
4067 * neighboring path.
4068 */
4069 el = path_leaf_el(right_path);
4070 next_free = le16_to_cpu(el->l_next_free_rec);
4071 if (next_free == 0 ||
4072 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
4073 u32 left_cpos;
4074
Joel Beckerd401dc12009-02-13 02:24:10 -08004075 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
4076 right_path, &left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004077 if (ret) {
4078 mlog_errno(ret);
4079 goto out;
4080 }
4081
4082 mlog(0, "Append may need a left path update. cpos: %u, "
4083 "left_cpos: %u\n", le32_to_cpu(insert_rec->e_cpos),
4084 left_cpos);
4085
4086 /*
4087 * No need to worry if the append is already in the
4088 * leftmost leaf.
4089 */
4090 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004091 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004092 if (!left_path) {
4093 ret = -ENOMEM;
4094 mlog_errno(ret);
4095 goto out;
4096 }
4097
Joel Beckerd401dc12009-02-13 02:24:10 -08004098 ret = ocfs2_find_path(et->et_ci, left_path,
Joel Beckerfacdb772009-02-12 18:08:48 -08004099 left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004100 if (ret) {
4101 mlog_errno(ret);
4102 goto out;
4103 }
4104
4105 /*
4106 * ocfs2_insert_path() will pass the left_path to the
4107 * journal for us.
4108 */
4109 }
4110 }
4111
Joel Beckerd401dc12009-02-13 02:24:10 -08004112 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004113 if (ret) {
4114 mlog_errno(ret);
4115 goto out;
4116 }
4117
Joel Beckerd401dc12009-02-13 02:24:10 -08004118 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004119
4120 *ret_left_path = left_path;
4121 ret = 0;
4122out:
4123 if (ret != 0)
4124 ocfs2_free_path(left_path);
4125
4126 return ret;
4127}
4128
Joel Beckerc38e52b2009-02-13 02:56:23 -08004129static void ocfs2_split_record(struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004130 struct ocfs2_path *left_path,
4131 struct ocfs2_path *right_path,
4132 struct ocfs2_extent_rec *split_rec,
4133 enum ocfs2_split_type split)
4134{
4135 int index;
4136 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4137 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4138 struct ocfs2_extent_rec *rec, *tmprec;
4139
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004140 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004141 if (left_path)
4142 left_el = path_leaf_el(left_path);
4143
4144 el = right_el;
4145 insert_el = right_el;
4146 index = ocfs2_search_extent_list(el, cpos);
4147 if (index != -1) {
4148 if (index == 0 && left_path) {
4149 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4150
4151 /*
4152 * This typically means that the record
4153 * started in the left path but moved to the
4154 * right as a result of rotation. We either
4155 * move the existing record to the left, or we
4156 * do the later insert there.
4157 *
4158 * In this case, the left path should always
4159 * exist as the rotate code will have passed
4160 * it back for a post-insert update.
4161 */
4162
4163 if (split == SPLIT_LEFT) {
4164 /*
4165 * It's a left split. Since we know
4166 * that the rotate code gave us an
4167 * empty extent in the left path, we
4168 * can just do the insert there.
4169 */
4170 insert_el = left_el;
4171 } else {
4172 /*
4173 * Right split - we have to move the
4174 * existing record over to the left
4175 * leaf. The insert will be into the
4176 * newly created empty extent in the
4177 * right leaf.
4178 */
4179 tmprec = &right_el->l_recs[index];
4180 ocfs2_rotate_leaf(left_el, tmprec);
4181 el = left_el;
4182
4183 memset(tmprec, 0, sizeof(*tmprec));
4184 index = ocfs2_search_extent_list(left_el, cpos);
4185 BUG_ON(index == -1);
4186 }
4187 }
4188 } else {
4189 BUG_ON(!left_path);
4190 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4191 /*
4192 * Left path is easy - we can just allow the insert to
4193 * happen.
4194 */
4195 el = left_el;
4196 insert_el = left_el;
4197 index = ocfs2_search_extent_list(el, cpos);
4198 BUG_ON(index == -1);
4199 }
4200
4201 rec = &el->l_recs[index];
Joel Beckerc38e52b2009-02-13 02:56:23 -08004202 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4203 split, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004204 ocfs2_rotate_leaf(insert_el, split_rec);
4205}
4206
Mark Fashehdcd05382007-01-16 11:32:23 -08004207/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004208 * This function only does inserts on an allocation b-tree. For tree
4209 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004210 *
4211 * right_path is the path we want to do the actual insert
4212 * in. left_path should only be passed in if we need to update that
4213 * portion of the tree after an edge insert.
4214 */
Joel Becker3505bec2009-02-13 02:57:58 -08004215static int ocfs2_insert_path(handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08004216 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004217 struct ocfs2_path *left_path,
4218 struct ocfs2_path *right_path,
4219 struct ocfs2_extent_rec *insert_rec,
4220 struct ocfs2_insert_type *insert)
4221{
4222 int ret, subtree_index;
4223 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004224
Mark Fashehdcd05382007-01-16 11:32:23 -08004225 if (left_path) {
4226 int credits = handle->h_buffer_credits;
4227
4228 /*
4229 * There's a chance that left_path got passed back to
4230 * us without being accounted for in the
4231 * journal. Extend our transaction here to be sure we
4232 * can change those blocks.
4233 */
4234 credits += left_path->p_tree_depth;
4235
4236 ret = ocfs2_extend_trans(handle, credits);
4237 if (ret < 0) {
4238 mlog_errno(ret);
4239 goto out;
4240 }
4241
Joel Becker7dc02802009-02-12 19:20:13 -08004242 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004243 if (ret < 0) {
4244 mlog_errno(ret);
4245 goto out;
4246 }
4247 }
4248
Mark Fashehe8aed342007-12-03 16:43:01 -08004249 /*
4250 * Pass both paths to the journal. The majority of inserts
4251 * will be touching all components anyway.
4252 */
Joel Becker7dc02802009-02-12 19:20:13 -08004253 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004254 if (ret < 0) {
4255 mlog_errno(ret);
4256 goto out;
4257 }
4258
Mark Fasheh328d5752007-06-18 10:48:04 -07004259 if (insert->ins_split != SPLIT_NONE) {
4260 /*
4261 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004262 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004263 * function sort it all out.
4264 */
Joel Beckerc38e52b2009-02-13 02:56:23 -08004265 ocfs2_split_record(et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004266 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004267
4268 /*
4269 * Split might have modified either leaf and we don't
4270 * have a guarantee that the later edge insert will
4271 * dirty this for us.
4272 */
4273 if (left_path)
4274 ret = ocfs2_journal_dirty(handle,
4275 path_leaf_bh(left_path));
4276 if (ret)
4277 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07004278 } else
Joel Beckerd5628622009-02-13 02:54:36 -08004279 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4280 insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004281
Mark Fashehdcd05382007-01-16 11:32:23 -08004282 ret = ocfs2_journal_dirty(handle, leaf_bh);
4283 if (ret)
4284 mlog_errno(ret);
4285
4286 if (left_path) {
4287 /*
4288 * The rotate code has indicated that we need to fix
4289 * up portions of the tree after the insert.
4290 *
4291 * XXX: Should we extend the transaction here?
4292 */
Joel Becker7dc02802009-02-12 19:20:13 -08004293 subtree_index = ocfs2_find_subtree_root(et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004294 right_path);
Joel Becker4619c732009-02-12 19:02:36 -08004295 ocfs2_complete_edge_insert(handle, left_path, right_path,
4296 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08004297 }
4298
4299 ret = 0;
4300out:
4301 return ret;
4302}
4303
Joel Becker3505bec2009-02-13 02:57:58 -08004304static int ocfs2_do_insert_extent(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004305 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004306 struct ocfs2_extent_rec *insert_rec,
4307 struct ocfs2_insert_type *type)
4308{
4309 int ret, rotate = 0;
4310 u32 cpos;
4311 struct ocfs2_path *right_path = NULL;
4312 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004313 struct ocfs2_extent_list *el;
4314
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004315 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004316
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004317 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004318 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004319 if (ret) {
4320 mlog_errno(ret);
4321 goto out;
4322 }
4323
4324 if (le16_to_cpu(el->l_tree_depth) == 0) {
Joel Beckerd5628622009-02-13 02:54:36 -08004325 ocfs2_insert_at_leaf(et, insert_rec, el, type);
Mark Fashehdcd05382007-01-16 11:32:23 -08004326 goto out_update_clusters;
4327 }
4328
Joel Beckerffdd7a52008-10-17 22:32:01 -07004329 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004330 if (!right_path) {
4331 ret = -ENOMEM;
4332 mlog_errno(ret);
4333 goto out;
4334 }
4335
4336 /*
4337 * Determine the path to start with. Rotations need the
4338 * rightmost path, everything else can go directly to the
4339 * target leaf.
4340 */
4341 cpos = le32_to_cpu(insert_rec->e_cpos);
4342 if (type->ins_appending == APPEND_NONE &&
4343 type->ins_contig == CONTIG_NONE) {
4344 rotate = 1;
4345 cpos = UINT_MAX;
4346 }
4347
Joel Beckerfacdb772009-02-12 18:08:48 -08004348 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004349 if (ret) {
4350 mlog_errno(ret);
4351 goto out;
4352 }
4353
4354 /*
4355 * Rotations and appends need special treatment - they modify
4356 * parts of the tree's above them.
4357 *
4358 * Both might pass back a path immediate to the left of the
4359 * one being inserted to. This will be cause
4360 * ocfs2_insert_path() to modify the rightmost records of
4361 * left_path to account for an edge insert.
4362 *
4363 * XXX: When modifying this code, keep in mind that an insert
4364 * can wind up skipping both of these two special cases...
4365 */
4366 if (rotate) {
Joel Becker1bbf0b82009-02-12 19:42:08 -08004367 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004368 le32_to_cpu(insert_rec->e_cpos),
4369 right_path, &left_path);
4370 if (ret) {
4371 mlog_errno(ret);
4372 goto out;
4373 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004374
4375 /*
4376 * ocfs2_rotate_tree_right() might have extended the
4377 * transaction without re-journaling our tree root.
4378 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004379 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004380 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004381 if (ret) {
4382 mlog_errno(ret);
4383 goto out;
4384 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004385 } else if (type->ins_appending == APPEND_TAIL
4386 && type->ins_contig != CONTIG_LEFT) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004387 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08004388 right_path, &left_path);
4389 if (ret) {
4390 mlog_errno(ret);
4391 goto out;
4392 }
4393 }
4394
Joel Becker3505bec2009-02-13 02:57:58 -08004395 ret = ocfs2_insert_path(handle, et, left_path, right_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004396 insert_rec, type);
4397 if (ret) {
4398 mlog_errno(ret);
4399 goto out;
4400 }
4401
4402out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004403 if (type->ins_split == SPLIT_NONE)
Joel Becker6136ca52009-02-12 19:32:43 -08004404 ocfs2_et_update_clusters(et,
Joel Becker35dc0aa2008-08-20 16:25:06 -07004405 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004406
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004407 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004408 if (ret)
4409 mlog_errno(ret);
4410
4411out:
4412 ocfs2_free_path(left_path);
4413 ocfs2_free_path(right_path);
4414
4415 return ret;
4416}
4417
Mark Fasheh328d5752007-06-18 10:48:04 -07004418static enum ocfs2_contig_type
Joel Beckera2970292009-02-13 03:09:54 -08004419ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
4420 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004421 struct ocfs2_extent_list *el, int index,
4422 struct ocfs2_extent_rec *split_rec)
4423{
Tao Maad5a4d72008-01-30 14:21:32 +08004424 int status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004425 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004426 u32 left_cpos, right_cpos;
4427 struct ocfs2_extent_rec *rec = NULL;
4428 struct ocfs2_extent_list *new_el;
4429 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4430 struct buffer_head *bh;
4431 struct ocfs2_extent_block *eb;
Joel Beckera2970292009-02-13 03:09:54 -08004432 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Tao Maad5a4d72008-01-30 14:21:32 +08004433
4434 if (index > 0) {
4435 rec = &el->l_recs[index - 1];
4436 } else if (path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004437 status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004438 if (status)
4439 goto out;
4440
4441 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004442 left_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004443 if (!left_path)
4444 goto out;
4445
Joel Beckera2970292009-02-13 03:09:54 -08004446 status = ocfs2_find_path(et->et_ci, left_path,
4447 left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004448 if (status)
4449 goto out;
4450
4451 new_el = path_leaf_el(left_path);
4452
4453 if (le16_to_cpu(new_el->l_next_free_rec) !=
4454 le16_to_cpu(new_el->l_count)) {
4455 bh = path_leaf_bh(left_path);
4456 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004457 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004458 "Extent block #%llu has an "
4459 "invalid l_next_free_rec of "
4460 "%d. It should have "
4461 "matched the l_count of %d",
4462 (unsigned long long)le64_to_cpu(eb->h_blkno),
4463 le16_to_cpu(new_el->l_next_free_rec),
4464 le16_to_cpu(new_el->l_count));
4465 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004466 goto out;
4467 }
4468 rec = &new_el->l_recs[
4469 le16_to_cpu(new_el->l_next_free_rec) - 1];
4470 }
4471 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004472
4473 /*
4474 * We're careful to check for an empty extent record here -
4475 * the merge code will know what to do if it sees one.
4476 */
Tao Maad5a4d72008-01-30 14:21:32 +08004477 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004478 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4479 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4480 ret = CONTIG_RIGHT;
4481 } else {
Tao Ma853a3a12009-08-18 11:22:18 +08004482 ret = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004483 }
4484 }
4485
Tao Maad5a4d72008-01-30 14:21:32 +08004486 rec = NULL;
4487 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4488 rec = &el->l_recs[index + 1];
4489 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4490 path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004491 status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004492 if (status)
4493 goto out;
4494
4495 if (right_cpos == 0)
4496 goto out;
4497
Joel Beckerffdd7a52008-10-17 22:32:01 -07004498 right_path = ocfs2_new_path_from_path(path);
Tao Maad5a4d72008-01-30 14:21:32 +08004499 if (!right_path)
4500 goto out;
4501
Joel Beckera2970292009-02-13 03:09:54 -08004502 status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004503 if (status)
4504 goto out;
4505
4506 new_el = path_leaf_el(right_path);
4507 rec = &new_el->l_recs[0];
4508 if (ocfs2_is_empty_extent(rec)) {
4509 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4510 bh = path_leaf_bh(right_path);
4511 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004512 ocfs2_error(sb,
Joel Becker5e965812008-11-13 14:49:16 -08004513 "Extent block #%llu has an "
4514 "invalid l_next_free_rec of %d",
4515 (unsigned long long)le64_to_cpu(eb->h_blkno),
4516 le16_to_cpu(new_el->l_next_free_rec));
4517 status = -EINVAL;
Tao Maad5a4d72008-01-30 14:21:32 +08004518 goto out;
4519 }
4520 rec = &new_el->l_recs[1];
4521 }
4522 }
4523
4524 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004525 enum ocfs2_contig_type contig_type;
4526
Tao Ma853a3a12009-08-18 11:22:18 +08004527 contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004528
4529 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4530 ret = CONTIG_LEFTRIGHT;
4531 else if (ret == CONTIG_NONE)
4532 ret = contig_type;
4533 }
4534
Tao Maad5a4d72008-01-30 14:21:32 +08004535out:
4536 if (left_path)
4537 ocfs2_free_path(left_path);
4538 if (right_path)
4539 ocfs2_free_path(right_path);
4540
Mark Fasheh328d5752007-06-18 10:48:04 -07004541 return ret;
4542}
4543
Joel Becker1ef61b32009-02-13 03:12:33 -08004544static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004545 struct ocfs2_insert_type *insert,
4546 struct ocfs2_extent_list *el,
Joel Becker1ef61b32009-02-13 03:12:33 -08004547 struct ocfs2_extent_rec *insert_rec)
Mark Fashehdcd05382007-01-16 11:32:23 -08004548{
4549 int i;
4550 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4551
Mark Fashehe48edee2007-03-07 16:46:57 -08004552 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4553
Mark Fashehdcd05382007-01-16 11:32:23 -08004554 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
Tao Ma853a3a12009-08-18 11:22:18 +08004555 contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4556 insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004557 if (contig_type != CONTIG_NONE) {
4558 insert->ins_contig_index = i;
4559 break;
4560 }
4561 }
4562 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004563
4564 if (insert->ins_contig != CONTIG_NONE) {
4565 struct ocfs2_extent_rec *rec =
4566 &el->l_recs[insert->ins_contig_index];
4567 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4568 le16_to_cpu(insert_rec->e_leaf_clusters);
4569
4570 /*
4571 * Caller might want us to limit the size of extents, don't
4572 * calculate contiguousness if we might exceed that limit.
4573 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004574 if (et->et_max_leaf_clusters &&
4575 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004576 insert->ins_contig = CONTIG_NONE;
4577 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004578}
4579
4580/*
4581 * This should only be called against the righmost leaf extent list.
4582 *
4583 * ocfs2_figure_appending_type() will figure out whether we'll have to
4584 * insert at the tail of the rightmost leaf.
4585 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004586 * This should also work against the root extent list for tree's with 0
4587 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004588 * then the logic here makes sense.
4589 */
4590static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4591 struct ocfs2_extent_list *el,
4592 struct ocfs2_extent_rec *insert_rec)
4593{
4594 int i;
4595 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4596 struct ocfs2_extent_rec *rec;
4597
4598 insert->ins_appending = APPEND_NONE;
4599
Mark Fashehe48edee2007-03-07 16:46:57 -08004600 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004601
4602 if (!el->l_next_free_rec)
4603 goto set_tail_append;
4604
4605 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4606 /* Were all records empty? */
4607 if (le16_to_cpu(el->l_next_free_rec) == 1)
4608 goto set_tail_append;
4609 }
4610
4611 i = le16_to_cpu(el->l_next_free_rec) - 1;
4612 rec = &el->l_recs[i];
4613
Mark Fashehe48edee2007-03-07 16:46:57 -08004614 if (cpos >=
4615 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004616 goto set_tail_append;
4617
4618 return;
4619
4620set_tail_append:
4621 insert->ins_appending = APPEND_TAIL;
4622}
4623
4624/*
4625 * Helper function called at the begining of an insert.
4626 *
4627 * This computes a few things that are commonly used in the process of
4628 * inserting into the btree:
4629 * - Whether the new extent is contiguous with an existing one.
4630 * - The current tree depth.
4631 * - Whether the insert is an appending one.
4632 * - The total # of free records in the tree.
4633 *
4634 * All of the information is stored on the ocfs2_insert_type
4635 * structure.
4636 */
Joel Becker627961b2009-02-13 03:14:38 -08004637static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004638 struct buffer_head **last_eb_bh,
4639 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004640 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004641 struct ocfs2_insert_type *insert)
4642{
4643 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004644 struct ocfs2_extent_block *eb;
4645 struct ocfs2_extent_list *el;
4646 struct ocfs2_path *path = NULL;
4647 struct buffer_head *bh = NULL;
4648
Mark Fasheh328d5752007-06-18 10:48:04 -07004649 insert->ins_split = SPLIT_NONE;
4650
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004651 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004652 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4653
4654 if (el->l_tree_depth) {
4655 /*
4656 * If we have tree depth, we read in the
4657 * rightmost extent block ahead of time as
4658 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4659 * may want it later.
4660 */
Joel Becker3d03a302009-02-12 17:49:26 -08004661 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08004662 ocfs2_et_get_last_eb_blk(et),
4663 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004664 if (ret) {
4665 mlog_exit(ret);
4666 goto out;
4667 }
4668 eb = (struct ocfs2_extent_block *) bh->b_data;
4669 el = &eb->h_list;
4670 }
4671
4672 /*
4673 * Unless we have a contiguous insert, we'll need to know if
4674 * there is room left in our allocation tree for another
4675 * extent record.
4676 *
4677 * XXX: This test is simplistic, we can search for empty
4678 * extent records too.
4679 */
Tao Maoc77534f2007-08-28 17:22:33 -07004680 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004681 le16_to_cpu(el->l_next_free_rec);
4682
4683 if (!insert->ins_tree_depth) {
Joel Becker1ef61b32009-02-13 03:12:33 -08004684 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004685 ocfs2_figure_appending_type(insert, el, insert_rec);
4686 return 0;
4687 }
4688
Joel Beckerffdd7a52008-10-17 22:32:01 -07004689 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004690 if (!path) {
4691 ret = -ENOMEM;
4692 mlog_errno(ret);
4693 goto out;
4694 }
4695
4696 /*
4697 * In the case that we're inserting past what the tree
4698 * currently accounts for, ocfs2_find_path() will return for
4699 * us the rightmost tree path. This is accounted for below in
4700 * the appending code.
4701 */
Joel Beckerfacdb772009-02-12 18:08:48 -08004702 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -08004703 if (ret) {
4704 mlog_errno(ret);
4705 goto out;
4706 }
4707
4708 el = path_leaf_el(path);
4709
4710 /*
4711 * Now that we have the path, there's two things we want to determine:
4712 * 1) Contiguousness (also set contig_index if this is so)
4713 *
4714 * 2) Are we doing an append? We can trivially break this up
4715 * into two types of appends: simple record append, or a
4716 * rotate inside the tail leaf.
4717 */
Joel Becker1ef61b32009-02-13 03:12:33 -08004718 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004719
4720 /*
4721 * The insert code isn't quite ready to deal with all cases of
4722 * left contiguousness. Specifically, if it's an insert into
4723 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004724 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004725 * left. For now, just catch that case and fool the layers
4726 * above us. This works just fine for tree_depth == 0, which
4727 * is why we allow that above.
4728 */
4729 if (insert->ins_contig == CONTIG_LEFT &&
4730 insert->ins_contig_index == 0)
4731 insert->ins_contig = CONTIG_NONE;
4732
4733 /*
4734 * Ok, so we can simply compare against last_eb to figure out
4735 * whether the path doesn't exist. This will only happen in
4736 * the case that we're doing a tail append, so maybe we can
4737 * take advantage of that information somehow.
4738 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004739 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004740 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004741 /*
4742 * Ok, ocfs2_find_path() returned us the rightmost
4743 * tree path. This might be an appending insert. There are
4744 * two cases:
4745 * 1) We're doing a true append at the tail:
4746 * -This might even be off the end of the leaf
4747 * 2) We're "appending" by rotating in the tail
4748 */
4749 ocfs2_figure_appending_type(insert, el, insert_rec);
4750 }
4751
4752out:
4753 ocfs2_free_path(path);
4754
4755 if (ret == 0)
4756 *last_eb_bh = bh;
4757 else
4758 brelse(bh);
4759 return ret;
4760}
4761
4762/*
Joel Beckercc79d8c2009-02-13 03:24:43 -08004763 * Insert an extent into a btree.
Mark Fashehdcd05382007-01-16 11:32:23 -08004764 *
Joel Beckercc79d8c2009-02-13 03:24:43 -08004765 * The caller needs to update the owning btree's cluster count.
Mark Fashehdcd05382007-01-16 11:32:23 -08004766 */
Joel Beckercc79d8c2009-02-13 03:24:43 -08004767int ocfs2_insert_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004768 struct ocfs2_extent_tree *et,
4769 u32 cpos,
4770 u64 start_blk,
4771 u32 new_clusters,
4772 u8 flags,
4773 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004774{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004775 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004776 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004777 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004778 struct ocfs2_insert_type insert = {0, };
4779 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004780
Joel Beckercc79d8c2009-02-13 03:24:43 -08004781 mlog(0, "add %u clusters at position %u to owner %llu\n",
4782 new_clusters, cpos,
4783 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08004784
Mark Fashehe48edee2007-03-07 16:46:57 -08004785 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004786 rec.e_cpos = cpu_to_le32(cpos);
4787 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004788 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004789 rec.e_flags = flags;
Joel Becker6136ca52009-02-12 19:32:43 -08004790 status = ocfs2_et_insert_check(et, &rec);
Joel Becker1e61ee72008-08-20 18:32:45 -07004791 if (status) {
4792 mlog_errno(status);
4793 goto bail;
4794 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004795
Joel Becker627961b2009-02-13 03:14:38 -08004796 status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004797 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004798 if (status < 0) {
4799 mlog_errno(status);
4800 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004801 }
4802
Mark Fashehdcd05382007-01-16 11:32:23 -08004803 mlog(0, "Insert.appending: %u, Insert.Contig: %u, "
4804 "Insert.contig_index: %d, Insert.free_records: %d, "
4805 "Insert.tree_depth: %d\n",
4806 insert.ins_appending, insert.ins_contig, insert.ins_contig_index,
Tao Maoc77534f2007-08-28 17:22:33 -07004807 free_records, insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004808
Tao Maoc77534f2007-08-28 17:22:33 -07004809 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004810 status = ocfs2_grow_tree(handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004811 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004812 meta_ac);
4813 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004814 mlog_errno(status);
4815 goto bail;
4816 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004817 }
4818
Mark Fashehdcd05382007-01-16 11:32:23 -08004819 /* Finally, we can add clusters. This might rotate the tree for us. */
Joel Becker3505bec2009-02-13 02:57:58 -08004820 status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004821 if (status < 0)
4822 mlog_errno(status);
Joel Becker92ba4702009-02-13 03:18:34 -08004823 else
4824 ocfs2_et_extent_map_insert(et, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004825
4826bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004827 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004828
Tao Maf56654c2008-08-18 17:38:48 +08004829 mlog_exit(status);
4830 return status;
4831}
4832
Tao Ma0eb8d472008-08-18 17:38:45 +08004833/*
4834 * Allcate and add clusters into the extent b-tree.
4835 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004836 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d472008-08-18 17:38:45 +08004837 * it is not limited to the file storage. Any extent tree can use this
4838 * function if it implements the proper ocfs2_extent_tree.
4839 */
Joel Beckercbee7e12009-02-13 03:34:15 -08004840int ocfs2_add_clusters_in_btree(handle_t *handle,
4841 struct ocfs2_extent_tree *et,
Tao Ma0eb8d472008-08-18 17:38:45 +08004842 u32 *logical_offset,
4843 u32 clusters_to_add,
4844 int mark_unwritten,
Tao Ma0eb8d472008-08-18 17:38:45 +08004845 struct ocfs2_alloc_context *data_ac,
4846 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004847 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d472008-08-18 17:38:45 +08004848{
4849 int status = 0;
4850 int free_extents;
4851 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4852 u32 bit_off, num_bits;
4853 u64 block;
4854 u8 flags = 0;
Joel Beckercbee7e12009-02-13 03:34:15 -08004855 struct ocfs2_super *osb =
4856 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Tao Ma0eb8d472008-08-18 17:38:45 +08004857
4858 BUG_ON(!clusters_to_add);
4859
4860 if (mark_unwritten)
4861 flags = OCFS2_EXT_UNWRITTEN;
4862
Joel Becker3d03a302009-02-12 17:49:26 -08004863 free_extents = ocfs2_num_free_extents(osb, et);
Tao Ma0eb8d472008-08-18 17:38:45 +08004864 if (free_extents < 0) {
4865 status = free_extents;
4866 mlog_errno(status);
4867 goto leave;
4868 }
4869
4870 /* there are two cases which could cause us to EAGAIN in the
4871 * we-need-more-metadata case:
4872 * 1) we haven't reserved *any*
4873 * 2) we are so fragmented, we've needed to add metadata too
4874 * many times. */
4875 if (!free_extents && !meta_ac) {
4876 mlog(0, "we haven't reserved any metadata!\n");
4877 status = -EAGAIN;
4878 reason = RESTART_META;
4879 goto leave;
4880 } else if ((!free_extents)
4881 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004882 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Ma0eb8d472008-08-18 17:38:45 +08004883 mlog(0, "filesystem is really fragmented...\n");
4884 status = -EAGAIN;
4885 reason = RESTART_META;
4886 goto leave;
4887 }
4888
4889 status = __ocfs2_claim_clusters(osb, handle, data_ac, 1,
4890 clusters_to_add, &bit_off, &num_bits);
4891 if (status < 0) {
4892 if (status != -ENOSPC)
4893 mlog_errno(status);
4894 goto leave;
4895 }
4896
4897 BUG_ON(num_bits > clusters_to_add);
4898
Joel Becker13723d02008-10-17 19:25:01 -07004899 /* reserve our write early -- insert_extent may update the tree root */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004900 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004901 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d472008-08-18 17:38:45 +08004902 if (status < 0) {
4903 mlog_errno(status);
4904 goto leave;
4905 }
4906
4907 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Joel Beckercbee7e12009-02-13 03:34:15 -08004908 mlog(0, "Allocating %u clusters at block %u for owner %llu\n",
4909 num_bits, bit_off,
4910 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Joel Beckercc79d8c2009-02-13 03:24:43 -08004911 status = ocfs2_insert_extent(handle, et, *logical_offset, block,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004912 num_bits, flags, meta_ac);
Tao Ma0eb8d472008-08-18 17:38:45 +08004913 if (status < 0) {
4914 mlog_errno(status);
4915 goto leave;
4916 }
4917
Joel Beckerf99b9b72008-08-20 19:36:33 -07004918 status = ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d472008-08-18 17:38:45 +08004919 if (status < 0) {
4920 mlog_errno(status);
4921 goto leave;
4922 }
4923
4924 clusters_to_add -= num_bits;
4925 *logical_offset += num_bits;
4926
4927 if (clusters_to_add) {
4928 mlog(0, "need to alloc once more, wanted = %u\n",
4929 clusters_to_add);
4930 status = -EAGAIN;
4931 reason = RESTART_TRANS;
4932 }
4933
4934leave:
4935 mlog_exit(status);
4936 if (reason_ret)
4937 *reason_ret = reason;
4938 return status;
4939}
4940
Mark Fasheh328d5752007-06-18 10:48:04 -07004941static void ocfs2_make_right_split_rec(struct super_block *sb,
4942 struct ocfs2_extent_rec *split_rec,
4943 u32 cpos,
4944 struct ocfs2_extent_rec *rec)
4945{
4946 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4947 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4948
4949 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4950
4951 split_rec->e_cpos = cpu_to_le32(cpos);
4952 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4953
4954 split_rec->e_blkno = rec->e_blkno;
4955 le64_add_cpu(&split_rec->e_blkno,
4956 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4957
4958 split_rec->e_flags = rec->e_flags;
4959}
4960
Joel Beckerd2311292009-02-13 03:43:22 -08004961static int ocfs2_split_and_insert(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004962 struct ocfs2_extent_tree *et,
Joel Beckerd2311292009-02-13 03:43:22 -08004963 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004964 struct buffer_head **last_eb_bh,
4965 int split_index,
4966 struct ocfs2_extent_rec *orig_split_rec,
4967 struct ocfs2_alloc_context *meta_ac)
4968{
4969 int ret = 0, depth;
4970 unsigned int insert_range, rec_range, do_leftright = 0;
4971 struct ocfs2_extent_rec tmprec;
4972 struct ocfs2_extent_list *rightmost_el;
4973 struct ocfs2_extent_rec rec;
4974 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4975 struct ocfs2_insert_type insert;
4976 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004977
4978leftright:
4979 /*
4980 * Store a copy of the record on the stack - it might move
4981 * around as the tree is manipulated below.
4982 */
4983 rec = path_leaf_el(path)->l_recs[split_index];
4984
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004985 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004986
4987 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4988 if (depth) {
4989 BUG_ON(!(*last_eb_bh));
4990 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4991 rightmost_el = &eb->h_list;
4992 }
4993
4994 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4995 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004996 ret = ocfs2_grow_tree(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08004997 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004998 if (ret) {
4999 mlog_errno(ret);
5000 goto out;
5001 }
Mark Fasheh328d5752007-06-18 10:48:04 -07005002 }
5003
5004 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5005 insert.ins_appending = APPEND_NONE;
5006 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07005007 insert.ins_tree_depth = depth;
5008
5009 insert_range = le32_to_cpu(split_rec.e_cpos) +
5010 le16_to_cpu(split_rec.e_leaf_clusters);
5011 rec_range = le32_to_cpu(rec.e_cpos) +
5012 le16_to_cpu(rec.e_leaf_clusters);
5013
5014 if (split_rec.e_cpos == rec.e_cpos) {
5015 insert.ins_split = SPLIT_LEFT;
5016 } else if (insert_range == rec_range) {
5017 insert.ins_split = SPLIT_RIGHT;
5018 } else {
5019 /*
5020 * Left/right split. We fake this as a right split
5021 * first and then make a second pass as a left split.
5022 */
5023 insert.ins_split = SPLIT_RIGHT;
5024
Joel Beckerd2311292009-02-13 03:43:22 -08005025 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5026 &tmprec, insert_range, &rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005027
5028 split_rec = tmprec;
5029
5030 BUG_ON(do_leftright);
5031 do_leftright = 1;
5032 }
5033
Joel Becker3505bec2009-02-13 02:57:58 -08005034 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07005035 if (ret) {
5036 mlog_errno(ret);
5037 goto out;
5038 }
5039
5040 if (do_leftright == 1) {
5041 u32 cpos;
5042 struct ocfs2_extent_list *el;
5043
5044 do_leftright++;
5045 split_rec = *orig_split_rec;
5046
5047 ocfs2_reinit_path(path, 1);
5048
5049 cpos = le32_to_cpu(split_rec.e_cpos);
Joel Beckerfacdb772009-02-12 18:08:48 -08005050 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005051 if (ret) {
5052 mlog_errno(ret);
5053 goto out;
5054 }
5055
5056 el = path_leaf_el(path);
5057 split_index = ocfs2_search_extent_list(el, cpos);
5058 goto leftright;
5059 }
5060out:
5061
5062 return ret;
5063}
5064
Joel Beckerf3868d02009-02-17 19:46:04 -08005065static int ocfs2_replace_extent_rec(handle_t *handle,
5066 struct ocfs2_extent_tree *et,
Tao Ma47be12e2009-01-09 07:32:48 +08005067 struct ocfs2_path *path,
5068 struct ocfs2_extent_list *el,
5069 int split_index,
5070 struct ocfs2_extent_rec *split_rec)
5071{
5072 int ret;
5073
Joel Beckerf3868d02009-02-17 19:46:04 -08005074 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Tao Ma47be12e2009-01-09 07:32:48 +08005075 path_num_items(path) - 1);
5076 if (ret) {
5077 mlog_errno(ret);
5078 goto out;
5079 }
5080
5081 el->l_recs[split_index] = *split_rec;
5082
5083 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5084out:
5085 return ret;
5086}
5087
Mark Fasheh328d5752007-06-18 10:48:04 -07005088/*
Tao Ma555936b2009-08-18 11:22:21 +08005089 * Split part or all of the extent record at split_index in the leaf
5090 * pointed to by path. Merge with the contiguous extent record if needed.
Mark Fasheh328d5752007-06-18 10:48:04 -07005091 *
5092 * Care is taken to handle contiguousness so as to not grow the tree.
5093 *
5094 * meta_ac is not strictly necessary - we only truly need it if growth
5095 * of the tree is required. All other cases will degrade into a less
5096 * optimal tree layout.
5097 *
Tao Mae7d4cb62008-08-18 17:38:44 +08005098 * last_eb_bh should be the rightmost leaf block for any extent
5099 * btree. Since a split may grow the tree or a merge might shrink it,
5100 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07005101 *
5102 * This code is optimized for readability - several passes might be
5103 * made over certain portions of the tree. All of those blocks will
5104 * have been brought into cache (and pinned via the journal), so the
5105 * extra overhead is not expressed in terms of disk reads.
5106 */
Tao Ma555936b2009-08-18 11:22:21 +08005107static int __ocfs2_split_extent(handle_t *handle,
5108 struct ocfs2_extent_tree *et,
5109 struct ocfs2_path *path,
5110 int split_index,
5111 struct ocfs2_extent_rec *split_rec,
5112 struct ocfs2_alloc_context *meta_ac,
5113 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005114{
5115 int ret = 0;
5116 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08005117 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07005118 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5119 struct ocfs2_merge_ctxt ctxt;
5120 struct ocfs2_extent_list *rightmost_el;
5121
Mark Fasheh328d5752007-06-18 10:48:04 -07005122 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5123 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5124 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5125 ret = -EIO;
5126 mlog_errno(ret);
5127 goto out;
5128 }
5129
Joel Beckera2970292009-02-13 03:09:54 -08005130 ctxt.c_contig_type = ocfs2_figure_merge_contig_type(et, path, el,
Mark Fasheh328d5752007-06-18 10:48:04 -07005131 split_index,
5132 split_rec);
5133
5134 /*
5135 * The core merge / split code wants to know how much room is
Joel Beckera1cf0762009-02-13 03:45:49 -08005136 * left in this allocation tree, so we pass the
Mark Fasheh328d5752007-06-18 10:48:04 -07005137 * rightmost extent list.
5138 */
5139 if (path->p_tree_depth) {
5140 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005141
Joel Becker3d03a302009-02-12 17:49:26 -08005142 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005143 ocfs2_et_get_last_eb_blk(et),
5144 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005145 if (ret) {
5146 mlog_exit(ret);
5147 goto out;
5148 }
5149
5150 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005151 rightmost_el = &eb->h_list;
5152 } else
5153 rightmost_el = path_root_el(path);
5154
Mark Fasheh328d5752007-06-18 10:48:04 -07005155 if (rec->e_cpos == split_rec->e_cpos &&
5156 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5157 ctxt.c_split_covers_rec = 1;
5158 else
5159 ctxt.c_split_covers_rec = 0;
5160
5161 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5162
Mark Fasheh015452b2007-09-12 10:21:22 -07005163 mlog(0, "index: %d, contig: %u, has_empty: %u, split_covers: %u\n",
5164 split_index, ctxt.c_contig_type, ctxt.c_has_empty_extent,
5165 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005166
5167 if (ctxt.c_contig_type == CONTIG_NONE) {
5168 if (ctxt.c_split_covers_rec)
Joel Beckerf3868d02009-02-17 19:46:04 -08005169 ret = ocfs2_replace_extent_rec(handle, et, path, el,
Tao Ma47be12e2009-01-09 07:32:48 +08005170 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005171 else
Joel Beckerd2311292009-02-13 03:43:22 -08005172 ret = ocfs2_split_and_insert(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005173 &last_eb_bh, split_index,
5174 split_rec, meta_ac);
5175 if (ret)
5176 mlog_errno(ret);
5177 } else {
Joel Beckerc495dd22009-02-13 02:19:11 -08005178 ret = ocfs2_try_to_merge_extent(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005179 split_index, split_rec,
Joel Beckerc495dd22009-02-13 02:19:11 -08005180 dealloc, &ctxt);
Mark Fasheh328d5752007-06-18 10:48:04 -07005181 if (ret)
5182 mlog_errno(ret);
5183 }
5184
Mark Fasheh328d5752007-06-18 10:48:04 -07005185out:
5186 brelse(last_eb_bh);
5187 return ret;
5188}
5189
5190/*
Tao Ma555936b2009-08-18 11:22:21 +08005191 * Change the flags of the already-existing extent at cpos for len clusters.
5192 *
5193 * new_flags: the flags we want to set.
5194 * clear_flags: the flags we want to clear.
5195 * phys: the new physical offset we want this new extent starts from.
Mark Fasheh328d5752007-06-18 10:48:04 -07005196 *
5197 * If the existing extent is larger than the request, initiate a
5198 * split. An attempt will be made at merging with adjacent extents.
5199 *
5200 * The caller is responsible for passing down meta_ac if we'll need it.
5201 */
Tao Ma555936b2009-08-18 11:22:21 +08005202static int ocfs2_change_extent_flag(handle_t *handle,
5203 struct ocfs2_extent_tree *et,
5204 u32 cpos, u32 len, u32 phys,
5205 struct ocfs2_alloc_context *meta_ac,
5206 struct ocfs2_cached_dealloc_ctxt *dealloc,
5207 int new_flags, int clear_flags)
Mark Fasheh328d5752007-06-18 10:48:04 -07005208{
5209 int ret, index;
Tao Ma555936b2009-08-18 11:22:21 +08005210 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5211 u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
Mark Fasheh328d5752007-06-18 10:48:04 -07005212 struct ocfs2_extent_rec split_rec;
5213 struct ocfs2_path *left_path = NULL;
5214 struct ocfs2_extent_list *el;
Tao Ma555936b2009-08-18 11:22:21 +08005215 struct ocfs2_extent_rec *rec;
Mark Fasheh328d5752007-06-18 10:48:04 -07005216
Joel Beckerffdd7a52008-10-17 22:32:01 -07005217 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005218 if (!left_path) {
5219 ret = -ENOMEM;
5220 mlog_errno(ret);
5221 goto out;
5222 }
5223
Joel Beckerfacdb772009-02-12 18:08:48 -08005224 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005225 if (ret) {
5226 mlog_errno(ret);
5227 goto out;
5228 }
5229 el = path_leaf_el(left_path);
5230
5231 index = ocfs2_search_extent_list(el, cpos);
5232 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Tao Ma555936b2009-08-18 11:22:21 +08005233 ocfs2_error(sb,
5234 "Owner %llu has an extent at cpos %u which can no "
Mark Fasheh328d5752007-06-18 10:48:04 -07005235 "longer be found.\n",
Tao Ma555936b2009-08-18 11:22:21 +08005236 (unsigned long long)
5237 ocfs2_metadata_cache_owner(et->et_ci), cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005238 ret = -EROFS;
5239 goto out;
5240 }
5241
Tao Ma555936b2009-08-18 11:22:21 +08005242 ret = -EIO;
5243 rec = &el->l_recs[index];
5244 if (new_flags && (rec->e_flags & new_flags)) {
5245 mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
5246 "extent that already had them",
5247 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5248 new_flags);
5249 goto out;
5250 }
5251
5252 if (clear_flags && !(rec->e_flags & clear_flags)) {
5253 mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
5254 "extent that didn't have them",
5255 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5256 clear_flags);
5257 goto out;
5258 }
5259
Mark Fasheh328d5752007-06-18 10:48:04 -07005260 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5261 split_rec.e_cpos = cpu_to_le32(cpos);
5262 split_rec.e_leaf_clusters = cpu_to_le16(len);
5263 split_rec.e_blkno = cpu_to_le64(start_blkno);
Tao Ma555936b2009-08-18 11:22:21 +08005264 split_rec.e_flags = rec->e_flags;
5265 if (new_flags)
5266 split_rec.e_flags |= new_flags;
5267 if (clear_flags)
5268 split_rec.e_flags &= ~clear_flags;
Mark Fasheh328d5752007-06-18 10:48:04 -07005269
Tao Ma555936b2009-08-18 11:22:21 +08005270 ret = __ocfs2_split_extent(handle, et, left_path,
5271 index, &split_rec, meta_ac,
5272 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005273 if (ret)
5274 mlog_errno(ret);
5275
5276out:
5277 ocfs2_free_path(left_path);
5278 return ret;
Tao Ma555936b2009-08-18 11:22:21 +08005279
5280}
5281
5282/*
5283 * Mark the already-existing extent at cpos as written for len clusters.
5284 * This removes the unwritten extent flag.
5285 *
5286 * If the existing extent is larger than the request, initiate a
5287 * split. An attempt will be made at merging with adjacent extents.
5288 *
5289 * The caller is responsible for passing down meta_ac if we'll need it.
5290 */
5291int ocfs2_mark_extent_written(struct inode *inode,
5292 struct ocfs2_extent_tree *et,
5293 handle_t *handle, u32 cpos, u32 len, u32 phys,
5294 struct ocfs2_alloc_context *meta_ac,
5295 struct ocfs2_cached_dealloc_ctxt *dealloc)
5296{
5297 int ret;
5298
5299 mlog(0, "Inode %lu cpos %u, len %u, phys clusters %u\n",
5300 inode->i_ino, cpos, len, phys);
5301
5302 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
5303 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents "
5304 "that are being written to, but the feature bit "
5305 "is not set in the super block.",
5306 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5307 ret = -EROFS;
5308 goto out;
5309 }
5310
5311 /*
5312 * XXX: This should be fixed up so that we just re-insert the
5313 * next extent records.
5314 */
5315 ocfs2_et_extent_map_truncate(et, 0);
5316
5317 ret = ocfs2_change_extent_flag(handle, et, cpos,
5318 len, phys, meta_ac, dealloc,
5319 0, OCFS2_EXT_UNWRITTEN);
5320 if (ret)
5321 mlog_errno(ret);
5322
5323out:
5324 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07005325}
5326
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005327static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5328 struct ocfs2_path *path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005329 int index, u32 new_range,
5330 struct ocfs2_alloc_context *meta_ac)
5331{
5332 int ret, depth, credits = handle->h_buffer_credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005333 struct buffer_head *last_eb_bh = NULL;
5334 struct ocfs2_extent_block *eb;
5335 struct ocfs2_extent_list *rightmost_el, *el;
5336 struct ocfs2_extent_rec split_rec;
5337 struct ocfs2_extent_rec *rec;
5338 struct ocfs2_insert_type insert;
5339
5340 /*
5341 * Setup the record to split before we grow the tree.
5342 */
5343 el = path_leaf_el(path);
5344 rec = &el->l_recs[index];
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005345 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5346 &split_rec, new_range, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005347
5348 depth = path->p_tree_depth;
5349 if (depth > 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08005350 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005351 ocfs2_et_get_last_eb_blk(et),
5352 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005353 if (ret < 0) {
5354 mlog_errno(ret);
5355 goto out;
5356 }
5357
5358 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5359 rightmost_el = &eb->h_list;
5360 } else
5361 rightmost_el = path_leaf_el(path);
5362
Tao Ma811f9332008-08-18 17:38:43 +08005363 credits += path->p_tree_depth +
Joel Beckerce1d9ea2008-08-20 16:30:07 -07005364 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005365 ret = ocfs2_extend_trans(handle, credits);
5366 if (ret) {
5367 mlog_errno(ret);
5368 goto out;
5369 }
5370
5371 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5372 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08005373 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005374 meta_ac);
5375 if (ret) {
5376 mlog_errno(ret);
5377 goto out;
5378 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005379 }
5380
5381 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5382 insert.ins_appending = APPEND_NONE;
5383 insert.ins_contig = CONTIG_NONE;
5384 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005385 insert.ins_tree_depth = depth;
5386
Joel Becker3505bec2009-02-13 02:57:58 -08005387 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005388 if (ret)
5389 mlog_errno(ret);
5390
5391out:
5392 brelse(last_eb_bh);
5393 return ret;
5394}
5395
Joel Becker043beeb2009-02-13 02:42:30 -08005396static int ocfs2_truncate_rec(handle_t *handle,
5397 struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005398 struct ocfs2_path *path, int index,
5399 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker043beeb2009-02-13 02:42:30 -08005400 u32 cpos, u32 len)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005401{
5402 int ret;
5403 u32 left_cpos, rec_range, trunc_range;
5404 int wants_rotate = 0, is_rightmost_tree_rec = 0;
Joel Becker043beeb2009-02-13 02:42:30 -08005405 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005406 struct ocfs2_path *left_path = NULL;
5407 struct ocfs2_extent_list *el = path_leaf_el(path);
5408 struct ocfs2_extent_rec *rec;
5409 struct ocfs2_extent_block *eb;
5410
5411 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Joel Becker70f18c02009-02-13 02:09:31 -08005412 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005413 if (ret) {
5414 mlog_errno(ret);
5415 goto out;
5416 }
5417
5418 index--;
5419 }
5420
5421 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5422 path->p_tree_depth) {
5423 /*
5424 * Check whether this is the rightmost tree record. If
5425 * we remove all of this record or part of its right
5426 * edge then an update of the record lengths above it
5427 * will be required.
5428 */
5429 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5430 if (eb->h_next_leaf_blk == 0)
5431 is_rightmost_tree_rec = 1;
5432 }
5433
5434 rec = &el->l_recs[index];
5435 if (index == 0 && path->p_tree_depth &&
5436 le32_to_cpu(rec->e_cpos) == cpos) {
5437 /*
5438 * Changing the leftmost offset (via partial or whole
5439 * record truncate) of an interior (or rightmost) path
5440 * means we have to update the subtree that is formed
5441 * by this leaf and the one to it's left.
5442 *
5443 * There are two cases we can skip:
Joel Becker043beeb2009-02-13 02:42:30 -08005444 * 1) Path is the leftmost one in our btree.
Mark Fashehd0c7d702007-07-03 13:27:22 -07005445 * 2) The leaf is rightmost and will be empty after
5446 * we remove the extent record - the rotate code
5447 * knows how to update the newly formed edge.
5448 */
5449
Joel Becker043beeb2009-02-13 02:42:30 -08005450 ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005451 if (ret) {
5452 mlog_errno(ret);
5453 goto out;
5454 }
5455
5456 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005457 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005458 if (!left_path) {
5459 ret = -ENOMEM;
5460 mlog_errno(ret);
5461 goto out;
5462 }
5463
Joel Beckerfacdb772009-02-12 18:08:48 -08005464 ret = ocfs2_find_path(et->et_ci, left_path,
5465 left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005466 if (ret) {
5467 mlog_errno(ret);
5468 goto out;
5469 }
5470 }
5471 }
5472
5473 ret = ocfs2_extend_rotate_transaction(handle, 0,
5474 handle->h_buffer_credits,
5475 path);
5476 if (ret) {
5477 mlog_errno(ret);
5478 goto out;
5479 }
5480
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005481 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005482 if (ret) {
5483 mlog_errno(ret);
5484 goto out;
5485 }
5486
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005487 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005488 if (ret) {
5489 mlog_errno(ret);
5490 goto out;
5491 }
5492
5493 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5494 trunc_range = cpos + len;
5495
5496 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5497 int next_free;
5498
5499 memset(rec, 0, sizeof(*rec));
5500 ocfs2_cleanup_merge(el, index);
5501 wants_rotate = 1;
5502
5503 next_free = le16_to_cpu(el->l_next_free_rec);
5504 if (is_rightmost_tree_rec && next_free > 1) {
5505 /*
5506 * We skip the edge update if this path will
5507 * be deleted by the rotate code.
5508 */
5509 rec = &el->l_recs[next_free - 1];
Joel Beckerd401dc12009-02-13 02:24:10 -08005510 ocfs2_adjust_rightmost_records(handle, et, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005511 rec);
5512 }
5513 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5514 /* Remove leftmost portion of the record. */
5515 le32_add_cpu(&rec->e_cpos, len);
5516 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5517 le16_add_cpu(&rec->e_leaf_clusters, -len);
5518 } else if (rec_range == trunc_range) {
5519 /* Remove rightmost portion of the record */
5520 le16_add_cpu(&rec->e_leaf_clusters, -len);
5521 if (is_rightmost_tree_rec)
Joel Beckerd401dc12009-02-13 02:24:10 -08005522 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005523 } else {
5524 /* Caller should have trapped this. */
Joel Becker043beeb2009-02-13 02:42:30 -08005525 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5526 "(%u, %u)\n",
5527 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005528 le32_to_cpu(rec->e_cpos),
5529 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5530 BUG();
5531 }
5532
5533 if (left_path) {
5534 int subtree_index;
5535
Joel Becker7dc02802009-02-12 19:20:13 -08005536 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Joel Becker4619c732009-02-12 19:02:36 -08005537 ocfs2_complete_edge_insert(handle, left_path, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005538 subtree_index);
5539 }
5540
5541 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5542
Joel Becker70f18c02009-02-13 02:09:31 -08005543 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005544 if (ret) {
5545 mlog_errno(ret);
5546 goto out;
5547 }
5548
5549out:
5550 ocfs2_free_path(left_path);
5551 return ret;
5552}
5553
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005554int ocfs2_remove_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005555 struct ocfs2_extent_tree *et,
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005556 u32 cpos, u32 len,
Mark Fasheh063c4562007-07-03 13:34:11 -07005557 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005558 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005559{
5560 int ret, index;
5561 u32 rec_range, trunc_range;
5562 struct ocfs2_extent_rec *rec;
5563 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005564 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005565
Joel Becker4c911ee2009-02-13 02:50:12 -08005566 /*
5567 * XXX: Why are we truncating to 0 instead of wherever this
5568 * affects us?
5569 */
5570 ocfs2_et_extent_map_truncate(et, 0);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005571
Joel Beckerffdd7a52008-10-17 22:32:01 -07005572 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005573 if (!path) {
5574 ret = -ENOMEM;
5575 mlog_errno(ret);
5576 goto out;
5577 }
5578
Joel Beckerfacdb772009-02-12 18:08:48 -08005579 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005580 if (ret) {
5581 mlog_errno(ret);
5582 goto out;
5583 }
5584
5585 el = path_leaf_el(path);
5586 index = ocfs2_search_extent_list(el, cpos);
5587 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005588 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5589 "Owner %llu has an extent at cpos %u which can no "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005590 "longer be found.\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005591 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5592 cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005593 ret = -EROFS;
5594 goto out;
5595 }
5596
5597 /*
5598 * We have 3 cases of extent removal:
5599 * 1) Range covers the entire extent rec
5600 * 2) Range begins or ends on one edge of the extent rec
5601 * 3) Range is in the middle of the extent rec (no shared edges)
5602 *
5603 * For case 1 we remove the extent rec and left rotate to
5604 * fill the hole.
5605 *
5606 * For case 2 we just shrink the existing extent rec, with a
5607 * tree update if the shrinking edge is also the edge of an
5608 * extent block.
5609 *
5610 * For case 3 we do a right split to turn the extent rec into
5611 * something case 2 can handle.
5612 */
5613 rec = &el->l_recs[index];
5614 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5615 trunc_range = cpos + len;
5616
5617 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5618
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005619 mlog(0, "Owner %llu, remove (cpos %u, len %u). Existing index %d "
Mark Fashehd0c7d702007-07-03 13:27:22 -07005620 "(cpos %u, len %u)\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005621 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5622 cpos, len, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005623 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec));
5624
5625 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
Joel Becker043beeb2009-02-13 02:42:30 -08005626 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5627 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005628 if (ret) {
5629 mlog_errno(ret);
5630 goto out;
5631 }
5632 } else {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005633 ret = ocfs2_split_tree(handle, et, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005634 trunc_range, meta_ac);
5635 if (ret) {
5636 mlog_errno(ret);
5637 goto out;
5638 }
5639
5640 /*
5641 * The split could have manipulated the tree enough to
5642 * move the record location, so we have to look for it again.
5643 */
5644 ocfs2_reinit_path(path, 1);
5645
Joel Beckerfacdb772009-02-12 18:08:48 -08005646 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005647 if (ret) {
5648 mlog_errno(ret);
5649 goto out;
5650 }
5651
5652 el = path_leaf_el(path);
5653 index = ocfs2_search_extent_list(el, cpos);
5654 if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005655 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5656 "Owner %llu: split at cpos %u lost record.",
5657 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005658 cpos);
5659 ret = -EROFS;
5660 goto out;
5661 }
5662
5663 /*
5664 * Double check our values here. If anything is fishy,
5665 * it's easier to catch it at the top level.
5666 */
5667 rec = &el->l_recs[index];
5668 rec_range = le32_to_cpu(rec->e_cpos) +
5669 ocfs2_rec_clusters(el, rec);
5670 if (rec_range != trunc_range) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005671 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
5672 "Owner %llu: error after split at cpos %u"
Mark Fashehd0c7d702007-07-03 13:27:22 -07005673 "trunc len %u, existing record is (%u,%u)",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005674 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005675 cpos, len, le32_to_cpu(rec->e_cpos),
5676 ocfs2_rec_clusters(el, rec));
5677 ret = -EROFS;
5678 goto out;
5679 }
5680
Joel Becker043beeb2009-02-13 02:42:30 -08005681 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5682 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005683 if (ret) {
5684 mlog_errno(ret);
5685 goto out;
5686 }
5687 }
5688
5689out:
5690 ocfs2_free_path(path);
5691 return ret;
5692}
5693
Mark Fashehfecc0112008-11-12 15:16:38 -08005694int ocfs2_remove_btree_range(struct inode *inode,
5695 struct ocfs2_extent_tree *et,
5696 u32 cpos, u32 phys_cpos, u32 len,
5697 struct ocfs2_cached_dealloc_ctxt *dealloc)
5698{
5699 int ret;
5700 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5701 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5702 struct inode *tl_inode = osb->osb_tl_inode;
5703 handle_t *handle;
5704 struct ocfs2_alloc_context *meta_ac = NULL;
5705
5706 ret = ocfs2_lock_allocators(inode, et, 0, 1, NULL, &meta_ac);
5707 if (ret) {
5708 mlog_errno(ret);
5709 return ret;
5710 }
5711
5712 mutex_lock(&tl_inode->i_mutex);
5713
5714 if (ocfs2_truncate_log_needs_flush(osb)) {
5715 ret = __ocfs2_flush_truncate_log(osb);
5716 if (ret < 0) {
5717 mlog_errno(ret);
5718 goto out;
5719 }
5720 }
5721
Jan Karaa90714c2008-10-09 19:38:40 +02005722 handle = ocfs2_start_trans(osb, ocfs2_remove_extent_credits(osb->sb));
Mark Fashehfecc0112008-11-12 15:16:38 -08005723 if (IS_ERR(handle)) {
5724 ret = PTR_ERR(handle);
5725 mlog_errno(ret);
5726 goto out;
5727 }
5728
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005729 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07005730 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005731 if (ret) {
5732 mlog_errno(ret);
5733 goto out;
5734 }
5735
Mark Fashehfd4ef232009-01-29 15:06:21 -08005736 vfs_dq_free_space_nodirty(inode,
5737 ocfs2_clusters_to_bytes(inode->i_sb, len));
5738
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005739 ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
Mark Fashehfecc0112008-11-12 15:16:38 -08005740 if (ret) {
5741 mlog_errno(ret);
5742 goto out_commit;
5743 }
5744
Joel Becker6136ca52009-02-12 19:32:43 -08005745 ocfs2_et_update_clusters(et, -len);
Mark Fashehfecc0112008-11-12 15:16:38 -08005746
5747 ret = ocfs2_journal_dirty(handle, et->et_root_bh);
5748 if (ret) {
5749 mlog_errno(ret);
5750 goto out_commit;
5751 }
5752
5753 ret = ocfs2_truncate_log_append(osb, handle, phys_blkno, len);
5754 if (ret)
5755 mlog_errno(ret);
5756
5757out_commit:
5758 ocfs2_commit_trans(osb, handle);
5759out:
5760 mutex_unlock(&tl_inode->i_mutex);
5761
5762 if (meta_ac)
5763 ocfs2_free_alloc_context(meta_ac);
5764
5765 return ret;
5766}
5767
Mark Fasheh063c4562007-07-03 13:34:11 -07005768int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005769{
5770 struct buffer_head *tl_bh = osb->osb_tl_bh;
5771 struct ocfs2_dinode *di;
5772 struct ocfs2_truncate_log *tl;
5773
5774 di = (struct ocfs2_dinode *) tl_bh->b_data;
5775 tl = &di->id2.i_dealloc;
5776
5777 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5778 "slot %d, invalid truncate log parameters: used = "
5779 "%u, count = %u\n", osb->slot_num,
5780 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5781 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5782}
5783
5784static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5785 unsigned int new_start)
5786{
5787 unsigned int tail_index;
5788 unsigned int current_tail;
5789
5790 /* No records, nothing to coalesce */
5791 if (!le16_to_cpu(tl->tl_used))
5792 return 0;
5793
5794 tail_index = le16_to_cpu(tl->tl_used) - 1;
5795 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5796 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5797
5798 return current_tail == new_start;
5799}
5800
Mark Fasheh063c4562007-07-03 13:34:11 -07005801int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5802 handle_t *handle,
5803 u64 start_blk,
5804 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005805{
5806 int status, index;
5807 unsigned int start_cluster, tl_count;
5808 struct inode *tl_inode = osb->osb_tl_inode;
5809 struct buffer_head *tl_bh = osb->osb_tl_bh;
5810 struct ocfs2_dinode *di;
5811 struct ocfs2_truncate_log *tl;
5812
Mark Fashehb06970532006-03-03 10:24:33 -08005813 mlog_entry("start_blk = %llu, num_clusters = %u\n",
5814 (unsigned long long)start_blk, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005815
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005816 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005817
5818 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5819
5820 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005821
Joel Becker10995aa2008-11-13 14:49:12 -08005822 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5823 * by the underlying call to ocfs2_read_inode_block(), so any
5824 * corruption is a code bug */
5825 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5826
5827 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005828 tl_count = le16_to_cpu(tl->tl_count);
5829 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5830 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005831 "Truncate record count on #%llu invalid "
5832 "wanted %u, actual %u\n",
5833 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005834 ocfs2_truncate_recs_per_inode(osb->sb),
5835 le16_to_cpu(tl->tl_count));
5836
5837 /* Caller should have known to flush before calling us. */
5838 index = le16_to_cpu(tl->tl_used);
5839 if (index >= tl_count) {
5840 status = -ENOSPC;
5841 mlog_errno(status);
5842 goto bail;
5843 }
5844
Joel Becker0cf2f762009-02-12 16:41:25 -08005845 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005846 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005847 if (status < 0) {
5848 mlog_errno(status);
5849 goto bail;
5850 }
5851
5852 mlog(0, "Log truncate of %u clusters starting at cluster %u to "
Mark Fashehb06970532006-03-03 10:24:33 -08005853 "%llu (index = %d)\n", num_clusters, start_cluster,
5854 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index);
Mark Fashehccd979b2005-12-15 14:31:24 -08005855
5856 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5857 /*
5858 * Move index back to the record we are coalescing with.
5859 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5860 */
5861 index--;
5862
5863 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
5864 mlog(0, "Coalesce with index %u (start = %u, clusters = %u)\n",
5865 index, le32_to_cpu(tl->tl_recs[index].t_start),
5866 num_clusters);
5867 } else {
5868 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5869 tl->tl_used = cpu_to_le16(index + 1);
5870 }
5871 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5872
5873 status = ocfs2_journal_dirty(handle, tl_bh);
5874 if (status < 0) {
5875 mlog_errno(status);
5876 goto bail;
5877 }
5878
5879bail:
5880 mlog_exit(status);
5881 return status;
5882}
5883
5884static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fasheh1fabe142006-10-09 18:11:45 -07005885 handle_t *handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08005886 struct inode *data_alloc_inode,
5887 struct buffer_head *data_alloc_bh)
5888{
5889 int status = 0;
5890 int i;
5891 unsigned int num_clusters;
5892 u64 start_blk;
5893 struct ocfs2_truncate_rec rec;
5894 struct ocfs2_dinode *di;
5895 struct ocfs2_truncate_log *tl;
5896 struct inode *tl_inode = osb->osb_tl_inode;
5897 struct buffer_head *tl_bh = osb->osb_tl_bh;
5898
5899 mlog_entry_void();
5900
5901 di = (struct ocfs2_dinode *) tl_bh->b_data;
5902 tl = &di->id2.i_dealloc;
5903 i = le16_to_cpu(tl->tl_used) - 1;
5904 while (i >= 0) {
5905 /* Caller has given us at least enough credits to
5906 * update the truncate log dinode */
Joel Becker0cf2f762009-02-12 16:41:25 -08005907 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005908 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005909 if (status < 0) {
5910 mlog_errno(status);
5911 goto bail;
5912 }
5913
5914 tl->tl_used = cpu_to_le16(i);
5915
5916 status = ocfs2_journal_dirty(handle, tl_bh);
5917 if (status < 0) {
5918 mlog_errno(status);
5919 goto bail;
5920 }
5921
5922 /* TODO: Perhaps we can calculate the bulk of the
5923 * credits up front rather than extending like
5924 * this. */
5925 status = ocfs2_extend_trans(handle,
5926 OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5927 if (status < 0) {
5928 mlog_errno(status);
5929 goto bail;
5930 }
5931
5932 rec = tl->tl_recs[i];
5933 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5934 le32_to_cpu(rec.t_start));
5935 num_clusters = le32_to_cpu(rec.t_clusters);
5936
5937 /* if start_blk is not set, we ignore the record as
5938 * invalid. */
5939 if (start_blk) {
5940 mlog(0, "free record %d, start = %u, clusters = %u\n",
5941 i, le32_to_cpu(rec.t_start), num_clusters);
5942
5943 status = ocfs2_free_clusters(handle, data_alloc_inode,
5944 data_alloc_bh, start_blk,
5945 num_clusters);
5946 if (status < 0) {
5947 mlog_errno(status);
5948 goto bail;
5949 }
5950 }
5951 i--;
5952 }
5953
5954bail:
5955 mlog_exit(status);
5956 return status;
5957}
5958
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005959/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005960int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005961{
5962 int status;
5963 unsigned int num_to_flush;
Mark Fasheh1fabe142006-10-09 18:11:45 -07005964 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005965 struct inode *tl_inode = osb->osb_tl_inode;
5966 struct inode *data_alloc_inode = NULL;
5967 struct buffer_head *tl_bh = osb->osb_tl_bh;
5968 struct buffer_head *data_alloc_bh = NULL;
5969 struct ocfs2_dinode *di;
5970 struct ocfs2_truncate_log *tl;
5971
5972 mlog_entry_void();
5973
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005974 BUG_ON(mutex_trylock(&tl_inode->i_mutex));
Mark Fashehccd979b2005-12-15 14:31:24 -08005975
5976 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005977
Joel Becker10995aa2008-11-13 14:49:12 -08005978 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5979 * by the underlying call to ocfs2_read_inode_block(), so any
5980 * corruption is a code bug */
5981 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5982
5983 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005984 num_to_flush = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08005985 mlog(0, "Flush %u records from truncate log #%llu\n",
5986 num_to_flush, (unsigned long long)OCFS2_I(tl_inode)->ip_blkno);
Mark Fashehccd979b2005-12-15 14:31:24 -08005987 if (!num_to_flush) {
5988 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005989 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005990 }
5991
5992 data_alloc_inode = ocfs2_get_system_file_inode(osb,
5993 GLOBAL_BITMAP_SYSTEM_INODE,
5994 OCFS2_INVALID_SLOT);
5995 if (!data_alloc_inode) {
5996 status = -EINVAL;
5997 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07005998 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08005999 }
6000
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006001 mutex_lock(&data_alloc_inode->i_mutex);
6002
Mark Fashehe63aecb62007-10-18 15:30:42 -07006003 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08006004 if (status < 0) {
6005 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006006 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08006007 }
6008
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006009 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006010 if (IS_ERR(handle)) {
6011 status = PTR_ERR(handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006012 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006013 goto out_unlock;
Mark Fashehccd979b2005-12-15 14:31:24 -08006014 }
6015
6016 status = ocfs2_replay_truncate_records(osb, handle, data_alloc_inode,
6017 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006018 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08006019 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08006020
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006021 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006022
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006023out_unlock:
6024 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07006025 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08006026
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006027out_mutex:
6028 mutex_unlock(&data_alloc_inode->i_mutex);
6029 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006030
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006031out:
Mark Fashehccd979b2005-12-15 14:31:24 -08006032 mlog_exit(status);
6033 return status;
6034}
6035
6036int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
6037{
6038 int status;
6039 struct inode *tl_inode = osb->osb_tl_inode;
6040
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006041 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006042 status = __ocfs2_flush_truncate_log(osb);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006043 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006044
6045 return status;
6046}
6047
David Howellsc4028952006-11-22 14:57:56 +00006048static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08006049{
6050 int status;
David Howellsc4028952006-11-22 14:57:56 +00006051 struct ocfs2_super *osb =
6052 container_of(work, struct ocfs2_super,
6053 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08006054
6055 mlog_entry_void();
6056
6057 status = ocfs2_flush_truncate_log(osb);
6058 if (status < 0)
6059 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08006060 else
6061 ocfs2_init_inode_steal_slot(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08006062
6063 mlog_exit(status);
6064}
6065
6066#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
6067void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
6068 int cancel)
6069{
6070 if (osb->osb_tl_inode) {
6071 /* We want to push off log flushes while truncates are
6072 * still running. */
6073 if (cancel)
6074 cancel_delayed_work(&osb->osb_truncate_log_wq);
6075
6076 queue_delayed_work(ocfs2_wq, &osb->osb_truncate_log_wq,
6077 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
6078 }
6079}
6080
6081static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
6082 int slot_num,
6083 struct inode **tl_inode,
6084 struct buffer_head **tl_bh)
6085{
6086 int status;
6087 struct inode *inode = NULL;
6088 struct buffer_head *bh = NULL;
6089
6090 inode = ocfs2_get_system_file_inode(osb,
6091 TRUNCATE_LOG_SYSTEM_INODE,
6092 slot_num);
6093 if (!inode) {
6094 status = -EINVAL;
6095 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
6096 goto bail;
6097 }
6098
Joel Beckerb657c952008-11-13 14:49:11 -08006099 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006100 if (status < 0) {
6101 iput(inode);
6102 mlog_errno(status);
6103 goto bail;
6104 }
6105
6106 *tl_inode = inode;
6107 *tl_bh = bh;
6108bail:
6109 mlog_exit(status);
6110 return status;
6111}
6112
6113/* called during the 1st stage of node recovery. we stamp a clean
6114 * truncate log and pass back a copy for processing later. if the
6115 * truncate log does not require processing, a *tl_copy is set to
6116 * NULL. */
6117int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6118 int slot_num,
6119 struct ocfs2_dinode **tl_copy)
6120{
6121 int status;
6122 struct inode *tl_inode = NULL;
6123 struct buffer_head *tl_bh = NULL;
6124 struct ocfs2_dinode *di;
6125 struct ocfs2_truncate_log *tl;
6126
6127 *tl_copy = NULL;
6128
6129 mlog(0, "recover truncate log from slot %d\n", slot_num);
6130
6131 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6132 if (status < 0) {
6133 mlog_errno(status);
6134 goto bail;
6135 }
6136
6137 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08006138
Joel Becker10995aa2008-11-13 14:49:12 -08006139 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6140 * validated by the underlying call to ocfs2_read_inode_block(),
6141 * so any corruption is a code bug */
6142 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6143
6144 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08006145 if (le16_to_cpu(tl->tl_used)) {
6146 mlog(0, "We'll have %u logs to recover\n",
6147 le16_to_cpu(tl->tl_used));
6148
6149 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6150 if (!(*tl_copy)) {
6151 status = -ENOMEM;
6152 mlog_errno(status);
6153 goto bail;
6154 }
6155
6156 /* Assuming the write-out below goes well, this copy
6157 * will be passed back to recovery for processing. */
6158 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6159
6160 /* All we need to do to clear the truncate log is set
6161 * tl_used. */
6162 tl->tl_used = 0;
6163
Joel Becker13723d02008-10-17 19:25:01 -07006164 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08006165 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08006166 if (status < 0) {
6167 mlog_errno(status);
6168 goto bail;
6169 }
6170 }
6171
6172bail:
6173 if (tl_inode)
6174 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006175 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006176
6177 if (status < 0 && (*tl_copy)) {
6178 kfree(*tl_copy);
6179 *tl_copy = NULL;
6180 }
6181
6182 mlog_exit(status);
6183 return status;
6184}
6185
6186int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6187 struct ocfs2_dinode *tl_copy)
6188{
6189 int status = 0;
6190 int i;
6191 unsigned int clusters, num_recs, start_cluster;
6192 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006193 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006194 struct inode *tl_inode = osb->osb_tl_inode;
6195 struct ocfs2_truncate_log *tl;
6196
6197 mlog_entry_void();
6198
6199 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6200 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6201 return -EINVAL;
6202 }
6203
6204 tl = &tl_copy->id2.i_dealloc;
6205 num_recs = le16_to_cpu(tl->tl_used);
Mark Fashehb06970532006-03-03 10:24:33 -08006206 mlog(0, "cleanup %u records from %llu\n", num_recs,
Mark Fasheh1ca1a112007-04-27 16:01:25 -07006207 (unsigned long long)le64_to_cpu(tl_copy->i_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08006208
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006209 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006210 for(i = 0; i < num_recs; i++) {
6211 if (ocfs2_truncate_log_needs_flush(osb)) {
6212 status = __ocfs2_flush_truncate_log(osb);
6213 if (status < 0) {
6214 mlog_errno(status);
6215 goto bail_up;
6216 }
6217 }
6218
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006219 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006220 if (IS_ERR(handle)) {
6221 status = PTR_ERR(handle);
6222 mlog_errno(status);
6223 goto bail_up;
6224 }
6225
6226 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6227 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6228 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6229
6230 status = ocfs2_truncate_log_append(osb, handle,
6231 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006232 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006233 if (status < 0) {
6234 mlog_errno(status);
6235 goto bail_up;
6236 }
6237 }
6238
6239bail_up:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08006240 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08006241
6242 mlog_exit(status);
6243 return status;
6244}
6245
6246void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6247{
6248 int status;
6249 struct inode *tl_inode = osb->osb_tl_inode;
6250
6251 mlog_entry_void();
6252
6253 if (tl_inode) {
6254 cancel_delayed_work(&osb->osb_truncate_log_wq);
6255 flush_workqueue(ocfs2_wq);
6256
6257 status = ocfs2_flush_truncate_log(osb);
6258 if (status < 0)
6259 mlog_errno(status);
6260
6261 brelse(osb->osb_tl_bh);
6262 iput(osb->osb_tl_inode);
6263 }
6264
6265 mlog_exit_void();
6266}
6267
6268int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6269{
6270 int status;
6271 struct inode *tl_inode = NULL;
6272 struct buffer_head *tl_bh = NULL;
6273
6274 mlog_entry_void();
6275
6276 status = ocfs2_get_truncate_log_info(osb,
6277 osb->slot_num,
6278 &tl_inode,
6279 &tl_bh);
6280 if (status < 0)
6281 mlog_errno(status);
6282
6283 /* ocfs2_truncate_log_shutdown keys on the existence of
6284 * osb->osb_tl_inode so we don't set any of the osb variables
6285 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006286 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6287 ocfs2_truncate_log_worker);
Mark Fashehccd979b2005-12-15 14:31:24 -08006288 osb->osb_tl_bh = tl_bh;
6289 osb->osb_tl_inode = tl_inode;
6290
6291 mlog_exit(status);
6292 return status;
6293}
6294
Mark Fasheh2b604352007-06-22 15:45:27 -07006295/*
6296 * Delayed de-allocation of suballocator blocks.
6297 *
6298 * Some sets of block de-allocations might involve multiple suballocator inodes.
6299 *
6300 * The locking for this can get extremely complicated, especially when
6301 * the suballocator inodes to delete from aren't known until deep
6302 * within an unrelated codepath.
6303 *
6304 * ocfs2_extent_block structures are a good example of this - an inode
6305 * btree could have been grown by any number of nodes each allocating
6306 * out of their own suballoc inode.
6307 *
6308 * These structures allow the delay of block de-allocation until a
6309 * later time, when locking of multiple cluster inodes won't cause
6310 * deadlock.
6311 */
6312
6313/*
Tao Ma2891d292008-11-12 08:26:58 +08006314 * Describe a single bit freed from a suballocator. For the block
6315 * suballocators, it represents one block. For the global cluster
6316 * allocator, it represents some clusters and free_bit indicates
6317 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006318 */
6319struct ocfs2_cached_block_free {
6320 struct ocfs2_cached_block_free *free_next;
6321 u64 free_blk;
6322 unsigned int free_bit;
6323};
6324
6325struct ocfs2_per_slot_free_list {
6326 struct ocfs2_per_slot_free_list *f_next_suballocator;
6327 int f_inode_type;
6328 int f_slot;
6329 struct ocfs2_cached_block_free *f_first;
6330};
6331
Tao Ma2891d292008-11-12 08:26:58 +08006332static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6333 int sysfile_type,
6334 int slot,
6335 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006336{
6337 int ret;
6338 u64 bg_blkno;
6339 handle_t *handle;
6340 struct inode *inode;
6341 struct buffer_head *di_bh = NULL;
6342 struct ocfs2_cached_block_free *tmp;
6343
6344 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6345 if (!inode) {
6346 ret = -EINVAL;
6347 mlog_errno(ret);
6348 goto out;
6349 }
6350
6351 mutex_lock(&inode->i_mutex);
6352
Mark Fashehe63aecb62007-10-18 15:30:42 -07006353 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006354 if (ret) {
6355 mlog_errno(ret);
6356 goto out_mutex;
6357 }
6358
6359 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6360 if (IS_ERR(handle)) {
6361 ret = PTR_ERR(handle);
6362 mlog_errno(ret);
6363 goto out_unlock;
6364 }
6365
6366 while (head) {
6367 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6368 head->free_bit);
6369 mlog(0, "Free bit: (bit %u, blkno %llu)\n",
6370 head->free_bit, (unsigned long long)head->free_blk);
6371
6372 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6373 head->free_bit, bg_blkno, 1);
6374 if (ret) {
6375 mlog_errno(ret);
6376 goto out_journal;
6377 }
6378
6379 ret = ocfs2_extend_trans(handle, OCFS2_SUBALLOC_FREE);
6380 if (ret) {
6381 mlog_errno(ret);
6382 goto out_journal;
6383 }
6384
6385 tmp = head;
6386 head = head->free_next;
6387 kfree(tmp);
6388 }
6389
6390out_journal:
6391 ocfs2_commit_trans(osb, handle);
6392
6393out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006394 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006395 brelse(di_bh);
6396out_mutex:
6397 mutex_unlock(&inode->i_mutex);
6398 iput(inode);
6399out:
6400 while(head) {
6401 /* Premature exit may have left some dangling items. */
6402 tmp = head;
6403 head = head->free_next;
6404 kfree(tmp);
6405 }
6406
6407 return ret;
6408}
6409
Tao Ma2891d292008-11-12 08:26:58 +08006410int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6411 u64 blkno, unsigned int bit)
6412{
6413 int ret = 0;
6414 struct ocfs2_cached_block_free *item;
6415
6416 item = kmalloc(sizeof(*item), GFP_NOFS);
6417 if (item == NULL) {
6418 ret = -ENOMEM;
6419 mlog_errno(ret);
6420 return ret;
6421 }
6422
6423 mlog(0, "Insert clusters: (bit %u, blk %llu)\n",
6424 bit, (unsigned long long)blkno);
6425
6426 item->free_blk = blkno;
6427 item->free_bit = bit;
6428 item->free_next = ctxt->c_global_allocator;
6429
6430 ctxt->c_global_allocator = item;
6431 return ret;
6432}
6433
6434static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6435 struct ocfs2_cached_block_free *head)
6436{
6437 struct ocfs2_cached_block_free *tmp;
6438 struct inode *tl_inode = osb->osb_tl_inode;
6439 handle_t *handle;
6440 int ret = 0;
6441
6442 mutex_lock(&tl_inode->i_mutex);
6443
6444 while (head) {
6445 if (ocfs2_truncate_log_needs_flush(osb)) {
6446 ret = __ocfs2_flush_truncate_log(osb);
6447 if (ret < 0) {
6448 mlog_errno(ret);
6449 break;
6450 }
6451 }
6452
6453 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6454 if (IS_ERR(handle)) {
6455 ret = PTR_ERR(handle);
6456 mlog_errno(ret);
6457 break;
6458 }
6459
6460 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6461 head->free_bit);
6462
6463 ocfs2_commit_trans(osb, handle);
6464 tmp = head;
6465 head = head->free_next;
6466 kfree(tmp);
6467
6468 if (ret < 0) {
6469 mlog_errno(ret);
6470 break;
6471 }
6472 }
6473
6474 mutex_unlock(&tl_inode->i_mutex);
6475
6476 while (head) {
6477 /* Premature exit may have left some dangling items. */
6478 tmp = head;
6479 head = head->free_next;
6480 kfree(tmp);
6481 }
6482
6483 return ret;
6484}
6485
Mark Fasheh2b604352007-06-22 15:45:27 -07006486int ocfs2_run_deallocs(struct ocfs2_super *osb,
6487 struct ocfs2_cached_dealloc_ctxt *ctxt)
6488{
6489 int ret = 0, ret2;
6490 struct ocfs2_per_slot_free_list *fl;
6491
6492 if (!ctxt)
6493 return 0;
6494
6495 while (ctxt->c_first_suballocator) {
6496 fl = ctxt->c_first_suballocator;
6497
6498 if (fl->f_first) {
6499 mlog(0, "Free items: (type %u, slot %d)\n",
6500 fl->f_inode_type, fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006501 ret2 = ocfs2_free_cached_blocks(osb,
6502 fl->f_inode_type,
6503 fl->f_slot,
6504 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006505 if (ret2)
6506 mlog_errno(ret2);
6507 if (!ret)
6508 ret = ret2;
6509 }
6510
6511 ctxt->c_first_suballocator = fl->f_next_suballocator;
6512 kfree(fl);
6513 }
6514
Tao Ma2891d292008-11-12 08:26:58 +08006515 if (ctxt->c_global_allocator) {
6516 ret2 = ocfs2_free_cached_clusters(osb,
6517 ctxt->c_global_allocator);
6518 if (ret2)
6519 mlog_errno(ret2);
6520 if (!ret)
6521 ret = ret2;
6522
6523 ctxt->c_global_allocator = NULL;
6524 }
6525
Mark Fasheh2b604352007-06-22 15:45:27 -07006526 return ret;
6527}
6528
6529static struct ocfs2_per_slot_free_list *
6530ocfs2_find_per_slot_free_list(int type,
6531 int slot,
6532 struct ocfs2_cached_dealloc_ctxt *ctxt)
6533{
6534 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6535
6536 while (fl) {
6537 if (fl->f_inode_type == type && fl->f_slot == slot)
6538 return fl;
6539
6540 fl = fl->f_next_suballocator;
6541 }
6542
6543 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6544 if (fl) {
6545 fl->f_inode_type = type;
6546 fl->f_slot = slot;
6547 fl->f_first = NULL;
6548 fl->f_next_suballocator = ctxt->c_first_suballocator;
6549
6550 ctxt->c_first_suballocator = fl;
6551 }
6552 return fl;
6553}
6554
6555static int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6556 int type, int slot, u64 blkno,
6557 unsigned int bit)
6558{
6559 int ret;
6560 struct ocfs2_per_slot_free_list *fl;
6561 struct ocfs2_cached_block_free *item;
6562
6563 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6564 if (fl == NULL) {
6565 ret = -ENOMEM;
6566 mlog_errno(ret);
6567 goto out;
6568 }
6569
6570 item = kmalloc(sizeof(*item), GFP_NOFS);
6571 if (item == NULL) {
6572 ret = -ENOMEM;
6573 mlog_errno(ret);
6574 goto out;
6575 }
6576
6577 mlog(0, "Insert: (type %d, slot %u, bit %u, blk %llu)\n",
6578 type, slot, bit, (unsigned long long)blkno);
6579
6580 item->free_blk = blkno;
6581 item->free_bit = bit;
6582 item->free_next = fl->f_first;
6583
6584 fl->f_first = item;
6585
6586 ret = 0;
6587out:
6588 return ret;
6589}
6590
Mark Fasheh59a5e412007-06-22 15:52:36 -07006591static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6592 struct ocfs2_extent_block *eb)
6593{
6594 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6595 le16_to_cpu(eb->h_suballoc_slot),
6596 le64_to_cpu(eb->h_blkno),
6597 le16_to_cpu(eb->h_suballoc_bit));
6598}
6599
Mark Fashehccd979b2005-12-15 14:31:24 -08006600/* This function will figure out whether the currently last extent
6601 * block will be deleted, and if it will, what the new last extent
6602 * block will be so we can update his h_next_leaf_blk field, as well
6603 * as the dinodes i_last_eb_blk */
Mark Fashehdcd05382007-01-16 11:32:23 -08006604static int ocfs2_find_new_last_ext_blk(struct inode *inode,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006605 unsigned int clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006606 struct ocfs2_path *path,
Mark Fashehccd979b2005-12-15 14:31:24 -08006607 struct buffer_head **new_last_eb)
6608{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006609 int next_free, ret = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08006610 u32 cpos;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006611 struct ocfs2_extent_rec *rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08006612 struct ocfs2_extent_block *eb;
6613 struct ocfs2_extent_list *el;
6614 struct buffer_head *bh = NULL;
6615
6616 *new_last_eb = NULL;
6617
Mark Fashehccd979b2005-12-15 14:31:24 -08006618 /* we have no tree, so of course, no last_eb. */
Mark Fashehdcd05382007-01-16 11:32:23 -08006619 if (!path->p_tree_depth)
6620 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006621
6622 /* trunc to zero special case - this makes tree_depth = 0
6623 * regardless of what it is. */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006624 if (OCFS2_I(inode)->ip_clusters == clusters_to_del)
Mark Fashehdcd05382007-01-16 11:32:23 -08006625 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006626
Mark Fashehdcd05382007-01-16 11:32:23 -08006627 el = path_leaf_el(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08006628 BUG_ON(!el->l_next_free_rec);
6629
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006630 /*
6631 * Make sure that this extent list will actually be empty
6632 * after we clear away the data. We can shortcut out if
6633 * there's more than one non-empty extent in the
6634 * list. Otherwise, a check of the remaining extent is
6635 * necessary.
6636 */
6637 next_free = le16_to_cpu(el->l_next_free_rec);
6638 rec = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08006639 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006640 if (next_free > 2)
Mark Fashehdcd05382007-01-16 11:32:23 -08006641 goto out;
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006642
6643 /* We may have a valid extent in index 1, check it. */
6644 if (next_free == 2)
6645 rec = &el->l_recs[1];
6646
6647 /*
6648 * Fall through - no more nonempty extents, so we want
6649 * to delete this leaf.
6650 */
6651 } else {
6652 if (next_free > 1)
6653 goto out;
6654
6655 rec = &el->l_recs[0];
6656 }
6657
6658 if (rec) {
6659 /*
6660 * Check it we'll only be trimming off the end of this
6661 * cluster.
6662 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006663 if (le16_to_cpu(rec->e_leaf_clusters) > clusters_to_del)
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006664 goto out;
6665 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006666
Mark Fashehdcd05382007-01-16 11:32:23 -08006667 ret = ocfs2_find_cpos_for_left_leaf(inode->i_sb, path, &cpos);
6668 if (ret) {
6669 mlog_errno(ret);
6670 goto out;
6671 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006672
Joel Beckerfacdb772009-02-12 18:08:48 -08006673 ret = ocfs2_find_leaf(INODE_CACHE(inode), path_root_el(path), cpos, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08006674 if (ret) {
6675 mlog_errno(ret);
6676 goto out;
6677 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006678
Mark Fashehdcd05382007-01-16 11:32:23 -08006679 eb = (struct ocfs2_extent_block *) bh->b_data;
6680 el = &eb->h_list;
Joel Becker5e965812008-11-13 14:49:16 -08006681
6682 /* ocfs2_find_leaf() gets the eb from ocfs2_read_extent_block().
6683 * Any corruption is a code bug. */
6684 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08006685
6686 *new_last_eb = bh;
6687 get_bh(*new_last_eb);
Mark Fashehdcd05382007-01-16 11:32:23 -08006688 mlog(0, "returning block %llu, (cpos: %u)\n",
6689 (unsigned long long)le64_to_cpu(eb->h_blkno), cpos);
6690out:
6691 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006692
Mark Fashehdcd05382007-01-16 11:32:23 -08006693 return ret;
Mark Fashehccd979b2005-12-15 14:31:24 -08006694}
6695
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006696/*
6697 * Trim some clusters off the rightmost edge of a tree. Only called
6698 * during truncate.
6699 *
6700 * The caller needs to:
6701 * - start journaling of each path component.
6702 * - compute and fully set up any new last ext block
6703 */
6704static int ocfs2_trim_tree(struct inode *inode, struct ocfs2_path *path,
6705 handle_t *handle, struct ocfs2_truncate_context *tc,
6706 u32 clusters_to_del, u64 *delete_start)
6707{
6708 int ret, i, index = path->p_tree_depth;
6709 u32 new_edge = 0;
6710 u64 deleted_eb = 0;
6711 struct buffer_head *bh;
6712 struct ocfs2_extent_list *el;
6713 struct ocfs2_extent_rec *rec;
6714
6715 *delete_start = 0;
6716
6717 while (index >= 0) {
6718 bh = path->p_node[index].bh;
6719 el = path->p_node[index].el;
6720
6721 mlog(0, "traveling tree (index = %d, block = %llu)\n",
6722 index, (unsigned long long)bh->b_blocknr);
6723
6724 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
6725
6726 if (index !=
6727 (path->p_tree_depth - le16_to_cpu(el->l_tree_depth))) {
6728 ocfs2_error(inode->i_sb,
6729 "Inode %lu has invalid ext. block %llu",
6730 inode->i_ino,
6731 (unsigned long long)bh->b_blocknr);
6732 ret = -EROFS;
6733 goto out;
6734 }
6735
6736find_tail_record:
6737 i = le16_to_cpu(el->l_next_free_rec) - 1;
6738 rec = &el->l_recs[i];
6739
6740 mlog(0, "Extent list before: record %d: (%u, %u, %llu), "
6741 "next = %u\n", i, le32_to_cpu(rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08006742 ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006743 (unsigned long long)le64_to_cpu(rec->e_blkno),
6744 le16_to_cpu(el->l_next_free_rec));
6745
Mark Fashehe48edee2007-03-07 16:46:57 -08006746 BUG_ON(ocfs2_rec_clusters(el, rec) < clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006747
6748 if (le16_to_cpu(el->l_tree_depth) == 0) {
6749 /*
6750 * If the leaf block contains a single empty
6751 * extent and no records, we can just remove
6752 * the block.
6753 */
6754 if (i == 0 && ocfs2_is_empty_extent(rec)) {
6755 memset(rec, 0,
6756 sizeof(struct ocfs2_extent_rec));
6757 el->l_next_free_rec = cpu_to_le16(0);
6758
6759 goto delete;
6760 }
6761
6762 /*
6763 * Remove any empty extents by shifting things
6764 * left. That should make life much easier on
6765 * the code below. This condition is rare
6766 * enough that we shouldn't see a performance
6767 * hit.
6768 */
6769 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
6770 le16_add_cpu(&el->l_next_free_rec, -1);
6771
6772 for(i = 0;
6773 i < le16_to_cpu(el->l_next_free_rec); i++)
6774 el->l_recs[i] = el->l_recs[i + 1];
6775
6776 memset(&el->l_recs[i], 0,
6777 sizeof(struct ocfs2_extent_rec));
6778
6779 /*
6780 * We've modified our extent list. The
6781 * simplest way to handle this change
6782 * is to being the search from the
6783 * start again.
6784 */
6785 goto find_tail_record;
6786 }
6787
Mark Fashehe48edee2007-03-07 16:46:57 -08006788 le16_add_cpu(&rec->e_leaf_clusters, -clusters_to_del);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006789
6790 /*
6791 * We'll use "new_edge" on our way back up the
6792 * tree to know what our rightmost cpos is.
6793 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006794 new_edge = le16_to_cpu(rec->e_leaf_clusters);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006795 new_edge += le32_to_cpu(rec->e_cpos);
6796
6797 /*
6798 * The caller will use this to delete data blocks.
6799 */
6800 *delete_start = le64_to_cpu(rec->e_blkno)
6801 + ocfs2_clusters_to_blocks(inode->i_sb,
Mark Fashehe48edee2007-03-07 16:46:57 -08006802 le16_to_cpu(rec->e_leaf_clusters));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006803
6804 /*
6805 * If it's now empty, remove this record.
6806 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006807 if (le16_to_cpu(rec->e_leaf_clusters) == 0) {
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006808 memset(rec, 0,
6809 sizeof(struct ocfs2_extent_rec));
6810 le16_add_cpu(&el->l_next_free_rec, -1);
6811 }
6812 } else {
6813 if (le64_to_cpu(rec->e_blkno) == deleted_eb) {
6814 memset(rec, 0,
6815 sizeof(struct ocfs2_extent_rec));
6816 le16_add_cpu(&el->l_next_free_rec, -1);
6817
6818 goto delete;
6819 }
6820
6821 /* Can this actually happen? */
6822 if (le16_to_cpu(el->l_next_free_rec) == 0)
6823 goto delete;
6824
6825 /*
6826 * We never actually deleted any clusters
6827 * because our leaf was empty. There's no
6828 * reason to adjust the rightmost edge then.
6829 */
6830 if (new_edge == 0)
6831 goto delete;
6832
Mark Fashehe48edee2007-03-07 16:46:57 -08006833 rec->e_int_clusters = cpu_to_le32(new_edge);
6834 le32_add_cpu(&rec->e_int_clusters,
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006835 -le32_to_cpu(rec->e_cpos));
6836
6837 /*
6838 * A deleted child record should have been
6839 * caught above.
6840 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006841 BUG_ON(le32_to_cpu(rec->e_int_clusters) == 0);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006842 }
6843
6844delete:
6845 ret = ocfs2_journal_dirty(handle, bh);
6846 if (ret) {
6847 mlog_errno(ret);
6848 goto out;
6849 }
6850
6851 mlog(0, "extent list container %llu, after: record %d: "
6852 "(%u, %u, %llu), next = %u.\n",
6853 (unsigned long long)bh->b_blocknr, i,
Mark Fashehe48edee2007-03-07 16:46:57 -08006854 le32_to_cpu(rec->e_cpos), ocfs2_rec_clusters(el, rec),
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006855 (unsigned long long)le64_to_cpu(rec->e_blkno),
6856 le16_to_cpu(el->l_next_free_rec));
6857
6858 /*
6859 * We must be careful to only attempt delete of an
6860 * extent block (and not the root inode block).
6861 */
6862 if (index > 0 && le16_to_cpu(el->l_next_free_rec) == 0) {
6863 struct ocfs2_extent_block *eb =
6864 (struct ocfs2_extent_block *)bh->b_data;
6865
6866 /*
6867 * Save this for use when processing the
6868 * parent block.
6869 */
6870 deleted_eb = le64_to_cpu(eb->h_blkno);
6871
6872 mlog(0, "deleting this extent block.\n");
6873
Joel Becker8cb471e2009-02-10 20:00:41 -08006874 ocfs2_remove_from_cache(INODE_CACHE(inode), bh);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006875
Mark Fashehe48edee2007-03-07 16:46:57 -08006876 BUG_ON(ocfs2_rec_clusters(el, &el->l_recs[0]));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006877 BUG_ON(le32_to_cpu(el->l_recs[0].e_cpos));
6878 BUG_ON(le64_to_cpu(el->l_recs[0].e_blkno));
6879
Mark Fasheh59a5e412007-06-22 15:52:36 -07006880 ret = ocfs2_cache_extent_block_free(&tc->tc_dealloc, eb);
6881 /* An error here is not fatal. */
6882 if (ret < 0)
6883 mlog_errno(ret);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006884 } else {
6885 deleted_eb = 0;
6886 }
6887
6888 index--;
6889 }
6890
6891 ret = 0;
6892out:
6893 return ret;
6894}
6895
Mark Fashehccd979b2005-12-15 14:31:24 -08006896static int ocfs2_do_truncate(struct ocfs2_super *osb,
6897 unsigned int clusters_to_del,
6898 struct inode *inode,
6899 struct buffer_head *fe_bh,
Mark Fasheh1fabe142006-10-09 18:11:45 -07006900 handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08006901 struct ocfs2_truncate_context *tc,
6902 struct ocfs2_path *path)
Mark Fashehccd979b2005-12-15 14:31:24 -08006903{
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006904 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08006905 struct ocfs2_dinode *fe;
Mark Fashehccd979b2005-12-15 14:31:24 -08006906 struct ocfs2_extent_block *last_eb = NULL;
6907 struct ocfs2_extent_list *el;
Mark Fashehccd979b2005-12-15 14:31:24 -08006908 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08006909 u64 delete_blk = 0;
6910
6911 fe = (struct ocfs2_dinode *) fe_bh->b_data;
6912
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006913 status = ocfs2_find_new_last_ext_blk(inode, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08006914 path, &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006915 if (status < 0) {
6916 mlog_errno(status);
6917 goto bail;
6918 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006919
Mark Fashehdcd05382007-01-16 11:32:23 -08006920 /*
6921 * Each component will be touched, so we might as well journal
6922 * here to avoid having to handle errors later.
6923 */
Joel Becker0cf2f762009-02-12 16:41:25 -08006924 status = ocfs2_journal_access_path(INODE_CACHE(inode), handle, path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006925 if (status < 0) {
6926 mlog_errno(status);
6927 goto bail;
Mark Fashehdcd05382007-01-16 11:32:23 -08006928 }
6929
6930 if (last_eb_bh) {
Joel Becker0cf2f762009-02-12 16:41:25 -08006931 status = ocfs2_journal_access_eb(handle, INODE_CACHE(inode), last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07006932 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08006933 if (status < 0) {
6934 mlog_errno(status);
6935 goto bail;
6936 }
6937
6938 last_eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
6939 }
6940
6941 el = &(fe->id2.i_list);
6942
6943 /*
6944 * Lower levels depend on this never happening, but it's best
6945 * to check it up here before changing the tree.
6946 */
Mark Fashehe48edee2007-03-07 16:46:57 -08006947 if (el->l_tree_depth && el->l_recs[0].e_int_clusters == 0) {
Mark Fashehdcd05382007-01-16 11:32:23 -08006948 ocfs2_error(inode->i_sb,
6949 "Inode %lu has an empty extent record, depth %u\n",
6950 inode->i_ino, le16_to_cpu(el->l_tree_depth));
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006951 status = -EROFS;
Mark Fashehccd979b2005-12-15 14:31:24 -08006952 goto bail;
6953 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006954
Jan Karaa90714c2008-10-09 19:38:40 +02006955 vfs_dq_free_space_nodirty(inode,
6956 ocfs2_clusters_to_bytes(osb->sb, clusters_to_del));
Mark Fashehccd979b2005-12-15 14:31:24 -08006957 spin_lock(&OCFS2_I(inode)->ip_lock);
6958 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters) -
6959 clusters_to_del;
6960 spin_unlock(&OCFS2_I(inode)->ip_lock);
6961 le32_add_cpu(&fe->i_clusters, -clusters_to_del);
Mark Fashehe535e2e2007-08-31 10:23:41 -07006962 inode->i_blocks = ocfs2_inode_sector_count(inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006963
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006964 status = ocfs2_trim_tree(inode, path, handle, tc,
6965 clusters_to_del, &delete_blk);
6966 if (status) {
6967 mlog_errno(status);
6968 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08006969 }
6970
Mark Fashehdcd05382007-01-16 11:32:23 -08006971 if (le32_to_cpu(fe->i_clusters) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006972 /* trunc to zero is a special case. */
6973 el->l_tree_depth = 0;
6974 fe->i_last_eb_blk = 0;
6975 } else if (last_eb)
6976 fe->i_last_eb_blk = last_eb->h_blkno;
6977
6978 status = ocfs2_journal_dirty(handle, fe_bh);
6979 if (status < 0) {
6980 mlog_errno(status);
6981 goto bail;
6982 }
6983
6984 if (last_eb) {
6985 /* If there will be a new last extent block, then by
6986 * definition, there cannot be any leaves to the right of
6987 * him. */
Mark Fashehccd979b2005-12-15 14:31:24 -08006988 last_eb->h_next_leaf_blk = 0;
6989 status = ocfs2_journal_dirty(handle, last_eb_bh);
6990 if (status < 0) {
6991 mlog_errno(status);
6992 goto bail;
6993 }
6994 }
6995
Mark Fasheh3a0782d2007-01-17 12:53:31 -08006996 if (delete_blk) {
6997 status = ocfs2_truncate_log_append(osb, handle, delete_blk,
6998 clusters_to_del);
Mark Fashehccd979b2005-12-15 14:31:24 -08006999 if (status < 0) {
7000 mlog_errno(status);
7001 goto bail;
7002 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007003 }
7004 status = 0;
7005bail:
Tao Ma60e2ec42009-08-12 14:42:47 +08007006 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007007 mlog_exit(status);
7008 return status;
7009}
7010
Joel Becker2b4e30f2008-09-03 20:03:41 -07007011static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08007012{
7013 set_buffer_uptodate(bh);
7014 mark_buffer_dirty(bh);
7015 return 0;
7016}
7017
Mark Fasheh1d410a62007-09-07 14:20:45 -07007018static void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
7019 unsigned int from, unsigned int to,
7020 struct page *page, int zero, u64 *phys)
7021{
7022 int ret, partial = 0;
7023
7024 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
7025 if (ret)
7026 mlog_errno(ret);
7027
7028 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08007029 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07007030
7031 /*
7032 * Need to set the buffers we zero'd into uptodate
7033 * here if they aren't - ocfs2_map_page_blocks()
7034 * might've skipped some
7035 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07007036 ret = walk_page_buffers(handle, page_buffers(page),
7037 from, to, &partial,
7038 ocfs2_zero_func);
7039 if (ret < 0)
7040 mlog_errno(ret);
7041 else if (ocfs2_should_order_data(inode)) {
7042 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07007043 if (ret < 0)
7044 mlog_errno(ret);
7045 }
7046
7047 if (!partial)
7048 SetPageUptodate(page);
7049
7050 flush_dcache_page(page);
7051}
7052
Mark Fasheh35edec12007-07-06 14:41:18 -07007053static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
7054 loff_t end, struct page **pages,
7055 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08007056{
Mark Fasheh1d410a62007-09-07 14:20:45 -07007057 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08007058 struct page *page;
7059 unsigned int from, to = PAGE_CACHE_SIZE;
7060 struct super_block *sb = inode->i_sb;
7061
7062 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
7063
7064 if (numpages == 0)
7065 goto out;
7066
Mark Fasheh35edec12007-07-06 14:41:18 -07007067 to = PAGE_CACHE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08007068 for(i = 0; i < numpages; i++) {
7069 page = pages[i];
7070
Mark Fasheh35edec12007-07-06 14:41:18 -07007071 from = start & (PAGE_CACHE_SIZE - 1);
7072 if ((end >> PAGE_CACHE_SHIFT) == page->index)
7073 to = end & (PAGE_CACHE_SIZE - 1);
7074
Mark Fasheh60b11392007-02-16 11:46:50 -08007075 BUG_ON(from > PAGE_CACHE_SIZE);
7076 BUG_ON(to > PAGE_CACHE_SIZE);
7077
Mark Fasheh1d410a62007-09-07 14:20:45 -07007078 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
7079 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08007080
Mark Fasheh35edec12007-07-06 14:41:18 -07007081 start = (page->index + 1) << PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08007082 }
7083out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07007084 if (pages)
7085 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08007086}
7087
Mark Fasheh35edec12007-07-06 14:41:18 -07007088static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
Mark Fasheh1d410a62007-09-07 14:20:45 -07007089 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08007090{
Mark Fasheh1d410a62007-09-07 14:20:45 -07007091 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08007092 struct super_block *sb = inode->i_sb;
7093 struct address_space *mapping = inode->i_mapping;
7094 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07007095 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08007096
Mark Fasheh35edec12007-07-06 14:41:18 -07007097 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08007098
Mark Fasheh35edec12007-07-06 14:41:18 -07007099 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
7100 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
7101
Mark Fasheh1d410a62007-09-07 14:20:45 -07007102 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07007103 last_page_bytes = PAGE_ALIGN(end);
7104 index = start >> PAGE_CACHE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08007105 do {
7106 pages[numpages] = grab_cache_page(mapping, index);
7107 if (!pages[numpages]) {
7108 ret = -ENOMEM;
7109 mlog_errno(ret);
7110 goto out;
7111 }
7112
7113 numpages++;
7114 index++;
Mark Fasheh35edec12007-07-06 14:41:18 -07007115 } while (index < (last_page_bytes >> PAGE_CACHE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08007116
7117out:
7118 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07007119 if (pages)
7120 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08007121 numpages = 0;
7122 }
7123
7124 *num = numpages;
7125
7126 return ret;
7127}
7128
7129/*
7130 * Zero the area past i_size but still within an allocated
7131 * cluster. This avoids exposing nonzero data on subsequent file
7132 * extends.
7133 *
7134 * We need to call this before i_size is updated on the inode because
7135 * otherwise block_write_full_page() will skip writeout of pages past
7136 * i_size. The new_i_size parameter is passed for this reason.
7137 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007138int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
7139 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08007140{
Mark Fasheh1d410a62007-09-07 14:20:45 -07007141 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08007142 struct page **pages = NULL;
7143 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07007144 unsigned int ext_flags;
7145 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08007146
7147 /*
7148 * File systems which don't support sparse files zero on every
7149 * extend.
7150 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07007151 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08007152 return 0;
7153
Mark Fasheh1d410a62007-09-07 14:20:45 -07007154 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08007155 sizeof(struct page *), GFP_NOFS);
7156 if (pages == NULL) {
7157 ret = -ENOMEM;
7158 mlog_errno(ret);
7159 goto out;
7160 }
7161
Mark Fasheh1d410a62007-09-07 14:20:45 -07007162 if (range_start == range_end)
7163 goto out;
7164
7165 ret = ocfs2_extent_map_get_blocks(inode,
7166 range_start >> sb->s_blocksize_bits,
7167 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08007168 if (ret) {
7169 mlog_errno(ret);
7170 goto out;
7171 }
7172
Mark Fasheh1d410a62007-09-07 14:20:45 -07007173 /*
7174 * Tail is a hole, or is marked unwritten. In either case, we
7175 * can count on read and write to return/push zero's.
7176 */
7177 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08007178 goto out;
7179
Mark Fasheh1d410a62007-09-07 14:20:45 -07007180 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
7181 &numpages);
7182 if (ret) {
7183 mlog_errno(ret);
7184 goto out;
7185 }
7186
Mark Fasheh35edec12007-07-06 14:41:18 -07007187 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
7188 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08007189
7190 /*
7191 * Initiate writeout of the pages we zero'd here. We don't
7192 * wait on them - the truncate_inode_pages() call later will
7193 * do that for us.
7194 */
Mark Fasheh35edec12007-07-06 14:41:18 -07007195 ret = do_sync_mapping_range(inode->i_mapping, range_start,
7196 range_end - 1, SYNC_FILE_RANGE_WRITE);
Mark Fasheh60b11392007-02-16 11:46:50 -08007197 if (ret)
7198 mlog_errno(ret);
7199
7200out:
7201 if (pages)
7202 kfree(pages);
7203
7204 return ret;
7205}
7206
Tiger Yangfdd77702008-08-18 17:08:55 +08007207static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
7208 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007209{
7210 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08007211 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007212
Tiger Yangfdd77702008-08-18 17:08:55 +08007213 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
7214 memset(&di->id2, 0, blocksize -
7215 offsetof(struct ocfs2_dinode, id2) -
7216 xattrsize);
7217 else
7218 memset(&di->id2, 0, blocksize -
7219 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007220}
7221
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007222void ocfs2_dinode_new_extent_list(struct inode *inode,
7223 struct ocfs2_dinode *di)
7224{
Tiger Yangfdd77702008-08-18 17:08:55 +08007225 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007226 di->id2.i_list.l_tree_depth = 0;
7227 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08007228 di->id2.i_list.l_count = cpu_to_le16(
7229 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007230}
7231
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007232void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
7233{
7234 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7235 struct ocfs2_inline_data *idata = &di->id2.i_data;
7236
7237 spin_lock(&oi->ip_lock);
7238 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
7239 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7240 spin_unlock(&oi->ip_lock);
7241
7242 /*
7243 * We clear the entire i_data structure here so that all
7244 * fields can be properly initialized.
7245 */
Tiger Yangfdd77702008-08-18 17:08:55 +08007246 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007247
Tiger Yangfdd77702008-08-18 17:08:55 +08007248 idata->id_count = cpu_to_le16(
7249 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007250}
7251
7252int ocfs2_convert_inline_data_to_extents(struct inode *inode,
7253 struct buffer_head *di_bh)
7254{
7255 int ret, i, has_data, num_pages = 0;
7256 handle_t *handle;
7257 u64 uninitialized_var(block);
7258 struct ocfs2_inode_info *oi = OCFS2_I(inode);
7259 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7260 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007261 struct ocfs2_alloc_context *data_ac = NULL;
7262 struct page **pages = NULL;
7263 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07007264 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02007265 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007266
7267 has_data = i_size_read(inode) ? 1 : 0;
7268
7269 if (has_data) {
7270 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
7271 sizeof(struct page *), GFP_NOFS);
7272 if (pages == NULL) {
7273 ret = -ENOMEM;
7274 mlog_errno(ret);
7275 goto out;
7276 }
7277
7278 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
7279 if (ret) {
7280 mlog_errno(ret);
7281 goto out;
7282 }
7283 }
7284
Jan Karaa90714c2008-10-09 19:38:40 +02007285 handle = ocfs2_start_trans(osb,
7286 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007287 if (IS_ERR(handle)) {
7288 ret = PTR_ERR(handle);
7289 mlog_errno(ret);
7290 goto out_unlock;
7291 }
7292
Joel Becker0cf2f762009-02-12 16:41:25 -08007293 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007294 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007295 if (ret) {
7296 mlog_errno(ret);
7297 goto out_commit;
7298 }
7299
7300 if (has_data) {
7301 u32 bit_off, num;
7302 unsigned int page_end;
7303 u64 phys;
7304
Jan Karaa90714c2008-10-09 19:38:40 +02007305 if (vfs_dq_alloc_space_nodirty(inode,
7306 ocfs2_clusters_to_bytes(osb->sb, 1))) {
7307 ret = -EDQUOT;
7308 goto out_commit;
7309 }
7310 did_quota = 1;
7311
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007312 ret = ocfs2_claim_clusters(osb, handle, data_ac, 1, &bit_off,
7313 &num);
7314 if (ret) {
7315 mlog_errno(ret);
7316 goto out_commit;
7317 }
7318
7319 /*
7320 * Save two copies, one for insert, and one that can
7321 * be changed by ocfs2_map_and_dirty_page() below.
7322 */
7323 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
7324
7325 /*
7326 * Non sparse file systems zero on extend, so no need
7327 * to do that now.
7328 */
7329 if (!ocfs2_sparse_alloc(osb) &&
7330 PAGE_CACHE_SIZE < osb->s_clustersize)
7331 end = PAGE_CACHE_SIZE;
7332
7333 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
7334 if (ret) {
7335 mlog_errno(ret);
7336 goto out_commit;
7337 }
7338
7339 /*
7340 * This should populate the 1st page for us and mark
7341 * it up to date.
7342 */
7343 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
7344 if (ret) {
7345 mlog_errno(ret);
7346 goto out_commit;
7347 }
7348
7349 page_end = PAGE_CACHE_SIZE;
7350 if (PAGE_CACHE_SIZE > osb->s_clustersize)
7351 page_end = osb->s_clustersize;
7352
7353 for (i = 0; i < num_pages; i++)
7354 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
7355 pages[i], i > 0, &phys);
7356 }
7357
7358 spin_lock(&oi->ip_lock);
7359 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
7360 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7361 spin_unlock(&oi->ip_lock);
7362
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07007363 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007364
7365 ocfs2_journal_dirty(handle, di_bh);
7366
7367 if (has_data) {
7368 /*
7369 * An error at this point should be extremely rare. If
7370 * this proves to be false, we could always re-build
7371 * the in-inode data from our pages.
7372 */
Joel Becker5e404e92009-02-13 03:54:22 -08007373 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
Joel Beckercc79d8c2009-02-13 03:24:43 -08007374 ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007375 if (ret) {
7376 mlog_errno(ret);
7377 goto out_commit;
7378 }
7379
7380 inode->i_blocks = ocfs2_inode_sector_count(inode);
7381 }
7382
7383out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007384 if (ret < 0 && did_quota)
7385 vfs_dq_free_space_nodirty(inode,
7386 ocfs2_clusters_to_bytes(osb->sb, 1));
7387
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007388 ocfs2_commit_trans(osb, handle);
7389
7390out_unlock:
7391 if (data_ac)
7392 ocfs2_free_alloc_context(data_ac);
7393
7394out:
7395 if (pages) {
7396 ocfs2_unlock_and_free_pages(pages, num_pages);
7397 kfree(pages);
7398 }
7399
7400 return ret;
7401}
7402
Mark Fashehccd979b2005-12-15 14:31:24 -08007403/*
7404 * It is expected, that by the time you call this function,
7405 * inode->i_size and fe->i_size have been adjusted.
7406 *
7407 * WARNING: This will kfree the truncate context
7408 */
7409int ocfs2_commit_truncate(struct ocfs2_super *osb,
7410 struct inode *inode,
7411 struct buffer_head *fe_bh,
7412 struct ocfs2_truncate_context *tc)
7413{
7414 int status, i, credits, tl_sem = 0;
Mark Fashehdcd05382007-01-16 11:32:23 -08007415 u32 clusters_to_del, new_highest_cpos, range;
Mark Fashehccd979b2005-12-15 14:31:24 -08007416 struct ocfs2_extent_list *el;
Mark Fasheh1fabe142006-10-09 18:11:45 -07007417 handle_t *handle = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007418 struct inode *tl_inode = osb->osb_tl_inode;
Mark Fashehdcd05382007-01-16 11:32:23 -08007419 struct ocfs2_path *path = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +08007420 struct ocfs2_dinode *di = (struct ocfs2_dinode *)fe_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007421
7422 mlog_entry_void();
7423
Mark Fashehdcd05382007-01-16 11:32:23 -08007424 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007425 i_size_read(inode));
7426
Joel Becker13723d02008-10-17 19:25:01 -07007427 path = ocfs2_new_path(fe_bh, &di->id2.i_list,
7428 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08007429 if (!path) {
7430 status = -ENOMEM;
7431 mlog_errno(status);
7432 goto bail;
7433 }
Mark Fasheh83418972007-04-23 18:53:12 -07007434
7435 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7436
Mark Fashehccd979b2005-12-15 14:31:24 -08007437start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007438 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007439 * Check that we still have allocation to delete.
7440 */
7441 if (OCFS2_I(inode)->ip_clusters == 0) {
7442 status = 0;
7443 goto bail;
7444 }
7445
7446 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007447 * Truncate always works against the rightmost tree branch.
7448 */
Joel Beckerfacdb772009-02-12 18:08:48 -08007449 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
Mark Fashehdcd05382007-01-16 11:32:23 -08007450 if (status) {
7451 mlog_errno(status);
7452 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007453 }
7454
Mark Fashehdcd05382007-01-16 11:32:23 -08007455 mlog(0, "inode->ip_clusters = %u, tree_depth = %u\n",
7456 OCFS2_I(inode)->ip_clusters, path->p_tree_depth);
7457
7458 /*
7459 * By now, el will point to the extent list on the bottom most
7460 * portion of this tree. Only the tail record is considered in
7461 * each pass.
7462 *
7463 * We handle the following cases, in order:
7464 * - empty extent: delete the remaining branch
7465 * - remove the entire record
7466 * - remove a partial record
7467 * - no record needs to be removed (truncate has completed)
7468 */
7469 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007470 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7471 ocfs2_error(inode->i_sb,
7472 "Inode %llu has empty extent block at %llu\n",
7473 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7474 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7475 status = -EROFS;
7476 goto bail;
7477 }
7478
Mark Fashehccd979b2005-12-15 14:31:24 -08007479 i = le16_to_cpu(el->l_next_free_rec) - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08007480 range = le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08007481 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007482 if (i == 0 && ocfs2_is_empty_extent(&el->l_recs[i])) {
7483 clusters_to_del = 0;
7484 } else if (le32_to_cpu(el->l_recs[i].e_cpos) >= new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007485 clusters_to_del = ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08007486 } else if (range > new_highest_cpos) {
Mark Fashehe48edee2007-03-07 16:46:57 -08007487 clusters_to_del = (ocfs2_rec_clusters(el, &el->l_recs[i]) +
Mark Fashehccd979b2005-12-15 14:31:24 -08007488 le32_to_cpu(el->l_recs[i].e_cpos)) -
Mark Fashehdcd05382007-01-16 11:32:23 -08007489 new_highest_cpos;
7490 } else {
7491 status = 0;
7492 goto bail;
7493 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007494
Mark Fashehdcd05382007-01-16 11:32:23 -08007495 mlog(0, "clusters_to_del = %u in this pass, tail blk=%llu\n",
7496 clusters_to_del, (unsigned long long)path_leaf_bh(path)->b_blocknr);
7497
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007498 mutex_lock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007499 tl_sem = 1;
7500 /* ocfs2_truncate_log_needs_flush guarantees us at least one
7501 * record is free for use. If there isn't any, we flush to get
7502 * an empty truncate log. */
7503 if (ocfs2_truncate_log_needs_flush(osb)) {
7504 status = __ocfs2_flush_truncate_log(osb);
7505 if (status < 0) {
7506 mlog_errno(status);
7507 goto bail;
7508 }
7509 }
7510
7511 credits = ocfs2_calc_tree_trunc_credits(osb->sb, clusters_to_del,
Mark Fashehdcd05382007-01-16 11:32:23 -08007512 (struct ocfs2_dinode *)fe_bh->b_data,
7513 el);
Mark Fasheh65eff9c2006-10-09 17:26:22 -07007514 handle = ocfs2_start_trans(osb, credits);
Mark Fashehccd979b2005-12-15 14:31:24 -08007515 if (IS_ERR(handle)) {
7516 status = PTR_ERR(handle);
7517 handle = NULL;
7518 mlog_errno(status);
7519 goto bail;
7520 }
7521
Mark Fashehdcd05382007-01-16 11:32:23 -08007522 status = ocfs2_do_truncate(osb, clusters_to_del, inode, fe_bh, handle,
7523 tc, path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007524 if (status < 0) {
7525 mlog_errno(status);
7526 goto bail;
7527 }
7528
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007529 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007530 tl_sem = 0;
7531
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007532 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007533 handle = NULL;
7534
Mark Fashehdcd05382007-01-16 11:32:23 -08007535 ocfs2_reinit_path(path, 1);
7536
7537 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007538 * The check above will catch the case where we've truncated
7539 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007540 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007541 goto start;
7542
Mark Fashehccd979b2005-12-15 14:31:24 -08007543bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08007544
7545 ocfs2_schedule_truncate_log_flush(osb, 1);
7546
7547 if (tl_sem)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08007548 mutex_unlock(&tl_inode->i_mutex);
Mark Fashehccd979b2005-12-15 14:31:24 -08007549
7550 if (handle)
Mark Fasheh02dc1af2006-10-09 16:48:10 -07007551 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08007552
Mark Fasheh59a5e412007-06-22 15:52:36 -07007553 ocfs2_run_deallocs(osb, &tc->tc_dealloc);
7554
Mark Fashehdcd05382007-01-16 11:32:23 -08007555 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007556
7557 /* This will drop the ext_alloc cluster lock for us */
7558 ocfs2_free_truncate_context(tc);
7559
7560 mlog_exit(status);
7561 return status;
7562}
7563
Mark Fashehccd979b2005-12-15 14:31:24 -08007564/*
Mark Fasheh59a5e412007-06-22 15:52:36 -07007565 * Expects the inode to already be locked.
Mark Fashehccd979b2005-12-15 14:31:24 -08007566 */
7567int ocfs2_prepare_truncate(struct ocfs2_super *osb,
7568 struct inode *inode,
7569 struct buffer_head *fe_bh,
7570 struct ocfs2_truncate_context **tc)
7571{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007572 int status;
Mark Fashehccd979b2005-12-15 14:31:24 -08007573 unsigned int new_i_clusters;
7574 struct ocfs2_dinode *fe;
7575 struct ocfs2_extent_block *eb;
Mark Fashehccd979b2005-12-15 14:31:24 -08007576 struct buffer_head *last_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007577
7578 mlog_entry_void();
7579
7580 *tc = NULL;
7581
7582 new_i_clusters = ocfs2_clusters_for_bytes(osb->sb,
7583 i_size_read(inode));
7584 fe = (struct ocfs2_dinode *) fe_bh->b_data;
7585
7586 mlog(0, "fe->i_clusters = %u, new_i_clusters = %u, fe->i_size ="
Mark Fasheh1ca1a112007-04-27 16:01:25 -07007587 "%llu\n", le32_to_cpu(fe->i_clusters), new_i_clusters,
7588 (unsigned long long)le64_to_cpu(fe->i_size));
Mark Fashehccd979b2005-12-15 14:31:24 -08007589
Robert P. J. Daycd861282006-12-13 00:34:52 -08007590 *tc = kzalloc(sizeof(struct ocfs2_truncate_context), GFP_KERNEL);
Mark Fashehccd979b2005-12-15 14:31:24 -08007591 if (!(*tc)) {
7592 status = -ENOMEM;
7593 mlog_errno(status);
7594 goto bail;
7595 }
Mark Fasheh59a5e412007-06-22 15:52:36 -07007596 ocfs2_init_dealloc_ctxt(&(*tc)->tc_dealloc);
Mark Fashehccd979b2005-12-15 14:31:24 -08007597
Mark Fashehccd979b2005-12-15 14:31:24 -08007598 if (fe->id2.i_list.l_tree_depth) {
Joel Becker3d03a302009-02-12 17:49:26 -08007599 status = ocfs2_read_extent_block(INODE_CACHE(inode),
Joel Becker5e965812008-11-13 14:49:16 -08007600 le64_to_cpu(fe->i_last_eb_blk),
7601 &last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007602 if (status < 0) {
7603 mlog_errno(status);
7604 goto bail;
7605 }
7606 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08007607 }
7608
7609 (*tc)->tc_last_eb_bh = last_eb_bh;
7610
Mark Fashehccd979b2005-12-15 14:31:24 -08007611 status = 0;
7612bail:
7613 if (status < 0) {
7614 if (*tc)
7615 ocfs2_free_truncate_context(*tc);
7616 *tc = NULL;
7617 }
7618 mlog_exit_void();
7619 return status;
7620}
7621
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007622/*
7623 * 'start' is inclusive, 'end' is not.
7624 */
7625int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7626 unsigned int start, unsigned int end, int trunc)
7627{
7628 int ret;
7629 unsigned int numbytes;
7630 handle_t *handle;
7631 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7632 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7633 struct ocfs2_inline_data *idata = &di->id2.i_data;
7634
7635 if (end > i_size_read(inode))
7636 end = i_size_read(inode);
7637
7638 BUG_ON(start >= end);
7639
7640 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7641 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7642 !ocfs2_supports_inline_data(osb)) {
7643 ocfs2_error(inode->i_sb,
7644 "Inline data flags for inode %llu don't agree! "
7645 "Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
7646 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7647 le16_to_cpu(di->i_dyn_features),
7648 OCFS2_I(inode)->ip_dyn_features,
7649 osb->s_feature_incompat);
7650 ret = -EROFS;
7651 goto out;
7652 }
7653
7654 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7655 if (IS_ERR(handle)) {
7656 ret = PTR_ERR(handle);
7657 mlog_errno(ret);
7658 goto out;
7659 }
7660
Joel Becker0cf2f762009-02-12 16:41:25 -08007661 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007662 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007663 if (ret) {
7664 mlog_errno(ret);
7665 goto out_commit;
7666 }
7667
7668 numbytes = end - start;
7669 memset(idata->id_data + start, 0, numbytes);
7670
7671 /*
7672 * No need to worry about the data page here - it's been
7673 * truncated already and inline data doesn't need it for
7674 * pushing zero's to disk, so we'll let readpage pick it up
7675 * later.
7676 */
7677 if (trunc) {
7678 i_size_write(inode, start);
7679 di->i_size = cpu_to_le64(start);
7680 }
7681
7682 inode->i_blocks = ocfs2_inode_sector_count(inode);
7683 inode->i_ctime = inode->i_mtime = CURRENT_TIME;
7684
7685 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7686 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7687
7688 ocfs2_journal_dirty(handle, di_bh);
7689
7690out_commit:
7691 ocfs2_commit_trans(osb, handle);
7692
7693out:
7694 return ret;
7695}
7696
Mark Fashehccd979b2005-12-15 14:31:24 -08007697static void ocfs2_free_truncate_context(struct ocfs2_truncate_context *tc)
7698{
Mark Fasheh59a5e412007-06-22 15:52:36 -07007699 /*
7700 * The caller is responsible for completing deallocation
7701 * before freeing the context.
7702 */
7703 if (tc->tc_dealloc.c_first_suballocator != NULL)
7704 mlog(ML_NOTICE,
7705 "Truncate completion has non-empty dealloc context\n");
Mark Fashehccd979b2005-12-15 14:31:24 -08007706
Mark Fasheha81cb882008-10-07 14:25:16 -07007707 brelse(tc->tc_last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08007708
7709 kfree(tc);
7710}