blob: fa5d29660ed2fa4e38b89ab31ccf01abcb9872f6 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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
27static int nilfs_valid_sb(struct nilfs_super_block *sbp);
28
29void 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 */
56struct 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 */
86void 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
96static 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
150static 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
156static 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 */
171static 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 */
204int 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
353static 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 */
369unsigned 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 */
380static 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
388void 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
394static 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 if (nilfs->ns_first_ino < NILFS_USER_INO) {
424 nilfs_err(nilfs->ns_sb,
425 "too small lower limit for non-reserved inode numbers: %u",
426 nilfs->ns_first_ino);
427 return -EINVAL;
428 }
429
430 nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
431 if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
432 nilfs_err(nilfs->ns_sb, "too short segment: %lu blocks",
433 nilfs->ns_blocks_per_segment);
434 return -EINVAL;
435 }
436
437 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
438 nilfs->ns_r_segments_percentage =
439 le32_to_cpu(sbp->s_r_segments_percentage);
440 if (nilfs->ns_r_segments_percentage < 1 ||
441 nilfs->ns_r_segments_percentage > 99) {
442 nilfs_err(nilfs->ns_sb,
443 "invalid reserved segments percentage: %lu",
444 nilfs->ns_r_segments_percentage);
445 return -EINVAL;
446 }
447
448 nsegments = le64_to_cpu(sbp->s_nsegments);
449 if (nsegments > nilfs_max_segment_count(nilfs)) {
450 nilfs_msg(nilfs->ns_sb, KERN_ERR,
451 "segment count %llu exceeds upper limit (%llu segments)",
452 (unsigned long long)nsegments,
453 (unsigned long long)nilfs_max_segment_count(nilfs));
454 return -EINVAL;
455 }
456
457 nblocks = (u64)i_size_read(nilfs->ns_sb->s_bdev->bd_inode) >>
458 nilfs->ns_sb->s_blocksize_bits;
459 if (nblocks) {
460 u64 min_block_count = nsegments * nilfs->ns_blocks_per_segment;
461 /*
462 * To avoid failing to mount early device images without a
463 * second superblock, exclude that block count from the
464 * "min_block_count" calculation.
465 */
466
467 if (nblocks < min_block_count) {
468 nilfs_msg(nilfs->ns_sb, KERN_ERR,
469 "total number of segment blocks %llu exceeds device size (%llu blocks)",
470 (unsigned long long)min_block_count,
471 (unsigned long long)nblocks);
472 return -EINVAL;
473 }
474 }
475
476 nilfs_set_nsegments(nilfs, nsegments);
477 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
478 return 0;
479}
480
481static int nilfs_valid_sb(struct nilfs_super_block *sbp)
482{
483 static unsigned char sum[4];
484 const int sumoff = offsetof(struct nilfs_super_block, s_sum);
485 size_t bytes;
486 u32 crc;
487
488 if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
489 return 0;
490 bytes = le16_to_cpu(sbp->s_bytes);
491 if (bytes < sumoff + 4 || bytes > BLOCK_SIZE)
492 return 0;
493 crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
494 sumoff);
495 crc = crc32_le(crc, sum, 4);
496 crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
497 bytes - sumoff - 4);
498 return crc == le32_to_cpu(sbp->s_sum);
499}
500
501/**
502 * nilfs_sb2_bad_offset - check the location of the second superblock
503 * @sbp: superblock raw data buffer
504 * @offset: byte offset of second superblock calculated from device size
505 *
506 * nilfs_sb2_bad_offset() checks if the position on the second
507 * superblock is valid or not based on the filesystem parameters
508 * stored in @sbp. If @offset points to a location within the segment
509 * area, or if the parameters themselves are not normal, it is
510 * determined to be invalid.
511 *
512 * Return Value: true if invalid, false if valid.
513 */
514static bool nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
515{
516 unsigned int shift_bits = le32_to_cpu(sbp->s_log_block_size);
517 u32 blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
518 u64 nsegments = le64_to_cpu(sbp->s_nsegments);
519 u64 index;
520
521 if (blocks_per_segment < NILFS_SEG_MIN_BLOCKS ||
522 shift_bits > ilog2(NILFS_MAX_BLOCK_SIZE) - BLOCK_SIZE_BITS)
523 return true;
524
525 index = offset >> (shift_bits + BLOCK_SIZE_BITS);
526 do_div(index, blocks_per_segment);
527 return index < nsegments;
528}
529
530static void nilfs_release_super_block(struct the_nilfs *nilfs)
531{
532 int i;
533
534 for (i = 0; i < 2; i++) {
535 if (nilfs->ns_sbp[i]) {
536 brelse(nilfs->ns_sbh[i]);
537 nilfs->ns_sbh[i] = NULL;
538 nilfs->ns_sbp[i] = NULL;
539 }
540 }
541}
542
543void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
544{
545 brelse(nilfs->ns_sbh[0]);
546 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
547 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
548 nilfs->ns_sbh[1] = NULL;
549 nilfs->ns_sbp[1] = NULL;
550}
551
552void nilfs_swap_super_block(struct the_nilfs *nilfs)
553{
554 struct buffer_head *tsbh = nilfs->ns_sbh[0];
555 struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
556
557 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
558 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
559 nilfs->ns_sbh[1] = tsbh;
560 nilfs->ns_sbp[1] = tsbp;
561}
562
563static int nilfs_load_super_block(struct the_nilfs *nilfs,
564 struct super_block *sb, int blocksize,
565 struct nilfs_super_block **sbpp)
566{
567 struct nilfs_super_block **sbp = nilfs->ns_sbp;
568 struct buffer_head **sbh = nilfs->ns_sbh;
569 u64 sb2off, devsize = nilfs->ns_bdev->bd_inode->i_size;
570 int valid[2], swp = 0;
571
572 if (devsize < NILFS_SEG_MIN_BLOCKS * NILFS_MIN_BLOCK_SIZE + 4096) {
573 nilfs_msg(sb, KERN_ERR, "device size too small");
574 return -EINVAL;
575 }
576 sb2off = NILFS_SB2_OFFSET_BYTES(devsize);
577
578 sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
579 &sbh[0]);
580 sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
581
582 if (!sbp[0]) {
583 if (!sbp[1]) {
584 nilfs_err(sb, "unable to read superblock");
585 return -EIO;
586 }
587 nilfs_warn(sb,
588 "unable to read primary superblock (blocksize = %d)",
589 blocksize);
590 } else if (!sbp[1]) {
591 nilfs_warn(sb,
592 "unable to read secondary superblock (blocksize = %d)",
593 blocksize);
594 }
595
596 /*
597 * Compare two super blocks and set 1 in swp if the secondary
598 * super block is valid and newer. Otherwise, set 0 in swp.
599 */
600 valid[0] = nilfs_valid_sb(sbp[0]);
601 valid[1] = nilfs_valid_sb(sbp[1]);
602 swp = valid[1] && (!valid[0] ||
603 le64_to_cpu(sbp[1]->s_last_cno) >
604 le64_to_cpu(sbp[0]->s_last_cno));
605
606 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
607 brelse(sbh[1]);
608 sbh[1] = NULL;
609 sbp[1] = NULL;
610 valid[1] = 0;
611 swp = 0;
612 }
613 if (!valid[swp]) {
614 nilfs_release_super_block(nilfs);
615 nilfs_err(sb, "couldn't find nilfs on the device");
616 return -EINVAL;
617 }
618
619 if (!valid[!swp])
620 nilfs_warn(sb,
621 "broken superblock, retrying with spare superblock (blocksize = %d)",
622 blocksize);
623 if (swp)
624 nilfs_swap_super_block(nilfs);
625
626 nilfs->ns_sbwcount = 0;
627 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
628 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
629 *sbpp = sbp[0];
630 return 0;
631}
632
633/**
634 * init_nilfs - initialize a NILFS instance.
635 * @nilfs: the_nilfs structure
636 * @sb: super block
637 * @data: mount options
638 *
639 * init_nilfs() performs common initialization per block device (e.g.
640 * reading the super block, getting disk layout information, initializing
641 * shared fields in the_nilfs).
642 *
643 * Return Value: On success, 0 is returned. On error, a negative error
644 * code is returned.
645 */
646int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb, char *data)
647{
648 struct nilfs_super_block *sbp;
649 int blocksize;
650 int err;
651
652 down_write(&nilfs->ns_sem);
653
654 blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
655 if (!blocksize) {
656 nilfs_err(sb, "unable to set blocksize");
657 err = -EINVAL;
658 goto out;
659 }
660 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
661 if (err)
662 goto out;
663
664 err = nilfs_store_magic_and_option(sb, sbp, data);
665 if (err)
666 goto failed_sbh;
667
668 err = nilfs_check_feature_compatibility(sb, sbp);
669 if (err)
670 goto failed_sbh;
671
672 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
673 if (blocksize < NILFS_MIN_BLOCK_SIZE ||
674 blocksize > NILFS_MAX_BLOCK_SIZE) {
675 nilfs_err(sb,
676 "couldn't mount because of unsupported filesystem blocksize %d",
677 blocksize);
678 err = -EINVAL;
679 goto failed_sbh;
680 }
681 if (sb->s_blocksize != blocksize) {
682 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
683
684 if (blocksize < hw_blocksize) {
685 nilfs_err(sb,
686 "blocksize %d too small for device (sector-size = %d)",
687 blocksize, hw_blocksize);
688 err = -EINVAL;
689 goto failed_sbh;
690 }
691 nilfs_release_super_block(nilfs);
692 if (!sb_set_blocksize(sb, blocksize)) {
693 nilfs_msg(sb, KERN_ERR, "bad blocksize %d", blocksize);
694 err = -EINVAL;
695 goto out;
696 }
697
698 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
699 if (err)
700 goto out;
701 /*
702 * Not to failed_sbh; sbh is released automatically
703 * when reloading fails.
704 */
705 }
706 nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
707 nilfs->ns_blocksize = blocksize;
708
709 get_random_bytes(&nilfs->ns_next_generation,
710 sizeof(nilfs->ns_next_generation));
711
712 err = nilfs_store_disk_layout(nilfs, sbp);
713 if (err)
714 goto failed_sbh;
715
716 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
717
718 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
719
720 err = nilfs_store_log_cursor(nilfs, sbp);
721 if (err)
722 goto failed_sbh;
723
724 set_nilfs_init(nilfs);
725 err = 0;
726 out:
727 up_write(&nilfs->ns_sem);
728 return err;
729
730 failed_sbh:
731 nilfs_release_super_block(nilfs);
732 goto out;
733}
734
735int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
736 size_t nsegs)
737{
738 sector_t seg_start, seg_end;
739 sector_t start = 0, nblocks = 0;
740 unsigned int sects_per_block;
741 __u64 *sn;
742 int ret = 0;
743
744 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
745 bdev_logical_block_size(nilfs->ns_bdev);
746 for (sn = segnump; sn < segnump + nsegs; sn++) {
747 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
748
749 if (!nblocks) {
750 start = seg_start;
751 nblocks = seg_end - seg_start + 1;
752 } else if (start + nblocks == seg_start) {
753 nblocks += seg_end - seg_start + 1;
754 } else {
755 ret = blkdev_issue_discard(nilfs->ns_bdev,
756 start * sects_per_block,
757 nblocks * sects_per_block,
758 GFP_NOFS, 0);
759 if (ret < 0)
760 return ret;
761 nblocks = 0;
762 }
763 }
764 if (nblocks)
765 ret = blkdev_issue_discard(nilfs->ns_bdev,
766 start * sects_per_block,
767 nblocks * sects_per_block,
768 GFP_NOFS, 0);
769 return ret;
770}
771
772int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
773{
774 unsigned long ncleansegs;
775
776 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
777 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
778 return 0;
779}
780
781int nilfs_near_disk_full(struct the_nilfs *nilfs)
782{
783 unsigned long ncleansegs, nincsegs;
784
785 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
786 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
787 nilfs->ns_blocks_per_segment + 1;
788
789 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
790}
791
792struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
793{
794 struct rb_node *n;
795 struct nilfs_root *root;
796
797 spin_lock(&nilfs->ns_cptree_lock);
798 n = nilfs->ns_cptree.rb_node;
799 while (n) {
800 root = rb_entry(n, struct nilfs_root, rb_node);
801
802 if (cno < root->cno) {
803 n = n->rb_left;
804 } else if (cno > root->cno) {
805 n = n->rb_right;
806 } else {
807 refcount_inc(&root->count);
808 spin_unlock(&nilfs->ns_cptree_lock);
809 return root;
810 }
811 }
812 spin_unlock(&nilfs->ns_cptree_lock);
813
814 return NULL;
815}
816
817struct nilfs_root *
818nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
819{
820 struct rb_node **p, *parent;
821 struct nilfs_root *root, *new;
822 int err;
823
824 root = nilfs_lookup_root(nilfs, cno);
825 if (root)
826 return root;
827
828 new = kzalloc(sizeof(*root), GFP_KERNEL);
829 if (!new)
830 return NULL;
831
832 spin_lock(&nilfs->ns_cptree_lock);
833
834 p = &nilfs->ns_cptree.rb_node;
835 parent = NULL;
836
837 while (*p) {
838 parent = *p;
839 root = rb_entry(parent, struct nilfs_root, rb_node);
840
841 if (cno < root->cno) {
842 p = &(*p)->rb_left;
843 } else if (cno > root->cno) {
844 p = &(*p)->rb_right;
845 } else {
846 refcount_inc(&root->count);
847 spin_unlock(&nilfs->ns_cptree_lock);
848 kfree(new);
849 return root;
850 }
851 }
852
853 new->cno = cno;
854 new->ifile = NULL;
855 new->nilfs = nilfs;
856 refcount_set(&new->count, 1);
857 atomic64_set(&new->inodes_count, 0);
858 atomic64_set(&new->blocks_count, 0);
859
860 rb_link_node(&new->rb_node, parent, p);
861 rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
862
863 spin_unlock(&nilfs->ns_cptree_lock);
864
865 err = nilfs_sysfs_create_snapshot_group(new);
866 if (err) {
867 kfree(new);
868 new = NULL;
869 }
870
871 return new;
872}
873
874void nilfs_put_root(struct nilfs_root *root)
875{
876 struct the_nilfs *nilfs = root->nilfs;
877
878 if (refcount_dec_and_lock(&root->count, &nilfs->ns_cptree_lock)) {
879 rb_erase(&root->rb_node, &nilfs->ns_cptree);
880 spin_unlock(&nilfs->ns_cptree_lock);
881
882 nilfs_sysfs_delete_snapshot_group(root);
883 iput(root->ifile);
884
885 kfree(root);
886 }
887}