mtdsuper.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include <linux/slab.h>
  16. /*
  17. * compare superblocks to see if they're equivalent
  18. * - they are if the underlying MTD device is the same
  19. */
  20. static int get_sb_mtd_compare(struct super_block *sb, void *_mtd)
  21. {
  22. struct mtd_info *mtd = _mtd;
  23. if (sb->s_mtd == mtd) {
  24. DEBUG(2, "MTDSB: Match on device %d (\"%s\")\n",
  25. mtd->index, mtd->name);
  26. return 1;
  27. }
  28. DEBUG(2, "MTDSB: No match, device %d (\"%s\"), device %d (\"%s\")\n",
  29. sb->s_mtd->index, sb->s_mtd->name, mtd->index, mtd->name);
  30. return 0;
  31. }
  32. /*
  33. * mark the superblock by the MTD device it is using
  34. * - set the device number to be the correct MTD block device for pesuperstence
  35. * of NFS exports
  36. */
  37. static int get_sb_mtd_set(struct super_block *sb, void *_mtd)
  38. {
  39. struct mtd_info *mtd = _mtd;
  40. sb->s_mtd = mtd;
  41. sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, mtd->index);
  42. sb->s_bdi = mtd->backing_dev_info;
  43. return 0;
  44. }
  45. /*
  46. * get a superblock on an MTD-backed filesystem
  47. */
  48. static int get_sb_mtd_aux(struct file_system_type *fs_type, int flags,
  49. const char *dev_name, void *data,
  50. struct mtd_info *mtd,
  51. int (*fill_super)(struct super_block *, void *, int),
  52. struct vfsmount *mnt)
  53. {
  54. struct super_block *sb;
  55. int ret;
  56. sb = sget(fs_type, get_sb_mtd_compare, get_sb_mtd_set, mtd);
  57. if (IS_ERR(sb))
  58. goto out_error;
  59. if (sb->s_root)
  60. goto already_mounted;
  61. /* fresh new superblock */
  62. DEBUG(1, "MTDSB: New superblock for device %d (\"%s\")\n",
  63. mtd->index, mtd->name);
  64. sb->s_flags = flags;
  65. ret = fill_super(sb, data, flags & MS_SILENT ? 1 : 0);
  66. if (ret < 0) {
  67. deactivate_locked_super(sb);
  68. return ret;
  69. }
  70. /* go */
  71. sb->s_flags |= MS_ACTIVE;
  72. simple_set_mnt(mnt, sb);
  73. return 0;
  74. /* new mountpoint for an already mounted superblock */
  75. already_mounted:
  76. DEBUG(1, "MTDSB: Device %d (\"%s\") is already mounted\n",
  77. mtd->index, mtd->name);
  78. simple_set_mnt(mnt, sb);
  79. ret = 0;
  80. goto out_put;
  81. out_error:
  82. ret = PTR_ERR(sb);
  83. out_put:
  84. put_mtd_device(mtd);
  85. return ret;
  86. }
  87. /*
  88. * get a superblock on an MTD-backed filesystem by MTD device number
  89. */
  90. static int get_sb_mtd_nr(struct file_system_type *fs_type, int flags,
  91. const char *dev_name, void *data, int mtdnr,
  92. int (*fill_super)(struct super_block *, void *, int),
  93. struct vfsmount *mnt)
  94. {
  95. struct mtd_info *mtd;
  96. mtd = get_mtd_device(NULL, mtdnr);
  97. if (IS_ERR(mtd)) {
  98. DEBUG(0, "MTDSB: Device #%u doesn't appear to exist\n", mtdnr);
  99. return PTR_ERR(mtd);
  100. }
  101. return get_sb_mtd_aux(fs_type, flags, dev_name, data, mtd, fill_super,
  102. mnt);
  103. }
  104. /*
  105. * set up an MTD-based superblock
  106. */
  107. int get_sb_mtd(struct file_system_type *fs_type, int flags,
  108. const char *dev_name, void *data,
  109. int (*fill_super)(struct super_block *, void *, int),
  110. struct vfsmount *mnt)
  111. {
  112. #ifdef CONFIG_BLOCK
  113. struct block_device *bdev;
  114. int ret, major;
  115. #endif
  116. int mtdnr;
  117. if (!dev_name)
  118. return -EINVAL;
  119. DEBUG(2, "MTDSB: dev_name \"%s\"\n", dev_name);
  120. /* the preferred way of mounting in future; especially when
  121. * CONFIG_BLOCK=n - we specify the underlying MTD device by number or
  122. * by name, so that we don't require block device support to be present
  123. * in the kernel. */
  124. if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') {
  125. if (dev_name[3] == ':') {
  126. struct mtd_info *mtd;
  127. /* mount by MTD device name */
  128. DEBUG(1, "MTDSB: mtd:%%s, name \"%s\"\n",
  129. dev_name + 4);
  130. mtd = get_mtd_device_nm(dev_name + 4);
  131. if (!IS_ERR(mtd))
  132. return get_sb_mtd_aux(
  133. fs_type, flags,
  134. dev_name, data, mtd,
  135. fill_super, mnt);
  136. printk(KERN_NOTICE "MTD:"
  137. " MTD device with name \"%s\" not found.\n",
  138. dev_name + 4);
  139. } else if (isdigit(dev_name[3])) {
  140. /* mount by MTD device number name */
  141. char *endptr;
  142. mtdnr = simple_strtoul(dev_name + 3, &endptr, 0);
  143. if (!*endptr) {
  144. /* It was a valid number */
  145. DEBUG(1, "MTDSB: mtd%%d, mtdnr %d\n",
  146. mtdnr);
  147. return get_sb_mtd_nr(fs_type, flags,
  148. dev_name, data,
  149. mtdnr, fill_super, mnt);
  150. }
  151. }
  152. }
  153. #ifdef CONFIG_BLOCK
  154. /* try the old way - the hack where we allowed users to mount
  155. * /dev/mtdblock$(n) but didn't actually _use_ the blockdev
  156. */
  157. bdev = lookup_bdev(dev_name);
  158. if (IS_ERR(bdev)) {
  159. ret = PTR_ERR(bdev);
  160. DEBUG(1, "MTDSB: lookup_bdev() returned %d\n", ret);
  161. return ret;
  162. }
  163. DEBUG(1, "MTDSB: lookup_bdev() returned 0\n");
  164. ret = -EINVAL;
  165. major = MAJOR(bdev->bd_dev);
  166. mtdnr = MINOR(bdev->bd_dev);
  167. bdput(bdev);
  168. if (major != MTD_BLOCK_MAJOR)
  169. goto not_an_MTD_device;
  170. return get_sb_mtd_nr(fs_type, flags, dev_name, data, mtdnr, fill_super,
  171. mnt);
  172. not_an_MTD_device:
  173. #endif /* CONFIG_BLOCK */
  174. if (!(flags & MS_SILENT))
  175. printk(KERN_NOTICE
  176. "MTD: Attempt to mount non-MTD device \"%s\"\n",
  177. dev_name);
  178. return -EINVAL;
  179. }
  180. EXPORT_SYMBOL_GPL(get_sb_mtd);
  181. /*
  182. * destroy an MTD-based superblock
  183. */
  184. void kill_mtd_super(struct super_block *sb)
  185. {
  186. generic_shutdown_super(sb);
  187. put_mtd_device(sb->s_mtd);
  188. sb->s_mtd = NULL;
  189. }
  190. EXPORT_SYMBOL_GPL(kill_mtd_super);