Skip to content

Commit 9f09f64

Browse files
author
Al Viro
committed
teach logfc() to handle prefices, give it saner calling conventions
Signed-off-by: Al Viro <[email protected]>
1 parent fbc2d16 commit 9f09f64

File tree

2 files changed

+27
-52
lines changed

2 files changed

+27
-52
lines changed

fs/fs_context.c

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -388,64 +388,33 @@ EXPORT_SYMBOL(vfs_dup_fs_context);
388388
* @fc: The filesystem context to log to.
389389
* @fmt: The format of the buffer.
390390
*/
391-
void logfc(struct fs_context *fc, const char *fmt, ...)
391+
void logfc(struct fc_log *log, const char *prefix, char level, const char *fmt, ...)
392392
{
393-
static const char store_failure[] = "OOM: Can't store error string";
394-
struct fc_log *log = fc ? fc->log : NULL;
395-
const char *p;
396393
va_list va;
397-
char *q;
398-
u8 freeable;
394+
struct va_format vaf = {.fmt = fmt, .va = &va};
399395

400396
va_start(va, fmt);
401-
if (!strchr(fmt, '%')) {
402-
p = fmt;
403-
goto unformatted_string;
404-
}
405-
if (strcmp(fmt, "%s") == 0) {
406-
p = va_arg(va, const char *);
407-
goto unformatted_string;
408-
}
409-
410-
q = kvasprintf(GFP_KERNEL, fmt, va);
411-
copied_string:
412-
if (!q)
413-
goto store_failure;
414-
freeable = 1;
415-
goto store_string;
416-
417-
unformatted_string:
418-
if ((unsigned long)p >= (unsigned long)__start_rodata &&
419-
(unsigned long)p < (unsigned long)__end_rodata)
420-
goto const_string;
421-
if (log && within_module_core((unsigned long)p, log->owner))
422-
goto const_string;
423-
q = kstrdup(p, GFP_KERNEL);
424-
goto copied_string;
425-
426-
store_failure:
427-
p = store_failure;
428-
const_string:
429-
q = (char *)p;
430-
freeable = 0;
431-
store_string:
432397
if (!log) {
433-
switch (fmt[0]) {
398+
switch (level) {
434399
case 'w':
435-
printk(KERN_WARNING "%s\n", q + 2);
400+
printk(KERN_WARNING "%s%s%pV\n", prefix ? prefix : "",
401+
prefix ? ": " : "", &vaf);
436402
break;
437403
case 'e':
438-
printk(KERN_ERR "%s\n", q + 2);
404+
printk(KERN_ERR "%s%s%pV\n", prefix ? prefix : "",
405+
prefix ? ": " : "", &vaf);
439406
break;
440407
default:
441-
printk(KERN_NOTICE "%s\n", q + 2);
408+
printk(KERN_NOTICE "%s%s%pV\n", prefix ? prefix : "",
409+
prefix ? ": " : "", &vaf);
442410
break;
443411
}
444-
if (freeable)
445-
kfree(q);
446412
} else {
447413
unsigned int logsize = ARRAY_SIZE(log->buffer);
448414
u8 index;
415+
char *q = kasprintf(GFP_KERNEL, "%c %s%s%pV\n", level,
416+
prefix ? prefix : "",
417+
prefix ? ": " : "", &vaf);
449418

450419
index = log->head & (logsize - 1);
451420
BUILD_BUG_ON(sizeof(log->head) != sizeof(u8) ||
@@ -457,9 +426,11 @@ void logfc(struct fs_context *fc, const char *fmt, ...)
457426
log->tail++;
458427
}
459428

460-
log->buffer[index] = q;
461-
log->need_free &= ~(1 << index);
462-
log->need_free |= freeable << index;
429+
log->buffer[index] = q ? q : "OOM: Can't store error string";
430+
if (q)
431+
log->need_free |= 1 << index;
432+
else
433+
log->need_free &= ~(1 << index);
463434
log->head++;
464435
}
465436
va_end(va);

include/linux/fs_context.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,13 @@ struct fc_log {
181181
char *buffer[8];
182182
};
183183

184-
extern __attribute__((format(printf, 2, 3)))
185-
void logfc(struct fs_context *fc, const char *fmt, ...);
184+
extern __attribute__((format(printf, 4, 5)))
185+
void logfc(struct fc_log *log, const char *prefix, char level, const char *fmt, ...);
186186

187+
#define __logfc(fc, l, fmt, ...) ({ \
188+
struct fs_context *__fc = (fc); \
189+
logfc(__fc ? __fc->log : NULL, NULL, \
190+
l, fmt, ## __VA_ARGS__);})
187191
/**
188192
* infof - Store supplementary informational message
189193
* @fc: The context in which to log the informational message
@@ -192,7 +196,7 @@ void logfc(struct fs_context *fc, const char *fmt, ...);
192196
* Store the supplementary informational message for the process if the process
193197
* has enabled the facility.
194198
*/
195-
#define infof(fc, fmt, ...) ({ logfc(fc, "i "fmt, ## __VA_ARGS__); })
199+
#define infof(fc, fmt, ...) __logfc(fc, 'i', fmt, ## __VA_ARGS__)
196200

197201
/**
198202
* warnf - Store supplementary warning message
@@ -202,7 +206,7 @@ void logfc(struct fs_context *fc, const char *fmt, ...);
202206
* Store the supplementary warning message for the process if the process has
203207
* enabled the facility.
204208
*/
205-
#define warnf(fc, fmt, ...) ({ logfc(fc, "w "fmt, ## __VA_ARGS__); })
209+
#define warnf(fc, fmt, ...) __logfc(fc, 'w', fmt, ## __VA_ARGS__)
206210

207211
/**
208212
* errorf - Store supplementary error message
@@ -212,7 +216,7 @@ void logfc(struct fs_context *fc, const char *fmt, ...);
212216
* Store the supplementary error message for the process if the process has
213217
* enabled the facility.
214218
*/
215-
#define errorf(fc, fmt, ...) ({ logfc(fc, "e "fmt, ## __VA_ARGS__); })
219+
#define errorf(fc, fmt, ...) __logfc(fc, 'e', fmt, ## __VA_ARGS__)
216220

217221
/**
218222
* invalf - Store supplementary invalid argument error message
@@ -222,6 +226,6 @@ void logfc(struct fs_context *fc, const char *fmt, ...);
222226
* Store the supplementary error message for the process if the process has
223227
* enabled the facility and return -EINVAL.
224228
*/
225-
#define invalf(fc, fmt, ...) ({ errorf(fc, fmt, ## __VA_ARGS__); -EINVAL; })
229+
#define invalf(fc, fmt, ...) (errorf(fc, fmt, ## __VA_ARGS__), -EINVAL)
226230

227231
#endif /* _LINUX_FS_CONTEXT_H */

0 commit comments

Comments
 (0)