From patchwork Mon May 7 08:37:44 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 10383617 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D236D60353 for ; Mon, 7 May 2018 08:48:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C651B28B60 for ; Mon, 7 May 2018 08:48:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BB58828B64; Mon, 7 May 2018 08:48:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 68FDC28B60 for ; Mon, 7 May 2018 08:48:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752428AbeEGIrb (ORCPT ); Mon, 7 May 2018 04:47:31 -0400 Received: from mail-wm0-f66.google.com ([74.125.82.66]:50269 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752132AbeEGIi2 (ORCPT ); Mon, 7 May 2018 04:38:28 -0400 Received: by mail-wm0-f66.google.com with SMTP id t11so12072874wmt.0 for ; Mon, 07 May 2018 01:38:27 -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=LwsJTSUT2RemjBhJkpSIUg+cLBfUDr1+MvgYud7NBTY=; b=l0lbd/gEsn19u1xxwtdueWaeBchX/dFq9aUzN+wftYp+AilsVWpCpLPs94WASViRmf FL5TNcNKzoYsNk5WJqHQLO2jduFYV6Tm2amjd7FRFO/Jc/QYP9KtR7krAfHTdhZATPpc h6BuzpdV2ieX6v6UMYu0ZG3S+O25QGtAULmdjmO4KBJZV763gVC1vMJ+pRPkDcTifE6o IEo+2aCk05i3mawC/w2IJ2mk++VxZBwn4N3A3wA4P7631h5qJcDmWUmYsCMt+X30DflR Ksgl8qQZH7+Y+nyW+OnTypNJnfYWx3WF/zbRo2/BkeEwQ+6JjKZ2SyWKxJvE3PxxpMo1 uczw== X-Gm-Message-State: ALQs6tA01p0In8k/nAxUSeSyimBvPeLbtSH+iNZMXDabsId6UO2bFvKo jh6RsjB3z5wiMMNcMhHjD6fUzQ== X-Google-Smtp-Source: AB8JxZpbS5BZYoDzNvnpLVtVXEMJMMjgWYgQGffsi4mMCpdivNGqGmIkGAqBJ3lcSoch/AEIVUtCFw== X-Received: by 2002:a1c:e696:: with SMTP id e22-v6mr170908wmi.118.1525682306944; Mon, 07 May 2018 01:38:26 -0700 (PDT) Received: from veci.piliscsaba.redhat.com (C2B0E321.catv.pool.telekom.hu. [194.176.227.33]) by smtp.gmail.com with ESMTPSA id h8-v6sm5908050wmc.16.2018.05.07.01.38.25 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 May 2018 01:38:26 -0700 (PDT) From: Miklos Szeredi To: linux-unionfs@vger.kernel.org Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 12/35] ovl: add helper to return real file Date: Mon, 7 May 2018 10:37:44 +0200 Message-Id: <20180507083807.28792-13-mszeredi@redhat.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180507083807.28792-1-mszeredi@redhat.com> References: <20180507083807.28792-1-mszeredi@redhat.com> Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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..db8778e7c37a 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_fdget(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);