]>
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 | 37 | |
6c125160 RK |
38 | static int nilfs_valid_sb(struct nilfs_super_block *sbp); |
39 | ||
8a9d2191 RK |
40 | void 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; | |
32502047 RK |
47 | |
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: | |
8a9d2191 RK |
57 | spin_unlock(&nilfs->ns_last_segment_lock); |
58 | } | |
59 | ||
60 | /** | |
348fe8da | 61 | * alloc_nilfs - allocate a nilfs object |
8a9d2191 RK |
62 | * @bdev: block device to which the_nilfs is related |
63 | * | |
8a9d2191 RK |
64 | * Return Value: On success, pointer to the_nilfs is returned. |
65 | * On error, NULL is returned. | |
66 | */ | |
348fe8da | 67 | struct the_nilfs *alloc_nilfs(struct block_device *bdev) |
8a9d2191 RK |
68 | { |
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; | |
8a9d2191 RK |
76 | atomic_set(&nilfs->ns_ndirtyblks, 0); |
77 | init_rwsem(&nilfs->ns_sem); | |
027d6404 | 78 | init_rwsem(&nilfs->ns_writer_sem); |
263d90ce | 79 | INIT_LIST_HEAD(&nilfs->ns_gc_inodes); |
8a9d2191 | 80 | spin_lock_init(&nilfs->ns_last_segment_lock); |
ba65ae47 RK |
81 | nilfs->ns_cptree = RB_ROOT; |
82 | spin_lock_init(&nilfs->ns_cptree_lock); | |
8a9d2191 | 83 | init_rwsem(&nilfs->ns_segctor_sem); |
8a9d2191 RK |
84 | |
85 | return nilfs; | |
86 | } | |
87 | ||
33c8e57c | 88 | /** |
348fe8da RK |
89 | * destroy_nilfs - destroy nilfs object |
90 | * @nilfs: nilfs object to be released | |
33c8e57c | 91 | */ |
348fe8da | 92 | void destroy_nilfs(struct the_nilfs *nilfs) |
33c8e57c | 93 | { |
8a9d2191 | 94 | might_sleep(); |
8a9d2191 | 95 | if (nilfs_init(nilfs)) { |
e339ad31 RK |
96 | brelse(nilfs->ns_sbh[0]); |
97 | brelse(nilfs->ns_sbh[1]); | |
8a9d2191 RK |
98 | } |
99 | kfree(nilfs); | |
100 | } | |
101 | ||
f1e89c86 RK |
102 | static int nilfs_load_super_root(struct the_nilfs *nilfs, |
103 | struct super_block *sb, sector_t sr_block) | |
8a9d2191 RK |
104 | { |
105 | struct buffer_head *bh_sr; | |
106 | struct nilfs_super_root *raw_sr; | |
e339ad31 | 107 | struct nilfs_super_block **sbp = nilfs->ns_sbp; |
f1e89c86 | 108 | struct nilfs_inode *rawi; |
8a9d2191 RK |
109 | unsigned dat_entry_size, segment_usage_size, checkpoint_size; |
110 | unsigned inode_size; | |
111 | int err; | |
112 | ||
8b94025c | 113 | err = nilfs_read_super_root_block(nilfs, sr_block, &bh_sr, 1); |
8a9d2191 RK |
114 | if (unlikely(err)) |
115 | return err; | |
116 | ||
117 | down_read(&nilfs->ns_sem); | |
e339ad31 RK |
118 | dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size); |
119 | checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size); | |
120 | segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size); | |
8a9d2191 RK |
121 | up_read(&nilfs->ns_sem); |
122 | ||
123 | inode_size = nilfs->ns_inode_size; | |
124 | ||
f1e89c86 RK |
125 | rawi = (void *)bh_sr->b_data + NILFS_SR_DAT_OFFSET(inode_size); |
126 | err = nilfs_dat_read(sb, dat_entry_size, rawi, &nilfs->ns_dat); | |
127 | if (err) | |
8a9d2191 RK |
128 | goto failed; |
129 | ||
f1e89c86 RK |
130 | rawi = (void *)bh_sr->b_data + NILFS_SR_CPFILE_OFFSET(inode_size); |
131 | err = nilfs_cpfile_read(sb, checkpoint_size, rawi, &nilfs->ns_cpfile); | |
132 | if (err) | |
c1c1d709 | 133 | goto failed_dat; |
8a9d2191 | 134 | |
f1e89c86 RK |
135 | rawi = (void *)bh_sr->b_data + NILFS_SR_SUFILE_OFFSET(inode_size); |
136 | err = nilfs_sufile_read(sb, segment_usage_size, rawi, | |
137 | &nilfs->ns_sufile); | |
138 | if (err) | |
8a9d2191 RK |
139 | goto failed_cpfile; |
140 | ||
8a9d2191 RK |
141 | raw_sr = (struct nilfs_super_root *)bh_sr->b_data; |
142 | nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime); | |
143 | ||
144 | failed: | |
145 | brelse(bh_sr); | |
146 | return err; | |
147 | ||
8a9d2191 | 148 | failed_cpfile: |
f1e89c86 | 149 | iput(nilfs->ns_cpfile); |
8a9d2191 | 150 | |
8a9d2191 | 151 | failed_dat: |
f1e89c86 | 152 | iput(nilfs->ns_dat); |
8a9d2191 RK |
153 | goto failed; |
154 | } | |
155 | ||
156 | static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri) | |
157 | { | |
158 | memset(ri, 0, sizeof(*ri)); | |
159 | INIT_LIST_HEAD(&ri->ri_used_segments); | |
160 | } | |
161 | ||
162 | static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri) | |
163 | { | |
164 | nilfs_dispose_segment_list(&ri->ri_used_segments); | |
165 | } | |
166 | ||
843d63ba RK |
167 | /** |
168 | * nilfs_store_log_cursor - load log cursor from a super block | |
169 | * @nilfs: nilfs object | |
170 | * @sbp: buffer storing super block to be read | |
171 | * | |
172 | * nilfs_store_log_cursor() reads the last position of the log | |
173 | * containing a super root from a given super block, and initializes | |
174 | * relevant information on the nilfs object preparatory for log | |
175 | * scanning and recovery. | |
176 | */ | |
177 | static int nilfs_store_log_cursor(struct the_nilfs *nilfs, | |
178 | struct nilfs_super_block *sbp) | |
179 | { | |
180 | int ret = 0; | |
181 | ||
182 | nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg); | |
183 | nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno); | |
184 | nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq); | |
185 | ||
32502047 | 186 | nilfs->ns_prev_seq = nilfs->ns_last_seq; |
843d63ba RK |
187 | nilfs->ns_seg_seq = nilfs->ns_last_seq; |
188 | nilfs->ns_segnum = | |
189 | nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg); | |
190 | nilfs->ns_cno = nilfs->ns_last_cno + 1; | |
191 | if (nilfs->ns_segnum >= nilfs->ns_nsegments) { | |
192 | printk(KERN_ERR "NILFS invalid last segment number.\n"); | |
193 | ret = -EINVAL; | |
194 | } | |
195 | return ret; | |
196 | } | |
197 | ||
8a9d2191 RK |
198 | /** |
199 | * load_nilfs - load and recover the nilfs | |
200 | * @nilfs: the_nilfs structure to be released | |
201 | * @sbi: nilfs_sb_info used to recover past segment | |
202 | * | |
203 | * load_nilfs() searches and load the latest super root, | |
204 | * attaches the last segment, and does recovery if needed. | |
205 | * The caller must call this exclusively for simultaneous mounts. | |
206 | */ | |
207 | int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi) | |
208 | { | |
209 | struct nilfs_recovery_info ri; | |
210 | unsigned int s_flags = sbi->s_super->s_flags; | |
211 | int really_read_only = bdev_read_only(nilfs->ns_bdev); | |
a057d2c0 | 212 | int valid_fs = nilfs_valid_fs(nilfs); |
f50a4c81 | 213 | int err; |
8a9d2191 | 214 | |
f50a4c81 RK |
215 | if (!valid_fs) { |
216 | printk(KERN_WARNING "NILFS warning: mounting unchecked fs\n"); | |
217 | if (s_flags & MS_RDONLY) { | |
218 | printk(KERN_INFO "NILFS: INFO: recovery " | |
219 | "required for readonly filesystem.\n"); | |
220 | printk(KERN_INFO "NILFS: write access will " | |
221 | "be enabled during recovery.\n"); | |
8a9d2191 | 222 | } |
8a9d2191 RK |
223 | } |
224 | ||
f50a4c81 RK |
225 | nilfs_init_recovery_info(&ri); |
226 | ||
8b94025c | 227 | err = nilfs_search_super_root(nilfs, &ri); |
8a9d2191 | 228 | if (unlikely(err)) { |
6c125160 RK |
229 | struct nilfs_super_block **sbp = nilfs->ns_sbp; |
230 | int blocksize; | |
231 | ||
232 | if (err != -EINVAL) | |
233 | goto scan_error; | |
234 | ||
235 | if (!nilfs_valid_sb(sbp[1])) { | |
236 | printk(KERN_WARNING | |
237 | "NILFS warning: unable to fall back to spare" | |
238 | "super block\n"); | |
239 | goto scan_error; | |
240 | } | |
241 | printk(KERN_INFO | |
242 | "NILFS: try rollback from an earlier position\n"); | |
243 | ||
244 | /* | |
245 | * restore super block with its spare and reconfigure | |
246 | * relevant states of the nilfs object. | |
247 | */ | |
248 | memcpy(sbp[0], sbp[1], nilfs->ns_sbsize); | |
249 | nilfs->ns_crc_seed = le32_to_cpu(sbp[0]->s_crc_seed); | |
250 | nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime); | |
251 | ||
252 | /* verify consistency between two super blocks */ | |
253 | blocksize = BLOCK_SIZE << le32_to_cpu(sbp[0]->s_log_block_size); | |
254 | if (blocksize != nilfs->ns_blocksize) { | |
255 | printk(KERN_WARNING | |
256 | "NILFS warning: blocksize differs between " | |
257 | "two super blocks (%d != %d)\n", | |
258 | blocksize, nilfs->ns_blocksize); | |
259 | goto scan_error; | |
260 | } | |
261 | ||
262 | err = nilfs_store_log_cursor(nilfs, sbp[0]); | |
263 | if (err) | |
264 | goto scan_error; | |
265 | ||
266 | /* drop clean flag to allow roll-forward and recovery */ | |
267 | nilfs->ns_mount_state &= ~NILFS_VALID_FS; | |
268 | valid_fs = 0; | |
269 | ||
270 | err = nilfs_search_super_root(nilfs, &ri); | |
271 | if (err) | |
272 | goto scan_error; | |
8a9d2191 RK |
273 | } |
274 | ||
f1e89c86 | 275 | err = nilfs_load_super_root(nilfs, sbi->s_super, ri.ri_super_root); |
8a9d2191 RK |
276 | if (unlikely(err)) { |
277 | printk(KERN_ERR "NILFS: error loading super root.\n"); | |
278 | goto failed; | |
279 | } | |
280 | ||
f50a4c81 RK |
281 | if (valid_fs) |
282 | goto skip_recovery; | |
283 | ||
284 | if (s_flags & MS_RDONLY) { | |
c5ca48aa RK |
285 | __u64 features; |
286 | ||
0234576d RK |
287 | if (nilfs_test_opt(sbi, NORECOVERY)) { |
288 | printk(KERN_INFO "NILFS: norecovery option specified. " | |
289 | "skipping roll-forward recovery\n"); | |
290 | goto skip_recovery; | |
291 | } | |
c5ca48aa RK |
292 | features = le64_to_cpu(nilfs->ns_sbp[0]->s_feature_compat_ro) & |
293 | ~NILFS_FEATURE_COMPAT_RO_SUPP; | |
294 | if (features) { | |
295 | printk(KERN_ERR "NILFS: couldn't proceed with " | |
296 | "recovery because of unsupported optional " | |
297 | "features (%llx)\n", | |
298 | (unsigned long long)features); | |
299 | err = -EROFS; | |
300 | goto failed_unload; | |
301 | } | |
f50a4c81 RK |
302 | if (really_read_only) { |
303 | printk(KERN_ERR "NILFS: write access " | |
304 | "unavailable, cannot proceed.\n"); | |
305 | err = -EROFS; | |
306 | goto failed_unload; | |
8a9d2191 | 307 | } |
f50a4c81 | 308 | sbi->s_super->s_flags &= ~MS_RDONLY; |
0234576d RK |
309 | } else if (nilfs_test_opt(sbi, NORECOVERY)) { |
310 | printk(KERN_ERR "NILFS: recovery cancelled because norecovery " | |
311 | "option was specified for a read/write mount\n"); | |
312 | err = -EINVAL; | |
313 | goto failed_unload; | |
f50a4c81 RK |
314 | } |
315 | ||
aee5ce2f | 316 | err = nilfs_salvage_orphan_logs(nilfs, sbi, &ri); |
f50a4c81 RK |
317 | if (err) |
318 | goto failed_unload; | |
319 | ||
320 | down_write(&nilfs->ns_sem); | |
7ecaa46c RK |
321 | nilfs->ns_mount_state |= NILFS_VALID_FS; /* set "clean" flag */ |
322 | err = nilfs_cleanup_super(sbi); | |
f50a4c81 RK |
323 | up_write(&nilfs->ns_sem); |
324 | ||
325 | if (err) { | |
326 | printk(KERN_ERR "NILFS: failed to update super block. " | |
327 | "recovery unfinished.\n"); | |
328 | goto failed_unload; | |
8a9d2191 | 329 | } |
f50a4c81 | 330 | printk(KERN_INFO "NILFS: recovery complete.\n"); |
8a9d2191 | 331 | |
f50a4c81 | 332 | skip_recovery: |
8a9d2191 | 333 | set_nilfs_loaded(nilfs); |
f50a4c81 RK |
334 | nilfs_clear_recovery_info(&ri); |
335 | sbi->s_super->s_flags = s_flags; | |
336 | return 0; | |
337 | ||
6c125160 RK |
338 | scan_error: |
339 | printk(KERN_ERR "NILFS: error searching super root.\n"); | |
340 | goto failed; | |
341 | ||
f50a4c81 | 342 | failed_unload: |
f1e89c86 RK |
343 | iput(nilfs->ns_cpfile); |
344 | iput(nilfs->ns_sufile); | |
345 | iput(nilfs->ns_dat); | |
8a9d2191 RK |
346 | |
347 | failed: | |
348 | nilfs_clear_recovery_info(&ri); | |
349 | sbi->s_super->s_flags = s_flags; | |
350 | return err; | |
351 | } | |
352 | ||
353 | static 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 | ||
e339ad31 RK |
364 | static int nilfs_store_disk_layout(struct the_nilfs *nilfs, |
365 | struct nilfs_super_block *sbp) | |
8a9d2191 | 366 | { |
9566a7a8 RK |
367 | if (le32_to_cpu(sbp->s_rev_level) < NILFS_MIN_SUPP_REV) { |
368 | printk(KERN_ERR "NILFS: unsupported revision " | |
8a9d2191 RK |
369 | "(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 | } | |
e339ad31 RK |
376 | nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes); |
377 | if (nilfs->ns_sbsize > BLOCK_SIZE) | |
378 | return -EINVAL; | |
379 | ||
8a9d2191 RK |
380 | 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) { | |
c91cea11 | 385 | printk(KERN_ERR "NILFS: too short segment.\n"); |
8a9d2191 RK |
386 | 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 | ||
e339ad31 RK |
401 | static 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 | ||
421 | static 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 | ||
428 | static 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 | ||
441 | void 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 | ||
450 | void 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 | ||
461 | static int nilfs_load_super_block(struct the_nilfs *nilfs, | |
462 | struct super_block *sb, int blocksize, | |
463 | struct nilfs_super_block **sbpp) | |
464 | { | |
465 | struct nilfs_super_block **sbp = nilfs->ns_sbp; | |
466 | struct buffer_head **sbh = nilfs->ns_sbh; | |
467 | u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size); | |
468 | int valid[2], swp = 0; | |
469 | ||
470 | sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize, | |
471 | &sbh[0]); | |
472 | sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]); | |
473 | ||
474 | if (!sbp[0]) { | |
475 | if (!sbp[1]) { | |
476 | printk(KERN_ERR "NILFS: unable to read superblock\n"); | |
477 | return -EIO; | |
478 | } | |
479 | printk(KERN_WARNING | |
480 | "NILFS warning: unable to read primary superblock\n"); | |
481 | } else if (!sbp[1]) | |
482 | printk(KERN_WARNING | |
483 | "NILFS warning: unable to read secondary superblock\n"); | |
484 | ||
25294d8c RK |
485 | /* |
486 | * Compare two super blocks and set 1 in swp if the secondary | |
487 | * super block is valid and newer. Otherwise, set 0 in swp. | |
488 | */ | |
e339ad31 RK |
489 | valid[0] = nilfs_valid_sb(sbp[0]); |
490 | valid[1] = nilfs_valid_sb(sbp[1]); | |
25294d8c RK |
491 | swp = valid[1] && (!valid[0] || |
492 | le64_to_cpu(sbp[1]->s_last_cno) > | |
493 | le64_to_cpu(sbp[0]->s_last_cno)); | |
e339ad31 RK |
494 | |
495 | if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) { | |
496 | brelse(sbh[1]); | |
497 | sbh[1] = NULL; | |
498 | sbp[1] = NULL; | |
499 | swp = 0; | |
500 | } | |
501 | if (!valid[swp]) { | |
502 | nilfs_release_super_block(nilfs); | |
503 | printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n", | |
504 | sb->s_id); | |
505 | return -EINVAL; | |
506 | } | |
507 | ||
ea1a16f7 | 508 | if (!valid[!swp]) |
e339ad31 RK |
509 | printk(KERN_WARNING "NILFS warning: broken superblock. " |
510 | "using spare superblock.\n"); | |
ea1a16f7 | 511 | if (swp) |
e339ad31 | 512 | nilfs_swap_super_block(nilfs); |
e339ad31 | 513 | |
b2ac86e1 JS |
514 | nilfs->ns_sbwcount = 0; |
515 | nilfs->ns_sbwtime = le64_to_cpu(sbp[0]->s_wtime); | |
e339ad31 RK |
516 | nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq); |
517 | *sbpp = sbp[0]; | |
518 | return 0; | |
519 | } | |
520 | ||
8a9d2191 RK |
521 | /** |
522 | * init_nilfs - initialize a NILFS instance. | |
523 | * @nilfs: the_nilfs structure | |
524 | * @sbi: nilfs_sb_info | |
525 | * @sb: super block | |
526 | * @data: mount options | |
527 | * | |
528 | * init_nilfs() performs common initialization per block device (e.g. | |
529 | * reading the super block, getting disk layout information, initializing | |
f11459ad | 530 | * shared fields in the_nilfs). |
8a9d2191 RK |
531 | * |
532 | * Return Value: On success, 0 is returned. On error, a negative error | |
533 | * code is returned. | |
534 | */ | |
535 | int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data) | |
536 | { | |
537 | struct super_block *sb = sbi->s_super; | |
8a9d2191 RK |
538 | struct nilfs_super_block *sbp; |
539 | struct backing_dev_info *bdi; | |
540 | int blocksize; | |
e339ad31 | 541 | int err; |
8a9d2191 RK |
542 | |
543 | down_write(&nilfs->ns_sem); | |
8a9d2191 | 544 | |
89c0fd01 | 545 | blocksize = sb_min_blocksize(sb, NILFS_MIN_BLOCK_SIZE); |
e339ad31 RK |
546 | if (!blocksize) { |
547 | printk(KERN_ERR "NILFS: unable to set blocksize\n"); | |
8a9d2191 RK |
548 | err = -EINVAL; |
549 | goto out; | |
550 | } | |
e339ad31 RK |
551 | err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp); |
552 | if (err) | |
553 | goto out; | |
554 | ||
8a9d2191 RK |
555 | err = nilfs_store_magic_and_option(sb, sbp, data); |
556 | if (err) | |
557 | goto failed_sbh; | |
558 | ||
c5ca48aa RK |
559 | err = nilfs_check_feature_compatibility(sb, sbp); |
560 | if (err) | |
561 | goto failed_sbh; | |
562 | ||
8a9d2191 | 563 | blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size); |
89c0fd01 RK |
564 | if (blocksize < NILFS_MIN_BLOCK_SIZE || |
565 | blocksize > NILFS_MAX_BLOCK_SIZE) { | |
566 | printk(KERN_ERR "NILFS: couldn't mount because of unsupported " | |
567 | "filesystem blocksize %d\n", blocksize); | |
568 | err = -EINVAL; | |
569 | goto failed_sbh; | |
570 | } | |
8a9d2191 | 571 | if (sb->s_blocksize != blocksize) { |
e1defc4f | 572 | int hw_blocksize = bdev_logical_block_size(sb->s_bdev); |
e339ad31 RK |
573 | |
574 | if (blocksize < hw_blocksize) { | |
575 | printk(KERN_ERR | |
576 | "NILFS: blocksize %d too small for device " | |
577 | "(sector-size = %d).\n", | |
578 | blocksize, hw_blocksize); | |
8a9d2191 | 579 | err = -EINVAL; |
e339ad31 RK |
580 | goto failed_sbh; |
581 | } | |
582 | nilfs_release_super_block(nilfs); | |
583 | sb_set_blocksize(sb, blocksize); | |
584 | ||
585 | err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp); | |
586 | if (err) | |
8a9d2191 RK |
587 | goto out; |
588 | /* not failed_sbh; sbh is released automatically | |
589 | when reloading fails. */ | |
8a9d2191 RK |
590 | } |
591 | nilfs->ns_blocksize_bits = sb->s_blocksize_bits; | |
92c60cca | 592 | nilfs->ns_blocksize = blocksize; |
8a9d2191 | 593 | |
e339ad31 | 594 | err = nilfs_store_disk_layout(nilfs, sbp); |
8a9d2191 RK |
595 | if (err) |
596 | goto failed_sbh; | |
597 | ||
598 | sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits); | |
599 | ||
600 | nilfs->ns_mount_state = le16_to_cpu(sbp->s_state); | |
8a9d2191 | 601 | |
2c96ce9f | 602 | bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info; |
8a9d2191 RK |
603 | nilfs->ns_bdi = bdi ? : &default_backing_dev_info; |
604 | ||
843d63ba RK |
605 | err = nilfs_store_log_cursor(nilfs, sbp); |
606 | if (err) | |
8a9d2191 | 607 | goto failed_sbh; |
8a9d2191 | 608 | |
8a9d2191 RK |
609 | set_nilfs_init(nilfs); |
610 | err = 0; | |
611 | out: | |
612 | up_write(&nilfs->ns_sem); | |
613 | return err; | |
614 | ||
615 | failed_sbh: | |
e339ad31 | 616 | nilfs_release_super_block(nilfs); |
8a9d2191 RK |
617 | goto out; |
618 | } | |
619 | ||
e902ec99 JS |
620 | int nilfs_discard_segments(struct the_nilfs *nilfs, __u64 *segnump, |
621 | size_t nsegs) | |
622 | { | |
623 | sector_t seg_start, seg_end; | |
624 | sector_t start = 0, nblocks = 0; | |
625 | unsigned int sects_per_block; | |
626 | __u64 *sn; | |
627 | int ret = 0; | |
628 | ||
629 | sects_per_block = (1 << nilfs->ns_blocksize_bits) / | |
630 | bdev_logical_block_size(nilfs->ns_bdev); | |
631 | for (sn = segnump; sn < segnump + nsegs; sn++) { | |
632 | nilfs_get_segment_range(nilfs, *sn, &seg_start, &seg_end); | |
633 | ||
634 | if (!nblocks) { | |
635 | start = seg_start; | |
636 | nblocks = seg_end - seg_start + 1; | |
637 | } else if (start + nblocks == seg_start) { | |
638 | nblocks += seg_end - seg_start + 1; | |
639 | } else { | |
640 | ret = blkdev_issue_discard(nilfs->ns_bdev, | |
641 | start * sects_per_block, | |
642 | nblocks * sects_per_block, | |
643 | GFP_NOFS, | |
1cb0c924 | 644 | BLKDEV_IFL_WAIT | |
6a47dc14 | 645 | BLKDEV_IFL_BARRIER); |
e902ec99 JS |
646 | if (ret < 0) |
647 | return ret; | |
648 | nblocks = 0; | |
649 | } | |
650 | } | |
651 | if (nblocks) | |
652 | ret = blkdev_issue_discard(nilfs->ns_bdev, | |
653 | start * sects_per_block, | |
654 | nblocks * sects_per_block, | |
1cb0c924 RK |
655 | GFP_NOFS, |
656 | BLKDEV_IFL_WAIT | BLKDEV_IFL_BARRIER); | |
e902ec99 JS |
657 | return ret; |
658 | } | |
659 | ||
8a9d2191 RK |
660 | int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks) |
661 | { | |
662 | struct inode *dat = nilfs_dat_inode(nilfs); | |
663 | unsigned long ncleansegs; | |
8a9d2191 RK |
664 | |
665 | down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ | |
ef7d4757 | 666 | ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile); |
8a9d2191 | 667 | up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ |
ef7d4757 RK |
668 | *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment; |
669 | return 0; | |
8a9d2191 RK |
670 | } |
671 | ||
8a9d2191 RK |
672 | int nilfs_near_disk_full(struct the_nilfs *nilfs) |
673 | { | |
8a9d2191 | 674 | unsigned long ncleansegs, nincsegs; |
ef7d4757 RK |
675 | |
676 | ncleansegs = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile); | |
677 | nincsegs = atomic_read(&nilfs->ns_ndirtyblks) / | |
678 | nilfs->ns_blocks_per_segment + 1; | |
679 | ||
680 | return ncleansegs <= nilfs->ns_nrsvsegs + nincsegs; | |
8a9d2191 RK |
681 | } |
682 | ||
ba65ae47 RK |
683 | struct nilfs_root *nilfs_lookup_root(struct the_nilfs *nilfs, __u64 cno) |
684 | { | |
685 | struct rb_node *n; | |
686 | struct nilfs_root *root; | |
687 | ||
688 | spin_lock(&nilfs->ns_cptree_lock); | |
689 | n = nilfs->ns_cptree.rb_node; | |
690 | while (n) { | |
691 | root = rb_entry(n, struct nilfs_root, rb_node); | |
692 | ||
693 | if (cno < root->cno) { | |
694 | n = n->rb_left; | |
695 | } else if (cno > root->cno) { | |
696 | n = n->rb_right; | |
697 | } else { | |
698 | atomic_inc(&root->count); | |
699 | spin_unlock(&nilfs->ns_cptree_lock); | |
700 | return root; | |
701 | } | |
702 | } | |
703 | spin_unlock(&nilfs->ns_cptree_lock); | |
704 | ||
705 | return NULL; | |
706 | } | |
707 | ||
708 | struct nilfs_root * | |
709 | nilfs_find_or_create_root(struct the_nilfs *nilfs, __u64 cno) | |
710 | { | |
711 | struct rb_node **p, *parent; | |
712 | struct nilfs_root *root, *new; | |
713 | ||
714 | root = nilfs_lookup_root(nilfs, cno); | |
715 | if (root) | |
716 | return root; | |
717 | ||
718 | new = kmalloc(sizeof(*root), GFP_KERNEL); | |
719 | if (!new) | |
720 | return NULL; | |
721 | ||
722 | spin_lock(&nilfs->ns_cptree_lock); | |
723 | ||
724 | p = &nilfs->ns_cptree.rb_node; | |
725 | parent = NULL; | |
726 | ||
727 | while (*p) { | |
728 | parent = *p; | |
729 | root = rb_entry(parent, struct nilfs_root, rb_node); | |
730 | ||
731 | if (cno < root->cno) { | |
732 | p = &(*p)->rb_left; | |
733 | } else if (cno > root->cno) { | |
734 | p = &(*p)->rb_right; | |
735 | } else { | |
736 | atomic_inc(&root->count); | |
737 | spin_unlock(&nilfs->ns_cptree_lock); | |
738 | kfree(new); | |
739 | return root; | |
740 | } | |
741 | } | |
742 | ||
743 | new->cno = cno; | |
744 | new->ifile = NULL; | |
745 | new->nilfs = nilfs; | |
746 | atomic_set(&new->count, 1); | |
747 | atomic_set(&new->inodes_count, 0); | |
748 | atomic_set(&new->blocks_count, 0); | |
749 | ||
750 | rb_link_node(&new->rb_node, parent, p); | |
751 | rb_insert_color(&new->rb_node, &nilfs->ns_cptree); | |
752 | ||
753 | spin_unlock(&nilfs->ns_cptree_lock); | |
754 | ||
755 | return new; | |
756 | } | |
757 | ||
758 | void nilfs_put_root(struct nilfs_root *root) | |
759 | { | |
760 | if (atomic_dec_and_test(&root->count)) { | |
761 | struct the_nilfs *nilfs = root->nilfs; | |
762 | ||
763 | spin_lock(&nilfs->ns_cptree_lock); | |
764 | rb_erase(&root->rb_node, &nilfs->ns_cptree); | |
765 | spin_unlock(&nilfs->ns_cptree_lock); | |
766 | if (root->ifile) | |
f1e89c86 | 767 | iput(root->ifile); |
ba65ae47 RK |
768 | |
769 | kfree(root); | |
770 | } | |
771 | } |