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