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