]>
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" | |
35 | #include "seglist.h" | |
36 | #include "segbuf.h" | |
37 | ||
33c8e57c RK |
38 | |
39 | static LIST_HEAD(nilfs_objects); | |
40 | static DEFINE_SPINLOCK(nilfs_lock); | |
41 | ||
8a9d2191 RK |
42 | void nilfs_set_last_segment(struct the_nilfs *nilfs, |
43 | sector_t start_blocknr, u64 seq, __u64 cno) | |
44 | { | |
45 | spin_lock(&nilfs->ns_last_segment_lock); | |
46 | nilfs->ns_last_pseg = start_blocknr; | |
47 | nilfs->ns_last_seq = seq; | |
48 | nilfs->ns_last_cno = cno; | |
49 | spin_unlock(&nilfs->ns_last_segment_lock); | |
50 | } | |
51 | ||
52 | /** | |
53 | * alloc_nilfs - allocate the_nilfs structure | |
54 | * @bdev: block device to which the_nilfs is related | |
55 | * | |
56 | * alloc_nilfs() allocates memory for the_nilfs and | |
57 | * initializes its reference count and locks. | |
58 | * | |
59 | * Return Value: On success, pointer to the_nilfs is returned. | |
60 | * On error, NULL is returned. | |
61 | */ | |
33c8e57c | 62 | static struct the_nilfs *alloc_nilfs(struct block_device *bdev) |
8a9d2191 RK |
63 | { |
64 | struct the_nilfs *nilfs; | |
65 | ||
66 | nilfs = kzalloc(sizeof(*nilfs), GFP_KERNEL); | |
67 | if (!nilfs) | |
68 | return NULL; | |
69 | ||
70 | nilfs->ns_bdev = bdev; | |
71 | atomic_set(&nilfs->ns_count, 1); | |
72 | atomic_set(&nilfs->ns_writer_refcount, -1); | |
73 | atomic_set(&nilfs->ns_ndirtyblks, 0); | |
74 | init_rwsem(&nilfs->ns_sem); | |
75 | mutex_init(&nilfs->ns_writer_mutex); | |
33c8e57c | 76 | INIT_LIST_HEAD(&nilfs->ns_list); |
8a9d2191 RK |
77 | INIT_LIST_HEAD(&nilfs->ns_supers); |
78 | spin_lock_init(&nilfs->ns_last_segment_lock); | |
79 | nilfs->ns_gc_inodes_h = NULL; | |
8a9d2191 | 80 | init_rwsem(&nilfs->ns_segctor_sem); |
8a9d2191 RK |
81 | |
82 | return nilfs; | |
83 | } | |
84 | ||
33c8e57c RK |
85 | /** |
86 | * find_or_create_nilfs - find or create nilfs object | |
87 | * @bdev: block device to which the_nilfs is related | |
88 | * | |
89 | * find_nilfs() looks up an existent nilfs object created on the | |
90 | * device and gets the reference count of the object. If no nilfs object | |
91 | * is found on the device, a new nilfs object is allocated. | |
92 | * | |
93 | * Return Value: On success, pointer to the nilfs object is returned. | |
94 | * On error, NULL is returned. | |
95 | */ | |
96 | struct the_nilfs *find_or_create_nilfs(struct block_device *bdev) | |
97 | { | |
98 | struct the_nilfs *nilfs, *new = NULL; | |
99 | ||
100 | retry: | |
101 | spin_lock(&nilfs_lock); | |
102 | list_for_each_entry(nilfs, &nilfs_objects, ns_list) { | |
103 | if (nilfs->ns_bdev == bdev) { | |
104 | get_nilfs(nilfs); | |
105 | spin_unlock(&nilfs_lock); | |
106 | if (new) | |
107 | put_nilfs(new); | |
108 | return nilfs; /* existing object */ | |
109 | } | |
110 | } | |
111 | if (new) { | |
112 | list_add_tail(&new->ns_list, &nilfs_objects); | |
113 | spin_unlock(&nilfs_lock); | |
114 | return new; /* new object */ | |
115 | } | |
116 | spin_unlock(&nilfs_lock); | |
117 | ||
118 | new = alloc_nilfs(bdev); | |
119 | if (new) | |
120 | goto retry; | |
121 | return NULL; /* insufficient memory */ | |
122 | } | |
123 | ||
8a9d2191 RK |
124 | /** |
125 | * put_nilfs - release a reference to the_nilfs | |
126 | * @nilfs: the_nilfs structure to be released | |
127 | * | |
128 | * put_nilfs() decrements a reference counter of the_nilfs. | |
129 | * If the reference count reaches zero, the_nilfs is freed. | |
130 | */ | |
131 | void put_nilfs(struct the_nilfs *nilfs) | |
132 | { | |
33c8e57c RK |
133 | spin_lock(&nilfs_lock); |
134 | if (!atomic_dec_and_test(&nilfs->ns_count)) { | |
135 | spin_unlock(&nilfs_lock); | |
8a9d2191 | 136 | return; |
33c8e57c RK |
137 | } |
138 | list_del_init(&nilfs->ns_list); | |
139 | spin_unlock(&nilfs_lock); | |
140 | ||
8a9d2191 | 141 | /* |
33c8e57c | 142 | * Increment of ns_count never occurs below because the caller |
8a9d2191 RK |
143 | * of get_nilfs() holds at least one reference to the_nilfs. |
144 | * Thus its exclusion control is not required here. | |
145 | */ | |
33c8e57c | 146 | |
8a9d2191 RK |
147 | might_sleep(); |
148 | if (nilfs_loaded(nilfs)) { | |
8a9d2191 RK |
149 | nilfs_mdt_clear(nilfs->ns_sufile); |
150 | nilfs_mdt_destroy(nilfs->ns_sufile); | |
151 | nilfs_mdt_clear(nilfs->ns_cpfile); | |
152 | nilfs_mdt_destroy(nilfs->ns_cpfile); | |
153 | nilfs_mdt_clear(nilfs->ns_dat); | |
154 | nilfs_mdt_destroy(nilfs->ns_dat); | |
155 | /* XXX: how and when to clear nilfs->ns_gc_dat? */ | |
156 | nilfs_mdt_destroy(nilfs->ns_gc_dat); | |
157 | } | |
158 | if (nilfs_init(nilfs)) { | |
159 | nilfs_destroy_gccache(nilfs); | |
e339ad31 RK |
160 | brelse(nilfs->ns_sbh[0]); |
161 | brelse(nilfs->ns_sbh[1]); | |
8a9d2191 RK |
162 | } |
163 | kfree(nilfs); | |
164 | } | |
165 | ||
166 | static int nilfs_load_super_root(struct the_nilfs *nilfs, | |
167 | struct nilfs_sb_info *sbi, sector_t sr_block) | |
168 | { | |
c2698e50 | 169 | static struct lock_class_key dat_lock_key; |
8a9d2191 RK |
170 | struct buffer_head *bh_sr; |
171 | struct nilfs_super_root *raw_sr; | |
e339ad31 | 172 | struct nilfs_super_block **sbp = nilfs->ns_sbp; |
8a9d2191 RK |
173 | unsigned dat_entry_size, segment_usage_size, checkpoint_size; |
174 | unsigned inode_size; | |
175 | int err; | |
176 | ||
177 | err = nilfs_read_super_root_block(sbi->s_super, sr_block, &bh_sr, 1); | |
178 | if (unlikely(err)) | |
179 | return err; | |
180 | ||
181 | down_read(&nilfs->ns_sem); | |
e339ad31 RK |
182 | dat_entry_size = le16_to_cpu(sbp[0]->s_dat_entry_size); |
183 | checkpoint_size = le16_to_cpu(sbp[0]->s_checkpoint_size); | |
184 | segment_usage_size = le16_to_cpu(sbp[0]->s_segment_usage_size); | |
8a9d2191 RK |
185 | up_read(&nilfs->ns_sem); |
186 | ||
187 | inode_size = nilfs->ns_inode_size; | |
188 | ||
189 | err = -ENOMEM; | |
190 | nilfs->ns_dat = nilfs_mdt_new( | |
191 | nilfs, NULL, NILFS_DAT_INO, NILFS_DAT_GFP); | |
192 | if (unlikely(!nilfs->ns_dat)) | |
193 | goto failed; | |
194 | ||
195 | nilfs->ns_gc_dat = nilfs_mdt_new( | |
196 | nilfs, NULL, NILFS_DAT_INO, NILFS_DAT_GFP); | |
197 | if (unlikely(!nilfs->ns_gc_dat)) | |
198 | goto failed_dat; | |
199 | ||
200 | nilfs->ns_cpfile = nilfs_mdt_new( | |
201 | nilfs, NULL, NILFS_CPFILE_INO, NILFS_CPFILE_GFP); | |
202 | if (unlikely(!nilfs->ns_cpfile)) | |
203 | goto failed_gc_dat; | |
204 | ||
205 | nilfs->ns_sufile = nilfs_mdt_new( | |
206 | nilfs, NULL, NILFS_SUFILE_INO, NILFS_SUFILE_GFP); | |
207 | if (unlikely(!nilfs->ns_sufile)) | |
208 | goto failed_cpfile; | |
209 | ||
210 | err = nilfs_palloc_init_blockgroup(nilfs->ns_dat, dat_entry_size); | |
211 | if (unlikely(err)) | |
212 | goto failed_sufile; | |
213 | ||
214 | err = nilfs_palloc_init_blockgroup(nilfs->ns_gc_dat, dat_entry_size); | |
215 | if (unlikely(err)) | |
216 | goto failed_sufile; | |
217 | ||
c2698e50 RK |
218 | lockdep_set_class(&NILFS_MDT(nilfs->ns_dat)->mi_sem, &dat_lock_key); |
219 | lockdep_set_class(&NILFS_MDT(nilfs->ns_gc_dat)->mi_sem, &dat_lock_key); | |
220 | ||
8a9d2191 RK |
221 | nilfs_mdt_set_shadow(nilfs->ns_dat, nilfs->ns_gc_dat); |
222 | nilfs_mdt_set_entry_size(nilfs->ns_cpfile, checkpoint_size, | |
223 | sizeof(struct nilfs_cpfile_header)); | |
224 | nilfs_mdt_set_entry_size(nilfs->ns_sufile, segment_usage_size, | |
225 | sizeof(struct nilfs_sufile_header)); | |
226 | ||
227 | err = nilfs_mdt_read_inode_direct( | |
228 | nilfs->ns_dat, bh_sr, NILFS_SR_DAT_OFFSET(inode_size)); | |
229 | if (unlikely(err)) | |
230 | goto failed_sufile; | |
231 | ||
232 | err = nilfs_mdt_read_inode_direct( | |
233 | nilfs->ns_cpfile, bh_sr, NILFS_SR_CPFILE_OFFSET(inode_size)); | |
234 | if (unlikely(err)) | |
235 | goto failed_sufile; | |
236 | ||
237 | err = nilfs_mdt_read_inode_direct( | |
238 | nilfs->ns_sufile, bh_sr, NILFS_SR_SUFILE_OFFSET(inode_size)); | |
239 | if (unlikely(err)) | |
240 | goto failed_sufile; | |
241 | ||
242 | raw_sr = (struct nilfs_super_root *)bh_sr->b_data; | |
243 | nilfs->ns_nongc_ctime = le64_to_cpu(raw_sr->sr_nongc_ctime); | |
244 | ||
245 | failed: | |
246 | brelse(bh_sr); | |
247 | return err; | |
248 | ||
249 | failed_sufile: | |
250 | nilfs_mdt_destroy(nilfs->ns_sufile); | |
251 | ||
252 | failed_cpfile: | |
253 | nilfs_mdt_destroy(nilfs->ns_cpfile); | |
254 | ||
255 | failed_gc_dat: | |
256 | nilfs_mdt_destroy(nilfs->ns_gc_dat); | |
257 | ||
258 | failed_dat: | |
259 | nilfs_mdt_destroy(nilfs->ns_dat); | |
260 | goto failed; | |
261 | } | |
262 | ||
263 | static void nilfs_init_recovery_info(struct nilfs_recovery_info *ri) | |
264 | { | |
265 | memset(ri, 0, sizeof(*ri)); | |
266 | INIT_LIST_HEAD(&ri->ri_used_segments); | |
267 | } | |
268 | ||
269 | static void nilfs_clear_recovery_info(struct nilfs_recovery_info *ri) | |
270 | { | |
271 | nilfs_dispose_segment_list(&ri->ri_used_segments); | |
272 | } | |
273 | ||
274 | /** | |
275 | * load_nilfs - load and recover the nilfs | |
276 | * @nilfs: the_nilfs structure to be released | |
277 | * @sbi: nilfs_sb_info used to recover past segment | |
278 | * | |
279 | * load_nilfs() searches and load the latest super root, | |
280 | * attaches the last segment, and does recovery if needed. | |
281 | * The caller must call this exclusively for simultaneous mounts. | |
282 | */ | |
283 | int load_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi) | |
284 | { | |
285 | struct nilfs_recovery_info ri; | |
286 | unsigned int s_flags = sbi->s_super->s_flags; | |
287 | int really_read_only = bdev_read_only(nilfs->ns_bdev); | |
288 | unsigned valid_fs; | |
289 | int err = 0; | |
290 | ||
291 | nilfs_init_recovery_info(&ri); | |
292 | ||
293 | down_write(&nilfs->ns_sem); | |
294 | valid_fs = (nilfs->ns_mount_state & NILFS_VALID_FS); | |
295 | up_write(&nilfs->ns_sem); | |
296 | ||
297 | if (!valid_fs && (s_flags & MS_RDONLY)) { | |
298 | printk(KERN_INFO "NILFS: INFO: recovery " | |
299 | "required for readonly filesystem.\n"); | |
300 | if (really_read_only) { | |
301 | printk(KERN_ERR "NILFS: write access " | |
302 | "unavailable, cannot proceed.\n"); | |
303 | err = -EROFS; | |
304 | goto failed; | |
305 | } | |
306 | printk(KERN_INFO "NILFS: write access will " | |
307 | "be enabled during recovery.\n"); | |
308 | sbi->s_super->s_flags &= ~MS_RDONLY; | |
309 | } | |
310 | ||
311 | err = nilfs_search_super_root(nilfs, sbi, &ri); | |
312 | if (unlikely(err)) { | |
313 | printk(KERN_ERR "NILFS: error searching super root.\n"); | |
314 | goto failed; | |
315 | } | |
316 | ||
317 | err = nilfs_load_super_root(nilfs, sbi, ri.ri_super_root); | |
318 | if (unlikely(err)) { | |
319 | printk(KERN_ERR "NILFS: error loading super root.\n"); | |
320 | goto failed; | |
321 | } | |
322 | ||
323 | if (!valid_fs) { | |
324 | err = nilfs_recover_logical_segments(nilfs, sbi, &ri); | |
325 | if (unlikely(err)) { | |
326 | nilfs_mdt_destroy(nilfs->ns_cpfile); | |
327 | nilfs_mdt_destroy(nilfs->ns_sufile); | |
328 | nilfs_mdt_destroy(nilfs->ns_dat); | |
329 | goto failed; | |
330 | } | |
e339ad31 RK |
331 | if (ri.ri_need_recovery == NILFS_RECOVERY_SR_UPDATED) |
332 | sbi->s_super->s_dirt = 1; | |
8a9d2191 RK |
333 | } |
334 | ||
335 | set_nilfs_loaded(nilfs); | |
336 | ||
337 | failed: | |
338 | nilfs_clear_recovery_info(&ri); | |
339 | sbi->s_super->s_flags = s_flags; | |
340 | return err; | |
341 | } | |
342 | ||
343 | static unsigned long long nilfs_max_size(unsigned int blkbits) | |
344 | { | |
345 | unsigned int max_bits; | |
346 | unsigned long long res = MAX_LFS_FILESIZE; /* page cache limit */ | |
347 | ||
348 | max_bits = blkbits + NILFS_BMAP_KEY_BIT; /* bmap size limit */ | |
349 | if (max_bits < 64) | |
350 | res = min_t(unsigned long long, res, (1ULL << max_bits) - 1); | |
351 | return res; | |
352 | } | |
353 | ||
e339ad31 RK |
354 | static int nilfs_store_disk_layout(struct the_nilfs *nilfs, |
355 | struct nilfs_super_block *sbp) | |
8a9d2191 RK |
356 | { |
357 | if (le32_to_cpu(sbp->s_rev_level) != NILFS_CURRENT_REV) { | |
358 | printk(KERN_ERR "NILFS: revision mismatch " | |
359 | "(superblock rev.=%d.%d, current rev.=%d.%d). " | |
360 | "Please check the version of mkfs.nilfs.\n", | |
361 | le32_to_cpu(sbp->s_rev_level), | |
362 | le16_to_cpu(sbp->s_minor_rev_level), | |
363 | NILFS_CURRENT_REV, NILFS_MINOR_REV); | |
364 | return -EINVAL; | |
365 | } | |
e339ad31 RK |
366 | nilfs->ns_sbsize = le16_to_cpu(sbp->s_bytes); |
367 | if (nilfs->ns_sbsize > BLOCK_SIZE) | |
368 | return -EINVAL; | |
369 | ||
8a9d2191 RK |
370 | nilfs->ns_inode_size = le16_to_cpu(sbp->s_inode_size); |
371 | nilfs->ns_first_ino = le32_to_cpu(sbp->s_first_ino); | |
372 | ||
373 | nilfs->ns_blocks_per_segment = le32_to_cpu(sbp->s_blocks_per_segment); | |
374 | if (nilfs->ns_blocks_per_segment < NILFS_SEG_MIN_BLOCKS) { | |
375 | printk(KERN_ERR "NILFS: too short segment. \n"); | |
376 | return -EINVAL; | |
377 | } | |
378 | ||
379 | nilfs->ns_first_data_block = le64_to_cpu(sbp->s_first_data_block); | |
380 | nilfs->ns_nsegments = le64_to_cpu(sbp->s_nsegments); | |
381 | nilfs->ns_r_segments_percentage = | |
382 | le32_to_cpu(sbp->s_r_segments_percentage); | |
383 | nilfs->ns_nrsvsegs = | |
384 | max_t(unsigned long, NILFS_MIN_NRSVSEGS, | |
385 | DIV_ROUND_UP(nilfs->ns_nsegments * | |
386 | nilfs->ns_r_segments_percentage, 100)); | |
387 | nilfs->ns_crc_seed = le32_to_cpu(sbp->s_crc_seed); | |
388 | return 0; | |
389 | } | |
390 | ||
e339ad31 RK |
391 | static int nilfs_valid_sb(struct nilfs_super_block *sbp) |
392 | { | |
393 | static unsigned char sum[4]; | |
394 | const int sumoff = offsetof(struct nilfs_super_block, s_sum); | |
395 | size_t bytes; | |
396 | u32 crc; | |
397 | ||
398 | if (!sbp || le16_to_cpu(sbp->s_magic) != NILFS_SUPER_MAGIC) | |
399 | return 0; | |
400 | bytes = le16_to_cpu(sbp->s_bytes); | |
401 | if (bytes > BLOCK_SIZE) | |
402 | return 0; | |
403 | crc = crc32_le(le32_to_cpu(sbp->s_crc_seed), (unsigned char *)sbp, | |
404 | sumoff); | |
405 | crc = crc32_le(crc, sum, 4); | |
406 | crc = crc32_le(crc, (unsigned char *)sbp + sumoff + 4, | |
407 | bytes - sumoff - 4); | |
408 | return crc == le32_to_cpu(sbp->s_sum); | |
409 | } | |
410 | ||
411 | static int nilfs_sb2_bad_offset(struct nilfs_super_block *sbp, u64 offset) | |
412 | { | |
413 | return offset < ((le64_to_cpu(sbp->s_nsegments) * | |
414 | le32_to_cpu(sbp->s_blocks_per_segment)) << | |
415 | (le32_to_cpu(sbp->s_log_block_size) + 10)); | |
416 | } | |
417 | ||
418 | static void nilfs_release_super_block(struct the_nilfs *nilfs) | |
419 | { | |
420 | int i; | |
421 | ||
422 | for (i = 0; i < 2; i++) { | |
423 | if (nilfs->ns_sbp[i]) { | |
424 | brelse(nilfs->ns_sbh[i]); | |
425 | nilfs->ns_sbh[i] = NULL; | |
426 | nilfs->ns_sbp[i] = NULL; | |
427 | } | |
428 | } | |
429 | } | |
430 | ||
431 | void nilfs_fall_back_super_block(struct the_nilfs *nilfs) | |
432 | { | |
433 | brelse(nilfs->ns_sbh[0]); | |
434 | nilfs->ns_sbh[0] = nilfs->ns_sbh[1]; | |
435 | nilfs->ns_sbp[0] = nilfs->ns_sbp[1]; | |
436 | nilfs->ns_sbh[1] = NULL; | |
437 | nilfs->ns_sbp[1] = NULL; | |
438 | } | |
439 | ||
440 | void nilfs_swap_super_block(struct the_nilfs *nilfs) | |
441 | { | |
442 | struct buffer_head *tsbh = nilfs->ns_sbh[0]; | |
443 | struct nilfs_super_block *tsbp = nilfs->ns_sbp[0]; | |
444 | ||
445 | nilfs->ns_sbh[0] = nilfs->ns_sbh[1]; | |
446 | nilfs->ns_sbp[0] = nilfs->ns_sbp[1]; | |
447 | nilfs->ns_sbh[1] = tsbh; | |
448 | nilfs->ns_sbp[1] = tsbp; | |
449 | } | |
450 | ||
451 | static int nilfs_load_super_block(struct the_nilfs *nilfs, | |
452 | struct super_block *sb, int blocksize, | |
453 | struct nilfs_super_block **sbpp) | |
454 | { | |
455 | struct nilfs_super_block **sbp = nilfs->ns_sbp; | |
456 | struct buffer_head **sbh = nilfs->ns_sbh; | |
457 | u64 sb2off = NILFS_SB2_OFFSET_BYTES(nilfs->ns_bdev->bd_inode->i_size); | |
458 | int valid[2], swp = 0; | |
459 | ||
460 | sbp[0] = nilfs_read_super_block(sb, NILFS_SB_OFFSET_BYTES, blocksize, | |
461 | &sbh[0]); | |
462 | sbp[1] = nilfs_read_super_block(sb, sb2off, blocksize, &sbh[1]); | |
463 | ||
464 | if (!sbp[0]) { | |
465 | if (!sbp[1]) { | |
466 | printk(KERN_ERR "NILFS: unable to read superblock\n"); | |
467 | return -EIO; | |
468 | } | |
469 | printk(KERN_WARNING | |
470 | "NILFS warning: unable to read primary superblock\n"); | |
471 | } else if (!sbp[1]) | |
472 | printk(KERN_WARNING | |
473 | "NILFS warning: unable to read secondary superblock\n"); | |
474 | ||
475 | valid[0] = nilfs_valid_sb(sbp[0]); | |
476 | valid[1] = nilfs_valid_sb(sbp[1]); | |
477 | swp = valid[1] && | |
478 | (!valid[0] || | |
479 | le64_to_cpu(sbp[1]->s_wtime) > le64_to_cpu(sbp[0]->s_wtime)); | |
480 | ||
481 | if (valid[swp] && nilfs_sb2_bad_offset(sbp[swp], sb2off)) { | |
482 | brelse(sbh[1]); | |
483 | sbh[1] = NULL; | |
484 | sbp[1] = NULL; | |
485 | swp = 0; | |
486 | } | |
487 | if (!valid[swp]) { | |
488 | nilfs_release_super_block(nilfs); | |
489 | printk(KERN_ERR "NILFS: Can't find nilfs on dev %s.\n", | |
490 | sb->s_id); | |
491 | return -EINVAL; | |
492 | } | |
493 | ||
494 | if (swp) { | |
495 | printk(KERN_WARNING "NILFS warning: broken superblock. " | |
496 | "using spare superblock.\n"); | |
497 | nilfs_swap_super_block(nilfs); | |
498 | } | |
499 | ||
500 | nilfs->ns_sbwtime[0] = le64_to_cpu(sbp[0]->s_wtime); | |
501 | nilfs->ns_sbwtime[1] = valid[!swp] ? le64_to_cpu(sbp[1]->s_wtime) : 0; | |
502 | nilfs->ns_prot_seq = le64_to_cpu(sbp[valid[1] & !swp]->s_last_seq); | |
503 | *sbpp = sbp[0]; | |
504 | return 0; | |
505 | } | |
506 | ||
8a9d2191 RK |
507 | /** |
508 | * init_nilfs - initialize a NILFS instance. | |
509 | * @nilfs: the_nilfs structure | |
510 | * @sbi: nilfs_sb_info | |
511 | * @sb: super block | |
512 | * @data: mount options | |
513 | * | |
514 | * init_nilfs() performs common initialization per block device (e.g. | |
515 | * reading the super block, getting disk layout information, initializing | |
516 | * shared fields in the_nilfs). It takes on some portion of the jobs | |
517 | * typically done by a fill_super() routine. This division arises from | |
518 | * the nature that multiple NILFS instances may be simultaneously | |
519 | * mounted on a device. | |
520 | * For multiple mounts on the same device, only the first mount | |
521 | * invokes these tasks. | |
522 | * | |
523 | * Return Value: On success, 0 is returned. On error, a negative error | |
524 | * code is returned. | |
525 | */ | |
526 | int init_nilfs(struct the_nilfs *nilfs, struct nilfs_sb_info *sbi, char *data) | |
527 | { | |
528 | struct super_block *sb = sbi->s_super; | |
8a9d2191 RK |
529 | struct nilfs_super_block *sbp; |
530 | struct backing_dev_info *bdi; | |
531 | int blocksize; | |
e339ad31 | 532 | int err; |
8a9d2191 RK |
533 | |
534 | down_write(&nilfs->ns_sem); | |
535 | if (nilfs_init(nilfs)) { | |
536 | /* Load values from existing the_nilfs */ | |
e339ad31 | 537 | sbp = nilfs->ns_sbp[0]; |
8a9d2191 RK |
538 | err = nilfs_store_magic_and_option(sb, sbp, data); |
539 | if (err) | |
540 | goto out; | |
541 | ||
542 | blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size); | |
543 | if (sb->s_blocksize != blocksize && | |
544 | !sb_set_blocksize(sb, blocksize)) { | |
545 | printk(KERN_ERR "NILFS: blocksize %d unfit to device\n", | |
546 | blocksize); | |
547 | err = -EINVAL; | |
548 | } | |
549 | sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits); | |
550 | goto out; | |
551 | } | |
552 | ||
e339ad31 RK |
553 | blocksize = sb_min_blocksize(sb, BLOCK_SIZE); |
554 | if (!blocksize) { | |
555 | printk(KERN_ERR "NILFS: unable to set blocksize\n"); | |
8a9d2191 RK |
556 | err = -EINVAL; |
557 | goto out; | |
558 | } | |
e339ad31 RK |
559 | err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp); |
560 | if (err) | |
561 | goto out; | |
562 | ||
8a9d2191 RK |
563 | err = nilfs_store_magic_and_option(sb, sbp, data); |
564 | if (err) | |
565 | goto failed_sbh; | |
566 | ||
567 | blocksize = BLOCK_SIZE << le32_to_cpu(sbp->s_log_block_size); | |
568 | if (sb->s_blocksize != blocksize) { | |
e1defc4f | 569 | int hw_blocksize = bdev_logical_block_size(sb->s_bdev); |
e339ad31 RK |
570 | |
571 | if (blocksize < hw_blocksize) { | |
572 | printk(KERN_ERR | |
573 | "NILFS: blocksize %d too small for device " | |
574 | "(sector-size = %d).\n", | |
575 | blocksize, hw_blocksize); | |
8a9d2191 | 576 | err = -EINVAL; |
e339ad31 RK |
577 | goto failed_sbh; |
578 | } | |
579 | nilfs_release_super_block(nilfs); | |
580 | sb_set_blocksize(sb, blocksize); | |
581 | ||
582 | err = nilfs_load_super_block(nilfs, sb, blocksize, &sbp); | |
583 | if (err) | |
8a9d2191 RK |
584 | goto out; |
585 | /* not failed_sbh; sbh is released automatically | |
586 | when reloading fails. */ | |
8a9d2191 RK |
587 | } |
588 | nilfs->ns_blocksize_bits = sb->s_blocksize_bits; | |
589 | ||
e339ad31 | 590 | err = nilfs_store_disk_layout(nilfs, sbp); |
8a9d2191 RK |
591 | if (err) |
592 | goto failed_sbh; | |
593 | ||
594 | sb->s_maxbytes = nilfs_max_size(sb->s_blocksize_bits); | |
595 | ||
596 | nilfs->ns_mount_state = le16_to_cpu(sbp->s_state); | |
8a9d2191 RK |
597 | |
598 | bdi = nilfs->ns_bdev->bd_inode_backing_dev_info; | |
599 | if (!bdi) | |
600 | bdi = nilfs->ns_bdev->bd_inode->i_mapping->backing_dev_info; | |
601 | nilfs->ns_bdi = bdi ? : &default_backing_dev_info; | |
602 | ||
603 | /* Finding last segment */ | |
604 | nilfs->ns_last_pseg = le64_to_cpu(sbp->s_last_pseg); | |
605 | nilfs->ns_last_cno = le64_to_cpu(sbp->s_last_cno); | |
606 | nilfs->ns_last_seq = le64_to_cpu(sbp->s_last_seq); | |
607 | ||
608 | nilfs->ns_seg_seq = nilfs->ns_last_seq; | |
609 | nilfs->ns_segnum = | |
610 | nilfs_get_segnum_of_block(nilfs, nilfs->ns_last_pseg); | |
611 | nilfs->ns_cno = nilfs->ns_last_cno + 1; | |
612 | if (nilfs->ns_segnum >= nilfs->ns_nsegments) { | |
613 | printk(KERN_ERR "NILFS invalid last segment number.\n"); | |
614 | err = -EINVAL; | |
615 | goto failed_sbh; | |
616 | } | |
617 | /* Dummy values */ | |
618 | nilfs->ns_free_segments_count = | |
619 | nilfs->ns_nsegments - (nilfs->ns_segnum + 1); | |
620 | ||
621 | /* Initialize gcinode cache */ | |
622 | err = nilfs_init_gccache(nilfs); | |
623 | if (err) | |
624 | goto failed_sbh; | |
625 | ||
626 | set_nilfs_init(nilfs); | |
627 | err = 0; | |
628 | out: | |
629 | up_write(&nilfs->ns_sem); | |
630 | return err; | |
631 | ||
632 | failed_sbh: | |
e339ad31 | 633 | nilfs_release_super_block(nilfs); |
8a9d2191 RK |
634 | goto out; |
635 | } | |
636 | ||
637 | int nilfs_count_free_blocks(struct the_nilfs *nilfs, sector_t *nblocks) | |
638 | { | |
639 | struct inode *dat = nilfs_dat_inode(nilfs); | |
640 | unsigned long ncleansegs; | |
641 | int err; | |
642 | ||
643 | down_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ | |
644 | err = nilfs_sufile_get_ncleansegs(nilfs->ns_sufile, &ncleansegs); | |
645 | up_read(&NILFS_MDT(dat)->mi_sem); /* XXX */ | |
646 | if (likely(!err)) | |
647 | *nblocks = (sector_t)ncleansegs * nilfs->ns_blocks_per_segment; | |
648 | return err; | |
649 | } | |
650 | ||
8a9d2191 RK |
651 | int nilfs_near_disk_full(struct the_nilfs *nilfs) |
652 | { | |
653 | struct inode *sufile = nilfs->ns_sufile; | |
654 | unsigned long ncleansegs, nincsegs; | |
655 | int ret; | |
656 | ||
657 | ret = nilfs_sufile_get_ncleansegs(sufile, &ncleansegs); | |
658 | if (likely(!ret)) { | |
659 | nincsegs = atomic_read(&nilfs->ns_ndirtyblks) / | |
660 | nilfs->ns_blocks_per_segment + 1; | |
661 | if (ncleansegs <= nilfs->ns_nrsvsegs + nincsegs) | |
662 | ret++; | |
663 | } | |
664 | return ret; | |
665 | } | |
666 | ||
667 | int nilfs_checkpoint_is_mounted(struct the_nilfs *nilfs, __u64 cno, | |
668 | int snapshot_mount) | |
669 | { | |
670 | struct nilfs_sb_info *sbi; | |
671 | int ret = 0; | |
672 | ||
673 | down_read(&nilfs->ns_sem); | |
674 | if (cno == 0 || cno > nilfs->ns_cno) | |
675 | goto out_unlock; | |
676 | ||
677 | list_for_each_entry(sbi, &nilfs->ns_supers, s_list) { | |
678 | if (sbi->s_snapshot_cno == cno && | |
679 | (!snapshot_mount || nilfs_test_opt(sbi, SNAPSHOT))) { | |
680 | /* exclude read-only mounts */ | |
681 | ret++; | |
682 | break; | |
683 | } | |
684 | } | |
685 | /* for protecting recent checkpoints */ | |
686 | if (cno >= nilfs_last_cno(nilfs)) | |
687 | ret++; | |
688 | ||
689 | out_unlock: | |
690 | up_read(&nilfs->ns_sem); | |
691 | return ret; | |
692 | } |