Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4653821pxj; Tue, 22 Jun 2021 05:15:37 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwDsECUQO5/XafzAz/qSZbRCsq3kNTLiLJfSOEHYmCb+eA+cQKhIWBV83DgVtJWu3+1H6hk X-Received: by 2002:a17:906:7012:: with SMTP id n18mr3741675ejj.236.1624364137431; Tue, 22 Jun 2021 05:15:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624364137; cv=none; d=google.com; s=arc-20160816; b=ZNO5q/BWzK/pxegK+3K2m4c03ihBLvl6MIghxatM23T/lB8Qu6gTRO+txR3jZlZ0de 8q5m5Z7HB4EurPTeGeYNQvjKz28MPEe5O6FQ/WawMLNzJKKHf5U3HAEF+1gohBCbdXhL Lyl1qqWd7+ayXJ8W0MjY3mlSPZR9M8I4JwqJ32cbHtnxYT4fjFa9IPbuQxwP+N2agXi9 3Lw5IKSSP09/dpIt+WgsoUlqYxAM2AvEhwOgzh0k6sL1BTvndLto0AvUb10i2Ak18os9 Bu5dGrUOZ7JHmLG4akYvXEzeMekuQLTEvRMp2Fjsc0LcyW/H8UNCTy+9b5fFkbeVbjX+ c6bA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:to:from; bh=gnawBoDXAkSCixFQj3tqTynS15R51PA7FxkD/pvDOu4=; b=mz/ahhVYyM/sbisHwsJDYIlmb0lGvPfG2m457TThNFrsrjcge8qSZXBlF8BqR2iYfT cR9CVOO6wWpWEpxTzJuPlZB4l5xFBcof+FzWl5tQoGif7Tvebvaop7+S4smUQYiTHWIe 6nVseP1ddAnM7dIDGYQEXic/sWJyCMQhiIbvKGInRusii8pPfbPlAy46TyytgQL+OAan ePQ+UU2uWpt9kyrNXYM8E+MEpjzUTOcN63T3/rLn5j8UXk3TFZfq1JGaPl6cQRHJH/P4 lyCTMzQAw0Q+jNwJfmvVZ8CVopDga7JyUbitk3h7YYhE7fJfRQ3dJou2uEn4r3tlCYBD +8sQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id u15si13623201eju.396.2021.06.22.05.15.13; Tue, 22 Jun 2021 05:15:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231512AbhFVMOJ (ORCPT + 99 others); Tue, 22 Jun 2021 08:14:09 -0400 Received: from mout.kundenserver.de ([217.72.192.75]:42771 "EHLO mout.kundenserver.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231261AbhFVMOB (ORCPT ); Tue, 22 Jun 2021 08:14:01 -0400 Received: from orion.localdomain ([95.117.21.172]) by mrelayeu.kundenserver.de (mreue107 [212.227.15.183]) with ESMTPSA (Nemesis) id 1MjSwu-1lXv4n04de-00l0Mu; Tue, 22 Jun 2021 14:11:43 +0200 From: "Enrico Weigelt, metux IT consult" To: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, viro@zeniv.linux.org.uk Subject: [RFC PATCH 1/6] fs: generic file operation for fstrim Date: Tue, 22 Jun 2021 14:11:31 +0200 Message-Id: <20210622121136.4394-2-info@metux.net> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210622121136.4394-1-info@metux.net> References: <20210622121136.4394-1-info@metux.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K1:mxnG189gnivpMOPC1iOvIJAilxj0ERkOrGBc0UZYCjdpWI1Ubs0 T9yoC6NEk+K7+Jpb+a+kR/KCI0/mw2BXxEUPAaAoyUP/I8zhmJnsdD9TiNOMCfQRu+8pOTU 2MA3ThpwpXuENCLw55zlsvIDwjSMV5cZCMQbNZ9y0bbfD5uVvCHwVoOyrJmSdeUpRXPIalb CieF9xMr5/I9R+3X/6vfA== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:0m9wi6GdRnY=:eW/MAhdwj1XV05G2upUjs7 tbo1j7ZY+NkNajmKR1YOC3INfClsijRQvlbwL7egQsrE1j+yrkekmh0aGtx1yfaEksEhqSgDW IM8pC4RfLWZVrhwfkQ6LalQndAnQR7bkRtROr7vhECdN5Kl0SoQGHCiB+/jbYSSZJH9r5k5oK paK+NUFGhbjFd1Ht5uIwLhY8QJbYyxug/4N98vmCGebnmAtr70EtwdWR4nQ+CwZC6Tnv5c1TD aX74x2s9x+Qut8j6SIGw5hBSs06flZK05X7D7Kz/WSGKsEHHxXs9IVopVVGyS9LlzTaroQOm3 +SBhoDePFCGj19Xw8ZUMnzzCn81QRlbEMGsm+lfhlBB2qShvoXIL1m7EM4bxtes2QEWAZtRFS pU48EnwzrDUOEU6W4dSK39p48bAFGXnmQpK8JIvM2PYJzwQ/vVH8762k4xx/9nHgYNGwZdBxk YQ8Axku+FuqjE00ZH4oFfBKVwckz23lBEPv6weOMPqp0ozKQnO0rWa3CyJg3AeY8oaovpBQS0 ALSieVLaPMus8KluY3RXeo= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In order to factor out common parts of fstrim ioctl's, introduce a new file_operation field for the file system specific parts. The generic ioctl code (do_vfs_ioctl) calls an helper for doing the common actions (eg. permission checks or buffer copying). If the file system doesn't supply an fstrim function, the call will be passed down directly to the file system, as it used to be. This change also enables an easier way for possible future relaxing of permissions, eg. allowing unprivileged users to trim their own files sounds like an apelling idea. Signed-off-by: Enrico Weigelt, metux IT consult --- fs/ioctl.c | 48 ++++++++++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 1 + 2 files changed, 49 insertions(+) diff --git a/fs/ioctl.c b/fs/ioctl.c index 1e2204fa9963..3a638f1eb0f5 100644 --- a/fs/ioctl.c +++ b/fs/ioctl.c @@ -961,6 +961,51 @@ static int ioctl_fssetxattr(struct file *file, void __user *argp) return err; } +/* + * ioctl_fitrim() generic handling for FITRIM ioctl() via dedicated file + * operation. It does't common things like copying the arg from/to userland, + * permission check, etc. + * + * If the handler in file_operation is NULL, just pass the ioctl down to the + * generic ioctl() handler, as it used to be. + */ +static int ioctl_fitrim(struct file *filp, unsigned int fd, unsigned int cmd, + void __user *argp) +{ + struct fstrim_range; + int ret; + + + if (S_ISREG(inode->i_mode)) + return file_ioctl(filp, cmd, argp); + break; + + /* if the fs doesn't implement the fitrim operation, just pass it to + the fs's ioctl() operation, so remaining implementations are kept + intact. this can be removed when all fs'es are converted */ + if (!filp->f_op->fitrim) { + if (S_ISREG(inode->i_mode)) + return file_ioctl(filp, cmd, argp); + + return -ENOIOCTLCMD; + } + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + if (copy_from_user(&range, (struct fstrim_range __user)*argp, + sizeof(range))) + return -EFAULT; + + ret = filp->f_op->fitrim(filp, &range); + + if (copy_to_user((struct fstrim_range __user)*argp, &range, + sizeof(range))) + return -EFAULT; + + return ret; +} + /* * do_vfs_ioctl() is not for drivers and not intended to be EXPORT_SYMBOL()'d. * It's just a simple helper for sys_ioctl and compat_sys_ioctl. @@ -1043,6 +1088,9 @@ static int do_vfs_ioctl(struct file *filp, unsigned int fd, case FS_IOC_FSSETXATTR: return ioctl_fssetxattr(filp, argp); + case FITRIM: + return ioctl_fitrim(filp, cmd, argp); + default: if (S_ISREG(inode->i_mode)) return file_ioctl(filp, cmd, argp); diff --git a/include/linux/fs.h b/include/linux/fs.h index c3c88fdb9b2a..9e2f0cd5c787 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2059,6 +2059,7 @@ struct file_operations { struct file *file_out, loff_t pos_out, loff_t len, unsigned int remap_flags); int (*fadvise)(struct file *, loff_t, loff_t, int); + long (*fstrim)(struct file *, struct fstrim_range *range); } __randomize_layout; struct inode_operations { -- 2.20.1