super.c 15 KB

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