1 // SPDX-License-Identifier: GPL-2.0
3 * Disk events - monitor disk events like media change and eject request.
5 #include <linux/export.h>
6 #include <linux/moduleparam.h>
7 #include <linux/genhd.h>
11 struct list_head node; /* all disk_event's */
12 struct gendisk *disk; /* the associated disk */
15 struct mutex block_mutex; /* protects blocking */
16 int block; /* event blocking depth */
17 unsigned int pending; /* events already sent out */
18 unsigned int clearing; /* events being cleared */
20 long poll_msecs; /* interval, -1 for default */
21 struct delayed_work dwork;
24 static const char *disk_events_strs[] = {
25 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "media_change",
26 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "eject_request",
29 static char *disk_uevents[] = {
30 [ilog2(DISK_EVENT_MEDIA_CHANGE)] = "DISK_MEDIA_CHANGE=1",
31 [ilog2(DISK_EVENT_EJECT_REQUEST)] = "DISK_EJECT_REQUEST=1",
34 /* list of all disk_events */
35 static DEFINE_MUTEX(disk_events_mutex);
36 static LIST_HEAD(disk_events);
38 /* disable in-kernel polling by default */
39 static unsigned long disk_events_dfl_poll_msecs;
41 static unsigned long disk_events_poll_jiffies(struct gendisk *disk)
43 struct disk_events *ev = disk->ev;
47 * If device-specific poll interval is set, always use it. If
48 * the default is being used, poll if the POLL flag is set.
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;
55 return msecs_to_jiffies(intv_msecs);
59 * disk_block_events - block and flush disk event checking
60 * @disk: disk to block events for
62 * On return from this function, it is guaranteed that event checking
63 * isn't in progress and won't happen until unblocked by
64 * disk_unblock_events(). Events blocking is counted and the actual
65 * unblocking happens after the matching number of unblocks are done.
67 * Note that this intentionally does not block event checking from
68 * disk_clear_events().
73 void disk_block_events(struct gendisk *disk)
75 struct disk_events *ev = disk->ev;
83 * Outer mutex ensures that the first blocker completes canceling
84 * the event work before further blockers are allowed to finish.
86 mutex_lock(&ev->block_mutex);
88 spin_lock_irqsave(&ev->lock, flags);
89 cancel = !ev->block++;
90 spin_unlock_irqrestore(&ev->lock, flags);
93 cancel_delayed_work_sync(&disk->ev->dwork);
95 mutex_unlock(&ev->block_mutex);
98 static void __disk_unblock_events(struct gendisk *disk, bool check_now)
100 struct disk_events *ev = disk->ev;
104 spin_lock_irqsave(&ev->lock, flags);
106 if (WARN_ON_ONCE(ev->block <= 0))
112 intv = disk_events_poll_jiffies(disk);
114 queue_delayed_work(system_freezable_power_efficient_wq,
117 queue_delayed_work(system_freezable_power_efficient_wq,
120 spin_unlock_irqrestore(&ev->lock, flags);
124 * disk_unblock_events - unblock disk event checking
125 * @disk: disk to unblock events for
127 * Undo disk_block_events(). When the block count reaches zero, it
128 * starts events polling if configured.
131 * Don't care. Safe to call from irq context.
133 void disk_unblock_events(struct gendisk *disk)
136 __disk_unblock_events(disk, false);
140 * disk_flush_events - schedule immediate event checking and flushing
141 * @disk: disk to check and flush events for
142 * @mask: events to flush
144 * Schedule immediate event checking on @disk if not blocked. Events in
145 * @mask are scheduled to be cleared from the driver. Note that this
146 * doesn't clear the events from @disk->ev.
149 * If @mask is non-zero must be called with disk->open_mutex held.
151 void disk_flush_events(struct gendisk *disk, unsigned int mask)
153 struct disk_events *ev = disk->ev;
158 spin_lock_irq(&ev->lock);
159 ev->clearing |= mask;
161 mod_delayed_work(system_freezable_power_efficient_wq,
163 spin_unlock_irq(&ev->lock);
166 static void disk_check_events(struct disk_events *ev,
167 unsigned int *clearing_ptr)
169 struct gendisk *disk = ev->disk;
170 char *envp[ARRAY_SIZE(disk_uevents) + 1] = { };
171 unsigned int clearing = *clearing_ptr;
174 int nr_events = 0, i;
177 events = disk->fops->check_events(disk, clearing);
179 /* accumulate pending events and schedule next poll if necessary */
180 spin_lock_irq(&ev->lock);
182 events &= ~ev->pending;
183 ev->pending |= events;
184 *clearing_ptr &= ~clearing;
186 intv = disk_events_poll_jiffies(disk);
187 if (!ev->block && intv)
188 queue_delayed_work(system_freezable_power_efficient_wq,
191 spin_unlock_irq(&ev->lock);
194 * Tell userland about new events. Only the events listed in
195 * @disk->events are reported, and only if DISK_EVENT_FLAG_UEVENT
196 * is set. Otherwise, events are processed internally but never
197 * get reported to userland.
199 for (i = 0; i < ARRAY_SIZE(disk_uevents); i++)
200 if ((events & disk->events & (1 << i)) &&
201 (disk->event_flags & DISK_EVENT_FLAG_UEVENT))
202 envp[nr_events++] = disk_uevents[i];
205 kobject_uevent_env(&disk_to_dev(disk)->kobj, KOBJ_CHANGE, envp);
209 * disk_clear_events - synchronously check, clear and return pending events
210 * @disk: disk to fetch and clear events from
211 * @mask: mask of events to be fetched and cleared
213 * Disk events are synchronously checked and pending events in @mask
214 * are cleared and returned. This ignores the block count.
219 static unsigned int disk_clear_events(struct gendisk *disk, unsigned int mask)
221 struct disk_events *ev = disk->ev;
222 unsigned int pending;
223 unsigned int clearing = mask;
228 disk_block_events(disk);
231 * store the union of mask and ev->clearing on the stack so that the
232 * race with disk_flush_events does not cause ambiguity (ev->clearing
233 * can still be modified even if events are blocked).
235 spin_lock_irq(&ev->lock);
236 clearing |= ev->clearing;
238 spin_unlock_irq(&ev->lock);
240 disk_check_events(ev, &clearing);
242 * if ev->clearing is not 0, the disk_flush_events got called in the
243 * middle of this function, so we want to run the workfn without delay.
245 __disk_unblock_events(disk, ev->clearing ? true : false);
247 /* then, fetch and clear pending events */
248 spin_lock_irq(&ev->lock);
249 pending = ev->pending & mask;
250 ev->pending &= ~mask;
251 spin_unlock_irq(&ev->lock);
252 WARN_ON_ONCE(clearing & mask);
258 * bdev_check_media_change - check if a removable media has been changed
259 * @bdev: block device to check
261 * Check whether a removable media has been changed, and attempt to free all
262 * dentries and inodes and invalidates all block device page cache entries in
265 * Returns %true if the block device changed, or %false if not.
267 bool bdev_check_media_change(struct block_device *bdev)
271 events = disk_clear_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE |
272 DISK_EVENT_EJECT_REQUEST);
273 if (!(events & DISK_EVENT_MEDIA_CHANGE))
276 if (__invalidate_device(bdev, true))
277 pr_warn("VFS: busy inodes on changed media %s\n",
278 bdev->bd_disk->disk_name);
279 set_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
282 EXPORT_SYMBOL(bdev_check_media_change);
285 * Separate this part out so that a different pointer for clearing_ptr can be
286 * passed in for disk_clear_events.
288 static void disk_events_workfn(struct work_struct *work)
290 struct delayed_work *dwork = to_delayed_work(work);
291 struct disk_events *ev = container_of(dwork, struct disk_events, dwork);
293 disk_check_events(ev, &ev->clearing);
297 * A disk events enabled device has the following sysfs nodes under
298 * its /sys/block/X/ directory.
300 * events : list of all supported events
301 * events_async : list of events which can be detected w/o polling
302 * (always empty, only for backwards compatibility)
303 * events_poll_msecs : polling interval, 0: disable, -1: system default
305 static ssize_t __disk_events_show(unsigned int events, char *buf)
307 const char *delim = "";
311 for (i = 0; i < ARRAY_SIZE(disk_events_strs); i++)
312 if (events & (1 << i)) {
313 pos += sprintf(buf + pos, "%s%s",
314 delim, disk_events_strs[i]);
318 pos += sprintf(buf + pos, "\n");
322 static ssize_t disk_events_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
325 struct gendisk *disk = dev_to_disk(dev);
327 if (!(disk->event_flags & DISK_EVENT_FLAG_UEVENT))
329 return __disk_events_show(disk->events, buf);
332 static ssize_t disk_events_async_show(struct device *dev,
333 struct device_attribute *attr, char *buf)
338 static ssize_t disk_events_poll_msecs_show(struct device *dev,
339 struct device_attribute *attr,
342 struct gendisk *disk = dev_to_disk(dev);
345 return sprintf(buf, "-1\n");
346 return sprintf(buf, "%ld\n", disk->ev->poll_msecs);
349 static ssize_t disk_events_poll_msecs_store(struct device *dev,
350 struct device_attribute *attr,
351 const char *buf, size_t count)
353 struct gendisk *disk = dev_to_disk(dev);
356 if (!count || !sscanf(buf, "%ld", &intv))
359 if (intv < 0 && intv != -1)
365 disk_block_events(disk);
366 disk->ev->poll_msecs = intv;
367 __disk_unblock_events(disk, true);
371 DEVICE_ATTR(events, 0444, disk_events_show, NULL);
372 DEVICE_ATTR(events_async, 0444, disk_events_async_show, NULL);
373 DEVICE_ATTR(events_poll_msecs, 0644, disk_events_poll_msecs_show,
374 disk_events_poll_msecs_store);
377 * The default polling interval can be specified by the kernel
378 * parameter block.events_dfl_poll_msecs which defaults to 0
379 * (disable). This can also be modified runtime by writing to
380 * /sys/module/block/parameters/events_dfl_poll_msecs.
382 static int disk_events_set_dfl_poll_msecs(const char *val,
383 const struct kernel_param *kp)
385 struct disk_events *ev;
388 ret = param_set_ulong(val, kp);
392 mutex_lock(&disk_events_mutex);
393 list_for_each_entry(ev, &disk_events, node)
394 disk_flush_events(ev->disk, 0);
395 mutex_unlock(&disk_events_mutex);
399 static const struct kernel_param_ops disk_events_dfl_poll_msecs_param_ops = {
400 .set = disk_events_set_dfl_poll_msecs,
401 .get = param_get_ulong,
404 #undef MODULE_PARAM_PREFIX
405 #define MODULE_PARAM_PREFIX "block."
407 module_param_cb(events_dfl_poll_msecs, &disk_events_dfl_poll_msecs_param_ops,
408 &disk_events_dfl_poll_msecs, 0644);
411 * disk_{alloc|add|del|release}_events - initialize and destroy disk_events.
413 void disk_alloc_events(struct gendisk *disk)
415 struct disk_events *ev;
417 if (!disk->fops->check_events || !disk->events)
420 ev = kzalloc(sizeof(*ev), GFP_KERNEL);
422 pr_warn("%s: failed to initialize events\n", disk->disk_name);
426 INIT_LIST_HEAD(&ev->node);
428 spin_lock_init(&ev->lock);
429 mutex_init(&ev->block_mutex);
432 INIT_DELAYED_WORK(&ev->dwork, disk_events_workfn);
437 void disk_add_events(struct gendisk *disk)
442 mutex_lock(&disk_events_mutex);
443 list_add_tail(&disk->ev->node, &disk_events);
444 mutex_unlock(&disk_events_mutex);
447 * Block count is initialized to 1 and the following initial
448 * unblock kicks it into action.
450 __disk_unblock_events(disk, true);
453 void disk_del_events(struct gendisk *disk)
456 disk_block_events(disk);
458 mutex_lock(&disk_events_mutex);
459 list_del_init(&disk->ev->node);
460 mutex_unlock(&disk_events_mutex);
464 void disk_release_events(struct gendisk *disk)
466 /* the block count should be 1 from disk_del_events() */
467 WARN_ON_ONCE(disk->ev && disk->ev->block != 1);