Skip to content

Commit 77b0402

Browse files
committed
btrfs: pass level to _btrfs_printk() to avoid parsing level from string
There's code in _btrfs_printk() to parse the message level from the input string so we can augment the message with the level description for better visibility in the logs. The parsing code has evolved over time, see commits: - 40f7828 ("btrfs: better handle btrfs_printk() defaults") - 262c5e8 ("printk/btrfs: handle more message headers") - 533574c ("btrfs: use printk_get_level and printk_skip_level, add __printf, fix fallout") - 4da3511 ("btrfs: add varargs to btrfs_error") As we are using the specific level helpers everywhere we can simply pass the message level so we don't have to parse it. The proper printk() message header is created as KERN_SOH + "level". Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 972016c commit 77b0402

File tree

2 files changed

+30
-45
lines changed

2 files changed

+30
-45
lines changed

fs/btrfs/messages.c

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -211,33 +211,19 @@ static struct ratelimit_state printk_limits[] = {
211211
RATELIMIT_STATE_INIT(printk_limits[7], DEFAULT_RATELIMIT_INTERVAL, 100),
212212
};
213213

214-
void __cold _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
214+
__printf(3, 4) __cold
215+
void _btrfs_printk(const struct btrfs_fs_info *fs_info, unsigned int level, const char *fmt, ...)
215216
{
216-
char lvl[PRINTK_MAX_SINGLE_HEADER_LEN + 1] = "\0";
217217
struct va_format vaf;
218218
va_list args;
219-
int kern_level;
220-
const char *type = logtypes[4];
221-
struct ratelimit_state *ratelimit = &printk_limits[4];
219+
const char *type = logtypes[level];
220+
struct ratelimit_state *ratelimit = &printk_limits[level];
222221

223222
#ifdef CONFIG_PRINTK_INDEX
224223
printk_index_subsys_emit("%sBTRFS %s (device %s): ", NULL, fmt);
225224
#endif
226225

227226
va_start(args, fmt);
228-
229-
while ((kern_level = printk_get_level(fmt)) != 0) {
230-
size_t size = printk_skip_level(fmt) - fmt;
231-
232-
if (kern_level >= '0' && kern_level <= '7') {
233-
memcpy(lvl, fmt, size);
234-
lvl[size] = '\0';
235-
type = logtypes[kern_level - '0'];
236-
ratelimit = &printk_limits[kern_level - '0'];
237-
}
238-
fmt += size;
239-
}
240-
241227
vaf.fmt = fmt;
242228
vaf.va = &args;
243229

@@ -247,10 +233,10 @@ void __cold _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt,
247233
char statestr[STATE_STRING_BUF_LEN];
248234

249235
btrfs_state_to_string(fs_info, statestr);
250-
_printk("%sBTRFS %s (device %s%s): %pV\n", lvl, type,
236+
_printk(KERN_SOH "%dBTRFS %s (device %s%s): %pV\n", level, type,
251237
fs_info->sb->s_id, statestr, &vaf);
252238
} else {
253-
_printk("%sBTRFS %s: %pV\n", lvl, type, &vaf);
239+
_printk(KERN_SOH "%dBTRFS %s: %pV\n", level, type, &vaf);
254240
}
255241
}
256242

fs/btrfs/messages.h

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,18 @@ void btrfs_no_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
2323

2424
#ifdef CONFIG_PRINTK
2525

26-
__printf(2, 3)
27-
__cold
28-
void _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...);
26+
__printf(3, 4) __cold
27+
void _btrfs_printk(const struct btrfs_fs_info *fs_info, unsigned int level, const char *fmt, ...);
2928

3029
#else
3130

32-
#define btrfs_printk(fs_info, fmt, args...) \
31+
#define btrfs_printk_in_rcu(fs_info, level, fmt, args...) \
3332
btrfs_no_printk(fs_info, fmt, ##args)
3433

35-
#define btrfs_printk_in_rcu(fs_info, fmt, args...) \
34+
#define btrfs_printk_in_rcu(fs_info, level, fmt, args...) \
3635
btrfs_no_printk(fs_info, fmt, ##args)
3736

38-
#define btrfs_printk_rl_in_rcu(fs_info, fmt, args...) \
37+
#define btrfs_printk_rl_in_rcu(fs_info, level, fmt, args...) \
3938
btrfs_no_printk(fs_info, fmt, ##args)
4039

4140
#endif
@@ -44,38 +43,38 @@ void _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...);
4443
* Print a message with filesystem info, enclosed in RCU protection.
4544
*/
4645
#define btrfs_crit(fs_info, fmt, args...) \
47-
btrfs_printk_in_rcu(fs_info, KERN_CRIT fmt, ##args)
46+
btrfs_printk_in_rcu(fs_info, LOGLEVEL_CRIT, fmt, ##args)
4847
#define btrfs_err(fs_info, fmt, args...) \
49-
btrfs_printk_in_rcu(fs_info, KERN_ERR fmt, ##args)
48+
btrfs_printk_in_rcu(fs_info, LOGLEVEL_ERR, fmt, ##args)
5049
#define btrfs_warn(fs_info, fmt, args...) \
51-
btrfs_printk_in_rcu(fs_info, KERN_WARNING fmt, ##args)
50+
btrfs_printk_in_rcu(fs_info, LOGLEVEL_WARNING, fmt, ##args)
5251
#define btrfs_info(fs_info, fmt, args...) \
53-
btrfs_printk_in_rcu(fs_info, KERN_INFO fmt, ##args)
52+
btrfs_printk_in_rcu(fs_info, LOGLEVEL_INFO, fmt, ##args)
5453

5554
/*
5655
* Wrappers that use a ratelimited printk
5756
*/
5857
#define btrfs_crit_rl(fs_info, fmt, args...) \
59-
btrfs_printk_rl_in_rcu(fs_info, KERN_CRIT fmt, ##args)
58+
btrfs_printk_rl_in_rcu(fs_info, LOGLEVEL_CRIT, fmt, ##args)
6059
#define btrfs_err_rl(fs_info, fmt, args...) \
61-
btrfs_printk_rl_in_rcu(fs_info, KERN_ERR fmt, ##args)
60+
btrfs_printk_rl_in_rcu(fs_info, LOGLEVEL_ERR, fmt, ##args)
6261
#define btrfs_warn_rl(fs_info, fmt, args...) \
63-
btrfs_printk_rl_in_rcu(fs_info, KERN_WARNING fmt, ##args)
62+
btrfs_printk_rl_in_rcu(fs_info, LOGLEVEL_WARNING, fmt, ##args)
6463
#define btrfs_info_rl(fs_info, fmt, args...) \
65-
btrfs_printk_rl_in_rcu(fs_info, KERN_INFO fmt, ##args)
64+
btrfs_printk_rl_in_rcu(fs_info, LOGLEVEL_INFO, fmt, ##args)
6665

6766
#if defined(CONFIG_DYNAMIC_DEBUG)
6867
#define btrfs_debug(fs_info, fmt, args...) \
6968
_dynamic_func_call_no_desc(fmt, btrfs_printk_in_rcu, \
70-
fs_info, KERN_DEBUG fmt, ##args)
69+
fs_info, LOGLEVEL_DEBUG, fmt, ##args)
7170
#define btrfs_debug_rl(fs_info, fmt, args...) \
7271
_dynamic_func_call_no_desc(fmt, btrfs_printk_rl_in_rcu, \
73-
fs_info, KERN_DEBUG fmt, ##args)
72+
fs_info, LOGLEVEL_DEBUG, fmt, ##args)
7473
#elif defined(DEBUG)
7574
#define btrfs_debug(fs_info, fmt, args...) \
76-
btrfs_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
75+
btrfs_printk_in_rcu(fs_info, LOGLEVEL_DEBUG, fmt, ##args)
7776
#define btrfs_debug_rl(fs_info, fmt, args...) \
78-
btrfs_printk_rl_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
77+
btrfs_printk_rl_in_rcu(fs_info, LOGLEVEl_DEBUG, fmt, ##args)
7978
#else
8079
/* When printk() is no_printk(), expand to no-op. */
8180
#define btrfs_debug(fs_info, fmt, args...) do { (void)(fs_info); } while(0)
@@ -84,22 +83,22 @@ void _btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...);
8483

8584
#ifdef CONFIG_PRINTK
8685

87-
#define btrfs_printk_in_rcu(fs_info, fmt, args...) \
88-
do { \
89-
rcu_read_lock(); \
90-
_btrfs_printk(fs_info, fmt, ##args); \
91-
rcu_read_unlock(); \
86+
#define btrfs_printk_in_rcu(fs_info, level, fmt, args...) \
87+
do { \
88+
rcu_read_lock(); \
89+
_btrfs_printk(fs_info, level, fmt, ##args); \
90+
rcu_read_unlock(); \
9291
} while (0)
9392

94-
#define btrfs_printk_rl_in_rcu(fs_info, fmt, args...) \
93+
#define btrfs_printk_rl_in_rcu(fs_info, level, fmt, args...) \
9594
do { \
9695
static DEFINE_RATELIMIT_STATE(_rs, \
9796
DEFAULT_RATELIMIT_INTERVAL, \
9897
DEFAULT_RATELIMIT_BURST); \
9998
\
10099
rcu_read_lock(); \
101100
if (__ratelimit(&_rs)) \
102-
_btrfs_printk(fs_info, fmt, ##args); \
101+
_btrfs_printk(fs_info, level, fmt, ##args); \
103102
rcu_read_unlock(); \
104103
} while (0)
105104

0 commit comments

Comments
 (0)