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