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