fs/adfs: super: correct superblock flags
[linux-2.6-block.git] / fs / adfs / super.c
1 /*
2  *  linux/fs/adfs/super.c
3  *
4  *  Copyright (C) 1997-1999 Russell King
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 version 2 as
8  * published by the Free Software Foundation.
9  */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/parser.h>
13 #include <linux/mount.h>
14 #include <linux/seq_file.h>
15 #include <linux/slab.h>
16 #include <linux/statfs.h>
17 #include <linux/user_namespace.h>
18 #include "adfs.h"
19 #include "dir_f.h"
20 #include "dir_fplus.h"
21
22 #define ADFS_SB_FLAGS SB_NOATIME
23
24 #define ADFS_DEFAULT_OWNER_MASK S_IRWXU
25 #define ADFS_DEFAULT_OTHER_MASK (S_IRWXG | S_IRWXO)
26
27 void __adfs_error(struct super_block *sb, const char *function, const char *fmt, ...)
28 {
29         struct va_format vaf;
30         va_list args;
31
32         va_start(args, fmt);
33         vaf.fmt = fmt;
34         vaf.va = &args;
35
36         printk(KERN_CRIT "ADFS-fs error (device %s)%s%s: %pV\n",
37                 sb->s_id, function ? ": " : "",
38                 function ? function : "", &vaf);
39
40         va_end(args);
41 }
42
43 void adfs_msg(struct super_block *sb, const char *pfx, const char *fmt, ...)
44 {
45         struct va_format vaf;
46         va_list args;
47
48         va_start(args, fmt);
49         vaf.fmt = fmt;
50         vaf.va = &args;
51         printk("%sADFS-fs (%s): %pV\n", pfx, sb->s_id, &vaf);
52         va_end(args);
53 }
54
55 static int adfs_checkdiscrecord(struct adfs_discrecord *dr)
56 {
57         int i;
58
59         /* sector size must be 256, 512 or 1024 bytes */
60         if (dr->log2secsize != 8 &&
61             dr->log2secsize != 9 &&
62             dr->log2secsize != 10)
63                 return 1;
64
65         /* idlen must be at least log2secsize + 3 */
66         if (dr->idlen < dr->log2secsize + 3)
67                 return 1;
68
69         /* we cannot have such a large disc that we
70          * are unable to represent sector offsets in
71          * 32 bits.  This works out at 2.0 TB.
72          */
73         if (le32_to_cpu(dr->disc_size_high) >> dr->log2secsize)
74                 return 1;
75
76         /* idlen must be no greater than 19 v2 [1.0] */
77         if (dr->idlen > 19)
78                 return 1;
79
80         /* reserved bytes should be zero */
81         for (i = 0; i < sizeof(dr->unused52); i++)
82                 if (dr->unused52[i] != 0)
83                         return 1;
84
85         return 0;
86 }
87
88 static unsigned char adfs_calczonecheck(struct super_block *sb, unsigned char *map)
89 {
90         unsigned int v0, v1, v2, v3;
91         int i;
92
93         v0 = v1 = v2 = v3 = 0;
94         for (i = sb->s_blocksize - 4; i; i -= 4) {
95                 v0 += map[i]     + (v3 >> 8);
96                 v3 &= 0xff;
97                 v1 += map[i + 1] + (v0 >> 8);
98                 v0 &= 0xff;
99                 v2 += map[i + 2] + (v1 >> 8);
100                 v1 &= 0xff;
101                 v3 += map[i + 3] + (v2 >> 8);
102                 v2 &= 0xff;
103         }
104         v0 +=           v3 >> 8;
105         v1 += map[1] + (v0 >> 8);
106         v2 += map[2] + (v1 >> 8);
107         v3 += map[3] + (v2 >> 8);
108
109         return v0 ^ v1 ^ v2 ^ v3;
110 }
111
112 static int adfs_checkmap(struct super_block *sb, struct adfs_discmap *dm)
113 {
114         unsigned char crosscheck = 0, zonecheck = 1;
115         int i;
116
117         for (i = 0; i < ADFS_SB(sb)->s_map_size; i++) {
118                 unsigned char *map;
119
120                 map = dm[i].dm_bh->b_data;
121
122                 if (adfs_calczonecheck(sb, map) != map[0]) {
123                         adfs_error(sb, "zone %d fails zonecheck", i);
124                         zonecheck = 0;
125                 }
126                 crosscheck ^= map[3];
127         }
128         if (crosscheck != 0xff)
129                 adfs_error(sb, "crosscheck != 0xff");
130         return crosscheck == 0xff && zonecheck;
131 }
132
133 static void adfs_put_super(struct super_block *sb)
134 {
135         int i;
136         struct adfs_sb_info *asb = ADFS_SB(sb);
137
138         for (i = 0; i < asb->s_map_size; i++)
139                 brelse(asb->s_map[i].dm_bh);
140         kfree(asb->s_map);
141         kfree_rcu(asb, rcu);
142 }
143
144 static int adfs_show_options(struct seq_file *seq, struct dentry *root)
145 {
146         struct adfs_sb_info *asb = ADFS_SB(root->d_sb);
147
148         if (!uid_eq(asb->s_uid, GLOBAL_ROOT_UID))
149                 seq_printf(seq, ",uid=%u", from_kuid_munged(&init_user_ns, asb->s_uid));
150         if (!gid_eq(asb->s_gid, GLOBAL_ROOT_GID))
151                 seq_printf(seq, ",gid=%u", from_kgid_munged(&init_user_ns, asb->s_gid));
152         if (asb->s_owner_mask != ADFS_DEFAULT_OWNER_MASK)
153                 seq_printf(seq, ",ownmask=%o", asb->s_owner_mask);
154         if (asb->s_other_mask != ADFS_DEFAULT_OTHER_MASK)
155                 seq_printf(seq, ",othmask=%o", asb->s_other_mask);
156         if (asb->s_ftsuffix != 0)
157                 seq_printf(seq, ",ftsuffix=%u", asb->s_ftsuffix);
158
159         return 0;
160 }
161
162 enum {Opt_uid, Opt_gid, Opt_ownmask, Opt_othmask, Opt_ftsuffix, Opt_err};
163
164 static const match_table_t tokens = {
165         {Opt_uid, "uid=%u"},
166         {Opt_gid, "gid=%u"},
167         {Opt_ownmask, "ownmask=%o"},
168         {Opt_othmask, "othmask=%o"},
169         {Opt_ftsuffix, "ftsuffix=%u"},
170         {Opt_err, NULL}
171 };
172
173 static int parse_options(struct super_block *sb, char *options)
174 {
175         char *p;
176         struct adfs_sb_info *asb = ADFS_SB(sb);
177         int option;
178
179         if (!options)
180                 return 0;
181
182         while ((p = strsep(&options, ",")) != NULL) {
183                 substring_t args[MAX_OPT_ARGS];
184                 int token;
185                 if (!*p)
186                         continue;
187
188                 token = match_token(p, tokens, args);
189                 switch (token) {
190                 case Opt_uid:
191                         if (match_int(args, &option))
192                                 return -EINVAL;
193                         asb->s_uid = make_kuid(current_user_ns(), option);
194                         if (!uid_valid(asb->s_uid))
195                                 return -EINVAL;
196                         break;
197                 case Opt_gid:
198                         if (match_int(args, &option))
199                                 return -EINVAL;
200                         asb->s_gid = make_kgid(current_user_ns(), option);
201                         if (!gid_valid(asb->s_gid))
202                                 return -EINVAL;
203                         break;
204                 case Opt_ownmask:
205                         if (match_octal(args, &option))
206                                 return -EINVAL;
207                         asb->s_owner_mask = option;
208                         break;
209                 case Opt_othmask:
210                         if (match_octal(args, &option))
211                                 return -EINVAL;
212                         asb->s_other_mask = option;
213                         break;
214                 case Opt_ftsuffix:
215                         if (match_int(args, &option))
216                                 return -EINVAL;
217                         asb->s_ftsuffix = option;
218                         break;
219                 default:
220                         adfs_msg(sb, KERN_ERR,
221                                  "unrecognised mount option \"%s\" or missing value",
222                                  p);
223                         return -EINVAL;
224                 }
225         }
226         return 0;
227 }
228
229 static int adfs_remount(struct super_block *sb, int *flags, char *data)
230 {
231         sync_filesystem(sb);
232         *flags |= ADFS_SB_FLAGS;
233         return parse_options(sb, data);
234 }
235
236 static int adfs_statfs(struct dentry *dentry, struct kstatfs *buf)
237 {
238         struct super_block *sb = dentry->d_sb;
239         struct adfs_sb_info *sbi = ADFS_SB(sb);
240         struct adfs_discrecord *dr = adfs_map_discrecord(sbi->s_map);
241         u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
242
243         buf->f_type    = ADFS_SUPER_MAGIC;
244         buf->f_namelen = sbi->s_namelen;
245         buf->f_bsize   = sb->s_blocksize;
246         buf->f_blocks  = adfs_disc_size(dr) >> sb->s_blocksize_bits;
247         buf->f_files   = sbi->s_ids_per_zone * sbi->s_map_size;
248         buf->f_bavail  =
249         buf->f_bfree   = adfs_map_free(sb);
250         buf->f_ffree   = (long)(buf->f_bfree * buf->f_files) / (long)buf->f_blocks;
251         buf->f_fsid.val[0] = (u32)id;
252         buf->f_fsid.val[1] = (u32)(id >> 32);
253
254         return 0;
255 }
256
257 static struct kmem_cache *adfs_inode_cachep;
258
259 static struct inode *adfs_alloc_inode(struct super_block *sb)
260 {
261         struct adfs_inode_info *ei;
262         ei = kmem_cache_alloc(adfs_inode_cachep, GFP_KERNEL);
263         if (!ei)
264                 return NULL;
265         return &ei->vfs_inode;
266 }
267
268 static void adfs_free_inode(struct inode *inode)
269 {
270         kmem_cache_free(adfs_inode_cachep, ADFS_I(inode));
271 }
272
273 static void init_once(void *foo)
274 {
275         struct adfs_inode_info *ei = (struct adfs_inode_info *) foo;
276
277         inode_init_once(&ei->vfs_inode);
278 }
279
280 static int __init init_inodecache(void)
281 {
282         adfs_inode_cachep = kmem_cache_create("adfs_inode_cache",
283                                              sizeof(struct adfs_inode_info),
284                                              0, (SLAB_RECLAIM_ACCOUNT|
285                                                 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
286                                              init_once);
287         if (adfs_inode_cachep == NULL)
288                 return -ENOMEM;
289         return 0;
290 }
291
292 static void destroy_inodecache(void)
293 {
294         /*
295          * Make sure all delayed rcu free inodes are flushed before we
296          * destroy cache.
297          */
298         rcu_barrier();
299         kmem_cache_destroy(adfs_inode_cachep);
300 }
301
302 static const struct super_operations adfs_sops = {
303         .alloc_inode    = adfs_alloc_inode,
304         .free_inode     = adfs_free_inode,
305         .drop_inode     = generic_delete_inode,
306         .write_inode    = adfs_write_inode,
307         .put_super      = adfs_put_super,
308         .statfs         = adfs_statfs,
309         .remount_fs     = adfs_remount,
310         .show_options   = adfs_show_options,
311 };
312
313 static struct adfs_discmap *adfs_read_map(struct super_block *sb, struct adfs_discrecord *dr)
314 {
315         struct adfs_discmap *dm;
316         unsigned int map_addr, zone_size, nzones;
317         int i, zone;
318         struct adfs_sb_info *asb = ADFS_SB(sb);
319
320         nzones    = asb->s_map_size;
321         zone_size = (8 << dr->log2secsize) - le16_to_cpu(dr->zone_spare);
322         map_addr  = (nzones >> 1) * zone_size -
323                      ((nzones > 1) ? ADFS_DR_SIZE_BITS : 0);
324         map_addr  = signed_asl(map_addr, asb->s_map2blk);
325
326         asb->s_ids_per_zone = zone_size / (asb->s_idlen + 1);
327
328         dm = kmalloc_array(nzones, sizeof(*dm), GFP_KERNEL);
329         if (dm == NULL) {
330                 adfs_error(sb, "not enough memory");
331                 return ERR_PTR(-ENOMEM);
332         }
333
334         for (zone = 0; zone < nzones; zone++, map_addr++) {
335                 dm[zone].dm_startbit = 0;
336                 dm[zone].dm_endbit   = zone_size;
337                 dm[zone].dm_startblk = zone * zone_size - ADFS_DR_SIZE_BITS;
338                 dm[zone].dm_bh       = sb_bread(sb, map_addr);
339
340                 if (!dm[zone].dm_bh) {
341                         adfs_error(sb, "unable to read map");
342                         goto error_free;
343                 }
344         }
345
346         /* adjust the limits for the first and last map zones */
347         i = zone - 1;
348         dm[0].dm_startblk = 0;
349         dm[0].dm_startbit = ADFS_DR_SIZE_BITS;
350         dm[i].dm_endbit   = (adfs_disc_size(dr) >> dr->log2bpmb) +
351                             (ADFS_DR_SIZE_BITS - i * zone_size);
352
353         if (adfs_checkmap(sb, dm))
354                 return dm;
355
356         adfs_error(sb, "map corrupted");
357
358 error_free:
359         while (--zone >= 0)
360                 brelse(dm[zone].dm_bh);
361
362         kfree(dm);
363         return ERR_PTR(-EIO);
364 }
365
366 static int adfs_fill_super(struct super_block *sb, void *data, int silent)
367 {
368         struct adfs_discrecord *dr;
369         struct buffer_head *bh;
370         struct object_info root_obj;
371         unsigned char *b_data;
372         struct adfs_sb_info *asb;
373         struct inode *root;
374         int ret = -EINVAL;
375
376         sb->s_flags |= ADFS_SB_FLAGS;
377
378         asb = kzalloc(sizeof(*asb), GFP_KERNEL);
379         if (!asb)
380                 return -ENOMEM;
381         sb->s_fs_info = asb;
382
383         /* set default options */
384         asb->s_uid = GLOBAL_ROOT_UID;
385         asb->s_gid = GLOBAL_ROOT_GID;
386         asb->s_owner_mask = ADFS_DEFAULT_OWNER_MASK;
387         asb->s_other_mask = ADFS_DEFAULT_OTHER_MASK;
388         asb->s_ftsuffix = 0;
389
390         if (parse_options(sb, data))
391                 goto error;
392
393         sb_set_blocksize(sb, BLOCK_SIZE);
394         if (!(bh = sb_bread(sb, ADFS_DISCRECORD / BLOCK_SIZE))) {
395                 adfs_msg(sb, KERN_ERR, "error: unable to read superblock");
396                 ret = -EIO;
397                 goto error;
398         }
399
400         b_data = bh->b_data + (ADFS_DISCRECORD % BLOCK_SIZE);
401
402         if (adfs_checkbblk(b_data)) {
403                 ret = -EINVAL;
404                 goto error_badfs;
405         }
406
407         dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET);
408
409         /*
410          * Do some sanity checks on the ADFS disc record
411          */
412         if (adfs_checkdiscrecord(dr)) {
413                 ret = -EINVAL;
414                 goto error_badfs;
415         }
416
417         brelse(bh);
418         if (sb_set_blocksize(sb, 1 << dr->log2secsize)) {
419                 bh = sb_bread(sb, ADFS_DISCRECORD / sb->s_blocksize);
420                 if (!bh) {
421                         adfs_msg(sb, KERN_ERR,
422                                  "error: couldn't read superblock on 2nd try.");
423                         ret = -EIO;
424                         goto error;
425                 }
426                 b_data = bh->b_data + (ADFS_DISCRECORD % sb->s_blocksize);
427                 if (adfs_checkbblk(b_data)) {
428                         adfs_msg(sb, KERN_ERR,
429                                  "error: disc record mismatch, very weird!");
430                         ret = -EINVAL;
431                         goto error_free_bh;
432                 }
433                 dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET);
434         } else {
435                 if (!silent)
436                         adfs_msg(sb, KERN_ERR,
437                                  "error: unsupported blocksize");
438                 ret = -EINVAL;
439                 goto error;
440         }
441
442         /*
443          * blocksize on this device should now be set to the ADFS log2secsize
444          */
445
446         sb->s_magic             = ADFS_SUPER_MAGIC;
447         asb->s_idlen            = dr->idlen;
448         asb->s_map_size         = dr->nzones | (dr->nzones_high << 8);
449         asb->s_map2blk          = dr->log2bpmb - dr->log2secsize;
450         asb->s_log2sharesize    = dr->log2sharesize;
451
452         asb->s_map = adfs_read_map(sb, dr);
453         if (IS_ERR(asb->s_map)) {
454                 ret =  PTR_ERR(asb->s_map);
455                 goto error_free_bh;
456         }
457
458         brelse(bh);
459
460         /*
461          * set up enough so that we can read an inode
462          */
463         sb->s_op = &adfs_sops;
464
465         dr = adfs_map_discrecord(asb->s_map);
466
467         root_obj.parent_id = root_obj.indaddr = le32_to_cpu(dr->root);
468         root_obj.name_len  = 0;
469         /* Set root object date as 01 Jan 1987 00:00:00 */
470         root_obj.loadaddr  = 0xfff0003f;
471         root_obj.execaddr  = 0xec22c000;
472         root_obj.size      = ADFS_NEWDIR_SIZE;
473         root_obj.attr      = ADFS_NDA_DIRECTORY   | ADFS_NDA_OWNER_READ |
474                              ADFS_NDA_OWNER_WRITE | ADFS_NDA_PUBLIC_READ;
475         root_obj.filetype  = -1;
476
477         /*
478          * If this is a F+ disk with variable length directories,
479          * get the root_size from the disc record.
480          */
481         if (dr->format_version) {
482                 root_obj.size = le32_to_cpu(dr->root_size);
483                 asb->s_dir     = &adfs_fplus_dir_ops;
484                 asb->s_namelen = ADFS_FPLUS_NAME_LEN;
485         } else {
486                 asb->s_dir     = &adfs_f_dir_ops;
487                 asb->s_namelen = ADFS_F_NAME_LEN;
488         }
489         /*
490          * ,xyz hex filetype suffix may be added by driver
491          * to files that have valid RISC OS filetype
492          */
493         if (asb->s_ftsuffix)
494                 asb->s_namelen += 4;
495
496         sb->s_d_op = &adfs_dentry_operations;
497         root = adfs_iget(sb, &root_obj);
498         sb->s_root = d_make_root(root);
499         if (!sb->s_root) {
500                 int i;
501                 for (i = 0; i < asb->s_map_size; i++)
502                         brelse(asb->s_map[i].dm_bh);
503                 kfree(asb->s_map);
504                 adfs_error(sb, "get root inode failed\n");
505                 ret = -EIO;
506                 goto error;
507         }
508         return 0;
509
510 error_badfs:
511         if (!silent)
512                 adfs_msg(sb, KERN_ERR,
513                          "error: can't find an ADFS filesystem on dev %s.",
514                          sb->s_id);
515 error_free_bh:
516         brelse(bh);
517 error:
518         sb->s_fs_info = NULL;
519         kfree(asb);
520         return ret;
521 }
522
523 static struct dentry *adfs_mount(struct file_system_type *fs_type,
524         int flags, const char *dev_name, void *data)
525 {
526         return mount_bdev(fs_type, flags, dev_name, data, adfs_fill_super);
527 }
528
529 static struct file_system_type adfs_fs_type = {
530         .owner          = THIS_MODULE,
531         .name           = "adfs",
532         .mount          = adfs_mount,
533         .kill_sb        = kill_block_super,
534         .fs_flags       = FS_REQUIRES_DEV,
535 };
536 MODULE_ALIAS_FS("adfs");
537
538 static int __init init_adfs_fs(void)
539 {
540         int err = init_inodecache();
541         if (err)
542                 goto out1;
543         err = register_filesystem(&adfs_fs_type);
544         if (err)
545                 goto out;
546         return 0;
547 out:
548         destroy_inodecache();
549 out1:
550         return err;
551 }
552
553 static void __exit exit_adfs_fs(void)
554 {
555         unregister_filesystem(&adfs_fs_type);
556         destroy_inodecache();
557 }
558
559 module_init(init_adfs_fs)
560 module_exit(exit_adfs_fs)
561 MODULE_LICENSE("GPL");