xfs_xattr.c 5.5 KB
Newer Older
D
Dave Chinner 已提交
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6 7
/*
 * Copyright (C) 2008 Christoph Hellwig.
 * Portions Copyright (C) 2000-2008 Silicon Graphics, Inc.
 */

#include "xfs.h"
8
#include "xfs_format.h"
9
#include "xfs_log_format.h"
10
#include "xfs_trans_resv.h"
11 12
#include "xfs_mount.h"
#include "xfs_da_format.h"
13 14 15 16 17 18 19 20 21 22
#include "xfs_inode.h"
#include "xfs_attr.h"
#include "xfs_attr_leaf.h"
#include "xfs_acl.h"

#include <linux/posix_acl_xattr.h>
#include <linux/xattr.h>


static int
23 24
xfs_xattr_get(const struct xattr_handler *handler, struct dentry *unused,
		struct inode *inode, const char *name, void *value, size_t size)
25
{
26
	int xflags = handler->flags;
27
	struct xfs_inode *ip = XFS_I(inode);
28 29 30 31 32 33 34 35
	int error, asize = size;

	/* Convert Linux syscall to XFS internal ATTR flags */
	if (!size) {
		xflags |= ATTR_KERNOVAL;
		value = NULL;
	}

D
Dave Chinner 已提交
36
	error = xfs_attr_get(ip, (unsigned char *)name, value, &asize, xflags);
37 38 39 40 41
	if (error)
		return error;
	return asize;
}

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
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
	}
}

64
static int
65 66 67
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)
68
{
69
	int			xflags = handler->flags;
70
	struct xfs_inode	*ip = XFS_I(inode);
71
	int			error;
72 73 74 75 76 77 78 79

	/* Convert Linux syscall to XFS internal ATTR flags */
	if (flags & XATTR_CREATE)
		xflags |= ATTR_CREATE;
	if (flags & XATTR_REPLACE)
		xflags |= ATTR_REPLACE;

	if (!value)
D
Dave Chinner 已提交
80
		return xfs_attr_remove(ip, (unsigned char *)name, xflags);
81
	error = xfs_attr_set(ip, (unsigned char *)name,
82
				(void *)value, size, xflags);
83
	if (!error)
84
		xfs_forget_acl(inode, name, xflags);
85 86

	return error;
87 88
}

S
Stephen Hemminger 已提交
89
static const struct xattr_handler xfs_xattr_user_handler = {
90
	.prefix	= XATTR_USER_PREFIX,
91 92 93
	.flags	= 0, /* no flags implies user namespace */
	.get	= xfs_xattr_get,
	.set	= xfs_xattr_set,
94 95
};

S
Stephen Hemminger 已提交
96
static const struct xattr_handler xfs_xattr_trusted_handler = {
97
	.prefix	= XATTR_TRUSTED_PREFIX,
98 99 100
	.flags	= ATTR_ROOT,
	.get	= xfs_xattr_get,
	.set	= xfs_xattr_set,
101 102
};

S
Stephen Hemminger 已提交
103
static const struct xattr_handler xfs_xattr_security_handler = {
104
	.prefix	= XATTR_SECURITY_PREFIX,
105 106 107
	.flags	= ATTR_SECURE,
	.get	= xfs_xattr_get,
	.set	= xfs_xattr_set,
108 109
};

S
Stephen Hemminger 已提交
110
const struct xattr_handler *xfs_xattr_handlers[] = {
111 112 113
	&xfs_xattr_user_handler,
	&xfs_xattr_trusted_handler,
	&xfs_xattr_security_handler,
114
#ifdef CONFIG_XFS_POSIX_ACL
115 116
	&posix_acl_access_xattr_handler,
	&posix_acl_default_xattr_handler,
117
#endif
118 119 120
	NULL
};

121
static void
122
__xfs_xattr_put_listent(
123
	struct xfs_attr_list_context *context,
124 125 126 127
	char *prefix,
	int prefix_len,
	unsigned char *name,
	int namelen)
128 129 130 131
{
	char *offset;
	int arraytop;

132 133
	if (!context->alist)
		goto compute_size;
134 135 136 137

	arraytop = context->count + prefix_len + namelen + 1;
	if (arraytop > context->firstu) {
		context->count = -1;	/* insufficient space */
138
		context->seen_enough = 1;
139
		return;
140 141
	}
	offset = (char *)context->alist + context->count;
142
	strncpy(offset, prefix, prefix_len);
143
	offset += prefix_len;
144
	strncpy(offset, (char *)name, namelen);			/* real name */
145 146
	offset += namelen;
	*offset = '\0';
147 148

compute_size:
149
	context->count += prefix_len + namelen + 1;
150
	return;
151 152
}

153
static void
154
xfs_xattr_put_listent(
155 156 157 158
	struct xfs_attr_list_context *context,
	int		flags,
	unsigned char	*name,
	int		namelen,
159
	int		valuelen)
160
{
161 162
	char *prefix;
	int prefix_len;
163

164
	ASSERT(context->count >= 0);
165

166 167 168 169 170
	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) {
171
			__xfs_xattr_put_listent(
172 173 174 175 176 177 178
					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) {
179
			__xfs_xattr_put_listent(
180 181 182 183 184 185
					context, XATTR_SYSTEM_PREFIX,
					XATTR_SYSTEM_PREFIX_LEN,
					XATTR_POSIX_ACL_DEFAULT,
					strlen(XATTR_POSIX_ACL_DEFAULT));
		}
#endif
186

187 188 189 190 191
		/*
		 * Only show root namespace entries if we are actually allowed to
		 * see them.
		 */
		if (!capable(CAP_SYS_ADMIN))
192
			return;
193 194 195 196 197 198 199 200 201 202 203

		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;
	}

204 205 206
	__xfs_xattr_put_listent(context, prefix, prefix_len, name,
				namelen);
	return;
207 208 209
}

ssize_t
210 211 212 213
xfs_vn_listxattr(
	struct dentry	*dentry,
	char		*data,
	size_t		size)
214 215 216
{
	struct xfs_attr_list_context context;
	struct attrlist_cursor_kern cursor = { 0 };
217 218
	struct inode	*inode = d_inode(dentry);
	int		error;
219 220 221 222 223 224 225 226

	/*
	 * First read the regular on-disk attributes.
	 */
	memset(&context, 0, sizeof(context));
	context.dp = XFS_I(inode);
	context.cursor = &cursor;
	context.resynch = 1;
227
	context.alist = size ? data : NULL;
228 229
	context.bufsize = size;
	context.firstu = context.bufsize;
230
	context.put_listent = xfs_xattr_put_listent;
231

232 233 234
	error = xfs_attr_list_int(&context);
	if (error)
		return error;
235 236 237 238 239
	if (context.count < 0)
		return -ERANGE;

	return context.count;
}