~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/ocfs2/refcounttree.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php