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 brelse(nilfs->ns_sbh[0]);
100 brelse(nilfs->ns_sbh[1]);
105 static int nilfs_load_super_root(struct the_nilfs *nilfs,
106 struct super_block *sb, sector_t sr_block)
108 struct buffer_head *bh_sr;
109 struct nilfs_super_root *raw_sr;
110 struct nilfs_super_block **sbp = nilfs->ns_sbp;
111 struct nilfs_inode *rawi;
112 unsigned int dat_entry_size, segment_usage_size, checkpoint_size;
113 unsigned int inode_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;
128 rawi = (void *)bh_sr->b_data + NILFS_SR_DAT_OFFSET(inode_size);
129 err = nilfs_dat_read(sb, dat_entry_size, rawi, &nilfs->ns_dat);
133 rawi = (void *)bh_sr->b_data + NILFS_SR_CPFILE_OFFSET(inode_size);
134 err = nilfs_cpfile_read(sb, checkpoint_size, rawi, &nilfs->ns_cpfile);
138 rawi = (void *)bh_sr->b_data + NILFS_SR_SUFILE_OFFSET(inode_size);
139 err = nilfs_sufile_read(sb, segment_usage_size, rawi,
144 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
145 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
152 iput(nilfs->ns_cpfile);
159 static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
161 memset(ri, 0, sizeof(*ri));
162 INIT_LIST_HEAD(&ri->ri_used_segments);
165 static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
167 nilfs_dispose_segment_list(&ri->ri_used_segments);
171 * nilfs_store_log_cursor - load log cursor from a super block
172 * @nilfs: nilfs object
173 * @sbp: buffer storing super block to be read
175 * nilfs_store_log_cursor() reads the last position of the log
176 * containing a super root from a given super block, and initializes
177 * relevant information on the nilfs object preparatory for log
178 * scanning and recovery.
180 static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
181 struct nilfs_super_block *sbp)
185 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
186 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
187 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
189 nilfs->ns_prev_seq = nilfs->ns_last_seq;
190 nilfs->ns_seg_seq = nilfs->ns_last_seq;
192 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
193 nilfs->ns_cno = nilfs->ns_last_cno + 1;
194 if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
195 nilfs_msg(nilfs->ns_sb, KERN_ERR,
196 "pointed segment number is out of range: segnum=%llu, nsegments=%lu",
197 (unsigned long long)nilfs->ns_segnum,
198 nilfs->ns_nsegments);
205 * load_nilfs - load and recover the nilfs
206 * @nilfs: the_nilfs structure to be released
207 * @sb: super block isntance used to recover past segment
209 * load_nilfs() searches and load the latest super root,
210 * attaches the last segment, and does recovery if needed.
211 * The caller must call this exclusively for simultaneous mounts.
213 int load_nilfs(struct the_nilfs *nilfs, struct super_block *sb)
215 struct nilfs_recovery_info ri;
216 unsigned int s_flags = sb->s_flags;
217 int really_read_only = bdev_read_only(nilfs->ns_bdev);
218 int valid_fs = nilfs_valid_fs(nilfs);
222 nilfs_msg(sb, KERN_WARNING, "mounting unchecked fs");
223 if (s_flags & MS_RDONLY) {
224 nilfs_msg(sb, KERN_INFO,
225 "recovery required for readonly filesystem");
226 nilfs_msg(sb, KERN_INFO,
227 "write access will be enabled during recovery");
231 nilfs_init_recovery_info(&ri);
233 err = nilfs_search_super_root(nilfs, &ri);
235 struct nilfs_super_block **sbp = nilfs->ns_sbp;
241 if (!nilfs_valid_sb(sbp[1])) {
242 nilfs_msg(sb, KERN_WARNING,
243 "unable to fall back to spare super block");
246 nilfs_msg(sb, KERN_INFO,
247 "trying rollback from an earlier position");
250 * restore super block with its spare and reconfigure
251 * relevant states of the nilfs object.
253 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
254 nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
255 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
257 /* verify consistency between two super blocks */
258 blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
259 if (blocksize != nilfs->ns_blocksize) {
260 nilfs_msg(sb, KERN_WARNING,
261 "blocksize differs between two super blocks (%d != %d)",
262 blocksize, nilfs->ns_blocksize);
266 err = nilfs_store_log_cursor(nilfs, sbp[0]);
270 /* drop clean flag to allow roll-forward and recovery */
271 nilfs->ns_mount_state &= ~NILFS_VALID_FS;
274 err = nilfs_search_super_root(nilfs, &ri);
279 err = nilfs_load_super_root(nilfs, sb, ri.ri_super_root);
281 nilfs_msg(sb, KERN_ERR, "error %d while loading super root",
286 err = nilfs_sysfs_create_device_group(sb);
293 if (s_flags & MS_RDONLY) {
296 if (nilfs_test_opt(nilfs, NORECOVERY)) {
297 nilfs_msg(sb, KERN_INFO,
298 "norecovery option specified, skipping roll-forward recovery");
301 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
302 ~NILFS_FEATURE_COMPAT_RO_SUPP;
304 nilfs_msg(sb, KERN_ERR,
305 "couldn't proceed with recovery because of unsupported optional features (%llx)",
306 (unsigned long long)features);
310 if (really_read_only) {
311 nilfs_msg(sb, KERN_ERR,
312 "write access unavailable, cannot proceed");
316 sb->s_flags &= ~MS_RDONLY;
317 } else if (nilfs_test_opt(nilfs, NORECOVERY)) {
318 nilfs_msg(sb, KERN_ERR,
319 "recovery cancelled because norecovery option was specified for a read/write mount");
324 err = nilfs_salvage_orphan_logs(nilfs, sb, &ri);
328 down_write(&nilfs->ns_sem);
329 nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
330 err = nilfs_cleanup_super(sb);
331 up_write(&nilfs->ns_sem);
334 nilfs_msg(sb, KERN_ERR,
335 "error %d updating super block. recovery unfinished.",
339 nilfs_msg(sb, KERN_INFO, "recovery complete");
342 nilfs_clear_recovery_info(&ri);
343 sb->s_flags = s_flags;
347 nilfs_msg(sb, KERN_ERR, "error %d while searching super root", err);
351 nilfs_sysfs_delete_device_group(nilfs);
354 iput(nilfs->ns_cpfile);
355 iput(nilfs->ns_sufile);
359 nilfs_clear_recovery_info(&ri);
360 sb->s_flags = s_flags;
364 static unsigned long long nilfs_max_size(unsigned int blkbits)
366 unsigned int max_bits;
367 unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
369 max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
371 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
376 * nilfs_nrsvsegs - calculate the number of reserved segments
377 * @nilfs: nilfs object
378 * @nsegs: total number of segments
380 unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs)
382 return max_t(unsigned long, NILFS_MIN_NRSVSEGS,
383 DIV_ROUND_UP(nsegs * nilfs->ns_r_segments_percentage,
387 void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs)
389 nilfs->ns_nsegments = nsegs;
390 nilfs->ns_nrsvsegs = nilfs_nrsvsegs(nilfs, nsegs);
393 static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
394 struct nilfs_super_block *sbp)
396 if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
397 nilfs_msg(nilfs->ns_sb, KERN_ERR,
398 "unsupported revision (superblock rev.=%d.%d, current rev.=%d.%d). Please check the version of mkfs.nilfs(2).",
399 le32_to_cpu(sbp->s_rev_level),
400 le16_to_cpu(sbp->s_minor_rev_level),
401 NILFS_CURRENT_REV, NILFS_MINOR_REV);
404 nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
405 if (nilfs->ns_sbsize > BLOCK_SIZE)
408 nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
409 if (nilfs->ns_inode_size > nilfs->ns_blocksize) {
410 nilfs_msg(nilfs->ns_sb, KERN_ERR,
411 "too large inode size: %d bytes",
412 nilfs->ns_inode_size);
414 } else if (nilfs->ns_inode_size < NILFS_MIN_INODE_SIZE) {
415 nilfs_msg(nilfs->ns_sb, KERN_ERR,
416 "too small inode size: %d bytes",
417 nilfs->ns_inode_size);
421 nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
423 nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
424 if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
425 nilfs_msg(nilfs->ns_sb, KERN_ERR,
426 "too short segment: %lu blocks",
427 nilfs->ns_blocks_per_segment);
431 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
432 nilfs->ns_r_segments_percentage =
433 le32_to_cpu(sbp->s_r_segments_percentage);
434 if (nilfs->ns_r_segments_percentage < 1 ||
435 nilfs->ns_r_segments_percentage > 99) {
436 nilfs_msg(nilfs->ns_sb, KERN_ERR,
437 "invalid reserved segments percentage: %lu",
438 nilfs->ns_r_segments_percentage);
442 nilfs_set_nsegments(nilfs, le64_to_cpu(sbp->s_nsegments));
443 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
447 static int nilfs_valid_sb(struct nilfs_super_block *sbp)
449 static unsigned char sum[4];
450 const int sumoff = offsetof(struct nilfs_super_block, s_sum);
454 if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
456 bytes = le16_to_cpu(sbp->s_bytes);
457 if (bytes < sumoff + 4 || bytes > BLOCK_SIZE)
459 crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
461 crc = crc32_le(crc, sum, 4);
462 crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
464 return crc == le32_to_cpu(sbp->s_sum);
468 * nilfs_sb2_bad_offset - check the location of the second superblock
469 * @sbp: superblock raw data buffer
470 * @offset: byte offset of second superblock calculated from device size
472 * nilfs_sb2_bad_offset() checks if the position on the second
473 * superblock is valid or not based on the filesystem parameters
474 * stored in @sbp. If @offset points to a location within the segment
475 * area, or if the parameters themselves are not normal, it is
476 * determined to be invalid.
478 * Return Value: true if invalid, false if valid.
480 static bool nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
482 unsigned int shift_bits = le32_to_cpu(sbp->s_log_block_size);
483 u32 blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
484 u64 nsegments = le64_to_cpu(sbp->s_nsegments);
487 if (blocks_per_segment < NILFS_SEG_MIN_BLOCKS ||
488 shift_bits > ilog2(NILFS_MAX_BLOCK_SIZE) - BLOCK_SIZE_BITS)
491 index = offset >> (shift_bits + BLOCK_SIZE_BITS);
492 do_div(index, blocks_per_segment);
493 return index < nsegments;
496 static void nilfs_release_super_block(struct the_nilfs *nilfs)
500 for (i = 0; i < 2; i++) {
501 if (nilfs->ns_sbp[i]) {
502 brelse(nilfs->ns_sbh[i]);
503 nilfs->ns_sbh[i] = NULL;
504 nilfs->ns_sbp[i] = NULL;
509 void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
511 brelse(nilfs->ns_sbh[0]);
512 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
513 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
514 nilfs->ns_sbh[1] = NULL;
515 nilfs->ns_sbp[1] = NULL;
518 void nilfs_swap_super_block(struct the_nilfs *nilfs)
520 struct buffer_head *tsbh = nilfs->ns_sbh[0];
521 struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
523 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
524 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
525 nilfs->ns_sbh[1] = tsbh;
526 nilfs->ns_sbp[1] = tsbp;
529 static int nilfs_load_super_block(struct the_nilfs *nilfs,
530 struct super_block *sb, int blocksize,
531 struct nilfs_super_block **sbpp)
533 struct nilfs_super_block **sbp = nilfs->ns_sbp;
534 struct buffer_head **sbh = nilfs->ns_sbh;
535 u64 sb2off, devsize = nilfs->ns_bdev->bd_inode->i_size;
536 int valid[2], swp = 0;
538 if (devsize < NILFS_SEG_MIN_BLOCKS * NILFS_MIN_BLOCK_SIZE + 4096) {
539 nilfs_msg(sb, KERN_ERR, "device size too small");
542 sb2off = NILFS_SB2_OFFSET_BYTES(devsize);
544 sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
546 sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
550 nilfs_msg(sb, KERN_ERR, "unable to read superblock");
553 nilfs_msg(sb, KERN_WARNING,
554 "unable to read primary superblock (blocksize = %d)",
556 } else if (!sbp[1]) {
557 nilfs_msg(sb, KERN_WARNING,
558 "unable to read secondary superblock (blocksize = %d)",
563 * Compare two super blocks and set 1 in swp if the secondary
564 * super block is valid and newer. Otherwise, set 0 in swp.
566 valid[0] = nilfs_valid_sb(sbp[0]);
567 valid[1] = nilfs_valid_sb(sbp[1]);
568 swp = valid[1] && (!valid[0] ||
569 le64_to_cpu(sbp[1]->s_last_cno) >
570 le64_to_cpu(sbp[0]->s_last_cno));
572 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
580 nilfs_release_super_block(nilfs);
581 nilfs_msg(sb, KERN_ERR, "couldn't find nilfs on the device");
586 nilfs_msg(sb, KERN_WARNING,
587 "broken superblock, retrying with spare superblock (blocksize = %d)",
590 nilfs_swap_super_block(nilfs);
592 nilfs->ns_sbwcount = 0;
593 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
594 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
600 * init_nilfs - initialize a NILFS instance.
601 * @nilfs: the_nilfs structure
603 * @data: mount options
605 * init_nilfs() performs common initialization per block device (e.g.
606 * reading the super block, getting disk layout information, initializing
607 * shared fields in the_nilfs).
609 * Return Value: On success, 0 is returned. On error, a negative error
612 int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb, char *data)
614 struct nilfs_super_block *sbp;
618 down_write(&nilfs->ns_sem);
620 blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
622 nilfs_msg(sb, KERN_ERR, "unable to set blocksize");
626 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
630 err = nilfs_store_magic_and_option(sb, sbp, data);
634 err = nilfs_check_feature_compatibility(sb, sbp);
638 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
639 if (blocksize < NILFS_MIN_BLOCK_SIZE ||
640 blocksize > NILFS_MAX_BLOCK_SIZE) {
641 nilfs_msg(sb, KERN_ERR,
642 "couldn't mount because of unsupported filesystem blocksize %d",
647 if (sb->s_blocksize != blocksize) {
648 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
650 if (blocksize < hw_blocksize) {
651 nilfs_msg(sb, KERN_ERR,
652 "blocksize %d too small for device (sector-size = %d)",
653 blocksize, hw_blocksize);
657 nilfs_release_super_block(nilfs);
658 sb_set_blocksize(sb, blocksize);
660 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
664 * Not to failed_sbh; sbh is released automatically
665 * when reloading fails.
668 nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
669 nilfs->ns_blocksize = blocksize;
671 get_random_bytes(&nilfs->ns_next_generation,
672 sizeof(nilfs->ns_next_generation));
674 err = nilfs_store_disk_layout(nilfs, sbp);
678 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
680 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
682 err = nilfs_store_log_cursor(nilfs, sbp);
686 set_nilfs_init(nilfs);
689 up_write(&nilfs->ns_sem);
693 nilfs_release_super_block(nilfs);
697 int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
700 sector_t seg_start, seg_end;
701 sector_t start = 0, nblocks = 0;
702 unsigned int sects_per_block;
706 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
707 bdev_logical_block_size(nilfs->ns_bdev);
708 for (sn = segnump; sn < segnump + nsegs; sn++) {
709 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
713 nblocks = seg_end - seg_start + 1;
714 } else if (start + nblocks == seg_start) {
715 nblocks += seg_end - seg_start + 1;
717 ret = blkdev_issue_discard(nilfs->ns_bdev,
718 start * sects_per_block,
719 nblocks * sects_per_block,
727 ret = blkdev_issue_discard(nilfs->ns_bdev,
728 start * sects_per_block,
729 nblocks * sects_per_block,
734 int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
736 unsigned long ncleansegs;
738 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
739 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
743 int nilfs_near_disk_full(struct the_nilfs *nilfs)
745 unsigned long ncleansegs, nincsegs;
747 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
748 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
749 nilfs->ns_blocks_per_segment + 1;
751 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
754 struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
757 struct nilfs_root *root;
759 spin_lock(&nilfs->ns_cptree_lock);
760 n = nilfs->ns_cptree.rb_node;
762 root = rb_entry(n, struct nilfs_root, rb_node);
764 if (cno < root->cno) {
766 } else if (cno > root->cno) {
769 atomic_inc(&root->count);
770 spin_unlock(&nilfs->ns_cptree_lock);
774 spin_unlock(&nilfs->ns_cptree_lock);
780 nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
782 struct rb_node **p, *parent;
783 struct nilfs_root *root, *new;
786 root = nilfs_lookup_root(nilfs, cno);
790 new = kzalloc(sizeof(*root), GFP_KERNEL);
794 spin_lock(&nilfs->ns_cptree_lock);
796 p = &nilfs->ns_cptree.rb_node;
801 root = rb_entry(parent, struct nilfs_root, rb_node);
803 if (cno < root->cno) {
805 } else if (cno > root->cno) {
808 atomic_inc(&root->count);
809 spin_unlock(&nilfs->ns_cptree_lock);
818 atomic_set(&new->count, 1);
819 atomic64_set(&new->inodes_count, 0);
820 atomic64_set(&new->blocks_count, 0);
822 rb_link_node(&new->rb_node, parent, p);
823 rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
825 spin_unlock(&nilfs->ns_cptree_lock);
827 err = nilfs_sysfs_create_snapshot_group(new);
836 void nilfs_put_root(struct nilfs_root *root)
838 if (atomic_dec_and_test(&root->count)) {
839 struct the_nilfs *nilfs = root->nilfs;
841 nilfs_sysfs_delete_snapshot_group(root);
843 spin_lock(&nilfs->ns_cptree_lock);
844 rb_erase(&root->rb_node, &nilfs->ns_cptree);
845 spin_unlock(&nilfs->ns_cptree_lock);