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