nilfs2: avoid readahead on metadata file for create mode
[safe/jmp/linux-2.6] / 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 void nilfs_set_last_segment(struct the_nilfs *nilfs,
42                             sector_t start_blocknr, u64 seq, __u64 cno)
43 {
44         spin_lock(&nilfs->ns_last_segment_lock);
45         nilfs->ns_last_pseg = start_blocknr;
46         nilfs->ns_last_seq = seq;
47         nilfs->ns_last_cno = cno;
48         spin_unlock(&nilfs->ns_last_segment_lock);
49 }
50
51 /**
52  * alloc_nilfs - allocate the_nilfs structure
53  * @bdev: block device to which the_nilfs is related
54  *
55  * alloc_nilfs() allocates memory for the_nilfs and
56  * initializes its reference count and locks.
57  *
58  * Return Value: On success, pointer to the_nilfs is returned.
59  * On error, NULL is returned.
60  */
61 static struct the_nilfs *alloc_nilfs(struct block_device *bdev)
62 {
63         struct the_nilfs *nilfs;
64
65         nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
66         if (!nilfs)
67                 return NULL;
68
69         nilfs->ns_bdev = bdev;
70         atomic_set(&nilfs->ns_count, 1);
71         atomic_set(&nilfs->ns_ndirtyblks, 0);
72         init_rwsem(&nilfs->ns_sem);
73         init_rwsem(&nilfs->ns_super_sem);
74         mutex_init(&nilfs->ns_mount_mutex);
75         init_rwsem(&nilfs->ns_writer_sem);
76         INIT_LIST_HEAD(&nilfs->ns_list);
77         INIT_LIST_HEAD(&nilfs->ns_supers);
78         spin_lock_init(&nilfs->ns_last_segment_lock);
79         nilfs->ns_gc_inodes_h = NULL;
80         init_rwsem(&nilfs->ns_segctor_sem);
81
82         return nilfs;
83 }
84
85 /**
86  * find_or_create_nilfs - find or create nilfs object
87  * @bdev: block device to which the_nilfs is related
88  *
89  * find_nilfs() looks up an existent nilfs object created on the
90  * device and gets the reference count of the object.  If no nilfs object
91  * is found on the device, a new nilfs object is allocated.
92  *
93  * Return Value: On success, pointer to the nilfs object is returned.
94  * On error, NULL is returned.
95  */
96 struct the_nilfs *find_or_create_nilfs(struct block_device *bdev)
97 {
98         struct the_nilfs *nilfs, *new = NULL;
99
100  retry:
101         spin_lock(&nilfs_lock);
102         list_for_each_entry(nilfs, &nilfs_objects, ns_list) {
103                 if (nilfs->ns_bdev == bdev) {
104                         get_nilfs(nilfs);
105                         spin_unlock(&nilfs_lock);
106                         if (new)
107                                 put_nilfs(new);
108                         return nilfs; /* existing object */
109                 }
110         }
111         if (new) {
112                 list_add_tail(&new->ns_list, &nilfs_objects);
113                 spin_unlock(&nilfs_lock);
114                 return new; /* new object */
115         }
116         spin_unlock(&nilfs_lock);
117
118         new = alloc_nilfs(bdev);
119         if (new)
120                 goto retry;
121         return NULL; /* insufficient memory */
122 }
123
124 /**
125  * put_nilfs - release a reference to the_nilfs
126  * @nilfs: the_nilfs structure to be released
127  *
128  * put_nilfs() decrements a reference counter of the_nilfs.
129  * If the reference count reaches zero, the_nilfs is freed.
130  */
131 void put_nilfs(struct the_nilfs *nilfs)
132 {
133         spin_lock(&nilfs_lock);
134         if (!atomic_dec_and_test(&nilfs->ns_count)) {
135                 spin_unlock(&nilfs_lock);
136                 return;
137         }
138         list_del_init(&nilfs->ns_list);
139         spin_unlock(&nilfs_lock);
140
141         /*
142          * Increment of ns_count never occurs below because the caller
143          * of get_nilfs() holds at least one reference to the_nilfs.
144          * Thus its exclusion control is not required here.
145          */
146
147         might_sleep();
148         if (nilfs_loaded(nilfs)) {
149                 nilfs_mdt_destroy(nilfs->ns_sufile);
150                 nilfs_mdt_destroy(nilfs->ns_cpfile);
151                 nilfs_mdt_destroy(nilfs->ns_dat);
152                 nilfs_mdt_destroy(nilfs->ns_gc_dat);
153         }
154         if (nilfs_init(nilfs)) {
155                 nilfs_destroy_gccache(nilfs);
156                 brelse(nilfs->ns_sbh[0]);
157                 brelse(nilfs->ns_sbh[1]);
158         }
159         kfree(nilfs);
160 }
161
162 static int nilfs_load_super_root(struct the_nilfs *nilfs,
163                                  struct nilfs_sb_info *sbi, sector_t sr_block)
164 {
165         struct buffer_head *bh_sr;
166         struct nilfs_super_root *raw_sr;
167         struct nilfs_super_block **sbp = nilfs->ns_sbp;
168         unsigned dat_entry_size, segment_usage_size, checkpoint_size;
169         unsigned inode_size;
170         int err;
171
172         err = nilfs_read_super_root_block(sbi->s_super, sr_block, &bh_sr, 1);
173         if (unlikely(err))
174                 return err;
175
176         down_read(&nilfs->ns_sem);
177         dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
178         checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
179         segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
180         up_read(&nilfs->ns_sem);
181
182         inode_size = nilfs->ns_inode_size;
183
184         err = -ENOMEM;
185         nilfs->ns_dat = nilfs_dat_new(nilfs, dat_entry_size);
186         if (unlikely(!nilfs->ns_dat))
187                 goto failed;
188
189         nilfs->ns_gc_dat = nilfs_dat_new(nilfs, dat_entry_size);
190         if (unlikely(!nilfs->ns_gc_dat))
191                 goto failed_dat;
192
193         nilfs->ns_cpfile = nilfs_cpfile_new(nilfs, checkpoint_size);
194         if (unlikely(!nilfs->ns_cpfile))
195                 goto failed_gc_dat;
196
197         nilfs->ns_sufile = nilfs_sufile_new(nilfs, segment_usage_size);
198         if (unlikely(!nilfs->ns_sufile))
199                 goto failed_cpfile;
200
201         nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat);
202
203         err = nilfs_dat_read(nilfs->ns_dat, (void *)bh_sr->b_data +
204                              NILFS_SR_DAT_OFFSET(inode_size));
205         if (unlikely(err))
206                 goto failed_sufile;
207
208         err = nilfs_cpfile_read(nilfs->ns_cpfile, (void *)bh_sr->b_data +
209                                 NILFS_SR_CPFILE_OFFSET(inode_size));
210         if (unlikely(err))
211                 goto failed_sufile;
212
213         err = nilfs_sufile_read(nilfs->ns_sufile, (void *)bh_sr->b_data +
214                                 NILFS_SR_SUFILE_OFFSET(inode_size));
215         if (unlikely(err))
216                 goto failed_sufile;
217
218         raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
219         nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
220
221  failed:
222         brelse(bh_sr);
223         return err;
224
225  failed_sufile:
226         nilfs_mdt_destroy(nilfs->ns_sufile);
227
228  failed_cpfile:
229         nilfs_mdt_destroy(nilfs->ns_cpfile);
230
231  failed_gc_dat:
232         nilfs_mdt_destroy(nilfs->ns_gc_dat);
233
234  failed_dat:
235         nilfs_mdt_destroy(nilfs->ns_dat);
236         goto failed;
237 }
238
239 static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
240 {
241         memset(ri, 0, sizeof(*ri));
242         INIT_LIST_HEAD(&ri->ri_used_segments);
243 }
244
245 static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
246 {
247         nilfs_dispose_segment_list(&ri->ri_used_segments);
248 }
249
250 /**
251  * load_nilfs - load and recover the nilfs
252  * @nilfs: the_nilfs structure to be released
253  * @sbi: nilfs_sb_info used to recover past segment
254  *
255  * load_nilfs() searches and load the latest super root,
256  * attaches the last segment, and does recovery if needed.
257  * The caller must call this exclusively for simultaneous mounts.
258  */
259 int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
260 {
261         struct nilfs_recovery_info ri;
262         unsigned int s_flags = sbi->s_super->s_flags;
263         int really_read_only = bdev_read_only(nilfs->ns_bdev);
264         unsigned valid_fs;
265         int err = 0;
266
267         nilfs_init_recovery_info(&ri);
268
269         down_write(&nilfs->ns_sem);
270         valid_fs = (nilfs->ns_mount_state & NILFS_VALID_FS);
271         up_write(&nilfs->ns_sem);
272
273         if (!valid_fs && (s_flags & MS_RDONLY)) {
274                 printk(KERN_INFO "NILFS: INFO: recovery "
275                        "required for readonly filesystem.\n");
276                 if (really_read_only) {
277                         printk(KERN_ERR "NILFS: write access "
278                                "unavailable, cannot proceed.\n");
279                         err = -EROFS;
280                         goto failed;
281                 }
282                 printk(KERN_INFO "NILFS: write access will "
283                        "be enabled during recovery.\n");
284                 sbi->s_super->s_flags &= ~MS_RDONLY;
285         }
286
287         err = nilfs_search_super_root(nilfs, sbi, &ri);
288         if (unlikely(err)) {
289                 printk(KERN_ERR "NILFS: error searching super root.\n");
290                 goto failed;
291         }
292
293         err = nilfs_load_super_root(nilfs, sbi, ri.ri_super_root);
294         if (unlikely(err)) {
295                 printk(KERN_ERR "NILFS: error loading super root.\n");
296                 goto failed;
297         }
298
299         if (!valid_fs) {
300                 err = nilfs_recover_logical_segments(nilfs, sbi, &ri);
301                 if (unlikely(err)) {
302                         nilfs_mdt_destroy(nilfs->ns_cpfile);
303                         nilfs_mdt_destroy(nilfs->ns_sufile);
304                         nilfs_mdt_destroy(nilfs->ns_dat);
305                         goto failed;
306                 }
307                 if (ri.ri_need_recovery == NILFS_RECOVERY_SR_UPDATED)
308                         sbi->s_super->s_dirt = 1;
309         }
310
311         set_nilfs_loaded(nilfs);
312
313  failed:
314         nilfs_clear_recovery_info(&ri);
315         sbi->s_super->s_flags = s_flags;
316         return err;
317 }
318
319 static unsigned long long nilfs_max_size(unsigned int blkbits)
320 {
321         unsigned int max_bits;
322         unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
323
324         max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
325         if (max_bits < 64)
326                 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
327         return res;
328 }
329
330 static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
331                                    struct nilfs_super_block *sbp)
332 {
333         if (le32_to_cpu(sbp->s_rev_level) != NILFS_CURRENT_REV) {
334                 printk(KERN_ERR "NILFS: revision mismatch "
335                        "(superblock rev.=%d.%d, current rev.=%d.%d). "
336                        "Please check the version of mkfs.nilfs.\n",
337                        le32_to_cpu(sbp->s_rev_level),
338                        le16_to_cpu(sbp->s_minor_rev_level),
339                        NILFS_CURRENT_REV, NILFS_MINOR_REV);
340                 return -EINVAL;
341         }
342         nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
343         if (nilfs->ns_sbsize > BLOCK_SIZE)
344                 return -EINVAL;
345
346         nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
347         nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
348
349         nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
350         if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
351                 printk(KERN_ERR "NILFS: too short segment. \n");
352                 return -EINVAL;
353         }
354
355         nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
356         nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
357         nilfs->ns_r_segments_percentage =
358                 le32_to_cpu(sbp->s_r_segments_percentage);
359         nilfs->ns_nrsvsegs =
360                 max_t(unsigned long, NILFS_MIN_NRSVSEGS,
361                       DIV_ROUND_UP(nilfs->ns_nsegments *
362                                    nilfs->ns_r_segments_percentage, 100));
363         nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
364         return 0;
365 }
366
367 static int nilfs_valid_sb(struct nilfs_super_block *sbp)
368 {
369         static unsigned char sum[4];
370         const int sumoff = offsetof(struct nilfs_super_block, s_sum);
371         size_t bytes;
372         u32 crc;
373
374         if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
375                 return 0;
376         bytes = le16_to_cpu(sbp->s_bytes);
377         if (bytes > BLOCK_SIZE)
378                 return 0;
379         crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
380                        sumoff);
381         crc = crc32_le(crc, sum, 4);
382         crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
383                        bytes - sumoff - 4);
384         return crc == le32_to_cpu(sbp->s_sum);
385 }
386
387 static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
388 {
389         return offset < ((le64_to_cpu(sbp->s_nsegments) *
390                           le32_to_cpu(sbp->s_blocks_per_segment)) <<
391                          (le32_to_cpu(sbp->s_log_block_size) + 10));
392 }
393
394 static void nilfs_release_super_block(struct the_nilfs *nilfs)
395 {
396         int i;
397
398         for (i = 0; i < 2; i++) {
399                 if (nilfs->ns_sbp[i]) {
400                         brelse(nilfs->ns_sbh[i]);
401                         nilfs->ns_sbh[i] = NULL;
402                         nilfs->ns_sbp[i] = NULL;
403                 }
404         }
405 }
406
407 void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
408 {
409         brelse(nilfs->ns_sbh[0]);
410         nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
411         nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
412         nilfs->ns_sbh[1] = NULL;
413         nilfs->ns_sbp[1] = NULL;
414 }
415
416 void nilfs_swap_super_block(struct the_nilfs *nilfs)
417 {
418         struct buffer_head *tsbh = nilfs->ns_sbh[0];
419         struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
420
421         nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
422         nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
423         nilfs->ns_sbh[1] = tsbh;
424         nilfs->ns_sbp[1] = tsbp;
425 }
426
427 static int nilfs_load_super_block(struct the_nilfs *nilfs,
428                                   struct super_block *sb, int blocksize,
429                                   struct nilfs_super_block **sbpp)
430 {
431         struct nilfs_super_block **sbp = nilfs->ns_sbp;
432         struct buffer_head **sbh = nilfs->ns_sbh;
433         u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
434         int valid[2], swp = 0;
435
436         sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
437                                         &sbh[0]);
438         sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
439
440         if (!sbp[0]) {
441                 if (!sbp[1]) {
442                         printk(KERN_ERR "NILFS: unable to read superblock\n");
443                         return -EIO;
444                 }
445                 printk(KERN_WARNING
446                        "NILFS warning: unable to read primary superblock\n");
447         } else if (!sbp[1])
448                 printk(KERN_WARNING
449                        "NILFS warning: unable to read secondary superblock\n");
450
451         valid[0] = nilfs_valid_sb(sbp[0]);
452         valid[1] = nilfs_valid_sb(sbp[1]);
453         swp = valid[1] &&
454                 (!valid[0] ||
455                  le64_to_cpu(sbp[1]->s_wtime) > le64_to_cpu(sbp[0]->s_wtime));
456
457         if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
458                 brelse(sbh[1]);
459                 sbh[1] = NULL;
460                 sbp[1] = NULL;
461                 swp = 0;
462         }
463         if (!valid[swp]) {
464                 nilfs_release_super_block(nilfs);
465                 printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
466                        sb->s_id);
467                 return -EINVAL;
468         }
469
470         if (swp) {
471                 printk(KERN_WARNING "NILFS warning: broken superblock. "
472                        "using spare superblock.\n");
473                 nilfs_swap_super_block(nilfs);
474         }
475
476         nilfs->ns_sbwtime[0] = le64_to_cpu(sbp[0]->s_wtime);
477         nilfs->ns_sbwtime[1] = valid[!swp] ? le64_to_cpu(sbp[1]->s_wtime) : 0;
478         nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
479         *sbpp = sbp[0];
480         return 0;
481 }
482
483 /**
484  * init_nilfs - initialize a NILFS instance.
485  * @nilfs: the_nilfs structure
486  * @sbi: nilfs_sb_info
487  * @sb: super block
488  * @data: mount options
489  *
490  * init_nilfs() performs common initialization per block device (e.g.
491  * reading the super block, getting disk layout information, initializing
492  * shared fields in the_nilfs). It takes on some portion of the jobs
493  * typically done by a fill_super() routine. This division arises from
494  * the nature that multiple NILFS instances may be simultaneously
495  * mounted on a device.
496  * For multiple mounts on the same device, only the first mount
497  * invokes these tasks.
498  *
499  * Return Value: On success, 0 is returned. On error, a negative error
500  * code is returned.
501  */
502 int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
503 {
504         struct super_block *sb = sbi->s_super;
505         struct nilfs_super_block *sbp;
506         struct backing_dev_info *bdi;
507         int blocksize;
508         int err;
509
510         down_write(&nilfs->ns_sem);
511         if (nilfs_init(nilfs)) {
512                 /* Load values from existing the_nilfs */
513                 sbp = nilfs->ns_sbp[0];
514                 err = nilfs_store_magic_and_option(sb, sbp, data);
515                 if (err)
516                         goto out;
517
518                 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
519                 if (sb->s_blocksize != blocksize &&
520                     !sb_set_blocksize(sb, blocksize)) {
521                         printk(KERN_ERR "NILFS: blocksize %d unfit to device\n",
522                                blocksize);
523                         err = -EINVAL;
524                 }
525                 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
526                 goto out;
527         }
528
529         blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
530         if (!blocksize) {
531                 printk(KERN_ERR "NILFS: unable to set blocksize\n");
532                 err = -EINVAL;
533                 goto out;
534         }
535         err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
536         if (err)
537                 goto out;
538
539         err = nilfs_store_magic_and_option(sb, sbp, data);
540         if (err)
541                 goto failed_sbh;
542
543         blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
544         if (sb->s_blocksize != blocksize) {
545                 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
546
547                 if (blocksize < hw_blocksize) {
548                         printk(KERN_ERR
549                                "NILFS: blocksize %d too small for device "
550                                "(sector-size = %d).\n",
551                                blocksize, hw_blocksize);
552                         err = -EINVAL;
553                         goto failed_sbh;
554                 }
555                 nilfs_release_super_block(nilfs);
556                 sb_set_blocksize(sb, blocksize);
557
558                 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
559                 if (err)
560                         goto out;
561                         /* not failed_sbh; sbh is released automatically
562                            when reloading fails. */
563         }
564         nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
565
566         err = nilfs_store_disk_layout(nilfs, sbp);
567         if (err)
568                 goto failed_sbh;
569
570         sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
571
572         nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
573
574         bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
575         nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
576
577         /* Finding last segment */
578         nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
579         nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
580         nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
581
582         nilfs->ns_seg_seq = nilfs->ns_last_seq;
583         nilfs->ns_segnum =
584                 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
585         nilfs->ns_cno = nilfs->ns_last_cno + 1;
586         if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
587                 printk(KERN_ERR "NILFS invalid last segment number.\n");
588                 err = -EINVAL;
589                 goto failed_sbh;
590         }
591         /* Dummy values  */
592         nilfs->ns_free_segments_count =
593                 nilfs->ns_nsegments - (nilfs->ns_segnum + 1);
594
595         /* Initialize gcinode cache */
596         err = nilfs_init_gccache(nilfs);
597         if (err)
598                 goto failed_sbh;
599
600         set_nilfs_init(nilfs);
601         err = 0;
602  out:
603         up_write(&nilfs->ns_sem);
604         return err;
605
606  failed_sbh:
607         nilfs_release_super_block(nilfs);
608         goto out;
609 }
610
611 int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
612 {
613         struct inode *dat = nilfs_dat_inode(nilfs);
614         unsigned long ncleansegs;
615
616         down_read(&NILFS_MDT(dat)->mi_sem);     /* XXX */
617         ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
618         up_read(&NILFS_MDT(dat)->mi_sem);       /* XXX */
619         *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
620         return 0;
621 }
622
623 int nilfs_near_disk_full(struct the_nilfs *nilfs)
624 {
625         unsigned long ncleansegs, nincsegs;
626
627         ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
628         nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
629                 nilfs->ns_blocks_per_segment + 1;
630
631         return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
632 }
633
634 /**
635  * nilfs_find_sbinfo - find existing nilfs_sb_info structure
636  * @nilfs: nilfs object
637  * @rw_mount: mount type (non-zero value for read/write mount)
638  * @cno: checkpoint number (zero for read-only mount)
639  *
640  * nilfs_find_sbinfo() returns the nilfs_sb_info structure which
641  * @rw_mount and @cno (in case of snapshots) matched.  If no instance
642  * was found, NULL is returned.  Although the super block instance can
643  * be unmounted after this function returns, the nilfs_sb_info struct
644  * is kept on memory until nilfs_put_sbinfo() is called.
645  */
646 struct nilfs_sb_info *nilfs_find_sbinfo(struct the_nilfs *nilfs,
647                                         int rw_mount, __u64 cno)
648 {
649         struct nilfs_sb_info *sbi;
650
651         down_read(&nilfs->ns_super_sem);
652         /*
653          * The SNAPSHOT flag and sb->s_flags are supposed to be
654          * protected with nilfs->ns_super_sem.
655          */
656         sbi = nilfs->ns_current;
657         if (rw_mount) {
658                 if (sbi && !(sbi->s_super->s_flags & MS_RDONLY))
659                         goto found; /* read/write mount */
660                 else
661                         goto out;
662         } else if (cno == 0) {
663                 if (sbi && (sbi->s_super->s_flags & MS_RDONLY))
664                         goto found; /* read-only mount */
665                 else
666                         goto out;
667         }
668
669         list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
670                 if (nilfs_test_opt(sbi, SNAPSHOT) &&
671                     sbi->s_snapshot_cno == cno)
672                         goto found; /* snapshot mount */
673         }
674  out:
675         up_read(&nilfs->ns_super_sem);
676         return NULL;
677
678  found:
679         atomic_inc(&sbi->s_count);
680         up_read(&nilfs->ns_super_sem);
681         return sbi;
682 }
683
684 int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
685                                 int snapshot_mount)
686 {
687         struct nilfs_sb_info *sbi;
688         int ret = 0;
689
690         down_read(&nilfs->ns_super_sem);
691         if (cno == 0 || cno > nilfs->ns_cno)
692                 goto out_unlock;
693
694         list_for_each_entry(sbi, &nilfs->ns_supers, s_list) {
695                 if (sbi->s_snapshot_cno == cno &&
696                     (!snapshot_mount || nilfs_test_opt(sbi, SNAPSHOT))) {
697                                         /* exclude read-only mounts */
698                         ret++;
699                         break;
700                 }
701         }
702         /* for protecting recent checkpoints */
703         if (cno >= nilfs_last_cno(nilfs))
704                 ret++;
705
706  out_unlock:
707         up_read(&nilfs->ns_super_sem);
708         return ret;
709 }