4 * Copyright IBM, Corp. 2010
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);
63 static ssize_t flistxattrat_nofollow(int dirfd, const char *filename,
64 char *list, size_t size)
66 char *proc_path = g_strdup_printf("/proc/self/fd/%d/%s", dirfd, filename);
69 ret = llistxattr(proc_path, list, size);
75 * Get the list and pass to each layer to find out whether
76 * to send the data or not
78 ssize_t v9fs_list_xattr(FsContext *ctx, const char *path,
79 void *value, size_t vsize)
83 XattrOperations *xops;
84 char *orig_value, *orig_value_start;
85 ssize_t xattr_len, parsed_len = 0, attr_len;
89 /* Get the actual len */
90 dirpath = g_path_get_dirname(path);
91 dirfd = local_opendir_nofollow(ctx, dirpath);
97 name = g_path_get_basename(path);
98 xattr_len = flistxattrat_nofollow(dirfd, name, value, 0);
101 close_preserve_errno(dirfd);
105 /* Now fetch the xattr and find the actual size */
106 orig_value = g_malloc(xattr_len);
107 xattr_len = flistxattrat_nofollow(dirfd, name, orig_value, xattr_len);
109 close_preserve_errno(dirfd);
114 /* store the orig pointer */
115 orig_value_start = orig_value;
116 while (xattr_len > parsed_len) {
117 xops = get_xattr_operations(ctx->xops, orig_value);
123 size += xops->listxattr(ctx, path, orig_value, value, vsize);
125 size = xops->listxattr(ctx, path, orig_value, value, vsize);
133 /* Got the next entry */
134 attr_len = strlen(orig_value) + 1;
135 parsed_len += attr_len;
136 orig_value += attr_len;
139 size = value - ovalue;
143 g_free(orig_value_start);
147 int v9fs_set_xattr(FsContext *ctx, const char *path, const char *name,
148 void *value, size_t size, int flags)
150 XattrOperations *xops = get_xattr_operations(ctx->xops, name);
152 return xops->setxattr(ctx, path, name, value, size, flags);
159 int v9fs_remove_xattr(FsContext *ctx,
160 const char *path, const char *name)
162 XattrOperations *xops = get_xattr_operations(ctx->xops, name);
164 return xops->removexattr(ctx, path, name);
171 ssize_t local_getxattr_nofollow(FsContext *ctx, const char *path,
172 const char *name, void *value, size_t size)
174 char *dirpath = g_path_get_dirname(path);
175 char *filename = g_path_get_basename(path);
179 dirfd = local_opendir_nofollow(ctx, dirpath);
184 ret = fgetxattrat_nofollow(dirfd, filename, name, value, size);
185 close_preserve_errno(dirfd);
192 ssize_t pt_getxattr(FsContext *ctx, const char *path, const char *name,
193 void *value, size_t size)
195 return local_getxattr_nofollow(ctx, path, name, value, size);
198 int fsetxattrat_nofollow(int dirfd, const char *filename, const char *name,
199 void *value, size_t size, int flags)
201 char *proc_path = g_strdup_printf("/proc/self/fd/%d/%s", dirfd, filename);
204 ret = lsetxattr(proc_path, name, value, size, flags);
209 ssize_t local_setxattr_nofollow(FsContext *ctx, const char *path,
210 const char *name, void *value, size_t size,
213 char *dirpath = g_path_get_dirname(path);
214 char *filename = g_path_get_basename(path);
218 dirfd = local_opendir_nofollow(ctx, dirpath);
223 ret = fsetxattrat_nofollow(dirfd, filename, name, value, size, flags);
224 close_preserve_errno(dirfd);
231 int pt_setxattr(FsContext *ctx, const char *path, const char *name, void *value,
232 size_t size, int flags)
234 return local_setxattr_nofollow(ctx, path, name, value, size, flags);
237 static ssize_t fremovexattrat_nofollow(int dirfd, const char *filename,
240 char *proc_path = g_strdup_printf("/proc/self/fd/%d/%s", dirfd, filename);
243 ret = lremovexattr(proc_path, name);
248 ssize_t local_removexattr_nofollow(FsContext *ctx, const char *path,
251 char *dirpath = g_path_get_dirname(path);
252 char *filename = g_path_get_basename(path);
256 dirfd = local_opendir_nofollow(ctx, dirpath);
261 ret = fremovexattrat_nofollow(dirfd, filename, name);
262 close_preserve_errno(dirfd);
269 int pt_removexattr(FsContext *ctx, const char *path, const char *name)
271 return local_removexattr_nofollow(ctx, path, name);
274 ssize_t notsup_getxattr(FsContext *ctx, const char *path, const char *name,
275 void *value, size_t size)
281 int notsup_setxattr(FsContext *ctx, const char *path, const char *name,
282 void *value, size_t size, int flags)
288 ssize_t notsup_listxattr(FsContext *ctx, const char *path, char *name,
289 void *value, size_t size)
294 int notsup_removexattr(FsContext *ctx, const char *path, const char *name)
300 XattrOperations *mapped_xattr_ops[] = {
307 XattrOperations *passthrough_xattr_ops[] = {
308 &passthrough_user_xattr,
309 &passthrough_acl_xattr,
313 /* for .user none model should be same as passthrough */
314 XattrOperations *none_xattr_ops[] = {
315 &passthrough_user_xattr,