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
20 #include "xfs_da_btree.h"
21 #include "xfs_bmap_btree.h"
22 #include "xfs_inode.h"
24 #include "xfs_attr_leaf.h"
26 #include "xfs_vnodeops.h"
28 #include <linux/posix_acl_xattr.h>
29 #include <linux/xattr.h>
33 * ACL handling. Should eventually be moved into xfs_acl.c
37 xfs_decode_acl(const char *name
)
39 if (strcmp(name
, "posix_acl_access") == 0)
40 return _ACL_TYPE_ACCESS
;
41 else if (strcmp(name
, "posix_acl_default") == 0)
42 return _ACL_TYPE_DEFAULT
;
47 * Get system extended attributes which at the moment only
48 * includes Posix ACLs.
51 xfs_xattr_system_get(struct inode
*inode
, const char *name
,
52 void *buffer
, size_t size
)
56 acl
= xfs_decode_acl(name
);
60 return xfs_acl_vget(inode
, buffer
, size
, acl
);
64 xfs_xattr_system_set(struct inode
*inode
, const char *name
,
65 const void *value
, size_t size
, int flags
)
69 acl
= xfs_decode_acl(name
);
72 if (flags
& XATTR_CREATE
)
76 return xfs_acl_vremove(inode
, acl
);
78 return xfs_acl_vset(inode
, (void *)value
, size
, acl
);
81 static struct xattr_handler xfs_xattr_system_handler
= {
82 .prefix
= XATTR_SYSTEM_PREFIX
,
83 .get
= xfs_xattr_system_get
,
84 .set
= xfs_xattr_system_set
,
89 * Real xattr handling. The only difference between the namespaces is
90 * a flag passed to the low-level attr code.
94 __xfs_xattr_get(struct inode
*inode
, const char *name
,
95 void *value
, size_t size
, int xflags
)
97 struct xfs_inode
*ip
= XFS_I(inode
);
98 int error
, asize
= size
;
100 if (strcmp(name
, "") == 0)
103 /* Convert Linux syscall to XFS internal ATTR flags */
105 xflags
|= ATTR_KERNOVAL
;
109 error
= -xfs_attr_get(ip
, name
, value
, &asize
, xflags
);
116 __xfs_xattr_set(struct inode
*inode
, const char *name
, const void *value
,
117 size_t size
, int flags
, int xflags
)
119 struct xfs_inode
*ip
= XFS_I(inode
);
121 if (strcmp(name
, "") == 0)
124 /* Convert Linux syscall to XFS internal ATTR flags */
125 if (flags
& XATTR_CREATE
)
126 xflags
|= ATTR_CREATE
;
127 if (flags
& XATTR_REPLACE
)
128 xflags
|= ATTR_REPLACE
;
131 return -xfs_attr_remove(ip
, name
, xflags
);
132 return -xfs_attr_set(ip
, name
, (void *)value
, size
, xflags
);
136 xfs_xattr_user_get(struct inode
*inode
, const char *name
,
137 void *value
, size_t size
)
139 return __xfs_xattr_get(inode
, name
, value
, size
, 0);
143 xfs_xattr_user_set(struct inode
*inode
, const char *name
,
144 const void *value
, size_t size
, int flags
)
146 return __xfs_xattr_set(inode
, name
, value
, size
, flags
, 0);
149 static struct xattr_handler xfs_xattr_user_handler
= {
150 .prefix
= XATTR_USER_PREFIX
,
151 .get
= xfs_xattr_user_get
,
152 .set
= xfs_xattr_user_set
,
157 xfs_xattr_trusted_get(struct inode
*inode
, const char *name
,
158 void *value
, size_t size
)
160 return __xfs_xattr_get(inode
, name
, value
, size
, ATTR_ROOT
);
164 xfs_xattr_trusted_set(struct inode
*inode
, const char *name
,
165 const void *value
, size_t size
, int flags
)
167 return __xfs_xattr_set(inode
, name
, value
, size
, flags
, ATTR_ROOT
);
170 static struct xattr_handler xfs_xattr_trusted_handler
= {
171 .prefix
= XATTR_TRUSTED_PREFIX
,
172 .get
= xfs_xattr_trusted_get
,
173 .set
= xfs_xattr_trusted_set
,
178 xfs_xattr_secure_get(struct inode
*inode
, const char *name
,
179 void *value
, size_t size
)
181 return __xfs_xattr_get(inode
, name
, value
, size
, ATTR_SECURE
);
185 xfs_xattr_secure_set(struct inode
*inode
, const char *name
,
186 const void *value
, size_t size
, int flags
)
188 return __xfs_xattr_set(inode
, name
, value
, size
, flags
, ATTR_SECURE
);
191 static struct xattr_handler xfs_xattr_security_handler
= {
192 .prefix
= XATTR_SECURITY_PREFIX
,
193 .get
= xfs_xattr_secure_get
,
194 .set
= xfs_xattr_secure_set
,
198 struct xattr_handler
*xfs_xattr_handlers
[] = {
199 &xfs_xattr_user_handler
,
200 &xfs_xattr_trusted_handler
,
201 &xfs_xattr_security_handler
,
202 &xfs_xattr_system_handler
,
206 static unsigned int xfs_xattr_prefix_len(int flags
)
208 if (flags
& XFS_ATTR_SECURE
)
209 return sizeof("security");
210 else if (flags
& XFS_ATTR_ROOT
)
211 return sizeof("trusted");
213 return sizeof("user");
216 static const char *xfs_xattr_prefix(int flags
)
218 if (flags
& XFS_ATTR_SECURE
)
219 return xfs_xattr_security_handler
.prefix
;
220 else if (flags
& XFS_ATTR_ROOT
)
221 return xfs_xattr_trusted_handler
.prefix
;
223 return xfs_xattr_user_handler
.prefix
;
227 xfs_xattr_put_listent(struct xfs_attr_list_context
*context
, int flags
,
228 char *name
, int namelen
, int valuelen
, char *value
)
230 unsigned int prefix_len
= xfs_xattr_prefix_len(flags
);
234 ASSERT(context
->count
>= 0);
237 * Only show root namespace entries if we are actually allowed to
240 if ((flags
& XFS_ATTR_ROOT
) && !capable(CAP_SYS_ADMIN
))
243 arraytop
= context
->count
+ prefix_len
+ namelen
+ 1;
244 if (arraytop
> context
->firstu
) {
245 context
->count
= -1; /* insufficient space */
248 offset
= (char *)context
->alist
+ context
->count
;
249 strncpy(offset
, xfs_xattr_prefix(flags
), prefix_len
);
250 offset
+= prefix_len
;
251 strncpy(offset
, name
, namelen
); /* real name */
254 context
->count
+= prefix_len
+ namelen
+ 1;
259 xfs_xattr_put_listent_sizes(struct xfs_attr_list_context
*context
, int flags
,
260 char *name
, int namelen
, int valuelen
, char *value
)
262 context
->count
+= xfs_xattr_prefix_len(flags
) + namelen
+ 1;
267 list_one_attr(const char *name
, const size_t len
, void *data
,
268 size_t size
, ssize_t
*result
)
270 char *p
= data
+ *result
;
283 xfs_vn_listxattr(struct dentry
*dentry
, char *data
, size_t size
)
285 struct xfs_attr_list_context context
;
286 struct attrlist_cursor_kern cursor
= { 0 };
287 struct inode
*inode
= dentry
->d_inode
;
291 * First read the regular on-disk attributes.
293 memset(&context
, 0, sizeof(context
));
294 context
.dp
= XFS_I(inode
);
295 context
.cursor
= &cursor
;
297 context
.alist
= data
;
298 context
.bufsize
= size
;
299 context
.firstu
= context
.bufsize
;
302 context
.put_listent
= xfs_xattr_put_listent
;
304 context
.put_listent
= xfs_xattr_put_listent_sizes
;
306 xfs_attr_list_int(&context
);
307 if (context
.count
< 0)
311 * Then add the two synthetic ACL attributes.
313 if (xfs_acl_vhasacl_access(inode
)) {
314 error
= list_one_attr(POSIX_ACL_XATTR_ACCESS
,
315 strlen(POSIX_ACL_XATTR_ACCESS
) + 1,
316 data
, size
, &context
.count
);
321 if (xfs_acl_vhasacl_default(inode
)) {
322 error
= list_one_attr(POSIX_ACL_XATTR_DEFAULT
,
323 strlen(POSIX_ACL_XATTR_DEFAULT
) + 1,
324 data
, size
, &context
.count
);
329 return context
.count
;