From: Allison Henderson <allison.henderson@oracle.com>
To: linux-xfs@vger.kernel.org
Subject: [PATCH v9 27/28] xfs: Add parent pointer ioctl
Date: Wed, 26 Sep 2018 03:15:06 -0700 [thread overview]
Message-ID: <1537956907-10244-28-git-send-email-allison.henderson@oracle.com> (raw)
In-Reply-To: <1537956907-10244-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 | 45 ++++++++++++++++++++++++
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 | 86 +++++++++++++++++++++++++++++++++++++++++++++-
fs/xfs/xfs_ondisk.h | 4 +++
fs/xfs/xfs_parent_utils.c | 84 ++++++++++++++++++++++++++++++++++++++++++++
fs/xfs/xfs_parent_utils.h | 2 ++
8 files changed, 235 insertions(+), 1 deletion(-)
diff --git a/fs/xfs/libxfs/xfs_fs.h b/fs/xfs/libxfs/xfs_fs.h
index 9c58a78..5d28033 100644
--- a/fs/xfs/libxfs/xfs_fs.h
+++ b/fs/xfs/libxfs/xfs_fs.h
@@ -547,6 +547,50 @@ struct xfs_scrub_metadata {
XFS_SCRUB_OFLAG_NO_REPAIR_NEEDED)
#define XFS_SCRUB_FLAGS_ALL (XFS_SCRUB_FLAGS_IN | XFS_SCRUB_FLAGS_OUT)
+#define XFS_PPTR_MAXNAMELEN 256
+
+/* return parents of the handle, not the open fd */
+#define XFS_PPTR_IFLAG_HANDLE (1U << 0)
+
+/* target was the root directory */
+#define XFS_PPTR_OFLAG_ROOT (1U << 1)
+
+/* Cursor is done iterating pptrs */
+#define XFS_PPTR_OFLAG_DONE (1U << 2)
+
+/* 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 */
+ __u32 xpp_pad;
+ __u8 xpp_name[XFS_PPTR_MAXNAMELEN]; /* File name */
+};
+
+/* Iterate through 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_parent_ptr 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
*/
@@ -591,6 +635,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 c82caf0..ea1e69a 100644
--- a/fs/xfs/libxfs/xfs_parent.c
+++ b/fs/xfs/libxfs/xfs_parent.c
@@ -33,6 +33,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 60f1172..3fcbbbb 100644
--- a/fs/xfs/libxfs/xfs_parent.h
+++ b/fs/xfs/libxfs/xfs_parent.h
@@ -32,4 +32,6 @@ void xfs_init_parent_name_irec(struct xfs_parent_name_irec *irec,
int xfs_parent_add(struct xfs_inode *parent,
struct xfs_inode *child, struct xfs_name *child_name,
uint32_t diroffset);
+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 3c30ec4..f4ffc2c 100644
--- a/fs/xfs/xfs_attr_list.c
+++ b/fs/xfs/xfs_attr_list.c
@@ -570,6 +570,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 b0a4b37..b0791f3 100644
--- a/fs/xfs/xfs_ioctl.c
+++ b/fs/xfs/xfs_ioctl.c
@@ -35,6 +35,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>
@@ -1730,6 +1732,87 @@ xfs_ioc_scrub_metadata(
return 0;
}
+/*
+ * IOCTL routine to get the parent pointers of an inode and return it to user
+ * space. Caller must pass a buffer space containing a struct xfs_pptr_info,
+ * followed by a region large enough to contain an array of struct
+ * xfs_parent_ptr of a size specified in pi_ptrs_size. If the inode contains
+ * more parent pointers than can fit in the buffer space, caller may re-call
+ * the function using the returned pi_cursor to resume iteration. The
+ * number of xfs_parent_ptr returned will be stored in pi_ptrs_used.
+ *
+ * Returns 0 on success or non-zero on failure
+ */
+STATIC int
+xfs_ioc_get_parent_pointer(
+ struct file *filp,
+ void __user *arg)
+{
+ struct xfs_pptr_info *ppi = NULL;
+ int error = 0;
+ struct xfs_inode *ip = XFS_I(file_inode(filp));
+ struct xfs_mount *mp = ip->i_mount;
+
+ if (!capable(CAP_SYS_ADMIN))
+ return -EPERM;
+
+ /* 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 */
+ error = copy_from_user(ppi, arg, sizeof(struct xfs_pptr_info));
+ if (error)
+ goto out;
+
+ /* Check size of buffer requested by user */
+ if (XFS_PPTR_INFO_SIZEOF(ppi->pi_ptrs_size) > XFS_XATTR_LIST_MAX) {
+ error = -ENOMEM;
+ goto out;
+ }
+
+ /*
+ * 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 != 0 && ppi->pi_flags != XFS_PPTR_IFLAG_HANDLE) {
+ error = -EINVAL;
+ goto out;
+ }
+
+ if (ppi->pi_flags == XFS_PPTR_IFLAG_HANDLE) {
+ error = xfs_iget(mp, NULL, ppi->pi_handle.ha_fid.fid_ino,
+ 0, 0, &ip);
+ if (error)
+ goto out;
+ }
+
+ if (ip->i_ino == mp->m_sb.sb_rootino)
+ ppi->pi_flags |= XFS_PPTR_OFLAG_ROOT;
+
+ /* Get the parent pointers */
+ error = xfs_attr_get_parent_pointer(ip, ppi);
+
+ if (error)
+ goto out;
+
+ /* Copy the parent pointers back to the user */
+ error = copy_to_user(arg, ppi,
+ XFS_PPTR_INFO_SIZEOF(ppi->pi_ptrs_size));
+ if (error)
+ goto out;
+
+out:
+ kmem_free(ppi);
+ return error;
+}
+
int
xfs_ioc_swapext(
xfs_swapext_t *sxp)
@@ -1972,7 +2055,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_ondisk.h b/fs/xfs/xfs_ondisk.h
index 9397b6b..a9ff19c 100644
--- a/fs/xfs/xfs_ondisk.h
+++ b/fs/xfs/xfs_ondisk.h
@@ -127,6 +127,10 @@ xfs_check_ondisk_structs(void)
XFS_CHECK_STRUCT_SIZE(struct xfs_trans_header, 16);
XFS_CHECK_STRUCT_SIZE(struct xfs_attri_log_format, 40);
XFS_CHECK_STRUCT_SIZE(struct xfs_attrd_log_format, 16);
+
+ /* parent pointer ioctls */
+ XFS_CHECK_STRUCT_SIZE(struct xfs_parent_ptr, 280);
+ XFS_CHECK_STRUCT_SIZE(struct xfs_pptr_info, 104);
}
#endif /* __XFS_ONDISK_H */
diff --git a/fs/xfs/xfs_parent_utils.c b/fs/xfs/xfs_parent_utils.c
index ae9a319..1b862ad 100644
--- a/fs/xfs/xfs_parent_utils.c
+++ b/fs/xfs/xfs_parent_utils.c
@@ -30,6 +30,7 @@
#include "xfs_da_btree.h"
#include "xfs_attr.h"
#include "xfs_parent.h"
+#include "xfs_da_btree.h"
/*
* Add a parent record to an inode with existing parent records.
@@ -68,3 +69,86 @@ xfs_parent_remove_deferred(
sizeof(rec), 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;
+ struct xfs_attr_list_context context;
+ struct xfs_da_args args;
+
+ /* 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_context_init(ip, namebuf, namebuf_size, flags,
+ (attrlist_cursor_kern_t *)&ppi->pi_cursor, &context);
+ if (error)
+ goto out_kfree;
+
+ xfs_ilock(ip, XFS_ILOCK_EXCL);
+
+ error = xfs_attr_list_int_ilocked(&context);
+ 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_args_init(&args, ip, (char *)xpnr,
+ sizeof(struct xfs_parent_name_rec), flags);
+ if (error)
+ goto out_kfree;
+
+ args.value = (unsigned char *)(xpp->xpp_name);
+ args.valuelen = name_len;
+ args.op_flags = XFS_DA_OP_OKNOENT;
+
+ error = xfs_attr_get_ilocked(ip, &args);
+ error = (error == -EEXIST ? 0 : error);
+ if (error)
+ goto out_kfree;
+
+ xpp->xpp_namelen = name_len;
+ xfs_init_parent_ptr(xpp, xpnr);
+ }
+ ppi->pi_ptrs_used = alist->al_count;
+ if (!alist->al_more)
+ ppi->pi_flags |= XFS_PPTR_OFLAG_DONE;
+
+out_kfree:
+ xfs_iunlock(ip, XFS_ILOCK_EXCL);
+ kmem_free(namebuf);
+
+ return error;
+}
+
diff --git a/fs/xfs/xfs_parent_utils.h b/fs/xfs/xfs_parent_utils.h
index 010e517..5f5a3e2 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_trans *tp,
struct xfs_inode *child,
xfs_dir2_dataptr_t diroffset);
+int xfs_attr_get_parent_pointer(struct xfs_inode *ip,
+ struct xfs_pptr_info *ppi);
#endif /* __XFS_PARENT_UTILS_H__ */
--
2.7.4
next prev parent reply other threads:[~2018-09-26 16:29 UTC|newest]
Thread overview: 33+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-26 10:14 [PATCH v9 00/28] Parent Pointers v9 Allison Henderson
2018-09-26 10:14 ` [PATCH v9 01/28] xfs: Move fs/xfs/xfs_attr.h to fs/xfs/libxfs/xfs_attr.h Allison Henderson
2018-09-26 10:14 ` [PATCH v9 02/28] xfs: Add helper function xfs_attr_try_sf_addname Allison Henderson
2018-10-06 3:01 ` Darrick J. Wong
2018-10-06 3:13 ` Dave Chinner
2018-09-26 10:14 ` [PATCH v9 03/28] xfs: Add attibute set and helper functions Allison Henderson
2018-10-06 2:51 ` Dave Chinner
2018-09-26 10:14 ` [PATCH v9 04/28] xfs: Add attibute remove " Allison Henderson
2018-09-26 10:14 ` [PATCH v9 05/28] xfs: Hold inode locks in xfs_ialloc Allison Henderson
2018-10-06 2:53 ` Dave Chinner
2018-09-26 10:14 ` [PATCH v9 06/28] xfs: Add trans toggle to attr routines Allison Henderson
2018-09-26 10:14 ` [PATCH v9 07/28] xfs: Set up infastructure for deferred attribute operations Allison Henderson
2018-09-26 10:14 ` [PATCH v9 08/28] xfs: Add xfs_attr_set_deferred and xfs_attr_remove_deferred Allison Henderson
2018-09-26 10:14 ` [PATCH v9 09/28] xfs: Add xfs_has_attr and subroutines Allison Henderson
2018-09-26 10:14 ` [PATCH v9 10/28] xfs: Add attr context to log item Allison Henderson
2018-09-26 10:14 ` [PATCH v9 11/28] xfs: Roll delayed attr operations by returning EAGAIN Allison Henderson
2018-09-26 10:14 ` [PATCH v9 12/28] xfs: Remove roll_trans boolean Allison Henderson
2018-09-26 10:14 ` [PATCH v9 13/28] xfs: Remove all strlen calls in all xfs_attr_* functions for attr names Allison Henderson
2018-09-26 10:14 ` [PATCH v9 14/28] xfs: get directory offset when adding directory name Allison Henderson
2018-09-26 10:14 ` [PATCH v9 15/28] xfs: get directory offset when removing " Allison Henderson
2018-09-26 10:14 ` [PATCH v9 16/28] xfs: get directory offset when replacing a " Allison Henderson
2018-09-26 10:14 ` [PATCH v9 17/28] xfs: add parent pointer support to attribute code Allison Henderson
2018-09-26 10:14 ` [PATCH v9 18/28] xfs: define parent pointer xattr format Allison Henderson
2018-09-26 10:14 ` [PATCH v9 19/28] xfs: extent transaction reservations for parent attributes Allison Henderson
2018-09-26 10:14 ` [PATCH v9 20/28] xfs: parent pointer attribute creation Allison Henderson
2018-09-26 10:15 ` [PATCH v9 21/28] xfs: add parent attributes to link Allison Henderson
2018-09-26 10:15 ` [PATCH v9 22/28] xfs: remove parent pointers in unlink Allison Henderson
2018-09-26 10:15 ` [PATCH v9 23/28] xfs: Add parent pointers to rename Allison Henderson
2018-09-26 10:15 ` [PATCH v9 24/28] xfs: Add the parent pointer support to the superblock version 5 Allison Henderson
2018-09-26 10:15 ` [PATCH v9 25/28] xfs: Add helper function xfs_attr_list_context_init Allison Henderson
2018-09-26 10:15 ` [PATCH v9 26/28] xfs: Increase XFS_DEFER_OPS_NR_INODES to 4 Allison Henderson
2018-09-26 10:15 ` Allison Henderson [this message]
2018-09-26 10:15 ` [PATCH v9 28/28] xfs: Add delayed attributes error tag 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=1537956907-10244-28-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).