super.c 14 KB

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