12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2acaebfd8SDavid Howells /* MTD-based superblock management 3acaebfd8SDavid Howells * 4acaebfd8SDavid Howells * Copyright © 2001-2007 Red Hat, Inc. All Rights Reserved. 5a1452a37SDavid Woodhouse * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org> 6a1452a37SDavid Woodhouse * 7acaebfd8SDavid Howells * Written by: David Howells <dhowells@redhat.com> 8acaebfd8SDavid Howells * David Woodhouse <dwmw2@infradead.org> 9acaebfd8SDavid Howells */ 10acaebfd8SDavid Howells 11acaebfd8SDavid Howells #include <linux/mtd/super.h> 12acaebfd8SDavid Howells #include <linux/namei.h> 13f3bcc017SPaul Gortmaker #include <linux/export.h> 14acaebfd8SDavid Howells #include <linux/ctype.h> 156de94002SJörn Engel #include <linux/slab.h> 16f83c3838SEzequiel Garcia #include <linux/major.h> 17fa06052dSJan Kara #include <linux/backing-dev.h> 18acaebfd8SDavid Howells 19acaebfd8SDavid Howells /* 20acaebfd8SDavid Howells * compare superblocks to see if they're equivalent 21acaebfd8SDavid Howells * - they are if the underlying MTD device is the same 22acaebfd8SDavid Howells */ 23acaebfd8SDavid Howells static int get_sb_mtd_compare(struct super_block *sb, void *_mtd) 24acaebfd8SDavid Howells { 25acaebfd8SDavid Howells struct mtd_info *mtd = _mtd; 26acaebfd8SDavid Howells 27acaebfd8SDavid Howells if (sb->s_mtd == mtd) { 28289c0522SBrian Norris pr_debug("MTDSB: Match on device %d (\"%s\")\n", 29acaebfd8SDavid Howells mtd->index, mtd->name); 30acaebfd8SDavid Howells return 1; 31acaebfd8SDavid Howells } 32acaebfd8SDavid Howells 33289c0522SBrian Norris pr_debug("MTDSB: No match, device %d (\"%s\"), device %d (\"%s\")\n", 34acaebfd8SDavid Howells sb->s_mtd->index, sb->s_mtd->name, mtd->index, mtd->name); 35acaebfd8SDavid Howells return 0; 36acaebfd8SDavid Howells } 37acaebfd8SDavid Howells 38fa06052dSJan Kara extern struct backing_dev_info *mtd_bdi; 39fa06052dSJan Kara 40acaebfd8SDavid Howells /* 41acaebfd8SDavid Howells * mark the superblock by the MTD device it is using 42acaebfd8SDavid Howells * - set the device number to be the correct MTD block device for pesuperstence 43acaebfd8SDavid Howells * of NFS exports 44acaebfd8SDavid Howells */ 45acaebfd8SDavid Howells static int get_sb_mtd_set(struct super_block *sb, void *_mtd) 46acaebfd8SDavid Howells { 47acaebfd8SDavid Howells struct mtd_info *mtd = _mtd; 48acaebfd8SDavid Howells 49acaebfd8SDavid Howells sb->s_mtd = mtd; 50acaebfd8SDavid Howells sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, mtd->index); 51fa06052dSJan Kara sb->s_bdi = bdi_get(mtd_bdi); 52fa06052dSJan Kara 53acaebfd8SDavid Howells return 0; 54acaebfd8SDavid Howells } 55acaebfd8SDavid Howells 56acaebfd8SDavid Howells /* 57acaebfd8SDavid Howells * get a superblock on an MTD-backed filesystem 58acaebfd8SDavid Howells */ 59848b83a5SAl Viro static struct dentry *mount_mtd_aux(struct file_system_type *fs_type, int flags, 60acaebfd8SDavid Howells const char *dev_name, void *data, 61acaebfd8SDavid Howells struct mtd_info *mtd, 62848b83a5SAl Viro int (*fill_super)(struct super_block *, void *, int)) 63acaebfd8SDavid Howells { 64acaebfd8SDavid Howells struct super_block *sb; 65acaebfd8SDavid Howells int ret; 66acaebfd8SDavid Howells 679249e17fSDavid Howells sb = sget(fs_type, get_sb_mtd_compare, get_sb_mtd_set, flags, mtd); 68acaebfd8SDavid Howells if (IS_ERR(sb)) 69acaebfd8SDavid Howells goto out_error; 70acaebfd8SDavid Howells 71acaebfd8SDavid Howells if (sb->s_root) 72acaebfd8SDavid Howells goto already_mounted; 73acaebfd8SDavid Howells 74acaebfd8SDavid Howells /* fresh new superblock */ 75289c0522SBrian Norris pr_debug("MTDSB: New superblock for device %d (\"%s\")\n", 76acaebfd8SDavid Howells mtd->index, mtd->name); 77acaebfd8SDavid Howells 781751e8a6SLinus Torvalds ret = fill_super(sb, data, flags & SB_SILENT ? 1 : 0); 79acaebfd8SDavid Howells if (ret < 0) { 806f5bbff9SAl Viro deactivate_locked_super(sb); 81848b83a5SAl Viro return ERR_PTR(ret); 82acaebfd8SDavid Howells } 83acaebfd8SDavid Howells 84acaebfd8SDavid Howells /* go */ 851751e8a6SLinus Torvalds sb->s_flags |= SB_ACTIVE; 86848b83a5SAl Viro return dget(sb->s_root); 87acaebfd8SDavid Howells 88acaebfd8SDavid Howells /* new mountpoint for an already mounted superblock */ 89acaebfd8SDavid Howells already_mounted: 90289c0522SBrian Norris pr_debug("MTDSB: Device %d (\"%s\") is already mounted\n", 91acaebfd8SDavid Howells mtd->index, mtd->name); 92848b83a5SAl Viro put_mtd_device(mtd); 93848b83a5SAl Viro return dget(sb->s_root); 94acaebfd8SDavid Howells 95acaebfd8SDavid Howells out_error: 96acaebfd8SDavid Howells put_mtd_device(mtd); 97848b83a5SAl Viro return ERR_CAST(sb); 98acaebfd8SDavid Howells } 99acaebfd8SDavid Howells 100acaebfd8SDavid Howells /* 101acaebfd8SDavid Howells * get a superblock on an MTD-backed filesystem by MTD device number 102acaebfd8SDavid Howells */ 103848b83a5SAl Viro static struct dentry *mount_mtd_nr(struct file_system_type *fs_type, int flags, 104acaebfd8SDavid Howells const char *dev_name, void *data, int mtdnr, 105848b83a5SAl Viro int (*fill_super)(struct super_block *, void *, int)) 106acaebfd8SDavid Howells { 107acaebfd8SDavid Howells struct mtd_info *mtd; 108acaebfd8SDavid Howells 109acaebfd8SDavid Howells mtd = get_mtd_device(NULL, mtdnr); 110718ea836SDavid Woodhouse if (IS_ERR(mtd)) { 111289c0522SBrian Norris pr_debug("MTDSB: Device #%u doesn't appear to exist\n", mtdnr); 112848b83a5SAl Viro return ERR_CAST(mtd); 113acaebfd8SDavid Howells } 114acaebfd8SDavid Howells 115848b83a5SAl Viro return mount_mtd_aux(fs_type, flags, dev_name, data, mtd, fill_super); 116acaebfd8SDavid Howells } 117acaebfd8SDavid Howells 118acaebfd8SDavid Howells /* 119acaebfd8SDavid Howells * set up an MTD-based superblock 120acaebfd8SDavid Howells */ 121848b83a5SAl Viro struct dentry *mount_mtd(struct file_system_type *fs_type, int flags, 122acaebfd8SDavid Howells const char *dev_name, void *data, 123848b83a5SAl Viro int (*fill_super)(struct super_block *, void *, int)) 124acaebfd8SDavid Howells { 125f1136d02SDavid Woodhouse #ifdef CONFIG_BLOCK 126d5686b44SAl Viro struct block_device *bdev; 127f1136d02SDavid Woodhouse int ret, major; 128f1136d02SDavid Woodhouse #endif 129f1136d02SDavid Woodhouse int mtdnr; 130acaebfd8SDavid Howells 131acaebfd8SDavid Howells if (!dev_name) 132848b83a5SAl Viro return ERR_PTR(-EINVAL); 133acaebfd8SDavid Howells 134289c0522SBrian Norris pr_debug("MTDSB: dev_name \"%s\"\n", dev_name); 135acaebfd8SDavid Howells 136acaebfd8SDavid Howells /* the preferred way of mounting in future; especially when 137acaebfd8SDavid Howells * CONFIG_BLOCK=n - we specify the underlying MTD device by number or 138acaebfd8SDavid Howells * by name, so that we don't require block device support to be present 139acaebfd8SDavid Howells * in the kernel. */ 140acaebfd8SDavid Howells if (dev_name[0] == 'm' && dev_name[1] == 't' && dev_name[2] == 'd') { 141acaebfd8SDavid Howells if (dev_name[3] == ':') { 142acaebfd8SDavid Howells struct mtd_info *mtd; 143acaebfd8SDavid Howells 144acaebfd8SDavid Howells /* mount by MTD device name */ 145289c0522SBrian Norris pr_debug("MTDSB: mtd:%%s, name \"%s\"\n", 146acaebfd8SDavid Howells dev_name + 4); 147acaebfd8SDavid Howells 148677c2aecSBen Hutchings mtd = get_mtd_device_nm(dev_name + 4); 149677c2aecSBen Hutchings if (!IS_ERR(mtd)) 150848b83a5SAl Viro return mount_mtd_aux( 151acaebfd8SDavid Howells fs_type, flags, 152acaebfd8SDavid Howells dev_name, data, mtd, 153848b83a5SAl Viro fill_super); 154acaebfd8SDavid Howells 155acaebfd8SDavid Howells printk(KERN_NOTICE "MTD:" 156acaebfd8SDavid Howells " MTD device with name \"%s\" not found.\n", 157acaebfd8SDavid Howells dev_name + 4); 158acaebfd8SDavid Howells 159acaebfd8SDavid Howells } else if (isdigit(dev_name[3])) { 160acaebfd8SDavid Howells /* mount by MTD device number name */ 161acaebfd8SDavid Howells char *endptr; 162acaebfd8SDavid Howells 163acaebfd8SDavid Howells mtdnr = simple_strtoul(dev_name + 3, &endptr, 0); 164acaebfd8SDavid Howells if (!*endptr) { 165acaebfd8SDavid Howells /* It was a valid number */ 166289c0522SBrian Norris pr_debug("MTDSB: mtd%%d, mtdnr %d\n", 167acaebfd8SDavid Howells mtdnr); 168848b83a5SAl Viro return mount_mtd_nr(fs_type, flags, 169acaebfd8SDavid Howells dev_name, data, 170848b83a5SAl Viro mtdnr, fill_super); 171acaebfd8SDavid Howells } 172acaebfd8SDavid Howells } 173acaebfd8SDavid Howells } 174acaebfd8SDavid Howells 175f1136d02SDavid Woodhouse #ifdef CONFIG_BLOCK 176acaebfd8SDavid Howells /* try the old way - the hack where we allowed users to mount 177acaebfd8SDavid Howells * /dev/mtdblock$(n) but didn't actually _use_ the blockdev 178acaebfd8SDavid Howells */ 179d5686b44SAl Viro bdev = lookup_bdev(dev_name); 180d5686b44SAl Viro if (IS_ERR(bdev)) { 181d5686b44SAl Viro ret = PTR_ERR(bdev); 182289c0522SBrian Norris pr_debug("MTDSB: lookup_bdev() returned %d\n", ret); 183848b83a5SAl Viro return ERR_PTR(ret); 184d5686b44SAl Viro } 185289c0522SBrian Norris pr_debug("MTDSB: lookup_bdev() returned 0\n"); 186acaebfd8SDavid Howells 187acaebfd8SDavid Howells ret = -EINVAL; 188acaebfd8SDavid Howells 189f1136d02SDavid Woodhouse major = MAJOR(bdev->bd_dev); 190d5686b44SAl Viro mtdnr = MINOR(bdev->bd_dev); 191d5686b44SAl Viro bdput(bdev); 192acaebfd8SDavid Howells 193f1136d02SDavid Woodhouse if (major != MTD_BLOCK_MAJOR) 194f1136d02SDavid Woodhouse goto not_an_MTD_device; 195f1136d02SDavid Woodhouse 196848b83a5SAl Viro return mount_mtd_nr(fs_type, flags, dev_name, data, mtdnr, fill_super); 197acaebfd8SDavid Howells 198acaebfd8SDavid Howells not_an_MTD_device: 199f1136d02SDavid Woodhouse #endif /* CONFIG_BLOCK */ 200f1136d02SDavid Woodhouse 2011751e8a6SLinus Torvalds if (!(flags & SB_SILENT)) 202acaebfd8SDavid Howells printk(KERN_NOTICE 203acaebfd8SDavid Howells "MTD: Attempt to mount non-MTD device \"%s\"\n", 204acaebfd8SDavid Howells dev_name); 205848b83a5SAl Viro return ERR_PTR(-EINVAL); 206acaebfd8SDavid Howells } 207acaebfd8SDavid Howells 208848b83a5SAl Viro EXPORT_SYMBOL_GPL(mount_mtd); 209acaebfd8SDavid Howells 210acaebfd8SDavid Howells /* 211acaebfd8SDavid Howells * destroy an MTD-based superblock 212acaebfd8SDavid Howells */ 213acaebfd8SDavid Howells void kill_mtd_super(struct super_block *sb) 214acaebfd8SDavid Howells { 215acaebfd8SDavid Howells generic_shutdown_super(sb); 216acaebfd8SDavid Howells put_mtd_device(sb->s_mtd); 217acaebfd8SDavid Howells sb->s_mtd = NULL; 218acaebfd8SDavid Howells } 219acaebfd8SDavid Howells 220acaebfd8SDavid Howells EXPORT_SYMBOL_GPL(kill_mtd_super); 221