forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathledtrig-blkdev.c
1225 lines (1057 loc) · 34.3 KB
/
ledtrig-blkdev.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0-only
/*
* Block device LED trigger
*
* Copyright 2021-2023 Ian Pilcher <[email protected]>
*/
#include <linux/blkdev.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/part_stat.h>
#include <linux/xarray.h>
/**
* DOC: Overview
*
* The ``blkdev`` LED trigger works by periodically checking the activity
* counters of block devices that have been linked to one or more LEDs and
* blinking those LED(s) if the correct type of activity has occurred. The
* periodic check is scheduled with the Linux kernel's deferred work facility.
*
* Trigger-specific data about block devices and LEDs is stored in two data
* structures --- &struct blkdev_trig_bdev (a "BTB") and &struct blkdev_trig_led
* (a "BTL"). Each structure contains a &struct xarray that holds links to any
* linked devices of the other type. I.e. &blkdev_trig_bdev.linked_btls
* contains links to all BTLs whose LEDs have been linked to the BTB's block
* device, and &blkdev_trig_led.linked_btbs contains links to all BTBs whose
* block devices have been linked to the BTL's LED. Thus, a block device can
* be linked to more than one LED, and an LED can be linked to more than one
* block device.
*/
/* Default, minimum & maximum blink duration (milliseconds) */
#define BLKDEV_TRIG_BLINK_DEF 75
#define BLKDEV_TRIG_BLINK_MIN 10
#define BLKDEV_TRIG_BLINK_MAX 86400000 /* 24 hours */
/* Default, minimum & maximum activity check interval (milliseconds) */
#define BLKDEV_TRIG_CHECK_DEF 100
#define BLKDEV_TRIG_CHECK_MIN 25
#define BLKDEV_TRIG_CHECK_MAX 86400000 /* 24 hours */
/*
* If blkdev_trig_check() can't lock the mutex, how long to wait before trying
* again (milliseconds)
*/
#define BLKDEV_TRIG_CHECK_RETRY 5
/**
* struct blkdev_trig_bdev - Trigger-specific data about a block device.
* @last_checked: Time (in jiffies) at which the trigger last checked this
* block device for activity.
* @last_activity: Time (in jiffies) at which the trigger last detected
* activity of each type.
* @ios: Activity counter values for each type, corresponding to
* the timestamps in &last_activity.
* @index: &xarray index, so the BTB can be included in one or more
* &blkdev_trig_led.linked_btbs.
* @bdev: The block device.
* @linked_btls: The BTLs that represent the LEDs linked to the BTB's
* block device.
*
* Every block device linked to at least one LED gets a "BTB." A BTB is created
* when a block device that is not currently linked to any LEDs is linked to an
* LED.
*
* A BTB is freed when one of the following occurs:
*
* * The number of LEDs linked to the block device becomes zero, because it has
* been unlinked from its last LED using the trigger's &sysfs interface.
*
* * The number of LEDs linked to the block device becomes zero, because the
* last LED to which it was linked has been disassociated from the trigger
* (which happens automatically if the LED device is removed from the system).
*
* * The BTB's block device is removed from the system. To accomodate this
* scenario, BTB's are created as device resources, so that the release
* function will be called by the driver core when the device is removed.
*/
struct blkdev_trig_bdev {
unsigned long last_checked;
unsigned long last_activity[NR_STAT_GROUPS];
unsigned long ios[NR_STAT_GROUPS];
unsigned long index;
struct block_device *bdev;
struct xarray linked_btls;
};
/**
* struct blkdev_trig_led - Trigger-specific data about an LED.
* @last_checked: Time (in jiffies) at which the trigger last checked the
* the block devices linked to this LED for activity.
* @index: &xarray index, so the BTL can be included in one or more
* &blkdev_trig_bdev.linked_btls.
* @mode: Bitmask for types of block device activity that will
* cause this LED to blink --- reads, writes, discards,
* etc.
* @led: The LED device.
* @blink_msec: Duration of a blink (milliseconds).
* @check_jiffies: Frequency with which block devices linked to this LED
* should be checked for activity (jiffies).
* @linked_btbs: The BTBs that represent the block devices linked to the
* BTL's LED.
* @all_btls_node: The BTL's node in the module's list of all BTLs.
*
* Every LED associated with the block device trigger gets a "BTL." A BTL is
* created when the trigger is "activated" on an LED (usually by writing
* ``blkdev`` to the LED's &sysfs &trigger attribute). A BTL is freed wnen its
* LED is disassociated from the trigger, either through the trigger's &sysfs
* interface or because the LED device is removed from the system.
*/
struct blkdev_trig_led {
unsigned long last_checked;
unsigned long index;
unsigned long mode; /* must be ulong for atomic bit ops */
struct led_classdev *led;
unsigned int blink_msec;
unsigned int check_jiffies;
struct xarray linked_btbs;
struct hlist_node all_btls_node;
};
/* Protects everything except atomic LED attributes */
static DEFINE_MUTEX(blkdev_trig_mutex);
/* BTB device resource release function */
static void blkdev_trig_btb_release(struct device *dev, void *res);
/* Index for next BTB or BTL */
static unsigned long blkdev_trig_next_index;
/* All LEDs associated with the trigger */
static HLIST_HEAD(blkdev_trig_all_btls);
/* Delayed work to periodically check for activity & blink LEDs */
static void blkdev_trig_check(struct work_struct *work);
static DECLARE_DELAYED_WORK(blkdev_trig_work, blkdev_trig_check);
/* When is the delayed work scheduled to run next (jiffies) */
static unsigned long blkdev_trig_next_check;
/* Total number of BTB-to-BTL links */
static unsigned int blkdev_trig_link_count;
/* Empty sysfs attribute list for next 2 declarations */
static struct attribute *blkdev_trig_attrs_empty[] = { NULL };
/* linked_leds sysfs directory for block devs linked to 1 or more LEDs */
static const struct attribute_group blkdev_trig_linked_leds = {
.name = "linked_leds",
.attrs = blkdev_trig_attrs_empty,
};
/* linked_devices sysfs directory for each LED associated with the trigger */
static const struct attribute_group blkdev_trig_linked_devs = {
.name = "linked_devices",
.attrs = blkdev_trig_attrs_empty,
};
/*
*
* Delayed work to check for activity & blink LEDs
*
*/
/**
* blkdev_trig_blink() - Blink an LED, if the correct type of activity has
* occurred on the block device.
* @btl: The BTL that represents the LED
* @btb: The BTB that represents the block device
*
* Context: Process context. Caller must hold &blkdev_trig_mutex.
* Return: &true if the LED is blinked, &false if not.
*/
static bool blkdev_trig_blink(const struct blkdev_trig_led *btl,
const struct blkdev_trig_bdev *btb)
{
unsigned long mode, mask, delay_on, delay_off;
enum stat_group i;
mode = READ_ONCE(btl->mode);
for (i = STAT_READ, mask = 1; i <= STAT_FLUSH; ++i, mask <<= 1) {
if (!(mode & mask))
continue;
if (time_before_eq(btb->last_activity[i], btl->last_checked))
continue;
delay_on = READ_ONCE(btl->blink_msec);
delay_off = 1; /* 0 leaves LED turned on */
led_blink_set_oneshot(btl->led, &delay_on, &delay_off, 0);
return true;
}
return false;
}
/**
* blkdev_trig_update_btb() - Update a BTB's activity counters and timestamps.
* @btb: The BTB
* @now: Timestamp (in jiffies)
*
* Context: Process context. Caller must hold &blkdev_trig_mutex.
*/
static void blkdev_trig_update_btb(struct blkdev_trig_bdev *btb,
unsigned long now)
{
unsigned long new_ios;
enum stat_group i;
for (i = STAT_READ; i <= STAT_FLUSH; ++i) {
new_ios = part_stat_read(btb->bdev, ios[i]);
if (new_ios != btb->ios[i]) {
btb->ios[i] = new_ios;
btb->last_activity[i] = now;
}
}
btb->last_checked = now;
}
/**
* blkdev_trig_check() - Check linked devices for activity and blink LEDs.
* @work: Delayed work (&blkdev_trig_work)
*
* Context: Process context. Takes and releases &blkdev_trig_mutex.
*/
static void blkdev_trig_check(struct work_struct *work)
{
struct blkdev_trig_led *btl;
struct blkdev_trig_bdev *btb;
unsigned long index, delay, now, led_check, led_delay;
bool blinked;
if (!mutex_trylock(&blkdev_trig_mutex)) {
delay = msecs_to_jiffies(BLKDEV_TRIG_CHECK_RETRY);
goto exit_reschedule;
}
now = jiffies;
delay = ULONG_MAX;
hlist_for_each_entry (btl, &blkdev_trig_all_btls, all_btls_node) {
led_check = btl->last_checked + btl->check_jiffies;
if (time_before_eq(led_check, now)) {
blinked = false;
xa_for_each (&btl->linked_btbs, index, btb) {
if (btb->last_checked != now)
blkdev_trig_update_btb(btb, now);
if (!blinked)
blinked = blkdev_trig_blink(btl, btb);
}
btl->last_checked = now;
led_delay = btl->check_jiffies;
} else {
led_delay = led_check - now;
}
if (led_delay < delay)
delay = led_delay;
}
mutex_unlock(&blkdev_trig_mutex);
exit_reschedule:
WARN_ON_ONCE(delay == ULONG_MAX);
WARN_ON_ONCE(!schedule_delayed_work(&blkdev_trig_work, delay));
}
/**
* blkdev_trig_sched_led() - Set the schedule of the delayed work when a new
* LED is added to the schedule.
* @btl: The BTL that represents the LED
*
* Called when the number of block devices to which an LED is linked becomes
* non-zero.
*
* Context: Process context. Caller must hold &blkdev_trig_mutex.
*/
static void blkdev_trig_sched_led(const struct blkdev_trig_led *btl)
{
unsigned long delay = READ_ONCE(btl->check_jiffies);
unsigned long check_by = jiffies + delay;
/*
* If no other LED-to-block device links exist, simply schedule the
* delayed work according to this LED's check_interval attribute
* (check_jiffies).
*/
if (blkdev_trig_link_count == 0) {
WARN_ON(!schedule_delayed_work(&blkdev_trig_work, delay));
blkdev_trig_next_check = check_by;
return;
}
/*
* If the next check is already scheduled to occur soon enough to
* accomodate this LED's check_interval, the schedule doesn't need
* to be changed.
*/
if (time_after_eq(check_by, blkdev_trig_next_check))
return;
/*
* Modify the schedule, so that the delayed work runs soon enough for
* this LED.
*/
WARN_ON(!mod_delayed_work(system_wq, &blkdev_trig_work, delay));
blkdev_trig_next_check = check_by;
}
/*
*
* Linking and unlinking LEDs and block devices
*
*/
/**
* blkdev_trig_link() - Link a block device to an LED.
* @btl: The BTL that represents the LED
* @btb: The BTB that represents the block device
*
* Context: Process context. Caller must hold &blkdev_trig_mutex.
* Return: &0 on success, negative &errno on error.
*/
static int blkdev_trig_link(struct blkdev_trig_led *btl,
struct blkdev_trig_bdev *btb)
{
bool led_first_link;
int err;
led_first_link = xa_empty(&btl->linked_btbs);
err = xa_insert(&btb->linked_btls, btl->index, btl, GFP_KERNEL);
if (err)
return err;
err = xa_insert(&btl->linked_btbs, btb->index, btb, GFP_KERNEL);
if (err)
goto error_erase_btl;
/* Create /sys/class/block/<bdev>/linked_leds/<led> symlink */
err = sysfs_add_link_to_group(bdev_kobj(btb->bdev),
blkdev_trig_linked_leds.name,
&btl->led->dev->kobj, btl->led->name);
if (err)
goto error_erase_btb;
/* Create /sys/class/leds/<led>/linked_devices/<bdev> symlink */
err = sysfs_add_link_to_group(&btl->led->dev->kobj,
blkdev_trig_linked_devs.name,
bdev_kobj(btb->bdev),
dev_name(&btb->bdev->bd_device));
if (err)
goto error_remove_symlink;
/*
* If this is the first block device linked to this LED, the delayed
* work schedule may need to be changed.
*/
if (led_first_link)
blkdev_trig_sched_led(btl);
++blkdev_trig_link_count;
return 0;
error_remove_symlink:
sysfs_remove_link_from_group(bdev_kobj(btb->bdev),
blkdev_trig_linked_leds.name,
btl->led->name);
error_erase_btb:
xa_erase(&btl->linked_btbs, btb->index);
error_erase_btl:
xa_erase(&btb->linked_btls, btl->index);
return err;
}
/**
* blkdev_trig_put_btb() - Remove and free a BTB, if it is no longer needed.
* @btb: The BTB
*
* Does nothing if the BTB (block device) is still linked to at least one LED.
*
* Context: Process context. Caller must hold &blkdev_trig_mutex.
*/
static void blkdev_trig_put_btb(struct blkdev_trig_bdev *btb)
{
struct block_device *bdev = btb->bdev;
int err;
if (xa_empty(&btb->linked_btls)) {
sysfs_remove_group(bdev_kobj(bdev), &blkdev_trig_linked_leds);
err = devres_destroy(&bdev->bd_device, blkdev_trig_btb_release,
NULL, NULL);
WARN_ON(err);
}
}
/**
* _blkdev_trig_unlink_always() - Perform the unconditionally required steps of
* unlinking a block device from an LED.
* @btl: The BTL that represents the LED
* @btb: The BTB that represents the block device
*
* When a block device is unlinked from an LED, certain steps must be performed
* only if the block device is **not** being released. This function performs
* those steps that are **always** required, whether or not the block device is
* being released.
*
* Context: Process context. Caller must hold &blkdev_trig_mutex.
*/
static void _blkdev_trig_unlink_always(struct blkdev_trig_led *btl,
struct blkdev_trig_bdev *btb)
{
--blkdev_trig_link_count;
if (blkdev_trig_link_count == 0)
WARN_ON(!cancel_delayed_work_sync(&blkdev_trig_work));
xa_erase(&btb->linked_btls, btl->index);
xa_erase(&btl->linked_btbs, btb->index);
/* Remove /sys/class/leds/<led>/linked_devices/<bdev> symlink */
sysfs_remove_link_from_group(&btl->led->dev->kobj,
blkdev_trig_linked_devs.name,
dev_name(&btb->bdev->bd_device));
}
/**
* blkdev_trig_unlink_norelease() - Unlink an LED from a block device that is
* **not** being released.
* @btl: The BTL that represents the LED.
* @btb: The BTB that represents the block device.
*
* Context: Process context. Caller must hold &blkdev_trig_mutex.
*/
static void blkdev_trig_unlink_norelease(struct blkdev_trig_led *btl,
struct blkdev_trig_bdev *btb)
{
_blkdev_trig_unlink_always(btl, btb);
/* Remove /sys/class/block/<bdev>/linked_leds/<led> symlink */
sysfs_remove_link_from_group(bdev_kobj(btb->bdev),
blkdev_trig_linked_leds.name,
btl->led->name);
blkdev_trig_put_btb(btb);
}
/**
* blkdev_trig_unlink_release() - Unlink an LED from a block device that is
* being released.
* @btl: The BTL that represents the LED
* @btb: The BTB that represents the block device
*
* Context: Process context. Caller must hold &blkdev_trig_mutex.
*/
static void blkdev_trig_unlink_release(struct blkdev_trig_led *btl,
struct blkdev_trig_bdev *btb)
{
_blkdev_trig_unlink_always(btl, btb);
/*
* If the BTB is being released, the driver core has already removed the
* device's attribute groups, and the BTB will be freed automatically,
* so there's nothing else to do.
*/
}
/*
*
* BTB creation
*
*/
/**
* blkdev_trig_btb_release() - BTB device resource release function.
* @dev: The block device
* @res: The BTB
*
* Called by the driver core when a block device with a BTB is removed.
*
* Context: Process context. Takes and releases &blkdev_trig_mutex.
*/
static void blkdev_trig_btb_release(struct device *dev, void *res)
{
struct blkdev_trig_bdev *btb = res;
struct blkdev_trig_led *btl;
unsigned long index;
mutex_lock(&blkdev_trig_mutex);
xa_for_each (&btb->linked_btls, index, btl)
blkdev_trig_unlink_release(btl, btb);
mutex_unlock(&blkdev_trig_mutex);
}
/**
* blkdev_trig_get_bdev() - Get a block device by path.
* @path: The value written to an LED's &link_dev_by_path or
* &unlink_dev_by_path attribute, which should be the path to a
* special file that represents a block device
* @len: The number of characters in &path (not including its
* terminating null)
*
* The caller must call blkdev_put() when finished with the device.
*
* Context: Process context.
* Return: The block device, or an error pointer.
*/
static struct bdev_handle *blkdev_trig_get_bdev(const char *path, size_t len)
{
struct bdev_handle *handle;
char *buf;
buf = kmemdup(path, len + 1, GFP_KERNEL); /* +1 to include null */
if (buf == NULL)
return ERR_PTR(-ENOMEM);
handle = bdev_open_by_path(strim(buf), 0, NULL, NULL);
kfree(buf);
return handle;
}
/**
* blkdev_trig_get_btb() - Find or create the BTB for a block device.
* @path: The value written to an LED's &link_dev_by_path attribute,
* which should be the path to a special file that represents a
* block device
* @len: The number of characters in &path
*
* If a new BTB is created, because the block device was not previously linked
* to any LEDs, the block device's &linked_leds &sysfs directory is created.
*
* Context: Process context. Caller must hold &blkdev_trig_mutex.
* Return: Pointer to the BTB, error pointer on error.
*/
static struct blkdev_trig_bdev *blkdev_trig_get_btb(const char *path,
size_t len)
{
struct block_device *bdev;
struct bdev_handle *handle;
struct blkdev_trig_bdev *btb;
int err;
handle = blkdev_trig_get_bdev(path, len);
if (IS_ERR(handle))
return ERR_CAST(handle);
bdev = handle->bdev;
btb = devres_find(&bdev->bd_device, blkdev_trig_btb_release,
NULL, NULL);
if (btb != NULL) {
err = 0;
goto exit_put_bdev;
}
if (blkdev_trig_next_index == ULONG_MAX) {
err = -EOVERFLOW;
goto exit_put_bdev;
}
btb = devres_alloc(blkdev_trig_btb_release, sizeof(*btb), GFP_KERNEL);
if (btb == NULL) {
err = -ENOMEM;
goto exit_put_bdev;
}
err = sysfs_create_group(bdev_kobj(bdev), &blkdev_trig_linked_leds);
if (err)
goto exit_free_btb;
btb->index = blkdev_trig_next_index++;
btb->bdev = bdev;
xa_init(&btb->linked_btls);
/* Populate BTB activity counters */
blkdev_trig_update_btb(btb, jiffies);
devres_add(&bdev->bd_device, btb);
exit_free_btb:
if (err)
devres_free(btb);
exit_put_bdev:
bdev_release(handle);
return err ? ERR_PTR(err) : btb;
}
/*
*
* Activating and deactivating the trigger on an LED
*
*/
/**
* blkdev_trig_activate() - Called by the LEDs subsystem when an LED is
* associated with the trigger.
* @led: The LED
*
* Context: Process context. Takes and releases &blkdev_trig_mutex.
* Return: &0 on success, negative &errno on error.
*/
static int blkdev_trig_activate(struct led_classdev *led)
{
struct blkdev_trig_led *btl;
int err;
btl = kzalloc(sizeof(*btl), GFP_KERNEL);
if (btl == NULL)
return -ENOMEM;
err = mutex_lock_interruptible(&blkdev_trig_mutex);
if (err)
goto exit_free;
if (blkdev_trig_next_index == ULONG_MAX) {
err = -EOVERFLOW;
goto exit_unlock;
}
btl->index = blkdev_trig_next_index++;
btl->last_checked = jiffies;
btl->mode = -1; /* set all bits */
btl->led = led;
btl->blink_msec = BLKDEV_TRIG_BLINK_DEF;
btl->check_jiffies = msecs_to_jiffies(BLKDEV_TRIG_CHECK_DEF);
xa_init(&btl->linked_btbs);
hlist_add_head(&btl->all_btls_node, &blkdev_trig_all_btls);
led_set_trigger_data(led, btl);
exit_unlock:
mutex_unlock(&blkdev_trig_mutex);
exit_free:
if (err)
kfree(btl);
return err;
}
/**
* blkdev_trig_deactivate() - Called by the the LEDs subsystem when an LED is
* disassociated from the trigger.
* @led: The LED
*
* The LEDs subsystem also calls this function when an LED associated with the
* trigger is removed or when the trigger is unregistered (if the module is
* unloaded).
*
* Context: Process context. Takes and releases &blkdev_trig_mutex.
*/
static void blkdev_trig_deactivate(struct led_classdev *led)
{
struct blkdev_trig_led *btl = led_get_trigger_data(led);
struct blkdev_trig_bdev *btb;
unsigned long index;
mutex_lock(&blkdev_trig_mutex);
xa_for_each (&btl->linked_btbs, index, btb)
blkdev_trig_unlink_norelease(btl, btb);
hlist_del(&btl->all_btls_node);
kfree(btl);
mutex_unlock(&blkdev_trig_mutex);
}
/*
*
* Link-related attribute store functions
*
*/
/**
* link_dev_by_path_store() - &link_dev_by_path device attribute store function.
* @dev: The LED device
* @attr: The &link_dev_by_path attribute (&dev_attr_link_dev_by_path)
* @buf: The value written to the attribute, which should be the path to
* a special file that represents a block device to be linked to
* the LED (e.g. ``/dev/sda``)
* @count: The number of characters in &buf
*
* Context: Process context. Takes and releases &blkdev_trig_mutex.
* Return: &count on success, negative &errno on error.
*/
static ssize_t link_dev_by_path_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct blkdev_trig_led *btl = led_trigger_get_drvdata(dev);
struct blkdev_trig_bdev *btb;
int err;
err = mutex_lock_interruptible(&blkdev_trig_mutex);
if (err)
return err;
btb = blkdev_trig_get_btb(buf, count);
if (IS_ERR(btb)) {
err = PTR_ERR(btb);
goto exit_unlock;
}
if (xa_load(&btb->linked_btls, btl->index) != NULL) {
err = -EEXIST;
goto exit_put_btb;
}
err = blkdev_trig_link(btl, btb);
exit_put_btb:
if (err)
blkdev_trig_put_btb(btb);
exit_unlock:
mutex_unlock(&blkdev_trig_mutex);
return err ? : count;
}
/**
* unlink_dev_by_path_store() - &unlink_dev_by_path device attribute store
* function.
* @dev: The LED device
* @attr: The &unlink_dev_by_path attribute (&dev_attr_unlink_dev_by_path)
* @buf: The value written to the attribute, which should be the path to
* a special file that represents a block device to be unlinked
* from the LED (e.g. ``/dev/sda``)
* @count: The number of characters in &buf
*
* Context: Process context. Takes and releases &blkdev_trig_mutex.
* Return: &count on success, negative &errno on error.
*/
static ssize_t unlink_dev_by_path_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct blkdev_trig_led *btl = led_trigger_get_drvdata(dev);
struct block_device *bdev;
struct blkdev_trig_bdev *btb;
struct bdev_handle *handle;
int err;
handle = blkdev_trig_get_bdev(buf, count);
if (IS_ERR(handle))
return PTR_ERR(handle);
bdev = handle->bdev;
err = mutex_lock_interruptible(&blkdev_trig_mutex);
if (err)
goto exit_put_bdev;
btb = devres_find(&bdev->bd_device, blkdev_trig_btb_release,
NULL, NULL);
if (btb == NULL) {
err = -EUNATCH; /* bdev isn't linked to any LED */
goto exit_unlock;
}
if (xa_load(&btb->linked_btls, btl->index) == NULL) {
err = -EUNATCH; /* bdev isn't linked to this LED */
goto exit_unlock;
}
blkdev_trig_unlink_norelease(btl, btb);
exit_unlock:
mutex_unlock(&blkdev_trig_mutex);
exit_put_bdev:
bdev_release(handle);
return err ? : count;
}
/**
* unlink_dev_by_name_store() - &unlink_dev_by_name device attribute store
* function.
* @dev: The LED device
* @attr: The &unlink_dev_by_name attribute (&dev_attr_unlink_dev_by_name)
* @buf: The value written to the attribute, which should be the kernel
* name of a block device to be unlinked from the LED (e.g.
* ``sda``)
* @count: The number of characters in &buf
*
* Context: Process context. Takes and releases &blkdev_trig_mutex.
* Return: &count on success, negative &errno on error.
*/
static ssize_t unlink_dev_by_name_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct blkdev_trig_led *btl = led_trigger_get_drvdata(dev);
struct blkdev_trig_bdev *btb;
unsigned long index;
int err;
err = mutex_lock_interruptible(&blkdev_trig_mutex);
if (err)
return err;
err = -EUNATCH;
xa_for_each (&btl->linked_btbs, index, btb) {
if (sysfs_streq(dev_name(&btb->bdev->bd_device), buf)) {
blkdev_trig_unlink_norelease(btl, btb);
err = 0;
break;
}
}
mutex_unlock(&blkdev_trig_mutex);
return err ? : count;
}
/*
*
* Atomic attribute show & store functions
*
*/
/**
* blink_time_show() - &blink_time device attribute show function.
* @dev: The LED device
* @attr: The &blink_time attribute (&dev_attr_blink_time)
* @buf: Output buffer
*
* Writes the value of &blkdev_trig_led.blink_msec to &buf.
*
* Context: Process context.
* Return: The number of characters written to &buf.
*/
static ssize_t blink_time_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
const struct blkdev_trig_led *btl = led_trigger_get_drvdata(dev);
return sysfs_emit(buf, "%u\n", READ_ONCE(btl->blink_msec));
}
/**
* blink_time_store() - &blink_time device attribute store function.
* @dev: The LED device
* @attr: The &blink_time attribute (&dev_attr_blink_time)
* @buf: The new value (as written to the &sysfs attribute)
* @count: The number of characters in &buf
*
* Sets &blkdev_trig_led.blink_msec to the value in &buf.
*
* Context: Process context.
* Return: &count on success, negative &errno on error.
*/
static ssize_t blink_time_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct blkdev_trig_led *btl = led_trigger_get_drvdata(dev);
unsigned int value;
int err;
err = kstrtouint(buf, 0, &value);
if (err)
return err;
if (value < BLKDEV_TRIG_BLINK_MIN || value > BLKDEV_TRIG_BLINK_MAX)
return -ERANGE;
WRITE_ONCE(btl->blink_msec, value);
return count;
}
/**
* check_interval_show() - &check_interval device attribute show function.
* @dev: The LED device
* @attr: The &check_interval attribute (&dev_attr_check_interval)
* @buf: Output buffer
*
* Writes the value of &blkdev_trig_led.check_jiffies (converted to
* milliseconds) to &buf.
*
* Context: Process context.
* Return: The number of characters written to &buf.
*/
static ssize_t check_interval_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct blkdev_trig_led *btl = led_trigger_get_drvdata(dev);
return sysfs_emit(buf, "%u\n",
jiffies_to_msecs(READ_ONCE(btl->check_jiffies)));
}
/**
* check_interval_store() - &check_interval device attribute store function
* @dev: The LED device
* @attr: The &check_interval attribute (&dev_attr_check_interval)
* @buf: The new value (as written to the &sysfs attribute)
* @count: The number of characters in &buf
*
* Sets &blkdev_trig_led.check_jiffies to the value in &buf (after converting
* from milliseconds).
*
* Context: Process context.
* Return: &count on success, negative &errno on error.
*/
static ssize_t check_interval_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct blkdev_trig_led *led = led_trigger_get_drvdata(dev);
unsigned int value;
int err;
err = kstrtouint(buf, 0, &value);
if (err)
return err;
if (value < BLKDEV_TRIG_CHECK_MIN || value > BLKDEV_TRIG_CHECK_MAX)
return -ERANGE;
WRITE_ONCE(led->check_jiffies, msecs_to_jiffies(value));
return count;
}
/**
* blkdev_trig_mode_show() - Helper for boolean attribute show functions.
* @led: The LED
* @buf: Output buffer
* @bit: Which bit to show
*
* Context: Process context.
* Return: The number of characters written to &buf.
*/
static int blkdev_trig_mode_show(const struct blkdev_trig_led *led, char *buf,
enum stat_group bit)
{
return sysfs_emit(buf,
READ_ONCE(led->mode) & (1 << bit) ? "Y\n" : "N\n");
}
/**
* blkdev_trig_mode_store() - Helper for boolean attribute store functions.
* @led: The LED
* @buf: The new value (as written to the &sysfs attribute)
* @count: The number of characters in &buf
* @bit: Which bit to set
*
* Context: Process context.
* Return: &count on success, negative &errno on error.
*/
static int blkdev_trig_mode_store(struct blkdev_trig_led *led,
const char *buf, size_t count,
enum stat_group bit)
{
bool set;
int err;
err = kstrtobool(buf, &set);
if (err)
return err;
if (set)
set_bit(bit, &led->mode);
else
clear_bit(bit, &led->mode);
return count;
}
/**
* blink_on_read_show() - &blink_on_read device attribute show function.
* @dev: The LED device
* @attr: The &blink_on_read attribute (&dev_attr_blink_on_read)
* @buf: Output buffer
*
* Writes ``Y`` or ``N`` to &buf, depending on whether the &STAT_READ bit in