1
2
3
4
5#include <linux/export.h>
6#include <linux/moduleparam.h>
7#include <linux/blkdev.h>
8#include "blk.h"
9
10struct disk_events {
11 struct list_head node;
12 struct gendisk *disk;
13 spinlock_t lock;
14
15 struct mutex block_mutex;
16 int block;
17 unsigned int pending;
18 unsigned int clearing;
19
20 long poll_msecs;
21 struct delayed_work dwork;
22};
23
24static const char *disk_events_strs[] = {
25 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
26 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
27};
28
29static char *disk_uevents[] = {
30 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
31 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
32};
33
34
35static DEFINE_MUTEX(disk_events_mutex);
36static LIST_HEAD(disk_events);
37
38
39static unsigned long disk_events_dfl_poll_msecs;
40
41static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
42{
43 struct disk_events *ev = disk->ev;
44 long intv_msecs = 0;
45
46
47
48
49
50 if (ev->poll_msecs >= 0)
51 intv_msecs = ev->poll_msecs;
52 else if (disk->event_flags & DISK_EVENT_FLAG_POLL)
53 intv_msecs = disk_events_dfl_poll_msecs;
54
55 return msecs_to_jiffies(intv_msecs);
56}
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73void disk_block_events(struct gendisk *disk)
74{
75 struct disk_events *ev = disk->ev;
76 unsigned long flags;
77 bool cancel;
78
79 if (!ev)
80 return;
81
82
83
84
85
86 mutex_lock(&ev->block_mutex);
87
88 spin_lock_irqsave(&ev->lock, flags);
89 cancel = !ev->block++;
90 spin_unlock_irqrestore(&ev->lock, flags);
91
92 if (cancel)
93 cancel_delayed_work_sync(&disk->ev->dwork);
94
95 mutex_unlock(&ev->block_mutex);
96}
97
98static void __disk_unblock_events(struct gendisk *disk, bool check_now)
99{
100 struct disk_events *ev = disk->ev;
101 unsigned long intv;
102 unsigned long flags;
103
104 spin_lock_irqsave(&ev->lock, flags);
105
106 if (WARN_ON_ONCE(ev->block <= 0))
107 goto out_unlock;
108
109 if (--ev->block)
110 goto out_unlock;
111
112 intv = disk_events_poll_jiffies(disk);
113 if (check_now)
114 queue_delayed_work(system_freezable_power_efficient_wq,
115 &ev->dwork, 0);
116 else if (intv)
117 queue_delayed_work(system_freezable_power_efficient_wq,
118 &ev->dwork, intv);
119out_unlock:
120 spin_unlock_irqrestore(&ev->lock, flags);
121}
122
123
124
125
126
127
128
129
130
131
132
133void disk_unblock_events(struct gendisk *disk)
134{
135 if (disk->ev)
136 __disk_unblock_events(disk, false);
137}
138
139
140
141
142
143
144
145
146
147
148
149
150
151void disk_flush_events(struct gendisk *disk, unsigned int mask)
152{
153 struct disk_events *ev = disk->ev;
154
155 if (!ev)
156 return;
157
158 spin_lock_irq(&ev->lock);
159 ev->clearing |= mask;
160 if (!ev->block)
161 mod_delayed_work(system_freezable_power_efficient_wq,
162 &ev->dwork, 0);
163 spin_unlock_irq(&ev->lock);
164}
165
166
167
168
169
170
171static void disk_event_uevent(struct gendisk *disk, unsigned int events)
172{
173 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
174 int nr_events = 0, i;
175
176 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
177 if (events & disk->events & (1 << i))
178 envp[nr_events++] = disk_uevents[i];
179
180 if (nr_events)
181 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
182}
183
184static void disk_check_events(struct disk_events *ev,
185 unsigned int *clearing_ptr)
186{
187 struct gendisk *disk = ev->disk;
188 unsigned int clearing = *clearing_ptr;
189 unsigned int events;
190 unsigned long intv;
191
192
193 events = disk->fops->check_events(disk, clearing);
194
195
196 spin_lock_irq(&ev->lock);
197
198 events &= ~ev->pending;
199 ev->pending |= events;
200 *clearing_ptr &= ~clearing;
201
202 intv = disk_events_poll_jiffies(disk);
203 if (!ev->block && intv)
204 queue_delayed_work(system_freezable_power_efficient_wq,
205 &ev->dwork, intv);
206
207 spin_unlock_irq(&ev->lock);
208
209 if (events & DISK_EVENT_MEDIA_CHANGE)
210 inc_diskseq(disk);
211
212 if (disk->event_flags & DISK_EVENT_FLAG_UEVENT)
213 disk_event_uevent(disk, events);
214}
215
216
217
218
219
220
221
222
223
224
225
226
227static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
228{
229 struct disk_events *ev = disk->ev;
230 unsigned int pending;
231 unsigned int clearing = mask;
232
233 if (!ev)
234 return 0;
235
236 disk_block_events(disk);
237
238
239
240
241
242
243 spin_lock_irq(&ev->lock);
244 clearing |= ev->clearing;
245 ev->clearing = 0;
246 spin_unlock_irq(&ev->lock);
247
248 disk_check_events(ev, &clearing);
249
250
251
252
253 __disk_unblock_events(disk, ev->clearing ? true : false);
254
255
256 spin_lock_irq(&ev->lock);
257 pending = ev->pending & mask;
258 ev->pending &= ~mask;
259 spin_unlock_irq(&ev->lock);
260 WARN_ON_ONCE(clearing & mask);
261
262 return pending;
263}
264
265
266
267
268
269
270
271
272bool disk_check_media_change(struct gendisk *disk)
273{
274 unsigned int events;
275
276 events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
277 DISK_EVENT_EJECT_REQUEST);
278 if (events & DISK_EVENT_MEDIA_CHANGE) {
279 set_bit(GD_NEED_PART_SCAN, &disk->state);
280 return true;
281 }
282 return false;
283}
284EXPORT_SYMBOL(disk_check_media_change);
285
286
287
288
289
290
291
292
293
294void disk_force_media_change(struct gendisk *disk)
295{
296 disk_event_uevent(disk, DISK_EVENT_MEDIA_CHANGE);
297 inc_diskseq(disk);
298 bdev_mark_dead(disk->part0, true);
299 set_bit(GD_NEED_PART_SCAN, &disk->state);
300}
301EXPORT_SYMBOL_GPL(disk_force_media_change);
302
303
304
305
306
307static void disk_events_workfn(struct work_struct *work)
308{
309 struct delayed_work *dwork = to_delayed_work(work);
310 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
311
312 disk_check_events(ev, &ev->clearing);
313}
314
315
316
317
318
319
320
321
322
323
324static ssize_t __disk_events_show(unsigned int events, char *buf)
325{
326 const char *delim = "";
327 ssize_t pos = 0;
328 int i;
329
330 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
331 if (events & (1 << i)) {
332 pos += sprintf(buf + pos, "%s%s",
333 delim, disk_events_strs[i]);
334 delim = " ";
335 }
336 if (pos)
337 pos += sprintf(buf + pos, "\n");
338 return pos;
339}
340
341static ssize_t disk_events_show(struct device *dev,
342 struct device_attribute *attr, char *buf)
343{
344 struct gendisk *disk = dev_to_disk(dev);
345
346 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
347 return 0;
348 return __disk_events_show(disk->events, buf);
349}
350
351static ssize_t disk_events_async_show(struct device *dev,
352 struct device_attribute *attr, char *buf)
353{
354 return 0;
355}
356
357static ssize_t disk_events_poll_msecs_show(struct device *dev,
358 struct device_attribute *attr,
359 char *buf)
360{
361 struct gendisk *disk = dev_to_disk(dev);
362
363 if (!disk->ev)
364 return sprintf(buf, "-1\n");
365 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
366}
367
368static ssize_t disk_events_poll_msecs_store(struct device *dev,
369 struct device_attribute *attr,
370 const char *buf, size_t count)
371{
372 struct gendisk *disk = dev_to_disk(dev);
373 long intv;
374
375 if (!count || !sscanf(buf, "%ld", &intv))
376 return -EINVAL;
377
378 if (intv < 0 && intv != -1)
379 return -EINVAL;
380
381 if (!disk->ev)
382 return -ENODEV;
383
384 disk_block_events(disk);
385 disk->ev->poll_msecs = intv;
386 __disk_unblock_events(disk, true);
387 return count;
388}
389
390DEVICE_ATTR(events, 0444, disk_events_show, NULL);
391DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
392DEVICE_ATTR(events_poll_msecs, 0644, disk_events_poll_msecs_show,
393 disk_events_poll_msecs_store);
394
395
396
397
398
399
400
401static int disk_events_set_dfl_poll_msecs(const char *val,
402 const struct kernel_param *kp)
403{
404 struct disk_events *ev;
405 int ret;
406
407 ret = param_set_ulong(val, kp);
408 if (ret < 0)
409 return ret;
410
411 mutex_lock(&disk_events_mutex);
412 list_for_each_entry(ev, &disk_events, node)
413 disk_flush_events(ev->disk, 0);
414 mutex_unlock(&disk_events_mutex);
415 return 0;
416}
417
418static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
419 .set = disk_events_set_dfl_poll_msecs,
420 .get = param_get_ulong,
421};
422
423#undef MODULE_PARAM_PREFIX
424#define MODULE_PARAM_PREFIX "block."
425
426module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
427 &disk_events_dfl_poll_msecs, 0644);
428
429
430
431
432int disk_alloc_events(struct gendisk *disk)
433{
434 struct disk_events *ev;
435
436 if (!disk->fops->check_events || !disk->events)
437 return 0;
438
439 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
440 if (!ev) {
441 pr_warn("%s: failed to initialize events\n", disk->disk_name);
442 return -ENOMEM;
443 }
444
445 INIT_LIST_HEAD(&ev->node);
446 ev->disk = disk;
447 spin_lock_init(&ev->lock);
448 mutex_init(&ev->block_mutex);
449 ev->block = 1;
450 ev->poll_msecs = -1;
451 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
452
453 disk->ev = ev;
454 return 0;
455}
456
457void disk_add_events(struct gendisk *disk)
458{
459 if (!disk->ev)
460 return;
461
462 mutex_lock(&disk_events_mutex);
463 list_add_tail(&disk->ev->node, &disk_events);
464 mutex_unlock(&disk_events_mutex);
465
466
467
468
469
470 __disk_unblock_events(disk, true);
471}
472
473void disk_del_events(struct gendisk *disk)
474{
475 if (disk->ev) {
476 disk_block_events(disk);
477
478 mutex_lock(&disk_events_mutex);
479 list_del_init(&disk->ev->node);
480 mutex_unlock(&disk_events_mutex);
481 }
482}
483
484void disk_release_events(struct gendisk *disk)
485{
486
487 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);
488 kfree(disk->ev);
489}
490