clean
[linux-2.4.21-pre4.git] / fs / affs / super.c
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/errno.h>
15 #include <linux/fs.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>
21 #include <linux/mm.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>
31
32 extern int *blk_size[];
33 extern struct timezone sys_tz;
34
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);
37
38 static void
39 affs_put_super(struct super_block *sb)
40 {
41         pr_debug("AFFS: put_super()\n");
42
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);
49         }
50
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);
56
57         return;
58 }
59
60 static void
61 affs_write_super(struct super_block *sb)
62 {
63         int clean = 2;
64
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)) {
68                 //                      clean = 0;
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 */
75         } else
76                 sb->s_dirt = 0;
77
78         pr_debug("AFFS: write_super() at %lu, clean=%d\n", CURRENT_TIME, clean);
79 }
80
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,
89         statfs:         affs_statfs,
90         remount_fs:     affs_remount,
91 };
92
93 static int
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)
96 {
97         char    *this_char, *value, *optn;
98         int      f;
99
100         /* Fill in defaults */
101
102         *uid        = current->uid;
103         *gid        = current->gid;
104         *reserved   = 2;
105         *root       = -1;
106         *blocksize  = -1;
107         volume[0]   = ':';
108         volume[1]   = 0;
109         *mount_opts = 0;
110         if (!options)
111                 return 1;
112         for (this_char = strtok(options,","); this_char; this_char = strtok(NULL,",")) {
113                 f = 0;
114                 if ((value = strchr(this_char,'=')) != NULL)
115                         *value++ = 0;
116                 if ((optn = "protect") && !strcmp(this_char, optn)) {
117                         if (value)
118                                 goto out_inv_arg;
119                         *mount_opts |= SF_IMMUTABLE;
120                 } else if ((optn = "verbose") && !strcmp(this_char, optn)) {
121                         if (value)
122                                 goto out_inv_arg;
123                         *mount_opts |= SF_VERBOSE;
124                 } else if ((optn = "mufs") && !strcmp(this_char, optn)) {
125                         if (value)
126                                 goto out_inv_arg;
127                         *mount_opts |= SF_MUFS;
128                 } else if ((f = !strcmp(this_char,"setuid")) || !strcmp(this_char,"setgid")) {
129                         if (value) {
130                                 if (!*value) {
131                                         printk("AFFS: Argument for set[ug]id option missing\n");
132                                         return 0;
133                                 } else {
134                                         (f ? *uid : *gid) = simple_strtoul(value,&value,0);
135                                         if (*value) {
136                                                 printk("AFFS: Bad set[ug]id argument\n");
137                                                 return 0;
138                                         }
139                                         *mount_opts |= f ? SF_SETUID : SF_SETGID;
140                                 }
141                         }
142                 } else if (!strcmp(this_char,"prefix")) {
143                         optn = "prefix";
144                         if (!value || !*value)
145                                 goto out_no_arg;
146                         if (*prefix) {          /* Free any previous prefix */
147                                 kfree(*prefix);
148                                 *prefix = NULL;
149                         }
150                         *prefix = kmalloc(strlen(value) + 1,GFP_KERNEL);
151                         if (!*prefix)
152                                 return 0;
153                         strcpy(*prefix,value);
154                         *mount_opts |= SF_PREFIX;
155                 } else if (!strcmp(this_char,"volume")) {
156                         optn = "volume";
157                         if (!value || !*value)
158                                 goto out_no_arg;
159                         if (strlen(value) > 30)
160                                 value[30] = 0;
161                         strncpy(volume,value,30);
162                 } else if (!strcmp(this_char,"mode")) {
163                         optn = "mode";
164                         if (!value || !*value)
165                                 goto out_no_arg;
166                         *mode = simple_strtoul(value,&value,8) & 0777;
167                         if (*value)
168                                 return 0;
169                         *mount_opts |= SF_SETMODE;
170                 } else if (!strcmp(this_char,"reserved")) {
171                         optn = "reserved";
172                         if (!value || !*value)
173                                 goto out_no_arg;
174                         *reserved = simple_strtoul(value,&value,0);
175                         if (*value)
176                                 return 0;
177                 } else if (!strcmp(this_char,"root")) {
178                         optn = "root";
179                         if (!value || !*value)
180                                 goto out_no_arg;
181                         *root = simple_strtoul(value,&value,0);
182                         if (*value)
183                                 return 0;
184                 } else if (!strcmp(this_char,"bs")) {
185                         optn = "bs";
186                         if (!value || !*value)
187                                 goto out_no_arg;
188                         *blocksize = simple_strtoul(value,&value,0);
189                         if (*value)
190                                 return 0;
191                         if (*blocksize != 512 && *blocksize != 1024 && *blocksize != 2048
192                             && *blocksize != 4096) {
193                                 printk ("AFFS: Invalid blocksize (512, 1024, 2048, 4096 allowed)\n");
194                                 return 0;
195                         }
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 */
201                         ;
202                 else {
203                         printk("AFFS: Unrecognized mount option %s\n", this_char);
204                         return 0;
205                 }
206         }
207         return 1;
208
209 out_no_arg:
210         printk("AFFS: The %s option requires an argument\n", optn);
211         return 0;
212 out_inv_arg:
213         printk("AFFS: Option %s does not take an argument\n", optn);
214         return 0;
215 }
216
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.
219  */
220
221 static struct super_block *
222 affs_read_super(struct super_block *sb, void *data, int silent)
223 {
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;
228         s32                      root_block;
229         int                      blocks, size, blocksize;
230         u32                      chksum;
231         int                      num_bm;
232         int                      i, j;
233         s32                      key;
234         uid_t                    uid;
235         gid_t                    gid;
236         int                      reserved;
237         unsigned long            mount_flags;
238
239         pr_debug("AFFS: read_super(%s)\n",data ? (const char *)data : "no options");
240
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);
245
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");
250                 return NULL;
251         }
252         /* N.B. after this point s_prefix must be released */
253
254         AFFS_SB->s_flags   = mount_flags;
255         AFFS_SB->s_mode    = i;
256         AFFS_SB->s_uid     = uid;
257         AFFS_SB->s_gid     = gid;
258         AFFS_SB->s_reserved= reserved;
259
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.
263          */
264
265         blocks = blk_size[MAJOR(dev)] ? blk_size[MAJOR(dev)][MINOR(dev)] : 0;
266         if (!blocks) {
267                 printk(KERN_ERR "AFFS: Could not determine device size\n");
268                 goto out_error;
269         }
270         size = (BLOCK_SIZE / 512) * blocks;
271         pr_debug("AFFS: initial blksize=%d, blocks=%d\n", 512, blocks);
272
273         affs_set_blocksize(sb, PAGE_SIZE);
274         /* Try to find root block. Its location depends on the block size. */
275
276         i = 512;
277         j = 4096;
278         if (blocksize > 0) {
279                 i = j = blocksize;
280                 size = size / (blocksize / 512);
281         }
282         for (blocksize = i, key = 0; blocksize <= j; blocksize <<= 1, size >>= 1) {
283                 AFFS_SB->s_root_block = root_block;
284                 if (root_block < 0)
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;
289
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.
299                  */
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",
303                                 kdevname(dev),
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);
307                         if (!root_bh)
308                                 continue;
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;
314                                 key                        = 1;
315                                 goto got_root;
316                         }
317                         affs_brelse(root_bh);
318                         root_bh = NULL;
319                 }
320         }
321         if (!silent)
322                 printk(KERN_ERR "AFFS: No valid root block on device %s\n",
323                         kdevname(dev));
324         goto out_error;
325
326         /* N.B. after this point bh must be released */
327 got_root:
328         root_block = AFFS_SB->s_root_block;
329
330         sb->s_blocksize_bits = blocksize == 512 ? 9 :
331                                blocksize == 1024 ? 10 :
332                                blocksize == 2048 ? 11 : 12;
333
334         /* Find out which kind of FS we have */
335         boot_bh = sb_bread(sb, 0);
336         if (!boot_bh) {
337                 printk(KERN_ERR "AFFS: Cannot read boot block\n");
338                 goto out_error;
339         }
340         chksum = be32_to_cpu(*(u32 *)boot_bh->b_data);
341         brelse(boot_bh);
342
343         /* Dircache filesystems are compatible with non-dircache ones
344          * when reading. As long as they aren't supported, writing is
345          * not recommended.
346          */
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",
350                         kdevname(dev));
351                 sb->s_flags |= MS_RDONLY;
352                 AFFS_SB->s_flags |= SF_READONLY;
353         }
354         switch (chksum) {
355                 case MUFS_FS:
356                 case MUFS_INTLFFS:
357                 case MUFS_DCFFS:
358                         AFFS_SB->s_flags |= SF_MUFS;
359                         /* fall thru */
360                 case FS_INTLFFS:
361                 case FS_DCFFS:
362                         AFFS_SB->s_flags |= SF_INTL;
363                         break;
364                 case MUFS_FFS:
365                         AFFS_SB->s_flags |= SF_MUFS;
366                         break;
367                 case FS_FFS:
368                         break;
369                 case MUFS_OFS:
370                         AFFS_SB->s_flags |= SF_MUFS;
371                         /* fall thru */
372                 case FS_OFS:
373                         AFFS_SB->s_flags |= SF_OFS;
374                         sb->s_flags |= MS_NOEXEC;
375                         break;
376                 case MUFS_DCOFS:
377                 case MUFS_INTLOFS:
378                         AFFS_SB->s_flags |= SF_MUFS;
379                 case FS_DCOFS:
380                 case FS_INTLOFS:
381                         AFFS_SB->s_flags |= SF_INTL | SF_OFS;
382                         sb->s_flags |= MS_NOEXEC;
383                         break;
384                 default:
385                         printk(KERN_ERR "AFFS: Unknown filesystem on device %s: %08X\n",
386                                 kdevname(dev), chksum);
387                         goto out_error;
388         }
389
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);
396         }
397
398         sb->s_flags |= MS_NODEV | MS_NOSUID;
399
400         AFFS_SB->s_data_blksize = sb->s_blocksize;
401         if (AFFS_SB->s_flags & SF_OFS)
402                 AFFS_SB->s_data_blksize -= 24;
403
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 */
407
408         if (affs_init_bitmap(sb))
409                 goto out_error;
410
411         /* set up enough so that it can read an inode */
412
413         root_inode = iget(sb, root_block);
414         sb->s_root = d_alloc_root(root_inode);
415         if (!sb->s_root) {
416                 printk(KERN_ERR "AFFS: Get root inode failed\n");
417                 goto out_error;
418         }
419         sb->s_root->d_op = &affs_dentry_operations;
420
421         pr_debug("AFFS: s_flags=%lX\n",sb->s_flags);
422         return sb;
423
424         /*
425          * Begin the cascaded cleanup ...
426          */
427 out_error:
428         if (root_inode)
429                 iput(root_inode);
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);
435         return NULL;
436 }
437
438 static int
439 affs_remount(struct super_block *sb, int *flags, char *data)
440 {
441         int                      blocksize;
442         uid_t                    uid;
443         gid_t                    gid;
444         int                      mode;
445         int                      reserved;
446         int                      root_block;
447         unsigned long            mount_flags;
448         unsigned long            read_only = AFFS_SB->s_flags & SF_READONLY;
449
450         pr_debug("AFFS: remount(flags=0x%x,opts=\"%s\")\n",*flags,data);
451
452         if (!parse_options(data,&uid,&gid,&mode,&reserved,&root_block,
453             &blocksize,&AFFS_SB->s_prefix,AFFS_SB->s_volume,&mount_flags))
454                 return -EINVAL;
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;
459
460         if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
461                 return 0;
462         if (*flags & MS_RDONLY) {
463                 sb->s_dirt = 1;
464                 while (sb->s_dirt)
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;
469         } else {
470                 affs_warning(sb,"remount","Cannot remount fs read/write because of errors");
471                 return -EINVAL;
472         }
473         return 0;
474 }
475
476 static int
477 affs_statfs(struct super_block *sb, struct statfs *buf)
478 {
479         int              free;
480
481         pr_debug("AFFS: statfs() partsize=%d, reserved=%d\n",AFFS_SB->s_partition_size,
482              AFFS_SB->s_reserved);
483
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;
488         buf->f_bfree   = free;
489         buf->f_bavail  = free;
490         return 0;
491 }
492
493 static DECLARE_FSTYPE_DEV(affs_fs_type, "affs", affs_read_super);
494
495 static int __init init_affs_fs(void)
496 {
497         return register_filesystem(&affs_fs_type);
498 }
499
500 static void __exit exit_affs_fs(void)
501 {
502         unregister_filesystem(&affs_fs_type);
503 }
504
505 EXPORT_NO_SYMBOLS;
506
507 MODULE_DESCRIPTION("Amiga filesystem support for Linux");
508 MODULE_LICENSE("GPL");
509
510 module_init(init_affs_fs)
511 module_exit(exit_affs_fs)