History

vfs: Implement logging through fs_context

Implement the ability for filesystems to log error, warning and informational messages through the fs_context. These can be extracted by userspace by reading from an fd created by fsopen(). Error messages are prefixed with "e ", warnings with "w " and informational messages with "i ". Inside the kernel, formatted messages are malloc'd but unformatted messages are not copied if they're either in the core .rodata section or in the .rodata section of the filesystem module pinned by fs_context::fs_type. The messages are only good till the fs_type is released. Note that the logging object is shared between duplicated fs_context structures. This is so that such as NFS which do a mount within a mount can get at least some of the errors from the inner mount. Five logging functions are provided for this: (1) void logfc(struct fs_context *fc, const char *fmt, ...); This logs a message into the context. If the buffer is full, the earliest message is discarded. (2) void errorf(fc, fmt, ...); This wraps logfc() to log an error. (3) void invalf(fc, fmt, ...); This wraps errorf() and returns -EINVAL for convenience. (4) void warnf(fc, fmt, ...); This wraps logfc() to log a warning. (5) void infof(fc, fmt, ...); This wraps logfc() to log an informational message.

David Howells committed 8 years ago

Commit 007ec261 parent 24dcb3d4 files · +179 −21Browse files

Unified · Split

4 files
  1. fs/fs_context.c +91 −14
  2. fs/fsopen.c +67 −0
  3. include/linux/fs_context.h +15 −7
  4. include/linux/module.h +6 −0
fs/fs_context.c+91−14
@@ -11,6 +11,7 @@ · 10 unmodified lines
11 */11 */
12 12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14#include <linux/module.h>
14#include <linux/fs_context.h>15#include <linux/fs_context.h>
15#include <linux/fs_parser.h>16#include <linux/fs_parser.h>
16#include <linux/fs.h>17#include <linux/fs.h>
@@ -23,6 +24,7 @@ · 6 unmodified lines
23#include <linux/pid_namespace.h>24#include <linux/pid_namespace.h>
24#include <linux/user_namespace.h>25#include <linux/user_namespace.h>
25#include <net/net_namespace.h>26#include <net/net_namespace.h>
27#include <asm/sections.h>
26#include "mount.h"28#include "mount.h"
27#include "internal.h"29#include "internal.h"
28 30
@@ -365,6 +367,8 @@ struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc) · 336 unmodified lines
365 get_net(fc->net_ns);367 get_net(fc->net_ns);
366 get_user_ns(fc->user_ns);368 get_user_ns(fc->user_ns);
367 get_cred(fc->cred);369 get_cred(fc->cred);
370 if (fc->log)
371 refcount_inc(&fc->log->usage);
368 372
369 /* Can't call put until we've called ->dup */373 /* Can't call put until we've called ->dup */
370 ret = fc->ops->dup(fc, src_fc);374 ret = fc->ops->dup(fc, src_fc);
@@ -382,7 +386,6 @@ err_fc: · 11 unmodified lines
382}386}
383EXPORT_SYMBOL(vfs_dup_fs_context);387EXPORT_SYMBOL(vfs_dup_fs_context);
384 388
385#ifdef CONFIG_PRINTK
386/**389/**
387 * logfc - Log a message to a filesystem context390 * logfc - Log a message to a filesystem context
388 * @fc: The filesystem context to log to.391 * @fc: The filesystem context to log to.
@@ -390,27 +393,100 @@ EXPORT_SYMBOL(vfs_dup_fs_context); · 1 unmodified line
390 */393 */
391void logfc(struct fs_context *fc, const char *fmt, ...)394void logfc(struct fs_context *fc, const char *fmt, ...)
392{395{
396 static const char store_failure[] = "OOM: Can't store error string";
397 struct fc_log *log = fc ? fc->log : NULL;
398 const char *p;
393 va_list va;399 va_list va;
400 char *q;
401 u8 freeable;
394 402
395 va_start(va, fmt);403 va_start(va, fmt);
396 404 if (!strchr(fmt, '%')) {
397 switch (fmt[0]) {405 p = fmt;
398 case 'w':406 goto unformatted_string;
399 vprintk_emit(0, LOGLEVEL_WARNING, NULL, 0, fmt, va);
400 break;
401 case 'e':
402 vprintk_emit(0, LOGLEVEL_ERR, NULL, 0, fmt, va);
403 break;
404 default:
405 vprintk_emit(0, LOGLEVEL_NOTICE, NULL, 0, fmt, va);
406 break;
407 }407 }
408 if (strcmp(fmt, "%s") == 0) {
409 p = va_arg(va, const char *);
410 goto unformatted_string;
411 }
412
413 q = kvasprintf(GFP_KERNEL, fmt, va);
414copied_string:
415 if (!q)
416 goto store_failure;
417 freeable = 1;
418 goto store_string;
419
420unformatted_string:
421 if ((unsigned long)p >= (unsigned long)__start_rodata &&
422 (unsigned long)p < (unsigned long)__end_rodata)
423 goto const_string;
424 if (log && within_module_core((unsigned long)p, log->owner))
425 goto const_string;
426 q = kstrdup(p, GFP_KERNEL);
427 goto copied_string;
428
429store_failure:
430 p = store_failure;
431const_string:
432 q = (char *)p;
433 freeable = 0;
434store_string:
435 if (!log) {
436 switch (fmt[0]) {
437 case 'w':
438 printk(KERN_WARNING "%s\n", q + 2);
439 break;
440 case 'e':
441 printk(KERN_ERR "%s\n", q + 2);
442 break;
443 default:
444 printk(KERN_NOTICE "%s\n", q + 2);
445 break;
446 }
447 if (freeable)
448 kfree(q);
449 } else {
450 unsigned int logsize = ARRAY_SIZE(log->buffer);
451 u8 index;
452
453 index = log->head & (logsize - 1);
454 BUILD_BUG_ON(sizeof(log->head) != sizeof(u8) ||
455 sizeof(log->tail) != sizeof(u8));
456 if ((u8)(log->head - log->tail) == logsize) {
457 /* The buffer is full, discard the oldest message */
458 if (log->need_free & (1 << index))
459 kfree(log->buffer[index]);
460 log->tail++;
461 }
408 462
409 pr_cont("\n");463 log->buffer[index] = q;
464 log->need_free &= ~(1 << index);
465 log->need_free |= freeable << index;
466 log->head++;
467 }
410 va_end(va);468 va_end(va);
411}469}
412EXPORT_SYMBOL(logfc);470EXPORT_SYMBOL(logfc);
413#endif471
472/*
473 * Free a logging structure.
474 */
475static void put_fc_log(struct fs_context *fc)
476{
477 struct fc_log *log = fc->log;
478 int i;
479
480 if (log) {
481 if (refcount_dec_and_test(&log->usage)) {
482 fc->log = NULL;
483 for (i = 0; i <= 7; i++)
484 if (log->need_free & (1 << i))
485 kfree(log->buffer[i]);
486 kfree(log);
487 }
488 }
489}
414 490
415/**491/**
416 * put_fs_context - Dispose of a superblock configuration context.492 * put_fs_context - Dispose of a superblock configuration context.
@@ -435,6 +511,7 @@ void put_fs_context(struct fs_context *fc) · 18 unmodified lines
435 put_user_ns(fc->user_ns);511 put_user_ns(fc->user_ns);
436 put_cred(fc->cred);512 put_cred(fc->cred);
437 kfree(fc->subtype);513 kfree(fc->subtype);
514 put_fc_log(fc);
438 put_filesystem(fc->fs_type);515 put_filesystem(fc->fs_type);
439 kfree(fc->source);516 kfree(fc->source);
440 kfree(fc);517 kfree(fc);
fs/fsopen.c+67−0
@@ -20,6 +20,52 @@ · 19 unmodified lines
20#include <uapi/linux/mount.h>20#include <uapi/linux/mount.h>
21#include "mount.h"21#include "mount.h"
22 22
23/*
24 * Allow the user to read back any error, warning or informational messages.
25 */
26static ssize_t fscontext_read(struct file *file,
27 char __user *_buf, size_t len, loff_t *pos)
28{
29 struct fs_context *fc = file->private_data;
30 struct fc_log *log = fc->log;
31 unsigned int logsize = ARRAY_SIZE(log->buffer);
32 ssize_t ret;
33 char *p;
34 bool need_free;
35 int index, n;
36
37 ret = mutex_lock_interruptible(&fc->uapi_mutex);
38 if (ret < 0)
39 return ret;
40
41 if (log->head == log->tail) {
42 mutex_unlock(&fc->uapi_mutex);
43 return -ENODATA;
44 }
45
46 index = log->tail & (logsize - 1);
47 p = log->buffer[index];
48 need_free = log->need_free & (1 << index);
49 log->buffer[index] = NULL;
50 log->need_free &= ~(1 << index);
51 log->tail++;
52 mutex_unlock(&fc->uapi_mutex);
53
54 ret = -EMSGSIZE;
55 n = strlen(p);
56 if (n > len)
57 goto err_free;
58 ret = -EFAULT;
59 if (copy_to_user(_buf, p, n) != 0)
60 goto err_free;
61 ret = n;
62
63err_free:
64 if (need_free)
65 kfree(p);
66 return ret;
67}
68
23static int fscontext_release(struct inode *inode, struct file *file)69static int fscontext_release(struct inode *inode, struct file *file)
24{70{
25 struct fs_context *fc = file->private_data;71 struct fs_context *fc = file->private_data;
@@ -32,6 +78,7 @@ static int fscontext_release(struct inode *inode, struct file *file) · 6 unmodified lines
32}78}
33 79
34const struct file_operations fscontext_fops = {80const struct file_operations fscontext_fops = {
81 .read = fscontext_read,
35 .release = fscontext_release,82 .release = fscontext_release,
36 .llseek = no_llseek,83 .llseek = no_llseek,
37};84};
@@ -50,6 +97,16 @@ static int fscontext_create_fd(struct fs_context *fc, unsigned int o_flags) · 12 unmodified lines
50 return fd;97 return fd;
51}98}
52 99
100static int fscontext_alloc_log(struct fs_context *fc)
101{
102 fc->log = kzalloc(sizeof(*fc->log), GFP_KERNEL);
103 if (!fc->log)
104 return -ENOMEM;
105 refcount_set(&fc->log->usage, 1);
106 fc->log->owner = fc->fs_type->owner;
107 return 0;
108}
109
53/*110/*
54 * Open a filesystem by name so that it can be configured for mounting.111 * Open a filesystem by name so that it can be configured for mounting.
55 *112 *
@@ -62,6 +119,7 @@ SYSCALL_DEFINE2(fsopen, const char __user *, _fs_name, unsigned int, flags) · 6 unmodified lines
62 struct file_system_type *fs_type;119 struct file_system_type *fs_type;
63 struct fs_context *fc;120 struct fs_context *fc;
64 const char *fs_name;121 const char *fs_name;
122 int ret;
65 123
66 if (!ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN))124 if (!ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN))
67 return -EPERM;125 return -EPERM;
@@ -84,5 +142,14 @@ SYSCALL_DEFINE2(fsopen, const char __user *, _fs_name, unsigned int, flags) · 16 unmodified lines
84 return PTR_ERR(fc);142 return PTR_ERR(fc);
85 143
86 fc->phase = FS_CONTEXT_CREATE_PARAMS;144 fc->phase = FS_CONTEXT_CREATE_PARAMS;
145
146 ret = fscontext_alloc_log(fc);
147 if (ret < 0)
148 goto err_fc;
149
87 return fscontext_create_fd(fc, flags & FSOPEN_CLOEXEC ? O_CLOEXEC : 0);150 return fscontext_create_fd(fc, flags & FSOPEN_CLOEXEC ? O_CLOEXEC : 0);
151
152err_fc:
153 put_fs_context(fc);
154 return ret;
88}155}
include/linux/fs_context.h+15−7
@@ -13,6 +13,7 @@ · 12 unmodified lines
13#define _LINUX_FS_CONTEXT_H13#define _LINUX_FS_CONTEXT_H
14 14
15#include <linux/kernel.h>15#include <linux/kernel.h>
16#include <linux/refcount.h>
16#include <linux/errno.h>17#include <linux/errno.h>
17#include <linux/security.h>18#include <linux/security.h>
18#include <linux/mutex.h>19#include <linux/mutex.h>
@@ -95,6 +96,7 @@ struct fs_context { · 76 unmodified lines
95 struct user_namespace *user_ns; /* The user namespace for this mount */96 struct user_namespace *user_ns; /* The user namespace for this mount */
96 struct net *net_ns; /* The network namespace for this mount */97 struct net *net_ns; /* The network namespace for this mount */
97 const struct cred *cred; /* The mounter's credentials */98 const struct cred *cred; /* The mounter's credentials */
99 struct fc_log *log; /* Logging buffer */
98 const char *source; /* The source name (eg. dev path) */100 const char *source; /* The source name (eg. dev path) */
99 const char *subtype; /* The subtype to set on the superblock */101 const char *subtype; /* The subtype to set on the superblock */
100 void *security; /* Linux S&M options */102 void *security; /* Linux S&M options */
@@ -151,15 +153,21 @@ extern int vfs_get_super(struct fs_context *fc, · 50 unmodified lines
151 153
152extern const struct file_operations fscontext_fops;154extern const struct file_operations fscontext_fops;
153 155
154#ifdef CONFIG_PRINTK156/*
157 * Mount error, warning and informational message logging. This structure is
158 * shareable between a mount and a subordinate mount.
159 */
160struct fc_log {
161 refcount_t usage;
162 u8 head; /* Insertion index in buffer[] */
163 u8 tail; /* Removal index in buffer[] */
164 u8 need_free; /* Mask of kfree'able items in buffer[] */
165 struct module *owner; /* Owner module for strings that don't then need freeing */
166 char *buffer[8];
167};
168
155extern __attribute__((format(printf, 2, 3)))169extern __attribute__((format(printf, 2, 3)))
156void logfc(struct fs_context *fc, const char *fmt, ...);170void logfc(struct fs_context *fc, const char *fmt, ...);
157#else
158static inline __attribute__((format(printf, 2, 3)))
159void logfc(struct fs_context *fc, const char *fmt, ...)
160{
161}
162#endif
163 171
164/**172/**
165 * infof - Store supplementary informational message173 * infof - Store supplementary informational message
include/linux/module.h+6−0
@@ -709,6 +709,12 @@ static inline bool is_module_text_address(unsigned long addr) · 708 unmodified lines
709 return false;709 return false;
710}710}
711 711
712static inline bool within_module_core(unsigned long addr,
713 const struct module *mod)
714{
715 return false;
716}
717
712/* Get/put a kernel symbol (calls should be symmetric) */718/* Get/put a kernel symbol (calls should be symmetric) */
713#define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })719#define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
714#define symbol_put(x) do { } while (0)720#define symbol_put(x) do { } while (0)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t