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 * Written by Ryusuke Konishi.
20 #include <linux/buffer_head.h>
21 #include <linux/slab.h>
22 #include <linux/blkdev.h>
23 #include <linux/backing-dev.h>
24 #include <linux/random.h>
25 #include <linux/log2.h>
26 #include <linux/crc32.h>
36 static int nilfs_valid_sb(struct nilfs_super_block *sbp);
38 void nilfs_set_last_segment(struct the_nilfs *nilfs,
39 sector_t start_blocknr, u64 seq, __u64 cno)
41 spin_lock(&nilfs->ns_last_segment_lock);
42 nilfs->ns_last_pseg = start_blocknr;
43 nilfs->ns_last_seq = seq;
44 nilfs->ns_last_cno = cno;
46 if (!nilfs_sb_dirty(nilfs)) {
47 if (nilfs->ns_prev_seq == nilfs->ns_last_seq)
50 set_nilfs_sb_dirty(nilfs);
52 nilfs->ns_prev_seq = nilfs->ns_last_seq;
55 spin_unlock(&nilfs->ns_last_segment_lock);
59 * alloc_nilfs - allocate a nilfs object
60 * @sb: super block instance
62 * Return Value: On success, pointer to the_nilfs is returned.
63 * On error, NULL is returned.
65 struct the_nilfs *alloc_nilfs(struct super_block *sb)
67 struct the_nilfs *nilfs;
69 nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
74 nilfs->ns_bdev = sb->s_bdev;
75 atomic_set(&nilfs->ns_ndirtyblks, 0);
76 init_rwsem(&nilfs->ns_sem);
77 mutex_init(&nilfs->ns_snapshot_mount_mutex);
78 INIT_LIST_HEAD(&nilfs->ns_dirty_files);
79 INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
80 spin_lock_init(&nilfs->ns_inode_lock);
81 spin_lock_init(&nilfs->ns_next_gen_lock);
82 spin_lock_init(&nilfs->ns_last_segment_lock);
83 nilfs->ns_cptree = RB_ROOT;
84 spin_lock_init(&nilfs->ns_cptree_lock);
85 init_rwsem(&nilfs->ns_segctor_sem);
86 nilfs->ns_sb_update_freq = NILFS_SB_FREQ;
92 * destroy_nilfs - destroy nilfs object
93 * @nilfs: nilfs object to be released
95 void destroy_nilfs(struct the_nilfs *nilfs)
98 if (nilfs_init(nilfs)) {
99 nilfs_sysfs_delete_device_group(nilfs);
100 brelse(nilfs->ns_sbh[0]);
101 brelse(nilfs->ns_sbh[1]);
106 static int nilfs_load_super_root(struct the_nilfs *nilfs,
107 struct super_block *sb, 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 struct nilfs_inode *rawi;
113 unsigned int dat_entry_size, segment_usage_size, checkpoint_size;
114 unsigned int inode_size;
117 err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
121 down_read(&nilfs->ns_sem);
122 dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
123 checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
124 segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
125 up_read(&nilfs->ns_sem);
127 inode_size = nilfs->ns_inode_size;
129 rawi = (void *)bh_sr->b_data + NILFS_SR_DAT_OFFSET(inode_size);
130 err = nilfs_dat_read(sb, dat_entry_size, rawi, &nilfs->ns_dat);
134 rawi = (void *)bh_sr->b_data + NILFS_SR_CPFILE_OFFSET(inode_size);
135 err = nilfs_cpfile_read(sb, checkpoint_size, rawi, &nilfs->ns_cpfile);
139 rawi = (void *)bh_sr->b_data + NILFS_SR_SUFILE_OFFSET(inode_size);
140 err = nilfs_sufile_read(sb, segment_usage_size, rawi,
145 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
146 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
153 iput(nilfs->ns_cpfile);
160 static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
162 memset(ri, 0, sizeof(*ri));
163 INIT_LIST_HEAD(&ri->ri_used_segments);
166 static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
168 nilfs_dispose_segment_list(&ri->ri_used_segments);
172 * nilfs_store_log_cursor - load log cursor from a super block
173 * @nilfs: nilfs object
174 * @sbp: buffer storing super block to be read
176 * nilfs_store_log_cursor() reads the last position of the log
177 * containing a super root from a given super block, and initializes
178 * relevant information on the nilfs object preparatory for log
179 * scanning and recovery.
181 static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
182 struct nilfs_super_block *sbp)
186 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
187 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
188 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
190 nilfs->ns_prev_seq = nilfs->ns_last_seq;
191 nilfs->ns_seg_seq = nilfs->ns_last_seq;
193 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
194 nilfs->ns_cno = nilfs->ns_last_cno + 1;
195 if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
196 nilfs_msg(nilfs->ns_sb, KERN_ERR,
197 "pointed segment number is out of range: segnum=%llu, nsegments=%lu",
198 (unsigned long long)nilfs->ns_segnum,
199 nilfs->ns_nsegments);
206 * load_nilfs - load and recover the nilfs
207 * @nilfs: the_nilfs structure to be released
208 * @sb: super block isntance used to recover past segment
210 * load_nilfs() searches and load the latest super root,
211 * attaches the last segment, and does recovery if needed.
212 * The caller must call this exclusively for simultaneous mounts.
214 int load_nilfs(struct the_nilfs *nilfs, struct super_block *sb)
216 struct nilfs_recovery_info ri;
217 unsigned int s_flags = sb->s_flags;
218 int really_read_only = bdev_read_only(nilfs->ns_bdev);
219 int valid_fs = nilfs_valid_fs(nilfs);
223 nilfs_msg(sb, KERN_WARNING, "mounting unchecked fs");
224 if (s_flags & MS_RDONLY) {
225 nilfs_msg(sb, KERN_INFO,
226 "recovery required for readonly filesystem");
227 nilfs_msg(sb, KERN_INFO,
228 "write access will be enabled during recovery");
232 nilfs_init_recovery_info(&ri);
234 err = nilfs_search_super_root(nilfs, &ri);
236 struct nilfs_super_block **sbp = nilfs->ns_sbp;
242 if (!nilfs_valid_sb(sbp[1])) {
243 nilfs_msg(sb, KERN_WARNING,
244 "unable to fall back to spare super block");
247 nilfs_msg(sb, KERN_INFO,
248 "trying rollback from an earlier position");
251 * restore super block with its spare and reconfigure
252 * relevant states of the nilfs object.
254 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
255 nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
256 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
258 /* verify consistency between two super blocks */
259 blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
260 if (blocksize != nilfs->ns_blocksize) {
261 nilfs_msg(sb, KERN_WARNING,
262 "blocksize differs between two super blocks (%d != %d)",
263 blocksize, nilfs->ns_blocksize);
267 err = nilfs_store_log_cursor(nilfs, sbp[0]);
271 /* drop clean flag to allow roll-forward and recovery */
272 nilfs->ns_mount_state &= ~NILFS_VALID_FS;
275 err = nilfs_search_super_root(nilfs, &ri);
280 err = nilfs_load_super_root(nilfs, sb, ri.ri_super_root);
282 nilfs_msg(sb, KERN_ERR, "error %d while loading super root",
290 if (s_flags & MS_RDONLY) {
293 if (nilfs_test_opt(nilfs, NORECOVERY)) {
294 nilfs_msg(sb, KERN_INFO,
295 "norecovery option specified, skipping roll-forward recovery");
298 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
299 ~NILFS_FEATURE_COMPAT_RO_SUPP;
301 nilfs_msg(sb, KERN_ERR,
302 "couldn't proceed with recovery because of unsupported optional features (%llx)",
303 (unsigned long long)features);
307 if (really_read_only) {
308 nilfs_msg(sb, KERN_ERR,
309 "write access unavailable, cannot proceed");
313 sb->s_flags &= ~MS_RDONLY;
314 } else if (nilfs_test_opt(nilfs, NORECOVERY)) {
315 nilfs_msg(sb, KERN_ERR,
316 "recovery cancelled because norecovery option was specified for a read/write mount");
321 err = nilfs_salvage_orphan_logs(nilfs, sb, &ri);
325 down_write(&nilfs->ns_sem);
326 nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
327 err = nilfs_cleanup_super(sb);
328 up_write(&nilfs->ns_sem);
331 nilfs_msg(sb, KERN_ERR,
332 "error %d updating super block. recovery unfinished.",
336 nilfs_msg(sb, KERN_INFO, "recovery complete");
339 nilfs_clear_recovery_info(&ri);
340 sb->s_flags = s_flags;
344 nilfs_msg(sb, KERN_ERR, "error %d while searching super root", err);
348 iput(nilfs->ns_cpfile);
349 iput(nilfs->ns_sufile);
353 nilfs_clear_recovery_info(&ri);
354 sb->s_flags = s_flags;
358 static unsigned long long nilfs_max_size(unsigned int blkbits)
360 unsigned int max_bits;
361 unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
363 max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
365 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
370 * nilfs_nrsvsegs - calculate the number of reserved segments
371 * @nilfs: nilfs object
372 * @nsegs: total number of segments
374 unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs)
376 return max_t(unsigned long, NILFS_MIN_NRSVSEGS,
377 DIV_ROUND_UP(nsegs * nilfs->ns_r_segments_percentage,
381 void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs)
383 nilfs->ns_nsegments = nsegs;
384 nilfs->ns_nrsvsegs = nilfs_nrsvsegs(nilfs, nsegs);
387 static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
388 struct nilfs_super_block *sbp)
390 if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
391 nilfs_msg(nilfs->ns_sb, KERN_ERR,
392 "unsupported revision (superblock rev.=%d.%d, current rev.=%d.%d). Please check the version of mkfs.nilfs(2).",
393 le32_to_cpu(sbp->s_rev_level),
394 le16_to_cpu(sbp->s_minor_rev_level),
395 NILFS_CURRENT_REV, NILFS_MINOR_REV);
398 nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
399 if (nilfs->ns_sbsize > BLOCK_SIZE)
402 nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
403 if (nilfs->ns_inode_size > nilfs->ns_blocksize) {
404 nilfs_msg(nilfs->ns_sb, KERN_ERR,
405 "too large inode size: %d bytes",
406 nilfs->ns_inode_size);
408 } else if (nilfs->ns_inode_size < NILFS_MIN_INODE_SIZE) {
409 nilfs_msg(nilfs->ns_sb, KERN_ERR,
410 "too small inode size: %d bytes",
411 nilfs->ns_inode_size);
415 nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
417 nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
418 if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
419 nilfs_msg(nilfs->ns_sb, KERN_ERR,
420 "too short segment: %lu blocks",
421 nilfs->ns_blocks_per_segment);
425 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
426 nilfs->ns_r_segments_percentage =
427 le32_to_cpu(sbp->s_r_segments_percentage);
428 if (nilfs->ns_r_segments_percentage < 1 ||
429 nilfs->ns_r_segments_percentage > 99) {
430 nilfs_msg(nilfs->ns_sb, KERN_ERR,
431 "invalid reserved segments percentage: %lu",
432 nilfs->ns_r_segments_percentage);
436 nilfs_set_nsegments(nilfs, le64_to_cpu(sbp->s_nsegments));
437 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
441 static int nilfs_valid_sb(struct nilfs_super_block *sbp)
443 static unsigned char sum[4];
444 const int sumoff = offsetof(struct nilfs_super_block, s_sum);
448 if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
450 bytes = le16_to_cpu(sbp->s_bytes);
451 if (bytes < sumoff + 4 || bytes > BLOCK_SIZE)
453 crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
455 crc = crc32_le(crc, sum, 4);
456 crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
458 return crc == le32_to_cpu(sbp->s_sum);
462 * nilfs_sb2_bad_offset - check the location of the second superblock
463 * @sbp: superblock raw data buffer
464 * @offset: byte offset of second superblock calculated from device size
466 * nilfs_sb2_bad_offset() checks if the position on the second
467 * superblock is valid or not based on the filesystem parameters
468 * stored in @sbp. If @offset points to a location within the segment
469 * area, or if the parameters themselves are not normal, it is
470 * determined to be invalid.
472 * Return Value: true if invalid, false if valid.
474 static bool nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
476 unsigned int shift_bits = le32_to_cpu(sbp->s_log_block_size);
477 u32 blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
478 u64 nsegments = le64_to_cpu(sbp->s_nsegments);
481 if (blocks_per_segment < NILFS_SEG_MIN_BLOCKS ||
482 shift_bits > ilog2(NILFS_MAX_BLOCK_SIZE) - BLOCK_SIZE_BITS)
485 index = offset >> (shift_bits + BLOCK_SIZE_BITS);
486 do_div(index, blocks_per_segment);
487 return index < nsegments;
490 static void nilfs_release_super_block(struct the_nilfs *nilfs)
494 for (i = 0; i < 2; i++) {
495 if (nilfs->ns_sbp[i]) {
496 brelse(nilfs->ns_sbh[i]);
497 nilfs->ns_sbh[i] = NULL;
498 nilfs->ns_sbp[i] = NULL;
503 void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
505 brelse(nilfs->ns_sbh[0]);
506 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
507 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
508 nilfs->ns_sbh[1] = NULL;
509 nilfs->ns_sbp[1] = NULL;
512 void nilfs_swap_super_block(struct the_nilfs *nilfs)
514 struct buffer_head *tsbh = nilfs->ns_sbh[0];
515 struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
517 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
518 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
519 nilfs->ns_sbh[1] = tsbh;
520 nilfs->ns_sbp[1] = tsbp;
523 static int nilfs_load_super_block(struct the_nilfs *nilfs,
524 struct super_block *sb, int blocksize,
525 struct nilfs_super_block **sbpp)
527 struct nilfs_super_block **sbp = nilfs->ns_sbp;
528 struct buffer_head **sbh = nilfs->ns_sbh;
529 u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
530 int valid[2], swp = 0;
532 sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
534 sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
538 nilfs_msg(sb, KERN_ERR, "unable to read superblock");
541 nilfs_msg(sb, KERN_WARNING,
542 "unable to read primary superblock (blocksize = %d)",
544 } else if (!sbp[1]) {
545 nilfs_msg(sb, KERN_WARNING,
546 "unable to read secondary superblock (blocksize = %d)",
551 * Compare two super blocks and set 1 in swp if the secondary
552 * super block is valid and newer. Otherwise, set 0 in swp.
554 valid[0] = nilfs_valid_sb(sbp[0]);
555 valid[1] = nilfs_valid_sb(sbp[1]);
556 swp = valid[1] && (!valid[0] ||
557 le64_to_cpu(sbp[1]->s_last_cno) >
558 le64_to_cpu(sbp[0]->s_last_cno));
560 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
568 nilfs_release_super_block(nilfs);
569 nilfs_msg(sb, KERN_ERR, "couldn't find nilfs on the device");
574 nilfs_msg(sb, KERN_WARNING,
575 "broken superblock, retrying with spare superblock (blocksize = %d)",
578 nilfs_swap_super_block(nilfs);
580 nilfs->ns_sbwcount = 0;
581 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
582 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
588 * init_nilfs - initialize a NILFS instance.
589 * @nilfs: the_nilfs structure
591 * @data: mount options
593 * init_nilfs() performs common initialization per block device (e.g.
594 * reading the super block, getting disk layout information, initializing
595 * shared fields in the_nilfs).
597 * Return Value: On success, 0 is returned. On error, a negative error
600 int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb, char *data)
602 struct nilfs_super_block *sbp;
606 down_write(&nilfs->ns_sem);
608 blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
610 nilfs_msg(sb, KERN_ERR, "unable to set blocksize");
614 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
618 err = nilfs_store_magic_and_option(sb, sbp, data);
622 err = nilfs_check_feature_compatibility(sb, sbp);
626 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
627 if (blocksize < NILFS_MIN_BLOCK_SIZE ||
628 blocksize > NILFS_MAX_BLOCK_SIZE) {
629 nilfs_msg(sb, KERN_ERR,
630 "couldn't mount because of unsupported filesystem blocksize %d",
635 if (sb->s_blocksize != blocksize) {
636 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
638 if (blocksize < hw_blocksize) {
639 nilfs_msg(sb, KERN_ERR,
640 "blocksize %d too small for device (sector-size = %d)",
641 blocksize, hw_blocksize);
645 nilfs_release_super_block(nilfs);
646 sb_set_blocksize(sb, blocksize);
648 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
652 * Not to failed_sbh; sbh is released automatically
653 * when reloading fails.
656 nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
657 nilfs->ns_blocksize = blocksize;
659 get_random_bytes(&nilfs->ns_next_generation,
660 sizeof(nilfs->ns_next_generation));
662 err = nilfs_store_disk_layout(nilfs, sbp);
666 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
668 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
670 err = nilfs_store_log_cursor(nilfs, sbp);
674 err = nilfs_sysfs_create_device_group(sb);
678 set_nilfs_init(nilfs);
681 up_write(&nilfs->ns_sem);
685 nilfs_release_super_block(nilfs);
689 int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
692 sector_t seg_start, seg_end;
693 sector_t start = 0, nblocks = 0;
694 unsigned int sects_per_block;
698 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
699 bdev_logical_block_size(nilfs->ns_bdev);
700 for (sn = segnump; sn < segnump + nsegs; sn++) {
701 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
705 nblocks = seg_end - seg_start + 1;
706 } else if (start + nblocks == seg_start) {
707 nblocks += seg_end - seg_start + 1;
709 ret = blkdev_issue_discard(nilfs->ns_bdev,
710 start * sects_per_block,
711 nblocks * sects_per_block,
719 ret = blkdev_issue_discard(nilfs->ns_bdev,
720 start * sects_per_block,
721 nblocks * sects_per_block,
726 int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
728 unsigned long ncleansegs;
730 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
731 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
735 int nilfs_near_disk_full(struct the_nilfs *nilfs)
737 unsigned long ncleansegs, nincsegs;
739 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
740 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
741 nilfs->ns_blocks_per_segment + 1;
743 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
746 struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
749 struct nilfs_root *root;
751 spin_lock(&nilfs->ns_cptree_lock);
752 n = nilfs->ns_cptree.rb_node;
754 root = rb_entry(n, struct nilfs_root, rb_node);
756 if (cno < root->cno) {
758 } else if (cno > root->cno) {
761 atomic_inc(&root->count);
762 spin_unlock(&nilfs->ns_cptree_lock);
766 spin_unlock(&nilfs->ns_cptree_lock);
772 nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
774 struct rb_node **p, *parent;
775 struct nilfs_root *root, *new;
778 root = nilfs_lookup_root(nilfs, cno);
782 new = kzalloc(sizeof(*root), GFP_KERNEL);
786 spin_lock(&nilfs->ns_cptree_lock);
788 p = &nilfs->ns_cptree.rb_node;
793 root = rb_entry(parent, struct nilfs_root, rb_node);
795 if (cno < root->cno) {
797 } else if (cno > root->cno) {
800 atomic_inc(&root->count);
801 spin_unlock(&nilfs->ns_cptree_lock);
810 atomic_set(&new->count, 1);
811 atomic64_set(&new->inodes_count, 0);
812 atomic64_set(&new->blocks_count, 0);
814 rb_link_node(&new->rb_node, parent, p);
815 rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
817 spin_unlock(&nilfs->ns_cptree_lock);
819 err = nilfs_sysfs_create_snapshot_group(new);
828 void nilfs_put_root(struct nilfs_root *root)
830 if (atomic_dec_and_test(&root->count)) {
831 struct the_nilfs *nilfs = root->nilfs;
833 nilfs_sysfs_delete_snapshot_group(root);
835 spin_lock(&nilfs->ns_cptree_lock);
836 rb_erase(&root->rb_node, &nilfs->ns_cptree);
837 spin_unlock(&nilfs->ns_cptree_lock);