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