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
1111 */
1212
1313 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14+#include <linux/module.h>
1415 #include <linux/fs_context.h>
1516 #include <linux/fs_parser.h>
1617 #include <linux/fs.h>
@@ -23,6 +24,7 @@ · 6 unmodified lines
2324 #include <linux/pid_namespace.h>
2425 #include <linux/user_namespace.h>
2526 #include <net/net_namespace.h>
27+#include <asm/sections.h>
2628 #include "mount.h"
2729 #include "internal.h"
2830
@@ -365,6 +367,8 @@ struct fs_context *vfs_dup_fs_context(struct fs_context *src_fc) · 336 unmodified lines
365367 get_net(fc->net_ns);
366368 get_user_ns(fc->user_ns);
367369 get_cred(fc->cred);
370+ if (fc->log)
371+ refcount_inc(&fc->log->usage);
368372
369373 /* Can't call put until we've called ->dup */
370374 ret = fc->ops->dup(fc, src_fc);
@@ -382,7 +386,6 @@ err_fc: · 11 unmodified lines
382386 }
383387 EXPORT_SYMBOL(vfs_dup_fs_context);
384388
385−#ifdef CONFIG_PRINTK
386389 /**
387390 * logfc - Log a message to a filesystem context
388391 * @fc: The filesystem context to log to.
@@ -390,27 +393,100 @@ EXPORT_SYMBOL(vfs_dup_fs_context); · 1 unmodified line
390393 */
391394 void logfc(struct fs_context *fc, const char *fmt, ...)
392395 {
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;
393399 va_list va;
400+ char *q;
401+ u8 freeable;
394402
395403 va_start(va, fmt);
396−
397− switch (fmt[0]) {
398− case 'w':
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;
404+ if (!strchr(fmt, '%')) {
405+ p = fmt;
406+ goto unformatted_string;
407407 }
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);
414+copied_string:
415+ if (!q)
416+ goto store_failure;
417+ freeable = 1;
418+ goto store_string;
419+
420+unformatted_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+
429+store_failure:
430+ p = store_failure;
431+const_string:
432+ q = (char *)p;
433+ freeable = 0;
434+store_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+ }
408462
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+ }
410468 va_end(va);
411469 }
412470 EXPORT_SYMBOL(logfc);
413−#endif
471+
472+/*
473+ * Free a logging structure.
474+ */
475+static 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+}
414490
415491 /**
416492 * put_fs_context - Dispose of a superblock configuration context.
@@ -435,6 +511,7 @@ void put_fs_context(struct fs_context *fc) · 18 unmodified lines
435511 put_user_ns(fc->user_ns);
436512 put_cred(fc->cred);
437513 kfree(fc->subtype);
514+ put_fc_log(fc);
438515 put_filesystem(fc->fs_type);
439516 kfree(fc->source);
440517 kfree(fc);
fs/fsopen.c+67−0
@@ -20,6 +20,52 @@ · 19 unmodified lines
2020 #include <uapi/linux/mount.h>
2121 #include "mount.h"
2222
23+/*
24+ * Allow the user to read back any error, warning or informational messages.
25+ */
26+static 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+
63+err_free:
64+ if (need_free)
65+ kfree(p);
66+ return ret;
67+}
68+
2369 static int fscontext_release(struct inode *inode, struct file *file)
2470 {
2571 struct fs_context *fc = file->private_data;
@@ -32,6 +78,7 @@ static int fscontext_release(struct inode *inode, struct file *file) · 6 unmodified lines
3278 }
3379
3480 const struct file_operations fscontext_fops = {
81+ .read = fscontext_read,
3582 .release = fscontext_release,
3683 .llseek = no_llseek,
3784 };
@@ -50,6 +97,16 @@ static int fscontext_create_fd(struct fs_context *fc, unsigned int o_flags) · 12 unmodified lines
5097 return fd;
5198 }
5299
100+static 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+
53110 /*
54111 * Open a filesystem by name so that it can be configured for mounting.
55112 *
@@ -62,6 +119,7 @@ SYSCALL_DEFINE2(fsopen, const char __user *, _fs_name, unsigned int, flags) · 6 unmodified lines
62119 struct file_system_type *fs_type;
63120 struct fs_context *fc;
64121 const char *fs_name;
122+ int ret;
65123
66124 if (!ns_capable(current->nsproxy->mnt_ns->user_ns, CAP_SYS_ADMIN))
67125 return -EPERM;
@@ -84,5 +142,14 @@ SYSCALL_DEFINE2(fsopen, const char __user *, _fs_name, unsigned int, flags) · 16 unmodified lines
84142 return PTR_ERR(fc);
85143
86144 fc->phase = FS_CONTEXT_CREATE_PARAMS;
145+
146+ ret = fscontext_alloc_log(fc);
147+ if (ret < 0)
148+ goto err_fc;
149+
87150 return fscontext_create_fd(fc, flags & FSOPEN_CLOEXEC ? O_CLOEXEC : 0);
151+
152+err_fc:
153+ put_fs_context(fc);
154+ return ret;
88155 }
include/linux/fs_context.h+15−7
@@ -13,6 +13,7 @@ · 12 unmodified lines
1313 #define _LINUX_FS_CONTEXT_H
1414
1515 #include <linux/kernel.h>
16+#include <linux/refcount.h>
1617 #include <linux/errno.h>
1718 #include <linux/security.h>
1819 #include <linux/mutex.h>
@@ -95,6 +96,7 @@ struct fs_context { · 76 unmodified lines
9596 struct user_namespace *user_ns; /* The user namespace for this mount */
9697 struct net *net_ns; /* The network namespace for this mount */
9798 const struct cred *cred; /* The mounter's credentials */
99+ struct fc_log *log; /* Logging buffer */
98100 const char *source; /* The source name (eg. dev path) */
99101 const char *subtype; /* The subtype to set on the superblock */
100102 void *security; /* Linux S&M options */
@@ -151,15 +153,21 @@ extern int vfs_get_super(struct fs_context *fc, · 50 unmodified lines
151153
152154 extern const struct file_operations fscontext_fops;
153155
154−#ifdef CONFIG_PRINTK
156+/*
157+ * Mount error, warning and informational message logging. This structure is
158+ * shareable between a mount and a subordinate mount.
159+ */
160+struct 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+
155169 extern __attribute__((format(printf, 2, 3)))
156170 void logfc(struct fs_context *fc, const char *fmt, ...);
157−#else
158−static inline __attribute__((format(printf, 2, 3)))
159−void logfc(struct fs_context *fc, const char *fmt, ...)
160−{
161−}
162−#endif
163171
164172 /**
165173 * 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
709709 return false;
710710 }
711711
712+static inline bool within_module_core(unsigned long addr,
713+ const struct module *mod)
714+{
715+ return false;
716+}
717+
712718 /* Get/put a kernel symbol (calls should be symmetric) */
713719 #define symbol_get(x) ({ extern typeof(x) x __attribute__((weak)); &(x); })
714720 #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