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