Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.128.53 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1793178770; b=lcTEFnoD5qRWUhwQpjR3kIYZ92T36ZFgLXf0h6/29u7A95ckizibNgJE99MnI6sGuHPY JfnM1mQiMmHSiGdN2c6xTgdFZddPRQrplNf+JSEGUDDNlshj/warFqrvHfqCiLIlZSNID UXheZV3h45sD5ST/OigfWr1KpXr8NFA7/nn8pucOHakMOeBbs9/NheZzW4u8o8fOhFHgD vuo/ez8aHqPxK8GI43mM0OGnPqKz+qzlaEMzU1ixJfpnyeq497nUGU0S0OA/ZFKZXC5Vb K+QHNuecTL6Rp5ZOi/TVV7/XWQBjTPAQdKIDxcs8u+F3J7CIsVNcTEqvezZC50ICd5A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1793178770; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Gm-Message-State:X-Gm-Gg:X-Received:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=/HBjYyx6sMBZgitLAOvfQm+eHp1gpfh7/trdmQ9YF5Y=; b=3iBjfpxfJ8e3QTFptzcOVx9aeSQOBY7jvLnFovt3idmFDKkOnu05cl5sa8+ZeXbwSV// FYnp/CY3WDbBOD6xwF97/d4muI3VD2pZ8rc6+lCQFJ84vp11lm98He+lCPGzzOgmaklr1 uqebAgdLIYmwU4B00B3iU9n1oxQWb4TmSAaEU3sduV/SHqeFyLSOzCCgyAitxdqMDogKI i1K8hmOGls5Az19ZRwQwQz0/mugoVw6T691Lgcy1EobhSH+mlPFepgvCa4l/IQRIYbMKH stw25SWSHSzOqKqNtodiOIOXQMtyD1tHqd/rNodM+m3pWXm0hzXru2GvQK8jbiby7Hg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=qNK1etUY; arc=none smtp.remote-ip=209.85.128.53 Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A95E3606A31A for ; Wed, 28 Oct 2026 09:12:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A95E3606A31A Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=qNK1etUY Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-4a9a8d6cea8so4036925e9.1 for ; Wed, 28 Oct 2026 02:12:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1793178768; x=1793783568; darn=lists.linux.dev; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/HBjYyx6sMBZgitLAOvfQm+eHp1gpfh7/trdmQ9YF5Y=; b=qNK1etUYxF8bItpvHhDNmOIhQCgBMkgReQ/oS2tqBhhrEEPZxXwD/Rh4OcopISiQTb JoJLkRaYsezGeFNUu/OckdW+yI5kPLALVdQEqGq8VxQMFuoifW/v1+vBiFyxger8YEtX ir5MhD2plCKUc05+G1mVLZBn0eVm3KZGfdFxOcJDWARquGOceXU4YYiIjpFwCmVTCXBT /yvKaSK9x+yuOcCPmwFEo+PYlLe/x0qdSAX8icGReZ5Ja9q8pg6XxVGIIbHL8ueOIxtu 2VkyQ9XxThBvq1S3mgedTWADgrpOWGRlVo/SQBxvKU9U3nZBqJr5TnsbflEbK5MZUD3F /Hqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1793178768; x=1793783568; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=/HBjYyx6sMBZgitLAOvfQm+eHp1gpfh7/trdmQ9YF5Y=; b=JGFKoitNJAJVx4/uWQOehvk1FvwghpnK3r7kt47m2MaITJmoE28cUwGwunlvAYve/g YvbFpf6cW8/fXXgwTtSiGomoWxwLSDS+BQ1UvmCoEly5VXi33YCUUOwWJoCXIQtYwBXN ph9lc0TeB74dwrcB0LXlJpvIdS4eA/4ekZZNeJqHaHneliEkJWoLdnmhA69jw28WRx7X UPAKOQ0g0DmvNOcZE6sfcjeg1s6hfS9Gh3vctblbR39/3EiYN0dXoFzFbdR14glqO38t iRxKRM3F4U8ngKGNDa4YnswyX+IRbCMYxTst9UhwFwl9BKtofTtIcZlXTjBGzLZKm0H8 Vulg== X-Gm-Message-State: AFq9FYIn4rWPEz6NrUI9zwAD+QDLffFg+ZGDk/Py3EBjHL5CllLE9Ta9 aks1LhrEqyO+JDeJiuW9hy9ZzY4ARwRpR+b5DRTnck3XiPO94gKplKHkXXTaiCU9 X-Gm-Gg: AYBFou33zAHnLb4aqXWe11GMmNzY8gC5zZmMf1d9ASh6RGtbCUsgAChUv4MiwKiqYw8 ZvRWnP65yU51C6dYFOxt8tyH7fyRuIxb2tYZL/VNqj62Cwc8wzL8ZPDAFPK1EDuS2WYFz2eQrDM Cg41A4Ksn3L7+kT4QSTy3r9xF/sEcLBHezjEsXLADKP0r1OHodBUsFYdj9jcFGjJjYN+SjzOMQg hnZQJzYCPPGMlwsZhBh3F8IzwOm5pmVJxY3D//v2yCQXw4+yDxavGtu7yvMPA5V7paSearK701+ s6su4flgE9pfcfxiR2OCUupkzVp+lBEABQm22bWc+1YmPndN1EkE1FZCc2MEP3917+ttSuZqszY +LzImi0k56yswaI2IPTEIleUyhM4Jg268JnxisE4FcbHc4PzBhpq+WT9muCXkfDl4l9HnHCksKh pF6ya4K1o3KCkLe3yxw3c4h61gMFHqh47yfETmjFFAjCGwh8ZJ2OTuoAvByZR7ry2WW7Jon7iv1 hLnXQTJXz+rwDyjVAo74oqiDXuyaBdXgouxYLfACXIQr50OhRxn/YFFWN+zh4LFyqoT9zKf7mrQ ToxkPOtEs1BfCPEeduYcyZA/Ayzqp6mroX6dRpJpcAvgG6zZ4qrwHE9gmajj4oc= X-Received: by 2002:a05:600c:860b:b0:49f:ffa2:1c72 with SMTP id 5b1f17b1804b1-4a997163b67mr25593125e9.11.1793178767692; Wed, 28 Oct 2026 02:12:47 -0700 (PDT) Received: from t480.fritz.box ([2001:a61:3a4e:c601:fc75:724e:6b78:3649]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4a97c42b3c1sm33507215e9.6.2026.10.28.02.12.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Oct 2026 02:12:47 -0700 (PDT) From: Han-Wen Nienhuys To: miklos@szeredi.hu Cc: fuse-devel@lists.linux.dev, Han-Wen Nienhuys Subject: [PATCH 3/7] fuse: fill the negative lookup cache from complete READDIRPLUS passes Date: Wed, 28 Oct 2026 10:12:11 +0100 Message-ID: <20261028091215.914885-4-hanwenn@gmail.com> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261028091215.914885-1-hanwenn@gmail.com> References: <20261028091215.914885-1-hanwenn@gmail.com> X-Mailing-List: fuse-devel@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add FOPEN_COMPLETE_DIR, with which the server marks a directory at OPENDIR, promising to announce remote additions with FUSE_NOTIFY_INVAL_ENTRY. When such a directory is read with READDIRPLUS from offset 0 to EOF, build its name filter from the returned names. This needs no extra transfer. Readdir holds the directory lock only per request, so local modifications in between are caught by the names generation, sampled at offset 0. The flag is per OPENDIR, as READDIRPLUS replies have no header to carry it. The filter expires with the entry timeout of ".", which is otherwise unused, so the server can choose it, also for an empty directory. If "." has no nodeid, the smallest timeout of the other entries with a nodeid is used, so that no negative answer outlives a positive one the server handed out. Without any timeout, no filter is built. Names are collected per struct fuse_file, as hashes in a temporary array, since their number is only known at EOF. The collection is abandoned on a seek, an error, or more than 2^18 names. While collecting, READDIRPLUS is forced, as READDIR carries no timeouts. A pass served from the readdir cache (FOPEN_CACHE_DIR) collects nothing, so an expired filter is not rebuilt until the cache is. Signed-off-by: Han-Wen Nienhuys Assisted-by: Claude:claude-opus-5-5 --- fs/fuse/dir.c | 1 + fs/fuse/fuse_i.h | 3 + fs/fuse/inode.c | 2 +- fs/fuse/readdir.c | 146 ++++++++++++++++++++++++++++++++++++-- include/uapi/linux/fuse.h | 16 ++++- 5 files changed, 162 insertions(+), 6 deletions(-) diff --git a/fs/fuse/dir.c b/fs/fuse/dir.c index 10efd6d32c0c..07a3aab51704 100644 --- a/fs/fuse/dir.c +++ b/fs/fuse/dir.c @@ -1951,6 +1951,7 @@ static int fuse_dir_open(struct inode *inode, struct file *file) static int fuse_dir_release(struct inode *inode, struct file *file) { + fuse_readdir_names_release(file->private_data); fuse_release_common(file, true); return 0; diff --git a/fs/fuse/fuse_i.h b/fs/fuse/fuse_i.h index 2a75c9597811..7306493f8a2a 100644 --- a/fs/fuse/fuse_i.h +++ b/fs/fuse/fuse_i.h @@ -343,6 +343,8 @@ struct fuse_file { /** @version: Version of cache we are reading */ u64 version; + /** @names: listing being collected for FOPEN_COMPLETE_DIR */ + struct fuse_dir_names_builder *names; } readdir; /** @polled_node: RB node to be linked on fuse_conn->polled_files */ @@ -1274,6 +1276,7 @@ int fuse_set_acl(struct mnt_idmap *, struct dentry *dentry, /* readdir.c */ int fuse_readdir(struct file *file, struct dir_context *ctx); +void fuse_readdir_names_release(struct fuse_file *ff); /* dir_names.c */ diff --git a/fs/fuse/inode.c b/fs/fuse/inode.c index c5229eed363d..5647089eaa5a 100644 --- a/fs/fuse/inode.c +++ b/fs/fuse/inode.c @@ -1508,7 +1508,7 @@ static struct fuse_init_args *fuse_new_init(struct fuse_mount *fm) FUSE_SECURITY_CTX | FUSE_CREATE_SUPP_GROUP | FUSE_HAS_EXPIRE_ONLY | FUSE_DIRECT_IO_ALLOW_MMAP | FUSE_NO_EXPORT_SUPPORT | FUSE_HAS_RESEND | FUSE_ALLOW_IDMAP | - FUSE_REQUEST_TIMEOUT; + FUSE_REQUEST_TIMEOUT | FUSE_HAS_COMPLETE_DIR; #ifdef CONFIG_FUSE_VDAX if (fm->fc->vdax) flags |= FUSE_MAP_ALIGNMENT; diff --git a/fs/fuse/readdir.c b/fs/fuse/readdir.c index f719545d5f59..77fcfd1c7343 100644 --- a/fs/fuse/readdir.c +++ b/fs/fuse/readdir.c @@ -13,6 +13,125 @@ #include #include +/* + * Names of a listing that is being read with FOPEN_COMPLETE_DIR. The number of + * names is only known at EOF, so the filter is built from these hashes then. + */ +struct fuse_dir_names_builder { + u64 *hash; + unsigned int count; + unsigned int size; + /* entry timeouts of ".", and the smallest of the others; or U64_MAX */ + u64 dot_expires; + u64 expires; + int epoch; + /* names generation when the listing was started */ + u64 gen; + /* ctx->pos expected by the next getdents call */ + loff_t pos; +}; + +void fuse_readdir_names_release(struct fuse_file *ff) +{ + struct fuse_dir_names_builder *nb = ff->readdir.names; + + if (!nb) + return; + kvfree(nb->hash); + kfree(nb); + ff->readdir.names = NULL; +} + +static void fuse_readdir_names_start(struct file *file) +{ + struct fuse_file *ff = file->private_data; + struct inode *inode = file_inode(file); + struct fuse_dir_names_builder *nb; + + fuse_readdir_names_release(ff); + + nb = kzalloc_obj(*nb, GFP_KERNEL_ACCOUNT); + if (!nb) + return; + nb->dot_expires = U64_MAX; + nb->expires = U64_MAX; + nb->epoch = atomic_read(&get_fuse_conn(inode)->epoch); + nb->gen = fuse_dir_names_gen(inode); + ff->readdir.names = nb; +} + +static void fuse_readdir_names_collect(struct file *file, + struct fuse_direntplus *direntplus) +{ + struct fuse_file *ff = file->private_data; + struct fuse_dir_names_builder *nb = ff->readdir.names; + struct fuse_conn *fc = get_fuse_conn(file_inode(file)); + struct fuse_dirent *dirent = &direntplus->dirent; + struct fuse_entry_out *o = &direntplus->entry_out; + + if (!nb) + return; + + if (o->nodeid) { + u64 expires = fuse_time_to_jiffies(o->entry_valid, + o->entry_valid_nsec); + + if (dirent->namelen == 1 && dirent->name[0] == '.') + nb->dot_expires = expires; + else + nb->expires = min(nb->expires, expires); + } + + if (name_is_dot_dotdot(dirent->name, dirent->namelen)) + return; + + if (nb->count == nb->size) { + unsigned int size = max(nb->size * 2, 64U); + u64 *hash; + + if (size > FUSE_DIR_NAMES_MAX) + goto abort; + hash = kvrealloc(nb->hash, size * sizeof(*hash), + GFP_KERNEL_ACCOUNT); + if (!hash) + goto abort; + nb->hash = hash; + nb->size = size; + } + nb->hash[nb->count++] = fuse_dir_name_hash(fc, dirent->name, + dirent->namelen); + return; + +abort: + fuse_readdir_names_release(ff); +} + +static void fuse_readdir_names_publish(struct file *file) +{ + struct fuse_file *ff = file->private_data; + struct fuse_dir_names_builder *nb = ff->readdir.names; + struct fuse_dir_names *names; + unsigned int i; + u64 expires; + + if (!nb) + return; + + /* "." is never looked up, so its timeout is for the listing */ + expires = nb->dot_expires != U64_MAX ? nb->dot_expires : nb->expires; + if (expires == U64_MAX) + goto out; + + names = fuse_dir_names_alloc(nb->count, expires, nb->epoch); + if (!names) + goto out; + for (i = 0; i < nb->count; i++) + fuse_dir_names_insert(names, nb->hash[i]); + fuse_dir_names_publish(file_inode(file), names, nb->gen); +out: + fuse_readdir_names_release(ff); +} + static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx) { struct fuse_conn *fc = get_fuse_conn(dir); @@ -365,8 +484,14 @@ static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file, which we did not link. */ over = !fuse_emit(file, ctx, dirent); - if (!over) + if (!over) { ctx->pos = dirent->off; + /* + * Only names the caller takes: the rest of the + * reply is sent again by the next request. + */ + fuse_readdir_names_collect(file, direntplus); + } } buf += reclen; @@ -426,9 +551,12 @@ static int fuse_readdir_uncached(struct file *file, struct dir_context *ctx) { int plus; ssize_t res; + struct fuse_file *ff = file->private_data; struct inode *inode = file_inode(file); struct fuse_mount *fm = get_fuse_mount(inode); struct fuse_conn *fc = fm->fc; + bool complete = fc->do_readdirplus && + (ff->open_flags & FOPEN_COMPLETE_DIR); struct fuse_io_args ia = {}; struct fuse_args_pages *ap = &ia.ap; void *buf; @@ -441,7 +569,14 @@ static int fuse_readdir_uncached(struct file *file, struct dir_context *ctx) if (!pages) return -ENOMEM; - plus = fuse_use_readdirplus(inode, ctx); + if (complete && !ctx->pos) + fuse_readdir_names_start(file); + else if (ff->readdir.names && ff->readdir.names->pos != ctx->pos) + fuse_readdir_names_release(ff); + + /* Collecting needs entry timeouts */ + plus = (complete && ff->readdir.names) || + fuse_use_readdirplus(inode, ctx); if (plus) { attr_version = fuse_get_attr_version(fm->fc); evict_ctr = fuse_get_evict_ctr(fm->fc); @@ -456,10 +591,9 @@ static int fuse_readdir_uncached(struct file *file, struct dir_context *ctx) goto out; if (!res) { - struct fuse_file *ff = file->private_data; - if (ff->open_flags & FOPEN_CACHE_DIR) fuse_readdir_cache_end(file, ctx->pos); + fuse_readdir_names_publish(file); goto out; } @@ -479,7 +613,11 @@ static int fuse_readdir_uncached(struct file *file, struct dir_context *ctx) /* The server took lookup references also for entries not linked */ if (plus && res < 0) fuse_direntplus_forget(inode, pages, parsed, nbytes); + if (!res && ff->readdir.names) + ff->readdir.names->pos = ctx->pos; out: + if (res < 0) + fuse_readdir_names_release(ff); kfree(ap->folios); release_pages(pages, ap->num_folios); kfree(pages); diff --git a/include/uapi/linux/fuse.h b/include/uapi/linux/fuse.h index 616430a2973c..32331c9b49b3 100644 --- a/include/uapi/linux/fuse.h +++ b/include/uapi/linux/fuse.h @@ -257,6 +257,9 @@ * - add backing_id_64 to fuse_open_out * - add FUSE_NOTIFY_BACKING_MAP, fuse_notify_backing_map_out, fuse_extent * - add FUSE_BACKING_MAP_CREATE, FUSE_BACKING_MAP_CYCLIC + * + * 7.48 + * - add FOPEN_COMPLETE_DIR and FUSE_HAS_COMPLETE_DIR */ #ifndef _LINUX_FUSE_H @@ -292,7 +295,7 @@ #define FUSE_KERNEL_VERSION 7 /** Minor version number of this interface */ -#define FUSE_KERNEL_MINOR_VERSION 47 +#define FUSE_KERNEL_MINOR_VERSION 48 /** The node ID of the root inode */ #define FUSE_ROOT_ID 1 @@ -406,6 +409,14 @@ struct fuse_file_lock { * (FUSE_URING_ZERO_COPY) and the request carries page * payload. Otherwise reads/writes fall back to * copying. + * FOPEN_COMPLETE_DIR: a complete READDIRPLUS listing of this directory is + * authoritative: names not in it are looked up as + * negative without sending FUSE_LOOKUP. This holds + * until the entry timeout of "." expires, or if "." has + * nodeid 0, the smallest entry timeout of the entries + * with a nonzero nodeid. "." does not count as a + * lookup. LOOKUP must only resolve names in the + * listing, byte for byte (eg. no case folding). */ #define FOPEN_DIRECT_IO (1 << 0) #define FOPEN_KEEP_CACHE (1 << 1) @@ -416,6 +427,7 @@ struct fuse_file_lock { #define FOPEN_PARALLEL_DIRECT_WRITES (1 << 6) #define FOPEN_PASSTHROUGH (1 << 7) #define FOPEN_IO_URING_ZERO_COPY (1 << 8) +#define FOPEN_COMPLETE_DIR (1 << 9) /* * INIT request/reply flags @@ -480,6 +492,7 @@ struct fuse_file_lock { * privilege that mounting virtiofs or fuseblk requires). * Insufficiently privileged servers ignore it. * FUSE_PASSTHROUGH_V2: use 64 bit server allocated backing ID + * FUSE_HAS_COMPLETE_DIR: kernel supports FOPEN_COMPLETE_DIR */ #define FUSE_ASYNC_READ (1 << 0) #define FUSE_POSIX_LOCKS (1 << 1) @@ -530,6 +543,7 @@ struct fuse_file_lock { #define FUSE_HAS_IO_URING_BUFPOOL (1ULL << 43) #define FUSE_HAS_SYNCFS (1ULL << 44) #define FUSE_PASSTHROUGH_V2 (1ULL << 45) +#define FUSE_HAS_COMPLETE_DIR (1ULL << 46) /* * CUSE INIT request/reply flags -- 2.56.0