xref: /openbmc/linux/drivers/mtd/mtdsuper.c (revision ccdf7741)
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>
18*ccdf7741SChristoph Hellwig #include <linux/blkdev.h>
190f071004SDavid Howells #include <linux/fs_context.h>
200f071004SDavid Howells #include "mtdcore.h"
210f071004SDavid Howells 
220f071004SDavid Howells /*
230f071004SDavid Howells  * compare superblocks to see if they're equivalent
240f071004SDavid Howells  * - they are if the underlying MTD device is the same
250f071004SDavid Howells  */
260f071004SDavid Howells static int mtd_test_super(struct super_block *sb, struct fs_context *fc)
270f071004SDavid Howells {
280f071004SDavid Howells 	struct mtd_info *mtd = fc->sget_key;
290f071004SDavid Howells 
300f071004SDavid Howells 	if (sb->s_mtd == fc->sget_key) {
310f071004SDavid Howells 		pr_debug("MTDSB: Match on device %d (\"%s\")\n",
320f071004SDavid Howells 			 mtd->index, mtd->name);
330f071004SDavid Howells 		return 1;
340f071004SDavid Howells 	}
350f071004SDavid Howells 
360f071004SDavid Howells 	pr_debug("MTDSB: No match, device %d (\"%s\"), device %d (\"%s\")\n",
370f071004SDavid Howells 		 sb->s_mtd->index, sb->s_mtd->name, mtd->index, mtd->name);
380f071004SDavid Howells 	return 0;
390f071004SDavid Howells }
400f071004SDavid Howells 
410f071004SDavid Howells /*
420f071004SDavid Howells  * mark the superblock by the MTD device it is using
430f071004SDavid Howells  * - set the device number to be the correct MTD block device for pesuperstence
440f071004SDavid Howells  *   of NFS exports
450f071004SDavid Howells  */
460f071004SDavid Howells static int mtd_set_super(struct super_block *sb, struct fs_context *fc)
470f071004SDavid Howells {
480f071004SDavid Howells 	sb->s_mtd = fc->sget_key;
490f071004SDavid Howells 	sb->s_dev = MKDEV(MTD_BLOCK_MAJOR, sb->s_mtd->index);
500f071004SDavid Howells 	sb->s_bdi = bdi_get(mtd_bdi);
510f071004SDavid Howells 	return 0;
520f071004SDavid Howells }
530f071004SDavid Howells 
540f071004SDavid Howells /*
550f071004SDavid Howells  * get a superblock on an MTD-backed filesystem
560f071004SDavid Howells  */
570f071004SDavid Howells static int mtd_get_sb(struct fs_context *fc,
580f071004SDavid Howells 		      struct mtd_info *mtd,
590f071004SDavid Howells 		      int (*fill_super)(struct super_block *,
600f071004SDavid Howells 					struct fs_context *))
610f071004SDavid Howells {
620f071004SDavid Howells 	struct super_block *sb;
630f071004SDavid Howells 	int ret;
640f071004SDavid Howells 
650f071004SDavid Howells 	fc->sget_key = mtd;
660f071004SDavid Howells 	sb = sget_fc(fc, mtd_test_super, mtd_set_super);
670f071004SDavid Howells 	if (IS_ERR(sb))
680f071004SDavid Howells 		return PTR_ERR(sb);
690f071004SDavid Howells 
700f071004SDavid Howells 	if (sb->s_root) {
710f071004SDavid Howells 		/* new mountpoint for an already mounted superblock */
720f071004SDavid Howells 		pr_debug("MTDSB: Device %d (\"%s\") is already mounted\n",
730f071004SDavid Howells 			 mtd->index, mtd->name);
740f071004SDavid Howells 		put_mtd_device(mtd);
750f071004SDavid Howells 	} else {
760f071004SDavid Howells 		/* fresh new superblock */
770f071004SDavid Howells 		pr_debug("MTDSB: New superblock for device %d (\"%s\")\n",
780f071004SDavid Howells 			 mtd->index, mtd->name);
790f071004SDavid Howells 
800f071004SDavid Howells 		ret = fill_super(sb, fc);
810f071004SDavid Howells 		if (ret < 0)
820f071004SDavid Howells 			goto error_sb;
830f071004SDavid Howells 
840f071004SDavid Howells 		sb->s_flags |= SB_ACTIVE;
850f071004SDavid Howells 	}
860f071004SDavid Howells 
870f071004SDavid Howells 	BUG_ON(fc->root);
880f071004SDavid Howells 	fc->root = dget(sb->s_root);
890f071004SDavid Howells 	return 0;
900f071004SDavid Howells 
910f071004SDavid Howells error_sb:
920f071004SDavid Howells 	deactivate_locked_super(sb);
930f071004SDavid Howells 	return ret;
940f071004SDavid Howells }
950f071004SDavid Howells 
960f071004SDavid Howells /*
970f071004SDavid Howells  * get a superblock on an MTD-backed filesystem by MTD device number
980f071004SDavid Howells  */
990f071004SDavid Howells static int mtd_get_sb_by_nr(struct fs_context *fc, int mtdnr,
1000f071004SDavid Howells 			    int (*fill_super)(struct super_block *,
1010f071004SDavid Howells 					      struct fs_context *))
1020f071004SDavid Howells {
1030f071004SDavid Howells 	struct mtd_info *mtd;
1040f071004SDavid Howells 
1050f071004SDavid Howells 	mtd = get_mtd_device(NULL, mtdnr);
1060f071004SDavid Howells 	if (IS_ERR(mtd)) {
1070f071004SDavid Howells 		errorf(fc, "MTDSB: Device #%u doesn't appear to exist\n", mtdnr);
1080f071004SDavid Howells 		return PTR_ERR(mtd);
1090f071004SDavid Howells 	}
1100f071004SDavid Howells 
1110f071004SDavid Howells 	return mtd_get_sb(fc, mtd, fill_super);
1120f071004SDavid Howells }
1130f071004SDavid Howells 
1140f071004SDavid Howells /**
1150f071004SDavid Howells  * get_tree_mtd - Get a superblock based on a single MTD device
1160f071004SDavid Howells  * @fc: The filesystem context holding the parameters
1170f071004SDavid Howells  * @fill_super: Helper to initialise a new superblock
1180f071004SDavid Howells  */
1190f071004SDavid Howells int get_tree_mtd(struct fs_context *fc,
1200f071004SDavid Howells 	      int (*fill_super)(struct super_block *sb,
1210f071004SDavid Howells 				struct fs_context *fc))
1220f071004SDavid Howells {
1230f071004SDavid Howells #ifdef CONFIG_BLOCK
1244e7b5671SChristoph Hellwig 	dev_t dev;
1254e7b5671SChristoph Hellwig 	int ret;
1260f071004SDavid Howells #endif
1270f071004SDavid Howells 	int mtdnr;
1280f071004SDavid Howells 
1290f071004SDavid Howells 	if (!fc->source)
1300f071004SDavid Howells 		return invalf(fc, "No source specified");
1310f071004SDavid Howells 
1320f071004SDavid Howells 	pr_debug("MTDSB: dev_name \"%s\"\n", fc->source);
1330f071004SDavid Howells 
1340f071004SDavid Howells 	/* the preferred way of mounting in future; especially when
1350f071004SDavid Howells 	 * CONFIG_BLOCK=n - we specify the underlying MTD device by number or
1360f071004SDavid Howells 	 * by name, so that we don't require block device support to be present
1370f071004SDavid Howells 	 * in the kernel.
1380f071004SDavid Howells 	 */
1390f071004SDavid Howells 	if (fc->source[0] == 'm' &&
1400f071004SDavid Howells 	    fc->source[1] == 't' &&
1410f071004SDavid Howells 	    fc->source[2] == 'd') {
1420f071004SDavid Howells 		if (fc->source[3] == ':') {
1430f071004SDavid Howells 			struct mtd_info *mtd;
1440f071004SDavid Howells 
1450f071004SDavid Howells 			/* mount by MTD device name */
1460f071004SDavid Howells 			pr_debug("MTDSB: mtd:%%s, name \"%s\"\n",
1470f071004SDavid Howells 				 fc->source + 4);
1480f071004SDavid Howells 
1490f071004SDavid Howells 			mtd = get_mtd_device_nm(fc->source + 4);
1500f071004SDavid Howells 			if (!IS_ERR(mtd))
1510f071004SDavid Howells 				return mtd_get_sb(fc, mtd, fill_super);
1520f071004SDavid Howells 
1530f071004SDavid Howells 			errorf(fc, "MTD: MTD device with name \"%s\" not found",
1540f071004SDavid Howells 			       fc->source + 4);
1550f071004SDavid Howells 
1560f071004SDavid Howells 		} else if (isdigit(fc->source[3])) {
1570f071004SDavid Howells 			/* mount by MTD device number name */
1580f071004SDavid Howells 			char *endptr;
1590f071004SDavid Howells 
1600f071004SDavid Howells 			mtdnr = simple_strtoul(fc->source + 3, &endptr, 0);
1610f071004SDavid Howells 			if (!*endptr) {
1620f071004SDavid Howells 				/* It was a valid number */
1630f071004SDavid Howells 				pr_debug("MTDSB: mtd%%d, mtdnr %d\n", mtdnr);
1640f071004SDavid Howells 				return mtd_get_sb_by_nr(fc, mtdnr, fill_super);
1650f071004SDavid Howells 			}
1660f071004SDavid Howells 		}
1670f071004SDavid Howells 	}
1680f071004SDavid Howells 
1690f071004SDavid Howells #ifdef CONFIG_BLOCK
1700f071004SDavid Howells 	/* try the old way - the hack where we allowed users to mount
1710f071004SDavid Howells 	 * /dev/mtdblock$(n) but didn't actually _use_ the blockdev
1720f071004SDavid Howells 	 */
1734e7b5671SChristoph Hellwig 	ret = lookup_bdev(fc->source, &dev);
1744e7b5671SChristoph Hellwig 	if (ret) {
1750f071004SDavid Howells 		errorf(fc, "MTD: Couldn't look up '%s': %d", fc->source, ret);
1760f071004SDavid Howells 		return ret;
1770f071004SDavid Howells 	}
1780f071004SDavid Howells 	pr_debug("MTDSB: lookup_bdev() returned 0\n");
1790f071004SDavid Howells 
1804e7b5671SChristoph Hellwig 	if (MAJOR(dev) == MTD_BLOCK_MAJOR)
1814e7b5671SChristoph Hellwig 		return mtd_get_sb_by_nr(fc, MINOR(dev), fill_super);
1820f071004SDavid Howells 
1830f071004SDavid Howells #endif /* CONFIG_BLOCK */
1840f071004SDavid Howells 
1850f071004SDavid Howells 	if (!(fc->sb_flags & SB_SILENT))
1860f071004SDavid Howells 		errorf(fc, "MTD: Attempt to mount non-MTD device \"%s\"",
1870f071004SDavid Howells 		       fc->source);
1880f071004SDavid Howells 	return -EINVAL;
1890f071004SDavid Howells }
1900f071004SDavid Howells EXPORT_SYMBOL_GPL(get_tree_mtd);
191acaebfd8SDavid Howells 
192acaebfd8SDavid Howells /*
193acaebfd8SDavid Howells  * destroy an MTD-based superblock
194acaebfd8SDavid Howells  */
195acaebfd8SDavid Howells void kill_mtd_super(struct super_block *sb)
196acaebfd8SDavid Howells {
197acaebfd8SDavid Howells 	generic_shutdown_super(sb);
198acaebfd8SDavid Howells 	put_mtd_device(sb->s_mtd);
199acaebfd8SDavid Howells 	sb->s_mtd = NULL;
200acaebfd8SDavid Howells }
201acaebfd8SDavid Howells 
202acaebfd8SDavid Howells EXPORT_SYMBOL_GPL(kill_mtd_super);
203