[PATCH v5 29/32] d_path: prt_path()

Matthew Wilcox (Oracle) posted 32 patches 3 years, 8 months ago
[PATCH v5 29/32] d_path: prt_path()
Posted by Matthew Wilcox (Oracle) 3 years, 8 months ago
From: Kent Overstreet <kent.overstreet@gmail.com>

This implements a new printbuf version of d_path()/mangle_path(), which
will replace the seq_buf version.

Part of what we're trying to do with printbufs is standardizing a
calling convention so they don't have to live in lib/vsprintf.c, and can
instead with the code for the types they're printing - so this patch
adds prt_path() to d_path.c, another patch will switch vsprintf.c to use
it.

Signed-off-by: Kent Overstreet <kent.overstreet@gmail.com>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
---
 fs/d_path.c            | 35 +++++++++++++++++++++++++++++++++++
 include/linux/dcache.h |  1 +
 2 files changed, 36 insertions(+)

diff --git a/fs/d_path.c b/fs/d_path.c
index e4e0ebad1f15..1bd9e85f2f65 100644
--- a/fs/d_path.c
+++ b/fs/d_path.c
@@ -5,6 +5,7 @@
 #include <linux/fs_struct.h>
 #include <linux/fs.h>
 #include <linux/slab.h>
+#include <linux/printbuf.h>
 #include <linux/prefetch.h>
 #include "mount.h"
 
@@ -294,6 +295,40 @@ char *d_path(const struct path *path, char *buf, int buflen)
 }
 EXPORT_SYMBOL(d_path);
 
+/**
+ * prt_path - format a path for output
+ * @out: printbuf to output to
+ * @path: path to write into the sequence buffer.
+ * @esc: set of characters to escape in the output
+ *
+ * Write a path name into the sequence buffer.
+ *
+ * Returns 0 on success, or error code from d_path
+ */
+int prt_path(struct printbuf *out, const struct path *path, const char *esc)
+{
+	char *p, *buf;
+	size_t size;
+again:
+	buf = out->buf + out->pos;
+	size = printbuf_remaining_size(out);
+
+	p = d_path(path, buf, size);
+	if (IS_ERR(p)) {
+		printbuf_make_room(out, max_t(size_t, 64, size * 2));
+		if (printbuf_remaining_size(out) > size)
+			goto again;
+
+		return PTR_ERR(p);
+	}
+
+	p = mangle_path(buf, p, esc);
+	if (p)
+		out->pos += p - buf;
+	return 0;
+}
+EXPORT_SYMBOL(prt_path);
+
 /*
  * Helper function for dentry_operations.d_dname() members
  */
diff --git a/include/linux/dcache.h b/include/linux/dcache.h
index c73e5e327e76..bdd5940aa75a 100644
--- a/include/linux/dcache.h
+++ b/include/linux/dcache.h
@@ -293,6 +293,7 @@ extern char *d_absolute_path(const struct path *, char *, int);
 extern char *d_path(const struct path *, char *, int);
 extern char *dentry_path_raw(const struct dentry *, char *, int);
 extern char *dentry_path(const struct dentry *, char *, int);
+extern int prt_path(struct printbuf *, const struct path *, const char *);
 
 /* Allocation counts.. */
 
-- 
2.35.1
Re: [PATCH v5 29/32] d_path: prt_path()
Posted by Al Viro 3 years, 8 months ago
On Mon, Aug 08, 2022 at 03:41:25AM +0100, Matthew Wilcox (Oracle) wrote:
> From: Kent Overstreet <kent.overstreet@gmail.com>
> 
> This implements a new printbuf version of d_path()/mangle_path(), which
> will replace the seq_buf version.
> 
> Part of what we're trying to do with printbufs is standardizing a
> calling convention so they don't have to live in lib/vsprintf.c, and can
> instead with the code for the types they're printing - so this patch
> adds prt_path() to d_path.c, another patch will switch vsprintf.c to use
> it.

To use it for *what*?  If you mean replacing %pd with it - forget about that.
Not going to happen.

There's a hard requirement for printk - it should be safe to call in any
locking environment.  d_path() isn't, and that's impossible to avoid.
Re: [PATCH v5 29/32] d_path: prt_path()
Posted by Kent Overstreet 3 years, 8 months ago
On 8/8/22 00:17, Al Viro wrote:
> On Mon, Aug 08, 2022 at 03:41:25AM +0100, Matthew Wilcox (Oracle) wrote:
>> From: Kent Overstreet <kent.overstreet@gmail.com>
>>
>> This implements a new printbuf version of d_path()/mangle_path(), which
>> will replace the seq_buf version.
>>
>> Part of what we're trying to do with printbufs is standardizing a
>> calling convention so they don't have to live in lib/vsprintf.c, and can
>> instead with the code for the types they're printing - so this patch
>> adds prt_path() to d_path.c, another patch will switch vsprintf.c to use
>> it.
> 
> To use it for *what*?  If you mean replacing %pd with it - forget about that.
> Not going to happen.
> 
> There's a hard requirement for printk - it should be safe to call in any
> locking environment.  d_path() isn't, and that's impossible to avoid.

Oof, good catch. It was to replace seq_buf_path() in this patch series, 
and is only used in trace_seq.c. I'll have to investigate what's going 
on in the tracing code, because now that you mention it that did look 
suspect.