]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/nilfs2/the_nilfs.c
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 void nilfs_set_last_segment(struct the_nilfs
*nilfs
,
42 sector_t start_blocknr
, u64 seq
, __u64 cno
)
44 spin_lock(&nilfs
->ns_last_segment_lock
);
45 nilfs
->ns_last_pseg
= start_blocknr
;
46 nilfs
->ns_last_seq
= seq
;
47 nilfs
->ns_last_cno
= cno
;
48 spin_unlock(&nilfs
->ns_last_segment_lock
);
52 * alloc_nilfs - allocate the_nilfs structure
53 * @bdev: block device to which the_nilfs is related
55 * alloc_nilfs() allocates memory for the_nilfs and
56 * initializes its reference count and locks.
58 * Return Value: On success, pointer to the_nilfs is returned.
59 * On error, NULL is returned.
61 static struct the_nilfs
*alloc_nilfs(struct block_device
*bdev
)
63 struct the_nilfs
*nilfs
;
65 nilfs
= kzalloc(sizeof(*nilfs
), GFP_KERNEL
);
69 nilfs
->ns_bdev
= bdev
;
70 atomic_set(&nilfs
->ns_count
, 1);
71 atomic_set(&nilfs
->ns_ndirtyblks
, 0);
72 init_rwsem(&nilfs
->ns_sem
);
73 init_rwsem(&nilfs
->ns_super_sem
);
74 mutex_init(&nilfs
->ns_mount_mutex
);
75 init_rwsem(&nilfs
->ns_writer_sem
);
76 INIT_LIST_HEAD(&nilfs
->ns_list
);
77 INIT_LIST_HEAD(&nilfs
->ns_supers
);
78 spin_lock_init(&nilfs
->ns_last_segment_lock
);
79 nilfs
->ns_gc_inodes_h
= NULL
;
80 init_rwsem(&nilfs
->ns_segctor_sem
);
86 * find_or_create_nilfs - find or create nilfs object
87 * @bdev: block device to which the_nilfs is related
89 * find_nilfs() looks up an existent nilfs object created on the
90 * device and gets the reference count of the object. If no nilfs object
91 * is found on the device, a new nilfs object is allocated.
93 * Return Value: On success, pointer to the nilfs object is returned.
94 * On error, NULL is returned.
96 struct the_nilfs
*find_or_create_nilfs(struct block_device
*bdev
)
98 struct the_nilfs
*nilfs
, *new = NULL
;
101 spin_lock(&nilfs_lock
);
102 list_for_each_entry(nilfs
, &nilfs_objects
, ns_list
) {
103 if (nilfs
->ns_bdev
== bdev
) {
105 spin_unlock(&nilfs_lock
);
108 return nilfs
; /* existing object */
112 list_add_tail(&new->ns_list
, &nilfs_objects
);
113 spin_unlock(&nilfs_lock
);
114 return new; /* new object */
116 spin_unlock(&nilfs_lock
);
118 new = alloc_nilfs(bdev
);
121 return NULL
; /* insufficient memory */
125 * put_nilfs - release a reference to the_nilfs
126 * @nilfs: the_nilfs structure to be released
128 * put_nilfs() decrements a reference counter of the_nilfs.
129 * If the reference count reaches zero, the_nilfs is freed.
131 void put_nilfs(struct the_nilfs
*nilfs
)
133 spin_lock(&nilfs_lock
);
134 if (!atomic_dec_and_test(&nilfs
->ns_count
)) {
135 spin_unlock(&nilfs_lock
);
138 list_del_init(&nilfs
->ns_list
);
139 spin_unlock(&nilfs_lock
);
142 * Increment of ns_count never occurs below because the caller
143 * of get_nilfs() holds at least one reference to the_nilfs.
144 * Thus its exclusion control is not required here.
148 if (nilfs_loaded(nilfs
)) {
149 nilfs_mdt_destroy(nilfs
->ns_sufile
);
150 nilfs_mdt_destroy(nilfs
->ns_cpfile
);
151 nilfs_mdt_destroy(nilfs
->ns_dat
);
152 nilfs_mdt_destroy(nilfs
->ns_gc_dat
);
154 if (nilfs_init(nilfs
)) {
155 nilfs_destroy_gccache(nilfs
);
156 brelse(nilfs
->ns_sbh
[0]);
157 brelse(nilfs
->ns_sbh
[1]);
162 static int nilfs_load_super_root(struct the_nilfs
*nilfs
,
163 struct nilfs_sb_info
*sbi
, sector_t sr_block
)
165 struct buffer_head
*bh_sr
;
166 struct nilfs_super_root
*raw_sr
;
167 struct nilfs_super_block
**sbp
= nilfs
->ns_sbp
;
168 unsigned dat_entry_size
, segment_usage_size
, checkpoint_size
;
172 err
= nilfs_read_super_root_block(sbi
->s_super
, sr_block
, &bh_sr
, 1);
176 down_read(&nilfs
->ns_sem
);
177 dat_entry_size
= le16_to_cpu(sbp
[0]->s_dat_entry_size
);
178 checkpoint_size
= le16_to_cpu(sbp
[0]->s_checkpoint_size
);
179 segment_usage_size
= le16_to_cpu(sbp
[0]->s_segment_usage_size
);
180 up_read(&nilfs
->ns_sem
);
182 inode_size
= nilfs
->ns_inode_size
;
185 nilfs
->ns_dat
= nilfs_dat_new(nilfs
, dat_entry_size
);
186 if (unlikely(!nilfs
->ns_dat
))
189 nilfs
->ns_gc_dat
= nilfs_dat_new(nilfs
, dat_entry_size
);
190 if (unlikely(!nilfs
->ns_gc_dat
))
193 nilfs
->ns_cpfile
= nilfs_cpfile_new(nilfs
, checkpoint_size
);
194 if (unlikely(!nilfs
->ns_cpfile
))
197 nilfs
->ns_sufile
= nilfs_sufile_new(nilfs
, segment_usage_size
);
198 if (unlikely(!nilfs
->ns_sufile
))
201 nilfs_mdt_set_shadow(nilfs
->ns_dat
, nilfs
->ns_gc_dat
);
203 err
= nilfs_dat_read(nilfs
->ns_dat
, (void *)bh_sr
->b_data
+
204 NILFS_SR_DAT_OFFSET(inode_size
));
208 err
= nilfs_cpfile_read(nilfs
->ns_cpfile
, (void *)bh_sr
->b_data
+
209 NILFS_SR_CPFILE_OFFSET(inode_size
));
213 err
= nilfs_sufile_read(nilfs
->ns_sufile
, (void *)bh_sr
->b_data
+
214 NILFS_SR_SUFILE_OFFSET(inode_size
));
218 raw_sr
= (struct nilfs_super_root
*)bh_sr
->b_data
;
219 nilfs
->ns_nongc_ctime
= le64_to_cpu(raw_sr
->sr_nongc_ctime
);
226 nilfs_mdt_destroy(nilfs
->ns_sufile
);
229 nilfs_mdt_destroy(nilfs
->ns_cpfile
);
232 nilfs_mdt_destroy(nilfs
->ns_gc_dat
);
235 nilfs_mdt_destroy(nilfs
->ns_dat
);
239 static void nilfs_init_recovery_info(struct nilfs_recovery_info
*ri
)
241 memset(ri
, 0, sizeof(*ri
));
242 INIT_LIST_HEAD(&ri
->ri_used_segments
);
245 static void nilfs_clear_recovery_info(struct nilfs_recovery_info
*ri
)
247 nilfs_dispose_segment_list(&ri
->ri_used_segments
);
251 * load_nilfs - load and recover the nilfs
252 * @nilfs: the_nilfs structure to be released
253 * @sbi: nilfs_sb_info used to recover past segment
255 * load_nilfs() searches and load the latest super root,
256 * attaches the last segment, and does recovery if needed.
257 * The caller must call this exclusively for simultaneous mounts.
259 int load_nilfs(struct the_nilfs
*nilfs
, struct nilfs_sb_info
*sbi
)
261 struct nilfs_recovery_info ri
;
262 unsigned int s_flags
= sbi
->s_super
->s_flags
;
263 int really_read_only
= bdev_read_only(nilfs
->ns_bdev
);
264 int valid_fs
= nilfs_valid_fs(nilfs
);
267 if (nilfs_loaded(nilfs
)) {
269 ((s_flags
& MS_RDONLY
) && nilfs_test_opt(sbi
, NORECOVERY
)))
271 printk(KERN_ERR
"NILFS: the filesystem is in an incomplete "
272 "recovery state.\n");
277 printk(KERN_WARNING
"NILFS warning: mounting unchecked fs\n");
278 if (s_flags
& MS_RDONLY
) {
279 printk(KERN_INFO
"NILFS: INFO: recovery "
280 "required for readonly filesystem.\n");
281 printk(KERN_INFO
"NILFS: write access will "
282 "be enabled during recovery.\n");
286 nilfs_init_recovery_info(&ri
);
288 err
= nilfs_search_super_root(nilfs
, sbi
, &ri
);
290 printk(KERN_ERR
"NILFS: error searching super root.\n");
294 err
= nilfs_load_super_root(nilfs
, sbi
, ri
.ri_super_root
);
296 printk(KERN_ERR
"NILFS: error loading super root.\n");
303 if (s_flags
& MS_RDONLY
) {
304 if (nilfs_test_opt(sbi
, NORECOVERY
)) {
305 printk(KERN_INFO
"NILFS: norecovery option specified. "
306 "skipping roll-forward recovery\n");
309 if (really_read_only
) {
310 printk(KERN_ERR
"NILFS: write access "
311 "unavailable, cannot proceed.\n");
315 sbi
->s_super
->s_flags
&= ~MS_RDONLY
;
316 } else if (nilfs_test_opt(sbi
, NORECOVERY
)) {
317 printk(KERN_ERR
"NILFS: recovery cancelled because norecovery "
318 "option was specified for a read/write mount\n");
323 err
= nilfs_recover_logical_segments(nilfs
, sbi
, &ri
);
327 down_write(&nilfs
->ns_sem
);
328 nilfs
->ns_mount_state
|= NILFS_VALID_FS
;
329 nilfs
->ns_sbp
[0]->s_state
= cpu_to_le16(nilfs
->ns_mount_state
);
330 err
= nilfs_commit_super(sbi
, 1);
331 up_write(&nilfs
->ns_sem
);
334 printk(KERN_ERR
"NILFS: failed to update super block. "
335 "recovery unfinished.\n");
338 printk(KERN_INFO
"NILFS: recovery complete.\n");
341 set_nilfs_loaded(nilfs
);
342 nilfs_clear_recovery_info(&ri
);
343 sbi
->s_super
->s_flags
= s_flags
;
347 nilfs_mdt_destroy(nilfs
->ns_cpfile
);
348 nilfs_mdt_destroy(nilfs
->ns_sufile
);
349 nilfs_mdt_destroy(nilfs
->ns_dat
);
352 nilfs_clear_recovery_info(&ri
);
353 sbi
->s_super
->s_flags
= s_flags
;
357 static unsigned long long nilfs_max_size(unsigned int blkbits
)
359 unsigned int max_bits
;
360 unsigned long long res
= MAX_LFS_FILESIZE
; /* page cache limit */
362 max_bits
= blkbits
+ NILFS_BMAP_KEY_BIT
; /* bmap size limit */
364 res
= min_t(unsigned long long, res
, (1ULL << max_bits
) - 1);
368 static int nilfs_store_disk_layout(struct the_nilfs
*nilfs
,
369 struct nilfs_super_block
*sbp
)
371 if (le32_to_cpu(sbp
->s_rev_level
) != NILFS_CURRENT_REV
) {
372 printk(KERN_ERR
"NILFS: revision mismatch "
373 "(superblock rev.=%d.%d, current rev.=%d.%d). "
374 "Please check the version of mkfs.nilfs.\n",
375 le32_to_cpu(sbp
->s_rev_level
),
376 le16_to_cpu(sbp
->s_minor_rev_level
),
377 NILFS_CURRENT_REV
, NILFS_MINOR_REV
);
380 nilfs
->ns_sbsize
= le16_to_cpu(sbp
->s_bytes
);
381 if (nilfs
->ns_sbsize
> BLOCK_SIZE
)
384 nilfs
->ns_inode_size
= le16_to_cpu(sbp
->s_inode_size
);
385 nilfs
->ns_first_ino
= le32_to_cpu(sbp
->s_first_ino
);
387 nilfs
->ns_blocks_per_segment
= le32_to_cpu(sbp
->s_blocks_per_segment
);
388 if (nilfs
->ns_blocks_per_segment
< NILFS_SEG_MIN_BLOCKS
) {
389 printk(KERN_ERR
"NILFS: too short segment.\n");
393 nilfs
->ns_first_data_block
= le64_to_cpu(sbp
->s_first_data_block
);
394 nilfs
->ns_nsegments
= le64_to_cpu(sbp
->s_nsegments
);
395 nilfs
->ns_r_segments_percentage
=
396 le32_to_cpu(sbp
->s_r_segments_percentage
);
398 max_t(unsigned long, NILFS_MIN_NRSVSEGS
,
399 DIV_ROUND_UP(nilfs
->ns_nsegments
*
400 nilfs
->ns_r_segments_percentage
, 100));
401 nilfs
->ns_crc_seed
= le32_to_cpu(sbp
->s_crc_seed
);
405 static int nilfs_valid_sb(struct nilfs_super_block
*sbp
)
407 static unsigned char sum
[4];
408 const int sumoff
= offsetof(struct nilfs_super_block
, s_sum
);
412 if (!sbp
|| le16_to_cpu(sbp
->s_magic
) != NILFS_SUPER_MAGIC
)
414 bytes
= le16_to_cpu(sbp
->s_bytes
);
415 if (bytes
> BLOCK_SIZE
)
417 crc
= crc32_le(le32_to_cpu(sbp
->s_crc_seed
), (unsigned char *)sbp
,
419 crc
= crc32_le(crc
, sum
, 4);
420 crc
= crc32_le(crc
, (unsigned char *)sbp
+ sumoff
+ 4,
422 return crc
== le32_to_cpu(sbp
->s_sum
);
425 static int nilfs_sb2_bad_offset(struct nilfs_super_block
*sbp
, u64 offset
)
427 return offset
< ((le64_to_cpu(sbp
->s_nsegments
) *
428 le32_to_cpu(sbp
->s_blocks_per_segment
)) <<
429 (le32_to_cpu(sbp
->s_log_block_size
) + 10));
432 static void nilfs_release_super_block(struct the_nilfs
*nilfs
)
436 for (i
= 0; i
< 2; i
++) {
437 if (nilfs
->ns_sbp
[i
]) {
438 brelse(nilfs
->ns_sbh
[i
]);
439 nilfs
->ns_sbh
[i
] = NULL
;
440 nilfs
->ns_sbp
[i
] = NULL
;
445 void nilfs_fall_back_super_block(struct the_nilfs
*nilfs
)
447 brelse(nilfs
->ns_sbh
[0]);
448 nilfs
->ns_sbh
[0] = nilfs
->ns_sbh
[1];
449 nilfs
->ns_sbp
[0] = nilfs
->ns_sbp
[1];
450 nilfs
->ns_sbh
[1] = NULL
;
451 nilfs
->ns_sbp
[1] = NULL
;
454 void nilfs_swap_super_block(struct the_nilfs
*nilfs
)
456 struct buffer_head
*tsbh
= nilfs
->ns_sbh
[0];
457 struct nilfs_super_block
*tsbp
= nilfs
->ns_sbp
[0];
459 nilfs
->ns_sbh
[0] = nilfs
->ns_sbh
[1];
460 nilfs
->ns_sbp
[0] = nilfs
->ns_sbp
[1];
461 nilfs
->ns_sbh
[1] = tsbh
;
462 nilfs
->ns_sbp
[1] = tsbp
;
465 static int nilfs_load_super_block(struct the_nilfs
*nilfs
,
466 struct super_block
*sb
, int blocksize
,
467 struct nilfs_super_block
**sbpp
)
469 struct nilfs_super_block
**sbp
= nilfs
->ns_sbp
;
470 struct buffer_head
**sbh
= nilfs
->ns_sbh
;
471 u64 sb2off
= NILFS_SB2_OFFSET_BYTES(nilfs
->ns_bdev
->bd_inode
->i_size
);
472 int valid
[2], swp
= 0;
474 sbp
[0] = nilfs_read_super_block(sb
, NILFS_SB_OFFSET_BYTES
, blocksize
,
476 sbp
[1] = nilfs_read_super_block(sb
, sb2off
, blocksize
, &sbh
[1]);
480 printk(KERN_ERR
"NILFS: unable to read superblock\n");
484 "NILFS warning: unable to read primary superblock\n");
487 "NILFS warning: unable to read secondary superblock\n");
490 * Compare two super blocks and set 1 in swp if the secondary
491 * super block is valid and newer. Otherwise, set 0 in swp.
493 valid
[0] = nilfs_valid_sb(sbp
[0]);
494 valid
[1] = nilfs_valid_sb(sbp
[1]);
495 swp
= valid
[1] && (!valid
[0] ||
496 le64_to_cpu(sbp
[1]->s_last_cno
) >
497 le64_to_cpu(sbp
[0]->s_last_cno
));
499 if (valid
[swp
] && nilfs_sb2_bad_offset(sbp
[swp
], sb2off
)) {
506 nilfs_release_super_block(nilfs
);
507 printk(KERN_ERR
"NILFS: Can't find nilfs on dev %s.\n",
513 printk(KERN_WARNING
"NILFS warning: broken superblock. "
514 "using spare superblock.\n");
515 nilfs_swap_super_block(nilfs
);
518 nilfs
->ns_sbwtime
[0] = le64_to_cpu(sbp
[0]->s_wtime
);
519 nilfs
->ns_sbwtime
[1] = valid
[!swp
] ? le64_to_cpu(sbp
[1]->s_wtime
) : 0;
520 nilfs
->ns_prot_seq
= le64_to_cpu(sbp
[valid
[1] & !swp
]->s_last_seq
);
526 * init_nilfs - initialize a NILFS instance.
527 * @nilfs: the_nilfs structure
528 * @sbi: nilfs_sb_info
530 * @data: mount options
532 * init_nilfs() performs common initialization per block device (e.g.
533 * reading the super block, getting disk layout information, initializing
534 * shared fields in the_nilfs). It takes on some portion of the jobs
535 * typically done by a fill_super() routine. This division arises from
536 * the nature that multiple NILFS instances may be simultaneously
537 * mounted on a device.
538 * For multiple mounts on the same device, only the first mount
539 * invokes these tasks.
541 * Return Value: On success, 0 is returned. On error, a negative error
544 int init_nilfs(struct the_nilfs
*nilfs
, struct nilfs_sb_info
*sbi
, char *data
)
546 struct super_block
*sb
= sbi
->s_super
;
547 struct nilfs_super_block
*sbp
;
548 struct backing_dev_info
*bdi
;
552 down_write(&nilfs
->ns_sem
);
553 if (nilfs_init(nilfs
)) {
554 /* Load values from existing the_nilfs */
555 sbp
= nilfs
->ns_sbp
[0];
556 err
= nilfs_store_magic_and_option(sb
, sbp
, data
);
560 blocksize
= BLOCK_SIZE
<< le32_to_cpu(sbp
->s_log_block_size
);
561 if (sb
->s_blocksize
!= blocksize
&&
562 !sb_set_blocksize(sb
, blocksize
)) {
563 printk(KERN_ERR
"NILFS: blocksize %d unfit to device\n",
567 sb
->s_maxbytes
= nilfs_max_size(sb
->s_blocksize_bits
);
571 blocksize
= sb_min_blocksize(sb
, BLOCK_SIZE
);
573 printk(KERN_ERR
"NILFS: unable to set blocksize\n");
577 err
= nilfs_load_super_block(nilfs
, sb
, blocksize
, &sbp
);
581 err
= nilfs_store_magic_and_option(sb
, sbp
, data
);
585 blocksize
= BLOCK_SIZE
<< le32_to_cpu(sbp
->s_log_block_size
);
586 if (sb
->s_blocksize
!= blocksize
) {
587 int hw_blocksize
= bdev_logical_block_size(sb
->s_bdev
);
589 if (blocksize
< hw_blocksize
) {
591 "NILFS: blocksize %d too small for device "
592 "(sector-size = %d).\n",
593 blocksize
, hw_blocksize
);
597 nilfs_release_super_block(nilfs
);
598 sb_set_blocksize(sb
, blocksize
);
600 err
= nilfs_load_super_block(nilfs
, sb
, blocksize
, &sbp
);
603 /* not failed_sbh; sbh is released automatically
604 when reloading fails. */
606 nilfs
->ns_blocksize_bits
= sb
->s_blocksize_bits
;
608 err
= nilfs_store_disk_layout(nilfs
, sbp
);
612 sb
->s_maxbytes
= nilfs_max_size(sb
->s_blocksize_bits
);
614 nilfs
->ns_mount_state
= le16_to_cpu(sbp
->s_state
);
616 bdi
= nilfs
->ns_bdev
->bd_inode
->i_mapping
->backing_dev_info
;
617 nilfs
->ns_bdi
= bdi
? : &default_backing_dev_info
;
619 /* Finding last segment */
620 nilfs
->ns_last_pseg
= le64_to_cpu(sbp
->s_last_pseg
);
621 nilfs
->ns_last_cno
= le64_to_cpu(sbp
->s_last_cno
);
622 nilfs
->ns_last_seq
= le64_to_cpu(sbp
->s_last_seq
);
624 nilfs
->ns_seg_seq
= nilfs
->ns_last_seq
;
626 nilfs_get_segnum_of_block(nilfs
, nilfs
->ns_last_pseg
);
627 nilfs
->ns_cno
= nilfs
->ns_last_cno
+ 1;
628 if (nilfs
->ns_segnum
>= nilfs
->ns_nsegments
) {
629 printk(KERN_ERR
"NILFS invalid last segment number.\n");
634 nilfs
->ns_free_segments_count
=
635 nilfs
->ns_nsegments
- (nilfs
->ns_segnum
+ 1);
637 /* Initialize gcinode cache */
638 err
= nilfs_init_gccache(nilfs
);
642 set_nilfs_init(nilfs
);
645 up_write(&nilfs
->ns_sem
);
649 nilfs_release_super_block(nilfs
);
653 int nilfs_discard_segments(struct the_nilfs
*nilfs
, __u64
*segnump
,
656 sector_t seg_start
, seg_end
;
657 sector_t start
= 0, nblocks
= 0;
658 unsigned int sects_per_block
;
662 sects_per_block
= (1 << nilfs
->ns_blocksize_bits
) /
663 bdev_logical_block_size(nilfs
->ns_bdev
);
664 for (sn
= segnump
; sn
< segnump
+ nsegs
; sn
++) {
665 nilfs_get_segment_range(nilfs
, *sn
, &seg_start
, &seg_end
);
669 nblocks
= seg_end
- seg_start
+ 1;
670 } else if (start
+ nblocks
== seg_start
) {
671 nblocks
+= seg_end
- seg_start
+ 1;
673 ret
= blkdev_issue_discard(nilfs
->ns_bdev
,
674 start
* sects_per_block
,
675 nblocks
* sects_per_block
,
684 ret
= blkdev_issue_discard(nilfs
->ns_bdev
,
685 start
* sects_per_block
,
686 nblocks
* sects_per_block
,
687 GFP_NOFS
, BLKDEV_IFL_BARRIER
);
691 int nilfs_count_free_blocks(struct the_nilfs
*nilfs
, sector_t
*nblocks
)
693 struct inode
*dat
= nilfs_dat_inode(nilfs
);
694 unsigned long ncleansegs
;
696 down_read(&NILFS_MDT(dat
)->mi_sem
); /* XXX */
697 ncleansegs
= nilfs_sufile_get_ncleansegs(nilfs
->ns_sufile
);
698 up_read(&NILFS_MDT(dat
)->mi_sem
); /* XXX */
699 *nblocks
= (sector_t
)ncleansegs
* nilfs
->ns_blocks_per_segment
;
703 int nilfs_near_disk_full(struct the_nilfs
*nilfs
)
705 unsigned long ncleansegs
, nincsegs
;
707 ncleansegs
= nilfs_sufile_get_ncleansegs(nilfs
->ns_sufile
);
708 nincsegs
= atomic_read(&nilfs
->ns_ndirtyblks
) /
709 nilfs
->ns_blocks_per_segment
+ 1;
711 return ncleansegs
<= nilfs
->ns_nrsvsegs
+ nincsegs
;
715 * nilfs_find_sbinfo - find existing nilfs_sb_info structure
716 * @nilfs: nilfs object
717 * @rw_mount: mount type (non-zero value for read/write mount)
718 * @cno: checkpoint number (zero for read-only mount)
720 * nilfs_find_sbinfo() returns the nilfs_sb_info structure which
721 * @rw_mount and @cno (in case of snapshots) matched. If no instance
722 * was found, NULL is returned. Although the super block instance can
723 * be unmounted after this function returns, the nilfs_sb_info struct
724 * is kept on memory until nilfs_put_sbinfo() is called.
726 struct nilfs_sb_info
*nilfs_find_sbinfo(struct the_nilfs
*nilfs
,
727 int rw_mount
, __u64 cno
)
729 struct nilfs_sb_info
*sbi
;
731 down_read(&nilfs
->ns_super_sem
);
733 * The SNAPSHOT flag and sb->s_flags are supposed to be
734 * protected with nilfs->ns_super_sem.
736 sbi
= nilfs
->ns_current
;
738 if (sbi
&& !(sbi
->s_super
->s_flags
& MS_RDONLY
))
739 goto found
; /* read/write mount */
742 } else if (cno
== 0) {
743 if (sbi
&& (sbi
->s_super
->s_flags
& MS_RDONLY
))
744 goto found
; /* read-only mount */
749 list_for_each_entry(sbi
, &nilfs
->ns_supers
, s_list
) {
750 if (nilfs_test_opt(sbi
, SNAPSHOT
) &&
751 sbi
->s_snapshot_cno
== cno
)
752 goto found
; /* snapshot mount */
755 up_read(&nilfs
->ns_super_sem
);
759 atomic_inc(&sbi
->s_count
);
760 up_read(&nilfs
->ns_super_sem
);
764 int nilfs_checkpoint_is_mounted(struct the_nilfs
*nilfs
, __u64 cno
,
767 struct nilfs_sb_info
*sbi
;
770 down_read(&nilfs
->ns_super_sem
);
771 if (cno
== 0 || cno
> nilfs
->ns_cno
)
774 list_for_each_entry(sbi
, &nilfs
->ns_supers
, s_list
) {
775 if (sbi
->s_snapshot_cno
== cno
&&
776 (!snapshot_mount
|| nilfs_test_opt(sbi
, SNAPSHOT
))) {
777 /* exclude read-only mounts */
782 /* for protecting recent checkpoints */
783 if (cno
>= nilfs_last_cno(nilfs
))
787 up_read(&nilfs
->ns_super_sem
);