blob: 40239a932dec6daa043c05f7a69a9a5b293fe6c0 [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 Konishi693dd322011-03-09 11:05:07 +090078 INIT_LIST_HEAD(&nilfs->ns_dirty_files);
Ryusuke Konishi263d90c2010-08-20 19:06:11 +090079 INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
Ryusuke Konishi693dd322011-03-09 11:05:07 +090080 spin_lock_init(&nilfs->ns_inode_lock);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070081 spin_lock_init(&nilfs->ns_last_segment_lock);
Ryusuke Konishiba65ae42010-08-14 12:59:15 +090082 nilfs->ns_cptree = RB_ROOT;
83 spin_lock_init(&nilfs->ns_cptree_lock);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070084 init_rwsem(&nilfs->ns_segctor_sem);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070085
86 return nilfs;
87}
88
89/**
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +090090 * destroy_nilfs - destroy nilfs object
91 * @nilfs: nilfs object to be released
Ryusuke Konishi33c8e572009-06-08 01:39:29 +090092 */
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +090093void destroy_nilfs(struct the_nilfs *nilfs)
Ryusuke Konishi33c8e572009-06-08 01:39:29 +090094{
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070095 might_sleep();
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070096 if (nilfs_init(nilfs)) {
Ryusuke Konishie339ad32009-04-06 19:01:59 -070097 brelse(nilfs->ns_sbh[0]);
98 brelse(nilfs->ns_sbh[1]);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070099 }
100 kfree(nilfs);
101}
102
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900103static int nilfs_load_super_root(struct the_nilfs *nilfs,
104 struct super_block *sb, sector_t sr_block)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700105{
106 struct buffer_head *bh_sr;
107 struct nilfs_super_root *raw_sr;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700108 struct nilfs_super_block **sbp = nilfs->ns_sbp;
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900109 struct nilfs_inode *rawi;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700110 unsigned dat_entry_size, segment_usage_size, checkpoint_size;
111 unsigned inode_size;
112 int err;
113
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900114 err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700115 if (unlikely(err))
116 return err;
117
118 down_read(&nilfs->ns_sem);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700119 dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
120 checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
121 segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700122 up_read(&nilfs->ns_sem);
123
124 inode_size = nilfs->ns_inode_size;
125
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900126 rawi = (void *)bh_sr->b_data + NILFS_SR_DAT_OFFSET(inode_size);
127 err = nilfs_dat_read(sb, dat_entry_size, rawi, &nilfs->ns_dat);
128 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700129 goto failed;
130
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900131 rawi = (void *)bh_sr->b_data + NILFS_SR_CPFILE_OFFSET(inode_size);
132 err = nilfs_cpfile_read(sb, checkpoint_size, rawi, &nilfs->ns_cpfile);
133 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700134 goto failed_dat;
135
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900136 rawi = (void *)bh_sr->b_data + NILFS_SR_SUFILE_OFFSET(inode_size);
137 err = nilfs_sufile_read(sb, segment_usage_size, rawi,
138 &nilfs->ns_sufile);
139 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700140 goto failed_cpfile;
141
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700142 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
143 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
144
145 failed:
146 brelse(bh_sr);
147 return err;
148
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700149 failed_cpfile:
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900150 iput(nilfs->ns_cpfile);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700151
152 failed_dat:
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900153 iput(nilfs->ns_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700154 goto failed;
155}
156
157static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
158{
159 memset(ri, 0, sizeof(*ri));
160 INIT_LIST_HEAD(&ri->ri_used_segments);
161}
162
163static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
164{
165 nilfs_dispose_segment_list(&ri->ri_used_segments);
166}
167
168/**
Ryusuke Konishi843d63ba2010-06-28 19:15:24 +0900169 * nilfs_store_log_cursor - load log cursor from a super block
170 * @nilfs: nilfs object
171 * @sbp: buffer storing super block to be read
172 *
173 * nilfs_store_log_cursor() reads the last position of the log
174 * containing a super root from a given super block, and initializes
175 * relevant information on the nilfs object preparatory for log
176 * scanning and recovery.
177 */
178static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
179 struct nilfs_super_block *sbp)
180{
181 int ret = 0;
182
183 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
184 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
185 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
186
Ryusuke Konishi32502042010-06-29 14:42:13 +0900187 nilfs->ns_prev_seq = nilfs->ns_last_seq;
Ryusuke Konishi843d63ba2010-06-28 19:15:24 +0900188 nilfs->ns_seg_seq = nilfs->ns_last_seq;
189 nilfs->ns_segnum =
190 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
191 nilfs->ns_cno = nilfs->ns_last_cno + 1;
192 if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
193 printk(KERN_ERR "NILFS invalid last segment number.\n");
194 ret = -EINVAL;
195 }
196 return ret;
197}
198
199/**
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700200 * load_nilfs - load and recover the nilfs
201 * @nilfs: the_nilfs structure to be released
202 * @sbi: nilfs_sb_info used to recover past segment
203 *
204 * load_nilfs() searches and load the latest super root,
205 * attaches the last segment, and does recovery if needed.
206 * The caller must call this exclusively for simultaneous mounts.
207 */
208int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi)
209{
210 struct nilfs_recovery_info ri;
211 unsigned int s_flags = sbi->s_super->s_flags;
212 int really_read_only = bdev_read_only(nilfs->ns_bdev);
Ryusuke Konishia057d2c2009-11-19 19:58:46 +0900213 int valid_fs = nilfs_valid_fs(nilfs);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900214 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700215
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900216 if (!valid_fs) {
217 printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
218 if (s_flags & MS_RDONLY) {
219 printk(KERN_INFO "NILFS: INFO: recovery "
220 "required for readonly filesystem.\n");
221 printk(KERN_INFO "NILFS: write access will "
222 "be enabled during recovery.\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700223 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700224 }
225
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900226 nilfs_init_recovery_info(&ri);
227
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900228 err = nilfs_search_super_root(nilfs, &ri);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700229 if (unlikely(err)) {
Ryusuke Konishi6c1251602010-06-28 19:15:26 +0900230 struct nilfs_super_block **sbp = nilfs->ns_sbp;
231 int blocksize;
232
233 if (err != -EINVAL)
234 goto scan_error;
235
236 if (!nilfs_valid_sb(sbp[1])) {
237 printk(KERN_WARNING
238 "NILFS warning: unable to fall back to spare"
239 "super block\n");
240 goto scan_error;
241 }
242 printk(KERN_INFO
243 "NILFS: try rollback from an earlier position\n");
244
245 /*
246 * restore super block with its spare and reconfigure
247 * relevant states of the nilfs object.
248 */
249 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
250 nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
251 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
252
253 /* verify consistency between two super blocks */
254 blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
255 if (blocksize != nilfs->ns_blocksize) {
256 printk(KERN_WARNING
257 "NILFS warning: blocksize differs between "
258 "two super blocks (%d != %d)\n",
259 blocksize, nilfs->ns_blocksize);
260 goto scan_error;
261 }
262
263 err = nilfs_store_log_cursor(nilfs, sbp[0]);
264 if (err)
265 goto scan_error;
266
267 /* drop clean flag to allow roll-forward and recovery */
268 nilfs->ns_mount_state &= ~NILFS_VALID_FS;
269 valid_fs = 0;
270
271 err = nilfs_search_super_root(nilfs, &ri);
272 if (err)
273 goto scan_error;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700274 }
275
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900276 err = nilfs_load_super_root(nilfs, sbi->s_super, ri.ri_super_root);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700277 if (unlikely(err)) {
278 printk(KERN_ERR "NILFS: error loading super root.\n");
279 goto failed;
280 }
281
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900282 if (valid_fs)
283 goto skip_recovery;
284
285 if (s_flags & MS_RDONLY) {
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900286 __u64 features;
287
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900288 if (nilfs_test_opt(nilfs, NORECOVERY)) {
Ryusuke Konishi02345762009-11-20 03:28:01 +0900289 printk(KERN_INFO "NILFS: norecovery option specified. "
290 "skipping roll-forward recovery\n");
291 goto skip_recovery;
292 }
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900293 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
294 ~NILFS_FEATURE_COMPAT_RO_SUPP;
295 if (features) {
296 printk(KERN_ERR "NILFS: couldn't proceed with "
297 "recovery because of unsupported optional "
298 "features (%llx)\n",
299 (unsigned long long)features);
300 err = -EROFS;
301 goto failed_unload;
302 }
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900303 if (really_read_only) {
304 printk(KERN_ERR "NILFS: write access "
305 "unavailable, cannot proceed.\n");
306 err = -EROFS;
307 goto failed_unload;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700308 }
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900309 sbi->s_super->s_flags &= ~MS_RDONLY;
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900310 } else if (nilfs_test_opt(nilfs, NORECOVERY)) {
Ryusuke Konishi02345762009-11-20 03:28:01 +0900311 printk(KERN_ERR "NILFS: recovery cancelled because norecovery "
312 "option was specified for a read/write mount\n");
313 err = -EINVAL;
314 goto failed_unload;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700315 }
316
Ryusuke Konishiaee5ce22010-05-23 12:21:57 +0900317 err = nilfs_salvage_orphan_logs(nilfs, sbi, &ri);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900318 if (err)
319 goto failed_unload;
320
321 down_write(&nilfs->ns_sem);
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900322 nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
323 err = nilfs_cleanup_super(sbi);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900324 up_write(&nilfs->ns_sem);
325
326 if (err) {
327 printk(KERN_ERR "NILFS: failed to update super block. "
328 "recovery unfinished.\n");
329 goto failed_unload;
330 }
331 printk(KERN_INFO "NILFS: recovery complete.\n");
332
333 skip_recovery:
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
Ryusuke Konishi4138ec22011-01-24 00:28:22 +0900480 "NILFS warning: unable to read primary superblock "
481 "(blocksize = %d)\n", blocksize);
482 } else if (!sbp[1]) {
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700483 printk(KERN_WARNING
Ryusuke Konishi4138ec22011-01-24 00:28:22 +0900484 "NILFS warning: unable to read secondary superblock "
485 "(blocksize = %d)\n", blocksize);
486 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700487
Ryusuke Konishi25294d82010-05-01 11:54:21 +0900488 /*
489 * Compare two super blocks and set 1 in swp if the secondary
490 * super block is valid and newer. Otherwise, set 0 in swp.
491 */
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700492 valid[0] = nilfs_valid_sb(sbp[0]);
493 valid[1] = nilfs_valid_sb(sbp[1]);
Ryusuke Konishi25294d82010-05-01 11:54:21 +0900494 swp = valid[1] && (!valid[0] ||
495 le64_to_cpu(sbp[1]->s_last_cno) >
496 le64_to_cpu(sbp[0]->s_last_cno));
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700497
498 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
499 brelse(sbh[1]);
500 sbh[1] = NULL;
501 sbp[1] = NULL;
502 swp = 0;
503 }
504 if (!valid[swp]) {
505 nilfs_release_super_block(nilfs);
506 printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
507 sb->s_id);
508 return -EINVAL;
509 }
510
Ryusuke Konishiea1a16f2010-08-15 20:16:11 +0900511 if (!valid[!swp])
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700512 printk(KERN_WARNING "NILFS warning: broken superblock. "
Ryusuke Konishi4138ec22011-01-24 00:28:22 +0900513 "using spare superblock (blocksize = %d).\n", blocksize);
Ryusuke Konishiea1a16f2010-08-15 20:16:11 +0900514 if (swp)
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700515 nilfs_swap_super_block(nilfs);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700516
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900517 nilfs->ns_sbwcount = 0;
518 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700519 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
520 *sbpp = sbp[0];
521 return 0;
522}
523
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700524/**
525 * init_nilfs - initialize a NILFS instance.
526 * @nilfs: the_nilfs structure
527 * @sbi: nilfs_sb_info
528 * @sb: super block
529 * @data: mount options
530 *
531 * init_nilfs() performs common initialization per block device (e.g.
532 * reading the super block, getting disk layout information, initializing
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900533 * shared fields in the_nilfs).
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700534 *
535 * Return Value: On success, 0 is returned. On error, a negative error
536 * code is returned.
537 */
538int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data)
539{
540 struct super_block *sb = sbi->s_super;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700541 struct nilfs_super_block *sbp;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700542 int blocksize;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700543 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700544
545 down_write(&nilfs->ns_sem);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700546
Ryusuke Konishi89c0fd02010-07-25 22:44:53 +0900547 blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700548 if (!blocksize) {
549 printk(KERN_ERR "NILFS: unable to set blocksize\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700550 err = -EINVAL;
551 goto out;
552 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700553 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
554 if (err)
555 goto out;
556
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700557 err = nilfs_store_magic_and_option(sb, sbp, data);
558 if (err)
559 goto failed_sbh;
560
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900561 err = nilfs_check_feature_compatibility(sb, sbp);
562 if (err)
563 goto failed_sbh;
564
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700565 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
Ryusuke Konishi89c0fd02010-07-25 22:44:53 +0900566 if (blocksize < NILFS_MIN_BLOCK_SIZE ||
567 blocksize > NILFS_MAX_BLOCK_SIZE) {
568 printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
569 "filesystem blocksize %d\n", blocksize);
570 err = -EINVAL;
571 goto failed_sbh;
572 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700573 if (sb->s_blocksize != blocksize) {
Martin K. Petersene1defc42009-05-22 17:17:49 -0400574 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700575
576 if (blocksize < hw_blocksize) {
577 printk(KERN_ERR
578 "NILFS: blocksize %d too small for device "
579 "(sector-size = %d).\n",
580 blocksize, hw_blocksize);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700581 err = -EINVAL;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700582 goto failed_sbh;
583 }
584 nilfs_release_super_block(nilfs);
585 sb_set_blocksize(sb, blocksize);
586
587 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
588 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700589 goto out;
590 /* not failed_sbh; sbh is released automatically
591 when reloading fails. */
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700592 }
593 nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
Ryusuke Konishi92c60cc2010-05-23 00:17:48 +0900594 nilfs->ns_blocksize = blocksize;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700595
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700596 err = nilfs_store_disk_layout(nilfs, sbp);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700597 if (err)
598 goto failed_sbh;
599
600 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
601
602 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700603
Ryusuke Konishi843d63ba2010-06-28 19:15:24 +0900604 err = nilfs_store_log_cursor(nilfs, sbp);
605 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700606 goto failed_sbh;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700607
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700608 set_nilfs_init(nilfs);
609 err = 0;
610 out:
611 up_write(&nilfs->ns_sem);
612 return err;
613
614 failed_sbh:
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700615 nilfs_release_super_block(nilfs);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700616 goto out;
617}
618
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900619int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
620 size_t nsegs)
621{
622 sector_t seg_start, seg_end;
623 sector_t start = 0, nblocks = 0;
624 unsigned int sects_per_block;
625 __u64 *sn;
626 int ret = 0;
627
628 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
629 bdev_logical_block_size(nilfs->ns_bdev);
630 for (sn = segnump; sn < segnump + nsegs; sn++) {
631 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
632
633 if (!nblocks) {
634 start = seg_start;
635 nblocks = seg_end - seg_start + 1;
636 } else if (start + nblocks == seg_start) {
637 nblocks += seg_end - seg_start + 1;
638 } else {
639 ret = blkdev_issue_discard(nilfs->ns_bdev,
640 start * sects_per_block,
641 nblocks * sects_per_block,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200642 GFP_NOFS, 0);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900643 if (ret < 0)
644 return ret;
645 nblocks = 0;
646 }
647 }
648 if (nblocks)
649 ret = blkdev_issue_discard(nilfs->ns_bdev,
650 start * sects_per_block,
651 nblocks * sects_per_block,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200652 GFP_NOFS, 0);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900653 return ret;
654}
655
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700656int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
657{
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700658 unsigned long ncleansegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700659
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900660 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900661 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900662 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900663 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
664 return 0;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700665}
666
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700667int nilfs_near_disk_full(struct the_nilfs *nilfs)
668{
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700669 unsigned long ncleansegs, nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700670
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900671 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
672 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
673 nilfs->ns_blocks_per_segment + 1;
674
675 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700676}
677
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900678struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900679{
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900680 struct rb_node *n;
681 struct nilfs_root *root;
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900682
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900683 spin_lock(&nilfs->ns_cptree_lock);
684 n = nilfs->ns_cptree.rb_node;
685 while (n) {
686 root = rb_entry(n, struct nilfs_root, rb_node);
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900687
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900688 if (cno < root->cno) {
689 n = n->rb_left;
690 } else if (cno > root->cno) {
691 n = n->rb_right;
692 } else {
693 atomic_inc(&root->count);
694 spin_unlock(&nilfs->ns_cptree_lock);
695 return root;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700696 }
697 }
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900698 spin_unlock(&nilfs->ns_cptree_lock);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700699
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900700 return NULL;
701}
702
703struct nilfs_root *
704nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
705{
706 struct rb_node **p, *parent;
707 struct nilfs_root *root, *new;
708
709 root = nilfs_lookup_root(nilfs, cno);
710 if (root)
711 return root;
712
713 new = kmalloc(sizeof(*root), GFP_KERNEL);
714 if (!new)
715 return NULL;
716
717 spin_lock(&nilfs->ns_cptree_lock);
718
719 p = &nilfs->ns_cptree.rb_node;
720 parent = NULL;
721
722 while (*p) {
723 parent = *p;
724 root = rb_entry(parent, struct nilfs_root, rb_node);
725
726 if (cno < root->cno) {
727 p = &(*p)->rb_left;
728 } else if (cno > root->cno) {
729 p = &(*p)->rb_right;
730 } else {
731 atomic_inc(&root->count);
732 spin_unlock(&nilfs->ns_cptree_lock);
733 kfree(new);
734 return root;
735 }
736 }
737
738 new->cno = cno;
739 new->ifile = NULL;
740 new->nilfs = nilfs;
741 atomic_set(&new->count, 1);
742 atomic_set(&new->inodes_count, 0);
743 atomic_set(&new->blocks_count, 0);
744
745 rb_link_node(&new->rb_node, parent, p);
746 rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
747
748 spin_unlock(&nilfs->ns_cptree_lock);
749
750 return new;
751}
752
753void nilfs_put_root(struct nilfs_root *root)
754{
755 if (atomic_dec_and_test(&root->count)) {
756 struct the_nilfs *nilfs = root->nilfs;
757
758 spin_lock(&nilfs->ns_cptree_lock);
759 rb_erase(&root->rb_node, &nilfs->ns_cptree);
760 spin_unlock(&nilfs->ns_cptree_lock);
761 if (root->ifile)
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900762 iput(root->ifile);
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900763
764 kfree(root);
765 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700766}