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