xref: /openbmc/linux/fs/ocfs2/refcounttree.c (revision fac59652993f075d57860769c99045b3ca18780d)
11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2fa60ce2cSMasahiro Yamada /*
3f2c870e3STao Ma  * refcounttree.c
4f2c870e3STao Ma  *
5f2c870e3STao Ma  * Copyright (C) 2009 Oracle.  All rights reserved.
6f2c870e3STao Ma  */
7f2c870e3STao Ma 
8e73a819dSTao Ma #include <linux/sort.h>
9f2c870e3STao Ma #include <cluster/masklog.h>
10f2c870e3STao Ma #include "ocfs2.h"
11f2c870e3STao Ma #include "inode.h"
12f2c870e3STao Ma #include "alloc.h"
13f2c870e3STao Ma #include "suballoc.h"
14f2c870e3STao Ma #include "journal.h"
15f2c870e3STao Ma #include "uptodate.h"
16f2c870e3STao Ma #include "super.h"
17f2c870e3STao Ma #include "buffer_head_io.h"
18f2c870e3STao Ma #include "blockcheck.h"
19c732eb16STao Ma #include "refcounttree.h"
208bf396deSTao Ma #include "sysfile.h"
21374a263eSTao Ma #include "dlmglue.h"
22e73a819dSTao Ma #include "extent_map.h"
236f70fa51STao Ma #include "aops.h"
24492a8a33STao Ma #include "xattr.h"
2509bf27a0STao Ma #include "namei.h"
26198aac28STao Ma #include "ocfs2_trace.h"
2729ac8e85SDarrick J. Wong #include "file.h"
28*637c00e0SGautham Ananthakrishna #include "symlink.h"
296f70fa51STao Ma 
306f70fa51STao Ma #include <linux/bio.h>
316f70fa51STao Ma #include <linux/blkdev.h>
326f70fa51STao Ma #include <linux/slab.h>
336f70fa51STao Ma #include <linux/writeback.h>
346f70fa51STao Ma #include <linux/pagevec.h>
356f70fa51STao Ma #include <linux/swap.h>
36bd50873dSTao Ma #include <linux/security.h>
37bd50873dSTao Ma #include <linux/fsnotify.h>
38bd50873dSTao Ma #include <linux/quotaops.h>
39bd50873dSTao Ma #include <linux/namei.h>
40bd50873dSTao Ma #include <linux/mount.h>
41702e5bc6SChristoph Hellwig #include <linux/posix_acl.h>
426f70fa51STao Ma 
436f70fa51STao Ma struct ocfs2_cow_context {
446f70fa51STao Ma 	struct inode *inode;
456f70fa51STao Ma 	u32 cow_start;
466f70fa51STao Ma 	u32 cow_len;
47913580b4STao Ma 	struct ocfs2_extent_tree data_et;
48913580b4STao Ma 	struct ocfs2_refcount_tree *ref_tree;
496f70fa51STao Ma 	struct buffer_head *ref_root_bh;
506f70fa51STao Ma 	struct ocfs2_alloc_context *meta_ac;
516f70fa51STao Ma 	struct ocfs2_alloc_context *data_ac;
526f70fa51STao Ma 	struct ocfs2_cached_dealloc_ctxt dealloc;
53492a8a33STao Ma 	void *cow_object;
54492a8a33STao Ma 	struct ocfs2_post_refcount *post_refcount;
55492a8a33STao Ma 	int extra_credits;
56913580b4STao Ma 	int (*get_clusters)(struct ocfs2_cow_context *context,
57913580b4STao Ma 			    u32 v_cluster, u32 *p_cluster,
58913580b4STao Ma 			    u32 *num_clusters,
59913580b4STao Ma 			    unsigned int *extent_flags);
60913580b4STao Ma 	int (*cow_duplicate_clusters)(handle_t *handle,
61c7dd3392STiger Yang 				      struct inode *inode,
62913580b4STao Ma 				      u32 cpos, u32 old_cluster,
63913580b4STao Ma 				      u32 new_cluster, u32 new_len);
646f70fa51STao Ma };
65c732eb16STao Ma 
66c732eb16STao Ma static inline struct ocfs2_refcount_tree *
cache_info_to_refcount(struct ocfs2_caching_info * ci)67c732eb16STao Ma cache_info_to_refcount(struct ocfs2_caching_info *ci)
68c732eb16STao Ma {
69c732eb16STao Ma 	return container_of(ci, struct ocfs2_refcount_tree, rf_ci);
70c732eb16STao Ma }
71f2c870e3STao Ma 
ocfs2_validate_refcount_block(struct super_block * sb,struct buffer_head * bh)72f2c870e3STao Ma static int ocfs2_validate_refcount_block(struct super_block *sb,
73f2c870e3STao Ma 					 struct buffer_head *bh)
74f2c870e3STao Ma {
75f2c870e3STao Ma 	int rc;
76f2c870e3STao Ma 	struct ocfs2_refcount_block *rb =
77f2c870e3STao Ma 		(struct ocfs2_refcount_block *)bh->b_data;
78f2c870e3STao Ma 
79198aac28STao Ma 	trace_ocfs2_validate_refcount_block((unsigned long long)bh->b_blocknr);
80f2c870e3STao Ma 
81f2c870e3STao Ma 	BUG_ON(!buffer_uptodate(bh));
82f2c870e3STao Ma 
83f2c870e3STao Ma 	/*
84f2c870e3STao Ma 	 * If the ecc fails, we return the error but otherwise
85f2c870e3STao Ma 	 * leave the filesystem running.  We know any error is
86f2c870e3STao Ma 	 * local to this block.
87f2c870e3STao Ma 	 */
88f2c870e3STao Ma 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &rb->rf_check);
89f2c870e3STao Ma 	if (rc) {
90f2c870e3STao Ma 		mlog(ML_ERROR, "Checksum failed for refcount block %llu\n",
91f2c870e3STao Ma 		     (unsigned long long)bh->b_blocknr);
92f2c870e3STao Ma 		return rc;
93f2c870e3STao Ma 	}
94f2c870e3STao Ma 
95f2c870e3STao Ma 
96f2c870e3STao Ma 	if (!OCFS2_IS_VALID_REFCOUNT_BLOCK(rb)) {
9717a5b9abSGoldwyn Rodrigues 		rc = ocfs2_error(sb,
987ecef14aSJoe Perches 				 "Refcount block #%llu has bad signature %.*s\n",
99f2c870e3STao Ma 				 (unsigned long long)bh->b_blocknr, 7,
100f2c870e3STao Ma 				 rb->rf_signature);
10117a5b9abSGoldwyn Rodrigues 		goto out;
102f2c870e3STao Ma 	}
103f2c870e3STao Ma 
104f2c870e3STao Ma 	if (le64_to_cpu(rb->rf_blkno) != bh->b_blocknr) {
10517a5b9abSGoldwyn Rodrigues 		rc = ocfs2_error(sb,
1067ecef14aSJoe Perches 				 "Refcount block #%llu has an invalid rf_blkno of %llu\n",
107f2c870e3STao Ma 				 (unsigned long long)bh->b_blocknr,
108f2c870e3STao Ma 				 (unsigned long long)le64_to_cpu(rb->rf_blkno));
10917a5b9abSGoldwyn Rodrigues 		goto out;
110f2c870e3STao Ma 	}
111f2c870e3STao Ma 
112f2c870e3STao Ma 	if (le32_to_cpu(rb->rf_fs_generation) != OCFS2_SB(sb)->fs_generation) {
11317a5b9abSGoldwyn Rodrigues 		rc = ocfs2_error(sb,
1147ecef14aSJoe Perches 				 "Refcount block #%llu has an invalid rf_fs_generation of #%u\n",
115f2c870e3STao Ma 				 (unsigned long long)bh->b_blocknr,
116f2c870e3STao Ma 				 le32_to_cpu(rb->rf_fs_generation));
11717a5b9abSGoldwyn Rodrigues 		goto out;
118f2c870e3STao Ma 	}
11917a5b9abSGoldwyn Rodrigues out:
12017a5b9abSGoldwyn Rodrigues 	return rc;
121f2c870e3STao Ma }
122f2c870e3STao Ma 
ocfs2_read_refcount_block(struct ocfs2_caching_info * ci,u64 rb_blkno,struct buffer_head ** bh)123f2c870e3STao Ma static int ocfs2_read_refcount_block(struct ocfs2_caching_info *ci,
124f2c870e3STao Ma 				     u64 rb_blkno,
125f2c870e3STao Ma 				     struct buffer_head **bh)
126f2c870e3STao Ma {
127f2c870e3STao Ma 	int rc;
128f2c870e3STao Ma 	struct buffer_head *tmp = *bh;
129f2c870e3STao Ma 
130f2c870e3STao Ma 	rc = ocfs2_read_block(ci, rb_blkno, &tmp,
131f2c870e3STao Ma 			      ocfs2_validate_refcount_block);
132f2c870e3STao Ma 
133f2c870e3STao Ma 	/* If ocfs2_read_block() got us a new bh, pass it up. */
134f2c870e3STao Ma 	if (!rc && !*bh)
135f2c870e3STao Ma 		*bh = tmp;
136f2c870e3STao Ma 
137f2c870e3STao Ma 	return rc;
138f2c870e3STao Ma }
139c732eb16STao Ma 
ocfs2_refcount_cache_owner(struct ocfs2_caching_info * ci)140c732eb16STao Ma static u64 ocfs2_refcount_cache_owner(struct ocfs2_caching_info *ci)
141c732eb16STao Ma {
142c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
143c732eb16STao Ma 
144c732eb16STao Ma 	return rf->rf_blkno;
145c732eb16STao Ma }
146c732eb16STao Ma 
147c732eb16STao Ma static struct super_block *
ocfs2_refcount_cache_get_super(struct ocfs2_caching_info * ci)148c732eb16STao Ma ocfs2_refcount_cache_get_super(struct ocfs2_caching_info *ci)
149c732eb16STao Ma {
150c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
151c732eb16STao Ma 
152c732eb16STao Ma 	return rf->rf_sb;
153c732eb16STao Ma }
154c732eb16STao Ma 
ocfs2_refcount_cache_lock(struct ocfs2_caching_info * ci)155c732eb16STao Ma static void ocfs2_refcount_cache_lock(struct ocfs2_caching_info *ci)
156185a7321SJules Irenge __acquires(&rf->rf_lock)
157c732eb16STao Ma {
158c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
159c732eb16STao Ma 
160c732eb16STao Ma 	spin_lock(&rf->rf_lock);
161c732eb16STao Ma }
162c732eb16STao Ma 
ocfs2_refcount_cache_unlock(struct ocfs2_caching_info * ci)163c732eb16STao Ma static void ocfs2_refcount_cache_unlock(struct ocfs2_caching_info *ci)
164185a7321SJules Irenge __releases(&rf->rf_lock)
165c732eb16STao Ma {
166c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
167c732eb16STao Ma 
168c732eb16STao Ma 	spin_unlock(&rf->rf_lock);
169c732eb16STao Ma }
170c732eb16STao Ma 
ocfs2_refcount_cache_io_lock(struct ocfs2_caching_info * ci)171c732eb16STao Ma static void ocfs2_refcount_cache_io_lock(struct ocfs2_caching_info *ci)
172c732eb16STao Ma {
173c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
174c732eb16STao Ma 
175c732eb16STao Ma 	mutex_lock(&rf->rf_io_mutex);
176c732eb16STao Ma }
177c732eb16STao Ma 
ocfs2_refcount_cache_io_unlock(struct ocfs2_caching_info * ci)178c732eb16STao Ma static void ocfs2_refcount_cache_io_unlock(struct ocfs2_caching_info *ci)
179c732eb16STao Ma {
180c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
181c732eb16STao Ma 
182c732eb16STao Ma 	mutex_unlock(&rf->rf_io_mutex);
183c732eb16STao Ma }
184c732eb16STao Ma 
185c732eb16STao Ma static const struct ocfs2_caching_operations ocfs2_refcount_caching_ops = {
186c732eb16STao Ma 	.co_owner		= ocfs2_refcount_cache_owner,
187c732eb16STao Ma 	.co_get_super		= ocfs2_refcount_cache_get_super,
188c732eb16STao Ma 	.co_cache_lock		= ocfs2_refcount_cache_lock,
189c732eb16STao Ma 	.co_cache_unlock	= ocfs2_refcount_cache_unlock,
190c732eb16STao Ma 	.co_io_lock		= ocfs2_refcount_cache_io_lock,
191c732eb16STao Ma 	.co_io_unlock		= ocfs2_refcount_cache_io_unlock,
192c732eb16STao Ma };
193374a263eSTao Ma 
194374a263eSTao Ma static struct ocfs2_refcount_tree *
ocfs2_find_refcount_tree(struct ocfs2_super * osb,u64 blkno)195374a263eSTao Ma ocfs2_find_refcount_tree(struct ocfs2_super *osb, u64 blkno)
196374a263eSTao Ma {
197374a263eSTao Ma 	struct rb_node *n = osb->osb_rf_lock_tree.rb_node;
198374a263eSTao Ma 	struct ocfs2_refcount_tree *tree = NULL;
199374a263eSTao Ma 
200374a263eSTao Ma 	while (n) {
201374a263eSTao Ma 		tree = rb_entry(n, struct ocfs2_refcount_tree, rf_node);
202374a263eSTao Ma 
203374a263eSTao Ma 		if (blkno < tree->rf_blkno)
204374a263eSTao Ma 			n = n->rb_left;
205374a263eSTao Ma 		else if (blkno > tree->rf_blkno)
206374a263eSTao Ma 			n = n->rb_right;
207374a263eSTao Ma 		else
208374a263eSTao Ma 			return tree;
209374a263eSTao Ma 	}
210374a263eSTao Ma 
211374a263eSTao Ma 	return NULL;
212374a263eSTao Ma }
213374a263eSTao Ma 
214374a263eSTao Ma /* osb_lock is already locked. */
ocfs2_insert_refcount_tree(struct ocfs2_super * osb,struct ocfs2_refcount_tree * new)215374a263eSTao Ma static void ocfs2_insert_refcount_tree(struct ocfs2_super *osb,
216374a263eSTao Ma 				       struct ocfs2_refcount_tree *new)
217374a263eSTao Ma {
218374a263eSTao Ma 	u64 rf_blkno = new->rf_blkno;
219374a263eSTao Ma 	struct rb_node *parent = NULL;
220374a263eSTao Ma 	struct rb_node **p = &osb->osb_rf_lock_tree.rb_node;
221374a263eSTao Ma 	struct ocfs2_refcount_tree *tmp;
222374a263eSTao Ma 
223374a263eSTao Ma 	while (*p) {
224374a263eSTao Ma 		parent = *p;
225374a263eSTao Ma 
226374a263eSTao Ma 		tmp = rb_entry(parent, struct ocfs2_refcount_tree,
227374a263eSTao Ma 			       rf_node);
228374a263eSTao Ma 
229374a263eSTao Ma 		if (rf_blkno < tmp->rf_blkno)
230374a263eSTao Ma 			p = &(*p)->rb_left;
231374a263eSTao Ma 		else if (rf_blkno > tmp->rf_blkno)
232374a263eSTao Ma 			p = &(*p)->rb_right;
233374a263eSTao Ma 		else {
234374a263eSTao Ma 			/* This should never happen! */
235374a263eSTao Ma 			mlog(ML_ERROR, "Duplicate refcount block %llu found!\n",
236374a263eSTao Ma 			     (unsigned long long)rf_blkno);
237374a263eSTao Ma 			BUG();
238374a263eSTao Ma 		}
239374a263eSTao Ma 	}
240374a263eSTao Ma 
241374a263eSTao Ma 	rb_link_node(&new->rf_node, parent, p);
242374a263eSTao Ma 	rb_insert_color(&new->rf_node, &osb->osb_rf_lock_tree);
243374a263eSTao Ma }
244374a263eSTao Ma 
ocfs2_free_refcount_tree(struct ocfs2_refcount_tree * tree)245374a263eSTao Ma static void ocfs2_free_refcount_tree(struct ocfs2_refcount_tree *tree)
246374a263eSTao Ma {
247374a263eSTao Ma 	ocfs2_metadata_cache_exit(&tree->rf_ci);
248374a263eSTao Ma 	ocfs2_simple_drop_lockres(OCFS2_SB(tree->rf_sb), &tree->rf_lockres);
249374a263eSTao Ma 	ocfs2_lock_res_free(&tree->rf_lockres);
250374a263eSTao Ma 	kfree(tree);
251374a263eSTao Ma }
252374a263eSTao Ma 
253374a263eSTao Ma static inline void
ocfs2_erase_refcount_tree_from_list_no_lock(struct ocfs2_super * osb,struct ocfs2_refcount_tree * tree)254374a263eSTao Ma ocfs2_erase_refcount_tree_from_list_no_lock(struct ocfs2_super *osb,
255374a263eSTao Ma 					struct ocfs2_refcount_tree *tree)
256374a263eSTao Ma {
257374a263eSTao Ma 	rb_erase(&tree->rf_node, &osb->osb_rf_lock_tree);
258374a263eSTao Ma 	if (osb->osb_ref_tree_lru && osb->osb_ref_tree_lru == tree)
259374a263eSTao Ma 		osb->osb_ref_tree_lru = NULL;
260374a263eSTao Ma }
261374a263eSTao Ma 
ocfs2_erase_refcount_tree_from_list(struct ocfs2_super * osb,struct ocfs2_refcount_tree * tree)262374a263eSTao Ma static void ocfs2_erase_refcount_tree_from_list(struct ocfs2_super *osb,
263374a263eSTao Ma 					struct ocfs2_refcount_tree *tree)
264374a263eSTao Ma {
265374a263eSTao Ma 	spin_lock(&osb->osb_lock);
266374a263eSTao Ma 	ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
267374a263eSTao Ma 	spin_unlock(&osb->osb_lock);
268374a263eSTao Ma }
269374a263eSTao Ma 
ocfs2_kref_remove_refcount_tree(struct kref * kref)27012d4cec9STao Ma static void ocfs2_kref_remove_refcount_tree(struct kref *kref)
271374a263eSTao Ma {
272374a263eSTao Ma 	struct ocfs2_refcount_tree *tree =
273374a263eSTao Ma 		container_of(kref, struct ocfs2_refcount_tree, rf_getcnt);
274374a263eSTao Ma 
275374a263eSTao Ma 	ocfs2_free_refcount_tree(tree);
276374a263eSTao Ma }
277374a263eSTao Ma 
278374a263eSTao Ma static inline void
ocfs2_refcount_tree_get(struct ocfs2_refcount_tree * tree)279374a263eSTao Ma ocfs2_refcount_tree_get(struct ocfs2_refcount_tree *tree)
280374a263eSTao Ma {
281374a263eSTao Ma 	kref_get(&tree->rf_getcnt);
282374a263eSTao Ma }
283374a263eSTao Ma 
284374a263eSTao Ma static inline void
ocfs2_refcount_tree_put(struct ocfs2_refcount_tree * tree)285374a263eSTao Ma ocfs2_refcount_tree_put(struct ocfs2_refcount_tree *tree)
286374a263eSTao Ma {
287374a263eSTao Ma 	kref_put(&tree->rf_getcnt, ocfs2_kref_remove_refcount_tree);
288374a263eSTao Ma }
289374a263eSTao Ma 
ocfs2_init_refcount_tree_ci(struct ocfs2_refcount_tree * new,struct super_block * sb)290374a263eSTao Ma static inline void ocfs2_init_refcount_tree_ci(struct ocfs2_refcount_tree *new,
291374a263eSTao Ma 					       struct super_block *sb)
292374a263eSTao Ma {
293374a263eSTao Ma 	ocfs2_metadata_cache_init(&new->rf_ci, &ocfs2_refcount_caching_ops);
294374a263eSTao Ma 	mutex_init(&new->rf_io_mutex);
295374a263eSTao Ma 	new->rf_sb = sb;
296374a263eSTao Ma 	spin_lock_init(&new->rf_lock);
297374a263eSTao Ma }
298374a263eSTao Ma 
ocfs2_init_refcount_tree_lock(struct ocfs2_super * osb,struct ocfs2_refcount_tree * new,u64 rf_blkno,u32 generation)299374a263eSTao Ma static inline void ocfs2_init_refcount_tree_lock(struct ocfs2_super *osb,
300374a263eSTao Ma 					struct ocfs2_refcount_tree *new,
301374a263eSTao Ma 					u64 rf_blkno, u32 generation)
302374a263eSTao Ma {
303374a263eSTao Ma 	init_rwsem(&new->rf_sem);
304374a263eSTao Ma 	ocfs2_refcount_lock_res_init(&new->rf_lockres, osb,
305374a263eSTao Ma 				     rf_blkno, generation);
306374a263eSTao Ma }
307374a263eSTao Ma 
3088bf396deSTao Ma static struct ocfs2_refcount_tree*
ocfs2_allocate_refcount_tree(struct ocfs2_super * osb,u64 rf_blkno)3098bf396deSTao Ma ocfs2_allocate_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno)
3108bf396deSTao Ma {
3118bf396deSTao Ma 	struct ocfs2_refcount_tree *new;
3128bf396deSTao Ma 
3138bf396deSTao Ma 	new = kzalloc(sizeof(struct ocfs2_refcount_tree), GFP_NOFS);
3148bf396deSTao Ma 	if (!new)
3158bf396deSTao Ma 		return NULL;
3168bf396deSTao Ma 
3178bf396deSTao Ma 	new->rf_blkno = rf_blkno;
3188bf396deSTao Ma 	kref_init(&new->rf_getcnt);
3198bf396deSTao Ma 	ocfs2_init_refcount_tree_ci(new, osb->sb);
3208bf396deSTao Ma 
3218bf396deSTao Ma 	return new;
3228bf396deSTao Ma }
3238bf396deSTao Ma 
ocfs2_get_refcount_tree(struct ocfs2_super * osb,u64 rf_blkno,struct ocfs2_refcount_tree ** ret_tree)324374a263eSTao Ma static int ocfs2_get_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno,
325374a263eSTao Ma 				   struct ocfs2_refcount_tree **ret_tree)
326374a263eSTao Ma {
327374a263eSTao Ma 	int ret = 0;
328374a263eSTao Ma 	struct ocfs2_refcount_tree *tree, *new = NULL;
329374a263eSTao Ma 	struct buffer_head *ref_root_bh = NULL;
330374a263eSTao Ma 	struct ocfs2_refcount_block *ref_rb;
331374a263eSTao Ma 
332374a263eSTao Ma 	spin_lock(&osb->osb_lock);
333374a263eSTao Ma 	if (osb->osb_ref_tree_lru &&
334374a263eSTao Ma 	    osb->osb_ref_tree_lru->rf_blkno == rf_blkno)
335374a263eSTao Ma 		tree = osb->osb_ref_tree_lru;
336374a263eSTao Ma 	else
337374a263eSTao Ma 		tree = ocfs2_find_refcount_tree(osb, rf_blkno);
338374a263eSTao Ma 	if (tree)
339374a263eSTao Ma 		goto out;
340374a263eSTao Ma 
341374a263eSTao Ma 	spin_unlock(&osb->osb_lock);
342374a263eSTao Ma 
3438bf396deSTao Ma 	new = ocfs2_allocate_refcount_tree(osb, rf_blkno);
344374a263eSTao Ma 	if (!new) {
345374a263eSTao Ma 		ret = -ENOMEM;
3468bf396deSTao Ma 		mlog_errno(ret);
347374a263eSTao Ma 		return ret;
348374a263eSTao Ma 	}
349374a263eSTao Ma 	/*
350374a263eSTao Ma 	 * We need the generation to create the refcount tree lock and since
351374a263eSTao Ma 	 * it isn't changed during the tree modification, we are safe here to
352374a263eSTao Ma 	 * read without protection.
353374a263eSTao Ma 	 * We also have to purge the cache after we create the lock since the
354374a263eSTao Ma 	 * refcount block may have the stale data. It can only be trusted when
355374a263eSTao Ma 	 * we hold the refcount lock.
356374a263eSTao Ma 	 */
357374a263eSTao Ma 	ret = ocfs2_read_refcount_block(&new->rf_ci, rf_blkno, &ref_root_bh);
358374a263eSTao Ma 	if (ret) {
359374a263eSTao Ma 		mlog_errno(ret);
360374a263eSTao Ma 		ocfs2_metadata_cache_exit(&new->rf_ci);
361374a263eSTao Ma 		kfree(new);
362374a263eSTao Ma 		return ret;
363374a263eSTao Ma 	}
364374a263eSTao Ma 
365374a263eSTao Ma 	ref_rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
366374a263eSTao Ma 	new->rf_generation = le32_to_cpu(ref_rb->rf_generation);
367374a263eSTao Ma 	ocfs2_init_refcount_tree_lock(osb, new, rf_blkno,
368374a263eSTao Ma 				      new->rf_generation);
369374a263eSTao Ma 	ocfs2_metadata_cache_purge(&new->rf_ci);
370374a263eSTao Ma 
371374a263eSTao Ma 	spin_lock(&osb->osb_lock);
372374a263eSTao Ma 	tree = ocfs2_find_refcount_tree(osb, rf_blkno);
373374a263eSTao Ma 	if (tree)
374374a263eSTao Ma 		goto out;
375374a263eSTao Ma 
376374a263eSTao Ma 	ocfs2_insert_refcount_tree(osb, new);
377374a263eSTao Ma 
378374a263eSTao Ma 	tree = new;
379374a263eSTao Ma 	new = NULL;
380374a263eSTao Ma 
381374a263eSTao Ma out:
382374a263eSTao Ma 	*ret_tree = tree;
383374a263eSTao Ma 
384374a263eSTao Ma 	osb->osb_ref_tree_lru = tree;
385374a263eSTao Ma 
386374a263eSTao Ma 	spin_unlock(&osb->osb_lock);
387374a263eSTao Ma 
388374a263eSTao Ma 	if (new)
389374a263eSTao Ma 		ocfs2_free_refcount_tree(new);
390374a263eSTao Ma 
391374a263eSTao Ma 	brelse(ref_root_bh);
392374a263eSTao Ma 	return ret;
393374a263eSTao Ma }
394374a263eSTao Ma 
ocfs2_get_refcount_block(struct inode * inode,u64 * ref_blkno)395374a263eSTao Ma static int ocfs2_get_refcount_block(struct inode *inode, u64 *ref_blkno)
396374a263eSTao Ma {
397374a263eSTao Ma 	int ret;
398374a263eSTao Ma 	struct buffer_head *di_bh = NULL;
399374a263eSTao Ma 	struct ocfs2_dinode *di;
400374a263eSTao Ma 
401374a263eSTao Ma 	ret = ocfs2_read_inode_block(inode, &di_bh);
402374a263eSTao Ma 	if (ret) {
403374a263eSTao Ma 		mlog_errno(ret);
404374a263eSTao Ma 		goto out;
405374a263eSTao Ma 	}
406374a263eSTao Ma 
40784e40080SDarrick J. Wong 	BUG_ON(!ocfs2_is_refcount_inode(inode));
408374a263eSTao Ma 
409374a263eSTao Ma 	di = (struct ocfs2_dinode *)di_bh->b_data;
410374a263eSTao Ma 	*ref_blkno = le64_to_cpu(di->i_refcount_loc);
411374a263eSTao Ma 	brelse(di_bh);
412374a263eSTao Ma out:
413374a263eSTao Ma 	return ret;
414374a263eSTao Ma }
415374a263eSTao Ma 
__ocfs2_lock_refcount_tree(struct ocfs2_super * osb,struct ocfs2_refcount_tree * tree,int rw)416374a263eSTao Ma static int __ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
417374a263eSTao Ma 				      struct ocfs2_refcount_tree *tree, int rw)
418374a263eSTao Ma {
419374a263eSTao Ma 	int ret;
420374a263eSTao Ma 
421374a263eSTao Ma 	ret = ocfs2_refcount_lock(tree, rw);
422374a263eSTao Ma 	if (ret) {
423374a263eSTao Ma 		mlog_errno(ret);
424374a263eSTao Ma 		goto out;
425374a263eSTao Ma 	}
426374a263eSTao Ma 
427374a263eSTao Ma 	if (rw)
428374a263eSTao Ma 		down_write(&tree->rf_sem);
429374a263eSTao Ma 	else
430374a263eSTao Ma 		down_read(&tree->rf_sem);
431374a263eSTao Ma 
432374a263eSTao Ma out:
433374a263eSTao Ma 	return ret;
434374a263eSTao Ma }
435374a263eSTao Ma 
436374a263eSTao Ma /*
437374a263eSTao Ma  * Lock the refcount tree pointed by ref_blkno and return the tree.
438374a263eSTao Ma  * In most case, we lock the tree and read the refcount block.
439374a263eSTao Ma  * So read it here if the caller really needs it.
440374a263eSTao Ma  *
441374a263eSTao Ma  * If the tree has been re-created by other node, it will free the
442374a263eSTao Ma  * old one and re-create it.
443374a263eSTao Ma  */
ocfs2_lock_refcount_tree(struct ocfs2_super * osb,u64 ref_blkno,int rw,struct ocfs2_refcount_tree ** ret_tree,struct buffer_head ** ref_bh)444374a263eSTao Ma int ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
445374a263eSTao Ma 			     u64 ref_blkno, int rw,
446374a263eSTao Ma 			     struct ocfs2_refcount_tree **ret_tree,
447374a263eSTao Ma 			     struct buffer_head **ref_bh)
448374a263eSTao Ma {
449374a263eSTao Ma 	int ret, delete_tree = 0;
450374a263eSTao Ma 	struct ocfs2_refcount_tree *tree = NULL;
451374a263eSTao Ma 	struct buffer_head *ref_root_bh = NULL;
452374a263eSTao Ma 	struct ocfs2_refcount_block *rb;
453374a263eSTao Ma 
454374a263eSTao Ma again:
455374a263eSTao Ma 	ret = ocfs2_get_refcount_tree(osb, ref_blkno, &tree);
456374a263eSTao Ma 	if (ret) {
457374a263eSTao Ma 		mlog_errno(ret);
458374a263eSTao Ma 		return ret;
459374a263eSTao Ma 	}
460374a263eSTao Ma 
461374a263eSTao Ma 	ocfs2_refcount_tree_get(tree);
462374a263eSTao Ma 
463374a263eSTao Ma 	ret = __ocfs2_lock_refcount_tree(osb, tree, rw);
464374a263eSTao Ma 	if (ret) {
465374a263eSTao Ma 		mlog_errno(ret);
466374a263eSTao Ma 		ocfs2_refcount_tree_put(tree);
467374a263eSTao Ma 		goto out;
468374a263eSTao Ma 	}
469374a263eSTao Ma 
470374a263eSTao Ma 	ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
471374a263eSTao Ma 					&ref_root_bh);
472374a263eSTao Ma 	if (ret) {
473374a263eSTao Ma 		mlog_errno(ret);
474374a263eSTao Ma 		ocfs2_unlock_refcount_tree(osb, tree, rw);
475374a263eSTao Ma 		goto out;
476374a263eSTao Ma 	}
477374a263eSTao Ma 
478374a263eSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
479374a263eSTao Ma 	/*
480374a263eSTao Ma 	 * If the refcount block has been freed and re-created, we may need
481374a263eSTao Ma 	 * to recreate the refcount tree also.
482374a263eSTao Ma 	 *
483374a263eSTao Ma 	 * Here we just remove the tree from the rb-tree, and the last
484374a263eSTao Ma 	 * kref holder will unlock and delete this refcount_tree.
485374a263eSTao Ma 	 * Then we goto "again" and ocfs2_get_refcount_tree will create
486374a263eSTao Ma 	 * the new refcount tree for us.
487374a263eSTao Ma 	 */
488374a263eSTao Ma 	if (tree->rf_generation != le32_to_cpu(rb->rf_generation)) {
489374a263eSTao Ma 		if (!tree->rf_removed) {
490374a263eSTao Ma 			ocfs2_erase_refcount_tree_from_list(osb, tree);
491374a263eSTao Ma 			tree->rf_removed = 1;
492374a263eSTao Ma 			delete_tree = 1;
493374a263eSTao Ma 		}
494374a263eSTao Ma 
495374a263eSTao Ma 		ocfs2_unlock_refcount_tree(osb, tree, rw);
496374a263eSTao Ma 		/*
497374a263eSTao Ma 		 * We get an extra reference when we create the refcount
498374a263eSTao Ma 		 * tree, so another put will destroy it.
499374a263eSTao Ma 		 */
500374a263eSTao Ma 		if (delete_tree)
501374a263eSTao Ma 			ocfs2_refcount_tree_put(tree);
502374a263eSTao Ma 		brelse(ref_root_bh);
503374a263eSTao Ma 		ref_root_bh = NULL;
504374a263eSTao Ma 		goto again;
505374a263eSTao Ma 	}
506374a263eSTao Ma 
507374a263eSTao Ma 	*ret_tree = tree;
508374a263eSTao Ma 	if (ref_bh) {
509374a263eSTao Ma 		*ref_bh = ref_root_bh;
510374a263eSTao Ma 		ref_root_bh = NULL;
511374a263eSTao Ma 	}
512374a263eSTao Ma out:
513374a263eSTao Ma 	brelse(ref_root_bh);
514374a263eSTao Ma 	return ret;
515374a263eSTao Ma }
516374a263eSTao Ma 
ocfs2_unlock_refcount_tree(struct ocfs2_super * osb,struct ocfs2_refcount_tree * tree,int rw)517374a263eSTao Ma void ocfs2_unlock_refcount_tree(struct ocfs2_super *osb,
518374a263eSTao Ma 				struct ocfs2_refcount_tree *tree, int rw)
519374a263eSTao Ma {
520374a263eSTao Ma 	if (rw)
521374a263eSTao Ma 		up_write(&tree->rf_sem);
522374a263eSTao Ma 	else
523374a263eSTao Ma 		up_read(&tree->rf_sem);
524374a263eSTao Ma 
525374a263eSTao Ma 	ocfs2_refcount_unlock(tree, rw);
526374a263eSTao Ma 	ocfs2_refcount_tree_put(tree);
527374a263eSTao Ma }
528374a263eSTao Ma 
ocfs2_purge_refcount_trees(struct ocfs2_super * osb)529374a263eSTao Ma void ocfs2_purge_refcount_trees(struct ocfs2_super *osb)
530374a263eSTao Ma {
531374a263eSTao Ma 	struct rb_node *node;
532374a263eSTao Ma 	struct ocfs2_refcount_tree *tree;
533374a263eSTao Ma 	struct rb_root *root = &osb->osb_rf_lock_tree;
534374a263eSTao Ma 
535374a263eSTao Ma 	while ((node = rb_last(root)) != NULL) {
536374a263eSTao Ma 		tree = rb_entry(node, struct ocfs2_refcount_tree, rf_node);
537374a263eSTao Ma 
538198aac28STao Ma 		trace_ocfs2_purge_refcount_trees(
539374a263eSTao Ma 				(unsigned long long) tree->rf_blkno);
540374a263eSTao Ma 
541374a263eSTao Ma 		rb_erase(&tree->rf_node, root);
542374a263eSTao Ma 		ocfs2_free_refcount_tree(tree);
543374a263eSTao Ma 	}
544374a263eSTao Ma }
5458bf396deSTao Ma 
5468bf396deSTao Ma /*
5478bf396deSTao Ma  * Create a refcount tree for an inode.
5488bf396deSTao Ma  * We take for granted that the inode is already locked.
5498bf396deSTao Ma  */
ocfs2_create_refcount_tree(struct inode * inode,struct buffer_head * di_bh)5508bf396deSTao Ma static int ocfs2_create_refcount_tree(struct inode *inode,
5518bf396deSTao Ma 				      struct buffer_head *di_bh)
5528bf396deSTao Ma {
5538bf396deSTao Ma 	int ret;
5548bf396deSTao Ma 	handle_t *handle = NULL;
5558bf396deSTao Ma 	struct ocfs2_alloc_context *meta_ac = NULL;
5568bf396deSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
5578bf396deSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
5588bf396deSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5598bf396deSTao Ma 	struct buffer_head *new_bh = NULL;
5608bf396deSTao Ma 	struct ocfs2_refcount_block *rb;
5618bf396deSTao Ma 	struct ocfs2_refcount_tree *new_tree = NULL, *tree = NULL;
5628bf396deSTao Ma 	u16 suballoc_bit_start;
5638bf396deSTao Ma 	u32 num_got;
5642b6cb576SJoel Becker 	u64 suballoc_loc, first_blkno;
5658bf396deSTao Ma 
56684e40080SDarrick J. Wong 	BUG_ON(ocfs2_is_refcount_inode(inode));
5678bf396deSTao Ma 
568198aac28STao Ma 	trace_ocfs2_create_refcount_tree(
569d324cd4cSpiaojun 		(unsigned long long)oi->ip_blkno);
5708bf396deSTao Ma 
5718bf396deSTao Ma 	ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
5728bf396deSTao Ma 	if (ret) {
5738bf396deSTao Ma 		mlog_errno(ret);
5748bf396deSTao Ma 		goto out;
5758bf396deSTao Ma 	}
5768bf396deSTao Ma 
5778bf396deSTao Ma 	handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_CREATE_CREDITS);
5788bf396deSTao Ma 	if (IS_ERR(handle)) {
5798bf396deSTao Ma 		ret = PTR_ERR(handle);
5808bf396deSTao Ma 		mlog_errno(ret);
5818bf396deSTao Ma 		goto out;
5828bf396deSTao Ma 	}
5838bf396deSTao Ma 
5848bf396deSTao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
5858bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
5868bf396deSTao Ma 	if (ret) {
5878bf396deSTao Ma 		mlog_errno(ret);
5888bf396deSTao Ma 		goto out_commit;
5898bf396deSTao Ma 	}
5908bf396deSTao Ma 
5912b6cb576SJoel Becker 	ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
5928bf396deSTao Ma 				   &suballoc_bit_start, &num_got,
5938bf396deSTao Ma 				   &first_blkno);
5948bf396deSTao Ma 	if (ret) {
5958bf396deSTao Ma 		mlog_errno(ret);
5968bf396deSTao Ma 		goto out_commit;
5978bf396deSTao Ma 	}
5988bf396deSTao Ma 
5998bf396deSTao Ma 	new_tree = ocfs2_allocate_refcount_tree(osb, first_blkno);
6008bf396deSTao Ma 	if (!new_tree) {
6018bf396deSTao Ma 		ret = -ENOMEM;
6028bf396deSTao Ma 		mlog_errno(ret);
6038bf396deSTao Ma 		goto out_commit;
6048bf396deSTao Ma 	}
6058bf396deSTao Ma 
6068bf396deSTao Ma 	new_bh = sb_getblk(inode->i_sb, first_blkno);
60758796207SRui Xiang 	if (!new_bh) {
60858796207SRui Xiang 		ret = -ENOMEM;
60958796207SRui Xiang 		mlog_errno(ret);
61058796207SRui Xiang 		goto out_commit;
61158796207SRui Xiang 	}
6128bf396deSTao Ma 	ocfs2_set_new_buffer_uptodate(&new_tree->rf_ci, new_bh);
6138bf396deSTao Ma 
6148bf396deSTao Ma 	ret = ocfs2_journal_access_rb(handle, &new_tree->rf_ci, new_bh,
6158bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_CREATE);
6168bf396deSTao Ma 	if (ret) {
6178bf396deSTao Ma 		mlog_errno(ret);
6188bf396deSTao Ma 		goto out_commit;
6198bf396deSTao Ma 	}
6208bf396deSTao Ma 
6218bf396deSTao Ma 	/* Initialize ocfs2_refcount_block. */
6228bf396deSTao Ma 	rb = (struct ocfs2_refcount_block *)new_bh->b_data;
6238bf396deSTao Ma 	memset(rb, 0, inode->i_sb->s_blocksize);
6248bf396deSTao Ma 	strcpy((void *)rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
625b89c5428STiger Yang 	rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
6262b6cb576SJoel Becker 	rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc);
6278bf396deSTao Ma 	rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
6288bf396deSTao Ma 	rb->rf_fs_generation = cpu_to_le32(osb->fs_generation);
6298bf396deSTao Ma 	rb->rf_blkno = cpu_to_le64(first_blkno);
6308bf396deSTao Ma 	rb->rf_count = cpu_to_le32(1);
6318bf396deSTao Ma 	rb->rf_records.rl_count =
6328bf396deSTao Ma 			cpu_to_le16(ocfs2_refcount_recs_per_rb(osb->sb));
6338bf396deSTao Ma 	spin_lock(&osb->osb_lock);
6348bf396deSTao Ma 	rb->rf_generation = osb->s_next_generation++;
6358bf396deSTao Ma 	spin_unlock(&osb->osb_lock);
6368bf396deSTao Ma 
6378bf396deSTao Ma 	ocfs2_journal_dirty(handle, new_bh);
6388bf396deSTao Ma 
6398bf396deSTao Ma 	spin_lock(&oi->ip_lock);
6408bf396deSTao Ma 	oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
6418bf396deSTao Ma 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6428bf396deSTao Ma 	di->i_refcount_loc = cpu_to_le64(first_blkno);
6438bf396deSTao Ma 	spin_unlock(&oi->ip_lock);
6448bf396deSTao Ma 
645198aac28STao Ma 	trace_ocfs2_create_refcount_tree_blkno((unsigned long long)first_blkno);
6468bf396deSTao Ma 
6478bf396deSTao Ma 	ocfs2_journal_dirty(handle, di_bh);
6488bf396deSTao Ma 
6498bf396deSTao Ma 	/*
6508bf396deSTao Ma 	 * We have to init the tree lock here since it will use
6518bf396deSTao Ma 	 * the generation number to create it.
6528bf396deSTao Ma 	 */
6538bf396deSTao Ma 	new_tree->rf_generation = le32_to_cpu(rb->rf_generation);
6548bf396deSTao Ma 	ocfs2_init_refcount_tree_lock(osb, new_tree, first_blkno,
6558bf396deSTao Ma 				      new_tree->rf_generation);
6568bf396deSTao Ma 
6578bf396deSTao Ma 	spin_lock(&osb->osb_lock);
6588bf396deSTao Ma 	tree = ocfs2_find_refcount_tree(osb, first_blkno);
6598bf396deSTao Ma 
6608bf396deSTao Ma 	/*
6618bf396deSTao Ma 	 * We've just created a new refcount tree in this block.  If
6628bf396deSTao Ma 	 * we found a refcount tree on the ocfs2_super, it must be
6638bf396deSTao Ma 	 * one we just deleted.  We free the old tree before
6648bf396deSTao Ma 	 * inserting the new tree.
6658bf396deSTao Ma 	 */
6668bf396deSTao Ma 	BUG_ON(tree && tree->rf_generation == new_tree->rf_generation);
6678bf396deSTao Ma 	if (tree)
6688bf396deSTao Ma 		ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
6698bf396deSTao Ma 	ocfs2_insert_refcount_tree(osb, new_tree);
6708bf396deSTao Ma 	spin_unlock(&osb->osb_lock);
6718bf396deSTao Ma 	new_tree = NULL;
6728bf396deSTao Ma 	if (tree)
6738bf396deSTao Ma 		ocfs2_refcount_tree_put(tree);
6748bf396deSTao Ma 
6758bf396deSTao Ma out_commit:
6768bf396deSTao Ma 	ocfs2_commit_trans(osb, handle);
6778bf396deSTao Ma 
6788bf396deSTao Ma out:
6798bf396deSTao Ma 	if (new_tree) {
6808bf396deSTao Ma 		ocfs2_metadata_cache_exit(&new_tree->rf_ci);
6818bf396deSTao Ma 		kfree(new_tree);
6828bf396deSTao Ma 	}
6838bf396deSTao Ma 
6848bf396deSTao Ma 	brelse(new_bh);
6858bf396deSTao Ma 	if (meta_ac)
6868bf396deSTao Ma 		ocfs2_free_alloc_context(meta_ac);
6878bf396deSTao Ma 
6888bf396deSTao Ma 	return ret;
6898bf396deSTao Ma }
6908bf396deSTao Ma 
ocfs2_set_refcount_tree(struct inode * inode,struct buffer_head * di_bh,u64 refcount_loc)6918bf396deSTao Ma static int ocfs2_set_refcount_tree(struct inode *inode,
6928bf396deSTao Ma 				   struct buffer_head *di_bh,
6938bf396deSTao Ma 				   u64 refcount_loc)
6948bf396deSTao Ma {
6958bf396deSTao Ma 	int ret;
6968bf396deSTao Ma 	handle_t *handle = NULL;
6978bf396deSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
6988bf396deSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
6998bf396deSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7008bf396deSTao Ma 	struct buffer_head *ref_root_bh = NULL;
7018bf396deSTao Ma 	struct ocfs2_refcount_block *rb;
7028bf396deSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
7038bf396deSTao Ma 
70484e40080SDarrick J. Wong 	BUG_ON(ocfs2_is_refcount_inode(inode));
7058bf396deSTao Ma 
7068bf396deSTao Ma 	ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
7078bf396deSTao Ma 				       &ref_tree, &ref_root_bh);
7088bf396deSTao Ma 	if (ret) {
7098bf396deSTao Ma 		mlog_errno(ret);
7108bf396deSTao Ma 		return ret;
7118bf396deSTao Ma 	}
7128bf396deSTao Ma 
7138bf396deSTao Ma 	handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_SET_CREDITS);
7148bf396deSTao Ma 	if (IS_ERR(handle)) {
7158bf396deSTao Ma 		ret = PTR_ERR(handle);
7168bf396deSTao Ma 		mlog_errno(ret);
7178bf396deSTao Ma 		goto out;
7188bf396deSTao Ma 	}
7198bf396deSTao Ma 
7208bf396deSTao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7218bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
7228bf396deSTao Ma 	if (ret) {
7238bf396deSTao Ma 		mlog_errno(ret);
7248bf396deSTao Ma 		goto out_commit;
7258bf396deSTao Ma 	}
7268bf396deSTao Ma 
7278bf396deSTao Ma 	ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, ref_root_bh,
7288bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
7298bf396deSTao Ma 	if (ret) {
7308bf396deSTao Ma 		mlog_errno(ret);
7318bf396deSTao Ma 		goto out_commit;
7328bf396deSTao Ma 	}
7338bf396deSTao Ma 
7348bf396deSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
7358bf396deSTao Ma 	le32_add_cpu(&rb->rf_count, 1);
7368bf396deSTao Ma 
7378bf396deSTao Ma 	ocfs2_journal_dirty(handle, ref_root_bh);
7388bf396deSTao Ma 
7398bf396deSTao Ma 	spin_lock(&oi->ip_lock);
7408bf396deSTao Ma 	oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
7418bf396deSTao Ma 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7428bf396deSTao Ma 	di->i_refcount_loc = cpu_to_le64(refcount_loc);
7438bf396deSTao Ma 	spin_unlock(&oi->ip_lock);
7448bf396deSTao Ma 	ocfs2_journal_dirty(handle, di_bh);
7458bf396deSTao Ma 
7468bf396deSTao Ma out_commit:
7478bf396deSTao Ma 	ocfs2_commit_trans(osb, handle);
7488bf396deSTao Ma out:
7498bf396deSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
7508bf396deSTao Ma 	brelse(ref_root_bh);
7518bf396deSTao Ma 
7528bf396deSTao Ma 	return ret;
7538bf396deSTao Ma }
7548bf396deSTao Ma 
ocfs2_remove_refcount_tree(struct inode * inode,struct buffer_head * di_bh)7558bf396deSTao Ma int ocfs2_remove_refcount_tree(struct inode *inode, struct buffer_head *di_bh)
7568bf396deSTao Ma {
7578bf396deSTao Ma 	int ret, delete_tree = 0;
7588bf396deSTao Ma 	handle_t *handle = NULL;
7598bf396deSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7608bf396deSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
7618bf396deSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7628bf396deSTao Ma 	struct ocfs2_refcount_block *rb;
7638bf396deSTao Ma 	struct inode *alloc_inode = NULL;
7648bf396deSTao Ma 	struct buffer_head *alloc_bh = NULL;
7658bf396deSTao Ma 	struct buffer_head *blk_bh = NULL;
7668bf396deSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
7678bf396deSTao Ma 	int credits = OCFS2_REFCOUNT_TREE_REMOVE_CREDITS;
7688bf396deSTao Ma 	u64 blk = 0, bg_blkno = 0, ref_blkno = le64_to_cpu(di->i_refcount_loc);
7698bf396deSTao Ma 	u16 bit = 0;
7708bf396deSTao Ma 
77184e40080SDarrick J. Wong 	if (!ocfs2_is_refcount_inode(inode))
7728bf396deSTao Ma 		return 0;
7738bf396deSTao Ma 
7748bf396deSTao Ma 	BUG_ON(!ref_blkno);
7758bf396deSTao Ma 	ret = ocfs2_lock_refcount_tree(osb, ref_blkno, 1, &ref_tree, &blk_bh);
7768bf396deSTao Ma 	if (ret) {
7778bf396deSTao Ma 		mlog_errno(ret);
7788bf396deSTao Ma 		return ret;
7798bf396deSTao Ma 	}
7808bf396deSTao Ma 
7818bf396deSTao Ma 	rb = (struct ocfs2_refcount_block *)blk_bh->b_data;
7828bf396deSTao Ma 
7838bf396deSTao Ma 	/*
7848bf396deSTao Ma 	 * If we are the last user, we need to free the block.
7858bf396deSTao Ma 	 * So lock the allocator ahead.
7868bf396deSTao Ma 	 */
7878bf396deSTao Ma 	if (le32_to_cpu(rb->rf_count) == 1) {
7888bf396deSTao Ma 		blk = le64_to_cpu(rb->rf_blkno);
7898bf396deSTao Ma 		bit = le16_to_cpu(rb->rf_suballoc_bit);
79074380c47STao Ma 		if (rb->rf_suballoc_loc)
79174380c47STao Ma 			bg_blkno = le64_to_cpu(rb->rf_suballoc_loc);
79274380c47STao Ma 		else
7938bf396deSTao Ma 			bg_blkno = ocfs2_which_suballoc_group(blk, bit);
7948bf396deSTao Ma 
7958bf396deSTao Ma 		alloc_inode = ocfs2_get_system_file_inode(osb,
7968bf396deSTao Ma 					EXTENT_ALLOC_SYSTEM_INODE,
7978bf396deSTao Ma 					le16_to_cpu(rb->rf_suballoc_slot));
7988bf396deSTao Ma 		if (!alloc_inode) {
7998bf396deSTao Ma 			ret = -ENOMEM;
8008bf396deSTao Ma 			mlog_errno(ret);
8018bf396deSTao Ma 			goto out;
8028bf396deSTao Ma 		}
8035955102cSAl Viro 		inode_lock(alloc_inode);
8048bf396deSTao Ma 
8058bf396deSTao Ma 		ret = ocfs2_inode_lock(alloc_inode, &alloc_bh, 1);
8068bf396deSTao Ma 		if (ret) {
8078bf396deSTao Ma 			mlog_errno(ret);
8088bf396deSTao Ma 			goto out_mutex;
8098bf396deSTao Ma 		}
8108bf396deSTao Ma 
8118bf396deSTao Ma 		credits += OCFS2_SUBALLOC_FREE;
8128bf396deSTao Ma 	}
8138bf396deSTao Ma 
8148bf396deSTao Ma 	handle = ocfs2_start_trans(osb, credits);
8158bf396deSTao Ma 	if (IS_ERR(handle)) {
8168bf396deSTao Ma 		ret = PTR_ERR(handle);
8178bf396deSTao Ma 		mlog_errno(ret);
8188bf396deSTao Ma 		goto out_unlock;
8198bf396deSTao Ma 	}
8208bf396deSTao Ma 
8218bf396deSTao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
8228bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
8238bf396deSTao Ma 	if (ret) {
8248bf396deSTao Ma 		mlog_errno(ret);
8258bf396deSTao Ma 		goto out_commit;
8268bf396deSTao Ma 	}
8278bf396deSTao Ma 
8288bf396deSTao Ma 	ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, blk_bh,
8298bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
8308bf396deSTao Ma 	if (ret) {
8318bf396deSTao Ma 		mlog_errno(ret);
8328bf396deSTao Ma 		goto out_commit;
8338bf396deSTao Ma 	}
8348bf396deSTao Ma 
8358bf396deSTao Ma 	spin_lock(&oi->ip_lock);
8368bf396deSTao Ma 	oi->ip_dyn_features &= ~OCFS2_HAS_REFCOUNT_FL;
8378bf396deSTao Ma 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
8388bf396deSTao Ma 	di->i_refcount_loc = 0;
8398bf396deSTao Ma 	spin_unlock(&oi->ip_lock);
8408bf396deSTao Ma 	ocfs2_journal_dirty(handle, di_bh);
8418bf396deSTao Ma 
8428bf396deSTao Ma 	le32_add_cpu(&rb->rf_count , -1);
8438bf396deSTao Ma 	ocfs2_journal_dirty(handle, blk_bh);
8448bf396deSTao Ma 
8458bf396deSTao Ma 	if (!rb->rf_count) {
8468bf396deSTao Ma 		delete_tree = 1;
8478bf396deSTao Ma 		ocfs2_erase_refcount_tree_from_list(osb, ref_tree);
8488bf396deSTao Ma 		ret = ocfs2_free_suballoc_bits(handle, alloc_inode,
8498bf396deSTao Ma 					       alloc_bh, bit, bg_blkno, 1);
8508bf396deSTao Ma 		if (ret)
8518bf396deSTao Ma 			mlog_errno(ret);
8528bf396deSTao Ma 	}
8538bf396deSTao Ma 
8548bf396deSTao Ma out_commit:
8558bf396deSTao Ma 	ocfs2_commit_trans(osb, handle);
8568bf396deSTao Ma out_unlock:
8578bf396deSTao Ma 	if (alloc_inode) {
8588bf396deSTao Ma 		ocfs2_inode_unlock(alloc_inode, 1);
8598bf396deSTao Ma 		brelse(alloc_bh);
8608bf396deSTao Ma 	}
8618bf396deSTao Ma out_mutex:
8628bf396deSTao Ma 	if (alloc_inode) {
8635955102cSAl Viro 		inode_unlock(alloc_inode);
8648bf396deSTao Ma 		iput(alloc_inode);
8658bf396deSTao Ma 	}
8668bf396deSTao Ma out:
8678bf396deSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
8688bf396deSTao Ma 	if (delete_tree)
8698bf396deSTao Ma 		ocfs2_refcount_tree_put(ref_tree);
8708bf396deSTao Ma 	brelse(blk_bh);
8718bf396deSTao Ma 
8728bf396deSTao Ma 	return ret;
8738bf396deSTao Ma }
874e73a819dSTao Ma 
ocfs2_find_refcount_rec_in_rl(struct ocfs2_caching_info * ci,struct buffer_head * ref_leaf_bh,u64 cpos,unsigned int len,struct ocfs2_refcount_rec * ret_rec,int * index)875e73a819dSTao Ma static void ocfs2_find_refcount_rec_in_rl(struct ocfs2_caching_info *ci,
876e73a819dSTao Ma 					  struct buffer_head *ref_leaf_bh,
877e73a819dSTao Ma 					  u64 cpos, unsigned int len,
878e73a819dSTao Ma 					  struct ocfs2_refcount_rec *ret_rec,
879e73a819dSTao Ma 					  int *index)
880e73a819dSTao Ma {
881e73a819dSTao Ma 	int i = 0;
882e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
883e73a819dSTao Ma 		(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
884e73a819dSTao Ma 	struct ocfs2_refcount_rec *rec = NULL;
885e73a819dSTao Ma 
886e73a819dSTao Ma 	for (; i < le16_to_cpu(rb->rf_records.rl_used); i++) {
887e73a819dSTao Ma 		rec = &rb->rf_records.rl_recs[i];
888e73a819dSTao Ma 
889e73a819dSTao Ma 		if (le64_to_cpu(rec->r_cpos) +
890e73a819dSTao Ma 		    le32_to_cpu(rec->r_clusters) <= cpos)
891e73a819dSTao Ma 			continue;
892e73a819dSTao Ma 		else if (le64_to_cpu(rec->r_cpos) > cpos)
893e73a819dSTao Ma 			break;
894e73a819dSTao Ma 
895e73a819dSTao Ma 		/* ok, cpos fail in this rec. Just return. */
896e73a819dSTao Ma 		if (ret_rec)
897e73a819dSTao Ma 			*ret_rec = *rec;
898e73a819dSTao Ma 		goto out;
899e73a819dSTao Ma 	}
900e73a819dSTao Ma 
901e73a819dSTao Ma 	if (ret_rec) {
902e73a819dSTao Ma 		/* We meet with a hole here, so fake the rec. */
903e73a819dSTao Ma 		ret_rec->r_cpos = cpu_to_le64(cpos);
904e73a819dSTao Ma 		ret_rec->r_refcount = 0;
905e73a819dSTao Ma 		if (i < le16_to_cpu(rb->rf_records.rl_used) &&
906e73a819dSTao Ma 		    le64_to_cpu(rec->r_cpos) < cpos + len)
907e73a819dSTao Ma 			ret_rec->r_clusters =
908e73a819dSTao Ma 				cpu_to_le32(le64_to_cpu(rec->r_cpos) - cpos);
909e73a819dSTao Ma 		else
910e73a819dSTao Ma 			ret_rec->r_clusters = cpu_to_le32(len);
911e73a819dSTao Ma 	}
912e73a819dSTao Ma 
913e73a819dSTao Ma out:
914e73a819dSTao Ma 	*index = i;
915e73a819dSTao Ma }
916e73a819dSTao Ma 
917e73a819dSTao Ma /*
9188b2c0dbaSTao Ma  * Try to remove refcount tree. The mechanism is:
9198b2c0dbaSTao Ma  * 1) Check whether i_clusters == 0, if no, exit.
9208b2c0dbaSTao Ma  * 2) check whether we have i_xattr_loc in dinode. if yes, exit.
9218b2c0dbaSTao Ma  * 3) Check whether we have inline xattr stored outside, if yes, exit.
9228b2c0dbaSTao Ma  * 4) Remove the tree.
9238b2c0dbaSTao Ma  */
ocfs2_try_remove_refcount_tree(struct inode * inode,struct buffer_head * di_bh)9248b2c0dbaSTao Ma int ocfs2_try_remove_refcount_tree(struct inode *inode,
9258b2c0dbaSTao Ma 				   struct buffer_head *di_bh)
9268b2c0dbaSTao Ma {
9278b2c0dbaSTao Ma 	int ret;
9288b2c0dbaSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
9298b2c0dbaSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
9308b2c0dbaSTao Ma 
9318b2c0dbaSTao Ma 	down_write(&oi->ip_xattr_sem);
9328b2c0dbaSTao Ma 	down_write(&oi->ip_alloc_sem);
9338b2c0dbaSTao Ma 
9348b2c0dbaSTao Ma 	if (oi->ip_clusters)
9358b2c0dbaSTao Ma 		goto out;
9368b2c0dbaSTao Ma 
9378b2c0dbaSTao Ma 	if ((oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) && di->i_xattr_loc)
9388b2c0dbaSTao Ma 		goto out;
9398b2c0dbaSTao Ma 
9408b2c0dbaSTao Ma 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL &&
9418b2c0dbaSTao Ma 	    ocfs2_has_inline_xattr_value_outside(inode, di))
9428b2c0dbaSTao Ma 		goto out;
9438b2c0dbaSTao Ma 
9448b2c0dbaSTao Ma 	ret = ocfs2_remove_refcount_tree(inode, di_bh);
9458b2c0dbaSTao Ma 	if (ret)
9468b2c0dbaSTao Ma 		mlog_errno(ret);
9478b2c0dbaSTao Ma out:
9488b2c0dbaSTao Ma 	up_write(&oi->ip_alloc_sem);
9498b2c0dbaSTao Ma 	up_write(&oi->ip_xattr_sem);
9508b2c0dbaSTao Ma 	return 0;
9518b2c0dbaSTao Ma }
9528b2c0dbaSTao Ma 
9538b2c0dbaSTao Ma /*
95438a04e43STao Ma  * Find the end range for a leaf refcount block indicated by
95538a04e43STao Ma  * el->l_recs[index].e_blkno.
95638a04e43STao Ma  */
ocfs2_get_refcount_cpos_end(struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct ocfs2_extent_block * eb,struct ocfs2_extent_list * el,int index,u32 * cpos_end)95738a04e43STao Ma static int ocfs2_get_refcount_cpos_end(struct ocfs2_caching_info *ci,
95838a04e43STao Ma 				       struct buffer_head *ref_root_bh,
95938a04e43STao Ma 				       struct ocfs2_extent_block *eb,
96038a04e43STao Ma 				       struct ocfs2_extent_list *el,
96138a04e43STao Ma 				       int index,  u32 *cpos_end)
96238a04e43STao Ma {
96338a04e43STao Ma 	int ret, i, subtree_root;
96438a04e43STao Ma 	u32 cpos;
96538a04e43STao Ma 	u64 blkno;
96638a04e43STao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
96738a04e43STao Ma 	struct ocfs2_path *left_path = NULL, *right_path = NULL;
96838a04e43STao Ma 	struct ocfs2_extent_tree et;
96938a04e43STao Ma 	struct ocfs2_extent_list *tmp_el;
97038a04e43STao Ma 
97138a04e43STao Ma 	if (index < le16_to_cpu(el->l_next_free_rec) - 1) {
97238a04e43STao Ma 		/*
97338a04e43STao Ma 		 * We have a extent rec after index, so just use the e_cpos
97438a04e43STao Ma 		 * of the next extent rec.
97538a04e43STao Ma 		 */
97638a04e43STao Ma 		*cpos_end = le32_to_cpu(el->l_recs[index+1].e_cpos);
97738a04e43STao Ma 		return 0;
97838a04e43STao Ma 	}
97938a04e43STao Ma 
9807c908aecSJiapeng Chong 	if (!eb || !eb->h_next_leaf_blk) {
98138a04e43STao Ma 		/*
98238a04e43STao Ma 		 * We are the last extent rec, so any high cpos should
98338a04e43STao Ma 		 * be stored in this leaf refcount block.
98438a04e43STao Ma 		 */
98538a04e43STao Ma 		*cpos_end = UINT_MAX;
98638a04e43STao Ma 		return 0;
98738a04e43STao Ma 	}
98838a04e43STao Ma 
98938a04e43STao Ma 	/*
99038a04e43STao Ma 	 * If the extent block isn't the last one, we have to find
99138a04e43STao Ma 	 * the subtree root between this extent block and the next
99238a04e43STao Ma 	 * leaf extent block and get the corresponding e_cpos from
99338a04e43STao Ma 	 * the subroot. Otherwise we may corrupt the b-tree.
99438a04e43STao Ma 	 */
99538a04e43STao Ma 	ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
99638a04e43STao Ma 
99738a04e43STao Ma 	left_path = ocfs2_new_path_from_et(&et);
99838a04e43STao Ma 	if (!left_path) {
99938a04e43STao Ma 		ret = -ENOMEM;
100038a04e43STao Ma 		mlog_errno(ret);
100138a04e43STao Ma 		goto out;
100238a04e43STao Ma 	}
100338a04e43STao Ma 
100438a04e43STao Ma 	cpos = le32_to_cpu(eb->h_list.l_recs[index].e_cpos);
100538a04e43STao Ma 	ret = ocfs2_find_path(ci, left_path, cpos);
100638a04e43STao Ma 	if (ret) {
100738a04e43STao Ma 		mlog_errno(ret);
100838a04e43STao Ma 		goto out;
100938a04e43STao Ma 	}
101038a04e43STao Ma 
101138a04e43STao Ma 	right_path = ocfs2_new_path_from_path(left_path);
101238a04e43STao Ma 	if (!right_path) {
101338a04e43STao Ma 		ret = -ENOMEM;
101438a04e43STao Ma 		mlog_errno(ret);
101538a04e43STao Ma 		goto out;
101638a04e43STao Ma 	}
101738a04e43STao Ma 
101838a04e43STao Ma 	ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, &cpos);
101938a04e43STao Ma 	if (ret) {
102038a04e43STao Ma 		mlog_errno(ret);
102138a04e43STao Ma 		goto out;
102238a04e43STao Ma 	}
102338a04e43STao Ma 
102438a04e43STao Ma 	ret = ocfs2_find_path(ci, right_path, cpos);
102538a04e43STao Ma 	if (ret) {
102638a04e43STao Ma 		mlog_errno(ret);
102738a04e43STao Ma 		goto out;
102838a04e43STao Ma 	}
102938a04e43STao Ma 
103038a04e43STao Ma 	subtree_root = ocfs2_find_subtree_root(&et, left_path,
103138a04e43STao Ma 					       right_path);
103238a04e43STao Ma 
103338a04e43STao Ma 	tmp_el = left_path->p_node[subtree_root].el;
103438a04e43STao Ma 	blkno = left_path->p_node[subtree_root+1].bh->b_blocknr;
10353a251f04SAl Viro 	for (i = 0; i < le16_to_cpu(tmp_el->l_next_free_rec); i++) {
103638a04e43STao Ma 		if (le64_to_cpu(tmp_el->l_recs[i].e_blkno) == blkno) {
103738a04e43STao Ma 			*cpos_end = le32_to_cpu(tmp_el->l_recs[i+1].e_cpos);
103838a04e43STao Ma 			break;
103938a04e43STao Ma 		}
104038a04e43STao Ma 	}
104138a04e43STao Ma 
10423a251f04SAl Viro 	BUG_ON(i == le16_to_cpu(tmp_el->l_next_free_rec));
104338a04e43STao Ma 
104438a04e43STao Ma out:
104538a04e43STao Ma 	ocfs2_free_path(left_path);
104638a04e43STao Ma 	ocfs2_free_path(right_path);
104738a04e43STao Ma 	return ret;
104838a04e43STao Ma }
104938a04e43STao Ma 
105038a04e43STao Ma /*
1051e73a819dSTao Ma  * Given a cpos and len, try to find the refcount record which contains cpos.
1052e73a819dSTao Ma  * 1. If cpos can be found in one refcount record, return the record.
1053e73a819dSTao Ma  * 2. If cpos can't be found, return a fake record which start from cpos
1054e73a819dSTao Ma  *    and end at a small value between cpos+len and start of the next record.
1055e73a819dSTao Ma  *    This fake record has r_refcount = 0.
1056e73a819dSTao Ma  */
ocfs2_get_refcount_rec(struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,u64 cpos,unsigned int len,struct ocfs2_refcount_rec * ret_rec,int * index,struct buffer_head ** ret_bh)1057e73a819dSTao Ma static int ocfs2_get_refcount_rec(struct ocfs2_caching_info *ci,
1058e73a819dSTao Ma 				  struct buffer_head *ref_root_bh,
1059e73a819dSTao Ma 				  u64 cpos, unsigned int len,
1060e73a819dSTao Ma 				  struct ocfs2_refcount_rec *ret_rec,
1061e73a819dSTao Ma 				  int *index,
1062e73a819dSTao Ma 				  struct buffer_head **ret_bh)
1063e73a819dSTao Ma {
1064e73a819dSTao Ma 	int ret = 0, i, found;
10653f649ab7SKees Cook 	u32 low_cpos, cpos_end;
1066e73a819dSTao Ma 	struct ocfs2_extent_list *el;
106738a04e43STao Ma 	struct ocfs2_extent_rec *rec = NULL;
106838a04e43STao Ma 	struct ocfs2_extent_block *eb = NULL;
1069e73a819dSTao Ma 	struct buffer_head *eb_bh = NULL, *ref_leaf_bh = NULL;
1070e73a819dSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1071e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1072e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1073e73a819dSTao Ma 
1074e73a819dSTao Ma 	if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)) {
1075e73a819dSTao Ma 		ocfs2_find_refcount_rec_in_rl(ci, ref_root_bh, cpos, len,
1076e73a819dSTao Ma 					      ret_rec, index);
1077e73a819dSTao Ma 		*ret_bh = ref_root_bh;
1078e73a819dSTao Ma 		get_bh(ref_root_bh);
1079e73a819dSTao Ma 		return 0;
1080e73a819dSTao Ma 	}
1081e73a819dSTao Ma 
1082e73a819dSTao Ma 	el = &rb->rf_list;
1083e73a819dSTao Ma 	low_cpos = cpos & OCFS2_32BIT_POS_MASK;
1084e73a819dSTao Ma 
1085e73a819dSTao Ma 	if (el->l_tree_depth) {
1086e73a819dSTao Ma 		ret = ocfs2_find_leaf(ci, el, low_cpos, &eb_bh);
1087e73a819dSTao Ma 		if (ret) {
1088e73a819dSTao Ma 			mlog_errno(ret);
1089e73a819dSTao Ma 			goto out;
1090e73a819dSTao Ma 		}
1091e73a819dSTao Ma 
1092e73a819dSTao Ma 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1093e73a819dSTao Ma 		el = &eb->h_list;
1094e73a819dSTao Ma 
1095e73a819dSTao Ma 		if (el->l_tree_depth) {
109617a5b9abSGoldwyn Rodrigues 			ret = ocfs2_error(sb,
10977ecef14aSJoe Perches 					  "refcount tree %llu has non zero tree depth in leaf btree tree block %llu\n",
1098e73a819dSTao Ma 					  (unsigned long long)ocfs2_metadata_cache_owner(ci),
1099e73a819dSTao Ma 					  (unsigned long long)eb_bh->b_blocknr);
1100e73a819dSTao Ma 			goto out;
1101e73a819dSTao Ma 		}
1102e73a819dSTao Ma 	}
1103e73a819dSTao Ma 
1104e73a819dSTao Ma 	found = 0;
1105e73a819dSTao Ma 	for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
1106e73a819dSTao Ma 		rec = &el->l_recs[i];
1107e73a819dSTao Ma 
1108e73a819dSTao Ma 		if (le32_to_cpu(rec->e_cpos) <= low_cpos) {
1109e73a819dSTao Ma 			found = 1;
1110e73a819dSTao Ma 			break;
1111e73a819dSTao Ma 		}
1112e73a819dSTao Ma 	}
1113e73a819dSTao Ma 
111438a04e43STao Ma 	if (found) {
111538a04e43STao Ma 		ret = ocfs2_get_refcount_cpos_end(ci, ref_root_bh,
111638a04e43STao Ma 						  eb, el, i, &cpos_end);
111738a04e43STao Ma 		if (ret) {
111838a04e43STao Ma 			mlog_errno(ret);
111938a04e43STao Ma 			goto out;
112038a04e43STao Ma 		}
1121e73a819dSTao Ma 
112238a04e43STao Ma 		if (cpos_end < low_cpos + len)
112338a04e43STao Ma 			len = cpos_end - low_cpos;
1124e73a819dSTao Ma 	}
1125e73a819dSTao Ma 
1126e73a819dSTao Ma 	ret = ocfs2_read_refcount_block(ci, le64_to_cpu(rec->e_blkno),
1127e73a819dSTao Ma 					&ref_leaf_bh);
1128e73a819dSTao Ma 	if (ret) {
1129e73a819dSTao Ma 		mlog_errno(ret);
1130e73a819dSTao Ma 		goto out;
1131e73a819dSTao Ma 	}
1132e73a819dSTao Ma 
1133e73a819dSTao Ma 	ocfs2_find_refcount_rec_in_rl(ci, ref_leaf_bh, cpos, len,
1134e73a819dSTao Ma 				      ret_rec, index);
1135e73a819dSTao Ma 	*ret_bh = ref_leaf_bh;
1136e73a819dSTao Ma out:
1137e73a819dSTao Ma 	brelse(eb_bh);
1138e73a819dSTao Ma 	return ret;
1139e73a819dSTao Ma }
1140e73a819dSTao Ma 
1141e73a819dSTao Ma enum ocfs2_ref_rec_contig {
1142e73a819dSTao Ma 	REF_CONTIG_NONE = 0,
1143e73a819dSTao Ma 	REF_CONTIG_LEFT,
1144e73a819dSTao Ma 	REF_CONTIG_RIGHT,
1145e73a819dSTao Ma 	REF_CONTIG_LEFTRIGHT,
1146e73a819dSTao Ma };
1147e73a819dSTao Ma 
1148e73a819dSTao Ma static enum ocfs2_ref_rec_contig
ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block * rb,int index)1149e73a819dSTao Ma 	ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block *rb,
1150e73a819dSTao Ma 				    int index)
1151e73a819dSTao Ma {
1152e73a819dSTao Ma 	if ((rb->rf_records.rl_recs[index].r_refcount ==
1153e73a819dSTao Ma 	    rb->rf_records.rl_recs[index + 1].r_refcount) &&
1154e73a819dSTao Ma 	    (le64_to_cpu(rb->rf_records.rl_recs[index].r_cpos) +
1155e73a819dSTao Ma 	    le32_to_cpu(rb->rf_records.rl_recs[index].r_clusters) ==
1156e73a819dSTao Ma 	    le64_to_cpu(rb->rf_records.rl_recs[index + 1].r_cpos)))
1157e73a819dSTao Ma 		return REF_CONTIG_RIGHT;
1158e73a819dSTao Ma 
1159e73a819dSTao Ma 	return REF_CONTIG_NONE;
1160e73a819dSTao Ma }
1161e73a819dSTao Ma 
1162e73a819dSTao Ma static enum ocfs2_ref_rec_contig
ocfs2_refcount_rec_contig(struct ocfs2_refcount_block * rb,int index)1163e73a819dSTao Ma 	ocfs2_refcount_rec_contig(struct ocfs2_refcount_block *rb,
1164e73a819dSTao Ma 				  int index)
1165e73a819dSTao Ma {
1166e73a819dSTao Ma 	enum ocfs2_ref_rec_contig ret = REF_CONTIG_NONE;
1167e73a819dSTao Ma 
1168e73a819dSTao Ma 	if (index < le16_to_cpu(rb->rf_records.rl_used) - 1)
1169e73a819dSTao Ma 		ret = ocfs2_refcount_rec_adjacent(rb, index);
1170e73a819dSTao Ma 
1171e73a819dSTao Ma 	if (index > 0) {
1172e73a819dSTao Ma 		enum ocfs2_ref_rec_contig tmp;
1173e73a819dSTao Ma 
1174e73a819dSTao Ma 		tmp = ocfs2_refcount_rec_adjacent(rb, index - 1);
1175e73a819dSTao Ma 
1176e73a819dSTao Ma 		if (tmp == REF_CONTIG_RIGHT) {
1177e73a819dSTao Ma 			if (ret == REF_CONTIG_RIGHT)
1178e73a819dSTao Ma 				ret = REF_CONTIG_LEFTRIGHT;
1179e73a819dSTao Ma 			else
1180e73a819dSTao Ma 				ret = REF_CONTIG_LEFT;
1181e73a819dSTao Ma 		}
1182e73a819dSTao Ma 	}
1183e73a819dSTao Ma 
1184e73a819dSTao Ma 	return ret;
1185e73a819dSTao Ma }
1186e73a819dSTao Ma 
ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block * rb,int index)1187e73a819dSTao Ma static void ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block *rb,
1188e73a819dSTao Ma 					   int index)
1189e73a819dSTao Ma {
1190e73a819dSTao Ma 	BUG_ON(rb->rf_records.rl_recs[index].r_refcount !=
1191e73a819dSTao Ma 	       rb->rf_records.rl_recs[index+1].r_refcount);
1192e73a819dSTao Ma 
1193e73a819dSTao Ma 	le32_add_cpu(&rb->rf_records.rl_recs[index].r_clusters,
1194e73a819dSTao Ma 		     le32_to_cpu(rb->rf_records.rl_recs[index+1].r_clusters));
1195e73a819dSTao Ma 
1196e73a819dSTao Ma 	if (index < le16_to_cpu(rb->rf_records.rl_used) - 2)
1197e73a819dSTao Ma 		memmove(&rb->rf_records.rl_recs[index + 1],
1198e73a819dSTao Ma 			&rb->rf_records.rl_recs[index + 2],
1199e73a819dSTao Ma 			sizeof(struct ocfs2_refcount_rec) *
1200e73a819dSTao Ma 			(le16_to_cpu(rb->rf_records.rl_used) - index - 2));
1201e73a819dSTao Ma 
1202e73a819dSTao Ma 	memset(&rb->rf_records.rl_recs[le16_to_cpu(rb->rf_records.rl_used) - 1],
1203e73a819dSTao Ma 	       0, sizeof(struct ocfs2_refcount_rec));
1204e73a819dSTao Ma 	le16_add_cpu(&rb->rf_records.rl_used, -1);
1205e73a819dSTao Ma }
1206e73a819dSTao Ma 
1207e73a819dSTao Ma /*
1208e73a819dSTao Ma  * Merge the refcount rec if we are contiguous with the adjacent recs.
1209e73a819dSTao Ma  */
ocfs2_refcount_rec_merge(struct ocfs2_refcount_block * rb,int index)1210e73a819dSTao Ma static void ocfs2_refcount_rec_merge(struct ocfs2_refcount_block *rb,
1211e73a819dSTao Ma 				     int index)
1212e73a819dSTao Ma {
1213e73a819dSTao Ma 	enum ocfs2_ref_rec_contig contig =
1214e73a819dSTao Ma 				ocfs2_refcount_rec_contig(rb, index);
1215e73a819dSTao Ma 
1216e73a819dSTao Ma 	if (contig == REF_CONTIG_NONE)
1217e73a819dSTao Ma 		return;
1218e73a819dSTao Ma 
1219e73a819dSTao Ma 	if (contig == REF_CONTIG_LEFT || contig == REF_CONTIG_LEFTRIGHT) {
1220e73a819dSTao Ma 		BUG_ON(index == 0);
1221e73a819dSTao Ma 		index--;
1222e73a819dSTao Ma 	}
1223e73a819dSTao Ma 
1224e73a819dSTao Ma 	ocfs2_rotate_refcount_rec_left(rb, index);
1225e73a819dSTao Ma 
1226e73a819dSTao Ma 	if (contig == REF_CONTIG_LEFTRIGHT)
1227e73a819dSTao Ma 		ocfs2_rotate_refcount_rec_left(rb, index);
1228e73a819dSTao Ma }
1229e73a819dSTao Ma 
12301823cb0bSTao Ma /*
12311823cb0bSTao Ma  * Change the refcount indexed by "index" in ref_bh.
12321823cb0bSTao Ma  * If refcount reaches 0, remove it.
12331823cb0bSTao Ma  */
ocfs2_change_refcount_rec(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_leaf_bh,int index,int merge,int change)1234e73a819dSTao Ma static int ocfs2_change_refcount_rec(handle_t *handle,
1235e73a819dSTao Ma 				     struct ocfs2_caching_info *ci,
1236e73a819dSTao Ma 				     struct buffer_head *ref_leaf_bh,
12377540c1a7STao Ma 				     int index, int merge, int change)
1238e73a819dSTao Ma {
1239e73a819dSTao Ma 	int ret;
1240e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1241e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
12421823cb0bSTao Ma 	struct ocfs2_refcount_list *rl = &rb->rf_records;
12431823cb0bSTao Ma 	struct ocfs2_refcount_rec *rec = &rl->rl_recs[index];
1244e73a819dSTao Ma 
1245e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1246e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1247e73a819dSTao Ma 	if (ret) {
1248e73a819dSTao Ma 		mlog_errno(ret);
1249e73a819dSTao Ma 		goto out;
1250e73a819dSTao Ma 	}
1251e73a819dSTao Ma 
1252198aac28STao Ma 	trace_ocfs2_change_refcount_rec(
1253198aac28STao Ma 		(unsigned long long)ocfs2_metadata_cache_owner(ci),
1254198aac28STao Ma 		index, le32_to_cpu(rec->r_refcount), change);
1255e73a819dSTao Ma 	le32_add_cpu(&rec->r_refcount, change);
1256e73a819dSTao Ma 
12571823cb0bSTao Ma 	if (!rec->r_refcount) {
12581823cb0bSTao Ma 		if (index != le16_to_cpu(rl->rl_used) - 1) {
12591823cb0bSTao Ma 			memmove(rec, rec + 1,
12601823cb0bSTao Ma 				(le16_to_cpu(rl->rl_used) - index - 1) *
12611823cb0bSTao Ma 				sizeof(struct ocfs2_refcount_rec));
12621823cb0bSTao Ma 			memset(&rl->rl_recs[le16_to_cpu(rl->rl_used) - 1],
12631823cb0bSTao Ma 			       0, sizeof(struct ocfs2_refcount_rec));
12641823cb0bSTao Ma 		}
12651823cb0bSTao Ma 
12661823cb0bSTao Ma 		le16_add_cpu(&rl->rl_used, -1);
12677540c1a7STao Ma 	} else if (merge)
1268e73a819dSTao Ma 		ocfs2_refcount_rec_merge(rb, index);
1269e73a819dSTao Ma 
1270ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1271e73a819dSTao Ma out:
1272e73a819dSTao Ma 	return ret;
1273e73a819dSTao Ma }
1274e73a819dSTao Ma 
ocfs2_expand_inline_ref_root(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head ** ref_leaf_bh,struct ocfs2_alloc_context * meta_ac)1275e73a819dSTao Ma static int ocfs2_expand_inline_ref_root(handle_t *handle,
1276e73a819dSTao Ma 					struct ocfs2_caching_info *ci,
1277e73a819dSTao Ma 					struct buffer_head *ref_root_bh,
1278e73a819dSTao Ma 					struct buffer_head **ref_leaf_bh,
1279e73a819dSTao Ma 					struct ocfs2_alloc_context *meta_ac)
1280e73a819dSTao Ma {
1281e73a819dSTao Ma 	int ret;
1282e73a819dSTao Ma 	u16 suballoc_bit_start;
1283e73a819dSTao Ma 	u32 num_got;
12842b6cb576SJoel Becker 	u64 suballoc_loc, blkno;
1285e73a819dSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1286e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1287e73a819dSTao Ma 	struct ocfs2_refcount_block *new_rb;
1288e73a819dSTao Ma 	struct ocfs2_refcount_block *root_rb =
1289e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1290e73a819dSTao Ma 
1291e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
1292e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1293e73a819dSTao Ma 	if (ret) {
1294e73a819dSTao Ma 		mlog_errno(ret);
1295e73a819dSTao Ma 		goto out;
1296e73a819dSTao Ma 	}
1297e73a819dSTao Ma 
12982b6cb576SJoel Becker 	ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
1299e73a819dSTao Ma 				   &suballoc_bit_start, &num_got,
1300e73a819dSTao Ma 				   &blkno);
1301e73a819dSTao Ma 	if (ret) {
1302e73a819dSTao Ma 		mlog_errno(ret);
1303e73a819dSTao Ma 		goto out;
1304e73a819dSTao Ma 	}
1305e73a819dSTao Ma 
1306e73a819dSTao Ma 	new_bh = sb_getblk(sb, blkno);
1307e73a819dSTao Ma 	if (new_bh == NULL) {
13087391a294SRui Xiang 		ret = -ENOMEM;
1309e73a819dSTao Ma 		mlog_errno(ret);
1310e73a819dSTao Ma 		goto out;
1311e73a819dSTao Ma 	}
1312e73a819dSTao Ma 	ocfs2_set_new_buffer_uptodate(ci, new_bh);
1313e73a819dSTao Ma 
1314e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, new_bh,
1315e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_CREATE);
1316e73a819dSTao Ma 	if (ret) {
1317e73a819dSTao Ma 		mlog_errno(ret);
1318e73a819dSTao Ma 		goto out;
1319e73a819dSTao Ma 	}
1320e73a819dSTao Ma 
1321e73a819dSTao Ma 	/*
1322e73a819dSTao Ma 	 * Initialize ocfs2_refcount_block.
1323e73a819dSTao Ma 	 * It should contain the same information as the old root.
1324e73a819dSTao Ma 	 * so just memcpy it and change the corresponding field.
1325e73a819dSTao Ma 	 */
1326e73a819dSTao Ma 	memcpy(new_bh->b_data, ref_root_bh->b_data, sb->s_blocksize);
1327e73a819dSTao Ma 
1328e73a819dSTao Ma 	new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1329b89c5428STiger Yang 	new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
13302b6cb576SJoel Becker 	new_rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc);
1331e73a819dSTao Ma 	new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1332e73a819dSTao Ma 	new_rb->rf_blkno = cpu_to_le64(blkno);
1333e73a819dSTao Ma 	new_rb->rf_cpos = cpu_to_le32(0);
1334e73a819dSTao Ma 	new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
1335e73a819dSTao Ma 	new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
1336e73a819dSTao Ma 	ocfs2_journal_dirty(handle, new_bh);
1337e73a819dSTao Ma 
1338e73a819dSTao Ma 	/* Now change the root. */
1339e73a819dSTao Ma 	memset(&root_rb->rf_list, 0, sb->s_blocksize -
1340e73a819dSTao Ma 	       offsetof(struct ocfs2_refcount_block, rf_list));
1341e73a819dSTao Ma 	root_rb->rf_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_rb(sb));
1342e73a819dSTao Ma 	root_rb->rf_clusters = cpu_to_le32(1);
1343e73a819dSTao Ma 	root_rb->rf_list.l_next_free_rec = cpu_to_le16(1);
1344e73a819dSTao Ma 	root_rb->rf_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
1345e73a819dSTao Ma 	root_rb->rf_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
1346e73a819dSTao Ma 	root_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_TREE_FL);
1347e73a819dSTao Ma 
1348e73a819dSTao Ma 	ocfs2_journal_dirty(handle, ref_root_bh);
1349e73a819dSTao Ma 
1350198aac28STao Ma 	trace_ocfs2_expand_inline_ref_root((unsigned long long)blkno,
1351e73a819dSTao Ma 		le16_to_cpu(new_rb->rf_records.rl_used));
1352e73a819dSTao Ma 
1353e73a819dSTao Ma 	*ref_leaf_bh = new_bh;
1354e73a819dSTao Ma 	new_bh = NULL;
1355e73a819dSTao Ma out:
1356e73a819dSTao Ma 	brelse(new_bh);
1357e73a819dSTao Ma 	return ret;
1358e73a819dSTao Ma }
1359e73a819dSTao Ma 
ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec * prev,struct ocfs2_refcount_rec * next)1360e73a819dSTao Ma static int ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec *prev,
1361e73a819dSTao Ma 					   struct ocfs2_refcount_rec *next)
1362e73a819dSTao Ma {
1363e73a819dSTao Ma 	if (ocfs2_get_ref_rec_low_cpos(prev) + le32_to_cpu(prev->r_clusters) <=
1364e73a819dSTao Ma 		ocfs2_get_ref_rec_low_cpos(next))
1365e73a819dSTao Ma 		return 1;
1366e73a819dSTao Ma 
1367e73a819dSTao Ma 	return 0;
1368e73a819dSTao Ma }
1369e73a819dSTao Ma 
cmp_refcount_rec_by_low_cpos(const void * a,const void * b)1370e73a819dSTao Ma static int cmp_refcount_rec_by_low_cpos(const void *a, const void *b)
1371e73a819dSTao Ma {
1372e73a819dSTao Ma 	const struct ocfs2_refcount_rec *l = a, *r = b;
1373e73a819dSTao Ma 	u32 l_cpos = ocfs2_get_ref_rec_low_cpos(l);
1374e73a819dSTao Ma 	u32 r_cpos = ocfs2_get_ref_rec_low_cpos(r);
1375e73a819dSTao Ma 
1376e73a819dSTao Ma 	if (l_cpos > r_cpos)
1377e73a819dSTao Ma 		return 1;
1378e73a819dSTao Ma 	if (l_cpos < r_cpos)
1379e73a819dSTao Ma 		return -1;
1380e73a819dSTao Ma 	return 0;
1381e73a819dSTao Ma }
1382e73a819dSTao Ma 
cmp_refcount_rec_by_cpos(const void * a,const void * b)1383e73a819dSTao Ma static int cmp_refcount_rec_by_cpos(const void *a, const void *b)
1384e73a819dSTao Ma {
1385e73a819dSTao Ma 	const struct ocfs2_refcount_rec *l = a, *r = b;
1386e73a819dSTao Ma 	u64 l_cpos = le64_to_cpu(l->r_cpos);
1387e73a819dSTao Ma 	u64 r_cpos = le64_to_cpu(r->r_cpos);
1388e73a819dSTao Ma 
1389e73a819dSTao Ma 	if (l_cpos > r_cpos)
1390e73a819dSTao Ma 		return 1;
1391e73a819dSTao Ma 	if (l_cpos < r_cpos)
1392e73a819dSTao Ma 		return -1;
1393e73a819dSTao Ma 	return 0;
1394e73a819dSTao Ma }
1395e73a819dSTao Ma 
swap_refcount_rec(void * a,void * b,int size)1396e73a819dSTao Ma static void swap_refcount_rec(void *a, void *b, int size)
1397e73a819dSTao Ma {
1398a612543fSFabian Frederick 	struct ocfs2_refcount_rec *l = a, *r = b;
1399e73a819dSTao Ma 
1400a612543fSFabian Frederick 	swap(*l, *r);
1401e73a819dSTao Ma }
1402e73a819dSTao Ma 
1403e73a819dSTao Ma /*
1404e73a819dSTao Ma  * The refcount cpos are ordered by their 64bit cpos,
1405e73a819dSTao Ma  * But we will use the low 32 bit to be the e_cpos in the b-tree.
1406e73a819dSTao Ma  * So we need to make sure that this pos isn't intersected with others.
1407e73a819dSTao Ma  *
1408e73a819dSTao Ma  * Note: The refcount block is already sorted by their low 32 bit cpos,
1409e73a819dSTao Ma  *       So just try the middle pos first, and we will exit when we find
1410e73a819dSTao Ma  *       the good position.
1411e73a819dSTao Ma  */
ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list * rl,u32 * split_pos,int * split_index)1412e73a819dSTao Ma static int ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list *rl,
1413e73a819dSTao Ma 					 u32 *split_pos, int *split_index)
1414e73a819dSTao Ma {
1415e73a819dSTao Ma 	int num_used = le16_to_cpu(rl->rl_used);
1416e73a819dSTao Ma 	int delta, middle = num_used / 2;
1417e73a819dSTao Ma 
1418e73a819dSTao Ma 	for (delta = 0; delta < middle; delta++) {
1419e73a819dSTao Ma 		/* Let's check delta earlier than middle */
1420e73a819dSTao Ma 		if (ocfs2_refcount_rec_no_intersect(
1421e73a819dSTao Ma 					&rl->rl_recs[middle - delta - 1],
1422e73a819dSTao Ma 					&rl->rl_recs[middle - delta])) {
1423e73a819dSTao Ma 			*split_index = middle - delta;
1424e73a819dSTao Ma 			break;
1425e73a819dSTao Ma 		}
1426e73a819dSTao Ma 
1427e73a819dSTao Ma 		/* For even counts, don't walk off the end */
1428e73a819dSTao Ma 		if ((middle + delta + 1) == num_used)
1429e73a819dSTao Ma 			continue;
1430e73a819dSTao Ma 
1431e73a819dSTao Ma 		/* Now try delta past middle */
1432e73a819dSTao Ma 		if (ocfs2_refcount_rec_no_intersect(
1433e73a819dSTao Ma 					&rl->rl_recs[middle + delta],
1434e73a819dSTao Ma 					&rl->rl_recs[middle + delta + 1])) {
1435e73a819dSTao Ma 			*split_index = middle + delta + 1;
1436e73a819dSTao Ma 			break;
1437e73a819dSTao Ma 		}
1438e73a819dSTao Ma 	}
1439e73a819dSTao Ma 
1440e73a819dSTao Ma 	if (delta >= middle)
1441e73a819dSTao Ma 		return -ENOSPC;
1442e73a819dSTao Ma 
1443e73a819dSTao Ma 	*split_pos = ocfs2_get_ref_rec_low_cpos(&rl->rl_recs[*split_index]);
1444e73a819dSTao Ma 	return 0;
1445e73a819dSTao Ma }
1446e73a819dSTao Ma 
ocfs2_divide_leaf_refcount_block(struct buffer_head * ref_leaf_bh,struct buffer_head * new_bh,u32 * split_cpos)1447e73a819dSTao Ma static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
1448e73a819dSTao Ma 					    struct buffer_head *new_bh,
1449e73a819dSTao Ma 					    u32 *split_cpos)
1450e73a819dSTao Ma {
1451e73a819dSTao Ma 	int split_index = 0, num_moved, ret;
1452e73a819dSTao Ma 	u32 cpos = 0;
1453e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1454e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1455e73a819dSTao Ma 	struct ocfs2_refcount_list *rl = &rb->rf_records;
1456e73a819dSTao Ma 	struct ocfs2_refcount_block *new_rb =
1457e73a819dSTao Ma 			(struct ocfs2_refcount_block *)new_bh->b_data;
1458e73a819dSTao Ma 	struct ocfs2_refcount_list *new_rl = &new_rb->rf_records;
1459e73a819dSTao Ma 
1460198aac28STao Ma 	trace_ocfs2_divide_leaf_refcount_block(
1461e73a819dSTao Ma 		(unsigned long long)ref_leaf_bh->b_blocknr,
146228748b32SAl Viro 		le16_to_cpu(rl->rl_count), le16_to_cpu(rl->rl_used));
1463e73a819dSTao Ma 
1464e73a819dSTao Ma 	/*
1465e73a819dSTao Ma 	 * XXX: Improvement later.
1466e73a819dSTao Ma 	 * If we know all the high 32 bit cpos is the same, no need to sort.
1467e73a819dSTao Ma 	 *
1468e73a819dSTao Ma 	 * In order to make the whole process safe, we do:
1469e73a819dSTao Ma 	 * 1. sort the entries by their low 32 bit cpos first so that we can
1470e73a819dSTao Ma 	 *    find the split cpos easily.
1471e73a819dSTao Ma 	 * 2. call ocfs2_insert_extent to insert the new refcount block.
1472e73a819dSTao Ma 	 * 3. move the refcount rec to the new block.
1473e73a819dSTao Ma 	 * 4. sort the entries by their 64 bit cpos.
1474e73a819dSTao Ma 	 * 5. dirty the new_rb and rb.
1475e73a819dSTao Ma 	 */
1476e73a819dSTao Ma 	sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
1477e73a819dSTao Ma 	     sizeof(struct ocfs2_refcount_rec),
1478e73a819dSTao Ma 	     cmp_refcount_rec_by_low_cpos, swap_refcount_rec);
1479e73a819dSTao Ma 
1480e73a819dSTao Ma 	ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index);
1481e73a819dSTao Ma 	if (ret) {
1482e73a819dSTao Ma 		mlog_errno(ret);
1483e73a819dSTao Ma 		return ret;
1484e73a819dSTao Ma 	}
1485e73a819dSTao Ma 
1486e73a819dSTao Ma 	new_rb->rf_cpos = cpu_to_le32(cpos);
1487e73a819dSTao Ma 
1488e73a819dSTao Ma 	/* move refcount records starting from split_index to the new block. */
1489e73a819dSTao Ma 	num_moved = le16_to_cpu(rl->rl_used) - split_index;
1490e73a819dSTao Ma 	memcpy(new_rl->rl_recs, &rl->rl_recs[split_index],
1491e73a819dSTao Ma 	       num_moved * sizeof(struct ocfs2_refcount_rec));
1492e73a819dSTao Ma 
1493e73a819dSTao Ma 	/*ok, remove the entries we just moved over to the other block. */
1494e73a819dSTao Ma 	memset(&rl->rl_recs[split_index], 0,
1495e73a819dSTao Ma 	       num_moved * sizeof(struct ocfs2_refcount_rec));
1496e73a819dSTao Ma 
1497e73a819dSTao Ma 	/* change old and new rl_used accordingly. */
1498e73a819dSTao Ma 	le16_add_cpu(&rl->rl_used, -num_moved);
149912d4cec9STao Ma 	new_rl->rl_used = cpu_to_le16(num_moved);
1500e73a819dSTao Ma 
1501e73a819dSTao Ma 	sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
1502e73a819dSTao Ma 	     sizeof(struct ocfs2_refcount_rec),
1503e73a819dSTao Ma 	     cmp_refcount_rec_by_cpos, swap_refcount_rec);
1504e73a819dSTao Ma 
1505e73a819dSTao Ma 	sort(&new_rl->rl_recs, le16_to_cpu(new_rl->rl_used),
1506e73a819dSTao Ma 	     sizeof(struct ocfs2_refcount_rec),
1507e73a819dSTao Ma 	     cmp_refcount_rec_by_cpos, swap_refcount_rec);
1508e73a819dSTao Ma 
1509e73a819dSTao Ma 	*split_cpos = cpos;
1510e73a819dSTao Ma 	return 0;
1511e73a819dSTao Ma }
1512e73a819dSTao Ma 
ocfs2_new_leaf_refcount_block(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head * ref_leaf_bh,struct ocfs2_alloc_context * meta_ac)1513e73a819dSTao Ma static int ocfs2_new_leaf_refcount_block(handle_t *handle,
1514e73a819dSTao Ma 					 struct ocfs2_caching_info *ci,
1515e73a819dSTao Ma 					 struct buffer_head *ref_root_bh,
1516e73a819dSTao Ma 					 struct buffer_head *ref_leaf_bh,
1517e73a819dSTao Ma 					 struct ocfs2_alloc_context *meta_ac)
1518e73a819dSTao Ma {
1519e73a819dSTao Ma 	int ret;
1520e73a819dSTao Ma 	u16 suballoc_bit_start;
1521e73a819dSTao Ma 	u32 num_got, new_cpos;
15222b6cb576SJoel Becker 	u64 suballoc_loc, blkno;
1523e73a819dSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1524e73a819dSTao Ma 	struct ocfs2_refcount_block *root_rb =
1525e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1526e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1527e73a819dSTao Ma 	struct ocfs2_refcount_block *new_rb;
1528e73a819dSTao Ma 	struct ocfs2_extent_tree ref_et;
1529e73a819dSTao Ma 
1530e73a819dSTao Ma 	BUG_ON(!(le32_to_cpu(root_rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL));
1531e73a819dSTao Ma 
1532e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
1533e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1534e73a819dSTao Ma 	if (ret) {
1535e73a819dSTao Ma 		mlog_errno(ret);
1536e73a819dSTao Ma 		goto out;
1537e73a819dSTao Ma 	}
1538e73a819dSTao Ma 
1539e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1540e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1541e73a819dSTao Ma 	if (ret) {
1542e73a819dSTao Ma 		mlog_errno(ret);
1543e73a819dSTao Ma 		goto out;
1544e73a819dSTao Ma 	}
1545e73a819dSTao Ma 
15462b6cb576SJoel Becker 	ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
1547e73a819dSTao Ma 				   &suballoc_bit_start, &num_got,
1548e73a819dSTao Ma 				   &blkno);
1549e73a819dSTao Ma 	if (ret) {
1550e73a819dSTao Ma 		mlog_errno(ret);
1551e73a819dSTao Ma 		goto out;
1552e73a819dSTao Ma 	}
1553e73a819dSTao Ma 
1554e73a819dSTao Ma 	new_bh = sb_getblk(sb, blkno);
1555e73a819dSTao Ma 	if (new_bh == NULL) {
15567391a294SRui Xiang 		ret = -ENOMEM;
1557e73a819dSTao Ma 		mlog_errno(ret);
1558e73a819dSTao Ma 		goto out;
1559e73a819dSTao Ma 	}
1560e73a819dSTao Ma 	ocfs2_set_new_buffer_uptodate(ci, new_bh);
1561e73a819dSTao Ma 
1562e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, new_bh,
1563e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_CREATE);
1564e73a819dSTao Ma 	if (ret) {
1565e73a819dSTao Ma 		mlog_errno(ret);
1566e73a819dSTao Ma 		goto out;
1567e73a819dSTao Ma 	}
1568e73a819dSTao Ma 
1569e73a819dSTao Ma 	/* Initialize ocfs2_refcount_block. */
1570e73a819dSTao Ma 	new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1571e73a819dSTao Ma 	memset(new_rb, 0, sb->s_blocksize);
1572e73a819dSTao Ma 	strcpy((void *)new_rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
1573b89c5428STiger Yang 	new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
15742b6cb576SJoel Becker 	new_rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc);
1575e73a819dSTao Ma 	new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1576e73a819dSTao Ma 	new_rb->rf_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
1577e73a819dSTao Ma 	new_rb->rf_blkno = cpu_to_le64(blkno);
1578e73a819dSTao Ma 	new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
1579e73a819dSTao Ma 	new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
1580e73a819dSTao Ma 	new_rb->rf_records.rl_count =
1581e73a819dSTao Ma 				cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
1582e73a819dSTao Ma 	new_rb->rf_generation = root_rb->rf_generation;
1583e73a819dSTao Ma 
1584e73a819dSTao Ma 	ret = ocfs2_divide_leaf_refcount_block(ref_leaf_bh, new_bh, &new_cpos);
1585e73a819dSTao Ma 	if (ret) {
1586e73a819dSTao Ma 		mlog_errno(ret);
1587e73a819dSTao Ma 		goto out;
1588e73a819dSTao Ma 	}
1589e73a819dSTao Ma 
1590e73a819dSTao Ma 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1591e73a819dSTao Ma 	ocfs2_journal_dirty(handle, new_bh);
1592e73a819dSTao Ma 
1593e73a819dSTao Ma 	ocfs2_init_refcount_extent_tree(&ref_et, ci, ref_root_bh);
1594e73a819dSTao Ma 
1595198aac28STao Ma 	trace_ocfs2_new_leaf_refcount_block(
1596e73a819dSTao Ma 			(unsigned long long)new_bh->b_blocknr, new_cpos);
1597e73a819dSTao Ma 
1598e73a819dSTao Ma 	/* Insert the new leaf block with the specific offset cpos. */
1599e73a819dSTao Ma 	ret = ocfs2_insert_extent(handle, &ref_et, new_cpos, new_bh->b_blocknr,
1600e73a819dSTao Ma 				  1, 0, meta_ac);
1601e73a819dSTao Ma 	if (ret)
1602e73a819dSTao Ma 		mlog_errno(ret);
1603e73a819dSTao Ma 
1604e73a819dSTao Ma out:
1605e73a819dSTao Ma 	brelse(new_bh);
1606e73a819dSTao Ma 	return ret;
1607e73a819dSTao Ma }
1608e73a819dSTao Ma 
ocfs2_expand_refcount_tree(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head * ref_leaf_bh,struct ocfs2_alloc_context * meta_ac)1609e73a819dSTao Ma static int ocfs2_expand_refcount_tree(handle_t *handle,
1610e73a819dSTao Ma 				      struct ocfs2_caching_info *ci,
1611e73a819dSTao Ma 				      struct buffer_head *ref_root_bh,
1612e73a819dSTao Ma 				      struct buffer_head *ref_leaf_bh,
1613e73a819dSTao Ma 				      struct ocfs2_alloc_context *meta_ac)
1614e73a819dSTao Ma {
1615e73a819dSTao Ma 	int ret;
1616e73a819dSTao Ma 	struct buffer_head *expand_bh = NULL;
1617e73a819dSTao Ma 
1618e73a819dSTao Ma 	if (ref_root_bh == ref_leaf_bh) {
1619e73a819dSTao Ma 		/*
1620e73a819dSTao Ma 		 * the old root bh hasn't been expanded to a b-tree,
1621e73a819dSTao Ma 		 * so expand it first.
1622e73a819dSTao Ma 		 */
1623e73a819dSTao Ma 		ret = ocfs2_expand_inline_ref_root(handle, ci, ref_root_bh,
1624e73a819dSTao Ma 						   &expand_bh, meta_ac);
1625e73a819dSTao Ma 		if (ret) {
1626e73a819dSTao Ma 			mlog_errno(ret);
1627e73a819dSTao Ma 			goto out;
1628e73a819dSTao Ma 		}
1629e73a819dSTao Ma 	} else {
1630e73a819dSTao Ma 		expand_bh = ref_leaf_bh;
1631e73a819dSTao Ma 		get_bh(expand_bh);
1632e73a819dSTao Ma 	}
1633e73a819dSTao Ma 
1634e73a819dSTao Ma 
1635e73a819dSTao Ma 	/* Now add a new refcount block into the tree.*/
1636e73a819dSTao Ma 	ret = ocfs2_new_leaf_refcount_block(handle, ci, ref_root_bh,
1637e73a819dSTao Ma 					    expand_bh, meta_ac);
1638e73a819dSTao Ma 	if (ret)
1639e73a819dSTao Ma 		mlog_errno(ret);
1640e73a819dSTao Ma out:
1641e73a819dSTao Ma 	brelse(expand_bh);
1642e73a819dSTao Ma 	return ret;
1643e73a819dSTao Ma }
1644e73a819dSTao Ma 
1645e73a819dSTao Ma /*
1646e73a819dSTao Ma  * Adjust the extent rec in b-tree representing ref_leaf_bh.
1647e73a819dSTao Ma  *
1648e73a819dSTao Ma  * Only called when we have inserted a new refcount rec at index 0
1649e73a819dSTao Ma  * which means ocfs2_extent_rec.e_cpos may need some change.
1650e73a819dSTao Ma  */
ocfs2_adjust_refcount_rec(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head * ref_leaf_bh,struct ocfs2_refcount_rec * rec)1651e73a819dSTao Ma static int ocfs2_adjust_refcount_rec(handle_t *handle,
1652e73a819dSTao Ma 				     struct ocfs2_caching_info *ci,
1653e73a819dSTao Ma 				     struct buffer_head *ref_root_bh,
1654e73a819dSTao Ma 				     struct buffer_head *ref_leaf_bh,
1655e73a819dSTao Ma 				     struct ocfs2_refcount_rec *rec)
1656e73a819dSTao Ma {
1657e73a819dSTao Ma 	int ret = 0, i;
1658e73a819dSTao Ma 	u32 new_cpos, old_cpos;
1659e73a819dSTao Ma 	struct ocfs2_path *path = NULL;
1660e73a819dSTao Ma 	struct ocfs2_extent_tree et;
1661e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1662e73a819dSTao Ma 		(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1663e73a819dSTao Ma 	struct ocfs2_extent_list *el;
1664e73a819dSTao Ma 
1665e73a819dSTao Ma 	if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL))
1666e73a819dSTao Ma 		goto out;
1667e73a819dSTao Ma 
1668e73a819dSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1669e73a819dSTao Ma 	old_cpos = le32_to_cpu(rb->rf_cpos);
1670e73a819dSTao Ma 	new_cpos = le64_to_cpu(rec->r_cpos) & OCFS2_32BIT_POS_MASK;
1671e73a819dSTao Ma 	if (old_cpos <= new_cpos)
1672e73a819dSTao Ma 		goto out;
1673e73a819dSTao Ma 
1674e73a819dSTao Ma 	ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
1675e73a819dSTao Ma 
1676e73a819dSTao Ma 	path = ocfs2_new_path_from_et(&et);
1677e73a819dSTao Ma 	if (!path) {
1678e73a819dSTao Ma 		ret = -ENOMEM;
1679e73a819dSTao Ma 		mlog_errno(ret);
1680e73a819dSTao Ma 		goto out;
1681e73a819dSTao Ma 	}
1682e73a819dSTao Ma 
1683e73a819dSTao Ma 	ret = ocfs2_find_path(ci, path, old_cpos);
1684e73a819dSTao Ma 	if (ret) {
1685e73a819dSTao Ma 		mlog_errno(ret);
1686e73a819dSTao Ma 		goto out;
1687e73a819dSTao Ma 	}
1688e73a819dSTao Ma 
1689e73a819dSTao Ma 	/*
1690e73a819dSTao Ma 	 * 2 more credits, one for the leaf refcount block, one for
1691e73a819dSTao Ma 	 * the extent block contains the extent rec.
1692e73a819dSTao Ma 	 */
1693c901fb00STao Ma 	ret = ocfs2_extend_trans(handle, 2);
1694e73a819dSTao Ma 	if (ret < 0) {
1695e73a819dSTao Ma 		mlog_errno(ret);
1696e73a819dSTao Ma 		goto out;
1697e73a819dSTao Ma 	}
1698e73a819dSTao Ma 
1699e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1700e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1701e73a819dSTao Ma 	if (ret < 0) {
1702e73a819dSTao Ma 		mlog_errno(ret);
1703e73a819dSTao Ma 		goto out;
1704e73a819dSTao Ma 	}
1705e73a819dSTao Ma 
1706e73a819dSTao Ma 	ret = ocfs2_journal_access_eb(handle, ci, path_leaf_bh(path),
1707e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1708e73a819dSTao Ma 	if (ret < 0) {
1709e73a819dSTao Ma 		mlog_errno(ret);
1710e73a819dSTao Ma 		goto out;
1711e73a819dSTao Ma 	}
1712e73a819dSTao Ma 
1713e73a819dSTao Ma 	/* change the leaf extent block first. */
1714e73a819dSTao Ma 	el = path_leaf_el(path);
1715e73a819dSTao Ma 
1716e73a819dSTao Ma 	for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++)
1717e73a819dSTao Ma 		if (le32_to_cpu(el->l_recs[i].e_cpos) == old_cpos)
1718e73a819dSTao Ma 			break;
1719e73a819dSTao Ma 
1720e73a819dSTao Ma 	BUG_ON(i == le16_to_cpu(el->l_next_free_rec));
1721e73a819dSTao Ma 
1722e73a819dSTao Ma 	el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1723e73a819dSTao Ma 
1724e73a819dSTao Ma 	/* change the r_cpos in the leaf block. */
1725e73a819dSTao Ma 	rb->rf_cpos = cpu_to_le32(new_cpos);
1726e73a819dSTao Ma 
1727e73a819dSTao Ma 	ocfs2_journal_dirty(handle, path_leaf_bh(path));
1728e73a819dSTao Ma 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1729e73a819dSTao Ma 
1730e73a819dSTao Ma out:
1731e73a819dSTao Ma 	ocfs2_free_path(path);
1732e73a819dSTao Ma 	return ret;
1733e73a819dSTao Ma }
1734e73a819dSTao Ma 
ocfs2_insert_refcount_rec(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head * ref_leaf_bh,struct ocfs2_refcount_rec * rec,int index,int merge,struct ocfs2_alloc_context * meta_ac)1735e73a819dSTao Ma static int ocfs2_insert_refcount_rec(handle_t *handle,
1736e73a819dSTao Ma 				     struct ocfs2_caching_info *ci,
1737e73a819dSTao Ma 				     struct buffer_head *ref_root_bh,
1738e73a819dSTao Ma 				     struct buffer_head *ref_leaf_bh,
1739e73a819dSTao Ma 				     struct ocfs2_refcount_rec *rec,
17407540c1a7STao Ma 				     int index, int merge,
1741e73a819dSTao Ma 				     struct ocfs2_alloc_context *meta_ac)
1742e73a819dSTao Ma {
1743e73a819dSTao Ma 	int ret;
1744e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1745e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1746e73a819dSTao Ma 	struct ocfs2_refcount_list *rf_list = &rb->rf_records;
1747e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1748e73a819dSTao Ma 
1749e73a819dSTao Ma 	BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
1750e73a819dSTao Ma 
1751e73a819dSTao Ma 	if (rf_list->rl_used == rf_list->rl_count) {
1752e73a819dSTao Ma 		u64 cpos = le64_to_cpu(rec->r_cpos);
1753e73a819dSTao Ma 		u32 len = le32_to_cpu(rec->r_clusters);
1754e73a819dSTao Ma 
1755e73a819dSTao Ma 		ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1756e73a819dSTao Ma 						 ref_leaf_bh, meta_ac);
1757e73a819dSTao Ma 		if (ret) {
1758e73a819dSTao Ma 			mlog_errno(ret);
1759e73a819dSTao Ma 			goto out;
1760e73a819dSTao Ma 		}
1761e73a819dSTao Ma 
1762e73a819dSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1763e73a819dSTao Ma 					     cpos, len, NULL, &index,
1764e73a819dSTao Ma 					     &new_bh);
1765e73a819dSTao Ma 		if (ret) {
1766e73a819dSTao Ma 			mlog_errno(ret);
1767e73a819dSTao Ma 			goto out;
1768e73a819dSTao Ma 		}
1769e73a819dSTao Ma 
1770e73a819dSTao Ma 		ref_leaf_bh = new_bh;
1771e73a819dSTao Ma 		rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1772e73a819dSTao Ma 		rf_list = &rb->rf_records;
1773e73a819dSTao Ma 	}
1774e73a819dSTao Ma 
1775e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1776e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1777e73a819dSTao Ma 	if (ret) {
1778e73a819dSTao Ma 		mlog_errno(ret);
1779e73a819dSTao Ma 		goto out;
1780e73a819dSTao Ma 	}
1781e73a819dSTao Ma 
1782e73a819dSTao Ma 	if (index < le16_to_cpu(rf_list->rl_used))
1783e73a819dSTao Ma 		memmove(&rf_list->rl_recs[index + 1],
1784e73a819dSTao Ma 			&rf_list->rl_recs[index],
1785e73a819dSTao Ma 			(le16_to_cpu(rf_list->rl_used) - index) *
1786e73a819dSTao Ma 			 sizeof(struct ocfs2_refcount_rec));
1787e73a819dSTao Ma 
1788198aac28STao Ma 	trace_ocfs2_insert_refcount_rec(
1789198aac28STao Ma 		(unsigned long long)ref_leaf_bh->b_blocknr, index,
1790e73a819dSTao Ma 		(unsigned long long)le64_to_cpu(rec->r_cpos),
1791198aac28STao Ma 		le32_to_cpu(rec->r_clusters), le32_to_cpu(rec->r_refcount));
1792e73a819dSTao Ma 
1793e73a819dSTao Ma 	rf_list->rl_recs[index] = *rec;
1794e73a819dSTao Ma 
1795e73a819dSTao Ma 	le16_add_cpu(&rf_list->rl_used, 1);
1796e73a819dSTao Ma 
17977540c1a7STao Ma 	if (merge)
1798e73a819dSTao Ma 		ocfs2_refcount_rec_merge(rb, index);
1799e73a819dSTao Ma 
1800ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1801e73a819dSTao Ma 
1802e73a819dSTao Ma 	if (index == 0) {
1803e73a819dSTao Ma 		ret = ocfs2_adjust_refcount_rec(handle, ci,
1804e73a819dSTao Ma 						ref_root_bh,
1805e73a819dSTao Ma 						ref_leaf_bh, rec);
1806e73a819dSTao Ma 		if (ret)
1807e73a819dSTao Ma 			mlog_errno(ret);
1808e73a819dSTao Ma 	}
1809e73a819dSTao Ma out:
1810e73a819dSTao Ma 	brelse(new_bh);
1811e73a819dSTao Ma 	return ret;
1812e73a819dSTao Ma }
1813e73a819dSTao Ma 
1814e73a819dSTao Ma /*
1815e73a819dSTao Ma  * Split the refcount_rec indexed by "index" in ref_leaf_bh.
1816e73a819dSTao Ma  * This is much simple than our b-tree code.
1817e73a819dSTao Ma  * split_rec is the new refcount rec we want to insert.
1818e73a819dSTao Ma  * If split_rec->r_refcount > 0, we are changing the refcount(in case we
1819e73a819dSTao Ma  * increase refcount or decrease a refcount to non-zero).
1820e73a819dSTao Ma  * If split_rec->r_refcount == 0, we are punching a hole in current refcount
1821e73a819dSTao Ma  * rec( in case we decrease a refcount to zero).
1822e73a819dSTao Ma  */
ocfs2_split_refcount_rec(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head * ref_leaf_bh,struct ocfs2_refcount_rec * split_rec,int index,int merge,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)1823e73a819dSTao Ma static int ocfs2_split_refcount_rec(handle_t *handle,
1824e73a819dSTao Ma 				    struct ocfs2_caching_info *ci,
1825e73a819dSTao Ma 				    struct buffer_head *ref_root_bh,
1826e73a819dSTao Ma 				    struct buffer_head *ref_leaf_bh,
1827e73a819dSTao Ma 				    struct ocfs2_refcount_rec *split_rec,
18287540c1a7STao Ma 				    int index, int merge,
1829e73a819dSTao Ma 				    struct ocfs2_alloc_context *meta_ac,
1830e73a819dSTao Ma 				    struct ocfs2_cached_dealloc_ctxt *dealloc)
1831e73a819dSTao Ma {
1832e73a819dSTao Ma 	int ret, recs_need;
1833e73a819dSTao Ma 	u32 len;
1834e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1835e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1836e73a819dSTao Ma 	struct ocfs2_refcount_list *rf_list = &rb->rf_records;
1837e73a819dSTao Ma 	struct ocfs2_refcount_rec *orig_rec = &rf_list->rl_recs[index];
1838e73a819dSTao Ma 	struct ocfs2_refcount_rec *tail_rec = NULL;
1839e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1840e73a819dSTao Ma 
1841e73a819dSTao Ma 	BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
1842e73a819dSTao Ma 
1843198aac28STao Ma 	trace_ocfs2_split_refcount_rec(le64_to_cpu(orig_rec->r_cpos),
1844198aac28STao Ma 		le32_to_cpu(orig_rec->r_clusters),
1845198aac28STao Ma 		le32_to_cpu(orig_rec->r_refcount),
1846e73a819dSTao Ma 		le64_to_cpu(split_rec->r_cpos),
1847198aac28STao Ma 		le32_to_cpu(split_rec->r_clusters),
1848198aac28STao Ma 		le32_to_cpu(split_rec->r_refcount));
1849e73a819dSTao Ma 
1850e73a819dSTao Ma 	/*
1851e73a819dSTao Ma 	 * If we just need to split the header or tail clusters,
1852e73a819dSTao Ma 	 * no more recs are needed, just split is OK.
1853e73a819dSTao Ma 	 * Otherwise we at least need one new recs.
1854e73a819dSTao Ma 	 */
1855e73a819dSTao Ma 	if (!split_rec->r_refcount &&
1856e73a819dSTao Ma 	    (split_rec->r_cpos == orig_rec->r_cpos ||
1857e73a819dSTao Ma 	     le64_to_cpu(split_rec->r_cpos) +
1858e73a819dSTao Ma 	     le32_to_cpu(split_rec->r_clusters) ==
1859e73a819dSTao Ma 	     le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
1860e73a819dSTao Ma 		recs_need = 0;
1861e73a819dSTao Ma 	else
1862e73a819dSTao Ma 		recs_need = 1;
1863e73a819dSTao Ma 
1864e73a819dSTao Ma 	/*
1865e73a819dSTao Ma 	 * We need one more rec if we split in the middle and the new rec have
1866e73a819dSTao Ma 	 * some refcount in it.
1867e73a819dSTao Ma 	 */
1868e73a819dSTao Ma 	if (split_rec->r_refcount &&
1869e73a819dSTao Ma 	    (split_rec->r_cpos != orig_rec->r_cpos &&
1870e73a819dSTao Ma 	     le64_to_cpu(split_rec->r_cpos) +
1871e73a819dSTao Ma 	     le32_to_cpu(split_rec->r_clusters) !=
1872e73a819dSTao Ma 	     le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
1873e73a819dSTao Ma 		recs_need++;
1874e73a819dSTao Ma 
1875e73a819dSTao Ma 	/* If the leaf block don't have enough record, expand it. */
187612d4cec9STao Ma 	if (le16_to_cpu(rf_list->rl_used) + recs_need >
187712d4cec9STao Ma 					 le16_to_cpu(rf_list->rl_count)) {
1878e73a819dSTao Ma 		struct ocfs2_refcount_rec tmp_rec;
1879e73a819dSTao Ma 		u64 cpos = le64_to_cpu(orig_rec->r_cpos);
1880e73a819dSTao Ma 		len = le32_to_cpu(orig_rec->r_clusters);
1881e73a819dSTao Ma 		ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1882e73a819dSTao Ma 						 ref_leaf_bh, meta_ac);
1883e73a819dSTao Ma 		if (ret) {
1884e73a819dSTao Ma 			mlog_errno(ret);
1885e73a819dSTao Ma 			goto out;
1886e73a819dSTao Ma 		}
1887e73a819dSTao Ma 
1888e73a819dSTao Ma 		/*
1889e73a819dSTao Ma 		 * We have to re-get it since now cpos may be moved to
1890e73a819dSTao Ma 		 * another leaf block.
1891e73a819dSTao Ma 		 */
1892e73a819dSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1893e73a819dSTao Ma 					     cpos, len, &tmp_rec, &index,
1894e73a819dSTao Ma 					     &new_bh);
1895e73a819dSTao Ma 		if (ret) {
1896e73a819dSTao Ma 			mlog_errno(ret);
1897e73a819dSTao Ma 			goto out;
1898e73a819dSTao Ma 		}
1899e73a819dSTao Ma 
1900e73a819dSTao Ma 		ref_leaf_bh = new_bh;
1901e73a819dSTao Ma 		rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1902e73a819dSTao Ma 		rf_list = &rb->rf_records;
1903e73a819dSTao Ma 		orig_rec = &rf_list->rl_recs[index];
1904e73a819dSTao Ma 	}
1905e73a819dSTao Ma 
1906e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1907e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1908e73a819dSTao Ma 	if (ret) {
1909e73a819dSTao Ma 		mlog_errno(ret);
1910e73a819dSTao Ma 		goto out;
1911e73a819dSTao Ma 	}
1912e73a819dSTao Ma 
1913e73a819dSTao Ma 	/*
1914e73a819dSTao Ma 	 * We have calculated out how many new records we need and store
1915e73a819dSTao Ma 	 * in recs_need, so spare enough space first by moving the records
1916e73a819dSTao Ma 	 * after "index" to the end.
1917e73a819dSTao Ma 	 */
1918e73a819dSTao Ma 	if (index != le16_to_cpu(rf_list->rl_used) - 1)
1919e73a819dSTao Ma 		memmove(&rf_list->rl_recs[index + 1 + recs_need],
1920e73a819dSTao Ma 			&rf_list->rl_recs[index + 1],
1921e73a819dSTao Ma 			(le16_to_cpu(rf_list->rl_used) - index - 1) *
1922e73a819dSTao Ma 			 sizeof(struct ocfs2_refcount_rec));
1923e73a819dSTao Ma 
1924e73a819dSTao Ma 	len = (le64_to_cpu(orig_rec->r_cpos) +
1925e73a819dSTao Ma 	      le32_to_cpu(orig_rec->r_clusters)) -
1926e73a819dSTao Ma 	      (le64_to_cpu(split_rec->r_cpos) +
1927e73a819dSTao Ma 	      le32_to_cpu(split_rec->r_clusters));
1928e73a819dSTao Ma 
1929e73a819dSTao Ma 	/*
1930e73a819dSTao Ma 	 * If we have "len", the we will split in the tail and move it
1931e73a819dSTao Ma 	 * to the end of the space we have just spared.
1932e73a819dSTao Ma 	 */
1933e73a819dSTao Ma 	if (len) {
1934e73a819dSTao Ma 		tail_rec = &rf_list->rl_recs[index + recs_need];
1935e73a819dSTao Ma 
1936e73a819dSTao Ma 		memcpy(tail_rec, orig_rec, sizeof(struct ocfs2_refcount_rec));
1937e73a819dSTao Ma 		le64_add_cpu(&tail_rec->r_cpos,
1938e73a819dSTao Ma 			     le32_to_cpu(tail_rec->r_clusters) - len);
193912d4cec9STao Ma 		tail_rec->r_clusters = cpu_to_le32(len);
1940e73a819dSTao Ma 	}
1941e73a819dSTao Ma 
1942e73a819dSTao Ma 	/*
1943e73a819dSTao Ma 	 * If the split pos isn't the same as the original one, we need to
1944e73a819dSTao Ma 	 * split in the head.
1945e73a819dSTao Ma 	 *
1946e73a819dSTao Ma 	 * Note: We have the chance that split_rec.r_refcount = 0,
1947e73a819dSTao Ma 	 * recs_need = 0 and len > 0, which means we just cut the head from
1948e73a819dSTao Ma 	 * the orig_rec and in that case we have done some modification in
1949e73a819dSTao Ma 	 * orig_rec above, so the check for r_cpos is faked.
1950e73a819dSTao Ma 	 */
1951e73a819dSTao Ma 	if (split_rec->r_cpos != orig_rec->r_cpos && tail_rec != orig_rec) {
1952e73a819dSTao Ma 		len = le64_to_cpu(split_rec->r_cpos) -
1953e73a819dSTao Ma 		      le64_to_cpu(orig_rec->r_cpos);
1954e73a819dSTao Ma 		orig_rec->r_clusters = cpu_to_le32(len);
1955e73a819dSTao Ma 		index++;
1956e73a819dSTao Ma 	}
1957e73a819dSTao Ma 
1958e73a819dSTao Ma 	le16_add_cpu(&rf_list->rl_used, recs_need);
1959e73a819dSTao Ma 
1960e73a819dSTao Ma 	if (split_rec->r_refcount) {
1961e73a819dSTao Ma 		rf_list->rl_recs[index] = *split_rec;
1962198aac28STao Ma 		trace_ocfs2_split_refcount_rec_insert(
1963198aac28STao Ma 			(unsigned long long)ref_leaf_bh->b_blocknr, index,
1964e73a819dSTao Ma 			(unsigned long long)le64_to_cpu(split_rec->r_cpos),
1965e73a819dSTao Ma 			le32_to_cpu(split_rec->r_clusters),
1966198aac28STao Ma 			le32_to_cpu(split_rec->r_refcount));
1967e73a819dSTao Ma 
19687540c1a7STao Ma 		if (merge)
1969e73a819dSTao Ma 			ocfs2_refcount_rec_merge(rb, index);
1970e73a819dSTao Ma 	}
1971e73a819dSTao Ma 
1972ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1973e73a819dSTao Ma 
1974e73a819dSTao Ma out:
1975e73a819dSTao Ma 	brelse(new_bh);
1976e73a819dSTao Ma 	return ret;
1977e73a819dSTao Ma }
1978e73a819dSTao Ma 
__ocfs2_increase_refcount(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,u64 cpos,u32 len,int merge,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)19797540c1a7STao Ma static int __ocfs2_increase_refcount(handle_t *handle,
1980e73a819dSTao Ma 				     struct ocfs2_caching_info *ci,
1981e73a819dSTao Ma 				     struct buffer_head *ref_root_bh,
19827540c1a7STao Ma 				     u64 cpos, u32 len, int merge,
1983e73a819dSTao Ma 				     struct ocfs2_alloc_context *meta_ac,
1984e73a819dSTao Ma 				     struct ocfs2_cached_dealloc_ctxt *dealloc)
1985e73a819dSTao Ma {
1986e73a819dSTao Ma 	int ret = 0, index;
1987e73a819dSTao Ma 	struct buffer_head *ref_leaf_bh = NULL;
1988e73a819dSTao Ma 	struct ocfs2_refcount_rec rec;
1989e73a819dSTao Ma 	unsigned int set_len = 0;
1990e73a819dSTao Ma 
1991198aac28STao Ma 	trace_ocfs2_increase_refcount_begin(
1992e73a819dSTao Ma 	     (unsigned long long)ocfs2_metadata_cache_owner(ci),
1993e73a819dSTao Ma 	     (unsigned long long)cpos, len);
1994e73a819dSTao Ma 
1995e73a819dSTao Ma 	while (len) {
1996e73a819dSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1997e73a819dSTao Ma 					     cpos, len, &rec, &index,
1998e73a819dSTao Ma 					     &ref_leaf_bh);
1999e73a819dSTao Ma 		if (ret) {
2000e73a819dSTao Ma 			mlog_errno(ret);
2001e73a819dSTao Ma 			goto out;
2002e73a819dSTao Ma 		}
2003e73a819dSTao Ma 
2004e73a819dSTao Ma 		set_len = le32_to_cpu(rec.r_clusters);
2005e73a819dSTao Ma 
2006e73a819dSTao Ma 		/*
2007e73a819dSTao Ma 		 * Here we may meet with 3 situations:
2008e73a819dSTao Ma 		 *
2009e73a819dSTao Ma 		 * 1. If we find an already existing record, and the length
2010e73a819dSTao Ma 		 *    is the same, cool, we just need to increase the r_refcount
2011e73a819dSTao Ma 		 *    and it is OK.
2012e73a819dSTao Ma 		 * 2. If we find a hole, just insert it with r_refcount = 1.
2013e73a819dSTao Ma 		 * 3. If we are in the middle of one extent record, split
2014e73a819dSTao Ma 		 *    it.
2015e73a819dSTao Ma 		 */
2016e73a819dSTao Ma 		if (rec.r_refcount && le64_to_cpu(rec.r_cpos) == cpos &&
2017e73a819dSTao Ma 		    set_len <= len) {
2018198aac28STao Ma 			trace_ocfs2_increase_refcount_change(
2019198aac28STao Ma 				(unsigned long long)cpos, set_len,
2020e73a819dSTao Ma 				le32_to_cpu(rec.r_refcount));
2021e73a819dSTao Ma 			ret = ocfs2_change_refcount_rec(handle, ci,
20227540c1a7STao Ma 							ref_leaf_bh, index,
20237540c1a7STao Ma 							merge, 1);
2024e73a819dSTao Ma 			if (ret) {
2025e73a819dSTao Ma 				mlog_errno(ret);
2026e73a819dSTao Ma 				goto out;
2027e73a819dSTao Ma 			}
2028e73a819dSTao Ma 		} else if (!rec.r_refcount) {
2029e73a819dSTao Ma 			rec.r_refcount = cpu_to_le32(1);
2030e73a819dSTao Ma 
2031198aac28STao Ma 			trace_ocfs2_increase_refcount_insert(
2032e73a819dSTao Ma 			     (unsigned long long)le64_to_cpu(rec.r_cpos),
2033e73a819dSTao Ma 			     set_len);
2034e73a819dSTao Ma 			ret = ocfs2_insert_refcount_rec(handle, ci, ref_root_bh,
2035e73a819dSTao Ma 							ref_leaf_bh,
20367540c1a7STao Ma 							&rec, index,
20377540c1a7STao Ma 							merge, meta_ac);
2038e73a819dSTao Ma 			if (ret) {
2039e73a819dSTao Ma 				mlog_errno(ret);
2040e73a819dSTao Ma 				goto out;
2041e73a819dSTao Ma 			}
2042e73a819dSTao Ma 		} else  {
2043e73a819dSTao Ma 			set_len = min((u64)(cpos + len),
2044e73a819dSTao Ma 				      le64_to_cpu(rec.r_cpos) + set_len) - cpos;
2045e73a819dSTao Ma 			rec.r_cpos = cpu_to_le64(cpos);
2046e73a819dSTao Ma 			rec.r_clusters = cpu_to_le32(set_len);
2047e73a819dSTao Ma 			le32_add_cpu(&rec.r_refcount, 1);
2048e73a819dSTao Ma 
2049198aac28STao Ma 			trace_ocfs2_increase_refcount_split(
2050e73a819dSTao Ma 			     (unsigned long long)le64_to_cpu(rec.r_cpos),
2051e73a819dSTao Ma 			     set_len, le32_to_cpu(rec.r_refcount));
2052e73a819dSTao Ma 			ret = ocfs2_split_refcount_rec(handle, ci,
2053e73a819dSTao Ma 						       ref_root_bh, ref_leaf_bh,
20547540c1a7STao Ma 						       &rec, index, merge,
2055e73a819dSTao Ma 						       meta_ac, dealloc);
2056e73a819dSTao Ma 			if (ret) {
2057e73a819dSTao Ma 				mlog_errno(ret);
2058e73a819dSTao Ma 				goto out;
2059e73a819dSTao Ma 			}
2060e73a819dSTao Ma 		}
2061e73a819dSTao Ma 
2062e73a819dSTao Ma 		cpos += set_len;
2063e73a819dSTao Ma 		len -= set_len;
2064e73a819dSTao Ma 		brelse(ref_leaf_bh);
2065e73a819dSTao Ma 		ref_leaf_bh = NULL;
2066e73a819dSTao Ma 	}
2067e73a819dSTao Ma 
2068e73a819dSTao Ma out:
2069e73a819dSTao Ma 	brelse(ref_leaf_bh);
2070e73a819dSTao Ma 	return ret;
2071e73a819dSTao Ma }
20721823cb0bSTao Ma 
ocfs2_remove_refcount_extent(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head * ref_leaf_bh,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)20731823cb0bSTao Ma static int ocfs2_remove_refcount_extent(handle_t *handle,
20741823cb0bSTao Ma 				struct ocfs2_caching_info *ci,
20751823cb0bSTao Ma 				struct buffer_head *ref_root_bh,
20761823cb0bSTao Ma 				struct buffer_head *ref_leaf_bh,
20771823cb0bSTao Ma 				struct ocfs2_alloc_context *meta_ac,
20781823cb0bSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
20791823cb0bSTao Ma {
20801823cb0bSTao Ma 	int ret;
20811823cb0bSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
20821823cb0bSTao Ma 	struct ocfs2_refcount_block *rb =
20831823cb0bSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
20841823cb0bSTao Ma 	struct ocfs2_extent_tree et;
20851823cb0bSTao Ma 
20861823cb0bSTao Ma 	BUG_ON(rb->rf_records.rl_used);
20871823cb0bSTao Ma 
2088198aac28STao Ma 	trace_ocfs2_remove_refcount_extent(
2089198aac28STao Ma 		(unsigned long long)ocfs2_metadata_cache_owner(ci),
2090198aac28STao Ma 		(unsigned long long)ref_leaf_bh->b_blocknr,
2091198aac28STao Ma 		le32_to_cpu(rb->rf_cpos));
2092198aac28STao Ma 
20931823cb0bSTao Ma 	ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
20941823cb0bSTao Ma 	ret = ocfs2_remove_extent(handle, &et, le32_to_cpu(rb->rf_cpos),
20951823cb0bSTao Ma 				  1, meta_ac, dealloc);
20961823cb0bSTao Ma 	if (ret) {
20971823cb0bSTao Ma 		mlog_errno(ret);
20981823cb0bSTao Ma 		goto out;
20991823cb0bSTao Ma 	}
21001823cb0bSTao Ma 
21011823cb0bSTao Ma 	ocfs2_remove_from_cache(ci, ref_leaf_bh);
21021823cb0bSTao Ma 
21031823cb0bSTao Ma 	/*
21041823cb0bSTao Ma 	 * add the freed block to the dealloc so that it will be freed
21051823cb0bSTao Ma 	 * when we run dealloc.
21061823cb0bSTao Ma 	 */
21071823cb0bSTao Ma 	ret = ocfs2_cache_block_dealloc(dealloc, EXTENT_ALLOC_SYSTEM_INODE,
21081823cb0bSTao Ma 					le16_to_cpu(rb->rf_suballoc_slot),
210974380c47STao Ma 					le64_to_cpu(rb->rf_suballoc_loc),
21101823cb0bSTao Ma 					le64_to_cpu(rb->rf_blkno),
21111823cb0bSTao Ma 					le16_to_cpu(rb->rf_suballoc_bit));
21121823cb0bSTao Ma 	if (ret) {
21131823cb0bSTao Ma 		mlog_errno(ret);
21141823cb0bSTao Ma 		goto out;
21151823cb0bSTao Ma 	}
21161823cb0bSTao Ma 
21171823cb0bSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
21181823cb0bSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
21191823cb0bSTao Ma 	if (ret) {
21201823cb0bSTao Ma 		mlog_errno(ret);
21211823cb0bSTao Ma 		goto out;
21221823cb0bSTao Ma 	}
21231823cb0bSTao Ma 
21241823cb0bSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
21251823cb0bSTao Ma 
21261823cb0bSTao Ma 	le32_add_cpu(&rb->rf_clusters, -1);
21271823cb0bSTao Ma 
21281823cb0bSTao Ma 	/*
21291823cb0bSTao Ma 	 * check whether we need to restore the root refcount block if
21301823cb0bSTao Ma 	 * there is no leaf extent block at atll.
21311823cb0bSTao Ma 	 */
21321823cb0bSTao Ma 	if (!rb->rf_list.l_next_free_rec) {
21331823cb0bSTao Ma 		BUG_ON(rb->rf_clusters);
21341823cb0bSTao Ma 
2135198aac28STao Ma 		trace_ocfs2_restore_refcount_block(
21361823cb0bSTao Ma 		     (unsigned long long)ref_root_bh->b_blocknr);
21371823cb0bSTao Ma 
21381823cb0bSTao Ma 		rb->rf_flags = 0;
21391823cb0bSTao Ma 		rb->rf_parent = 0;
21401823cb0bSTao Ma 		rb->rf_cpos = 0;
21411823cb0bSTao Ma 		memset(&rb->rf_records, 0, sb->s_blocksize -
21421823cb0bSTao Ma 		       offsetof(struct ocfs2_refcount_block, rf_records));
21431823cb0bSTao Ma 		rb->rf_records.rl_count =
21441823cb0bSTao Ma 				cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
21451823cb0bSTao Ma 	}
21461823cb0bSTao Ma 
21471823cb0bSTao Ma 	ocfs2_journal_dirty(handle, ref_root_bh);
21481823cb0bSTao Ma 
21491823cb0bSTao Ma out:
21501823cb0bSTao Ma 	return ret;
21511823cb0bSTao Ma }
21521823cb0bSTao Ma 
ocfs2_increase_refcount(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,u64 cpos,u32 len,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)21537540c1a7STao Ma int ocfs2_increase_refcount(handle_t *handle,
21547540c1a7STao Ma 			    struct ocfs2_caching_info *ci,
21557540c1a7STao Ma 			    struct buffer_head *ref_root_bh,
21567540c1a7STao Ma 			    u64 cpos, u32 len,
21577540c1a7STao Ma 			    struct ocfs2_alloc_context *meta_ac,
21587540c1a7STao Ma 			    struct ocfs2_cached_dealloc_ctxt *dealloc)
21597540c1a7STao Ma {
21607540c1a7STao Ma 	return __ocfs2_increase_refcount(handle, ci, ref_root_bh,
21617540c1a7STao Ma 					 cpos, len, 1,
21627540c1a7STao Ma 					 meta_ac, dealloc);
21637540c1a7STao Ma }
21647540c1a7STao Ma 
ocfs2_decrease_refcount_rec(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head * ref_leaf_bh,int index,u64 cpos,unsigned int len,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)21651823cb0bSTao Ma static int ocfs2_decrease_refcount_rec(handle_t *handle,
21661823cb0bSTao Ma 				struct ocfs2_caching_info *ci,
21671823cb0bSTao Ma 				struct buffer_head *ref_root_bh,
21681823cb0bSTao Ma 				struct buffer_head *ref_leaf_bh,
21691823cb0bSTao Ma 				int index, u64 cpos, unsigned int len,
21701823cb0bSTao Ma 				struct ocfs2_alloc_context *meta_ac,
21711823cb0bSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
21721823cb0bSTao Ma {
21731823cb0bSTao Ma 	int ret;
21741823cb0bSTao Ma 	struct ocfs2_refcount_block *rb =
21751823cb0bSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
21761823cb0bSTao Ma 	struct ocfs2_refcount_rec *rec = &rb->rf_records.rl_recs[index];
21771823cb0bSTao Ma 
21781823cb0bSTao Ma 	BUG_ON(cpos < le64_to_cpu(rec->r_cpos));
21791823cb0bSTao Ma 	BUG_ON(cpos + len >
21801823cb0bSTao Ma 	       le64_to_cpu(rec->r_cpos) + le32_to_cpu(rec->r_clusters));
21811823cb0bSTao Ma 
2182198aac28STao Ma 	trace_ocfs2_decrease_refcount_rec(
2183198aac28STao Ma 		(unsigned long long)ocfs2_metadata_cache_owner(ci),
2184198aac28STao Ma 		(unsigned long long)cpos, len);
2185198aac28STao Ma 
21861823cb0bSTao Ma 	if (cpos == le64_to_cpu(rec->r_cpos) &&
21871823cb0bSTao Ma 	    len == le32_to_cpu(rec->r_clusters))
21881823cb0bSTao Ma 		ret = ocfs2_change_refcount_rec(handle, ci,
21897540c1a7STao Ma 						ref_leaf_bh, index, 1, -1);
21901823cb0bSTao Ma 	else {
21911823cb0bSTao Ma 		struct ocfs2_refcount_rec split = *rec;
21921823cb0bSTao Ma 		split.r_cpos = cpu_to_le64(cpos);
21931823cb0bSTao Ma 		split.r_clusters = cpu_to_le32(len);
21941823cb0bSTao Ma 
21951823cb0bSTao Ma 		le32_add_cpu(&split.r_refcount, -1);
21961823cb0bSTao Ma 
21971823cb0bSTao Ma 		ret = ocfs2_split_refcount_rec(handle, ci,
21981823cb0bSTao Ma 					       ref_root_bh, ref_leaf_bh,
21997540c1a7STao Ma 					       &split, index, 1,
22001823cb0bSTao Ma 					       meta_ac, dealloc);
22011823cb0bSTao Ma 	}
22021823cb0bSTao Ma 
22031823cb0bSTao Ma 	if (ret) {
22041823cb0bSTao Ma 		mlog_errno(ret);
22051823cb0bSTao Ma 		goto out;
22061823cb0bSTao Ma 	}
22071823cb0bSTao Ma 
22081823cb0bSTao Ma 	/* Remove the leaf refcount block if it contains no refcount record. */
22091823cb0bSTao Ma 	if (!rb->rf_records.rl_used && ref_leaf_bh != ref_root_bh) {
22101823cb0bSTao Ma 		ret = ocfs2_remove_refcount_extent(handle, ci, ref_root_bh,
22111823cb0bSTao Ma 						   ref_leaf_bh, meta_ac,
22121823cb0bSTao Ma 						   dealloc);
22131823cb0bSTao Ma 		if (ret)
22141823cb0bSTao Ma 			mlog_errno(ret);
22151823cb0bSTao Ma 	}
22161823cb0bSTao Ma 
22171823cb0bSTao Ma out:
22181823cb0bSTao Ma 	return ret;
22191823cb0bSTao Ma }
22201823cb0bSTao Ma 
__ocfs2_decrease_refcount(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,u64 cpos,u32 len,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc,int delete)22211823cb0bSTao Ma static int __ocfs2_decrease_refcount(handle_t *handle,
22221823cb0bSTao Ma 				     struct ocfs2_caching_info *ci,
22231823cb0bSTao Ma 				     struct buffer_head *ref_root_bh,
22241823cb0bSTao Ma 				     u64 cpos, u32 len,
22251823cb0bSTao Ma 				     struct ocfs2_alloc_context *meta_ac,
22266ae23c55STao Ma 				     struct ocfs2_cached_dealloc_ctxt *dealloc,
22276ae23c55STao Ma 				     int delete)
22281823cb0bSTao Ma {
22291823cb0bSTao Ma 	int ret = 0, index = 0;
22301823cb0bSTao Ma 	struct ocfs2_refcount_rec rec;
22311823cb0bSTao Ma 	unsigned int r_count = 0, r_len;
22321823cb0bSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
22331823cb0bSTao Ma 	struct buffer_head *ref_leaf_bh = NULL;
22341823cb0bSTao Ma 
2235198aac28STao Ma 	trace_ocfs2_decrease_refcount(
22361823cb0bSTao Ma 		(unsigned long long)ocfs2_metadata_cache_owner(ci),
22376ae23c55STao Ma 		(unsigned long long)cpos, len, delete);
22381823cb0bSTao Ma 
22391823cb0bSTao Ma 	while (len) {
22401823cb0bSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
22411823cb0bSTao Ma 					     cpos, len, &rec, &index,
22421823cb0bSTao Ma 					     &ref_leaf_bh);
22431823cb0bSTao Ma 		if (ret) {
22441823cb0bSTao Ma 			mlog_errno(ret);
22451823cb0bSTao Ma 			goto out;
22461823cb0bSTao Ma 		}
22471823cb0bSTao Ma 
22481823cb0bSTao Ma 		r_count = le32_to_cpu(rec.r_refcount);
22491823cb0bSTao Ma 		BUG_ON(r_count == 0);
22506ae23c55STao Ma 		if (!delete)
22516ae23c55STao Ma 			BUG_ON(r_count > 1);
22521823cb0bSTao Ma 
22531823cb0bSTao Ma 		r_len = min((u64)(cpos + len), le64_to_cpu(rec.r_cpos) +
22541823cb0bSTao Ma 			      le32_to_cpu(rec.r_clusters)) - cpos;
22551823cb0bSTao Ma 
22561823cb0bSTao Ma 		ret = ocfs2_decrease_refcount_rec(handle, ci, ref_root_bh,
22571823cb0bSTao Ma 						  ref_leaf_bh, index,
22581823cb0bSTao Ma 						  cpos, r_len,
22591823cb0bSTao Ma 						  meta_ac, dealloc);
22601823cb0bSTao Ma 		if (ret) {
22611823cb0bSTao Ma 			mlog_errno(ret);
22621823cb0bSTao Ma 			goto out;
22631823cb0bSTao Ma 		}
22641823cb0bSTao Ma 
22656ae23c55STao Ma 		if (le32_to_cpu(rec.r_refcount) == 1 && delete) {
22661823cb0bSTao Ma 			ret = ocfs2_cache_cluster_dealloc(dealloc,
22671823cb0bSTao Ma 					  ocfs2_clusters_to_blocks(sb, cpos),
22681823cb0bSTao Ma 							  r_len);
22691823cb0bSTao Ma 			if (ret) {
22701823cb0bSTao Ma 				mlog_errno(ret);
22711823cb0bSTao Ma 				goto out;
22721823cb0bSTao Ma 			}
22731823cb0bSTao Ma 		}
22741823cb0bSTao Ma 
22751823cb0bSTao Ma 		cpos += r_len;
22761823cb0bSTao Ma 		len -= r_len;
22771823cb0bSTao Ma 		brelse(ref_leaf_bh);
22781823cb0bSTao Ma 		ref_leaf_bh = NULL;
22791823cb0bSTao Ma 	}
22801823cb0bSTao Ma 
22811823cb0bSTao Ma out:
22821823cb0bSTao Ma 	brelse(ref_leaf_bh);
22831823cb0bSTao Ma 	return ret;
22841823cb0bSTao Ma }
22851823cb0bSTao Ma 
22861823cb0bSTao Ma /* Caller must hold refcount tree lock. */
ocfs2_decrease_refcount(struct inode * inode,handle_t * handle,u32 cpos,u32 len,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc,int delete)22871823cb0bSTao Ma int ocfs2_decrease_refcount(struct inode *inode,
22881823cb0bSTao Ma 			    handle_t *handle, u32 cpos, u32 len,
22891823cb0bSTao Ma 			    struct ocfs2_alloc_context *meta_ac,
22906ae23c55STao Ma 			    struct ocfs2_cached_dealloc_ctxt *dealloc,
22916ae23c55STao Ma 			    int delete)
22921823cb0bSTao Ma {
22931823cb0bSTao Ma 	int ret;
22941823cb0bSTao Ma 	u64 ref_blkno;
22951823cb0bSTao Ma 	struct buffer_head *ref_root_bh = NULL;
22961823cb0bSTao Ma 	struct ocfs2_refcount_tree *tree;
22971823cb0bSTao Ma 
229884e40080SDarrick J. Wong 	BUG_ON(!ocfs2_is_refcount_inode(inode));
22991823cb0bSTao Ma 
23001823cb0bSTao Ma 	ret = ocfs2_get_refcount_block(inode, &ref_blkno);
23011823cb0bSTao Ma 	if (ret) {
23021823cb0bSTao Ma 		mlog_errno(ret);
23031823cb0bSTao Ma 		goto out;
23041823cb0bSTao Ma 	}
23051823cb0bSTao Ma 
23061823cb0bSTao Ma 	ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno, &tree);
23071823cb0bSTao Ma 	if (ret) {
23081823cb0bSTao Ma 		mlog_errno(ret);
23091823cb0bSTao Ma 		goto out;
23101823cb0bSTao Ma 	}
23111823cb0bSTao Ma 
23121823cb0bSTao Ma 	ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
23131823cb0bSTao Ma 					&ref_root_bh);
23141823cb0bSTao Ma 	if (ret) {
23151823cb0bSTao Ma 		mlog_errno(ret);
23161823cb0bSTao Ma 		goto out;
23171823cb0bSTao Ma 	}
23181823cb0bSTao Ma 
23191823cb0bSTao Ma 	ret = __ocfs2_decrease_refcount(handle, &tree->rf_ci, ref_root_bh,
23206ae23c55STao Ma 					cpos, len, meta_ac, dealloc, delete);
23211823cb0bSTao Ma 	if (ret)
23221823cb0bSTao Ma 		mlog_errno(ret);
23231823cb0bSTao Ma out:
23241823cb0bSTao Ma 	brelse(ref_root_bh);
23251823cb0bSTao Ma 	return ret;
23261823cb0bSTao Ma }
23271aa75feaSTao Ma 
23281aa75feaSTao Ma /*
23291aa75feaSTao Ma  * Mark the already-existing extent at cpos as refcounted for len clusters.
23301aa75feaSTao Ma  * This adds the refcount extent flag.
23311aa75feaSTao Ma  *
23321aa75feaSTao Ma  * If the existing extent is larger than the request, initiate a
23331aa75feaSTao Ma  * split. An attempt will be made at merging with adjacent extents.
23341aa75feaSTao Ma  *
23351aa75feaSTao Ma  * The caller is responsible for passing down meta_ac if we'll need it.
23361aa75feaSTao Ma  */
ocfs2_mark_extent_refcounted(struct inode * inode,struct ocfs2_extent_tree * et,handle_t * handle,u32 cpos,u32 len,u32 phys,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)23371aa75feaSTao Ma static int ocfs2_mark_extent_refcounted(struct inode *inode,
23381aa75feaSTao Ma 				struct ocfs2_extent_tree *et,
23391aa75feaSTao Ma 				handle_t *handle, u32 cpos,
23401aa75feaSTao Ma 				u32 len, u32 phys,
23411aa75feaSTao Ma 				struct ocfs2_alloc_context *meta_ac,
23421aa75feaSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
23431aa75feaSTao Ma {
23441aa75feaSTao Ma 	int ret;
23451aa75feaSTao Ma 
2346198aac28STao Ma 	trace_ocfs2_mark_extent_refcounted(OCFS2_I(inode)->ip_blkno,
2347198aac28STao Ma 					   cpos, len, phys);
23481aa75feaSTao Ma 
23491aa75feaSTao Ma 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
23507ecef14aSJoe Perches 		ret = ocfs2_error(inode->i_sb, "Inode %lu want to use refcount tree, but the feature bit is not set in the super block\n",
23517ecef14aSJoe Perches 				  inode->i_ino);
23521aa75feaSTao Ma 		goto out;
23531aa75feaSTao Ma 	}
23541aa75feaSTao Ma 
23551aa75feaSTao Ma 	ret = ocfs2_change_extent_flag(handle, et, cpos,
23561aa75feaSTao Ma 				       len, phys, meta_ac, dealloc,
23571aa75feaSTao Ma 				       OCFS2_EXT_REFCOUNTED, 0);
23581aa75feaSTao Ma 	if (ret)
23591aa75feaSTao Ma 		mlog_errno(ret);
23601aa75feaSTao Ma 
23611aa75feaSTao Ma out:
23621aa75feaSTao Ma 	return ret;
23631aa75feaSTao Ma }
2364bcbbb24aSTao Ma 
2365bcbbb24aSTao Ma /*
2366bcbbb24aSTao Ma  * Given some contiguous physical clusters, calculate what we need
2367bcbbb24aSTao Ma  * for modifying their refcount.
2368bcbbb24aSTao Ma  */
ocfs2_calc_refcount_meta_credits(struct super_block * sb,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,u64 start_cpos,u32 clusters,int * meta_add,int * credits)2369bcbbb24aSTao Ma static int ocfs2_calc_refcount_meta_credits(struct super_block *sb,
2370bcbbb24aSTao Ma 					    struct ocfs2_caching_info *ci,
2371bcbbb24aSTao Ma 					    struct buffer_head *ref_root_bh,
2372bcbbb24aSTao Ma 					    u64 start_cpos,
2373bcbbb24aSTao Ma 					    u32 clusters,
2374bcbbb24aSTao Ma 					    int *meta_add,
2375bcbbb24aSTao Ma 					    int *credits)
2376bcbbb24aSTao Ma {
2377bcbbb24aSTao Ma 	int ret = 0, index, ref_blocks = 0, recs_add = 0;
2378bcbbb24aSTao Ma 	u64 cpos = start_cpos;
2379bcbbb24aSTao Ma 	struct ocfs2_refcount_block *rb;
2380bcbbb24aSTao Ma 	struct ocfs2_refcount_rec rec;
2381bcbbb24aSTao Ma 	struct buffer_head *ref_leaf_bh = NULL, *prev_bh = NULL;
2382bcbbb24aSTao Ma 	u32 len;
2383bcbbb24aSTao Ma 
2384bcbbb24aSTao Ma 	while (clusters) {
2385bcbbb24aSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
2386bcbbb24aSTao Ma 					     cpos, clusters, &rec,
2387bcbbb24aSTao Ma 					     &index, &ref_leaf_bh);
2388bcbbb24aSTao Ma 		if (ret) {
2389bcbbb24aSTao Ma 			mlog_errno(ret);
2390bcbbb24aSTao Ma 			goto out;
2391bcbbb24aSTao Ma 		}
2392bcbbb24aSTao Ma 
2393bcbbb24aSTao Ma 		if (ref_leaf_bh != prev_bh) {
2394bcbbb24aSTao Ma 			/*
2395bcbbb24aSTao Ma 			 * Now we encounter a new leaf block, so calculate
2396bcbbb24aSTao Ma 			 * whether we need to extend the old leaf.
2397bcbbb24aSTao Ma 			 */
2398bcbbb24aSTao Ma 			if (prev_bh) {
2399bcbbb24aSTao Ma 				rb = (struct ocfs2_refcount_block *)
2400bcbbb24aSTao Ma 							prev_bh->b_data;
2401bcbbb24aSTao Ma 
2402e1bf4cc6SAl Viro 				if (le16_to_cpu(rb->rf_records.rl_used) +
2403bcbbb24aSTao Ma 				    recs_add >
2404bcbbb24aSTao Ma 				    le16_to_cpu(rb->rf_records.rl_count))
2405bcbbb24aSTao Ma 					ref_blocks++;
2406bcbbb24aSTao Ma 			}
2407bcbbb24aSTao Ma 
2408bcbbb24aSTao Ma 			recs_add = 0;
2409bcbbb24aSTao Ma 			*credits += 1;
2410bcbbb24aSTao Ma 			brelse(prev_bh);
2411bcbbb24aSTao Ma 			prev_bh = ref_leaf_bh;
2412bcbbb24aSTao Ma 			get_bh(prev_bh);
2413bcbbb24aSTao Ma 		}
2414bcbbb24aSTao Ma 
2415198aac28STao Ma 		trace_ocfs2_calc_refcount_meta_credits_iterate(
2416bcbbb24aSTao Ma 				recs_add, (unsigned long long)cpos, clusters,
2417bcbbb24aSTao Ma 				(unsigned long long)le64_to_cpu(rec.r_cpos),
2418bcbbb24aSTao Ma 				le32_to_cpu(rec.r_clusters),
2419bcbbb24aSTao Ma 				le32_to_cpu(rec.r_refcount), index);
2420bcbbb24aSTao Ma 
2421bcbbb24aSTao Ma 		len = min((u64)cpos + clusters, le64_to_cpu(rec.r_cpos) +
2422bcbbb24aSTao Ma 			  le32_to_cpu(rec.r_clusters)) - cpos;
2423bcbbb24aSTao Ma 		/*
2424bcbbb24aSTao Ma 		 * We record all the records which will be inserted to the
2425bcbbb24aSTao Ma 		 * same refcount block, so that we can tell exactly whether
2426bcbbb24aSTao Ma 		 * we need a new refcount block or not.
24278a2e70c4STao Ma 		 *
24288a2e70c4STao Ma 		 * If we will insert a new one, this is easy and only happens
24298a2e70c4STao Ma 		 * during adding refcounted flag to the extent, so we don't
24308a2e70c4STao Ma 		 * have a chance of spliting. We just need one record.
24318a2e70c4STao Ma 		 *
24328a2e70c4STao Ma 		 * If the refcount rec already exists, that would be a little
24338a2e70c4STao Ma 		 * complicated. we may have to:
24348a2e70c4STao Ma 		 * 1) split at the beginning if the start pos isn't aligned.
24358a2e70c4STao Ma 		 *    we need 1 more record in this case.
24368a2e70c4STao Ma 		 * 2) split int the end if the end pos isn't aligned.
24378a2e70c4STao Ma 		 *    we need 1 more record in this case.
24388a2e70c4STao Ma 		 * 3) split in the middle because of file system fragmentation.
24398a2e70c4STao Ma 		 *    we need 2 more records in this case(we can't detect this
24408a2e70c4STao Ma 		 *    beforehand, so always think of the worst case).
2441bcbbb24aSTao Ma 		 */
2442bcbbb24aSTao Ma 		if (rec.r_refcount) {
24438a2e70c4STao Ma 			recs_add += 2;
2444bcbbb24aSTao Ma 			/* Check whether we need a split at the beginning. */
2445bcbbb24aSTao Ma 			if (cpos == start_cpos &&
2446bcbbb24aSTao Ma 			    cpos != le64_to_cpu(rec.r_cpos))
2447bcbbb24aSTao Ma 				recs_add++;
2448bcbbb24aSTao Ma 
2449bcbbb24aSTao Ma 			/* Check whether we need a split in the end. */
2450bcbbb24aSTao Ma 			if (cpos + clusters < le64_to_cpu(rec.r_cpos) +
2451bcbbb24aSTao Ma 			    le32_to_cpu(rec.r_clusters))
2452bcbbb24aSTao Ma 				recs_add++;
2453bcbbb24aSTao Ma 		} else
2454bcbbb24aSTao Ma 			recs_add++;
2455bcbbb24aSTao Ma 
2456bcbbb24aSTao Ma 		brelse(ref_leaf_bh);
2457bcbbb24aSTao Ma 		ref_leaf_bh = NULL;
2458bcbbb24aSTao Ma 		clusters -= len;
2459bcbbb24aSTao Ma 		cpos += len;
2460bcbbb24aSTao Ma 	}
2461bcbbb24aSTao Ma 
2462bcbbb24aSTao Ma 	if (prev_bh) {
2463bcbbb24aSTao Ma 		rb = (struct ocfs2_refcount_block *)prev_bh->b_data;
2464bcbbb24aSTao Ma 
2465e1bf4cc6SAl Viro 		if (le16_to_cpu(rb->rf_records.rl_used) + recs_add >
2466bcbbb24aSTao Ma 		    le16_to_cpu(rb->rf_records.rl_count))
2467bcbbb24aSTao Ma 			ref_blocks++;
2468bcbbb24aSTao Ma 
2469bcbbb24aSTao Ma 		*credits += 1;
2470bcbbb24aSTao Ma 	}
2471bcbbb24aSTao Ma 
2472bcbbb24aSTao Ma 	if (!ref_blocks)
2473bcbbb24aSTao Ma 		goto out;
2474bcbbb24aSTao Ma 
2475bcbbb24aSTao Ma 	*meta_add += ref_blocks;
2476bcbbb24aSTao Ma 	*credits += ref_blocks;
2477bcbbb24aSTao Ma 
2478bcbbb24aSTao Ma 	/*
2479bcbbb24aSTao Ma 	 * So we may need ref_blocks to insert into the tree.
2480bcbbb24aSTao Ma 	 * That also means we need to change the b-tree and add that number
2481bcbbb24aSTao Ma 	 * of records since we never merge them.
2482bcbbb24aSTao Ma 	 * We need one more block for expansion since the new created leaf
2483bcbbb24aSTao Ma 	 * block is also full and needs split.
2484bcbbb24aSTao Ma 	 */
2485bcbbb24aSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
2486bcbbb24aSTao Ma 	if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) {
2487bcbbb24aSTao Ma 		struct ocfs2_extent_tree et;
2488bcbbb24aSTao Ma 
2489bcbbb24aSTao Ma 		ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
2490bcbbb24aSTao Ma 		*meta_add += ocfs2_extend_meta_needed(et.et_root_el);
2491bcbbb24aSTao Ma 		*credits += ocfs2_calc_extend_credits(sb,
249206f9da6eSGoldwyn Rodrigues 						      et.et_root_el);
2493bcbbb24aSTao Ma 	} else {
2494bcbbb24aSTao Ma 		*credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
2495bcbbb24aSTao Ma 		*meta_add += 1;
2496bcbbb24aSTao Ma 	}
2497bcbbb24aSTao Ma 
2498bcbbb24aSTao Ma out:
2499198aac28STao Ma 
2500198aac28STao Ma 	trace_ocfs2_calc_refcount_meta_credits(
2501198aac28STao Ma 		(unsigned long long)start_cpos, clusters,
2502198aac28STao Ma 		*meta_add, *credits);
2503bcbbb24aSTao Ma 	brelse(ref_leaf_bh);
2504bcbbb24aSTao Ma 	brelse(prev_bh);
2505bcbbb24aSTao Ma 	return ret;
2506bcbbb24aSTao Ma }
2507bcbbb24aSTao Ma 
2508bcbbb24aSTao Ma /*
2509bcbbb24aSTao Ma  * For refcount tree, we will decrease some contiguous clusters
2510bcbbb24aSTao Ma  * refcount count, so just go through it to see how many blocks
2511bcbbb24aSTao Ma  * we gonna touch and whether we need to create new blocks.
2512bcbbb24aSTao Ma  *
2513bcbbb24aSTao Ma  * Normally the refcount blocks store these refcount should be
2514af901ca1SAndré Goddard Rosa  * contiguous also, so that we can get the number easily.
251578f94673STristan Ye  * We will at most add split 2 refcount records and 2 more
251678f94673STristan Ye  * refcount blocks, so just check it in a rough way.
2517bcbbb24aSTao Ma  *
2518bcbbb24aSTao Ma  * Caller must hold refcount tree lock.
2519bcbbb24aSTao Ma  */
ocfs2_prepare_refcount_change_for_del(struct inode * inode,u64 refcount_loc,u64 phys_blkno,u32 clusters,int * credits,int * ref_blocks)2520bcbbb24aSTao Ma int ocfs2_prepare_refcount_change_for_del(struct inode *inode,
252178f94673STristan Ye 					  u64 refcount_loc,
2522bcbbb24aSTao Ma 					  u64 phys_blkno,
2523bcbbb24aSTao Ma 					  u32 clusters,
2524bcbbb24aSTao Ma 					  int *credits,
252578f94673STristan Ye 					  int *ref_blocks)
2526bcbbb24aSTao Ma {
252778f94673STristan Ye 	int ret;
2528bcbbb24aSTao Ma 	struct buffer_head *ref_root_bh = NULL;
2529bcbbb24aSTao Ma 	struct ocfs2_refcount_tree *tree;
2530bcbbb24aSTao Ma 	u64 start_cpos = ocfs2_blocks_to_clusters(inode->i_sb, phys_blkno);
2531bcbbb24aSTao Ma 
2532bcbbb24aSTao Ma 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
25337ecef14aSJoe Perches 		ret = ocfs2_error(inode->i_sb, "Inode %lu want to use refcount tree, but the feature bit is not set in the super block\n",
25347ecef14aSJoe Perches 				  inode->i_ino);
2535bcbbb24aSTao Ma 		goto out;
2536bcbbb24aSTao Ma 	}
2537bcbbb24aSTao Ma 
253884e40080SDarrick J. Wong 	BUG_ON(!ocfs2_is_refcount_inode(inode));
2539bcbbb24aSTao Ma 
2540bcbbb24aSTao Ma 	ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb),
254178f94673STristan Ye 				      refcount_loc, &tree);
2542bcbbb24aSTao Ma 	if (ret) {
2543bcbbb24aSTao Ma 		mlog_errno(ret);
2544bcbbb24aSTao Ma 		goto out;
2545bcbbb24aSTao Ma 	}
2546bcbbb24aSTao Ma 
254778f94673STristan Ye 	ret = ocfs2_read_refcount_block(&tree->rf_ci, refcount_loc,
2548bcbbb24aSTao Ma 					&ref_root_bh);
2549bcbbb24aSTao Ma 	if (ret) {
2550bcbbb24aSTao Ma 		mlog_errno(ret);
2551bcbbb24aSTao Ma 		goto out;
2552bcbbb24aSTao Ma 	}
2553bcbbb24aSTao Ma 
2554bcbbb24aSTao Ma 	ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
2555bcbbb24aSTao Ma 					       &tree->rf_ci,
2556bcbbb24aSTao Ma 					       ref_root_bh,
2557bcbbb24aSTao Ma 					       start_cpos, clusters,
255878f94673STristan Ye 					       ref_blocks, credits);
2559bcbbb24aSTao Ma 	if (ret) {
2560bcbbb24aSTao Ma 		mlog_errno(ret);
2561bcbbb24aSTao Ma 		goto out;
2562bcbbb24aSTao Ma 	}
2563bcbbb24aSTao Ma 
2564198aac28STao Ma 	trace_ocfs2_prepare_refcount_change_for_del(*ref_blocks, *credits);
2565bcbbb24aSTao Ma 
2566bcbbb24aSTao Ma out:
2567bcbbb24aSTao Ma 	brelse(ref_root_bh);
2568bcbbb24aSTao Ma 	return ret;
2569bcbbb24aSTao Ma }
25706f70fa51STao Ma 
25716f70fa51STao Ma #define	MAX_CONTIG_BYTES	1048576
25726f70fa51STao Ma 
ocfs2_cow_contig_clusters(struct super_block * sb)25736f70fa51STao Ma static inline unsigned int ocfs2_cow_contig_clusters(struct super_block *sb)
25746f70fa51STao Ma {
25756f70fa51STao Ma 	return ocfs2_clusters_for_bytes(sb, MAX_CONTIG_BYTES);
25766f70fa51STao Ma }
25776f70fa51STao Ma 
ocfs2_cow_contig_mask(struct super_block * sb)25786f70fa51STao Ma static inline unsigned int ocfs2_cow_contig_mask(struct super_block *sb)
25796f70fa51STao Ma {
25806f70fa51STao Ma 	return ~(ocfs2_cow_contig_clusters(sb) - 1);
25816f70fa51STao Ma }
25826f70fa51STao Ma 
25836f70fa51STao Ma /*
25846f70fa51STao Ma  * Given an extent that starts at 'start' and an I/O that starts at 'cpos',
25856f70fa51STao Ma  * find an offset (start + (n * contig_clusters)) that is closest to cpos
25866f70fa51STao Ma  * while still being less than or equal to it.
25876f70fa51STao Ma  *
25886f70fa51STao Ma  * The goal is to break the extent at a multiple of contig_clusters.
25896f70fa51STao Ma  */
ocfs2_cow_align_start(struct super_block * sb,unsigned int start,unsigned int cpos)25906f70fa51STao Ma static inline unsigned int ocfs2_cow_align_start(struct super_block *sb,
25916f70fa51STao Ma 						 unsigned int start,
25926f70fa51STao Ma 						 unsigned int cpos)
25936f70fa51STao Ma {
25946f70fa51STao Ma 	BUG_ON(start > cpos);
25956f70fa51STao Ma 
25966f70fa51STao Ma 	return start + ((cpos - start) & ocfs2_cow_contig_mask(sb));
25976f70fa51STao Ma }
25986f70fa51STao Ma 
25996f70fa51STao Ma /*
26006f70fa51STao Ma  * Given a cluster count of len, pad it out so that it is a multiple
26016f70fa51STao Ma  * of contig_clusters.
26026f70fa51STao Ma  */
ocfs2_cow_align_length(struct super_block * sb,unsigned int len)26036f70fa51STao Ma static inline unsigned int ocfs2_cow_align_length(struct super_block *sb,
26046f70fa51STao Ma 						  unsigned int len)
26056f70fa51STao Ma {
26066f70fa51STao Ma 	unsigned int padded =
26076f70fa51STao Ma 		(len + (ocfs2_cow_contig_clusters(sb) - 1)) &
26086f70fa51STao Ma 		ocfs2_cow_contig_mask(sb);
26096f70fa51STao Ma 
26106f70fa51STao Ma 	/* Did we wrap? */
26116f70fa51STao Ma 	if (padded < len)
26126f70fa51STao Ma 		padded = UINT_MAX;
26136f70fa51STao Ma 
26146f70fa51STao Ma 	return padded;
26156f70fa51STao Ma }
26166f70fa51STao Ma 
26176f70fa51STao Ma /*
2618e77999c1Swangjianli  * Calculate out the start and number of virtual clusters we need to CoW.
26196f70fa51STao Ma  *
26206f70fa51STao Ma  * cpos is vitual start cluster position we want to do CoW in a
26216f70fa51STao Ma  * file and write_len is the cluster length.
262237f8a2bfSTao Ma  * max_cpos is the place where we want to stop CoW intentionally.
26236f70fa51STao Ma  *
26246f70fa51STao Ma  * Normal we will start CoW from the beginning of extent record cotaining cpos.
26256f70fa51STao Ma  * We try to break up extents on boundaries of MAX_CONTIG_BYTES so that we
26266f70fa51STao Ma  * get good I/O from the resulting extent tree.
26276f70fa51STao Ma  */
ocfs2_refcount_cal_cow_clusters(struct inode * inode,struct ocfs2_extent_list * el,u32 cpos,u32 write_len,u32 max_cpos,u32 * cow_start,u32 * cow_len)26286f70fa51STao Ma static int ocfs2_refcount_cal_cow_clusters(struct inode *inode,
2629913580b4STao Ma 					   struct ocfs2_extent_list *el,
26306f70fa51STao Ma 					   u32 cpos,
26316f70fa51STao Ma 					   u32 write_len,
263237f8a2bfSTao Ma 					   u32 max_cpos,
26336f70fa51STao Ma 					   u32 *cow_start,
26346f70fa51STao Ma 					   u32 *cow_len)
26356f70fa51STao Ma {
26366f70fa51STao Ma 	int ret = 0;
26376f70fa51STao Ma 	int tree_height = le16_to_cpu(el->l_tree_depth), i;
26386f70fa51STao Ma 	struct buffer_head *eb_bh = NULL;
26396f70fa51STao Ma 	struct ocfs2_extent_block *eb = NULL;
26406f70fa51STao Ma 	struct ocfs2_extent_rec *rec;
26416f70fa51STao Ma 	unsigned int want_clusters, rec_end = 0;
26426f70fa51STao Ma 	int contig_clusters = ocfs2_cow_contig_clusters(inode->i_sb);
26436f70fa51STao Ma 	int leaf_clusters;
26446f70fa51STao Ma 
264537f8a2bfSTao Ma 	BUG_ON(cpos + write_len > max_cpos);
264637f8a2bfSTao Ma 
26476f70fa51STao Ma 	if (tree_height > 0) {
26486f70fa51STao Ma 		ret = ocfs2_find_leaf(INODE_CACHE(inode), el, cpos, &eb_bh);
26496f70fa51STao Ma 		if (ret) {
26506f70fa51STao Ma 			mlog_errno(ret);
26516f70fa51STao Ma 			goto out;
26526f70fa51STao Ma 		}
26536f70fa51STao Ma 
26546f70fa51STao Ma 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
26556f70fa51STao Ma 		el = &eb->h_list;
26566f70fa51STao Ma 
26576f70fa51STao Ma 		if (el->l_tree_depth) {
265817a5b9abSGoldwyn Rodrigues 			ret = ocfs2_error(inode->i_sb,
26597ecef14aSJoe Perches 					  "Inode %lu has non zero tree depth in leaf block %llu\n",
26607ecef14aSJoe Perches 					  inode->i_ino,
26616f70fa51STao Ma 					  (unsigned long long)eb_bh->b_blocknr);
26626f70fa51STao Ma 			goto out;
26636f70fa51STao Ma 		}
26646f70fa51STao Ma 	}
26656f70fa51STao Ma 
26666f70fa51STao Ma 	*cow_len = 0;
26676f70fa51STao Ma 	for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
26686f70fa51STao Ma 		rec = &el->l_recs[i];
26696f70fa51STao Ma 
26706f70fa51STao Ma 		if (ocfs2_is_empty_extent(rec)) {
26716f70fa51STao Ma 			mlog_bug_on_msg(i != 0, "Inode %lu has empty record in "
26726f70fa51STao Ma 					"index %d\n", inode->i_ino, i);
26736f70fa51STao Ma 			continue;
26746f70fa51STao Ma 		}
26756f70fa51STao Ma 
26766f70fa51STao Ma 		if (le32_to_cpu(rec->e_cpos) +
26776f70fa51STao Ma 		    le16_to_cpu(rec->e_leaf_clusters) <= cpos)
26786f70fa51STao Ma 			continue;
26796f70fa51STao Ma 
26806f70fa51STao Ma 		if (*cow_len == 0) {
26816f70fa51STao Ma 			/*
26826f70fa51STao Ma 			 * We should find a refcounted record in the
26836f70fa51STao Ma 			 * first pass.
26846f70fa51STao Ma 			 */
26856f70fa51STao Ma 			BUG_ON(!(rec->e_flags & OCFS2_EXT_REFCOUNTED));
26866f70fa51STao Ma 			*cow_start = le32_to_cpu(rec->e_cpos);
26876f70fa51STao Ma 		}
26886f70fa51STao Ma 
26896f70fa51STao Ma 		/*
269037f8a2bfSTao Ma 		 * If we encounter a hole, a non-refcounted record or
269137f8a2bfSTao Ma 		 * pass the max_cpos, stop the search.
26926f70fa51STao Ma 		 */
26936f70fa51STao Ma 		if ((!(rec->e_flags & OCFS2_EXT_REFCOUNTED)) ||
269437f8a2bfSTao Ma 		    (*cow_len && rec_end != le32_to_cpu(rec->e_cpos)) ||
269537f8a2bfSTao Ma 		    (max_cpos <= le32_to_cpu(rec->e_cpos)))
26966f70fa51STao Ma 			break;
26976f70fa51STao Ma 
26986f70fa51STao Ma 		leaf_clusters = le16_to_cpu(rec->e_leaf_clusters);
26996f70fa51STao Ma 		rec_end = le32_to_cpu(rec->e_cpos) + leaf_clusters;
270037f8a2bfSTao Ma 		if (rec_end > max_cpos) {
270137f8a2bfSTao Ma 			rec_end = max_cpos;
270237f8a2bfSTao Ma 			leaf_clusters = rec_end - le32_to_cpu(rec->e_cpos);
270337f8a2bfSTao Ma 		}
27046f70fa51STao Ma 
27056f70fa51STao Ma 		/*
27066f70fa51STao Ma 		 * How many clusters do we actually need from
27076f70fa51STao Ma 		 * this extent?  First we see how many we actually
27086f70fa51STao Ma 		 * need to complete the write.  If that's smaller
27096f70fa51STao Ma 		 * than contig_clusters, we try for contig_clusters.
27106f70fa51STao Ma 		 */
27116f70fa51STao Ma 		if (!*cow_len)
27126f70fa51STao Ma 			want_clusters = write_len;
27136f70fa51STao Ma 		else
27146f70fa51STao Ma 			want_clusters = (cpos + write_len) -
27156f70fa51STao Ma 				(*cow_start + *cow_len);
27166f70fa51STao Ma 		if (want_clusters < contig_clusters)
27176f70fa51STao Ma 			want_clusters = contig_clusters;
27186f70fa51STao Ma 
27196f70fa51STao Ma 		/*
27206f70fa51STao Ma 		 * If the write does not cover the whole extent, we
27216f70fa51STao Ma 		 * need to calculate how we're going to split the extent.
27226f70fa51STao Ma 		 * We try to do it on contig_clusters boundaries.
27236f70fa51STao Ma 		 *
27246f70fa51STao Ma 		 * Any extent smaller than contig_clusters will be
27256f70fa51STao Ma 		 * CoWed in its entirety.
27266f70fa51STao Ma 		 */
27276f70fa51STao Ma 		if (leaf_clusters <= contig_clusters)
27286f70fa51STao Ma 			*cow_len += leaf_clusters;
27296f70fa51STao Ma 		else if (*cow_len || (*cow_start == cpos)) {
27306f70fa51STao Ma 			/*
27316f70fa51STao Ma 			 * This extent needs to be CoW'd from its
27326f70fa51STao Ma 			 * beginning, so all we have to do is compute
27336f70fa51STao Ma 			 * how many clusters to grab.  We align
27346f70fa51STao Ma 			 * want_clusters to the edge of contig_clusters
27356f70fa51STao Ma 			 * to get better I/O.
27366f70fa51STao Ma 			 */
27376f70fa51STao Ma 			want_clusters = ocfs2_cow_align_length(inode->i_sb,
27386f70fa51STao Ma 							       want_clusters);
27396f70fa51STao Ma 
27406f70fa51STao Ma 			if (leaf_clusters < want_clusters)
27416f70fa51STao Ma 				*cow_len += leaf_clusters;
27426f70fa51STao Ma 			else
27436f70fa51STao Ma 				*cow_len += want_clusters;
27446f70fa51STao Ma 		} else if ((*cow_start + contig_clusters) >=
27456f70fa51STao Ma 			   (cpos + write_len)) {
27466f70fa51STao Ma 			/*
27476f70fa51STao Ma 			 * Breaking off contig_clusters at the front
27486f70fa51STao Ma 			 * of the extent will cover our write.  That's
27496f70fa51STao Ma 			 * easy.
27506f70fa51STao Ma 			 */
27516f70fa51STao Ma 			*cow_len = contig_clusters;
27526f70fa51STao Ma 		} else if ((rec_end - cpos) <= contig_clusters) {
27536f70fa51STao Ma 			/*
27546f70fa51STao Ma 			 * Breaking off contig_clusters at the tail of
27556f70fa51STao Ma 			 * this extent will cover cpos.
27566f70fa51STao Ma 			 */
27576f70fa51STao Ma 			*cow_start = rec_end - contig_clusters;
27586f70fa51STao Ma 			*cow_len = contig_clusters;
27596f70fa51STao Ma 		} else if ((rec_end - cpos) <= want_clusters) {
27606f70fa51STao Ma 			/*
27616f70fa51STao Ma 			 * While we can't fit the entire write in this
27626f70fa51STao Ma 			 * extent, we know that the write goes from cpos
27636f70fa51STao Ma 			 * to the end of the extent.  Break that off.
27646f70fa51STao Ma 			 * We try to break it at some multiple of
27656f70fa51STao Ma 			 * contig_clusters from the front of the extent.
27666f70fa51STao Ma 			 * Failing that (ie, cpos is within
27676f70fa51STao Ma 			 * contig_clusters of the front), we'll CoW the
27686f70fa51STao Ma 			 * entire extent.
27696f70fa51STao Ma 			 */
27706f70fa51STao Ma 			*cow_start = ocfs2_cow_align_start(inode->i_sb,
27716f70fa51STao Ma 							   *cow_start, cpos);
27726f70fa51STao Ma 			*cow_len = rec_end - *cow_start;
27736f70fa51STao Ma 		} else {
27746f70fa51STao Ma 			/*
27756f70fa51STao Ma 			 * Ok, the entire write lives in the middle of
27766f70fa51STao Ma 			 * this extent.  Let's try to slice the extent up
27776f70fa51STao Ma 			 * nicely.  Optimally, our CoW region starts at
27786f70fa51STao Ma 			 * m*contig_clusters from the beginning of the
27796f70fa51STao Ma 			 * extent and goes for n*contig_clusters,
27806f70fa51STao Ma 			 * covering the entire write.
27816f70fa51STao Ma 			 */
27826f70fa51STao Ma 			*cow_start = ocfs2_cow_align_start(inode->i_sb,
27836f70fa51STao Ma 							   *cow_start, cpos);
27846f70fa51STao Ma 
27856f70fa51STao Ma 			want_clusters = (cpos + write_len) - *cow_start;
27866f70fa51STao Ma 			want_clusters = ocfs2_cow_align_length(inode->i_sb,
27876f70fa51STao Ma 							       want_clusters);
27886f70fa51STao Ma 			if (*cow_start + want_clusters <= rec_end)
27896f70fa51STao Ma 				*cow_len = want_clusters;
27906f70fa51STao Ma 			else
27916f70fa51STao Ma 				*cow_len = rec_end - *cow_start;
27926f70fa51STao Ma 		}
27936f70fa51STao Ma 
27946f70fa51STao Ma 		/* Have we covered our entire write yet? */
27956f70fa51STao Ma 		if ((*cow_start + *cow_len) >= (cpos + write_len))
27966f70fa51STao Ma 			break;
27976f70fa51STao Ma 
27986f70fa51STao Ma 		/*
27996f70fa51STao Ma 		 * If we reach the end of the extent block and don't get enough
28006f70fa51STao Ma 		 * clusters, continue with the next extent block if possible.
28016f70fa51STao Ma 		 */
28026f70fa51STao Ma 		if (i + 1 == le16_to_cpu(el->l_next_free_rec) &&
28036f70fa51STao Ma 		    eb && eb->h_next_leaf_blk) {
28046f70fa51STao Ma 			brelse(eb_bh);
28056f70fa51STao Ma 			eb_bh = NULL;
28066f70fa51STao Ma 
28076f70fa51STao Ma 			ret = ocfs2_read_extent_block(INODE_CACHE(inode),
28086f70fa51STao Ma 					       le64_to_cpu(eb->h_next_leaf_blk),
28096f70fa51STao Ma 					       &eb_bh);
28106f70fa51STao Ma 			if (ret) {
28116f70fa51STao Ma 				mlog_errno(ret);
28126f70fa51STao Ma 				goto out;
28136f70fa51STao Ma 			}
28146f70fa51STao Ma 
28156f70fa51STao Ma 			eb = (struct ocfs2_extent_block *) eb_bh->b_data;
28166f70fa51STao Ma 			el = &eb->h_list;
28176f70fa51STao Ma 			i = -1;
28186f70fa51STao Ma 		}
28196f70fa51STao Ma 	}
28206f70fa51STao Ma 
28216f70fa51STao Ma out:
28226f70fa51STao Ma 	brelse(eb_bh);
28236f70fa51STao Ma 	return ret;
28246f70fa51STao Ma }
28256f70fa51STao Ma 
28266f70fa51STao Ma /*
28276f70fa51STao Ma  * Prepare meta_ac, data_ac and calculate credits when we want to add some
28286f70fa51STao Ma  * num_clusters in data_tree "et" and change the refcount for the old
28296f70fa51STao Ma  * clusters(starting form p_cluster) in the refcount tree.
28306f70fa51STao Ma  *
28316f70fa51STao Ma  * Note:
28326f70fa51STao Ma  * 1. since we may split the old tree, so we at most will need num_clusters + 2
28336f70fa51STao Ma  *    more new leaf records.
28346f70fa51STao Ma  * 2. In some case, we may not need to reserve new clusters(e.g, reflink), so
28356f70fa51STao Ma  *    just give data_ac = NULL.
28366f70fa51STao Ma  */
ocfs2_lock_refcount_allocators(struct super_block * sb,u32 p_cluster,u32 num_clusters,struct ocfs2_extent_tree * et,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_alloc_context ** meta_ac,struct ocfs2_alloc_context ** data_ac,int * credits)28376f70fa51STao Ma static int ocfs2_lock_refcount_allocators(struct super_block *sb,
28386f70fa51STao Ma 					u32 p_cluster, u32 num_clusters,
28396f70fa51STao Ma 					struct ocfs2_extent_tree *et,
28406f70fa51STao Ma 					struct ocfs2_caching_info *ref_ci,
28416f70fa51STao Ma 					struct buffer_head *ref_root_bh,
28426f70fa51STao Ma 					struct ocfs2_alloc_context **meta_ac,
28436f70fa51STao Ma 					struct ocfs2_alloc_context **data_ac,
28446f70fa51STao Ma 					int *credits)
28456f70fa51STao Ma {
28466f70fa51STao Ma 	int ret = 0, meta_add = 0;
2847964f14a0SJun Piao 	int num_free_extents = ocfs2_num_free_extents(et);
28486f70fa51STao Ma 
28496f70fa51STao Ma 	if (num_free_extents < 0) {
28506f70fa51STao Ma 		ret = num_free_extents;
28516f70fa51STao Ma 		mlog_errno(ret);
28526f70fa51STao Ma 		goto out;
28536f70fa51STao Ma 	}
28546f70fa51STao Ma 
28556f70fa51STao Ma 	if (num_free_extents < num_clusters + 2)
28566f70fa51STao Ma 		meta_add =
28576f70fa51STao Ma 			ocfs2_extend_meta_needed(et->et_root_el);
28586f70fa51STao Ma 
285906f9da6eSGoldwyn Rodrigues 	*credits += ocfs2_calc_extend_credits(sb, et->et_root_el);
28606f70fa51STao Ma 
28616f70fa51STao Ma 	ret = ocfs2_calc_refcount_meta_credits(sb, ref_ci, ref_root_bh,
28626f70fa51STao Ma 					       p_cluster, num_clusters,
28636f70fa51STao Ma 					       &meta_add, credits);
28646f70fa51STao Ma 	if (ret) {
28656f70fa51STao Ma 		mlog_errno(ret);
28666f70fa51STao Ma 		goto out;
28676f70fa51STao Ma 	}
28686f70fa51STao Ma 
2869198aac28STao Ma 	trace_ocfs2_lock_refcount_allocators(meta_add, *credits);
28706f70fa51STao Ma 	ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(sb), meta_add,
28716f70fa51STao Ma 						meta_ac);
28726f70fa51STao Ma 	if (ret) {
28736f70fa51STao Ma 		mlog_errno(ret);
28746f70fa51STao Ma 		goto out;
28756f70fa51STao Ma 	}
28766f70fa51STao Ma 
28776f70fa51STao Ma 	if (data_ac) {
28786f70fa51STao Ma 		ret = ocfs2_reserve_clusters(OCFS2_SB(sb), num_clusters,
28796f70fa51STao Ma 					     data_ac);
28806f70fa51STao Ma 		if (ret)
28816f70fa51STao Ma 			mlog_errno(ret);
28826f70fa51STao Ma 	}
28836f70fa51STao Ma 
28846f70fa51STao Ma out:
28856f70fa51STao Ma 	if (ret) {
28866f70fa51STao Ma 		if (*meta_ac) {
28876f70fa51STao Ma 			ocfs2_free_alloc_context(*meta_ac);
28886f70fa51STao Ma 			*meta_ac = NULL;
28896f70fa51STao Ma 		}
28906f70fa51STao Ma 	}
28916f70fa51STao Ma 
28926f70fa51STao Ma 	return ret;
28936f70fa51STao Ma }
28946f70fa51STao Ma 
ocfs2_clear_cow_buffer(handle_t * handle,struct buffer_head * bh)28956f70fa51STao Ma static int ocfs2_clear_cow_buffer(handle_t *handle, struct buffer_head *bh)
28966f70fa51STao Ma {
28976f70fa51STao Ma 	BUG_ON(buffer_dirty(bh));
28986f70fa51STao Ma 
28996f70fa51STao Ma 	clear_buffer_mapped(bh);
29006f70fa51STao Ma 
29016f70fa51STao Ma 	return 0;
29026f70fa51STao Ma }
29036f70fa51STao Ma 
ocfs2_duplicate_clusters_by_page(handle_t * handle,struct inode * inode,u32 cpos,u32 old_cluster,u32 new_cluster,u32 new_len)29043e19a25eSTristan Ye int ocfs2_duplicate_clusters_by_page(handle_t *handle,
2905c7dd3392STiger Yang 				     struct inode *inode,
29066f70fa51STao Ma 				     u32 cpos, u32 old_cluster,
29076f70fa51STao Ma 				     u32 new_cluster, u32 new_len)
29086f70fa51STao Ma {
29096f70fa51STao Ma 	int ret = 0, partial;
2910c7dd3392STiger Yang 	struct super_block *sb = inode->i_sb;
29116f70fa51STao Ma 	u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
29126f70fa51STao Ma 	struct page *page;
29136f70fa51STao Ma 	pgoff_t page_index;
2914262d8a87SJoseph Qi 	unsigned int from, to;
29156f70fa51STao Ma 	loff_t offset, end, map_end;
29163e19a25eSTristan Ye 	struct address_space *mapping = inode->i_mapping;
29176f70fa51STao Ma 
2918198aac28STao Ma 	trace_ocfs2_duplicate_clusters_by_page(cpos, old_cluster,
2919198aac28STao Ma 					       new_cluster, new_len);
29206f70fa51STao Ma 
29216f70fa51STao Ma 	offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
29226f70fa51STao Ma 	end = offset + (new_len << OCFS2_SB(sb)->s_clustersize_bits);
2923f5e27b6dSTao Ma 	/*
2924f5e27b6dSTao Ma 	 * We only duplicate pages until we reach the page contains i_size - 1.
2925f5e27b6dSTao Ma 	 * So trim 'end' to i_size.
2926f5e27b6dSTao Ma 	 */
29273e19a25eSTristan Ye 	if (end > i_size_read(inode))
29283e19a25eSTristan Ye 		end = i_size_read(inode);
29296f70fa51STao Ma 
29306f70fa51STao Ma 	while (offset < end) {
293109cbfeafSKirill A. Shutemov 		page_index = offset >> PAGE_SHIFT;
293209cbfeafSKirill A. Shutemov 		map_end = ((loff_t)page_index + 1) << PAGE_SHIFT;
29336f70fa51STao Ma 		if (map_end > end)
29346f70fa51STao Ma 			map_end = end;
29356f70fa51STao Ma 
29366f70fa51STao Ma 		/* from, to is the offset within the page. */
293709cbfeafSKirill A. Shutemov 		from = offset & (PAGE_SIZE - 1);
293809cbfeafSKirill A. Shutemov 		to = PAGE_SIZE;
293909cbfeafSKirill A. Shutemov 		if (map_end & (PAGE_SIZE - 1))
294009cbfeafSKirill A. Shutemov 			to = map_end & (PAGE_SIZE - 1);
29416f70fa51STao Ma 
294269eb7765SLarry Chen retry:
29439b4c0ff3SJan Kara 		page = find_or_create_page(mapping, page_index, GFP_NOFS);
294462c61046SGu Zheng 		if (!page) {
294562c61046SGu Zheng 			ret = -ENOMEM;
294662c61046SGu Zheng 			mlog_errno(ret);
294762c61046SGu Zheng 			break;
294862c61046SGu Zheng 		}
29496f70fa51STao Ma 
29500a1ea437STao Ma 		/*
295169eb7765SLarry Chen 		 * In case PAGE_SIZE <= CLUSTER_SIZE, we do not expect a dirty
295269eb7765SLarry Chen 		 * page, so write it back.
29530a1ea437STao Ma 		 */
295469eb7765SLarry Chen 		if (PAGE_SIZE <= OCFS2_SB(sb)->s_clustersize) {
295569eb7765SLarry Chen 			if (PageDirty(page)) {
29568e4bfd13SChristoph Hellwig 				unlock_page(page);
29578e4bfd13SChristoph Hellwig 				put_page(page);
29588e4bfd13SChristoph Hellwig 
29598e4bfd13SChristoph Hellwig 				ret = filemap_write_and_wait_range(mapping,
29608e4bfd13SChristoph Hellwig 						offset, map_end - 1);
296169eb7765SLarry Chen 				goto retry;
296269eb7765SLarry Chen 			}
296369eb7765SLarry Chen 		}
29646f70fa51STao Ma 
29656f70fa51STao Ma 		if (!PageUptodate(page)) {
29662c69e205SMatthew Wilcox (Oracle) 			struct folio *folio = page_folio(page);
29672c69e205SMatthew Wilcox (Oracle) 
29682c69e205SMatthew Wilcox (Oracle) 			ret = block_read_full_folio(folio, ocfs2_get_block);
29696f70fa51STao Ma 			if (ret) {
29706f70fa51STao Ma 				mlog_errno(ret);
29716f70fa51STao Ma 				goto unlock;
29726f70fa51STao Ma 			}
29732c69e205SMatthew Wilcox (Oracle) 			folio_lock(folio);
29746f70fa51STao Ma 		}
29756f70fa51STao Ma 
29766f70fa51STao Ma 		if (page_has_buffers(page)) {
29776f70fa51STao Ma 			ret = walk_page_buffers(handle, page_buffers(page),
29786f70fa51STao Ma 						from, to, &partial,
29796f70fa51STao Ma 						ocfs2_clear_cow_buffer);
29806f70fa51STao Ma 			if (ret) {
29816f70fa51STao Ma 				mlog_errno(ret);
29826f70fa51STao Ma 				goto unlock;
29836f70fa51STao Ma 			}
29846f70fa51STao Ma 		}
29856f70fa51STao Ma 
2986c7dd3392STiger Yang 		ocfs2_map_and_dirty_page(inode,
2987c7dd3392STiger Yang 					 handle, from, to,
29886f70fa51STao Ma 					 page, 0, &new_block);
29896f70fa51STao Ma 		mark_page_accessed(page);
29906f70fa51STao Ma unlock:
29916f70fa51STao Ma 		unlock_page(page);
299209cbfeafSKirill A. Shutemov 		put_page(page);
29936f70fa51STao Ma 		page = NULL;
29946f70fa51STao Ma 		offset = map_end;
29956f70fa51STao Ma 		if (ret)
29966f70fa51STao Ma 			break;
29976f70fa51STao Ma 	}
29986f70fa51STao Ma 
29996f70fa51STao Ma 	return ret;
30006f70fa51STao Ma }
30016f70fa51STao Ma 
ocfs2_duplicate_clusters_by_jbd(handle_t * handle,struct inode * inode,u32 cpos,u32 old_cluster,u32 new_cluster,u32 new_len)30023e19a25eSTristan Ye int ocfs2_duplicate_clusters_by_jbd(handle_t *handle,
3003c7dd3392STiger Yang 				    struct inode *inode,
3004492a8a33STao Ma 				    u32 cpos, u32 old_cluster,
3005492a8a33STao Ma 				    u32 new_cluster, u32 new_len)
3006492a8a33STao Ma {
3007492a8a33STao Ma 	int ret = 0;
30083e19a25eSTristan Ye 	struct super_block *sb = inode->i_sb;
30093e19a25eSTristan Ye 	struct ocfs2_caching_info *ci = INODE_CACHE(inode);
3010492a8a33STao Ma 	int i, blocks = ocfs2_clusters_to_blocks(sb, new_len);
3011492a8a33STao Ma 	u64 old_block = ocfs2_clusters_to_blocks(sb, old_cluster);
3012492a8a33STao Ma 	u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
3013492a8a33STao Ma 	struct ocfs2_super *osb = OCFS2_SB(sb);
3014492a8a33STao Ma 	struct buffer_head *old_bh = NULL;
3015492a8a33STao Ma 	struct buffer_head *new_bh = NULL;
3016492a8a33STao Ma 
3017198aac28STao Ma 	trace_ocfs2_duplicate_clusters_by_page(cpos, old_cluster,
3018492a8a33STao Ma 					       new_cluster, new_len);
3019492a8a33STao Ma 
3020492a8a33STao Ma 	for (i = 0; i < blocks; i++, old_block++, new_block++) {
3021492a8a33STao Ma 		new_bh = sb_getblk(osb->sb, new_block);
3022492a8a33STao Ma 		if (new_bh == NULL) {
30237391a294SRui Xiang 			ret = -ENOMEM;
3024492a8a33STao Ma 			mlog_errno(ret);
3025492a8a33STao Ma 			break;
3026492a8a33STao Ma 		}
3027492a8a33STao Ma 
3028492a8a33STao Ma 		ocfs2_set_new_buffer_uptodate(ci, new_bh);
3029492a8a33STao Ma 
3030492a8a33STao Ma 		ret = ocfs2_read_block(ci, old_block, &old_bh, NULL);
3031492a8a33STao Ma 		if (ret) {
3032492a8a33STao Ma 			mlog_errno(ret);
3033492a8a33STao Ma 			break;
3034492a8a33STao Ma 		}
3035492a8a33STao Ma 
3036492a8a33STao Ma 		ret = ocfs2_journal_access(handle, ci, new_bh,
3037492a8a33STao Ma 					   OCFS2_JOURNAL_ACCESS_CREATE);
3038492a8a33STao Ma 		if (ret) {
3039492a8a33STao Ma 			mlog_errno(ret);
3040492a8a33STao Ma 			break;
3041492a8a33STao Ma 		}
3042492a8a33STao Ma 
3043492a8a33STao Ma 		memcpy(new_bh->b_data, old_bh->b_data, sb->s_blocksize);
3044ec20cec7SJoel Becker 		ocfs2_journal_dirty(handle, new_bh);
3045492a8a33STao Ma 
3046492a8a33STao Ma 		brelse(new_bh);
3047492a8a33STao Ma 		brelse(old_bh);
3048492a8a33STao Ma 		new_bh = NULL;
3049492a8a33STao Ma 		old_bh = NULL;
3050492a8a33STao Ma 	}
3051492a8a33STao Ma 
3052492a8a33STao Ma 	brelse(new_bh);
3053492a8a33STao Ma 	brelse(old_bh);
3054492a8a33STao Ma 	return ret;
3055492a8a33STao Ma }
3056492a8a33STao Ma 
ocfs2_clear_ext_refcount(handle_t * handle,struct ocfs2_extent_tree * et,u32 cpos,u32 p_cluster,u32 len,unsigned int ext_flags,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)30576f70fa51STao Ma static int ocfs2_clear_ext_refcount(handle_t *handle,
30586f70fa51STao Ma 				    struct ocfs2_extent_tree *et,
30596f70fa51STao Ma 				    u32 cpos, u32 p_cluster, u32 len,
30606f70fa51STao Ma 				    unsigned int ext_flags,
30616f70fa51STao Ma 				    struct ocfs2_alloc_context *meta_ac,
30626f70fa51STao Ma 				    struct ocfs2_cached_dealloc_ctxt *dealloc)
30636f70fa51STao Ma {
30646f70fa51STao Ma 	int ret, index;
30656f70fa51STao Ma 	struct ocfs2_extent_rec replace_rec;
30666f70fa51STao Ma 	struct ocfs2_path *path = NULL;
30676f70fa51STao Ma 	struct ocfs2_extent_list *el;
30686f70fa51STao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
30696f70fa51STao Ma 	u64 ino = ocfs2_metadata_cache_owner(et->et_ci);
30706f70fa51STao Ma 
3071198aac28STao Ma 	trace_ocfs2_clear_ext_refcount((unsigned long long)ino,
3072198aac28STao Ma 				       cpos, len, p_cluster, ext_flags);
30736f70fa51STao Ma 
30746f70fa51STao Ma 	memset(&replace_rec, 0, sizeof(replace_rec));
30756f70fa51STao Ma 	replace_rec.e_cpos = cpu_to_le32(cpos);
30766f70fa51STao Ma 	replace_rec.e_leaf_clusters = cpu_to_le16(len);
30776f70fa51STao Ma 	replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(sb,
30786f70fa51STao Ma 								   p_cluster));
30796f70fa51STao Ma 	replace_rec.e_flags = ext_flags;
30806f70fa51STao Ma 	replace_rec.e_flags &= ~OCFS2_EXT_REFCOUNTED;
30816f70fa51STao Ma 
30826f70fa51STao Ma 	path = ocfs2_new_path_from_et(et);
30836f70fa51STao Ma 	if (!path) {
30846f70fa51STao Ma 		ret = -ENOMEM;
30856f70fa51STao Ma 		mlog_errno(ret);
30866f70fa51STao Ma 		goto out;
30876f70fa51STao Ma 	}
30886f70fa51STao Ma 
30896f70fa51STao Ma 	ret = ocfs2_find_path(et->et_ci, path, cpos);
30906f70fa51STao Ma 	if (ret) {
30916f70fa51STao Ma 		mlog_errno(ret);
30926f70fa51STao Ma 		goto out;
30936f70fa51STao Ma 	}
30946f70fa51STao Ma 
30956f70fa51STao Ma 	el = path_leaf_el(path);
30966f70fa51STao Ma 
30976f70fa51STao Ma 	index = ocfs2_search_extent_list(el, cpos);
3098981035b4SYingtai Xie 	if (index == -1) {
309917a5b9abSGoldwyn Rodrigues 		ret = ocfs2_error(sb,
31007ecef14aSJoe Perches 				  "Inode %llu has an extent at cpos %u which can no longer be found\n",
31016f70fa51STao Ma 				  (unsigned long long)ino, cpos);
31026f70fa51STao Ma 		goto out;
31036f70fa51STao Ma 	}
31046f70fa51STao Ma 
31056f70fa51STao Ma 	ret = ocfs2_split_extent(handle, et, path, index,
31066f70fa51STao Ma 				 &replace_rec, meta_ac, dealloc);
31076f70fa51STao Ma 	if (ret)
31086f70fa51STao Ma 		mlog_errno(ret);
31096f70fa51STao Ma 
31106f70fa51STao Ma out:
31116f70fa51STao Ma 	ocfs2_free_path(path);
31126f70fa51STao Ma 	return ret;
31136f70fa51STao Ma }
31146f70fa51STao Ma 
ocfs2_replace_clusters(handle_t * handle,struct ocfs2_cow_context * context,u32 cpos,u32 old,u32 new,u32 len,unsigned int ext_flags)31156f70fa51STao Ma static int ocfs2_replace_clusters(handle_t *handle,
31166f70fa51STao Ma 				  struct ocfs2_cow_context *context,
31176f70fa51STao Ma 				  u32 cpos, u32 old,
31186f70fa51STao Ma 				  u32 new, u32 len,
31196f70fa51STao Ma 				  unsigned int ext_flags)
31206f70fa51STao Ma {
31216f70fa51STao Ma 	int ret;
3122913580b4STao Ma 	struct ocfs2_caching_info *ci = context->data_et.et_ci;
31236f70fa51STao Ma 	u64 ino = ocfs2_metadata_cache_owner(ci);
31246f70fa51STao Ma 
3125198aac28STao Ma 	trace_ocfs2_replace_clusters((unsigned long long)ino,
3126198aac28STao Ma 				     cpos, old, new, len, ext_flags);
31276f70fa51STao Ma 
31286f70fa51STao Ma 	/*If the old clusters is unwritten, no need to duplicate. */
31296f70fa51STao Ma 	if (!(ext_flags & OCFS2_EXT_UNWRITTEN)) {
3130c7dd3392STiger Yang 		ret = context->cow_duplicate_clusters(handle, context->inode,
31313e19a25eSTristan Ye 						      cpos, old, new, len);
31326f70fa51STao Ma 		if (ret) {
31336f70fa51STao Ma 			mlog_errno(ret);
31346f70fa51STao Ma 			goto out;
31356f70fa51STao Ma 		}
31366f70fa51STao Ma 	}
31376f70fa51STao Ma 
3138913580b4STao Ma 	ret = ocfs2_clear_ext_refcount(handle, &context->data_et,
31396f70fa51STao Ma 				       cpos, new, len, ext_flags,
31406f70fa51STao Ma 				       context->meta_ac, &context->dealloc);
31416f70fa51STao Ma 	if (ret)
31426f70fa51STao Ma 		mlog_errno(ret);
31436f70fa51STao Ma out:
31446f70fa51STao Ma 	return ret;
31456f70fa51STao Ma }
31466f70fa51STao Ma 
ocfs2_cow_sync_writeback(struct super_block * sb,struct inode * inode,u32 cpos,u32 num_clusters)31473e19a25eSTristan Ye int ocfs2_cow_sync_writeback(struct super_block *sb,
31483e19a25eSTristan Ye 			     struct inode *inode,
31496f70fa51STao Ma 			     u32 cpos, u32 num_clusters)
31506f70fa51STao Ma {
315197a3a383SMatthew Wilcox (Oracle) 	int ret;
315297a3a383SMatthew Wilcox (Oracle) 	loff_t start, end;
31536f70fa51STao Ma 
31543e19a25eSTristan Ye 	if (ocfs2_should_order_data(inode))
31556f70fa51STao Ma 		return 0;
31566f70fa51STao Ma 
315797a3a383SMatthew Wilcox (Oracle) 	start = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
315897a3a383SMatthew Wilcox (Oracle) 	end = start + (num_clusters << OCFS2_SB(sb)->s_clustersize_bits) - 1;
31596f70fa51STao Ma 
316097a3a383SMatthew Wilcox (Oracle) 	ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
316197a3a383SMatthew Wilcox (Oracle) 	if (ret < 0)
31626f70fa51STao Ma 		mlog_errno(ret);
31636f70fa51STao Ma 
31646f70fa51STao Ma 	return ret;
31656f70fa51STao Ma }
31666f70fa51STao Ma 
ocfs2_di_get_clusters(struct ocfs2_cow_context * context,u32 v_cluster,u32 * p_cluster,u32 * num_clusters,unsigned int * extent_flags)3167913580b4STao Ma static int ocfs2_di_get_clusters(struct ocfs2_cow_context *context,
3168913580b4STao Ma 				 u32 v_cluster, u32 *p_cluster,
3169913580b4STao Ma 				 u32 *num_clusters,
3170913580b4STao Ma 				 unsigned int *extent_flags)
3171913580b4STao Ma {
3172913580b4STao Ma 	return ocfs2_get_clusters(context->inode, v_cluster, p_cluster,
3173913580b4STao Ma 				  num_clusters, extent_flags);
3174913580b4STao Ma }
3175913580b4STao Ma 
ocfs2_make_clusters_writable(struct super_block * sb,struct ocfs2_cow_context * context,u32 cpos,u32 p_cluster,u32 num_clusters,unsigned int e_flags)31766f70fa51STao Ma static int ocfs2_make_clusters_writable(struct super_block *sb,
31776f70fa51STao Ma 					struct ocfs2_cow_context *context,
31786f70fa51STao Ma 					u32 cpos, u32 p_cluster,
31796f70fa51STao Ma 					u32 num_clusters, unsigned int e_flags)
31806f70fa51STao Ma {
31816ae23c55STao Ma 	int ret, delete, index, credits =  0;
3182acf3bb00STristan Ye 	u32 new_bit, new_len, orig_num_clusters;
31836ae23c55STao Ma 	unsigned int set_len;
31846f70fa51STao Ma 	struct ocfs2_super *osb = OCFS2_SB(sb);
31856f70fa51STao Ma 	handle_t *handle;
31866ae23c55STao Ma 	struct buffer_head *ref_leaf_bh = NULL;
3187913580b4STao Ma 	struct ocfs2_caching_info *ref_ci = &context->ref_tree->rf_ci;
31886ae23c55STao Ma 	struct ocfs2_refcount_rec rec;
31896ae23c55STao Ma 
3190198aac28STao Ma 	trace_ocfs2_make_clusters_writable(cpos, p_cluster,
3191198aac28STao Ma 					   num_clusters, e_flags);
31926f70fa51STao Ma 
31936f70fa51STao Ma 	ret = ocfs2_lock_refcount_allocators(sb, p_cluster, num_clusters,
3194913580b4STao Ma 					     &context->data_et,
3195913580b4STao Ma 					     ref_ci,
31966f70fa51STao Ma 					     context->ref_root_bh,
31976f70fa51STao Ma 					     &context->meta_ac,
31986f70fa51STao Ma 					     &context->data_ac, &credits);
31996f70fa51STao Ma 	if (ret) {
32006f70fa51STao Ma 		mlog_errno(ret);
32016f70fa51STao Ma 		return ret;
32026f70fa51STao Ma 	}
32036f70fa51STao Ma 
3204492a8a33STao Ma 	if (context->post_refcount)
3205492a8a33STao Ma 		credits += context->post_refcount->credits;
3206492a8a33STao Ma 
3207492a8a33STao Ma 	credits += context->extra_credits;
32086f70fa51STao Ma 	handle = ocfs2_start_trans(osb, credits);
32096f70fa51STao Ma 	if (IS_ERR(handle)) {
32106f70fa51STao Ma 		ret = PTR_ERR(handle);
32116f70fa51STao Ma 		mlog_errno(ret);
32126f70fa51STao Ma 		goto out;
32136f70fa51STao Ma 	}
32146f70fa51STao Ma 
3215acf3bb00STristan Ye 	orig_num_clusters = num_clusters;
3216acf3bb00STristan Ye 
32176f70fa51STao Ma 	while (num_clusters) {
3218913580b4STao Ma 		ret = ocfs2_get_refcount_rec(ref_ci, context->ref_root_bh,
32196ae23c55STao Ma 					     p_cluster, num_clusters,
32206ae23c55STao Ma 					     &rec, &index, &ref_leaf_bh);
32216ae23c55STao Ma 		if (ret) {
32226ae23c55STao Ma 			mlog_errno(ret);
32236ae23c55STao Ma 			goto out_commit;
32246ae23c55STao Ma 		}
32256ae23c55STao Ma 
32266ae23c55STao Ma 		BUG_ON(!rec.r_refcount);
32276ae23c55STao Ma 		set_len = min((u64)p_cluster + num_clusters,
32286ae23c55STao Ma 			      le64_to_cpu(rec.r_cpos) +
32296ae23c55STao Ma 			      le32_to_cpu(rec.r_clusters)) - p_cluster;
32306ae23c55STao Ma 
32316ae23c55STao Ma 		/*
32326ae23c55STao Ma 		 * There are many different situation here.
32336ae23c55STao Ma 		 * 1. If refcount == 1, remove the flag and don't COW.
32346ae23c55STao Ma 		 * 2. If refcount > 1, allocate clusters.
32356ae23c55STao Ma 		 *    Here we may not allocate r_len once at a time, so continue
32366ae23c55STao Ma 		 *    until we reach num_clusters.
32376ae23c55STao Ma 		 */
32386ae23c55STao Ma 		if (le32_to_cpu(rec.r_refcount) == 1) {
32396ae23c55STao Ma 			delete = 0;
3240913580b4STao Ma 			ret = ocfs2_clear_ext_refcount(handle,
3241913580b4STao Ma 						       &context->data_et,
32426ae23c55STao Ma 						       cpos, p_cluster,
32436ae23c55STao Ma 						       set_len, e_flags,
32446ae23c55STao Ma 						       context->meta_ac,
32456ae23c55STao Ma 						       &context->dealloc);
32466ae23c55STao Ma 			if (ret) {
32476ae23c55STao Ma 				mlog_errno(ret);
32486ae23c55STao Ma 				goto out_commit;
32496ae23c55STao Ma 			}
32506ae23c55STao Ma 		} else {
32516ae23c55STao Ma 			delete = 1;
32526ae23c55STao Ma 
32531ed9b777SJoel Becker 			ret = __ocfs2_claim_clusters(handle,
32546ae23c55STao Ma 						     context->data_ac,
32556ae23c55STao Ma 						     1, set_len,
32566f70fa51STao Ma 						     &new_bit, &new_len);
32576f70fa51STao Ma 			if (ret) {
32586f70fa51STao Ma 				mlog_errno(ret);
32596f70fa51STao Ma 				goto out_commit;
32606f70fa51STao Ma 			}
32616f70fa51STao Ma 
32626f70fa51STao Ma 			ret = ocfs2_replace_clusters(handle, context,
32636f70fa51STao Ma 						     cpos, p_cluster, new_bit,
32646f70fa51STao Ma 						     new_len, e_flags);
32656f70fa51STao Ma 			if (ret) {
32666f70fa51STao Ma 				mlog_errno(ret);
32676f70fa51STao Ma 				goto out_commit;
32686f70fa51STao Ma 			}
32696ae23c55STao Ma 			set_len = new_len;
32706f70fa51STao Ma 		}
32716f70fa51STao Ma 
3272913580b4STao Ma 		ret = __ocfs2_decrease_refcount(handle, ref_ci,
32736f70fa51STao Ma 						context->ref_root_bh,
32746ae23c55STao Ma 						p_cluster, set_len,
32756f70fa51STao Ma 						context->meta_ac,
32766ae23c55STao Ma 						&context->dealloc, delete);
32776f70fa51STao Ma 		if (ret) {
32786f70fa51STao Ma 			mlog_errno(ret);
32796f70fa51STao Ma 			goto out_commit;
32806f70fa51STao Ma 		}
32816f70fa51STao Ma 
32826ae23c55STao Ma 		cpos += set_len;
32836ae23c55STao Ma 		p_cluster += set_len;
32846ae23c55STao Ma 		num_clusters -= set_len;
32856ae23c55STao Ma 		brelse(ref_leaf_bh);
32866ae23c55STao Ma 		ref_leaf_bh = NULL;
32876ae23c55STao Ma 	}
32886ae23c55STao Ma 
3289492a8a33STao Ma 	/* handle any post_cow action. */
3290492a8a33STao Ma 	if (context->post_refcount && context->post_refcount->func) {
3291492a8a33STao Ma 		ret = context->post_refcount->func(context->inode, handle,
3292492a8a33STao Ma 						context->post_refcount->para);
3293492a8a33STao Ma 		if (ret) {
3294492a8a33STao Ma 			mlog_errno(ret);
3295492a8a33STao Ma 			goto out_commit;
3296492a8a33STao Ma 		}
3297492a8a33STao Ma 	}
3298492a8a33STao Ma 
32996f70fa51STao Ma 	/*
33006f70fa51STao Ma 	 * Here we should write the new page out first if we are
33016f70fa51STao Ma 	 * in write-back mode.
33026f70fa51STao Ma 	 */
3303492a8a33STao Ma 	if (context->get_clusters == ocfs2_di_get_clusters) {
33043e19a25eSTristan Ye 		ret = ocfs2_cow_sync_writeback(sb, context->inode, cpos,
3305acf3bb00STristan Ye 					       orig_num_clusters);
33066f70fa51STao Ma 		if (ret)
33076f70fa51STao Ma 			mlog_errno(ret);
3308492a8a33STao Ma 	}
33096f70fa51STao Ma 
33106f70fa51STao Ma out_commit:
33116f70fa51STao Ma 	ocfs2_commit_trans(osb, handle);
33126f70fa51STao Ma 
33136f70fa51STao Ma out:
33146f70fa51STao Ma 	if (context->data_ac) {
33156f70fa51STao Ma 		ocfs2_free_alloc_context(context->data_ac);
33166f70fa51STao Ma 		context->data_ac = NULL;
33176f70fa51STao Ma 	}
33186f70fa51STao Ma 	if (context->meta_ac) {
33196f70fa51STao Ma 		ocfs2_free_alloc_context(context->meta_ac);
33206f70fa51STao Ma 		context->meta_ac = NULL;
33216f70fa51STao Ma 	}
33226ae23c55STao Ma 	brelse(ref_leaf_bh);
33236f70fa51STao Ma 
33246f70fa51STao Ma 	return ret;
33256f70fa51STao Ma }
33266f70fa51STao Ma 
ocfs2_replace_cow(struct ocfs2_cow_context * context)3327913580b4STao Ma static int ocfs2_replace_cow(struct ocfs2_cow_context *context)
33286f70fa51STao Ma {
33296f70fa51STao Ma 	int ret = 0;
3330913580b4STao Ma 	struct inode *inode = context->inode;
3331913580b4STao Ma 	u32 cow_start = context->cow_start, cow_len = context->cow_len;
3332913580b4STao Ma 	u32 p_cluster, num_clusters;
33336f70fa51STao Ma 	unsigned int ext_flags;
33346f70fa51STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
33356f70fa51STao Ma 
33361119d3c0Spiaojun 	if (!ocfs2_refcount_tree(osb)) {
33377ecef14aSJoe Perches 		return ocfs2_error(inode->i_sb, "Inode %lu want to use refcount tree, but the feature bit is not set in the super block\n",
33387ecef14aSJoe Perches 				   inode->i_ino);
33396f70fa51STao Ma 	}
33406f70fa51STao Ma 
33416f70fa51STao Ma 	ocfs2_init_dealloc_ctxt(&context->dealloc);
33426f70fa51STao Ma 
33436f70fa51STao Ma 	while (cow_len) {
3344913580b4STao Ma 		ret = context->get_clusters(context, cow_start, &p_cluster,
33456f70fa51STao Ma 					    &num_clusters, &ext_flags);
33466f70fa51STao Ma 		if (ret) {
33476f70fa51STao Ma 			mlog_errno(ret);
33486f70fa51STao Ma 			break;
33496f70fa51STao Ma 		}
33506f70fa51STao Ma 
33516f70fa51STao Ma 		BUG_ON(!(ext_flags & OCFS2_EXT_REFCOUNTED));
33526f70fa51STao Ma 
33536f70fa51STao Ma 		if (cow_len < num_clusters)
33546f70fa51STao Ma 			num_clusters = cow_len;
33556f70fa51STao Ma 
33566f70fa51STao Ma 		ret = ocfs2_make_clusters_writable(inode->i_sb, context,
33576f70fa51STao Ma 						   cow_start, p_cluster,
33586f70fa51STao Ma 						   num_clusters, ext_flags);
33596f70fa51STao Ma 		if (ret) {
33606f70fa51STao Ma 			mlog_errno(ret);
33616f70fa51STao Ma 			break;
33626f70fa51STao Ma 		}
33636f70fa51STao Ma 
33646f70fa51STao Ma 		cow_len -= num_clusters;
33656f70fa51STao Ma 		cow_start += num_clusters;
33666f70fa51STao Ma 	}
33676f70fa51STao Ma 
33686f70fa51STao Ma 	if (ocfs2_dealloc_has_cluster(&context->dealloc)) {
33696f70fa51STao Ma 		ocfs2_schedule_truncate_log_flush(osb, 1);
33706f70fa51STao Ma 		ocfs2_run_deallocs(osb, &context->dealloc);
33716f70fa51STao Ma 	}
33726f70fa51STao Ma 
33736f70fa51STao Ma 	return ret;
33746f70fa51STao Ma }
33756f70fa51STao Ma 
33766f70fa51STao Ma /*
337737f8a2bfSTao Ma  * Starting at cpos, try to CoW write_len clusters.  Don't CoW
337837f8a2bfSTao Ma  * past max_cpos.  This will stop when it runs into a hole or an
337937f8a2bfSTao Ma  * unrefcounted extent.
33806f70fa51STao Ma  */
ocfs2_refcount_cow_hunk(struct inode * inode,struct buffer_head * di_bh,u32 cpos,u32 write_len,u32 max_cpos)33816f70fa51STao Ma static int ocfs2_refcount_cow_hunk(struct inode *inode,
33826f70fa51STao Ma 				   struct buffer_head *di_bh,
338337f8a2bfSTao Ma 				   u32 cpos, u32 write_len, u32 max_cpos)
33846f70fa51STao Ma {
33856f70fa51STao Ma 	int ret;
33866f70fa51STao Ma 	u32 cow_start = 0, cow_len = 0;
33876f70fa51STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
33886f70fa51STao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
33896f70fa51STao Ma 	struct buffer_head *ref_root_bh = NULL;
33906f70fa51STao Ma 	struct ocfs2_refcount_tree *ref_tree;
3391913580b4STao Ma 	struct ocfs2_cow_context *context = NULL;
33926f70fa51STao Ma 
339384e40080SDarrick J. Wong 	BUG_ON(!ocfs2_is_refcount_inode(inode));
33946f70fa51STao Ma 
3395913580b4STao Ma 	ret = ocfs2_refcount_cal_cow_clusters(inode, &di->id2.i_list,
339637f8a2bfSTao Ma 					      cpos, write_len, max_cpos,
33976f70fa51STao Ma 					      &cow_start, &cow_len);
33986f70fa51STao Ma 	if (ret) {
33996f70fa51STao Ma 		mlog_errno(ret);
34006f70fa51STao Ma 		goto out;
34016f70fa51STao Ma 	}
340237f8a2bfSTao Ma 
3403198aac28STao Ma 	trace_ocfs2_refcount_cow_hunk(OCFS2_I(inode)->ip_blkno,
3404198aac28STao Ma 				      cpos, write_len, max_cpos,
3405198aac28STao Ma 				      cow_start, cow_len);
34066f70fa51STao Ma 
34076f70fa51STao Ma 	BUG_ON(cow_len == 0);
34086f70fa51STao Ma 
3409913580b4STao Ma 	context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
3410913580b4STao Ma 	if (!context) {
3411913580b4STao Ma 		ret = -ENOMEM;
3412913580b4STao Ma 		mlog_errno(ret);
3413913580b4STao Ma 		goto out;
3414913580b4STao Ma 	}
3415913580b4STao Ma 
34166f70fa51STao Ma 	ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
34176f70fa51STao Ma 				       1, &ref_tree, &ref_root_bh);
34186f70fa51STao Ma 	if (ret) {
34196f70fa51STao Ma 		mlog_errno(ret);
34206f70fa51STao Ma 		goto out;
34216f70fa51STao Ma 	}
34226f70fa51STao Ma 
3423913580b4STao Ma 	context->inode = inode;
3424913580b4STao Ma 	context->cow_start = cow_start;
3425913580b4STao Ma 	context->cow_len = cow_len;
3426913580b4STao Ma 	context->ref_tree = ref_tree;
3427913580b4STao Ma 	context->ref_root_bh = ref_root_bh;
3428913580b4STao Ma 	context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_page;
3429913580b4STao Ma 	context->get_clusters = ocfs2_di_get_clusters;
3430913580b4STao Ma 
3431913580b4STao Ma 	ocfs2_init_dinode_extent_tree(&context->data_et,
3432913580b4STao Ma 				      INODE_CACHE(inode), di_bh);
3433913580b4STao Ma 
3434913580b4STao Ma 	ret = ocfs2_replace_cow(context);
34356f70fa51STao Ma 	if (ret)
34366f70fa51STao Ma 		mlog_errno(ret);
34376f70fa51STao Ma 
3438913580b4STao Ma 	/*
3439913580b4STao Ma 	 * truncate the extent map here since no matter whether we meet with
3440913580b4STao Ma 	 * any error during the action, we shouldn't trust cached extent map
3441913580b4STao Ma 	 * any more.
3442913580b4STao Ma 	 */
3443913580b4STao Ma 	ocfs2_extent_map_trunc(inode, cow_start);
3444913580b4STao Ma 
34456f70fa51STao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
34466f70fa51STao Ma 	brelse(ref_root_bh);
34476f70fa51STao Ma out:
3448913580b4STao Ma 	kfree(context);
34496f70fa51STao Ma 	return ret;
34506f70fa51STao Ma }
34516f70fa51STao Ma 
34526f70fa51STao Ma /*
34536f70fa51STao Ma  * CoW any and all clusters between cpos and cpos+write_len.
345437f8a2bfSTao Ma  * Don't CoW past max_cpos.  If this returns successfully, all
345537f8a2bfSTao Ma  * clusters between cpos and cpos+write_len are safe to modify.
34566f70fa51STao Ma  */
ocfs2_refcount_cow(struct inode * inode,struct buffer_head * di_bh,u32 cpos,u32 write_len,u32 max_cpos)34576f70fa51STao Ma int ocfs2_refcount_cow(struct inode *inode,
34586f70fa51STao Ma 		       struct buffer_head *di_bh,
345937f8a2bfSTao Ma 		       u32 cpos, u32 write_len, u32 max_cpos)
34606f70fa51STao Ma {
34616f70fa51STao Ma 	int ret = 0;
34626f70fa51STao Ma 	u32 p_cluster, num_clusters;
34636f70fa51STao Ma 	unsigned int ext_flags;
34646f70fa51STao Ma 
34656f70fa51STao Ma 	while (write_len) {
34666f70fa51STao Ma 		ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
34676f70fa51STao Ma 					 &num_clusters, &ext_flags);
34686f70fa51STao Ma 		if (ret) {
34696f70fa51STao Ma 			mlog_errno(ret);
34706f70fa51STao Ma 			break;
34716f70fa51STao Ma 		}
34726f70fa51STao Ma 
34736f70fa51STao Ma 		if (write_len < num_clusters)
34746f70fa51STao Ma 			num_clusters = write_len;
34756f70fa51STao Ma 
34766f70fa51STao Ma 		if (ext_flags & OCFS2_EXT_REFCOUNTED) {
3477c7dd3392STiger Yang 			ret = ocfs2_refcount_cow_hunk(inode, di_bh, cpos,
347837f8a2bfSTao Ma 						      num_clusters, max_cpos);
34796f70fa51STao Ma 			if (ret) {
34806f70fa51STao Ma 				mlog_errno(ret);
34816f70fa51STao Ma 				break;
34826f70fa51STao Ma 			}
34836f70fa51STao Ma 		}
34846f70fa51STao Ma 
34856f70fa51STao Ma 		write_len -= num_clusters;
34866f70fa51STao Ma 		cpos += num_clusters;
34876f70fa51STao Ma 	}
34886f70fa51STao Ma 
34896f70fa51STao Ma 	return ret;
34906f70fa51STao Ma }
3491110a045aSTao Ma 
ocfs2_xattr_value_get_clusters(struct ocfs2_cow_context * context,u32 v_cluster,u32 * p_cluster,u32 * num_clusters,unsigned int * extent_flags)3492492a8a33STao Ma static int ocfs2_xattr_value_get_clusters(struct ocfs2_cow_context *context,
3493492a8a33STao Ma 					  u32 v_cluster, u32 *p_cluster,
3494492a8a33STao Ma 					  u32 *num_clusters,
3495492a8a33STao Ma 					  unsigned int *extent_flags)
3496492a8a33STao Ma {
3497492a8a33STao Ma 	struct inode *inode = context->inode;
3498492a8a33STao Ma 	struct ocfs2_xattr_value_root *xv = context->cow_object;
3499492a8a33STao Ma 
3500492a8a33STao Ma 	return ocfs2_xattr_get_clusters(inode, v_cluster, p_cluster,
3501492a8a33STao Ma 					num_clusters, &xv->xr_list,
3502492a8a33STao Ma 					extent_flags);
3503492a8a33STao Ma }
3504492a8a33STao Ma 
3505492a8a33STao Ma /*
3506492a8a33STao Ma  * Given a xattr value root, calculate the most meta/credits we need for
3507492a8a33STao Ma  * refcount tree change if we truncate it to 0.
3508492a8a33STao Ma  */
ocfs2_refcounted_xattr_delete_need(struct inode * inode,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_xattr_value_root * xv,int * meta_add,int * credits)3509492a8a33STao Ma int ocfs2_refcounted_xattr_delete_need(struct inode *inode,
3510492a8a33STao Ma 				       struct ocfs2_caching_info *ref_ci,
3511492a8a33STao Ma 				       struct buffer_head *ref_root_bh,
3512492a8a33STao Ma 				       struct ocfs2_xattr_value_root *xv,
3513492a8a33STao Ma 				       int *meta_add, int *credits)
3514492a8a33STao Ma {
3515492a8a33STao Ma 	int ret = 0, index, ref_blocks = 0;
3516492a8a33STao Ma 	u32 p_cluster, num_clusters;
3517492a8a33STao Ma 	u32 cpos = 0, clusters = le32_to_cpu(xv->xr_clusters);
3518492a8a33STao Ma 	struct ocfs2_refcount_block *rb;
3519492a8a33STao Ma 	struct ocfs2_refcount_rec rec;
3520492a8a33STao Ma 	struct buffer_head *ref_leaf_bh = NULL;
3521492a8a33STao Ma 
3522492a8a33STao Ma 	while (cpos < clusters) {
3523492a8a33STao Ma 		ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
3524492a8a33STao Ma 					       &num_clusters, &xv->xr_list,
3525492a8a33STao Ma 					       NULL);
3526492a8a33STao Ma 		if (ret) {
3527492a8a33STao Ma 			mlog_errno(ret);
3528492a8a33STao Ma 			goto out;
3529492a8a33STao Ma 		}
3530492a8a33STao Ma 
3531492a8a33STao Ma 		cpos += num_clusters;
3532492a8a33STao Ma 
3533492a8a33STao Ma 		while (num_clusters) {
3534492a8a33STao Ma 			ret = ocfs2_get_refcount_rec(ref_ci, ref_root_bh,
3535492a8a33STao Ma 						     p_cluster, num_clusters,
3536492a8a33STao Ma 						     &rec, &index,
3537492a8a33STao Ma 						     &ref_leaf_bh);
3538492a8a33STao Ma 			if (ret) {
3539492a8a33STao Ma 				mlog_errno(ret);
3540492a8a33STao Ma 				goto out;
3541492a8a33STao Ma 			}
3542492a8a33STao Ma 
3543492a8a33STao Ma 			BUG_ON(!rec.r_refcount);
3544492a8a33STao Ma 
3545492a8a33STao Ma 			rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
3546492a8a33STao Ma 
3547492a8a33STao Ma 			/*
3548492a8a33STao Ma 			 * We really don't know whether the other clusters is in
3549492a8a33STao Ma 			 * this refcount block or not, so just take the worst
3550492a8a33STao Ma 			 * case that all the clusters are in this block and each
3551492a8a33STao Ma 			 * one will split a refcount rec, so totally we need
3552492a8a33STao Ma 			 * clusters * 2 new refcount rec.
3553492a8a33STao Ma 			 */
3554e1bf4cc6SAl Viro 			if (le16_to_cpu(rb->rf_records.rl_used) + clusters * 2 >
3555492a8a33STao Ma 			    le16_to_cpu(rb->rf_records.rl_count))
3556492a8a33STao Ma 				ref_blocks++;
3557492a8a33STao Ma 
3558492a8a33STao Ma 			*credits += 1;
3559492a8a33STao Ma 			brelse(ref_leaf_bh);
3560492a8a33STao Ma 			ref_leaf_bh = NULL;
3561492a8a33STao Ma 
3562492a8a33STao Ma 			if (num_clusters <= le32_to_cpu(rec.r_clusters))
3563492a8a33STao Ma 				break;
3564492a8a33STao Ma 			else
3565492a8a33STao Ma 				num_clusters -= le32_to_cpu(rec.r_clusters);
3566492a8a33STao Ma 			p_cluster += num_clusters;
3567492a8a33STao Ma 		}
3568492a8a33STao Ma 	}
3569492a8a33STao Ma 
3570492a8a33STao Ma 	*meta_add += ref_blocks;
3571492a8a33STao Ma 	if (!ref_blocks)
3572492a8a33STao Ma 		goto out;
3573492a8a33STao Ma 
3574492a8a33STao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
3575492a8a33STao Ma 	if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
3576492a8a33STao Ma 		*credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
3577492a8a33STao Ma 	else {
3578492a8a33STao Ma 		struct ocfs2_extent_tree et;
3579492a8a33STao Ma 
3580492a8a33STao Ma 		ocfs2_init_refcount_extent_tree(&et, ref_ci, ref_root_bh);
3581492a8a33STao Ma 		*credits += ocfs2_calc_extend_credits(inode->i_sb,
358206f9da6eSGoldwyn Rodrigues 						      et.et_root_el);
3583492a8a33STao Ma 	}
3584492a8a33STao Ma 
3585492a8a33STao Ma out:
3586492a8a33STao Ma 	brelse(ref_leaf_bh);
3587492a8a33STao Ma 	return ret;
3588492a8a33STao Ma }
3589492a8a33STao Ma 
3590492a8a33STao Ma /*
3591492a8a33STao Ma  * Do CoW for xattr.
3592492a8a33STao Ma  */
ocfs2_refcount_cow_xattr(struct inode * inode,struct ocfs2_dinode * di,struct ocfs2_xattr_value_buf * vb,struct ocfs2_refcount_tree * ref_tree,struct buffer_head * ref_root_bh,u32 cpos,u32 write_len,struct ocfs2_post_refcount * post)3593492a8a33STao Ma int ocfs2_refcount_cow_xattr(struct inode *inode,
3594492a8a33STao Ma 			     struct ocfs2_dinode *di,
3595492a8a33STao Ma 			     struct ocfs2_xattr_value_buf *vb,
3596492a8a33STao Ma 			     struct ocfs2_refcount_tree *ref_tree,
3597492a8a33STao Ma 			     struct buffer_head *ref_root_bh,
3598492a8a33STao Ma 			     u32 cpos, u32 write_len,
3599492a8a33STao Ma 			     struct ocfs2_post_refcount *post)
3600492a8a33STao Ma {
3601492a8a33STao Ma 	int ret;
3602492a8a33STao Ma 	struct ocfs2_xattr_value_root *xv = vb->vb_xv;
3603492a8a33STao Ma 	struct ocfs2_cow_context *context = NULL;
3604492a8a33STao Ma 	u32 cow_start, cow_len;
3605492a8a33STao Ma 
360684e40080SDarrick J. Wong 	BUG_ON(!ocfs2_is_refcount_inode(inode));
3607492a8a33STao Ma 
3608492a8a33STao Ma 	ret = ocfs2_refcount_cal_cow_clusters(inode, &xv->xr_list,
3609492a8a33STao Ma 					      cpos, write_len, UINT_MAX,
3610492a8a33STao Ma 					      &cow_start, &cow_len);
3611492a8a33STao Ma 	if (ret) {
3612492a8a33STao Ma 		mlog_errno(ret);
3613492a8a33STao Ma 		goto out;
3614492a8a33STao Ma 	}
3615492a8a33STao Ma 
3616492a8a33STao Ma 	BUG_ON(cow_len == 0);
3617492a8a33STao Ma 
3618492a8a33STao Ma 	context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
3619492a8a33STao Ma 	if (!context) {
3620492a8a33STao Ma 		ret = -ENOMEM;
3621492a8a33STao Ma 		mlog_errno(ret);
3622492a8a33STao Ma 		goto out;
3623492a8a33STao Ma 	}
3624492a8a33STao Ma 
3625492a8a33STao Ma 	context->inode = inode;
3626492a8a33STao Ma 	context->cow_start = cow_start;
3627492a8a33STao Ma 	context->cow_len = cow_len;
3628492a8a33STao Ma 	context->ref_tree = ref_tree;
36296eab04a8SJustin P. Mattock 	context->ref_root_bh = ref_root_bh;
3630492a8a33STao Ma 	context->cow_object = xv;
3631492a8a33STao Ma 
3632492a8a33STao Ma 	context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_jbd;
3633492a8a33STao Ma 	/* We need the extra credits for duplicate_clusters by jbd. */
3634492a8a33STao Ma 	context->extra_credits =
3635492a8a33STao Ma 		ocfs2_clusters_to_blocks(inode->i_sb, 1) * cow_len;
3636492a8a33STao Ma 	context->get_clusters = ocfs2_xattr_value_get_clusters;
3637492a8a33STao Ma 	context->post_refcount = post;
3638492a8a33STao Ma 
3639492a8a33STao Ma 	ocfs2_init_xattr_value_extent_tree(&context->data_et,
3640492a8a33STao Ma 					   INODE_CACHE(inode), vb);
3641492a8a33STao Ma 
3642492a8a33STao Ma 	ret = ocfs2_replace_cow(context);
3643492a8a33STao Ma 	if (ret)
3644492a8a33STao Ma 		mlog_errno(ret);
3645492a8a33STao Ma 
3646492a8a33STao Ma out:
3647492a8a33STao Ma 	kfree(context);
3648492a8a33STao Ma 	return ret;
3649492a8a33STao Ma }
3650492a8a33STao Ma 
3651110a045aSTao Ma /*
3652110a045aSTao Ma  * Insert a new extent into refcount tree and mark a extent rec
3653110a045aSTao Ma  * as refcounted in the dinode tree.
3654110a045aSTao Ma  */
ocfs2_add_refcount_flag(struct inode * inode,struct ocfs2_extent_tree * data_et,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,u32 cpos,u32 p_cluster,u32 num_clusters,struct ocfs2_cached_dealloc_ctxt * dealloc,struct ocfs2_post_refcount * post)3655110a045aSTao Ma int ocfs2_add_refcount_flag(struct inode *inode,
3656110a045aSTao Ma 			    struct ocfs2_extent_tree *data_et,
3657110a045aSTao Ma 			    struct ocfs2_caching_info *ref_ci,
3658110a045aSTao Ma 			    struct buffer_head *ref_root_bh,
3659110a045aSTao Ma 			    u32 cpos, u32 p_cluster, u32 num_clusters,
36600129241eSTao Ma 			    struct ocfs2_cached_dealloc_ctxt *dealloc,
36610129241eSTao Ma 			    struct ocfs2_post_refcount *post)
3662110a045aSTao Ma {
3663110a045aSTao Ma 	int ret;
3664110a045aSTao Ma 	handle_t *handle;
3665110a045aSTao Ma 	int credits = 1, ref_blocks = 0;
3666110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3667110a045aSTao Ma 	struct ocfs2_alloc_context *meta_ac = NULL;
3668110a045aSTao Ma 
36693e10b793SDarrick J. Wong 	/* We need to be able to handle at least an extent tree split. */
36703e10b793SDarrick J. Wong 	ref_blocks = ocfs2_extend_meta_needed(data_et->et_root_el);
36713e10b793SDarrick J. Wong 
3672110a045aSTao Ma 	ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
3673110a045aSTao Ma 					       ref_ci, ref_root_bh,
3674110a045aSTao Ma 					       p_cluster, num_clusters,
3675110a045aSTao Ma 					       &ref_blocks, &credits);
3676110a045aSTao Ma 	if (ret) {
3677110a045aSTao Ma 		mlog_errno(ret);
3678110a045aSTao Ma 		goto out;
3679110a045aSTao Ma 	}
3680110a045aSTao Ma 
3681198aac28STao Ma 	trace_ocfs2_add_refcount_flag(ref_blocks, credits);
3682110a045aSTao Ma 
3683110a045aSTao Ma 	if (ref_blocks) {
36841119d3c0Spiaojun 		ret = ocfs2_reserve_new_metadata_blocks(osb,
3685110a045aSTao Ma 							ref_blocks, &meta_ac);
3686110a045aSTao Ma 		if (ret) {
3687110a045aSTao Ma 			mlog_errno(ret);
3688110a045aSTao Ma 			goto out;
3689110a045aSTao Ma 		}
3690110a045aSTao Ma 	}
3691110a045aSTao Ma 
36920129241eSTao Ma 	if (post)
36930129241eSTao Ma 		credits += post->credits;
36940129241eSTao Ma 
3695110a045aSTao Ma 	handle = ocfs2_start_trans(osb, credits);
3696110a045aSTao Ma 	if (IS_ERR(handle)) {
3697110a045aSTao Ma 		ret = PTR_ERR(handle);
3698110a045aSTao Ma 		mlog_errno(ret);
3699110a045aSTao Ma 		goto out;
3700110a045aSTao Ma 	}
3701110a045aSTao Ma 
3702110a045aSTao Ma 	ret = ocfs2_mark_extent_refcounted(inode, data_et, handle,
3703110a045aSTao Ma 					   cpos, num_clusters, p_cluster,
3704110a045aSTao Ma 					   meta_ac, dealloc);
3705110a045aSTao Ma 	if (ret) {
3706110a045aSTao Ma 		mlog_errno(ret);
3707110a045aSTao Ma 		goto out_commit;
3708110a045aSTao Ma 	}
3709110a045aSTao Ma 
37107540c1a7STao Ma 	ret = __ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
37117540c1a7STao Ma 					p_cluster, num_clusters, 0,
3712110a045aSTao Ma 					meta_ac, dealloc);
37130129241eSTao Ma 	if (ret) {
37140129241eSTao Ma 		mlog_errno(ret);
37150129241eSTao Ma 		goto out_commit;
37160129241eSTao Ma 	}
37170129241eSTao Ma 
37180129241eSTao Ma 	if (post && post->func) {
37190129241eSTao Ma 		ret = post->func(inode, handle, post->para);
3720110a045aSTao Ma 		if (ret)
3721110a045aSTao Ma 			mlog_errno(ret);
37220129241eSTao Ma 	}
3723110a045aSTao Ma 
3724110a045aSTao Ma out_commit:
3725110a045aSTao Ma 	ocfs2_commit_trans(osb, handle);
3726110a045aSTao Ma out:
3727110a045aSTao Ma 	if (meta_ac)
3728110a045aSTao Ma 		ocfs2_free_alloc_context(meta_ac);
3729110a045aSTao Ma 	return ret;
3730110a045aSTao Ma }
3731110a045aSTao Ma 
ocfs2_change_ctime(struct inode * inode,struct buffer_head * di_bh)3732a9063ab9STao Ma static int ocfs2_change_ctime(struct inode *inode,
3733110a045aSTao Ma 			      struct buffer_head *di_bh)
3734110a045aSTao Ma {
3735110a045aSTao Ma 	int ret;
3736a9063ab9STao Ma 	handle_t *handle;
3737a9063ab9STao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3738a9063ab9STao Ma 
3739a9063ab9STao Ma 	handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
3740a9063ab9STao Ma 				   OCFS2_INODE_UPDATE_CREDITS);
3741a9063ab9STao Ma 	if (IS_ERR(handle)) {
3742a9063ab9STao Ma 		ret = PTR_ERR(handle);
3743a9063ab9STao Ma 		mlog_errno(ret);
3744a9063ab9STao Ma 		goto out;
3745a9063ab9STao Ma 	}
3746a9063ab9STao Ma 
3747a9063ab9STao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
3748a9063ab9STao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
3749a9063ab9STao Ma 	if (ret) {
3750a9063ab9STao Ma 		mlog_errno(ret);
3751a9063ab9STao Ma 		goto out_commit;
3752a9063ab9STao Ma 	}
3753a9063ab9STao Ma 
37546861de97SJeff Layton 	inode_set_ctime_current(inode);
375510fc3a18SJeff Layton 	di->i_ctime = cpu_to_le64(inode_get_ctime_sec(inode));
375610fc3a18SJeff Layton 	di->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode));
3757a9063ab9STao Ma 
3758a9063ab9STao Ma 	ocfs2_journal_dirty(handle, di_bh);
3759a9063ab9STao Ma 
3760a9063ab9STao Ma out_commit:
3761a9063ab9STao Ma 	ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3762a9063ab9STao Ma out:
3763a9063ab9STao Ma 	return ret;
3764a9063ab9STao Ma }
3765a9063ab9STao Ma 
ocfs2_attach_refcount_tree(struct inode * inode,struct buffer_head * di_bh)3766a9063ab9STao Ma static int ocfs2_attach_refcount_tree(struct inode *inode,
3767a9063ab9STao Ma 				      struct buffer_head *di_bh)
3768a9063ab9STao Ma {
3769a9063ab9STao Ma 	int ret, data_changed = 0;
3770110a045aSTao Ma 	struct buffer_head *ref_root_bh = NULL;
3771110a045aSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
3772110a045aSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3773110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3774110a045aSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
3775110a045aSTao Ma 	unsigned int ext_flags;
3776110a045aSTao Ma 	loff_t size;
3777110a045aSTao Ma 	u32 cpos, num_clusters, clusters, p_cluster;
3778110a045aSTao Ma 	struct ocfs2_cached_dealloc_ctxt dealloc;
3779110a045aSTao Ma 	struct ocfs2_extent_tree di_et;
3780110a045aSTao Ma 
3781110a045aSTao Ma 	ocfs2_init_dealloc_ctxt(&dealloc);
3782110a045aSTao Ma 
378384e40080SDarrick J. Wong 	if (!ocfs2_is_refcount_inode(inode)) {
3784110a045aSTao Ma 		ret = ocfs2_create_refcount_tree(inode, di_bh);
3785110a045aSTao Ma 		if (ret) {
3786110a045aSTao Ma 			mlog_errno(ret);
3787110a045aSTao Ma 			goto out;
3788110a045aSTao Ma 		}
3789110a045aSTao Ma 	}
3790110a045aSTao Ma 
3791110a045aSTao Ma 	BUG_ON(!di->i_refcount_loc);
3792110a045aSTao Ma 	ret = ocfs2_lock_refcount_tree(osb,
3793110a045aSTao Ma 				       le64_to_cpu(di->i_refcount_loc), 1,
3794110a045aSTao Ma 				       &ref_tree, &ref_root_bh);
3795110a045aSTao Ma 	if (ret) {
3796110a045aSTao Ma 		mlog_errno(ret);
3797110a045aSTao Ma 		goto out;
3798110a045aSTao Ma 	}
3799110a045aSTao Ma 
38002f48d593STao Ma 	if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
38012f48d593STao Ma 		goto attach_xattr;
38022f48d593STao Ma 
3803110a045aSTao Ma 	ocfs2_init_dinode_extent_tree(&di_et, INODE_CACHE(inode), di_bh);
3804110a045aSTao Ma 
3805110a045aSTao Ma 	size = i_size_read(inode);
3806110a045aSTao Ma 	clusters = ocfs2_clusters_for_bytes(inode->i_sb, size);
3807110a045aSTao Ma 
3808110a045aSTao Ma 	cpos = 0;
3809110a045aSTao Ma 	while (cpos < clusters) {
3810110a045aSTao Ma 		ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
3811110a045aSTao Ma 					 &num_clusters, &ext_flags);
38122b0f6eaeSJoseph Qi 		if (ret) {
38132b0f6eaeSJoseph Qi 			mlog_errno(ret);
38142b0f6eaeSJoseph Qi 			goto unlock;
38152b0f6eaeSJoseph Qi 		}
3816110a045aSTao Ma 		if (p_cluster && !(ext_flags & OCFS2_EXT_REFCOUNTED)) {
3817110a045aSTao Ma 			ret = ocfs2_add_refcount_flag(inode, &di_et,
3818110a045aSTao Ma 						      &ref_tree->rf_ci,
3819110a045aSTao Ma 						      ref_root_bh, cpos,
3820110a045aSTao Ma 						      p_cluster, num_clusters,
38210129241eSTao Ma 						      &dealloc, NULL);
3822110a045aSTao Ma 			if (ret) {
3823110a045aSTao Ma 				mlog_errno(ret);
3824a9063ab9STao Ma 				goto unlock;
3825110a045aSTao Ma 			}
3826a9063ab9STao Ma 
3827a9063ab9STao Ma 			data_changed = 1;
3828110a045aSTao Ma 		}
3829110a045aSTao Ma 		cpos += num_clusters;
3830110a045aSTao Ma 	}
3831110a045aSTao Ma 
38322f48d593STao Ma attach_xattr:
38330129241eSTao Ma 	if (oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) {
38340129241eSTao Ma 		ret = ocfs2_xattr_attach_refcount_tree(inode, di_bh,
38350129241eSTao Ma 						       &ref_tree->rf_ci,
38360129241eSTao Ma 						       ref_root_bh,
38370129241eSTao Ma 						       &dealloc);
38380129241eSTao Ma 		if (ret) {
38390129241eSTao Ma 			mlog_errno(ret);
38400129241eSTao Ma 			goto unlock;
38410129241eSTao Ma 		}
38420129241eSTao Ma 	}
38430129241eSTao Ma 
3844a9063ab9STao Ma 	if (data_changed) {
3845a9063ab9STao Ma 		ret = ocfs2_change_ctime(inode, di_bh);
3846a9063ab9STao Ma 		if (ret)
3847a9063ab9STao Ma 			mlog_errno(ret);
3848a9063ab9STao Ma 	}
3849a9063ab9STao Ma 
3850a9063ab9STao Ma unlock:
3851110a045aSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3852110a045aSTao Ma 	brelse(ref_root_bh);
3853110a045aSTao Ma 
3854110a045aSTao Ma 	if (!ret && ocfs2_dealloc_has_cluster(&dealloc)) {
3855110a045aSTao Ma 		ocfs2_schedule_truncate_log_flush(osb, 1);
3856110a045aSTao Ma 		ocfs2_run_deallocs(osb, &dealloc);
3857110a045aSTao Ma 	}
3858110a045aSTao Ma out:
3859110a045aSTao Ma 	/*
3860110a045aSTao Ma 	 * Empty the extent map so that we may get the right extent
3861110a045aSTao Ma 	 * record from the disk.
3862110a045aSTao Ma 	 */
3863110a045aSTao Ma 	ocfs2_extent_map_trunc(inode, 0);
3864110a045aSTao Ma 
3865110a045aSTao Ma 	return ret;
3866110a045aSTao Ma }
3867110a045aSTao Ma 
ocfs2_add_refcounted_extent(struct inode * inode,struct ocfs2_extent_tree * et,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,u32 cpos,u32 p_cluster,u32 num_clusters,unsigned int ext_flags,struct ocfs2_cached_dealloc_ctxt * dealloc)3868110a045aSTao Ma static int ocfs2_add_refcounted_extent(struct inode *inode,
3869110a045aSTao Ma 				   struct ocfs2_extent_tree *et,
3870110a045aSTao Ma 				   struct ocfs2_caching_info *ref_ci,
3871110a045aSTao Ma 				   struct buffer_head *ref_root_bh,
3872110a045aSTao Ma 				   u32 cpos, u32 p_cluster, u32 num_clusters,
3873110a045aSTao Ma 				   unsigned int ext_flags,
3874110a045aSTao Ma 				   struct ocfs2_cached_dealloc_ctxt *dealloc)
3875110a045aSTao Ma {
3876110a045aSTao Ma 	int ret;
3877110a045aSTao Ma 	handle_t *handle;
3878110a045aSTao Ma 	int credits = 0;
3879110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3880110a045aSTao Ma 	struct ocfs2_alloc_context *meta_ac = NULL;
3881110a045aSTao Ma 
3882110a045aSTao Ma 	ret = ocfs2_lock_refcount_allocators(inode->i_sb,
3883110a045aSTao Ma 					     p_cluster, num_clusters,
3884110a045aSTao Ma 					     et, ref_ci,
3885110a045aSTao Ma 					     ref_root_bh, &meta_ac,
3886110a045aSTao Ma 					     NULL, &credits);
3887110a045aSTao Ma 	if (ret) {
3888110a045aSTao Ma 		mlog_errno(ret);
3889110a045aSTao Ma 		goto out;
3890110a045aSTao Ma 	}
3891110a045aSTao Ma 
3892110a045aSTao Ma 	handle = ocfs2_start_trans(osb, credits);
3893110a045aSTao Ma 	if (IS_ERR(handle)) {
3894110a045aSTao Ma 		ret = PTR_ERR(handle);
3895110a045aSTao Ma 		mlog_errno(ret);
3896110a045aSTao Ma 		goto out;
3897110a045aSTao Ma 	}
3898110a045aSTao Ma 
3899110a045aSTao Ma 	ret = ocfs2_insert_extent(handle, et, cpos,
390012d4cec9STao Ma 			ocfs2_clusters_to_blocks(inode->i_sb, p_cluster),
3901110a045aSTao Ma 			num_clusters, ext_flags, meta_ac);
3902110a045aSTao Ma 	if (ret) {
3903110a045aSTao Ma 		mlog_errno(ret);
3904110a045aSTao Ma 		goto out_commit;
3905110a045aSTao Ma 	}
3906110a045aSTao Ma 
39072999d12fSTao Ma 	ret = ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
3908110a045aSTao Ma 				      p_cluster, num_clusters,
3909110a045aSTao Ma 				      meta_ac, dealloc);
391086e59436SDarrick J. Wong 	if (ret) {
391186e59436SDarrick J. Wong 		mlog_errno(ret);
391286e59436SDarrick J. Wong 		goto out_commit;
391386e59436SDarrick J. Wong 	}
391486e59436SDarrick J. Wong 
391586e59436SDarrick J. Wong 	ret = dquot_alloc_space_nodirty(inode,
391686e59436SDarrick J. Wong 		ocfs2_clusters_to_bytes(osb->sb, num_clusters));
3917110a045aSTao Ma 	if (ret)
3918110a045aSTao Ma 		mlog_errno(ret);
3919110a045aSTao Ma 
3920110a045aSTao Ma out_commit:
3921110a045aSTao Ma 	ocfs2_commit_trans(osb, handle);
3922110a045aSTao Ma out:
3923110a045aSTao Ma 	if (meta_ac)
3924110a045aSTao Ma 		ocfs2_free_alloc_context(meta_ac);
3925110a045aSTao Ma 	return ret;
3926110a045aSTao Ma }
3927110a045aSTao Ma 
ocfs2_duplicate_inline_data(struct inode * s_inode,struct buffer_head * s_bh,struct inode * t_inode,struct buffer_head * t_bh)39282f48d593STao Ma static int ocfs2_duplicate_inline_data(struct inode *s_inode,
39292f48d593STao Ma 				       struct buffer_head *s_bh,
39302f48d593STao Ma 				       struct inode *t_inode,
39312f48d593STao Ma 				       struct buffer_head *t_bh)
39322f48d593STao Ma {
39332f48d593STao Ma 	int ret;
39342f48d593STao Ma 	handle_t *handle;
39352f48d593STao Ma 	struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb);
39362f48d593STao Ma 	struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data;
39372f48d593STao Ma 	struct ocfs2_dinode *t_di = (struct ocfs2_dinode *)t_bh->b_data;
39382f48d593STao Ma 
39392f48d593STao Ma 	BUG_ON(!(OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL));
39402f48d593STao Ma 
39412f48d593STao Ma 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
39422f48d593STao Ma 	if (IS_ERR(handle)) {
39432f48d593STao Ma 		ret = PTR_ERR(handle);
39442f48d593STao Ma 		mlog_errno(ret);
39452f48d593STao Ma 		goto out;
39462f48d593STao Ma 	}
39472f48d593STao Ma 
39482f48d593STao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh,
39492f48d593STao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
39502f48d593STao Ma 	if (ret) {
39512f48d593STao Ma 		mlog_errno(ret);
39522f48d593STao Ma 		goto out_commit;
39532f48d593STao Ma 	}
39542f48d593STao Ma 
39552f48d593STao Ma 	t_di->id2.i_data.id_count = s_di->id2.i_data.id_count;
39562f48d593STao Ma 	memcpy(t_di->id2.i_data.id_data, s_di->id2.i_data.id_data,
39572f48d593STao Ma 	       le16_to_cpu(s_di->id2.i_data.id_count));
39582f48d593STao Ma 	spin_lock(&OCFS2_I(t_inode)->ip_lock);
39592f48d593STao Ma 	OCFS2_I(t_inode)->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
39602f48d593STao Ma 	t_di->i_dyn_features = cpu_to_le16(OCFS2_I(t_inode)->ip_dyn_features);
39612f48d593STao Ma 	spin_unlock(&OCFS2_I(t_inode)->ip_lock);
39622f48d593STao Ma 
39632f48d593STao Ma 	ocfs2_journal_dirty(handle, t_bh);
39642f48d593STao Ma 
39652f48d593STao Ma out_commit:
39662f48d593STao Ma 	ocfs2_commit_trans(osb, handle);
39672f48d593STao Ma out:
39682f48d593STao Ma 	return ret;
39692f48d593STao Ma }
39702f48d593STao Ma 
ocfs2_duplicate_extent_list(struct inode * s_inode,struct inode * t_inode,struct buffer_head * t_bh,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_cached_dealloc_ctxt * dealloc)3971110a045aSTao Ma static int ocfs2_duplicate_extent_list(struct inode *s_inode,
3972110a045aSTao Ma 				struct inode *t_inode,
3973110a045aSTao Ma 				struct buffer_head *t_bh,
3974110a045aSTao Ma 				struct ocfs2_caching_info *ref_ci,
3975110a045aSTao Ma 				struct buffer_head *ref_root_bh,
3976110a045aSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
3977110a045aSTao Ma {
3978110a045aSTao Ma 	int ret = 0;
3979110a045aSTao Ma 	u32 p_cluster, num_clusters, clusters, cpos;
3980110a045aSTao Ma 	loff_t size;
3981110a045aSTao Ma 	unsigned int ext_flags;
3982110a045aSTao Ma 	struct ocfs2_extent_tree et;
3983110a045aSTao Ma 
3984110a045aSTao Ma 	ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(t_inode), t_bh);
3985110a045aSTao Ma 
3986110a045aSTao Ma 	size = i_size_read(s_inode);
3987110a045aSTao Ma 	clusters = ocfs2_clusters_for_bytes(s_inode->i_sb, size);
3988110a045aSTao Ma 
3989110a045aSTao Ma 	cpos = 0;
3990110a045aSTao Ma 	while (cpos < clusters) {
3991110a045aSTao Ma 		ret = ocfs2_get_clusters(s_inode, cpos, &p_cluster,
3992110a045aSTao Ma 					 &num_clusters, &ext_flags);
39932b0f6eaeSJoseph Qi 		if (ret) {
39942b0f6eaeSJoseph Qi 			mlog_errno(ret);
39952b0f6eaeSJoseph Qi 			goto out;
39962b0f6eaeSJoseph Qi 		}
3997110a045aSTao Ma 		if (p_cluster) {
3998110a045aSTao Ma 			ret = ocfs2_add_refcounted_extent(t_inode, &et,
3999110a045aSTao Ma 							  ref_ci, ref_root_bh,
4000110a045aSTao Ma 							  cpos, p_cluster,
4001110a045aSTao Ma 							  num_clusters,
4002110a045aSTao Ma 							  ext_flags,
4003110a045aSTao Ma 							  dealloc);
4004110a045aSTao Ma 			if (ret) {
4005110a045aSTao Ma 				mlog_errno(ret);
4006110a045aSTao Ma 				goto out;
4007110a045aSTao Ma 			}
4008110a045aSTao Ma 		}
4009110a045aSTao Ma 
4010110a045aSTao Ma 		cpos += num_clusters;
4011110a045aSTao Ma 	}
4012110a045aSTao Ma 
4013110a045aSTao Ma out:
4014110a045aSTao Ma 	return ret;
4015110a045aSTao Ma }
4016110a045aSTao Ma 
4017a9063ab9STao Ma /*
4018a9063ab9STao Ma  * change the new file's attributes to the src.
4019a9063ab9STao Ma  *
4020a9063ab9STao Ma  * reflink creates a snapshot of a file, that means the attributes
4021a9063ab9STao Ma  * must be identical except for three exceptions - nlink, ino, and ctime.
4022a9063ab9STao Ma  */
ocfs2_complete_reflink(struct inode * s_inode,struct buffer_head * s_bh,struct inode * t_inode,struct buffer_head * t_bh,bool preserve)4023a9063ab9STao Ma static int ocfs2_complete_reflink(struct inode *s_inode,
4024a9063ab9STao Ma 				  struct buffer_head *s_bh,
4025a9063ab9STao Ma 				  struct inode *t_inode,
40260fe9b66cSTao Ma 				  struct buffer_head *t_bh,
40270fe9b66cSTao Ma 				  bool preserve)
4028a9063ab9STao Ma {
4029a9063ab9STao Ma 	int ret;
4030a9063ab9STao Ma 	handle_t *handle;
4031a9063ab9STao Ma 	struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data;
4032a9063ab9STao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)t_bh->b_data;
4033a9063ab9STao Ma 	loff_t size = i_size_read(s_inode);
4034a9063ab9STao Ma 
4035a9063ab9STao Ma 	handle = ocfs2_start_trans(OCFS2_SB(t_inode->i_sb),
4036a9063ab9STao Ma 				   OCFS2_INODE_UPDATE_CREDITS);
4037a9063ab9STao Ma 	if (IS_ERR(handle)) {
4038a9063ab9STao Ma 		ret = PTR_ERR(handle);
4039a9063ab9STao Ma 		mlog_errno(ret);
4040a9063ab9STao Ma 		return ret;
4041a9063ab9STao Ma 	}
4042a9063ab9STao Ma 
4043a9063ab9STao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh,
4044a9063ab9STao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
4045a9063ab9STao Ma 	if (ret) {
4046a9063ab9STao Ma 		mlog_errno(ret);
4047a9063ab9STao Ma 		goto out_commit;
4048a9063ab9STao Ma 	}
4049a9063ab9STao Ma 
4050a9063ab9STao Ma 	spin_lock(&OCFS2_I(t_inode)->ip_lock);
4051a9063ab9STao Ma 	OCFS2_I(t_inode)->ip_clusters = OCFS2_I(s_inode)->ip_clusters;
4052a9063ab9STao Ma 	OCFS2_I(t_inode)->ip_attr = OCFS2_I(s_inode)->ip_attr;
4053a9063ab9STao Ma 	OCFS2_I(t_inode)->ip_dyn_features = OCFS2_I(s_inode)->ip_dyn_features;
4054a9063ab9STao Ma 	spin_unlock(&OCFS2_I(t_inode)->ip_lock);
4055a9063ab9STao Ma 	i_size_write(t_inode, size);
40566527f8f8STao Ma 	t_inode->i_blocks = s_inode->i_blocks;
4057a9063ab9STao Ma 
4058a9063ab9STao Ma 	di->i_xattr_inline_size = s_di->i_xattr_inline_size;
4059a9063ab9STao Ma 	di->i_clusters = s_di->i_clusters;
4060a9063ab9STao Ma 	di->i_size = s_di->i_size;
4061a9063ab9STao Ma 	di->i_dyn_features = s_di->i_dyn_features;
4062a9063ab9STao Ma 	di->i_attr = s_di->i_attr;
40630fe9b66cSTao Ma 
40640fe9b66cSTao Ma 	if (preserve) {
4065c21a534eSTao Ma 		t_inode->i_uid = s_inode->i_uid;
4066c21a534eSTao Ma 		t_inode->i_gid = s_inode->i_gid;
4067c21a534eSTao Ma 		t_inode->i_mode = s_inode->i_mode;
4068a9063ab9STao Ma 		di->i_uid = s_di->i_uid;
4069a9063ab9STao Ma 		di->i_gid = s_di->i_gid;
4070a9063ab9STao Ma 		di->i_mode = s_di->i_mode;
4071a9063ab9STao Ma 
4072a9063ab9STao Ma 		/*
4073a9063ab9STao Ma 		 * update time.
40740fe9b66cSTao Ma 		 * we want mtime to appear identical to the source and
40750fe9b66cSTao Ma 		 * update ctime.
4076a9063ab9STao Ma 		 */
40776861de97SJeff Layton 		inode_set_ctime_current(t_inode);
4078a9063ab9STao Ma 
407910fc3a18SJeff Layton 		di->i_ctime = cpu_to_le64(inode_get_ctime_sec(t_inode));
408010fc3a18SJeff Layton 		di->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(t_inode));
4081a9063ab9STao Ma 
408210fc3a18SJeff Layton 		inode_set_mtime_to_ts(t_inode, inode_get_mtime(s_inode));
4083a9063ab9STao Ma 		di->i_mtime = s_di->i_mtime;
4084a9063ab9STao Ma 		di->i_mtime_nsec = s_di->i_mtime_nsec;
40850fe9b66cSTao Ma 	}
4086a9063ab9STao Ma 
4087a9063ab9STao Ma 	ocfs2_journal_dirty(handle, t_bh);
4088a9063ab9STao Ma 
4089a9063ab9STao Ma out_commit:
4090a9063ab9STao Ma 	ocfs2_commit_trans(OCFS2_SB(t_inode->i_sb), handle);
4091a9063ab9STao Ma 	return ret;
4092a9063ab9STao Ma }
4093a9063ab9STao Ma 
ocfs2_create_reflink_node(struct inode * s_inode,struct buffer_head * s_bh,struct inode * t_inode,struct buffer_head * t_bh,bool preserve)4094110a045aSTao Ma static int ocfs2_create_reflink_node(struct inode *s_inode,
4095110a045aSTao Ma 				     struct buffer_head *s_bh,
4096110a045aSTao Ma 				     struct inode *t_inode,
40970fe9b66cSTao Ma 				     struct buffer_head *t_bh,
40980fe9b66cSTao Ma 				     bool preserve)
4099110a045aSTao Ma {
4100110a045aSTao Ma 	int ret;
4101110a045aSTao Ma 	struct buffer_head *ref_root_bh = NULL;
4102110a045aSTao Ma 	struct ocfs2_cached_dealloc_ctxt dealloc;
4103110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb);
4104110a045aSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)s_bh->b_data;
4105110a045aSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
4106110a045aSTao Ma 
4107110a045aSTao Ma 	ocfs2_init_dealloc_ctxt(&dealloc);
4108110a045aSTao Ma 
4109110a045aSTao Ma 	ret = ocfs2_set_refcount_tree(t_inode, t_bh,
4110110a045aSTao Ma 				      le64_to_cpu(di->i_refcount_loc));
4111110a045aSTao Ma 	if (ret) {
4112110a045aSTao Ma 		mlog_errno(ret);
4113110a045aSTao Ma 		goto out;
4114110a045aSTao Ma 	}
4115110a045aSTao Ma 
41162f48d593STao Ma 	if (OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
41172f48d593STao Ma 		ret = ocfs2_duplicate_inline_data(s_inode, s_bh,
41182f48d593STao Ma 						  t_inode, t_bh);
41192f48d593STao Ma 		if (ret)
41202f48d593STao Ma 			mlog_errno(ret);
41212f48d593STao Ma 		goto out;
41222f48d593STao Ma 	}
41232f48d593STao Ma 
4124110a045aSTao Ma 	ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
4125110a045aSTao Ma 				       1, &ref_tree, &ref_root_bh);
4126110a045aSTao Ma 	if (ret) {
4127110a045aSTao Ma 		mlog_errno(ret);
4128110a045aSTao Ma 		goto out;
4129110a045aSTao Ma 	}
4130110a045aSTao Ma 
4131110a045aSTao Ma 	ret = ocfs2_duplicate_extent_list(s_inode, t_inode, t_bh,
4132110a045aSTao Ma 					  &ref_tree->rf_ci, ref_root_bh,
4133110a045aSTao Ma 					  &dealloc);
4134a9063ab9STao Ma 	if (ret) {
4135a9063ab9STao Ma 		mlog_errno(ret);
4136a9063ab9STao Ma 		goto out_unlock_refcount;
4137a9063ab9STao Ma 	}
4138a9063ab9STao Ma 
4139a9063ab9STao Ma out_unlock_refcount:
4140110a045aSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
4141110a045aSTao Ma 	brelse(ref_root_bh);
4142110a045aSTao Ma out:
4143110a045aSTao Ma 	if (ocfs2_dealloc_has_cluster(&dealloc)) {
4144110a045aSTao Ma 		ocfs2_schedule_truncate_log_flush(osb, 1);
4145110a045aSTao Ma 		ocfs2_run_deallocs(osb, &dealloc);
4146110a045aSTao Ma 	}
4147110a045aSTao Ma 
4148110a045aSTao Ma 	return ret;
4149110a045aSTao Ma }
415009bf27a0STao Ma 
__ocfs2_reflink(struct dentry * old_dentry,struct buffer_head * old_bh,struct inode * new_inode,bool preserve)415109bf27a0STao Ma static int __ocfs2_reflink(struct dentry *old_dentry,
415209bf27a0STao Ma 			   struct buffer_head *old_bh,
415309bf27a0STao Ma 			   struct inode *new_inode,
415409bf27a0STao Ma 			   bool preserve)
415509bf27a0STao Ma {
415609bf27a0STao Ma 	int ret;
41572b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
415809bf27a0STao Ma 	struct buffer_head *new_bh = NULL;
4159*637c00e0SGautham Ananthakrishna 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
416009bf27a0STao Ma 
4161*637c00e0SGautham Ananthakrishna 	if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
41625693486bSJoel Becker 		ret = -EINVAL;
41635693486bSJoel Becker 		mlog_errno(ret);
41645693486bSJoel Becker 		goto out;
41655693486bSJoel Becker 	}
41665693486bSJoel Becker 
416709bf27a0STao Ma 	ret = filemap_fdatawrite(inode->i_mapping);
416809bf27a0STao Ma 	if (ret) {
416909bf27a0STao Ma 		mlog_errno(ret);
417009bf27a0STao Ma 		goto out;
417109bf27a0STao Ma 	}
417209bf27a0STao Ma 
417309bf27a0STao Ma 	ret = ocfs2_attach_refcount_tree(inode, old_bh);
417409bf27a0STao Ma 	if (ret) {
417509bf27a0STao Ma 		mlog_errno(ret);
417609bf27a0STao Ma 		goto out;
417709bf27a0STao Ma 	}
417809bf27a0STao Ma 
41795955102cSAl Viro 	inode_lock_nested(new_inode, I_MUTEX_CHILD);
418007eaac94STao Ma 	ret = ocfs2_inode_lock_nested(new_inode, &new_bh, 1,
418107eaac94STao Ma 				      OI_LS_REFLINK_TARGET);
418209bf27a0STao Ma 	if (ret) {
418309bf27a0STao Ma 		mlog_errno(ret);
418409bf27a0STao Ma 		goto out_unlock;
418509bf27a0STao Ma 	}
418609bf27a0STao Ma 
4187*637c00e0SGautham Ananthakrishna 	if ((oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) &&
4188*637c00e0SGautham Ananthakrishna 	    (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL)) {
4189*637c00e0SGautham Ananthakrishna 		/*
4190*637c00e0SGautham Ananthakrishna 		 * Adjust extent record count to reserve space for extended attribute.
4191*637c00e0SGautham Ananthakrishna 		 * Inline data count had been adjusted in ocfs2_duplicate_inline_data().
4192*637c00e0SGautham Ananthakrishna 		 */
4193*637c00e0SGautham Ananthakrishna 		struct ocfs2_inode_info *new_oi = OCFS2_I(new_inode);
4194*637c00e0SGautham Ananthakrishna 
4195*637c00e0SGautham Ananthakrishna 		if (!(new_oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) &&
4196*637c00e0SGautham Ananthakrishna 		    !(ocfs2_inode_is_fast_symlink(new_inode))) {
4197*637c00e0SGautham Ananthakrishna 			struct ocfs2_dinode *new_di = (struct ocfs2_dinode *)new_bh->b_data;
4198*637c00e0SGautham Ananthakrishna 			struct ocfs2_dinode *old_di = (struct ocfs2_dinode *)old_bh->b_data;
4199*637c00e0SGautham Ananthakrishna 			struct ocfs2_extent_list *el = &new_di->id2.i_list;
4200*637c00e0SGautham Ananthakrishna 			int inline_size = le16_to_cpu(old_di->i_xattr_inline_size);
4201*637c00e0SGautham Ananthakrishna 
4202*637c00e0SGautham Ananthakrishna 			le16_add_cpu(&el->l_count, -(inline_size /
4203*637c00e0SGautham Ananthakrishna 					sizeof(struct ocfs2_extent_rec)));
4204*637c00e0SGautham Ananthakrishna 		}
4205*637c00e0SGautham Ananthakrishna 	}
4206*637c00e0SGautham Ananthakrishna 
420709bf27a0STao Ma 	ret = ocfs2_create_reflink_node(inode, old_bh,
420809bf27a0STao Ma 					new_inode, new_bh, preserve);
420909bf27a0STao Ma 	if (ret) {
421009bf27a0STao Ma 		mlog_errno(ret);
421109bf27a0STao Ma 		goto inode_unlock;
421209bf27a0STao Ma 	}
421309bf27a0STao Ma 
4214*637c00e0SGautham Ananthakrishna 	if (oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) {
421509bf27a0STao Ma 		ret = ocfs2_reflink_xattrs(inode, old_bh,
421609bf27a0STao Ma 					   new_inode, new_bh,
421709bf27a0STao Ma 					   preserve);
421887f4b1bbSTao Ma 		if (ret) {
421987f4b1bbSTao Ma 			mlog_errno(ret);
422087f4b1bbSTao Ma 			goto inode_unlock;
422187f4b1bbSTao Ma 		}
422287f4b1bbSTao Ma 	}
422387f4b1bbSTao Ma 
422487f4b1bbSTao Ma 	ret = ocfs2_complete_reflink(inode, old_bh,
422587f4b1bbSTao Ma 				     new_inode, new_bh, preserve);
422609bf27a0STao Ma 	if (ret)
422709bf27a0STao Ma 		mlog_errno(ret);
422887f4b1bbSTao Ma 
422909bf27a0STao Ma inode_unlock:
423009bf27a0STao Ma 	ocfs2_inode_unlock(new_inode, 1);
423109bf27a0STao Ma 	brelse(new_bh);
423209bf27a0STao Ma out_unlock:
42335955102cSAl Viro 	inode_unlock(new_inode);
423409bf27a0STao Ma out:
423509bf27a0STao Ma 	if (!ret) {
423609bf27a0STao Ma 		ret = filemap_fdatawait(inode->i_mapping);
423709bf27a0STao Ma 		if (ret)
423809bf27a0STao Ma 			mlog_errno(ret);
423909bf27a0STao Ma 	}
424009bf27a0STao Ma 	return ret;
424109bf27a0STao Ma }
424209bf27a0STao Ma 
ocfs2_reflink(struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry,bool preserve)424309bf27a0STao Ma static int ocfs2_reflink(struct dentry *old_dentry, struct inode *dir,
424409bf27a0STao Ma 			 struct dentry *new_dentry, bool preserve)
424509bf27a0STao Ma {
4246e4383029SAshish Samant 	int error, had_lock;
42472b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
424809bf27a0STao Ma 	struct buffer_head *old_bh = NULL;
424909bf27a0STao Ma 	struct inode *new_orphan_inode = NULL;
4250e4383029SAshish Samant 	struct ocfs2_lock_holder oh;
425109bf27a0STao Ma 
425209bf27a0STao Ma 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)))
425309bf27a0STao Ma 		return -EOPNOTSUPP;
425409bf27a0STao Ma 
4255702e5bc6SChristoph Hellwig 
4256c25a1e06SJunxiao Bi 	error = ocfs2_create_inode_in_orphan(dir, inode->i_mode,
425709bf27a0STao Ma 					     &new_orphan_inode);
425809bf27a0STao Ma 	if (error) {
425909bf27a0STao Ma 		mlog_errno(error);
426009bf27a0STao Ma 		goto out;
426109bf27a0STao Ma 	}
426209bf27a0STao Ma 
42638a8ad1c2SWengang Wang 	error = ocfs2_rw_lock(inode, 1);
42648a8ad1c2SWengang Wang 	if (error) {
42658a8ad1c2SWengang Wang 		mlog_errno(error);
42668a8ad1c2SWengang Wang 		goto out;
42678a8ad1c2SWengang Wang 	}
42688a8ad1c2SWengang Wang 
426909bf27a0STao Ma 	error = ocfs2_inode_lock(inode, &old_bh, 1);
427009bf27a0STao Ma 	if (error) {
427109bf27a0STao Ma 		mlog_errno(error);
42728a8ad1c2SWengang Wang 		ocfs2_rw_unlock(inode, 1);
427309bf27a0STao Ma 		goto out;
427409bf27a0STao Ma 	}
427509bf27a0STao Ma 
427609bf27a0STao Ma 	down_write(&OCFS2_I(inode)->ip_xattr_sem);
427709bf27a0STao Ma 	down_write(&OCFS2_I(inode)->ip_alloc_sem);
427809bf27a0STao Ma 	error = __ocfs2_reflink(old_dentry, old_bh,
427909bf27a0STao Ma 				new_orphan_inode, preserve);
428009bf27a0STao Ma 	up_write(&OCFS2_I(inode)->ip_alloc_sem);
428109bf27a0STao Ma 	up_write(&OCFS2_I(inode)->ip_xattr_sem);
428209bf27a0STao Ma 
428309bf27a0STao Ma 	ocfs2_inode_unlock(inode, 1);
42848a8ad1c2SWengang Wang 	ocfs2_rw_unlock(inode, 1);
428509bf27a0STao Ma 	brelse(old_bh);
428609bf27a0STao Ma 
428709bf27a0STao Ma 	if (error) {
428809bf27a0STao Ma 		mlog_errno(error);
428909bf27a0STao Ma 		goto out;
429009bf27a0STao Ma 	}
429109bf27a0STao Ma 
4292e4383029SAshish Samant 	had_lock = ocfs2_inode_lock_tracker(new_orphan_inode, NULL, 1,
4293e4383029SAshish Samant 					    &oh);
4294e4383029SAshish Samant 	if (had_lock < 0) {
4295e4383029SAshish Samant 		error = had_lock;
4296e4383029SAshish Samant 		mlog_errno(error);
4297e4383029SAshish Samant 		goto out;
4298e4383029SAshish Samant 	}
4299e4383029SAshish Samant 
430009bf27a0STao Ma 	/* If the security isn't preserved, we need to re-initialize them. */
430109bf27a0STao Ma 	if (!preserve) {
43022a7dba39SEric Paris 		error = ocfs2_init_security_and_acl(dir, new_orphan_inode,
4303c25a1e06SJunxiao Bi 						    &new_dentry->d_name);
430409bf27a0STao Ma 		if (error)
430509bf27a0STao Ma 			mlog_errno(error);
430609bf27a0STao Ma 	}
430709bf27a0STao Ma 	if (!error) {
430809bf27a0STao Ma 		error = ocfs2_mv_orphaned_inode_to_new(dir, new_orphan_inode,
430909bf27a0STao Ma 						       new_dentry);
431009bf27a0STao Ma 		if (error)
431109bf27a0STao Ma 			mlog_errno(error);
431209bf27a0STao Ma 	}
4313e4383029SAshish Samant 	ocfs2_inode_unlock_tracker(new_orphan_inode, 1, &oh, had_lock);
431409bf27a0STao Ma 
4315e4383029SAshish Samant out:
431609bf27a0STao Ma 	if (new_orphan_inode) {
431709bf27a0STao Ma 		/*
431809bf27a0STao Ma 		 * We need to open_unlock the inode no matter whether we
431909bf27a0STao Ma 		 * succeed or not, so that other nodes can delete it later.
432009bf27a0STao Ma 		 */
432109bf27a0STao Ma 		ocfs2_open_unlock(new_orphan_inode);
432209bf27a0STao Ma 		if (error)
432309bf27a0STao Ma 			iput(new_orphan_inode);
432409bf27a0STao Ma 	}
432509bf27a0STao Ma 
432609bf27a0STao Ma 	return error;
432709bf27a0STao Ma }
4328bd50873dSTao Ma 
4329bd50873dSTao Ma /*
4330bd50873dSTao Ma  * Below here are the bits used by OCFS2_IOC_REFLINK() to fake
4331bd50873dSTao Ma  * sys_reflink().  This will go away when vfs_reflink() exists in
4332bd50873dSTao Ma  * fs/namei.c.
4333bd50873dSTao Ma  */
4334bd50873dSTao Ma 
4335bd50873dSTao Ma /* copied from may_create in VFS. */
ocfs2_may_create(struct inode * dir,struct dentry * child)4336bd50873dSTao Ma static inline int ocfs2_may_create(struct inode *dir, struct dentry *child)
4337bd50873dSTao Ma {
43382b0143b5SDavid Howells 	if (d_really_is_positive(child))
4339bd50873dSTao Ma 		return -EEXIST;
4340bd50873dSTao Ma 	if (IS_DEADDIR(dir))
4341bd50873dSTao Ma 		return -ENOENT;
43424609e1f1SChristian Brauner 	return inode_permission(&nop_mnt_idmap, dir, MAY_WRITE | MAY_EXEC);
4343bd50873dSTao Ma }
4344bd50873dSTao Ma 
4345bd50873dSTao Ma /**
4346bd50873dSTao Ma  * ocfs2_vfs_reflink - Create a reference-counted link
4347bd50873dSTao Ma  *
4348bd50873dSTao Ma  * @old_dentry:        source dentry + inode
4349bd50873dSTao Ma  * @dir:       directory to create the target
4350bd50873dSTao Ma  * @new_dentry:        target dentry
4351bd50873dSTao Ma  * @preserve:  if true, preserve all file attributes
4352bd50873dSTao Ma  */
ocfs2_vfs_reflink(struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry,bool preserve)435312d4cec9STao Ma static int ocfs2_vfs_reflink(struct dentry *old_dentry, struct inode *dir,
4354bd50873dSTao Ma 			     struct dentry *new_dentry, bool preserve)
4355bd50873dSTao Ma {
43562b0143b5SDavid Howells 	struct inode *inode = d_inode(old_dentry);
4357bd50873dSTao Ma 	int error;
4358bd50873dSTao Ma 
4359bd50873dSTao Ma 	if (!inode)
4360bd50873dSTao Ma 		return -ENOENT;
4361bd50873dSTao Ma 
4362bd50873dSTao Ma 	error = ocfs2_may_create(dir, new_dentry);
4363bd50873dSTao Ma 	if (error)
4364bd50873dSTao Ma 		return error;
4365bd50873dSTao Ma 
4366bd50873dSTao Ma 	if (dir->i_sb != inode->i_sb)
4367bd50873dSTao Ma 		return -EXDEV;
4368bd50873dSTao Ma 
4369bd50873dSTao Ma 	/*
4370bd50873dSTao Ma 	 * A reflink to an append-only or immutable file cannot be created.
4371bd50873dSTao Ma 	 */
4372bd50873dSTao Ma 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4373bd50873dSTao Ma 		return -EPERM;
4374bd50873dSTao Ma 
4375bd50873dSTao Ma 	/* Only regular files can be reflinked. */
4376bd50873dSTao Ma 	if (!S_ISREG(inode->i_mode))
4377bd50873dSTao Ma 		return -EPERM;
4378bd50873dSTao Ma 
4379bd50873dSTao Ma 	/*
4380bd50873dSTao Ma 	 * If the caller wants to preserve ownership, they require the
4381bd50873dSTao Ma 	 * rights to do so.
4382bd50873dSTao Ma 	 */
4383bd50873dSTao Ma 	if (preserve) {
4384488c8ef0SEric W. Biederman 		if (!uid_eq(current_fsuid(), inode->i_uid) && !capable(CAP_CHOWN))
4385bd50873dSTao Ma 			return -EPERM;
4386bd50873dSTao Ma 		if (!in_group_p(inode->i_gid) && !capable(CAP_CHOWN))
4387bd50873dSTao Ma 			return -EPERM;
4388bd50873dSTao Ma 	}
4389bd50873dSTao Ma 
4390bd50873dSTao Ma 	/*
4391bd50873dSTao Ma 	 * If the caller is modifying any aspect of the attributes, they
4392bd50873dSTao Ma 	 * are not creating a snapshot.  They need read permission on the
4393bd50873dSTao Ma 	 * file.
4394bd50873dSTao Ma 	 */
4395bd50873dSTao Ma 	if (!preserve) {
43964609e1f1SChristian Brauner 		error = inode_permission(&nop_mnt_idmap, inode, MAY_READ);
4397bd50873dSTao Ma 		if (error)
4398bd50873dSTao Ma 			return error;
4399bd50873dSTao Ma 	}
4400bd50873dSTao Ma 
44015955102cSAl Viro 	inode_lock(inode);
44029c89fe0aSJan Kara 	error = dquot_initialize(dir);
44039c89fe0aSJan Kara 	if (!error)
4404bd50873dSTao Ma 		error = ocfs2_reflink(old_dentry, dir, new_dentry, preserve);
44055955102cSAl Viro 	inode_unlock(inode);
4406bd50873dSTao Ma 	if (!error)
4407bd50873dSTao Ma 		fsnotify_create(dir, new_dentry);
4408bd50873dSTao Ma 	return error;
4409bd50873dSTao Ma }
4410bd50873dSTao Ma /*
4411bd50873dSTao Ma  * Most codes are copied from sys_linkat.
4412bd50873dSTao Ma  */
ocfs2_reflink_ioctl(struct inode * inode,const char __user * oldname,const char __user * newname,bool preserve)4413bd50873dSTao Ma int ocfs2_reflink_ioctl(struct inode *inode,
4414bd50873dSTao Ma 			const char __user *oldname,
4415bd50873dSTao Ma 			const char __user *newname,
4416bd50873dSTao Ma 			bool preserve)
4417bd50873dSTao Ma {
4418bd50873dSTao Ma 	struct dentry *new_dentry;
4419dae6ad8fSAl Viro 	struct path old_path, new_path;
4420bd50873dSTao Ma 	int error;
4421bd50873dSTao Ma 
4422bd50873dSTao Ma 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)))
4423bd50873dSTao Ma 		return -EOPNOTSUPP;
4424bd50873dSTao Ma 
4425bd50873dSTao Ma 	error = user_path_at(AT_FDCWD, oldname, 0, &old_path);
4426bd50873dSTao Ma 	if (error) {
4427bd50873dSTao Ma 		mlog_errno(error);
4428bd50873dSTao Ma 		return error;
4429bd50873dSTao Ma 	}
4430bd50873dSTao Ma 
4431dae6ad8fSAl Viro 	new_dentry = user_path_create(AT_FDCWD, newname, &new_path, 0);
4432dae6ad8fSAl Viro 	error = PTR_ERR(new_dentry);
4433dae6ad8fSAl Viro 	if (IS_ERR(new_dentry)) {
4434bd50873dSTao Ma 		mlog_errno(error);
4435bd50873dSTao Ma 		goto out;
4436bd50873dSTao Ma 	}
4437bd50873dSTao Ma 
4438bd50873dSTao Ma 	error = -EXDEV;
4439dae6ad8fSAl Viro 	if (old_path.mnt != new_path.mnt) {
4440bd50873dSTao Ma 		mlog_errno(error);
4441dae6ad8fSAl Viro 		goto out_dput;
4442bd50873dSTao Ma 	}
4443bd50873dSTao Ma 
4444bd50873dSTao Ma 	error = ocfs2_vfs_reflink(old_path.dentry,
44452b0143b5SDavid Howells 				  d_inode(new_path.dentry),
4446bd50873dSTao Ma 				  new_dentry, preserve);
4447bd50873dSTao Ma out_dput:
4448921a1650SAl Viro 	done_path_create(&new_path, new_dentry);
4449bd50873dSTao Ma out:
4450bd50873dSTao Ma 	path_put(&old_path);
4451bd50873dSTao Ma 
4452bd50873dSTao Ma 	return error;
4453bd50873dSTao Ma }
445429ac8e85SDarrick J. Wong 
445529ac8e85SDarrick J. Wong /* Update destination inode size, if necessary. */
ocfs2_reflink_update_dest(struct inode * dest,struct buffer_head * d_bh,loff_t newlen)445665f098e9SDarrick J. Wong int ocfs2_reflink_update_dest(struct inode *dest,
445729ac8e85SDarrick J. Wong 			      struct buffer_head *d_bh,
445829ac8e85SDarrick J. Wong 			      loff_t newlen)
445929ac8e85SDarrick J. Wong {
446029ac8e85SDarrick J. Wong 	handle_t *handle;
446129ac8e85SDarrick J. Wong 	int ret;
446229ac8e85SDarrick J. Wong 
446329ac8e85SDarrick J. Wong 	dest->i_blocks = ocfs2_inode_sector_count(dest);
446429ac8e85SDarrick J. Wong 
446529ac8e85SDarrick J. Wong 	if (newlen <= i_size_read(dest))
446629ac8e85SDarrick J. Wong 		return 0;
446729ac8e85SDarrick J. Wong 
446829ac8e85SDarrick J. Wong 	handle = ocfs2_start_trans(OCFS2_SB(dest->i_sb),
446929ac8e85SDarrick J. Wong 				   OCFS2_INODE_UPDATE_CREDITS);
447029ac8e85SDarrick J. Wong 	if (IS_ERR(handle)) {
447129ac8e85SDarrick J. Wong 		ret = PTR_ERR(handle);
447229ac8e85SDarrick J. Wong 		mlog_errno(ret);
447329ac8e85SDarrick J. Wong 		return ret;
447429ac8e85SDarrick J. Wong 	}
447529ac8e85SDarrick J. Wong 
447629ac8e85SDarrick J. Wong 	/* Extend i_size if needed. */
447729ac8e85SDarrick J. Wong 	spin_lock(&OCFS2_I(dest)->ip_lock);
447829ac8e85SDarrick J. Wong 	if (newlen > i_size_read(dest))
447929ac8e85SDarrick J. Wong 		i_size_write(dest, newlen);
448029ac8e85SDarrick J. Wong 	spin_unlock(&OCFS2_I(dest)->ip_lock);
448110fc3a18SJeff Layton 	inode_set_mtime_to_ts(dest, inode_set_ctime_current(dest));
448229ac8e85SDarrick J. Wong 
448329ac8e85SDarrick J. Wong 	ret = ocfs2_mark_inode_dirty(handle, dest, d_bh);
448429ac8e85SDarrick J. Wong 	if (ret) {
448529ac8e85SDarrick J. Wong 		mlog_errno(ret);
448629ac8e85SDarrick J. Wong 		goto out_commit;
448729ac8e85SDarrick J. Wong 	}
448829ac8e85SDarrick J. Wong 
448929ac8e85SDarrick J. Wong out_commit:
449029ac8e85SDarrick J. Wong 	ocfs2_commit_trans(OCFS2_SB(dest->i_sb), handle);
449129ac8e85SDarrick J. Wong 	return ret;
449229ac8e85SDarrick J. Wong }
449329ac8e85SDarrick J. Wong 
449429ac8e85SDarrick J. Wong /* Remap the range pos_in:len in s_inode to pos_out:len in t_inode. */
ocfs2_reflink_remap_extent(struct inode * s_inode,struct buffer_head * s_bh,loff_t pos_in,struct inode * t_inode,struct buffer_head * t_bh,loff_t pos_out,loff_t len,struct ocfs2_cached_dealloc_ctxt * dealloc)4495900611a1SDarrick J. Wong static loff_t ocfs2_reflink_remap_extent(struct inode *s_inode,
449629ac8e85SDarrick J. Wong 					 struct buffer_head *s_bh,
449729ac8e85SDarrick J. Wong 					 loff_t pos_in,
449829ac8e85SDarrick J. Wong 					 struct inode *t_inode,
449929ac8e85SDarrick J. Wong 					 struct buffer_head *t_bh,
450029ac8e85SDarrick J. Wong 					 loff_t pos_out,
450129ac8e85SDarrick J. Wong 					 loff_t len,
450229ac8e85SDarrick J. Wong 					 struct ocfs2_cached_dealloc_ctxt *dealloc)
450329ac8e85SDarrick J. Wong {
450429ac8e85SDarrick J. Wong 	struct ocfs2_extent_tree s_et;
450529ac8e85SDarrick J. Wong 	struct ocfs2_extent_tree t_et;
450629ac8e85SDarrick J. Wong 	struct ocfs2_dinode *dis;
450729ac8e85SDarrick J. Wong 	struct buffer_head *ref_root_bh = NULL;
450829ac8e85SDarrick J. Wong 	struct ocfs2_refcount_tree *ref_tree;
450929ac8e85SDarrick J. Wong 	struct ocfs2_super *osb;
4510900611a1SDarrick J. Wong 	loff_t remapped_bytes = 0;
451129ac8e85SDarrick J. Wong 	loff_t pstart, plen;
4512900611a1SDarrick J. Wong 	u32 p_cluster, num_clusters, slast, spos, tpos, remapped_clus = 0;
451329ac8e85SDarrick J. Wong 	unsigned int ext_flags;
451429ac8e85SDarrick J. Wong 	int ret = 0;
451529ac8e85SDarrick J. Wong 
451629ac8e85SDarrick J. Wong 	osb = OCFS2_SB(s_inode->i_sb);
451729ac8e85SDarrick J. Wong 	dis = (struct ocfs2_dinode *)s_bh->b_data;
451829ac8e85SDarrick J. Wong 	ocfs2_init_dinode_extent_tree(&s_et, INODE_CACHE(s_inode), s_bh);
451929ac8e85SDarrick J. Wong 	ocfs2_init_dinode_extent_tree(&t_et, INODE_CACHE(t_inode), t_bh);
452029ac8e85SDarrick J. Wong 
452129ac8e85SDarrick J. Wong 	spos = ocfs2_bytes_to_clusters(s_inode->i_sb, pos_in);
452229ac8e85SDarrick J. Wong 	tpos = ocfs2_bytes_to_clusters(t_inode->i_sb, pos_out);
452329ac8e85SDarrick J. Wong 	slast = ocfs2_clusters_for_bytes(s_inode->i_sb, pos_in + len);
452429ac8e85SDarrick J. Wong 
452529ac8e85SDarrick J. Wong 	while (spos < slast) {
452629ac8e85SDarrick J. Wong 		if (fatal_signal_pending(current)) {
452729ac8e85SDarrick J. Wong 			ret = -EINTR;
452829ac8e85SDarrick J. Wong 			goto out;
452929ac8e85SDarrick J. Wong 		}
453029ac8e85SDarrick J. Wong 
453129ac8e85SDarrick J. Wong 		/* Look up the extent. */
453229ac8e85SDarrick J. Wong 		ret = ocfs2_get_clusters(s_inode, spos, &p_cluster,
453329ac8e85SDarrick J. Wong 					 &num_clusters, &ext_flags);
453429ac8e85SDarrick J. Wong 		if (ret) {
453529ac8e85SDarrick J. Wong 			mlog_errno(ret);
453629ac8e85SDarrick J. Wong 			goto out;
453729ac8e85SDarrick J. Wong 		}
453829ac8e85SDarrick J. Wong 
453929ac8e85SDarrick J. Wong 		num_clusters = min_t(u32, num_clusters, slast - spos);
454029ac8e85SDarrick J. Wong 
454129ac8e85SDarrick J. Wong 		/* Punch out the dest range. */
454229ac8e85SDarrick J. Wong 		pstart = ocfs2_clusters_to_bytes(t_inode->i_sb, tpos);
454329ac8e85SDarrick J. Wong 		plen = ocfs2_clusters_to_bytes(t_inode->i_sb, num_clusters);
454429ac8e85SDarrick J. Wong 		ret = ocfs2_remove_inode_range(t_inode, t_bh, pstart, plen);
454529ac8e85SDarrick J. Wong 		if (ret) {
454629ac8e85SDarrick J. Wong 			mlog_errno(ret);
454729ac8e85SDarrick J. Wong 			goto out;
454829ac8e85SDarrick J. Wong 		}
454929ac8e85SDarrick J. Wong 
455029ac8e85SDarrick J. Wong 		if (p_cluster == 0)
455129ac8e85SDarrick J. Wong 			goto next_loop;
455229ac8e85SDarrick J. Wong 
455329ac8e85SDarrick J. Wong 		/* Lock the refcount btree... */
455429ac8e85SDarrick J. Wong 		ret = ocfs2_lock_refcount_tree(osb,
455529ac8e85SDarrick J. Wong 					       le64_to_cpu(dis->i_refcount_loc),
455629ac8e85SDarrick J. Wong 					       1, &ref_tree, &ref_root_bh);
455729ac8e85SDarrick J. Wong 		if (ret) {
455829ac8e85SDarrick J. Wong 			mlog_errno(ret);
455929ac8e85SDarrick J. Wong 			goto out;
456029ac8e85SDarrick J. Wong 		}
456129ac8e85SDarrick J. Wong 
456229ac8e85SDarrick J. Wong 		/* Mark s_inode's extent as refcounted. */
456329ac8e85SDarrick J. Wong 		if (!(ext_flags & OCFS2_EXT_REFCOUNTED)) {
456429ac8e85SDarrick J. Wong 			ret = ocfs2_add_refcount_flag(s_inode, &s_et,
456529ac8e85SDarrick J. Wong 						      &ref_tree->rf_ci,
456629ac8e85SDarrick J. Wong 						      ref_root_bh, spos,
456729ac8e85SDarrick J. Wong 						      p_cluster, num_clusters,
456829ac8e85SDarrick J. Wong 						      dealloc, NULL);
456929ac8e85SDarrick J. Wong 			if (ret) {
457029ac8e85SDarrick J. Wong 				mlog_errno(ret);
457129ac8e85SDarrick J. Wong 				goto out_unlock_refcount;
457229ac8e85SDarrick J. Wong 			}
457329ac8e85SDarrick J. Wong 		}
457429ac8e85SDarrick J. Wong 
457529ac8e85SDarrick J. Wong 		/* Map in the new extent. */
457629ac8e85SDarrick J. Wong 		ext_flags |= OCFS2_EXT_REFCOUNTED;
457729ac8e85SDarrick J. Wong 		ret = ocfs2_add_refcounted_extent(t_inode, &t_et,
457829ac8e85SDarrick J. Wong 						  &ref_tree->rf_ci,
457929ac8e85SDarrick J. Wong 						  ref_root_bh,
458029ac8e85SDarrick J. Wong 						  tpos, p_cluster,
458129ac8e85SDarrick J. Wong 						  num_clusters,
458229ac8e85SDarrick J. Wong 						  ext_flags,
458329ac8e85SDarrick J. Wong 						  dealloc);
458429ac8e85SDarrick J. Wong 		if (ret) {
458529ac8e85SDarrick J. Wong 			mlog_errno(ret);
458629ac8e85SDarrick J. Wong 			goto out_unlock_refcount;
458729ac8e85SDarrick J. Wong 		}
458829ac8e85SDarrick J. Wong 
458929ac8e85SDarrick J. Wong 		ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
459029ac8e85SDarrick J. Wong 		brelse(ref_root_bh);
459129ac8e85SDarrick J. Wong next_loop:
459229ac8e85SDarrick J. Wong 		spos += num_clusters;
459329ac8e85SDarrick J. Wong 		tpos += num_clusters;
4594900611a1SDarrick J. Wong 		remapped_clus += num_clusters;
459529ac8e85SDarrick J. Wong 	}
459629ac8e85SDarrick J. Wong 
4597900611a1SDarrick J. Wong 	goto out;
459829ac8e85SDarrick J. Wong out_unlock_refcount:
459929ac8e85SDarrick J. Wong 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
460029ac8e85SDarrick J. Wong 	brelse(ref_root_bh);
4601900611a1SDarrick J. Wong out:
4602900611a1SDarrick J. Wong 	remapped_bytes = ocfs2_clusters_to_bytes(t_inode->i_sb, remapped_clus);
4603900611a1SDarrick J. Wong 	remapped_bytes = min_t(loff_t, len, remapped_bytes);
4604900611a1SDarrick J. Wong 
4605900611a1SDarrick J. Wong 	return remapped_bytes > 0 ? remapped_bytes : ret;
460629ac8e85SDarrick J. Wong }
460729ac8e85SDarrick J. Wong 
460829ac8e85SDarrick J. Wong /* Set up refcount tree and remap s_inode to t_inode. */
ocfs2_reflink_remap_blocks(struct inode * s_inode,struct buffer_head * s_bh,loff_t pos_in,struct inode * t_inode,struct buffer_head * t_bh,loff_t pos_out,loff_t len)460965f098e9SDarrick J. Wong loff_t ocfs2_reflink_remap_blocks(struct inode *s_inode,
461029ac8e85SDarrick J. Wong 				  struct buffer_head *s_bh,
461129ac8e85SDarrick J. Wong 				  loff_t pos_in,
461229ac8e85SDarrick J. Wong 				  struct inode *t_inode,
461329ac8e85SDarrick J. Wong 				  struct buffer_head *t_bh,
461429ac8e85SDarrick J. Wong 				  loff_t pos_out,
461529ac8e85SDarrick J. Wong 				  loff_t len)
461629ac8e85SDarrick J. Wong {
461729ac8e85SDarrick J. Wong 	struct ocfs2_cached_dealloc_ctxt dealloc;
461829ac8e85SDarrick J. Wong 	struct ocfs2_super *osb;
461929ac8e85SDarrick J. Wong 	struct ocfs2_dinode *dis;
462029ac8e85SDarrick J. Wong 	struct ocfs2_dinode *dit;
4621900611a1SDarrick J. Wong 	loff_t ret;
462229ac8e85SDarrick J. Wong 
462329ac8e85SDarrick J. Wong 	osb = OCFS2_SB(s_inode->i_sb);
462429ac8e85SDarrick J. Wong 	dis = (struct ocfs2_dinode *)s_bh->b_data;
462529ac8e85SDarrick J. Wong 	dit = (struct ocfs2_dinode *)t_bh->b_data;
462629ac8e85SDarrick J. Wong 	ocfs2_init_dealloc_ctxt(&dealloc);
462729ac8e85SDarrick J. Wong 
462829ac8e85SDarrick J. Wong 	/*
462929ac8e85SDarrick J. Wong 	 * If we're reflinking the entire file and the source is inline
463029ac8e85SDarrick J. Wong 	 * data, just copy the contents.
463129ac8e85SDarrick J. Wong 	 */
463229ac8e85SDarrick J. Wong 	if (pos_in == pos_out && pos_in == 0 && len == i_size_read(s_inode) &&
463329ac8e85SDarrick J. Wong 	    i_size_read(t_inode) <= len &&
463429ac8e85SDarrick J. Wong 	    (OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)) {
463529ac8e85SDarrick J. Wong 		ret = ocfs2_duplicate_inline_data(s_inode, s_bh, t_inode, t_bh);
463629ac8e85SDarrick J. Wong 		if (ret)
463729ac8e85SDarrick J. Wong 			mlog_errno(ret);
463829ac8e85SDarrick J. Wong 		goto out;
463929ac8e85SDarrick J. Wong 	}
464029ac8e85SDarrick J. Wong 
464129ac8e85SDarrick J. Wong 	/*
464229ac8e85SDarrick J. Wong 	 * If both inodes belong to two different refcount groups then
464329ac8e85SDarrick J. Wong 	 * forget it because we don't know how (or want) to go merging
464429ac8e85SDarrick J. Wong 	 * refcount trees.
464529ac8e85SDarrick J. Wong 	 */
464629ac8e85SDarrick J. Wong 	ret = -EOPNOTSUPP;
464729ac8e85SDarrick J. Wong 	if (ocfs2_is_refcount_inode(s_inode) &&
464829ac8e85SDarrick J. Wong 	    ocfs2_is_refcount_inode(t_inode) &&
464929ac8e85SDarrick J. Wong 	    le64_to_cpu(dis->i_refcount_loc) !=
465029ac8e85SDarrick J. Wong 	    le64_to_cpu(dit->i_refcount_loc))
465129ac8e85SDarrick J. Wong 		goto out;
465229ac8e85SDarrick J. Wong 
465329ac8e85SDarrick J. Wong 	/* Neither inode has a refcount tree.  Add one to s_inode. */
465429ac8e85SDarrick J. Wong 	if (!ocfs2_is_refcount_inode(s_inode) &&
465529ac8e85SDarrick J. Wong 	    !ocfs2_is_refcount_inode(t_inode)) {
465629ac8e85SDarrick J. Wong 		ret = ocfs2_create_refcount_tree(s_inode, s_bh);
465729ac8e85SDarrick J. Wong 		if (ret) {
465829ac8e85SDarrick J. Wong 			mlog_errno(ret);
465929ac8e85SDarrick J. Wong 			goto out;
466029ac8e85SDarrick J. Wong 		}
466129ac8e85SDarrick J. Wong 	}
466229ac8e85SDarrick J. Wong 
466329ac8e85SDarrick J. Wong 	/* Ensure that both inodes end up with the same refcount tree. */
466429ac8e85SDarrick J. Wong 	if (!ocfs2_is_refcount_inode(s_inode)) {
466529ac8e85SDarrick J. Wong 		ret = ocfs2_set_refcount_tree(s_inode, s_bh,
466629ac8e85SDarrick J. Wong 					      le64_to_cpu(dit->i_refcount_loc));
466729ac8e85SDarrick J. Wong 		if (ret) {
466829ac8e85SDarrick J. Wong 			mlog_errno(ret);
466929ac8e85SDarrick J. Wong 			goto out;
467029ac8e85SDarrick J. Wong 		}
467129ac8e85SDarrick J. Wong 	}
467229ac8e85SDarrick J. Wong 	if (!ocfs2_is_refcount_inode(t_inode)) {
467329ac8e85SDarrick J. Wong 		ret = ocfs2_set_refcount_tree(t_inode, t_bh,
467429ac8e85SDarrick J. Wong 					      le64_to_cpu(dis->i_refcount_loc));
467529ac8e85SDarrick J. Wong 		if (ret) {
467629ac8e85SDarrick J. Wong 			mlog_errno(ret);
467729ac8e85SDarrick J. Wong 			goto out;
467829ac8e85SDarrick J. Wong 		}
467929ac8e85SDarrick J. Wong 	}
468029ac8e85SDarrick J. Wong 
468129ac8e85SDarrick J. Wong 	/* Turn off inline data in the dest file. */
468229ac8e85SDarrick J. Wong 	if (OCFS2_I(t_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
468329ac8e85SDarrick J. Wong 		ret = ocfs2_convert_inline_data_to_extents(t_inode, t_bh);
468429ac8e85SDarrick J. Wong 		if (ret) {
468529ac8e85SDarrick J. Wong 			mlog_errno(ret);
468629ac8e85SDarrick J. Wong 			goto out;
468729ac8e85SDarrick J. Wong 		}
468829ac8e85SDarrick J. Wong 	}
468929ac8e85SDarrick J. Wong 
469029ac8e85SDarrick J. Wong 	/* Actually remap extents now. */
469129ac8e85SDarrick J. Wong 	ret = ocfs2_reflink_remap_extent(s_inode, s_bh, pos_in, t_inode, t_bh,
469229ac8e85SDarrick J. Wong 					 pos_out, len, &dealloc);
4693900611a1SDarrick J. Wong 	if (ret < 0) {
469429ac8e85SDarrick J. Wong 		mlog_errno(ret);
469529ac8e85SDarrick J. Wong 		goto out;
469629ac8e85SDarrick J. Wong 	}
469729ac8e85SDarrick J. Wong 
469829ac8e85SDarrick J. Wong out:
469929ac8e85SDarrick J. Wong 	if (ocfs2_dealloc_has_cluster(&dealloc)) {
470029ac8e85SDarrick J. Wong 		ocfs2_schedule_truncate_log_flush(osb, 1);
470129ac8e85SDarrick J. Wong 		ocfs2_run_deallocs(osb, &dealloc);
470229ac8e85SDarrick J. Wong 	}
470329ac8e85SDarrick J. Wong 
470429ac8e85SDarrick J. Wong 	return ret;
470529ac8e85SDarrick J. Wong }
470629ac8e85SDarrick J. Wong 
470729ac8e85SDarrick J. Wong /* Lock an inode and grab a bh pointing to the inode. */
ocfs2_reflink_inodes_lock(struct inode * s_inode,struct buffer_head ** bh_s,struct inode * t_inode,struct buffer_head ** bh_t)470865f098e9SDarrick J. Wong int ocfs2_reflink_inodes_lock(struct inode *s_inode,
4709e6a9467eSDarrick J. Wong 			      struct buffer_head **bh_s,
471029ac8e85SDarrick J. Wong 			      struct inode *t_inode,
4711e6a9467eSDarrick J. Wong 			      struct buffer_head **bh_t)
471229ac8e85SDarrick J. Wong {
4713e6a9467eSDarrick J. Wong 	struct inode *inode1 = s_inode;
4714e6a9467eSDarrick J. Wong 	struct inode *inode2 = t_inode;
471529ac8e85SDarrick J. Wong 	struct ocfs2_inode_info *oi1;
471629ac8e85SDarrick J. Wong 	struct ocfs2_inode_info *oi2;
4717e6a9467eSDarrick J. Wong 	struct buffer_head *bh1 = NULL;
4718e6a9467eSDarrick J. Wong 	struct buffer_head *bh2 = NULL;
471929ac8e85SDarrick J. Wong 	bool same_inode = (s_inode == t_inode);
4720e6a9467eSDarrick J. Wong 	bool need_swap = (inode1->i_ino > inode2->i_ino);
472129ac8e85SDarrick J. Wong 	int status;
472229ac8e85SDarrick J. Wong 
472329ac8e85SDarrick J. Wong 	/* First grab the VFS and rw locks. */
472429ac8e85SDarrick J. Wong 	lock_two_nondirectories(s_inode, t_inode);
4725e6a9467eSDarrick J. Wong 	if (need_swap)
472629ac8e85SDarrick J. Wong 		swap(inode1, inode2);
472729ac8e85SDarrick J. Wong 
472829ac8e85SDarrick J. Wong 	status = ocfs2_rw_lock(inode1, 1);
472929ac8e85SDarrick J. Wong 	if (status) {
473029ac8e85SDarrick J. Wong 		mlog_errno(status);
473129ac8e85SDarrick J. Wong 		goto out_i1;
473229ac8e85SDarrick J. Wong 	}
473329ac8e85SDarrick J. Wong 	if (!same_inode) {
473429ac8e85SDarrick J. Wong 		status = ocfs2_rw_lock(inode2, 1);
473529ac8e85SDarrick J. Wong 		if (status) {
473629ac8e85SDarrick J. Wong 			mlog_errno(status);
473729ac8e85SDarrick J. Wong 			goto out_i2;
473829ac8e85SDarrick J. Wong 		}
473929ac8e85SDarrick J. Wong 	}
474029ac8e85SDarrick J. Wong 
474129ac8e85SDarrick J. Wong 	/* Now go for the cluster locks */
474229ac8e85SDarrick J. Wong 	oi1 = OCFS2_I(inode1);
474329ac8e85SDarrick J. Wong 	oi2 = OCFS2_I(inode2);
474429ac8e85SDarrick J. Wong 
474529ac8e85SDarrick J. Wong 	trace_ocfs2_double_lock((unsigned long long)oi1->ip_blkno,
474629ac8e85SDarrick J. Wong 				(unsigned long long)oi2->ip_blkno);
474729ac8e85SDarrick J. Wong 
474829ac8e85SDarrick J. Wong 	/* We always want to lock the one with the lower lockid first. */
474929ac8e85SDarrick J. Wong 	if (oi1->ip_blkno > oi2->ip_blkno)
475029ac8e85SDarrick J. Wong 		mlog_errno(-ENOLCK);
475129ac8e85SDarrick J. Wong 
475229ac8e85SDarrick J. Wong 	/* lock id1 */
4753e6a9467eSDarrick J. Wong 	status = ocfs2_inode_lock_nested(inode1, &bh1, 1,
4754e6a9467eSDarrick J. Wong 					 OI_LS_REFLINK_TARGET);
475529ac8e85SDarrick J. Wong 	if (status < 0) {
475629ac8e85SDarrick J. Wong 		if (status != -ENOENT)
475729ac8e85SDarrick J. Wong 			mlog_errno(status);
475829ac8e85SDarrick J. Wong 		goto out_rw2;
475929ac8e85SDarrick J. Wong 	}
476029ac8e85SDarrick J. Wong 
476129ac8e85SDarrick J. Wong 	/* lock id2 */
476229ac8e85SDarrick J. Wong 	if (!same_inode) {
4763e6a9467eSDarrick J. Wong 		status = ocfs2_inode_lock_nested(inode2, &bh2, 1,
476429ac8e85SDarrick J. Wong 						 OI_LS_REFLINK_TARGET);
476529ac8e85SDarrick J. Wong 		if (status < 0) {
476629ac8e85SDarrick J. Wong 			if (status != -ENOENT)
476729ac8e85SDarrick J. Wong 				mlog_errno(status);
476829ac8e85SDarrick J. Wong 			goto out_cl1;
476929ac8e85SDarrick J. Wong 		}
4770e6a9467eSDarrick J. Wong 	} else {
4771e6a9467eSDarrick J. Wong 		bh2 = bh1;
4772e6a9467eSDarrick J. Wong 	}
4773e6a9467eSDarrick J. Wong 
4774e6a9467eSDarrick J. Wong 	/*
4775e6a9467eSDarrick J. Wong 	 * If we swapped inode order above, we have to swap the buffer heads
4776e6a9467eSDarrick J. Wong 	 * before passing them back to the caller.
4777e6a9467eSDarrick J. Wong 	 */
4778e6a9467eSDarrick J. Wong 	if (need_swap)
4779e6a9467eSDarrick J. Wong 		swap(bh1, bh2);
4780e6a9467eSDarrick J. Wong 	*bh_s = bh1;
4781e6a9467eSDarrick J. Wong 	*bh_t = bh2;
478229ac8e85SDarrick J. Wong 
478329ac8e85SDarrick J. Wong 	trace_ocfs2_double_lock_end(
4784d324cd4cSpiaojun 			(unsigned long long)oi1->ip_blkno,
4785d324cd4cSpiaojun 			(unsigned long long)oi2->ip_blkno);
478629ac8e85SDarrick J. Wong 
478729ac8e85SDarrick J. Wong 	return 0;
478829ac8e85SDarrick J. Wong 
478929ac8e85SDarrick J. Wong out_cl1:
479029ac8e85SDarrick J. Wong 	ocfs2_inode_unlock(inode1, 1);
4791e6a9467eSDarrick J. Wong 	brelse(bh1);
479229ac8e85SDarrick J. Wong out_rw2:
479329ac8e85SDarrick J. Wong 	ocfs2_rw_unlock(inode2, 1);
479429ac8e85SDarrick J. Wong out_i2:
479529ac8e85SDarrick J. Wong 	ocfs2_rw_unlock(inode1, 1);
479629ac8e85SDarrick J. Wong out_i1:
479729ac8e85SDarrick J. Wong 	unlock_two_nondirectories(s_inode, t_inode);
479829ac8e85SDarrick J. Wong 	return status;
479929ac8e85SDarrick J. Wong }
480029ac8e85SDarrick J. Wong 
480129ac8e85SDarrick J. Wong /* Unlock both inodes and release buffers. */
ocfs2_reflink_inodes_unlock(struct inode * s_inode,struct buffer_head * s_bh,struct inode * t_inode,struct buffer_head * t_bh)480265f098e9SDarrick J. Wong void ocfs2_reflink_inodes_unlock(struct inode *s_inode,
480329ac8e85SDarrick J. Wong 				 struct buffer_head *s_bh,
480429ac8e85SDarrick J. Wong 				 struct inode *t_inode,
480529ac8e85SDarrick J. Wong 				 struct buffer_head *t_bh)
480629ac8e85SDarrick J. Wong {
480729ac8e85SDarrick J. Wong 	ocfs2_inode_unlock(s_inode, 1);
480829ac8e85SDarrick J. Wong 	ocfs2_rw_unlock(s_inode, 1);
480929ac8e85SDarrick J. Wong 	brelse(s_bh);
481029ac8e85SDarrick J. Wong 	if (s_inode != t_inode) {
481129ac8e85SDarrick J. Wong 		ocfs2_inode_unlock(t_inode, 1);
481229ac8e85SDarrick J. Wong 		ocfs2_rw_unlock(t_inode, 1);
481329ac8e85SDarrick J. Wong 		brelse(t_bh);
481429ac8e85SDarrick J. Wong 	}
481529ac8e85SDarrick J. Wong 	unlock_two_nondirectories(s_inode, t_inode);
481629ac8e85SDarrick J. Wong }
4817