Merge "virtio-9p: enable the virtfs on Windows." into tizen
[sdk/emulator/qemu.git] / hw / 9pfs / virtio-9p-xattr-user.c
1 /*
2  * Virtio 9p user. xattr callback
3  *
4  * Copyright IBM, Corp. 2010
5  *
6  * Authors:
7  * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13 #include <sys/types.h>
14 #include "hw/virtio/virtio.h"
15 #include "virtio-9p.h"
16 #include "fsdev/file-op-9p.h"
17 #include "virtio-9p-xattr.h"
18
19
20 static ssize_t mp_user_getxattr(FsContext *ctx, const char *path,
21                                 const char *name, void *value, size_t size)
22 {
23     char buffer[PATH_MAX];
24     if (strncmp(name, "user.virtfs.", 12) == 0) {
25         /*
26          * Don't allow fetch of user.virtfs namesapce
27          * in case of mapped security
28          */
29         errno = ENOATTR;
30         return -1;
31     }
32 #ifdef CONFIG_LINUX
33     return lgetxattr(rpath(ctx, path, buffer), name, value, size);
34 #else
35     return getxattr(rpath(ctx, path, buffer), name, value, size, 0, XATTR_NOFOLLOW);
36 #endif
37 }
38
39 static ssize_t mp_user_listxattr(FsContext *ctx, const char *path,
40                                  char *name, void *value, size_t size)
41 {
42     int name_size = strlen(name) + 1;
43     if (strncmp(name, "user.virtfs.", 12) == 0) {
44
45         /*  check if it is a mapped posix acl */
46         if (strncmp(name, "user.virtfs.system.posix_acl_", 29) == 0) {
47             /* adjust the name and size */
48             name += 12;
49             name_size -= 12;
50         } else {
51             /*
52              * Don't allow fetch of user.virtfs namesapce
53              * in case of mapped security
54              */
55             return 0;
56         }
57     }
58     if (!value) {
59         return name_size;
60     }
61
62     if (size < name_size) {
63         errno = ERANGE;
64         return -1;
65     }
66
67     /* name_size includes the trailing NUL. */
68     memcpy(value, name, name_size);
69     return name_size;
70 }
71
72 static int mp_user_setxattr(FsContext *ctx, const char *path, const char *name,
73                             void *value, size_t size, int flags)
74 {
75     char buffer[PATH_MAX];
76     if (strncmp(name, "user.virtfs.", 12) == 0) {
77         /*
78          * Don't allow fetch of user.virtfs namesapce
79          * in case of mapped security
80          */
81         errno = EACCES;
82         return -1;
83     }
84 #ifdef CONFIG_LINUX
85     return lsetxattr(rpath(ctx, path, buffer), name, value, size, flags);
86 #else
87     return setxattr(rpath(ctx, path, buffer), name, value, size, 0, flags | XATTR_NOFOLLOW);
88 #endif
89 }
90
91 static int mp_user_removexattr(FsContext *ctx,
92                                const char *path, const char *name)
93 {
94     char buffer[PATH_MAX];
95     if (strncmp(name, "user.virtfs.", 12) == 0) {
96         /*
97          * Don't allow fetch of user.virtfs namesapce
98          * in case of mapped security
99          */
100         errno = EACCES;
101         return -1;
102     }
103 #ifdef CONFIG_LINUX
104     return lremovexattr(rpath(ctx, path, buffer), name);
105 #else
106     return removexattr(rpath(ctx, path, buffer), name, XATTR_NOFOLLOW);
107 #endif
108 }
109
110 XattrOperations mapped_user_xattr = {
111     .name = "user.",
112     .getxattr = mp_user_getxattr,
113     .setxattr = mp_user_setxattr,
114     .listxattr = mp_user_listxattr,
115     .removexattr = mp_user_removexattr,
116 };
117
118 XattrOperations passthrough_user_xattr = {
119     .name = "user.",
120     .getxattr = pt_getxattr,
121     .setxattr = pt_setxattr,
122     .listxattr = pt_listxattr,
123     .removexattr = pt_removexattr,
124 };