xref: /openbmc/linux/fs/ocfs2/refcounttree.c (revision 2999d12f)
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"
366f70fa51STao Ma 
376f70fa51STao Ma #include <linux/bio.h>
386f70fa51STao Ma #include <linux/blkdev.h>
396f70fa51STao Ma #include <linux/gfp.h>
406f70fa51STao Ma #include <linux/slab.h>
416f70fa51STao Ma #include <linux/writeback.h>
426f70fa51STao Ma #include <linux/pagevec.h>
436f70fa51STao Ma #include <linux/swap.h>
446f70fa51STao Ma 
456f70fa51STao Ma struct ocfs2_cow_context {
466f70fa51STao Ma 	struct inode *inode;
476f70fa51STao Ma 	u32 cow_start;
486f70fa51STao Ma 	u32 cow_len;
49913580b4STao Ma 	struct ocfs2_extent_tree data_et;
50913580b4STao Ma 	struct ocfs2_refcount_tree *ref_tree;
516f70fa51STao Ma 	struct buffer_head *ref_root_bh;
526f70fa51STao Ma 	struct ocfs2_alloc_context *meta_ac;
536f70fa51STao Ma 	struct ocfs2_alloc_context *data_ac;
546f70fa51STao Ma 	struct ocfs2_cached_dealloc_ctxt dealloc;
55492a8a33STao Ma 	void *cow_object;
56492a8a33STao Ma 	struct ocfs2_post_refcount *post_refcount;
57492a8a33STao Ma 	int extra_credits;
58913580b4STao Ma 	int (*get_clusters)(struct ocfs2_cow_context *context,
59913580b4STao Ma 			    u32 v_cluster, u32 *p_cluster,
60913580b4STao Ma 			    u32 *num_clusters,
61913580b4STao Ma 			    unsigned int *extent_flags);
62913580b4STao Ma 	int (*cow_duplicate_clusters)(handle_t *handle,
63913580b4STao Ma 				      struct ocfs2_cow_context *context,
64913580b4STao Ma 				      u32 cpos, u32 old_cluster,
65913580b4STao Ma 				      u32 new_cluster, u32 new_len);
666f70fa51STao Ma };
67c732eb16STao Ma 
68c732eb16STao Ma static inline struct ocfs2_refcount_tree *
69c732eb16STao Ma cache_info_to_refcount(struct ocfs2_caching_info *ci)
70c732eb16STao Ma {
71c732eb16STao Ma 	return container_of(ci, struct ocfs2_refcount_tree, rf_ci);
72c732eb16STao Ma }
73f2c870e3STao Ma 
74f2c870e3STao Ma static int ocfs2_validate_refcount_block(struct super_block *sb,
75f2c870e3STao Ma 					 struct buffer_head *bh)
76f2c870e3STao Ma {
77f2c870e3STao Ma 	int rc;
78f2c870e3STao Ma 	struct ocfs2_refcount_block *rb =
79f2c870e3STao Ma 		(struct ocfs2_refcount_block *)bh->b_data;
80f2c870e3STao Ma 
81f2c870e3STao Ma 	mlog(0, "Validating refcount block %llu\n",
82f2c870e3STao Ma 	     (unsigned long long)bh->b_blocknr);
83f2c870e3STao Ma 
84f2c870e3STao Ma 	BUG_ON(!buffer_uptodate(bh));
85f2c870e3STao Ma 
86f2c870e3STao Ma 	/*
87f2c870e3STao Ma 	 * If the ecc fails, we return the error but otherwise
88f2c870e3STao Ma 	 * leave the filesystem running.  We know any error is
89f2c870e3STao Ma 	 * local to this block.
90f2c870e3STao Ma 	 */
91f2c870e3STao Ma 	rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &rb->rf_check);
92f2c870e3STao Ma 	if (rc) {
93f2c870e3STao Ma 		mlog(ML_ERROR, "Checksum failed for refcount block %llu\n",
94f2c870e3STao Ma 		     (unsigned long long)bh->b_blocknr);
95f2c870e3STao Ma 		return rc;
96f2c870e3STao Ma 	}
97f2c870e3STao Ma 
98f2c870e3STao Ma 
99f2c870e3STao Ma 	if (!OCFS2_IS_VALID_REFCOUNT_BLOCK(rb)) {
100f2c870e3STao Ma 		ocfs2_error(sb,
101f2c870e3STao Ma 			    "Refcount block #%llu has bad signature %.*s",
102f2c870e3STao Ma 			    (unsigned long long)bh->b_blocknr, 7,
103f2c870e3STao Ma 			    rb->rf_signature);
104f2c870e3STao Ma 		return -EINVAL;
105f2c870e3STao Ma 	}
106f2c870e3STao Ma 
107f2c870e3STao Ma 	if (le64_to_cpu(rb->rf_blkno) != bh->b_blocknr) {
108f2c870e3STao Ma 		ocfs2_error(sb,
109f2c870e3STao Ma 			    "Refcount block #%llu has an invalid rf_blkno "
110f2c870e3STao Ma 			    "of %llu",
111f2c870e3STao Ma 			    (unsigned long long)bh->b_blocknr,
112f2c870e3STao Ma 			    (unsigned long long)le64_to_cpu(rb->rf_blkno));
113f2c870e3STao Ma 		return -EINVAL;
114f2c870e3STao Ma 	}
115f2c870e3STao Ma 
116f2c870e3STao Ma 	if (le32_to_cpu(rb->rf_fs_generation) != OCFS2_SB(sb)->fs_generation) {
117f2c870e3STao Ma 		ocfs2_error(sb,
118f2c870e3STao Ma 			    "Refcount block #%llu has an invalid "
119f2c870e3STao Ma 			    "rf_fs_generation of #%u",
120f2c870e3STao Ma 			    (unsigned long long)bh->b_blocknr,
121f2c870e3STao Ma 			    le32_to_cpu(rb->rf_fs_generation));
122f2c870e3STao Ma 		return -EINVAL;
123f2c870e3STao Ma 	}
124f2c870e3STao Ma 
125f2c870e3STao Ma 	return 0;
126f2c870e3STao Ma }
127f2c870e3STao Ma 
128f2c870e3STao Ma static int ocfs2_read_refcount_block(struct ocfs2_caching_info *ci,
129f2c870e3STao Ma 				     u64 rb_blkno,
130f2c870e3STao Ma 				     struct buffer_head **bh)
131f2c870e3STao Ma {
132f2c870e3STao Ma 	int rc;
133f2c870e3STao Ma 	struct buffer_head *tmp = *bh;
134f2c870e3STao Ma 
135f2c870e3STao Ma 	rc = ocfs2_read_block(ci, rb_blkno, &tmp,
136f2c870e3STao Ma 			      ocfs2_validate_refcount_block);
137f2c870e3STao Ma 
138f2c870e3STao Ma 	/* If ocfs2_read_block() got us a new bh, pass it up. */
139f2c870e3STao Ma 	if (!rc && !*bh)
140f2c870e3STao Ma 		*bh = tmp;
141f2c870e3STao Ma 
142f2c870e3STao Ma 	return rc;
143f2c870e3STao Ma }
144c732eb16STao Ma 
145c732eb16STao Ma static u64 ocfs2_refcount_cache_owner(struct ocfs2_caching_info *ci)
146c732eb16STao Ma {
147c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
148c732eb16STao Ma 
149c732eb16STao Ma 	return rf->rf_blkno;
150c732eb16STao Ma }
151c732eb16STao Ma 
152c732eb16STao Ma static struct super_block *
153c732eb16STao Ma ocfs2_refcount_cache_get_super(struct ocfs2_caching_info *ci)
154c732eb16STao Ma {
155c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
156c732eb16STao Ma 
157c732eb16STao Ma 	return rf->rf_sb;
158c732eb16STao Ma }
159c732eb16STao Ma 
160c732eb16STao Ma static void ocfs2_refcount_cache_lock(struct ocfs2_caching_info *ci)
161c732eb16STao Ma {
162c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
163c732eb16STao Ma 
164c732eb16STao Ma 	spin_lock(&rf->rf_lock);
165c732eb16STao Ma }
166c732eb16STao Ma 
167c732eb16STao Ma static void ocfs2_refcount_cache_unlock(struct ocfs2_caching_info *ci)
168c732eb16STao Ma {
169c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
170c732eb16STao Ma 
171c732eb16STao Ma 	spin_unlock(&rf->rf_lock);
172c732eb16STao Ma }
173c732eb16STao Ma 
174c732eb16STao Ma static void ocfs2_refcount_cache_io_lock(struct ocfs2_caching_info *ci)
175c732eb16STao Ma {
176c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
177c732eb16STao Ma 
178c732eb16STao Ma 	mutex_lock(&rf->rf_io_mutex);
179c732eb16STao Ma }
180c732eb16STao Ma 
181c732eb16STao Ma static void ocfs2_refcount_cache_io_unlock(struct ocfs2_caching_info *ci)
182c732eb16STao Ma {
183c732eb16STao Ma 	struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
184c732eb16STao Ma 
185c732eb16STao Ma 	mutex_unlock(&rf->rf_io_mutex);
186c732eb16STao Ma }
187c732eb16STao Ma 
188c732eb16STao Ma static const struct ocfs2_caching_operations ocfs2_refcount_caching_ops = {
189c732eb16STao Ma 	.co_owner		= ocfs2_refcount_cache_owner,
190c732eb16STao Ma 	.co_get_super		= ocfs2_refcount_cache_get_super,
191c732eb16STao Ma 	.co_cache_lock		= ocfs2_refcount_cache_lock,
192c732eb16STao Ma 	.co_cache_unlock	= ocfs2_refcount_cache_unlock,
193c732eb16STao Ma 	.co_io_lock		= ocfs2_refcount_cache_io_lock,
194c732eb16STao Ma 	.co_io_unlock		= ocfs2_refcount_cache_io_unlock,
195c732eb16STao Ma };
196374a263eSTao Ma 
197374a263eSTao Ma static struct ocfs2_refcount_tree *
198374a263eSTao Ma ocfs2_find_refcount_tree(struct ocfs2_super *osb, u64 blkno)
199374a263eSTao Ma {
200374a263eSTao Ma 	struct rb_node *n = osb->osb_rf_lock_tree.rb_node;
201374a263eSTao Ma 	struct ocfs2_refcount_tree *tree = NULL;
202374a263eSTao Ma 
203374a263eSTao Ma 	while (n) {
204374a263eSTao Ma 		tree = rb_entry(n, struct ocfs2_refcount_tree, rf_node);
205374a263eSTao Ma 
206374a263eSTao Ma 		if (blkno < tree->rf_blkno)
207374a263eSTao Ma 			n = n->rb_left;
208374a263eSTao Ma 		else if (blkno > tree->rf_blkno)
209374a263eSTao Ma 			n = n->rb_right;
210374a263eSTao Ma 		else
211374a263eSTao Ma 			return tree;
212374a263eSTao Ma 	}
213374a263eSTao Ma 
214374a263eSTao Ma 	return NULL;
215374a263eSTao Ma }
216374a263eSTao Ma 
217374a263eSTao Ma /* osb_lock is already locked. */
218374a263eSTao Ma static void ocfs2_insert_refcount_tree(struct ocfs2_super *osb,
219374a263eSTao Ma 				       struct ocfs2_refcount_tree *new)
220374a263eSTao Ma {
221374a263eSTao Ma 	u64 rf_blkno = new->rf_blkno;
222374a263eSTao Ma 	struct rb_node *parent = NULL;
223374a263eSTao Ma 	struct rb_node **p = &osb->osb_rf_lock_tree.rb_node;
224374a263eSTao Ma 	struct ocfs2_refcount_tree *tmp;
225374a263eSTao Ma 
226374a263eSTao Ma 	while (*p) {
227374a263eSTao Ma 		parent = *p;
228374a263eSTao Ma 
229374a263eSTao Ma 		tmp = rb_entry(parent, struct ocfs2_refcount_tree,
230374a263eSTao Ma 			       rf_node);
231374a263eSTao Ma 
232374a263eSTao Ma 		if (rf_blkno < tmp->rf_blkno)
233374a263eSTao Ma 			p = &(*p)->rb_left;
234374a263eSTao Ma 		else if (rf_blkno > tmp->rf_blkno)
235374a263eSTao Ma 			p = &(*p)->rb_right;
236374a263eSTao Ma 		else {
237374a263eSTao Ma 			/* This should never happen! */
238374a263eSTao Ma 			mlog(ML_ERROR, "Duplicate refcount block %llu found!\n",
239374a263eSTao Ma 			     (unsigned long long)rf_blkno);
240374a263eSTao Ma 			BUG();
241374a263eSTao Ma 		}
242374a263eSTao Ma 	}
243374a263eSTao Ma 
244374a263eSTao Ma 	rb_link_node(&new->rf_node, parent, p);
245374a263eSTao Ma 	rb_insert_color(&new->rf_node, &osb->osb_rf_lock_tree);
246374a263eSTao Ma }
247374a263eSTao Ma 
248374a263eSTao Ma static void ocfs2_free_refcount_tree(struct ocfs2_refcount_tree *tree)
249374a263eSTao Ma {
250374a263eSTao Ma 	ocfs2_metadata_cache_exit(&tree->rf_ci);
251374a263eSTao Ma 	ocfs2_simple_drop_lockres(OCFS2_SB(tree->rf_sb), &tree->rf_lockres);
252374a263eSTao Ma 	ocfs2_lock_res_free(&tree->rf_lockres);
253374a263eSTao Ma 	kfree(tree);
254374a263eSTao Ma }
255374a263eSTao Ma 
256374a263eSTao Ma static inline void
257374a263eSTao Ma ocfs2_erase_refcount_tree_from_list_no_lock(struct ocfs2_super *osb,
258374a263eSTao Ma 					struct ocfs2_refcount_tree *tree)
259374a263eSTao Ma {
260374a263eSTao Ma 	rb_erase(&tree->rf_node, &osb->osb_rf_lock_tree);
261374a263eSTao Ma 	if (osb->osb_ref_tree_lru && osb->osb_ref_tree_lru == tree)
262374a263eSTao Ma 		osb->osb_ref_tree_lru = NULL;
263374a263eSTao Ma }
264374a263eSTao Ma 
265374a263eSTao Ma static void ocfs2_erase_refcount_tree_from_list(struct ocfs2_super *osb,
266374a263eSTao Ma 					struct ocfs2_refcount_tree *tree)
267374a263eSTao Ma {
268374a263eSTao Ma 	spin_lock(&osb->osb_lock);
269374a263eSTao Ma 	ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
270374a263eSTao Ma 	spin_unlock(&osb->osb_lock);
271374a263eSTao Ma }
272374a263eSTao Ma 
273374a263eSTao Ma void ocfs2_kref_remove_refcount_tree(struct kref *kref)
274374a263eSTao Ma {
275374a263eSTao Ma 	struct ocfs2_refcount_tree *tree =
276374a263eSTao Ma 		container_of(kref, struct ocfs2_refcount_tree, rf_getcnt);
277374a263eSTao Ma 
278374a263eSTao Ma 	ocfs2_free_refcount_tree(tree);
279374a263eSTao Ma }
280374a263eSTao Ma 
281374a263eSTao Ma static inline void
282374a263eSTao Ma ocfs2_refcount_tree_get(struct ocfs2_refcount_tree *tree)
283374a263eSTao Ma {
284374a263eSTao Ma 	kref_get(&tree->rf_getcnt);
285374a263eSTao Ma }
286374a263eSTao Ma 
287374a263eSTao Ma static inline void
288374a263eSTao Ma ocfs2_refcount_tree_put(struct ocfs2_refcount_tree *tree)
289374a263eSTao Ma {
290374a263eSTao Ma 	kref_put(&tree->rf_getcnt, ocfs2_kref_remove_refcount_tree);
291374a263eSTao Ma }
292374a263eSTao Ma 
293374a263eSTao Ma static inline void ocfs2_init_refcount_tree_ci(struct ocfs2_refcount_tree *new,
294374a263eSTao Ma 					       struct super_block *sb)
295374a263eSTao Ma {
296374a263eSTao Ma 	ocfs2_metadata_cache_init(&new->rf_ci, &ocfs2_refcount_caching_ops);
297374a263eSTao Ma 	mutex_init(&new->rf_io_mutex);
298374a263eSTao Ma 	new->rf_sb = sb;
299374a263eSTao Ma 	spin_lock_init(&new->rf_lock);
300374a263eSTao Ma }
301374a263eSTao Ma 
302374a263eSTao Ma static inline void ocfs2_init_refcount_tree_lock(struct ocfs2_super *osb,
303374a263eSTao Ma 					struct ocfs2_refcount_tree *new,
304374a263eSTao Ma 					u64 rf_blkno, u32 generation)
305374a263eSTao Ma {
306374a263eSTao Ma 	init_rwsem(&new->rf_sem);
307374a263eSTao Ma 	ocfs2_refcount_lock_res_init(&new->rf_lockres, osb,
308374a263eSTao Ma 				     rf_blkno, generation);
309374a263eSTao Ma }
310374a263eSTao Ma 
3118bf396deSTao Ma static struct ocfs2_refcount_tree*
3128bf396deSTao Ma ocfs2_allocate_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno)
3138bf396deSTao Ma {
3148bf396deSTao Ma 	struct ocfs2_refcount_tree *new;
3158bf396deSTao Ma 
3168bf396deSTao Ma 	new = kzalloc(sizeof(struct ocfs2_refcount_tree), GFP_NOFS);
3178bf396deSTao Ma 	if (!new)
3188bf396deSTao Ma 		return NULL;
3198bf396deSTao Ma 
3208bf396deSTao Ma 	new->rf_blkno = rf_blkno;
3218bf396deSTao Ma 	kref_init(&new->rf_getcnt);
3228bf396deSTao Ma 	ocfs2_init_refcount_tree_ci(new, osb->sb);
3238bf396deSTao Ma 
3248bf396deSTao Ma 	return new;
3258bf396deSTao Ma }
3268bf396deSTao Ma 
327374a263eSTao Ma static int ocfs2_get_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno,
328374a263eSTao Ma 				   struct ocfs2_refcount_tree **ret_tree)
329374a263eSTao Ma {
330374a263eSTao Ma 	int ret = 0;
331374a263eSTao Ma 	struct ocfs2_refcount_tree *tree, *new = NULL;
332374a263eSTao Ma 	struct buffer_head *ref_root_bh = NULL;
333374a263eSTao Ma 	struct ocfs2_refcount_block *ref_rb;
334374a263eSTao Ma 
335374a263eSTao Ma 	spin_lock(&osb->osb_lock);
336374a263eSTao Ma 	if (osb->osb_ref_tree_lru &&
337374a263eSTao Ma 	    osb->osb_ref_tree_lru->rf_blkno == rf_blkno)
338374a263eSTao Ma 		tree = osb->osb_ref_tree_lru;
339374a263eSTao Ma 	else
340374a263eSTao Ma 		tree = ocfs2_find_refcount_tree(osb, rf_blkno);
341374a263eSTao Ma 	if (tree)
342374a263eSTao Ma 		goto out;
343374a263eSTao Ma 
344374a263eSTao Ma 	spin_unlock(&osb->osb_lock);
345374a263eSTao Ma 
3468bf396deSTao Ma 	new = ocfs2_allocate_refcount_tree(osb, rf_blkno);
347374a263eSTao Ma 	if (!new) {
348374a263eSTao Ma 		ret = -ENOMEM;
3498bf396deSTao Ma 		mlog_errno(ret);
350374a263eSTao Ma 		return ret;
351374a263eSTao Ma 	}
352374a263eSTao Ma 	/*
353374a263eSTao Ma 	 * We need the generation to create the refcount tree lock and since
354374a263eSTao Ma 	 * it isn't changed during the tree modification, we are safe here to
355374a263eSTao Ma 	 * read without protection.
356374a263eSTao Ma 	 * We also have to purge the cache after we create the lock since the
357374a263eSTao Ma 	 * refcount block may have the stale data. It can only be trusted when
358374a263eSTao Ma 	 * we hold the refcount lock.
359374a263eSTao Ma 	 */
360374a263eSTao Ma 	ret = ocfs2_read_refcount_block(&new->rf_ci, rf_blkno, &ref_root_bh);
361374a263eSTao Ma 	if (ret) {
362374a263eSTao Ma 		mlog_errno(ret);
363374a263eSTao Ma 		ocfs2_metadata_cache_exit(&new->rf_ci);
364374a263eSTao Ma 		kfree(new);
365374a263eSTao Ma 		return ret;
366374a263eSTao Ma 	}
367374a263eSTao Ma 
368374a263eSTao Ma 	ref_rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
369374a263eSTao Ma 	new->rf_generation = le32_to_cpu(ref_rb->rf_generation);
370374a263eSTao Ma 	ocfs2_init_refcount_tree_lock(osb, new, rf_blkno,
371374a263eSTao Ma 				      new->rf_generation);
372374a263eSTao Ma 	ocfs2_metadata_cache_purge(&new->rf_ci);
373374a263eSTao Ma 
374374a263eSTao Ma 	spin_lock(&osb->osb_lock);
375374a263eSTao Ma 	tree = ocfs2_find_refcount_tree(osb, rf_blkno);
376374a263eSTao Ma 	if (tree)
377374a263eSTao Ma 		goto out;
378374a263eSTao Ma 
379374a263eSTao Ma 	ocfs2_insert_refcount_tree(osb, new);
380374a263eSTao Ma 
381374a263eSTao Ma 	tree = new;
382374a263eSTao Ma 	new = NULL;
383374a263eSTao Ma 
384374a263eSTao Ma out:
385374a263eSTao Ma 	*ret_tree = tree;
386374a263eSTao Ma 
387374a263eSTao Ma 	osb->osb_ref_tree_lru = tree;
388374a263eSTao Ma 
389374a263eSTao Ma 	spin_unlock(&osb->osb_lock);
390374a263eSTao Ma 
391374a263eSTao Ma 	if (new)
392374a263eSTao Ma 		ocfs2_free_refcount_tree(new);
393374a263eSTao Ma 
394374a263eSTao Ma 	brelse(ref_root_bh);
395374a263eSTao Ma 	return ret;
396374a263eSTao Ma }
397374a263eSTao Ma 
398374a263eSTao Ma static int ocfs2_get_refcount_block(struct inode *inode, u64 *ref_blkno)
399374a263eSTao Ma {
400374a263eSTao Ma 	int ret;
401374a263eSTao Ma 	struct buffer_head *di_bh = NULL;
402374a263eSTao Ma 	struct ocfs2_dinode *di;
403374a263eSTao Ma 
404374a263eSTao Ma 	ret = ocfs2_read_inode_block(inode, &di_bh);
405374a263eSTao Ma 	if (ret) {
406374a263eSTao Ma 		mlog_errno(ret);
407374a263eSTao Ma 		goto out;
408374a263eSTao Ma 	}
409374a263eSTao Ma 
410374a263eSTao Ma 	BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
411374a263eSTao Ma 
412374a263eSTao Ma 	di = (struct ocfs2_dinode *)di_bh->b_data;
413374a263eSTao Ma 	*ref_blkno = le64_to_cpu(di->i_refcount_loc);
414374a263eSTao Ma 	brelse(di_bh);
415374a263eSTao Ma out:
416374a263eSTao Ma 	return ret;
417374a263eSTao Ma }
418374a263eSTao Ma 
419374a263eSTao Ma static int __ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
420374a263eSTao Ma 				      struct ocfs2_refcount_tree *tree, int rw)
421374a263eSTao Ma {
422374a263eSTao Ma 	int ret;
423374a263eSTao Ma 
424374a263eSTao Ma 	ret = ocfs2_refcount_lock(tree, rw);
425374a263eSTao Ma 	if (ret) {
426374a263eSTao Ma 		mlog_errno(ret);
427374a263eSTao Ma 		goto out;
428374a263eSTao Ma 	}
429374a263eSTao Ma 
430374a263eSTao Ma 	if (rw)
431374a263eSTao Ma 		down_write(&tree->rf_sem);
432374a263eSTao Ma 	else
433374a263eSTao Ma 		down_read(&tree->rf_sem);
434374a263eSTao Ma 
435374a263eSTao Ma out:
436374a263eSTao Ma 	return ret;
437374a263eSTao Ma }
438374a263eSTao Ma 
439374a263eSTao Ma /*
440374a263eSTao Ma  * Lock the refcount tree pointed by ref_blkno and return the tree.
441374a263eSTao Ma  * In most case, we lock the tree and read the refcount block.
442374a263eSTao Ma  * So read it here if the caller really needs it.
443374a263eSTao Ma  *
444374a263eSTao Ma  * If the tree has been re-created by other node, it will free the
445374a263eSTao Ma  * old one and re-create it.
446374a263eSTao Ma  */
447374a263eSTao Ma int ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
448374a263eSTao Ma 			     u64 ref_blkno, int rw,
449374a263eSTao Ma 			     struct ocfs2_refcount_tree **ret_tree,
450374a263eSTao Ma 			     struct buffer_head **ref_bh)
451374a263eSTao Ma {
452374a263eSTao Ma 	int ret, delete_tree = 0;
453374a263eSTao Ma 	struct ocfs2_refcount_tree *tree = NULL;
454374a263eSTao Ma 	struct buffer_head *ref_root_bh = NULL;
455374a263eSTao Ma 	struct ocfs2_refcount_block *rb;
456374a263eSTao Ma 
457374a263eSTao Ma again:
458374a263eSTao Ma 	ret = ocfs2_get_refcount_tree(osb, ref_blkno, &tree);
459374a263eSTao Ma 	if (ret) {
460374a263eSTao Ma 		mlog_errno(ret);
461374a263eSTao Ma 		return ret;
462374a263eSTao Ma 	}
463374a263eSTao Ma 
464374a263eSTao Ma 	ocfs2_refcount_tree_get(tree);
465374a263eSTao Ma 
466374a263eSTao Ma 	ret = __ocfs2_lock_refcount_tree(osb, tree, rw);
467374a263eSTao Ma 	if (ret) {
468374a263eSTao Ma 		mlog_errno(ret);
469374a263eSTao Ma 		ocfs2_refcount_tree_put(tree);
470374a263eSTao Ma 		goto out;
471374a263eSTao Ma 	}
472374a263eSTao Ma 
473374a263eSTao Ma 	ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
474374a263eSTao Ma 					&ref_root_bh);
475374a263eSTao Ma 	if (ret) {
476374a263eSTao Ma 		mlog_errno(ret);
477374a263eSTao Ma 		ocfs2_unlock_refcount_tree(osb, tree, rw);
478374a263eSTao Ma 		ocfs2_refcount_tree_put(tree);
479374a263eSTao Ma 		goto out;
480374a263eSTao Ma 	}
481374a263eSTao Ma 
482374a263eSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
483374a263eSTao Ma 	/*
484374a263eSTao Ma 	 * If the refcount block has been freed and re-created, we may need
485374a263eSTao Ma 	 * to recreate the refcount tree also.
486374a263eSTao Ma 	 *
487374a263eSTao Ma 	 * Here we just remove the tree from the rb-tree, and the last
488374a263eSTao Ma 	 * kref holder will unlock and delete this refcount_tree.
489374a263eSTao Ma 	 * Then we goto "again" and ocfs2_get_refcount_tree will create
490374a263eSTao Ma 	 * the new refcount tree for us.
491374a263eSTao Ma 	 */
492374a263eSTao Ma 	if (tree->rf_generation != le32_to_cpu(rb->rf_generation)) {
493374a263eSTao Ma 		if (!tree->rf_removed) {
494374a263eSTao Ma 			ocfs2_erase_refcount_tree_from_list(osb, tree);
495374a263eSTao Ma 			tree->rf_removed = 1;
496374a263eSTao Ma 			delete_tree = 1;
497374a263eSTao Ma 		}
498374a263eSTao Ma 
499374a263eSTao Ma 		ocfs2_unlock_refcount_tree(osb, tree, rw);
500374a263eSTao Ma 		/*
501374a263eSTao Ma 		 * We get an extra reference when we create the refcount
502374a263eSTao Ma 		 * tree, so another put will destroy it.
503374a263eSTao Ma 		 */
504374a263eSTao Ma 		if (delete_tree)
505374a263eSTao Ma 			ocfs2_refcount_tree_put(tree);
506374a263eSTao Ma 		brelse(ref_root_bh);
507374a263eSTao Ma 		ref_root_bh = NULL;
508374a263eSTao Ma 		goto again;
509374a263eSTao Ma 	}
510374a263eSTao Ma 
511374a263eSTao Ma 	*ret_tree = tree;
512374a263eSTao Ma 	if (ref_bh) {
513374a263eSTao Ma 		*ref_bh = ref_root_bh;
514374a263eSTao Ma 		ref_root_bh = NULL;
515374a263eSTao Ma 	}
516374a263eSTao Ma out:
517374a263eSTao Ma 	brelse(ref_root_bh);
518374a263eSTao Ma 	return ret;
519374a263eSTao Ma }
520374a263eSTao Ma 
521374a263eSTao Ma int ocfs2_lock_refcount_tree_by_inode(struct inode *inode, int rw,
522374a263eSTao Ma 				      struct ocfs2_refcount_tree **ret_tree,
523374a263eSTao Ma 				      struct buffer_head **ref_bh)
524374a263eSTao Ma {
525374a263eSTao Ma 	int ret;
526374a263eSTao Ma 	u64 ref_blkno;
527374a263eSTao Ma 
528374a263eSTao Ma 	ret = ocfs2_get_refcount_block(inode, &ref_blkno);
529374a263eSTao Ma 	if (ret) {
530374a263eSTao Ma 		mlog_errno(ret);
531374a263eSTao Ma 		return ret;
532374a263eSTao Ma 	}
533374a263eSTao Ma 
534374a263eSTao Ma 	return ocfs2_lock_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno,
535374a263eSTao Ma 					rw, ret_tree, ref_bh);
536374a263eSTao Ma }
537374a263eSTao Ma 
538374a263eSTao Ma void ocfs2_unlock_refcount_tree(struct ocfs2_super *osb,
539374a263eSTao Ma 				struct ocfs2_refcount_tree *tree, int rw)
540374a263eSTao Ma {
541374a263eSTao Ma 	if (rw)
542374a263eSTao Ma 		up_write(&tree->rf_sem);
543374a263eSTao Ma 	else
544374a263eSTao Ma 		up_read(&tree->rf_sem);
545374a263eSTao Ma 
546374a263eSTao Ma 	ocfs2_refcount_unlock(tree, rw);
547374a263eSTao Ma 	ocfs2_refcount_tree_put(tree);
548374a263eSTao Ma }
549374a263eSTao Ma 
550374a263eSTao Ma void ocfs2_purge_refcount_trees(struct ocfs2_super *osb)
551374a263eSTao Ma {
552374a263eSTao Ma 	struct rb_node *node;
553374a263eSTao Ma 	struct ocfs2_refcount_tree *tree;
554374a263eSTao Ma 	struct rb_root *root = &osb->osb_rf_lock_tree;
555374a263eSTao Ma 
556374a263eSTao Ma 	while ((node = rb_last(root)) != NULL) {
557374a263eSTao Ma 		tree = rb_entry(node, struct ocfs2_refcount_tree, rf_node);
558374a263eSTao Ma 
559374a263eSTao Ma 		mlog(0, "Purge tree %llu\n",
560374a263eSTao Ma 		     (unsigned long long) tree->rf_blkno);
561374a263eSTao Ma 
562374a263eSTao Ma 		rb_erase(&tree->rf_node, root);
563374a263eSTao Ma 		ocfs2_free_refcount_tree(tree);
564374a263eSTao Ma 	}
565374a263eSTao Ma }
5668bf396deSTao Ma 
5678bf396deSTao Ma /*
5688bf396deSTao Ma  * Create a refcount tree for an inode.
5698bf396deSTao Ma  * We take for granted that the inode is already locked.
5708bf396deSTao Ma  */
5718bf396deSTao Ma static int ocfs2_create_refcount_tree(struct inode *inode,
5728bf396deSTao Ma 				      struct buffer_head *di_bh)
5738bf396deSTao Ma {
5748bf396deSTao Ma 	int ret;
5758bf396deSTao Ma 	handle_t *handle = NULL;
5768bf396deSTao Ma 	struct ocfs2_alloc_context *meta_ac = NULL;
5778bf396deSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
5788bf396deSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
5798bf396deSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
5808bf396deSTao Ma 	struct buffer_head *new_bh = NULL;
5818bf396deSTao Ma 	struct ocfs2_refcount_block *rb;
5828bf396deSTao Ma 	struct ocfs2_refcount_tree *new_tree = NULL, *tree = NULL;
5838bf396deSTao Ma 	u16 suballoc_bit_start;
5848bf396deSTao Ma 	u32 num_got;
5858bf396deSTao Ma 	u64 first_blkno;
5868bf396deSTao Ma 
5878bf396deSTao Ma 	BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL);
5888bf396deSTao Ma 
5898bf396deSTao Ma 	mlog(0, "create tree for inode %lu\n", inode->i_ino);
5908bf396deSTao Ma 
5918bf396deSTao Ma 	ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
5928bf396deSTao Ma 	if (ret) {
5938bf396deSTao Ma 		mlog_errno(ret);
5948bf396deSTao Ma 		goto out;
5958bf396deSTao Ma 	}
5968bf396deSTao Ma 
5978bf396deSTao Ma 	handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_CREATE_CREDITS);
5988bf396deSTao Ma 	if (IS_ERR(handle)) {
5998bf396deSTao Ma 		ret = PTR_ERR(handle);
6008bf396deSTao Ma 		mlog_errno(ret);
6018bf396deSTao Ma 		goto out;
6028bf396deSTao Ma 	}
6038bf396deSTao Ma 
6048bf396deSTao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
6058bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
6068bf396deSTao Ma 	if (ret) {
6078bf396deSTao Ma 		mlog_errno(ret);
6088bf396deSTao Ma 		goto out_commit;
6098bf396deSTao Ma 	}
6108bf396deSTao Ma 
6118bf396deSTao Ma 	ret = ocfs2_claim_metadata(osb, handle, meta_ac, 1,
6128bf396deSTao Ma 				   &suballoc_bit_start, &num_got,
6138bf396deSTao Ma 				   &first_blkno);
6148bf396deSTao Ma 	if (ret) {
6158bf396deSTao Ma 		mlog_errno(ret);
6168bf396deSTao Ma 		goto out_commit;
6178bf396deSTao Ma 	}
6188bf396deSTao Ma 
6198bf396deSTao Ma 	new_tree = ocfs2_allocate_refcount_tree(osb, first_blkno);
6208bf396deSTao Ma 	if (!new_tree) {
6218bf396deSTao Ma 		ret = -ENOMEM;
6228bf396deSTao Ma 		mlog_errno(ret);
6238bf396deSTao Ma 		goto out_commit;
6248bf396deSTao Ma 	}
6258bf396deSTao Ma 
6268bf396deSTao Ma 	new_bh = sb_getblk(inode->i_sb, first_blkno);
6278bf396deSTao Ma 	ocfs2_set_new_buffer_uptodate(&new_tree->rf_ci, new_bh);
6288bf396deSTao Ma 
6298bf396deSTao Ma 	ret = ocfs2_journal_access_rb(handle, &new_tree->rf_ci, new_bh,
6308bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_CREATE);
6318bf396deSTao Ma 	if (ret) {
6328bf396deSTao Ma 		mlog_errno(ret);
6338bf396deSTao Ma 		goto out_commit;
6348bf396deSTao Ma 	}
6358bf396deSTao Ma 
6368bf396deSTao Ma 	/* Initialize ocfs2_refcount_block. */
6378bf396deSTao Ma 	rb = (struct ocfs2_refcount_block *)new_bh->b_data;
6388bf396deSTao Ma 	memset(rb, 0, inode->i_sb->s_blocksize);
6398bf396deSTao Ma 	strcpy((void *)rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
6408bf396deSTao Ma 	rb->rf_suballoc_slot = cpu_to_le16(osb->slot_num);
6418bf396deSTao Ma 	rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
6428bf396deSTao Ma 	rb->rf_fs_generation = cpu_to_le32(osb->fs_generation);
6438bf396deSTao Ma 	rb->rf_blkno = cpu_to_le64(first_blkno);
6448bf396deSTao Ma 	rb->rf_count = cpu_to_le32(1);
6458bf396deSTao Ma 	rb->rf_records.rl_count =
6468bf396deSTao Ma 			cpu_to_le16(ocfs2_refcount_recs_per_rb(osb->sb));
6478bf396deSTao Ma 	spin_lock(&osb->osb_lock);
6488bf396deSTao Ma 	rb->rf_generation = osb->s_next_generation++;
6498bf396deSTao Ma 	spin_unlock(&osb->osb_lock);
6508bf396deSTao Ma 
6518bf396deSTao Ma 	ocfs2_journal_dirty(handle, new_bh);
6528bf396deSTao Ma 
6538bf396deSTao Ma 	spin_lock(&oi->ip_lock);
6548bf396deSTao Ma 	oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
6558bf396deSTao Ma 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
6568bf396deSTao Ma 	di->i_refcount_loc = cpu_to_le64(first_blkno);
6578bf396deSTao Ma 	spin_unlock(&oi->ip_lock);
6588bf396deSTao Ma 
6598bf396deSTao Ma 	mlog(0, "created tree for inode %lu, refblock %llu\n",
6608bf396deSTao Ma 	     inode->i_ino, (unsigned long long)first_blkno);
6618bf396deSTao Ma 
6628bf396deSTao Ma 	ocfs2_journal_dirty(handle, di_bh);
6638bf396deSTao Ma 
6648bf396deSTao Ma 	/*
6658bf396deSTao Ma 	 * We have to init the tree lock here since it will use
6668bf396deSTao Ma 	 * the generation number to create it.
6678bf396deSTao Ma 	 */
6688bf396deSTao Ma 	new_tree->rf_generation = le32_to_cpu(rb->rf_generation);
6698bf396deSTao Ma 	ocfs2_init_refcount_tree_lock(osb, new_tree, first_blkno,
6708bf396deSTao Ma 				      new_tree->rf_generation);
6718bf396deSTao Ma 
6728bf396deSTao Ma 	spin_lock(&osb->osb_lock);
6738bf396deSTao Ma 	tree = ocfs2_find_refcount_tree(osb, first_blkno);
6748bf396deSTao Ma 
6758bf396deSTao Ma 	/*
6768bf396deSTao Ma 	 * We've just created a new refcount tree in this block.  If
6778bf396deSTao Ma 	 * we found a refcount tree on the ocfs2_super, it must be
6788bf396deSTao Ma 	 * one we just deleted.  We free the old tree before
6798bf396deSTao Ma 	 * inserting the new tree.
6808bf396deSTao Ma 	 */
6818bf396deSTao Ma 	BUG_ON(tree && tree->rf_generation == new_tree->rf_generation);
6828bf396deSTao Ma 	if (tree)
6838bf396deSTao Ma 		ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
6848bf396deSTao Ma 	ocfs2_insert_refcount_tree(osb, new_tree);
6858bf396deSTao Ma 	spin_unlock(&osb->osb_lock);
6868bf396deSTao Ma 	new_tree = NULL;
6878bf396deSTao Ma 	if (tree)
6888bf396deSTao Ma 		ocfs2_refcount_tree_put(tree);
6898bf396deSTao Ma 
6908bf396deSTao Ma out_commit:
6918bf396deSTao Ma 	ocfs2_commit_trans(osb, handle);
6928bf396deSTao Ma 
6938bf396deSTao Ma out:
6948bf396deSTao Ma 	if (new_tree) {
6958bf396deSTao Ma 		ocfs2_metadata_cache_exit(&new_tree->rf_ci);
6968bf396deSTao Ma 		kfree(new_tree);
6978bf396deSTao Ma 	}
6988bf396deSTao Ma 
6998bf396deSTao Ma 	brelse(new_bh);
7008bf396deSTao Ma 	if (meta_ac)
7018bf396deSTao Ma 		ocfs2_free_alloc_context(meta_ac);
7028bf396deSTao Ma 
7038bf396deSTao Ma 	return ret;
7048bf396deSTao Ma }
7058bf396deSTao Ma 
7068bf396deSTao Ma static int ocfs2_set_refcount_tree(struct inode *inode,
7078bf396deSTao Ma 				   struct buffer_head *di_bh,
7088bf396deSTao Ma 				   u64 refcount_loc)
7098bf396deSTao Ma {
7108bf396deSTao Ma 	int ret;
7118bf396deSTao Ma 	handle_t *handle = NULL;
7128bf396deSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7138bf396deSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
7148bf396deSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7158bf396deSTao Ma 	struct buffer_head *ref_root_bh = NULL;
7168bf396deSTao Ma 	struct ocfs2_refcount_block *rb;
7178bf396deSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
7188bf396deSTao Ma 
7198bf396deSTao Ma 	BUG_ON(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL);
7208bf396deSTao Ma 
7218bf396deSTao Ma 	ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
7228bf396deSTao Ma 				       &ref_tree, &ref_root_bh);
7238bf396deSTao Ma 	if (ret) {
7248bf396deSTao Ma 		mlog_errno(ret);
7258bf396deSTao Ma 		return ret;
7268bf396deSTao Ma 	}
7278bf396deSTao Ma 
7288bf396deSTao Ma 	handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_SET_CREDITS);
7298bf396deSTao Ma 	if (IS_ERR(handle)) {
7308bf396deSTao Ma 		ret = PTR_ERR(handle);
7318bf396deSTao Ma 		mlog_errno(ret);
7328bf396deSTao Ma 		goto out;
7338bf396deSTao Ma 	}
7348bf396deSTao Ma 
7358bf396deSTao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
7368bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
7378bf396deSTao Ma 	if (ret) {
7388bf396deSTao Ma 		mlog_errno(ret);
7398bf396deSTao Ma 		goto out_commit;
7408bf396deSTao Ma 	}
7418bf396deSTao Ma 
7428bf396deSTao Ma 	ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, ref_root_bh,
7438bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
7448bf396deSTao Ma 	if (ret) {
7458bf396deSTao Ma 		mlog_errno(ret);
7468bf396deSTao Ma 		goto out_commit;
7478bf396deSTao Ma 	}
7488bf396deSTao Ma 
7498bf396deSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
7508bf396deSTao Ma 	le32_add_cpu(&rb->rf_count, 1);
7518bf396deSTao Ma 
7528bf396deSTao Ma 	ocfs2_journal_dirty(handle, ref_root_bh);
7538bf396deSTao Ma 
7548bf396deSTao Ma 	spin_lock(&oi->ip_lock);
7558bf396deSTao Ma 	oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
7568bf396deSTao Ma 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
7578bf396deSTao Ma 	di->i_refcount_loc = cpu_to_le64(refcount_loc);
7588bf396deSTao Ma 	spin_unlock(&oi->ip_lock);
7598bf396deSTao Ma 	ocfs2_journal_dirty(handle, di_bh);
7608bf396deSTao Ma 
7618bf396deSTao Ma out_commit:
7628bf396deSTao Ma 	ocfs2_commit_trans(osb, handle);
7638bf396deSTao Ma out:
7648bf396deSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
7658bf396deSTao Ma 	brelse(ref_root_bh);
7668bf396deSTao Ma 
7678bf396deSTao Ma 	return ret;
7688bf396deSTao Ma }
7698bf396deSTao Ma 
7708bf396deSTao Ma int ocfs2_remove_refcount_tree(struct inode *inode, struct buffer_head *di_bh)
7718bf396deSTao Ma {
7728bf396deSTao Ma 	int ret, delete_tree = 0;
7738bf396deSTao Ma 	handle_t *handle = NULL;
7748bf396deSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
7758bf396deSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
7768bf396deSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
7778bf396deSTao Ma 	struct ocfs2_refcount_block *rb;
7788bf396deSTao Ma 	struct inode *alloc_inode = NULL;
7798bf396deSTao Ma 	struct buffer_head *alloc_bh = NULL;
7808bf396deSTao Ma 	struct buffer_head *blk_bh = NULL;
7818bf396deSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
7828bf396deSTao Ma 	int credits = OCFS2_REFCOUNT_TREE_REMOVE_CREDITS;
7838bf396deSTao Ma 	u64 blk = 0, bg_blkno = 0, ref_blkno = le64_to_cpu(di->i_refcount_loc);
7848bf396deSTao Ma 	u16 bit = 0;
7858bf396deSTao Ma 
7868bf396deSTao Ma 	if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL))
7878bf396deSTao Ma 		return 0;
7888bf396deSTao Ma 
7898bf396deSTao Ma 	BUG_ON(!ref_blkno);
7908bf396deSTao Ma 	ret = ocfs2_lock_refcount_tree(osb, ref_blkno, 1, &ref_tree, &blk_bh);
7918bf396deSTao Ma 	if (ret) {
7928bf396deSTao Ma 		mlog_errno(ret);
7938bf396deSTao Ma 		return ret;
7948bf396deSTao Ma 	}
7958bf396deSTao Ma 
7968bf396deSTao Ma 	rb = (struct ocfs2_refcount_block *)blk_bh->b_data;
7978bf396deSTao Ma 
7988bf396deSTao Ma 	/*
7998bf396deSTao Ma 	 * If we are the last user, we need to free the block.
8008bf396deSTao Ma 	 * So lock the allocator ahead.
8018bf396deSTao Ma 	 */
8028bf396deSTao Ma 	if (le32_to_cpu(rb->rf_count) == 1) {
8038bf396deSTao Ma 		blk = le64_to_cpu(rb->rf_blkno);
8048bf396deSTao Ma 		bit = le16_to_cpu(rb->rf_suballoc_bit);
8058bf396deSTao Ma 		bg_blkno = ocfs2_which_suballoc_group(blk, bit);
8068bf396deSTao Ma 
8078bf396deSTao Ma 		alloc_inode = ocfs2_get_system_file_inode(osb,
8088bf396deSTao Ma 					EXTENT_ALLOC_SYSTEM_INODE,
8098bf396deSTao Ma 					le16_to_cpu(rb->rf_suballoc_slot));
8108bf396deSTao Ma 		if (!alloc_inode) {
8118bf396deSTao Ma 			ret = -ENOMEM;
8128bf396deSTao Ma 			mlog_errno(ret);
8138bf396deSTao Ma 			goto out;
8148bf396deSTao Ma 		}
8158bf396deSTao Ma 		mutex_lock(&alloc_inode->i_mutex);
8168bf396deSTao Ma 
8178bf396deSTao Ma 		ret = ocfs2_inode_lock(alloc_inode, &alloc_bh, 1);
8188bf396deSTao Ma 		if (ret) {
8198bf396deSTao Ma 			mlog_errno(ret);
8208bf396deSTao Ma 			goto out_mutex;
8218bf396deSTao Ma 		}
8228bf396deSTao Ma 
8238bf396deSTao Ma 		credits += OCFS2_SUBALLOC_FREE;
8248bf396deSTao Ma 	}
8258bf396deSTao Ma 
8268bf396deSTao Ma 	handle = ocfs2_start_trans(osb, credits);
8278bf396deSTao Ma 	if (IS_ERR(handle)) {
8288bf396deSTao Ma 		ret = PTR_ERR(handle);
8298bf396deSTao Ma 		mlog_errno(ret);
8308bf396deSTao Ma 		goto out_unlock;
8318bf396deSTao Ma 	}
8328bf396deSTao Ma 
8338bf396deSTao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
8348bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
8358bf396deSTao Ma 	if (ret) {
8368bf396deSTao Ma 		mlog_errno(ret);
8378bf396deSTao Ma 		goto out_commit;
8388bf396deSTao Ma 	}
8398bf396deSTao Ma 
8408bf396deSTao Ma 	ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, blk_bh,
8418bf396deSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
8428bf396deSTao Ma 	if (ret) {
8438bf396deSTao Ma 		mlog_errno(ret);
8448bf396deSTao Ma 		goto out_commit;
8458bf396deSTao Ma 	}
8468bf396deSTao Ma 
8478bf396deSTao Ma 	spin_lock(&oi->ip_lock);
8488bf396deSTao Ma 	oi->ip_dyn_features &= ~OCFS2_HAS_REFCOUNT_FL;
8498bf396deSTao Ma 	di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
8508bf396deSTao Ma 	di->i_refcount_loc = 0;
8518bf396deSTao Ma 	spin_unlock(&oi->ip_lock);
8528bf396deSTao Ma 	ocfs2_journal_dirty(handle, di_bh);
8538bf396deSTao Ma 
8548bf396deSTao Ma 	le32_add_cpu(&rb->rf_count , -1);
8558bf396deSTao Ma 	ocfs2_journal_dirty(handle, blk_bh);
8568bf396deSTao Ma 
8578bf396deSTao Ma 	if (!rb->rf_count) {
8588bf396deSTao Ma 		delete_tree = 1;
8598bf396deSTao Ma 		ocfs2_erase_refcount_tree_from_list(osb, ref_tree);
8608bf396deSTao Ma 		ret = ocfs2_free_suballoc_bits(handle, alloc_inode,
8618bf396deSTao Ma 					       alloc_bh, bit, bg_blkno, 1);
8628bf396deSTao Ma 		if (ret)
8638bf396deSTao Ma 			mlog_errno(ret);
8648bf396deSTao Ma 	}
8658bf396deSTao Ma 
8668bf396deSTao Ma out_commit:
8678bf396deSTao Ma 	ocfs2_commit_trans(osb, handle);
8688bf396deSTao Ma out_unlock:
8698bf396deSTao Ma 	if (alloc_inode) {
8708bf396deSTao Ma 		ocfs2_inode_unlock(alloc_inode, 1);
8718bf396deSTao Ma 		brelse(alloc_bh);
8728bf396deSTao Ma 	}
8738bf396deSTao Ma out_mutex:
8748bf396deSTao Ma 	if (alloc_inode) {
8758bf396deSTao Ma 		mutex_unlock(&alloc_inode->i_mutex);
8768bf396deSTao Ma 		iput(alloc_inode);
8778bf396deSTao Ma 	}
8788bf396deSTao Ma out:
8798bf396deSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
8808bf396deSTao Ma 	if (delete_tree)
8818bf396deSTao Ma 		ocfs2_refcount_tree_put(ref_tree);
8828bf396deSTao Ma 	brelse(blk_bh);
8838bf396deSTao Ma 
8848bf396deSTao Ma 	return ret;
8858bf396deSTao Ma }
886e73a819dSTao Ma 
887e73a819dSTao Ma static void ocfs2_find_refcount_rec_in_rl(struct ocfs2_caching_info *ci,
888e73a819dSTao Ma 					  struct buffer_head *ref_leaf_bh,
889e73a819dSTao Ma 					  u64 cpos, unsigned int len,
890e73a819dSTao Ma 					  struct ocfs2_refcount_rec *ret_rec,
891e73a819dSTao Ma 					  int *index)
892e73a819dSTao Ma {
893e73a819dSTao Ma 	int i = 0;
894e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
895e73a819dSTao Ma 		(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
896e73a819dSTao Ma 	struct ocfs2_refcount_rec *rec = NULL;
897e73a819dSTao Ma 
898e73a819dSTao Ma 	for (; i < le16_to_cpu(rb->rf_records.rl_used); i++) {
899e73a819dSTao Ma 		rec = &rb->rf_records.rl_recs[i];
900e73a819dSTao Ma 
901e73a819dSTao Ma 		if (le64_to_cpu(rec->r_cpos) +
902e73a819dSTao Ma 		    le32_to_cpu(rec->r_clusters) <= cpos)
903e73a819dSTao Ma 			continue;
904e73a819dSTao Ma 		else if (le64_to_cpu(rec->r_cpos) > cpos)
905e73a819dSTao Ma 			break;
906e73a819dSTao Ma 
907e73a819dSTao Ma 		/* ok, cpos fail in this rec. Just return. */
908e73a819dSTao Ma 		if (ret_rec)
909e73a819dSTao Ma 			*ret_rec = *rec;
910e73a819dSTao Ma 		goto out;
911e73a819dSTao Ma 	}
912e73a819dSTao Ma 
913e73a819dSTao Ma 	if (ret_rec) {
914e73a819dSTao Ma 		/* We meet with a hole here, so fake the rec. */
915e73a819dSTao Ma 		ret_rec->r_cpos = cpu_to_le64(cpos);
916e73a819dSTao Ma 		ret_rec->r_refcount = 0;
917e73a819dSTao Ma 		if (i < le16_to_cpu(rb->rf_records.rl_used) &&
918e73a819dSTao Ma 		    le64_to_cpu(rec->r_cpos) < cpos + len)
919e73a819dSTao Ma 			ret_rec->r_clusters =
920e73a819dSTao Ma 				cpu_to_le32(le64_to_cpu(rec->r_cpos) - cpos);
921e73a819dSTao Ma 		else
922e73a819dSTao Ma 			ret_rec->r_clusters = cpu_to_le32(len);
923e73a819dSTao Ma 	}
924e73a819dSTao Ma 
925e73a819dSTao Ma out:
926e73a819dSTao Ma 	*index = i;
927e73a819dSTao Ma }
928e73a819dSTao Ma 
929e73a819dSTao Ma /*
9308b2c0dbaSTao Ma  * Try to remove refcount tree. The mechanism is:
9318b2c0dbaSTao Ma  * 1) Check whether i_clusters == 0, if no, exit.
9328b2c0dbaSTao Ma  * 2) check whether we have i_xattr_loc in dinode. if yes, exit.
9338b2c0dbaSTao Ma  * 3) Check whether we have inline xattr stored outside, if yes, exit.
9348b2c0dbaSTao Ma  * 4) Remove the tree.
9358b2c0dbaSTao Ma  */
9368b2c0dbaSTao Ma int ocfs2_try_remove_refcount_tree(struct inode *inode,
9378b2c0dbaSTao Ma 				   struct buffer_head *di_bh)
9388b2c0dbaSTao Ma {
9398b2c0dbaSTao Ma 	int ret;
9408b2c0dbaSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
9418b2c0dbaSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
9428b2c0dbaSTao Ma 
9438b2c0dbaSTao Ma 	down_write(&oi->ip_xattr_sem);
9448b2c0dbaSTao Ma 	down_write(&oi->ip_alloc_sem);
9458b2c0dbaSTao Ma 
9468b2c0dbaSTao Ma 	if (oi->ip_clusters)
9478b2c0dbaSTao Ma 		goto out;
9488b2c0dbaSTao Ma 
9498b2c0dbaSTao Ma 	if ((oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) && di->i_xattr_loc)
9508b2c0dbaSTao Ma 		goto out;
9518b2c0dbaSTao Ma 
9528b2c0dbaSTao Ma 	if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL &&
9538b2c0dbaSTao Ma 	    ocfs2_has_inline_xattr_value_outside(inode, di))
9548b2c0dbaSTao Ma 		goto out;
9558b2c0dbaSTao Ma 
9568b2c0dbaSTao Ma 	ret = ocfs2_remove_refcount_tree(inode, di_bh);
9578b2c0dbaSTao Ma 	if (ret)
9588b2c0dbaSTao Ma 		mlog_errno(ret);
9598b2c0dbaSTao Ma out:
9608b2c0dbaSTao Ma 	up_write(&oi->ip_alloc_sem);
9618b2c0dbaSTao Ma 	up_write(&oi->ip_xattr_sem);
9628b2c0dbaSTao Ma 	return 0;
9638b2c0dbaSTao Ma }
9648b2c0dbaSTao Ma 
9658b2c0dbaSTao Ma /*
966e73a819dSTao Ma  * Given a cpos and len, try to find the refcount record which contains cpos.
967e73a819dSTao Ma  * 1. If cpos can be found in one refcount record, return the record.
968e73a819dSTao Ma  * 2. If cpos can't be found, return a fake record which start from cpos
969e73a819dSTao Ma  *    and end at a small value between cpos+len and start of the next record.
970e73a819dSTao Ma  *    This fake record has r_refcount = 0.
971e73a819dSTao Ma  */
972e73a819dSTao Ma static int ocfs2_get_refcount_rec(struct ocfs2_caching_info *ci,
973e73a819dSTao Ma 				  struct buffer_head *ref_root_bh,
974e73a819dSTao Ma 				  u64 cpos, unsigned int len,
975e73a819dSTao Ma 				  struct ocfs2_refcount_rec *ret_rec,
976e73a819dSTao Ma 				  int *index,
977e73a819dSTao Ma 				  struct buffer_head **ret_bh)
978e73a819dSTao Ma {
979e73a819dSTao Ma 	int ret = 0, i, found;
980e73a819dSTao Ma 	u32 low_cpos;
981e73a819dSTao Ma 	struct ocfs2_extent_list *el;
982e73a819dSTao Ma 	struct ocfs2_extent_rec *tmp, *rec = NULL;
983e73a819dSTao Ma 	struct ocfs2_extent_block *eb;
984e73a819dSTao Ma 	struct buffer_head *eb_bh = NULL, *ref_leaf_bh = NULL;
985e73a819dSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
986e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
987e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
988e73a819dSTao Ma 
989e73a819dSTao Ma 	if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)) {
990e73a819dSTao Ma 		ocfs2_find_refcount_rec_in_rl(ci, ref_root_bh, cpos, len,
991e73a819dSTao Ma 					      ret_rec, index);
992e73a819dSTao Ma 		*ret_bh = ref_root_bh;
993e73a819dSTao Ma 		get_bh(ref_root_bh);
994e73a819dSTao Ma 		return 0;
995e73a819dSTao Ma 	}
996e73a819dSTao Ma 
997e73a819dSTao Ma 	el = &rb->rf_list;
998e73a819dSTao Ma 	low_cpos = cpos & OCFS2_32BIT_POS_MASK;
999e73a819dSTao Ma 
1000e73a819dSTao Ma 	if (el->l_tree_depth) {
1001e73a819dSTao Ma 		ret = ocfs2_find_leaf(ci, el, low_cpos, &eb_bh);
1002e73a819dSTao Ma 		if (ret) {
1003e73a819dSTao Ma 			mlog_errno(ret);
1004e73a819dSTao Ma 			goto out;
1005e73a819dSTao Ma 		}
1006e73a819dSTao Ma 
1007e73a819dSTao Ma 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1008e73a819dSTao Ma 		el = &eb->h_list;
1009e73a819dSTao Ma 
1010e73a819dSTao Ma 		if (el->l_tree_depth) {
1011e73a819dSTao Ma 			ocfs2_error(sb,
1012e73a819dSTao Ma 			"refcount tree %llu has non zero tree "
1013e73a819dSTao Ma 			"depth in leaf btree tree block %llu\n",
1014e73a819dSTao Ma 			(unsigned long long)ocfs2_metadata_cache_owner(ci),
1015e73a819dSTao Ma 			(unsigned long long)eb_bh->b_blocknr);
1016e73a819dSTao Ma 			ret = -EROFS;
1017e73a819dSTao Ma 			goto out;
1018e73a819dSTao Ma 		}
1019e73a819dSTao Ma 	}
1020e73a819dSTao Ma 
1021e73a819dSTao Ma 	found = 0;
1022e73a819dSTao Ma 	for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
1023e73a819dSTao Ma 		rec = &el->l_recs[i];
1024e73a819dSTao Ma 
1025e73a819dSTao Ma 		if (le32_to_cpu(rec->e_cpos) <= low_cpos) {
1026e73a819dSTao Ma 			found = 1;
1027e73a819dSTao Ma 			break;
1028e73a819dSTao Ma 		}
1029e73a819dSTao Ma 	}
1030e73a819dSTao Ma 
1031e73a819dSTao Ma 	/* adjust len when we have ocfs2_extent_rec after it. */
1032e73a819dSTao Ma 	if (found && i < le16_to_cpu(el->l_next_free_rec) - 1) {
1033e73a819dSTao Ma 		tmp = &el->l_recs[i+1];
1034e73a819dSTao Ma 
1035e73a819dSTao Ma 		if (le32_to_cpu(tmp->e_cpos) < cpos + len)
1036e73a819dSTao Ma 			len = le32_to_cpu(tmp->e_cpos) - cpos;
1037e73a819dSTao Ma 	}
1038e73a819dSTao Ma 
1039e73a819dSTao Ma 	ret = ocfs2_read_refcount_block(ci, le64_to_cpu(rec->e_blkno),
1040e73a819dSTao Ma 					&ref_leaf_bh);
1041e73a819dSTao Ma 	if (ret) {
1042e73a819dSTao Ma 		mlog_errno(ret);
1043e73a819dSTao Ma 		goto out;
1044e73a819dSTao Ma 	}
1045e73a819dSTao Ma 
1046e73a819dSTao Ma 	ocfs2_find_refcount_rec_in_rl(ci, ref_leaf_bh, cpos, len,
1047e73a819dSTao Ma 				      ret_rec, index);
1048e73a819dSTao Ma 	*ret_bh = ref_leaf_bh;
1049e73a819dSTao Ma out:
1050e73a819dSTao Ma 	brelse(eb_bh);
1051e73a819dSTao Ma 	return ret;
1052e73a819dSTao Ma }
1053e73a819dSTao Ma 
1054e73a819dSTao Ma enum ocfs2_ref_rec_contig {
1055e73a819dSTao Ma 	REF_CONTIG_NONE = 0,
1056e73a819dSTao Ma 	REF_CONTIG_LEFT,
1057e73a819dSTao Ma 	REF_CONTIG_RIGHT,
1058e73a819dSTao Ma 	REF_CONTIG_LEFTRIGHT,
1059e73a819dSTao Ma };
1060e73a819dSTao Ma 
1061e73a819dSTao Ma static enum ocfs2_ref_rec_contig
1062e73a819dSTao Ma 	ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block *rb,
1063e73a819dSTao Ma 				    int index)
1064e73a819dSTao Ma {
1065e73a819dSTao Ma 	if ((rb->rf_records.rl_recs[index].r_refcount ==
1066e73a819dSTao Ma 	    rb->rf_records.rl_recs[index + 1].r_refcount) &&
1067e73a819dSTao Ma 	    (le64_to_cpu(rb->rf_records.rl_recs[index].r_cpos) +
1068e73a819dSTao Ma 	    le32_to_cpu(rb->rf_records.rl_recs[index].r_clusters) ==
1069e73a819dSTao Ma 	    le64_to_cpu(rb->rf_records.rl_recs[index + 1].r_cpos)))
1070e73a819dSTao Ma 		return REF_CONTIG_RIGHT;
1071e73a819dSTao Ma 
1072e73a819dSTao Ma 	return REF_CONTIG_NONE;
1073e73a819dSTao Ma }
1074e73a819dSTao Ma 
1075e73a819dSTao Ma static enum ocfs2_ref_rec_contig
1076e73a819dSTao Ma 	ocfs2_refcount_rec_contig(struct ocfs2_refcount_block *rb,
1077e73a819dSTao Ma 				  int index)
1078e73a819dSTao Ma {
1079e73a819dSTao Ma 	enum ocfs2_ref_rec_contig ret = REF_CONTIG_NONE;
1080e73a819dSTao Ma 
1081e73a819dSTao Ma 	if (index < le16_to_cpu(rb->rf_records.rl_used) - 1)
1082e73a819dSTao Ma 		ret = ocfs2_refcount_rec_adjacent(rb, index);
1083e73a819dSTao Ma 
1084e73a819dSTao Ma 	if (index > 0) {
1085e73a819dSTao Ma 		enum ocfs2_ref_rec_contig tmp;
1086e73a819dSTao Ma 
1087e73a819dSTao Ma 		tmp = ocfs2_refcount_rec_adjacent(rb, index - 1);
1088e73a819dSTao Ma 
1089e73a819dSTao Ma 		if (tmp == REF_CONTIG_RIGHT) {
1090e73a819dSTao Ma 			if (ret == REF_CONTIG_RIGHT)
1091e73a819dSTao Ma 				ret = REF_CONTIG_LEFTRIGHT;
1092e73a819dSTao Ma 			else
1093e73a819dSTao Ma 				ret = REF_CONTIG_LEFT;
1094e73a819dSTao Ma 		}
1095e73a819dSTao Ma 	}
1096e73a819dSTao Ma 
1097e73a819dSTao Ma 	return ret;
1098e73a819dSTao Ma }
1099e73a819dSTao Ma 
1100e73a819dSTao Ma static void ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block *rb,
1101e73a819dSTao Ma 					   int index)
1102e73a819dSTao Ma {
1103e73a819dSTao Ma 	BUG_ON(rb->rf_records.rl_recs[index].r_refcount !=
1104e73a819dSTao Ma 	       rb->rf_records.rl_recs[index+1].r_refcount);
1105e73a819dSTao Ma 
1106e73a819dSTao Ma 	le32_add_cpu(&rb->rf_records.rl_recs[index].r_clusters,
1107e73a819dSTao Ma 		     le32_to_cpu(rb->rf_records.rl_recs[index+1].r_clusters));
1108e73a819dSTao Ma 
1109e73a819dSTao Ma 	if (index < le16_to_cpu(rb->rf_records.rl_used) - 2)
1110e73a819dSTao Ma 		memmove(&rb->rf_records.rl_recs[index + 1],
1111e73a819dSTao Ma 			&rb->rf_records.rl_recs[index + 2],
1112e73a819dSTao Ma 			sizeof(struct ocfs2_refcount_rec) *
1113e73a819dSTao Ma 			(le16_to_cpu(rb->rf_records.rl_used) - index - 2));
1114e73a819dSTao Ma 
1115e73a819dSTao Ma 	memset(&rb->rf_records.rl_recs[le16_to_cpu(rb->rf_records.rl_used) - 1],
1116e73a819dSTao Ma 	       0, sizeof(struct ocfs2_refcount_rec));
1117e73a819dSTao Ma 	le16_add_cpu(&rb->rf_records.rl_used, -1);
1118e73a819dSTao Ma }
1119e73a819dSTao Ma 
1120e73a819dSTao Ma /*
1121e73a819dSTao Ma  * Merge the refcount rec if we are contiguous with the adjacent recs.
1122e73a819dSTao Ma  */
1123e73a819dSTao Ma static void ocfs2_refcount_rec_merge(struct ocfs2_refcount_block *rb,
1124e73a819dSTao Ma 				     int index)
1125e73a819dSTao Ma {
1126e73a819dSTao Ma 	enum ocfs2_ref_rec_contig contig =
1127e73a819dSTao Ma 				ocfs2_refcount_rec_contig(rb, index);
1128e73a819dSTao Ma 
1129e73a819dSTao Ma 	if (contig == REF_CONTIG_NONE)
1130e73a819dSTao Ma 		return;
1131e73a819dSTao Ma 
1132e73a819dSTao Ma 	if (contig == REF_CONTIG_LEFT || contig == REF_CONTIG_LEFTRIGHT) {
1133e73a819dSTao Ma 		BUG_ON(index == 0);
1134e73a819dSTao Ma 		index--;
1135e73a819dSTao Ma 	}
1136e73a819dSTao Ma 
1137e73a819dSTao Ma 	ocfs2_rotate_refcount_rec_left(rb, index);
1138e73a819dSTao Ma 
1139e73a819dSTao Ma 	if (contig == REF_CONTIG_LEFTRIGHT)
1140e73a819dSTao Ma 		ocfs2_rotate_refcount_rec_left(rb, index);
1141e73a819dSTao Ma }
1142e73a819dSTao Ma 
11431823cb0bSTao Ma /*
11441823cb0bSTao Ma  * Change the refcount indexed by "index" in ref_bh.
11451823cb0bSTao Ma  * If refcount reaches 0, remove it.
11461823cb0bSTao Ma  */
1147e73a819dSTao Ma static int ocfs2_change_refcount_rec(handle_t *handle,
1148e73a819dSTao Ma 				     struct ocfs2_caching_info *ci,
1149e73a819dSTao Ma 				     struct buffer_head *ref_leaf_bh,
1150e73a819dSTao Ma 				     int index, int change)
1151e73a819dSTao Ma {
1152e73a819dSTao Ma 	int ret;
1153e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1154e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
11551823cb0bSTao Ma 	struct ocfs2_refcount_list *rl = &rb->rf_records;
11561823cb0bSTao Ma 	struct ocfs2_refcount_rec *rec = &rl->rl_recs[index];
1157e73a819dSTao Ma 
1158e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1159e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1160e73a819dSTao Ma 	if (ret) {
1161e73a819dSTao Ma 		mlog_errno(ret);
1162e73a819dSTao Ma 		goto out;
1163e73a819dSTao Ma 	}
1164e73a819dSTao Ma 
1165e73a819dSTao Ma 	mlog(0, "change index %d, old count %u, change %d\n", index,
1166e73a819dSTao Ma 	     le32_to_cpu(rec->r_refcount), change);
1167e73a819dSTao Ma 	le32_add_cpu(&rec->r_refcount, change);
1168e73a819dSTao Ma 
11691823cb0bSTao Ma 	if (!rec->r_refcount) {
11701823cb0bSTao Ma 		if (index != le16_to_cpu(rl->rl_used) - 1) {
11711823cb0bSTao Ma 			memmove(rec, rec + 1,
11721823cb0bSTao Ma 				(le16_to_cpu(rl->rl_used) - index - 1) *
11731823cb0bSTao Ma 				sizeof(struct ocfs2_refcount_rec));
11741823cb0bSTao Ma 			memset(&rl->rl_recs[le16_to_cpu(rl->rl_used) - 1],
11751823cb0bSTao Ma 			       0, sizeof(struct ocfs2_refcount_rec));
11761823cb0bSTao Ma 		}
11771823cb0bSTao Ma 
11781823cb0bSTao Ma 		le16_add_cpu(&rl->rl_used, -1);
11791823cb0bSTao Ma 	} else
1180e73a819dSTao Ma 		ocfs2_refcount_rec_merge(rb, index);
1181e73a819dSTao Ma 
1182e73a819dSTao Ma 	ret = ocfs2_journal_dirty(handle, ref_leaf_bh);
1183e73a819dSTao Ma 	if (ret)
1184e73a819dSTao Ma 		mlog_errno(ret);
1185e73a819dSTao Ma out:
1186e73a819dSTao Ma 	return ret;
1187e73a819dSTao Ma }
1188e73a819dSTao Ma 
1189e73a819dSTao Ma static int ocfs2_expand_inline_ref_root(handle_t *handle,
1190e73a819dSTao Ma 					struct ocfs2_caching_info *ci,
1191e73a819dSTao Ma 					struct buffer_head *ref_root_bh,
1192e73a819dSTao Ma 					struct buffer_head **ref_leaf_bh,
1193e73a819dSTao Ma 					struct ocfs2_alloc_context *meta_ac)
1194e73a819dSTao Ma {
1195e73a819dSTao Ma 	int ret;
1196e73a819dSTao Ma 	u16 suballoc_bit_start;
1197e73a819dSTao Ma 	u32 num_got;
1198e73a819dSTao Ma 	u64 blkno;
1199e73a819dSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1200e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1201e73a819dSTao Ma 	struct ocfs2_refcount_block *new_rb;
1202e73a819dSTao Ma 	struct ocfs2_refcount_block *root_rb =
1203e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1204e73a819dSTao Ma 
1205e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
1206e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1207e73a819dSTao Ma 	if (ret) {
1208e73a819dSTao Ma 		mlog_errno(ret);
1209e73a819dSTao Ma 		goto out;
1210e73a819dSTao Ma 	}
1211e73a819dSTao Ma 
1212e73a819dSTao Ma 	ret = ocfs2_claim_metadata(OCFS2_SB(sb), handle, meta_ac, 1,
1213e73a819dSTao Ma 				   &suballoc_bit_start, &num_got,
1214e73a819dSTao Ma 				   &blkno);
1215e73a819dSTao Ma 	if (ret) {
1216e73a819dSTao Ma 		mlog_errno(ret);
1217e73a819dSTao Ma 		goto out;
1218e73a819dSTao Ma 	}
1219e73a819dSTao Ma 
1220e73a819dSTao Ma 	new_bh = sb_getblk(sb, blkno);
1221e73a819dSTao Ma 	if (new_bh == NULL) {
1222e73a819dSTao Ma 		ret = -EIO;
1223e73a819dSTao Ma 		mlog_errno(ret);
1224e73a819dSTao Ma 		goto out;
1225e73a819dSTao Ma 	}
1226e73a819dSTao Ma 	ocfs2_set_new_buffer_uptodate(ci, new_bh);
1227e73a819dSTao Ma 
1228e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, new_bh,
1229e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_CREATE);
1230e73a819dSTao Ma 	if (ret) {
1231e73a819dSTao Ma 		mlog_errno(ret);
1232e73a819dSTao Ma 		goto out;
1233e73a819dSTao Ma 	}
1234e73a819dSTao Ma 
1235e73a819dSTao Ma 	/*
1236e73a819dSTao Ma 	 * Initialize ocfs2_refcount_block.
1237e73a819dSTao Ma 	 * It should contain the same information as the old root.
1238e73a819dSTao Ma 	 * so just memcpy it and change the corresponding field.
1239e73a819dSTao Ma 	 */
1240e73a819dSTao Ma 	memcpy(new_bh->b_data, ref_root_bh->b_data, sb->s_blocksize);
1241e73a819dSTao Ma 
1242e73a819dSTao Ma 	new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1243e73a819dSTao Ma 	new_rb->rf_suballoc_slot = cpu_to_le16(OCFS2_SB(sb)->slot_num);
1244e73a819dSTao Ma 	new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1245e73a819dSTao Ma 	new_rb->rf_blkno = cpu_to_le64(blkno);
1246e73a819dSTao Ma 	new_rb->rf_cpos = cpu_to_le32(0);
1247e73a819dSTao Ma 	new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
1248e73a819dSTao Ma 	new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
1249e73a819dSTao Ma 	ocfs2_journal_dirty(handle, new_bh);
1250e73a819dSTao Ma 
1251e73a819dSTao Ma 	/* Now change the root. */
1252e73a819dSTao Ma 	memset(&root_rb->rf_list, 0, sb->s_blocksize -
1253e73a819dSTao Ma 	       offsetof(struct ocfs2_refcount_block, rf_list));
1254e73a819dSTao Ma 	root_rb->rf_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_rb(sb));
1255e73a819dSTao Ma 	root_rb->rf_clusters = cpu_to_le32(1);
1256e73a819dSTao Ma 	root_rb->rf_list.l_next_free_rec = cpu_to_le16(1);
1257e73a819dSTao Ma 	root_rb->rf_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
1258e73a819dSTao Ma 	root_rb->rf_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
1259e73a819dSTao Ma 	root_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_TREE_FL);
1260e73a819dSTao Ma 
1261e73a819dSTao Ma 	ocfs2_journal_dirty(handle, ref_root_bh);
1262e73a819dSTao Ma 
1263e73a819dSTao Ma 	mlog(0, "new leaf block %llu, used %u\n", (unsigned long long)blkno,
1264e73a819dSTao Ma 	     le16_to_cpu(new_rb->rf_records.rl_used));
1265e73a819dSTao Ma 
1266e73a819dSTao Ma 	*ref_leaf_bh = new_bh;
1267e73a819dSTao Ma 	new_bh = NULL;
1268e73a819dSTao Ma out:
1269e73a819dSTao Ma 	brelse(new_bh);
1270e73a819dSTao Ma 	return ret;
1271e73a819dSTao Ma }
1272e73a819dSTao Ma 
1273e73a819dSTao Ma static int ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec *prev,
1274e73a819dSTao Ma 					   struct ocfs2_refcount_rec *next)
1275e73a819dSTao Ma {
1276e73a819dSTao Ma 	if (ocfs2_get_ref_rec_low_cpos(prev) + le32_to_cpu(prev->r_clusters) <=
1277e73a819dSTao Ma 		ocfs2_get_ref_rec_low_cpos(next))
1278e73a819dSTao Ma 		return 1;
1279e73a819dSTao Ma 
1280e73a819dSTao Ma 	return 0;
1281e73a819dSTao Ma }
1282e73a819dSTao Ma 
1283e73a819dSTao Ma static int cmp_refcount_rec_by_low_cpos(const void *a, const void *b)
1284e73a819dSTao Ma {
1285e73a819dSTao Ma 	const struct ocfs2_refcount_rec *l = a, *r = b;
1286e73a819dSTao Ma 	u32 l_cpos = ocfs2_get_ref_rec_low_cpos(l);
1287e73a819dSTao Ma 	u32 r_cpos = ocfs2_get_ref_rec_low_cpos(r);
1288e73a819dSTao Ma 
1289e73a819dSTao Ma 	if (l_cpos > r_cpos)
1290e73a819dSTao Ma 		return 1;
1291e73a819dSTao Ma 	if (l_cpos < r_cpos)
1292e73a819dSTao Ma 		return -1;
1293e73a819dSTao Ma 	return 0;
1294e73a819dSTao Ma }
1295e73a819dSTao Ma 
1296e73a819dSTao Ma static int cmp_refcount_rec_by_cpos(const void *a, const void *b)
1297e73a819dSTao Ma {
1298e73a819dSTao Ma 	const struct ocfs2_refcount_rec *l = a, *r = b;
1299e73a819dSTao Ma 	u64 l_cpos = le64_to_cpu(l->r_cpos);
1300e73a819dSTao Ma 	u64 r_cpos = le64_to_cpu(r->r_cpos);
1301e73a819dSTao Ma 
1302e73a819dSTao Ma 	if (l_cpos > r_cpos)
1303e73a819dSTao Ma 		return 1;
1304e73a819dSTao Ma 	if (l_cpos < r_cpos)
1305e73a819dSTao Ma 		return -1;
1306e73a819dSTao Ma 	return 0;
1307e73a819dSTao Ma }
1308e73a819dSTao Ma 
1309e73a819dSTao Ma static void swap_refcount_rec(void *a, void *b, int size)
1310e73a819dSTao Ma {
1311e73a819dSTao Ma 	struct ocfs2_refcount_rec *l = a, *r = b, tmp;
1312e73a819dSTao Ma 
1313e73a819dSTao Ma 	tmp = *(struct ocfs2_refcount_rec *)l;
1314e73a819dSTao Ma 	*(struct ocfs2_refcount_rec *)l =
1315e73a819dSTao Ma 			*(struct ocfs2_refcount_rec *)r;
1316e73a819dSTao Ma 	*(struct ocfs2_refcount_rec *)r = tmp;
1317e73a819dSTao Ma }
1318e73a819dSTao Ma 
1319e73a819dSTao Ma /*
1320e73a819dSTao Ma  * The refcount cpos are ordered by their 64bit cpos,
1321e73a819dSTao Ma  * But we will use the low 32 bit to be the e_cpos in the b-tree.
1322e73a819dSTao Ma  * So we need to make sure that this pos isn't intersected with others.
1323e73a819dSTao Ma  *
1324e73a819dSTao Ma  * Note: The refcount block is already sorted by their low 32 bit cpos,
1325e73a819dSTao Ma  *       So just try the middle pos first, and we will exit when we find
1326e73a819dSTao Ma  *       the good position.
1327e73a819dSTao Ma  */
1328e73a819dSTao Ma static int ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list *rl,
1329e73a819dSTao Ma 					 u32 *split_pos, int *split_index)
1330e73a819dSTao Ma {
1331e73a819dSTao Ma 	int num_used = le16_to_cpu(rl->rl_used);
1332e73a819dSTao Ma 	int delta, middle = num_used / 2;
1333e73a819dSTao Ma 
1334e73a819dSTao Ma 	for (delta = 0; delta < middle; delta++) {
1335e73a819dSTao Ma 		/* Let's check delta earlier than middle */
1336e73a819dSTao Ma 		if (ocfs2_refcount_rec_no_intersect(
1337e73a819dSTao Ma 					&rl->rl_recs[middle - delta - 1],
1338e73a819dSTao Ma 					&rl->rl_recs[middle - delta])) {
1339e73a819dSTao Ma 			*split_index = middle - delta;
1340e73a819dSTao Ma 			break;
1341e73a819dSTao Ma 		}
1342e73a819dSTao Ma 
1343e73a819dSTao Ma 		/* For even counts, don't walk off the end */
1344e73a819dSTao Ma 		if ((middle + delta + 1) == num_used)
1345e73a819dSTao Ma 			continue;
1346e73a819dSTao Ma 
1347e73a819dSTao Ma 		/* Now try delta past middle */
1348e73a819dSTao Ma 		if (ocfs2_refcount_rec_no_intersect(
1349e73a819dSTao Ma 					&rl->rl_recs[middle + delta],
1350e73a819dSTao Ma 					&rl->rl_recs[middle + delta + 1])) {
1351e73a819dSTao Ma 			*split_index = middle + delta + 1;
1352e73a819dSTao Ma 			break;
1353e73a819dSTao Ma 		}
1354e73a819dSTao Ma 	}
1355e73a819dSTao Ma 
1356e73a819dSTao Ma 	if (delta >= middle)
1357e73a819dSTao Ma 		return -ENOSPC;
1358e73a819dSTao Ma 
1359e73a819dSTao Ma 	*split_pos = ocfs2_get_ref_rec_low_cpos(&rl->rl_recs[*split_index]);
1360e73a819dSTao Ma 	return 0;
1361e73a819dSTao Ma }
1362e73a819dSTao Ma 
1363e73a819dSTao Ma static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
1364e73a819dSTao Ma 					    struct buffer_head *new_bh,
1365e73a819dSTao Ma 					    u32 *split_cpos)
1366e73a819dSTao Ma {
1367e73a819dSTao Ma 	int split_index = 0, num_moved, ret;
1368e73a819dSTao Ma 	u32 cpos = 0;
1369e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1370e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1371e73a819dSTao Ma 	struct ocfs2_refcount_list *rl = &rb->rf_records;
1372e73a819dSTao Ma 	struct ocfs2_refcount_block *new_rb =
1373e73a819dSTao Ma 			(struct ocfs2_refcount_block *)new_bh->b_data;
1374e73a819dSTao Ma 	struct ocfs2_refcount_list *new_rl = &new_rb->rf_records;
1375e73a819dSTao Ma 
1376e73a819dSTao Ma 	mlog(0, "split old leaf refcount block %llu, count = %u, used = %u\n",
1377e73a819dSTao Ma 	     (unsigned long long)ref_leaf_bh->b_blocknr,
1378e73a819dSTao Ma 	     le32_to_cpu(rl->rl_count), le32_to_cpu(rl->rl_used));
1379e73a819dSTao Ma 
1380e73a819dSTao Ma 	/*
1381e73a819dSTao Ma 	 * XXX: Improvement later.
1382e73a819dSTao Ma 	 * If we know all the high 32 bit cpos is the same, no need to sort.
1383e73a819dSTao Ma 	 *
1384e73a819dSTao Ma 	 * In order to make the whole process safe, we do:
1385e73a819dSTao Ma 	 * 1. sort the entries by their low 32 bit cpos first so that we can
1386e73a819dSTao Ma 	 *    find the split cpos easily.
1387e73a819dSTao Ma 	 * 2. call ocfs2_insert_extent to insert the new refcount block.
1388e73a819dSTao Ma 	 * 3. move the refcount rec to the new block.
1389e73a819dSTao Ma 	 * 4. sort the entries by their 64 bit cpos.
1390e73a819dSTao Ma 	 * 5. dirty the new_rb and rb.
1391e73a819dSTao Ma 	 */
1392e73a819dSTao Ma 	sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
1393e73a819dSTao Ma 	     sizeof(struct ocfs2_refcount_rec),
1394e73a819dSTao Ma 	     cmp_refcount_rec_by_low_cpos, swap_refcount_rec);
1395e73a819dSTao Ma 
1396e73a819dSTao Ma 	ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index);
1397e73a819dSTao Ma 	if (ret) {
1398e73a819dSTao Ma 		mlog_errno(ret);
1399e73a819dSTao Ma 		return ret;
1400e73a819dSTao Ma 	}
1401e73a819dSTao Ma 
1402e73a819dSTao Ma 	new_rb->rf_cpos = cpu_to_le32(cpos);
1403e73a819dSTao Ma 
1404e73a819dSTao Ma 	/* move refcount records starting from split_index to the new block. */
1405e73a819dSTao Ma 	num_moved = le16_to_cpu(rl->rl_used) - split_index;
1406e73a819dSTao Ma 	memcpy(new_rl->rl_recs, &rl->rl_recs[split_index],
1407e73a819dSTao Ma 	       num_moved * sizeof(struct ocfs2_refcount_rec));
1408e73a819dSTao Ma 
1409e73a819dSTao Ma 	/*ok, remove the entries we just moved over to the other block. */
1410e73a819dSTao Ma 	memset(&rl->rl_recs[split_index], 0,
1411e73a819dSTao Ma 	       num_moved * sizeof(struct ocfs2_refcount_rec));
1412e73a819dSTao Ma 
1413e73a819dSTao Ma 	/* change old and new rl_used accordingly. */
1414e73a819dSTao Ma 	le16_add_cpu(&rl->rl_used, -num_moved);
1415e73a819dSTao Ma 	new_rl->rl_used = cpu_to_le32(num_moved);
1416e73a819dSTao Ma 
1417e73a819dSTao Ma 	sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
1418e73a819dSTao Ma 	     sizeof(struct ocfs2_refcount_rec),
1419e73a819dSTao Ma 	     cmp_refcount_rec_by_cpos, swap_refcount_rec);
1420e73a819dSTao Ma 
1421e73a819dSTao Ma 	sort(&new_rl->rl_recs, le16_to_cpu(new_rl->rl_used),
1422e73a819dSTao Ma 	     sizeof(struct ocfs2_refcount_rec),
1423e73a819dSTao Ma 	     cmp_refcount_rec_by_cpos, swap_refcount_rec);
1424e73a819dSTao Ma 
1425e73a819dSTao Ma 	*split_cpos = cpos;
1426e73a819dSTao Ma 	return 0;
1427e73a819dSTao Ma }
1428e73a819dSTao Ma 
1429e73a819dSTao Ma static int ocfs2_new_leaf_refcount_block(handle_t *handle,
1430e73a819dSTao Ma 					 struct ocfs2_caching_info *ci,
1431e73a819dSTao Ma 					 struct buffer_head *ref_root_bh,
1432e73a819dSTao Ma 					 struct buffer_head *ref_leaf_bh,
1433e73a819dSTao Ma 					 struct ocfs2_alloc_context *meta_ac)
1434e73a819dSTao Ma {
1435e73a819dSTao Ma 	int ret;
1436e73a819dSTao Ma 	u16 suballoc_bit_start;
1437e73a819dSTao Ma 	u32 num_got, new_cpos;
1438e73a819dSTao Ma 	u64 blkno;
1439e73a819dSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1440e73a819dSTao Ma 	struct ocfs2_refcount_block *root_rb =
1441e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1442e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1443e73a819dSTao Ma 	struct ocfs2_refcount_block *new_rb;
1444e73a819dSTao Ma 	struct ocfs2_extent_tree ref_et;
1445e73a819dSTao Ma 
1446e73a819dSTao Ma 	BUG_ON(!(le32_to_cpu(root_rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL));
1447e73a819dSTao Ma 
1448e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
1449e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1450e73a819dSTao Ma 	if (ret) {
1451e73a819dSTao Ma 		mlog_errno(ret);
1452e73a819dSTao Ma 		goto out;
1453e73a819dSTao Ma 	}
1454e73a819dSTao Ma 
1455e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1456e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1457e73a819dSTao Ma 	if (ret) {
1458e73a819dSTao Ma 		mlog_errno(ret);
1459e73a819dSTao Ma 		goto out;
1460e73a819dSTao Ma 	}
1461e73a819dSTao Ma 
1462e73a819dSTao Ma 	ret = ocfs2_claim_metadata(OCFS2_SB(sb), handle, meta_ac, 1,
1463e73a819dSTao Ma 				   &suballoc_bit_start, &num_got,
1464e73a819dSTao Ma 				   &blkno);
1465e73a819dSTao Ma 	if (ret) {
1466e73a819dSTao Ma 		mlog_errno(ret);
1467e73a819dSTao Ma 		goto out;
1468e73a819dSTao Ma 	}
1469e73a819dSTao Ma 
1470e73a819dSTao Ma 	new_bh = sb_getblk(sb, blkno);
1471e73a819dSTao Ma 	if (new_bh == NULL) {
1472e73a819dSTao Ma 		ret = -EIO;
1473e73a819dSTao Ma 		mlog_errno(ret);
1474e73a819dSTao Ma 		goto out;
1475e73a819dSTao Ma 	}
1476e73a819dSTao Ma 	ocfs2_set_new_buffer_uptodate(ci, new_bh);
1477e73a819dSTao Ma 
1478e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, new_bh,
1479e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_CREATE);
1480e73a819dSTao Ma 	if (ret) {
1481e73a819dSTao Ma 		mlog_errno(ret);
1482e73a819dSTao Ma 		goto out;
1483e73a819dSTao Ma 	}
1484e73a819dSTao Ma 
1485e73a819dSTao Ma 	/* Initialize ocfs2_refcount_block. */
1486e73a819dSTao Ma 	new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1487e73a819dSTao Ma 	memset(new_rb, 0, sb->s_blocksize);
1488e73a819dSTao Ma 	strcpy((void *)new_rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
1489e73a819dSTao Ma 	new_rb->rf_suballoc_slot = cpu_to_le16(OCFS2_SB(sb)->slot_num);
1490e73a819dSTao Ma 	new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1491e73a819dSTao Ma 	new_rb->rf_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
1492e73a819dSTao Ma 	new_rb->rf_blkno = cpu_to_le64(blkno);
1493e73a819dSTao Ma 	new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
1494e73a819dSTao Ma 	new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
1495e73a819dSTao Ma 	new_rb->rf_records.rl_count =
1496e73a819dSTao Ma 				cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
1497e73a819dSTao Ma 	new_rb->rf_generation = root_rb->rf_generation;
1498e73a819dSTao Ma 
1499e73a819dSTao Ma 	ret = ocfs2_divide_leaf_refcount_block(ref_leaf_bh, new_bh, &new_cpos);
1500e73a819dSTao Ma 	if (ret) {
1501e73a819dSTao Ma 		mlog_errno(ret);
1502e73a819dSTao Ma 		goto out;
1503e73a819dSTao Ma 	}
1504e73a819dSTao Ma 
1505e73a819dSTao Ma 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1506e73a819dSTao Ma 	ocfs2_journal_dirty(handle, new_bh);
1507e73a819dSTao Ma 
1508e73a819dSTao Ma 	ocfs2_init_refcount_extent_tree(&ref_et, ci, ref_root_bh);
1509e73a819dSTao Ma 
1510e73a819dSTao Ma 	mlog(0, "insert new leaf block %llu at %u\n",
1511e73a819dSTao Ma 	     (unsigned long long)new_bh->b_blocknr, new_cpos);
1512e73a819dSTao Ma 
1513e73a819dSTao Ma 	/* Insert the new leaf block with the specific offset cpos. */
1514e73a819dSTao Ma 	ret = ocfs2_insert_extent(handle, &ref_et, new_cpos, new_bh->b_blocknr,
1515e73a819dSTao Ma 				  1, 0, meta_ac);
1516e73a819dSTao Ma 	if (ret)
1517e73a819dSTao Ma 		mlog_errno(ret);
1518e73a819dSTao Ma 
1519e73a819dSTao Ma out:
1520e73a819dSTao Ma 	brelse(new_bh);
1521e73a819dSTao Ma 	return ret;
1522e73a819dSTao Ma }
1523e73a819dSTao Ma 
1524e73a819dSTao Ma static int ocfs2_expand_refcount_tree(handle_t *handle,
1525e73a819dSTao Ma 				      struct ocfs2_caching_info *ci,
1526e73a819dSTao Ma 				      struct buffer_head *ref_root_bh,
1527e73a819dSTao Ma 				      struct buffer_head *ref_leaf_bh,
1528e73a819dSTao Ma 				      struct ocfs2_alloc_context *meta_ac)
1529e73a819dSTao Ma {
1530e73a819dSTao Ma 	int ret;
1531e73a819dSTao Ma 	struct buffer_head *expand_bh = NULL;
1532e73a819dSTao Ma 
1533e73a819dSTao Ma 	if (ref_root_bh == ref_leaf_bh) {
1534e73a819dSTao Ma 		/*
1535e73a819dSTao Ma 		 * the old root bh hasn't been expanded to a b-tree,
1536e73a819dSTao Ma 		 * so expand it first.
1537e73a819dSTao Ma 		 */
1538e73a819dSTao Ma 		ret = ocfs2_expand_inline_ref_root(handle, ci, ref_root_bh,
1539e73a819dSTao Ma 						   &expand_bh, meta_ac);
1540e73a819dSTao Ma 		if (ret) {
1541e73a819dSTao Ma 			mlog_errno(ret);
1542e73a819dSTao Ma 			goto out;
1543e73a819dSTao Ma 		}
1544e73a819dSTao Ma 	} else {
1545e73a819dSTao Ma 		expand_bh = ref_leaf_bh;
1546e73a819dSTao Ma 		get_bh(expand_bh);
1547e73a819dSTao Ma 	}
1548e73a819dSTao Ma 
1549e73a819dSTao Ma 
1550e73a819dSTao Ma 	/* Now add a new refcount block into the tree.*/
1551e73a819dSTao Ma 	ret = ocfs2_new_leaf_refcount_block(handle, ci, ref_root_bh,
1552e73a819dSTao Ma 					    expand_bh, meta_ac);
1553e73a819dSTao Ma 	if (ret)
1554e73a819dSTao Ma 		mlog_errno(ret);
1555e73a819dSTao Ma out:
1556e73a819dSTao Ma 	brelse(expand_bh);
1557e73a819dSTao Ma 	return ret;
1558e73a819dSTao Ma }
1559e73a819dSTao Ma 
1560e73a819dSTao Ma /*
1561e73a819dSTao Ma  * Adjust the extent rec in b-tree representing ref_leaf_bh.
1562e73a819dSTao Ma  *
1563e73a819dSTao Ma  * Only called when we have inserted a new refcount rec at index 0
1564e73a819dSTao Ma  * which means ocfs2_extent_rec.e_cpos may need some change.
1565e73a819dSTao Ma  */
1566e73a819dSTao Ma static int ocfs2_adjust_refcount_rec(handle_t *handle,
1567e73a819dSTao Ma 				     struct ocfs2_caching_info *ci,
1568e73a819dSTao Ma 				     struct buffer_head *ref_root_bh,
1569e73a819dSTao Ma 				     struct buffer_head *ref_leaf_bh,
1570e73a819dSTao Ma 				     struct ocfs2_refcount_rec *rec)
1571e73a819dSTao Ma {
1572e73a819dSTao Ma 	int ret = 0, i;
1573e73a819dSTao Ma 	u32 new_cpos, old_cpos;
1574e73a819dSTao Ma 	struct ocfs2_path *path = NULL;
1575e73a819dSTao Ma 	struct ocfs2_extent_tree et;
1576e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1577e73a819dSTao Ma 		(struct ocfs2_refcount_block *)ref_root_bh->b_data;
1578e73a819dSTao Ma 	struct ocfs2_extent_list *el;
1579e73a819dSTao Ma 
1580e73a819dSTao Ma 	if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL))
1581e73a819dSTao Ma 		goto out;
1582e73a819dSTao Ma 
1583e73a819dSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1584e73a819dSTao Ma 	old_cpos = le32_to_cpu(rb->rf_cpos);
1585e73a819dSTao Ma 	new_cpos = le64_to_cpu(rec->r_cpos) & OCFS2_32BIT_POS_MASK;
1586e73a819dSTao Ma 	if (old_cpos <= new_cpos)
1587e73a819dSTao Ma 		goto out;
1588e73a819dSTao Ma 
1589e73a819dSTao Ma 	ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
1590e73a819dSTao Ma 
1591e73a819dSTao Ma 	path = ocfs2_new_path_from_et(&et);
1592e73a819dSTao Ma 	if (!path) {
1593e73a819dSTao Ma 		ret = -ENOMEM;
1594e73a819dSTao Ma 		mlog_errno(ret);
1595e73a819dSTao Ma 		goto out;
1596e73a819dSTao Ma 	}
1597e73a819dSTao Ma 
1598e73a819dSTao Ma 	ret = ocfs2_find_path(ci, path, old_cpos);
1599e73a819dSTao Ma 	if (ret) {
1600e73a819dSTao Ma 		mlog_errno(ret);
1601e73a819dSTao Ma 		goto out;
1602e73a819dSTao Ma 	}
1603e73a819dSTao Ma 
1604e73a819dSTao Ma 	/*
1605e73a819dSTao Ma 	 * 2 more credits, one for the leaf refcount block, one for
1606e73a819dSTao Ma 	 * the extent block contains the extent rec.
1607e73a819dSTao Ma 	 */
1608e73a819dSTao Ma 	ret = ocfs2_extend_trans(handle, handle->h_buffer_credits + 2);
1609e73a819dSTao Ma 	if (ret < 0) {
1610e73a819dSTao Ma 		mlog_errno(ret);
1611e73a819dSTao Ma 		goto out;
1612e73a819dSTao Ma 	}
1613e73a819dSTao Ma 
1614e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1615e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1616e73a819dSTao Ma 	if (ret < 0) {
1617e73a819dSTao Ma 		mlog_errno(ret);
1618e73a819dSTao Ma 		goto out;
1619e73a819dSTao Ma 	}
1620e73a819dSTao Ma 
1621e73a819dSTao Ma 	ret = ocfs2_journal_access_eb(handle, ci, path_leaf_bh(path),
1622e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1623e73a819dSTao Ma 	if (ret < 0) {
1624e73a819dSTao Ma 		mlog_errno(ret);
1625e73a819dSTao Ma 		goto out;
1626e73a819dSTao Ma 	}
1627e73a819dSTao Ma 
1628e73a819dSTao Ma 	/* change the leaf extent block first. */
1629e73a819dSTao Ma 	el = path_leaf_el(path);
1630e73a819dSTao Ma 
1631e73a819dSTao Ma 	for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++)
1632e73a819dSTao Ma 		if (le32_to_cpu(el->l_recs[i].e_cpos) == old_cpos)
1633e73a819dSTao Ma 			break;
1634e73a819dSTao Ma 
1635e73a819dSTao Ma 	BUG_ON(i == le16_to_cpu(el->l_next_free_rec));
1636e73a819dSTao Ma 
1637e73a819dSTao Ma 	el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1638e73a819dSTao Ma 
1639e73a819dSTao Ma 	/* change the r_cpos in the leaf block. */
1640e73a819dSTao Ma 	rb->rf_cpos = cpu_to_le32(new_cpos);
1641e73a819dSTao Ma 
1642e73a819dSTao Ma 	ocfs2_journal_dirty(handle, path_leaf_bh(path));
1643e73a819dSTao Ma 	ocfs2_journal_dirty(handle, ref_leaf_bh);
1644e73a819dSTao Ma 
1645e73a819dSTao Ma out:
1646e73a819dSTao Ma 	ocfs2_free_path(path);
1647e73a819dSTao Ma 	return ret;
1648e73a819dSTao Ma }
1649e73a819dSTao Ma 
1650e73a819dSTao Ma static int ocfs2_insert_refcount_rec(handle_t *handle,
1651e73a819dSTao Ma 				     struct ocfs2_caching_info *ci,
1652e73a819dSTao Ma 				     struct buffer_head *ref_root_bh,
1653e73a819dSTao Ma 				     struct buffer_head *ref_leaf_bh,
1654e73a819dSTao Ma 				     struct ocfs2_refcount_rec *rec,
1655e73a819dSTao Ma 				     int index,
1656e73a819dSTao Ma 				     struct ocfs2_alloc_context *meta_ac)
1657e73a819dSTao Ma {
1658e73a819dSTao Ma 	int ret;
1659e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1660e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1661e73a819dSTao Ma 	struct ocfs2_refcount_list *rf_list = &rb->rf_records;
1662e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1663e73a819dSTao Ma 
1664e73a819dSTao Ma 	BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
1665e73a819dSTao Ma 
1666e73a819dSTao Ma 	if (rf_list->rl_used == rf_list->rl_count) {
1667e73a819dSTao Ma 		u64 cpos = le64_to_cpu(rec->r_cpos);
1668e73a819dSTao Ma 		u32 len = le32_to_cpu(rec->r_clusters);
1669e73a819dSTao Ma 
1670e73a819dSTao Ma 		ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1671e73a819dSTao Ma 						 ref_leaf_bh, meta_ac);
1672e73a819dSTao Ma 		if (ret) {
1673e73a819dSTao Ma 			mlog_errno(ret);
1674e73a819dSTao Ma 			goto out;
1675e73a819dSTao Ma 		}
1676e73a819dSTao Ma 
1677e73a819dSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1678e73a819dSTao Ma 					     cpos, len, NULL, &index,
1679e73a819dSTao Ma 					     &new_bh);
1680e73a819dSTao Ma 		if (ret) {
1681e73a819dSTao Ma 			mlog_errno(ret);
1682e73a819dSTao Ma 			goto out;
1683e73a819dSTao Ma 		}
1684e73a819dSTao Ma 
1685e73a819dSTao Ma 		ref_leaf_bh = new_bh;
1686e73a819dSTao Ma 		rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1687e73a819dSTao Ma 		rf_list = &rb->rf_records;
1688e73a819dSTao Ma 	}
1689e73a819dSTao Ma 
1690e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1691e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1692e73a819dSTao Ma 	if (ret) {
1693e73a819dSTao Ma 		mlog_errno(ret);
1694e73a819dSTao Ma 		goto out;
1695e73a819dSTao Ma 	}
1696e73a819dSTao Ma 
1697e73a819dSTao Ma 	if (index < le16_to_cpu(rf_list->rl_used))
1698e73a819dSTao Ma 		memmove(&rf_list->rl_recs[index + 1],
1699e73a819dSTao Ma 			&rf_list->rl_recs[index],
1700e73a819dSTao Ma 			(le16_to_cpu(rf_list->rl_used) - index) *
1701e73a819dSTao Ma 			 sizeof(struct ocfs2_refcount_rec));
1702e73a819dSTao Ma 
1703e73a819dSTao Ma 	mlog(0, "insert refcount record start %llu, len %u, count %u "
1704e73a819dSTao Ma 	     "to leaf block %llu at index %d\n",
1705e73a819dSTao Ma 	     (unsigned long long)le64_to_cpu(rec->r_cpos),
1706e73a819dSTao Ma 	     le32_to_cpu(rec->r_clusters), le32_to_cpu(rec->r_refcount),
1707e73a819dSTao Ma 	     (unsigned long long)ref_leaf_bh->b_blocknr, index);
1708e73a819dSTao Ma 
1709e73a819dSTao Ma 	rf_list->rl_recs[index] = *rec;
1710e73a819dSTao Ma 
1711e73a819dSTao Ma 	le16_add_cpu(&rf_list->rl_used, 1);
1712e73a819dSTao Ma 
1713e73a819dSTao Ma 	ocfs2_refcount_rec_merge(rb, index);
1714e73a819dSTao Ma 
1715e73a819dSTao Ma 	ret = ocfs2_journal_dirty(handle, ref_leaf_bh);
1716e73a819dSTao Ma 	if (ret) {
1717e73a819dSTao Ma 		mlog_errno(ret);
1718e73a819dSTao Ma 		goto out;
1719e73a819dSTao Ma 	}
1720e73a819dSTao Ma 
1721e73a819dSTao Ma 	if (index == 0) {
1722e73a819dSTao Ma 		ret = ocfs2_adjust_refcount_rec(handle, ci,
1723e73a819dSTao Ma 						ref_root_bh,
1724e73a819dSTao Ma 						ref_leaf_bh, rec);
1725e73a819dSTao Ma 		if (ret)
1726e73a819dSTao Ma 			mlog_errno(ret);
1727e73a819dSTao Ma 	}
1728e73a819dSTao Ma out:
1729e73a819dSTao Ma 	brelse(new_bh);
1730e73a819dSTao Ma 	return ret;
1731e73a819dSTao Ma }
1732e73a819dSTao Ma 
1733e73a819dSTao Ma /*
1734e73a819dSTao Ma  * Split the refcount_rec indexed by "index" in ref_leaf_bh.
1735e73a819dSTao Ma  * This is much simple than our b-tree code.
1736e73a819dSTao Ma  * split_rec is the new refcount rec we want to insert.
1737e73a819dSTao Ma  * If split_rec->r_refcount > 0, we are changing the refcount(in case we
1738e73a819dSTao Ma  * increase refcount or decrease a refcount to non-zero).
1739e73a819dSTao Ma  * If split_rec->r_refcount == 0, we are punching a hole in current refcount
1740e73a819dSTao Ma  * rec( in case we decrease a refcount to zero).
1741e73a819dSTao Ma  */
1742e73a819dSTao Ma static int ocfs2_split_refcount_rec(handle_t *handle,
1743e73a819dSTao Ma 				    struct ocfs2_caching_info *ci,
1744e73a819dSTao Ma 				    struct buffer_head *ref_root_bh,
1745e73a819dSTao Ma 				    struct buffer_head *ref_leaf_bh,
1746e73a819dSTao Ma 				    struct ocfs2_refcount_rec *split_rec,
1747e73a819dSTao Ma 				    int index,
1748e73a819dSTao Ma 				    struct ocfs2_alloc_context *meta_ac,
1749e73a819dSTao Ma 				    struct ocfs2_cached_dealloc_ctxt *dealloc)
1750e73a819dSTao Ma {
1751e73a819dSTao Ma 	int ret, recs_need;
1752e73a819dSTao Ma 	u32 len;
1753e73a819dSTao Ma 	struct ocfs2_refcount_block *rb =
1754e73a819dSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1755e73a819dSTao Ma 	struct ocfs2_refcount_list *rf_list = &rb->rf_records;
1756e73a819dSTao Ma 	struct ocfs2_refcount_rec *orig_rec = &rf_list->rl_recs[index];
1757e73a819dSTao Ma 	struct ocfs2_refcount_rec *tail_rec = NULL;
1758e73a819dSTao Ma 	struct buffer_head *new_bh = NULL;
1759e73a819dSTao Ma 
1760e73a819dSTao Ma 	BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
1761e73a819dSTao Ma 
1762e73a819dSTao Ma 	mlog(0, "original r_pos %llu, cluster %u, split %llu, cluster %u\n",
1763e73a819dSTao Ma 	     le64_to_cpu(orig_rec->r_cpos), le32_to_cpu(orig_rec->r_clusters),
1764e73a819dSTao Ma 	     le64_to_cpu(split_rec->r_cpos),
1765e73a819dSTao Ma 	     le32_to_cpu(split_rec->r_clusters));
1766e73a819dSTao Ma 
1767e73a819dSTao Ma 	/*
1768e73a819dSTao Ma 	 * If we just need to split the header or tail clusters,
1769e73a819dSTao Ma 	 * no more recs are needed, just split is OK.
1770e73a819dSTao Ma 	 * Otherwise we at least need one new recs.
1771e73a819dSTao Ma 	 */
1772e73a819dSTao Ma 	if (!split_rec->r_refcount &&
1773e73a819dSTao Ma 	    (split_rec->r_cpos == orig_rec->r_cpos ||
1774e73a819dSTao Ma 	     le64_to_cpu(split_rec->r_cpos) +
1775e73a819dSTao Ma 	     le32_to_cpu(split_rec->r_clusters) ==
1776e73a819dSTao Ma 	     le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
1777e73a819dSTao Ma 		recs_need = 0;
1778e73a819dSTao Ma 	else
1779e73a819dSTao Ma 		recs_need = 1;
1780e73a819dSTao Ma 
1781e73a819dSTao Ma 	/*
1782e73a819dSTao Ma 	 * We need one more rec if we split in the middle and the new rec have
1783e73a819dSTao Ma 	 * some refcount in it.
1784e73a819dSTao Ma 	 */
1785e73a819dSTao Ma 	if (split_rec->r_refcount &&
1786e73a819dSTao Ma 	    (split_rec->r_cpos != orig_rec->r_cpos &&
1787e73a819dSTao Ma 	     le64_to_cpu(split_rec->r_cpos) +
1788e73a819dSTao Ma 	     le32_to_cpu(split_rec->r_clusters) !=
1789e73a819dSTao Ma 	     le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
1790e73a819dSTao Ma 		recs_need++;
1791e73a819dSTao Ma 
1792e73a819dSTao Ma 	/* If the leaf block don't have enough record, expand it. */
1793e73a819dSTao Ma 	if (le16_to_cpu(rf_list->rl_used) + recs_need > rf_list->rl_count) {
1794e73a819dSTao Ma 		struct ocfs2_refcount_rec tmp_rec;
1795e73a819dSTao Ma 		u64 cpos = le64_to_cpu(orig_rec->r_cpos);
1796e73a819dSTao Ma 		len = le32_to_cpu(orig_rec->r_clusters);
1797e73a819dSTao Ma 		ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1798e73a819dSTao Ma 						 ref_leaf_bh, meta_ac);
1799e73a819dSTao Ma 		if (ret) {
1800e73a819dSTao Ma 			mlog_errno(ret);
1801e73a819dSTao Ma 			goto out;
1802e73a819dSTao Ma 		}
1803e73a819dSTao Ma 
1804e73a819dSTao Ma 		/*
1805e73a819dSTao Ma 		 * We have to re-get it since now cpos may be moved to
1806e73a819dSTao Ma 		 * another leaf block.
1807e73a819dSTao Ma 		 */
1808e73a819dSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1809e73a819dSTao Ma 					     cpos, len, &tmp_rec, &index,
1810e73a819dSTao Ma 					     &new_bh);
1811e73a819dSTao Ma 		if (ret) {
1812e73a819dSTao Ma 			mlog_errno(ret);
1813e73a819dSTao Ma 			goto out;
1814e73a819dSTao Ma 		}
1815e73a819dSTao Ma 
1816e73a819dSTao Ma 		ref_leaf_bh = new_bh;
1817e73a819dSTao Ma 		rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1818e73a819dSTao Ma 		rf_list = &rb->rf_records;
1819e73a819dSTao Ma 		orig_rec = &rf_list->rl_recs[index];
1820e73a819dSTao Ma 	}
1821e73a819dSTao Ma 
1822e73a819dSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1823e73a819dSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
1824e73a819dSTao Ma 	if (ret) {
1825e73a819dSTao Ma 		mlog_errno(ret);
1826e73a819dSTao Ma 		goto out;
1827e73a819dSTao Ma 	}
1828e73a819dSTao Ma 
1829e73a819dSTao Ma 	/*
1830e73a819dSTao Ma 	 * We have calculated out how many new records we need and store
1831e73a819dSTao Ma 	 * in recs_need, so spare enough space first by moving the records
1832e73a819dSTao Ma 	 * after "index" to the end.
1833e73a819dSTao Ma 	 */
1834e73a819dSTao Ma 	if (index != le16_to_cpu(rf_list->rl_used) - 1)
1835e73a819dSTao Ma 		memmove(&rf_list->rl_recs[index + 1 + recs_need],
1836e73a819dSTao Ma 			&rf_list->rl_recs[index + 1],
1837e73a819dSTao Ma 			(le16_to_cpu(rf_list->rl_used) - index - 1) *
1838e73a819dSTao Ma 			 sizeof(struct ocfs2_refcount_rec));
1839e73a819dSTao Ma 
1840e73a819dSTao Ma 	len = (le64_to_cpu(orig_rec->r_cpos) +
1841e73a819dSTao Ma 	      le32_to_cpu(orig_rec->r_clusters)) -
1842e73a819dSTao Ma 	      (le64_to_cpu(split_rec->r_cpos) +
1843e73a819dSTao Ma 	      le32_to_cpu(split_rec->r_clusters));
1844e73a819dSTao Ma 
1845e73a819dSTao Ma 	/*
1846e73a819dSTao Ma 	 * If we have "len", the we will split in the tail and move it
1847e73a819dSTao Ma 	 * to the end of the space we have just spared.
1848e73a819dSTao Ma 	 */
1849e73a819dSTao Ma 	if (len) {
1850e73a819dSTao Ma 		tail_rec = &rf_list->rl_recs[index + recs_need];
1851e73a819dSTao Ma 
1852e73a819dSTao Ma 		memcpy(tail_rec, orig_rec, sizeof(struct ocfs2_refcount_rec));
1853e73a819dSTao Ma 		le64_add_cpu(&tail_rec->r_cpos,
1854e73a819dSTao Ma 			     le32_to_cpu(tail_rec->r_clusters) - len);
1855e73a819dSTao Ma 		tail_rec->r_clusters = le32_to_cpu(len);
1856e73a819dSTao Ma 	}
1857e73a819dSTao Ma 
1858e73a819dSTao Ma 	/*
1859e73a819dSTao Ma 	 * If the split pos isn't the same as the original one, we need to
1860e73a819dSTao Ma 	 * split in the head.
1861e73a819dSTao Ma 	 *
1862e73a819dSTao Ma 	 * Note: We have the chance that split_rec.r_refcount = 0,
1863e73a819dSTao Ma 	 * recs_need = 0 and len > 0, which means we just cut the head from
1864e73a819dSTao Ma 	 * the orig_rec and in that case we have done some modification in
1865e73a819dSTao Ma 	 * orig_rec above, so the check for r_cpos is faked.
1866e73a819dSTao Ma 	 */
1867e73a819dSTao Ma 	if (split_rec->r_cpos != orig_rec->r_cpos && tail_rec != orig_rec) {
1868e73a819dSTao Ma 		len = le64_to_cpu(split_rec->r_cpos) -
1869e73a819dSTao Ma 		      le64_to_cpu(orig_rec->r_cpos);
1870e73a819dSTao Ma 		orig_rec->r_clusters = cpu_to_le32(len);
1871e73a819dSTao Ma 		index++;
1872e73a819dSTao Ma 	}
1873e73a819dSTao Ma 
1874e73a819dSTao Ma 	le16_add_cpu(&rf_list->rl_used, recs_need);
1875e73a819dSTao Ma 
1876e73a819dSTao Ma 	if (split_rec->r_refcount) {
1877e73a819dSTao Ma 		rf_list->rl_recs[index] = *split_rec;
1878e73a819dSTao Ma 		mlog(0, "insert refcount record start %llu, len %u, count %u "
1879e73a819dSTao Ma 		     "to leaf block %llu at index %d\n",
1880e73a819dSTao Ma 		     (unsigned long long)le64_to_cpu(split_rec->r_cpos),
1881e73a819dSTao Ma 		     le32_to_cpu(split_rec->r_clusters),
1882e73a819dSTao Ma 		     le32_to_cpu(split_rec->r_refcount),
1883e73a819dSTao Ma 		     (unsigned long long)ref_leaf_bh->b_blocknr, index);
1884e73a819dSTao Ma 
1885e73a819dSTao Ma 		ocfs2_refcount_rec_merge(rb, index);
1886e73a819dSTao Ma 	}
1887e73a819dSTao Ma 
1888e73a819dSTao Ma 	ret = ocfs2_journal_dirty(handle, ref_leaf_bh);
1889e73a819dSTao Ma 	if (ret)
1890e73a819dSTao Ma 		mlog_errno(ret);
1891e73a819dSTao Ma 
1892e73a819dSTao Ma out:
1893e73a819dSTao Ma 	brelse(new_bh);
1894e73a819dSTao Ma 	return ret;
1895e73a819dSTao Ma }
1896e73a819dSTao Ma 
18972999d12fSTao Ma int ocfs2_increase_refcount(handle_t *handle,
1898e73a819dSTao Ma 			    struct ocfs2_caching_info *ci,
1899e73a819dSTao Ma 			    struct buffer_head *ref_root_bh,
1900e73a819dSTao Ma 			    u64 cpos, u32 len,
1901e73a819dSTao Ma 			    struct ocfs2_alloc_context *meta_ac,
1902e73a819dSTao Ma 			    struct ocfs2_cached_dealloc_ctxt *dealloc)
1903e73a819dSTao Ma {
1904e73a819dSTao Ma 	int ret = 0, index;
1905e73a819dSTao Ma 	struct buffer_head *ref_leaf_bh = NULL;
1906e73a819dSTao Ma 	struct ocfs2_refcount_rec rec;
1907e73a819dSTao Ma 	unsigned int set_len = 0;
1908e73a819dSTao Ma 
1909e73a819dSTao Ma 	mlog(0, "Tree owner %llu, add refcount start %llu, len %u\n",
1910e73a819dSTao Ma 	     (unsigned long long)ocfs2_metadata_cache_owner(ci),
1911e73a819dSTao Ma 	     (unsigned long long)cpos, len);
1912e73a819dSTao Ma 
1913e73a819dSTao Ma 	while (len) {
1914e73a819dSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1915e73a819dSTao Ma 					     cpos, len, &rec, &index,
1916e73a819dSTao Ma 					     &ref_leaf_bh);
1917e73a819dSTao Ma 		if (ret) {
1918e73a819dSTao Ma 			mlog_errno(ret);
1919e73a819dSTao Ma 			goto out;
1920e73a819dSTao Ma 		}
1921e73a819dSTao Ma 
1922e73a819dSTao Ma 		set_len = le32_to_cpu(rec.r_clusters);
1923e73a819dSTao Ma 
1924e73a819dSTao Ma 		/*
1925e73a819dSTao Ma 		 * Here we may meet with 3 situations:
1926e73a819dSTao Ma 		 *
1927e73a819dSTao Ma 		 * 1. If we find an already existing record, and the length
1928e73a819dSTao Ma 		 *    is the same, cool, we just need to increase the r_refcount
1929e73a819dSTao Ma 		 *    and it is OK.
1930e73a819dSTao Ma 		 * 2. If we find a hole, just insert it with r_refcount = 1.
1931e73a819dSTao Ma 		 * 3. If we are in the middle of one extent record, split
1932e73a819dSTao Ma 		 *    it.
1933e73a819dSTao Ma 		 */
1934e73a819dSTao Ma 		if (rec.r_refcount && le64_to_cpu(rec.r_cpos) == cpos &&
1935e73a819dSTao Ma 		    set_len <= len) {
1936e73a819dSTao Ma 			mlog(0, "increase refcount rec, start %llu, len %u, "
1937e73a819dSTao Ma 			     "count %u\n", (unsigned long long)cpos, set_len,
1938e73a819dSTao Ma 			     le32_to_cpu(rec.r_refcount));
1939e73a819dSTao Ma 			ret = ocfs2_change_refcount_rec(handle, ci,
1940e73a819dSTao Ma 							ref_leaf_bh, index, 1);
1941e73a819dSTao Ma 			if (ret) {
1942e73a819dSTao Ma 				mlog_errno(ret);
1943e73a819dSTao Ma 				goto out;
1944e73a819dSTao Ma 			}
1945e73a819dSTao Ma 		} else if (!rec.r_refcount) {
1946e73a819dSTao Ma 			rec.r_refcount = cpu_to_le32(1);
1947e73a819dSTao Ma 
1948e73a819dSTao Ma 			mlog(0, "insert refcount rec, start %llu, len %u\n",
1949e73a819dSTao Ma 			     (unsigned long long)le64_to_cpu(rec.r_cpos),
1950e73a819dSTao Ma 			     set_len);
1951e73a819dSTao Ma 			ret = ocfs2_insert_refcount_rec(handle, ci, ref_root_bh,
1952e73a819dSTao Ma 							ref_leaf_bh,
1953e73a819dSTao Ma 							&rec, index, meta_ac);
1954e73a819dSTao Ma 			if (ret) {
1955e73a819dSTao Ma 				mlog_errno(ret);
1956e73a819dSTao Ma 				goto out;
1957e73a819dSTao Ma 			}
1958e73a819dSTao Ma 		} else  {
1959e73a819dSTao Ma 			set_len = min((u64)(cpos + len),
1960e73a819dSTao Ma 				      le64_to_cpu(rec.r_cpos) + set_len) - cpos;
1961e73a819dSTao Ma 			rec.r_cpos = cpu_to_le64(cpos);
1962e73a819dSTao Ma 			rec.r_clusters = cpu_to_le32(set_len);
1963e73a819dSTao Ma 			le32_add_cpu(&rec.r_refcount, 1);
1964e73a819dSTao Ma 
1965e73a819dSTao Ma 			mlog(0, "split refcount rec, start %llu, "
1966e73a819dSTao Ma 			     "len %u, count %u\n",
1967e73a819dSTao Ma 			     (unsigned long long)le64_to_cpu(rec.r_cpos),
1968e73a819dSTao Ma 			     set_len, le32_to_cpu(rec.r_refcount));
1969e73a819dSTao Ma 			ret = ocfs2_split_refcount_rec(handle, ci,
1970e73a819dSTao Ma 						       ref_root_bh, ref_leaf_bh,
1971e73a819dSTao Ma 						       &rec, index,
1972e73a819dSTao Ma 						       meta_ac, dealloc);
1973e73a819dSTao Ma 			if (ret) {
1974e73a819dSTao Ma 				mlog_errno(ret);
1975e73a819dSTao Ma 				goto out;
1976e73a819dSTao Ma 			}
1977e73a819dSTao Ma 		}
1978e73a819dSTao Ma 
1979e73a819dSTao Ma 		cpos += set_len;
1980e73a819dSTao Ma 		len -= set_len;
1981e73a819dSTao Ma 		brelse(ref_leaf_bh);
1982e73a819dSTao Ma 		ref_leaf_bh = NULL;
1983e73a819dSTao Ma 	}
1984e73a819dSTao Ma 
1985e73a819dSTao Ma out:
1986e73a819dSTao Ma 	brelse(ref_leaf_bh);
1987e73a819dSTao Ma 	return ret;
1988e73a819dSTao Ma }
19891823cb0bSTao Ma 
19901823cb0bSTao Ma static int ocfs2_remove_refcount_extent(handle_t *handle,
19911823cb0bSTao Ma 				struct ocfs2_caching_info *ci,
19921823cb0bSTao Ma 				struct buffer_head *ref_root_bh,
19931823cb0bSTao Ma 				struct buffer_head *ref_leaf_bh,
19941823cb0bSTao Ma 				struct ocfs2_alloc_context *meta_ac,
19951823cb0bSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
19961823cb0bSTao Ma {
19971823cb0bSTao Ma 	int ret;
19981823cb0bSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
19991823cb0bSTao Ma 	struct ocfs2_refcount_block *rb =
20001823cb0bSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
20011823cb0bSTao Ma 	struct ocfs2_extent_tree et;
20021823cb0bSTao Ma 
20031823cb0bSTao Ma 	BUG_ON(rb->rf_records.rl_used);
20041823cb0bSTao Ma 
20051823cb0bSTao Ma 	ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
20061823cb0bSTao Ma 	ret = ocfs2_remove_extent(handle, &et, le32_to_cpu(rb->rf_cpos),
20071823cb0bSTao Ma 				  1, meta_ac, dealloc);
20081823cb0bSTao Ma 	if (ret) {
20091823cb0bSTao Ma 		mlog_errno(ret);
20101823cb0bSTao Ma 		goto out;
20111823cb0bSTao Ma 	}
20121823cb0bSTao Ma 
20131823cb0bSTao Ma 	ocfs2_remove_from_cache(ci, ref_leaf_bh);
20141823cb0bSTao Ma 
20151823cb0bSTao Ma 	/*
20161823cb0bSTao Ma 	 * add the freed block to the dealloc so that it will be freed
20171823cb0bSTao Ma 	 * when we run dealloc.
20181823cb0bSTao Ma 	 */
20191823cb0bSTao Ma 	ret = ocfs2_cache_block_dealloc(dealloc, EXTENT_ALLOC_SYSTEM_INODE,
20201823cb0bSTao Ma 					le16_to_cpu(rb->rf_suballoc_slot),
20211823cb0bSTao Ma 					le64_to_cpu(rb->rf_blkno),
20221823cb0bSTao Ma 					le16_to_cpu(rb->rf_suballoc_bit));
20231823cb0bSTao Ma 	if (ret) {
20241823cb0bSTao Ma 		mlog_errno(ret);
20251823cb0bSTao Ma 		goto out;
20261823cb0bSTao Ma 	}
20271823cb0bSTao Ma 
20281823cb0bSTao Ma 	ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
20291823cb0bSTao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
20301823cb0bSTao Ma 	if (ret) {
20311823cb0bSTao Ma 		mlog_errno(ret);
20321823cb0bSTao Ma 		goto out;
20331823cb0bSTao Ma 	}
20341823cb0bSTao Ma 
20351823cb0bSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
20361823cb0bSTao Ma 
20371823cb0bSTao Ma 	le32_add_cpu(&rb->rf_clusters, -1);
20381823cb0bSTao Ma 
20391823cb0bSTao Ma 	/*
20401823cb0bSTao Ma 	 * check whether we need to restore the root refcount block if
20411823cb0bSTao Ma 	 * there is no leaf extent block at atll.
20421823cb0bSTao Ma 	 */
20431823cb0bSTao Ma 	if (!rb->rf_list.l_next_free_rec) {
20441823cb0bSTao Ma 		BUG_ON(rb->rf_clusters);
20451823cb0bSTao Ma 
20461823cb0bSTao Ma 		mlog(0, "reset refcount tree root %llu to be a record block.\n",
20471823cb0bSTao Ma 		     (unsigned long long)ref_root_bh->b_blocknr);
20481823cb0bSTao Ma 
20491823cb0bSTao Ma 		rb->rf_flags = 0;
20501823cb0bSTao Ma 		rb->rf_parent = 0;
20511823cb0bSTao Ma 		rb->rf_cpos = 0;
20521823cb0bSTao Ma 		memset(&rb->rf_records, 0, sb->s_blocksize -
20531823cb0bSTao Ma 		       offsetof(struct ocfs2_refcount_block, rf_records));
20541823cb0bSTao Ma 		rb->rf_records.rl_count =
20551823cb0bSTao Ma 				cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
20561823cb0bSTao Ma 	}
20571823cb0bSTao Ma 
20581823cb0bSTao Ma 	ocfs2_journal_dirty(handle, ref_root_bh);
20591823cb0bSTao Ma 
20601823cb0bSTao Ma out:
20611823cb0bSTao Ma 	return ret;
20621823cb0bSTao Ma }
20631823cb0bSTao Ma 
20641823cb0bSTao Ma static int ocfs2_decrease_refcount_rec(handle_t *handle,
20651823cb0bSTao Ma 				struct ocfs2_caching_info *ci,
20661823cb0bSTao Ma 				struct buffer_head *ref_root_bh,
20671823cb0bSTao Ma 				struct buffer_head *ref_leaf_bh,
20681823cb0bSTao Ma 				int index, u64 cpos, unsigned int len,
20691823cb0bSTao Ma 				struct ocfs2_alloc_context *meta_ac,
20701823cb0bSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
20711823cb0bSTao Ma {
20721823cb0bSTao Ma 	int ret;
20731823cb0bSTao Ma 	struct ocfs2_refcount_block *rb =
20741823cb0bSTao Ma 			(struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
20751823cb0bSTao Ma 	struct ocfs2_refcount_rec *rec = &rb->rf_records.rl_recs[index];
20761823cb0bSTao Ma 
20771823cb0bSTao Ma 	BUG_ON(cpos < le64_to_cpu(rec->r_cpos));
20781823cb0bSTao Ma 	BUG_ON(cpos + len >
20791823cb0bSTao Ma 	       le64_to_cpu(rec->r_cpos) + le32_to_cpu(rec->r_clusters));
20801823cb0bSTao Ma 
20811823cb0bSTao Ma 	if (cpos == le64_to_cpu(rec->r_cpos) &&
20821823cb0bSTao Ma 	    len == le32_to_cpu(rec->r_clusters))
20831823cb0bSTao Ma 		ret = ocfs2_change_refcount_rec(handle, ci,
20841823cb0bSTao Ma 						ref_leaf_bh, index, -1);
20851823cb0bSTao Ma 	else {
20861823cb0bSTao Ma 		struct ocfs2_refcount_rec split = *rec;
20871823cb0bSTao Ma 		split.r_cpos = cpu_to_le64(cpos);
20881823cb0bSTao Ma 		split.r_clusters = cpu_to_le32(len);
20891823cb0bSTao Ma 
20901823cb0bSTao Ma 		le32_add_cpu(&split.r_refcount, -1);
20911823cb0bSTao Ma 
20921823cb0bSTao Ma 		mlog(0, "split refcount rec, start %llu, "
20931823cb0bSTao Ma 		     "len %u, count %u, original start %llu, len %u\n",
20941823cb0bSTao Ma 		     (unsigned long long)le64_to_cpu(split.r_cpos),
20951823cb0bSTao Ma 		     len, le32_to_cpu(split.r_refcount),
20961823cb0bSTao Ma 		     (unsigned long long)le64_to_cpu(rec->r_cpos),
20971823cb0bSTao Ma 		     le32_to_cpu(rec->r_clusters));
20981823cb0bSTao Ma 		ret = ocfs2_split_refcount_rec(handle, ci,
20991823cb0bSTao Ma 					       ref_root_bh, ref_leaf_bh,
21001823cb0bSTao Ma 					       &split, index,
21011823cb0bSTao Ma 					       meta_ac, dealloc);
21021823cb0bSTao Ma 	}
21031823cb0bSTao Ma 
21041823cb0bSTao Ma 	if (ret) {
21051823cb0bSTao Ma 		mlog_errno(ret);
21061823cb0bSTao Ma 		goto out;
21071823cb0bSTao Ma 	}
21081823cb0bSTao Ma 
21091823cb0bSTao Ma 	/* Remove the leaf refcount block if it contains no refcount record. */
21101823cb0bSTao Ma 	if (!rb->rf_records.rl_used && ref_leaf_bh != ref_root_bh) {
21111823cb0bSTao Ma 		ret = ocfs2_remove_refcount_extent(handle, ci, ref_root_bh,
21121823cb0bSTao Ma 						   ref_leaf_bh, meta_ac,
21131823cb0bSTao Ma 						   dealloc);
21141823cb0bSTao Ma 		if (ret)
21151823cb0bSTao Ma 			mlog_errno(ret);
21161823cb0bSTao Ma 	}
21171823cb0bSTao Ma 
21181823cb0bSTao Ma out:
21191823cb0bSTao Ma 	return ret;
21201823cb0bSTao Ma }
21211823cb0bSTao Ma 
21221823cb0bSTao Ma static int __ocfs2_decrease_refcount(handle_t *handle,
21231823cb0bSTao Ma 				     struct ocfs2_caching_info *ci,
21241823cb0bSTao Ma 				     struct buffer_head *ref_root_bh,
21251823cb0bSTao Ma 				     u64 cpos, u32 len,
21261823cb0bSTao Ma 				     struct ocfs2_alloc_context *meta_ac,
21276ae23c55STao Ma 				     struct ocfs2_cached_dealloc_ctxt *dealloc,
21286ae23c55STao Ma 				     int delete)
21291823cb0bSTao Ma {
21301823cb0bSTao Ma 	int ret = 0, index = 0;
21311823cb0bSTao Ma 	struct ocfs2_refcount_rec rec;
21321823cb0bSTao Ma 	unsigned int r_count = 0, r_len;
21331823cb0bSTao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
21341823cb0bSTao Ma 	struct buffer_head *ref_leaf_bh = NULL;
21351823cb0bSTao Ma 
21366ae23c55STao Ma 	mlog(0, "Tree owner %llu, decrease refcount start %llu, "
21376ae23c55STao Ma 	     "len %u, delete %u\n",
21381823cb0bSTao Ma 	     (unsigned long long)ocfs2_metadata_cache_owner(ci),
21396ae23c55STao Ma 	     (unsigned long long)cpos, len, delete);
21401823cb0bSTao Ma 
21411823cb0bSTao Ma 	while (len) {
21421823cb0bSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
21431823cb0bSTao Ma 					     cpos, len, &rec, &index,
21441823cb0bSTao Ma 					     &ref_leaf_bh);
21451823cb0bSTao Ma 		if (ret) {
21461823cb0bSTao Ma 			mlog_errno(ret);
21471823cb0bSTao Ma 			goto out;
21481823cb0bSTao Ma 		}
21491823cb0bSTao Ma 
21501823cb0bSTao Ma 		r_count = le32_to_cpu(rec.r_refcount);
21511823cb0bSTao Ma 		BUG_ON(r_count == 0);
21526ae23c55STao Ma 		if (!delete)
21536ae23c55STao Ma 			BUG_ON(r_count > 1);
21541823cb0bSTao Ma 
21551823cb0bSTao Ma 		r_len = min((u64)(cpos + len), le64_to_cpu(rec.r_cpos) +
21561823cb0bSTao Ma 			      le32_to_cpu(rec.r_clusters)) - cpos;
21571823cb0bSTao Ma 
21581823cb0bSTao Ma 		ret = ocfs2_decrease_refcount_rec(handle, ci, ref_root_bh,
21591823cb0bSTao Ma 						  ref_leaf_bh, index,
21601823cb0bSTao Ma 						  cpos, r_len,
21611823cb0bSTao Ma 						  meta_ac, dealloc);
21621823cb0bSTao Ma 		if (ret) {
21631823cb0bSTao Ma 			mlog_errno(ret);
21641823cb0bSTao Ma 			goto out;
21651823cb0bSTao Ma 		}
21661823cb0bSTao Ma 
21676ae23c55STao Ma 		if (le32_to_cpu(rec.r_refcount) == 1 && delete) {
21681823cb0bSTao Ma 			ret = ocfs2_cache_cluster_dealloc(dealloc,
21691823cb0bSTao Ma 					  ocfs2_clusters_to_blocks(sb, cpos),
21701823cb0bSTao Ma 							  r_len);
21711823cb0bSTao Ma 			if (ret) {
21721823cb0bSTao Ma 				mlog_errno(ret);
21731823cb0bSTao Ma 				goto out;
21741823cb0bSTao Ma 			}
21751823cb0bSTao Ma 		}
21761823cb0bSTao Ma 
21771823cb0bSTao Ma 		cpos += r_len;
21781823cb0bSTao Ma 		len -= r_len;
21791823cb0bSTao Ma 		brelse(ref_leaf_bh);
21801823cb0bSTao Ma 		ref_leaf_bh = NULL;
21811823cb0bSTao Ma 	}
21821823cb0bSTao Ma 
21831823cb0bSTao Ma out:
21841823cb0bSTao Ma 	brelse(ref_leaf_bh);
21851823cb0bSTao Ma 	return ret;
21861823cb0bSTao Ma }
21871823cb0bSTao Ma 
21881823cb0bSTao Ma /* Caller must hold refcount tree lock. */
21891823cb0bSTao Ma int ocfs2_decrease_refcount(struct inode *inode,
21901823cb0bSTao Ma 			    handle_t *handle, u32 cpos, u32 len,
21911823cb0bSTao Ma 			    struct ocfs2_alloc_context *meta_ac,
21926ae23c55STao Ma 			    struct ocfs2_cached_dealloc_ctxt *dealloc,
21936ae23c55STao Ma 			    int delete)
21941823cb0bSTao Ma {
21951823cb0bSTao Ma 	int ret;
21961823cb0bSTao Ma 	u64 ref_blkno;
21971823cb0bSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
21981823cb0bSTao Ma 	struct buffer_head *ref_root_bh = NULL;
21991823cb0bSTao Ma 	struct ocfs2_refcount_tree *tree;
22001823cb0bSTao Ma 
22011823cb0bSTao Ma 	BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
22021823cb0bSTao Ma 
22031823cb0bSTao Ma 	ret = ocfs2_get_refcount_block(inode, &ref_blkno);
22041823cb0bSTao Ma 	if (ret) {
22051823cb0bSTao Ma 		mlog_errno(ret);
22061823cb0bSTao Ma 		goto out;
22071823cb0bSTao Ma 	}
22081823cb0bSTao Ma 
22091823cb0bSTao Ma 	ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno, &tree);
22101823cb0bSTao Ma 	if (ret) {
22111823cb0bSTao Ma 		mlog_errno(ret);
22121823cb0bSTao Ma 		goto out;
22131823cb0bSTao Ma 	}
22141823cb0bSTao Ma 
22151823cb0bSTao Ma 	ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
22161823cb0bSTao Ma 					&ref_root_bh);
22171823cb0bSTao Ma 	if (ret) {
22181823cb0bSTao Ma 		mlog_errno(ret);
22191823cb0bSTao Ma 		goto out;
22201823cb0bSTao Ma 	}
22211823cb0bSTao Ma 
22221823cb0bSTao Ma 	ret = __ocfs2_decrease_refcount(handle, &tree->rf_ci, ref_root_bh,
22236ae23c55STao Ma 					cpos, len, meta_ac, dealloc, delete);
22241823cb0bSTao Ma 	if (ret)
22251823cb0bSTao Ma 		mlog_errno(ret);
22261823cb0bSTao Ma out:
22271823cb0bSTao Ma 	brelse(ref_root_bh);
22281823cb0bSTao Ma 	return ret;
22291823cb0bSTao Ma }
22301aa75feaSTao Ma 
22311aa75feaSTao Ma /*
22321aa75feaSTao Ma  * Mark the already-existing extent at cpos as refcounted for len clusters.
22331aa75feaSTao Ma  * This adds the refcount extent flag.
22341aa75feaSTao Ma  *
22351aa75feaSTao Ma  * If the existing extent is larger than the request, initiate a
22361aa75feaSTao Ma  * split. An attempt will be made at merging with adjacent extents.
22371aa75feaSTao Ma  *
22381aa75feaSTao Ma  * The caller is responsible for passing down meta_ac if we'll need it.
22391aa75feaSTao Ma  */
22401aa75feaSTao Ma static int ocfs2_mark_extent_refcounted(struct inode *inode,
22411aa75feaSTao Ma 				struct ocfs2_extent_tree *et,
22421aa75feaSTao Ma 				handle_t *handle, u32 cpos,
22431aa75feaSTao Ma 				u32 len, u32 phys,
22441aa75feaSTao Ma 				struct ocfs2_alloc_context *meta_ac,
22451aa75feaSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
22461aa75feaSTao Ma {
22471aa75feaSTao Ma 	int ret;
22481aa75feaSTao Ma 
22491aa75feaSTao Ma 	mlog(0, "Inode %lu refcount tree cpos %u, len %u, phys cluster %u\n",
22501aa75feaSTao Ma 	     inode->i_ino, cpos, len, phys);
22511aa75feaSTao Ma 
22521aa75feaSTao Ma 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
22531aa75feaSTao Ma 		ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
22541aa75feaSTao Ma 			    "tree, but the feature bit is not set in the "
22551aa75feaSTao Ma 			    "super block.", inode->i_ino);
22561aa75feaSTao Ma 		ret = -EROFS;
22571aa75feaSTao Ma 		goto out;
22581aa75feaSTao Ma 	}
22591aa75feaSTao Ma 
22601aa75feaSTao Ma 	ret = ocfs2_change_extent_flag(handle, et, cpos,
22611aa75feaSTao Ma 				       len, phys, meta_ac, dealloc,
22621aa75feaSTao Ma 				       OCFS2_EXT_REFCOUNTED, 0);
22631aa75feaSTao Ma 	if (ret)
22641aa75feaSTao Ma 		mlog_errno(ret);
22651aa75feaSTao Ma 
22661aa75feaSTao Ma out:
22671aa75feaSTao Ma 	return ret;
22681aa75feaSTao Ma }
2269bcbbb24aSTao Ma 
2270bcbbb24aSTao Ma /*
2271bcbbb24aSTao Ma  * Given some contiguous physical clusters, calculate what we need
2272bcbbb24aSTao Ma  * for modifying their refcount.
2273bcbbb24aSTao Ma  */
2274bcbbb24aSTao Ma static int ocfs2_calc_refcount_meta_credits(struct super_block *sb,
2275bcbbb24aSTao Ma 					    struct ocfs2_caching_info *ci,
2276bcbbb24aSTao Ma 					    struct buffer_head *ref_root_bh,
2277bcbbb24aSTao Ma 					    u64 start_cpos,
2278bcbbb24aSTao Ma 					    u32 clusters,
2279bcbbb24aSTao Ma 					    int *meta_add,
2280bcbbb24aSTao Ma 					    int *credits)
2281bcbbb24aSTao Ma {
2282bcbbb24aSTao Ma 	int ret = 0, index, ref_blocks = 0, recs_add = 0;
2283bcbbb24aSTao Ma 	u64 cpos = start_cpos;
2284bcbbb24aSTao Ma 	struct ocfs2_refcount_block *rb;
2285bcbbb24aSTao Ma 	struct ocfs2_refcount_rec rec;
2286bcbbb24aSTao Ma 	struct buffer_head *ref_leaf_bh = NULL, *prev_bh = NULL;
2287bcbbb24aSTao Ma 	u32 len;
2288bcbbb24aSTao Ma 
2289bcbbb24aSTao Ma 	mlog(0, "start_cpos %llu, clusters %u\n",
2290bcbbb24aSTao Ma 	     (unsigned long long)start_cpos, clusters);
2291bcbbb24aSTao Ma 	while (clusters) {
2292bcbbb24aSTao Ma 		ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
2293bcbbb24aSTao Ma 					     cpos, clusters, &rec,
2294bcbbb24aSTao Ma 					     &index, &ref_leaf_bh);
2295bcbbb24aSTao Ma 		if (ret) {
2296bcbbb24aSTao Ma 			mlog_errno(ret);
2297bcbbb24aSTao Ma 			goto out;
2298bcbbb24aSTao Ma 		}
2299bcbbb24aSTao Ma 
2300bcbbb24aSTao Ma 		if (ref_leaf_bh != prev_bh) {
2301bcbbb24aSTao Ma 			/*
2302bcbbb24aSTao Ma 			 * Now we encounter a new leaf block, so calculate
2303bcbbb24aSTao Ma 			 * whether we need to extend the old leaf.
2304bcbbb24aSTao Ma 			 */
2305bcbbb24aSTao Ma 			if (prev_bh) {
2306bcbbb24aSTao Ma 				rb = (struct ocfs2_refcount_block *)
2307bcbbb24aSTao Ma 							prev_bh->b_data;
2308bcbbb24aSTao Ma 
2309bcbbb24aSTao Ma 				if (le64_to_cpu(rb->rf_records.rl_used) +
2310bcbbb24aSTao Ma 				    recs_add >
2311bcbbb24aSTao Ma 				    le16_to_cpu(rb->rf_records.rl_count))
2312bcbbb24aSTao Ma 					ref_blocks++;
2313bcbbb24aSTao Ma 			}
2314bcbbb24aSTao Ma 
2315bcbbb24aSTao Ma 			recs_add = 0;
2316bcbbb24aSTao Ma 			*credits += 1;
2317bcbbb24aSTao Ma 			brelse(prev_bh);
2318bcbbb24aSTao Ma 			prev_bh = ref_leaf_bh;
2319bcbbb24aSTao Ma 			get_bh(prev_bh);
2320bcbbb24aSTao Ma 		}
2321bcbbb24aSTao Ma 
2322bcbbb24aSTao Ma 		rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
2323bcbbb24aSTao Ma 
2324bcbbb24aSTao Ma 		mlog(0, "recs_add %d,cpos %llu, clusters %u, rec->r_cpos %llu,"
2325bcbbb24aSTao Ma 		     "rec->r_clusters %u, rec->r_refcount %u, index %d\n",
2326bcbbb24aSTao Ma 		     recs_add, (unsigned long long)cpos, clusters,
2327bcbbb24aSTao Ma 		     (unsigned long long)le64_to_cpu(rec.r_cpos),
2328bcbbb24aSTao Ma 		     le32_to_cpu(rec.r_clusters),
2329bcbbb24aSTao Ma 		     le32_to_cpu(rec.r_refcount), index);
2330bcbbb24aSTao Ma 
2331bcbbb24aSTao Ma 		len = min((u64)cpos + clusters, le64_to_cpu(rec.r_cpos) +
2332bcbbb24aSTao Ma 			  le32_to_cpu(rec.r_clusters)) - cpos;
2333bcbbb24aSTao Ma 		/*
2334bcbbb24aSTao Ma 		 * If the refcount rec already exist, cool. We just need
2335bcbbb24aSTao Ma 		 * to check whether there is a split. Otherwise we just need
2336bcbbb24aSTao Ma 		 * to increase the refcount.
2337bcbbb24aSTao Ma 		 * If we will insert one, increases recs_add.
2338bcbbb24aSTao Ma 		 *
2339bcbbb24aSTao Ma 		 * We record all the records which will be inserted to the
2340bcbbb24aSTao Ma 		 * same refcount block, so that we can tell exactly whether
2341bcbbb24aSTao Ma 		 * we need a new refcount block or not.
2342bcbbb24aSTao Ma 		 */
2343bcbbb24aSTao Ma 		if (rec.r_refcount) {
2344bcbbb24aSTao Ma 			/* Check whether we need a split at the beginning. */
2345bcbbb24aSTao Ma 			if (cpos == start_cpos &&
2346bcbbb24aSTao Ma 			    cpos != le64_to_cpu(rec.r_cpos))
2347bcbbb24aSTao Ma 				recs_add++;
2348bcbbb24aSTao Ma 
2349bcbbb24aSTao Ma 			/* Check whether we need a split in the end. */
2350bcbbb24aSTao Ma 			if (cpos + clusters < le64_to_cpu(rec.r_cpos) +
2351bcbbb24aSTao Ma 			    le32_to_cpu(rec.r_clusters))
2352bcbbb24aSTao Ma 				recs_add++;
2353bcbbb24aSTao Ma 		} else
2354bcbbb24aSTao Ma 			recs_add++;
2355bcbbb24aSTao Ma 
2356bcbbb24aSTao Ma 		brelse(ref_leaf_bh);
2357bcbbb24aSTao Ma 		ref_leaf_bh = NULL;
2358bcbbb24aSTao Ma 		clusters -= len;
2359bcbbb24aSTao Ma 		cpos += len;
2360bcbbb24aSTao Ma 	}
2361bcbbb24aSTao Ma 
2362bcbbb24aSTao Ma 	if (prev_bh) {
2363bcbbb24aSTao Ma 		rb = (struct ocfs2_refcount_block *)prev_bh->b_data;
2364bcbbb24aSTao Ma 
2365bcbbb24aSTao Ma 		if (le64_to_cpu(rb->rf_records.rl_used) + recs_add >
2366bcbbb24aSTao Ma 		    le16_to_cpu(rb->rf_records.rl_count))
2367bcbbb24aSTao Ma 			ref_blocks++;
2368bcbbb24aSTao Ma 
2369bcbbb24aSTao Ma 		*credits += 1;
2370bcbbb24aSTao Ma 	}
2371bcbbb24aSTao Ma 
2372bcbbb24aSTao Ma 	if (!ref_blocks)
2373bcbbb24aSTao Ma 		goto out;
2374bcbbb24aSTao Ma 
2375bcbbb24aSTao Ma 	mlog(0, "we need ref_blocks %d\n", ref_blocks);
2376bcbbb24aSTao Ma 	*meta_add += ref_blocks;
2377bcbbb24aSTao Ma 	*credits += ref_blocks;
2378bcbbb24aSTao Ma 
2379bcbbb24aSTao Ma 	/*
2380bcbbb24aSTao Ma 	 * So we may need ref_blocks to insert into the tree.
2381bcbbb24aSTao Ma 	 * That also means we need to change the b-tree and add that number
2382bcbbb24aSTao Ma 	 * of records since we never merge them.
2383bcbbb24aSTao Ma 	 * We need one more block for expansion since the new created leaf
2384bcbbb24aSTao Ma 	 * block is also full and needs split.
2385bcbbb24aSTao Ma 	 */
2386bcbbb24aSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
2387bcbbb24aSTao Ma 	if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) {
2388bcbbb24aSTao Ma 		struct ocfs2_extent_tree et;
2389bcbbb24aSTao Ma 
2390bcbbb24aSTao Ma 		ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
2391bcbbb24aSTao Ma 		*meta_add += ocfs2_extend_meta_needed(et.et_root_el);
2392bcbbb24aSTao Ma 		*credits += ocfs2_calc_extend_credits(sb,
2393bcbbb24aSTao Ma 						      et.et_root_el,
2394bcbbb24aSTao Ma 						      ref_blocks);
2395bcbbb24aSTao Ma 	} else {
2396bcbbb24aSTao Ma 		*credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
2397bcbbb24aSTao Ma 		*meta_add += 1;
2398bcbbb24aSTao Ma 	}
2399bcbbb24aSTao Ma 
2400bcbbb24aSTao Ma out:
2401bcbbb24aSTao Ma 	brelse(ref_leaf_bh);
2402bcbbb24aSTao Ma 	brelse(prev_bh);
2403bcbbb24aSTao Ma 	return ret;
2404bcbbb24aSTao Ma }
2405bcbbb24aSTao Ma 
2406bcbbb24aSTao Ma /*
2407bcbbb24aSTao Ma  * For refcount tree, we will decrease some contiguous clusters
2408bcbbb24aSTao Ma  * refcount count, so just go through it to see how many blocks
2409bcbbb24aSTao Ma  * we gonna touch and whether we need to create new blocks.
2410bcbbb24aSTao Ma  *
2411bcbbb24aSTao Ma  * Normally the refcount blocks store these refcount should be
2412bcbbb24aSTao Ma  * continguous also, so that we can get the number easily.
2413bcbbb24aSTao Ma  * As for meta_ac, we will at most add split 2 refcount record and
2414bcbbb24aSTao Ma  * 2 more refcount block, so just check it in a rough way.
2415bcbbb24aSTao Ma  *
2416bcbbb24aSTao Ma  * Caller must hold refcount tree lock.
2417bcbbb24aSTao Ma  */
2418bcbbb24aSTao Ma int ocfs2_prepare_refcount_change_for_del(struct inode *inode,
2419bcbbb24aSTao Ma 					  struct buffer_head *di_bh,
2420bcbbb24aSTao Ma 					  u64 phys_blkno,
2421bcbbb24aSTao Ma 					  u32 clusters,
2422bcbbb24aSTao Ma 					  int *credits,
2423bcbbb24aSTao Ma 					  struct ocfs2_alloc_context **meta_ac)
2424bcbbb24aSTao Ma {
2425bcbbb24aSTao Ma 	int ret, ref_blocks = 0;
2426bcbbb24aSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
2427bcbbb24aSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
2428bcbbb24aSTao Ma 	struct buffer_head *ref_root_bh = NULL;
2429bcbbb24aSTao Ma 	struct ocfs2_refcount_tree *tree;
2430bcbbb24aSTao Ma 	u64 start_cpos = ocfs2_blocks_to_clusters(inode->i_sb, phys_blkno);
2431bcbbb24aSTao Ma 
2432bcbbb24aSTao Ma 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
2433bcbbb24aSTao Ma 		ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
2434bcbbb24aSTao Ma 			    "tree, but the feature bit is not set in the "
2435bcbbb24aSTao Ma 			    "super block.", inode->i_ino);
2436bcbbb24aSTao Ma 		ret = -EROFS;
2437bcbbb24aSTao Ma 		goto out;
2438bcbbb24aSTao Ma 	}
2439bcbbb24aSTao Ma 
2440bcbbb24aSTao Ma 	BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
2441bcbbb24aSTao Ma 
2442bcbbb24aSTao Ma 	ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb),
2443bcbbb24aSTao Ma 				      le64_to_cpu(di->i_refcount_loc), &tree);
2444bcbbb24aSTao Ma 	if (ret) {
2445bcbbb24aSTao Ma 		mlog_errno(ret);
2446bcbbb24aSTao Ma 		goto out;
2447bcbbb24aSTao Ma 	}
2448bcbbb24aSTao Ma 
2449bcbbb24aSTao Ma 	ret = ocfs2_read_refcount_block(&tree->rf_ci,
2450bcbbb24aSTao Ma 					le64_to_cpu(di->i_refcount_loc),
2451bcbbb24aSTao Ma 					&ref_root_bh);
2452bcbbb24aSTao Ma 	if (ret) {
2453bcbbb24aSTao Ma 		mlog_errno(ret);
2454bcbbb24aSTao Ma 		goto out;
2455bcbbb24aSTao Ma 	}
2456bcbbb24aSTao Ma 
2457bcbbb24aSTao Ma 	ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
2458bcbbb24aSTao Ma 					       &tree->rf_ci,
2459bcbbb24aSTao Ma 					       ref_root_bh,
2460bcbbb24aSTao Ma 					       start_cpos, clusters,
2461bcbbb24aSTao Ma 					       &ref_blocks, credits);
2462bcbbb24aSTao Ma 	if (ret) {
2463bcbbb24aSTao Ma 		mlog_errno(ret);
2464bcbbb24aSTao Ma 		goto out;
2465bcbbb24aSTao Ma 	}
2466bcbbb24aSTao Ma 
2467bcbbb24aSTao Ma 	mlog(0, "reserve new metadata %d, credits = %d\n",
2468bcbbb24aSTao Ma 	     ref_blocks, *credits);
2469bcbbb24aSTao Ma 
2470bcbbb24aSTao Ma 	if (ref_blocks) {
2471bcbbb24aSTao Ma 		ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
2472bcbbb24aSTao Ma 							ref_blocks, meta_ac);
2473bcbbb24aSTao Ma 		if (ret)
2474bcbbb24aSTao Ma 			mlog_errno(ret);
2475bcbbb24aSTao Ma 	}
2476bcbbb24aSTao Ma 
2477bcbbb24aSTao Ma out:
2478bcbbb24aSTao Ma 	brelse(ref_root_bh);
2479bcbbb24aSTao Ma 	return ret;
2480bcbbb24aSTao Ma }
24816f70fa51STao Ma 
24826f70fa51STao Ma #define	MAX_CONTIG_BYTES	1048576
24836f70fa51STao Ma 
24846f70fa51STao Ma static inline unsigned int ocfs2_cow_contig_clusters(struct super_block *sb)
24856f70fa51STao Ma {
24866f70fa51STao Ma 	return ocfs2_clusters_for_bytes(sb, MAX_CONTIG_BYTES);
24876f70fa51STao Ma }
24886f70fa51STao Ma 
24896f70fa51STao Ma static inline unsigned int ocfs2_cow_contig_mask(struct super_block *sb)
24906f70fa51STao Ma {
24916f70fa51STao Ma 	return ~(ocfs2_cow_contig_clusters(sb) - 1);
24926f70fa51STao Ma }
24936f70fa51STao Ma 
24946f70fa51STao Ma /*
24956f70fa51STao Ma  * Given an extent that starts at 'start' and an I/O that starts at 'cpos',
24966f70fa51STao Ma  * find an offset (start + (n * contig_clusters)) that is closest to cpos
24976f70fa51STao Ma  * while still being less than or equal to it.
24986f70fa51STao Ma  *
24996f70fa51STao Ma  * The goal is to break the extent at a multiple of contig_clusters.
25006f70fa51STao Ma  */
25016f70fa51STao Ma static inline unsigned int ocfs2_cow_align_start(struct super_block *sb,
25026f70fa51STao Ma 						 unsigned int start,
25036f70fa51STao Ma 						 unsigned int cpos)
25046f70fa51STao Ma {
25056f70fa51STao Ma 	BUG_ON(start > cpos);
25066f70fa51STao Ma 
25076f70fa51STao Ma 	return start + ((cpos - start) & ocfs2_cow_contig_mask(sb));
25086f70fa51STao Ma }
25096f70fa51STao Ma 
25106f70fa51STao Ma /*
25116f70fa51STao Ma  * Given a cluster count of len, pad it out so that it is a multiple
25126f70fa51STao Ma  * of contig_clusters.
25136f70fa51STao Ma  */
25146f70fa51STao Ma static inline unsigned int ocfs2_cow_align_length(struct super_block *sb,
25156f70fa51STao Ma 						  unsigned int len)
25166f70fa51STao Ma {
25176f70fa51STao Ma 	unsigned int padded =
25186f70fa51STao Ma 		(len + (ocfs2_cow_contig_clusters(sb) - 1)) &
25196f70fa51STao Ma 		ocfs2_cow_contig_mask(sb);
25206f70fa51STao Ma 
25216f70fa51STao Ma 	/* Did we wrap? */
25226f70fa51STao Ma 	if (padded < len)
25236f70fa51STao Ma 		padded = UINT_MAX;
25246f70fa51STao Ma 
25256f70fa51STao Ma 	return padded;
25266f70fa51STao Ma }
25276f70fa51STao Ma 
25286f70fa51STao Ma /*
25296f70fa51STao Ma  * Calculate out the start and number of virtual clusters we need to to CoW.
25306f70fa51STao Ma  *
25316f70fa51STao Ma  * cpos is vitual start cluster position we want to do CoW in a
25326f70fa51STao Ma  * file and write_len is the cluster length.
253337f8a2bfSTao Ma  * max_cpos is the place where we want to stop CoW intentionally.
25346f70fa51STao Ma  *
25356f70fa51STao Ma  * Normal we will start CoW from the beginning of extent record cotaining cpos.
25366f70fa51STao Ma  * We try to break up extents on boundaries of MAX_CONTIG_BYTES so that we
25376f70fa51STao Ma  * get good I/O from the resulting extent tree.
25386f70fa51STao Ma  */
25396f70fa51STao Ma static int ocfs2_refcount_cal_cow_clusters(struct inode *inode,
2540913580b4STao Ma 					   struct ocfs2_extent_list *el,
25416f70fa51STao Ma 					   u32 cpos,
25426f70fa51STao Ma 					   u32 write_len,
254337f8a2bfSTao Ma 					   u32 max_cpos,
25446f70fa51STao Ma 					   u32 *cow_start,
25456f70fa51STao Ma 					   u32 *cow_len)
25466f70fa51STao Ma {
25476f70fa51STao Ma 	int ret = 0;
25486f70fa51STao Ma 	int tree_height = le16_to_cpu(el->l_tree_depth), i;
25496f70fa51STao Ma 	struct buffer_head *eb_bh = NULL;
25506f70fa51STao Ma 	struct ocfs2_extent_block *eb = NULL;
25516f70fa51STao Ma 	struct ocfs2_extent_rec *rec;
25526f70fa51STao Ma 	unsigned int want_clusters, rec_end = 0;
25536f70fa51STao Ma 	int contig_clusters = ocfs2_cow_contig_clusters(inode->i_sb);
25546f70fa51STao Ma 	int leaf_clusters;
25556f70fa51STao Ma 
255637f8a2bfSTao Ma 	BUG_ON(cpos + write_len > max_cpos);
255737f8a2bfSTao Ma 
25586f70fa51STao Ma 	if (tree_height > 0) {
25596f70fa51STao Ma 		ret = ocfs2_find_leaf(INODE_CACHE(inode), el, cpos, &eb_bh);
25606f70fa51STao Ma 		if (ret) {
25616f70fa51STao Ma 			mlog_errno(ret);
25626f70fa51STao Ma 			goto out;
25636f70fa51STao Ma 		}
25646f70fa51STao Ma 
25656f70fa51STao Ma 		eb = (struct ocfs2_extent_block *) eb_bh->b_data;
25666f70fa51STao Ma 		el = &eb->h_list;
25676f70fa51STao Ma 
25686f70fa51STao Ma 		if (el->l_tree_depth) {
25696f70fa51STao Ma 			ocfs2_error(inode->i_sb,
25706f70fa51STao Ma 				    "Inode %lu has non zero tree depth in "
25716f70fa51STao Ma 				    "leaf block %llu\n", inode->i_ino,
25726f70fa51STao Ma 				    (unsigned long long)eb_bh->b_blocknr);
25736f70fa51STao Ma 			ret = -EROFS;
25746f70fa51STao Ma 			goto out;
25756f70fa51STao Ma 		}
25766f70fa51STao Ma 	}
25776f70fa51STao Ma 
25786f70fa51STao Ma 	*cow_len = 0;
25796f70fa51STao Ma 	for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
25806f70fa51STao Ma 		rec = &el->l_recs[i];
25816f70fa51STao Ma 
25826f70fa51STao Ma 		if (ocfs2_is_empty_extent(rec)) {
25836f70fa51STao Ma 			mlog_bug_on_msg(i != 0, "Inode %lu has empty record in "
25846f70fa51STao Ma 					"index %d\n", inode->i_ino, i);
25856f70fa51STao Ma 			continue;
25866f70fa51STao Ma 		}
25876f70fa51STao Ma 
25886f70fa51STao Ma 		if (le32_to_cpu(rec->e_cpos) +
25896f70fa51STao Ma 		    le16_to_cpu(rec->e_leaf_clusters) <= cpos)
25906f70fa51STao Ma 			continue;
25916f70fa51STao Ma 
25926f70fa51STao Ma 		if (*cow_len == 0) {
25936f70fa51STao Ma 			/*
25946f70fa51STao Ma 			 * We should find a refcounted record in the
25956f70fa51STao Ma 			 * first pass.
25966f70fa51STao Ma 			 */
25976f70fa51STao Ma 			BUG_ON(!(rec->e_flags & OCFS2_EXT_REFCOUNTED));
25986f70fa51STao Ma 			*cow_start = le32_to_cpu(rec->e_cpos);
25996f70fa51STao Ma 		}
26006f70fa51STao Ma 
26016f70fa51STao Ma 		/*
260237f8a2bfSTao Ma 		 * If we encounter a hole, a non-refcounted record or
260337f8a2bfSTao Ma 		 * pass the max_cpos, stop the search.
26046f70fa51STao Ma 		 */
26056f70fa51STao Ma 		if ((!(rec->e_flags & OCFS2_EXT_REFCOUNTED)) ||
260637f8a2bfSTao Ma 		    (*cow_len && rec_end != le32_to_cpu(rec->e_cpos)) ||
260737f8a2bfSTao Ma 		    (max_cpos <= le32_to_cpu(rec->e_cpos)))
26086f70fa51STao Ma 			break;
26096f70fa51STao Ma 
26106f70fa51STao Ma 		leaf_clusters = le16_to_cpu(rec->e_leaf_clusters);
26116f70fa51STao Ma 		rec_end = le32_to_cpu(rec->e_cpos) + leaf_clusters;
261237f8a2bfSTao Ma 		if (rec_end > max_cpos) {
261337f8a2bfSTao Ma 			rec_end = max_cpos;
261437f8a2bfSTao Ma 			leaf_clusters = rec_end - le32_to_cpu(rec->e_cpos);
261537f8a2bfSTao Ma 		}
26166f70fa51STao Ma 
26176f70fa51STao Ma 		/*
26186f70fa51STao Ma 		 * How many clusters do we actually need from
26196f70fa51STao Ma 		 * this extent?  First we see how many we actually
26206f70fa51STao Ma 		 * need to complete the write.  If that's smaller
26216f70fa51STao Ma 		 * than contig_clusters, we try for contig_clusters.
26226f70fa51STao Ma 		 */
26236f70fa51STao Ma 		if (!*cow_len)
26246f70fa51STao Ma 			want_clusters = write_len;
26256f70fa51STao Ma 		else
26266f70fa51STao Ma 			want_clusters = (cpos + write_len) -
26276f70fa51STao Ma 				(*cow_start + *cow_len);
26286f70fa51STao Ma 		if (want_clusters < contig_clusters)
26296f70fa51STao Ma 			want_clusters = contig_clusters;
26306f70fa51STao Ma 
26316f70fa51STao Ma 		/*
26326f70fa51STao Ma 		 * If the write does not cover the whole extent, we
26336f70fa51STao Ma 		 * need to calculate how we're going to split the extent.
26346f70fa51STao Ma 		 * We try to do it on contig_clusters boundaries.
26356f70fa51STao Ma 		 *
26366f70fa51STao Ma 		 * Any extent smaller than contig_clusters will be
26376f70fa51STao Ma 		 * CoWed in its entirety.
26386f70fa51STao Ma 		 */
26396f70fa51STao Ma 		if (leaf_clusters <= contig_clusters)
26406f70fa51STao Ma 			*cow_len += leaf_clusters;
26416f70fa51STao Ma 		else if (*cow_len || (*cow_start == cpos)) {
26426f70fa51STao Ma 			/*
26436f70fa51STao Ma 			 * This extent needs to be CoW'd from its
26446f70fa51STao Ma 			 * beginning, so all we have to do is compute
26456f70fa51STao Ma 			 * how many clusters to grab.  We align
26466f70fa51STao Ma 			 * want_clusters to the edge of contig_clusters
26476f70fa51STao Ma 			 * to get better I/O.
26486f70fa51STao Ma 			 */
26496f70fa51STao Ma 			want_clusters = ocfs2_cow_align_length(inode->i_sb,
26506f70fa51STao Ma 							       want_clusters);
26516f70fa51STao Ma 
26526f70fa51STao Ma 			if (leaf_clusters < want_clusters)
26536f70fa51STao Ma 				*cow_len += leaf_clusters;
26546f70fa51STao Ma 			else
26556f70fa51STao Ma 				*cow_len += want_clusters;
26566f70fa51STao Ma 		} else if ((*cow_start + contig_clusters) >=
26576f70fa51STao Ma 			   (cpos + write_len)) {
26586f70fa51STao Ma 			/*
26596f70fa51STao Ma 			 * Breaking off contig_clusters at the front
26606f70fa51STao Ma 			 * of the extent will cover our write.  That's
26616f70fa51STao Ma 			 * easy.
26626f70fa51STao Ma 			 */
26636f70fa51STao Ma 			*cow_len = contig_clusters;
26646f70fa51STao Ma 		} else if ((rec_end - cpos) <= contig_clusters) {
26656f70fa51STao Ma 			/*
26666f70fa51STao Ma 			 * Breaking off contig_clusters at the tail of
26676f70fa51STao Ma 			 * this extent will cover cpos.
26686f70fa51STao Ma 			 */
26696f70fa51STao Ma 			*cow_start = rec_end - contig_clusters;
26706f70fa51STao Ma 			*cow_len = contig_clusters;
26716f70fa51STao Ma 		} else if ((rec_end - cpos) <= want_clusters) {
26726f70fa51STao Ma 			/*
26736f70fa51STao Ma 			 * While we can't fit the entire write in this
26746f70fa51STao Ma 			 * extent, we know that the write goes from cpos
26756f70fa51STao Ma 			 * to the end of the extent.  Break that off.
26766f70fa51STao Ma 			 * We try to break it at some multiple of
26776f70fa51STao Ma 			 * contig_clusters from the front of the extent.
26786f70fa51STao Ma 			 * Failing that (ie, cpos is within
26796f70fa51STao Ma 			 * contig_clusters of the front), we'll CoW the
26806f70fa51STao Ma 			 * entire extent.
26816f70fa51STao Ma 			 */
26826f70fa51STao Ma 			*cow_start = ocfs2_cow_align_start(inode->i_sb,
26836f70fa51STao Ma 							   *cow_start, cpos);
26846f70fa51STao Ma 			*cow_len = rec_end - *cow_start;
26856f70fa51STao Ma 		} else {
26866f70fa51STao Ma 			/*
26876f70fa51STao Ma 			 * Ok, the entire write lives in the middle of
26886f70fa51STao Ma 			 * this extent.  Let's try to slice the extent up
26896f70fa51STao Ma 			 * nicely.  Optimally, our CoW region starts at
26906f70fa51STao Ma 			 * m*contig_clusters from the beginning of the
26916f70fa51STao Ma 			 * extent and goes for n*contig_clusters,
26926f70fa51STao Ma 			 * covering the entire write.
26936f70fa51STao Ma 			 */
26946f70fa51STao Ma 			*cow_start = ocfs2_cow_align_start(inode->i_sb,
26956f70fa51STao Ma 							   *cow_start, cpos);
26966f70fa51STao Ma 
26976f70fa51STao Ma 			want_clusters = (cpos + write_len) - *cow_start;
26986f70fa51STao Ma 			want_clusters = ocfs2_cow_align_length(inode->i_sb,
26996f70fa51STao Ma 							       want_clusters);
27006f70fa51STao Ma 			if (*cow_start + want_clusters <= rec_end)
27016f70fa51STao Ma 				*cow_len = want_clusters;
27026f70fa51STao Ma 			else
27036f70fa51STao Ma 				*cow_len = rec_end - *cow_start;
27046f70fa51STao Ma 		}
27056f70fa51STao Ma 
27066f70fa51STao Ma 		/* Have we covered our entire write yet? */
27076f70fa51STao Ma 		if ((*cow_start + *cow_len) >= (cpos + write_len))
27086f70fa51STao Ma 			break;
27096f70fa51STao Ma 
27106f70fa51STao Ma 		/*
27116f70fa51STao Ma 		 * If we reach the end of the extent block and don't get enough
27126f70fa51STao Ma 		 * clusters, continue with the next extent block if possible.
27136f70fa51STao Ma 		 */
27146f70fa51STao Ma 		if (i + 1 == le16_to_cpu(el->l_next_free_rec) &&
27156f70fa51STao Ma 		    eb && eb->h_next_leaf_blk) {
27166f70fa51STao Ma 			brelse(eb_bh);
27176f70fa51STao Ma 			eb_bh = NULL;
27186f70fa51STao Ma 
27196f70fa51STao Ma 			ret = ocfs2_read_extent_block(INODE_CACHE(inode),
27206f70fa51STao Ma 					       le64_to_cpu(eb->h_next_leaf_blk),
27216f70fa51STao Ma 					       &eb_bh);
27226f70fa51STao Ma 			if (ret) {
27236f70fa51STao Ma 				mlog_errno(ret);
27246f70fa51STao Ma 				goto out;
27256f70fa51STao Ma 			}
27266f70fa51STao Ma 
27276f70fa51STao Ma 			eb = (struct ocfs2_extent_block *) eb_bh->b_data;
27286f70fa51STao Ma 			el = &eb->h_list;
27296f70fa51STao Ma 			i = -1;
27306f70fa51STao Ma 		}
27316f70fa51STao Ma 	}
27326f70fa51STao Ma 
27336f70fa51STao Ma out:
27346f70fa51STao Ma 	brelse(eb_bh);
27356f70fa51STao Ma 	return ret;
27366f70fa51STao Ma }
27376f70fa51STao Ma 
27386f70fa51STao Ma /*
27396f70fa51STao Ma  * Prepare meta_ac, data_ac and calculate credits when we want to add some
27406f70fa51STao Ma  * num_clusters in data_tree "et" and change the refcount for the old
27416f70fa51STao Ma  * clusters(starting form p_cluster) in the refcount tree.
27426f70fa51STao Ma  *
27436f70fa51STao Ma  * Note:
27446f70fa51STao Ma  * 1. since we may split the old tree, so we at most will need num_clusters + 2
27456f70fa51STao Ma  *    more new leaf records.
27466f70fa51STao Ma  * 2. In some case, we may not need to reserve new clusters(e.g, reflink), so
27476f70fa51STao Ma  *    just give data_ac = NULL.
27486f70fa51STao Ma  */
27496f70fa51STao Ma static int ocfs2_lock_refcount_allocators(struct super_block *sb,
27506f70fa51STao Ma 					u32 p_cluster, u32 num_clusters,
27516f70fa51STao Ma 					struct ocfs2_extent_tree *et,
27526f70fa51STao Ma 					struct ocfs2_caching_info *ref_ci,
27536f70fa51STao Ma 					struct buffer_head *ref_root_bh,
27546f70fa51STao Ma 					struct ocfs2_alloc_context **meta_ac,
27556f70fa51STao Ma 					struct ocfs2_alloc_context **data_ac,
27566f70fa51STao Ma 					int *credits)
27576f70fa51STao Ma {
27586f70fa51STao Ma 	int ret = 0, meta_add = 0;
27596f70fa51STao Ma 	int num_free_extents = ocfs2_num_free_extents(OCFS2_SB(sb), et);
27606f70fa51STao Ma 
27616f70fa51STao Ma 	if (num_free_extents < 0) {
27626f70fa51STao Ma 		ret = num_free_extents;
27636f70fa51STao Ma 		mlog_errno(ret);
27646f70fa51STao Ma 		goto out;
27656f70fa51STao Ma 	}
27666f70fa51STao Ma 
27676f70fa51STao Ma 	if (num_free_extents < num_clusters + 2)
27686f70fa51STao Ma 		meta_add =
27696f70fa51STao Ma 			ocfs2_extend_meta_needed(et->et_root_el);
27706f70fa51STao Ma 
27716f70fa51STao Ma 	*credits += ocfs2_calc_extend_credits(sb, et->et_root_el,
27726f70fa51STao Ma 					      num_clusters + 2);
27736f70fa51STao Ma 
27746f70fa51STao Ma 	ret = ocfs2_calc_refcount_meta_credits(sb, ref_ci, ref_root_bh,
27756f70fa51STao Ma 					       p_cluster, num_clusters,
27766f70fa51STao Ma 					       &meta_add, credits);
27776f70fa51STao Ma 	if (ret) {
27786f70fa51STao Ma 		mlog_errno(ret);
27796f70fa51STao Ma 		goto out;
27806f70fa51STao Ma 	}
27816f70fa51STao Ma 
27826f70fa51STao Ma 	mlog(0, "reserve new metadata %d, clusters %u, credits = %d\n",
27836f70fa51STao Ma 	     meta_add, num_clusters, *credits);
27846f70fa51STao Ma 	ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(sb), meta_add,
27856f70fa51STao Ma 						meta_ac);
27866f70fa51STao Ma 	if (ret) {
27876f70fa51STao Ma 		mlog_errno(ret);
27886f70fa51STao Ma 		goto out;
27896f70fa51STao Ma 	}
27906f70fa51STao Ma 
27916f70fa51STao Ma 	if (data_ac) {
27926f70fa51STao Ma 		ret = ocfs2_reserve_clusters(OCFS2_SB(sb), num_clusters,
27936f70fa51STao Ma 					     data_ac);
27946f70fa51STao Ma 		if (ret)
27956f70fa51STao Ma 			mlog_errno(ret);
27966f70fa51STao Ma 	}
27976f70fa51STao Ma 
27986f70fa51STao Ma out:
27996f70fa51STao Ma 	if (ret) {
28006f70fa51STao Ma 		if (*meta_ac) {
28016f70fa51STao Ma 			ocfs2_free_alloc_context(*meta_ac);
28026f70fa51STao Ma 			*meta_ac = NULL;
28036f70fa51STao Ma 		}
28046f70fa51STao Ma 	}
28056f70fa51STao Ma 
28066f70fa51STao Ma 	return ret;
28076f70fa51STao Ma }
28086f70fa51STao Ma 
28096f70fa51STao Ma static int ocfs2_clear_cow_buffer(handle_t *handle, struct buffer_head *bh)
28106f70fa51STao Ma {
28116f70fa51STao Ma 	BUG_ON(buffer_dirty(bh));
28126f70fa51STao Ma 
28136f70fa51STao Ma 	clear_buffer_mapped(bh);
28146f70fa51STao Ma 
28156f70fa51STao Ma 	return 0;
28166f70fa51STao Ma }
28176f70fa51STao Ma 
2818913580b4STao Ma static int ocfs2_duplicate_clusters_by_page(handle_t *handle,
28196f70fa51STao Ma 					    struct ocfs2_cow_context *context,
28206f70fa51STao Ma 					    u32 cpos, u32 old_cluster,
28216f70fa51STao Ma 					    u32 new_cluster, u32 new_len)
28226f70fa51STao Ma {
28236f70fa51STao Ma 	int ret = 0, partial;
2824913580b4STao Ma 	struct ocfs2_caching_info *ci = context->data_et.et_ci;
28256f70fa51STao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
28266f70fa51STao Ma 	u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
28276f70fa51STao Ma 	struct page *page;
28286f70fa51STao Ma 	pgoff_t page_index;
28296f70fa51STao Ma 	unsigned int from, to;
28306f70fa51STao Ma 	loff_t offset, end, map_end;
28316f70fa51STao Ma 	struct address_space *mapping = context->inode->i_mapping;
28326f70fa51STao Ma 
28336f70fa51STao Ma 	mlog(0, "old_cluster %u, new %u, len %u at offset %u\n", old_cluster,
28346f70fa51STao Ma 	     new_cluster, new_len, cpos);
28356f70fa51STao Ma 
28366f70fa51STao Ma 	offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
28376f70fa51STao Ma 	end = offset + (new_len << OCFS2_SB(sb)->s_clustersize_bits);
28386f70fa51STao Ma 
28396f70fa51STao Ma 	while (offset < end) {
28406f70fa51STao Ma 		page_index = offset >> PAGE_CACHE_SHIFT;
28416f70fa51STao Ma 		map_end = (page_index + 1) << PAGE_CACHE_SHIFT;
28426f70fa51STao Ma 		if (map_end > end)
28436f70fa51STao Ma 			map_end = end;
28446f70fa51STao Ma 
28456f70fa51STao Ma 		/* from, to is the offset within the page. */
28466f70fa51STao Ma 		from = offset & (PAGE_CACHE_SIZE - 1);
28476f70fa51STao Ma 		to = PAGE_CACHE_SIZE;
28486f70fa51STao Ma 		if (map_end & (PAGE_CACHE_SIZE - 1))
28496f70fa51STao Ma 			to = map_end & (PAGE_CACHE_SIZE - 1);
28506f70fa51STao Ma 
28516f70fa51STao Ma 		page = grab_cache_page(mapping, page_index);
28526f70fa51STao Ma 
28536f70fa51STao Ma 		/* This page can't be dirtied before we CoW it out. */
28546f70fa51STao Ma 		BUG_ON(PageDirty(page));
28556f70fa51STao Ma 
28566f70fa51STao Ma 		if (!PageUptodate(page)) {
28576f70fa51STao Ma 			ret = block_read_full_page(page, ocfs2_get_block);
28586f70fa51STao Ma 			if (ret) {
28596f70fa51STao Ma 				mlog_errno(ret);
28606f70fa51STao Ma 				goto unlock;
28616f70fa51STao Ma 			}
28626f70fa51STao Ma 			lock_page(page);
28636f70fa51STao Ma 		}
28646f70fa51STao Ma 
28656f70fa51STao Ma 		if (page_has_buffers(page)) {
28666f70fa51STao Ma 			ret = walk_page_buffers(handle, page_buffers(page),
28676f70fa51STao Ma 						from, to, &partial,
28686f70fa51STao Ma 						ocfs2_clear_cow_buffer);
28696f70fa51STao Ma 			if (ret) {
28706f70fa51STao Ma 				mlog_errno(ret);
28716f70fa51STao Ma 				goto unlock;
28726f70fa51STao Ma 			}
28736f70fa51STao Ma 		}
28746f70fa51STao Ma 
28756f70fa51STao Ma 		ocfs2_map_and_dirty_page(context->inode,
28766f70fa51STao Ma 					 handle, from, to,
28776f70fa51STao Ma 					 page, 0, &new_block);
28786f70fa51STao Ma 		mark_page_accessed(page);
28796f70fa51STao Ma unlock:
28806f70fa51STao Ma 		unlock_page(page);
28816f70fa51STao Ma 		page_cache_release(page);
28826f70fa51STao Ma 		page = NULL;
28836f70fa51STao Ma 		offset = map_end;
28846f70fa51STao Ma 		if (ret)
28856f70fa51STao Ma 			break;
28866f70fa51STao Ma 	}
28876f70fa51STao Ma 
28886f70fa51STao Ma 	return ret;
28896f70fa51STao Ma }
28906f70fa51STao Ma 
2891492a8a33STao Ma static int ocfs2_duplicate_clusters_by_jbd(handle_t *handle,
2892492a8a33STao Ma 					   struct ocfs2_cow_context *context,
2893492a8a33STao Ma 					   u32 cpos, u32 old_cluster,
2894492a8a33STao Ma 					   u32 new_cluster, u32 new_len)
2895492a8a33STao Ma {
2896492a8a33STao Ma 	int ret = 0;
2897492a8a33STao Ma 	struct super_block *sb = context->inode->i_sb;
2898492a8a33STao Ma 	struct ocfs2_caching_info *ci = context->data_et.et_ci;
2899492a8a33STao Ma 	int i, blocks = ocfs2_clusters_to_blocks(sb, new_len);
2900492a8a33STao Ma 	u64 old_block = ocfs2_clusters_to_blocks(sb, old_cluster);
2901492a8a33STao Ma 	u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
2902492a8a33STao Ma 	struct ocfs2_super *osb = OCFS2_SB(sb);
2903492a8a33STao Ma 	struct buffer_head *old_bh = NULL;
2904492a8a33STao Ma 	struct buffer_head *new_bh = NULL;
2905492a8a33STao Ma 
2906492a8a33STao Ma 	mlog(0, "old_cluster %u, new %u, len %u\n", old_cluster,
2907492a8a33STao Ma 	     new_cluster, new_len);
2908492a8a33STao Ma 
2909492a8a33STao Ma 	for (i = 0; i < blocks; i++, old_block++, new_block++) {
2910492a8a33STao Ma 		new_bh = sb_getblk(osb->sb, new_block);
2911492a8a33STao Ma 		if (new_bh == NULL) {
2912492a8a33STao Ma 			ret = -EIO;
2913492a8a33STao Ma 			mlog_errno(ret);
2914492a8a33STao Ma 			break;
2915492a8a33STao Ma 		}
2916492a8a33STao Ma 
2917492a8a33STao Ma 		ocfs2_set_new_buffer_uptodate(ci, new_bh);
2918492a8a33STao Ma 
2919492a8a33STao Ma 		ret = ocfs2_read_block(ci, old_block, &old_bh, NULL);
2920492a8a33STao Ma 		if (ret) {
2921492a8a33STao Ma 			mlog_errno(ret);
2922492a8a33STao Ma 			break;
2923492a8a33STao Ma 		}
2924492a8a33STao Ma 
2925492a8a33STao Ma 		ret = ocfs2_journal_access(handle, ci, new_bh,
2926492a8a33STao Ma 					   OCFS2_JOURNAL_ACCESS_CREATE);
2927492a8a33STao Ma 		if (ret) {
2928492a8a33STao Ma 			mlog_errno(ret);
2929492a8a33STao Ma 			break;
2930492a8a33STao Ma 		}
2931492a8a33STao Ma 
2932492a8a33STao Ma 		memcpy(new_bh->b_data, old_bh->b_data, sb->s_blocksize);
2933492a8a33STao Ma 		ret = ocfs2_journal_dirty(handle, new_bh);
2934492a8a33STao Ma 		if (ret) {
2935492a8a33STao Ma 			mlog_errno(ret);
2936492a8a33STao Ma 			break;
2937492a8a33STao Ma 		}
2938492a8a33STao Ma 
2939492a8a33STao Ma 		brelse(new_bh);
2940492a8a33STao Ma 		brelse(old_bh);
2941492a8a33STao Ma 		new_bh = NULL;
2942492a8a33STao Ma 		old_bh = NULL;
2943492a8a33STao Ma 	}
2944492a8a33STao Ma 
2945492a8a33STao Ma 	brelse(new_bh);
2946492a8a33STao Ma 	brelse(old_bh);
2947492a8a33STao Ma 	return ret;
2948492a8a33STao Ma }
2949492a8a33STao Ma 
29506f70fa51STao Ma static int ocfs2_clear_ext_refcount(handle_t *handle,
29516f70fa51STao Ma 				    struct ocfs2_extent_tree *et,
29526f70fa51STao Ma 				    u32 cpos, u32 p_cluster, u32 len,
29536f70fa51STao Ma 				    unsigned int ext_flags,
29546f70fa51STao Ma 				    struct ocfs2_alloc_context *meta_ac,
29556f70fa51STao Ma 				    struct ocfs2_cached_dealloc_ctxt *dealloc)
29566f70fa51STao Ma {
29576f70fa51STao Ma 	int ret, index;
29586f70fa51STao Ma 	struct ocfs2_extent_rec replace_rec;
29596f70fa51STao Ma 	struct ocfs2_path *path = NULL;
29606f70fa51STao Ma 	struct ocfs2_extent_list *el;
29616f70fa51STao Ma 	struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
29626f70fa51STao Ma 	u64 ino = ocfs2_metadata_cache_owner(et->et_ci);
29636f70fa51STao Ma 
29646f70fa51STao Ma 	mlog(0, "inode %llu cpos %u, len %u, p_cluster %u, ext_flags %u\n",
29656f70fa51STao Ma 	     (unsigned long long)ino, cpos, len, p_cluster, ext_flags);
29666f70fa51STao Ma 
29676f70fa51STao Ma 	memset(&replace_rec, 0, sizeof(replace_rec));
29686f70fa51STao Ma 	replace_rec.e_cpos = cpu_to_le32(cpos);
29696f70fa51STao Ma 	replace_rec.e_leaf_clusters = cpu_to_le16(len);
29706f70fa51STao Ma 	replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(sb,
29716f70fa51STao Ma 								   p_cluster));
29726f70fa51STao Ma 	replace_rec.e_flags = ext_flags;
29736f70fa51STao Ma 	replace_rec.e_flags &= ~OCFS2_EXT_REFCOUNTED;
29746f70fa51STao Ma 
29756f70fa51STao Ma 	path = ocfs2_new_path_from_et(et);
29766f70fa51STao Ma 	if (!path) {
29776f70fa51STao Ma 		ret = -ENOMEM;
29786f70fa51STao Ma 		mlog_errno(ret);
29796f70fa51STao Ma 		goto out;
29806f70fa51STao Ma 	}
29816f70fa51STao Ma 
29826f70fa51STao Ma 	ret = ocfs2_find_path(et->et_ci, path, cpos);
29836f70fa51STao Ma 	if (ret) {
29846f70fa51STao Ma 		mlog_errno(ret);
29856f70fa51STao Ma 		goto out;
29866f70fa51STao Ma 	}
29876f70fa51STao Ma 
29886f70fa51STao Ma 	el = path_leaf_el(path);
29896f70fa51STao Ma 
29906f70fa51STao Ma 	index = ocfs2_search_extent_list(el, cpos);
29916f70fa51STao Ma 	if (index == -1 || index >= le16_to_cpu(el->l_next_free_rec)) {
29926f70fa51STao Ma 		ocfs2_error(sb,
29936f70fa51STao Ma 			    "Inode %llu has an extent at cpos %u which can no "
29946f70fa51STao Ma 			    "longer be found.\n",
29956f70fa51STao Ma 			    (unsigned long long)ino, cpos);
29966f70fa51STao Ma 		ret = -EROFS;
29976f70fa51STao Ma 		goto out;
29986f70fa51STao Ma 	}
29996f70fa51STao Ma 
30006f70fa51STao Ma 	ret = ocfs2_split_extent(handle, et, path, index,
30016f70fa51STao Ma 				 &replace_rec, meta_ac, dealloc);
30026f70fa51STao Ma 	if (ret)
30036f70fa51STao Ma 		mlog_errno(ret);
30046f70fa51STao Ma 
30056f70fa51STao Ma out:
30066f70fa51STao Ma 	ocfs2_free_path(path);
30076f70fa51STao Ma 	return ret;
30086f70fa51STao Ma }
30096f70fa51STao Ma 
30106f70fa51STao Ma static int ocfs2_replace_clusters(handle_t *handle,
30116f70fa51STao Ma 				  struct ocfs2_cow_context *context,
30126f70fa51STao Ma 				  u32 cpos, u32 old,
30136f70fa51STao Ma 				  u32 new, u32 len,
30146f70fa51STao Ma 				  unsigned int ext_flags)
30156f70fa51STao Ma {
30166f70fa51STao Ma 	int ret;
3017913580b4STao Ma 	struct ocfs2_caching_info *ci = context->data_et.et_ci;
30186f70fa51STao Ma 	u64 ino = ocfs2_metadata_cache_owner(ci);
30196f70fa51STao Ma 
30206f70fa51STao Ma 	mlog(0, "inode %llu, cpos %u, old %u, new %u, len %u, ext_flags %u\n",
30216f70fa51STao Ma 	     (unsigned long long)ino, cpos, old, new, len, ext_flags);
30226f70fa51STao Ma 
30236f70fa51STao Ma 	/*If the old clusters is unwritten, no need to duplicate. */
30246f70fa51STao Ma 	if (!(ext_flags & OCFS2_EXT_UNWRITTEN)) {
3025913580b4STao Ma 		ret = context->cow_duplicate_clusters(handle, context, cpos,
30266f70fa51STao Ma 						      old, new, len);
30276f70fa51STao Ma 		if (ret) {
30286f70fa51STao Ma 			mlog_errno(ret);
30296f70fa51STao Ma 			goto out;
30306f70fa51STao Ma 		}
30316f70fa51STao Ma 	}
30326f70fa51STao Ma 
3033913580b4STao Ma 	ret = ocfs2_clear_ext_refcount(handle, &context->data_et,
30346f70fa51STao Ma 				       cpos, new, len, ext_flags,
30356f70fa51STao Ma 				       context->meta_ac, &context->dealloc);
30366f70fa51STao Ma 	if (ret)
30376f70fa51STao Ma 		mlog_errno(ret);
30386f70fa51STao Ma out:
30396f70fa51STao Ma 	return ret;
30406f70fa51STao Ma }
30416f70fa51STao Ma 
30426f70fa51STao Ma static int ocfs2_cow_sync_writeback(struct super_block *sb,
30436f70fa51STao Ma 				    struct ocfs2_cow_context *context,
30446f70fa51STao Ma 				    u32 cpos, u32 num_clusters)
30456f70fa51STao Ma {
30466f70fa51STao Ma 	int ret = 0;
30476f70fa51STao Ma 	loff_t offset, end, map_end;
30486f70fa51STao Ma 	pgoff_t page_index;
30496f70fa51STao Ma 	struct page *page;
30506f70fa51STao Ma 
30516f70fa51STao Ma 	if (ocfs2_should_order_data(context->inode))
30526f70fa51STao Ma 		return 0;
30536f70fa51STao Ma 
30546f70fa51STao Ma 	offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
30556f70fa51STao Ma 	end = offset + (num_clusters << OCFS2_SB(sb)->s_clustersize_bits);
30566f70fa51STao Ma 
30576f70fa51STao Ma 	ret = filemap_fdatawrite_range(context->inode->i_mapping,
30586f70fa51STao Ma 				       offset, end - 1);
30596f70fa51STao Ma 	if (ret < 0) {
30606f70fa51STao Ma 		mlog_errno(ret);
30616f70fa51STao Ma 		return ret;
30626f70fa51STao Ma 	}
30636f70fa51STao Ma 
30646f70fa51STao Ma 	while (offset < end) {
30656f70fa51STao Ma 		page_index = offset >> PAGE_CACHE_SHIFT;
30666f70fa51STao Ma 		map_end = (page_index + 1) << PAGE_CACHE_SHIFT;
30676f70fa51STao Ma 		if (map_end > end)
30686f70fa51STao Ma 			map_end = end;
30696f70fa51STao Ma 
30706f70fa51STao Ma 		page = grab_cache_page(context->inode->i_mapping, page_index);
30716f70fa51STao Ma 		BUG_ON(!page);
30726f70fa51STao Ma 
30736f70fa51STao Ma 		wait_on_page_writeback(page);
30746f70fa51STao Ma 		if (PageError(page)) {
30756f70fa51STao Ma 			ret = -EIO;
30766f70fa51STao Ma 			mlog_errno(ret);
30776f70fa51STao Ma 		} else
30786f70fa51STao Ma 			mark_page_accessed(page);
30796f70fa51STao Ma 
30806f70fa51STao Ma 		unlock_page(page);
30816f70fa51STao Ma 		page_cache_release(page);
30826f70fa51STao Ma 		page = NULL;
30836f70fa51STao Ma 		offset = map_end;
30846f70fa51STao Ma 		if (ret)
30856f70fa51STao Ma 			break;
30866f70fa51STao Ma 	}
30876f70fa51STao Ma 
30886f70fa51STao Ma 	return ret;
30896f70fa51STao Ma }
30906f70fa51STao Ma 
3091913580b4STao Ma static int ocfs2_di_get_clusters(struct ocfs2_cow_context *context,
3092913580b4STao Ma 				 u32 v_cluster, u32 *p_cluster,
3093913580b4STao Ma 				 u32 *num_clusters,
3094913580b4STao Ma 				 unsigned int *extent_flags)
3095913580b4STao Ma {
3096913580b4STao Ma 	return ocfs2_get_clusters(context->inode, v_cluster, p_cluster,
3097913580b4STao Ma 				  num_clusters, extent_flags);
3098913580b4STao Ma }
3099913580b4STao Ma 
31006f70fa51STao Ma static int ocfs2_make_clusters_writable(struct super_block *sb,
31016f70fa51STao Ma 					struct ocfs2_cow_context *context,
31026f70fa51STao Ma 					u32 cpos, u32 p_cluster,
31036f70fa51STao Ma 					u32 num_clusters, unsigned int e_flags)
31046f70fa51STao Ma {
31056ae23c55STao Ma 	int ret, delete, index, credits =  0;
31066f70fa51STao Ma 	u32 new_bit, new_len;
31076ae23c55STao Ma 	unsigned int set_len;
31086f70fa51STao Ma 	struct ocfs2_super *osb = OCFS2_SB(sb);
31096f70fa51STao Ma 	handle_t *handle;
31106ae23c55STao Ma 	struct buffer_head *ref_leaf_bh = NULL;
3111913580b4STao Ma 	struct ocfs2_caching_info *ref_ci = &context->ref_tree->rf_ci;
31126ae23c55STao Ma 	struct ocfs2_refcount_rec rec;
31136ae23c55STao Ma 
31146ae23c55STao Ma 	mlog(0, "cpos %u, p_cluster %u, num_clusters %u, e_flags %u\n",
31156ae23c55STao Ma 	     cpos, p_cluster, num_clusters, e_flags);
31166f70fa51STao Ma 
31176f70fa51STao Ma 	ret = ocfs2_lock_refcount_allocators(sb, p_cluster, num_clusters,
3118913580b4STao Ma 					     &context->data_et,
3119913580b4STao Ma 					     ref_ci,
31206f70fa51STao Ma 					     context->ref_root_bh,
31216f70fa51STao Ma 					     &context->meta_ac,
31226f70fa51STao Ma 					     &context->data_ac, &credits);
31236f70fa51STao Ma 	if (ret) {
31246f70fa51STao Ma 		mlog_errno(ret);
31256f70fa51STao Ma 		return ret;
31266f70fa51STao Ma 	}
31276f70fa51STao Ma 
3128492a8a33STao Ma 	if (context->post_refcount)
3129492a8a33STao Ma 		credits += context->post_refcount->credits;
3130492a8a33STao Ma 
3131492a8a33STao Ma 	credits += context->extra_credits;
31326f70fa51STao Ma 	handle = ocfs2_start_trans(osb, credits);
31336f70fa51STao Ma 	if (IS_ERR(handle)) {
31346f70fa51STao Ma 		ret = PTR_ERR(handle);
31356f70fa51STao Ma 		mlog_errno(ret);
31366f70fa51STao Ma 		goto out;
31376f70fa51STao Ma 	}
31386f70fa51STao Ma 
31396f70fa51STao Ma 	while (num_clusters) {
3140913580b4STao Ma 		ret = ocfs2_get_refcount_rec(ref_ci, context->ref_root_bh,
31416ae23c55STao Ma 					     p_cluster, num_clusters,
31426ae23c55STao Ma 					     &rec, &index, &ref_leaf_bh);
31436ae23c55STao Ma 		if (ret) {
31446ae23c55STao Ma 			mlog_errno(ret);
31456ae23c55STao Ma 			goto out_commit;
31466ae23c55STao Ma 		}
31476ae23c55STao Ma 
31486ae23c55STao Ma 		BUG_ON(!rec.r_refcount);
31496ae23c55STao Ma 		set_len = min((u64)p_cluster + num_clusters,
31506ae23c55STao Ma 			      le64_to_cpu(rec.r_cpos) +
31516ae23c55STao Ma 			      le32_to_cpu(rec.r_clusters)) - p_cluster;
31526ae23c55STao Ma 
31536ae23c55STao Ma 		/*
31546ae23c55STao Ma 		 * There are many different situation here.
31556ae23c55STao Ma 		 * 1. If refcount == 1, remove the flag and don't COW.
31566ae23c55STao Ma 		 * 2. If refcount > 1, allocate clusters.
31576ae23c55STao Ma 		 *    Here we may not allocate r_len once at a time, so continue
31586ae23c55STao Ma 		 *    until we reach num_clusters.
31596ae23c55STao Ma 		 */
31606ae23c55STao Ma 		if (le32_to_cpu(rec.r_refcount) == 1) {
31616ae23c55STao Ma 			delete = 0;
3162913580b4STao Ma 			ret = ocfs2_clear_ext_refcount(handle,
3163913580b4STao Ma 						       &context->data_et,
31646ae23c55STao Ma 						       cpos, p_cluster,
31656ae23c55STao Ma 						       set_len, e_flags,
31666ae23c55STao Ma 						       context->meta_ac,
31676ae23c55STao Ma 						       &context->dealloc);
31686ae23c55STao Ma 			if (ret) {
31696ae23c55STao Ma 				mlog_errno(ret);
31706ae23c55STao Ma 				goto out_commit;
31716ae23c55STao Ma 			}
31726ae23c55STao Ma 		} else {
31736ae23c55STao Ma 			delete = 1;
31746ae23c55STao Ma 
31756ae23c55STao Ma 			ret = __ocfs2_claim_clusters(osb, handle,
31766ae23c55STao Ma 						     context->data_ac,
31776ae23c55STao Ma 						     1, set_len,
31786f70fa51STao Ma 						     &new_bit, &new_len);
31796f70fa51STao Ma 			if (ret) {
31806f70fa51STao Ma 				mlog_errno(ret);
31816f70fa51STao Ma 				goto out_commit;
31826f70fa51STao Ma 			}
31836f70fa51STao Ma 
31846f70fa51STao Ma 			ret = ocfs2_replace_clusters(handle, context,
31856f70fa51STao Ma 						     cpos, p_cluster, new_bit,
31866f70fa51STao Ma 						     new_len, e_flags);
31876f70fa51STao Ma 			if (ret) {
31886f70fa51STao Ma 				mlog_errno(ret);
31896f70fa51STao Ma 				goto out_commit;
31906f70fa51STao Ma 			}
31916ae23c55STao Ma 			set_len = new_len;
31926f70fa51STao Ma 		}
31936f70fa51STao Ma 
3194913580b4STao Ma 		ret = __ocfs2_decrease_refcount(handle, ref_ci,
31956f70fa51STao Ma 						context->ref_root_bh,
31966ae23c55STao Ma 						p_cluster, set_len,
31976f70fa51STao Ma 						context->meta_ac,
31986ae23c55STao Ma 						&context->dealloc, delete);
31996f70fa51STao Ma 		if (ret) {
32006f70fa51STao Ma 			mlog_errno(ret);
32016f70fa51STao Ma 			goto out_commit;
32026f70fa51STao Ma 		}
32036f70fa51STao Ma 
32046ae23c55STao Ma 		cpos += set_len;
32056ae23c55STao Ma 		p_cluster += set_len;
32066ae23c55STao Ma 		num_clusters -= set_len;
32076ae23c55STao Ma 		brelse(ref_leaf_bh);
32086ae23c55STao Ma 		ref_leaf_bh = NULL;
32096ae23c55STao Ma 	}
32106ae23c55STao Ma 
3211492a8a33STao Ma 	/* handle any post_cow action. */
3212492a8a33STao Ma 	if (context->post_refcount && context->post_refcount->func) {
3213492a8a33STao Ma 		ret = context->post_refcount->func(context->inode, handle,
3214492a8a33STao Ma 						context->post_refcount->para);
3215492a8a33STao Ma 		if (ret) {
3216492a8a33STao Ma 			mlog_errno(ret);
3217492a8a33STao Ma 			goto out_commit;
3218492a8a33STao Ma 		}
3219492a8a33STao Ma 	}
3220492a8a33STao Ma 
32216f70fa51STao Ma 	/*
32226f70fa51STao Ma 	 * Here we should write the new page out first if we are
32236f70fa51STao Ma 	 * in write-back mode.
32246f70fa51STao Ma 	 */
3225492a8a33STao Ma 	if (context->get_clusters == ocfs2_di_get_clusters) {
32266f70fa51STao Ma 		ret = ocfs2_cow_sync_writeback(sb, context, cpos, num_clusters);
32276f70fa51STao Ma 		if (ret)
32286f70fa51STao Ma 			mlog_errno(ret);
3229492a8a33STao Ma 	}
32306f70fa51STao Ma 
32316f70fa51STao Ma out_commit:
32326f70fa51STao Ma 	ocfs2_commit_trans(osb, handle);
32336f70fa51STao Ma 
32346f70fa51STao Ma out:
32356f70fa51STao Ma 	if (context->data_ac) {
32366f70fa51STao Ma 		ocfs2_free_alloc_context(context->data_ac);
32376f70fa51STao Ma 		context->data_ac = NULL;
32386f70fa51STao Ma 	}
32396f70fa51STao Ma 	if (context->meta_ac) {
32406f70fa51STao Ma 		ocfs2_free_alloc_context(context->meta_ac);
32416f70fa51STao Ma 		context->meta_ac = NULL;
32426f70fa51STao Ma 	}
32436ae23c55STao Ma 	brelse(ref_leaf_bh);
32446f70fa51STao Ma 
32456f70fa51STao Ma 	return ret;
32466f70fa51STao Ma }
32476f70fa51STao Ma 
3248913580b4STao Ma static int ocfs2_replace_cow(struct ocfs2_cow_context *context)
32496f70fa51STao Ma {
32506f70fa51STao Ma 	int ret = 0;
3251913580b4STao Ma 	struct inode *inode = context->inode;
3252913580b4STao Ma 	u32 cow_start = context->cow_start, cow_len = context->cow_len;
3253913580b4STao Ma 	u32 p_cluster, num_clusters;
32546f70fa51STao Ma 	unsigned int ext_flags;
32556f70fa51STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
32566f70fa51STao Ma 
32576f70fa51STao Ma 	if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
32586f70fa51STao Ma 		ocfs2_error(inode->i_sb, "Inode %lu want to use refcount "
32596f70fa51STao Ma 			    "tree, but the feature bit is not set in the "
32606f70fa51STao Ma 			    "super block.", inode->i_ino);
32616f70fa51STao Ma 		return -EROFS;
32626f70fa51STao Ma 	}
32636f70fa51STao Ma 
32646f70fa51STao Ma 	ocfs2_init_dealloc_ctxt(&context->dealloc);
32656f70fa51STao Ma 
32666f70fa51STao Ma 	while (cow_len) {
3267913580b4STao Ma 		ret = context->get_clusters(context, cow_start, &p_cluster,
32686f70fa51STao Ma 					    &num_clusters, &ext_flags);
32696f70fa51STao Ma 		if (ret) {
32706f70fa51STao Ma 			mlog_errno(ret);
32716f70fa51STao Ma 			break;
32726f70fa51STao Ma 		}
32736f70fa51STao Ma 
32746f70fa51STao Ma 		BUG_ON(!(ext_flags & OCFS2_EXT_REFCOUNTED));
32756f70fa51STao Ma 
32766f70fa51STao Ma 		if (cow_len < num_clusters)
32776f70fa51STao Ma 			num_clusters = cow_len;
32786f70fa51STao Ma 
32796f70fa51STao Ma 		ret = ocfs2_make_clusters_writable(inode->i_sb, context,
32806f70fa51STao Ma 						   cow_start, p_cluster,
32816f70fa51STao Ma 						   num_clusters, ext_flags);
32826f70fa51STao Ma 		if (ret) {
32836f70fa51STao Ma 			mlog_errno(ret);
32846f70fa51STao Ma 			break;
32856f70fa51STao Ma 		}
32866f70fa51STao Ma 
32876f70fa51STao Ma 		cow_len -= num_clusters;
32886f70fa51STao Ma 		cow_start += num_clusters;
32896f70fa51STao Ma 	}
32906f70fa51STao Ma 
32916f70fa51STao Ma 	if (ocfs2_dealloc_has_cluster(&context->dealloc)) {
32926f70fa51STao Ma 		ocfs2_schedule_truncate_log_flush(osb, 1);
32936f70fa51STao Ma 		ocfs2_run_deallocs(osb, &context->dealloc);
32946f70fa51STao Ma 	}
32956f70fa51STao Ma 
32966f70fa51STao Ma 	return ret;
32976f70fa51STao Ma }
32986f70fa51STao Ma 
32996f70fa51STao Ma /*
330037f8a2bfSTao Ma  * Starting at cpos, try to CoW write_len clusters.  Don't CoW
330137f8a2bfSTao Ma  * past max_cpos.  This will stop when it runs into a hole or an
330237f8a2bfSTao Ma  * unrefcounted extent.
33036f70fa51STao Ma  */
33046f70fa51STao Ma static int ocfs2_refcount_cow_hunk(struct inode *inode,
33056f70fa51STao Ma 				   struct buffer_head *di_bh,
330637f8a2bfSTao Ma 				   u32 cpos, u32 write_len, u32 max_cpos)
33076f70fa51STao Ma {
33086f70fa51STao Ma 	int ret;
33096f70fa51STao Ma 	u32 cow_start = 0, cow_len = 0;
33106f70fa51STao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
33116f70fa51STao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
33126f70fa51STao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
33136f70fa51STao Ma 	struct buffer_head *ref_root_bh = NULL;
33146f70fa51STao Ma 	struct ocfs2_refcount_tree *ref_tree;
3315913580b4STao Ma 	struct ocfs2_cow_context *context = NULL;
33166f70fa51STao Ma 
33176f70fa51STao Ma 	BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
33186f70fa51STao Ma 
3319913580b4STao Ma 	ret = ocfs2_refcount_cal_cow_clusters(inode, &di->id2.i_list,
332037f8a2bfSTao Ma 					      cpos, write_len, max_cpos,
33216f70fa51STao Ma 					      &cow_start, &cow_len);
33226f70fa51STao Ma 	if (ret) {
33236f70fa51STao Ma 		mlog_errno(ret);
33246f70fa51STao Ma 		goto out;
33256f70fa51STao Ma 	}
332637f8a2bfSTao Ma 
33276f70fa51STao Ma 	mlog(0, "CoW inode %lu, cpos %u, write_len %u, cow_start %u, "
33286f70fa51STao Ma 	     "cow_len %u\n", inode->i_ino,
33296f70fa51STao Ma 	     cpos, write_len, cow_start, cow_len);
33306f70fa51STao Ma 
33316f70fa51STao Ma 	BUG_ON(cow_len == 0);
33326f70fa51STao Ma 
3333913580b4STao Ma 	context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
3334913580b4STao Ma 	if (!context) {
3335913580b4STao Ma 		ret = -ENOMEM;
3336913580b4STao Ma 		mlog_errno(ret);
3337913580b4STao Ma 		goto out;
3338913580b4STao Ma 	}
3339913580b4STao Ma 
33406f70fa51STao Ma 	ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
33416f70fa51STao Ma 				       1, &ref_tree, &ref_root_bh);
33426f70fa51STao Ma 	if (ret) {
33436f70fa51STao Ma 		mlog_errno(ret);
33446f70fa51STao Ma 		goto out;
33456f70fa51STao Ma 	}
33466f70fa51STao Ma 
3347913580b4STao Ma 	context->inode = inode;
3348913580b4STao Ma 	context->cow_start = cow_start;
3349913580b4STao Ma 	context->cow_len = cow_len;
3350913580b4STao Ma 	context->ref_tree = ref_tree;
3351913580b4STao Ma 	context->ref_root_bh = ref_root_bh;
3352913580b4STao Ma 	context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_page;
3353913580b4STao Ma 	context->get_clusters = ocfs2_di_get_clusters;
3354913580b4STao Ma 
3355913580b4STao Ma 	ocfs2_init_dinode_extent_tree(&context->data_et,
3356913580b4STao Ma 				      INODE_CACHE(inode), di_bh);
3357913580b4STao Ma 
3358913580b4STao Ma 	ret = ocfs2_replace_cow(context);
33596f70fa51STao Ma 	if (ret)
33606f70fa51STao Ma 		mlog_errno(ret);
33616f70fa51STao Ma 
3362913580b4STao Ma 	/*
3363913580b4STao Ma 	 * truncate the extent map here since no matter whether we meet with
3364913580b4STao Ma 	 * any error during the action, we shouldn't trust cached extent map
3365913580b4STao Ma 	 * any more.
3366913580b4STao Ma 	 */
3367913580b4STao Ma 	ocfs2_extent_map_trunc(inode, cow_start);
3368913580b4STao Ma 
33696f70fa51STao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
33706f70fa51STao Ma 	brelse(ref_root_bh);
33716f70fa51STao Ma out:
3372913580b4STao Ma 	kfree(context);
33736f70fa51STao Ma 	return ret;
33746f70fa51STao Ma }
33756f70fa51STao Ma 
33766f70fa51STao Ma /*
33776f70fa51STao Ma  * CoW any and all clusters between cpos and cpos+write_len.
337837f8a2bfSTao Ma  * Don't CoW past max_cpos.  If this returns successfully, all
337937f8a2bfSTao Ma  * clusters between cpos and cpos+write_len are safe to modify.
33806f70fa51STao Ma  */
33816f70fa51STao Ma int ocfs2_refcount_cow(struct inode *inode,
33826f70fa51STao Ma 		       struct buffer_head *di_bh,
338337f8a2bfSTao Ma 		       u32 cpos, u32 write_len, u32 max_cpos)
33846f70fa51STao Ma {
33856f70fa51STao Ma 	int ret = 0;
33866f70fa51STao Ma 	u32 p_cluster, num_clusters;
33876f70fa51STao Ma 	unsigned int ext_flags;
33886f70fa51STao Ma 
33896f70fa51STao Ma 	while (write_len) {
33906f70fa51STao Ma 		ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
33916f70fa51STao Ma 					 &num_clusters, &ext_flags);
33926f70fa51STao Ma 		if (ret) {
33936f70fa51STao Ma 			mlog_errno(ret);
33946f70fa51STao Ma 			break;
33956f70fa51STao Ma 		}
33966f70fa51STao Ma 
33976f70fa51STao Ma 		if (write_len < num_clusters)
33986f70fa51STao Ma 			num_clusters = write_len;
33996f70fa51STao Ma 
34006f70fa51STao Ma 		if (ext_flags & OCFS2_EXT_REFCOUNTED) {
34016f70fa51STao Ma 			ret = ocfs2_refcount_cow_hunk(inode, di_bh, cpos,
340237f8a2bfSTao Ma 						      num_clusters, max_cpos);
34036f70fa51STao Ma 			if (ret) {
34046f70fa51STao Ma 				mlog_errno(ret);
34056f70fa51STao Ma 				break;
34066f70fa51STao Ma 			}
34076f70fa51STao Ma 		}
34086f70fa51STao Ma 
34096f70fa51STao Ma 		write_len -= num_clusters;
34106f70fa51STao Ma 		cpos += num_clusters;
34116f70fa51STao Ma 	}
34126f70fa51STao Ma 
34136f70fa51STao Ma 	return ret;
34146f70fa51STao Ma }
3415110a045aSTao Ma 
3416492a8a33STao Ma static int ocfs2_xattr_value_get_clusters(struct ocfs2_cow_context *context,
3417492a8a33STao Ma 					  u32 v_cluster, u32 *p_cluster,
3418492a8a33STao Ma 					  u32 *num_clusters,
3419492a8a33STao Ma 					  unsigned int *extent_flags)
3420492a8a33STao Ma {
3421492a8a33STao Ma 	struct inode *inode = context->inode;
3422492a8a33STao Ma 	struct ocfs2_xattr_value_root *xv = context->cow_object;
3423492a8a33STao Ma 
3424492a8a33STao Ma 	return ocfs2_xattr_get_clusters(inode, v_cluster, p_cluster,
3425492a8a33STao Ma 					num_clusters, &xv->xr_list,
3426492a8a33STao Ma 					extent_flags);
3427492a8a33STao Ma }
3428492a8a33STao Ma 
3429492a8a33STao Ma /*
3430492a8a33STao Ma  * Given a xattr value root, calculate the most meta/credits we need for
3431492a8a33STao Ma  * refcount tree change if we truncate it to 0.
3432492a8a33STao Ma  */
3433492a8a33STao Ma int ocfs2_refcounted_xattr_delete_need(struct inode *inode,
3434492a8a33STao Ma 				       struct ocfs2_caching_info *ref_ci,
3435492a8a33STao Ma 				       struct buffer_head *ref_root_bh,
3436492a8a33STao Ma 				       struct ocfs2_xattr_value_root *xv,
3437492a8a33STao Ma 				       int *meta_add, int *credits)
3438492a8a33STao Ma {
3439492a8a33STao Ma 	int ret = 0, index, ref_blocks = 0;
3440492a8a33STao Ma 	u32 p_cluster, num_clusters;
3441492a8a33STao Ma 	u32 cpos = 0, clusters = le32_to_cpu(xv->xr_clusters);
3442492a8a33STao Ma 	struct ocfs2_refcount_block *rb;
3443492a8a33STao Ma 	struct ocfs2_refcount_rec rec;
3444492a8a33STao Ma 	struct buffer_head *ref_leaf_bh = NULL;
3445492a8a33STao Ma 
3446492a8a33STao Ma 	while (cpos < clusters) {
3447492a8a33STao Ma 		ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
3448492a8a33STao Ma 					       &num_clusters, &xv->xr_list,
3449492a8a33STao Ma 					       NULL);
3450492a8a33STao Ma 		if (ret) {
3451492a8a33STao Ma 			mlog_errno(ret);
3452492a8a33STao Ma 			goto out;
3453492a8a33STao Ma 		}
3454492a8a33STao Ma 
3455492a8a33STao Ma 		cpos += num_clusters;
3456492a8a33STao Ma 
3457492a8a33STao Ma 		while (num_clusters) {
3458492a8a33STao Ma 			ret = ocfs2_get_refcount_rec(ref_ci, ref_root_bh,
3459492a8a33STao Ma 						     p_cluster, num_clusters,
3460492a8a33STao Ma 						     &rec, &index,
3461492a8a33STao Ma 						     &ref_leaf_bh);
3462492a8a33STao Ma 			if (ret) {
3463492a8a33STao Ma 				mlog_errno(ret);
3464492a8a33STao Ma 				goto out;
3465492a8a33STao Ma 			}
3466492a8a33STao Ma 
3467492a8a33STao Ma 			BUG_ON(!rec.r_refcount);
3468492a8a33STao Ma 
3469492a8a33STao Ma 			rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
3470492a8a33STao Ma 
3471492a8a33STao Ma 			/*
3472492a8a33STao Ma 			 * We really don't know whether the other clusters is in
3473492a8a33STao Ma 			 * this refcount block or not, so just take the worst
3474492a8a33STao Ma 			 * case that all the clusters are in this block and each
3475492a8a33STao Ma 			 * one will split a refcount rec, so totally we need
3476492a8a33STao Ma 			 * clusters * 2 new refcount rec.
3477492a8a33STao Ma 			 */
3478492a8a33STao Ma 			if (le64_to_cpu(rb->rf_records.rl_used) + clusters * 2 >
3479492a8a33STao Ma 			    le16_to_cpu(rb->rf_records.rl_count))
3480492a8a33STao Ma 				ref_blocks++;
3481492a8a33STao Ma 
3482492a8a33STao Ma 			*credits += 1;
3483492a8a33STao Ma 			brelse(ref_leaf_bh);
3484492a8a33STao Ma 			ref_leaf_bh = NULL;
3485492a8a33STao Ma 
3486492a8a33STao Ma 			if (num_clusters <= le32_to_cpu(rec.r_clusters))
3487492a8a33STao Ma 				break;
3488492a8a33STao Ma 			else
3489492a8a33STao Ma 				num_clusters -= le32_to_cpu(rec.r_clusters);
3490492a8a33STao Ma 			p_cluster += num_clusters;
3491492a8a33STao Ma 		}
3492492a8a33STao Ma 	}
3493492a8a33STao Ma 
3494492a8a33STao Ma 	*meta_add += ref_blocks;
3495492a8a33STao Ma 	if (!ref_blocks)
3496492a8a33STao Ma 		goto out;
3497492a8a33STao Ma 
3498492a8a33STao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
3499492a8a33STao Ma 	if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
3500492a8a33STao Ma 		*credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
3501492a8a33STao Ma 	else {
3502492a8a33STao Ma 		struct ocfs2_extent_tree et;
3503492a8a33STao Ma 
3504492a8a33STao Ma 		ocfs2_init_refcount_extent_tree(&et, ref_ci, ref_root_bh);
3505492a8a33STao Ma 		*credits += ocfs2_calc_extend_credits(inode->i_sb,
3506492a8a33STao Ma 						      et.et_root_el,
3507492a8a33STao Ma 						      ref_blocks);
3508492a8a33STao Ma 	}
3509492a8a33STao Ma 
3510492a8a33STao Ma out:
3511492a8a33STao Ma 	brelse(ref_leaf_bh);
3512492a8a33STao Ma 	return ret;
3513492a8a33STao Ma }
3514492a8a33STao Ma 
3515492a8a33STao Ma /*
3516492a8a33STao Ma  * Do CoW for xattr.
3517492a8a33STao Ma  */
3518492a8a33STao Ma int ocfs2_refcount_cow_xattr(struct inode *inode,
3519492a8a33STao Ma 			     struct ocfs2_dinode *di,
3520492a8a33STao Ma 			     struct ocfs2_xattr_value_buf *vb,
3521492a8a33STao Ma 			     struct ocfs2_refcount_tree *ref_tree,
3522492a8a33STao Ma 			     struct buffer_head *ref_root_bh,
3523492a8a33STao Ma 			     u32 cpos, u32 write_len,
3524492a8a33STao Ma 			     struct ocfs2_post_refcount *post)
3525492a8a33STao Ma {
3526492a8a33STao Ma 	int ret;
3527492a8a33STao Ma 	struct ocfs2_xattr_value_root *xv = vb->vb_xv;
3528492a8a33STao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
3529492a8a33STao Ma 	struct ocfs2_cow_context *context = NULL;
3530492a8a33STao Ma 	u32 cow_start, cow_len;
3531492a8a33STao Ma 
3532492a8a33STao Ma 	BUG_ON(!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL));
3533492a8a33STao Ma 
3534492a8a33STao Ma 	ret = ocfs2_refcount_cal_cow_clusters(inode, &xv->xr_list,
3535492a8a33STao Ma 					      cpos, write_len, UINT_MAX,
3536492a8a33STao Ma 					      &cow_start, &cow_len);
3537492a8a33STao Ma 	if (ret) {
3538492a8a33STao Ma 		mlog_errno(ret);
3539492a8a33STao Ma 		goto out;
3540492a8a33STao Ma 	}
3541492a8a33STao Ma 
3542492a8a33STao Ma 	BUG_ON(cow_len == 0);
3543492a8a33STao Ma 
3544492a8a33STao Ma 	context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
3545492a8a33STao Ma 	if (!context) {
3546492a8a33STao Ma 		ret = -ENOMEM;
3547492a8a33STao Ma 		mlog_errno(ret);
3548492a8a33STao Ma 		goto out;
3549492a8a33STao Ma 	}
3550492a8a33STao Ma 
3551492a8a33STao Ma 	context->inode = inode;
3552492a8a33STao Ma 	context->cow_start = cow_start;
3553492a8a33STao Ma 	context->cow_len = cow_len;
3554492a8a33STao Ma 	context->ref_tree = ref_tree;
3555492a8a33STao Ma 	context->ref_root_bh = ref_root_bh;;
3556492a8a33STao Ma 	context->cow_object = xv;
3557492a8a33STao Ma 
3558492a8a33STao Ma 	context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_jbd;
3559492a8a33STao Ma 	/* We need the extra credits for duplicate_clusters by jbd. */
3560492a8a33STao Ma 	context->extra_credits =
3561492a8a33STao Ma 		ocfs2_clusters_to_blocks(inode->i_sb, 1) * cow_len;
3562492a8a33STao Ma 	context->get_clusters = ocfs2_xattr_value_get_clusters;
3563492a8a33STao Ma 	context->post_refcount = post;
3564492a8a33STao Ma 
3565492a8a33STao Ma 	ocfs2_init_xattr_value_extent_tree(&context->data_et,
3566492a8a33STao Ma 					   INODE_CACHE(inode), vb);
3567492a8a33STao Ma 
3568492a8a33STao Ma 	ret = ocfs2_replace_cow(context);
3569492a8a33STao Ma 	if (ret)
3570492a8a33STao Ma 		mlog_errno(ret);
3571492a8a33STao Ma 
3572492a8a33STao Ma out:
3573492a8a33STao Ma 	kfree(context);
3574492a8a33STao Ma 	return ret;
3575492a8a33STao Ma }
3576492a8a33STao Ma 
3577110a045aSTao Ma /*
3578110a045aSTao Ma  * Insert a new extent into refcount tree and mark a extent rec
3579110a045aSTao Ma  * as refcounted in the dinode tree.
3580110a045aSTao Ma  */
3581110a045aSTao Ma int ocfs2_add_refcount_flag(struct inode *inode,
3582110a045aSTao Ma 			    struct ocfs2_extent_tree *data_et,
3583110a045aSTao Ma 			    struct ocfs2_caching_info *ref_ci,
3584110a045aSTao Ma 			    struct buffer_head *ref_root_bh,
3585110a045aSTao Ma 			    u32 cpos, u32 p_cluster, u32 num_clusters,
35860129241eSTao Ma 			    struct ocfs2_cached_dealloc_ctxt *dealloc,
35870129241eSTao Ma 			    struct ocfs2_post_refcount *post)
3588110a045aSTao Ma {
3589110a045aSTao Ma 	int ret;
3590110a045aSTao Ma 	handle_t *handle;
3591110a045aSTao Ma 	int credits = 1, ref_blocks = 0;
3592110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3593110a045aSTao Ma 	struct ocfs2_alloc_context *meta_ac = NULL;
3594110a045aSTao Ma 
3595110a045aSTao Ma 	ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
3596110a045aSTao Ma 					       ref_ci, ref_root_bh,
3597110a045aSTao Ma 					       p_cluster, num_clusters,
3598110a045aSTao Ma 					       &ref_blocks, &credits);
3599110a045aSTao Ma 	if (ret) {
3600110a045aSTao Ma 		mlog_errno(ret);
3601110a045aSTao Ma 		goto out;
3602110a045aSTao Ma 	}
3603110a045aSTao Ma 
3604110a045aSTao Ma 	mlog(0, "reserve new metadata %d, credits = %d\n",
3605110a045aSTao Ma 	     ref_blocks, credits);
3606110a045aSTao Ma 
3607110a045aSTao Ma 	if (ref_blocks) {
3608110a045aSTao Ma 		ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(inode->i_sb),
3609110a045aSTao Ma 							ref_blocks, &meta_ac);
3610110a045aSTao Ma 		if (ret) {
3611110a045aSTao Ma 			mlog_errno(ret);
3612110a045aSTao Ma 			goto out;
3613110a045aSTao Ma 		}
3614110a045aSTao Ma 	}
3615110a045aSTao Ma 
36160129241eSTao Ma 	if (post)
36170129241eSTao Ma 		credits += post->credits;
36180129241eSTao Ma 
3619110a045aSTao Ma 	handle = ocfs2_start_trans(osb, credits);
3620110a045aSTao Ma 	if (IS_ERR(handle)) {
3621110a045aSTao Ma 		ret = PTR_ERR(handle);
3622110a045aSTao Ma 		mlog_errno(ret);
3623110a045aSTao Ma 		goto out;
3624110a045aSTao Ma 	}
3625110a045aSTao Ma 
3626110a045aSTao Ma 	ret = ocfs2_mark_extent_refcounted(inode, data_et, handle,
3627110a045aSTao Ma 					   cpos, num_clusters, p_cluster,
3628110a045aSTao Ma 					   meta_ac, dealloc);
3629110a045aSTao Ma 	if (ret) {
3630110a045aSTao Ma 		mlog_errno(ret);
3631110a045aSTao Ma 		goto out_commit;
3632110a045aSTao Ma 	}
3633110a045aSTao Ma 
36342999d12fSTao Ma 	ret = ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
3635110a045aSTao Ma 				      p_cluster, num_clusters,
3636110a045aSTao Ma 				      meta_ac, dealloc);
36370129241eSTao Ma 	if (ret) {
36380129241eSTao Ma 		mlog_errno(ret);
36390129241eSTao Ma 		goto out_commit;
36400129241eSTao Ma 	}
36410129241eSTao Ma 
36420129241eSTao Ma 	if (post && post->func) {
36430129241eSTao Ma 		ret = post->func(inode, handle, post->para);
3644110a045aSTao Ma 		if (ret)
3645110a045aSTao Ma 			mlog_errno(ret);
36460129241eSTao Ma 	}
3647110a045aSTao Ma 
3648110a045aSTao Ma out_commit:
3649110a045aSTao Ma 	ocfs2_commit_trans(osb, handle);
3650110a045aSTao Ma out:
3651110a045aSTao Ma 	if (meta_ac)
3652110a045aSTao Ma 		ocfs2_free_alloc_context(meta_ac);
3653110a045aSTao Ma 	return ret;
3654110a045aSTao Ma }
3655110a045aSTao Ma 
3656a9063ab9STao Ma static int ocfs2_change_ctime(struct inode *inode,
3657110a045aSTao Ma 			      struct buffer_head *di_bh)
3658110a045aSTao Ma {
3659110a045aSTao Ma 	int ret;
3660a9063ab9STao Ma 	handle_t *handle;
3661a9063ab9STao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3662a9063ab9STao Ma 
3663a9063ab9STao Ma 	handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
3664a9063ab9STao Ma 				   OCFS2_INODE_UPDATE_CREDITS);
3665a9063ab9STao Ma 	if (IS_ERR(handle)) {
3666a9063ab9STao Ma 		ret = PTR_ERR(handle);
3667a9063ab9STao Ma 		mlog_errno(ret);
3668a9063ab9STao Ma 		goto out;
3669a9063ab9STao Ma 	}
3670a9063ab9STao Ma 
3671a9063ab9STao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
3672a9063ab9STao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
3673a9063ab9STao Ma 	if (ret) {
3674a9063ab9STao Ma 		mlog_errno(ret);
3675a9063ab9STao Ma 		goto out_commit;
3676a9063ab9STao Ma 	}
3677a9063ab9STao Ma 
3678a9063ab9STao Ma 	inode->i_ctime = CURRENT_TIME;
3679a9063ab9STao Ma 	di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3680a9063ab9STao Ma 	di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3681a9063ab9STao Ma 
3682a9063ab9STao Ma 	ocfs2_journal_dirty(handle, di_bh);
3683a9063ab9STao Ma 
3684a9063ab9STao Ma out_commit:
3685a9063ab9STao Ma 	ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3686a9063ab9STao Ma out:
3687a9063ab9STao Ma 	return ret;
3688a9063ab9STao Ma }
3689a9063ab9STao Ma 
3690a9063ab9STao Ma static int ocfs2_attach_refcount_tree(struct inode *inode,
3691a9063ab9STao Ma 				      struct buffer_head *di_bh)
3692a9063ab9STao Ma {
3693a9063ab9STao Ma 	int ret, data_changed = 0;
3694110a045aSTao Ma 	struct buffer_head *ref_root_bh = NULL;
3695110a045aSTao Ma 	struct ocfs2_inode_info *oi = OCFS2_I(inode);
3696110a045aSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3697110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3698110a045aSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
3699110a045aSTao Ma 	unsigned int ext_flags;
3700110a045aSTao Ma 	loff_t size;
3701110a045aSTao Ma 	u32 cpos, num_clusters, clusters, p_cluster;
3702110a045aSTao Ma 	struct ocfs2_cached_dealloc_ctxt dealloc;
3703110a045aSTao Ma 	struct ocfs2_extent_tree di_et;
3704110a045aSTao Ma 
3705110a045aSTao Ma 	ocfs2_init_dealloc_ctxt(&dealloc);
3706110a045aSTao Ma 
3707110a045aSTao Ma 	if (!(oi->ip_dyn_features & OCFS2_HAS_REFCOUNT_FL)) {
3708110a045aSTao Ma 		ret = ocfs2_create_refcount_tree(inode, di_bh);
3709110a045aSTao Ma 		if (ret) {
3710110a045aSTao Ma 			mlog_errno(ret);
3711110a045aSTao Ma 			goto out;
3712110a045aSTao Ma 		}
3713110a045aSTao Ma 	}
3714110a045aSTao Ma 
3715110a045aSTao Ma 	BUG_ON(!di->i_refcount_loc);
3716110a045aSTao Ma 	ret = ocfs2_lock_refcount_tree(osb,
3717110a045aSTao Ma 				       le64_to_cpu(di->i_refcount_loc), 1,
3718110a045aSTao Ma 				       &ref_tree, &ref_root_bh);
3719110a045aSTao Ma 	if (ret) {
3720110a045aSTao Ma 		mlog_errno(ret);
3721110a045aSTao Ma 		goto out;
3722110a045aSTao Ma 	}
3723110a045aSTao Ma 
3724110a045aSTao Ma 	ocfs2_init_dinode_extent_tree(&di_et, INODE_CACHE(inode), di_bh);
3725110a045aSTao Ma 
3726110a045aSTao Ma 	size = i_size_read(inode);
3727110a045aSTao Ma 	clusters = ocfs2_clusters_for_bytes(inode->i_sb, size);
3728110a045aSTao Ma 
3729110a045aSTao Ma 	cpos = 0;
3730110a045aSTao Ma 	while (cpos < clusters) {
3731110a045aSTao Ma 		ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
3732110a045aSTao Ma 					 &num_clusters, &ext_flags);
3733110a045aSTao Ma 
3734110a045aSTao Ma 		if (p_cluster && !(ext_flags & OCFS2_EXT_REFCOUNTED)) {
3735110a045aSTao Ma 			ret = ocfs2_add_refcount_flag(inode, &di_et,
3736110a045aSTao Ma 						      &ref_tree->rf_ci,
3737110a045aSTao Ma 						      ref_root_bh, cpos,
3738110a045aSTao Ma 						      p_cluster, num_clusters,
37390129241eSTao Ma 						      &dealloc, NULL);
3740110a045aSTao Ma 			if (ret) {
3741110a045aSTao Ma 				mlog_errno(ret);
3742a9063ab9STao Ma 				goto unlock;
3743110a045aSTao Ma 			}
3744a9063ab9STao Ma 
3745a9063ab9STao Ma 			data_changed = 1;
3746110a045aSTao Ma 		}
3747110a045aSTao Ma 		cpos += num_clusters;
3748110a045aSTao Ma 	}
3749110a045aSTao Ma 
37500129241eSTao Ma 	if (oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) {
37510129241eSTao Ma 		ret = ocfs2_xattr_attach_refcount_tree(inode, di_bh,
37520129241eSTao Ma 						       &ref_tree->rf_ci,
37530129241eSTao Ma 						       ref_root_bh,
37540129241eSTao Ma 						       &dealloc);
37550129241eSTao Ma 		if (ret) {
37560129241eSTao Ma 			mlog_errno(ret);
37570129241eSTao Ma 			goto unlock;
37580129241eSTao Ma 		}
37590129241eSTao Ma 	}
37600129241eSTao Ma 
3761a9063ab9STao Ma 	if (data_changed) {
3762a9063ab9STao Ma 		ret = ocfs2_change_ctime(inode, di_bh);
3763a9063ab9STao Ma 		if (ret)
3764a9063ab9STao Ma 			mlog_errno(ret);
3765a9063ab9STao Ma 	}
3766a9063ab9STao Ma 
3767a9063ab9STao Ma unlock:
3768110a045aSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3769110a045aSTao Ma 	brelse(ref_root_bh);
3770110a045aSTao Ma 
3771110a045aSTao Ma 	if (!ret && ocfs2_dealloc_has_cluster(&dealloc)) {
3772110a045aSTao Ma 		ocfs2_schedule_truncate_log_flush(osb, 1);
3773110a045aSTao Ma 		ocfs2_run_deallocs(osb, &dealloc);
3774110a045aSTao Ma 	}
3775110a045aSTao Ma out:
3776110a045aSTao Ma 	/*
3777110a045aSTao Ma 	 * Empty the extent map so that we may get the right extent
3778110a045aSTao Ma 	 * record from the disk.
3779110a045aSTao Ma 	 */
3780110a045aSTao Ma 	ocfs2_extent_map_trunc(inode, 0);
3781110a045aSTao Ma 
3782110a045aSTao Ma 	return ret;
3783110a045aSTao Ma }
3784110a045aSTao Ma 
3785110a045aSTao Ma static int ocfs2_add_refcounted_extent(struct inode *inode,
3786110a045aSTao Ma 				   struct ocfs2_extent_tree *et,
3787110a045aSTao Ma 				   struct ocfs2_caching_info *ref_ci,
3788110a045aSTao Ma 				   struct buffer_head *ref_root_bh,
3789110a045aSTao Ma 				   u32 cpos, u32 p_cluster, u32 num_clusters,
3790110a045aSTao Ma 				   unsigned int ext_flags,
3791110a045aSTao Ma 				   struct ocfs2_cached_dealloc_ctxt *dealloc)
3792110a045aSTao Ma {
3793110a045aSTao Ma 	int ret;
3794110a045aSTao Ma 	handle_t *handle;
3795110a045aSTao Ma 	int credits = 0;
3796110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3797110a045aSTao Ma 	struct ocfs2_alloc_context *meta_ac = NULL;
3798110a045aSTao Ma 
3799110a045aSTao Ma 	ret = ocfs2_lock_refcount_allocators(inode->i_sb,
3800110a045aSTao Ma 					     p_cluster, num_clusters,
3801110a045aSTao Ma 					     et, ref_ci,
3802110a045aSTao Ma 					     ref_root_bh, &meta_ac,
3803110a045aSTao Ma 					     NULL, &credits);
3804110a045aSTao Ma 	if (ret) {
3805110a045aSTao Ma 		mlog_errno(ret);
3806110a045aSTao Ma 		goto out;
3807110a045aSTao Ma 	}
3808110a045aSTao Ma 
3809110a045aSTao Ma 	handle = ocfs2_start_trans(osb, credits);
3810110a045aSTao Ma 	if (IS_ERR(handle)) {
3811110a045aSTao Ma 		ret = PTR_ERR(handle);
3812110a045aSTao Ma 		mlog_errno(ret);
3813110a045aSTao Ma 		goto out;
3814110a045aSTao Ma 	}
3815110a045aSTao Ma 
3816110a045aSTao Ma 	ret = ocfs2_insert_extent(handle, et, cpos,
3817110a045aSTao Ma 			cpu_to_le64(ocfs2_clusters_to_blocks(inode->i_sb,
3818110a045aSTao Ma 							     p_cluster)),
3819110a045aSTao Ma 			num_clusters, ext_flags, meta_ac);
3820110a045aSTao Ma 	if (ret) {
3821110a045aSTao Ma 		mlog_errno(ret);
3822110a045aSTao Ma 		goto out_commit;
3823110a045aSTao Ma 	}
3824110a045aSTao Ma 
38252999d12fSTao Ma 	ret = ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
3826110a045aSTao Ma 				      p_cluster, num_clusters,
3827110a045aSTao Ma 				      meta_ac, dealloc);
3828110a045aSTao Ma 	if (ret)
3829110a045aSTao Ma 		mlog_errno(ret);
3830110a045aSTao Ma 
3831110a045aSTao Ma out_commit:
3832110a045aSTao Ma 	ocfs2_commit_trans(osb, handle);
3833110a045aSTao Ma out:
3834110a045aSTao Ma 	if (meta_ac)
3835110a045aSTao Ma 		ocfs2_free_alloc_context(meta_ac);
3836110a045aSTao Ma 	return ret;
3837110a045aSTao Ma }
3838110a045aSTao Ma 
3839110a045aSTao Ma static int ocfs2_duplicate_extent_list(struct inode *s_inode,
3840110a045aSTao Ma 				struct inode *t_inode,
3841110a045aSTao Ma 				struct buffer_head *t_bh,
3842110a045aSTao Ma 				struct ocfs2_caching_info *ref_ci,
3843110a045aSTao Ma 				struct buffer_head *ref_root_bh,
3844110a045aSTao Ma 				struct ocfs2_cached_dealloc_ctxt *dealloc)
3845110a045aSTao Ma {
3846110a045aSTao Ma 	int ret = 0;
3847110a045aSTao Ma 	u32 p_cluster, num_clusters, clusters, cpos;
3848110a045aSTao Ma 	loff_t size;
3849110a045aSTao Ma 	unsigned int ext_flags;
3850110a045aSTao Ma 	struct ocfs2_extent_tree et;
3851110a045aSTao Ma 
3852110a045aSTao Ma 	ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(t_inode), t_bh);
3853110a045aSTao Ma 
3854110a045aSTao Ma 	size = i_size_read(s_inode);
3855110a045aSTao Ma 	clusters = ocfs2_clusters_for_bytes(s_inode->i_sb, size);
3856110a045aSTao Ma 
3857110a045aSTao Ma 	cpos = 0;
3858110a045aSTao Ma 	while (cpos < clusters) {
3859110a045aSTao Ma 		ret = ocfs2_get_clusters(s_inode, cpos, &p_cluster,
3860110a045aSTao Ma 					 &num_clusters, &ext_flags);
3861110a045aSTao Ma 
3862110a045aSTao Ma 		if (p_cluster) {
3863110a045aSTao Ma 			ret = ocfs2_add_refcounted_extent(t_inode, &et,
3864110a045aSTao Ma 							  ref_ci, ref_root_bh,
3865110a045aSTao Ma 							  cpos, p_cluster,
3866110a045aSTao Ma 							  num_clusters,
3867110a045aSTao Ma 							  ext_flags,
3868110a045aSTao Ma 							  dealloc);
3869110a045aSTao Ma 			if (ret) {
3870110a045aSTao Ma 				mlog_errno(ret);
3871110a045aSTao Ma 				goto out;
3872110a045aSTao Ma 			}
3873110a045aSTao Ma 		}
3874110a045aSTao Ma 
3875110a045aSTao Ma 		cpos += num_clusters;
3876110a045aSTao Ma 	}
3877110a045aSTao Ma 
3878110a045aSTao Ma out:
3879110a045aSTao Ma 	return ret;
3880110a045aSTao Ma }
3881110a045aSTao Ma 
3882a9063ab9STao Ma /*
3883a9063ab9STao Ma  * change the new file's attributes to the src.
3884a9063ab9STao Ma  *
3885a9063ab9STao Ma  * reflink creates a snapshot of a file, that means the attributes
3886a9063ab9STao Ma  * must be identical except for three exceptions - nlink, ino, and ctime.
3887a9063ab9STao Ma  */
3888a9063ab9STao Ma static int ocfs2_complete_reflink(struct inode *s_inode,
3889a9063ab9STao Ma 				  struct buffer_head *s_bh,
3890a9063ab9STao Ma 				  struct inode *t_inode,
3891a9063ab9STao Ma 				  struct buffer_head *t_bh)
3892a9063ab9STao Ma {
3893a9063ab9STao Ma 	int ret;
3894a9063ab9STao Ma 	handle_t *handle;
3895a9063ab9STao Ma 	struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data;
3896a9063ab9STao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)t_bh->b_data;
3897a9063ab9STao Ma 	loff_t size = i_size_read(s_inode);
3898a9063ab9STao Ma 
3899a9063ab9STao Ma 	handle = ocfs2_start_trans(OCFS2_SB(t_inode->i_sb),
3900a9063ab9STao Ma 				   OCFS2_INODE_UPDATE_CREDITS);
3901a9063ab9STao Ma 	if (IS_ERR(handle)) {
3902a9063ab9STao Ma 		ret = PTR_ERR(handle);
3903a9063ab9STao Ma 		mlog_errno(ret);
3904a9063ab9STao Ma 		return ret;
3905a9063ab9STao Ma 	}
3906a9063ab9STao Ma 
3907a9063ab9STao Ma 	ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh,
3908a9063ab9STao Ma 				      OCFS2_JOURNAL_ACCESS_WRITE);
3909a9063ab9STao Ma 	if (ret) {
3910a9063ab9STao Ma 		mlog_errno(ret);
3911a9063ab9STao Ma 		goto out_commit;
3912a9063ab9STao Ma 	}
3913a9063ab9STao Ma 
3914a9063ab9STao Ma 	spin_lock(&OCFS2_I(t_inode)->ip_lock);
3915a9063ab9STao Ma 	OCFS2_I(t_inode)->ip_clusters = OCFS2_I(s_inode)->ip_clusters;
3916a9063ab9STao Ma 	OCFS2_I(t_inode)->ip_attr = OCFS2_I(s_inode)->ip_attr;
3917a9063ab9STao Ma 	OCFS2_I(t_inode)->ip_dyn_features = OCFS2_I(s_inode)->ip_dyn_features;
3918a9063ab9STao Ma 	spin_unlock(&OCFS2_I(t_inode)->ip_lock);
3919a9063ab9STao Ma 	i_size_write(t_inode, size);
3920a9063ab9STao Ma 
3921a9063ab9STao Ma 	di->i_xattr_inline_size = s_di->i_xattr_inline_size;
3922a9063ab9STao Ma 	di->i_clusters = s_di->i_clusters;
3923a9063ab9STao Ma 	di->i_size = s_di->i_size;
3924a9063ab9STao Ma 	di->i_dyn_features = s_di->i_dyn_features;
3925a9063ab9STao Ma 	di->i_attr = s_di->i_attr;
3926a9063ab9STao Ma 	di->i_uid = s_di->i_uid;
3927a9063ab9STao Ma 	di->i_gid = s_di->i_gid;
3928a9063ab9STao Ma 	di->i_mode = s_di->i_mode;
3929a9063ab9STao Ma 
3930a9063ab9STao Ma 	/*
3931a9063ab9STao Ma 	 * update time.
3932a9063ab9STao Ma 	 * we want mtime to appear identical to the source and update ctime.
3933a9063ab9STao Ma 	 */
3934a9063ab9STao Ma 	t_inode->i_ctime = CURRENT_TIME;
3935a9063ab9STao Ma 
3936a9063ab9STao Ma 	di->i_ctime = cpu_to_le64(t_inode->i_ctime.tv_sec);
3937a9063ab9STao Ma 	di->i_ctime_nsec = cpu_to_le32(t_inode->i_ctime.tv_nsec);
3938a9063ab9STao Ma 
3939a9063ab9STao Ma 	t_inode->i_mtime = s_inode->i_mtime;
3940a9063ab9STao Ma 	di->i_mtime = s_di->i_mtime;
3941a9063ab9STao Ma 	di->i_mtime_nsec = s_di->i_mtime_nsec;
3942a9063ab9STao Ma 
3943a9063ab9STao Ma 	ocfs2_journal_dirty(handle, t_bh);
3944a9063ab9STao Ma 
3945a9063ab9STao Ma out_commit:
3946a9063ab9STao Ma 	ocfs2_commit_trans(OCFS2_SB(t_inode->i_sb), handle);
3947a9063ab9STao Ma 	return ret;
3948a9063ab9STao Ma }
3949a9063ab9STao Ma 
3950110a045aSTao Ma static int ocfs2_create_reflink_node(struct inode *s_inode,
3951110a045aSTao Ma 				     struct buffer_head *s_bh,
3952110a045aSTao Ma 				     struct inode *t_inode,
3953110a045aSTao Ma 				     struct buffer_head *t_bh)
3954110a045aSTao Ma {
3955110a045aSTao Ma 	int ret;
3956110a045aSTao Ma 	struct buffer_head *ref_root_bh = NULL;
3957110a045aSTao Ma 	struct ocfs2_cached_dealloc_ctxt dealloc;
3958110a045aSTao Ma 	struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb);
3959110a045aSTao Ma 	struct ocfs2_refcount_block *rb;
3960110a045aSTao Ma 	struct ocfs2_dinode *di = (struct ocfs2_dinode *)s_bh->b_data;
3961110a045aSTao Ma 	struct ocfs2_refcount_tree *ref_tree;
3962110a045aSTao Ma 
3963110a045aSTao Ma 	ocfs2_init_dealloc_ctxt(&dealloc);
3964110a045aSTao Ma 
3965110a045aSTao Ma 	ret = ocfs2_set_refcount_tree(t_inode, t_bh,
3966110a045aSTao Ma 				      le64_to_cpu(di->i_refcount_loc));
3967110a045aSTao Ma 	if (ret) {
3968110a045aSTao Ma 		mlog_errno(ret);
3969110a045aSTao Ma 		goto out;
3970110a045aSTao Ma 	}
3971110a045aSTao Ma 
3972110a045aSTao Ma 	ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
3973110a045aSTao Ma 				       1, &ref_tree, &ref_root_bh);
3974110a045aSTao Ma 	if (ret) {
3975110a045aSTao Ma 		mlog_errno(ret);
3976110a045aSTao Ma 		goto out;
3977110a045aSTao Ma 	}
3978110a045aSTao Ma 	rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
3979110a045aSTao Ma 
3980110a045aSTao Ma 	ret = ocfs2_duplicate_extent_list(s_inode, t_inode, t_bh,
3981110a045aSTao Ma 					  &ref_tree->rf_ci, ref_root_bh,
3982110a045aSTao Ma 					  &dealloc);
3983a9063ab9STao Ma 	if (ret) {
3984a9063ab9STao Ma 		mlog_errno(ret);
3985a9063ab9STao Ma 		goto out_unlock_refcount;
3986a9063ab9STao Ma 	}
3987a9063ab9STao Ma 
3988a9063ab9STao Ma 	ret = ocfs2_complete_reflink(s_inode, s_bh, t_inode, t_bh);
3989110a045aSTao Ma 	if (ret)
3990110a045aSTao Ma 		mlog_errno(ret);
3991110a045aSTao Ma 
3992a9063ab9STao Ma out_unlock_refcount:
3993110a045aSTao Ma 	ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3994110a045aSTao Ma 	brelse(ref_root_bh);
3995110a045aSTao Ma out:
3996110a045aSTao Ma 	if (ocfs2_dealloc_has_cluster(&dealloc)) {
3997110a045aSTao Ma 		ocfs2_schedule_truncate_log_flush(osb, 1);
3998110a045aSTao Ma 		ocfs2_run_deallocs(osb, &dealloc);
3999110a045aSTao Ma 	}
4000110a045aSTao Ma 
4001110a045aSTao Ma 	return ret;
4002110a045aSTao Ma }
4003