From patchwork Mon May 7 08:37:43 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Miklos Szeredi X-Patchwork-Id: 10383607 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 1DD8560353 for ; Mon, 7 May 2018 08:47:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 12313286DF for ; Mon, 7 May 2018 08:47:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 06BDF28A96; Mon, 7 May 2018 08:47:37 +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=ham 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 7D23128823 for ; Mon, 7 May 2018 08:47:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752499AbeEGIrd (ORCPT ); Mon, 7 May 2018 04:47:33 -0400 Received: from mail-wm0-f67.google.com ([74.125.82.67]:54681 "EHLO mail-wm0-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752129AbeEGIi1 (ORCPT ); Mon, 7 May 2018 04:38:27 -0400 Received: by mail-wm0-f67.google.com with SMTP id f6so12027582wmc.4 for ; Mon, 07 May 2018 01:38:26 -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=nyDOuW1GfPZQ1GZQbmZzRzhKXJNNA2S5T6nZHJoQFag=; b=XLFlFvR1edRQvQNgEwB08SV0oT22g2i7vpGltdhi74BfQgjrVqU0QhUD+Eg54m/NSq FLGwvVMYkP9dGm01Y7mHb3+UUc/B3kzphoxC19KCaqIXzuDg6Ht114hftMpN0++96TBP ITQLKHCmDFm7SrO0XIQVV0S883gdlKIjENr3TycKYwudoK1QbCpJi1lbC88OpFL2JY8v 4NncUTjDeTy3I55K/thqneL03ClK/ynhqPj1jJRJtSBoIDm2WSnBs/EozcC/2nsrNttD xyxO9bJovv5hObVOYaNNN69riSb3vBRMge0ZVYGyZErYD9UI6BZbo0JDatWabWrzny53 voBA== X-Gm-Message-State: ALKqPwdr3y8mnPA2n+WpzZshzHeHVrNzw0pvkH3yBNp9oSuW8O+hkQor PB8iZK+Am2xTFw6qdTBz1L2tQA== X-Google-Smtp-Source: AB8JxZp/dGd+H7MdCID0GFouLxSzRnk4liAmi36vCNiMyZ4HpJkI26tvMQEAdbdS355Ryy+8H9+HEQ== X-Received: by 2002:a1c:1082:: with SMTP id 124-v6mr171960wmq.46.1525682305792; Mon, 07 May 2018 01:38:25 -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.24 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 07 May 2018 01:38:25 -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 11/35] ovl: stack file ops Date: Mon, 7 May 2018 10:37:43 +0200 Message-Id: <20180507083807.28792-12-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 Implement file operations on a regular overlay file. The underlying file is opened separately and cached in ->private_data. It might be worth making an exception for such files when accounting in nr_file to confirm to userspace expectations. We are only adding a small overhead (248bytes for the struct file) since the real inode and dentry are pinned by overlayfs anyway. This patch doesn't have any effect, since the vfs will use d_real() to find the real underlying file to open. The patch at the end of the series will actually enable this functionality. Signed-off-by: Miklos Szeredi --- fs/overlayfs/Makefile | 4 +-- fs/overlayfs/file.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++ fs/overlayfs/inode.c | 1 + fs/overlayfs/overlayfs.h | 3 ++ 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 fs/overlayfs/file.c diff --git a/fs/overlayfs/Makefile b/fs/overlayfs/Makefile index 30802347a020..46e1ff8ac056 100644 --- a/fs/overlayfs/Makefile +++ b/fs/overlayfs/Makefile @@ -4,5 +4,5 @@ obj-$(CONFIG_OVERLAY_FS) += overlay.o -overlay-objs := super.o namei.o util.o inode.o dir.o readdir.o copy_up.o \ - export.o +overlay-objs := super.o namei.o util.o inode.o file.o dir.o readdir.o \ + copy_up.o export.o diff --git a/fs/overlayfs/file.c b/fs/overlayfs/file.c new file mode 100644 index 000000000000..a0b606885c41 --- /dev/null +++ b/fs/overlayfs/file.c @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2017 Red Hat, Inc. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 as published by + * the Free Software Foundation. + */ + +#include +#include +#include +#include "overlayfs.h" + +static struct file *ovl_open_realfile(const struct file *file) +{ + struct inode *inode = file_inode(file); + struct inode *upperinode = ovl_inode_upper(inode); + struct inode *realinode = upperinode ?: ovl_inode_lower(inode); + struct file *realfile; + const struct cred *old_cred; + + old_cred = ovl_override_creds(inode->i_sb); + realfile = path_open(&file->f_path, file->f_flags | O_NOATIME, + realinode, current_cred(), false); + revert_creds(old_cred); + + pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n", + file, file, upperinode ? 'u' : 'l', file->f_flags, + realfile, IS_ERR(realfile) ? 0 : realfile->f_flags); + + return realfile; +} + +static int ovl_open(struct inode *inode, struct file *file) +{ + struct dentry *dentry = file_dentry(file); + struct file *realfile; + int err; + + err = ovl_open_maybe_copy_up(dentry, file->f_flags); + if (err) + return err; + + /* No longer need these flags, so don't pass them on to underlying fs */ + file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); + + realfile = ovl_open_realfile(file); + if (IS_ERR(realfile)) + return PTR_ERR(realfile); + + file->private_data = realfile; + + return 0; +} + +static int ovl_release(struct inode *inode, struct file *file) +{ + fput(file->private_data); + + return 0; +} + +static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) +{ + struct inode *realinode = ovl_inode_real(file_inode(file)); + + return generic_file_llseek_size(file, offset, whence, + realinode->i_sb->s_maxbytes, + i_size_read(realinode)); +} + +const struct file_operations ovl_file_operations = { + .open = ovl_open, + .release = ovl_release, + .llseek = ovl_llseek, +}; diff --git a/fs/overlayfs/inode.c b/fs/overlayfs/inode.c index ba3f832cc39a..a228eda66ad8 100644 --- a/fs/overlayfs/inode.c +++ b/fs/overlayfs/inode.c @@ -535,6 +535,7 @@ static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev, switch (mode & S_IFMT) { case S_IFREG: inode->i_op = &ovl_file_inode_operations; + inode->i_fop = &ovl_file_operations; break; case S_IFDIR: diff --git a/fs/overlayfs/overlayfs.h b/fs/overlayfs/overlayfs.h index b9f7c632ab9c..edbf69c8f45d 100644 --- a/fs/overlayfs/overlayfs.h +++ b/fs/overlayfs/overlayfs.h @@ -374,6 +374,9 @@ int ovl_create_real(struct inode *dir, struct dentry *newdentry, struct dentry *hardlink, bool debug); int ovl_cleanup(struct inode *dir, struct dentry *dentry); +/* file.c */ +extern const struct file_operations ovl_file_operations; + /* copy_up.c */ int ovl_copy_up(struct dentry *dentry); int ovl_copy_up_flags(struct dentry *dentry, int flags);