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