Statistics
| Branch: | Revision:

root / hw / 9pfs / virtio-9p-xattr.c @ 9eb08a43

History | View | Annotate | Download (3.9 kB)

1 fc22118d Aneesh Kumar K.V
/*
2 fc22118d Aneesh Kumar K.V
 * Virtio 9p  xattr callback
3 fc22118d Aneesh Kumar K.V
 *
4 fc22118d Aneesh Kumar K.V
 * Copyright IBM, Corp. 2010
5 fc22118d Aneesh Kumar K.V
 *
6 fc22118d Aneesh Kumar K.V
 * Authors:
7 fc22118d Aneesh Kumar K.V
 * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
8 fc22118d Aneesh Kumar K.V
 *
9 fc22118d Aneesh Kumar K.V
 * This work is licensed under the terms of the GNU GPL, version 2.  See
10 fc22118d Aneesh Kumar K.V
 * the COPYING file in the top-level directory.
11 fc22118d Aneesh Kumar K.V
 *
12 fc22118d Aneesh Kumar K.V
 */
13 fc22118d Aneesh Kumar K.V
14 0d09e41a Paolo Bonzini
#include "hw/virtio/virtio.h"
15 fc22118d Aneesh Kumar K.V
#include "virtio-9p.h"
16 353ac78d Aneesh Kumar K.V
#include "fsdev/file-op-9p.h"
17 fc22118d Aneesh Kumar K.V
#include "virtio-9p-xattr.h"
18 fc22118d Aneesh Kumar K.V
19 fc22118d Aneesh Kumar K.V
20 fc22118d Aneesh Kumar K.V
static XattrOperations *get_xattr_operations(XattrOperations **h,
21 fc22118d Aneesh Kumar K.V
                                             const char *name)
22 fc22118d Aneesh Kumar K.V
{
23 fc22118d Aneesh Kumar K.V
    XattrOperations *xops;
24 fc22118d Aneesh Kumar K.V
    for (xops = *(h)++; xops != NULL; xops = *(h)++) {
25 fc22118d Aneesh Kumar K.V
        if (!strncmp(name, xops->name, strlen(xops->name))) {
26 fc22118d Aneesh Kumar K.V
            return xops;
27 fc22118d Aneesh Kumar K.V
        }
28 fc22118d Aneesh Kumar K.V
    }
29 fc22118d Aneesh Kumar K.V
    return NULL;
30 fc22118d Aneesh Kumar K.V
}
31 fc22118d Aneesh Kumar K.V
32 fc22118d Aneesh Kumar K.V
ssize_t v9fs_get_xattr(FsContext *ctx, const char *path,
33 fc22118d Aneesh Kumar K.V
                       const char *name, void *value, size_t size)
34 fc22118d Aneesh Kumar K.V
{
35 fc22118d Aneesh Kumar K.V
    XattrOperations *xops = get_xattr_operations(ctx->xops, name);
36 fc22118d Aneesh Kumar K.V
    if (xops) {
37 fc22118d Aneesh Kumar K.V
        return xops->getxattr(ctx, path, name, value, size);
38 fc22118d Aneesh Kumar K.V
    }
39 8af00205 Daniel P. Berrange
    errno = EOPNOTSUPP;
40 fc22118d Aneesh Kumar K.V
    return -1;
41 fc22118d Aneesh Kumar K.V
}
42 fc22118d Aneesh Kumar K.V
43 fc22118d Aneesh Kumar K.V
ssize_t pt_listxattr(FsContext *ctx, const char *path,
44 fc22118d Aneesh Kumar K.V
                     char *name, void *value, size_t size)
45 fc22118d Aneesh Kumar K.V
{
46 fc22118d Aneesh Kumar K.V
    int name_size = strlen(name) + 1;
47 fc22118d Aneesh Kumar K.V
    if (!value) {
48 fc22118d Aneesh Kumar K.V
        return name_size;
49 fc22118d Aneesh Kumar K.V
    }
50 fc22118d Aneesh Kumar K.V
51 fc22118d Aneesh Kumar K.V
    if (size < name_size) {
52 fc22118d Aneesh Kumar K.V
        errno = ERANGE;
53 fc22118d Aneesh Kumar K.V
        return -1;
54 fc22118d Aneesh Kumar K.V
    }
55 fc22118d Aneesh Kumar K.V
56 9238c209 Jim Meyering
    /* no need for strncpy: name_size is strlen(name)+1 */
57 9238c209 Jim Meyering
    memcpy(value, name, name_size);
58 fc22118d Aneesh Kumar K.V
    return name_size;
59 fc22118d Aneesh Kumar K.V
}
60 fc22118d Aneesh Kumar K.V
61 fc22118d Aneesh Kumar K.V
62 fc22118d Aneesh Kumar K.V
/*
63 fc22118d Aneesh Kumar K.V
 * Get the list and pass to each layer to find out whether
64 fc22118d Aneesh Kumar K.V
 * to send the data or not
65 fc22118d Aneesh Kumar K.V
 */
66 fc22118d Aneesh Kumar K.V
ssize_t v9fs_list_xattr(FsContext *ctx, const char *path,
67 fc22118d Aneesh Kumar K.V
                        void *value, size_t vsize)
68 fc22118d Aneesh Kumar K.V
{
69 fc22118d Aneesh Kumar K.V
    ssize_t size = 0;
70 faa44e3d Venkateswararao Jujjuri (JV)
    char buffer[PATH_MAX];
71 fc22118d Aneesh Kumar K.V
    void *ovalue = value;
72 fc22118d Aneesh Kumar K.V
    XattrOperations *xops;
73 fc22118d Aneesh Kumar K.V
    char *orig_value, *orig_value_start;
74 fc22118d Aneesh Kumar K.V
    ssize_t xattr_len, parsed_len = 0, attr_len;
75 fc22118d Aneesh Kumar K.V
76 fc22118d Aneesh Kumar K.V
    /* Get the actual len */
77 faa44e3d Venkateswararao Jujjuri (JV)
    xattr_len = llistxattr(rpath(ctx, path, buffer), value, 0);
78 0562c674 Kusanagi Kouichi
    if (xattr_len <= 0) {
79 0562c674 Kusanagi Kouichi
        return xattr_len;
80 0562c674 Kusanagi Kouichi
    }
81 fc22118d Aneesh Kumar K.V
82 fc22118d Aneesh Kumar K.V
    /* Now fetch the xattr and find the actual size */
83 7267c094 Anthony Liguori
    orig_value = g_malloc(xattr_len);
84 faa44e3d Venkateswararao Jujjuri (JV)
    xattr_len = llistxattr(rpath(ctx, path, buffer), orig_value, xattr_len);
85 fc22118d Aneesh Kumar K.V
86 fc22118d Aneesh Kumar K.V
    /* store the orig pointer */
87 fc22118d Aneesh Kumar K.V
    orig_value_start = orig_value;
88 fc22118d Aneesh Kumar K.V
    while (xattr_len > parsed_len) {
89 fc22118d Aneesh Kumar K.V
        xops = get_xattr_operations(ctx->xops, orig_value);
90 fc22118d Aneesh Kumar K.V
        if (!xops) {
91 fc22118d Aneesh Kumar K.V
            goto next_entry;
92 fc22118d Aneesh Kumar K.V
        }
93 fc22118d Aneesh Kumar K.V
94 fc22118d Aneesh Kumar K.V
        if (!value) {
95 fc22118d Aneesh Kumar K.V
            size += xops->listxattr(ctx, path, orig_value, value, vsize);
96 fc22118d Aneesh Kumar K.V
        } else {
97 fc22118d Aneesh Kumar K.V
            size = xops->listxattr(ctx, path, orig_value, value, vsize);
98 fc22118d Aneesh Kumar K.V
            if (size < 0) {
99 fc22118d Aneesh Kumar K.V
                goto err_out;
100 fc22118d Aneesh Kumar K.V
            }
101 fc22118d Aneesh Kumar K.V
            value += size;
102 fc22118d Aneesh Kumar K.V
            vsize -= size;
103 fc22118d Aneesh Kumar K.V
        }
104 fc22118d Aneesh Kumar K.V
next_entry:
105 fc22118d Aneesh Kumar K.V
        /* Got the next entry */
106 fc22118d Aneesh Kumar K.V
        attr_len = strlen(orig_value) + 1;
107 fc22118d Aneesh Kumar K.V
        parsed_len += attr_len;
108 fc22118d Aneesh Kumar K.V
        orig_value += attr_len;
109 fc22118d Aneesh Kumar K.V
    }
110 fc22118d Aneesh Kumar K.V
    if (value) {
111 fc22118d Aneesh Kumar K.V
        size = value - ovalue;
112 fc22118d Aneesh Kumar K.V
    }
113 fc22118d Aneesh Kumar K.V
114 fc22118d Aneesh Kumar K.V
err_out:
115 7267c094 Anthony Liguori
    g_free(orig_value_start);
116 fc22118d Aneesh Kumar K.V
    return size;
117 fc22118d Aneesh Kumar K.V
}
118 fc22118d Aneesh Kumar K.V
119 fc22118d Aneesh Kumar K.V
int v9fs_set_xattr(FsContext *ctx, const char *path, const char *name,
120 fc22118d Aneesh Kumar K.V
                   void *value, size_t size, int flags)
121 fc22118d Aneesh Kumar K.V
{
122 fc22118d Aneesh Kumar K.V
    XattrOperations *xops = get_xattr_operations(ctx->xops, name);
123 fc22118d Aneesh Kumar K.V
    if (xops) {
124 fc22118d Aneesh Kumar K.V
        return xops->setxattr(ctx, path, name, value, size, flags);
125 fc22118d Aneesh Kumar K.V
    }
126 8af00205 Daniel P. Berrange
    errno = EOPNOTSUPP;
127 fc22118d Aneesh Kumar K.V
    return -1;
128 fc22118d Aneesh Kumar K.V
129 fc22118d Aneesh Kumar K.V
}
130 fc22118d Aneesh Kumar K.V
131 fc22118d Aneesh Kumar K.V
int v9fs_remove_xattr(FsContext *ctx,
132 fc22118d Aneesh Kumar K.V
                      const char *path, const char *name)
133 fc22118d Aneesh Kumar K.V
{
134 fc22118d Aneesh Kumar K.V
    XattrOperations *xops = get_xattr_operations(ctx->xops, name);
135 fc22118d Aneesh Kumar K.V
    if (xops) {
136 fc22118d Aneesh Kumar K.V
        return xops->removexattr(ctx, path, name);
137 fc22118d Aneesh Kumar K.V
    }
138 8af00205 Daniel P. Berrange
    errno = EOPNOTSUPP;
139 fc22118d Aneesh Kumar K.V
    return -1;
140 fc22118d Aneesh Kumar K.V
141 fc22118d Aneesh Kumar K.V
}
142 fc22118d Aneesh Kumar K.V
143 fc22118d Aneesh Kumar K.V
XattrOperations *mapped_xattr_ops[] = {
144 fc22118d Aneesh Kumar K.V
    &mapped_user_xattr,
145 70fc55eb Aneesh Kumar K.V
    &mapped_pacl_xattr,
146 70fc55eb Aneesh Kumar K.V
    &mapped_dacl_xattr,
147 fc22118d Aneesh Kumar K.V
    NULL,
148 fc22118d Aneesh Kumar K.V
};
149 fc22118d Aneesh Kumar K.V
150 fc22118d Aneesh Kumar K.V
XattrOperations *passthrough_xattr_ops[] = {
151 fc22118d Aneesh Kumar K.V
    &passthrough_user_xattr,
152 70fc55eb Aneesh Kumar K.V
    &passthrough_acl_xattr,
153 fc22118d Aneesh Kumar K.V
    NULL,
154 fc22118d Aneesh Kumar K.V
};
155 fc22118d Aneesh Kumar K.V
156 fc22118d Aneesh Kumar K.V
/* for .user none model should be same as passthrough */
157 fc22118d Aneesh Kumar K.V
XattrOperations *none_xattr_ops[] = {
158 fc22118d Aneesh Kumar K.V
    &passthrough_user_xattr,
159 70fc55eb Aneesh Kumar K.V
    &none_acl_xattr,
160 fc22118d Aneesh Kumar K.V
    NULL,
161 fc22118d Aneesh Kumar K.V
};