2018-06-06 10:42:14 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2008-06-23 11:34:09 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 Christoph Hellwig.
|
|
|
|
* Portions Copyright (C) 2000-2008 Silicon Graphics, Inc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "xfs.h"
|
2019-06-29 10:25:35 +08:00
|
|
|
#include "xfs_shared.h"
|
2013-10-23 07:51:50 +08:00
|
|
|
#include "xfs_format.h"
|
2013-08-12 18:49:23 +08:00
|
|
|
#include "xfs_log_format.h"
|
2013-10-15 06:17:51 +08:00
|
|
|
#include "xfs_da_format.h"
|
2008-06-23 11:34:09 +08:00
|
|
|
#include "xfs_inode.h"
|
|
|
|
#include "xfs_attr.h"
|
|
|
|
|
|
|
|
#include <linux/posix_acl_xattr.h>
|
|
|
|
#include <linux/xattr.h>
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2016-04-11 08:48:24 +08:00
|
|
|
xfs_xattr_get(const struct xattr_handler *handler, struct dentry *unused,
|
|
|
|
struct inode *inode, const char *name, void *value, size_t size)
|
2008-06-23 11:34:09 +08:00
|
|
|
{
|
2015-10-05 01:18:51 +08:00
|
|
|
int xflags = handler->flags;
|
2016-04-11 08:48:24 +08:00
|
|
|
struct xfs_inode *ip = XFS_I(inode);
|
2008-06-23 11:34:09 +08:00
|
|
|
int error, asize = size;
|
|
|
|
|
|
|
|
/* Convert Linux syscall to XFS internal ATTR flags */
|
|
|
|
if (!size) {
|
|
|
|
xflags |= ATTR_KERNOVAL;
|
|
|
|
value = NULL;
|
|
|
|
}
|
|
|
|
|
xfs: allocate xattr buffer on demand
When doing file lookups and checking for permissions, we end up in
xfs_get_acl() to see if there are any ACLs on the inode. This
requires and xattr lookup, and to do that we have to supply a buffer
large enough to hold an maximum sized xattr.
On workloads were we are accessing a wide range of cache cold files
under memory pressure (e.g. NFS fileservers) we end up spending a
lot of time allocating the buffer. The buffer is 64k in length, so
is a contiguous multi-page allocation, and if that then fails we
fall back to vmalloc(). Hence the allocation here is /expensive/
when we are looking up hundreds of thousands of files a second.
Initial numbers from a bpf trace show average time in xfs_get_acl()
is ~32us, with ~19us of that in the memory allocation. Note these
are average times, so there are going to be affected by the worst
case allocations more than the common fast case...
To avoid this, we could just do a "null" lookup to see if the ACL
xattr exists and then only do the allocation if it exists. This,
however, optimises the path for the "no ACL present" case at the
expense of the "acl present" case. i.e. we can halve the time in
xfs_get_acl() for the no acl case (i.e down to ~10-15us), but that
then increases the ACL case by 30% (i.e. up to 40-45us).
To solve this and speed up both cases, drive the xattr buffer
allocation into the attribute code once we know what the actual
xattr length is. For the no-xattr case, we avoid the allocation
completely, speeding up that case. For the common ACL case, we'll
end up with a fast heap allocation (because it'll be smaller than a
page), and only for the rarer "we have a remote xattr" will we have
a multi-page allocation occur. Hence the common ACL case will be
much faster, too.
Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
2019-08-30 00:04:10 +08:00
|
|
|
error = xfs_attr_get(ip, name, (unsigned char **)&value, &asize, xflags);
|
2008-06-23 11:34:09 +08:00
|
|
|
if (error)
|
|
|
|
return error;
|
|
|
|
return asize;
|
|
|
|
}
|
|
|
|
|
2015-11-03 09:56:17 +08:00
|
|
|
void
|
|
|
|
xfs_forget_acl(
|
|
|
|
struct inode *inode,
|
|
|
|
const char *name,
|
|
|
|
int xflags)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Invalidate any cached ACLs if the user has bypassed the ACL
|
|
|
|
* interface. We don't validate the content whatsoever so it is caller
|
|
|
|
* responsibility to provide data in valid format and ensure i_mode is
|
|
|
|
* consistent.
|
|
|
|
*/
|
|
|
|
if (xflags & ATTR_ROOT) {
|
|
|
|
#ifdef CONFIG_XFS_POSIX_ACL
|
|
|
|
if (!strcmp(name, SGI_ACL_FILE))
|
|
|
|
forget_cached_acl(inode, ACL_TYPE_ACCESS);
|
|
|
|
else if (!strcmp(name, SGI_ACL_DEFAULT))
|
|
|
|
forget_cached_acl(inode, ACL_TYPE_DEFAULT);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-23 11:34:09 +08:00
|
|
|
static int
|
2016-05-27 22:19:30 +08:00
|
|
|
xfs_xattr_set(const struct xattr_handler *handler, struct dentry *unused,
|
|
|
|
struct inode *inode, const char *name, const void *value,
|
|
|
|
size_t size, int flags)
|
2008-06-23 11:34:09 +08:00
|
|
|
{
|
2015-11-14 10:02:30 +08:00
|
|
|
int xflags = handler->flags;
|
2016-05-27 22:19:30 +08:00
|
|
|
struct xfs_inode *ip = XFS_I(inode);
|
2015-11-03 09:40:59 +08:00
|
|
|
int error;
|
2008-06-23 11:34:09 +08:00
|
|
|
|
|
|
|
/* Convert Linux syscall to XFS internal ATTR flags */
|
|
|
|
if (flags & XATTR_CREATE)
|
|
|
|
xflags |= ATTR_CREATE;
|
|
|
|
if (flags & XATTR_REPLACE)
|
|
|
|
xflags |= ATTR_REPLACE;
|
|
|
|
|
|
|
|
if (!value)
|
2014-06-25 12:58:08 +08:00
|
|
|
return xfs_attr_remove(ip, (unsigned char *)name, xflags);
|
2015-11-03 09:40:59 +08:00
|
|
|
error = xfs_attr_set(ip, (unsigned char *)name,
|
2010-01-20 07:47:48 +08:00
|
|
|
(void *)value, size, xflags);
|
2015-11-03 09:56:17 +08:00
|
|
|
if (!error)
|
2016-05-27 22:19:30 +08:00
|
|
|
xfs_forget_acl(inode, name, xflags);
|
2015-11-03 09:40:59 +08:00
|
|
|
|
|
|
|
return error;
|
2008-06-23 11:34:09 +08:00
|
|
|
}
|
|
|
|
|
2010-05-14 08:53:20 +08:00
|
|
|
static const struct xattr_handler xfs_xattr_user_handler = {
|
2008-06-23 11:34:09 +08:00
|
|
|
.prefix = XATTR_USER_PREFIX,
|
2009-11-13 17:52:56 +08:00
|
|
|
.flags = 0, /* no flags implies user namespace */
|
|
|
|
.get = xfs_xattr_get,
|
|
|
|
.set = xfs_xattr_set,
|
2008-06-23 11:34:09 +08:00
|
|
|
};
|
|
|
|
|
2010-05-14 08:53:20 +08:00
|
|
|
static const struct xattr_handler xfs_xattr_trusted_handler = {
|
2008-06-23 11:34:09 +08:00
|
|
|
.prefix = XATTR_TRUSTED_PREFIX,
|
2009-11-13 17:52:56 +08:00
|
|
|
.flags = ATTR_ROOT,
|
|
|
|
.get = xfs_xattr_get,
|
|
|
|
.set = xfs_xattr_set,
|
2008-06-23 11:34:09 +08:00
|
|
|
};
|
|
|
|
|
2010-05-14 08:53:20 +08:00
|
|
|
static const struct xattr_handler xfs_xattr_security_handler = {
|
2008-06-23 11:34:09 +08:00
|
|
|
.prefix = XATTR_SECURITY_PREFIX,
|
2009-11-13 17:52:56 +08:00
|
|
|
.flags = ATTR_SECURE,
|
|
|
|
.get = xfs_xattr_get,
|
|
|
|
.set = xfs_xattr_set,
|
2008-06-23 11:34:09 +08:00
|
|
|
};
|
|
|
|
|
2010-05-14 08:53:20 +08:00
|
|
|
const struct xattr_handler *xfs_xattr_handlers[] = {
|
2008-06-23 11:34:09 +08:00
|
|
|
&xfs_xattr_user_handler,
|
|
|
|
&xfs_xattr_trusted_handler,
|
|
|
|
&xfs_xattr_security_handler,
|
2009-06-10 23:07:47 +08:00
|
|
|
#ifdef CONFIG_XFS_POSIX_ACL
|
2013-12-20 21:16:50 +08:00
|
|
|
&posix_acl_access_xattr_handler,
|
|
|
|
&posix_acl_default_xattr_handler,
|
2009-06-10 23:07:47 +08:00
|
|
|
#endif
|
2008-06-23 11:34:09 +08:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2016-12-05 09:32:14 +08:00
|
|
|
static void
|
2015-12-02 21:44:40 +08:00
|
|
|
__xfs_xattr_put_listent(
|
2010-01-20 07:47:48 +08:00
|
|
|
struct xfs_attr_list_context *context,
|
2015-12-02 21:44:40 +08:00
|
|
|
char *prefix,
|
|
|
|
int prefix_len,
|
|
|
|
unsigned char *name,
|
|
|
|
int namelen)
|
2008-06-23 11:34:09 +08:00
|
|
|
{
|
|
|
|
char *offset;
|
|
|
|
int arraytop;
|
|
|
|
|
2019-02-14 03:15:17 +08:00
|
|
|
if (context->count < 0 || context->seen_enough)
|
|
|
|
return;
|
|
|
|
|
2015-12-02 21:44:40 +08:00
|
|
|
if (!context->alist)
|
|
|
|
goto compute_size;
|
2008-06-23 11:34:09 +08:00
|
|
|
|
|
|
|
arraytop = context->count + prefix_len + namelen + 1;
|
|
|
|
if (arraytop > context->firstu) {
|
|
|
|
context->count = -1; /* insufficient space */
|
2016-09-14 05:40:35 +08:00
|
|
|
context->seen_enough = 1;
|
2016-12-05 09:32:14 +08:00
|
|
|
return;
|
2008-06-23 11:34:09 +08:00
|
|
|
}
|
|
|
|
offset = (char *)context->alist + context->count;
|
2015-12-02 21:44:40 +08:00
|
|
|
strncpy(offset, prefix, prefix_len);
|
2008-06-23 11:34:09 +08:00
|
|
|
offset += prefix_len;
|
2010-01-20 07:47:48 +08:00
|
|
|
strncpy(offset, (char *)name, namelen); /* real name */
|
2008-06-23 11:34:09 +08:00
|
|
|
offset += namelen;
|
|
|
|
*offset = '\0';
|
2015-12-02 21:44:40 +08:00
|
|
|
|
|
|
|
compute_size:
|
2008-06-23 11:34:09 +08:00
|
|
|
context->count += prefix_len + namelen + 1;
|
2016-12-05 09:32:14 +08:00
|
|
|
return;
|
2008-06-23 11:34:09 +08:00
|
|
|
}
|
|
|
|
|
2016-12-05 09:32:14 +08:00
|
|
|
static void
|
2015-12-02 21:44:40 +08:00
|
|
|
xfs_xattr_put_listent(
|
2010-01-20 07:47:48 +08:00
|
|
|
struct xfs_attr_list_context *context,
|
|
|
|
int flags,
|
|
|
|
unsigned char *name,
|
|
|
|
int namelen,
|
2016-04-06 05:57:32 +08:00
|
|
|
int valuelen)
|
2008-06-23 11:34:09 +08:00
|
|
|
{
|
2015-12-02 21:44:40 +08:00
|
|
|
char *prefix;
|
|
|
|
int prefix_len;
|
2008-06-23 11:34:09 +08:00
|
|
|
|
2015-12-02 21:44:40 +08:00
|
|
|
ASSERT(context->count >= 0);
|
2008-06-23 11:34:09 +08:00
|
|
|
|
2015-12-02 21:44:40 +08:00
|
|
|
if (flags & XFS_ATTR_ROOT) {
|
|
|
|
#ifdef CONFIG_XFS_POSIX_ACL
|
|
|
|
if (namelen == SGI_ACL_FILE_SIZE &&
|
|
|
|
strncmp(name, SGI_ACL_FILE,
|
|
|
|
SGI_ACL_FILE_SIZE) == 0) {
|
2016-12-05 09:32:14 +08:00
|
|
|
__xfs_xattr_put_listent(
|
2015-12-02 21:44:40 +08:00
|
|
|
context, XATTR_SYSTEM_PREFIX,
|
|
|
|
XATTR_SYSTEM_PREFIX_LEN,
|
|
|
|
XATTR_POSIX_ACL_ACCESS,
|
|
|
|
strlen(XATTR_POSIX_ACL_ACCESS));
|
|
|
|
} else if (namelen == SGI_ACL_DEFAULT_SIZE &&
|
|
|
|
strncmp(name, SGI_ACL_DEFAULT,
|
|
|
|
SGI_ACL_DEFAULT_SIZE) == 0) {
|
2016-12-05 09:32:14 +08:00
|
|
|
__xfs_xattr_put_listent(
|
2015-12-02 21:44:40 +08:00
|
|
|
context, XATTR_SYSTEM_PREFIX,
|
|
|
|
XATTR_SYSTEM_PREFIX_LEN,
|
|
|
|
XATTR_POSIX_ACL_DEFAULT,
|
|
|
|
strlen(XATTR_POSIX_ACL_DEFAULT));
|
|
|
|
}
|
|
|
|
#endif
|
2008-06-23 11:34:09 +08:00
|
|
|
|
2015-12-02 21:44:40 +08:00
|
|
|
/*
|
|
|
|
* Only show root namespace entries if we are actually allowed to
|
|
|
|
* see them.
|
|
|
|
*/
|
|
|
|
if (!capable(CAP_SYS_ADMIN))
|
2016-12-05 09:32:14 +08:00
|
|
|
return;
|
2015-12-02 21:44:40 +08:00
|
|
|
|
|
|
|
prefix = XATTR_TRUSTED_PREFIX;
|
|
|
|
prefix_len = XATTR_TRUSTED_PREFIX_LEN;
|
|
|
|
} else if (flags & XFS_ATTR_SECURE) {
|
|
|
|
prefix = XATTR_SECURITY_PREFIX;
|
|
|
|
prefix_len = XATTR_SECURITY_PREFIX_LEN;
|
|
|
|
} else {
|
|
|
|
prefix = XATTR_USER_PREFIX;
|
|
|
|
prefix_len = XATTR_USER_PREFIX_LEN;
|
|
|
|
}
|
|
|
|
|
2016-12-05 09:32:14 +08:00
|
|
|
__xfs_xattr_put_listent(context, prefix, prefix_len, name,
|
|
|
|
namelen);
|
|
|
|
return;
|
2008-06-23 11:34:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
2016-04-06 05:57:18 +08:00
|
|
|
xfs_vn_listxattr(
|
|
|
|
struct dentry *dentry,
|
|
|
|
char *data,
|
|
|
|
size_t size)
|
2008-06-23 11:34:09 +08:00
|
|
|
{
|
|
|
|
struct xfs_attr_list_context context;
|
|
|
|
struct attrlist_cursor_kern cursor = { 0 };
|
2016-04-06 05:57:18 +08:00
|
|
|
struct inode *inode = d_inode(dentry);
|
|
|
|
int error;
|
2008-06-23 11:34:09 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* First read the regular on-disk attributes.
|
|
|
|
*/
|
|
|
|
memset(&context, 0, sizeof(context));
|
|
|
|
context.dp = XFS_I(inode);
|
|
|
|
context.cursor = &cursor;
|
|
|
|
context.resynch = 1;
|
2015-12-02 21:44:40 +08:00
|
|
|
context.alist = size ? data : NULL;
|
2008-06-23 11:34:09 +08:00
|
|
|
context.bufsize = size;
|
|
|
|
context.firstu = context.bufsize;
|
2015-12-02 21:44:40 +08:00
|
|
|
context.put_listent = xfs_xattr_put_listent;
|
2008-06-23 11:34:09 +08:00
|
|
|
|
2016-04-06 05:57:18 +08:00
|
|
|
error = xfs_attr_list_int(&context);
|
|
|
|
if (error)
|
|
|
|
return error;
|
2008-06-23 11:34:09 +08:00
|
|
|
if (context.count < 0)
|
|
|
|
return -ERANGE;
|
|
|
|
|
|
|
|
return context.count;
|
|
|
|
}
|