]>
Commit | Line | Data |
---|---|---|
8a9d2191 RK |
1 | /* |
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> | |
e339ad31 | 28 | #include <linux/crc32.h> |
8a9d2191 RK |
29 | #include "nilfs.h" |
30 | #include "segment.h" | |
31 | #include "alloc.h" | |
32 | #include "cpfile.h" | |
33 | #include "sufile.h" | |
34 | #include "dat.h" | |
8a9d2191 RK |
35 | #include "segbuf.h" |
36 | ||
33c8e57c RK |
37 | |
38 | static LIST_HEAD(nilfs_objects); | |
39 | static DEFINE_SPINLOCK(nilfs_lock); | |
40 | ||
6c125160 RK |
41 | static int nilfs_valid_sb(struct nilfs_super_block *sbp); |
42 | ||
8a9d2191 RK |
43 | void nilfs_set_last_segment(struct the_nilfs *nilfs, |
44 | sector_t start_blocknr, u64 seq, __u64 cno) | |
45 | { | |
46 | spin_lock(&nilfs->ns_last_segment_lock); | |
47 | nilfs->ns_last_pseg = start_blocknr; | |
48 | nilfs->ns_last_seq = seq; | |
49 | nilfs->ns_last_cno = cno; | |
32502047 RK |
50 | |
51 | if (!nilfs_sb_dirty(nilfs)) { | |
52 | if (nilfs->ns_prev_seq == nilfs->ns_last_seq) | |
53 | goto stay_cursor; | |
54 | ||
55 | set_nilfs_sb_dirty(nilfs); | |
56 | } | |
57 | nilfs->ns_prev_seq = nilfs->ns_last_seq; | |
58 | ||
59 | stay_cursor: | |
8a9d2191 RK |
60 | spin_unlock(&nilfs->ns_last_segment_lock); |
61 | } | |
62 | ||
63 | /** | |
64 | * alloc_nilfs - allocate the_nilfs structure | |
65 | * @bdev: block device to which the_nilfs is related | |
66 | * | |
67 | * alloc_nilfs() allocates memory for the_nilfs and | |
68 | * initializes its reference count and locks. | |
69 | * | |
70 | * Return Value: On success, pointer to the_nilfs is returned. | |
71 | * On error, NULL is returned. | |
72 | */ | |
33c8e57c | 73 | static struct the_nilfs *alloc_nilfs(struct block_device *bdev) |
8a9d2191 RK |
74 | { |
75 | struct the_nilfs *nilfs; | |
76 | ||
77 | nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL); | |
78 | if (!nilfs) | |
79 | return NULL; | |
80 | ||
81 | nilfs->ns_bdev = bdev; | |
82 | atomic_set(&nilfs->ns_count, 1); | |
8a9d2191 RK |
83 | atomic_set(&nilfs->ns_ndirtyblks, 0); |
84 | init_rwsem(&nilfs->ns_sem); | |
e59399d0 | 85 | init_rwsem(&nilfs->ns_super_sem); |
aa7dfb89 | 86 | mutex_init(&nilfs->ns_mount_mutex); |
027d6404 | 87 | init_rwsem(&nilfs->ns_writer_sem); |
33c8e57c | 88 | INIT_LIST_HEAD(&nilfs->ns_list); |
8a9d2191 | 89 | INIT_LIST_HEAD(&nilfs->ns_supers); |
263d90ce | 90 | INIT_LIST_HEAD(&nilfs->ns_gc_inodes); |
8a9d2191 | 91 | spin_lock_init(&nilfs->ns_last_segment_lock); |
ba65ae47 RK |
92 | nilfs->ns_cptree = RB_ROOT; |
93 | spin_lock_init(&nilfs->ns_cptree_lock); | |
8a9d2191 | 94 | init_rwsem(&nilfs->ns_segctor_sem); |
8a9d2191 RK |
95 | |
96 | return nilfs; | |
97 | } | |
98 | ||
33c8e57c RK |
99 | /** |
100 | * find_or_create_nilfs - find or create nilfs object | |
101 | * @bdev: block device to which the_nilfs is related | |
102 | * | |
103 | * find_nilfs() looks up an existent nilfs object created on the | |
104 | * device and gets the reference count of the object. If no nilfs object | |
105 | * is found on the device, a new nilfs object is allocated. | |
106 | * | |
107 | * Return Value: On success, pointer to the nilfs object is returned. | |
108 | * On error, NULL is returned. | |
109 | */ | |
110 | struct the_nilfs *find_or_create_nilfs(struct block_device *bdev) | |
111 | { | |
112 | struct the_nilfs *nilfs, *new = NULL; | |
113 | ||
114 | retry: | |
115 | spin_lock(&nilfs_lock); | |
116 | list_for_each_entry(nilfs, &nilfs_objects, ns_list) { | |
117 | if (nilfs->ns_bdev == bdev) { | |
118 | get_nilfs(nilfs); | |
119 | spin_unlock(&nilfs_lock); | |
120 | if (new) | |
121 | put_nilfs(new); | |
122 | return nilfs; /* existing object */ | |
123 | } | |
124 | } | |
125 | if (new) { | |
126 | list_add_tail(&new->ns_list, &nilfs_objects); | |
127 | spin_unlock(&nilfs_lock); | |
128 | return new; /* new object */ | |
129 | } | |
130 | spin_unlock(&nilfs_lock); | |
131 | ||
132 | new = alloc_nilfs(bdev); | |
133 | if (new) | |
134 | goto retry; | |
135 | return NULL; /* insufficient memory */ | |
136 | } | |
137 | ||
8a9d2191 RK |
138 | /** |
139 | * put_nilfs - release a reference to the_nilfs | |
140 | * @nilfs: the_nilfs structure to be released | |
141 | * | |
142 | * put_nilfs() decrements a reference counter of the_nilfs. | |
143 | * If the reference count reaches zero, the_nilfs is freed. | |
144 | */ | |
145 | void put_nilfs(struct the_nilfs *nilfs) | |
146 | { | |
33c8e57c RK |
147 | spin_lock(&nilfs_lock); |
148 | if (!atomic_dec_and_test(&nilfs->ns_count)) { | |
149 | spin_unlock(&nilfs_lock); | |
8a9d2191 | 150 | return; |
33c8e57c RK |
151 | } |
152 | list_del_init(&nilfs->ns_list); | |
153 | spin_unlock(&nilfs_lock); | |
154 | ||
8a9d2191 | 155 | /* |
33c8e57c | 156 | * Increment of ns_count never occurs below because the caller |
8a9d2191 RK |
157 | * of get_nilfs() holds at least one reference to the_nilfs. |
158 | * Thus its exclusion control is not required here. | |
159 | */ | |
33c8e57c | 160 | |
8a9d2191 RK |
161 | might_sleep(); |
162 | if (nilfs_loaded(nilfs)) { | |
8a9d2191 | 163 | nilfs_mdt_destroy(nilfs->ns_sufile); |
8a9d2191 | 164 | nilfs_mdt_destroy(nilfs->ns_cpfile); |
8a9d2191 | 165 | nilfs_mdt_destroy(nilfs->ns_dat); |
8a9d2191 RK |
166 | nilfs_mdt_destroy(nilfs->ns_gc_dat); |
167 | } | |
168 | if (nilfs_init(nilfs)) { | |
e339ad31 RK |
169 | brelse(nilfs->ns_sbh[0]); |
170 | brelse(nilfs->ns_sbh[1]); | |
8a9d2191 RK |
171 | } |
172 | kfree(nilfs); | |
173 | } | |
174 | ||
8b94025c | 175 | static int nilfs_load_super_root(struct the_nilfs *nilfs, sector_t sr_block) |
8a9d2191 RK |
176 | { |
177 | struct buffer_head *bh_sr; | |
178 | struct nilfs_super_root *raw_sr; | |
e339ad31 | 179 | struct nilfs_super_block **sbp = nilfs->ns_sbp; |
8a9d2191 RK |
180 | unsigned dat_entry_size, segment_usage_size, checkpoint_size; |
181 | unsigned inode_size; | |
182 | int err; | |
183 | ||
8b94025c | 184 | err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1); |
8a9d2191 RK |
185 | if (unlikely(err)) |
186 | return err; | |
187 | ||
188 | down_read(&nilfs->ns_sem); | |
e339ad31 RK |
189 | dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size); |
190 | checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size); | |
191 | segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size); | |
8a9d2191 RK |
192 | up_read(&nilfs->ns_sem); |
193 | ||
194 | inode_size = nilfs->ns_inode_size; | |
195 | ||
196 | err = -ENOMEM; | |
79739565 | 197 | nilfs->ns_dat = nilfs_dat_new(nilfs, dat_entry_size); |
8a9d2191 RK |
198 | if (unlikely(!nilfs->ns_dat)) |
199 | goto failed; | |
200 | ||
79739565 | 201 | nilfs->ns_gc_dat = nilfs_dat_new(nilfs, dat_entry_size); |
8a9d2191 RK |
202 | if (unlikely(!nilfs->ns_gc_dat)) |
203 | goto failed_dat; | |
204 | ||
79739565 | 205 | nilfs->ns_cpfile = nilfs_cpfile_new(nilfs, checkpoint_size); |
8a9d2191 RK |
206 | if (unlikely(!nilfs->ns_cpfile)) |
207 | goto failed_gc_dat; | |
208 | ||
79739565 | 209 | nilfs->ns_sufile = nilfs_sufile_new(nilfs, segment_usage_size); |
8a9d2191 RK |
210 | if (unlikely(!nilfs->ns_sufile)) |
211 | goto failed_cpfile; | |
212 | ||
8a9d2191 | 213 | nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat); |
8a9d2191 | 214 | |
8707df38 RK |
215 | err = nilfs_dat_read(nilfs->ns_dat, (void *)bh_sr->b_data + |
216 | NILFS_SR_DAT_OFFSET(inode_size)); | |
8a9d2191 RK |
217 | if (unlikely(err)) |
218 | goto failed_sufile; | |
219 | ||
8707df38 RK |
220 | err = nilfs_cpfile_read(nilfs->ns_cpfile, (void *)bh_sr->b_data + |
221 | NILFS_SR_CPFILE_OFFSET(inode_size)); | |
8a9d2191 RK |
222 | if (unlikely(err)) |
223 | goto failed_sufile; | |
224 | ||
8707df38 RK |
225 | err = nilfs_sufile_read(nilfs->ns_sufile, (void *)bh_sr->b_data + |
226 | NILFS_SR_SUFILE_OFFSET(inode_size)); | |
8a9d2191 RK |
227 | if (unlikely(err)) |
228 | goto failed_sufile; | |
229 | ||
230 | raw_sr = (struct nilfs_super_root *)bh_sr->b_data; | |
231 | nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime); | |
232 | ||
233 | failed: | |
234 | brelse(bh_sr); | |
235 | return err; | |
236 | ||
237 | failed_sufile: | |
238 | nilfs_mdt_destroy(nilfs->ns_sufile); | |
239 | ||
240 | failed_cpfile: | |
241 | nilfs_mdt_destroy(nilfs->ns_cpfile); | |
242 | ||
243 | failed_gc_dat: | |
244 | nilfs_mdt_destroy(nilfs->ns_gc_dat); | |
245 | ||
246 | failed_dat: | |
247 | nilfs_mdt_destroy(nilfs->ns_dat); | |
248 | goto failed; | |
249 | } | |
250 | ||
251 | static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri) | |
252 | { | |
253 | memset(ri, 0, sizeof(*ri)); | |
254 | INIT_LIST_HEAD(&ri->ri_used_segments); | |
255 | } | |
256 | ||
257 | static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri) | |
258 | { | |
259 | nilfs_dispose_segment_list(&ri->ri_used_segments); | |
260 | } | |
261 | ||
843d63ba RK |
262 | /** |
263 | * nilfs_store_log_cursor - load log cursor from a super block | |
264 | * @nilfs: nilfs object | |
265 | * @sbp: buffer storing super block to be read | |
266 | * | |
267 | * nilfs_store_log_cursor() reads the last position of the log | |
268 | * containing a super root from a given super block, and initializes | |
269 | * relevant information on the nilfs object preparatory for log | |
270 | * scanning and recovery. | |
271 | */ | |
272 | static int nilfs_store_log_cursor(struct the_nilfs *nilfs, | |
273 | struct nilfs_super_block *sbp) | |
274 | { | |
275 | int ret = 0; | |
276 | ||
277 | nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg); | |
278 | nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno); | |
279 | nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq); | |
280 | ||
32502047 | 281 | nilfs->ns_prev_seq = nilfs->ns_last_seq; |
843d63ba RK |
282 | nilfs->ns_seg_seq = nilfs->ns_last_seq; |
283 | nilfs->ns_segnum = | |
284 | nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg); | |
285 | nilfs->ns_cno = nilfs->ns_last_cno + 1; | |
286 | if (nilfs->ns_segnum >= nilfs->ns_nsegments) { | |
287 | printk(KERN_ERR "NILFS invalid last segment number.\n"); | |
288 | ret = -EINVAL; | |
289 | } | |
290 | return ret; | |
291 | } | |
292 | ||
8a9d2191 RK |
293 | /** |
294 | * load_nilfs - load and recover the nilfs | |
295 | * @nilfs: the_nilfs structure to be released | |
296 | * @sbi: nilfs_sb_info used to recover past segment | |
297 | * | |
298 | * load_nilfs() searches and load the latest super root, | |
299 | * attaches the last segment, and does recovery if needed. | |
300 | * The caller must call this exclusively for simultaneous mounts. | |
301 | */ | |
302 | int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi) | |
303 | { | |
304 | struct nilfs_recovery_info ri; | |
305 | unsigned int s_flags = sbi->s_super->s_flags; | |
306 | int really_read_only = bdev_read_only(nilfs->ns_bdev); | |
a057d2c0 | 307 | int valid_fs = nilfs_valid_fs(nilfs); |
f50a4c81 | 308 | int err; |
8a9d2191 | 309 | |
0234576d RK |
310 | if (nilfs_loaded(nilfs)) { |
311 | if (valid_fs || | |
312 | ((s_flags & MS_RDONLY) && nilfs_test_opt(sbi, NORECOVERY))) | |
313 | return 0; | |
314 | printk(KERN_ERR "NILFS: the filesystem is in an incomplete " | |
315 | "recovery state.\n"); | |
316 | return -EINVAL; | |
317 | } | |
8a9d2191 | 318 | |
f50a4c81 RK |
319 | if (!valid_fs) { |
320 | printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n"); | |
321 | if (s_flags & MS_RDONLY) { | |
322 | printk(KERN_INFO "NILFS: INFO: recovery " | |
323 | "required for readonly filesystem.\n"); | |
324 | printk(KERN_INFO "NILFS: write access will " | |
325 | "be enabled during recovery.\n"); | |
8a9d2191 | 326 | } |
8a9d2191 RK |
327 | } |
328 | ||
f50a4c81 RK |
329 | nilfs_init_recovery_info(&ri); |
330 | ||
8b94025c | 331 | err = nilfs_search_super_root(nilfs, &ri); |
8a9d2191 | 332 | if (unlikely(err)) { |
6c125160 RK |
333 | struct nilfs_super_block **sbp = nilfs->ns_sbp; |
334 | int blocksize; | |
335 | ||
336 | if (err != -EINVAL) | |
337 | goto scan_error; | |
338 | ||
339 | if (!nilfs_valid_sb(sbp[1])) { | |
340 | printk(KERN_WARNING | |
341 | "NILFS warning: unable to fall back to spare" | |
342 | "super block\n"); | |
343 | goto scan_error; | |
344 | } | |
345 | printk(KERN_INFO | |
346 | "NILFS: try rollback from an earlier position\n"); | |
347 | ||
348 | /* | |
349 | * restore super block with its spare and reconfigure | |
350 | * relevant states of the nilfs object. | |
351 | */ | |
352 | memcpy(sbp[0], sbp[1], nilfs->ns_sbsize); | |
353 | nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed); | |
354 | nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime); | |
355 | ||
356 | /* verify consistency between two super blocks */ | |
357 | blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size); | |
358 | if (blocksize != nilfs->ns_blocksize) { | |
359 | printk(KERN_WARNING | |
360 | "NILFS warning: blocksize differs between " | |
361 | "two super blocks (%d != %d)\n", | |
362 | blocksize, nilfs->ns_blocksize); | |
363 | goto scan_error; | |
364 | } | |
365 | ||
366 | err = nilfs_store_log_cursor(nilfs, sbp[0]); | |
367 | if (err) | |
368 | goto scan_error; | |
369 | ||
370 | /* drop clean flag to allow roll-forward and recovery */ | |
371 | nilfs->ns_mount_state &= ~NILFS_VALID_FS; | |
372 | valid_fs = 0; | |
373 | ||
374 | err = nilfs_search_super_root(nilfs, &ri); | |
375 | if (err) | |
376 | goto scan_error; | |
8a9d2191 RK |
377 | } |
378 | ||
8b94025c | 379 | err = nilfs_load_super_root(nilfs, ri.ri_super_root); |
8a9d2191 RK |
380 | if (unlikely(err)) { |
381 | printk(KERN_ERR "NILFS: error loading super root.\n"); | |
382 | goto failed; | |
383 | } | |
384 | ||
f50a4c81 RK |
385 | if (valid_fs) |
386 | goto skip_recovery; | |
387 | ||
388 | if (s_flags & MS_RDONLY) { | |
c5ca48aa RK |
389 | __u64 features; |
390 | ||
0234576d RK |
391 | if (nilfs_test_opt(sbi, NORECOVERY)) { |
392 | printk(KERN_INFO "NILFS: norecovery option specified. " | |
393 | "skipping roll-forward recovery\n"); | |
394 | goto skip_recovery; | |
395 | } | |
c5ca48aa RK |
396 | features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) & |
397 | ~NILFS_FEATURE_COMPAT_RO_SUPP; | |
398 | if (features) { | |
399 | printk(KERN_ERR "NILFS: couldn't proceed with " | |
400 | "recovery because of unsupported optional " | |
401 | "features (%llx)\n", | |
402 | (unsigned long long)features); | |
403 | err = -EROFS; | |
404 | goto failed_unload; | |
405 | } | |
f50a4c81 RK |
406 | if (really_read_only) { |
407 | printk(KERN_ERR "NILFS: write access " | |
408 | "unavailable, cannot proceed.\n"); | |
409 | err = -EROFS; | |
410 | goto failed_unload; | |
8a9d2191 | 411 | } |
f50a4c81 | 412 | sbi->s_super->s_flags &= ~MS_RDONLY; |
0234576d RK |
413 | } else if (nilfs_test_opt(sbi, NORECOVERY)) { |
414 | printk(KERN_ERR "NILFS: recovery cancelled because norecovery " | |
415 | "option was specified for a read/write mount\n"); | |
416 | err = -EINVAL; | |
417 | goto failed_unload; | |
f50a4c81 RK |
418 | } |
419 | ||
aee5ce2f | 420 | err = nilfs_salvage_orphan_logs(nilfs, sbi, &ri); |
f50a4c81 RK |
421 | if (err) |
422 | goto failed_unload; | |
423 | ||
424 | down_write(&nilfs->ns_sem); | |
7ecaa46c RK |
425 | nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */ |
426 | err = nilfs_cleanup_super(sbi); | |
f50a4c81 RK |
427 | up_write(&nilfs->ns_sem); |
428 | ||
429 | if (err) { | |
430 | printk(KERN_ERR "NILFS: failed to update super block. " | |
431 | "recovery unfinished.\n"); | |
432 | goto failed_unload; | |
8a9d2191 | 433 | } |
f50a4c81 | 434 | printk(KERN_INFO "NILFS: recovery complete.\n"); |
8a9d2191 | 435 | |
f50a4c81 | 436 | skip_recovery: |
8a9d2191 | 437 | set_nilfs_loaded(nilfs); |
f50a4c81 RK |
438 | nilfs_clear_recovery_info(&ri); |
439 | sbi->s_super->s_flags = s_flags; | |
440 | return 0; | |
441 | ||
6c125160 RK |
442 | scan_error: |
443 | printk(KERN_ERR "NILFS: error searching super root.\n"); | |
444 | goto failed; | |
445 | ||
f50a4c81 RK |
446 | failed_unload: |
447 | nilfs_mdt_destroy(nilfs->ns_cpfile); | |
448 | nilfs_mdt_destroy(nilfs->ns_sufile); | |
449 | nilfs_mdt_destroy(nilfs->ns_dat); | |
4afc3134 | 450 | nilfs_mdt_destroy(nilfs->ns_gc_dat); |
8a9d2191 RK |
451 | |
452 | failed: | |
453 | nilfs_clear_recovery_info(&ri); | |
454 | sbi->s_super->s_flags = s_flags; | |
455 | return err; | |
456 | } | |
457 | ||
458 | static unsigned long long nilfs_max_size(unsigned int blkbits) | |
459 | { | |
460 | unsigned int max_bits; | |
461 | unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */ | |
462 | ||
463 | max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */ | |
464 | if (max_bits < 64) | |
465 | res = min_t(unsigned long long, res, (1ULL << max_bits) - 1); | |
466 | return res; | |
467 | } | |
468 | ||
e339ad31 RK |
469 | static int nilfs_store_disk_layout(struct the_nilfs *nilfs, |
470 | struct nilfs_super_block *sbp) | |
8a9d2191 | 471 | { |
9566a7a8 RK |
472 | if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) { |
473 | printk(KERN_ERR "NILFS: unsupported revision " | |
8a9d2191 RK |
474 | "(superblock rev.=%d.%d, current rev.=%d.%d). " |
475 | "Please check the version of mkfs.nilfs.\n", | |
476 | le32_to_cpu(sbp->s_rev_level), | |
477 | le16_to_cpu(sbp->s_minor_rev_level), | |
478 | NILFS_CURRENT_REV, NILFS_MINOR_REV); | |
479 | return -EINVAL; | |
480 | } | |
e339ad31 RK |
481 | nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes); |
482 | if (nilfs->ns_sbsize > BLOCK_SIZE) | |
483 | return -EINVAL; | |
484 | ||
8a9d2191 RK |
485 | nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size); |
486 | nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino); | |
487 | ||
488 | nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment); | |
489 | if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) { | |
c91cea11 | 490 | printk(KERN_ERR "NILFS: too short segment.\n"); |
8a9d2191 RK |
491 | return -EINVAL; |
492 | } | |
493 | ||
494 | nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block); | |
495 | nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments); | |
496 | nilfs->ns_r_segments_percentage = | |
497 | le32_to_cpu(sbp->s_r_segments_percentage); | |
498 | nilfs->ns_nrsvsegs = | |
499 | max_t(unsigned long, NILFS_MIN_NRSVSEGS, | |
500 | DIV_ROUND_UP(nilfs->ns_nsegments * | |
501 | nilfs->ns_r_segments_percentage, 100)); | |
502 | nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed); | |
503 | return 0; | |
504 | } | |
505 | ||
e339ad31 RK |
506 | static int nilfs_valid_sb(struct nilfs_super_block *sbp) |
507 | { | |
508 | static unsigned char sum[4]; | |
509 | const int sumoff = offsetof(struct nilfs_super_block, s_sum); | |
510 | size_t bytes; | |
511 | u32 crc; | |
512 | ||
513 | if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC) | |
514 | return 0; | |
515 | bytes = le16_to_cpu(sbp->s_bytes); | |
516 | if (bytes > BLOCK_SIZE) | |
517 | return 0; | |
518 | crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp, | |
519 | sumoff); | |
520 | crc = crc32_le(crc, sum, 4); | |
521 | crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4, | |
522 | bytes - sumoff - 4); | |
523 | return crc == le32_to_cpu(sbp->s_sum); | |
524 | } | |
525 | ||
526 | static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset) | |
527 | { | |
528 | return offset < ((le64_to_cpu(sbp->s_nsegments) * | |
529 | le32_to_cpu(sbp->s_blocks_per_segment)) << | |
530 | (le32_to_cpu(sbp->s_log_block_size) + 10)); | |
531 | } | |
532 | ||
533 | static void nilfs_release_super_block(struct the_nilfs *nilfs) | |
534 | { | |
535 | int i; | |
536 | ||
537 | for (i = 0; i < 2; i++) { | |
538 | if (nilfs->ns_sbp[i]) { | |
539 | brelse(nilfs->ns_sbh[i]); | |
540 | nilfs->ns_sbh[i] = NULL; | |
541 | nilfs->ns_sbp[i] = NULL; | |
542 | } | |
543 | } | |
544 | } | |
545 | ||
546 | void nilfs_fall_back_super_block(struct the_nilfs *nilfs) | |
547 | { | |
548 | brelse(nilfs->ns_sbh[0]); | |
549 | nilfs->ns_sbh[0] = nilfs->ns_sbh[1]; | |
550 | nilfs->ns_sbp[0] = nilfs->ns_sbp[1]; | |
551 | nilfs->ns_sbh[1] = NULL; | |
552 | nilfs->ns_sbp[1] = NULL; | |
553 | } | |
554 | ||
555 | void nilfs_swap_super_block(struct the_nilfs *nilfs) | |
556 | { | |
557 | struct buffer_head *tsbh = nilfs->ns_sbh[0]; | |
558 | struct nilfs_super_block *tsbp = nilfs->ns_sbp[0]; | |
559 | ||
560 | nilfs->ns_sbh[0] = nilfs->ns_sbh[1]; | |
561 | nilfs->ns_sbp[0] = nilfs->ns_sbp[1]; | |
562 | nilfs->ns_sbh[1] = tsbh; | |
563 | nilfs->ns_sbp[1] = tsbp; | |
564 | } | |
565 | ||
566 | static int nilfs_load_super_block(struct the_nilfs *nilfs, | |
567 | struct super_block *sb, int blocksize, | |
568 | struct nilfs_super_block **sbpp) | |
569 | { | |
570 | struct nilfs_super_block **sbp = nilfs->ns_sbp; | |
571 | struct buffer_head **sbh = nilfs->ns_sbh; | |
572 | u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size); | |
573 | int valid[2], swp = 0; | |
574 | ||
575 | sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize, | |
576 | &sbh[0]); | |
577 | sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]); | |
578 | ||
579 | if (!sbp[0]) { | |
580 | if (!sbp[1]) { | |
581 | printk(KERN_ERR "NILFS: unable to read superblock\n"); | |
582 | return -EIO; | |
583 | } | |
584 | printk(KERN_WARNING | |
585 | "NILFS warning: unable to read primary superblock\n"); | |
586 | } else if (!sbp[1]) | |
587 | printk(KERN_WARNING | |
588 | "NILFS warning: unable to read secondary superblock\n"); | |
589 | ||
25294d8c RK |
590 | /* |
591 | * Compare two super blocks and set 1 in swp if the secondary | |
592 | * super block is valid and newer. Otherwise, set 0 in swp. | |
593 | */ | |
e339ad31 RK |
594 | valid[0] = nilfs_valid_sb(sbp[0]); |
595 | valid[1] = nilfs_valid_sb(sbp[1]); | |
25294d8c RK |
596 | swp = valid[1] && (!valid[0] || |
597 | le64_to_cpu(sbp[1]->s_last_cno) > | |
598 | le64_to_cpu(sbp[0]->s_last_cno)); | |
e339ad31 RK |
599 | |
600 | if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) { | |
601 | brelse(sbh[1]); | |
602 | sbh[1] = NULL; | |
603 | sbp[1] = NULL; | |
604 | swp = 0; | |
605 | } | |
606 | if (!valid[swp]) { | |
607 | nilfs_release_super_block(nilfs); | |
608 | printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n", | |
609 | sb->s_id); | |
610 | return -EINVAL; | |
611 | } | |
612 | ||
ea1a16f7 | 613 | if (!valid[!swp]) |
e339ad31 RK |
614 | printk(KERN_WARNING "NILFS warning: broken superblock. " |
615 | "using spare superblock.\n"); | |
ea1a16f7 | 616 | if (swp) |
e339ad31 | 617 | nilfs_swap_super_block(nilfs); |
e339ad31 | 618 | |
b2ac86e1 JS |
619 | nilfs->ns_sbwcount = 0; |
620 | nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime); | |
e339ad31 RK |
621 | nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq); |
622 | *sbpp = sbp[0]; | |
623 | return 0; | |
624 | } | |
625 | ||
8a9d2191 RK |
626 | /** |
627 | * init_nilfs - initialize a NILFS instance. | |
628 | * @nilfs: the_nilfs structure | |
629 | * @sbi: nilfs_sb_info | |
630 | * @sb: super block | |
631 | * @data: mount options | |
632 | * | |
633 | * init_nilfs() performs common initialization per block device (e.g. | |
634 | * reading the super block, getting disk layout information, initializing | |
635 | * shared fields in the_nilfs). It takes on some portion of the jobs | |
636 | * typically done by a fill_super() routine. This division arises from | |
637 | * the nature that multiple NILFS instances may be simultaneously | |
638 | * mounted on a device. | |
639 | * For multiple mounts on the same device, only the first mount | |
640 | * invokes these tasks. | |
641 | * | |
642 | * Return Value: On success, 0 is returned. On error, a negative error | |
643 | * code is returned. | |
644 | */ | |
645 | int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data) | |
646 | { | |
647 | struct super_block *sb = sbi->s_super; | |
8a9d2191 RK |
648 | struct nilfs_super_block *sbp; |
649 | struct backing_dev_info *bdi; | |
650 | int blocksize; | |
e339ad31 | 651 | int err; |
8a9d2191 RK |
652 | |
653 | down_write(&nilfs->ns_sem); | |
654 | if (nilfs_init(nilfs)) { | |
655 | /* Load values from existing the_nilfs */ | |
e339ad31 | 656 | sbp = nilfs->ns_sbp[0]; |
8a9d2191 RK |
657 | err = nilfs_store_magic_and_option(sb, sbp, data); |
658 | if (err) | |
659 | goto out; | |
660 | ||
c5ca48aa RK |
661 | err = nilfs_check_feature_compatibility(sb, sbp); |
662 | if (err) | |
663 | goto out; | |
664 | ||
8a9d2191 RK |
665 | blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size); |
666 | if (sb->s_blocksize != blocksize && | |
667 | !sb_set_blocksize(sb, blocksize)) { | |
668 | printk(KERN_ERR "NILFS: blocksize %d unfit to device\n", | |
669 | blocksize); | |
670 | err = -EINVAL; | |
671 | } | |
672 | sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits); | |
673 | goto out; | |
674 | } | |
675 | ||
89c0fd01 | 676 | blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE); |
e339ad31 RK |
677 | if (!blocksize) { |
678 | printk(KERN_ERR "NILFS: unable to set blocksize\n"); | |
8a9d2191 RK |
679 | err = -EINVAL; |
680 | goto out; | |
681 | } | |
e339ad31 RK |
682 | err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp); |
683 | if (err) | |
684 | goto out; | |
685 | ||
8a9d2191 RK |
686 | err = nilfs_store_magic_and_option(sb, sbp, data); |
687 | if (err) | |
688 | goto failed_sbh; | |
689 | ||
c5ca48aa RK |
690 | err = nilfs_check_feature_compatibility(sb, sbp); |
691 | if (err) | |
692 | goto failed_sbh; | |
693 | ||
8a9d2191 | 694 | blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size); |
89c0fd01 RK |
695 | if (blocksize < NILFS_MIN_BLOCK_SIZE || |
696 | blocksize > NILFS_MAX_BLOCK_SIZE) { | |
697 | printk(KERN_ERR "NILFS: couldn't mount because of unsupported " | |
698 | "filesystem blocksize %d\n", blocksize); | |
699 | err = -EINVAL; | |
700 | goto failed_sbh; | |
701 | } | |
8a9d2191 | 702 | if (sb->s_blocksize != blocksize) { |
e1defc4f | 703 | int hw_blocksize = bdev_logical_block_size(sb->s_bdev); |
e339ad31 RK |
704 | |
705 | if (blocksize < hw_blocksize) { | |
706 | printk(KERN_ERR | |
707 | "NILFS: blocksize %d too small for device " | |
708 | "(sector-size = %d).\n", | |
709 | blocksize, hw_blocksize); | |
8a9d2191 | 710 | err = -EINVAL; |
e339ad31 RK |
711 | goto failed_sbh; |
712 | } | |
713 | nilfs_release_super_block(nilfs); | |
714 | sb_set_blocksize(sb, blocksize); | |
715 | ||
716 | err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp); | |
717 | if (err) | |
8a9d2191 RK |
718 | goto out; |
719 | /* not failed_sbh; sbh is released automatically | |
720 | when reloading fails. */ | |
8a9d2191 RK |
721 | } |
722 | nilfs->ns_blocksize_bits = sb->s_blocksize_bits; | |
92c60cca | 723 | nilfs->ns_blocksize = blocksize; |
8a9d2191 | 724 | |
e339ad31 | 725 | err = nilfs_store_disk_layout(nilfs, sbp); |
8a9d2191 RK |
726 | if (err) |
727 | goto failed_sbh; | |
728 | ||
729 | sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits); | |
730 | ||
731 | nilfs->ns_mount_state = le16_to_cpu(sbp->s_state); | |
8a9d2191 | 732 | |
2c96ce9f | 733 | bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info; |
8a9d2191 RK |
734 | nilfs->ns_bdi = bdi ? : &default_backing_dev_info; |
735 | ||
843d63ba RK |
736 | err = nilfs_store_log_cursor(nilfs, sbp); |
737 | if (err) | |
8a9d2191 | 738 | goto failed_sbh; |
8a9d2191 | 739 | |
8a9d2191 RK |
740 | set_nilfs_init(nilfs); |
741 | err = 0; | |
742 | out: | |
743 | up_write(&nilfs->ns_sem); | |
744 | return err; | |
745 | ||
746 | failed_sbh: | |
e339ad31 | 747 | nilfs_release_super_block(nilfs); |
8a9d2191 RK |
748 | goto out; |
749 | } | |
750 | ||
e902ec99 JS |
751 | int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump, |
752 | size_t nsegs) | |
753 | { | |
754 | sector_t seg_start, seg_end; | |
755 | sector_t start = 0, nblocks = 0; | |
756 | unsigned int sects_per_block; | |
757 | __u64 *sn; | |
758 | int ret = 0; | |
759 | ||
760 | sects_per_block = (1 << nilfs->ns_blocksize_bits) / | |
761 | bdev_logical_block_size(nilfs->ns_bdev); | |
762 | for (sn = segnump; sn < segnump + nsegs; sn++) { | |
763 | nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end); | |
764 | ||
765 | if (!nblocks) { | |
766 | start = seg_start; | |
767 | nblocks = seg_end - seg_start + 1; | |
768 | } else if (start + nblocks == seg_start) { | |
769 | nblocks += seg_end - seg_start + 1; | |
770 | } else { | |
771 | ret = blkdev_issue_discard(nilfs->ns_bdev, | |
772 | start * sects_per_block, | |
773 | nblocks * sects_per_block, | |
774 | GFP_NOFS, | |
1cb0c924 | 775 | BLKDEV_IFL_WAIT | |
6a47dc14 | 776 | BLKDEV_IFL_BARRIER); |
e902ec99 JS |
777 | if (ret < 0) |
778 | return ret; | |
779 | nblocks = 0; | |
780 | } | |
781 | } | |
782 | if (nblocks) | |
783 | ret = blkdev_issue_discard(nilfs->ns_bdev, | |
784 | start * sects_per_block, | |
785 | nblocks * sects_per_block, | |
1cb0c924 RK |
786 | GFP_NOFS, |
787 | BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER); | |
e902ec99 JS |
788 | return ret; |
789 | } | |
790 | ||
8a9d2191 RK |
791 | int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks) |
792 | { | |
793 | struct inode *dat = nilfs_dat_inode(nilfs); | |
794 | unsigned long ncleansegs; | |
8a9d2191 RK |
795 | |
796 | down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ | |
ef7d4757 | 797 | ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile); |
8a9d2191 | 798 | up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ |
ef7d4757 RK |
799 | *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment; |
800 | return 0; | |
8a9d2191 RK |
801 | } |
802 | ||
8a9d2191 RK |
803 | int nilfs_near_disk_full(struct the_nilfs *nilfs) |
804 | { | |
8a9d2191 | 805 | unsigned long ncleansegs, nincsegs; |
ef7d4757 RK |
806 | |
807 | ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile); | |
808 | nincsegs = atomic_read(&nilfs->ns_ndirtyblks) / | |
809 | nilfs->ns_blocks_per_segment + 1; | |
810 | ||
811 | return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs; | |
8a9d2191 RK |
812 | } |
813 | ||
ba65ae47 RK |
814 | struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno) |
815 | { | |
816 | struct rb_node *n; | |
817 | struct nilfs_root *root; | |
818 | ||
819 | spin_lock(&nilfs->ns_cptree_lock); | |
820 | n = nilfs->ns_cptree.rb_node; | |
821 | while (n) { | |
822 | root = rb_entry(n, struct nilfs_root, rb_node); | |
823 | ||
824 | if (cno < root->cno) { | |
825 | n = n->rb_left; | |
826 | } else if (cno > root->cno) { | |
827 | n = n->rb_right; | |
828 | } else { | |
829 | atomic_inc(&root->count); | |
830 | spin_unlock(&nilfs->ns_cptree_lock); | |
831 | return root; | |
832 | } | |
833 | } | |
834 | spin_unlock(&nilfs->ns_cptree_lock); | |
835 | ||
836 | return NULL; | |
837 | } | |
838 | ||
839 | struct nilfs_root * | |
840 | nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno) | |
841 | { | |
842 | struct rb_node **p, *parent; | |
843 | struct nilfs_root *root, *new; | |
844 | ||
845 | root = nilfs_lookup_root(nilfs, cno); | |
846 | if (root) | |
847 | return root; | |
848 | ||
849 | new = kmalloc(sizeof(*root), GFP_KERNEL); | |
850 | if (!new) | |
851 | return NULL; | |
852 | ||
853 | spin_lock(&nilfs->ns_cptree_lock); | |
854 | ||
855 | p = &nilfs->ns_cptree.rb_node; | |
856 | parent = NULL; | |
857 | ||
858 | while (*p) { | |
859 | parent = *p; | |
860 | root = rb_entry(parent, struct nilfs_root, rb_node); | |
861 | ||
862 | if (cno < root->cno) { | |
863 | p = &(*p)->rb_left; | |
864 | } else if (cno > root->cno) { | |
865 | p = &(*p)->rb_right; | |
866 | } else { | |
867 | atomic_inc(&root->count); | |
868 | spin_unlock(&nilfs->ns_cptree_lock); | |
869 | kfree(new); | |
870 | return root; | |
871 | } | |
872 | } | |
873 | ||
874 | new->cno = cno; | |
875 | new->ifile = NULL; | |
876 | new->nilfs = nilfs; | |
877 | atomic_set(&new->count, 1); | |
878 | atomic_set(&new->inodes_count, 0); | |
879 | atomic_set(&new->blocks_count, 0); | |
880 | ||
881 | rb_link_node(&new->rb_node, parent, p); | |
882 | rb_insert_color(&new->rb_node, &nilfs->ns_cptree); | |
883 | ||
884 | spin_unlock(&nilfs->ns_cptree_lock); | |
885 | ||
886 | return new; | |
887 | } | |
888 | ||
889 | void nilfs_put_root(struct nilfs_root *root) | |
890 | { | |
891 | if (atomic_dec_and_test(&root->count)) { | |
892 | struct the_nilfs *nilfs = root->nilfs; | |
893 | ||
894 | spin_lock(&nilfs->ns_cptree_lock); | |
895 | rb_erase(&root->rb_node, &nilfs->ns_cptree); | |
896 | spin_unlock(&nilfs->ns_cptree_lock); | |
897 | if (root->ifile) | |
898 | nilfs_mdt_destroy(root->ifile); | |
899 | ||
900 | kfree(root); | |
901 | } | |
902 | } | |
903 | ||
6dd47406 RK |
904 | /** |
905 | * nilfs_find_sbinfo - find existing nilfs_sb_info structure | |
906 | * @nilfs: nilfs object | |
907 | * @rw_mount: mount type (non-zero value for read/write mount) | |
908 | * @cno: checkpoint number (zero for read-only mount) | |
909 | * | |
910 | * nilfs_find_sbinfo() returns the nilfs_sb_info structure which | |
911 | * @rw_mount and @cno (in case of snapshots) matched. If no instance | |
912 | * was found, NULL is returned. Although the super block instance can | |
913 | * be unmounted after this function returns, the nilfs_sb_info struct | |
914 | * is kept on memory until nilfs_put_sbinfo() is called. | |
915 | */ | |
916 | struct nilfs_sb_info *nilfs_find_sbinfo(struct the_nilfs *nilfs, | |
917 | int rw_mount, __u64 cno) | |
918 | { | |
919 | struct nilfs_sb_info *sbi; | |
920 | ||
e59399d0 | 921 | down_read(&nilfs->ns_super_sem); |
6dd47406 RK |
922 | /* |
923 | * The SNAPSHOT flag and sb->s_flags are supposed to be | |
e59399d0 | 924 | * protected with nilfs->ns_super_sem. |
6dd47406 RK |
925 | */ |
926 | sbi = nilfs->ns_current; | |
927 | if (rw_mount) { | |
928 | if (sbi && !(sbi->s_super->s_flags & MS_RDONLY)) | |
929 | goto found; /* read/write mount */ | |
930 | else | |
931 | goto out; | |
932 | } else if (cno == 0) { | |
933 | if (sbi && (sbi->s_super->s_flags & MS_RDONLY)) | |
934 | goto found; /* read-only mount */ | |
935 | else | |
936 | goto out; | |
937 | } | |
938 | ||
939 | list_for_each_entry(sbi, &nilfs->ns_supers, s_list) { | |
940 | if (nilfs_test_opt(sbi, SNAPSHOT) && | |
941 | sbi->s_snapshot_cno == cno) | |
942 | goto found; /* snapshot mount */ | |
943 | } | |
944 | out: | |
e59399d0 | 945 | up_read(&nilfs->ns_super_sem); |
6dd47406 RK |
946 | return NULL; |
947 | ||
948 | found: | |
949 | atomic_inc(&sbi->s_count); | |
e59399d0 | 950 | up_read(&nilfs->ns_super_sem); |
6dd47406 RK |
951 | return sbi; |
952 | } | |
953 | ||
8a9d2191 RK |
954 | int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno, |
955 | int snapshot_mount) | |
956 | { | |
957 | struct nilfs_sb_info *sbi; | |
958 | int ret = 0; | |
959 | ||
e59399d0 | 960 | down_read(&nilfs->ns_super_sem); |
8a9d2191 RK |
961 | if (cno == 0 || cno > nilfs->ns_cno) |
962 | goto out_unlock; | |
963 | ||
964 | list_for_each_entry(sbi, &nilfs->ns_supers, s_list) { | |
965 | if (sbi->s_snapshot_cno == cno && | |
966 | (!snapshot_mount || nilfs_test_opt(sbi, SNAPSHOT))) { | |
967 | /* exclude read-only mounts */ | |
968 | ret++; | |
969 | break; | |
970 | } | |
971 | } | |
972 | /* for protecting recent checkpoints */ | |
973 | if (cno >= nilfs_last_cno(nilfs)) | |
974 | ret++; | |
975 | ||
976 | out_unlock: | |
e59399d0 | 977 | up_read(&nilfs->ns_super_sem); |
8a9d2191 RK |
978 | return ret; |
979 | } |