super.c 12 KB

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