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