super.c 12 KB

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