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_log_format.h"
22 #include "xfs_trans.h"
25 #include "xfs_mount.h"
26 #include "xfs_da_format.h"
27 #include "xfs_bmap_btree.h"
28 #include "xfs_inode.h"
30 #include "xfs_attr_leaf.h"
33 #include <linux/posix_acl_xattr.h>
34 #include <linux/xattr.h>
38 xfs_xattr_get(struct dentry
*dentry
, const char *name
,
39 void *value
, size_t size
, int xflags
)
41 struct xfs_inode
*ip
= XFS_I(dentry
->d_inode
);
42 int error
, asize
= size
;
44 if (strcmp(name
, "") == 0)
47 /* Convert Linux syscall to XFS internal ATTR flags */
49 xflags
|= ATTR_KERNOVAL
;
53 error
= -xfs_attr_get(ip
, (unsigned char *)name
, value
, &asize
, xflags
);
60 xfs_xattr_set(struct dentry
*dentry
, const char *name
, const void *value
,
61 size_t size
, int flags
, int xflags
)
63 struct xfs_inode
*ip
= XFS_I(dentry
->d_inode
);
65 if (strcmp(name
, "") == 0)
68 /* Convert Linux syscall to XFS internal ATTR flags */
69 if (flags
& XATTR_CREATE
)
70 xflags
|= ATTR_CREATE
;
71 if (flags
& XATTR_REPLACE
)
72 xflags
|= ATTR_REPLACE
;
75 return -xfs_attr_remove(ip
, (unsigned char *)name
, xflags
);
76 return -xfs_attr_set(ip
, (unsigned char *)name
,
77 (void *)value
, size
, xflags
);
80 static const struct xattr_handler xfs_xattr_user_handler
= {
81 .prefix
= XATTR_USER_PREFIX
,
82 .flags
= 0, /* no flags implies user namespace */
87 static const struct xattr_handler xfs_xattr_trusted_handler
= {
88 .prefix
= XATTR_TRUSTED_PREFIX
,
94 static const struct xattr_handler xfs_xattr_security_handler
= {
95 .prefix
= XATTR_SECURITY_PREFIX
,
101 const struct xattr_handler
*xfs_xattr_handlers
[] = {
102 &xfs_xattr_user_handler
,
103 &xfs_xattr_trusted_handler
,
104 &xfs_xattr_security_handler
,
105 #ifdef CONFIG_XFS_POSIX_ACL
106 &xfs_xattr_acl_access_handler
,
107 &xfs_xattr_acl_default_handler
,
112 static unsigned int xfs_xattr_prefix_len(int flags
)
114 if (flags
& XFS_ATTR_SECURE
)
115 return sizeof("security");
116 else if (flags
& XFS_ATTR_ROOT
)
117 return sizeof("trusted");
119 return sizeof("user");
122 static const char *xfs_xattr_prefix(int flags
)
124 if (flags
& XFS_ATTR_SECURE
)
125 return xfs_xattr_security_handler
.prefix
;
126 else if (flags
& XFS_ATTR_ROOT
)
127 return xfs_xattr_trusted_handler
.prefix
;
129 return xfs_xattr_user_handler
.prefix
;
133 xfs_xattr_put_listent(
134 struct xfs_attr_list_context
*context
,
139 unsigned char *value
)
141 unsigned int prefix_len
= xfs_xattr_prefix_len(flags
);
145 ASSERT(context
->count
>= 0);
148 * Only show root namespace entries if we are actually allowed to
151 if ((flags
& XFS_ATTR_ROOT
) && !capable(CAP_SYS_ADMIN
))
154 arraytop
= context
->count
+ prefix_len
+ namelen
+ 1;
155 if (arraytop
> context
->firstu
) {
156 context
->count
= -1; /* insufficient space */
159 offset
= (char *)context
->alist
+ context
->count
;
160 strncpy(offset
, xfs_xattr_prefix(flags
), prefix_len
);
161 offset
+= prefix_len
;
162 strncpy(offset
, (char *)name
, namelen
); /* real name */
165 context
->count
+= prefix_len
+ namelen
+ 1;
170 xfs_xattr_put_listent_sizes(
171 struct xfs_attr_list_context
*context
,
176 unsigned char *value
)
178 context
->count
+= xfs_xattr_prefix_len(flags
) + namelen
+ 1;
183 list_one_attr(const char *name
, const size_t len
, void *data
,
184 size_t size
, ssize_t
*result
)
186 char *p
= data
+ *result
;
199 xfs_vn_listxattr(struct dentry
*dentry
, char *data
, size_t size
)
201 struct xfs_attr_list_context context
;
202 struct attrlist_cursor_kern cursor
= { 0 };
203 struct inode
*inode
= dentry
->d_inode
;
207 * First read the regular on-disk attributes.
209 memset(&context
, 0, sizeof(context
));
210 context
.dp
= XFS_I(inode
);
211 context
.cursor
= &cursor
;
213 context
.alist
= data
;
214 context
.bufsize
= size
;
215 context
.firstu
= context
.bufsize
;
218 context
.put_listent
= xfs_xattr_put_listent
;
220 context
.put_listent
= xfs_xattr_put_listent_sizes
;
222 xfs_attr_list_int(&context
);
223 if (context
.count
< 0)
227 * Then add the two synthetic ACL attributes.
229 if (posix_acl_access_exists(inode
)) {
230 error
= list_one_attr(POSIX_ACL_XATTR_ACCESS
,
231 strlen(POSIX_ACL_XATTR_ACCESS
) + 1,
232 data
, size
, &context
.count
);
237 if (posix_acl_default_exists(inode
)) {
238 error
= list_one_attr(POSIX_ACL_XATTR_DEFAULT
,
239 strlen(POSIX_ACL_XATTR_DEFAULT
) + 1,
240 data
, size
, &context
.count
);
245 return context
.count
;