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 * Get system extended attributes which at the moment only
34 * includes Posix ACLs.
37 xfs_xattr_system_get(struct inode
*inode
, const char *name
,
38 void *buffer
, size_t size
)
42 acl
= xfs_decode_acl(name
);
46 return xfs_acl_vget(inode
, buffer
, size
, acl
);
50 xfs_xattr_system_set(struct inode
*inode
, const char *name
,
51 const void *value
, size_t size
, int flags
)
55 acl
= xfs_decode_acl(name
);
58 if (flags
& XATTR_CREATE
)
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.
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)
89 /* Convert Linux syscall to XFS internal ATTR flags */
91 xflags
|= ATTR_KERNOVAL
;
95 error
= -xfs_attr_get(ip
, name
, value
, &asize
, xflags
);
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)
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
;
117 return -xfs_attr_remove(ip
, name
, xflags
);
118 return -xfs_attr_set(ip
, name
, (void *)value
, size
, xflags
);
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);
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
,
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
);
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
,
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
);
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
,
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");
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
;
209 return xfs_xattr_user_handler
.prefix
;
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
);
220 ASSERT(context
->count
>= 0);
223 * Only show root namespace entries if we are actually allowed to
226 if ((flags
& XFS_ATTR_ROOT
) && !capable(CAP_SYS_ADMIN
))
229 arraytop
= context
->count
+ prefix_len
+ namelen
+ 1;
230 if (arraytop
> context
->firstu
) {
231 context
->count
= -1; /* insufficient space */
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 */
240 context
->count
+= prefix_len
+ namelen
+ 1;
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;
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
;
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
;
277 * First read the regular on-disk attributes.
279 memset(&context
, 0, sizeof(context
));
280 context
.dp
= XFS_I(inode
);
281 context
.cursor
= &cursor
;
283 context
.alist
= data
;
284 context
.bufsize
= size
;
285 context
.firstu
= context
.bufsize
;
288 context
.put_listent
= xfs_xattr_put_listent
;
290 context
.put_listent
= xfs_xattr_put_listent_sizes
;
292 xfs_attr_list_int(&context
);
293 if (context
.count
< 0)
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
);
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
);
315 return context
.count
;