From nobody Wed Dec 17 05:41:37 2025 Delivered-To: importer@patchew.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=1607709319; cv=none; d=zohomail.com; s=zohoarc; b=gBxsNn3uzwtwhyA+Q771fRAWJKEGLbwencNFqd8M0uPMENQIGivKAFm0ReU+8U+ZXlzE499SSNFDx+KAiWHXq8UikMyQFvIEry3vyLsrWEJ8fh/iSujPZYiON/HqOh3aOr7t9tXwCLHrW871xgmjCsIeehlikpHVssj0DKjM55M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1607709319; h=Content-Type:Content-Transfer-Encoding:Cc: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=0NAnf8ONMI16zDQq9Ngg351OMk8hdJE4xuxXJA0C5X0=; b=EgxzcF9vXjJlYqgN6p11C3FAvUHTDOSQNsA4fIaDmjdIS3BhG7+RJHoHvAAs4JKtxgtxGXcBL4TpLGVdRs3UZiKdOllsomk3+5jET6FZXdMOm0COx5nf8SnEYkkH1ESPpC8txVwLpbhfLHgQ+lzTWm3Z2kPG0svRD611b8WycwQ= 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 1607709319839335.43913226316306; Fri, 11 Dec 2020 09:55:19 -0800 (PST) Received: from localhost ([::1]:60560 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1knm7x-0001ze-Oc for importer@patchew.org; Fri, 11 Dec 2020 12:22:45 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:49908) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1knluO-0003eH-5B for qemu-devel@nongnu.org; Fri, 11 Dec 2020 12:08:44 -0500 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:50221) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.90_1) (envelope-from ) id 1knluD-00026J-7K for qemu-devel@nongnu.org; Fri, 11 Dec 2020 12:08:43 -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-301-Dl6XdjUSNOaDySuzR3DTvQ-1; Fri, 11 Dec 2020 12:08:29 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1954459; Fri, 11 Dec 2020 17:08:28 +0000 (UTC) Received: from merkur.redhat.com (ovpn-114-231.ams2.redhat.com [10.36.114.231]) by smtp.corp.redhat.com (Postfix) with ESMTP id 174425D6A8; Fri, 11 Dec 2020 17:08:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1607706511; 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=0NAnf8ONMI16zDQq9Ngg351OMk8hdJE4xuxXJA0C5X0=; b=TcRE92tgItNxm355TSZnTLq7XpIIRQnX+05XNUzVudQJ48BdcPN9435zYCpwfQ8zb7hK5T Lar0FnFV1lyysSS5erOo7QoPnRrPFw7rXfJNKumwP3QgDycGk+9FMOx3yXS4RIovuTlSCD +8sBGHN7s1SwCvKCINMTfzkrVwS7Y8I= X-MC-Unique: Dl6XdjUSNOaDySuzR3DTvQ-1 From: Kevin Wolf To: qemu-block@nongnu.org Subject: [PULL 07/34] fuse: Implement standard FUSE operations Date: Fri, 11 Dec 2020 18:07:45 +0100 Message-Id: <20201211170812.228643-8-kwolf@redhat.com> In-Reply-To: <20201211170812.228643-1-kwolf@redhat.com> References: <20201211170812.228643-1-kwolf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=kwolf@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable 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; Received-SPF: pass client-ip=216.205.24.124; envelope-from=kwolf@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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: kwolf@redhat.com, peter.maydell@linaro.org, qemu-devel@nongnu.org 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: Max Reitz This makes the export actually useful instead of only producing errors whenever it is accessed. Signed-off-by: Max Reitz Message-Id: <20201027190600.192171-4-mreitz@redhat.com> Signed-off-by: Kevin Wolf --- block/export/fuse.c | 242 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 242 insertions(+) diff --git a/block/export/fuse.c b/block/export/fuse.c index 0553bcd630..d995829ab7 100644 --- a/block/export/fuse.c +++ b/block/export/fuse.c @@ -282,8 +282,250 @@ static void fuse_init(void *userdata, struct fuse_con= n_info *conn) conn->max_write =3D MIN_NON_ZERO(BDRV_REQUEST_MAX_BYTES, conn->max_wri= te); } =20 +/** + * Let clients look up files. Always return ENOENT because we only + * care about the mountpoint itself. + */ +static void fuse_lookup(fuse_req_t req, fuse_ino_t parent, const char *nam= e) +{ + fuse_reply_err(req, ENOENT); +} + +/** + * Let clients get file attributes (i.e., stat() the file). + */ +static void fuse_getattr(fuse_req_t req, fuse_ino_t inode, + struct fuse_file_info *fi) +{ + struct stat statbuf; + int64_t length, allocated_blocks; + time_t now =3D time(NULL); + FuseExport *exp =3D fuse_req_userdata(req); + mode_t mode; + + length =3D blk_getlength(exp->common.blk); + if (length < 0) { + fuse_reply_err(req, -length); + return; + } + + allocated_blocks =3D bdrv_get_allocated_file_size(blk_bs(exp->common.b= lk)); + if (allocated_blocks <=3D 0) { + allocated_blocks =3D DIV_ROUND_UP(length, 512); + } else { + allocated_blocks =3D DIV_ROUND_UP(allocated_blocks, 512); + } + + mode =3D S_IFREG | S_IRUSR; + if (exp->writable) { + mode |=3D S_IWUSR; + } + + statbuf =3D (struct stat) { + .st_ino =3D inode, + .st_mode =3D mode, + .st_nlink =3D 1, + .st_uid =3D getuid(), + .st_gid =3D getgid(), + .st_size =3D length, + .st_blksize =3D blk_bs(exp->common.blk)->bl.request_alignment, + .st_blocks =3D allocated_blocks, + .st_atime =3D now, + .st_mtime =3D now, + .st_ctime =3D now, + }; + + fuse_reply_attr(req, &statbuf, 1.); +} + +static int fuse_do_truncate(const FuseExport *exp, int64_t size, + bool req_zero_write, PreallocMode prealloc) +{ + uint64_t blk_perm, blk_shared_perm; + BdrvRequestFlags truncate_flags =3D 0; + int ret; + + if (req_zero_write) { + truncate_flags |=3D BDRV_REQ_ZERO_WRITE; + } + + blk_get_perm(exp->common.blk, &blk_perm, &blk_shared_perm); + + ret =3D blk_set_perm(exp->common.blk, blk_perm | BLK_PERM_RESIZE, + blk_shared_perm, NULL); + if (ret < 0) { + return ret; + } + + ret =3D blk_truncate(exp->common.blk, size, true, prealloc, + truncate_flags, NULL); + + /* Must succeed, because we are only giving up the RESIZE permission */ + blk_set_perm(exp->common.blk, blk_perm, blk_shared_perm, &error_abort); + + return ret; +} + +/** + * Let clients set file attributes. Only resizing is supported. + */ +static void fuse_setattr(fuse_req_t req, fuse_ino_t inode, struct stat *st= atbuf, + int to_set, struct fuse_file_info *fi) +{ + FuseExport *exp =3D fuse_req_userdata(req); + int ret; + + if (!exp->writable) { + fuse_reply_err(req, EACCES); + return; + } + + if (to_set & ~FUSE_SET_ATTR_SIZE) { + fuse_reply_err(req, ENOTSUP); + return; + } + + ret =3D fuse_do_truncate(exp, statbuf->st_size, true, PREALLOC_MODE_OF= F); + if (ret < 0) { + fuse_reply_err(req, -ret); + return; + } + + fuse_getattr(req, inode, fi); +} + +/** + * Let clients open a file (i.e., the exported image). + */ +static void fuse_open(fuse_req_t req, fuse_ino_t inode, + struct fuse_file_info *fi) +{ + fuse_reply_open(req, fi); +} + +/** + * Handle client reads from the exported image. + */ +static void fuse_read(fuse_req_t req, fuse_ino_t inode, + size_t size, off_t offset, struct fuse_file_info *fi) +{ + FuseExport *exp =3D fuse_req_userdata(req); + int64_t length; + void *buf; + int ret; + + /* Limited by max_read, should not happen */ + if (size > FUSE_MAX_BOUNCE_BYTES) { + fuse_reply_err(req, EINVAL); + return; + } + + /** + * Clients will expect short reads at EOF, so we have to limit + * offset+size to the image length. + */ + length =3D blk_getlength(exp->common.blk); + if (length < 0) { + fuse_reply_err(req, -length); + return; + } + + if (offset + size > length) { + size =3D length - offset; + } + + buf =3D qemu_try_blockalign(blk_bs(exp->common.blk), size); + if (!buf) { + fuse_reply_err(req, ENOMEM); + return; + } + + ret =3D blk_pread(exp->common.blk, offset, buf, size); + if (ret >=3D 0) { + fuse_reply_buf(req, buf, size); + } else { + fuse_reply_err(req, -ret); + } + + qemu_vfree(buf); +} + +/** + * Handle client writes to the exported image. + */ +static void fuse_write(fuse_req_t req, fuse_ino_t inode, const char *buf, + size_t size, off_t offset, struct fuse_file_info *f= i) +{ + FuseExport *exp =3D fuse_req_userdata(req); + int64_t length; + int ret; + + /* Limited by max_write, should not happen */ + if (size > BDRV_REQUEST_MAX_BYTES) { + fuse_reply_err(req, EINVAL); + return; + } + + if (!exp->writable) { + fuse_reply_err(req, EACCES); + return; + } + + /** + * Clients will expect short writes at EOF, so we have to limit + * offset+size to the image length. + */ + length =3D blk_getlength(exp->common.blk); + if (length < 0) { + fuse_reply_err(req, -length); + return; + } + + if (offset + size > length) { + size =3D length - offset; + } + + ret =3D blk_pwrite(exp->common.blk, offset, buf, size, 0); + if (ret >=3D 0) { + fuse_reply_write(req, size); + } else { + fuse_reply_err(req, -ret); + } +} + +/** + * Let clients fsync the exported image. + */ +static void fuse_fsync(fuse_req_t req, fuse_ino_t inode, int datasync, + struct fuse_file_info *fi) +{ + FuseExport *exp =3D fuse_req_userdata(req); + int ret; + + ret =3D blk_flush(exp->common.blk); + fuse_reply_err(req, ret < 0 ? -ret : 0); +} + +/** + * Called before an FD to the exported image is closed. (libfuse + * notes this to be a way to return last-minute errors.) + */ +static void fuse_flush(fuse_req_t req, fuse_ino_t inode, + struct fuse_file_info *fi) +{ + fuse_fsync(req, inode, 1, fi); +} + static const struct fuse_lowlevel_ops fuse_ops =3D { .init =3D fuse_init, + .lookup =3D fuse_lookup, + .getattr =3D fuse_getattr, + .setattr =3D fuse_setattr, + .open =3D fuse_open, + .read =3D fuse_read, + .write =3D fuse_write, + .flush =3D fuse_flush, + .fsync =3D fuse_fsync, }; =20 const BlockExportDriver blk_exp_fuse =3D { --=20 2.29.2