projects/linux·blob master · refs

4834 lines · 121.5 KiB

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