Received: by 10.192.165.156 with SMTP id m28csp2090590imm; Thu, 12 Apr 2018 08:27:29 -0700 (PDT) X-Google-Smtp-Source: AIpwx4+s0IeeORwycodfIMBt5YjyylQyNnjQYPpeao2e5oOAYBPFkAVaHNmJjoOYtRM2M2CNfDVU X-Received: by 10.98.70.199 with SMTP id o68mr7990713pfi.169.1523546849792; Thu, 12 Apr 2018 08:27:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1523546849; cv=none; d=google.com; s=arc-20160816; b=rrRx1eikTr7TVXwetlI3SGOaw51kIuUKpA9ccagt+PSX/PeDvU+7cOSO5GT18Vc9eu nKHZSWm6EDPhxBXNEneyAUVaxbTuEB34mnb89kn0XJ/WxdRM0iKAuoVAz3nuc/77eg4W B5Uyc5CkyB3c7nmponeVvA1+/waiETHC4xAWlqiRtIHb8WMmlYjVkBenvjhNNxHjfbu/ Z7MFcEI6Xc3c3AmIhBssCmsI8QdebxBXSulFD/TaFe5Iz7OyWlgZozXhfSRfhcG3Lv0L iUPYQmJ4MivMFUr3o790/prV5j8dCl0jJuvjK/kri9gU9gvHhGLaCXVjrJbESzqYDqzF emLg== 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=Q9fLNE+M6ErT2yrbe3T//zaIzsp+ursvLw/9HAvWhTE=; b=bqGljxuXEcFBo6qsaN9BN1E8KqKv2saB+Dib6V/JhuP8kuBYSkxY9VN5255zlKy4ig pNXJDJ9VbuKdtsL0GYzhNY13YQit6Lzy04BtSyn2bMVdhTPUq2Miz99YStOQcuNkGRVO WI2QdyOctFj9rNLaWgrHV+pZlLGCgF0VjNSrAdkAiPVBtaEfXcM3Xt1yI+O6IXT8ti6D Kb36tMiV5PdrYlokBsmTnV13z4JEfjfxSOaACeL3gteJ5tje93RFJ3HNw/T0CbnT3B8Z h5TF1Dfp4u6Rwz65l8Fc3vOCDlS6rxF3ZpuMFfEEzq4H1iVg7/RuRSG+XBnZNNP/eO6c Kakg== 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 v7-v6si3542234plp.276.2018.04.12.08.26.52; Thu, 12 Apr 2018 08:27:29 -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 S1754267AbeDLPVt (ORCPT + 99 others); Thu, 12 Apr 2018 11:21:49 -0400 Received: from mail-wr0-f196.google.com ([209.85.128.196]:34317 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753316AbeDLPIn (ORCPT ); Thu, 12 Apr 2018 11:08:43 -0400 Received: by mail-wr0-f196.google.com with SMTP id d19so5473702wre.1 for ; Thu, 12 Apr 2018 08:08:42 -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=Q9fLNE+M6ErT2yrbe3T//zaIzsp+ursvLw/9HAvWhTE=; b=IxNU8G5P81qrM4kA74x+/1t0Q0/y2Y+IZwjNDJXdpONTfOcrWOHDoZ9lgv182eVoyv lNFFJqE/FSfVFiEcm33QoKLxcO9qoRPgvL6rK5sqGeIlU004+SbwXxteTf6Hd+EShNtn iLu+VKiMEWfs7tF0lUlFYK6UAW5JHR/K0vU+Z8jIAXxlMCtyBN6+dfEqtilCO18Uh/nZ +a8pYmYLdKFqUQvdpWkq1W5LAoFer8YU/eJ3kZtCbKWsus+7xOE6D3DxvdZBKmrqkWDv OVA7dKwrSOznD4AwE1uTt/uQsVhz+HveCVKAtKqLzOiSsaIXTWl2+ieGR3HDNBSQRg18 asrA== X-Gm-Message-State: ALQs6tAgyHrKh/3fQn9rcV7bidP7rPJnKPqpU2FoJvvrit6FcFW0xzYF IP7w4HhrzEwYpg65HPeAkFCx/TkGLaA= X-Received: by 10.223.141.144 with SMTP id o16mr216579wrb.81.1523545722174; Thu, 12 Apr 2018 08:08:42 -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.41 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 12 Apr 2018 08:08:41 -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 09/35] ovl: stack file ops Date: Thu, 12 Apr 2018 17:08:00 +0200 Message-Id: <20180412150826.20988-10-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 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 e213bb9823ec..3b996639e1ad 100644 --- a/fs/overlayfs/inode.c +++ b/fs/overlayfs/inode.c @@ -542,6 +542,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 10f5f3bf9d96..b29c1688f372 100644 --- a/fs/overlayfs/overlayfs.h +++ b/fs/overlayfs/overlayfs.h @@ -381,6 +381,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); -- 2.14.3