super.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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, 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. if (wait)
  34. sync_dirty_buffer(bh);
  35. }
  36. static void
  37. affs_put_super(struct super_block *sb)
  38. {
  39. struct affs_sb_info *sbi = AFFS_SB(sb);
  40. pr_debug("AFFS: put_super()\n");
  41. if (!(sb->s_flags & MS_RDONLY) && sb->s_dirt)
  42. affs_commit_super(sb, 1, 1);
  43. kfree(sbi->s_prefix);
  44. affs_free_bitmap(sb);
  45. affs_brelse(sbi->s_root_bh);
  46. kfree(sbi);
  47. sb->s_fs_info = NULL;
  48. }
  49. static void
  50. affs_write_super(struct super_block *sb)
  51. {
  52. lock_super(sb);
  53. if (!(sb->s_flags & MS_RDONLY))
  54. affs_commit_super(sb, 1, 2);
  55. sb->s_dirt = 0;
  56. unlock_super(sb);
  57. pr_debug("AFFS: write_super() at %lu, clean=2\n", get_seconds());
  58. }
  59. static int
  60. affs_sync_fs(struct super_block *sb, int wait)
  61. {
  62. lock_super(sb);
  63. affs_commit_super(sb, wait, 2);
  64. sb->s_dirt = 0;
  65. unlock_super(sb);
  66. return 0;
  67. }
  68. static struct kmem_cache * affs_inode_cachep;
  69. static struct inode *affs_alloc_inode(struct super_block *sb)
  70. {
  71. struct affs_inode_info *i;
  72. i = kmem_cache_alloc(affs_inode_cachep, GFP_KERNEL);
  73. if (!i)
  74. return NULL;
  75. i->vfs_inode.i_version = 1;
  76. i->i_lc = NULL;
  77. i->i_ext_bh = NULL;
  78. i->i_pa_cnt = 0;
  79. return &i->vfs_inode;
  80. }
  81. static void affs_destroy_inode(struct inode *inode)
  82. {
  83. kmem_cache_free(affs_inode_cachep, AFFS_I(inode));
  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. mutex_init(&sbi->s_bmlock);
  267. spin_lock_init(&sbi->symlink_lock);
  268. if (!parse_options(data,&uid,&gid,&i,&reserved,&root_block,
  269. &blocksize,&sbi->s_prefix,
  270. sbi->s_volume, &mount_flags)) {
  271. printk(KERN_ERR "AFFS: Error parsing options\n");
  272. kfree(sbi->s_prefix);
  273. kfree(sbi);
  274. return -EINVAL;
  275. }
  276. /* N.B. after this point s_prefix must be released */
  277. sbi->s_flags = mount_flags;
  278. sbi->s_mode = i;
  279. sbi->s_uid = uid;
  280. sbi->s_gid = gid;
  281. sbi->s_reserved= reserved;
  282. /* Get the size of the device in 512-byte blocks.
  283. * If we later see that the partition uses bigger
  284. * blocks, we will have to change it.
  285. */
  286. size = sb->s_bdev->bd_inode->i_size >> 9;
  287. pr_debug("AFFS: initial blocksize=%d, #blocks=%d\n", 512, size);
  288. affs_set_blocksize(sb, PAGE_SIZE);
  289. /* Try to find root block. Its location depends on the block size. */
  290. i = 512;
  291. j = 4096;
  292. if (blocksize > 0) {
  293. i = j = blocksize;
  294. size = size / (blocksize / 512);
  295. }
  296. for (blocksize = i, key = 0; blocksize <= j; blocksize <<= 1, size >>= 1) {
  297. sbi->s_root_block = root_block;
  298. if (root_block < 0)
  299. sbi->s_root_block = (reserved + size - 1) / 2;
  300. pr_debug("AFFS: setting blocksize to %d\n", blocksize);
  301. affs_set_blocksize(sb, blocksize);
  302. sbi->s_partition_size = size;
  303. /* The root block location that was calculated above is not
  304. * correct if the partition size is an odd number of 512-
  305. * byte blocks, which will be rounded down to a number of
  306. * 1024-byte blocks, and if there were an even number of
  307. * reserved blocks. Ideally, all partition checkers should
  308. * report the real number of blocks of the real blocksize,
  309. * but since this just cannot be done, we have to try to
  310. * find the root block anyways. In the above case, it is one
  311. * block behind the calculated one. So we check this one, too.
  312. */
  313. for (num_bm = 0; num_bm < 2; num_bm++) {
  314. pr_debug("AFFS: Dev %s, trying root=%u, bs=%d, "
  315. "size=%d, reserved=%d\n",
  316. sb->s_id,
  317. sbi->s_root_block + num_bm,
  318. blocksize, size, reserved);
  319. root_bh = affs_bread(sb, sbi->s_root_block + num_bm);
  320. if (!root_bh)
  321. continue;
  322. if (!affs_checksum_block(sb, root_bh) &&
  323. be32_to_cpu(AFFS_ROOT_HEAD(root_bh)->ptype) == T_SHORT &&
  324. be32_to_cpu(AFFS_ROOT_TAIL(sb, root_bh)->stype) == ST_ROOT) {
  325. sbi->s_hashsize = blocksize / 4 - 56;
  326. sbi->s_root_block += num_bm;
  327. key = 1;
  328. goto got_root;
  329. }
  330. affs_brelse(root_bh);
  331. root_bh = NULL;
  332. }
  333. }
  334. if (!silent)
  335. printk(KERN_ERR "AFFS: No valid root block on device %s\n",
  336. sb->s_id);
  337. goto out_error;
  338. /* N.B. after this point bh must be released */
  339. got_root:
  340. root_block = sbi->s_root_block;
  341. /* Find out which kind of FS we have */
  342. boot_bh = sb_bread(sb, 0);
  343. if (!boot_bh) {
  344. printk(KERN_ERR "AFFS: Cannot read boot block\n");
  345. goto out_error;
  346. }
  347. memcpy(sig, boot_bh->b_data, 4);
  348. brelse(boot_bh);
  349. chksum = be32_to_cpu(*(__be32 *)sig);
  350. /* Dircache filesystems are compatible with non-dircache ones
  351. * when reading. As long as they aren't supported, writing is
  352. * not recommended.
  353. */
  354. if ((chksum == FS_DCFFS || chksum == MUFS_DCFFS || chksum == FS_DCOFS
  355. || chksum == MUFS_DCOFS) && !(sb->s_flags & MS_RDONLY)) {
  356. printk(KERN_NOTICE "AFFS: Dircache FS - mounting %s read only\n",
  357. sb->s_id);
  358. sb->s_flags |= MS_RDONLY;
  359. }
  360. switch (chksum) {
  361. case MUFS_FS:
  362. case MUFS_INTLFFS:
  363. case MUFS_DCFFS:
  364. sbi->s_flags |= SF_MUFS;
  365. /* fall thru */
  366. case FS_INTLFFS:
  367. case FS_DCFFS:
  368. sbi->s_flags |= SF_INTL;
  369. break;
  370. case MUFS_FFS:
  371. sbi->s_flags |= SF_MUFS;
  372. break;
  373. case FS_FFS:
  374. break;
  375. case MUFS_OFS:
  376. sbi->s_flags |= SF_MUFS;
  377. /* fall thru */
  378. case FS_OFS:
  379. sbi->s_flags |= SF_OFS;
  380. sb->s_flags |= MS_NOEXEC;
  381. break;
  382. case MUFS_DCOFS:
  383. case MUFS_INTLOFS:
  384. sbi->s_flags |= SF_MUFS;
  385. case FS_DCOFS:
  386. case FS_INTLOFS:
  387. sbi->s_flags |= SF_INTL | SF_OFS;
  388. sb->s_flags |= MS_NOEXEC;
  389. break;
  390. default:
  391. printk(KERN_ERR "AFFS: Unknown filesystem on device %s: %08X\n",
  392. sb->s_id, chksum);
  393. goto out_error;
  394. }
  395. if (mount_flags & SF_VERBOSE) {
  396. u8 len = AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0];
  397. printk(KERN_NOTICE "AFFS: Mounting volume \"%.*s\": Type=%.3s\\%c, Blocksize=%d\n",
  398. len > 31 ? 31 : len,
  399. AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
  400. sig, sig[3] + '0', blocksize);
  401. }
  402. sb->s_flags |= MS_NODEV | MS_NOSUID;
  403. sbi->s_data_blksize = sb->s_blocksize;
  404. if (sbi->s_flags & SF_OFS)
  405. sbi->s_data_blksize -= 24;
  406. /* Keep super block in cache */
  407. sbi->s_root_bh = root_bh;
  408. /* N.B. after this point s_root_bh must be released */
  409. tmp_flags = sb->s_flags;
  410. if (affs_init_bitmap(sb, &tmp_flags))
  411. goto out_error;
  412. sb->s_flags = tmp_flags;
  413. /* set up enough so that it can read an inode */
  414. root_inode = affs_iget(sb, root_block);
  415. if (IS_ERR(root_inode)) {
  416. ret = PTR_ERR(root_inode);
  417. goto out_error_noinode;
  418. }
  419. sb->s_root = d_alloc_root(root_inode);
  420. if (!sb->s_root) {
  421. printk(KERN_ERR "AFFS: Get root inode failed\n");
  422. goto out_error;
  423. }
  424. sb->s_root->d_op = &affs_dentry_operations;
  425. pr_debug("AFFS: s_flags=%lX\n",sb->s_flags);
  426. return 0;
  427. /*
  428. * Begin the cascaded cleanup ...
  429. */
  430. out_error:
  431. if (root_inode)
  432. iput(root_inode);
  433. out_error_noinode:
  434. kfree(sbi->s_bitmap);
  435. affs_brelse(root_bh);
  436. kfree(sbi->s_prefix);
  437. kfree(sbi);
  438. sb->s_fs_info = NULL;
  439. return ret;
  440. }
  441. static int
  442. affs_remount(struct super_block *sb, int *flags, char *data)
  443. {
  444. struct affs_sb_info *sbi = AFFS_SB(sb);
  445. int blocksize;
  446. uid_t uid;
  447. gid_t gid;
  448. int mode;
  449. int reserved;
  450. int root_block;
  451. unsigned long mount_flags;
  452. int res = 0;
  453. char *new_opts = kstrdup(data, GFP_KERNEL);
  454. char volume[32];
  455. char *prefix = NULL;
  456. pr_debug("AFFS: remount(flags=0x%x,opts=\"%s\")\n",*flags,data);
  457. *flags |= MS_NODIRATIME;
  458. memcpy(volume, sbi->s_volume, 32);
  459. if (!parse_options(data, &uid, &gid, &mode, &reserved, &root_block,
  460. &blocksize, &prefix, volume,
  461. &mount_flags)) {
  462. kfree(prefix);
  463. kfree(new_opts);
  464. return -EINVAL;
  465. }
  466. replace_mount_options(sb, new_opts);
  467. sbi->s_flags = mount_flags;
  468. sbi->s_mode = mode;
  469. sbi->s_uid = uid;
  470. sbi->s_gid = gid;
  471. /* protect against readers */
  472. spin_lock(&sbi->symlink_lock);
  473. if (prefix) {
  474. kfree(sbi->s_prefix);
  475. sbi->s_prefix = prefix;
  476. }
  477. memcpy(sbi->s_volume, volume, 32);
  478. spin_unlock(&sbi->symlink_lock);
  479. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  480. return 0;
  481. if (*flags & MS_RDONLY) {
  482. affs_write_super(sb);
  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)