From patchwork Thu Jul 21 13:53:36 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 9241725 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 4166460574 for ; Thu, 21 Jul 2016 13:54:39 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 32E171FF2F for ; Thu, 21 Jul 2016 13:54:39 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 27E3327E22; Thu, 21 Jul 2016 13:54:39 +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=-6.9 required=2.0 tests=BAYES_00,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 C904D1FF2F for ; Thu, 21 Jul 2016 13:54:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751858AbcGUNyR (ORCPT ); Thu, 21 Jul 2016 09:54:17 -0400 Received: from mail-wm0-f54.google.com ([74.125.82.54]:36676 "EHLO mail-wm0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751853AbcGUNxo (ORCPT ); Thu, 21 Jul 2016 09:53:44 -0400 Received: by mail-wm0-f54.google.com with SMTP id q128so22763968wma.1 for ; Thu, 21 Jul 2016 06:53:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=jZpvMT2Osutjru40F8ukJYxW3EYa6fkH2UXOJbkG7WY=; b=CWiXki3Fn4EI7WmF1XXOgc9lJdDH1lCYMMc87VTX2CV8Ke+aAhzGUmmd9B+y5Q/o0b N2Xdf2OgdJNnEzCXnRoLnTDXRWTOOR0JHYlTchQZjyaL81N4vOV0p3DLc9hs7vhUNPtF g2YxCDHI67Xkft9of5u3wOcXMCzwN5bNMzEtTcTMuyMYu/EzOyV3RlY0S6zL8AQJI3F2 xbG73jSs1N/ik3vlwsYOCVR4FWXd1y1GkDSIV+TZ9M87DBG1NjytCECWkshsgnLTKR6W k1OMzowlG6evJ49F6YKWo/Kfqq4zvuQQduFFYjHVnTfaTxf5kgj7Pl+rqXfXiwIQNQtC Kxmw== X-Gm-Message-State: ALyK8tIIw7o9HB58uKXvIQ6uhi+Z4MILY/tVFfeAhKdt5dl3XdXa+XkDqYgA3hLc/LkLH48G X-Received: by 10.28.168.206 with SMTP id r197mr17512982wme.15.1469109222897; Thu, 21 Jul 2016 06:53:42 -0700 (PDT) Received: from veci.piliscsaba.szeredi.hu (pool-dsl-2c-0018.externet.hu. [217.173.44.24]) by smtp.gmail.com with ESMTPSA id g184sm4260615wme.15.2016.07.21.06.53.41 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 21 Jul 2016 06:53:42 -0700 (PDT) From: Miklos Szeredi To: Jeff Layton , "J. Bruce Fields" , Al Viro Cc: linux-unionfs@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [RFC PATCH v2 3/3] vfs: do get_write_access() on upper layer of overlayfs Date: Thu, 21 Jul 2016 15:53:36 +0200 Message-Id: <1469109216-24353-4-git-send-email-mszeredi@redhat.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1469109216-24353-1-git-send-email-mszeredi@redhat.com> References: <1469109216-24353-1-git-send-email-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 The problem with writecount is: we want consistent handling of it for underlying filesystems as well as overlayfs. Making sure i_writecount is correct on all layers is difficult. Instead this patch makes sure that when write access is acquired, it's always done on the underlying writable layer (called the upper layer). We must also make sure to look at the writecount on this layer when checking for conflicting leases. Open for write already updates the upper layer's writecount. Leaving only truncate. For truncate copy up must happen before get_write_access() so that the writecount is updated on the upper layer. Problem with this is if something fails after that, then copy-up was done needlessly. E.g. if break_lease() was interrupted. Probably not a big deal in practice. Another interesting case is if there's a denywrite on a lower file that is then opened for write or truncated. With this patch these will succeed, which is somewhat counterintuitive. But I think it's still acceptable, considering that the copy-up does actually create a different file, so the old, denywrite mapping won't be touched. On non-overlayfs d_real() is an identity function and d_real_inode() is equivalent to d_inode() so this patch doesn't change behavior in that case. Signed-off-by: Miklos Szeredi --- fs/locks.c | 3 ++- fs/open.c | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/fs/locks.c b/fs/locks.c index c1656cff53ee..b242d5b99589 100644 --- a/fs/locks.c +++ b/fs/locks.c @@ -1618,7 +1618,8 @@ check_conflicting_open(const struct dentry *dentry, const long arg, int flags) if (flags & FL_LAYOUT) return 0; - if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0)) + if ((arg == F_RDLCK) && + (atomic_read(&d_real_inode(dentry)->i_writecount) > 0)) return -EAGAIN; if ((arg == F_WRLCK) && ((d_count(dentry) > 1) || diff --git a/fs/open.c b/fs/open.c index 451fed14a843..76d8d97b5136 100644 --- a/fs/open.c +++ b/fs/open.c @@ -68,6 +68,7 @@ int do_truncate(struct dentry *dentry, loff_t length, unsigned int time_attrs, long vfs_truncate(const struct path *path, loff_t length) { struct inode *inode; + struct dentry *upperdentry; long error; inode = path->dentry->d_inode; @@ -90,7 +91,17 @@ long vfs_truncate(const struct path *path, loff_t length) if (IS_APPEND(inode)) goto mnt_drop_write_and_out; - error = get_write_access(inode); + /* + * If this is an overlayfs then do as if opening the file so we get + * write access on the upper inode, not on the overlay inode. For + * non-overlay filesystems d_real() is an identity function. + */ + upperdentry = d_real(path->dentry, NULL, O_WRONLY); + error = PTR_ERR(upperdentry); + if (IS_ERR(upperdentry)) + goto mnt_drop_write_and_out; + + error = get_write_access(upperdentry->d_inode); if (error) goto mnt_drop_write_and_out; @@ -109,7 +120,7 @@ long vfs_truncate(const struct path *path, loff_t length) error = do_truncate(path->dentry, length, 0, NULL); put_write_and_out: - put_write_access(inode); + put_write_access(upperdentry->d_inode); mnt_drop_write_and_out: mnt_drop_write(path->mnt); out: