affs: re-structure superblock locking a bit
[deliverable/linux.git] / fs / affs / super.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/affs/inode.c
3 *
4 * (c) 1996 Hans-Joachim Widmaier - Rewritten
5 *
6 * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
7 *
8 * (C) 1992 Eric Youngdale Modified for ISO 9660 filesystem.
9 *
10 * (C) 1991 Linus Torvalds - minix filesystem
11 */
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/statfs.h>
16#include <linux/parser.h>
e18fa700 17#include <linux/magic.h>
e8edc6e0 18#include <linux/sched.h>
5a0e3ad6 19#include <linux/slab.h>
1da177e4
LT
20#include "affs.h"
21
22extern struct timezone sys_tz;
23
726c3342 24static int affs_statfs(struct dentry *dentry, struct kstatfs *buf);
1da177e4
LT
25static int affs_remount (struct super_block *sb, int *flags, char *data);
26
e2896436 27static void
bc86256d 28affs_commit_super(struct super_block *sb, int wait)
e2896436
CH
29{
30 struct affs_sb_info *sbi = AFFS_SB(sb);
31 struct buffer_head *bh = sbi->s_root_bh;
32 struct affs_root_tail *tail = AFFS_ROOT_TAIL(sb, bh);
33
e0471c8d 34 lock_super(sb);
e2896436
CH
35 secs_to_datestamp(get_seconds(), &tail->disk_change);
36 affs_fix_checksum(sb, bh);
37 mark_buffer_dirty(bh);
7435d506
AB
38 if (wait)
39 sync_dirty_buffer(bh);
e0471c8d 40 unlock_super(sb);
e2896436
CH
41}
42
1da177e4
LT
43static void
44affs_put_super(struct super_block *sb)
45{
46 struct affs_sb_info *sbi = AFFS_SB(sb);
47 pr_debug("AFFS: put_super()\n");
48
f99d49ad 49 kfree(sbi->s_prefix);
1da177e4
LT
50 affs_free_bitmap(sb);
51 affs_brelse(sbi->s_root_bh);
52 kfree(sbi);
53 sb->s_fs_info = NULL;
1da177e4
LT
54}
55
56static void
57affs_write_super(struct super_block *sb)
58{
669d5f1f 59 if (!(sb->s_flags & MS_RDONLY))
bc86256d 60 affs_commit_super(sb, 1);
669d5f1f 61 sb->s_dirt = 0;
669d5f1f 62 pr_debug("AFFS: write_super() at %lu, clean=2\n", get_seconds());
1da177e4
LT
63}
64
e2896436
CH
65static int
66affs_sync_fs(struct super_block *sb, int wait)
67{
bc86256d 68 affs_commit_super(sb, wait);
e2896436 69 sb->s_dirt = 0;
e2896436
CH
70 return 0;
71}
72
e18b890b 73static struct kmem_cache * affs_inode_cachep;
1da177e4
LT
74
75static struct inode *affs_alloc_inode(struct super_block *sb)
76{
dca3c336
RZ
77 struct affs_inode_info *i;
78
79 i = kmem_cache_alloc(affs_inode_cachep, GFP_KERNEL);
80 if (!i)
1da177e4 81 return NULL;
dca3c336
RZ
82
83 i->vfs_inode.i_version = 1;
84 i->i_lc = NULL;
85 i->i_ext_bh = NULL;
86 i->i_pa_cnt = 0;
87
88 return &i->vfs_inode;
1da177e4
LT
89}
90
fa0d7e3d 91static void affs_i_callback(struct rcu_head *head)
1da177e4 92{
fa0d7e3d 93 struct inode *inode = container_of(head, struct inode, i_rcu);
1da177e4
LT
94 kmem_cache_free(affs_inode_cachep, AFFS_I(inode));
95}
96
fa0d7e3d
NP
97static void affs_destroy_inode(struct inode *inode)
98{
99 call_rcu(&inode->i_rcu, affs_i_callback);
100}
101
51cc5068 102static void init_once(void *foo)
1da177e4
LT
103{
104 struct affs_inode_info *ei = (struct affs_inode_info *) foo;
105
756b0322
TG
106 sema_init(&ei->i_link_lock, 1);
107 sema_init(&ei->i_ext_lock, 1);
a35afb83 108 inode_init_once(&ei->vfs_inode);
1da177e4
LT
109}
110
111static int init_inodecache(void)
112{
113 affs_inode_cachep = kmem_cache_create("affs_inode_cache",
114 sizeof(struct affs_inode_info),
fffb60f9
PJ
115 0, (SLAB_RECLAIM_ACCOUNT|
116 SLAB_MEM_SPREAD),
20c2df83 117 init_once);
1da177e4
LT
118 if (affs_inode_cachep == NULL)
119 return -ENOMEM;
120 return 0;
121}
122
123static void destroy_inodecache(void)
124{
1a1d92c1 125 kmem_cache_destroy(affs_inode_cachep);
1da177e4
LT
126}
127
ee9b6d61 128static const struct super_operations affs_sops = {
1da177e4
LT
129 .alloc_inode = affs_alloc_inode,
130 .destroy_inode = affs_destroy_inode,
1da177e4 131 .write_inode = affs_write_inode,
f053ddde 132 .evict_inode = affs_evict_inode,
1da177e4
LT
133 .put_super = affs_put_super,
134 .write_super = affs_write_super,
e2896436 135 .sync_fs = affs_sync_fs,
1da177e4
LT
136 .statfs = affs_statfs,
137 .remount_fs = affs_remount,
e9b3961b 138 .show_options = generic_show_options,
1da177e4
LT
139};
140
141enum {
142 Opt_bs, Opt_mode, Opt_mufs, Opt_prefix, Opt_protect,
143 Opt_reserved, Opt_root, Opt_setgid, Opt_setuid,
144 Opt_verbose, Opt_volume, Opt_ignore, Opt_err,
145};
146
a447c093 147static const match_table_t tokens = {
1da177e4
LT
148 {Opt_bs, "bs=%u"},
149 {Opt_mode, "mode=%o"},
150 {Opt_mufs, "mufs"},
151 {Opt_prefix, "prefix=%s"},
152 {Opt_protect, "protect"},
153 {Opt_reserved, "reserved=%u"},
154 {Opt_root, "root=%u"},
155 {Opt_setgid, "setgid=%u"},
156 {Opt_setuid, "setuid=%u"},
157 {Opt_verbose, "verbose"},
158 {Opt_volume, "volume=%s"},
159 {Opt_ignore, "grpquota"},
160 {Opt_ignore, "noquota"},
161 {Opt_ignore, "quota"},
162 {Opt_ignore, "usrquota"},
163 {Opt_err, NULL},
164};
165
166static int
167parse_options(char *options, uid_t *uid, gid_t *gid, int *mode, int *reserved, s32 *root,
168 int *blocksize, char **prefix, char *volume, unsigned long *mount_opts)
169{
170 char *p;
171 substring_t args[MAX_OPT_ARGS];
172
173 /* Fill in defaults */
174
21559981
DH
175 *uid = current_uid();
176 *gid = current_gid();
1da177e4
LT
177 *reserved = 2;
178 *root = -1;
179 *blocksize = -1;
180 volume[0] = ':';
181 volume[1] = 0;
182 *mount_opts = 0;
183 if (!options)
184 return 1;
185
186 while ((p = strsep(&options, ",")) != NULL) {
187 int token, n, option;
188 if (!*p)
189 continue;
190
191 token = match_token(p, tokens, args);
192 switch (token) {
193 case Opt_bs:
194 if (match_int(&args[0], &n))
217686e9 195 return 0;
1da177e4
LT
196 if (n != 512 && n != 1024 && n != 2048
197 && n != 4096) {
198 printk ("AFFS: Invalid blocksize (512, 1024, 2048, 4096 allowed)\n");
199 return 0;
200 }
201 *blocksize = n;
202 break;
203 case Opt_mode:
204 if (match_octal(&args[0], &option))
217686e9 205 return 0;
1da177e4
LT
206 *mode = option & 0777;
207 *mount_opts |= SF_SETMODE;
208 break;
209 case Opt_mufs:
210 *mount_opts |= SF_MUFS;
211 break;
212 case Opt_prefix:
1da177e4
LT
213 *prefix = match_strdup(&args[0]);
214 if (!*prefix)
215 return 0;
216 *mount_opts |= SF_PREFIX;
217 break;
218 case Opt_protect:
219 *mount_opts |= SF_IMMUTABLE;
220 break;
221 case Opt_reserved:
222 if (match_int(&args[0], reserved))
217686e9 223 return 0;
1da177e4
LT
224 break;
225 case Opt_root:
226 if (match_int(&args[0], root))
217686e9 227 return 0;
1da177e4
LT
228 break;
229 case Opt_setgid:
230 if (match_int(&args[0], &option))
217686e9 231 return 0;
1da177e4
LT
232 *gid = option;
233 *mount_opts |= SF_SETGID;
234 break;
235 case Opt_setuid:
236 if (match_int(&args[0], &option))
217686e9 237 return 0;
1da177e4
LT
238 *uid = option;
239 *mount_opts |= SF_SETUID;
240 break;
241 case Opt_verbose:
242 *mount_opts |= SF_VERBOSE;
243 break;
244 case Opt_volume: {
245 char *vol = match_strdup(&args[0]);
6db27dd9
JM
246 if (!vol)
247 return 0;
1da177e4
LT
248 strlcpy(volume, vol, 32);
249 kfree(vol);
250 break;
251 }
252 case Opt_ignore:
253 /* Silently ignore the quota options */
254 break;
255 default:
256 printk("AFFS: Unrecognized mount option \"%s\" "
257 "or missing value\n", p);
258 return 0;
259 }
260 }
261 return 1;
262}
263
264/* This function definitely needs to be split up. Some fine day I'll
265 * hopefully have the guts to do so. Until then: sorry for the mess.
266 */
267
268static int affs_fill_super(struct super_block *sb, void *data, int silent)
269{
270 struct affs_sb_info *sbi;
271 struct buffer_head *root_bh = NULL;
272 struct buffer_head *boot_bh;
273 struct inode *root_inode = NULL;
274 s32 root_block;
275 int size, blocksize;
276 u32 chksum;
277 int num_bm;
278 int i, j;
279 s32 key;
280 uid_t uid;
281 gid_t gid;
282 int reserved;
283 unsigned long mount_flags;
284 int tmp_flags; /* fix remount prototype... */
2b943cf0 285 u8 sig[4];
210f8559 286 int ret = -EINVAL;
1da177e4 287
e9b3961b
MS
288 save_mount_options(sb, data);
289
1da177e4
LT
290 pr_debug("AFFS: read_super(%s)\n",data ? (const char *)data : "no options");
291
292 sb->s_magic = AFFS_SUPER_MAGIC;
293 sb->s_op = &affs_sops;
294 sb->s_flags |= MS_NODIRATIME;
295
f8314dc6 296 sbi = kzalloc(sizeof(struct affs_sb_info), GFP_KERNEL);
1da177e4
LT
297 if (!sbi)
298 return -ENOMEM;
74c41429 299
1da177e4 300 sb->s_fs_info = sbi;
7d135a5d 301 mutex_init(&sbi->s_bmlock);
29333920 302 spin_lock_init(&sbi->symlink_lock);
1da177e4
LT
303
304 if (!parse_options(data,&uid,&gid,&i,&reserved,&root_block,
305 &blocksize,&sbi->s_prefix,
306 sbi->s_volume, &mount_flags)) {
307 printk(KERN_ERR "AFFS: Error parsing options\n");
afc70ed0
AV
308 kfree(sbi->s_prefix);
309 kfree(sbi);
1da177e4
LT
310 return -EINVAL;
311 }
312 /* N.B. after this point s_prefix must be released */
313
314 sbi->s_flags = mount_flags;
315 sbi->s_mode = i;
316 sbi->s_uid = uid;
317 sbi->s_gid = gid;
318 sbi->s_reserved= reserved;
319
320 /* Get the size of the device in 512-byte blocks.
321 * If we later see that the partition uses bigger
322 * blocks, we will have to change it.
323 */
324
325 size = sb->s_bdev->bd_inode->i_size >> 9;
326 pr_debug("AFFS: initial blocksize=%d, #blocks=%d\n", 512, size);
327
328 affs_set_blocksize(sb, PAGE_SIZE);
329 /* Try to find root block. Its location depends on the block size. */
330
331 i = 512;
332 j = 4096;
333 if (blocksize > 0) {
334 i = j = blocksize;
335 size = size / (blocksize / 512);
336 }
337 for (blocksize = i, key = 0; blocksize <= j; blocksize <<= 1, size >>= 1) {
338 sbi->s_root_block = root_block;
339 if (root_block < 0)
340 sbi->s_root_block = (reserved + size - 1) / 2;
341 pr_debug("AFFS: setting blocksize to %d\n", blocksize);
342 affs_set_blocksize(sb, blocksize);
343 sbi->s_partition_size = size;
344
345 /* The root block location that was calculated above is not
346 * correct if the partition size is an odd number of 512-
347 * byte blocks, which will be rounded down to a number of
348 * 1024-byte blocks, and if there were an even number of
349 * reserved blocks. Ideally, all partition checkers should
350 * report the real number of blocks of the real blocksize,
351 * but since this just cannot be done, we have to try to
352 * find the root block anyways. In the above case, it is one
353 * block behind the calculated one. So we check this one, too.
354 */
355 for (num_bm = 0; num_bm < 2; num_bm++) {
356 pr_debug("AFFS: Dev %s, trying root=%u, bs=%d, "
357 "size=%d, reserved=%d\n",
358 sb->s_id,
359 sbi->s_root_block + num_bm,
360 blocksize, size, reserved);
361 root_bh = affs_bread(sb, sbi->s_root_block + num_bm);
362 if (!root_bh)
363 continue;
364 if (!affs_checksum_block(sb, root_bh) &&
365 be32_to_cpu(AFFS_ROOT_HEAD(root_bh)->ptype) == T_SHORT &&
366 be32_to_cpu(AFFS_ROOT_TAIL(sb, root_bh)->stype) == ST_ROOT) {
367 sbi->s_hashsize = blocksize / 4 - 56;
368 sbi->s_root_block += num_bm;
369 key = 1;
370 goto got_root;
371 }
372 affs_brelse(root_bh);
373 root_bh = NULL;
374 }
375 }
376 if (!silent)
377 printk(KERN_ERR "AFFS: No valid root block on device %s\n",
378 sb->s_id);
379 goto out_error;
380
381 /* N.B. after this point bh must be released */
382got_root:
383 root_block = sbi->s_root_block;
384
385 /* Find out which kind of FS we have */
386 boot_bh = sb_bread(sb, 0);
387 if (!boot_bh) {
388 printk(KERN_ERR "AFFS: Cannot read boot block\n");
389 goto out_error;
390 }
2b943cf0 391 memcpy(sig, boot_bh->b_data, 4);
1da177e4 392 brelse(boot_bh);
2b943cf0 393 chksum = be32_to_cpu(*(__be32 *)sig);
1da177e4
LT
394
395 /* Dircache filesystems are compatible with non-dircache ones
396 * when reading. As long as they aren't supported, writing is
397 * not recommended.
398 */
399 if ((chksum == FS_DCFFS || chksum == MUFS_DCFFS || chksum == FS_DCOFS
400 || chksum == MUFS_DCOFS) && !(sb->s_flags & MS_RDONLY)) {
401 printk(KERN_NOTICE "AFFS: Dircache FS - mounting %s read only\n",
402 sb->s_id);
403 sb->s_flags |= MS_RDONLY;
404 }
405 switch (chksum) {
406 case MUFS_FS:
407 case MUFS_INTLFFS:
408 case MUFS_DCFFS:
409 sbi->s_flags |= SF_MUFS;
410 /* fall thru */
411 case FS_INTLFFS:
412 case FS_DCFFS:
413 sbi->s_flags |= SF_INTL;
414 break;
415 case MUFS_FFS:
416 sbi->s_flags |= SF_MUFS;
417 break;
418 case FS_FFS:
419 break;
420 case MUFS_OFS:
421 sbi->s_flags |= SF_MUFS;
422 /* fall thru */
423 case FS_OFS:
424 sbi->s_flags |= SF_OFS;
425 sb->s_flags |= MS_NOEXEC;
426 break;
427 case MUFS_DCOFS:
428 case MUFS_INTLOFS:
429 sbi->s_flags |= SF_MUFS;
430 case FS_DCOFS:
431 case FS_INTLOFS:
432 sbi->s_flags |= SF_INTL | SF_OFS;
433 sb->s_flags |= MS_NOEXEC;
434 break;
435 default:
436 printk(KERN_ERR "AFFS: Unknown filesystem on device %s: %08X\n",
437 sb->s_id, chksum);
438 goto out_error;
439 }
440
441 if (mount_flags & SF_VERBOSE) {
2b943cf0
AV
442 u8 len = AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0];
443 printk(KERN_NOTICE "AFFS: Mounting volume \"%.*s\": Type=%.3s\\%c, Blocksize=%d\n",
444 len > 31 ? 31 : len,
1da177e4 445 AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
2b943cf0 446 sig, sig[3] + '0', blocksize);
1da177e4
LT
447 }
448
449 sb->s_flags |= MS_NODEV | MS_NOSUID;
450
451 sbi->s_data_blksize = sb->s_blocksize;
452 if (sbi->s_flags & SF_OFS)
453 sbi->s_data_blksize -= 24;
454
455 /* Keep super block in cache */
456 sbi->s_root_bh = root_bh;
457 /* N.B. after this point s_root_bh must be released */
458
459 tmp_flags = sb->s_flags;
460 if (affs_init_bitmap(sb, &tmp_flags))
461 goto out_error;
462 sb->s_flags = tmp_flags;
463
464 /* set up enough so that it can read an inode */
465
210f8559
DH
466 root_inode = affs_iget(sb, root_block);
467 if (IS_ERR(root_inode)) {
468 ret = PTR_ERR(root_inode);
48fde701 469 goto out_error;
210f8559
DH
470 }
471
a129880d
AV
472 if (AFFS_SB(sb)->s_flags & SF_INTL)
473 sb->s_d_op = &affs_intl_dentry_operations;
474 else
475 sb->s_d_op = &affs_dentry_operations;
476
48fde701 477 sb->s_root = d_make_root(root_inode);
1da177e4
LT
478 if (!sb->s_root) {
479 printk(KERN_ERR "AFFS: Get root inode failed\n");
480 goto out_error;
481 }
1da177e4
LT
482
483 pr_debug("AFFS: s_flags=%lX\n",sb->s_flags);
484 return 0;
485
486 /*
487 * Begin the cascaded cleanup ...
488 */
489out_error:
f99d49ad 490 kfree(sbi->s_bitmap);
1da177e4 491 affs_brelse(root_bh);
f99d49ad 492 kfree(sbi->s_prefix);
1da177e4
LT
493 kfree(sbi);
494 sb->s_fs_info = NULL;
210f8559 495 return ret;
1da177e4
LT
496}
497
498static int
499affs_remount(struct super_block *sb, int *flags, char *data)
500{
501 struct affs_sb_info *sbi = AFFS_SB(sb);
502 int blocksize;
503 uid_t uid;
504 gid_t gid;
505 int mode;
506 int reserved;
507 int root_block;
508 unsigned long mount_flags;
509 int res = 0;
e9b3961b 510 char *new_opts = kstrdup(data, GFP_KERNEL);
29333920
AV
511 char volume[32];
512 char *prefix = NULL;
1da177e4
LT
513
514 pr_debug("AFFS: remount(flags=0x%x,opts=\"%s\")\n",*flags,data);
515
516 *flags |= MS_NODIRATIME;
517
29333920 518 memcpy(volume, sbi->s_volume, 32);
e9b3961b 519 if (!parse_options(data, &uid, &gid, &mode, &reserved, &root_block,
29333920 520 &blocksize, &prefix, volume,
e9b3961b 521 &mount_flags)) {
29333920 522 kfree(prefix);
e9b3961b 523 kfree(new_opts);
1da177e4 524 return -EINVAL;
e9b3961b 525 }
74c41429 526
2a32cebd 527 replace_mount_options(sb, new_opts);
e9b3961b 528
1da177e4
LT
529 sbi->s_flags = mount_flags;
530 sbi->s_mode = mode;
531 sbi->s_uid = uid;
532 sbi->s_gid = gid;
29333920
AV
533 /* protect against readers */
534 spin_lock(&sbi->symlink_lock);
535 if (prefix) {
536 kfree(sbi->s_prefix);
537 sbi->s_prefix = prefix;
538 }
539 memcpy(sbi->s_volume, volume, 32);
540 spin_unlock(&sbi->symlink_lock);
1da177e4 541
74c41429 542 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
1da177e4 543 return 0;
74c41429 544
0164b1a3 545 if (*flags & MS_RDONLY)
1da177e4 546 affs_free_bitmap(sb);
0164b1a3 547 else
1da177e4
LT
548 res = affs_init_bitmap(sb, flags);
549
550 return res;
551}
552
553static int
726c3342 554affs_statfs(struct dentry *dentry, struct kstatfs *buf)
1da177e4 555{
726c3342 556 struct super_block *sb = dentry->d_sb;
1da177e4 557 int free;
a6a2a73c 558 u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
1da177e4
LT
559
560 pr_debug("AFFS: statfs() partsize=%d, reserved=%d\n",AFFS_SB(sb)->s_partition_size,
561 AFFS_SB(sb)->s_reserved);
562
563 free = affs_count_free_blocks(sb);
564 buf->f_type = AFFS_SUPER_MAGIC;
565 buf->f_bsize = sb->s_blocksize;
566 buf->f_blocks = AFFS_SB(sb)->s_partition_size - AFFS_SB(sb)->s_reserved;
567 buf->f_bfree = free;
568 buf->f_bavail = free;
a6a2a73c
CL
569 buf->f_fsid.val[0] = (u32)id;
570 buf->f_fsid.val[1] = (u32)(id >> 32);
571 buf->f_namelen = 30;
1da177e4
LT
572 return 0;
573}
574
152a0836
AV
575static struct dentry *affs_mount(struct file_system_type *fs_type,
576 int flags, const char *dev_name, void *data)
1da177e4 577{
152a0836 578 return mount_bdev(fs_type, flags, dev_name, data, affs_fill_super);
1da177e4
LT
579}
580
581static struct file_system_type affs_fs_type = {
582 .owner = THIS_MODULE,
583 .name = "affs",
152a0836 584 .mount = affs_mount,
1da177e4
LT
585 .kill_sb = kill_block_super,
586 .fs_flags = FS_REQUIRES_DEV,
587};
588
589static int __init init_affs_fs(void)
590{
591 int err = init_inodecache();
592 if (err)
593 goto out1;
594 err = register_filesystem(&affs_fs_type);
595 if (err)
596 goto out;
597 return 0;
598out:
599 destroy_inodecache();
600out1:
601 return err;
602}
603
604static void __exit exit_affs_fs(void)
605{
606 unregister_filesystem(&affs_fs_type);
607 destroy_inodecache();
608}
609
610MODULE_DESCRIPTION("Amiga filesystem support for Linux");
611MODULE_LICENSE("GPL");
612
613module_init(init_affs_fs)
614module_exit(exit_affs_fs)
This page took 0.580504 seconds and 5 git commands to generate.