super.c 12 KB

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