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