xref: /openbmc/linux/drivers/mtd/mtdsuper.c (revision ec952aa2)
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>
18ccdf7741SChristoph Hellwig #include <linux/blkdev.h>
190f071004SDavid Howells #include <linux/fs_context.h>
200f071004SDavid Howells #include "mtdcore.h"
210f071004SDavid Howells 
220f071004SDavid Howells /*
230f071004SDavid Howells  * get a superblock on an MTD-backed filesystem
240f071004SDavid Howells  */
mtd_get_sb(struct fs_context * fc,struct mtd_info * mtd,int (* fill_super)(struct super_block *,struct fs_context *))250f071004SDavid Howells static int mtd_get_sb(struct fs_context *fc,
260f071004SDavid Howells 		      struct mtd_info *mtd,
270f071004SDavid Howells 		      int (*fill_super)(struct super_block *,
280f071004SDavid Howells 					struct fs_context *))
290f071004SDavid Howells {
300f071004SDavid Howells 	struct super_block *sb;
310f071004SDavid Howells 	int ret;
320f071004SDavid Howells 
33*ec952aa2SChristian Brauner 	sb = sget_dev(fc, MKDEV(MTD_BLOCK_MAJOR, mtd->index));
340f071004SDavid Howells 	if (IS_ERR(sb))
350f071004SDavid Howells 		return PTR_ERR(sb);
360f071004SDavid Howells 
370f071004SDavid Howells 	if (sb->s_root) {
380f071004SDavid Howells 		/* new mountpoint for an already mounted superblock */
390f071004SDavid Howells 		pr_debug("MTDSB: Device %d (\"%s\") is already mounted\n",
400f071004SDavid Howells 			 mtd->index, mtd->name);
410f071004SDavid Howells 		put_mtd_device(mtd);
420f071004SDavid Howells 	} else {
430f071004SDavid Howells 		/* fresh new superblock */
440f071004SDavid Howells 		pr_debug("MTDSB: New superblock for device %d (\"%s\")\n",
450f071004SDavid Howells 			 mtd->index, mtd->name);
460f071004SDavid Howells 
47*ec952aa2SChristian Brauner 		/*
48*ec952aa2SChristian Brauner 		 * Would usually have been set with @sb_lock held but in
49*ec952aa2SChristian Brauner 		 * contrast to sb->s_bdev that's checked with only
50*ec952aa2SChristian Brauner 		 * @sb_lock held, nothing checks sb->s_mtd without also
51*ec952aa2SChristian Brauner 		 * holding sb->s_umount and we're holding sb->s_umount
52*ec952aa2SChristian Brauner 		 * here.
53*ec952aa2SChristian Brauner 		 */
54*ec952aa2SChristian Brauner 		sb->s_mtd = mtd;
55*ec952aa2SChristian Brauner 		sb->s_bdi = bdi_get(mtd_bdi);
56*ec952aa2SChristian Brauner 
570f071004SDavid Howells 		ret = fill_super(sb, fc);
580f071004SDavid Howells 		if (ret < 0)
590f071004SDavid Howells 			goto error_sb;
600f071004SDavid Howells 
610f071004SDavid Howells 		sb->s_flags |= SB_ACTIVE;
620f071004SDavid Howells 	}
630f071004SDavid Howells 
640f071004SDavid Howells 	BUG_ON(fc->root);
650f071004SDavid Howells 	fc->root = dget(sb->s_root);
660f071004SDavid Howells 	return 0;
670f071004SDavid Howells 
680f071004SDavid Howells error_sb:
690f071004SDavid Howells 	deactivate_locked_super(sb);
700f071004SDavid Howells 	return ret;
710f071004SDavid Howells }
720f071004SDavid Howells 
730f071004SDavid Howells /*
740f071004SDavid Howells  * get a superblock on an MTD-backed filesystem by MTD device number
750f071004SDavid Howells  */
mtd_get_sb_by_nr(struct fs_context * fc,int mtdnr,int (* fill_super)(struct super_block *,struct fs_context *))760f071004SDavid Howells static int mtd_get_sb_by_nr(struct fs_context *fc, int mtdnr,
770f071004SDavid Howells 			    int (*fill_super)(struct super_block *,
780f071004SDavid Howells 					      struct fs_context *))
790f071004SDavid Howells {
800f071004SDavid Howells 	struct mtd_info *mtd;
810f071004SDavid Howells 
820f071004SDavid Howells 	mtd = get_mtd_device(NULL, mtdnr);
830f071004SDavid Howells 	if (IS_ERR(mtd)) {
840f071004SDavid Howells 		errorf(fc, "MTDSB: Device #%u doesn't appear to exist\n", mtdnr);
850f071004SDavid Howells 		return PTR_ERR(mtd);
860f071004SDavid Howells 	}
870f071004SDavid Howells 
880f071004SDavid Howells 	return mtd_get_sb(fc, mtd, fill_super);
890f071004SDavid Howells }
900f071004SDavid Howells 
910f071004SDavid Howells /**
920f071004SDavid Howells  * get_tree_mtd - Get a superblock based on a single MTD device
930f071004SDavid Howells  * @fc: The filesystem context holding the parameters
940f071004SDavid Howells  * @fill_super: Helper to initialise a new superblock
950f071004SDavid Howells  */
get_tree_mtd(struct fs_context * fc,int (* fill_super)(struct super_block * sb,struct fs_context * fc))960f071004SDavid Howells int get_tree_mtd(struct fs_context *fc,
970f071004SDavid Howells 	      int (*fill_super)(struct super_block *sb,
980f071004SDavid Howells 				struct fs_context *fc))
990f071004SDavid Howells {
1000f071004SDavid Howells #ifdef CONFIG_BLOCK
1014e7b5671SChristoph Hellwig 	dev_t dev;
1024e7b5671SChristoph Hellwig 	int ret;
1030f071004SDavid Howells #endif
1040f071004SDavid Howells 	int mtdnr;
1050f071004SDavid Howells 
1060f071004SDavid Howells 	if (!fc->source)
1070f071004SDavid Howells 		return invalf(fc, "No source specified");
1080f071004SDavid Howells 
1090f071004SDavid Howells 	pr_debug("MTDSB: dev_name \"%s\"\n", fc->source);
1100f071004SDavid Howells 
1110f071004SDavid Howells 	/* the preferred way of mounting in future; especially when
1120f071004SDavid Howells 	 * CONFIG_BLOCK=n - we specify the underlying MTD device by number or
1130f071004SDavid Howells 	 * by name, so that we don't require block device support to be present
1140f071004SDavid Howells 	 * in the kernel.
1150f071004SDavid Howells 	 */
1160f071004SDavid Howells 	if (fc->source[0] == 'm' &&
1170f071004SDavid Howells 	    fc->source[1] == 't' &&
1180f071004SDavid Howells 	    fc->source[2] == 'd') {
1190f071004SDavid Howells 		if (fc->source[3] == ':') {
1200f071004SDavid Howells 			struct mtd_info *mtd;
1210f071004SDavid Howells 
1220f071004SDavid Howells 			/* mount by MTD device name */
1230f071004SDavid Howells 			pr_debug("MTDSB: mtd:%%s, name \"%s\"\n",
1240f071004SDavid Howells 				 fc->source + 4);
1250f071004SDavid Howells 
1260f071004SDavid Howells 			mtd = get_mtd_device_nm(fc->source + 4);
1270f071004SDavid Howells 			if (!IS_ERR(mtd))
1280f071004SDavid Howells 				return mtd_get_sb(fc, mtd, fill_super);
1290f071004SDavid Howells 
1300f071004SDavid Howells 			errorf(fc, "MTD: MTD device with name \"%s\" not found",
1310f071004SDavid Howells 			       fc->source + 4);
1320f071004SDavid Howells 
1330f071004SDavid Howells 		} else if (isdigit(fc->source[3])) {
1340f071004SDavid Howells 			/* mount by MTD device number name */
1350f071004SDavid Howells 			char *endptr;
1360f071004SDavid Howells 
1370f071004SDavid Howells 			mtdnr = simple_strtoul(fc->source + 3, &endptr, 0);
1380f071004SDavid Howells 			if (!*endptr) {
1390f071004SDavid Howells 				/* It was a valid number */
1400f071004SDavid Howells 				pr_debug("MTDSB: mtd%%d, mtdnr %d\n", mtdnr);
1410f071004SDavid Howells 				return mtd_get_sb_by_nr(fc, mtdnr, fill_super);
1420f071004SDavid Howells 			}
1430f071004SDavid Howells 		}
1440f071004SDavid Howells 	}
1450f071004SDavid Howells 
1460f071004SDavid Howells #ifdef CONFIG_BLOCK
1470f071004SDavid Howells 	/* try the old way - the hack where we allowed users to mount
1480f071004SDavid Howells 	 * /dev/mtdblock$(n) but didn't actually _use_ the blockdev
1490f071004SDavid Howells 	 */
1504e7b5671SChristoph Hellwig 	ret = lookup_bdev(fc->source, &dev);
1514e7b5671SChristoph Hellwig 	if (ret) {
1520f071004SDavid Howells 		errorf(fc, "MTD: Couldn't look up '%s': %d", fc->source, ret);
1530f071004SDavid Howells 		return ret;
1540f071004SDavid Howells 	}
1550f071004SDavid Howells 	pr_debug("MTDSB: lookup_bdev() returned 0\n");
1560f071004SDavid Howells 
1574e7b5671SChristoph Hellwig 	if (MAJOR(dev) == MTD_BLOCK_MAJOR)
1584e7b5671SChristoph Hellwig 		return mtd_get_sb_by_nr(fc, MINOR(dev), fill_super);
1590f071004SDavid Howells 
1600f071004SDavid Howells #endif /* CONFIG_BLOCK */
1610f071004SDavid Howells 
1620f071004SDavid Howells 	if (!(fc->sb_flags & SB_SILENT))
1630f071004SDavid Howells 		errorf(fc, "MTD: Attempt to mount non-MTD device \"%s\"",
1640f071004SDavid Howells 		       fc->source);
1650f071004SDavid Howells 	return -EINVAL;
1660f071004SDavid Howells }
1670f071004SDavid Howells EXPORT_SYMBOL_GPL(get_tree_mtd);
168acaebfd8SDavid Howells 
169acaebfd8SDavid Howells /*
170acaebfd8SDavid Howells  * destroy an MTD-based superblock
171acaebfd8SDavid Howells  */
kill_mtd_super(struct super_block * sb)172acaebfd8SDavid Howells void kill_mtd_super(struct super_block *sb)
173acaebfd8SDavid Howells {
174acaebfd8SDavid Howells 	generic_shutdown_super(sb);
175acaebfd8SDavid Howells 	put_mtd_device(sb->s_mtd);
176acaebfd8SDavid Howells 	sb->s_mtd = NULL;
177acaebfd8SDavid Howells }
178acaebfd8SDavid Howells 
179acaebfd8SDavid Howells EXPORT_SYMBOL_GPL(kill_mtd_super);
180