1 From 2bba1cdcfcd2907d0696cc0139f1bd078d36ee81 Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Tue, 19 Dec 2023 02:32:35 +0000
4 Subject: [PATCH 3/8] mtd: ubi: block: use notifier to create ubiblock from
7 Use UBI_VOLUME_ADDED notification to create ubiblock device specified
8 on kernel cmdline or module parameter.
9 This makes thing more simple and has the advantage that ubiblock devices
10 on volumes which are not present at the time the ubi module is probed
11 will still be created.
13 Suggested-by: Zhihao Cheng <chengzhihao1@huawei.com>
14 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
15 Signed-off-by: Richard Weinberger <richard@nod.at>
17 drivers/mtd/ubi/block.c | 136 ++++++++++++++++++++--------------------
18 drivers/mtd/ubi/kapi.c | 54 +++++++++++-----
19 drivers/mtd/ubi/ubi.h | 1 +
20 3 files changed, 106 insertions(+), 85 deletions(-)
22 --- a/drivers/mtd/ubi/block.c
23 +++ b/drivers/mtd/ubi/block.c
24 @@ -65,10 +65,10 @@ struct ubiblock_pdu {
27 /* Numbers of elements set in the @ubiblock_param array */
28 -static int ubiblock_devs __initdata;
29 +static int ubiblock_devs;
31 /* MTD devices specification parameters */
32 -static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES] __initdata;
33 +static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES];
36 struct ubi_volume_desc *desc;
37 @@ -532,6 +532,70 @@ static int ubiblock_resize(struct ubi_vo
42 +match_volume_desc(struct ubi_volume_info *vi, const char *name, int ubi_num, int vol_id)
44 + int err, len, cur_ubi_num, cur_vol_id;
46 + if (ubi_num == -1) {
47 + /* No ubi num, name must be a vol device path */
48 + err = ubi_get_num_by_path(name, &cur_ubi_num, &cur_vol_id);
49 + if (err || vi->ubi_num != cur_ubi_num || vi->vol_id != cur_vol_id)
56 + /* Got ubi_num, but no vol_id, name must be volume name */
57 + if (vi->ubi_num != ubi_num)
60 + len = strnlen(name, UBI_VOL_NAME_MAX + 1);
61 + if (len < 1 || vi->name_len != len)
64 + if (strcmp(name, vi->name))
70 + if (vi->ubi_num != ubi_num)
73 + if (vi->vol_id != vol_id)
80 +ubiblock_create_from_param(struct ubi_volume_info *vi)
83 + struct ubiblock_param *p;
86 + * Iterate over ubiblock cmdline parameters. If a parameter matches the
87 + * newly added volume create the ubiblock device for it.
89 + for (i = 0; i < ubiblock_devs; i++) {
90 + p = &ubiblock_param[i];
92 + if (!match_volume_desc(vi, p->name, p->ubi_num, p->vol_id))
95 + ret = ubiblock_create(vi);
98 + "UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n",
99 + vi->name, p->ubi_num, p->vol_id, ret);
105 static int ubiblock_notify(struct notifier_block *nb,
106 unsigned long notification_type, void *ns_ptr)
108 @@ -539,10 +603,7 @@ static int ubiblock_notify(struct notifi
110 switch (notification_type) {
111 case UBI_VOLUME_ADDED:
113 - * We want to enforce explicit block device creation for
114 - * volumes, so when a volume is added we do nothing.
116 + ubiblock_create_from_param(&nt->vi);
118 case UBI_VOLUME_REMOVED:
119 ubiblock_remove(&nt->vi);
120 @@ -568,56 +629,6 @@ static struct notifier_block ubiblock_no
121 .notifier_call = ubiblock_notify,
124 -static struct ubi_volume_desc * __init
125 -open_volume_desc(const char *name, int ubi_num, int vol_id)
128 - /* No ubi num, name must be a vol device path */
129 - return ubi_open_volume_path(name, UBI_READONLY);
130 - else if (vol_id == -1)
131 - /* No vol_id, must be vol_name */
132 - return ubi_open_volume_nm(ubi_num, name, UBI_READONLY);
134 - return ubi_open_volume(ubi_num, vol_id, UBI_READONLY);
137 -static void __init ubiblock_create_from_param(void)
140 - struct ubiblock_param *p;
141 - struct ubi_volume_desc *desc;
142 - struct ubi_volume_info vi;
145 - * If there is an error creating one of the ubiblocks, continue on to
146 - * create the following ubiblocks. This helps in a circumstance where
147 - * the kernel command-line specifies multiple block devices and some
148 - * may be broken, but we still want the working ones to come up.
150 - for (i = 0; i < ubiblock_devs; i++) {
151 - p = &ubiblock_param[i];
153 - desc = open_volume_desc(p->name, p->ubi_num, p->vol_id);
154 - if (IS_ERR(desc)) {
156 - "UBI: block: can't open volume on ubi%d_%d, err=%ld\n",
157 - p->ubi_num, p->vol_id, PTR_ERR(desc));
161 - ubi_get_volume_info(desc, &vi);
162 - ubi_close_volume(desc);
164 - ret = ubiblock_create(&vi);
167 - "UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n",
168 - vi.name, p->ubi_num, p->vol_id, ret);
174 static void ubiblock_remove_all(void)
176 struct ubiblock *next;
177 @@ -643,18 +654,7 @@ int __init ubiblock_init(void)
178 if (ubiblock_major < 0)
179 return ubiblock_major;
182 - * Attach block devices from 'block=' module param.
183 - * Even if one block device in the param list fails to come up,
184 - * still allow the module to load and leave any others up.
186 - ubiblock_create_from_param();
189 - * Block devices are only created upon user requests, so we ignore
190 - * existing volumes.
192 - ret = ubi_register_volume_notifier(&ubiblock_notifier, 1);
193 + ret = ubi_register_volume_notifier(&ubiblock_notifier, 0);
197 --- a/drivers/mtd/ubi/kapi.c
198 +++ b/drivers/mtd/ubi/kapi.c
199 @@ -280,6 +280,41 @@ struct ubi_volume_desc *ubi_open_volume_
200 EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
203 + * ubi_get_num_by_path - get UBI device and volume number from device path
204 + * @pathname: volume character device node path
205 + * @ubi_num: pointer to UBI device number to be set
206 + * @vol_id: pointer to UBI volume ID to be set
208 + * Returns 0 on success and sets ubi_num and vol_id, returns error otherwise.
210 +int ubi_get_num_by_path(const char *pathname, int *ubi_num, int *vol_id)
216 + error = kern_path(pathname, LOOKUP_FOLLOW, &path);
220 + error = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT);
225 + if (!S_ISCHR(stat.mode))
228 + *ubi_num = ubi_major2num(MAJOR(stat.rdev));
229 + *vol_id = MINOR(stat.rdev) - 1;
231 + if (*vol_id < 0 || *ubi_num < 0)
238 * ubi_open_volume_path - open UBI volume by its character device node path.
239 * @pathname: volume character device node path
241 @@ -290,32 +325,17 @@ EXPORT_SYMBOL_GPL(ubi_open_volume_nm);
242 struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode)
244 int error, ubi_num, vol_id;
248 dbg_gen("open volume %s, mode %d", pathname, mode);
250 if (!pathname || !*pathname)
251 return ERR_PTR(-EINVAL);
253 - error = kern_path(pathname, LOOKUP_FOLLOW, &path);
255 - return ERR_PTR(error);
257 - error = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT);
259 + error = ubi_get_num_by_path(pathname, &ubi_num, &vol_id);
261 return ERR_PTR(error);
263 - if (!S_ISCHR(stat.mode))
264 - return ERR_PTR(-EINVAL);
266 - ubi_num = ubi_major2num(MAJOR(stat.rdev));
267 - vol_id = MINOR(stat.rdev) - 1;
269 - if (vol_id >= 0 && ubi_num >= 0)
270 - return ubi_open_volume(ubi_num, vol_id, mode);
271 - return ERR_PTR(-ENODEV);
272 + return ubi_open_volume(ubi_num, vol_id, mode);
274 EXPORT_SYMBOL_GPL(ubi_open_volume_path);
276 --- a/drivers/mtd/ubi/ubi.h
277 +++ b/drivers/mtd/ubi/ubi.h
278 @@ -956,6 +956,7 @@ void ubi_free_internal_volumes(struct ub
279 void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di);
280 void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
281 struct ubi_volume_info *vi);
282 +int ubi_get_num_by_path(const char *pathname, int *ubi_num, int *vol_id);
284 int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
285 int pnum, const struct ubi_vid_hdr *vid_hdr);