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 LIST_HEAD(nilfs_objects);
39 static DEFINE_SPINLOCK(nilfs_lock);
41 static int nilfs_valid_sb(struct nilfs_super_block *sbp);
43 void nilfs_set_last_segment(struct the_nilfs *nilfs,
44 sector_t start_blocknr, u64 seq, __u64 cno)
46 spin_lock(&nilfs->ns_last_segment_lock);
47 nilfs->ns_last_pseg = start_blocknr;
48 nilfs->ns_last_seq = seq;
49 nilfs->ns_last_cno = cno;
51 if (!nilfs_sb_dirty(nilfs)) {
52 if (nilfs->ns_prev_seq == nilfs->ns_last_seq)
55 set_nilfs_sb_dirty(nilfs);
57 nilfs->ns_prev_seq = nilfs->ns_last_seq;
60 spin_unlock(&nilfs->ns_last_segment_lock);
64 * alloc_nilfs - allocate the_nilfs structure
65 * @bdev: block device to which the_nilfs is related
67 * alloc_nilfs() allocates memory for the_nilfs and
68 * initializes its reference count and locks.
70 * Return Value: On success, pointer to the_nilfs is returned.
71 * On error, NULL is returned.
73 static struct the_nilfs *alloc_nilfs(struct block_device *bdev)
75 struct the_nilfs *nilfs;
77 nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
81 nilfs->ns_bdev = bdev;
82 atomic_set(&nilfs->ns_count, 1);
83 atomic_set(&nilfs->ns_ndirtyblks, 0);
84 init_rwsem(&nilfs->ns_sem);
85 mutex_init(&nilfs->ns_mount_mutex);
86 init_rwsem(&nilfs->ns_writer_sem);
87 INIT_LIST_HEAD(&nilfs->ns_list);
88 INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
89 spin_lock_init(&nilfs->ns_last_segment_lock);
90 nilfs->ns_cptree = RB_ROOT;
91 spin_lock_init(&nilfs->ns_cptree_lock);
92 init_rwsem(&nilfs->ns_segctor_sem);
98 * find_or_create_nilfs - find or create nilfs object
99 * @bdev: block device to which the_nilfs is related
101 * find_nilfs() looks up an existent nilfs object created on the
102 * device and gets the reference count of the object. If no nilfs object
103 * is found on the device, a new nilfs object is allocated.
105 * Return Value: On success, pointer to the nilfs object is returned.
106 * On error, NULL is returned.
108 struct the_nilfs *find_or_create_nilfs(struct block_device *bdev)
110 struct the_nilfs *nilfs, *new = NULL;
113 spin_lock(&nilfs_lock);
114 list_for_each_entry(nilfs, &nilfs_objects, ns_list) {
115 if (nilfs->ns_bdev == bdev) {
117 spin_unlock(&nilfs_lock);
120 return nilfs; /* existing object */
124 list_add_tail(&new->ns_list, &nilfs_objects);
125 spin_unlock(&nilfs_lock);
126 return new; /* new object */
128 spin_unlock(&nilfs_lock);
130 new = alloc_nilfs(bdev);
133 return NULL; /* insufficient memory */
137 * put_nilfs - release a reference to the_nilfs
138 * @nilfs: the_nilfs structure to be released
140 * put_nilfs() decrements a reference counter of the_nilfs.
141 * If the reference count reaches zero, the_nilfs is freed.
143 void put_nilfs(struct the_nilfs *nilfs)
145 spin_lock(&nilfs_lock);
146 if (!atomic_dec_and_test(&nilfs->ns_count)) {
147 spin_unlock(&nilfs_lock);
150 list_del_init(&nilfs->ns_list);
151 spin_unlock(&nilfs_lock);
154 * Increment of ns_count never occurs below because the caller
155 * of get_nilfs() holds at least one reference to the_nilfs.
156 * Thus its exclusion control is not required here.
160 if (nilfs_loaded(nilfs)) {
161 nilfs_mdt_destroy(nilfs->ns_sufile);
162 nilfs_mdt_destroy(nilfs->ns_cpfile);
163 nilfs_mdt_destroy(nilfs->ns_dat);
164 nilfs_mdt_destroy(nilfs->ns_gc_dat);
166 if (nilfs_init(nilfs)) {
167 brelse(nilfs->ns_sbh[0]);
168 brelse(nilfs->ns_sbh[1]);
173 static int nilfs_load_super_root(struct the_nilfs *nilfs, sector_t sr_block)
175 struct buffer_head *bh_sr;
176 struct nilfs_super_root *raw_sr;
177 struct nilfs_super_block **sbp = nilfs->ns_sbp;
178 unsigned dat_entry_size, segment_usage_size, checkpoint_size;
182 err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
186 down_read(&nilfs->ns_sem);
187 dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
188 checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
189 segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
190 up_read(&nilfs->ns_sem);
192 inode_size = nilfs->ns_inode_size;
195 nilfs->ns_dat = nilfs_dat_new(nilfs, dat_entry_size);
196 if (unlikely(!nilfs->ns_dat))
199 nilfs->ns_gc_dat = nilfs_dat_new(nilfs, dat_entry_size);
200 if (unlikely(!nilfs->ns_gc_dat))
203 nilfs->ns_cpfile = nilfs_cpfile_new(nilfs, checkpoint_size);
204 if (unlikely(!nilfs->ns_cpfile))
207 nilfs->ns_sufile = nilfs_sufile_new(nilfs, segment_usage_size);
208 if (unlikely(!nilfs->ns_sufile))
211 nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat);
213 err = nilfs_dat_read(nilfs->ns_dat, (void *)bh_sr->b_data +
214 NILFS_SR_DAT_OFFSET(inode_size));
218 err = nilfs_cpfile_read(nilfs->ns_cpfile, (void *)bh_sr->b_data +
219 NILFS_SR_CPFILE_OFFSET(inode_size));
223 err = nilfs_sufile_read(nilfs->ns_sufile, (void *)bh_sr->b_data +
224 NILFS_SR_SUFILE_OFFSET(inode_size));
228 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
229 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
236 nilfs_mdt_destroy(nilfs->ns_sufile);
239 nilfs_mdt_destroy(nilfs->ns_cpfile);
242 nilfs_mdt_destroy(nilfs->ns_gc_dat);
245 nilfs_mdt_destroy(nilfs->ns_dat);
249 static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
251 memset(ri, 0, sizeof(*ri));
252 INIT_LIST_HEAD(&ri->ri_used_segments);
255 static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
257 nilfs_dispose_segment_list(&ri->ri_used_segments);
261 * nilfs_store_log_cursor - load log cursor from a super block
262 * @nilfs: nilfs object
263 * @sbp: buffer storing super block to be read
265 * nilfs_store_log_cursor() reads the last position of the log
266 * containing a super root from a given super block, and initializes
267 * relevant information on the nilfs object preparatory for log
268 * scanning and recovery.
270 static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
271 struct nilfs_super_block *sbp)
275 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
276 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
277 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
279 nilfs->ns_prev_seq = nilfs->ns_last_seq;
280 nilfs->ns_seg_seq = nilfs->ns_last_seq;
282 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
283 nilfs->ns_cno = nilfs->ns_last_cno + 1;
284 if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
285 printk(KERN_ERR "NILFS invalid last segment number.\n");
292 * load_nilfs - load and recover the nilfs
293 * @nilfs: the_nilfs structure to be released
294 * @sbi: nilfs_sb_info used to recover past segment
296 * load_nilfs() searches and load the latest super root,
297 * attaches the last segment, and does recovery if needed.
298 * The caller must call this exclusively for simultaneous mounts.
300 int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
302 struct nilfs_recovery_info ri;
303 unsigned int s_flags = sbi->s_super->s_flags;
304 int really_read_only = bdev_read_only(nilfs->ns_bdev);
305 int valid_fs = nilfs_valid_fs(nilfs);
309 printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
310 if (s_flags & MS_RDONLY) {
311 printk(KERN_INFO "NILFS: INFO: recovery "
312 "required for readonly filesystem.\n");
313 printk(KERN_INFO "NILFS: write access will "
314 "be enabled during recovery.\n");
318 nilfs_init_recovery_info(&ri);
320 err = nilfs_search_super_root(nilfs, &ri);
322 struct nilfs_super_block **sbp = nilfs->ns_sbp;
328 if (!nilfs_valid_sb(sbp[1])) {
330 "NILFS warning: unable to fall back to spare"
335 "NILFS: try rollback from an earlier position\n");
338 * restore super block with its spare and reconfigure
339 * relevant states of the nilfs object.
341 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
342 nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
343 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
345 /* verify consistency between two super blocks */
346 blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
347 if (blocksize != nilfs->ns_blocksize) {
349 "NILFS warning: blocksize differs between "
350 "two super blocks (%d != %d)\n",
351 blocksize, nilfs->ns_blocksize);
355 err = nilfs_store_log_cursor(nilfs, sbp[0]);
359 /* drop clean flag to allow roll-forward and recovery */
360 nilfs->ns_mount_state &= ~NILFS_VALID_FS;
363 err = nilfs_search_super_root(nilfs, &ri);
368 err = nilfs_load_super_root(nilfs, ri.ri_super_root);
370 printk(KERN_ERR "NILFS: error loading super root.\n");
377 if (s_flags & MS_RDONLY) {
380 if (nilfs_test_opt(sbi, NORECOVERY)) {
381 printk(KERN_INFO "NILFS: norecovery option specified. "
382 "skipping roll-forward recovery\n");
385 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
386 ~NILFS_FEATURE_COMPAT_RO_SUPP;
388 printk(KERN_ERR "NILFS: couldn't proceed with "
389 "recovery because of unsupported optional "
391 (unsigned long long)features);
395 if (really_read_only) {
396 printk(KERN_ERR "NILFS: write access "
397 "unavailable, cannot proceed.\n");
401 sbi->s_super->s_flags &= ~MS_RDONLY;
402 } else if (nilfs_test_opt(sbi, NORECOVERY)) {
403 printk(KERN_ERR "NILFS: recovery cancelled because norecovery "
404 "option was specified for a read/write mount\n");
409 err = nilfs_salvage_orphan_logs(nilfs, sbi, &ri);
413 down_write(&nilfs->ns_sem);
414 nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
415 err = nilfs_cleanup_super(sbi);
416 up_write(&nilfs->ns_sem);
419 printk(KERN_ERR "NILFS: failed to update super block. "
420 "recovery unfinished.\n");
423 printk(KERN_INFO "NILFS: recovery complete.\n");
426 set_nilfs_loaded(nilfs);
427 nilfs_clear_recovery_info(&ri);
428 sbi->s_super->s_flags = s_flags;
432 printk(KERN_ERR "NILFS: error searching super root.\n");
436 nilfs_mdt_destroy(nilfs->ns_cpfile);
437 nilfs_mdt_destroy(nilfs->ns_sufile);
438 nilfs_mdt_destroy(nilfs->ns_dat);
439 nilfs_mdt_destroy(nilfs->ns_gc_dat);
442 nilfs_clear_recovery_info(&ri);
443 sbi->s_super->s_flags = s_flags;
447 static unsigned long long nilfs_max_size(unsigned int blkbits)
449 unsigned int max_bits;
450 unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
452 max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
454 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
458 static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
459 struct nilfs_super_block *sbp)
461 if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
462 printk(KERN_ERR "NILFS: unsupported revision "
463 "(superblock rev.=%d.%d, current rev.=%d.%d). "
464 "Please check the version of mkfs.nilfs.\n",
465 le32_to_cpu(sbp->s_rev_level),
466 le16_to_cpu(sbp->s_minor_rev_level),
467 NILFS_CURRENT_REV, NILFS_MINOR_REV);
470 nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
471 if (nilfs->ns_sbsize > BLOCK_SIZE)
474 nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
475 nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
477 nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
478 if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
479 printk(KERN_ERR "NILFS: too short segment.\n");
483 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
484 nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
485 nilfs->ns_r_segments_percentage =
486 le32_to_cpu(sbp->s_r_segments_percentage);
488 max_t(unsigned long, NILFS_MIN_NRSVSEGS,
489 DIV_ROUND_UP(nilfs->ns_nsegments *
490 nilfs->ns_r_segments_percentage, 100));
491 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
495 static int nilfs_valid_sb(struct nilfs_super_block *sbp)
497 static unsigned char sum[4];
498 const int sumoff = offsetof(struct nilfs_super_block, s_sum);
502 if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
504 bytes = le16_to_cpu(sbp->s_bytes);
505 if (bytes > BLOCK_SIZE)
507 crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
509 crc = crc32_le(crc, sum, 4);
510 crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
512 return crc == le32_to_cpu(sbp->s_sum);
515 static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
517 return offset < ((le64_to_cpu(sbp->s_nsegments) *
518 le32_to_cpu(sbp->s_blocks_per_segment)) <<
519 (le32_to_cpu(sbp->s_log_block_size) + 10));
522 static void nilfs_release_super_block(struct the_nilfs *nilfs)
526 for (i = 0; i < 2; i++) {
527 if (nilfs->ns_sbp[i]) {
528 brelse(nilfs->ns_sbh[i]);
529 nilfs->ns_sbh[i] = NULL;
530 nilfs->ns_sbp[i] = NULL;
535 void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
537 brelse(nilfs->ns_sbh[0]);
538 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
539 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
540 nilfs->ns_sbh[1] = NULL;
541 nilfs->ns_sbp[1] = NULL;
544 void nilfs_swap_super_block(struct the_nilfs *nilfs)
546 struct buffer_head *tsbh = nilfs->ns_sbh[0];
547 struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
549 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
550 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
551 nilfs->ns_sbh[1] = tsbh;
552 nilfs->ns_sbp[1] = tsbp;
555 static int nilfs_load_super_block(struct the_nilfs *nilfs,
556 struct super_block *sb, int blocksize,
557 struct nilfs_super_block **sbpp)
559 struct nilfs_super_block **sbp = nilfs->ns_sbp;
560 struct buffer_head **sbh = nilfs->ns_sbh;
561 u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
562 int valid[2], swp = 0;
564 sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
566 sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
570 printk(KERN_ERR "NILFS: unable to read superblock\n");
574 "NILFS warning: unable to read primary superblock\n");
577 "NILFS warning: unable to read secondary superblock\n");
580 * Compare two super blocks and set 1 in swp if the secondary
581 * super block is valid and newer. Otherwise, set 0 in swp.
583 valid[0] = nilfs_valid_sb(sbp[0]);
584 valid[1] = nilfs_valid_sb(sbp[1]);
585 swp = valid[1] && (!valid[0] ||
586 le64_to_cpu(sbp[1]->s_last_cno) >
587 le64_to_cpu(sbp[0]->s_last_cno));
589 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
596 nilfs_release_super_block(nilfs);
597 printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
603 printk(KERN_WARNING "NILFS warning: broken superblock. "
604 "using spare superblock.\n");
606 nilfs_swap_super_block(nilfs);
608 nilfs->ns_sbwcount = 0;
609 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
610 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
616 * init_nilfs - initialize a NILFS instance.
617 * @nilfs: the_nilfs structure
618 * @sbi: nilfs_sb_info
620 * @data: mount options
622 * init_nilfs() performs common initialization per block device (e.g.
623 * reading the super block, getting disk layout information, initializing
624 * shared fields in the_nilfs).
626 * Return Value: On success, 0 is returned. On error, a negative error
629 int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
631 struct super_block *sb = sbi->s_super;
632 struct nilfs_super_block *sbp;
633 struct backing_dev_info *bdi;
637 down_write(&nilfs->ns_sem);
639 blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
641 printk(KERN_ERR "NILFS: unable to set blocksize\n");
645 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
649 err = nilfs_store_magic_and_option(sb, sbp, data);
653 err = nilfs_check_feature_compatibility(sb, sbp);
657 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
658 if (blocksize < NILFS_MIN_BLOCK_SIZE ||
659 blocksize > NILFS_MAX_BLOCK_SIZE) {
660 printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
661 "filesystem blocksize %d\n", blocksize);
665 if (sb->s_blocksize != blocksize) {
666 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
668 if (blocksize < hw_blocksize) {
670 "NILFS: blocksize %d too small for device "
671 "(sector-size = %d).\n",
672 blocksize, hw_blocksize);
676 nilfs_release_super_block(nilfs);
677 sb_set_blocksize(sb, blocksize);
679 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
682 /* not failed_sbh; sbh is released automatically
683 when reloading fails. */
685 nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
686 nilfs->ns_blocksize = blocksize;
688 err = nilfs_store_disk_layout(nilfs, sbp);
692 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
694 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
696 bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
697 nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
699 err = nilfs_store_log_cursor(nilfs, sbp);
703 set_nilfs_init(nilfs);
706 up_write(&nilfs->ns_sem);
710 nilfs_release_super_block(nilfs);
714 int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
717 sector_t seg_start, seg_end;
718 sector_t start = 0, nblocks = 0;
719 unsigned int sects_per_block;
723 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
724 bdev_logical_block_size(nilfs->ns_bdev);
725 for (sn = segnump; sn < segnump + nsegs; sn++) {
726 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
730 nblocks = seg_end - seg_start + 1;
731 } else if (start + nblocks == seg_start) {
732 nblocks += seg_end - seg_start + 1;
734 ret = blkdev_issue_discard(nilfs->ns_bdev,
735 start * sects_per_block,
736 nblocks * sects_per_block,
746 ret = blkdev_issue_discard(nilfs->ns_bdev,
747 start * sects_per_block,
748 nblocks * sects_per_block,
750 BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER);
754 int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
756 struct inode *dat = nilfs_dat_inode(nilfs);
757 unsigned long ncleansegs;
759 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
760 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
761 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
762 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
766 int nilfs_near_disk_full(struct the_nilfs *nilfs)
768 unsigned long ncleansegs, nincsegs;
770 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
771 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
772 nilfs->ns_blocks_per_segment + 1;
774 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
777 struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
780 struct nilfs_root *root;
782 spin_lock(&nilfs->ns_cptree_lock);
783 n = nilfs->ns_cptree.rb_node;
785 root = rb_entry(n, struct nilfs_root, rb_node);
787 if (cno < root->cno) {
789 } else if (cno > root->cno) {
792 atomic_inc(&root->count);
793 spin_unlock(&nilfs->ns_cptree_lock);
797 spin_unlock(&nilfs->ns_cptree_lock);
803 nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
805 struct rb_node **p, *parent;
806 struct nilfs_root *root, *new;
808 root = nilfs_lookup_root(nilfs, cno);
812 new = kmalloc(sizeof(*root), GFP_KERNEL);
816 spin_lock(&nilfs->ns_cptree_lock);
818 p = &nilfs->ns_cptree.rb_node;
823 root = rb_entry(parent, struct nilfs_root, rb_node);
825 if (cno < root->cno) {
827 } else if (cno > root->cno) {
830 atomic_inc(&root->count);
831 spin_unlock(&nilfs->ns_cptree_lock);
840 atomic_set(&new->count, 1);
841 atomic_set(&new->inodes_count, 0);
842 atomic_set(&new->blocks_count, 0);
844 rb_link_node(&new->rb_node, parent, p);
845 rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
847 spin_unlock(&nilfs->ns_cptree_lock);
852 void nilfs_put_root(struct nilfs_root *root)
854 if (atomic_dec_and_test(&root->count)) {
855 struct the_nilfs *nilfs = root->nilfs;
857 spin_lock(&nilfs->ns_cptree_lock);
858 rb_erase(&root->rb_node, &nilfs->ns_cptree);
859 spin_unlock(&nilfs->ns_cptree_lock);
861 nilfs_mdt_destroy(root->ifile);
867 int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
870 struct nilfs_root *root;
873 if (cno < 0 || cno > nilfs->ns_cno)
876 if (cno >= nilfs_last_cno(nilfs))
877 return true; /* protect recent checkpoints */
880 root = nilfs_lookup_root(nilfs, cno);
883 nilfs_put_root(root);