mtdsuper.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* MTD-based superblock management
  2. *
  3. * Copyright © 2001-2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by: David Howells <dhowells@redhat.com>
  5. * David Woodhouse <dwmw2@infradead.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/mtd/super.h>
  13. #include <linux/namei.h>
  14. #include <linux/ctype.h>
  15. /*
  16. * compare superblocks to see if they're equivalent
  17. * - they are if the underlying MTD device is the same
  18. */
  19. static int get_sb_mtd_compare(struct super_block *sb, void *_mtd)
  20. {
  21. struct mtd_info *mtd = _mtd;
  22. if (sb->s_mtd == mtd) {
  23. DEBUG(2, "MTDSB: Match on device %d (\"%s\")\n",
  24. mtd->index, mtd->name);
  25. return 1;
  26. }
  27. DEBUG(2, "MTDSB: No match, device %d (\"%s\"), device %d (\"%s\")\n",
  28. sb->s_mtd->index, sb->s_mtd->name, mtd->index, mtd->name);
  29. return 0;
  30. }
  31. /*
  32. * mark the superblock by the MTD device it is using
  33. * - set the device number to be the correct MTD block device for pesuperstence
  34. * of NFS exports
  35. */
  36. static int get_sb_mtd_set(struct super_block *sb, void *_mtd)
  37. {
  38. struct mtd_info *mtd = _mtd;
  39. sb->s_mtd = mtd;
  40. sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, mtd->index);
  41. return 0;
  42. }
  43. /*
  44. * get a superblock on an MTD-backed filesystem
  45. */
  46. static int get_sb_mtd_aux(struct file_system_type *fs_type, int flags,
  47. const char *dev_name, void *data,
  48. struct mtd_info *mtd,
  49. int (*fill_super)(struct super_block *, void *, int),
  50. struct vfsmount *mnt)
  51. {
  52. struct super_block *sb;
  53. int ret;
  54. sb = sget(fs_type, get_sb_mtd_compare, get_sb_mtd_set, mtd);
  55. if (IS_ERR(sb))
  56. goto out_error;
  57. if (sb->s_root)
  58. goto already_mounted;
  59. /* fresh new superblock */
  60. DEBUG(1, "MTDSB: New superblock for device %d (\"%s\")\n",
  61. mtd->index, mtd->name);
  62. ret = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  63. if (ret < 0) {
  64. up_write(&sb->s_umount);
  65. deactivate_super(sb);
  66. return ret;
  67. }
  68. /* go */
  69. sb->s_flags |= MS_ACTIVE;
  70. return simple_set_mnt(mnt, sb);
  71. /* new mountpoint for an already mounted superblock */
  72. already_mounted:
  73. DEBUG(1, "MTDSB: Device %d (\"%s\") is already mounted\n",
  74. mtd->index, mtd->name);
  75. ret = simple_set_mnt(mnt, sb);
  76. goto out_put;
  77. out_error:
  78. ret = PTR_ERR(sb);
  79. out_put:
  80. put_mtd_device(mtd);
  81. return ret;
  82. }
  83. /*
  84. * get a superblock on an MTD-backed filesystem by MTD device number
  85. */
  86. static int get_sb_mtd_nr(struct file_system_type *fs_type, int flags,
  87. const char *dev_name, void *data, int mtdnr,
  88. int (*fill_super)(struct super_block *, void *, int),
  89. struct vfsmount *mnt)
  90. {
  91. struct mtd_info *mtd;
  92. mtd = get_mtd_device(NULL, mtdnr);
  93. if (IS_ERR(mtd)) {
  94. DEBUG(0, "MTDSB: Device #%u doesn't appear to exist\n", mtdnr);
  95. return PTR_ERR(mtd);
  96. }
  97. return get_sb_mtd_aux(fs_type, flags, dev_name, data, mtd, fill_super,
  98. mnt);
  99. }
  100. /*
  101. * set up an MTD-based superblock
  102. */
  103. int get_sb_mtd(struct file_system_type *fs_type, int flags,
  104. const char *dev_name, void *data,
  105. int (*fill_super)(struct super_block *, void *, int),
  106. struct vfsmount *mnt)
  107. {
  108. struct nameidata nd;
  109. int mtdnr, ret;
  110. if (!dev_name)
  111. return -EINVAL;
  112. DEBUG(2, "MTDSB: dev_name \"%s\"\n", dev_name);
  113. /* the preferred way of mounting in future; especially when
  114. * CONFIG_BLOCK=n - we specify the underlying MTD device by number or
  115. * by name, so that we don't require block device support to be present
  116. * in the kernel. */
  117. if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') {
  118. if (dev_name[3] == ':') {
  119. struct mtd_info *mtd;
  120. /* mount by MTD device name */
  121. DEBUG(1, "MTDSB: mtd:%%s, name \"%s\"\n",
  122. dev_name + 4);
  123. for (mtdnr = 0; mtdnr < MAX_MTD_DEVICES; mtdnr++) {
  124. mtd = get_mtd_device(NULL, mtdnr);
  125. if (!IS_ERR(mtd)) {
  126. if (!strcmp(mtd->name, dev_name + 4))
  127. return get_sb_mtd_aux(
  128. fs_type, flags,
  129. dev_name, data, mtd,
  130. fill_super, mnt);
  131. put_mtd_device(mtd);
  132. }
  133. }
  134. printk(KERN_NOTICE "MTD:"
  135. " MTD device with name \"%s\" not found.\n",
  136. dev_name + 4);
  137. } else if (isdigit(dev_name[3])) {
  138. /* mount by MTD device number name */
  139. char *endptr;
  140. mtdnr = simple_strtoul(dev_name + 3, &endptr, 0);
  141. if (!*endptr) {
  142. /* It was a valid number */
  143. DEBUG(1, "MTDSB: mtd%%d, mtdnr %d\n",
  144. mtdnr);
  145. return get_sb_mtd_nr(fs_type, flags,
  146. dev_name, data,
  147. mtdnr, fill_super, mnt);
  148. }
  149. }
  150. }
  151. /* try the old way - the hack where we allowed users to mount
  152. * /dev/mtdblock$(n) but didn't actually _use_ the blockdev
  153. */
  154. ret = path_lookup(dev_name, LOOKUP_FOLLOW, &nd);
  155. DEBUG(1, "MTDSB: path_lookup() returned %d, inode %p\n",
  156. ret, nd.dentry ? nd.dentry->d_inode : NULL);
  157. if (ret)
  158. return ret;
  159. ret = -EINVAL;
  160. if (!S_ISBLK(nd.dentry->d_inode->i_mode))
  161. goto out;
  162. if (nd.mnt->mnt_flags & MNT_NODEV) {
  163. ret = -EACCES;
  164. goto out;
  165. }
  166. if (imajor(nd.dentry->d_inode) != MTD_BLOCK_MAJOR)
  167. goto not_an_MTD_device;
  168. mtdnr = iminor(nd.dentry->d_inode);
  169. path_release(&nd);
  170. return get_sb_mtd_nr(fs_type, flags, dev_name, data, mtdnr, fill_super,
  171. mnt);
  172. not_an_MTD_device:
  173. if (!(flags & MS_SILENT))
  174. printk(KERN_NOTICE
  175. "MTD: Attempt to mount non-MTD device \"%s\"\n",
  176. dev_name);
  177. out:
  178. path_release(&nd);
  179. return ret;
  180. }
  181. EXPORT_SYMBOL_GPL(get_sb_mtd);
  182. /*
  183. * destroy an MTD-based superblock
  184. */
  185. void kill_mtd_super(struct super_block *sb)
  186. {
  187. generic_shutdown_super(sb);
  188. put_mtd_device(sb->s_mtd);
  189. sb->s_mtd = NULL;
  190. }
  191. EXPORT_SYMBOL_GPL(kill_mtd_super);