xref: /linux/fs/nilfs2/the_nilfs.c (revision 24168c5e6dfbdd5b414f048f47f75d64533296ca)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * the_nilfs shared structure.
4  *
5  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6  *
7  * Written by Ryusuke Konishi.
8  *
9  */
10 
11 #include <linux/buffer_head.h>
12 #include <linux/slab.h>
13 #include <linux/blkdev.h>
14 #include <linux/backing-dev.h>
15 #include <linux/random.h>
16 #include <linux/log2.h>
17 #include <linux/crc32.h>
18 #include "nilfs.h"
19 #include "segment.h"
20 #include "alloc.h"
21 #include "cpfile.h"
22 #include "sufile.h"
23 #include "dat.h"
24 #include "segbuf.h"
25 
26 
27 static int nilfs_valid_sb(struct nilfs_super_block *sbp);
28 
29 void nilfs_set_last_segment(struct the_nilfs *nilfs,
30 			    sector_t start_blocknr, u64 seq, __u64 cno)
31 {
32 	spin_lock(&nilfs->ns_last_segment_lock);
33 	nilfs->ns_last_pseg = start_blocknr;
34 	nilfs->ns_last_seq = seq;
35 	nilfs->ns_last_cno = cno;
36 
37 	if (!nilfs_sb_dirty(nilfs)) {
38 		if (nilfs->ns_prev_seq == nilfs->ns_last_seq)
39 			goto stay_cursor;
40 
41 		set_nilfs_sb_dirty(nilfs);
42 	}
43 	nilfs->ns_prev_seq = nilfs->ns_last_seq;
44 
45  stay_cursor:
46 	spin_unlock(&nilfs->ns_last_segment_lock);
47 }
48 
49 /**
50  * alloc_nilfs - allocate a nilfs object
51  * @sb: super block instance
52  *
53  * Return Value: On success, pointer to the_nilfs is returned.
54  * On error, NULL is returned.
55  */
56 struct the_nilfs *alloc_nilfs(struct super_block *sb)
57 {
58 	struct the_nilfs *nilfs;
59 
60 	nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
61 	if (!nilfs)
62 		return NULL;
63 
64 	nilfs->ns_sb = sb;
65 	nilfs->ns_bdev = sb->s_bdev;
66 	atomic_set(&nilfs->ns_ndirtyblks, 0);
67 	init_rwsem(&nilfs->ns_sem);
68 	mutex_init(&nilfs->ns_snapshot_mount_mutex);
69 	INIT_LIST_HEAD(&nilfs->ns_dirty_files);
70 	INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
71 	spin_lock_init(&nilfs->ns_inode_lock);
72 	spin_lock_init(&nilfs->ns_next_gen_lock);
73 	spin_lock_init(&nilfs->ns_last_segment_lock);
74 	nilfs->ns_cptree = RB_ROOT;
75 	spin_lock_init(&nilfs->ns_cptree_lock);
76 	init_rwsem(&nilfs->ns_segctor_sem);
77 	nilfs->ns_sb_update_freq = NILFS_SB_FREQ;
78 
79 	return nilfs;
80 }
81 
82 /**
83  * destroy_nilfs - destroy nilfs object
84  * @nilfs: nilfs object to be released
85  */
86 void destroy_nilfs(struct the_nilfs *nilfs)
87 {
88 	might_sleep();
89 	if (nilfs_init(nilfs)) {
90 		brelse(nilfs->ns_sbh[0]);
91 		brelse(nilfs->ns_sbh[1]);
92 	}
93 	kfree(nilfs);
94 }
95 
96 static int nilfs_load_super_root(struct the_nilfs *nilfs,
97 				 struct super_block *sb, sector_t sr_block)
98 {
99 	struct buffer_head *bh_sr;
100 	struct nilfs_super_root *raw_sr;
101 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
102 	struct nilfs_inode *rawi;
103 	unsigned int dat_entry_size, segment_usage_size, checkpoint_size;
104 	unsigned int inode_size;
105 	int err;
106 
107 	err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
108 	if (unlikely(err))
109 		return err;
110 
111 	down_read(&nilfs->ns_sem);
112 	dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
113 	checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
114 	segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
115 	up_read(&nilfs->ns_sem);
116 
117 	inode_size = nilfs->ns_inode_size;
118 
119 	rawi = (void *)bh_sr->b_data + NILFS_SR_DAT_OFFSET(inode_size);
120 	err = nilfs_dat_read(sb, dat_entry_size, rawi, &nilfs->ns_dat);
121 	if (err)
122 		goto failed;
123 
124 	rawi = (void *)bh_sr->b_data + NILFS_SR_CPFILE_OFFSET(inode_size);
125 	err = nilfs_cpfile_read(sb, checkpoint_size, rawi, &nilfs->ns_cpfile);
126 	if (err)
127 		goto failed_dat;
128 
129 	rawi = (void *)bh_sr->b_data + NILFS_SR_SUFILE_OFFSET(inode_size);
130 	err = nilfs_sufile_read(sb, segment_usage_size, rawi,
131 				&nilfs->ns_sufile);
132 	if (err)
133 		goto failed_cpfile;
134 
135 	raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
136 	nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
137 
138  failed:
139 	brelse(bh_sr);
140 	return err;
141 
142  failed_cpfile:
143 	iput(nilfs->ns_cpfile);
144 
145  failed_dat:
146 	iput(nilfs->ns_dat);
147 	goto failed;
148 }
149 
150 static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
151 {
152 	memset(ri, 0, sizeof(*ri));
153 	INIT_LIST_HEAD(&ri->ri_used_segments);
154 }
155 
156 static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
157 {
158 	nilfs_dispose_segment_list(&ri->ri_used_segments);
159 }
160 
161 /**
162  * nilfs_store_log_cursor - load log cursor from a super block
163  * @nilfs: nilfs object
164  * @sbp: buffer storing super block to be read
165  *
166  * nilfs_store_log_cursor() reads the last position of the log
167  * containing a super root from a given super block, and initializes
168  * relevant information on the nilfs object preparatory for log
169  * scanning and recovery.
170  */
171 static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
172 				  struct nilfs_super_block *sbp)
173 {
174 	int ret = 0;
175 
176 	nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
177 	nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
178 	nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
179 
180 	nilfs->ns_prev_seq = nilfs->ns_last_seq;
181 	nilfs->ns_seg_seq = nilfs->ns_last_seq;
182 	nilfs->ns_segnum =
183 		nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
184 	nilfs->ns_cno = nilfs->ns_last_cno + 1;
185 	if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
186 		nilfs_err(nilfs->ns_sb,
187 			  "pointed segment number is out of range: segnum=%llu, nsegments=%lu",
188 			  (unsigned long long)nilfs->ns_segnum,
189 			  nilfs->ns_nsegments);
190 		ret = -EINVAL;
191 	}
192 	return ret;
193 }
194 
195 /**
196  * nilfs_get_blocksize - get block size from raw superblock data
197  * @sb: super block instance
198  * @sbp: superblock raw data buffer
199  * @blocksize: place to store block size
200  *
201  * nilfs_get_blocksize() calculates the block size from the block size
202  * exponent information written in @sbp and stores it in @blocksize,
203  * or aborts with an error message if it's too large.
204  *
205  * Return Value: On success, 0 is returned. If the block size is too
206  * large, -EINVAL is returned.
207  */
208 static int nilfs_get_blocksize(struct super_block *sb,
209 			       struct nilfs_super_block *sbp, int *blocksize)
210 {
211 	unsigned int shift_bits = le32_to_cpu(sbp->s_log_block_size);
212 
213 	if (unlikely(shift_bits >
214 		     ilog2(NILFS_MAX_BLOCK_SIZE) - BLOCK_SIZE_BITS)) {
215 		nilfs_err(sb, "too large filesystem blocksize: 2 ^ %u KiB",
216 			  shift_bits);
217 		return -EINVAL;
218 	}
219 	*blocksize = BLOCK_SIZE << shift_bits;
220 	return 0;
221 }
222 
223 /**
224  * load_nilfs - load and recover the nilfs
225  * @nilfs: the_nilfs structure to be released
226  * @sb: super block instance used to recover past segment
227  *
228  * load_nilfs() searches and load the latest super root,
229  * attaches the last segment, and does recovery if needed.
230  * The caller must call this exclusively for simultaneous mounts.
231  */
232 int load_nilfs(struct the_nilfs *nilfs, struct super_block *sb)
233 {
234 	struct nilfs_recovery_info ri;
235 	unsigned int s_flags = sb->s_flags;
236 	int really_read_only = bdev_read_only(nilfs->ns_bdev);
237 	int valid_fs = nilfs_valid_fs(nilfs);
238 	int err;
239 
240 	if (!valid_fs) {
241 		nilfs_warn(sb, "mounting unchecked fs");
242 		if (s_flags & SB_RDONLY) {
243 			nilfs_info(sb,
244 				   "recovery required for readonly filesystem");
245 			nilfs_info(sb,
246 				   "write access will be enabled during recovery");
247 		}
248 	}
249 
250 	nilfs_init_recovery_info(&ri);
251 
252 	err = nilfs_search_super_root(nilfs, &ri);
253 	if (unlikely(err)) {
254 		struct nilfs_super_block **sbp = nilfs->ns_sbp;
255 		int blocksize;
256 
257 		if (err != -EINVAL)
258 			goto scan_error;
259 
260 		if (!nilfs_valid_sb(sbp[1])) {
261 			nilfs_warn(sb,
262 				   "unable to fall back to spare super block");
263 			goto scan_error;
264 		}
265 		nilfs_info(sb, "trying rollback from an earlier position");
266 
267 		/*
268 		 * restore super block with its spare and reconfigure
269 		 * relevant states of the nilfs object.
270 		 */
271 		memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
272 		nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
273 		nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
274 
275 		/* verify consistency between two super blocks */
276 		err = nilfs_get_blocksize(sb, sbp[0], &blocksize);
277 		if (err)
278 			goto scan_error;
279 
280 		if (blocksize != nilfs->ns_blocksize) {
281 			nilfs_warn(sb,
282 				   "blocksize differs between two super blocks (%d != %d)",
283 				   blocksize, nilfs->ns_blocksize);
284 			err = -EINVAL;
285 			goto scan_error;
286 		}
287 
288 		err = nilfs_store_log_cursor(nilfs, sbp[0]);
289 		if (err)
290 			goto scan_error;
291 
292 		/* drop clean flag to allow roll-forward and recovery */
293 		nilfs->ns_mount_state &= ~NILFS_VALID_FS;
294 		valid_fs = 0;
295 
296 		err = nilfs_search_super_root(nilfs, &ri);
297 		if (err)
298 			goto scan_error;
299 	}
300 
301 	err = nilfs_load_super_root(nilfs, sb, ri.ri_super_root);
302 	if (unlikely(err)) {
303 		nilfs_err(sb, "error %d while loading super root", err);
304 		goto failed;
305 	}
306 
307 	err = nilfs_sysfs_create_device_group(sb);
308 	if (unlikely(err))
309 		goto sysfs_error;
310 
311 	if (valid_fs)
312 		goto skip_recovery;
313 
314 	if (s_flags & SB_RDONLY) {
315 		__u64 features;
316 
317 		if (nilfs_test_opt(nilfs, NORECOVERY)) {
318 			nilfs_info(sb,
319 				   "norecovery option specified, skipping roll-forward recovery");
320 			goto skip_recovery;
321 		}
322 		features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
323 			~NILFS_FEATURE_COMPAT_RO_SUPP;
324 		if (features) {
325 			nilfs_err(sb,
326 				  "couldn't proceed with recovery because of unsupported optional features (%llx)",
327 				  (unsigned long long)features);
328 			err = -EROFS;
329 			goto failed_unload;
330 		}
331 		if (really_read_only) {
332 			nilfs_err(sb,
333 				  "write access unavailable, cannot proceed");
334 			err = -EROFS;
335 			goto failed_unload;
336 		}
337 		sb->s_flags &= ~SB_RDONLY;
338 	} else if (nilfs_test_opt(nilfs, NORECOVERY)) {
339 		nilfs_err(sb,
340 			  "recovery cancelled because norecovery option was specified for a read/write mount");
341 		err = -EINVAL;
342 		goto failed_unload;
343 	}
344 
345 	err = nilfs_salvage_orphan_logs(nilfs, sb, &ri);
346 	if (err)
347 		goto failed_unload;
348 
349 	down_write(&nilfs->ns_sem);
350 	nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
351 	err = nilfs_cleanup_super(sb);
352 	up_write(&nilfs->ns_sem);
353 
354 	if (err) {
355 		nilfs_err(sb,
356 			  "error %d updating super block. recovery unfinished.",
357 			  err);
358 		goto failed_unload;
359 	}
360 	nilfs_info(sb, "recovery complete");
361 
362  skip_recovery:
363 	nilfs_clear_recovery_info(&ri);
364 	sb->s_flags = s_flags;
365 	return 0;
366 
367  scan_error:
368 	nilfs_err(sb, "error %d while searching super root", err);
369 	goto failed;
370 
371  failed_unload:
372 	nilfs_sysfs_delete_device_group(nilfs);
373 
374  sysfs_error:
375 	iput(nilfs->ns_cpfile);
376 	iput(nilfs->ns_sufile);
377 	iput(nilfs->ns_dat);
378 
379  failed:
380 	nilfs_clear_recovery_info(&ri);
381 	sb->s_flags = s_flags;
382 	return err;
383 }
384 
385 static unsigned long long nilfs_max_size(unsigned int blkbits)
386 {
387 	unsigned int max_bits;
388 	unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
389 
390 	max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
391 	if (max_bits < 64)
392 		res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
393 	return res;
394 }
395 
396 /**
397  * nilfs_nrsvsegs - calculate the number of reserved segments
398  * @nilfs: nilfs object
399  * @nsegs: total number of segments
400  */
401 unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs)
402 {
403 	return max_t(unsigned long, NILFS_MIN_NRSVSEGS,
404 		     DIV_ROUND_UP(nsegs * nilfs->ns_r_segments_percentage,
405 				  100));
406 }
407 
408 /**
409  * nilfs_max_segment_count - calculate the maximum number of segments
410  * @nilfs: nilfs object
411  */
412 static u64 nilfs_max_segment_count(struct the_nilfs *nilfs)
413 {
414 	u64 max_count = U64_MAX;
415 
416 	max_count = div64_ul(max_count, nilfs->ns_blocks_per_segment);
417 	return min_t(u64, max_count, ULONG_MAX);
418 }
419 
420 void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs)
421 {
422 	nilfs->ns_nsegments = nsegs;
423 	nilfs->ns_nrsvsegs = nilfs_nrsvsegs(nilfs, nsegs);
424 }
425 
426 static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
427 				   struct nilfs_super_block *sbp)
428 {
429 	u64 nsegments, nblocks;
430 
431 	if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
432 		nilfs_err(nilfs->ns_sb,
433 			  "unsupported revision (superblock rev.=%d.%d, current rev.=%d.%d). Please check the version of mkfs.nilfs(2).",
434 			  le32_to_cpu(sbp->s_rev_level),
435 			  le16_to_cpu(sbp->s_minor_rev_level),
436 			  NILFS_CURRENT_REV, NILFS_MINOR_REV);
437 		return -EINVAL;
438 	}
439 	nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
440 	if (nilfs->ns_sbsize > BLOCK_SIZE)
441 		return -EINVAL;
442 
443 	nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
444 	if (nilfs->ns_inode_size > nilfs->ns_blocksize) {
445 		nilfs_err(nilfs->ns_sb, "too large inode size: %d bytes",
446 			  nilfs->ns_inode_size);
447 		return -EINVAL;
448 	} else if (nilfs->ns_inode_size < NILFS_MIN_INODE_SIZE) {
449 		nilfs_err(nilfs->ns_sb, "too small inode size: %d bytes",
450 			  nilfs->ns_inode_size);
451 		return -EINVAL;
452 	}
453 
454 	nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
455 
456 	nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
457 	if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
458 		nilfs_err(nilfs->ns_sb, "too short segment: %lu blocks",
459 			  nilfs->ns_blocks_per_segment);
460 		return -EINVAL;
461 	}
462 
463 	nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
464 	nilfs->ns_r_segments_percentage =
465 		le32_to_cpu(sbp->s_r_segments_percentage);
466 	if (nilfs->ns_r_segments_percentage < 1 ||
467 	    nilfs->ns_r_segments_percentage > 99) {
468 		nilfs_err(nilfs->ns_sb,
469 			  "invalid reserved segments percentage: %lu",
470 			  nilfs->ns_r_segments_percentage);
471 		return -EINVAL;
472 	}
473 
474 	nsegments = le64_to_cpu(sbp->s_nsegments);
475 	if (nsegments > nilfs_max_segment_count(nilfs)) {
476 		nilfs_err(nilfs->ns_sb,
477 			  "segment count %llu exceeds upper limit (%llu segments)",
478 			  (unsigned long long)nsegments,
479 			  (unsigned long long)nilfs_max_segment_count(nilfs));
480 		return -EINVAL;
481 	}
482 
483 	nblocks = sb_bdev_nr_blocks(nilfs->ns_sb);
484 	if (nblocks) {
485 		u64 min_block_count = nsegments * nilfs->ns_blocks_per_segment;
486 		/*
487 		 * To avoid failing to mount early device images without a
488 		 * second superblock, exclude that block count from the
489 		 * "min_block_count" calculation.
490 		 */
491 
492 		if (nblocks < min_block_count) {
493 			nilfs_err(nilfs->ns_sb,
494 				  "total number of segment blocks %llu exceeds device size (%llu blocks)",
495 				  (unsigned long long)min_block_count,
496 				  (unsigned long long)nblocks);
497 			return -EINVAL;
498 		}
499 	}
500 
501 	nilfs_set_nsegments(nilfs, nsegments);
502 	nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
503 	return 0;
504 }
505 
506 static int nilfs_valid_sb(struct nilfs_super_block *sbp)
507 {
508 	static unsigned char sum[4];
509 	const int sumoff = offsetof(struct nilfs_super_block, s_sum);
510 	size_t bytes;
511 	u32 crc;
512 
513 	if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
514 		return 0;
515 	bytes = le16_to_cpu(sbp->s_bytes);
516 	if (bytes < sumoff + 4 || bytes > BLOCK_SIZE)
517 		return 0;
518 	crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
519 		       sumoff);
520 	crc = crc32_le(crc, sum, 4);
521 	crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
522 		       bytes - sumoff - 4);
523 	return crc == le32_to_cpu(sbp->s_sum);
524 }
525 
526 /**
527  * nilfs_sb2_bad_offset - check the location of the second superblock
528  * @sbp: superblock raw data buffer
529  * @offset: byte offset of second superblock calculated from device size
530  *
531  * nilfs_sb2_bad_offset() checks if the position on the second
532  * superblock is valid or not based on the filesystem parameters
533  * stored in @sbp.  If @offset points to a location within the segment
534  * area, or if the parameters themselves are not normal, it is
535  * determined to be invalid.
536  *
537  * Return Value: true if invalid, false if valid.
538  */
539 static bool nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
540 {
541 	unsigned int shift_bits = le32_to_cpu(sbp->s_log_block_size);
542 	u32 blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
543 	u64 nsegments = le64_to_cpu(sbp->s_nsegments);
544 	u64 index;
545 
546 	if (blocks_per_segment < NILFS_SEG_MIN_BLOCKS ||
547 	    shift_bits > ilog2(NILFS_MAX_BLOCK_SIZE) - BLOCK_SIZE_BITS)
548 		return true;
549 
550 	index = offset >> (shift_bits + BLOCK_SIZE_BITS);
551 	do_div(index, blocks_per_segment);
552 	return index < nsegments;
553 }
554 
555 static void nilfs_release_super_block(struct the_nilfs *nilfs)
556 {
557 	int i;
558 
559 	for (i = 0; i < 2; i++) {
560 		if (nilfs->ns_sbp[i]) {
561 			brelse(nilfs->ns_sbh[i]);
562 			nilfs->ns_sbh[i] = NULL;
563 			nilfs->ns_sbp[i] = NULL;
564 		}
565 	}
566 }
567 
568 void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
569 {
570 	brelse(nilfs->ns_sbh[0]);
571 	nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
572 	nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
573 	nilfs->ns_sbh[1] = NULL;
574 	nilfs->ns_sbp[1] = NULL;
575 }
576 
577 void nilfs_swap_super_block(struct the_nilfs *nilfs)
578 {
579 	struct buffer_head *tsbh = nilfs->ns_sbh[0];
580 	struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
581 
582 	nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
583 	nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
584 	nilfs->ns_sbh[1] = tsbh;
585 	nilfs->ns_sbp[1] = tsbp;
586 }
587 
588 static int nilfs_load_super_block(struct the_nilfs *nilfs,
589 				  struct super_block *sb, int blocksize,
590 				  struct nilfs_super_block **sbpp)
591 {
592 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
593 	struct buffer_head **sbh = nilfs->ns_sbh;
594 	u64 sb2off, devsize = bdev_nr_bytes(nilfs->ns_bdev);
595 	int valid[2], swp = 0, older;
596 
597 	if (devsize < NILFS_SEG_MIN_BLOCKS * NILFS_MIN_BLOCK_SIZE + 4096) {
598 		nilfs_err(sb, "device size too small");
599 		return -EINVAL;
600 	}
601 	sb2off = NILFS_SB2_OFFSET_BYTES(devsize);
602 
603 	sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
604 					&sbh[0]);
605 	sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
606 
607 	if (!sbp[0]) {
608 		if (!sbp[1]) {
609 			nilfs_err(sb, "unable to read superblock");
610 			return -EIO;
611 		}
612 		nilfs_warn(sb,
613 			   "unable to read primary superblock (blocksize = %d)",
614 			   blocksize);
615 	} else if (!sbp[1]) {
616 		nilfs_warn(sb,
617 			   "unable to read secondary superblock (blocksize = %d)",
618 			   blocksize);
619 	}
620 
621 	/*
622 	 * Compare two super blocks and set 1 in swp if the secondary
623 	 * super block is valid and newer.  Otherwise, set 0 in swp.
624 	 */
625 	valid[0] = nilfs_valid_sb(sbp[0]);
626 	valid[1] = nilfs_valid_sb(sbp[1]);
627 	swp = valid[1] && (!valid[0] ||
628 			   le64_to_cpu(sbp[1]->s_last_cno) >
629 			   le64_to_cpu(sbp[0]->s_last_cno));
630 
631 	if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
632 		brelse(sbh[1]);
633 		sbh[1] = NULL;
634 		sbp[1] = NULL;
635 		valid[1] = 0;
636 		swp = 0;
637 	}
638 	if (!valid[swp]) {
639 		nilfs_release_super_block(nilfs);
640 		nilfs_err(sb, "couldn't find nilfs on the device");
641 		return -EINVAL;
642 	}
643 
644 	if (!valid[!swp])
645 		nilfs_warn(sb,
646 			   "broken superblock, retrying with spare superblock (blocksize = %d)",
647 			   blocksize);
648 	if (swp)
649 		nilfs_swap_super_block(nilfs);
650 
651 	/*
652 	 * Calculate the array index of the older superblock data.
653 	 * If one has been dropped, set index 0 pointing to the remaining one,
654 	 * otherwise set index 1 pointing to the old one (including if both
655 	 * are the same).
656 	 *
657 	 *  Divided case             valid[0]  valid[1]  swp  ->  older
658 	 *  -------------------------------------------------------------
659 	 *  Both SBs are invalid        0         0       N/A (Error)
660 	 *  SB1 is invalid              0         1       1         0
661 	 *  SB2 is invalid              1         0       0         0
662 	 *  SB2 is newer                1         1       1         0
663 	 *  SB2 is older or the same    1         1       0         1
664 	 */
665 	older = valid[1] ^ swp;
666 
667 	nilfs->ns_sbwcount = 0;
668 	nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
669 	nilfs->ns_prot_seq = le64_to_cpu(sbp[older]->s_last_seq);
670 	*sbpp = sbp[0];
671 	return 0;
672 }
673 
674 /**
675  * init_nilfs - initialize a NILFS instance.
676  * @nilfs: the_nilfs structure
677  * @sb: super block
678  *
679  * init_nilfs() performs common initialization per block device (e.g.
680  * reading the super block, getting disk layout information, initializing
681  * shared fields in the_nilfs).
682  *
683  * Return Value: On success, 0 is returned. On error, a negative error
684  * code is returned.
685  */
686 int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb)
687 {
688 	struct nilfs_super_block *sbp;
689 	int blocksize;
690 	int err;
691 
692 	down_write(&nilfs->ns_sem);
693 
694 	blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
695 	if (!blocksize) {
696 		nilfs_err(sb, "unable to set blocksize");
697 		err = -EINVAL;
698 		goto out;
699 	}
700 	err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
701 	if (err)
702 		goto out;
703 
704 	err = nilfs_store_magic(sb, sbp);
705 	if (err)
706 		goto failed_sbh;
707 
708 	err = nilfs_check_feature_compatibility(sb, sbp);
709 	if (err)
710 		goto failed_sbh;
711 
712 	err = nilfs_get_blocksize(sb, sbp, &blocksize);
713 	if (err)
714 		goto failed_sbh;
715 
716 	if (blocksize < NILFS_MIN_BLOCK_SIZE) {
717 		nilfs_err(sb,
718 			  "couldn't mount because of unsupported filesystem blocksize %d",
719 			  blocksize);
720 		err = -EINVAL;
721 		goto failed_sbh;
722 	}
723 	if (sb->s_blocksize != blocksize) {
724 		int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
725 
726 		if (blocksize < hw_blocksize) {
727 			nilfs_err(sb,
728 				  "blocksize %d too small for device (sector-size = %d)",
729 				  blocksize, hw_blocksize);
730 			err = -EINVAL;
731 			goto failed_sbh;
732 		}
733 		nilfs_release_super_block(nilfs);
734 		if (!sb_set_blocksize(sb, blocksize)) {
735 			nilfs_err(sb, "bad blocksize %d", blocksize);
736 			err = -EINVAL;
737 			goto out;
738 		}
739 
740 		err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
741 		if (err)
742 			goto out;
743 			/*
744 			 * Not to failed_sbh; sbh is released automatically
745 			 * when reloading fails.
746 			 */
747 	}
748 	nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
749 	nilfs->ns_blocksize = blocksize;
750 
751 	get_random_bytes(&nilfs->ns_next_generation,
752 			 sizeof(nilfs->ns_next_generation));
753 
754 	err = nilfs_store_disk_layout(nilfs, sbp);
755 	if (err)
756 		goto failed_sbh;
757 
758 	sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
759 
760 	nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
761 
762 	err = nilfs_store_log_cursor(nilfs, sbp);
763 	if (err)
764 		goto failed_sbh;
765 
766 	set_nilfs_init(nilfs);
767 	err = 0;
768  out:
769 	up_write(&nilfs->ns_sem);
770 	return err;
771 
772  failed_sbh:
773 	nilfs_release_super_block(nilfs);
774 	goto out;
775 }
776 
777 int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
778 			    size_t nsegs)
779 {
780 	sector_t seg_start, seg_end;
781 	sector_t start = 0, nblocks = 0;
782 	unsigned int sects_per_block;
783 	__u64 *sn;
784 	int ret = 0;
785 
786 	sects_per_block = (1 << nilfs->ns_blocksize_bits) /
787 		bdev_logical_block_size(nilfs->ns_bdev);
788 	for (sn = segnump; sn < segnump + nsegs; sn++) {
789 		nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
790 
791 		if (!nblocks) {
792 			start = seg_start;
793 			nblocks = seg_end - seg_start + 1;
794 		} else if (start + nblocks == seg_start) {
795 			nblocks += seg_end - seg_start + 1;
796 		} else {
797 			ret = blkdev_issue_discard(nilfs->ns_bdev,
798 						   start * sects_per_block,
799 						   nblocks * sects_per_block,
800 						   GFP_NOFS);
801 			if (ret < 0)
802 				return ret;
803 			nblocks = 0;
804 		}
805 	}
806 	if (nblocks)
807 		ret = blkdev_issue_discard(nilfs->ns_bdev,
808 					   start * sects_per_block,
809 					   nblocks * sects_per_block,
810 					   GFP_NOFS);
811 	return ret;
812 }
813 
814 int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
815 {
816 	unsigned long ncleansegs;
817 
818 	ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
819 	*nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
820 	return 0;
821 }
822 
823 int nilfs_near_disk_full(struct the_nilfs *nilfs)
824 {
825 	unsigned long ncleansegs, nincsegs;
826 
827 	ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
828 	nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
829 		nilfs->ns_blocks_per_segment + 1;
830 
831 	return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
832 }
833 
834 struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
835 {
836 	struct rb_node *n;
837 	struct nilfs_root *root;
838 
839 	spin_lock(&nilfs->ns_cptree_lock);
840 	n = nilfs->ns_cptree.rb_node;
841 	while (n) {
842 		root = rb_entry(n, struct nilfs_root, rb_node);
843 
844 		if (cno < root->cno) {
845 			n = n->rb_left;
846 		} else if (cno > root->cno) {
847 			n = n->rb_right;
848 		} else {
849 			refcount_inc(&root->count);
850 			spin_unlock(&nilfs->ns_cptree_lock);
851 			return root;
852 		}
853 	}
854 	spin_unlock(&nilfs->ns_cptree_lock);
855 
856 	return NULL;
857 }
858 
859 struct nilfs_root *
860 nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
861 {
862 	struct rb_node **p, *parent;
863 	struct nilfs_root *root, *new;
864 	int err;
865 
866 	root = nilfs_lookup_root(nilfs, cno);
867 	if (root)
868 		return root;
869 
870 	new = kzalloc(sizeof(*root), GFP_KERNEL);
871 	if (!new)
872 		return NULL;
873 
874 	spin_lock(&nilfs->ns_cptree_lock);
875 
876 	p = &nilfs->ns_cptree.rb_node;
877 	parent = NULL;
878 
879 	while (*p) {
880 		parent = *p;
881 		root = rb_entry(parent, struct nilfs_root, rb_node);
882 
883 		if (cno < root->cno) {
884 			p = &(*p)->rb_left;
885 		} else if (cno > root->cno) {
886 			p = &(*p)->rb_right;
887 		} else {
888 			refcount_inc(&root->count);
889 			spin_unlock(&nilfs->ns_cptree_lock);
890 			kfree(new);
891 			return root;
892 		}
893 	}
894 
895 	new->cno = cno;
896 	new->ifile = NULL;
897 	new->nilfs = nilfs;
898 	refcount_set(&new->count, 1);
899 	atomic64_set(&new->inodes_count, 0);
900 	atomic64_set(&new->blocks_count, 0);
901 
902 	rb_link_node(&new->rb_node, parent, p);
903 	rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
904 
905 	spin_unlock(&nilfs->ns_cptree_lock);
906 
907 	err = nilfs_sysfs_create_snapshot_group(new);
908 	if (err) {
909 		kfree(new);
910 		new = NULL;
911 	}
912 
913 	return new;
914 }
915 
916 void nilfs_put_root(struct nilfs_root *root)
917 {
918 	struct the_nilfs *nilfs = root->nilfs;
919 
920 	if (refcount_dec_and_lock(&root->count, &nilfs->ns_cptree_lock)) {
921 		rb_erase(&root->rb_node, &nilfs->ns_cptree);
922 		spin_unlock(&nilfs->ns_cptree_lock);
923 
924 		nilfs_sysfs_delete_snapshot_group(root);
925 		iput(root->ifile);
926 
927 		kfree(root);
928 	}
929 }
930