421daa012580230c9df2dd7c0115f10e35bd1e7e
[openwrt/staging/981213.git] / target / linux / generic / pending-6.6 / 450-03-mtd-ubi-block-use-notifier-to-create-ubiblock-from-p.patch
1 From e5cf19bd8204925f3bd2067df9e867313eac388b Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Mon, 1 May 2023 11:57:51 +0100
4 Subject: [PATCH 03/15] mtd: ubi: block: use notifier to create ubiblock from
5 parameter
6
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.
12
13 Suggested-by: Zhihao Cheng <chengzhihao1@huawei.com>
14 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
15 ---
16 drivers/mtd/ubi/block.c | 154 ++++++++++++++++++++++------------------
17 1 file changed, 85 insertions(+), 69 deletions(-)
18
19 --- a/drivers/mtd/ubi/block.c
20 +++ b/drivers/mtd/ubi/block.c
21 @@ -33,6 +33,7 @@
22 #include <linux/kernel.h>
23 #include <linux/list.h>
24 #include <linux/mutex.h>
25 +#include <linux/namei.h>
26 #include <linux/slab.h>
27 #include <linux/mtd/ubi.h>
28 #include <linux/blkdev.h>
29 @@ -67,10 +68,10 @@ struct ubiblock_pdu {
30 };
31
32 /* Numbers of elements set in the @ubiblock_param array */
33 -static int ubiblock_devs __initdata;
34 +static int ubiblock_devs;
35
36 /* MTD devices specification parameters */
37 -static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES] __initdata;
38 +static struct ubiblock_param ubiblock_param[UBIBLOCK_MAX_DEVICES];
39
40 struct ubiblock {
41 struct ubi_volume_desc *desc;
42 @@ -504,7 +505,7 @@ int ubiblock_remove(struct ubi_volume_in
43 }
44
45 /* Found a device, let's lock it so we can check if it's busy */
46 - mutex_lock(&dev->dev_mutex);
47 + mutex_lock_nested(&dev->dev_mutex, SINGLE_DEPTH_NESTING);
48 if (dev->refcnt > 0) {
49 ret = -EBUSY;
50 goto out_unlock_dev;
51 @@ -567,6 +568,85 @@ static int ubiblock_resize(struct ubi_vo
52 return 0;
53 }
54
55 +static bool
56 +match_volume_desc(struct ubi_volume_info *vi, const char *name, int ubi_num, int vol_id)
57 +{
58 + int err, len;
59 + struct path path;
60 + struct kstat stat;
61 +
62 + if (ubi_num == -1) {
63 + /* No ubi num, name must be a vol device path */
64 + err = kern_path(name, LOOKUP_FOLLOW, &path);
65 + if (err)
66 + return false;
67 +
68 + err = vfs_getattr(&path, &stat, STATX_TYPE, AT_STATX_SYNC_AS_STAT);
69 + path_put(&path);
70 + if (err)
71 + return false;
72 +
73 + if (!S_ISCHR(stat.mode))
74 + return false;
75 +
76 + if (vi->ubi_num != ubi_major2num(MAJOR(stat.rdev)))
77 + return false;
78 +
79 + if (vi->vol_id != MINOR(stat.rdev) - 1)
80 + return false;
81 +
82 + return true;
83 + }
84 +
85 + if (vol_id == -1) {
86 + if (vi->ubi_num != ubi_num)
87 + return false;
88 +
89 + len = strnlen(name, UBI_VOL_NAME_MAX + 1);
90 + if (len < 1 || vi->name_len != len)
91 + return false;
92 +
93 + if (strcmp(name, vi->name))
94 + return false;
95 +
96 + return true;
97 + }
98 +
99 + if (vi->ubi_num != ubi_num)
100 + return false;
101 +
102 + if (vi->vol_id != vol_id)
103 + return false;
104 +
105 + return true;
106 +}
107 +
108 +static void
109 +ubiblock_create_from_param(struct ubi_volume_info *vi)
110 +{
111 + int i, ret = 0;
112 + struct ubiblock_param *p;
113 +
114 + /*
115 + * Iterate over ubiblock cmdline parameters. If a parameter matches the
116 + * newly added volume create the ubiblock device for it.
117 + */
118 + for (i = 0; i < ubiblock_devs; i++) {
119 + p = &ubiblock_param[i];
120 +
121 + if (!match_volume_desc(vi, p->name, p->ubi_num, p->vol_id))
122 + continue;
123 +
124 + ret = ubiblock_create(vi);
125 + if (ret) {
126 + pr_err(
127 + "UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n",
128 + vi->name, p->ubi_num, p->vol_id, ret);
129 + }
130 + break;
131 + }
132 +}
133 +
134 static int ubiblock_notify(struct notifier_block *nb,
135 unsigned long notification_type, void *ns_ptr)
136 {
137 @@ -574,10 +654,7 @@ static int ubiblock_notify(struct notifi
138
139 switch (notification_type) {
140 case UBI_VOLUME_ADDED:
141 - /*
142 - * We want to enforce explicit block device creation for
143 - * volumes, so when a volume is added we do nothing.
144 - */
145 + ubiblock_create_from_param(&nt->vi);
146 break;
147 case UBI_VOLUME_REMOVED:
148 ubiblock_remove(&nt->vi);
149 @@ -603,56 +680,6 @@ static struct notifier_block ubiblock_no
150 .notifier_call = ubiblock_notify,
151 };
152
153 -static struct ubi_volume_desc * __init
154 -open_volume_desc(const char *name, int ubi_num, int vol_id)
155 -{
156 - if (ubi_num == -1)
157 - /* No ubi num, name must be a vol device path */
158 - return ubi_open_volume_path(name, UBI_READONLY);
159 - else if (vol_id == -1)
160 - /* No vol_id, must be vol_name */
161 - return ubi_open_volume_nm(ubi_num, name, UBI_READONLY);
162 - else
163 - return ubi_open_volume(ubi_num, vol_id, UBI_READONLY);
164 -}
165 -
166 -static void __init ubiblock_create_from_param(void)
167 -{
168 - int i, ret = 0;
169 - struct ubiblock_param *p;
170 - struct ubi_volume_desc *desc;
171 - struct ubi_volume_info vi;
172 -
173 - /*
174 - * If there is an error creating one of the ubiblocks, continue on to
175 - * create the following ubiblocks. This helps in a circumstance where
176 - * the kernel command-line specifies multiple block devices and some
177 - * may be broken, but we still want the working ones to come up.
178 - */
179 - for (i = 0; i < ubiblock_devs; i++) {
180 - p = &ubiblock_param[i];
181 -
182 - desc = open_volume_desc(p->name, p->ubi_num, p->vol_id);
183 - if (IS_ERR(desc)) {
184 - pr_err(
185 - "UBI: block: can't open volume on ubi%d_%d, err=%ld\n",
186 - p->ubi_num, p->vol_id, PTR_ERR(desc));
187 - continue;
188 - }
189 -
190 - ubi_get_volume_info(desc, &vi);
191 - ubi_close_volume(desc);
192 -
193 - ret = ubiblock_create(&vi);
194 - if (ret) {
195 - pr_err(
196 - "UBI: block: can't add '%s' volume on ubi%d_%d, err=%d\n",
197 - vi.name, p->ubi_num, p->vol_id, ret);
198 - continue;
199 - }
200 - }
201 -}
202 -
203 static void ubiblock_remove_all(void)
204 {
205 struct ubiblock *next;
206 @@ -678,18 +705,7 @@ int __init ubiblock_init(void)
207 if (ubiblock_major < 0)
208 return ubiblock_major;
209
210 - /*
211 - * Attach block devices from 'block=' module param.
212 - * Even if one block device in the param list fails to come up,
213 - * still allow the module to load and leave any others up.
214 - */
215 - ubiblock_create_from_param();
216 -
217 - /*
218 - * Block devices are only created upon user requests, so we ignore
219 - * existing volumes.
220 - */
221 - ret = ubi_register_volume_notifier(&ubiblock_notifier, 1);
222 + ret = ubi_register_volume_notifier(&ubiblock_notifier, 0);
223 if (ret)
224 goto err_unreg;
225 return 0;