blob: b7311aaa5f0e7b22a7acc5bc7a846f14f73607f7 [file] [log] [blame]
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +10001/*
2 * Copyright (C) 2008 Christoph Hellwig.
3 * Portions Copyright (C) 2000-2008 Silicon Graphics, Inc.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "xfs.h"
Dave Chinner69432832013-08-12 20:49:23 +100020#include "xfs_log_format.h"
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100021#include "xfs_da_btree.h"
22#include "xfs_bmap_btree.h"
23#include "xfs_inode.h"
24#include "xfs_attr.h"
25#include "xfs_attr_leaf.h"
26#include "xfs_acl.h"
27#include "xfs_vnodeops.h"
28
29#include <linux/posix_acl_xattr.h>
30#include <linux/xattr.h>
31
32
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100033static int
Christoph Hellwig431547b2009-11-13 09:52:56 +000034xfs_xattr_get(struct dentry *dentry, const char *name,
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100035 void *value, size_t size, int xflags)
36{
Christoph Hellwig431547b2009-11-13 09:52:56 +000037 struct xfs_inode *ip = XFS_I(dentry->d_inode);
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100038 int error, asize = size;
39
40 if (strcmp(name, "") == 0)
41 return -EINVAL;
42
43 /* Convert Linux syscall to XFS internal ATTR flags */
44 if (!size) {
45 xflags |= ATTR_KERNOVAL;
46 value = NULL;
47 }
48
Dave Chinnera9273ca2010-01-20 10:47:48 +110049 error = -xfs_attr_get(ip, (unsigned char *)name, value, &asize, xflags);
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100050 if (error)
51 return error;
52 return asize;
53}
54
55static int
Christoph Hellwig431547b2009-11-13 09:52:56 +000056xfs_xattr_set(struct dentry *dentry, const char *name, const void *value,
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100057 size_t size, int flags, int xflags)
58{
Christoph Hellwig431547b2009-11-13 09:52:56 +000059 struct xfs_inode *ip = XFS_I(dentry->d_inode);
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100060
61 if (strcmp(name, "") == 0)
62 return -EINVAL;
63
64 /* Convert Linux syscall to XFS internal ATTR flags */
65 if (flags & XATTR_CREATE)
66 xflags |= ATTR_CREATE;
67 if (flags & XATTR_REPLACE)
68 xflags |= ATTR_REPLACE;
69
70 if (!value)
Dave Chinnera9273ca2010-01-20 10:47:48 +110071 return -xfs_attr_remove(ip, (unsigned char *)name, xflags);
72 return -xfs_attr_set(ip, (unsigned char *)name,
73 (void *)value, size, xflags);
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100074}
75
Stephen Hemminger46e58762010-05-13 17:53:20 -070076static const struct xattr_handler xfs_xattr_user_handler = {
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100077 .prefix = XATTR_USER_PREFIX,
Christoph Hellwig431547b2009-11-13 09:52:56 +000078 .flags = 0, /* no flags implies user namespace */
79 .get = xfs_xattr_get,
80 .set = xfs_xattr_set,
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100081};
82
Stephen Hemminger46e58762010-05-13 17:53:20 -070083static const struct xattr_handler xfs_xattr_trusted_handler = {
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100084 .prefix = XATTR_TRUSTED_PREFIX,
Christoph Hellwig431547b2009-11-13 09:52:56 +000085 .flags = ATTR_ROOT,
86 .get = xfs_xattr_get,
87 .set = xfs_xattr_set,
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100088};
89
Stephen Hemminger46e58762010-05-13 17:53:20 -070090static const struct xattr_handler xfs_xattr_security_handler = {
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100091 .prefix = XATTR_SECURITY_PREFIX,
Christoph Hellwig431547b2009-11-13 09:52:56 +000092 .flags = ATTR_SECURE,
93 .get = xfs_xattr_get,
94 .set = xfs_xattr_set,
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100095};
96
Stephen Hemminger46e58762010-05-13 17:53:20 -070097const struct xattr_handler *xfs_xattr_handlers[] = {
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +100098 &xfs_xattr_user_handler,
99 &xfs_xattr_trusted_handler,
100 &xfs_xattr_security_handler,
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200101#ifdef CONFIG_XFS_POSIX_ACL
Christoph Hellwig431547b2009-11-13 09:52:56 +0000102 &xfs_xattr_acl_access_handler,
103 &xfs_xattr_acl_default_handler,
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200104#endif
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000105 NULL
106};
107
108static unsigned int xfs_xattr_prefix_len(int flags)
109{
110 if (flags & XFS_ATTR_SECURE)
111 return sizeof("security");
112 else if (flags & XFS_ATTR_ROOT)
113 return sizeof("trusted");
114 else
115 return sizeof("user");
116}
117
118static const char *xfs_xattr_prefix(int flags)
119{
120 if (flags & XFS_ATTR_SECURE)
121 return xfs_xattr_security_handler.prefix;
122 else if (flags & XFS_ATTR_ROOT)
123 return xfs_xattr_trusted_handler.prefix;
124 else
125 return xfs_xattr_user_handler.prefix;
126}
127
128static int
Dave Chinnera9273ca2010-01-20 10:47:48 +1100129xfs_xattr_put_listent(
130 struct xfs_attr_list_context *context,
131 int flags,
132 unsigned char *name,
133 int namelen,
134 int valuelen,
135 unsigned char *value)
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000136{
137 unsigned int prefix_len = xfs_xattr_prefix_len(flags);
138 char *offset;
139 int arraytop;
140
141 ASSERT(context->count >= 0);
142
143 /*
144 * Only show root namespace entries if we are actually allowed to
145 * see them.
146 */
147 if ((flags & XFS_ATTR_ROOT) && !capable(CAP_SYS_ADMIN))
148 return 0;
149
150 arraytop = context->count + prefix_len + namelen + 1;
151 if (arraytop > context->firstu) {
152 context->count = -1; /* insufficient space */
153 return 1;
154 }
155 offset = (char *)context->alist + context->count;
156 strncpy(offset, xfs_xattr_prefix(flags), prefix_len);
157 offset += prefix_len;
Dave Chinnera9273ca2010-01-20 10:47:48 +1100158 strncpy(offset, (char *)name, namelen); /* real name */
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000159 offset += namelen;
160 *offset = '\0';
161 context->count += prefix_len + namelen + 1;
162 return 0;
163}
164
165static int
Dave Chinnera9273ca2010-01-20 10:47:48 +1100166xfs_xattr_put_listent_sizes(
167 struct xfs_attr_list_context *context,
168 int flags,
169 unsigned char *name,
170 int namelen,
171 int valuelen,
172 unsigned char *value)
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000173{
174 context->count += xfs_xattr_prefix_len(flags) + namelen + 1;
175 return 0;
176}
177
178static int
179list_one_attr(const char *name, const size_t len, void *data,
180 size_t size, ssize_t *result)
181{
182 char *p = data + *result;
183
184 *result += len;
185 if (!size)
186 return 0;
187 if (*result > size)
188 return -ERANGE;
189
190 strcpy(p, name);
191 return 0;
192}
193
194ssize_t
195xfs_vn_listxattr(struct dentry *dentry, char *data, size_t size)
196{
197 struct xfs_attr_list_context context;
198 struct attrlist_cursor_kern cursor = { 0 };
199 struct inode *inode = dentry->d_inode;
200 int error;
201
202 /*
203 * First read the regular on-disk attributes.
204 */
205 memset(&context, 0, sizeof(context));
206 context.dp = XFS_I(inode);
207 context.cursor = &cursor;
208 context.resynch = 1;
209 context.alist = data;
210 context.bufsize = size;
211 context.firstu = context.bufsize;
212
213 if (size)
214 context.put_listent = xfs_xattr_put_listent;
215 else
216 context.put_listent = xfs_xattr_put_listent_sizes;
217
218 xfs_attr_list_int(&context);
219 if (context.count < 0)
220 return -ERANGE;
221
222 /*
223 * Then add the two synthetic ACL attributes.
224 */
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200225 if (posix_acl_access_exists(inode)) {
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000226 error = list_one_attr(POSIX_ACL_XATTR_ACCESS,
227 strlen(POSIX_ACL_XATTR_ACCESS) + 1,
228 data, size, &context.count);
229 if (error)
230 return error;
231 }
232
Christoph Hellwigef14f0c2009-06-10 17:07:47 +0200233 if (posix_acl_default_exists(inode)) {
Lachlan McIlroyf9e09f02008-06-23 13:34:09 +1000234 error = list_one_attr(POSIX_ACL_XATTR_DEFAULT,
235 strlen(POSIX_ACL_XATTR_DEFAULT) + 1,
236 data, size, &context.count);
237 if (error)
238 return error;
239 }
240
241 return context.count;
242}