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
;
50 spin_unlock(&nilfs
->ns_last_segment_lock
);
54 * alloc_nilfs - allocate the_nilfs structure
55 * @bdev: block device to which the_nilfs is related
57 * alloc_nilfs() allocates memory for the_nilfs and
58 * initializes its reference count and locks.
60 * Return Value: On success, pointer to the_nilfs is returned.
61 * On error, NULL is returned.
63 static struct the_nilfs
*alloc_nilfs(struct block_device
*bdev
)
65 struct the_nilfs
*nilfs
;
67 nilfs
= kzalloc(sizeof(*nilfs
), GFP_KERNEL
);
71 nilfs
->ns_bdev
= bdev
;
72 atomic_set(&nilfs
->ns_count
, 1);
73 atomic_set(&nilfs
->ns_ndirtyblks
, 0);
74 init_rwsem(&nilfs
->ns_sem
);
75 init_rwsem(&nilfs
->ns_super_sem
);
76 mutex_init(&nilfs
->ns_mount_mutex
);
77 init_rwsem(&nilfs
->ns_writer_sem
);
78 INIT_LIST_HEAD(&nilfs
->ns_list
);
79 INIT_LIST_HEAD(&nilfs
->ns_supers
);
80 spin_lock_init(&nilfs
->ns_last_segment_lock
);
81 nilfs
->ns_gc_inodes_h
= NULL
;
82 init_rwsem(&nilfs
->ns_segctor_sem
);
88 * find_or_create_nilfs - find or create nilfs object
89 * @bdev: block device to which the_nilfs is related
91 * find_nilfs() looks up an existent nilfs object created on the
92 * device and gets the reference count of the object. If no nilfs object
93 * is found on the device, a new nilfs object is allocated.
95 * Return Value: On success, pointer to the nilfs object is returned.
96 * On error, NULL is returned.
98 struct the_nilfs
*find_or_create_nilfs(struct block_device
*bdev
)
100 struct the_nilfs
*nilfs
, *new = NULL
;
103 spin_lock(&nilfs_lock
);
104 list_for_each_entry(nilfs
, &nilfs_objects
, ns_list
) {
105 if (nilfs
->ns_bdev
== bdev
) {
107 spin_unlock(&nilfs_lock
);
110 return nilfs
; /* existing object */
114 list_add_tail(&new->ns_list
, &nilfs_objects
);
115 spin_unlock(&nilfs_lock
);
116 return new; /* new object */
118 spin_unlock(&nilfs_lock
);
120 new = alloc_nilfs(bdev
);
123 return NULL
; /* insufficient memory */
127 * put_nilfs - release a reference to the_nilfs
128 * @nilfs: the_nilfs structure to be released
130 * put_nilfs() decrements a reference counter of the_nilfs.
131 * If the reference count reaches zero, the_nilfs is freed.
133 void put_nilfs(struct the_nilfs
*nilfs
)
135 spin_lock(&nilfs_lock
);
136 if (!atomic_dec_and_test(&nilfs
->ns_count
)) {
137 spin_unlock(&nilfs_lock
);
140 list_del_init(&nilfs
->ns_list
);
141 spin_unlock(&nilfs_lock
);
144 * Increment of ns_count never occurs below because the caller
145 * of get_nilfs() holds at least one reference to the_nilfs.
146 * Thus its exclusion control is not required here.
150 if (nilfs_loaded(nilfs
)) {
151 nilfs_mdt_destroy(nilfs
->ns_sufile
);
152 nilfs_mdt_destroy(nilfs
->ns_cpfile
);
153 nilfs_mdt_destroy(nilfs
->ns_dat
);
154 nilfs_mdt_destroy(nilfs
->ns_gc_dat
);
156 if (nilfs_init(nilfs
)) {
157 nilfs_destroy_gccache(nilfs
);
158 brelse(nilfs
->ns_sbh
[0]);
159 brelse(nilfs
->ns_sbh
[1]);
164 static int nilfs_load_super_root(struct the_nilfs
*nilfs
, sector_t sr_block
)
166 struct buffer_head
*bh_sr
;
167 struct nilfs_super_root
*raw_sr
;
168 struct nilfs_super_block
**sbp
= nilfs
->ns_sbp
;
169 unsigned dat_entry_size
, segment_usage_size
, checkpoint_size
;
173 err
= nilfs_read_super_root_block(nilfs
, sr_block
, &bh_sr
, 1);
177 down_read(&nilfs
->ns_sem
);
178 dat_entry_size
= le16_to_cpu(sbp
[0]->s_dat_entry_size
);
179 checkpoint_size
= le16_to_cpu(sbp
[0]->s_checkpoint_size
);
180 segment_usage_size
= le16_to_cpu(sbp
[0]->s_segment_usage_size
);
181 up_read(&nilfs
->ns_sem
);
183 inode_size
= nilfs
->ns_inode_size
;
186 nilfs
->ns_dat
= nilfs_dat_new(nilfs
, dat_entry_size
);
187 if (unlikely(!nilfs
->ns_dat
))
190 nilfs
->ns_gc_dat
= nilfs_dat_new(nilfs
, dat_entry_size
);
191 if (unlikely(!nilfs
->ns_gc_dat
))
194 nilfs
->ns_cpfile
= nilfs_cpfile_new(nilfs
, checkpoint_size
);
195 if (unlikely(!nilfs
->ns_cpfile
))
198 nilfs
->ns_sufile
= nilfs_sufile_new(nilfs
, segment_usage_size
);
199 if (unlikely(!nilfs
->ns_sufile
))
202 nilfs_mdt_set_shadow(nilfs
->ns_dat
, nilfs
->ns_gc_dat
);
204 err
= nilfs_dat_read(nilfs
->ns_dat
, (void *)bh_sr
->b_data
+
205 NILFS_SR_DAT_OFFSET(inode_size
));
209 err
= nilfs_cpfile_read(nilfs
->ns_cpfile
, (void *)bh_sr
->b_data
+
210 NILFS_SR_CPFILE_OFFSET(inode_size
));
214 err
= nilfs_sufile_read(nilfs
->ns_sufile
, (void *)bh_sr
->b_data
+
215 NILFS_SR_SUFILE_OFFSET(inode_size
));
219 raw_sr
= (struct nilfs_super_root
*)bh_sr
->b_data
;
220 nilfs
->ns_nongc_ctime
= le64_to_cpu(raw_sr
->sr_nongc_ctime
);
227 nilfs_mdt_destroy(nilfs
->ns_sufile
);
230 nilfs_mdt_destroy(nilfs
->ns_cpfile
);
233 nilfs_mdt_destroy(nilfs
->ns_gc_dat
);
236 nilfs_mdt_destroy(nilfs
->ns_dat
);
240 static void nilfs_init_recovery_info(struct nilfs_recovery_info
*ri
)
242 memset(ri
, 0, sizeof(*ri
));
243 INIT_LIST_HEAD(&ri
->ri_used_segments
);
246 static void nilfs_clear_recovery_info(struct nilfs_recovery_info
*ri
)
248 nilfs_dispose_segment_list(&ri
->ri_used_segments
);
252 * nilfs_store_log_cursor - load log cursor from a super block
253 * @nilfs: nilfs object
254 * @sbp: buffer storing super block to be read
256 * nilfs_store_log_cursor() reads the last position of the log
257 * containing a super root from a given super block, and initializes
258 * relevant information on the nilfs object preparatory for log
259 * scanning and recovery.
261 static int nilfs_store_log_cursor(struct the_nilfs
*nilfs
,
262 struct nilfs_super_block
*sbp
)
266 nilfs
->ns_last_pseg
= le64_to_cpu(sbp
->s_last_pseg
);
267 nilfs
->ns_last_cno
= le64_to_cpu(sbp
->s_last_cno
);
268 nilfs
->ns_last_seq
= le64_to_cpu(sbp
->s_last_seq
);
270 nilfs
->ns_seg_seq
= nilfs
->ns_last_seq
;
272 nilfs_get_segnum_of_block(nilfs
, nilfs
->ns_last_pseg
);
273 nilfs
->ns_cno
= nilfs
->ns_last_cno
+ 1;
274 if (nilfs
->ns_segnum
>= nilfs
->ns_nsegments
) {
275 printk(KERN_ERR
"NILFS invalid last segment number.\n");
282 * load_nilfs - load and recover the nilfs
283 * @nilfs: the_nilfs structure to be released
284 * @sbi: nilfs_sb_info used to recover past segment
286 * load_nilfs() searches and load the latest super root,
287 * attaches the last segment, and does recovery if needed.
288 * The caller must call this exclusively for simultaneous mounts.
290 int load_nilfs(struct the_nilfs
*nilfs
, struct nilfs_sb_info
*sbi
)
292 struct nilfs_recovery_info ri
;
293 unsigned int s_flags
= sbi
->s_super
->s_flags
;
294 int really_read_only
= bdev_read_only(nilfs
->ns_bdev
);
295 int valid_fs
= nilfs_valid_fs(nilfs
);
298 if (nilfs_loaded(nilfs
)) {
300 ((s_flags
& MS_RDONLY
) && nilfs_test_opt(sbi
, NORECOVERY
)))
302 printk(KERN_ERR
"NILFS: the filesystem is in an incomplete "
303 "recovery state.\n");
308 printk(KERN_WARNING
"NILFS warning: mounting unchecked fs\n");
309 if (s_flags
& MS_RDONLY
) {
310 printk(KERN_INFO
"NILFS: INFO: recovery "
311 "required for readonly filesystem.\n");
312 printk(KERN_INFO
"NILFS: write access will "
313 "be enabled during recovery.\n");
317 nilfs_init_recovery_info(&ri
);
319 err
= nilfs_search_super_root(nilfs
, &ri
);
321 struct nilfs_super_block
**sbp
= nilfs
->ns_sbp
;
327 if (!nilfs_valid_sb(sbp
[1])) {
329 "NILFS warning: unable to fall back to spare"
334 "NILFS: try rollback from an earlier position\n");
337 * restore super block with its spare and reconfigure
338 * relevant states of the nilfs object.
340 memcpy(sbp
[0], sbp
[1], nilfs
->ns_sbsize
);
341 nilfs
->ns_crc_seed
= le32_to_cpu(sbp
[0]->s_crc_seed
);
342 nilfs
->ns_sbwtime
= le64_to_cpu(sbp
[0]->s_wtime
);
344 /* verify consistency between two super blocks */
345 blocksize
= BLOCK_SIZE
<< le32_to_cpu(sbp
[0]->s_log_block_size
);
346 if (blocksize
!= nilfs
->ns_blocksize
) {
348 "NILFS warning: blocksize differs between "
349 "two super blocks (%d != %d)\n",
350 blocksize
, nilfs
->ns_blocksize
);
354 err
= nilfs_store_log_cursor(nilfs
, sbp
[0]);
358 /* drop clean flag to allow roll-forward and recovery */
359 nilfs
->ns_mount_state
&= ~NILFS_VALID_FS
;
362 err
= nilfs_search_super_root(nilfs
, &ri
);
367 err
= nilfs_load_super_root(nilfs
, ri
.ri_super_root
);
369 printk(KERN_ERR
"NILFS: error loading super root.\n");
376 if (s_flags
& MS_RDONLY
) {
377 if (nilfs_test_opt(sbi
, NORECOVERY
)) {
378 printk(KERN_INFO
"NILFS: norecovery option specified. "
379 "skipping roll-forward recovery\n");
382 if (really_read_only
) {
383 printk(KERN_ERR
"NILFS: write access "
384 "unavailable, cannot proceed.\n");
388 sbi
->s_super
->s_flags
&= ~MS_RDONLY
;
389 } else if (nilfs_test_opt(sbi
, NORECOVERY
)) {
390 printk(KERN_ERR
"NILFS: recovery cancelled because norecovery "
391 "option was specified for a read/write mount\n");
396 err
= nilfs_salvage_orphan_logs(nilfs
, sbi
, &ri
);
400 down_write(&nilfs
->ns_sem
);
401 nilfs
->ns_mount_state
|= NILFS_VALID_FS
; /* set "clean" flag */
402 err
= nilfs_cleanup_super(sbi
);
403 up_write(&nilfs
->ns_sem
);
406 printk(KERN_ERR
"NILFS: failed to update super block. "
407 "recovery unfinished.\n");
410 printk(KERN_INFO
"NILFS: recovery complete.\n");
413 set_nilfs_loaded(nilfs
);
414 nilfs_clear_recovery_info(&ri
);
415 sbi
->s_super
->s_flags
= s_flags
;
419 printk(KERN_ERR
"NILFS: error searching super root.\n");
423 nilfs_mdt_destroy(nilfs
->ns_cpfile
);
424 nilfs_mdt_destroy(nilfs
->ns_sufile
);
425 nilfs_mdt_destroy(nilfs
->ns_dat
);
428 nilfs_clear_recovery_info(&ri
);
429 sbi
->s_super
->s_flags
= s_flags
;
433 static unsigned long long nilfs_max_size(unsigned int blkbits
)
435 unsigned int max_bits
;
436 unsigned long long res
= MAX_LFS_FILESIZE
; /* page cache limit */
438 max_bits
= blkbits
+ NILFS_BMAP_KEY_BIT
; /* bmap size limit */
440 res
= min_t(unsigned long long, res
, (1ULL << max_bits
) - 1);
444 static int nilfs_store_disk_layout(struct the_nilfs
*nilfs
,
445 struct nilfs_super_block
*sbp
)
447 if (le32_to_cpu(sbp
->s_rev_level
) != NILFS_CURRENT_REV
) {
448 printk(KERN_ERR
"NILFS: revision mismatch "
449 "(superblock rev.=%d.%d, current rev.=%d.%d). "
450 "Please check the version of mkfs.nilfs.\n",
451 le32_to_cpu(sbp
->s_rev_level
),
452 le16_to_cpu(sbp
->s_minor_rev_level
),
453 NILFS_CURRENT_REV
, NILFS_MINOR_REV
);
456 nilfs
->ns_sbsize
= le16_to_cpu(sbp
->s_bytes
);
457 if (nilfs
->ns_sbsize
> BLOCK_SIZE
)
460 nilfs
->ns_inode_size
= le16_to_cpu(sbp
->s_inode_size
);
461 nilfs
->ns_first_ino
= le32_to_cpu(sbp
->s_first_ino
);
463 nilfs
->ns_blocks_per_segment
= le32_to_cpu(sbp
->s_blocks_per_segment
);
464 if (nilfs
->ns_blocks_per_segment
< NILFS_SEG_MIN_BLOCKS
) {
465 printk(KERN_ERR
"NILFS: too short segment.\n");
469 nilfs
->ns_first_data_block
= le64_to_cpu(sbp
->s_first_data_block
);
470 nilfs
->ns_nsegments
= le64_to_cpu(sbp
->s_nsegments
);
471 nilfs
->ns_r_segments_percentage
=
472 le32_to_cpu(sbp
->s_r_segments_percentage
);
474 max_t(unsigned long, NILFS_MIN_NRSVSEGS
,
475 DIV_ROUND_UP(nilfs
->ns_nsegments
*
476 nilfs
->ns_r_segments_percentage
, 100));
477 nilfs
->ns_crc_seed
= le32_to_cpu(sbp
->s_crc_seed
);
481 static int nilfs_valid_sb(struct nilfs_super_block
*sbp
)
483 static unsigned char sum
[4];
484 const int sumoff
= offsetof(struct nilfs_super_block
, s_sum
);
488 if (!sbp
|| le16_to_cpu(sbp
->s_magic
) != NILFS_SUPER_MAGIC
)
490 bytes
= le16_to_cpu(sbp
->s_bytes
);
491 if (bytes
> BLOCK_SIZE
)
493 crc
= crc32_le(le32_to_cpu(sbp
->s_crc_seed
), (unsigned char *)sbp
,
495 crc
= crc32_le(crc
, sum
, 4);
496 crc
= crc32_le(crc
, (unsigned char *)sbp
+ sumoff
+ 4,
498 return crc
== le32_to_cpu(sbp
->s_sum
);
501 static int nilfs_sb2_bad_offset(struct nilfs_super_block
*sbp
, u64 offset
)
503 return offset
< ((le64_to_cpu(sbp
->s_nsegments
) *
504 le32_to_cpu(sbp
->s_blocks_per_segment
)) <<
505 (le32_to_cpu(sbp
->s_log_block_size
) + 10));
508 static void nilfs_release_super_block(struct the_nilfs
*nilfs
)
512 for (i
= 0; i
< 2; i
++) {
513 if (nilfs
->ns_sbp
[i
]) {
514 brelse(nilfs
->ns_sbh
[i
]);
515 nilfs
->ns_sbh
[i
] = NULL
;
516 nilfs
->ns_sbp
[i
] = NULL
;
521 void nilfs_fall_back_super_block(struct the_nilfs
*nilfs
)
523 brelse(nilfs
->ns_sbh
[0]);
524 nilfs
->ns_sbh
[0] = nilfs
->ns_sbh
[1];
525 nilfs
->ns_sbp
[0] = nilfs
->ns_sbp
[1];
526 nilfs
->ns_sbh
[1] = NULL
;
527 nilfs
->ns_sbp
[1] = NULL
;
530 void nilfs_swap_super_block(struct the_nilfs
*nilfs
)
532 struct buffer_head
*tsbh
= nilfs
->ns_sbh
[0];
533 struct nilfs_super_block
*tsbp
= nilfs
->ns_sbp
[0];
535 nilfs
->ns_sbh
[0] = nilfs
->ns_sbh
[1];
536 nilfs
->ns_sbp
[0] = nilfs
->ns_sbp
[1];
537 nilfs
->ns_sbh
[1] = tsbh
;
538 nilfs
->ns_sbp
[1] = tsbp
;
541 static int nilfs_load_super_block(struct the_nilfs
*nilfs
,
542 struct super_block
*sb
, int blocksize
,
543 struct nilfs_super_block
**sbpp
)
545 struct nilfs_super_block
**sbp
= nilfs
->ns_sbp
;
546 struct buffer_head
**sbh
= nilfs
->ns_sbh
;
547 u64 sb2off
= NILFS_SB2_OFFSET_BYTES(nilfs
->ns_bdev
->bd_inode
->i_size
);
548 int valid
[2], swp
= 0;
550 sbp
[0] = nilfs_read_super_block(sb
, NILFS_SB_OFFSET_BYTES
, blocksize
,
552 sbp
[1] = nilfs_read_super_block(sb
, sb2off
, blocksize
, &sbh
[1]);
556 printk(KERN_ERR
"NILFS: unable to read superblock\n");
560 "NILFS warning: unable to read primary superblock\n");
563 "NILFS warning: unable to read secondary superblock\n");
566 * Compare two super blocks and set 1 in swp if the secondary
567 * super block is valid and newer. Otherwise, set 0 in swp.
569 valid
[0] = nilfs_valid_sb(sbp
[0]);
570 valid
[1] = nilfs_valid_sb(sbp
[1]);
571 swp
= valid
[1] && (!valid
[0] ||
572 le64_to_cpu(sbp
[1]->s_last_cno
) >
573 le64_to_cpu(sbp
[0]->s_last_cno
));
575 if (valid
[swp
] && nilfs_sb2_bad_offset(sbp
[swp
], sb2off
)) {
582 nilfs_release_super_block(nilfs
);
583 printk(KERN_ERR
"NILFS: Can't find nilfs on dev %s.\n",
589 printk(KERN_WARNING
"NILFS warning: broken superblock. "
590 "using spare superblock.\n");
591 nilfs_swap_super_block(nilfs
);
594 nilfs
->ns_sbwcount
= 0;
595 nilfs
->ns_sbwtime
= le64_to_cpu(sbp
[0]->s_wtime
);
596 nilfs
->ns_prot_seq
= le64_to_cpu(sbp
[valid
[1] & !swp
]->s_last_seq
);
602 * init_nilfs - initialize a NILFS instance.
603 * @nilfs: the_nilfs structure
604 * @sbi: nilfs_sb_info
606 * @data: mount options
608 * init_nilfs() performs common initialization per block device (e.g.
609 * reading the super block, getting disk layout information, initializing
610 * shared fields in the_nilfs). It takes on some portion of the jobs
611 * typically done by a fill_super() routine. This division arises from
612 * the nature that multiple NILFS instances may be simultaneously
613 * mounted on a device.
614 * For multiple mounts on the same device, only the first mount
615 * invokes these tasks.
617 * Return Value: On success, 0 is returned. On error, a negative error
620 int init_nilfs(struct the_nilfs
*nilfs
, struct nilfs_sb_info
*sbi
, char *data
)
622 struct super_block
*sb
= sbi
->s_super
;
623 struct nilfs_super_block
*sbp
;
624 struct backing_dev_info
*bdi
;
628 down_write(&nilfs
->ns_sem
);
629 if (nilfs_init(nilfs
)) {
630 /* Load values from existing the_nilfs */
631 sbp
= nilfs
->ns_sbp
[0];
632 err
= nilfs_store_magic_and_option(sb
, sbp
, data
);
636 blocksize
= BLOCK_SIZE
<< le32_to_cpu(sbp
->s_log_block_size
);
637 if (sb
->s_blocksize
!= blocksize
&&
638 !sb_set_blocksize(sb
, blocksize
)) {
639 printk(KERN_ERR
"NILFS: blocksize %d unfit to device\n",
643 sb
->s_maxbytes
= nilfs_max_size(sb
->s_blocksize_bits
);
647 blocksize
= sb_min_blocksize(sb
, BLOCK_SIZE
);
649 printk(KERN_ERR
"NILFS: unable to set blocksize\n");
653 err
= nilfs_load_super_block(nilfs
, sb
, blocksize
, &sbp
);
657 err
= nilfs_store_magic_and_option(sb
, sbp
, data
);
661 blocksize
= BLOCK_SIZE
<< le32_to_cpu(sbp
->s_log_block_size
);
662 if (sb
->s_blocksize
!= blocksize
) {
663 int hw_blocksize
= bdev_logical_block_size(sb
->s_bdev
);
665 if (blocksize
< hw_blocksize
) {
667 "NILFS: blocksize %d too small for device "
668 "(sector-size = %d).\n",
669 blocksize
, hw_blocksize
);
673 nilfs_release_super_block(nilfs
);
674 sb_set_blocksize(sb
, blocksize
);
676 err
= nilfs_load_super_block(nilfs
, sb
, blocksize
, &sbp
);
679 /* not failed_sbh; sbh is released automatically
680 when reloading fails. */
682 nilfs
->ns_blocksize_bits
= sb
->s_blocksize_bits
;
683 nilfs
->ns_blocksize
= blocksize
;
685 err
= nilfs_store_disk_layout(nilfs
, sbp
);
689 sb
->s_maxbytes
= nilfs_max_size(sb
->s_blocksize_bits
);
691 nilfs
->ns_mount_state
= le16_to_cpu(sbp
->s_state
);
693 bdi
= nilfs
->ns_bdev
->bd_inode
->i_mapping
->backing_dev_info
;
694 nilfs
->ns_bdi
= bdi
? : &default_backing_dev_info
;
696 err
= nilfs_store_log_cursor(nilfs
, sbp
);
700 /* Initialize gcinode cache */
701 err
= nilfs_init_gccache(nilfs
);
705 set_nilfs_init(nilfs
);
708 up_write(&nilfs
->ns_sem
);
712 nilfs_release_super_block(nilfs
);
716 int nilfs_discard_segments(struct the_nilfs
*nilfs
, __u64
*segnump
,
719 sector_t seg_start
, seg_end
;
720 sector_t start
= 0, nblocks
= 0;
721 unsigned int sects_per_block
;
725 sects_per_block
= (1 << nilfs
->ns_blocksize_bits
) /
726 bdev_logical_block_size(nilfs
->ns_bdev
);
727 for (sn
= segnump
; sn
< segnump
+ nsegs
; sn
++) {
728 nilfs_get_segment_range(nilfs
, *sn
, &seg_start
, &seg_end
);
732 nblocks
= seg_end
- seg_start
+ 1;
733 } else if (start
+ nblocks
== seg_start
) {
734 nblocks
+= seg_end
- seg_start
+ 1;
736 ret
= blkdev_issue_discard(nilfs
->ns_bdev
,
737 start
* sects_per_block
,
738 nblocks
* sects_per_block
,
747 ret
= blkdev_issue_discard(nilfs
->ns_bdev
,
748 start
* sects_per_block
,
749 nblocks
* sects_per_block
,
750 GFP_NOFS
, 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
;
778 * nilfs_find_sbinfo - find existing nilfs_sb_info structure
779 * @nilfs: nilfs object
780 * @rw_mount: mount type (non-zero value for read/write mount)
781 * @cno: checkpoint number (zero for read-only mount)
783 * nilfs_find_sbinfo() returns the nilfs_sb_info structure which
784 * @rw_mount and @cno (in case of snapshots) matched. If no instance
785 * was found, NULL is returned. Although the super block instance can
786 * be unmounted after this function returns, the nilfs_sb_info struct
787 * is kept on memory until nilfs_put_sbinfo() is called.
789 struct nilfs_sb_info
*nilfs_find_sbinfo(struct the_nilfs
*nilfs
,
790 int rw_mount
, __u64 cno
)
792 struct nilfs_sb_info
*sbi
;
794 down_read(&nilfs
->ns_super_sem
);
796 * The SNAPSHOT flag and sb->s_flags are supposed to be
797 * protected with nilfs->ns_super_sem.
799 sbi
= nilfs
->ns_current
;
801 if (sbi
&& !(sbi
->s_super
->s_flags
& MS_RDONLY
))
802 goto found
; /* read/write mount */
805 } else if (cno
== 0) {
806 if (sbi
&& (sbi
->s_super
->s_flags
& MS_RDONLY
))
807 goto found
; /* read-only mount */
812 list_for_each_entry(sbi
, &nilfs
->ns_supers
, s_list
) {
813 if (nilfs_test_opt(sbi
, SNAPSHOT
) &&
814 sbi
->s_snapshot_cno
== cno
)
815 goto found
; /* snapshot mount */
818 up_read(&nilfs
->ns_super_sem
);
822 atomic_inc(&sbi
->s_count
);
823 up_read(&nilfs
->ns_super_sem
);
827 int nilfs_checkpoint_is_mounted(struct the_nilfs
*nilfs
, __u64 cno
,
830 struct nilfs_sb_info
*sbi
;
833 down_read(&nilfs
->ns_super_sem
);
834 if (cno
== 0 || cno
> nilfs
->ns_cno
)
837 list_for_each_entry(sbi
, &nilfs
->ns_supers
, s_list
) {
838 if (sbi
->s_snapshot_cno
== cno
&&
839 (!snapshot_mount
|| nilfs_test_opt(sbi
, SNAPSHOT
))) {
840 /* exclude read-only mounts */
845 /* for protecting recent checkpoints */
846 if (cno
>= nilfs_last_cno(nilfs
))
850 up_read(&nilfs
->ns_super_sem
);