• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * the_nilfs.c - 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 
nilfs_set_last_segment(struct the_nilfs * nilfs,sector_t start_blocknr,u64 seq,__u64 cno)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  */
alloc_nilfs(struct super_block * sb)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  */
destroy_nilfs(struct the_nilfs * nilfs)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 
nilfs_load_super_root(struct the_nilfs * nilfs,struct super_block * sb,sector_t sr_block)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 
nilfs_init_recovery_info(struct nilfs_recovery_info * ri)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 
nilfs_clear_recovery_info(struct nilfs_recovery_info * ri)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  */
nilfs_store_log_cursor(struct the_nilfs * nilfs,struct nilfs_super_block * sbp)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  * load_nilfs - load and recover the nilfs
197  * @nilfs: the_nilfs structure to be released
198  * @sb: super block isntance used to recover past segment
199  *
200  * load_nilfs() searches and load the latest super root,
201  * attaches the last segment, and does recovery if needed.
202  * The caller must call this exclusively for simultaneous mounts.
203  */
load_nilfs(struct the_nilfs * nilfs,struct super_block * sb)204 int load_nilfs(struct the_nilfs *nilfs, struct super_block *sb)
205 {
206 	struct nilfs_recovery_info ri;
207 	unsigned int s_flags = sb->s_flags;
208 	int really_read_only = bdev_read_only(nilfs->ns_bdev);
209 	int valid_fs = nilfs_valid_fs(nilfs);
210 	int err;
211 
212 	if (!valid_fs) {
213 		nilfs_warn(sb, "mounting unchecked fs");
214 		if (s_flags & SB_RDONLY) {
215 			nilfs_info(sb,
216 				   "recovery required for readonly filesystem");
217 			nilfs_info(sb,
218 				   "write access will be enabled during recovery");
219 		}
220 	}
221 
222 	nilfs_init_recovery_info(&ri);
223 
224 	err = nilfs_search_super_root(nilfs, &ri);
225 	if (unlikely(err)) {
226 		struct nilfs_super_block **sbp = nilfs->ns_sbp;
227 		int blocksize;
228 
229 		if (err != -EINVAL)
230 			goto scan_error;
231 
232 		if (!nilfs_valid_sb(sbp[1])) {
233 			nilfs_warn(sb,
234 				   "unable to fall back to spare super block");
235 			goto scan_error;
236 		}
237 		nilfs_info(sb, "trying rollback from an earlier position");
238 
239 		/*
240 		 * restore super block with its spare and reconfigure
241 		 * relevant states of the nilfs object.
242 		 */
243 		memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
244 		nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
245 		nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
246 
247 		/* verify consistency between two super blocks */
248 		blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
249 		if (blocksize != nilfs->ns_blocksize) {
250 			nilfs_warn(sb,
251 				   "blocksize differs between two super blocks (%d != %d)",
252 				   blocksize, nilfs->ns_blocksize);
253 			goto scan_error;
254 		}
255 
256 		err = nilfs_store_log_cursor(nilfs, sbp[0]);
257 		if (err)
258 			goto scan_error;
259 
260 		/* drop clean flag to allow roll-forward and recovery */
261 		nilfs->ns_mount_state &= ~NILFS_VALID_FS;
262 		valid_fs = 0;
263 
264 		err = nilfs_search_super_root(nilfs, &ri);
265 		if (err)
266 			goto scan_error;
267 	}
268 
269 	err = nilfs_load_super_root(nilfs, sb, ri.ri_super_root);
270 	if (unlikely(err)) {
271 		nilfs_err(sb, "error %d while loading super root", err);
272 		goto failed;
273 	}
274 
275 	err = nilfs_sysfs_create_device_group(sb);
276 	if (unlikely(err))
277 		goto sysfs_error;
278 
279 	if (valid_fs)
280 		goto skip_recovery;
281 
282 	if (s_flags & SB_RDONLY) {
283 		__u64 features;
284 
285 		if (nilfs_test_opt(nilfs, NORECOVERY)) {
286 			nilfs_info(sb,
287 				   "norecovery option specified, skipping roll-forward recovery");
288 			goto skip_recovery;
289 		}
290 		features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
291 			~NILFS_FEATURE_COMPAT_RO_SUPP;
292 		if (features) {
293 			nilfs_err(sb,
294 				  "couldn't proceed with recovery because of unsupported optional features (%llx)",
295 				  (unsigned long long)features);
296 			err = -EROFS;
297 			goto failed_unload;
298 		}
299 		if (really_read_only) {
300 			nilfs_err(sb,
301 				  "write access unavailable, cannot proceed");
302 			err = -EROFS;
303 			goto failed_unload;
304 		}
305 		sb->s_flags &= ~SB_RDONLY;
306 	} else if (nilfs_test_opt(nilfs, NORECOVERY)) {
307 		nilfs_err(sb,
308 			  "recovery cancelled because norecovery option was specified for a read/write mount");
309 		err = -EINVAL;
310 		goto failed_unload;
311 	}
312 
313 	err = nilfs_salvage_orphan_logs(nilfs, sb, &ri);
314 	if (err)
315 		goto failed_unload;
316 
317 	down_write(&nilfs->ns_sem);
318 	nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
319 	err = nilfs_cleanup_super(sb);
320 	up_write(&nilfs->ns_sem);
321 
322 	if (err) {
323 		nilfs_err(sb,
324 			  "error %d updating super block. recovery unfinished.",
325 			  err);
326 		goto failed_unload;
327 	}
328 	nilfs_info(sb, "recovery complete");
329 
330  skip_recovery:
331 	nilfs_clear_recovery_info(&ri);
332 	sb->s_flags = s_flags;
333 	return 0;
334 
335  scan_error:
336 	nilfs_err(sb, "error %d while searching super root", err);
337 	goto failed;
338 
339  failed_unload:
340 	nilfs_sysfs_delete_device_group(nilfs);
341 
342  sysfs_error:
343 	iput(nilfs->ns_cpfile);
344 	iput(nilfs->ns_sufile);
345 	iput(nilfs->ns_dat);
346 
347  failed:
348 	nilfs_clear_recovery_info(&ri);
349 	sb->s_flags = s_flags;
350 	return err;
351 }
352 
nilfs_max_size(unsigned int blkbits)353 static unsigned long long nilfs_max_size(unsigned int blkbits)
354 {
355 	unsigned int max_bits;
356 	unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
357 
358 	max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
359 	if (max_bits < 64)
360 		res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
361 	return res;
362 }
363 
364 /**
365  * nilfs_nrsvsegs - calculate the number of reserved segments
366  * @nilfs: nilfs object
367  * @nsegs: total number of segments
368  */
nilfs_nrsvsegs(struct the_nilfs * nilfs,unsigned long nsegs)369 unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs)
370 {
371 	return max_t(unsigned long, NILFS_MIN_NRSVSEGS,
372 		     DIV_ROUND_UP(nsegs * nilfs->ns_r_segments_percentage,
373 				  100));
374 }
375 
376 /**
377  * nilfs_max_segment_count - calculate the maximum number of segments
378  * @nilfs: nilfs object
379  */
nilfs_max_segment_count(struct the_nilfs * nilfs)380 static u64 nilfs_max_segment_count(struct the_nilfs *nilfs)
381 {
382 	u64 max_count = U64_MAX;
383 
384 	do_div(max_count, nilfs->ns_blocks_per_segment);
385 	return min_t(u64, max_count, ULONG_MAX);
386 }
387 
nilfs_set_nsegments(struct the_nilfs * nilfs,unsigned long nsegs)388 void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs)
389 {
390 	nilfs->ns_nsegments = nsegs;
391 	nilfs->ns_nrsvsegs = nilfs_nrsvsegs(nilfs, nsegs);
392 }
393 
nilfs_store_disk_layout(struct the_nilfs * nilfs,struct nilfs_super_block * sbp)394 static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
395 				   struct nilfs_super_block *sbp)
396 {
397 	u64 nsegments, nblocks;
398 
399 	if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
400 		nilfs_err(nilfs->ns_sb,
401 			  "unsupported revision (superblock rev.=%d.%d, current rev.=%d.%d). Please check the version of mkfs.nilfs(2).",
402 			  le32_to_cpu(sbp->s_rev_level),
403 			  le16_to_cpu(sbp->s_minor_rev_level),
404 			  NILFS_CURRENT_REV, NILFS_MINOR_REV);
405 		return -EINVAL;
406 	}
407 	nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
408 	if (nilfs->ns_sbsize > BLOCK_SIZE)
409 		return -EINVAL;
410 
411 	nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
412 	if (nilfs->ns_inode_size > nilfs->ns_blocksize) {
413 		nilfs_err(nilfs->ns_sb, "too large inode size: %d bytes",
414 			  nilfs->ns_inode_size);
415 		return -EINVAL;
416 	} else if (nilfs->ns_inode_size < NILFS_MIN_INODE_SIZE) {
417 		nilfs_err(nilfs->ns_sb, "too small inode size: %d bytes",
418 			  nilfs->ns_inode_size);
419 		return -EINVAL;
420 	}
421 
422 	nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
423 
424 	nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
425 	if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
426 		nilfs_err(nilfs->ns_sb, "too short segment: %lu blocks",
427 			  nilfs->ns_blocks_per_segment);
428 		return -EINVAL;
429 	}
430 
431 	nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
432 	nilfs->ns_r_segments_percentage =
433 		le32_to_cpu(sbp->s_r_segments_percentage);
434 	if (nilfs->ns_r_segments_percentage < 1 ||
435 	    nilfs->ns_r_segments_percentage > 99) {
436 		nilfs_err(nilfs->ns_sb,
437 			  "invalid reserved segments percentage: %lu",
438 			  nilfs->ns_r_segments_percentage);
439 		return -EINVAL;
440 	}
441 
442 	nsegments = le64_to_cpu(sbp->s_nsegments);
443 	if (nsegments > nilfs_max_segment_count(nilfs)) {
444 		nilfs_msg(nilfs->ns_sb, KERN_ERR,
445 			  "segment count %llu exceeds upper limit (%llu segments)",
446 			  (unsigned long long)nsegments,
447 			  (unsigned long long)nilfs_max_segment_count(nilfs));
448 		return -EINVAL;
449 	}
450 
451 	nblocks = (u64)i_size_read(nilfs->ns_sb->s_bdev->bd_inode) >>
452 		nilfs->ns_sb->s_blocksize_bits;
453 	if (nblocks) {
454 		u64 min_block_count = nsegments * nilfs->ns_blocks_per_segment;
455 		/*
456 		 * To avoid failing to mount early device images without a
457 		 * second superblock, exclude that block count from the
458 		 * "min_block_count" calculation.
459 		 */
460 
461 		if (nblocks < min_block_count) {
462 			nilfs_msg(nilfs->ns_sb, KERN_ERR,
463 				  "total number of segment blocks %llu exceeds device size (%llu blocks)",
464 				  (unsigned long long)min_block_count,
465 				  (unsigned long long)nblocks);
466 			return -EINVAL;
467 		}
468 	}
469 
470 	nilfs_set_nsegments(nilfs, nsegments);
471 	nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
472 	return 0;
473 }
474 
nilfs_valid_sb(struct nilfs_super_block * sbp)475 static int nilfs_valid_sb(struct nilfs_super_block *sbp)
476 {
477 	static unsigned char sum[4];
478 	const int sumoff = offsetof(struct nilfs_super_block, s_sum);
479 	size_t bytes;
480 	u32 crc;
481 
482 	if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
483 		return 0;
484 	bytes = le16_to_cpu(sbp->s_bytes);
485 	if (bytes < sumoff + 4 || bytes > BLOCK_SIZE)
486 		return 0;
487 	crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
488 		       sumoff);
489 	crc = crc32_le(crc, sum, 4);
490 	crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
491 		       bytes - sumoff - 4);
492 	return crc == le32_to_cpu(sbp->s_sum);
493 }
494 
495 /**
496  * nilfs_sb2_bad_offset - check the location of the second superblock
497  * @sbp: superblock raw data buffer
498  * @offset: byte offset of second superblock calculated from device size
499  *
500  * nilfs_sb2_bad_offset() checks if the position on the second
501  * superblock is valid or not based on the filesystem parameters
502  * stored in @sbp.  If @offset points to a location within the segment
503  * area, or if the parameters themselves are not normal, it is
504  * determined to be invalid.
505  *
506  * Return Value: true if invalid, false if valid.
507  */
nilfs_sb2_bad_offset(struct nilfs_super_block * sbp,u64 offset)508 static bool nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
509 {
510 	unsigned int shift_bits = le32_to_cpu(sbp->s_log_block_size);
511 	u32 blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
512 	u64 nsegments = le64_to_cpu(sbp->s_nsegments);
513 	u64 index;
514 
515 	if (blocks_per_segment < NILFS_SEG_MIN_BLOCKS ||
516 	    shift_bits > ilog2(NILFS_MAX_BLOCK_SIZE) - BLOCK_SIZE_BITS)
517 		return true;
518 
519 	index = offset >> (shift_bits + BLOCK_SIZE_BITS);
520 	do_div(index, blocks_per_segment);
521 	return index < nsegments;
522 }
523 
nilfs_release_super_block(struct the_nilfs * nilfs)524 static void nilfs_release_super_block(struct the_nilfs *nilfs)
525 {
526 	int i;
527 
528 	for (i = 0; i < 2; i++) {
529 		if (nilfs->ns_sbp[i]) {
530 			brelse(nilfs->ns_sbh[i]);
531 			nilfs->ns_sbh[i] = NULL;
532 			nilfs->ns_sbp[i] = NULL;
533 		}
534 	}
535 }
536 
nilfs_fall_back_super_block(struct the_nilfs * nilfs)537 void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
538 {
539 	brelse(nilfs->ns_sbh[0]);
540 	nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
541 	nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
542 	nilfs->ns_sbh[1] = NULL;
543 	nilfs->ns_sbp[1] = NULL;
544 }
545 
nilfs_swap_super_block(struct the_nilfs * nilfs)546 void nilfs_swap_super_block(struct the_nilfs *nilfs)
547 {
548 	struct buffer_head *tsbh = nilfs->ns_sbh[0];
549 	struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
550 
551 	nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
552 	nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
553 	nilfs->ns_sbh[1] = tsbh;
554 	nilfs->ns_sbp[1] = tsbp;
555 }
556 
nilfs_load_super_block(struct the_nilfs * nilfs,struct super_block * sb,int blocksize,struct nilfs_super_block ** sbpp)557 static int nilfs_load_super_block(struct the_nilfs *nilfs,
558 				  struct super_block *sb, int blocksize,
559 				  struct nilfs_super_block **sbpp)
560 {
561 	struct nilfs_super_block **sbp = nilfs->ns_sbp;
562 	struct buffer_head **sbh = nilfs->ns_sbh;
563 	u64 sb2off, devsize = nilfs->ns_bdev->bd_inode->i_size;
564 	int valid[2], swp = 0;
565 
566 	if (devsize < NILFS_SEG_MIN_BLOCKS * NILFS_MIN_BLOCK_SIZE + 4096) {
567 		nilfs_msg(sb, KERN_ERR, "device size too small");
568 		return -EINVAL;
569 	}
570 	sb2off = NILFS_SB2_OFFSET_BYTES(devsize);
571 
572 	sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
573 					&sbh[0]);
574 	sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
575 
576 	if (!sbp[0]) {
577 		if (!sbp[1]) {
578 			nilfs_err(sb, "unable to read superblock");
579 			return -EIO;
580 		}
581 		nilfs_warn(sb,
582 			   "unable to read primary superblock (blocksize = %d)",
583 			   blocksize);
584 	} else if (!sbp[1]) {
585 		nilfs_warn(sb,
586 			   "unable to read secondary superblock (blocksize = %d)",
587 			   blocksize);
588 	}
589 
590 	/*
591 	 * Compare two super blocks and set 1 in swp if the secondary
592 	 * super block is valid and newer.  Otherwise, set 0 in swp.
593 	 */
594 	valid[0] = nilfs_valid_sb(sbp[0]);
595 	valid[1] = nilfs_valid_sb(sbp[1]);
596 	swp = valid[1] && (!valid[0] ||
597 			   le64_to_cpu(sbp[1]->s_last_cno) >
598 			   le64_to_cpu(sbp[0]->s_last_cno));
599 
600 	if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
601 		brelse(sbh[1]);
602 		sbh[1] = NULL;
603 		sbp[1] = NULL;
604 		valid[1] = 0;
605 		swp = 0;
606 	}
607 	if (!valid[swp]) {
608 		nilfs_release_super_block(nilfs);
609 		nilfs_err(sb, "couldn't find nilfs on the device");
610 		return -EINVAL;
611 	}
612 
613 	if (!valid[!swp])
614 		nilfs_warn(sb,
615 			   "broken superblock, retrying with spare superblock (blocksize = %d)",
616 			   blocksize);
617 	if (swp)
618 		nilfs_swap_super_block(nilfs);
619 
620 	nilfs->ns_sbwcount = 0;
621 	nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
622 	nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
623 	*sbpp = sbp[0];
624 	return 0;
625 }
626 
627 /**
628  * init_nilfs - initialize a NILFS instance.
629  * @nilfs: the_nilfs structure
630  * @sb: super block
631  * @data: mount options
632  *
633  * init_nilfs() performs common initialization per block device (e.g.
634  * reading the super block, getting disk layout information, initializing
635  * shared fields in the_nilfs).
636  *
637  * Return Value: On success, 0 is returned. On error, a negative error
638  * code is returned.
639  */
init_nilfs(struct the_nilfs * nilfs,struct super_block * sb,char * data)640 int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb, char *data)
641 {
642 	struct nilfs_super_block *sbp;
643 	int blocksize;
644 	int err;
645 
646 	down_write(&nilfs->ns_sem);
647 
648 	blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
649 	if (!blocksize) {
650 		nilfs_err(sb, "unable to set blocksize");
651 		err = -EINVAL;
652 		goto out;
653 	}
654 	err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
655 	if (err)
656 		goto out;
657 
658 	err = nilfs_store_magic_and_option(sb, sbp, data);
659 	if (err)
660 		goto failed_sbh;
661 
662 	err = nilfs_check_feature_compatibility(sb, sbp);
663 	if (err)
664 		goto failed_sbh;
665 
666 	blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
667 	if (blocksize < NILFS_MIN_BLOCK_SIZE ||
668 	    blocksize > NILFS_MAX_BLOCK_SIZE) {
669 		nilfs_err(sb,
670 			  "couldn't mount because of unsupported filesystem blocksize %d",
671 			  blocksize);
672 		err = -EINVAL;
673 		goto failed_sbh;
674 	}
675 	if (sb->s_blocksize != blocksize) {
676 		int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
677 
678 		if (blocksize < hw_blocksize) {
679 			nilfs_err(sb,
680 				  "blocksize %d too small for device (sector-size = %d)",
681 				  blocksize, hw_blocksize);
682 			err = -EINVAL;
683 			goto failed_sbh;
684 		}
685 		nilfs_release_super_block(nilfs);
686 		if (!sb_set_blocksize(sb, blocksize)) {
687 			nilfs_msg(sb, KERN_ERR, "bad blocksize %d", blocksize);
688 			err = -EINVAL;
689 			goto out;
690 		}
691 
692 		err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
693 		if (err)
694 			goto out;
695 			/*
696 			 * Not to failed_sbh; sbh is released automatically
697 			 * when reloading fails.
698 			 */
699 	}
700 	nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
701 	nilfs->ns_blocksize = blocksize;
702 
703 	get_random_bytes(&nilfs->ns_next_generation,
704 			 sizeof(nilfs->ns_next_generation));
705 
706 	err = nilfs_store_disk_layout(nilfs, sbp);
707 	if (err)
708 		goto failed_sbh;
709 
710 	sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
711 
712 	nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
713 
714 	err = nilfs_store_log_cursor(nilfs, sbp);
715 	if (err)
716 		goto failed_sbh;
717 
718 	set_nilfs_init(nilfs);
719 	err = 0;
720  out:
721 	up_write(&nilfs->ns_sem);
722 	return err;
723 
724  failed_sbh:
725 	nilfs_release_super_block(nilfs);
726 	goto out;
727 }
728 
nilfs_discard_segments(struct the_nilfs * nilfs,__u64 * segnump,size_t nsegs)729 int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
730 			    size_t nsegs)
731 {
732 	sector_t seg_start, seg_end;
733 	sector_t start = 0, nblocks = 0;
734 	unsigned int sects_per_block;
735 	__u64 *sn;
736 	int ret = 0;
737 
738 	sects_per_block = (1 << nilfs->ns_blocksize_bits) /
739 		bdev_logical_block_size(nilfs->ns_bdev);
740 	for (sn = segnump; sn < segnump + nsegs; sn++) {
741 		nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
742 
743 		if (!nblocks) {
744 			start = seg_start;
745 			nblocks = seg_end - seg_start + 1;
746 		} else if (start + nblocks == seg_start) {
747 			nblocks += seg_end - seg_start + 1;
748 		} else {
749 			ret = blkdev_issue_discard(nilfs->ns_bdev,
750 						   start * sects_per_block,
751 						   nblocks * sects_per_block,
752 						   GFP_NOFS, 0);
753 			if (ret < 0)
754 				return ret;
755 			nblocks = 0;
756 		}
757 	}
758 	if (nblocks)
759 		ret = blkdev_issue_discard(nilfs->ns_bdev,
760 					   start * sects_per_block,
761 					   nblocks * sects_per_block,
762 					   GFP_NOFS, 0);
763 	return ret;
764 }
765 
nilfs_count_free_blocks(struct the_nilfs * nilfs,sector_t * nblocks)766 int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
767 {
768 	unsigned long ncleansegs;
769 
770 	ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
771 	*nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
772 	return 0;
773 }
774 
nilfs_near_disk_full(struct the_nilfs * nilfs)775 int nilfs_near_disk_full(struct the_nilfs *nilfs)
776 {
777 	unsigned long ncleansegs, nincsegs;
778 
779 	ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
780 	nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
781 		nilfs->ns_blocks_per_segment + 1;
782 
783 	return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
784 }
785 
nilfs_lookup_root(struct the_nilfs * nilfs,__u64 cno)786 struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
787 {
788 	struct rb_node *n;
789 	struct nilfs_root *root;
790 
791 	spin_lock(&nilfs->ns_cptree_lock);
792 	n = nilfs->ns_cptree.rb_node;
793 	while (n) {
794 		root = rb_entry(n, struct nilfs_root, rb_node);
795 
796 		if (cno < root->cno) {
797 			n = n->rb_left;
798 		} else if (cno > root->cno) {
799 			n = n->rb_right;
800 		} else {
801 			refcount_inc(&root->count);
802 			spin_unlock(&nilfs->ns_cptree_lock);
803 			return root;
804 		}
805 	}
806 	spin_unlock(&nilfs->ns_cptree_lock);
807 
808 	return NULL;
809 }
810 
811 struct nilfs_root *
nilfs_find_or_create_root(struct the_nilfs * nilfs,__u64 cno)812 nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
813 {
814 	struct rb_node **p, *parent;
815 	struct nilfs_root *root, *new;
816 	int err;
817 
818 	root = nilfs_lookup_root(nilfs, cno);
819 	if (root)
820 		return root;
821 
822 	new = kzalloc(sizeof(*root), GFP_KERNEL);
823 	if (!new)
824 		return NULL;
825 
826 	spin_lock(&nilfs->ns_cptree_lock);
827 
828 	p = &nilfs->ns_cptree.rb_node;
829 	parent = NULL;
830 
831 	while (*p) {
832 		parent = *p;
833 		root = rb_entry(parent, struct nilfs_root, rb_node);
834 
835 		if (cno < root->cno) {
836 			p = &(*p)->rb_left;
837 		} else if (cno > root->cno) {
838 			p = &(*p)->rb_right;
839 		} else {
840 			refcount_inc(&root->count);
841 			spin_unlock(&nilfs->ns_cptree_lock);
842 			kfree(new);
843 			return root;
844 		}
845 	}
846 
847 	new->cno = cno;
848 	new->ifile = NULL;
849 	new->nilfs = nilfs;
850 	refcount_set(&new->count, 1);
851 	atomic64_set(&new->inodes_count, 0);
852 	atomic64_set(&new->blocks_count, 0);
853 
854 	rb_link_node(&new->rb_node, parent, p);
855 	rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
856 
857 	spin_unlock(&nilfs->ns_cptree_lock);
858 
859 	err = nilfs_sysfs_create_snapshot_group(new);
860 	if (err) {
861 		kfree(new);
862 		new = NULL;
863 	}
864 
865 	return new;
866 }
867 
nilfs_put_root(struct nilfs_root * root)868 void nilfs_put_root(struct nilfs_root *root)
869 {
870 	struct the_nilfs *nilfs = root->nilfs;
871 
872 	if (refcount_dec_and_lock(&root->count, &nilfs->ns_cptree_lock)) {
873 		rb_erase(&root->rb_node, &nilfs->ns_cptree);
874 		spin_unlock(&nilfs->ns_cptree_lock);
875 
876 		nilfs_sysfs_delete_snapshot_group(root);
877 		iput(root->ifile);
878 
879 		kfree(root);
880 	}
881 }
882