xref: /openbmc/linux/fs/nilfs2/the_nilfs.c (revision 654137dd46bc7e9f088a4a551a2b77a8541dfdb8)
1 /*
2  * the_nilfs.c - the_nilfs shared structure.
3  *
4  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5  *
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.
10  *
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.
15  *
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
19  *
20  * Written by Ryusuke Konishi <ryusuke@osrg.net>
21  *
22  */
23 
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>
29 #include "nilfs.h"
30 #include "segment.h"
31 #include "alloc.h"
32 #include "cpfile.h"
33 #include "sufile.h"
34 #include "dat.h"
35 #include "segbuf.h"
36 
37 void nilfs_set_last_segment(struct the_nilfs *nilfs,
38 			    sector_t start_blocknr, u64 seq, __u64 cno)
39 {
40 	spin_lock(&nilfs->ns_last_segment_lock);
41 	nilfs->ns_last_pseg = start_blocknr;
42 	nilfs->ns_last_seq = seq;
43 	nilfs->ns_last_cno = cno;
44 	spin_unlock(&nilfs->ns_last_segment_lock);
45 }
46 
47 /**
48  * alloc_nilfs - allocate the_nilfs structure
49  * @bdev: block device to which the_nilfs is related
50  *
51  * alloc_nilfs() allocates memory for the_nilfs and
52  * initializes its reference count and locks.
53  *
54  * Return Value: On success, pointer to the_nilfs is returned.
55  * On error, NULL is returned.
56  */
57 struct the_nilfs *alloc_nilfs(struct block_device *bdev)
58 {
59 	struct the_nilfs *nilfs;
60 
61 	nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
62 	if (!nilfs)
63 		return NULL;
64 
65 	nilfs->ns_bdev = bdev;
66 	atomic_set(&nilfs->ns_count, 1);
67 	atomic_set(&nilfs->ns_writer_refcount, -1);
68 	atomic_set(&nilfs->ns_ndirtyblks, 0);
69 	init_rwsem(&nilfs->ns_sem);
70 	mutex_init(&nilfs->ns_writer_mutex);
71 	INIT_LIST_HEAD(&nilfs->ns_supers);
72 	spin_lock_init(&nilfs->ns_last_segment_lock);
73 	nilfs->ns_gc_inodes_h = NULL;
74 	init_rwsem(&nilfs->ns_segctor_sem);
75 
76 	return nilfs;
77 }
78 
79 /**
80  * put_nilfs - release a reference to the_nilfs
81  * @nilfs: the_nilfs structure to be released
82  *
83  * put_nilfs() decrements a reference counter of the_nilfs.
84  * If the reference count reaches zero, the_nilfs is freed.
85  */
86 void put_nilfs(struct the_nilfs *nilfs)
87 {
88 	if (!atomic_dec_and_test(&nilfs->ns_count))
89 		return;
90 	/*
91 	 * Increment of ns_count never occur below because the caller
92 	 * of get_nilfs() holds at least one reference to the_nilfs.
93 	 * Thus its exclusion control is not required here.
94 	 */
95 	might_sleep();
96 	if (nilfs_loaded(nilfs)) {
97 		nilfs_mdt_clear(nilfs->ns_sufile);
98 		nilfs_mdt_destroy(nilfs->ns_sufile);
99 		nilfs_mdt_clear(nilfs->ns_cpfile);
100 		nilfs_mdt_destroy(nilfs->ns_cpfile);
101 		nilfs_mdt_clear(nilfs->ns_dat);
102 		nilfs_mdt_destroy(nilfs->ns_dat);
103 		/* XXX: how and when to clear nilfs->ns_gc_dat? */
104 		nilfs_mdt_destroy(nilfs->ns_gc_dat);
105 	}
106 	if (nilfs_init(nilfs)) {
107 		nilfs_destroy_gccache(nilfs);
108 		brelse(nilfs->ns_sbh[0]);
109 		brelse(nilfs->ns_sbh[1]);
110 	}
111 	kfree(nilfs);
112 }
113 
114 static int nilfs_load_super_root(struct the_nilfs *nilfs,
115 				 struct nilfs_sb_info *sbi, sector_t sr_block)
116 {
117 	static struct lock_class_key dat_lock_key;
118 	struct buffer_head *bh_sr;
119 	struct nilfs_super_root *raw_sr;
120 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
121 	unsigned dat_entry_size, segment_usage_size, checkpoint_size;
122 	unsigned inode_size;
123 	int err;
124 
125 	err = nilfs_read_super_root_block(sbi->s_super, sr_block, &bh_sr, 1);
126 	if (unlikely(err))
127 		return err;
128 
129 	down_read(&nilfs->ns_sem);
130 	dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
131 	checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
132 	segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
133 	up_read(&nilfs->ns_sem);
134 
135 	inode_size = nilfs->ns_inode_size;
136 
137 	err = -ENOMEM;
138 	nilfs->ns_dat = nilfs_mdt_new(
139 		nilfs, NULL, NILFS_DAT_INO, NILFS_DAT_GFP);
140 	if (unlikely(!nilfs->ns_dat))
141 		goto failed;
142 
143 	nilfs->ns_gc_dat = nilfs_mdt_new(
144 		nilfs, NULL, NILFS_DAT_INO, NILFS_DAT_GFP);
145 	if (unlikely(!nilfs->ns_gc_dat))
146 		goto failed_dat;
147 
148 	nilfs->ns_cpfile = nilfs_mdt_new(
149 		nilfs, NULL, NILFS_CPFILE_INO, NILFS_CPFILE_GFP);
150 	if (unlikely(!nilfs->ns_cpfile))
151 		goto failed_gc_dat;
152 
153 	nilfs->ns_sufile = nilfs_mdt_new(
154 		nilfs, NULL, NILFS_SUFILE_INO, NILFS_SUFILE_GFP);
155 	if (unlikely(!nilfs->ns_sufile))
156 		goto failed_cpfile;
157 
158 	err = nilfs_palloc_init_blockgroup(nilfs->ns_dat, dat_entry_size);
159 	if (unlikely(err))
160 		goto failed_sufile;
161 
162 	err = nilfs_palloc_init_blockgroup(nilfs->ns_gc_dat, dat_entry_size);
163 	if (unlikely(err))
164 		goto failed_sufile;
165 
166 	lockdep_set_class(&NILFS_MDT(nilfs->ns_dat)->mi_sem, &dat_lock_key);
167 	lockdep_set_class(&NILFS_MDT(nilfs->ns_gc_dat)->mi_sem, &dat_lock_key);
168 
169 	nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat);
170 	nilfs_mdt_set_entry_size(nilfs->ns_cpfile, checkpoint_size,
171 				 sizeof(struct nilfs_cpfile_header));
172 	nilfs_mdt_set_entry_size(nilfs->ns_sufile, segment_usage_size,
173 				 sizeof(struct nilfs_sufile_header));
174 
175 	err = nilfs_mdt_read_inode_direct(
176 		nilfs->ns_dat, bh_sr, NILFS_SR_DAT_OFFSET(inode_size));
177 	if (unlikely(err))
178 		goto failed_sufile;
179 
180 	err = nilfs_mdt_read_inode_direct(
181 		nilfs->ns_cpfile, bh_sr, NILFS_SR_CPFILE_OFFSET(inode_size));
182 	if (unlikely(err))
183 		goto failed_sufile;
184 
185 	err = nilfs_mdt_read_inode_direct(
186 		nilfs->ns_sufile, bh_sr, NILFS_SR_SUFILE_OFFSET(inode_size));
187 	if (unlikely(err))
188 		goto failed_sufile;
189 
190 	raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
191 	nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
192 
193  failed:
194 	brelse(bh_sr);
195 	return err;
196 
197  failed_sufile:
198 	nilfs_mdt_destroy(nilfs->ns_sufile);
199 
200  failed_cpfile:
201 	nilfs_mdt_destroy(nilfs->ns_cpfile);
202 
203  failed_gc_dat:
204 	nilfs_mdt_destroy(nilfs->ns_gc_dat);
205 
206  failed_dat:
207 	nilfs_mdt_destroy(nilfs->ns_dat);
208 	goto failed;
209 }
210 
211 static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
212 {
213 	memset(ri, 0, sizeof(*ri));
214 	INIT_LIST_HEAD(&ri->ri_used_segments);
215 }
216 
217 static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
218 {
219 	nilfs_dispose_segment_list(&ri->ri_used_segments);
220 }
221 
222 /**
223  * load_nilfs - load and recover the nilfs
224  * @nilfs: the_nilfs structure to be released
225  * @sbi: nilfs_sb_info used to recover past segment
226  *
227  * load_nilfs() searches and load the latest super root,
228  * attaches the last segment, and does recovery if needed.
229  * The caller must call this exclusively for simultaneous mounts.
230  */
231 int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
232 {
233 	struct nilfs_recovery_info ri;
234 	unsigned int s_flags = sbi->s_super->s_flags;
235 	int really_read_only = bdev_read_only(nilfs->ns_bdev);
236 	unsigned valid_fs;
237 	int err = 0;
238 
239 	nilfs_init_recovery_info(&ri);
240 
241 	down_write(&nilfs->ns_sem);
242 	valid_fs = (nilfs->ns_mount_state & NILFS_VALID_FS);
243 	up_write(&nilfs->ns_sem);
244 
245 	if (!valid_fs && (s_flags & MS_RDONLY)) {
246 		printk(KERN_INFO "NILFS: INFO: recovery "
247 		       "required for readonly filesystem.\n");
248 		if (really_read_only) {
249 			printk(KERN_ERR "NILFS: write access "
250 			       "unavailable, cannot proceed.\n");
251 			err = -EROFS;
252 			goto failed;
253 		}
254 		printk(KERN_INFO "NILFS: write access will "
255 		       "be enabled during recovery.\n");
256 		sbi->s_super->s_flags &= ~MS_RDONLY;
257 	}
258 
259 	err = nilfs_search_super_root(nilfs, sbi, &ri);
260 	if (unlikely(err)) {
261 		printk(KERN_ERR "NILFS: error searching super root.\n");
262 		goto failed;
263 	}
264 
265 	err = nilfs_load_super_root(nilfs, sbi, ri.ri_super_root);
266 	if (unlikely(err)) {
267 		printk(KERN_ERR "NILFS: error loading super root.\n");
268 		goto failed;
269 	}
270 
271 	if (!valid_fs) {
272 		err = nilfs_recover_logical_segments(nilfs, sbi, &ri);
273 		if (unlikely(err)) {
274 			nilfs_mdt_destroy(nilfs->ns_cpfile);
275 			nilfs_mdt_destroy(nilfs->ns_sufile);
276 			nilfs_mdt_destroy(nilfs->ns_dat);
277 			goto failed;
278 		}
279 		if (ri.ri_need_recovery == NILFS_RECOVERY_SR_UPDATED)
280 			sbi->s_super->s_dirt = 1;
281 	}
282 
283 	set_nilfs_loaded(nilfs);
284 
285  failed:
286 	nilfs_clear_recovery_info(&ri);
287 	sbi->s_super->s_flags = s_flags;
288 	return err;
289 }
290 
291 static unsigned long long nilfs_max_size(unsigned int blkbits)
292 {
293 	unsigned int max_bits;
294 	unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
295 
296 	max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
297 	if (max_bits < 64)
298 		res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
299 	return res;
300 }
301 
302 static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
303 				   struct nilfs_super_block *sbp)
304 {
305 	if (le32_to_cpu(sbp->s_rev_level) != NILFS_CURRENT_REV) {
306 		printk(KERN_ERR "NILFS: revision mismatch "
307 		       "(superblock rev.=%d.%d, current rev.=%d.%d). "
308 		       "Please check the version of mkfs.nilfs.\n",
309 		       le32_to_cpu(sbp->s_rev_level),
310 		       le16_to_cpu(sbp->s_minor_rev_level),
311 		       NILFS_CURRENT_REV, NILFS_MINOR_REV);
312 		return -EINVAL;
313 	}
314 	nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
315 	if (nilfs->ns_sbsize > BLOCK_SIZE)
316 		return -EINVAL;
317 
318 	nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
319 	nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
320 
321 	nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
322 	if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
323 		printk(KERN_ERR "NILFS: too short segment. \n");
324 		return -EINVAL;
325 	}
326 
327 	nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
328 	nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
329 	nilfs->ns_r_segments_percentage =
330 		le32_to_cpu(sbp->s_r_segments_percentage);
331 	nilfs->ns_nrsvsegs =
332 		max_t(unsigned long, NILFS_MIN_NRSVSEGS,
333 		      DIV_ROUND_UP(nilfs->ns_nsegments *
334 				   nilfs->ns_r_segments_percentage, 100));
335 	nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
336 	return 0;
337 }
338 
339 static int nilfs_valid_sb(struct nilfs_super_block *sbp)
340 {
341 	static unsigned char sum[4];
342 	const int sumoff = offsetof(struct nilfs_super_block, s_sum);
343 	size_t bytes;
344 	u32 crc;
345 
346 	if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
347 		return 0;
348 	bytes = le16_to_cpu(sbp->s_bytes);
349 	if (bytes > BLOCK_SIZE)
350 		return 0;
351 	crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
352 		       sumoff);
353 	crc = crc32_le(crc, sum, 4);
354 	crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
355 		       bytes - sumoff - 4);
356 	return crc == le32_to_cpu(sbp->s_sum);
357 }
358 
359 static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
360 {
361 	return offset < ((le64_to_cpu(sbp->s_nsegments) *
362 			  le32_to_cpu(sbp->s_blocks_per_segment)) <<
363 			 (le32_to_cpu(sbp->s_log_block_size) + 10));
364 }
365 
366 static void nilfs_release_super_block(struct the_nilfs *nilfs)
367 {
368 	int i;
369 
370 	for (i = 0; i < 2; i++) {
371 		if (nilfs->ns_sbp[i]) {
372 			brelse(nilfs->ns_sbh[i]);
373 			nilfs->ns_sbh[i] = NULL;
374 			nilfs->ns_sbp[i] = NULL;
375 		}
376 	}
377 }
378 
379 void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
380 {
381 	brelse(nilfs->ns_sbh[0]);
382 	nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
383 	nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
384 	nilfs->ns_sbh[1] = NULL;
385 	nilfs->ns_sbp[1] = NULL;
386 }
387 
388 void nilfs_swap_super_block(struct the_nilfs *nilfs)
389 {
390 	struct buffer_head *tsbh = nilfs->ns_sbh[0];
391 	struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
392 
393 	nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
394 	nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
395 	nilfs->ns_sbh[1] = tsbh;
396 	nilfs->ns_sbp[1] = tsbp;
397 }
398 
399 static int nilfs_load_super_block(struct the_nilfs *nilfs,
400 				  struct super_block *sb, int blocksize,
401 				  struct nilfs_super_block **sbpp)
402 {
403 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
404 	struct buffer_head **sbh = nilfs->ns_sbh;
405 	u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
406 	int valid[2], swp = 0;
407 
408 	sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
409 					&sbh[0]);
410 	sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
411 
412 	if (!sbp[0]) {
413 		if (!sbp[1]) {
414 			printk(KERN_ERR "NILFS: unable to read superblock\n");
415 			return -EIO;
416 		}
417 		printk(KERN_WARNING
418 		       "NILFS warning: unable to read primary superblock\n");
419 	} else if (!sbp[1])
420 		printk(KERN_WARNING
421 		       "NILFS warning: unable to read secondary superblock\n");
422 
423 	valid[0] = nilfs_valid_sb(sbp[0]);
424 	valid[1] = nilfs_valid_sb(sbp[1]);
425 	swp = valid[1] &&
426 		(!valid[0] ||
427 		 le64_to_cpu(sbp[1]->s_wtime) > le64_to_cpu(sbp[0]->s_wtime));
428 
429 	if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
430 		brelse(sbh[1]);
431 		sbh[1] = NULL;
432 		sbp[1] = NULL;
433 		swp = 0;
434 	}
435 	if (!valid[swp]) {
436 		nilfs_release_super_block(nilfs);
437 		printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
438 		       sb->s_id);
439 		return -EINVAL;
440 	}
441 
442 	if (swp) {
443 		printk(KERN_WARNING "NILFS warning: broken superblock. "
444 		       "using spare superblock.\n");
445 		nilfs_swap_super_block(nilfs);
446 	}
447 
448 	nilfs->ns_sbwtime[0] = le64_to_cpu(sbp[0]->s_wtime);
449 	nilfs->ns_sbwtime[1] = valid[!swp] ? le64_to_cpu(sbp[1]->s_wtime) : 0;
450 	nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
451 	*sbpp = sbp[0];
452 	return 0;
453 }
454 
455 /**
456  * init_nilfs - initialize a NILFS instance.
457  * @nilfs: the_nilfs structure
458  * @sbi: nilfs_sb_info
459  * @sb: super block
460  * @data: mount options
461  *
462  * init_nilfs() performs common initialization per block device (e.g.
463  * reading the super block, getting disk layout information, initializing
464  * shared fields in the_nilfs). It takes on some portion of the jobs
465  * typically done by a fill_super() routine. This division arises from
466  * the nature that multiple NILFS instances may be simultaneously
467  * mounted on a device.
468  * For multiple mounts on the same device, only the first mount
469  * invokes these tasks.
470  *
471  * Return Value: On success, 0 is returned. On error, a negative error
472  * code is returned.
473  */
474 int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
475 {
476 	struct super_block *sb = sbi->s_super;
477 	struct nilfs_super_block *sbp;
478 	struct backing_dev_info *bdi;
479 	int blocksize;
480 	int err;
481 
482 	down_write(&nilfs->ns_sem);
483 	if (nilfs_init(nilfs)) {
484 		/* Load values from existing the_nilfs */
485 		sbp = nilfs->ns_sbp[0];
486 		err = nilfs_store_magic_and_option(sb, sbp, data);
487 		if (err)
488 			goto out;
489 
490 		blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
491 		if (sb->s_blocksize != blocksize &&
492 		    !sb_set_blocksize(sb, blocksize)) {
493 			printk(KERN_ERR "NILFS: blocksize %d unfit to device\n",
494 			       blocksize);
495 			err = -EINVAL;
496 		}
497 		sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
498 		goto out;
499 	}
500 
501 	blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
502 	if (!blocksize) {
503 		printk(KERN_ERR "NILFS: unable to set blocksize\n");
504 		err = -EINVAL;
505 		goto out;
506 	}
507 	err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
508 	if (err)
509 		goto out;
510 
511 	err = nilfs_store_magic_and_option(sb, sbp, data);
512 	if (err)
513 		goto failed_sbh;
514 
515 	blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
516 	if (sb->s_blocksize != blocksize) {
517 		int hw_blocksize = bdev_hardsect_size(sb->s_bdev);
518 
519 		if (blocksize < hw_blocksize) {
520 			printk(KERN_ERR
521 			       "NILFS: blocksize %d too small for device "
522 			       "(sector-size = %d).\n",
523 			       blocksize, hw_blocksize);
524 			err = -EINVAL;
525 			goto failed_sbh;
526 		}
527 		nilfs_release_super_block(nilfs);
528 		sb_set_blocksize(sb, blocksize);
529 
530 		err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
531 		if (err)
532 			goto out;
533 			/* not failed_sbh; sbh is released automatically
534 			   when reloading fails. */
535 	}
536 	nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
537 
538 	err = nilfs_store_disk_layout(nilfs, sbp);
539 	if (err)
540 		goto failed_sbh;
541 
542 	sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
543 
544 	nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
545 
546 	bdi = nilfs->ns_bdev->bd_inode_backing_dev_info;
547 	if (!bdi)
548 		bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
549 	nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
550 
551 	/* Finding last segment */
552 	nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
553 	nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
554 	nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
555 
556 	nilfs->ns_seg_seq = nilfs->ns_last_seq;
557 	nilfs->ns_segnum =
558 		nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
559 	nilfs->ns_cno = nilfs->ns_last_cno + 1;
560 	if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
561 		printk(KERN_ERR "NILFS invalid last segment number.\n");
562 		err = -EINVAL;
563 		goto failed_sbh;
564 	}
565 	/* Dummy values  */
566 	nilfs->ns_free_segments_count =
567 		nilfs->ns_nsegments - (nilfs->ns_segnum + 1);
568 
569 	/* Initialize gcinode cache */
570 	err = nilfs_init_gccache(nilfs);
571 	if (err)
572 		goto failed_sbh;
573 
574 	set_nilfs_init(nilfs);
575 	err = 0;
576  out:
577 	up_write(&nilfs->ns_sem);
578 	return err;
579 
580  failed_sbh:
581 	nilfs_release_super_block(nilfs);
582 	goto out;
583 }
584 
585 int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
586 {
587 	struct inode *dat = nilfs_dat_inode(nilfs);
588 	unsigned long ncleansegs;
589 	int err;
590 
591 	down_read(&NILFS_MDT(dat)->mi_sem);	/* XXX */
592 	err = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile, &ncleansegs);
593 	up_read(&NILFS_MDT(dat)->mi_sem);	/* XXX */
594 	if (likely(!err))
595 		*nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
596 	return err;
597 }
598 
599 int nilfs_near_disk_full(struct the_nilfs *nilfs)
600 {
601 	struct inode *sufile = nilfs->ns_sufile;
602 	unsigned long ncleansegs, nincsegs;
603 	int ret;
604 
605 	ret = nilfs_sufile_get_ncleansegs(sufile, &ncleansegs);
606 	if (likely(!ret)) {
607 		nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
608 			nilfs->ns_blocks_per_segment + 1;
609 		if (ncleansegs <= nilfs->ns_nrsvsegs + nincsegs)
610 			ret++;
611 	}
612 	return ret;
613 }
614 
615 int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
616 				int snapshot_mount)
617 {
618 	struct nilfs_sb_info *sbi;
619 	int ret = 0;
620 
621 	down_read(&nilfs->ns_sem);
622 	if (cno == 0 || cno > nilfs->ns_cno)
623 		goto out_unlock;
624 
625 	list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
626 		if (sbi->s_snapshot_cno == cno &&
627 		    (!snapshot_mount || nilfs_test_opt(sbi, SNAPSHOT))) {
628 					/* exclude read-only mounts */
629 			ret++;
630 			break;
631 		}
632 	}
633 	/* for protecting recent checkpoints */
634 	if (cno >= nilfs_last_cno(nilfs))
635 		ret++;
636 
637  out_unlock:
638 	up_read(&nilfs->ns_sem);
639 	return ret;
640 }
641