2 * the_nilfs.c - the_nilfs shared structure.
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Written by Ryusuke Konishi <ryusuke@osrg.net>
24 #include <linux/buffer_head.h>
25 #include <linux/slab.h>
26 #include <linux/blkdev.h>
27 #include <linux/backing-dev.h>
28 #include <linux/crc32.h>
38 static int nilfs_valid_sb(struct nilfs_super_block
*sbp
);
40 void nilfs_set_last_segment(struct the_nilfs
*nilfs
,
41 sector_t start_blocknr
, u64 seq
, __u64 cno
)
43 spin_lock(&nilfs
->ns_last_segment_lock
);
44 nilfs
->ns_last_pseg
= start_blocknr
;
45 nilfs
->ns_last_seq
= seq
;
46 nilfs
->ns_last_cno
= cno
;
48 if (!nilfs_sb_dirty(nilfs
)) {
49 if (nilfs
->ns_prev_seq
== nilfs
->ns_last_seq
)
52 set_nilfs_sb_dirty(nilfs
);
54 nilfs
->ns_prev_seq
= nilfs
->ns_last_seq
;
57 spin_unlock(&nilfs
->ns_last_segment_lock
);
61 * alloc_nilfs - allocate a nilfs object
62 * @bdev: block device to which the_nilfs is related
64 * Return Value: On success, pointer to the_nilfs is returned.
65 * On error, NULL is returned.
67 struct the_nilfs
*alloc_nilfs(struct block_device
*bdev
)
69 struct the_nilfs
*nilfs
;
71 nilfs
= kzalloc(sizeof(*nilfs
), GFP_KERNEL
);
75 nilfs
->ns_bdev
= bdev
;
76 atomic_set(&nilfs
->ns_ndirtyblks
, 0);
77 init_rwsem(&nilfs
->ns_sem
);
78 init_rwsem(&nilfs
->ns_writer_sem
);
79 INIT_LIST_HEAD(&nilfs
->ns_gc_inodes
);
80 spin_lock_init(&nilfs
->ns_last_segment_lock
);
81 nilfs
->ns_cptree
= RB_ROOT
;
82 spin_lock_init(&nilfs
->ns_cptree_lock
);
83 init_rwsem(&nilfs
->ns_segctor_sem
);
89 * destroy_nilfs - destroy nilfs object
90 * @nilfs: nilfs object to be released
92 void destroy_nilfs(struct the_nilfs
*nilfs
)
95 if (nilfs_loaded(nilfs
)) {
96 nilfs_mdt_destroy(nilfs
->ns_sufile
);
97 nilfs_mdt_destroy(nilfs
->ns_cpfile
);
98 nilfs_mdt_destroy(nilfs
->ns_dat
);
100 if (nilfs_init(nilfs
)) {
101 brelse(nilfs
->ns_sbh
[0]);
102 brelse(nilfs
->ns_sbh
[1]);
107 static int nilfs_load_super_root(struct the_nilfs
*nilfs
, sector_t sr_block
)
109 struct buffer_head
*bh_sr
;
110 struct nilfs_super_root
*raw_sr
;
111 struct nilfs_super_block
**sbp
= nilfs
->ns_sbp
;
112 unsigned dat_entry_size
, segment_usage_size
, checkpoint_size
;
116 err
= nilfs_read_super_root_block(nilfs
, sr_block
, &bh_sr
, 1);
120 down_read(&nilfs
->ns_sem
);
121 dat_entry_size
= le16_to_cpu(sbp
[0]->s_dat_entry_size
);
122 checkpoint_size
= le16_to_cpu(sbp
[0]->s_checkpoint_size
);
123 segment_usage_size
= le16_to_cpu(sbp
[0]->s_segment_usage_size
);
124 up_read(&nilfs
->ns_sem
);
126 inode_size
= nilfs
->ns_inode_size
;
129 nilfs
->ns_dat
= nilfs_dat_new(nilfs
, dat_entry_size
);
130 if (unlikely(!nilfs
->ns_dat
))
133 nilfs
->ns_cpfile
= nilfs_cpfile_new(nilfs
, checkpoint_size
);
134 if (unlikely(!nilfs
->ns_cpfile
))
137 nilfs
->ns_sufile
= nilfs_sufile_new(nilfs
, segment_usage_size
);
138 if (unlikely(!nilfs
->ns_sufile
))
141 err
= nilfs_dat_read(nilfs
->ns_dat
, (void *)bh_sr
->b_data
+
142 NILFS_SR_DAT_OFFSET(inode_size
));
146 err
= nilfs_cpfile_read(nilfs
->ns_cpfile
, (void *)bh_sr
->b_data
+
147 NILFS_SR_CPFILE_OFFSET(inode_size
));
151 err
= nilfs_sufile_read(nilfs
->ns_sufile
, (void *)bh_sr
->b_data
+
152 NILFS_SR_SUFILE_OFFSET(inode_size
));
156 raw_sr
= (struct nilfs_super_root
*)bh_sr
->b_data
;
157 nilfs
->ns_nongc_ctime
= le64_to_cpu(raw_sr
->sr_nongc_ctime
);
164 nilfs_mdt_destroy(nilfs
->ns_sufile
);
167 nilfs_mdt_destroy(nilfs
->ns_cpfile
);
170 nilfs_mdt_destroy(nilfs
->ns_dat
);
174 static void nilfs_init_recovery_info(struct nilfs_recovery_info
*ri
)
176 memset(ri
, 0, sizeof(*ri
));
177 INIT_LIST_HEAD(&ri
->ri_used_segments
);
180 static void nilfs_clear_recovery_info(struct nilfs_recovery_info
*ri
)
182 nilfs_dispose_segment_list(&ri
->ri_used_segments
);
186 * nilfs_store_log_cursor - load log cursor from a super block
187 * @nilfs: nilfs object
188 * @sbp: buffer storing super block to be read
190 * nilfs_store_log_cursor() reads the last position of the log
191 * containing a super root from a given super block, and initializes
192 * relevant information on the nilfs object preparatory for log
193 * scanning and recovery.
195 static int nilfs_store_log_cursor(struct the_nilfs
*nilfs
,
196 struct nilfs_super_block
*sbp
)
200 nilfs
->ns_last_pseg
= le64_to_cpu(sbp
->s_last_pseg
);
201 nilfs
->ns_last_cno
= le64_to_cpu(sbp
->s_last_cno
);
202 nilfs
->ns_last_seq
= le64_to_cpu(sbp
->s_last_seq
);
204 nilfs
->ns_prev_seq
= nilfs
->ns_last_seq
;
205 nilfs
->ns_seg_seq
= nilfs
->ns_last_seq
;
207 nilfs_get_segnum_of_block(nilfs
, nilfs
->ns_last_pseg
);
208 nilfs
->ns_cno
= nilfs
->ns_last_cno
+ 1;
209 if (nilfs
->ns_segnum
>= nilfs
->ns_nsegments
) {
210 printk(KERN_ERR
"NILFS invalid last segment number.\n");
217 * load_nilfs - load and recover the nilfs
218 * @nilfs: the_nilfs structure to be released
219 * @sbi: nilfs_sb_info used to recover past segment
221 * load_nilfs() searches and load the latest super root,
222 * attaches the last segment, and does recovery if needed.
223 * The caller must call this exclusively for simultaneous mounts.
225 int load_nilfs(struct the_nilfs
*nilfs
, struct nilfs_sb_info
*sbi
)
227 struct nilfs_recovery_info ri
;
228 unsigned int s_flags
= sbi
->s_super
->s_flags
;
229 int really_read_only
= bdev_read_only(nilfs
->ns_bdev
);
230 int valid_fs
= nilfs_valid_fs(nilfs
);
234 printk(KERN_WARNING
"NILFS warning: mounting unchecked fs\n");
235 if (s_flags
& MS_RDONLY
) {
236 printk(KERN_INFO
"NILFS: INFO: recovery "
237 "required for readonly filesystem.\n");
238 printk(KERN_INFO
"NILFS: write access will "
239 "be enabled during recovery.\n");
243 nilfs_init_recovery_info(&ri
);
245 err
= nilfs_search_super_root(nilfs
, &ri
);
247 struct nilfs_super_block
**sbp
= nilfs
->ns_sbp
;
253 if (!nilfs_valid_sb(sbp
[1])) {
255 "NILFS warning: unable to fall back to spare"
260 "NILFS: try rollback from an earlier position\n");
263 * restore super block with its spare and reconfigure
264 * relevant states of the nilfs object.
266 memcpy(sbp
[0], sbp
[1], nilfs
->ns_sbsize
);
267 nilfs
->ns_crc_seed
= le32_to_cpu(sbp
[0]->s_crc_seed
);
268 nilfs
->ns_sbwtime
= le64_to_cpu(sbp
[0]->s_wtime
);
270 /* verify consistency between two super blocks */
271 blocksize
= BLOCK_SIZE
<< le32_to_cpu(sbp
[0]->s_log_block_size
);
272 if (blocksize
!= nilfs
->ns_blocksize
) {
274 "NILFS warning: blocksize differs between "
275 "two super blocks (%d != %d)\n",
276 blocksize
, nilfs
->ns_blocksize
);
280 err
= nilfs_store_log_cursor(nilfs
, sbp
[0]);
284 /* drop clean flag to allow roll-forward and recovery */
285 nilfs
->ns_mount_state
&= ~NILFS_VALID_FS
;
288 err
= nilfs_search_super_root(nilfs
, &ri
);
293 err
= nilfs_load_super_root(nilfs
, ri
.ri_super_root
);
295 printk(KERN_ERR
"NILFS: error loading super root.\n");
302 if (s_flags
& MS_RDONLY
) {
305 if (nilfs_test_opt(sbi
, NORECOVERY
)) {
306 printk(KERN_INFO
"NILFS: norecovery option specified. "
307 "skipping roll-forward recovery\n");
310 features
= le64_to_cpu(nilfs
->ns_sbp
[0]->s_feature_compat_ro
) &
311 ~NILFS_FEATURE_COMPAT_RO_SUPP
;
313 printk(KERN_ERR
"NILFS: couldn't proceed with "
314 "recovery because of unsupported optional "
316 (unsigned long long)features
);
320 if (really_read_only
) {
321 printk(KERN_ERR
"NILFS: write access "
322 "unavailable, cannot proceed.\n");
326 sbi
->s_super
->s_flags
&= ~MS_RDONLY
;
327 } else if (nilfs_test_opt(sbi
, NORECOVERY
)) {
328 printk(KERN_ERR
"NILFS: recovery cancelled because norecovery "
329 "option was specified for a read/write mount\n");
334 err
= nilfs_salvage_orphan_logs(nilfs
, sbi
, &ri
);
338 down_write(&nilfs
->ns_sem
);
339 nilfs
->ns_mount_state
|= NILFS_VALID_FS
; /* set "clean" flag */
340 err
= nilfs_cleanup_super(sbi
);
341 up_write(&nilfs
->ns_sem
);
344 printk(KERN_ERR
"NILFS: failed to update super block. "
345 "recovery unfinished.\n");
348 printk(KERN_INFO
"NILFS: recovery complete.\n");
351 set_nilfs_loaded(nilfs
);
352 nilfs_clear_recovery_info(&ri
);
353 sbi
->s_super
->s_flags
= s_flags
;
357 printk(KERN_ERR
"NILFS: error searching super root.\n");
361 nilfs_mdt_destroy(nilfs
->ns_cpfile
);
362 nilfs_mdt_destroy(nilfs
->ns_sufile
);
363 nilfs_mdt_destroy(nilfs
->ns_dat
);
366 nilfs_clear_recovery_info(&ri
);
367 sbi
->s_super
->s_flags
= s_flags
;
371 static unsigned long long nilfs_max_size(unsigned int blkbits
)
373 unsigned int max_bits
;
374 unsigned long long res
= MAX_LFS_FILESIZE
; /* page cache limit */
376 max_bits
= blkbits
+ NILFS_BMAP_KEY_BIT
; /* bmap size limit */
378 res
= min_t(unsigned long long, res
, (1ULL << max_bits
) - 1);
382 static int nilfs_store_disk_layout(struct the_nilfs
*nilfs
,
383 struct nilfs_super_block
*sbp
)
385 if (le32_to_cpu(sbp
->s_rev_level
) < NILFS_MIN_SUPP_REV
) {
386 printk(KERN_ERR
"NILFS: unsupported revision "
387 "(superblock rev.=%d.%d, current rev.=%d.%d). "
388 "Please check the version of mkfs.nilfs.\n",
389 le32_to_cpu(sbp
->s_rev_level
),
390 le16_to_cpu(sbp
->s_minor_rev_level
),
391 NILFS_CURRENT_REV
, NILFS_MINOR_REV
);
394 nilfs
->ns_sbsize
= le16_to_cpu(sbp
->s_bytes
);
395 if (nilfs
->ns_sbsize
> BLOCK_SIZE
)
398 nilfs
->ns_inode_size
= le16_to_cpu(sbp
->s_inode_size
);
399 nilfs
->ns_first_ino
= le32_to_cpu(sbp
->s_first_ino
);
401 nilfs
->ns_blocks_per_segment
= le32_to_cpu(sbp
->s_blocks_per_segment
);
402 if (nilfs
->ns_blocks_per_segment
< NILFS_SEG_MIN_BLOCKS
) {
403 printk(KERN_ERR
"NILFS: too short segment.\n");
407 nilfs
->ns_first_data_block
= le64_to_cpu(sbp
->s_first_data_block
);
408 nilfs
->ns_nsegments
= le64_to_cpu(sbp
->s_nsegments
);
409 nilfs
->ns_r_segments_percentage
=
410 le32_to_cpu(sbp
->s_r_segments_percentage
);
412 max_t(unsigned long, NILFS_MIN_NRSVSEGS
,
413 DIV_ROUND_UP(nilfs
->ns_nsegments
*
414 nilfs
->ns_r_segments_percentage
, 100));
415 nilfs
->ns_crc_seed
= le32_to_cpu(sbp
->s_crc_seed
);
419 static int nilfs_valid_sb(struct nilfs_super_block
*sbp
)
421 static unsigned char sum
[4];
422 const int sumoff
= offsetof(struct nilfs_super_block
, s_sum
);
426 if (!sbp
|| le16_to_cpu(sbp
->s_magic
) != NILFS_SUPER_MAGIC
)
428 bytes
= le16_to_cpu(sbp
->s_bytes
);
429 if (bytes
> BLOCK_SIZE
)
431 crc
= crc32_le(le32_to_cpu(sbp
->s_crc_seed
), (unsigned char *)sbp
,
433 crc
= crc32_le(crc
, sum
, 4);
434 crc
= crc32_le(crc
, (unsigned char *)sbp
+ sumoff
+ 4,
436 return crc
== le32_to_cpu(sbp
->s_sum
);
439 static int nilfs_sb2_bad_offset(struct nilfs_super_block
*sbp
, u64 offset
)
441 return offset
< ((le64_to_cpu(sbp
->s_nsegments
) *
442 le32_to_cpu(sbp
->s_blocks_per_segment
)) <<
443 (le32_to_cpu(sbp
->s_log_block_size
) + 10));
446 static void nilfs_release_super_block(struct the_nilfs
*nilfs
)
450 for (i
= 0; i
< 2; i
++) {
451 if (nilfs
->ns_sbp
[i
]) {
452 brelse(nilfs
->ns_sbh
[i
]);
453 nilfs
->ns_sbh
[i
] = NULL
;
454 nilfs
->ns_sbp
[i
] = NULL
;
459 void nilfs_fall_back_super_block(struct the_nilfs
*nilfs
)
461 brelse(nilfs
->ns_sbh
[0]);
462 nilfs
->ns_sbh
[0] = nilfs
->ns_sbh
[1];
463 nilfs
->ns_sbp
[0] = nilfs
->ns_sbp
[1];
464 nilfs
->ns_sbh
[1] = NULL
;
465 nilfs
->ns_sbp
[1] = NULL
;
468 void nilfs_swap_super_block(struct the_nilfs
*nilfs
)
470 struct buffer_head
*tsbh
= nilfs
->ns_sbh
[0];
471 struct nilfs_super_block
*tsbp
= nilfs
->ns_sbp
[0];
473 nilfs
->ns_sbh
[0] = nilfs
->ns_sbh
[1];
474 nilfs
->ns_sbp
[0] = nilfs
->ns_sbp
[1];
475 nilfs
->ns_sbh
[1] = tsbh
;
476 nilfs
->ns_sbp
[1] = tsbp
;
479 static int nilfs_load_super_block(struct the_nilfs
*nilfs
,
480 struct super_block
*sb
, int blocksize
,
481 struct nilfs_super_block
**sbpp
)
483 struct nilfs_super_block
**sbp
= nilfs
->ns_sbp
;
484 struct buffer_head
**sbh
= nilfs
->ns_sbh
;
485 u64 sb2off
= NILFS_SB2_OFFSET_BYTES(nilfs
->ns_bdev
->bd_inode
->i_size
);
486 int valid
[2], swp
= 0;
488 sbp
[0] = nilfs_read_super_block(sb
, NILFS_SB_OFFSET_BYTES
, blocksize
,
490 sbp
[1] = nilfs_read_super_block(sb
, sb2off
, blocksize
, &sbh
[1]);
494 printk(KERN_ERR
"NILFS: unable to read superblock\n");
498 "NILFS warning: unable to read primary superblock\n");
501 "NILFS warning: unable to read secondary superblock\n");
504 * Compare two super blocks and set 1 in swp if the secondary
505 * super block is valid and newer. Otherwise, set 0 in swp.
507 valid
[0] = nilfs_valid_sb(sbp
[0]);
508 valid
[1] = nilfs_valid_sb(sbp
[1]);
509 swp
= valid
[1] && (!valid
[0] ||
510 le64_to_cpu(sbp
[1]->s_last_cno
) >
511 le64_to_cpu(sbp
[0]->s_last_cno
));
513 if (valid
[swp
] && nilfs_sb2_bad_offset(sbp
[swp
], sb2off
)) {
520 nilfs_release_super_block(nilfs
);
521 printk(KERN_ERR
"NILFS: Can't find nilfs on dev %s.\n",
527 printk(KERN_WARNING
"NILFS warning: broken superblock. "
528 "using spare superblock.\n");
530 nilfs_swap_super_block(nilfs
);
532 nilfs
->ns_sbwcount
= 0;
533 nilfs
->ns_sbwtime
= le64_to_cpu(sbp
[0]->s_wtime
);
534 nilfs
->ns_prot_seq
= le64_to_cpu(sbp
[valid
[1] & !swp
]->s_last_seq
);
540 * init_nilfs - initialize a NILFS instance.
541 * @nilfs: the_nilfs structure
542 * @sbi: nilfs_sb_info
544 * @data: mount options
546 * init_nilfs() performs common initialization per block device (e.g.
547 * reading the super block, getting disk layout information, initializing
548 * shared fields in the_nilfs).
550 * Return Value: On success, 0 is returned. On error, a negative error
553 int init_nilfs(struct the_nilfs
*nilfs
, struct nilfs_sb_info
*sbi
, char *data
)
555 struct super_block
*sb
= sbi
->s_super
;
556 struct nilfs_super_block
*sbp
;
557 struct backing_dev_info
*bdi
;
561 down_write(&nilfs
->ns_sem
);
563 blocksize
= sb_min_blocksize(sb
, NILFS_MIN_BLOCK_SIZE
);
565 printk(KERN_ERR
"NILFS: unable to set blocksize\n");
569 err
= nilfs_load_super_block(nilfs
, sb
, blocksize
, &sbp
);
573 err
= nilfs_store_magic_and_option(sb
, sbp
, data
);
577 err
= nilfs_check_feature_compatibility(sb
, sbp
);
581 blocksize
= BLOCK_SIZE
<< le32_to_cpu(sbp
->s_log_block_size
);
582 if (blocksize
< NILFS_MIN_BLOCK_SIZE
||
583 blocksize
> NILFS_MAX_BLOCK_SIZE
) {
584 printk(KERN_ERR
"NILFS: couldn't mount because of unsupported "
585 "filesystem blocksize %d\n", blocksize
);
589 if (sb
->s_blocksize
!= blocksize
) {
590 int hw_blocksize
= bdev_logical_block_size(sb
->s_bdev
);
592 if (blocksize
< hw_blocksize
) {
594 "NILFS: blocksize %d too small for device "
595 "(sector-size = %d).\n",
596 blocksize
, hw_blocksize
);
600 nilfs_release_super_block(nilfs
);
601 sb_set_blocksize(sb
, blocksize
);
603 err
= nilfs_load_super_block(nilfs
, sb
, blocksize
, &sbp
);
606 /* not failed_sbh; sbh is released automatically
607 when reloading fails. */
609 nilfs
->ns_blocksize_bits
= sb
->s_blocksize_bits
;
610 nilfs
->ns_blocksize
= blocksize
;
612 err
= nilfs_store_disk_layout(nilfs
, sbp
);
616 sb
->s_maxbytes
= nilfs_max_size(sb
->s_blocksize_bits
);
618 nilfs
->ns_mount_state
= le16_to_cpu(sbp
->s_state
);
620 bdi
= nilfs
->ns_bdev
->bd_inode
->i_mapping
->backing_dev_info
;
621 nilfs
->ns_bdi
= bdi
? : &default_backing_dev_info
;
623 err
= nilfs_store_log_cursor(nilfs
, sbp
);
627 set_nilfs_init(nilfs
);
630 up_write(&nilfs
->ns_sem
);
634 nilfs_release_super_block(nilfs
);
638 int nilfs_discard_segments(struct the_nilfs
*nilfs
, __u64
*segnump
,
641 sector_t seg_start
, seg_end
;
642 sector_t start
= 0, nblocks
= 0;
643 unsigned int sects_per_block
;
647 sects_per_block
= (1 << nilfs
->ns_blocksize_bits
) /
648 bdev_logical_block_size(nilfs
->ns_bdev
);
649 for (sn
= segnump
; sn
< segnump
+ nsegs
; sn
++) {
650 nilfs_get_segment_range(nilfs
, *sn
, &seg_start
, &seg_end
);
654 nblocks
= seg_end
- seg_start
+ 1;
655 } else if (start
+ nblocks
== seg_start
) {
656 nblocks
+= seg_end
- seg_start
+ 1;
658 ret
= blkdev_issue_discard(nilfs
->ns_bdev
,
659 start
* sects_per_block
,
660 nblocks
* sects_per_block
,
670 ret
= blkdev_issue_discard(nilfs
->ns_bdev
,
671 start
* sects_per_block
,
672 nblocks
* sects_per_block
,
674 BLKDEV_IFL_WAIT
| BLKDEV_IFL_BARRIER
);
678 int nilfs_count_free_blocks(struct the_nilfs
*nilfs
, sector_t
*nblocks
)
680 struct inode
*dat
= nilfs_dat_inode(nilfs
);
681 unsigned long ncleansegs
;
683 down_read(&NILFS_MDT(dat
)->mi_sem
); /* XXX */
684 ncleansegs
= nilfs_sufile_get_ncleansegs(nilfs
->ns_sufile
);
685 up_read(&NILFS_MDT(dat
)->mi_sem
); /* XXX */
686 *nblocks
= (sector_t
)ncleansegs
* nilfs
->ns_blocks_per_segment
;
690 int nilfs_near_disk_full(struct the_nilfs
*nilfs
)
692 unsigned long ncleansegs
, nincsegs
;
694 ncleansegs
= nilfs_sufile_get_ncleansegs(nilfs
->ns_sufile
);
695 nincsegs
= atomic_read(&nilfs
->ns_ndirtyblks
) /
696 nilfs
->ns_blocks_per_segment
+ 1;
698 return ncleansegs
<= nilfs
->ns_nrsvsegs
+ nincsegs
;
701 struct nilfs_root
*nilfs_lookup_root(struct the_nilfs
*nilfs
, __u64 cno
)
704 struct nilfs_root
*root
;
706 spin_lock(&nilfs
->ns_cptree_lock
);
707 n
= nilfs
->ns_cptree
.rb_node
;
709 root
= rb_entry(n
, struct nilfs_root
, rb_node
);
711 if (cno
< root
->cno
) {
713 } else if (cno
> root
->cno
) {
716 atomic_inc(&root
->count
);
717 spin_unlock(&nilfs
->ns_cptree_lock
);
721 spin_unlock(&nilfs
->ns_cptree_lock
);
727 nilfs_find_or_create_root(struct the_nilfs
*nilfs
, __u64 cno
)
729 struct rb_node
**p
, *parent
;
730 struct nilfs_root
*root
, *new;
732 root
= nilfs_lookup_root(nilfs
, cno
);
736 new = kmalloc(sizeof(*root
), GFP_KERNEL
);
740 spin_lock(&nilfs
->ns_cptree_lock
);
742 p
= &nilfs
->ns_cptree
.rb_node
;
747 root
= rb_entry(parent
, struct nilfs_root
, rb_node
);
749 if (cno
< root
->cno
) {
751 } else if (cno
> root
->cno
) {
754 atomic_inc(&root
->count
);
755 spin_unlock(&nilfs
->ns_cptree_lock
);
764 atomic_set(&new->count
, 1);
765 atomic_set(&new->inodes_count
, 0);
766 atomic_set(&new->blocks_count
, 0);
768 rb_link_node(&new->rb_node
, parent
, p
);
769 rb_insert_color(&new->rb_node
, &nilfs
->ns_cptree
);
771 spin_unlock(&nilfs
->ns_cptree_lock
);
776 void nilfs_put_root(struct nilfs_root
*root
)
778 if (atomic_dec_and_test(&root
->count
)) {
779 struct the_nilfs
*nilfs
= root
->nilfs
;
781 spin_lock(&nilfs
->ns_cptree_lock
);
782 rb_erase(&root
->rb_node
, &nilfs
->ns_cptree
);
783 spin_unlock(&nilfs
->ns_cptree_lock
);
785 nilfs_mdt_destroy(root
->ifile
);
791 int nilfs_checkpoint_is_mounted(struct the_nilfs
*nilfs
, __u64 cno
,
794 struct nilfs_root
*root
;
797 if (cno
< 0 || cno
> nilfs
->ns_cno
)
800 if (cno
>= nilfs_last_cno(nilfs
))
801 return true; /* protect recent checkpoints */
804 root
= nilfs_lookup_root(nilfs
, cno
);
807 nilfs_put_root(root
);