From patchwork Wed Jun 28 15:25:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13295921 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id F4208EB64DA for ; Wed, 28 Jun 2023 15:25:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 691738D0003; Wed, 28 Jun 2023 11:25:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 641D18D0001; Wed, 28 Jun 2023 11:25:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5097F8D0003; Wed, 28 Jun 2023 11:25:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 42B8D8D0001 for ; Wed, 28 Jun 2023 11:25:14 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 0CEFCC0A9C for ; Wed, 28 Jun 2023 15:25:14 +0000 (UTC) X-FDA: 80952530148.12.37F7CBB Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf25.hostedemail.com (Postfix) with ESMTP id 183C6A0023 for ; Wed, 28 Jun 2023 15:25:11 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="p/3sJWhs"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687965912; h=from:from:sender: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:dkim-signature; bh=HvXjUFv2Mu75qhSRrn0KZaontdvtcYunu6qPvJZN9Kw=; b=hKWiGFT48/QBmIHWpn0kFtQExhYyEhJYKkAVUiP1uRopFXG8ITHuwxMG2Z6kouwMk/tDjZ E2xULN7n2nM/y0X8Mb6lod6J1zsb6cuZxwEO/lGmKS1DWXeOo9GnMGQ36g9j9D2OsdQKbg Pu/n16OU6yCi7XutzKyd+eW4U4Kh5us= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="p/3sJWhs"; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf25.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687965912; a=rsa-sha256; cv=none; b=PmS3AjdVn2Z4Qr/Aai1AaE/EqybOUv+7wdU0opfZoC3/fq+jctwGbT8rwVakh4NUqVN2K0 i13z/R1I9YBReTF/zQaYh9FXAw532u2t1fSYV055ZiImTS9rsDIn1Mwgexr9bD/sb2GzJt RkAXqFUY4IvI1bKXRb5EOnBiPwL3zL4= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2EA5861362; Wed, 28 Jun 2023 15:25:11 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 03D3CC433C0; Wed, 28 Jun 2023 15:25:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1687965910; bh=1/kaoCog/lqOfH5B87rGS+l38/fN9AN0iB2Xjv/TkMU=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=p/3sJWhs31NmFOoWPEaGLREXN3MCt5ITWUnr8s1nabcSizzYALGInWR6OvQNmyMdV NKV0K/+jgmaN5/Up9GORuZJ9c3nttObQKMTZIvoVOmPPRUjAYu5y/a+MQF5AFnRkGT 2WDBZs53PqqOmbOgtAVNc5kLa1aXxPflPOl2reCFh/uzbTrGx+xvZ3gB9qurpvEQQ4 eWEeQm/A1/LI5RqE2loTKizYrefb0SLj/IwYh30cfQFnS+EpDBXZ+dAM3acAyC1SJb vk7v+g4wAhyAin6LOTU75s8v+A5nXSvQgyM4CrNPbP2EhTQN+33p/elUi9vCur1wJr GDloO6iYbJh5g== Subject: [PATCH v6 1/3] libfs: Add directory operations for stable offsets From: Chuck Lever To: viro@zeniv.linux.org.uk, brauner@kernel.org, hughd@google.com, akpm@linux-foundation.org Cc: Chuck Lever , jlayton@redhat.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Date: Wed, 28 Jun 2023 11:25:09 -0400 Message-ID: <168796590904.157221.11286772826871541854.stgit@manet.1015granger.net> In-Reply-To: <168796579723.157221.1988816921257656153.stgit@manet.1015granger.net> References: <168796579723.157221.1988816921257656153.stgit@manet.1015granger.net> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspamd-Queue-Id: 183C6A0023 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: nfzic4m8ws44qpe5szrpetzqezfxstrp X-HE-Tag: 1687965911-442384 X-HE-Meta: U2FsdGVkX19JvwaukLZ5avWeRvdIxPZJsyhiJxBpiRgJTs16nxLGDbIfMQOMBRT/C8bCrNuAaQbPd6PATwbxYN91dcyC4rnYZW5OTLQP/JWHAdcVcWlnohyKrnKr0QFNfUWrc00m6pzRL9wSXk/N2hoOyn4I7FCVNjUIbHfschh8vT22MYrFTqwTMIDnNhdZCGqZ28U0hq1ea6rLP7+GcIOs7e+8+mtyy4OteKuX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0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Chuck Lever Create a vector of directory operations in fs/libfs.c that handles directory seeks and readdir via stable offsets instead of the current cursor-based mechanism. For the moment these are unused. Signed-off-by: Chuck Lever --- fs/libfs.c | 247 ++++++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 18 ++++ 2 files changed, 265 insertions(+) diff --git a/fs/libfs.c b/fs/libfs.c index 89cf614a3271..2b0d5ac472df 100644 --- a/fs/libfs.c +++ b/fs/libfs.c @@ -239,6 +239,253 @@ const struct inode_operations simple_dir_inode_operations = { }; EXPORT_SYMBOL(simple_dir_inode_operations); +static void offset_set(struct dentry *dentry, unsigned long offset) +{ + dentry->d_fsdata = (void *)offset; +} + +static unsigned long dentry2offset(struct dentry *dentry) +{ + return (unsigned long)dentry->d_fsdata; +} + +/** + * simple_offset_init - initialize an offset_ctx + * @octx: directory offset map to be initialized + * + */ +void simple_offset_init(struct offset_ctx *octx) +{ + xa_init_flags(&octx->xa, XA_FLAGS_ALLOC1); + + /* 0 is '.', 1 is '..', so always start with offset 2 */ + octx->next_offset = 2; +} + +/** + * simple_offset_add - Add an entry to a directory's offset map + * @octx: directory offset ctx to be updated + * @dentry: new dentry being added + * + * Returns zero on success. @so_ctx and the dentry offset are updated. + * Otherwise, a negative errno value is returned. + */ +int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry) +{ + static const struct xa_limit limit = XA_LIMIT(2, U32_MAX); + u32 offset; + int ret; + + if (dentry2offset(dentry) != 0) + return -EBUSY; + + ret = xa_alloc_cyclic(&octx->xa, &offset, dentry, limit, + &octx->next_offset, GFP_KERNEL); + if (ret < 0) + return ret; + + offset_set(dentry, offset); + return 0; +} + +/** + * simple_offset_remove - Remove an entry to a directory's offset map + * @octx: directory offset ctx to be updated + * @dentry: dentry being removed + * + */ +void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry) +{ + unsigned long index = dentry2offset(dentry); + + if (index == 0) + return; + + xa_erase(&octx->xa, index); + offset_set(dentry, 0); +} + +/** + * simple_offset_rename_exchange - exchange rename with directory offsets + * @old_dir: parent of dentry being moved + * @old_dentry: dentry being moved + * @new_dir: destination parent + * @new_dentry: destination dentry + * + * Returns zero on success. Otherwise a negative errno is returned and the + * rename is rolled back. + */ +int simple_offset_rename_exchange(struct inode *old_dir, + struct dentry *old_dentry, + struct inode *new_dir, + struct dentry *new_dentry) +{ + struct offset_ctx *old_ctx = old_dir->i_op->get_offset_ctx(old_dir); + struct offset_ctx *new_ctx = new_dir->i_op->get_offset_ctx(new_dir); + unsigned long old_index = dentry2offset(old_dentry); + unsigned long new_index = dentry2offset(new_dentry); + int ret; + + simple_offset_remove(old_ctx, old_dentry); + simple_offset_remove(new_ctx, new_dentry); + + ret = simple_offset_add(new_ctx, old_dentry); + if (ret) + goto out_restore; + + ret = simple_offset_add(old_ctx, new_dentry); + if (ret) { + simple_offset_remove(new_ctx, old_dentry); + goto out_restore; + } + + ret = simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry); + if (ret) { + simple_offset_remove(new_ctx, old_dentry); + simple_offset_remove(old_ctx, new_dentry); + goto out_restore; + } + return 0; + +out_restore: + offset_set(old_dentry, old_index); + xa_store(&old_ctx->xa, old_index, old_dentry, GFP_KERNEL); + offset_set(new_dentry, new_index); + xa_store(&new_ctx->xa, new_index, new_dentry, GFP_KERNEL); + return ret; +} + +/** + * simple_offset_destroy - Release offset map + * @octx: directory offset ctx that is about to be destroyed + * + * During fs teardown (eg. umount), a directory's offset map might still + * contain entries. xa_destroy() cleans out anything that remains. + */ +void simple_offset_destroy(struct offset_ctx *octx) +{ + xa_destroy(&octx->xa); +} + +/** + * offset_dir_llseek - Advance the read position of a directory descriptor + * @file: an open directory whose position is to be updated + * @offset: a byte offset + * @whence: enumerator describing the starting position for this update + * + * SEEK_END, SEEK_DATA, and SEEK_HOLE are not supported for directories. + * + * Returns the updated read position if successful; otherwise a + * negative errno is returned and the read position remains unchanged. + */ +static loff_t offset_dir_llseek(struct file *file, loff_t offset, int whence) +{ + switch (whence) { + case SEEK_CUR: + offset += file->f_pos; + fallthrough; + case SEEK_SET: + if (offset >= 0) + break; + fallthrough; + default: + return -EINVAL; + } + + return vfs_setpos(file, offset, U32_MAX); +} + +static struct dentry *offset_find_next(struct xa_state *xas) +{ + struct dentry *child, *found = NULL; + + rcu_read_lock(); + child = xas_next_entry(xas, U32_MAX); + if (!child) + goto out; + spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED); + if (simple_positive(child)) + found = dget_dlock(child); + spin_unlock(&child->d_lock); +out: + rcu_read_unlock(); + return found; +} + +static bool offset_dir_emit(struct dir_context *ctx, struct dentry *dentry) +{ + loff_t offset = dentry2offset(dentry); + struct inode *inode = d_inode(dentry); + + return ctx->actor(ctx, dentry->d_name.name, dentry->d_name.len, offset, + inode->i_ino, fs_umode_to_dtype(inode->i_mode)); +} + +static void offset_iterate_dir(struct dentry *dir, struct dir_context *ctx) +{ + struct inode *inode = d_inode(dir); + struct offset_ctx *so_ctx = inode->i_op->get_offset_ctx(inode); + XA_STATE(xas, &so_ctx->xa, ctx->pos); + struct dentry *dentry; + + while (true) { + spin_lock(&dir->d_lock); + dentry = offset_find_next(&xas); + spin_unlock(&dir->d_lock); + if (!dentry) + break; + + if (!offset_dir_emit(ctx, dentry)) { + dput(dentry); + break; + } + + dput(dentry); + ctx->pos = xas.xa_index + 1; + } +} + +/** + * offset_readdir - Emit entries starting at offset @ctx->pos + * @file: an open directory to iterate over + * @ctx: directory iteration context + * + * Caller must hold @file's i_rwsem to prevent insertion or removal of + * entries during this call. + * + * On entry, @ctx->pos contains an offset that represents the first entry + * to be read from the directory. + * + * The operation continues until there are no more entries to read, or + * until the ctx->actor indicates there is no more space in the caller's + * output buffer. + * + * On return, @ctx->pos contains an offset that will read the next entry + * in this directory when shmem_readdir() is called again with @ctx. + * + * Return values: + * %0 - Complete + */ +static int offset_readdir(struct file *file, struct dir_context *ctx) +{ + struct dentry *dir = file->f_path.dentry; + + lockdep_assert_held(&d_inode(dir)->i_rwsem); + + if (!dir_emit_dots(file, ctx)) + return 0; + + offset_iterate_dir(dir, ctx); + return 0; +} + +const struct file_operations simple_offset_dir_operations = { + .llseek = offset_dir_llseek, + .iterate_shared = offset_readdir, + .read = generic_read_dir, + .fsync = noop_fsync, +}; + static struct dentry *find_next_child(struct dentry *parent, struct dentry *prev) { struct dentry *child = NULL; diff --git a/include/linux/fs.h b/include/linux/fs.h index 133f0640fb24..85de389e4eb8 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1767,6 +1767,7 @@ struct dir_context { struct iov_iter; struct io_uring_cmd; +struct offset_ctx; struct file_operations { struct module *owner; @@ -1854,6 +1855,7 @@ struct inode_operations { int (*fileattr_set)(struct mnt_idmap *idmap, struct dentry *dentry, struct fileattr *fa); int (*fileattr_get)(struct dentry *dentry, struct fileattr *fa); + struct offset_ctx *(*get_offset_ctx)(struct inode *inode); } ____cacheline_aligned; static inline ssize_t call_read_iter(struct file *file, struct kiocb *kio, @@ -2954,6 +2956,22 @@ extern ssize_t simple_read_from_buffer(void __user *to, size_t count, extern ssize_t simple_write_to_buffer(void *to, size_t available, loff_t *ppos, const void __user *from, size_t count); +struct offset_ctx { + struct xarray xa; + u32 next_offset; +}; + +void simple_offset_init(struct offset_ctx *octx); +int simple_offset_add(struct offset_ctx *octx, struct dentry *dentry); +void simple_offset_remove(struct offset_ctx *octx, struct dentry *dentry); +int simple_offset_rename_exchange(struct inode *old_dir, + struct dentry *old_dentry, + struct inode *new_dir, + struct dentry *new_dentry); +void simple_offset_destroy(struct offset_ctx *octx); + +extern const struct file_operations simple_offset_dir_operations; + extern int __generic_file_fsync(struct file *, loff_t, loff_t, int); extern int generic_file_fsync(struct file *, loff_t, loff_t, int); From patchwork Wed Jun 28 15:25:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13295922 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id AB582EB64D7 for ; Wed, 28 Jun 2023 15:25:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4BCD78D0005; Wed, 28 Jun 2023 11:25:22 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 46D288D0001; Wed, 28 Jun 2023 11:25:22 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 35BC38D0005; Wed, 28 Jun 2023 11:25:22 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 295558D0001 for ; Wed, 28 Jun 2023 11:25:22 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id EA46C80B17 for ; Wed, 28 Jun 2023 15:25:21 +0000 (UTC) X-FDA: 80952530442.20.922D342 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf16.hostedemail.com (Postfix) with ESMTP id D43F3180022 for ; Wed, 28 Jun 2023 15:25:18 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=j0i6ikly; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf16.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687965918; h=from:from:sender: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:dkim-signature; bh=w7U1ETJF3+b23Qu0/lBzi5jnA7O+fNb1OvT4FhatSZs=; b=KL7ja1kFguXsfZfW0FAvLFbJ0oqWlPGIBkpWUkLCSLj4kpnZvjpxJ2f23zHkr8EcREdRQk o24y67hBwVr6eIGhMCDr74V2LcUeIBoRt+N6U00isTQOzQSkiSlH2DZhmrHkBsa6iS8C30 EHCFMe3g++kIbUJSNGQsDYZFLUcO4OA= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=j0i6ikly; dmarc=pass (policy=none) header.from=kernel.org; spf=pass (imf16.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687965918; a=rsa-sha256; cv=none; b=1Jb63Il6+MHfpmnyWhYzS1zlrOueiyfAH/SBn4G6esLbLNRQ2KotyEhcmKBNpK++E/9LXl zfGlFVT1t7SSdgit5vqPAhvuE226yZIIJvWY9/o8SJiVL6vJDqkHXS4Vr2w/qOMG4GMC8a YdeQ7CH3obicrF1IHgVybsnjw/LU5AU= Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E782161368; Wed, 28 Jun 2023 15:25:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A4E3AC433C9; Wed, 28 Jun 2023 15:25:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1687965917; bh=e4I/gNHDIjpSKufk2xxM8B41vPKw1FE7ZKuVtO31ayU=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=j0i6ikly/vKfjVuwx9wICD5KcaOQQ01VEGi3ol8P4+JUzBYb+bgUI0oGPnxlbHbrd iHzrPbCdSvNvOnfpVYD5iZkuYXU47+RI0/ZYrjyQvHnleHON3qExf5wjKf1HiOtQ+K ES+mAU1eJ8piaIb7lSu7XFLKB1p6kSgDB5THy8oE1MBCWD52xhXBHFArJHok4erBr4 Sl83t67t1GjeXvTP2DvXpet4h/9OMSiBM8M1EKm7klMeVYtTu0/O/9RF8/ArL8F1lU pR6yLbhjZ7/qtA42xeNFwUd14F9l5IIvTomiUdF/7NdFiaB7aANHaY1OPpMvxjoSUC 2bZehSg3sC1Kg== Subject: [PATCH v6 2/3] shmem: Refactor shmem_symlink() From: Chuck Lever To: viro@zeniv.linux.org.uk, brauner@kernel.org, hughd@google.com, akpm@linux-foundation.org Cc: Jeff Layton , Christoph Hellwig , Chuck Lever , jlayton@redhat.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Date: Wed, 28 Jun 2023 11:25:15 -0400 Message-ID: <168796591580.157221.7871489061567042327.stgit@manet.1015granger.net> In-Reply-To: <168796579723.157221.1988816921257656153.stgit@manet.1015granger.net> References: <168796579723.157221.1988816921257656153.stgit@manet.1015granger.net> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Rspamd-Queue-Id: D43F3180022 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: udxokaqd8pi4swd8d4nam3sa7k7ob5kb X-HE-Tag: 1687965918-376074 X-HE-Meta: U2FsdGVkX1+KH+SJSjJ1Lj2yH3i9/bwFewmttQLdsA1R96sVisCLvAtiLBXXi7ZQ2IgKzW1GXDAvcQMBRtIynTV0BuG4G7QCvq6t7MFdYC9hTdgtheM0/JyLdl0Pr9w7cb6TCIGYPElwAF3h4q4xJ0GouNJJq7k1esIDpV5V9frq9a9RH/HNMhVnAKNwoHHm0VMV1+iExC7rfJ1+UwiiGp/OPKESj3aVKOMxREMakFywRamieT9wNwnxAuFCa0ukxJ7FDhu8vi8OgTAZaMCWAAMmSEGo50dhAk+G6socvXWjQIRNFOYga4Zy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, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Chuck Lever De-duplicate the error handling paths. No change in behavior is expected. Suggested-by: Jeff Layton Reviewed-by: Christoph Hellwig Signed-off-by: Chuck Lever --- mm/shmem.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index e40a08c5c6d7..721f9fd064aa 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -3161,26 +3161,22 @@ static int shmem_symlink(struct mnt_idmap *idmap, struct inode *dir, error = security_inode_init_security(inode, dir, &dentry->d_name, shmem_initxattrs, NULL); - if (error && error != -EOPNOTSUPP) { - iput(inode); - return error; - } + if (error && error != -EOPNOTSUPP) + goto out_iput; inode->i_size = len-1; if (len <= SHORT_SYMLINK_LEN) { inode->i_link = kmemdup(symname, len, GFP_KERNEL); if (!inode->i_link) { - iput(inode); - return -ENOMEM; + error = -ENOMEM; + goto out_iput; } inode->i_op = &shmem_short_symlink_operations; } else { inode_nohighmem(inode); error = shmem_get_folio(inode, 0, &folio, SGP_WRITE); - if (error) { - iput(inode); - return error; - } + if (error) + goto out_iput; inode->i_mapping->a_ops = &shmem_aops; inode->i_op = &shmem_symlink_inode_operations; memcpy(folio_address(folio), symname, len); @@ -3195,6 +3191,9 @@ static int shmem_symlink(struct mnt_idmap *idmap, struct inode *dir, d_instantiate(dentry, inode); dget(dentry); return 0; +out_iput: + iput(inode); + return error; } static void shmem_put_link(void *arg) From patchwork Wed Jun 28 15:25:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chuck Lever X-Patchwork-Id: 13295923 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9BC53EB64D7 for ; Wed, 28 Jun 2023 15:25:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EA7D38D0006; Wed, 28 Jun 2023 11:25:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E57D28D0001; Wed, 28 Jun 2023 11:25:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D1FCA8D0006; Wed, 28 Jun 2023 11:25:27 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id C56BC8D0001 for ; Wed, 28 Jun 2023 11:25:27 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 806EF40B0E for ; Wed, 28 Jun 2023 15:25:27 +0000 (UTC) X-FDA: 80952530694.22.DB6CFAE Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf30.hostedemail.com (Postfix) with ESMTP id 947C18000F for ; Wed, 28 Jun 2023 15:25:25 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=eRsGE8so; spf=pass (imf30.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=none) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1687965925; h=from:from:sender: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:dkim-signature; bh=v23t7EDYgrbP6VdssuRTVott1ueed+wJjz4P8pmDN9s=; b=A8zZU7kDqImmaGEwrrs6/cuQdXOdC/o2WFOWIcn1F1F3i+n7ecl3PV6WE6Gt589IjV4NZW 24BPhYXox1d1Ibw+/ZjkwrBnv15ZT6GwBf85TdrsiaKHwLyS/ud721bqnFLM071m3hqMiK +mE89GCo7hC1vqfLlZOyoG5X1Av2DjU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1687965925; a=rsa-sha256; cv=none; b=MlYC54IG2FZwqM2YxQQMLi0KYGk/uGWentTuNXocLUfbnDeaPhP5Qs73HSYEW498VTda8i 8uO+ajG7OwHbZd54g9o2vTmbWJLr54gFtlXW9Y4sLxMEkmqg0EJZG4/uuH9DmuuxXtkMCt z5SF31BW7D6KxADHFCD/L2otfaHJ1+4= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=eRsGE8so; spf=pass (imf30.hostedemail.com: domain of cel@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=cel@kernel.org; dmarc=pass (policy=none) header.from=kernel.org Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id AA2E061362; Wed, 28 Jun 2023 15:25:24 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 83E6BC433C0; Wed, 28 Jun 2023 15:25:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1687965924; bh=ZMnvUqzeadwEtrY+PraNrm34AEWiOd4yOx+5Vnlx2ig=; h=Subject:From:To:Cc:Date:In-Reply-To:References:From; b=eRsGE8sow61Jtmo9m1fmSdyyOPmgSQMrcRjqKc0muqWxg1u7+6Fj8L+xgURNdQ/pO +Ix3A5CvAA6TfBuKqIs/JHBu60Xh4ptvkPZcTrUZsuC6HeKwPMtnQrzriek9O1ZTlA M1hoB1LZaxyCGqx1bnhp6/kBYfJiBSUpUeREoKlr8X52aj2ogK1XMj50Qe9jPQhQuX 5dJxdwZmp88VV5qBbLlT2ekDYX0QLDejwVj9sB0ljcwEzxAAkW7iVWGh5B4wF3Bub/ 4yoapVGZhQBh4Xo95xjUluRsXp4qRY/s/m4ohVp5PQTiwmSnwLVRaX8yOPV6iczvP/ aqGdhWuTBaNNw== Subject: [PATCH v6 3/3] shmem: stable directory offsets From: Chuck Lever To: viro@zeniv.linux.org.uk, brauner@kernel.org, hughd@google.com, akpm@linux-foundation.org Cc: Chuck Lever , jlayton@redhat.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org Date: Wed, 28 Jun 2023 11:25:22 -0400 Message-ID: <168796592256.157221.15420161253379368867.stgit@manet.1015granger.net> In-Reply-To: <168796579723.157221.1988816921257656153.stgit@manet.1015granger.net> References: <168796579723.157221.1988816921257656153.stgit@manet.1015granger.net> User-Agent: StGit/1.5 MIME-Version: 1.0 X-Stat-Signature: w69ewjigpbr857icjni6o9h5mowjuy4b X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 947C18000F X-Rspam-User: X-HE-Tag: 1687965925-329532 X-HE-Meta: U2FsdGVkX1+YIxBJmehcH02kldTTB8xqmppelMDGCbpMncngebtzUplHyycDZtFuUk5IvHfPJwYuOvfmMXkyMNrbIiA4muUDa08Fv029PUhBuScg9RG5n/G2QVXCEicA4L4esAC+FEO9ReoYzeuI7LhOZ5iOeTfGADh1o3C8rxYEAxMU/VjbcL4agJ4vlgEZlZdAbsx4L6GYJkN1apjLHDzrcZYFy9qfwV8cXgfdZr6d+MXaxmc6Bs9IURUtVdgaOgCEAQh7d7eJkNOqz7OPTBVIzZW2Jj+QGLssB59ev0jp8rMFhwWHHAPwPM6srkLTUesrDGD9b1XtvLESV/H1BnAJbPpTbnRfY1UYny0bvI0EbOejhN2sfr3EVX4U6IbgkAU4Um8liHVbSCdMZRvDskmgXXNzt+l2Phlh45Ki5E904Q+KFwyJR1CA5SBA3YDMHMdmJlkd5TzHiO4eUAw52z4TalQXBcuDu5+UGYt4Kv4tXXZBBPnuMqOlGN02dsWGsvElI6frUZo+1aC0fmqk7G/dm6vZrjI7Rb3ol0absguUOkRdqJnTh9QA45a0l7AaQjAgXPMgfcQRnLDNFSovKXuZExJ0mEQ/JpkqlI9hKpcT4EyaEB3p9AVz8DTaVY654KBOMdNW/xSxQTv2nV/6Q06ZcmzXqHI3I8TOLnorPDrlQb3KPebmVJZIggpR2mlelNZSyDOhOzfKQuV6Dvyfb43do88OQ6PjcPcK+QyrunmsVyyXA92zoIf1YrxRSpWjsHY4n07T9ZKf2/g8hMap4ziBiVxjvqUPtAw3k03iHULC/YBxxTX2CQqjHZ++jvKfntNlCIpWKhxfkWakEJuH+oWc69rT5acqsnmHYp6Q6omWhRjE0Z9k3lIdG/Q5eynZP7RYQCuR7FpnypnuYbCps8BCoReXOYSw9ePC3KhyGkvTfwM2h2gL76BZj/O6a+qlE5ObhhRc8Ghvx6cPMmR 5Svuqu6p itAo5u74RZqmyMA2YiNLXI3wdBXVxSPKUk2bXzBqwyxsNCwwBL3EsBglcKp3dEBSYm95vNu4ZkKsUcaZB+4b9eeiOXDfNtpfk4/2XDbXKid/UdMFwwOcClg8NLrgMV+1D7guVkYt8+M7dp+Ap5LvZ9ttTuX1BQLuFQN6znM6x7L8YJyp3S+7rjogEGZ/uS3J9e8T6y/dIhY5NNt+mN6I1EwGmZZIYFEdDxtt5k+vrI7IKgLskEhMYjcgtJc33uuQbFyTXubxA1T8mzVVG4u3TNbbNgQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Chuck Lever The current cursor-based directory offset mechanism doesn't work when a tmpfs filesystem is exported via NFS. This is because NFS clients do not open directories. Each server-side READDIR operation has to open the directory, read it, then close it. The cursor state for that directory, being associated strictly with the opened struct file, is thus discarded after each NFS READDIR operation. Directory offsets are cached not only by NFS clients, but also by user space libraries on those clients. Essentially there is no way to invalidate those caches when directory offsets have changed on an NFS server after the offset-to-dentry mapping changes. Thus the whole application stack depends on unchanging directory offsets. The solution we've come up with is to make the directory offset for each file in a tmpfs filesystem stable for the life of the directory entry it represents. shmem_readdir() and shmem_dir_llseek() now use an xarray to map each directory offset (an loff_t integer) to the memory address of a struct dentry. Signed-off-by: Chuck Lever --- include/linux/shmem_fs.h | 1 + mm/shmem.c | 47 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 7 deletions(-) diff --git a/include/linux/shmem_fs.h b/include/linux/shmem_fs.h index 9029abd29b1c..a5454a80ab30 100644 --- a/include/linux/shmem_fs.h +++ b/include/linux/shmem_fs.h @@ -27,6 +27,7 @@ struct shmem_inode_info { atomic_t stop_eviction; /* hold when working on inode */ struct timespec64 i_crtime; /* file creation time */ unsigned int fsflags; /* flags for FS_IOC_[SG]ETFLAGS */ + struct offset_ctx dir_offsets; /* stable entry offsets */ struct inode vfs_inode; }; diff --git a/mm/shmem.c b/mm/shmem.c index 721f9fd064aa..318a6e70b6b1 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -2355,6 +2355,11 @@ static void shmem_set_inode_flags(struct inode *inode, unsigned int fsflags) #define shmem_initxattrs NULL #endif +static struct offset_ctx *shmem_get_offset_ctx(struct inode *inode) +{ + return &SHMEM_I(inode)->dir_offsets; +} + static struct inode *shmem_get_inode(struct mnt_idmap *idmap, struct super_block *sb, struct inode *dir, umode_t mode, dev_t dev, unsigned long flags) @@ -2410,7 +2415,8 @@ static struct inode *shmem_get_inode(struct mnt_idmap *idmap, struct super_block /* Some things misbehave if size == 0 on a directory */ inode->i_size = 2 * BOGO_DIRENT_SIZE; inode->i_op = &shmem_dir_inode_operations; - inode->i_fop = &simple_dir_operations; + inode->i_fop = &simple_offset_dir_operations; + simple_offset_init(shmem_get_offset_ctx(inode)); break; case S_IFLNK: /* @@ -2950,7 +2956,10 @@ shmem_mknod(struct mnt_idmap *idmap, struct inode *dir, if (error && error != -EOPNOTSUPP) goto out_iput; - error = 0; + error = simple_offset_add(shmem_get_offset_ctx(dir), dentry); + if (error) + goto out_iput; + dir->i_size += BOGO_DIRENT_SIZE; dir->i_ctime = dir->i_mtime = current_time(dir); inode_inc_iversion(dir); @@ -3027,6 +3036,13 @@ static int shmem_link(struct dentry *old_dentry, struct inode *dir, struct dentr goto out; } + ret = simple_offset_add(shmem_get_offset_ctx(dir), dentry); + if (ret) { + if (inode->i_nlink) + shmem_free_inode(inode->i_sb); + goto out; + } + dir->i_size += BOGO_DIRENT_SIZE; inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); inode_inc_iversion(dir); @@ -3045,6 +3061,8 @@ static int shmem_unlink(struct inode *dir, struct dentry *dentry) if (inode->i_nlink > 1 && !S_ISDIR(inode->i_mode)) shmem_free_inode(inode->i_sb); + simple_offset_remove(shmem_get_offset_ctx(dir), dentry); + dir->i_size -= BOGO_DIRENT_SIZE; inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode); inode_inc_iversion(dir); @@ -3103,24 +3121,29 @@ static int shmem_rename2(struct mnt_idmap *idmap, { struct inode *inode = d_inode(old_dentry); int they_are_dirs = S_ISDIR(inode->i_mode); + int error; if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) return -EINVAL; if (flags & RENAME_EXCHANGE) - return simple_rename_exchange(old_dir, old_dentry, new_dir, new_dentry); + return simple_offset_rename_exchange(old_dir, old_dentry, + new_dir, new_dentry); if (!simple_empty(new_dentry)) return -ENOTEMPTY; if (flags & RENAME_WHITEOUT) { - int error; - error = shmem_whiteout(idmap, old_dir, old_dentry); if (error) return error; } + simple_offset_remove(shmem_get_offset_ctx(old_dir), old_dentry); + error = simple_offset_add(shmem_get_offset_ctx(new_dir), old_dentry); + if (error) + return error; + if (d_really_is_positive(new_dentry)) { (void) shmem_unlink(new_dir, new_dentry); if (they_are_dirs) { @@ -3164,19 +3187,23 @@ static int shmem_symlink(struct mnt_idmap *idmap, struct inode *dir, if (error && error != -EOPNOTSUPP) goto out_iput; + error = simple_offset_add(shmem_get_offset_ctx(dir), dentry); + if (error) + goto out_iput; + inode->i_size = len-1; if (len <= SHORT_SYMLINK_LEN) { inode->i_link = kmemdup(symname, len, GFP_KERNEL); if (!inode->i_link) { error = -ENOMEM; - goto out_iput; + goto out_remove_offset; } inode->i_op = &shmem_short_symlink_operations; } else { inode_nohighmem(inode); error = shmem_get_folio(inode, 0, &folio, SGP_WRITE); if (error) - goto out_iput; + goto out_remove_offset; inode->i_mapping->a_ops = &shmem_aops; inode->i_op = &shmem_symlink_inode_operations; memcpy(folio_address(folio), symname, len); @@ -3191,6 +3218,9 @@ static int shmem_symlink(struct mnt_idmap *idmap, struct inode *dir, d_instantiate(dentry, inode); dget(dentry); return 0; + +out_remove_offset: + simple_offset_remove(shmem_get_offset_ctx(dir), dentry); out_iput: iput(inode); return error; @@ -3920,6 +3950,8 @@ static void shmem_destroy_inode(struct inode *inode) { if (S_ISREG(inode->i_mode)) mpol_free_shared_policy(&SHMEM_I(inode)->policy); + if (S_ISDIR(inode->i_mode)) + simple_offset_destroy(shmem_get_offset_ctx(inode)); } static void shmem_init_inode(void *foo) @@ -4000,6 +4032,7 @@ static const struct inode_operations shmem_dir_inode_operations = { .mknod = shmem_mknod, .rename = shmem_rename2, .tmpfile = shmem_tmpfile, + .get_offset_ctx = shmem_get_offset_ctx, #endif #ifdef CONFIG_TMPFS_XATTR .listxattr = shmem_listxattr,