blob: b7666bc042562c6ef918a01ab1e8fa9fae7c7b90 [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 }
100 if (nilfs_init(nilfs)) {
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700101 brelse(nilfs->ns_sbh[0]);
102 brelse(nilfs->ns_sbh[1]);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700103 }
104 kfree(nilfs);
105}
106
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900107static int nilfs_load_super_root(struct the_nilfs *nilfs, sector_t sr_block)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700108{
109 struct buffer_head *bh_sr;
110 struct nilfs_super_root *raw_sr;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700111 struct nilfs_super_block **sbp = nilfs->ns_sbp;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700112 unsigned dat_entry_size, segment_usage_size, checkpoint_size;
113 unsigned inode_size;
114 int err;
115
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900116 err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700117 if (unlikely(err))
118 return err;
119
120 down_read(&nilfs->ns_sem);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700121 dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
122 checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
123 segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700124 up_read(&nilfs->ns_sem);
125
126 inode_size = nilfs->ns_inode_size;
127
128 err = -ENOMEM;
Ryusuke Konishi79739562009-11-12 23:56:43 +0900129 nilfs->ns_dat = nilfs_dat_new(nilfs, dat_entry_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700130 if (unlikely(!nilfs->ns_dat))
131 goto failed;
132
Ryusuke Konishi79739562009-11-12 23:56:43 +0900133 nilfs->ns_cpfile = nilfs_cpfile_new(nilfs, checkpoint_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700134 if (unlikely(!nilfs->ns_cpfile))
Ryusuke Konishic1c1d70922010-08-29 12:44:56 +0900135 goto failed_dat;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700136
Ryusuke Konishi79739562009-11-12 23:56:43 +0900137 nilfs->ns_sufile = nilfs_sufile_new(nilfs, segment_usage_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700138 if (unlikely(!nilfs->ns_sufile))
139 goto failed_cpfile;
140
Ryusuke Konishi8707df32009-11-13 01:36:56 +0900141 err = nilfs_dat_read(nilfs->ns_dat, (void *)bh_sr->b_data +
142 NILFS_SR_DAT_OFFSET(inode_size));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700143 if (unlikely(err))
144 goto failed_sufile;
145
Ryusuke Konishi8707df32009-11-13 01:36:56 +0900146 err = nilfs_cpfile_read(nilfs->ns_cpfile, (void *)bh_sr->b_data +
147 NILFS_SR_CPFILE_OFFSET(inode_size));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700148 if (unlikely(err))
149 goto failed_sufile;
150
Ryusuke Konishi8707df32009-11-13 01:36:56 +0900151 err = nilfs_sufile_read(nilfs->ns_sufile, (void *)bh_sr->b_data +
152 NILFS_SR_SUFILE_OFFSET(inode_size));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700153 if (unlikely(err))
154 goto failed_sufile;
155
156 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
157 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
158
159 failed:
160 brelse(bh_sr);
161 return err;
162
163 failed_sufile:
164 nilfs_mdt_destroy(nilfs->ns_sufile);
165
166 failed_cpfile:
167 nilfs_mdt_destroy(nilfs->ns_cpfile);
168
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700169 failed_dat:
170 nilfs_mdt_destroy(nilfs->ns_dat);
171 goto failed;
172}
173
174static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
175{
176 memset(ri, 0, sizeof(*ri));
177 INIT_LIST_HEAD(&ri->ri_used_segments);
178}
179
180static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
181{
182 nilfs_dispose_segment_list(&ri->ri_used_segments);
183}
184
185/**
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900186 * nilfs_store_log_cursor - load log cursor from a super block
187 * @nilfs: nilfs object
188 * @sbp: buffer storing super block to be read
189 *
190 * nilfs_store_log_cursor() reads the last position of the log
191 * containing a super root from a given super block, and initializes
192 * relevant information on the nilfs object preparatory for log
193 * scanning and recovery.
194 */
195static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
196 struct nilfs_super_block *sbp)
197{
198 int ret = 0;
199
200 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
201 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
202 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
203
Ryusuke Konishi32502042010-06-29 14:42:13 +0900204 nilfs->ns_prev_seq = nilfs->ns_last_seq;
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900205 nilfs->ns_seg_seq = nilfs->ns_last_seq;
206 nilfs->ns_segnum =
207 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
208 nilfs->ns_cno = nilfs->ns_last_cno + 1;
209 if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
210 printk(KERN_ERR "NILFS invalid last segment number.\n");
211 ret = -EINVAL;
212 }
213 return ret;
214}
215
216/**
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700217 * load_nilfs - load and recover the nilfs
218 * @nilfs: the_nilfs structure to be released
219 * @sbi: nilfs_sb_info used to recover past segment
220 *
221 * load_nilfs() searches and load the latest super root,
222 * attaches the last segment, and does recovery if needed.
223 * The caller must call this exclusively for simultaneous mounts.
224 */
225int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
226{
227 struct nilfs_recovery_info ri;
228 unsigned int s_flags = sbi->s_super->s_flags;
229 int really_read_only = bdev_read_only(nilfs->ns_bdev);
Ryusuke Konishia057d2c2009-11-19 19:58:46 +0900230 int valid_fs = nilfs_valid_fs(nilfs);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900231 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700232
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900233 if (!valid_fs) {
234 printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
235 if (s_flags & MS_RDONLY) {
236 printk(KERN_INFO "NILFS: INFO: recovery "
237 "required for readonly filesystem.\n");
238 printk(KERN_INFO "NILFS: write access will "
239 "be enabled during recovery.\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700240 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700241 }
242
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900243 nilfs_init_recovery_info(&ri);
244
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900245 err = nilfs_search_super_root(nilfs, &ri);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700246 if (unlikely(err)) {
Ryusuke Konishi6c1251602010-06-28 19:15:26 +0900247 struct nilfs_super_block **sbp = nilfs->ns_sbp;
248 int blocksize;
249
250 if (err != -EINVAL)
251 goto scan_error;
252
253 if (!nilfs_valid_sb(sbp[1])) {
254 printk(KERN_WARNING
255 "NILFS warning: unable to fall back to spare"
256 "super block\n");
257 goto scan_error;
258 }
259 printk(KERN_INFO
260 "NILFS: try rollback from an earlier position\n");
261
262 /*
263 * restore super block with its spare and reconfigure
264 * relevant states of the nilfs object.
265 */
266 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
267 nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
268 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
269
270 /* verify consistency between two super blocks */
271 blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
272 if (blocksize != nilfs->ns_blocksize) {
273 printk(KERN_WARNING
274 "NILFS warning: blocksize differs between "
275 "two super blocks (%d != %d)\n",
276 blocksize, nilfs->ns_blocksize);
277 goto scan_error;
278 }
279
280 err = nilfs_store_log_cursor(nilfs, sbp[0]);
281 if (err)
282 goto scan_error;
283
284 /* drop clean flag to allow roll-forward and recovery */
285 nilfs->ns_mount_state &= ~NILFS_VALID_FS;
286 valid_fs = 0;
287
288 err = nilfs_search_super_root(nilfs, &ri);
289 if (err)
290 goto scan_error;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700291 }
292
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900293 err = nilfs_load_super_root(nilfs, ri.ri_super_root);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700294 if (unlikely(err)) {
295 printk(KERN_ERR "NILFS: error loading super root.\n");
296 goto failed;
297 }
298
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900299 if (valid_fs)
300 goto skip_recovery;
301
302 if (s_flags & MS_RDONLY) {
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900303 __u64 features;
304
Ryusuke Konishi02345762009-11-20 03:28:01 +0900305 if (nilfs_test_opt(sbi, NORECOVERY)) {
306 printk(KERN_INFO "NILFS: norecovery option specified. "
307 "skipping roll-forward recovery\n");
308 goto skip_recovery;
309 }
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900310 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
311 ~NILFS_FEATURE_COMPAT_RO_SUPP;
312 if (features) {
313 printk(KERN_ERR "NILFS: couldn't proceed with "
314 "recovery because of unsupported optional "
315 "features (%llx)\n",
316 (unsigned long long)features);
317 err = -EROFS;
318 goto failed_unload;
319 }
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900320 if (really_read_only) {
321 printk(KERN_ERR "NILFS: write access "
322 "unavailable, cannot proceed.\n");
323 err = -EROFS;
324 goto failed_unload;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700325 }
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900326 sbi->s_super->s_flags &= ~MS_RDONLY;
Ryusuke Konishi02345762009-11-20 03:28:01 +0900327 } else if (nilfs_test_opt(sbi, NORECOVERY)) {
328 printk(KERN_ERR "NILFS: recovery cancelled because norecovery "
329 "option was specified for a read/write mount\n");
330 err = -EINVAL;
331 goto failed_unload;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700332 }
333
Ryusuke Konishiaee5ce22010-05-23 12:21:57 +0900334 err = nilfs_salvage_orphan_logs(nilfs, sbi, &ri);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900335 if (err)
336 goto failed_unload;
337
338 down_write(&nilfs->ns_sem);
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900339 nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
340 err = nilfs_cleanup_super(sbi);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900341 up_write(&nilfs->ns_sem);
342
343 if (err) {
344 printk(KERN_ERR "NILFS: failed to update super block. "
345 "recovery unfinished.\n");
346 goto failed_unload;
347 }
348 printk(KERN_INFO "NILFS: recovery complete.\n");
349
350 skip_recovery:
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700351 set_nilfs_loaded(nilfs);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900352 nilfs_clear_recovery_info(&ri);
353 sbi->s_super->s_flags = s_flags;
354 return 0;
355
Ryusuke Konishi6c1251602010-06-28 19:15:26 +0900356 scan_error:
357 printk(KERN_ERR "NILFS: error searching super root.\n");
358 goto failed;
359
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900360 failed_unload:
361 nilfs_mdt_destroy(nilfs->ns_cpfile);
362 nilfs_mdt_destroy(nilfs->ns_sufile);
363 nilfs_mdt_destroy(nilfs->ns_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700364
365 failed:
366 nilfs_clear_recovery_info(&ri);
367 sbi->s_super->s_flags = s_flags;
368 return err;
369}
370
371static unsigned long long nilfs_max_size(unsigned int blkbits)
372{
373 unsigned int max_bits;
374 unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
375
376 max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
377 if (max_bits < 64)
378 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
379 return res;
380}
381
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700382static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
383 struct nilfs_super_block *sbp)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700384{
Ryusuke Konishi9566a7a2010-08-10 00:58:41 +0900385 if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
386 printk(KERN_ERR "NILFS: unsupported revision "
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700387 "(superblock rev.=%d.%d, current rev.=%d.%d). "
388 "Please check the version of mkfs.nilfs.\n",
389 le32_to_cpu(sbp->s_rev_level),
390 le16_to_cpu(sbp->s_minor_rev_level),
391 NILFS_CURRENT_REV, NILFS_MINOR_REV);
392 return -EINVAL;
393 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700394 nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
395 if (nilfs->ns_sbsize > BLOCK_SIZE)
396 return -EINVAL;
397
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700398 nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
399 nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
400
401 nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
402 if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
Ryusuke Konishic91cea12010-03-14 04:01:27 +0900403 printk(KERN_ERR "NILFS: too short segment.\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700404 return -EINVAL;
405 }
406
407 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
408 nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments);
409 nilfs->ns_r_segments_percentage =
410 le32_to_cpu(sbp->s_r_segments_percentage);
411 nilfs->ns_nrsvsegs =
412 max_t(unsigned long, NILFS_MIN_NRSVSEGS,
413 DIV_ROUND_UP(nilfs->ns_nsegments *
414 nilfs->ns_r_segments_percentage, 100));
415 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
416 return 0;
417}
418
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700419static int nilfs_valid_sb(struct nilfs_super_block *sbp)
420{
421 static unsigned char sum[4];
422 const int sumoff = offsetof(struct nilfs_super_block, s_sum);
423 size_t bytes;
424 u32 crc;
425
426 if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
427 return 0;
428 bytes = le16_to_cpu(sbp->s_bytes);
429 if (bytes > BLOCK_SIZE)
430 return 0;
431 crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
432 sumoff);
433 crc = crc32_le(crc, sum, 4);
434 crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
435 bytes - sumoff - 4);
436 return crc == le32_to_cpu(sbp->s_sum);
437}
438
439static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
440{
441 return offset < ((le64_to_cpu(sbp->s_nsegments) *
442 le32_to_cpu(sbp->s_blocks_per_segment)) <<
443 (le32_to_cpu(sbp->s_log_block_size) + 10));
444}
445
446static void nilfs_release_super_block(struct the_nilfs *nilfs)
447{
448 int i;
449
450 for (i = 0; i < 2; i++) {
451 if (nilfs->ns_sbp[i]) {
452 brelse(nilfs->ns_sbh[i]);
453 nilfs->ns_sbh[i] = NULL;
454 nilfs->ns_sbp[i] = NULL;
455 }
456 }
457}
458
459void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
460{
461 brelse(nilfs->ns_sbh[0]);
462 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
463 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
464 nilfs->ns_sbh[1] = NULL;
465 nilfs->ns_sbp[1] = NULL;
466}
467
468void nilfs_swap_super_block(struct the_nilfs *nilfs)
469{
470 struct buffer_head *tsbh = nilfs->ns_sbh[0];
471 struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
472
473 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
474 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
475 nilfs->ns_sbh[1] = tsbh;
476 nilfs->ns_sbp[1] = tsbp;
477}
478
479static int nilfs_load_super_block(struct the_nilfs *nilfs,
480 struct super_block *sb, int blocksize,
481 struct nilfs_super_block **sbpp)
482{
483 struct nilfs_super_block **sbp = nilfs->ns_sbp;
484 struct buffer_head **sbh = nilfs->ns_sbh;
485 u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
486 int valid[2], swp = 0;
487
488 sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
489 &sbh[0]);
490 sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
491
492 if (!sbp[0]) {
493 if (!sbp[1]) {
494 printk(KERN_ERR "NILFS: unable to read superblock\n");
495 return -EIO;
496 }
497 printk(KERN_WARNING
498 "NILFS warning: unable to read primary superblock\n");
499 } else if (!sbp[1])
500 printk(KERN_WARNING
501 "NILFS warning: unable to read secondary superblock\n");
502
Ryusuke Konishi25294d82010-05-01 11:54:21 +0900503 /*
504 * Compare two super blocks and set 1 in swp if the secondary
505 * super block is valid and newer. Otherwise, set 0 in swp.
506 */
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700507 valid[0] = nilfs_valid_sb(sbp[0]);
508 valid[1] = nilfs_valid_sb(sbp[1]);
Ryusuke Konishi25294d82010-05-01 11:54:21 +0900509 swp = valid[1] && (!valid[0] ||
510 le64_to_cpu(sbp[1]->s_last_cno) >
511 le64_to_cpu(sbp[0]->s_last_cno));
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700512
513 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
514 brelse(sbh[1]);
515 sbh[1] = NULL;
516 sbp[1] = NULL;
517 swp = 0;
518 }
519 if (!valid[swp]) {
520 nilfs_release_super_block(nilfs);
521 printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
522 sb->s_id);
523 return -EINVAL;
524 }
525
Ryusuke Konishiea1a16f2010-08-15 20:16:11 +0900526 if (!valid[!swp])
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700527 printk(KERN_WARNING "NILFS warning: broken superblock. "
528 "using spare superblock.\n");
Ryusuke Konishiea1a16f2010-08-15 20:16:11 +0900529 if (swp)
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700530 nilfs_swap_super_block(nilfs);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700531
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900532 nilfs->ns_sbwcount = 0;
533 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700534 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
535 *sbpp = sbp[0];
536 return 0;
537}
538
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700539/**
540 * init_nilfs - initialize a NILFS instance.
541 * @nilfs: the_nilfs structure
542 * @sbi: nilfs_sb_info
543 * @sb: super block
544 * @data: mount options
545 *
546 * init_nilfs() performs common initialization per block device (e.g.
547 * reading the super block, getting disk layout information, initializing
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900548 * shared fields in the_nilfs).
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700549 *
550 * Return Value: On success, 0 is returned. On error, a negative error
551 * code is returned.
552 */
553int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
554{
555 struct super_block *sb = sbi->s_super;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700556 struct nilfs_super_block *sbp;
557 struct backing_dev_info *bdi;
558 int blocksize;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700559 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700560
561 down_write(&nilfs->ns_sem);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700562
Ryusuke Konishi89c0fd02010-07-25 22:44:53 +0900563 blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700564 if (!blocksize) {
565 printk(KERN_ERR "NILFS: unable to set blocksize\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700566 err = -EINVAL;
567 goto out;
568 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700569 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
570 if (err)
571 goto out;
572
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700573 err = nilfs_store_magic_and_option(sb, sbp, data);
574 if (err)
575 goto failed_sbh;
576
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900577 err = nilfs_check_feature_compatibility(sb, sbp);
578 if (err)
579 goto failed_sbh;
580
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700581 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
Ryusuke Konishi89c0fd02010-07-25 22:44:53 +0900582 if (blocksize < NILFS_MIN_BLOCK_SIZE ||
583 blocksize > NILFS_MAX_BLOCK_SIZE) {
584 printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
585 "filesystem blocksize %d\n", blocksize);
586 err = -EINVAL;
587 goto failed_sbh;
588 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700589 if (sb->s_blocksize != blocksize) {
Martin K. Petersene1defc42009-05-22 17:17:49 -0400590 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700591
592 if (blocksize < hw_blocksize) {
593 printk(KERN_ERR
594 "NILFS: blocksize %d too small for device "
595 "(sector-size = %d).\n",
596 blocksize, hw_blocksize);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700597 err = -EINVAL;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700598 goto failed_sbh;
599 }
600 nilfs_release_super_block(nilfs);
601 sb_set_blocksize(sb, blocksize);
602
603 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
604 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700605 goto out;
606 /* not failed_sbh; sbh is released automatically
607 when reloading fails. */
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700608 }
609 nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
Ryusuke Konishi92c60cc2010-05-23 00:17:48 +0900610 nilfs->ns_blocksize = blocksize;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700611
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700612 err = nilfs_store_disk_layout(nilfs, sbp);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700613 if (err)
614 goto failed_sbh;
615
616 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
617
618 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700619
Jens Axboe2c96ce92009-09-15 09:43:56 +0200620 bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700621 nilfs->ns_bdi = bdi ? : &default_backing_dev_info;
622
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900623 err = nilfs_store_log_cursor(nilfs, sbp);
624 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700625 goto failed_sbh;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700626
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700627 set_nilfs_init(nilfs);
628 err = 0;
629 out:
630 up_write(&nilfs->ns_sem);
631 return err;
632
633 failed_sbh:
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700634 nilfs_release_super_block(nilfs);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700635 goto out;
636}
637
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900638int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
639 size_t nsegs)
640{
641 sector_t seg_start, seg_end;
642 sector_t start = 0, nblocks = 0;
643 unsigned int sects_per_block;
644 __u64 *sn;
645 int ret = 0;
646
647 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
648 bdev_logical_block_size(nilfs->ns_bdev);
649 for (sn = segnump; sn < segnump + nsegs; sn++) {
650 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
651
652 if (!nblocks) {
653 start = seg_start;
654 nblocks = seg_end - seg_start + 1;
655 } else if (start + nblocks == seg_start) {
656 nblocks += seg_end - seg_start + 1;
657 } else {
658 ret = blkdev_issue_discard(nilfs->ns_bdev,
659 start * sects_per_block,
660 nblocks * sects_per_block,
661 GFP_NOFS,
Ryusuke Konishi1cb0c922010-08-18 21:11:11 +0900662 BLKDEV_IFL_WAIT |
Stephen Rothwell6a47dc12010-04-29 09:32:00 +0200663 BLKDEV_IFL_BARRIER);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900664 if (ret < 0)
665 return ret;
666 nblocks = 0;
667 }
668 }
669 if (nblocks)
670 ret = blkdev_issue_discard(nilfs->ns_bdev,
671 start * sects_per_block,
672 nblocks * sects_per_block,
Ryusuke Konishi1cb0c922010-08-18 21:11:11 +0900673 GFP_NOFS,
674 BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900675 return ret;
676}
677
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700678int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
679{
680 struct inode *dat = nilfs_dat_inode(nilfs);
681 unsigned long ncleansegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700682
683 down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900684 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700685 up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900686 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
687 return 0;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700688}
689
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700690int nilfs_near_disk_full(struct the_nilfs *nilfs)
691{
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700692 unsigned long ncleansegs, nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700693
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900694 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
695 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
696 nilfs->ns_blocks_per_segment + 1;
697
698 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700699}
700
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900701struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
702{
703 struct rb_node *n;
704 struct nilfs_root *root;
705
706 spin_lock(&nilfs->ns_cptree_lock);
707 n = nilfs->ns_cptree.rb_node;
708 while (n) {
709 root = rb_entry(n, struct nilfs_root, rb_node);
710
711 if (cno < root->cno) {
712 n = n->rb_left;
713 } else if (cno > root->cno) {
714 n = n->rb_right;
715 } else {
716 atomic_inc(&root->count);
717 spin_unlock(&nilfs->ns_cptree_lock);
718 return root;
719 }
720 }
721 spin_unlock(&nilfs->ns_cptree_lock);
722
723 return NULL;
724}
725
726struct nilfs_root *
727nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
728{
729 struct rb_node **p, *parent;
730 struct nilfs_root *root, *new;
731
732 root = nilfs_lookup_root(nilfs, cno);
733 if (root)
734 return root;
735
736 new = kmalloc(sizeof(*root), GFP_KERNEL);
737 if (!new)
738 return NULL;
739
740 spin_lock(&nilfs->ns_cptree_lock);
741
742 p = &nilfs->ns_cptree.rb_node;
743 parent = NULL;
744
745 while (*p) {
746 parent = *p;
747 root = rb_entry(parent, struct nilfs_root, rb_node);
748
749 if (cno < root->cno) {
750 p = &(*p)->rb_left;
751 } else if (cno > root->cno) {
752 p = &(*p)->rb_right;
753 } else {
754 atomic_inc(&root->count);
755 spin_unlock(&nilfs->ns_cptree_lock);
756 kfree(new);
757 return root;
758 }
759 }
760
761 new->cno = cno;
762 new->ifile = NULL;
763 new->nilfs = nilfs;
764 atomic_set(&new->count, 1);
765 atomic_set(&new->inodes_count, 0);
766 atomic_set(&new->blocks_count, 0);
767
768 rb_link_node(&new->rb_node, parent, p);
769 rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
770
771 spin_unlock(&nilfs->ns_cptree_lock);
772
773 return new;
774}
775
776void nilfs_put_root(struct nilfs_root *root)
777{
778 if (atomic_dec_and_test(&root->count)) {
779 struct the_nilfs *nilfs = root->nilfs;
780
781 spin_lock(&nilfs->ns_cptree_lock);
782 rb_erase(&root->rb_node, &nilfs->ns_cptree);
783 spin_unlock(&nilfs->ns_cptree_lock);
784 if (root->ifile)
785 nilfs_mdt_destroy(root->ifile);
786
787 kfree(root);
788 }
789}
790
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700791int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno,
792 int snapshot_mount)
793{
Ryusuke Konishifd522022010-08-14 21:44:51 +0900794 struct nilfs_root *root;
795 int ret;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700796
Ryusuke Konishifd522022010-08-14 21:44:51 +0900797 if (cno < 0 || cno > nilfs->ns_cno)
798 return false;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700799
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700800 if (cno >= nilfs_last_cno(nilfs))
Ryusuke Konishifd522022010-08-14 21:44:51 +0900801 return true; /* protect recent checkpoints */
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700802
Ryusuke Konishifd522022010-08-14 21:44:51 +0900803 ret = false;
804 root = nilfs_lookup_root(nilfs, cno);
805 if (root) {
806 ret = true;
807 nilfs_put_root(root);
808 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700809 return ret;
810}