super.c 15 KB

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