linux-xfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Allison Henderson <allison.henderson@oracle.com>
To: linux-xfs@vger.kernel.org
Subject: [PATCH 20/21] xfs: Add parent pointer ioctl
Date: Sun,  6 May 2018 10:24:53 -0700	[thread overview]
Message-ID: <1525627494-12873-21-git-send-email-allison.henderson@oracle.com> (raw)
In-Reply-To: <1525627494-12873-1-git-send-email-allison.henderson@oracle.com>

This patch adds a new file ioctl to retrieve the parent
pointer of a given inode

Signed-off-by: Allison Henderson <allison.henderson@oracle.com>
---
 fs/xfs/libxfs/xfs_fs.h     | 38 ++++++++++++++++++++++++++
 fs/xfs/libxfs/xfs_parent.c | 10 +++++++
 fs/xfs/libxfs/xfs_parent.h |  2 ++
 fs/xfs/xfs_attr_list.c     |  3 +++
 fs/xfs/xfs_ioctl.c         | 61 +++++++++++++++++++++++++++++++++++++++++-
 fs/xfs/xfs_parent_utils.c  | 66 ++++++++++++++++++++++++++++++++++++++++++++++
 fs/xfs/xfs_parent_utils.h  |  2 ++
 7 files changed, 181 insertions(+), 1 deletion(-)

diff --git a/fs/xfs/libxfs/xfs_fs.h b/fs/xfs/libxfs/xfs_fs.h
index 641e0af..4e0ccdd 100644
--- a/fs/xfs/libxfs/xfs_fs.h
+++ b/fs/xfs/libxfs/xfs_fs.h
@@ -552,6 +552,43 @@ struct xfs_scrub_metadata {
 				 XFS_SCRUB_OFLAG_WARNING)
 #define XFS_SCRUB_FLAGS_ALL	(XFS_SCRUB_FLAGS_IN | XFS_SCRUB_FLAGS_OUT)
 
+#define XFS_PPTR_MAXNAMELEN				255
+
+/* return parents of the handle, not the open fd */
+#define XFS_PPTR_IFLAG_HANDLE  (1U << 0)
+
+/* Get an inode parent pointer through ioctl */
+struct xfs_parent_ptr {
+	__u64		xpp_ino;			/* Inode */
+	__u32		xpp_gen;			/* Inode generation */
+	__u32		xpp_diroffset;			/* Directory offset */
+	__u32		xpp_namelen;			/* File name length */
+	__u8		xpp_name[XFS_PPTR_MAXNAMELEN];	/* File name */
+};
+
+/* Iterate though an inodes parent pointers */
+struct xfs_pptr_info {
+	struct xfs_handle		pi_handle;
+	struct xfs_attrlist_cursor	pi_cursor;
+	__u32				pi_flags;
+	__u32				pi_reserved;
+	__u32				pi_ptrs_size;
+	__u32				pi_ptrs_used;
+	__u64				pi_reserved2[6];
+
+	/*
+	 * An array of struct xfs_pptr follows the header
+	 * information. Use XFS_PPINFO_TO_PP() to access the
+	 * parent pointer array entries.
+	 */
+};
+
+#define XFS_PPTR_INFO_SIZEOF(nr_ptrs) sizeof (struct xfs_pptr_info) + \
+				      nr_ptrs * sizeof(struct xfs_parent_ptr)
+
+#define XFS_PPINFO_TO_PP(info, idx)    \
+	(&(((struct xfs_parent_ptr *)((char *)(info) + sizeof(*(info))))[(idx)]))
+
 /*
  * ioctl limits
  */
@@ -596,6 +633,7 @@ struct xfs_scrub_metadata {
 #define XFS_IOC_FREE_EOFBLOCKS	_IOR ('X', 58, struct xfs_fs_eofblocks)
 /*	XFS_IOC_GETFSMAP ------ hoisted 59         */
 #define XFS_IOC_SCRUB_METADATA	_IOWR('X', 60, struct xfs_scrub_metadata)
+#define XFS_IOC_GETPPOINTER	_IOR ('X', 61, struct xfs_parent_ptr)
 
 /*
  * ioctl commands that replace IRIX syssgi()'s
diff --git a/fs/xfs/libxfs/xfs_parent.c b/fs/xfs/libxfs/xfs_parent.c
index e6de97c..61f1961 100644
--- a/fs/xfs/libxfs/xfs_parent.c
+++ b/fs/xfs/libxfs/xfs_parent.c
@@ -32,6 +32,16 @@
 #include "xfs_attr_sf.h"
 #include "xfs_bmap.h"
 
+/* Initializes a xfs_parent_ptr from an xfs_parent_name_rec */
+void
+xfs_init_parent_ptr(struct xfs_parent_ptr		*xpp,
+		     struct xfs_parent_name_rec	*rec)
+{
+	xpp->xpp_ino = be64_to_cpu(rec->p_ino);
+	xpp->xpp_gen = be32_to_cpu(rec->p_gen);
+	xpp->xpp_diroffset = be32_to_cpu(rec->p_diroffset);
+}
+
 /*
  * Parent pointer attribute handling.
  *
diff --git a/fs/xfs/libxfs/xfs_parent.h b/fs/xfs/libxfs/xfs_parent.h
index 298562b..1a321db 100644
--- a/fs/xfs/libxfs/xfs_parent.h
+++ b/fs/xfs/libxfs/xfs_parent.h
@@ -33,4 +33,6 @@ int xfs_parent_add(struct xfs_trans *tp, struct xfs_inode *parent,
 		   struct xfs_inode *child, struct xfs_name *child_name,
 		   uint32_t diroffset, xfs_fsblock_t *firstblock,
 		   struct xfs_defer_ops *dfops);
+void xfs_init_parent_ptr(struct xfs_parent_ptr *xpp,
+			 struct xfs_parent_name_rec *rec);
 #endif	/* __XFS_PARENT_H__ */
diff --git a/fs/xfs/xfs_attr_list.c b/fs/xfs/xfs_attr_list.c
index 3e59a34..bdbe9fb 100644
--- a/fs/xfs/xfs_attr_list.c
+++ b/fs/xfs/xfs_attr_list.c
@@ -581,6 +581,9 @@ xfs_attr_put_listent(
 	if (((context->flags & ATTR_ROOT) == 0) !=
 	    ((flags & XFS_ATTR_ROOT) == 0))
 		return;
+	if (((context->flags & ATTR_PARENT) == 0) !=
+	    ((flags & XFS_ATTR_PARENT) == 0))
+		return;
 
 	arraytop = sizeof(*alist) +
 			context->count * sizeof(alist->al_offset[0]);
diff --git a/fs/xfs/xfs_ioctl.c b/fs/xfs/xfs_ioctl.c
index 844480a..ee544f2 100644
--- a/fs/xfs/xfs_ioctl.c
+++ b/fs/xfs/xfs_ioctl.c
@@ -46,6 +46,8 @@
 #include "xfs_fsmap.h"
 #include "scrub/xfs_scrub.h"
 #include "xfs_sb.h"
+#include "xfs_da_format.h"
+#include "xfs_parent_utils.h"
 
 #include <linux/capability.h>
 #include <linux/cred.h>
@@ -1738,6 +1740,62 @@ xfs_ioc_scrub_metadata(
 	return 0;
 }
 
+/*
+ * IOCTL routine to get the parent pointer of an inode and return it to user
+ * space.  Caller must pass an struct xfs_parent_name_irec with a name buffer
+ * large enough to hold the file name.  Returns 0 on success or non-zero on
+ * failure
+ */
+STATIC int
+xfs_ioc_get_parent_pointer(
+	struct file			*filp,
+	void				__user *arg)
+{
+	struct xfs_inode		*ip;
+	struct xfs_pptr_info		*ppi;
+	struct dentry			*dentry;
+	int				error = 0;
+
+	/* Allocate an xfs_pptr_info to put the user data */
+	ppi = kmem_alloc(sizeof(struct xfs_pptr_info), KM_SLEEP);
+	if (!ppi)
+		return -ENOMEM;
+
+	/* Copy the data from the user */
+	copy_from_user(ppi, arg, sizeof(struct xfs_pptr_info));
+
+	/*
+	 * Now that we know how big the trailing buffer is, expand
+	 * our kernel xfs_pptr_info to be the same size
+	 */
+	ppi = kmem_realloc(ppi, XFS_PPTR_INFO_SIZEOF(ppi->pi_ptrs_size),
+			     KM_SLEEP);
+	if (!ppi)
+		return -ENOMEM;
+
+	if (ppi->pi_flags == XFS_PPTR_IFLAG_HANDLE) {
+		dentry = xfs_handle_to_dentry(filp, &ppi->pi_handle,
+					      sizeof(struct xfs_handle));
+		if (IS_ERR(dentry))
+			return PTR_ERR(dentry);
+		ip = XFS_I(d_inode(dentry));
+	} else
+		ip = XFS_I(file_inode(filp));
+
+	/* Get the parent pointers */
+	error = xfs_attr_get_parent_pointer(ip, ppi);
+
+	if (error)
+		goto out;
+
+	/* Copy the parent pointers back to the user */
+	copy_to_user(arg, ppi, XFS_PPTR_INFO_SIZEOF(ppi->pi_ptrs_size));
+
+out:
+	kmem_free(ppi);
+	return error;
+}
+
 int
 xfs_ioc_swapext(
 	xfs_swapext_t	*sxp)
@@ -1894,7 +1952,8 @@ xfs_file_ioctl(
 		return xfs_ioc_getxflags(ip, arg);
 	case XFS_IOC_SETXFLAGS:
 		return xfs_ioc_setxflags(ip, filp, arg);
-
+	case XFS_IOC_GETPPOINTER:
+		return xfs_ioc_get_parent_pointer(filp, arg);
 	case XFS_IOC_FSSETDM: {
 		struct fsdmidata	dmi;
 
diff --git a/fs/xfs/xfs_parent_utils.c b/fs/xfs/xfs_parent_utils.c
index 0fd48b8..1df003a 100644
--- a/fs/xfs/xfs_parent_utils.c
+++ b/fs/xfs/xfs_parent_utils.c
@@ -68,3 +68,69 @@ xfs_parent_remove_deferred(
 					ATTR_PARENT);
 }
 
+/*
+ * Get the parent pointers for a given inode
+ *
+ * Returns 0 on success and non zero on error
+ */
+int
+xfs_attr_get_parent_pointer(struct xfs_inode		*ip,
+			     struct xfs_pptr_info	*ppi)
+
+{
+
+	struct attrlist			*alist;
+	struct attrlist_ent		*aent;
+	struct xfs_parent_ptr		*xpp;
+	struct xfs_parent_name_rec	*xpnr;
+	char				*namebuf;
+	unsigned int			namebuf_size;
+	int				name_len;
+	int				error = 0;
+	unsigned int			flags = ATTR_PARENT;
+	int				i;
+
+	/* Allocate a buffer to store the attribute names */
+	namebuf_size = sizeof(struct attrlist) +
+		       (ppi->pi_ptrs_size) * sizeof(struct attrlist_ent);
+	namebuf = kmem_zalloc_large(namebuf_size, KM_SLEEP);
+	if (!namebuf)
+		return -ENOMEM;
+
+	error = xfs_attr_list(ip, namebuf, namebuf_size, flags,
+			      (attrlist_cursor_kern_t *)&ppi->pi_cursor);
+	if (error)
+		goto out_kfree;
+
+	alist = (struct attrlist *)namebuf;
+
+	for (i = 0; i < alist->al_count; i++) {
+		xpp = XFS_PPINFO_TO_PP(ppi, i);
+		memset(xpp, 0, sizeof(struct xfs_parent_ptr));
+		aent = (struct attrlist_ent *) &namebuf[alist->al_offset[i]];
+		xpnr = (struct xfs_parent_name_rec *)(aent->a_name);
+
+		if (aent->a_valuelen > XFS_PPTR_MAXNAMELEN) {
+			error = -ERANGE;
+			goto out_kfree;
+		}
+
+		name_len = aent->a_valuelen;
+		error = xfs_attr_get(ip, (char *)xpnr,
+					sizeof(struct xfs_parent_name_rec),
+					(unsigned char *)(xpp->xpp_name),
+					&name_len, flags);
+		if (error)
+			goto out_kfree;
+
+		xpp->xpp_namelen = name_len;
+		xfs_init_parent_ptr(xpp, xpnr);
+	}
+	ppi->pi_ptrs_used = alist->al_count;
+
+out_kfree:
+	kmem_free(namebuf);
+
+	return error;
+}
+
diff --git a/fs/xfs/xfs_parent_utils.h b/fs/xfs/xfs_parent_utils.h
index 9e0ac13..33e3b2c 100644
--- a/fs/xfs/xfs_parent_utils.h
+++ b/fs/xfs/xfs_parent_utils.h
@@ -27,4 +27,6 @@ int xfs_parent_remove_deferred(struct xfs_inode *parent,
 			       struct xfs_inode *child,
 			       xfs_dir2_dataptr_t diroffset,
 			       struct xfs_defer_ops *dfops);
+int xfs_attr_get_parent_pointer(struct xfs_inode *ip,
+				struct xfs_pptr_info *ppi);
 #endif	/* __XFS_PARENT_UTILS_H__ */
-- 
2.7.4


  parent reply	other threads:[~2018-05-06 17:27 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-06 17:24 [PATCH 00/21] Parent Pointers v6 Allison Henderson
2018-05-06 17:24 ` [PATCH 01/21] xfs: Move fs/xfs/xfs_attr.h to fs/xfs/libxfs/xfs_attr.h Allison Henderson
2018-05-07 23:39   ` Darrick J. Wong
2018-05-06 17:24 ` [PATCH 02/21] Add trans toggle to attr routines Allison Henderson
2018-05-07 23:52   ` Darrick J. Wong
2018-05-08 17:04     ` Allison Henderson
2018-05-06 17:24 ` [PATCH 03/21] xfs: Add attibute set and helper functions Allison Henderson
2018-05-07 23:36   ` Darrick J. Wong
2018-05-08  7:25     ` Amir Goldstein
2018-05-08 17:02       ` Allison Henderson
2018-05-08 17:01     ` Allison Henderson
2018-05-06 17:24 ` [PATCH 04/21] xfs: Add attibute remove " Allison Henderson
2018-05-07 23:21   ` Darrick J. Wong
2018-05-08  7:33   ` Amir Goldstein
2018-05-08 17:02     ` Allison Henderson
2018-05-08 17:14     ` Darrick J. Wong
2018-05-06 17:24 ` [PATCH 05/21] xfs: Set up infastructure for deferred attribute operations Allison Henderson
2018-05-07 23:19   ` Darrick J. Wong
2018-05-08 17:01     ` Allison Henderson
2018-05-08  9:55   ` Amir Goldstein
2018-05-06 17:24 ` [PATCH 06/21] xfs: Add xfs_attr_set_deferred and xfs_attr_remove_deferred Allison Henderson
2018-05-07 22:59   ` Darrick J. Wong
2018-05-08 17:01     ` Allison Henderson
2018-05-06 17:24 ` [PATCH 07/21] xfs: Remove all strlen calls in all xfs_attr_* functions for attr names Allison Henderson
2018-05-07 22:54   ` Darrick J. Wong
2018-05-08 17:00     ` Allison Henderson
2018-05-06 17:24 ` [PATCH 08/21] xfs: get directory offset when adding directory name Allison Henderson
2018-05-07 22:50   ` Darrick J. Wong
2018-05-06 17:24 ` [PATCH 09/21] xfs: get directory offset when removing " Allison Henderson
2018-05-07 22:48   ` Darrick J. Wong
2018-05-08 17:00     ` Allison Henderson
2018-05-06 17:24 ` [PATCH 10/21] xfs: get directory offset when replacing a " Allison Henderson
2018-05-07 22:45   ` Darrick J. Wong
2018-05-08 17:00     ` Allison Henderson
2018-05-06 17:24 ` [PATCH 11/21] xfs: add parent pointer support to attribute code Allison Henderson
2018-05-07 22:36   ` Darrick J. Wong
2018-05-06 17:24 ` [PATCH 12/21] xfs: define parent pointer xattr format Allison Henderson
2018-05-07 22:35   ` Darrick J. Wong
2018-05-08 17:00     ` Allison Henderson
2018-05-06 17:24 ` [PATCH 13/21] xfs: extent transaction reservations for parent attributes Allison Henderson
2018-05-07 22:34   ` Darrick J. Wong
2018-05-08 17:00     ` Allison Henderson
2018-05-06 17:24 ` [PATCH 14/21] Add lock_flags to xfs_ialloc and xfs_dir_ialloc Allison Henderson
2018-05-07 22:30   ` Darrick J. Wong
2018-05-08 16:59     ` Allison Henderson
2018-05-06 17:24 ` [PATCH 15/21] xfs: parent pointer attribute creation Allison Henderson
2018-05-07 22:19   ` Darrick J. Wong
2018-05-08 16:58     ` Allison Henderson
2018-05-06 17:24 ` [PATCH 16/21] xfs: add parent attributes to link Allison Henderson
2018-05-07 22:12   ` Darrick J. Wong
2018-05-08 16:58     ` Allison Henderson
2018-05-06 17:24 ` [PATCH 17/21] xfs: remove parent pointers in unlink Allison Henderson
2018-05-07 21:59   ` Darrick J. Wong
2018-05-08 16:58     ` Allison Henderson
2018-05-06 17:24 ` [PATCH 18/21] xfs: Add parent pointers to rename Allison Henderson
2018-05-07 21:52   ` Darrick J. Wong
2018-05-08 16:58     ` Allison Henderson
2018-05-08 10:04   ` Amir Goldstein
2018-05-06 17:24 ` [PATCH 19/21] xfs: Add the parent pointer support to the superblock version 5 Allison Henderson
2018-05-07 21:38   ` Darrick J. Wong
2018-05-08 16:58     ` Allison Henderson
2018-05-06 17:24 ` Allison Henderson [this message]
2018-05-07 21:36   ` [PATCH 20/21] xfs: Add parent pointer ioctl Darrick J. Wong
2018-05-08 10:24     ` Amir Goldstein
2018-05-08 10:25       ` Amir Goldstein
2018-05-08 16:57     ` Allison Henderson
2018-05-15 16:27   ` Catalin Iacob
2018-05-15 16:52     ` Allison Henderson
2018-05-06 17:24 ` [PATCH 21/21] xfs: Add delayed attributes error tag Allison Henderson
2018-05-07 20:57   ` Darrick J. Wong
2018-05-08  5:36 ` [PATCH 00/21] Parent Pointers v6 Amir Goldstein
2018-05-08 17:03   ` Allison Henderson

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=1525627494-12873-21-git-send-email-allison.henderson@oracle.com \
    --to=allison.henderson@oracle.com \
    --cc=linux-xfs@vger.kernel.org \
    /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).