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 if (AFFS_SB->s_prefix)
52 kfree(AFFS_SB->s_prefix);
54 affs_brelse(AFFS_SB->s_root_bh);
60 affs_write_super(struct super_block *sb)
64 if (!(sb->s_flags & MS_RDONLY)) {
65 // if (AFFS_SB->s_bitmap[i].bm_bh) {
66 // if (buffer_dirty(AFFS_SB->s_bitmap[i].bm_bh)) {
68 AFFS_ROOT_TAIL(sb, AFFS_SB->s_root_bh)->bm_flag = be32_to_cpu(clean);
69 secs_to_datestamp(CURRENT_TIME,
70 &AFFS_ROOT_TAIL(sb, AFFS_SB->s_root_bh)->disk_change);
71 affs_fix_checksum(sb, AFFS_SB->s_root_bh);
72 mark_buffer_dirty(AFFS_SB->s_root_bh);
73 sb->s_dirt = !clean; /* redo until bitmap synced */
77 pr_debug("AFFS: write_super() at %lu, clean=%d\n", CURRENT_TIME, clean);
80 static struct super_operations affs_sops = {
81 read_inode: affs_read_inode,
82 write_inode: affs_write_inode,
83 put_inode: affs_put_inode,
84 delete_inode: affs_delete_inode,
85 clear_inode: affs_clear_inode,
86 put_super: affs_put_super,
87 write_super: affs_write_super,
89 remount_fs: affs_remount,
93 parse_options(char *options, uid_t *uid, gid_t *gid, int *mode, int *reserved, s32 *root,
94 int *blocksize, char **prefix, char *volume, unsigned long *mount_opts)
96 char *this_char, *value, *optn;
99 /* Fill in defaults */
111 for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
113 if ((value = strchr(this_char,'=')) != NULL)
115 if ((optn = "protect") && !strcmp(this_char, optn)) {
118 *mount_opts |= SF_IMMUTABLE;
119 } else if ((optn = "verbose") && !strcmp(this_char, optn)) {
122 *mount_opts |= SF_VERBOSE;
123 } else if ((optn = "mufs") && !strcmp(this_char, optn)) {
126 *mount_opts |= SF_MUFS;
127 } else if ((f = !strcmp(this_char,"setuid")) || !strcmp(this_char,"setgid")) {
130 printk("AFFS: Argument for set[ug]id option missing\n");
133 *(f ? uid : gid) = simple_strtoul(value,&value,0);
135 printk("AFFS: Bad set[ug]id argument\n");
138 *mount_opts |= f ? SF_SETUID : SF_SETGID;
141 } else if (!strcmp(this_char,"prefix")) {
143 if (!value || !*value)
145 if (*prefix) { /* Free any previous prefix */
149 *prefix = kmalloc(strlen(value) + 1,GFP_KERNEL);
152 strcpy(*prefix,value);
153 *mount_opts |= SF_PREFIX;
154 } else if (!strcmp(this_char,"volume")) {
156 if (!value || !*value)
158 if (strlen(value) > 30)
160 strncpy(volume,value,30);
161 } else if (!strcmp(this_char,"mode")) {
163 if (!value || !*value)
165 *mode = simple_strtoul(value,&value,8) & 0777;
168 *mount_opts |= SF_SETMODE;
169 } else if (!strcmp(this_char,"reserved")) {
171 if (!value || !*value)
173 *reserved = simple_strtoul(value,&value,0);
176 } else if (!strcmp(this_char,"root")) {
178 if (!value || !*value)
180 *root = simple_strtoul(value,&value,0);
183 } else if (!strcmp(this_char,"bs")) {
185 if (!value || !*value)
187 *blocksize = simple_strtoul(value,&value,0);
190 if (*blocksize != 512 && *blocksize != 1024 && *blocksize != 2048
191 && *blocksize != 4096) {
192 printk ("AFFS: Invalid blocksize (512, 1024, 2048, 4096 allowed)\n");
195 } else if (!strcmp (this_char, "grpquota")
196 || !strcmp (this_char, "noquota")
197 || !strcmp (this_char, "quota")
198 || !strcmp (this_char, "usrquota"))
199 /* Silently ignore the quota options */
202 printk("AFFS: Unrecognized mount option %s\n", this_char);
209 printk("AFFS: The %s option requires an argument\n", optn);
212 printk("AFFS: Option %s does not take an argument\n", optn);
216 /* This function definitely needs to be split up. Some fine day I'll
217 * hopefully have the guts to do so. Until then: sorry for the mess.
220 static struct super_block *
221 affs_read_super(struct super_block *sb, void *data, int silent)
223 struct buffer_head *root_bh = NULL;
224 struct buffer_head *boot_bh;
225 struct inode *root_inode = NULL;
226 kdev_t dev = sb->s_dev;
228 int blocks, size, blocksize;
236 unsigned long mount_flags;
237 int tmp_flags; /* fix remount prototype... */
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;
357 AFFS_SB->s_flags |= SF_MUFS;
361 AFFS_SB->s_flags |= SF_INTL;
364 AFFS_SB->s_flags |= SF_MUFS;
369 AFFS_SB->s_flags |= SF_MUFS;
372 AFFS_SB->s_flags |= SF_OFS;
373 sb->s_flags |= MS_NOEXEC;
377 AFFS_SB->s_flags |= SF_MUFS;
380 AFFS_SB->s_flags |= SF_INTL | SF_OFS;
381 sb->s_flags |= MS_NOEXEC;
384 printk(KERN_ERR "AFFS: Unknown filesystem on device %s: %08X\n",
385 kdevname(dev), chksum);
389 if (mount_flags & SF_VERBOSE) {
390 chksum = cpu_to_be32(chksum);
391 printk(KERN_NOTICE "AFFS: Mounting volume \"%*s\": Type=%.3s\\%c, Blocksize=%d\n",
392 AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0],
393 AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
394 (char *)&chksum,((char *)&chksum)[3] + '0',blocksize);
397 sb->s_flags |= MS_NODEV | MS_NOSUID;
399 AFFS_SB->s_data_blksize = sb->s_blocksize;
400 if (AFFS_SB->s_flags & SF_OFS)
401 AFFS_SB->s_data_blksize -= 24;
403 /* Keep super block in cache */
404 AFFS_SB->s_root_bh = root_bh;
405 /* N.B. after this point s_root_bh must be released */
407 tmp_flags = sb->s_flags;
408 if (affs_init_bitmap(sb, &tmp_flags))
410 sb->s_flags = tmp_flags;
412 /* set up enough so that it can read an inode */
414 root_inode = iget(sb, root_block);
415 sb->s_root = d_alloc_root(root_inode);
417 printk(KERN_ERR "AFFS: Get root inode failed\n");
420 sb->s_root->d_op = &affs_dentry_operations;
422 pr_debug("AFFS: s_flags=%lX\n",sb->s_flags);
426 * Begin the cascaded cleanup ...
431 if (AFFS_SB->s_bitmap)
432 kfree(AFFS_SB->s_bitmap);
433 affs_brelse(root_bh);
434 if (AFFS_SB->s_prefix)
435 kfree(AFFS_SB->s_prefix);
440 affs_remount(struct super_block *sb, int *flags, char *data)
448 unsigned long mount_flags;
451 pr_debug("AFFS: remount(flags=0x%x,opts=\"%s\")\n",*flags,data);
453 if (!parse_options(data,&uid,&gid,&mode,&reserved,&root_block,
454 &blocksize,&AFFS_SB->s_prefix,AFFS_SB->s_volume,&mount_flags))
456 AFFS_SB->s_flags = mount_flags;
457 AFFS_SB->s_mode = mode;
458 AFFS_SB->s_uid = uid;
459 AFFS_SB->s_gid = gid;
461 if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
463 if (*flags & MS_RDONLY) {
466 affs_write_super(sb);
467 affs_free_bitmap(sb);
469 res = affs_init_bitmap(sb, flags);
475 affs_statfs(struct super_block *sb, struct statfs *buf)
479 pr_debug("AFFS: statfs() partsize=%d, reserved=%d\n",AFFS_SB->s_partition_size,
480 AFFS_SB->s_reserved);
482 free = affs_count_free_blocks(sb);
483 buf->f_type = AFFS_SUPER_MAGIC;
484 buf->f_bsize = sb->s_blocksize;
485 buf->f_blocks = AFFS_SB->s_partition_size - AFFS_SB->s_reserved;
487 buf->f_bavail = free;
491 static DECLARE_FSTYPE_DEV(affs_fs_type, "affs", affs_read_super);
493 static int __init init_affs_fs(void)
495 return register_filesystem(&affs_fs_type);
498 static void __exit exit_affs_fs(void)
500 unregister_filesystem(&affs_fs_type);
505 MODULE_DESCRIPTION("Amiga filesystem support for Linux");
506 MODULE_LICENSE("GPL");
508 module_init(init_affs_fs)
509 module_exit(exit_affs_fs)