super.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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(void * foo, struct kmem_cache * cachep, unsigned long flags)
  196. {
  197. struct adfs_inode_info *ei = (struct adfs_inode_info *) foo;
  198. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
  199. SLAB_CTOR_CONSTRUCTOR)
  200. inode_init_once(&ei->vfs_inode);
  201. }
  202. static int init_inodecache(void)
  203. {
  204. adfs_inode_cachep = kmem_cache_create("adfs_inode_cache",
  205. sizeof(struct adfs_inode_info),
  206. 0, (SLAB_RECLAIM_ACCOUNT|
  207. SLAB_MEM_SPREAD),
  208. init_once, NULL);
  209. if (adfs_inode_cachep == NULL)
  210. return -ENOMEM;
  211. return 0;
  212. }
  213. static void destroy_inodecache(void)
  214. {
  215. kmem_cache_destroy(adfs_inode_cachep);
  216. }
  217. static const struct super_operations adfs_sops = {
  218. .alloc_inode = adfs_alloc_inode,
  219. .destroy_inode = adfs_destroy_inode,
  220. .write_inode = adfs_write_inode,
  221. .put_super = adfs_put_super,
  222. .statfs = adfs_statfs,
  223. .remount_fs = adfs_remount,
  224. };
  225. static struct adfs_discmap *adfs_read_map(struct super_block *sb, struct adfs_discrecord *dr)
  226. {
  227. struct adfs_discmap *dm;
  228. unsigned int map_addr, zone_size, nzones;
  229. int i, zone;
  230. struct adfs_sb_info *asb = ADFS_SB(sb);
  231. nzones = asb->s_map_size;
  232. zone_size = (8 << dr->log2secsize) - le16_to_cpu(dr->zone_spare);
  233. map_addr = (nzones >> 1) * zone_size -
  234. ((nzones > 1) ? ADFS_DR_SIZE_BITS : 0);
  235. map_addr = signed_asl(map_addr, asb->s_map2blk);
  236. asb->s_ids_per_zone = zone_size / (asb->s_idlen + 1);
  237. dm = kmalloc(nzones * sizeof(*dm), GFP_KERNEL);
  238. if (dm == NULL) {
  239. adfs_error(sb, "not enough memory");
  240. return NULL;
  241. }
  242. for (zone = 0; zone < nzones; zone++, map_addr++) {
  243. dm[zone].dm_startbit = 0;
  244. dm[zone].dm_endbit = zone_size;
  245. dm[zone].dm_startblk = zone * zone_size - ADFS_DR_SIZE_BITS;
  246. dm[zone].dm_bh = sb_bread(sb, map_addr);
  247. if (!dm[zone].dm_bh) {
  248. adfs_error(sb, "unable to read map");
  249. goto error_free;
  250. }
  251. }
  252. /* adjust the limits for the first and last map zones */
  253. i = zone - 1;
  254. dm[0].dm_startblk = 0;
  255. dm[0].dm_startbit = ADFS_DR_SIZE_BITS;
  256. dm[i].dm_endbit = (le32_to_cpu(dr->disc_size_high) << (32 - dr->log2bpmb)) +
  257. (le32_to_cpu(dr->disc_size) >> dr->log2bpmb) +
  258. (ADFS_DR_SIZE_BITS - i * zone_size);
  259. if (adfs_checkmap(sb, dm))
  260. return dm;
  261. adfs_error(sb, "map corrupted");
  262. error_free:
  263. while (--zone >= 0)
  264. brelse(dm[zone].dm_bh);
  265. kfree(dm);
  266. return NULL;
  267. }
  268. static inline unsigned long adfs_discsize(struct adfs_discrecord *dr, int block_bits)
  269. {
  270. unsigned long discsize;
  271. discsize = le32_to_cpu(dr->disc_size_high) << (32 - block_bits);
  272. discsize |= le32_to_cpu(dr->disc_size) >> block_bits;
  273. return discsize;
  274. }
  275. static int adfs_fill_super(struct super_block *sb, void *data, int silent)
  276. {
  277. struct adfs_discrecord *dr;
  278. struct buffer_head *bh;
  279. struct object_info root_obj;
  280. unsigned char *b_data;
  281. struct adfs_sb_info *asb;
  282. struct inode *root;
  283. sb->s_flags |= MS_NODIRATIME;
  284. asb = kzalloc(sizeof(*asb), GFP_KERNEL);
  285. if (!asb)
  286. return -ENOMEM;
  287. sb->s_fs_info = asb;
  288. /* set default options */
  289. asb->s_uid = 0;
  290. asb->s_gid = 0;
  291. asb->s_owner_mask = S_IRWXU;
  292. asb->s_other_mask = S_IRWXG | S_IRWXO;
  293. if (parse_options(sb, data))
  294. goto error;
  295. sb_set_blocksize(sb, BLOCK_SIZE);
  296. if (!(bh = sb_bread(sb, ADFS_DISCRECORD / BLOCK_SIZE))) {
  297. adfs_error(sb, "unable to read superblock");
  298. goto error;
  299. }
  300. b_data = bh->b_data + (ADFS_DISCRECORD % BLOCK_SIZE);
  301. if (adfs_checkbblk(b_data)) {
  302. if (!silent)
  303. printk("VFS: Can't find an adfs filesystem on dev "
  304. "%s.\n", sb->s_id);
  305. goto error_free_bh;
  306. }
  307. dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET);
  308. /*
  309. * Do some sanity checks on the ADFS disc record
  310. */
  311. if (adfs_checkdiscrecord(dr)) {
  312. if (!silent)
  313. printk("VPS: Can't find an adfs filesystem on dev "
  314. "%s.\n", sb->s_id);
  315. goto error_free_bh;
  316. }
  317. brelse(bh);
  318. if (sb_set_blocksize(sb, 1 << dr->log2secsize)) {
  319. bh = sb_bread(sb, ADFS_DISCRECORD / sb->s_blocksize);
  320. if (!bh) {
  321. adfs_error(sb, "couldn't read superblock on "
  322. "2nd try.");
  323. goto error;
  324. }
  325. b_data = bh->b_data + (ADFS_DISCRECORD % sb->s_blocksize);
  326. if (adfs_checkbblk(b_data)) {
  327. adfs_error(sb, "disc record mismatch, very weird!");
  328. goto error_free_bh;
  329. }
  330. dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET);
  331. } else {
  332. if (!silent)
  333. printk(KERN_ERR "VFS: Unsupported blocksize on dev "
  334. "%s.\n", sb->s_id);
  335. goto error;
  336. }
  337. /*
  338. * blocksize on this device should now be set to the ADFS log2secsize
  339. */
  340. sb->s_magic = ADFS_SUPER_MAGIC;
  341. asb->s_idlen = dr->idlen;
  342. asb->s_map_size = dr->nzones | (dr->nzones_high << 8);
  343. asb->s_map2blk = dr->log2bpmb - dr->log2secsize;
  344. asb->s_size = adfs_discsize(dr, sb->s_blocksize_bits);
  345. asb->s_version = dr->format_version;
  346. asb->s_log2sharesize = dr->log2sharesize;
  347. asb->s_map = adfs_read_map(sb, dr);
  348. if (!asb->s_map)
  349. goto error_free_bh;
  350. brelse(bh);
  351. /*
  352. * set up enough so that we can read an inode
  353. */
  354. sb->s_op = &adfs_sops;
  355. dr = (struct adfs_discrecord *)(asb->s_map[0].dm_bh->b_data + 4);
  356. root_obj.parent_id = root_obj.file_id = le32_to_cpu(dr->root);
  357. root_obj.name_len = 0;
  358. root_obj.loadaddr = 0;
  359. root_obj.execaddr = 0;
  360. root_obj.size = ADFS_NEWDIR_SIZE;
  361. root_obj.attr = ADFS_NDA_DIRECTORY | ADFS_NDA_OWNER_READ |
  362. ADFS_NDA_OWNER_WRITE | ADFS_NDA_PUBLIC_READ;
  363. /*
  364. * If this is a F+ disk with variable length directories,
  365. * get the root_size from the disc record.
  366. */
  367. if (asb->s_version) {
  368. root_obj.size = le32_to_cpu(dr->root_size);
  369. asb->s_dir = &adfs_fplus_dir_ops;
  370. asb->s_namelen = ADFS_FPLUS_NAME_LEN;
  371. } else {
  372. asb->s_dir = &adfs_f_dir_ops;
  373. asb->s_namelen = ADFS_F_NAME_LEN;
  374. }
  375. root = adfs_iget(sb, &root_obj);
  376. sb->s_root = d_alloc_root(root);
  377. if (!sb->s_root) {
  378. int i;
  379. iput(root);
  380. for (i = 0; i < asb->s_map_size; i++)
  381. brelse(asb->s_map[i].dm_bh);
  382. kfree(asb->s_map);
  383. adfs_error(sb, "get root inode failed\n");
  384. goto error;
  385. } else
  386. sb->s_root->d_op = &adfs_dentry_operations;
  387. return 0;
  388. error_free_bh:
  389. brelse(bh);
  390. error:
  391. sb->s_fs_info = NULL;
  392. kfree(asb);
  393. return -EINVAL;
  394. }
  395. static int adfs_get_sb(struct file_system_type *fs_type,
  396. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  397. {
  398. return get_sb_bdev(fs_type, flags, dev_name, data, adfs_fill_super,
  399. mnt);
  400. }
  401. static struct file_system_type adfs_fs_type = {
  402. .owner = THIS_MODULE,
  403. .name = "adfs",
  404. .get_sb = adfs_get_sb,
  405. .kill_sb = kill_block_super,
  406. .fs_flags = FS_REQUIRES_DEV,
  407. };
  408. static int __init init_adfs_fs(void)
  409. {
  410. int err = init_inodecache();
  411. if (err)
  412. goto out1;
  413. err = register_filesystem(&adfs_fs_type);
  414. if (err)
  415. goto out;
  416. return 0;
  417. out:
  418. destroy_inodecache();
  419. out1:
  420. return err;
  421. }
  422. static void __exit exit_adfs_fs(void)
  423. {
  424. unregister_filesystem(&adfs_fs_type);
  425. destroy_inodecache();
  426. }
  427. module_init(init_adfs_fs)
  428. module_exit(exit_adfs_fs)