4 * Copyright IBM, Corp. 2010
7 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu/osdep.h"
16 #include "fsdev/file-op-9p.h"
22 static XattrOperations
*get_xattr_operations(XattrOperations
**h
,
25 XattrOperations
*xops
;
26 for (xops
= *(h
)++; xops
!= NULL
; xops
= *(h
)++) {
27 if (!strncmp(name
, xops
->name
, strlen(xops
->name
))) {
34 ssize_t
v9fs_get_xattr(FsContext
*ctx
, const char *path
,
35 const char *name
, void *value
, size_t size
)
37 XattrOperations
*xops
= get_xattr_operations(ctx
->xops
, name
);
39 return xops
->getxattr(ctx
, path
, name
, value
, size
);
45 ssize_t
pt_listxattr(FsContext
*ctx
, const char *path
,
46 char *name
, void *value
, size_t size
)
48 int name_size
= strlen(name
) + 1;
53 if (size
< name_size
) {
58 /* no need for strncpy: name_size is strlen(name)+1 */
59 memcpy(value
, name
, name_size
);
64 * Get the list and pass to each layer to find out whether
65 * to send the data or not
67 ssize_t
v9fs_list_xattr(FsContext
*ctx
, const char *path
,
68 void *value
, size_t vsize
)
72 XattrOperations
*xops
;
73 char *orig_value
, *orig_value_start
;
74 ssize_t xattr_len
, parsed_len
= 0, attr_len
;
78 /* Get the actual len */
79 dirpath
= g_path_get_dirname(path
);
80 dirfd
= local_opendir_nofollow(ctx
, dirpath
);
86 name
= g_path_get_basename(path
);
87 xattr_len
= flistxattrat_nofollow(dirfd
, name
, value
, 0);
90 close_preserve_errno(dirfd
);
94 /* Now fetch the xattr and find the actual size */
95 orig_value
= g_malloc(xattr_len
);
96 xattr_len
= flistxattrat_nofollow(dirfd
, name
, orig_value
, xattr_len
);
98 close_preserve_errno(dirfd
);
104 /* store the orig pointer */
105 orig_value_start
= orig_value
;
106 while (xattr_len
> parsed_len
) {
107 xops
= get_xattr_operations(ctx
->xops
, orig_value
);
113 size
+= xops
->listxattr(ctx
, path
, orig_value
, value
, vsize
);
115 size
= xops
->listxattr(ctx
, path
, orig_value
, value
, vsize
);
123 /* Got the next entry */
124 attr_len
= strlen(orig_value
) + 1;
125 parsed_len
+= attr_len
;
126 orig_value
+= attr_len
;
129 size
= value
- ovalue
;
133 g_free(orig_value_start
);
137 int v9fs_set_xattr(FsContext
*ctx
, const char *path
, const char *name
,
138 void *value
, size_t size
, int flags
)
140 XattrOperations
*xops
= get_xattr_operations(ctx
->xops
, name
);
142 return xops
->setxattr(ctx
, path
, name
, value
, size
, flags
);
149 int v9fs_remove_xattr(FsContext
*ctx
,
150 const char *path
, const char *name
)
152 XattrOperations
*xops
= get_xattr_operations(ctx
->xops
, name
);
154 return xops
->removexattr(ctx
, path
, name
);
161 ssize_t
local_getxattr_nofollow(FsContext
*ctx
, const char *path
,
162 const char *name
, void *value
, size_t size
)
164 char *dirpath
= g_path_get_dirname(path
);
165 char *filename
= g_path_get_basename(path
);
169 dirfd
= local_opendir_nofollow(ctx
, dirpath
);
174 ret
= fgetxattrat_nofollow(dirfd
, filename
, name
, value
, size
);
175 close_preserve_errno(dirfd
);
182 ssize_t
pt_getxattr(FsContext
*ctx
, const char *path
, const char *name
,
183 void *value
, size_t size
)
185 return local_getxattr_nofollow(ctx
, path
, name
, value
, size
);
188 ssize_t
local_setxattr_nofollow(FsContext
*ctx
, const char *path
,
189 const char *name
, void *value
, size_t size
,
192 char *dirpath
= g_path_get_dirname(path
);
193 char *filename
= g_path_get_basename(path
);
197 dirfd
= local_opendir_nofollow(ctx
, dirpath
);
202 ret
= fsetxattrat_nofollow(dirfd
, filename
, name
, value
, size
, flags
);
203 close_preserve_errno(dirfd
);
210 int pt_setxattr(FsContext
*ctx
, const char *path
, const char *name
, void *value
,
211 size_t size
, int flags
)
213 return local_setxattr_nofollow(ctx
, path
, name
, value
, size
, flags
);
216 ssize_t
local_removexattr_nofollow(FsContext
*ctx
, const char *path
,
219 char *dirpath
= g_path_get_dirname(path
);
220 char *filename
= g_path_get_basename(path
);
224 dirfd
= local_opendir_nofollow(ctx
, dirpath
);
229 ret
= fremovexattrat_nofollow(dirfd
, filename
, name
);
230 close_preserve_errno(dirfd
);
237 int pt_removexattr(FsContext
*ctx
, const char *path
, const char *name
)
239 return local_removexattr_nofollow(ctx
, path
, name
);
242 ssize_t
notsup_getxattr(FsContext
*ctx
, const char *path
, const char *name
,
243 void *value
, size_t size
)
249 int notsup_setxattr(FsContext
*ctx
, const char *path
, const char *name
,
250 void *value
, size_t size
, int flags
)
256 ssize_t
notsup_listxattr(FsContext
*ctx
, const char *path
, char *name
,
257 void *value
, size_t size
)
262 int notsup_removexattr(FsContext
*ctx
, const char *path
, const char *name
)
268 XattrOperations
*mapped_xattr_ops
[] = {
275 XattrOperations
*passthrough_xattr_ops
[] = {
276 &passthrough_user_xattr
,
277 &passthrough_acl_xattr
,
281 /* for .user none model should be same as passthrough */
282 XattrOperations
*none_xattr_ops
[] = {
283 &passthrough_user_xattr
,