Add qemu 2.4.0
[kvmfornfv.git] / qemu / 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
14 #include <sys/types.h>
15 #include "hw/virtio/virtio.h"
16 #include "virtio-9p.h"
17 #include "fsdev/file-op-9p.h"
18 #include "virtio-9p-xattr.h"
19
20
21 static ssize_t mp_user_getxattr(FsContext *ctx, const char *path,
22                                 const char *name, void *value, size_t size)
23 {
24     char *buffer;
25     ssize_t ret;
26
27     if (strncmp(name, "user.virtfs.", 12) == 0) {
28         /*
29          * Don't allow fetch of user.virtfs namesapce
30          * in case of mapped security
31          */
32         errno = ENOATTR;
33         return -1;
34     }
35     buffer = rpath(ctx, path);
36     ret = lgetxattr(buffer, name, value, size);
37     g_free(buffer);
38     return ret;
39 }
40
41 static ssize_t mp_user_listxattr(FsContext *ctx, const char *path,
42                                  char *name, void *value, size_t size)
43 {
44     int name_size = strlen(name) + 1;
45     if (strncmp(name, "user.virtfs.", 12) == 0) {
46
47         /*  check if it is a mapped posix acl */
48         if (strncmp(name, "user.virtfs.system.posix_acl_", 29) == 0) {
49             /* adjust the name and size */
50             name += 12;
51             name_size -= 12;
52         } else {
53             /*
54              * Don't allow fetch of user.virtfs namesapce
55              * in case of mapped security
56              */
57             return 0;
58         }
59     }
60     if (!value) {
61         return name_size;
62     }
63
64     if (size < name_size) {
65         errno = ERANGE;
66         return -1;
67     }
68
69     /* name_size includes the trailing NUL. */
70     memcpy(value, name, name_size);
71     return name_size;
72 }
73
74 static int mp_user_setxattr(FsContext *ctx, const char *path, const char *name,
75                             void *value, size_t size, int flags)
76 {
77     char *buffer;
78     int ret;
79
80     if (strncmp(name, "user.virtfs.", 12) == 0) {
81         /*
82          * Don't allow fetch of user.virtfs namesapce
83          * in case of mapped security
84          */
85         errno = EACCES;
86         return -1;
87     }
88     buffer = rpath(ctx, path);
89     ret = lsetxattr(buffer, name, value, size, flags);
90     g_free(buffer);
91     return ret;
92 }
93
94 static int mp_user_removexattr(FsContext *ctx,
95                                const char *path, const char *name)
96 {
97     char *buffer;
98     int ret;
99
100     if (strncmp(name, "user.virtfs.", 12) == 0) {
101         /*
102          * Don't allow fetch of user.virtfs namesapce
103          * in case of mapped security
104          */
105         errno = EACCES;
106         return -1;
107     }
108     buffer = rpath(ctx, path);
109     ret = lremovexattr(buffer, name);
110     g_free(buffer);
111     return ret;
112 }
113
114 XattrOperations mapped_user_xattr = {
115     .name = "user.",
116     .getxattr = mp_user_getxattr,
117     .setxattr = mp_user_setxattr,
118     .listxattr = mp_user_listxattr,
119     .removexattr = mp_user_removexattr,
120 };
121
122 XattrOperations passthrough_user_xattr = {
123     .name = "user.",
124     .getxattr = pt_getxattr,
125     .setxattr = pt_setxattr,
126     .listxattr = pt_listxattr,
127     .removexattr = pt_removexattr,
128 };