xref: /openbmc/linux/fs/ocfs2/refcounttree.c (revision c901fb00)
1f2c870e3STao Ma /* -*- mode: c; c-basic-offset: 8; -*-
2f2c870e3STao Ma  * vim: noexpandtab sw=8 ts=8 sts=0:
3f2c870e3STao Ma  *
4f2c870e3STao Ma  * refcounttree.c
5f2c870e3STao Ma  *
6f2c870e3STao Ma  * Copyright (C) 2009 Oracle.  All rights reserved.
7f2c870e3STao Ma  *
8f2c870e3STao Ma  * This program is free software; you can redistribute it and/or
9f2c870e3STao Ma  * modify it under the terms of the GNU General Public
10f2c870e3STao Ma  * License version 2 as published by the Free Software Foundation.
11f2c870e3STao Ma  *
12f2c870e3STao Ma  * This program is distributed in the hope that it will be useful,
13f2c870e3STao Ma  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14f2c870e3STao Ma  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15f2c870e3STao Ma  * General Public License for more details.
16f2c870e3STao Ma  */
17f2c870e3STao Ma 
18e73a819dSTao Ma #include <linux/sort.h>
19f2c870e3STao Ma #define MLOG_MASK_PREFIX ML_REFCOUNT
20f2c870e3STao Ma #include <cluster/masklog.h>
21f2c870e3STao Ma #include "ocfs2.h"
22f2c870e3STao Ma #include "inode.h"
23f2c870e3STao Ma #include "alloc.h"
24f2c870e3STao Ma #include "suballoc.h"
25f2c870e3STao Ma #include "journal.h"
26f2c870e3STao Ma #include "uptodate.h"
27f2c870e3STao Ma #include "super.h"
28f2c870e3STao Ma #include "buffer_head_io.h"
29f2c870e3STao Ma #include "blockcheck.h"
30c732eb16STao Ma #include "refcounttree.h"
318bf396deSTao Ma #include "sysfile.h"
32374a263eSTao Ma #include "dlmglue.h"
33e73a819dSTao Ma #include "extent_map.h"
346f70fa51STao Ma #include "aops.h"
35492a8a33STao Ma #include "xattr.h"
3609bf27a0STao Ma #include "namei.h"
376f70fa51STao Ma 
386f70fa51STao Ma #include <linux/bio.h>
396f70fa51STao Ma #include <linux/blkdev.h>
406f70fa51STao Ma #include <linux/gfp.h>
416f70fa51STao Ma #include <linux/slab.h>
426f70fa51STao Ma #include <linux/writeback.h>
436f70fa51STao Ma #include <linux/pagevec.h>
446f70fa51STao Ma #include <linux/swap.h>
45bd50873dSTao Ma #include <linux/security.h>
46bd50873dSTao Ma #include <linux/fsnotify.h>
47bd50873dSTao Ma #include <linux/quotaops.h>
48bd50873dSTao Ma #include <linux/namei.h>
49bd50873dSTao Ma #include <linux/mount.h>
506f70fa51STao Ma 
516f70fa51STao Ma struct ocfs2_cow_context {
526f70fa51STao Ma 	struct inode *inode;
536f70fa51STao Ma 	u32 cow_start;
546f70fa51STao Ma 	u32 cow_len;
55913580b4STao Ma 	struct ocfs2_extent_tree data_et;
56913580b4STao Ma 	struct ocfs2_refcount_tree *ref_tree;
576f70fa51STao Ma 	struct buffer_head *ref_root_bh;
586f70fa51STao Ma 	struct ocfs2_alloc_context *meta_ac;
596f70fa51STao Ma 	struct ocfs2_alloc_context *data_ac;
606f70fa51STao Ma 	struct ocfs2_cached_dealloc_ctxt dealloc;
61492a8a33STao Ma 	void *cow_object;
62492a8a33STao Ma 	struct ocfs2_post_refcount *post_refcount;
63492a8a33STao Ma 	int extra_credits;
64913580b4STao Ma 	int (*get_clusters)(struct ocfs2_cow_context *context,
65913580b4STao Ma 			    u32 v_cluster, u32 *p_cluster,
66913580b4STao Ma 			    u32 *num_clusters,
67913580b4STao Ma 			    unsigned int *extent_flags);
68913580b4STao Ma 	int (*cow_duplicate_clusters)(handle_t *handle,
69913580b4STao Ma 				      struct ocfs2_cow_context *context,
70913580b4STao Ma 				      u32 cpos, u32 old_cluster,
71913580b4STao Ma 				      u32 new_cluster, u32 new_len);
726f70fa51STao Ma };
73c732eb16STao Ma 
74c732eb16STao Ma static inline struct ocfs2_refcount_tree *
75c732eb16STao Ma cache_info_to_refcount(struct ocfs2_caching_info *ci)
76c732eb16STao Ma {
77c732eb16STao Ma 	return container_of(ci, struct ocfs2_refcount_tree, rf_ci);
78c732eb16STao Ma }
79f2c870e3STao Ma 
80f2c870e3STao Ma static int ocfs2_validate_refcount_block(struct super_block *sb,
81f2c870e3STao Ma 					 struct buffer_head *bh)
82f2c870e3STao Ma {
83f2c870e3STao Ma 	int rc;
84f2c870e3STao Ma 	struct ocfs2_refcount_block *rb =
85f2c870e3STao Ma 		(struct ocfs2_refcount_block *)bh->b_data;
86f2c870e3STao Ma 
87f2c870e3STao Ma 	mlog(0, "Validating refcount block %llu\n",
88f2c870e3STao Ma 	     (unsigned long long)bh->b_blocknr);
89f2c870e3STao Ma 
90f2c870e3STao Ma 	BUG_ON(!buffer_uptodate(bh));
91f2c870e3STao Ma 
92f2c870e3STao Ma 	/*
93f2c870e3STao Ma 	 * If the ecc fails, we return the error but otherwise
94f2c870e3STao Ma 	 * leave the filesystem running.  We know any error is
95f2c870e3STao Ma 	 * local to this block.
96f2c870e3STao Ma 	 */
97f2c870e3STao Ma 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &rb->rf_check);
98f2c870e3STao Ma 	if (rc) {
99f2c870e3STao Ma 		mlog(ML_ERROR, "Checksum failed for refcount block %llu\n",
100f2c870e3STao Ma 		     (unsigned long long)bh->b_blocknr);
101f2c870e3STao Ma 		return rc;
102f2c870e3STao Ma 	}
103f2c870e3STao Ma 
104f2c870e3STao Ma 
105f2c870e3STao Ma 	if (!OCFS2_IS_VALID_REFCOUNT_BLOCK(rb)) {
106f2c870e3STao Ma 		ocfs2_error(sb,
107f2c870e3STao Ma 			    "Refcount block #%llu has bad signature %.*s",
108f2c870e3STao Ma 			    (unsigned long long)bh->b_blocknr, 7,
109f2c870e3STao Ma 			    rb->rf_signature);
110f2c870e3STao Ma 		return -EINVAL;
111f2c870e3STao Ma 	}
112f2c870e3STao Ma 
113f2c870e3STao Ma 	if (le64_to_cpu(rb->rf_blkno) != bh->b_blocknr) {
114f2c870e3STao Ma 		ocfs2_error(sb,
115f2c870e3STao Ma 			    "Refcount block #%llu has an invalid rf_blkno "
116f2c870e3STao Ma 			    "of %llu",
117f2c870e3STao Ma 			    (unsigned long long)bh->b_blocknr,
118f2c870e3STao Ma 			    (unsigned long long)le64_to_cpu(rb->rf_blkno));
119f2c870e3STao Ma 		return -EINVAL;
120f2c870e3STao Ma 	}
121f2c870e3STao Ma 
122f2c870e3STao Ma 	if (le32_to_cpu(rb->rf_fs_generation) != OCFS2_SB(sb)->fs_generation) {
123f2c870e3STao Ma 		ocfs2_error(sb,
124f2c870e3STao Ma 			    "Refcount block #%llu has an invalid "
125f2c870e3STao Ma 			    "rf_fs_generation of #%u",
126f2c870e3STao Ma 			    (unsigned long long)bh->b_blocknr,
127f2c870e3STao Ma 			    le32_to_cpu(rb->rf_fs_generation));
128f2c870e3STao Ma 		return -EINVAL;
129f2c870e3STao Ma 	}
130f2c870e3STao Ma 
131f2c870e3STao Ma 	return 0;
132f2c870e3STao Ma }
133f2c870e3STao Ma 
134f2c870e3STao Ma static int ocfs2_read_refcount_block(struct ocfs2_caching_info *ci,
135f2c870e3STao Ma 				     u64 rb_blkno,
136f2c870e3STao Ma 				     struct buffer_head **bh)
137f2c870e3STao Ma {
138f2c870e3STao Ma 	int rc;
139f2c870e3STao Ma 	struct buffer_head *tmp = *bh;
140f2c870e3STao Ma 
141f2c870e3STao Ma 	rc = ocfs2_read_block(ci, rb_blkno, &tmp,
142f2c870e3STao Ma 			      ocfs2_validate_refcount_block);
143f2c870e3STao Ma 
144f2c870e3STao Ma 	/* If ocfs2_read_block() got us a new bh, pass it up. */
145f2c870e3STao Ma 	if (!rc && !*bh)
146f2c870e3STao Ma 		*bh = tmp;
147f2c870e3STao Ma 
148f2c870e3STao Ma 	return rc;
149f2c870e3STao Ma }
150c732eb16STao Ma 
151c732eb16STao Ma static u64 ocfs2_refcount_cache_owner(struct ocfs2_caching_info *ci)
152c732eb16STao Ma {
153c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
154c732eb16STao Ma 
155c732eb16STao Ma 	return rf->rf_blkno;
156c732eb16STao Ma }
157c732eb16STao Ma 
158c732eb16STao Ma static struct super_block *
159c732eb16STao Ma ocfs2_refcount_cache_get_super(struct ocfs2_caching_info *ci)
160c732eb16STao Ma {
161c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
162c732eb16STao Ma 
163c732eb16STao Ma 	return rf->rf_sb;
164c732eb16STao Ma }
165c732eb16STao Ma 
166c732eb16STao Ma static void ocfs2_refcount_cache_lock(struct ocfs2_caching_info *ci)
167c732eb16STao Ma {
168c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
169c732eb16STao Ma 
170c732eb16STao Ma 	spin_lock(&rf->rf_lock);
171c732eb16STao Ma }
172c732eb16STao Ma 
173c732eb16STao Ma static void ocfs2_refcount_cache_unlock(struct ocfs2_caching_info *ci)
174c732eb16STao Ma {
175c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
176c732eb16STao Ma 
177c732eb16STao Ma 	spin_unlock(&rf->rf_lock);
178c732eb16STao Ma }
179c732eb16STao Ma 
180c732eb16STao Ma static void ocfs2_refcount_cache_io_lock(struct ocfs2_caching_info *ci)
181c732eb16STao Ma {
182c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
183c732eb16STao Ma 
184c732eb16STao Ma 	mutex_lock(&rf->rf_io_mutex);
185c732eb16STao Ma }
186c732eb16STao Ma 
187c732eb16STao Ma static void ocfs2_refcount_cache_io_unlock(struct ocfs2_caching_info *ci)
188c732eb16STao Ma {
189c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
190c732eb16STao Ma 
191c732eb16STao Ma 	mutex_unlock(&rf->rf_io_mutex);
192c732eb16STao Ma }
193c732eb16STao Ma 
194c732eb16STao Ma static const struct ocfs2_caching_operations ocfs2_refcount_caching_ops = {
195c732eb16STao Ma 	.co_owner		= ocfs2_refcount_cache_owner,
196c732eb16STao Ma 	.co_get_super		= ocfs2_refcount_cache_get_super,
197c732eb16STao Ma 	.co_cache_lock		= ocfs2_refcount_cache_lock,
198c732eb16STao Ma 	.co_cache_unlock	= ocfs2_refcount_cache_unlock,
199c732eb16STao Ma 	.co_io_lock		= ocfs2_refcount_cache_io_lock,
200c732eb16STao Ma 	.co_io_unlock		= ocfs2_refcount_cache_io_unlock,
201c732eb16STao Ma };
202374a263eSTao Ma 
203374a263eSTao Ma static struct ocfs2_refcount_tree *
204374a263eSTao Ma ocfs2_find_refcount_tree(struct ocfs2_super *osb, u64 blkno)
205374a263eSTao Ma {
206374a263eSTao Ma 	struct rb_node *n = osb->osb_rf_lock_tree.rb_node;
207374a263eSTao Ma 	struct ocfs2_refcount_tree *tree = NULL;
208374a263eSTao Ma 
209374a263eSTao Ma 	while (n) {
210374a263eSTao Ma 		tree = rb_entry(n, struct ocfs2_refcount_tree, rf_node);
211374a263eSTao Ma 
212374a263eSTao Ma 		if (blkno < tree->rf_blkno)
213374a263eSTao Ma 			n = n->rb_left;
214374a263eSTao Ma 		else if (blkno > tree->rf_blkno)
215374a263eSTao Ma 			n = n->rb_right;
216374a263eSTao Ma 		else
217374a263eSTao Ma 			return tree;
218374a263eSTao Ma 	}
219374a263eSTao Ma 
220374a263eSTao Ma 	return NULL;
221374a263eSTao Ma }
222374a263eSTao Ma 
223374a263eSTao Ma /* osb_lock is already locked. */
224374a263eSTao Ma static void ocfs2_insert_refcount_tree(struct ocfs2_super *osb,
225374a263eSTao Ma 				       struct ocfs2_refcount_tree *new)
226374a263eSTao Ma {
227374a263eSTao Ma 	u64 rf_blkno = new->rf_blkno;
228374a263eSTao Ma 	struct rb_node *parent = NULL;
229374a263eSTao Ma 	struct rb_node **p = &osb->osb_rf_lock_tree.rb_node;
230374a263eSTao Ma 	struct ocfs2_refcount_tree *tmp;
231374a263eSTao Ma 
232374a263eSTao Ma 	while (*p) {
233374a263eSTao Ma 		parent = *p;
234374a263eSTao Ma 
235374a263eSTao Ma 		tmp = rb_entry(parent, struct ocfs2_refcount_tree,
236374a263eSTao Ma 			       rf_node);
237374a263eSTao Ma 
238374a263eSTao Ma 		if (rf_blkno < tmp->rf_blkno)
239374a263eSTao Ma 			p = &(*p)->rb_left;
240374a263eSTao Ma 		else if (rf_blkno > tmp->rf_blkno)
241374a263eSTao Ma 			p = &(*p)->rb_right;
242374a263eSTao Ma 		else {
243374a263eSTao Ma 			/* This should never happen! */
244374a263eSTao Ma 			mlog(ML_ERROR, "Duplicate refcount block %llu found!\n",
245374a263eSTao Ma 			     (unsigned long long)rf_blkno);
246374a263eSTao Ma 			BUG();
247374a263eSTao Ma 		}
248374a263eSTao Ma 	}
249374a263eSTao Ma 
250374a263eSTao Ma 	rb_link_node(&new->rf_node, parent, p);
251374a263eSTao Ma 	rb_insert_color(&new->rf_node, &osb->osb_rf_lock_tree);
252374a263eSTao Ma }
253374a263eSTao Ma 
254374a263eSTao Ma static void ocfs2_free_refcount_tree(struct ocfs2_refcount_tree *tree)
255374a263eSTao Ma {
256374a263eSTao Ma 	ocfs2_metadata_cache_exit(&tree->rf_ci);
257374a263eSTao Ma 	ocfs2_simple_drop_lockres(OCFS2_SB(tree->rf_sb), &tree->rf_lockres);
258374a263eSTao Ma 	ocfs2_lock_res_free(&tree->rf_lockres);
259374a263eSTao Ma 	kfree(tree);
260374a263eSTao Ma }
261374a263eSTao Ma 
262374a263eSTao Ma static inline void
263374a263eSTao Ma ocfs2_erase_refcount_tree_from_list_no_lock(struct ocfs2_super *osb,
264374a263eSTao Ma 					struct ocfs2_refcount_tree *tree)
265374a263eSTao Ma {
266374a263eSTao Ma 	rb_erase(&tree->rf_node, &osb->osb_rf_lock_tree);
267374a263eSTao Ma 	if (osb->osb_ref_tree_lru && osb->osb_ref_tree_lru == tree)
268374a263eSTao Ma 		osb->osb_ref_tree_lru = NULL;
269374a263eSTao Ma }
270374a263eSTao Ma 
271374a263eSTao Ma static void ocfs2_erase_refcount_tree_from_list(struct ocfs2_super *osb,
272374a263eSTao Ma 					struct ocfs2_refcount_tree *tree)
273374a263eSTao Ma {
274374a263eSTao Ma 	spin_lock(&osb->osb_lock);
275374a263eSTao Ma 	ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
276374a263eSTao Ma 	spin_unlock(&osb->osb_lock);
277374a263eSTao Ma }
278374a263eSTao Ma 
27912d4cec9STao Ma static void ocfs2_kref_remove_refcount_tree(struct kref *kref)
280374a263eSTao Ma {
281374a263eSTao Ma 	struct ocfs2_refcount_tree *tree =
282374a263eSTao Ma 		container_of(kref, struct ocfs2_refcount_tree, rf_getcnt);
283374a263eSTao Ma 
284374a263eSTao Ma 	ocfs2_free_refcount_tree(tree);
285374a263eSTao Ma }
286374a263eSTao Ma 
287374a263eSTao Ma static inline void
288374a263eSTao Ma ocfs2_refcount_tree_get(struct ocfs2_refcount_tree *tree)
289374a263eSTao Ma {
290374a263eSTao Ma 	kref_get(&tree->rf_getcnt);
291374a263eSTao Ma }
292374a263eSTao Ma 
293374a263eSTao Ma static inline void
294374a263eSTao Ma ocfs2_refcount_tree_put(struct ocfs2_refcount_tree *tree)
295374a263eSTao Ma {
296374a263eSTao Ma 	kref_put(&tree->rf_getcnt, ocfs2_kref_remove_refcount_tree);
297374a263eSTao Ma }
298374a263eSTao Ma 
299374a263eSTao Ma static inline void ocfs2_init_refcount_tree_ci(struct ocfs2_refcount_tree *new,
300374a263eSTao Ma 					       struct super_block *sb)
301374a263eSTao Ma {
302374a263eSTao Ma 	ocfs2_metadata_cache_init(&new->rf_ci, &ocfs2_refcount_caching_ops);
303374a263eSTao Ma 	mutex_init(&new->rf_io_mutex);
304374a263eSTao Ma 	new->rf_sb = sb;
305374a263eSTao Ma 	spin_lock_init(&new->rf_lock);
306374a263eSTao Ma }
307374a263eSTao Ma 
308374a263eSTao Ma static inline void ocfs2_init_refcount_tree_lock(struct ocfs2_super *osb,
309374a263eSTao Ma 					struct ocfs2_refcount_tree *new,
310374a263eSTao Ma 					u64 rf_blkno, u32 generation)
311374a263eSTao Ma {
312374a263eSTao Ma 	init_rwsem(&new->rf_sem);
313374a263eSTao Ma 	ocfs2_refcount_lock_res_init(&new->rf_lockres, osb,
314374a263eSTao Ma 				     rf_blkno, generation);
315374a263eSTao Ma }
316374a263eSTao Ma 
3178bf396deSTao Ma static struct ocfs2_refcount_tree*
3188bf396deSTao Ma ocfs2_allocate_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno)
3198bf396deSTao Ma {
3208bf396deSTao Ma 	struct ocfs2_refcount_tree *new;
3218bf396deSTao Ma 
3228bf396deSTao Ma 	new = kzalloc(sizeof(struct ocfs2_refcount_tree), GFP_NOFS);
3238bf396deSTao Ma 	if (!new)
3248bf396deSTao Ma 		return NULL;
3258bf396deSTao Ma 
3268bf396deSTao Ma 	new->rf_blkno = rf_blkno;
3278bf396deSTao Ma 	kref_init(&new->rf_getcnt);
3288bf396deSTao Ma 	ocfs2_init_refcount_tree_ci(new, osb->sb);
3298bf396deSTao Ma 
3308bf396deSTao Ma 	return new;
3318bf396deSTao Ma }
3328bf396deSTao Ma 
333374a263eSTao Ma static int ocfs2_get_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno,
334374a263eSTao Ma 				   struct ocfs2_refcount_tree **ret_tree)
335374a263eSTao Ma {
336374a263eSTao Ma 	int ret = 0;
337374a263eSTao Ma 	struct ocfs2_refcount_tree *tree, *new = NULL;
338374a263eSTao Ma 	struct buffer_head *ref_root_bh = NULL;
339374a263eSTao Ma 	struct ocfs2_refcount_block *ref_rb;
340374a263eSTao Ma 
341374a263eSTao Ma 	spin_lock(&osb->osb_lock);
342374a263eSTao Ma 	if (osb->osb_ref_tree_lru &&
343374a263eSTao Ma 	    osb->osb_ref_tree_lru->rf_blkno == rf_blkno)
344374a263eSTao Ma 		tree = osb->osb_ref_tree_lru;
345374a263eSTao Ma 	else
346374a263eSTao Ma 		tree = ocfs2_find_refcount_tree(osb, rf_blkno);
347374a263eSTao Ma 	if (tree)
348374a263eSTao Ma 		goto out;
349374a263eSTao Ma 
350374a263eSTao Ma 	spin_unlock(&osb->osb_lock);
351374a263eSTao Ma 
3528bf396deSTao Ma 	new = ocfs2_allocate_refcount_tree(osb, rf_blkno);
353374a263eSTao Ma 	if (!new) {
354374a263eSTao Ma 		ret = -ENOMEM;
3558bf396deSTao Ma 		mlog_errno(ret);
356374a263eSTao Ma 		return ret;
357374a263eSTao Ma 	}
358374a263eSTao Ma 	/*
359374a263eSTao Ma 	 * We need the generation to create the refcount tree lock and since
360374a263eSTao Ma 	 * it isn't changed during the tree modification, we are safe here to
361374a263eSTao Ma 	 * read without protection.
362374a263eSTao Ma 	 * We also have to purge the cache after we create the lock since the
363374a263eSTao Ma 	 * refcount block may have the stale data. It can only be trusted when
364374a263eSTao Ma 	 * we hold the refcount lock.
365374a263eSTao Ma 	 */
366374a263eSTao Ma 	ret = ocfs2_read_refcount_block(&new->rf_ci, rf_blkno, &ref_root_bh);
367374a263eSTao Ma 	if (ret) {
368374a263eSTao Ma 		mlog_errno(ret);
369374a263eSTao Ma 		ocfs2_metadata_cache_exit(&new->rf_ci);
370374a263eSTao Ma 		kfree(new);
371374a263eSTao Ma 		return ret;
372374a263eSTao Ma 	}
373374a263eSTao Ma 
374374a263eSTao Ma 	ref_rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
375374a263eSTao Ma 	new->rf_generation = le32_to_cpu(ref_rb->rf_generation);
376374a263eSTao Ma 	ocfs2_init_refcount_tree_lock(osb, new, rf_blkno,
377374a263eSTao Ma 				      new->rf_generation);
378374a263eSTao Ma 	ocfs2_metadata_cache_purge(&new->rf_ci);
379374a263eSTao Ma 
380374a263eSTao Ma 	spin_lock(&osb->osb_lock);
381374a263eSTao Ma 	tree = ocfs2_find_refcount_tree(osb, rf_blkno);
382374a263eSTao Ma 	if (tree)
383374a263eSTao Ma 		goto out;
384374a263eSTao Ma 
385374a263eSTao Ma 	ocfs2_insert_refcount_tree(osb, new);
386374a263eSTao Ma 
387374a263eSTao Ma 	tree = new;
388374a263eSTao Ma 	new = NULL;
389374a263eSTao Ma 
390374a263eSTao Ma out:
391374a263eSTao Ma 	*ret_tree = tree;
392374a263eSTao Ma 
393374a263eSTao Ma 	osb->osb_ref_tree_lru = tree;
394374a263eSTao Ma 
395374a263eSTao Ma 	spin_unlock(&osb->osb_lock);
396374a263eSTao Ma 
397374a263eSTao Ma 	if (new)
398374a263eSTao Ma 		ocfs2_free_refcount_tree(new);
399374a263eSTao Ma 
400374a263eSTao Ma 	brelse(ref_root_bh);
401374a263eSTao Ma 	return ret;
402374a263eSTao Ma }
403374a263eSTao Ma 
404374a263eSTao Ma static int ocfs2_get_refcount_block(struct inode *inode, u64 *ref_blkno)
405374a263eSTao Ma {
406374a263eSTao Ma 	int ret;
407374a263eSTao Ma 	struct buffer_head *di_bh = NULL;
408374a263eSTao Ma 	struct ocfs2_dinode *di;
409374a263eSTao Ma 
410374a263eSTao Ma 	ret = ocfs2_read_inode_block(inode, &di_bh);
411374a263eSTao Ma 	if (ret) {
412374a263eSTao Ma 		mlog_errno(ret);
413374a263eSTao Ma 		goto out;
414374a263eSTao Ma 	}
415374a263eSTao Ma 
416374a263eSTao Ma 	BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
417374a263eSTao Ma 
418374a263eSTao Ma 	di = (struct ocfs2_dinode *)di_bh->b_data;
419374a263eSTao Ma 	*ref_blkno = le64_to_cpu(di->i_refcount_loc);
420374a263eSTao Ma 	brelse(di_bh);
421374a263eSTao Ma out:
422374a263eSTao Ma 	return ret;
423374a263eSTao Ma }
424374a263eSTao Ma 
425374a263eSTao Ma static int __ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
426374a263eSTao Ma 				      struct ocfs2_refcount_tree *tree, int rw)
427374a263eSTao Ma {
428374a263eSTao Ma 	int ret;
429374a263eSTao Ma 
430374a263eSTao Ma 	ret = ocfs2_refcount_lock(tree, rw);
431374a263eSTao Ma 	if (ret) {
432374a263eSTao Ma 		mlog_errno(ret);
433374a263eSTao Ma 		goto out;
434374a263eSTao Ma 	}
435374a263eSTao Ma 
436374a263eSTao Ma 	if (rw)
437374a263eSTao Ma 		down_write(&tree->rf_sem);
438374a263eSTao Ma 	else
439374a263eSTao Ma 		down_read(&tree->rf_sem);
440374a263eSTao Ma 
441374a263eSTao Ma out:
442374a263eSTao Ma 	return ret;
443374a263eSTao Ma }
444374a263eSTao Ma 
445374a263eSTao Ma /*
446374a263eSTao Ma  * Lock the refcount tree pointed by ref_blkno and return the tree.
447374a263eSTao Ma  * In most case, we lock the tree and read the refcount block.
448374a263eSTao Ma  * So read it here if the caller really needs it.
449374a263eSTao Ma  *
450374a263eSTao Ma  * If the tree has been re-created by other node, it will free the
451374a263eSTao Ma  * old one and re-create it.
452374a263eSTao Ma  */
453374a263eSTao Ma int ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
454374a263eSTao Ma 			     u64 ref_blkno, int rw,
455374a263eSTao Ma 			     struct ocfs2_refcount_tree **ret_tree,
456374a263eSTao Ma 			     struct buffer_head **ref_bh)
457374a263eSTao Ma {
458374a263eSTao Ma 	int ret, delete_tree = 0;
459374a263eSTao Ma 	struct ocfs2_refcount_tree *tree = NULL;
460374a263eSTao Ma 	struct buffer_head *ref_root_bh = NULL;
461374a263eSTao Ma 	struct ocfs2_refcount_block *rb;
462374a263eSTao Ma 
463374a263eSTao Ma again:
464374a263eSTao Ma 	ret = ocfs2_get_refcount_tree(osb, ref_blkno, &tree);
465374a263eSTao Ma 	if (ret) {
466374a263eSTao Ma 		mlog_errno(ret);
467374a263eSTao Ma 		return ret;
468374a263eSTao Ma 	}
469374a263eSTao Ma 
470374a263eSTao Ma 	ocfs2_refcount_tree_get(tree);
471374a263eSTao Ma 
472374a263eSTao Ma 	ret = __ocfs2_lock_refcount_tree(osb, tree, rw);
473374a263eSTao Ma 	if (ret) {
474374a263eSTao Ma 		mlog_errno(ret);
475374a263eSTao Ma 		ocfs2_refcount_tree_put(tree);
476374a263eSTao Ma 		goto out;
477374a263eSTao Ma 	}
478374a263eSTao Ma 
479374a263eSTao Ma 	ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
480374a263eSTao Ma 					&ref_root_bh);
481374a263eSTao Ma 	if (ret) {
482374a263eSTao Ma 		mlog_errno(ret);
483374a263eSTao Ma 		ocfs2_unlock_refcount_tree(osb, tree, rw);
484374a263eSTao Ma 		ocfs2_refcount_tree_put(tree);
485374a263eSTao Ma 		goto out;
486374a263eSTao Ma 	}
487374a263eSTao Ma 
488374a263eSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
489374a263eSTao Ma 	/*
490374a263eSTao Ma 	 * If the refcount block has been freed and re-created, we may need
491374a263eSTao Ma 	 * to recreate the refcount tree also.
492374a263eSTao Ma 	 *
493374a263eSTao Ma 	 * Here we just remove the tree from the rb-tree, and the last
494374a263eSTao Ma 	 * kref holder will unlock and delete this refcount_tree.
495374a263eSTao Ma 	 * Then we goto "again" and ocfs2_get_refcount_tree will create
496374a263eSTao Ma 	 * the new refcount tree for us.
497374a263eSTao Ma 	 */
498374a263eSTao Ma 	if (tree->rf_generation != le32_to_cpu(rb->rf_generation)) {
499374a263eSTao Ma 		if (!tree->rf_removed) {
500374a263eSTao Ma 			ocfs2_erase_refcount_tree_from_list(osb, tree);
501374a263eSTao Ma 			tree->rf_removed = 1;
502374a263eSTao Ma 			delete_tree = 1;
503374a263eSTao Ma 		}
504374a263eSTao Ma 
505374a263eSTao Ma 		ocfs2_unlock_refcount_tree(osb, tree, rw);
506374a263eSTao Ma 		/*
507374a263eSTao Ma 		 * We get an extra reference when we create the refcount
508374a263eSTao Ma 		 * tree, so another put will destroy it.
509374a263eSTao Ma 		 */
510374a263eSTao Ma 		if (delete_tree)
511374a263eSTao Ma 			ocfs2_refcount_tree_put(tree);
512374a263eSTao Ma 		brelse(ref_root_bh);
513374a263eSTao Ma 		ref_root_bh = NULL;
514374a263eSTao Ma 		goto again;
515374a263eSTao Ma 	}
516374a263eSTao Ma 
517374a263eSTao Ma 	*ret_tree = tree;
518374a263eSTao Ma 	if (ref_bh) {
519374a263eSTao Ma 		*ref_bh = ref_root_bh;
520374a263eSTao Ma 		ref_root_bh = NULL;
521374a263eSTao Ma 	}
522374a263eSTao Ma out:
523374a263eSTao Ma 	brelse(ref_root_bh);
524374a263eSTao Ma 	return ret;
525374a263eSTao Ma }
526374a263eSTao Ma 
527374a263eSTao Ma void ocfs2_unlock_refcount_tree(struct ocfs2_super *osb,
528374a263eSTao Ma 				struct ocfs2_refcount_tree *tree, int rw)
529374a263eSTao Ma {
530374a263eSTao Ma 	if (rw)
531374a263eSTao Ma 		up_write(&tree->rf_sem);
532374a263eSTao Ma 	else
533374a263eSTao Ma 		up_read(&tree->rf_sem);
534374a263eSTao Ma 
535374a263eSTao Ma 	ocfs2_refcount_unlock(tree, rw);
536374a263eSTao Ma 	ocfs2_refcount_tree_put(tree);
537374a263eSTao Ma }
538374a263eSTao Ma 
539374a263eSTao Ma void ocfs2_purge_refcount_trees(struct ocfs2_super *osb)
540374a263eSTao Ma {
541374a263eSTao Ma 	struct rb_node *node;
542374a263eSTao Ma 	struct ocfs2_refcount_tree *tree;
543374a263eSTao Ma 	struct rb_root *root = &osb->osb_rf_lock_tree;
544374a263eSTao Ma 
545374a263eSTao Ma 	while ((node = rb_last(root)) != NULL) {
546374a263eSTao Ma 		tree = rb_entry(node, struct ocfs2_refcount_tree, rf_node);
547374a263eSTao Ma 
548374a263eSTao Ma 		mlog(0, "Purge tree %llu\n",
549374a263eSTao Ma 		     (unsigned long long) tree->rf_blkno);
550374a263eSTao Ma 
551374a263eSTao Ma 		rb_erase(&tree->rf_node, root);
552374a263eSTao Ma 		ocfs2_free_refcount_tree(tree);
553374a263eSTao Ma 	}
554374a263eSTao Ma }
5558bf396deSTao Ma 
5568bf396deSTao Ma /*
5578bf396deSTao Ma  * Create a refcount tree for an inode.
5588bf396deSTao Ma  * We take for granted that the inode is already locked.
5598bf396deSTao Ma  */
5608bf396deSTao Ma static int ocfs2_create_refcount_tree(struct inode *inode,
5618bf396deSTao Ma 				      struct buffer_head *di_bh)
5628bf396deSTao Ma {
5638bf396deSTao Ma 	int ret;
5648bf396deSTao Ma 	handle_t *handle = NULL;
5658bf396deSTao Ma 	struct ocfs2_alloc_context *meta_ac = NULL;
5668bf396deSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
5678bf396deSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
5688bf396deSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5698bf396deSTao Ma 	struct buffer_head *new_bh = NULL;
5708bf396deSTao Ma 	struct ocfs2_refcount_block *rb;
5718bf396deSTao Ma 	struct ocfs2_refcount_tree *new_tree = NULL, *tree = NULL;
5728bf396deSTao Ma 	u16 suballoc_bit_start;
5738bf396deSTao Ma 	u32 num_got;
5748bf396deSTao Ma 	u64 first_blkno;
5758bf396deSTao Ma 
5768bf396deSTao Ma 	BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL);
5778bf396deSTao Ma 
5788bf396deSTao Ma 	mlog(0, "create tree for inode %lu\n", inode->i_ino);
5798bf396deSTao Ma 
5808bf396deSTao Ma 	ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
5818bf396deSTao Ma 	if (ret) {
5828bf396deSTao Ma 		mlog_errno(ret);
5838bf396deSTao Ma 		goto out;
5848bf396deSTao Ma 	}
5858bf396deSTao Ma 
5868bf396deSTao Ma 	handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_CREATE_CREDITS);
5878bf396deSTao Ma 	if (IS_ERR(handle)) {
5888bf396deSTao Ma 		ret = PTR_ERR(handle);
5898bf396deSTao Ma 		mlog_errno(ret);
5908bf396deSTao Ma 		goto out;
5918bf396deSTao Ma 	}
5928bf396deSTao Ma 
5938bf396deSTao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
5948bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
5958bf396deSTao Ma 	if (ret) {
5968bf396deSTao Ma 		mlog_errno(ret);
5978bf396deSTao Ma 		goto out_commit;
5988bf396deSTao Ma 	}
5998bf396deSTao Ma 
6008bf396deSTao Ma 	ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
6018bf396deSTao Ma 				   &suballoc_bit_start, &num_got,
6028bf396deSTao Ma 				   &first_blkno);
6038bf396deSTao Ma 	if (ret) {
6048bf396deSTao Ma 		mlog_errno(ret);
6058bf396deSTao Ma 		goto out_commit;
6068bf396deSTao Ma 	}
6078bf396deSTao Ma 
6088bf396deSTao Ma 	new_tree = ocfs2_allocate_refcount_tree(osb, first_blkno);
6098bf396deSTao Ma 	if (!new_tree) {
6108bf396deSTao Ma 		ret = -ENOMEM;
6118bf396deSTao Ma 		mlog_errno(ret);
6128bf396deSTao Ma 		goto out_commit;
6138bf396deSTao Ma 	}
6148bf396deSTao Ma 
6158bf396deSTao Ma 	new_bh = sb_getblk(inode->i_sb, first_blkno);
6168bf396deSTao Ma 	ocfs2_set_new_buffer_uptodate(&new_tree->rf_ci, new_bh);
6178bf396deSTao Ma 
6188bf396deSTao Ma 	ret = ocfs2_journal_access_rb(handle, &new_tree->rf_ci, new_bh,
6198bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_CREATE);
6208bf396deSTao Ma 	if (ret) {
6218bf396deSTao Ma 		mlog_errno(ret);
6228bf396deSTao Ma 		goto out_commit;
6238bf396deSTao Ma 	}
6248bf396deSTao Ma 
6258bf396deSTao Ma 	/* Initialize ocfs2_refcount_block. */
6268bf396deSTao Ma 	rb = (struct ocfs2_refcount_block *)new_bh->b_data;
6278bf396deSTao Ma 	memset(rb, 0, inode->i_sb->s_blocksize);
6288bf396deSTao Ma 	strcpy((void *)rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
629b89c5428STiger Yang 	rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
6308bf396deSTao Ma 	rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
6318bf396deSTao Ma 	rb->rf_fs_generation = cpu_to_le32(osb->fs_generation);
6328bf396deSTao Ma 	rb->rf_blkno = cpu_to_le64(first_blkno);
6338bf396deSTao Ma 	rb->rf_count = cpu_to_le32(1);
6348bf396deSTao Ma 	rb->rf_records.rl_count =
6358bf396deSTao Ma 			cpu_to_le16(ocfs2_refcount_recs_per_rb(osb->sb));
6368bf396deSTao Ma 	spin_lock(&osb->osb_lock);
6378bf396deSTao Ma 	rb->rf_generation = osb->s_next_generation++;
6388bf396deSTao Ma 	spin_unlock(&osb->osb_lock);
6398bf396deSTao Ma 
6408bf396deSTao Ma 	ocfs2_journal_dirty(handle, new_bh);
6418bf396deSTao Ma 
6428bf396deSTao Ma 	spin_lock(&oi->ip_lock);
6438bf396deSTao Ma 	oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
6448bf396deSTao Ma 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6458bf396deSTao Ma 	di->i_refcount_loc = cpu_to_le64(first_blkno);
6468bf396deSTao Ma 	spin_unlock(&oi->ip_lock);
6478bf396deSTao Ma 
6488bf396deSTao Ma 	mlog(0, "created tree for inode %lu, refblock %llu\n",
6498bf396deSTao Ma 	     inode->i_ino, (unsigned long long)first_blkno);
6508bf396deSTao Ma 
6518bf396deSTao Ma 	ocfs2_journal_dirty(handle, di_bh);
6528bf396deSTao Ma 
6538bf396deSTao Ma 	/*
6548bf396deSTao Ma 	 * We have to init the tree lock here since it will use
6558bf396deSTao Ma 	 * the generation number to create it.
6568bf396deSTao Ma 	 */
6578bf396deSTao Ma 	new_tree->rf_generation = le32_to_cpu(rb->rf_generation);
6588bf396deSTao Ma 	ocfs2_init_refcount_tree_lock(osb, new_tree, first_blkno,
6598bf396deSTao Ma 				      new_tree->rf_generation);
6608bf396deSTao Ma 
6618bf396deSTao Ma 	spin_lock(&osb->osb_lock);
6628bf396deSTao Ma 	tree = ocfs2_find_refcount_tree(osb, first_blkno);
6638bf396deSTao Ma 
6648bf396deSTao Ma 	/*
6658bf396deSTao Ma 	 * We've just created a new refcount tree in this block.  If
6668bf396deSTao Ma 	 * we found a refcount tree on the ocfs2_super, it must be
6678bf396deSTao Ma 	 * one we just deleted.  We free the old tree before
6688bf396deSTao Ma 	 * inserting the new tree.
6698bf396deSTao Ma 	 */
6708bf396deSTao Ma 	BUG_ON(tree && tree->rf_generation == new_tree->rf_generation);
6718bf396deSTao Ma 	if (tree)
6728bf396deSTao Ma 		ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
6738bf396deSTao Ma 	ocfs2_insert_refcount_tree(osb, new_tree);
6748bf396deSTao Ma 	spin_unlock(&osb->osb_lock);
6758bf396deSTao Ma 	new_tree = NULL;
6768bf396deSTao Ma 	if (tree)
6778bf396deSTao Ma 		ocfs2_refcount_tree_put(tree);
6788bf396deSTao Ma 
6798bf396deSTao Ma out_commit:
6808bf396deSTao Ma 	ocfs2_commit_trans(osb, handle);
6818bf396deSTao Ma 
6828bf396deSTao Ma out:
6838bf396deSTao Ma 	if (new_tree) {
6848bf396deSTao Ma 		ocfs2_metadata_cache_exit(&new_tree->rf_ci);
6858bf396deSTao Ma 		kfree(new_tree);
6868bf396deSTao Ma 	}
6878bf396deSTao Ma 
6888bf396deSTao Ma 	brelse(new_bh);
6898bf396deSTao Ma 	if (meta_ac)
6908bf396deSTao Ma 		ocfs2_free_alloc_context(meta_ac);
6918bf396deSTao Ma 
6928bf396deSTao Ma 	return ret;
6938bf396deSTao Ma }
6948bf396deSTao Ma 
6958bf396deSTao Ma static int ocfs2_set_refcount_tree(struct inode *inode,
6968bf396deSTao Ma 				   struct buffer_head *di_bh,
6978bf396deSTao Ma 				   u64 refcount_loc)
6988bf396deSTao Ma {
6998bf396deSTao Ma 	int ret;
7008bf396deSTao Ma 	handle_t *handle = NULL;
7018bf396deSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7028bf396deSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
7038bf396deSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7048bf396deSTao Ma 	struct buffer_head *ref_root_bh = NULL;
7058bf396deSTao Ma 	struct ocfs2_refcount_block *rb;
7068bf396deSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
7078bf396deSTao Ma 
7088bf396deSTao Ma 	BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL);
7098bf396deSTao Ma 
7108bf396deSTao Ma 	ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
7118bf396deSTao Ma 				       &ref_tree, &ref_root_bh);
7128bf396deSTao Ma 	if (ret) {
7138bf396deSTao Ma 		mlog_errno(ret);
7148bf396deSTao Ma 		return ret;
7158bf396deSTao Ma 	}
7168bf396deSTao Ma 
7178bf396deSTao Ma 	handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_SET_CREDITS);
7188bf396deSTao Ma 	if (IS_ERR(handle)) {
7198bf396deSTao Ma 		ret = PTR_ERR(handle);
7208bf396deSTao Ma 		mlog_errno(ret);
7218bf396deSTao Ma 		goto out;
7228bf396deSTao Ma 	}
7238bf396deSTao Ma 
7248bf396deSTao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7258bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
7268bf396deSTao Ma 	if (ret) {
7278bf396deSTao Ma 		mlog_errno(ret);
7288bf396deSTao Ma 		goto out_commit;
7298bf396deSTao Ma 	}
7308bf396deSTao Ma 
7318bf396deSTao Ma 	ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, ref_root_bh,
7328bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
7338bf396deSTao Ma 	if (ret) {
7348bf396deSTao Ma 		mlog_errno(ret);
7358bf396deSTao Ma 		goto out_commit;
7368bf396deSTao Ma 	}
7378bf396deSTao Ma 
7388bf396deSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
7398bf396deSTao Ma 	le32_add_cpu(&rb->rf_count, 1);
7408bf396deSTao Ma 
7418bf396deSTao Ma 	ocfs2_journal_dirty(handle, ref_root_bh);
7428bf396deSTao Ma 
7438bf396deSTao Ma 	spin_lock(&oi->ip_lock);
7448bf396deSTao Ma 	oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
7458bf396deSTao Ma 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7468bf396deSTao Ma 	di->i_refcount_loc = cpu_to_le64(refcount_loc);
7478bf396deSTao Ma 	spin_unlock(&oi->ip_lock);
7488bf396deSTao Ma 	ocfs2_journal_dirty(handle, di_bh);
7498bf396deSTao Ma 
7508bf396deSTao Ma out_commit:
7518bf396deSTao Ma 	ocfs2_commit_trans(osb, handle);
7528bf396deSTao Ma out:
7538bf396deSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
7548bf396deSTao Ma 	brelse(ref_root_bh);
7558bf396deSTao Ma 
7568bf396deSTao Ma 	return ret;
7578bf396deSTao Ma }
7588bf396deSTao Ma 
7598bf396deSTao Ma int ocfs2_remove_refcount_tree(struct inode *inode, struct buffer_head *di_bh)
7608bf396deSTao Ma {
7618bf396deSTao Ma 	int ret, delete_tree = 0;
7628bf396deSTao Ma 	handle_t *handle = NULL;
7638bf396deSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7648bf396deSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
7658bf396deSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7668bf396deSTao Ma 	struct ocfs2_refcount_block *rb;
7678bf396deSTao Ma 	struct inode *alloc_inode = NULL;
7688bf396deSTao Ma 	struct buffer_head *alloc_bh = NULL;
7698bf396deSTao Ma 	struct buffer_head *blk_bh = NULL;
7708bf396deSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
7718bf396deSTao Ma 	int credits = OCFS2_REFCOUNT_TREE_REMOVE_CREDITS;
7728bf396deSTao Ma 	u64 blk = 0, bg_blkno = 0, ref_blkno = le64_to_cpu(di->i_refcount_loc);
7738bf396deSTao Ma 	u16 bit = 0;
7748bf396deSTao Ma 
7758bf396deSTao Ma 	if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL))
7768bf396deSTao Ma 		return 0;
7778bf396deSTao Ma 
7788bf396deSTao Ma 	BUG_ON(!ref_blkno);
7798bf396deSTao Ma 	ret = ocfs2_lock_refcount_tree(osb, ref_blkno, 1, &ref_tree, &blk_bh);
7808bf396deSTao Ma 	if (ret) {
7818bf396deSTao Ma 		mlog_errno(ret);
7828bf396deSTao Ma 		return ret;
7838bf396deSTao Ma 	}
7848bf396deSTao Ma 
7858bf396deSTao Ma 	rb = (struct ocfs2_refcount_block *)blk_bh->b_data;
7868bf396deSTao Ma 
7878bf396deSTao Ma 	/*
7888bf396deSTao Ma 	 * If we are the last user, we need to free the block.
7898bf396deSTao Ma 	 * So lock the allocator ahead.
7908bf396deSTao Ma 	 */
7918bf396deSTao Ma 	if (le32_to_cpu(rb->rf_count) == 1) {
7928bf396deSTao Ma 		blk = le64_to_cpu(rb->rf_blkno);
7938bf396deSTao Ma 		bit = le16_to_cpu(rb->rf_suballoc_bit);
7948bf396deSTao Ma 		bg_blkno = ocfs2_which_suballoc_group(blk, bit);
7958bf396deSTao Ma 
7968bf396deSTao Ma 		alloc_inode = ocfs2_get_system_file_inode(osb,
7978bf396deSTao Ma 					EXTENT_ALLOC_SYSTEM_INODE,
7988bf396deSTao Ma 					le16_to_cpu(rb->rf_suballoc_slot));
7998bf396deSTao Ma 		if (!alloc_inode) {
8008bf396deSTao Ma 			ret = -ENOMEM;
8018bf396deSTao Ma 			mlog_errno(ret);
8028bf396deSTao Ma 			goto out;
8038bf396deSTao Ma 		}
8048bf396deSTao Ma 		mutex_lock(&alloc_inode->i_mutex);
8058bf396deSTao Ma 
8068bf396deSTao Ma 		ret = ocfs2_inode_lock(alloc_inode, &alloc_bh, 1);
8078bf396deSTao Ma 		if (ret) {
8088bf396deSTao Ma 			mlog_errno(ret);
8098bf396deSTao Ma 			goto out_mutex;
8108bf396deSTao Ma 		}
8118bf396deSTao Ma 
8128bf396deSTao Ma 		credits += OCFS2_SUBALLOC_FREE;
8138bf396deSTao Ma 	}
8148bf396deSTao Ma 
8158bf396deSTao Ma 	handle = ocfs2_start_trans(osb, credits);
8168bf396deSTao Ma 	if (IS_ERR(handle)) {
8178bf396deSTao Ma 		ret = PTR_ERR(handle);
8188bf396deSTao Ma 		mlog_errno(ret);
8198bf396deSTao Ma 		goto out_unlock;
8208bf396deSTao Ma 	}
8218bf396deSTao Ma 
8228bf396deSTao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
8238bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
8248bf396deSTao Ma 	if (ret) {
8258bf396deSTao Ma 		mlog_errno(ret);
8268bf396deSTao Ma 		goto out_commit;
8278bf396deSTao Ma 	}
8288bf396deSTao Ma 
8298bf396deSTao Ma 	ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, blk_bh,
8308bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
8318bf396deSTao Ma 	if (ret) {
8328bf396deSTao Ma 		mlog_errno(ret);
8338bf396deSTao Ma 		goto out_commit;
8348bf396deSTao Ma 	}
8358bf396deSTao Ma 
8368bf396deSTao Ma 	spin_lock(&oi->ip_lock);
8378bf396deSTao Ma 	oi->ip_dyn_features &= ~OCFS2_HAS_REFCOUNT_FL;
8388bf396deSTao Ma 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
8398bf396deSTao Ma 	di->i_refcount_loc = 0;
8408bf396deSTao Ma 	spin_unlock(&oi->ip_lock);
8418bf396deSTao Ma 	ocfs2_journal_dirty(handle, di_bh);
8428bf396deSTao Ma 
8438bf396deSTao Ma 	le32_add_cpu(&rb->rf_count , -1);
8448bf396deSTao Ma 	ocfs2_journal_dirty(handle, blk_bh);
8458bf396deSTao Ma 
8468bf396deSTao Ma 	if (!rb->rf_count) {
8478bf396deSTao Ma 		delete_tree = 1;
8488bf396deSTao Ma 		ocfs2_erase_refcount_tree_from_list(osb, ref_tree);
8498bf396deSTao Ma 		ret = ocfs2_free_suballoc_bits(handle, alloc_inode,
8508bf396deSTao Ma 					       alloc_bh, bit, bg_blkno, 1);
8518bf396deSTao Ma 		if (ret)
8528bf396deSTao Ma 			mlog_errno(ret);
8538bf396deSTao Ma 	}
8548bf396deSTao Ma 
8558bf396deSTao Ma out_commit:
8568bf396deSTao Ma 	ocfs2_commit_trans(osb, handle);
8578bf396deSTao Ma out_unlock:
8588bf396deSTao Ma 	if (alloc_inode) {
8598bf396deSTao Ma 		ocfs2_inode_unlock(alloc_inode, 1);
8608bf396deSTao Ma 		brelse(alloc_bh);
8618bf396deSTao Ma 	}
8628bf396deSTao Ma out_mutex:
8638bf396deSTao Ma 	if (alloc_inode) {
8648bf396deSTao Ma 		mutex_unlock(&alloc_inode->i_mutex);
8658bf396deSTao Ma 		iput(alloc_inode);
8668bf396deSTao Ma 	}
8678bf396deSTao Ma out:
8688bf396deSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
8698bf396deSTao Ma 	if (delete_tree)
8708bf396deSTao Ma 		ocfs2_refcount_tree_put(ref_tree);
8718bf396deSTao Ma 	brelse(blk_bh);
8728bf396deSTao Ma 
8738bf396deSTao Ma 	return ret;
8748bf396deSTao Ma }
875e73a819dSTao Ma 
876e73a819dSTao Ma static void ocfs2_find_refcount_rec_in_rl(struct ocfs2_caching_info *ci,
877e73a819dSTao Ma 					  struct buffer_head *ref_leaf_bh,
878e73a819dSTao Ma 					  u64 cpos, unsigned int len,
879e73a819dSTao Ma 					  struct ocfs2_refcount_rec *ret_rec,
880e73a819dSTao Ma 					  int *index)
881e73a819dSTao Ma {
882e73a819dSTao Ma 	int i = 0;
883e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
884e73a819dSTao Ma 		(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
885e73a819dSTao Ma 	struct ocfs2_refcount_rec *rec = NULL;
886e73a819dSTao Ma 
887e73a819dSTao Ma 	for (; i < le16_to_cpu(rb->rf_records.rl_used); i++) {
888e73a819dSTao Ma 		rec = &rb->rf_records.rl_recs[i];
889e73a819dSTao Ma 
890e73a819dSTao Ma 		if (le64_to_cpu(rec->r_cpos) +
891e73a819dSTao Ma 		    le32_to_cpu(rec->r_clusters) <= cpos)
892e73a819dSTao Ma 			continue;
893e73a819dSTao Ma 		else if (le64_to_cpu(rec->r_cpos) > cpos)
894e73a819dSTao Ma 			break;
895e73a819dSTao Ma 
896e73a819dSTao Ma 		/* ok, cpos fail in this rec. Just return. */
897e73a819dSTao Ma 		if (ret_rec)
898e73a819dSTao Ma 			*ret_rec = *rec;
899e73a819dSTao Ma 		goto out;
900e73a819dSTao Ma 	}
901e73a819dSTao Ma 
902e73a819dSTao Ma 	if (ret_rec) {
903e73a819dSTao Ma 		/* We meet with a hole here, so fake the rec. */
904e73a819dSTao Ma 		ret_rec->r_cpos = cpu_to_le64(cpos);
905e73a819dSTao Ma 		ret_rec->r_refcount = 0;
906e73a819dSTao Ma 		if (i < le16_to_cpu(rb->rf_records.rl_used) &&
907e73a819dSTao Ma 		    le64_to_cpu(rec->r_cpos) < cpos + len)
908e73a819dSTao Ma 			ret_rec->r_clusters =
909e73a819dSTao Ma 				cpu_to_le32(le64_to_cpu(rec->r_cpos) - cpos);
910e73a819dSTao Ma 		else
911e73a819dSTao Ma 			ret_rec->r_clusters = cpu_to_le32(len);
912e73a819dSTao Ma 	}
913e73a819dSTao Ma 
914e73a819dSTao Ma out:
915e73a819dSTao Ma 	*index = i;
916e73a819dSTao Ma }
917e73a819dSTao Ma 
918e73a819dSTao Ma /*
9198b2c0dbaSTao Ma  * Try to remove refcount tree. The mechanism is:
9208b2c0dbaSTao Ma  * 1) Check whether i_clusters == 0, if no, exit.
9218b2c0dbaSTao Ma  * 2) check whether we have i_xattr_loc in dinode. if yes, exit.
9228b2c0dbaSTao Ma  * 3) Check whether we have inline xattr stored outside, if yes, exit.
9238b2c0dbaSTao Ma  * 4) Remove the tree.
9248b2c0dbaSTao Ma  */
9258b2c0dbaSTao Ma int ocfs2_try_remove_refcount_tree(struct inode *inode,
9268b2c0dbaSTao Ma 				   struct buffer_head *di_bh)
9278b2c0dbaSTao Ma {
9288b2c0dbaSTao Ma 	int ret;
9298b2c0dbaSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
9308b2c0dbaSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
9318b2c0dbaSTao Ma 
9328b2c0dbaSTao Ma 	down_write(&oi->ip_xattr_sem);
9338b2c0dbaSTao Ma 	down_write(&oi->ip_alloc_sem);
9348b2c0dbaSTao Ma 
9358b2c0dbaSTao Ma 	if (oi->ip_clusters)
9368b2c0dbaSTao Ma 		goto out;
9378b2c0dbaSTao Ma 
9388b2c0dbaSTao Ma 	if ((oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) && di->i_xattr_loc)
9398b2c0dbaSTao Ma 		goto out;
9408b2c0dbaSTao Ma 
9418b2c0dbaSTao Ma 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL &&
9428b2c0dbaSTao Ma 	    ocfs2_has_inline_xattr_value_outside(inode, di))
9438b2c0dbaSTao Ma 		goto out;
9448b2c0dbaSTao Ma 
9458b2c0dbaSTao Ma 	ret = ocfs2_remove_refcount_tree(inode, di_bh);
9468b2c0dbaSTao Ma 	if (ret)
9478b2c0dbaSTao Ma 		mlog_errno(ret);
9488b2c0dbaSTao Ma out:
9498b2c0dbaSTao Ma 	up_write(&oi->ip_alloc_sem);
9508b2c0dbaSTao Ma 	up_write(&oi->ip_xattr_sem);
9518b2c0dbaSTao Ma 	return 0;
9528b2c0dbaSTao Ma }
9538b2c0dbaSTao Ma 
9548b2c0dbaSTao Ma /*
95538a04e43STao Ma  * Find the end range for a leaf refcount block indicated by
95638a04e43STao Ma  * el->l_recs[index].e_blkno.
95738a04e43STao Ma  */
95838a04e43STao Ma static int ocfs2_get_refcount_cpos_end(struct ocfs2_caching_info *ci,
95938a04e43STao Ma 				       struct buffer_head *ref_root_bh,
96038a04e43STao Ma 				       struct ocfs2_extent_block *eb,
96138a04e43STao Ma 				       struct ocfs2_extent_list *el,
96238a04e43STao Ma 				       int index,  u32 *cpos_end)
96338a04e43STao Ma {
96438a04e43STao Ma 	int ret, i, subtree_root;
96538a04e43STao Ma 	u32 cpos;
96638a04e43STao Ma 	u64 blkno;
96738a04e43STao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
96838a04e43STao Ma 	struct ocfs2_path *left_path = NULL, *right_path = NULL;
96938a04e43STao Ma 	struct ocfs2_extent_tree et;
97038a04e43STao Ma 	struct ocfs2_extent_list *tmp_el;
97138a04e43STao Ma 
97238a04e43STao Ma 	if (index < le16_to_cpu(el->l_next_free_rec) - 1) {
97338a04e43STao Ma 		/*
97438a04e43STao Ma 		 * We have a extent rec after index, so just use the e_cpos
97538a04e43STao Ma 		 * of the next extent rec.
97638a04e43STao Ma 		 */
97738a04e43STao Ma 		*cpos_end = le32_to_cpu(el->l_recs[index+1].e_cpos);
97838a04e43STao Ma 		return 0;
97938a04e43STao Ma 	}
98038a04e43STao Ma 
98138a04e43STao Ma 	if (!eb || (eb && !eb->h_next_leaf_blk)) {
98238a04e43STao Ma 		/*
98338a04e43STao Ma 		 * We are the last extent rec, so any high cpos should
98438a04e43STao Ma 		 * be stored in this leaf refcount block.
98538a04e43STao Ma 		 */
98638a04e43STao Ma 		*cpos_end = UINT_MAX;
98738a04e43STao Ma 		return 0;
98838a04e43STao Ma 	}
98938a04e43STao Ma 
99038a04e43STao Ma 	/*
99138a04e43STao Ma 	 * If the extent block isn't the last one, we have to find
99238a04e43STao Ma 	 * the subtree root between this extent block and the next
99338a04e43STao Ma 	 * leaf extent block and get the corresponding e_cpos from
99438a04e43STao Ma 	 * the subroot. Otherwise we may corrupt the b-tree.
99538a04e43STao Ma 	 */
99638a04e43STao Ma 	ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
99738a04e43STao Ma 
99838a04e43STao Ma 	left_path = ocfs2_new_path_from_et(&et);
99938a04e43STao Ma 	if (!left_path) {
100038a04e43STao Ma 		ret = -ENOMEM;
100138a04e43STao Ma 		mlog_errno(ret);
100238a04e43STao Ma 		goto out;
100338a04e43STao Ma 	}
100438a04e43STao Ma 
100538a04e43STao Ma 	cpos = le32_to_cpu(eb->h_list.l_recs[index].e_cpos);
100638a04e43STao Ma 	ret = ocfs2_find_path(ci, left_path, cpos);
100738a04e43STao Ma 	if (ret) {
100838a04e43STao Ma 		mlog_errno(ret);
100938a04e43STao Ma 		goto out;
101038a04e43STao Ma 	}
101138a04e43STao Ma 
101238a04e43STao Ma 	right_path = ocfs2_new_path_from_path(left_path);
101338a04e43STao Ma 	if (!right_path) {
101438a04e43STao Ma 		ret = -ENOMEM;
101538a04e43STao Ma 		mlog_errno(ret);
101638a04e43STao Ma 		goto out;
101738a04e43STao Ma 	}
101838a04e43STao Ma 
101938a04e43STao Ma 	ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, &cpos);
102038a04e43STao Ma 	if (ret) {
102138a04e43STao Ma 		mlog_errno(ret);
102238a04e43STao Ma 		goto out;
102338a04e43STao Ma 	}
102438a04e43STao Ma 
102538a04e43STao Ma 	ret = ocfs2_find_path(ci, right_path, cpos);
102638a04e43STao Ma 	if (ret) {
102738a04e43STao Ma 		mlog_errno(ret);
102838a04e43STao Ma 		goto out;
102938a04e43STao Ma 	}
103038a04e43STao Ma 
103138a04e43STao Ma 	subtree_root = ocfs2_find_subtree_root(&et, left_path,
103238a04e43STao Ma 					       right_path);
103338a04e43STao Ma 
103438a04e43STao Ma 	tmp_el = left_path->p_node[subtree_root].el;
103538a04e43STao Ma 	blkno = left_path->p_node[subtree_root+1].bh->b_blocknr;
103638a04e43STao Ma 	for (i = 0; i < le32_to_cpu(tmp_el->l_next_free_rec); i++) {
103738a04e43STao Ma 		if (le64_to_cpu(tmp_el->l_recs[i].e_blkno) == blkno) {
103838a04e43STao Ma 			*cpos_end = le32_to_cpu(tmp_el->l_recs[i+1].e_cpos);
103938a04e43STao Ma 			break;
104038a04e43STao Ma 		}
104138a04e43STao Ma 	}
104238a04e43STao Ma 
104338a04e43STao Ma 	BUG_ON(i == le32_to_cpu(tmp_el->l_next_free_rec));
104438a04e43STao Ma 
104538a04e43STao Ma out:
104638a04e43STao Ma 	ocfs2_free_path(left_path);
104738a04e43STao Ma 	ocfs2_free_path(right_path);
104838a04e43STao Ma 	return ret;
104938a04e43STao Ma }
105038a04e43STao Ma 
105138a04e43STao Ma /*
1052e73a819dSTao Ma  * Given a cpos and len, try to find the refcount record which contains cpos.
1053e73a819dSTao Ma  * 1. If cpos can be found in one refcount record, return the record.
1054e73a819dSTao Ma  * 2. If cpos can't be found, return a fake record which start from cpos
1055e73a819dSTao Ma  *    and end at a small value between cpos+len and start of the next record.
1056e73a819dSTao Ma  *    This fake record has r_refcount = 0.
1057e73a819dSTao Ma  */
1058e73a819dSTao Ma static int ocfs2_get_refcount_rec(struct ocfs2_caching_info *ci,
1059e73a819dSTao Ma 				  struct buffer_head *ref_root_bh,
1060e73a819dSTao Ma 				  u64 cpos, unsigned int len,
1061e73a819dSTao Ma 				  struct ocfs2_refcount_rec *ret_rec,
1062e73a819dSTao Ma 				  int *index,
1063e73a819dSTao Ma 				  struct buffer_head **ret_bh)
1064e73a819dSTao Ma {
1065e73a819dSTao Ma 	int ret = 0, i, found;
106638a04e43STao Ma 	u32 low_cpos, uninitialized_var(cpos_end);
1067e73a819dSTao Ma 	struct ocfs2_extent_list *el;
106838a04e43STao Ma 	struct ocfs2_extent_rec *rec = NULL;
106938a04e43STao Ma 	struct ocfs2_extent_block *eb = NULL;
1070e73a819dSTao Ma 	struct buffer_head *eb_bh = NULL, *ref_leaf_bh = NULL;
1071e73a819dSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1072e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1073e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1074e73a819dSTao Ma 
1075e73a819dSTao Ma 	if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)) {
1076e73a819dSTao Ma 		ocfs2_find_refcount_rec_in_rl(ci, ref_root_bh, cpos, len,
1077e73a819dSTao Ma 					      ret_rec, index);
1078e73a819dSTao Ma 		*ret_bh = ref_root_bh;
1079e73a819dSTao Ma 		get_bh(ref_root_bh);
1080e73a819dSTao Ma 		return 0;
1081e73a819dSTao Ma 	}
1082e73a819dSTao Ma 
1083e73a819dSTao Ma 	el = &rb->rf_list;
1084e73a819dSTao Ma 	low_cpos = cpos & OCFS2_32BIT_POS_MASK;
1085e73a819dSTao Ma 
1086e73a819dSTao Ma 	if (el->l_tree_depth) {
1087e73a819dSTao Ma 		ret = ocfs2_find_leaf(ci, el, low_cpos, &eb_bh);
1088e73a819dSTao Ma 		if (ret) {
1089e73a819dSTao Ma 			mlog_errno(ret);
1090e73a819dSTao Ma 			goto out;
1091e73a819dSTao Ma 		}
1092e73a819dSTao Ma 
1093e73a819dSTao Ma 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1094e73a819dSTao Ma 		el = &eb->h_list;
1095e73a819dSTao Ma 
1096e73a819dSTao Ma 		if (el->l_tree_depth) {
1097e73a819dSTao Ma 			ocfs2_error(sb,
1098e73a819dSTao Ma 			"refcount tree %llu has non zero tree "
1099e73a819dSTao Ma 			"depth in leaf btree tree block %llu\n",
1100e73a819dSTao Ma 			(unsigned long long)ocfs2_metadata_cache_owner(ci),
1101e73a819dSTao Ma 			(unsigned long long)eb_bh->b_blocknr);
1102e73a819dSTao Ma 			ret = -EROFS;
1103e73a819dSTao Ma 			goto out;
1104e73a819dSTao Ma 		}
1105e73a819dSTao Ma 	}
1106e73a819dSTao Ma 
1107e73a819dSTao Ma 	found = 0;
1108e73a819dSTao Ma 	for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
1109e73a819dSTao Ma 		rec = &el->l_recs[i];
1110e73a819dSTao Ma 
1111e73a819dSTao Ma 		if (le32_to_cpu(rec->e_cpos) <= low_cpos) {
1112e73a819dSTao Ma 			found = 1;
1113e73a819dSTao Ma 			break;
1114e73a819dSTao Ma 		}
1115e73a819dSTao Ma 	}
1116e73a819dSTao Ma 
111738a04e43STao Ma 	if (found) {
111838a04e43STao Ma 		ret = ocfs2_get_refcount_cpos_end(ci, ref_root_bh,
111938a04e43STao Ma 						  eb, el, i, &cpos_end);
112038a04e43STao Ma 		if (ret) {
112138a04e43STao Ma 			mlog_errno(ret);
112238a04e43STao Ma 			goto out;
112338a04e43STao Ma 		}
1124e73a819dSTao Ma 
112538a04e43STao Ma 		if (cpos_end < low_cpos + len)
112638a04e43STao Ma 			len = cpos_end - low_cpos;
1127e73a819dSTao Ma 	}
1128e73a819dSTao Ma 
1129e73a819dSTao Ma 	ret = ocfs2_read_refcount_block(ci, le64_to_cpu(rec->e_blkno),
1130e73a819dSTao Ma 					&ref_leaf_bh);
1131e73a819dSTao Ma 	if (ret) {
1132e73a819dSTao Ma 		mlog_errno(ret);
1133e73a819dSTao Ma 		goto out;
1134e73a819dSTao Ma 	}
1135e73a819dSTao Ma 
1136e73a819dSTao Ma 	ocfs2_find_refcount_rec_in_rl(ci, ref_leaf_bh, cpos, len,
1137e73a819dSTao Ma 				      ret_rec, index);
1138e73a819dSTao Ma 	*ret_bh = ref_leaf_bh;
1139e73a819dSTao Ma out:
1140e73a819dSTao Ma 	brelse(eb_bh);
1141e73a819dSTao Ma 	return ret;
1142e73a819dSTao Ma }
1143e73a819dSTao Ma 
1144e73a819dSTao Ma enum ocfs2_ref_rec_contig {
1145e73a819dSTao Ma 	REF_CONTIG_NONE = 0,
1146e73a819dSTao Ma 	REF_CONTIG_LEFT,
1147e73a819dSTao Ma 	REF_CONTIG_RIGHT,
1148e73a819dSTao Ma 	REF_CONTIG_LEFTRIGHT,
1149e73a819dSTao Ma };
1150e73a819dSTao Ma 
1151e73a819dSTao Ma static enum ocfs2_ref_rec_contig
1152e73a819dSTao Ma 	ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block *rb,
1153e73a819dSTao Ma 				    int index)
1154e73a819dSTao Ma {
1155e73a819dSTao Ma 	if ((rb->rf_records.rl_recs[index].r_refcount ==
1156e73a819dSTao Ma 	    rb->rf_records.rl_recs[index + 1].r_refcount) &&
1157e73a819dSTao Ma 	    (le64_to_cpu(rb->rf_records.rl_recs[index].r_cpos) +
1158e73a819dSTao Ma 	    le32_to_cpu(rb->rf_records.rl_recs[index].r_clusters) ==
1159e73a819dSTao Ma 	    le64_to_cpu(rb->rf_records.rl_recs[index + 1].r_cpos)))
1160e73a819dSTao Ma 		return REF_CONTIG_RIGHT;
1161e73a819dSTao Ma 
1162e73a819dSTao Ma 	return REF_CONTIG_NONE;
1163e73a819dSTao Ma }
1164e73a819dSTao Ma 
1165e73a819dSTao Ma static enum ocfs2_ref_rec_contig
1166e73a819dSTao Ma 	ocfs2_refcount_rec_contig(struct ocfs2_refcount_block *rb,
1167e73a819dSTao Ma 				  int index)
1168e73a819dSTao Ma {
1169e73a819dSTao Ma 	enum ocfs2_ref_rec_contig ret = REF_CONTIG_NONE;
1170e73a819dSTao Ma 
1171e73a819dSTao Ma 	if (index < le16_to_cpu(rb->rf_records.rl_used) - 1)
1172e73a819dSTao Ma 		ret = ocfs2_refcount_rec_adjacent(rb, index);
1173e73a819dSTao Ma 
1174e73a819dSTao Ma 	if (index > 0) {
1175e73a819dSTao Ma 		enum ocfs2_ref_rec_contig tmp;
1176e73a819dSTao Ma 
1177e73a819dSTao Ma 		tmp = ocfs2_refcount_rec_adjacent(rb, index - 1);
1178e73a819dSTao Ma 
1179e73a819dSTao Ma 		if (tmp == REF_CONTIG_RIGHT) {
1180e73a819dSTao Ma 			if (ret == REF_CONTIG_RIGHT)
1181e73a819dSTao Ma 				ret = REF_CONTIG_LEFTRIGHT;
1182e73a819dSTao Ma 			else
1183e73a819dSTao Ma 				ret = REF_CONTIG_LEFT;
1184e73a819dSTao Ma 		}
1185e73a819dSTao Ma 	}
1186e73a819dSTao Ma 
1187e73a819dSTao Ma 	return ret;
1188e73a819dSTao Ma }
1189e73a819dSTao Ma 
1190e73a819dSTao Ma static void ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block *rb,
1191e73a819dSTao Ma 					   int index)
1192e73a819dSTao Ma {
1193e73a819dSTao Ma 	BUG_ON(rb->rf_records.rl_recs[index].r_refcount !=
1194e73a819dSTao Ma 	       rb->rf_records.rl_recs[index+1].r_refcount);
1195e73a819dSTao Ma 
1196e73a819dSTao Ma 	le32_add_cpu(&rb->rf_records.rl_recs[index].r_clusters,
1197e73a819dSTao Ma 		     le32_to_cpu(rb->rf_records.rl_recs[index+1].r_clusters));
1198e73a819dSTao Ma 
1199e73a819dSTao Ma 	if (index < le16_to_cpu(rb->rf_records.rl_used) - 2)
1200e73a819dSTao Ma 		memmove(&rb->rf_records.rl_recs[index + 1],
1201e73a819dSTao Ma 			&rb->rf_records.rl_recs[index + 2],
1202e73a819dSTao Ma 			sizeof(struct ocfs2_refcount_rec) *
1203e73a819dSTao Ma 			(le16_to_cpu(rb->rf_records.rl_used) - index - 2));
1204e73a819dSTao Ma 
1205e73a819dSTao Ma 	memset(&rb->rf_records.rl_recs[le16_to_cpu(rb->rf_records.rl_used) - 1],
1206e73a819dSTao Ma 	       0, sizeof(struct ocfs2_refcount_rec));
1207e73a819dSTao Ma 	le16_add_cpu(&rb->rf_records.rl_used, -1);
1208e73a819dSTao Ma }
1209e73a819dSTao Ma 
1210e73a819dSTao Ma /*
1211e73a819dSTao Ma  * Merge the refcount rec if we are contiguous with the adjacent recs.
1212e73a819dSTao Ma  */
1213e73a819dSTao Ma static void ocfs2_refcount_rec_merge(struct ocfs2_refcount_block *rb,
1214e73a819dSTao Ma 				     int index)
1215e73a819dSTao Ma {
1216e73a819dSTao Ma 	enum ocfs2_ref_rec_contig contig =
1217e73a819dSTao Ma 				ocfs2_refcount_rec_contig(rb, index);
1218e73a819dSTao Ma 
1219e73a819dSTao Ma 	if (contig == REF_CONTIG_NONE)
1220e73a819dSTao Ma 		return;
1221e73a819dSTao Ma 
1222e73a819dSTao Ma 	if (contig == REF_CONTIG_LEFT || contig == REF_CONTIG_LEFTRIGHT) {
1223e73a819dSTao Ma 		BUG_ON(index == 0);
1224e73a819dSTao Ma 		index--;
1225e73a819dSTao Ma 	}
1226e73a819dSTao Ma 
1227e73a819dSTao Ma 	ocfs2_rotate_refcount_rec_left(rb, index);
1228e73a819dSTao Ma 
1229e73a819dSTao Ma 	if (contig == REF_CONTIG_LEFTRIGHT)
1230e73a819dSTao Ma 		ocfs2_rotate_refcount_rec_left(rb, index);
1231e73a819dSTao Ma }
1232e73a819dSTao Ma 
12331823cb0bSTao Ma /*
12341823cb0bSTao Ma  * Change the refcount indexed by "index" in ref_bh.
12351823cb0bSTao Ma  * If refcount reaches 0, remove it.
12361823cb0bSTao Ma  */
1237e73a819dSTao Ma static int ocfs2_change_refcount_rec(handle_t *handle,
1238e73a819dSTao Ma 				     struct ocfs2_caching_info *ci,
1239e73a819dSTao Ma 				     struct buffer_head *ref_leaf_bh,
12407540c1a7STao Ma 				     int index, int merge, int change)
1241e73a819dSTao Ma {
1242e73a819dSTao Ma 	int ret;
1243e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1244e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
12451823cb0bSTao Ma 	struct ocfs2_refcount_list *rl = &rb->rf_records;
12461823cb0bSTao Ma 	struct ocfs2_refcount_rec *rec = &rl->rl_recs[index];
1247e73a819dSTao Ma 
1248e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1249e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1250e73a819dSTao Ma 	if (ret) {
1251e73a819dSTao Ma 		mlog_errno(ret);
1252e73a819dSTao Ma 		goto out;
1253e73a819dSTao Ma 	}
1254e73a819dSTao Ma 
1255e73a819dSTao Ma 	mlog(0, "change index %d, old count %u, change %d\n", index,
1256e73a819dSTao Ma 	     le32_to_cpu(rec->r_refcount), change);
1257e73a819dSTao Ma 	le32_add_cpu(&rec->r_refcount, change);
1258e73a819dSTao Ma 
12591823cb0bSTao Ma 	if (!rec->r_refcount) {
12601823cb0bSTao Ma 		if (index != le16_to_cpu(rl->rl_used) - 1) {
12611823cb0bSTao Ma 			memmove(rec, rec + 1,
12621823cb0bSTao Ma 				(le16_to_cpu(rl->rl_used) - index - 1) *
12631823cb0bSTao Ma 				sizeof(struct ocfs2_refcount_rec));
12641823cb0bSTao Ma 			memset(&rl->rl_recs[le16_to_cpu(rl->rl_used) - 1],
12651823cb0bSTao Ma 			       0, sizeof(struct ocfs2_refcount_rec));
12661823cb0bSTao Ma 		}
12671823cb0bSTao Ma 
12681823cb0bSTao Ma 		le16_add_cpu(&rl->rl_used, -1);
12697540c1a7STao Ma 	} else if (merge)
1270e73a819dSTao Ma 		ocfs2_refcount_rec_merge(rb, index);
1271e73a819dSTao Ma 
1272ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1273e73a819dSTao Ma out:
1274e73a819dSTao Ma 	return ret;
1275e73a819dSTao Ma }
1276e73a819dSTao Ma 
1277e73a819dSTao Ma static int ocfs2_expand_inline_ref_root(handle_t *handle,
1278e73a819dSTao Ma 					struct ocfs2_caching_info *ci,
1279e73a819dSTao Ma 					struct buffer_head *ref_root_bh,
1280e73a819dSTao Ma 					struct buffer_head **ref_leaf_bh,
1281e73a819dSTao Ma 					struct ocfs2_alloc_context *meta_ac)
1282e73a819dSTao Ma {
1283e73a819dSTao Ma 	int ret;
1284e73a819dSTao Ma 	u16 suballoc_bit_start;
1285e73a819dSTao Ma 	u32 num_got;
1286e73a819dSTao Ma 	u64 blkno;
1287e73a819dSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1288e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1289e73a819dSTao Ma 	struct ocfs2_refcount_block *new_rb;
1290e73a819dSTao Ma 	struct ocfs2_refcount_block *root_rb =
1291e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1292e73a819dSTao Ma 
1293e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
1294e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1295e73a819dSTao Ma 	if (ret) {
1296e73a819dSTao Ma 		mlog_errno(ret);
1297e73a819dSTao Ma 		goto out;
1298e73a819dSTao Ma 	}
1299e73a819dSTao Ma 
1300e73a819dSTao Ma 	ret = ocfs2_claim_metadata(OCFS2_SB(sb), handle, meta_ac, 1,
1301e73a819dSTao Ma 				   &suballoc_bit_start, &num_got,
1302e73a819dSTao Ma 				   &blkno);
1303e73a819dSTao Ma 	if (ret) {
1304e73a819dSTao Ma 		mlog_errno(ret);
1305e73a819dSTao Ma 		goto out;
1306e73a819dSTao Ma 	}
1307e73a819dSTao Ma 
1308e73a819dSTao Ma 	new_bh = sb_getblk(sb, blkno);
1309e73a819dSTao Ma 	if (new_bh == NULL) {
1310e73a819dSTao Ma 		ret = -EIO;
1311e73a819dSTao Ma 		mlog_errno(ret);
1312e73a819dSTao Ma 		goto out;
1313e73a819dSTao Ma 	}
1314e73a819dSTao Ma 	ocfs2_set_new_buffer_uptodate(ci, new_bh);
1315e73a819dSTao Ma 
1316e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, new_bh,
1317e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_CREATE);
1318e73a819dSTao Ma 	if (ret) {
1319e73a819dSTao Ma 		mlog_errno(ret);
1320e73a819dSTao Ma 		goto out;
1321e73a819dSTao Ma 	}
1322e73a819dSTao Ma 
1323e73a819dSTao Ma 	/*
1324e73a819dSTao Ma 	 * Initialize ocfs2_refcount_block.
1325e73a819dSTao Ma 	 * It should contain the same information as the old root.
1326e73a819dSTao Ma 	 * so just memcpy it and change the corresponding field.
1327e73a819dSTao Ma 	 */
1328e73a819dSTao Ma 	memcpy(new_bh->b_data, ref_root_bh->b_data, sb->s_blocksize);
1329e73a819dSTao Ma 
1330e73a819dSTao Ma 	new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1331b89c5428STiger Yang 	new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
1332e73a819dSTao Ma 	new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1333e73a819dSTao Ma 	new_rb->rf_blkno = cpu_to_le64(blkno);
1334e73a819dSTao Ma 	new_rb->rf_cpos = cpu_to_le32(0);
1335e73a819dSTao Ma 	new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
1336e73a819dSTao Ma 	new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
1337e73a819dSTao Ma 	ocfs2_journal_dirty(handle, new_bh);
1338e73a819dSTao Ma 
1339e73a819dSTao Ma 	/* Now change the root. */
1340e73a819dSTao Ma 	memset(&root_rb->rf_list, 0, sb->s_blocksize -
1341e73a819dSTao Ma 	       offsetof(struct ocfs2_refcount_block, rf_list));
1342e73a819dSTao Ma 	root_rb->rf_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_rb(sb));
1343e73a819dSTao Ma 	root_rb->rf_clusters = cpu_to_le32(1);
1344e73a819dSTao Ma 	root_rb->rf_list.l_next_free_rec = cpu_to_le16(1);
1345e73a819dSTao Ma 	root_rb->rf_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
1346e73a819dSTao Ma 	root_rb->rf_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
1347e73a819dSTao Ma 	root_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_TREE_FL);
1348e73a819dSTao Ma 
1349e73a819dSTao Ma 	ocfs2_journal_dirty(handle, ref_root_bh);
1350e73a819dSTao Ma 
1351e73a819dSTao Ma 	mlog(0, "new leaf block %llu, used %u\n", (unsigned long long)blkno,
1352e73a819dSTao Ma 	     le16_to_cpu(new_rb->rf_records.rl_used));
1353e73a819dSTao Ma 
1354e73a819dSTao Ma 	*ref_leaf_bh = new_bh;
1355e73a819dSTao Ma 	new_bh = NULL;
1356e73a819dSTao Ma out:
1357e73a819dSTao Ma 	brelse(new_bh);
1358e73a819dSTao Ma 	return ret;
1359e73a819dSTao Ma }
1360e73a819dSTao Ma 
1361e73a819dSTao Ma static int ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec *prev,
1362e73a819dSTao Ma 					   struct ocfs2_refcount_rec *next)
1363e73a819dSTao Ma {
1364e73a819dSTao Ma 	if (ocfs2_get_ref_rec_low_cpos(prev) + le32_to_cpu(prev->r_clusters) <=
1365e73a819dSTao Ma 		ocfs2_get_ref_rec_low_cpos(next))
1366e73a819dSTao Ma 		return 1;
1367e73a819dSTao Ma 
1368e73a819dSTao Ma 	return 0;
1369e73a819dSTao Ma }
1370e73a819dSTao Ma 
1371e73a819dSTao Ma static int cmp_refcount_rec_by_low_cpos(const void *a, const void *b)
1372e73a819dSTao Ma {
1373e73a819dSTao Ma 	const struct ocfs2_refcount_rec *l = a, *r = b;
1374e73a819dSTao Ma 	u32 l_cpos = ocfs2_get_ref_rec_low_cpos(l);
1375e73a819dSTao Ma 	u32 r_cpos = ocfs2_get_ref_rec_low_cpos(r);
1376e73a819dSTao Ma 
1377e73a819dSTao Ma 	if (l_cpos > r_cpos)
1378e73a819dSTao Ma 		return 1;
1379e73a819dSTao Ma 	if (l_cpos < r_cpos)
1380e73a819dSTao Ma 		return -1;
1381e73a819dSTao Ma 	return 0;
1382e73a819dSTao Ma }
1383e73a819dSTao Ma 
1384e73a819dSTao Ma static int cmp_refcount_rec_by_cpos(const void *a, const void *b)
1385e73a819dSTao Ma {
1386e73a819dSTao Ma 	const struct ocfs2_refcount_rec *l = a, *r = b;
1387e73a819dSTao Ma 	u64 l_cpos = le64_to_cpu(l->r_cpos);
1388e73a819dSTao Ma 	u64 r_cpos = le64_to_cpu(r->r_cpos);
1389e73a819dSTao Ma 
1390e73a819dSTao Ma 	if (l_cpos > r_cpos)
1391e73a819dSTao Ma 		return 1;
1392e73a819dSTao Ma 	if (l_cpos < r_cpos)
1393e73a819dSTao Ma 		return -1;
1394e73a819dSTao Ma 	return 0;
1395e73a819dSTao Ma }
1396e73a819dSTao Ma 
1397e73a819dSTao Ma static void swap_refcount_rec(void *a, void *b, int size)
1398e73a819dSTao Ma {
1399e73a819dSTao Ma 	struct ocfs2_refcount_rec *l = a, *r = b, tmp;
1400e73a819dSTao Ma 
1401e73a819dSTao Ma 	tmp = *(struct ocfs2_refcount_rec *)l;
1402e73a819dSTao Ma 	*(struct ocfs2_refcount_rec *)l =
1403e73a819dSTao Ma 			*(struct ocfs2_refcount_rec *)r;
1404e73a819dSTao Ma 	*(struct ocfs2_refcount_rec *)r = tmp;
1405e73a819dSTao Ma }
1406e73a819dSTao Ma 
1407e73a819dSTao Ma /*
1408e73a819dSTao Ma  * The refcount cpos are ordered by their 64bit cpos,
1409e73a819dSTao Ma  * But we will use the low 32 bit to be the e_cpos in the b-tree.
1410e73a819dSTao Ma  * So we need to make sure that this pos isn't intersected with others.
1411e73a819dSTao Ma  *
1412e73a819dSTao Ma  * Note: The refcount block is already sorted by their low 32 bit cpos,
1413e73a819dSTao Ma  *       So just try the middle pos first, and we will exit when we find
1414e73a819dSTao Ma  *       the good position.
1415e73a819dSTao Ma  */
1416e73a819dSTao Ma static int ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list *rl,
1417e73a819dSTao Ma 					 u32 *split_pos, int *split_index)
1418e73a819dSTao Ma {
1419e73a819dSTao Ma 	int num_used = le16_to_cpu(rl->rl_used);
1420e73a819dSTao Ma 	int delta, middle = num_used / 2;
1421e73a819dSTao Ma 
1422e73a819dSTao Ma 	for (delta = 0; delta < middle; delta++) {
1423e73a819dSTao Ma 		/* Let's check delta earlier than middle */
1424e73a819dSTao Ma 		if (ocfs2_refcount_rec_no_intersect(
1425e73a819dSTao Ma 					&rl->rl_recs[middle - delta - 1],
1426e73a819dSTao Ma 					&rl->rl_recs[middle - delta])) {
1427e73a819dSTao Ma 			*split_index = middle - delta;
1428e73a819dSTao Ma 			break;
1429e73a819dSTao Ma 		}
1430e73a819dSTao Ma 
1431e73a819dSTao Ma 		/* For even counts, don't walk off the end */
1432e73a819dSTao Ma 		if ((middle + delta + 1) == num_used)
1433e73a819dSTao Ma 			continue;
1434e73a819dSTao Ma 
1435e73a819dSTao Ma 		/* Now try delta past middle */
1436e73a819dSTao Ma 		if (ocfs2_refcount_rec_no_intersect(
1437e73a819dSTao Ma 					&rl->rl_recs[middle + delta],
1438e73a819dSTao Ma 					&rl->rl_recs[middle + delta + 1])) {
1439e73a819dSTao Ma 			*split_index = middle + delta + 1;
1440e73a819dSTao Ma 			break;
1441e73a819dSTao Ma 		}
1442e73a819dSTao Ma 	}
1443e73a819dSTao Ma 
1444e73a819dSTao Ma 	if (delta >= middle)
1445e73a819dSTao Ma 		return -ENOSPC;
1446e73a819dSTao Ma 
1447e73a819dSTao Ma 	*split_pos = ocfs2_get_ref_rec_low_cpos(&rl->rl_recs[*split_index]);
1448e73a819dSTao Ma 	return 0;
1449e73a819dSTao Ma }
1450e73a819dSTao Ma 
1451e73a819dSTao Ma static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
1452e73a819dSTao Ma 					    struct buffer_head *new_bh,
1453e73a819dSTao Ma 					    u32 *split_cpos)
1454e73a819dSTao Ma {
1455e73a819dSTao Ma 	int split_index = 0, num_moved, ret;
1456e73a819dSTao Ma 	u32 cpos = 0;
1457e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1458e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1459e73a819dSTao Ma 	struct ocfs2_refcount_list *rl = &rb->rf_records;
1460e73a819dSTao Ma 	struct ocfs2_refcount_block *new_rb =
1461e73a819dSTao Ma 			(struct ocfs2_refcount_block *)new_bh->b_data;
1462e73a819dSTao Ma 	struct ocfs2_refcount_list *new_rl = &new_rb->rf_records;
1463e73a819dSTao Ma 
1464e73a819dSTao Ma 	mlog(0, "split old leaf refcount block %llu, count = %u, used = %u\n",
1465e73a819dSTao Ma 	     (unsigned long long)ref_leaf_bh->b_blocknr,
1466e73a819dSTao Ma 	     le32_to_cpu(rl->rl_count), le32_to_cpu(rl->rl_used));
1467e73a819dSTao Ma 
1468e73a819dSTao Ma 	/*
1469e73a819dSTao Ma 	 * XXX: Improvement later.
1470e73a819dSTao Ma 	 * If we know all the high 32 bit cpos is the same, no need to sort.
1471e73a819dSTao Ma 	 *
1472e73a819dSTao Ma 	 * In order to make the whole process safe, we do:
1473e73a819dSTao Ma 	 * 1. sort the entries by their low 32 bit cpos first so that we can
1474e73a819dSTao Ma 	 *    find the split cpos easily.
1475e73a819dSTao Ma 	 * 2. call ocfs2_insert_extent to insert the new refcount block.
1476e73a819dSTao Ma 	 * 3. move the refcount rec to the new block.
1477e73a819dSTao Ma 	 * 4. sort the entries by their 64 bit cpos.
1478e73a819dSTao Ma 	 * 5. dirty the new_rb and rb.
1479e73a819dSTao Ma 	 */
1480e73a819dSTao Ma 	sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
1481e73a819dSTao Ma 	     sizeof(struct ocfs2_refcount_rec),
1482e73a819dSTao Ma 	     cmp_refcount_rec_by_low_cpos, swap_refcount_rec);
1483e73a819dSTao Ma 
1484e73a819dSTao Ma 	ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index);
1485e73a819dSTao Ma 	if (ret) {
1486e73a819dSTao Ma 		mlog_errno(ret);
1487e73a819dSTao Ma 		return ret;
1488e73a819dSTao Ma 	}
1489e73a819dSTao Ma 
1490e73a819dSTao Ma 	new_rb->rf_cpos = cpu_to_le32(cpos);
1491e73a819dSTao Ma 
1492e73a819dSTao Ma 	/* move refcount records starting from split_index to the new block. */
1493e73a819dSTao Ma 	num_moved = le16_to_cpu(rl->rl_used) - split_index;
1494e73a819dSTao Ma 	memcpy(new_rl->rl_recs, &rl->rl_recs[split_index],
1495e73a819dSTao Ma 	       num_moved * sizeof(struct ocfs2_refcount_rec));
1496e73a819dSTao Ma 
1497e73a819dSTao Ma 	/*ok, remove the entries we just moved over to the other block. */
1498e73a819dSTao Ma 	memset(&rl->rl_recs[split_index], 0,
1499e73a819dSTao Ma 	       num_moved * sizeof(struct ocfs2_refcount_rec));
1500e73a819dSTao Ma 
1501e73a819dSTao Ma 	/* change old and new rl_used accordingly. */
1502e73a819dSTao Ma 	le16_add_cpu(&rl->rl_used, -num_moved);
150312d4cec9STao Ma 	new_rl->rl_used = cpu_to_le16(num_moved);
1504e73a819dSTao Ma 
1505e73a819dSTao Ma 	sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
1506e73a819dSTao Ma 	     sizeof(struct ocfs2_refcount_rec),
1507e73a819dSTao Ma 	     cmp_refcount_rec_by_cpos, swap_refcount_rec);
1508e73a819dSTao Ma 
1509e73a819dSTao Ma 	sort(&new_rl->rl_recs, le16_to_cpu(new_rl->rl_used),
1510e73a819dSTao Ma 	     sizeof(struct ocfs2_refcount_rec),
1511e73a819dSTao Ma 	     cmp_refcount_rec_by_cpos, swap_refcount_rec);
1512e73a819dSTao Ma 
1513e73a819dSTao Ma 	*split_cpos = cpos;
1514e73a819dSTao Ma 	return 0;
1515e73a819dSTao Ma }
1516e73a819dSTao Ma 
1517e73a819dSTao Ma static int ocfs2_new_leaf_refcount_block(handle_t *handle,
1518e73a819dSTao Ma 					 struct ocfs2_caching_info *ci,
1519e73a819dSTao Ma 					 struct buffer_head *ref_root_bh,
1520e73a819dSTao Ma 					 struct buffer_head *ref_leaf_bh,
1521e73a819dSTao Ma 					 struct ocfs2_alloc_context *meta_ac)
1522e73a819dSTao Ma {
1523e73a819dSTao Ma 	int ret;
1524e73a819dSTao Ma 	u16 suballoc_bit_start;
1525e73a819dSTao Ma 	u32 num_got, new_cpos;
1526e73a819dSTao Ma 	u64 blkno;
1527e73a819dSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1528e73a819dSTao Ma 	struct ocfs2_refcount_block *root_rb =
1529e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1530e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1531e73a819dSTao Ma 	struct ocfs2_refcount_block *new_rb;
1532e73a819dSTao Ma 	struct ocfs2_extent_tree ref_et;
1533e73a819dSTao Ma 
1534e73a819dSTao Ma 	BUG_ON(!(le32_to_cpu(root_rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL));
1535e73a819dSTao Ma 
1536e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
1537e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1538e73a819dSTao Ma 	if (ret) {
1539e73a819dSTao Ma 		mlog_errno(ret);
1540e73a819dSTao Ma 		goto out;
1541e73a819dSTao Ma 	}
1542e73a819dSTao Ma 
1543e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1544e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1545e73a819dSTao Ma 	if (ret) {
1546e73a819dSTao Ma 		mlog_errno(ret);
1547e73a819dSTao Ma 		goto out;
1548e73a819dSTao Ma 	}
1549e73a819dSTao Ma 
1550e73a819dSTao Ma 	ret = ocfs2_claim_metadata(OCFS2_SB(sb), handle, meta_ac, 1,
1551e73a819dSTao Ma 				   &suballoc_bit_start, &num_got,
1552e73a819dSTao Ma 				   &blkno);
1553e73a819dSTao Ma 	if (ret) {
1554e73a819dSTao Ma 		mlog_errno(ret);
1555e73a819dSTao Ma 		goto out;
1556e73a819dSTao Ma 	}
1557e73a819dSTao Ma 
1558e73a819dSTao Ma 	new_bh = sb_getblk(sb, blkno);
1559e73a819dSTao Ma 	if (new_bh == NULL) {
1560e73a819dSTao Ma 		ret = -EIO;
1561e73a819dSTao Ma 		mlog_errno(ret);
1562e73a819dSTao Ma 		goto out;
1563e73a819dSTao Ma 	}
1564e73a819dSTao Ma 	ocfs2_set_new_buffer_uptodate(ci, new_bh);
1565e73a819dSTao Ma 
1566e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, new_bh,
1567e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_CREATE);
1568e73a819dSTao Ma 	if (ret) {
1569e73a819dSTao Ma 		mlog_errno(ret);
1570e73a819dSTao Ma 		goto out;
1571e73a819dSTao Ma 	}
1572e73a819dSTao Ma 
1573e73a819dSTao Ma 	/* Initialize ocfs2_refcount_block. */
1574e73a819dSTao Ma 	new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1575e73a819dSTao Ma 	memset(new_rb, 0, sb->s_blocksize);
1576e73a819dSTao Ma 	strcpy((void *)new_rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
1577b89c5428STiger Yang 	new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
1578e73a819dSTao Ma 	new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1579e73a819dSTao Ma 	new_rb->rf_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
1580e73a819dSTao Ma 	new_rb->rf_blkno = cpu_to_le64(blkno);
1581e73a819dSTao Ma 	new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
1582e73a819dSTao Ma 	new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
1583e73a819dSTao Ma 	new_rb->rf_records.rl_count =
1584e73a819dSTao Ma 				cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
1585e73a819dSTao Ma 	new_rb->rf_generation = root_rb->rf_generation;
1586e73a819dSTao Ma 
1587e73a819dSTao Ma 	ret = ocfs2_divide_leaf_refcount_block(ref_leaf_bh, new_bh, &new_cpos);
1588e73a819dSTao Ma 	if (ret) {
1589e73a819dSTao Ma 		mlog_errno(ret);
1590e73a819dSTao Ma 		goto out;
1591e73a819dSTao Ma 	}
1592e73a819dSTao Ma 
1593e73a819dSTao Ma 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1594e73a819dSTao Ma 	ocfs2_journal_dirty(handle, new_bh);
1595e73a819dSTao Ma 
1596e73a819dSTao Ma 	ocfs2_init_refcount_extent_tree(&ref_et, ci, ref_root_bh);
1597e73a819dSTao Ma 
1598e73a819dSTao Ma 	mlog(0, "insert new leaf block %llu at %u\n",
1599e73a819dSTao Ma 	     (unsigned long long)new_bh->b_blocknr, new_cpos);
1600e73a819dSTao Ma 
1601e73a819dSTao Ma 	/* Insert the new leaf block with the specific offset cpos. */
1602e73a819dSTao Ma 	ret = ocfs2_insert_extent(handle, &ref_et, new_cpos, new_bh->b_blocknr,
1603e73a819dSTao Ma 				  1, 0, meta_ac);
1604e73a819dSTao Ma 	if (ret)
1605e73a819dSTao Ma 		mlog_errno(ret);
1606e73a819dSTao Ma 
1607e73a819dSTao Ma out:
1608e73a819dSTao Ma 	brelse(new_bh);
1609e73a819dSTao Ma 	return ret;
1610e73a819dSTao Ma }
1611e73a819dSTao Ma 
1612e73a819dSTao Ma static int ocfs2_expand_refcount_tree(handle_t *handle,
1613e73a819dSTao Ma 				      struct ocfs2_caching_info *ci,
1614e73a819dSTao Ma 				      struct buffer_head *ref_root_bh,
1615e73a819dSTao Ma 				      struct buffer_head *ref_leaf_bh,
1616e73a819dSTao Ma 				      struct ocfs2_alloc_context *meta_ac)
1617e73a819dSTao Ma {
1618e73a819dSTao Ma 	int ret;
1619e73a819dSTao Ma 	struct buffer_head *expand_bh = NULL;
1620e73a819dSTao Ma 
1621e73a819dSTao Ma 	if (ref_root_bh == ref_leaf_bh) {
1622e73a819dSTao Ma 		/*
1623e73a819dSTao Ma 		 * the old root bh hasn't been expanded to a b-tree,
1624e73a819dSTao Ma 		 * so expand it first.
1625e73a819dSTao Ma 		 */
1626e73a819dSTao Ma 		ret = ocfs2_expand_inline_ref_root(handle, ci, ref_root_bh,
1627e73a819dSTao Ma 						   &expand_bh, meta_ac);
1628e73a819dSTao Ma 		if (ret) {
1629e73a819dSTao Ma 			mlog_errno(ret);
1630e73a819dSTao Ma 			goto out;
1631e73a819dSTao Ma 		}
1632e73a819dSTao Ma 	} else {
1633e73a819dSTao Ma 		expand_bh = ref_leaf_bh;
1634e73a819dSTao Ma 		get_bh(expand_bh);
1635e73a819dSTao Ma 	}
1636e73a819dSTao Ma 
1637e73a819dSTao Ma 
1638e73a819dSTao Ma 	/* Now add a new refcount block into the tree.*/
1639e73a819dSTao Ma 	ret = ocfs2_new_leaf_refcount_block(handle, ci, ref_root_bh,
1640e73a819dSTao Ma 					    expand_bh, meta_ac);
1641e73a819dSTao Ma 	if (ret)
1642e73a819dSTao Ma 		mlog_errno(ret);
1643e73a819dSTao Ma out:
1644e73a819dSTao Ma 	brelse(expand_bh);
1645e73a819dSTao Ma 	return ret;
1646e73a819dSTao Ma }
1647e73a819dSTao Ma 
1648e73a819dSTao Ma /*
1649e73a819dSTao Ma  * Adjust the extent rec in b-tree representing ref_leaf_bh.
1650e73a819dSTao Ma  *
1651e73a819dSTao Ma  * Only called when we have inserted a new refcount rec at index 0
1652e73a819dSTao Ma  * which means ocfs2_extent_rec.e_cpos may need some change.
1653e73a819dSTao Ma  */
1654e73a819dSTao Ma static int ocfs2_adjust_refcount_rec(handle_t *handle,
1655e73a819dSTao Ma 				     struct ocfs2_caching_info *ci,
1656e73a819dSTao Ma 				     struct buffer_head *ref_root_bh,
1657e73a819dSTao Ma 				     struct buffer_head *ref_leaf_bh,
1658e73a819dSTao Ma 				     struct ocfs2_refcount_rec *rec)
1659e73a819dSTao Ma {
1660e73a819dSTao Ma 	int ret = 0, i;
1661e73a819dSTao Ma 	u32 new_cpos, old_cpos;
1662e73a819dSTao Ma 	struct ocfs2_path *path = NULL;
1663e73a819dSTao Ma 	struct ocfs2_extent_tree et;
1664e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1665e73a819dSTao Ma 		(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1666e73a819dSTao Ma 	struct ocfs2_extent_list *el;
1667e73a819dSTao Ma 
1668e73a819dSTao Ma 	if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL))
1669e73a819dSTao Ma 		goto out;
1670e73a819dSTao Ma 
1671e73a819dSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1672e73a819dSTao Ma 	old_cpos = le32_to_cpu(rb->rf_cpos);
1673e73a819dSTao Ma 	new_cpos = le64_to_cpu(rec->r_cpos) & OCFS2_32BIT_POS_MASK;
1674e73a819dSTao Ma 	if (old_cpos <= new_cpos)
1675e73a819dSTao Ma 		goto out;
1676e73a819dSTao Ma 
1677e73a819dSTao Ma 	ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
1678e73a819dSTao Ma 
1679e73a819dSTao Ma 	path = ocfs2_new_path_from_et(&et);
1680e73a819dSTao Ma 	if (!path) {
1681e73a819dSTao Ma 		ret = -ENOMEM;
1682e73a819dSTao Ma 		mlog_errno(ret);
1683e73a819dSTao Ma 		goto out;
1684e73a819dSTao Ma 	}
1685e73a819dSTao Ma 
1686e73a819dSTao Ma 	ret = ocfs2_find_path(ci, path, old_cpos);
1687e73a819dSTao Ma 	if (ret) {
1688e73a819dSTao Ma 		mlog_errno(ret);
1689e73a819dSTao Ma 		goto out;
1690e73a819dSTao Ma 	}
1691e73a819dSTao Ma 
1692e73a819dSTao Ma 	/*
1693e73a819dSTao Ma 	 * 2 more credits, one for the leaf refcount block, one for
1694e73a819dSTao Ma 	 * the extent block contains the extent rec.
1695e73a819dSTao Ma 	 */
1696c901fb00STao Ma 	ret = ocfs2_extend_trans(handle, 2);
1697e73a819dSTao Ma 	if (ret < 0) {
1698e73a819dSTao Ma 		mlog_errno(ret);
1699e73a819dSTao Ma 		goto out;
1700e73a819dSTao Ma 	}
1701e73a819dSTao Ma 
1702e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1703e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1704e73a819dSTao Ma 	if (ret < 0) {
1705e73a819dSTao Ma 		mlog_errno(ret);
1706e73a819dSTao Ma 		goto out;
1707e73a819dSTao Ma 	}
1708e73a819dSTao Ma 
1709e73a819dSTao Ma 	ret = ocfs2_journal_access_eb(handle, ci, path_leaf_bh(path),
1710e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1711e73a819dSTao Ma 	if (ret < 0) {
1712e73a819dSTao Ma 		mlog_errno(ret);
1713e73a819dSTao Ma 		goto out;
1714e73a819dSTao Ma 	}
1715e73a819dSTao Ma 
1716e73a819dSTao Ma 	/* change the leaf extent block first. */
1717e73a819dSTao Ma 	el = path_leaf_el(path);
1718e73a819dSTao Ma 
1719e73a819dSTao Ma 	for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++)
1720e73a819dSTao Ma 		if (le32_to_cpu(el->l_recs[i].e_cpos) == old_cpos)
1721e73a819dSTao Ma 			break;
1722e73a819dSTao Ma 
1723e73a819dSTao Ma 	BUG_ON(i == le16_to_cpu(el->l_next_free_rec));
1724e73a819dSTao Ma 
1725e73a819dSTao Ma 	el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1726e73a819dSTao Ma 
1727e73a819dSTao Ma 	/* change the r_cpos in the leaf block. */
1728e73a819dSTao Ma 	rb->rf_cpos = cpu_to_le32(new_cpos);
1729e73a819dSTao Ma 
1730e73a819dSTao Ma 	ocfs2_journal_dirty(handle, path_leaf_bh(path));
1731e73a819dSTao Ma 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1732e73a819dSTao Ma 
1733e73a819dSTao Ma out:
1734e73a819dSTao Ma 	ocfs2_free_path(path);
1735e73a819dSTao Ma 	return ret;
1736e73a819dSTao Ma }
1737e73a819dSTao Ma 
1738e73a819dSTao Ma static int ocfs2_insert_refcount_rec(handle_t *handle,
1739e73a819dSTao Ma 				     struct ocfs2_caching_info *ci,
1740e73a819dSTao Ma 				     struct buffer_head *ref_root_bh,
1741e73a819dSTao Ma 				     struct buffer_head *ref_leaf_bh,
1742e73a819dSTao Ma 				     struct ocfs2_refcount_rec *rec,
17437540c1a7STao Ma 				     int index, int merge,
1744e73a819dSTao Ma 				     struct ocfs2_alloc_context *meta_ac)
1745e73a819dSTao Ma {
1746e73a819dSTao Ma 	int ret;
1747e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1748e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1749e73a819dSTao Ma 	struct ocfs2_refcount_list *rf_list = &rb->rf_records;
1750e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1751e73a819dSTao Ma 
1752e73a819dSTao Ma 	BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
1753e73a819dSTao Ma 
1754e73a819dSTao Ma 	if (rf_list->rl_used == rf_list->rl_count) {
1755e73a819dSTao Ma 		u64 cpos = le64_to_cpu(rec->r_cpos);
1756e73a819dSTao Ma 		u32 len = le32_to_cpu(rec->r_clusters);
1757e73a819dSTao Ma 
1758e73a819dSTao Ma 		ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1759e73a819dSTao Ma 						 ref_leaf_bh, meta_ac);
1760e73a819dSTao Ma 		if (ret) {
1761e73a819dSTao Ma 			mlog_errno(ret);
1762e73a819dSTao Ma 			goto out;
1763e73a819dSTao Ma 		}
1764e73a819dSTao Ma 
1765e73a819dSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1766e73a819dSTao Ma 					     cpos, len, NULL, &index,
1767e73a819dSTao Ma 					     &new_bh);
1768e73a819dSTao Ma 		if (ret) {
1769e73a819dSTao Ma 			mlog_errno(ret);
1770e73a819dSTao Ma 			goto out;
1771e73a819dSTao Ma 		}
1772e73a819dSTao Ma 
1773e73a819dSTao Ma 		ref_leaf_bh = new_bh;
1774e73a819dSTao Ma 		rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1775e73a819dSTao Ma 		rf_list = &rb->rf_records;
1776e73a819dSTao Ma 	}
1777e73a819dSTao Ma 
1778e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1779e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1780e73a819dSTao Ma 	if (ret) {
1781e73a819dSTao Ma 		mlog_errno(ret);
1782e73a819dSTao Ma 		goto out;
1783e73a819dSTao Ma 	}
1784e73a819dSTao Ma 
1785e73a819dSTao Ma 	if (index < le16_to_cpu(rf_list->rl_used))
1786e73a819dSTao Ma 		memmove(&rf_list->rl_recs[index + 1],
1787e73a819dSTao Ma 			&rf_list->rl_recs[index],
1788e73a819dSTao Ma 			(le16_to_cpu(rf_list->rl_used) - index) *
1789e73a819dSTao Ma 			 sizeof(struct ocfs2_refcount_rec));
1790e73a819dSTao Ma 
1791e73a819dSTao Ma 	mlog(0, "insert refcount record start %llu, len %u, count %u "
1792e73a819dSTao Ma 	     "to leaf block %llu at index %d\n",
1793e73a819dSTao Ma 	     (unsigned long long)le64_to_cpu(rec->r_cpos),
1794e73a819dSTao Ma 	     le32_to_cpu(rec->r_clusters), le32_to_cpu(rec->r_refcount),
1795e73a819dSTao Ma 	     (unsigned long long)ref_leaf_bh->b_blocknr, index);
1796e73a819dSTao Ma 
1797e73a819dSTao Ma 	rf_list->rl_recs[index] = *rec;
1798e73a819dSTao Ma 
1799e73a819dSTao Ma 	le16_add_cpu(&rf_list->rl_used, 1);
1800e73a819dSTao Ma 
18017540c1a7STao Ma 	if (merge)
1802e73a819dSTao Ma 		ocfs2_refcount_rec_merge(rb, index);
1803e73a819dSTao Ma 
1804ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1805e73a819dSTao Ma 
1806e73a819dSTao Ma 	if (index == 0) {
1807e73a819dSTao Ma 		ret = ocfs2_adjust_refcount_rec(handle, ci,
1808e73a819dSTao Ma 						ref_root_bh,
1809e73a819dSTao Ma 						ref_leaf_bh, rec);
1810e73a819dSTao Ma 		if (ret)
1811e73a819dSTao Ma 			mlog_errno(ret);
1812e73a819dSTao Ma 	}
1813e73a819dSTao Ma out:
1814e73a819dSTao Ma 	brelse(new_bh);
1815e73a819dSTao Ma 	return ret;
1816e73a819dSTao Ma }
1817e73a819dSTao Ma 
1818e73a819dSTao Ma /*
1819e73a819dSTao Ma  * Split the refcount_rec indexed by "index" in ref_leaf_bh.
1820e73a819dSTao Ma  * This is much simple than our b-tree code.
1821e73a819dSTao Ma  * split_rec is the new refcount rec we want to insert.
1822e73a819dSTao Ma  * If split_rec->r_refcount > 0, we are changing the refcount(in case we
1823e73a819dSTao Ma  * increase refcount or decrease a refcount to non-zero).
1824e73a819dSTao Ma  * If split_rec->r_refcount == 0, we are punching a hole in current refcount
1825e73a819dSTao Ma  * rec( in case we decrease a refcount to zero).
1826e73a819dSTao Ma  */
1827e73a819dSTao Ma static int ocfs2_split_refcount_rec(handle_t *handle,
1828e73a819dSTao Ma 				    struct ocfs2_caching_info *ci,
1829e73a819dSTao Ma 				    struct buffer_head *ref_root_bh,
1830e73a819dSTao Ma 				    struct buffer_head *ref_leaf_bh,
1831e73a819dSTao Ma 				    struct ocfs2_refcount_rec *split_rec,
18327540c1a7STao Ma 				    int index, int merge,
1833e73a819dSTao Ma 				    struct ocfs2_alloc_context *meta_ac,
1834e73a819dSTao Ma 				    struct ocfs2_cached_dealloc_ctxt *dealloc)
1835e73a819dSTao Ma {
1836e73a819dSTao Ma 	int ret, recs_need;
1837e73a819dSTao Ma 	u32 len;
1838e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1839e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1840e73a819dSTao Ma 	struct ocfs2_refcount_list *rf_list = &rb->rf_records;
1841e73a819dSTao Ma 	struct ocfs2_refcount_rec *orig_rec = &rf_list->rl_recs[index];
1842e73a819dSTao Ma 	struct ocfs2_refcount_rec *tail_rec = NULL;
1843e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1844e73a819dSTao Ma 
1845e73a819dSTao Ma 	BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
1846e73a819dSTao Ma 
1847e73a819dSTao Ma 	mlog(0, "original r_pos %llu, cluster %u, split %llu, cluster %u\n",
1848e73a819dSTao Ma 	     le64_to_cpu(orig_rec->r_cpos), le32_to_cpu(orig_rec->r_clusters),
1849e73a819dSTao Ma 	     le64_to_cpu(split_rec->r_cpos),
1850e73a819dSTao Ma 	     le32_to_cpu(split_rec->r_clusters));
1851e73a819dSTao Ma 
1852e73a819dSTao Ma 	/*
1853e73a819dSTao Ma 	 * If we just need to split the header or tail clusters,
1854e73a819dSTao Ma 	 * no more recs are needed, just split is OK.
1855e73a819dSTao Ma 	 * Otherwise we at least need one new recs.
1856e73a819dSTao Ma 	 */
1857e73a819dSTao Ma 	if (!split_rec->r_refcount &&
1858e73a819dSTao Ma 	    (split_rec->r_cpos == orig_rec->r_cpos ||
1859e73a819dSTao Ma 	     le64_to_cpu(split_rec->r_cpos) +
1860e73a819dSTao Ma 	     le32_to_cpu(split_rec->r_clusters) ==
1861e73a819dSTao Ma 	     le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
1862e73a819dSTao Ma 		recs_need = 0;
1863e73a819dSTao Ma 	else
1864e73a819dSTao Ma 		recs_need = 1;
1865e73a819dSTao Ma 
1866e73a819dSTao Ma 	/*
1867e73a819dSTao Ma 	 * We need one more rec if we split in the middle and the new rec have
1868e73a819dSTao Ma 	 * some refcount in it.
1869e73a819dSTao Ma 	 */
1870e73a819dSTao Ma 	if (split_rec->r_refcount &&
1871e73a819dSTao Ma 	    (split_rec->r_cpos != orig_rec->r_cpos &&
1872e73a819dSTao Ma 	     le64_to_cpu(split_rec->r_cpos) +
1873e73a819dSTao Ma 	     le32_to_cpu(split_rec->r_clusters) !=
1874e73a819dSTao Ma 	     le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
1875e73a819dSTao Ma 		recs_need++;
1876e73a819dSTao Ma 
1877e73a819dSTao Ma 	/* If the leaf block don't have enough record, expand it. */
187812d4cec9STao Ma 	if (le16_to_cpu(rf_list->rl_used) + recs_need >
187912d4cec9STao Ma 					 le16_to_cpu(rf_list->rl_count)) {
1880e73a819dSTao Ma 		struct ocfs2_refcount_rec tmp_rec;
1881e73a819dSTao Ma 		u64 cpos = le64_to_cpu(orig_rec->r_cpos);
1882e73a819dSTao Ma 		len = le32_to_cpu(orig_rec->r_clusters);
1883e73a819dSTao Ma 		ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1884e73a819dSTao Ma 						 ref_leaf_bh, meta_ac);
1885e73a819dSTao Ma 		if (ret) {
1886e73a819dSTao Ma 			mlog_errno(ret);
1887e73a819dSTao Ma 			goto out;
1888e73a819dSTao Ma 		}
1889e73a819dSTao Ma 
1890e73a819dSTao Ma 		/*
1891e73a819dSTao Ma 		 * We have to re-get it since now cpos may be moved to
1892e73a819dSTao Ma 		 * another leaf block.
1893e73a819dSTao Ma 		 */
1894e73a819dSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1895e73a819dSTao Ma 					     cpos, len, &tmp_rec, &index,
1896e73a819dSTao Ma 					     &new_bh);
1897e73a819dSTao Ma 		if (ret) {
1898e73a819dSTao Ma 			mlog_errno(ret);
1899e73a819dSTao Ma 			goto out;
1900e73a819dSTao Ma 		}
1901e73a819dSTao Ma 
1902e73a819dSTao Ma 		ref_leaf_bh = new_bh;
1903e73a819dSTao Ma 		rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1904e73a819dSTao Ma 		rf_list = &rb->rf_records;
1905e73a819dSTao Ma 		orig_rec = &rf_list->rl_recs[index];
1906e73a819dSTao Ma 	}
1907e73a819dSTao Ma 
1908e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1909e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1910e73a819dSTao Ma 	if (ret) {
1911e73a819dSTao Ma 		mlog_errno(ret);
1912e73a819dSTao Ma 		goto out;
1913e73a819dSTao Ma 	}
1914e73a819dSTao Ma 
1915e73a819dSTao Ma 	/*
1916e73a819dSTao Ma 	 * We have calculated out how many new records we need and store
1917e73a819dSTao Ma 	 * in recs_need, so spare enough space first by moving the records
1918e73a819dSTao Ma 	 * after "index" to the end.
1919e73a819dSTao Ma 	 */
1920e73a819dSTao Ma 	if (index != le16_to_cpu(rf_list->rl_used) - 1)
1921e73a819dSTao Ma 		memmove(&rf_list->rl_recs[index + 1 + recs_need],
1922e73a819dSTao Ma 			&rf_list->rl_recs[index + 1],
1923e73a819dSTao Ma 			(le16_to_cpu(rf_list->rl_used) - index - 1) *
1924e73a819dSTao Ma 			 sizeof(struct ocfs2_refcount_rec));
1925e73a819dSTao Ma 
1926e73a819dSTao Ma 	len = (le64_to_cpu(orig_rec->r_cpos) +
1927e73a819dSTao Ma 	      le32_to_cpu(orig_rec->r_clusters)) -
1928e73a819dSTao Ma 	      (le64_to_cpu(split_rec->r_cpos) +
1929e73a819dSTao Ma 	      le32_to_cpu(split_rec->r_clusters));
1930e73a819dSTao Ma 
1931e73a819dSTao Ma 	/*
1932e73a819dSTao Ma 	 * If we have "len", the we will split in the tail and move it
1933e73a819dSTao Ma 	 * to the end of the space we have just spared.
1934e73a819dSTao Ma 	 */
1935e73a819dSTao Ma 	if (len) {
1936e73a819dSTao Ma 		tail_rec = &rf_list->rl_recs[index + recs_need];
1937e73a819dSTao Ma 
1938e73a819dSTao Ma 		memcpy(tail_rec, orig_rec, sizeof(struct ocfs2_refcount_rec));
1939e73a819dSTao Ma 		le64_add_cpu(&tail_rec->r_cpos,
1940e73a819dSTao Ma 			     le32_to_cpu(tail_rec->r_clusters) - len);
194112d4cec9STao Ma 		tail_rec->r_clusters = cpu_to_le32(len);
1942e73a819dSTao Ma 	}
1943e73a819dSTao Ma 
1944e73a819dSTao Ma 	/*
1945e73a819dSTao Ma 	 * If the split pos isn't the same as the original one, we need to
1946e73a819dSTao Ma 	 * split in the head.
1947e73a819dSTao Ma 	 *
1948e73a819dSTao Ma 	 * Note: We have the chance that split_rec.r_refcount = 0,
1949e73a819dSTao Ma 	 * recs_need = 0 and len > 0, which means we just cut the head from
1950e73a819dSTao Ma 	 * the orig_rec and in that case we have done some modification in
1951e73a819dSTao Ma 	 * orig_rec above, so the check for r_cpos is faked.
1952e73a819dSTao Ma 	 */
1953e73a819dSTao Ma 	if (split_rec->r_cpos != orig_rec->r_cpos && tail_rec != orig_rec) {
1954e73a819dSTao Ma 		len = le64_to_cpu(split_rec->r_cpos) -
1955e73a819dSTao Ma 		      le64_to_cpu(orig_rec->r_cpos);
1956e73a819dSTao Ma 		orig_rec->r_clusters = cpu_to_le32(len);
1957e73a819dSTao Ma 		index++;
1958e73a819dSTao Ma 	}
1959e73a819dSTao Ma 
1960e73a819dSTao Ma 	le16_add_cpu(&rf_list->rl_used, recs_need);
1961e73a819dSTao Ma 
1962e73a819dSTao Ma 	if (split_rec->r_refcount) {
1963e73a819dSTao Ma 		rf_list->rl_recs[index] = *split_rec;
1964e73a819dSTao Ma 		mlog(0, "insert refcount record start %llu, len %u, count %u "
1965e73a819dSTao Ma 		     "to leaf block %llu at index %d\n",
1966e73a819dSTao Ma 		     (unsigned long long)le64_to_cpu(split_rec->r_cpos),
1967e73a819dSTao Ma 		     le32_to_cpu(split_rec->r_clusters),
1968e73a819dSTao Ma 		     le32_to_cpu(split_rec->r_refcount),
1969e73a819dSTao Ma 		     (unsigned long long)ref_leaf_bh->b_blocknr, index);
1970e73a819dSTao Ma 
19717540c1a7STao Ma 		if (merge)
1972e73a819dSTao Ma 			ocfs2_refcount_rec_merge(rb, index);
1973e73a819dSTao Ma 	}
1974e73a819dSTao Ma 
1975ec20cec7SJoel Becker 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1976e73a819dSTao Ma 
1977e73a819dSTao Ma out:
1978e73a819dSTao Ma 	brelse(new_bh);
1979e73a819dSTao Ma 	return ret;
1980e73a819dSTao Ma }
1981e73a819dSTao Ma 
19827540c1a7STao Ma static int __ocfs2_increase_refcount(handle_t *handle,
1983e73a819dSTao Ma 				     struct ocfs2_caching_info *ci,
1984e73a819dSTao Ma 				     struct buffer_head *ref_root_bh,
19857540c1a7STao Ma 				     u64 cpos, u32 len, int merge,
1986e73a819dSTao Ma 				     struct ocfs2_alloc_context *meta_ac,
1987e73a819dSTao Ma 				     struct ocfs2_cached_dealloc_ctxt *dealloc)
1988e73a819dSTao Ma {
1989e73a819dSTao Ma 	int ret = 0, index;
1990e73a819dSTao Ma 	struct buffer_head *ref_leaf_bh = NULL;
1991e73a819dSTao Ma 	struct ocfs2_refcount_rec rec;
1992e73a819dSTao Ma 	unsigned int set_len = 0;
1993e73a819dSTao Ma 
1994e73a819dSTao Ma 	mlog(0, "Tree owner %llu, add refcount start %llu, len %u\n",
1995e73a819dSTao Ma 	     (unsigned long long)ocfs2_metadata_cache_owner(ci),
1996e73a819dSTao Ma 	     (unsigned long long)cpos, len);
1997e73a819dSTao Ma 
1998e73a819dSTao Ma 	while (len) {
1999e73a819dSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
2000e73a819dSTao Ma 					     cpos, len, &rec, &index,
2001e73a819dSTao Ma 					     &ref_leaf_bh);
2002e73a819dSTao Ma 		if (ret) {
2003e73a819dSTao Ma 			mlog_errno(ret);
2004e73a819dSTao Ma 			goto out;
2005e73a819dSTao Ma 		}
2006e73a819dSTao Ma 
2007e73a819dSTao Ma 		set_len = le32_to_cpu(rec.r_clusters);
2008e73a819dSTao Ma 
2009e73a819dSTao Ma 		/*
2010e73a819dSTao Ma 		 * Here we may meet with 3 situations:
2011e73a819dSTao Ma 		 *
2012e73a819dSTao Ma 		 * 1. If we find an already existing record, and the length
2013e73a819dSTao Ma 		 *    is the same, cool, we just need to increase the r_refcount
2014e73a819dSTao Ma 		 *    and it is OK.
2015e73a819dSTao Ma 		 * 2. If we find a hole, just insert it with r_refcount = 1.
2016e73a819dSTao Ma 		 * 3. If we are in the middle of one extent record, split
2017e73a819dSTao Ma 		 *    it.
2018e73a819dSTao Ma 		 */
2019e73a819dSTao Ma 		if (rec.r_refcount && le64_to_cpu(rec.r_cpos) == cpos &&
2020e73a819dSTao Ma 		    set_len <= len) {
2021e73a819dSTao Ma 			mlog(0, "increase refcount rec, start %llu, len %u, "
2022e73a819dSTao Ma 			     "count %u\n", (unsigned long long)cpos, set_len,
2023e73a819dSTao Ma 			     le32_to_cpu(rec.r_refcount));
2024e73a819dSTao Ma 			ret = ocfs2_change_refcount_rec(handle, ci,
20257540c1a7STao Ma 							ref_leaf_bh, index,
20267540c1a7STao Ma 							merge, 1);
2027e73a819dSTao Ma 			if (ret) {
2028e73a819dSTao Ma 				mlog_errno(ret);
2029e73a819dSTao Ma 				goto out;
2030e73a819dSTao Ma 			}
2031e73a819dSTao Ma 		} else if (!rec.r_refcount) {
2032e73a819dSTao Ma 			rec.r_refcount = cpu_to_le32(1);
2033e73a819dSTao Ma 
2034e73a819dSTao Ma 			mlog(0, "insert refcount rec, start %llu, len %u\n",
2035e73a819dSTao Ma 			     (unsigned long long)le64_to_cpu(rec.r_cpos),
2036e73a819dSTao Ma 			     set_len);
2037e73a819dSTao Ma 			ret = ocfs2_insert_refcount_rec(handle, ci, ref_root_bh,
2038e73a819dSTao Ma 							ref_leaf_bh,
20397540c1a7STao Ma 							&rec, index,
20407540c1a7STao Ma 							merge, meta_ac);
2041e73a819dSTao Ma 			if (ret) {
2042e73a819dSTao Ma 				mlog_errno(ret);
2043e73a819dSTao Ma 				goto out;
2044e73a819dSTao Ma 			}
2045e73a819dSTao Ma 		} else  {
2046e73a819dSTao Ma 			set_len = min((u64)(cpos + len),
2047e73a819dSTao Ma 				      le64_to_cpu(rec.r_cpos) + set_len) - cpos;
2048e73a819dSTao Ma 			rec.r_cpos = cpu_to_le64(cpos);
2049e73a819dSTao Ma 			rec.r_clusters = cpu_to_le32(set_len);
2050e73a819dSTao Ma 			le32_add_cpu(&rec.r_refcount, 1);
2051e73a819dSTao Ma 
2052e73a819dSTao Ma 			mlog(0, "split refcount rec, start %llu, "
2053e73a819dSTao Ma 			     "len %u, count %u\n",
2054e73a819dSTao Ma 			     (unsigned long long)le64_to_cpu(rec.r_cpos),
2055e73a819dSTao Ma 			     set_len, le32_to_cpu(rec.r_refcount));
2056e73a819dSTao Ma 			ret = ocfs2_split_refcount_rec(handle, ci,
2057e73a819dSTao Ma 						       ref_root_bh, ref_leaf_bh,
20587540c1a7STao Ma 						       &rec, index, merge,
2059e73a819dSTao Ma 						       meta_ac, dealloc);
2060e73a819dSTao Ma 			if (ret) {
2061e73a819dSTao Ma 				mlog_errno(ret);
2062e73a819dSTao Ma 				goto out;
2063e73a819dSTao Ma 			}
2064e73a819dSTao Ma 		}
2065e73a819dSTao Ma 
2066e73a819dSTao Ma 		cpos += set_len;
2067e73a819dSTao Ma 		len -= set_len;
2068e73a819dSTao Ma 		brelse(ref_leaf_bh);
2069e73a819dSTao Ma 		ref_leaf_bh = NULL;
2070e73a819dSTao Ma 	}
2071e73a819dSTao Ma 
2072e73a819dSTao Ma out:
2073e73a819dSTao Ma 	brelse(ref_leaf_bh);
2074e73a819dSTao Ma 	return ret;
2075e73a819dSTao Ma }
20761823cb0bSTao Ma 
20771823cb0bSTao Ma static int ocfs2_remove_refcount_extent(handle_t *handle,
20781823cb0bSTao Ma 				struct ocfs2_caching_info *ci,
20791823cb0bSTao Ma 				struct buffer_head *ref_root_bh,
20801823cb0bSTao Ma 				struct buffer_head *ref_leaf_bh,
20811823cb0bSTao Ma 				struct ocfs2_alloc_context *meta_ac,
20821823cb0bSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
20831823cb0bSTao Ma {
20841823cb0bSTao Ma 	int ret;
20851823cb0bSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
20861823cb0bSTao Ma 	struct ocfs2_refcount_block *rb =
20871823cb0bSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
20881823cb0bSTao Ma 	struct ocfs2_extent_tree et;
20891823cb0bSTao Ma 
20901823cb0bSTao Ma 	BUG_ON(rb->rf_records.rl_used);
20911823cb0bSTao Ma 
20921823cb0bSTao Ma 	ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
20931823cb0bSTao Ma 	ret = ocfs2_remove_extent(handle, &et, le32_to_cpu(rb->rf_cpos),
20941823cb0bSTao Ma 				  1, meta_ac, dealloc);
20951823cb0bSTao Ma 	if (ret) {
20961823cb0bSTao Ma 		mlog_errno(ret);
20971823cb0bSTao Ma 		goto out;
20981823cb0bSTao Ma 	}
20991823cb0bSTao Ma 
21001823cb0bSTao Ma 	ocfs2_remove_from_cache(ci, ref_leaf_bh);
21011823cb0bSTao Ma 
21021823cb0bSTao Ma 	/*
21031823cb0bSTao Ma 	 * add the freed block to the dealloc so that it will be freed
21041823cb0bSTao Ma 	 * when we run dealloc.
21051823cb0bSTao Ma 	 */
21061823cb0bSTao Ma 	ret = ocfs2_cache_block_dealloc(dealloc, EXTENT_ALLOC_SYSTEM_INODE,
21071823cb0bSTao Ma 					le16_to_cpu(rb->rf_suballoc_slot),
21081823cb0bSTao Ma 					le64_to_cpu(rb->rf_blkno),
21091823cb0bSTao Ma 					le16_to_cpu(rb->rf_suballoc_bit));
21101823cb0bSTao Ma 	if (ret) {
21111823cb0bSTao Ma 		mlog_errno(ret);
21121823cb0bSTao Ma 		goto out;
21131823cb0bSTao Ma 	}
21141823cb0bSTao Ma 
21151823cb0bSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
21161823cb0bSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
21171823cb0bSTao Ma 	if (ret) {
21181823cb0bSTao Ma 		mlog_errno(ret);
21191823cb0bSTao Ma 		goto out;
21201823cb0bSTao Ma 	}
21211823cb0bSTao Ma 
21221823cb0bSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
21231823cb0bSTao Ma 
21241823cb0bSTao Ma 	le32_add_cpu(&rb->rf_clusters, -1);
21251823cb0bSTao Ma 
21261823cb0bSTao Ma 	/*
21271823cb0bSTao Ma 	 * check whether we need to restore the root refcount block if
21281823cb0bSTao Ma 	 * there is no leaf extent block at atll.
21291823cb0bSTao Ma 	 */
21301823cb0bSTao Ma 	if (!rb->rf_list.l_next_free_rec) {
21311823cb0bSTao Ma 		BUG_ON(rb->rf_clusters);
21321823cb0bSTao Ma 
21331823cb0bSTao Ma 		mlog(0, "reset refcount tree root %llu to be a record block.\n",
21341823cb0bSTao Ma 		     (unsigned long long)ref_root_bh->b_blocknr);
21351823cb0bSTao Ma 
21361823cb0bSTao Ma 		rb->rf_flags = 0;
21371823cb0bSTao Ma 		rb->rf_parent = 0;
21381823cb0bSTao Ma 		rb->rf_cpos = 0;
21391823cb0bSTao Ma 		memset(&rb->rf_records, 0, sb->s_blocksize -
21401823cb0bSTao Ma 		       offsetof(struct ocfs2_refcount_block, rf_records));
21411823cb0bSTao Ma 		rb->rf_records.rl_count =
21421823cb0bSTao Ma 				cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
21431823cb0bSTao Ma 	}
21441823cb0bSTao Ma 
21451823cb0bSTao Ma 	ocfs2_journal_dirty(handle, ref_root_bh);
21461823cb0bSTao Ma 
21471823cb0bSTao Ma out:
21481823cb0bSTao Ma 	return ret;
21491823cb0bSTao Ma }
21501823cb0bSTao Ma 
21517540c1a7STao Ma int ocfs2_increase_refcount(handle_t *handle,
21527540c1a7STao Ma 			    struct ocfs2_caching_info *ci,
21537540c1a7STao Ma 			    struct buffer_head *ref_root_bh,
21547540c1a7STao Ma 			    u64 cpos, u32 len,
21557540c1a7STao Ma 			    struct ocfs2_alloc_context *meta_ac,
21567540c1a7STao Ma 			    struct ocfs2_cached_dealloc_ctxt *dealloc)
21577540c1a7STao Ma {
21587540c1a7STao Ma 	return __ocfs2_increase_refcount(handle, ci, ref_root_bh,
21597540c1a7STao Ma 					 cpos, len, 1,
21607540c1a7STao Ma 					 meta_ac, dealloc);
21617540c1a7STao Ma }
21627540c1a7STao Ma 
21631823cb0bSTao Ma static int ocfs2_decrease_refcount_rec(handle_t *handle,
21641823cb0bSTao Ma 				struct ocfs2_caching_info *ci,
21651823cb0bSTao Ma 				struct buffer_head *ref_root_bh,
21661823cb0bSTao Ma 				struct buffer_head *ref_leaf_bh,
21671823cb0bSTao Ma 				int index, u64 cpos, unsigned int len,
21681823cb0bSTao Ma 				struct ocfs2_alloc_context *meta_ac,
21691823cb0bSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
21701823cb0bSTao Ma {
21711823cb0bSTao Ma 	int ret;
21721823cb0bSTao Ma 	struct ocfs2_refcount_block *rb =
21731823cb0bSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
21741823cb0bSTao Ma 	struct ocfs2_refcount_rec *rec = &rb->rf_records.rl_recs[index];
21751823cb0bSTao Ma 
21761823cb0bSTao Ma 	BUG_ON(cpos < le64_to_cpu(rec->r_cpos));
21771823cb0bSTao Ma 	BUG_ON(cpos + len >
21781823cb0bSTao Ma 	       le64_to_cpu(rec->r_cpos) + le32_to_cpu(rec->r_clusters));
21791823cb0bSTao Ma 
21801823cb0bSTao Ma 	if (cpos == le64_to_cpu(rec->r_cpos) &&
21811823cb0bSTao Ma 	    len == le32_to_cpu(rec->r_clusters))
21821823cb0bSTao Ma 		ret = ocfs2_change_refcount_rec(handle, ci,
21837540c1a7STao Ma 						ref_leaf_bh, index, 1, -1);
21841823cb0bSTao Ma 	else {
21851823cb0bSTao Ma 		struct ocfs2_refcount_rec split = *rec;
21861823cb0bSTao Ma 		split.r_cpos = cpu_to_le64(cpos);
21871823cb0bSTao Ma 		split.r_clusters = cpu_to_le32(len);
21881823cb0bSTao Ma 
21891823cb0bSTao Ma 		le32_add_cpu(&split.r_refcount, -1);
21901823cb0bSTao Ma 
21911823cb0bSTao Ma 		mlog(0, "split refcount rec, start %llu, "
21921823cb0bSTao Ma 		     "len %u, count %u, original start %llu, len %u\n",
21931823cb0bSTao Ma 		     (unsigned long long)le64_to_cpu(split.r_cpos),
21941823cb0bSTao Ma 		     len, le32_to_cpu(split.r_refcount),
21951823cb0bSTao Ma 		     (unsigned long long)le64_to_cpu(rec->r_cpos),
21961823cb0bSTao Ma 		     le32_to_cpu(rec->r_clusters));
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 
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 
22356ae23c55STao Ma 	mlog(0, "Tree owner %llu, decrease refcount start %llu, "
22366ae23c55STao Ma 	     "len %u, delete %u\n",
22371823cb0bSTao Ma 	     (unsigned long long)ocfs2_metadata_cache_owner(ci),
22386ae23c55STao Ma 	     (unsigned long long)cpos, len, delete);
22391823cb0bSTao Ma 
22401823cb0bSTao Ma 	while (len) {
22411823cb0bSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
22421823cb0bSTao Ma 					     cpos, len, &rec, &index,
22431823cb0bSTao Ma 					     &ref_leaf_bh);
22441823cb0bSTao Ma 		if (ret) {
22451823cb0bSTao Ma 			mlog_errno(ret);
22461823cb0bSTao Ma 			goto out;
22471823cb0bSTao Ma 		}
22481823cb0bSTao Ma 
22491823cb0bSTao Ma 		r_count = le32_to_cpu(rec.r_refcount);
22501823cb0bSTao Ma 		BUG_ON(r_count == 0);
22516ae23c55STao Ma 		if (!delete)
22526ae23c55STao Ma 			BUG_ON(r_count > 1);
22531823cb0bSTao Ma 
22541823cb0bSTao Ma 		r_len = min((u64)(cpos + len), le64_to_cpu(rec.r_cpos) +
22551823cb0bSTao Ma 			      le32_to_cpu(rec.r_clusters)) - cpos;
22561823cb0bSTao Ma 
22571823cb0bSTao Ma 		ret = ocfs2_decrease_refcount_rec(handle, ci, ref_root_bh,
22581823cb0bSTao Ma 						  ref_leaf_bh, index,
22591823cb0bSTao Ma 						  cpos, r_len,
22601823cb0bSTao Ma 						  meta_ac, dealloc);
22611823cb0bSTao Ma 		if (ret) {
22621823cb0bSTao Ma 			mlog_errno(ret);
22631823cb0bSTao Ma 			goto out;
22641823cb0bSTao Ma 		}
22651823cb0bSTao Ma 
22666ae23c55STao Ma 		if (le32_to_cpu(rec.r_refcount) == 1 && delete) {
22671823cb0bSTao Ma 			ret = ocfs2_cache_cluster_dealloc(dealloc,
22681823cb0bSTao Ma 					  ocfs2_clusters_to_blocks(sb, cpos),
22691823cb0bSTao Ma 							  r_len);
22701823cb0bSTao Ma 			if (ret) {
22711823cb0bSTao Ma 				mlog_errno(ret);
22721823cb0bSTao Ma 				goto out;
22731823cb0bSTao Ma 			}
22741823cb0bSTao Ma 		}
22751823cb0bSTao Ma 
22761823cb0bSTao Ma 		cpos += r_len;
22771823cb0bSTao Ma 		len -= r_len;
22781823cb0bSTao Ma 		brelse(ref_leaf_bh);
22791823cb0bSTao Ma 		ref_leaf_bh = NULL;
22801823cb0bSTao Ma 	}
22811823cb0bSTao Ma 
22821823cb0bSTao Ma out:
22831823cb0bSTao Ma 	brelse(ref_leaf_bh);
22841823cb0bSTao Ma 	return ret;
22851823cb0bSTao Ma }
22861823cb0bSTao Ma 
22871823cb0bSTao Ma /* Caller must hold refcount tree lock. */
22881823cb0bSTao Ma int ocfs2_decrease_refcount(struct inode *inode,
22891823cb0bSTao Ma 			    handle_t *handle, u32 cpos, u32 len,
22901823cb0bSTao Ma 			    struct ocfs2_alloc_context *meta_ac,
22916ae23c55STao Ma 			    struct ocfs2_cached_dealloc_ctxt *dealloc,
22926ae23c55STao Ma 			    int delete)
22931823cb0bSTao Ma {
22941823cb0bSTao Ma 	int ret;
22951823cb0bSTao Ma 	u64 ref_blkno;
22961823cb0bSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
22971823cb0bSTao Ma 	struct buffer_head *ref_root_bh = NULL;
22981823cb0bSTao Ma 	struct ocfs2_refcount_tree *tree;
22991823cb0bSTao Ma 
23001823cb0bSTao Ma 	BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
23011823cb0bSTao Ma 
23021823cb0bSTao Ma 	ret = ocfs2_get_refcount_block(inode, &ref_blkno);
23031823cb0bSTao Ma 	if (ret) {
23041823cb0bSTao Ma 		mlog_errno(ret);
23051823cb0bSTao Ma 		goto out;
23061823cb0bSTao Ma 	}
23071823cb0bSTao Ma 
23081823cb0bSTao Ma 	ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno, &tree);
23091823cb0bSTao Ma 	if (ret) {
23101823cb0bSTao Ma 		mlog_errno(ret);
23111823cb0bSTao Ma 		goto out;
23121823cb0bSTao Ma 	}
23131823cb0bSTao Ma 
23141823cb0bSTao Ma 	ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
23151823cb0bSTao Ma 					&ref_root_bh);
23161823cb0bSTao Ma 	if (ret) {
23171823cb0bSTao Ma 		mlog_errno(ret);
23181823cb0bSTao Ma 		goto out;
23191823cb0bSTao Ma 	}
23201823cb0bSTao Ma 
23211823cb0bSTao Ma 	ret = __ocfs2_decrease_refcount(handle, &tree->rf_ci, ref_root_bh,
23226ae23c55STao Ma 					cpos, len, meta_ac, dealloc, delete);
23231823cb0bSTao Ma 	if (ret)
23241823cb0bSTao Ma 		mlog_errno(ret);
23251823cb0bSTao Ma out:
23261823cb0bSTao Ma 	brelse(ref_root_bh);
23271823cb0bSTao Ma 	return ret;
23281823cb0bSTao Ma }
23291aa75feaSTao Ma 
23301aa75feaSTao Ma /*
23311aa75feaSTao Ma  * Mark the already-existing extent at cpos as refcounted for len clusters.
23321aa75feaSTao Ma  * This adds the refcount extent flag.
23331aa75feaSTao Ma  *
23341aa75feaSTao Ma  * If the existing extent is larger than the request, initiate a
23351aa75feaSTao Ma  * split. An attempt will be made at merging with adjacent extents.
23361aa75feaSTao Ma  *
23371aa75feaSTao Ma  * The caller is responsible for passing down meta_ac if we'll need it.
23381aa75feaSTao Ma  */
23391aa75feaSTao Ma static int ocfs2_mark_extent_refcounted(struct inode *inode,
23401aa75feaSTao Ma 				struct ocfs2_extent_tree *et,
23411aa75feaSTao Ma 				handle_t *handle, u32 cpos,
23421aa75feaSTao Ma 				u32 len, u32 phys,
23431aa75feaSTao Ma 				struct ocfs2_alloc_context *meta_ac,
23441aa75feaSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
23451aa75feaSTao Ma {
23461aa75feaSTao Ma 	int ret;
23471aa75feaSTao Ma 
23481aa75feaSTao Ma 	mlog(0, "Inode %lu refcount tree cpos %u, len %u, phys cluster %u\n",
23491aa75feaSTao Ma 	     inode->i_ino, cpos, len, phys);
23501aa75feaSTao Ma 
23511aa75feaSTao Ma 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
23521aa75feaSTao Ma 		ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
23531aa75feaSTao Ma 			    "tree, but the feature bit is not set in the "
23541aa75feaSTao Ma 			    "super block.", inode->i_ino);
23551aa75feaSTao Ma 		ret = -EROFS;
23561aa75feaSTao Ma 		goto out;
23571aa75feaSTao Ma 	}
23581aa75feaSTao Ma 
23591aa75feaSTao Ma 	ret = ocfs2_change_extent_flag(handle, et, cpos,
23601aa75feaSTao Ma 				       len, phys, meta_ac, dealloc,
23611aa75feaSTao Ma 				       OCFS2_EXT_REFCOUNTED, 0);
23621aa75feaSTao Ma 	if (ret)
23631aa75feaSTao Ma 		mlog_errno(ret);
23641aa75feaSTao Ma 
23651aa75feaSTao Ma out:
23661aa75feaSTao Ma 	return ret;
23671aa75feaSTao Ma }
2368bcbbb24aSTao Ma 
2369bcbbb24aSTao Ma /*
2370bcbbb24aSTao Ma  * Given some contiguous physical clusters, calculate what we need
2371bcbbb24aSTao Ma  * for modifying their refcount.
2372bcbbb24aSTao Ma  */
2373bcbbb24aSTao Ma static int ocfs2_calc_refcount_meta_credits(struct super_block *sb,
2374bcbbb24aSTao Ma 					    struct ocfs2_caching_info *ci,
2375bcbbb24aSTao Ma 					    struct buffer_head *ref_root_bh,
2376bcbbb24aSTao Ma 					    u64 start_cpos,
2377bcbbb24aSTao Ma 					    u32 clusters,
2378bcbbb24aSTao Ma 					    int *meta_add,
2379bcbbb24aSTao Ma 					    int *credits)
2380bcbbb24aSTao Ma {
2381bcbbb24aSTao Ma 	int ret = 0, index, ref_blocks = 0, recs_add = 0;
2382bcbbb24aSTao Ma 	u64 cpos = start_cpos;
2383bcbbb24aSTao Ma 	struct ocfs2_refcount_block *rb;
2384bcbbb24aSTao Ma 	struct ocfs2_refcount_rec rec;
2385bcbbb24aSTao Ma 	struct buffer_head *ref_leaf_bh = NULL, *prev_bh = NULL;
2386bcbbb24aSTao Ma 	u32 len;
2387bcbbb24aSTao Ma 
2388bcbbb24aSTao Ma 	mlog(0, "start_cpos %llu, clusters %u\n",
2389bcbbb24aSTao Ma 	     (unsigned long long)start_cpos, clusters);
2390bcbbb24aSTao Ma 	while (clusters) {
2391bcbbb24aSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
2392bcbbb24aSTao Ma 					     cpos, clusters, &rec,
2393bcbbb24aSTao Ma 					     &index, &ref_leaf_bh);
2394bcbbb24aSTao Ma 		if (ret) {
2395bcbbb24aSTao Ma 			mlog_errno(ret);
2396bcbbb24aSTao Ma 			goto out;
2397bcbbb24aSTao Ma 		}
2398bcbbb24aSTao Ma 
2399bcbbb24aSTao Ma 		if (ref_leaf_bh != prev_bh) {
2400bcbbb24aSTao Ma 			/*
2401bcbbb24aSTao Ma 			 * Now we encounter a new leaf block, so calculate
2402bcbbb24aSTao Ma 			 * whether we need to extend the old leaf.
2403bcbbb24aSTao Ma 			 */
2404bcbbb24aSTao Ma 			if (prev_bh) {
2405bcbbb24aSTao Ma 				rb = (struct ocfs2_refcount_block *)
2406bcbbb24aSTao Ma 							prev_bh->b_data;
2407bcbbb24aSTao Ma 
2408bcbbb24aSTao Ma 				if (le64_to_cpu(rb->rf_records.rl_used) +
2409bcbbb24aSTao Ma 				    recs_add >
2410bcbbb24aSTao Ma 				    le16_to_cpu(rb->rf_records.rl_count))
2411bcbbb24aSTao Ma 					ref_blocks++;
2412bcbbb24aSTao Ma 			}
2413bcbbb24aSTao Ma 
2414bcbbb24aSTao Ma 			recs_add = 0;
2415bcbbb24aSTao Ma 			*credits += 1;
2416bcbbb24aSTao Ma 			brelse(prev_bh);
2417bcbbb24aSTao Ma 			prev_bh = ref_leaf_bh;
2418bcbbb24aSTao Ma 			get_bh(prev_bh);
2419bcbbb24aSTao Ma 		}
2420bcbbb24aSTao Ma 
2421bcbbb24aSTao Ma 		rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
2422bcbbb24aSTao Ma 
2423bcbbb24aSTao Ma 		mlog(0, "recs_add %d,cpos %llu, clusters %u, rec->r_cpos %llu,"
2424bcbbb24aSTao Ma 		     "rec->r_clusters %u, rec->r_refcount %u, index %d\n",
2425bcbbb24aSTao Ma 		     recs_add, (unsigned long long)cpos, clusters,
2426bcbbb24aSTao Ma 		     (unsigned long long)le64_to_cpu(rec.r_cpos),
2427bcbbb24aSTao Ma 		     le32_to_cpu(rec.r_clusters),
2428bcbbb24aSTao Ma 		     le32_to_cpu(rec.r_refcount), index);
2429bcbbb24aSTao Ma 
2430bcbbb24aSTao Ma 		len = min((u64)cpos + clusters, le64_to_cpu(rec.r_cpos) +
2431bcbbb24aSTao Ma 			  le32_to_cpu(rec.r_clusters)) - cpos;
2432bcbbb24aSTao Ma 		/*
2433bcbbb24aSTao Ma 		 * If the refcount rec already exist, cool. We just need
2434bcbbb24aSTao Ma 		 * to check whether there is a split. Otherwise we just need
2435bcbbb24aSTao Ma 		 * to increase the refcount.
2436bcbbb24aSTao Ma 		 * If we will insert one, increases recs_add.
2437bcbbb24aSTao Ma 		 *
2438bcbbb24aSTao Ma 		 * We record all the records which will be inserted to the
2439bcbbb24aSTao Ma 		 * same refcount block, so that we can tell exactly whether
2440bcbbb24aSTao Ma 		 * we need a new refcount block or not.
2441bcbbb24aSTao Ma 		 */
2442bcbbb24aSTao Ma 		if (rec.r_refcount) {
2443bcbbb24aSTao Ma 			/* Check whether we need a split at the beginning. */
2444bcbbb24aSTao Ma 			if (cpos == start_cpos &&
2445bcbbb24aSTao Ma 			    cpos != le64_to_cpu(rec.r_cpos))
2446bcbbb24aSTao Ma 				recs_add++;
2447bcbbb24aSTao Ma 
2448bcbbb24aSTao Ma 			/* Check whether we need a split in the end. */
2449bcbbb24aSTao Ma 			if (cpos + clusters < le64_to_cpu(rec.r_cpos) +
2450bcbbb24aSTao Ma 			    le32_to_cpu(rec.r_clusters))
2451bcbbb24aSTao Ma 				recs_add++;
2452bcbbb24aSTao Ma 		} else
2453bcbbb24aSTao Ma 			recs_add++;
2454bcbbb24aSTao Ma 
2455bcbbb24aSTao Ma 		brelse(ref_leaf_bh);
2456bcbbb24aSTao Ma 		ref_leaf_bh = NULL;
2457bcbbb24aSTao Ma 		clusters -= len;
2458bcbbb24aSTao Ma 		cpos += len;
2459bcbbb24aSTao Ma 	}
2460bcbbb24aSTao Ma 
2461bcbbb24aSTao Ma 	if (prev_bh) {
2462bcbbb24aSTao Ma 		rb = (struct ocfs2_refcount_block *)prev_bh->b_data;
2463bcbbb24aSTao Ma 
2464bcbbb24aSTao Ma 		if (le64_to_cpu(rb->rf_records.rl_used) + recs_add >
2465bcbbb24aSTao Ma 		    le16_to_cpu(rb->rf_records.rl_count))
2466bcbbb24aSTao Ma 			ref_blocks++;
2467bcbbb24aSTao Ma 
2468bcbbb24aSTao Ma 		*credits += 1;
2469bcbbb24aSTao Ma 	}
2470bcbbb24aSTao Ma 
2471bcbbb24aSTao Ma 	if (!ref_blocks)
2472bcbbb24aSTao Ma 		goto out;
2473bcbbb24aSTao Ma 
2474bcbbb24aSTao Ma 	mlog(0, "we need ref_blocks %d\n", ref_blocks);
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,
2492bcbbb24aSTao Ma 						      et.et_root_el,
2493bcbbb24aSTao Ma 						      ref_blocks);
2494bcbbb24aSTao Ma 	} else {
2495bcbbb24aSTao Ma 		*credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
2496bcbbb24aSTao Ma 		*meta_add += 1;
2497bcbbb24aSTao Ma 	}
2498bcbbb24aSTao Ma 
2499bcbbb24aSTao Ma out:
2500bcbbb24aSTao Ma 	brelse(ref_leaf_bh);
2501bcbbb24aSTao Ma 	brelse(prev_bh);
2502bcbbb24aSTao Ma 	return ret;
2503bcbbb24aSTao Ma }
2504bcbbb24aSTao Ma 
2505bcbbb24aSTao Ma /*
2506bcbbb24aSTao Ma  * For refcount tree, we will decrease some contiguous clusters
2507bcbbb24aSTao Ma  * refcount count, so just go through it to see how many blocks
2508bcbbb24aSTao Ma  * we gonna touch and whether we need to create new blocks.
2509bcbbb24aSTao Ma  *
2510bcbbb24aSTao Ma  * Normally the refcount blocks store these refcount should be
2511af901ca1SAndré Goddard Rosa  * contiguous also, so that we can get the number easily.
2512bcbbb24aSTao Ma  * As for meta_ac, we will at most add split 2 refcount record and
2513bcbbb24aSTao Ma  * 2 more refcount block, so just check it in a rough way.
2514bcbbb24aSTao Ma  *
2515bcbbb24aSTao Ma  * Caller must hold refcount tree lock.
2516bcbbb24aSTao Ma  */
2517bcbbb24aSTao Ma int ocfs2_prepare_refcount_change_for_del(struct inode *inode,
2518bcbbb24aSTao Ma 					  struct buffer_head *di_bh,
2519bcbbb24aSTao Ma 					  u64 phys_blkno,
2520bcbbb24aSTao Ma 					  u32 clusters,
2521bcbbb24aSTao Ma 					  int *credits,
2522bcbbb24aSTao Ma 					  struct ocfs2_alloc_context **meta_ac)
2523bcbbb24aSTao Ma {
2524bcbbb24aSTao Ma 	int ret, ref_blocks = 0;
2525bcbbb24aSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2526bcbbb24aSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2527bcbbb24aSTao Ma 	struct buffer_head *ref_root_bh = NULL;
2528bcbbb24aSTao Ma 	struct ocfs2_refcount_tree *tree;
2529bcbbb24aSTao Ma 	u64 start_cpos = ocfs2_blocks_to_clusters(inode->i_sb, phys_blkno);
2530bcbbb24aSTao Ma 
2531bcbbb24aSTao Ma 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
2532bcbbb24aSTao Ma 		ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
2533bcbbb24aSTao Ma 			    "tree, but the feature bit is not set in the "
2534bcbbb24aSTao Ma 			    "super block.", inode->i_ino);
2535bcbbb24aSTao Ma 		ret = -EROFS;
2536bcbbb24aSTao Ma 		goto out;
2537bcbbb24aSTao Ma 	}
2538bcbbb24aSTao Ma 
2539bcbbb24aSTao Ma 	BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
2540bcbbb24aSTao Ma 
2541bcbbb24aSTao Ma 	ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb),
2542bcbbb24aSTao Ma 				      le64_to_cpu(di->i_refcount_loc), &tree);
2543bcbbb24aSTao Ma 	if (ret) {
2544bcbbb24aSTao Ma 		mlog_errno(ret);
2545bcbbb24aSTao Ma 		goto out;
2546bcbbb24aSTao Ma 	}
2547bcbbb24aSTao Ma 
2548bcbbb24aSTao Ma 	ret = ocfs2_read_refcount_block(&tree->rf_ci,
2549bcbbb24aSTao Ma 					le64_to_cpu(di->i_refcount_loc),
2550bcbbb24aSTao Ma 					&ref_root_bh);
2551bcbbb24aSTao Ma 	if (ret) {
2552bcbbb24aSTao Ma 		mlog_errno(ret);
2553bcbbb24aSTao Ma 		goto out;
2554bcbbb24aSTao Ma 	}
2555bcbbb24aSTao Ma 
2556bcbbb24aSTao Ma 	ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
2557bcbbb24aSTao Ma 					       &tree->rf_ci,
2558bcbbb24aSTao Ma 					       ref_root_bh,
2559bcbbb24aSTao Ma 					       start_cpos, clusters,
2560bcbbb24aSTao Ma 					       &ref_blocks, credits);
2561bcbbb24aSTao Ma 	if (ret) {
2562bcbbb24aSTao Ma 		mlog_errno(ret);
2563bcbbb24aSTao Ma 		goto out;
2564bcbbb24aSTao Ma 	}
2565bcbbb24aSTao Ma 
2566bcbbb24aSTao Ma 	mlog(0, "reserve new metadata %d, credits = %d\n",
2567bcbbb24aSTao Ma 	     ref_blocks, *credits);
2568bcbbb24aSTao Ma 
2569bcbbb24aSTao Ma 	if (ref_blocks) {
2570bcbbb24aSTao Ma 		ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2571bcbbb24aSTao Ma 							ref_blocks, meta_ac);
2572bcbbb24aSTao Ma 		if (ret)
2573bcbbb24aSTao Ma 			mlog_errno(ret);
2574bcbbb24aSTao Ma 	}
2575bcbbb24aSTao Ma 
2576bcbbb24aSTao Ma out:
2577bcbbb24aSTao Ma 	brelse(ref_root_bh);
2578bcbbb24aSTao Ma 	return ret;
2579bcbbb24aSTao Ma }
25806f70fa51STao Ma 
25816f70fa51STao Ma #define	MAX_CONTIG_BYTES	1048576
25826f70fa51STao Ma 
25836f70fa51STao Ma static inline unsigned int ocfs2_cow_contig_clusters(struct super_block *sb)
25846f70fa51STao Ma {
25856f70fa51STao Ma 	return ocfs2_clusters_for_bytes(sb, MAX_CONTIG_BYTES);
25866f70fa51STao Ma }
25876f70fa51STao Ma 
25886f70fa51STao Ma static inline unsigned int ocfs2_cow_contig_mask(struct super_block *sb)
25896f70fa51STao Ma {
25906f70fa51STao Ma 	return ~(ocfs2_cow_contig_clusters(sb) - 1);
25916f70fa51STao Ma }
25926f70fa51STao Ma 
25936f70fa51STao Ma /*
25946f70fa51STao Ma  * Given an extent that starts at 'start' and an I/O that starts at 'cpos',
25956f70fa51STao Ma  * find an offset (start + (n * contig_clusters)) that is closest to cpos
25966f70fa51STao Ma  * while still being less than or equal to it.
25976f70fa51STao Ma  *
25986f70fa51STao Ma  * The goal is to break the extent at a multiple of contig_clusters.
25996f70fa51STao Ma  */
26006f70fa51STao Ma static inline unsigned int ocfs2_cow_align_start(struct super_block *sb,
26016f70fa51STao Ma 						 unsigned int start,
26026f70fa51STao Ma 						 unsigned int cpos)
26036f70fa51STao Ma {
26046f70fa51STao Ma 	BUG_ON(start > cpos);
26056f70fa51STao Ma 
26066f70fa51STao Ma 	return start + ((cpos - start) & ocfs2_cow_contig_mask(sb));
26076f70fa51STao Ma }
26086f70fa51STao Ma 
26096f70fa51STao Ma /*
26106f70fa51STao Ma  * Given a cluster count of len, pad it out so that it is a multiple
26116f70fa51STao Ma  * of contig_clusters.
26126f70fa51STao Ma  */
26136f70fa51STao Ma static inline unsigned int ocfs2_cow_align_length(struct super_block *sb,
26146f70fa51STao Ma 						  unsigned int len)
26156f70fa51STao Ma {
26166f70fa51STao Ma 	unsigned int padded =
26176f70fa51STao Ma 		(len + (ocfs2_cow_contig_clusters(sb) - 1)) &
26186f70fa51STao Ma 		ocfs2_cow_contig_mask(sb);
26196f70fa51STao Ma 
26206f70fa51STao Ma 	/* Did we wrap? */
26216f70fa51STao Ma 	if (padded < len)
26226f70fa51STao Ma 		padded = UINT_MAX;
26236f70fa51STao Ma 
26246f70fa51STao Ma 	return padded;
26256f70fa51STao Ma }
26266f70fa51STao Ma 
26276f70fa51STao Ma /*
26286f70fa51STao Ma  * Calculate out the start and number of virtual clusters we need to to CoW.
26296f70fa51STao Ma  *
26306f70fa51STao Ma  * cpos is vitual start cluster position we want to do CoW in a
26316f70fa51STao Ma  * file and write_len is the cluster length.
263237f8a2bfSTao Ma  * max_cpos is the place where we want to stop CoW intentionally.
26336f70fa51STao Ma  *
26346f70fa51STao Ma  * Normal we will start CoW from the beginning of extent record cotaining cpos.
26356f70fa51STao Ma  * We try to break up extents on boundaries of MAX_CONTIG_BYTES so that we
26366f70fa51STao Ma  * get good I/O from the resulting extent tree.
26376f70fa51STao Ma  */
26386f70fa51STao Ma static int ocfs2_refcount_cal_cow_clusters(struct inode *inode,
2639913580b4STao Ma 					   struct ocfs2_extent_list *el,
26406f70fa51STao Ma 					   u32 cpos,
26416f70fa51STao Ma 					   u32 write_len,
264237f8a2bfSTao Ma 					   u32 max_cpos,
26436f70fa51STao Ma 					   u32 *cow_start,
26446f70fa51STao Ma 					   u32 *cow_len)
26456f70fa51STao Ma {
26466f70fa51STao Ma 	int ret = 0;
26476f70fa51STao Ma 	int tree_height = le16_to_cpu(el->l_tree_depth), i;
26486f70fa51STao Ma 	struct buffer_head *eb_bh = NULL;
26496f70fa51STao Ma 	struct ocfs2_extent_block *eb = NULL;
26506f70fa51STao Ma 	struct ocfs2_extent_rec *rec;
26516f70fa51STao Ma 	unsigned int want_clusters, rec_end = 0;
26526f70fa51STao Ma 	int contig_clusters = ocfs2_cow_contig_clusters(inode->i_sb);
26536f70fa51STao Ma 	int leaf_clusters;
26546f70fa51STao Ma 
265537f8a2bfSTao Ma 	BUG_ON(cpos + write_len > max_cpos);
265637f8a2bfSTao Ma 
26576f70fa51STao Ma 	if (tree_height > 0) {
26586f70fa51STao Ma 		ret = ocfs2_find_leaf(INODE_CACHE(inode), el, cpos, &eb_bh);
26596f70fa51STao Ma 		if (ret) {
26606f70fa51STao Ma 			mlog_errno(ret);
26616f70fa51STao Ma 			goto out;
26626f70fa51STao Ma 		}
26636f70fa51STao Ma 
26646f70fa51STao Ma 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
26656f70fa51STao Ma 		el = &eb->h_list;
26666f70fa51STao Ma 
26676f70fa51STao Ma 		if (el->l_tree_depth) {
26686f70fa51STao Ma 			ocfs2_error(inode->i_sb,
26696f70fa51STao Ma 				    "Inode %lu has non zero tree depth in "
26706f70fa51STao Ma 				    "leaf block %llu\n", inode->i_ino,
26716f70fa51STao Ma 				    (unsigned long long)eb_bh->b_blocknr);
26726f70fa51STao Ma 			ret = -EROFS;
26736f70fa51STao Ma 			goto out;
26746f70fa51STao Ma 		}
26756f70fa51STao Ma 	}
26766f70fa51STao Ma 
26776f70fa51STao Ma 	*cow_len = 0;
26786f70fa51STao Ma 	for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
26796f70fa51STao Ma 		rec = &el->l_recs[i];
26806f70fa51STao Ma 
26816f70fa51STao Ma 		if (ocfs2_is_empty_extent(rec)) {
26826f70fa51STao Ma 			mlog_bug_on_msg(i != 0, "Inode %lu has empty record in "
26836f70fa51STao Ma 					"index %d\n", inode->i_ino, i);
26846f70fa51STao Ma 			continue;
26856f70fa51STao Ma 		}
26866f70fa51STao Ma 
26876f70fa51STao Ma 		if (le32_to_cpu(rec->e_cpos) +
26886f70fa51STao Ma 		    le16_to_cpu(rec->e_leaf_clusters) <= cpos)
26896f70fa51STao Ma 			continue;
26906f70fa51STao Ma 
26916f70fa51STao Ma 		if (*cow_len == 0) {
26926f70fa51STao Ma 			/*
26936f70fa51STao Ma 			 * We should find a refcounted record in the
26946f70fa51STao Ma 			 * first pass.
26956f70fa51STao Ma 			 */
26966f70fa51STao Ma 			BUG_ON(!(rec->e_flags & OCFS2_EXT_REFCOUNTED));
26976f70fa51STao Ma 			*cow_start = le32_to_cpu(rec->e_cpos);
26986f70fa51STao Ma 		}
26996f70fa51STao Ma 
27006f70fa51STao Ma 		/*
270137f8a2bfSTao Ma 		 * If we encounter a hole, a non-refcounted record or
270237f8a2bfSTao Ma 		 * pass the max_cpos, stop the search.
27036f70fa51STao Ma 		 */
27046f70fa51STao Ma 		if ((!(rec->e_flags & OCFS2_EXT_REFCOUNTED)) ||
270537f8a2bfSTao Ma 		    (*cow_len && rec_end != le32_to_cpu(rec->e_cpos)) ||
270637f8a2bfSTao Ma 		    (max_cpos <= le32_to_cpu(rec->e_cpos)))
27076f70fa51STao Ma 			break;
27086f70fa51STao Ma 
27096f70fa51STao Ma 		leaf_clusters = le16_to_cpu(rec->e_leaf_clusters);
27106f70fa51STao Ma 		rec_end = le32_to_cpu(rec->e_cpos) + leaf_clusters;
271137f8a2bfSTao Ma 		if (rec_end > max_cpos) {
271237f8a2bfSTao Ma 			rec_end = max_cpos;
271337f8a2bfSTao Ma 			leaf_clusters = rec_end - le32_to_cpu(rec->e_cpos);
271437f8a2bfSTao Ma 		}
27156f70fa51STao Ma 
27166f70fa51STao Ma 		/*
27176f70fa51STao Ma 		 * How many clusters do we actually need from
27186f70fa51STao Ma 		 * this extent?  First we see how many we actually
27196f70fa51STao Ma 		 * need to complete the write.  If that's smaller
27206f70fa51STao Ma 		 * than contig_clusters, we try for contig_clusters.
27216f70fa51STao Ma 		 */
27226f70fa51STao Ma 		if (!*cow_len)
27236f70fa51STao Ma 			want_clusters = write_len;
27246f70fa51STao Ma 		else
27256f70fa51STao Ma 			want_clusters = (cpos + write_len) -
27266f70fa51STao Ma 				(*cow_start + *cow_len);
27276f70fa51STao Ma 		if (want_clusters < contig_clusters)
27286f70fa51STao Ma 			want_clusters = contig_clusters;
27296f70fa51STao Ma 
27306f70fa51STao Ma 		/*
27316f70fa51STao Ma 		 * If the write does not cover the whole extent, we
27326f70fa51STao Ma 		 * need to calculate how we're going to split the extent.
27336f70fa51STao Ma 		 * We try to do it on contig_clusters boundaries.
27346f70fa51STao Ma 		 *
27356f70fa51STao Ma 		 * Any extent smaller than contig_clusters will be
27366f70fa51STao Ma 		 * CoWed in its entirety.
27376f70fa51STao Ma 		 */
27386f70fa51STao Ma 		if (leaf_clusters <= contig_clusters)
27396f70fa51STao Ma 			*cow_len += leaf_clusters;
27406f70fa51STao Ma 		else if (*cow_len || (*cow_start == cpos)) {
27416f70fa51STao Ma 			/*
27426f70fa51STao Ma 			 * This extent needs to be CoW'd from its
27436f70fa51STao Ma 			 * beginning, so all we have to do is compute
27446f70fa51STao Ma 			 * how many clusters to grab.  We align
27456f70fa51STao Ma 			 * want_clusters to the edge of contig_clusters
27466f70fa51STao Ma 			 * to get better I/O.
27476f70fa51STao Ma 			 */
27486f70fa51STao Ma 			want_clusters = ocfs2_cow_align_length(inode->i_sb,
27496f70fa51STao Ma 							       want_clusters);
27506f70fa51STao Ma 
27516f70fa51STao Ma 			if (leaf_clusters < want_clusters)
27526f70fa51STao Ma 				*cow_len += leaf_clusters;
27536f70fa51STao Ma 			else
27546f70fa51STao Ma 				*cow_len += want_clusters;
27556f70fa51STao Ma 		} else if ((*cow_start + contig_clusters) >=
27566f70fa51STao Ma 			   (cpos + write_len)) {
27576f70fa51STao Ma 			/*
27586f70fa51STao Ma 			 * Breaking off contig_clusters at the front
27596f70fa51STao Ma 			 * of the extent will cover our write.  That's
27606f70fa51STao Ma 			 * easy.
27616f70fa51STao Ma 			 */
27626f70fa51STao Ma 			*cow_len = contig_clusters;
27636f70fa51STao Ma 		} else if ((rec_end - cpos) <= contig_clusters) {
27646f70fa51STao Ma 			/*
27656f70fa51STao Ma 			 * Breaking off contig_clusters at the tail of
27666f70fa51STao Ma 			 * this extent will cover cpos.
27676f70fa51STao Ma 			 */
27686f70fa51STao Ma 			*cow_start = rec_end - contig_clusters;
27696f70fa51STao Ma 			*cow_len = contig_clusters;
27706f70fa51STao Ma 		} else if ((rec_end - cpos) <= want_clusters) {
27716f70fa51STao Ma 			/*
27726f70fa51STao Ma 			 * While we can't fit the entire write in this
27736f70fa51STao Ma 			 * extent, we know that the write goes from cpos
27746f70fa51STao Ma 			 * to the end of the extent.  Break that off.
27756f70fa51STao Ma 			 * We try to break it at some multiple of
27766f70fa51STao Ma 			 * contig_clusters from the front of the extent.
27776f70fa51STao Ma 			 * Failing that (ie, cpos is within
27786f70fa51STao Ma 			 * contig_clusters of the front), we'll CoW the
27796f70fa51STao Ma 			 * entire extent.
27806f70fa51STao Ma 			 */
27816f70fa51STao Ma 			*cow_start = ocfs2_cow_align_start(inode->i_sb,
27826f70fa51STao Ma 							   *cow_start, cpos);
27836f70fa51STao Ma 			*cow_len = rec_end - *cow_start;
27846f70fa51STao Ma 		} else {
27856f70fa51STao Ma 			/*
27866f70fa51STao Ma 			 * Ok, the entire write lives in the middle of
27876f70fa51STao Ma 			 * this extent.  Let's try to slice the extent up
27886f70fa51STao Ma 			 * nicely.  Optimally, our CoW region starts at
27896f70fa51STao Ma 			 * m*contig_clusters from the beginning of the
27906f70fa51STao Ma 			 * extent and goes for n*contig_clusters,
27916f70fa51STao Ma 			 * covering the entire write.
27926f70fa51STao Ma 			 */
27936f70fa51STao Ma 			*cow_start = ocfs2_cow_align_start(inode->i_sb,
27946f70fa51STao Ma 							   *cow_start, cpos);
27956f70fa51STao Ma 
27966f70fa51STao Ma 			want_clusters = (cpos + write_len) - *cow_start;
27976f70fa51STao Ma 			want_clusters = ocfs2_cow_align_length(inode->i_sb,
27986f70fa51STao Ma 							       want_clusters);
27996f70fa51STao Ma 			if (*cow_start + want_clusters <= rec_end)
28006f70fa51STao Ma 				*cow_len = want_clusters;
28016f70fa51STao Ma 			else
28026f70fa51STao Ma 				*cow_len = rec_end - *cow_start;
28036f70fa51STao Ma 		}
28046f70fa51STao Ma 
28056f70fa51STao Ma 		/* Have we covered our entire write yet? */
28066f70fa51STao Ma 		if ((*cow_start + *cow_len) >= (cpos + write_len))
28076f70fa51STao Ma 			break;
28086f70fa51STao Ma 
28096f70fa51STao Ma 		/*
28106f70fa51STao Ma 		 * If we reach the end of the extent block and don't get enough
28116f70fa51STao Ma 		 * clusters, continue with the next extent block if possible.
28126f70fa51STao Ma 		 */
28136f70fa51STao Ma 		if (i + 1 == le16_to_cpu(el->l_next_free_rec) &&
28146f70fa51STao Ma 		    eb && eb->h_next_leaf_blk) {
28156f70fa51STao Ma 			brelse(eb_bh);
28166f70fa51STao Ma 			eb_bh = NULL;
28176f70fa51STao Ma 
28186f70fa51STao Ma 			ret = ocfs2_read_extent_block(INODE_CACHE(inode),
28196f70fa51STao Ma 					       le64_to_cpu(eb->h_next_leaf_blk),
28206f70fa51STao Ma 					       &eb_bh);
28216f70fa51STao Ma 			if (ret) {
28226f70fa51STao Ma 				mlog_errno(ret);
28236f70fa51STao Ma 				goto out;
28246f70fa51STao Ma 			}
28256f70fa51STao Ma 
28266f70fa51STao Ma 			eb = (struct ocfs2_extent_block *) eb_bh->b_data;
28276f70fa51STao Ma 			el = &eb->h_list;
28286f70fa51STao Ma 			i = -1;
28296f70fa51STao Ma 		}
28306f70fa51STao Ma 	}
28316f70fa51STao Ma 
28326f70fa51STao Ma out:
28336f70fa51STao Ma 	brelse(eb_bh);
28346f70fa51STao Ma 	return ret;
28356f70fa51STao Ma }
28366f70fa51STao Ma 
28376f70fa51STao Ma /*
28386f70fa51STao Ma  * Prepare meta_ac, data_ac and calculate credits when we want to add some
28396f70fa51STao Ma  * num_clusters in data_tree "et" and change the refcount for the old
28406f70fa51STao Ma  * clusters(starting form p_cluster) in the refcount tree.
28416f70fa51STao Ma  *
28426f70fa51STao Ma  * Note:
28436f70fa51STao Ma  * 1. since we may split the old tree, so we at most will need num_clusters + 2
28446f70fa51STao Ma  *    more new leaf records.
28456f70fa51STao Ma  * 2. In some case, we may not need to reserve new clusters(e.g, reflink), so
28466f70fa51STao Ma  *    just give data_ac = NULL.
28476f70fa51STao Ma  */
28486f70fa51STao Ma static int ocfs2_lock_refcount_allocators(struct super_block *sb,
28496f70fa51STao Ma 					u32 p_cluster, u32 num_clusters,
28506f70fa51STao Ma 					struct ocfs2_extent_tree *et,
28516f70fa51STao Ma 					struct ocfs2_caching_info *ref_ci,
28526f70fa51STao Ma 					struct buffer_head *ref_root_bh,
28536f70fa51STao Ma 					struct ocfs2_alloc_context **meta_ac,
28546f70fa51STao Ma 					struct ocfs2_alloc_context **data_ac,
28556f70fa51STao Ma 					int *credits)
28566f70fa51STao Ma {
28576f70fa51STao Ma 	int ret = 0, meta_add = 0;
28586f70fa51STao Ma 	int num_free_extents = ocfs2_num_free_extents(OCFS2_SB(sb), et);
28596f70fa51STao Ma 
28606f70fa51STao Ma 	if (num_free_extents < 0) {
28616f70fa51STao Ma 		ret = num_free_extents;
28626f70fa51STao Ma 		mlog_errno(ret);
28636f70fa51STao Ma 		goto out;
28646f70fa51STao Ma 	}
28656f70fa51STao Ma 
28666f70fa51STao Ma 	if (num_free_extents < num_clusters + 2)
28676f70fa51STao Ma 		meta_add =
28686f70fa51STao Ma 			ocfs2_extend_meta_needed(et->et_root_el);
28696f70fa51STao Ma 
28706f70fa51STao Ma 	*credits += ocfs2_calc_extend_credits(sb, et->et_root_el,
28716f70fa51STao Ma 					      num_clusters + 2);
28726f70fa51STao Ma 
28736f70fa51STao Ma 	ret = ocfs2_calc_refcount_meta_credits(sb, ref_ci, ref_root_bh,
28746f70fa51STao Ma 					       p_cluster, num_clusters,
28756f70fa51STao Ma 					       &meta_add, credits);
28766f70fa51STao Ma 	if (ret) {
28776f70fa51STao Ma 		mlog_errno(ret);
28786f70fa51STao Ma 		goto out;
28796f70fa51STao Ma 	}
28806f70fa51STao Ma 
28816f70fa51STao Ma 	mlog(0, "reserve new metadata %d, clusters %u, credits = %d\n",
28826f70fa51STao Ma 	     meta_add, num_clusters, *credits);
28836f70fa51STao Ma 	ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(sb), meta_add,
28846f70fa51STao Ma 						meta_ac);
28856f70fa51STao Ma 	if (ret) {
28866f70fa51STao Ma 		mlog_errno(ret);
28876f70fa51STao Ma 		goto out;
28886f70fa51STao Ma 	}
28896f70fa51STao Ma 
28906f70fa51STao Ma 	if (data_ac) {
28916f70fa51STao Ma 		ret = ocfs2_reserve_clusters(OCFS2_SB(sb), num_clusters,
28926f70fa51STao Ma 					     data_ac);
28936f70fa51STao Ma 		if (ret)
28946f70fa51STao Ma 			mlog_errno(ret);
28956f70fa51STao Ma 	}
28966f70fa51STao Ma 
28976f70fa51STao Ma out:
28986f70fa51STao Ma 	if (ret) {
28996f70fa51STao Ma 		if (*meta_ac) {
29006f70fa51STao Ma 			ocfs2_free_alloc_context(*meta_ac);
29016f70fa51STao Ma 			*meta_ac = NULL;
29026f70fa51STao Ma 		}
29036f70fa51STao Ma 	}
29046f70fa51STao Ma 
29056f70fa51STao Ma 	return ret;
29066f70fa51STao Ma }
29076f70fa51STao Ma 
29086f70fa51STao Ma static int ocfs2_clear_cow_buffer(handle_t *handle, struct buffer_head *bh)
29096f70fa51STao Ma {
29106f70fa51STao Ma 	BUG_ON(buffer_dirty(bh));
29116f70fa51STao Ma 
29126f70fa51STao Ma 	clear_buffer_mapped(bh);
29136f70fa51STao Ma 
29146f70fa51STao Ma 	return 0;
29156f70fa51STao Ma }
29166f70fa51STao Ma 
2917913580b4STao Ma static int ocfs2_duplicate_clusters_by_page(handle_t *handle,
29186f70fa51STao Ma 					    struct ocfs2_cow_context *context,
29196f70fa51STao Ma 					    u32 cpos, u32 old_cluster,
29206f70fa51STao Ma 					    u32 new_cluster, u32 new_len)
29216f70fa51STao Ma {
29226f70fa51STao Ma 	int ret = 0, partial;
2923913580b4STao Ma 	struct ocfs2_caching_info *ci = context->data_et.et_ci;
29246f70fa51STao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
29256f70fa51STao Ma 	u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
29266f70fa51STao Ma 	struct page *page;
29276f70fa51STao Ma 	pgoff_t page_index;
29286f70fa51STao Ma 	unsigned int from, to;
29296f70fa51STao Ma 	loff_t offset, end, map_end;
29306f70fa51STao Ma 	struct address_space *mapping = context->inode->i_mapping;
29316f70fa51STao Ma 
29326f70fa51STao Ma 	mlog(0, "old_cluster %u, new %u, len %u at offset %u\n", old_cluster,
29336f70fa51STao Ma 	     new_cluster, new_len, cpos);
29346f70fa51STao Ma 
29356f70fa51STao Ma 	offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
29366f70fa51STao Ma 	end = offset + (new_len << OCFS2_SB(sb)->s_clustersize_bits);
29376f70fa51STao Ma 
29386f70fa51STao Ma 	while (offset < end) {
29396f70fa51STao Ma 		page_index = offset >> PAGE_CACHE_SHIFT;
2940d622b89aSTao Ma 		map_end = ((loff_t)page_index + 1) << PAGE_CACHE_SHIFT;
29416f70fa51STao Ma 		if (map_end > end)
29426f70fa51STao Ma 			map_end = end;
29436f70fa51STao Ma 
29446f70fa51STao Ma 		/* from, to is the offset within the page. */
29456f70fa51STao Ma 		from = offset & (PAGE_CACHE_SIZE - 1);
29466f70fa51STao Ma 		to = PAGE_CACHE_SIZE;
29476f70fa51STao Ma 		if (map_end & (PAGE_CACHE_SIZE - 1))
29486f70fa51STao Ma 			to = map_end & (PAGE_CACHE_SIZE - 1);
29496f70fa51STao Ma 
29506f70fa51STao Ma 		page = grab_cache_page(mapping, page_index);
29516f70fa51STao Ma 
29520a1ea437STao Ma 		/*
29530a1ea437STao Ma 		 * In case PAGE_CACHE_SIZE <= CLUSTER_SIZE, This page
29540a1ea437STao Ma 		 * can't be dirtied before we CoW it out.
29550a1ea437STao Ma 		 */
29560a1ea437STao Ma 		if (PAGE_CACHE_SIZE <= OCFS2_SB(sb)->s_clustersize)
29576f70fa51STao Ma 			BUG_ON(PageDirty(page));
29586f70fa51STao Ma 
29596f70fa51STao Ma 		if (!PageUptodate(page)) {
29606f70fa51STao Ma 			ret = block_read_full_page(page, ocfs2_get_block);
29616f70fa51STao Ma 			if (ret) {
29626f70fa51STao Ma 				mlog_errno(ret);
29636f70fa51STao Ma 				goto unlock;
29646f70fa51STao Ma 			}
29656f70fa51STao Ma 			lock_page(page);
29666f70fa51STao Ma 		}
29676f70fa51STao Ma 
29686f70fa51STao Ma 		if (page_has_buffers(page)) {
29696f70fa51STao Ma 			ret = walk_page_buffers(handle, page_buffers(page),
29706f70fa51STao Ma 						from, to, &partial,
29716f70fa51STao Ma 						ocfs2_clear_cow_buffer);
29726f70fa51STao Ma 			if (ret) {
29736f70fa51STao Ma 				mlog_errno(ret);
29746f70fa51STao Ma 				goto unlock;
29756f70fa51STao Ma 			}
29766f70fa51STao Ma 		}
29776f70fa51STao Ma 
29786f70fa51STao Ma 		ocfs2_map_and_dirty_page(context->inode,
29796f70fa51STao Ma 					 handle, from, to,
29806f70fa51STao Ma 					 page, 0, &new_block);
29816f70fa51STao Ma 		mark_page_accessed(page);
29826f70fa51STao Ma unlock:
29836f70fa51STao Ma 		unlock_page(page);
29846f70fa51STao Ma 		page_cache_release(page);
29856f70fa51STao Ma 		page = NULL;
29866f70fa51STao Ma 		offset = map_end;
29876f70fa51STao Ma 		if (ret)
29886f70fa51STao Ma 			break;
29896f70fa51STao Ma 	}
29906f70fa51STao Ma 
29916f70fa51STao Ma 	return ret;
29926f70fa51STao Ma }
29936f70fa51STao Ma 
2994492a8a33STao Ma static int ocfs2_duplicate_clusters_by_jbd(handle_t *handle,
2995492a8a33STao Ma 					   struct ocfs2_cow_context *context,
2996492a8a33STao Ma 					   u32 cpos, u32 old_cluster,
2997492a8a33STao Ma 					   u32 new_cluster, u32 new_len)
2998492a8a33STao Ma {
2999492a8a33STao Ma 	int ret = 0;
3000492a8a33STao Ma 	struct super_block *sb = context->inode->i_sb;
3001492a8a33STao Ma 	struct ocfs2_caching_info *ci = context->data_et.et_ci;
3002492a8a33STao Ma 	int i, blocks = ocfs2_clusters_to_blocks(sb, new_len);
3003492a8a33STao Ma 	u64 old_block = ocfs2_clusters_to_blocks(sb, old_cluster);
3004492a8a33STao Ma 	u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
3005492a8a33STao Ma 	struct ocfs2_super *osb = OCFS2_SB(sb);
3006492a8a33STao Ma 	struct buffer_head *old_bh = NULL;
3007492a8a33STao Ma 	struct buffer_head *new_bh = NULL;
3008492a8a33STao Ma 
3009492a8a33STao Ma 	mlog(0, "old_cluster %u, new %u, len %u\n", old_cluster,
3010492a8a33STao Ma 	     new_cluster, new_len);
3011492a8a33STao Ma 
3012492a8a33STao Ma 	for (i = 0; i < blocks; i++, old_block++, new_block++) {
3013492a8a33STao Ma 		new_bh = sb_getblk(osb->sb, new_block);
3014492a8a33STao Ma 		if (new_bh == NULL) {
3015492a8a33STao Ma 			ret = -EIO;
3016492a8a33STao Ma 			mlog_errno(ret);
3017492a8a33STao Ma 			break;
3018492a8a33STao Ma 		}
3019492a8a33STao Ma 
3020492a8a33STao Ma 		ocfs2_set_new_buffer_uptodate(ci, new_bh);
3021492a8a33STao Ma 
3022492a8a33STao Ma 		ret = ocfs2_read_block(ci, old_block, &old_bh, NULL);
3023492a8a33STao Ma 		if (ret) {
3024492a8a33STao Ma 			mlog_errno(ret);
3025492a8a33STao Ma 			break;
3026492a8a33STao Ma 		}
3027492a8a33STao Ma 
3028492a8a33STao Ma 		ret = ocfs2_journal_access(handle, ci, new_bh,
3029492a8a33STao Ma 					   OCFS2_JOURNAL_ACCESS_CREATE);
3030492a8a33STao Ma 		if (ret) {
3031492a8a33STao Ma 			mlog_errno(ret);
3032492a8a33STao Ma 			break;
3033492a8a33STao Ma 		}
3034492a8a33STao Ma 
3035492a8a33STao Ma 		memcpy(new_bh->b_data, old_bh->b_data, sb->s_blocksize);
3036ec20cec7SJoel Becker 		ocfs2_journal_dirty(handle, new_bh);
3037492a8a33STao Ma 
3038492a8a33STao Ma 		brelse(new_bh);
3039492a8a33STao Ma 		brelse(old_bh);
3040492a8a33STao Ma 		new_bh = NULL;
3041492a8a33STao Ma 		old_bh = NULL;
3042492a8a33STao Ma 	}
3043492a8a33STao Ma 
3044492a8a33STao Ma 	brelse(new_bh);
3045492a8a33STao Ma 	brelse(old_bh);
3046492a8a33STao Ma 	return ret;
3047492a8a33STao Ma }
3048492a8a33STao Ma 
30496f70fa51STao Ma static int ocfs2_clear_ext_refcount(handle_t *handle,
30506f70fa51STao Ma 				    struct ocfs2_extent_tree *et,
30516f70fa51STao Ma 				    u32 cpos, u32 p_cluster, u32 len,
30526f70fa51STao Ma 				    unsigned int ext_flags,
30536f70fa51STao Ma 				    struct ocfs2_alloc_context *meta_ac,
30546f70fa51STao Ma 				    struct ocfs2_cached_dealloc_ctxt *dealloc)
30556f70fa51STao Ma {
30566f70fa51STao Ma 	int ret, index;
30576f70fa51STao Ma 	struct ocfs2_extent_rec replace_rec;
30586f70fa51STao Ma 	struct ocfs2_path *path = NULL;
30596f70fa51STao Ma 	struct ocfs2_extent_list *el;
30606f70fa51STao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
30616f70fa51STao Ma 	u64 ino = ocfs2_metadata_cache_owner(et->et_ci);
30626f70fa51STao Ma 
30636f70fa51STao Ma 	mlog(0, "inode %llu cpos %u, len %u, p_cluster %u, ext_flags %u\n",
30646f70fa51STao Ma 	     (unsigned long long)ino, cpos, len, p_cluster, ext_flags);
30656f70fa51STao Ma 
30666f70fa51STao Ma 	memset(&replace_rec, 0, sizeof(replace_rec));
30676f70fa51STao Ma 	replace_rec.e_cpos = cpu_to_le32(cpos);
30686f70fa51STao Ma 	replace_rec.e_leaf_clusters = cpu_to_le16(len);
30696f70fa51STao Ma 	replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(sb,
30706f70fa51STao Ma 								   p_cluster));
30716f70fa51STao Ma 	replace_rec.e_flags = ext_flags;
30726f70fa51STao Ma 	replace_rec.e_flags &= ~OCFS2_EXT_REFCOUNTED;
30736f70fa51STao Ma 
30746f70fa51STao Ma 	path = ocfs2_new_path_from_et(et);
30756f70fa51STao Ma 	if (!path) {
30766f70fa51STao Ma 		ret = -ENOMEM;
30776f70fa51STao Ma 		mlog_errno(ret);
30786f70fa51STao Ma 		goto out;
30796f70fa51STao Ma 	}
30806f70fa51STao Ma 
30816f70fa51STao Ma 	ret = ocfs2_find_path(et->et_ci, path, cpos);
30826f70fa51STao Ma 	if (ret) {
30836f70fa51STao Ma 		mlog_errno(ret);
30846f70fa51STao Ma 		goto out;
30856f70fa51STao Ma 	}
30866f70fa51STao Ma 
30876f70fa51STao Ma 	el = path_leaf_el(path);
30886f70fa51STao Ma 
30896f70fa51STao Ma 	index = ocfs2_search_extent_list(el, cpos);
30906f70fa51STao Ma 	if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
30916f70fa51STao Ma 		ocfs2_error(sb,
30926f70fa51STao Ma 			    "Inode %llu has an extent at cpos %u which can no "
30936f70fa51STao Ma 			    "longer be found.\n",
30946f70fa51STao Ma 			    (unsigned long long)ino, cpos);
30956f70fa51STao Ma 		ret = -EROFS;
30966f70fa51STao Ma 		goto out;
30976f70fa51STao Ma 	}
30986f70fa51STao Ma 
30996f70fa51STao Ma 	ret = ocfs2_split_extent(handle, et, path, index,
31006f70fa51STao Ma 				 &replace_rec, meta_ac, dealloc);
31016f70fa51STao Ma 	if (ret)
31026f70fa51STao Ma 		mlog_errno(ret);
31036f70fa51STao Ma 
31046f70fa51STao Ma out:
31056f70fa51STao Ma 	ocfs2_free_path(path);
31066f70fa51STao Ma 	return ret;
31076f70fa51STao Ma }
31086f70fa51STao Ma 
31096f70fa51STao Ma static int ocfs2_replace_clusters(handle_t *handle,
31106f70fa51STao Ma 				  struct ocfs2_cow_context *context,
31116f70fa51STao Ma 				  u32 cpos, u32 old,
31126f70fa51STao Ma 				  u32 new, u32 len,
31136f70fa51STao Ma 				  unsigned int ext_flags)
31146f70fa51STao Ma {
31156f70fa51STao Ma 	int ret;
3116913580b4STao Ma 	struct ocfs2_caching_info *ci = context->data_et.et_ci;
31176f70fa51STao Ma 	u64 ino = ocfs2_metadata_cache_owner(ci);
31186f70fa51STao Ma 
31196f70fa51STao Ma 	mlog(0, "inode %llu, cpos %u, old %u, new %u, len %u, ext_flags %u\n",
31206f70fa51STao Ma 	     (unsigned long long)ino, cpos, old, new, len, ext_flags);
31216f70fa51STao Ma 
31226f70fa51STao Ma 	/*If the old clusters is unwritten, no need to duplicate. */
31236f70fa51STao Ma 	if (!(ext_flags & OCFS2_EXT_UNWRITTEN)) {
3124913580b4STao Ma 		ret = context->cow_duplicate_clusters(handle, context, cpos,
31256f70fa51STao Ma 						      old, new, len);
31266f70fa51STao Ma 		if (ret) {
31276f70fa51STao Ma 			mlog_errno(ret);
31286f70fa51STao Ma 			goto out;
31296f70fa51STao Ma 		}
31306f70fa51STao Ma 	}
31316f70fa51STao Ma 
3132913580b4STao Ma 	ret = ocfs2_clear_ext_refcount(handle, &context->data_et,
31336f70fa51STao Ma 				       cpos, new, len, ext_flags,
31346f70fa51STao Ma 				       context->meta_ac, &context->dealloc);
31356f70fa51STao Ma 	if (ret)
31366f70fa51STao Ma 		mlog_errno(ret);
31376f70fa51STao Ma out:
31386f70fa51STao Ma 	return ret;
31396f70fa51STao Ma }
31406f70fa51STao Ma 
31416f70fa51STao Ma static int ocfs2_cow_sync_writeback(struct super_block *sb,
31426f70fa51STao Ma 				    struct ocfs2_cow_context *context,
31436f70fa51STao Ma 				    u32 cpos, u32 num_clusters)
31446f70fa51STao Ma {
31456f70fa51STao Ma 	int ret = 0;
31466f70fa51STao Ma 	loff_t offset, end, map_end;
31476f70fa51STao Ma 	pgoff_t page_index;
31486f70fa51STao Ma 	struct page *page;
31496f70fa51STao Ma 
31506f70fa51STao Ma 	if (ocfs2_should_order_data(context->inode))
31516f70fa51STao Ma 		return 0;
31526f70fa51STao Ma 
31536f70fa51STao Ma 	offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
31546f70fa51STao Ma 	end = offset + (num_clusters << OCFS2_SB(sb)->s_clustersize_bits);
31556f70fa51STao Ma 
31566f70fa51STao Ma 	ret = filemap_fdatawrite_range(context->inode->i_mapping,
31576f70fa51STao Ma 				       offset, end - 1);
31586f70fa51STao Ma 	if (ret < 0) {
31596f70fa51STao Ma 		mlog_errno(ret);
31606f70fa51STao Ma 		return ret;
31616f70fa51STao Ma 	}
31626f70fa51STao Ma 
31636f70fa51STao Ma 	while (offset < end) {
31646f70fa51STao Ma 		page_index = offset >> PAGE_CACHE_SHIFT;
3165d622b89aSTao Ma 		map_end = ((loff_t)page_index + 1) << PAGE_CACHE_SHIFT;
31666f70fa51STao Ma 		if (map_end > end)
31676f70fa51STao Ma 			map_end = end;
31686f70fa51STao Ma 
31696f70fa51STao Ma 		page = grab_cache_page(context->inode->i_mapping, page_index);
31706f70fa51STao Ma 		BUG_ON(!page);
31716f70fa51STao Ma 
31726f70fa51STao Ma 		wait_on_page_writeback(page);
31736f70fa51STao Ma 		if (PageError(page)) {
31746f70fa51STao Ma 			ret = -EIO;
31756f70fa51STao Ma 			mlog_errno(ret);
31766f70fa51STao Ma 		} else
31776f70fa51STao Ma 			mark_page_accessed(page);
31786f70fa51STao Ma 
31796f70fa51STao Ma 		unlock_page(page);
31806f70fa51STao Ma 		page_cache_release(page);
31816f70fa51STao Ma 		page = NULL;
31826f70fa51STao Ma 		offset = map_end;
31836f70fa51STao Ma 		if (ret)
31846f70fa51STao Ma 			break;
31856f70fa51STao Ma 	}
31866f70fa51STao Ma 
31876f70fa51STao Ma 	return ret;
31886f70fa51STao Ma }
31896f70fa51STao Ma 
3190913580b4STao Ma static int ocfs2_di_get_clusters(struct ocfs2_cow_context *context,
3191913580b4STao Ma 				 u32 v_cluster, u32 *p_cluster,
3192913580b4STao Ma 				 u32 *num_clusters,
3193913580b4STao Ma 				 unsigned int *extent_flags)
3194913580b4STao Ma {
3195913580b4STao Ma 	return ocfs2_get_clusters(context->inode, v_cluster, p_cluster,
3196913580b4STao Ma 				  num_clusters, extent_flags);
3197913580b4STao Ma }
3198913580b4STao Ma 
31996f70fa51STao Ma static int ocfs2_make_clusters_writable(struct super_block *sb,
32006f70fa51STao Ma 					struct ocfs2_cow_context *context,
32016f70fa51STao Ma 					u32 cpos, u32 p_cluster,
32026f70fa51STao Ma 					u32 num_clusters, unsigned int e_flags)
32036f70fa51STao Ma {
32046ae23c55STao Ma 	int ret, delete, index, credits =  0;
32056f70fa51STao Ma 	u32 new_bit, new_len;
32066ae23c55STao Ma 	unsigned int set_len;
32076f70fa51STao Ma 	struct ocfs2_super *osb = OCFS2_SB(sb);
32086f70fa51STao Ma 	handle_t *handle;
32096ae23c55STao Ma 	struct buffer_head *ref_leaf_bh = NULL;
3210913580b4STao Ma 	struct ocfs2_caching_info *ref_ci = &context->ref_tree->rf_ci;
32116ae23c55STao Ma 	struct ocfs2_refcount_rec rec;
32126ae23c55STao Ma 
32136ae23c55STao Ma 	mlog(0, "cpos %u, p_cluster %u, num_clusters %u, e_flags %u\n",
32146ae23c55STao Ma 	     cpos, p_cluster, num_clusters, e_flags);
32156f70fa51STao Ma 
32166f70fa51STao Ma 	ret = ocfs2_lock_refcount_allocators(sb, p_cluster, num_clusters,
3217913580b4STao Ma 					     &context->data_et,
3218913580b4STao Ma 					     ref_ci,
32196f70fa51STao Ma 					     context->ref_root_bh,
32206f70fa51STao Ma 					     &context->meta_ac,
32216f70fa51STao Ma 					     &context->data_ac, &credits);
32226f70fa51STao Ma 	if (ret) {
32236f70fa51STao Ma 		mlog_errno(ret);
32246f70fa51STao Ma 		return ret;
32256f70fa51STao Ma 	}
32266f70fa51STao Ma 
3227492a8a33STao Ma 	if (context->post_refcount)
3228492a8a33STao Ma 		credits += context->post_refcount->credits;
3229492a8a33STao Ma 
3230492a8a33STao Ma 	credits += context->extra_credits;
32316f70fa51STao Ma 	handle = ocfs2_start_trans(osb, credits);
32326f70fa51STao Ma 	if (IS_ERR(handle)) {
32336f70fa51STao Ma 		ret = PTR_ERR(handle);
32346f70fa51STao Ma 		mlog_errno(ret);
32356f70fa51STao Ma 		goto out;
32366f70fa51STao Ma 	}
32376f70fa51STao Ma 
32386f70fa51STao Ma 	while (num_clusters) {
3239913580b4STao Ma 		ret = ocfs2_get_refcount_rec(ref_ci, context->ref_root_bh,
32406ae23c55STao Ma 					     p_cluster, num_clusters,
32416ae23c55STao Ma 					     &rec, &index, &ref_leaf_bh);
32426ae23c55STao Ma 		if (ret) {
32436ae23c55STao Ma 			mlog_errno(ret);
32446ae23c55STao Ma 			goto out_commit;
32456ae23c55STao Ma 		}
32466ae23c55STao Ma 
32476ae23c55STao Ma 		BUG_ON(!rec.r_refcount);
32486ae23c55STao Ma 		set_len = min((u64)p_cluster + num_clusters,
32496ae23c55STao Ma 			      le64_to_cpu(rec.r_cpos) +
32506ae23c55STao Ma 			      le32_to_cpu(rec.r_clusters)) - p_cluster;
32516ae23c55STao Ma 
32526ae23c55STao Ma 		/*
32536ae23c55STao Ma 		 * There are many different situation here.
32546ae23c55STao Ma 		 * 1. If refcount == 1, remove the flag and don't COW.
32556ae23c55STao Ma 		 * 2. If refcount > 1, allocate clusters.
32566ae23c55STao Ma 		 *    Here we may not allocate r_len once at a time, so continue
32576ae23c55STao Ma 		 *    until we reach num_clusters.
32586ae23c55STao Ma 		 */
32596ae23c55STao Ma 		if (le32_to_cpu(rec.r_refcount) == 1) {
32606ae23c55STao Ma 			delete = 0;
3261913580b4STao Ma 			ret = ocfs2_clear_ext_refcount(handle,
3262913580b4STao Ma 						       &context->data_et,
32636ae23c55STao Ma 						       cpos, p_cluster,
32646ae23c55STao Ma 						       set_len, e_flags,
32656ae23c55STao Ma 						       context->meta_ac,
32666ae23c55STao Ma 						       &context->dealloc);
32676ae23c55STao Ma 			if (ret) {
32686ae23c55STao Ma 				mlog_errno(ret);
32696ae23c55STao Ma 				goto out_commit;
32706ae23c55STao Ma 			}
32716ae23c55STao Ma 		} else {
32726ae23c55STao Ma 			delete = 1;
32736ae23c55STao Ma 
32746ae23c55STao Ma 			ret = __ocfs2_claim_clusters(osb, handle,
32756ae23c55STao Ma 						     context->data_ac,
32766ae23c55STao Ma 						     1, set_len,
32776f70fa51STao Ma 						     &new_bit, &new_len);
32786f70fa51STao Ma 			if (ret) {
32796f70fa51STao Ma 				mlog_errno(ret);
32806f70fa51STao Ma 				goto out_commit;
32816f70fa51STao Ma 			}
32826f70fa51STao Ma 
32836f70fa51STao Ma 			ret = ocfs2_replace_clusters(handle, context,
32846f70fa51STao Ma 						     cpos, p_cluster, new_bit,
32856f70fa51STao Ma 						     new_len, e_flags);
32866f70fa51STao Ma 			if (ret) {
32876f70fa51STao Ma 				mlog_errno(ret);
32886f70fa51STao Ma 				goto out_commit;
32896f70fa51STao Ma 			}
32906ae23c55STao Ma 			set_len = new_len;
32916f70fa51STao Ma 		}
32926f70fa51STao Ma 
3293913580b4STao Ma 		ret = __ocfs2_decrease_refcount(handle, ref_ci,
32946f70fa51STao Ma 						context->ref_root_bh,
32956ae23c55STao Ma 						p_cluster, set_len,
32966f70fa51STao Ma 						context->meta_ac,
32976ae23c55STao Ma 						&context->dealloc, delete);
32986f70fa51STao Ma 		if (ret) {
32996f70fa51STao Ma 			mlog_errno(ret);
33006f70fa51STao Ma 			goto out_commit;
33016f70fa51STao Ma 		}
33026f70fa51STao Ma 
33036ae23c55STao Ma 		cpos += set_len;
33046ae23c55STao Ma 		p_cluster += set_len;
33056ae23c55STao Ma 		num_clusters -= set_len;
33066ae23c55STao Ma 		brelse(ref_leaf_bh);
33076ae23c55STao Ma 		ref_leaf_bh = NULL;
33086ae23c55STao Ma 	}
33096ae23c55STao Ma 
3310492a8a33STao Ma 	/* handle any post_cow action. */
3311492a8a33STao Ma 	if (context->post_refcount && context->post_refcount->func) {
3312492a8a33STao Ma 		ret = context->post_refcount->func(context->inode, handle,
3313492a8a33STao Ma 						context->post_refcount->para);
3314492a8a33STao Ma 		if (ret) {
3315492a8a33STao Ma 			mlog_errno(ret);
3316492a8a33STao Ma 			goto out_commit;
3317492a8a33STao Ma 		}
3318492a8a33STao Ma 	}
3319492a8a33STao Ma 
33206f70fa51STao Ma 	/*
33216f70fa51STao Ma 	 * Here we should write the new page out first if we are
33226f70fa51STao Ma 	 * in write-back mode.
33236f70fa51STao Ma 	 */
3324492a8a33STao Ma 	if (context->get_clusters == ocfs2_di_get_clusters) {
33256f70fa51STao Ma 		ret = ocfs2_cow_sync_writeback(sb, context, cpos, num_clusters);
33266f70fa51STao Ma 		if (ret)
33276f70fa51STao Ma 			mlog_errno(ret);
3328492a8a33STao Ma 	}
33296f70fa51STao Ma 
33306f70fa51STao Ma out_commit:
33316f70fa51STao Ma 	ocfs2_commit_trans(osb, handle);
33326f70fa51STao Ma 
33336f70fa51STao Ma out:
33346f70fa51STao Ma 	if (context->data_ac) {
33356f70fa51STao Ma 		ocfs2_free_alloc_context(context->data_ac);
33366f70fa51STao Ma 		context->data_ac = NULL;
33376f70fa51STao Ma 	}
33386f70fa51STao Ma 	if (context->meta_ac) {
33396f70fa51STao Ma 		ocfs2_free_alloc_context(context->meta_ac);
33406f70fa51STao Ma 		context->meta_ac = NULL;
33416f70fa51STao Ma 	}
33426ae23c55STao Ma 	brelse(ref_leaf_bh);
33436f70fa51STao Ma 
33446f70fa51STao Ma 	return ret;
33456f70fa51STao Ma }
33466f70fa51STao Ma 
3347913580b4STao Ma static int ocfs2_replace_cow(struct ocfs2_cow_context *context)
33486f70fa51STao Ma {
33496f70fa51STao Ma 	int ret = 0;
3350913580b4STao Ma 	struct inode *inode = context->inode;
3351913580b4STao Ma 	u32 cow_start = context->cow_start, cow_len = context->cow_len;
3352913580b4STao Ma 	u32 p_cluster, num_clusters;
33536f70fa51STao Ma 	unsigned int ext_flags;
33546f70fa51STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
33556f70fa51STao Ma 
33566f70fa51STao Ma 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
33576f70fa51STao Ma 		ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
33586f70fa51STao Ma 			    "tree, but the feature bit is not set in the "
33596f70fa51STao Ma 			    "super block.", inode->i_ino);
33606f70fa51STao Ma 		return -EROFS;
33616f70fa51STao Ma 	}
33626f70fa51STao Ma 
33636f70fa51STao Ma 	ocfs2_init_dealloc_ctxt(&context->dealloc);
33646f70fa51STao Ma 
33656f70fa51STao Ma 	while (cow_len) {
3366913580b4STao Ma 		ret = context->get_clusters(context, cow_start, &p_cluster,
33676f70fa51STao Ma 					    &num_clusters, &ext_flags);
33686f70fa51STao Ma 		if (ret) {
33696f70fa51STao Ma 			mlog_errno(ret);
33706f70fa51STao Ma 			break;
33716f70fa51STao Ma 		}
33726f70fa51STao Ma 
33736f70fa51STao Ma 		BUG_ON(!(ext_flags & OCFS2_EXT_REFCOUNTED));
33746f70fa51STao Ma 
33756f70fa51STao Ma 		if (cow_len < num_clusters)
33766f70fa51STao Ma 			num_clusters = cow_len;
33776f70fa51STao Ma 
33786f70fa51STao Ma 		ret = ocfs2_make_clusters_writable(inode->i_sb, context,
33796f70fa51STao Ma 						   cow_start, p_cluster,
33806f70fa51STao Ma 						   num_clusters, ext_flags);
33816f70fa51STao Ma 		if (ret) {
33826f70fa51STao Ma 			mlog_errno(ret);
33836f70fa51STao Ma 			break;
33846f70fa51STao Ma 		}
33856f70fa51STao Ma 
33866f70fa51STao Ma 		cow_len -= num_clusters;
33876f70fa51STao Ma 		cow_start += num_clusters;
33886f70fa51STao Ma 	}
33896f70fa51STao Ma 
33906f70fa51STao Ma 	if (ocfs2_dealloc_has_cluster(&context->dealloc)) {
33916f70fa51STao Ma 		ocfs2_schedule_truncate_log_flush(osb, 1);
33926f70fa51STao Ma 		ocfs2_run_deallocs(osb, &context->dealloc);
33936f70fa51STao Ma 	}
33946f70fa51STao Ma 
33956f70fa51STao Ma 	return ret;
33966f70fa51STao Ma }
33976f70fa51STao Ma 
33986f70fa51STao Ma /*
339937f8a2bfSTao Ma  * Starting at cpos, try to CoW write_len clusters.  Don't CoW
340037f8a2bfSTao Ma  * past max_cpos.  This will stop when it runs into a hole or an
340137f8a2bfSTao Ma  * unrefcounted extent.
34026f70fa51STao Ma  */
34036f70fa51STao Ma static int ocfs2_refcount_cow_hunk(struct inode *inode,
34046f70fa51STao Ma 				   struct buffer_head *di_bh,
340537f8a2bfSTao Ma 				   u32 cpos, u32 write_len, u32 max_cpos)
34066f70fa51STao Ma {
34076f70fa51STao Ma 	int ret;
34086f70fa51STao Ma 	u32 cow_start = 0, cow_len = 0;
34096f70fa51STao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
34106f70fa51STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
34116f70fa51STao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
34126f70fa51STao Ma 	struct buffer_head *ref_root_bh = NULL;
34136f70fa51STao Ma 	struct ocfs2_refcount_tree *ref_tree;
3414913580b4STao Ma 	struct ocfs2_cow_context *context = NULL;
34156f70fa51STao Ma 
34166f70fa51STao Ma 	BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
34176f70fa51STao Ma 
3418913580b4STao Ma 	ret = ocfs2_refcount_cal_cow_clusters(inode, &di->id2.i_list,
341937f8a2bfSTao Ma 					      cpos, write_len, max_cpos,
34206f70fa51STao Ma 					      &cow_start, &cow_len);
34216f70fa51STao Ma 	if (ret) {
34226f70fa51STao Ma 		mlog_errno(ret);
34236f70fa51STao Ma 		goto out;
34246f70fa51STao Ma 	}
342537f8a2bfSTao Ma 
34266f70fa51STao Ma 	mlog(0, "CoW inode %lu, cpos %u, write_len %u, cow_start %u, "
34276f70fa51STao Ma 	     "cow_len %u\n", inode->i_ino,
34286f70fa51STao Ma 	     cpos, write_len, cow_start, cow_len);
34296f70fa51STao Ma 
34306f70fa51STao Ma 	BUG_ON(cow_len == 0);
34316f70fa51STao Ma 
3432913580b4STao Ma 	context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
3433913580b4STao Ma 	if (!context) {
3434913580b4STao Ma 		ret = -ENOMEM;
3435913580b4STao Ma 		mlog_errno(ret);
3436913580b4STao Ma 		goto out;
3437913580b4STao Ma 	}
3438913580b4STao Ma 
34396f70fa51STao Ma 	ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
34406f70fa51STao Ma 				       1, &ref_tree, &ref_root_bh);
34416f70fa51STao Ma 	if (ret) {
34426f70fa51STao Ma 		mlog_errno(ret);
34436f70fa51STao Ma 		goto out;
34446f70fa51STao Ma 	}
34456f70fa51STao Ma 
3446913580b4STao Ma 	context->inode = inode;
3447913580b4STao Ma 	context->cow_start = cow_start;
3448913580b4STao Ma 	context->cow_len = cow_len;
3449913580b4STao Ma 	context->ref_tree = ref_tree;
3450913580b4STao Ma 	context->ref_root_bh = ref_root_bh;
3451913580b4STao Ma 	context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_page;
3452913580b4STao Ma 	context->get_clusters = ocfs2_di_get_clusters;
3453913580b4STao Ma 
3454913580b4STao Ma 	ocfs2_init_dinode_extent_tree(&context->data_et,
3455913580b4STao Ma 				      INODE_CACHE(inode), di_bh);
3456913580b4STao Ma 
3457913580b4STao Ma 	ret = ocfs2_replace_cow(context);
34586f70fa51STao Ma 	if (ret)
34596f70fa51STao Ma 		mlog_errno(ret);
34606f70fa51STao Ma 
3461913580b4STao Ma 	/*
3462913580b4STao Ma 	 * truncate the extent map here since no matter whether we meet with
3463913580b4STao Ma 	 * any error during the action, we shouldn't trust cached extent map
3464913580b4STao Ma 	 * any more.
3465913580b4STao Ma 	 */
3466913580b4STao Ma 	ocfs2_extent_map_trunc(inode, cow_start);
3467913580b4STao Ma 
34686f70fa51STao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
34696f70fa51STao Ma 	brelse(ref_root_bh);
34706f70fa51STao Ma out:
3471913580b4STao Ma 	kfree(context);
34726f70fa51STao Ma 	return ret;
34736f70fa51STao Ma }
34746f70fa51STao Ma 
34756f70fa51STao Ma /*
34766f70fa51STao Ma  * CoW any and all clusters between cpos and cpos+write_len.
347737f8a2bfSTao Ma  * Don't CoW past max_cpos.  If this returns successfully, all
347837f8a2bfSTao Ma  * clusters between cpos and cpos+write_len are safe to modify.
34796f70fa51STao Ma  */
34806f70fa51STao Ma int ocfs2_refcount_cow(struct inode *inode,
34816f70fa51STao Ma 		       struct buffer_head *di_bh,
348237f8a2bfSTao Ma 		       u32 cpos, u32 write_len, u32 max_cpos)
34836f70fa51STao Ma {
34846f70fa51STao Ma 	int ret = 0;
34856f70fa51STao Ma 	u32 p_cluster, num_clusters;
34866f70fa51STao Ma 	unsigned int ext_flags;
34876f70fa51STao Ma 
34886f70fa51STao Ma 	while (write_len) {
34896f70fa51STao Ma 		ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
34906f70fa51STao Ma 					 &num_clusters, &ext_flags);
34916f70fa51STao Ma 		if (ret) {
34926f70fa51STao Ma 			mlog_errno(ret);
34936f70fa51STao Ma 			break;
34946f70fa51STao Ma 		}
34956f70fa51STao Ma 
34966f70fa51STao Ma 		if (write_len < num_clusters)
34976f70fa51STao Ma 			num_clusters = write_len;
34986f70fa51STao Ma 
34996f70fa51STao Ma 		if (ext_flags & OCFS2_EXT_REFCOUNTED) {
35006f70fa51STao Ma 			ret = ocfs2_refcount_cow_hunk(inode, di_bh, cpos,
350137f8a2bfSTao Ma 						      num_clusters, max_cpos);
35026f70fa51STao Ma 			if (ret) {
35036f70fa51STao Ma 				mlog_errno(ret);
35046f70fa51STao Ma 				break;
35056f70fa51STao Ma 			}
35066f70fa51STao Ma 		}
35076f70fa51STao Ma 
35086f70fa51STao Ma 		write_len -= num_clusters;
35096f70fa51STao Ma 		cpos += num_clusters;
35106f70fa51STao Ma 	}
35116f70fa51STao Ma 
35126f70fa51STao Ma 	return ret;
35136f70fa51STao Ma }
3514110a045aSTao Ma 
3515492a8a33STao Ma static int ocfs2_xattr_value_get_clusters(struct ocfs2_cow_context *context,
3516492a8a33STao Ma 					  u32 v_cluster, u32 *p_cluster,
3517492a8a33STao Ma 					  u32 *num_clusters,
3518492a8a33STao Ma 					  unsigned int *extent_flags)
3519492a8a33STao Ma {
3520492a8a33STao Ma 	struct inode *inode = context->inode;
3521492a8a33STao Ma 	struct ocfs2_xattr_value_root *xv = context->cow_object;
3522492a8a33STao Ma 
3523492a8a33STao Ma 	return ocfs2_xattr_get_clusters(inode, v_cluster, p_cluster,
3524492a8a33STao Ma 					num_clusters, &xv->xr_list,
3525492a8a33STao Ma 					extent_flags);
3526492a8a33STao Ma }
3527492a8a33STao Ma 
3528492a8a33STao Ma /*
3529492a8a33STao Ma  * Given a xattr value root, calculate the most meta/credits we need for
3530492a8a33STao Ma  * refcount tree change if we truncate it to 0.
3531492a8a33STao Ma  */
3532492a8a33STao Ma int ocfs2_refcounted_xattr_delete_need(struct inode *inode,
3533492a8a33STao Ma 				       struct ocfs2_caching_info *ref_ci,
3534492a8a33STao Ma 				       struct buffer_head *ref_root_bh,
3535492a8a33STao Ma 				       struct ocfs2_xattr_value_root *xv,
3536492a8a33STao Ma 				       int *meta_add, int *credits)
3537492a8a33STao Ma {
3538492a8a33STao Ma 	int ret = 0, index, ref_blocks = 0;
3539492a8a33STao Ma 	u32 p_cluster, num_clusters;
3540492a8a33STao Ma 	u32 cpos = 0, clusters = le32_to_cpu(xv->xr_clusters);
3541492a8a33STao Ma 	struct ocfs2_refcount_block *rb;
3542492a8a33STao Ma 	struct ocfs2_refcount_rec rec;
3543492a8a33STao Ma 	struct buffer_head *ref_leaf_bh = NULL;
3544492a8a33STao Ma 
3545492a8a33STao Ma 	while (cpos < clusters) {
3546492a8a33STao Ma 		ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
3547492a8a33STao Ma 					       &num_clusters, &xv->xr_list,
3548492a8a33STao Ma 					       NULL);
3549492a8a33STao Ma 		if (ret) {
3550492a8a33STao Ma 			mlog_errno(ret);
3551492a8a33STao Ma 			goto out;
3552492a8a33STao Ma 		}
3553492a8a33STao Ma 
3554492a8a33STao Ma 		cpos += num_clusters;
3555492a8a33STao Ma 
3556492a8a33STao Ma 		while (num_clusters) {
3557492a8a33STao Ma 			ret = ocfs2_get_refcount_rec(ref_ci, ref_root_bh,
3558492a8a33STao Ma 						     p_cluster, num_clusters,
3559492a8a33STao Ma 						     &rec, &index,
3560492a8a33STao Ma 						     &ref_leaf_bh);
3561492a8a33STao Ma 			if (ret) {
3562492a8a33STao Ma 				mlog_errno(ret);
3563492a8a33STao Ma 				goto out;
3564492a8a33STao Ma 			}
3565492a8a33STao Ma 
3566492a8a33STao Ma 			BUG_ON(!rec.r_refcount);
3567492a8a33STao Ma 
3568492a8a33STao Ma 			rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
3569492a8a33STao Ma 
3570492a8a33STao Ma 			/*
3571492a8a33STao Ma 			 * We really don't know whether the other clusters is in
3572492a8a33STao Ma 			 * this refcount block or not, so just take the worst
3573492a8a33STao Ma 			 * case that all the clusters are in this block and each
3574492a8a33STao Ma 			 * one will split a refcount rec, so totally we need
3575492a8a33STao Ma 			 * clusters * 2 new refcount rec.
3576492a8a33STao Ma 			 */
3577492a8a33STao Ma 			if (le64_to_cpu(rb->rf_records.rl_used) + clusters * 2 >
3578492a8a33STao Ma 			    le16_to_cpu(rb->rf_records.rl_count))
3579492a8a33STao Ma 				ref_blocks++;
3580492a8a33STao Ma 
3581492a8a33STao Ma 			*credits += 1;
3582492a8a33STao Ma 			brelse(ref_leaf_bh);
3583492a8a33STao Ma 			ref_leaf_bh = NULL;
3584492a8a33STao Ma 
3585492a8a33STao Ma 			if (num_clusters <= le32_to_cpu(rec.r_clusters))
3586492a8a33STao Ma 				break;
3587492a8a33STao Ma 			else
3588492a8a33STao Ma 				num_clusters -= le32_to_cpu(rec.r_clusters);
3589492a8a33STao Ma 			p_cluster += num_clusters;
3590492a8a33STao Ma 		}
3591492a8a33STao Ma 	}
3592492a8a33STao Ma 
3593492a8a33STao Ma 	*meta_add += ref_blocks;
3594492a8a33STao Ma 	if (!ref_blocks)
3595492a8a33STao Ma 		goto out;
3596492a8a33STao Ma 
3597492a8a33STao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
3598492a8a33STao Ma 	if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
3599492a8a33STao Ma 		*credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
3600492a8a33STao Ma 	else {
3601492a8a33STao Ma 		struct ocfs2_extent_tree et;
3602492a8a33STao Ma 
3603492a8a33STao Ma 		ocfs2_init_refcount_extent_tree(&et, ref_ci, ref_root_bh);
3604492a8a33STao Ma 		*credits += ocfs2_calc_extend_credits(inode->i_sb,
3605492a8a33STao Ma 						      et.et_root_el,
3606492a8a33STao Ma 						      ref_blocks);
3607492a8a33STao Ma 	}
3608492a8a33STao Ma 
3609492a8a33STao Ma out:
3610492a8a33STao Ma 	brelse(ref_leaf_bh);
3611492a8a33STao Ma 	return ret;
3612492a8a33STao Ma }
3613492a8a33STao Ma 
3614492a8a33STao Ma /*
3615492a8a33STao Ma  * Do CoW for xattr.
3616492a8a33STao Ma  */
3617492a8a33STao Ma int ocfs2_refcount_cow_xattr(struct inode *inode,
3618492a8a33STao Ma 			     struct ocfs2_dinode *di,
3619492a8a33STao Ma 			     struct ocfs2_xattr_value_buf *vb,
3620492a8a33STao Ma 			     struct ocfs2_refcount_tree *ref_tree,
3621492a8a33STao Ma 			     struct buffer_head *ref_root_bh,
3622492a8a33STao Ma 			     u32 cpos, u32 write_len,
3623492a8a33STao Ma 			     struct ocfs2_post_refcount *post)
3624492a8a33STao Ma {
3625492a8a33STao Ma 	int ret;
3626492a8a33STao Ma 	struct ocfs2_xattr_value_root *xv = vb->vb_xv;
3627492a8a33STao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
3628492a8a33STao Ma 	struct ocfs2_cow_context *context = NULL;
3629492a8a33STao Ma 	u32 cow_start, cow_len;
3630492a8a33STao Ma 
3631492a8a33STao Ma 	BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
3632492a8a33STao Ma 
3633492a8a33STao Ma 	ret = ocfs2_refcount_cal_cow_clusters(inode, &xv->xr_list,
3634492a8a33STao Ma 					      cpos, write_len, UINT_MAX,
3635492a8a33STao Ma 					      &cow_start, &cow_len);
3636492a8a33STao Ma 	if (ret) {
3637492a8a33STao Ma 		mlog_errno(ret);
3638492a8a33STao Ma 		goto out;
3639492a8a33STao Ma 	}
3640492a8a33STao Ma 
3641492a8a33STao Ma 	BUG_ON(cow_len == 0);
3642492a8a33STao Ma 
3643492a8a33STao Ma 	context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
3644492a8a33STao Ma 	if (!context) {
3645492a8a33STao Ma 		ret = -ENOMEM;
3646492a8a33STao Ma 		mlog_errno(ret);
3647492a8a33STao Ma 		goto out;
3648492a8a33STao Ma 	}
3649492a8a33STao Ma 
3650492a8a33STao Ma 	context->inode = inode;
3651492a8a33STao Ma 	context->cow_start = cow_start;
3652492a8a33STao Ma 	context->cow_len = cow_len;
3653492a8a33STao Ma 	context->ref_tree = ref_tree;
3654492a8a33STao Ma 	context->ref_root_bh = ref_root_bh;;
3655492a8a33STao Ma 	context->cow_object = xv;
3656492a8a33STao Ma 
3657492a8a33STao Ma 	context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_jbd;
3658492a8a33STao Ma 	/* We need the extra credits for duplicate_clusters by jbd. */
3659492a8a33STao Ma 	context->extra_credits =
3660492a8a33STao Ma 		ocfs2_clusters_to_blocks(inode->i_sb, 1) * cow_len;
3661492a8a33STao Ma 	context->get_clusters = ocfs2_xattr_value_get_clusters;
3662492a8a33STao Ma 	context->post_refcount = post;
3663492a8a33STao Ma 
3664492a8a33STao Ma 	ocfs2_init_xattr_value_extent_tree(&context->data_et,
3665492a8a33STao Ma 					   INODE_CACHE(inode), vb);
3666492a8a33STao Ma 
3667492a8a33STao Ma 	ret = ocfs2_replace_cow(context);
3668492a8a33STao Ma 	if (ret)
3669492a8a33STao Ma 		mlog_errno(ret);
3670492a8a33STao Ma 
3671492a8a33STao Ma out:
3672492a8a33STao Ma 	kfree(context);
3673492a8a33STao Ma 	return ret;
3674492a8a33STao Ma }
3675492a8a33STao Ma 
3676110a045aSTao Ma /*
3677110a045aSTao Ma  * Insert a new extent into refcount tree and mark a extent rec
3678110a045aSTao Ma  * as refcounted in the dinode tree.
3679110a045aSTao Ma  */
3680110a045aSTao Ma int ocfs2_add_refcount_flag(struct inode *inode,
3681110a045aSTao Ma 			    struct ocfs2_extent_tree *data_et,
3682110a045aSTao Ma 			    struct ocfs2_caching_info *ref_ci,
3683110a045aSTao Ma 			    struct buffer_head *ref_root_bh,
3684110a045aSTao Ma 			    u32 cpos, u32 p_cluster, u32 num_clusters,
36850129241eSTao Ma 			    struct ocfs2_cached_dealloc_ctxt *dealloc,
36860129241eSTao Ma 			    struct ocfs2_post_refcount *post)
3687110a045aSTao Ma {
3688110a045aSTao Ma 	int ret;
3689110a045aSTao Ma 	handle_t *handle;
3690110a045aSTao Ma 	int credits = 1, ref_blocks = 0;
3691110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3692110a045aSTao Ma 	struct ocfs2_alloc_context *meta_ac = NULL;
3693110a045aSTao Ma 
3694110a045aSTao Ma 	ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
3695110a045aSTao Ma 					       ref_ci, ref_root_bh,
3696110a045aSTao Ma 					       p_cluster, num_clusters,
3697110a045aSTao Ma 					       &ref_blocks, &credits);
3698110a045aSTao Ma 	if (ret) {
3699110a045aSTao Ma 		mlog_errno(ret);
3700110a045aSTao Ma 		goto out;
3701110a045aSTao Ma 	}
3702110a045aSTao Ma 
3703110a045aSTao Ma 	mlog(0, "reserve new metadata %d, credits = %d\n",
3704110a045aSTao Ma 	     ref_blocks, credits);
3705110a045aSTao Ma 
3706110a045aSTao Ma 	if (ref_blocks) {
3707110a045aSTao Ma 		ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
3708110a045aSTao Ma 							ref_blocks, &meta_ac);
3709110a045aSTao Ma 		if (ret) {
3710110a045aSTao Ma 			mlog_errno(ret);
3711110a045aSTao Ma 			goto out;
3712110a045aSTao Ma 		}
3713110a045aSTao Ma 	}
3714110a045aSTao Ma 
37150129241eSTao Ma 	if (post)
37160129241eSTao Ma 		credits += post->credits;
37170129241eSTao Ma 
3718110a045aSTao Ma 	handle = ocfs2_start_trans(osb, credits);
3719110a045aSTao Ma 	if (IS_ERR(handle)) {
3720110a045aSTao Ma 		ret = PTR_ERR(handle);
3721110a045aSTao Ma 		mlog_errno(ret);
3722110a045aSTao Ma 		goto out;
3723110a045aSTao Ma 	}
3724110a045aSTao Ma 
3725110a045aSTao Ma 	ret = ocfs2_mark_extent_refcounted(inode, data_et, handle,
3726110a045aSTao Ma 					   cpos, num_clusters, p_cluster,
3727110a045aSTao Ma 					   meta_ac, dealloc);
3728110a045aSTao Ma 	if (ret) {
3729110a045aSTao Ma 		mlog_errno(ret);
3730110a045aSTao Ma 		goto out_commit;
3731110a045aSTao Ma 	}
3732110a045aSTao Ma 
37337540c1a7STao Ma 	ret = __ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
37347540c1a7STao Ma 					p_cluster, num_clusters, 0,
3735110a045aSTao Ma 					meta_ac, dealloc);
37360129241eSTao Ma 	if (ret) {
37370129241eSTao Ma 		mlog_errno(ret);
37380129241eSTao Ma 		goto out_commit;
37390129241eSTao Ma 	}
37400129241eSTao Ma 
37410129241eSTao Ma 	if (post && post->func) {
37420129241eSTao Ma 		ret = post->func(inode, handle, post->para);
3743110a045aSTao Ma 		if (ret)
3744110a045aSTao Ma 			mlog_errno(ret);
37450129241eSTao Ma 	}
3746110a045aSTao Ma 
3747110a045aSTao Ma out_commit:
3748110a045aSTao Ma 	ocfs2_commit_trans(osb, handle);
3749110a045aSTao Ma out:
3750110a045aSTao Ma 	if (meta_ac)
3751110a045aSTao Ma 		ocfs2_free_alloc_context(meta_ac);
3752110a045aSTao Ma 	return ret;
3753110a045aSTao Ma }
3754110a045aSTao Ma 
3755a9063ab9STao Ma static int ocfs2_change_ctime(struct inode *inode,
3756110a045aSTao Ma 			      struct buffer_head *di_bh)
3757110a045aSTao Ma {
3758110a045aSTao Ma 	int ret;
3759a9063ab9STao Ma 	handle_t *handle;
3760a9063ab9STao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3761a9063ab9STao Ma 
3762a9063ab9STao Ma 	handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
3763a9063ab9STao Ma 				   OCFS2_INODE_UPDATE_CREDITS);
3764a9063ab9STao Ma 	if (IS_ERR(handle)) {
3765a9063ab9STao Ma 		ret = PTR_ERR(handle);
3766a9063ab9STao Ma 		mlog_errno(ret);
3767a9063ab9STao Ma 		goto out;
3768a9063ab9STao Ma 	}
3769a9063ab9STao Ma 
3770a9063ab9STao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
3771a9063ab9STao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
3772a9063ab9STao Ma 	if (ret) {
3773a9063ab9STao Ma 		mlog_errno(ret);
3774a9063ab9STao Ma 		goto out_commit;
3775a9063ab9STao Ma 	}
3776a9063ab9STao Ma 
3777a9063ab9STao Ma 	inode->i_ctime = CURRENT_TIME;
3778a9063ab9STao Ma 	di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3779a9063ab9STao Ma 	di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3780a9063ab9STao Ma 
3781a9063ab9STao Ma 	ocfs2_journal_dirty(handle, di_bh);
3782a9063ab9STao Ma 
3783a9063ab9STao Ma out_commit:
3784a9063ab9STao Ma 	ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3785a9063ab9STao Ma out:
3786a9063ab9STao Ma 	return ret;
3787a9063ab9STao Ma }
3788a9063ab9STao Ma 
3789a9063ab9STao Ma static int ocfs2_attach_refcount_tree(struct inode *inode,
3790a9063ab9STao Ma 				      struct buffer_head *di_bh)
3791a9063ab9STao Ma {
3792a9063ab9STao Ma 	int ret, data_changed = 0;
3793110a045aSTao Ma 	struct buffer_head *ref_root_bh = NULL;
3794110a045aSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
3795110a045aSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3796110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3797110a045aSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
3798110a045aSTao Ma 	unsigned int ext_flags;
3799110a045aSTao Ma 	loff_t size;
3800110a045aSTao Ma 	u32 cpos, num_clusters, clusters, p_cluster;
3801110a045aSTao Ma 	struct ocfs2_cached_dealloc_ctxt dealloc;
3802110a045aSTao Ma 	struct ocfs2_extent_tree di_et;
3803110a045aSTao Ma 
3804110a045aSTao Ma 	ocfs2_init_dealloc_ctxt(&dealloc);
3805110a045aSTao Ma 
3806110a045aSTao Ma 	if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL)) {
3807110a045aSTao Ma 		ret = ocfs2_create_refcount_tree(inode, di_bh);
3808110a045aSTao Ma 		if (ret) {
3809110a045aSTao Ma 			mlog_errno(ret);
3810110a045aSTao Ma 			goto out;
3811110a045aSTao Ma 		}
3812110a045aSTao Ma 	}
3813110a045aSTao Ma 
3814110a045aSTao Ma 	BUG_ON(!di->i_refcount_loc);
3815110a045aSTao Ma 	ret = ocfs2_lock_refcount_tree(osb,
3816110a045aSTao Ma 				       le64_to_cpu(di->i_refcount_loc), 1,
3817110a045aSTao Ma 				       &ref_tree, &ref_root_bh);
3818110a045aSTao Ma 	if (ret) {
3819110a045aSTao Ma 		mlog_errno(ret);
3820110a045aSTao Ma 		goto out;
3821110a045aSTao Ma 	}
3822110a045aSTao Ma 
38232f48d593STao Ma 	if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
38242f48d593STao Ma 		goto attach_xattr;
38252f48d593STao Ma 
3826110a045aSTao Ma 	ocfs2_init_dinode_extent_tree(&di_et, INODE_CACHE(inode), di_bh);
3827110a045aSTao Ma 
3828110a045aSTao Ma 	size = i_size_read(inode);
3829110a045aSTao Ma 	clusters = ocfs2_clusters_for_bytes(inode->i_sb, size);
3830110a045aSTao Ma 
3831110a045aSTao Ma 	cpos = 0;
3832110a045aSTao Ma 	while (cpos < clusters) {
3833110a045aSTao Ma 		ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
3834110a045aSTao Ma 					 &num_clusters, &ext_flags);
3835110a045aSTao Ma 
3836110a045aSTao Ma 		if (p_cluster && !(ext_flags & OCFS2_EXT_REFCOUNTED)) {
3837110a045aSTao Ma 			ret = ocfs2_add_refcount_flag(inode, &di_et,
3838110a045aSTao Ma 						      &ref_tree->rf_ci,
3839110a045aSTao Ma 						      ref_root_bh, cpos,
3840110a045aSTao Ma 						      p_cluster, num_clusters,
38410129241eSTao Ma 						      &dealloc, NULL);
3842110a045aSTao Ma 			if (ret) {
3843110a045aSTao Ma 				mlog_errno(ret);
3844a9063ab9STao Ma 				goto unlock;
3845110a045aSTao Ma 			}
3846a9063ab9STao Ma 
3847a9063ab9STao Ma 			data_changed = 1;
3848110a045aSTao Ma 		}
3849110a045aSTao Ma 		cpos += num_clusters;
3850110a045aSTao Ma 	}
3851110a045aSTao Ma 
38522f48d593STao Ma attach_xattr:
38530129241eSTao Ma 	if (oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) {
38540129241eSTao Ma 		ret = ocfs2_xattr_attach_refcount_tree(inode, di_bh,
38550129241eSTao Ma 						       &ref_tree->rf_ci,
38560129241eSTao Ma 						       ref_root_bh,
38570129241eSTao Ma 						       &dealloc);
38580129241eSTao Ma 		if (ret) {
38590129241eSTao Ma 			mlog_errno(ret);
38600129241eSTao Ma 			goto unlock;
38610129241eSTao Ma 		}
38620129241eSTao Ma 	}
38630129241eSTao Ma 
3864a9063ab9STao Ma 	if (data_changed) {
3865a9063ab9STao Ma 		ret = ocfs2_change_ctime(inode, di_bh);
3866a9063ab9STao Ma 		if (ret)
3867a9063ab9STao Ma 			mlog_errno(ret);
3868a9063ab9STao Ma 	}
3869a9063ab9STao Ma 
3870a9063ab9STao Ma unlock:
3871110a045aSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3872110a045aSTao Ma 	brelse(ref_root_bh);
3873110a045aSTao Ma 
3874110a045aSTao Ma 	if (!ret && ocfs2_dealloc_has_cluster(&dealloc)) {
3875110a045aSTao Ma 		ocfs2_schedule_truncate_log_flush(osb, 1);
3876110a045aSTao Ma 		ocfs2_run_deallocs(osb, &dealloc);
3877110a045aSTao Ma 	}
3878110a045aSTao Ma out:
3879110a045aSTao Ma 	/*
3880110a045aSTao Ma 	 * Empty the extent map so that we may get the right extent
3881110a045aSTao Ma 	 * record from the disk.
3882110a045aSTao Ma 	 */
3883110a045aSTao Ma 	ocfs2_extent_map_trunc(inode, 0);
3884110a045aSTao Ma 
3885110a045aSTao Ma 	return ret;
3886110a045aSTao Ma }
3887110a045aSTao Ma 
3888110a045aSTao Ma static int ocfs2_add_refcounted_extent(struct inode *inode,
3889110a045aSTao Ma 				   struct ocfs2_extent_tree *et,
3890110a045aSTao Ma 				   struct ocfs2_caching_info *ref_ci,
3891110a045aSTao Ma 				   struct buffer_head *ref_root_bh,
3892110a045aSTao Ma 				   u32 cpos, u32 p_cluster, u32 num_clusters,
3893110a045aSTao Ma 				   unsigned int ext_flags,
3894110a045aSTao Ma 				   struct ocfs2_cached_dealloc_ctxt *dealloc)
3895110a045aSTao Ma {
3896110a045aSTao Ma 	int ret;
3897110a045aSTao Ma 	handle_t *handle;
3898110a045aSTao Ma 	int credits = 0;
3899110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3900110a045aSTao Ma 	struct ocfs2_alloc_context *meta_ac = NULL;
3901110a045aSTao Ma 
3902110a045aSTao Ma 	ret = ocfs2_lock_refcount_allocators(inode->i_sb,
3903110a045aSTao Ma 					     p_cluster, num_clusters,
3904110a045aSTao Ma 					     et, ref_ci,
3905110a045aSTao Ma 					     ref_root_bh, &meta_ac,
3906110a045aSTao Ma 					     NULL, &credits);
3907110a045aSTao Ma 	if (ret) {
3908110a045aSTao Ma 		mlog_errno(ret);
3909110a045aSTao Ma 		goto out;
3910110a045aSTao Ma 	}
3911110a045aSTao Ma 
3912110a045aSTao Ma 	handle = ocfs2_start_trans(osb, credits);
3913110a045aSTao Ma 	if (IS_ERR(handle)) {
3914110a045aSTao Ma 		ret = PTR_ERR(handle);
3915110a045aSTao Ma 		mlog_errno(ret);
3916110a045aSTao Ma 		goto out;
3917110a045aSTao Ma 	}
3918110a045aSTao Ma 
3919110a045aSTao Ma 	ret = ocfs2_insert_extent(handle, et, cpos,
392012d4cec9STao Ma 			ocfs2_clusters_to_blocks(inode->i_sb, p_cluster),
3921110a045aSTao Ma 			num_clusters, ext_flags, meta_ac);
3922110a045aSTao Ma 	if (ret) {
3923110a045aSTao Ma 		mlog_errno(ret);
3924110a045aSTao Ma 		goto out_commit;
3925110a045aSTao Ma 	}
3926110a045aSTao Ma 
39272999d12fSTao Ma 	ret = ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
3928110a045aSTao Ma 				      p_cluster, num_clusters,
3929110a045aSTao Ma 				      meta_ac, dealloc);
3930110a045aSTao Ma 	if (ret)
3931110a045aSTao Ma 		mlog_errno(ret);
3932110a045aSTao Ma 
3933110a045aSTao Ma out_commit:
3934110a045aSTao Ma 	ocfs2_commit_trans(osb, handle);
3935110a045aSTao Ma out:
3936110a045aSTao Ma 	if (meta_ac)
3937110a045aSTao Ma 		ocfs2_free_alloc_context(meta_ac);
3938110a045aSTao Ma 	return ret;
3939110a045aSTao Ma }
3940110a045aSTao Ma 
39412f48d593STao Ma static int ocfs2_duplicate_inline_data(struct inode *s_inode,
39422f48d593STao Ma 				       struct buffer_head *s_bh,
39432f48d593STao Ma 				       struct inode *t_inode,
39442f48d593STao Ma 				       struct buffer_head *t_bh)
39452f48d593STao Ma {
39462f48d593STao Ma 	int ret;
39472f48d593STao Ma 	handle_t *handle;
39482f48d593STao Ma 	struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb);
39492f48d593STao Ma 	struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data;
39502f48d593STao Ma 	struct ocfs2_dinode *t_di = (struct ocfs2_dinode *)t_bh->b_data;
39512f48d593STao Ma 
39522f48d593STao Ma 	BUG_ON(!(OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL));
39532f48d593STao Ma 
39542f48d593STao Ma 	handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
39552f48d593STao Ma 	if (IS_ERR(handle)) {
39562f48d593STao Ma 		ret = PTR_ERR(handle);
39572f48d593STao Ma 		mlog_errno(ret);
39582f48d593STao Ma 		goto out;
39592f48d593STao Ma 	}
39602f48d593STao Ma 
39612f48d593STao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh,
39622f48d593STao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
39632f48d593STao Ma 	if (ret) {
39642f48d593STao Ma 		mlog_errno(ret);
39652f48d593STao Ma 		goto out_commit;
39662f48d593STao Ma 	}
39672f48d593STao Ma 
39682f48d593STao Ma 	t_di->id2.i_data.id_count = s_di->id2.i_data.id_count;
39692f48d593STao Ma 	memcpy(t_di->id2.i_data.id_data, s_di->id2.i_data.id_data,
39702f48d593STao Ma 	       le16_to_cpu(s_di->id2.i_data.id_count));
39712f48d593STao Ma 	spin_lock(&OCFS2_I(t_inode)->ip_lock);
39722f48d593STao Ma 	OCFS2_I(t_inode)->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
39732f48d593STao Ma 	t_di->i_dyn_features = cpu_to_le16(OCFS2_I(t_inode)->ip_dyn_features);
39742f48d593STao Ma 	spin_unlock(&OCFS2_I(t_inode)->ip_lock);
39752f48d593STao Ma 
39762f48d593STao Ma 	ocfs2_journal_dirty(handle, t_bh);
39772f48d593STao Ma 
39782f48d593STao Ma out_commit:
39792f48d593STao Ma 	ocfs2_commit_trans(osb, handle);
39802f48d593STao Ma out:
39812f48d593STao Ma 	return ret;
39822f48d593STao Ma }
39832f48d593STao Ma 
3984110a045aSTao Ma static int ocfs2_duplicate_extent_list(struct inode *s_inode,
3985110a045aSTao Ma 				struct inode *t_inode,
3986110a045aSTao Ma 				struct buffer_head *t_bh,
3987110a045aSTao Ma 				struct ocfs2_caching_info *ref_ci,
3988110a045aSTao Ma 				struct buffer_head *ref_root_bh,
3989110a045aSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
3990110a045aSTao Ma {
3991110a045aSTao Ma 	int ret = 0;
3992110a045aSTao Ma 	u32 p_cluster, num_clusters, clusters, cpos;
3993110a045aSTao Ma 	loff_t size;
3994110a045aSTao Ma 	unsigned int ext_flags;
3995110a045aSTao Ma 	struct ocfs2_extent_tree et;
3996110a045aSTao Ma 
3997110a045aSTao Ma 	ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(t_inode), t_bh);
3998110a045aSTao Ma 
3999110a045aSTao Ma 	size = i_size_read(s_inode);
4000110a045aSTao Ma 	clusters = ocfs2_clusters_for_bytes(s_inode->i_sb, size);
4001110a045aSTao Ma 
4002110a045aSTao Ma 	cpos = 0;
4003110a045aSTao Ma 	while (cpos < clusters) {
4004110a045aSTao Ma 		ret = ocfs2_get_clusters(s_inode, cpos, &p_cluster,
4005110a045aSTao Ma 					 &num_clusters, &ext_flags);
4006110a045aSTao Ma 
4007110a045aSTao Ma 		if (p_cluster) {
4008110a045aSTao Ma 			ret = ocfs2_add_refcounted_extent(t_inode, &et,
4009110a045aSTao Ma 							  ref_ci, ref_root_bh,
4010110a045aSTao Ma 							  cpos, p_cluster,
4011110a045aSTao Ma 							  num_clusters,
4012110a045aSTao Ma 							  ext_flags,
4013110a045aSTao Ma 							  dealloc);
4014110a045aSTao Ma 			if (ret) {
4015110a045aSTao Ma 				mlog_errno(ret);
4016110a045aSTao Ma 				goto out;
4017110a045aSTao Ma 			}
4018110a045aSTao Ma 		}
4019110a045aSTao Ma 
4020110a045aSTao Ma 		cpos += num_clusters;
4021110a045aSTao Ma 	}
4022110a045aSTao Ma 
4023110a045aSTao Ma out:
4024110a045aSTao Ma 	return ret;
4025110a045aSTao Ma }
4026110a045aSTao Ma 
4027a9063ab9STao Ma /*
4028a9063ab9STao Ma  * change the new file's attributes to the src.
4029a9063ab9STao Ma  *
4030a9063ab9STao Ma  * reflink creates a snapshot of a file, that means the attributes
4031a9063ab9STao Ma  * must be identical except for three exceptions - nlink, ino, and ctime.
4032a9063ab9STao Ma  */
4033a9063ab9STao Ma static int ocfs2_complete_reflink(struct inode *s_inode,
4034a9063ab9STao Ma 				  struct buffer_head *s_bh,
4035a9063ab9STao Ma 				  struct inode *t_inode,
40360fe9b66cSTao Ma 				  struct buffer_head *t_bh,
40370fe9b66cSTao Ma 				  bool preserve)
4038a9063ab9STao Ma {
4039a9063ab9STao Ma 	int ret;
4040a9063ab9STao Ma 	handle_t *handle;
4041a9063ab9STao Ma 	struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data;
4042a9063ab9STao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)t_bh->b_data;
4043a9063ab9STao Ma 	loff_t size = i_size_read(s_inode);
4044a9063ab9STao Ma 
4045a9063ab9STao Ma 	handle = ocfs2_start_trans(OCFS2_SB(t_inode->i_sb),
4046a9063ab9STao Ma 				   OCFS2_INODE_UPDATE_CREDITS);
4047a9063ab9STao Ma 	if (IS_ERR(handle)) {
4048a9063ab9STao Ma 		ret = PTR_ERR(handle);
4049a9063ab9STao Ma 		mlog_errno(ret);
4050a9063ab9STao Ma 		return ret;
4051a9063ab9STao Ma 	}
4052a9063ab9STao Ma 
4053a9063ab9STao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh,
4054a9063ab9STao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
4055a9063ab9STao Ma 	if (ret) {
4056a9063ab9STao Ma 		mlog_errno(ret);
4057a9063ab9STao Ma 		goto out_commit;
4058a9063ab9STao Ma 	}
4059a9063ab9STao Ma 
4060a9063ab9STao Ma 	spin_lock(&OCFS2_I(t_inode)->ip_lock);
4061a9063ab9STao Ma 	OCFS2_I(t_inode)->ip_clusters = OCFS2_I(s_inode)->ip_clusters;
4062a9063ab9STao Ma 	OCFS2_I(t_inode)->ip_attr = OCFS2_I(s_inode)->ip_attr;
4063a9063ab9STao Ma 	OCFS2_I(t_inode)->ip_dyn_features = OCFS2_I(s_inode)->ip_dyn_features;
4064a9063ab9STao Ma 	spin_unlock(&OCFS2_I(t_inode)->ip_lock);
4065a9063ab9STao Ma 	i_size_write(t_inode, size);
40666527f8f8STao Ma 	t_inode->i_blocks = s_inode->i_blocks;
4067a9063ab9STao Ma 
4068a9063ab9STao Ma 	di->i_xattr_inline_size = s_di->i_xattr_inline_size;
4069a9063ab9STao Ma 	di->i_clusters = s_di->i_clusters;
4070a9063ab9STao Ma 	di->i_size = s_di->i_size;
4071a9063ab9STao Ma 	di->i_dyn_features = s_di->i_dyn_features;
4072a9063ab9STao Ma 	di->i_attr = s_di->i_attr;
40730fe9b66cSTao Ma 
40740fe9b66cSTao Ma 	if (preserve) {
4075a9063ab9STao Ma 		di->i_uid = s_di->i_uid;
4076a9063ab9STao Ma 		di->i_gid = s_di->i_gid;
4077a9063ab9STao Ma 		di->i_mode = s_di->i_mode;
4078a9063ab9STao Ma 
4079a9063ab9STao Ma 		/*
4080a9063ab9STao Ma 		 * update time.
40810fe9b66cSTao Ma 		 * we want mtime to appear identical to the source and
40820fe9b66cSTao Ma 		 * update ctime.
4083a9063ab9STao Ma 		 */
4084a9063ab9STao Ma 		t_inode->i_ctime = CURRENT_TIME;
4085a9063ab9STao Ma 
4086a9063ab9STao Ma 		di->i_ctime = cpu_to_le64(t_inode->i_ctime.tv_sec);
4087a9063ab9STao Ma 		di->i_ctime_nsec = cpu_to_le32(t_inode->i_ctime.tv_nsec);
4088a9063ab9STao Ma 
4089a9063ab9STao Ma 		t_inode->i_mtime = s_inode->i_mtime;
4090a9063ab9STao Ma 		di->i_mtime = s_di->i_mtime;
4091a9063ab9STao Ma 		di->i_mtime_nsec = s_di->i_mtime_nsec;
40920fe9b66cSTao Ma 	}
4093a9063ab9STao Ma 
4094a9063ab9STao Ma 	ocfs2_journal_dirty(handle, t_bh);
4095a9063ab9STao Ma 
4096a9063ab9STao Ma out_commit:
4097a9063ab9STao Ma 	ocfs2_commit_trans(OCFS2_SB(t_inode->i_sb), handle);
4098a9063ab9STao Ma 	return ret;
4099a9063ab9STao Ma }
4100a9063ab9STao Ma 
4101110a045aSTao Ma static int ocfs2_create_reflink_node(struct inode *s_inode,
4102110a045aSTao Ma 				     struct buffer_head *s_bh,
4103110a045aSTao Ma 				     struct inode *t_inode,
41040fe9b66cSTao Ma 				     struct buffer_head *t_bh,
41050fe9b66cSTao Ma 				     bool preserve)
4106110a045aSTao Ma {
4107110a045aSTao Ma 	int ret;
4108110a045aSTao Ma 	struct buffer_head *ref_root_bh = NULL;
4109110a045aSTao Ma 	struct ocfs2_cached_dealloc_ctxt dealloc;
4110110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb);
4111110a045aSTao Ma 	struct ocfs2_refcount_block *rb;
4112110a045aSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)s_bh->b_data;
4113110a045aSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
4114110a045aSTao Ma 
4115110a045aSTao Ma 	ocfs2_init_dealloc_ctxt(&dealloc);
4116110a045aSTao Ma 
4117110a045aSTao Ma 	ret = ocfs2_set_refcount_tree(t_inode, t_bh,
4118110a045aSTao Ma 				      le64_to_cpu(di->i_refcount_loc));
4119110a045aSTao Ma 	if (ret) {
4120110a045aSTao Ma 		mlog_errno(ret);
4121110a045aSTao Ma 		goto out;
4122110a045aSTao Ma 	}
4123110a045aSTao Ma 
41242f48d593STao Ma 	if (OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
41252f48d593STao Ma 		ret = ocfs2_duplicate_inline_data(s_inode, s_bh,
41262f48d593STao Ma 						  t_inode, t_bh);
41272f48d593STao Ma 		if (ret)
41282f48d593STao Ma 			mlog_errno(ret);
41292f48d593STao Ma 		goto out;
41302f48d593STao Ma 	}
41312f48d593STao Ma 
4132110a045aSTao Ma 	ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
4133110a045aSTao Ma 				       1, &ref_tree, &ref_root_bh);
4134110a045aSTao Ma 	if (ret) {
4135110a045aSTao Ma 		mlog_errno(ret);
4136110a045aSTao Ma 		goto out;
4137110a045aSTao Ma 	}
4138110a045aSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
4139110a045aSTao Ma 
4140110a045aSTao Ma 	ret = ocfs2_duplicate_extent_list(s_inode, t_inode, t_bh,
4141110a045aSTao Ma 					  &ref_tree->rf_ci, ref_root_bh,
4142110a045aSTao Ma 					  &dealloc);
4143a9063ab9STao Ma 	if (ret) {
4144a9063ab9STao Ma 		mlog_errno(ret);
4145a9063ab9STao Ma 		goto out_unlock_refcount;
4146a9063ab9STao Ma 	}
4147a9063ab9STao Ma 
4148a9063ab9STao Ma out_unlock_refcount:
4149110a045aSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
4150110a045aSTao Ma 	brelse(ref_root_bh);
4151110a045aSTao Ma out:
4152110a045aSTao Ma 	if (ocfs2_dealloc_has_cluster(&dealloc)) {
4153110a045aSTao Ma 		ocfs2_schedule_truncate_log_flush(osb, 1);
4154110a045aSTao Ma 		ocfs2_run_deallocs(osb, &dealloc);
4155110a045aSTao Ma 	}
4156110a045aSTao Ma 
4157110a045aSTao Ma 	return ret;
4158110a045aSTao Ma }
415909bf27a0STao Ma 
416009bf27a0STao Ma static int __ocfs2_reflink(struct dentry *old_dentry,
416109bf27a0STao Ma 			   struct buffer_head *old_bh,
416209bf27a0STao Ma 			   struct inode *new_inode,
416309bf27a0STao Ma 			   bool preserve)
416409bf27a0STao Ma {
416509bf27a0STao Ma 	int ret;
416609bf27a0STao Ma 	struct inode *inode = old_dentry->d_inode;
416709bf27a0STao Ma 	struct buffer_head *new_bh = NULL;
416809bf27a0STao Ma 
416909bf27a0STao Ma 	ret = filemap_fdatawrite(inode->i_mapping);
417009bf27a0STao Ma 	if (ret) {
417109bf27a0STao Ma 		mlog_errno(ret);
417209bf27a0STao Ma 		goto out;
417309bf27a0STao Ma 	}
417409bf27a0STao Ma 
417509bf27a0STao Ma 	ret = ocfs2_attach_refcount_tree(inode, old_bh);
417609bf27a0STao Ma 	if (ret) {
417709bf27a0STao Ma 		mlog_errno(ret);
417809bf27a0STao Ma 		goto out;
417909bf27a0STao Ma 	}
418009bf27a0STao Ma 
418109bf27a0STao Ma 	mutex_lock(&new_inode->i_mutex);
418209bf27a0STao Ma 	ret = ocfs2_inode_lock(new_inode, &new_bh, 1);
418309bf27a0STao Ma 	if (ret) {
418409bf27a0STao Ma 		mlog_errno(ret);
418509bf27a0STao Ma 		goto out_unlock;
418609bf27a0STao Ma 	}
418709bf27a0STao Ma 
418809bf27a0STao Ma 	ret = ocfs2_create_reflink_node(inode, old_bh,
418909bf27a0STao Ma 					new_inode, new_bh, preserve);
419009bf27a0STao Ma 	if (ret) {
419109bf27a0STao Ma 		mlog_errno(ret);
419209bf27a0STao Ma 		goto inode_unlock;
419309bf27a0STao Ma 	}
419409bf27a0STao Ma 
419509bf27a0STao Ma 	if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_XATTR_FL) {
419609bf27a0STao Ma 		ret = ocfs2_reflink_xattrs(inode, old_bh,
419709bf27a0STao Ma 					   new_inode, new_bh,
419809bf27a0STao Ma 					   preserve);
419987f4b1bbSTao Ma 		if (ret) {
420087f4b1bbSTao Ma 			mlog_errno(ret);
420187f4b1bbSTao Ma 			goto inode_unlock;
420287f4b1bbSTao Ma 		}
420387f4b1bbSTao Ma 	}
420487f4b1bbSTao Ma 
420587f4b1bbSTao Ma 	ret = ocfs2_complete_reflink(inode, old_bh,
420687f4b1bbSTao Ma 				     new_inode, new_bh, preserve);
420709bf27a0STao Ma 	if (ret)
420809bf27a0STao Ma 		mlog_errno(ret);
420987f4b1bbSTao Ma 
421009bf27a0STao Ma inode_unlock:
421109bf27a0STao Ma 	ocfs2_inode_unlock(new_inode, 1);
421209bf27a0STao Ma 	brelse(new_bh);
421309bf27a0STao Ma out_unlock:
421409bf27a0STao Ma 	mutex_unlock(&new_inode->i_mutex);
421509bf27a0STao Ma out:
421609bf27a0STao Ma 	if (!ret) {
421709bf27a0STao Ma 		ret = filemap_fdatawait(inode->i_mapping);
421809bf27a0STao Ma 		if (ret)
421909bf27a0STao Ma 			mlog_errno(ret);
422009bf27a0STao Ma 	}
422109bf27a0STao Ma 	return ret;
422209bf27a0STao Ma }
422309bf27a0STao Ma 
422409bf27a0STao Ma static int ocfs2_reflink(struct dentry *old_dentry, struct inode *dir,
422509bf27a0STao Ma 			 struct dentry *new_dentry, bool preserve)
422609bf27a0STao Ma {
422709bf27a0STao Ma 	int error;
422809bf27a0STao Ma 	struct inode *inode = old_dentry->d_inode;
422909bf27a0STao Ma 	struct buffer_head *old_bh = NULL;
423009bf27a0STao Ma 	struct inode *new_orphan_inode = NULL;
423109bf27a0STao Ma 
423209bf27a0STao Ma 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)))
423309bf27a0STao Ma 		return -EOPNOTSUPP;
423409bf27a0STao Ma 
423509bf27a0STao Ma 	error = ocfs2_create_inode_in_orphan(dir, inode->i_mode,
423609bf27a0STao Ma 					     &new_orphan_inode);
423709bf27a0STao Ma 	if (error) {
423809bf27a0STao Ma 		mlog_errno(error);
423909bf27a0STao Ma 		goto out;
424009bf27a0STao Ma 	}
424109bf27a0STao Ma 
424209bf27a0STao Ma 	error = ocfs2_inode_lock(inode, &old_bh, 1);
424309bf27a0STao Ma 	if (error) {
424409bf27a0STao Ma 		mlog_errno(error);
424509bf27a0STao Ma 		goto out;
424609bf27a0STao Ma 	}
424709bf27a0STao Ma 
424809bf27a0STao Ma 	down_write(&OCFS2_I(inode)->ip_xattr_sem);
424909bf27a0STao Ma 	down_write(&OCFS2_I(inode)->ip_alloc_sem);
425009bf27a0STao Ma 	error = __ocfs2_reflink(old_dentry, old_bh,
425109bf27a0STao Ma 				new_orphan_inode, preserve);
425209bf27a0STao Ma 	up_write(&OCFS2_I(inode)->ip_alloc_sem);
425309bf27a0STao Ma 	up_write(&OCFS2_I(inode)->ip_xattr_sem);
425409bf27a0STao Ma 
425509bf27a0STao Ma 	ocfs2_inode_unlock(inode, 1);
425609bf27a0STao Ma 	brelse(old_bh);
425709bf27a0STao Ma 
425809bf27a0STao Ma 	if (error) {
425909bf27a0STao Ma 		mlog_errno(error);
426009bf27a0STao Ma 		goto out;
426109bf27a0STao Ma 	}
426209bf27a0STao Ma 
426309bf27a0STao Ma 	/* If the security isn't preserved, we need to re-initialize them. */
426409bf27a0STao Ma 	if (!preserve) {
426509bf27a0STao Ma 		error = ocfs2_init_security_and_acl(dir, new_orphan_inode);
426609bf27a0STao Ma 		if (error)
426709bf27a0STao Ma 			mlog_errno(error);
426809bf27a0STao Ma 	}
426909bf27a0STao Ma out:
427009bf27a0STao Ma 	if (!error) {
427109bf27a0STao Ma 		error = ocfs2_mv_orphaned_inode_to_new(dir, new_orphan_inode,
427209bf27a0STao Ma 						       new_dentry);
427309bf27a0STao Ma 		if (error)
427409bf27a0STao Ma 			mlog_errno(error);
427509bf27a0STao Ma 	}
427609bf27a0STao Ma 
427709bf27a0STao Ma 	if (new_orphan_inode) {
427809bf27a0STao Ma 		/*
427909bf27a0STao Ma 		 * We need to open_unlock the inode no matter whether we
428009bf27a0STao Ma 		 * succeed or not, so that other nodes can delete it later.
428109bf27a0STao Ma 		 */
428209bf27a0STao Ma 		ocfs2_open_unlock(new_orphan_inode);
428309bf27a0STao Ma 		if (error)
428409bf27a0STao Ma 			iput(new_orphan_inode);
428509bf27a0STao Ma 	}
428609bf27a0STao Ma 
428709bf27a0STao Ma 	return error;
428809bf27a0STao Ma }
4289bd50873dSTao Ma 
4290bd50873dSTao Ma /*
4291bd50873dSTao Ma  * Below here are the bits used by OCFS2_IOC_REFLINK() to fake
4292bd50873dSTao Ma  * sys_reflink().  This will go away when vfs_reflink() exists in
4293bd50873dSTao Ma  * fs/namei.c.
4294bd50873dSTao Ma  */
4295bd50873dSTao Ma 
4296bd50873dSTao Ma /* copied from may_create in VFS. */
4297bd50873dSTao Ma static inline int ocfs2_may_create(struct inode *dir, struct dentry *child)
4298bd50873dSTao Ma {
4299bd50873dSTao Ma 	if (child->d_inode)
4300bd50873dSTao Ma 		return -EEXIST;
4301bd50873dSTao Ma 	if (IS_DEADDIR(dir))
4302bd50873dSTao Ma 		return -ENOENT;
4303bd50873dSTao Ma 	return inode_permission(dir, MAY_WRITE | MAY_EXEC);
4304bd50873dSTao Ma }
4305bd50873dSTao Ma 
4306bd50873dSTao Ma /* copied from user_path_parent. */
4307bd50873dSTao Ma static int ocfs2_user_path_parent(const char __user *path,
4308bd50873dSTao Ma 				  struct nameidata *nd, char **name)
4309bd50873dSTao Ma {
4310bd50873dSTao Ma 	char *s = getname(path);
4311bd50873dSTao Ma 	int error;
4312bd50873dSTao Ma 
4313bd50873dSTao Ma 	if (IS_ERR(s))
4314bd50873dSTao Ma 		return PTR_ERR(s);
4315bd50873dSTao Ma 
4316bd50873dSTao Ma 	error = path_lookup(s, LOOKUP_PARENT, nd);
4317bd50873dSTao Ma 	if (error)
4318bd50873dSTao Ma 		putname(s);
4319bd50873dSTao Ma 	else
4320bd50873dSTao Ma 		*name = s;
4321bd50873dSTao Ma 
4322bd50873dSTao Ma 	return error;
4323bd50873dSTao Ma }
4324bd50873dSTao Ma 
4325bd50873dSTao Ma /**
4326bd50873dSTao Ma  * ocfs2_vfs_reflink - Create a reference-counted link
4327bd50873dSTao Ma  *
4328bd50873dSTao Ma  * @old_dentry:        source dentry + inode
4329bd50873dSTao Ma  * @dir:       directory to create the target
4330bd50873dSTao Ma  * @new_dentry:        target dentry
4331bd50873dSTao Ma  * @preserve:  if true, preserve all file attributes
4332bd50873dSTao Ma  */
433312d4cec9STao Ma static int ocfs2_vfs_reflink(struct dentry *old_dentry, struct inode *dir,
4334bd50873dSTao Ma 			     struct dentry *new_dentry, bool preserve)
4335bd50873dSTao Ma {
4336bd50873dSTao Ma 	struct inode *inode = old_dentry->d_inode;
4337bd50873dSTao Ma 	int error;
4338bd50873dSTao Ma 
4339bd50873dSTao Ma 	if (!inode)
4340bd50873dSTao Ma 		return -ENOENT;
4341bd50873dSTao Ma 
4342bd50873dSTao Ma 	error = ocfs2_may_create(dir, new_dentry);
4343bd50873dSTao Ma 	if (error)
4344bd50873dSTao Ma 		return error;
4345bd50873dSTao Ma 
4346bd50873dSTao Ma 	if (dir->i_sb != inode->i_sb)
4347bd50873dSTao Ma 		return -EXDEV;
4348bd50873dSTao Ma 
4349bd50873dSTao Ma 	/*
4350bd50873dSTao Ma 	 * A reflink to an append-only or immutable file cannot be created.
4351bd50873dSTao Ma 	 */
4352bd50873dSTao Ma 	if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4353bd50873dSTao Ma 		return -EPERM;
4354bd50873dSTao Ma 
4355bd50873dSTao Ma 	/* Only regular files can be reflinked. */
4356bd50873dSTao Ma 	if (!S_ISREG(inode->i_mode))
4357bd50873dSTao Ma 		return -EPERM;
4358bd50873dSTao Ma 
4359bd50873dSTao Ma 	/*
4360bd50873dSTao Ma 	 * If the caller wants to preserve ownership, they require the
4361bd50873dSTao Ma 	 * rights to do so.
4362bd50873dSTao Ma 	 */
4363bd50873dSTao Ma 	if (preserve) {
4364bd50873dSTao Ma 		if ((current_fsuid() != inode->i_uid) && !capable(CAP_CHOWN))
4365bd50873dSTao Ma 			return -EPERM;
4366bd50873dSTao Ma 		if (!in_group_p(inode->i_gid) && !capable(CAP_CHOWN))
4367bd50873dSTao Ma 			return -EPERM;
4368bd50873dSTao Ma 	}
4369bd50873dSTao Ma 
4370bd50873dSTao Ma 	/*
4371bd50873dSTao Ma 	 * If the caller is modifying any aspect of the attributes, they
4372bd50873dSTao Ma 	 * are not creating a snapshot.  They need read permission on the
4373bd50873dSTao Ma 	 * file.
4374bd50873dSTao Ma 	 */
4375bd50873dSTao Ma 	if (!preserve) {
4376bd50873dSTao Ma 		error = inode_permission(inode, MAY_READ);
4377bd50873dSTao Ma 		if (error)
4378bd50873dSTao Ma 			return error;
4379bd50873dSTao Ma 	}
4380bd50873dSTao Ma 
4381bd50873dSTao Ma 	mutex_lock(&inode->i_mutex);
4382871a2931SChristoph Hellwig 	dquot_initialize(dir);
4383bd50873dSTao Ma 	error = ocfs2_reflink(old_dentry, dir, new_dentry, preserve);
4384bd50873dSTao Ma 	mutex_unlock(&inode->i_mutex);
4385bd50873dSTao Ma 	if (!error)
4386bd50873dSTao Ma 		fsnotify_create(dir, new_dentry);
4387bd50873dSTao Ma 	return error;
4388bd50873dSTao Ma }
4389bd50873dSTao Ma /*
4390bd50873dSTao Ma  * Most codes are copied from sys_linkat.
4391bd50873dSTao Ma  */
4392bd50873dSTao Ma int ocfs2_reflink_ioctl(struct inode *inode,
4393bd50873dSTao Ma 			const char __user *oldname,
4394bd50873dSTao Ma 			const char __user *newname,
4395bd50873dSTao Ma 			bool preserve)
4396bd50873dSTao Ma {
4397bd50873dSTao Ma 	struct dentry *new_dentry;
4398bd50873dSTao Ma 	struct nameidata nd;
4399bd50873dSTao Ma 	struct path old_path;
4400bd50873dSTao Ma 	int error;
4401bd50873dSTao Ma 	char *to = NULL;
4402bd50873dSTao Ma 
4403bd50873dSTao Ma 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)))
4404bd50873dSTao Ma 		return -EOPNOTSUPP;
4405bd50873dSTao Ma 
4406bd50873dSTao Ma 	error = user_path_at(AT_FDCWD, oldname, 0, &old_path);
4407bd50873dSTao Ma 	if (error) {
4408bd50873dSTao Ma 		mlog_errno(error);
4409bd50873dSTao Ma 		return error;
4410bd50873dSTao Ma 	}
4411bd50873dSTao Ma 
4412bd50873dSTao Ma 	error = ocfs2_user_path_parent(newname, &nd, &to);
4413bd50873dSTao Ma 	if (error) {
4414bd50873dSTao Ma 		mlog_errno(error);
4415bd50873dSTao Ma 		goto out;
4416bd50873dSTao Ma 	}
4417bd50873dSTao Ma 
4418bd50873dSTao Ma 	error = -EXDEV;
4419bd50873dSTao Ma 	if (old_path.mnt != nd.path.mnt)
4420bd50873dSTao Ma 		goto out_release;
4421bd50873dSTao Ma 	new_dentry = lookup_create(&nd, 0);
4422bd50873dSTao Ma 	error = PTR_ERR(new_dentry);
4423bd50873dSTao Ma 	if (IS_ERR(new_dentry)) {
4424bd50873dSTao Ma 		mlog_errno(error);
4425bd50873dSTao Ma 		goto out_unlock;
4426bd50873dSTao Ma 	}
4427bd50873dSTao Ma 
4428bd50873dSTao Ma 	error = mnt_want_write(nd.path.mnt);
4429bd50873dSTao Ma 	if (error) {
4430bd50873dSTao Ma 		mlog_errno(error);
4431bd50873dSTao Ma 		goto out_dput;
4432bd50873dSTao Ma 	}
4433bd50873dSTao Ma 
4434bd50873dSTao Ma 	error = ocfs2_vfs_reflink(old_path.dentry,
4435bd50873dSTao Ma 				  nd.path.dentry->d_inode,
4436bd50873dSTao Ma 				  new_dentry, preserve);
4437bd50873dSTao Ma 	mnt_drop_write(nd.path.mnt);
4438bd50873dSTao Ma out_dput:
4439bd50873dSTao Ma 	dput(new_dentry);
4440bd50873dSTao Ma out_unlock:
4441bd50873dSTao Ma 	mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
4442bd50873dSTao Ma out_release:
4443bd50873dSTao Ma 	path_put(&nd.path);
4444bd50873dSTao Ma 	putname(to);
4445bd50873dSTao Ma out:
4446bd50873dSTao Ma 	path_put(&old_path);
4447bd50873dSTao Ma 
4448bd50873dSTao Ma 	return error;
4449bd50873dSTao Ma }
4450