2 * linux/fs/affs/inode.c
4 * (c) 1996 Hans-Joachim Widmaier - Rewritten
6 * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
8 * (C) 1992 Eric Youngdale Modified for ISO 9660 filesystem.
10 * (C) 1991 Linus Torvalds - minix filesystem
13 #include <linux/module.h>
14 #include <linux/errno.h>
16 #include <linux/slab.h>
17 #include <linux/stat.h>
18 #include <linux/sched.h>
19 #include <linux/affs_fs.h>
20 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/locks.h>
24 #include <linux/genhd.h>
25 #include <linux/amigaffs.h>
26 #include <linux/major.h>
27 #include <linux/blkdev.h>
28 #include <linux/init.h>
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
32 extern int *blk_size[];
33 extern struct timezone sys_tz;
35 static int affs_statfs(struct super_block *sb, struct statfs *buf);
36 static int affs_remount (struct super_block *sb, int *flags, char *data);
39 affs_put_super(struct super_block *sb)
41 pr_debug("AFFS: put_super()\n");
43 if (!(sb->s_flags & MS_RDONLY)) {
44 AFFS_ROOT_TAIL(sb, AFFS_SB->s_root_bh)->bm_flag = be32_to_cpu(1);
45 secs_to_datestamp(CURRENT_TIME,
46 &AFFS_ROOT_TAIL(sb, AFFS_SB->s_root_bh)->disk_change);
47 affs_fix_checksum(sb, AFFS_SB->s_root_bh);
48 mark_buffer_dirty(AFFS_SB->s_root_bh);
51 affs_brelse(AFFS_SB->s_bmap_bh);
52 if (AFFS_SB->s_prefix)
53 kfree(AFFS_SB->s_prefix);
54 kfree(AFFS_SB->s_bitmap);
55 affs_brelse(AFFS_SB->s_root_bh);
61 affs_write_super(struct super_block *sb)
65 if (!(sb->s_flags & MS_RDONLY)) {
66 // if (AFFS_SB->s_bitmap[i].bm_bh) {
67 // if (buffer_dirty(AFFS_SB->s_bitmap[i].bm_bh)) {
69 AFFS_ROOT_TAIL(sb, AFFS_SB->s_root_bh)->bm_flag = be32_to_cpu(clean);
70 secs_to_datestamp(CURRENT_TIME,
71 &AFFS_ROOT_TAIL(sb, AFFS_SB->s_root_bh)->disk_change);
72 affs_fix_checksum(sb, AFFS_SB->s_root_bh);
73 mark_buffer_dirty(AFFS_SB->s_root_bh);
74 sb->s_dirt = !clean; /* redo until bitmap synced */
78 pr_debug("AFFS: write_super() at %lu, clean=%d\n", CURRENT_TIME, clean);
81 static struct super_operations affs_sops = {
82 read_inode: affs_read_inode,
83 write_inode: affs_write_inode,
84 put_inode: affs_put_inode,
85 delete_inode: affs_delete_inode,
86 clear_inode: affs_clear_inode,
87 put_super: affs_put_super,
88 write_super: affs_write_super,
90 remount_fs: affs_remount,
94 parse_options(char *options, uid_t *uid, gid_t *gid, int *mode, int *reserved, s32 *root,
95 int *blocksize, char **prefix, char *volume, unsigned long *mount_opts)
97 char *this_char, *value, *optn;
100 /* Fill in defaults */
112 for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
114 if ((value = strchr(this_char,'=')) != NULL)
116 if ((optn = "protect") && !strcmp(this_char, optn)) {
119 *mount_opts |= SF_IMMUTABLE;
120 } else if ((optn = "verbose") && !strcmp(this_char, optn)) {
123 *mount_opts |= SF_VERBOSE;
124 } else if ((optn = "mufs") && !strcmp(this_char, optn)) {
127 *mount_opts |= SF_MUFS;
128 } else if ((f = !strcmp(this_char,"setuid")) || !strcmp(this_char,"setgid")) {
131 printk("AFFS: Argument for set[ug]id option missing\n");
134 (f ? *uid : *gid) = simple_strtoul(value,&value,0);
136 printk("AFFS: Bad set[ug]id argument\n");
139 *mount_opts |= f ? SF_SETUID : SF_SETGID;
142 } else if (!strcmp(this_char,"prefix")) {
144 if (!value || !*value)
146 if (*prefix) { /* Free any previous prefix */
150 *prefix = kmalloc(strlen(value) + 1,GFP_KERNEL);
153 strcpy(*prefix,value);
154 *mount_opts |= SF_PREFIX;
155 } else if (!strcmp(this_char,"volume")) {
157 if (!value || !*value)
159 if (strlen(value) > 30)
161 strncpy(volume,value,30);
162 } else if (!strcmp(this_char,"mode")) {
164 if (!value || !*value)
166 *mode = simple_strtoul(value,&value,8) & 0777;
169 *mount_opts |= SF_SETMODE;
170 } else if (!strcmp(this_char,"reserved")) {
172 if (!value || !*value)
174 *reserved = simple_strtoul(value,&value,0);
177 } else if (!strcmp(this_char,"root")) {
179 if (!value || !*value)
181 *root = simple_strtoul(value,&value,0);
184 } else if (!strcmp(this_char,"bs")) {
186 if (!value || !*value)
188 *blocksize = simple_strtoul(value,&value,0);
191 if (*blocksize != 512 && *blocksize != 1024 && *blocksize != 2048
192 && *blocksize != 4096) {
193 printk ("AFFS: Invalid blocksize (512, 1024, 2048, 4096 allowed)\n");
196 } else if (!strcmp (this_char, "grpquota")
197 || !strcmp (this_char, "noquota")
198 || !strcmp (this_char, "quota")
199 || !strcmp (this_char, "usrquota"))
200 /* Silently ignore the quota options */
203 printk("AFFS: Unrecognized mount option %s\n", this_char);
210 printk("AFFS: The %s option requires an argument\n", optn);
213 printk("AFFS: Option %s does not take an argument\n", optn);
217 /* This function definitely needs to be split up. Some fine day I'll
218 * hopefully have the guts to do so. Until then: sorry for the mess.
221 static struct super_block *
222 affs_read_super(struct super_block *sb, void *data, int silent)
224 struct buffer_head *root_bh = NULL;
225 struct buffer_head *boot_bh;
226 struct inode *root_inode = NULL;
227 kdev_t dev = sb->s_dev;
229 int blocks, size, blocksize;
237 unsigned long mount_flags;
239 pr_debug("AFFS: read_super(%s)\n",data ? (const char *)data : "no options");
241 sb->s_magic = AFFS_SUPER_MAGIC;
242 sb->s_op = &affs_sops;
243 memset(AFFS_SB, 0, sizeof(*AFFS_SB));
244 init_MUTEX(&AFFS_SB->s_bmlock);
246 if (!parse_options(data,&uid,&gid,&i,&reserved,&root_block,
247 &blocksize,&AFFS_SB->s_prefix,
248 AFFS_SB->s_volume, &mount_flags)) {
249 printk(KERN_ERR "AFFS: Error parsing options\n");
252 /* N.B. after this point s_prefix must be released */
254 AFFS_SB->s_flags = mount_flags;
256 AFFS_SB->s_uid = uid;
257 AFFS_SB->s_gid = gid;
258 AFFS_SB->s_reserved= reserved;
260 /* Get the size of the device in 512-byte blocks.
261 * If we later see that the partition uses bigger
262 * blocks, we will have to change it.
265 blocks = blk_size[MAJOR(dev)] ? blk_size[MAJOR(dev)][MINOR(dev)] : 0;
267 printk(KERN_ERR "AFFS: Could not determine device size\n");
270 size = (BLOCK_SIZE / 512) * blocks;
271 pr_debug("AFFS: initial blksize=%d, blocks=%d\n", 512, blocks);
273 affs_set_blocksize(sb, PAGE_SIZE);
274 /* Try to find root block. Its location depends on the block size. */
280 size = size / (blocksize / 512);
282 for (blocksize = i, key = 0; blocksize <= j; blocksize <<= 1, size >>= 1) {
283 AFFS_SB->s_root_block = root_block;
285 AFFS_SB->s_root_block = (reserved + size - 1) / 2;
286 pr_debug("AFFS: setting blocksize to %d\n", blocksize);
287 affs_set_blocksize(sb, blocksize);
288 AFFS_SB->s_partition_size = size;
290 /* The root block location that was calculated above is not
291 * correct if the partition size is an odd number of 512-
292 * byte blocks, which will be rounded down to a number of
293 * 1024-byte blocks, and if there were an even number of
294 * reserved blocks. Ideally, all partition checkers should
295 * report the real number of blocks of the real blocksize,
296 * but since this just cannot be done, we have to try to
297 * find the root block anyways. In the above case, it is one
298 * block behind the calculated one. So we check this one, too.
300 for (num_bm = 0; num_bm < 2; num_bm++) {
301 pr_debug("AFFS: Dev %s, trying root=%u, bs=%d, "
302 "size=%d, reserved=%d\n",
304 AFFS_SB->s_root_block + num_bm,
305 blocksize, size, reserved);
306 root_bh = affs_bread(sb, AFFS_SB->s_root_block + num_bm);
309 if (!affs_checksum_block(sb, root_bh) &&
310 be32_to_cpu(AFFS_ROOT_HEAD(root_bh)->ptype) == T_SHORT &&
311 be32_to_cpu(AFFS_ROOT_TAIL(sb, root_bh)->stype) == ST_ROOT) {
312 AFFS_SB->s_hashsize = blocksize / 4 - 56;
313 AFFS_SB->s_root_block += num_bm;
317 affs_brelse(root_bh);
322 printk(KERN_ERR "AFFS: No valid root block on device %s\n",
326 /* N.B. after this point bh must be released */
328 root_block = AFFS_SB->s_root_block;
330 sb->s_blocksize_bits = blocksize == 512 ? 9 :
331 blocksize == 1024 ? 10 :
332 blocksize == 2048 ? 11 : 12;
334 /* Find out which kind of FS we have */
335 boot_bh = sb_bread(sb, 0);
337 printk(KERN_ERR "AFFS: Cannot read boot block\n");
340 chksum = be32_to_cpu(*(u32 *)boot_bh->b_data);
343 /* Dircache filesystems are compatible with non-dircache ones
344 * when reading. As long as they aren't supported, writing is
347 if ((chksum == FS_DCFFS || chksum == MUFS_DCFFS || chksum == FS_DCOFS
348 || chksum == MUFS_DCOFS) && !(sb->s_flags & MS_RDONLY)) {
349 printk(KERN_NOTICE "AFFS: Dircache FS - mounting %s read only\n",
351 sb->s_flags |= MS_RDONLY;
352 AFFS_SB->s_flags |= SF_READONLY;
358 AFFS_SB->s_flags |= SF_MUFS;
362 AFFS_SB->s_flags |= SF_INTL;
365 AFFS_SB->s_flags |= SF_MUFS;
370 AFFS_SB->s_flags |= SF_MUFS;
373 AFFS_SB->s_flags |= SF_OFS;
374 sb->s_flags |= MS_NOEXEC;
378 AFFS_SB->s_flags |= SF_MUFS;
381 AFFS_SB->s_flags |= SF_INTL | SF_OFS;
382 sb->s_flags |= MS_NOEXEC;
385 printk(KERN_ERR "AFFS: Unknown filesystem on device %s: %08X\n",
386 kdevname(dev), chksum);
390 if (mount_flags & SF_VERBOSE) {
391 chksum = cpu_to_be32(chksum);
392 printk(KERN_NOTICE "AFFS: Mounting volume \"%*s\": Type=%.3s\\%c, Blocksize=%d\n",
393 AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0],
394 AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
395 (char *)&chksum,((char *)&chksum)[3] + '0',blocksize);
398 sb->s_flags |= MS_NODEV | MS_NOSUID;
400 AFFS_SB->s_data_blksize = sb->s_blocksize;
401 if (AFFS_SB->s_flags & SF_OFS)
402 AFFS_SB->s_data_blksize -= 24;
404 /* Keep super block in cache */
405 AFFS_SB->s_root_bh = root_bh;
406 /* N.B. after this point s_root_bh must be released */
408 if (affs_init_bitmap(sb))
411 /* set up enough so that it can read an inode */
413 root_inode = iget(sb, root_block);
414 sb->s_root = d_alloc_root(root_inode);
416 printk(KERN_ERR "AFFS: Get root inode failed\n");
419 sb->s_root->d_op = &affs_dentry_operations;
421 pr_debug("AFFS: s_flags=%lX\n",sb->s_flags);
425 * Begin the cascaded cleanup ...
430 if (AFFS_SB->s_bitmap)
431 kfree(AFFS_SB->s_bitmap);
432 affs_brelse(root_bh);
433 if (AFFS_SB->s_prefix)
434 kfree(AFFS_SB->s_prefix);
439 affs_remount(struct super_block *sb, int *flags, char *data)
447 unsigned long mount_flags;
448 unsigned long read_only = AFFS_SB->s_flags & SF_READONLY;
450 pr_debug("AFFS: remount(flags=0x%x,opts=\"%s\")\n",*flags,data);
452 if (!parse_options(data,&uid,&gid,&mode,&reserved,&root_block,
453 &blocksize,&AFFS_SB->s_prefix,AFFS_SB->s_volume,&mount_flags))
455 AFFS_SB->s_flags = mount_flags | read_only;
456 AFFS_SB->s_mode = mode;
457 AFFS_SB->s_uid = uid;
458 AFFS_SB->s_gid = gid;
460 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
462 if (*flags & MS_RDONLY) {
465 affs_write_super(sb);
466 sb->s_flags |= MS_RDONLY;
467 } else if (!(AFFS_SB->s_flags & SF_READONLY)) {
468 sb->s_flags &= ~MS_RDONLY;
470 affs_warning(sb,"remount","Cannot remount fs read/write because of errors");
477 affs_statfs(struct super_block *sb, struct statfs *buf)
481 pr_debug("AFFS: statfs() partsize=%d, reserved=%d\n",AFFS_SB->s_partition_size,
482 AFFS_SB->s_reserved);
484 free = affs_count_free_blocks(sb);
485 buf->f_type = AFFS_SUPER_MAGIC;
486 buf->f_bsize = sb->s_blocksize;
487 buf->f_blocks = AFFS_SB->s_partition_size - AFFS_SB->s_reserved;
489 buf->f_bavail = free;
493 static DECLARE_FSTYPE_DEV(affs_fs_type, "affs", affs_read_super);
495 static int __init init_affs_fs(void)
497 return register_filesystem(&affs_fs_type);
500 static void __exit exit_affs_fs(void)
502 unregister_filesystem(&affs_fs_type);
507 MODULE_DESCRIPTION("Amiga filesystem support for Linux");
508 MODULE_LICENSE("GPL");
510 module_init(init_affs_fs)
511 module_exit(exit_affs_fs)