1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /* -*- mode: c; c-basic-offset: 8; -*-
3*4882a593Smuzhiyun * vim: noexpandtab sw=8 ts=8 sts=0:
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * refcounttree.c
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Copyright (C) 2009 Oracle. All rights reserved.
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/sort.h>
11*4882a593Smuzhiyun #include <cluster/masklog.h>
12*4882a593Smuzhiyun #include "ocfs2.h"
13*4882a593Smuzhiyun #include "inode.h"
14*4882a593Smuzhiyun #include "alloc.h"
15*4882a593Smuzhiyun #include "suballoc.h"
16*4882a593Smuzhiyun #include "journal.h"
17*4882a593Smuzhiyun #include "uptodate.h"
18*4882a593Smuzhiyun #include "super.h"
19*4882a593Smuzhiyun #include "buffer_head_io.h"
20*4882a593Smuzhiyun #include "blockcheck.h"
21*4882a593Smuzhiyun #include "refcounttree.h"
22*4882a593Smuzhiyun #include "sysfile.h"
23*4882a593Smuzhiyun #include "dlmglue.h"
24*4882a593Smuzhiyun #include "extent_map.h"
25*4882a593Smuzhiyun #include "aops.h"
26*4882a593Smuzhiyun #include "xattr.h"
27*4882a593Smuzhiyun #include "namei.h"
28*4882a593Smuzhiyun #include "ocfs2_trace.h"
29*4882a593Smuzhiyun #include "file.h"
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #include <linux/bio.h>
32*4882a593Smuzhiyun #include <linux/blkdev.h>
33*4882a593Smuzhiyun #include <linux/slab.h>
34*4882a593Smuzhiyun #include <linux/writeback.h>
35*4882a593Smuzhiyun #include <linux/pagevec.h>
36*4882a593Smuzhiyun #include <linux/swap.h>
37*4882a593Smuzhiyun #include <linux/security.h>
38*4882a593Smuzhiyun #include <linux/fsnotify.h>
39*4882a593Smuzhiyun #include <linux/quotaops.h>
40*4882a593Smuzhiyun #include <linux/namei.h>
41*4882a593Smuzhiyun #include <linux/mount.h>
42*4882a593Smuzhiyun #include <linux/posix_acl.h>
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun struct ocfs2_cow_context {
45*4882a593Smuzhiyun struct inode *inode;
46*4882a593Smuzhiyun u32 cow_start;
47*4882a593Smuzhiyun u32 cow_len;
48*4882a593Smuzhiyun struct ocfs2_extent_tree data_et;
49*4882a593Smuzhiyun struct ocfs2_refcount_tree *ref_tree;
50*4882a593Smuzhiyun struct buffer_head *ref_root_bh;
51*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac;
52*4882a593Smuzhiyun struct ocfs2_alloc_context *data_ac;
53*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt dealloc;
54*4882a593Smuzhiyun void *cow_object;
55*4882a593Smuzhiyun struct ocfs2_post_refcount *post_refcount;
56*4882a593Smuzhiyun int extra_credits;
57*4882a593Smuzhiyun int (*get_clusters)(struct ocfs2_cow_context *context,
58*4882a593Smuzhiyun u32 v_cluster, u32 *p_cluster,
59*4882a593Smuzhiyun u32 *num_clusters,
60*4882a593Smuzhiyun unsigned int *extent_flags);
61*4882a593Smuzhiyun int (*cow_duplicate_clusters)(handle_t *handle,
62*4882a593Smuzhiyun struct inode *inode,
63*4882a593Smuzhiyun u32 cpos, u32 old_cluster,
64*4882a593Smuzhiyun u32 new_cluster, u32 new_len);
65*4882a593Smuzhiyun };
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun static inline struct ocfs2_refcount_tree *
cache_info_to_refcount(struct ocfs2_caching_info * ci)68*4882a593Smuzhiyun cache_info_to_refcount(struct ocfs2_caching_info *ci)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun return container_of(ci, struct ocfs2_refcount_tree, rf_ci);
71*4882a593Smuzhiyun }
72*4882a593Smuzhiyun
ocfs2_validate_refcount_block(struct super_block * sb,struct buffer_head * bh)73*4882a593Smuzhiyun static int ocfs2_validate_refcount_block(struct super_block *sb,
74*4882a593Smuzhiyun struct buffer_head *bh)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun int rc;
77*4882a593Smuzhiyun struct ocfs2_refcount_block *rb =
78*4882a593Smuzhiyun (struct ocfs2_refcount_block *)bh->b_data;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun trace_ocfs2_validate_refcount_block((unsigned long long)bh->b_blocknr);
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun BUG_ON(!buffer_uptodate(bh));
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun /*
85*4882a593Smuzhiyun * If the ecc fails, we return the error but otherwise
86*4882a593Smuzhiyun * leave the filesystem running. We know any error is
87*4882a593Smuzhiyun * local to this block.
88*4882a593Smuzhiyun */
89*4882a593Smuzhiyun rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &rb->rf_check);
90*4882a593Smuzhiyun if (rc) {
91*4882a593Smuzhiyun mlog(ML_ERROR, "Checksum failed for refcount block %llu\n",
92*4882a593Smuzhiyun (unsigned long long)bh->b_blocknr);
93*4882a593Smuzhiyun return rc;
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun if (!OCFS2_IS_VALID_REFCOUNT_BLOCK(rb)) {
98*4882a593Smuzhiyun rc = ocfs2_error(sb,
99*4882a593Smuzhiyun "Refcount block #%llu has bad signature %.*s\n",
100*4882a593Smuzhiyun (unsigned long long)bh->b_blocknr, 7,
101*4882a593Smuzhiyun rb->rf_signature);
102*4882a593Smuzhiyun goto out;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun if (le64_to_cpu(rb->rf_blkno) != bh->b_blocknr) {
106*4882a593Smuzhiyun rc = ocfs2_error(sb,
107*4882a593Smuzhiyun "Refcount block #%llu has an invalid rf_blkno of %llu\n",
108*4882a593Smuzhiyun (unsigned long long)bh->b_blocknr,
109*4882a593Smuzhiyun (unsigned long long)le64_to_cpu(rb->rf_blkno));
110*4882a593Smuzhiyun goto out;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun if (le32_to_cpu(rb->rf_fs_generation) != OCFS2_SB(sb)->fs_generation) {
114*4882a593Smuzhiyun rc = ocfs2_error(sb,
115*4882a593Smuzhiyun "Refcount block #%llu has an invalid rf_fs_generation of #%u\n",
116*4882a593Smuzhiyun (unsigned long long)bh->b_blocknr,
117*4882a593Smuzhiyun le32_to_cpu(rb->rf_fs_generation));
118*4882a593Smuzhiyun goto out;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun out:
121*4882a593Smuzhiyun return rc;
122*4882a593Smuzhiyun }
123*4882a593Smuzhiyun
ocfs2_read_refcount_block(struct ocfs2_caching_info * ci,u64 rb_blkno,struct buffer_head ** bh)124*4882a593Smuzhiyun static int ocfs2_read_refcount_block(struct ocfs2_caching_info *ci,
125*4882a593Smuzhiyun u64 rb_blkno,
126*4882a593Smuzhiyun struct buffer_head **bh)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun int rc;
129*4882a593Smuzhiyun struct buffer_head *tmp = *bh;
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun rc = ocfs2_read_block(ci, rb_blkno, &tmp,
132*4882a593Smuzhiyun ocfs2_validate_refcount_block);
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun /* If ocfs2_read_block() got us a new bh, pass it up. */
135*4882a593Smuzhiyun if (!rc && !*bh)
136*4882a593Smuzhiyun *bh = tmp;
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun return rc;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
ocfs2_refcount_cache_owner(struct ocfs2_caching_info * ci)141*4882a593Smuzhiyun static u64 ocfs2_refcount_cache_owner(struct ocfs2_caching_info *ci)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun return rf->rf_blkno;
146*4882a593Smuzhiyun }
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun static struct super_block *
ocfs2_refcount_cache_get_super(struct ocfs2_caching_info * ci)149*4882a593Smuzhiyun ocfs2_refcount_cache_get_super(struct ocfs2_caching_info *ci)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun return rf->rf_sb;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
ocfs2_refcount_cache_lock(struct ocfs2_caching_info * ci)156*4882a593Smuzhiyun static void ocfs2_refcount_cache_lock(struct ocfs2_caching_info *ci)
157*4882a593Smuzhiyun __acquires(&rf->rf_lock)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun spin_lock(&rf->rf_lock);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
ocfs2_refcount_cache_unlock(struct ocfs2_caching_info * ci)164*4882a593Smuzhiyun static void ocfs2_refcount_cache_unlock(struct ocfs2_caching_info *ci)
165*4882a593Smuzhiyun __releases(&rf->rf_lock)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun spin_unlock(&rf->rf_lock);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
ocfs2_refcount_cache_io_lock(struct ocfs2_caching_info * ci)172*4882a593Smuzhiyun static void ocfs2_refcount_cache_io_lock(struct ocfs2_caching_info *ci)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun mutex_lock(&rf->rf_io_mutex);
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun
ocfs2_refcount_cache_io_unlock(struct ocfs2_caching_info * ci)179*4882a593Smuzhiyun static void ocfs2_refcount_cache_io_unlock(struct ocfs2_caching_info *ci)
180*4882a593Smuzhiyun {
181*4882a593Smuzhiyun struct ocfs2_refcount_tree *rf = cache_info_to_refcount(ci);
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun mutex_unlock(&rf->rf_io_mutex);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun static const struct ocfs2_caching_operations ocfs2_refcount_caching_ops = {
187*4882a593Smuzhiyun .co_owner = ocfs2_refcount_cache_owner,
188*4882a593Smuzhiyun .co_get_super = ocfs2_refcount_cache_get_super,
189*4882a593Smuzhiyun .co_cache_lock = ocfs2_refcount_cache_lock,
190*4882a593Smuzhiyun .co_cache_unlock = ocfs2_refcount_cache_unlock,
191*4882a593Smuzhiyun .co_io_lock = ocfs2_refcount_cache_io_lock,
192*4882a593Smuzhiyun .co_io_unlock = ocfs2_refcount_cache_io_unlock,
193*4882a593Smuzhiyun };
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun static struct ocfs2_refcount_tree *
ocfs2_find_refcount_tree(struct ocfs2_super * osb,u64 blkno)196*4882a593Smuzhiyun ocfs2_find_refcount_tree(struct ocfs2_super *osb, u64 blkno)
197*4882a593Smuzhiyun {
198*4882a593Smuzhiyun struct rb_node *n = osb->osb_rf_lock_tree.rb_node;
199*4882a593Smuzhiyun struct ocfs2_refcount_tree *tree = NULL;
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun while (n) {
202*4882a593Smuzhiyun tree = rb_entry(n, struct ocfs2_refcount_tree, rf_node);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun if (blkno < tree->rf_blkno)
205*4882a593Smuzhiyun n = n->rb_left;
206*4882a593Smuzhiyun else if (blkno > tree->rf_blkno)
207*4882a593Smuzhiyun n = n->rb_right;
208*4882a593Smuzhiyun else
209*4882a593Smuzhiyun return tree;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun return NULL;
213*4882a593Smuzhiyun }
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /* osb_lock is already locked. */
ocfs2_insert_refcount_tree(struct ocfs2_super * osb,struct ocfs2_refcount_tree * new)216*4882a593Smuzhiyun static void ocfs2_insert_refcount_tree(struct ocfs2_super *osb,
217*4882a593Smuzhiyun struct ocfs2_refcount_tree *new)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun u64 rf_blkno = new->rf_blkno;
220*4882a593Smuzhiyun struct rb_node *parent = NULL;
221*4882a593Smuzhiyun struct rb_node **p = &osb->osb_rf_lock_tree.rb_node;
222*4882a593Smuzhiyun struct ocfs2_refcount_tree *tmp;
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun while (*p) {
225*4882a593Smuzhiyun parent = *p;
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun tmp = rb_entry(parent, struct ocfs2_refcount_tree,
228*4882a593Smuzhiyun rf_node);
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun if (rf_blkno < tmp->rf_blkno)
231*4882a593Smuzhiyun p = &(*p)->rb_left;
232*4882a593Smuzhiyun else if (rf_blkno > tmp->rf_blkno)
233*4882a593Smuzhiyun p = &(*p)->rb_right;
234*4882a593Smuzhiyun else {
235*4882a593Smuzhiyun /* This should never happen! */
236*4882a593Smuzhiyun mlog(ML_ERROR, "Duplicate refcount block %llu found!\n",
237*4882a593Smuzhiyun (unsigned long long)rf_blkno);
238*4882a593Smuzhiyun BUG();
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun rb_link_node(&new->rf_node, parent, p);
243*4882a593Smuzhiyun rb_insert_color(&new->rf_node, &osb->osb_rf_lock_tree);
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
ocfs2_free_refcount_tree(struct ocfs2_refcount_tree * tree)246*4882a593Smuzhiyun static void ocfs2_free_refcount_tree(struct ocfs2_refcount_tree *tree)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun ocfs2_metadata_cache_exit(&tree->rf_ci);
249*4882a593Smuzhiyun ocfs2_simple_drop_lockres(OCFS2_SB(tree->rf_sb), &tree->rf_lockres);
250*4882a593Smuzhiyun ocfs2_lock_res_free(&tree->rf_lockres);
251*4882a593Smuzhiyun kfree(tree);
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun static inline void
ocfs2_erase_refcount_tree_from_list_no_lock(struct ocfs2_super * osb,struct ocfs2_refcount_tree * tree)255*4882a593Smuzhiyun ocfs2_erase_refcount_tree_from_list_no_lock(struct ocfs2_super *osb,
256*4882a593Smuzhiyun struct ocfs2_refcount_tree *tree)
257*4882a593Smuzhiyun {
258*4882a593Smuzhiyun rb_erase(&tree->rf_node, &osb->osb_rf_lock_tree);
259*4882a593Smuzhiyun if (osb->osb_ref_tree_lru && osb->osb_ref_tree_lru == tree)
260*4882a593Smuzhiyun osb->osb_ref_tree_lru = NULL;
261*4882a593Smuzhiyun }
262*4882a593Smuzhiyun
ocfs2_erase_refcount_tree_from_list(struct ocfs2_super * osb,struct ocfs2_refcount_tree * tree)263*4882a593Smuzhiyun static void ocfs2_erase_refcount_tree_from_list(struct ocfs2_super *osb,
264*4882a593Smuzhiyun struct ocfs2_refcount_tree *tree)
265*4882a593Smuzhiyun {
266*4882a593Smuzhiyun spin_lock(&osb->osb_lock);
267*4882a593Smuzhiyun ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
268*4882a593Smuzhiyun spin_unlock(&osb->osb_lock);
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
ocfs2_kref_remove_refcount_tree(struct kref * kref)271*4882a593Smuzhiyun static void ocfs2_kref_remove_refcount_tree(struct kref *kref)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun struct ocfs2_refcount_tree *tree =
274*4882a593Smuzhiyun container_of(kref, struct ocfs2_refcount_tree, rf_getcnt);
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun ocfs2_free_refcount_tree(tree);
277*4882a593Smuzhiyun }
278*4882a593Smuzhiyun
279*4882a593Smuzhiyun static inline void
ocfs2_refcount_tree_get(struct ocfs2_refcount_tree * tree)280*4882a593Smuzhiyun ocfs2_refcount_tree_get(struct ocfs2_refcount_tree *tree)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun kref_get(&tree->rf_getcnt);
283*4882a593Smuzhiyun }
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun static inline void
ocfs2_refcount_tree_put(struct ocfs2_refcount_tree * tree)286*4882a593Smuzhiyun ocfs2_refcount_tree_put(struct ocfs2_refcount_tree *tree)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun kref_put(&tree->rf_getcnt, ocfs2_kref_remove_refcount_tree);
289*4882a593Smuzhiyun }
290*4882a593Smuzhiyun
ocfs2_init_refcount_tree_ci(struct ocfs2_refcount_tree * new,struct super_block * sb)291*4882a593Smuzhiyun static inline void ocfs2_init_refcount_tree_ci(struct ocfs2_refcount_tree *new,
292*4882a593Smuzhiyun struct super_block *sb)
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun ocfs2_metadata_cache_init(&new->rf_ci, &ocfs2_refcount_caching_ops);
295*4882a593Smuzhiyun mutex_init(&new->rf_io_mutex);
296*4882a593Smuzhiyun new->rf_sb = sb;
297*4882a593Smuzhiyun spin_lock_init(&new->rf_lock);
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
ocfs2_init_refcount_tree_lock(struct ocfs2_super * osb,struct ocfs2_refcount_tree * new,u64 rf_blkno,u32 generation)300*4882a593Smuzhiyun static inline void ocfs2_init_refcount_tree_lock(struct ocfs2_super *osb,
301*4882a593Smuzhiyun struct ocfs2_refcount_tree *new,
302*4882a593Smuzhiyun u64 rf_blkno, u32 generation)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun init_rwsem(&new->rf_sem);
305*4882a593Smuzhiyun ocfs2_refcount_lock_res_init(&new->rf_lockres, osb,
306*4882a593Smuzhiyun rf_blkno, generation);
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun static struct ocfs2_refcount_tree*
ocfs2_allocate_refcount_tree(struct ocfs2_super * osb,u64 rf_blkno)310*4882a593Smuzhiyun ocfs2_allocate_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno)
311*4882a593Smuzhiyun {
312*4882a593Smuzhiyun struct ocfs2_refcount_tree *new;
313*4882a593Smuzhiyun
314*4882a593Smuzhiyun new = kzalloc(sizeof(struct ocfs2_refcount_tree), GFP_NOFS);
315*4882a593Smuzhiyun if (!new)
316*4882a593Smuzhiyun return NULL;
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun new->rf_blkno = rf_blkno;
319*4882a593Smuzhiyun kref_init(&new->rf_getcnt);
320*4882a593Smuzhiyun ocfs2_init_refcount_tree_ci(new, osb->sb);
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun return new;
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun
ocfs2_get_refcount_tree(struct ocfs2_super * osb,u64 rf_blkno,struct ocfs2_refcount_tree ** ret_tree)325*4882a593Smuzhiyun static int ocfs2_get_refcount_tree(struct ocfs2_super *osb, u64 rf_blkno,
326*4882a593Smuzhiyun struct ocfs2_refcount_tree **ret_tree)
327*4882a593Smuzhiyun {
328*4882a593Smuzhiyun int ret = 0;
329*4882a593Smuzhiyun struct ocfs2_refcount_tree *tree, *new = NULL;
330*4882a593Smuzhiyun struct buffer_head *ref_root_bh = NULL;
331*4882a593Smuzhiyun struct ocfs2_refcount_block *ref_rb;
332*4882a593Smuzhiyun
333*4882a593Smuzhiyun spin_lock(&osb->osb_lock);
334*4882a593Smuzhiyun if (osb->osb_ref_tree_lru &&
335*4882a593Smuzhiyun osb->osb_ref_tree_lru->rf_blkno == rf_blkno)
336*4882a593Smuzhiyun tree = osb->osb_ref_tree_lru;
337*4882a593Smuzhiyun else
338*4882a593Smuzhiyun tree = ocfs2_find_refcount_tree(osb, rf_blkno);
339*4882a593Smuzhiyun if (tree)
340*4882a593Smuzhiyun goto out;
341*4882a593Smuzhiyun
342*4882a593Smuzhiyun spin_unlock(&osb->osb_lock);
343*4882a593Smuzhiyun
344*4882a593Smuzhiyun new = ocfs2_allocate_refcount_tree(osb, rf_blkno);
345*4882a593Smuzhiyun if (!new) {
346*4882a593Smuzhiyun ret = -ENOMEM;
347*4882a593Smuzhiyun mlog_errno(ret);
348*4882a593Smuzhiyun return ret;
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun /*
351*4882a593Smuzhiyun * We need the generation to create the refcount tree lock and since
352*4882a593Smuzhiyun * it isn't changed during the tree modification, we are safe here to
353*4882a593Smuzhiyun * read without protection.
354*4882a593Smuzhiyun * We also have to purge the cache after we create the lock since the
355*4882a593Smuzhiyun * refcount block may have the stale data. It can only be trusted when
356*4882a593Smuzhiyun * we hold the refcount lock.
357*4882a593Smuzhiyun */
358*4882a593Smuzhiyun ret = ocfs2_read_refcount_block(&new->rf_ci, rf_blkno, &ref_root_bh);
359*4882a593Smuzhiyun if (ret) {
360*4882a593Smuzhiyun mlog_errno(ret);
361*4882a593Smuzhiyun ocfs2_metadata_cache_exit(&new->rf_ci);
362*4882a593Smuzhiyun kfree(new);
363*4882a593Smuzhiyun return ret;
364*4882a593Smuzhiyun }
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun ref_rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
367*4882a593Smuzhiyun new->rf_generation = le32_to_cpu(ref_rb->rf_generation);
368*4882a593Smuzhiyun ocfs2_init_refcount_tree_lock(osb, new, rf_blkno,
369*4882a593Smuzhiyun new->rf_generation);
370*4882a593Smuzhiyun ocfs2_metadata_cache_purge(&new->rf_ci);
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun spin_lock(&osb->osb_lock);
373*4882a593Smuzhiyun tree = ocfs2_find_refcount_tree(osb, rf_blkno);
374*4882a593Smuzhiyun if (tree)
375*4882a593Smuzhiyun goto out;
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun ocfs2_insert_refcount_tree(osb, new);
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun tree = new;
380*4882a593Smuzhiyun new = NULL;
381*4882a593Smuzhiyun
382*4882a593Smuzhiyun out:
383*4882a593Smuzhiyun *ret_tree = tree;
384*4882a593Smuzhiyun
385*4882a593Smuzhiyun osb->osb_ref_tree_lru = tree;
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun spin_unlock(&osb->osb_lock);
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun if (new)
390*4882a593Smuzhiyun ocfs2_free_refcount_tree(new);
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun brelse(ref_root_bh);
393*4882a593Smuzhiyun return ret;
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun
ocfs2_get_refcount_block(struct inode * inode,u64 * ref_blkno)396*4882a593Smuzhiyun static int ocfs2_get_refcount_block(struct inode *inode, u64 *ref_blkno)
397*4882a593Smuzhiyun {
398*4882a593Smuzhiyun int ret;
399*4882a593Smuzhiyun struct buffer_head *di_bh = NULL;
400*4882a593Smuzhiyun struct ocfs2_dinode *di;
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun ret = ocfs2_read_inode_block(inode, &di_bh);
403*4882a593Smuzhiyun if (ret) {
404*4882a593Smuzhiyun mlog_errno(ret);
405*4882a593Smuzhiyun goto out;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
408*4882a593Smuzhiyun BUG_ON(!ocfs2_is_refcount_inode(inode));
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun di = (struct ocfs2_dinode *)di_bh->b_data;
411*4882a593Smuzhiyun *ref_blkno = le64_to_cpu(di->i_refcount_loc);
412*4882a593Smuzhiyun brelse(di_bh);
413*4882a593Smuzhiyun out:
414*4882a593Smuzhiyun return ret;
415*4882a593Smuzhiyun }
416*4882a593Smuzhiyun
__ocfs2_lock_refcount_tree(struct ocfs2_super * osb,struct ocfs2_refcount_tree * tree,int rw)417*4882a593Smuzhiyun static int __ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
418*4882a593Smuzhiyun struct ocfs2_refcount_tree *tree, int rw)
419*4882a593Smuzhiyun {
420*4882a593Smuzhiyun int ret;
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun ret = ocfs2_refcount_lock(tree, rw);
423*4882a593Smuzhiyun if (ret) {
424*4882a593Smuzhiyun mlog_errno(ret);
425*4882a593Smuzhiyun goto out;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun
428*4882a593Smuzhiyun if (rw)
429*4882a593Smuzhiyun down_write(&tree->rf_sem);
430*4882a593Smuzhiyun else
431*4882a593Smuzhiyun down_read(&tree->rf_sem);
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun out:
434*4882a593Smuzhiyun return ret;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun /*
438*4882a593Smuzhiyun * Lock the refcount tree pointed by ref_blkno and return the tree.
439*4882a593Smuzhiyun * In most case, we lock the tree and read the refcount block.
440*4882a593Smuzhiyun * So read it here if the caller really needs it.
441*4882a593Smuzhiyun *
442*4882a593Smuzhiyun * If the tree has been re-created by other node, it will free the
443*4882a593Smuzhiyun * old one and re-create it.
444*4882a593Smuzhiyun */
ocfs2_lock_refcount_tree(struct ocfs2_super * osb,u64 ref_blkno,int rw,struct ocfs2_refcount_tree ** ret_tree,struct buffer_head ** ref_bh)445*4882a593Smuzhiyun int ocfs2_lock_refcount_tree(struct ocfs2_super *osb,
446*4882a593Smuzhiyun u64 ref_blkno, int rw,
447*4882a593Smuzhiyun struct ocfs2_refcount_tree **ret_tree,
448*4882a593Smuzhiyun struct buffer_head **ref_bh)
449*4882a593Smuzhiyun {
450*4882a593Smuzhiyun int ret, delete_tree = 0;
451*4882a593Smuzhiyun struct ocfs2_refcount_tree *tree = NULL;
452*4882a593Smuzhiyun struct buffer_head *ref_root_bh = NULL;
453*4882a593Smuzhiyun struct ocfs2_refcount_block *rb;
454*4882a593Smuzhiyun
455*4882a593Smuzhiyun again:
456*4882a593Smuzhiyun ret = ocfs2_get_refcount_tree(osb, ref_blkno, &tree);
457*4882a593Smuzhiyun if (ret) {
458*4882a593Smuzhiyun mlog_errno(ret);
459*4882a593Smuzhiyun return ret;
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun
462*4882a593Smuzhiyun ocfs2_refcount_tree_get(tree);
463*4882a593Smuzhiyun
464*4882a593Smuzhiyun ret = __ocfs2_lock_refcount_tree(osb, tree, rw);
465*4882a593Smuzhiyun if (ret) {
466*4882a593Smuzhiyun mlog_errno(ret);
467*4882a593Smuzhiyun ocfs2_refcount_tree_put(tree);
468*4882a593Smuzhiyun goto out;
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
472*4882a593Smuzhiyun &ref_root_bh);
473*4882a593Smuzhiyun if (ret) {
474*4882a593Smuzhiyun mlog_errno(ret);
475*4882a593Smuzhiyun ocfs2_unlock_refcount_tree(osb, tree, rw);
476*4882a593Smuzhiyun goto out;
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun
479*4882a593Smuzhiyun rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
480*4882a593Smuzhiyun /*
481*4882a593Smuzhiyun * If the refcount block has been freed and re-created, we may need
482*4882a593Smuzhiyun * to recreate the refcount tree also.
483*4882a593Smuzhiyun *
484*4882a593Smuzhiyun * Here we just remove the tree from the rb-tree, and the last
485*4882a593Smuzhiyun * kref holder will unlock and delete this refcount_tree.
486*4882a593Smuzhiyun * Then we goto "again" and ocfs2_get_refcount_tree will create
487*4882a593Smuzhiyun * the new refcount tree for us.
488*4882a593Smuzhiyun */
489*4882a593Smuzhiyun if (tree->rf_generation != le32_to_cpu(rb->rf_generation)) {
490*4882a593Smuzhiyun if (!tree->rf_removed) {
491*4882a593Smuzhiyun ocfs2_erase_refcount_tree_from_list(osb, tree);
492*4882a593Smuzhiyun tree->rf_removed = 1;
493*4882a593Smuzhiyun delete_tree = 1;
494*4882a593Smuzhiyun }
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun ocfs2_unlock_refcount_tree(osb, tree, rw);
497*4882a593Smuzhiyun /*
498*4882a593Smuzhiyun * We get an extra reference when we create the refcount
499*4882a593Smuzhiyun * tree, so another put will destroy it.
500*4882a593Smuzhiyun */
501*4882a593Smuzhiyun if (delete_tree)
502*4882a593Smuzhiyun ocfs2_refcount_tree_put(tree);
503*4882a593Smuzhiyun brelse(ref_root_bh);
504*4882a593Smuzhiyun ref_root_bh = NULL;
505*4882a593Smuzhiyun goto again;
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun *ret_tree = tree;
509*4882a593Smuzhiyun if (ref_bh) {
510*4882a593Smuzhiyun *ref_bh = ref_root_bh;
511*4882a593Smuzhiyun ref_root_bh = NULL;
512*4882a593Smuzhiyun }
513*4882a593Smuzhiyun out:
514*4882a593Smuzhiyun brelse(ref_root_bh);
515*4882a593Smuzhiyun return ret;
516*4882a593Smuzhiyun }
517*4882a593Smuzhiyun
ocfs2_unlock_refcount_tree(struct ocfs2_super * osb,struct ocfs2_refcount_tree * tree,int rw)518*4882a593Smuzhiyun void ocfs2_unlock_refcount_tree(struct ocfs2_super *osb,
519*4882a593Smuzhiyun struct ocfs2_refcount_tree *tree, int rw)
520*4882a593Smuzhiyun {
521*4882a593Smuzhiyun if (rw)
522*4882a593Smuzhiyun up_write(&tree->rf_sem);
523*4882a593Smuzhiyun else
524*4882a593Smuzhiyun up_read(&tree->rf_sem);
525*4882a593Smuzhiyun
526*4882a593Smuzhiyun ocfs2_refcount_unlock(tree, rw);
527*4882a593Smuzhiyun ocfs2_refcount_tree_put(tree);
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun
ocfs2_purge_refcount_trees(struct ocfs2_super * osb)530*4882a593Smuzhiyun void ocfs2_purge_refcount_trees(struct ocfs2_super *osb)
531*4882a593Smuzhiyun {
532*4882a593Smuzhiyun struct rb_node *node;
533*4882a593Smuzhiyun struct ocfs2_refcount_tree *tree;
534*4882a593Smuzhiyun struct rb_root *root = &osb->osb_rf_lock_tree;
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun while ((node = rb_last(root)) != NULL) {
537*4882a593Smuzhiyun tree = rb_entry(node, struct ocfs2_refcount_tree, rf_node);
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun trace_ocfs2_purge_refcount_trees(
540*4882a593Smuzhiyun (unsigned long long) tree->rf_blkno);
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun rb_erase(&tree->rf_node, root);
543*4882a593Smuzhiyun ocfs2_free_refcount_tree(tree);
544*4882a593Smuzhiyun }
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun
547*4882a593Smuzhiyun /*
548*4882a593Smuzhiyun * Create a refcount tree for an inode.
549*4882a593Smuzhiyun * We take for granted that the inode is already locked.
550*4882a593Smuzhiyun */
ocfs2_create_refcount_tree(struct inode * inode,struct buffer_head * di_bh)551*4882a593Smuzhiyun static int ocfs2_create_refcount_tree(struct inode *inode,
552*4882a593Smuzhiyun struct buffer_head *di_bh)
553*4882a593Smuzhiyun {
554*4882a593Smuzhiyun int ret;
555*4882a593Smuzhiyun handle_t *handle = NULL;
556*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac = NULL;
557*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
558*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(inode);
559*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
560*4882a593Smuzhiyun struct buffer_head *new_bh = NULL;
561*4882a593Smuzhiyun struct ocfs2_refcount_block *rb;
562*4882a593Smuzhiyun struct ocfs2_refcount_tree *new_tree = NULL, *tree = NULL;
563*4882a593Smuzhiyun u16 suballoc_bit_start;
564*4882a593Smuzhiyun u32 num_got;
565*4882a593Smuzhiyun u64 suballoc_loc, first_blkno;
566*4882a593Smuzhiyun
567*4882a593Smuzhiyun BUG_ON(ocfs2_is_refcount_inode(inode));
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun trace_ocfs2_create_refcount_tree(
570*4882a593Smuzhiyun (unsigned long long)oi->ip_blkno);
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun ret = ocfs2_reserve_new_metadata_blocks(osb, 1, &meta_ac);
573*4882a593Smuzhiyun if (ret) {
574*4882a593Smuzhiyun mlog_errno(ret);
575*4882a593Smuzhiyun goto out;
576*4882a593Smuzhiyun }
577*4882a593Smuzhiyun
578*4882a593Smuzhiyun handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_CREATE_CREDITS);
579*4882a593Smuzhiyun if (IS_ERR(handle)) {
580*4882a593Smuzhiyun ret = PTR_ERR(handle);
581*4882a593Smuzhiyun mlog_errno(ret);
582*4882a593Smuzhiyun goto out;
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun
585*4882a593Smuzhiyun ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
586*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
587*4882a593Smuzhiyun if (ret) {
588*4882a593Smuzhiyun mlog_errno(ret);
589*4882a593Smuzhiyun goto out_commit;
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun
592*4882a593Smuzhiyun ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
593*4882a593Smuzhiyun &suballoc_bit_start, &num_got,
594*4882a593Smuzhiyun &first_blkno);
595*4882a593Smuzhiyun if (ret) {
596*4882a593Smuzhiyun mlog_errno(ret);
597*4882a593Smuzhiyun goto out_commit;
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun new_tree = ocfs2_allocate_refcount_tree(osb, first_blkno);
601*4882a593Smuzhiyun if (!new_tree) {
602*4882a593Smuzhiyun ret = -ENOMEM;
603*4882a593Smuzhiyun mlog_errno(ret);
604*4882a593Smuzhiyun goto out_commit;
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun new_bh = sb_getblk(inode->i_sb, first_blkno);
608*4882a593Smuzhiyun if (!new_bh) {
609*4882a593Smuzhiyun ret = -ENOMEM;
610*4882a593Smuzhiyun mlog_errno(ret);
611*4882a593Smuzhiyun goto out_commit;
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun ocfs2_set_new_buffer_uptodate(&new_tree->rf_ci, new_bh);
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun ret = ocfs2_journal_access_rb(handle, &new_tree->rf_ci, new_bh,
616*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_CREATE);
617*4882a593Smuzhiyun if (ret) {
618*4882a593Smuzhiyun mlog_errno(ret);
619*4882a593Smuzhiyun goto out_commit;
620*4882a593Smuzhiyun }
621*4882a593Smuzhiyun
622*4882a593Smuzhiyun /* Initialize ocfs2_refcount_block. */
623*4882a593Smuzhiyun rb = (struct ocfs2_refcount_block *)new_bh->b_data;
624*4882a593Smuzhiyun memset(rb, 0, inode->i_sb->s_blocksize);
625*4882a593Smuzhiyun strcpy((void *)rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
626*4882a593Smuzhiyun rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
627*4882a593Smuzhiyun rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc);
628*4882a593Smuzhiyun rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
629*4882a593Smuzhiyun rb->rf_fs_generation = cpu_to_le32(osb->fs_generation);
630*4882a593Smuzhiyun rb->rf_blkno = cpu_to_le64(first_blkno);
631*4882a593Smuzhiyun rb->rf_count = cpu_to_le32(1);
632*4882a593Smuzhiyun rb->rf_records.rl_count =
633*4882a593Smuzhiyun cpu_to_le16(ocfs2_refcount_recs_per_rb(osb->sb));
634*4882a593Smuzhiyun spin_lock(&osb->osb_lock);
635*4882a593Smuzhiyun rb->rf_generation = osb->s_next_generation++;
636*4882a593Smuzhiyun spin_unlock(&osb->osb_lock);
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun ocfs2_journal_dirty(handle, new_bh);
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun spin_lock(&oi->ip_lock);
641*4882a593Smuzhiyun oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
642*4882a593Smuzhiyun di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
643*4882a593Smuzhiyun di->i_refcount_loc = cpu_to_le64(first_blkno);
644*4882a593Smuzhiyun spin_unlock(&oi->ip_lock);
645*4882a593Smuzhiyun
646*4882a593Smuzhiyun trace_ocfs2_create_refcount_tree_blkno((unsigned long long)first_blkno);
647*4882a593Smuzhiyun
648*4882a593Smuzhiyun ocfs2_journal_dirty(handle, di_bh);
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun /*
651*4882a593Smuzhiyun * We have to init the tree lock here since it will use
652*4882a593Smuzhiyun * the generation number to create it.
653*4882a593Smuzhiyun */
654*4882a593Smuzhiyun new_tree->rf_generation = le32_to_cpu(rb->rf_generation);
655*4882a593Smuzhiyun ocfs2_init_refcount_tree_lock(osb, new_tree, first_blkno,
656*4882a593Smuzhiyun new_tree->rf_generation);
657*4882a593Smuzhiyun
658*4882a593Smuzhiyun spin_lock(&osb->osb_lock);
659*4882a593Smuzhiyun tree = ocfs2_find_refcount_tree(osb, first_blkno);
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun /*
662*4882a593Smuzhiyun * We've just created a new refcount tree in this block. If
663*4882a593Smuzhiyun * we found a refcount tree on the ocfs2_super, it must be
664*4882a593Smuzhiyun * one we just deleted. We free the old tree before
665*4882a593Smuzhiyun * inserting the new tree.
666*4882a593Smuzhiyun */
667*4882a593Smuzhiyun BUG_ON(tree && tree->rf_generation == new_tree->rf_generation);
668*4882a593Smuzhiyun if (tree)
669*4882a593Smuzhiyun ocfs2_erase_refcount_tree_from_list_no_lock(osb, tree);
670*4882a593Smuzhiyun ocfs2_insert_refcount_tree(osb, new_tree);
671*4882a593Smuzhiyun spin_unlock(&osb->osb_lock);
672*4882a593Smuzhiyun new_tree = NULL;
673*4882a593Smuzhiyun if (tree)
674*4882a593Smuzhiyun ocfs2_refcount_tree_put(tree);
675*4882a593Smuzhiyun
676*4882a593Smuzhiyun out_commit:
677*4882a593Smuzhiyun ocfs2_commit_trans(osb, handle);
678*4882a593Smuzhiyun
679*4882a593Smuzhiyun out:
680*4882a593Smuzhiyun if (new_tree) {
681*4882a593Smuzhiyun ocfs2_metadata_cache_exit(&new_tree->rf_ci);
682*4882a593Smuzhiyun kfree(new_tree);
683*4882a593Smuzhiyun }
684*4882a593Smuzhiyun
685*4882a593Smuzhiyun brelse(new_bh);
686*4882a593Smuzhiyun if (meta_ac)
687*4882a593Smuzhiyun ocfs2_free_alloc_context(meta_ac);
688*4882a593Smuzhiyun
689*4882a593Smuzhiyun return ret;
690*4882a593Smuzhiyun }
691*4882a593Smuzhiyun
ocfs2_set_refcount_tree(struct inode * inode,struct buffer_head * di_bh,u64 refcount_loc)692*4882a593Smuzhiyun static int ocfs2_set_refcount_tree(struct inode *inode,
693*4882a593Smuzhiyun struct buffer_head *di_bh,
694*4882a593Smuzhiyun u64 refcount_loc)
695*4882a593Smuzhiyun {
696*4882a593Smuzhiyun int ret;
697*4882a593Smuzhiyun handle_t *handle = NULL;
698*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
699*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(inode);
700*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
701*4882a593Smuzhiyun struct buffer_head *ref_root_bh = NULL;
702*4882a593Smuzhiyun struct ocfs2_refcount_block *rb;
703*4882a593Smuzhiyun struct ocfs2_refcount_tree *ref_tree;
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun BUG_ON(ocfs2_is_refcount_inode(inode));
706*4882a593Smuzhiyun
707*4882a593Smuzhiyun ret = ocfs2_lock_refcount_tree(osb, refcount_loc, 1,
708*4882a593Smuzhiyun &ref_tree, &ref_root_bh);
709*4882a593Smuzhiyun if (ret) {
710*4882a593Smuzhiyun mlog_errno(ret);
711*4882a593Smuzhiyun return ret;
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun handle = ocfs2_start_trans(osb, OCFS2_REFCOUNT_TREE_SET_CREDITS);
715*4882a593Smuzhiyun if (IS_ERR(handle)) {
716*4882a593Smuzhiyun ret = PTR_ERR(handle);
717*4882a593Smuzhiyun mlog_errno(ret);
718*4882a593Smuzhiyun goto out;
719*4882a593Smuzhiyun }
720*4882a593Smuzhiyun
721*4882a593Smuzhiyun ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
722*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
723*4882a593Smuzhiyun if (ret) {
724*4882a593Smuzhiyun mlog_errno(ret);
725*4882a593Smuzhiyun goto out_commit;
726*4882a593Smuzhiyun }
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, ref_root_bh,
729*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
730*4882a593Smuzhiyun if (ret) {
731*4882a593Smuzhiyun mlog_errno(ret);
732*4882a593Smuzhiyun goto out_commit;
733*4882a593Smuzhiyun }
734*4882a593Smuzhiyun
735*4882a593Smuzhiyun rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
736*4882a593Smuzhiyun le32_add_cpu(&rb->rf_count, 1);
737*4882a593Smuzhiyun
738*4882a593Smuzhiyun ocfs2_journal_dirty(handle, ref_root_bh);
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun spin_lock(&oi->ip_lock);
741*4882a593Smuzhiyun oi->ip_dyn_features |= OCFS2_HAS_REFCOUNT_FL;
742*4882a593Smuzhiyun di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
743*4882a593Smuzhiyun di->i_refcount_loc = cpu_to_le64(refcount_loc);
744*4882a593Smuzhiyun spin_unlock(&oi->ip_lock);
745*4882a593Smuzhiyun ocfs2_journal_dirty(handle, di_bh);
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun out_commit:
748*4882a593Smuzhiyun ocfs2_commit_trans(osb, handle);
749*4882a593Smuzhiyun out:
750*4882a593Smuzhiyun ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
751*4882a593Smuzhiyun brelse(ref_root_bh);
752*4882a593Smuzhiyun
753*4882a593Smuzhiyun return ret;
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun
ocfs2_remove_refcount_tree(struct inode * inode,struct buffer_head * di_bh)756*4882a593Smuzhiyun int ocfs2_remove_refcount_tree(struct inode *inode, struct buffer_head *di_bh)
757*4882a593Smuzhiyun {
758*4882a593Smuzhiyun int ret, delete_tree = 0;
759*4882a593Smuzhiyun handle_t *handle = NULL;
760*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
761*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(inode);
762*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
763*4882a593Smuzhiyun struct ocfs2_refcount_block *rb;
764*4882a593Smuzhiyun struct inode *alloc_inode = NULL;
765*4882a593Smuzhiyun struct buffer_head *alloc_bh = NULL;
766*4882a593Smuzhiyun struct buffer_head *blk_bh = NULL;
767*4882a593Smuzhiyun struct ocfs2_refcount_tree *ref_tree;
768*4882a593Smuzhiyun int credits = OCFS2_REFCOUNT_TREE_REMOVE_CREDITS;
769*4882a593Smuzhiyun u64 blk = 0, bg_blkno = 0, ref_blkno = le64_to_cpu(di->i_refcount_loc);
770*4882a593Smuzhiyun u16 bit = 0;
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun if (!ocfs2_is_refcount_inode(inode))
773*4882a593Smuzhiyun return 0;
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun BUG_ON(!ref_blkno);
776*4882a593Smuzhiyun ret = ocfs2_lock_refcount_tree(osb, ref_blkno, 1, &ref_tree, &blk_bh);
777*4882a593Smuzhiyun if (ret) {
778*4882a593Smuzhiyun mlog_errno(ret);
779*4882a593Smuzhiyun return ret;
780*4882a593Smuzhiyun }
781*4882a593Smuzhiyun
782*4882a593Smuzhiyun rb = (struct ocfs2_refcount_block *)blk_bh->b_data;
783*4882a593Smuzhiyun
784*4882a593Smuzhiyun /*
785*4882a593Smuzhiyun * If we are the last user, we need to free the block.
786*4882a593Smuzhiyun * So lock the allocator ahead.
787*4882a593Smuzhiyun */
788*4882a593Smuzhiyun if (le32_to_cpu(rb->rf_count) == 1) {
789*4882a593Smuzhiyun blk = le64_to_cpu(rb->rf_blkno);
790*4882a593Smuzhiyun bit = le16_to_cpu(rb->rf_suballoc_bit);
791*4882a593Smuzhiyun if (rb->rf_suballoc_loc)
792*4882a593Smuzhiyun bg_blkno = le64_to_cpu(rb->rf_suballoc_loc);
793*4882a593Smuzhiyun else
794*4882a593Smuzhiyun bg_blkno = ocfs2_which_suballoc_group(blk, bit);
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun alloc_inode = ocfs2_get_system_file_inode(osb,
797*4882a593Smuzhiyun EXTENT_ALLOC_SYSTEM_INODE,
798*4882a593Smuzhiyun le16_to_cpu(rb->rf_suballoc_slot));
799*4882a593Smuzhiyun if (!alloc_inode) {
800*4882a593Smuzhiyun ret = -ENOMEM;
801*4882a593Smuzhiyun mlog_errno(ret);
802*4882a593Smuzhiyun goto out;
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun inode_lock(alloc_inode);
805*4882a593Smuzhiyun
806*4882a593Smuzhiyun ret = ocfs2_inode_lock(alloc_inode, &alloc_bh, 1);
807*4882a593Smuzhiyun if (ret) {
808*4882a593Smuzhiyun mlog_errno(ret);
809*4882a593Smuzhiyun goto out_mutex;
810*4882a593Smuzhiyun }
811*4882a593Smuzhiyun
812*4882a593Smuzhiyun credits += OCFS2_SUBALLOC_FREE;
813*4882a593Smuzhiyun }
814*4882a593Smuzhiyun
815*4882a593Smuzhiyun handle = ocfs2_start_trans(osb, credits);
816*4882a593Smuzhiyun if (IS_ERR(handle)) {
817*4882a593Smuzhiyun ret = PTR_ERR(handle);
818*4882a593Smuzhiyun mlog_errno(ret);
819*4882a593Smuzhiyun goto out_unlock;
820*4882a593Smuzhiyun }
821*4882a593Smuzhiyun
822*4882a593Smuzhiyun ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
823*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
824*4882a593Smuzhiyun if (ret) {
825*4882a593Smuzhiyun mlog_errno(ret);
826*4882a593Smuzhiyun goto out_commit;
827*4882a593Smuzhiyun }
828*4882a593Smuzhiyun
829*4882a593Smuzhiyun ret = ocfs2_journal_access_rb(handle, &ref_tree->rf_ci, blk_bh,
830*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
831*4882a593Smuzhiyun if (ret) {
832*4882a593Smuzhiyun mlog_errno(ret);
833*4882a593Smuzhiyun goto out_commit;
834*4882a593Smuzhiyun }
835*4882a593Smuzhiyun
836*4882a593Smuzhiyun spin_lock(&oi->ip_lock);
837*4882a593Smuzhiyun oi->ip_dyn_features &= ~OCFS2_HAS_REFCOUNT_FL;
838*4882a593Smuzhiyun di->i_dyn_features = cpu_to_le16(oi->ip_dyn_features);
839*4882a593Smuzhiyun di->i_refcount_loc = 0;
840*4882a593Smuzhiyun spin_unlock(&oi->ip_lock);
841*4882a593Smuzhiyun ocfs2_journal_dirty(handle, di_bh);
842*4882a593Smuzhiyun
843*4882a593Smuzhiyun le32_add_cpu(&rb->rf_count , -1);
844*4882a593Smuzhiyun ocfs2_journal_dirty(handle, blk_bh);
845*4882a593Smuzhiyun
846*4882a593Smuzhiyun if (!rb->rf_count) {
847*4882a593Smuzhiyun delete_tree = 1;
848*4882a593Smuzhiyun ocfs2_erase_refcount_tree_from_list(osb, ref_tree);
849*4882a593Smuzhiyun ret = ocfs2_free_suballoc_bits(handle, alloc_inode,
850*4882a593Smuzhiyun alloc_bh, bit, bg_blkno, 1);
851*4882a593Smuzhiyun if (ret)
852*4882a593Smuzhiyun mlog_errno(ret);
853*4882a593Smuzhiyun }
854*4882a593Smuzhiyun
855*4882a593Smuzhiyun out_commit:
856*4882a593Smuzhiyun ocfs2_commit_trans(osb, handle);
857*4882a593Smuzhiyun out_unlock:
858*4882a593Smuzhiyun if (alloc_inode) {
859*4882a593Smuzhiyun ocfs2_inode_unlock(alloc_inode, 1);
860*4882a593Smuzhiyun brelse(alloc_bh);
861*4882a593Smuzhiyun }
862*4882a593Smuzhiyun out_mutex:
863*4882a593Smuzhiyun if (alloc_inode) {
864*4882a593Smuzhiyun inode_unlock(alloc_inode);
865*4882a593Smuzhiyun iput(alloc_inode);
866*4882a593Smuzhiyun }
867*4882a593Smuzhiyun out:
868*4882a593Smuzhiyun ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
869*4882a593Smuzhiyun if (delete_tree)
870*4882a593Smuzhiyun ocfs2_refcount_tree_put(ref_tree);
871*4882a593Smuzhiyun brelse(blk_bh);
872*4882a593Smuzhiyun
873*4882a593Smuzhiyun return ret;
874*4882a593Smuzhiyun }
875*4882a593Smuzhiyun
ocfs2_find_refcount_rec_in_rl(struct ocfs2_caching_info * ci,struct buffer_head * ref_leaf_bh,u64 cpos,unsigned int len,struct ocfs2_refcount_rec * ret_rec,int * index)876*4882a593Smuzhiyun static void ocfs2_find_refcount_rec_in_rl(struct ocfs2_caching_info *ci,
877*4882a593Smuzhiyun struct buffer_head *ref_leaf_bh,
878*4882a593Smuzhiyun u64 cpos, unsigned int len,
879*4882a593Smuzhiyun struct ocfs2_refcount_rec *ret_rec,
880*4882a593Smuzhiyun int *index)
881*4882a593Smuzhiyun {
882*4882a593Smuzhiyun int i = 0;
883*4882a593Smuzhiyun struct ocfs2_refcount_block *rb =
884*4882a593Smuzhiyun (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
885*4882a593Smuzhiyun struct ocfs2_refcount_rec *rec = NULL;
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun for (; i < le16_to_cpu(rb->rf_records.rl_used); i++) {
888*4882a593Smuzhiyun rec = &rb->rf_records.rl_recs[i];
889*4882a593Smuzhiyun
890*4882a593Smuzhiyun if (le64_to_cpu(rec->r_cpos) +
891*4882a593Smuzhiyun le32_to_cpu(rec->r_clusters) <= cpos)
892*4882a593Smuzhiyun continue;
893*4882a593Smuzhiyun else if (le64_to_cpu(rec->r_cpos) > cpos)
894*4882a593Smuzhiyun break;
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun /* ok, cpos fail in this rec. Just return. */
897*4882a593Smuzhiyun if (ret_rec)
898*4882a593Smuzhiyun *ret_rec = *rec;
899*4882a593Smuzhiyun goto out;
900*4882a593Smuzhiyun }
901*4882a593Smuzhiyun
902*4882a593Smuzhiyun if (ret_rec) {
903*4882a593Smuzhiyun /* We meet with a hole here, so fake the rec. */
904*4882a593Smuzhiyun ret_rec->r_cpos = cpu_to_le64(cpos);
905*4882a593Smuzhiyun ret_rec->r_refcount = 0;
906*4882a593Smuzhiyun if (i < le16_to_cpu(rb->rf_records.rl_used) &&
907*4882a593Smuzhiyun le64_to_cpu(rec->r_cpos) < cpos + len)
908*4882a593Smuzhiyun ret_rec->r_clusters =
909*4882a593Smuzhiyun cpu_to_le32(le64_to_cpu(rec->r_cpos) - cpos);
910*4882a593Smuzhiyun else
911*4882a593Smuzhiyun ret_rec->r_clusters = cpu_to_le32(len);
912*4882a593Smuzhiyun }
913*4882a593Smuzhiyun
914*4882a593Smuzhiyun out:
915*4882a593Smuzhiyun *index = i;
916*4882a593Smuzhiyun }
917*4882a593Smuzhiyun
918*4882a593Smuzhiyun /*
919*4882a593Smuzhiyun * Try to remove refcount tree. The mechanism is:
920*4882a593Smuzhiyun * 1) Check whether i_clusters == 0, if no, exit.
921*4882a593Smuzhiyun * 2) check whether we have i_xattr_loc in dinode. if yes, exit.
922*4882a593Smuzhiyun * 3) Check whether we have inline xattr stored outside, if yes, exit.
923*4882a593Smuzhiyun * 4) Remove the tree.
924*4882a593Smuzhiyun */
ocfs2_try_remove_refcount_tree(struct inode * inode,struct buffer_head * di_bh)925*4882a593Smuzhiyun int ocfs2_try_remove_refcount_tree(struct inode *inode,
926*4882a593Smuzhiyun struct buffer_head *di_bh)
927*4882a593Smuzhiyun {
928*4882a593Smuzhiyun int ret;
929*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(inode);
930*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
931*4882a593Smuzhiyun
932*4882a593Smuzhiyun down_write(&oi->ip_xattr_sem);
933*4882a593Smuzhiyun down_write(&oi->ip_alloc_sem);
934*4882a593Smuzhiyun
935*4882a593Smuzhiyun if (oi->ip_clusters)
936*4882a593Smuzhiyun goto out;
937*4882a593Smuzhiyun
938*4882a593Smuzhiyun if ((oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) && di->i_xattr_loc)
939*4882a593Smuzhiyun goto out;
940*4882a593Smuzhiyun
941*4882a593Smuzhiyun if (oi->ip_dyn_features & OCFS2_INLINE_XATTR_FL &&
942*4882a593Smuzhiyun ocfs2_has_inline_xattr_value_outside(inode, di))
943*4882a593Smuzhiyun goto out;
944*4882a593Smuzhiyun
945*4882a593Smuzhiyun ret = ocfs2_remove_refcount_tree(inode, di_bh);
946*4882a593Smuzhiyun if (ret)
947*4882a593Smuzhiyun mlog_errno(ret);
948*4882a593Smuzhiyun out:
949*4882a593Smuzhiyun up_write(&oi->ip_alloc_sem);
950*4882a593Smuzhiyun up_write(&oi->ip_xattr_sem);
951*4882a593Smuzhiyun return 0;
952*4882a593Smuzhiyun }
953*4882a593Smuzhiyun
954*4882a593Smuzhiyun /*
955*4882a593Smuzhiyun * Find the end range for a leaf refcount block indicated by
956*4882a593Smuzhiyun * el->l_recs[index].e_blkno.
957*4882a593Smuzhiyun */
ocfs2_get_refcount_cpos_end(struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct ocfs2_extent_block * eb,struct ocfs2_extent_list * el,int index,u32 * cpos_end)958*4882a593Smuzhiyun static int ocfs2_get_refcount_cpos_end(struct ocfs2_caching_info *ci,
959*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
960*4882a593Smuzhiyun struct ocfs2_extent_block *eb,
961*4882a593Smuzhiyun struct ocfs2_extent_list *el,
962*4882a593Smuzhiyun int index, u32 *cpos_end)
963*4882a593Smuzhiyun {
964*4882a593Smuzhiyun int ret, i, subtree_root;
965*4882a593Smuzhiyun u32 cpos;
966*4882a593Smuzhiyun u64 blkno;
967*4882a593Smuzhiyun struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
968*4882a593Smuzhiyun struct ocfs2_path *left_path = NULL, *right_path = NULL;
969*4882a593Smuzhiyun struct ocfs2_extent_tree et;
970*4882a593Smuzhiyun struct ocfs2_extent_list *tmp_el;
971*4882a593Smuzhiyun
972*4882a593Smuzhiyun if (index < le16_to_cpu(el->l_next_free_rec) - 1) {
973*4882a593Smuzhiyun /*
974*4882a593Smuzhiyun * We have a extent rec after index, so just use the e_cpos
975*4882a593Smuzhiyun * of the next extent rec.
976*4882a593Smuzhiyun */
977*4882a593Smuzhiyun *cpos_end = le32_to_cpu(el->l_recs[index+1].e_cpos);
978*4882a593Smuzhiyun return 0;
979*4882a593Smuzhiyun }
980*4882a593Smuzhiyun
981*4882a593Smuzhiyun if (!eb || (eb && !eb->h_next_leaf_blk)) {
982*4882a593Smuzhiyun /*
983*4882a593Smuzhiyun * We are the last extent rec, so any high cpos should
984*4882a593Smuzhiyun * be stored in this leaf refcount block.
985*4882a593Smuzhiyun */
986*4882a593Smuzhiyun *cpos_end = UINT_MAX;
987*4882a593Smuzhiyun return 0;
988*4882a593Smuzhiyun }
989*4882a593Smuzhiyun
990*4882a593Smuzhiyun /*
991*4882a593Smuzhiyun * If the extent block isn't the last one, we have to find
992*4882a593Smuzhiyun * the subtree root between this extent block and the next
993*4882a593Smuzhiyun * leaf extent block and get the corresponding e_cpos from
994*4882a593Smuzhiyun * the subroot. Otherwise we may corrupt the b-tree.
995*4882a593Smuzhiyun */
996*4882a593Smuzhiyun ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
997*4882a593Smuzhiyun
998*4882a593Smuzhiyun left_path = ocfs2_new_path_from_et(&et);
999*4882a593Smuzhiyun if (!left_path) {
1000*4882a593Smuzhiyun ret = -ENOMEM;
1001*4882a593Smuzhiyun mlog_errno(ret);
1002*4882a593Smuzhiyun goto out;
1003*4882a593Smuzhiyun }
1004*4882a593Smuzhiyun
1005*4882a593Smuzhiyun cpos = le32_to_cpu(eb->h_list.l_recs[index].e_cpos);
1006*4882a593Smuzhiyun ret = ocfs2_find_path(ci, left_path, cpos);
1007*4882a593Smuzhiyun if (ret) {
1008*4882a593Smuzhiyun mlog_errno(ret);
1009*4882a593Smuzhiyun goto out;
1010*4882a593Smuzhiyun }
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun right_path = ocfs2_new_path_from_path(left_path);
1013*4882a593Smuzhiyun if (!right_path) {
1014*4882a593Smuzhiyun ret = -ENOMEM;
1015*4882a593Smuzhiyun mlog_errno(ret);
1016*4882a593Smuzhiyun goto out;
1017*4882a593Smuzhiyun }
1018*4882a593Smuzhiyun
1019*4882a593Smuzhiyun ret = ocfs2_find_cpos_for_right_leaf(sb, left_path, &cpos);
1020*4882a593Smuzhiyun if (ret) {
1021*4882a593Smuzhiyun mlog_errno(ret);
1022*4882a593Smuzhiyun goto out;
1023*4882a593Smuzhiyun }
1024*4882a593Smuzhiyun
1025*4882a593Smuzhiyun ret = ocfs2_find_path(ci, right_path, cpos);
1026*4882a593Smuzhiyun if (ret) {
1027*4882a593Smuzhiyun mlog_errno(ret);
1028*4882a593Smuzhiyun goto out;
1029*4882a593Smuzhiyun }
1030*4882a593Smuzhiyun
1031*4882a593Smuzhiyun subtree_root = ocfs2_find_subtree_root(&et, left_path,
1032*4882a593Smuzhiyun right_path);
1033*4882a593Smuzhiyun
1034*4882a593Smuzhiyun tmp_el = left_path->p_node[subtree_root].el;
1035*4882a593Smuzhiyun blkno = left_path->p_node[subtree_root+1].bh->b_blocknr;
1036*4882a593Smuzhiyun for (i = 0; i < le16_to_cpu(tmp_el->l_next_free_rec); i++) {
1037*4882a593Smuzhiyun if (le64_to_cpu(tmp_el->l_recs[i].e_blkno) == blkno) {
1038*4882a593Smuzhiyun *cpos_end = le32_to_cpu(tmp_el->l_recs[i+1].e_cpos);
1039*4882a593Smuzhiyun break;
1040*4882a593Smuzhiyun }
1041*4882a593Smuzhiyun }
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun BUG_ON(i == le16_to_cpu(tmp_el->l_next_free_rec));
1044*4882a593Smuzhiyun
1045*4882a593Smuzhiyun out:
1046*4882a593Smuzhiyun ocfs2_free_path(left_path);
1047*4882a593Smuzhiyun ocfs2_free_path(right_path);
1048*4882a593Smuzhiyun return ret;
1049*4882a593Smuzhiyun }
1050*4882a593Smuzhiyun
1051*4882a593Smuzhiyun /*
1052*4882a593Smuzhiyun * Given a cpos and len, try to find the refcount record which contains cpos.
1053*4882a593Smuzhiyun * 1. If cpos can be found in one refcount record, return the record.
1054*4882a593Smuzhiyun * 2. If cpos can't be found, return a fake record which start from cpos
1055*4882a593Smuzhiyun * and end at a small value between cpos+len and start of the next record.
1056*4882a593Smuzhiyun * This fake record has r_refcount = 0.
1057*4882a593Smuzhiyun */
ocfs2_get_refcount_rec(struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,u64 cpos,unsigned int len,struct ocfs2_refcount_rec * ret_rec,int * index,struct buffer_head ** ret_bh)1058*4882a593Smuzhiyun static int ocfs2_get_refcount_rec(struct ocfs2_caching_info *ci,
1059*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
1060*4882a593Smuzhiyun u64 cpos, unsigned int len,
1061*4882a593Smuzhiyun struct ocfs2_refcount_rec *ret_rec,
1062*4882a593Smuzhiyun int *index,
1063*4882a593Smuzhiyun struct buffer_head **ret_bh)
1064*4882a593Smuzhiyun {
1065*4882a593Smuzhiyun int ret = 0, i, found;
1066*4882a593Smuzhiyun u32 low_cpos, cpos_end;
1067*4882a593Smuzhiyun struct ocfs2_extent_list *el;
1068*4882a593Smuzhiyun struct ocfs2_extent_rec *rec = NULL;
1069*4882a593Smuzhiyun struct ocfs2_extent_block *eb = NULL;
1070*4882a593Smuzhiyun struct buffer_head *eb_bh = NULL, *ref_leaf_bh = NULL;
1071*4882a593Smuzhiyun struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1072*4882a593Smuzhiyun struct ocfs2_refcount_block *rb =
1073*4882a593Smuzhiyun (struct ocfs2_refcount_block *)ref_root_bh->b_data;
1074*4882a593Smuzhiyun
1075*4882a593Smuzhiyun if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)) {
1076*4882a593Smuzhiyun ocfs2_find_refcount_rec_in_rl(ci, ref_root_bh, cpos, len,
1077*4882a593Smuzhiyun ret_rec, index);
1078*4882a593Smuzhiyun *ret_bh = ref_root_bh;
1079*4882a593Smuzhiyun get_bh(ref_root_bh);
1080*4882a593Smuzhiyun return 0;
1081*4882a593Smuzhiyun }
1082*4882a593Smuzhiyun
1083*4882a593Smuzhiyun el = &rb->rf_list;
1084*4882a593Smuzhiyun low_cpos = cpos & OCFS2_32BIT_POS_MASK;
1085*4882a593Smuzhiyun
1086*4882a593Smuzhiyun if (el->l_tree_depth) {
1087*4882a593Smuzhiyun ret = ocfs2_find_leaf(ci, el, low_cpos, &eb_bh);
1088*4882a593Smuzhiyun if (ret) {
1089*4882a593Smuzhiyun mlog_errno(ret);
1090*4882a593Smuzhiyun goto out;
1091*4882a593Smuzhiyun }
1092*4882a593Smuzhiyun
1093*4882a593Smuzhiyun eb = (struct ocfs2_extent_block *) eb_bh->b_data;
1094*4882a593Smuzhiyun el = &eb->h_list;
1095*4882a593Smuzhiyun
1096*4882a593Smuzhiyun if (el->l_tree_depth) {
1097*4882a593Smuzhiyun ret = ocfs2_error(sb,
1098*4882a593Smuzhiyun "refcount tree %llu has non zero tree depth in leaf btree tree block %llu\n",
1099*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
1100*4882a593Smuzhiyun (unsigned long long)eb_bh->b_blocknr);
1101*4882a593Smuzhiyun goto out;
1102*4882a593Smuzhiyun }
1103*4882a593Smuzhiyun }
1104*4882a593Smuzhiyun
1105*4882a593Smuzhiyun found = 0;
1106*4882a593Smuzhiyun for (i = le16_to_cpu(el->l_next_free_rec) - 1; i >= 0; i--) {
1107*4882a593Smuzhiyun rec = &el->l_recs[i];
1108*4882a593Smuzhiyun
1109*4882a593Smuzhiyun if (le32_to_cpu(rec->e_cpos) <= low_cpos) {
1110*4882a593Smuzhiyun found = 1;
1111*4882a593Smuzhiyun break;
1112*4882a593Smuzhiyun }
1113*4882a593Smuzhiyun }
1114*4882a593Smuzhiyun
1115*4882a593Smuzhiyun if (found) {
1116*4882a593Smuzhiyun ret = ocfs2_get_refcount_cpos_end(ci, ref_root_bh,
1117*4882a593Smuzhiyun eb, el, i, &cpos_end);
1118*4882a593Smuzhiyun if (ret) {
1119*4882a593Smuzhiyun mlog_errno(ret);
1120*4882a593Smuzhiyun goto out;
1121*4882a593Smuzhiyun }
1122*4882a593Smuzhiyun
1123*4882a593Smuzhiyun if (cpos_end < low_cpos + len)
1124*4882a593Smuzhiyun len = cpos_end - low_cpos;
1125*4882a593Smuzhiyun }
1126*4882a593Smuzhiyun
1127*4882a593Smuzhiyun ret = ocfs2_read_refcount_block(ci, le64_to_cpu(rec->e_blkno),
1128*4882a593Smuzhiyun &ref_leaf_bh);
1129*4882a593Smuzhiyun if (ret) {
1130*4882a593Smuzhiyun mlog_errno(ret);
1131*4882a593Smuzhiyun goto out;
1132*4882a593Smuzhiyun }
1133*4882a593Smuzhiyun
1134*4882a593Smuzhiyun ocfs2_find_refcount_rec_in_rl(ci, ref_leaf_bh, cpos, len,
1135*4882a593Smuzhiyun ret_rec, index);
1136*4882a593Smuzhiyun *ret_bh = ref_leaf_bh;
1137*4882a593Smuzhiyun out:
1138*4882a593Smuzhiyun brelse(eb_bh);
1139*4882a593Smuzhiyun return ret;
1140*4882a593Smuzhiyun }
1141*4882a593Smuzhiyun
1142*4882a593Smuzhiyun enum ocfs2_ref_rec_contig {
1143*4882a593Smuzhiyun REF_CONTIG_NONE = 0,
1144*4882a593Smuzhiyun REF_CONTIG_LEFT,
1145*4882a593Smuzhiyun REF_CONTIG_RIGHT,
1146*4882a593Smuzhiyun REF_CONTIG_LEFTRIGHT,
1147*4882a593Smuzhiyun };
1148*4882a593Smuzhiyun
1149*4882a593Smuzhiyun static enum ocfs2_ref_rec_contig
ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block * rb,int index)1150*4882a593Smuzhiyun ocfs2_refcount_rec_adjacent(struct ocfs2_refcount_block *rb,
1151*4882a593Smuzhiyun int index)
1152*4882a593Smuzhiyun {
1153*4882a593Smuzhiyun if ((rb->rf_records.rl_recs[index].r_refcount ==
1154*4882a593Smuzhiyun rb->rf_records.rl_recs[index + 1].r_refcount) &&
1155*4882a593Smuzhiyun (le64_to_cpu(rb->rf_records.rl_recs[index].r_cpos) +
1156*4882a593Smuzhiyun le32_to_cpu(rb->rf_records.rl_recs[index].r_clusters) ==
1157*4882a593Smuzhiyun le64_to_cpu(rb->rf_records.rl_recs[index + 1].r_cpos)))
1158*4882a593Smuzhiyun return REF_CONTIG_RIGHT;
1159*4882a593Smuzhiyun
1160*4882a593Smuzhiyun return REF_CONTIG_NONE;
1161*4882a593Smuzhiyun }
1162*4882a593Smuzhiyun
1163*4882a593Smuzhiyun static enum ocfs2_ref_rec_contig
ocfs2_refcount_rec_contig(struct ocfs2_refcount_block * rb,int index)1164*4882a593Smuzhiyun ocfs2_refcount_rec_contig(struct ocfs2_refcount_block *rb,
1165*4882a593Smuzhiyun int index)
1166*4882a593Smuzhiyun {
1167*4882a593Smuzhiyun enum ocfs2_ref_rec_contig ret = REF_CONTIG_NONE;
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun if (index < le16_to_cpu(rb->rf_records.rl_used) - 1)
1170*4882a593Smuzhiyun ret = ocfs2_refcount_rec_adjacent(rb, index);
1171*4882a593Smuzhiyun
1172*4882a593Smuzhiyun if (index > 0) {
1173*4882a593Smuzhiyun enum ocfs2_ref_rec_contig tmp;
1174*4882a593Smuzhiyun
1175*4882a593Smuzhiyun tmp = ocfs2_refcount_rec_adjacent(rb, index - 1);
1176*4882a593Smuzhiyun
1177*4882a593Smuzhiyun if (tmp == REF_CONTIG_RIGHT) {
1178*4882a593Smuzhiyun if (ret == REF_CONTIG_RIGHT)
1179*4882a593Smuzhiyun ret = REF_CONTIG_LEFTRIGHT;
1180*4882a593Smuzhiyun else
1181*4882a593Smuzhiyun ret = REF_CONTIG_LEFT;
1182*4882a593Smuzhiyun }
1183*4882a593Smuzhiyun }
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun return ret;
1186*4882a593Smuzhiyun }
1187*4882a593Smuzhiyun
ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block * rb,int index)1188*4882a593Smuzhiyun static void ocfs2_rotate_refcount_rec_left(struct ocfs2_refcount_block *rb,
1189*4882a593Smuzhiyun int index)
1190*4882a593Smuzhiyun {
1191*4882a593Smuzhiyun BUG_ON(rb->rf_records.rl_recs[index].r_refcount !=
1192*4882a593Smuzhiyun rb->rf_records.rl_recs[index+1].r_refcount);
1193*4882a593Smuzhiyun
1194*4882a593Smuzhiyun le32_add_cpu(&rb->rf_records.rl_recs[index].r_clusters,
1195*4882a593Smuzhiyun le32_to_cpu(rb->rf_records.rl_recs[index+1].r_clusters));
1196*4882a593Smuzhiyun
1197*4882a593Smuzhiyun if (index < le16_to_cpu(rb->rf_records.rl_used) - 2)
1198*4882a593Smuzhiyun memmove(&rb->rf_records.rl_recs[index + 1],
1199*4882a593Smuzhiyun &rb->rf_records.rl_recs[index + 2],
1200*4882a593Smuzhiyun sizeof(struct ocfs2_refcount_rec) *
1201*4882a593Smuzhiyun (le16_to_cpu(rb->rf_records.rl_used) - index - 2));
1202*4882a593Smuzhiyun
1203*4882a593Smuzhiyun memset(&rb->rf_records.rl_recs[le16_to_cpu(rb->rf_records.rl_used) - 1],
1204*4882a593Smuzhiyun 0, sizeof(struct ocfs2_refcount_rec));
1205*4882a593Smuzhiyun le16_add_cpu(&rb->rf_records.rl_used, -1);
1206*4882a593Smuzhiyun }
1207*4882a593Smuzhiyun
1208*4882a593Smuzhiyun /*
1209*4882a593Smuzhiyun * Merge the refcount rec if we are contiguous with the adjacent recs.
1210*4882a593Smuzhiyun */
ocfs2_refcount_rec_merge(struct ocfs2_refcount_block * rb,int index)1211*4882a593Smuzhiyun static void ocfs2_refcount_rec_merge(struct ocfs2_refcount_block *rb,
1212*4882a593Smuzhiyun int index)
1213*4882a593Smuzhiyun {
1214*4882a593Smuzhiyun enum ocfs2_ref_rec_contig contig =
1215*4882a593Smuzhiyun ocfs2_refcount_rec_contig(rb, index);
1216*4882a593Smuzhiyun
1217*4882a593Smuzhiyun if (contig == REF_CONTIG_NONE)
1218*4882a593Smuzhiyun return;
1219*4882a593Smuzhiyun
1220*4882a593Smuzhiyun if (contig == REF_CONTIG_LEFT || contig == REF_CONTIG_LEFTRIGHT) {
1221*4882a593Smuzhiyun BUG_ON(index == 0);
1222*4882a593Smuzhiyun index--;
1223*4882a593Smuzhiyun }
1224*4882a593Smuzhiyun
1225*4882a593Smuzhiyun ocfs2_rotate_refcount_rec_left(rb, index);
1226*4882a593Smuzhiyun
1227*4882a593Smuzhiyun if (contig == REF_CONTIG_LEFTRIGHT)
1228*4882a593Smuzhiyun ocfs2_rotate_refcount_rec_left(rb, index);
1229*4882a593Smuzhiyun }
1230*4882a593Smuzhiyun
1231*4882a593Smuzhiyun /*
1232*4882a593Smuzhiyun * Change the refcount indexed by "index" in ref_bh.
1233*4882a593Smuzhiyun * If refcount reaches 0, remove it.
1234*4882a593Smuzhiyun */
ocfs2_change_refcount_rec(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_leaf_bh,int index,int merge,int change)1235*4882a593Smuzhiyun static int ocfs2_change_refcount_rec(handle_t *handle,
1236*4882a593Smuzhiyun struct ocfs2_caching_info *ci,
1237*4882a593Smuzhiyun struct buffer_head *ref_leaf_bh,
1238*4882a593Smuzhiyun int index, int merge, int change)
1239*4882a593Smuzhiyun {
1240*4882a593Smuzhiyun int ret;
1241*4882a593Smuzhiyun struct ocfs2_refcount_block *rb =
1242*4882a593Smuzhiyun (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1243*4882a593Smuzhiyun struct ocfs2_refcount_list *rl = &rb->rf_records;
1244*4882a593Smuzhiyun struct ocfs2_refcount_rec *rec = &rl->rl_recs[index];
1245*4882a593Smuzhiyun
1246*4882a593Smuzhiyun ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1247*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
1248*4882a593Smuzhiyun if (ret) {
1249*4882a593Smuzhiyun mlog_errno(ret);
1250*4882a593Smuzhiyun goto out;
1251*4882a593Smuzhiyun }
1252*4882a593Smuzhiyun
1253*4882a593Smuzhiyun trace_ocfs2_change_refcount_rec(
1254*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
1255*4882a593Smuzhiyun index, le32_to_cpu(rec->r_refcount), change);
1256*4882a593Smuzhiyun le32_add_cpu(&rec->r_refcount, change);
1257*4882a593Smuzhiyun
1258*4882a593Smuzhiyun if (!rec->r_refcount) {
1259*4882a593Smuzhiyun if (index != le16_to_cpu(rl->rl_used) - 1) {
1260*4882a593Smuzhiyun memmove(rec, rec + 1,
1261*4882a593Smuzhiyun (le16_to_cpu(rl->rl_used) - index - 1) *
1262*4882a593Smuzhiyun sizeof(struct ocfs2_refcount_rec));
1263*4882a593Smuzhiyun memset(&rl->rl_recs[le16_to_cpu(rl->rl_used) - 1],
1264*4882a593Smuzhiyun 0, sizeof(struct ocfs2_refcount_rec));
1265*4882a593Smuzhiyun }
1266*4882a593Smuzhiyun
1267*4882a593Smuzhiyun le16_add_cpu(&rl->rl_used, -1);
1268*4882a593Smuzhiyun } else if (merge)
1269*4882a593Smuzhiyun ocfs2_refcount_rec_merge(rb, index);
1270*4882a593Smuzhiyun
1271*4882a593Smuzhiyun ocfs2_journal_dirty(handle, ref_leaf_bh);
1272*4882a593Smuzhiyun out:
1273*4882a593Smuzhiyun return ret;
1274*4882a593Smuzhiyun }
1275*4882a593Smuzhiyun
ocfs2_expand_inline_ref_root(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head ** ref_leaf_bh,struct ocfs2_alloc_context * meta_ac)1276*4882a593Smuzhiyun static int ocfs2_expand_inline_ref_root(handle_t *handle,
1277*4882a593Smuzhiyun struct ocfs2_caching_info *ci,
1278*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
1279*4882a593Smuzhiyun struct buffer_head **ref_leaf_bh,
1280*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac)
1281*4882a593Smuzhiyun {
1282*4882a593Smuzhiyun int ret;
1283*4882a593Smuzhiyun u16 suballoc_bit_start;
1284*4882a593Smuzhiyun u32 num_got;
1285*4882a593Smuzhiyun u64 suballoc_loc, blkno;
1286*4882a593Smuzhiyun struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1287*4882a593Smuzhiyun struct buffer_head *new_bh = NULL;
1288*4882a593Smuzhiyun struct ocfs2_refcount_block *new_rb;
1289*4882a593Smuzhiyun struct ocfs2_refcount_block *root_rb =
1290*4882a593Smuzhiyun (struct ocfs2_refcount_block *)ref_root_bh->b_data;
1291*4882a593Smuzhiyun
1292*4882a593Smuzhiyun ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
1293*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
1294*4882a593Smuzhiyun if (ret) {
1295*4882a593Smuzhiyun mlog_errno(ret);
1296*4882a593Smuzhiyun goto out;
1297*4882a593Smuzhiyun }
1298*4882a593Smuzhiyun
1299*4882a593Smuzhiyun ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
1300*4882a593Smuzhiyun &suballoc_bit_start, &num_got,
1301*4882a593Smuzhiyun &blkno);
1302*4882a593Smuzhiyun if (ret) {
1303*4882a593Smuzhiyun mlog_errno(ret);
1304*4882a593Smuzhiyun goto out;
1305*4882a593Smuzhiyun }
1306*4882a593Smuzhiyun
1307*4882a593Smuzhiyun new_bh = sb_getblk(sb, blkno);
1308*4882a593Smuzhiyun if (new_bh == NULL) {
1309*4882a593Smuzhiyun ret = -ENOMEM;
1310*4882a593Smuzhiyun mlog_errno(ret);
1311*4882a593Smuzhiyun goto out;
1312*4882a593Smuzhiyun }
1313*4882a593Smuzhiyun ocfs2_set_new_buffer_uptodate(ci, new_bh);
1314*4882a593Smuzhiyun
1315*4882a593Smuzhiyun ret = ocfs2_journal_access_rb(handle, ci, new_bh,
1316*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_CREATE);
1317*4882a593Smuzhiyun if (ret) {
1318*4882a593Smuzhiyun mlog_errno(ret);
1319*4882a593Smuzhiyun goto out;
1320*4882a593Smuzhiyun }
1321*4882a593Smuzhiyun
1322*4882a593Smuzhiyun /*
1323*4882a593Smuzhiyun * Initialize ocfs2_refcount_block.
1324*4882a593Smuzhiyun * It should contain the same information as the old root.
1325*4882a593Smuzhiyun * so just memcpy it and change the corresponding field.
1326*4882a593Smuzhiyun */
1327*4882a593Smuzhiyun memcpy(new_bh->b_data, ref_root_bh->b_data, sb->s_blocksize);
1328*4882a593Smuzhiyun
1329*4882a593Smuzhiyun new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1330*4882a593Smuzhiyun new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
1331*4882a593Smuzhiyun new_rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc);
1332*4882a593Smuzhiyun new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1333*4882a593Smuzhiyun new_rb->rf_blkno = cpu_to_le64(blkno);
1334*4882a593Smuzhiyun new_rb->rf_cpos = cpu_to_le32(0);
1335*4882a593Smuzhiyun new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
1336*4882a593Smuzhiyun new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
1337*4882a593Smuzhiyun ocfs2_journal_dirty(handle, new_bh);
1338*4882a593Smuzhiyun
1339*4882a593Smuzhiyun /* Now change the root. */
1340*4882a593Smuzhiyun memset(&root_rb->rf_list, 0, sb->s_blocksize -
1341*4882a593Smuzhiyun offsetof(struct ocfs2_refcount_block, rf_list));
1342*4882a593Smuzhiyun root_rb->rf_list.l_count = cpu_to_le16(ocfs2_extent_recs_per_rb(sb));
1343*4882a593Smuzhiyun root_rb->rf_clusters = cpu_to_le32(1);
1344*4882a593Smuzhiyun root_rb->rf_list.l_next_free_rec = cpu_to_le16(1);
1345*4882a593Smuzhiyun root_rb->rf_list.l_recs[0].e_blkno = cpu_to_le64(blkno);
1346*4882a593Smuzhiyun root_rb->rf_list.l_recs[0].e_leaf_clusters = cpu_to_le16(1);
1347*4882a593Smuzhiyun root_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_TREE_FL);
1348*4882a593Smuzhiyun
1349*4882a593Smuzhiyun ocfs2_journal_dirty(handle, ref_root_bh);
1350*4882a593Smuzhiyun
1351*4882a593Smuzhiyun trace_ocfs2_expand_inline_ref_root((unsigned long long)blkno,
1352*4882a593Smuzhiyun le16_to_cpu(new_rb->rf_records.rl_used));
1353*4882a593Smuzhiyun
1354*4882a593Smuzhiyun *ref_leaf_bh = new_bh;
1355*4882a593Smuzhiyun new_bh = NULL;
1356*4882a593Smuzhiyun out:
1357*4882a593Smuzhiyun brelse(new_bh);
1358*4882a593Smuzhiyun return ret;
1359*4882a593Smuzhiyun }
1360*4882a593Smuzhiyun
ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec * prev,struct ocfs2_refcount_rec * next)1361*4882a593Smuzhiyun static int ocfs2_refcount_rec_no_intersect(struct ocfs2_refcount_rec *prev,
1362*4882a593Smuzhiyun struct ocfs2_refcount_rec *next)
1363*4882a593Smuzhiyun {
1364*4882a593Smuzhiyun if (ocfs2_get_ref_rec_low_cpos(prev) + le32_to_cpu(prev->r_clusters) <=
1365*4882a593Smuzhiyun ocfs2_get_ref_rec_low_cpos(next))
1366*4882a593Smuzhiyun return 1;
1367*4882a593Smuzhiyun
1368*4882a593Smuzhiyun return 0;
1369*4882a593Smuzhiyun }
1370*4882a593Smuzhiyun
cmp_refcount_rec_by_low_cpos(const void * a,const void * b)1371*4882a593Smuzhiyun static int cmp_refcount_rec_by_low_cpos(const void *a, const void *b)
1372*4882a593Smuzhiyun {
1373*4882a593Smuzhiyun const struct ocfs2_refcount_rec *l = a, *r = b;
1374*4882a593Smuzhiyun u32 l_cpos = ocfs2_get_ref_rec_low_cpos(l);
1375*4882a593Smuzhiyun u32 r_cpos = ocfs2_get_ref_rec_low_cpos(r);
1376*4882a593Smuzhiyun
1377*4882a593Smuzhiyun if (l_cpos > r_cpos)
1378*4882a593Smuzhiyun return 1;
1379*4882a593Smuzhiyun if (l_cpos < r_cpos)
1380*4882a593Smuzhiyun return -1;
1381*4882a593Smuzhiyun return 0;
1382*4882a593Smuzhiyun }
1383*4882a593Smuzhiyun
cmp_refcount_rec_by_cpos(const void * a,const void * b)1384*4882a593Smuzhiyun static int cmp_refcount_rec_by_cpos(const void *a, const void *b)
1385*4882a593Smuzhiyun {
1386*4882a593Smuzhiyun const struct ocfs2_refcount_rec *l = a, *r = b;
1387*4882a593Smuzhiyun u64 l_cpos = le64_to_cpu(l->r_cpos);
1388*4882a593Smuzhiyun u64 r_cpos = le64_to_cpu(r->r_cpos);
1389*4882a593Smuzhiyun
1390*4882a593Smuzhiyun if (l_cpos > r_cpos)
1391*4882a593Smuzhiyun return 1;
1392*4882a593Smuzhiyun if (l_cpos < r_cpos)
1393*4882a593Smuzhiyun return -1;
1394*4882a593Smuzhiyun return 0;
1395*4882a593Smuzhiyun }
1396*4882a593Smuzhiyun
swap_refcount_rec(void * a,void * b,int size)1397*4882a593Smuzhiyun static void swap_refcount_rec(void *a, void *b, int size)
1398*4882a593Smuzhiyun {
1399*4882a593Smuzhiyun struct ocfs2_refcount_rec *l = a, *r = b;
1400*4882a593Smuzhiyun
1401*4882a593Smuzhiyun swap(*l, *r);
1402*4882a593Smuzhiyun }
1403*4882a593Smuzhiyun
1404*4882a593Smuzhiyun /*
1405*4882a593Smuzhiyun * The refcount cpos are ordered by their 64bit cpos,
1406*4882a593Smuzhiyun * But we will use the low 32 bit to be the e_cpos in the b-tree.
1407*4882a593Smuzhiyun * So we need to make sure that this pos isn't intersected with others.
1408*4882a593Smuzhiyun *
1409*4882a593Smuzhiyun * Note: The refcount block is already sorted by their low 32 bit cpos,
1410*4882a593Smuzhiyun * So just try the middle pos first, and we will exit when we find
1411*4882a593Smuzhiyun * the good position.
1412*4882a593Smuzhiyun */
ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list * rl,u32 * split_pos,int * split_index)1413*4882a593Smuzhiyun static int ocfs2_find_refcount_split_pos(struct ocfs2_refcount_list *rl,
1414*4882a593Smuzhiyun u32 *split_pos, int *split_index)
1415*4882a593Smuzhiyun {
1416*4882a593Smuzhiyun int num_used = le16_to_cpu(rl->rl_used);
1417*4882a593Smuzhiyun int delta, middle = num_used / 2;
1418*4882a593Smuzhiyun
1419*4882a593Smuzhiyun for (delta = 0; delta < middle; delta++) {
1420*4882a593Smuzhiyun /* Let's check delta earlier than middle */
1421*4882a593Smuzhiyun if (ocfs2_refcount_rec_no_intersect(
1422*4882a593Smuzhiyun &rl->rl_recs[middle - delta - 1],
1423*4882a593Smuzhiyun &rl->rl_recs[middle - delta])) {
1424*4882a593Smuzhiyun *split_index = middle - delta;
1425*4882a593Smuzhiyun break;
1426*4882a593Smuzhiyun }
1427*4882a593Smuzhiyun
1428*4882a593Smuzhiyun /* For even counts, don't walk off the end */
1429*4882a593Smuzhiyun if ((middle + delta + 1) == num_used)
1430*4882a593Smuzhiyun continue;
1431*4882a593Smuzhiyun
1432*4882a593Smuzhiyun /* Now try delta past middle */
1433*4882a593Smuzhiyun if (ocfs2_refcount_rec_no_intersect(
1434*4882a593Smuzhiyun &rl->rl_recs[middle + delta],
1435*4882a593Smuzhiyun &rl->rl_recs[middle + delta + 1])) {
1436*4882a593Smuzhiyun *split_index = middle + delta + 1;
1437*4882a593Smuzhiyun break;
1438*4882a593Smuzhiyun }
1439*4882a593Smuzhiyun }
1440*4882a593Smuzhiyun
1441*4882a593Smuzhiyun if (delta >= middle)
1442*4882a593Smuzhiyun return -ENOSPC;
1443*4882a593Smuzhiyun
1444*4882a593Smuzhiyun *split_pos = ocfs2_get_ref_rec_low_cpos(&rl->rl_recs[*split_index]);
1445*4882a593Smuzhiyun return 0;
1446*4882a593Smuzhiyun }
1447*4882a593Smuzhiyun
ocfs2_divide_leaf_refcount_block(struct buffer_head * ref_leaf_bh,struct buffer_head * new_bh,u32 * split_cpos)1448*4882a593Smuzhiyun static int ocfs2_divide_leaf_refcount_block(struct buffer_head *ref_leaf_bh,
1449*4882a593Smuzhiyun struct buffer_head *new_bh,
1450*4882a593Smuzhiyun u32 *split_cpos)
1451*4882a593Smuzhiyun {
1452*4882a593Smuzhiyun int split_index = 0, num_moved, ret;
1453*4882a593Smuzhiyun u32 cpos = 0;
1454*4882a593Smuzhiyun struct ocfs2_refcount_block *rb =
1455*4882a593Smuzhiyun (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1456*4882a593Smuzhiyun struct ocfs2_refcount_list *rl = &rb->rf_records;
1457*4882a593Smuzhiyun struct ocfs2_refcount_block *new_rb =
1458*4882a593Smuzhiyun (struct ocfs2_refcount_block *)new_bh->b_data;
1459*4882a593Smuzhiyun struct ocfs2_refcount_list *new_rl = &new_rb->rf_records;
1460*4882a593Smuzhiyun
1461*4882a593Smuzhiyun trace_ocfs2_divide_leaf_refcount_block(
1462*4882a593Smuzhiyun (unsigned long long)ref_leaf_bh->b_blocknr,
1463*4882a593Smuzhiyun le16_to_cpu(rl->rl_count), le16_to_cpu(rl->rl_used));
1464*4882a593Smuzhiyun
1465*4882a593Smuzhiyun /*
1466*4882a593Smuzhiyun * XXX: Improvement later.
1467*4882a593Smuzhiyun * If we know all the high 32 bit cpos is the same, no need to sort.
1468*4882a593Smuzhiyun *
1469*4882a593Smuzhiyun * In order to make the whole process safe, we do:
1470*4882a593Smuzhiyun * 1. sort the entries by their low 32 bit cpos first so that we can
1471*4882a593Smuzhiyun * find the split cpos easily.
1472*4882a593Smuzhiyun * 2. call ocfs2_insert_extent to insert the new refcount block.
1473*4882a593Smuzhiyun * 3. move the refcount rec to the new block.
1474*4882a593Smuzhiyun * 4. sort the entries by their 64 bit cpos.
1475*4882a593Smuzhiyun * 5. dirty the new_rb and rb.
1476*4882a593Smuzhiyun */
1477*4882a593Smuzhiyun sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
1478*4882a593Smuzhiyun sizeof(struct ocfs2_refcount_rec),
1479*4882a593Smuzhiyun cmp_refcount_rec_by_low_cpos, swap_refcount_rec);
1480*4882a593Smuzhiyun
1481*4882a593Smuzhiyun ret = ocfs2_find_refcount_split_pos(rl, &cpos, &split_index);
1482*4882a593Smuzhiyun if (ret) {
1483*4882a593Smuzhiyun mlog_errno(ret);
1484*4882a593Smuzhiyun return ret;
1485*4882a593Smuzhiyun }
1486*4882a593Smuzhiyun
1487*4882a593Smuzhiyun new_rb->rf_cpos = cpu_to_le32(cpos);
1488*4882a593Smuzhiyun
1489*4882a593Smuzhiyun /* move refcount records starting from split_index to the new block. */
1490*4882a593Smuzhiyun num_moved = le16_to_cpu(rl->rl_used) - split_index;
1491*4882a593Smuzhiyun memcpy(new_rl->rl_recs, &rl->rl_recs[split_index],
1492*4882a593Smuzhiyun num_moved * sizeof(struct ocfs2_refcount_rec));
1493*4882a593Smuzhiyun
1494*4882a593Smuzhiyun /*ok, remove the entries we just moved over to the other block. */
1495*4882a593Smuzhiyun memset(&rl->rl_recs[split_index], 0,
1496*4882a593Smuzhiyun num_moved * sizeof(struct ocfs2_refcount_rec));
1497*4882a593Smuzhiyun
1498*4882a593Smuzhiyun /* change old and new rl_used accordingly. */
1499*4882a593Smuzhiyun le16_add_cpu(&rl->rl_used, -num_moved);
1500*4882a593Smuzhiyun new_rl->rl_used = cpu_to_le16(num_moved);
1501*4882a593Smuzhiyun
1502*4882a593Smuzhiyun sort(&rl->rl_recs, le16_to_cpu(rl->rl_used),
1503*4882a593Smuzhiyun sizeof(struct ocfs2_refcount_rec),
1504*4882a593Smuzhiyun cmp_refcount_rec_by_cpos, swap_refcount_rec);
1505*4882a593Smuzhiyun
1506*4882a593Smuzhiyun sort(&new_rl->rl_recs, le16_to_cpu(new_rl->rl_used),
1507*4882a593Smuzhiyun sizeof(struct ocfs2_refcount_rec),
1508*4882a593Smuzhiyun cmp_refcount_rec_by_cpos, swap_refcount_rec);
1509*4882a593Smuzhiyun
1510*4882a593Smuzhiyun *split_cpos = cpos;
1511*4882a593Smuzhiyun return 0;
1512*4882a593Smuzhiyun }
1513*4882a593Smuzhiyun
ocfs2_new_leaf_refcount_block(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head * ref_leaf_bh,struct ocfs2_alloc_context * meta_ac)1514*4882a593Smuzhiyun static int ocfs2_new_leaf_refcount_block(handle_t *handle,
1515*4882a593Smuzhiyun struct ocfs2_caching_info *ci,
1516*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
1517*4882a593Smuzhiyun struct buffer_head *ref_leaf_bh,
1518*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac)
1519*4882a593Smuzhiyun {
1520*4882a593Smuzhiyun int ret;
1521*4882a593Smuzhiyun u16 suballoc_bit_start;
1522*4882a593Smuzhiyun u32 num_got, new_cpos;
1523*4882a593Smuzhiyun u64 suballoc_loc, blkno;
1524*4882a593Smuzhiyun struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
1525*4882a593Smuzhiyun struct ocfs2_refcount_block *root_rb =
1526*4882a593Smuzhiyun (struct ocfs2_refcount_block *)ref_root_bh->b_data;
1527*4882a593Smuzhiyun struct buffer_head *new_bh = NULL;
1528*4882a593Smuzhiyun struct ocfs2_refcount_block *new_rb;
1529*4882a593Smuzhiyun struct ocfs2_extent_tree ref_et;
1530*4882a593Smuzhiyun
1531*4882a593Smuzhiyun BUG_ON(!(le32_to_cpu(root_rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL));
1532*4882a593Smuzhiyun
1533*4882a593Smuzhiyun ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
1534*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
1535*4882a593Smuzhiyun if (ret) {
1536*4882a593Smuzhiyun mlog_errno(ret);
1537*4882a593Smuzhiyun goto out;
1538*4882a593Smuzhiyun }
1539*4882a593Smuzhiyun
1540*4882a593Smuzhiyun ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1541*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
1542*4882a593Smuzhiyun if (ret) {
1543*4882a593Smuzhiyun mlog_errno(ret);
1544*4882a593Smuzhiyun goto out;
1545*4882a593Smuzhiyun }
1546*4882a593Smuzhiyun
1547*4882a593Smuzhiyun ret = ocfs2_claim_metadata(handle, meta_ac, 1, &suballoc_loc,
1548*4882a593Smuzhiyun &suballoc_bit_start, &num_got,
1549*4882a593Smuzhiyun &blkno);
1550*4882a593Smuzhiyun if (ret) {
1551*4882a593Smuzhiyun mlog_errno(ret);
1552*4882a593Smuzhiyun goto out;
1553*4882a593Smuzhiyun }
1554*4882a593Smuzhiyun
1555*4882a593Smuzhiyun new_bh = sb_getblk(sb, blkno);
1556*4882a593Smuzhiyun if (new_bh == NULL) {
1557*4882a593Smuzhiyun ret = -ENOMEM;
1558*4882a593Smuzhiyun mlog_errno(ret);
1559*4882a593Smuzhiyun goto out;
1560*4882a593Smuzhiyun }
1561*4882a593Smuzhiyun ocfs2_set_new_buffer_uptodate(ci, new_bh);
1562*4882a593Smuzhiyun
1563*4882a593Smuzhiyun ret = ocfs2_journal_access_rb(handle, ci, new_bh,
1564*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_CREATE);
1565*4882a593Smuzhiyun if (ret) {
1566*4882a593Smuzhiyun mlog_errno(ret);
1567*4882a593Smuzhiyun goto out;
1568*4882a593Smuzhiyun }
1569*4882a593Smuzhiyun
1570*4882a593Smuzhiyun /* Initialize ocfs2_refcount_block. */
1571*4882a593Smuzhiyun new_rb = (struct ocfs2_refcount_block *)new_bh->b_data;
1572*4882a593Smuzhiyun memset(new_rb, 0, sb->s_blocksize);
1573*4882a593Smuzhiyun strcpy((void *)new_rb, OCFS2_REFCOUNT_BLOCK_SIGNATURE);
1574*4882a593Smuzhiyun new_rb->rf_suballoc_slot = cpu_to_le16(meta_ac->ac_alloc_slot);
1575*4882a593Smuzhiyun new_rb->rf_suballoc_loc = cpu_to_le64(suballoc_loc);
1576*4882a593Smuzhiyun new_rb->rf_suballoc_bit = cpu_to_le16(suballoc_bit_start);
1577*4882a593Smuzhiyun new_rb->rf_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation);
1578*4882a593Smuzhiyun new_rb->rf_blkno = cpu_to_le64(blkno);
1579*4882a593Smuzhiyun new_rb->rf_parent = cpu_to_le64(ref_root_bh->b_blocknr);
1580*4882a593Smuzhiyun new_rb->rf_flags = cpu_to_le32(OCFS2_REFCOUNT_LEAF_FL);
1581*4882a593Smuzhiyun new_rb->rf_records.rl_count =
1582*4882a593Smuzhiyun cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
1583*4882a593Smuzhiyun new_rb->rf_generation = root_rb->rf_generation;
1584*4882a593Smuzhiyun
1585*4882a593Smuzhiyun ret = ocfs2_divide_leaf_refcount_block(ref_leaf_bh, new_bh, &new_cpos);
1586*4882a593Smuzhiyun if (ret) {
1587*4882a593Smuzhiyun mlog_errno(ret);
1588*4882a593Smuzhiyun goto out;
1589*4882a593Smuzhiyun }
1590*4882a593Smuzhiyun
1591*4882a593Smuzhiyun ocfs2_journal_dirty(handle, ref_leaf_bh);
1592*4882a593Smuzhiyun ocfs2_journal_dirty(handle, new_bh);
1593*4882a593Smuzhiyun
1594*4882a593Smuzhiyun ocfs2_init_refcount_extent_tree(&ref_et, ci, ref_root_bh);
1595*4882a593Smuzhiyun
1596*4882a593Smuzhiyun trace_ocfs2_new_leaf_refcount_block(
1597*4882a593Smuzhiyun (unsigned long long)new_bh->b_blocknr, new_cpos);
1598*4882a593Smuzhiyun
1599*4882a593Smuzhiyun /* Insert the new leaf block with the specific offset cpos. */
1600*4882a593Smuzhiyun ret = ocfs2_insert_extent(handle, &ref_et, new_cpos, new_bh->b_blocknr,
1601*4882a593Smuzhiyun 1, 0, meta_ac);
1602*4882a593Smuzhiyun if (ret)
1603*4882a593Smuzhiyun mlog_errno(ret);
1604*4882a593Smuzhiyun
1605*4882a593Smuzhiyun out:
1606*4882a593Smuzhiyun brelse(new_bh);
1607*4882a593Smuzhiyun return ret;
1608*4882a593Smuzhiyun }
1609*4882a593Smuzhiyun
ocfs2_expand_refcount_tree(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head * ref_leaf_bh,struct ocfs2_alloc_context * meta_ac)1610*4882a593Smuzhiyun static int ocfs2_expand_refcount_tree(handle_t *handle,
1611*4882a593Smuzhiyun struct ocfs2_caching_info *ci,
1612*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
1613*4882a593Smuzhiyun struct buffer_head *ref_leaf_bh,
1614*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac)
1615*4882a593Smuzhiyun {
1616*4882a593Smuzhiyun int ret;
1617*4882a593Smuzhiyun struct buffer_head *expand_bh = NULL;
1618*4882a593Smuzhiyun
1619*4882a593Smuzhiyun if (ref_root_bh == ref_leaf_bh) {
1620*4882a593Smuzhiyun /*
1621*4882a593Smuzhiyun * the old root bh hasn't been expanded to a b-tree,
1622*4882a593Smuzhiyun * so expand it first.
1623*4882a593Smuzhiyun */
1624*4882a593Smuzhiyun ret = ocfs2_expand_inline_ref_root(handle, ci, ref_root_bh,
1625*4882a593Smuzhiyun &expand_bh, meta_ac);
1626*4882a593Smuzhiyun if (ret) {
1627*4882a593Smuzhiyun mlog_errno(ret);
1628*4882a593Smuzhiyun goto out;
1629*4882a593Smuzhiyun }
1630*4882a593Smuzhiyun } else {
1631*4882a593Smuzhiyun expand_bh = ref_leaf_bh;
1632*4882a593Smuzhiyun get_bh(expand_bh);
1633*4882a593Smuzhiyun }
1634*4882a593Smuzhiyun
1635*4882a593Smuzhiyun
1636*4882a593Smuzhiyun /* Now add a new refcount block into the tree.*/
1637*4882a593Smuzhiyun ret = ocfs2_new_leaf_refcount_block(handle, ci, ref_root_bh,
1638*4882a593Smuzhiyun expand_bh, meta_ac);
1639*4882a593Smuzhiyun if (ret)
1640*4882a593Smuzhiyun mlog_errno(ret);
1641*4882a593Smuzhiyun out:
1642*4882a593Smuzhiyun brelse(expand_bh);
1643*4882a593Smuzhiyun return ret;
1644*4882a593Smuzhiyun }
1645*4882a593Smuzhiyun
1646*4882a593Smuzhiyun /*
1647*4882a593Smuzhiyun * Adjust the extent rec in b-tree representing ref_leaf_bh.
1648*4882a593Smuzhiyun *
1649*4882a593Smuzhiyun * Only called when we have inserted a new refcount rec at index 0
1650*4882a593Smuzhiyun * which means ocfs2_extent_rec.e_cpos may need some change.
1651*4882a593Smuzhiyun */
ocfs2_adjust_refcount_rec(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head * ref_leaf_bh,struct ocfs2_refcount_rec * rec)1652*4882a593Smuzhiyun static int ocfs2_adjust_refcount_rec(handle_t *handle,
1653*4882a593Smuzhiyun struct ocfs2_caching_info *ci,
1654*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
1655*4882a593Smuzhiyun struct buffer_head *ref_leaf_bh,
1656*4882a593Smuzhiyun struct ocfs2_refcount_rec *rec)
1657*4882a593Smuzhiyun {
1658*4882a593Smuzhiyun int ret = 0, i;
1659*4882a593Smuzhiyun u32 new_cpos, old_cpos;
1660*4882a593Smuzhiyun struct ocfs2_path *path = NULL;
1661*4882a593Smuzhiyun struct ocfs2_extent_tree et;
1662*4882a593Smuzhiyun struct ocfs2_refcount_block *rb =
1663*4882a593Smuzhiyun (struct ocfs2_refcount_block *)ref_root_bh->b_data;
1664*4882a593Smuzhiyun struct ocfs2_extent_list *el;
1665*4882a593Smuzhiyun
1666*4882a593Smuzhiyun if (!(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL))
1667*4882a593Smuzhiyun goto out;
1668*4882a593Smuzhiyun
1669*4882a593Smuzhiyun rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1670*4882a593Smuzhiyun old_cpos = le32_to_cpu(rb->rf_cpos);
1671*4882a593Smuzhiyun new_cpos = le64_to_cpu(rec->r_cpos) & OCFS2_32BIT_POS_MASK;
1672*4882a593Smuzhiyun if (old_cpos <= new_cpos)
1673*4882a593Smuzhiyun goto out;
1674*4882a593Smuzhiyun
1675*4882a593Smuzhiyun ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
1676*4882a593Smuzhiyun
1677*4882a593Smuzhiyun path = ocfs2_new_path_from_et(&et);
1678*4882a593Smuzhiyun if (!path) {
1679*4882a593Smuzhiyun ret = -ENOMEM;
1680*4882a593Smuzhiyun mlog_errno(ret);
1681*4882a593Smuzhiyun goto out;
1682*4882a593Smuzhiyun }
1683*4882a593Smuzhiyun
1684*4882a593Smuzhiyun ret = ocfs2_find_path(ci, path, old_cpos);
1685*4882a593Smuzhiyun if (ret) {
1686*4882a593Smuzhiyun mlog_errno(ret);
1687*4882a593Smuzhiyun goto out;
1688*4882a593Smuzhiyun }
1689*4882a593Smuzhiyun
1690*4882a593Smuzhiyun /*
1691*4882a593Smuzhiyun * 2 more credits, one for the leaf refcount block, one for
1692*4882a593Smuzhiyun * the extent block contains the extent rec.
1693*4882a593Smuzhiyun */
1694*4882a593Smuzhiyun ret = ocfs2_extend_trans(handle, 2);
1695*4882a593Smuzhiyun if (ret < 0) {
1696*4882a593Smuzhiyun mlog_errno(ret);
1697*4882a593Smuzhiyun goto out;
1698*4882a593Smuzhiyun }
1699*4882a593Smuzhiyun
1700*4882a593Smuzhiyun ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1701*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
1702*4882a593Smuzhiyun if (ret < 0) {
1703*4882a593Smuzhiyun mlog_errno(ret);
1704*4882a593Smuzhiyun goto out;
1705*4882a593Smuzhiyun }
1706*4882a593Smuzhiyun
1707*4882a593Smuzhiyun ret = ocfs2_journal_access_eb(handle, ci, path_leaf_bh(path),
1708*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
1709*4882a593Smuzhiyun if (ret < 0) {
1710*4882a593Smuzhiyun mlog_errno(ret);
1711*4882a593Smuzhiyun goto out;
1712*4882a593Smuzhiyun }
1713*4882a593Smuzhiyun
1714*4882a593Smuzhiyun /* change the leaf extent block first. */
1715*4882a593Smuzhiyun el = path_leaf_el(path);
1716*4882a593Smuzhiyun
1717*4882a593Smuzhiyun for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++)
1718*4882a593Smuzhiyun if (le32_to_cpu(el->l_recs[i].e_cpos) == old_cpos)
1719*4882a593Smuzhiyun break;
1720*4882a593Smuzhiyun
1721*4882a593Smuzhiyun BUG_ON(i == le16_to_cpu(el->l_next_free_rec));
1722*4882a593Smuzhiyun
1723*4882a593Smuzhiyun el->l_recs[i].e_cpos = cpu_to_le32(new_cpos);
1724*4882a593Smuzhiyun
1725*4882a593Smuzhiyun /* change the r_cpos in the leaf block. */
1726*4882a593Smuzhiyun rb->rf_cpos = cpu_to_le32(new_cpos);
1727*4882a593Smuzhiyun
1728*4882a593Smuzhiyun ocfs2_journal_dirty(handle, path_leaf_bh(path));
1729*4882a593Smuzhiyun ocfs2_journal_dirty(handle, ref_leaf_bh);
1730*4882a593Smuzhiyun
1731*4882a593Smuzhiyun out:
1732*4882a593Smuzhiyun ocfs2_free_path(path);
1733*4882a593Smuzhiyun return ret;
1734*4882a593Smuzhiyun }
1735*4882a593Smuzhiyun
ocfs2_insert_refcount_rec(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head * ref_leaf_bh,struct ocfs2_refcount_rec * rec,int index,int merge,struct ocfs2_alloc_context * meta_ac)1736*4882a593Smuzhiyun static int ocfs2_insert_refcount_rec(handle_t *handle,
1737*4882a593Smuzhiyun struct ocfs2_caching_info *ci,
1738*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
1739*4882a593Smuzhiyun struct buffer_head *ref_leaf_bh,
1740*4882a593Smuzhiyun struct ocfs2_refcount_rec *rec,
1741*4882a593Smuzhiyun int index, int merge,
1742*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac)
1743*4882a593Smuzhiyun {
1744*4882a593Smuzhiyun int ret;
1745*4882a593Smuzhiyun struct ocfs2_refcount_block *rb =
1746*4882a593Smuzhiyun (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1747*4882a593Smuzhiyun struct ocfs2_refcount_list *rf_list = &rb->rf_records;
1748*4882a593Smuzhiyun struct buffer_head *new_bh = NULL;
1749*4882a593Smuzhiyun
1750*4882a593Smuzhiyun BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
1751*4882a593Smuzhiyun
1752*4882a593Smuzhiyun if (rf_list->rl_used == rf_list->rl_count) {
1753*4882a593Smuzhiyun u64 cpos = le64_to_cpu(rec->r_cpos);
1754*4882a593Smuzhiyun u32 len = le32_to_cpu(rec->r_clusters);
1755*4882a593Smuzhiyun
1756*4882a593Smuzhiyun ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1757*4882a593Smuzhiyun ref_leaf_bh, meta_ac);
1758*4882a593Smuzhiyun if (ret) {
1759*4882a593Smuzhiyun mlog_errno(ret);
1760*4882a593Smuzhiyun goto out;
1761*4882a593Smuzhiyun }
1762*4882a593Smuzhiyun
1763*4882a593Smuzhiyun ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1764*4882a593Smuzhiyun cpos, len, NULL, &index,
1765*4882a593Smuzhiyun &new_bh);
1766*4882a593Smuzhiyun if (ret) {
1767*4882a593Smuzhiyun mlog_errno(ret);
1768*4882a593Smuzhiyun goto out;
1769*4882a593Smuzhiyun }
1770*4882a593Smuzhiyun
1771*4882a593Smuzhiyun ref_leaf_bh = new_bh;
1772*4882a593Smuzhiyun rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1773*4882a593Smuzhiyun rf_list = &rb->rf_records;
1774*4882a593Smuzhiyun }
1775*4882a593Smuzhiyun
1776*4882a593Smuzhiyun ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1777*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
1778*4882a593Smuzhiyun if (ret) {
1779*4882a593Smuzhiyun mlog_errno(ret);
1780*4882a593Smuzhiyun goto out;
1781*4882a593Smuzhiyun }
1782*4882a593Smuzhiyun
1783*4882a593Smuzhiyun if (index < le16_to_cpu(rf_list->rl_used))
1784*4882a593Smuzhiyun memmove(&rf_list->rl_recs[index + 1],
1785*4882a593Smuzhiyun &rf_list->rl_recs[index],
1786*4882a593Smuzhiyun (le16_to_cpu(rf_list->rl_used) - index) *
1787*4882a593Smuzhiyun sizeof(struct ocfs2_refcount_rec));
1788*4882a593Smuzhiyun
1789*4882a593Smuzhiyun trace_ocfs2_insert_refcount_rec(
1790*4882a593Smuzhiyun (unsigned long long)ref_leaf_bh->b_blocknr, index,
1791*4882a593Smuzhiyun (unsigned long long)le64_to_cpu(rec->r_cpos),
1792*4882a593Smuzhiyun le32_to_cpu(rec->r_clusters), le32_to_cpu(rec->r_refcount));
1793*4882a593Smuzhiyun
1794*4882a593Smuzhiyun rf_list->rl_recs[index] = *rec;
1795*4882a593Smuzhiyun
1796*4882a593Smuzhiyun le16_add_cpu(&rf_list->rl_used, 1);
1797*4882a593Smuzhiyun
1798*4882a593Smuzhiyun if (merge)
1799*4882a593Smuzhiyun ocfs2_refcount_rec_merge(rb, index);
1800*4882a593Smuzhiyun
1801*4882a593Smuzhiyun ocfs2_journal_dirty(handle, ref_leaf_bh);
1802*4882a593Smuzhiyun
1803*4882a593Smuzhiyun if (index == 0) {
1804*4882a593Smuzhiyun ret = ocfs2_adjust_refcount_rec(handle, ci,
1805*4882a593Smuzhiyun ref_root_bh,
1806*4882a593Smuzhiyun ref_leaf_bh, rec);
1807*4882a593Smuzhiyun if (ret)
1808*4882a593Smuzhiyun mlog_errno(ret);
1809*4882a593Smuzhiyun }
1810*4882a593Smuzhiyun out:
1811*4882a593Smuzhiyun brelse(new_bh);
1812*4882a593Smuzhiyun return ret;
1813*4882a593Smuzhiyun }
1814*4882a593Smuzhiyun
1815*4882a593Smuzhiyun /*
1816*4882a593Smuzhiyun * Split the refcount_rec indexed by "index" in ref_leaf_bh.
1817*4882a593Smuzhiyun * This is much simple than our b-tree code.
1818*4882a593Smuzhiyun * split_rec is the new refcount rec we want to insert.
1819*4882a593Smuzhiyun * If split_rec->r_refcount > 0, we are changing the refcount(in case we
1820*4882a593Smuzhiyun * increase refcount or decrease a refcount to non-zero).
1821*4882a593Smuzhiyun * If split_rec->r_refcount == 0, we are punching a hole in current refcount
1822*4882a593Smuzhiyun * rec( in case we decrease a refcount to zero).
1823*4882a593Smuzhiyun */
ocfs2_split_refcount_rec(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head * ref_leaf_bh,struct ocfs2_refcount_rec * split_rec,int index,int merge,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)1824*4882a593Smuzhiyun static int ocfs2_split_refcount_rec(handle_t *handle,
1825*4882a593Smuzhiyun struct ocfs2_caching_info *ci,
1826*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
1827*4882a593Smuzhiyun struct buffer_head *ref_leaf_bh,
1828*4882a593Smuzhiyun struct ocfs2_refcount_rec *split_rec,
1829*4882a593Smuzhiyun int index, int merge,
1830*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac,
1831*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc)
1832*4882a593Smuzhiyun {
1833*4882a593Smuzhiyun int ret, recs_need;
1834*4882a593Smuzhiyun u32 len;
1835*4882a593Smuzhiyun struct ocfs2_refcount_block *rb =
1836*4882a593Smuzhiyun (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1837*4882a593Smuzhiyun struct ocfs2_refcount_list *rf_list = &rb->rf_records;
1838*4882a593Smuzhiyun struct ocfs2_refcount_rec *orig_rec = &rf_list->rl_recs[index];
1839*4882a593Smuzhiyun struct ocfs2_refcount_rec *tail_rec = NULL;
1840*4882a593Smuzhiyun struct buffer_head *new_bh = NULL;
1841*4882a593Smuzhiyun
1842*4882a593Smuzhiyun BUG_ON(le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL);
1843*4882a593Smuzhiyun
1844*4882a593Smuzhiyun trace_ocfs2_split_refcount_rec(le64_to_cpu(orig_rec->r_cpos),
1845*4882a593Smuzhiyun le32_to_cpu(orig_rec->r_clusters),
1846*4882a593Smuzhiyun le32_to_cpu(orig_rec->r_refcount),
1847*4882a593Smuzhiyun le64_to_cpu(split_rec->r_cpos),
1848*4882a593Smuzhiyun le32_to_cpu(split_rec->r_clusters),
1849*4882a593Smuzhiyun le32_to_cpu(split_rec->r_refcount));
1850*4882a593Smuzhiyun
1851*4882a593Smuzhiyun /*
1852*4882a593Smuzhiyun * If we just need to split the header or tail clusters,
1853*4882a593Smuzhiyun * no more recs are needed, just split is OK.
1854*4882a593Smuzhiyun * Otherwise we at least need one new recs.
1855*4882a593Smuzhiyun */
1856*4882a593Smuzhiyun if (!split_rec->r_refcount &&
1857*4882a593Smuzhiyun (split_rec->r_cpos == orig_rec->r_cpos ||
1858*4882a593Smuzhiyun le64_to_cpu(split_rec->r_cpos) +
1859*4882a593Smuzhiyun le32_to_cpu(split_rec->r_clusters) ==
1860*4882a593Smuzhiyun le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
1861*4882a593Smuzhiyun recs_need = 0;
1862*4882a593Smuzhiyun else
1863*4882a593Smuzhiyun recs_need = 1;
1864*4882a593Smuzhiyun
1865*4882a593Smuzhiyun /*
1866*4882a593Smuzhiyun * We need one more rec if we split in the middle and the new rec have
1867*4882a593Smuzhiyun * some refcount in it.
1868*4882a593Smuzhiyun */
1869*4882a593Smuzhiyun if (split_rec->r_refcount &&
1870*4882a593Smuzhiyun (split_rec->r_cpos != orig_rec->r_cpos &&
1871*4882a593Smuzhiyun le64_to_cpu(split_rec->r_cpos) +
1872*4882a593Smuzhiyun le32_to_cpu(split_rec->r_clusters) !=
1873*4882a593Smuzhiyun le64_to_cpu(orig_rec->r_cpos) + le32_to_cpu(orig_rec->r_clusters)))
1874*4882a593Smuzhiyun recs_need++;
1875*4882a593Smuzhiyun
1876*4882a593Smuzhiyun /* If the leaf block don't have enough record, expand it. */
1877*4882a593Smuzhiyun if (le16_to_cpu(rf_list->rl_used) + recs_need >
1878*4882a593Smuzhiyun le16_to_cpu(rf_list->rl_count)) {
1879*4882a593Smuzhiyun struct ocfs2_refcount_rec tmp_rec;
1880*4882a593Smuzhiyun u64 cpos = le64_to_cpu(orig_rec->r_cpos);
1881*4882a593Smuzhiyun len = le32_to_cpu(orig_rec->r_clusters);
1882*4882a593Smuzhiyun ret = ocfs2_expand_refcount_tree(handle, ci, ref_root_bh,
1883*4882a593Smuzhiyun ref_leaf_bh, meta_ac);
1884*4882a593Smuzhiyun if (ret) {
1885*4882a593Smuzhiyun mlog_errno(ret);
1886*4882a593Smuzhiyun goto out;
1887*4882a593Smuzhiyun }
1888*4882a593Smuzhiyun
1889*4882a593Smuzhiyun /*
1890*4882a593Smuzhiyun * We have to re-get it since now cpos may be moved to
1891*4882a593Smuzhiyun * another leaf block.
1892*4882a593Smuzhiyun */
1893*4882a593Smuzhiyun ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1894*4882a593Smuzhiyun cpos, len, &tmp_rec, &index,
1895*4882a593Smuzhiyun &new_bh);
1896*4882a593Smuzhiyun if (ret) {
1897*4882a593Smuzhiyun mlog_errno(ret);
1898*4882a593Smuzhiyun goto out;
1899*4882a593Smuzhiyun }
1900*4882a593Smuzhiyun
1901*4882a593Smuzhiyun ref_leaf_bh = new_bh;
1902*4882a593Smuzhiyun rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
1903*4882a593Smuzhiyun rf_list = &rb->rf_records;
1904*4882a593Smuzhiyun orig_rec = &rf_list->rl_recs[index];
1905*4882a593Smuzhiyun }
1906*4882a593Smuzhiyun
1907*4882a593Smuzhiyun ret = ocfs2_journal_access_rb(handle, ci, ref_leaf_bh,
1908*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
1909*4882a593Smuzhiyun if (ret) {
1910*4882a593Smuzhiyun mlog_errno(ret);
1911*4882a593Smuzhiyun goto out;
1912*4882a593Smuzhiyun }
1913*4882a593Smuzhiyun
1914*4882a593Smuzhiyun /*
1915*4882a593Smuzhiyun * We have calculated out how many new records we need and store
1916*4882a593Smuzhiyun * in recs_need, so spare enough space first by moving the records
1917*4882a593Smuzhiyun * after "index" to the end.
1918*4882a593Smuzhiyun */
1919*4882a593Smuzhiyun if (index != le16_to_cpu(rf_list->rl_used) - 1)
1920*4882a593Smuzhiyun memmove(&rf_list->rl_recs[index + 1 + recs_need],
1921*4882a593Smuzhiyun &rf_list->rl_recs[index + 1],
1922*4882a593Smuzhiyun (le16_to_cpu(rf_list->rl_used) - index - 1) *
1923*4882a593Smuzhiyun sizeof(struct ocfs2_refcount_rec));
1924*4882a593Smuzhiyun
1925*4882a593Smuzhiyun len = (le64_to_cpu(orig_rec->r_cpos) +
1926*4882a593Smuzhiyun le32_to_cpu(orig_rec->r_clusters)) -
1927*4882a593Smuzhiyun (le64_to_cpu(split_rec->r_cpos) +
1928*4882a593Smuzhiyun le32_to_cpu(split_rec->r_clusters));
1929*4882a593Smuzhiyun
1930*4882a593Smuzhiyun /*
1931*4882a593Smuzhiyun * If we have "len", the we will split in the tail and move it
1932*4882a593Smuzhiyun * to the end of the space we have just spared.
1933*4882a593Smuzhiyun */
1934*4882a593Smuzhiyun if (len) {
1935*4882a593Smuzhiyun tail_rec = &rf_list->rl_recs[index + recs_need];
1936*4882a593Smuzhiyun
1937*4882a593Smuzhiyun memcpy(tail_rec, orig_rec, sizeof(struct ocfs2_refcount_rec));
1938*4882a593Smuzhiyun le64_add_cpu(&tail_rec->r_cpos,
1939*4882a593Smuzhiyun le32_to_cpu(tail_rec->r_clusters) - len);
1940*4882a593Smuzhiyun tail_rec->r_clusters = cpu_to_le32(len);
1941*4882a593Smuzhiyun }
1942*4882a593Smuzhiyun
1943*4882a593Smuzhiyun /*
1944*4882a593Smuzhiyun * If the split pos isn't the same as the original one, we need to
1945*4882a593Smuzhiyun * split in the head.
1946*4882a593Smuzhiyun *
1947*4882a593Smuzhiyun * Note: We have the chance that split_rec.r_refcount = 0,
1948*4882a593Smuzhiyun * recs_need = 0 and len > 0, which means we just cut the head from
1949*4882a593Smuzhiyun * the orig_rec and in that case we have done some modification in
1950*4882a593Smuzhiyun * orig_rec above, so the check for r_cpos is faked.
1951*4882a593Smuzhiyun */
1952*4882a593Smuzhiyun if (split_rec->r_cpos != orig_rec->r_cpos && tail_rec != orig_rec) {
1953*4882a593Smuzhiyun len = le64_to_cpu(split_rec->r_cpos) -
1954*4882a593Smuzhiyun le64_to_cpu(orig_rec->r_cpos);
1955*4882a593Smuzhiyun orig_rec->r_clusters = cpu_to_le32(len);
1956*4882a593Smuzhiyun index++;
1957*4882a593Smuzhiyun }
1958*4882a593Smuzhiyun
1959*4882a593Smuzhiyun le16_add_cpu(&rf_list->rl_used, recs_need);
1960*4882a593Smuzhiyun
1961*4882a593Smuzhiyun if (split_rec->r_refcount) {
1962*4882a593Smuzhiyun rf_list->rl_recs[index] = *split_rec;
1963*4882a593Smuzhiyun trace_ocfs2_split_refcount_rec_insert(
1964*4882a593Smuzhiyun (unsigned long long)ref_leaf_bh->b_blocknr, index,
1965*4882a593Smuzhiyun (unsigned long long)le64_to_cpu(split_rec->r_cpos),
1966*4882a593Smuzhiyun le32_to_cpu(split_rec->r_clusters),
1967*4882a593Smuzhiyun le32_to_cpu(split_rec->r_refcount));
1968*4882a593Smuzhiyun
1969*4882a593Smuzhiyun if (merge)
1970*4882a593Smuzhiyun ocfs2_refcount_rec_merge(rb, index);
1971*4882a593Smuzhiyun }
1972*4882a593Smuzhiyun
1973*4882a593Smuzhiyun ocfs2_journal_dirty(handle, ref_leaf_bh);
1974*4882a593Smuzhiyun
1975*4882a593Smuzhiyun out:
1976*4882a593Smuzhiyun brelse(new_bh);
1977*4882a593Smuzhiyun return ret;
1978*4882a593Smuzhiyun }
1979*4882a593Smuzhiyun
__ocfs2_increase_refcount(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,u64 cpos,u32 len,int merge,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)1980*4882a593Smuzhiyun static int __ocfs2_increase_refcount(handle_t *handle,
1981*4882a593Smuzhiyun struct ocfs2_caching_info *ci,
1982*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
1983*4882a593Smuzhiyun u64 cpos, u32 len, int merge,
1984*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac,
1985*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc)
1986*4882a593Smuzhiyun {
1987*4882a593Smuzhiyun int ret = 0, index;
1988*4882a593Smuzhiyun struct buffer_head *ref_leaf_bh = NULL;
1989*4882a593Smuzhiyun struct ocfs2_refcount_rec rec;
1990*4882a593Smuzhiyun unsigned int set_len = 0;
1991*4882a593Smuzhiyun
1992*4882a593Smuzhiyun trace_ocfs2_increase_refcount_begin(
1993*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
1994*4882a593Smuzhiyun (unsigned long long)cpos, len);
1995*4882a593Smuzhiyun
1996*4882a593Smuzhiyun while (len) {
1997*4882a593Smuzhiyun ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
1998*4882a593Smuzhiyun cpos, len, &rec, &index,
1999*4882a593Smuzhiyun &ref_leaf_bh);
2000*4882a593Smuzhiyun if (ret) {
2001*4882a593Smuzhiyun mlog_errno(ret);
2002*4882a593Smuzhiyun goto out;
2003*4882a593Smuzhiyun }
2004*4882a593Smuzhiyun
2005*4882a593Smuzhiyun set_len = le32_to_cpu(rec.r_clusters);
2006*4882a593Smuzhiyun
2007*4882a593Smuzhiyun /*
2008*4882a593Smuzhiyun * Here we may meet with 3 situations:
2009*4882a593Smuzhiyun *
2010*4882a593Smuzhiyun * 1. If we find an already existing record, and the length
2011*4882a593Smuzhiyun * is the same, cool, we just need to increase the r_refcount
2012*4882a593Smuzhiyun * and it is OK.
2013*4882a593Smuzhiyun * 2. If we find a hole, just insert it with r_refcount = 1.
2014*4882a593Smuzhiyun * 3. If we are in the middle of one extent record, split
2015*4882a593Smuzhiyun * it.
2016*4882a593Smuzhiyun */
2017*4882a593Smuzhiyun if (rec.r_refcount && le64_to_cpu(rec.r_cpos) == cpos &&
2018*4882a593Smuzhiyun set_len <= len) {
2019*4882a593Smuzhiyun trace_ocfs2_increase_refcount_change(
2020*4882a593Smuzhiyun (unsigned long long)cpos, set_len,
2021*4882a593Smuzhiyun le32_to_cpu(rec.r_refcount));
2022*4882a593Smuzhiyun ret = ocfs2_change_refcount_rec(handle, ci,
2023*4882a593Smuzhiyun ref_leaf_bh, index,
2024*4882a593Smuzhiyun merge, 1);
2025*4882a593Smuzhiyun if (ret) {
2026*4882a593Smuzhiyun mlog_errno(ret);
2027*4882a593Smuzhiyun goto out;
2028*4882a593Smuzhiyun }
2029*4882a593Smuzhiyun } else if (!rec.r_refcount) {
2030*4882a593Smuzhiyun rec.r_refcount = cpu_to_le32(1);
2031*4882a593Smuzhiyun
2032*4882a593Smuzhiyun trace_ocfs2_increase_refcount_insert(
2033*4882a593Smuzhiyun (unsigned long long)le64_to_cpu(rec.r_cpos),
2034*4882a593Smuzhiyun set_len);
2035*4882a593Smuzhiyun ret = ocfs2_insert_refcount_rec(handle, ci, ref_root_bh,
2036*4882a593Smuzhiyun ref_leaf_bh,
2037*4882a593Smuzhiyun &rec, index,
2038*4882a593Smuzhiyun merge, meta_ac);
2039*4882a593Smuzhiyun if (ret) {
2040*4882a593Smuzhiyun mlog_errno(ret);
2041*4882a593Smuzhiyun goto out;
2042*4882a593Smuzhiyun }
2043*4882a593Smuzhiyun } else {
2044*4882a593Smuzhiyun set_len = min((u64)(cpos + len),
2045*4882a593Smuzhiyun le64_to_cpu(rec.r_cpos) + set_len) - cpos;
2046*4882a593Smuzhiyun rec.r_cpos = cpu_to_le64(cpos);
2047*4882a593Smuzhiyun rec.r_clusters = cpu_to_le32(set_len);
2048*4882a593Smuzhiyun le32_add_cpu(&rec.r_refcount, 1);
2049*4882a593Smuzhiyun
2050*4882a593Smuzhiyun trace_ocfs2_increase_refcount_split(
2051*4882a593Smuzhiyun (unsigned long long)le64_to_cpu(rec.r_cpos),
2052*4882a593Smuzhiyun set_len, le32_to_cpu(rec.r_refcount));
2053*4882a593Smuzhiyun ret = ocfs2_split_refcount_rec(handle, ci,
2054*4882a593Smuzhiyun ref_root_bh, ref_leaf_bh,
2055*4882a593Smuzhiyun &rec, index, merge,
2056*4882a593Smuzhiyun meta_ac, dealloc);
2057*4882a593Smuzhiyun if (ret) {
2058*4882a593Smuzhiyun mlog_errno(ret);
2059*4882a593Smuzhiyun goto out;
2060*4882a593Smuzhiyun }
2061*4882a593Smuzhiyun }
2062*4882a593Smuzhiyun
2063*4882a593Smuzhiyun cpos += set_len;
2064*4882a593Smuzhiyun len -= set_len;
2065*4882a593Smuzhiyun brelse(ref_leaf_bh);
2066*4882a593Smuzhiyun ref_leaf_bh = NULL;
2067*4882a593Smuzhiyun }
2068*4882a593Smuzhiyun
2069*4882a593Smuzhiyun out:
2070*4882a593Smuzhiyun brelse(ref_leaf_bh);
2071*4882a593Smuzhiyun return ret;
2072*4882a593Smuzhiyun }
2073*4882a593Smuzhiyun
ocfs2_remove_refcount_extent(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head * ref_leaf_bh,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)2074*4882a593Smuzhiyun static int ocfs2_remove_refcount_extent(handle_t *handle,
2075*4882a593Smuzhiyun struct ocfs2_caching_info *ci,
2076*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
2077*4882a593Smuzhiyun struct buffer_head *ref_leaf_bh,
2078*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac,
2079*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc)
2080*4882a593Smuzhiyun {
2081*4882a593Smuzhiyun int ret;
2082*4882a593Smuzhiyun struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
2083*4882a593Smuzhiyun struct ocfs2_refcount_block *rb =
2084*4882a593Smuzhiyun (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
2085*4882a593Smuzhiyun struct ocfs2_extent_tree et;
2086*4882a593Smuzhiyun
2087*4882a593Smuzhiyun BUG_ON(rb->rf_records.rl_used);
2088*4882a593Smuzhiyun
2089*4882a593Smuzhiyun trace_ocfs2_remove_refcount_extent(
2090*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
2091*4882a593Smuzhiyun (unsigned long long)ref_leaf_bh->b_blocknr,
2092*4882a593Smuzhiyun le32_to_cpu(rb->rf_cpos));
2093*4882a593Smuzhiyun
2094*4882a593Smuzhiyun ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
2095*4882a593Smuzhiyun ret = ocfs2_remove_extent(handle, &et, le32_to_cpu(rb->rf_cpos),
2096*4882a593Smuzhiyun 1, meta_ac, dealloc);
2097*4882a593Smuzhiyun if (ret) {
2098*4882a593Smuzhiyun mlog_errno(ret);
2099*4882a593Smuzhiyun goto out;
2100*4882a593Smuzhiyun }
2101*4882a593Smuzhiyun
2102*4882a593Smuzhiyun ocfs2_remove_from_cache(ci, ref_leaf_bh);
2103*4882a593Smuzhiyun
2104*4882a593Smuzhiyun /*
2105*4882a593Smuzhiyun * add the freed block to the dealloc so that it will be freed
2106*4882a593Smuzhiyun * when we run dealloc.
2107*4882a593Smuzhiyun */
2108*4882a593Smuzhiyun ret = ocfs2_cache_block_dealloc(dealloc, EXTENT_ALLOC_SYSTEM_INODE,
2109*4882a593Smuzhiyun le16_to_cpu(rb->rf_suballoc_slot),
2110*4882a593Smuzhiyun le64_to_cpu(rb->rf_suballoc_loc),
2111*4882a593Smuzhiyun le64_to_cpu(rb->rf_blkno),
2112*4882a593Smuzhiyun le16_to_cpu(rb->rf_suballoc_bit));
2113*4882a593Smuzhiyun if (ret) {
2114*4882a593Smuzhiyun mlog_errno(ret);
2115*4882a593Smuzhiyun goto out;
2116*4882a593Smuzhiyun }
2117*4882a593Smuzhiyun
2118*4882a593Smuzhiyun ret = ocfs2_journal_access_rb(handle, ci, ref_root_bh,
2119*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
2120*4882a593Smuzhiyun if (ret) {
2121*4882a593Smuzhiyun mlog_errno(ret);
2122*4882a593Smuzhiyun goto out;
2123*4882a593Smuzhiyun }
2124*4882a593Smuzhiyun
2125*4882a593Smuzhiyun rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
2126*4882a593Smuzhiyun
2127*4882a593Smuzhiyun le32_add_cpu(&rb->rf_clusters, -1);
2128*4882a593Smuzhiyun
2129*4882a593Smuzhiyun /*
2130*4882a593Smuzhiyun * check whether we need to restore the root refcount block if
2131*4882a593Smuzhiyun * there is no leaf extent block at atll.
2132*4882a593Smuzhiyun */
2133*4882a593Smuzhiyun if (!rb->rf_list.l_next_free_rec) {
2134*4882a593Smuzhiyun BUG_ON(rb->rf_clusters);
2135*4882a593Smuzhiyun
2136*4882a593Smuzhiyun trace_ocfs2_restore_refcount_block(
2137*4882a593Smuzhiyun (unsigned long long)ref_root_bh->b_blocknr);
2138*4882a593Smuzhiyun
2139*4882a593Smuzhiyun rb->rf_flags = 0;
2140*4882a593Smuzhiyun rb->rf_parent = 0;
2141*4882a593Smuzhiyun rb->rf_cpos = 0;
2142*4882a593Smuzhiyun memset(&rb->rf_records, 0, sb->s_blocksize -
2143*4882a593Smuzhiyun offsetof(struct ocfs2_refcount_block, rf_records));
2144*4882a593Smuzhiyun rb->rf_records.rl_count =
2145*4882a593Smuzhiyun cpu_to_le16(ocfs2_refcount_recs_per_rb(sb));
2146*4882a593Smuzhiyun }
2147*4882a593Smuzhiyun
2148*4882a593Smuzhiyun ocfs2_journal_dirty(handle, ref_root_bh);
2149*4882a593Smuzhiyun
2150*4882a593Smuzhiyun out:
2151*4882a593Smuzhiyun return ret;
2152*4882a593Smuzhiyun }
2153*4882a593Smuzhiyun
ocfs2_increase_refcount(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,u64 cpos,u32 len,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)2154*4882a593Smuzhiyun int ocfs2_increase_refcount(handle_t *handle,
2155*4882a593Smuzhiyun struct ocfs2_caching_info *ci,
2156*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
2157*4882a593Smuzhiyun u64 cpos, u32 len,
2158*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac,
2159*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc)
2160*4882a593Smuzhiyun {
2161*4882a593Smuzhiyun return __ocfs2_increase_refcount(handle, ci, ref_root_bh,
2162*4882a593Smuzhiyun cpos, len, 1,
2163*4882a593Smuzhiyun meta_ac, dealloc);
2164*4882a593Smuzhiyun }
2165*4882a593Smuzhiyun
ocfs2_decrease_refcount_rec(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,struct buffer_head * ref_leaf_bh,int index,u64 cpos,unsigned int len,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)2166*4882a593Smuzhiyun static int ocfs2_decrease_refcount_rec(handle_t *handle,
2167*4882a593Smuzhiyun struct ocfs2_caching_info *ci,
2168*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
2169*4882a593Smuzhiyun struct buffer_head *ref_leaf_bh,
2170*4882a593Smuzhiyun int index, u64 cpos, unsigned int len,
2171*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac,
2172*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc)
2173*4882a593Smuzhiyun {
2174*4882a593Smuzhiyun int ret;
2175*4882a593Smuzhiyun struct ocfs2_refcount_block *rb =
2176*4882a593Smuzhiyun (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
2177*4882a593Smuzhiyun struct ocfs2_refcount_rec *rec = &rb->rf_records.rl_recs[index];
2178*4882a593Smuzhiyun
2179*4882a593Smuzhiyun BUG_ON(cpos < le64_to_cpu(rec->r_cpos));
2180*4882a593Smuzhiyun BUG_ON(cpos + len >
2181*4882a593Smuzhiyun le64_to_cpu(rec->r_cpos) + le32_to_cpu(rec->r_clusters));
2182*4882a593Smuzhiyun
2183*4882a593Smuzhiyun trace_ocfs2_decrease_refcount_rec(
2184*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
2185*4882a593Smuzhiyun (unsigned long long)cpos, len);
2186*4882a593Smuzhiyun
2187*4882a593Smuzhiyun if (cpos == le64_to_cpu(rec->r_cpos) &&
2188*4882a593Smuzhiyun len == le32_to_cpu(rec->r_clusters))
2189*4882a593Smuzhiyun ret = ocfs2_change_refcount_rec(handle, ci,
2190*4882a593Smuzhiyun ref_leaf_bh, index, 1, -1);
2191*4882a593Smuzhiyun else {
2192*4882a593Smuzhiyun struct ocfs2_refcount_rec split = *rec;
2193*4882a593Smuzhiyun split.r_cpos = cpu_to_le64(cpos);
2194*4882a593Smuzhiyun split.r_clusters = cpu_to_le32(len);
2195*4882a593Smuzhiyun
2196*4882a593Smuzhiyun le32_add_cpu(&split.r_refcount, -1);
2197*4882a593Smuzhiyun
2198*4882a593Smuzhiyun ret = ocfs2_split_refcount_rec(handle, ci,
2199*4882a593Smuzhiyun ref_root_bh, ref_leaf_bh,
2200*4882a593Smuzhiyun &split, index, 1,
2201*4882a593Smuzhiyun meta_ac, dealloc);
2202*4882a593Smuzhiyun }
2203*4882a593Smuzhiyun
2204*4882a593Smuzhiyun if (ret) {
2205*4882a593Smuzhiyun mlog_errno(ret);
2206*4882a593Smuzhiyun goto out;
2207*4882a593Smuzhiyun }
2208*4882a593Smuzhiyun
2209*4882a593Smuzhiyun /* Remove the leaf refcount block if it contains no refcount record. */
2210*4882a593Smuzhiyun if (!rb->rf_records.rl_used && ref_leaf_bh != ref_root_bh) {
2211*4882a593Smuzhiyun ret = ocfs2_remove_refcount_extent(handle, ci, ref_root_bh,
2212*4882a593Smuzhiyun ref_leaf_bh, meta_ac,
2213*4882a593Smuzhiyun dealloc);
2214*4882a593Smuzhiyun if (ret)
2215*4882a593Smuzhiyun mlog_errno(ret);
2216*4882a593Smuzhiyun }
2217*4882a593Smuzhiyun
2218*4882a593Smuzhiyun out:
2219*4882a593Smuzhiyun return ret;
2220*4882a593Smuzhiyun }
2221*4882a593Smuzhiyun
__ocfs2_decrease_refcount(handle_t * handle,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,u64 cpos,u32 len,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc,int delete)2222*4882a593Smuzhiyun static int __ocfs2_decrease_refcount(handle_t *handle,
2223*4882a593Smuzhiyun struct ocfs2_caching_info *ci,
2224*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
2225*4882a593Smuzhiyun u64 cpos, u32 len,
2226*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac,
2227*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc,
2228*4882a593Smuzhiyun int delete)
2229*4882a593Smuzhiyun {
2230*4882a593Smuzhiyun int ret = 0, index = 0;
2231*4882a593Smuzhiyun struct ocfs2_refcount_rec rec;
2232*4882a593Smuzhiyun unsigned int r_count = 0, r_len;
2233*4882a593Smuzhiyun struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
2234*4882a593Smuzhiyun struct buffer_head *ref_leaf_bh = NULL;
2235*4882a593Smuzhiyun
2236*4882a593Smuzhiyun trace_ocfs2_decrease_refcount(
2237*4882a593Smuzhiyun (unsigned long long)ocfs2_metadata_cache_owner(ci),
2238*4882a593Smuzhiyun (unsigned long long)cpos, len, delete);
2239*4882a593Smuzhiyun
2240*4882a593Smuzhiyun while (len) {
2241*4882a593Smuzhiyun ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
2242*4882a593Smuzhiyun cpos, len, &rec, &index,
2243*4882a593Smuzhiyun &ref_leaf_bh);
2244*4882a593Smuzhiyun if (ret) {
2245*4882a593Smuzhiyun mlog_errno(ret);
2246*4882a593Smuzhiyun goto out;
2247*4882a593Smuzhiyun }
2248*4882a593Smuzhiyun
2249*4882a593Smuzhiyun r_count = le32_to_cpu(rec.r_refcount);
2250*4882a593Smuzhiyun BUG_ON(r_count == 0);
2251*4882a593Smuzhiyun if (!delete)
2252*4882a593Smuzhiyun BUG_ON(r_count > 1);
2253*4882a593Smuzhiyun
2254*4882a593Smuzhiyun r_len = min((u64)(cpos + len), le64_to_cpu(rec.r_cpos) +
2255*4882a593Smuzhiyun le32_to_cpu(rec.r_clusters)) - cpos;
2256*4882a593Smuzhiyun
2257*4882a593Smuzhiyun ret = ocfs2_decrease_refcount_rec(handle, ci, ref_root_bh,
2258*4882a593Smuzhiyun ref_leaf_bh, index,
2259*4882a593Smuzhiyun cpos, r_len,
2260*4882a593Smuzhiyun meta_ac, dealloc);
2261*4882a593Smuzhiyun if (ret) {
2262*4882a593Smuzhiyun mlog_errno(ret);
2263*4882a593Smuzhiyun goto out;
2264*4882a593Smuzhiyun }
2265*4882a593Smuzhiyun
2266*4882a593Smuzhiyun if (le32_to_cpu(rec.r_refcount) == 1 && delete) {
2267*4882a593Smuzhiyun ret = ocfs2_cache_cluster_dealloc(dealloc,
2268*4882a593Smuzhiyun ocfs2_clusters_to_blocks(sb, cpos),
2269*4882a593Smuzhiyun r_len);
2270*4882a593Smuzhiyun if (ret) {
2271*4882a593Smuzhiyun mlog_errno(ret);
2272*4882a593Smuzhiyun goto out;
2273*4882a593Smuzhiyun }
2274*4882a593Smuzhiyun }
2275*4882a593Smuzhiyun
2276*4882a593Smuzhiyun cpos += r_len;
2277*4882a593Smuzhiyun len -= r_len;
2278*4882a593Smuzhiyun brelse(ref_leaf_bh);
2279*4882a593Smuzhiyun ref_leaf_bh = NULL;
2280*4882a593Smuzhiyun }
2281*4882a593Smuzhiyun
2282*4882a593Smuzhiyun out:
2283*4882a593Smuzhiyun brelse(ref_leaf_bh);
2284*4882a593Smuzhiyun return ret;
2285*4882a593Smuzhiyun }
2286*4882a593Smuzhiyun
2287*4882a593Smuzhiyun /* Caller must hold refcount tree lock. */
ocfs2_decrease_refcount(struct inode * inode,handle_t * handle,u32 cpos,u32 len,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc,int delete)2288*4882a593Smuzhiyun int ocfs2_decrease_refcount(struct inode *inode,
2289*4882a593Smuzhiyun handle_t *handle, u32 cpos, u32 len,
2290*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac,
2291*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc,
2292*4882a593Smuzhiyun int delete)
2293*4882a593Smuzhiyun {
2294*4882a593Smuzhiyun int ret;
2295*4882a593Smuzhiyun u64 ref_blkno;
2296*4882a593Smuzhiyun struct buffer_head *ref_root_bh = NULL;
2297*4882a593Smuzhiyun struct ocfs2_refcount_tree *tree;
2298*4882a593Smuzhiyun
2299*4882a593Smuzhiyun BUG_ON(!ocfs2_is_refcount_inode(inode));
2300*4882a593Smuzhiyun
2301*4882a593Smuzhiyun ret = ocfs2_get_refcount_block(inode, &ref_blkno);
2302*4882a593Smuzhiyun if (ret) {
2303*4882a593Smuzhiyun mlog_errno(ret);
2304*4882a593Smuzhiyun goto out;
2305*4882a593Smuzhiyun }
2306*4882a593Smuzhiyun
2307*4882a593Smuzhiyun ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb), ref_blkno, &tree);
2308*4882a593Smuzhiyun if (ret) {
2309*4882a593Smuzhiyun mlog_errno(ret);
2310*4882a593Smuzhiyun goto out;
2311*4882a593Smuzhiyun }
2312*4882a593Smuzhiyun
2313*4882a593Smuzhiyun ret = ocfs2_read_refcount_block(&tree->rf_ci, tree->rf_blkno,
2314*4882a593Smuzhiyun &ref_root_bh);
2315*4882a593Smuzhiyun if (ret) {
2316*4882a593Smuzhiyun mlog_errno(ret);
2317*4882a593Smuzhiyun goto out;
2318*4882a593Smuzhiyun }
2319*4882a593Smuzhiyun
2320*4882a593Smuzhiyun ret = __ocfs2_decrease_refcount(handle, &tree->rf_ci, ref_root_bh,
2321*4882a593Smuzhiyun cpos, len, meta_ac, dealloc, delete);
2322*4882a593Smuzhiyun if (ret)
2323*4882a593Smuzhiyun mlog_errno(ret);
2324*4882a593Smuzhiyun out:
2325*4882a593Smuzhiyun brelse(ref_root_bh);
2326*4882a593Smuzhiyun return ret;
2327*4882a593Smuzhiyun }
2328*4882a593Smuzhiyun
2329*4882a593Smuzhiyun /*
2330*4882a593Smuzhiyun * Mark the already-existing extent at cpos as refcounted for len clusters.
2331*4882a593Smuzhiyun * This adds the refcount extent flag.
2332*4882a593Smuzhiyun *
2333*4882a593Smuzhiyun * If the existing extent is larger than the request, initiate a
2334*4882a593Smuzhiyun * split. An attempt will be made at merging with adjacent extents.
2335*4882a593Smuzhiyun *
2336*4882a593Smuzhiyun * The caller is responsible for passing down meta_ac if we'll need it.
2337*4882a593Smuzhiyun */
ocfs2_mark_extent_refcounted(struct inode * inode,struct ocfs2_extent_tree * et,handle_t * handle,u32 cpos,u32 len,u32 phys,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)2338*4882a593Smuzhiyun static int ocfs2_mark_extent_refcounted(struct inode *inode,
2339*4882a593Smuzhiyun struct ocfs2_extent_tree *et,
2340*4882a593Smuzhiyun handle_t *handle, u32 cpos,
2341*4882a593Smuzhiyun u32 len, u32 phys,
2342*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac,
2343*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc)
2344*4882a593Smuzhiyun {
2345*4882a593Smuzhiyun int ret;
2346*4882a593Smuzhiyun
2347*4882a593Smuzhiyun trace_ocfs2_mark_extent_refcounted(OCFS2_I(inode)->ip_blkno,
2348*4882a593Smuzhiyun cpos, len, phys);
2349*4882a593Smuzhiyun
2350*4882a593Smuzhiyun if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
2351*4882a593Smuzhiyun ret = ocfs2_error(inode->i_sb, "Inode %lu want to use refcount tree, but the feature bit is not set in the super block\n",
2352*4882a593Smuzhiyun inode->i_ino);
2353*4882a593Smuzhiyun goto out;
2354*4882a593Smuzhiyun }
2355*4882a593Smuzhiyun
2356*4882a593Smuzhiyun ret = ocfs2_change_extent_flag(handle, et, cpos,
2357*4882a593Smuzhiyun len, phys, meta_ac, dealloc,
2358*4882a593Smuzhiyun OCFS2_EXT_REFCOUNTED, 0);
2359*4882a593Smuzhiyun if (ret)
2360*4882a593Smuzhiyun mlog_errno(ret);
2361*4882a593Smuzhiyun
2362*4882a593Smuzhiyun out:
2363*4882a593Smuzhiyun return ret;
2364*4882a593Smuzhiyun }
2365*4882a593Smuzhiyun
2366*4882a593Smuzhiyun /*
2367*4882a593Smuzhiyun * Given some contiguous physical clusters, calculate what we need
2368*4882a593Smuzhiyun * for modifying their refcount.
2369*4882a593Smuzhiyun */
ocfs2_calc_refcount_meta_credits(struct super_block * sb,struct ocfs2_caching_info * ci,struct buffer_head * ref_root_bh,u64 start_cpos,u32 clusters,int * meta_add,int * credits)2370*4882a593Smuzhiyun static int ocfs2_calc_refcount_meta_credits(struct super_block *sb,
2371*4882a593Smuzhiyun struct ocfs2_caching_info *ci,
2372*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
2373*4882a593Smuzhiyun u64 start_cpos,
2374*4882a593Smuzhiyun u32 clusters,
2375*4882a593Smuzhiyun int *meta_add,
2376*4882a593Smuzhiyun int *credits)
2377*4882a593Smuzhiyun {
2378*4882a593Smuzhiyun int ret = 0, index, ref_blocks = 0, recs_add = 0;
2379*4882a593Smuzhiyun u64 cpos = start_cpos;
2380*4882a593Smuzhiyun struct ocfs2_refcount_block *rb;
2381*4882a593Smuzhiyun struct ocfs2_refcount_rec rec;
2382*4882a593Smuzhiyun struct buffer_head *ref_leaf_bh = NULL, *prev_bh = NULL;
2383*4882a593Smuzhiyun u32 len;
2384*4882a593Smuzhiyun
2385*4882a593Smuzhiyun while (clusters) {
2386*4882a593Smuzhiyun ret = ocfs2_get_refcount_rec(ci, ref_root_bh,
2387*4882a593Smuzhiyun cpos, clusters, &rec,
2388*4882a593Smuzhiyun &index, &ref_leaf_bh);
2389*4882a593Smuzhiyun if (ret) {
2390*4882a593Smuzhiyun mlog_errno(ret);
2391*4882a593Smuzhiyun goto out;
2392*4882a593Smuzhiyun }
2393*4882a593Smuzhiyun
2394*4882a593Smuzhiyun if (ref_leaf_bh != prev_bh) {
2395*4882a593Smuzhiyun /*
2396*4882a593Smuzhiyun * Now we encounter a new leaf block, so calculate
2397*4882a593Smuzhiyun * whether we need to extend the old leaf.
2398*4882a593Smuzhiyun */
2399*4882a593Smuzhiyun if (prev_bh) {
2400*4882a593Smuzhiyun rb = (struct ocfs2_refcount_block *)
2401*4882a593Smuzhiyun prev_bh->b_data;
2402*4882a593Smuzhiyun
2403*4882a593Smuzhiyun if (le16_to_cpu(rb->rf_records.rl_used) +
2404*4882a593Smuzhiyun recs_add >
2405*4882a593Smuzhiyun le16_to_cpu(rb->rf_records.rl_count))
2406*4882a593Smuzhiyun ref_blocks++;
2407*4882a593Smuzhiyun }
2408*4882a593Smuzhiyun
2409*4882a593Smuzhiyun recs_add = 0;
2410*4882a593Smuzhiyun *credits += 1;
2411*4882a593Smuzhiyun brelse(prev_bh);
2412*4882a593Smuzhiyun prev_bh = ref_leaf_bh;
2413*4882a593Smuzhiyun get_bh(prev_bh);
2414*4882a593Smuzhiyun }
2415*4882a593Smuzhiyun
2416*4882a593Smuzhiyun trace_ocfs2_calc_refcount_meta_credits_iterate(
2417*4882a593Smuzhiyun recs_add, (unsigned long long)cpos, clusters,
2418*4882a593Smuzhiyun (unsigned long long)le64_to_cpu(rec.r_cpos),
2419*4882a593Smuzhiyun le32_to_cpu(rec.r_clusters),
2420*4882a593Smuzhiyun le32_to_cpu(rec.r_refcount), index);
2421*4882a593Smuzhiyun
2422*4882a593Smuzhiyun len = min((u64)cpos + clusters, le64_to_cpu(rec.r_cpos) +
2423*4882a593Smuzhiyun le32_to_cpu(rec.r_clusters)) - cpos;
2424*4882a593Smuzhiyun /*
2425*4882a593Smuzhiyun * We record all the records which will be inserted to the
2426*4882a593Smuzhiyun * same refcount block, so that we can tell exactly whether
2427*4882a593Smuzhiyun * we need a new refcount block or not.
2428*4882a593Smuzhiyun *
2429*4882a593Smuzhiyun * If we will insert a new one, this is easy and only happens
2430*4882a593Smuzhiyun * during adding refcounted flag to the extent, so we don't
2431*4882a593Smuzhiyun * have a chance of spliting. We just need one record.
2432*4882a593Smuzhiyun *
2433*4882a593Smuzhiyun * If the refcount rec already exists, that would be a little
2434*4882a593Smuzhiyun * complicated. we may have to:
2435*4882a593Smuzhiyun * 1) split at the beginning if the start pos isn't aligned.
2436*4882a593Smuzhiyun * we need 1 more record in this case.
2437*4882a593Smuzhiyun * 2) split int the end if the end pos isn't aligned.
2438*4882a593Smuzhiyun * we need 1 more record in this case.
2439*4882a593Smuzhiyun * 3) split in the middle because of file system fragmentation.
2440*4882a593Smuzhiyun * we need 2 more records in this case(we can't detect this
2441*4882a593Smuzhiyun * beforehand, so always think of the worst case).
2442*4882a593Smuzhiyun */
2443*4882a593Smuzhiyun if (rec.r_refcount) {
2444*4882a593Smuzhiyun recs_add += 2;
2445*4882a593Smuzhiyun /* Check whether we need a split at the beginning. */
2446*4882a593Smuzhiyun if (cpos == start_cpos &&
2447*4882a593Smuzhiyun cpos != le64_to_cpu(rec.r_cpos))
2448*4882a593Smuzhiyun recs_add++;
2449*4882a593Smuzhiyun
2450*4882a593Smuzhiyun /* Check whether we need a split in the end. */
2451*4882a593Smuzhiyun if (cpos + clusters < le64_to_cpu(rec.r_cpos) +
2452*4882a593Smuzhiyun le32_to_cpu(rec.r_clusters))
2453*4882a593Smuzhiyun recs_add++;
2454*4882a593Smuzhiyun } else
2455*4882a593Smuzhiyun recs_add++;
2456*4882a593Smuzhiyun
2457*4882a593Smuzhiyun brelse(ref_leaf_bh);
2458*4882a593Smuzhiyun ref_leaf_bh = NULL;
2459*4882a593Smuzhiyun clusters -= len;
2460*4882a593Smuzhiyun cpos += len;
2461*4882a593Smuzhiyun }
2462*4882a593Smuzhiyun
2463*4882a593Smuzhiyun if (prev_bh) {
2464*4882a593Smuzhiyun rb = (struct ocfs2_refcount_block *)prev_bh->b_data;
2465*4882a593Smuzhiyun
2466*4882a593Smuzhiyun if (le16_to_cpu(rb->rf_records.rl_used) + recs_add >
2467*4882a593Smuzhiyun le16_to_cpu(rb->rf_records.rl_count))
2468*4882a593Smuzhiyun ref_blocks++;
2469*4882a593Smuzhiyun
2470*4882a593Smuzhiyun *credits += 1;
2471*4882a593Smuzhiyun }
2472*4882a593Smuzhiyun
2473*4882a593Smuzhiyun if (!ref_blocks)
2474*4882a593Smuzhiyun goto out;
2475*4882a593Smuzhiyun
2476*4882a593Smuzhiyun *meta_add += ref_blocks;
2477*4882a593Smuzhiyun *credits += ref_blocks;
2478*4882a593Smuzhiyun
2479*4882a593Smuzhiyun /*
2480*4882a593Smuzhiyun * So we may need ref_blocks to insert into the tree.
2481*4882a593Smuzhiyun * That also means we need to change the b-tree and add that number
2482*4882a593Smuzhiyun * of records since we never merge them.
2483*4882a593Smuzhiyun * We need one more block for expansion since the new created leaf
2484*4882a593Smuzhiyun * block is also full and needs split.
2485*4882a593Smuzhiyun */
2486*4882a593Smuzhiyun rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
2487*4882a593Smuzhiyun if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL) {
2488*4882a593Smuzhiyun struct ocfs2_extent_tree et;
2489*4882a593Smuzhiyun
2490*4882a593Smuzhiyun ocfs2_init_refcount_extent_tree(&et, ci, ref_root_bh);
2491*4882a593Smuzhiyun *meta_add += ocfs2_extend_meta_needed(et.et_root_el);
2492*4882a593Smuzhiyun *credits += ocfs2_calc_extend_credits(sb,
2493*4882a593Smuzhiyun et.et_root_el);
2494*4882a593Smuzhiyun } else {
2495*4882a593Smuzhiyun *credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
2496*4882a593Smuzhiyun *meta_add += 1;
2497*4882a593Smuzhiyun }
2498*4882a593Smuzhiyun
2499*4882a593Smuzhiyun out:
2500*4882a593Smuzhiyun
2501*4882a593Smuzhiyun trace_ocfs2_calc_refcount_meta_credits(
2502*4882a593Smuzhiyun (unsigned long long)start_cpos, clusters,
2503*4882a593Smuzhiyun *meta_add, *credits);
2504*4882a593Smuzhiyun brelse(ref_leaf_bh);
2505*4882a593Smuzhiyun brelse(prev_bh);
2506*4882a593Smuzhiyun return ret;
2507*4882a593Smuzhiyun }
2508*4882a593Smuzhiyun
2509*4882a593Smuzhiyun /*
2510*4882a593Smuzhiyun * For refcount tree, we will decrease some contiguous clusters
2511*4882a593Smuzhiyun * refcount count, so just go through it to see how many blocks
2512*4882a593Smuzhiyun * we gonna touch and whether we need to create new blocks.
2513*4882a593Smuzhiyun *
2514*4882a593Smuzhiyun * Normally the refcount blocks store these refcount should be
2515*4882a593Smuzhiyun * contiguous also, so that we can get the number easily.
2516*4882a593Smuzhiyun * We will at most add split 2 refcount records and 2 more
2517*4882a593Smuzhiyun * refcount blocks, so just check it in a rough way.
2518*4882a593Smuzhiyun *
2519*4882a593Smuzhiyun * Caller must hold refcount tree lock.
2520*4882a593Smuzhiyun */
ocfs2_prepare_refcount_change_for_del(struct inode * inode,u64 refcount_loc,u64 phys_blkno,u32 clusters,int * credits,int * ref_blocks)2521*4882a593Smuzhiyun int ocfs2_prepare_refcount_change_for_del(struct inode *inode,
2522*4882a593Smuzhiyun u64 refcount_loc,
2523*4882a593Smuzhiyun u64 phys_blkno,
2524*4882a593Smuzhiyun u32 clusters,
2525*4882a593Smuzhiyun int *credits,
2526*4882a593Smuzhiyun int *ref_blocks)
2527*4882a593Smuzhiyun {
2528*4882a593Smuzhiyun int ret;
2529*4882a593Smuzhiyun struct buffer_head *ref_root_bh = NULL;
2530*4882a593Smuzhiyun struct ocfs2_refcount_tree *tree;
2531*4882a593Smuzhiyun u64 start_cpos = ocfs2_blocks_to_clusters(inode->i_sb, phys_blkno);
2532*4882a593Smuzhiyun
2533*4882a593Smuzhiyun if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb))) {
2534*4882a593Smuzhiyun ret = ocfs2_error(inode->i_sb, "Inode %lu want to use refcount tree, but the feature bit is not set in the super block\n",
2535*4882a593Smuzhiyun inode->i_ino);
2536*4882a593Smuzhiyun goto out;
2537*4882a593Smuzhiyun }
2538*4882a593Smuzhiyun
2539*4882a593Smuzhiyun BUG_ON(!ocfs2_is_refcount_inode(inode));
2540*4882a593Smuzhiyun
2541*4882a593Smuzhiyun ret = ocfs2_get_refcount_tree(OCFS2_SB(inode->i_sb),
2542*4882a593Smuzhiyun refcount_loc, &tree);
2543*4882a593Smuzhiyun if (ret) {
2544*4882a593Smuzhiyun mlog_errno(ret);
2545*4882a593Smuzhiyun goto out;
2546*4882a593Smuzhiyun }
2547*4882a593Smuzhiyun
2548*4882a593Smuzhiyun ret = ocfs2_read_refcount_block(&tree->rf_ci, refcount_loc,
2549*4882a593Smuzhiyun &ref_root_bh);
2550*4882a593Smuzhiyun if (ret) {
2551*4882a593Smuzhiyun mlog_errno(ret);
2552*4882a593Smuzhiyun goto out;
2553*4882a593Smuzhiyun }
2554*4882a593Smuzhiyun
2555*4882a593Smuzhiyun ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
2556*4882a593Smuzhiyun &tree->rf_ci,
2557*4882a593Smuzhiyun ref_root_bh,
2558*4882a593Smuzhiyun start_cpos, clusters,
2559*4882a593Smuzhiyun ref_blocks, credits);
2560*4882a593Smuzhiyun if (ret) {
2561*4882a593Smuzhiyun mlog_errno(ret);
2562*4882a593Smuzhiyun goto out;
2563*4882a593Smuzhiyun }
2564*4882a593Smuzhiyun
2565*4882a593Smuzhiyun trace_ocfs2_prepare_refcount_change_for_del(*ref_blocks, *credits);
2566*4882a593Smuzhiyun
2567*4882a593Smuzhiyun out:
2568*4882a593Smuzhiyun brelse(ref_root_bh);
2569*4882a593Smuzhiyun return ret;
2570*4882a593Smuzhiyun }
2571*4882a593Smuzhiyun
2572*4882a593Smuzhiyun #define MAX_CONTIG_BYTES 1048576
2573*4882a593Smuzhiyun
ocfs2_cow_contig_clusters(struct super_block * sb)2574*4882a593Smuzhiyun static inline unsigned int ocfs2_cow_contig_clusters(struct super_block *sb)
2575*4882a593Smuzhiyun {
2576*4882a593Smuzhiyun return ocfs2_clusters_for_bytes(sb, MAX_CONTIG_BYTES);
2577*4882a593Smuzhiyun }
2578*4882a593Smuzhiyun
ocfs2_cow_contig_mask(struct super_block * sb)2579*4882a593Smuzhiyun static inline unsigned int ocfs2_cow_contig_mask(struct super_block *sb)
2580*4882a593Smuzhiyun {
2581*4882a593Smuzhiyun return ~(ocfs2_cow_contig_clusters(sb) - 1);
2582*4882a593Smuzhiyun }
2583*4882a593Smuzhiyun
2584*4882a593Smuzhiyun /*
2585*4882a593Smuzhiyun * Given an extent that starts at 'start' and an I/O that starts at 'cpos',
2586*4882a593Smuzhiyun * find an offset (start + (n * contig_clusters)) that is closest to cpos
2587*4882a593Smuzhiyun * while still being less than or equal to it.
2588*4882a593Smuzhiyun *
2589*4882a593Smuzhiyun * The goal is to break the extent at a multiple of contig_clusters.
2590*4882a593Smuzhiyun */
ocfs2_cow_align_start(struct super_block * sb,unsigned int start,unsigned int cpos)2591*4882a593Smuzhiyun static inline unsigned int ocfs2_cow_align_start(struct super_block *sb,
2592*4882a593Smuzhiyun unsigned int start,
2593*4882a593Smuzhiyun unsigned int cpos)
2594*4882a593Smuzhiyun {
2595*4882a593Smuzhiyun BUG_ON(start > cpos);
2596*4882a593Smuzhiyun
2597*4882a593Smuzhiyun return start + ((cpos - start) & ocfs2_cow_contig_mask(sb));
2598*4882a593Smuzhiyun }
2599*4882a593Smuzhiyun
2600*4882a593Smuzhiyun /*
2601*4882a593Smuzhiyun * Given a cluster count of len, pad it out so that it is a multiple
2602*4882a593Smuzhiyun * of contig_clusters.
2603*4882a593Smuzhiyun */
ocfs2_cow_align_length(struct super_block * sb,unsigned int len)2604*4882a593Smuzhiyun static inline unsigned int ocfs2_cow_align_length(struct super_block *sb,
2605*4882a593Smuzhiyun unsigned int len)
2606*4882a593Smuzhiyun {
2607*4882a593Smuzhiyun unsigned int padded =
2608*4882a593Smuzhiyun (len + (ocfs2_cow_contig_clusters(sb) - 1)) &
2609*4882a593Smuzhiyun ocfs2_cow_contig_mask(sb);
2610*4882a593Smuzhiyun
2611*4882a593Smuzhiyun /* Did we wrap? */
2612*4882a593Smuzhiyun if (padded < len)
2613*4882a593Smuzhiyun padded = UINT_MAX;
2614*4882a593Smuzhiyun
2615*4882a593Smuzhiyun return padded;
2616*4882a593Smuzhiyun }
2617*4882a593Smuzhiyun
2618*4882a593Smuzhiyun /*
2619*4882a593Smuzhiyun * Calculate out the start and number of virtual clusters we need to to CoW.
2620*4882a593Smuzhiyun *
2621*4882a593Smuzhiyun * cpos is vitual start cluster position we want to do CoW in a
2622*4882a593Smuzhiyun * file and write_len is the cluster length.
2623*4882a593Smuzhiyun * max_cpos is the place where we want to stop CoW intentionally.
2624*4882a593Smuzhiyun *
2625*4882a593Smuzhiyun * Normal we will start CoW from the beginning of extent record cotaining cpos.
2626*4882a593Smuzhiyun * We try to break up extents on boundaries of MAX_CONTIG_BYTES so that we
2627*4882a593Smuzhiyun * get good I/O from the resulting extent tree.
2628*4882a593Smuzhiyun */
ocfs2_refcount_cal_cow_clusters(struct inode * inode,struct ocfs2_extent_list * el,u32 cpos,u32 write_len,u32 max_cpos,u32 * cow_start,u32 * cow_len)2629*4882a593Smuzhiyun static int ocfs2_refcount_cal_cow_clusters(struct inode *inode,
2630*4882a593Smuzhiyun struct ocfs2_extent_list *el,
2631*4882a593Smuzhiyun u32 cpos,
2632*4882a593Smuzhiyun u32 write_len,
2633*4882a593Smuzhiyun u32 max_cpos,
2634*4882a593Smuzhiyun u32 *cow_start,
2635*4882a593Smuzhiyun u32 *cow_len)
2636*4882a593Smuzhiyun {
2637*4882a593Smuzhiyun int ret = 0;
2638*4882a593Smuzhiyun int tree_height = le16_to_cpu(el->l_tree_depth), i;
2639*4882a593Smuzhiyun struct buffer_head *eb_bh = NULL;
2640*4882a593Smuzhiyun struct ocfs2_extent_block *eb = NULL;
2641*4882a593Smuzhiyun struct ocfs2_extent_rec *rec;
2642*4882a593Smuzhiyun unsigned int want_clusters, rec_end = 0;
2643*4882a593Smuzhiyun int contig_clusters = ocfs2_cow_contig_clusters(inode->i_sb);
2644*4882a593Smuzhiyun int leaf_clusters;
2645*4882a593Smuzhiyun
2646*4882a593Smuzhiyun BUG_ON(cpos + write_len > max_cpos);
2647*4882a593Smuzhiyun
2648*4882a593Smuzhiyun if (tree_height > 0) {
2649*4882a593Smuzhiyun ret = ocfs2_find_leaf(INODE_CACHE(inode), el, cpos, &eb_bh);
2650*4882a593Smuzhiyun if (ret) {
2651*4882a593Smuzhiyun mlog_errno(ret);
2652*4882a593Smuzhiyun goto out;
2653*4882a593Smuzhiyun }
2654*4882a593Smuzhiyun
2655*4882a593Smuzhiyun eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2656*4882a593Smuzhiyun el = &eb->h_list;
2657*4882a593Smuzhiyun
2658*4882a593Smuzhiyun if (el->l_tree_depth) {
2659*4882a593Smuzhiyun ret = ocfs2_error(inode->i_sb,
2660*4882a593Smuzhiyun "Inode %lu has non zero tree depth in leaf block %llu\n",
2661*4882a593Smuzhiyun inode->i_ino,
2662*4882a593Smuzhiyun (unsigned long long)eb_bh->b_blocknr);
2663*4882a593Smuzhiyun goto out;
2664*4882a593Smuzhiyun }
2665*4882a593Smuzhiyun }
2666*4882a593Smuzhiyun
2667*4882a593Smuzhiyun *cow_len = 0;
2668*4882a593Smuzhiyun for (i = 0; i < le16_to_cpu(el->l_next_free_rec); i++) {
2669*4882a593Smuzhiyun rec = &el->l_recs[i];
2670*4882a593Smuzhiyun
2671*4882a593Smuzhiyun if (ocfs2_is_empty_extent(rec)) {
2672*4882a593Smuzhiyun mlog_bug_on_msg(i != 0, "Inode %lu has empty record in "
2673*4882a593Smuzhiyun "index %d\n", inode->i_ino, i);
2674*4882a593Smuzhiyun continue;
2675*4882a593Smuzhiyun }
2676*4882a593Smuzhiyun
2677*4882a593Smuzhiyun if (le32_to_cpu(rec->e_cpos) +
2678*4882a593Smuzhiyun le16_to_cpu(rec->e_leaf_clusters) <= cpos)
2679*4882a593Smuzhiyun continue;
2680*4882a593Smuzhiyun
2681*4882a593Smuzhiyun if (*cow_len == 0) {
2682*4882a593Smuzhiyun /*
2683*4882a593Smuzhiyun * We should find a refcounted record in the
2684*4882a593Smuzhiyun * first pass.
2685*4882a593Smuzhiyun */
2686*4882a593Smuzhiyun BUG_ON(!(rec->e_flags & OCFS2_EXT_REFCOUNTED));
2687*4882a593Smuzhiyun *cow_start = le32_to_cpu(rec->e_cpos);
2688*4882a593Smuzhiyun }
2689*4882a593Smuzhiyun
2690*4882a593Smuzhiyun /*
2691*4882a593Smuzhiyun * If we encounter a hole, a non-refcounted record or
2692*4882a593Smuzhiyun * pass the max_cpos, stop the search.
2693*4882a593Smuzhiyun */
2694*4882a593Smuzhiyun if ((!(rec->e_flags & OCFS2_EXT_REFCOUNTED)) ||
2695*4882a593Smuzhiyun (*cow_len && rec_end != le32_to_cpu(rec->e_cpos)) ||
2696*4882a593Smuzhiyun (max_cpos <= le32_to_cpu(rec->e_cpos)))
2697*4882a593Smuzhiyun break;
2698*4882a593Smuzhiyun
2699*4882a593Smuzhiyun leaf_clusters = le16_to_cpu(rec->e_leaf_clusters);
2700*4882a593Smuzhiyun rec_end = le32_to_cpu(rec->e_cpos) + leaf_clusters;
2701*4882a593Smuzhiyun if (rec_end > max_cpos) {
2702*4882a593Smuzhiyun rec_end = max_cpos;
2703*4882a593Smuzhiyun leaf_clusters = rec_end - le32_to_cpu(rec->e_cpos);
2704*4882a593Smuzhiyun }
2705*4882a593Smuzhiyun
2706*4882a593Smuzhiyun /*
2707*4882a593Smuzhiyun * How many clusters do we actually need from
2708*4882a593Smuzhiyun * this extent? First we see how many we actually
2709*4882a593Smuzhiyun * need to complete the write. If that's smaller
2710*4882a593Smuzhiyun * than contig_clusters, we try for contig_clusters.
2711*4882a593Smuzhiyun */
2712*4882a593Smuzhiyun if (!*cow_len)
2713*4882a593Smuzhiyun want_clusters = write_len;
2714*4882a593Smuzhiyun else
2715*4882a593Smuzhiyun want_clusters = (cpos + write_len) -
2716*4882a593Smuzhiyun (*cow_start + *cow_len);
2717*4882a593Smuzhiyun if (want_clusters < contig_clusters)
2718*4882a593Smuzhiyun want_clusters = contig_clusters;
2719*4882a593Smuzhiyun
2720*4882a593Smuzhiyun /*
2721*4882a593Smuzhiyun * If the write does not cover the whole extent, we
2722*4882a593Smuzhiyun * need to calculate how we're going to split the extent.
2723*4882a593Smuzhiyun * We try to do it on contig_clusters boundaries.
2724*4882a593Smuzhiyun *
2725*4882a593Smuzhiyun * Any extent smaller than contig_clusters will be
2726*4882a593Smuzhiyun * CoWed in its entirety.
2727*4882a593Smuzhiyun */
2728*4882a593Smuzhiyun if (leaf_clusters <= contig_clusters)
2729*4882a593Smuzhiyun *cow_len += leaf_clusters;
2730*4882a593Smuzhiyun else if (*cow_len || (*cow_start == cpos)) {
2731*4882a593Smuzhiyun /*
2732*4882a593Smuzhiyun * This extent needs to be CoW'd from its
2733*4882a593Smuzhiyun * beginning, so all we have to do is compute
2734*4882a593Smuzhiyun * how many clusters to grab. We align
2735*4882a593Smuzhiyun * want_clusters to the edge of contig_clusters
2736*4882a593Smuzhiyun * to get better I/O.
2737*4882a593Smuzhiyun */
2738*4882a593Smuzhiyun want_clusters = ocfs2_cow_align_length(inode->i_sb,
2739*4882a593Smuzhiyun want_clusters);
2740*4882a593Smuzhiyun
2741*4882a593Smuzhiyun if (leaf_clusters < want_clusters)
2742*4882a593Smuzhiyun *cow_len += leaf_clusters;
2743*4882a593Smuzhiyun else
2744*4882a593Smuzhiyun *cow_len += want_clusters;
2745*4882a593Smuzhiyun } else if ((*cow_start + contig_clusters) >=
2746*4882a593Smuzhiyun (cpos + write_len)) {
2747*4882a593Smuzhiyun /*
2748*4882a593Smuzhiyun * Breaking off contig_clusters at the front
2749*4882a593Smuzhiyun * of the extent will cover our write. That's
2750*4882a593Smuzhiyun * easy.
2751*4882a593Smuzhiyun */
2752*4882a593Smuzhiyun *cow_len = contig_clusters;
2753*4882a593Smuzhiyun } else if ((rec_end - cpos) <= contig_clusters) {
2754*4882a593Smuzhiyun /*
2755*4882a593Smuzhiyun * Breaking off contig_clusters at the tail of
2756*4882a593Smuzhiyun * this extent will cover cpos.
2757*4882a593Smuzhiyun */
2758*4882a593Smuzhiyun *cow_start = rec_end - contig_clusters;
2759*4882a593Smuzhiyun *cow_len = contig_clusters;
2760*4882a593Smuzhiyun } else if ((rec_end - cpos) <= want_clusters) {
2761*4882a593Smuzhiyun /*
2762*4882a593Smuzhiyun * While we can't fit the entire write in this
2763*4882a593Smuzhiyun * extent, we know that the write goes from cpos
2764*4882a593Smuzhiyun * to the end of the extent. Break that off.
2765*4882a593Smuzhiyun * We try to break it at some multiple of
2766*4882a593Smuzhiyun * contig_clusters from the front of the extent.
2767*4882a593Smuzhiyun * Failing that (ie, cpos is within
2768*4882a593Smuzhiyun * contig_clusters of the front), we'll CoW the
2769*4882a593Smuzhiyun * entire extent.
2770*4882a593Smuzhiyun */
2771*4882a593Smuzhiyun *cow_start = ocfs2_cow_align_start(inode->i_sb,
2772*4882a593Smuzhiyun *cow_start, cpos);
2773*4882a593Smuzhiyun *cow_len = rec_end - *cow_start;
2774*4882a593Smuzhiyun } else {
2775*4882a593Smuzhiyun /*
2776*4882a593Smuzhiyun * Ok, the entire write lives in the middle of
2777*4882a593Smuzhiyun * this extent. Let's try to slice the extent up
2778*4882a593Smuzhiyun * nicely. Optimally, our CoW region starts at
2779*4882a593Smuzhiyun * m*contig_clusters from the beginning of the
2780*4882a593Smuzhiyun * extent and goes for n*contig_clusters,
2781*4882a593Smuzhiyun * covering the entire write.
2782*4882a593Smuzhiyun */
2783*4882a593Smuzhiyun *cow_start = ocfs2_cow_align_start(inode->i_sb,
2784*4882a593Smuzhiyun *cow_start, cpos);
2785*4882a593Smuzhiyun
2786*4882a593Smuzhiyun want_clusters = (cpos + write_len) - *cow_start;
2787*4882a593Smuzhiyun want_clusters = ocfs2_cow_align_length(inode->i_sb,
2788*4882a593Smuzhiyun want_clusters);
2789*4882a593Smuzhiyun if (*cow_start + want_clusters <= rec_end)
2790*4882a593Smuzhiyun *cow_len = want_clusters;
2791*4882a593Smuzhiyun else
2792*4882a593Smuzhiyun *cow_len = rec_end - *cow_start;
2793*4882a593Smuzhiyun }
2794*4882a593Smuzhiyun
2795*4882a593Smuzhiyun /* Have we covered our entire write yet? */
2796*4882a593Smuzhiyun if ((*cow_start + *cow_len) >= (cpos + write_len))
2797*4882a593Smuzhiyun break;
2798*4882a593Smuzhiyun
2799*4882a593Smuzhiyun /*
2800*4882a593Smuzhiyun * If we reach the end of the extent block and don't get enough
2801*4882a593Smuzhiyun * clusters, continue with the next extent block if possible.
2802*4882a593Smuzhiyun */
2803*4882a593Smuzhiyun if (i + 1 == le16_to_cpu(el->l_next_free_rec) &&
2804*4882a593Smuzhiyun eb && eb->h_next_leaf_blk) {
2805*4882a593Smuzhiyun brelse(eb_bh);
2806*4882a593Smuzhiyun eb_bh = NULL;
2807*4882a593Smuzhiyun
2808*4882a593Smuzhiyun ret = ocfs2_read_extent_block(INODE_CACHE(inode),
2809*4882a593Smuzhiyun le64_to_cpu(eb->h_next_leaf_blk),
2810*4882a593Smuzhiyun &eb_bh);
2811*4882a593Smuzhiyun if (ret) {
2812*4882a593Smuzhiyun mlog_errno(ret);
2813*4882a593Smuzhiyun goto out;
2814*4882a593Smuzhiyun }
2815*4882a593Smuzhiyun
2816*4882a593Smuzhiyun eb = (struct ocfs2_extent_block *) eb_bh->b_data;
2817*4882a593Smuzhiyun el = &eb->h_list;
2818*4882a593Smuzhiyun i = -1;
2819*4882a593Smuzhiyun }
2820*4882a593Smuzhiyun }
2821*4882a593Smuzhiyun
2822*4882a593Smuzhiyun out:
2823*4882a593Smuzhiyun brelse(eb_bh);
2824*4882a593Smuzhiyun return ret;
2825*4882a593Smuzhiyun }
2826*4882a593Smuzhiyun
2827*4882a593Smuzhiyun /*
2828*4882a593Smuzhiyun * Prepare meta_ac, data_ac and calculate credits when we want to add some
2829*4882a593Smuzhiyun * num_clusters in data_tree "et" and change the refcount for the old
2830*4882a593Smuzhiyun * clusters(starting form p_cluster) in the refcount tree.
2831*4882a593Smuzhiyun *
2832*4882a593Smuzhiyun * Note:
2833*4882a593Smuzhiyun * 1. since we may split the old tree, so we at most will need num_clusters + 2
2834*4882a593Smuzhiyun * more new leaf records.
2835*4882a593Smuzhiyun * 2. In some case, we may not need to reserve new clusters(e.g, reflink), so
2836*4882a593Smuzhiyun * just give data_ac = NULL.
2837*4882a593Smuzhiyun */
ocfs2_lock_refcount_allocators(struct super_block * sb,u32 p_cluster,u32 num_clusters,struct ocfs2_extent_tree * et,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_alloc_context ** meta_ac,struct ocfs2_alloc_context ** data_ac,int * credits)2838*4882a593Smuzhiyun static int ocfs2_lock_refcount_allocators(struct super_block *sb,
2839*4882a593Smuzhiyun u32 p_cluster, u32 num_clusters,
2840*4882a593Smuzhiyun struct ocfs2_extent_tree *et,
2841*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci,
2842*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
2843*4882a593Smuzhiyun struct ocfs2_alloc_context **meta_ac,
2844*4882a593Smuzhiyun struct ocfs2_alloc_context **data_ac,
2845*4882a593Smuzhiyun int *credits)
2846*4882a593Smuzhiyun {
2847*4882a593Smuzhiyun int ret = 0, meta_add = 0;
2848*4882a593Smuzhiyun int num_free_extents = ocfs2_num_free_extents(et);
2849*4882a593Smuzhiyun
2850*4882a593Smuzhiyun if (num_free_extents < 0) {
2851*4882a593Smuzhiyun ret = num_free_extents;
2852*4882a593Smuzhiyun mlog_errno(ret);
2853*4882a593Smuzhiyun goto out;
2854*4882a593Smuzhiyun }
2855*4882a593Smuzhiyun
2856*4882a593Smuzhiyun if (num_free_extents < num_clusters + 2)
2857*4882a593Smuzhiyun meta_add =
2858*4882a593Smuzhiyun ocfs2_extend_meta_needed(et->et_root_el);
2859*4882a593Smuzhiyun
2860*4882a593Smuzhiyun *credits += ocfs2_calc_extend_credits(sb, et->et_root_el);
2861*4882a593Smuzhiyun
2862*4882a593Smuzhiyun ret = ocfs2_calc_refcount_meta_credits(sb, ref_ci, ref_root_bh,
2863*4882a593Smuzhiyun p_cluster, num_clusters,
2864*4882a593Smuzhiyun &meta_add, credits);
2865*4882a593Smuzhiyun if (ret) {
2866*4882a593Smuzhiyun mlog_errno(ret);
2867*4882a593Smuzhiyun goto out;
2868*4882a593Smuzhiyun }
2869*4882a593Smuzhiyun
2870*4882a593Smuzhiyun trace_ocfs2_lock_refcount_allocators(meta_add, *credits);
2871*4882a593Smuzhiyun ret = ocfs2_reserve_new_metadata_blocks(OCFS2_SB(sb), meta_add,
2872*4882a593Smuzhiyun meta_ac);
2873*4882a593Smuzhiyun if (ret) {
2874*4882a593Smuzhiyun mlog_errno(ret);
2875*4882a593Smuzhiyun goto out;
2876*4882a593Smuzhiyun }
2877*4882a593Smuzhiyun
2878*4882a593Smuzhiyun if (data_ac) {
2879*4882a593Smuzhiyun ret = ocfs2_reserve_clusters(OCFS2_SB(sb), num_clusters,
2880*4882a593Smuzhiyun data_ac);
2881*4882a593Smuzhiyun if (ret)
2882*4882a593Smuzhiyun mlog_errno(ret);
2883*4882a593Smuzhiyun }
2884*4882a593Smuzhiyun
2885*4882a593Smuzhiyun out:
2886*4882a593Smuzhiyun if (ret) {
2887*4882a593Smuzhiyun if (*meta_ac) {
2888*4882a593Smuzhiyun ocfs2_free_alloc_context(*meta_ac);
2889*4882a593Smuzhiyun *meta_ac = NULL;
2890*4882a593Smuzhiyun }
2891*4882a593Smuzhiyun }
2892*4882a593Smuzhiyun
2893*4882a593Smuzhiyun return ret;
2894*4882a593Smuzhiyun }
2895*4882a593Smuzhiyun
ocfs2_clear_cow_buffer(handle_t * handle,struct buffer_head * bh)2896*4882a593Smuzhiyun static int ocfs2_clear_cow_buffer(handle_t *handle, struct buffer_head *bh)
2897*4882a593Smuzhiyun {
2898*4882a593Smuzhiyun BUG_ON(buffer_dirty(bh));
2899*4882a593Smuzhiyun
2900*4882a593Smuzhiyun clear_buffer_mapped(bh);
2901*4882a593Smuzhiyun
2902*4882a593Smuzhiyun return 0;
2903*4882a593Smuzhiyun }
2904*4882a593Smuzhiyun
ocfs2_duplicate_clusters_by_page(handle_t * handle,struct inode * inode,u32 cpos,u32 old_cluster,u32 new_cluster,u32 new_len)2905*4882a593Smuzhiyun int ocfs2_duplicate_clusters_by_page(handle_t *handle,
2906*4882a593Smuzhiyun struct inode *inode,
2907*4882a593Smuzhiyun u32 cpos, u32 old_cluster,
2908*4882a593Smuzhiyun u32 new_cluster, u32 new_len)
2909*4882a593Smuzhiyun {
2910*4882a593Smuzhiyun int ret = 0, partial;
2911*4882a593Smuzhiyun struct super_block *sb = inode->i_sb;
2912*4882a593Smuzhiyun u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
2913*4882a593Smuzhiyun struct page *page;
2914*4882a593Smuzhiyun pgoff_t page_index;
2915*4882a593Smuzhiyun unsigned int from, to;
2916*4882a593Smuzhiyun loff_t offset, end, map_end;
2917*4882a593Smuzhiyun struct address_space *mapping = inode->i_mapping;
2918*4882a593Smuzhiyun
2919*4882a593Smuzhiyun trace_ocfs2_duplicate_clusters_by_page(cpos, old_cluster,
2920*4882a593Smuzhiyun new_cluster, new_len);
2921*4882a593Smuzhiyun
2922*4882a593Smuzhiyun offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
2923*4882a593Smuzhiyun end = offset + (new_len << OCFS2_SB(sb)->s_clustersize_bits);
2924*4882a593Smuzhiyun /*
2925*4882a593Smuzhiyun * We only duplicate pages until we reach the page contains i_size - 1.
2926*4882a593Smuzhiyun * So trim 'end' to i_size.
2927*4882a593Smuzhiyun */
2928*4882a593Smuzhiyun if (end > i_size_read(inode))
2929*4882a593Smuzhiyun end = i_size_read(inode);
2930*4882a593Smuzhiyun
2931*4882a593Smuzhiyun while (offset < end) {
2932*4882a593Smuzhiyun page_index = offset >> PAGE_SHIFT;
2933*4882a593Smuzhiyun map_end = ((loff_t)page_index + 1) << PAGE_SHIFT;
2934*4882a593Smuzhiyun if (map_end > end)
2935*4882a593Smuzhiyun map_end = end;
2936*4882a593Smuzhiyun
2937*4882a593Smuzhiyun /* from, to is the offset within the page. */
2938*4882a593Smuzhiyun from = offset & (PAGE_SIZE - 1);
2939*4882a593Smuzhiyun to = PAGE_SIZE;
2940*4882a593Smuzhiyun if (map_end & (PAGE_SIZE - 1))
2941*4882a593Smuzhiyun to = map_end & (PAGE_SIZE - 1);
2942*4882a593Smuzhiyun
2943*4882a593Smuzhiyun retry:
2944*4882a593Smuzhiyun page = find_or_create_page(mapping, page_index, GFP_NOFS);
2945*4882a593Smuzhiyun if (!page) {
2946*4882a593Smuzhiyun ret = -ENOMEM;
2947*4882a593Smuzhiyun mlog_errno(ret);
2948*4882a593Smuzhiyun break;
2949*4882a593Smuzhiyun }
2950*4882a593Smuzhiyun
2951*4882a593Smuzhiyun /*
2952*4882a593Smuzhiyun * In case PAGE_SIZE <= CLUSTER_SIZE, we do not expect a dirty
2953*4882a593Smuzhiyun * page, so write it back.
2954*4882a593Smuzhiyun */
2955*4882a593Smuzhiyun if (PAGE_SIZE <= OCFS2_SB(sb)->s_clustersize) {
2956*4882a593Smuzhiyun if (PageDirty(page)) {
2957*4882a593Smuzhiyun /*
2958*4882a593Smuzhiyun * write_on_page will unlock the page on return
2959*4882a593Smuzhiyun */
2960*4882a593Smuzhiyun ret = write_one_page(page);
2961*4882a593Smuzhiyun goto retry;
2962*4882a593Smuzhiyun }
2963*4882a593Smuzhiyun }
2964*4882a593Smuzhiyun
2965*4882a593Smuzhiyun if (!PageUptodate(page)) {
2966*4882a593Smuzhiyun ret = block_read_full_page(page, ocfs2_get_block);
2967*4882a593Smuzhiyun if (ret) {
2968*4882a593Smuzhiyun mlog_errno(ret);
2969*4882a593Smuzhiyun goto unlock;
2970*4882a593Smuzhiyun }
2971*4882a593Smuzhiyun lock_page(page);
2972*4882a593Smuzhiyun }
2973*4882a593Smuzhiyun
2974*4882a593Smuzhiyun if (page_has_buffers(page)) {
2975*4882a593Smuzhiyun ret = walk_page_buffers(handle, page_buffers(page),
2976*4882a593Smuzhiyun from, to, &partial,
2977*4882a593Smuzhiyun ocfs2_clear_cow_buffer);
2978*4882a593Smuzhiyun if (ret) {
2979*4882a593Smuzhiyun mlog_errno(ret);
2980*4882a593Smuzhiyun goto unlock;
2981*4882a593Smuzhiyun }
2982*4882a593Smuzhiyun }
2983*4882a593Smuzhiyun
2984*4882a593Smuzhiyun ocfs2_map_and_dirty_page(inode,
2985*4882a593Smuzhiyun handle, from, to,
2986*4882a593Smuzhiyun page, 0, &new_block);
2987*4882a593Smuzhiyun mark_page_accessed(page);
2988*4882a593Smuzhiyun unlock:
2989*4882a593Smuzhiyun unlock_page(page);
2990*4882a593Smuzhiyun put_page(page);
2991*4882a593Smuzhiyun page = NULL;
2992*4882a593Smuzhiyun offset = map_end;
2993*4882a593Smuzhiyun if (ret)
2994*4882a593Smuzhiyun break;
2995*4882a593Smuzhiyun }
2996*4882a593Smuzhiyun
2997*4882a593Smuzhiyun return ret;
2998*4882a593Smuzhiyun }
2999*4882a593Smuzhiyun
ocfs2_duplicate_clusters_by_jbd(handle_t * handle,struct inode * inode,u32 cpos,u32 old_cluster,u32 new_cluster,u32 new_len)3000*4882a593Smuzhiyun int ocfs2_duplicate_clusters_by_jbd(handle_t *handle,
3001*4882a593Smuzhiyun struct inode *inode,
3002*4882a593Smuzhiyun u32 cpos, u32 old_cluster,
3003*4882a593Smuzhiyun u32 new_cluster, u32 new_len)
3004*4882a593Smuzhiyun {
3005*4882a593Smuzhiyun int ret = 0;
3006*4882a593Smuzhiyun struct super_block *sb = inode->i_sb;
3007*4882a593Smuzhiyun struct ocfs2_caching_info *ci = INODE_CACHE(inode);
3008*4882a593Smuzhiyun int i, blocks = ocfs2_clusters_to_blocks(sb, new_len);
3009*4882a593Smuzhiyun u64 old_block = ocfs2_clusters_to_blocks(sb, old_cluster);
3010*4882a593Smuzhiyun u64 new_block = ocfs2_clusters_to_blocks(sb, new_cluster);
3011*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(sb);
3012*4882a593Smuzhiyun struct buffer_head *old_bh = NULL;
3013*4882a593Smuzhiyun struct buffer_head *new_bh = NULL;
3014*4882a593Smuzhiyun
3015*4882a593Smuzhiyun trace_ocfs2_duplicate_clusters_by_page(cpos, old_cluster,
3016*4882a593Smuzhiyun new_cluster, new_len);
3017*4882a593Smuzhiyun
3018*4882a593Smuzhiyun for (i = 0; i < blocks; i++, old_block++, new_block++) {
3019*4882a593Smuzhiyun new_bh = sb_getblk(osb->sb, new_block);
3020*4882a593Smuzhiyun if (new_bh == NULL) {
3021*4882a593Smuzhiyun ret = -ENOMEM;
3022*4882a593Smuzhiyun mlog_errno(ret);
3023*4882a593Smuzhiyun break;
3024*4882a593Smuzhiyun }
3025*4882a593Smuzhiyun
3026*4882a593Smuzhiyun ocfs2_set_new_buffer_uptodate(ci, new_bh);
3027*4882a593Smuzhiyun
3028*4882a593Smuzhiyun ret = ocfs2_read_block(ci, old_block, &old_bh, NULL);
3029*4882a593Smuzhiyun if (ret) {
3030*4882a593Smuzhiyun mlog_errno(ret);
3031*4882a593Smuzhiyun break;
3032*4882a593Smuzhiyun }
3033*4882a593Smuzhiyun
3034*4882a593Smuzhiyun ret = ocfs2_journal_access(handle, ci, new_bh,
3035*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_CREATE);
3036*4882a593Smuzhiyun if (ret) {
3037*4882a593Smuzhiyun mlog_errno(ret);
3038*4882a593Smuzhiyun break;
3039*4882a593Smuzhiyun }
3040*4882a593Smuzhiyun
3041*4882a593Smuzhiyun memcpy(new_bh->b_data, old_bh->b_data, sb->s_blocksize);
3042*4882a593Smuzhiyun ocfs2_journal_dirty(handle, new_bh);
3043*4882a593Smuzhiyun
3044*4882a593Smuzhiyun brelse(new_bh);
3045*4882a593Smuzhiyun brelse(old_bh);
3046*4882a593Smuzhiyun new_bh = NULL;
3047*4882a593Smuzhiyun old_bh = NULL;
3048*4882a593Smuzhiyun }
3049*4882a593Smuzhiyun
3050*4882a593Smuzhiyun brelse(new_bh);
3051*4882a593Smuzhiyun brelse(old_bh);
3052*4882a593Smuzhiyun return ret;
3053*4882a593Smuzhiyun }
3054*4882a593Smuzhiyun
ocfs2_clear_ext_refcount(handle_t * handle,struct ocfs2_extent_tree * et,u32 cpos,u32 p_cluster,u32 len,unsigned int ext_flags,struct ocfs2_alloc_context * meta_ac,struct ocfs2_cached_dealloc_ctxt * dealloc)3055*4882a593Smuzhiyun static int ocfs2_clear_ext_refcount(handle_t *handle,
3056*4882a593Smuzhiyun struct ocfs2_extent_tree *et,
3057*4882a593Smuzhiyun u32 cpos, u32 p_cluster, u32 len,
3058*4882a593Smuzhiyun unsigned int ext_flags,
3059*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac,
3060*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc)
3061*4882a593Smuzhiyun {
3062*4882a593Smuzhiyun int ret, index;
3063*4882a593Smuzhiyun struct ocfs2_extent_rec replace_rec;
3064*4882a593Smuzhiyun struct ocfs2_path *path = NULL;
3065*4882a593Smuzhiyun struct ocfs2_extent_list *el;
3066*4882a593Smuzhiyun struct super_block *sb = ocfs2_metadata_cache_get_super(et->et_ci);
3067*4882a593Smuzhiyun u64 ino = ocfs2_metadata_cache_owner(et->et_ci);
3068*4882a593Smuzhiyun
3069*4882a593Smuzhiyun trace_ocfs2_clear_ext_refcount((unsigned long long)ino,
3070*4882a593Smuzhiyun cpos, len, p_cluster, ext_flags);
3071*4882a593Smuzhiyun
3072*4882a593Smuzhiyun memset(&replace_rec, 0, sizeof(replace_rec));
3073*4882a593Smuzhiyun replace_rec.e_cpos = cpu_to_le32(cpos);
3074*4882a593Smuzhiyun replace_rec.e_leaf_clusters = cpu_to_le16(len);
3075*4882a593Smuzhiyun replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(sb,
3076*4882a593Smuzhiyun p_cluster));
3077*4882a593Smuzhiyun replace_rec.e_flags = ext_flags;
3078*4882a593Smuzhiyun replace_rec.e_flags &= ~OCFS2_EXT_REFCOUNTED;
3079*4882a593Smuzhiyun
3080*4882a593Smuzhiyun path = ocfs2_new_path_from_et(et);
3081*4882a593Smuzhiyun if (!path) {
3082*4882a593Smuzhiyun ret = -ENOMEM;
3083*4882a593Smuzhiyun mlog_errno(ret);
3084*4882a593Smuzhiyun goto out;
3085*4882a593Smuzhiyun }
3086*4882a593Smuzhiyun
3087*4882a593Smuzhiyun ret = ocfs2_find_path(et->et_ci, path, cpos);
3088*4882a593Smuzhiyun if (ret) {
3089*4882a593Smuzhiyun mlog_errno(ret);
3090*4882a593Smuzhiyun goto out;
3091*4882a593Smuzhiyun }
3092*4882a593Smuzhiyun
3093*4882a593Smuzhiyun el = path_leaf_el(path);
3094*4882a593Smuzhiyun
3095*4882a593Smuzhiyun index = ocfs2_search_extent_list(el, cpos);
3096*4882a593Smuzhiyun if (index == -1) {
3097*4882a593Smuzhiyun ret = ocfs2_error(sb,
3098*4882a593Smuzhiyun "Inode %llu has an extent at cpos %u which can no longer be found\n",
3099*4882a593Smuzhiyun (unsigned long long)ino, cpos);
3100*4882a593Smuzhiyun goto out;
3101*4882a593Smuzhiyun }
3102*4882a593Smuzhiyun
3103*4882a593Smuzhiyun ret = ocfs2_split_extent(handle, et, path, index,
3104*4882a593Smuzhiyun &replace_rec, meta_ac, dealloc);
3105*4882a593Smuzhiyun if (ret)
3106*4882a593Smuzhiyun mlog_errno(ret);
3107*4882a593Smuzhiyun
3108*4882a593Smuzhiyun out:
3109*4882a593Smuzhiyun ocfs2_free_path(path);
3110*4882a593Smuzhiyun return ret;
3111*4882a593Smuzhiyun }
3112*4882a593Smuzhiyun
ocfs2_replace_clusters(handle_t * handle,struct ocfs2_cow_context * context,u32 cpos,u32 old,u32 new,u32 len,unsigned int ext_flags)3113*4882a593Smuzhiyun static int ocfs2_replace_clusters(handle_t *handle,
3114*4882a593Smuzhiyun struct ocfs2_cow_context *context,
3115*4882a593Smuzhiyun u32 cpos, u32 old,
3116*4882a593Smuzhiyun u32 new, u32 len,
3117*4882a593Smuzhiyun unsigned int ext_flags)
3118*4882a593Smuzhiyun {
3119*4882a593Smuzhiyun int ret;
3120*4882a593Smuzhiyun struct ocfs2_caching_info *ci = context->data_et.et_ci;
3121*4882a593Smuzhiyun u64 ino = ocfs2_metadata_cache_owner(ci);
3122*4882a593Smuzhiyun
3123*4882a593Smuzhiyun trace_ocfs2_replace_clusters((unsigned long long)ino,
3124*4882a593Smuzhiyun cpos, old, new, len, ext_flags);
3125*4882a593Smuzhiyun
3126*4882a593Smuzhiyun /*If the old clusters is unwritten, no need to duplicate. */
3127*4882a593Smuzhiyun if (!(ext_flags & OCFS2_EXT_UNWRITTEN)) {
3128*4882a593Smuzhiyun ret = context->cow_duplicate_clusters(handle, context->inode,
3129*4882a593Smuzhiyun cpos, old, new, len);
3130*4882a593Smuzhiyun if (ret) {
3131*4882a593Smuzhiyun mlog_errno(ret);
3132*4882a593Smuzhiyun goto out;
3133*4882a593Smuzhiyun }
3134*4882a593Smuzhiyun }
3135*4882a593Smuzhiyun
3136*4882a593Smuzhiyun ret = ocfs2_clear_ext_refcount(handle, &context->data_et,
3137*4882a593Smuzhiyun cpos, new, len, ext_flags,
3138*4882a593Smuzhiyun context->meta_ac, &context->dealloc);
3139*4882a593Smuzhiyun if (ret)
3140*4882a593Smuzhiyun mlog_errno(ret);
3141*4882a593Smuzhiyun out:
3142*4882a593Smuzhiyun return ret;
3143*4882a593Smuzhiyun }
3144*4882a593Smuzhiyun
ocfs2_cow_sync_writeback(struct super_block * sb,struct inode * inode,u32 cpos,u32 num_clusters)3145*4882a593Smuzhiyun int ocfs2_cow_sync_writeback(struct super_block *sb,
3146*4882a593Smuzhiyun struct inode *inode,
3147*4882a593Smuzhiyun u32 cpos, u32 num_clusters)
3148*4882a593Smuzhiyun {
3149*4882a593Smuzhiyun int ret = 0;
3150*4882a593Smuzhiyun loff_t offset, end, map_end;
3151*4882a593Smuzhiyun pgoff_t page_index;
3152*4882a593Smuzhiyun struct page *page;
3153*4882a593Smuzhiyun
3154*4882a593Smuzhiyun if (ocfs2_should_order_data(inode))
3155*4882a593Smuzhiyun return 0;
3156*4882a593Smuzhiyun
3157*4882a593Smuzhiyun offset = ((loff_t)cpos) << OCFS2_SB(sb)->s_clustersize_bits;
3158*4882a593Smuzhiyun end = offset + (num_clusters << OCFS2_SB(sb)->s_clustersize_bits);
3159*4882a593Smuzhiyun
3160*4882a593Smuzhiyun ret = filemap_fdatawrite_range(inode->i_mapping,
3161*4882a593Smuzhiyun offset, end - 1);
3162*4882a593Smuzhiyun if (ret < 0) {
3163*4882a593Smuzhiyun mlog_errno(ret);
3164*4882a593Smuzhiyun return ret;
3165*4882a593Smuzhiyun }
3166*4882a593Smuzhiyun
3167*4882a593Smuzhiyun while (offset < end) {
3168*4882a593Smuzhiyun page_index = offset >> PAGE_SHIFT;
3169*4882a593Smuzhiyun map_end = ((loff_t)page_index + 1) << PAGE_SHIFT;
3170*4882a593Smuzhiyun if (map_end > end)
3171*4882a593Smuzhiyun map_end = end;
3172*4882a593Smuzhiyun
3173*4882a593Smuzhiyun page = find_or_create_page(inode->i_mapping,
3174*4882a593Smuzhiyun page_index, GFP_NOFS);
3175*4882a593Smuzhiyun BUG_ON(!page);
3176*4882a593Smuzhiyun
3177*4882a593Smuzhiyun wait_on_page_writeback(page);
3178*4882a593Smuzhiyun if (PageError(page)) {
3179*4882a593Smuzhiyun ret = -EIO;
3180*4882a593Smuzhiyun mlog_errno(ret);
3181*4882a593Smuzhiyun } else
3182*4882a593Smuzhiyun mark_page_accessed(page);
3183*4882a593Smuzhiyun
3184*4882a593Smuzhiyun unlock_page(page);
3185*4882a593Smuzhiyun put_page(page);
3186*4882a593Smuzhiyun page = NULL;
3187*4882a593Smuzhiyun offset = map_end;
3188*4882a593Smuzhiyun if (ret)
3189*4882a593Smuzhiyun break;
3190*4882a593Smuzhiyun }
3191*4882a593Smuzhiyun
3192*4882a593Smuzhiyun return ret;
3193*4882a593Smuzhiyun }
3194*4882a593Smuzhiyun
ocfs2_di_get_clusters(struct ocfs2_cow_context * context,u32 v_cluster,u32 * p_cluster,u32 * num_clusters,unsigned int * extent_flags)3195*4882a593Smuzhiyun static int ocfs2_di_get_clusters(struct ocfs2_cow_context *context,
3196*4882a593Smuzhiyun u32 v_cluster, u32 *p_cluster,
3197*4882a593Smuzhiyun u32 *num_clusters,
3198*4882a593Smuzhiyun unsigned int *extent_flags)
3199*4882a593Smuzhiyun {
3200*4882a593Smuzhiyun return ocfs2_get_clusters(context->inode, v_cluster, p_cluster,
3201*4882a593Smuzhiyun num_clusters, extent_flags);
3202*4882a593Smuzhiyun }
3203*4882a593Smuzhiyun
ocfs2_make_clusters_writable(struct super_block * sb,struct ocfs2_cow_context * context,u32 cpos,u32 p_cluster,u32 num_clusters,unsigned int e_flags)3204*4882a593Smuzhiyun static int ocfs2_make_clusters_writable(struct super_block *sb,
3205*4882a593Smuzhiyun struct ocfs2_cow_context *context,
3206*4882a593Smuzhiyun u32 cpos, u32 p_cluster,
3207*4882a593Smuzhiyun u32 num_clusters, unsigned int e_flags)
3208*4882a593Smuzhiyun {
3209*4882a593Smuzhiyun int ret, delete, index, credits = 0;
3210*4882a593Smuzhiyun u32 new_bit, new_len, orig_num_clusters;
3211*4882a593Smuzhiyun unsigned int set_len;
3212*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(sb);
3213*4882a593Smuzhiyun handle_t *handle;
3214*4882a593Smuzhiyun struct buffer_head *ref_leaf_bh = NULL;
3215*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci = &context->ref_tree->rf_ci;
3216*4882a593Smuzhiyun struct ocfs2_refcount_rec rec;
3217*4882a593Smuzhiyun
3218*4882a593Smuzhiyun trace_ocfs2_make_clusters_writable(cpos, p_cluster,
3219*4882a593Smuzhiyun num_clusters, e_flags);
3220*4882a593Smuzhiyun
3221*4882a593Smuzhiyun ret = ocfs2_lock_refcount_allocators(sb, p_cluster, num_clusters,
3222*4882a593Smuzhiyun &context->data_et,
3223*4882a593Smuzhiyun ref_ci,
3224*4882a593Smuzhiyun context->ref_root_bh,
3225*4882a593Smuzhiyun &context->meta_ac,
3226*4882a593Smuzhiyun &context->data_ac, &credits);
3227*4882a593Smuzhiyun if (ret) {
3228*4882a593Smuzhiyun mlog_errno(ret);
3229*4882a593Smuzhiyun return ret;
3230*4882a593Smuzhiyun }
3231*4882a593Smuzhiyun
3232*4882a593Smuzhiyun if (context->post_refcount)
3233*4882a593Smuzhiyun credits += context->post_refcount->credits;
3234*4882a593Smuzhiyun
3235*4882a593Smuzhiyun credits += context->extra_credits;
3236*4882a593Smuzhiyun handle = ocfs2_start_trans(osb, credits);
3237*4882a593Smuzhiyun if (IS_ERR(handle)) {
3238*4882a593Smuzhiyun ret = PTR_ERR(handle);
3239*4882a593Smuzhiyun mlog_errno(ret);
3240*4882a593Smuzhiyun goto out;
3241*4882a593Smuzhiyun }
3242*4882a593Smuzhiyun
3243*4882a593Smuzhiyun orig_num_clusters = num_clusters;
3244*4882a593Smuzhiyun
3245*4882a593Smuzhiyun while (num_clusters) {
3246*4882a593Smuzhiyun ret = ocfs2_get_refcount_rec(ref_ci, context->ref_root_bh,
3247*4882a593Smuzhiyun p_cluster, num_clusters,
3248*4882a593Smuzhiyun &rec, &index, &ref_leaf_bh);
3249*4882a593Smuzhiyun if (ret) {
3250*4882a593Smuzhiyun mlog_errno(ret);
3251*4882a593Smuzhiyun goto out_commit;
3252*4882a593Smuzhiyun }
3253*4882a593Smuzhiyun
3254*4882a593Smuzhiyun BUG_ON(!rec.r_refcount);
3255*4882a593Smuzhiyun set_len = min((u64)p_cluster + num_clusters,
3256*4882a593Smuzhiyun le64_to_cpu(rec.r_cpos) +
3257*4882a593Smuzhiyun le32_to_cpu(rec.r_clusters)) - p_cluster;
3258*4882a593Smuzhiyun
3259*4882a593Smuzhiyun /*
3260*4882a593Smuzhiyun * There are many different situation here.
3261*4882a593Smuzhiyun * 1. If refcount == 1, remove the flag and don't COW.
3262*4882a593Smuzhiyun * 2. If refcount > 1, allocate clusters.
3263*4882a593Smuzhiyun * Here we may not allocate r_len once at a time, so continue
3264*4882a593Smuzhiyun * until we reach num_clusters.
3265*4882a593Smuzhiyun */
3266*4882a593Smuzhiyun if (le32_to_cpu(rec.r_refcount) == 1) {
3267*4882a593Smuzhiyun delete = 0;
3268*4882a593Smuzhiyun ret = ocfs2_clear_ext_refcount(handle,
3269*4882a593Smuzhiyun &context->data_et,
3270*4882a593Smuzhiyun cpos, p_cluster,
3271*4882a593Smuzhiyun set_len, e_flags,
3272*4882a593Smuzhiyun context->meta_ac,
3273*4882a593Smuzhiyun &context->dealloc);
3274*4882a593Smuzhiyun if (ret) {
3275*4882a593Smuzhiyun mlog_errno(ret);
3276*4882a593Smuzhiyun goto out_commit;
3277*4882a593Smuzhiyun }
3278*4882a593Smuzhiyun } else {
3279*4882a593Smuzhiyun delete = 1;
3280*4882a593Smuzhiyun
3281*4882a593Smuzhiyun ret = __ocfs2_claim_clusters(handle,
3282*4882a593Smuzhiyun context->data_ac,
3283*4882a593Smuzhiyun 1, set_len,
3284*4882a593Smuzhiyun &new_bit, &new_len);
3285*4882a593Smuzhiyun if (ret) {
3286*4882a593Smuzhiyun mlog_errno(ret);
3287*4882a593Smuzhiyun goto out_commit;
3288*4882a593Smuzhiyun }
3289*4882a593Smuzhiyun
3290*4882a593Smuzhiyun ret = ocfs2_replace_clusters(handle, context,
3291*4882a593Smuzhiyun cpos, p_cluster, new_bit,
3292*4882a593Smuzhiyun new_len, e_flags);
3293*4882a593Smuzhiyun if (ret) {
3294*4882a593Smuzhiyun mlog_errno(ret);
3295*4882a593Smuzhiyun goto out_commit;
3296*4882a593Smuzhiyun }
3297*4882a593Smuzhiyun set_len = new_len;
3298*4882a593Smuzhiyun }
3299*4882a593Smuzhiyun
3300*4882a593Smuzhiyun ret = __ocfs2_decrease_refcount(handle, ref_ci,
3301*4882a593Smuzhiyun context->ref_root_bh,
3302*4882a593Smuzhiyun p_cluster, set_len,
3303*4882a593Smuzhiyun context->meta_ac,
3304*4882a593Smuzhiyun &context->dealloc, delete);
3305*4882a593Smuzhiyun if (ret) {
3306*4882a593Smuzhiyun mlog_errno(ret);
3307*4882a593Smuzhiyun goto out_commit;
3308*4882a593Smuzhiyun }
3309*4882a593Smuzhiyun
3310*4882a593Smuzhiyun cpos += set_len;
3311*4882a593Smuzhiyun p_cluster += set_len;
3312*4882a593Smuzhiyun num_clusters -= set_len;
3313*4882a593Smuzhiyun brelse(ref_leaf_bh);
3314*4882a593Smuzhiyun ref_leaf_bh = NULL;
3315*4882a593Smuzhiyun }
3316*4882a593Smuzhiyun
3317*4882a593Smuzhiyun /* handle any post_cow action. */
3318*4882a593Smuzhiyun if (context->post_refcount && context->post_refcount->func) {
3319*4882a593Smuzhiyun ret = context->post_refcount->func(context->inode, handle,
3320*4882a593Smuzhiyun context->post_refcount->para);
3321*4882a593Smuzhiyun if (ret) {
3322*4882a593Smuzhiyun mlog_errno(ret);
3323*4882a593Smuzhiyun goto out_commit;
3324*4882a593Smuzhiyun }
3325*4882a593Smuzhiyun }
3326*4882a593Smuzhiyun
3327*4882a593Smuzhiyun /*
3328*4882a593Smuzhiyun * Here we should write the new page out first if we are
3329*4882a593Smuzhiyun * in write-back mode.
3330*4882a593Smuzhiyun */
3331*4882a593Smuzhiyun if (context->get_clusters == ocfs2_di_get_clusters) {
3332*4882a593Smuzhiyun ret = ocfs2_cow_sync_writeback(sb, context->inode, cpos,
3333*4882a593Smuzhiyun orig_num_clusters);
3334*4882a593Smuzhiyun if (ret)
3335*4882a593Smuzhiyun mlog_errno(ret);
3336*4882a593Smuzhiyun }
3337*4882a593Smuzhiyun
3338*4882a593Smuzhiyun out_commit:
3339*4882a593Smuzhiyun ocfs2_commit_trans(osb, handle);
3340*4882a593Smuzhiyun
3341*4882a593Smuzhiyun out:
3342*4882a593Smuzhiyun if (context->data_ac) {
3343*4882a593Smuzhiyun ocfs2_free_alloc_context(context->data_ac);
3344*4882a593Smuzhiyun context->data_ac = NULL;
3345*4882a593Smuzhiyun }
3346*4882a593Smuzhiyun if (context->meta_ac) {
3347*4882a593Smuzhiyun ocfs2_free_alloc_context(context->meta_ac);
3348*4882a593Smuzhiyun context->meta_ac = NULL;
3349*4882a593Smuzhiyun }
3350*4882a593Smuzhiyun brelse(ref_leaf_bh);
3351*4882a593Smuzhiyun
3352*4882a593Smuzhiyun return ret;
3353*4882a593Smuzhiyun }
3354*4882a593Smuzhiyun
ocfs2_replace_cow(struct ocfs2_cow_context * context)3355*4882a593Smuzhiyun static int ocfs2_replace_cow(struct ocfs2_cow_context *context)
3356*4882a593Smuzhiyun {
3357*4882a593Smuzhiyun int ret = 0;
3358*4882a593Smuzhiyun struct inode *inode = context->inode;
3359*4882a593Smuzhiyun u32 cow_start = context->cow_start, cow_len = context->cow_len;
3360*4882a593Smuzhiyun u32 p_cluster, num_clusters;
3361*4882a593Smuzhiyun unsigned int ext_flags;
3362*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3363*4882a593Smuzhiyun
3364*4882a593Smuzhiyun if (!ocfs2_refcount_tree(osb)) {
3365*4882a593Smuzhiyun return ocfs2_error(inode->i_sb, "Inode %lu want to use refcount tree, but the feature bit is not set in the super block\n",
3366*4882a593Smuzhiyun inode->i_ino);
3367*4882a593Smuzhiyun }
3368*4882a593Smuzhiyun
3369*4882a593Smuzhiyun ocfs2_init_dealloc_ctxt(&context->dealloc);
3370*4882a593Smuzhiyun
3371*4882a593Smuzhiyun while (cow_len) {
3372*4882a593Smuzhiyun ret = context->get_clusters(context, cow_start, &p_cluster,
3373*4882a593Smuzhiyun &num_clusters, &ext_flags);
3374*4882a593Smuzhiyun if (ret) {
3375*4882a593Smuzhiyun mlog_errno(ret);
3376*4882a593Smuzhiyun break;
3377*4882a593Smuzhiyun }
3378*4882a593Smuzhiyun
3379*4882a593Smuzhiyun BUG_ON(!(ext_flags & OCFS2_EXT_REFCOUNTED));
3380*4882a593Smuzhiyun
3381*4882a593Smuzhiyun if (cow_len < num_clusters)
3382*4882a593Smuzhiyun num_clusters = cow_len;
3383*4882a593Smuzhiyun
3384*4882a593Smuzhiyun ret = ocfs2_make_clusters_writable(inode->i_sb, context,
3385*4882a593Smuzhiyun cow_start, p_cluster,
3386*4882a593Smuzhiyun num_clusters, ext_flags);
3387*4882a593Smuzhiyun if (ret) {
3388*4882a593Smuzhiyun mlog_errno(ret);
3389*4882a593Smuzhiyun break;
3390*4882a593Smuzhiyun }
3391*4882a593Smuzhiyun
3392*4882a593Smuzhiyun cow_len -= num_clusters;
3393*4882a593Smuzhiyun cow_start += num_clusters;
3394*4882a593Smuzhiyun }
3395*4882a593Smuzhiyun
3396*4882a593Smuzhiyun if (ocfs2_dealloc_has_cluster(&context->dealloc)) {
3397*4882a593Smuzhiyun ocfs2_schedule_truncate_log_flush(osb, 1);
3398*4882a593Smuzhiyun ocfs2_run_deallocs(osb, &context->dealloc);
3399*4882a593Smuzhiyun }
3400*4882a593Smuzhiyun
3401*4882a593Smuzhiyun return ret;
3402*4882a593Smuzhiyun }
3403*4882a593Smuzhiyun
3404*4882a593Smuzhiyun /*
3405*4882a593Smuzhiyun * Starting at cpos, try to CoW write_len clusters. Don't CoW
3406*4882a593Smuzhiyun * past max_cpos. This will stop when it runs into a hole or an
3407*4882a593Smuzhiyun * unrefcounted extent.
3408*4882a593Smuzhiyun */
ocfs2_refcount_cow_hunk(struct inode * inode,struct buffer_head * di_bh,u32 cpos,u32 write_len,u32 max_cpos)3409*4882a593Smuzhiyun static int ocfs2_refcount_cow_hunk(struct inode *inode,
3410*4882a593Smuzhiyun struct buffer_head *di_bh,
3411*4882a593Smuzhiyun u32 cpos, u32 write_len, u32 max_cpos)
3412*4882a593Smuzhiyun {
3413*4882a593Smuzhiyun int ret;
3414*4882a593Smuzhiyun u32 cow_start = 0, cow_len = 0;
3415*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3416*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3417*4882a593Smuzhiyun struct buffer_head *ref_root_bh = NULL;
3418*4882a593Smuzhiyun struct ocfs2_refcount_tree *ref_tree;
3419*4882a593Smuzhiyun struct ocfs2_cow_context *context = NULL;
3420*4882a593Smuzhiyun
3421*4882a593Smuzhiyun BUG_ON(!ocfs2_is_refcount_inode(inode));
3422*4882a593Smuzhiyun
3423*4882a593Smuzhiyun ret = ocfs2_refcount_cal_cow_clusters(inode, &di->id2.i_list,
3424*4882a593Smuzhiyun cpos, write_len, max_cpos,
3425*4882a593Smuzhiyun &cow_start, &cow_len);
3426*4882a593Smuzhiyun if (ret) {
3427*4882a593Smuzhiyun mlog_errno(ret);
3428*4882a593Smuzhiyun goto out;
3429*4882a593Smuzhiyun }
3430*4882a593Smuzhiyun
3431*4882a593Smuzhiyun trace_ocfs2_refcount_cow_hunk(OCFS2_I(inode)->ip_blkno,
3432*4882a593Smuzhiyun cpos, write_len, max_cpos,
3433*4882a593Smuzhiyun cow_start, cow_len);
3434*4882a593Smuzhiyun
3435*4882a593Smuzhiyun BUG_ON(cow_len == 0);
3436*4882a593Smuzhiyun
3437*4882a593Smuzhiyun context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
3438*4882a593Smuzhiyun if (!context) {
3439*4882a593Smuzhiyun ret = -ENOMEM;
3440*4882a593Smuzhiyun mlog_errno(ret);
3441*4882a593Smuzhiyun goto out;
3442*4882a593Smuzhiyun }
3443*4882a593Smuzhiyun
3444*4882a593Smuzhiyun ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
3445*4882a593Smuzhiyun 1, &ref_tree, &ref_root_bh);
3446*4882a593Smuzhiyun if (ret) {
3447*4882a593Smuzhiyun mlog_errno(ret);
3448*4882a593Smuzhiyun goto out;
3449*4882a593Smuzhiyun }
3450*4882a593Smuzhiyun
3451*4882a593Smuzhiyun context->inode = inode;
3452*4882a593Smuzhiyun context->cow_start = cow_start;
3453*4882a593Smuzhiyun context->cow_len = cow_len;
3454*4882a593Smuzhiyun context->ref_tree = ref_tree;
3455*4882a593Smuzhiyun context->ref_root_bh = ref_root_bh;
3456*4882a593Smuzhiyun context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_page;
3457*4882a593Smuzhiyun context->get_clusters = ocfs2_di_get_clusters;
3458*4882a593Smuzhiyun
3459*4882a593Smuzhiyun ocfs2_init_dinode_extent_tree(&context->data_et,
3460*4882a593Smuzhiyun INODE_CACHE(inode), di_bh);
3461*4882a593Smuzhiyun
3462*4882a593Smuzhiyun ret = ocfs2_replace_cow(context);
3463*4882a593Smuzhiyun if (ret)
3464*4882a593Smuzhiyun mlog_errno(ret);
3465*4882a593Smuzhiyun
3466*4882a593Smuzhiyun /*
3467*4882a593Smuzhiyun * truncate the extent map here since no matter whether we meet with
3468*4882a593Smuzhiyun * any error during the action, we shouldn't trust cached extent map
3469*4882a593Smuzhiyun * any more.
3470*4882a593Smuzhiyun */
3471*4882a593Smuzhiyun ocfs2_extent_map_trunc(inode, cow_start);
3472*4882a593Smuzhiyun
3473*4882a593Smuzhiyun ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3474*4882a593Smuzhiyun brelse(ref_root_bh);
3475*4882a593Smuzhiyun out:
3476*4882a593Smuzhiyun kfree(context);
3477*4882a593Smuzhiyun return ret;
3478*4882a593Smuzhiyun }
3479*4882a593Smuzhiyun
3480*4882a593Smuzhiyun /*
3481*4882a593Smuzhiyun * CoW any and all clusters between cpos and cpos+write_len.
3482*4882a593Smuzhiyun * Don't CoW past max_cpos. If this returns successfully, all
3483*4882a593Smuzhiyun * clusters between cpos and cpos+write_len are safe to modify.
3484*4882a593Smuzhiyun */
ocfs2_refcount_cow(struct inode * inode,struct buffer_head * di_bh,u32 cpos,u32 write_len,u32 max_cpos)3485*4882a593Smuzhiyun int ocfs2_refcount_cow(struct inode *inode,
3486*4882a593Smuzhiyun struct buffer_head *di_bh,
3487*4882a593Smuzhiyun u32 cpos, u32 write_len, u32 max_cpos)
3488*4882a593Smuzhiyun {
3489*4882a593Smuzhiyun int ret = 0;
3490*4882a593Smuzhiyun u32 p_cluster, num_clusters;
3491*4882a593Smuzhiyun unsigned int ext_flags;
3492*4882a593Smuzhiyun
3493*4882a593Smuzhiyun while (write_len) {
3494*4882a593Smuzhiyun ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
3495*4882a593Smuzhiyun &num_clusters, &ext_flags);
3496*4882a593Smuzhiyun if (ret) {
3497*4882a593Smuzhiyun mlog_errno(ret);
3498*4882a593Smuzhiyun break;
3499*4882a593Smuzhiyun }
3500*4882a593Smuzhiyun
3501*4882a593Smuzhiyun if (write_len < num_clusters)
3502*4882a593Smuzhiyun num_clusters = write_len;
3503*4882a593Smuzhiyun
3504*4882a593Smuzhiyun if (ext_flags & OCFS2_EXT_REFCOUNTED) {
3505*4882a593Smuzhiyun ret = ocfs2_refcount_cow_hunk(inode, di_bh, cpos,
3506*4882a593Smuzhiyun num_clusters, max_cpos);
3507*4882a593Smuzhiyun if (ret) {
3508*4882a593Smuzhiyun mlog_errno(ret);
3509*4882a593Smuzhiyun break;
3510*4882a593Smuzhiyun }
3511*4882a593Smuzhiyun }
3512*4882a593Smuzhiyun
3513*4882a593Smuzhiyun write_len -= num_clusters;
3514*4882a593Smuzhiyun cpos += num_clusters;
3515*4882a593Smuzhiyun }
3516*4882a593Smuzhiyun
3517*4882a593Smuzhiyun return ret;
3518*4882a593Smuzhiyun }
3519*4882a593Smuzhiyun
ocfs2_xattr_value_get_clusters(struct ocfs2_cow_context * context,u32 v_cluster,u32 * p_cluster,u32 * num_clusters,unsigned int * extent_flags)3520*4882a593Smuzhiyun static int ocfs2_xattr_value_get_clusters(struct ocfs2_cow_context *context,
3521*4882a593Smuzhiyun u32 v_cluster, u32 *p_cluster,
3522*4882a593Smuzhiyun u32 *num_clusters,
3523*4882a593Smuzhiyun unsigned int *extent_flags)
3524*4882a593Smuzhiyun {
3525*4882a593Smuzhiyun struct inode *inode = context->inode;
3526*4882a593Smuzhiyun struct ocfs2_xattr_value_root *xv = context->cow_object;
3527*4882a593Smuzhiyun
3528*4882a593Smuzhiyun return ocfs2_xattr_get_clusters(inode, v_cluster, p_cluster,
3529*4882a593Smuzhiyun num_clusters, &xv->xr_list,
3530*4882a593Smuzhiyun extent_flags);
3531*4882a593Smuzhiyun }
3532*4882a593Smuzhiyun
3533*4882a593Smuzhiyun /*
3534*4882a593Smuzhiyun * Given a xattr value root, calculate the most meta/credits we need for
3535*4882a593Smuzhiyun * refcount tree change if we truncate it to 0.
3536*4882a593Smuzhiyun */
ocfs2_refcounted_xattr_delete_need(struct inode * inode,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_xattr_value_root * xv,int * meta_add,int * credits)3537*4882a593Smuzhiyun int ocfs2_refcounted_xattr_delete_need(struct inode *inode,
3538*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci,
3539*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
3540*4882a593Smuzhiyun struct ocfs2_xattr_value_root *xv,
3541*4882a593Smuzhiyun int *meta_add, int *credits)
3542*4882a593Smuzhiyun {
3543*4882a593Smuzhiyun int ret = 0, index, ref_blocks = 0;
3544*4882a593Smuzhiyun u32 p_cluster, num_clusters;
3545*4882a593Smuzhiyun u32 cpos = 0, clusters = le32_to_cpu(xv->xr_clusters);
3546*4882a593Smuzhiyun struct ocfs2_refcount_block *rb;
3547*4882a593Smuzhiyun struct ocfs2_refcount_rec rec;
3548*4882a593Smuzhiyun struct buffer_head *ref_leaf_bh = NULL;
3549*4882a593Smuzhiyun
3550*4882a593Smuzhiyun while (cpos < clusters) {
3551*4882a593Smuzhiyun ret = ocfs2_xattr_get_clusters(inode, cpos, &p_cluster,
3552*4882a593Smuzhiyun &num_clusters, &xv->xr_list,
3553*4882a593Smuzhiyun NULL);
3554*4882a593Smuzhiyun if (ret) {
3555*4882a593Smuzhiyun mlog_errno(ret);
3556*4882a593Smuzhiyun goto out;
3557*4882a593Smuzhiyun }
3558*4882a593Smuzhiyun
3559*4882a593Smuzhiyun cpos += num_clusters;
3560*4882a593Smuzhiyun
3561*4882a593Smuzhiyun while (num_clusters) {
3562*4882a593Smuzhiyun ret = ocfs2_get_refcount_rec(ref_ci, ref_root_bh,
3563*4882a593Smuzhiyun p_cluster, num_clusters,
3564*4882a593Smuzhiyun &rec, &index,
3565*4882a593Smuzhiyun &ref_leaf_bh);
3566*4882a593Smuzhiyun if (ret) {
3567*4882a593Smuzhiyun mlog_errno(ret);
3568*4882a593Smuzhiyun goto out;
3569*4882a593Smuzhiyun }
3570*4882a593Smuzhiyun
3571*4882a593Smuzhiyun BUG_ON(!rec.r_refcount);
3572*4882a593Smuzhiyun
3573*4882a593Smuzhiyun rb = (struct ocfs2_refcount_block *)ref_leaf_bh->b_data;
3574*4882a593Smuzhiyun
3575*4882a593Smuzhiyun /*
3576*4882a593Smuzhiyun * We really don't know whether the other clusters is in
3577*4882a593Smuzhiyun * this refcount block or not, so just take the worst
3578*4882a593Smuzhiyun * case that all the clusters are in this block and each
3579*4882a593Smuzhiyun * one will split a refcount rec, so totally we need
3580*4882a593Smuzhiyun * clusters * 2 new refcount rec.
3581*4882a593Smuzhiyun */
3582*4882a593Smuzhiyun if (le16_to_cpu(rb->rf_records.rl_used) + clusters * 2 >
3583*4882a593Smuzhiyun le16_to_cpu(rb->rf_records.rl_count))
3584*4882a593Smuzhiyun ref_blocks++;
3585*4882a593Smuzhiyun
3586*4882a593Smuzhiyun *credits += 1;
3587*4882a593Smuzhiyun brelse(ref_leaf_bh);
3588*4882a593Smuzhiyun ref_leaf_bh = NULL;
3589*4882a593Smuzhiyun
3590*4882a593Smuzhiyun if (num_clusters <= le32_to_cpu(rec.r_clusters))
3591*4882a593Smuzhiyun break;
3592*4882a593Smuzhiyun else
3593*4882a593Smuzhiyun num_clusters -= le32_to_cpu(rec.r_clusters);
3594*4882a593Smuzhiyun p_cluster += num_clusters;
3595*4882a593Smuzhiyun }
3596*4882a593Smuzhiyun }
3597*4882a593Smuzhiyun
3598*4882a593Smuzhiyun *meta_add += ref_blocks;
3599*4882a593Smuzhiyun if (!ref_blocks)
3600*4882a593Smuzhiyun goto out;
3601*4882a593Smuzhiyun
3602*4882a593Smuzhiyun rb = (struct ocfs2_refcount_block *)ref_root_bh->b_data;
3603*4882a593Smuzhiyun if (le32_to_cpu(rb->rf_flags) & OCFS2_REFCOUNT_TREE_FL)
3604*4882a593Smuzhiyun *credits += OCFS2_EXPAND_REFCOUNT_TREE_CREDITS;
3605*4882a593Smuzhiyun else {
3606*4882a593Smuzhiyun struct ocfs2_extent_tree et;
3607*4882a593Smuzhiyun
3608*4882a593Smuzhiyun ocfs2_init_refcount_extent_tree(&et, ref_ci, ref_root_bh);
3609*4882a593Smuzhiyun *credits += ocfs2_calc_extend_credits(inode->i_sb,
3610*4882a593Smuzhiyun et.et_root_el);
3611*4882a593Smuzhiyun }
3612*4882a593Smuzhiyun
3613*4882a593Smuzhiyun out:
3614*4882a593Smuzhiyun brelse(ref_leaf_bh);
3615*4882a593Smuzhiyun return ret;
3616*4882a593Smuzhiyun }
3617*4882a593Smuzhiyun
3618*4882a593Smuzhiyun /*
3619*4882a593Smuzhiyun * Do CoW for xattr.
3620*4882a593Smuzhiyun */
ocfs2_refcount_cow_xattr(struct inode * inode,struct ocfs2_dinode * di,struct ocfs2_xattr_value_buf * vb,struct ocfs2_refcount_tree * ref_tree,struct buffer_head * ref_root_bh,u32 cpos,u32 write_len,struct ocfs2_post_refcount * post)3621*4882a593Smuzhiyun int ocfs2_refcount_cow_xattr(struct inode *inode,
3622*4882a593Smuzhiyun struct ocfs2_dinode *di,
3623*4882a593Smuzhiyun struct ocfs2_xattr_value_buf *vb,
3624*4882a593Smuzhiyun struct ocfs2_refcount_tree *ref_tree,
3625*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
3626*4882a593Smuzhiyun u32 cpos, u32 write_len,
3627*4882a593Smuzhiyun struct ocfs2_post_refcount *post)
3628*4882a593Smuzhiyun {
3629*4882a593Smuzhiyun int ret;
3630*4882a593Smuzhiyun struct ocfs2_xattr_value_root *xv = vb->vb_xv;
3631*4882a593Smuzhiyun struct ocfs2_cow_context *context = NULL;
3632*4882a593Smuzhiyun u32 cow_start, cow_len;
3633*4882a593Smuzhiyun
3634*4882a593Smuzhiyun BUG_ON(!ocfs2_is_refcount_inode(inode));
3635*4882a593Smuzhiyun
3636*4882a593Smuzhiyun ret = ocfs2_refcount_cal_cow_clusters(inode, &xv->xr_list,
3637*4882a593Smuzhiyun cpos, write_len, UINT_MAX,
3638*4882a593Smuzhiyun &cow_start, &cow_len);
3639*4882a593Smuzhiyun if (ret) {
3640*4882a593Smuzhiyun mlog_errno(ret);
3641*4882a593Smuzhiyun goto out;
3642*4882a593Smuzhiyun }
3643*4882a593Smuzhiyun
3644*4882a593Smuzhiyun BUG_ON(cow_len == 0);
3645*4882a593Smuzhiyun
3646*4882a593Smuzhiyun context = kzalloc(sizeof(struct ocfs2_cow_context), GFP_NOFS);
3647*4882a593Smuzhiyun if (!context) {
3648*4882a593Smuzhiyun ret = -ENOMEM;
3649*4882a593Smuzhiyun mlog_errno(ret);
3650*4882a593Smuzhiyun goto out;
3651*4882a593Smuzhiyun }
3652*4882a593Smuzhiyun
3653*4882a593Smuzhiyun context->inode = inode;
3654*4882a593Smuzhiyun context->cow_start = cow_start;
3655*4882a593Smuzhiyun context->cow_len = cow_len;
3656*4882a593Smuzhiyun context->ref_tree = ref_tree;
3657*4882a593Smuzhiyun context->ref_root_bh = ref_root_bh;
3658*4882a593Smuzhiyun context->cow_object = xv;
3659*4882a593Smuzhiyun
3660*4882a593Smuzhiyun context->cow_duplicate_clusters = ocfs2_duplicate_clusters_by_jbd;
3661*4882a593Smuzhiyun /* We need the extra credits for duplicate_clusters by jbd. */
3662*4882a593Smuzhiyun context->extra_credits =
3663*4882a593Smuzhiyun ocfs2_clusters_to_blocks(inode->i_sb, 1) * cow_len;
3664*4882a593Smuzhiyun context->get_clusters = ocfs2_xattr_value_get_clusters;
3665*4882a593Smuzhiyun context->post_refcount = post;
3666*4882a593Smuzhiyun
3667*4882a593Smuzhiyun ocfs2_init_xattr_value_extent_tree(&context->data_et,
3668*4882a593Smuzhiyun INODE_CACHE(inode), vb);
3669*4882a593Smuzhiyun
3670*4882a593Smuzhiyun ret = ocfs2_replace_cow(context);
3671*4882a593Smuzhiyun if (ret)
3672*4882a593Smuzhiyun mlog_errno(ret);
3673*4882a593Smuzhiyun
3674*4882a593Smuzhiyun out:
3675*4882a593Smuzhiyun kfree(context);
3676*4882a593Smuzhiyun return ret;
3677*4882a593Smuzhiyun }
3678*4882a593Smuzhiyun
3679*4882a593Smuzhiyun /*
3680*4882a593Smuzhiyun * Insert a new extent into refcount tree and mark a extent rec
3681*4882a593Smuzhiyun * as refcounted in the dinode tree.
3682*4882a593Smuzhiyun */
ocfs2_add_refcount_flag(struct inode * inode,struct ocfs2_extent_tree * data_et,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,u32 cpos,u32 p_cluster,u32 num_clusters,struct ocfs2_cached_dealloc_ctxt * dealloc,struct ocfs2_post_refcount * post)3683*4882a593Smuzhiyun int ocfs2_add_refcount_flag(struct inode *inode,
3684*4882a593Smuzhiyun struct ocfs2_extent_tree *data_et,
3685*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci,
3686*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
3687*4882a593Smuzhiyun u32 cpos, u32 p_cluster, u32 num_clusters,
3688*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc,
3689*4882a593Smuzhiyun struct ocfs2_post_refcount *post)
3690*4882a593Smuzhiyun {
3691*4882a593Smuzhiyun int ret;
3692*4882a593Smuzhiyun handle_t *handle;
3693*4882a593Smuzhiyun int credits = 1, ref_blocks = 0;
3694*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3695*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac = NULL;
3696*4882a593Smuzhiyun
3697*4882a593Smuzhiyun /* We need to be able to handle at least an extent tree split. */
3698*4882a593Smuzhiyun ref_blocks = ocfs2_extend_meta_needed(data_et->et_root_el);
3699*4882a593Smuzhiyun
3700*4882a593Smuzhiyun ret = ocfs2_calc_refcount_meta_credits(inode->i_sb,
3701*4882a593Smuzhiyun ref_ci, ref_root_bh,
3702*4882a593Smuzhiyun p_cluster, num_clusters,
3703*4882a593Smuzhiyun &ref_blocks, &credits);
3704*4882a593Smuzhiyun if (ret) {
3705*4882a593Smuzhiyun mlog_errno(ret);
3706*4882a593Smuzhiyun goto out;
3707*4882a593Smuzhiyun }
3708*4882a593Smuzhiyun
3709*4882a593Smuzhiyun trace_ocfs2_add_refcount_flag(ref_blocks, credits);
3710*4882a593Smuzhiyun
3711*4882a593Smuzhiyun if (ref_blocks) {
3712*4882a593Smuzhiyun ret = ocfs2_reserve_new_metadata_blocks(osb,
3713*4882a593Smuzhiyun ref_blocks, &meta_ac);
3714*4882a593Smuzhiyun if (ret) {
3715*4882a593Smuzhiyun mlog_errno(ret);
3716*4882a593Smuzhiyun goto out;
3717*4882a593Smuzhiyun }
3718*4882a593Smuzhiyun }
3719*4882a593Smuzhiyun
3720*4882a593Smuzhiyun if (post)
3721*4882a593Smuzhiyun credits += post->credits;
3722*4882a593Smuzhiyun
3723*4882a593Smuzhiyun handle = ocfs2_start_trans(osb, credits);
3724*4882a593Smuzhiyun if (IS_ERR(handle)) {
3725*4882a593Smuzhiyun ret = PTR_ERR(handle);
3726*4882a593Smuzhiyun mlog_errno(ret);
3727*4882a593Smuzhiyun goto out;
3728*4882a593Smuzhiyun }
3729*4882a593Smuzhiyun
3730*4882a593Smuzhiyun ret = ocfs2_mark_extent_refcounted(inode, data_et, handle,
3731*4882a593Smuzhiyun cpos, num_clusters, p_cluster,
3732*4882a593Smuzhiyun meta_ac, dealloc);
3733*4882a593Smuzhiyun if (ret) {
3734*4882a593Smuzhiyun mlog_errno(ret);
3735*4882a593Smuzhiyun goto out_commit;
3736*4882a593Smuzhiyun }
3737*4882a593Smuzhiyun
3738*4882a593Smuzhiyun ret = __ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
3739*4882a593Smuzhiyun p_cluster, num_clusters, 0,
3740*4882a593Smuzhiyun meta_ac, dealloc);
3741*4882a593Smuzhiyun if (ret) {
3742*4882a593Smuzhiyun mlog_errno(ret);
3743*4882a593Smuzhiyun goto out_commit;
3744*4882a593Smuzhiyun }
3745*4882a593Smuzhiyun
3746*4882a593Smuzhiyun if (post && post->func) {
3747*4882a593Smuzhiyun ret = post->func(inode, handle, post->para);
3748*4882a593Smuzhiyun if (ret)
3749*4882a593Smuzhiyun mlog_errno(ret);
3750*4882a593Smuzhiyun }
3751*4882a593Smuzhiyun
3752*4882a593Smuzhiyun out_commit:
3753*4882a593Smuzhiyun ocfs2_commit_trans(osb, handle);
3754*4882a593Smuzhiyun out:
3755*4882a593Smuzhiyun if (meta_ac)
3756*4882a593Smuzhiyun ocfs2_free_alloc_context(meta_ac);
3757*4882a593Smuzhiyun return ret;
3758*4882a593Smuzhiyun }
3759*4882a593Smuzhiyun
ocfs2_change_ctime(struct inode * inode,struct buffer_head * di_bh)3760*4882a593Smuzhiyun static int ocfs2_change_ctime(struct inode *inode,
3761*4882a593Smuzhiyun struct buffer_head *di_bh)
3762*4882a593Smuzhiyun {
3763*4882a593Smuzhiyun int ret;
3764*4882a593Smuzhiyun handle_t *handle;
3765*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3766*4882a593Smuzhiyun
3767*4882a593Smuzhiyun handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
3768*4882a593Smuzhiyun OCFS2_INODE_UPDATE_CREDITS);
3769*4882a593Smuzhiyun if (IS_ERR(handle)) {
3770*4882a593Smuzhiyun ret = PTR_ERR(handle);
3771*4882a593Smuzhiyun mlog_errno(ret);
3772*4882a593Smuzhiyun goto out;
3773*4882a593Smuzhiyun }
3774*4882a593Smuzhiyun
3775*4882a593Smuzhiyun ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
3776*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
3777*4882a593Smuzhiyun if (ret) {
3778*4882a593Smuzhiyun mlog_errno(ret);
3779*4882a593Smuzhiyun goto out_commit;
3780*4882a593Smuzhiyun }
3781*4882a593Smuzhiyun
3782*4882a593Smuzhiyun inode->i_ctime = current_time(inode);
3783*4882a593Smuzhiyun di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
3784*4882a593Smuzhiyun di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
3785*4882a593Smuzhiyun
3786*4882a593Smuzhiyun ocfs2_journal_dirty(handle, di_bh);
3787*4882a593Smuzhiyun
3788*4882a593Smuzhiyun out_commit:
3789*4882a593Smuzhiyun ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
3790*4882a593Smuzhiyun out:
3791*4882a593Smuzhiyun return ret;
3792*4882a593Smuzhiyun }
3793*4882a593Smuzhiyun
ocfs2_attach_refcount_tree(struct inode * inode,struct buffer_head * di_bh)3794*4882a593Smuzhiyun static int ocfs2_attach_refcount_tree(struct inode *inode,
3795*4882a593Smuzhiyun struct buffer_head *di_bh)
3796*4882a593Smuzhiyun {
3797*4882a593Smuzhiyun int ret, data_changed = 0;
3798*4882a593Smuzhiyun struct buffer_head *ref_root_bh = NULL;
3799*4882a593Smuzhiyun struct ocfs2_inode_info *oi = OCFS2_I(inode);
3800*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
3801*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3802*4882a593Smuzhiyun struct ocfs2_refcount_tree *ref_tree;
3803*4882a593Smuzhiyun unsigned int ext_flags;
3804*4882a593Smuzhiyun loff_t size;
3805*4882a593Smuzhiyun u32 cpos, num_clusters, clusters, p_cluster;
3806*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt dealloc;
3807*4882a593Smuzhiyun struct ocfs2_extent_tree di_et;
3808*4882a593Smuzhiyun
3809*4882a593Smuzhiyun ocfs2_init_dealloc_ctxt(&dealloc);
3810*4882a593Smuzhiyun
3811*4882a593Smuzhiyun if (!ocfs2_is_refcount_inode(inode)) {
3812*4882a593Smuzhiyun ret = ocfs2_create_refcount_tree(inode, di_bh);
3813*4882a593Smuzhiyun if (ret) {
3814*4882a593Smuzhiyun mlog_errno(ret);
3815*4882a593Smuzhiyun goto out;
3816*4882a593Smuzhiyun }
3817*4882a593Smuzhiyun }
3818*4882a593Smuzhiyun
3819*4882a593Smuzhiyun BUG_ON(!di->i_refcount_loc);
3820*4882a593Smuzhiyun ret = ocfs2_lock_refcount_tree(osb,
3821*4882a593Smuzhiyun le64_to_cpu(di->i_refcount_loc), 1,
3822*4882a593Smuzhiyun &ref_tree, &ref_root_bh);
3823*4882a593Smuzhiyun if (ret) {
3824*4882a593Smuzhiyun mlog_errno(ret);
3825*4882a593Smuzhiyun goto out;
3826*4882a593Smuzhiyun }
3827*4882a593Smuzhiyun
3828*4882a593Smuzhiyun if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
3829*4882a593Smuzhiyun goto attach_xattr;
3830*4882a593Smuzhiyun
3831*4882a593Smuzhiyun ocfs2_init_dinode_extent_tree(&di_et, INODE_CACHE(inode), di_bh);
3832*4882a593Smuzhiyun
3833*4882a593Smuzhiyun size = i_size_read(inode);
3834*4882a593Smuzhiyun clusters = ocfs2_clusters_for_bytes(inode->i_sb, size);
3835*4882a593Smuzhiyun
3836*4882a593Smuzhiyun cpos = 0;
3837*4882a593Smuzhiyun while (cpos < clusters) {
3838*4882a593Smuzhiyun ret = ocfs2_get_clusters(inode, cpos, &p_cluster,
3839*4882a593Smuzhiyun &num_clusters, &ext_flags);
3840*4882a593Smuzhiyun if (ret) {
3841*4882a593Smuzhiyun mlog_errno(ret);
3842*4882a593Smuzhiyun goto unlock;
3843*4882a593Smuzhiyun }
3844*4882a593Smuzhiyun if (p_cluster && !(ext_flags & OCFS2_EXT_REFCOUNTED)) {
3845*4882a593Smuzhiyun ret = ocfs2_add_refcount_flag(inode, &di_et,
3846*4882a593Smuzhiyun &ref_tree->rf_ci,
3847*4882a593Smuzhiyun ref_root_bh, cpos,
3848*4882a593Smuzhiyun p_cluster, num_clusters,
3849*4882a593Smuzhiyun &dealloc, NULL);
3850*4882a593Smuzhiyun if (ret) {
3851*4882a593Smuzhiyun mlog_errno(ret);
3852*4882a593Smuzhiyun goto unlock;
3853*4882a593Smuzhiyun }
3854*4882a593Smuzhiyun
3855*4882a593Smuzhiyun data_changed = 1;
3856*4882a593Smuzhiyun }
3857*4882a593Smuzhiyun cpos += num_clusters;
3858*4882a593Smuzhiyun }
3859*4882a593Smuzhiyun
3860*4882a593Smuzhiyun attach_xattr:
3861*4882a593Smuzhiyun if (oi->ip_dyn_features & OCFS2_HAS_XATTR_FL) {
3862*4882a593Smuzhiyun ret = ocfs2_xattr_attach_refcount_tree(inode, di_bh,
3863*4882a593Smuzhiyun &ref_tree->rf_ci,
3864*4882a593Smuzhiyun ref_root_bh,
3865*4882a593Smuzhiyun &dealloc);
3866*4882a593Smuzhiyun if (ret) {
3867*4882a593Smuzhiyun mlog_errno(ret);
3868*4882a593Smuzhiyun goto unlock;
3869*4882a593Smuzhiyun }
3870*4882a593Smuzhiyun }
3871*4882a593Smuzhiyun
3872*4882a593Smuzhiyun if (data_changed) {
3873*4882a593Smuzhiyun ret = ocfs2_change_ctime(inode, di_bh);
3874*4882a593Smuzhiyun if (ret)
3875*4882a593Smuzhiyun mlog_errno(ret);
3876*4882a593Smuzhiyun }
3877*4882a593Smuzhiyun
3878*4882a593Smuzhiyun unlock:
3879*4882a593Smuzhiyun ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
3880*4882a593Smuzhiyun brelse(ref_root_bh);
3881*4882a593Smuzhiyun
3882*4882a593Smuzhiyun if (!ret && ocfs2_dealloc_has_cluster(&dealloc)) {
3883*4882a593Smuzhiyun ocfs2_schedule_truncate_log_flush(osb, 1);
3884*4882a593Smuzhiyun ocfs2_run_deallocs(osb, &dealloc);
3885*4882a593Smuzhiyun }
3886*4882a593Smuzhiyun out:
3887*4882a593Smuzhiyun /*
3888*4882a593Smuzhiyun * Empty the extent map so that we may get the right extent
3889*4882a593Smuzhiyun * record from the disk.
3890*4882a593Smuzhiyun */
3891*4882a593Smuzhiyun ocfs2_extent_map_trunc(inode, 0);
3892*4882a593Smuzhiyun
3893*4882a593Smuzhiyun return ret;
3894*4882a593Smuzhiyun }
3895*4882a593Smuzhiyun
ocfs2_add_refcounted_extent(struct inode * inode,struct ocfs2_extent_tree * et,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,u32 cpos,u32 p_cluster,u32 num_clusters,unsigned int ext_flags,struct ocfs2_cached_dealloc_ctxt * dealloc)3896*4882a593Smuzhiyun static int ocfs2_add_refcounted_extent(struct inode *inode,
3897*4882a593Smuzhiyun struct ocfs2_extent_tree *et,
3898*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci,
3899*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
3900*4882a593Smuzhiyun u32 cpos, u32 p_cluster, u32 num_clusters,
3901*4882a593Smuzhiyun unsigned int ext_flags,
3902*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc)
3903*4882a593Smuzhiyun {
3904*4882a593Smuzhiyun int ret;
3905*4882a593Smuzhiyun handle_t *handle;
3906*4882a593Smuzhiyun int credits = 0;
3907*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
3908*4882a593Smuzhiyun struct ocfs2_alloc_context *meta_ac = NULL;
3909*4882a593Smuzhiyun
3910*4882a593Smuzhiyun ret = ocfs2_lock_refcount_allocators(inode->i_sb,
3911*4882a593Smuzhiyun p_cluster, num_clusters,
3912*4882a593Smuzhiyun et, ref_ci,
3913*4882a593Smuzhiyun ref_root_bh, &meta_ac,
3914*4882a593Smuzhiyun NULL, &credits);
3915*4882a593Smuzhiyun if (ret) {
3916*4882a593Smuzhiyun mlog_errno(ret);
3917*4882a593Smuzhiyun goto out;
3918*4882a593Smuzhiyun }
3919*4882a593Smuzhiyun
3920*4882a593Smuzhiyun handle = ocfs2_start_trans(osb, credits);
3921*4882a593Smuzhiyun if (IS_ERR(handle)) {
3922*4882a593Smuzhiyun ret = PTR_ERR(handle);
3923*4882a593Smuzhiyun mlog_errno(ret);
3924*4882a593Smuzhiyun goto out;
3925*4882a593Smuzhiyun }
3926*4882a593Smuzhiyun
3927*4882a593Smuzhiyun ret = ocfs2_insert_extent(handle, et, cpos,
3928*4882a593Smuzhiyun ocfs2_clusters_to_blocks(inode->i_sb, p_cluster),
3929*4882a593Smuzhiyun num_clusters, ext_flags, meta_ac);
3930*4882a593Smuzhiyun if (ret) {
3931*4882a593Smuzhiyun mlog_errno(ret);
3932*4882a593Smuzhiyun goto out_commit;
3933*4882a593Smuzhiyun }
3934*4882a593Smuzhiyun
3935*4882a593Smuzhiyun ret = ocfs2_increase_refcount(handle, ref_ci, ref_root_bh,
3936*4882a593Smuzhiyun p_cluster, num_clusters,
3937*4882a593Smuzhiyun meta_ac, dealloc);
3938*4882a593Smuzhiyun if (ret) {
3939*4882a593Smuzhiyun mlog_errno(ret);
3940*4882a593Smuzhiyun goto out_commit;
3941*4882a593Smuzhiyun }
3942*4882a593Smuzhiyun
3943*4882a593Smuzhiyun ret = dquot_alloc_space_nodirty(inode,
3944*4882a593Smuzhiyun ocfs2_clusters_to_bytes(osb->sb, num_clusters));
3945*4882a593Smuzhiyun if (ret)
3946*4882a593Smuzhiyun mlog_errno(ret);
3947*4882a593Smuzhiyun
3948*4882a593Smuzhiyun out_commit:
3949*4882a593Smuzhiyun ocfs2_commit_trans(osb, handle);
3950*4882a593Smuzhiyun out:
3951*4882a593Smuzhiyun if (meta_ac)
3952*4882a593Smuzhiyun ocfs2_free_alloc_context(meta_ac);
3953*4882a593Smuzhiyun return ret;
3954*4882a593Smuzhiyun }
3955*4882a593Smuzhiyun
ocfs2_duplicate_inline_data(struct inode * s_inode,struct buffer_head * s_bh,struct inode * t_inode,struct buffer_head * t_bh)3956*4882a593Smuzhiyun static int ocfs2_duplicate_inline_data(struct inode *s_inode,
3957*4882a593Smuzhiyun struct buffer_head *s_bh,
3958*4882a593Smuzhiyun struct inode *t_inode,
3959*4882a593Smuzhiyun struct buffer_head *t_bh)
3960*4882a593Smuzhiyun {
3961*4882a593Smuzhiyun int ret;
3962*4882a593Smuzhiyun handle_t *handle;
3963*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb);
3964*4882a593Smuzhiyun struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data;
3965*4882a593Smuzhiyun struct ocfs2_dinode *t_di = (struct ocfs2_dinode *)t_bh->b_data;
3966*4882a593Smuzhiyun
3967*4882a593Smuzhiyun BUG_ON(!(OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL));
3968*4882a593Smuzhiyun
3969*4882a593Smuzhiyun handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
3970*4882a593Smuzhiyun if (IS_ERR(handle)) {
3971*4882a593Smuzhiyun ret = PTR_ERR(handle);
3972*4882a593Smuzhiyun mlog_errno(ret);
3973*4882a593Smuzhiyun goto out;
3974*4882a593Smuzhiyun }
3975*4882a593Smuzhiyun
3976*4882a593Smuzhiyun ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh,
3977*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
3978*4882a593Smuzhiyun if (ret) {
3979*4882a593Smuzhiyun mlog_errno(ret);
3980*4882a593Smuzhiyun goto out_commit;
3981*4882a593Smuzhiyun }
3982*4882a593Smuzhiyun
3983*4882a593Smuzhiyun t_di->id2.i_data.id_count = s_di->id2.i_data.id_count;
3984*4882a593Smuzhiyun memcpy(t_di->id2.i_data.id_data, s_di->id2.i_data.id_data,
3985*4882a593Smuzhiyun le16_to_cpu(s_di->id2.i_data.id_count));
3986*4882a593Smuzhiyun spin_lock(&OCFS2_I(t_inode)->ip_lock);
3987*4882a593Smuzhiyun OCFS2_I(t_inode)->ip_dyn_features |= OCFS2_INLINE_DATA_FL;
3988*4882a593Smuzhiyun t_di->i_dyn_features = cpu_to_le16(OCFS2_I(t_inode)->ip_dyn_features);
3989*4882a593Smuzhiyun spin_unlock(&OCFS2_I(t_inode)->ip_lock);
3990*4882a593Smuzhiyun
3991*4882a593Smuzhiyun ocfs2_journal_dirty(handle, t_bh);
3992*4882a593Smuzhiyun
3993*4882a593Smuzhiyun out_commit:
3994*4882a593Smuzhiyun ocfs2_commit_trans(osb, handle);
3995*4882a593Smuzhiyun out:
3996*4882a593Smuzhiyun return ret;
3997*4882a593Smuzhiyun }
3998*4882a593Smuzhiyun
ocfs2_duplicate_extent_list(struct inode * s_inode,struct inode * t_inode,struct buffer_head * t_bh,struct ocfs2_caching_info * ref_ci,struct buffer_head * ref_root_bh,struct ocfs2_cached_dealloc_ctxt * dealloc)3999*4882a593Smuzhiyun static int ocfs2_duplicate_extent_list(struct inode *s_inode,
4000*4882a593Smuzhiyun struct inode *t_inode,
4001*4882a593Smuzhiyun struct buffer_head *t_bh,
4002*4882a593Smuzhiyun struct ocfs2_caching_info *ref_ci,
4003*4882a593Smuzhiyun struct buffer_head *ref_root_bh,
4004*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc)
4005*4882a593Smuzhiyun {
4006*4882a593Smuzhiyun int ret = 0;
4007*4882a593Smuzhiyun u32 p_cluster, num_clusters, clusters, cpos;
4008*4882a593Smuzhiyun loff_t size;
4009*4882a593Smuzhiyun unsigned int ext_flags;
4010*4882a593Smuzhiyun struct ocfs2_extent_tree et;
4011*4882a593Smuzhiyun
4012*4882a593Smuzhiyun ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(t_inode), t_bh);
4013*4882a593Smuzhiyun
4014*4882a593Smuzhiyun size = i_size_read(s_inode);
4015*4882a593Smuzhiyun clusters = ocfs2_clusters_for_bytes(s_inode->i_sb, size);
4016*4882a593Smuzhiyun
4017*4882a593Smuzhiyun cpos = 0;
4018*4882a593Smuzhiyun while (cpos < clusters) {
4019*4882a593Smuzhiyun ret = ocfs2_get_clusters(s_inode, cpos, &p_cluster,
4020*4882a593Smuzhiyun &num_clusters, &ext_flags);
4021*4882a593Smuzhiyun if (ret) {
4022*4882a593Smuzhiyun mlog_errno(ret);
4023*4882a593Smuzhiyun goto out;
4024*4882a593Smuzhiyun }
4025*4882a593Smuzhiyun if (p_cluster) {
4026*4882a593Smuzhiyun ret = ocfs2_add_refcounted_extent(t_inode, &et,
4027*4882a593Smuzhiyun ref_ci, ref_root_bh,
4028*4882a593Smuzhiyun cpos, p_cluster,
4029*4882a593Smuzhiyun num_clusters,
4030*4882a593Smuzhiyun ext_flags,
4031*4882a593Smuzhiyun dealloc);
4032*4882a593Smuzhiyun if (ret) {
4033*4882a593Smuzhiyun mlog_errno(ret);
4034*4882a593Smuzhiyun goto out;
4035*4882a593Smuzhiyun }
4036*4882a593Smuzhiyun }
4037*4882a593Smuzhiyun
4038*4882a593Smuzhiyun cpos += num_clusters;
4039*4882a593Smuzhiyun }
4040*4882a593Smuzhiyun
4041*4882a593Smuzhiyun out:
4042*4882a593Smuzhiyun return ret;
4043*4882a593Smuzhiyun }
4044*4882a593Smuzhiyun
4045*4882a593Smuzhiyun /*
4046*4882a593Smuzhiyun * change the new file's attributes to the src.
4047*4882a593Smuzhiyun *
4048*4882a593Smuzhiyun * reflink creates a snapshot of a file, that means the attributes
4049*4882a593Smuzhiyun * must be identical except for three exceptions - nlink, ino, and ctime.
4050*4882a593Smuzhiyun */
ocfs2_complete_reflink(struct inode * s_inode,struct buffer_head * s_bh,struct inode * t_inode,struct buffer_head * t_bh,bool preserve)4051*4882a593Smuzhiyun static int ocfs2_complete_reflink(struct inode *s_inode,
4052*4882a593Smuzhiyun struct buffer_head *s_bh,
4053*4882a593Smuzhiyun struct inode *t_inode,
4054*4882a593Smuzhiyun struct buffer_head *t_bh,
4055*4882a593Smuzhiyun bool preserve)
4056*4882a593Smuzhiyun {
4057*4882a593Smuzhiyun int ret;
4058*4882a593Smuzhiyun handle_t *handle;
4059*4882a593Smuzhiyun struct ocfs2_dinode *s_di = (struct ocfs2_dinode *)s_bh->b_data;
4060*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)t_bh->b_data;
4061*4882a593Smuzhiyun loff_t size = i_size_read(s_inode);
4062*4882a593Smuzhiyun
4063*4882a593Smuzhiyun handle = ocfs2_start_trans(OCFS2_SB(t_inode->i_sb),
4064*4882a593Smuzhiyun OCFS2_INODE_UPDATE_CREDITS);
4065*4882a593Smuzhiyun if (IS_ERR(handle)) {
4066*4882a593Smuzhiyun ret = PTR_ERR(handle);
4067*4882a593Smuzhiyun mlog_errno(ret);
4068*4882a593Smuzhiyun return ret;
4069*4882a593Smuzhiyun }
4070*4882a593Smuzhiyun
4071*4882a593Smuzhiyun ret = ocfs2_journal_access_di(handle, INODE_CACHE(t_inode), t_bh,
4072*4882a593Smuzhiyun OCFS2_JOURNAL_ACCESS_WRITE);
4073*4882a593Smuzhiyun if (ret) {
4074*4882a593Smuzhiyun mlog_errno(ret);
4075*4882a593Smuzhiyun goto out_commit;
4076*4882a593Smuzhiyun }
4077*4882a593Smuzhiyun
4078*4882a593Smuzhiyun spin_lock(&OCFS2_I(t_inode)->ip_lock);
4079*4882a593Smuzhiyun OCFS2_I(t_inode)->ip_clusters = OCFS2_I(s_inode)->ip_clusters;
4080*4882a593Smuzhiyun OCFS2_I(t_inode)->ip_attr = OCFS2_I(s_inode)->ip_attr;
4081*4882a593Smuzhiyun OCFS2_I(t_inode)->ip_dyn_features = OCFS2_I(s_inode)->ip_dyn_features;
4082*4882a593Smuzhiyun spin_unlock(&OCFS2_I(t_inode)->ip_lock);
4083*4882a593Smuzhiyun i_size_write(t_inode, size);
4084*4882a593Smuzhiyun t_inode->i_blocks = s_inode->i_blocks;
4085*4882a593Smuzhiyun
4086*4882a593Smuzhiyun di->i_xattr_inline_size = s_di->i_xattr_inline_size;
4087*4882a593Smuzhiyun di->i_clusters = s_di->i_clusters;
4088*4882a593Smuzhiyun di->i_size = s_di->i_size;
4089*4882a593Smuzhiyun di->i_dyn_features = s_di->i_dyn_features;
4090*4882a593Smuzhiyun di->i_attr = s_di->i_attr;
4091*4882a593Smuzhiyun
4092*4882a593Smuzhiyun if (preserve) {
4093*4882a593Smuzhiyun t_inode->i_uid = s_inode->i_uid;
4094*4882a593Smuzhiyun t_inode->i_gid = s_inode->i_gid;
4095*4882a593Smuzhiyun t_inode->i_mode = s_inode->i_mode;
4096*4882a593Smuzhiyun di->i_uid = s_di->i_uid;
4097*4882a593Smuzhiyun di->i_gid = s_di->i_gid;
4098*4882a593Smuzhiyun di->i_mode = s_di->i_mode;
4099*4882a593Smuzhiyun
4100*4882a593Smuzhiyun /*
4101*4882a593Smuzhiyun * update time.
4102*4882a593Smuzhiyun * we want mtime to appear identical to the source and
4103*4882a593Smuzhiyun * update ctime.
4104*4882a593Smuzhiyun */
4105*4882a593Smuzhiyun t_inode->i_ctime = current_time(t_inode);
4106*4882a593Smuzhiyun
4107*4882a593Smuzhiyun di->i_ctime = cpu_to_le64(t_inode->i_ctime.tv_sec);
4108*4882a593Smuzhiyun di->i_ctime_nsec = cpu_to_le32(t_inode->i_ctime.tv_nsec);
4109*4882a593Smuzhiyun
4110*4882a593Smuzhiyun t_inode->i_mtime = s_inode->i_mtime;
4111*4882a593Smuzhiyun di->i_mtime = s_di->i_mtime;
4112*4882a593Smuzhiyun di->i_mtime_nsec = s_di->i_mtime_nsec;
4113*4882a593Smuzhiyun }
4114*4882a593Smuzhiyun
4115*4882a593Smuzhiyun ocfs2_journal_dirty(handle, t_bh);
4116*4882a593Smuzhiyun
4117*4882a593Smuzhiyun out_commit:
4118*4882a593Smuzhiyun ocfs2_commit_trans(OCFS2_SB(t_inode->i_sb), handle);
4119*4882a593Smuzhiyun return ret;
4120*4882a593Smuzhiyun }
4121*4882a593Smuzhiyun
ocfs2_create_reflink_node(struct inode * s_inode,struct buffer_head * s_bh,struct inode * t_inode,struct buffer_head * t_bh,bool preserve)4122*4882a593Smuzhiyun static int ocfs2_create_reflink_node(struct inode *s_inode,
4123*4882a593Smuzhiyun struct buffer_head *s_bh,
4124*4882a593Smuzhiyun struct inode *t_inode,
4125*4882a593Smuzhiyun struct buffer_head *t_bh,
4126*4882a593Smuzhiyun bool preserve)
4127*4882a593Smuzhiyun {
4128*4882a593Smuzhiyun int ret;
4129*4882a593Smuzhiyun struct buffer_head *ref_root_bh = NULL;
4130*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt dealloc;
4131*4882a593Smuzhiyun struct ocfs2_super *osb = OCFS2_SB(s_inode->i_sb);
4132*4882a593Smuzhiyun struct ocfs2_dinode *di = (struct ocfs2_dinode *)s_bh->b_data;
4133*4882a593Smuzhiyun struct ocfs2_refcount_tree *ref_tree;
4134*4882a593Smuzhiyun
4135*4882a593Smuzhiyun ocfs2_init_dealloc_ctxt(&dealloc);
4136*4882a593Smuzhiyun
4137*4882a593Smuzhiyun ret = ocfs2_set_refcount_tree(t_inode, t_bh,
4138*4882a593Smuzhiyun le64_to_cpu(di->i_refcount_loc));
4139*4882a593Smuzhiyun if (ret) {
4140*4882a593Smuzhiyun mlog_errno(ret);
4141*4882a593Smuzhiyun goto out;
4142*4882a593Smuzhiyun }
4143*4882a593Smuzhiyun
4144*4882a593Smuzhiyun if (OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
4145*4882a593Smuzhiyun ret = ocfs2_duplicate_inline_data(s_inode, s_bh,
4146*4882a593Smuzhiyun t_inode, t_bh);
4147*4882a593Smuzhiyun if (ret)
4148*4882a593Smuzhiyun mlog_errno(ret);
4149*4882a593Smuzhiyun goto out;
4150*4882a593Smuzhiyun }
4151*4882a593Smuzhiyun
4152*4882a593Smuzhiyun ret = ocfs2_lock_refcount_tree(osb, le64_to_cpu(di->i_refcount_loc),
4153*4882a593Smuzhiyun 1, &ref_tree, &ref_root_bh);
4154*4882a593Smuzhiyun if (ret) {
4155*4882a593Smuzhiyun mlog_errno(ret);
4156*4882a593Smuzhiyun goto out;
4157*4882a593Smuzhiyun }
4158*4882a593Smuzhiyun
4159*4882a593Smuzhiyun ret = ocfs2_duplicate_extent_list(s_inode, t_inode, t_bh,
4160*4882a593Smuzhiyun &ref_tree->rf_ci, ref_root_bh,
4161*4882a593Smuzhiyun &dealloc);
4162*4882a593Smuzhiyun if (ret) {
4163*4882a593Smuzhiyun mlog_errno(ret);
4164*4882a593Smuzhiyun goto out_unlock_refcount;
4165*4882a593Smuzhiyun }
4166*4882a593Smuzhiyun
4167*4882a593Smuzhiyun out_unlock_refcount:
4168*4882a593Smuzhiyun ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
4169*4882a593Smuzhiyun brelse(ref_root_bh);
4170*4882a593Smuzhiyun out:
4171*4882a593Smuzhiyun if (ocfs2_dealloc_has_cluster(&dealloc)) {
4172*4882a593Smuzhiyun ocfs2_schedule_truncate_log_flush(osb, 1);
4173*4882a593Smuzhiyun ocfs2_run_deallocs(osb, &dealloc);
4174*4882a593Smuzhiyun }
4175*4882a593Smuzhiyun
4176*4882a593Smuzhiyun return ret;
4177*4882a593Smuzhiyun }
4178*4882a593Smuzhiyun
__ocfs2_reflink(struct dentry * old_dentry,struct buffer_head * old_bh,struct inode * new_inode,bool preserve)4179*4882a593Smuzhiyun static int __ocfs2_reflink(struct dentry *old_dentry,
4180*4882a593Smuzhiyun struct buffer_head *old_bh,
4181*4882a593Smuzhiyun struct inode *new_inode,
4182*4882a593Smuzhiyun bool preserve)
4183*4882a593Smuzhiyun {
4184*4882a593Smuzhiyun int ret;
4185*4882a593Smuzhiyun struct inode *inode = d_inode(old_dentry);
4186*4882a593Smuzhiyun struct buffer_head *new_bh = NULL;
4187*4882a593Smuzhiyun
4188*4882a593Smuzhiyun if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE) {
4189*4882a593Smuzhiyun ret = -EINVAL;
4190*4882a593Smuzhiyun mlog_errno(ret);
4191*4882a593Smuzhiyun goto out;
4192*4882a593Smuzhiyun }
4193*4882a593Smuzhiyun
4194*4882a593Smuzhiyun ret = filemap_fdatawrite(inode->i_mapping);
4195*4882a593Smuzhiyun if (ret) {
4196*4882a593Smuzhiyun mlog_errno(ret);
4197*4882a593Smuzhiyun goto out;
4198*4882a593Smuzhiyun }
4199*4882a593Smuzhiyun
4200*4882a593Smuzhiyun ret = ocfs2_attach_refcount_tree(inode, old_bh);
4201*4882a593Smuzhiyun if (ret) {
4202*4882a593Smuzhiyun mlog_errno(ret);
4203*4882a593Smuzhiyun goto out;
4204*4882a593Smuzhiyun }
4205*4882a593Smuzhiyun
4206*4882a593Smuzhiyun inode_lock_nested(new_inode, I_MUTEX_CHILD);
4207*4882a593Smuzhiyun ret = ocfs2_inode_lock_nested(new_inode, &new_bh, 1,
4208*4882a593Smuzhiyun OI_LS_REFLINK_TARGET);
4209*4882a593Smuzhiyun if (ret) {
4210*4882a593Smuzhiyun mlog_errno(ret);
4211*4882a593Smuzhiyun goto out_unlock;
4212*4882a593Smuzhiyun }
4213*4882a593Smuzhiyun
4214*4882a593Smuzhiyun ret = ocfs2_create_reflink_node(inode, old_bh,
4215*4882a593Smuzhiyun new_inode, new_bh, preserve);
4216*4882a593Smuzhiyun if (ret) {
4217*4882a593Smuzhiyun mlog_errno(ret);
4218*4882a593Smuzhiyun goto inode_unlock;
4219*4882a593Smuzhiyun }
4220*4882a593Smuzhiyun
4221*4882a593Smuzhiyun if (OCFS2_I(inode)->ip_dyn_features & OCFS2_HAS_XATTR_FL) {
4222*4882a593Smuzhiyun ret = ocfs2_reflink_xattrs(inode, old_bh,
4223*4882a593Smuzhiyun new_inode, new_bh,
4224*4882a593Smuzhiyun preserve);
4225*4882a593Smuzhiyun if (ret) {
4226*4882a593Smuzhiyun mlog_errno(ret);
4227*4882a593Smuzhiyun goto inode_unlock;
4228*4882a593Smuzhiyun }
4229*4882a593Smuzhiyun }
4230*4882a593Smuzhiyun
4231*4882a593Smuzhiyun ret = ocfs2_complete_reflink(inode, old_bh,
4232*4882a593Smuzhiyun new_inode, new_bh, preserve);
4233*4882a593Smuzhiyun if (ret)
4234*4882a593Smuzhiyun mlog_errno(ret);
4235*4882a593Smuzhiyun
4236*4882a593Smuzhiyun inode_unlock:
4237*4882a593Smuzhiyun ocfs2_inode_unlock(new_inode, 1);
4238*4882a593Smuzhiyun brelse(new_bh);
4239*4882a593Smuzhiyun out_unlock:
4240*4882a593Smuzhiyun inode_unlock(new_inode);
4241*4882a593Smuzhiyun out:
4242*4882a593Smuzhiyun if (!ret) {
4243*4882a593Smuzhiyun ret = filemap_fdatawait(inode->i_mapping);
4244*4882a593Smuzhiyun if (ret)
4245*4882a593Smuzhiyun mlog_errno(ret);
4246*4882a593Smuzhiyun }
4247*4882a593Smuzhiyun return ret;
4248*4882a593Smuzhiyun }
4249*4882a593Smuzhiyun
ocfs2_reflink(struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry,bool preserve)4250*4882a593Smuzhiyun static int ocfs2_reflink(struct dentry *old_dentry, struct inode *dir,
4251*4882a593Smuzhiyun struct dentry *new_dentry, bool preserve)
4252*4882a593Smuzhiyun {
4253*4882a593Smuzhiyun int error, had_lock;
4254*4882a593Smuzhiyun struct inode *inode = d_inode(old_dentry);
4255*4882a593Smuzhiyun struct buffer_head *old_bh = NULL;
4256*4882a593Smuzhiyun struct inode *new_orphan_inode = NULL;
4257*4882a593Smuzhiyun struct ocfs2_lock_holder oh;
4258*4882a593Smuzhiyun
4259*4882a593Smuzhiyun if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)))
4260*4882a593Smuzhiyun return -EOPNOTSUPP;
4261*4882a593Smuzhiyun
4262*4882a593Smuzhiyun
4263*4882a593Smuzhiyun error = ocfs2_create_inode_in_orphan(dir, inode->i_mode,
4264*4882a593Smuzhiyun &new_orphan_inode);
4265*4882a593Smuzhiyun if (error) {
4266*4882a593Smuzhiyun mlog_errno(error);
4267*4882a593Smuzhiyun goto out;
4268*4882a593Smuzhiyun }
4269*4882a593Smuzhiyun
4270*4882a593Smuzhiyun error = ocfs2_rw_lock(inode, 1);
4271*4882a593Smuzhiyun if (error) {
4272*4882a593Smuzhiyun mlog_errno(error);
4273*4882a593Smuzhiyun goto out;
4274*4882a593Smuzhiyun }
4275*4882a593Smuzhiyun
4276*4882a593Smuzhiyun error = ocfs2_inode_lock(inode, &old_bh, 1);
4277*4882a593Smuzhiyun if (error) {
4278*4882a593Smuzhiyun mlog_errno(error);
4279*4882a593Smuzhiyun ocfs2_rw_unlock(inode, 1);
4280*4882a593Smuzhiyun goto out;
4281*4882a593Smuzhiyun }
4282*4882a593Smuzhiyun
4283*4882a593Smuzhiyun down_write(&OCFS2_I(inode)->ip_xattr_sem);
4284*4882a593Smuzhiyun down_write(&OCFS2_I(inode)->ip_alloc_sem);
4285*4882a593Smuzhiyun error = __ocfs2_reflink(old_dentry, old_bh,
4286*4882a593Smuzhiyun new_orphan_inode, preserve);
4287*4882a593Smuzhiyun up_write(&OCFS2_I(inode)->ip_alloc_sem);
4288*4882a593Smuzhiyun up_write(&OCFS2_I(inode)->ip_xattr_sem);
4289*4882a593Smuzhiyun
4290*4882a593Smuzhiyun ocfs2_inode_unlock(inode, 1);
4291*4882a593Smuzhiyun ocfs2_rw_unlock(inode, 1);
4292*4882a593Smuzhiyun brelse(old_bh);
4293*4882a593Smuzhiyun
4294*4882a593Smuzhiyun if (error) {
4295*4882a593Smuzhiyun mlog_errno(error);
4296*4882a593Smuzhiyun goto out;
4297*4882a593Smuzhiyun }
4298*4882a593Smuzhiyun
4299*4882a593Smuzhiyun had_lock = ocfs2_inode_lock_tracker(new_orphan_inode, NULL, 1,
4300*4882a593Smuzhiyun &oh);
4301*4882a593Smuzhiyun if (had_lock < 0) {
4302*4882a593Smuzhiyun error = had_lock;
4303*4882a593Smuzhiyun mlog_errno(error);
4304*4882a593Smuzhiyun goto out;
4305*4882a593Smuzhiyun }
4306*4882a593Smuzhiyun
4307*4882a593Smuzhiyun /* If the security isn't preserved, we need to re-initialize them. */
4308*4882a593Smuzhiyun if (!preserve) {
4309*4882a593Smuzhiyun error = ocfs2_init_security_and_acl(dir, new_orphan_inode,
4310*4882a593Smuzhiyun &new_dentry->d_name);
4311*4882a593Smuzhiyun if (error)
4312*4882a593Smuzhiyun mlog_errno(error);
4313*4882a593Smuzhiyun }
4314*4882a593Smuzhiyun if (!error) {
4315*4882a593Smuzhiyun error = ocfs2_mv_orphaned_inode_to_new(dir, new_orphan_inode,
4316*4882a593Smuzhiyun new_dentry);
4317*4882a593Smuzhiyun if (error)
4318*4882a593Smuzhiyun mlog_errno(error);
4319*4882a593Smuzhiyun }
4320*4882a593Smuzhiyun ocfs2_inode_unlock_tracker(new_orphan_inode, 1, &oh, had_lock);
4321*4882a593Smuzhiyun
4322*4882a593Smuzhiyun out:
4323*4882a593Smuzhiyun if (new_orphan_inode) {
4324*4882a593Smuzhiyun /*
4325*4882a593Smuzhiyun * We need to open_unlock the inode no matter whether we
4326*4882a593Smuzhiyun * succeed or not, so that other nodes can delete it later.
4327*4882a593Smuzhiyun */
4328*4882a593Smuzhiyun ocfs2_open_unlock(new_orphan_inode);
4329*4882a593Smuzhiyun if (error)
4330*4882a593Smuzhiyun iput(new_orphan_inode);
4331*4882a593Smuzhiyun }
4332*4882a593Smuzhiyun
4333*4882a593Smuzhiyun return error;
4334*4882a593Smuzhiyun }
4335*4882a593Smuzhiyun
4336*4882a593Smuzhiyun /*
4337*4882a593Smuzhiyun * Below here are the bits used by OCFS2_IOC_REFLINK() to fake
4338*4882a593Smuzhiyun * sys_reflink(). This will go away when vfs_reflink() exists in
4339*4882a593Smuzhiyun * fs/namei.c.
4340*4882a593Smuzhiyun */
4341*4882a593Smuzhiyun
4342*4882a593Smuzhiyun /* copied from may_create in VFS. */
ocfs2_may_create(struct inode * dir,struct dentry * child)4343*4882a593Smuzhiyun static inline int ocfs2_may_create(struct inode *dir, struct dentry *child)
4344*4882a593Smuzhiyun {
4345*4882a593Smuzhiyun if (d_really_is_positive(child))
4346*4882a593Smuzhiyun return -EEXIST;
4347*4882a593Smuzhiyun if (IS_DEADDIR(dir))
4348*4882a593Smuzhiyun return -ENOENT;
4349*4882a593Smuzhiyun return inode_permission(dir, MAY_WRITE | MAY_EXEC);
4350*4882a593Smuzhiyun }
4351*4882a593Smuzhiyun
4352*4882a593Smuzhiyun /**
4353*4882a593Smuzhiyun * ocfs2_vfs_reflink - Create a reference-counted link
4354*4882a593Smuzhiyun *
4355*4882a593Smuzhiyun * @old_dentry: source dentry + inode
4356*4882a593Smuzhiyun * @dir: directory to create the target
4357*4882a593Smuzhiyun * @new_dentry: target dentry
4358*4882a593Smuzhiyun * @preserve: if true, preserve all file attributes
4359*4882a593Smuzhiyun */
ocfs2_vfs_reflink(struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry,bool preserve)4360*4882a593Smuzhiyun static int ocfs2_vfs_reflink(struct dentry *old_dentry, struct inode *dir,
4361*4882a593Smuzhiyun struct dentry *new_dentry, bool preserve)
4362*4882a593Smuzhiyun {
4363*4882a593Smuzhiyun struct inode *inode = d_inode(old_dentry);
4364*4882a593Smuzhiyun int error;
4365*4882a593Smuzhiyun
4366*4882a593Smuzhiyun if (!inode)
4367*4882a593Smuzhiyun return -ENOENT;
4368*4882a593Smuzhiyun
4369*4882a593Smuzhiyun error = ocfs2_may_create(dir, new_dentry);
4370*4882a593Smuzhiyun if (error)
4371*4882a593Smuzhiyun return error;
4372*4882a593Smuzhiyun
4373*4882a593Smuzhiyun if (dir->i_sb != inode->i_sb)
4374*4882a593Smuzhiyun return -EXDEV;
4375*4882a593Smuzhiyun
4376*4882a593Smuzhiyun /*
4377*4882a593Smuzhiyun * A reflink to an append-only or immutable file cannot be created.
4378*4882a593Smuzhiyun */
4379*4882a593Smuzhiyun if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4380*4882a593Smuzhiyun return -EPERM;
4381*4882a593Smuzhiyun
4382*4882a593Smuzhiyun /* Only regular files can be reflinked. */
4383*4882a593Smuzhiyun if (!S_ISREG(inode->i_mode))
4384*4882a593Smuzhiyun return -EPERM;
4385*4882a593Smuzhiyun
4386*4882a593Smuzhiyun /*
4387*4882a593Smuzhiyun * If the caller wants to preserve ownership, they require the
4388*4882a593Smuzhiyun * rights to do so.
4389*4882a593Smuzhiyun */
4390*4882a593Smuzhiyun if (preserve) {
4391*4882a593Smuzhiyun if (!uid_eq(current_fsuid(), inode->i_uid) && !capable(CAP_CHOWN))
4392*4882a593Smuzhiyun return -EPERM;
4393*4882a593Smuzhiyun if (!in_group_p(inode->i_gid) && !capable(CAP_CHOWN))
4394*4882a593Smuzhiyun return -EPERM;
4395*4882a593Smuzhiyun }
4396*4882a593Smuzhiyun
4397*4882a593Smuzhiyun /*
4398*4882a593Smuzhiyun * If the caller is modifying any aspect of the attributes, they
4399*4882a593Smuzhiyun * are not creating a snapshot. They need read permission on the
4400*4882a593Smuzhiyun * file.
4401*4882a593Smuzhiyun */
4402*4882a593Smuzhiyun if (!preserve) {
4403*4882a593Smuzhiyun error = inode_permission(inode, MAY_READ);
4404*4882a593Smuzhiyun if (error)
4405*4882a593Smuzhiyun return error;
4406*4882a593Smuzhiyun }
4407*4882a593Smuzhiyun
4408*4882a593Smuzhiyun inode_lock(inode);
4409*4882a593Smuzhiyun error = dquot_initialize(dir);
4410*4882a593Smuzhiyun if (!error)
4411*4882a593Smuzhiyun error = ocfs2_reflink(old_dentry, dir, new_dentry, preserve);
4412*4882a593Smuzhiyun inode_unlock(inode);
4413*4882a593Smuzhiyun if (!error)
4414*4882a593Smuzhiyun fsnotify_create(dir, new_dentry);
4415*4882a593Smuzhiyun return error;
4416*4882a593Smuzhiyun }
4417*4882a593Smuzhiyun /*
4418*4882a593Smuzhiyun * Most codes are copied from sys_linkat.
4419*4882a593Smuzhiyun */
ocfs2_reflink_ioctl(struct inode * inode,const char __user * oldname,const char __user * newname,bool preserve)4420*4882a593Smuzhiyun int ocfs2_reflink_ioctl(struct inode *inode,
4421*4882a593Smuzhiyun const char __user *oldname,
4422*4882a593Smuzhiyun const char __user *newname,
4423*4882a593Smuzhiyun bool preserve)
4424*4882a593Smuzhiyun {
4425*4882a593Smuzhiyun struct dentry *new_dentry;
4426*4882a593Smuzhiyun struct path old_path, new_path;
4427*4882a593Smuzhiyun int error;
4428*4882a593Smuzhiyun
4429*4882a593Smuzhiyun if (!ocfs2_refcount_tree(OCFS2_SB(inode->i_sb)))
4430*4882a593Smuzhiyun return -EOPNOTSUPP;
4431*4882a593Smuzhiyun
4432*4882a593Smuzhiyun error = user_path_at(AT_FDCWD, oldname, 0, &old_path);
4433*4882a593Smuzhiyun if (error) {
4434*4882a593Smuzhiyun mlog_errno(error);
4435*4882a593Smuzhiyun return error;
4436*4882a593Smuzhiyun }
4437*4882a593Smuzhiyun
4438*4882a593Smuzhiyun new_dentry = user_path_create(AT_FDCWD, newname, &new_path, 0);
4439*4882a593Smuzhiyun error = PTR_ERR(new_dentry);
4440*4882a593Smuzhiyun if (IS_ERR(new_dentry)) {
4441*4882a593Smuzhiyun mlog_errno(error);
4442*4882a593Smuzhiyun goto out;
4443*4882a593Smuzhiyun }
4444*4882a593Smuzhiyun
4445*4882a593Smuzhiyun error = -EXDEV;
4446*4882a593Smuzhiyun if (old_path.mnt != new_path.mnt) {
4447*4882a593Smuzhiyun mlog_errno(error);
4448*4882a593Smuzhiyun goto out_dput;
4449*4882a593Smuzhiyun }
4450*4882a593Smuzhiyun
4451*4882a593Smuzhiyun error = ocfs2_vfs_reflink(old_path.dentry,
4452*4882a593Smuzhiyun d_inode(new_path.dentry),
4453*4882a593Smuzhiyun new_dentry, preserve);
4454*4882a593Smuzhiyun out_dput:
4455*4882a593Smuzhiyun done_path_create(&new_path, new_dentry);
4456*4882a593Smuzhiyun out:
4457*4882a593Smuzhiyun path_put(&old_path);
4458*4882a593Smuzhiyun
4459*4882a593Smuzhiyun return error;
4460*4882a593Smuzhiyun }
4461*4882a593Smuzhiyun
4462*4882a593Smuzhiyun /* Update destination inode size, if necessary. */
ocfs2_reflink_update_dest(struct inode * dest,struct buffer_head * d_bh,loff_t newlen)4463*4882a593Smuzhiyun int ocfs2_reflink_update_dest(struct inode *dest,
4464*4882a593Smuzhiyun struct buffer_head *d_bh,
4465*4882a593Smuzhiyun loff_t newlen)
4466*4882a593Smuzhiyun {
4467*4882a593Smuzhiyun handle_t *handle;
4468*4882a593Smuzhiyun int ret;
4469*4882a593Smuzhiyun
4470*4882a593Smuzhiyun dest->i_blocks = ocfs2_inode_sector_count(dest);
4471*4882a593Smuzhiyun
4472*4882a593Smuzhiyun if (newlen <= i_size_read(dest))
4473*4882a593Smuzhiyun return 0;
4474*4882a593Smuzhiyun
4475*4882a593Smuzhiyun handle = ocfs2_start_trans(OCFS2_SB(dest->i_sb),
4476*4882a593Smuzhiyun OCFS2_INODE_UPDATE_CREDITS);
4477*4882a593Smuzhiyun if (IS_ERR(handle)) {
4478*4882a593Smuzhiyun ret = PTR_ERR(handle);
4479*4882a593Smuzhiyun mlog_errno(ret);
4480*4882a593Smuzhiyun return ret;
4481*4882a593Smuzhiyun }
4482*4882a593Smuzhiyun
4483*4882a593Smuzhiyun /* Extend i_size if needed. */
4484*4882a593Smuzhiyun spin_lock(&OCFS2_I(dest)->ip_lock);
4485*4882a593Smuzhiyun if (newlen > i_size_read(dest))
4486*4882a593Smuzhiyun i_size_write(dest, newlen);
4487*4882a593Smuzhiyun spin_unlock(&OCFS2_I(dest)->ip_lock);
4488*4882a593Smuzhiyun dest->i_ctime = dest->i_mtime = current_time(dest);
4489*4882a593Smuzhiyun
4490*4882a593Smuzhiyun ret = ocfs2_mark_inode_dirty(handle, dest, d_bh);
4491*4882a593Smuzhiyun if (ret) {
4492*4882a593Smuzhiyun mlog_errno(ret);
4493*4882a593Smuzhiyun goto out_commit;
4494*4882a593Smuzhiyun }
4495*4882a593Smuzhiyun
4496*4882a593Smuzhiyun out_commit:
4497*4882a593Smuzhiyun ocfs2_commit_trans(OCFS2_SB(dest->i_sb), handle);
4498*4882a593Smuzhiyun return ret;
4499*4882a593Smuzhiyun }
4500*4882a593Smuzhiyun
4501*4882a593Smuzhiyun /* Remap the range pos_in:len in s_inode to pos_out:len in t_inode. */
ocfs2_reflink_remap_extent(struct inode * s_inode,struct buffer_head * s_bh,loff_t pos_in,struct inode * t_inode,struct buffer_head * t_bh,loff_t pos_out,loff_t len,struct ocfs2_cached_dealloc_ctxt * dealloc)4502*4882a593Smuzhiyun static loff_t ocfs2_reflink_remap_extent(struct inode *s_inode,
4503*4882a593Smuzhiyun struct buffer_head *s_bh,
4504*4882a593Smuzhiyun loff_t pos_in,
4505*4882a593Smuzhiyun struct inode *t_inode,
4506*4882a593Smuzhiyun struct buffer_head *t_bh,
4507*4882a593Smuzhiyun loff_t pos_out,
4508*4882a593Smuzhiyun loff_t len,
4509*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt *dealloc)
4510*4882a593Smuzhiyun {
4511*4882a593Smuzhiyun struct ocfs2_extent_tree s_et;
4512*4882a593Smuzhiyun struct ocfs2_extent_tree t_et;
4513*4882a593Smuzhiyun struct ocfs2_dinode *dis;
4514*4882a593Smuzhiyun struct buffer_head *ref_root_bh = NULL;
4515*4882a593Smuzhiyun struct ocfs2_refcount_tree *ref_tree;
4516*4882a593Smuzhiyun struct ocfs2_super *osb;
4517*4882a593Smuzhiyun loff_t remapped_bytes = 0;
4518*4882a593Smuzhiyun loff_t pstart, plen;
4519*4882a593Smuzhiyun u32 p_cluster, num_clusters, slast, spos, tpos, remapped_clus = 0;
4520*4882a593Smuzhiyun unsigned int ext_flags;
4521*4882a593Smuzhiyun int ret = 0;
4522*4882a593Smuzhiyun
4523*4882a593Smuzhiyun osb = OCFS2_SB(s_inode->i_sb);
4524*4882a593Smuzhiyun dis = (struct ocfs2_dinode *)s_bh->b_data;
4525*4882a593Smuzhiyun ocfs2_init_dinode_extent_tree(&s_et, INODE_CACHE(s_inode), s_bh);
4526*4882a593Smuzhiyun ocfs2_init_dinode_extent_tree(&t_et, INODE_CACHE(t_inode), t_bh);
4527*4882a593Smuzhiyun
4528*4882a593Smuzhiyun spos = ocfs2_bytes_to_clusters(s_inode->i_sb, pos_in);
4529*4882a593Smuzhiyun tpos = ocfs2_bytes_to_clusters(t_inode->i_sb, pos_out);
4530*4882a593Smuzhiyun slast = ocfs2_clusters_for_bytes(s_inode->i_sb, pos_in + len);
4531*4882a593Smuzhiyun
4532*4882a593Smuzhiyun while (spos < slast) {
4533*4882a593Smuzhiyun if (fatal_signal_pending(current)) {
4534*4882a593Smuzhiyun ret = -EINTR;
4535*4882a593Smuzhiyun goto out;
4536*4882a593Smuzhiyun }
4537*4882a593Smuzhiyun
4538*4882a593Smuzhiyun /* Look up the extent. */
4539*4882a593Smuzhiyun ret = ocfs2_get_clusters(s_inode, spos, &p_cluster,
4540*4882a593Smuzhiyun &num_clusters, &ext_flags);
4541*4882a593Smuzhiyun if (ret) {
4542*4882a593Smuzhiyun mlog_errno(ret);
4543*4882a593Smuzhiyun goto out;
4544*4882a593Smuzhiyun }
4545*4882a593Smuzhiyun
4546*4882a593Smuzhiyun num_clusters = min_t(u32, num_clusters, slast - spos);
4547*4882a593Smuzhiyun
4548*4882a593Smuzhiyun /* Punch out the dest range. */
4549*4882a593Smuzhiyun pstart = ocfs2_clusters_to_bytes(t_inode->i_sb, tpos);
4550*4882a593Smuzhiyun plen = ocfs2_clusters_to_bytes(t_inode->i_sb, num_clusters);
4551*4882a593Smuzhiyun ret = ocfs2_remove_inode_range(t_inode, t_bh, pstart, plen);
4552*4882a593Smuzhiyun if (ret) {
4553*4882a593Smuzhiyun mlog_errno(ret);
4554*4882a593Smuzhiyun goto out;
4555*4882a593Smuzhiyun }
4556*4882a593Smuzhiyun
4557*4882a593Smuzhiyun if (p_cluster == 0)
4558*4882a593Smuzhiyun goto next_loop;
4559*4882a593Smuzhiyun
4560*4882a593Smuzhiyun /* Lock the refcount btree... */
4561*4882a593Smuzhiyun ret = ocfs2_lock_refcount_tree(osb,
4562*4882a593Smuzhiyun le64_to_cpu(dis->i_refcount_loc),
4563*4882a593Smuzhiyun 1, &ref_tree, &ref_root_bh);
4564*4882a593Smuzhiyun if (ret) {
4565*4882a593Smuzhiyun mlog_errno(ret);
4566*4882a593Smuzhiyun goto out;
4567*4882a593Smuzhiyun }
4568*4882a593Smuzhiyun
4569*4882a593Smuzhiyun /* Mark s_inode's extent as refcounted. */
4570*4882a593Smuzhiyun if (!(ext_flags & OCFS2_EXT_REFCOUNTED)) {
4571*4882a593Smuzhiyun ret = ocfs2_add_refcount_flag(s_inode, &s_et,
4572*4882a593Smuzhiyun &ref_tree->rf_ci,
4573*4882a593Smuzhiyun ref_root_bh, spos,
4574*4882a593Smuzhiyun p_cluster, num_clusters,
4575*4882a593Smuzhiyun dealloc, NULL);
4576*4882a593Smuzhiyun if (ret) {
4577*4882a593Smuzhiyun mlog_errno(ret);
4578*4882a593Smuzhiyun goto out_unlock_refcount;
4579*4882a593Smuzhiyun }
4580*4882a593Smuzhiyun }
4581*4882a593Smuzhiyun
4582*4882a593Smuzhiyun /* Map in the new extent. */
4583*4882a593Smuzhiyun ext_flags |= OCFS2_EXT_REFCOUNTED;
4584*4882a593Smuzhiyun ret = ocfs2_add_refcounted_extent(t_inode, &t_et,
4585*4882a593Smuzhiyun &ref_tree->rf_ci,
4586*4882a593Smuzhiyun ref_root_bh,
4587*4882a593Smuzhiyun tpos, p_cluster,
4588*4882a593Smuzhiyun num_clusters,
4589*4882a593Smuzhiyun ext_flags,
4590*4882a593Smuzhiyun dealloc);
4591*4882a593Smuzhiyun if (ret) {
4592*4882a593Smuzhiyun mlog_errno(ret);
4593*4882a593Smuzhiyun goto out_unlock_refcount;
4594*4882a593Smuzhiyun }
4595*4882a593Smuzhiyun
4596*4882a593Smuzhiyun ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
4597*4882a593Smuzhiyun brelse(ref_root_bh);
4598*4882a593Smuzhiyun next_loop:
4599*4882a593Smuzhiyun spos += num_clusters;
4600*4882a593Smuzhiyun tpos += num_clusters;
4601*4882a593Smuzhiyun remapped_clus += num_clusters;
4602*4882a593Smuzhiyun }
4603*4882a593Smuzhiyun
4604*4882a593Smuzhiyun goto out;
4605*4882a593Smuzhiyun out_unlock_refcount:
4606*4882a593Smuzhiyun ocfs2_unlock_refcount_tree(osb, ref_tree, 1);
4607*4882a593Smuzhiyun brelse(ref_root_bh);
4608*4882a593Smuzhiyun out:
4609*4882a593Smuzhiyun remapped_bytes = ocfs2_clusters_to_bytes(t_inode->i_sb, remapped_clus);
4610*4882a593Smuzhiyun remapped_bytes = min_t(loff_t, len, remapped_bytes);
4611*4882a593Smuzhiyun
4612*4882a593Smuzhiyun return remapped_bytes > 0 ? remapped_bytes : ret;
4613*4882a593Smuzhiyun }
4614*4882a593Smuzhiyun
4615*4882a593Smuzhiyun /* Set up refcount tree and remap s_inode to t_inode. */
ocfs2_reflink_remap_blocks(struct inode * s_inode,struct buffer_head * s_bh,loff_t pos_in,struct inode * t_inode,struct buffer_head * t_bh,loff_t pos_out,loff_t len)4616*4882a593Smuzhiyun loff_t ocfs2_reflink_remap_blocks(struct inode *s_inode,
4617*4882a593Smuzhiyun struct buffer_head *s_bh,
4618*4882a593Smuzhiyun loff_t pos_in,
4619*4882a593Smuzhiyun struct inode *t_inode,
4620*4882a593Smuzhiyun struct buffer_head *t_bh,
4621*4882a593Smuzhiyun loff_t pos_out,
4622*4882a593Smuzhiyun loff_t len)
4623*4882a593Smuzhiyun {
4624*4882a593Smuzhiyun struct ocfs2_cached_dealloc_ctxt dealloc;
4625*4882a593Smuzhiyun struct ocfs2_super *osb;
4626*4882a593Smuzhiyun struct ocfs2_dinode *dis;
4627*4882a593Smuzhiyun struct ocfs2_dinode *dit;
4628*4882a593Smuzhiyun loff_t ret;
4629*4882a593Smuzhiyun
4630*4882a593Smuzhiyun osb = OCFS2_SB(s_inode->i_sb);
4631*4882a593Smuzhiyun dis = (struct ocfs2_dinode *)s_bh->b_data;
4632*4882a593Smuzhiyun dit = (struct ocfs2_dinode *)t_bh->b_data;
4633*4882a593Smuzhiyun ocfs2_init_dealloc_ctxt(&dealloc);
4634*4882a593Smuzhiyun
4635*4882a593Smuzhiyun /*
4636*4882a593Smuzhiyun * If we're reflinking the entire file and the source is inline
4637*4882a593Smuzhiyun * data, just copy the contents.
4638*4882a593Smuzhiyun */
4639*4882a593Smuzhiyun if (pos_in == pos_out && pos_in == 0 && len == i_size_read(s_inode) &&
4640*4882a593Smuzhiyun i_size_read(t_inode) <= len &&
4641*4882a593Smuzhiyun (OCFS2_I(s_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)) {
4642*4882a593Smuzhiyun ret = ocfs2_duplicate_inline_data(s_inode, s_bh, t_inode, t_bh);
4643*4882a593Smuzhiyun if (ret)
4644*4882a593Smuzhiyun mlog_errno(ret);
4645*4882a593Smuzhiyun goto out;
4646*4882a593Smuzhiyun }
4647*4882a593Smuzhiyun
4648*4882a593Smuzhiyun /*
4649*4882a593Smuzhiyun * If both inodes belong to two different refcount groups then
4650*4882a593Smuzhiyun * forget it because we don't know how (or want) to go merging
4651*4882a593Smuzhiyun * refcount trees.
4652*4882a593Smuzhiyun */
4653*4882a593Smuzhiyun ret = -EOPNOTSUPP;
4654*4882a593Smuzhiyun if (ocfs2_is_refcount_inode(s_inode) &&
4655*4882a593Smuzhiyun ocfs2_is_refcount_inode(t_inode) &&
4656*4882a593Smuzhiyun le64_to_cpu(dis->i_refcount_loc) !=
4657*4882a593Smuzhiyun le64_to_cpu(dit->i_refcount_loc))
4658*4882a593Smuzhiyun goto out;
4659*4882a593Smuzhiyun
4660*4882a593Smuzhiyun /* Neither inode has a refcount tree. Add one to s_inode. */
4661*4882a593Smuzhiyun if (!ocfs2_is_refcount_inode(s_inode) &&
4662*4882a593Smuzhiyun !ocfs2_is_refcount_inode(t_inode)) {
4663*4882a593Smuzhiyun ret = ocfs2_create_refcount_tree(s_inode, s_bh);
4664*4882a593Smuzhiyun if (ret) {
4665*4882a593Smuzhiyun mlog_errno(ret);
4666*4882a593Smuzhiyun goto out;
4667*4882a593Smuzhiyun }
4668*4882a593Smuzhiyun }
4669*4882a593Smuzhiyun
4670*4882a593Smuzhiyun /* Ensure that both inodes end up with the same refcount tree. */
4671*4882a593Smuzhiyun if (!ocfs2_is_refcount_inode(s_inode)) {
4672*4882a593Smuzhiyun ret = ocfs2_set_refcount_tree(s_inode, s_bh,
4673*4882a593Smuzhiyun le64_to_cpu(dit->i_refcount_loc));
4674*4882a593Smuzhiyun if (ret) {
4675*4882a593Smuzhiyun mlog_errno(ret);
4676*4882a593Smuzhiyun goto out;
4677*4882a593Smuzhiyun }
4678*4882a593Smuzhiyun }
4679*4882a593Smuzhiyun if (!ocfs2_is_refcount_inode(t_inode)) {
4680*4882a593Smuzhiyun ret = ocfs2_set_refcount_tree(t_inode, t_bh,
4681*4882a593Smuzhiyun le64_to_cpu(dis->i_refcount_loc));
4682*4882a593Smuzhiyun if (ret) {
4683*4882a593Smuzhiyun mlog_errno(ret);
4684*4882a593Smuzhiyun goto out;
4685*4882a593Smuzhiyun }
4686*4882a593Smuzhiyun }
4687*4882a593Smuzhiyun
4688*4882a593Smuzhiyun /* Turn off inline data in the dest file. */
4689*4882a593Smuzhiyun if (OCFS2_I(t_inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
4690*4882a593Smuzhiyun ret = ocfs2_convert_inline_data_to_extents(t_inode, t_bh);
4691*4882a593Smuzhiyun if (ret) {
4692*4882a593Smuzhiyun mlog_errno(ret);
4693*4882a593Smuzhiyun goto out;
4694*4882a593Smuzhiyun }
4695*4882a593Smuzhiyun }
4696*4882a593Smuzhiyun
4697*4882a593Smuzhiyun /* Actually remap extents now. */
4698*4882a593Smuzhiyun ret = ocfs2_reflink_remap_extent(s_inode, s_bh, pos_in, t_inode, t_bh,
4699*4882a593Smuzhiyun pos_out, len, &dealloc);
4700*4882a593Smuzhiyun if (ret < 0) {
4701*4882a593Smuzhiyun mlog_errno(ret);
4702*4882a593Smuzhiyun goto out;
4703*4882a593Smuzhiyun }
4704*4882a593Smuzhiyun
4705*4882a593Smuzhiyun out:
4706*4882a593Smuzhiyun if (ocfs2_dealloc_has_cluster(&dealloc)) {
4707*4882a593Smuzhiyun ocfs2_schedule_truncate_log_flush(osb, 1);
4708*4882a593Smuzhiyun ocfs2_run_deallocs(osb, &dealloc);
4709*4882a593Smuzhiyun }
4710*4882a593Smuzhiyun
4711*4882a593Smuzhiyun return ret;
4712*4882a593Smuzhiyun }
4713*4882a593Smuzhiyun
4714*4882a593Smuzhiyun /* Lock an inode and grab a bh pointing to the inode. */
ocfs2_reflink_inodes_lock(struct inode * s_inode,struct buffer_head ** bh_s,struct inode * t_inode,struct buffer_head ** bh_t)4715*4882a593Smuzhiyun int ocfs2_reflink_inodes_lock(struct inode *s_inode,
4716*4882a593Smuzhiyun struct buffer_head **bh_s,
4717*4882a593Smuzhiyun struct inode *t_inode,
4718*4882a593Smuzhiyun struct buffer_head **bh_t)
4719*4882a593Smuzhiyun {
4720*4882a593Smuzhiyun struct inode *inode1 = s_inode;
4721*4882a593Smuzhiyun struct inode *inode2 = t_inode;
4722*4882a593Smuzhiyun struct ocfs2_inode_info *oi1;
4723*4882a593Smuzhiyun struct ocfs2_inode_info *oi2;
4724*4882a593Smuzhiyun struct buffer_head *bh1 = NULL;
4725*4882a593Smuzhiyun struct buffer_head *bh2 = NULL;
4726*4882a593Smuzhiyun bool same_inode = (s_inode == t_inode);
4727*4882a593Smuzhiyun bool need_swap = (inode1->i_ino > inode2->i_ino);
4728*4882a593Smuzhiyun int status;
4729*4882a593Smuzhiyun
4730*4882a593Smuzhiyun /* First grab the VFS and rw locks. */
4731*4882a593Smuzhiyun lock_two_nondirectories(s_inode, t_inode);
4732*4882a593Smuzhiyun if (need_swap)
4733*4882a593Smuzhiyun swap(inode1, inode2);
4734*4882a593Smuzhiyun
4735*4882a593Smuzhiyun status = ocfs2_rw_lock(inode1, 1);
4736*4882a593Smuzhiyun if (status) {
4737*4882a593Smuzhiyun mlog_errno(status);
4738*4882a593Smuzhiyun goto out_i1;
4739*4882a593Smuzhiyun }
4740*4882a593Smuzhiyun if (!same_inode) {
4741*4882a593Smuzhiyun status = ocfs2_rw_lock(inode2, 1);
4742*4882a593Smuzhiyun if (status) {
4743*4882a593Smuzhiyun mlog_errno(status);
4744*4882a593Smuzhiyun goto out_i2;
4745*4882a593Smuzhiyun }
4746*4882a593Smuzhiyun }
4747*4882a593Smuzhiyun
4748*4882a593Smuzhiyun /* Now go for the cluster locks */
4749*4882a593Smuzhiyun oi1 = OCFS2_I(inode1);
4750*4882a593Smuzhiyun oi2 = OCFS2_I(inode2);
4751*4882a593Smuzhiyun
4752*4882a593Smuzhiyun trace_ocfs2_double_lock((unsigned long long)oi1->ip_blkno,
4753*4882a593Smuzhiyun (unsigned long long)oi2->ip_blkno);
4754*4882a593Smuzhiyun
4755*4882a593Smuzhiyun /* We always want to lock the one with the lower lockid first. */
4756*4882a593Smuzhiyun if (oi1->ip_blkno > oi2->ip_blkno)
4757*4882a593Smuzhiyun mlog_errno(-ENOLCK);
4758*4882a593Smuzhiyun
4759*4882a593Smuzhiyun /* lock id1 */
4760*4882a593Smuzhiyun status = ocfs2_inode_lock_nested(inode1, &bh1, 1,
4761*4882a593Smuzhiyun OI_LS_REFLINK_TARGET);
4762*4882a593Smuzhiyun if (status < 0) {
4763*4882a593Smuzhiyun if (status != -ENOENT)
4764*4882a593Smuzhiyun mlog_errno(status);
4765*4882a593Smuzhiyun goto out_rw2;
4766*4882a593Smuzhiyun }
4767*4882a593Smuzhiyun
4768*4882a593Smuzhiyun /* lock id2 */
4769*4882a593Smuzhiyun if (!same_inode) {
4770*4882a593Smuzhiyun status = ocfs2_inode_lock_nested(inode2, &bh2, 1,
4771*4882a593Smuzhiyun OI_LS_REFLINK_TARGET);
4772*4882a593Smuzhiyun if (status < 0) {
4773*4882a593Smuzhiyun if (status != -ENOENT)
4774*4882a593Smuzhiyun mlog_errno(status);
4775*4882a593Smuzhiyun goto out_cl1;
4776*4882a593Smuzhiyun }
4777*4882a593Smuzhiyun } else {
4778*4882a593Smuzhiyun bh2 = bh1;
4779*4882a593Smuzhiyun }
4780*4882a593Smuzhiyun
4781*4882a593Smuzhiyun /*
4782*4882a593Smuzhiyun * If we swapped inode order above, we have to swap the buffer heads
4783*4882a593Smuzhiyun * before passing them back to the caller.
4784*4882a593Smuzhiyun */
4785*4882a593Smuzhiyun if (need_swap)
4786*4882a593Smuzhiyun swap(bh1, bh2);
4787*4882a593Smuzhiyun *bh_s = bh1;
4788*4882a593Smuzhiyun *bh_t = bh2;
4789*4882a593Smuzhiyun
4790*4882a593Smuzhiyun trace_ocfs2_double_lock_end(
4791*4882a593Smuzhiyun (unsigned long long)oi1->ip_blkno,
4792*4882a593Smuzhiyun (unsigned long long)oi2->ip_blkno);
4793*4882a593Smuzhiyun
4794*4882a593Smuzhiyun return 0;
4795*4882a593Smuzhiyun
4796*4882a593Smuzhiyun out_cl1:
4797*4882a593Smuzhiyun ocfs2_inode_unlock(inode1, 1);
4798*4882a593Smuzhiyun brelse(bh1);
4799*4882a593Smuzhiyun out_rw2:
4800*4882a593Smuzhiyun ocfs2_rw_unlock(inode2, 1);
4801*4882a593Smuzhiyun out_i2:
4802*4882a593Smuzhiyun ocfs2_rw_unlock(inode1, 1);
4803*4882a593Smuzhiyun out_i1:
4804*4882a593Smuzhiyun unlock_two_nondirectories(s_inode, t_inode);
4805*4882a593Smuzhiyun return status;
4806*4882a593Smuzhiyun }
4807*4882a593Smuzhiyun
4808*4882a593Smuzhiyun /* Unlock both inodes and release buffers. */
ocfs2_reflink_inodes_unlock(struct inode * s_inode,struct buffer_head * s_bh,struct inode * t_inode,struct buffer_head * t_bh)4809*4882a593Smuzhiyun void ocfs2_reflink_inodes_unlock(struct inode *s_inode,
4810*4882a593Smuzhiyun struct buffer_head *s_bh,
4811*4882a593Smuzhiyun struct inode *t_inode,
4812*4882a593Smuzhiyun struct buffer_head *t_bh)
4813*4882a593Smuzhiyun {
4814*4882a593Smuzhiyun ocfs2_inode_unlock(s_inode, 1);
4815*4882a593Smuzhiyun ocfs2_rw_unlock(s_inode, 1);
4816*4882a593Smuzhiyun brelse(s_bh);
4817*4882a593Smuzhiyun if (s_inode != t_inode) {
4818*4882a593Smuzhiyun ocfs2_inode_unlock(t_inode, 1);
4819*4882a593Smuzhiyun ocfs2_rw_unlock(t_inode, 1);
4820*4882a593Smuzhiyun brelse(t_bh);
4821*4882a593Smuzhiyun }
4822*4882a593Smuzhiyun unlock_two_nondirectories(s_inode, t_inode);
4823*4882a593Smuzhiyun }
4824