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