From patchwork Tue Jan 21 12:23:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Dr. David Alan Gilbert" X-Patchwork-Id: 1226473 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=NnHlVIkG; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4827jv3s07z9sRG for ; Tue, 21 Jan 2020 23:52:51 +1100 (AEDT) Received: from localhost ([::1]:53364 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1itt1U-0007vw-O4 for incoming@patchwork.ozlabs.org; Tue, 21 Jan 2020 07:52:48 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:51658) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1itsdQ-00086j-W6 for qemu-devel@nongnu.org; Tue, 21 Jan 2020 07:27:58 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1itsdP-0002wa-5i for qemu-devel@nongnu.org; Tue, 21 Jan 2020 07:27:56 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:27771 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 1itsdP-0002w3-1l for qemu-devel@nongnu.org; Tue, 21 Jan 2020 07:27:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579609673; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZhtmLzERkcPdjLO5Z9jk3eI+ZPCKKWvfMDs8T+IoAwo=; b=NnHlVIkGeIS/E12lBeemVQAt4flDUn/T5ToSxAzJSy+KhoKxJAoj8DIEGEYLoiiZI0HM+d LZoWjAjbS2zqC18P7KUkf2D1XYeHvYwOk8J8OS9pPQMk5pscGjdgAWbu/UetaJ5foDcwRM nJfrDyJGCYO3TasTU4xH/VNkDXxgkdc= 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-333-PeEUNzvoMVK3YihswLc_cA-1; Tue, 21 Jan 2020 07:27:52 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id ECF82937C0; Tue, 21 Jan 2020 12:27:50 +0000 (UTC) Received: from dgilbert-t580.localhost (unknown [10.36.118.32]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8A3F460BE0; Tue, 21 Jan 2020 12:27:30 +0000 (UTC) From: "Dr. David Alan Gilbert (git)" To: qemu-devel@nongnu.org, stefanha@redhat.com, vgoyal@redhat.com, berrange@redhat.com, slp@redhat.com, philmd@redhat.com Subject: [PATCH v2 038/109] virtiofsd: passthrough_ll: add fallback for racy ops Date: Tue, 21 Jan 2020 12:23:22 +0000 Message-Id: <20200121122433.50803-39-dgilbert@redhat.com> In-Reply-To: <20200121122433.50803-1-dgilbert@redhat.com> References: <20200121122433.50803-1-dgilbert@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: PeEUNzvoMVK3YihswLc_cA-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] 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: , Cc: m.mizuma@jp.fujitsu.com, misono.tomohiro@jp.fujitsu.com Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" From: Miklos Szeredi We have two operations that cannot be done race-free on a symlink in certain cases: utimes and link. Add racy fallback for these if the race-free method doesn't work. We do our best to avoid races even in this case: - get absolute path by reading /proc/self/fd/NN symlink - lookup parent directory: after this we are safe against renames in ancestors - lookup name in parent directory, and verify that we got to the original inode, if not retry the whole thing Both utimes(2) and link(2) hold i_lock on the inode across the operation, so a racing rename/delete by this fuse instance is not possible, only from other entities changing the filesystem. If the "norace" option is given, then disable the racy fallbacks. Signed-off-by: Miklos Szeredi Reviewed-by: Masayoshi Mizuma --- tools/virtiofsd/helper.c | 5 +- tools/virtiofsd/passthrough_ll.c | 157 +++++++++++++++++++++++++++---- 2 files changed, 145 insertions(+), 17 deletions(-) diff --git a/tools/virtiofsd/helper.c b/tools/virtiofsd/helper.c index 8d66be46f8..d6fde8604b 100644 --- a/tools/virtiofsd/helper.c +++ b/tools/virtiofsd/helper.c @@ -143,7 +143,10 @@ void fuse_cmdline_help(void) " -s disable multi-threaded operation\n" " -o max_idle_threads the maximum number of idle worker " "threads\n" - " allowed (default: 10)\n"); + " allowed (default: 10)\n" + " -o norace disable racy fallback\n" + " default: false\n" + ); } static int fuse_helper_opt_proc(void *data, const char *arg, int key, diff --git a/tools/virtiofsd/passthrough_ll.c b/tools/virtiofsd/passthrough_ll.c index 327b5c1bf8..a207f7a97b 100644 --- a/tools/virtiofsd/passthrough_ll.c +++ b/tools/virtiofsd/passthrough_ll.c @@ -97,6 +97,7 @@ enum { struct lo_data { pthread_mutex_t mutex; int debug; + int norace; int writeback; int flock; int xattr; @@ -123,10 +124,15 @@ static const struct fuse_opt lo_opts[] = { { "cache=never", offsetof(struct lo_data, cache), CACHE_NEVER }, { "cache=auto", offsetof(struct lo_data, cache), CACHE_NORMAL }, { "cache=always", offsetof(struct lo_data, cache), CACHE_ALWAYS }, - + { "norace", offsetof(struct lo_data, norace), 1 }, FUSE_OPT_END }; +static void unref_inode(struct lo_data *lo, struct lo_inode *inode, uint64_t n); + +static struct lo_inode *lo_find(struct lo_data *lo, struct stat *st); + + static struct lo_data *lo_data(fuse_req_t req) { return (struct lo_data *)fuse_req_userdata(req); @@ -346,23 +352,127 @@ static void lo_getattr(fuse_req_t req, fuse_ino_t ino, fuse_reply_attr(req, &buf, lo->timeout); } -static int utimensat_empty_nofollow(struct lo_inode *inode, - const struct timespec *tv) +static int lo_parent_and_name(struct lo_data *lo, struct lo_inode *inode, + char path[PATH_MAX], struct lo_inode **parent) { - int res; char procname[64]; + char *last; + struct stat stat; + struct lo_inode *p; + int retries = 2; + int res; + +retry: + sprintf(procname, "/proc/self/fd/%i", inode->fd); + + res = readlink(procname, path, PATH_MAX); + if (res < 0) { + fuse_log(FUSE_LOG_WARNING, "%s: readlink failed: %m\n", __func__); + goto fail_noretry; + } + + if (res >= PATH_MAX) { + fuse_log(FUSE_LOG_WARNING, "%s: readlink overflowed\n", __func__); + goto fail_noretry; + } + path[res] = '\0'; + + last = strrchr(path, '/'); + if (last == NULL) { + /* Shouldn't happen */ + fuse_log( + FUSE_LOG_WARNING, + "%s: INTERNAL ERROR: bad path read from proc\n", __func__); + goto fail_noretry; + } + if (last == path) { + p = &lo->root; + pthread_mutex_lock(&lo->mutex); + p->refcount++; + pthread_mutex_unlock(&lo->mutex); + } else { + *last = '\0'; + res = fstatat(AT_FDCWD, last == path ? "/" : path, &stat, 0); + if (res == -1) { + if (!retries) { + fuse_log(FUSE_LOG_WARNING, + "%s: failed to stat parent: %m\n", __func__); + } + goto fail; + } + p = lo_find(lo, &stat); + if (p == NULL) { + if (!retries) { + fuse_log(FUSE_LOG_WARNING, + "%s: failed to find parent\n", __func__); + } + goto fail; + } + } + last++; + res = fstatat(p->fd, last, &stat, AT_SYMLINK_NOFOLLOW); + if (res == -1) { + if (!retries) { + fuse_log(FUSE_LOG_WARNING, + "%s: failed to stat last\n", __func__); + } + goto fail_unref; + } + if (stat.st_dev != inode->dev || stat.st_ino != inode->ino) { + if (!retries) { + fuse_log(FUSE_LOG_WARNING, + "%s: failed to match last\n", __func__); + } + goto fail_unref; + } + *parent = p; + memmove(path, last, strlen(last) + 1); + + return 0; + +fail_unref: + unref_inode(lo, p, 1); +fail: + if (retries) { + retries--; + goto retry; + } +fail_noretry: + errno = EIO; + return -1; +} + +static int utimensat_empty(struct lo_data *lo, struct lo_inode *inode, + const struct timespec *tv) +{ + int res; + struct lo_inode *parent; + char path[PATH_MAX]; if (inode->is_symlink) { - res = utimensat(inode->fd, "", tv, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW); + res = utimensat(inode->fd, "", tv, AT_EMPTY_PATH); if (res == -1 && errno == EINVAL) { /* Sorry, no race free way to set times on symlink. */ - errno = EPERM; + if (lo->norace) { + errno = EPERM; + } else { + goto fallback; + } } return res; } - sprintf(procname, "/proc/self/fd/%i", inode->fd); + sprintf(path, "/proc/self/fd/%i", inode->fd); - return utimensat(AT_FDCWD, procname, tv, 0); + return utimensat(AT_FDCWD, path, tv, 0); + +fallback: + res = lo_parent_and_name(lo, inode, path, &parent); + if (res != -1) { + res = utimensat(parent->fd, path, tv, AT_SYMLINK_NOFOLLOW); + unref_inode(lo, parent, 1); + } + + return res; } static int lo_fi_fd(fuse_req_t req, struct fuse_file_info *fi) @@ -386,6 +496,7 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr, { int saverr; char procname[64]; + struct lo_data *lo = lo_data(req); struct lo_inode *inode; int ifd; int res; @@ -458,7 +569,7 @@ static void lo_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr, if (fi) { res = futimens(fd, tv); } else { - res = utimensat_empty_nofollow(inode, tv); + res = utimensat_empty(lo, inode, tv); } if (res == -1) { goto out_err; @@ -708,24 +819,38 @@ static void lo_symlink(fuse_req_t req, const char *link, fuse_ino_t parent, lo_mknod_symlink(req, parent, name, S_IFLNK, 0, link); } -static int linkat_empty_nofollow(struct lo_inode *inode, int dfd, - const char *name) +static int linkat_empty_nofollow(struct lo_data *lo, struct lo_inode *inode, + int dfd, const char *name) { int res; - char procname[64]; + struct lo_inode *parent; + char path[PATH_MAX]; if (inode->is_symlink) { res = linkat(inode->fd, "", dfd, name, AT_EMPTY_PATH); if (res == -1 && (errno == ENOENT || errno == EINVAL)) { /* Sorry, no race free way to hard-link a symlink. */ - errno = EPERM; + if (lo->norace) { + errno = EPERM; + } else { + goto fallback; + } } return res; } - sprintf(procname, "/proc/self/fd/%i", inode->fd); + sprintf(path, "/proc/self/fd/%i", inode->fd); + + return linkat(AT_FDCWD, path, dfd, name, AT_SYMLINK_FOLLOW); + +fallback: + res = lo_parent_and_name(lo, inode, path, &parent); + if (res != -1) { + res = linkat(parent->fd, path, dfd, name, 0); + unref_inode(lo, parent, 1); + } - return linkat(AT_FDCWD, procname, dfd, name, AT_SYMLINK_FOLLOW); + return res; } static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent, @@ -747,7 +872,7 @@ static void lo_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t parent, e.attr_timeout = lo->timeout; e.entry_timeout = lo->timeout; - res = linkat_empty_nofollow(inode, lo_fd(req, parent), name); + res = linkat_empty_nofollow(lo, inode, lo_fd(req, parent), name); if (res == -1) { goto out_err; }