super.c 15 KB

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