Merge branch 'tizen_qemu_2.0' into tizen
[sdk/emulator/qemu.git] / hw / 9pfs / virtio-9p-xattr.c
1 /*
2  * Virtio 9p  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 "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 XattrOperations *get_xattr_operations(XattrOperations **h,
21                                              const char *name)
22 {
23     XattrOperations *xops;
24     for (xops = *(h)++; xops != NULL; xops = *(h)++) {
25         if (!strncmp(name, xops->name, strlen(xops->name))) {
26             return xops;
27         }
28     }
29     return NULL;
30 }
31
32 ssize_t v9fs_get_xattr(FsContext *ctx, const char *path,
33                        const char *name, void *value, size_t size)
34 {
35     XattrOperations *xops = get_xattr_operations(ctx->xops, name);
36     if (xops) {
37         return xops->getxattr(ctx, path, name, value, size);
38     }
39     errno = EOPNOTSUPP;
40     return -1;
41 }
42
43 ssize_t pt_listxattr(FsContext *ctx, const char *path,
44                      char *name, void *value, size_t size)
45 {
46     int name_size = strlen(name) + 1;
47     if (!value) {
48         return name_size;
49     }
50
51     if (size < name_size) {
52         errno = ERANGE;
53         return -1;
54     }
55
56     /* no need for strncpy: name_size is strlen(name)+1 */
57     memcpy(value, name, name_size);
58     return name_size;
59 }
60
61
62 /*
63  * Get the list and pass to each layer to find out whether
64  * to send the data or not
65  */
66 ssize_t v9fs_list_xattr(FsContext *ctx, const char *path,
67                         void *value, size_t vsize)
68 {
69     ssize_t size = 0;
70     char *buffer;
71     void *ovalue = value;
72     XattrOperations *xops;
73     char *orig_value, *orig_value_start;
74     ssize_t xattr_len, parsed_len = 0, attr_len;
75
76     /* Get the actual len */
77     buffer = rpath(ctx, path);
78 #ifdef CONFIG_LINUX
79     xattr_len = llistxattr(buffer, value, 0);
80 #else
81     xattr_len = listxattr(buffer, value, 0, XATTR_NOFOLLOW);
82 #endif
83     if (xattr_len <= 0) {
84         g_free(buffer);
85         return xattr_len;
86     }
87
88     /* Now fetch the xattr and find the actual size */
89     orig_value = g_malloc(xattr_len);
90 #ifdef CONFIG_LINUX
91     xattr_len = llistxattr(buffer, orig_value, xattr_len);
92 #else
93     xattr_len = listxattr(buffer, orig_value, xattr_len, XATTR_NOFOLLOW);
94 #endif
95     g_free(buffer);
96
97     /* store the orig pointer */
98     orig_value_start = orig_value;
99     while (xattr_len > parsed_len) {
100         xops = get_xattr_operations(ctx->xops, orig_value);
101         if (!xops) {
102             goto next_entry;
103         }
104
105         if (!value) {
106             size += xops->listxattr(ctx, path, orig_value, value, vsize);
107         } else {
108             size = xops->listxattr(ctx, path, orig_value, value, vsize);
109             if (size < 0) {
110                 goto err_out;
111             }
112             value += size;
113             vsize -= size;
114         }
115 next_entry:
116         /* Got the next entry */
117         attr_len = strlen(orig_value) + 1;
118         parsed_len += attr_len;
119         orig_value += attr_len;
120     }
121     if (value) {
122         size = value - ovalue;
123     }
124
125 err_out:
126     g_free(orig_value_start);
127     return size;
128 }
129
130 int v9fs_set_xattr(FsContext *ctx, const char *path, const char *name,
131                    void *value, size_t size, int flags)
132 {
133     XattrOperations *xops = get_xattr_operations(ctx->xops, name);
134     if (xops) {
135         return xops->setxattr(ctx, path, name, value, size, flags);
136     }
137     errno = EOPNOTSUPP;
138     return -1;
139
140 }
141
142 int v9fs_remove_xattr(FsContext *ctx,
143                       const char *path, const char *name)
144 {
145     XattrOperations *xops = get_xattr_operations(ctx->xops, name);
146     if (xops) {
147         return xops->removexattr(ctx, path, name);
148     }
149     errno = EOPNOTSUPP;
150     return -1;
151
152 }
153
154 XattrOperations *mapped_xattr_ops[] = {
155     &mapped_user_xattr,
156     &mapped_pacl_xattr,
157     &mapped_dacl_xattr,
158     NULL,
159 };
160
161 XattrOperations *passthrough_xattr_ops[] = {
162     &passthrough_user_xattr,
163     &passthrough_acl_xattr,
164     NULL,
165 };
166
167 /* for .user none model should be same as passthrough */
168 XattrOperations *none_xattr_ops[] = {
169     &passthrough_user_xattr,
170     &none_acl_xattr,
171     NULL,
172 };