super.c 14 KB

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