super.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. * linux/fs/affs/inode.c
  3. *
  4. * (c) 1996 Hans-Joachim Widmaier - Rewritten
  5. *
  6. * (C) 1993 Ray Burr - Modified for Amiga FFS filesystem.
  7. *
  8. * (C) 1992 Eric Youngdale Modified for ISO 9660 filesystem.
  9. *
  10. * (C) 1991 Linus Torvalds - minix filesystem
  11. */
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/statfs.h>
  15. #include <linux/parser.h>
  16. #include <linux/magic.h>
  17. #include <linux/sched.h>
  18. #include "affs.h"
  19. extern struct timezone sys_tz;
  20. static int affs_statfs(struct dentry *dentry, struct kstatfs *buf);
  21. static int affs_remount (struct super_block *sb, int *flags, char *data);
  22. static void
  23. affs_put_super(struct super_block *sb)
  24. {
  25. struct affs_sb_info *sbi = AFFS_SB(sb);
  26. pr_debug("AFFS: put_super()\n");
  27. if (!(sb->s_flags & MS_RDONLY)) {
  28. AFFS_ROOT_TAIL(sb, sbi->s_root_bh)->bm_flag = cpu_to_be32(1);
  29. secs_to_datestamp(get_seconds(),
  30. &AFFS_ROOT_TAIL(sb, sbi->s_root_bh)->disk_change);
  31. affs_fix_checksum(sb, sbi->s_root_bh);
  32. mark_buffer_dirty(sbi->s_root_bh);
  33. }
  34. kfree(sbi->s_prefix);
  35. affs_free_bitmap(sb);
  36. affs_brelse(sbi->s_root_bh);
  37. kfree(sbi);
  38. sb->s_fs_info = NULL;
  39. return;
  40. }
  41. static void
  42. affs_write_super(struct super_block *sb)
  43. {
  44. int clean = 2;
  45. struct affs_sb_info *sbi = AFFS_SB(sb);
  46. if (!(sb->s_flags & MS_RDONLY)) {
  47. // if (sbi->s_bitmap[i].bm_bh) {
  48. // if (buffer_dirty(sbi->s_bitmap[i].bm_bh)) {
  49. // clean = 0;
  50. AFFS_ROOT_TAIL(sb, sbi->s_root_bh)->bm_flag = cpu_to_be32(clean);
  51. secs_to_datestamp(get_seconds(),
  52. &AFFS_ROOT_TAIL(sb, sbi->s_root_bh)->disk_change);
  53. affs_fix_checksum(sb, sbi->s_root_bh);
  54. mark_buffer_dirty(sbi->s_root_bh);
  55. sb->s_dirt = !clean; /* redo until bitmap synced */
  56. } else
  57. sb->s_dirt = 0;
  58. pr_debug("AFFS: write_super() at %lu, clean=%d\n", get_seconds(), clean);
  59. }
  60. static struct kmem_cache * affs_inode_cachep;
  61. static struct inode *affs_alloc_inode(struct super_block *sb)
  62. {
  63. struct affs_inode_info *ei;
  64. ei = (struct affs_inode_info *)kmem_cache_alloc(affs_inode_cachep, GFP_KERNEL);
  65. if (!ei)
  66. return NULL;
  67. ei->vfs_inode.i_version = 1;
  68. return &ei->vfs_inode;
  69. }
  70. static void affs_destroy_inode(struct inode *inode)
  71. {
  72. kmem_cache_free(affs_inode_cachep, AFFS_I(inode));
  73. }
  74. static void init_once(struct kmem_cache *cachep, void *foo)
  75. {
  76. struct affs_inode_info *ei = (struct affs_inode_info *) foo;
  77. init_MUTEX(&ei->i_link_lock);
  78. init_MUTEX(&ei->i_ext_lock);
  79. inode_init_once(&ei->vfs_inode);
  80. }
  81. static int init_inodecache(void)
  82. {
  83. affs_inode_cachep = kmem_cache_create("affs_inode_cache",
  84. sizeof(struct affs_inode_info),
  85. 0, (SLAB_RECLAIM_ACCOUNT|
  86. SLAB_MEM_SPREAD),
  87. init_once);
  88. if (affs_inode_cachep == NULL)
  89. return -ENOMEM;
  90. return 0;
  91. }
  92. static void destroy_inodecache(void)
  93. {
  94. kmem_cache_destroy(affs_inode_cachep);
  95. }
  96. static const struct super_operations affs_sops = {
  97. .alloc_inode = affs_alloc_inode,
  98. .destroy_inode = affs_destroy_inode,
  99. .write_inode = affs_write_inode,
  100. .put_inode = affs_put_inode,
  101. .drop_inode = affs_drop_inode,
  102. .delete_inode = affs_delete_inode,
  103. .clear_inode = affs_clear_inode,
  104. .put_super = affs_put_super,
  105. .write_super = affs_write_super,
  106. .statfs = affs_statfs,
  107. .remount_fs = affs_remount,
  108. .show_options = generic_show_options,
  109. };
  110. enum {
  111. Opt_bs, Opt_mode, Opt_mufs, Opt_prefix, Opt_protect,
  112. Opt_reserved, Opt_root, Opt_setgid, Opt_setuid,
  113. Opt_verbose, Opt_volume, Opt_ignore, Opt_err,
  114. };
  115. static match_table_t tokens = {
  116. {Opt_bs, "bs=%u"},
  117. {Opt_mode, "mode=%o"},
  118. {Opt_mufs, "mufs"},
  119. {Opt_prefix, "prefix=%s"},
  120. {Opt_protect, "protect"},
  121. {Opt_reserved, "reserved=%u"},
  122. {Opt_root, "root=%u"},
  123. {Opt_setgid, "setgid=%u"},
  124. {Opt_setuid, "setuid=%u"},
  125. {Opt_verbose, "verbose"},
  126. {Opt_volume, "volume=%s"},
  127. {Opt_ignore, "grpquota"},
  128. {Opt_ignore, "noquota"},
  129. {Opt_ignore, "quota"},
  130. {Opt_ignore, "usrquota"},
  131. {Opt_err, NULL},
  132. };
  133. static int
  134. parse_options(char *options, uid_t *uid, gid_t *gid, int *mode, int *reserved, s32 *root,
  135. int *blocksize, char **prefix, char *volume, unsigned long *mount_opts)
  136. {
  137. char *p;
  138. substring_t args[MAX_OPT_ARGS];
  139. /* Fill in defaults */
  140. *uid = current->uid;
  141. *gid = current->gid;
  142. *reserved = 2;
  143. *root = -1;
  144. *blocksize = -1;
  145. volume[0] = ':';
  146. volume[1] = 0;
  147. *mount_opts = 0;
  148. if (!options)
  149. return 1;
  150. while ((p = strsep(&options, ",")) != NULL) {
  151. int token, n, option;
  152. if (!*p)
  153. continue;
  154. token = match_token(p, tokens, args);
  155. switch (token) {
  156. case Opt_bs:
  157. if (match_int(&args[0], &n))
  158. return -EINVAL;
  159. if (n != 512 && n != 1024 && n != 2048
  160. && n != 4096) {
  161. printk ("AFFS: Invalid blocksize (512, 1024, 2048, 4096 allowed)\n");
  162. return 0;
  163. }
  164. *blocksize = n;
  165. break;
  166. case Opt_mode:
  167. if (match_octal(&args[0], &option))
  168. return 1;
  169. *mode = option & 0777;
  170. *mount_opts |= SF_SETMODE;
  171. break;
  172. case Opt_mufs:
  173. *mount_opts |= SF_MUFS;
  174. break;
  175. case Opt_prefix:
  176. /* Free any previous prefix */
  177. kfree(*prefix);
  178. *prefix = NULL;
  179. *prefix = match_strdup(&args[0]);
  180. if (!*prefix)
  181. return 0;
  182. *mount_opts |= SF_PREFIX;
  183. break;
  184. case Opt_protect:
  185. *mount_opts |= SF_IMMUTABLE;
  186. break;
  187. case Opt_reserved:
  188. if (match_int(&args[0], reserved))
  189. return 1;
  190. break;
  191. case Opt_root:
  192. if (match_int(&args[0], root))
  193. return 1;
  194. break;
  195. case Opt_setgid:
  196. if (match_int(&args[0], &option))
  197. return 1;
  198. *gid = option;
  199. *mount_opts |= SF_SETGID;
  200. break;
  201. case Opt_setuid:
  202. if (match_int(&args[0], &option))
  203. return -EINVAL;
  204. *uid = option;
  205. *mount_opts |= SF_SETUID;
  206. break;
  207. case Opt_verbose:
  208. *mount_opts |= SF_VERBOSE;
  209. break;
  210. case Opt_volume: {
  211. char *vol = match_strdup(&args[0]);
  212. strlcpy(volume, vol, 32);
  213. kfree(vol);
  214. break;
  215. }
  216. case Opt_ignore:
  217. /* Silently ignore the quota options */
  218. break;
  219. default:
  220. printk("AFFS: Unrecognized mount option \"%s\" "
  221. "or missing value\n", p);
  222. return 0;
  223. }
  224. }
  225. return 1;
  226. }
  227. /* This function definitely needs to be split up. Some fine day I'll
  228. * hopefully have the guts to do so. Until then: sorry for the mess.
  229. */
  230. static int affs_fill_super(struct super_block *sb, void *data, int silent)
  231. {
  232. struct affs_sb_info *sbi;
  233. struct buffer_head *root_bh = NULL;
  234. struct buffer_head *boot_bh;
  235. struct inode *root_inode = NULL;
  236. s32 root_block;
  237. int size, blocksize;
  238. u32 chksum;
  239. int num_bm;
  240. int i, j;
  241. s32 key;
  242. uid_t uid;
  243. gid_t gid;
  244. int reserved;
  245. unsigned long mount_flags;
  246. int tmp_flags; /* fix remount prototype... */
  247. u8 sig[4];
  248. int ret = -EINVAL;
  249. save_mount_options(sb, data);
  250. pr_debug("AFFS: read_super(%s)\n",data ? (const char *)data : "no options");
  251. sb->s_magic = AFFS_SUPER_MAGIC;
  252. sb->s_op = &affs_sops;
  253. sb->s_flags |= MS_NODIRATIME;
  254. sbi = kzalloc(sizeof(struct affs_sb_info), GFP_KERNEL);
  255. if (!sbi)
  256. return -ENOMEM;
  257. sb->s_fs_info = sbi;
  258. init_MUTEX(&sbi->s_bmlock);
  259. if (!parse_options(data,&uid,&gid,&i,&reserved,&root_block,
  260. &blocksize,&sbi->s_prefix,
  261. sbi->s_volume, &mount_flags)) {
  262. printk(KERN_ERR "AFFS: Error parsing options\n");
  263. return -EINVAL;
  264. }
  265. /* N.B. after this point s_prefix must be released */
  266. sbi->s_flags = mount_flags;
  267. sbi->s_mode = i;
  268. sbi->s_uid = uid;
  269. sbi->s_gid = gid;
  270. sbi->s_reserved= reserved;
  271. /* Get the size of the device in 512-byte blocks.
  272. * If we later see that the partition uses bigger
  273. * blocks, we will have to change it.
  274. */
  275. size = sb->s_bdev->bd_inode->i_size >> 9;
  276. pr_debug("AFFS: initial blocksize=%d, #blocks=%d\n", 512, size);
  277. affs_set_blocksize(sb, PAGE_SIZE);
  278. /* Try to find root block. Its location depends on the block size. */
  279. i = 512;
  280. j = 4096;
  281. if (blocksize > 0) {
  282. i = j = blocksize;
  283. size = size / (blocksize / 512);
  284. }
  285. for (blocksize = i, key = 0; blocksize <= j; blocksize <<= 1, size >>= 1) {
  286. sbi->s_root_block = root_block;
  287. if (root_block < 0)
  288. sbi->s_root_block = (reserved + size - 1) / 2;
  289. pr_debug("AFFS: setting blocksize to %d\n", blocksize);
  290. affs_set_blocksize(sb, blocksize);
  291. sbi->s_partition_size = size;
  292. /* The root block location that was calculated above is not
  293. * correct if the partition size is an odd number of 512-
  294. * byte blocks, which will be rounded down to a number of
  295. * 1024-byte blocks, and if there were an even number of
  296. * reserved blocks. Ideally, all partition checkers should
  297. * report the real number of blocks of the real blocksize,
  298. * but since this just cannot be done, we have to try to
  299. * find the root block anyways. In the above case, it is one
  300. * block behind the calculated one. So we check this one, too.
  301. */
  302. for (num_bm = 0; num_bm < 2; num_bm++) {
  303. pr_debug("AFFS: Dev %s, trying root=%u, bs=%d, "
  304. "size=%d, reserved=%d\n",
  305. sb->s_id,
  306. sbi->s_root_block + num_bm,
  307. blocksize, size, reserved);
  308. root_bh = affs_bread(sb, sbi->s_root_block + num_bm);
  309. if (!root_bh)
  310. continue;
  311. if (!affs_checksum_block(sb, root_bh) &&
  312. be32_to_cpu(AFFS_ROOT_HEAD(root_bh)->ptype) == T_SHORT &&
  313. be32_to_cpu(AFFS_ROOT_TAIL(sb, root_bh)->stype) == ST_ROOT) {
  314. sbi->s_hashsize = blocksize / 4 - 56;
  315. sbi->s_root_block += num_bm;
  316. key = 1;
  317. goto got_root;
  318. }
  319. affs_brelse(root_bh);
  320. root_bh = NULL;
  321. }
  322. }
  323. if (!silent)
  324. printk(KERN_ERR "AFFS: No valid root block on device %s\n",
  325. sb->s_id);
  326. goto out_error;
  327. /* N.B. after this point bh must be released */
  328. got_root:
  329. root_block = sbi->s_root_block;
  330. /* Find out which kind of FS we have */
  331. boot_bh = sb_bread(sb, 0);
  332. if (!boot_bh) {
  333. printk(KERN_ERR "AFFS: Cannot read boot block\n");
  334. goto out_error;
  335. }
  336. memcpy(sig, boot_bh->b_data, 4);
  337. brelse(boot_bh);
  338. chksum = be32_to_cpu(*(__be32 *)sig);
  339. /* Dircache filesystems are compatible with non-dircache ones
  340. * when reading. As long as they aren't supported, writing is
  341. * not recommended.
  342. */
  343. if ((chksum == FS_DCFFS || chksum == MUFS_DCFFS || chksum == FS_DCOFS
  344. || chksum == MUFS_DCOFS) && !(sb->s_flags & MS_RDONLY)) {
  345. printk(KERN_NOTICE "AFFS: Dircache FS - mounting %s read only\n",
  346. sb->s_id);
  347. sb->s_flags |= MS_RDONLY;
  348. }
  349. switch (chksum) {
  350. case MUFS_FS:
  351. case MUFS_INTLFFS:
  352. case MUFS_DCFFS:
  353. sbi->s_flags |= SF_MUFS;
  354. /* fall thru */
  355. case FS_INTLFFS:
  356. case FS_DCFFS:
  357. sbi->s_flags |= SF_INTL;
  358. break;
  359. case MUFS_FFS:
  360. sbi->s_flags |= SF_MUFS;
  361. break;
  362. case FS_FFS:
  363. break;
  364. case MUFS_OFS:
  365. sbi->s_flags |= SF_MUFS;
  366. /* fall thru */
  367. case FS_OFS:
  368. sbi->s_flags |= SF_OFS;
  369. sb->s_flags |= MS_NOEXEC;
  370. break;
  371. case MUFS_DCOFS:
  372. case MUFS_INTLOFS:
  373. sbi->s_flags |= SF_MUFS;
  374. case FS_DCOFS:
  375. case FS_INTLOFS:
  376. sbi->s_flags |= SF_INTL | SF_OFS;
  377. sb->s_flags |= MS_NOEXEC;
  378. break;
  379. default:
  380. printk(KERN_ERR "AFFS: Unknown filesystem on device %s: %08X\n",
  381. sb->s_id, chksum);
  382. goto out_error;
  383. }
  384. if (mount_flags & SF_VERBOSE) {
  385. u8 len = AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0];
  386. printk(KERN_NOTICE "AFFS: Mounting volume \"%.*s\": Type=%.3s\\%c, Blocksize=%d\n",
  387. len > 31 ? 31 : len,
  388. AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
  389. sig, sig[3] + '0', blocksize);
  390. }
  391. sb->s_flags |= MS_NODEV | MS_NOSUID;
  392. sbi->s_data_blksize = sb->s_blocksize;
  393. if (sbi->s_flags & SF_OFS)
  394. sbi->s_data_blksize -= 24;
  395. /* Keep super block in cache */
  396. sbi->s_root_bh = root_bh;
  397. /* N.B. after this point s_root_bh must be released */
  398. tmp_flags = sb->s_flags;
  399. if (affs_init_bitmap(sb, &tmp_flags))
  400. goto out_error;
  401. sb->s_flags = tmp_flags;
  402. /* set up enough so that it can read an inode */
  403. root_inode = affs_iget(sb, root_block);
  404. if (IS_ERR(root_inode)) {
  405. ret = PTR_ERR(root_inode);
  406. goto out_error_noinode;
  407. }
  408. sb->s_root = d_alloc_root(root_inode);
  409. if (!sb->s_root) {
  410. printk(KERN_ERR "AFFS: Get root inode failed\n");
  411. goto out_error;
  412. }
  413. sb->s_root->d_op = &affs_dentry_operations;
  414. pr_debug("AFFS: s_flags=%lX\n",sb->s_flags);
  415. return 0;
  416. /*
  417. * Begin the cascaded cleanup ...
  418. */
  419. out_error:
  420. if (root_inode)
  421. iput(root_inode);
  422. out_error_noinode:
  423. kfree(sbi->s_bitmap);
  424. affs_brelse(root_bh);
  425. kfree(sbi->s_prefix);
  426. kfree(sbi);
  427. sb->s_fs_info = NULL;
  428. return ret;
  429. }
  430. static int
  431. affs_remount(struct super_block *sb, int *flags, char *data)
  432. {
  433. struct affs_sb_info *sbi = AFFS_SB(sb);
  434. int blocksize;
  435. uid_t uid;
  436. gid_t gid;
  437. int mode;
  438. int reserved;
  439. int root_block;
  440. unsigned long mount_flags;
  441. int res = 0;
  442. char *new_opts = kstrdup(data, GFP_KERNEL);
  443. pr_debug("AFFS: remount(flags=0x%x,opts=\"%s\")\n",*flags,data);
  444. *flags |= MS_NODIRATIME;
  445. if (!parse_options(data, &uid, &gid, &mode, &reserved, &root_block,
  446. &blocksize, &sbi->s_prefix, sbi->s_volume,
  447. &mount_flags)) {
  448. kfree(new_opts);
  449. return -EINVAL;
  450. }
  451. kfree(sb->s_options);
  452. sb->s_options = new_opts;
  453. sbi->s_flags = mount_flags;
  454. sbi->s_mode = mode;
  455. sbi->s_uid = uid;
  456. sbi->s_gid = gid;
  457. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  458. return 0;
  459. if (*flags & MS_RDONLY) {
  460. sb->s_dirt = 1;
  461. while (sb->s_dirt)
  462. affs_write_super(sb);
  463. affs_free_bitmap(sb);
  464. } else
  465. res = affs_init_bitmap(sb, flags);
  466. return res;
  467. }
  468. static int
  469. affs_statfs(struct dentry *dentry, struct kstatfs *buf)
  470. {
  471. struct super_block *sb = dentry->d_sb;
  472. int free;
  473. pr_debug("AFFS: statfs() partsize=%d, reserved=%d\n",AFFS_SB(sb)->s_partition_size,
  474. AFFS_SB(sb)->s_reserved);
  475. free = affs_count_free_blocks(sb);
  476. buf->f_type = AFFS_SUPER_MAGIC;
  477. buf->f_bsize = sb->s_blocksize;
  478. buf->f_blocks = AFFS_SB(sb)->s_partition_size - AFFS_SB(sb)->s_reserved;
  479. buf->f_bfree = free;
  480. buf->f_bavail = free;
  481. return 0;
  482. }
  483. static int affs_get_sb(struct file_system_type *fs_type,
  484. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  485. {
  486. return get_sb_bdev(fs_type, flags, dev_name, data, affs_fill_super,
  487. mnt);
  488. }
  489. static struct file_system_type affs_fs_type = {
  490. .owner = THIS_MODULE,
  491. .name = "affs",
  492. .get_sb = affs_get_sb,
  493. .kill_sb = kill_block_super,
  494. .fs_flags = FS_REQUIRES_DEV,
  495. };
  496. static int __init init_affs_fs(void)
  497. {
  498. int err = init_inodecache();
  499. if (err)
  500. goto out1;
  501. err = register_filesystem(&affs_fs_type);
  502. if (err)
  503. goto out;
  504. return 0;
  505. out:
  506. destroy_inodecache();
  507. out1:
  508. return err;
  509. }
  510. static void __exit exit_affs_fs(void)
  511. {
  512. unregister_filesystem(&affs_fs_type);
  513. destroy_inodecache();
  514. }
  515. MODULE_DESCRIPTION("Amiga filesystem support for Linux");
  516. MODULE_LICENSE("GPL");
  517. module_init(init_affs_fs)
  518. module_exit(exit_affs_fs)