super.c 13 KB

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