linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Miklos Szeredi <mszeredi@redhat.com>
To: linux-fsdevel@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Al Viro <viro@zeniv.linux.org.uk>
Subject: [PATCH 10/17] nsfs: clean up ns_get_name() interface
Date: Mon, 12 Sep 2016 21:29:12 +0200	[thread overview]
Message-ID: <1473708559-12714-11-git-send-email-mszeredi@redhat.com> (raw)
In-Reply-To: <1473708559-12714-1-git-send-email-mszeredi@redhat.com>

The only user of this is proc_ns_get_link(), which needs an allocated
buffer.  And ns_get_name() is much better positioned to know the size of
the buffer to allocate, since it's the one that is going to fill it.

Signed-off-by: Miklos Szeredi <mszeredi@redhat.com>
---
 fs/nsfs.c               | 17 +++++++++++++----
 fs/proc/namespaces.c    | 20 +++-----------------
 include/linux/proc_ns.h |  4 ++--
 3 files changed, 18 insertions(+), 23 deletions(-)

diff --git a/fs/nsfs.c b/fs/nsfs.c
index 8f20d6016e20..33ec85b91aba 100644
--- a/fs/nsfs.c
+++ b/fs/nsfs.c
@@ -5,6 +5,7 @@
 #include <linux/magic.h>
 #include <linux/ktime.h>
 #include <linux/seq_file.h>
+#include <linux/slab.h>
 
 static struct vfsmount *nsfs_mnt;
 
@@ -106,14 +107,22 @@ slow:
 	goto got_it;
 }
 
-int ns_get_name(char *buf, size_t size, struct task_struct *task,
-			const struct proc_ns_operations *ns_ops)
+char *ns_get_name(struct task_struct *task,
+		  const struct proc_ns_operations *ns_ops)
 {
 	struct ns_common *ns;
-	int res = -ENOENT;
+	char *res = ERR_PTR(-ENOENT);
+
 	ns = ns_ops->get(task);
 	if (ns) {
-		res = snprintf(buf, size, "%s:[%u]", ns_ops->name, ns->inum);
+		/* 10 for unsigned int in decimal + 3 extra chars + term null */
+		size_t size = strlen(ns_ops->name) + 14;
+
+		res = kmalloc(size, GFP_KERNEL);
+		if (!res)
+			res = ERR_PTR(-ENOMEM);
+		else
+			snprintf(res, size, "%s:[%u]", ns_ops->name, ns->inum);
 		ns_ops->put(ns);
 	}
 	return res;
diff --git a/fs/proc/namespaces.c b/fs/proc/namespaces.c
index 9c9a1683791a..9c7209734b05 100644
--- a/fs/proc/namespaces.c
+++ b/fs/proc/namespaces.c
@@ -33,8 +33,6 @@ static const struct proc_ns_operations *ns_entries[] = {
 #endif
 };
 
-#define PROC_NS_LINK_MAX 50
-
 static const char *proc_ns_get_link(struct dentry *dentry,
 				    struct inode *inode,
 				    struct delayed_call *done)
@@ -59,21 +57,9 @@ static const char *proc_ns_get_link(struct dentry *dentry,
 		if (!res)
 			nd_jump_link(&ns_path);
 	} else {
-		char *name = kmalloc(PROC_NS_LINK_MAX, GFP_KERNEL);
-		int err;
-
-		res = ERR_PTR(-ENOMEM);
-		if (!name)
-			goto out_put;
-
-		err = ns_get_name(name, PROC_NS_LINK_MAX, task, ns_ops);
-		if (err < 0) {
-			kfree(name);
-			res = ERR_PTR(err);
-			goto out_put;
-		}
-		set_delayed_call(done, kfree_link, name);
-		res = name;
+		res = ns_get_name(task, ns_ops);
+		if (!IS_ERR(res))
+			set_delayed_call(done, kfree_link, res);
 	}
 out_put:
 	put_task_struct(task);
diff --git a/include/linux/proc_ns.h b/include/linux/proc_ns.h
index de0e7719d4c5..9ab3fb9b9101 100644
--- a/include/linux/proc_ns.h
+++ b/include/linux/proc_ns.h
@@ -74,8 +74,8 @@ extern struct file *proc_ns_fget(int fd);
 extern void *ns_get_path(struct path *path, struct task_struct *task,
 			const struct proc_ns_operations *ns_ops);
 
-extern int ns_get_name(char *buf, size_t size, struct task_struct *task,
-			const struct proc_ns_operations *ns_ops);
+char *ns_get_name(struct task_struct *task,
+		  const struct proc_ns_operations *ns_ops);
 extern void nsfs_init(void);
 
 #endif /* _LINUX_PROC_NS_H */
-- 
2.5.5


  parent reply	other threads:[~2016-09-12 19:29 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-09-12 19:29 [PATCH 00/17] clean up readlinks Miklos Szeredi
2016-09-12 19:29 ` [PATCH 01/17] bad_inode: add missing i_op initializers Miklos Szeredi
2016-09-12 19:29 ` [PATCH 02/17] ovl: use generic_readlink Miklos Szeredi
2016-09-12 19:29 ` [PATCH 03/17] proc/self: " Miklos Szeredi
2016-09-12 19:29 ` [PATCH 04/17] afs: " Miklos Szeredi
2016-09-12 19:29 ` [PATCH 05/17] bad_inode: " Miklos Szeredi
2016-09-12 19:29 ` [PATCH 06/17] vfs: remove page_readlink() Miklos Szeredi
2016-09-12 19:29 ` [PATCH 07/17] vfs: add is_following_link() helper Miklos Szeredi
2016-09-12 19:29 ` [PATCH 08/17] proc: merge proc_pid_readlink() into proc_pid_get_link() Miklos Szeredi
2016-09-12 19:29 ` [PATCH 09/17] proc: merge proc_ns_readlink() into proc_ns_get_link() Miklos Szeredi
2016-09-12 19:29 ` Miklos Szeredi [this message]
2016-09-12 19:29 ` [PATCH 11/17] vfs: replace calling i_op->readlink with vfs_readlink() Miklos Szeredi
2016-09-12 19:29 ` [PATCH 12/17] vfs: remove ".readlink = generic_readlink" assignments Miklos Szeredi
2016-09-12 19:29 ` [PATCH 13/17] vfs: remove unused i_op->readlink Miklos Szeredi
2016-09-12 19:29 ` [PATCH 14/17] vfs: remove unused generic_readlink() Miklos Szeredi
2016-09-12 19:29 ` [PATCH 15/17] vfs: add vfs_get_link() helper Miklos Szeredi
2016-09-12 19:29 ` [PATCH 16/17] ovl: use vfs_get_link() Miklos Szeredi
2016-09-12 19:29 ` [PATCH 17/17] ecryptfs: " Miklos Szeredi
2016-09-27  3:10 ` [PATCH 00/17] clean up readlinks Al Viro
2016-09-27  9:38   ` Miklos Szeredi
2016-09-28  2:17     ` Al Viro
2016-09-28 14:47       ` Miklos Szeredi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1473708559-12714-11-git-send-email-mszeredi@redhat.com \
    --to=mszeredi@redhat.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).