From patchwork Wed Sep 26 10:15:06 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Allison Henderson X-Patchwork-Id: 10615541 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 84D62112B for ; Wed, 26 Sep 2018 10:17:32 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 844D72A76C for ; Wed, 26 Sep 2018 10:17:32 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 791372A76F; Wed, 26 Sep 2018 10:17:32 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AAA9D2A75F for ; Wed, 26 Sep 2018 10:17:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727250AbeIZQ3o (ORCPT ); Wed, 26 Sep 2018 12:29:44 -0400 Received: from aserp2120.oracle.com ([141.146.126.78]:48548 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727234AbeIZQ3o (ORCPT ); Wed, 26 Sep 2018 12:29:44 -0400 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.22/8.16.0.22) with SMTP id w8QADwMp079540 for ; Wed, 26 Sep 2018 10:17:29 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : subject : date : message-id : in-reply-to : references; s=corp-2018-07-02; bh=NqkoGUsugjinG9IQM/hDtLaTiub2Fqk7P8iwKuNCx/0=; b=dMHzahdGBeUsRNPcufYAhU3QS/0iu+FSgJ2zr1SnQeHKfjcTxvTaFd5TU1Sbs6ZVXxoe Ki9QDo3Ui4hHJoC1Wircex2k9ArE3K+BmawSWcdr8q8Gku/ZupAdEFuQG6R2BpdZ02EY b8ZHhGSZHWiOzNFTYUS3zgHaSQwny2AYhXgsgFLK/rsYsqWngMzlyAvreWeXRlnfQ1kN 8qiSZS2WA90ixP/jQgvIb3WG2brQW1ZrgpeXj9qWCZgTOXhlr4sjfVWNCqjSJvOUTV6t SnTp1BZ4Im00HFbUpQMfmu4otIbrYLJVxrqMkfZl2mn2ceQUBaCLW3M9qtrSKHweuhaA NQ== Received: from aserv0022.oracle.com (aserv0022.oracle.com [141.146.126.234]) by aserp2120.oracle.com with ESMTP id 2mndpphg6c-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 26 Sep 2018 10:17:29 +0000 Received: from aserv0121.oracle.com (aserv0121.oracle.com [141.146.126.235]) by aserv0022.oracle.com (8.14.4/8.14.4) with ESMTP id w8QAHSr9010015 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 26 Sep 2018 10:17:29 GMT Received: from abhmp0013.oracle.com (abhmp0013.oracle.com [141.146.116.19]) by aserv0121.oracle.com (8.14.4/8.13.8) with ESMTP id w8QAHSJc027777 for ; Wed, 26 Sep 2018 10:17:28 GMT Received: from localhost.localdomain (/70.176.225.12) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Wed, 26 Sep 2018 03:17:28 -0700 From: Allison Henderson 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 Message-Id: <1537956907-10244-28-git-send-email-allison.henderson@oracle.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1537956907-10244-1-git-send-email-allison.henderson@oracle.com> References: <1537956907-10244-1-git-send-email-allison.henderson@oracle.com> X-Proofpoint-Virus-Version: vendor=nai engine=5900 definitions=9027 signatures=668707 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=3 malwarescore=0 phishscore=0 bulkscore=0 spamscore=0 mlxscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1807170000 definitions=main-1809260103 Sender: linux-xfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This patch adds a new file ioctl to retrieve the parent pointer of a given inode Signed-off-by: Allison Henderson --- 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 #include @@ -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__ */