super.c 14 KB

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