super.c 15 KB

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