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