Received: by 10.192.165.156 with SMTP id m28csp2075250imm; Thu, 12 Apr 2018 08:12:39 -0700 (PDT) X-Google-Smtp-Source: AIpwx4/yFegaqSeGjjETVO08a+x5DwB6y3lnJSHJ+7EEEBM2Vcppv9tluwJaWT1+fTwPkSQWZOxM X-Received: by 2002:a17:902:968d:: with SMTP id n13-v6mr1475039plp.168.1523545959619; Thu, 12 Apr 2018 08:12:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523545959; cv=none; d=google.com; s=arc-20160816; b=lyEOwJI16GqbpP75N7Vn9jyABdxBd4VaFm7i1/fgiILqEbpOipw/qOv963gzOnOGz0 lGFbwBl+HzX8GILZXpkpCpwpTD5x3K2Uw7v7WHqEoK2A+6X4ADPS44YLpV3DKxYJpc27 eGCEP7pnQY6ZZoWsTMIF0p2NwKIQAp1dlNcxrZuQj3N/4EkUVBWgPzIyKtaN+pwCRQXu zwDr7kSW1Uhwsimo5Qhc76vAo59DPbTglLBMGKLyNgmkmGSior8Z7VPDPa7ZzD+OaSCZ GkOt+BetmkZ2Ew6sepih9SpNwdkccrRpa80jtp5c2XjsYHyeZcVzrMuciLF4CbiHb0Vk OGoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=bGdfRxTXrF0hDjrXyus5crJ39DH6ScOb7dkDHuuU3j0=; b=fArLCX3ZV7I7jCtaoTJ/GiNzPLfbF8mDXFRZSO+VcDgSZ5a7OmyyleE/5/iOZNrBjC Xj2EG7XeUvsI0wLQubr8JKBLwbkPiZTKjL02+0D4PpW4iFEXIvrzOTknoHHNJNCSgDrX 39+UrELr4g+YNC6hP8TYBlVft54T9jou1lCK7BHkfvcAj9r3nnTNVndLCgy3ufAd7fYy TFcqRuUjNj0MEjzZDNQZfRC2B0JzscllOY3gBqJy95DRjtK9hzzeSl3FgGrb4VCSNeA7 /yWuM/uWn5hm7oYdA6HLkRuMO3dX+pFNHwsiidUyBQhh/q0CQ5kea5BJ2wsVCLoBp0d+ h9lg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w19-v6si3320567plq.250.2018.04.12.08.12.01; Thu, 12 Apr 2018 08:12:39 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753383AbeDLPIu (ORCPT + 99 others); Thu, 12 Apr 2018 11:08:50 -0400 Received: from mail-wr0-f194.google.com ([209.85.128.194]:43096 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753265AbeDLPIo (ORCPT ); Thu, 12 Apr 2018 11:08:44 -0400 Received: by mail-wr0-f194.google.com with SMTP id y7so5453337wrh.10 for ; Thu, 12 Apr 2018 08:08:44 -0700 (PDT) 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; bh=bGdfRxTXrF0hDjrXyus5crJ39DH6ScOb7dkDHuuU3j0=; b=IC8pnGJkuTMlH57uQnP3z1nhnfuN0C7N3c/RvxPmhsuAXWuiMWVHPLwxbTosaEvie5 Gv5U7rYFYZ0mrnV4LxR/eRmKXCFOogdMgYB36Kkk8mhJIiLpeB6fNA3m9DqOKhPEEt9P fwXzbpIK3F/m1K70HSvx4QG518XlLU5JqoEasRI5C1W1r7H1meHxM3ljJvU3GdlRyFDs ifDQTx+Lu0j+tHBvVKAfbHNtBproN4m5Bn/0pl9ciU6S7k6Hbfkpe465RcXSiyB4wWHM FYYjmiCWdBYUiUf7tqaJCTKce1EdvaGxIKWlIub15FjzyHIq8klwW4WbEi6935ysiR9F N8ig== X-Gm-Message-State: ALQs6tA+38frD1HMlq2bguZuET1/2Px7E7c/9U6EDelmy7JIqmLtAQ9o Cxp3gGWdqCxE3ZIztmIH160+6Ph+oYk= X-Received: by 10.223.209.198 with SMTP id m6mr1113606wri.267.1523545723361; Thu, 12 Apr 2018 08:08:43 -0700 (PDT) Received: from veci.piliscsaba.redhat.com (catv-176-63-54-97.catv.broadband.hu. [176.63.54.97]) by smtp.gmail.com with ESMTPSA id p197sm2621783wme.43.2018.04.12.08.08.42 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 12 Apr 2018 08:08:42 -0700 (PDT) From: Miklos Szeredi To: linux-unionfs@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 10/35] ovl: add helper to return real file Date: Thu, 12 Apr 2018 17:08:01 +0200 Message-Id: <20180412150826.20988-11-mszeredi@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180412150826.20988-1-mszeredi@redhat.com> References: <20180412150826.20988-1-mszeredi@redhat.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In the common case we can just use the real file cached in file->private_data. There are two exceptions: 1) File has been copied up since open: in this unlikely corner case just use a throwaway real file for the operation. If ever this becomes a perfomance problem (very unlikely, since overlayfs has been doing most fine without correctly handling this case at all), then we can deal with that by updating the cached real file. 2) File's f_flags have changed since open: no need to reopen the cached real file, we can just change the flags there as well. Signed-off-by: Miklos Szeredi --- fs/overlayfs/file.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c index a0b606885c41..409b542ff30c 100644 --- a/fs/overlayfs/file.c +++ b/fs/overlayfs/file.c @@ -31,6 +31,66 @@ static struct file *ovl_open_realfile(const struct file *file) return realfile; } +#define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT) + +static int ovl_change_flags(struct file *file, unsigned int flags) +{ + struct inode *inode = file_inode(file); + int err; + + /* No atime modificaton on underlying */ + flags |= O_NOATIME; + + /* If some flag changed that cannot be changed then something's amiss */ + if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK)) + return -EIO; + + flags &= OVL_SETFL_MASK; + + if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode)) + return -EPERM; + + if (flags & O_DIRECT) { + if (!file->f_mapping->a_ops || + !file->f_mapping->a_ops->direct_IO) + return -EINVAL; + } + + if (file->f_op->check_flags) { + err = file->f_op->check_flags(flags); + if (err) + return err; + } + + spin_lock(&file->f_lock); + file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags; + spin_unlock(&file->f_lock); + + return 0; +} + +static int ovl_real_file(const struct file *file, struct fd *real) +{ + struct inode *inode = file_inode(file); + + real->flags = 0; + real->file = file->private_data; + + /* Has it been copied up since we'd opened it? */ + if (unlikely(file_inode(real->file) != ovl_inode_real(inode))) { + real->flags = FDPUT_FPUT; + real->file = ovl_open_realfile(file); + + return PTR_ERR_OR_ZERO(real->file); + } + + /* Did the flags change since open? */ + if (unlikely((file->f_flags ^ real->file->f_flags) & ~O_NOATIME)) + return ovl_change_flags(real->file, file->f_flags); + + return 0; +} + static int ovl_open(struct inode *inode, struct file *file) { struct dentry *dentry = file_dentry(file); -- 2.14.3