super.c 14 KB

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