blob: e9fd241b9a0acd685fe1df0e62adef8839bb54e6 [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 *
Ryusuke Konishi4b420ab2016-05-23 16:23:09 -070016 * Written by Ryusuke Konishi.
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070017 *
18 */
19
20#include <linux/buffer_head.h>
21#include <linux/slab.h>
22#include <linux/blkdev.h>
23#include <linux/backing-dev.h>
Ryusuke Konishi9b1fc4e42011-03-09 11:05:08 +090024#include <linux/random.h>
Ryusuke Konishie339ad32009-04-06 19:01:59 -070025#include <linux/crc32.h>
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070026#include "nilfs.h"
27#include "segment.h"
28#include "alloc.h"
29#include "cpfile.h"
30#include "sufile.h"
31#include "dat.h"
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070032#include "segbuf.h"
33
Ryusuke Konishi33c8e572009-06-08 01:39:29 +090034
Ryusuke Konishi6c1251602010-06-28 19:15:26 +090035static int nilfs_valid_sb(struct nilfs_super_block *sbp);
36
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070037void nilfs_set_last_segment(struct the_nilfs *nilfs,
38 sector_t start_blocknr, u64 seq, __u64 cno)
39{
40 spin_lock(&nilfs->ns_last_segment_lock);
41 nilfs->ns_last_pseg = start_blocknr;
42 nilfs->ns_last_seq = seq;
43 nilfs->ns_last_cno = cno;
Ryusuke Konishi32502042010-06-29 14:42:13 +090044
45 if (!nilfs_sb_dirty(nilfs)) {
46 if (nilfs->ns_prev_seq == nilfs->ns_last_seq)
47 goto stay_cursor;
48
49 set_nilfs_sb_dirty(nilfs);
50 }
51 nilfs->ns_prev_seq = nilfs->ns_last_seq;
52
53 stay_cursor:
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070054 spin_unlock(&nilfs->ns_last_segment_lock);
55}
56
57/**
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +090058 * alloc_nilfs - allocate a nilfs object
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070059 * @bdev: block device to which the_nilfs is related
60 *
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070061 * Return Value: On success, pointer to the_nilfs is returned.
62 * On error, NULL is returned.
63 */
Ryusuke Konishi348fe8d2010-09-09 02:07:56 +090064struct the_nilfs *alloc_nilfs(struct block_device *bdev)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070065{
66 struct the_nilfs *nilfs;
67
68 nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL);
69 if (!nilfs)
70 return NULL;
71
72 nilfs->ns_bdev = bdev;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -070073 atomic_set(&nilfs->ns_ndirtyblks, 0);
74 init_rwsem(&nilfs->ns_sem);
Ryusuke Konishi572d8b32012-07-30 14:42:07 -070075 mutex_init(&nilfs->ns_snapshot_mount_mutex);
Ryusuke Konishi693dd322011-03-09 11:05:07 +090076 INIT_LIST_HEAD(&nilfs->ns_dirty_files);
Ryusuke Konishi263d90c2010-08-20 19:06:11 +090077 INIT_LIST_HEAD(&nilfs->ns_gc_inodes);
Ryusuke Konishi693dd322011-03-09 11:05:07 +090078 spin_lock_init(&nilfs->ns_inode_lock);
Ryusuke Konishi9b1fc4e42011-03-09 11:05:08 +090079 spin_lock_init(&nilfs->ns_next_gen_lock);
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);
Vyacheslav Dubeykocaa05d42014-08-08 14:20:42 -070084 nilfs->ns_sb_update_freq = NILFS_SB_FREQ;
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)) {
Vyacheslav Dubeykodd70edb2014-08-08 14:20:55 -070097 nilfs_sysfs_delete_device_group(nilfs);
Ryusuke Konishie339ad32009-04-06 19:01:59 -070098 brelse(nilfs->ns_sbh[0]);
99 brelse(nilfs->ns_sbh[1]);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700100 }
101 kfree(nilfs);
102}
103
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900104static int nilfs_load_super_root(struct the_nilfs *nilfs,
105 struct super_block *sb, sector_t sr_block)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700106{
107 struct buffer_head *bh_sr;
108 struct nilfs_super_root *raw_sr;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700109 struct nilfs_super_block **sbp = nilfs->ns_sbp;
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900110 struct nilfs_inode *rawi;
Ryusuke Konishi0c6c44c2016-05-23 16:23:39 -0700111 unsigned int dat_entry_size, segment_usage_size, checkpoint_size;
112 unsigned int inode_size;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700113 int err;
114
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900115 err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700116 if (unlikely(err))
117 return err;
118
119 down_read(&nilfs->ns_sem);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700120 dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size);
121 checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size);
122 segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700123 up_read(&nilfs->ns_sem);
124
125 inode_size = nilfs->ns_inode_size;
126
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900127 rawi = (void *)bh_sr->b_data + NILFS_SR_DAT_OFFSET(inode_size);
128 err = nilfs_dat_read(sb, dat_entry_size, rawi, &nilfs->ns_dat);
129 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700130 goto failed;
131
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900132 rawi = (void *)bh_sr->b_data + NILFS_SR_CPFILE_OFFSET(inode_size);
133 err = nilfs_cpfile_read(sb, checkpoint_size, rawi, &nilfs->ns_cpfile);
134 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700135 goto failed_dat;
136
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900137 rawi = (void *)bh_sr->b_data + NILFS_SR_SUFILE_OFFSET(inode_size);
138 err = nilfs_sufile_read(sb, segment_usage_size, rawi,
139 &nilfs->ns_sufile);
140 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700141 goto failed_cpfile;
142
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700143 raw_sr = (struct nilfs_super_root *)bh_sr->b_data;
144 nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime);
145
146 failed:
147 brelse(bh_sr);
148 return err;
149
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700150 failed_cpfile:
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900151 iput(nilfs->ns_cpfile);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700152
153 failed_dat:
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900154 iput(nilfs->ns_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700155 goto failed;
156}
157
158static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri)
159{
160 memset(ri, 0, sizeof(*ri));
161 INIT_LIST_HEAD(&ri->ri_used_segments);
162}
163
164static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri)
165{
166 nilfs_dispose_segment_list(&ri->ri_used_segments);
167}
168
169/**
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900170 * nilfs_store_log_cursor - load log cursor from a super block
171 * @nilfs: nilfs object
172 * @sbp: buffer storing super block to be read
173 *
174 * nilfs_store_log_cursor() reads the last position of the log
175 * containing a super root from a given super block, and initializes
176 * relevant information on the nilfs object preparatory for log
177 * scanning and recovery.
178 */
179static int nilfs_store_log_cursor(struct the_nilfs *nilfs,
180 struct nilfs_super_block *sbp)
181{
182 int ret = 0;
183
184 nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg);
185 nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno);
186 nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq);
187
Ryusuke Konishi32502042010-06-29 14:42:13 +0900188 nilfs->ns_prev_seq = nilfs->ns_last_seq;
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900189 nilfs->ns_seg_seq = nilfs->ns_last_seq;
190 nilfs->ns_segnum =
191 nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg);
192 nilfs->ns_cno = nilfs->ns_last_cno + 1;
193 if (nilfs->ns_segnum >= nilfs->ns_nsegments) {
194 printk(KERN_ERR "NILFS invalid last segment number.\n");
195 ret = -EINVAL;
196 }
197 return ret;
198}
199
200/**
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700201 * load_nilfs - load and recover the nilfs
202 * @nilfs: the_nilfs structure to be released
Ryusuke Konishif7545142011-03-09 11:05:08 +0900203 * @sb: super block isntance used to recover past segment
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700204 *
205 * load_nilfs() searches and load the latest super root,
206 * attaches the last segment, and does recovery if needed.
207 * The caller must call this exclusively for simultaneous mounts.
208 */
Ryusuke Konishif7545142011-03-09 11:05:08 +0900209int load_nilfs(struct the_nilfs *nilfs, struct super_block *sb)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700210{
211 struct nilfs_recovery_info ri;
Ryusuke Konishif7545142011-03-09 11:05:08 +0900212 unsigned int s_flags = sb->s_flags;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700213 int really_read_only = bdev_read_only(nilfs->ns_bdev);
Ryusuke Konishia057d2c2009-11-19 19:58:46 +0900214 int valid_fs = nilfs_valid_fs(nilfs);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900215 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700216
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900217 if (!valid_fs) {
218 printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n");
219 if (s_flags & MS_RDONLY) {
220 printk(KERN_INFO "NILFS: INFO: recovery "
221 "required for readonly filesystem.\n");
222 printk(KERN_INFO "NILFS: write access will "
223 "be enabled during recovery.\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700224 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700225 }
226
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900227 nilfs_init_recovery_info(&ri);
228
Ryusuke Konishi8b940252010-05-23 01:39:02 +0900229 err = nilfs_search_super_root(nilfs, &ri);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700230 if (unlikely(err)) {
Ryusuke Konishi6c1251602010-06-28 19:15:26 +0900231 struct nilfs_super_block **sbp = nilfs->ns_sbp;
232 int blocksize;
233
234 if (err != -EINVAL)
235 goto scan_error;
236
237 if (!nilfs_valid_sb(sbp[1])) {
238 printk(KERN_WARNING
239 "NILFS warning: unable to fall back to spare"
240 "super block\n");
241 goto scan_error;
242 }
243 printk(KERN_INFO
244 "NILFS: try rollback from an earlier position\n");
245
246 /*
247 * restore super block with its spare and reconfigure
248 * relevant states of the nilfs object.
249 */
250 memcpy(sbp[0], sbp[1], nilfs->ns_sbsize);
251 nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed);
252 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
253
254 /* verify consistency between two super blocks */
255 blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size);
256 if (blocksize != nilfs->ns_blocksize) {
257 printk(KERN_WARNING
258 "NILFS warning: blocksize differs between "
259 "two super blocks (%d != %d)\n",
260 blocksize, nilfs->ns_blocksize);
261 goto scan_error;
262 }
263
264 err = nilfs_store_log_cursor(nilfs, sbp[0]);
265 if (err)
266 goto scan_error;
267
268 /* drop clean flag to allow roll-forward and recovery */
269 nilfs->ns_mount_state &= ~NILFS_VALID_FS;
270 valid_fs = 0;
271
272 err = nilfs_search_super_root(nilfs, &ri);
273 if (err)
274 goto scan_error;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700275 }
276
Ryusuke Konishif7545142011-03-09 11:05:08 +0900277 err = nilfs_load_super_root(nilfs, sb, ri.ri_super_root);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700278 if (unlikely(err)) {
279 printk(KERN_ERR "NILFS: error loading super root.\n");
280 goto failed;
281 }
282
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900283 if (valid_fs)
284 goto skip_recovery;
285
286 if (s_flags & MS_RDONLY) {
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900287 __u64 features;
288
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900289 if (nilfs_test_opt(nilfs, NORECOVERY)) {
Ryusuke Konishi02345762009-11-20 03:28:01 +0900290 printk(KERN_INFO "NILFS: norecovery option specified. "
291 "skipping roll-forward recovery\n");
292 goto skip_recovery;
293 }
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900294 features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) &
295 ~NILFS_FEATURE_COMPAT_RO_SUPP;
296 if (features) {
297 printk(KERN_ERR "NILFS: couldn't proceed with "
298 "recovery because of unsupported optional "
299 "features (%llx)\n",
300 (unsigned long long)features);
301 err = -EROFS;
302 goto failed_unload;
303 }
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900304 if (really_read_only) {
305 printk(KERN_ERR "NILFS: write access "
306 "unavailable, cannot proceed.\n");
307 err = -EROFS;
308 goto failed_unload;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700309 }
Ryusuke Konishif7545142011-03-09 11:05:08 +0900310 sb->s_flags &= ~MS_RDONLY;
Ryusuke Konishi3b2ce582011-03-09 11:05:07 +0900311 } else if (nilfs_test_opt(nilfs, NORECOVERY)) {
Ryusuke Konishi02345762009-11-20 03:28:01 +0900312 printk(KERN_ERR "NILFS: recovery cancelled because norecovery "
313 "option was specified for a read/write mount\n");
314 err = -EINVAL;
315 goto failed_unload;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700316 }
317
Ryusuke Konishif7545142011-03-09 11:05:08 +0900318 err = nilfs_salvage_orphan_logs(nilfs, sb, &ri);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900319 if (err)
320 goto failed_unload;
321
322 down_write(&nilfs->ns_sem);
Ryusuke Konishi7ecaa462010-06-28 17:49:29 +0900323 nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */
Ryusuke Konishif7545142011-03-09 11:05:08 +0900324 err = nilfs_cleanup_super(sb);
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900325 up_write(&nilfs->ns_sem);
326
327 if (err) {
328 printk(KERN_ERR "NILFS: failed to update super block. "
329 "recovery unfinished.\n");
330 goto failed_unload;
331 }
332 printk(KERN_INFO "NILFS: recovery complete.\n");
333
334 skip_recovery:
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900335 nilfs_clear_recovery_info(&ri);
Ryusuke Konishif7545142011-03-09 11:05:08 +0900336 sb->s_flags = s_flags;
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900337 return 0;
338
Ryusuke Konishi6c1251602010-06-28 19:15:26 +0900339 scan_error:
340 printk(KERN_ERR "NILFS: error searching super root.\n");
341 goto failed;
342
Ryusuke Konishif50a4c82009-11-19 16:58:40 +0900343 failed_unload:
Ryusuke Konishif1e89c82010-09-05 12:20:59 +0900344 iput(nilfs->ns_cpfile);
345 iput(nilfs->ns_sufile);
346 iput(nilfs->ns_dat);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700347
348 failed:
349 nilfs_clear_recovery_info(&ri);
Ryusuke Konishif7545142011-03-09 11:05:08 +0900350 sb->s_flags = s_flags;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700351 return err;
352}
353
354static unsigned long long nilfs_max_size(unsigned int blkbits)
355{
356 unsigned int max_bits;
357 unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */
358
359 max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */
360 if (max_bits < 64)
361 res = min_t(unsigned long long, res, (1ULL << max_bits) - 1);
362 return res;
363}
364
Ryusuke Konishi4e33f9e2011-05-05 01:23:58 +0900365/**
366 * nilfs_nrsvsegs - calculate the number of reserved segments
367 * @nilfs: nilfs object
368 * @nsegs: total number of segments
369 */
370unsigned long nilfs_nrsvsegs(struct the_nilfs *nilfs, unsigned long nsegs)
371{
372 return max_t(unsigned long, NILFS_MIN_NRSVSEGS,
373 DIV_ROUND_UP(nsegs * nilfs->ns_r_segments_percentage,
374 100));
375}
376
377void nilfs_set_nsegments(struct the_nilfs *nilfs, unsigned long nsegs)
378{
379 nilfs->ns_nsegments = nsegs;
380 nilfs->ns_nrsvsegs = nilfs_nrsvsegs(nilfs, nsegs);
381}
382
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700383static int nilfs_store_disk_layout(struct the_nilfs *nilfs,
384 struct nilfs_super_block *sbp)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700385{
Ryusuke Konishi9566a7a2010-08-10 00:58:41 +0900386 if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) {
387 printk(KERN_ERR "NILFS: unsupported revision "
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700388 "(superblock rev.=%d.%d, current rev.=%d.%d). "
389 "Please check the version of mkfs.nilfs.\n",
390 le32_to_cpu(sbp->s_rev_level),
391 le16_to_cpu(sbp->s_minor_rev_level),
392 NILFS_CURRENT_REV, NILFS_MINOR_REV);
393 return -EINVAL;
394 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700395 nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes);
396 if (nilfs->ns_sbsize > BLOCK_SIZE)
397 return -EINVAL;
398
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700399 nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size);
Ryusuke Konishi0ec060d2014-04-03 14:50:31 -0700400 if (nilfs->ns_inode_size > nilfs->ns_blocksize) {
401 printk(KERN_ERR "NILFS: too large inode size: %d bytes.\n",
402 nilfs->ns_inode_size);
403 return -EINVAL;
404 } else if (nilfs->ns_inode_size < NILFS_MIN_INODE_SIZE) {
405 printk(KERN_ERR "NILFS: too small inode size: %d bytes.\n",
406 nilfs->ns_inode_size);
407 return -EINVAL;
408 }
409
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700410 nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino);
411
412 nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment);
413 if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) {
Ryusuke Konishic91cea12010-03-14 04:01:27 +0900414 printk(KERN_ERR "NILFS: too short segment.\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700415 return -EINVAL;
416 }
417
418 nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700419 nilfs->ns_r_segments_percentage =
420 le32_to_cpu(sbp->s_r_segments_percentage);
Haogang Chen3d777a62012-03-16 17:08:38 -0700421 if (nilfs->ns_r_segments_percentage < 1 ||
422 nilfs->ns_r_segments_percentage > 99) {
423 printk(KERN_ERR "NILFS: invalid reserved segments percentage.\n");
424 return -EINVAL;
425 }
426
Ryusuke Konishi4e33f9e2011-05-05 01:23:58 +0900427 nilfs_set_nsegments(nilfs, le64_to_cpu(sbp->s_nsegments));
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700428 nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed);
429 return 0;
430}
431
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700432static int nilfs_valid_sb(struct nilfs_super_block *sbp)
433{
434 static unsigned char sum[4];
435 const int sumoff = offsetof(struct nilfs_super_block, s_sum);
436 size_t bytes;
437 u32 crc;
438
439 if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC)
440 return 0;
441 bytes = le16_to_cpu(sbp->s_bytes);
Torsten Hilbrich63d2f952016-06-24 14:50:18 -0700442 if (bytes < sumoff + 4 || bytes > BLOCK_SIZE)
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700443 return 0;
444 crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp,
445 sumoff);
446 crc = crc32_le(crc, sum, 4);
447 crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4,
448 bytes - sumoff - 4);
449 return crc == le32_to_cpu(sbp->s_sum);
450}
451
452static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset)
453{
454 return offset < ((le64_to_cpu(sbp->s_nsegments) *
455 le32_to_cpu(sbp->s_blocks_per_segment)) <<
456 (le32_to_cpu(sbp->s_log_block_size) + 10));
457}
458
459static void nilfs_release_super_block(struct the_nilfs *nilfs)
460{
461 int i;
462
463 for (i = 0; i < 2; i++) {
464 if (nilfs->ns_sbp[i]) {
465 brelse(nilfs->ns_sbh[i]);
466 nilfs->ns_sbh[i] = NULL;
467 nilfs->ns_sbp[i] = NULL;
468 }
469 }
470}
471
472void nilfs_fall_back_super_block(struct the_nilfs *nilfs)
473{
474 brelse(nilfs->ns_sbh[0]);
475 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
476 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
477 nilfs->ns_sbh[1] = NULL;
478 nilfs->ns_sbp[1] = NULL;
479}
480
481void nilfs_swap_super_block(struct the_nilfs *nilfs)
482{
483 struct buffer_head *tsbh = nilfs->ns_sbh[0];
484 struct nilfs_super_block *tsbp = nilfs->ns_sbp[0];
485
486 nilfs->ns_sbh[0] = nilfs->ns_sbh[1];
487 nilfs->ns_sbp[0] = nilfs->ns_sbp[1];
488 nilfs->ns_sbh[1] = tsbh;
489 nilfs->ns_sbp[1] = tsbp;
490}
491
492static int nilfs_load_super_block(struct the_nilfs *nilfs,
493 struct super_block *sb, int blocksize,
494 struct nilfs_super_block **sbpp)
495{
496 struct nilfs_super_block **sbp = nilfs->ns_sbp;
497 struct buffer_head **sbh = nilfs->ns_sbh;
498 u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size);
499 int valid[2], swp = 0;
500
501 sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize,
502 &sbh[0]);
503 sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]);
504
505 if (!sbp[0]) {
506 if (!sbp[1]) {
507 printk(KERN_ERR "NILFS: unable to read superblock\n");
508 return -EIO;
509 }
510 printk(KERN_WARNING
Ryusuke Konishi4138ec22011-01-24 00:28:22 +0900511 "NILFS warning: unable to read primary superblock "
512 "(blocksize = %d)\n", blocksize);
513 } else if (!sbp[1]) {
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700514 printk(KERN_WARNING
Ryusuke Konishi4138ec22011-01-24 00:28:22 +0900515 "NILFS warning: unable to read secondary superblock "
516 "(blocksize = %d)\n", blocksize);
517 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700518
Ryusuke Konishi25294d82010-05-01 11:54:21 +0900519 /*
520 * Compare two super blocks and set 1 in swp if the secondary
521 * super block is valid and newer. Otherwise, set 0 in swp.
522 */
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700523 valid[0] = nilfs_valid_sb(sbp[0]);
524 valid[1] = nilfs_valid_sb(sbp[1]);
Ryusuke Konishi25294d82010-05-01 11:54:21 +0900525 swp = valid[1] && (!valid[0] ||
526 le64_to_cpu(sbp[1]->s_last_cno) >
527 le64_to_cpu(sbp[0]->s_last_cno));
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700528
529 if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) {
530 brelse(sbh[1]);
531 sbh[1] = NULL;
532 sbp[1] = NULL;
Ryusuke Konishid7178c72012-03-16 17:08:39 -0700533 valid[1] = 0;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700534 swp = 0;
535 }
536 if (!valid[swp]) {
537 nilfs_release_super_block(nilfs);
538 printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n",
539 sb->s_id);
540 return -EINVAL;
541 }
542
Ryusuke Konishiea1a16f2010-08-15 20:16:11 +0900543 if (!valid[!swp])
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700544 printk(KERN_WARNING "NILFS warning: broken superblock. "
Ryusuke Konishi4138ec22011-01-24 00:28:22 +0900545 "using spare superblock (blocksize = %d).\n", blocksize);
Ryusuke Konishiea1a16f2010-08-15 20:16:11 +0900546 if (swp)
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700547 nilfs_swap_super_block(nilfs);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700548
Jiro SEKIBAb2ac86e2010-06-28 17:49:33 +0900549 nilfs->ns_sbwcount = 0;
550 nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700551 nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq);
552 *sbpp = sbp[0];
553 return 0;
554}
555
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700556/**
557 * init_nilfs - initialize a NILFS instance.
558 * @nilfs: the_nilfs structure
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700559 * @sb: super block
560 * @data: mount options
561 *
562 * init_nilfs() performs common initialization per block device (e.g.
563 * reading the super block, getting disk layout information, initializing
Ryusuke Konishif11459a2010-08-16 01:54:52 +0900564 * shared fields in the_nilfs).
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700565 *
566 * Return Value: On success, 0 is returned. On error, a negative error
567 * code is returned.
568 */
Ryusuke Konishif7545142011-03-09 11:05:08 +0900569int init_nilfs(struct the_nilfs *nilfs, struct super_block *sb, char *data)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700570{
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700571 struct nilfs_super_block *sbp;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700572 int blocksize;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700573 int err;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700574
575 down_write(&nilfs->ns_sem);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700576
Ryusuke Konishi89c0fd02010-07-25 22:44:53 +0900577 blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700578 if (!blocksize) {
579 printk(KERN_ERR "NILFS: unable to set blocksize\n");
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700580 err = -EINVAL;
581 goto out;
582 }
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700583 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
584 if (err)
585 goto out;
586
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700587 err = nilfs_store_magic_and_option(sb, sbp, data);
588 if (err)
589 goto failed_sbh;
590
Ryusuke Konishic5ca48a2010-07-22 03:22:20 +0900591 err = nilfs_check_feature_compatibility(sb, sbp);
592 if (err)
593 goto failed_sbh;
594
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700595 blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size);
Ryusuke Konishi89c0fd02010-07-25 22:44:53 +0900596 if (blocksize < NILFS_MIN_BLOCK_SIZE ||
597 blocksize > NILFS_MAX_BLOCK_SIZE) {
598 printk(KERN_ERR "NILFS: couldn't mount because of unsupported "
599 "filesystem blocksize %d\n", blocksize);
600 err = -EINVAL;
601 goto failed_sbh;
602 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700603 if (sb->s_blocksize != blocksize) {
Martin K. Petersene1defc42009-05-22 17:17:49 -0400604 int hw_blocksize = bdev_logical_block_size(sb->s_bdev);
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700605
606 if (blocksize < hw_blocksize) {
607 printk(KERN_ERR
608 "NILFS: blocksize %d too small for device "
609 "(sector-size = %d).\n",
610 blocksize, hw_blocksize);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700611 err = -EINVAL;
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700612 goto failed_sbh;
613 }
614 nilfs_release_super_block(nilfs);
615 sb_set_blocksize(sb, blocksize);
616
617 err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp);
618 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700619 goto out;
Ryusuke Konishi076a3782016-05-23 16:23:48 -0700620 /*
621 * Not to failed_sbh; sbh is released automatically
622 * when reloading fails.
623 */
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700624 }
625 nilfs->ns_blocksize_bits = sb->s_blocksize_bits;
Ryusuke Konishi92c60cc2010-05-23 00:17:48 +0900626 nilfs->ns_blocksize = blocksize;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700627
Ryusuke Konishi9b1fc4e42011-03-09 11:05:08 +0900628 get_random_bytes(&nilfs->ns_next_generation,
629 sizeof(nilfs->ns_next_generation));
630
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700631 err = nilfs_store_disk_layout(nilfs, sbp);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700632 if (err)
633 goto failed_sbh;
634
635 sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits);
636
637 nilfs->ns_mount_state = le16_to_cpu(sbp->s_state);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700638
Ryusuke Konishi843d63b2010-06-28 19:15:24 +0900639 err = nilfs_store_log_cursor(nilfs, sbp);
640 if (err)
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700641 goto failed_sbh;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700642
Vyacheslav Dubeykodd70edb2014-08-08 14:20:55 -0700643 err = nilfs_sysfs_create_device_group(sb);
644 if (err)
645 goto failed_sbh;
646
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700647 set_nilfs_init(nilfs);
648 err = 0;
649 out:
650 up_write(&nilfs->ns_sem);
651 return err;
652
653 failed_sbh:
Ryusuke Konishie339ad32009-04-06 19:01:59 -0700654 nilfs_release_super_block(nilfs);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700655 goto out;
656}
657
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900658int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump,
659 size_t nsegs)
660{
661 sector_t seg_start, seg_end;
662 sector_t start = 0, nblocks = 0;
663 unsigned int sects_per_block;
664 __u64 *sn;
665 int ret = 0;
666
667 sects_per_block = (1 << nilfs->ns_blocksize_bits) /
668 bdev_logical_block_size(nilfs->ns_bdev);
669 for (sn = segnump; sn < segnump + nsegs; sn++) {
670 nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end);
671
672 if (!nblocks) {
673 start = seg_start;
674 nblocks = seg_end - seg_start + 1;
675 } else if (start + nblocks == seg_start) {
676 nblocks += seg_end - seg_start + 1;
677 } else {
678 ret = blkdev_issue_discard(nilfs->ns_bdev,
679 start * sects_per_block,
680 nblocks * sects_per_block,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200681 GFP_NOFS, 0);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900682 if (ret < 0)
683 return ret;
684 nblocks = 0;
685 }
686 }
687 if (nblocks)
688 ret = blkdev_issue_discard(nilfs->ns_bdev,
689 start * sects_per_block,
690 nblocks * sects_per_block,
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200691 GFP_NOFS, 0);
Jiro SEKIBAe902ec92010-01-30 18:06:35 +0900692 return ret;
693}
694
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700695int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks)
696{
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700697 unsigned long ncleansegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700698
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900699 down_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900700 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
Ryusuke Konishi365e2152010-12-27 00:07:30 +0900701 up_read(&NILFS_MDT(nilfs->ns_dat)->mi_sem);
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900702 *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment;
703 return 0;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700704}
705
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700706int nilfs_near_disk_full(struct the_nilfs *nilfs)
707{
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700708 unsigned long ncleansegs, nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700709
Ryusuke Konishief7d4752009-11-13 08:45:32 +0900710 ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile);
711 nincsegs = atomic_read(&nilfs->ns_ndirtyblks) /
712 nilfs->ns_blocks_per_segment + 1;
713
714 return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700715}
716
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900717struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno)
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900718{
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900719 struct rb_node *n;
720 struct nilfs_root *root;
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900721
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900722 spin_lock(&nilfs->ns_cptree_lock);
723 n = nilfs->ns_cptree.rb_node;
724 while (n) {
725 root = rb_entry(n, struct nilfs_root, rb_node);
Ryusuke Konishi6dd47402009-06-08 01:39:31 +0900726
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900727 if (cno < root->cno) {
728 n = n->rb_left;
729 } else if (cno > root->cno) {
730 n = n->rb_right;
731 } else {
732 atomic_inc(&root->count);
733 spin_unlock(&nilfs->ns_cptree_lock);
734 return root;
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700735 }
736 }
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900737 spin_unlock(&nilfs->ns_cptree_lock);
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700738
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900739 return NULL;
740}
741
742struct nilfs_root *
743nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno)
744{
745 struct rb_node **p, *parent;
746 struct nilfs_root *root, *new;
Vyacheslav Dubeykodd70edb2014-08-08 14:20:55 -0700747 int err;
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900748
749 root = nilfs_lookup_root(nilfs, cno);
750 if (root)
751 return root;
752
Vyacheslav Dubeykodd70edb2014-08-08 14:20:55 -0700753 new = kzalloc(sizeof(*root), GFP_KERNEL);
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900754 if (!new)
755 return NULL;
756
757 spin_lock(&nilfs->ns_cptree_lock);
758
759 p = &nilfs->ns_cptree.rb_node;
760 parent = NULL;
761
762 while (*p) {
763 parent = *p;
764 root = rb_entry(parent, struct nilfs_root, rb_node);
765
766 if (cno < root->cno) {
767 p = &(*p)->rb_left;
768 } else if (cno > root->cno) {
769 p = &(*p)->rb_right;
770 } else {
771 atomic_inc(&root->count);
772 spin_unlock(&nilfs->ns_cptree_lock);
773 kfree(new);
774 return root;
775 }
776 }
777
778 new->cno = cno;
779 new->ifile = NULL;
780 new->nilfs = nilfs;
781 atomic_set(&new->count, 1);
Vyacheslav Dubeykoe5f7f842013-07-03 15:08:06 -0700782 atomic64_set(&new->inodes_count, 0);
783 atomic64_set(&new->blocks_count, 0);
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900784
785 rb_link_node(&new->rb_node, parent, p);
786 rb_insert_color(&new->rb_node, &nilfs->ns_cptree);
787
788 spin_unlock(&nilfs->ns_cptree_lock);
789
Vyacheslav Dubeykodd70edb2014-08-08 14:20:55 -0700790 err = nilfs_sysfs_create_snapshot_group(new);
791 if (err) {
792 kfree(new);
793 new = NULL;
794 }
795
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900796 return new;
797}
798
799void nilfs_put_root(struct nilfs_root *root)
800{
801 if (atomic_dec_and_test(&root->count)) {
802 struct the_nilfs *nilfs = root->nilfs;
803
Vyacheslav Dubeykodd70edb2014-08-08 14:20:55 -0700804 nilfs_sysfs_delete_snapshot_group(root);
805
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900806 spin_lock(&nilfs->ns_cptree_lock);
807 rb_erase(&root->rb_node, &nilfs->ns_cptree);
808 spin_unlock(&nilfs->ns_cptree_lock);
Markus Elfring72b99182014-12-10 15:54:31 -0800809 iput(root->ifile);
Ryusuke Konishiba65ae42010-08-14 12:59:15 +0900810
811 kfree(root);
812 }
Ryusuke Konishi8a9d2192009-04-06 19:01:35 -0700813}