From patchwork Sat Dec 12 16:51:01 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 11970151 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-13.9 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNWANTED_LANGUAGE_BODY, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E54A4C433FE for ; Sat, 12 Dec 2020 16:52:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BB4182246B for ; Sat, 12 Dec 2020 16:52:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2407682AbgLLQwW (ORCPT ); Sat, 12 Dec 2020 11:52:22 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49358 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2404701AbgLLQvz (ORCPT ); Sat, 12 Dec 2020 11:51:55 -0500 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 127ECC0613D3 for ; Sat, 12 Dec 2020 08:51:15 -0800 (PST) Received: by mail-pf1-x441.google.com with SMTP id b26so9185372pfi.3 for ; Sat, 12 Dec 2020 08:51:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=HNS3V4F8jAe8Zg8QFutKhAJSQ459FMWmyXZjmDhEIcQ=; b=LBeFVWwSvVYcHQOqOnBvJy6D7RQeZXrLMa7FHTN7lzKiON6X/gPAszHopNoHYqQaUT DBScY/GsVBtQA1GZMdltjSjR9NYHbJWl0jXiZAvon1qBe/1DA5ONjDUi7fIFWVzMjvw9 hVQ24SoovJxckdMOrgT4gg0Diem0kjnO+z605nXLz0c7mwQwg+Y5w+RvnVy0j8+Moo1j Sx9S4AwZ7BuNgsYYxT1HdQiSrqilw5b5frKUfWA/uwc8GEtzE2h6fEpttOjqL/Q1xcsE zyNgmLQJCqyHsNRlQgO1tdrZXvyUVkx+PXit+fDysZfK6mql2GigGyiwY6U7k5Xd1qdX mpMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=HNS3V4F8jAe8Zg8QFutKhAJSQ459FMWmyXZjmDhEIcQ=; b=fDNPVL00GnVhHmQn9t/RLDlnxe9RhYDviqD2Mez38V8D/1036BlNKXeZXofLaBe6xk qJnZeZ5h6gfr+WGU47DqEIatpEUX5EGBjGR++hstrCgiAfUH8u+h8zDgsAukfTYMqpzd q6RmsXt0GriTouUzmoCuZhzMJLBwY5sgEnTGA754hypaki2UcoTqwKdgLM1OxU99mMMJ RwycJUO6LIsmxSzrZ6ydQ9qqLgMK+D1nlhyjBCxtDlinEIeU1JRZ01eZMB85P/pPSsF7 kCklbv9gLcLu8c3TZS/z22yCaXElpt52o2zS/FY+7rVtHjOvpUqtKuXYPJNObn70UMAA AhFg== X-Gm-Message-State: AOAM531uKvxzvSLlzur+eisfL87gwlBeFi9XFboEBwr8Y6A5oERd/0YP t3Y4ePYrzA0yZWq39WaysDcz0940MLqWDw== X-Google-Smtp-Source: ABdhPJzWfiWqvSEVuazHYU7fN/0MbPH8ZVCUGFFEyfc7T0zyHPgnPzmzd0cOg0ix3D3ZQdEpWnU/Aw== X-Received: by 2002:a63:560b:: with SMTP id k11mr11205752pgb.407.1607791874246; Sat, 12 Dec 2020 08:51:14 -0800 (PST) Received: from p1.localdomain ([66.219.217.173]) by smtp.gmail.com with ESMTPSA id s17sm14855352pge.37.2020.12.12.08.51.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Dec 2020 08:51:13 -0800 (PST) From: Jens Axboe To: linux-fsdevel@vger.kernel.org Cc: torvalds@linux-foundation.org, viro@zeniv.linux.org.uk, Jens Axboe Subject: [PATCH 1/5] fs: make unlazy_walk() error handling consistent Date: Sat, 12 Dec 2020 09:51:01 -0700 Message-Id: <20201212165105.902688-2-axboe@kernel.dk> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201212165105.902688-1-axboe@kernel.dk> References: <20201212165105.902688-1-axboe@kernel.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Most callers check for non-zero return, and assume it's -ECHILD (which it always will be). One caller uses the actual error return. Clean this up and make it fully consistent, by having unlazy_walk() return a bool instead. Rename it to try_to_unlazy() and return true on success, and failure on error. That's easier to read. No functional changes in this patch. Cc: Al Viro Signed-off-by: Jens Axboe --- fs/namei.c | 43 +++++++++++++++++-------------------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index 03d0e11e4f36..7eb7830da298 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -669,17 +669,17 @@ static bool legitimize_root(struct nameidata *nd) */ /** - * unlazy_walk - try to switch to ref-walk mode. + * try_to_unlazy - try to switch to ref-walk mode. * @nd: nameidata pathwalk data - * Returns: 0 on success, -ECHILD on failure + * Returns: true on success, false on failure * - * unlazy_walk attempts to legitimize the current nd->path and nd->root + * try_to_unlazyk attempts to legitimize the current nd->path and nd->root * for ref-walk mode. * Must be called from rcu-walk context. - * Nothing should touch nameidata between unlazy_walk() failure and + * Nothing should touch nameidata between try_to_unlazy() failure and * terminate_walk(). */ -static int unlazy_walk(struct nameidata *nd) +static bool try_to_unlazy(struct nameidata *nd) { struct dentry *parent = nd->path.dentry; @@ -694,14 +694,14 @@ static int unlazy_walk(struct nameidata *nd) goto out; rcu_read_unlock(); BUG_ON(nd->inode != parent->d_inode); - return 0; + return true; out1: nd->path.mnt = NULL; nd->path.dentry = NULL; out: rcu_read_unlock(); - return -ECHILD; + return false; } /** @@ -792,7 +792,7 @@ static int complete_walk(struct nameidata *nd) */ if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED))) nd->root.mnt = NULL; - if (unlikely(unlazy_walk(nd))) + if (!try_to_unlazy(nd)) return -ECHILD; } @@ -1466,7 +1466,7 @@ static struct dentry *lookup_fast(struct nameidata *nd, unsigned seq; dentry = __d_lookup_rcu(parent, &nd->last, &seq); if (unlikely(!dentry)) { - if (unlazy_walk(nd)) + if (!try_to_unlazy(nd)) return ERR_PTR(-ECHILD); return NULL; } @@ -1567,10 +1567,8 @@ static inline int may_lookup(struct nameidata *nd) { if (nd->flags & LOOKUP_RCU) { int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); - if (err != -ECHILD) + if (err != -ECHILD || !try_to_unlazy(nd)) return err; - if (unlazy_walk(nd)) - return -ECHILD; } return inode_permission(nd->inode, MAY_EXEC); } @@ -1592,7 +1590,7 @@ static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq) // unlazy even if we fail to grab the link - cleanup needs it bool grabbed_link = legitimize_path(nd, link, seq); - if (unlazy_walk(nd) != 0 || !grabbed_link) + if (!try_to_unlazy(nd) != 0 || !grabbed_link) return -ECHILD; if (nd_alloc_stack(nd)) @@ -1634,7 +1632,7 @@ static const char *pick_link(struct nameidata *nd, struct path *link, touch_atime(&last->link); cond_resched(); } else if (atime_needs_update(&last->link, inode)) { - if (unlikely(unlazy_walk(nd))) + if (!try_to_unlazy(nd)) return ERR_PTR(-ECHILD); touch_atime(&last->link); } @@ -1651,11 +1649,8 @@ static const char *pick_link(struct nameidata *nd, struct path *link, get = inode->i_op->get_link; if (nd->flags & LOOKUP_RCU) { res = get(NULL, inode, &last->done); - if (res == ERR_PTR(-ECHILD)) { - if (unlikely(unlazy_walk(nd))) - return ERR_PTR(-ECHILD); + if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd)) res = get(link->dentry, inode, &last->done); - } } else { res = get(link->dentry, inode, &last->done); } @@ -2193,7 +2188,7 @@ static int link_path_walk(const char *name, struct nameidata *nd) } if (unlikely(!d_can_lookup(nd->path.dentry))) { if (nd->flags & LOOKUP_RCU) { - if (unlazy_walk(nd)) + if (!try_to_unlazy(nd)) return -ECHILD; } return -ENOTDIR; @@ -3127,7 +3122,6 @@ static const char *open_last_lookups(struct nameidata *nd, struct inode *inode; struct dentry *dentry; const char *res; - int error; nd->flags |= op->intent; @@ -3151,9 +3145,8 @@ static const char *open_last_lookups(struct nameidata *nd, } else { /* create side of things */ if (nd->flags & LOOKUP_RCU) { - error = unlazy_walk(nd); - if (unlikely(error)) - return ERR_PTR(error); + if (!try_to_unlazy(nd)) + return ERR_PTR(-ECHILD); } audit_inode(nd->name, dir, AUDIT_INODE_PARENT); /* trailing slashes? */ @@ -3162,9 +3155,7 @@ static const char *open_last_lookups(struct nameidata *nd, } if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { - error = mnt_want_write(nd->path.mnt); - if (!error) - got_write = true; + got_write = !mnt_want_write(nd->path.mnt); /* * do _not_ fail yet - we might not need that or fail with * a different error; let lookup_open() decide; we'll be From patchwork Sat Dec 12 16:51:02 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 11970149 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CF572C4361B for ; Sat, 12 Dec 2020 16:52:22 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A1F2D22475 for ; Sat, 12 Dec 2020 16:52:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2407681AbgLLQwR (ORCPT ); Sat, 12 Dec 2020 11:52:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49362 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2405827AbgLLQv4 (ORCPT ); Sat, 12 Dec 2020 11:51:56 -0500 Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5559FC0613D6 for ; Sat, 12 Dec 2020 08:51:16 -0800 (PST) Received: by mail-pg1-x541.google.com with SMTP id w16so9460064pga.9 for ; Sat, 12 Dec 2020 08:51:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=bs8HhYRrXZk0BV9zDW0PP2lEDiHkjRKLbU8rK5axhYs=; b=DCAvRPh01mhUixtYpjdb3M4oF+UQwH1+U1L3H8V1di+UmKZfcBIf7s59fhRCiIqYXP ci/T6N1fzeMocK02pTvSQDSbbwKlUpDy+/QWWSy8V92zsAmBuspiP0zDzlC7g1uPweBu /UaOHpwzyV4DpUy6MRSlz5QRQhz3n0NacJWj3n02lgMwhqvL83yxcyOKywZTm6s2199p Re+PqjC1mIrOSW4wEfxMR9TYMWy/i+gmdGBb8SEb+V389IqP6nAPYgURV6jKjCTT2TuG mikgnPvzXEUhwyxVH8ZNOB9wBwpMDgGJLPBXDJmKS5BGllpd+bV5SBaPbsgscxywd/hB mN3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bs8HhYRrXZk0BV9zDW0PP2lEDiHkjRKLbU8rK5axhYs=; b=RvnWc5zsivSyIHaJsWzLhKvNi2LXKILFXnkNkVdV39Yx+sZJKKU20hjnUtDdkJlzIl kTPoABRs9bpxZcdNrYoc+ivLsJMpXV4mzBtcfqULVWjOE7wszvKgetZYVfst+J4Vdy2g ZQOKYUj/SEkNs19mgs9aXnFTYrw4rv8lrFX2kSqnV42dwQuz6PXUrRyoE+Abcw+GvDfz 77OzbxJvdRXBC/8QiB9bZoJ7MZwO7Zple2YDiLtarapnkNCSZykrvKTZmXVfjshCLu9Y 6+6gbE+V1C6AfUiFY6MgGwZezOiMYq6GsQGCa/acgLmlh7/YF/1TLdNowB+wx52srjKz fo2Q== X-Gm-Message-State: AOAM530Y1CVRZIZfIUeiVmazcBUfHpFqitqyIvFiJg3B20WhIyg6Bn15 TlDkasFvrHqczSGyXz9gs67giGBN0wTHRg== X-Google-Smtp-Source: ABdhPJw3+16ATTQ3MDjPe8kI6WZqsUxvc4F+vRsV2sRVnR9mEgeXdD2JrRpWmD03ZXgSsJ7I0lVbPw== X-Received: by 2002:aa7:9e52:0:b029:19e:6c32:30d4 with SMTP id z18-20020aa79e520000b029019e6c3230d4mr16709260pfq.21.1607791875532; Sat, 12 Dec 2020 08:51:15 -0800 (PST) Received: from p1.localdomain ([66.219.217.173]) by smtp.gmail.com with ESMTPSA id s17sm14855352pge.37.2020.12.12.08.51.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Dec 2020 08:51:15 -0800 (PST) From: Jens Axboe To: linux-fsdevel@vger.kernel.org Cc: torvalds@linux-foundation.org, viro@zeniv.linux.org.uk, Jens Axboe Subject: [PATCH 2/5] fs: add support for LOOKUP_NONBLOCK Date: Sat, 12 Dec 2020 09:51:02 -0700 Message-Id: <20201212165105.902688-3-axboe@kernel.dk> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201212165105.902688-1-axboe@kernel.dk> References: <20201212165105.902688-1-axboe@kernel.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org io_uring always punts opens to async context, since there's no control over whether the lookup blocks or not. Add LOOKUP_NONBLOCK to support just doing the fast RCU based lookups, which we know will not block. If we can do a cached path resolution of the filename, then we don't have to always punt lookups for a worker. During path resolution, we always do LOOKUP_RCU first. If that fails and we terminate LOOKUP_RCU, then fail a LOOKUP_NONBLOCK attempt as well. Cc: Al Viro Signed-off-by: Jens Axboe --- fs/namei.c | 9 +++++++++ include/linux/namei.h | 1 + 2 files changed, 10 insertions(+) diff --git a/fs/namei.c b/fs/namei.c index 7eb7830da298..07a1aa874f65 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -686,6 +686,8 @@ static bool try_to_unlazy(struct nameidata *nd) BUG_ON(!(nd->flags & LOOKUP_RCU)); nd->flags &= ~LOOKUP_RCU; + if (nd->flags & LOOKUP_NONBLOCK) + goto out1; if (unlikely(!legitimize_links(nd))) goto out1; if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) @@ -722,6 +724,8 @@ static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned se BUG_ON(!(nd->flags & LOOKUP_RCU)); nd->flags &= ~LOOKUP_RCU; + if (nd->flags & LOOKUP_NONBLOCK) + goto out2; if (unlikely(!legitimize_links(nd))) goto out2; if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq))) @@ -792,6 +796,7 @@ static int complete_walk(struct nameidata *nd) */ if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED))) nd->root.mnt = NULL; + nd->flags &= ~LOOKUP_NONBLOCK; if (!try_to_unlazy(nd)) return -ECHILD; } @@ -2202,6 +2207,10 @@ static const char *path_init(struct nameidata *nd, unsigned flags) int error; const char *s = nd->name->name; + /* LOOKUP_NONBLOCK requires RCU, ask caller to retry */ + if ((flags & (LOOKUP_RCU | LOOKUP_NONBLOCK)) == LOOKUP_NONBLOCK) + return ERR_PTR(-EAGAIN); + if (!*s) flags &= ~LOOKUP_RCU; if (flags & LOOKUP_RCU) diff --git a/include/linux/namei.h b/include/linux/namei.h index a4bb992623c4..c36c4e0805fc 100644 --- a/include/linux/namei.h +++ b/include/linux/namei.h @@ -46,6 +46,7 @@ enum {LAST_NORM, LAST_ROOT, LAST_DOT, LAST_DOTDOT}; #define LOOKUP_NO_XDEV 0x040000 /* No mountpoint crossing. */ #define LOOKUP_BENEATH 0x080000 /* No escaping from starting point. */ #define LOOKUP_IN_ROOT 0x100000 /* Treat dirfd as fs root. */ +#define LOOKUP_NONBLOCK 0x200000 /* don't block for lookup */ /* LOOKUP_* flags which do scope-related checks based on the dirfd. */ #define LOOKUP_IS_SCOPED (LOOKUP_BENEATH | LOOKUP_IN_ROOT) From patchwork Sat Dec 12 16:51:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 11970147 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 04C6BC4361B for ; Sat, 12 Dec 2020 16:52:18 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D210522475 for ; Sat, 12 Dec 2020 16:52:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2407680AbgLLQwI (ORCPT ); Sat, 12 Dec 2020 11:52:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2405951AbgLLQv5 (ORCPT ); Sat, 12 Dec 2020 11:51:57 -0500 Received: from mail-pj1-x1043.google.com (mail-pj1-x1043.google.com [IPv6:2607:f8b0:4864:20::1043]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F5A3C061793 for ; Sat, 12 Dec 2020 08:51:17 -0800 (PST) Received: by mail-pj1-x1043.google.com with SMTP id lj6so2508240pjb.0 for ; Sat, 12 Dec 2020 08:51:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VTka/gpM64k/H7yRLstKZFGJk0r4OmXli/cCm+jSN2I=; b=YI4UOEoSrYqL2SQF5uzul8hd8gHkG76ZWTU4YGdGrqYHXjSD2gWqk0eNdj/CSxwkd1 H72EbdOohorQTaHOhjOgjxqPxlBsqurA45LhdvWPoN/4KuiECfQBUQZlsLTYG22Ivag+ UYaP6I32RGIfEG9tiStfbF0CjotP2Hpeec85RzM6ShxVgsxGufpxeI+SsIUkeDPXPEuw wV7ENWShhY0IW3ijUD9609kpNKn3f4SR9+L3qeUyeZ8ZixEVzDEHrvyTeH+oTvtzx202 Eg9yVo36WVqh0XvY87Y674obWsqPbIeBnP7io4SP2VaBf6YS8rlg0Fbtp18on7g4qxan 8JPQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VTka/gpM64k/H7yRLstKZFGJk0r4OmXli/cCm+jSN2I=; b=RS+ZP09pxDr0c5su0qmGYKP2Ds438Bwn1L2oLncLP8lQp7EZBZJevV33zmH5+noM35 25Bo7kmWxyvVUsbZEgy5+ESoeQWHjwL5Ai3fZFgmUcoNmNypRZCVN4TXZ/JB9KPlAbXz WRPQzDwtGyOnIRTF8YhHlNQXDttBqiLLxCCVUnEID/RVaD7kWCFe3Wzf1zqLxGfWR/3H GTJ40Cr99BHXdjIR+ZGHE8+abYSgykZBdKZPXoEM4uLnISpdq/8PHBkhge5+lqfsSYNL 9VQ93IymElSVx74DFGbjW6u1+B3aDfJsAotc6HlYIBaCtBBGFCbGjHkQCSvAWDia5UsS Db/A== X-Gm-Message-State: AOAM531QoVBZVPJKOQzCKytaiIyNrH9S/7uxh1fiRrGgNrzIxHaxG6zf YE/0GD683RHPmamgSjEPuCSMkkZwJXhU9g== X-Google-Smtp-Source: ABdhPJw3/r5x8pvaInMYvJmCE5bzsm0V4ctUb8EA2WRVTatl4tefhaPU8/dQ3MwANxJ5JluG827ycA== X-Received: by 2002:a17:902:bcca:b029:da:61e3:a032 with SMTP id o10-20020a170902bccab02900da61e3a032mr15779975pls.63.1607791876678; Sat, 12 Dec 2020 08:51:16 -0800 (PST) Received: from p1.localdomain ([66.219.217.173]) by smtp.gmail.com with ESMTPSA id s17sm14855352pge.37.2020.12.12.08.51.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Dec 2020 08:51:16 -0800 (PST) From: Jens Axboe To: linux-fsdevel@vger.kernel.org Cc: torvalds@linux-foundation.org, viro@zeniv.linux.org.uk, Jens Axboe Subject: [PATCH 3/5] fs: add mnt_want_write_trylock() Date: Sat, 12 Dec 2020 09:51:03 -0700 Message-Id: <20201212165105.902688-4-axboe@kernel.dk> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201212165105.902688-1-axboe@kernel.dk> References: <20201212165105.902688-1-axboe@kernel.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org trylock variant of mnt_want_write() - this ends up being pretty trivial, as we already have a trylock variant of the sb_start_write() helper. Signed-off-by: Jens Axboe --- fs/namespace.c | 18 ++++++++++++++++++ include/linux/mount.h | 1 + 2 files changed, 19 insertions(+) diff --git a/fs/namespace.c b/fs/namespace.c index cebaa3e81794..7881cb5595af 100644 --- a/fs/namespace.c +++ b/fs/namespace.c @@ -359,6 +359,24 @@ int mnt_want_write(struct vfsmount *m) } EXPORT_SYMBOL_GPL(mnt_want_write); +/** + * mnt_want_write_trylock - try to get write access to a mount + * @m: the mount on which to take a write + * + * trylock variant of @mnt_want_write. See description above. + */ +int mnt_want_write_trylock(struct vfsmount *m) +{ + int ret; + + if (!sb_start_write_trylock(m->mnt_sb)) + return -EAGAIN; + ret = __mnt_want_write(m); + if (ret) + sb_end_write(m->mnt_sb); + return ret; +} + /** * mnt_clone_write - get write access to a mount * @mnt: the mount on which to take a write diff --git a/include/linux/mount.h b/include/linux/mount.h index aaf343b38671..e267e622d843 100644 --- a/include/linux/mount.h +++ b/include/linux/mount.h @@ -78,6 +78,7 @@ struct file; /* forward dec */ struct path; extern int mnt_want_write(struct vfsmount *mnt); +extern int mnt_want_write_trylock(struct vfsmount *mnt); extern int mnt_want_write_file(struct file *file); extern int mnt_clone_write(struct vfsmount *mnt); extern void mnt_drop_write(struct vfsmount *mnt); From patchwork Sat Dec 12 16:51:04 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 11970153 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A2518C433FE for ; Sat, 12 Dec 2020 16:52:43 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 7657D22475 for ; Sat, 12 Dec 2020 16:52:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2407683AbgLLQwc (ORCPT ); Sat, 12 Dec 2020 11:52:32 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49374 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2406053AbgLLQv6 (ORCPT ); Sat, 12 Dec 2020 11:51:58 -0500 Received: from mail-pl1-x644.google.com (mail-pl1-x644.google.com [IPv6:2607:f8b0:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BAC67C061794 for ; Sat, 12 Dec 2020 08:51:18 -0800 (PST) Received: by mail-pl1-x644.google.com with SMTP id g20so5508921plo.2 for ; Sat, 12 Dec 2020 08:51:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Sj4dbiovnGr54xIL9U5s6SlD51u/w+iAlhDwtYg8FCA=; b=XdXT5Krilx/VzAUqbz0eIs0QqP956iAcogJMiY0ybLxfyQeHZKpZwzIkPtS2Os//QM qCC5LlBB4UY/m8ayEvmRluD0vQe41fU2SllSCp3mtP173nrxEatkc0WnjRVeojbzAiW9 85SkLUSEYT+GueTiOBPOazt3Lfc8HoRXAcIquEER+qBm+mTU0SCuidfXfgR8+wp7v8My eGXo8jtwne07i2eKNNQZ24/1KRbCraki7ty2Uk3FN8RWFdbom9A7emJetfKUWzDe9/7B 4WxED0dwfvWu/4Kb59c8W5osrbc8qfPvOD7F6lkZXYwhgUqZB04K9yF4CQpmFEwSJxWv BNXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Sj4dbiovnGr54xIL9U5s6SlD51u/w+iAlhDwtYg8FCA=; b=gvzF4m+eK6TMWcZGn+A74FU3fD7BHP/7A+k8i6Rzit8ZonEFRMIL6PA+IL3JdH0yaM LqNxo288FgvuKCW6t+Y+TJxKWF3pNudR53pBIZqFDPFQflQqB9H8snlf54wIcIMMRx55 evnTD1Z516yslnri/B1NgBD6xh4/MnAUBqk+7WiMqcg/hzKzvhvHS2CaSqkXQVbFfRB1 a693/DqPYFczfCJamwU3own2o4nHFdV+fIxfoh1KTXu3YdsstOBqr9BexfQB7LwSn7WN 9Tw41kNG9gDGf9Pk/mRkAKX+7WqPxTIkKdiDnP6XYgkA+sIY5lUm1iH0dQxdYo22fVVJ W2/g== X-Gm-Message-State: AOAM531CGDRooQ8j0mI51y+VnlabIwRnBCZopOWg6vi7CQ43GPB24Qob 5bGNogA8AekO1U6TGgQ4RvSVQoJfEYX+Vg== X-Google-Smtp-Source: ABdhPJzRGbpwnjTZBThsdFy1NV7Dz41RTMAu5K+gu5WbJGMGoE0YZcijxCWto/LeBfbmGj1ipniqFw== X-Received: by 2002:a17:902:ac93:b029:db:c725:e321 with SMTP id h19-20020a170902ac93b02900dbc725e321mr15786186plr.41.1607791877960; Sat, 12 Dec 2020 08:51:17 -0800 (PST) Received: from p1.localdomain ([66.219.217.173]) by smtp.gmail.com with ESMTPSA id s17sm14855352pge.37.2020.12.12.08.51.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Dec 2020 08:51:17 -0800 (PST) From: Jens Axboe To: linux-fsdevel@vger.kernel.org Cc: torvalds@linux-foundation.org, viro@zeniv.linux.org.uk, Jens Axboe Subject: [PATCH 4/5] fs: honor LOOKUP_NONBLOCK for the last part of file open Date: Sat, 12 Dec 2020 09:51:04 -0700 Message-Id: <20201212165105.902688-5-axboe@kernel.dk> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201212165105.902688-1-axboe@kernel.dk> References: <20201212165105.902688-1-axboe@kernel.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org We handle it for the path resolution itself, but we should also factor it in for open_last_lookups() and tmpfile open. We don't allow RESOLVE_NONBLOCK with O_TRUNC, so that case we can safely ignore. Signed-off-by: Jens Axboe --- fs/namei.c | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/fs/namei.c b/fs/namei.c index 07a1aa874f65..1f976a213eef 100644 --- a/fs/namei.c +++ b/fs/namei.c @@ -3127,6 +3127,7 @@ static const char *open_last_lookups(struct nameidata *nd, struct dentry *dir = nd->path.dentry; int open_flag = op->open_flag; bool got_write = false; + bool nonblock = nd->flags & LOOKUP_NONBLOCK; unsigned seq; struct inode *inode; struct dentry *dentry; @@ -3164,17 +3165,38 @@ static const char *open_last_lookups(struct nameidata *nd, } if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { - got_write = !mnt_want_write(nd->path.mnt); + if (nonblock) { + got_write = !mnt_want_write_trylock(nd->path.mnt); + if (!got_write) + return ERR_PTR(-EAGAIN); + } else { + got_write = !mnt_want_write(nd->path.mnt); + } /* * do _not_ fail yet - we might not need that or fail with * a different error; let lookup_open() decide; we'll be * dropping this one anyway. */ } - if (open_flag & O_CREAT) - inode_lock(dir->d_inode); - else - inode_lock_shared(dir->d_inode); + if (open_flag & O_CREAT) { + if (nonblock) { + if (!inode_trylock(dir->d_inode)) { + dentry = ERR_PTR(-EAGAIN); + goto drop_write; + } + } else { + inode_lock(dir->d_inode); + } + } else { + if (nonblock) { + if (!inode_trylock_shared(dir->d_inode)) { + dentry = ERR_PTR(-EAGAIN); + goto drop_write; + } + } else { + inode_lock_shared(dir->d_inode); + } + } dentry = lookup_open(nd, file, op, got_write); if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED)) fsnotify_create(dir->d_inode, dentry); @@ -3183,6 +3205,7 @@ static const char *open_last_lookups(struct nameidata *nd, else inode_unlock_shared(dir->d_inode); +drop_write: if (got_write) mnt_drop_write(nd->path.mnt); @@ -3242,6 +3265,7 @@ static int do_open(struct nameidata *nd, open_flag &= ~O_TRUNC; acc_mode = 0; } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { + WARN_ON_ONCE(nd->flags & LOOKUP_NONBLOCK); error = mnt_want_write(nd->path.mnt); if (error) return error; @@ -3311,7 +3335,10 @@ static int do_tmpfile(struct nameidata *nd, unsigned flags, int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); if (unlikely(error)) return error; - error = mnt_want_write(path.mnt); + if (flags & LOOKUP_NONBLOCK) + error = mnt_want_write_trylock(path.mnt); + else + error = mnt_want_write(path.mnt); if (unlikely(error)) goto out; child = vfs_tmpfile(path.dentry, op->mode, op->open_flag); From patchwork Sat Dec 12 16:51:05 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jens Axboe X-Patchwork-Id: 11970155 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.7 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 296C2C4361B for ; Sat, 12 Dec 2020 16:52:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id F2AC022475 for ; Sat, 12 Dec 2020 16:52:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2406246AbgLLQwr (ORCPT ); Sat, 12 Dec 2020 11:52:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49464 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2407684AbgLLQwe (ORCPT ); Sat, 12 Dec 2020 11:52:34 -0500 Received: from mail-pl1-x643.google.com (mail-pl1-x643.google.com [IPv6:2607:f8b0:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0487FC06179C for ; Sat, 12 Dec 2020 08:51:20 -0800 (PST) Received: by mail-pl1-x643.google.com with SMTP id bj5so6341165plb.4 for ; Sat, 12 Dec 2020 08:51:20 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel-dk.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Jx2p8zKcStTihV6+upJYHlS8EgfFfVFr751VhyGZa2I=; b=UuvnrO3fWVC9xrRHQSZQ5RM0661ic/ajsGVxdApFEYAjmW9J8bEm5CnP8+W4gz1DI4 6syYsWwGocS1HyG9ow6tw+40+QRNLxv6ptuj/HbD8+5KeMtcxCRh9jO8mxLfMsUTAyT4 y5Vy7XYTklBlV7rWAiglVtQXLwbdr4fHM+7uCqNbESQBr/2+dkRzSXWcuW0vi/7IL/hw LwDNfxf+LprxZGZgK7czpWhP9nQfGsydBuFWZ/pLAZG1WC3TXvW/i1PaAKb1Ig+Gc8C9 v5cUU7Mijh4qNUaf+DFhvcBi+M7WGJo8b0dyb7/fm2mw+jkrBZZ23D9eLnBZYQJdUqCC jxOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Jx2p8zKcStTihV6+upJYHlS8EgfFfVFr751VhyGZa2I=; b=poDOFrSAzBuovddLT0CfjwjNmCrF2xQVMSNOnUW16jxao7XbgaP0ggXgojVEmvErHe 7bJTrXA374QpRPEkeDLz+zND+EdMByLqDtuEr/fnkt4RD4kraIRdfoalqA4IgM6ZT7X6 7W1+QoBOaK724a9sHpeUM7ytbQpx7nFMGDceixMQ+fJ9VKYxMNoUpLtE4Xc8RJyb6BKH oM1kswpz1bH8zrQ0/i71N7d2LCY3mKauffb0vkXxYTUi7GULaaQFjS5a5u8zPmydrN5/ JbLaPwiHxKmg/6X5J2eaEEGwYQHbF7r05ac3ertLitKAehrWsdcSZMvPYhPGLpGne4lZ YGYA== X-Gm-Message-State: AOAM533Qmrk5oJdKR6sj6+b2V5DQ4lKJId37o8jl6cA2xg932YuBO5O9 rfnQwaI1tADL+a5CmLcdhP52GcXoUUQGZg== X-Google-Smtp-Source: ABdhPJz8j6dPy0qxFPoutYPKyocd3AhloYQEN6avW7MC1aH9b47/N+XAv608OtdUji8jfh2F4YvPIg== X-Received: by 2002:a17:902:9309:b029:db:c725:d19c with SMTP id bc9-20020a1709029309b02900dbc725d19cmr16197172plb.39.1607791879295; Sat, 12 Dec 2020 08:51:19 -0800 (PST) Received: from p1.localdomain ([66.219.217.173]) by smtp.gmail.com with ESMTPSA id s17sm14855352pge.37.2020.12.12.08.51.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 12 Dec 2020 08:51:18 -0800 (PST) From: Jens Axboe To: linux-fsdevel@vger.kernel.org Cc: torvalds@linux-foundation.org, viro@zeniv.linux.org.uk, Jens Axboe Subject: [PATCH 5/5] fs: expose LOOKUP_NONBLOCK through openat2() RESOLVE_NONBLOCK Date: Sat, 12 Dec 2020 09:51:05 -0700 Message-Id: <20201212165105.902688-6-axboe@kernel.dk> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201212165105.902688-1-axboe@kernel.dk> References: <20201212165105.902688-1-axboe@kernel.dk> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org Now that we support non-blocking path resolution internally, expose it via openat2() in the struct open_how ->resolve flags. This allows applications using openat2() to limit path resolution to the extent that it is already cached. If the lookup cannot be satisfied in a non-blocking manner, openat2(2) will return -1/-EAGAIN. Cc: Al Viro Signed-off-by: Jens Axboe --- fs/open.c | 5 +++++ include/linux/fcntl.h | 2 +- include/uapi/linux/openat2.h | 4 ++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/fs/open.c b/fs/open.c index 9af548fb841b..3561ef4a6689 100644 --- a/fs/open.c +++ b/fs/open.c @@ -1087,6 +1087,11 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op) lookup_flags |= LOOKUP_BENEATH; if (how->resolve & RESOLVE_IN_ROOT) lookup_flags |= LOOKUP_IN_ROOT; + if (how->resolve & RESOLVE_NONBLOCK) { + if (flags & O_TRUNC) + return -EAGAIN; + lookup_flags |= LOOKUP_NONBLOCK; + } op->lookup_flags = lookup_flags; return 0; diff --git a/include/linux/fcntl.h b/include/linux/fcntl.h index 921e750843e6..919a13c9317c 100644 --- a/include/linux/fcntl.h +++ b/include/linux/fcntl.h @@ -19,7 +19,7 @@ /* List of all valid flags for the how->resolve argument: */ #define VALID_RESOLVE_FLAGS \ (RESOLVE_NO_XDEV | RESOLVE_NO_MAGICLINKS | RESOLVE_NO_SYMLINKS | \ - RESOLVE_BENEATH | RESOLVE_IN_ROOT) + RESOLVE_BENEATH | RESOLVE_IN_ROOT | RESOLVE_NONBLOCK) /* List of all open_how "versions". */ #define OPEN_HOW_SIZE_VER0 24 /* sizeof first published struct */ diff --git a/include/uapi/linux/openat2.h b/include/uapi/linux/openat2.h index 58b1eb711360..7bc1d0c35108 100644 --- a/include/uapi/linux/openat2.h +++ b/include/uapi/linux/openat2.h @@ -35,5 +35,9 @@ struct open_how { #define RESOLVE_IN_ROOT 0x10 /* Make all jumps to "/" and ".." be scoped inside the dirfd (similar to chroot(2)). */ +#define RESOLVE_NONBLOCK 0x20 /* Only complete if resolution can be + completed through cached lookup. May + return -EAGAIN if that's not + possible. */ #endif /* _UAPI_LINUX_OPENAT2_H */