super.c 15 KB

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