super.c 13 KB

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