From patchwork Wed Jan 22 08:28:43 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fabian Frederick X-Patchwork-Id: 313301 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:770:15f::2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id EF8D72C009B for ; Thu, 23 Jan 2014 03:28:43 +1100 (EST) Received: from merlin.infradead.org ([2001:4978:20e::2]) by casper.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1W60eq-0000Vv-33; Wed, 22 Jan 2014 16:28:04 +0000 Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1W60eo-0003y9-II; Wed, 22 Jan 2014 16:28:02 +0000 Received: from mailrelay007.isp.belgacom.be ([195.238.6.173]) by merlin.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1W60el-0003ws-Sz for linux-mtd@lists.infradead.org; Wed, 22 Jan 2014 16:28:01 +0000 X-Belgacom-Dynamic: yes X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgYhACfx31JXQzVI/2dsb2JhbABbKIJjOIMCpBuVEwICgRMXdIJTExwjGIECN4gJAcMBjQGBex2EIgSJRY5cAYpOh0qBb4FLLw Received: from 72.53-67-87.adsl-dyn.isp.belgacom.be (HELO localhost) ([87.67.53.72]) by relay.skynet.be with SMTP; 22 Jan 2014 17:27:32 +0100 Date: Wed, 22 Jan 2014 16:28:43 +0800 From: Fabian Frederick To: linux-mtd@lists.infradead.org Subject: [PATCH 1/1 RESEND] mtd: block2mtd: char mtd major and erasesize parameter check + mutex_destroy Message-Id: <20140122162843.15c3fb1e1318346339fcbff4@skynet.be> X-Mailer: Sylpheed 3.3.0 (GTK+ 2.24.10; i686-pc-linux-gnu) Mime-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20140122_112800_041641_722CB8E3 X-CRM114-Status: GOOD ( 11.30 ) X-Spam-Score: -0.4 (/) X-Spam-Report: SpamAssassin version 3.3.2 on merlin.infradead.org summary: Content analysis details: (-0.4 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at http://www.dnswl.org/, no trust [195.238.6.173 listed in list.dnswl.org] 1.5 DATE_IN_PAST_06_12 Date: is 6 to 12 hours before Received: date -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] Cc: akpm@linux-foundation.org, rdunlap@infradead.org, joern@lazybastard.org, linux-kernel@vger.kernel.org X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: Linux MTD discussion mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-mtd" Errors-To: linux-mtd-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org -Deny use of a char mtd device to map as block device. -mutex_init when mtd structure is available. -fixme applied : check device size is a multiple of erasesize. -mutex_destroy on each device in block2mtd_exit and add_device failure. Signed-off-by: Fabian Frederick --- drivers/mtd/devices/block2mtd.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/drivers/mtd/devices/block2mtd.c b/drivers/mtd/devices/block2mtd.c index d9fd87a..be67731 100644 --- a/drivers/mtd/devices/block2mtd.c +++ b/drivers/mtd/devices/block2mtd.c @@ -209,7 +209,6 @@ static void block2mtd_free_device(struct block2mtd_dev *dev) } -/* FIXME: ensure that mtd->size % erase_size == 0 */ static struct block2mtd_dev *add_device(char *devname, int erase_size) { const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL; @@ -244,21 +243,27 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size) } dev->blkdev = bdev; - if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) { + if ((MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) || + (MAJOR(bdev->bd_dev) == MTD_CHAR_MAJOR)) { pr_err("attempting to use an MTD device as a block device\n"); goto devinit_err; } - mutex_init(&dev->write_mutex); /* Setup the MTD structure */ /* make the name contain the block device in */ name = kasprintf(GFP_KERNEL, "block2mtd: %s", devname); + if (!name) goto devinit_err; + + if ((long)dev->blkdev->bd_inode->i_size % erase_size) { + pr_err("erasesize must be a divisor of device size\n"); + goto devinit_err; + } + mutex_init(&dev->write_mutex); dev->mtd.name = name; - dev->mtd.size = dev->blkdev->bd_inode->i_size & PAGE_MASK; dev->mtd.erasesize = erase_size; dev->mtd.writesize = 1; @@ -274,7 +279,7 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size) if (mtd_device_register(&dev->mtd, NULL, 0)) { /* Device didn't get added, so free the entry */ - goto devinit_err; + goto register_err; } list_add(&dev->list, &blkmtd_device_list); pr_info("mtd%d: [%s] erase_size = %dKiB [%d]\n", @@ -283,6 +288,8 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size) dev->mtd.erasesize >> 10, dev->mtd.erasesize); return dev; +register_err: + mutex_destroy(&dev->write_mutex); devinit_err: block2mtd_free_device(dev); return NULL; @@ -448,6 +455,7 @@ static void block2mtd_exit(void) struct block2mtd_dev *dev = list_entry(pos, typeof(*dev), list); block2mtd_sync(&dev->mtd); mtd_device_unregister(&dev->mtd); + mutex_destroy(&dev->write_mutex); pr_info("mtd%d: [%s] removed\n", dev->mtd.index, dev->mtd.name + strlen("block2mtd: "));