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