mtdsuper.c 5.5 KB

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