1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Copyright (c) 2019 Linus Walleij <linus.walleij@linaro.org>
4 * Copyright (c) 2023 Linux Test Project
5 */
6
7 #ifndef LTP_IOPRIO_H
8 #define LTP_IOPRIO_H
9
10 #include "lapi/ioprio.h"
11 #include "lapi/syscalls.h"
12
13 static const char * const to_class_str[] = {
14 [IOPRIO_CLASS_NONE] = "NONE",
15 [IOPRIO_CLASS_RT] = "REALTIME",
16 [IOPRIO_CLASS_BE] = "BEST-EFFORT",
17 [IOPRIO_CLASS_IDLE] = "IDLE"
18 };
19
sys_ioprio_get(int which,int who)20 static inline int sys_ioprio_get(int which, int who)
21 {
22 return tst_syscall(__NR_ioprio_get, which, who);
23 }
24
sys_ioprio_set(int which,int who,int ioprio)25 static inline int sys_ioprio_set(int which, int who, int ioprio)
26 {
27 return tst_syscall(__NR_ioprio_set, which, who, ioprio);
28 }
29
30 /* Priority range from 0 (highest) to IOPRIO_PRIO_NUM (lowest) */
prio_in_range(int prio)31 static inline int prio_in_range(int prio)
32 {
33 if ((prio < 0) || (prio >= IOPRIO_PRIO_NUM))
34 return 0;
35 return 1;
36 }
37
38 /* Priority range from 0 to 3 using the enum */
class_in_range(int class)39 static inline int class_in_range(int class)
40 {
41 if ((class < IOPRIO_CLASS_NONE) || (class > IOPRIO_CLASS_IDLE))
42 return 0;
43 return 1;
44 }
45
ioprio_check_setting(int class,int prio,int report)46 static inline void ioprio_check_setting(int class, int prio, int report)
47 {
48 int res;
49 int newclass, newprio;
50
51 res = sys_ioprio_get(IOPRIO_WHO_PROCESS, 0);
52 if (res == -1) {
53 tst_res(TFAIL | TTERRNO,
54 "reading back prio failed");
55 return;
56 }
57
58 newclass = IOPRIO_PRIO_CLASS(res);
59 newprio = IOPRIO_PRIO_LEVEL(res);
60 if (newclass != class)
61 tst_res(TFAIL,
62 "wrong class after setting, expected %s got %s",
63 to_class_str[class],
64 to_class_str[newclass]);
65 else if (newprio != prio)
66 tst_res(TFAIL,
67 "wrong prio after setting, expected %d got %d",
68 prio, newprio);
69 else if (report)
70 tst_res(TPASS, "ioprio_set new class %s, new prio %d",
71 to_class_str[newclass],
72 newprio);
73 }
74
75 #endif /* LTP_IOPRIO_H */
76