1acaebfd8SDavid Howells /* MTD-based superblock management 2acaebfd8SDavid Howells * 3acaebfd8SDavid Howells * Copyright © 2001-2007 Red Hat, Inc. All Rights Reserved. 4a1452a37SDavid Woodhouse * Copyright © 2001-2010 David Woodhouse <dwmw2@infradead.org> 5a1452a37SDavid Woodhouse * 6acaebfd8SDavid Howells * Written by: David Howells <dhowells@redhat.com> 7acaebfd8SDavid Howells * David Woodhouse <dwmw2@infradead.org> 8acaebfd8SDavid Howells * 9acaebfd8SDavid Howells * This program is free software; you can redistribute it and/or 10acaebfd8SDavid Howells * modify it under the terms of the GNU General Public License 11acaebfd8SDavid Howells * as published by the Free Software Foundation; either version 12acaebfd8SDavid Howells * 2 of the License, or (at your option) any later version. 13acaebfd8SDavid Howells */ 14acaebfd8SDavid Howells 15acaebfd8SDavid Howells #include <linux/mtd/super.h> 16acaebfd8SDavid Howells #include <linux/namei.h> 17acaebfd8SDavid Howells #include <linux/ctype.h> 186de94002SJörn Engel #include <linux/slab.h> 19acaebfd8SDavid Howells 20acaebfd8SDavid Howells /* 21acaebfd8SDavid Howells * compare superblocks to see if they're equivalent 22acaebfd8SDavid Howells * - they are if the underlying MTD device is the same 23acaebfd8SDavid Howells */ 24acaebfd8SDavid Howells static int get_sb_mtd_compare(struct super_block *sb, void *_mtd) 25acaebfd8SDavid Howells { 26acaebfd8SDavid Howells struct mtd_info *mtd = _mtd; 27acaebfd8SDavid Howells 28acaebfd8SDavid Howells if (sb->s_mtd == mtd) { 29289c0522SBrian Norris pr_debug("MTDSB: Match on device %d (\"%s\")\n", 30acaebfd8SDavid Howells mtd->index, mtd->name); 31acaebfd8SDavid Howells return 1; 32acaebfd8SDavid Howells } 33acaebfd8SDavid Howells 34289c0522SBrian Norris pr_debug("MTDSB: No match, device %d (\"%s\"), device %d (\"%s\")\n", 35acaebfd8SDavid Howells sb->s_mtd->index, sb->s_mtd->name, mtd->index, mtd->name); 36acaebfd8SDavid Howells return 0; 37acaebfd8SDavid Howells } 38acaebfd8SDavid Howells 39acaebfd8SDavid Howells /* 40acaebfd8SDavid Howells * mark the superblock by the MTD device it is using 41acaebfd8SDavid Howells * - set the device number to be the correct MTD block device for pesuperstence 42acaebfd8SDavid Howells * of NFS exports 43acaebfd8SDavid Howells */ 44acaebfd8SDavid Howells static int get_sb_mtd_set(struct super_block *sb, void *_mtd) 45acaebfd8SDavid Howells { 46acaebfd8SDavid Howells struct mtd_info *mtd = _mtd; 47acaebfd8SDavid Howells 48acaebfd8SDavid Howells sb->s_mtd = mtd; 49acaebfd8SDavid Howells sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, mtd->index); 506de94002SJörn Engel sb->s_bdi = mtd->backing_dev_info; 51acaebfd8SDavid Howells return 0; 52acaebfd8SDavid Howells } 53acaebfd8SDavid Howells 54acaebfd8SDavid Howells /* 55acaebfd8SDavid Howells * get a superblock on an MTD-backed filesystem 56acaebfd8SDavid Howells */ 57848b83a5SAl Viro static struct dentry *mount_mtd_aux(struct file_system_type *fs_type, int flags, 58acaebfd8SDavid Howells const char *dev_name, void *data, 59acaebfd8SDavid Howells struct mtd_info *mtd, 60848b83a5SAl Viro int (*fill_super)(struct super_block *, void *, int)) 61acaebfd8SDavid Howells { 62acaebfd8SDavid Howells struct super_block *sb; 63acaebfd8SDavid Howells int ret; 64acaebfd8SDavid Howells 65acaebfd8SDavid Howells sb = sget(fs_type, get_sb_mtd_compare, get_sb_mtd_set, mtd); 66acaebfd8SDavid Howells if (IS_ERR(sb)) 67acaebfd8SDavid Howells goto out_error; 68acaebfd8SDavid Howells 69acaebfd8SDavid Howells if (sb->s_root) 70acaebfd8SDavid Howells goto already_mounted; 71acaebfd8SDavid Howells 72acaebfd8SDavid Howells /* fresh new superblock */ 73289c0522SBrian Norris pr_debug("MTDSB: New superblock for device %d (\"%s\")\n", 74acaebfd8SDavid Howells mtd->index, mtd->name); 75acaebfd8SDavid Howells 7648440e89SDavid Howells sb->s_flags = flags; 7748440e89SDavid Howells 78acaebfd8SDavid Howells ret = fill_super(sb, data, flags & MS_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 */ 85acaebfd8SDavid Howells sb->s_flags |= MS_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 201acaebfd8SDavid Howells if (!(flags & MS_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