From nobody Fri Oct 3 11:28:54 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1579804324; cv=none; d=zohomail.com; s=zohoarc; b=aMET32x/gfE1Jd4opawi6g5d26cYT+JjMjJmzA+u3RW0sJ03BT4bsXmgjUZ/HPBa6etraHq9wlpUtK6OiHBwMW9MRKHJmhVK+uF4i16/V7rWDSJDx9uQ+okc+nZG6qWau1JXGMr8CI+SgXv64WqgdV8HcczS297zL5kOg9xLxqc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1579804324; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=Pp/qATplDrz6aSdEj663YVGvomxACTRs1AhbwReR+AA=; b=f+vwHcXgq/nAfIy9xJmmFGCM7+C+dxN2CW3LJSsWiDhQ2Eh93/e/0gY6U4AvGTlhbTdyta6FAMyAosv9A1IAbJ2HxifD73aU7HphjPRe3SCW5III/Xf+b09UYjMiBtS0RgzcoJAKLKoxVryAB0Lr8AjgSo7/WzB63q4U965q9O4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1579804324832153.13372242887635; Thu, 23 Jan 2020 10:32:04 -0800 (PST) Received: from localhost ([::1]:34810 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iuhGt-0007p7-Fi for importer@patchew.org; Thu, 23 Jan 2020 13:32:03 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:40745) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iufd5-0003xN-6n for qemu-devel@nongnu.org; Thu, 23 Jan 2020 11:46:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iufd0-00062S-SZ for qemu-devel@nongnu.org; Thu, 23 Jan 2020 11:46:51 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:44830 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iufd0-00062I-PL for qemu-devel@nongnu.org; Thu, 23 Jan 2020 11:46:46 -0500 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-299-619Lz1agMaC_fd4fn-UeTQ-1; Thu, 23 Jan 2020 11:46:41 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1AE8C8018A6 for ; Thu, 23 Jan 2020 16:46:40 +0000 (UTC) Received: from dgilbert-t580.localhost (ovpn-116-110.ams2.redhat.com [10.36.116.110]) by smtp.corp.redhat.com (Postfix) with ESMTP id 667E128998; Thu, 23 Jan 2020 16:46:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579798006; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Pp/qATplDrz6aSdEj663YVGvomxACTRs1AhbwReR+AA=; b=TuFSEGV3b85HBKI3h2SJg+PzdP/BGzSyIqB4wes55xf4z5WK8IO1OL9QPQkK3CHem2S62v JjINHEIFQhiQD2PFwxE4JyoHedaxfOGLf2MpayJ5oB5k7mkdPvZsAXY1UP2tQ2baLqdDRh p1d4MexLUJdYjteK8h0Hi0FXuTXQdKI= From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, stefanha@redhat.com Subject: [PULL 003/108] virtiofsd: Add auxiliary .c's Date: Thu, 23 Jan 2020 16:44:45 +0000 Message-Id: <20200123164630.91498-4-dgilbert@redhat.com> In-Reply-To: <20200123164630.91498-1-dgilbert@redhat.com> References: <20200123164630.91498-1-dgilbert@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-MC-Unique: 619Lz1agMaC_fd4fn-UeTQ-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" From: "Dr. David Alan Gilbert" Add most of the non-main .c files we need from upstream fuse-3.8.0 Signed-off-by: Dr. David Alan Gilbert Reviewed-by: Daniel P. Berrang=C3=A9 Signed-off-by: Dr. David Alan Gilbert --- tools/virtiofsd/buffer.c | 321 ++++++++++++++++++++++++ tools/virtiofsd/fuse_log.c | 40 +++ tools/virtiofsd/fuse_opt.c | 423 +++++++++++++++++++++++++++++++ tools/virtiofsd/fuse_signals.c | 91 +++++++ tools/virtiofsd/helper.c | 440 +++++++++++++++++++++++++++++++++ 5 files changed, 1315 insertions(+) create mode 100644 tools/virtiofsd/buffer.c create mode 100644 tools/virtiofsd/fuse_log.c create mode 100644 tools/virtiofsd/fuse_opt.c create mode 100644 tools/virtiofsd/fuse_signals.c create mode 100644 tools/virtiofsd/helper.c diff --git a/tools/virtiofsd/buffer.c b/tools/virtiofsd/buffer.c new file mode 100644 index 0000000000..5ab9b87455 --- /dev/null +++ b/tools/virtiofsd/buffer.c @@ -0,0 +1,321 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2010 Miklos Szeredi + + Functions for dealing with `struct fuse_buf` and `struct + fuse_bufvec`. + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#define _GNU_SOURCE + +#include "config.h" +#include "fuse_i.h" +#include "fuse_lowlevel.h" +#include +#include +#include +#include + +size_t fuse_buf_size(const struct fuse_bufvec *bufv) +{ + size_t i; + size_t size =3D 0; + + for (i =3D 0; i < bufv->count; i++) { + if (bufv->buf[i].size =3D=3D SIZE_MAX) + size =3D SIZE_MAX; + else + size +=3D bufv->buf[i].size; + } + + return size; +} + +static size_t min_size(size_t s1, size_t s2) +{ + return s1 < s2 ? s1 : s2; +} + +static ssize_t fuse_buf_write(const struct fuse_buf *dst, size_t dst_off, + const struct fuse_buf *src, size_t src_off, + size_t len) +{ + ssize_t res =3D 0; + size_t copied =3D 0; + + while (len) { + if (dst->flags & FUSE_BUF_FD_SEEK) { + res =3D pwrite(dst->fd, (char *)src->mem + src_off, len, + dst->pos + dst_off); + } else { + res =3D write(dst->fd, (char *)src->mem + src_off, len); + } + if (res =3D=3D -1) { + if (!copied) + return -errno; + break; + } + if (res =3D=3D 0) + break; + + copied +=3D res; + if (!(dst->flags & FUSE_BUF_FD_RETRY)) + break; + + src_off +=3D res; + dst_off +=3D res; + len -=3D res; + } + + return copied; +} + +static ssize_t fuse_buf_read(const struct fuse_buf *dst, size_t dst_off, + const struct fuse_buf *src, size_t src_off, + size_t len) +{ + ssize_t res =3D 0; + size_t copied =3D 0; + + while (len) { + if (src->flags & FUSE_BUF_FD_SEEK) { + res =3D pread(src->fd, (char *)dst->mem + dst_off, len, + src->pos + src_off); + } else { + res =3D read(src->fd, (char *)dst->mem + dst_off, len); + } + if (res =3D=3D -1) { + if (!copied) + return -errno; + break; + } + if (res =3D=3D 0) + break; + + copied +=3D res; + if (!(src->flags & FUSE_BUF_FD_RETRY)) + break; + + dst_off +=3D res; + src_off +=3D res; + len -=3D res; + } + + return copied; +} + +static ssize_t fuse_buf_fd_to_fd(const struct fuse_buf *dst, size_t dst_of= f, + const struct fuse_buf *src, size_t src_off, + size_t len) +{ + char buf[4096]; + struct fuse_buf tmp =3D { + .size =3D sizeof(buf), + .flags =3D 0, + }; + ssize_t res; + size_t copied =3D 0; + + tmp.mem =3D buf; + + while (len) { + size_t this_len =3D min_size(tmp.size, len); + size_t read_len; + + res =3D fuse_buf_read(&tmp, 0, src, src_off, this_len); + if (res < 0) { + if (!copied) + return res; + break; + } + if (res =3D=3D 0) + break; + + read_len =3D res; + res =3D fuse_buf_write(dst, dst_off, &tmp, 0, read_len); + if (res < 0) { + if (!copied) + return res; + break; + } + if (res =3D=3D 0) + break; + + copied +=3D res; + + if (res < this_len) + break; + + dst_off +=3D res; + src_off +=3D res; + len -=3D res; + } + + return copied; +} + +#ifdef HAVE_SPLICE +static ssize_t fuse_buf_splice(const struct fuse_buf *dst, size_t dst_off, + const struct fuse_buf *src, size_t src_off, + size_t len, enum fuse_buf_copy_flags flags) +{ + int splice_flags =3D 0; + off_t *srcpos =3D NULL; + off_t *dstpos =3D NULL; + off_t srcpos_val; + off_t dstpos_val; + ssize_t res; + size_t copied =3D 0; + + if (flags & FUSE_BUF_SPLICE_MOVE) + splice_flags |=3D SPLICE_F_MOVE; + if (flags & FUSE_BUF_SPLICE_NONBLOCK) + splice_flags |=3D SPLICE_F_NONBLOCK; + + if (src->flags & FUSE_BUF_FD_SEEK) { + srcpos_val =3D src->pos + src_off; + srcpos =3D &srcpos_val; + } + if (dst->flags & FUSE_BUF_FD_SEEK) { + dstpos_val =3D dst->pos + dst_off; + dstpos =3D &dstpos_val; + } + + while (len) { + res =3D splice(src->fd, srcpos, dst->fd, dstpos, len, + splice_flags); + if (res =3D=3D -1) { + if (copied) + break; + + if (errno !=3D EINVAL || (flags & FUSE_BUF_FORCE_SPLICE)) + return -errno; + + /* Maybe splice is not supported for this combination */ + return fuse_buf_fd_to_fd(dst, dst_off, src, src_off, + len); + } + if (res =3D=3D 0) + break; + + copied +=3D res; + if (!(src->flags & FUSE_BUF_FD_RETRY) && + !(dst->flags & FUSE_BUF_FD_RETRY)) { + break; + } + + len -=3D res; + } + + return copied; +} +#else +static ssize_t fuse_buf_splice(const struct fuse_buf *dst, size_t dst_off, + const struct fuse_buf *src, size_t src_off, + size_t len, enum fuse_buf_copy_flags flags) +{ + (void) flags; + + return fuse_buf_fd_to_fd(dst, dst_off, src, src_off, len); +} +#endif + + +static ssize_t fuse_buf_copy_one(const struct fuse_buf *dst, size_t dst_of= f, + const struct fuse_buf *src, size_t src_off, + size_t len, enum fuse_buf_copy_flags flags) +{ + int src_is_fd =3D src->flags & FUSE_BUF_IS_FD; + int dst_is_fd =3D dst->flags & FUSE_BUF_IS_FD; + + if (!src_is_fd && !dst_is_fd) { + char *dstmem =3D (char *)dst->mem + dst_off; + char *srcmem =3D (char *)src->mem + src_off; + + if (dstmem !=3D srcmem) { + if (dstmem + len <=3D srcmem || srcmem + len <=3D dstmem) + memcpy(dstmem, srcmem, len); + else + memmove(dstmem, srcmem, len); + } + + return len; + } else if (!src_is_fd) { + return fuse_buf_write(dst, dst_off, src, src_off, len); + } else if (!dst_is_fd) { + return fuse_buf_read(dst, dst_off, src, src_off, len); + } else if (flags & FUSE_BUF_NO_SPLICE) { + return fuse_buf_fd_to_fd(dst, dst_off, src, src_off, len); + } else { + return fuse_buf_splice(dst, dst_off, src, src_off, len, flags); + } +} + +static const struct fuse_buf *fuse_bufvec_current(struct fuse_bufvec *bufv) +{ + if (bufv->idx < bufv->count) + return &bufv->buf[bufv->idx]; + else + return NULL; +} + +static int fuse_bufvec_advance(struct fuse_bufvec *bufv, size_t len) +{ + const struct fuse_buf *buf =3D fuse_bufvec_current(bufv); + + bufv->off +=3D len; + assert(bufv->off <=3D buf->size); + if (bufv->off =3D=3D buf->size) { + assert(bufv->idx < bufv->count); + bufv->idx++; + if (bufv->idx =3D=3D bufv->count) + return 0; + bufv->off =3D 0; + } + return 1; +} + +ssize_t fuse_buf_copy(struct fuse_bufvec *dstv, struct fuse_bufvec *srcv, + enum fuse_buf_copy_flags flags) +{ + size_t copied =3D 0; + + if (dstv =3D=3D srcv) + return fuse_buf_size(dstv); + + for (;;) { + const struct fuse_buf *src =3D fuse_bufvec_current(srcv); + const struct fuse_buf *dst =3D fuse_bufvec_current(dstv); + size_t src_len; + size_t dst_len; + size_t len; + ssize_t res; + + if (src =3D=3D NULL || dst =3D=3D NULL) + break; + + src_len =3D src->size - srcv->off; + dst_len =3D dst->size - dstv->off; + len =3D min_size(src_len, dst_len); + + res =3D fuse_buf_copy_one(dst, dstv->off, src, srcv->off, len, flags); + if (res < 0) { + if (!copied) + return res; + break; + } + copied +=3D res; + + if (!fuse_bufvec_advance(srcv, res) || + !fuse_bufvec_advance(dstv, res)) + break; + + if (res < len) + break; + } + + return copied; +} diff --git a/tools/virtiofsd/fuse_log.c b/tools/virtiofsd/fuse_log.c new file mode 100644 index 0000000000..0d268ab014 --- /dev/null +++ b/tools/virtiofsd/fuse_log.c @@ -0,0 +1,40 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2019 Red Hat, Inc. + + Logging API. + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "fuse_log.h" + +#include +#include + +static void default_log_func( + __attribute__(( unused )) enum fuse_log_level level, + const char *fmt, va_list ap) +{ + vfprintf(stderr, fmt, ap); +} + +static fuse_log_func_t log_func =3D default_log_func; + +void fuse_set_log_func(fuse_log_func_t func) +{ + if (!func) + func =3D default_log_func; + + log_func =3D func; +} + +void fuse_log(enum fuse_log_level level, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + log_func(level, fmt, ap); + va_end(ap); +} diff --git a/tools/virtiofsd/fuse_opt.c b/tools/virtiofsd/fuse_opt.c new file mode 100644 index 0000000000..93066b926e --- /dev/null +++ b/tools/virtiofsd/fuse_opt.c @@ -0,0 +1,423 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + Implementation of option parsing routines (dealing with `struct + fuse_args`). + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "config.h" +#include "fuse_i.h" +#include "fuse_opt.h" +#include "fuse_misc.h" + +#include +#include +#include +#include + +struct fuse_opt_context { + void *data; + const struct fuse_opt *opt; + fuse_opt_proc_t proc; + int argctr; + int argc; + char **argv; + struct fuse_args outargs; + char *opts; + int nonopt; +}; + +void fuse_opt_free_args(struct fuse_args *args) +{ + if (args) { + if (args->argv && args->allocated) { + int i; + for (i =3D 0; i < args->argc; i++) + free(args->argv[i]); + free(args->argv); + } + args->argc =3D 0; + args->argv =3D NULL; + args->allocated =3D 0; + } +} + +static int alloc_failed(void) +{ + fuse_log(FUSE_LOG_ERR, "fuse: memory allocation failed\n"); + return -1; +} + +int fuse_opt_add_arg(struct fuse_args *args, const char *arg) +{ + char **newargv; + char *newarg; + + assert(!args->argv || args->allocated); + + newarg =3D strdup(arg); + if (!newarg) + return alloc_failed(); + + newargv =3D realloc(args->argv, (args->argc + 2) * sizeof(char *)); + if (!newargv) { + free(newarg); + return alloc_failed(); + } + + args->argv =3D newargv; + args->allocated =3D 1; + args->argv[args->argc++] =3D newarg; + args->argv[args->argc] =3D NULL; + return 0; +} + +static int fuse_opt_insert_arg_common(struct fuse_args *args, int pos, + const char *arg) +{ + assert(pos <=3D args->argc); + if (fuse_opt_add_arg(args, arg) =3D=3D -1) + return -1; + + if (pos !=3D args->argc - 1) { + char *newarg =3D args->argv[args->argc - 1]; + memmove(&args->argv[pos + 1], &args->argv[pos], + sizeof(char *) * (args->argc - pos - 1)); + args->argv[pos] =3D newarg; + } + return 0; +} + +int fuse_opt_insert_arg(struct fuse_args *args, int pos, const char *arg) +{ + return fuse_opt_insert_arg_common(args, pos, arg); +} + +static int next_arg(struct fuse_opt_context *ctx, const char *opt) +{ + if (ctx->argctr + 1 >=3D ctx->argc) { + fuse_log(FUSE_LOG_ERR, "fuse: missing argument after `%s'\n", opt); + return -1; + } + ctx->argctr++; + return 0; +} + +static int add_arg(struct fuse_opt_context *ctx, const char *arg) +{ + return fuse_opt_add_arg(&ctx->outargs, arg); +} + +static int add_opt_common(char **opts, const char *opt, int esc) +{ + unsigned oldlen =3D *opts ? strlen(*opts) : 0; + char *d =3D realloc(*opts, oldlen + 1 + strlen(opt) * 2 + 1); + + if (!d) + return alloc_failed(); + + *opts =3D d; + if (oldlen) { + d +=3D oldlen; + *d++ =3D ','; + } + + for (; *opt; opt++) { + if (esc && (*opt =3D=3D ',' || *opt =3D=3D '\\')) + *d++ =3D '\\'; + *d++ =3D *opt; + } + *d =3D '\0'; + + return 0; +} + +int fuse_opt_add_opt(char **opts, const char *opt) +{ + return add_opt_common(opts, opt, 0); +} + +int fuse_opt_add_opt_escaped(char **opts, const char *opt) +{ + return add_opt_common(opts, opt, 1); +} + +static int add_opt(struct fuse_opt_context *ctx, const char *opt) +{ + return add_opt_common(&ctx->opts, opt, 1); +} + +static int call_proc(struct fuse_opt_context *ctx, const char *arg, int ke= y, + int iso) +{ + if (key =3D=3D FUSE_OPT_KEY_DISCARD) + return 0; + + if (key !=3D FUSE_OPT_KEY_KEEP && ctx->proc) { + int res =3D ctx->proc(ctx->data, arg, key, &ctx->outargs); + if (res =3D=3D -1 || !res) + return res; + } + if (iso) + return add_opt(ctx, arg); + else + return add_arg(ctx, arg); +} + +static int match_template(const char *t, const char *arg, unsigned *sepp) +{ + int arglen =3D strlen(arg); + const char *sep =3D strchr(t, '=3D'); + sep =3D sep ? sep : strchr(t, ' '); + if (sep && (!sep[1] || sep[1] =3D=3D '%')) { + int tlen =3D sep - t; + if (sep[0] =3D=3D '=3D') + tlen ++; + if (arglen >=3D tlen && strncmp(arg, t, tlen) =3D=3D 0) { + *sepp =3D sep - t; + return 1; + } + } + if (strcmp(t, arg) =3D=3D 0) { + *sepp =3D 0; + return 1; + } + return 0; +} + +static const struct fuse_opt *find_opt(const struct fuse_opt *opt, + const char *arg, unsigned *sepp) +{ + for (; opt && opt->templ; opt++) + if (match_template(opt->templ, arg, sepp)) + return opt; + return NULL; +} + +int fuse_opt_match(const struct fuse_opt *opts, const char *opt) +{ + unsigned dummy; + return find_opt(opts, opt, &dummy) ? 1 : 0; +} + +static int process_opt_param(void *var, const char *format, const char *pa= ram, + const char *arg) +{ + assert(format[0] =3D=3D '%'); + if (format[1] =3D=3D 's') { + char **s =3D var; + char *copy =3D strdup(param); + if (!copy) + return alloc_failed(); + + free(*s); + *s =3D copy; + } else { + if (sscanf(param, format, var) !=3D 1) { + fuse_log(FUSE_LOG_ERR, "fuse: invalid parameter in option `%s'\n", arg); + return -1; + } + } + return 0; +} + +static int process_opt(struct fuse_opt_context *ctx, + const struct fuse_opt *opt, unsigned sep, + const char *arg, int iso) +{ + if (opt->offset =3D=3D -1U) { + if (call_proc(ctx, arg, opt->value, iso) =3D=3D -1) + return -1; + } else { + void *var =3D (char *)ctx->data + opt->offset; + if (sep && opt->templ[sep + 1]) { + const char *param =3D arg + sep; + if (opt->templ[sep] =3D=3D '=3D') + param ++; + if (process_opt_param(var, opt->templ + sep + 1, + param, arg) =3D=3D -1) + return -1; + } else + *(int *)var =3D opt->value; + } + return 0; +} + +static int process_opt_sep_arg(struct fuse_opt_context *ctx, + const struct fuse_opt *opt, unsigned sep, + const char *arg, int iso) +{ + int res; + char *newarg; + char *param; + + if (next_arg(ctx, arg) =3D=3D -1) + return -1; + + param =3D ctx->argv[ctx->argctr]; + newarg =3D malloc(sep + strlen(param) + 1); + if (!newarg) + return alloc_failed(); + + memcpy(newarg, arg, sep); + strcpy(newarg + sep, param); + res =3D process_opt(ctx, opt, sep, newarg, iso); + free(newarg); + + return res; +} + +static int process_gopt(struct fuse_opt_context *ctx, const char *arg, int= iso) +{ + unsigned sep; + const struct fuse_opt *opt =3D find_opt(ctx->opt, arg, &sep); + if (opt) { + for (; opt; opt =3D find_opt(opt + 1, arg, &sep)) { + int res; + if (sep && opt->templ[sep] =3D=3D ' ' && !arg[sep]) + res =3D process_opt_sep_arg(ctx, opt, sep, arg, + iso); + else + res =3D process_opt(ctx, opt, sep, arg, iso); + if (res =3D=3D -1) + return -1; + } + return 0; + } else + return call_proc(ctx, arg, FUSE_OPT_KEY_OPT, iso); +} + +static int process_real_option_group(struct fuse_opt_context *ctx, char *o= pts) +{ + char *s =3D opts; + char *d =3D s; + int end =3D 0; + + while (!end) { + if (*s =3D=3D '\0') + end =3D 1; + if (*s =3D=3D ',' || end) { + int res; + + *d =3D '\0'; + res =3D process_gopt(ctx, opts, 1); + if (res =3D=3D -1) + return -1; + d =3D opts; + } else { + if (s[0] =3D=3D '\\' && s[1] !=3D '\0') { + s++; + if (s[0] >=3D '0' && s[0] <=3D '3' && + s[1] >=3D '0' && s[1] <=3D '7' && + s[2] >=3D '0' && s[2] <=3D '7') { + *d++ =3D (s[0] - '0') * 0100 + + (s[1] - '0') * 0010 + + (s[2] - '0'); + s +=3D 2; + } else { + *d++ =3D *s; + } + } else { + *d++ =3D *s; + } + } + s++; + } + + return 0; +} + +static int process_option_group(struct fuse_opt_context *ctx, const char *= opts) +{ + int res; + char *copy =3D strdup(opts); + + if (!copy) { + fuse_log(FUSE_LOG_ERR, "fuse: memory allocation failed\n"); + return -1; + } + res =3D process_real_option_group(ctx, copy); + free(copy); + return res; +} + +static int process_one(struct fuse_opt_context *ctx, const char *arg) +{ + if (ctx->nonopt || arg[0] !=3D '-') + return call_proc(ctx, arg, FUSE_OPT_KEY_NONOPT, 0); + else if (arg[1] =3D=3D 'o') { + if (arg[2]) + return process_option_group(ctx, arg + 2); + else { + if (next_arg(ctx, arg) =3D=3D -1) + return -1; + + return process_option_group(ctx, + ctx->argv[ctx->argctr]); + } + } else if (arg[1] =3D=3D '-' && !arg[2]) { + if (add_arg(ctx, arg) =3D=3D -1) + return -1; + ctx->nonopt =3D ctx->outargs.argc; + return 0; + } else + return process_gopt(ctx, arg, 0); +} + +static int opt_parse(struct fuse_opt_context *ctx) +{ + if (ctx->argc) { + if (add_arg(ctx, ctx->argv[0]) =3D=3D -1) + return -1; + } + + for (ctx->argctr =3D 1; ctx->argctr < ctx->argc; ctx->argctr++) + if (process_one(ctx, ctx->argv[ctx->argctr]) =3D=3D -1) + return -1; + + if (ctx->opts) { + if (fuse_opt_insert_arg(&ctx->outargs, 1, "-o") =3D=3D -1 || + fuse_opt_insert_arg(&ctx->outargs, 2, ctx->opts) =3D=3D -1) + return -1; + } + + /* If option separator ("--") is the last argument, remove it */ + if (ctx->nonopt && ctx->nonopt =3D=3D ctx->outargs.argc && + strcmp(ctx->outargs.argv[ctx->outargs.argc - 1], "--") =3D=3D 0) { + free(ctx->outargs.argv[ctx->outargs.argc - 1]); + ctx->outargs.argv[--ctx->outargs.argc] =3D NULL; + } + + return 0; +} + +int fuse_opt_parse(struct fuse_args *args, void *data, + const struct fuse_opt opts[], fuse_opt_proc_t proc) +{ + int res; + struct fuse_opt_context ctx =3D { + .data =3D data, + .opt =3D opts, + .proc =3D proc, + }; + + if (!args || !args->argv || !args->argc) + return 0; + + ctx.argc =3D args->argc; + ctx.argv =3D args->argv; + + res =3D opt_parse(&ctx); + if (res !=3D -1) { + struct fuse_args tmp =3D *args; + *args =3D ctx.outargs; + ctx.outargs =3D tmp; + } + free(ctx.opts); + fuse_opt_free_args(&ctx.outargs); + return res; +} diff --git a/tools/virtiofsd/fuse_signals.c b/tools/virtiofsd/fuse_signals.c new file mode 100644 index 0000000000..4271947bd4 --- /dev/null +++ b/tools/virtiofsd/fuse_signals.c @@ -0,0 +1,91 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + Utility functions for setting signal handlers. + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB +*/ + +#include "config.h" +#include "fuse_lowlevel.h" +#include "fuse_i.h" + +#include +#include +#include +#include + +static struct fuse_session *fuse_instance; + +static void exit_handler(int sig) +{ + if (fuse_instance) { + fuse_session_exit(fuse_instance); + if(sig <=3D 0) { + fuse_log(FUSE_LOG_ERR, "assertion error: signal value <=3D 0\n"); + abort(); + } + fuse_instance->error =3D sig; + } +} + +static void do_nothing(int sig) +{ + (void) sig; +} + +static int set_one_signal_handler(int sig, void (*handler)(int), int remov= e) +{ + struct sigaction sa; + struct sigaction old_sa; + + memset(&sa, 0, sizeof(struct sigaction)); + sa.sa_handler =3D remove ? SIG_DFL : handler; + sigemptyset(&(sa.sa_mask)); + sa.sa_flags =3D 0; + + if (sigaction(sig, NULL, &old_sa) =3D=3D -1) { + perror("fuse: cannot get old signal handler"); + return -1; + } + + if (old_sa.sa_handler =3D=3D (remove ? handler : SIG_DFL) && + sigaction(sig, &sa, NULL) =3D=3D -1) { + perror("fuse: cannot set signal handler"); + return -1; + } + return 0; +} + +int fuse_set_signal_handlers(struct fuse_session *se) +{ + /* If we used SIG_IGN instead of the do_nothing function, + then we would be unable to tell if we set SIG_IGN (and + thus should reset to SIG_DFL in fuse_remove_signal_handlers) + or if it was already set to SIG_IGN (and should be left + untouched. */ + if (set_one_signal_handler(SIGHUP, exit_handler, 0) =3D=3D -1 || + set_one_signal_handler(SIGINT, exit_handler, 0) =3D=3D -1 || + set_one_signal_handler(SIGTERM, exit_handler, 0) =3D=3D -1 || + set_one_signal_handler(SIGPIPE, do_nothing, 0) =3D=3D -1) + return -1; + + fuse_instance =3D se; + return 0; +} + +void fuse_remove_signal_handlers(struct fuse_session *se) +{ + if (fuse_instance !=3D se) + fuse_log(FUSE_LOG_ERR, + "fuse: fuse_remove_signal_handlers: unknown session\n"); + else + fuse_instance =3D NULL; + + set_one_signal_handler(SIGHUP, exit_handler, 1); + set_one_signal_handler(SIGINT, exit_handler, 1); + set_one_signal_handler(SIGTERM, exit_handler, 1); + set_one_signal_handler(SIGPIPE, do_nothing, 1); +} diff --git a/tools/virtiofsd/helper.c b/tools/virtiofsd/helper.c new file mode 100644 index 0000000000..64ff7ad6d5 --- /dev/null +++ b/tools/virtiofsd/helper.c @@ -0,0 +1,440 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2007 Miklos Szeredi + + Helper functions to create (simple) standalone programs. With the + aid of these functions it should be possible to create full FUSE + file system by implementing nothing but the request handlers. + + This program can be distributed under the terms of the GNU LGPLv2. + See the file COPYING.LIB. +*/ + +#include "config.h" +#include "fuse_i.h" +#include "fuse_misc.h" +#include "fuse_opt.h" +#include "fuse_lowlevel.h" +#include "mount_util.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#define FUSE_HELPER_OPT(t, p) \ + { t, offsetof(struct fuse_cmdline_opts, p), 1 } + +static const struct fuse_opt fuse_helper_opts[] =3D { + FUSE_HELPER_OPT("-h", show_help), + FUSE_HELPER_OPT("--help", show_help), + FUSE_HELPER_OPT("-V", show_version), + FUSE_HELPER_OPT("--version", show_version), + FUSE_HELPER_OPT("-d", debug), + FUSE_HELPER_OPT("debug", debug), + FUSE_HELPER_OPT("-d", foreground), + FUSE_HELPER_OPT("debug", foreground), + FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP), + FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP), + FUSE_HELPER_OPT("-f", foreground), + FUSE_HELPER_OPT("-s", singlethread), + FUSE_HELPER_OPT("fsname=3D", nodefault_subtype), + FUSE_OPT_KEY("fsname=3D", FUSE_OPT_KEY_KEEP), +#ifndef __FreeBSD__ + FUSE_HELPER_OPT("subtype=3D", nodefault_subtype), + FUSE_OPT_KEY("subtype=3D", FUSE_OPT_KEY_KEEP), +#endif + FUSE_HELPER_OPT("clone_fd", clone_fd), + FUSE_HELPER_OPT("max_idle_threads=3D%u", max_idle_threads), + FUSE_OPT_END +}; + +struct fuse_conn_info_opts { + int atomic_o_trunc; + int no_remote_posix_lock; + int no_remote_flock; + int splice_write; + int splice_move; + int splice_read; + int no_splice_write; + int no_splice_move; + int no_splice_read; + int auto_inval_data; + int no_auto_inval_data; + int no_readdirplus; + int no_readdirplus_auto; + int async_dio; + int no_async_dio; + int writeback_cache; + int no_writeback_cache; + int async_read; + int sync_read; + unsigned max_write; + unsigned max_readahead; + unsigned max_background; + unsigned congestion_threshold; + unsigned time_gran; + int set_max_write; + int set_max_readahead; + int set_max_background; + int set_congestion_threshold; + int set_time_gran; +}; + +#define CONN_OPTION(t, p, v) \ + { t, offsetof(struct fuse_conn_info_opts, p), v } +static const struct fuse_opt conn_info_opt_spec[] =3D { + CONN_OPTION("max_write=3D%u", max_write, 0), + CONN_OPTION("max_write=3D", set_max_write, 1), + CONN_OPTION("max_readahead=3D%u", max_readahead, 0), + CONN_OPTION("max_readahead=3D", set_max_readahead, 1), + CONN_OPTION("max_background=3D%u", max_background, 0), + CONN_OPTION("max_background=3D", set_max_background, 1), + CONN_OPTION("congestion_threshold=3D%u", congestion_threshold, 0), + CONN_OPTION("congestion_threshold=3D", set_congestion_threshold, 1), + CONN_OPTION("sync_read", sync_read, 1), + CONN_OPTION("async_read", async_read, 1), + CONN_OPTION("atomic_o_trunc", atomic_o_trunc, 1), + CONN_OPTION("no_remote_lock", no_remote_posix_lock, 1), + CONN_OPTION("no_remote_lock", no_remote_flock, 1), + CONN_OPTION("no_remote_flock", no_remote_flock, 1), + CONN_OPTION("no_remote_posix_lock", no_remote_posix_lock, 1), + CONN_OPTION("splice_write", splice_write, 1), + CONN_OPTION("no_splice_write", no_splice_write, 1), + CONN_OPTION("splice_move", splice_move, 1), + CONN_OPTION("no_splice_move", no_splice_move, 1), + CONN_OPTION("splice_read", splice_read, 1), + CONN_OPTION("no_splice_read", no_splice_read, 1), + CONN_OPTION("auto_inval_data", auto_inval_data, 1), + CONN_OPTION("no_auto_inval_data", no_auto_inval_data, 1), + CONN_OPTION("readdirplus=3Dno", no_readdirplus, 1), + CONN_OPTION("readdirplus=3Dyes", no_readdirplus, 0), + CONN_OPTION("readdirplus=3Dyes", no_readdirplus_auto, 1), + CONN_OPTION("readdirplus=3Dauto", no_readdirplus, 0), + CONN_OPTION("readdirplus=3Dauto", no_readdirplus_auto, 0), + CONN_OPTION("async_dio", async_dio, 1), + CONN_OPTION("no_async_dio", no_async_dio, 1), + CONN_OPTION("writeback_cache", writeback_cache, 1), + CONN_OPTION("no_writeback_cache", no_writeback_cache, 1), + CONN_OPTION("time_gran=3D%u", time_gran, 0), + CONN_OPTION("time_gran=3D", set_time_gran, 1), + FUSE_OPT_END +}; + + +void fuse_cmdline_help(void) +{ + printf(" -h --help print help\n" + " -V --version print version\n" + " -d -o debug enable debug output (implies -f)\n" + " -f foreground operation\n" + " -s disable multi-threaded operation\n" + " -o clone_fd use separate fuse device fd for each t= hread\n" + " (may improve performance)\n" + " -o max_idle_threads the maximum number of idle worker thre= ads\n" + " allowed (default: 10)\n"); +} + +static int fuse_helper_opt_proc(void *data, const char *arg, int key, + struct fuse_args *outargs) +{ + (void) outargs; + struct fuse_cmdline_opts *opts =3D data; + + switch (key) { + case FUSE_OPT_KEY_NONOPT: + if (!opts->mountpoint) { + if (fuse_mnt_parse_fuse_fd(arg) !=3D -1) { + return fuse_opt_add_opt(&opts->mountpoint, arg); + } + + char mountpoint[PATH_MAX] =3D ""; + if (realpath(arg, mountpoint) =3D=3D NULL) { + fuse_log(FUSE_LOG_ERR, + "fuse: bad mount point `%s': %s\n", + arg, strerror(errno)); + return -1; + } + return fuse_opt_add_opt(&opts->mountpoint, mountpoint); + } else { + fuse_log(FUSE_LOG_ERR, "fuse: invalid argument `%s'\n", arg); + return -1; + } + + default: + /* Pass through unknown options */ + return 1; + } +} + +/* Under FreeBSD, there is no subtype option so this + function actually sets the fsname */ +static int add_default_subtype(const char *progname, struct fuse_args *arg= s) +{ + int res; + char *subtype_opt; + + const char *basename =3D strrchr(progname, '/'); + if (basename =3D=3D NULL) + basename =3D progname; + else if (basename[1] !=3D '\0') + basename++; + + subtype_opt =3D (char *) malloc(strlen(basename) + 64); + if (subtype_opt =3D=3D NULL) { + fuse_log(FUSE_LOG_ERR, "fuse: memory allocation failed\n"); + return -1; + } +#ifdef __FreeBSD__ + sprintf(subtype_opt, "-ofsname=3D%s", basename); +#else + sprintf(subtype_opt, "-osubtype=3D%s", basename); +#endif + res =3D fuse_opt_add_arg(args, subtype_opt); + free(subtype_opt); + return res; +} + +int fuse_parse_cmdline(struct fuse_args *args, + struct fuse_cmdline_opts *opts) +{ + memset(opts, 0, sizeof(struct fuse_cmdline_opts)); + + opts->max_idle_threads =3D 10; + + if (fuse_opt_parse(args, opts, fuse_helper_opts, + fuse_helper_opt_proc) =3D=3D -1) + return -1; + + /* *Linux*: if neither -o subtype nor -o fsname are specified, + set subtype to program's basename. + *FreeBSD*: if fsname is not specified, set to program's + basename. */ + if (!opts->nodefault_subtype) + if (add_default_subtype(args->argv[0], args) =3D=3D -1) + return -1; + + return 0; +} + + +int fuse_daemonize(int foreground) +{ + if (!foreground) { + int nullfd; + int waiter[2]; + char completed; + + if (pipe(waiter)) { + perror("fuse_daemonize: pipe"); + return -1; + } + + /* + * demonize current process by forking it and killing the + * parent. This makes current process as a child of 'init'. + */ + switch(fork()) { + case -1: + perror("fuse_daemonize: fork"); + return -1; + case 0: + break; + default: + (void) read(waiter[0], &completed, sizeof(completed)); + _exit(0); + } + + if (setsid() =3D=3D -1) { + perror("fuse_daemonize: setsid"); + return -1; + } + + (void) chdir("/"); + + nullfd =3D open("/dev/null", O_RDWR, 0); + if (nullfd !=3D -1) { + (void) dup2(nullfd, 0); + (void) dup2(nullfd, 1); + (void) dup2(nullfd, 2); + if (nullfd > 2) + close(nullfd); + } + + /* Propagate completion of daemon initialization */ + completed =3D 1; + (void) write(waiter[1], &completed, sizeof(completed)); + close(waiter[0]); + close(waiter[1]); + } else { + (void) chdir("/"); + } + return 0; +} + +int fuse_main_real(int argc, char *argv[], const struct fuse_operations *o= p, + size_t op_size, void *user_data) +{ + struct fuse_args args =3D FUSE_ARGS_INIT(argc, argv); + struct fuse *fuse; + struct fuse_cmdline_opts opts; + int res; + + if (fuse_parse_cmdline(&args, &opts) !=3D 0) + return 1; + + if (opts.show_version) { + printf("FUSE library version %s\n", PACKAGE_VERSION); + fuse_lowlevel_version(); + res =3D 0; + goto out1; + } + + if (opts.show_help) { + if(args.argv[0][0] !=3D '\0') + printf("usage: %s [options] \n\n", + args.argv[0]); + printf("FUSE options:\n"); + fuse_cmdline_help(); + fuse_lib_help(&args); + res =3D 0; + goto out1; + } + + if (!opts.show_help && + !opts.mountpoint) { + fuse_log(FUSE_LOG_ERR, "error: no mountpoint specified\n"); + res =3D 2; + goto out1; + } + + + fuse =3D fuse_new_31(&args, op, op_size, user_data); + if (fuse =3D=3D NULL) { + res =3D 3; + goto out1; + } + + if (fuse_mount(fuse,opts.mountpoint) !=3D 0) { + res =3D 4; + goto out2; + } + + if (fuse_daemonize(opts.foreground) !=3D 0) { + res =3D 5; + goto out3; + } + + struct fuse_session *se =3D fuse_get_session(fuse); + if (fuse_set_signal_handlers(se) !=3D 0) { + res =3D 6; + goto out3; + } + + if (opts.singlethread) + res =3D fuse_loop(fuse); + else { + struct fuse_loop_config loop_config; + loop_config.clone_fd =3D opts.clone_fd; + loop_config.max_idle_threads =3D opts.max_idle_threads; + res =3D fuse_loop_mt_32(fuse, &loop_config); + } + if (res) + res =3D 7; + + fuse_remove_signal_handlers(se); +out3: + fuse_unmount(fuse); +out2: + fuse_destroy(fuse); +out1: + free(opts.mountpoint); + fuse_opt_free_args(&args); + return res; +} + + +void fuse_apply_conn_info_opts(struct fuse_conn_info_opts *opts, + struct fuse_conn_info *conn) +{ + if(opts->set_max_write) + conn->max_write =3D opts->max_write; + if(opts->set_max_background) + conn->max_background =3D opts->max_background; + if(opts->set_congestion_threshold) + conn->congestion_threshold =3D opts->congestion_threshold; + if(opts->set_time_gran) + conn->time_gran =3D opts->time_gran; + if(opts->set_max_readahead) + conn->max_readahead =3D opts->max_readahead; + +#define LL_ENABLE(cond,cap) \ + if (cond) conn->want |=3D (cap) +#define LL_DISABLE(cond,cap) \ + if (cond) conn->want &=3D ~(cap) + + LL_ENABLE(opts->splice_read, FUSE_CAP_SPLICE_READ); + LL_DISABLE(opts->no_splice_read, FUSE_CAP_SPLICE_READ); + + LL_ENABLE(opts->splice_write, FUSE_CAP_SPLICE_WRITE); + LL_DISABLE(opts->no_splice_write, FUSE_CAP_SPLICE_WRITE); + + LL_ENABLE(opts->splice_move, FUSE_CAP_SPLICE_MOVE); + LL_DISABLE(opts->no_splice_move, FUSE_CAP_SPLICE_MOVE); + + LL_ENABLE(opts->auto_inval_data, FUSE_CAP_AUTO_INVAL_DATA); + LL_DISABLE(opts->no_auto_inval_data, FUSE_CAP_AUTO_INVAL_DATA); + + LL_DISABLE(opts->no_readdirplus, FUSE_CAP_READDIRPLUS); + LL_DISABLE(opts->no_readdirplus_auto, FUSE_CAP_READDIRPLUS_AUTO); + + LL_ENABLE(opts->async_dio, FUSE_CAP_ASYNC_DIO); + LL_DISABLE(opts->no_async_dio, FUSE_CAP_ASYNC_DIO); + + LL_ENABLE(opts->writeback_cache, FUSE_CAP_WRITEBACK_CACHE); + LL_DISABLE(opts->no_writeback_cache, FUSE_CAP_WRITEBACK_CACHE); + + LL_ENABLE(opts->async_read, FUSE_CAP_ASYNC_READ); + LL_DISABLE(opts->sync_read, FUSE_CAP_ASYNC_READ); + + LL_DISABLE(opts->no_remote_posix_lock, FUSE_CAP_POSIX_LOCKS); + LL_DISABLE(opts->no_remote_flock, FUSE_CAP_FLOCK_LOCKS); +} + +struct fuse_conn_info_opts* fuse_parse_conn_info_opts(struct fuse_args *ar= gs) +{ + struct fuse_conn_info_opts *opts; + + opts =3D calloc(1, sizeof(struct fuse_conn_info_opts)); + if(opts =3D=3D NULL) { + fuse_log(FUSE_LOG_ERR, "calloc failed\n"); + return NULL; + } + if(fuse_opt_parse(args, opts, conn_info_opt_spec, NULL) =3D=3D -1) { + free(opts); + return NULL; + } + return opts; +} + +int fuse_open_channel(const char *mountpoint, const char* options) +{ + struct mount_opts *opts =3D NULL; + int fd =3D -1; + const char *argv[] =3D { "", "-o", options }; + int argc =3D sizeof(argv) / sizeof(argv[0]); + struct fuse_args args =3D FUSE_ARGS_INIT(argc, (char**) argv); + + opts =3D parse_mount_opts(&args); + if (opts =3D=3D NULL) + return -1; + + fd =3D fuse_kern_mount(mountpoint, opts); + destroy_mount_opts(opts); + + return fd; +} --=20 2.24.1