PageRenderTime 29ms CodeModel.GetById 2ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/affs/super.c

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