From nobody Fri Dec 19 04:15:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E2785C7EE22 for ; Tue, 2 May 2023 16:48:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234318AbjEBQsS (ORCPT ); Tue, 2 May 2023 12:48:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36392 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233478AbjEBQsP (ORCPT ); Tue, 2 May 2023 12:48:15 -0400 Received: from fudo.makrotopia.org (fudo.makrotopia.org [IPv6:2a07:2ec0:3002::71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 132442118 for ; Tue, 2 May 2023 09:48:14 -0700 (PDT) Received: from local by fudo.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1pttAi-0000UW-1L; Tue, 02 May 2023 18:48:12 +0200 Date: Tue, 2 May 2023 17:48:07 +0100 From: Daniel Golle To: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Zhihao Cheng Subject: [PATCH 1/4] mtd: ubi: block: don't return on error when removing Message-ID: <4bca8ffa66fa094da37625e66f3a2681058531d6.1683043928.git.daniel@makrotopia.org> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" There is no point on returning the error from ubiblock_remove in case it is being called due to a volume removal event -- the volume is gone, we should destroy and remove the ubiblock device no matter what. Introduce a new boolean parameter 'force' to tell ubiblock_remove to go on even in case the ubiblock device is still busy. Use that new option when calling ubiblock_remove due to a UBI_VOLUME_REMOVED event. Signed-off-by: Daniel Golle --- drivers/mtd/ubi/block.c | 6 +++--- drivers/mtd/ubi/cdev.c | 2 +- drivers/mtd/ubi/ubi.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c index 3711d7f746003..6f5804f4b8f55 100644 --- a/drivers/mtd/ubi/block.c +++ b/drivers/mtd/ubi/block.c @@ -457,7 +457,7 @@ static void ubiblock_cleanup(struct ubiblock *dev) idr_remove(&ubiblock_minor_idr, dev->gd->first_minor); } =20 -int ubiblock_remove(struct ubi_volume_info *vi) +int ubiblock_remove(struct ubi_volume_info *vi, bool force) { struct ubiblock *dev; int ret; @@ -471,7 +471,7 @@ int ubiblock_remove(struct ubi_volume_info *vi) =20 /* Found a device, let's lock it so we can check if it's busy */ mutex_lock(&dev->dev_mutex); - if (dev->refcnt > 0) { + if (dev->refcnt > 0 && !force) { ret =3D -EBUSY; goto out_unlock_dev; } @@ -546,7 +546,7 @@ static int ubiblock_notify(struct notifier_block *nb, */ break; case UBI_VOLUME_REMOVED: - ubiblock_remove(&nt->vi); + ubiblock_remove(&nt->vi, true); break; case UBI_VOLUME_RESIZED: ubiblock_resize(&nt->vi); diff --git a/drivers/mtd/ubi/cdev.c b/drivers/mtd/ubi/cdev.c index f43430b9c1e65..bb55e863dd296 100644 --- a/drivers/mtd/ubi/cdev.c +++ b/drivers/mtd/ubi/cdev.c @@ -572,7 +572,7 @@ static long vol_cdev_ioctl(struct file *file, unsigned = int cmd, struct ubi_volume_info vi; =20 ubi_get_volume_info(desc, &vi); - err =3D ubiblock_remove(&vi); + err =3D ubiblock_remove(&vi, false); break; } =20 diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h index c8f1bd4fa1008..44c0eeaf1e1b0 100644 --- a/drivers/mtd/ubi/ubi.h +++ b/drivers/mtd/ubi/ubi.h @@ -979,7 +979,7 @@ static inline void ubi_fastmap_destroy_checkmap(struct = ubi_volume *vol) {} int ubiblock_init(void); void ubiblock_exit(void); int ubiblock_create(struct ubi_volume_info *vi); -int ubiblock_remove(struct ubi_volume_info *vi); +int ubiblock_remove(struct ubi_volume_info *vi, bool force); #else static inline int ubiblock_init(void) { return 0; } static inline void ubiblock_exit(void) {} @@ -987,7 +987,7 @@ static inline int ubiblock_create(struct ubi_volume_inf= o *vi) { return -ENOSYS; } -static inline int ubiblock_remove(struct ubi_volume_info *vi) +static inline int ubiblock_remove(struct ubi_volume_info *vi, bool force) { return -ENOSYS; } --=20 2.40.1 From nobody Fri Dec 19 04:15:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D1462C77B73 for ; Tue, 2 May 2023 16:48:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234146AbjEBQsg (ORCPT ); Tue, 2 May 2023 12:48:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233608AbjEBQse (ORCPT ); Tue, 2 May 2023 12:48:34 -0400 Received: from fudo.makrotopia.org (fudo.makrotopia.org [IPv6:2a07:2ec0:3002::71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76597212E for ; Tue, 2 May 2023 09:48:30 -0700 (PDT) Received: from local by fudo.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1pttAy-0000Us-1h; Tue, 02 May 2023 18:48:28 +0200 Date: Tue, 2 May 2023 17:48:23 +0100 From: Daniel Golle To: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Zhihao Cheng Subject: [PATCH 2/4] mtd: ubi: block: use notifier to create ubiblock from parameter Message-ID: <726b7629d0819092edeb193f3bfc13c99bd13339.1683043928.git.daniel@makrotopia.org> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use UBI_VOLUME_ADDED notification to create ubiblock device specified on kernel cmdline or module parameter. This makes things more simple and has the advantage that ubiblock devices on volumes which are not present at the time the ubi module is probed will still be created. Suggested-by: Zhihao Cheng Signed-off-by: Daniel Golle --- drivers/mtd/ubi/block.c | 150 ++++++++++++++++++++++------------------ 1 file changed, 82 insertions(+), 68 deletions(-) diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c index 6f5804f4b8f55..7f3024bddf306 100644 --- a/drivers/mtd/ubi/block.c +++ b/drivers/mtd/ubi/block.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -65,10 +66,10 @@ struct ubiblock_pdu { }; =20 /* Numbers of elements set in the @ubiblock_param array */ -static int ubiblock_devs __initdata; +static int ubiblock_devs; =20 /* MTD devices specification parameters */ -static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES] __initda= ta; +static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES]; =20 struct ubiblock { struct ubi_volume_desc *desc; @@ -533,6 +534,83 @@ static int ubiblock_resize(struct ubi_volume_info *vi) return 0; } =20 +static bool +match_volume_desc(struct ubi_volume_info *vi, const char *name, int ubi_nu= m, int vol_id) +{ + int err, len; + struct path path; + struct kstat stat; + + if (ubi_num =3D=3D -1) { + /* No ubi num, name must be a vol device path */ + err =3D kern_path(name, LOOKUP_FOLLOW, &path); + if (err) + return false; + + err =3D vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT); + path_put(&path); + if (err) + return false; + + if (!S_ISCHR(stat.mode)) + return false; + + if (vi->ubi_num !=3D ubi_major2num(MAJOR(stat.rdev))) + return false; + + if (vi->vol_id !=3D MINOR(stat.rdev) - 1) + return false; + + return true; + } else if (vol_id =3D=3D -1) { + if (vi->ubi_num !=3D ubi_num) + return false; + + len =3D strnlen(name, UBI_VOL_NAME_MAX + 1); + if (len < 1 || vi->name_len !=3D len) + return false; + + if (strcmp(name, vi->name)) + return false; + + return true; + } else { + if (vi->ubi_num !=3D ubi_num) + return false; + + if (vi->vol_id !=3D vol_id) + return false; + + return true; + } +} + +static void +ubiblock_create_from_param(struct ubi_volume_info *vi) +{ + int i, ret =3D 0; + struct ubiblock_param *p; + + /* + * Iterate over ubiblock cmdline parameters. If a parameter matches the + * newly added volume create the ubiblock device for it. + */ + for (i =3D 0; i < ubiblock_devs; i++) { + p =3D &ubiblock_param[i]; + + if (!match_volume_desc(vi, p->name, p->ubi_num, p->vol_id)) + continue; + + ret =3D ubiblock_create(vi); + if (ret) { + pr_err( + "UBI: block: can't add '%s' volume on ubi%d_%d, err=3D%d\n", + vi->name, p->ubi_num, p->vol_id, ret); + } + break; + } +} + static int ubiblock_notify(struct notifier_block *nb, unsigned long notification_type, void *ns_ptr) { @@ -540,10 +618,7 @@ static int ubiblock_notify(struct notifier_block *nb, =20 switch (notification_type) { case UBI_VOLUME_ADDED: - /* - * We want to enforce explicit block device creation for - * volumes, so when a volume is added we do nothing. - */ + ubiblock_create_from_param(&nt->vi); break; case UBI_VOLUME_REMOVED: ubiblock_remove(&nt->vi, true); @@ -569,56 +644,6 @@ static struct notifier_block ubiblock_notifier =3D { .notifier_call =3D ubiblock_notify, }; =20 -static struct ubi_volume_desc * __init -open_volume_desc(const char *name, int ubi_num, int vol_id) -{ - if (ubi_num =3D=3D -1) - /* No ubi num, name must be a vol device path */ - return ubi_open_volume_path(name, UBI_READONLY); - else if (vol_id =3D=3D -1) - /* No vol_id, must be vol_name */ - return ubi_open_volume_nm(ubi_num, name, UBI_READONLY); - else - return ubi_open_volume(ubi_num, vol_id, UBI_READONLY); -} - -static void __init ubiblock_create_from_param(void) -{ - int i, ret =3D 0; - struct ubiblock_param *p; - struct ubi_volume_desc *desc; - struct ubi_volume_info vi; - - /* - * If there is an error creating one of the ubiblocks, continue on to - * create the following ubiblocks. This helps in a circumstance where - * the kernel command-line specifies multiple block devices and some - * may be broken, but we still want the working ones to come up. - */ - for (i =3D 0; i < ubiblock_devs; i++) { - p =3D &ubiblock_param[i]; - - desc =3D open_volume_desc(p->name, p->ubi_num, p->vol_id); - if (IS_ERR(desc)) { - pr_err( - "UBI: block: can't open volume on ubi%d_%d, err=3D%ld\n", - p->ubi_num, p->vol_id, PTR_ERR(desc)); - continue; - } - - ubi_get_volume_info(desc, &vi); - ubi_close_volume(desc); - - ret =3D ubiblock_create(&vi); - if (ret) { - pr_err( - "UBI: block: can't add '%s' volume on ubi%d_%d, err=3D%d\n", - vi.name, p->ubi_num, p->vol_id, ret); - continue; - } - } -} - static void ubiblock_remove_all(void) { struct ubiblock *next; @@ -644,18 +669,7 @@ int __init ubiblock_init(void) if (ubiblock_major < 0) return ubiblock_major; =20 - /* - * Attach block devices from 'block=3D' module param. - * Even if one block device in the param list fails to come up, - * still allow the module to load and leave any others up. - */ - ubiblock_create_from_param(); - - /* - * Block devices are only created upon user requests, so we ignore - * existing volumes. - */ - ret =3D ubi_register_volume_notifier(&ubiblock_notifier, 1); + ret =3D ubi_register_volume_notifier(&ubiblock_notifier, 0); if (ret) goto err_unreg; return 0; --=20 2.40.1 From nobody Fri Dec 19 04:15:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 19D0BC7EE22 for ; Tue, 2 May 2023 16:49:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234346AbjEBQtJ (ORCPT ); Tue, 2 May 2023 12:49:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37296 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234144AbjEBQtC (ORCPT ); Tue, 2 May 2023 12:49:02 -0400 Received: from fudo.makrotopia.org (fudo.makrotopia.org [IPv6:2a07:2ec0:3002::71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B2D1E3C14; Tue, 2 May 2023 09:48:48 -0700 (PDT) Received: from local by fudo.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1pttBH-0000VJ-06; Tue, 02 May 2023 18:48:47 +0200 Date: Tue, 2 May 2023 17:48:39 +0100 From: Daniel Golle To: linux-mtd@lists.infradead.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Rob Herring , Krzysztof Kozlowski , Zhihao Cheng , =?utf-8?B?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH 3/4] dt-bindings: mtd: partitions: add linux,ubi compatible Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add bindings for MTD partitions to be attached as UBI devices. Signed-off-by: Daniel Golle --- .../bindings/mtd/partitions/ubi.yaml | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Documentation/devicetree/bindings/mtd/partitions/ubi.ya= ml diff --git a/Documentation/devicetree/bindings/mtd/partitions/ubi.yaml b/Do= cumentation/devicetree/bindings/mtd/partitions/ubi.yaml new file mode 100644 index 0000000000000..aa02fbbd50716 --- /dev/null +++ b/Documentation/devicetree/bindings/mtd/partitions/ubi.yaml @@ -0,0 +1,49 @@ +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/mtd/partitions/ubi.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Unsorted Block Images (UBI) + +description: | + Unsorted Block Images (UBI) is a volume management system typically + used on NAND flash providing bad block management as well as + wear-leveling. + Any partition containing the compatible "linux,ubi" will be attached + as UBI device. + +maintainers: + - Daniel Golle + +allOf: + - $ref: /schemas/mtd/partitions/partition.yaml# + +properties: + compatible: + const: linux,ubi + +required: + - compatible + +unevaluatedProperties: false + +examples: + - | + partitions { + compatible =3D "fixed-partitions"; + #address-cells =3D <1>; + #size-cells =3D <1>; + + partition@0 { + label =3D "bootloader"; + reg =3D <0x000000 0x100000>; + read-only; + }; + + ubi@100000 { + compatible =3D "linux,ubi"; + label =3D "ubi"; + reg =3D <0x100000 0x7f00000>; + }; + }; --=20 2.40.1 From nobody Fri Dec 19 04:15:08 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 129F0C77B7E for ; Tue, 2 May 2023 16:49:26 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234322AbjEBQtN (ORCPT ); Tue, 2 May 2023 12:49:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234338AbjEBQtI (ORCPT ); Tue, 2 May 2023 12:49:08 -0400 Received: from fudo.makrotopia.org (fudo.makrotopia.org [IPv6:2a07:2ec0:3002::71]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9B13A1BF6 for ; Tue, 2 May 2023 09:49:04 -0700 (PDT) Received: from local by fudo.makrotopia.org with esmtpsa (TLS1.3:TLS_AES_256_GCM_SHA384:256) (Exim 4.96) (envelope-from ) id 1pttBW-0000Vf-34; Tue, 02 May 2023 18:49:03 +0200 Date: Tue, 2 May 2023 17:48:57 +0100 From: Daniel Golle To: linux-mtd@lists.infradead.org, linux-kernel@vger.kernel.org, Miquel Raynal , Richard Weinberger , Vignesh Raghavendra , Zhihao Cheng , =?utf-8?B?UmFmYcWCIE1pxYJlY2tp?= Subject: [PATCH 4/4] mtd: ubi: attach MTD partition from device-tree Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Split ubi_init() function into early function to be called by device_initcall() and keep cmdline attachment in late_initcall(). (when building ubi as module, both is still done in a single module_init() call) Register MTD notifier and attach MTD devices which are marked as compatible with 'linux,ubi' in OF device-tree when being added, detach UBI device from MTD device when it is being removed. For existing users this should not change anything besides automatic removal of (dead) UBI devices when their underlying MTD devices are already gone, e.g. in case of MTD driver module or (SPI) bus driver module being removed. For new users this opens up the option to attach UBI using device-tree which then happens early and in parallel with other drivers being probed which slightly reduces the total boot time. Attachment no longer happening late is also a requirement for other drivers to make use of UBI, e.g. drivers/nvmem/u-boot-env.c can now be extended to support U-Boot environment stored in UBI volumes. Signed-off-by: Daniel Golle --- drivers/mtd/ubi/block.c | 2 +- drivers/mtd/ubi/build.c | 189 ++++++++++++++++++++++++++++------------ drivers/mtd/ubi/cdev.c | 2 +- drivers/mtd/ubi/ubi.h | 2 +- 4 files changed, 134 insertions(+), 61 deletions(-) diff --git a/drivers/mtd/ubi/block.c b/drivers/mtd/ubi/block.c index 7f3024bddf306..8754ed86eac3a 100644 --- a/drivers/mtd/ubi/block.c +++ b/drivers/mtd/ubi/block.c @@ -471,7 +471,7 @@ int ubiblock_remove(struct ubi_volume_info *vi, bool fo= rce) } =20 /* Found a device, let's lock it so we can check if it's busy */ - mutex_lock(&dev->dev_mutex); + mutex_lock_nested(&dev->dev_mutex, SINGLE_DEPTH_NESTING); if (dev->refcnt > 0 && !force) { ret =3D -EBUSY; goto out_unlock_dev; diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c index bb1a90bbc8ffb..32422dcf1bf48 100644 --- a/drivers/mtd/ubi/build.c +++ b/drivers/mtd/ubi/build.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include "ubi.h" @@ -1066,6 +1067,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_= num, * ubi_detach_mtd_dev - detach an MTD device. * @ubi_num: UBI device number to detach from * @anyway: detach MTD even if device reference count is not zero + * @have_lock: called by MTD notifier holding mtd_table_mutex * * This function destroys an UBI device number @ubi_num and detaches the * underlying MTD device. Returns zero in case of success and %-EBUSY if t= he @@ -1075,7 +1077,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_= num, * Note, the invocations of this function has to be serialized by the * @ubi_devices_mutex. */ -int ubi_detach_mtd_dev(int ubi_num, int anyway) +int ubi_detach_mtd_dev(int ubi_num, int anyway, bool have_lock) { struct ubi_device *ubi; =20 @@ -1131,7 +1136,11 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway) vfree(ubi->peb_buf); vfree(ubi->fm_buf); ubi_msg(ubi, "mtd%d is detached", ubi->mtd->index); - put_mtd_device(ubi->mtd); + if (have_lock) + __put_mtd_device(ubi->mtd); + else + put_mtd_device(ubi->mtd); + put_device(&ubi->dev); return 0; } @@ -1208,43 +1217,51 @@ static struct mtd_info * __init open_mtd_device(con= st char *mtd_dev) return mtd; } =20 -static int __init ubi_init(void) +static void ubi_notify_add(struct mtd_info *mtd) { - int err, i, k; + struct device_node *np =3D mtd_get_of_node(mtd); + int err; =20 - /* Ensure that EC and VID headers have correct size */ - BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) !=3D 64); - BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) !=3D 64); + if (!of_device_is_compatible(np, "linux,ubi")) + return; =20 - if (mtd_devs > UBI_MAX_DEVICES) { - pr_err("UBI error: too many MTD devices, maximum is %d\n", - UBI_MAX_DEVICES); - return -EINVAL; - } + /* + * we are already holding &mtd_table_mutex, but still need + * to bump refcount + */ + err =3D __get_mtd_device(mtd); + if (err) + return; =20 - /* Create base sysfs directory and sysfs files */ - err =3D class_register(&ubi_class); + /* called while holding mtd_table_mutex */ + mutex_lock_nested(&ubi_devices_mutex, SINGLE_DEPTH_NESTING); + err =3D ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0, false); + mutex_unlock(&ubi_devices_mutex); if (err < 0) - return err; + __put_mtd_device(mtd); +} =20 - err =3D misc_register(&ubi_ctrl_cdev); - if (err) { - pr_err("UBI error: cannot register device\n"); - goto out; - } +static void ubi_notify_remove(struct mtd_info *mtd) +{ + int i; =20 - ubi_wl_entry_slab =3D kmem_cache_create("ubi_wl_entry_slab", - sizeof(struct ubi_wl_entry), - 0, 0, NULL); - if (!ubi_wl_entry_slab) { - err =3D -ENOMEM; - goto out_dev_unreg; - } + /* called while holding mtd_table_mutex */ + mutex_lock_nested(&ubi_devices_mutex, SINGLE_DEPTH_NESTING); + for (i =3D 0; i < UBI_MAX_DEVICES; i++) + if (ubi_devices[i] && + ubi_devices[i]->mtd->index =3D=3D mtd->index) + ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1, true); + mutex_unlock(&ubi_devices_mutex); +} =20 - err =3D ubi_debugfs_init(); - if (err) - goto out_slab; +static struct mtd_notifier ubi_mtd_notifier =3D { + .add =3D ubi_notify_add, + .remove =3D ubi_notify_remove, +}; =20 +static int __init ubi_init_attach(void) +{ + int err, i, k; =20 /* Attach MTD devices */ for (i =3D 0; i < mtd_devs; i++) { @@ -1292,25 +1309,79 @@ static int __init ubi_init(void) } } =20 + return 0; + +out_detach: + for (k =3D 0; k < i; k++) + if (ubi_devices[k]) { + mutex_lock(&ubi_devices_mutex); + ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1, false); + mutex_unlock(&ubi_devices_mutex); + } + return err; +} +#ifndef CONFIG_MTD_UBI_MODULE +late_initcall(ubi_init_attach); +#endif + +static int __init ubi_init(void) +{ + int err; + + /* Ensure that EC and VID headers have correct size */ + BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) !=3D 64); + BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) !=3D 64); + + if (mtd_devs > UBI_MAX_DEVICES) { + pr_err("UBI error: too many MTD devices, maximum is %d\n", + UBI_MAX_DEVICES); + return -EINVAL; + } + + /* Create base sysfs directory and sysfs files */ + err =3D class_register(&ubi_class); + if (err < 0) + return err; + + err =3D misc_register(&ubi_ctrl_cdev); + if (err) { + pr_err("UBI error: cannot register device\n"); + goto out; + } + + ubi_wl_entry_slab =3D kmem_cache_create("ubi_wl_entry_slab", + sizeof(struct ubi_wl_entry), + 0, 0, NULL); + if (!ubi_wl_entry_slab) { + err =3D -ENOMEM; + goto out_dev_unreg; + } + + err =3D ubi_debugfs_init(); + if (err) + goto out_slab; + err =3D ubiblock_init(); if (err) { pr_err("UBI error: block: cannot initialize, error %d\n", err); =20 /* See comment above re-ubi_is_module(). */ if (ubi_is_module()) - goto out_detach; + goto out_slab; + } + + register_mtd_user(&ubi_mtd_notifier); + + if (ubi_is_module()) { + err =3D ubi_init_attach(); + if (err) + goto out_mtd_notifier; } =20 return 0; =20 -out_detach: - for (k =3D 0; k < i; k++) - if (ubi_devices[k]) { - mutex_lock(&ubi_devices_mutex); - ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1); - mutex_unlock(&ubi_devices_mutex); - } - ubi_debugfs_exit(); +out_mtd_notifier: + unregister_mtd_user(&ubi_mtd_notifier); out_slab: kmem_cache_destroy(ubi_wl_entry_slab); out_dev_unreg: @@ -1320,18 +1391,20 @@ static int __init ubi_init(void) pr_err("UBI error: cannot initialize UBI, error %d\n", err); return err; } -late_initcall(ubi_init); +device_initcall(ubi_init); + =20 static void __exit ubi_exit(void) { int i; =20 ubiblock_exit(); + unregister_mtd_user(&ubi_mtd_notifier); =20 for (i =3D 0; i < UBI_MAX_DEVICES; i++) if (ubi_devices[i]) { mutex_lock(&ubi_devices_mutex); - ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1); + ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1, false); mutex_unlock(&ubi_devices_mutex); } ubi_debugfs_exit(); diff --git a/drivers/mtd/ubi/cdev.c b/drivers/mtd/ubi/cdev.c index bb55e863dd296..0ba6aa6a2e11d 100644 --- a/drivers/mtd/ubi/cdev.c +++ b/drivers/mtd/ubi/cdev.c @@ -1065,7 +1065,7 @@ static long ctrl_cdev_ioctl(struct file *file, unsign= ed int cmd, } =20 mutex_lock(&ubi_devices_mutex); - err =3D ubi_detach_mtd_dev(ubi_num, 0); + err =3D ubi_detach_mtd_dev(ubi_num, 0, false); mutex_unlock(&ubi_devices_mutex); break; } diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h index 44c0eeaf1e1b0..54093858f3385 100644 --- a/drivers/mtd/ubi/ubi.h +++ b/drivers/mtd/ubi/ubi.h @@ -939,7 +939,7 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pn= um, int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset, int max_beb_per1024, bool disable_fm); -int ubi_detach_mtd_dev(int ubi_num, int anyway); +int ubi_detach_mtd_dev(int ubi_num, int anyway, bool have_lock); struct ubi_device *ubi_get_device(int ubi_num); void ubi_put_device(struct ubi_device *ubi); struct ubi_device *ubi_get_by_major(int major); --=20 2.40.1