super.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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, uid_t *uid, gid_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 = option;
  222. *mount_opts |= SF_SETGID;
  223. break;
  224. case Opt_setuid:
  225. if (match_int(&args[0], &option))
  226. return 0;
  227. *uid = option;
  228. *mount_opts |= SF_SETUID;
  229. break;
  230. case Opt_verbose:
  231. *mount_opts |= SF_VERBOSE;
  232. break;
  233. case Opt_volume: {
  234. char *vol = match_strdup(&args[0]);
  235. if (!vol)
  236. return 0;
  237. strlcpy(volume, vol, 32);
  238. kfree(vol);
  239. break;
  240. }
  241. case Opt_ignore:
  242. /* Silently ignore the quota options */
  243. break;
  244. default:
  245. printk("AFFS: Unrecognized mount option \"%s\" "
  246. "or missing value\n", p);
  247. return 0;
  248. }
  249. }
  250. return 1;
  251. }
  252. /* This function definitely needs to be split up. Some fine day I'll
  253. * hopefully have the guts to do so. Until then: sorry for the mess.
  254. */
  255. static int affs_fill_super(struct super_block *sb, void *data, int silent)
  256. {
  257. struct affs_sb_info *sbi;
  258. struct buffer_head *root_bh = NULL;
  259. struct buffer_head *boot_bh;
  260. struct inode *root_inode = NULL;
  261. s32 root_block;
  262. int size, blocksize;
  263. u32 chksum;
  264. int num_bm;
  265. int i, j;
  266. s32 key;
  267. uid_t uid;
  268. gid_t gid;
  269. int reserved;
  270. unsigned long mount_flags;
  271. int tmp_flags; /* fix remount prototype... */
  272. u8 sig[4];
  273. int ret = -EINVAL;
  274. save_mount_options(sb, data);
  275. pr_debug("AFFS: read_super(%s)\n",data ? (const char *)data : "no options");
  276. sb->s_magic = AFFS_SUPER_MAGIC;
  277. sb->s_op = &affs_sops;
  278. sb->s_flags |= MS_NODIRATIME;
  279. sbi = kzalloc(sizeof(struct affs_sb_info), GFP_KERNEL);
  280. if (!sbi)
  281. return -ENOMEM;
  282. sb->s_fs_info = sbi;
  283. sbi->sb = sb;
  284. mutex_init(&sbi->s_bmlock);
  285. spin_lock_init(&sbi->symlink_lock);
  286. spin_lock_init(&sbi->work_lock);
  287. INIT_DELAYED_WORK(&sbi->sb_work, flush_superblock);
  288. if (!parse_options(data,&uid,&gid,&i,&reserved,&root_block,
  289. &blocksize,&sbi->s_prefix,
  290. sbi->s_volume, &mount_flags)) {
  291. printk(KERN_ERR "AFFS: Error parsing options\n");
  292. kfree(sbi->s_prefix);
  293. kfree(sbi);
  294. return -EINVAL;
  295. }
  296. /* N.B. after this point s_prefix must be released */
  297. sbi->s_flags = mount_flags;
  298. sbi->s_mode = i;
  299. sbi->s_uid = uid;
  300. sbi->s_gid = gid;
  301. sbi->s_reserved= reserved;
  302. /* Get the size of the device in 512-byte blocks.
  303. * If we later see that the partition uses bigger
  304. * blocks, we will have to change it.
  305. */
  306. size = sb->s_bdev->bd_inode->i_size >> 9;
  307. pr_debug("AFFS: initial blocksize=%d, #blocks=%d\n", 512, size);
  308. affs_set_blocksize(sb, PAGE_SIZE);
  309. /* Try to find root block. Its location depends on the block size. */
  310. i = 512;
  311. j = 4096;
  312. if (blocksize > 0) {
  313. i = j = blocksize;
  314. size = size / (blocksize / 512);
  315. }
  316. for (blocksize = i, key = 0; blocksize <= j; blocksize <<= 1, size >>= 1) {
  317. sbi->s_root_block = root_block;
  318. if (root_block < 0)
  319. sbi->s_root_block = (reserved + size - 1) / 2;
  320. pr_debug("AFFS: setting blocksize to %d\n", blocksize);
  321. affs_set_blocksize(sb, blocksize);
  322. sbi->s_partition_size = size;
  323. /* The root block location that was calculated above is not
  324. * correct if the partition size is an odd number of 512-
  325. * byte blocks, which will be rounded down to a number of
  326. * 1024-byte blocks, and if there were an even number of
  327. * reserved blocks. Ideally, all partition checkers should
  328. * report the real number of blocks of the real blocksize,
  329. * but since this just cannot be done, we have to try to
  330. * find the root block anyways. In the above case, it is one
  331. * block behind the calculated one. So we check this one, too.
  332. */
  333. for (num_bm = 0; num_bm < 2; num_bm++) {
  334. pr_debug("AFFS: Dev %s, trying root=%u, bs=%d, "
  335. "size=%d, reserved=%d\n",
  336. sb->s_id,
  337. sbi->s_root_block + num_bm,
  338. blocksize, size, reserved);
  339. root_bh = affs_bread(sb, sbi->s_root_block + num_bm);
  340. if (!root_bh)
  341. continue;
  342. if (!affs_checksum_block(sb, root_bh) &&
  343. be32_to_cpu(AFFS_ROOT_HEAD(root_bh)->ptype) == T_SHORT &&
  344. be32_to_cpu(AFFS_ROOT_TAIL(sb, root_bh)->stype) == ST_ROOT) {
  345. sbi->s_hashsize = blocksize / 4 - 56;
  346. sbi->s_root_block += num_bm;
  347. key = 1;
  348. goto got_root;
  349. }
  350. affs_brelse(root_bh);
  351. root_bh = NULL;
  352. }
  353. }
  354. if (!silent)
  355. printk(KERN_ERR "AFFS: No valid root block on device %s\n",
  356. sb->s_id);
  357. goto out_error;
  358. /* N.B. after this point bh must be released */
  359. got_root:
  360. root_block = sbi->s_root_block;
  361. /* Find out which kind of FS we have */
  362. boot_bh = sb_bread(sb, 0);
  363. if (!boot_bh) {
  364. printk(KERN_ERR "AFFS: Cannot read boot block\n");
  365. goto out_error;
  366. }
  367. memcpy(sig, boot_bh->b_data, 4);
  368. brelse(boot_bh);
  369. chksum = be32_to_cpu(*(__be32 *)sig);
  370. /* Dircache filesystems are compatible with non-dircache ones
  371. * when reading. As long as they aren't supported, writing is
  372. * not recommended.
  373. */
  374. if ((chksum == FS_DCFFS || chksum == MUFS_DCFFS || chksum == FS_DCOFS
  375. || chksum == MUFS_DCOFS) && !(sb->s_flags & MS_RDONLY)) {
  376. printk(KERN_NOTICE "AFFS: Dircache FS - mounting %s read only\n",
  377. sb->s_id);
  378. sb->s_flags |= MS_RDONLY;
  379. }
  380. switch (chksum) {
  381. case MUFS_FS:
  382. case MUFS_INTLFFS:
  383. case MUFS_DCFFS:
  384. sbi->s_flags |= SF_MUFS;
  385. /* fall thru */
  386. case FS_INTLFFS:
  387. case FS_DCFFS:
  388. sbi->s_flags |= SF_INTL;
  389. break;
  390. case MUFS_FFS:
  391. sbi->s_flags |= SF_MUFS;
  392. break;
  393. case FS_FFS:
  394. break;
  395. case MUFS_OFS:
  396. sbi->s_flags |= SF_MUFS;
  397. /* fall thru */
  398. case FS_OFS:
  399. sbi->s_flags |= SF_OFS;
  400. sb->s_flags |= MS_NOEXEC;
  401. break;
  402. case MUFS_DCOFS:
  403. case MUFS_INTLOFS:
  404. sbi->s_flags |= SF_MUFS;
  405. case FS_DCOFS:
  406. case FS_INTLOFS:
  407. sbi->s_flags |= SF_INTL | SF_OFS;
  408. sb->s_flags |= MS_NOEXEC;
  409. break;
  410. default:
  411. printk(KERN_ERR "AFFS: Unknown filesystem on device %s: %08X\n",
  412. sb->s_id, chksum);
  413. goto out_error;
  414. }
  415. if (mount_flags & SF_VERBOSE) {
  416. u8 len = AFFS_ROOT_TAIL(sb, root_bh)->disk_name[0];
  417. printk(KERN_NOTICE "AFFS: Mounting volume \"%.*s\": Type=%.3s\\%c, Blocksize=%d\n",
  418. len > 31 ? 31 : len,
  419. AFFS_ROOT_TAIL(sb, root_bh)->disk_name + 1,
  420. sig, sig[3] + '0', blocksize);
  421. }
  422. sb->s_flags |= MS_NODEV | MS_NOSUID;
  423. sbi->s_data_blksize = sb->s_blocksize;
  424. if (sbi->s_flags & SF_OFS)
  425. sbi->s_data_blksize -= 24;
  426. /* Keep super block in cache */
  427. sbi->s_root_bh = root_bh;
  428. /* N.B. after this point s_root_bh must be released */
  429. tmp_flags = sb->s_flags;
  430. if (affs_init_bitmap(sb, &tmp_flags))
  431. goto out_error;
  432. sb->s_flags = tmp_flags;
  433. /* set up enough so that it can read an inode */
  434. root_inode = affs_iget(sb, root_block);
  435. if (IS_ERR(root_inode)) {
  436. ret = PTR_ERR(root_inode);
  437. goto out_error;
  438. }
  439. if (AFFS_SB(sb)->s_flags & SF_INTL)
  440. sb->s_d_op = &affs_intl_dentry_operations;
  441. else
  442. sb->s_d_op = &affs_dentry_operations;
  443. sb->s_root = d_make_root(root_inode);
  444. if (!sb->s_root) {
  445. printk(KERN_ERR "AFFS: Get root inode failed\n");
  446. goto out_error;
  447. }
  448. pr_debug("AFFS: s_flags=%lX\n",sb->s_flags);
  449. return 0;
  450. /*
  451. * Begin the cascaded cleanup ...
  452. */
  453. out_error:
  454. kfree(sbi->s_bitmap);
  455. affs_brelse(root_bh);
  456. kfree(sbi->s_prefix);
  457. kfree(sbi);
  458. sb->s_fs_info = NULL;
  459. return ret;
  460. }
  461. static int
  462. affs_remount(struct super_block *sb, int *flags, char *data)
  463. {
  464. struct affs_sb_info *sbi = AFFS_SB(sb);
  465. int blocksize;
  466. uid_t uid;
  467. gid_t gid;
  468. int mode;
  469. int reserved;
  470. int root_block;
  471. unsigned long mount_flags;
  472. int res = 0;
  473. char *new_opts = kstrdup(data, GFP_KERNEL);
  474. char volume[32];
  475. char *prefix = NULL;
  476. pr_debug("AFFS: remount(flags=0x%x,opts=\"%s\")\n",*flags,data);
  477. *flags |= MS_NODIRATIME;
  478. memcpy(volume, sbi->s_volume, 32);
  479. if (!parse_options(data, &uid, &gid, &mode, &reserved, &root_block,
  480. &blocksize, &prefix, volume,
  481. &mount_flags)) {
  482. kfree(prefix);
  483. kfree(new_opts);
  484. return -EINVAL;
  485. }
  486. flush_delayed_work_sync(&sbi->sb_work);
  487. replace_mount_options(sb, new_opts);
  488. sbi->s_flags = mount_flags;
  489. sbi->s_mode = mode;
  490. sbi->s_uid = uid;
  491. sbi->s_gid = gid;
  492. /* protect against readers */
  493. spin_lock(&sbi->symlink_lock);
  494. if (prefix) {
  495. kfree(sbi->s_prefix);
  496. sbi->s_prefix = prefix;
  497. }
  498. memcpy(sbi->s_volume, volume, 32);
  499. spin_unlock(&sbi->symlink_lock);
  500. if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
  501. return 0;
  502. if (*flags & MS_RDONLY)
  503. affs_free_bitmap(sb);
  504. else
  505. res = affs_init_bitmap(sb, flags);
  506. return res;
  507. }
  508. static int
  509. affs_statfs(struct dentry *dentry, struct kstatfs *buf)
  510. {
  511. struct super_block *sb = dentry->d_sb;
  512. int free;
  513. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  514. pr_debug("AFFS: statfs() partsize=%d, reserved=%d\n",AFFS_SB(sb)->s_partition_size,
  515. AFFS_SB(sb)->s_reserved);
  516. free = affs_count_free_blocks(sb);
  517. buf->f_type = AFFS_SUPER_MAGIC;
  518. buf->f_bsize = sb->s_blocksize;
  519. buf->f_blocks = AFFS_SB(sb)->s_partition_size - AFFS_SB(sb)->s_reserved;
  520. buf->f_bfree = free;
  521. buf->f_bavail = free;
  522. buf->f_fsid.val[0] = (u32)id;
  523. buf->f_fsid.val[1] = (u32)(id >> 32);
  524. buf->f_namelen = 30;
  525. return 0;
  526. }
  527. static struct dentry *affs_mount(struct file_system_type *fs_type,
  528. int flags, const char *dev_name, void *data)
  529. {
  530. return mount_bdev(fs_type, flags, dev_name, data, affs_fill_super);
  531. }
  532. static struct file_system_type affs_fs_type = {
  533. .owner = THIS_MODULE,
  534. .name = "affs",
  535. .mount = affs_mount,
  536. .kill_sb = kill_block_super,
  537. .fs_flags = FS_REQUIRES_DEV,
  538. };
  539. static int __init init_affs_fs(void)
  540. {
  541. int err = init_inodecache();
  542. if (err)
  543. goto out1;
  544. err = register_filesystem(&affs_fs_type);
  545. if (err)
  546. goto out;
  547. return 0;
  548. out:
  549. destroy_inodecache();
  550. out1:
  551. return err;
  552. }
  553. static void __exit exit_affs_fs(void)
  554. {
  555. unregister_filesystem(&affs_fs_type);
  556. destroy_inodecache();
  557. }
  558. MODULE_DESCRIPTION("Amiga filesystem support for Linux");
  559. MODULE_LICENSE("GPL");
  560. module_init(init_affs_fs)
  561. module_exit(exit_affs_fs)