2 * Copyright (C) 2012-2020 all contributors <cmogstored-public@yhbt.net>
3 * License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
6 #include <sys/syscall.h>
8 #if defined(SYS_ioprio_get) && defined(SYS_ioprio_set)
10 #ifndef IOPRIO_PRIO_CLASS
11 #include "ioprio_linux.h"
12 #endif /* Linux headers */
15 * specifying which == IOPRIO_WHO_PROCESS and who == 0
16 * means ioprio works on the current task in Linux.
17 * This means we can avoid a syscall for gettid()
20 static inline int mog_ioprio_get(int which
, int who
)
22 return syscall(SYS_ioprio_get
, which
, who
);
25 static inline int mog_ioprio_set(int which
, int who
, int ioprio
)
27 return syscall(SYS_ioprio_set
, which
, who
, ioprio
);
30 static int mog_ioprio_drop(void)
33 int oldprio
= mog_ioprio_get(IOPRIO_WHO_PROCESS
, 0);
40 /* don't bother unless it's already best-effort */
41 switch (IOPRIO_PRIO_CLASS(oldprio
)) {
42 case IOPRIO_CLASS_NONE
:
44 * we have to filter out data for ioprio_set to succeed,
45 * NONE means the task gets ioprio based on niceness
47 oldprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_NONE
, 0);
54 newprio
= IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE
, 7);
55 rc
= mog_ioprio_set(IOPRIO_WHO_PROCESS
, 0, newprio
);
61 syslog(LOG_WARNING
, "failed to drop IO priority: %m");
66 static void mog_ioprio_restore(int ioprio
)
69 int rc
= mog_ioprio_set(IOPRIO_WHO_PROCESS
, 0, ioprio
);
71 if (rc
< 0 && !warned
) {
73 syslog(LOG_WARNING
, "failed to restore IO priority: %m");
77 #else /* workalikes */
78 static inline int mog_ioprio_drop(void)
83 static inline void mog_ioprio_restore(int ioprio
)