blob: 4d6763e28eb5ed518ec871771d567923d07ef85a [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();
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070095 if (nilfs_init(nilfs)) {
Ryusuke Konishie339ad32009-04-06 19:01:59 -070096 brelse(nilfs->ns_sbh[0]);
97 brelse(nilfs->ns_sbh[1]);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070098 }
99 kfree(nilfs);
100}
101
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900102static int nilfs_load_super_root(struct the_nilfs *nilfs,
103 struct super_block *sb, sector_t sr_block)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700104{
105 struct buffer_head *bh_sr;
106 struct nilfs_super_root *raw_sr;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700107 struct nilfs_super_block **sbp = nilfs->ns_sbp;
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900108 struct nilfs_inode *rawi;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700109 unsigned dat_entry_size, segment_usage_size, checkpoint_size;
110 unsigned inode_size;
111 int err;
112
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900113 err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700114 if (unlikely(err))
115 return err;
116
117 down_read(&nilfs->ns_sem);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700118 dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
119 checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
120 segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700121 up_read(&nilfs->ns_sem);
122
123 inode_size = nilfs->ns_inode_size;
124
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900125 rawi = (void *)bh_sr->b_data + NILFS_SR_DAT_OFFSET(inode_size);
126 err = nilfs_dat_read(sb, dat_entry_size, rawi, &nilfs->ns_dat);
127 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700128 goto failed;
129
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900130 rawi = (void *)bh_sr->b_data + NILFS_SR_CPFILE_OFFSET(inode_size);
131 err = nilfs_cpfile_read(sb, checkpoint_size, rawi, &nilfs->ns_cpfile);
132 if (err)
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +0900133 goto failed_dat;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700134
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900135 rawi = (void *)bh_sr->b_data + NILFS_SR_SUFILE_OFFSET(inode_size);
136 err = nilfs_sufile_read(sb, segment_usage_size, rawi,
137 &nilfs->ns_sufile);
138 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700139 goto failed_cpfile;
140
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700141 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
142 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
143
144 failed:
145 brelse(bh_sr);
146 return err;
147
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700148 failed_cpfile:
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900149 iput(nilfs->ns_cpfile);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700150
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700151 failed_dat:
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900152 iput(nilfs->ns_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700153 goto failed;
154}
155
156static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
157{
158 memset(ri, 0, sizeof(*ri));
159 INIT_LIST_HEAD(&ri->ri_used_segments);
160}
161
162static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
163{
164 nilfs_dispose_segment_list(&ri->ri_used_segments);
165}
166
167/**
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900168 * nilfs_store_log_cursor - load log cursor from a super block
169 * @nilfs: nilfs object
170 * @sbp: buffer storing super block to be read
171 *
172 * nilfs_store_log_cursor() reads the last position of the log
173 * containing a super root from a given super block, and initializes
174 * relevant information on the nilfs object preparatory for log
175 * scanning and recovery.
176 */
177static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
178 struct nilfs_super_block *sbp)
179{
180 int ret = 0;
181
182 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
183 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
184 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
185
Ryusuke Konishi32502042010-06-29 14:42:13 +0900186 nilfs->ns_prev_seq = nilfs->ns_last_seq;
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900187 nilfs->ns_seg_seq = nilfs->ns_last_seq;
188 nilfs->ns_segnum =
189 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
190 nilfs->ns_cno = nilfs->ns_last_cno + 1;
191 if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
192 printk(KERN_ERR "NILFS invalid last segment number.\n");
193 ret = -EINVAL;
194 }
195 return ret;
196}
197
198/**
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700199 * load_nilfs - load and recover the nilfs
200 * @nilfs: the_nilfs structure to be released
201 * @sbi: nilfs_sb_info used to recover past segment
202 *
203 * load_nilfs() searches and load the latest super root,
204 * attaches the last segment, and does recovery if needed.
205 * The caller must call this exclusively for simultaneous mounts.
206 */
207int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
208{
209 struct nilfs_recovery_info ri;
210 unsigned int s_flags = sbi->s_super->s_flags;
211 int really_read_only = bdev_read_only(nilfs->ns_bdev);
Ryusuke Konishia057d2c2009-11-19 19:58:46 +0900212 int valid_fs = nilfs_valid_fs(nilfs);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900213 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700214
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900215 if (!valid_fs) {
216 printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
217 if (s_flags & MS_RDONLY) {
218 printk(KERN_INFO "NILFS: INFO: recovery "
219 "required for readonly filesystem.\n");
220 printk(KERN_INFO "NILFS: write access will "
221 "be enabled during recovery.\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700222 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700223 }
224
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900225 nilfs_init_recovery_info(&ri);
226
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900227 err = nilfs_search_super_root(nilfs, &ri);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700228 if (unlikely(err)) {
Ryusuke Konishi6c1251602010-06-28 19:15:26 +0900229 struct nilfs_super_block **sbp = nilfs->ns_sbp;
230 int blocksize;
231
232 if (err != -EINVAL)
233 goto scan_error;
234
235 if (!nilfs_valid_sb(sbp[1])) {
236 printk(KERN_WARNING
237 "NILFS warning: unable to fall back to spare"
238 "super block\n");
239 goto scan_error;
240 }
241 printk(KERN_INFO
242 "NILFS: try rollback from an earlier position\n");
243
244 /*
245 * restore super block with its spare and reconfigure
246 * relevant states of the nilfs object.
247 */
248 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
249 nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
250 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
251
252 /* verify consistency between two super blocks */
253 blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
254 if (blocksize != nilfs->ns_blocksize) {
255 printk(KERN_WARNING
256 "NILFS warning: blocksize differs between "
257 "two super blocks (%d != %d)\n",
258 blocksize, nilfs->ns_blocksize);
259 goto scan_error;
260 }
261
262 err = nilfs_store_log_cursor(nilfs, sbp[0]);
263 if (err)
264 goto scan_error;
265
266 /* drop clean flag to allow roll-forward and recovery */
267 nilfs->ns_mount_state &= ~NILFS_VALID_FS;
268 valid_fs = 0;
269
270 err = nilfs_search_super_root(nilfs, &ri);
271 if (err)
272 goto scan_error;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700273 }
274
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900275 err = nilfs_load_super_root(nilfs, sbi->s_super, ri.ri_super_root);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700276 if (unlikely(err)) {
277 printk(KERN_ERR "NILFS: error loading super root.\n");
278 goto failed;
279 }
280
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900281 if (valid_fs)
282 goto skip_recovery;
283
284 if (s_flags & MS_RDONLY) {
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900285 __u64 features;
286
Ryusuke Konishi02345762009-11-20 03:28:01 +0900287 if (nilfs_test_opt(sbi, NORECOVERY)) {
288 printk(KERN_INFO "NILFS: norecovery option specified. "
289 "skipping roll-forward recovery\n");
290 goto skip_recovery;
291 }
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900292 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
293 ~NILFS_FEATURE_COMPAT_RO_SUPP;
294 if (features) {
295 printk(KERN_ERR "NILFS: couldn't proceed with "
296 "recovery because of unsupported optional "
297 "features (%llx)\n",
298 (unsigned long long)features);
299 err = -EROFS;
300 goto failed_unload;
301 }
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900302 if (really_read_only) {
303 printk(KERN_ERR "NILFS: write access "
304 "unavailable, cannot proceed.\n");
305 err = -EROFS;
306 goto failed_unload;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700307 }
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900308 sbi->s_super->s_flags &= ~MS_RDONLY;
Ryusuke Konishi02345762009-11-20 03:28:01 +0900309 } else if (nilfs_test_opt(sbi, NORECOVERY)) {
310 printk(KERN_ERR "NILFS: recovery cancelled because norecovery "
311 "option was specified for a read/write mount\n");
312 err = -EINVAL;
313 goto failed_unload;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700314 }
315
Ryusuke Konishiaee5ce22010-05-23 12:21:57 +0900316 err = nilfs_salvage_orphan_logs(nilfs, sbi, &ri);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900317 if (err)
318 goto failed_unload;
319
320 down_write(&nilfs->ns_sem);
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900321 nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
322 err = nilfs_cleanup_super(sbi);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900323 up_write(&nilfs->ns_sem);
324
325 if (err) {
326 printk(KERN_ERR "NILFS: failed to update super block. "
327 "recovery unfinished.\n");
328 goto failed_unload;
329 }
330 printk(KERN_INFO "NILFS: recovery complete.\n");
331
332 skip_recovery:
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700333 set_nilfs_loaded(nilfs);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900334 nilfs_clear_recovery_info(&ri);
335 sbi->s_super->s_flags = s_flags;
336 return 0;
337
Ryusuke Konishi6c1251602010-06-28 19:15:26 +0900338 scan_error:
339 printk(KERN_ERR "NILFS: error searching super root.\n");
340 goto failed;
341
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900342 failed_unload:
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900343 iput(nilfs->ns_cpfile);
344 iput(nilfs->ns_sufile);
345 iput(nilfs->ns_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700346
347 failed:
348 nilfs_clear_recovery_info(&ri);
349 sbi->s_super->s_flags = s_flags;
350 return err;
351}
352
353static unsigned long long nilfs_max_size(unsigned int blkbits)
354{
355 unsigned int max_bits;
356 unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
357
358 max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
359 if (max_bits < 64)
360 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
361 return res;
362}
363
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700364static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
365 struct nilfs_super_block *sbp)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700366{
Ryusuke Konishi9566a7a2010-08-10 00:58:41 +0900367 if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
368 printk(KERN_ERR "NILFS: unsupported revision "
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700369 "(superblock rev.=%d.%d, current rev.=%d.%d). "
370 "Please check the version of mkfs.nilfs.\n",
371 le32_to_cpu(sbp->s_rev_level),
372 le16_to_cpu(sbp->s_minor_rev_level),
373 NILFS_CURRENT_REV, NILFS_MINOR_REV);
374 return -EINVAL;
375 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700376 nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
377 if (nilfs->ns_sbsize > BLOCK_SIZE)
378 return -EINVAL;
379
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700380 nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
381 nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
382
383 nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
384 if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
Ryusuke Konishic91cea12010-03-14 04:01:27 +0900385 printk(KERN_ERR "NILFS: too short segment.\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700386 return -EINVAL;
387 }
388
389 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
390 nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
391 nilfs->ns_r_segments_percentage =
392 le32_to_cpu(sbp->s_r_segments_percentage);
393 nilfs->ns_nrsvsegs =
394 max_t(unsigned long, NILFS_MIN_NRSVSEGS,
395 DIV_ROUND_UP(nilfs->ns_nsegments *
396 nilfs->ns_r_segments_percentage, 100));
397 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
398 return 0;
399}
400
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700401static int nilfs_valid_sb(struct nilfs_super_block *sbp)
402{
403 static unsigned char sum[4];
404 const int sumoff = offsetof(struct nilfs_super_block, s_sum);
405 size_t bytes;
406 u32 crc;
407
408 if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
409 return 0;
410 bytes = le16_to_cpu(sbp->s_bytes);
411 if (bytes > BLOCK_SIZE)
412 return 0;
413 crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
414 sumoff);
415 crc = crc32_le(crc, sum, 4);
416 crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
417 bytes - sumoff - 4);
418 return crc == le32_to_cpu(sbp->s_sum);
419}
420
421static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
422{
423 return offset < ((le64_to_cpu(sbp->s_nsegments) *
424 le32_to_cpu(sbp->s_blocks_per_segment)) <<
425 (le32_to_cpu(sbp->s_log_block_size) + 10));
426}
427
428static void nilfs_release_super_block(struct the_nilfs *nilfs)
429{
430 int i;
431
432 for (i = 0; i < 2; i++) {
433 if (nilfs->ns_sbp[i]) {
434 brelse(nilfs->ns_sbh[i]);
435 nilfs->ns_sbh[i] = NULL;
436 nilfs->ns_sbp[i] = NULL;
437 }
438 }
439}
440
441void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
442{
443 brelse(nilfs->ns_sbh[0]);
444 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
445 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
446 nilfs->ns_sbh[1] = NULL;
447 nilfs->ns_sbp[1] = NULL;
448}
449
450void nilfs_swap_super_block(struct the_nilfs *nilfs)
451{
452 struct buffer_head *tsbh = nilfs->ns_sbh[0];
453 struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
454
455 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
456 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
457 nilfs->ns_sbh[1] = tsbh;
458 nilfs->ns_sbp[1] = tsbp;
459}
460
461static int nilfs_load_super_block(struct the_nilfs *nilfs,
462 struct super_block *sb, int blocksize,
463 struct nilfs_super_block **sbpp)
464{
465 struct nilfs_super_block **sbp = nilfs->ns_sbp;
466 struct buffer_head **sbh = nilfs->ns_sbh;
467 u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
468 int valid[2], swp = 0;
469
470 sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
471 &sbh[0]);
472 sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
473
474 if (!sbp[0]) {
475 if (!sbp[1]) {
476 printk(KERN_ERR "NILFS: unable to read superblock\n");
477 return -EIO;
478 }
479 printk(KERN_WARNING
480 "NILFS warning: unable to read primary superblock\n");
481 } else if (!sbp[1])
482 printk(KERN_WARNING
483 "NILFS warning: unable to read secondary superblock\n");
484
Ryusuke Konishi25294d82010-05-01 11:54:21 +0900485 /*
486 * Compare two super blocks and set 1 in swp if the secondary
487 * super block is valid and newer. Otherwise, set 0 in swp.
488 */
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700489 valid[0] = nilfs_valid_sb(sbp[0]);
490 valid[1] = nilfs_valid_sb(sbp[1]);
Ryusuke Konishi25294d82010-05-01 11:54:21 +0900491 swp = valid[1] && (!valid[0] ||
492 le64_to_cpu(sbp[1]->s_last_cno) >
493 le64_to_cpu(sbp[0]->s_last_cno));
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700494
495 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
496 brelse(sbh[1]);
497 sbh[1] = NULL;
498 sbp[1] = NULL;
499 swp = 0;
500 }
501 if (!valid[swp]) {
502 nilfs_release_super_block(nilfs);
503 printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
504 sb->s_id);
505 return -EINVAL;
506 }
507
Ryusuke Konishiea1a16f2010-08-15 20:16:11 +0900508 if (!valid[!swp])
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700509 printk(KERN_WARNING "NILFS warning: broken superblock. "
510 "using spare superblock.\n");
Ryusuke Konishiea1a16f2010-08-15 20:16:11 +0900511 if (swp)
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700512 nilfs_swap_super_block(nilfs);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700513
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900514 nilfs->ns_sbwcount = 0;
515 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700516 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
517 *sbpp = sbp[0];
518 return 0;
519}
520
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700521/**
522 * init_nilfs - initialize a NILFS instance.
523 * @nilfs: the_nilfs structure
524 * @sbi: nilfs_sb_info
525 * @sb: super block
526 * @data: mount options
527 *
528 * init_nilfs() performs common initialization per block device (e.g.
529 * reading the super block, getting disk layout information, initializing
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900530 * shared fields in the_nilfs).
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700531 *
532 * Return Value: On success, 0 is returned. On error, a negative error
533 * code is returned.
534 */
535int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
536{
537 struct super_block *sb = sbi->s_super;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700538 struct nilfs_super_block *sbp;
539 struct backing_dev_info *bdi;
540 int blocksize;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700541 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700542
543 down_write(&nilfs->ns_sem);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700544
Ryusuke Konishi89c0fd02010-07-25 22:44:53 +0900545 blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700546 if (!blocksize) {
547 printk(KERN_ERR "NILFS: unable to set blocksize\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700548 err = -EINVAL;
549 goto out;
550 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700551 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
552 if (err)
553 goto out;
554
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700555 err = nilfs_store_magic_and_option(sb, sbp, data);
556 if (err)
557 goto failed_sbh;
558
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900559 err = nilfs_check_feature_compatibility(sb, sbp);
560 if (err)
561 goto failed_sbh;
562
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700563 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
Ryusuke Konishi89c0fd02010-07-25 22:44:53 +0900564 if (blocksize < NILFS_MIN_BLOCK_SIZE ||
565 blocksize > NILFS_MAX_BLOCK_SIZE) {
566 printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
567 "filesystem blocksize %d\n", blocksize);
568 err = -EINVAL;
569 goto failed_sbh;
570 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700571 if (sb->s_blocksize != blocksize) {
Martin K. Petersene1defc42009-05-22 17:17:49 -0400572 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700573
574 if (blocksize < hw_blocksize) {
575 printk(KERN_ERR
576 "NILFS: blocksize %d too small for device "
577 "(sector-size = %d).\n",
578 blocksize, hw_blocksize);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700579 err = -EINVAL;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700580 goto failed_sbh;
581 }
582 nilfs_release_super_block(nilfs);
583 sb_set_blocksize(sb, blocksize);
584
585 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
586 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700587 goto out;
588 /* not failed_sbh; sbh is released automatically
589 when reloading fails. */
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700590 }
591 nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
Ryusuke Konishi92c60cc2010-05-23 00:17:48 +0900592 nilfs->ns_blocksize = blocksize;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700593
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700594 err = nilfs_store_disk_layout(nilfs, sbp);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700595 if (err)
596 goto failed_sbh;
597
598 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
599
600 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700601
Jens Axboe2c96ce92009-09-15 09:43:56 +0200602 bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700603 nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
604
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900605 err = nilfs_store_log_cursor(nilfs, sbp);
606 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700607 goto failed_sbh;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700608
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700609 set_nilfs_init(nilfs);
610 err = 0;
611 out:
612 up_write(&nilfs->ns_sem);
613 return err;
614
615 failed_sbh:
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700616 nilfs_release_super_block(nilfs);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700617 goto out;
618}
619
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900620int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
621 size_t nsegs)
622{
623 sector_t seg_start, seg_end;
624 sector_t start = 0, nblocks = 0;
625 unsigned int sects_per_block;
626 __u64 *sn;
627 int ret = 0;
628
629 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
630 bdev_logical_block_size(nilfs->ns_bdev);
631 for (sn = segnump; sn < segnump + nsegs; sn++) {
632 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
633
634 if (!nblocks) {
635 start = seg_start;
636 nblocks = seg_end - seg_start + 1;
637 } else if (start + nblocks == seg_start) {
638 nblocks += seg_end - seg_start + 1;
639 } else {
640 ret = blkdev_issue_discard(nilfs->ns_bdev,
641 start * sects_per_block,
642 nblocks * sects_per_block,
643 GFP_NOFS,
Ryusuke Konishi1cb0c922010-08-18 21:11:11 +0900644 BLKDEV_IFL_WAIT |
Stephen Rothwell6a47dc12010-04-29 09:32:00 +0200645 BLKDEV_IFL_BARRIER);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900646 if (ret < 0)
647 return ret;
648 nblocks = 0;
649 }
650 }
651 if (nblocks)
652 ret = blkdev_issue_discard(nilfs->ns_bdev,
653 start * sects_per_block,
654 nblocks * sects_per_block,
Ryusuke Konishi1cb0c922010-08-18 21:11:11 +0900655 GFP_NOFS,
656 BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900657 return ret;
658}
659
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700660int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
661{
662 struct inode *dat = nilfs_dat_inode(nilfs);
663 unsigned long ncleansegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700664
665 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900666 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700667 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900668 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
669 return 0;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700670}
671
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700672int nilfs_near_disk_full(struct the_nilfs *nilfs)
673{
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700674 unsigned long ncleansegs, nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700675
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900676 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
677 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
678 nilfs->ns_blocks_per_segment + 1;
679
680 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700681}
682
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900683struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
684{
685 struct rb_node *n;
686 struct nilfs_root *root;
687
688 spin_lock(&nilfs->ns_cptree_lock);
689 n = nilfs->ns_cptree.rb_node;
690 while (n) {
691 root = rb_entry(n, struct nilfs_root, rb_node);
692
693 if (cno < root->cno) {
694 n = n->rb_left;
695 } else if (cno > root->cno) {
696 n = n->rb_right;
697 } else {
698 atomic_inc(&root->count);
699 spin_unlock(&nilfs->ns_cptree_lock);
700 return root;
701 }
702 }
703 spin_unlock(&nilfs->ns_cptree_lock);
704
705 return NULL;
706}
707
708struct nilfs_root *
709nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
710{
711 struct rb_node **p, *parent;
712 struct nilfs_root *root, *new;
713
714 root = nilfs_lookup_root(nilfs, cno);
715 if (root)
716 return root;
717
718 new = kmalloc(sizeof(*root), GFP_KERNEL);
719 if (!new)
720 return NULL;
721
722 spin_lock(&nilfs->ns_cptree_lock);
723
724 p = &nilfs->ns_cptree.rb_node;
725 parent = NULL;
726
727 while (*p) {
728 parent = *p;
729 root = rb_entry(parent, struct nilfs_root, rb_node);
730
731 if (cno < root->cno) {
732 p = &(*p)->rb_left;
733 } else if (cno > root->cno) {
734 p = &(*p)->rb_right;
735 } else {
736 atomic_inc(&root->count);
737 spin_unlock(&nilfs->ns_cptree_lock);
738 kfree(new);
739 return root;
740 }
741 }
742
743 new->cno = cno;
744 new->ifile = NULL;
745 new->nilfs = nilfs;
746 atomic_set(&new->count, 1);
747 atomic_set(&new->inodes_count, 0);
748 atomic_set(&new->blocks_count, 0);
749
750 rb_link_node(&new->rb_node, parent, p);
751 rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
752
753 spin_unlock(&nilfs->ns_cptree_lock);
754
755 return new;
756}
757
758void nilfs_put_root(struct nilfs_root *root)
759{
760 if (atomic_dec_and_test(&root->count)) {
761 struct the_nilfs *nilfs = root->nilfs;
762
763 spin_lock(&nilfs->ns_cptree_lock);
764 rb_erase(&root->rb_node, &nilfs->ns_cptree);
765 spin_unlock(&nilfs->ns_cptree_lock);
766 if (root->ifile)
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900767 iput(root->ifile);
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900768
769 kfree(root);
770 }
771}
772
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700773int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
774 int snapshot_mount)
775{
Ryusuke Konishifd522022010-08-14 21:44:51 +0900776 struct nilfs_root *root;
777 int ret;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700778
Ryusuke Konishifd522022010-08-14 21:44:51 +0900779 if (cno < 0 || cno > nilfs->ns_cno)
780 return false;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700781
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700782 if (cno >= nilfs_last_cno(nilfs))
Ryusuke Konishifd522022010-08-14 21:44:51 +0900783 return true; /* protect recent checkpoints */
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700784
Ryusuke Konishifd522022010-08-14 21:44:51 +0900785 ret = false;
786 root = nilfs_lookup_root(nilfs, cno);
787 if (root) {
788 ret = true;
789 nilfs_put_root(root);
790 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700791 return ret;
792}