2010-10-18 11:58:16 +02:00
|
|
|
/*
|
2015-11-18 19:31:52 +01:00
|
|
|
* 9p xattr callback
|
2010-10-18 11:58:16 +02:00
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2010
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2. See
|
|
|
|
* the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-26 19:17:10 +01:00
|
|
|
#include "qemu/osdep.h"
|
2016-01-07 19:18:02 +01:00
|
|
|
#include "9p.h"
|
2011-01-28 13:39:08 +01:00
|
|
|
#include "fsdev/file-op-9p.h"
|
2015-11-18 19:31:52 +01:00
|
|
|
#include "9p-xattr.h"
|
2017-02-26 23:42:26 +01:00
|
|
|
#include "9p-util.h"
|
|
|
|
#include "9p-local.h"
|
2010-10-18 11:58:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
static XattrOperations *get_xattr_operations(XattrOperations **h,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
XattrOperations *xops;
|
|
|
|
for (xops = *(h)++; xops != NULL; xops = *(h)++) {
|
|
|
|
if (!strncmp(name, xops->name, strlen(xops->name))) {
|
|
|
|
return xops;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t v9fs_get_xattr(FsContext *ctx, const char *path,
|
|
|
|
const char *name, void *value, size_t size)
|
|
|
|
{
|
|
|
|
XattrOperations *xops = get_xattr_operations(ctx->xops, name);
|
|
|
|
if (xops) {
|
|
|
|
return xops->getxattr(ctx, path, name, value, size);
|
|
|
|
}
|
2013-10-01 13:28:17 +02:00
|
|
|
errno = EOPNOTSUPP;
|
2010-10-18 11:58:16 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t pt_listxattr(FsContext *ctx, const char *path,
|
|
|
|
char *name, void *value, size_t size)
|
|
|
|
{
|
|
|
|
int name_size = strlen(name) + 1;
|
|
|
|
if (!value) {
|
|
|
|
return name_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size < name_size) {
|
|
|
|
errno = ERANGE;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-10-04 13:09:56 +02:00
|
|
|
/* no need for strncpy: name_size is strlen(name)+1 */
|
|
|
|
memcpy(value, name, name_size);
|
2010-10-18 11:58:16 +02:00
|
|
|
return name_size;
|
|
|
|
}
|
|
|
|
|
2017-02-26 23:42:34 +01:00
|
|
|
static ssize_t flistxattrat_nofollow(int dirfd, const char *filename,
|
|
|
|
char *list, size_t size)
|
|
|
|
{
|
|
|
|
char *proc_path = g_strdup_printf("/proc/self/fd/%d/%s", dirfd, filename);
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = llistxattr(proc_path, list, size);
|
|
|
|
g_free(proc_path);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-10-18 11:58:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the list and pass to each layer to find out whether
|
|
|
|
* to send the data or not
|
|
|
|
*/
|
|
|
|
ssize_t v9fs_list_xattr(FsContext *ctx, const char *path,
|
|
|
|
void *value, size_t vsize)
|
|
|
|
{
|
|
|
|
ssize_t size = 0;
|
|
|
|
void *ovalue = value;
|
|
|
|
XattrOperations *xops;
|
|
|
|
char *orig_value, *orig_value_start;
|
|
|
|
ssize_t xattr_len, parsed_len = 0, attr_len;
|
2017-02-26 23:42:34 +01:00
|
|
|
char *dirpath, *name;
|
|
|
|
int dirfd;
|
2010-10-18 11:58:16 +02:00
|
|
|
|
|
|
|
/* Get the actual len */
|
2017-02-26 23:42:34 +01:00
|
|
|
dirpath = g_path_get_dirname(path);
|
|
|
|
dirfd = local_opendir_nofollow(ctx, dirpath);
|
|
|
|
g_free(dirpath);
|
|
|
|
if (dirfd == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
name = g_path_get_basename(path);
|
|
|
|
xattr_len = flistxattrat_nofollow(dirfd, name, value, 0);
|
2010-10-30 04:52:39 +02:00
|
|
|
if (xattr_len <= 0) {
|
2017-02-26 23:42:34 +01:00
|
|
|
g_free(name);
|
|
|
|
close_preserve_errno(dirfd);
|
2010-10-30 04:52:39 +02:00
|
|
|
return xattr_len;
|
|
|
|
}
|
2010-10-18 11:58:16 +02:00
|
|
|
|
|
|
|
/* Now fetch the xattr and find the actual size */
|
2011-08-21 05:09:37 +02:00
|
|
|
orig_value = g_malloc(xattr_len);
|
2017-02-26 23:42:34 +01:00
|
|
|
xattr_len = flistxattrat_nofollow(dirfd, name, orig_value, xattr_len);
|
|
|
|
g_free(name);
|
|
|
|
close_preserve_errno(dirfd);
|
|
|
|
if (xattr_len < 0) {
|
2017-04-07 12:48:52 +02:00
|
|
|
g_free(orig_value);
|
2017-02-26 23:42:34 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-10-18 11:58:16 +02:00
|
|
|
|
|
|
|
/* store the orig pointer */
|
|
|
|
orig_value_start = orig_value;
|
|
|
|
while (xattr_len > parsed_len) {
|
|
|
|
xops = get_xattr_operations(ctx->xops, orig_value);
|
|
|
|
if (!xops) {
|
|
|
|
goto next_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
size += xops->listxattr(ctx, path, orig_value, value, vsize);
|
|
|
|
} else {
|
|
|
|
size = xops->listxattr(ctx, path, orig_value, value, vsize);
|
|
|
|
if (size < 0) {
|
|
|
|
goto err_out;
|
|
|
|
}
|
|
|
|
value += size;
|
|
|
|
vsize -= size;
|
|
|
|
}
|
|
|
|
next_entry:
|
|
|
|
/* Got the next entry */
|
|
|
|
attr_len = strlen(orig_value) + 1;
|
|
|
|
parsed_len += attr_len;
|
|
|
|
orig_value += attr_len;
|
|
|
|
}
|
|
|
|
if (value) {
|
|
|
|
size = value - ovalue;
|
|
|
|
}
|
|
|
|
|
|
|
|
err_out:
|
2011-08-21 05:09:37 +02:00
|
|
|
g_free(orig_value_start);
|
2010-10-18 11:58:16 +02:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
int v9fs_set_xattr(FsContext *ctx, const char *path, const char *name,
|
|
|
|
void *value, size_t size, int flags)
|
|
|
|
{
|
|
|
|
XattrOperations *xops = get_xattr_operations(ctx->xops, name);
|
|
|
|
if (xops) {
|
|
|
|
return xops->setxattr(ctx, path, name, value, size, flags);
|
|
|
|
}
|
2013-10-01 13:28:17 +02:00
|
|
|
errno = EOPNOTSUPP;
|
2010-10-18 11:58:16 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int v9fs_remove_xattr(FsContext *ctx,
|
|
|
|
const char *path, const char *name)
|
|
|
|
{
|
|
|
|
XattrOperations *xops = get_xattr_operations(ctx->xops, name);
|
|
|
|
if (xops) {
|
|
|
|
return xops->removexattr(ctx, path, name);
|
|
|
|
}
|
2013-10-01 13:28:17 +02:00
|
|
|
errno = EOPNOTSUPP;
|
2010-10-18 11:58:16 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-02-26 23:42:26 +01:00
|
|
|
ssize_t local_getxattr_nofollow(FsContext *ctx, const char *path,
|
|
|
|
const char *name, void *value, size_t size)
|
2017-02-26 23:41:40 +01:00
|
|
|
{
|
2017-02-26 23:42:26 +01:00
|
|
|
char *dirpath = g_path_get_dirname(path);
|
|
|
|
char *filename = g_path_get_basename(path);
|
|
|
|
int dirfd;
|
|
|
|
ssize_t ret = -1;
|
|
|
|
|
|
|
|
dirfd = local_opendir_nofollow(ctx, dirpath);
|
|
|
|
if (dirfd == -1) {
|
|
|
|
goto out;
|
|
|
|
}
|
2017-02-26 23:41:40 +01:00
|
|
|
|
2017-02-26 23:42:26 +01:00
|
|
|
ret = fgetxattrat_nofollow(dirfd, filename, name, value, size);
|
|
|
|
close_preserve_errno(dirfd);
|
|
|
|
out:
|
|
|
|
g_free(dirpath);
|
|
|
|
g_free(filename);
|
2017-02-26 23:41:40 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-26 23:42:26 +01:00
|
|
|
ssize_t pt_getxattr(FsContext *ctx, const char *path, const char *name,
|
|
|
|
void *value, size_t size)
|
|
|
|
{
|
|
|
|
return local_getxattr_nofollow(ctx, path, name, value, size);
|
|
|
|
}
|
|
|
|
|
2017-02-26 23:42:43 +01:00
|
|
|
int fsetxattrat_nofollow(int dirfd, const char *filename, const char *name,
|
|
|
|
void *value, size_t size, int flags)
|
2017-02-26 23:41:40 +01:00
|
|
|
{
|
2017-02-26 23:42:43 +01:00
|
|
|
char *proc_path = g_strdup_printf("/proc/self/fd/%d/%s", dirfd, filename);
|
2017-02-26 23:41:40 +01:00
|
|
|
int ret;
|
|
|
|
|
2017-02-26 23:42:43 +01:00
|
|
|
ret = lsetxattr(proc_path, name, value, size, flags);
|
|
|
|
g_free(proc_path);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t local_setxattr_nofollow(FsContext *ctx, const char *path,
|
|
|
|
const char *name, void *value, size_t size,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
char *dirpath = g_path_get_dirname(path);
|
|
|
|
char *filename = g_path_get_basename(path);
|
|
|
|
int dirfd;
|
|
|
|
ssize_t ret = -1;
|
|
|
|
|
|
|
|
dirfd = local_opendir_nofollow(ctx, dirpath);
|
|
|
|
if (dirfd == -1) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fsetxattrat_nofollow(dirfd, filename, name, value, size, flags);
|
|
|
|
close_preserve_errno(dirfd);
|
|
|
|
out:
|
|
|
|
g_free(dirpath);
|
|
|
|
g_free(filename);
|
2017-02-26 23:41:40 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-26 23:42:43 +01:00
|
|
|
int pt_setxattr(FsContext *ctx, const char *path, const char *name, void *value,
|
|
|
|
size_t size, int flags)
|
|
|
|
{
|
|
|
|
return local_setxattr_nofollow(ctx, path, name, value, size, flags);
|
|
|
|
}
|
|
|
|
|
2017-02-26 23:42:51 +01:00
|
|
|
static ssize_t fremovexattrat_nofollow(int dirfd, const char *filename,
|
|
|
|
const char *name)
|
2017-02-26 23:41:40 +01:00
|
|
|
{
|
2017-02-26 23:42:51 +01:00
|
|
|
char *proc_path = g_strdup_printf("/proc/self/fd/%d/%s", dirfd, filename);
|
2017-02-26 23:41:40 +01:00
|
|
|
int ret;
|
|
|
|
|
2017-02-26 23:42:51 +01:00
|
|
|
ret = lremovexattr(proc_path, name);
|
|
|
|
g_free(proc_path);
|
2017-02-26 23:41:40 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-02-26 23:42:51 +01:00
|
|
|
ssize_t local_removexattr_nofollow(FsContext *ctx, const char *path,
|
|
|
|
const char *name)
|
|
|
|
{
|
|
|
|
char *dirpath = g_path_get_dirname(path);
|
|
|
|
char *filename = g_path_get_basename(path);
|
|
|
|
int dirfd;
|
|
|
|
ssize_t ret = -1;
|
|
|
|
|
|
|
|
dirfd = local_opendir_nofollow(ctx, dirpath);
|
|
|
|
if (dirfd == -1) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = fremovexattrat_nofollow(dirfd, filename, name);
|
|
|
|
close_preserve_errno(dirfd);
|
|
|
|
out:
|
|
|
|
g_free(dirpath);
|
|
|
|
g_free(filename);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pt_removexattr(FsContext *ctx, const char *path, const char *name)
|
|
|
|
{
|
|
|
|
return local_removexattr_nofollow(ctx, path, name);
|
|
|
|
}
|
|
|
|
|
2017-02-26 23:41:40 +01:00
|
|
|
ssize_t notsup_getxattr(FsContext *ctx, const char *path, const char *name,
|
|
|
|
void *value, size_t size)
|
|
|
|
{
|
|
|
|
errno = ENOTSUP;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int notsup_setxattr(FsContext *ctx, const char *path, const char *name,
|
|
|
|
void *value, size_t size, int flags)
|
|
|
|
{
|
|
|
|
errno = ENOTSUP;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t notsup_listxattr(FsContext *ctx, const char *path, char *name,
|
|
|
|
void *value, size_t size)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int notsup_removexattr(FsContext *ctx, const char *path, const char *name)
|
|
|
|
{
|
|
|
|
errno = ENOTSUP;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-10-18 11:58:16 +02:00
|
|
|
XattrOperations *mapped_xattr_ops[] = {
|
|
|
|
&mapped_user_xattr,
|
2010-10-18 11:58:16 +02:00
|
|
|
&mapped_pacl_xattr,
|
|
|
|
&mapped_dacl_xattr,
|
2010-10-18 11:58:16 +02:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
XattrOperations *passthrough_xattr_ops[] = {
|
|
|
|
&passthrough_user_xattr,
|
2010-10-18 11:58:16 +02:00
|
|
|
&passthrough_acl_xattr,
|
2010-10-18 11:58:16 +02:00
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* for .user none model should be same as passthrough */
|
|
|
|
XattrOperations *none_xattr_ops[] = {
|
|
|
|
&passthrough_user_xattr,
|
2010-10-18 11:58:16 +02:00
|
|
|
&none_acl_xattr,
|
2010-10-18 11:58:16 +02:00
|
|
|
NULL,
|
|
|
|
};
|