blob: ab5105f9767edde5aaf5892c272907e8288afc85 [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>
Tao Mae80de362011-05-23 10:36:43 +080032#include <linux/blkdev.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010033#include <linux/sched/signal.h>
Mark Fashehccd979b2005-12-15 14:31:24 -080034
Mark Fashehccd979b2005-12-15 14:31:24 -080035#include <cluster/masklog.h>
36
37#include "ocfs2.h"
38
39#include "alloc.h"
Mark Fasheh60b11392007-02-16 11:46:50 -080040#include "aops.h"
Joel Beckerd6b32bb2008-10-17 14:55:01 -070041#include "blockcheck.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080042#include "dlmglue.h"
43#include "extent_map.h"
44#include "inode.h"
45#include "journal.h"
46#include "localalloc.h"
47#include "suballoc.h"
48#include "sysfile.h"
49#include "file.h"
50#include "super.h"
51#include "uptodate.h"
Joel Becker2a50a742008-12-09 14:24:33 -080052#include "xattr.h"
Tao Mabcbbb242009-08-18 11:29:12 +080053#include "refcounttree.h"
Tao Maa09d09b2011-02-22 08:21:10 +080054#include "ocfs2_trace.h"
Mark Fashehccd979b2005-12-15 14:31:24 -080055
56#include "buffer_head_io.h"
57
Tao Ma853a3a12009-08-18 11:22:18 +080058enum ocfs2_contig_type {
59 CONTIG_NONE = 0,
60 CONTIG_LEFT,
61 CONTIG_RIGHT,
62 CONTIG_LEFTRIGHT,
63};
Tao Mae7d4cb62008-08-18 17:38:44 +080064
Tao Ma853a3a12009-08-18 11:22:18 +080065static enum ocfs2_contig_type
66 ocfs2_extent_rec_contig(struct super_block *sb,
67 struct ocfs2_extent_rec *ext,
68 struct ocfs2_extent_rec *insert_rec);
Joel Becker1625f8a2008-08-21 17:11:10 -070069/*
70 * Operations for a specific extent tree type.
71 *
72 * To implement an on-disk btree (extent tree) type in ocfs2, add
73 * an ocfs2_extent_tree_operations structure and the matching
Joel Becker8d6220d2008-08-22 12:46:09 -070074 * ocfs2_init_<thingy>_extent_tree() function. That's pretty much it
Joel Becker1625f8a2008-08-21 17:11:10 -070075 * for the allocation portion of the extent tree.
76 */
Tao Mae7d4cb62008-08-18 17:38:44 +080077struct ocfs2_extent_tree_operations {
Joel Becker1625f8a2008-08-21 17:11:10 -070078 /*
79 * last_eb_blk is the block number of the right most leaf extent
80 * block. Most on-disk structures containing an extent tree store
81 * this value for fast access. The ->eo_set_last_eb_blk() and
82 * ->eo_get_last_eb_blk() operations access this value. They are
83 * both required.
84 */
Joel Becker35dc0aa2008-08-20 16:25:06 -070085 void (*eo_set_last_eb_blk)(struct ocfs2_extent_tree *et,
86 u64 blkno);
87 u64 (*eo_get_last_eb_blk)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -070088
89 /*
90 * The on-disk structure usually keeps track of how many total
91 * clusters are stored in this extent tree. This function updates
92 * that value. new_clusters is the delta, and must be
93 * added to the total. Required.
94 */
Joel Becker6136ca52009-02-12 19:32:43 -080095 void (*eo_update_clusters)(struct ocfs2_extent_tree *et,
Joel Becker35dc0aa2008-08-20 16:25:06 -070096 u32 new_clusters);
Joel Becker1625f8a2008-08-21 17:11:10 -070097
98 /*
Joel Becker92ba4702009-02-13 03:18:34 -080099 * If this extent tree is supported by an extent map, insert
100 * a record into the map.
101 */
102 void (*eo_extent_map_insert)(struct ocfs2_extent_tree *et,
103 struct ocfs2_extent_rec *rec);
104
105 /*
Joel Becker4c911ee2009-02-13 02:50:12 -0800106 * If this extent tree is supported by an extent map, truncate the
107 * map to clusters,
108 */
109 void (*eo_extent_map_truncate)(struct ocfs2_extent_tree *et,
110 u32 clusters);
111
112 /*
Joel Becker1625f8a2008-08-21 17:11:10 -0700113 * If ->eo_insert_check() exists, it is called before rec is
114 * inserted into the extent tree. It is optional.
115 */
Joel Becker6136ca52009-02-12 19:32:43 -0800116 int (*eo_insert_check)(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700117 struct ocfs2_extent_rec *rec);
Joel Becker6136ca52009-02-12 19:32:43 -0800118 int (*eo_sanity_check)(struct ocfs2_extent_tree *et);
Joel Becker0ce10102008-08-20 17:19:50 -0700119
Joel Becker1625f8a2008-08-21 17:11:10 -0700120 /*
121 * --------------------------------------------------------------
122 * The remaining are internal to ocfs2_extent_tree and don't have
123 * accessor functions
124 */
125
126 /*
127 * ->eo_fill_root_el() takes et->et_object and sets et->et_root_el.
128 * It is required.
129 */
Joel Becker0ce10102008-08-20 17:19:50 -0700130 void (*eo_fill_root_el)(struct ocfs2_extent_tree *et);
Joel Becker1625f8a2008-08-21 17:11:10 -0700131
132 /*
133 * ->eo_fill_max_leaf_clusters sets et->et_max_leaf_clusters if
134 * it exists. If it does not, et->et_max_leaf_clusters is set
135 * to 0 (unlimited). Optional.
136 */
Joel Becker6136ca52009-02-12 19:32:43 -0800137 void (*eo_fill_max_leaf_clusters)(struct ocfs2_extent_tree *et);
Tao Ma853a3a12009-08-18 11:22:18 +0800138
139 /*
140 * ->eo_extent_contig test whether the 2 ocfs2_extent_rec
141 * are contiguous or not. Optional. Don't need to set it if use
142 * ocfs2_extent_rec as the tree leaf.
143 */
144 enum ocfs2_contig_type
145 (*eo_extent_contig)(struct ocfs2_extent_tree *et,
146 struct ocfs2_extent_rec *ext,
147 struct ocfs2_extent_rec *insert_rec);
Tao Mae7d4cb62008-08-18 17:38:44 +0800148};
149
Joel Beckerf99b9b72008-08-20 19:36:33 -0700150
151/*
152 * Pre-declare ocfs2_dinode_et_ops so we can use it as a sanity check
153 * in the methods.
154 */
155static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et);
156static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
157 u64 blkno);
Joel Becker6136ca52009-02-12 19:32:43 -0800158static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700159 u32 clusters);
Joel Becker92ba4702009-02-13 03:18:34 -0800160static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
161 struct ocfs2_extent_rec *rec);
Joel Becker4c911ee2009-02-13 02:50:12 -0800162static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
163 u32 clusters);
Joel Becker6136ca52009-02-12 19:32:43 -0800164static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700165 struct ocfs2_extent_rec *rec);
Joel Becker6136ca52009-02-12 19:32:43 -0800166static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et);
Joel Beckerf99b9b72008-08-20 19:36:33 -0700167static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et);
Julia Lawall9e62dc02016-01-14 15:17:21 -0800168static const struct ocfs2_extent_tree_operations ocfs2_dinode_et_ops = {
Joel Beckerf99b9b72008-08-20 19:36:33 -0700169 .eo_set_last_eb_blk = ocfs2_dinode_set_last_eb_blk,
170 .eo_get_last_eb_blk = ocfs2_dinode_get_last_eb_blk,
171 .eo_update_clusters = ocfs2_dinode_update_clusters,
Joel Becker92ba4702009-02-13 03:18:34 -0800172 .eo_extent_map_insert = ocfs2_dinode_extent_map_insert,
Joel Becker4c911ee2009-02-13 02:50:12 -0800173 .eo_extent_map_truncate = ocfs2_dinode_extent_map_truncate,
Joel Beckerf99b9b72008-08-20 19:36:33 -0700174 .eo_insert_check = ocfs2_dinode_insert_check,
175 .eo_sanity_check = ocfs2_dinode_sanity_check,
176 .eo_fill_root_el = ocfs2_dinode_fill_root_el,
Tao Mae7d4cb62008-08-18 17:38:44 +0800177};
178
179static void ocfs2_dinode_set_last_eb_blk(struct ocfs2_extent_tree *et,
180 u64 blkno)
181{
Joel Beckerea5efa12008-08-20 16:57:27 -0700182 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800183
Joel Beckerf99b9b72008-08-20 19:36:33 -0700184 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800185 di->i_last_eb_blk = cpu_to_le64(blkno);
186}
187
188static u64 ocfs2_dinode_get_last_eb_blk(struct ocfs2_extent_tree *et)
189{
Joel Beckerea5efa12008-08-20 16:57:27 -0700190 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800191
Joel Beckerf99b9b72008-08-20 19:36:33 -0700192 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800193 return le64_to_cpu(di->i_last_eb_blk);
194}
195
Joel Becker6136ca52009-02-12 19:32:43 -0800196static void ocfs2_dinode_update_clusters(struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +0800197 u32 clusters)
198{
Joel Becker6136ca52009-02-12 19:32:43 -0800199 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
Joel Beckerea5efa12008-08-20 16:57:27 -0700200 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800201
202 le32_add_cpu(&di->i_clusters, clusters);
Joel Becker6136ca52009-02-12 19:32:43 -0800203 spin_lock(&oi->ip_lock);
204 oi->ip_clusters = le32_to_cpu(di->i_clusters);
205 spin_unlock(&oi->ip_lock);
Tao Mae7d4cb62008-08-18 17:38:44 +0800206}
207
Joel Becker92ba4702009-02-13 03:18:34 -0800208static void ocfs2_dinode_extent_map_insert(struct ocfs2_extent_tree *et,
209 struct ocfs2_extent_rec *rec)
210{
211 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
212
213 ocfs2_extent_map_insert_rec(inode, rec);
214}
215
Joel Becker4c911ee2009-02-13 02:50:12 -0800216static void ocfs2_dinode_extent_map_truncate(struct ocfs2_extent_tree *et,
217 u32 clusters)
218{
219 struct inode *inode = &cache_info_to_inode(et->et_ci)->vfs_inode;
220
221 ocfs2_extent_map_trunc(inode, clusters);
222}
223
Joel Becker6136ca52009-02-12 19:32:43 -0800224static int ocfs2_dinode_insert_check(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700225 struct ocfs2_extent_rec *rec)
226{
Joel Becker6136ca52009-02-12 19:32:43 -0800227 struct ocfs2_inode_info *oi = cache_info_to_inode(et->et_ci);
228 struct ocfs2_super *osb = OCFS2_SB(oi->vfs_inode.i_sb);
Joel Becker1e61ee72008-08-20 18:32:45 -0700229
Joel Becker6136ca52009-02-12 19:32:43 -0800230 BUG_ON(oi->ip_dyn_features & OCFS2_INLINE_DATA_FL);
Joel Becker1e61ee72008-08-20 18:32:45 -0700231 mlog_bug_on_msg(!ocfs2_sparse_alloc(osb) &&
Joel Becker6136ca52009-02-12 19:32:43 -0800232 (oi->ip_clusters != le32_to_cpu(rec->e_cpos)),
Joel Becker1e61ee72008-08-20 18:32:45 -0700233 "Device %s, asking for sparse allocation: inode %llu, "
234 "cpos %u, clusters %u\n",
235 osb->dev_str,
Joel Becker6136ca52009-02-12 19:32:43 -0800236 (unsigned long long)oi->ip_blkno,
237 rec->e_cpos, oi->ip_clusters);
Joel Becker1e61ee72008-08-20 18:32:45 -0700238
239 return 0;
240}
241
Joel Becker6136ca52009-02-12 19:32:43 -0800242static int ocfs2_dinode_sanity_check(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800243{
Joel Becker10995aa2008-11-13 14:49:12 -0800244 struct ocfs2_dinode *di = et->et_object;
Tao Mae7d4cb62008-08-18 17:38:44 +0800245
Joel Beckerf99b9b72008-08-20 19:36:33 -0700246 BUG_ON(et->et_ops != &ocfs2_dinode_et_ops);
Joel Becker10995aa2008-11-13 14:49:12 -0800247 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
Tao Mae7d4cb62008-08-18 17:38:44 +0800248
Joel Becker10995aa2008-11-13 14:49:12 -0800249 return 0;
Tao Mae7d4cb62008-08-18 17:38:44 +0800250}
251
Joel Beckerf99b9b72008-08-20 19:36:33 -0700252static void ocfs2_dinode_fill_root_el(struct ocfs2_extent_tree *et)
253{
254 struct ocfs2_dinode *di = et->et_object;
255
256 et->et_root_el = &di->id2.i_list;
257}
258
Tao Mae7d4cb62008-08-18 17:38:44 +0800259
Joel Becker0ce10102008-08-20 17:19:50 -0700260static void ocfs2_xattr_value_fill_root_el(struct ocfs2_extent_tree *et)
261{
Joel Becker2a50a742008-12-09 14:24:33 -0800262 struct ocfs2_xattr_value_buf *vb = et->et_object;
Joel Becker0ce10102008-08-20 17:19:50 -0700263
Joel Becker2a50a742008-12-09 14:24:33 -0800264 et->et_root_el = &vb->vb_xv->xr_list;
Joel Becker0ce10102008-08-20 17:19:50 -0700265}
266
Tao Maf56654c2008-08-18 17:38:48 +0800267static void ocfs2_xattr_value_set_last_eb_blk(struct ocfs2_extent_tree *et,
268 u64 blkno)
269{
Joel Becker2a50a742008-12-09 14:24:33 -0800270 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800271
Joel Becker2a50a742008-12-09 14:24:33 -0800272 vb->vb_xv->xr_last_eb_blk = cpu_to_le64(blkno);
Tao Maf56654c2008-08-18 17:38:48 +0800273}
274
275static u64 ocfs2_xattr_value_get_last_eb_blk(struct ocfs2_extent_tree *et)
276{
Joel Becker2a50a742008-12-09 14:24:33 -0800277 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800278
Joel Becker2a50a742008-12-09 14:24:33 -0800279 return le64_to_cpu(vb->vb_xv->xr_last_eb_blk);
Tao Maf56654c2008-08-18 17:38:48 +0800280}
281
Joel Becker6136ca52009-02-12 19:32:43 -0800282static void ocfs2_xattr_value_update_clusters(struct ocfs2_extent_tree *et,
Tao Maf56654c2008-08-18 17:38:48 +0800283 u32 clusters)
284{
Joel Becker2a50a742008-12-09 14:24:33 -0800285 struct ocfs2_xattr_value_buf *vb = et->et_object;
Tao Maf56654c2008-08-18 17:38:48 +0800286
Joel Becker2a50a742008-12-09 14:24:33 -0800287 le32_add_cpu(&vb->vb_xv->xr_clusters, clusters);
Tao Maf56654c2008-08-18 17:38:48 +0800288}
289
Julia Lawall9e62dc02016-01-14 15:17:21 -0800290static const struct ocfs2_extent_tree_operations ocfs2_xattr_value_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700291 .eo_set_last_eb_blk = ocfs2_xattr_value_set_last_eb_blk,
292 .eo_get_last_eb_blk = ocfs2_xattr_value_get_last_eb_blk,
293 .eo_update_clusters = ocfs2_xattr_value_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700294 .eo_fill_root_el = ocfs2_xattr_value_fill_root_el,
Tao Maf56654c2008-08-18 17:38:48 +0800295};
296
Joel Becker0ce10102008-08-20 17:19:50 -0700297static void ocfs2_xattr_tree_fill_root_el(struct ocfs2_extent_tree *et)
298{
299 struct ocfs2_xattr_block *xb = et->et_object;
300
301 et->et_root_el = &xb->xb_attrs.xb_root.xt_list;
302}
303
Joel Becker6136ca52009-02-12 19:32:43 -0800304static void ocfs2_xattr_tree_fill_max_leaf_clusters(struct ocfs2_extent_tree *et)
Joel Becker943cced2008-08-20 17:31:10 -0700305{
Joel Becker6136ca52009-02-12 19:32:43 -0800306 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Joel Becker943cced2008-08-20 17:31:10 -0700307 et->et_max_leaf_clusters =
Joel Becker6136ca52009-02-12 19:32:43 -0800308 ocfs2_clusters_for_bytes(sb, OCFS2_MAX_XATTR_TREE_LEAF_SIZE);
Joel Becker943cced2008-08-20 17:31:10 -0700309}
310
Tao Maba492612008-08-18 17:38:49 +0800311static void ocfs2_xattr_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
312 u64 blkno)
313{
Joel Beckerea5efa12008-08-20 16:57:27 -0700314 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800315 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
316
317 xt->xt_last_eb_blk = cpu_to_le64(blkno);
318}
319
320static u64 ocfs2_xattr_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
321{
Joel Beckerea5efa12008-08-20 16:57:27 -0700322 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800323 struct ocfs2_xattr_tree_root *xt = &xb->xb_attrs.xb_root;
324
325 return le64_to_cpu(xt->xt_last_eb_blk);
326}
327
Joel Becker6136ca52009-02-12 19:32:43 -0800328static void ocfs2_xattr_tree_update_clusters(struct ocfs2_extent_tree *et,
Tao Maba492612008-08-18 17:38:49 +0800329 u32 clusters)
330{
Joel Beckerea5efa12008-08-20 16:57:27 -0700331 struct ocfs2_xattr_block *xb = et->et_object;
Tao Maba492612008-08-18 17:38:49 +0800332
333 le32_add_cpu(&xb->xb_attrs.xb_root.xt_clusters, clusters);
334}
335
Julia Lawall9e62dc02016-01-14 15:17:21 -0800336static const struct ocfs2_extent_tree_operations ocfs2_xattr_tree_et_ops = {
Joel Becker35dc0aa2008-08-20 16:25:06 -0700337 .eo_set_last_eb_blk = ocfs2_xattr_tree_set_last_eb_blk,
338 .eo_get_last_eb_blk = ocfs2_xattr_tree_get_last_eb_blk,
339 .eo_update_clusters = ocfs2_xattr_tree_update_clusters,
Joel Becker0ce10102008-08-20 17:19:50 -0700340 .eo_fill_root_el = ocfs2_xattr_tree_fill_root_el,
Joel Becker943cced2008-08-20 17:31:10 -0700341 .eo_fill_max_leaf_clusters = ocfs2_xattr_tree_fill_max_leaf_clusters,
Tao Maba492612008-08-18 17:38:49 +0800342};
343
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800344static void ocfs2_dx_root_set_last_eb_blk(struct ocfs2_extent_tree *et,
345 u64 blkno)
346{
347 struct ocfs2_dx_root_block *dx_root = et->et_object;
348
349 dx_root->dr_last_eb_blk = cpu_to_le64(blkno);
350}
351
352static u64 ocfs2_dx_root_get_last_eb_blk(struct ocfs2_extent_tree *et)
353{
354 struct ocfs2_dx_root_block *dx_root = et->et_object;
355
356 return le64_to_cpu(dx_root->dr_last_eb_blk);
357}
358
Joel Becker6136ca52009-02-12 19:32:43 -0800359static void ocfs2_dx_root_update_clusters(struct ocfs2_extent_tree *et,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800360 u32 clusters)
361{
362 struct ocfs2_dx_root_block *dx_root = et->et_object;
363
364 le32_add_cpu(&dx_root->dr_clusters, clusters);
365}
366
Joel Becker6136ca52009-02-12 19:32:43 -0800367static int ocfs2_dx_root_sanity_check(struct ocfs2_extent_tree *et)
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800368{
369 struct ocfs2_dx_root_block *dx_root = et->et_object;
370
371 BUG_ON(!OCFS2_IS_VALID_DX_ROOT(dx_root));
372
373 return 0;
374}
375
376static void ocfs2_dx_root_fill_root_el(struct ocfs2_extent_tree *et)
377{
378 struct ocfs2_dx_root_block *dx_root = et->et_object;
379
380 et->et_root_el = &dx_root->dr_list;
381}
382
Julia Lawall9e62dc02016-01-14 15:17:21 -0800383static const struct ocfs2_extent_tree_operations ocfs2_dx_root_et_ops = {
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800384 .eo_set_last_eb_blk = ocfs2_dx_root_set_last_eb_blk,
385 .eo_get_last_eb_blk = ocfs2_dx_root_get_last_eb_blk,
386 .eo_update_clusters = ocfs2_dx_root_update_clusters,
387 .eo_sanity_check = ocfs2_dx_root_sanity_check,
388 .eo_fill_root_el = ocfs2_dx_root_fill_root_el,
389};
390
Tao Mafe924412009-08-18 11:22:25 +0800391static void ocfs2_refcount_tree_fill_root_el(struct ocfs2_extent_tree *et)
392{
393 struct ocfs2_refcount_block *rb = et->et_object;
394
395 et->et_root_el = &rb->rf_list;
396}
397
398static void ocfs2_refcount_tree_set_last_eb_blk(struct ocfs2_extent_tree *et,
399 u64 blkno)
400{
401 struct ocfs2_refcount_block *rb = et->et_object;
402
403 rb->rf_last_eb_blk = cpu_to_le64(blkno);
404}
405
406static u64 ocfs2_refcount_tree_get_last_eb_blk(struct ocfs2_extent_tree *et)
407{
408 struct ocfs2_refcount_block *rb = et->et_object;
409
410 return le64_to_cpu(rb->rf_last_eb_blk);
411}
412
413static void ocfs2_refcount_tree_update_clusters(struct ocfs2_extent_tree *et,
414 u32 clusters)
415{
416 struct ocfs2_refcount_block *rb = et->et_object;
417
418 le32_add_cpu(&rb->rf_clusters, clusters);
419}
420
421static enum ocfs2_contig_type
422ocfs2_refcount_tree_extent_contig(struct ocfs2_extent_tree *et,
423 struct ocfs2_extent_rec *ext,
424 struct ocfs2_extent_rec *insert_rec)
425{
426 return CONTIG_NONE;
427}
428
Julia Lawall9e62dc02016-01-14 15:17:21 -0800429static const struct ocfs2_extent_tree_operations ocfs2_refcount_tree_et_ops = {
Tao Mafe924412009-08-18 11:22:25 +0800430 .eo_set_last_eb_blk = ocfs2_refcount_tree_set_last_eb_blk,
431 .eo_get_last_eb_blk = ocfs2_refcount_tree_get_last_eb_blk,
432 .eo_update_clusters = ocfs2_refcount_tree_update_clusters,
433 .eo_fill_root_el = ocfs2_refcount_tree_fill_root_el,
434 .eo_extent_contig = ocfs2_refcount_tree_extent_contig,
435};
436
Joel Becker8d6220d2008-08-22 12:46:09 -0700437static void __ocfs2_init_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800438 struct ocfs2_caching_info *ci,
Joel Becker8d6220d2008-08-22 12:46:09 -0700439 struct buffer_head *bh,
Joel Becker13723d02008-10-17 19:25:01 -0700440 ocfs2_journal_access_func access,
Joel Becker8d6220d2008-08-22 12:46:09 -0700441 void *obj,
Julia Lawall9e62dc02016-01-14 15:17:21 -0800442 const struct ocfs2_extent_tree_operations *ops)
Tao Mae7d4cb62008-08-18 17:38:44 +0800443{
Joel Becker1a09f552008-08-20 17:44:24 -0700444 et->et_ops = ops;
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700445 et->et_root_bh = bh;
Joel Becker5e404e92009-02-13 03:54:22 -0800446 et->et_ci = ci;
Joel Becker13723d02008-10-17 19:25:01 -0700447 et->et_root_journal_access = access;
Joel Beckerea5efa12008-08-20 16:57:27 -0700448 if (!obj)
449 obj = (void *)bh->b_data;
450 et->et_object = obj;
Tao Mae7d4cb62008-08-18 17:38:44 +0800451
Joel Becker0ce10102008-08-20 17:19:50 -0700452 et->et_ops->eo_fill_root_el(et);
Joel Becker943cced2008-08-20 17:31:10 -0700453 if (!et->et_ops->eo_fill_max_leaf_clusters)
454 et->et_max_leaf_clusters = 0;
455 else
Joel Becker6136ca52009-02-12 19:32:43 -0800456 et->et_ops->eo_fill_max_leaf_clusters(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800457}
458
Joel Becker8d6220d2008-08-22 12:46:09 -0700459void ocfs2_init_dinode_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800460 struct ocfs2_caching_info *ci,
Joel Becker8d6220d2008-08-22 12:46:09 -0700461 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700462{
Joel Becker5e404e92009-02-13 03:54:22 -0800463 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_di,
Joel Becker13723d02008-10-17 19:25:01 -0700464 NULL, &ocfs2_dinode_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700465}
466
Joel Becker8d6220d2008-08-22 12:46:09 -0700467void ocfs2_init_xattr_tree_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800468 struct ocfs2_caching_info *ci,
Joel Becker8d6220d2008-08-22 12:46:09 -0700469 struct buffer_head *bh)
Joel Becker1a09f552008-08-20 17:44:24 -0700470{
Joel Becker5e404e92009-02-13 03:54:22 -0800471 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_xb,
Joel Becker13723d02008-10-17 19:25:01 -0700472 NULL, &ocfs2_xattr_tree_et_ops);
Joel Becker1a09f552008-08-20 17:44:24 -0700473}
474
Joel Becker8d6220d2008-08-22 12:46:09 -0700475void ocfs2_init_xattr_value_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800476 struct ocfs2_caching_info *ci,
Joel Becker2a50a742008-12-09 14:24:33 -0800477 struct ocfs2_xattr_value_buf *vb)
Tao Mae7d4cb62008-08-18 17:38:44 +0800478{
Joel Becker5e404e92009-02-13 03:54:22 -0800479 __ocfs2_init_extent_tree(et, ci, vb->vb_bh, vb->vb_access, vb,
Joel Becker8d6220d2008-08-22 12:46:09 -0700480 &ocfs2_xattr_value_et_ops);
Tao Mae7d4cb62008-08-18 17:38:44 +0800481}
482
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800483void ocfs2_init_dx_root_extent_tree(struct ocfs2_extent_tree *et,
Joel Becker5e404e92009-02-13 03:54:22 -0800484 struct ocfs2_caching_info *ci,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800485 struct buffer_head *bh)
486{
Joel Becker5e404e92009-02-13 03:54:22 -0800487 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_dr,
Mark Fasheh9b7895e2008-11-12 16:27:44 -0800488 NULL, &ocfs2_dx_root_et_ops);
489}
490
Tao Mafe924412009-08-18 11:22:25 +0800491void ocfs2_init_refcount_extent_tree(struct ocfs2_extent_tree *et,
492 struct ocfs2_caching_info *ci,
493 struct buffer_head *bh)
494{
495 __ocfs2_init_extent_tree(et, ci, bh, ocfs2_journal_access_rb,
496 NULL, &ocfs2_refcount_tree_et_ops);
497}
498
Joel Becker35dc0aa2008-08-20 16:25:06 -0700499static inline void ocfs2_et_set_last_eb_blk(struct ocfs2_extent_tree *et,
500 u64 new_last_eb_blk)
Tao Mae7d4cb62008-08-18 17:38:44 +0800501{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700502 et->et_ops->eo_set_last_eb_blk(et, new_last_eb_blk);
Tao Mae7d4cb62008-08-18 17:38:44 +0800503}
504
Joel Becker35dc0aa2008-08-20 16:25:06 -0700505static inline u64 ocfs2_et_get_last_eb_blk(struct ocfs2_extent_tree *et)
Tao Mae7d4cb62008-08-18 17:38:44 +0800506{
Joel Beckerce1d9ea2008-08-20 16:30:07 -0700507 return et->et_ops->eo_get_last_eb_blk(et);
Tao Mae7d4cb62008-08-18 17:38:44 +0800508}
509
Joel Becker6136ca52009-02-12 19:32:43 -0800510static inline void ocfs2_et_update_clusters(struct ocfs2_extent_tree *et,
Joel Becker35dc0aa2008-08-20 16:25:06 -0700511 u32 clusters)
Tao Mae7d4cb62008-08-18 17:38:44 +0800512{
Joel Becker6136ca52009-02-12 19:32:43 -0800513 et->et_ops->eo_update_clusters(et, clusters);
Joel Becker35dc0aa2008-08-20 16:25:06 -0700514}
515
Joel Becker92ba4702009-02-13 03:18:34 -0800516static inline void ocfs2_et_extent_map_insert(struct ocfs2_extent_tree *et,
517 struct ocfs2_extent_rec *rec)
518{
519 if (et->et_ops->eo_extent_map_insert)
520 et->et_ops->eo_extent_map_insert(et, rec);
521}
522
Joel Becker4c911ee2009-02-13 02:50:12 -0800523static inline void ocfs2_et_extent_map_truncate(struct ocfs2_extent_tree *et,
524 u32 clusters)
525{
526 if (et->et_ops->eo_extent_map_truncate)
527 et->et_ops->eo_extent_map_truncate(et, clusters);
528}
529
Joel Becker13723d02008-10-17 19:25:01 -0700530static inline int ocfs2_et_root_journal_access(handle_t *handle,
Joel Becker13723d02008-10-17 19:25:01 -0700531 struct ocfs2_extent_tree *et,
532 int type)
533{
Joel Beckerd9a0a1f2009-02-12 17:32:34 -0800534 return et->et_root_journal_access(handle, et->et_ci, et->et_root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700535 type);
536}
537
Tao Ma853a3a12009-08-18 11:22:18 +0800538static inline enum ocfs2_contig_type
539 ocfs2_et_extent_contig(struct ocfs2_extent_tree *et,
540 struct ocfs2_extent_rec *rec,
541 struct ocfs2_extent_rec *insert_rec)
542{
543 if (et->et_ops->eo_extent_contig)
544 return et->et_ops->eo_extent_contig(et, rec, insert_rec);
545
546 return ocfs2_extent_rec_contig(
547 ocfs2_metadata_cache_get_super(et->et_ci),
548 rec, insert_rec);
549}
550
Joel Becker6136ca52009-02-12 19:32:43 -0800551static inline int ocfs2_et_insert_check(struct ocfs2_extent_tree *et,
Joel Becker1e61ee72008-08-20 18:32:45 -0700552 struct ocfs2_extent_rec *rec)
553{
554 int ret = 0;
555
556 if (et->et_ops->eo_insert_check)
Joel Becker6136ca52009-02-12 19:32:43 -0800557 ret = et->et_ops->eo_insert_check(et, rec);
Joel Becker1e61ee72008-08-20 18:32:45 -0700558 return ret;
559}
560
Joel Becker6136ca52009-02-12 19:32:43 -0800561static inline int ocfs2_et_sanity_check(struct ocfs2_extent_tree *et)
Joel Becker35dc0aa2008-08-20 16:25:06 -0700562{
Joel Becker1e61ee72008-08-20 18:32:45 -0700563 int ret = 0;
564
565 if (et->et_ops->eo_sanity_check)
Joel Becker6136ca52009-02-12 19:32:43 -0800566 ret = et->et_ops->eo_sanity_check(et);
Joel Becker1e61ee72008-08-20 18:32:45 -0700567 return ret;
Tao Mae7d4cb62008-08-18 17:38:44 +0800568}
569
Mark Fasheh59a5e412007-06-22 15:52:36 -0700570static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
571 struct ocfs2_extent_block *eb);
Joel Beckerd401dc12009-02-13 02:24:10 -0800572static void ocfs2_adjust_rightmost_records(handle_t *handle,
573 struct ocfs2_extent_tree *et,
Tao Ma6b791bc2009-06-12 14:18:36 +0800574 struct ocfs2_path *path,
575 struct ocfs2_extent_rec *insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -0800576/*
577 * Reset the actual path elements so that we can re-use the structure
578 * to build another path. Generally, this involves freeing the buffer
579 * heads.
580 */
Tao Mae2e9f602009-08-18 11:22:34 +0800581void ocfs2_reinit_path(struct ocfs2_path *path, int keep_root)
Mark Fashehdcd05382007-01-16 11:32:23 -0800582{
583 int i, start = 0, depth = 0;
584 struct ocfs2_path_item *node;
585
586 if (keep_root)
587 start = 1;
588
589 for(i = start; i < path_num_items(path); i++) {
590 node = &path->p_node[i];
591
592 brelse(node->bh);
593 node->bh = NULL;
594 node->el = NULL;
595 }
596
597 /*
598 * Tree depth may change during truncate, or insert. If we're
599 * keeping the root extent list, then make sure that our path
600 * structure reflects the proper depth.
601 */
602 if (keep_root)
603 depth = le16_to_cpu(path_root_el(path)->l_tree_depth);
Joel Becker13723d02008-10-17 19:25:01 -0700604 else
605 path_root_access(path) = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -0800606
607 path->p_tree_depth = depth;
608}
609
Tao Mae2e9f602009-08-18 11:22:34 +0800610void ocfs2_free_path(struct ocfs2_path *path)
Mark Fashehdcd05382007-01-16 11:32:23 -0800611{
612 if (path) {
613 ocfs2_reinit_path(path, 0);
614 kfree(path);
615 }
616}
617
618/*
Mark Fasheh328d5752007-06-18 10:48:04 -0700619 * All the elements of src into dest. After this call, src could be freed
620 * without affecting dest.
621 *
622 * Both paths should have the same root. Any non-root elements of dest
623 * will be freed.
624 */
625static void ocfs2_cp_path(struct ocfs2_path *dest, struct ocfs2_path *src)
626{
627 int i;
628
629 BUG_ON(path_root_bh(dest) != path_root_bh(src));
630 BUG_ON(path_root_el(dest) != path_root_el(src));
Joel Becker13723d02008-10-17 19:25:01 -0700631 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fasheh328d5752007-06-18 10:48:04 -0700632
633 ocfs2_reinit_path(dest, 1);
634
635 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
636 dest->p_node[i].bh = src->p_node[i].bh;
637 dest->p_node[i].el = src->p_node[i].el;
638
639 if (dest->p_node[i].bh)
640 get_bh(dest->p_node[i].bh);
641 }
642}
643
644/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800645 * Make the *dest path the same as src and re-initialize src path to
646 * have a root only.
647 */
648static void ocfs2_mv_path(struct ocfs2_path *dest, struct ocfs2_path *src)
649{
650 int i;
651
652 BUG_ON(path_root_bh(dest) != path_root_bh(src));
Joel Becker13723d02008-10-17 19:25:01 -0700653 BUG_ON(path_root_access(dest) != path_root_access(src));
Mark Fashehdcd05382007-01-16 11:32:23 -0800654
655 for(i = 1; i < OCFS2_MAX_PATH_DEPTH; i++) {
656 brelse(dest->p_node[i].bh);
657
658 dest->p_node[i].bh = src->p_node[i].bh;
659 dest->p_node[i].el = src->p_node[i].el;
660
661 src->p_node[i].bh = NULL;
662 src->p_node[i].el = NULL;
663 }
664}
665
666/*
667 * Insert an extent block at given index.
668 *
669 * This will not take an additional reference on eb_bh.
670 */
671static inline void ocfs2_path_insert_eb(struct ocfs2_path *path, int index,
672 struct buffer_head *eb_bh)
673{
674 struct ocfs2_extent_block *eb = (struct ocfs2_extent_block *)eb_bh->b_data;
675
676 /*
677 * Right now, no root bh is an extent block, so this helps
678 * catch code errors with dinode trees. The assertion can be
679 * safely removed if we ever need to insert extent block
680 * structures at the root.
681 */
682 BUG_ON(index == 0);
683
684 path->p_node[index].bh = eb_bh;
685 path->p_node[index].el = &eb->h_list;
686}
687
688static struct ocfs2_path *ocfs2_new_path(struct buffer_head *root_bh,
Joel Becker13723d02008-10-17 19:25:01 -0700689 struct ocfs2_extent_list *root_el,
690 ocfs2_journal_access_func access)
Mark Fashehdcd05382007-01-16 11:32:23 -0800691{
692 struct ocfs2_path *path;
693
694 BUG_ON(le16_to_cpu(root_el->l_tree_depth) >= OCFS2_MAX_PATH_DEPTH);
695
696 path = kzalloc(sizeof(*path), GFP_NOFS);
697 if (path) {
698 path->p_tree_depth = le16_to_cpu(root_el->l_tree_depth);
699 get_bh(root_bh);
700 path_root_bh(path) = root_bh;
701 path_root_el(path) = root_el;
Joel Becker13723d02008-10-17 19:25:01 -0700702 path_root_access(path) = access;
Mark Fashehdcd05382007-01-16 11:32:23 -0800703 }
704
705 return path;
706}
707
Tao Mae2e9f602009-08-18 11:22:34 +0800708struct ocfs2_path *ocfs2_new_path_from_path(struct ocfs2_path *path)
Joel Beckerffdd7a52008-10-17 22:32:01 -0700709{
Joel Becker13723d02008-10-17 19:25:01 -0700710 return ocfs2_new_path(path_root_bh(path), path_root_el(path),
711 path_root_access(path));
Joel Beckerffdd7a52008-10-17 22:32:01 -0700712}
713
Tao Mae2e9f602009-08-18 11:22:34 +0800714struct ocfs2_path *ocfs2_new_path_from_et(struct ocfs2_extent_tree *et)
Joel Beckerffdd7a52008-10-17 22:32:01 -0700715{
Joel Becker13723d02008-10-17 19:25:01 -0700716 return ocfs2_new_path(et->et_root_bh, et->et_root_el,
717 et->et_root_journal_access);
718}
719
720/*
721 * Journal the buffer at depth idx. All idx>0 are extent_blocks,
722 * otherwise it's the root_access function.
723 *
724 * I don't like the way this function's name looks next to
725 * ocfs2_journal_access_path(), but I don't have a better one.
726 */
Tao Mae2e9f602009-08-18 11:22:34 +0800727int ocfs2_path_bh_journal_access(handle_t *handle,
728 struct ocfs2_caching_info *ci,
729 struct ocfs2_path *path,
730 int idx)
Joel Becker13723d02008-10-17 19:25:01 -0700731{
732 ocfs2_journal_access_func access = path_root_access(path);
733
734 if (!access)
735 access = ocfs2_journal_access;
736
737 if (idx)
738 access = ocfs2_journal_access_eb;
739
Joel Becker0cf2f762009-02-12 16:41:25 -0800740 return access(handle, ci, path->p_node[idx].bh,
Joel Becker13723d02008-10-17 19:25:01 -0700741 OCFS2_JOURNAL_ACCESS_WRITE);
Joel Beckerffdd7a52008-10-17 22:32:01 -0700742}
743
Mark Fashehdcd05382007-01-16 11:32:23 -0800744/*
Mark Fashehdcd05382007-01-16 11:32:23 -0800745 * Convenience function to journal all components in a path.
746 */
Tao Mae2e9f602009-08-18 11:22:34 +0800747int ocfs2_journal_access_path(struct ocfs2_caching_info *ci,
748 handle_t *handle,
749 struct ocfs2_path *path)
Mark Fashehdcd05382007-01-16 11:32:23 -0800750{
751 int i, ret = 0;
752
753 if (!path)
754 goto out;
755
756 for(i = 0; i < path_num_items(path); i++) {
Joel Becker0cf2f762009-02-12 16:41:25 -0800757 ret = ocfs2_path_bh_journal_access(handle, ci, path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -0800758 if (ret < 0) {
759 mlog_errno(ret);
760 goto out;
761 }
762 }
763
764out:
765 return ret;
766}
767
Mark Fasheh328d5752007-06-18 10:48:04 -0700768/*
769 * Return the index of the extent record which contains cluster #v_cluster.
770 * -1 is returned if it was not found.
771 *
772 * Should work fine on interior and exterior nodes.
773 */
774int ocfs2_search_extent_list(struct ocfs2_extent_list *el, u32 v_cluster)
775{
776 int ret = -1;
777 int i;
778 struct ocfs2_extent_rec *rec;
779 u32 rec_end, rec_start, clusters;
780
781 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
782 rec = &el->l_recs[i];
783
784 rec_start = le32_to_cpu(rec->e_cpos);
785 clusters = ocfs2_rec_clusters(el, rec);
786
787 rec_end = rec_start + clusters;
788
789 if (v_cluster >= rec_start && v_cluster < rec_end) {
790 ret = i;
791 break;
792 }
793 }
794
795 return ret;
796}
797
Mark Fashehe48edee2007-03-07 16:46:57 -0800798/*
799 * NOTE: ocfs2_block_extent_contig(), ocfs2_extents_adjacent() and
Tao Ma853a3a12009-08-18 11:22:18 +0800800 * ocfs2_extent_rec_contig only work properly against leaf nodes!
Mark Fashehe48edee2007-03-07 16:46:57 -0800801 */
Mark Fashehdcd05382007-01-16 11:32:23 -0800802static int ocfs2_block_extent_contig(struct super_block *sb,
803 struct ocfs2_extent_rec *ext,
804 u64 blkno)
Mark Fashehccd979b2005-12-15 14:31:24 -0800805{
Mark Fashehe48edee2007-03-07 16:46:57 -0800806 u64 blk_end = le64_to_cpu(ext->e_blkno);
807
808 blk_end += ocfs2_clusters_to_blocks(sb,
809 le16_to_cpu(ext->e_leaf_clusters));
810
811 return blkno == blk_end;
Mark Fashehccd979b2005-12-15 14:31:24 -0800812}
813
Mark Fashehdcd05382007-01-16 11:32:23 -0800814static int ocfs2_extents_adjacent(struct ocfs2_extent_rec *left,
815 struct ocfs2_extent_rec *right)
816{
Mark Fashehe48edee2007-03-07 16:46:57 -0800817 u32 left_range;
818
819 left_range = le32_to_cpu(left->e_cpos) +
820 le16_to_cpu(left->e_leaf_clusters);
821
822 return (left_range == le32_to_cpu(right->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -0800823}
824
825static enum ocfs2_contig_type
Tao Ma853a3a12009-08-18 11:22:18 +0800826 ocfs2_extent_rec_contig(struct super_block *sb,
827 struct ocfs2_extent_rec *ext,
828 struct ocfs2_extent_rec *insert_rec)
Mark Fashehdcd05382007-01-16 11:32:23 -0800829{
830 u64 blkno = le64_to_cpu(insert_rec->e_blkno);
831
Mark Fasheh328d5752007-06-18 10:48:04 -0700832 /*
833 * Refuse to coalesce extent records with different flag
834 * fields - we don't want to mix unwritten extents with user
835 * data.
836 */
837 if (ext->e_flags != insert_rec->e_flags)
838 return CONTIG_NONE;
839
Mark Fashehdcd05382007-01-16 11:32:23 -0800840 if (ocfs2_extents_adjacent(ext, insert_rec) &&
Joel Beckerb4a17652009-02-13 03:07:09 -0800841 ocfs2_block_extent_contig(sb, ext, blkno))
Mark Fashehdcd05382007-01-16 11:32:23 -0800842 return CONTIG_RIGHT;
843
844 blkno = le64_to_cpu(ext->e_blkno);
845 if (ocfs2_extents_adjacent(insert_rec, ext) &&
Joel Beckerb4a17652009-02-13 03:07:09 -0800846 ocfs2_block_extent_contig(sb, insert_rec, blkno))
Mark Fashehdcd05382007-01-16 11:32:23 -0800847 return CONTIG_LEFT;
848
849 return CONTIG_NONE;
850}
851
852/*
853 * NOTE: We can have pretty much any combination of contiguousness and
854 * appending.
855 *
856 * The usefulness of APPEND_TAIL is more in that it lets us know that
857 * we'll have to update the path to that leaf.
858 */
859enum ocfs2_append_type {
860 APPEND_NONE = 0,
861 APPEND_TAIL,
862};
863
Mark Fasheh328d5752007-06-18 10:48:04 -0700864enum ocfs2_split_type {
865 SPLIT_NONE = 0,
866 SPLIT_LEFT,
867 SPLIT_RIGHT,
868};
869
Mark Fashehdcd05382007-01-16 11:32:23 -0800870struct ocfs2_insert_type {
Mark Fasheh328d5752007-06-18 10:48:04 -0700871 enum ocfs2_split_type ins_split;
Mark Fashehdcd05382007-01-16 11:32:23 -0800872 enum ocfs2_append_type ins_appending;
873 enum ocfs2_contig_type ins_contig;
874 int ins_contig_index;
Mark Fashehdcd05382007-01-16 11:32:23 -0800875 int ins_tree_depth;
876};
877
Mark Fasheh328d5752007-06-18 10:48:04 -0700878struct ocfs2_merge_ctxt {
879 enum ocfs2_contig_type c_contig_type;
880 int c_has_empty_extent;
881 int c_split_covers_rec;
Mark Fasheh328d5752007-06-18 10:48:04 -0700882};
883
Joel Becker5e965812008-11-13 14:49:16 -0800884static int ocfs2_validate_extent_block(struct super_block *sb,
885 struct buffer_head *bh)
886{
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700887 int rc;
Joel Becker5e965812008-11-13 14:49:16 -0800888 struct ocfs2_extent_block *eb =
889 (struct ocfs2_extent_block *)bh->b_data;
890
Tao Maa09d09b2011-02-22 08:21:10 +0800891 trace_ocfs2_validate_extent_block((unsigned long long)bh->b_blocknr);
Joel Becker970e4932008-11-13 14:49:19 -0800892
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700893 BUG_ON(!buffer_uptodate(bh));
894
895 /*
896 * If the ecc fails, we return the error but otherwise
897 * leave the filesystem running. We know any error is
898 * local to this block.
899 */
900 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &eb->h_check);
Joel Becker13723d02008-10-17 19:25:01 -0700901 if (rc) {
902 mlog(ML_ERROR, "Checksum failed for extent block %llu\n",
903 (unsigned long long)bh->b_blocknr);
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700904 return rc;
Joel Becker13723d02008-10-17 19:25:01 -0700905 }
Joel Beckerd6b32bb2008-10-17 14:55:01 -0700906
907 /*
908 * Errors after here are fatal.
909 */
910
Joel Becker5e965812008-11-13 14:49:16 -0800911 if (!OCFS2_IS_VALID_EXTENT_BLOCK(eb)) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700912 rc = ocfs2_error(sb,
Joe Perches7ecef142015-09-04 15:44:51 -0700913 "Extent block #%llu has bad signature %.*s\n",
914 (unsigned long long)bh->b_blocknr, 7,
915 eb->h_signature);
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700916 goto bail;
Joel Becker5e965812008-11-13 14:49:16 -0800917 }
918
919 if (le64_to_cpu(eb->h_blkno) != bh->b_blocknr) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700920 rc = ocfs2_error(sb,
Joe Perches7ecef142015-09-04 15:44:51 -0700921 "Extent block #%llu has an invalid h_blkno of %llu\n",
922 (unsigned long long)bh->b_blocknr,
923 (unsigned long long)le64_to_cpu(eb->h_blkno));
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700924 goto bail;
Joel Becker5e965812008-11-13 14:49:16 -0800925 }
926
927 if (le32_to_cpu(eb->h_fs_generation) != OCFS2_SB(sb)->fs_generation) {
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700928 rc = ocfs2_error(sb,
Joe Perches7ecef142015-09-04 15:44:51 -0700929 "Extent block #%llu has an invalid h_fs_generation of #%u\n",
930 (unsigned long long)bh->b_blocknr,
931 le32_to_cpu(eb->h_fs_generation));
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700932 goto bail;
Joel Becker5e965812008-11-13 14:49:16 -0800933 }
Goldwyn Rodrigues17a5b9a2015-09-04 15:44:17 -0700934bail:
935 return rc;
Joel Becker5e965812008-11-13 14:49:16 -0800936}
937
Joel Becker3d03a302009-02-12 17:49:26 -0800938int ocfs2_read_extent_block(struct ocfs2_caching_info *ci, u64 eb_blkno,
Joel Becker5e965812008-11-13 14:49:16 -0800939 struct buffer_head **bh)
940{
941 int rc;
942 struct buffer_head *tmp = *bh;
943
Joel Becker3d03a302009-02-12 17:49:26 -0800944 rc = ocfs2_read_block(ci, eb_blkno, &tmp,
Joel Becker970e4932008-11-13 14:49:19 -0800945 ocfs2_validate_extent_block);
Joel Becker5e965812008-11-13 14:49:16 -0800946
947 /* If ocfs2_read_block() got us a new bh, pass it up. */
Joel Becker970e4932008-11-13 14:49:19 -0800948 if (!rc && !*bh)
Joel Becker5e965812008-11-13 14:49:16 -0800949 *bh = tmp;
950
Joel Becker5e965812008-11-13 14:49:16 -0800951 return rc;
952}
953
954
Mark Fashehccd979b2005-12-15 14:31:24 -0800955/*
956 * How many free extents have we got before we need more meta data?
957 */
Jun Piao964f14a2017-09-06 16:19:11 -0700958int ocfs2_num_free_extents(struct ocfs2_extent_tree *et)
Mark Fashehccd979b2005-12-15 14:31:24 -0800959{
960 int retval;
Tao Mae7d4cb62008-08-18 17:38:44 +0800961 struct ocfs2_extent_list *el = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -0800962 struct ocfs2_extent_block *eb;
963 struct buffer_head *eb_bh = NULL;
Tao Mae7d4cb62008-08-18 17:38:44 +0800964 u64 last_eb_blk = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -0800965
Joel Beckerf99b9b72008-08-20 19:36:33 -0700966 el = et->et_root_el;
967 last_eb_blk = ocfs2_et_get_last_eb_blk(et);
Mark Fashehccd979b2005-12-15 14:31:24 -0800968
Tao Mae7d4cb62008-08-18 17:38:44 +0800969 if (last_eb_blk) {
Joel Becker3d03a302009-02-12 17:49:26 -0800970 retval = ocfs2_read_extent_block(et->et_ci, last_eb_blk,
971 &eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800972 if (retval < 0) {
973 mlog_errno(retval);
974 goto bail;
975 }
976 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
977 el = &eb->h_list;
Tao Mae7d4cb62008-08-18 17:38:44 +0800978 }
Mark Fashehccd979b2005-12-15 14:31:24 -0800979
980 BUG_ON(el->l_tree_depth != 0);
981
982 retval = le16_to_cpu(el->l_count) - le16_to_cpu(el->l_next_free_rec);
983bail:
Mark Fasheha81cb882008-10-07 14:25:16 -0700984 brelse(eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -0800985
Tao Maa09d09b2011-02-22 08:21:10 +0800986 trace_ocfs2_num_free_extents(retval);
Mark Fashehccd979b2005-12-15 14:31:24 -0800987 return retval;
988}
989
990/* expects array to already be allocated
991 *
992 * sets h_signature, h_blkno, h_suballoc_bit, h_suballoc_slot, and
993 * l_count for you
994 */
Joel Becker42a5a7a2009-02-12 18:49:19 -0800995static int ocfs2_create_new_meta_bhs(handle_t *handle,
996 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -0800997 int wanted,
998 struct ocfs2_alloc_context *meta_ac,
999 struct buffer_head *bhs[])
1000{
1001 int count, status, i;
1002 u16 suballoc_bit_start;
1003 u32 num_got;
Joel Becker2b6cb572010-03-26 10:09:15 +08001004 u64 suballoc_loc, first_blkno;
Joel Becker42a5a7a2009-02-12 18:49:19 -08001005 struct ocfs2_super *osb =
1006 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001007 struct ocfs2_extent_block *eb;
1008
Mark Fashehccd979b2005-12-15 14:31:24 -08001009 count = 0;
1010 while (count < wanted) {
Joel Becker1ed9b772010-05-06 13:59:06 +08001011 status = ocfs2_claim_metadata(handle,
Mark Fashehccd979b2005-12-15 14:31:24 -08001012 meta_ac,
1013 wanted - count,
Joel Becker2b6cb572010-03-26 10:09:15 +08001014 &suballoc_loc,
Mark Fashehccd979b2005-12-15 14:31:24 -08001015 &suballoc_bit_start,
1016 &num_got,
1017 &first_blkno);
1018 if (status < 0) {
1019 mlog_errno(status);
1020 goto bail;
1021 }
1022
1023 for(i = count; i < (num_got + count); i++) {
1024 bhs[i] = sb_getblk(osb->sb, first_blkno);
1025 if (bhs[i] == NULL) {
Rui Xiang7391a292013-11-12 15:06:54 -08001026 status = -ENOMEM;
Mark Fashehccd979b2005-12-15 14:31:24 -08001027 mlog_errno(status);
1028 goto bail;
1029 }
Joel Becker42a5a7a2009-02-12 18:49:19 -08001030 ocfs2_set_new_buffer_uptodate(et->et_ci, bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001031
Joel Becker42a5a7a2009-02-12 18:49:19 -08001032 status = ocfs2_journal_access_eb(handle, et->et_ci,
1033 bhs[i],
Joel Becker13723d02008-10-17 19:25:01 -07001034 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001035 if (status < 0) {
1036 mlog_errno(status);
1037 goto bail;
1038 }
1039
1040 memset(bhs[i]->b_data, 0, osb->sb->s_blocksize);
1041 eb = (struct ocfs2_extent_block *) bhs[i]->b_data;
1042 /* Ok, setup the minimal stuff here. */
1043 strcpy(eb->h_signature, OCFS2_EXTENT_BLOCK_SIGNATURE);
1044 eb->h_blkno = cpu_to_le64(first_blkno);
1045 eb->h_fs_generation = cpu_to_le32(osb->fs_generation);
Tiger Yangb89c5422010-01-25 14:11:06 +08001046 eb->h_suballoc_slot =
1047 cpu_to_le16(meta_ac->ac_alloc_slot);
Joel Becker2b6cb572010-03-26 10:09:15 +08001048 eb->h_suballoc_loc = cpu_to_le64(suballoc_loc);
Mark Fashehccd979b2005-12-15 14:31:24 -08001049 eb->h_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1050 eb->h_list.l_count =
1051 cpu_to_le16(ocfs2_extent_recs_per_eb(osb->sb));
1052
1053 suballoc_bit_start++;
1054 first_blkno++;
1055
1056 /* We'll also be dirtied by the caller, so
1057 * this isn't absolutely necessary. */
Joel Beckerec20cec2010-03-19 14:13:52 -07001058 ocfs2_journal_dirty(handle, bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001059 }
1060
1061 count += num_got;
1062 }
1063
1064 status = 0;
1065bail:
1066 if (status < 0) {
1067 for(i = 0; i < wanted; i++) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001068 brelse(bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001069 bhs[i] = NULL;
1070 }
Tao Mac1e8d352011-03-07 16:43:21 +08001071 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08001072 }
Mark Fashehccd979b2005-12-15 14:31:24 -08001073 return status;
1074}
1075
1076/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001077 * Helper function for ocfs2_add_branch() and ocfs2_shift_tree_depth().
1078 *
1079 * Returns the sum of the rightmost extent rec logical offset and
1080 * cluster count.
1081 *
1082 * ocfs2_add_branch() uses this to determine what logical cluster
1083 * value should be populated into the leftmost new branch records.
1084 *
1085 * ocfs2_shift_tree_depth() uses this to determine the # clusters
1086 * value for the new topmost tree record.
1087 */
1088static inline u32 ocfs2_sum_rightmost_rec(struct ocfs2_extent_list *el)
1089{
1090 int i;
1091
1092 i = le16_to_cpu(el->l_next_free_rec) - 1;
1093
1094 return le32_to_cpu(el->l_recs[i].e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001095 ocfs2_rec_clusters(el, &el->l_recs[i]);
Mark Fashehdcd05382007-01-16 11:32:23 -08001096}
1097
1098/*
Tao Ma6b791bc2009-06-12 14:18:36 +08001099 * Change range of the branches in the right most path according to the leaf
1100 * extent block's rightmost record.
1101 */
1102static int ocfs2_adjust_rightmost_branch(handle_t *handle,
Tao Ma6b791bc2009-06-12 14:18:36 +08001103 struct ocfs2_extent_tree *et)
1104{
1105 int status;
1106 struct ocfs2_path *path = NULL;
1107 struct ocfs2_extent_list *el;
1108 struct ocfs2_extent_rec *rec;
1109
1110 path = ocfs2_new_path_from_et(et);
1111 if (!path) {
1112 status = -ENOMEM;
1113 return status;
1114 }
1115
Joel Beckerfacdb772009-02-12 18:08:48 -08001116 status = ocfs2_find_path(et->et_ci, path, UINT_MAX);
Tao Ma6b791bc2009-06-12 14:18:36 +08001117 if (status < 0) {
1118 mlog_errno(status);
1119 goto out;
1120 }
1121
Tao Mac901fb02010-04-26 14:34:57 +08001122 status = ocfs2_extend_trans(handle, path_num_items(path));
Tao Ma6b791bc2009-06-12 14:18:36 +08001123 if (status < 0) {
1124 mlog_errno(status);
1125 goto out;
1126 }
1127
Joel Beckerd401dc12009-02-13 02:24:10 -08001128 status = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma6b791bc2009-06-12 14:18:36 +08001129 if (status < 0) {
1130 mlog_errno(status);
1131 goto out;
1132 }
1133
1134 el = path_leaf_el(path);
Al Viro3a251f02012-04-13 12:22:00 -04001135 rec = &el->l_recs[le16_to_cpu(el->l_next_free_rec) - 1];
Tao Ma6b791bc2009-06-12 14:18:36 +08001136
Joel Beckerd401dc12009-02-13 02:24:10 -08001137 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Tao Ma6b791bc2009-06-12 14:18:36 +08001138
1139out:
1140 ocfs2_free_path(path);
1141 return status;
1142}
1143
1144/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001145 * Add an entire tree branch to our inode. eb_bh is the extent block
Joel Beckerd401dc12009-02-13 02:24:10 -08001146 * to start at, if we don't want to start the branch at the root
Mark Fashehccd979b2005-12-15 14:31:24 -08001147 * structure.
1148 *
1149 * last_eb_bh is required as we have to update it's next_leaf pointer
1150 * for the new last extent block.
1151 *
1152 * the new branch will be 'empty' in the sense that every block will
Mark Fashehe48edee2007-03-07 16:46:57 -08001153 * contain a single record with cluster count == 0.
Mark Fashehccd979b2005-12-15 14:31:24 -08001154 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001155static int ocfs2_add_branch(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001156 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001157 struct buffer_head *eb_bh,
Mark Fasheh328d5752007-06-18 10:48:04 -07001158 struct buffer_head **last_eb_bh,
Mark Fashehccd979b2005-12-15 14:31:24 -08001159 struct ocfs2_alloc_context *meta_ac)
1160{
1161 int status, new_blocks, i;
1162 u64 next_blkno, new_last_eb_blk;
1163 struct buffer_head *bh;
1164 struct buffer_head **new_eb_bhs = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001165 struct ocfs2_extent_block *eb;
1166 struct ocfs2_extent_list *eb_el;
1167 struct ocfs2_extent_list *el;
Tao Ma6b791bc2009-06-12 14:18:36 +08001168 u32 new_cpos, root_end;
Mark Fashehccd979b2005-12-15 14:31:24 -08001169
Mark Fasheh328d5752007-06-18 10:48:04 -07001170 BUG_ON(!last_eb_bh || !*last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001171
Mark Fashehccd979b2005-12-15 14:31:24 -08001172 if (eb_bh) {
1173 eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1174 el = &eb->h_list;
1175 } else
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001176 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001177
1178 /* we never add a branch to a leaf. */
1179 BUG_ON(!el->l_tree_depth);
1180
1181 new_blocks = le16_to_cpu(el->l_tree_depth);
1182
Tao Ma6b791bc2009-06-12 14:18:36 +08001183 eb = (struct ocfs2_extent_block *)(*last_eb_bh)->b_data;
1184 new_cpos = ocfs2_sum_rightmost_rec(&eb->h_list);
1185 root_end = ocfs2_sum_rightmost_rec(et->et_root_el);
1186
1187 /*
1188 * If there is a gap before the root end and the real end
1189 * of the righmost leaf block, we need to remove the gap
1190 * between new_cpos and root_end first so that the tree
1191 * is consistent after we add a new branch(it will start
1192 * from new_cpos).
1193 */
1194 if (root_end > new_cpos) {
Tao Maa09d09b2011-02-22 08:21:10 +08001195 trace_ocfs2_adjust_rightmost_branch(
1196 (unsigned long long)
1197 ocfs2_metadata_cache_owner(et->et_ci),
1198 root_end, new_cpos);
1199
Joel Beckerd401dc12009-02-13 02:24:10 -08001200 status = ocfs2_adjust_rightmost_branch(handle, et);
Tao Ma6b791bc2009-06-12 14:18:36 +08001201 if (status) {
1202 mlog_errno(status);
1203 goto bail;
1204 }
1205 }
1206
Mark Fashehccd979b2005-12-15 14:31:24 -08001207 /* allocate the number of new eb blocks we need */
1208 new_eb_bhs = kcalloc(new_blocks, sizeof(struct buffer_head *),
1209 GFP_KERNEL);
1210 if (!new_eb_bhs) {
1211 status = -ENOMEM;
1212 mlog_errno(status);
1213 goto bail;
1214 }
1215
Joel Becker42a5a7a2009-02-12 18:49:19 -08001216 status = ocfs2_create_new_meta_bhs(handle, et, new_blocks,
Mark Fashehccd979b2005-12-15 14:31:24 -08001217 meta_ac, new_eb_bhs);
1218 if (status < 0) {
1219 mlog_errno(status);
1220 goto bail;
1221 }
1222
1223 /* Note: new_eb_bhs[new_blocks - 1] is the guy which will be
1224 * linked with the rest of the tree.
1225 * conversly, new_eb_bhs[0] is the new bottommost leaf.
1226 *
1227 * when we leave the loop, new_last_eb_blk will point to the
1228 * newest leaf, and next_blkno will point to the topmost extent
1229 * block. */
1230 next_blkno = new_last_eb_blk = 0;
1231 for(i = 0; i < new_blocks; i++) {
1232 bh = new_eb_bhs[i];
1233 eb = (struct ocfs2_extent_block *) bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001234 /* ocfs2_create_new_meta_bhs() should create it right! */
1235 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001236 eb_el = &eb->h_list;
1237
Joel Beckerd401dc12009-02-13 02:24:10 -08001238 status = ocfs2_journal_access_eb(handle, et->et_ci, bh,
Joel Becker13723d02008-10-17 19:25:01 -07001239 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001240 if (status < 0) {
1241 mlog_errno(status);
1242 goto bail;
1243 }
1244
1245 eb->h_next_leaf_blk = 0;
1246 eb_el->l_tree_depth = cpu_to_le16(i);
1247 eb_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehdcd05382007-01-16 11:32:23 -08001248 /*
1249 * This actually counts as an empty extent as
1250 * c_clusters == 0
1251 */
1252 eb_el->l_recs[0].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehccd979b2005-12-15 14:31:24 -08001253 eb_el->l_recs[0].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehe48edee2007-03-07 16:46:57 -08001254 /*
1255 * eb_el isn't always an interior node, but even leaf
1256 * nodes want a zero'd flags and reserved field so
1257 * this gets the whole 32 bits regardless of use.
1258 */
1259 eb_el->l_recs[0].e_int_clusters = cpu_to_le32(0);
Mark Fashehccd979b2005-12-15 14:31:24 -08001260 if (!eb_el->l_tree_depth)
1261 new_last_eb_blk = le64_to_cpu(eb->h_blkno);
1262
Joel Beckerec20cec2010-03-19 14:13:52 -07001263 ocfs2_journal_dirty(handle, bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001264 next_blkno = le64_to_cpu(eb->h_blkno);
1265 }
1266
1267 /* This is a bit hairy. We want to update up to three blocks
1268 * here without leaving any of them in an inconsistent state
1269 * in case of error. We don't have to worry about
1270 * journal_dirty erroring as it won't unless we've aborted the
1271 * handle (in which case we would never be here) so reserving
1272 * the write with journal_access is all we need to do. */
Joel Beckerd401dc12009-02-13 02:24:10 -08001273 status = ocfs2_journal_access_eb(handle, et->et_ci, *last_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001274 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001275 if (status < 0) {
1276 mlog_errno(status);
1277 goto bail;
1278 }
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001279 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001280 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001281 if (status < 0) {
1282 mlog_errno(status);
1283 goto bail;
1284 }
1285 if (eb_bh) {
Joel Beckerd401dc12009-02-13 02:24:10 -08001286 status = ocfs2_journal_access_eb(handle, et->et_ci, eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001287 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001288 if (status < 0) {
1289 mlog_errno(status);
1290 goto bail;
1291 }
1292 }
1293
1294 /* Link the new branch into the rest of the tree (el will
Tao Mae7d4cb62008-08-18 17:38:44 +08001295 * either be on the root_bh, or the extent block passed in. */
Mark Fashehccd979b2005-12-15 14:31:24 -08001296 i = le16_to_cpu(el->l_next_free_rec);
1297 el->l_recs[i].e_blkno = cpu_to_le64(next_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08001298 el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001299 el->l_recs[i].e_int_clusters = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08001300 le16_add_cpu(&el->l_next_free_rec, 1);
1301
1302 /* fe needs a new last extent block pointer, as does the
1303 * next_leaf on the previously last-extent-block. */
Joel Becker35dc0aa2008-08-20 16:25:06 -07001304 ocfs2_et_set_last_eb_blk(et, new_last_eb_blk);
Mark Fashehccd979b2005-12-15 14:31:24 -08001305
Mark Fasheh328d5752007-06-18 10:48:04 -07001306 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001307 eb->h_next_leaf_blk = cpu_to_le64(new_last_eb_blk);
1308
Joel Beckerec20cec2010-03-19 14:13:52 -07001309 ocfs2_journal_dirty(handle, *last_eb_bh);
1310 ocfs2_journal_dirty(handle, et->et_root_bh);
1311 if (eb_bh)
1312 ocfs2_journal_dirty(handle, eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001313
Mark Fasheh328d5752007-06-18 10:48:04 -07001314 /*
1315 * Some callers want to track the rightmost leaf so pass it
1316 * back here.
1317 */
1318 brelse(*last_eb_bh);
1319 get_bh(new_eb_bhs[0]);
1320 *last_eb_bh = new_eb_bhs[0];
1321
Mark Fashehccd979b2005-12-15 14:31:24 -08001322 status = 0;
1323bail:
1324 if (new_eb_bhs) {
1325 for (i = 0; i < new_blocks; i++)
Mark Fasheha81cb882008-10-07 14:25:16 -07001326 brelse(new_eb_bhs[i]);
Mark Fashehccd979b2005-12-15 14:31:24 -08001327 kfree(new_eb_bhs);
1328 }
1329
Mark Fashehccd979b2005-12-15 14:31:24 -08001330 return status;
1331}
1332
1333/*
1334 * adds another level to the allocation tree.
1335 * returns back the new extent block so you can add a branch to it
1336 * after this call.
1337 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001338static int ocfs2_shift_tree_depth(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08001339 struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001340 struct ocfs2_alloc_context *meta_ac,
1341 struct buffer_head **ret_new_eb_bh)
1342{
1343 int status, i;
Mark Fashehdcd05382007-01-16 11:32:23 -08001344 u32 new_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08001345 struct buffer_head *new_eb_bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001346 struct ocfs2_extent_block *eb;
Tao Mae7d4cb62008-08-18 17:38:44 +08001347 struct ocfs2_extent_list *root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001348 struct ocfs2_extent_list *eb_el;
1349
Joel Becker42a5a7a2009-02-12 18:49:19 -08001350 status = ocfs2_create_new_meta_bhs(handle, et, 1, meta_ac,
Mark Fashehccd979b2005-12-15 14:31:24 -08001351 &new_eb_bh);
1352 if (status < 0) {
1353 mlog_errno(status);
1354 goto bail;
1355 }
1356
1357 eb = (struct ocfs2_extent_block *) new_eb_bh->b_data;
Joel Becker5e965812008-11-13 14:49:16 -08001358 /* ocfs2_create_new_meta_bhs() should create it right! */
1359 BUG_ON(!OCFS2_IS_VALID_EXTENT_BLOCK(eb));
Mark Fashehccd979b2005-12-15 14:31:24 -08001360
1361 eb_el = &eb->h_list;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001362 root_el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001363
Joel Beckerd401dc12009-02-13 02:24:10 -08001364 status = ocfs2_journal_access_eb(handle, et->et_ci, new_eb_bh,
Joel Becker13723d02008-10-17 19:25:01 -07001365 OCFS2_JOURNAL_ACCESS_CREATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001366 if (status < 0) {
1367 mlog_errno(status);
1368 goto bail;
1369 }
1370
Tao Mae7d4cb62008-08-18 17:38:44 +08001371 /* copy the root extent list data into the new extent block */
1372 eb_el->l_tree_depth = root_el->l_tree_depth;
1373 eb_el->l_next_free_rec = root_el->l_next_free_rec;
1374 for (i = 0; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1375 eb_el->l_recs[i] = root_el->l_recs[i];
Mark Fashehccd979b2005-12-15 14:31:24 -08001376
Joel Beckerec20cec2010-03-19 14:13:52 -07001377 ocfs2_journal_dirty(handle, new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001378
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08001379 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07001380 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08001381 if (status < 0) {
1382 mlog_errno(status);
1383 goto bail;
1384 }
1385
Mark Fashehdcd05382007-01-16 11:32:23 -08001386 new_clusters = ocfs2_sum_rightmost_rec(eb_el);
1387
Tao Mae7d4cb62008-08-18 17:38:44 +08001388 /* update root_bh now */
1389 le16_add_cpu(&root_el->l_tree_depth, 1);
1390 root_el->l_recs[0].e_cpos = 0;
1391 root_el->l_recs[0].e_blkno = eb->h_blkno;
1392 root_el->l_recs[0].e_int_clusters = cpu_to_le32(new_clusters);
1393 for (i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
1394 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
1395 root_el->l_next_free_rec = cpu_to_le16(1);
Mark Fashehccd979b2005-12-15 14:31:24 -08001396
1397 /* If this is our 1st tree depth shift, then last_eb_blk
1398 * becomes the allocated extent block */
Tao Mae7d4cb62008-08-18 17:38:44 +08001399 if (root_el->l_tree_depth == cpu_to_le16(1))
Joel Becker35dc0aa2008-08-20 16:25:06 -07001400 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fashehccd979b2005-12-15 14:31:24 -08001401
Joel Beckerec20cec2010-03-19 14:13:52 -07001402 ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001403
1404 *ret_new_eb_bh = new_eb_bh;
1405 new_eb_bh = NULL;
1406 status = 0;
1407bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001408 brelse(new_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001409
Mark Fashehccd979b2005-12-15 14:31:24 -08001410 return status;
1411}
1412
1413/*
Mark Fashehccd979b2005-12-15 14:31:24 -08001414 * Should only be called when there is no space left in any of the
1415 * leaf nodes. What we want to do is find the lowest tree depth
1416 * non-leaf extent block with room for new records. There are three
1417 * valid results of this search:
1418 *
1419 * 1) a lowest extent block is found, then we pass it back in
1420 * *lowest_eb_bh and return '0'
1421 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001422 * 2) the search fails to find anything, but the root_el has room. We
Mark Fashehccd979b2005-12-15 14:31:24 -08001423 * pass NULL back in *lowest_eb_bh, but still return '0'
1424 *
Tao Mae7d4cb62008-08-18 17:38:44 +08001425 * 3) the search fails to find anything AND the root_el is full, in
Mark Fashehccd979b2005-12-15 14:31:24 -08001426 * which case we return > 0
1427 *
1428 * return status < 0 indicates an error.
1429 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001430static int ocfs2_find_branch_target(struct ocfs2_extent_tree *et,
Mark Fashehccd979b2005-12-15 14:31:24 -08001431 struct buffer_head **target_bh)
1432{
1433 int status = 0, i;
1434 u64 blkno;
Mark Fashehccd979b2005-12-15 14:31:24 -08001435 struct ocfs2_extent_block *eb;
1436 struct ocfs2_extent_list *el;
1437 struct buffer_head *bh = NULL;
1438 struct buffer_head *lowest_bh = NULL;
1439
Mark Fashehccd979b2005-12-15 14:31:24 -08001440 *target_bh = NULL;
1441
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001442 el = et->et_root_el;
Mark Fashehccd979b2005-12-15 14:31:24 -08001443
1444 while(le16_to_cpu(el->l_tree_depth) > 1) {
1445 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08001446 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Joe Perches7ecef142015-09-04 15:44:51 -07001447 "Owner %llu has empty extent list (next_free_rec == 0)\n",
Joel Becker3d03a302009-02-12 17:49:26 -08001448 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fashehccd979b2005-12-15 14:31:24 -08001449 status = -EIO;
1450 goto bail;
1451 }
1452 i = le16_to_cpu(el->l_next_free_rec) - 1;
1453 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1454 if (!blkno) {
Joel Becker3d03a302009-02-12 17:49:26 -08001455 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Joe Perches7ecef142015-09-04 15:44:51 -07001456 "Owner %llu has extent list where extent # %d has no physical block start\n",
Joel Becker3d03a302009-02-12 17:49:26 -08001457 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci), i);
Mark Fashehccd979b2005-12-15 14:31:24 -08001458 status = -EIO;
1459 goto bail;
1460 }
1461
Mark Fasheha81cb882008-10-07 14:25:16 -07001462 brelse(bh);
1463 bh = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08001464
Joel Becker3d03a302009-02-12 17:49:26 -08001465 status = ocfs2_read_extent_block(et->et_ci, blkno, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001466 if (status < 0) {
1467 mlog_errno(status);
1468 goto bail;
1469 }
1470
1471 eb = (struct ocfs2_extent_block *) bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08001472 el = &eb->h_list;
1473
1474 if (le16_to_cpu(el->l_next_free_rec) <
1475 le16_to_cpu(el->l_count)) {
Mark Fasheha81cb882008-10-07 14:25:16 -07001476 brelse(lowest_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001477 lowest_bh = bh;
1478 get_bh(lowest_bh);
1479 }
1480 }
1481
1482 /* If we didn't find one and the fe doesn't have any room,
1483 * then return '1' */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001484 el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001485 if (!lowest_bh && (el->l_next_free_rec == el->l_count))
Mark Fashehccd979b2005-12-15 14:31:24 -08001486 status = 1;
1487
1488 *target_bh = lowest_bh;
1489bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07001490 brelse(bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08001491
Mark Fashehccd979b2005-12-15 14:31:24 -08001492 return status;
1493}
1494
Mark Fashehe48edee2007-03-07 16:46:57 -08001495/*
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001496 * Grow a b-tree so that it has more records.
1497 *
1498 * We might shift the tree depth in which case existing paths should
1499 * be considered invalid.
1500 *
1501 * Tree depth after the grow is returned via *final_depth.
Mark Fasheh328d5752007-06-18 10:48:04 -07001502 *
1503 * *last_eb_bh will be updated by ocfs2_add_branch().
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001504 */
Joel Beckerd401dc12009-02-13 02:24:10 -08001505static int ocfs2_grow_tree(handle_t *handle, struct ocfs2_extent_tree *et,
1506 int *final_depth, struct buffer_head **last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001507 struct ocfs2_alloc_context *meta_ac)
1508{
1509 int ret, shift;
Joel Beckerce1d9ea2008-08-20 16:30:07 -07001510 struct ocfs2_extent_list *el = et->et_root_el;
Tao Mae7d4cb62008-08-18 17:38:44 +08001511 int depth = le16_to_cpu(el->l_tree_depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001512 struct buffer_head *bh = NULL;
1513
1514 BUG_ON(meta_ac == NULL);
1515
Joel Beckerd401dc12009-02-13 02:24:10 -08001516 shift = ocfs2_find_branch_target(et, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001517 if (shift < 0) {
1518 ret = shift;
1519 mlog_errno(ret);
1520 goto out;
1521 }
1522
1523 /* We traveled all the way to the bottom of the allocation tree
1524 * and didn't find room for any more extents - we need to add
1525 * another tree level */
1526 if (shift) {
1527 BUG_ON(bh);
Tao Maa09d09b2011-02-22 08:21:10 +08001528 trace_ocfs2_grow_tree(
1529 (unsigned long long)
1530 ocfs2_metadata_cache_owner(et->et_ci),
1531 depth);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001532
1533 /* ocfs2_shift_tree_depth will return us a buffer with
1534 * the new extent block (so we can pass that to
1535 * ocfs2_add_branch). */
Joel Beckerd401dc12009-02-13 02:24:10 -08001536 ret = ocfs2_shift_tree_depth(handle, et, meta_ac, &bh);
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001537 if (ret < 0) {
1538 mlog_errno(ret);
1539 goto out;
1540 }
1541 depth++;
Mark Fasheh328d5752007-06-18 10:48:04 -07001542 if (depth == 1) {
1543 /*
1544 * Special case: we have room now if we shifted from
1545 * tree_depth 0, so no more work needs to be done.
1546 *
1547 * We won't be calling add_branch, so pass
1548 * back *last_eb_bh as the new leaf. At depth
1549 * zero, it should always be null so there's
1550 * no reason to brelse.
1551 */
1552 BUG_ON(*last_eb_bh);
1553 get_bh(bh);
1554 *last_eb_bh = bh;
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001555 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07001556 }
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001557 }
1558
1559 /* call ocfs2_add_branch to add the final part of the tree with
1560 * the new data. */
Joel Beckerd401dc12009-02-13 02:24:10 -08001561 ret = ocfs2_add_branch(handle, et, bh, last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07001562 meta_ac);
1563 if (ret < 0) {
1564 mlog_errno(ret);
1565 goto out;
1566 }
1567
1568out:
1569 if (final_depth)
1570 *final_depth = depth;
1571 brelse(bh);
1572 return ret;
1573}
1574
1575/*
Mark Fashehdcd05382007-01-16 11:32:23 -08001576 * This function will discard the rightmost extent record.
1577 */
1578static void ocfs2_shift_records_right(struct ocfs2_extent_list *el)
1579{
1580 int next_free = le16_to_cpu(el->l_next_free_rec);
1581 int count = le16_to_cpu(el->l_count);
1582 unsigned int num_bytes;
1583
1584 BUG_ON(!next_free);
1585 /* This will cause us to go off the end of our extent list. */
1586 BUG_ON(next_free >= count);
1587
1588 num_bytes = sizeof(struct ocfs2_extent_rec) * next_free;
1589
1590 memmove(&el->l_recs[1], &el->l_recs[0], num_bytes);
1591}
1592
1593static void ocfs2_rotate_leaf(struct ocfs2_extent_list *el,
1594 struct ocfs2_extent_rec *insert_rec)
1595{
1596 int i, insert_index, next_free, has_empty, num_bytes;
1597 u32 insert_cpos = le32_to_cpu(insert_rec->e_cpos);
1598 struct ocfs2_extent_rec *rec;
1599
1600 next_free = le16_to_cpu(el->l_next_free_rec);
1601 has_empty = ocfs2_is_empty_extent(&el->l_recs[0]);
1602
1603 BUG_ON(!next_free);
1604
1605 /* The tree code before us didn't allow enough room in the leaf. */
Julia Lawallb1f35502008-03-04 15:21:05 -08001606 BUG_ON(el->l_next_free_rec == el->l_count && !has_empty);
Mark Fashehdcd05382007-01-16 11:32:23 -08001607
1608 /*
1609 * The easiest way to approach this is to just remove the
1610 * empty extent and temporarily decrement next_free.
1611 */
1612 if (has_empty) {
1613 /*
1614 * If next_free was 1 (only an empty extent), this
1615 * loop won't execute, which is fine. We still want
1616 * the decrement above to happen.
1617 */
1618 for(i = 0; i < (next_free - 1); i++)
1619 el->l_recs[i] = el->l_recs[i+1];
1620
1621 next_free--;
1622 }
1623
1624 /*
1625 * Figure out what the new record index should be.
1626 */
1627 for(i = 0; i < next_free; i++) {
1628 rec = &el->l_recs[i];
1629
1630 if (insert_cpos < le32_to_cpu(rec->e_cpos))
1631 break;
1632 }
1633 insert_index = i;
1634
Tao Maa09d09b2011-02-22 08:21:10 +08001635 trace_ocfs2_rotate_leaf(insert_cpos, insert_index,
1636 has_empty, next_free,
1637 le16_to_cpu(el->l_count));
Mark Fashehdcd05382007-01-16 11:32:23 -08001638
1639 BUG_ON(insert_index < 0);
1640 BUG_ON(insert_index >= le16_to_cpu(el->l_count));
1641 BUG_ON(insert_index > next_free);
1642
1643 /*
1644 * No need to memmove if we're just adding to the tail.
1645 */
1646 if (insert_index != next_free) {
1647 BUG_ON(next_free >= le16_to_cpu(el->l_count));
1648
1649 num_bytes = next_free - insert_index;
1650 num_bytes *= sizeof(struct ocfs2_extent_rec);
1651 memmove(&el->l_recs[insert_index + 1],
1652 &el->l_recs[insert_index],
1653 num_bytes);
1654 }
1655
1656 /*
1657 * Either we had an empty extent, and need to re-increment or
1658 * there was no empty extent on a non full rightmost leaf node,
1659 * in which case we still need to increment.
1660 */
1661 next_free++;
1662 el->l_next_free_rec = cpu_to_le16(next_free);
1663 /*
1664 * Make sure none of the math above just messed up our tree.
1665 */
1666 BUG_ON(le16_to_cpu(el->l_next_free_rec) > le16_to_cpu(el->l_count));
1667
1668 el->l_recs[insert_index] = *insert_rec;
1669
1670}
1671
Mark Fasheh328d5752007-06-18 10:48:04 -07001672static void ocfs2_remove_empty_extent(struct ocfs2_extent_list *el)
1673{
1674 int size, num_recs = le16_to_cpu(el->l_next_free_rec);
1675
1676 BUG_ON(num_recs == 0);
1677
1678 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
1679 num_recs--;
1680 size = num_recs * sizeof(struct ocfs2_extent_rec);
1681 memmove(&el->l_recs[0], &el->l_recs[1], size);
1682 memset(&el->l_recs[num_recs], 0,
1683 sizeof(struct ocfs2_extent_rec));
1684 el->l_next_free_rec = cpu_to_le16(num_recs);
1685 }
1686}
1687
Mark Fashehdcd05382007-01-16 11:32:23 -08001688/*
1689 * Create an empty extent record .
1690 *
1691 * l_next_free_rec may be updated.
1692 *
1693 * If an empty extent already exists do nothing.
1694 */
1695static void ocfs2_create_empty_extent(struct ocfs2_extent_list *el)
1696{
1697 int next_free = le16_to_cpu(el->l_next_free_rec);
1698
Mark Fashehe48edee2007-03-07 16:46:57 -08001699 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
1700
Mark Fashehdcd05382007-01-16 11:32:23 -08001701 if (next_free == 0)
1702 goto set_and_inc;
1703
1704 if (ocfs2_is_empty_extent(&el->l_recs[0]))
1705 return;
1706
1707 mlog_bug_on_msg(el->l_count == el->l_next_free_rec,
1708 "Asked to create an empty extent in a full list:\n"
1709 "count = %u, tree depth = %u",
1710 le16_to_cpu(el->l_count),
1711 le16_to_cpu(el->l_tree_depth));
1712
1713 ocfs2_shift_records_right(el);
1714
1715set_and_inc:
1716 le16_add_cpu(&el->l_next_free_rec, 1);
1717 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
1718}
1719
1720/*
1721 * For a rotation which involves two leaf nodes, the "root node" is
1722 * the lowest level tree node which contains a path to both leafs. This
1723 * resulting set of information can be used to form a complete "subtree"
1724 *
1725 * This function is passed two full paths from the dinode down to a
1726 * pair of adjacent leaves. It's task is to figure out which path
1727 * index contains the subtree root - this can be the root index itself
1728 * in a worst-case rotation.
1729 *
1730 * The array index of the subtree root is passed back.
1731 */
Tao Ma38a04e42009-11-30 14:32:19 +08001732int ocfs2_find_subtree_root(struct ocfs2_extent_tree *et,
1733 struct ocfs2_path *left,
1734 struct ocfs2_path *right)
Mark Fashehdcd05382007-01-16 11:32:23 -08001735{
1736 int i = 0;
1737
1738 /*
1739 * Check that the caller passed in two paths from the same tree.
1740 */
1741 BUG_ON(path_root_bh(left) != path_root_bh(right));
1742
1743 do {
1744 i++;
1745
1746 /*
1747 * The caller didn't pass two adjacent paths.
1748 */
1749 mlog_bug_on_msg(i > left->p_tree_depth,
Joel Becker7dc02802009-02-12 19:20:13 -08001750 "Owner %llu, left depth %u, right depth %u\n"
Mark Fashehdcd05382007-01-16 11:32:23 -08001751 "left leaf blk %llu, right leaf blk %llu\n",
Joel Becker7dc02802009-02-12 19:20:13 -08001752 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
1753 left->p_tree_depth, right->p_tree_depth,
Mark Fashehdcd05382007-01-16 11:32:23 -08001754 (unsigned long long)path_leaf_bh(left)->b_blocknr,
1755 (unsigned long long)path_leaf_bh(right)->b_blocknr);
1756 } while (left->p_node[i].bh->b_blocknr ==
1757 right->p_node[i].bh->b_blocknr);
1758
1759 return i - 1;
1760}
1761
1762typedef void (path_insert_t)(void *, struct buffer_head *);
1763
1764/*
1765 * Traverse a btree path in search of cpos, starting at root_el.
1766 *
1767 * This code can be called with a cpos larger than the tree, in which
1768 * case it will return the rightmost path.
1769 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001770static int __ocfs2_find_path(struct ocfs2_caching_info *ci,
Mark Fashehdcd05382007-01-16 11:32:23 -08001771 struct ocfs2_extent_list *root_el, u32 cpos,
1772 path_insert_t *func, void *data)
1773{
1774 int i, ret = 0;
1775 u32 range;
1776 u64 blkno;
1777 struct buffer_head *bh = NULL;
1778 struct ocfs2_extent_block *eb;
1779 struct ocfs2_extent_list *el;
1780 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08001781
1782 el = root_el;
1783 while (el->l_tree_depth) {
1784 if (le16_to_cpu(el->l_next_free_rec) == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001785 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
Joe Perches7ecef142015-09-04 15:44:51 -07001786 "Owner %llu has empty extent list at depth %u\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001787 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001788 le16_to_cpu(el->l_tree_depth));
1789 ret = -EROFS;
1790 goto out;
1791
1792 }
1793
1794 for(i = 0; i < le16_to_cpu(el->l_next_free_rec) - 1; i++) {
1795 rec = &el->l_recs[i];
1796
1797 /*
1798 * In the case that cpos is off the allocation
1799 * tree, this should just wind up returning the
1800 * rightmost record.
1801 */
1802 range = le32_to_cpu(rec->e_cpos) +
Mark Fashehe48edee2007-03-07 16:46:57 -08001803 ocfs2_rec_clusters(el, rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08001804 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
1805 break;
1806 }
1807
1808 blkno = le64_to_cpu(el->l_recs[i].e_blkno);
1809 if (blkno == 0) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001810 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
Joe Perches7ecef142015-09-04 15:44:51 -07001811 "Owner %llu has bad blkno in extent list at depth %u (index %d)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001812 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001813 le16_to_cpu(el->l_tree_depth), i);
1814 ret = -EROFS;
1815 goto out;
1816 }
1817
1818 brelse(bh);
1819 bh = NULL;
Joel Beckerfacdb772009-02-12 18:08:48 -08001820 ret = ocfs2_read_extent_block(ci, blkno, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001821 if (ret) {
1822 mlog_errno(ret);
1823 goto out;
1824 }
1825
1826 eb = (struct ocfs2_extent_block *) bh->b_data;
1827 el = &eb->h_list;
Mark Fashehdcd05382007-01-16 11:32:23 -08001828
1829 if (le16_to_cpu(el->l_next_free_rec) >
1830 le16_to_cpu(el->l_count)) {
Joel Beckerfacdb772009-02-12 18:08:48 -08001831 ocfs2_error(ocfs2_metadata_cache_get_super(ci),
Joe Perches7ecef142015-09-04 15:44:51 -07001832 "Owner %llu has bad count in extent list at block %llu (next free=%u, count=%u)\n",
Joel Beckerfacdb772009-02-12 18:08:48 -08001833 (unsigned long long)ocfs2_metadata_cache_owner(ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08001834 (unsigned long long)bh->b_blocknr,
1835 le16_to_cpu(el->l_next_free_rec),
1836 le16_to_cpu(el->l_count));
1837 ret = -EROFS;
1838 goto out;
1839 }
1840
1841 if (func)
1842 func(data, bh);
1843 }
1844
1845out:
1846 /*
1847 * Catch any trailing bh that the loop didn't handle.
1848 */
1849 brelse(bh);
1850
1851 return ret;
1852}
1853
1854/*
1855 * Given an initialized path (that is, it has a valid root extent
1856 * list), this function will traverse the btree in search of the path
1857 * which would contain cpos.
1858 *
1859 * The path traveled is recorded in the path structure.
1860 *
1861 * Note that this will not do any comparisons on leaf node extent
1862 * records, so it will work fine in the case that we just added a tree
1863 * branch.
1864 */
1865struct find_path_data {
1866 int index;
1867 struct ocfs2_path *path;
1868};
1869static void find_path_ins(void *data, struct buffer_head *bh)
1870{
1871 struct find_path_data *fp = data;
1872
1873 get_bh(bh);
1874 ocfs2_path_insert_eb(fp->path, fp->index, bh);
1875 fp->index++;
1876}
Tao Mae2e9f602009-08-18 11:22:34 +08001877int ocfs2_find_path(struct ocfs2_caching_info *ci,
1878 struct ocfs2_path *path, u32 cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08001879{
1880 struct find_path_data data;
1881
1882 data.index = 1;
1883 data.path = path;
Joel Beckerfacdb772009-02-12 18:08:48 -08001884 return __ocfs2_find_path(ci, path_root_el(path), cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08001885 find_path_ins, &data);
1886}
1887
1888static void find_leaf_ins(void *data, struct buffer_head *bh)
1889{
1890 struct ocfs2_extent_block *eb =(struct ocfs2_extent_block *)bh->b_data;
1891 struct ocfs2_extent_list *el = &eb->h_list;
1892 struct buffer_head **ret = data;
1893
1894 /* We want to retain only the leaf block. */
1895 if (le16_to_cpu(el->l_tree_depth) == 0) {
1896 get_bh(bh);
1897 *ret = bh;
1898 }
1899}
1900/*
1901 * Find the leaf block in the tree which would contain cpos. No
1902 * checking of the actual leaf is done.
1903 *
1904 * Some paths want to call this instead of allocating a path structure
1905 * and calling ocfs2_find_path().
1906 *
1907 * This function doesn't handle non btree extent lists.
1908 */
Joel Beckerfacdb772009-02-12 18:08:48 -08001909int ocfs2_find_leaf(struct ocfs2_caching_info *ci,
1910 struct ocfs2_extent_list *root_el, u32 cpos,
1911 struct buffer_head **leaf_bh)
Mark Fashehdcd05382007-01-16 11:32:23 -08001912{
1913 int ret;
1914 struct buffer_head *bh = NULL;
1915
Joel Beckerfacdb772009-02-12 18:08:48 -08001916 ret = __ocfs2_find_path(ci, root_el, cpos, find_leaf_ins, &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08001917 if (ret) {
1918 mlog_errno(ret);
1919 goto out;
1920 }
1921
1922 *leaf_bh = bh;
1923out:
1924 return ret;
1925}
1926
1927/*
1928 * Adjust the adjacent records (left_rec, right_rec) involved in a rotation.
1929 *
1930 * Basically, we've moved stuff around at the bottom of the tree and
1931 * we need to fix up the extent records above the changes to reflect
1932 * the new changes.
1933 *
1934 * left_rec: the record on the left.
Mark Fashehdcd05382007-01-16 11:32:23 -08001935 * right_rec: the record to the right of left_rec
1936 * right_child_el: is the child list pointed to by right_rec
1937 *
1938 * By definition, this only works on interior nodes.
1939 */
1940static void ocfs2_adjust_adjacent_records(struct ocfs2_extent_rec *left_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08001941 struct ocfs2_extent_rec *right_rec,
1942 struct ocfs2_extent_list *right_child_el)
1943{
1944 u32 left_clusters, right_end;
1945
1946 /*
1947 * Interior nodes never have holes. Their cpos is the cpos of
1948 * the leftmost record in their child list. Their cluster
1949 * count covers the full theoretical range of their child list
1950 * - the range between their cpos and the cpos of the record
1951 * immediately to their right.
1952 */
1953 left_clusters = le32_to_cpu(right_child_el->l_recs[0].e_cpos);
Tao Ma82e12642009-07-23 08:12:58 +08001954 if (!ocfs2_rec_clusters(right_child_el, &right_child_el->l_recs[0])) {
1955 BUG_ON(right_child_el->l_tree_depth);
Mark Fasheh328d5752007-06-18 10:48:04 -07001956 BUG_ON(le16_to_cpu(right_child_el->l_next_free_rec) <= 1);
1957 left_clusters = le32_to_cpu(right_child_el->l_recs[1].e_cpos);
1958 }
Mark Fashehdcd05382007-01-16 11:32:23 -08001959 left_clusters -= le32_to_cpu(left_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001960 left_rec->e_int_clusters = cpu_to_le32(left_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001961
1962 /*
1963 * Calculate the rightmost cluster count boundary before
Mark Fashehe48edee2007-03-07 16:46:57 -08001964 * moving cpos - we will need to adjust clusters after
Mark Fashehdcd05382007-01-16 11:32:23 -08001965 * updating e_cpos to keep the same highest cluster count.
1966 */
1967 right_end = le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001968 right_end += le32_to_cpu(right_rec->e_int_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08001969
1970 right_rec->e_cpos = left_rec->e_cpos;
1971 le32_add_cpu(&right_rec->e_cpos, left_clusters);
1972
1973 right_end -= le32_to_cpu(right_rec->e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08001974 right_rec->e_int_clusters = cpu_to_le32(right_end);
Mark Fashehdcd05382007-01-16 11:32:23 -08001975}
1976
1977/*
1978 * Adjust the adjacent root node records involved in a
1979 * rotation. left_el_blkno is passed in as a key so that we can easily
1980 * find it's index in the root list.
1981 */
1982static void ocfs2_adjust_root_records(struct ocfs2_extent_list *root_el,
1983 struct ocfs2_extent_list *left_el,
1984 struct ocfs2_extent_list *right_el,
1985 u64 left_el_blkno)
1986{
1987 int i;
1988
1989 BUG_ON(le16_to_cpu(root_el->l_tree_depth) <=
1990 le16_to_cpu(left_el->l_tree_depth));
1991
1992 for(i = 0; i < le16_to_cpu(root_el->l_next_free_rec) - 1; i++) {
1993 if (le64_to_cpu(root_el->l_recs[i].e_blkno) == left_el_blkno)
1994 break;
1995 }
1996
1997 /*
1998 * The path walking code should have never returned a root and
1999 * two paths which are not adjacent.
2000 */
2001 BUG_ON(i >= (le16_to_cpu(root_el->l_next_free_rec) - 1));
2002
Jun Piao964f14a2017-09-06 16:19:11 -07002003 ocfs2_adjust_adjacent_records(&root_el->l_recs[i],
Mark Fashehdcd05382007-01-16 11:32:23 -08002004 &root_el->l_recs[i + 1], right_el);
2005}
2006
2007/*
2008 * We've changed a leaf block (in right_path) and need to reflect that
2009 * change back up the subtree.
2010 *
2011 * This happens in multiple places:
2012 * - When we've moved an extent record from the left path leaf to the right
2013 * path leaf to make room for an empty extent in the left path leaf.
2014 * - When our insert into the right path leaf is at the leftmost edge
2015 * and requires an update of the path immediately to it's left. This
2016 * can occur at the end of some types of rotation and appending inserts.
Tao Ma677b9752008-01-30 14:21:05 +08002017 * - When we've adjusted the last extent record in the left path leaf and the
2018 * 1st extent record in the right path leaf during cross extent block merge.
Mark Fashehdcd05382007-01-16 11:32:23 -08002019 */
Joel Becker4619c732009-02-12 19:02:36 -08002020static void ocfs2_complete_edge_insert(handle_t *handle,
Mark Fashehdcd05382007-01-16 11:32:23 -08002021 struct ocfs2_path *left_path,
2022 struct ocfs2_path *right_path,
2023 int subtree_index)
2024{
Joel Beckerec20cec2010-03-19 14:13:52 -07002025 int i, idx;
Mark Fashehdcd05382007-01-16 11:32:23 -08002026 struct ocfs2_extent_list *el, *left_el, *right_el;
2027 struct ocfs2_extent_rec *left_rec, *right_rec;
2028 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2029
2030 /*
2031 * Update the counts and position values within all the
2032 * interior nodes to reflect the leaf rotation we just did.
2033 *
2034 * The root node is handled below the loop.
2035 *
2036 * We begin the loop with right_el and left_el pointing to the
2037 * leaf lists and work our way up.
2038 *
2039 * NOTE: within this loop, left_el and right_el always refer
2040 * to the *child* lists.
2041 */
2042 left_el = path_leaf_el(left_path);
2043 right_el = path_leaf_el(right_path);
2044 for(i = left_path->p_tree_depth - 1; i > subtree_index; i--) {
Tao Maa09d09b2011-02-22 08:21:10 +08002045 trace_ocfs2_complete_edge_insert(i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002046
2047 /*
2048 * One nice property of knowing that all of these
2049 * nodes are below the root is that we only deal with
2050 * the leftmost right node record and the rightmost
2051 * left node record.
2052 */
2053 el = left_path->p_node[i].el;
2054 idx = le16_to_cpu(left_el->l_next_free_rec) - 1;
2055 left_rec = &el->l_recs[idx];
2056
2057 el = right_path->p_node[i].el;
2058 right_rec = &el->l_recs[0];
2059
Jun Piao964f14a2017-09-06 16:19:11 -07002060 ocfs2_adjust_adjacent_records(left_rec, right_rec, right_el);
Mark Fashehdcd05382007-01-16 11:32:23 -08002061
Joel Beckerec20cec2010-03-19 14:13:52 -07002062 ocfs2_journal_dirty(handle, left_path->p_node[i].bh);
2063 ocfs2_journal_dirty(handle, right_path->p_node[i].bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002064
2065 /*
2066 * Setup our list pointers now so that the current
2067 * parents become children in the next iteration.
2068 */
2069 left_el = left_path->p_node[i].el;
2070 right_el = right_path->p_node[i].el;
2071 }
2072
2073 /*
2074 * At the root node, adjust the two adjacent records which
2075 * begin our path to the leaves.
2076 */
2077
2078 el = left_path->p_node[subtree_index].el;
2079 left_el = left_path->p_node[subtree_index + 1].el;
2080 right_el = right_path->p_node[subtree_index + 1].el;
2081
2082 ocfs2_adjust_root_records(el, left_el, right_el,
2083 left_path->p_node[subtree_index + 1].bh->b_blocknr);
2084
2085 root_bh = left_path->p_node[subtree_index].bh;
2086
Joel Beckerec20cec2010-03-19 14:13:52 -07002087 ocfs2_journal_dirty(handle, root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002088}
2089
Joel Becker5c601ab2009-02-12 19:10:13 -08002090static int ocfs2_rotate_subtree_right(handle_t *handle,
2091 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08002092 struct ocfs2_path *left_path,
2093 struct ocfs2_path *right_path,
2094 int subtree_index)
2095{
2096 int ret, i;
2097 struct buffer_head *right_leaf_bh;
2098 struct buffer_head *left_leaf_bh = NULL;
2099 struct buffer_head *root_bh;
2100 struct ocfs2_extent_list *right_el, *left_el;
2101 struct ocfs2_extent_rec move_rec;
2102
2103 left_leaf_bh = path_leaf_bh(left_path);
2104 left_el = path_leaf_el(left_path);
2105
2106 if (left_el->l_next_free_rec != left_el->l_count) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002107 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Joe Perches7ecef142015-09-04 15:44:51 -07002108 "Inode %llu has non-full interior leaf node %llu (next free = %u)\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002109 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002110 (unsigned long long)left_leaf_bh->b_blocknr,
2111 le16_to_cpu(left_el->l_next_free_rec));
2112 return -EROFS;
2113 }
2114
2115 /*
2116 * This extent block may already have an empty record, so we
2117 * return early if so.
2118 */
2119 if (ocfs2_is_empty_extent(&left_el->l_recs[0]))
2120 return 0;
2121
2122 root_bh = left_path->p_node[subtree_index].bh;
2123 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2124
Joel Becker5c601ab2009-02-12 19:10:13 -08002125 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002126 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002127 if (ret) {
2128 mlog_errno(ret);
2129 goto out;
2130 }
2131
2132 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker5c601ab2009-02-12 19:10:13 -08002133 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002134 right_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002135 if (ret) {
2136 mlog_errno(ret);
2137 goto out;
2138 }
2139
Joel Becker5c601ab2009-02-12 19:10:13 -08002140 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002141 left_path, i);
Mark Fashehdcd05382007-01-16 11:32:23 -08002142 if (ret) {
2143 mlog_errno(ret);
2144 goto out;
2145 }
2146 }
2147
2148 right_leaf_bh = path_leaf_bh(right_path);
2149 right_el = path_leaf_el(right_path);
2150
2151 /* This is a code error, not a disk corruption. */
2152 mlog_bug_on_msg(!right_el->l_next_free_rec, "Inode %llu: Rotate fails "
2153 "because rightmost leaf block %llu is empty\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002154 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08002155 (unsigned long long)right_leaf_bh->b_blocknr);
2156
2157 ocfs2_create_empty_extent(right_el);
2158
Joel Beckerec20cec2010-03-19 14:13:52 -07002159 ocfs2_journal_dirty(handle, right_leaf_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002160
2161 /* Do the copy now. */
2162 i = le16_to_cpu(left_el->l_next_free_rec) - 1;
2163 move_rec = left_el->l_recs[i];
2164 right_el->l_recs[0] = move_rec;
2165
2166 /*
2167 * Clear out the record we just copied and shift everything
2168 * over, leaving an empty extent in the left leaf.
2169 *
2170 * We temporarily subtract from next_free_rec so that the
2171 * shift will lose the tail record (which is now defunct).
2172 */
2173 le16_add_cpu(&left_el->l_next_free_rec, -1);
2174 ocfs2_shift_records_right(left_el);
2175 memset(&left_el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2176 le16_add_cpu(&left_el->l_next_free_rec, 1);
2177
Joel Beckerec20cec2010-03-19 14:13:52 -07002178 ocfs2_journal_dirty(handle, left_leaf_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08002179
Joel Becker4619c732009-02-12 19:02:36 -08002180 ocfs2_complete_edge_insert(handle, left_path, right_path,
2181 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08002182
2183out:
2184 return ret;
2185}
2186
2187/*
2188 * Given a full path, determine what cpos value would return us a path
2189 * containing the leaf immediately to the left of the current one.
2190 *
2191 * Will return zero if the path passed in is already the leftmost path.
2192 */
Tristan Yeee149a72010-05-11 17:54:44 +08002193int ocfs2_find_cpos_for_left_leaf(struct super_block *sb,
2194 struct ocfs2_path *path, u32 *cpos)
Mark Fashehdcd05382007-01-16 11:32:23 -08002195{
2196 int i, j, ret = 0;
2197 u64 blkno;
2198 struct ocfs2_extent_list *el;
2199
Mark Fashehe48edee2007-03-07 16:46:57 -08002200 BUG_ON(path->p_tree_depth == 0);
2201
Mark Fashehdcd05382007-01-16 11:32:23 -08002202 *cpos = 0;
2203
2204 blkno = path_leaf_bh(path)->b_blocknr;
2205
2206 /* Start at the tree node just above the leaf and work our way up. */
2207 i = path->p_tree_depth - 1;
2208 while (i >= 0) {
2209 el = path->p_node[i].el;
2210
2211 /*
2212 * Find the extent record just before the one in our
2213 * path.
2214 */
2215 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2216 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2217 if (j == 0) {
2218 if (i == 0) {
2219 /*
2220 * We've determined that the
2221 * path specified is already
2222 * the leftmost one - return a
2223 * cpos of zero.
2224 */
2225 goto out;
2226 }
2227 /*
2228 * The leftmost record points to our
2229 * leaf - we need to travel up the
2230 * tree one level.
2231 */
2232 goto next_node;
2233 }
2234
2235 *cpos = le32_to_cpu(el->l_recs[j - 1].e_cpos);
Mark Fashehe48edee2007-03-07 16:46:57 -08002236 *cpos = *cpos + ocfs2_rec_clusters(el,
2237 &el->l_recs[j - 1]);
2238 *cpos = *cpos - 1;
Mark Fashehdcd05382007-01-16 11:32:23 -08002239 goto out;
2240 }
2241 }
2242
2243 /*
2244 * If we got here, we never found a valid node where
2245 * the tree indicated one should be.
2246 */
Joe Perches7ecef142015-09-04 15:44:51 -07002247 ocfs2_error(sb, "Invalid extent tree at extent block %llu\n",
Mark Fashehdcd05382007-01-16 11:32:23 -08002248 (unsigned long long)blkno);
2249 ret = -EROFS;
2250 goto out;
2251
2252next_node:
2253 blkno = path->p_node[i].bh->b_blocknr;
2254 i--;
2255 }
2256
2257out:
2258 return ret;
2259}
2260
Mark Fasheh328d5752007-06-18 10:48:04 -07002261/*
2262 * Extend the transaction by enough credits to complete the rotation,
2263 * and still leave at least the original number of credits allocated
2264 * to this transaction.
2265 */
Mark Fashehdcd05382007-01-16 11:32:23 -08002266static int ocfs2_extend_rotate_transaction(handle_t *handle, int subtree_depth,
Mark Fasheh328d5752007-06-18 10:48:04 -07002267 int op_credits,
Mark Fashehdcd05382007-01-16 11:32:23 -08002268 struct ocfs2_path *path)
2269{
Tao Mac901fb02010-04-26 14:34:57 +08002270 int ret = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07002271 int credits = (path->p_tree_depth - subtree_depth) * 2 + 1 + op_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002272
Tao Mac901fb02010-04-26 14:34:57 +08002273 if (handle->h_buffer_credits < credits)
Tao Mac18b8122009-08-18 11:44:07 +08002274 ret = ocfs2_extend_trans(handle,
2275 credits - handle->h_buffer_credits);
Tao Mac18b8122009-08-18 11:44:07 +08002276
Tao Mac901fb02010-04-26 14:34:57 +08002277 return ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08002278}
2279
2280/*
2281 * Trap the case where we're inserting into the theoretical range past
2282 * the _actual_ left leaf range. Otherwise, we'll rotate a record
2283 * whose cpos is less than ours into the right leaf.
2284 *
2285 * It's only necessary to look at the rightmost record of the left
2286 * leaf because the logic that calls us should ensure that the
2287 * theoretical ranges in the path components above the leaves are
2288 * correct.
2289 */
2290static int ocfs2_rotate_requires_path_adjustment(struct ocfs2_path *left_path,
2291 u32 insert_cpos)
2292{
2293 struct ocfs2_extent_list *left_el;
2294 struct ocfs2_extent_rec *rec;
2295 int next_free;
2296
2297 left_el = path_leaf_el(left_path);
2298 next_free = le16_to_cpu(left_el->l_next_free_rec);
2299 rec = &left_el->l_recs[next_free - 1];
2300
2301 if (insert_cpos > le32_to_cpu(rec->e_cpos))
2302 return 1;
2303 return 0;
2304}
2305
Mark Fasheh328d5752007-06-18 10:48:04 -07002306static int ocfs2_leftmost_rec_contains(struct ocfs2_extent_list *el, u32 cpos)
2307{
2308 int next_free = le16_to_cpu(el->l_next_free_rec);
2309 unsigned int range;
2310 struct ocfs2_extent_rec *rec;
2311
2312 if (next_free == 0)
2313 return 0;
2314
2315 rec = &el->l_recs[0];
2316 if (ocfs2_is_empty_extent(rec)) {
2317 /* Empty list. */
2318 if (next_free == 1)
2319 return 0;
2320 rec = &el->l_recs[1];
2321 }
2322
2323 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2324 if (cpos >= le32_to_cpu(rec->e_cpos) && cpos < range)
2325 return 1;
2326 return 0;
2327}
2328
Mark Fashehdcd05382007-01-16 11:32:23 -08002329/*
2330 * Rotate all the records in a btree right one record, starting at insert_cpos.
2331 *
2332 * The path to the rightmost leaf should be passed in.
2333 *
2334 * The array is assumed to be large enough to hold an entire path (tree depth).
2335 *
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002336 * Upon successful return from this function:
Mark Fashehdcd05382007-01-16 11:32:23 -08002337 *
2338 * - The 'right_path' array will contain a path to the leaf block
2339 * whose range contains e_cpos.
2340 * - That leaf block will have a single empty extent in list index 0.
2341 * - In the case that the rotation requires a post-insert update,
2342 * *ret_left_path will contain a valid path which can be passed to
2343 * ocfs2_insert_path().
2344 */
Joel Becker1bbf0b82009-02-12 19:42:08 -08002345static int ocfs2_rotate_tree_right(handle_t *handle,
Joel Becker5c601ab2009-02-12 19:10:13 -08002346 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002347 enum ocfs2_split_type split,
Mark Fashehdcd05382007-01-16 11:32:23 -08002348 u32 insert_cpos,
2349 struct ocfs2_path *right_path,
2350 struct ocfs2_path **ret_left_path)
2351{
Mark Fasheh328d5752007-06-18 10:48:04 -07002352 int ret, start, orig_credits = handle->h_buffer_credits;
Mark Fashehdcd05382007-01-16 11:32:23 -08002353 u32 cpos;
2354 struct ocfs2_path *left_path = NULL;
Joel Becker5c601ab2009-02-12 19:10:13 -08002355 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehdcd05382007-01-16 11:32:23 -08002356
2357 *ret_left_path = NULL;
2358
Joel Beckerffdd7a52008-10-17 22:32:01 -07002359 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002360 if (!left_path) {
2361 ret = -ENOMEM;
2362 mlog_errno(ret);
2363 goto out;
2364 }
2365
Joel Becker5c601ab2009-02-12 19:10:13 -08002366 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002367 if (ret) {
2368 mlog_errno(ret);
2369 goto out;
2370 }
2371
Tao Maa09d09b2011-02-22 08:21:10 +08002372 trace_ocfs2_rotate_tree_right(
2373 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2374 insert_cpos, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002375
2376 /*
2377 * What we want to do here is:
2378 *
2379 * 1) Start with the rightmost path.
2380 *
2381 * 2) Determine a path to the leaf block directly to the left
2382 * of that leaf.
2383 *
2384 * 3) Determine the 'subtree root' - the lowest level tree node
2385 * which contains a path to both leaves.
2386 *
2387 * 4) Rotate the subtree.
2388 *
2389 * 5) Find the next subtree by considering the left path to be
2390 * the new right path.
2391 *
2392 * The check at the top of this while loop also accepts
2393 * insert_cpos == cpos because cpos is only a _theoretical_
2394 * value to get us the left path - insert_cpos might very well
2395 * be filling that hole.
2396 *
2397 * Stop at a cpos of '0' because we either started at the
2398 * leftmost branch (i.e., a tree with one branch and a
2399 * rotation inside of it), or we've gone as far as we can in
2400 * rotating subtrees.
2401 */
2402 while (cpos && insert_cpos <= cpos) {
Tao Maa09d09b2011-02-22 08:21:10 +08002403 trace_ocfs2_rotate_tree_right(
2404 (unsigned long long)
2405 ocfs2_metadata_cache_owner(et->et_ci),
2406 insert_cpos, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002407
Joel Becker5c601ab2009-02-12 19:10:13 -08002408 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002409 if (ret) {
2410 mlog_errno(ret);
2411 goto out;
2412 }
2413
2414 mlog_bug_on_msg(path_leaf_bh(left_path) ==
2415 path_leaf_bh(right_path),
Joel Becker5c601ab2009-02-12 19:10:13 -08002416 "Owner %llu: error during insert of %u "
Mark Fashehdcd05382007-01-16 11:32:23 -08002417 "(left path cpos %u) results in two identical "
2418 "paths ending at %llu\n",
Joel Becker5c601ab2009-02-12 19:10:13 -08002419 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
2420 insert_cpos, cpos,
Mark Fashehdcd05382007-01-16 11:32:23 -08002421 (unsigned long long)
2422 path_leaf_bh(left_path)->b_blocknr);
2423
Mark Fasheh328d5752007-06-18 10:48:04 -07002424 if (split == SPLIT_NONE &&
2425 ocfs2_rotate_requires_path_adjustment(left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002426 insert_cpos)) {
Mark Fashehdcd05382007-01-16 11:32:23 -08002427
2428 /*
2429 * We've rotated the tree as much as we
2430 * should. The rest is up to
2431 * ocfs2_insert_path() to complete, after the
2432 * record insertion. We indicate this
2433 * situation by returning the left path.
2434 *
2435 * The reason we don't adjust the records here
2436 * before the record insert is that an error
2437 * later might break the rule where a parent
2438 * record e_cpos will reflect the actual
2439 * e_cpos of the 1st nonempty record of the
2440 * child list.
2441 */
2442 *ret_left_path = left_path;
2443 goto out_ret_path;
2444 }
2445
Joel Becker7dc02802009-02-12 19:20:13 -08002446 start = ocfs2_find_subtree_root(et, left_path, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002447
Tao Maa09d09b2011-02-22 08:21:10 +08002448 trace_ocfs2_rotate_subtree(start,
2449 (unsigned long long)
2450 right_path->p_node[start].bh->b_blocknr,
2451 right_path->p_tree_depth);
Mark Fashehdcd05382007-01-16 11:32:23 -08002452
2453 ret = ocfs2_extend_rotate_transaction(handle, start,
Mark Fasheh328d5752007-06-18 10:48:04 -07002454 orig_credits, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08002455 if (ret) {
2456 mlog_errno(ret);
2457 goto out;
2458 }
2459
Joel Becker5c601ab2009-02-12 19:10:13 -08002460 ret = ocfs2_rotate_subtree_right(handle, et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08002461 right_path, start);
2462 if (ret) {
2463 mlog_errno(ret);
2464 goto out;
2465 }
2466
Mark Fasheh328d5752007-06-18 10:48:04 -07002467 if (split != SPLIT_NONE &&
2468 ocfs2_leftmost_rec_contains(path_leaf_el(right_path),
2469 insert_cpos)) {
2470 /*
2471 * A rotate moves the rightmost left leaf
2472 * record over to the leftmost right leaf
2473 * slot. If we're doing an extent split
2474 * instead of a real insert, then we have to
2475 * check that the extent to be split wasn't
2476 * just moved over. If it was, then we can
2477 * exit here, passing left_path back -
2478 * ocfs2_split_extent() is smart enough to
2479 * search both leaves.
2480 */
2481 *ret_left_path = left_path;
2482 goto out_ret_path;
2483 }
2484
Mark Fashehdcd05382007-01-16 11:32:23 -08002485 /*
2486 * There is no need to re-read the next right path
2487 * as we know that it'll be our current left
2488 * path. Optimize by copying values instead.
2489 */
2490 ocfs2_mv_path(right_path, left_path);
2491
Joel Becker5c601ab2009-02-12 19:10:13 -08002492 ret = ocfs2_find_cpos_for_left_leaf(sb, right_path, &cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08002493 if (ret) {
2494 mlog_errno(ret);
2495 goto out;
2496 }
2497 }
2498
2499out:
2500 ocfs2_free_path(left_path);
2501
2502out_ret_path:
2503 return ret;
2504}
2505
Joel Becker09106ba2009-02-12 19:43:57 -08002506static int ocfs2_update_edge_lengths(handle_t *handle,
2507 struct ocfs2_extent_tree *et,
Jun Piao964f14a2017-09-06 16:19:11 -07002508 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002509{
Tao Ma3c5e1062009-07-21 15:42:05 +08002510 int i, idx, ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002511 struct ocfs2_extent_rec *rec;
2512 struct ocfs2_extent_list *el;
2513 struct ocfs2_extent_block *eb;
2514 u32 range;
2515
Joel Becker09106ba2009-02-12 19:43:57 -08002516 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002517 if (ret) {
2518 mlog_errno(ret);
2519 goto out;
2520 }
2521
Mark Fasheh328d5752007-06-18 10:48:04 -07002522 /* Path should always be rightmost. */
2523 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
2524 BUG_ON(eb->h_next_leaf_blk != 0ULL);
2525
2526 el = &eb->h_list;
2527 BUG_ON(le16_to_cpu(el->l_next_free_rec) == 0);
2528 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2529 rec = &el->l_recs[idx];
2530 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
2531
2532 for (i = 0; i < path->p_tree_depth; i++) {
2533 el = path->p_node[i].el;
2534 idx = le16_to_cpu(el->l_next_free_rec) - 1;
2535 rec = &el->l_recs[idx];
2536
2537 rec->e_int_clusters = cpu_to_le32(range);
2538 le32_add_cpu(&rec->e_int_clusters, -le32_to_cpu(rec->e_cpos));
2539
2540 ocfs2_journal_dirty(handle, path->p_node[i].bh);
2541 }
Tao Ma3c5e1062009-07-21 15:42:05 +08002542out:
2543 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07002544}
2545
Joel Becker6641b0c2009-02-12 18:57:52 -08002546static void ocfs2_unlink_path(handle_t *handle,
2547 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002548 struct ocfs2_cached_dealloc_ctxt *dealloc,
2549 struct ocfs2_path *path, int unlink_start)
2550{
2551 int ret, i;
2552 struct ocfs2_extent_block *eb;
2553 struct ocfs2_extent_list *el;
2554 struct buffer_head *bh;
2555
2556 for(i = unlink_start; i < path_num_items(path); i++) {
2557 bh = path->p_node[i].bh;
2558
2559 eb = (struct ocfs2_extent_block *)bh->b_data;
2560 /*
2561 * Not all nodes might have had their final count
2562 * decremented by the caller - handle this here.
2563 */
2564 el = &eb->h_list;
2565 if (le16_to_cpu(el->l_next_free_rec) > 1) {
2566 mlog(ML_ERROR,
2567 "Inode %llu, attempted to remove extent block "
2568 "%llu with %u records\n",
Joel Becker6641b0c2009-02-12 18:57:52 -08002569 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07002570 (unsigned long long)le64_to_cpu(eb->h_blkno),
2571 le16_to_cpu(el->l_next_free_rec));
2572
2573 ocfs2_journal_dirty(handle, bh);
Joel Becker6641b0c2009-02-12 18:57:52 -08002574 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002575 continue;
2576 }
2577
2578 el->l_next_free_rec = 0;
2579 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
2580
2581 ocfs2_journal_dirty(handle, bh);
2582
2583 ret = ocfs2_cache_extent_block_free(dealloc, eb);
2584 if (ret)
2585 mlog_errno(ret);
2586
Joel Becker6641b0c2009-02-12 18:57:52 -08002587 ocfs2_remove_from_cache(et->et_ci, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002588 }
2589}
2590
Joel Becker6641b0c2009-02-12 18:57:52 -08002591static void ocfs2_unlink_subtree(handle_t *handle,
2592 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002593 struct ocfs2_path *left_path,
2594 struct ocfs2_path *right_path,
2595 int subtree_index,
2596 struct ocfs2_cached_dealloc_ctxt *dealloc)
2597{
2598 int i;
2599 struct buffer_head *root_bh = left_path->p_node[subtree_index].bh;
2600 struct ocfs2_extent_list *root_el = left_path->p_node[subtree_index].el;
2601 struct ocfs2_extent_list *el;
2602 struct ocfs2_extent_block *eb;
2603
2604 el = path_leaf_el(left_path);
2605
2606 eb = (struct ocfs2_extent_block *)right_path->p_node[subtree_index + 1].bh->b_data;
2607
2608 for(i = 1; i < le16_to_cpu(root_el->l_next_free_rec); i++)
2609 if (root_el->l_recs[i].e_blkno == eb->h_blkno)
2610 break;
2611
2612 BUG_ON(i >= le16_to_cpu(root_el->l_next_free_rec));
2613
2614 memset(&root_el->l_recs[i], 0, sizeof(struct ocfs2_extent_rec));
2615 le16_add_cpu(&root_el->l_next_free_rec, -1);
2616
2617 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
2618 eb->h_next_leaf_blk = 0;
2619
2620 ocfs2_journal_dirty(handle, root_bh);
2621 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2622
Joel Becker6641b0c2009-02-12 18:57:52 -08002623 ocfs2_unlink_path(handle, et, dealloc, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002624 subtree_index + 1);
2625}
2626
Joel Becker1e2dd632009-02-12 19:45:28 -08002627static int ocfs2_rotate_subtree_left(handle_t *handle,
2628 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07002629 struct ocfs2_path *left_path,
2630 struct ocfs2_path *right_path,
2631 int subtree_index,
2632 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker1e2dd632009-02-12 19:45:28 -08002633 int *deleted)
Mark Fasheh328d5752007-06-18 10:48:04 -07002634{
2635 int ret, i, del_right_subtree = 0, right_has_empty = 0;
Tao Mae7d4cb62008-08-18 17:38:44 +08002636 struct buffer_head *root_bh, *et_root_bh = path_root_bh(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002637 struct ocfs2_extent_list *right_leaf_el, *left_leaf_el;
2638 struct ocfs2_extent_block *eb;
2639
2640 *deleted = 0;
2641
2642 right_leaf_el = path_leaf_el(right_path);
2643 left_leaf_el = path_leaf_el(left_path);
2644 root_bh = left_path->p_node[subtree_index].bh;
2645 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
2646
2647 if (!ocfs2_is_empty_extent(&left_leaf_el->l_recs[0]))
2648 return 0;
2649
2650 eb = (struct ocfs2_extent_block *)path_leaf_bh(right_path)->b_data;
2651 if (ocfs2_is_empty_extent(&right_leaf_el->l_recs[0])) {
2652 /*
2653 * It's legal for us to proceed if the right leaf is
2654 * the rightmost one and it has an empty extent. There
2655 * are two cases to handle - whether the leaf will be
2656 * empty after removal or not. If the leaf isn't empty
2657 * then just remove the empty extent up front. The
2658 * next block will handle empty leaves by flagging
2659 * them for unlink.
2660 *
2661 * Non rightmost leaves will throw -EAGAIN and the
2662 * caller can manually move the subtree and retry.
2663 */
2664
2665 if (eb->h_next_leaf_blk != 0ULL)
2666 return -EAGAIN;
2667
2668 if (le16_to_cpu(right_leaf_el->l_next_free_rec) > 1) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002669 ret = ocfs2_journal_access_eb(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002670 path_leaf_bh(right_path),
2671 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002672 if (ret) {
2673 mlog_errno(ret);
2674 goto out;
2675 }
2676
2677 ocfs2_remove_empty_extent(right_leaf_el);
2678 } else
2679 right_has_empty = 1;
2680 }
2681
2682 if (eb->h_next_leaf_blk == 0ULL &&
2683 le16_to_cpu(right_leaf_el->l_next_free_rec) == 1) {
2684 /*
2685 * We have to update i_last_eb_blk during the meta
2686 * data delete.
2687 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08002688 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07002689 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh328d5752007-06-18 10:48:04 -07002690 if (ret) {
2691 mlog_errno(ret);
2692 goto out;
2693 }
2694
2695 del_right_subtree = 1;
2696 }
2697
2698 /*
2699 * Getting here with an empty extent in the right path implies
2700 * that it's the rightmost path and will be deleted.
2701 */
2702 BUG_ON(right_has_empty && !del_right_subtree);
2703
Joel Becker1e2dd632009-02-12 19:45:28 -08002704 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07002705 subtree_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07002706 if (ret) {
2707 mlog_errno(ret);
2708 goto out;
2709 }
2710
2711 for(i = subtree_index + 1; i < path_num_items(right_path); i++) {
Joel Becker1e2dd632009-02-12 19:45:28 -08002712 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002713 right_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002714 if (ret) {
2715 mlog_errno(ret);
2716 goto out;
2717 }
2718
Joel Becker1e2dd632009-02-12 19:45:28 -08002719 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002720 left_path, i);
Mark Fasheh328d5752007-06-18 10:48:04 -07002721 if (ret) {
2722 mlog_errno(ret);
2723 goto out;
2724 }
2725 }
2726
2727 if (!right_has_empty) {
2728 /*
2729 * Only do this if we're moving a real
2730 * record. Otherwise, the action is delayed until
2731 * after removal of the right path in which case we
2732 * can do a simple shift to remove the empty extent.
2733 */
2734 ocfs2_rotate_leaf(left_leaf_el, &right_leaf_el->l_recs[0]);
2735 memset(&right_leaf_el->l_recs[0], 0,
2736 sizeof(struct ocfs2_extent_rec));
2737 }
2738 if (eb->h_next_leaf_blk == 0ULL) {
2739 /*
2740 * Move recs over to get rid of empty extent, decrease
2741 * next_free. This is allowed to remove the last
2742 * extent in our leaf (setting l_next_free_rec to
2743 * zero) - the delete code below won't care.
2744 */
2745 ocfs2_remove_empty_extent(right_leaf_el);
2746 }
2747
Joel Beckerec20cec2010-03-19 14:13:52 -07002748 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
2749 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
Mark Fasheh328d5752007-06-18 10:48:04 -07002750
2751 if (del_right_subtree) {
Joel Becker6641b0c2009-02-12 18:57:52 -08002752 ocfs2_unlink_subtree(handle, et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002753 subtree_index, dealloc);
Jun Piao964f14a2017-09-06 16:19:11 -07002754 ret = ocfs2_update_edge_lengths(handle, et, left_path);
Tao Ma3c5e1062009-07-21 15:42:05 +08002755 if (ret) {
2756 mlog_errno(ret);
2757 goto out;
2758 }
Mark Fasheh328d5752007-06-18 10:48:04 -07002759
2760 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07002761 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07002762
2763 /*
2764 * Removal of the extent in the left leaf was skipped
2765 * above so we could delete the right path
2766 * 1st.
2767 */
2768 if (right_has_empty)
2769 ocfs2_remove_empty_extent(left_leaf_el);
2770
Joel Beckerec20cec2010-03-19 14:13:52 -07002771 ocfs2_journal_dirty(handle, et_root_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002772
2773 *deleted = 1;
2774 } else
Joel Becker4619c732009-02-12 19:02:36 -08002775 ocfs2_complete_edge_insert(handle, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002776 subtree_index);
2777
2778out:
2779 return ret;
2780}
2781
2782/*
2783 * Given a full path, determine what cpos value would return us a path
2784 * containing the leaf immediately to the right of the current one.
2785 *
2786 * Will return zero if the path passed in is already the rightmost path.
2787 *
2788 * This looks similar, but is subtly different to
2789 * ocfs2_find_cpos_for_left_leaf().
2790 */
Tao Ma38a04e42009-11-30 14:32:19 +08002791int ocfs2_find_cpos_for_right_leaf(struct super_block *sb,
2792 struct ocfs2_path *path, u32 *cpos)
Mark Fasheh328d5752007-06-18 10:48:04 -07002793{
2794 int i, j, ret = 0;
2795 u64 blkno;
2796 struct ocfs2_extent_list *el;
2797
2798 *cpos = 0;
2799
2800 if (path->p_tree_depth == 0)
2801 return 0;
2802
2803 blkno = path_leaf_bh(path)->b_blocknr;
2804
2805 /* Start at the tree node just above the leaf and work our way up. */
2806 i = path->p_tree_depth - 1;
2807 while (i >= 0) {
2808 int next_free;
2809
2810 el = path->p_node[i].el;
2811
2812 /*
2813 * Find the extent record just after the one in our
2814 * path.
2815 */
2816 next_free = le16_to_cpu(el->l_next_free_rec);
2817 for(j = 0; j < le16_to_cpu(el->l_next_free_rec); j++) {
2818 if (le64_to_cpu(el->l_recs[j].e_blkno) == blkno) {
2819 if (j == (next_free - 1)) {
2820 if (i == 0) {
2821 /*
2822 * We've determined that the
2823 * path specified is already
2824 * the rightmost one - return a
2825 * cpos of zero.
2826 */
2827 goto out;
2828 }
2829 /*
2830 * The rightmost record points to our
2831 * leaf - we need to travel up the
2832 * tree one level.
2833 */
2834 goto next_node;
2835 }
2836
2837 *cpos = le32_to_cpu(el->l_recs[j + 1].e_cpos);
2838 goto out;
2839 }
2840 }
2841
2842 /*
2843 * If we got here, we never found a valid node where
2844 * the tree indicated one should be.
2845 */
Joe Perches7ecef142015-09-04 15:44:51 -07002846 ocfs2_error(sb, "Invalid extent tree at extent block %llu\n",
Mark Fasheh328d5752007-06-18 10:48:04 -07002847 (unsigned long long)blkno);
2848 ret = -EROFS;
2849 goto out;
2850
2851next_node:
2852 blkno = path->p_node[i].bh->b_blocknr;
2853 i--;
2854 }
2855
2856out:
2857 return ret;
2858}
2859
Joel Becker70f18c02009-02-13 02:09:31 -08002860static int ocfs2_rotate_rightmost_leaf_left(handle_t *handle,
2861 struct ocfs2_extent_tree *et,
Joel Becker13723d02008-10-17 19:25:01 -07002862 struct ocfs2_path *path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002863{
2864 int ret;
Joel Becker13723d02008-10-17 19:25:01 -07002865 struct buffer_head *bh = path_leaf_bh(path);
2866 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002867
2868 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
2869 return 0;
2870
Joel Becker70f18c02009-02-13 02:09:31 -08002871 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Joel Becker13723d02008-10-17 19:25:01 -07002872 path_num_items(path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07002873 if (ret) {
2874 mlog_errno(ret);
2875 goto out;
2876 }
2877
2878 ocfs2_remove_empty_extent(el);
Joel Beckerec20cec2010-03-19 14:13:52 -07002879 ocfs2_journal_dirty(handle, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07002880
2881out:
2882 return ret;
2883}
2884
Joel Beckere46f74d2009-02-12 19:47:43 -08002885static int __ocfs2_rotate_tree_left(handle_t *handle,
2886 struct ocfs2_extent_tree *et,
2887 int orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07002888 struct ocfs2_path *path,
2889 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08002890 struct ocfs2_path **empty_extent_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07002891{
2892 int ret, subtree_root, deleted;
2893 u32 right_cpos;
2894 struct ocfs2_path *left_path = NULL;
2895 struct ocfs2_path *right_path = NULL;
Joel Beckere46f74d2009-02-12 19:47:43 -08002896 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fasheh328d5752007-06-18 10:48:04 -07002897
Xue jiufei099768b2015-06-24 16:55:09 -07002898 if (!ocfs2_is_empty_extent(&(path_leaf_el(path)->l_recs[0])))
2899 return 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07002900
2901 *empty_extent_path = NULL;
2902
Joel Beckere46f74d2009-02-12 19:47:43 -08002903 ret = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002904 if (ret) {
2905 mlog_errno(ret);
2906 goto out;
2907 }
2908
Joel Beckerffdd7a52008-10-17 22:32:01 -07002909 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002910 if (!left_path) {
2911 ret = -ENOMEM;
2912 mlog_errno(ret);
2913 goto out;
2914 }
2915
2916 ocfs2_cp_path(left_path, path);
2917
Joel Beckerffdd7a52008-10-17 22:32:01 -07002918 right_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07002919 if (!right_path) {
2920 ret = -ENOMEM;
2921 mlog_errno(ret);
2922 goto out;
2923 }
2924
2925 while (right_cpos) {
Joel Beckerfacdb772009-02-12 18:08:48 -08002926 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07002927 if (ret) {
2928 mlog_errno(ret);
2929 goto out;
2930 }
2931
Joel Becker7dc02802009-02-12 19:20:13 -08002932 subtree_root = ocfs2_find_subtree_root(et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002933 right_path);
2934
Tao Maa09d09b2011-02-22 08:21:10 +08002935 trace_ocfs2_rotate_subtree(subtree_root,
Mark Fasheh328d5752007-06-18 10:48:04 -07002936 (unsigned long long)
2937 right_path->p_node[subtree_root].bh->b_blocknr,
2938 right_path->p_tree_depth);
2939
Xue jiufei17215982016-03-25 14:21:41 -07002940 ret = ocfs2_extend_rotate_transaction(handle, 0,
Mark Fasheh328d5752007-06-18 10:48:04 -07002941 orig_credits, left_path);
2942 if (ret) {
2943 mlog_errno(ret);
2944 goto out;
2945 }
2946
Mark Fashehe8aed342007-12-03 16:43:01 -08002947 /*
2948 * Caller might still want to make changes to the
2949 * tree root, so re-add it to the journal here.
2950 */
Joel Beckere46f74d2009-02-12 19:47:43 -08002951 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07002952 left_path, 0);
Mark Fashehe8aed342007-12-03 16:43:01 -08002953 if (ret) {
2954 mlog_errno(ret);
2955 goto out;
2956 }
2957
Joel Becker1e2dd632009-02-12 19:45:28 -08002958 ret = ocfs2_rotate_subtree_left(handle, et, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002959 right_path, subtree_root,
Joel Becker1e2dd632009-02-12 19:45:28 -08002960 dealloc, &deleted);
Mark Fasheh328d5752007-06-18 10:48:04 -07002961 if (ret == -EAGAIN) {
2962 /*
2963 * The rotation has to temporarily stop due to
2964 * the right subtree having an empty
2965 * extent. Pass it back to the caller for a
2966 * fixup.
2967 */
2968 *empty_extent_path = right_path;
2969 right_path = NULL;
2970 goto out;
2971 }
2972 if (ret) {
2973 mlog_errno(ret);
2974 goto out;
2975 }
2976
2977 /*
2978 * The subtree rotate might have removed records on
2979 * the rightmost edge. If so, then rotation is
2980 * complete.
2981 */
2982 if (deleted)
2983 break;
2984
2985 ocfs2_mv_path(left_path, right_path);
2986
Joel Beckere46f74d2009-02-12 19:47:43 -08002987 ret = ocfs2_find_cpos_for_right_leaf(sb, left_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07002988 &right_cpos);
2989 if (ret) {
2990 mlog_errno(ret);
2991 goto out;
2992 }
2993 }
2994
2995out:
2996 ocfs2_free_path(right_path);
2997 ocfs2_free_path(left_path);
2998
2999 return ret;
3000}
3001
Joel Becker70f18c02009-02-13 02:09:31 -08003002static int ocfs2_remove_rightmost_path(handle_t *handle,
3003 struct ocfs2_extent_tree *et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003004 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003005 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003006{
3007 int ret, subtree_index;
3008 u32 cpos;
3009 struct ocfs2_path *left_path = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003010 struct ocfs2_extent_block *eb;
3011 struct ocfs2_extent_list *el;
3012
Joel Becker6136ca52009-02-12 19:32:43 -08003013 ret = ocfs2_et_sanity_check(et);
Tao Mae7d4cb62008-08-18 17:38:44 +08003014 if (ret)
3015 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003016
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003017 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003018 if (ret) {
3019 mlog_errno(ret);
3020 goto out;
3021 }
3022
Joel Becker3d03a302009-02-12 17:49:26 -08003023 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3024 path, &cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003025 if (ret) {
3026 mlog_errno(ret);
3027 goto out;
3028 }
3029
3030 if (cpos) {
3031 /*
3032 * We have a path to the left of this one - it needs
3033 * an update too.
3034 */
Joel Beckerffdd7a52008-10-17 22:32:01 -07003035 left_path = ocfs2_new_path_from_path(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003036 if (!left_path) {
3037 ret = -ENOMEM;
3038 mlog_errno(ret);
3039 goto out;
3040 }
3041
Joel Beckerfacdb772009-02-12 18:08:48 -08003042 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07003043 if (ret) {
3044 mlog_errno(ret);
3045 goto out;
3046 }
3047
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08003048 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003049 if (ret) {
3050 mlog_errno(ret);
3051 goto out;
3052 }
3053
Joel Becker7dc02802009-02-12 19:20:13 -08003054 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003055
Joel Becker6641b0c2009-02-12 18:57:52 -08003056 ocfs2_unlink_subtree(handle, et, left_path, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003057 subtree_index, dealloc);
Jun Piao964f14a2017-09-06 16:19:11 -07003058 ret = ocfs2_update_edge_lengths(handle, et, left_path);
Tao Ma3c5e1062009-07-21 15:42:05 +08003059 if (ret) {
3060 mlog_errno(ret);
3061 goto out;
3062 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003063
3064 eb = (struct ocfs2_extent_block *)path_leaf_bh(left_path)->b_data;
Joel Becker35dc0aa2008-08-20 16:25:06 -07003065 ocfs2_et_set_last_eb_blk(et, le64_to_cpu(eb->h_blkno));
Mark Fasheh328d5752007-06-18 10:48:04 -07003066 } else {
3067 /*
3068 * 'path' is also the leftmost path which
3069 * means it must be the only one. This gets
3070 * handled differently because we want to
Joel Becker70f18c02009-02-13 02:09:31 -08003071 * revert the root back to having extents
Mark Fasheh328d5752007-06-18 10:48:04 -07003072 * in-line.
3073 */
Joel Becker6641b0c2009-02-12 18:57:52 -08003074 ocfs2_unlink_path(handle, et, dealloc, path, 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003075
Joel Beckerce1d9ea2008-08-20 16:30:07 -07003076 el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003077 el->l_tree_depth = 0;
3078 el->l_next_free_rec = 0;
3079 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3080
Joel Becker35dc0aa2008-08-20 16:25:06 -07003081 ocfs2_et_set_last_eb_blk(et, 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003082 }
3083
3084 ocfs2_journal_dirty(handle, path_root_bh(path));
3085
3086out:
3087 ocfs2_free_path(left_path);
3088 return ret;
3089}
3090
Xue jiufeid0c97d52015-09-04 15:44:48 -07003091static int ocfs2_remove_rightmost_empty_extent(struct ocfs2_super *osb,
3092 struct ocfs2_extent_tree *et,
3093 struct ocfs2_path *path,
3094 struct ocfs2_cached_dealloc_ctxt *dealloc)
3095{
3096 handle_t *handle;
3097 int ret;
3098 int credits = path->p_tree_depth * 2 + 1;
3099
3100 handle = ocfs2_start_trans(osb, credits);
3101 if (IS_ERR(handle)) {
3102 ret = PTR_ERR(handle);
3103 mlog_errno(ret);
3104 return ret;
3105 }
3106
3107 ret = ocfs2_remove_rightmost_path(handle, et, path, dealloc);
3108 if (ret)
3109 mlog_errno(ret);
3110
3111 ocfs2_commit_trans(osb, handle);
3112 return ret;
3113}
3114
Mark Fasheh328d5752007-06-18 10:48:04 -07003115/*
3116 * Left rotation of btree records.
3117 *
3118 * In many ways, this is (unsurprisingly) the opposite of right
3119 * rotation. We start at some non-rightmost path containing an empty
3120 * extent in the leaf block. The code works its way to the rightmost
3121 * path by rotating records to the left in every subtree.
3122 *
3123 * This is used by any code which reduces the number of extent records
3124 * in a leaf. After removal, an empty record should be placed in the
3125 * leftmost list position.
3126 *
3127 * This won't handle a length update of the rightmost path records if
3128 * the rightmost tree leaf record is removed so the caller is
3129 * responsible for detecting and correcting that.
3130 */
Joel Becker70f18c02009-02-13 02:09:31 -08003131static int ocfs2_rotate_tree_left(handle_t *handle,
3132 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003133 struct ocfs2_path *path,
Joel Becker70f18c02009-02-13 02:09:31 -08003134 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07003135{
3136 int ret, orig_credits = handle->h_buffer_credits;
3137 struct ocfs2_path *tmp_path = NULL, *restart_path = NULL;
3138 struct ocfs2_extent_block *eb;
3139 struct ocfs2_extent_list *el;
3140
3141 el = path_leaf_el(path);
3142 if (!ocfs2_is_empty_extent(&el->l_recs[0]))
3143 return 0;
3144
3145 if (path->p_tree_depth == 0) {
3146rightmost_no_delete:
3147 /*
Tao Mae7d4cb62008-08-18 17:38:44 +08003148 * Inline extents. This is trivially handled, so do
Mark Fasheh328d5752007-06-18 10:48:04 -07003149 * it up front.
3150 */
Joel Becker70f18c02009-02-13 02:09:31 -08003151 ret = ocfs2_rotate_rightmost_leaf_left(handle, et, path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003152 if (ret)
3153 mlog_errno(ret);
3154 goto out;
3155 }
3156
3157 /*
3158 * Handle rightmost branch now. There's several cases:
3159 * 1) simple rotation leaving records in there. That's trivial.
3160 * 2) rotation requiring a branch delete - there's no more
3161 * records left. Two cases of this:
3162 * a) There are branches to the left.
3163 * b) This is also the leftmost (the only) branch.
3164 *
3165 * 1) is handled via ocfs2_rotate_rightmost_leaf_left()
3166 * 2a) we need the left branch so that we can update it with the unlink
Joel Becker70f18c02009-02-13 02:09:31 -08003167 * 2b) we need to bring the root back to inline extents.
Mark Fasheh328d5752007-06-18 10:48:04 -07003168 */
3169
3170 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
3171 el = &eb->h_list;
3172 if (eb->h_next_leaf_blk == 0) {
3173 /*
3174 * This gets a bit tricky if we're going to delete the
3175 * rightmost path. Get the other cases out of the way
3176 * 1st.
3177 */
3178 if (le16_to_cpu(el->l_next_free_rec) > 1)
3179 goto rightmost_no_delete;
3180
3181 if (le16_to_cpu(el->l_next_free_rec) == 0) {
3182 ret = -EIO;
Joel Becker70f18c02009-02-13 02:09:31 -08003183 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Joe Perches7ecef142015-09-04 15:44:51 -07003184 "Owner %llu has empty extent block at %llu\n",
Joel Becker70f18c02009-02-13 02:09:31 -08003185 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fasheh328d5752007-06-18 10:48:04 -07003186 (unsigned long long)le64_to_cpu(eb->h_blkno));
3187 goto out;
3188 }
3189
3190 /*
3191 * XXX: The caller can not trust "path" any more after
3192 * this as it will have been deleted. What do we do?
3193 *
3194 * In theory the rotate-for-merge code will never get
3195 * here because it'll always ask for a rotate in a
3196 * nonempty list.
3197 */
3198
Joel Becker70f18c02009-02-13 02:09:31 -08003199 ret = ocfs2_remove_rightmost_path(handle, et, path,
3200 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003201 if (ret)
3202 mlog_errno(ret);
3203 goto out;
3204 }
3205
3206 /*
3207 * Now we can loop, remembering the path we get from -EAGAIN
3208 * and restarting from there.
3209 */
3210try_rotate:
Joel Beckere46f74d2009-02-12 19:47:43 -08003211 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits, path,
3212 dealloc, &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003213 if (ret && ret != -EAGAIN) {
3214 mlog_errno(ret);
3215 goto out;
3216 }
3217
3218 while (ret == -EAGAIN) {
3219 tmp_path = restart_path;
3220 restart_path = NULL;
3221
Joel Beckere46f74d2009-02-12 19:47:43 -08003222 ret = __ocfs2_rotate_tree_left(handle, et, orig_credits,
Mark Fasheh328d5752007-06-18 10:48:04 -07003223 tmp_path, dealloc,
Joel Beckere46f74d2009-02-12 19:47:43 -08003224 &restart_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003225 if (ret && ret != -EAGAIN) {
3226 mlog_errno(ret);
3227 goto out;
3228 }
3229
3230 ocfs2_free_path(tmp_path);
3231 tmp_path = NULL;
3232
3233 if (ret == 0)
3234 goto try_rotate;
3235 }
3236
3237out:
3238 ocfs2_free_path(tmp_path);
3239 ocfs2_free_path(restart_path);
3240 return ret;
3241}
3242
3243static void ocfs2_cleanup_merge(struct ocfs2_extent_list *el,
3244 int index)
3245{
3246 struct ocfs2_extent_rec *rec = &el->l_recs[index];
3247 unsigned int size;
3248
3249 if (rec->e_leaf_clusters == 0) {
3250 /*
3251 * We consumed all of the merged-from record. An empty
3252 * extent cannot exist anywhere but the 1st array
3253 * position, so move things over if the merged-from
3254 * record doesn't occupy that position.
3255 *
3256 * This creates a new empty extent so the caller
3257 * should be smart enough to have removed any existing
3258 * ones.
3259 */
3260 if (index > 0) {
3261 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
3262 size = index * sizeof(struct ocfs2_extent_rec);
3263 memmove(&el->l_recs[1], &el->l_recs[0], size);
3264 }
3265
3266 /*
3267 * Always memset - the caller doesn't check whether it
3268 * created an empty extent, so there could be junk in
3269 * the other fields.
3270 */
3271 memset(&el->l_recs[0], 0, sizeof(struct ocfs2_extent_rec));
3272 }
3273}
3274
Joel Becker4fe82c32009-02-13 02:16:08 -08003275static int ocfs2_get_right_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003276 struct ocfs2_path *left_path,
3277 struct ocfs2_path **ret_right_path)
Mark Fasheh328d5752007-06-18 10:48:04 -07003278{
3279 int ret;
Tao Ma677b9752008-01-30 14:21:05 +08003280 u32 right_cpos;
3281 struct ocfs2_path *right_path = NULL;
3282 struct ocfs2_extent_list *left_el;
3283
3284 *ret_right_path = NULL;
3285
3286 /* This function shouldn't be called for non-trees. */
3287 BUG_ON(left_path->p_tree_depth == 0);
3288
3289 left_el = path_leaf_el(left_path);
3290 BUG_ON(left_el->l_next_free_rec != left_el->l_count);
3291
Joel Becker4fe82c32009-02-13 02:16:08 -08003292 ret = ocfs2_find_cpos_for_right_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
3293 left_path, &right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003294 if (ret) {
3295 mlog_errno(ret);
3296 goto out;
3297 }
3298
3299 /* This function shouldn't be called for the rightmost leaf. */
3300 BUG_ON(right_cpos == 0);
3301
Joel Beckerffdd7a52008-10-17 22:32:01 -07003302 right_path = ocfs2_new_path_from_path(left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003303 if (!right_path) {
3304 ret = -ENOMEM;
3305 mlog_errno(ret);
3306 goto out;
3307 }
3308
Joel Becker4fe82c32009-02-13 02:16:08 -08003309 ret = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003310 if (ret) {
3311 mlog_errno(ret);
3312 goto out;
3313 }
3314
3315 *ret_right_path = right_path;
3316out:
3317 if (ret)
3318 ocfs2_free_path(right_path);
3319 return ret;
3320}
3321
3322/*
3323 * Remove split_rec clusters from the record at index and merge them
3324 * onto the beginning of the record "next" to it.
3325 * For index < l_count - 1, the next means the extent rec at index + 1.
3326 * For index == l_count - 1, the "next" means the 1st extent rec of the
3327 * next extent block.
3328 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003329static int ocfs2_merge_rec_right(struct ocfs2_path *left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003330 handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003331 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003332 struct ocfs2_extent_rec *split_rec,
3333 int index)
3334{
3335 int ret, next_free, i;
Mark Fasheh328d5752007-06-18 10:48:04 -07003336 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3337 struct ocfs2_extent_rec *left_rec;
3338 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003339 struct ocfs2_extent_list *right_el;
3340 struct ocfs2_path *right_path = NULL;
3341 int subtree_index = 0;
3342 struct ocfs2_extent_list *el = path_leaf_el(left_path);
3343 struct buffer_head *bh = path_leaf_bh(left_path);
3344 struct buffer_head *root_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07003345
3346 BUG_ON(index >= le16_to_cpu(el->l_next_free_rec));
Mark Fasheh328d5752007-06-18 10:48:04 -07003347 left_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003348
Al Viro9d8df6a2008-05-21 06:32:11 +01003349 if (index == le16_to_cpu(el->l_next_free_rec) - 1 &&
Tao Ma677b9752008-01-30 14:21:05 +08003350 le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count)) {
3351 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003352 ret = ocfs2_get_right_path(et, left_path, &right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003353 if (ret) {
3354 mlog_errno(ret);
Markus Elfring629a3b52015-04-14 15:42:53 -07003355 return ret;
Tao Ma677b9752008-01-30 14:21:05 +08003356 }
3357
3358 right_el = path_leaf_el(right_path);
3359 next_free = le16_to_cpu(right_el->l_next_free_rec);
3360 BUG_ON(next_free <= 0);
3361 right_rec = &right_el->l_recs[0];
3362 if (ocfs2_is_empty_extent(right_rec)) {
Al Viro9d8df6a2008-05-21 06:32:11 +01003363 BUG_ON(next_free <= 1);
Tao Ma677b9752008-01-30 14:21:05 +08003364 right_rec = &right_el->l_recs[1];
3365 }
3366
3367 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3368 le16_to_cpu(left_rec->e_leaf_clusters) !=
3369 le32_to_cpu(right_rec->e_cpos));
3370
Joel Becker7dc02802009-02-12 19:20:13 -08003371 subtree_index = ocfs2_find_subtree_root(et, left_path,
3372 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003373
3374 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3375 handle->h_buffer_credits,
3376 right_path);
3377 if (ret) {
3378 mlog_errno(ret);
3379 goto out;
3380 }
3381
3382 root_bh = left_path->p_node[subtree_index].bh;
3383 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3384
Joel Becker7dc02802009-02-12 19:20:13 -08003385 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003386 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003387 if (ret) {
3388 mlog_errno(ret);
3389 goto out;
3390 }
3391
3392 for (i = subtree_index + 1;
3393 i < path_num_items(right_path); i++) {
Joel Becker7dc02802009-02-12 19:20:13 -08003394 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003395 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003396 if (ret) {
3397 mlog_errno(ret);
3398 goto out;
3399 }
3400
Joel Becker7dc02802009-02-12 19:20:13 -08003401 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003402 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003403 if (ret) {
3404 mlog_errno(ret);
3405 goto out;
3406 }
3407 }
3408
3409 } else {
3410 BUG_ON(index == le16_to_cpu(el->l_next_free_rec) - 1);
3411 right_rec = &el->l_recs[index + 1];
3412 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003413
Joel Becker7dc02802009-02-12 19:20:13 -08003414 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, left_path,
Joel Becker13723d02008-10-17 19:25:01 -07003415 path_num_items(left_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003416 if (ret) {
3417 mlog_errno(ret);
3418 goto out;
3419 }
3420
3421 le16_add_cpu(&left_rec->e_leaf_clusters, -split_clusters);
3422
3423 le32_add_cpu(&right_rec->e_cpos, -split_clusters);
3424 le64_add_cpu(&right_rec->e_blkno,
Joel Becker7dc02802009-02-12 19:20:13 -08003425 -ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3426 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003427 le16_add_cpu(&right_rec->e_leaf_clusters, split_clusters);
3428
3429 ocfs2_cleanup_merge(el, index);
3430
Joel Beckerec20cec2010-03-19 14:13:52 -07003431 ocfs2_journal_dirty(handle, bh);
Tao Ma677b9752008-01-30 14:21:05 +08003432 if (right_path) {
Joel Beckerec20cec2010-03-19 14:13:52 -07003433 ocfs2_journal_dirty(handle, path_leaf_bh(right_path));
Joel Becker4619c732009-02-12 19:02:36 -08003434 ocfs2_complete_edge_insert(handle, left_path, right_path,
3435 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003436 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003437out:
Markus Elfringfd90d4df2015-04-14 15:42:42 -07003438 ocfs2_free_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003439 return ret;
3440}
3441
Joel Becker4fe82c32009-02-13 02:16:08 -08003442static int ocfs2_get_left_path(struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003443 struct ocfs2_path *right_path,
3444 struct ocfs2_path **ret_left_path)
3445{
3446 int ret;
3447 u32 left_cpos;
3448 struct ocfs2_path *left_path = NULL;
3449
3450 *ret_left_path = NULL;
3451
3452 /* This function shouldn't be called for non-trees. */
3453 BUG_ON(right_path->p_tree_depth == 0);
3454
Joel Becker4fe82c32009-02-13 02:16:08 -08003455 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
Tao Ma677b9752008-01-30 14:21:05 +08003456 right_path, &left_cpos);
3457 if (ret) {
3458 mlog_errno(ret);
3459 goto out;
3460 }
3461
3462 /* This function shouldn't be called for the leftmost leaf. */
3463 BUG_ON(left_cpos == 0);
3464
Joel Beckerffdd7a52008-10-17 22:32:01 -07003465 left_path = ocfs2_new_path_from_path(right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003466 if (!left_path) {
3467 ret = -ENOMEM;
3468 mlog_errno(ret);
3469 goto out;
3470 }
3471
Joel Becker4fe82c32009-02-13 02:16:08 -08003472 ret = ocfs2_find_path(et->et_ci, left_path, left_cpos);
Tao Ma677b9752008-01-30 14:21:05 +08003473 if (ret) {
3474 mlog_errno(ret);
3475 goto out;
3476 }
3477
3478 *ret_left_path = left_path;
3479out:
3480 if (ret)
3481 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003482 return ret;
3483}
3484
3485/*
3486 * Remove split_rec clusters from the record at index and merge them
Tao Ma677b9752008-01-30 14:21:05 +08003487 * onto the tail of the record "before" it.
3488 * For index > 0, the "before" means the extent rec at index - 1.
3489 *
3490 * For index == 0, the "before" means the last record of the previous
3491 * extent block. And there is also a situation that we may need to
3492 * remove the rightmost leaf extent block in the right_path and change
3493 * the right path to indicate the new rightmost path.
Mark Fasheh328d5752007-06-18 10:48:04 -07003494 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003495static int ocfs2_merge_rec_left(struct ocfs2_path *right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003496 handle_t *handle,
Joel Becker4fe82c32009-02-13 02:16:08 -08003497 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003498 struct ocfs2_extent_rec *split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003499 struct ocfs2_cached_dealloc_ctxt *dealloc,
3500 int index)
Mark Fasheh328d5752007-06-18 10:48:04 -07003501{
Tao Ma677b9752008-01-30 14:21:05 +08003502 int ret, i, subtree_index = 0, has_empty_extent = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07003503 unsigned int split_clusters = le16_to_cpu(split_rec->e_leaf_clusters);
3504 struct ocfs2_extent_rec *left_rec;
3505 struct ocfs2_extent_rec *right_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003506 struct ocfs2_extent_list *el = path_leaf_el(right_path);
3507 struct buffer_head *bh = path_leaf_bh(right_path);
3508 struct buffer_head *root_bh = NULL;
3509 struct ocfs2_path *left_path = NULL;
3510 struct ocfs2_extent_list *left_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07003511
Tao Ma677b9752008-01-30 14:21:05 +08003512 BUG_ON(index < 0);
Mark Fasheh328d5752007-06-18 10:48:04 -07003513
Mark Fasheh328d5752007-06-18 10:48:04 -07003514 right_rec = &el->l_recs[index];
Tao Ma677b9752008-01-30 14:21:05 +08003515 if (index == 0) {
3516 /* we meet with a cross extent block merge. */
Joel Becker4fe82c32009-02-13 02:16:08 -08003517 ret = ocfs2_get_left_path(et, right_path, &left_path);
Tao Ma677b9752008-01-30 14:21:05 +08003518 if (ret) {
3519 mlog_errno(ret);
Markus Elfring992ef6e2015-04-14 15:42:51 -07003520 return ret;
Tao Ma677b9752008-01-30 14:21:05 +08003521 }
3522
3523 left_el = path_leaf_el(left_path);
3524 BUG_ON(le16_to_cpu(left_el->l_next_free_rec) !=
3525 le16_to_cpu(left_el->l_count));
3526
3527 left_rec = &left_el->l_recs[
3528 le16_to_cpu(left_el->l_next_free_rec) - 1];
3529 BUG_ON(le32_to_cpu(left_rec->e_cpos) +
3530 le16_to_cpu(left_rec->e_leaf_clusters) !=
3531 le32_to_cpu(split_rec->e_cpos));
3532
Joel Becker7dc02802009-02-12 19:20:13 -08003533 subtree_index = ocfs2_find_subtree_root(et, left_path,
3534 right_path);
Tao Ma677b9752008-01-30 14:21:05 +08003535
3536 ret = ocfs2_extend_rotate_transaction(handle, subtree_index,
3537 handle->h_buffer_credits,
3538 left_path);
3539 if (ret) {
3540 mlog_errno(ret);
3541 goto out;
3542 }
3543
3544 root_bh = left_path->p_node[subtree_index].bh;
3545 BUG_ON(root_bh != right_path->p_node[subtree_index].bh);
3546
Joel Becker4fe82c32009-02-13 02:16:08 -08003547 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Joel Becker13723d02008-10-17 19:25:01 -07003548 subtree_index);
Tao Ma677b9752008-01-30 14:21:05 +08003549 if (ret) {
3550 mlog_errno(ret);
3551 goto out;
3552 }
3553
3554 for (i = subtree_index + 1;
3555 i < path_num_items(right_path); i++) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003556 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003557 right_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003558 if (ret) {
3559 mlog_errno(ret);
3560 goto out;
3561 }
3562
Joel Becker4fe82c32009-02-13 02:16:08 -08003563 ret = ocfs2_path_bh_journal_access(handle, et->et_ci,
Joel Becker13723d02008-10-17 19:25:01 -07003564 left_path, i);
Tao Ma677b9752008-01-30 14:21:05 +08003565 if (ret) {
3566 mlog_errno(ret);
3567 goto out;
3568 }
3569 }
3570 } else {
3571 left_rec = &el->l_recs[index - 1];
3572 if (ocfs2_is_empty_extent(&el->l_recs[0]))
3573 has_empty_extent = 1;
3574 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003575
Joel Becker4fe82c32009-02-13 02:16:08 -08003576 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, right_path,
Tao Ma9047bea2009-01-05 14:45:24 +08003577 path_num_items(right_path) - 1);
Mark Fasheh328d5752007-06-18 10:48:04 -07003578 if (ret) {
3579 mlog_errno(ret);
3580 goto out;
3581 }
3582
3583 if (has_empty_extent && index == 1) {
3584 /*
3585 * The easy case - we can just plop the record right in.
3586 */
3587 *left_rec = *split_rec;
Tao Ma677b9752008-01-30 14:21:05 +08003588 } else
Mark Fasheh328d5752007-06-18 10:48:04 -07003589 le16_add_cpu(&left_rec->e_leaf_clusters, split_clusters);
Mark Fasheh328d5752007-06-18 10:48:04 -07003590
3591 le32_add_cpu(&right_rec->e_cpos, split_clusters);
3592 le64_add_cpu(&right_rec->e_blkno,
Joel Becker4fe82c32009-02-13 02:16:08 -08003593 ocfs2_clusters_to_blocks(ocfs2_metadata_cache_get_super(et->et_ci),
3594 split_clusters));
Mark Fasheh328d5752007-06-18 10:48:04 -07003595 le16_add_cpu(&right_rec->e_leaf_clusters, -split_clusters);
3596
3597 ocfs2_cleanup_merge(el, index);
3598
Joel Beckerec20cec2010-03-19 14:13:52 -07003599 ocfs2_journal_dirty(handle, bh);
Tao Ma677b9752008-01-30 14:21:05 +08003600 if (left_path) {
Joel Beckerec20cec2010-03-19 14:13:52 -07003601 ocfs2_journal_dirty(handle, path_leaf_bh(left_path));
Tao Ma677b9752008-01-30 14:21:05 +08003602
3603 /*
3604 * In the situation that the right_rec is empty and the extent
3605 * block is empty also, ocfs2_complete_edge_insert can't handle
3606 * it and we need to delete the right extent block.
3607 */
3608 if (le16_to_cpu(right_rec->e_leaf_clusters) == 0 &&
3609 le16_to_cpu(el->l_next_free_rec) == 1) {
Xue jiufei17215982016-03-25 14:21:41 -07003610 /* extend credit for ocfs2_remove_rightmost_path */
3611 ret = ocfs2_extend_rotate_transaction(handle, 0,
3612 handle->h_buffer_credits,
3613 right_path);
3614 if (ret) {
3615 mlog_errno(ret);
3616 goto out;
3617 }
Tao Ma677b9752008-01-30 14:21:05 +08003618
Joel Becker70f18c02009-02-13 02:09:31 -08003619 ret = ocfs2_remove_rightmost_path(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08003620 right_path,
Joel Becker70f18c02009-02-13 02:09:31 -08003621 dealloc);
Tao Ma677b9752008-01-30 14:21:05 +08003622 if (ret) {
3623 mlog_errno(ret);
3624 goto out;
3625 }
3626
3627 /* Now the rightmost extent block has been deleted.
3628 * So we use the new rightmost path.
3629 */
3630 ocfs2_mv_path(right_path, left_path);
3631 left_path = NULL;
3632 } else
Joel Becker4619c732009-02-12 19:02:36 -08003633 ocfs2_complete_edge_insert(handle, left_path,
Tao Ma677b9752008-01-30 14:21:05 +08003634 right_path, subtree_index);
3635 }
Mark Fasheh328d5752007-06-18 10:48:04 -07003636out:
Markus Elfringfd90d4df2015-04-14 15:42:42 -07003637 ocfs2_free_path(left_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003638 return ret;
3639}
3640
Joel Beckerc495dd22009-02-13 02:19:11 -08003641static int ocfs2_try_to_merge_extent(handle_t *handle,
3642 struct ocfs2_extent_tree *et,
Tao Ma677b9752008-01-30 14:21:05 +08003643 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07003644 int split_index,
3645 struct ocfs2_extent_rec *split_rec,
3646 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Beckerc495dd22009-02-13 02:19:11 -08003647 struct ocfs2_merge_ctxt *ctxt)
Mark Fasheh328d5752007-06-18 10:48:04 -07003648{
Tao Mao518d7262007-08-28 17:25:35 -07003649 int ret = 0;
Tao Ma677b9752008-01-30 14:21:05 +08003650 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fasheh328d5752007-06-18 10:48:04 -07003651 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
3652
3653 BUG_ON(ctxt->c_contig_type == CONTIG_NONE);
3654
Tao Mao518d7262007-08-28 17:25:35 -07003655 if (ctxt->c_split_covers_rec && ctxt->c_has_empty_extent) {
Xue jiufei17215982016-03-25 14:21:41 -07003656 /* extend credit for ocfs2_remove_rightmost_path */
3657 ret = ocfs2_extend_rotate_transaction(handle, 0,
3658 handle->h_buffer_credits,
3659 path);
3660 if (ret) {
3661 mlog_errno(ret);
3662 goto out;
3663 }
Tao Mao518d7262007-08-28 17:25:35 -07003664 /*
3665 * The merge code will need to create an empty
3666 * extent to take the place of the newly
3667 * emptied slot. Remove any pre-existing empty
3668 * extents - having more than one in a leaf is
3669 * illegal.
3670 */
Joel Becker70f18c02009-02-13 02:09:31 -08003671 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Tao Mao518d7262007-08-28 17:25:35 -07003672 if (ret) {
3673 mlog_errno(ret);
3674 goto out;
Mark Fasheh328d5752007-06-18 10:48:04 -07003675 }
Tao Mao518d7262007-08-28 17:25:35 -07003676 split_index--;
3677 rec = &el->l_recs[split_index];
Mark Fasheh328d5752007-06-18 10:48:04 -07003678 }
3679
3680 if (ctxt->c_contig_type == CONTIG_LEFTRIGHT) {
3681 /*
3682 * Left-right contig implies this.
3683 */
3684 BUG_ON(!ctxt->c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07003685
3686 /*
3687 * Since the leftright insert always covers the entire
3688 * extent, this call will delete the insert record
3689 * entirely, resulting in an empty extent record added to
3690 * the extent block.
3691 *
3692 * Since the adding of an empty extent shifts
3693 * everything back to the right, there's no need to
3694 * update split_index here.
Tao Ma677b9752008-01-30 14:21:05 +08003695 *
3696 * When the split_index is zero, we need to merge it to the
3697 * prevoius extent block. It is more efficient and easier
3698 * if we do merge_right first and merge_left later.
Mark Fasheh328d5752007-06-18 10:48:04 -07003699 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003700 ret = ocfs2_merge_rec_right(path, handle, et, split_rec,
Tao Ma677b9752008-01-30 14:21:05 +08003701 split_index);
Mark Fasheh328d5752007-06-18 10:48:04 -07003702 if (ret) {
3703 mlog_errno(ret);
3704 goto out;
3705 }
3706
3707 /*
3708 * We can only get this from logic error above.
3709 */
3710 BUG_ON(!ocfs2_is_empty_extent(&el->l_recs[0]));
3711
Xue jiufei17215982016-03-25 14:21:41 -07003712 /* extend credit for ocfs2_remove_rightmost_path */
3713 ret = ocfs2_extend_rotate_transaction(handle, 0,
3714 handle->h_buffer_credits,
3715 path);
3716 if (ret) {
3717 mlog_errno(ret);
3718 goto out;
3719 }
3720
Tao Ma677b9752008-01-30 14:21:05 +08003721 /* The merge left us with an empty extent, remove it. */
Joel Becker70f18c02009-02-13 02:09:31 -08003722 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003723 if (ret) {
3724 mlog_errno(ret);
3725 goto out;
3726 }
Tao Ma677b9752008-01-30 14:21:05 +08003727
Mark Fasheh328d5752007-06-18 10:48:04 -07003728 rec = &el->l_recs[split_index];
3729
3730 /*
3731 * Note that we don't pass split_rec here on purpose -
Tao Ma677b9752008-01-30 14:21:05 +08003732 * we've merged it into the rec already.
Mark Fasheh328d5752007-06-18 10:48:04 -07003733 */
Joel Becker4fe82c32009-02-13 02:16:08 -08003734 ret = ocfs2_merge_rec_left(path, handle, et, rec,
3735 dealloc, split_index);
Tao Ma677b9752008-01-30 14:21:05 +08003736
Mark Fasheh328d5752007-06-18 10:48:04 -07003737 if (ret) {
3738 mlog_errno(ret);
3739 goto out;
3740 }
3741
Xue jiufei17215982016-03-25 14:21:41 -07003742 /* extend credit for ocfs2_remove_rightmost_path */
3743 ret = ocfs2_extend_rotate_transaction(handle, 0,
3744 handle->h_buffer_credits,
3745 path);
3746 if (ret) {
3747 mlog_errno(ret);
3748 goto out;
3749 }
3750
Joel Becker70f18c02009-02-13 02:09:31 -08003751 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003752 /*
3753 * Error from this last rotate is not critical, so
3754 * print but don't bubble it up.
3755 */
3756 if (ret)
3757 mlog_errno(ret);
3758 ret = 0;
3759 } else {
3760 /*
3761 * Merge a record to the left or right.
3762 *
3763 * 'contig_type' is relative to the existing record,
3764 * so for example, if we're "right contig", it's to
3765 * the record on the left (hence the left merge).
3766 */
3767 if (ctxt->c_contig_type == CONTIG_RIGHT) {
Joel Becker4fe82c32009-02-13 02:16:08 -08003768 ret = ocfs2_merge_rec_left(path, handle, et,
3769 split_rec, dealloc,
Mark Fasheh328d5752007-06-18 10:48:04 -07003770 split_index);
3771 if (ret) {
3772 mlog_errno(ret);
3773 goto out;
3774 }
3775 } else {
Joel Becker4fe82c32009-02-13 02:16:08 -08003776 ret = ocfs2_merge_rec_right(path, handle,
Joel Becker7dc02802009-02-12 19:20:13 -08003777 et, split_rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003778 split_index);
3779 if (ret) {
3780 mlog_errno(ret);
3781 goto out;
3782 }
3783 }
3784
3785 if (ctxt->c_split_covers_rec) {
Xue jiufei17215982016-03-25 14:21:41 -07003786 /* extend credit for ocfs2_remove_rightmost_path */
3787 ret = ocfs2_extend_rotate_transaction(handle, 0,
3788 handle->h_buffer_credits,
3789 path);
3790 if (ret) {
3791 mlog_errno(ret);
3792 ret = 0;
3793 goto out;
3794 }
3795
Mark Fasheh328d5752007-06-18 10:48:04 -07003796 /*
3797 * The merge may have left an empty extent in
3798 * our leaf. Try to rotate it away.
3799 */
Joel Becker70f18c02009-02-13 02:09:31 -08003800 ret = ocfs2_rotate_tree_left(handle, et, path,
3801 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07003802 if (ret)
3803 mlog_errno(ret);
3804 ret = 0;
3805 }
3806 }
3807
3808out:
3809 return ret;
3810}
3811
3812static void ocfs2_subtract_from_rec(struct super_block *sb,
3813 enum ocfs2_split_type split,
3814 struct ocfs2_extent_rec *rec,
3815 struct ocfs2_extent_rec *split_rec)
3816{
3817 u64 len_blocks;
3818
3819 len_blocks = ocfs2_clusters_to_blocks(sb,
3820 le16_to_cpu(split_rec->e_leaf_clusters));
3821
3822 if (split == SPLIT_LEFT) {
3823 /*
3824 * Region is on the left edge of the existing
3825 * record.
3826 */
3827 le32_add_cpu(&rec->e_cpos,
3828 le16_to_cpu(split_rec->e_leaf_clusters));
3829 le64_add_cpu(&rec->e_blkno, len_blocks);
3830 le16_add_cpu(&rec->e_leaf_clusters,
3831 -le16_to_cpu(split_rec->e_leaf_clusters));
3832 } else {
3833 /*
3834 * Region is on the right edge of the existing
3835 * record.
3836 */
3837 le16_add_cpu(&rec->e_leaf_clusters,
3838 -le16_to_cpu(split_rec->e_leaf_clusters));
3839 }
3840}
3841
Mark Fashehdcd05382007-01-16 11:32:23 -08003842/*
3843 * Do the final bits of extent record insertion at the target leaf
3844 * list. If this leaf is part of an allocation tree, it is assumed
3845 * that the tree above has been prepared.
3846 */
Joel Beckerd5628622009-02-13 02:54:36 -08003847static void ocfs2_insert_at_leaf(struct ocfs2_extent_tree *et,
3848 struct ocfs2_extent_rec *insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08003849 struct ocfs2_extent_list *el,
Joel Beckerd5628622009-02-13 02:54:36 -08003850 struct ocfs2_insert_type *insert)
Mark Fashehdcd05382007-01-16 11:32:23 -08003851{
3852 int i = insert->ins_contig_index;
3853 unsigned int range;
3854 struct ocfs2_extent_rec *rec;
3855
Mark Fashehe48edee2007-03-07 16:46:57 -08003856 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08003857
Mark Fasheh328d5752007-06-18 10:48:04 -07003858 if (insert->ins_split != SPLIT_NONE) {
3859 i = ocfs2_search_extent_list(el, le32_to_cpu(insert_rec->e_cpos));
3860 BUG_ON(i == -1);
3861 rec = &el->l_recs[i];
Joel Beckerd5628622009-02-13 02:54:36 -08003862 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
3863 insert->ins_split, rec,
Mark Fasheh328d5752007-06-18 10:48:04 -07003864 insert_rec);
3865 goto rotate;
3866 }
3867
Mark Fashehdcd05382007-01-16 11:32:23 -08003868 /*
3869 * Contiguous insert - either left or right.
3870 */
3871 if (insert->ins_contig != CONTIG_NONE) {
3872 rec = &el->l_recs[i];
3873 if (insert->ins_contig == CONTIG_LEFT) {
3874 rec->e_blkno = insert_rec->e_blkno;
3875 rec->e_cpos = insert_rec->e_cpos;
3876 }
Mark Fashehe48edee2007-03-07 16:46:57 -08003877 le16_add_cpu(&rec->e_leaf_clusters,
3878 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003879 return;
3880 }
3881
3882 /*
3883 * Handle insert into an empty leaf.
3884 */
3885 if (le16_to_cpu(el->l_next_free_rec) == 0 ||
3886 ((le16_to_cpu(el->l_next_free_rec) == 1) &&
3887 ocfs2_is_empty_extent(&el->l_recs[0]))) {
3888 el->l_recs[0] = *insert_rec;
3889 el->l_next_free_rec = cpu_to_le16(1);
3890 return;
3891 }
3892
3893 /*
3894 * Appending insert.
3895 */
3896 if (insert->ins_appending == APPEND_TAIL) {
3897 i = le16_to_cpu(el->l_next_free_rec) - 1;
3898 rec = &el->l_recs[i];
Mark Fashehe48edee2007-03-07 16:46:57 -08003899 range = le32_to_cpu(rec->e_cpos)
3900 + le16_to_cpu(rec->e_leaf_clusters);
Mark Fashehdcd05382007-01-16 11:32:23 -08003901 BUG_ON(le32_to_cpu(insert_rec->e_cpos) < range);
3902
3903 mlog_bug_on_msg(le16_to_cpu(el->l_next_free_rec) >=
3904 le16_to_cpu(el->l_count),
Joel Beckerd5628622009-02-13 02:54:36 -08003905 "owner %llu, depth %u, count %u, next free %u, "
Mark Fashehdcd05382007-01-16 11:32:23 -08003906 "rec.cpos %u, rec.clusters %u, "
3907 "insert.cpos %u, insert.clusters %u\n",
Joel Beckerd5628622009-02-13 02:54:36 -08003908 ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehdcd05382007-01-16 11:32:23 -08003909 le16_to_cpu(el->l_tree_depth),
3910 le16_to_cpu(el->l_count),
3911 le16_to_cpu(el->l_next_free_rec),
3912 le32_to_cpu(el->l_recs[i].e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003913 le16_to_cpu(el->l_recs[i].e_leaf_clusters),
Mark Fashehdcd05382007-01-16 11:32:23 -08003914 le32_to_cpu(insert_rec->e_cpos),
Mark Fashehe48edee2007-03-07 16:46:57 -08003915 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08003916 i++;
3917 el->l_recs[i] = *insert_rec;
3918 le16_add_cpu(&el->l_next_free_rec, 1);
3919 return;
3920 }
3921
Mark Fasheh328d5752007-06-18 10:48:04 -07003922rotate:
Mark Fashehdcd05382007-01-16 11:32:23 -08003923 /*
3924 * Ok, we have to rotate.
3925 *
3926 * At this point, it is safe to assume that inserting into an
3927 * empty leaf and appending to a leaf have both been handled
3928 * above.
3929 *
3930 * This leaf needs to have space, either by the empty 1st
3931 * extent record, or by virtue of an l_next_rec < l_count.
3932 */
3933 ocfs2_rotate_leaf(el, insert_rec);
3934}
3935
Joel Beckerd401dc12009-02-13 02:24:10 -08003936static void ocfs2_adjust_rightmost_records(handle_t *handle,
3937 struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07003938 struct ocfs2_path *path,
3939 struct ocfs2_extent_rec *insert_rec)
3940{
3941 int ret, i, next_free;
3942 struct buffer_head *bh;
3943 struct ocfs2_extent_list *el;
3944 struct ocfs2_extent_rec *rec;
3945
3946 /*
3947 * Update everything except the leaf block.
3948 */
3949 for (i = 0; i < path->p_tree_depth; i++) {
3950 bh = path->p_node[i].bh;
3951 el = path->p_node[i].el;
3952
3953 next_free = le16_to_cpu(el->l_next_free_rec);
3954 if (next_free == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08003955 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Joe Perches7ecef142015-09-04 15:44:51 -07003956 "Owner %llu has a bad extent list\n",
Joel Beckerd401dc12009-02-13 02:24:10 -08003957 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci));
Mark Fasheh328d5752007-06-18 10:48:04 -07003958 ret = -EIO;
3959 return;
3960 }
3961
3962 rec = &el->l_recs[next_free - 1];
3963
3964 rec->e_int_clusters = insert_rec->e_cpos;
3965 le32_add_cpu(&rec->e_int_clusters,
3966 le16_to_cpu(insert_rec->e_leaf_clusters));
3967 le32_add_cpu(&rec->e_int_clusters,
3968 -le32_to_cpu(rec->e_cpos));
3969
Joel Beckerec20cec2010-03-19 14:13:52 -07003970 ocfs2_journal_dirty(handle, bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07003971 }
3972}
3973
Joel Beckerd401dc12009-02-13 02:24:10 -08003974static int ocfs2_append_rec_to_path(handle_t *handle,
3975 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08003976 struct ocfs2_extent_rec *insert_rec,
3977 struct ocfs2_path *right_path,
3978 struct ocfs2_path **ret_left_path)
3979{
Mark Fasheh328d5752007-06-18 10:48:04 -07003980 int ret, next_free;
Mark Fashehdcd05382007-01-16 11:32:23 -08003981 struct ocfs2_extent_list *el;
3982 struct ocfs2_path *left_path = NULL;
3983
3984 *ret_left_path = NULL;
3985
3986 /*
Mark Fashehe48edee2007-03-07 16:46:57 -08003987 * This shouldn't happen for non-trees. The extent rec cluster
3988 * count manipulation below only works for interior nodes.
3989 */
3990 BUG_ON(right_path->p_tree_depth == 0);
3991
3992 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08003993 * If our appending insert is at the leftmost edge of a leaf,
3994 * then we might need to update the rightmost records of the
3995 * neighboring path.
3996 */
3997 el = path_leaf_el(right_path);
3998 next_free = le16_to_cpu(el->l_next_free_rec);
3999 if (next_free == 0 ||
4000 (next_free == 1 && ocfs2_is_empty_extent(&el->l_recs[0]))) {
4001 u32 left_cpos;
4002
Joel Beckerd401dc12009-02-13 02:24:10 -08004003 ret = ocfs2_find_cpos_for_left_leaf(ocfs2_metadata_cache_get_super(et->et_ci),
4004 right_path, &left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004005 if (ret) {
4006 mlog_errno(ret);
4007 goto out;
4008 }
4009
Tao Maa09d09b2011-02-22 08:21:10 +08004010 trace_ocfs2_append_rec_to_path(
4011 (unsigned long long)
4012 ocfs2_metadata_cache_owner(et->et_ci),
4013 le32_to_cpu(insert_rec->e_cpos),
4014 left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004015
4016 /*
4017 * No need to worry if the append is already in the
4018 * leftmost leaf.
4019 */
4020 if (left_cpos) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004021 left_path = ocfs2_new_path_from_path(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004022 if (!left_path) {
4023 ret = -ENOMEM;
4024 mlog_errno(ret);
4025 goto out;
4026 }
4027
Joel Beckerd401dc12009-02-13 02:24:10 -08004028 ret = ocfs2_find_path(et->et_ci, left_path,
Joel Beckerfacdb772009-02-12 18:08:48 -08004029 left_cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004030 if (ret) {
4031 mlog_errno(ret);
4032 goto out;
4033 }
4034
4035 /*
4036 * ocfs2_insert_path() will pass the left_path to the
4037 * journal for us.
4038 */
4039 }
4040 }
4041
Joel Beckerd401dc12009-02-13 02:24:10 -08004042 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004043 if (ret) {
4044 mlog_errno(ret);
4045 goto out;
4046 }
4047
Joel Beckerd401dc12009-02-13 02:24:10 -08004048 ocfs2_adjust_rightmost_records(handle, et, right_path, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004049
4050 *ret_left_path = left_path;
4051 ret = 0;
4052out:
4053 if (ret != 0)
4054 ocfs2_free_path(left_path);
4055
4056 return ret;
4057}
4058
Joel Beckerc38e52b2009-02-13 02:56:23 -08004059static void ocfs2_split_record(struct ocfs2_extent_tree *et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004060 struct ocfs2_path *left_path,
4061 struct ocfs2_path *right_path,
4062 struct ocfs2_extent_rec *split_rec,
4063 enum ocfs2_split_type split)
4064{
4065 int index;
4066 u32 cpos = le32_to_cpu(split_rec->e_cpos);
4067 struct ocfs2_extent_list *left_el = NULL, *right_el, *insert_el, *el;
4068 struct ocfs2_extent_rec *rec, *tmprec;
4069
Fernando Carrijoc19a28e2009-01-07 18:09:08 -08004070 right_el = path_leaf_el(right_path);
Mark Fasheh328d5752007-06-18 10:48:04 -07004071 if (left_path)
4072 left_el = path_leaf_el(left_path);
4073
4074 el = right_el;
4075 insert_el = right_el;
4076 index = ocfs2_search_extent_list(el, cpos);
4077 if (index != -1) {
4078 if (index == 0 && left_path) {
4079 BUG_ON(ocfs2_is_empty_extent(&el->l_recs[0]));
4080
4081 /*
4082 * This typically means that the record
4083 * started in the left path but moved to the
4084 * right as a result of rotation. We either
4085 * move the existing record to the left, or we
4086 * do the later insert there.
4087 *
4088 * In this case, the left path should always
4089 * exist as the rotate code will have passed
4090 * it back for a post-insert update.
4091 */
4092
4093 if (split == SPLIT_LEFT) {
4094 /*
4095 * It's a left split. Since we know
4096 * that the rotate code gave us an
4097 * empty extent in the left path, we
4098 * can just do the insert there.
4099 */
4100 insert_el = left_el;
4101 } else {
4102 /*
4103 * Right split - we have to move the
4104 * existing record over to the left
4105 * leaf. The insert will be into the
4106 * newly created empty extent in the
4107 * right leaf.
4108 */
4109 tmprec = &right_el->l_recs[index];
4110 ocfs2_rotate_leaf(left_el, tmprec);
4111 el = left_el;
4112
4113 memset(tmprec, 0, sizeof(*tmprec));
4114 index = ocfs2_search_extent_list(left_el, cpos);
4115 BUG_ON(index == -1);
4116 }
4117 }
4118 } else {
4119 BUG_ON(!left_path);
4120 BUG_ON(!ocfs2_is_empty_extent(&left_el->l_recs[0]));
4121 /*
4122 * Left path is easy - we can just allow the insert to
4123 * happen.
4124 */
4125 el = left_el;
4126 insert_el = left_el;
4127 index = ocfs2_search_extent_list(el, cpos);
4128 BUG_ON(index == -1);
4129 }
4130
4131 rec = &el->l_recs[index];
Joel Beckerc38e52b2009-02-13 02:56:23 -08004132 ocfs2_subtract_from_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4133 split, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004134 ocfs2_rotate_leaf(insert_el, split_rec);
4135}
4136
Mark Fashehdcd05382007-01-16 11:32:23 -08004137/*
Tao Mae7d4cb62008-08-18 17:38:44 +08004138 * This function only does inserts on an allocation b-tree. For tree
4139 * depth = 0, ocfs2_insert_at_leaf() is called directly.
Mark Fashehdcd05382007-01-16 11:32:23 -08004140 *
4141 * right_path is the path we want to do the actual insert
4142 * in. left_path should only be passed in if we need to update that
4143 * portion of the tree after an edge insert.
4144 */
Joel Becker3505bec2009-02-13 02:57:58 -08004145static int ocfs2_insert_path(handle_t *handle,
Joel Becker7dc02802009-02-12 19:20:13 -08004146 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004147 struct ocfs2_path *left_path,
4148 struct ocfs2_path *right_path,
4149 struct ocfs2_extent_rec *insert_rec,
4150 struct ocfs2_insert_type *insert)
4151{
4152 int ret, subtree_index;
4153 struct buffer_head *leaf_bh = path_leaf_bh(right_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004154
Mark Fashehdcd05382007-01-16 11:32:23 -08004155 if (left_path) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004156 /*
4157 * There's a chance that left_path got passed back to
4158 * us without being accounted for in the
4159 * journal. Extend our transaction here to be sure we
4160 * can change those blocks.
4161 */
Tao Mac901fb02010-04-26 14:34:57 +08004162 ret = ocfs2_extend_trans(handle, left_path->p_tree_depth);
Mark Fashehdcd05382007-01-16 11:32:23 -08004163 if (ret < 0) {
4164 mlog_errno(ret);
4165 goto out;
4166 }
4167
Joel Becker7dc02802009-02-12 19:20:13 -08004168 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehdcd05382007-01-16 11:32:23 -08004169 if (ret < 0) {
4170 mlog_errno(ret);
4171 goto out;
4172 }
4173 }
4174
Mark Fashehe8aed342007-12-03 16:43:01 -08004175 /*
4176 * Pass both paths to the journal. The majority of inserts
4177 * will be touching all components anyway.
4178 */
Joel Becker7dc02802009-02-12 19:20:13 -08004179 ret = ocfs2_journal_access_path(et->et_ci, handle, right_path);
Mark Fashehe8aed342007-12-03 16:43:01 -08004180 if (ret < 0) {
4181 mlog_errno(ret);
4182 goto out;
4183 }
4184
Mark Fasheh328d5752007-06-18 10:48:04 -07004185 if (insert->ins_split != SPLIT_NONE) {
4186 /*
4187 * We could call ocfs2_insert_at_leaf() for some types
Joe Perchesc78bad12008-02-03 17:33:42 +02004188 * of splits, but it's easier to just let one separate
Mark Fasheh328d5752007-06-18 10:48:04 -07004189 * function sort it all out.
4190 */
Joel Beckerc38e52b2009-02-13 02:56:23 -08004191 ocfs2_split_record(et, left_path, right_path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004192 insert_rec, insert->ins_split);
Mark Fashehe8aed342007-12-03 16:43:01 -08004193
4194 /*
4195 * Split might have modified either leaf and we don't
4196 * have a guarantee that the later edge insert will
4197 * dirty this for us.
4198 */
4199 if (left_path)
Joel Beckerec20cec2010-03-19 14:13:52 -07004200 ocfs2_journal_dirty(handle,
4201 path_leaf_bh(left_path));
Mark Fasheh328d5752007-06-18 10:48:04 -07004202 } else
Joel Beckerd5628622009-02-13 02:54:36 -08004203 ocfs2_insert_at_leaf(et, insert_rec, path_leaf_el(right_path),
4204 insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004205
Joel Beckerec20cec2010-03-19 14:13:52 -07004206 ocfs2_journal_dirty(handle, leaf_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004207
4208 if (left_path) {
4209 /*
4210 * The rotate code has indicated that we need to fix
4211 * up portions of the tree after the insert.
4212 *
4213 * XXX: Should we extend the transaction here?
4214 */
Joel Becker7dc02802009-02-12 19:20:13 -08004215 subtree_index = ocfs2_find_subtree_root(et, left_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004216 right_path);
Joel Becker4619c732009-02-12 19:02:36 -08004217 ocfs2_complete_edge_insert(handle, left_path, right_path,
4218 subtree_index);
Mark Fashehdcd05382007-01-16 11:32:23 -08004219 }
4220
4221 ret = 0;
4222out:
4223 return ret;
4224}
4225
Joel Becker3505bec2009-02-13 02:57:58 -08004226static int ocfs2_do_insert_extent(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004227 struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004228 struct ocfs2_extent_rec *insert_rec,
4229 struct ocfs2_insert_type *type)
4230{
4231 int ret, rotate = 0;
4232 u32 cpos;
4233 struct ocfs2_path *right_path = NULL;
4234 struct ocfs2_path *left_path = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004235 struct ocfs2_extent_list *el;
4236
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004237 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004238
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004239 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004240 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehdcd05382007-01-16 11:32:23 -08004241 if (ret) {
4242 mlog_errno(ret);
4243 goto out;
4244 }
4245
4246 if (le16_to_cpu(el->l_tree_depth) == 0) {
Joel Beckerd5628622009-02-13 02:54:36 -08004247 ocfs2_insert_at_leaf(et, insert_rec, el, type);
Mark Fashehdcd05382007-01-16 11:32:23 -08004248 goto out_update_clusters;
4249 }
4250
Joel Beckerffdd7a52008-10-17 22:32:01 -07004251 right_path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004252 if (!right_path) {
4253 ret = -ENOMEM;
4254 mlog_errno(ret);
4255 goto out;
4256 }
4257
4258 /*
4259 * Determine the path to start with. Rotations need the
4260 * rightmost path, everything else can go directly to the
4261 * target leaf.
4262 */
4263 cpos = le32_to_cpu(insert_rec->e_cpos);
4264 if (type->ins_appending == APPEND_NONE &&
4265 type->ins_contig == CONTIG_NONE) {
4266 rotate = 1;
4267 cpos = UINT_MAX;
4268 }
4269
Joel Beckerfacdb772009-02-12 18:08:48 -08004270 ret = ocfs2_find_path(et->et_ci, right_path, cpos);
Mark Fashehdcd05382007-01-16 11:32:23 -08004271 if (ret) {
4272 mlog_errno(ret);
4273 goto out;
4274 }
4275
4276 /*
4277 * Rotations and appends need special treatment - they modify
4278 * parts of the tree's above them.
4279 *
4280 * Both might pass back a path immediate to the left of the
4281 * one being inserted to. This will be cause
4282 * ocfs2_insert_path() to modify the rightmost records of
4283 * left_path to account for an edge insert.
4284 *
4285 * XXX: When modifying this code, keep in mind that an insert
4286 * can wind up skipping both of these two special cases...
4287 */
4288 if (rotate) {
Joel Becker1bbf0b82009-02-12 19:42:08 -08004289 ret = ocfs2_rotate_tree_right(handle, et, type->ins_split,
Mark Fashehdcd05382007-01-16 11:32:23 -08004290 le32_to_cpu(insert_rec->e_cpos),
4291 right_path, &left_path);
4292 if (ret) {
4293 mlog_errno(ret);
4294 goto out;
4295 }
Mark Fashehe8aed342007-12-03 16:43:01 -08004296
4297 /*
4298 * ocfs2_rotate_tree_right() might have extended the
4299 * transaction without re-journaling our tree root.
4300 */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004301 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004302 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehe8aed342007-12-03 16:43:01 -08004303 if (ret) {
4304 mlog_errno(ret);
4305 goto out;
4306 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004307 } else if (type->ins_appending == APPEND_TAIL
4308 && type->ins_contig != CONTIG_LEFT) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004309 ret = ocfs2_append_rec_to_path(handle, et, insert_rec,
Mark Fashehdcd05382007-01-16 11:32:23 -08004310 right_path, &left_path);
4311 if (ret) {
4312 mlog_errno(ret);
4313 goto out;
4314 }
4315 }
4316
Joel Becker3505bec2009-02-13 02:57:58 -08004317 ret = ocfs2_insert_path(handle, et, left_path, right_path,
Mark Fashehdcd05382007-01-16 11:32:23 -08004318 insert_rec, type);
4319 if (ret) {
4320 mlog_errno(ret);
4321 goto out;
4322 }
4323
4324out_update_clusters:
Mark Fasheh328d5752007-06-18 10:48:04 -07004325 if (type->ins_split == SPLIT_NONE)
Joel Becker6136ca52009-02-12 19:32:43 -08004326 ocfs2_et_update_clusters(et,
Joel Becker35dc0aa2008-08-20 16:25:06 -07004327 le16_to_cpu(insert_rec->e_leaf_clusters));
Mark Fashehdcd05382007-01-16 11:32:23 -08004328
Joel Beckerec20cec2010-03-19 14:13:52 -07004329 ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004330
4331out:
4332 ocfs2_free_path(left_path);
4333 ocfs2_free_path(right_path);
4334
4335 return ret;
4336}
4337
Xue jiufei9f99ad02015-06-24 16:55:07 -07004338static int ocfs2_figure_merge_contig_type(struct ocfs2_extent_tree *et,
Joel Beckera2970292009-02-13 03:09:54 -08004339 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004340 struct ocfs2_extent_list *el, int index,
Xue jiufei9f99ad02015-06-24 16:55:07 -07004341 struct ocfs2_extent_rec *split_rec,
4342 struct ocfs2_merge_ctxt *ctxt)
Mark Fasheh328d5752007-06-18 10:48:04 -07004343{
Xue jiufei9f99ad02015-06-24 16:55:07 -07004344 int status = 0;
Mark Fasheh328d5752007-06-18 10:48:04 -07004345 enum ocfs2_contig_type ret = CONTIG_NONE;
Tao Maad5a4d72008-01-30 14:21:32 +08004346 u32 left_cpos, right_cpos;
4347 struct ocfs2_extent_rec *rec = NULL;
4348 struct ocfs2_extent_list *new_el;
4349 struct ocfs2_path *left_path = NULL, *right_path = NULL;
4350 struct buffer_head *bh;
4351 struct ocfs2_extent_block *eb;
Joel Beckera2970292009-02-13 03:09:54 -08004352 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Tao Maad5a4d72008-01-30 14:21:32 +08004353
4354 if (index > 0) {
4355 rec = &el->l_recs[index - 1];
4356 } else if (path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004357 status = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004358 if (status)
Markus Elfring06a269cc2015-04-14 15:42:48 -07004359 goto exit;
Tao Maad5a4d72008-01-30 14:21:32 +08004360
4361 if (left_cpos != 0) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07004362 left_path = ocfs2_new_path_from_path(path);
Xue jiufei9f99ad02015-06-24 16:55:07 -07004363 if (!left_path) {
4364 status = -ENOMEM;
4365 mlog_errno(status);
Markus Elfring06a269cc2015-04-14 15:42:48 -07004366 goto exit;
Xue jiufei9f99ad02015-06-24 16:55:07 -07004367 }
Tao Maad5a4d72008-01-30 14:21:32 +08004368
Joel Beckera2970292009-02-13 03:09:54 -08004369 status = ocfs2_find_path(et->et_ci, left_path,
4370 left_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004371 if (status)
Markus Elfring06a269cc2015-04-14 15:42:48 -07004372 goto free_left_path;
Tao Maad5a4d72008-01-30 14:21:32 +08004373
4374 new_el = path_leaf_el(left_path);
4375
4376 if (le16_to_cpu(new_el->l_next_free_rec) !=
4377 le16_to_cpu(new_el->l_count)) {
4378 bh = path_leaf_bh(left_path);
4379 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004380 ocfs2_error(sb,
Joe Perches7ecef142015-09-04 15:44:51 -07004381 "Extent block #%llu has an invalid l_next_free_rec of %d. It should have matched the l_count of %d\n",
Joel Becker5e965812008-11-13 14:49:16 -08004382 (unsigned long long)le64_to_cpu(eb->h_blkno),
4383 le16_to_cpu(new_el->l_next_free_rec),
4384 le16_to_cpu(new_el->l_count));
4385 status = -EINVAL;
Markus Elfring06a269cc2015-04-14 15:42:48 -07004386 goto free_left_path;
Tao Maad5a4d72008-01-30 14:21:32 +08004387 }
4388 rec = &new_el->l_recs[
4389 le16_to_cpu(new_el->l_next_free_rec) - 1];
4390 }
4391 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004392
4393 /*
4394 * We're careful to check for an empty extent record here -
4395 * the merge code will know what to do if it sees one.
4396 */
Tao Maad5a4d72008-01-30 14:21:32 +08004397 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004398 if (index == 1 && ocfs2_is_empty_extent(rec)) {
4399 if (split_rec->e_cpos == el->l_recs[index].e_cpos)
4400 ret = CONTIG_RIGHT;
4401 } else {
Tao Ma853a3a12009-08-18 11:22:18 +08004402 ret = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004403 }
4404 }
4405
Tao Maad5a4d72008-01-30 14:21:32 +08004406 rec = NULL;
4407 if (index < (le16_to_cpu(el->l_next_free_rec) - 1))
4408 rec = &el->l_recs[index + 1];
4409 else if (le16_to_cpu(el->l_next_free_rec) == le16_to_cpu(el->l_count) &&
4410 path->p_tree_depth > 0) {
Joel Beckera2970292009-02-13 03:09:54 -08004411 status = ocfs2_find_cpos_for_right_leaf(sb, path, &right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004412 if (status)
Markus Elfring06a269cc2015-04-14 15:42:48 -07004413 goto free_left_path;
Tao Maad5a4d72008-01-30 14:21:32 +08004414
4415 if (right_cpos == 0)
Markus Elfring06a269cc2015-04-14 15:42:48 -07004416 goto free_left_path;
Tao Maad5a4d72008-01-30 14:21:32 +08004417
Joel Beckerffdd7a52008-10-17 22:32:01 -07004418 right_path = ocfs2_new_path_from_path(path);
Xue jiufei9f99ad02015-06-24 16:55:07 -07004419 if (!right_path) {
4420 status = -ENOMEM;
4421 mlog_errno(status);
Markus Elfring06a269cc2015-04-14 15:42:48 -07004422 goto free_left_path;
Xue jiufei9f99ad02015-06-24 16:55:07 -07004423 }
Tao Maad5a4d72008-01-30 14:21:32 +08004424
Joel Beckera2970292009-02-13 03:09:54 -08004425 status = ocfs2_find_path(et->et_ci, right_path, right_cpos);
Tao Maad5a4d72008-01-30 14:21:32 +08004426 if (status)
Markus Elfring06a269cc2015-04-14 15:42:48 -07004427 goto free_right_path;
Tao Maad5a4d72008-01-30 14:21:32 +08004428
4429 new_el = path_leaf_el(right_path);
4430 rec = &new_el->l_recs[0];
4431 if (ocfs2_is_empty_extent(rec)) {
4432 if (le16_to_cpu(new_el->l_next_free_rec) <= 1) {
4433 bh = path_leaf_bh(right_path);
4434 eb = (struct ocfs2_extent_block *)bh->b_data;
Joel Beckera2970292009-02-13 03:09:54 -08004435 ocfs2_error(sb,
Joe Perches7ecef142015-09-04 15:44:51 -07004436 "Extent block #%llu has an invalid l_next_free_rec of %d\n",
Joel Becker5e965812008-11-13 14:49:16 -08004437 (unsigned long long)le64_to_cpu(eb->h_blkno),
4438 le16_to_cpu(new_el->l_next_free_rec));
4439 status = -EINVAL;
Markus Elfring06a269cc2015-04-14 15:42:48 -07004440 goto free_right_path;
Tao Maad5a4d72008-01-30 14:21:32 +08004441 }
4442 rec = &new_el->l_recs[1];
4443 }
4444 }
4445
4446 if (rec) {
Mark Fasheh328d5752007-06-18 10:48:04 -07004447 enum ocfs2_contig_type contig_type;
4448
Tao Ma853a3a12009-08-18 11:22:18 +08004449 contig_type = ocfs2_et_extent_contig(et, rec, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004450
4451 if (contig_type == CONTIG_LEFT && ret == CONTIG_RIGHT)
4452 ret = CONTIG_LEFTRIGHT;
4453 else if (ret == CONTIG_NONE)
4454 ret = contig_type;
4455 }
4456
Markus Elfring06a269cc2015-04-14 15:42:48 -07004457free_right_path:
Markus Elfringfd90d4df2015-04-14 15:42:42 -07004458 ocfs2_free_path(right_path);
Markus Elfring06a269cc2015-04-14 15:42:48 -07004459free_left_path:
4460 ocfs2_free_path(left_path);
4461exit:
Xue jiufei9f99ad02015-06-24 16:55:07 -07004462 if (status == 0)
4463 ctxt->c_contig_type = ret;
4464
4465 return status;
Mark Fasheh328d5752007-06-18 10:48:04 -07004466}
4467
Joel Becker1ef61b32009-02-13 03:12:33 -08004468static void ocfs2_figure_contig_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004469 struct ocfs2_insert_type *insert,
4470 struct ocfs2_extent_list *el,
Joel Becker1ef61b32009-02-13 03:12:33 -08004471 struct ocfs2_extent_rec *insert_rec)
Mark Fashehdcd05382007-01-16 11:32:23 -08004472{
4473 int i;
4474 enum ocfs2_contig_type contig_type = CONTIG_NONE;
4475
Mark Fashehe48edee2007-03-07 16:46:57 -08004476 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
4477
Mark Fashehdcd05382007-01-16 11:32:23 -08004478 for(i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
Tao Ma853a3a12009-08-18 11:22:18 +08004479 contig_type = ocfs2_et_extent_contig(et, &el->l_recs[i],
4480 insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004481 if (contig_type != CONTIG_NONE) {
4482 insert->ins_contig_index = i;
4483 break;
4484 }
4485 }
4486 insert->ins_contig = contig_type;
Tao Maca12b7c2008-08-18 17:38:52 +08004487
4488 if (insert->ins_contig != CONTIG_NONE) {
4489 struct ocfs2_extent_rec *rec =
4490 &el->l_recs[insert->ins_contig_index];
4491 unsigned int len = le16_to_cpu(rec->e_leaf_clusters) +
4492 le16_to_cpu(insert_rec->e_leaf_clusters);
4493
4494 /*
4495 * Caller might want us to limit the size of extents, don't
4496 * calculate contiguousness if we might exceed that limit.
4497 */
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004498 if (et->et_max_leaf_clusters &&
4499 (len > et->et_max_leaf_clusters))
Tao Maca12b7c2008-08-18 17:38:52 +08004500 insert->ins_contig = CONTIG_NONE;
4501 }
Mark Fashehdcd05382007-01-16 11:32:23 -08004502}
4503
4504/*
4505 * This should only be called against the righmost leaf extent list.
4506 *
4507 * ocfs2_figure_appending_type() will figure out whether we'll have to
4508 * insert at the tail of the rightmost leaf.
4509 *
Tao Mae7d4cb62008-08-18 17:38:44 +08004510 * This should also work against the root extent list for tree's with 0
4511 * depth. If we consider the root extent list to be the rightmost leaf node
Mark Fashehdcd05382007-01-16 11:32:23 -08004512 * then the logic here makes sense.
4513 */
4514static void ocfs2_figure_appending_type(struct ocfs2_insert_type *insert,
4515 struct ocfs2_extent_list *el,
4516 struct ocfs2_extent_rec *insert_rec)
4517{
4518 int i;
4519 u32 cpos = le32_to_cpu(insert_rec->e_cpos);
4520 struct ocfs2_extent_rec *rec;
4521
4522 insert->ins_appending = APPEND_NONE;
4523
Mark Fashehe48edee2007-03-07 16:46:57 -08004524 BUG_ON(le16_to_cpu(el->l_tree_depth) != 0);
Mark Fashehdcd05382007-01-16 11:32:23 -08004525
4526 if (!el->l_next_free_rec)
4527 goto set_tail_append;
4528
4529 if (ocfs2_is_empty_extent(&el->l_recs[0])) {
4530 /* Were all records empty? */
4531 if (le16_to_cpu(el->l_next_free_rec) == 1)
4532 goto set_tail_append;
4533 }
4534
4535 i = le16_to_cpu(el->l_next_free_rec) - 1;
4536 rec = &el->l_recs[i];
4537
Mark Fashehe48edee2007-03-07 16:46:57 -08004538 if (cpos >=
4539 (le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)))
Mark Fashehdcd05382007-01-16 11:32:23 -08004540 goto set_tail_append;
4541
4542 return;
4543
4544set_tail_append:
4545 insert->ins_appending = APPEND_TAIL;
4546}
4547
4548/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004549 * Helper function called at the beginning of an insert.
Mark Fashehdcd05382007-01-16 11:32:23 -08004550 *
4551 * This computes a few things that are commonly used in the process of
4552 * inserting into the btree:
4553 * - Whether the new extent is contiguous with an existing one.
4554 * - The current tree depth.
4555 * - Whether the insert is an appending one.
4556 * - The total # of free records in the tree.
4557 *
4558 * All of the information is stored on the ocfs2_insert_type
4559 * structure.
4560 */
Joel Becker627961b2009-02-13 03:14:38 -08004561static int ocfs2_figure_insert_type(struct ocfs2_extent_tree *et,
Mark Fashehdcd05382007-01-16 11:32:23 -08004562 struct buffer_head **last_eb_bh,
4563 struct ocfs2_extent_rec *insert_rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004564 int *free_records,
Mark Fashehdcd05382007-01-16 11:32:23 -08004565 struct ocfs2_insert_type *insert)
4566{
4567 int ret;
Mark Fashehdcd05382007-01-16 11:32:23 -08004568 struct ocfs2_extent_block *eb;
4569 struct ocfs2_extent_list *el;
4570 struct ocfs2_path *path = NULL;
4571 struct buffer_head *bh = NULL;
4572
Mark Fasheh328d5752007-06-18 10:48:04 -07004573 insert->ins_split = SPLIT_NONE;
4574
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004575 el = et->et_root_el;
Mark Fashehdcd05382007-01-16 11:32:23 -08004576 insert->ins_tree_depth = le16_to_cpu(el->l_tree_depth);
4577
4578 if (el->l_tree_depth) {
4579 /*
4580 * If we have tree depth, we read in the
4581 * rightmost extent block ahead of time as
4582 * ocfs2_figure_insert_type() and ocfs2_add_branch()
4583 * may want it later.
4584 */
Joel Becker3d03a302009-02-12 17:49:26 -08004585 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08004586 ocfs2_et_get_last_eb_blk(et),
4587 &bh);
Mark Fashehdcd05382007-01-16 11:32:23 -08004588 if (ret) {
Tao Mac1e8d352011-03-07 16:43:21 +08004589 mlog_errno(ret);
Mark Fashehdcd05382007-01-16 11:32:23 -08004590 goto out;
4591 }
4592 eb = (struct ocfs2_extent_block *) bh->b_data;
4593 el = &eb->h_list;
4594 }
4595
4596 /*
4597 * Unless we have a contiguous insert, we'll need to know if
4598 * there is room left in our allocation tree for another
4599 * extent record.
4600 *
4601 * XXX: This test is simplistic, we can search for empty
4602 * extent records too.
4603 */
Tao Maoc77534f2007-08-28 17:22:33 -07004604 *free_records = le16_to_cpu(el->l_count) -
Mark Fashehdcd05382007-01-16 11:32:23 -08004605 le16_to_cpu(el->l_next_free_rec);
4606
4607 if (!insert->ins_tree_depth) {
Joel Becker1ef61b32009-02-13 03:12:33 -08004608 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004609 ocfs2_figure_appending_type(insert, el, insert_rec);
4610 return 0;
4611 }
4612
Joel Beckerffdd7a52008-10-17 22:32:01 -07004613 path = ocfs2_new_path_from_et(et);
Mark Fashehdcd05382007-01-16 11:32:23 -08004614 if (!path) {
4615 ret = -ENOMEM;
4616 mlog_errno(ret);
4617 goto out;
4618 }
4619
4620 /*
4621 * In the case that we're inserting past what the tree
4622 * currently accounts for, ocfs2_find_path() will return for
4623 * us the rightmost tree path. This is accounted for below in
4624 * the appending code.
4625 */
Joel Beckerfacdb772009-02-12 18:08:48 -08004626 ret = ocfs2_find_path(et->et_ci, path, le32_to_cpu(insert_rec->e_cpos));
Mark Fashehdcd05382007-01-16 11:32:23 -08004627 if (ret) {
4628 mlog_errno(ret);
4629 goto out;
4630 }
4631
4632 el = path_leaf_el(path);
4633
4634 /*
4635 * Now that we have the path, there's two things we want to determine:
4636 * 1) Contiguousness (also set contig_index if this is so)
4637 *
4638 * 2) Are we doing an append? We can trivially break this up
4639 * into two types of appends: simple record append, or a
4640 * rotate inside the tail leaf.
4641 */
Joel Becker1ef61b32009-02-13 03:12:33 -08004642 ocfs2_figure_contig_type(et, insert, el, insert_rec);
Mark Fashehdcd05382007-01-16 11:32:23 -08004643
4644 /*
4645 * The insert code isn't quite ready to deal with all cases of
4646 * left contiguousness. Specifically, if it's an insert into
4647 * the 1st record in a leaf, it will require the adjustment of
Mark Fashehe48edee2007-03-07 16:46:57 -08004648 * cluster count on the last record of the path directly to it's
Mark Fashehdcd05382007-01-16 11:32:23 -08004649 * left. For now, just catch that case and fool the layers
4650 * above us. This works just fine for tree_depth == 0, which
4651 * is why we allow that above.
4652 */
4653 if (insert->ins_contig == CONTIG_LEFT &&
4654 insert->ins_contig_index == 0)
4655 insert->ins_contig = CONTIG_NONE;
4656
4657 /*
4658 * Ok, so we can simply compare against last_eb to figure out
4659 * whether the path doesn't exist. This will only happen in
4660 * the case that we're doing a tail append, so maybe we can
4661 * take advantage of that information somehow.
4662 */
Joel Becker35dc0aa2008-08-20 16:25:06 -07004663 if (ocfs2_et_get_last_eb_blk(et) ==
Tao Mae7d4cb62008-08-18 17:38:44 +08004664 path_leaf_bh(path)->b_blocknr) {
Mark Fashehdcd05382007-01-16 11:32:23 -08004665 /*
4666 * Ok, ocfs2_find_path() returned us the rightmost
4667 * tree path. This might be an appending insert. There are
4668 * two cases:
4669 * 1) We're doing a true append at the tail:
4670 * -This might even be off the end of the leaf
4671 * 2) We're "appending" by rotating in the tail
4672 */
4673 ocfs2_figure_appending_type(insert, el, insert_rec);
4674 }
4675
4676out:
4677 ocfs2_free_path(path);
4678
4679 if (ret == 0)
4680 *last_eb_bh = bh;
4681 else
4682 brelse(bh);
4683 return ret;
4684}
4685
4686/*
Joel Beckercc79d8c2009-02-13 03:24:43 -08004687 * Insert an extent into a btree.
Mark Fashehdcd05382007-01-16 11:32:23 -08004688 *
Joel Beckercc79d8c2009-02-13 03:24:43 -08004689 * The caller needs to update the owning btree's cluster count.
Mark Fashehdcd05382007-01-16 11:32:23 -08004690 */
Joel Beckercc79d8c2009-02-13 03:24:43 -08004691int ocfs2_insert_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004692 struct ocfs2_extent_tree *et,
4693 u32 cpos,
4694 u64 start_blk,
4695 u32 new_clusters,
4696 u8 flags,
4697 struct ocfs2_alloc_context *meta_ac)
Mark Fashehccd979b2005-12-15 14:31:24 -08004698{
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004699 int status;
Tao Maoc77534f2007-08-28 17:22:33 -07004700 int uninitialized_var(free_records);
Mark Fashehccd979b2005-12-15 14:31:24 -08004701 struct buffer_head *last_eb_bh = NULL;
Mark Fashehdcd05382007-01-16 11:32:23 -08004702 struct ocfs2_insert_type insert = {0, };
4703 struct ocfs2_extent_rec rec;
Mark Fashehccd979b2005-12-15 14:31:24 -08004704
Tao Maa09d09b2011-02-22 08:21:10 +08004705 trace_ocfs2_insert_extent_start(
4706 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
4707 cpos, new_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08004708
Mark Fashehe48edee2007-03-07 16:46:57 -08004709 memset(&rec, 0, sizeof(rec));
Mark Fashehdcd05382007-01-16 11:32:23 -08004710 rec.e_cpos = cpu_to_le32(cpos);
4711 rec.e_blkno = cpu_to_le64(start_blk);
Mark Fashehe48edee2007-03-07 16:46:57 -08004712 rec.e_leaf_clusters = cpu_to_le16(new_clusters);
Mark Fasheh2ae99a62007-03-09 16:43:28 -08004713 rec.e_flags = flags;
Joel Becker6136ca52009-02-12 19:32:43 -08004714 status = ocfs2_et_insert_check(et, &rec);
Joel Becker1e61ee72008-08-20 18:32:45 -07004715 if (status) {
4716 mlog_errno(status);
4717 goto bail;
4718 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004719
Joel Becker627961b2009-02-13 03:14:38 -08004720 status = ocfs2_figure_insert_type(et, &last_eb_bh, &rec,
Tao Maoc77534f2007-08-28 17:22:33 -07004721 &free_records, &insert);
Mark Fashehdcd05382007-01-16 11:32:23 -08004722 if (status < 0) {
4723 mlog_errno(status);
4724 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08004725 }
4726
Tao Maa09d09b2011-02-22 08:21:10 +08004727 trace_ocfs2_insert_extent(insert.ins_appending, insert.ins_contig,
4728 insert.ins_contig_index, free_records,
4729 insert.ins_tree_depth);
Mark Fashehccd979b2005-12-15 14:31:24 -08004730
Tao Maoc77534f2007-08-28 17:22:33 -07004731 if (insert.ins_contig == CONTIG_NONE && free_records == 0) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004732 status = ocfs2_grow_tree(handle, et,
Mark Fasheh328d5752007-06-18 10:48:04 -07004733 &insert.ins_tree_depth, &last_eb_bh,
Mark Fashehc3afcbb2007-05-29 14:28:51 -07004734 meta_ac);
4735 if (status) {
Mark Fashehccd979b2005-12-15 14:31:24 -08004736 mlog_errno(status);
4737 goto bail;
4738 }
Mark Fashehccd979b2005-12-15 14:31:24 -08004739 }
4740
Mark Fashehdcd05382007-01-16 11:32:23 -08004741 /* Finally, we can add clusters. This might rotate the tree for us. */
Joel Becker3505bec2009-02-13 02:57:58 -08004742 status = ocfs2_do_insert_extent(handle, et, &rec, &insert);
Mark Fashehccd979b2005-12-15 14:31:24 -08004743 if (status < 0)
4744 mlog_errno(status);
Joel Becker92ba4702009-02-13 03:18:34 -08004745 else
4746 ocfs2_et_extent_map_insert(et, &rec);
Mark Fashehccd979b2005-12-15 14:31:24 -08004747
4748bail:
Mark Fasheha81cb882008-10-07 14:25:16 -07004749 brelse(last_eb_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08004750
Tao Maf56654c2008-08-18 17:38:48 +08004751 return status;
4752}
4753
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004754/*
4755 * Allcate and add clusters into the extent b-tree.
4756 * The new clusters(clusters_to_add) will be inserted at logical_offset.
Joel Beckerf99b9b72008-08-20 19:36:33 -07004757 * The extent b-tree's root is specified by et, and
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004758 * it is not limited to the file storage. Any extent tree can use this
4759 * function if it implements the proper ocfs2_extent_tree.
4760 */
Joel Beckercbee7e12009-02-13 03:34:15 -08004761int ocfs2_add_clusters_in_btree(handle_t *handle,
4762 struct ocfs2_extent_tree *et,
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004763 u32 *logical_offset,
4764 u32 clusters_to_add,
4765 int mark_unwritten,
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004766 struct ocfs2_alloc_context *data_ac,
4767 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004768 enum ocfs2_alloc_restarted *reason_ret)
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004769{
Tao Maa09d09b2011-02-22 08:21:10 +08004770 int status = 0, err = 0;
Zongxun Wangfb951eb2014-02-06 12:04:20 -08004771 int need_free = 0;
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004772 int free_extents;
4773 enum ocfs2_alloc_restarted reason = RESTART_NONE;
4774 u32 bit_off, num_bits;
4775 u64 block;
4776 u8 flags = 0;
Joel Beckercbee7e12009-02-13 03:34:15 -08004777 struct ocfs2_super *osb =
4778 OCFS2_SB(ocfs2_metadata_cache_get_super(et->et_ci));
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004779
4780 BUG_ON(!clusters_to_add);
4781
4782 if (mark_unwritten)
4783 flags = OCFS2_EXT_UNWRITTEN;
4784
Jun Piao964f14a2017-09-06 16:19:11 -07004785 free_extents = ocfs2_num_free_extents(et);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004786 if (free_extents < 0) {
4787 status = free_extents;
4788 mlog_errno(status);
4789 goto leave;
4790 }
4791
4792 /* there are two cases which could cause us to EAGAIN in the
4793 * we-need-more-metadata case:
4794 * 1) we haven't reserved *any*
4795 * 2) we are so fragmented, we've needed to add metadata too
4796 * many times. */
4797 if (!free_extents && !meta_ac) {
Tao Maa09d09b2011-02-22 08:21:10 +08004798 err = -1;
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004799 status = -EAGAIN;
4800 reason = RESTART_META;
4801 goto leave;
4802 } else if ((!free_extents)
4803 && (ocfs2_alloc_context_bits_left(meta_ac)
Joel Beckerf99b9b72008-08-20 19:36:33 -07004804 < ocfs2_extend_meta_needed(et->et_root_el))) {
Tao Maa09d09b2011-02-22 08:21:10 +08004805 err = -2;
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004806 status = -EAGAIN;
4807 reason = RESTART_META;
4808 goto leave;
4809 }
4810
Joel Becker1ed9b772010-05-06 13:59:06 +08004811 status = __ocfs2_claim_clusters(handle, data_ac, 1,
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004812 clusters_to_add, &bit_off, &num_bits);
4813 if (status < 0) {
4814 if (status != -ENOSPC)
4815 mlog_errno(status);
4816 goto leave;
4817 }
4818
4819 BUG_ON(num_bits > clusters_to_add);
4820
Joel Becker13723d02008-10-17 19:25:01 -07004821 /* reserve our write early -- insert_extent may update the tree root */
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08004822 status = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07004823 OCFS2_JOURNAL_ACCESS_WRITE);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004824 if (status < 0) {
4825 mlog_errno(status);
Zongxun Wangfb951eb2014-02-06 12:04:20 -08004826 need_free = 1;
4827 goto bail;
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004828 }
4829
4830 block = ocfs2_clusters_to_blocks(osb->sb, bit_off);
Tao Maa09d09b2011-02-22 08:21:10 +08004831 trace_ocfs2_add_clusters_in_btree(
4832 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
4833 bit_off, num_bits);
Joel Beckercc79d8c2009-02-13 03:24:43 -08004834 status = ocfs2_insert_extent(handle, et, *logical_offset, block,
Joel Beckerf99b9b72008-08-20 19:36:33 -07004835 num_bits, flags, meta_ac);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004836 if (status < 0) {
4837 mlog_errno(status);
Zongxun Wangfb951eb2014-02-06 12:04:20 -08004838 need_free = 1;
4839 goto bail;
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004840 }
4841
Joel Beckerec20cec2010-03-19 14:13:52 -07004842 ocfs2_journal_dirty(handle, et->et_root_bh);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004843
4844 clusters_to_add -= num_bits;
4845 *logical_offset += num_bits;
4846
4847 if (clusters_to_add) {
Tao Maa09d09b2011-02-22 08:21:10 +08004848 err = clusters_to_add;
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004849 status = -EAGAIN;
4850 reason = RESTART_TRANS;
4851 }
4852
Zongxun Wangfb951eb2014-02-06 12:04:20 -08004853bail:
4854 if (need_free) {
4855 if (data_ac->ac_which == OCFS2_AC_USE_LOCAL)
4856 ocfs2_free_local_alloc_bits(osb, handle, data_ac,
4857 bit_off, num_bits);
4858 else
4859 ocfs2_free_clusters(handle,
4860 data_ac->ac_inode,
4861 data_ac->ac_bh,
4862 ocfs2_clusters_to_blocks(osb->sb, bit_off),
4863 num_bits);
4864 }
4865
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004866leave:
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004867 if (reason_ret)
4868 *reason_ret = reason;
Tao Maa09d09b2011-02-22 08:21:10 +08004869 trace_ocfs2_add_clusters_in_btree_ret(status, reason, err);
Tao Ma0eb8d47e2008-08-18 17:38:45 +08004870 return status;
4871}
4872
Mark Fasheh328d5752007-06-18 10:48:04 -07004873static void ocfs2_make_right_split_rec(struct super_block *sb,
4874 struct ocfs2_extent_rec *split_rec,
4875 u32 cpos,
4876 struct ocfs2_extent_rec *rec)
4877{
4878 u32 rec_cpos = le32_to_cpu(rec->e_cpos);
4879 u32 rec_range = rec_cpos + le16_to_cpu(rec->e_leaf_clusters);
4880
4881 memset(split_rec, 0, sizeof(struct ocfs2_extent_rec));
4882
4883 split_rec->e_cpos = cpu_to_le32(cpos);
4884 split_rec->e_leaf_clusters = cpu_to_le16(rec_range - cpos);
4885
4886 split_rec->e_blkno = rec->e_blkno;
4887 le64_add_cpu(&split_rec->e_blkno,
4888 ocfs2_clusters_to_blocks(sb, cpos - rec_cpos));
4889
4890 split_rec->e_flags = rec->e_flags;
4891}
4892
Joel Beckerd2311292009-02-13 03:43:22 -08004893static int ocfs2_split_and_insert(handle_t *handle,
Tao Mae7d4cb62008-08-18 17:38:44 +08004894 struct ocfs2_extent_tree *et,
Joel Beckerd2311292009-02-13 03:43:22 -08004895 struct ocfs2_path *path,
Mark Fasheh328d5752007-06-18 10:48:04 -07004896 struct buffer_head **last_eb_bh,
4897 int split_index,
4898 struct ocfs2_extent_rec *orig_split_rec,
4899 struct ocfs2_alloc_context *meta_ac)
4900{
4901 int ret = 0, depth;
4902 unsigned int insert_range, rec_range, do_leftright = 0;
4903 struct ocfs2_extent_rec tmprec;
4904 struct ocfs2_extent_list *rightmost_el;
4905 struct ocfs2_extent_rec rec;
4906 struct ocfs2_extent_rec split_rec = *orig_split_rec;
4907 struct ocfs2_insert_type insert;
4908 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07004909
4910leftright:
4911 /*
4912 * Store a copy of the record on the stack - it might move
4913 * around as the tree is manipulated below.
4914 */
4915 rec = path_leaf_el(path)->l_recs[split_index];
4916
Joel Beckerce1d9ea2008-08-20 16:30:07 -07004917 rightmost_el = et->et_root_el;
Mark Fasheh328d5752007-06-18 10:48:04 -07004918
4919 depth = le16_to_cpu(rightmost_el->l_tree_depth);
4920 if (depth) {
4921 BUG_ON(!(*last_eb_bh));
4922 eb = (struct ocfs2_extent_block *) (*last_eb_bh)->b_data;
4923 rightmost_el = &eb->h_list;
4924 }
4925
4926 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
4927 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08004928 ret = ocfs2_grow_tree(handle, et,
Tao Mae7d4cb62008-08-18 17:38:44 +08004929 &depth, last_eb_bh, meta_ac);
Mark Fasheh328d5752007-06-18 10:48:04 -07004930 if (ret) {
4931 mlog_errno(ret);
4932 goto out;
4933 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004934 }
4935
4936 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
4937 insert.ins_appending = APPEND_NONE;
4938 insert.ins_contig = CONTIG_NONE;
Mark Fasheh328d5752007-06-18 10:48:04 -07004939 insert.ins_tree_depth = depth;
4940
4941 insert_range = le32_to_cpu(split_rec.e_cpos) +
4942 le16_to_cpu(split_rec.e_leaf_clusters);
4943 rec_range = le32_to_cpu(rec.e_cpos) +
4944 le16_to_cpu(rec.e_leaf_clusters);
4945
4946 if (split_rec.e_cpos == rec.e_cpos) {
4947 insert.ins_split = SPLIT_LEFT;
4948 } else if (insert_range == rec_range) {
4949 insert.ins_split = SPLIT_RIGHT;
4950 } else {
4951 /*
4952 * Left/right split. We fake this as a right split
4953 * first and then make a second pass as a left split.
4954 */
4955 insert.ins_split = SPLIT_RIGHT;
4956
Joel Beckerd2311292009-02-13 03:43:22 -08004957 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
4958 &tmprec, insert_range, &rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07004959
4960 split_rec = tmprec;
4961
4962 BUG_ON(do_leftright);
4963 do_leftright = 1;
4964 }
4965
Joel Becker3505bec2009-02-13 02:57:58 -08004966 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fasheh328d5752007-06-18 10:48:04 -07004967 if (ret) {
4968 mlog_errno(ret);
4969 goto out;
4970 }
4971
4972 if (do_leftright == 1) {
4973 u32 cpos;
4974 struct ocfs2_extent_list *el;
4975
4976 do_leftright++;
4977 split_rec = *orig_split_rec;
4978
4979 ocfs2_reinit_path(path, 1);
4980
4981 cpos = le32_to_cpu(split_rec.e_cpos);
Joel Beckerfacdb772009-02-12 18:08:48 -08004982 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07004983 if (ret) {
4984 mlog_errno(ret);
4985 goto out;
4986 }
4987
4988 el = path_leaf_el(path);
4989 split_index = ocfs2_search_extent_list(el, cpos);
Yingtai Xie981035b2014-08-06 16:03:54 -07004990 if (split_index == -1) {
4991 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Joe Perches7ecef142015-09-04 15:44:51 -07004992 "Owner %llu has an extent at cpos %u which can no longer be found\n",
4993 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
4994 cpos);
Yingtai Xie981035b2014-08-06 16:03:54 -07004995 ret = -EROFS;
4996 goto out;
4997 }
Mark Fasheh328d5752007-06-18 10:48:04 -07004998 goto leftright;
4999 }
5000out:
5001
5002 return ret;
5003}
5004
Joel Beckerf3868d02009-02-17 19:46:04 -08005005static int ocfs2_replace_extent_rec(handle_t *handle,
5006 struct ocfs2_extent_tree *et,
Tao Ma47be12e2009-01-09 07:32:48 +08005007 struct ocfs2_path *path,
5008 struct ocfs2_extent_list *el,
5009 int split_index,
5010 struct ocfs2_extent_rec *split_rec)
5011{
5012 int ret;
5013
Joel Beckerf3868d02009-02-17 19:46:04 -08005014 ret = ocfs2_path_bh_journal_access(handle, et->et_ci, path,
Tao Ma47be12e2009-01-09 07:32:48 +08005015 path_num_items(path) - 1);
5016 if (ret) {
5017 mlog_errno(ret);
5018 goto out;
5019 }
5020
5021 el->l_recs[split_index] = *split_rec;
5022
5023 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5024out:
5025 return ret;
5026}
5027
Mark Fasheh328d5752007-06-18 10:48:04 -07005028/*
Tao Ma555936b2009-08-18 11:22:21 +08005029 * Split part or all of the extent record at split_index in the leaf
5030 * pointed to by path. Merge with the contiguous extent record if needed.
Mark Fasheh328d5752007-06-18 10:48:04 -07005031 *
5032 * Care is taken to handle contiguousness so as to not grow the tree.
5033 *
5034 * meta_ac is not strictly necessary - we only truly need it if growth
5035 * of the tree is required. All other cases will degrade into a less
5036 * optimal tree layout.
5037 *
Tao Mae7d4cb62008-08-18 17:38:44 +08005038 * last_eb_bh should be the rightmost leaf block for any extent
5039 * btree. Since a split may grow the tree or a merge might shrink it,
5040 * the caller cannot trust the contents of that buffer after this call.
Mark Fasheh328d5752007-06-18 10:48:04 -07005041 *
5042 * This code is optimized for readability - several passes might be
5043 * made over certain portions of the tree. All of those blocks will
5044 * have been brought into cache (and pinned via the journal), so the
5045 * extra overhead is not expressed in terms of disk reads.
5046 */
Tao Mae2e9f602009-08-18 11:22:34 +08005047int ocfs2_split_extent(handle_t *handle,
5048 struct ocfs2_extent_tree *et,
5049 struct ocfs2_path *path,
5050 int split_index,
5051 struct ocfs2_extent_rec *split_rec,
5052 struct ocfs2_alloc_context *meta_ac,
5053 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fasheh328d5752007-06-18 10:48:04 -07005054{
5055 int ret = 0;
5056 struct ocfs2_extent_list *el = path_leaf_el(path);
Mark Fashehe8aed342007-12-03 16:43:01 -08005057 struct buffer_head *last_eb_bh = NULL;
Mark Fasheh328d5752007-06-18 10:48:04 -07005058 struct ocfs2_extent_rec *rec = &el->l_recs[split_index];
5059 struct ocfs2_merge_ctxt ctxt;
5060 struct ocfs2_extent_list *rightmost_el;
5061
Mark Fasheh328d5752007-06-18 10:48:04 -07005062 if (le32_to_cpu(rec->e_cpos) > le32_to_cpu(split_rec->e_cpos) ||
5063 ((le32_to_cpu(rec->e_cpos) + le16_to_cpu(rec->e_leaf_clusters)) <
5064 (le32_to_cpu(split_rec->e_cpos) + le16_to_cpu(split_rec->e_leaf_clusters)))) {
5065 ret = -EIO;
5066 mlog_errno(ret);
5067 goto out;
5068 }
5069
Xue jiufei9f99ad02015-06-24 16:55:07 -07005070 ret = ocfs2_figure_merge_contig_type(et, path, el,
5071 split_index,
5072 split_rec,
5073 &ctxt);
5074 if (ret) {
5075 mlog_errno(ret);
5076 goto out;
5077 }
Mark Fasheh328d5752007-06-18 10:48:04 -07005078
5079 /*
5080 * The core merge / split code wants to know how much room is
Joel Beckera1cf0762009-02-13 03:45:49 -08005081 * left in this allocation tree, so we pass the
Mark Fasheh328d5752007-06-18 10:48:04 -07005082 * rightmost extent list.
5083 */
5084 if (path->p_tree_depth) {
5085 struct ocfs2_extent_block *eb;
Mark Fasheh328d5752007-06-18 10:48:04 -07005086
Joel Becker3d03a302009-02-12 17:49:26 -08005087 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005088 ocfs2_et_get_last_eb_blk(et),
5089 &last_eb_bh);
Mark Fasheh328d5752007-06-18 10:48:04 -07005090 if (ret) {
Tao Mac1e8d352011-03-07 16:43:21 +08005091 mlog_errno(ret);
Mark Fasheh328d5752007-06-18 10:48:04 -07005092 goto out;
5093 }
5094
5095 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
Mark Fasheh328d5752007-06-18 10:48:04 -07005096 rightmost_el = &eb->h_list;
5097 } else
5098 rightmost_el = path_root_el(path);
5099
Mark Fasheh328d5752007-06-18 10:48:04 -07005100 if (rec->e_cpos == split_rec->e_cpos &&
5101 rec->e_leaf_clusters == split_rec->e_leaf_clusters)
5102 ctxt.c_split_covers_rec = 1;
5103 else
5104 ctxt.c_split_covers_rec = 0;
5105
5106 ctxt.c_has_empty_extent = ocfs2_is_empty_extent(&el->l_recs[0]);
5107
Tao Maa09d09b2011-02-22 08:21:10 +08005108 trace_ocfs2_split_extent(split_index, ctxt.c_contig_type,
5109 ctxt.c_has_empty_extent,
5110 ctxt.c_split_covers_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005111
5112 if (ctxt.c_contig_type == CONTIG_NONE) {
5113 if (ctxt.c_split_covers_rec)
Joel Beckerf3868d02009-02-17 19:46:04 -08005114 ret = ocfs2_replace_extent_rec(handle, et, path, el,
Tao Ma47be12e2009-01-09 07:32:48 +08005115 split_index, split_rec);
Mark Fasheh328d5752007-06-18 10:48:04 -07005116 else
Joel Beckerd2311292009-02-13 03:43:22 -08005117 ret = ocfs2_split_and_insert(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005118 &last_eb_bh, split_index,
5119 split_rec, meta_ac);
5120 if (ret)
5121 mlog_errno(ret);
5122 } else {
Joel Beckerc495dd22009-02-13 02:19:11 -08005123 ret = ocfs2_try_to_merge_extent(handle, et, path,
Mark Fasheh328d5752007-06-18 10:48:04 -07005124 split_index, split_rec,
Joel Beckerc495dd22009-02-13 02:19:11 -08005125 dealloc, &ctxt);
Mark Fasheh328d5752007-06-18 10:48:04 -07005126 if (ret)
5127 mlog_errno(ret);
5128 }
5129
Mark Fasheh328d5752007-06-18 10:48:04 -07005130out:
5131 brelse(last_eb_bh);
5132 return ret;
5133}
5134
5135/*
Tao Ma555936b2009-08-18 11:22:21 +08005136 * Change the flags of the already-existing extent at cpos for len clusters.
5137 *
5138 * new_flags: the flags we want to set.
5139 * clear_flags: the flags we want to clear.
5140 * phys: the new physical offset we want this new extent starts from.
Mark Fasheh328d5752007-06-18 10:48:04 -07005141 *
5142 * If the existing extent is larger than the request, initiate a
5143 * split. An attempt will be made at merging with adjacent extents.
5144 *
5145 * The caller is responsible for passing down meta_ac if we'll need it.
5146 */
Tao Ma1aa75fe2009-08-18 11:28:39 +08005147int ocfs2_change_extent_flag(handle_t *handle,
5148 struct ocfs2_extent_tree *et,
5149 u32 cpos, u32 len, u32 phys,
5150 struct ocfs2_alloc_context *meta_ac,
5151 struct ocfs2_cached_dealloc_ctxt *dealloc,
5152 int new_flags, int clear_flags)
Mark Fasheh328d5752007-06-18 10:48:04 -07005153{
5154 int ret, index;
Tao Ma555936b2009-08-18 11:22:21 +08005155 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
5156 u64 start_blkno = ocfs2_clusters_to_blocks(sb, phys);
Mark Fasheh328d5752007-06-18 10:48:04 -07005157 struct ocfs2_extent_rec split_rec;
5158 struct ocfs2_path *left_path = NULL;
5159 struct ocfs2_extent_list *el;
Tao Ma555936b2009-08-18 11:22:21 +08005160 struct ocfs2_extent_rec *rec;
Mark Fasheh328d5752007-06-18 10:48:04 -07005161
Joel Beckerffdd7a52008-10-17 22:32:01 -07005162 left_path = ocfs2_new_path_from_et(et);
Mark Fasheh328d5752007-06-18 10:48:04 -07005163 if (!left_path) {
5164 ret = -ENOMEM;
5165 mlog_errno(ret);
5166 goto out;
5167 }
5168
Joel Beckerfacdb772009-02-12 18:08:48 -08005169 ret = ocfs2_find_path(et->et_ci, left_path, cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005170 if (ret) {
5171 mlog_errno(ret);
5172 goto out;
5173 }
5174 el = path_leaf_el(left_path);
5175
5176 index = ocfs2_search_extent_list(el, cpos);
Yingtai Xie981035b2014-08-06 16:03:54 -07005177 if (index == -1) {
Tao Ma555936b2009-08-18 11:22:21 +08005178 ocfs2_error(sb,
Joe Perches7ecef142015-09-04 15:44:51 -07005179 "Owner %llu has an extent at cpos %u which can no longer be found\n",
5180 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5181 cpos);
Mark Fasheh328d5752007-06-18 10:48:04 -07005182 ret = -EROFS;
5183 goto out;
5184 }
5185
Tao Ma555936b2009-08-18 11:22:21 +08005186 ret = -EIO;
5187 rec = &el->l_recs[index];
5188 if (new_flags && (rec->e_flags & new_flags)) {
5189 mlog(ML_ERROR, "Owner %llu tried to set %d flags on an "
Darrick J. Wong86544fb2016-11-09 14:13:09 -08005190 "extent that already had them\n",
Tao Ma555936b2009-08-18 11:22:21 +08005191 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5192 new_flags);
5193 goto out;
5194 }
5195
5196 if (clear_flags && !(rec->e_flags & clear_flags)) {
5197 mlog(ML_ERROR, "Owner %llu tried to clear %d flags on an "
Darrick J. Wong86544fb2016-11-09 14:13:09 -08005198 "extent that didn't have them\n",
Tao Ma555936b2009-08-18 11:22:21 +08005199 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5200 clear_flags);
5201 goto out;
5202 }
5203
Mark Fasheh328d5752007-06-18 10:48:04 -07005204 memset(&split_rec, 0, sizeof(struct ocfs2_extent_rec));
5205 split_rec.e_cpos = cpu_to_le32(cpos);
5206 split_rec.e_leaf_clusters = cpu_to_le16(len);
5207 split_rec.e_blkno = cpu_to_le64(start_blkno);
Tao Ma555936b2009-08-18 11:22:21 +08005208 split_rec.e_flags = rec->e_flags;
5209 if (new_flags)
5210 split_rec.e_flags |= new_flags;
5211 if (clear_flags)
5212 split_rec.e_flags &= ~clear_flags;
Mark Fasheh328d5752007-06-18 10:48:04 -07005213
Tao Mae2e9f602009-08-18 11:22:34 +08005214 ret = ocfs2_split_extent(handle, et, left_path,
5215 index, &split_rec, meta_ac,
5216 dealloc);
Mark Fasheh328d5752007-06-18 10:48:04 -07005217 if (ret)
5218 mlog_errno(ret);
5219
5220out:
5221 ocfs2_free_path(left_path);
5222 return ret;
Tao Ma555936b2009-08-18 11:22:21 +08005223
5224}
5225
5226/*
5227 * Mark the already-existing extent at cpos as written for len clusters.
5228 * This removes the unwritten extent flag.
5229 *
5230 * If the existing extent is larger than the request, initiate a
5231 * split. An attempt will be made at merging with adjacent extents.
5232 *
5233 * The caller is responsible for passing down meta_ac if we'll need it.
5234 */
5235int ocfs2_mark_extent_written(struct inode *inode,
5236 struct ocfs2_extent_tree *et,
5237 handle_t *handle, u32 cpos, u32 len, u32 phys,
5238 struct ocfs2_alloc_context *meta_ac,
5239 struct ocfs2_cached_dealloc_ctxt *dealloc)
5240{
5241 int ret;
5242
Tao Maa09d09b2011-02-22 08:21:10 +08005243 trace_ocfs2_mark_extent_written(
5244 (unsigned long long)OCFS2_I(inode)->ip_blkno,
5245 cpos, len, phys);
Tao Ma555936b2009-08-18 11:22:21 +08005246
5247 if (!ocfs2_writes_unwritten_extents(OCFS2_SB(inode->i_sb))) {
Joe Perches7ecef142015-09-04 15:44:51 -07005248 ocfs2_error(inode->i_sb, "Inode %llu has unwritten extents that are being written to, but the feature bit is not set in the super block\n",
Tao Ma555936b2009-08-18 11:22:21 +08005249 (unsigned long long)OCFS2_I(inode)->ip_blkno);
5250 ret = -EROFS;
5251 goto out;
5252 }
5253
5254 /*
5255 * XXX: This should be fixed up so that we just re-insert the
5256 * next extent records.
5257 */
5258 ocfs2_et_extent_map_truncate(et, 0);
5259
5260 ret = ocfs2_change_extent_flag(handle, et, cpos,
5261 len, phys, meta_ac, dealloc,
5262 0, OCFS2_EXT_UNWRITTEN);
5263 if (ret)
5264 mlog_errno(ret);
5265
5266out:
5267 return ret;
Mark Fasheh328d5752007-06-18 10:48:04 -07005268}
5269
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005270static int ocfs2_split_tree(handle_t *handle, struct ocfs2_extent_tree *et,
5271 struct ocfs2_path *path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005272 int index, u32 new_range,
5273 struct ocfs2_alloc_context *meta_ac)
5274{
Tao Mac901fb02010-04-26 14:34:57 +08005275 int ret, depth, credits;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005276 struct buffer_head *last_eb_bh = NULL;
5277 struct ocfs2_extent_block *eb;
5278 struct ocfs2_extent_list *rightmost_el, *el;
5279 struct ocfs2_extent_rec split_rec;
5280 struct ocfs2_extent_rec *rec;
5281 struct ocfs2_insert_type insert;
5282
5283 /*
5284 * Setup the record to split before we grow the tree.
5285 */
5286 el = path_leaf_el(path);
5287 rec = &el->l_recs[index];
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005288 ocfs2_make_right_split_rec(ocfs2_metadata_cache_get_super(et->et_ci),
5289 &split_rec, new_range, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005290
5291 depth = path->p_tree_depth;
5292 if (depth > 0) {
Joel Becker3d03a302009-02-12 17:49:26 -08005293 ret = ocfs2_read_extent_block(et->et_ci,
Joel Becker5e965812008-11-13 14:49:16 -08005294 ocfs2_et_get_last_eb_blk(et),
5295 &last_eb_bh);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005296 if (ret < 0) {
5297 mlog_errno(ret);
5298 goto out;
5299 }
5300
5301 eb = (struct ocfs2_extent_block *) last_eb_bh->b_data;
5302 rightmost_el = &eb->h_list;
5303 } else
5304 rightmost_el = path_leaf_el(path);
5305
Tao Mac901fb02010-04-26 14:34:57 +08005306 credits = path->p_tree_depth +
5307 ocfs2_extend_meta_needed(et->et_root_el);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005308 ret = ocfs2_extend_trans(handle, credits);
5309 if (ret) {
5310 mlog_errno(ret);
5311 goto out;
5312 }
5313
5314 if (le16_to_cpu(rightmost_el->l_next_free_rec) ==
5315 le16_to_cpu(rightmost_el->l_count)) {
Joel Beckerd401dc12009-02-13 02:24:10 -08005316 ret = ocfs2_grow_tree(handle, et, &depth, &last_eb_bh,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005317 meta_ac);
5318 if (ret) {
5319 mlog_errno(ret);
5320 goto out;
5321 }
Mark Fashehd0c7d702007-07-03 13:27:22 -07005322 }
5323
5324 memset(&insert, 0, sizeof(struct ocfs2_insert_type));
5325 insert.ins_appending = APPEND_NONE;
5326 insert.ins_contig = CONTIG_NONE;
5327 insert.ins_split = SPLIT_RIGHT;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005328 insert.ins_tree_depth = depth;
5329
Joel Becker3505bec2009-02-13 02:57:58 -08005330 ret = ocfs2_do_insert_extent(handle, et, &split_rec, &insert);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005331 if (ret)
5332 mlog_errno(ret);
5333
5334out:
5335 brelse(last_eb_bh);
5336 return ret;
5337}
5338
Joel Becker043beeb2009-02-13 02:42:30 -08005339static int ocfs2_truncate_rec(handle_t *handle,
5340 struct ocfs2_extent_tree *et,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005341 struct ocfs2_path *path, int index,
5342 struct ocfs2_cached_dealloc_ctxt *dealloc,
Joel Becker043beeb2009-02-13 02:42:30 -08005343 u32 cpos, u32 len)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005344{
5345 int ret;
5346 u32 left_cpos, rec_range, trunc_range;
piaojunc14688e2016-05-19 17:09:47 -07005347 int is_rightmost_tree_rec = 0;
Joel Becker043beeb2009-02-13 02:42:30 -08005348 struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005349 struct ocfs2_path *left_path = NULL;
5350 struct ocfs2_extent_list *el = path_leaf_el(path);
5351 struct ocfs2_extent_rec *rec;
5352 struct ocfs2_extent_block *eb;
5353
5354 if (ocfs2_is_empty_extent(&el->l_recs[0]) && index > 0) {
Xue jiufei17215982016-03-25 14:21:41 -07005355 /* extend credit for ocfs2_remove_rightmost_path */
5356 ret = ocfs2_extend_rotate_transaction(handle, 0,
5357 handle->h_buffer_credits,
5358 path);
5359 if (ret) {
5360 mlog_errno(ret);
5361 goto out;
5362 }
5363
Joel Becker70f18c02009-02-13 02:09:31 -08005364 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005365 if (ret) {
5366 mlog_errno(ret);
5367 goto out;
5368 }
5369
5370 index--;
5371 }
5372
5373 if (index == (le16_to_cpu(el->l_next_free_rec) - 1) &&
5374 path->p_tree_depth) {
5375 /*
5376 * Check whether this is the rightmost tree record. If
5377 * we remove all of this record or part of its right
5378 * edge then an update of the record lengths above it
5379 * will be required.
5380 */
5381 eb = (struct ocfs2_extent_block *)path_leaf_bh(path)->b_data;
5382 if (eb->h_next_leaf_blk == 0)
5383 is_rightmost_tree_rec = 1;
5384 }
5385
5386 rec = &el->l_recs[index];
5387 if (index == 0 && path->p_tree_depth &&
5388 le32_to_cpu(rec->e_cpos) == cpos) {
5389 /*
5390 * Changing the leftmost offset (via partial or whole
5391 * record truncate) of an interior (or rightmost) path
5392 * means we have to update the subtree that is formed
5393 * by this leaf and the one to it's left.
5394 *
5395 * There are two cases we can skip:
Joel Becker043beeb2009-02-13 02:42:30 -08005396 * 1) Path is the leftmost one in our btree.
Mark Fashehd0c7d702007-07-03 13:27:22 -07005397 * 2) The leaf is rightmost and will be empty after
5398 * we remove the extent record - the rotate code
5399 * knows how to update the newly formed edge.
5400 */
5401
Joel Becker043beeb2009-02-13 02:42:30 -08005402 ret = ocfs2_find_cpos_for_left_leaf(sb, path, &left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005403 if (ret) {
5404 mlog_errno(ret);
5405 goto out;
5406 }
5407
5408 if (left_cpos && le16_to_cpu(el->l_next_free_rec) > 1) {
Joel Beckerffdd7a52008-10-17 22:32:01 -07005409 left_path = ocfs2_new_path_from_path(path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005410 if (!left_path) {
5411 ret = -ENOMEM;
5412 mlog_errno(ret);
5413 goto out;
5414 }
5415
Joel Beckerfacdb772009-02-12 18:08:48 -08005416 ret = ocfs2_find_path(et->et_ci, left_path,
5417 left_cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005418 if (ret) {
5419 mlog_errno(ret);
5420 goto out;
5421 }
5422 }
5423 }
5424
5425 ret = ocfs2_extend_rotate_transaction(handle, 0,
5426 handle->h_buffer_credits,
5427 path);
5428 if (ret) {
5429 mlog_errno(ret);
5430 goto out;
5431 }
5432
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005433 ret = ocfs2_journal_access_path(et->et_ci, handle, path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005434 if (ret) {
5435 mlog_errno(ret);
5436 goto out;
5437 }
5438
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005439 ret = ocfs2_journal_access_path(et->et_ci, handle, left_path);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005440 if (ret) {
5441 mlog_errno(ret);
5442 goto out;
5443 }
5444
5445 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5446 trunc_range = cpos + len;
5447
5448 if (le32_to_cpu(rec->e_cpos) == cpos && rec_range == trunc_range) {
5449 int next_free;
5450
5451 memset(rec, 0, sizeof(*rec));
5452 ocfs2_cleanup_merge(el, index);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005453
5454 next_free = le16_to_cpu(el->l_next_free_rec);
5455 if (is_rightmost_tree_rec && next_free > 1) {
5456 /*
5457 * We skip the edge update if this path will
5458 * be deleted by the rotate code.
5459 */
5460 rec = &el->l_recs[next_free - 1];
Joel Beckerd401dc12009-02-13 02:24:10 -08005461 ocfs2_adjust_rightmost_records(handle, et, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005462 rec);
5463 }
5464 } else if (le32_to_cpu(rec->e_cpos) == cpos) {
5465 /* Remove leftmost portion of the record. */
5466 le32_add_cpu(&rec->e_cpos, len);
5467 le64_add_cpu(&rec->e_blkno, ocfs2_clusters_to_blocks(sb, len));
5468 le16_add_cpu(&rec->e_leaf_clusters, -len);
5469 } else if (rec_range == trunc_range) {
5470 /* Remove rightmost portion of the record */
5471 le16_add_cpu(&rec->e_leaf_clusters, -len);
5472 if (is_rightmost_tree_rec)
Joel Beckerd401dc12009-02-13 02:24:10 -08005473 ocfs2_adjust_rightmost_records(handle, et, path, rec);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005474 } else {
5475 /* Caller should have trapped this. */
Joel Becker043beeb2009-02-13 02:42:30 -08005476 mlog(ML_ERROR, "Owner %llu: Invalid record truncate: (%u, %u) "
5477 "(%u, %u)\n",
5478 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005479 le32_to_cpu(rec->e_cpos),
5480 le16_to_cpu(rec->e_leaf_clusters), cpos, len);
5481 BUG();
5482 }
5483
5484 if (left_path) {
5485 int subtree_index;
5486
Joel Becker7dc02802009-02-12 19:20:13 -08005487 subtree_index = ocfs2_find_subtree_root(et, left_path, path);
Joel Becker4619c732009-02-12 19:02:36 -08005488 ocfs2_complete_edge_insert(handle, left_path, path,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005489 subtree_index);
5490 }
5491
5492 ocfs2_journal_dirty(handle, path_leaf_bh(path));
5493
Joel Becker70f18c02009-02-13 02:09:31 -08005494 ret = ocfs2_rotate_tree_left(handle, et, path, dealloc);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005495 if (ret) {
5496 mlog_errno(ret);
5497 goto out;
5498 }
5499
5500out:
5501 ocfs2_free_path(left_path);
5502 return ret;
5503}
5504
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005505int ocfs2_remove_extent(handle_t *handle,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005506 struct ocfs2_extent_tree *et,
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005507 u32 cpos, u32 len,
Mark Fasheh063c4562007-07-03 13:34:11 -07005508 struct ocfs2_alloc_context *meta_ac,
Joel Beckerf99b9b72008-08-20 19:36:33 -07005509 struct ocfs2_cached_dealloc_ctxt *dealloc)
Mark Fashehd0c7d702007-07-03 13:27:22 -07005510{
5511 int ret, index;
5512 u32 rec_range, trunc_range;
5513 struct ocfs2_extent_rec *rec;
5514 struct ocfs2_extent_list *el;
Tao Mae7d4cb62008-08-18 17:38:44 +08005515 struct ocfs2_path *path = NULL;
Mark Fashehd0c7d702007-07-03 13:27:22 -07005516
Joel Becker4c911ee2009-02-13 02:50:12 -08005517 /*
5518 * XXX: Why are we truncating to 0 instead of wherever this
5519 * affects us?
5520 */
5521 ocfs2_et_extent_map_truncate(et, 0);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005522
Joel Beckerffdd7a52008-10-17 22:32:01 -07005523 path = ocfs2_new_path_from_et(et);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005524 if (!path) {
5525 ret = -ENOMEM;
5526 mlog_errno(ret);
5527 goto out;
5528 }
5529
Joel Beckerfacdb772009-02-12 18:08:48 -08005530 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005531 if (ret) {
5532 mlog_errno(ret);
5533 goto out;
5534 }
5535
5536 el = path_leaf_el(path);
5537 index = ocfs2_search_extent_list(el, cpos);
Yingtai Xie981035b2014-08-06 16:03:54 -07005538 if (index == -1) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005539 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Joe Perches7ecef142015-09-04 15:44:51 -07005540 "Owner %llu has an extent at cpos %u which can no longer be found\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005541 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5542 cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005543 ret = -EROFS;
5544 goto out;
5545 }
5546
5547 /*
5548 * We have 3 cases of extent removal:
5549 * 1) Range covers the entire extent rec
5550 * 2) Range begins or ends on one edge of the extent rec
5551 * 3) Range is in the middle of the extent rec (no shared edges)
5552 *
5553 * For case 1 we remove the extent rec and left rotate to
5554 * fill the hole.
5555 *
5556 * For case 2 we just shrink the existing extent rec, with a
5557 * tree update if the shrinking edge is also the edge of an
5558 * extent block.
5559 *
5560 * For case 3 we do a right split to turn the extent rec into
5561 * something case 2 can handle.
5562 */
5563 rec = &el->l_recs[index];
5564 rec_range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
5565 trunc_range = cpos + len;
5566
5567 BUG_ON(cpos < le32_to_cpu(rec->e_cpos) || trunc_range > rec_range);
5568
Tao Maa09d09b2011-02-22 08:21:10 +08005569 trace_ocfs2_remove_extent(
5570 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
5571 cpos, len, index, le32_to_cpu(rec->e_cpos),
5572 ocfs2_rec_clusters(el, rec));
Mark Fashehd0c7d702007-07-03 13:27:22 -07005573
5574 if (le32_to_cpu(rec->e_cpos) == cpos || rec_range == trunc_range) {
Joel Becker043beeb2009-02-13 02:42:30 -08005575 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5576 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005577 if (ret) {
5578 mlog_errno(ret);
5579 goto out;
5580 }
5581 } else {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005582 ret = ocfs2_split_tree(handle, et, path, index,
Mark Fashehd0c7d702007-07-03 13:27:22 -07005583 trunc_range, meta_ac);
5584 if (ret) {
5585 mlog_errno(ret);
5586 goto out;
5587 }
5588
5589 /*
5590 * The split could have manipulated the tree enough to
5591 * move the record location, so we have to look for it again.
5592 */
5593 ocfs2_reinit_path(path, 1);
5594
Joel Beckerfacdb772009-02-12 18:08:48 -08005595 ret = ocfs2_find_path(et->et_ci, path, cpos);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005596 if (ret) {
5597 mlog_errno(ret);
5598 goto out;
5599 }
5600
5601 el = path_leaf_el(path);
5602 index = ocfs2_search_extent_list(el, cpos);
Yingtai Xie981035b2014-08-06 16:03:54 -07005603 if (index == -1) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005604 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Joe Perches7ecef142015-09-04 15:44:51 -07005605 "Owner %llu: split at cpos %u lost record\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005606 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005607 cpos);
5608 ret = -EROFS;
5609 goto out;
5610 }
5611
5612 /*
5613 * Double check our values here. If anything is fishy,
5614 * it's easier to catch it at the top level.
5615 */
5616 rec = &el->l_recs[index];
5617 rec_range = le32_to_cpu(rec->e_cpos) +
5618 ocfs2_rec_clusters(el, rec);
5619 if (rec_range != trunc_range) {
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005620 ocfs2_error(ocfs2_metadata_cache_get_super(et->et_ci),
Joe Perches7ecef142015-09-04 15:44:51 -07005621 "Owner %llu: error after split at cpos %u trunc len %u, existing record is (%u,%u)\n",
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005622 (unsigned long long)ocfs2_metadata_cache_owner(et->et_ci),
Mark Fashehd0c7d702007-07-03 13:27:22 -07005623 cpos, len, le32_to_cpu(rec->e_cpos),
5624 ocfs2_rec_clusters(el, rec));
5625 ret = -EROFS;
5626 goto out;
5627 }
5628
Joel Becker043beeb2009-02-13 02:42:30 -08005629 ret = ocfs2_truncate_rec(handle, et, path, index, dealloc,
5630 cpos, len);
Mark Fashehd0c7d702007-07-03 13:27:22 -07005631 if (ret) {
5632 mlog_errno(ret);
5633 goto out;
5634 }
5635 }
5636
5637out:
5638 ocfs2_free_path(path);
5639 return ret;
5640}
5641
Tristan Ye78f94672010-05-11 17:54:42 +08005642/*
5643 * ocfs2_reserve_blocks_for_rec_trunc() would look basically the
5644 * same as ocfs2_lock_alloctors(), except for it accepts a blocks
5645 * number to reserve some extra blocks, and it only handles meta
5646 * data allocations.
5647 *
5648 * Currently, only ocfs2_remove_btree_range() uses it for truncating
5649 * and punching holes.
5650 */
5651static int ocfs2_reserve_blocks_for_rec_trunc(struct inode *inode,
5652 struct ocfs2_extent_tree *et,
5653 u32 extents_to_split,
5654 struct ocfs2_alloc_context **ac,
5655 int extra_blocks)
5656{
5657 int ret = 0, num_free_extents;
5658 unsigned int max_recs_needed = 2 * extents_to_split;
5659 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5660
5661 *ac = NULL;
5662
Jun Piao964f14a2017-09-06 16:19:11 -07005663 num_free_extents = ocfs2_num_free_extents(et);
Tristan Ye78f94672010-05-11 17:54:42 +08005664 if (num_free_extents < 0) {
5665 ret = num_free_extents;
5666 mlog_errno(ret);
5667 goto out;
5668 }
5669
5670 if (!num_free_extents ||
5671 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed))
5672 extra_blocks += ocfs2_extend_meta_needed(et->et_root_el);
5673
5674 if (extra_blocks) {
5675 ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, ac);
5676 if (ret < 0) {
5677 if (ret != -ENOSPC)
5678 mlog_errno(ret);
5679 goto out;
5680 }
5681 }
5682
5683out:
5684 if (ret) {
5685 if (*ac) {
5686 ocfs2_free_alloc_context(*ac);
5687 *ac = NULL;
5688 }
5689 }
5690
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,
Tristan Ye78f94672010-05-11 17:54:42 +08005696 u32 cpos, u32 phys_cpos, u32 len, int flags,
5697 struct ocfs2_cached_dealloc_ctxt *dealloc,
Junxiao Bif62f12b2014-12-18 16:17:32 -08005698 u64 refcount_loc, bool refcount_tree_locked)
Mark Fashehfecc0112008-11-12 15:16:38 -08005699{
Tristan Ye78f94672010-05-11 17:54:42 +08005700 int ret, credits = 0, extra_blocks = 0;
Mark Fashehfecc0112008-11-12 15:16:38 -08005701 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos);
5702 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5703 struct inode *tl_inode = osb->osb_tl_inode;
5704 handle_t *handle;
5705 struct ocfs2_alloc_context *meta_ac = NULL;
Tristan Ye78f94672010-05-11 17:54:42 +08005706 struct ocfs2_refcount_tree *ref_tree = NULL;
Mark Fashehfecc0112008-11-12 15:16:38 -08005707
Tristan Ye78f94672010-05-11 17:54:42 +08005708 if ((flags & OCFS2_EXT_REFCOUNTED) && len) {
Darrick J. Wong84e40082016-11-09 14:13:09 -08005709 BUG_ON(!ocfs2_is_refcount_inode(inode));
Tristan Ye78f94672010-05-11 17:54:42 +08005710
Junxiao Bif62f12b2014-12-18 16:17:32 -08005711 if (!refcount_tree_locked) {
5712 ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
5713 &ref_tree, NULL);
5714 if (ret) {
5715 mlog_errno(ret);
5716 goto bail;
5717 }
Tristan Ye78f94672010-05-11 17:54:42 +08005718 }
5719
5720 ret = ocfs2_prepare_refcount_change_for_del(inode,
5721 refcount_loc,
5722 phys_blkno,
5723 len,
5724 &credits,
5725 &extra_blocks);
5726 if (ret < 0) {
5727 mlog_errno(ret);
Joseph Qi33add0e2013-07-03 15:00:52 -07005728 goto bail;
Tristan Ye78f94672010-05-11 17:54:42 +08005729 }
5730 }
5731
5732 ret = ocfs2_reserve_blocks_for_rec_trunc(inode, et, 1, &meta_ac,
5733 extra_blocks);
Mark Fashehfecc0112008-11-12 15:16:38 -08005734 if (ret) {
5735 mlog_errno(ret);
Joseph Qi33add0e2013-07-03 15:00:52 -07005736 goto bail;
Mark Fashehfecc0112008-11-12 15:16:38 -08005737 }
5738
Al Viro59551022016-01-22 15:40:57 -05005739 inode_lock(tl_inode);
Mark Fashehfecc0112008-11-12 15:16:38 -08005740
5741 if (ocfs2_truncate_log_needs_flush(osb)) {
5742 ret = __ocfs2_flush_truncate_log(osb);
5743 if (ret < 0) {
5744 mlog_errno(ret);
5745 goto out;
5746 }
5747 }
5748
Tristan Ye78f94672010-05-11 17:54:42 +08005749 handle = ocfs2_start_trans(osb,
5750 ocfs2_remove_extent_credits(osb->sb) + credits);
Mark Fashehfecc0112008-11-12 15:16:38 -08005751 if (IS_ERR(handle)) {
5752 ret = PTR_ERR(handle);
5753 mlog_errno(ret);
5754 goto out;
5755 }
5756
Joel Beckerd9a0a1f2009-02-12 17:32:34 -08005757 ret = ocfs2_et_root_journal_access(handle, et,
Joel Becker13723d02008-10-17 19:25:01 -07005758 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehfecc0112008-11-12 15:16:38 -08005759 if (ret) {
5760 mlog_errno(ret);
Wengang Wangb8a0ae52011-10-12 15:22:15 +08005761 goto out_commit;
Mark Fashehfecc0112008-11-12 15:16:38 -08005762 }
5763
Christoph Hellwig5dd40562010-03-03 09:05:00 -05005764 dquot_free_space_nodirty(inode,
Mark Fashehfd4ef232009-01-29 15:06:21 -08005765 ocfs2_clusters_to_bytes(inode->i_sb, len));
5766
Joel Beckerdbdcf6a2009-02-13 03:41:26 -08005767 ret = ocfs2_remove_extent(handle, et, cpos, len, meta_ac, dealloc);
Mark Fashehfecc0112008-11-12 15:16:38 -08005768 if (ret) {
5769 mlog_errno(ret);
5770 goto out_commit;
5771 }
5772
Joel Becker6136ca52009-02-12 19:32:43 -08005773 ocfs2_et_update_clusters(et, -len);
Darrick J. Wong6fdb7022014-04-03 14:47:08 -07005774 ocfs2_update_inode_fsync_trans(handle, inode, 1);
Mark Fashehfecc0112008-11-12 15:16:38 -08005775
Joel Beckerec20cec2010-03-19 14:13:52 -07005776 ocfs2_journal_dirty(handle, et->et_root_bh);
Mark Fashehfecc0112008-11-12 15:16:38 -08005777
Tristan Ye78f94672010-05-11 17:54:42 +08005778 if (phys_blkno) {
5779 if (flags & OCFS2_EXT_REFCOUNTED)
5780 ret = ocfs2_decrease_refcount(inode, handle,
5781 ocfs2_blocks_to_clusters(osb->sb,
5782 phys_blkno),
5783 len, meta_ac,
5784 dealloc, 1);
5785 else
5786 ret = ocfs2_truncate_log_append(osb, handle,
5787 phys_blkno, len);
5788 if (ret)
5789 mlog_errno(ret);
5790
5791 }
Mark Fashehfecc0112008-11-12 15:16:38 -08005792
5793out_commit:
5794 ocfs2_commit_trans(osb, handle);
5795out:
Al Viro59551022016-01-22 15:40:57 -05005796 inode_unlock(tl_inode);
Joseph Qi33add0e2013-07-03 15:00:52 -07005797bail:
Mark Fashehfecc0112008-11-12 15:16:38 -08005798 if (meta_ac)
5799 ocfs2_free_alloc_context(meta_ac);
5800
Tristan Ye78f94672010-05-11 17:54:42 +08005801 if (ref_tree)
5802 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
5803
Mark Fashehfecc0112008-11-12 15:16:38 -08005804 return ret;
5805}
5806
Mark Fasheh063c4562007-07-03 13:34:11 -07005807int ocfs2_truncate_log_needs_flush(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005808{
5809 struct buffer_head *tl_bh = osb->osb_tl_bh;
5810 struct ocfs2_dinode *di;
5811 struct ocfs2_truncate_log *tl;
5812
5813 di = (struct ocfs2_dinode *) tl_bh->b_data;
5814 tl = &di->id2.i_dealloc;
5815
5816 mlog_bug_on_msg(le16_to_cpu(tl->tl_used) > le16_to_cpu(tl->tl_count),
5817 "slot %d, invalid truncate log parameters: used = "
5818 "%u, count = %u\n", osb->slot_num,
5819 le16_to_cpu(tl->tl_used), le16_to_cpu(tl->tl_count));
5820 return le16_to_cpu(tl->tl_used) == le16_to_cpu(tl->tl_count);
5821}
5822
5823static int ocfs2_truncate_log_can_coalesce(struct ocfs2_truncate_log *tl,
5824 unsigned int new_start)
5825{
5826 unsigned int tail_index;
5827 unsigned int current_tail;
5828
5829 /* No records, nothing to coalesce */
5830 if (!le16_to_cpu(tl->tl_used))
5831 return 0;
5832
5833 tail_index = le16_to_cpu(tl->tl_used) - 1;
5834 current_tail = le32_to_cpu(tl->tl_recs[tail_index].t_start);
5835 current_tail += le32_to_cpu(tl->tl_recs[tail_index].t_clusters);
5836
5837 return current_tail == new_start;
5838}
5839
Mark Fasheh063c4562007-07-03 13:34:11 -07005840int ocfs2_truncate_log_append(struct ocfs2_super *osb,
5841 handle_t *handle,
5842 u64 start_blk,
5843 unsigned int num_clusters)
Mark Fashehccd979b2005-12-15 14:31:24 -08005844{
5845 int status, index;
5846 unsigned int start_cluster, tl_count;
5847 struct inode *tl_inode = osb->osb_tl_inode;
5848 struct buffer_head *tl_bh = osb->osb_tl_bh;
5849 struct ocfs2_dinode *di;
5850 struct ocfs2_truncate_log *tl;
5851
Al Viro59551022016-01-22 15:40:57 -05005852 BUG_ON(inode_trylock(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08005853
5854 start_cluster = ocfs2_blocks_to_clusters(osb->sb, start_blk);
5855
5856 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08005857
Joel Becker10995aa2008-11-13 14:49:12 -08005858 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
5859 * by the underlying call to ocfs2_read_inode_block(), so any
5860 * corruption is a code bug */
5861 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
5862
5863 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08005864 tl_count = le16_to_cpu(tl->tl_count);
5865 mlog_bug_on_msg(tl_count > ocfs2_truncate_recs_per_inode(osb->sb) ||
5866 tl_count == 0,
Mark Fashehb06970532006-03-03 10:24:33 -08005867 "Truncate record count on #%llu invalid "
5868 "wanted %u, actual %u\n",
5869 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
Mark Fashehccd979b2005-12-15 14:31:24 -08005870 ocfs2_truncate_recs_per_inode(osb->sb),
5871 le16_to_cpu(tl->tl_count));
5872
5873 /* Caller should have known to flush before calling us. */
5874 index = le16_to_cpu(tl->tl_used);
5875 if (index >= tl_count) {
5876 status = -ENOSPC;
5877 mlog_errno(status);
5878 goto bail;
5879 }
5880
Joel Becker0cf2f762009-02-12 16:41:25 -08005881 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005882 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005883 if (status < 0) {
5884 mlog_errno(status);
5885 goto bail;
5886 }
5887
Tao Maa09d09b2011-02-22 08:21:10 +08005888 trace_ocfs2_truncate_log_append(
5889 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno, index,
5890 start_cluster, num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005891 if (ocfs2_truncate_log_can_coalesce(tl, start_cluster)) {
5892 /*
5893 * Move index back to the record we are coalescing with.
5894 * ocfs2_truncate_log_can_coalesce() guarantees nonzero
5895 */
5896 index--;
5897
5898 num_clusters += le32_to_cpu(tl->tl_recs[index].t_clusters);
Tao Maa09d09b2011-02-22 08:21:10 +08005899 trace_ocfs2_truncate_log_append(
5900 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5901 index, le32_to_cpu(tl->tl_recs[index].t_start),
5902 num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005903 } else {
5904 tl->tl_recs[index].t_start = cpu_to_le32(start_cluster);
5905 tl->tl_used = cpu_to_le16(index + 1);
5906 }
5907 tl->tl_recs[index].t_clusters = cpu_to_le32(num_clusters);
5908
Joel Beckerec20cec2010-03-19 14:13:52 -07005909 ocfs2_journal_dirty(handle, tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005910
Tao Ma50308d82010-11-04 15:14:11 +08005911 osb->truncated_clusters += num_clusters;
Mark Fashehccd979b2005-12-15 14:31:24 -08005912bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08005913 return status;
5914}
5915
5916static int ocfs2_replay_truncate_records(struct ocfs2_super *osb,
Mark Fashehccd979b2005-12-15 14:31:24 -08005917 struct inode *data_alloc_inode,
5918 struct buffer_head *data_alloc_bh)
5919{
5920 int status = 0;
5921 int i;
5922 unsigned int num_clusters;
5923 u64 start_blk;
5924 struct ocfs2_truncate_rec rec;
5925 struct ocfs2_dinode *di;
5926 struct ocfs2_truncate_log *tl;
5927 struct inode *tl_inode = osb->osb_tl_inode;
5928 struct buffer_head *tl_bh = osb->osb_tl_bh;
Junxiao Bi2b0ad002016-09-19 14:44:21 -07005929 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08005930
Mark Fashehccd979b2005-12-15 14:31:24 -08005931 di = (struct ocfs2_dinode *) tl_bh->b_data;
5932 tl = &di->id2.i_dealloc;
5933 i = le16_to_cpu(tl->tl_used) - 1;
5934 while (i >= 0) {
Junxiao Bi2b0ad002016-09-19 14:44:21 -07005935 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_FLUSH_ONE_REC);
5936 if (IS_ERR(handle)) {
5937 status = PTR_ERR(handle);
5938 mlog_errno(status);
5939 goto bail;
5940 }
5941
Mark Fashehccd979b2005-12-15 14:31:24 -08005942 /* Caller has given us at least enough credits to
5943 * update the truncate log dinode */
Joel Becker0cf2f762009-02-12 16:41:25 -08005944 status = ocfs2_journal_access_di(handle, INODE_CACHE(tl_inode), tl_bh,
Joel Becker13723d02008-10-17 19:25:01 -07005945 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fashehccd979b2005-12-15 14:31:24 -08005946 if (status < 0) {
5947 mlog_errno(status);
5948 goto bail;
5949 }
5950
5951 tl->tl_used = cpu_to_le16(i);
5952
Joel Beckerec20cec2010-03-19 14:13:52 -07005953 ocfs2_journal_dirty(handle, tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08005954
Mark Fashehccd979b2005-12-15 14:31:24 -08005955 rec = tl->tl_recs[i];
5956 start_blk = ocfs2_clusters_to_blocks(data_alloc_inode->i_sb,
5957 le32_to_cpu(rec.t_start));
5958 num_clusters = le32_to_cpu(rec.t_clusters);
5959
5960 /* if start_blk is not set, we ignore the record as
5961 * invalid. */
5962 if (start_blk) {
Tao Maa09d09b2011-02-22 08:21:10 +08005963 trace_ocfs2_replay_truncate_records(
5964 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
5965 i, le32_to_cpu(rec.t_start), num_clusters);
Mark Fashehccd979b2005-12-15 14:31:24 -08005966
5967 status = ocfs2_free_clusters(handle, data_alloc_inode,
5968 data_alloc_bh, start_blk,
5969 num_clusters);
5970 if (status < 0) {
5971 mlog_errno(status);
5972 goto bail;
5973 }
5974 }
Xue jiufei102c2592016-03-25 14:21:44 -07005975
Junxiao Bi2b0ad002016-09-19 14:44:21 -07005976 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08005977 i--;
5978 }
5979
Tao Ma50308d82010-11-04 15:14:11 +08005980 osb->truncated_clusters = 0;
5981
Mark Fashehccd979b2005-12-15 14:31:24 -08005982bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08005983 return status;
5984}
5985
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08005986/* Expects you to already be holding tl_inode->i_mutex */
Mark Fasheh063c4562007-07-03 13:34:11 -07005987int __ocfs2_flush_truncate_log(struct ocfs2_super *osb)
Mark Fashehccd979b2005-12-15 14:31:24 -08005988{
5989 int status;
5990 unsigned int num_to_flush;
Mark Fashehccd979b2005-12-15 14:31:24 -08005991 struct inode *tl_inode = osb->osb_tl_inode;
5992 struct inode *data_alloc_inode = NULL;
5993 struct buffer_head *tl_bh = osb->osb_tl_bh;
5994 struct buffer_head *data_alloc_bh = NULL;
5995 struct ocfs2_dinode *di;
5996 struct ocfs2_truncate_log *tl;
5997
Al Viro59551022016-01-22 15:40:57 -05005998 BUG_ON(inode_trylock(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08005999
6000 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08006001
Joel Becker10995aa2008-11-13 14:49:12 -08006002 /* tl_bh is loaded from ocfs2_truncate_log_init(). It's validated
6003 * by the underlying call to ocfs2_read_inode_block(), so any
6004 * corruption is a code bug */
6005 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6006
6007 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08006008 num_to_flush = le16_to_cpu(tl->tl_used);
Tao Maa09d09b2011-02-22 08:21:10 +08006009 trace_ocfs2_flush_truncate_log(
6010 (unsigned long long)OCFS2_I(tl_inode)->ip_blkno,
6011 num_to_flush);
Mark Fashehccd979b2005-12-15 14:31:24 -08006012 if (!num_to_flush) {
6013 status = 0;
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006014 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006015 }
6016
6017 data_alloc_inode = ocfs2_get_system_file_inode(osb,
6018 GLOBAL_BITMAP_SYSTEM_INODE,
6019 OCFS2_INVALID_SLOT);
6020 if (!data_alloc_inode) {
6021 status = -EINVAL;
6022 mlog(ML_ERROR, "Could not get bitmap inode!\n");
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006023 goto out;
Mark Fashehccd979b2005-12-15 14:31:24 -08006024 }
6025
Al Viro59551022016-01-22 15:40:57 -05006026 inode_lock(data_alloc_inode);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006027
Mark Fashehe63aecb62007-10-18 15:30:42 -07006028 status = ocfs2_inode_lock(data_alloc_inode, &data_alloc_bh, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08006029 if (status < 0) {
6030 mlog_errno(status);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006031 goto out_mutex;
Mark Fashehccd979b2005-12-15 14:31:24 -08006032 }
6033
Junxiao Bi2b0ad002016-09-19 14:44:21 -07006034 status = ocfs2_replay_truncate_records(osb, data_alloc_inode,
Mark Fashehccd979b2005-12-15 14:31:24 -08006035 data_alloc_bh);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006036 if (status < 0)
Mark Fashehccd979b2005-12-15 14:31:24 -08006037 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08006038
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006039 brelse(data_alloc_bh);
Mark Fashehe63aecb62007-10-18 15:30:42 -07006040 ocfs2_inode_unlock(data_alloc_inode, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08006041
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006042out_mutex:
Al Viro59551022016-01-22 15:40:57 -05006043 inode_unlock(data_alloc_inode);
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006044 iput(data_alloc_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006045
Mark Fashehe08dc8b2006-10-05 15:58:48 -07006046out:
Mark Fashehccd979b2005-12-15 14:31:24 -08006047 return status;
6048}
6049
6050int ocfs2_flush_truncate_log(struct ocfs2_super *osb)
6051{
6052 int status;
6053 struct inode *tl_inode = osb->osb_tl_inode;
6054
Al Viro59551022016-01-22 15:40:57 -05006055 inode_lock(tl_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006056 status = __ocfs2_flush_truncate_log(osb);
Al Viro59551022016-01-22 15:40:57 -05006057 inode_unlock(tl_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006058
6059 return status;
6060}
6061
David Howellsc4028952006-11-22 14:57:56 +00006062static void ocfs2_truncate_log_worker(struct work_struct *work)
Mark Fashehccd979b2005-12-15 14:31:24 -08006063{
6064 int status;
David Howellsc4028952006-11-22 14:57:56 +00006065 struct ocfs2_super *osb =
6066 container_of(work, struct ocfs2_super,
6067 osb_truncate_log_wq.work);
Mark Fashehccd979b2005-12-15 14:31:24 -08006068
Mark Fashehccd979b2005-12-15 14:31:24 -08006069 status = ocfs2_flush_truncate_log(osb);
6070 if (status < 0)
6071 mlog_errno(status);
Tao Ma4d0ddb22008-03-05 16:11:46 +08006072 else
Tiger Yangb89c5422010-01-25 14:11:06 +08006073 ocfs2_init_steal_slots(osb);
Mark Fashehccd979b2005-12-15 14:31:24 -08006074}
6075
6076#define OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL (2 * HZ)
6077void ocfs2_schedule_truncate_log_flush(struct ocfs2_super *osb,
6078 int cancel)
6079{
Xue jiufeia9e9aca2014-06-04 16:06:13 -07006080 if (osb->osb_tl_inode &&
6081 atomic_read(&osb->osb_tl_disable) == 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006082 /* We want to push off log flushes while truncates are
6083 * still running. */
6084 if (cancel)
6085 cancel_delayed_work(&osb->osb_truncate_log_wq);
6086
jiangyiwen35ddf782016-03-25 14:21:32 -07006087 queue_delayed_work(osb->ocfs2_wq, &osb->osb_truncate_log_wq,
Mark Fashehccd979b2005-12-15 14:31:24 -08006088 OCFS2_TRUNCATE_LOG_FLUSH_INTERVAL);
6089 }
6090}
6091
Eric Ren2070ad12016-08-02 14:02:10 -07006092/*
6093 * Try to flush truncate logs if we can free enough clusters from it.
6094 * As for return value, "< 0" means error, "0" no space and "1" means
6095 * we have freed enough spaces and let the caller try to allocate again.
6096 */
6097int ocfs2_try_to_free_truncate_log(struct ocfs2_super *osb,
6098 unsigned int needed)
6099{
6100 tid_t target;
6101 int ret = 0;
6102 unsigned int truncated_clusters;
6103
6104 inode_lock(osb->osb_tl_inode);
6105 truncated_clusters = osb->truncated_clusters;
6106 inode_unlock(osb->osb_tl_inode);
6107
6108 /*
6109 * Check whether we can succeed in allocating if we free
6110 * the truncate log.
6111 */
6112 if (truncated_clusters < needed)
6113 goto out;
6114
6115 ret = ocfs2_flush_truncate_log(osb);
6116 if (ret) {
6117 mlog_errno(ret);
6118 goto out;
6119 }
6120
6121 if (jbd2_journal_start_commit(osb->journal->j_journal, &target)) {
6122 jbd2_log_wait_commit(osb->journal->j_journal, target);
6123 ret = 1;
6124 }
6125out:
6126 return ret;
6127}
6128
Mark Fashehccd979b2005-12-15 14:31:24 -08006129static int ocfs2_get_truncate_log_info(struct ocfs2_super *osb,
6130 int slot_num,
6131 struct inode **tl_inode,
6132 struct buffer_head **tl_bh)
6133{
6134 int status;
6135 struct inode *inode = NULL;
6136 struct buffer_head *bh = NULL;
6137
6138 inode = ocfs2_get_system_file_inode(osb,
6139 TRUNCATE_LOG_SYSTEM_INODE,
6140 slot_num);
6141 if (!inode) {
6142 status = -EINVAL;
6143 mlog(ML_ERROR, "Could not get load truncate log inode!\n");
6144 goto bail;
6145 }
6146
Joel Beckerb657c952008-11-13 14:49:11 -08006147 status = ocfs2_read_inode_block(inode, &bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006148 if (status < 0) {
6149 iput(inode);
6150 mlog_errno(status);
6151 goto bail;
6152 }
6153
6154 *tl_inode = inode;
6155 *tl_bh = bh;
6156bail:
Mark Fashehccd979b2005-12-15 14:31:24 -08006157 return status;
6158}
6159
6160/* called during the 1st stage of node recovery. we stamp a clean
6161 * truncate log and pass back a copy for processing later. if the
6162 * truncate log does not require processing, a *tl_copy is set to
6163 * NULL. */
6164int ocfs2_begin_truncate_log_recovery(struct ocfs2_super *osb,
6165 int slot_num,
6166 struct ocfs2_dinode **tl_copy)
6167{
6168 int status;
6169 struct inode *tl_inode = NULL;
6170 struct buffer_head *tl_bh = NULL;
6171 struct ocfs2_dinode *di;
6172 struct ocfs2_truncate_log *tl;
6173
6174 *tl_copy = NULL;
6175
Tao Maa09d09b2011-02-22 08:21:10 +08006176 trace_ocfs2_begin_truncate_log_recovery(slot_num);
Mark Fashehccd979b2005-12-15 14:31:24 -08006177
6178 status = ocfs2_get_truncate_log_info(osb, slot_num, &tl_inode, &tl_bh);
6179 if (status < 0) {
6180 mlog_errno(status);
6181 goto bail;
6182 }
6183
6184 di = (struct ocfs2_dinode *) tl_bh->b_data;
Mark Fashehccd979b2005-12-15 14:31:24 -08006185
Joel Becker10995aa2008-11-13 14:49:12 -08006186 /* tl_bh is loaded from ocfs2_get_truncate_log_info(). It's
6187 * validated by the underlying call to ocfs2_read_inode_block(),
6188 * so any corruption is a code bug */
6189 BUG_ON(!OCFS2_IS_VALID_DINODE(di));
6190
6191 tl = &di->id2.i_dealloc;
Mark Fashehccd979b2005-12-15 14:31:24 -08006192 if (le16_to_cpu(tl->tl_used)) {
Tao Maa09d09b2011-02-22 08:21:10 +08006193 trace_ocfs2_truncate_log_recovery_num(le16_to_cpu(tl->tl_used));
Mark Fashehccd979b2005-12-15 14:31:24 -08006194
6195 *tl_copy = kmalloc(tl_bh->b_size, GFP_KERNEL);
6196 if (!(*tl_copy)) {
6197 status = -ENOMEM;
6198 mlog_errno(status);
6199 goto bail;
6200 }
6201
6202 /* Assuming the write-out below goes well, this copy
6203 * will be passed back to recovery for processing. */
6204 memcpy(*tl_copy, tl_bh->b_data, tl_bh->b_size);
6205
6206 /* All we need to do to clear the truncate log is set
6207 * tl_used. */
6208 tl->tl_used = 0;
6209
Joel Becker13723d02008-10-17 19:25:01 -07006210 ocfs2_compute_meta_ecc(osb->sb, tl_bh->b_data, &di->i_check);
Joel Becker8cb471e2009-02-10 20:00:41 -08006211 status = ocfs2_write_block(osb, tl_bh, INODE_CACHE(tl_inode));
Mark Fashehccd979b2005-12-15 14:31:24 -08006212 if (status < 0) {
6213 mlog_errno(status);
6214 goto bail;
6215 }
6216 }
6217
6218bail:
Joseph Qi72865d92016-01-14 15:17:27 -08006219 iput(tl_inode);
Mark Fasheha81cb882008-10-07 14:25:16 -07006220 brelse(tl_bh);
Mark Fashehccd979b2005-12-15 14:31:24 -08006221
Joseph Qi46359292015-09-04 15:44:54 -07006222 if (status < 0) {
Mark Fashehccd979b2005-12-15 14:31:24 -08006223 kfree(*tl_copy);
6224 *tl_copy = NULL;
Tao Mac1e8d352011-03-07 16:43:21 +08006225 mlog_errno(status);
Mark Fashehccd979b2005-12-15 14:31:24 -08006226 }
6227
Mark Fashehccd979b2005-12-15 14:31:24 -08006228 return status;
6229}
6230
6231int ocfs2_complete_truncate_log_recovery(struct ocfs2_super *osb,
6232 struct ocfs2_dinode *tl_copy)
6233{
6234 int status = 0;
6235 int i;
6236 unsigned int clusters, num_recs, start_cluster;
6237 u64 start_blk;
Mark Fasheh1fabe142006-10-09 18:11:45 -07006238 handle_t *handle;
Mark Fashehccd979b2005-12-15 14:31:24 -08006239 struct inode *tl_inode = osb->osb_tl_inode;
6240 struct ocfs2_truncate_log *tl;
6241
Mark Fashehccd979b2005-12-15 14:31:24 -08006242 if (OCFS2_I(tl_inode)->ip_blkno == le64_to_cpu(tl_copy->i_blkno)) {
6243 mlog(ML_ERROR, "Asked to recover my own truncate log!\n");
6244 return -EINVAL;
6245 }
6246
6247 tl = &tl_copy->id2.i_dealloc;
6248 num_recs = le16_to_cpu(tl->tl_used);
Tao Maa09d09b2011-02-22 08:21:10 +08006249 trace_ocfs2_complete_truncate_log_recovery(
6250 (unsigned long long)le64_to_cpu(tl_copy->i_blkno),
6251 num_recs);
Mark Fashehccd979b2005-12-15 14:31:24 -08006252
Al Viro59551022016-01-22 15:40:57 -05006253 inode_lock(tl_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006254 for(i = 0; i < num_recs; i++) {
6255 if (ocfs2_truncate_log_needs_flush(osb)) {
6256 status = __ocfs2_flush_truncate_log(osb);
6257 if (status < 0) {
6258 mlog_errno(status);
6259 goto bail_up;
6260 }
6261 }
6262
Mark Fasheh65eff9c2006-10-09 17:26:22 -07006263 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
Mark Fashehccd979b2005-12-15 14:31:24 -08006264 if (IS_ERR(handle)) {
6265 status = PTR_ERR(handle);
6266 mlog_errno(status);
6267 goto bail_up;
6268 }
6269
6270 clusters = le32_to_cpu(tl->tl_recs[i].t_clusters);
6271 start_cluster = le32_to_cpu(tl->tl_recs[i].t_start);
6272 start_blk = ocfs2_clusters_to_blocks(osb->sb, start_cluster);
6273
6274 status = ocfs2_truncate_log_append(osb, handle,
6275 start_blk, clusters);
Mark Fasheh02dc1af2006-10-09 16:48:10 -07006276 ocfs2_commit_trans(osb, handle);
Mark Fashehccd979b2005-12-15 14:31:24 -08006277 if (status < 0) {
6278 mlog_errno(status);
6279 goto bail_up;
6280 }
6281 }
6282
6283bail_up:
Al Viro59551022016-01-22 15:40:57 -05006284 inode_unlock(tl_inode);
Mark Fashehccd979b2005-12-15 14:31:24 -08006285
Mark Fashehccd979b2005-12-15 14:31:24 -08006286 return status;
6287}
6288
6289void ocfs2_truncate_log_shutdown(struct ocfs2_super *osb)
6290{
6291 int status;
6292 struct inode *tl_inode = osb->osb_tl_inode;
6293
Xue jiufeia9e9aca2014-06-04 16:06:13 -07006294 atomic_set(&osb->osb_tl_disable, 1);
6295
Mark Fashehccd979b2005-12-15 14:31:24 -08006296 if (tl_inode) {
6297 cancel_delayed_work(&osb->osb_truncate_log_wq);
jiangyiwen35ddf782016-03-25 14:21:32 -07006298 flush_workqueue(osb->ocfs2_wq);
Mark Fashehccd979b2005-12-15 14:31:24 -08006299
6300 status = ocfs2_flush_truncate_log(osb);
6301 if (status < 0)
6302 mlog_errno(status);
6303
6304 brelse(osb->osb_tl_bh);
6305 iput(osb->osb_tl_inode);
6306 }
Mark Fashehccd979b2005-12-15 14:31:24 -08006307}
6308
6309int ocfs2_truncate_log_init(struct ocfs2_super *osb)
6310{
6311 int status;
6312 struct inode *tl_inode = NULL;
6313 struct buffer_head *tl_bh = NULL;
6314
Mark Fashehccd979b2005-12-15 14:31:24 -08006315 status = ocfs2_get_truncate_log_info(osb,
6316 osb->slot_num,
6317 &tl_inode,
6318 &tl_bh);
6319 if (status < 0)
6320 mlog_errno(status);
6321
6322 /* ocfs2_truncate_log_shutdown keys on the existence of
6323 * osb->osb_tl_inode so we don't set any of the osb variables
6324 * until we're sure all is well. */
David Howellsc4028952006-11-22 14:57:56 +00006325 INIT_DELAYED_WORK(&osb->osb_truncate_log_wq,
6326 ocfs2_truncate_log_worker);
Xue jiufeia9e9aca2014-06-04 16:06:13 -07006327 atomic_set(&osb->osb_tl_disable, 0);
Mark Fashehccd979b2005-12-15 14:31:24 -08006328 osb->osb_tl_bh = tl_bh;
6329 osb->osb_tl_inode = tl_inode;
6330
Mark Fashehccd979b2005-12-15 14:31:24 -08006331 return status;
6332}
6333
Mark Fasheh2b604352007-06-22 15:45:27 -07006334/*
6335 * Delayed de-allocation of suballocator blocks.
6336 *
6337 * Some sets of block de-allocations might involve multiple suballocator inodes.
6338 *
6339 * The locking for this can get extremely complicated, especially when
6340 * the suballocator inodes to delete from aren't known until deep
6341 * within an unrelated codepath.
6342 *
6343 * ocfs2_extent_block structures are a good example of this - an inode
6344 * btree could have been grown by any number of nodes each allocating
6345 * out of their own suballoc inode.
6346 *
6347 * These structures allow the delay of block de-allocation until a
6348 * later time, when locking of multiple cluster inodes won't cause
6349 * deadlock.
6350 */
6351
6352/*
Tao Ma2891d292008-11-12 08:26:58 +08006353 * Describe a single bit freed from a suballocator. For the block
6354 * suballocators, it represents one block. For the global cluster
6355 * allocator, it represents some clusters and free_bit indicates
6356 * clusters number.
Mark Fasheh2b604352007-06-22 15:45:27 -07006357 */
6358struct ocfs2_cached_block_free {
6359 struct ocfs2_cached_block_free *free_next;
Tao Ma74380c42010-03-22 14:20:18 +08006360 u64 free_bg;
Mark Fasheh2b604352007-06-22 15:45:27 -07006361 u64 free_blk;
6362 unsigned int free_bit;
6363};
6364
6365struct ocfs2_per_slot_free_list {
6366 struct ocfs2_per_slot_free_list *f_next_suballocator;
6367 int f_inode_type;
6368 int f_slot;
6369 struct ocfs2_cached_block_free *f_first;
6370};
6371
Tao Ma2891d292008-11-12 08:26:58 +08006372static int ocfs2_free_cached_blocks(struct ocfs2_super *osb,
6373 int sysfile_type,
6374 int slot,
6375 struct ocfs2_cached_block_free *head)
Mark Fasheh2b604352007-06-22 15:45:27 -07006376{
6377 int ret;
6378 u64 bg_blkno;
6379 handle_t *handle;
6380 struct inode *inode;
6381 struct buffer_head *di_bh = NULL;
6382 struct ocfs2_cached_block_free *tmp;
6383
6384 inode = ocfs2_get_system_file_inode(osb, sysfile_type, slot);
6385 if (!inode) {
6386 ret = -EINVAL;
6387 mlog_errno(ret);
6388 goto out;
6389 }
6390
Al Viro59551022016-01-22 15:40:57 -05006391 inode_lock(inode);
Mark Fasheh2b604352007-06-22 15:45:27 -07006392
Mark Fashehe63aecb62007-10-18 15:30:42 -07006393 ret = ocfs2_inode_lock(inode, &di_bh, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006394 if (ret) {
6395 mlog_errno(ret);
6396 goto out_mutex;
6397 }
6398
Mark Fasheh2b604352007-06-22 15:45:27 -07006399 while (head) {
Tao Ma74380c42010-03-22 14:20:18 +08006400 if (head->free_bg)
6401 bg_blkno = head->free_bg;
6402 else
6403 bg_blkno = ocfs2_which_suballoc_group(head->free_blk,
6404 head->free_bit);
Junxiao Bid5bf1412016-09-19 14:44:24 -07006405 handle = ocfs2_start_trans(osb, OCFS2_SUBALLOC_FREE);
6406 if (IS_ERR(handle)) {
6407 ret = PTR_ERR(handle);
6408 mlog_errno(ret);
6409 goto out_unlock;
6410 }
6411
Tao Maa09d09b2011-02-22 08:21:10 +08006412 trace_ocfs2_free_cached_blocks(
6413 (unsigned long long)head->free_blk, head->free_bit);
Mark Fasheh2b604352007-06-22 15:45:27 -07006414
6415 ret = ocfs2_free_suballoc_bits(handle, inode, di_bh,
6416 head->free_bit, bg_blkno, 1);
Junxiao Bid5bf1412016-09-19 14:44:24 -07006417 if (ret)
Mark Fasheh2b604352007-06-22 15:45:27 -07006418 mlog_errno(ret);
Mark Fasheh2b604352007-06-22 15:45:27 -07006419
Junxiao Bid5bf1412016-09-19 14:44:24 -07006420 ocfs2_commit_trans(osb, handle);
Mark Fasheh2b604352007-06-22 15:45:27 -07006421
6422 tmp = head;
6423 head = head->free_next;
6424 kfree(tmp);
6425 }
6426
Mark Fasheh2b604352007-06-22 15:45:27 -07006427out_unlock:
Mark Fashehe63aecb62007-10-18 15:30:42 -07006428 ocfs2_inode_unlock(inode, 1);
Mark Fasheh2b604352007-06-22 15:45:27 -07006429 brelse(di_bh);
6430out_mutex:
Al Viro59551022016-01-22 15:40:57 -05006431 inode_unlock(inode);
Mark Fasheh2b604352007-06-22 15:45:27 -07006432 iput(inode);
6433out:
6434 while(head) {
6435 /* Premature exit may have left some dangling items. */
6436 tmp = head;
6437 head = head->free_next;
6438 kfree(tmp);
6439 }
6440
6441 return ret;
6442}
6443
Tao Ma2891d292008-11-12 08:26:58 +08006444int ocfs2_cache_cluster_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
6445 u64 blkno, unsigned int bit)
6446{
6447 int ret = 0;
6448 struct ocfs2_cached_block_free *item;
6449
Tao Ma74380c42010-03-22 14:20:18 +08006450 item = kzalloc(sizeof(*item), GFP_NOFS);
Tao Ma2891d292008-11-12 08:26:58 +08006451 if (item == NULL) {
6452 ret = -ENOMEM;
6453 mlog_errno(ret);
6454 return ret;
6455 }
6456
Tao Maa09d09b2011-02-22 08:21:10 +08006457 trace_ocfs2_cache_cluster_dealloc((unsigned long long)blkno, bit);
Tao Ma2891d292008-11-12 08:26:58 +08006458
6459 item->free_blk = blkno;
6460 item->free_bit = bit;
6461 item->free_next = ctxt->c_global_allocator;
6462
6463 ctxt->c_global_allocator = item;
6464 return ret;
6465}
6466
6467static int ocfs2_free_cached_clusters(struct ocfs2_super *osb,
6468 struct ocfs2_cached_block_free *head)
6469{
6470 struct ocfs2_cached_block_free *tmp;
6471 struct inode *tl_inode = osb->osb_tl_inode;
6472 handle_t *handle;
6473 int ret = 0;
6474
Al Viro59551022016-01-22 15:40:57 -05006475 inode_lock(tl_inode);
Tao Ma2891d292008-11-12 08:26:58 +08006476
6477 while (head) {
6478 if (ocfs2_truncate_log_needs_flush(osb)) {
6479 ret = __ocfs2_flush_truncate_log(osb);
6480 if (ret < 0) {
6481 mlog_errno(ret);
6482 break;
6483 }
6484 }
6485
6486 handle = ocfs2_start_trans(osb, OCFS2_TRUNCATE_LOG_UPDATE);
6487 if (IS_ERR(handle)) {
6488 ret = PTR_ERR(handle);
6489 mlog_errno(ret);
6490 break;
6491 }
6492
6493 ret = ocfs2_truncate_log_append(osb, handle, head->free_blk,
6494 head->free_bit);
6495
6496 ocfs2_commit_trans(osb, handle);
6497 tmp = head;
6498 head = head->free_next;
6499 kfree(tmp);
6500
6501 if (ret < 0) {
6502 mlog_errno(ret);
6503 break;
6504 }
6505 }
6506
Al Viro59551022016-01-22 15:40:57 -05006507 inode_unlock(tl_inode);
Tao Ma2891d292008-11-12 08:26:58 +08006508
6509 while (head) {
6510 /* Premature exit may have left some dangling items. */
6511 tmp = head;
6512 head = head->free_next;
6513 kfree(tmp);
6514 }
6515
6516 return ret;
6517}
6518
Mark Fasheh2b604352007-06-22 15:45:27 -07006519int ocfs2_run_deallocs(struct ocfs2_super *osb,
6520 struct ocfs2_cached_dealloc_ctxt *ctxt)
6521{
6522 int ret = 0, ret2;
6523 struct ocfs2_per_slot_free_list *fl;
6524
6525 if (!ctxt)
6526 return 0;
6527
6528 while (ctxt->c_first_suballocator) {
6529 fl = ctxt->c_first_suballocator;
6530
6531 if (fl->f_first) {
Tao Maa09d09b2011-02-22 08:21:10 +08006532 trace_ocfs2_run_deallocs(fl->f_inode_type,
6533 fl->f_slot);
Tao Ma2891d292008-11-12 08:26:58 +08006534 ret2 = ocfs2_free_cached_blocks(osb,
6535 fl->f_inode_type,
6536 fl->f_slot,
6537 fl->f_first);
Mark Fasheh2b604352007-06-22 15:45:27 -07006538 if (ret2)
6539 mlog_errno(ret2);
6540 if (!ret)
6541 ret = ret2;
6542 }
6543
6544 ctxt->c_first_suballocator = fl->f_next_suballocator;
6545 kfree(fl);
6546 }
6547
Tao Ma2891d292008-11-12 08:26:58 +08006548 if (ctxt->c_global_allocator) {
6549 ret2 = ocfs2_free_cached_clusters(osb,
6550 ctxt->c_global_allocator);
6551 if (ret2)
6552 mlog_errno(ret2);
6553 if (!ret)
6554 ret = ret2;
6555
6556 ctxt->c_global_allocator = NULL;
6557 }
6558
Mark Fasheh2b604352007-06-22 15:45:27 -07006559 return ret;
6560}
6561
6562static struct ocfs2_per_slot_free_list *
6563ocfs2_find_per_slot_free_list(int type,
6564 int slot,
6565 struct ocfs2_cached_dealloc_ctxt *ctxt)
6566{
6567 struct ocfs2_per_slot_free_list *fl = ctxt->c_first_suballocator;
6568
6569 while (fl) {
6570 if (fl->f_inode_type == type && fl->f_slot == slot)
6571 return fl;
6572
6573 fl = fl->f_next_suballocator;
6574 }
6575
6576 fl = kmalloc(sizeof(*fl), GFP_NOFS);
6577 if (fl) {
6578 fl->f_inode_type = type;
6579 fl->f_slot = slot;
6580 fl->f_first = NULL;
6581 fl->f_next_suballocator = ctxt->c_first_suballocator;
6582
6583 ctxt->c_first_suballocator = fl;
6584 }
6585 return fl;
6586}
6587
Tao Ma1823cb02009-08-18 11:24:49 +08006588int ocfs2_cache_block_dealloc(struct ocfs2_cached_dealloc_ctxt *ctxt,
Tao Ma74380c42010-03-22 14:20:18 +08006589 int type, int slot, u64 suballoc,
6590 u64 blkno, unsigned int bit)
Mark Fasheh2b604352007-06-22 15:45:27 -07006591{
6592 int ret;
6593 struct ocfs2_per_slot_free_list *fl;
6594 struct ocfs2_cached_block_free *item;
6595
6596 fl = ocfs2_find_per_slot_free_list(type, slot, ctxt);
6597 if (fl == NULL) {
6598 ret = -ENOMEM;
6599 mlog_errno(ret);
6600 goto out;
6601 }
6602
Tao Ma74380c42010-03-22 14:20:18 +08006603 item = kzalloc(sizeof(*item), GFP_NOFS);
Mark Fasheh2b604352007-06-22 15:45:27 -07006604 if (item == NULL) {
6605 ret = -ENOMEM;
6606 mlog_errno(ret);
6607 goto out;
6608 }
6609
Tao Maa09d09b2011-02-22 08:21:10 +08006610 trace_ocfs2_cache_block_dealloc(type, slot,
6611 (unsigned long long)suballoc,
6612 (unsigned long long)blkno, bit);
Mark Fasheh2b604352007-06-22 15:45:27 -07006613
Tao Ma74380c42010-03-22 14:20:18 +08006614 item->free_bg = suballoc;
Mark Fasheh2b604352007-06-22 15:45:27 -07006615 item->free_blk = blkno;
6616 item->free_bit = bit;
6617 item->free_next = fl->f_first;
6618
6619 fl->f_first = item;
6620
6621 ret = 0;
6622out:
6623 return ret;
6624}
6625
Mark Fasheh59a5e412007-06-22 15:52:36 -07006626static int ocfs2_cache_extent_block_free(struct ocfs2_cached_dealloc_ctxt *ctxt,
6627 struct ocfs2_extent_block *eb)
6628{
6629 return ocfs2_cache_block_dealloc(ctxt, EXTENT_ALLOC_SYSTEM_INODE,
6630 le16_to_cpu(eb->h_suballoc_slot),
Tao Ma74380c42010-03-22 14:20:18 +08006631 le64_to_cpu(eb->h_suballoc_loc),
Mark Fasheh59a5e412007-06-22 15:52:36 -07006632 le64_to_cpu(eb->h_blkno),
6633 le16_to_cpu(eb->h_suballoc_bit));
6634}
6635
Joel Becker2b4e30f2008-09-03 20:03:41 -07006636static int ocfs2_zero_func(handle_t *handle, struct buffer_head *bh)
Mark Fasheh60b11392007-02-16 11:46:50 -08006637{
6638 set_buffer_uptodate(bh);
6639 mark_buffer_dirty(bh);
6640 return 0;
6641}
6642
Tao Ma6f70fa52009-08-25 08:05:12 +08006643void ocfs2_map_and_dirty_page(struct inode *inode, handle_t *handle,
6644 unsigned int from, unsigned int to,
6645 struct page *page, int zero, u64 *phys)
Mark Fasheh1d410a62007-09-07 14:20:45 -07006646{
6647 int ret, partial = 0;
6648
6649 ret = ocfs2_map_page_blocks(page, phys, inode, from, to, 0);
6650 if (ret)
6651 mlog_errno(ret);
6652
6653 if (zero)
Christoph Lametereebd2aa2008-02-04 22:28:29 -08006654 zero_user_segment(page, from, to);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006655
6656 /*
6657 * Need to set the buffers we zero'd into uptodate
6658 * here if they aren't - ocfs2_map_page_blocks()
6659 * might've skipped some
6660 */
Joel Becker2b4e30f2008-09-03 20:03:41 -07006661 ret = walk_page_buffers(handle, page_buffers(page),
6662 from, to, &partial,
6663 ocfs2_zero_func);
6664 if (ret < 0)
6665 mlog_errno(ret);
6666 else if (ocfs2_should_order_data(inode)) {
6667 ret = ocfs2_jbd2_file_inode(handle, inode);
Mark Fasheh1d410a62007-09-07 14:20:45 -07006668 if (ret < 0)
6669 mlog_errno(ret);
6670 }
6671
6672 if (!partial)
6673 SetPageUptodate(page);
6674
6675 flush_dcache_page(page);
6676}
6677
Mark Fasheh35edec12007-07-06 14:41:18 -07006678static void ocfs2_zero_cluster_pages(struct inode *inode, loff_t start,
6679 loff_t end, struct page **pages,
6680 int numpages, u64 phys, handle_t *handle)
Mark Fasheh60b11392007-02-16 11:46:50 -08006681{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006682 int i;
Mark Fasheh60b11392007-02-16 11:46:50 -08006683 struct page *page;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006684 unsigned int from, to = PAGE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006685 struct super_block *sb = inode->i_sb;
6686
6687 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(sb)));
6688
6689 if (numpages == 0)
6690 goto out;
6691
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006692 to = PAGE_SIZE;
Mark Fasheh60b11392007-02-16 11:46:50 -08006693 for(i = 0; i < numpages; i++) {
6694 page = pages[i];
6695
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006696 from = start & (PAGE_SIZE - 1);
6697 if ((end >> PAGE_SHIFT) == page->index)
6698 to = end & (PAGE_SIZE - 1);
Mark Fasheh35edec12007-07-06 14:41:18 -07006699
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006700 BUG_ON(from > PAGE_SIZE);
6701 BUG_ON(to > PAGE_SIZE);
Mark Fasheh60b11392007-02-16 11:46:50 -08006702
Mark Fasheh1d410a62007-09-07 14:20:45 -07006703 ocfs2_map_and_dirty_page(inode, handle, from, to, page, 1,
6704 &phys);
Mark Fasheh60b11392007-02-16 11:46:50 -08006705
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006706 start = (page->index + 1) << PAGE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006707 }
6708out:
Mark Fasheh1d410a62007-09-07 14:20:45 -07006709 if (pages)
6710 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006711}
6712
Tao Ma6f70fa52009-08-25 08:05:12 +08006713int ocfs2_grab_pages(struct inode *inode, loff_t start, loff_t end,
6714 struct page **pages, int *num)
Mark Fasheh60b11392007-02-16 11:46:50 -08006715{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006716 int numpages, ret = 0;
Mark Fasheh60b11392007-02-16 11:46:50 -08006717 struct address_space *mapping = inode->i_mapping;
6718 unsigned long index;
Mark Fasheh35edec12007-07-06 14:41:18 -07006719 loff_t last_page_bytes;
Mark Fasheh60b11392007-02-16 11:46:50 -08006720
Mark Fasheh35edec12007-07-06 14:41:18 -07006721 BUG_ON(start > end);
Mark Fasheh60b11392007-02-16 11:46:50 -08006722
Mark Fasheh1d410a62007-09-07 14:20:45 -07006723 numpages = 0;
Mark Fasheh35edec12007-07-06 14:41:18 -07006724 last_page_bytes = PAGE_ALIGN(end);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006725 index = start >> PAGE_SHIFT;
Mark Fasheh60b11392007-02-16 11:46:50 -08006726 do {
Jan Kara9b4c0ff2010-08-24 14:28:03 +02006727 pages[numpages] = find_or_create_page(mapping, index, GFP_NOFS);
Mark Fasheh60b11392007-02-16 11:46:50 -08006728 if (!pages[numpages]) {
6729 ret = -ENOMEM;
6730 mlog_errno(ret);
6731 goto out;
6732 }
6733
6734 numpages++;
6735 index++;
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006736 } while (index < (last_page_bytes >> PAGE_SHIFT));
Mark Fasheh60b11392007-02-16 11:46:50 -08006737
6738out:
6739 if (ret != 0) {
Mark Fasheh1d410a62007-09-07 14:20:45 -07006740 if (pages)
6741 ocfs2_unlock_and_free_pages(pages, numpages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006742 numpages = 0;
6743 }
6744
6745 *num = numpages;
6746
6747 return ret;
6748}
6749
Tao Ma6f70fa52009-08-25 08:05:12 +08006750static int ocfs2_grab_eof_pages(struct inode *inode, loff_t start, loff_t end,
6751 struct page **pages, int *num)
6752{
6753 struct super_block *sb = inode->i_sb;
6754
6755 BUG_ON(start >> OCFS2_SB(sb)->s_clustersize_bits !=
6756 (end - 1) >> OCFS2_SB(sb)->s_clustersize_bits);
6757
6758 return ocfs2_grab_pages(inode, start, end, pages, num);
6759}
6760
Mark Fasheh60b11392007-02-16 11:46:50 -08006761/*
6762 * Zero the area past i_size but still within an allocated
6763 * cluster. This avoids exposing nonzero data on subsequent file
6764 * extends.
6765 *
6766 * We need to call this before i_size is updated on the inode because
6767 * otherwise block_write_full_page() will skip writeout of pages past
6768 * i_size. The new_i_size parameter is passed for this reason.
6769 */
Mark Fasheh35edec12007-07-06 14:41:18 -07006770int ocfs2_zero_range_for_truncate(struct inode *inode, handle_t *handle,
6771 u64 range_start, u64 range_end)
Mark Fasheh60b11392007-02-16 11:46:50 -08006772{
Mark Fasheh1d410a62007-09-07 14:20:45 -07006773 int ret = 0, numpages;
Mark Fasheh60b11392007-02-16 11:46:50 -08006774 struct page **pages = NULL;
6775 u64 phys;
Mark Fasheh1d410a62007-09-07 14:20:45 -07006776 unsigned int ext_flags;
6777 struct super_block *sb = inode->i_sb;
Mark Fasheh60b11392007-02-16 11:46:50 -08006778
6779 /*
6780 * File systems which don't support sparse files zero on every
6781 * extend.
6782 */
Mark Fasheh1d410a62007-09-07 14:20:45 -07006783 if (!ocfs2_sparse_alloc(OCFS2_SB(sb)))
Mark Fasheh60b11392007-02-16 11:46:50 -08006784 return 0;
6785
Mark Fasheh1d410a62007-09-07 14:20:45 -07006786 pages = kcalloc(ocfs2_pages_per_cluster(sb),
Mark Fasheh60b11392007-02-16 11:46:50 -08006787 sizeof(struct page *), GFP_NOFS);
6788 if (pages == NULL) {
6789 ret = -ENOMEM;
6790 mlog_errno(ret);
6791 goto out;
6792 }
6793
Mark Fasheh1d410a62007-09-07 14:20:45 -07006794 if (range_start == range_end)
6795 goto out;
6796
6797 ret = ocfs2_extent_map_get_blocks(inode,
6798 range_start >> sb->s_blocksize_bits,
6799 &phys, NULL, &ext_flags);
Mark Fasheh60b11392007-02-16 11:46:50 -08006800 if (ret) {
6801 mlog_errno(ret);
6802 goto out;
6803 }
6804
Mark Fasheh1d410a62007-09-07 14:20:45 -07006805 /*
6806 * Tail is a hole, or is marked unwritten. In either case, we
6807 * can count on read and write to return/push zero's.
6808 */
6809 if (phys == 0 || ext_flags & OCFS2_EXT_UNWRITTEN)
Mark Fasheh60b11392007-02-16 11:46:50 -08006810 goto out;
6811
Mark Fasheh1d410a62007-09-07 14:20:45 -07006812 ret = ocfs2_grab_eof_pages(inode, range_start, range_end, pages,
6813 &numpages);
6814 if (ret) {
6815 mlog_errno(ret);
6816 goto out;
6817 }
6818
Mark Fasheh35edec12007-07-06 14:41:18 -07006819 ocfs2_zero_cluster_pages(inode, range_start, range_end, pages,
6820 numpages, phys, handle);
Mark Fasheh60b11392007-02-16 11:46:50 -08006821
6822 /*
6823 * Initiate writeout of the pages we zero'd here. We don't
6824 * wait on them - the truncate_inode_pages() call later will
6825 * do that for us.
6826 */
Christoph Hellwig2cfd30a2009-09-23 15:04:02 +02006827 ret = filemap_fdatawrite_range(inode->i_mapping, range_start,
6828 range_end - 1);
Mark Fasheh60b11392007-02-16 11:46:50 -08006829 if (ret)
6830 mlog_errno(ret);
6831
6832out:
Tim Gardnerd787ab02013-02-21 16:42:44 -08006833 kfree(pages);
Mark Fasheh60b11392007-02-16 11:46:50 -08006834
6835 return ret;
6836}
6837
Tiger Yangfdd77702008-08-18 17:08:55 +08006838static void ocfs2_zero_dinode_id2_with_xattr(struct inode *inode,
6839 struct ocfs2_dinode *di)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006840{
6841 unsigned int blocksize = 1 << inode->i_sb->s_blocksize_bits;
Tiger Yangfdd77702008-08-18 17:08:55 +08006842 unsigned int xattrsize = le16_to_cpu(di->i_xattr_inline_size);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006843
Tiger Yangfdd77702008-08-18 17:08:55 +08006844 if (le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_XATTR_FL)
6845 memset(&di->id2, 0, blocksize -
6846 offsetof(struct ocfs2_dinode, id2) -
6847 xattrsize);
6848 else
6849 memset(&di->id2, 0, blocksize -
6850 offsetof(struct ocfs2_dinode, id2));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006851}
6852
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006853void ocfs2_dinode_new_extent_list(struct inode *inode,
6854 struct ocfs2_dinode *di)
6855{
Tiger Yangfdd77702008-08-18 17:08:55 +08006856 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006857 di->id2.i_list.l_tree_depth = 0;
6858 di->id2.i_list.l_next_free_rec = 0;
Tiger Yangfdd77702008-08-18 17:08:55 +08006859 di->id2.i_list.l_count = cpu_to_le16(
6860 ocfs2_extent_recs_per_inode_with_xattr(inode->i_sb, di));
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006861}
6862
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006863void ocfs2_set_inode_data_inline(struct inode *inode, struct ocfs2_dinode *di)
6864{
6865 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6866 struct ocfs2_inline_data *idata = &di->id2.i_data;
6867
6868 spin_lock(&oi->ip_lock);
6869 oi->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
6870 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6871 spin_unlock(&oi->ip_lock);
6872
6873 /*
6874 * We clear the entire i_data structure here so that all
6875 * fields can be properly initialized.
6876 */
Tiger Yangfdd77702008-08-18 17:08:55 +08006877 ocfs2_zero_dinode_id2_with_xattr(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006878
Tiger Yangfdd77702008-08-18 17:08:55 +08006879 idata->id_count = cpu_to_le16(
6880 ocfs2_max_inline_data_with_xattr(inode->i_sb, di));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006881}
6882
6883int ocfs2_convert_inline_data_to_extents(struct inode *inode,
6884 struct buffer_head *di_bh)
6885{
6886 int ret, i, has_data, num_pages = 0;
Zongxun Wangfb951eb2014-02-06 12:04:20 -08006887 int need_free = 0;
6888 u32 bit_off, num;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006889 handle_t *handle;
6890 u64 uninitialized_var(block);
6891 struct ocfs2_inode_info *oi = OCFS2_I(inode);
6892 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
6893 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006894 struct ocfs2_alloc_context *data_ac = NULL;
6895 struct page **pages = NULL;
6896 loff_t end = osb->s_clustersize;
Joel Beckerf99b9b72008-08-20 19:36:33 -07006897 struct ocfs2_extent_tree et;
Jan Karaa90714c2008-10-09 19:38:40 +02006898 int did_quota = 0;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006899
6900 has_data = i_size_read(inode) ? 1 : 0;
6901
6902 if (has_data) {
6903 pages = kcalloc(ocfs2_pages_per_cluster(osb->sb),
6904 sizeof(struct page *), GFP_NOFS);
6905 if (pages == NULL) {
6906 ret = -ENOMEM;
6907 mlog_errno(ret);
Markus Elfring3cc79b72015-04-14 15:42:45 -07006908 return ret;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006909 }
6910
6911 ret = ocfs2_reserve_clusters(osb, 1, &data_ac);
6912 if (ret) {
6913 mlog_errno(ret);
Markus Elfring3cc79b72015-04-14 15:42:45 -07006914 goto free_pages;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006915 }
6916 }
6917
Jan Karaa90714c2008-10-09 19:38:40 +02006918 handle = ocfs2_start_trans(osb,
6919 ocfs2_inline_to_extents_credits(osb->sb));
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006920 if (IS_ERR(handle)) {
6921 ret = PTR_ERR(handle);
6922 mlog_errno(ret);
alex chen15eba0f2015-02-10 14:09:02 -08006923 goto out;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006924 }
6925
Joel Becker0cf2f762009-02-12 16:41:25 -08006926 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07006927 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006928 if (ret) {
6929 mlog_errno(ret);
6930 goto out_commit;
6931 }
6932
6933 if (has_data) {
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006934 unsigned int page_end;
6935 u64 phys;
6936
Christoph Hellwig5dd40562010-03-03 09:05:00 -05006937 ret = dquot_alloc_space_nodirty(inode,
6938 ocfs2_clusters_to_bytes(osb->sb, 1));
6939 if (ret)
Jan Karaa90714c2008-10-09 19:38:40 +02006940 goto out_commit;
Jan Karaa90714c2008-10-09 19:38:40 +02006941 did_quota = 1;
6942
Mark Fasheh4fe370a2009-12-07 13:15:40 -08006943 data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
6944
Joel Becker1ed9b772010-05-06 13:59:06 +08006945 ret = ocfs2_claim_clusters(handle, data_ac, 1, &bit_off,
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006946 &num);
6947 if (ret) {
6948 mlog_errno(ret);
6949 goto out_commit;
6950 }
6951
6952 /*
6953 * Save two copies, one for insert, and one that can
6954 * be changed by ocfs2_map_and_dirty_page() below.
6955 */
6956 block = phys = ocfs2_clusters_to_blocks(inode->i_sb, bit_off);
6957
6958 /*
6959 * Non sparse file systems zero on extend, so no need
6960 * to do that now.
6961 */
6962 if (!ocfs2_sparse_alloc(osb) &&
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006963 PAGE_SIZE < osb->s_clustersize)
6964 end = PAGE_SIZE;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006965
6966 ret = ocfs2_grab_eof_pages(inode, 0, end, pages, &num_pages);
6967 if (ret) {
6968 mlog_errno(ret);
Zongxun Wangfb951eb2014-02-06 12:04:20 -08006969 need_free = 1;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006970 goto out_commit;
6971 }
6972
6973 /*
6974 * This should populate the 1st page for us and mark
6975 * it up to date.
6976 */
6977 ret = ocfs2_read_inline_data(inode, pages[0], di_bh);
6978 if (ret) {
6979 mlog_errno(ret);
Zongxun Wangfb951eb2014-02-06 12:04:20 -08006980 need_free = 1;
alex chen15eba0f2015-02-10 14:09:02 -08006981 goto out_unlock;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006982 }
6983
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03006984 page_end = PAGE_SIZE;
6985 if (PAGE_SIZE > osb->s_clustersize)
Mark Fasheh1afc32b2007-09-07 14:46:51 -07006986 page_end = osb->s_clustersize;
6987
6988 for (i = 0; i < num_pages; i++)
6989 ocfs2_map_and_dirty_page(inode, handle, 0, page_end,
6990 pages[i], i > 0, &phys);
6991 }
6992
6993 spin_lock(&oi->ip_lock);
6994 oi->ip_dyn_features &= ~OCFS2_INLINE_DATA_FL;
6995 di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6996 spin_unlock(&oi->ip_lock);
6997
Darrick J. Wong2931cdc2014-04-03 14:46:48 -07006998 ocfs2_update_inode_fsync_trans(handle, inode, 1);
Mark Fasheh5b6a3a22007-09-13 16:33:54 -07006999 ocfs2_dinode_new_extent_list(inode, di);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007000
7001 ocfs2_journal_dirty(handle, di_bh);
7002
7003 if (has_data) {
7004 /*
7005 * An error at this point should be extremely rare. If
7006 * this proves to be false, we could always re-build
7007 * the in-inode data from our pages.
7008 */
Joel Becker5e404e92009-02-13 03:54:22 -08007009 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
Joel Beckercc79d8c2009-02-13 03:24:43 -08007010 ret = ocfs2_insert_extent(handle, &et, 0, block, 1, 0, NULL);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007011 if (ret) {
7012 mlog_errno(ret);
Zongxun Wangfb951eb2014-02-06 12:04:20 -08007013 need_free = 1;
alex chen15eba0f2015-02-10 14:09:02 -08007014 goto out_unlock;
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007015 }
7016
7017 inode->i_blocks = ocfs2_inode_sector_count(inode);
7018 }
7019
alex chen15eba0f2015-02-10 14:09:02 -08007020out_unlock:
7021 if (pages)
7022 ocfs2_unlock_and_free_pages(pages, num_pages);
7023
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007024out_commit:
Jan Karaa90714c2008-10-09 19:38:40 +02007025 if (ret < 0 && did_quota)
Christoph Hellwig5dd40562010-03-03 09:05:00 -05007026 dquot_free_space_nodirty(inode,
Jan Karaa90714c2008-10-09 19:38:40 +02007027 ocfs2_clusters_to_bytes(osb->sb, 1));
7028
Zongxun Wangfb951eb2014-02-06 12:04:20 -08007029 if (need_free) {
7030 if (data_ac->ac_which == OCFS2_AC_USE_LOCAL)
7031 ocfs2_free_local_alloc_bits(osb, handle, data_ac,
7032 bit_off, num);
7033 else
7034 ocfs2_free_clusters(handle,
7035 data_ac->ac_inode,
7036 data_ac->ac_bh,
7037 ocfs2_clusters_to_blocks(osb->sb, bit_off),
7038 num);
7039 }
7040
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007041 ocfs2_commit_trans(osb, handle);
7042
alex chen15eba0f2015-02-10 14:09:02 -08007043out:
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007044 if (data_ac)
7045 ocfs2_free_alloc_context(data_ac);
Markus Elfring3cc79b72015-04-14 15:42:45 -07007046free_pages:
Markus Elfringfd90d4df2015-04-14 15:42:42 -07007047 kfree(pages);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007048 return ret;
7049}
7050
Mark Fashehccd979b2005-12-15 14:31:24 -08007051/*
7052 * It is expected, that by the time you call this function,
7053 * inode->i_size and fe->i_size have been adjusted.
7054 *
7055 * WARNING: This will kfree the truncate context
7056 */
7057int ocfs2_commit_truncate(struct ocfs2_super *osb,
7058 struct inode *inode,
Tristan Ye78f94672010-05-11 17:54:42 +08007059 struct buffer_head *di_bh)
Mark Fashehccd979b2005-12-15 14:31:24 -08007060{
Tristan Ye78f94672010-05-11 17:54:42 +08007061 int status = 0, i, flags = 0;
7062 u32 new_highest_cpos, range, trunc_cpos, trunc_len, phys_cpos, coff;
Tao Mabcbbb242009-08-18 11:29:12 +08007063 u64 blkno = 0;
Mark Fashehccd979b2005-12-15 14:31:24 -08007064 struct ocfs2_extent_list *el;
Tristan Ye78f94672010-05-11 17:54:42 +08007065 struct ocfs2_extent_rec *rec;
Mark Fashehdcd05382007-01-16 11:32:23 -08007066 struct ocfs2_path *path = NULL;
Tristan Ye78f94672010-05-11 17:54:42 +08007067 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7068 struct ocfs2_extent_list *root_el = &(di->id2.i_list);
7069 u64 refcount_loc = le64_to_cpu(di->i_refcount_loc);
7070 struct ocfs2_extent_tree et;
7071 struct ocfs2_cached_dealloc_ctxt dealloc;
Junxiao Bif62f12b2014-12-18 16:17:32 -08007072 struct ocfs2_refcount_tree *ref_tree = NULL;
Mark Fashehccd979b2005-12-15 14:31:24 -08007073
Tristan Ye78f94672010-05-11 17:54:42 +08007074 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
7075 ocfs2_init_dealloc_ctxt(&dealloc);
7076
Mark Fashehdcd05382007-01-16 11:32:23 -08007077 new_highest_cpos = ocfs2_clusters_for_bytes(osb->sb,
Mark Fashehccd979b2005-12-15 14:31:24 -08007078 i_size_read(inode));
7079
Tristan Ye78f94672010-05-11 17:54:42 +08007080 path = ocfs2_new_path(di_bh, &di->id2.i_list,
Joel Becker13723d02008-10-17 19:25:01 -07007081 ocfs2_journal_access_di);
Mark Fashehdcd05382007-01-16 11:32:23 -08007082 if (!path) {
7083 status = -ENOMEM;
7084 mlog_errno(status);
7085 goto bail;
7086 }
Mark Fasheh83418972007-04-23 18:53:12 -07007087
7088 ocfs2_extent_map_trunc(inode, new_highest_cpos);
7089
Mark Fashehccd979b2005-12-15 14:31:24 -08007090start:
Mark Fashehdcd05382007-01-16 11:32:23 -08007091 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007092 * Check that we still have allocation to delete.
7093 */
7094 if (OCFS2_I(inode)->ip_clusters == 0) {
7095 status = 0;
7096 goto bail;
7097 }
7098
7099 /*
Mark Fashehdcd05382007-01-16 11:32:23 -08007100 * Truncate always works against the rightmost tree branch.
7101 */
Joel Beckerfacdb772009-02-12 18:08:48 -08007102 status = ocfs2_find_path(INODE_CACHE(inode), path, UINT_MAX);
Mark Fashehdcd05382007-01-16 11:32:23 -08007103 if (status) {
7104 mlog_errno(status);
7105 goto bail;
Mark Fashehccd979b2005-12-15 14:31:24 -08007106 }
7107
Tao Maa09d09b2011-02-22 08:21:10 +08007108 trace_ocfs2_commit_truncate(
7109 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7110 new_highest_cpos,
7111 OCFS2_I(inode)->ip_clusters,
7112 path->p_tree_depth);
Mark Fashehdcd05382007-01-16 11:32:23 -08007113
7114 /*
7115 * By now, el will point to the extent list on the bottom most
7116 * portion of this tree. Only the tail record is considered in
7117 * each pass.
7118 *
7119 * We handle the following cases, in order:
7120 * - empty extent: delete the remaining branch
7121 * - remove the entire record
7122 * - remove a partial record
7123 * - no record needs to be removed (truncate has completed)
7124 */
7125 el = path_leaf_el(path);
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007126 if (le16_to_cpu(el->l_next_free_rec) == 0) {
7127 ocfs2_error(inode->i_sb,
7128 "Inode %llu has empty extent block at %llu\n",
7129 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7130 (unsigned long long)path_leaf_bh(path)->b_blocknr);
7131 status = -EROFS;
7132 goto bail;
7133 }
7134
Mark Fashehccd979b2005-12-15 14:31:24 -08007135 i = le16_to_cpu(el->l_next_free_rec) - 1;
Tristan Ye78f94672010-05-11 17:54:42 +08007136 rec = &el->l_recs[i];
7137 flags = rec->e_flags;
7138 range = le32_to_cpu(rec->e_cpos) + ocfs2_rec_clusters(el, rec);
7139
7140 if (i == 0 && ocfs2_is_empty_extent(rec)) {
7141 /*
7142 * Lower levels depend on this never happening, but it's best
7143 * to check it up here before changing the tree.
7144 */
7145 if (root_el->l_tree_depth && rec->e_int_clusters == 0) {
Xue jiufeid0c97d52015-09-04 15:44:48 -07007146 mlog(ML_ERROR, "Inode %lu has an empty "
Tristan Ye78f94672010-05-11 17:54:42 +08007147 "extent record, depth %u\n", inode->i_ino,
7148 le16_to_cpu(root_el->l_tree_depth));
Xue jiufeid0c97d52015-09-04 15:44:48 -07007149 status = ocfs2_remove_rightmost_empty_extent(osb,
7150 &et, path, &dealloc);
7151 if (status) {
7152 mlog_errno(status);
7153 goto bail;
7154 }
7155
7156 ocfs2_reinit_path(path, 1);
7157 goto start;
7158 } else {
7159 trunc_cpos = le32_to_cpu(rec->e_cpos);
7160 trunc_len = 0;
7161 blkno = 0;
Tristan Ye78f94672010-05-11 17:54:42 +08007162 }
Tristan Ye78f94672010-05-11 17:54:42 +08007163 } else if (le32_to_cpu(rec->e_cpos) >= new_highest_cpos) {
7164 /*
7165 * Truncate entire record.
7166 */
7167 trunc_cpos = le32_to_cpu(rec->e_cpos);
7168 trunc_len = ocfs2_rec_clusters(el, rec);
7169 blkno = le64_to_cpu(rec->e_blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08007170 } else if (range > new_highest_cpos) {
Tristan Ye78f94672010-05-11 17:54:42 +08007171 /*
7172 * Partial truncate. it also should be
7173 * the last truncate we're doing.
7174 */
7175 trunc_cpos = new_highest_cpos;
7176 trunc_len = range - new_highest_cpos;
7177 coff = new_highest_cpos - le32_to_cpu(rec->e_cpos);
7178 blkno = le64_to_cpu(rec->e_blkno) +
7179 ocfs2_clusters_to_blocks(inode->i_sb, coff);
Mark Fashehdcd05382007-01-16 11:32:23 -08007180 } else {
Tristan Ye78f94672010-05-11 17:54:42 +08007181 /*
7182 * Truncate completed, leave happily.
7183 */
Mark Fashehdcd05382007-01-16 11:32:23 -08007184 status = 0;
7185 goto bail;
7186 }
Mark Fashehccd979b2005-12-15 14:31:24 -08007187
Tristan Ye78f94672010-05-11 17:54:42 +08007188 phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, blkno);
Mark Fashehdcd05382007-01-16 11:32:23 -08007189
Junxiao Bif62f12b2014-12-18 16:17:32 -08007190 if ((flags & OCFS2_EXT_REFCOUNTED) && trunc_len && !ref_tree) {
7191 status = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
7192 &ref_tree, NULL);
7193 if (status) {
7194 mlog_errno(status);
7195 goto bail;
7196 }
7197 }
7198
Tristan Ye78f94672010-05-11 17:54:42 +08007199 status = ocfs2_remove_btree_range(inode, &et, trunc_cpos,
7200 phys_cpos, trunc_len, flags, &dealloc,
Junxiao Bif62f12b2014-12-18 16:17:32 -08007201 refcount_loc, true);
Mark Fashehccd979b2005-12-15 14:31:24 -08007202 if (status < 0) {
7203 mlog_errno(status);
7204 goto bail;
7205 }
7206
Mark Fashehdcd05382007-01-16 11:32:23 -08007207 ocfs2_reinit_path(path, 1);
7208
7209 /*
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007210 * The check above will catch the case where we've truncated
7211 * away all allocation.
Mark Fashehdcd05382007-01-16 11:32:23 -08007212 */
Mark Fasheh3a0782d2007-01-17 12:53:31 -08007213 goto start;
7214
Mark Fashehccd979b2005-12-15 14:31:24 -08007215bail:
Junxiao Bif62f12b2014-12-18 16:17:32 -08007216 if (ref_tree)
7217 ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
Mark Fashehccd979b2005-12-15 14:31:24 -08007218
7219 ocfs2_schedule_truncate_log_flush(osb, 1);
7220
Tristan Ye78f94672010-05-11 17:54:42 +08007221 ocfs2_run_deallocs(osb, &dealloc);
Mark Fasheh59a5e412007-06-22 15:52:36 -07007222
Mark Fashehdcd05382007-01-16 11:32:23 -08007223 ocfs2_free_path(path);
Mark Fashehccd979b2005-12-15 14:31:24 -08007224
Mark Fashehccd979b2005-12-15 14:31:24 -08007225 return status;
7226}
7227
Mark Fashehccd979b2005-12-15 14:31:24 -08007228/*
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007229 * 'start' is inclusive, 'end' is not.
7230 */
7231int ocfs2_truncate_inline(struct inode *inode, struct buffer_head *di_bh,
7232 unsigned int start, unsigned int end, int trunc)
7233{
7234 int ret;
7235 unsigned int numbytes;
7236 handle_t *handle;
7237 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7238 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7239 struct ocfs2_inline_data *idata = &di->id2.i_data;
7240
7241 if (end > i_size_read(inode))
7242 end = i_size_read(inode);
7243
Younger Liud62e74b2014-02-10 14:25:51 -08007244 BUG_ON(start > end);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007245
7246 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) ||
7247 !(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL) ||
7248 !ocfs2_supports_inline_data(osb)) {
7249 ocfs2_error(inode->i_sb,
Joe Perches7ecef142015-09-04 15:44:51 -07007250 "Inline data flags for inode %llu don't agree! Disk: 0x%x, Memory: 0x%x, Superblock: 0x%x\n",
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007251 (unsigned long long)OCFS2_I(inode)->ip_blkno,
7252 le16_to_cpu(di->i_dyn_features),
7253 OCFS2_I(inode)->ip_dyn_features,
7254 osb->s_feature_incompat);
7255 ret = -EROFS;
7256 goto out;
7257 }
7258
7259 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
7260 if (IS_ERR(handle)) {
7261 ret = PTR_ERR(handle);
7262 mlog_errno(ret);
7263 goto out;
7264 }
7265
Joel Becker0cf2f762009-02-12 16:41:25 -08007266 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
Joel Becker13723d02008-10-17 19:25:01 -07007267 OCFS2_JOURNAL_ACCESS_WRITE);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007268 if (ret) {
7269 mlog_errno(ret);
7270 goto out_commit;
7271 }
7272
7273 numbytes = end - start;
7274 memset(idata->id_data + start, 0, numbytes);
7275
7276 /*
7277 * No need to worry about the data page here - it's been
7278 * truncated already and inline data doesn't need it for
7279 * pushing zero's to disk, so we'll let readpage pick it up
7280 * later.
7281 */
7282 if (trunc) {
7283 i_size_write(inode, start);
7284 di->i_size = cpu_to_le64(start);
7285 }
7286
7287 inode->i_blocks = ocfs2_inode_sector_count(inode);
Deepa Dinamani078cd822016-09-14 07:48:04 -07007288 inode->i_ctime = inode->i_mtime = current_time(inode);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007289
7290 di->i_ctime = di->i_mtime = cpu_to_le64(inode->i_ctime.tv_sec);
7291 di->i_ctime_nsec = di->i_mtime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
7292
Darrick J. Wong6fdb7022014-04-03 14:47:08 -07007293 ocfs2_update_inode_fsync_trans(handle, inode, 1);
Mark Fasheh1afc32b2007-09-07 14:46:51 -07007294 ocfs2_journal_dirty(handle, di_bh);
7295
7296out_commit:
7297 ocfs2_commit_trans(osb, handle);
7298
7299out:
7300 return ret;
7301}
Tao Mae80de362011-05-23 10:36:43 +08007302
7303static int ocfs2_trim_extent(struct super_block *sb,
7304 struct ocfs2_group_desc *gd,
Ashish Samant105ddc92017-11-02 15:59:37 -07007305 u64 group, u32 start, u32 count)
Tao Mae80de362011-05-23 10:36:43 +08007306{
7307 u64 discard, bcount;
Ashish Samant105ddc92017-11-02 15:59:37 -07007308 struct ocfs2_super *osb = OCFS2_SB(sb);
Tao Mae80de362011-05-23 10:36:43 +08007309
7310 bcount = ocfs2_clusters_to_blocks(sb, count);
Ashish Samant105ddc92017-11-02 15:59:37 -07007311 discard = ocfs2_clusters_to_blocks(sb, start);
7312
7313 /*
7314 * For the first cluster group, the gd->bg_blkno is not at the start
7315 * of the group, but at an offset from the start. If we add it while
7316 * calculating discard for first group, we will wrongly start fstrim a
7317 * few blocks after the desried start block and the range can cross
7318 * over into the next cluster group. So, add it only if this is not
7319 * the first cluster group.
7320 */
7321 if (group != osb->first_cluster_group_blkno)
7322 discard += le64_to_cpu(gd->bg_blkno);
Tao Mae80de362011-05-23 10:36:43 +08007323
Tao Ma10fca352011-05-23 10:36:45 +08007324 trace_ocfs2_trim_extent(sb, (unsigned long long)discard, bcount);
7325
Tao Mae80de362011-05-23 10:36:43 +08007326 return sb_issue_discard(sb, discard, bcount, GFP_NOFS, 0);
7327}
7328
7329static int ocfs2_trim_group(struct super_block *sb,
Ashish Samant105ddc92017-11-02 15:59:37 -07007330 struct ocfs2_group_desc *gd, u64 group,
Tao Mae80de362011-05-23 10:36:43 +08007331 u32 start, u32 max, u32 minbits)
7332{
7333 int ret = 0, count = 0, next;
7334 void *bitmap = gd->bg_bitmap;
7335
7336 if (le16_to_cpu(gd->bg_free_bits_count) < minbits)
7337 return 0;
7338
Tao Ma10fca352011-05-23 10:36:45 +08007339 trace_ocfs2_trim_group((unsigned long long)le64_to_cpu(gd->bg_blkno),
7340 start, max, minbits);
7341
Tao Mae80de362011-05-23 10:36:43 +08007342 while (start < max) {
7343 start = ocfs2_find_next_zero_bit(bitmap, max, start);
7344 if (start >= max)
7345 break;
7346 next = ocfs2_find_next_bit(bitmap, max, start);
7347
7348 if ((next - start) >= minbits) {
Ashish Samant105ddc92017-11-02 15:59:37 -07007349 ret = ocfs2_trim_extent(sb, gd, group,
Tao Mae80de362011-05-23 10:36:43 +08007350 start, next - start);
7351 if (ret < 0) {
7352 mlog_errno(ret);
7353 break;
7354 }
7355 count += next - start;
7356 }
7357 start = next + 1;
7358
7359 if (fatal_signal_pending(current)) {
7360 count = -ERESTARTSYS;
7361 break;
7362 }
7363
7364 if ((le16_to_cpu(gd->bg_free_bits_count) - count) < minbits)
7365 break;
7366 }
7367
7368 if (ret < 0)
7369 count = ret;
7370
7371 return count;
7372}
7373
7374int ocfs2_trim_fs(struct super_block *sb, struct fstrim_range *range)
7375{
7376 struct ocfs2_super *osb = OCFS2_SB(sb);
7377 u64 start, len, trimmed, first_group, last_group, group;
7378 int ret, cnt;
7379 u32 first_bit, last_bit, minlen;
7380 struct buffer_head *main_bm_bh = NULL;
7381 struct inode *main_bm_inode = NULL;
7382 struct buffer_head *gd_bh = NULL;
7383 struct ocfs2_dinode *main_bm;
7384 struct ocfs2_group_desc *gd = NULL;
7385
7386 start = range->start >> osb->s_clustersize_bits;
7387 len = range->len >> osb->s_clustersize_bits;
7388 minlen = range->minlen >> osb->s_clustersize_bits;
Tao Mae80de362011-05-23 10:36:43 +08007389
Jie Liuaa897622014-01-21 15:48:35 -08007390 if (minlen >= osb->bitmap_cpg || range->len < sb->s_blocksize)
Tao Mae80de362011-05-23 10:36:43 +08007391 return -EINVAL;
7392
7393 main_bm_inode = ocfs2_get_system_file_inode(osb,
7394 GLOBAL_BITMAP_SYSTEM_INODE,
7395 OCFS2_INVALID_SLOT);
7396 if (!main_bm_inode) {
7397 ret = -EIO;
7398 mlog_errno(ret);
7399 goto out;
7400 }
7401
Al Viro59551022016-01-22 15:40:57 -05007402 inode_lock(main_bm_inode);
Tao Mae80de362011-05-23 10:36:43 +08007403
7404 ret = ocfs2_inode_lock(main_bm_inode, &main_bm_bh, 0);
7405 if (ret < 0) {
7406 mlog_errno(ret);
7407 goto out_mutex;
7408 }
7409 main_bm = (struct ocfs2_dinode *)main_bm_bh->b_data;
7410
7411 if (start >= le32_to_cpu(main_bm->i_clusters)) {
7412 ret = -EINVAL;
7413 goto out_unlock;
7414 }
7415
Jie Liuaa897622014-01-21 15:48:35 -08007416 len = range->len >> osb->s_clustersize_bits;
Tao Mae80de362011-05-23 10:36:43 +08007417 if (start + len > le32_to_cpu(main_bm->i_clusters))
7418 len = le32_to_cpu(main_bm->i_clusters) - start;
7419
Tao Ma10fca352011-05-23 10:36:45 +08007420 trace_ocfs2_trim_fs(start, len, minlen);
7421
Tao Mae80de362011-05-23 10:36:43 +08007422 /* Determine first and last group to examine based on start and len */
7423 first_group = ocfs2_which_cluster_group(main_bm_inode, start);
7424 if (first_group == osb->first_cluster_group_blkno)
7425 first_bit = start;
7426 else
7427 first_bit = start - ocfs2_blocks_to_clusters(sb, first_group);
7428 last_group = ocfs2_which_cluster_group(main_bm_inode, start + len - 1);
7429 last_bit = osb->bitmap_cpg;
7430
Jie Liuaa897622014-01-21 15:48:35 -08007431 trimmed = 0;
Tao Mae80de362011-05-23 10:36:43 +08007432 for (group = first_group; group <= last_group;) {
7433 if (first_bit + len >= osb->bitmap_cpg)
7434 last_bit = osb->bitmap_cpg;
7435 else
7436 last_bit = first_bit + len;
7437
7438 ret = ocfs2_read_group_descriptor(main_bm_inode,
7439 main_bm, group,
7440 &gd_bh);
7441 if (ret < 0) {
7442 mlog_errno(ret);
7443 break;
7444 }
7445
7446 gd = (struct ocfs2_group_desc *)gd_bh->b_data;
Ashish Samant105ddc92017-11-02 15:59:37 -07007447 cnt = ocfs2_trim_group(sb, gd, group,
7448 first_bit, last_bit, minlen);
Tao Mae80de362011-05-23 10:36:43 +08007449 brelse(gd_bh);
7450 gd_bh = NULL;
7451 if (cnt < 0) {
7452 ret = cnt;
7453 mlog_errno(ret);
7454 break;
7455 }
7456
7457 trimmed += cnt;
7458 len -= osb->bitmap_cpg - first_bit;
7459 first_bit = 0;
7460 if (group == osb->first_cluster_group_blkno)
7461 group = ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg);
7462 else
7463 group += ocfs2_clusters_to_blocks(sb, osb->bitmap_cpg);
7464 }
7465 range->len = trimmed * sb->s_blocksize;
7466out_unlock:
7467 ocfs2_inode_unlock(main_bm_inode, 0);
7468 brelse(main_bm_bh);
7469out_mutex:
Al Viro59551022016-01-22 15:40:57 -05007470 inode_unlock(main_bm_inode);
Tao Mae80de362011-05-23 10:36:43 +08007471 iput(main_bm_inode);
7472out:
7473 return ret;
7474}