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