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