mtdsuper.c 5.5 KB

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