blob: 6eeb4f072f83774b8beabeaa07245c191c2f9d9f [file] [log] [blame]
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -07001/*
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>
Ryusuke Konishie339ad32009-04-06 19:01:59 -070028#include <linux/crc32.h>
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070029#include "nilfs.h"
30#include "segment.h"
31#include "alloc.h"
32#include "cpfile.h"
33#include "sufile.h"
34#include "dat.h"
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070035#include "segbuf.h"
36
Ryusuke Konishi33c8e572009-06-08 01:39:29 +090037
Ryusuke Konishi6c1251602010-06-28 19:15:26 +090038static int nilfs_valid_sb(struct nilfs_super_block *sbp);
39
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070040void nilfs_set_last_segment(struct the_nilfs *nilfs,
41 sector_t start_blocknr, u64 seq, __u64 cno)
42{
43 spin_lock(&nilfs->ns_last_segment_lock);
44 nilfs->ns_last_pseg = start_blocknr;
45 nilfs->ns_last_seq = seq;
46 nilfs->ns_last_cno = cno;
Ryusuke Konishi32502042010-06-29 14:42:13 +090047
48 if (!nilfs_sb_dirty(nilfs)) {
49 if (nilfs->ns_prev_seq == nilfs->ns_last_seq)
50 goto stay_cursor;
51
52 set_nilfs_sb_dirty(nilfs);
53 }
54 nilfs->ns_prev_seq = nilfs->ns_last_seq;
55
56 stay_cursor:
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070057 spin_unlock(&nilfs->ns_last_segment_lock);
58}
59
60/**
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +090061 * alloc_nilfs - allocate a nilfs object
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070062 * @bdev: block device to which the_nilfs is related
63 *
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070064 * Return Value: On success, pointer to the_nilfs is returned.
65 * On error, NULL is returned.
66 */
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +090067struct the_nilfs *alloc_nilfs(struct block_device *bdev)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070068{
69 struct the_nilfs *nilfs;
70
71 nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
72 if (!nilfs)
73 return NULL;
74
75 nilfs->ns_bdev = bdev;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070076 atomic_set(&nilfs->ns_ndirtyblks, 0);
77 init_rwsem(&nilfs->ns_sem);
Ryusuke Konishi027d6402009-08-02 22:45:33 +090078 init_rwsem(&nilfs->ns_writer_sem);
Ryusuke Konishi263d90c2010-08-20 19:06:11 +090079 INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070080 spin_lock_init(&nilfs->ns_last_segment_lock);
Ryusuke Konishiba65ae42010-08-14 12:59:15 +090081 nilfs->ns_cptree = RB_ROOT;
82 spin_lock_init(&nilfs->ns_cptree_lock);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070083 init_rwsem(&nilfs->ns_segctor_sem);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070084
85 return nilfs;
86}
87
88/**
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +090089 * destroy_nilfs - destroy nilfs object
90 * @nilfs: nilfs object to be released
Ryusuke Konishi33c8e572009-06-08 01:39:29 +090091 */
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +090092void destroy_nilfs(struct the_nilfs *nilfs)
Ryusuke Konishi33c8e572009-06-08 01:39:29 +090093{
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070094 might_sleep();
95 if (nilfs_loaded(nilfs)) {
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070096 nilfs_mdt_destroy(nilfs->ns_sufile);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070097 nilfs_mdt_destroy(nilfs->ns_cpfile);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070098 nilfs_mdt_destroy(nilfs->ns_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070099 nilfs_mdt_destroy(nilfs->ns_gc_dat);
100 }
101 if (nilfs_init(nilfs)) {
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700102 brelse(nilfs->ns_sbh[0]);
103 brelse(nilfs->ns_sbh[1]);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700104 }
105 kfree(nilfs);
106}
107
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900108static int nilfs_load_super_root(struct the_nilfs *nilfs, sector_t sr_block)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700109{
110 struct buffer_head *bh_sr;
111 struct nilfs_super_root *raw_sr;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700112 struct nilfs_super_block **sbp = nilfs->ns_sbp;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700113 unsigned dat_entry_size, segment_usage_size, checkpoint_size;
114 unsigned inode_size;
115 int err;
116
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900117 err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700118 if (unlikely(err))
119 return err;
120
121 down_read(&nilfs->ns_sem);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700122 dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
123 checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
124 segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700125 up_read(&nilfs->ns_sem);
126
127 inode_size = nilfs->ns_inode_size;
128
129 err = -ENOMEM;
Ryusuke Konishi79739562009-11-12 23:56:43 +0900130 nilfs->ns_dat = nilfs_dat_new(nilfs, dat_entry_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700131 if (unlikely(!nilfs->ns_dat))
132 goto failed;
133
Ryusuke Konishi79739562009-11-12 23:56:43 +0900134 nilfs->ns_gc_dat = nilfs_dat_new(nilfs, dat_entry_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700135 if (unlikely(!nilfs->ns_gc_dat))
136 goto failed_dat;
137
Ryusuke Konishi79739562009-11-12 23:56:43 +0900138 nilfs->ns_cpfile = nilfs_cpfile_new(nilfs, checkpoint_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700139 if (unlikely(!nilfs->ns_cpfile))
140 goto failed_gc_dat;
141
Ryusuke Konishi79739562009-11-12 23:56:43 +0900142 nilfs->ns_sufile = nilfs_sufile_new(nilfs, segment_usage_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700143 if (unlikely(!nilfs->ns_sufile))
144 goto failed_cpfile;
145
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700146 nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700147
Ryusuke Konishi8707df32009-11-13 01:36:56 +0900148 err = nilfs_dat_read(nilfs->ns_dat, (void *)bh_sr->b_data +
149 NILFS_SR_DAT_OFFSET(inode_size));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700150 if (unlikely(err))
151 goto failed_sufile;
152
Ryusuke Konishi8707df32009-11-13 01:36:56 +0900153 err = nilfs_cpfile_read(nilfs->ns_cpfile, (void *)bh_sr->b_data +
154 NILFS_SR_CPFILE_OFFSET(inode_size));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700155 if (unlikely(err))
156 goto failed_sufile;
157
Ryusuke Konishi8707df32009-11-13 01:36:56 +0900158 err = nilfs_sufile_read(nilfs->ns_sufile, (void *)bh_sr->b_data +
159 NILFS_SR_SUFILE_OFFSET(inode_size));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700160 if (unlikely(err))
161 goto failed_sufile;
162
163 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
164 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
165
166 failed:
167 brelse(bh_sr);
168 return err;
169
170 failed_sufile:
171 nilfs_mdt_destroy(nilfs->ns_sufile);
172
173 failed_cpfile:
174 nilfs_mdt_destroy(nilfs->ns_cpfile);
175
176 failed_gc_dat:
177 nilfs_mdt_destroy(nilfs->ns_gc_dat);
178
179 failed_dat:
180 nilfs_mdt_destroy(nilfs->ns_dat);
181 goto failed;
182}
183
184static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
185{
186 memset(ri, 0, sizeof(*ri));
187 INIT_LIST_HEAD(&ri->ri_used_segments);
188}
189
190static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
191{
192 nilfs_dispose_segment_list(&ri->ri_used_segments);
193}
194
195/**
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900196 * nilfs_store_log_cursor - load log cursor from a super block
197 * @nilfs: nilfs object
198 * @sbp: buffer storing super block to be read
199 *
200 * nilfs_store_log_cursor() reads the last position of the log
201 * containing a super root from a given super block, and initializes
202 * relevant information on the nilfs object preparatory for log
203 * scanning and recovery.
204 */
205static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
206 struct nilfs_super_block *sbp)
207{
208 int ret = 0;
209
210 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
211 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
212 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
213
Ryusuke Konishi32502042010-06-29 14:42:13 +0900214 nilfs->ns_prev_seq = nilfs->ns_last_seq;
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900215 nilfs->ns_seg_seq = nilfs->ns_last_seq;
216 nilfs->ns_segnum =
217 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
218 nilfs->ns_cno = nilfs->ns_last_cno + 1;
219 if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
220 printk(KERN_ERR "NILFS invalid last segment number.\n");
221 ret = -EINVAL;
222 }
223 return ret;
224}
225
226/**
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700227 * load_nilfs - load and recover the nilfs
228 * @nilfs: the_nilfs structure to be released
229 * @sbi: nilfs_sb_info used to recover past segment
230 *
231 * load_nilfs() searches and load the latest super root,
232 * attaches the last segment, and does recovery if needed.
233 * The caller must call this exclusively for simultaneous mounts.
234 */
235int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
236{
237 struct nilfs_recovery_info ri;
238 unsigned int s_flags = sbi->s_super->s_flags;
239 int really_read_only = bdev_read_only(nilfs->ns_bdev);
Ryusuke Konishia057d2c2009-11-19 19:58:46 +0900240 int valid_fs = nilfs_valid_fs(nilfs);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900241 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700242
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900243 if (!valid_fs) {
244 printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
245 if (s_flags & MS_RDONLY) {
246 printk(KERN_INFO "NILFS: INFO: recovery "
247 "required for readonly filesystem.\n");
248 printk(KERN_INFO "NILFS: write access will "
249 "be enabled during recovery.\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700250 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700251 }
252
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900253 nilfs_init_recovery_info(&ri);
254
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900255 err = nilfs_search_super_root(nilfs, &ri);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700256 if (unlikely(err)) {
Ryusuke Konishi6c1251602010-06-28 19:15:26 +0900257 struct nilfs_super_block **sbp = nilfs->ns_sbp;
258 int blocksize;
259
260 if (err != -EINVAL)
261 goto scan_error;
262
263 if (!nilfs_valid_sb(sbp[1])) {
264 printk(KERN_WARNING
265 "NILFS warning: unable to fall back to spare"
266 "super block\n");
267 goto scan_error;
268 }
269 printk(KERN_INFO
270 "NILFS: try rollback from an earlier position\n");
271
272 /*
273 * restore super block with its spare and reconfigure
274 * relevant states of the nilfs object.
275 */
276 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
277 nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
278 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
279
280 /* verify consistency between two super blocks */
281 blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
282 if (blocksize != nilfs->ns_blocksize) {
283 printk(KERN_WARNING
284 "NILFS warning: blocksize differs between "
285 "two super blocks (%d != %d)\n",
286 blocksize, nilfs->ns_blocksize);
287 goto scan_error;
288 }
289
290 err = nilfs_store_log_cursor(nilfs, sbp[0]);
291 if (err)
292 goto scan_error;
293
294 /* drop clean flag to allow roll-forward and recovery */
295 nilfs->ns_mount_state &= ~NILFS_VALID_FS;
296 valid_fs = 0;
297
298 err = nilfs_search_super_root(nilfs, &ri);
299 if (err)
300 goto scan_error;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700301 }
302
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900303 err = nilfs_load_super_root(nilfs, ri.ri_super_root);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700304 if (unlikely(err)) {
305 printk(KERN_ERR "NILFS: error loading super root.\n");
306 goto failed;
307 }
308
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900309 if (valid_fs)
310 goto skip_recovery;
311
312 if (s_flags & MS_RDONLY) {
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900313 __u64 features;
314
Ryusuke Konishi02345762009-11-20 03:28:01 +0900315 if (nilfs_test_opt(sbi, NORECOVERY)) {
316 printk(KERN_INFO "NILFS: norecovery option specified. "
317 "skipping roll-forward recovery\n");
318 goto skip_recovery;
319 }
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900320 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
321 ~NILFS_FEATURE_COMPAT_RO_SUPP;
322 if (features) {
323 printk(KERN_ERR "NILFS: couldn't proceed with "
324 "recovery because of unsupported optional "
325 "features (%llx)\n",
326 (unsigned long long)features);
327 err = -EROFS;
328 goto failed_unload;
329 }
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900330 if (really_read_only) {
331 printk(KERN_ERR "NILFS: write access "
332 "unavailable, cannot proceed.\n");
333 err = -EROFS;
334 goto failed_unload;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700335 }
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900336 sbi->s_super->s_flags &= ~MS_RDONLY;
Ryusuke Konishi02345762009-11-20 03:28:01 +0900337 } else if (nilfs_test_opt(sbi, NORECOVERY)) {
338 printk(KERN_ERR "NILFS: recovery cancelled because norecovery "
339 "option was specified for a read/write mount\n");
340 err = -EINVAL;
341 goto failed_unload;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700342 }
343
Ryusuke Konishiaee5ce22010-05-23 12:21:57 +0900344 err = nilfs_salvage_orphan_logs(nilfs, sbi, &ri);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900345 if (err)
346 goto failed_unload;
347
348 down_write(&nilfs->ns_sem);
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900349 nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
350 err = nilfs_cleanup_super(sbi);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900351 up_write(&nilfs->ns_sem);
352
353 if (err) {
354 printk(KERN_ERR "NILFS: failed to update super block. "
355 "recovery unfinished.\n");
356 goto failed_unload;
357 }
358 printk(KERN_INFO "NILFS: recovery complete.\n");
359
360 skip_recovery:
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700361 set_nilfs_loaded(nilfs);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900362 nilfs_clear_recovery_info(&ri);
363 sbi->s_super->s_flags = s_flags;
364 return 0;
365
Ryusuke Konishi6c1251602010-06-28 19:15:26 +0900366 scan_error:
367 printk(KERN_ERR "NILFS: error searching super root.\n");
368 goto failed;
369
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900370 failed_unload:
371 nilfs_mdt_destroy(nilfs->ns_cpfile);
372 nilfs_mdt_destroy(nilfs->ns_sufile);
373 nilfs_mdt_destroy(nilfs->ns_dat);
Ryusuke Konishi4afc3132010-08-29 01:55:38 +0900374 nilfs_mdt_destroy(nilfs->ns_gc_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700375
376 failed:
377 nilfs_clear_recovery_info(&ri);
378 sbi->s_super->s_flags = s_flags;
379 return err;
380}
381
382static unsigned long long nilfs_max_size(unsigned int blkbits)
383{
384 unsigned int max_bits;
385 unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
386
387 max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
388 if (max_bits < 64)
389 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
390 return res;
391}
392
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700393static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
394 struct nilfs_super_block *sbp)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700395{
Ryusuke Konishi9566a7a2010-08-10 00:58:41 +0900396 if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
397 printk(KERN_ERR "NILFS: unsupported revision "
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700398 "(superblock rev.=%d.%d, current rev.=%d.%d). "
399 "Please check the version of mkfs.nilfs.\n",
400 le32_to_cpu(sbp->s_rev_level),
401 le16_to_cpu(sbp->s_minor_rev_level),
402 NILFS_CURRENT_REV, NILFS_MINOR_REV);
403 return -EINVAL;
404 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700405 nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
406 if (nilfs->ns_sbsize > BLOCK_SIZE)
407 return -EINVAL;
408
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700409 nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
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) {
Ryusuke Konishic91cea12010-03-14 04:01:27 +0900414 printk(KERN_ERR "NILFS: too short segment.\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700415 return -EINVAL;
416 }
417
418 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
419 nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
420 nilfs->ns_r_segments_percentage =
421 le32_to_cpu(sbp->s_r_segments_percentage);
422 nilfs->ns_nrsvsegs =
423 max_t(unsigned long, NILFS_MIN_NRSVSEGS,
424 DIV_ROUND_UP(nilfs->ns_nsegments *
425 nilfs->ns_r_segments_percentage, 100));
426 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
427 return 0;
428}
429
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700430static int nilfs_valid_sb(struct nilfs_super_block *sbp)
431{
432 static unsigned char sum[4];
433 const int sumoff = offsetof(struct nilfs_super_block, s_sum);
434 size_t bytes;
435 u32 crc;
436
437 if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
438 return 0;
439 bytes = le16_to_cpu(sbp->s_bytes);
440 if (bytes > BLOCK_SIZE)
441 return 0;
442 crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
443 sumoff);
444 crc = crc32_le(crc, sum, 4);
445 crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
446 bytes - sumoff - 4);
447 return crc == le32_to_cpu(sbp->s_sum);
448}
449
450static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
451{
452 return offset < ((le64_to_cpu(sbp->s_nsegments) *
453 le32_to_cpu(sbp->s_blocks_per_segment)) <<
454 (le32_to_cpu(sbp->s_log_block_size) + 10));
455}
456
457static void nilfs_release_super_block(struct the_nilfs *nilfs)
458{
459 int i;
460
461 for (i = 0; i < 2; i++) {
462 if (nilfs->ns_sbp[i]) {
463 brelse(nilfs->ns_sbh[i]);
464 nilfs->ns_sbh[i] = NULL;
465 nilfs->ns_sbp[i] = NULL;
466 }
467 }
468}
469
470void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
471{
472 brelse(nilfs->ns_sbh[0]);
473 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
474 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
475 nilfs->ns_sbh[1] = NULL;
476 nilfs->ns_sbp[1] = NULL;
477}
478
479void nilfs_swap_super_block(struct the_nilfs *nilfs)
480{
481 struct buffer_head *tsbh = nilfs->ns_sbh[0];
482 struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
483
484 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
485 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
486 nilfs->ns_sbh[1] = tsbh;
487 nilfs->ns_sbp[1] = tsbp;
488}
489
490static int nilfs_load_super_block(struct the_nilfs *nilfs,
491 struct super_block *sb, int blocksize,
492 struct nilfs_super_block **sbpp)
493{
494 struct nilfs_super_block **sbp = nilfs->ns_sbp;
495 struct buffer_head **sbh = nilfs->ns_sbh;
496 u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
497 int valid[2], swp = 0;
498
499 sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
500 &sbh[0]);
501 sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
502
503 if (!sbp[0]) {
504 if (!sbp[1]) {
505 printk(KERN_ERR "NILFS: unable to read superblock\n");
506 return -EIO;
507 }
508 printk(KERN_WARNING
509 "NILFS warning: unable to read primary superblock\n");
510 } else if (!sbp[1])
511 printk(KERN_WARNING
512 "NILFS warning: unable to read secondary superblock\n");
513
Ryusuke Konishi25294d82010-05-01 11:54:21 +0900514 /*
515 * Compare two super blocks and set 1 in swp if the secondary
516 * super block is valid and newer. Otherwise, set 0 in swp.
517 */
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700518 valid[0] = nilfs_valid_sb(sbp[0]);
519 valid[1] = nilfs_valid_sb(sbp[1]);
Ryusuke Konishi25294d82010-05-01 11:54:21 +0900520 swp = valid[1] && (!valid[0] ||
521 le64_to_cpu(sbp[1]->s_last_cno) >
522 le64_to_cpu(sbp[0]->s_last_cno));
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700523
524 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
525 brelse(sbh[1]);
526 sbh[1] = NULL;
527 sbp[1] = NULL;
528 swp = 0;
529 }
530 if (!valid[swp]) {
531 nilfs_release_super_block(nilfs);
532 printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
533 sb->s_id);
534 return -EINVAL;
535 }
536
Ryusuke Konishiea1a16f2010-08-15 20:16:11 +0900537 if (!valid[!swp])
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700538 printk(KERN_WARNING "NILFS warning: broken superblock. "
539 "using spare superblock.\n");
Ryusuke Konishiea1a16f2010-08-15 20:16:11 +0900540 if (swp)
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700541 nilfs_swap_super_block(nilfs);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700542
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900543 nilfs->ns_sbwcount = 0;
544 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700545 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
546 *sbpp = sbp[0];
547 return 0;
548}
549
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700550/**
551 * init_nilfs - initialize a NILFS instance.
552 * @nilfs: the_nilfs structure
553 * @sbi: nilfs_sb_info
554 * @sb: super block
555 * @data: mount options
556 *
557 * init_nilfs() performs common initialization per block device (e.g.
558 * reading the super block, getting disk layout information, initializing
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900559 * shared fields in the_nilfs).
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700560 *
561 * Return Value: On success, 0 is returned. On error, a negative error
562 * code is returned.
563 */
564int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
565{
566 struct super_block *sb = sbi->s_super;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700567 struct nilfs_super_block *sbp;
568 struct backing_dev_info *bdi;
569 int blocksize;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700570 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700571
572 down_write(&nilfs->ns_sem);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700573
Ryusuke Konishi89c0fd02010-07-25 22:44:53 +0900574 blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700575 if (!blocksize) {
576 printk(KERN_ERR "NILFS: unable to set blocksize\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700577 err = -EINVAL;
578 goto out;
579 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700580 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
581 if (err)
582 goto out;
583
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700584 err = nilfs_store_magic_and_option(sb, sbp, data);
585 if (err)
586 goto failed_sbh;
587
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900588 err = nilfs_check_feature_compatibility(sb, sbp);
589 if (err)
590 goto failed_sbh;
591
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700592 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
Ryusuke Konishi89c0fd02010-07-25 22:44:53 +0900593 if (blocksize < NILFS_MIN_BLOCK_SIZE ||
594 blocksize > NILFS_MAX_BLOCK_SIZE) {
595 printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
596 "filesystem blocksize %d\n", blocksize);
597 err = -EINVAL;
598 goto failed_sbh;
599 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700600 if (sb->s_blocksize != blocksize) {
Martin K. Petersene1defc42009-05-22 17:17:49 -0400601 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700602
603 if (blocksize < hw_blocksize) {
604 printk(KERN_ERR
605 "NILFS: blocksize %d too small for device "
606 "(sector-size = %d).\n",
607 blocksize, hw_blocksize);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700608 err = -EINVAL;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700609 goto failed_sbh;
610 }
611 nilfs_release_super_block(nilfs);
612 sb_set_blocksize(sb, blocksize);
613
614 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
615 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700616 goto out;
617 /* not failed_sbh; sbh is released automatically
618 when reloading fails. */
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700619 }
620 nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
Ryusuke Konishi92c60cc2010-05-23 00:17:48 +0900621 nilfs->ns_blocksize = blocksize;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700622
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700623 err = nilfs_store_disk_layout(nilfs, sbp);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700624 if (err)
625 goto failed_sbh;
626
627 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
628
629 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700630
Jens Axboe2c96ce92009-09-15 09:43:56 +0200631 bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700632 nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
633
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900634 err = nilfs_store_log_cursor(nilfs, sbp);
635 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700636 goto failed_sbh;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700637
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700638 set_nilfs_init(nilfs);
639 err = 0;
640 out:
641 up_write(&nilfs->ns_sem);
642 return err;
643
644 failed_sbh:
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700645 nilfs_release_super_block(nilfs);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700646 goto out;
647}
648
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900649int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
650 size_t nsegs)
651{
652 sector_t seg_start, seg_end;
653 sector_t start = 0, nblocks = 0;
654 unsigned int sects_per_block;
655 __u64 *sn;
656 int ret = 0;
657
658 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
659 bdev_logical_block_size(nilfs->ns_bdev);
660 for (sn = segnump; sn < segnump + nsegs; sn++) {
661 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
662
663 if (!nblocks) {
664 start = seg_start;
665 nblocks = seg_end - seg_start + 1;
666 } else if (start + nblocks == seg_start) {
667 nblocks += seg_end - seg_start + 1;
668 } else {
669 ret = blkdev_issue_discard(nilfs->ns_bdev,
670 start * sects_per_block,
671 nblocks * sects_per_block,
672 GFP_NOFS,
Ryusuke Konishi1cb0c922010-08-18 21:11:11 +0900673 BLKDEV_IFL_WAIT |
Stephen Rothwell6a47dc12010-04-29 09:32:00 +0200674 BLKDEV_IFL_BARRIER);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900675 if (ret < 0)
676 return ret;
677 nblocks = 0;
678 }
679 }
680 if (nblocks)
681 ret = blkdev_issue_discard(nilfs->ns_bdev,
682 start * sects_per_block,
683 nblocks * sects_per_block,
Ryusuke Konishi1cb0c922010-08-18 21:11:11 +0900684 GFP_NOFS,
685 BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900686 return ret;
687}
688
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700689int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
690{
691 struct inode *dat = nilfs_dat_inode(nilfs);
692 unsigned long ncleansegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700693
694 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900695 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700696 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900697 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
698 return 0;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700699}
700
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700701int nilfs_near_disk_full(struct the_nilfs *nilfs)
702{
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700703 unsigned long ncleansegs, nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700704
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900705 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
706 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
707 nilfs->ns_blocks_per_segment + 1;
708
709 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700710}
711
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900712struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
713{
714 struct rb_node *n;
715 struct nilfs_root *root;
716
717 spin_lock(&nilfs->ns_cptree_lock);
718 n = nilfs->ns_cptree.rb_node;
719 while (n) {
720 root = rb_entry(n, struct nilfs_root, rb_node);
721
722 if (cno < root->cno) {
723 n = n->rb_left;
724 } else if (cno > root->cno) {
725 n = n->rb_right;
726 } else {
727 atomic_inc(&root->count);
728 spin_unlock(&nilfs->ns_cptree_lock);
729 return root;
730 }
731 }
732 spin_unlock(&nilfs->ns_cptree_lock);
733
734 return NULL;
735}
736
737struct nilfs_root *
738nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
739{
740 struct rb_node **p, *parent;
741 struct nilfs_root *root, *new;
742
743 root = nilfs_lookup_root(nilfs, cno);
744 if (root)
745 return root;
746
747 new = kmalloc(sizeof(*root), GFP_KERNEL);
748 if (!new)
749 return NULL;
750
751 spin_lock(&nilfs->ns_cptree_lock);
752
753 p = &nilfs->ns_cptree.rb_node;
754 parent = NULL;
755
756 while (*p) {
757 parent = *p;
758 root = rb_entry(parent, struct nilfs_root, rb_node);
759
760 if (cno < root->cno) {
761 p = &(*p)->rb_left;
762 } else if (cno > root->cno) {
763 p = &(*p)->rb_right;
764 } else {
765 atomic_inc(&root->count);
766 spin_unlock(&nilfs->ns_cptree_lock);
767 kfree(new);
768 return root;
769 }
770 }
771
772 new->cno = cno;
773 new->ifile = NULL;
774 new->nilfs = nilfs;
775 atomic_set(&new->count, 1);
776 atomic_set(&new->inodes_count, 0);
777 atomic_set(&new->blocks_count, 0);
778
779 rb_link_node(&new->rb_node, parent, p);
780 rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
781
782 spin_unlock(&nilfs->ns_cptree_lock);
783
784 return new;
785}
786
787void nilfs_put_root(struct nilfs_root *root)
788{
789 if (atomic_dec_and_test(&root->count)) {
790 struct the_nilfs *nilfs = root->nilfs;
791
792 spin_lock(&nilfs->ns_cptree_lock);
793 rb_erase(&root->rb_node, &nilfs->ns_cptree);
794 spin_unlock(&nilfs->ns_cptree_lock);
795 if (root->ifile)
796 nilfs_mdt_destroy(root->ifile);
797
798 kfree(root);
799 }
800}
801
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700802int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
803 int snapshot_mount)
804{
Ryusuke Konishifd522022010-08-14 21:44:51 +0900805 struct nilfs_root *root;
806 int ret;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700807
Ryusuke Konishifd522022010-08-14 21:44:51 +0900808 if (cno < 0 || cno > nilfs->ns_cno)
809 return false;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700810
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700811 if (cno >= nilfs_last_cno(nilfs))
Ryusuke Konishifd522022010-08-14 21:44:51 +0900812 return true; /* protect recent checkpoints */
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700813
Ryusuke Konishifd522022010-08-14 21:44:51 +0900814 ret = false;
815 root = nilfs_lookup_root(nilfs, cno);
816 if (root) {
817 ret = true;
818 nilfs_put_root(root);
819 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700820 return ret;
821}