Commit | Line | Data |
---|---|---|
783f6184 RK |
1 | /* |
2 | * super.c - NILFS module and super block management. | |
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 | * linux/fs/ext2/super.c | |
24 | * | |
25 | * Copyright (C) 1992, 1993, 1994, 1995 | |
26 | * Remy Card (card@masi.ibp.fr) | |
27 | * Laboratoire MASI - Institut Blaise Pascal | |
28 | * Universite Pierre et Marie Curie (Paris VI) | |
29 | * | |
30 | * from | |
31 | * | |
32 | * linux/fs/minix/inode.c | |
33 | * | |
34 | * Copyright (C) 1991, 1992 Linus Torvalds | |
35 | * | |
36 | * Big-endian to little-endian byte-swapping/bitmaps by | |
37 | * David S. Miller (davem@caip.rutgers.edu), 1995 | |
38 | */ | |
39 | ||
40 | #include <linux/module.h> | |
41 | #include <linux/string.h> | |
42 | #include <linux/slab.h> | |
43 | #include <linux/init.h> | |
44 | #include <linux/blkdev.h> | |
45 | #include <linux/parser.h> | |
46 | #include <linux/random.h> | |
47 | #include <linux/crc32.h> | |
48 | #include <linux/smp_lock.h> | |
49 | #include <linux/vfs.h> | |
50 | #include <linux/writeback.h> | |
51 | #include <linux/kobject.h> | |
52 | #include <linux/exportfs.h> | |
b58a285b JS |
53 | #include <linux/seq_file.h> |
54 | #include <linux/mount.h> | |
783f6184 RK |
55 | #include "nilfs.h" |
56 | #include "mdt.h" | |
57 | #include "alloc.h" | |
58 | #include "page.h" | |
59 | #include "cpfile.h" | |
60 | #include "ifile.h" | |
61 | #include "dat.h" | |
62 | #include "segment.h" | |
63 | #include "segbuf.h" | |
64 | ||
65 | MODULE_AUTHOR("NTT Corp."); | |
66 | MODULE_DESCRIPTION("A New Implementation of the Log-structured Filesystem " | |
67 | "(NILFS)"); | |
783f6184 RK |
68 | MODULE_LICENSE("GPL"); |
69 | ||
41c88bd7 LH |
70 | struct kmem_cache *nilfs_inode_cachep; |
71 | struct kmem_cache *nilfs_transaction_cachep; | |
72 | struct kmem_cache *nilfs_segbuf_cachep; | |
73 | struct kmem_cache *nilfs_btree_path_cache; | |
74 | ||
783f6184 | 75 | static int nilfs_remount(struct super_block *sb, int *flags, char *data); |
783f6184 | 76 | |
c8a11c8a RK |
77 | static void nilfs_set_error(struct nilfs_sb_info *sbi) |
78 | { | |
79 | struct the_nilfs *nilfs = sbi->s_nilfs; | |
d26493b6 | 80 | struct nilfs_super_block **sbp; |
c8a11c8a RK |
81 | |
82 | down_write(&nilfs->ns_sem); | |
83 | if (!(nilfs->ns_mount_state & NILFS_ERROR_FS)) { | |
84 | nilfs->ns_mount_state |= NILFS_ERROR_FS; | |
d26493b6 JS |
85 | sbp = nilfs_prepare_super(sbi); |
86 | if (likely(sbp)) { | |
87 | sbp[0]->s_state |= cpu_to_le16(NILFS_ERROR_FS); | |
88 | nilfs_commit_super(sbi, 1); | |
89 | } | |
c8a11c8a RK |
90 | } |
91 | up_write(&nilfs->ns_sem); | |
92 | } | |
93 | ||
783f6184 RK |
94 | /** |
95 | * nilfs_error() - report failure condition on a filesystem | |
96 | * | |
97 | * nilfs_error() sets an ERROR_FS flag on the superblock as well as | |
98 | * reporting an error message. It should be called when NILFS detects | |
99 | * incoherences or defects of meta data on disk. As for sustainable | |
100 | * errors such as a single-shot I/O error, nilfs_warning() or the printk() | |
101 | * function should be used instead. | |
102 | * | |
103 | * The segment constructor must not call this function because it can | |
104 | * kill itself. | |
105 | */ | |
106 | void nilfs_error(struct super_block *sb, const char *function, | |
107 | const char *fmt, ...) | |
108 | { | |
109 | struct nilfs_sb_info *sbi = NILFS_SB(sb); | |
110 | va_list args; | |
111 | ||
112 | va_start(args, fmt); | |
113 | printk(KERN_CRIT "NILFS error (device %s): %s: ", sb->s_id, function); | |
114 | vprintk(fmt, args); | |
115 | printk("\n"); | |
116 | va_end(args); | |
117 | ||
118 | if (!(sb->s_flags & MS_RDONLY)) { | |
c8a11c8a | 119 | nilfs_set_error(sbi); |
783f6184 RK |
120 | |
121 | if (nilfs_test_opt(sbi, ERRORS_RO)) { | |
122 | printk(KERN_CRIT "Remounting filesystem read-only\n"); | |
123 | sb->s_flags |= MS_RDONLY; | |
124 | } | |
125 | } | |
126 | ||
127 | if (nilfs_test_opt(sbi, ERRORS_PANIC)) | |
128 | panic("NILFS (device %s): panic forced after error\n", | |
129 | sb->s_id); | |
130 | } | |
131 | ||
132 | void nilfs_warning(struct super_block *sb, const char *function, | |
133 | const char *fmt, ...) | |
134 | { | |
135 | va_list args; | |
136 | ||
137 | va_start(args, fmt); | |
138 | printk(KERN_WARNING "NILFS warning (device %s): %s: ", | |
139 | sb->s_id, function); | |
140 | vprintk(fmt, args); | |
141 | printk("\n"); | |
142 | va_end(args); | |
143 | } | |
144 | ||
783f6184 | 145 | |
a53b4751 | 146 | struct inode *nilfs_alloc_inode_common(struct the_nilfs *nilfs) |
783f6184 RK |
147 | { |
148 | struct nilfs_inode_info *ii; | |
149 | ||
150 | ii = kmem_cache_alloc(nilfs_inode_cachep, GFP_NOFS); | |
151 | if (!ii) | |
152 | return NULL; | |
153 | ii->i_bh = NULL; | |
154 | ii->i_state = 0; | |
155 | ii->vfs_inode.i_version = 1; | |
a53b4751 | 156 | nilfs_btnode_cache_init(&ii->i_btnode_cache, nilfs->ns_bdi); |
783f6184 RK |
157 | return &ii->vfs_inode; |
158 | } | |
159 | ||
a53b4751 RK |
160 | struct inode *nilfs_alloc_inode(struct super_block *sb) |
161 | { | |
162 | return nilfs_alloc_inode_common(NILFS_SB(sb)->s_nilfs); | |
163 | } | |
164 | ||
783f6184 RK |
165 | void nilfs_destroy_inode(struct inode *inode) |
166 | { | |
167 | kmem_cache_free(nilfs_inode_cachep, NILFS_I(inode)); | |
168 | } | |
169 | ||
783f6184 RK |
170 | static void nilfs_clear_inode(struct inode *inode) |
171 | { | |
172 | struct nilfs_inode_info *ii = NILFS_I(inode); | |
783f6184 | 173 | |
783f6184 RK |
174 | /* |
175 | * Free resources allocated in nilfs_read_inode(), here. | |
176 | */ | |
a2e7d2df | 177 | BUG_ON(!list_empty(&ii->i_dirty)); |
783f6184 RK |
178 | brelse(ii->i_bh); |
179 | ii->i_bh = NULL; | |
783f6184 RK |
180 | |
181 | if (test_bit(NILFS_I_BMAP, &ii->i_state)) | |
182 | nilfs_bmap_clear(ii->i_bmap); | |
183 | ||
184 | nilfs_btnode_cache_clear(&ii->i_btnode_cache); | |
783f6184 RK |
185 | } |
186 | ||
e339ad31 | 187 | static int nilfs_sync_super(struct nilfs_sb_info *sbi, int dupsb) |
783f6184 RK |
188 | { |
189 | struct the_nilfs *nilfs = sbi->s_nilfs; | |
190 | int err; | |
191 | int barrier_done = 0; | |
192 | ||
193 | if (nilfs_test_opt(sbi, BARRIER)) { | |
e339ad31 | 194 | set_buffer_ordered(nilfs->ns_sbh[0]); |
783f6184 RK |
195 | barrier_done = 1; |
196 | } | |
197 | retry: | |
e339ad31 RK |
198 | set_buffer_dirty(nilfs->ns_sbh[0]); |
199 | err = sync_dirty_buffer(nilfs->ns_sbh[0]); | |
783f6184 RK |
200 | if (err == -EOPNOTSUPP && barrier_done) { |
201 | nilfs_warning(sbi->s_super, __func__, | |
202 | "barrier-based sync failed. " | |
203 | "disabling barriers\n"); | |
204 | nilfs_clear_opt(sbi, BARRIER); | |
205 | barrier_done = 0; | |
e339ad31 | 206 | clear_buffer_ordered(nilfs->ns_sbh[0]); |
783f6184 RK |
207 | goto retry; |
208 | } | |
e339ad31 | 209 | if (unlikely(err)) { |
783f6184 RK |
210 | printk(KERN_ERR |
211 | "NILFS: unable to write superblock (err=%d)\n", err); | |
e339ad31 RK |
212 | if (err == -EIO && nilfs->ns_sbh[1]) { |
213 | nilfs_fall_back_super_block(nilfs); | |
214 | goto retry; | |
215 | } | |
216 | } else { | |
217 | struct nilfs_super_block *sbp = nilfs->ns_sbp[0]; | |
218 | ||
219 | /* | |
220 | * The latest segment becomes trailable from the position | |
221 | * written in superblock. | |
222 | */ | |
783f6184 | 223 | clear_nilfs_discontinued(nilfs); |
e339ad31 RK |
224 | |
225 | /* update GC protection for recent segments */ | |
226 | if (nilfs->ns_sbh[1]) { | |
227 | sbp = NULL; | |
228 | if (dupsb) { | |
229 | set_buffer_dirty(nilfs->ns_sbh[1]); | |
230 | if (!sync_dirty_buffer(nilfs->ns_sbh[1])) | |
231 | sbp = nilfs->ns_sbp[1]; | |
232 | } | |
233 | } | |
234 | if (sbp) { | |
235 | spin_lock(&nilfs->ns_last_segment_lock); | |
236 | nilfs->ns_prot_seq = le64_to_cpu(sbp->s_last_seq); | |
237 | spin_unlock(&nilfs->ns_last_segment_lock); | |
238 | } | |
783f6184 RK |
239 | } |
240 | ||
241 | return err; | |
242 | } | |
243 | ||
60f46b7e RK |
244 | void nilfs_set_log_cursor(struct nilfs_super_block *sbp, |
245 | struct the_nilfs *nilfs) | |
246 | { | |
247 | sector_t nfreeblocks; | |
248 | ||
249 | /* nilfs->ns_sem must be locked by the caller. */ | |
250 | nilfs_count_free_blocks(nilfs, &nfreeblocks); | |
251 | sbp->s_free_blocks_count = cpu_to_le64(nfreeblocks); | |
252 | ||
253 | spin_lock(&nilfs->ns_last_segment_lock); | |
254 | sbp->s_last_seq = cpu_to_le64(nilfs->ns_last_seq); | |
255 | sbp->s_last_pseg = cpu_to_le64(nilfs->ns_last_pseg); | |
256 | sbp->s_last_cno = cpu_to_le64(nilfs->ns_last_cno); | |
257 | spin_unlock(&nilfs->ns_last_segment_lock); | |
258 | } | |
259 | ||
d26493b6 | 260 | struct nilfs_super_block **nilfs_prepare_super(struct nilfs_sb_info *sbi) |
783f6184 RK |
261 | { |
262 | struct the_nilfs *nilfs = sbi->s_nilfs; | |
e339ad31 | 263 | struct nilfs_super_block **sbp = nilfs->ns_sbp; |
783f6184 | 264 | |
d26493b6 | 265 | /* nilfs->ns_sem must be locked by the caller. */ |
34cb9b5c | 266 | if (sbp[0]->s_magic != cpu_to_le16(NILFS_SUPER_MAGIC)) { |
d26493b6 JS |
267 | if (sbp[1] && |
268 | sbp[1]->s_magic == cpu_to_le16(NILFS_SUPER_MAGIC)) { | |
e339ad31 | 269 | nilfs_swap_super_block(nilfs); |
d26493b6 | 270 | } else { |
e339ad31 RK |
271 | printk(KERN_CRIT "NILFS: superblock broke on dev %s\n", |
272 | sbi->s_super->s_id); | |
d26493b6 | 273 | return NULL; |
e339ad31 RK |
274 | } |
275 | } | |
d26493b6 JS |
276 | return sbp; |
277 | } | |
278 | ||
279 | int nilfs_commit_super(struct nilfs_sb_info *sbi, int dupsb) | |
280 | { | |
281 | struct the_nilfs *nilfs = sbi->s_nilfs; | |
282 | struct nilfs_super_block **sbp = nilfs->ns_sbp; | |
283 | time_t t; | |
284 | ||
285 | /* nilfs->ns_sem must be locked by the caller. */ | |
60f46b7e | 286 | nilfs_set_log_cursor(sbp[0], nilfs); |
e339ad31 RK |
287 | |
288 | t = get_seconds(); | |
289 | nilfs->ns_sbwtime[0] = t; | |
e339ad31 RK |
290 | sbp[0]->s_wtime = cpu_to_le64(t); |
291 | sbp[0]->s_sum = 0; | |
292 | sbp[0]->s_sum = cpu_to_le32(crc32_le(nilfs->ns_crc_seed, | |
293 | (unsigned char *)sbp[0], | |
294 | nilfs->ns_sbsize)); | |
295 | if (dupsb && sbp[1]) { | |
296 | memcpy(sbp[1], sbp[0], nilfs->ns_sbsize); | |
297 | nilfs->ns_sbwtime[1] = t; | |
298 | } | |
e605f0a7 | 299 | clear_nilfs_sb_dirty(nilfs); |
e339ad31 | 300 | return nilfs_sync_super(sbi, dupsb); |
783f6184 RK |
301 | } |
302 | ||
7ecaa46c RK |
303 | /** |
304 | * nilfs_cleanup_super() - write filesystem state for cleanup | |
305 | * @sbi: nilfs_sb_info to be unmounted or degraded to read-only | |
306 | * | |
307 | * This function restores state flags in the on-disk super block. | |
308 | * This will set "clean" flag (i.e. NILFS_VALID_FS) unless the | |
309 | * filesystem was not clean previously. | |
310 | */ | |
311 | int nilfs_cleanup_super(struct nilfs_sb_info *sbi) | |
312 | { | |
d26493b6 JS |
313 | struct nilfs_super_block **sbp; |
314 | int ret = -EIO; | |
7ecaa46c | 315 | |
d26493b6 JS |
316 | sbp = nilfs_prepare_super(sbi); |
317 | if (sbp) { | |
318 | sbp[0]->s_state = cpu_to_le16(sbi->s_nilfs->ns_mount_state); | |
319 | ret = nilfs_commit_super(sbi, 1); | |
320 | } | |
7ecaa46c RK |
321 | return ret; |
322 | } | |
323 | ||
783f6184 RK |
324 | static void nilfs_put_super(struct super_block *sb) |
325 | { | |
326 | struct nilfs_sb_info *sbi = NILFS_SB(sb); | |
327 | struct the_nilfs *nilfs = sbi->s_nilfs; | |
328 | ||
6cfd0148 CH |
329 | lock_kernel(); |
330 | ||
783f6184 RK |
331 | nilfs_detach_segment_constructor(sbi); |
332 | ||
333 | if (!(sb->s_flags & MS_RDONLY)) { | |
334 | down_write(&nilfs->ns_sem); | |
7ecaa46c | 335 | nilfs_cleanup_super(sbi); |
783f6184 RK |
336 | up_write(&nilfs->ns_sem); |
337 | } | |
e59399d0 | 338 | down_write(&nilfs->ns_super_sem); |
3f82ff55 RK |
339 | if (nilfs->ns_current == sbi) |
340 | nilfs->ns_current = NULL; | |
e59399d0 | 341 | up_write(&nilfs->ns_super_sem); |
783f6184 RK |
342 | |
343 | nilfs_detach_checkpoint(sbi); | |
344 | put_nilfs(sbi->s_nilfs); | |
345 | sbi->s_super = NULL; | |
346 | sb->s_fs_info = NULL; | |
6dd47406 | 347 | nilfs_put_sbinfo(sbi); |
6cfd0148 CH |
348 | |
349 | unlock_kernel(); | |
783f6184 RK |
350 | } |
351 | ||
783f6184 RK |
352 | static int nilfs_sync_fs(struct super_block *sb, int wait) |
353 | { | |
6233caa9 JS |
354 | struct nilfs_sb_info *sbi = NILFS_SB(sb); |
355 | struct the_nilfs *nilfs = sbi->s_nilfs; | |
d26493b6 | 356 | struct nilfs_super_block **sbp; |
783f6184 RK |
357 | int err = 0; |
358 | ||
359 | /* This function is called when super block should be written back */ | |
360 | if (wait) | |
361 | err = nilfs_construct_segment(sb); | |
6233caa9 JS |
362 | |
363 | down_write(&nilfs->ns_sem); | |
d26493b6 JS |
364 | if (nilfs_sb_dirty(nilfs)) { |
365 | sbp = nilfs_prepare_super(sbi); | |
366 | if (likely(sbp)) | |
367 | nilfs_commit_super(sbi, 1); | |
368 | } | |
6233caa9 JS |
369 | up_write(&nilfs->ns_sem); |
370 | ||
783f6184 RK |
371 | return err; |
372 | } | |
373 | ||
374 | int nilfs_attach_checkpoint(struct nilfs_sb_info *sbi, __u64 cno) | |
375 | { | |
376 | struct the_nilfs *nilfs = sbi->s_nilfs; | |
377 | struct nilfs_checkpoint *raw_cp; | |
378 | struct buffer_head *bh_cp; | |
379 | int err; | |
380 | ||
e59399d0 | 381 | down_write(&nilfs->ns_super_sem); |
783f6184 | 382 | list_add(&sbi->s_list, &nilfs->ns_supers); |
e59399d0 | 383 | up_write(&nilfs->ns_super_sem); |
783f6184 | 384 | |
79739565 | 385 | sbi->s_ifile = nilfs_ifile_new(sbi, nilfs->ns_inode_size); |
783f6184 RK |
386 | if (!sbi->s_ifile) |
387 | return -ENOMEM; | |
388 | ||
1154ecbd | 389 | down_read(&nilfs->ns_segctor_sem); |
783f6184 RK |
390 | err = nilfs_cpfile_get_checkpoint(nilfs->ns_cpfile, cno, 0, &raw_cp, |
391 | &bh_cp); | |
1154ecbd | 392 | up_read(&nilfs->ns_segctor_sem); |
783f6184 RK |
393 | if (unlikely(err)) { |
394 | if (err == -ENOENT || err == -EINVAL) { | |
395 | printk(KERN_ERR | |
396 | "NILFS: Invalid checkpoint " | |
397 | "(checkpoint number=%llu)\n", | |
398 | (unsigned long long)cno); | |
399 | err = -EINVAL; | |
400 | } | |
401 | goto failed; | |
402 | } | |
403 | err = nilfs_read_inode_common(sbi->s_ifile, &raw_cp->cp_ifile_inode); | |
404 | if (unlikely(err)) | |
405 | goto failed_bh; | |
406 | atomic_set(&sbi->s_inodes_count, le64_to_cpu(raw_cp->cp_inodes_count)); | |
407 | atomic_set(&sbi->s_blocks_count, le64_to_cpu(raw_cp->cp_blocks_count)); | |
408 | ||
409 | nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp); | |
410 | return 0; | |
411 | ||
412 | failed_bh: | |
413 | nilfs_cpfile_put_checkpoint(nilfs->ns_cpfile, cno, bh_cp); | |
414 | failed: | |
415 | nilfs_mdt_destroy(sbi->s_ifile); | |
416 | sbi->s_ifile = NULL; | |
417 | ||
e59399d0 | 418 | down_write(&nilfs->ns_super_sem); |
783f6184 | 419 | list_del_init(&sbi->s_list); |
e59399d0 | 420 | up_write(&nilfs->ns_super_sem); |
783f6184 RK |
421 | |
422 | return err; | |
423 | } | |
424 | ||
425 | void nilfs_detach_checkpoint(struct nilfs_sb_info *sbi) | |
426 | { | |
427 | struct the_nilfs *nilfs = sbi->s_nilfs; | |
428 | ||
783f6184 RK |
429 | nilfs_mdt_destroy(sbi->s_ifile); |
430 | sbi->s_ifile = NULL; | |
e59399d0 | 431 | down_write(&nilfs->ns_super_sem); |
783f6184 | 432 | list_del_init(&sbi->s_list); |
e59399d0 | 433 | up_write(&nilfs->ns_super_sem); |
783f6184 RK |
434 | } |
435 | ||
783f6184 RK |
436 | static int nilfs_statfs(struct dentry *dentry, struct kstatfs *buf) |
437 | { | |
438 | struct super_block *sb = dentry->d_sb; | |
439 | struct nilfs_sb_info *sbi = NILFS_SB(sb); | |
c306af23 RK |
440 | struct the_nilfs *nilfs = sbi->s_nilfs; |
441 | u64 id = huge_encode_dev(sb->s_bdev->bd_dev); | |
783f6184 RK |
442 | unsigned long long blocks; |
443 | unsigned long overhead; | |
444 | unsigned long nrsvblocks; | |
445 | sector_t nfreeblocks; | |
783f6184 RK |
446 | int err; |
447 | ||
448 | /* | |
449 | * Compute all of the segment blocks | |
450 | * | |
451 | * The blocks before first segment and after last segment | |
452 | * are excluded. | |
453 | */ | |
454 | blocks = nilfs->ns_blocks_per_segment * nilfs->ns_nsegments | |
455 | - nilfs->ns_first_data_block; | |
456 | nrsvblocks = nilfs->ns_nrsvsegs * nilfs->ns_blocks_per_segment; | |
457 | ||
458 | /* | |
459 | * Compute the overhead | |
460 | * | |
7a65004b | 461 | * When distributing meta data blocks outside segment structure, |
783f6184 RK |
462 | * We must count them as the overhead. |
463 | */ | |
464 | overhead = 0; | |
465 | ||
466 | err = nilfs_count_free_blocks(nilfs, &nfreeblocks); | |
467 | if (unlikely(err)) | |
468 | return err; | |
469 | ||
470 | buf->f_type = NILFS_SUPER_MAGIC; | |
471 | buf->f_bsize = sb->s_blocksize; | |
472 | buf->f_blocks = blocks - overhead; | |
473 | buf->f_bfree = nfreeblocks; | |
474 | buf->f_bavail = (buf->f_bfree >= nrsvblocks) ? | |
475 | (buf->f_bfree - nrsvblocks) : 0; | |
476 | buf->f_files = atomic_read(&sbi->s_inodes_count); | |
477 | buf->f_ffree = 0; /* nilfs_count_free_inodes(sb); */ | |
478 | buf->f_namelen = NILFS_NAME_LEN; | |
c306af23 RK |
479 | buf->f_fsid.val[0] = (u32)id; |
480 | buf->f_fsid.val[1] = (u32)(id >> 32); | |
481 | ||
783f6184 RK |
482 | return 0; |
483 | } | |
484 | ||
b58a285b JS |
485 | static int nilfs_show_options(struct seq_file *seq, struct vfsmount *vfs) |
486 | { | |
487 | struct super_block *sb = vfs->mnt_sb; | |
488 | struct nilfs_sb_info *sbi = NILFS_SB(sb); | |
489 | ||
490 | if (!nilfs_test_opt(sbi, BARRIER)) | |
91f1953b | 491 | seq_printf(seq, ",nobarrier"); |
b58a285b JS |
492 | if (nilfs_test_opt(sbi, SNAPSHOT)) |
493 | seq_printf(seq, ",cp=%llu", | |
494 | (unsigned long long int)sbi->s_snapshot_cno); | |
b58a285b JS |
495 | if (nilfs_test_opt(sbi, ERRORS_PANIC)) |
496 | seq_printf(seq, ",errors=panic"); | |
277a6a34 RK |
497 | if (nilfs_test_opt(sbi, ERRORS_CONT)) |
498 | seq_printf(seq, ",errors=continue"); | |
b58a285b JS |
499 | if (nilfs_test_opt(sbi, STRICT_ORDER)) |
500 | seq_printf(seq, ",order=strict"); | |
0234576d RK |
501 | if (nilfs_test_opt(sbi, NORECOVERY)) |
502 | seq_printf(seq, ",norecovery"); | |
e902ec99 JS |
503 | if (nilfs_test_opt(sbi, DISCARD)) |
504 | seq_printf(seq, ",discard"); | |
b58a285b JS |
505 | |
506 | return 0; | |
507 | } | |
508 | ||
b87221de | 509 | static const struct super_operations nilfs_sops = { |
783f6184 RK |
510 | .alloc_inode = nilfs_alloc_inode, |
511 | .destroy_inode = nilfs_destroy_inode, | |
512 | .dirty_inode = nilfs_dirty_inode, | |
513 | /* .write_inode = nilfs_write_inode, */ | |
514 | /* .put_inode = nilfs_put_inode, */ | |
515 | /* .drop_inode = nilfs_drop_inode, */ | |
516 | .delete_inode = nilfs_delete_inode, | |
517 | .put_super = nilfs_put_super, | |
1dfa2710 | 518 | /* .write_super = nilfs_write_super, */ |
783f6184 RK |
519 | .sync_fs = nilfs_sync_fs, |
520 | /* .write_super_lockfs */ | |
521 | /* .unlockfs */ | |
522 | .statfs = nilfs_statfs, | |
523 | .remount_fs = nilfs_remount, | |
524 | .clear_inode = nilfs_clear_inode, | |
525 | /* .umount_begin */ | |
b58a285b | 526 | .show_options = nilfs_show_options |
783f6184 RK |
527 | }; |
528 | ||
529 | static struct inode * | |
530 | nilfs_nfs_get_inode(struct super_block *sb, u64 ino, u32 generation) | |
531 | { | |
532 | struct inode *inode; | |
533 | ||
534 | if (ino < NILFS_FIRST_INO(sb) && ino != NILFS_ROOT_INO && | |
535 | ino != NILFS_SKETCH_INO) | |
536 | return ERR_PTR(-ESTALE); | |
537 | ||
538 | inode = nilfs_iget(sb, ino); | |
539 | if (IS_ERR(inode)) | |
540 | return ERR_CAST(inode); | |
541 | if (generation && inode->i_generation != generation) { | |
542 | iput(inode); | |
543 | return ERR_PTR(-ESTALE); | |
544 | } | |
545 | ||
546 | return inode; | |
547 | } | |
548 | ||
549 | static struct dentry * | |
550 | nilfs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len, | |
551 | int fh_type) | |
552 | { | |
553 | return generic_fh_to_dentry(sb, fid, fh_len, fh_type, | |
554 | nilfs_nfs_get_inode); | |
555 | } | |
556 | ||
557 | static struct dentry * | |
558 | nilfs_fh_to_parent(struct super_block *sb, struct fid *fid, int fh_len, | |
559 | int fh_type) | |
560 | { | |
561 | return generic_fh_to_parent(sb, fid, fh_len, fh_type, | |
562 | nilfs_nfs_get_inode); | |
563 | } | |
564 | ||
ac4cfdd6 | 565 | static const struct export_operations nilfs_export_ops = { |
783f6184 RK |
566 | .fh_to_dentry = nilfs_fh_to_dentry, |
567 | .fh_to_parent = nilfs_fh_to_parent, | |
568 | .get_parent = nilfs_get_parent, | |
569 | }; | |
570 | ||
571 | enum { | |
572 | Opt_err_cont, Opt_err_panic, Opt_err_ro, | |
0234576d | 573 | Opt_nobarrier, Opt_snapshot, Opt_order, Opt_norecovery, |
e902ec99 | 574 | Opt_discard, Opt_err, |
783f6184 RK |
575 | }; |
576 | ||
577 | static match_table_t tokens = { | |
578 | {Opt_err_cont, "errors=continue"}, | |
579 | {Opt_err_panic, "errors=panic"}, | |
580 | {Opt_err_ro, "errors=remount-ro"}, | |
91f1953b | 581 | {Opt_nobarrier, "nobarrier"}, |
783f6184 RK |
582 | {Opt_snapshot, "cp=%u"}, |
583 | {Opt_order, "order=%s"}, | |
0234576d | 584 | {Opt_norecovery, "norecovery"}, |
e902ec99 | 585 | {Opt_discard, "discard"}, |
783f6184 RK |
586 | {Opt_err, NULL} |
587 | }; | |
588 | ||
783f6184 RK |
589 | static int parse_options(char *options, struct super_block *sb) |
590 | { | |
591 | struct nilfs_sb_info *sbi = NILFS_SB(sb); | |
592 | char *p; | |
593 | substring_t args[MAX_OPT_ARGS]; | |
594 | int option; | |
595 | ||
596 | if (!options) | |
597 | return 1; | |
598 | ||
599 | while ((p = strsep(&options, ",")) != NULL) { | |
600 | int token; | |
601 | if (!*p) | |
602 | continue; | |
603 | ||
604 | token = match_token(p, tokens, args); | |
605 | switch (token) { | |
91f1953b JS |
606 | case Opt_nobarrier: |
607 | nilfs_clear_opt(sbi, BARRIER); | |
783f6184 RK |
608 | break; |
609 | case Opt_order: | |
610 | if (strcmp(args[0].from, "relaxed") == 0) | |
611 | /* Ordered data semantics */ | |
612 | nilfs_clear_opt(sbi, STRICT_ORDER); | |
613 | else if (strcmp(args[0].from, "strict") == 0) | |
614 | /* Strict in-order semantics */ | |
615 | nilfs_set_opt(sbi, STRICT_ORDER); | |
616 | else | |
617 | return 0; | |
618 | break; | |
619 | case Opt_err_panic: | |
620 | nilfs_write_opt(sbi, ERROR_MODE, ERRORS_PANIC); | |
621 | break; | |
622 | case Opt_err_ro: | |
623 | nilfs_write_opt(sbi, ERROR_MODE, ERRORS_RO); | |
624 | break; | |
625 | case Opt_err_cont: | |
626 | nilfs_write_opt(sbi, ERROR_MODE, ERRORS_CONT); | |
627 | break; | |
628 | case Opt_snapshot: | |
629 | if (match_int(&args[0], &option) || option <= 0) | |
630 | return 0; | |
631 | if (!(sb->s_flags & MS_RDONLY)) | |
632 | return 0; | |
633 | sbi->s_snapshot_cno = option; | |
634 | nilfs_set_opt(sbi, SNAPSHOT); | |
635 | break; | |
0234576d RK |
636 | case Opt_norecovery: |
637 | nilfs_set_opt(sbi, NORECOVERY); | |
638 | break; | |
e902ec99 JS |
639 | case Opt_discard: |
640 | nilfs_set_opt(sbi, DISCARD); | |
641 | break; | |
783f6184 RK |
642 | default: |
643 | printk(KERN_ERR | |
644 | "NILFS: Unrecognized mount option \"%s\"\n", p); | |
645 | return 0; | |
646 | } | |
647 | } | |
648 | return 1; | |
649 | } | |
650 | ||
651 | static inline void | |
652 | nilfs_set_default_options(struct nilfs_sb_info *sbi, | |
653 | struct nilfs_super_block *sbp) | |
654 | { | |
655 | sbi->s_mount_opt = | |
277a6a34 | 656 | NILFS_MOUNT_ERRORS_RO | NILFS_MOUNT_BARRIER; |
783f6184 RK |
657 | } |
658 | ||
659 | static int nilfs_setup_super(struct nilfs_sb_info *sbi) | |
660 | { | |
661 | struct the_nilfs *nilfs = sbi->s_nilfs; | |
d26493b6 JS |
662 | struct nilfs_super_block **sbp; |
663 | int max_mnt_count; | |
664 | int mnt_count; | |
665 | ||
666 | /* nilfs->ns_sem must be locked by the caller. */ | |
667 | sbp = nilfs_prepare_super(sbi); | |
668 | if (!sbp) | |
669 | return -EIO; | |
670 | ||
671 | max_mnt_count = le16_to_cpu(sbp[0]->s_max_mnt_count); | |
672 | mnt_count = le16_to_cpu(sbp[0]->s_mnt_count); | |
783f6184 | 673 | |
f50a4c81 | 674 | if (nilfs->ns_mount_state & NILFS_ERROR_FS) { |
783f6184 RK |
675 | printk(KERN_WARNING |
676 | "NILFS warning: mounting fs with errors\n"); | |
677 | #if 0 | |
678 | } else if (max_mnt_count >= 0 && mnt_count >= max_mnt_count) { | |
679 | printk(KERN_WARNING | |
680 | "NILFS warning: maximal mount count reached\n"); | |
681 | #endif | |
682 | } | |
683 | if (!max_mnt_count) | |
d26493b6 | 684 | sbp[0]->s_max_mnt_count = cpu_to_le16(NILFS_DFL_MAX_MNT_COUNT); |
783f6184 | 685 | |
d26493b6 JS |
686 | sbp[0]->s_mnt_count = cpu_to_le16(mnt_count + 1); |
687 | sbp[0]->s_state = | |
688 | cpu_to_le16(le16_to_cpu(sbp[0]->s_state) & ~NILFS_VALID_FS); | |
689 | sbp[0]->s_mtime = cpu_to_le64(get_seconds()); | |
e339ad31 | 690 | return nilfs_commit_super(sbi, 1); |
783f6184 RK |
691 | } |
692 | ||
e339ad31 RK |
693 | struct nilfs_super_block *nilfs_read_super_block(struct super_block *sb, |
694 | u64 pos, int blocksize, | |
695 | struct buffer_head **pbh) | |
783f6184 | 696 | { |
e339ad31 RK |
697 | unsigned long long sb_index = pos; |
698 | unsigned long offset; | |
783f6184 | 699 | |
e339ad31 | 700 | offset = do_div(sb_index, blocksize); |
783f6184 | 701 | *pbh = sb_bread(sb, sb_index); |
e339ad31 | 702 | if (!*pbh) |
783f6184 | 703 | return NULL; |
783f6184 RK |
704 | return (struct nilfs_super_block *)((char *)(*pbh)->b_data + offset); |
705 | } | |
706 | ||
783f6184 RK |
707 | int nilfs_store_magic_and_option(struct super_block *sb, |
708 | struct nilfs_super_block *sbp, | |
709 | char *data) | |
710 | { | |
711 | struct nilfs_sb_info *sbi = NILFS_SB(sb); | |
712 | ||
783f6184 RK |
713 | sb->s_magic = le16_to_cpu(sbp->s_magic); |
714 | ||
715 | /* FS independent flags */ | |
716 | #ifdef NILFS_ATIME_DISABLE | |
717 | sb->s_flags |= MS_NOATIME; | |
718 | #endif | |
719 | ||
783f6184 RK |
720 | nilfs_set_default_options(sbi, sbp); |
721 | ||
722 | sbi->s_resuid = le16_to_cpu(sbp->s_def_resuid); | |
723 | sbi->s_resgid = le16_to_cpu(sbp->s_def_resgid); | |
724 | sbi->s_interval = le32_to_cpu(sbp->s_c_interval); | |
725 | sbi->s_watermark = le32_to_cpu(sbp->s_c_block_max); | |
726 | ||
e339ad31 | 727 | return !parse_options(data, sb) ? -EINVAL : 0 ; |
783f6184 RK |
728 | } |
729 | ||
730 | /** | |
731 | * nilfs_fill_super() - initialize a super block instance | |
732 | * @sb: super_block | |
733 | * @data: mount options | |
734 | * @silent: silent mode flag | |
735 | * @nilfs: the_nilfs struct | |
736 | * | |
aa7dfb89 | 737 | * This function is called exclusively by nilfs->ns_mount_mutex. |
783f6184 RK |
738 | * So, the recovery process is protected from other simultaneous mounts. |
739 | */ | |
740 | static int | |
741 | nilfs_fill_super(struct super_block *sb, void *data, int silent, | |
742 | struct the_nilfs *nilfs) | |
743 | { | |
744 | struct nilfs_sb_info *sbi; | |
745 | struct inode *root; | |
746 | __u64 cno; | |
747 | int err; | |
748 | ||
749 | sbi = kzalloc(sizeof(*sbi), GFP_KERNEL); | |
750 | if (!sbi) | |
751 | return -ENOMEM; | |
752 | ||
753 | sb->s_fs_info = sbi; | |
754 | ||
755 | get_nilfs(nilfs); | |
756 | sbi->s_nilfs = nilfs; | |
757 | sbi->s_super = sb; | |
6dd47406 | 758 | atomic_set(&sbi->s_count, 1); |
783f6184 RK |
759 | |
760 | err = init_nilfs(nilfs, sbi, (char *)data); | |
761 | if (err) | |
762 | goto failed_sbi; | |
763 | ||
764 | spin_lock_init(&sbi->s_inode_lock); | |
765 | INIT_LIST_HEAD(&sbi->s_dirty_files); | |
766 | INIT_LIST_HEAD(&sbi->s_list); | |
767 | ||
768 | /* | |
769 | * Following initialization is overlapped because | |
770 | * nilfs_sb_info structure has been cleared at the beginning. | |
771 | * But we reserve them to keep our interest and make ready | |
772 | * for the future change. | |
773 | */ | |
774 | get_random_bytes(&sbi->s_next_generation, | |
775 | sizeof(sbi->s_next_generation)); | |
776 | spin_lock_init(&sbi->s_next_gen_lock); | |
777 | ||
778 | sb->s_op = &nilfs_sops; | |
779 | sb->s_export_op = &nilfs_export_ops; | |
780 | sb->s_root = NULL; | |
61239230 | 781 | sb->s_time_gran = 1; |
973bec34 | 782 | sb->s_bdi = nilfs->ns_bdi; |
783f6184 | 783 | |
f50a4c81 RK |
784 | err = load_nilfs(nilfs, sbi); |
785 | if (err) | |
786 | goto failed_sbi; | |
787 | ||
783f6184 RK |
788 | cno = nilfs_last_cno(nilfs); |
789 | ||
790 | if (sb->s_flags & MS_RDONLY) { | |
791 | if (nilfs_test_opt(sbi, SNAPSHOT)) { | |
43be0ec0 | 792 | down_read(&nilfs->ns_segctor_sem); |
1f5abe7e RK |
793 | err = nilfs_cpfile_is_snapshot(nilfs->ns_cpfile, |
794 | sbi->s_snapshot_cno); | |
43be0ec0 ZY |
795 | up_read(&nilfs->ns_segctor_sem); |
796 | if (err < 0) { | |
797 | if (err == -ENOENT) | |
798 | err = -EINVAL; | |
1f5abe7e | 799 | goto failed_sbi; |
43be0ec0 | 800 | } |
1f5abe7e | 801 | if (!err) { |
783f6184 RK |
802 | printk(KERN_ERR |
803 | "NILFS: The specified checkpoint is " | |
804 | "not a snapshot " | |
805 | "(checkpoint number=%llu).\n", | |
806 | (unsigned long long)sbi->s_snapshot_cno); | |
807 | err = -EINVAL; | |
808 | goto failed_sbi; | |
809 | } | |
810 | cno = sbi->s_snapshot_cno; | |
d240e067 | 811 | } |
783f6184 RK |
812 | } |
813 | ||
814 | err = nilfs_attach_checkpoint(sbi, cno); | |
815 | if (err) { | |
816 | printk(KERN_ERR "NILFS: error loading a checkpoint" | |
817 | " (checkpoint number=%llu).\n", (unsigned long long)cno); | |
818 | goto failed_sbi; | |
819 | } | |
820 | ||
821 | if (!(sb->s_flags & MS_RDONLY)) { | |
cece5520 | 822 | err = nilfs_attach_segment_constructor(sbi); |
783f6184 RK |
823 | if (err) |
824 | goto failed_checkpoint; | |
825 | } | |
826 | ||
827 | root = nilfs_iget(sb, NILFS_ROOT_INO); | |
828 | if (IS_ERR(root)) { | |
829 | printk(KERN_ERR "NILFS: get root inode failed\n"); | |
830 | err = PTR_ERR(root); | |
831 | goto failed_segctor; | |
832 | } | |
833 | if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) { | |
834 | iput(root); | |
835 | printk(KERN_ERR "NILFS: corrupt root inode.\n"); | |
836 | err = -EINVAL; | |
837 | goto failed_segctor; | |
838 | } | |
839 | sb->s_root = d_alloc_root(root); | |
840 | if (!sb->s_root) { | |
841 | iput(root); | |
842 | printk(KERN_ERR "NILFS: get root dentry failed\n"); | |
843 | err = -ENOMEM; | |
844 | goto failed_segctor; | |
845 | } | |
846 | ||
847 | if (!(sb->s_flags & MS_RDONLY)) { | |
848 | down_write(&nilfs->ns_sem); | |
849 | nilfs_setup_super(sbi); | |
850 | up_write(&nilfs->ns_sem); | |
851 | } | |
852 | ||
e59399d0 | 853 | down_write(&nilfs->ns_super_sem); |
3f82ff55 RK |
854 | if (!nilfs_test_opt(sbi, SNAPSHOT)) |
855 | nilfs->ns_current = sbi; | |
e59399d0 | 856 | up_write(&nilfs->ns_super_sem); |
3f82ff55 | 857 | |
783f6184 RK |
858 | return 0; |
859 | ||
783f6184 RK |
860 | failed_segctor: |
861 | nilfs_detach_segment_constructor(sbi); | |
862 | ||
863 | failed_checkpoint: | |
864 | nilfs_detach_checkpoint(sbi); | |
865 | ||
866 | failed_sbi: | |
867 | put_nilfs(nilfs); | |
868 | sb->s_fs_info = NULL; | |
6dd47406 | 869 | nilfs_put_sbinfo(sbi); |
783f6184 RK |
870 | return err; |
871 | } | |
872 | ||
873 | static int nilfs_remount(struct super_block *sb, int *flags, char *data) | |
874 | { | |
875 | struct nilfs_sb_info *sbi = NILFS_SB(sb); | |
783f6184 RK |
876 | struct the_nilfs *nilfs = sbi->s_nilfs; |
877 | unsigned long old_sb_flags; | |
878 | struct nilfs_mount_options old_opts; | |
d240e067 | 879 | int was_snapshot, err; |
783f6184 | 880 | |
337eb00a AIB |
881 | lock_kernel(); |
882 | ||
e59399d0 | 883 | down_write(&nilfs->ns_super_sem); |
783f6184 RK |
884 | old_sb_flags = sb->s_flags; |
885 | old_opts.mount_opt = sbi->s_mount_opt; | |
886 | old_opts.snapshot_cno = sbi->s_snapshot_cno; | |
d240e067 | 887 | was_snapshot = nilfs_test_opt(sbi, SNAPSHOT); |
783f6184 RK |
888 | |
889 | if (!parse_options(data, sb)) { | |
890 | err = -EINVAL; | |
891 | goto restore_opts; | |
892 | } | |
893 | sb->s_flags = (sb->s_flags & ~MS_POSIXACL); | |
894 | ||
d240e067 RK |
895 | err = -EINVAL; |
896 | if (was_snapshot) { | |
897 | if (!(*flags & MS_RDONLY)) { | |
898 | printk(KERN_ERR "NILFS (device %s): cannot remount " | |
899 | "snapshot read/write.\n", | |
900 | sb->s_id); | |
901 | goto restore_opts; | |
902 | } else if (sbi->s_snapshot_cno != old_opts.snapshot_cno) { | |
903 | printk(KERN_ERR "NILFS (device %s): cannot " | |
904 | "remount to a different snapshot.\n", | |
905 | sb->s_id); | |
906 | goto restore_opts; | |
907 | } | |
908 | } else { | |
909 | if (nilfs_test_opt(sbi, SNAPSHOT)) { | |
910 | printk(KERN_ERR "NILFS (device %s): cannot change " | |
911 | "a regular mount to a snapshot.\n", | |
912 | sb->s_id); | |
913 | goto restore_opts; | |
914 | } | |
783f6184 RK |
915 | } |
916 | ||
0234576d RK |
917 | if (!nilfs_valid_fs(nilfs)) { |
918 | printk(KERN_WARNING "NILFS (device %s): couldn't " | |
919 | "remount because the filesystem is in an " | |
920 | "incomplete recovery state.\n", sb->s_id); | |
0234576d RK |
921 | goto restore_opts; |
922 | } | |
923 | ||
783f6184 RK |
924 | if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY)) |
925 | goto out; | |
926 | if (*flags & MS_RDONLY) { | |
927 | /* Shutting down the segment constructor */ | |
928 | nilfs_detach_segment_constructor(sbi); | |
929 | sb->s_flags |= MS_RDONLY; | |
930 | ||
783f6184 RK |
931 | /* |
932 | * Remounting a valid RW partition RDONLY, so set | |
933 | * the RDONLY flag and then mark the partition as valid again. | |
934 | */ | |
935 | down_write(&nilfs->ns_sem); | |
7ecaa46c | 936 | nilfs_cleanup_super(sbi); |
783f6184 RK |
937 | up_write(&nilfs->ns_sem); |
938 | } else { | |
939 | /* | |
940 | * Mounting a RDONLY partition read-write, so reread and | |
941 | * store the current valid flag. (It may have been changed | |
942 | * by fsck since we originally mounted the partition.) | |
943 | */ | |
783f6184 | 944 | sb->s_flags &= ~MS_RDONLY; |
783f6184 | 945 | |
cece5520 | 946 | err = nilfs_attach_segment_constructor(sbi); |
783f6184 | 947 | if (err) |
e59399d0 | 948 | goto restore_opts; |
783f6184 RK |
949 | |
950 | down_write(&nilfs->ns_sem); | |
951 | nilfs_setup_super(sbi); | |
952 | up_write(&nilfs->ns_sem); | |
783f6184 RK |
953 | } |
954 | out: | |
e59399d0 | 955 | up_write(&nilfs->ns_super_sem); |
337eb00a | 956 | unlock_kernel(); |
783f6184 RK |
957 | return 0; |
958 | ||
783f6184 RK |
959 | restore_opts: |
960 | sb->s_flags = old_sb_flags; | |
961 | sbi->s_mount_opt = old_opts.mount_opt; | |
962 | sbi->s_snapshot_cno = old_opts.snapshot_cno; | |
e59399d0 | 963 | up_write(&nilfs->ns_super_sem); |
337eb00a | 964 | unlock_kernel(); |
783f6184 RK |
965 | return err; |
966 | } | |
967 | ||
968 | struct nilfs_super_data { | |
969 | struct block_device *bdev; | |
6dd47406 | 970 | struct nilfs_sb_info *sbi; |
783f6184 RK |
971 | __u64 cno; |
972 | int flags; | |
973 | }; | |
974 | ||
975 | /** | |
976 | * nilfs_identify - pre-read mount options needed to identify mount instance | |
977 | * @data: mount options | |
978 | * @sd: nilfs_super_data | |
979 | */ | |
980 | static int nilfs_identify(char *data, struct nilfs_super_data *sd) | |
981 | { | |
982 | char *p, *options = data; | |
983 | substring_t args[MAX_OPT_ARGS]; | |
984 | int option, token; | |
985 | int ret = 0; | |
986 | ||
987 | do { | |
988 | p = strsep(&options, ","); | |
989 | if (p != NULL && *p) { | |
990 | token = match_token(p, tokens, args); | |
991 | if (token == Opt_snapshot) { | |
992 | if (!(sd->flags & MS_RDONLY)) | |
993 | ret++; | |
994 | else { | |
995 | ret = match_int(&args[0], &option); | |
996 | if (!ret) { | |
997 | if (option > 0) | |
998 | sd->cno = option; | |
999 | else | |
1000 | ret++; | |
1001 | } | |
1002 | } | |
1003 | } | |
1004 | if (ret) | |
1005 | printk(KERN_ERR | |
1006 | "NILFS: invalid mount option: %s\n", p); | |
1007 | } | |
1008 | if (!options) | |
1009 | break; | |
1010 | BUG_ON(options == data); | |
1011 | *(options - 1) = ','; | |
1012 | } while (!ret); | |
1013 | return ret; | |
1014 | } | |
1015 | ||
1016 | static int nilfs_set_bdev_super(struct super_block *s, void *data) | |
1017 | { | |
1018 | struct nilfs_super_data *sd = data; | |
1019 | ||
1020 | s->s_bdev = sd->bdev; | |
1021 | s->s_dev = s->s_bdev->bd_dev; | |
1022 | return 0; | |
1023 | } | |
1024 | ||
1025 | static int nilfs_test_bdev_super(struct super_block *s, void *data) | |
783f6184 RK |
1026 | { |
1027 | struct nilfs_super_data *sd = data; | |
6dd47406 RK |
1028 | |
1029 | return sd->sbi && s->s_fs_info == (void *)sd->sbi; | |
783f6184 RK |
1030 | } |
1031 | ||
1032 | static int | |
1033 | nilfs_get_sb(struct file_system_type *fs_type, int flags, | |
1034 | const char *dev_name, void *data, struct vfsmount *mnt) | |
1035 | { | |
1036 | struct nilfs_super_data sd; | |
33c8e57c | 1037 | struct super_block *s; |
13e90559 | 1038 | fmode_t mode = FMODE_READ; |
33c8e57c | 1039 | struct the_nilfs *nilfs; |
783f6184 RK |
1040 | int err, need_to_close = 1; |
1041 | ||
13e90559 RK |
1042 | if (!(flags & MS_RDONLY)) |
1043 | mode |= FMODE_WRITE; | |
1044 | ||
1045 | sd.bdev = open_bdev_exclusive(dev_name, mode, fs_type); | |
783f6184 RK |
1046 | if (IS_ERR(sd.bdev)) |
1047 | return PTR_ERR(sd.bdev); | |
1048 | ||
1049 | /* | |
1050 | * To get mount instance using sget() vfs-routine, NILFS needs | |
1051 | * much more information than normal filesystems to identify mount | |
1052 | * instance. For snapshot mounts, not only a mount type (ro-mount | |
1053 | * or rw-mount) but also a checkpoint number is required. | |
783f6184 RK |
1054 | */ |
1055 | sd.cno = 0; | |
1056 | sd.flags = flags; | |
1057 | if (nilfs_identify((char *)data, &sd)) { | |
1058 | err = -EINVAL; | |
1059 | goto failed; | |
1060 | } | |
1061 | ||
33c8e57c RK |
1062 | nilfs = find_or_create_nilfs(sd.bdev); |
1063 | if (!nilfs) { | |
1064 | err = -ENOMEM; | |
1065 | goto failed; | |
1066 | } | |
1067 | ||
aa7dfb89 | 1068 | mutex_lock(&nilfs->ns_mount_mutex); |
3f82ff55 RK |
1069 | |
1070 | if (!sd.cno) { | |
1071 | /* | |
1072 | * Check if an exclusive mount exists or not. | |
1073 | * Snapshot mounts coexist with a current mount | |
1074 | * (i.e. rw-mount or ro-mount), whereas rw-mount and | |
1075 | * ro-mount are mutually exclusive. | |
1076 | */ | |
e59399d0 | 1077 | down_read(&nilfs->ns_super_sem); |
3f82ff55 RK |
1078 | if (nilfs->ns_current && |
1079 | ((nilfs->ns_current->s_super->s_flags ^ flags) | |
1080 | & MS_RDONLY)) { | |
e59399d0 | 1081 | up_read(&nilfs->ns_super_sem); |
3f82ff55 RK |
1082 | err = -EBUSY; |
1083 | goto failed_unlock; | |
1084 | } | |
e59399d0 | 1085 | up_read(&nilfs->ns_super_sem); |
783f6184 RK |
1086 | } |
1087 | ||
1088 | /* | |
6dd47406 | 1089 | * Find existing nilfs_sb_info struct |
783f6184 | 1090 | */ |
6dd47406 RK |
1091 | sd.sbi = nilfs_find_sbinfo(nilfs, !(flags & MS_RDONLY), sd.cno); |
1092 | ||
6dd47406 RK |
1093 | /* |
1094 | * Get super block instance holding the nilfs_sb_info struct. | |
1095 | * A new instance is allocated if no existing mount is present or | |
1096 | * existing instance has been unmounted. | |
1097 | */ | |
33c8e57c | 1098 | s = sget(fs_type, nilfs_test_bdev_super, nilfs_set_bdev_super, &sd); |
6dd47406 RK |
1099 | if (sd.sbi) |
1100 | nilfs_put_sbinfo(sd.sbi); | |
1101 | ||
33c8e57c RK |
1102 | if (IS_ERR(s)) { |
1103 | err = PTR_ERR(s); | |
1104 | goto failed_unlock; | |
783f6184 RK |
1105 | } |
1106 | ||
1107 | if (!s->s_root) { | |
1108 | char b[BDEVNAME_SIZE]; | |
1109 | ||
33c8e57c | 1110 | /* New superblock instance created */ |
783f6184 | 1111 | s->s_flags = flags; |
4571b82c | 1112 | s->s_mode = mode; |
783f6184 RK |
1113 | strlcpy(s->s_id, bdevname(sd.bdev, b), sizeof(s->s_id)); |
1114 | sb_set_blocksize(s, block_size(sd.bdev)); | |
1115 | ||
e2d1591a RK |
1116 | err = nilfs_fill_super(s, data, flags & MS_SILENT ? 1 : 0, |
1117 | nilfs); | |
783f6184 RK |
1118 | if (err) |
1119 | goto cancel_new; | |
1120 | ||
1121 | s->s_flags |= MS_ACTIVE; | |
1122 | need_to_close = 0; | |
783f6184 RK |
1123 | } |
1124 | ||
aa7dfb89 | 1125 | mutex_unlock(&nilfs->ns_mount_mutex); |
783f6184 RK |
1126 | put_nilfs(nilfs); |
1127 | if (need_to_close) | |
13e90559 | 1128 | close_bdev_exclusive(sd.bdev, mode); |
783f6184 RK |
1129 | simple_set_mnt(mnt, s); |
1130 | return 0; | |
1131 | ||
783f6184 | 1132 | failed_unlock: |
aa7dfb89 | 1133 | mutex_unlock(&nilfs->ns_mount_mutex); |
33c8e57c | 1134 | put_nilfs(nilfs); |
783f6184 | 1135 | failed: |
13e90559 | 1136 | close_bdev_exclusive(sd.bdev, mode); |
783f6184 RK |
1137 | |
1138 | return err; | |
1139 | ||
1140 | cancel_new: | |
1141 | /* Abandoning the newly allocated superblock */ | |
aa7dfb89 | 1142 | mutex_unlock(&nilfs->ns_mount_mutex); |
33c8e57c | 1143 | put_nilfs(nilfs); |
a95161aa | 1144 | deactivate_locked_super(s); |
783f6184 | 1145 | /* |
b87ca919 | 1146 | * deactivate_locked_super() invokes close_bdev_exclusive(). |
783f6184 | 1147 | * We must finish all post-cleaning before this call; |
aa7dfb89 | 1148 | * put_nilfs() needs the block device. |
783f6184 RK |
1149 | */ |
1150 | return err; | |
1151 | } | |
1152 | ||
783f6184 RK |
1153 | struct file_system_type nilfs_fs_type = { |
1154 | .owner = THIS_MODULE, | |
1155 | .name = "nilfs2", | |
1156 | .get_sb = nilfs_get_sb, | |
1157 | .kill_sb = kill_block_super, | |
1158 | .fs_flags = FS_REQUIRES_DEV, | |
1159 | }; | |
1160 | ||
41c88bd7 | 1161 | static void nilfs_inode_init_once(void *obj) |
783f6184 | 1162 | { |
41c88bd7 | 1163 | struct nilfs_inode_info *ii = obj; |
783f6184 | 1164 | |
41c88bd7 LH |
1165 | INIT_LIST_HEAD(&ii->i_dirty); |
1166 | #ifdef CONFIG_NILFS_XATTR | |
1167 | init_rwsem(&ii->xattr_sem); | |
1168 | #endif | |
1169 | nilfs_btnode_cache_init_once(&ii->i_btnode_cache); | |
1170 | ii->i_bmap = (struct nilfs_bmap *)&ii->i_bmap_union; | |
1171 | inode_init_once(&ii->vfs_inode); | |
1172 | } | |
783f6184 | 1173 | |
41c88bd7 LH |
1174 | static void nilfs_segbuf_init_once(void *obj) |
1175 | { | |
1176 | memset(obj, 0, sizeof(struct nilfs_segment_buffer)); | |
1177 | } | |
783f6184 | 1178 | |
41c88bd7 LH |
1179 | static void nilfs_destroy_cachep(void) |
1180 | { | |
84cb0999 | 1181 | if (nilfs_inode_cachep) |
41c88bd7 | 1182 | kmem_cache_destroy(nilfs_inode_cachep); |
84cb0999 | 1183 | if (nilfs_transaction_cachep) |
41c88bd7 | 1184 | kmem_cache_destroy(nilfs_transaction_cachep); |
84cb0999 | 1185 | if (nilfs_segbuf_cachep) |
41c88bd7 | 1186 | kmem_cache_destroy(nilfs_segbuf_cachep); |
84cb0999 | 1187 | if (nilfs_btree_path_cache) |
41c88bd7 LH |
1188 | kmem_cache_destroy(nilfs_btree_path_cache); |
1189 | } | |
783f6184 | 1190 | |
41c88bd7 LH |
1191 | static int __init nilfs_init_cachep(void) |
1192 | { | |
1193 | nilfs_inode_cachep = kmem_cache_create("nilfs2_inode_cache", | |
1194 | sizeof(struct nilfs_inode_info), 0, | |
1195 | SLAB_RECLAIM_ACCOUNT, nilfs_inode_init_once); | |
1196 | if (!nilfs_inode_cachep) | |
1197 | goto fail; | |
1198 | ||
1199 | nilfs_transaction_cachep = kmem_cache_create("nilfs2_transaction_cache", | |
1200 | sizeof(struct nilfs_transaction_info), 0, | |
1201 | SLAB_RECLAIM_ACCOUNT, NULL); | |
1202 | if (!nilfs_transaction_cachep) | |
1203 | goto fail; | |
1204 | ||
1205 | nilfs_segbuf_cachep = kmem_cache_create("nilfs2_segbuf_cache", | |
1206 | sizeof(struct nilfs_segment_buffer), 0, | |
1207 | SLAB_RECLAIM_ACCOUNT, nilfs_segbuf_init_once); | |
1208 | if (!nilfs_segbuf_cachep) | |
1209 | goto fail; | |
1210 | ||
1211 | nilfs_btree_path_cache = kmem_cache_create("nilfs2_btree_path_cache", | |
1212 | sizeof(struct nilfs_btree_path) * NILFS_BTREE_LEVEL_MAX, | |
1213 | 0, 0, NULL); | |
1214 | if (!nilfs_btree_path_cache) | |
1215 | goto fail; | |
783f6184 RK |
1216 | |
1217 | return 0; | |
1218 | ||
41c88bd7 LH |
1219 | fail: |
1220 | nilfs_destroy_cachep(); | |
1221 | return -ENOMEM; | |
1222 | } | |
1223 | ||
1224 | static int __init init_nilfs_fs(void) | |
1225 | { | |
1226 | int err; | |
783f6184 | 1227 | |
41c88bd7 LH |
1228 | err = nilfs_init_cachep(); |
1229 | if (err) | |
1230 | goto fail; | |
783f6184 | 1231 | |
41c88bd7 LH |
1232 | err = register_filesystem(&nilfs_fs_type); |
1233 | if (err) | |
1234 | goto free_cachep; | |
783f6184 | 1235 | |
9f130263 | 1236 | printk(KERN_INFO "NILFS version 2 loaded\n"); |
41c88bd7 | 1237 | return 0; |
783f6184 | 1238 | |
41c88bd7 LH |
1239 | free_cachep: |
1240 | nilfs_destroy_cachep(); | |
1241 | fail: | |
783f6184 RK |
1242 | return err; |
1243 | } | |
1244 | ||
1245 | static void __exit exit_nilfs_fs(void) | |
1246 | { | |
41c88bd7 | 1247 | nilfs_destroy_cachep(); |
783f6184 RK |
1248 | unregister_filesystem(&nilfs_fs_type); |
1249 | } | |
1250 | ||
1251 | module_init(init_nilfs_fs) | |
1252 | module_exit(exit_nilfs_fs) |