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 init_rwsem(&nilfs
->ns_super_sem
);
86 mutex_init(&nilfs
->ns_mount_mutex
);
87 init_rwsem(&nilfs
->ns_writer_sem
);
88 INIT_LIST_HEAD(&nilfs
->ns_list
);
89 INIT_LIST_HEAD(&nilfs
->ns_supers
);
90 spin_lock_init(&nilfs
->ns_last_segment_lock
);
91 nilfs
->ns_gc_inodes_h
= NULL
;
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 nilfs_destroy_gccache(nilfs
);
168 brelse(nilfs
->ns_sbh
[0]);
169 brelse(nilfs
->ns_sbh
[1]);
174 static int nilfs_load_super_root(struct the_nilfs
*nilfs
, sector_t sr_block
)
176 struct buffer_head
*bh_sr
;
177 struct nilfs_super_root
*raw_sr
;
178 struct nilfs_super_block
**sbp
= nilfs
->ns_sbp
;
179 unsigned dat_entry_size
, segment_usage_size
, checkpoint_size
;
183 err
= nilfs_read_super_root_block(nilfs
, sr_block
, &bh_sr
, 1);
187 down_read(&nilfs
->ns_sem
);
188 dat_entry_size
= le16_to_cpu(sbp
[0]->s_dat_entry_size
);
189 checkpoint_size
= le16_to_cpu(sbp
[0]->s_checkpoint_size
);
190 segment_usage_size
= le16_to_cpu(sbp
[0]->s_segment_usage_size
);
191 up_read(&nilfs
->ns_sem
);
193 inode_size
= nilfs
->ns_inode_size
;
196 nilfs
->ns_dat
= nilfs_dat_new(nilfs
, dat_entry_size
);
197 if (unlikely(!nilfs
->ns_dat
))
200 nilfs
->ns_gc_dat
= nilfs_dat_new(nilfs
, dat_entry_size
);
201 if (unlikely(!nilfs
->ns_gc_dat
))
204 nilfs
->ns_cpfile
= nilfs_cpfile_new(nilfs
, checkpoint_size
);
205 if (unlikely(!nilfs
->ns_cpfile
))
208 nilfs
->ns_sufile
= nilfs_sufile_new(nilfs
, segment_usage_size
);
209 if (unlikely(!nilfs
->ns_sufile
))
212 nilfs_mdt_set_shadow(nilfs
->ns_dat
, nilfs
->ns_gc_dat
);
214 err
= nilfs_dat_read(nilfs
->ns_dat
, (void *)bh_sr
->b_data
+
215 NILFS_SR_DAT_OFFSET(inode_size
));
219 err
= nilfs_cpfile_read(nilfs
->ns_cpfile
, (void *)bh_sr
->b_data
+
220 NILFS_SR_CPFILE_OFFSET(inode_size
));
224 err
= nilfs_sufile_read(nilfs
->ns_sufile
, (void *)bh_sr
->b_data
+
225 NILFS_SR_SUFILE_OFFSET(inode_size
));
229 raw_sr
= (struct nilfs_super_root
*)bh_sr
->b_data
;
230 nilfs
->ns_nongc_ctime
= le64_to_cpu(raw_sr
->sr_nongc_ctime
);
237 nilfs_mdt_destroy(nilfs
->ns_sufile
);
240 nilfs_mdt_destroy(nilfs
->ns_cpfile
);
243 nilfs_mdt_destroy(nilfs
->ns_gc_dat
);
246 nilfs_mdt_destroy(nilfs
->ns_dat
);
250 static void nilfs_init_recovery_info(struct nilfs_recovery_info
*ri
)
252 memset(ri
, 0, sizeof(*ri
));
253 INIT_LIST_HEAD(&ri
->ri_used_segments
);
256 static void nilfs_clear_recovery_info(struct nilfs_recovery_info
*ri
)
258 nilfs_dispose_segment_list(&ri
->ri_used_segments
);
262 * nilfs_store_log_cursor - load log cursor from a super block
263 * @nilfs: nilfs object
264 * @sbp: buffer storing super block to be read
266 * nilfs_store_log_cursor() reads the last position of the log
267 * containing a super root from a given super block, and initializes
268 * relevant information on the nilfs object preparatory for log
269 * scanning and recovery.
271 static int nilfs_store_log_cursor(struct the_nilfs
*nilfs
,
272 struct nilfs_super_block
*sbp
)
276 nilfs
->ns_last_pseg
= le64_to_cpu(sbp
->s_last_pseg
);
277 nilfs
->ns_last_cno
= le64_to_cpu(sbp
->s_last_cno
);
278 nilfs
->ns_last_seq
= le64_to_cpu(sbp
->s_last_seq
);
280 nilfs
->ns_prev_seq
= nilfs
->ns_last_seq
;
281 nilfs
->ns_seg_seq
= nilfs
->ns_last_seq
;
283 nilfs_get_segnum_of_block(nilfs
, nilfs
->ns_last_pseg
);
284 nilfs
->ns_cno
= nilfs
->ns_last_cno
+ 1;
285 if (nilfs
->ns_segnum
>= nilfs
->ns_nsegments
) {
286 printk(KERN_ERR
"NILFS invalid last segment number.\n");
293 * load_nilfs - load and recover the nilfs
294 * @nilfs: the_nilfs structure to be released
295 * @sbi: nilfs_sb_info used to recover past segment
297 * load_nilfs() searches and load the latest super root,
298 * attaches the last segment, and does recovery if needed.
299 * The caller must call this exclusively for simultaneous mounts.
301 int load_nilfs(struct the_nilfs
*nilfs
, struct nilfs_sb_info
*sbi
)
303 struct nilfs_recovery_info ri
;
304 unsigned int s_flags
= sbi
->s_super
->s_flags
;
305 int really_read_only
= bdev_read_only(nilfs
->ns_bdev
);
306 int valid_fs
= nilfs_valid_fs(nilfs
);
309 if (nilfs_loaded(nilfs
)) {
311 ((s_flags
& MS_RDONLY
) && nilfs_test_opt(sbi
, NORECOVERY
)))
313 printk(KERN_ERR
"NILFS: the filesystem is in an incomplete "
314 "recovery state.\n");
319 printk(KERN_WARNING
"NILFS warning: mounting unchecked fs\n");
320 if (s_flags
& MS_RDONLY
) {
321 printk(KERN_INFO
"NILFS: INFO: recovery "
322 "required for readonly filesystem.\n");
323 printk(KERN_INFO
"NILFS: write access will "
324 "be enabled during recovery.\n");
328 nilfs_init_recovery_info(&ri
);
330 err
= nilfs_search_super_root(nilfs
, &ri
);
332 struct nilfs_super_block
**sbp
= nilfs
->ns_sbp
;
338 if (!nilfs_valid_sb(sbp
[1])) {
340 "NILFS warning: unable to fall back to spare"
345 "NILFS: try rollback from an earlier position\n");
348 * restore super block with its spare and reconfigure
349 * relevant states of the nilfs object.
351 memcpy(sbp
[0], sbp
[1], nilfs
->ns_sbsize
);
352 nilfs
->ns_crc_seed
= le32_to_cpu(sbp
[0]->s_crc_seed
);
353 nilfs
->ns_sbwtime
= le64_to_cpu(sbp
[0]->s_wtime
);
355 /* verify consistency between two super blocks */
356 blocksize
= BLOCK_SIZE
<< le32_to_cpu(sbp
[0]->s_log_block_size
);
357 if (blocksize
!= nilfs
->ns_blocksize
) {
359 "NILFS warning: blocksize differs between "
360 "two super blocks (%d != %d)\n",
361 blocksize
, nilfs
->ns_blocksize
);
365 err
= nilfs_store_log_cursor(nilfs
, sbp
[0]);
369 /* drop clean flag to allow roll-forward and recovery */
370 nilfs
->ns_mount_state
&= ~NILFS_VALID_FS
;
373 err
= nilfs_search_super_root(nilfs
, &ri
);
378 err
= nilfs_load_super_root(nilfs
, ri
.ri_super_root
);
380 printk(KERN_ERR
"NILFS: error loading super root.\n");
387 if (s_flags
& MS_RDONLY
) {
390 if (nilfs_test_opt(sbi
, NORECOVERY
)) {
391 printk(KERN_INFO
"NILFS: norecovery option specified. "
392 "skipping roll-forward recovery\n");
395 features
= le64_to_cpu(nilfs
->ns_sbp
[0]->s_feature_compat_ro
) &
396 ~NILFS_FEATURE_COMPAT_RO_SUPP
;
398 printk(KERN_ERR
"NILFS: couldn't proceed with "
399 "recovery because of unsupported optional "
401 (unsigned long long)features
);
405 if (really_read_only
) {
406 printk(KERN_ERR
"NILFS: write access "
407 "unavailable, cannot proceed.\n");
411 sbi
->s_super
->s_flags
&= ~MS_RDONLY
;
412 } else if (nilfs_test_opt(sbi
, NORECOVERY
)) {
413 printk(KERN_ERR
"NILFS: recovery cancelled because norecovery "
414 "option was specified for a read/write mount\n");
419 err
= nilfs_salvage_orphan_logs(nilfs
, sbi
, &ri
);
423 down_write(&nilfs
->ns_sem
);
424 nilfs
->ns_mount_state
|= NILFS_VALID_FS
; /* set "clean" flag */
425 err
= nilfs_cleanup_super(sbi
);
426 up_write(&nilfs
->ns_sem
);
429 printk(KERN_ERR
"NILFS: failed to update super block. "
430 "recovery unfinished.\n");
433 printk(KERN_INFO
"NILFS: recovery complete.\n");
436 set_nilfs_loaded(nilfs
);
437 nilfs_clear_recovery_info(&ri
);
438 sbi
->s_super
->s_flags
= s_flags
;
442 printk(KERN_ERR
"NILFS: error searching super root.\n");
446 nilfs_mdt_destroy(nilfs
->ns_cpfile
);
447 nilfs_mdt_destroy(nilfs
->ns_sufile
);
448 nilfs_mdt_destroy(nilfs
->ns_dat
);
449 nilfs_mdt_destroy(nilfs
->ns_gc_dat
);
452 nilfs_clear_recovery_info(&ri
);
453 sbi
->s_super
->s_flags
= s_flags
;
457 static unsigned long long nilfs_max_size(unsigned int blkbits
)
459 unsigned int max_bits
;
460 unsigned long long res
= MAX_LFS_FILESIZE
; /* page cache limit */
462 max_bits
= blkbits
+ NILFS_BMAP_KEY_BIT
; /* bmap size limit */
464 res
= min_t(unsigned long long, res
, (1ULL << max_bits
) - 1);
468 static int nilfs_store_disk_layout(struct the_nilfs
*nilfs
,
469 struct nilfs_super_block
*sbp
)
471 if (le32_to_cpu(sbp
->s_rev_level
) != NILFS_CURRENT_REV
) {
472 printk(KERN_ERR
"NILFS: revision mismatch "
473 "(superblock rev.=%d.%d, current rev.=%d.%d). "
474 "Please check the version of mkfs.nilfs.\n",
475 le32_to_cpu(sbp
->s_rev_level
),
476 le16_to_cpu(sbp
->s_minor_rev_level
),
477 NILFS_CURRENT_REV
, NILFS_MINOR_REV
);
480 nilfs
->ns_sbsize
= le16_to_cpu(sbp
->s_bytes
);
481 if (nilfs
->ns_sbsize
> BLOCK_SIZE
)
484 nilfs
->ns_inode_size
= le16_to_cpu(sbp
->s_inode_size
);
485 nilfs
->ns_first_ino
= le32_to_cpu(sbp
->s_first_ino
);
487 nilfs
->ns_blocks_per_segment
= le32_to_cpu(sbp
->s_blocks_per_segment
);
488 if (nilfs
->ns_blocks_per_segment
< NILFS_SEG_MIN_BLOCKS
) {
489 printk(KERN_ERR
"NILFS: too short segment.\n");
493 nilfs
->ns_first_data_block
= le64_to_cpu(sbp
->s_first_data_block
);
494 nilfs
->ns_nsegments
= le64_to_cpu(sbp
->s_nsegments
);
495 nilfs
->ns_r_segments_percentage
=
496 le32_to_cpu(sbp
->s_r_segments_percentage
);
498 max_t(unsigned long, NILFS_MIN_NRSVSEGS
,
499 DIV_ROUND_UP(nilfs
->ns_nsegments
*
500 nilfs
->ns_r_segments_percentage
, 100));
501 nilfs
->ns_crc_seed
= le32_to_cpu(sbp
->s_crc_seed
);
505 static int nilfs_valid_sb(struct nilfs_super_block
*sbp
)
507 static unsigned char sum
[4];
508 const int sumoff
= offsetof(struct nilfs_super_block
, s_sum
);
512 if (!sbp
|| le16_to_cpu(sbp
->s_magic
) != NILFS_SUPER_MAGIC
)
514 bytes
= le16_to_cpu(sbp
->s_bytes
);
515 if (bytes
> BLOCK_SIZE
)
517 crc
= crc32_le(le32_to_cpu(sbp
->s_crc_seed
), (unsigned char *)sbp
,
519 crc
= crc32_le(crc
, sum
, 4);
520 crc
= crc32_le(crc
, (unsigned char *)sbp
+ sumoff
+ 4,
522 return crc
== le32_to_cpu(sbp
->s_sum
);
525 static int nilfs_sb2_bad_offset(struct nilfs_super_block
*sbp
, u64 offset
)
527 return offset
< ((le64_to_cpu(sbp
->s_nsegments
) *
528 le32_to_cpu(sbp
->s_blocks_per_segment
)) <<
529 (le32_to_cpu(sbp
->s_log_block_size
) + 10));
532 static void nilfs_release_super_block(struct the_nilfs
*nilfs
)
536 for (i
= 0; i
< 2; i
++) {
537 if (nilfs
->ns_sbp
[i
]) {
538 brelse(nilfs
->ns_sbh
[i
]);
539 nilfs
->ns_sbh
[i
] = NULL
;
540 nilfs
->ns_sbp
[i
] = NULL
;
545 void nilfs_fall_back_super_block(struct the_nilfs
*nilfs
)
547 brelse(nilfs
->ns_sbh
[0]);
548 nilfs
->ns_sbh
[0] = nilfs
->ns_sbh
[1];
549 nilfs
->ns_sbp
[0] = nilfs
->ns_sbp
[1];
550 nilfs
->ns_sbh
[1] = NULL
;
551 nilfs
->ns_sbp
[1] = NULL
;
554 void nilfs_swap_super_block(struct the_nilfs
*nilfs
)
556 struct buffer_head
*tsbh
= nilfs
->ns_sbh
[0];
557 struct nilfs_super_block
*tsbp
= nilfs
->ns_sbp
[0];
559 nilfs
->ns_sbh
[0] = nilfs
->ns_sbh
[1];
560 nilfs
->ns_sbp
[0] = nilfs
->ns_sbp
[1];
561 nilfs
->ns_sbh
[1] = tsbh
;
562 nilfs
->ns_sbp
[1] = tsbp
;
565 static int nilfs_load_super_block(struct the_nilfs
*nilfs
,
566 struct super_block
*sb
, int blocksize
,
567 struct nilfs_super_block
**sbpp
)
569 struct nilfs_super_block
**sbp
= nilfs
->ns_sbp
;
570 struct buffer_head
**sbh
= nilfs
->ns_sbh
;
571 u64 sb2off
= NILFS_SB2_OFFSET_BYTES(nilfs
->ns_bdev
->bd_inode
->i_size
);
572 int valid
[2], swp
= 0;
574 sbp
[0] = nilfs_read_super_block(sb
, NILFS_SB_OFFSET_BYTES
, blocksize
,
576 sbp
[1] = nilfs_read_super_block(sb
, sb2off
, blocksize
, &sbh
[1]);
580 printk(KERN_ERR
"NILFS: unable to read superblock\n");
584 "NILFS warning: unable to read primary superblock\n");
587 "NILFS warning: unable to read secondary superblock\n");
590 * Compare two super blocks and set 1 in swp if the secondary
591 * super block is valid and newer. Otherwise, set 0 in swp.
593 valid
[0] = nilfs_valid_sb(sbp
[0]);
594 valid
[1] = nilfs_valid_sb(sbp
[1]);
595 swp
= valid
[1] && (!valid
[0] ||
596 le64_to_cpu(sbp
[1]->s_last_cno
) >
597 le64_to_cpu(sbp
[0]->s_last_cno
));
599 if (valid
[swp
] && nilfs_sb2_bad_offset(sbp
[swp
], sb2off
)) {
606 nilfs_release_super_block(nilfs
);
607 printk(KERN_ERR
"NILFS: Can't find nilfs on dev %s.\n",
613 printk(KERN_WARNING
"NILFS warning: broken superblock. "
614 "using spare superblock.\n");
616 nilfs_swap_super_block(nilfs
);
618 nilfs
->ns_sbwcount
= 0;
619 nilfs
->ns_sbwtime
= le64_to_cpu(sbp
[0]->s_wtime
);
620 nilfs
->ns_prot_seq
= le64_to_cpu(sbp
[valid
[1] & !swp
]->s_last_seq
);
626 * init_nilfs - initialize a NILFS instance.
627 * @nilfs: the_nilfs structure
628 * @sbi: nilfs_sb_info
630 * @data: mount options
632 * init_nilfs() performs common initialization per block device (e.g.
633 * reading the super block, getting disk layout information, initializing
634 * shared fields in the_nilfs). It takes on some portion of the jobs
635 * typically done by a fill_super() routine. This division arises from
636 * the nature that multiple NILFS instances may be simultaneously
637 * mounted on a device.
638 * For multiple mounts on the same device, only the first mount
639 * invokes these tasks.
641 * Return Value: On success, 0 is returned. On error, a negative error
644 int init_nilfs(struct the_nilfs
*nilfs
, struct nilfs_sb_info
*sbi
, char *data
)
646 struct super_block
*sb
= sbi
->s_super
;
647 struct nilfs_super_block
*sbp
;
648 struct backing_dev_info
*bdi
;
652 down_write(&nilfs
->ns_sem
);
653 if (nilfs_init(nilfs
)) {
654 /* Load values from existing the_nilfs */
655 sbp
= nilfs
->ns_sbp
[0];
656 err
= nilfs_store_magic_and_option(sb
, sbp
, data
);
660 err
= nilfs_check_feature_compatibility(sb
, sbp
);
664 blocksize
= BLOCK_SIZE
<< le32_to_cpu(sbp
->s_log_block_size
);
665 if (sb
->s_blocksize
!= blocksize
&&
666 !sb_set_blocksize(sb
, blocksize
)) {
667 printk(KERN_ERR
"NILFS: blocksize %d unfit to device\n",
671 sb
->s_maxbytes
= nilfs_max_size(sb
->s_blocksize_bits
);
675 blocksize
= sb_min_blocksize(sb
, NILFS_MIN_BLOCK_SIZE
);
677 printk(KERN_ERR
"NILFS: unable to set blocksize\n");
681 err
= nilfs_load_super_block(nilfs
, sb
, blocksize
, &sbp
);
685 err
= nilfs_store_magic_and_option(sb
, sbp
, data
);
689 err
= nilfs_check_feature_compatibility(sb
, sbp
);
693 blocksize
= BLOCK_SIZE
<< le32_to_cpu(sbp
->s_log_block_size
);
694 if (blocksize
< NILFS_MIN_BLOCK_SIZE
||
695 blocksize
> NILFS_MAX_BLOCK_SIZE
) {
696 printk(KERN_ERR
"NILFS: couldn't mount because of unsupported "
697 "filesystem blocksize %d\n", blocksize
);
701 if (sb
->s_blocksize
!= blocksize
) {
702 int hw_blocksize
= bdev_logical_block_size(sb
->s_bdev
);
704 if (blocksize
< hw_blocksize
) {
706 "NILFS: blocksize %d too small for device "
707 "(sector-size = %d).\n",
708 blocksize
, hw_blocksize
);
712 nilfs_release_super_block(nilfs
);
713 sb_set_blocksize(sb
, blocksize
);
715 err
= nilfs_load_super_block(nilfs
, sb
, blocksize
, &sbp
);
718 /* not failed_sbh; sbh is released automatically
719 when reloading fails. */
721 nilfs
->ns_blocksize_bits
= sb
->s_blocksize_bits
;
722 nilfs
->ns_blocksize
= blocksize
;
724 err
= nilfs_store_disk_layout(nilfs
, sbp
);
728 sb
->s_maxbytes
= nilfs_max_size(sb
->s_blocksize_bits
);
730 nilfs
->ns_mount_state
= le16_to_cpu(sbp
->s_state
);
732 bdi
= nilfs
->ns_bdev
->bd_inode
->i_mapping
->backing_dev_info
;
733 nilfs
->ns_bdi
= bdi
? : &default_backing_dev_info
;
735 err
= nilfs_store_log_cursor(nilfs
, sbp
);
739 /* Initialize gcinode cache */
740 err
= nilfs_init_gccache(nilfs
);
744 set_nilfs_init(nilfs
);
747 up_write(&nilfs
->ns_sem
);
751 nilfs_release_super_block(nilfs
);
755 int nilfs_discard_segments(struct the_nilfs
*nilfs
, __u64
*segnump
,
758 sector_t seg_start
, seg_end
;
759 sector_t start
= 0, nblocks
= 0;
760 unsigned int sects_per_block
;
764 sects_per_block
= (1 << nilfs
->ns_blocksize_bits
) /
765 bdev_logical_block_size(nilfs
->ns_bdev
);
766 for (sn
= segnump
; sn
< segnump
+ nsegs
; sn
++) {
767 nilfs_get_segment_range(nilfs
, *sn
, &seg_start
, &seg_end
);
771 nblocks
= seg_end
- seg_start
+ 1;
772 } else if (start
+ nblocks
== seg_start
) {
773 nblocks
+= seg_end
- seg_start
+ 1;
775 ret
= blkdev_issue_discard(nilfs
->ns_bdev
,
776 start
* sects_per_block
,
777 nblocks
* sects_per_block
,
787 ret
= blkdev_issue_discard(nilfs
->ns_bdev
,
788 start
* sects_per_block
,
789 nblocks
* sects_per_block
,
791 BLKDEV_IFL_WAIT
| BLKDEV_IFL_BARRIER
);
795 int nilfs_count_free_blocks(struct the_nilfs
*nilfs
, sector_t
*nblocks
)
797 struct inode
*dat
= nilfs_dat_inode(nilfs
);
798 unsigned long ncleansegs
;
800 down_read(&NILFS_MDT(dat
)->mi_sem
); /* XXX */
801 ncleansegs
= nilfs_sufile_get_ncleansegs(nilfs
->ns_sufile
);
802 up_read(&NILFS_MDT(dat
)->mi_sem
); /* XXX */
803 *nblocks
= (sector_t
)ncleansegs
* nilfs
->ns_blocks_per_segment
;
807 int nilfs_near_disk_full(struct the_nilfs
*nilfs
)
809 unsigned long ncleansegs
, nincsegs
;
811 ncleansegs
= nilfs_sufile_get_ncleansegs(nilfs
->ns_sufile
);
812 nincsegs
= atomic_read(&nilfs
->ns_ndirtyblks
) /
813 nilfs
->ns_blocks_per_segment
+ 1;
815 return ncleansegs
<= nilfs
->ns_nrsvsegs
+ nincsegs
;
819 * nilfs_find_sbinfo - find existing nilfs_sb_info structure
820 * @nilfs: nilfs object
821 * @rw_mount: mount type (non-zero value for read/write mount)
822 * @cno: checkpoint number (zero for read-only mount)
824 * nilfs_find_sbinfo() returns the nilfs_sb_info structure which
825 * @rw_mount and @cno (in case of snapshots) matched. If no instance
826 * was found, NULL is returned. Although the super block instance can
827 * be unmounted after this function returns, the nilfs_sb_info struct
828 * is kept on memory until nilfs_put_sbinfo() is called.
830 struct nilfs_sb_info
*nilfs_find_sbinfo(struct the_nilfs
*nilfs
,
831 int rw_mount
, __u64 cno
)
833 struct nilfs_sb_info
*sbi
;
835 down_read(&nilfs
->ns_super_sem
);
837 * The SNAPSHOT flag and sb->s_flags are supposed to be
838 * protected with nilfs->ns_super_sem.
840 sbi
= nilfs
->ns_current
;
842 if (sbi
&& !(sbi
->s_super
->s_flags
& MS_RDONLY
))
843 goto found
; /* read/write mount */
846 } else if (cno
== 0) {
847 if (sbi
&& (sbi
->s_super
->s_flags
& MS_RDONLY
))
848 goto found
; /* read-only mount */
853 list_for_each_entry(sbi
, &nilfs
->ns_supers
, s_list
) {
854 if (nilfs_test_opt(sbi
, SNAPSHOT
) &&
855 sbi
->s_snapshot_cno
== cno
)
856 goto found
; /* snapshot mount */
859 up_read(&nilfs
->ns_super_sem
);
863 atomic_inc(&sbi
->s_count
);
864 up_read(&nilfs
->ns_super_sem
);
868 int nilfs_checkpoint_is_mounted(struct the_nilfs
*nilfs
, __u64 cno
,
871 struct nilfs_sb_info
*sbi
;
874 down_read(&nilfs
->ns_super_sem
);
875 if (cno
== 0 || cno
> nilfs
->ns_cno
)
878 list_for_each_entry(sbi
, &nilfs
->ns_supers
, s_list
) {
879 if (sbi
->s_snapshot_cno
== cno
&&
880 (!snapshot_mount
|| nilfs_test_opt(sbi
, SNAPSHOT
))) {
881 /* exclude read-only mounts */
886 /* for protecting recent checkpoints */
887 if (cno
>= nilfs_last_cno(nilfs
))
891 up_read(&nilfs
->ns_super_sem
);