super.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. kmem_cache_destroy(affs_inode_cachep);
  124. }
  125. static const struct super_operations affs_sops = {
  126. .alloc_inode = affs_alloc_inode,
  127. .destroy_inode = affs_destroy_inode,
  128. .write_inode = affs_write_inode,
  129. .evict_inode = affs_evict_inode,
  130. .put_super = affs_put_super,
  131. .sync_fs = affs_sync_fs,
  132. .statfs = affs_statfs,
  133. .remount_fs = affs_remount,
  134. .show_options = generic_show_options,
  135. };
  136. enum {
  137. Opt_bs, Opt_mode, Opt_mufs, Opt_prefix, Opt_protect,
  138. Opt_reserved, Opt_root, Opt_setgid, Opt_setuid,
  139. Opt_verbose, Opt_volume, Opt_ignore, Opt_err,
  140. };
  141. static const match_table_t tokens = {
  142. {Opt_bs, "bs=%u"},
  143. {Opt_mode, "mode=%o"},
  144. {Opt_mufs, "mufs"},
  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. printk ("AFFS: 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_prefix:
  202. *prefix = match_strdup(&args[0]);
  203. if (!*prefix)
  204. return 0;
  205. *mount_opts |= SF_PREFIX;
  206. break;
  207. case Opt_protect:
  208. *mount_opts |= SF_IMMUTABLE;
  209. break;
  210. case Opt_reserved:
  211. if (match_int(&args[0], reserved))
  212. return 0;
  213. break;
  214. case Opt_root:
  215. if (match_int(&args[0], root))
  216. return 0;
  217. break;
  218. case Opt_setgid:
  219. if (match_int(&args[0], &option))
  220. return 0;
  221. *gid = make_kgid(current_user_ns(), option);
  222. if (!gid_valid(*gid))
  223. return 0;
  224. *mount_opts |= SF_SETGID;
  225. break;
  226. case Opt_setuid:
  227. if (match_int(&args[0], &option))
  228. return 0;
  229. *uid = make_kuid(current_user_ns(), option);
  230. if (!uid_valid(*uid))
  231. return 0;
  232. *mount_opts |= SF_SETUID;
  233. break;
  234. case Opt_verbose:
  235. *mount_opts |= SF_VERBOSE;
  236. break;
  237. case Opt_volume: {
  238. char *vol = match_strdup(&args[0]);
  239. if (!vol)
  240. return 0;
  241. strlcpy(volume, vol, 32);
  242. kfree(vol);
  243. break;
  244. }
  245. case Opt_ignore:
  246. /* Silently ignore the quota options */
  247. break;
  248. default:
  249. printk("AFFS: Unrecognized mount option \"%s\" "
  250. "or missing value\n", p);
  251. return 0;
  252. }
  253. }
  254. return 1;
  255. }
  256. /* This function definitely needs to be split up. Some fine day I'll
  257. * hopefully have the guts to do so. Until then: sorry for the mess.
  258. */
  259. static int affs_fill_super(struct super_block *sb, void *data, int silent)
  260. {
  261. struct affs_sb_info *sbi;
  262. struct buffer_head *root_bh = NULL;
  263. struct buffer_head *boot_bh;
  264. struct inode *root_inode = NULL;
  265. s32 root_block;
  266. int size, blocksize;
  267. u32 chksum;
  268. int num_bm;
  269. int i, j;
  270. s32 key;
  271. kuid_t uid;
  272. kgid_t gid;
  273. int reserved;
  274. unsigned long mount_flags;
  275. int tmp_flags; /* fix remount prototype... */
  276. u8 sig[4];
  277. int ret = -EINVAL;
  278. save_mount_options(sb, data);
  279. pr_debug("AFFS: read_super(%s)\n",data ? (const char *)data : "no options");
  280. sb->s_magic = AFFS_SUPER_MAGIC;
  281. sb->s_op = &affs_sops;
  282. sb->s_flags |= MS_NODIRATIME;
  283. sbi = kzalloc(sizeof(struct affs_sb_info), GFP_KERNEL);
  284. if (!sbi)
  285. return -ENOMEM;
  286. sb->s_fs_info = sbi;
  287. sbi->sb = sb;
  288. mutex_init(&sbi->s_bmlock);
  289. spin_lock_init(&sbi->symlink_lock);
  290. spin_lock_init(&sbi->work_lock);
  291. INIT_DELAYED_WORK(&sbi->sb_work, flush_superblock);
  292. if (!parse_options(data,&uid,&gid,&i,&reserved,&root_block,
  293. &blocksize,&sbi->s_prefix,
  294. sbi->s_volume, &mount_flags)) {
  295. printk(KERN_ERR "AFFS: Error parsing options\n");
  296. kfree(sbi->s_prefix);
  297. kfree(sbi);
  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("AFFS: 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, key = 0; 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("AFFS: 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("AFFS: 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. key = 1;
  352. goto got_root;
  353. }
  354. affs_brelse(root_bh);
  355. root_bh = NULL;
  356. }
  357. }
  358. if (!silent)
  359. printk(KERN_ERR "AFFS: No valid root block on device %s\n",
  360. sb->s_id);
  361. goto out_error;
  362. /* N.B. after this point bh must be released */
  363. got_root:
  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. printk(KERN_ERR "AFFS: Cannot read boot block\n");
  369. goto out_error;
  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. printk(KERN_NOTICE "AFFS: Dircache FS - mounting %s read only\n",
  381. sb->s_id);
  382. sb->s_flags |= MS_RDONLY;
  383. }
  384. switch (chksum) {
  385. case MUFS_FS:
  386. case MUFS_INTLFFS:
  387. case MUFS_DCFFS:
  388. sbi->s_flags |= SF_MUFS;
  389. /* fall thru */
  390. case FS_INTLFFS:
  391. case FS_DCFFS:
  392. sbi->s_flags |= SF_INTL;
  393. break;
  394. case MUFS_FFS:
  395. sbi->s_flags |= SF_MUFS;
  396. break;
  397. case FS_FFS:
  398. break;
  399. case MUFS_OFS:
  400. sbi->s_flags |= SF_MUFS;
  401. /* fall thru */
  402. case FS_OFS:
  403. sbi->s_flags |= SF_OFS;
  404. sb->s_flags |= MS_NOEXEC;
  405. break;
  406. case MUFS_DCOFS:
  407. case MUFS_INTLOFS:
  408. sbi->s_flags |= SF_MUFS;
  409. case FS_DCOFS:
  410. case FS_INTLOFS:
  411. sbi->s_flags |= SF_INTL | SF_OFS;
  412. sb->s_flags |= MS_NOEXEC;
  413. break;
  414. default:
  415. printk(KERN_ERR "AFFS: Unknown filesystem on device %s: %08X\n",
  416. sb->s_id, chksum);
  417. goto out_error;
  418. }
  419. if (mount_flags & SF_VERBOSE) {
  420. u8 len = AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0];
  421. printk(KERN_NOTICE "AFFS: Mounting volume \"%.*s\": Type=%.3s\\%c, Blocksize=%d\n",
  422. len > 31 ? 31 : len,
  423. AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
  424. sig, sig[3] + '0', blocksize);
  425. }
  426. sb->s_flags |= MS_NODEV | MS_NOSUID;
  427. sbi->s_data_blksize = sb->s_blocksize;
  428. if (sbi->s_flags & SF_OFS)
  429. sbi->s_data_blksize -= 24;
  430. /* Keep super block in cache */
  431. sbi->s_root_bh = root_bh;
  432. /* N.B. after this point s_root_bh must be released */
  433. tmp_flags = sb->s_flags;
  434. if (affs_init_bitmap(sb, &tmp_flags))
  435. goto out_error;
  436. sb->s_flags = tmp_flags;
  437. /* set up enough so that it can read an inode */
  438. root_inode = affs_iget(sb, root_block);
  439. if (IS_ERR(root_inode)) {
  440. ret = PTR_ERR(root_inode);
  441. goto out_error;
  442. }
  443. if (AFFS_SB(sb)->s_flags & SF_INTL)
  444. sb->s_d_op = &affs_intl_dentry_operations;
  445. else
  446. sb->s_d_op = &affs_dentry_operations;
  447. sb->s_root = d_make_root(root_inode);
  448. if (!sb->s_root) {
  449. printk(KERN_ERR "AFFS: Get root inode failed\n");
  450. goto out_error;
  451. }
  452. pr_debug("AFFS: s_flags=%lX\n",sb->s_flags);
  453. return 0;
  454. /*
  455. * Begin the cascaded cleanup ...
  456. */
  457. out_error:
  458. kfree(sbi->s_bitmap);
  459. affs_brelse(root_bh);
  460. kfree(sbi->s_prefix);
  461. kfree(sbi);
  462. sb->s_fs_info = NULL;
  463. return ret;
  464. }
  465. static int
  466. affs_remount(struct super_block *sb, int *flags, char *data)
  467. {
  468. struct affs_sb_info *sbi = AFFS_SB(sb);
  469. int blocksize;
  470. kuid_t uid;
  471. kgid_t gid;
  472. int mode;
  473. int reserved;
  474. int root_block;
  475. unsigned long mount_flags;
  476. int res = 0;
  477. char *new_opts = kstrdup(data, GFP_KERNEL);
  478. char volume[32];
  479. char *prefix = NULL;
  480. pr_debug("AFFS: remount(flags=0x%x,opts=\"%s\")\n",*flags,data);
  481. *flags |= MS_NODIRATIME;
  482. memcpy(volume, sbi->s_volume, 32);
  483. if (!parse_options(data, &uid, &gid, &mode, &reserved, &root_block,
  484. &blocksize, &prefix, volume,
  485. &mount_flags)) {
  486. kfree(prefix);
  487. kfree(new_opts);
  488. return -EINVAL;
  489. }
  490. flush_delayed_work(&sbi->sb_work);
  491. replace_mount_options(sb, new_opts);
  492. sbi->s_flags = mount_flags;
  493. sbi->s_mode = mode;
  494. sbi->s_uid = uid;
  495. sbi->s_gid = gid;
  496. /* protect against readers */
  497. spin_lock(&sbi->symlink_lock);
  498. if (prefix) {
  499. kfree(sbi->s_prefix);
  500. sbi->s_prefix = prefix;
  501. }
  502. memcpy(sbi->s_volume, volume, 32);
  503. spin_unlock(&sbi->symlink_lock);
  504. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  505. return 0;
  506. if (*flags & MS_RDONLY)
  507. affs_free_bitmap(sb);
  508. else
  509. res = affs_init_bitmap(sb, flags);
  510. return res;
  511. }
  512. static int
  513. affs_statfs(struct dentry *dentry, struct kstatfs *buf)
  514. {
  515. struct super_block *sb = dentry->d_sb;
  516. int free;
  517. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  518. pr_debug("AFFS: statfs() partsize=%d, reserved=%d\n",AFFS_SB(sb)->s_partition_size,
  519. AFFS_SB(sb)->s_reserved);
  520. free = affs_count_free_blocks(sb);
  521. buf->f_type = AFFS_SUPER_MAGIC;
  522. buf->f_bsize = sb->s_blocksize;
  523. buf->f_blocks = AFFS_SB(sb)->s_partition_size - AFFS_SB(sb)->s_reserved;
  524. buf->f_bfree = free;
  525. buf->f_bavail = free;
  526. buf->f_fsid.val[0] = (u32)id;
  527. buf->f_fsid.val[1] = (u32)(id >> 32);
  528. buf->f_namelen = 30;
  529. return 0;
  530. }
  531. static struct dentry *affs_mount(struct file_system_type *fs_type,
  532. int flags, const char *dev_name, void *data)
  533. {
  534. return mount_bdev(fs_type, flags, dev_name, data, affs_fill_super);
  535. }
  536. static struct file_system_type affs_fs_type = {
  537. .owner = THIS_MODULE,
  538. .name = "affs",
  539. .mount = affs_mount,
  540. .kill_sb = kill_block_super,
  541. .fs_flags = FS_REQUIRES_DEV,
  542. };
  543. static int __init init_affs_fs(void)
  544. {
  545. int err = init_inodecache();
  546. if (err)
  547. goto out1;
  548. err = register_filesystem(&affs_fs_type);
  549. if (err)
  550. goto out;
  551. return 0;
  552. out:
  553. destroy_inodecache();
  554. out1:
  555. return err;
  556. }
  557. static void __exit exit_affs_fs(void)
  558. {
  559. unregister_filesystem(&affs_fs_type);
  560. destroy_inodecache();
  561. }
  562. MODULE_DESCRIPTION("Amiga filesystem support for Linux");
  563. MODULE_LICENSE("GPL");
  564. module_init(init_affs_fs)
  565. module_exit(exit_affs_fs)