repo init
[linux-rt-nao.git] / fs / xfs / linux-2.6 / xfs_xattr.c
blobb7b459cb0604748d86c04df3c74f9d5cde698a4e
1 /*
2 * Copyright (C) 2008 Christoph Hellwig.
3 * Portions Copyright (C) 2000-2008 Silicon Graphics, Inc.
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.
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.
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
19 #include "xfs.h"
20 #include "xfs_da_btree.h"
21 #include "xfs_bmap_btree.h"
22 #include "xfs_inode.h"
23 #include "xfs_attr.h"
24 #include "xfs_attr_leaf.h"
25 #include "xfs_acl.h"
26 #include "xfs_vnodeops.h"
28 #include <linux/posix_acl_xattr.h>
29 #include <linux/xattr.h>
33 * Get system extended attributes which at the moment only
34 * includes Posix ACLs.
36 static int
37 xfs_xattr_system_get(struct inode *inode, const char *name,
38 void *buffer, size_t size)
40 int acl;
42 acl = xfs_decode_acl(name);
43 if (acl < 0)
44 return acl;
46 return xfs_acl_vget(inode, buffer, size, acl);
49 static int
50 xfs_xattr_system_set(struct inode *inode, const char *name,
51 const void *value, size_t size, int flags)
53 int acl;
55 acl = xfs_decode_acl(name);
56 if (acl < 0)
57 return acl;
58 if (flags & XATTR_CREATE)
59 return -EINVAL;
61 if (!value)
62 return xfs_acl_vremove(inode, acl);
64 return xfs_acl_vset(inode, (void *)value, size, acl);
67 static struct xattr_handler xfs_xattr_system_handler = {
68 .prefix = XATTR_SYSTEM_PREFIX,
69 .get = xfs_xattr_system_get,
70 .set = xfs_xattr_system_set,
75 * Real xattr handling. The only difference between the namespaces is
76 * a flag passed to the low-level attr code.
79 static int
80 __xfs_xattr_get(struct inode *inode, const char *name,
81 void *value, size_t size, int xflags)
83 struct xfs_inode *ip = XFS_I(inode);
84 int error, asize = size;
86 if (strcmp(name, "") == 0)
87 return -EINVAL;
89 /* Convert Linux syscall to XFS internal ATTR flags */
90 if (!size) {
91 xflags |= ATTR_KERNOVAL;
92 value = NULL;
95 error = -xfs_attr_get(ip, name, value, &asize, xflags);
96 if (error)
97 return error;
98 return asize;
101 static int
102 __xfs_xattr_set(struct inode *inode, const char *name, const void *value,
103 size_t size, int flags, int xflags)
105 struct xfs_inode *ip = XFS_I(inode);
107 if (strcmp(name, "") == 0)
108 return -EINVAL;
110 /* Convert Linux syscall to XFS internal ATTR flags */
111 if (flags & XATTR_CREATE)
112 xflags |= ATTR_CREATE;
113 if (flags & XATTR_REPLACE)
114 xflags |= ATTR_REPLACE;
116 if (!value)
117 return -xfs_attr_remove(ip, name, xflags);
118 return -xfs_attr_set(ip, name, (void *)value, size, xflags);
121 static int
122 xfs_xattr_user_get(struct inode *inode, const char *name,
123 void *value, size_t size)
125 return __xfs_xattr_get(inode, name, value, size, 0);
128 static int
129 xfs_xattr_user_set(struct inode *inode, const char *name,
130 const void *value, size_t size, int flags)
132 return __xfs_xattr_set(inode, name, value, size, flags, 0);
135 static struct xattr_handler xfs_xattr_user_handler = {
136 .prefix = XATTR_USER_PREFIX,
137 .get = xfs_xattr_user_get,
138 .set = xfs_xattr_user_set,
142 static int
143 xfs_xattr_trusted_get(struct inode *inode, const char *name,
144 void *value, size_t size)
146 return __xfs_xattr_get(inode, name, value, size, ATTR_ROOT);
149 static int
150 xfs_xattr_trusted_set(struct inode *inode, const char *name,
151 const void *value, size_t size, int flags)
153 return __xfs_xattr_set(inode, name, value, size, flags, ATTR_ROOT);
156 static struct xattr_handler xfs_xattr_trusted_handler = {
157 .prefix = XATTR_TRUSTED_PREFIX,
158 .get = xfs_xattr_trusted_get,
159 .set = xfs_xattr_trusted_set,
163 static int
164 xfs_xattr_secure_get(struct inode *inode, const char *name,
165 void *value, size_t size)
167 return __xfs_xattr_get(inode, name, value, size, ATTR_SECURE);
170 static int
171 xfs_xattr_secure_set(struct inode *inode, const char *name,
172 const void *value, size_t size, int flags)
174 return __xfs_xattr_set(inode, name, value, size, flags, ATTR_SECURE);
177 static struct xattr_handler xfs_xattr_security_handler = {
178 .prefix = XATTR_SECURITY_PREFIX,
179 .get = xfs_xattr_secure_get,
180 .set = xfs_xattr_secure_set,
184 struct xattr_handler *xfs_xattr_handlers[] = {
185 &xfs_xattr_user_handler,
186 &xfs_xattr_trusted_handler,
187 &xfs_xattr_security_handler,
188 &xfs_xattr_system_handler,
189 NULL
192 static unsigned int xfs_xattr_prefix_len(int flags)
194 if (flags & XFS_ATTR_SECURE)
195 return sizeof("security");
196 else if (flags & XFS_ATTR_ROOT)
197 return sizeof("trusted");
198 else
199 return sizeof("user");
202 static const char *xfs_xattr_prefix(int flags)
204 if (flags & XFS_ATTR_SECURE)
205 return xfs_xattr_security_handler.prefix;
206 else if (flags & XFS_ATTR_ROOT)
207 return xfs_xattr_trusted_handler.prefix;
208 else
209 return xfs_xattr_user_handler.prefix;
212 static int
213 xfs_xattr_put_listent(struct xfs_attr_list_context *context, int flags,
214 char *name, int namelen, int valuelen, char *value)
216 unsigned int prefix_len = xfs_xattr_prefix_len(flags);
217 char *offset;
218 int arraytop;
220 ASSERT(context->count >= 0);
223 * Only show root namespace entries if we are actually allowed to
224 * see them.
226 if ((flags & XFS_ATTR_ROOT) && !capable(CAP_SYS_ADMIN))
227 return 0;
229 arraytop = context->count + prefix_len + namelen + 1;
230 if (arraytop > context->firstu) {
231 context->count = -1; /* insufficient space */
232 return 1;
234 offset = (char *)context->alist + context->count;
235 strncpy(offset, xfs_xattr_prefix(flags), prefix_len);
236 offset += prefix_len;
237 strncpy(offset, name, namelen); /* real name */
238 offset += namelen;
239 *offset = '\0';
240 context->count += prefix_len + namelen + 1;
241 return 0;
244 static int
245 xfs_xattr_put_listent_sizes(struct xfs_attr_list_context *context, int flags,
246 char *name, int namelen, int valuelen, char *value)
248 context->count += xfs_xattr_prefix_len(flags) + namelen + 1;
249 return 0;
252 static int
253 list_one_attr(const char *name, const size_t len, void *data,
254 size_t size, ssize_t *result)
256 char *p = data + *result;
258 *result += len;
259 if (!size)
260 return 0;
261 if (*result > size)
262 return -ERANGE;
264 strcpy(p, name);
265 return 0;
268 ssize_t
269 xfs_vn_listxattr(struct dentry *dentry, char *data, size_t size)
271 struct xfs_attr_list_context context;
272 struct attrlist_cursor_kern cursor = { 0 };
273 struct inode *inode = dentry->d_inode;
274 int error;
277 * First read the regular on-disk attributes.
279 memset(&context, 0, sizeof(context));
280 context.dp = XFS_I(inode);
281 context.cursor = &cursor;
282 context.resynch = 1;
283 context.alist = data;
284 context.bufsize = size;
285 context.firstu = context.bufsize;
287 if (size)
288 context.put_listent = xfs_xattr_put_listent;
289 else
290 context.put_listent = xfs_xattr_put_listent_sizes;
292 xfs_attr_list_int(&context);
293 if (context.count < 0)
294 return -ERANGE;
297 * Then add the two synthetic ACL attributes.
299 if (xfs_acl_vhasacl_access(inode)) {
300 error = list_one_attr(POSIX_ACL_XATTR_ACCESS,
301 strlen(POSIX_ACL_XATTR_ACCESS) + 1,
302 data, size, &context.count);
303 if (error)
304 return error;
307 if (xfs_acl_vhasacl_default(inode)) {
308 error = list_one_attr(POSIX_ACL_XATTR_DEFAULT,
309 strlen(POSIX_ACL_XATTR_DEFAULT) + 1,
310 data, size, &context.count);
311 if (error)
312 return error;
315 return context.count;