super.c 12 KB

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